blob: 22f220859b462e9d1c8c0caaaed2a70e3c9d1805 [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
3732static int gaudi_init_cpu(struct hl_device *hdev)
3733{
3734 struct gaudi_device *gaudi = hdev->asic_specific;
3735 int rc;
3736
Ofir Bitton6a2f5d72021-02-15 13:23:04 +02003737 if (!(hdev->fw_components & FW_TYPE_PREBOOT_CPU))
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03003738 return 0;
3739
3740 if (gaudi->hw_cap_initialized & HW_CAP_CPU)
3741 return 0;
3742
3743 /*
3744 * The device CPU works with 40 bits addresses.
3745 * This register sets the extension to 50 bits.
3746 */
Ofir Bittonc692dec2020-10-04 17:34:37 +03003747 if (hdev->asic_prop.fw_security_disabled)
3748 WREG32(mmCPU_IF_CPU_MSB_ADDR, hdev->cpu_pci_msb_addr);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03003749
3750 rc = hl_fw_init_cpu(hdev, mmPSOC_GLOBAL_CONF_CPU_BOOT_STATUS,
3751 mmPSOC_GLOBAL_CONF_KMD_MSG_TO_CPU,
3752 mmCPU_CMD_STATUS_TO_HOST,
Ofir Bitton323b7262020-10-04 09:09:19 +03003753 mmCPU_BOOT_DEV_STS0, mmCPU_BOOT_ERR0,
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03003754 !hdev->bmc_enable, GAUDI_CPU_TIMEOUT_USEC,
3755 GAUDI_BOOT_FIT_REQ_TIMEOUT_USEC);
3756
3757 if (rc)
3758 return rc;
3759
3760 gaudi->hw_cap_initialized |= HW_CAP_CPU;
3761
3762 return 0;
3763}
3764
3765static int gaudi_init_cpu_queues(struct hl_device *hdev, u32 cpu_timeout)
3766{
3767 struct gaudi_device *gaudi = hdev->asic_specific;
Ohad Sharabi5b6b7802021-02-02 13:33:34 +02003768 struct asic_fixed_properties *prop = &hdev->asic_prop;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03003769 struct hl_eq *eq;
3770 u32 status;
3771 struct hl_hw_queue *cpu_pq =
3772 &hdev->kernel_queues[GAUDI_QUEUE_ID_CPU_PQ];
3773 int err;
3774
3775 if (!hdev->cpu_queues_enable)
3776 return 0;
3777
3778 if (gaudi->hw_cap_initialized & HW_CAP_CPU_Q)
3779 return 0;
3780
3781 eq = &hdev->event_queue;
3782
3783 WREG32(mmCPU_IF_PQ_BASE_ADDR_LOW, lower_32_bits(cpu_pq->bus_address));
3784 WREG32(mmCPU_IF_PQ_BASE_ADDR_HIGH, upper_32_bits(cpu_pq->bus_address));
3785
3786 WREG32(mmCPU_IF_EQ_BASE_ADDR_LOW, lower_32_bits(eq->bus_address));
3787 WREG32(mmCPU_IF_EQ_BASE_ADDR_HIGH, upper_32_bits(eq->bus_address));
3788
3789 WREG32(mmCPU_IF_CQ_BASE_ADDR_LOW,
3790 lower_32_bits(hdev->cpu_accessible_dma_address));
3791 WREG32(mmCPU_IF_CQ_BASE_ADDR_HIGH,
3792 upper_32_bits(hdev->cpu_accessible_dma_address));
3793
3794 WREG32(mmCPU_IF_PQ_LENGTH, HL_QUEUE_SIZE_IN_BYTES);
3795 WREG32(mmCPU_IF_EQ_LENGTH, HL_EQ_SIZE_IN_BYTES);
3796 WREG32(mmCPU_IF_CQ_LENGTH, HL_CPU_ACCESSIBLE_MEM_SIZE);
3797
3798 /* Used for EQ CI */
3799 WREG32(mmCPU_IF_EQ_RD_OFFS, 0);
3800
3801 WREG32(mmCPU_IF_PF_PQ_PI, 0);
3802
3803 if (gaudi->multi_msi_mode)
3804 WREG32(mmCPU_IF_QUEUE_INIT, PQ_INIT_STATUS_READY_FOR_CP);
3805 else
3806 WREG32(mmCPU_IF_QUEUE_INIT,
3807 PQ_INIT_STATUS_READY_FOR_CP_SINGLE_MSI);
3808
3809 WREG32(mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR, GAUDI_EVENT_PI_UPDATE);
3810
3811 err = hl_poll_timeout(
3812 hdev,
3813 mmCPU_IF_QUEUE_INIT,
3814 status,
3815 (status == PQ_INIT_STATUS_READY_FOR_HOST),
3816 1000,
3817 cpu_timeout);
3818
3819 if (err) {
3820 dev_err(hdev->dev,
Oded Gabbay6138bbe2020-09-04 20:18:16 +03003821 "Failed to communicate with Device CPU (CPU-CP timeout)\n");
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03003822 return -EIO;
3823 }
3824
Ohad Sharabi5b6b7802021-02-02 13:33:34 +02003825 /* update FW application security bits */
3826 if (prop->fw_security_status_valid)
3827 prop->fw_app_security_map = RREG32(mmCPU_BOOT_DEV_STS0);
3828
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03003829 gaudi->hw_cap_initialized |= HW_CAP_CPU_Q;
3830 return 0;
3831}
3832
3833static void gaudi_pre_hw_init(struct hl_device *hdev)
3834{
3835 /* Perform read from the device to make sure device is up */
Oded Gabbay377182a2020-12-09 19:50:46 +02003836 RREG32(mmHW_STATE);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03003837
Ofir Bittonc692dec2020-10-04 17:34:37 +03003838 if (hdev->asic_prop.fw_security_disabled) {
3839 /* Set the access through PCI bars (Linux driver only) as
3840 * secured
3841 */
3842 WREG32(mmPCIE_WRAP_LBW_PROT_OVR,
3843 (PCIE_WRAP_LBW_PROT_OVR_RD_EN_MASK |
3844 PCIE_WRAP_LBW_PROT_OVR_WR_EN_MASK));
Oded Gabbay57799ce2020-09-13 15:51:28 +03003845
Ofir Bittonc692dec2020-10-04 17:34:37 +03003846 /* Perform read to flush the waiting writes to ensure
3847 * configuration was set in the device
3848 */
3849 RREG32(mmPCIE_WRAP_LBW_PROT_OVR);
3850 }
Oded Gabbay57799ce2020-09-13 15:51:28 +03003851
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03003852 /*
3853 * Let's mark in the H/W that we have reached this point. We check
3854 * this value in the reset_before_init function to understand whether
3855 * we need to reset the chip before doing H/W init. This register is
3856 * cleared by the H/W upon H/W reset
3857 */
3858 WREG32(mmHW_STATE, HL_DEVICE_HW_STATE_DIRTY);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03003859}
3860
3861static int gaudi_hw_init(struct hl_device *hdev)
3862{
3863 int rc;
3864
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03003865 gaudi_pre_hw_init(hdev);
3866
3867 gaudi_init_pci_dma_qmans(hdev);
3868
3869 gaudi_init_hbm_dma_qmans(hdev);
3870
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03003871 rc = gaudi_init_cpu(hdev);
3872 if (rc) {
3873 dev_err(hdev->dev, "failed to initialize CPU\n");
3874 return rc;
3875 }
3876
Oded Gabbay0024c0942020-12-05 22:55:09 +02003877 /* In case the clock gating was enabled in preboot we need to disable
3878 * it here before touching the MME/TPC registers.
3879 * There is no need to take clk gating mutex because when this function
3880 * runs, no other relevant code can run
3881 */
3882 hdev->asic_funcs->disable_clock_gating(hdev);
3883
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03003884 /* SRAM scrambler must be initialized after CPU is running from HBM */
3885 gaudi_init_scrambler_sram(hdev);
3886
3887 /* This is here just in case we are working without CPU */
3888 gaudi_init_scrambler_hbm(hdev);
3889
3890 gaudi_init_golden_registers(hdev);
3891
3892 rc = gaudi_mmu_init(hdev);
3893 if (rc)
3894 return rc;
3895
Omer Shpigelman3a3a5bf12020-05-11 10:45:12 +03003896 gaudi_init_security(hdev);
3897
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03003898 gaudi_init_mme_qmans(hdev);
3899
3900 gaudi_init_tpc_qmans(hdev);
3901
Oded Gabbay3c681572020-11-02 21:10:39 +02003902 gaudi_init_nic_qmans(hdev);
3903
Oded Gabbaye38bfd32020-07-03 20:46:12 +03003904 hdev->asic_funcs->set_clock_gating(hdev);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03003905
3906 gaudi_enable_timestamp(hdev);
3907
Oded Gabbay3c681572020-11-02 21:10:39 +02003908 /* MSI must be enabled before CPU queues and NIC are initialized */
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03003909 rc = gaudi_enable_msi(hdev);
3910 if (rc)
3911 goto disable_queues;
3912
3913 /* must be called after MSI was enabled */
3914 rc = gaudi_init_cpu_queues(hdev, GAUDI_CPU_TIMEOUT_USEC);
3915 if (rc) {
3916 dev_err(hdev->dev, "failed to initialize CPU H/W queues %d\n",
3917 rc);
3918 goto disable_msi;
3919 }
3920
3921 /* Perform read from the device to flush all configuration */
Oded Gabbay377182a2020-12-09 19:50:46 +02003922 RREG32(mmHW_STATE);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03003923
3924 return 0;
3925
3926disable_msi:
3927 gaudi_disable_msi(hdev);
3928disable_queues:
3929 gaudi_disable_mme_qmans(hdev);
3930 gaudi_disable_pci_dma_qmans(hdev);
3931
3932 return rc;
3933}
3934
3935static void gaudi_hw_fini(struct hl_device *hdev, bool hard_reset)
3936{
3937 struct gaudi_device *gaudi = hdev->asic_specific;
Igor Grinbergb726a2f2020-10-29 14:06:54 +02003938 u32 status, reset_timeout_ms, cpu_timeout_ms;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03003939
Oded Gabbay12ae3132020-07-03 20:58:23 +03003940 if (!hard_reset) {
3941 dev_err(hdev->dev, "GAUDI doesn't support soft-reset\n");
3942 return;
3943 }
3944
Oded Gabbayc83c4172020-07-05 15:48:34 +03003945 if (hdev->pldm) {
Oded Gabbay12ae3132020-07-03 20:58:23 +03003946 reset_timeout_ms = GAUDI_PLDM_HRESET_TIMEOUT_MSEC;
Oded Gabbayc83c4172020-07-05 15:48:34 +03003947 cpu_timeout_ms = GAUDI_PLDM_RESET_WAIT_MSEC;
3948 } else {
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03003949 reset_timeout_ms = GAUDI_RESET_TIMEOUT_MSEC;
Oded Gabbayc83c4172020-07-05 15:48:34 +03003950 cpu_timeout_ms = GAUDI_CPU_RESET_WAIT_MSEC;
3951 }
3952
3953 /* Set device to handle FLR by H/W as we will put the device CPU to
3954 * halt mode
3955 */
Oded Gabbaya63c3fb2020-11-26 18:11:05 +02003956 if (hdev->asic_prop.fw_security_disabled &&
3957 !hdev->asic_prop.hard_reset_done_by_fw)
Ofir Bittonb90c8942020-11-08 12:59:04 +02003958 WREG32(mmPCIE_AUX_FLR_CTRL, (PCIE_AUX_FLR_CTRL_HW_CTRL_MASK |
Oded Gabbayc83c4172020-07-05 15:48:34 +03003959 PCIE_AUX_FLR_CTRL_INT_MASK_MASK));
3960
3961 /* I don't know what is the state of the CPU so make sure it is
3962 * stopped in any means necessary
3963 */
Ofir Bitton9c9013c2020-12-01 10:39:54 +02003964 if (hdev->asic_prop.hard_reset_done_by_fw)
3965 WREG32(mmPSOC_GLOBAL_CONF_KMD_MSG_TO_CPU, KMD_MSG_RST_DEV);
3966 else
3967 WREG32(mmPSOC_GLOBAL_CONF_KMD_MSG_TO_CPU, KMD_MSG_GOTO_WFE);
Ofir Bittonb90c8942020-11-08 12:59:04 +02003968
Oded Gabbayc83c4172020-07-05 15:48:34 +03003969 WREG32(mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR, GAUDI_EVENT_HALT_MACHINE);
3970
Oded Gabbaya63c3fb2020-11-26 18:11:05 +02003971 if (hdev->asic_prop.fw_security_disabled &&
3972 !hdev->asic_prop.hard_reset_done_by_fw) {
3973
3974 /* Configure the reset registers. Must be done as early as
3975 * possible in case we fail during H/W initialization
3976 */
3977 WREG32(mmPSOC_GLOBAL_CONF_SOFT_RST_CFG_H,
3978 (CFG_RST_H_DMA_MASK |
3979 CFG_RST_H_MME_MASK |
3980 CFG_RST_H_SM_MASK |
3981 CFG_RST_H_TPC_7_MASK));
3982
3983 WREG32(mmPSOC_GLOBAL_CONF_SOFT_RST_CFG_L, CFG_RST_L_TPC_MASK);
3984
3985 WREG32(mmPSOC_GLOBAL_CONF_SW_ALL_RST_CFG_H,
3986 (CFG_RST_H_HBM_MASK |
3987 CFG_RST_H_TPC_7_MASK |
3988 CFG_RST_H_NIC_MASK |
3989 CFG_RST_H_SM_MASK |
3990 CFG_RST_H_DMA_MASK |
3991 CFG_RST_H_MME_MASK |
3992 CFG_RST_H_CPU_MASK |
3993 CFG_RST_H_MMU_MASK));
3994
3995 WREG32(mmPSOC_GLOBAL_CONF_SW_ALL_RST_CFG_L,
3996 (CFG_RST_L_IF_MASK |
3997 CFG_RST_L_PSOC_MASK |
3998 CFG_RST_L_TPC_MASK));
3999
Ofir Bittonb90c8942020-11-08 12:59:04 +02004000 msleep(cpu_timeout_ms);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03004001
Ofir Bittonb90c8942020-11-08 12:59:04 +02004002 /* Tell ASIC not to re-initialize PCIe */
4003 WREG32(mmPREBOOT_PCIE_EN, LKD_HARD_RESET_MAGIC);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03004004
Ofir Bittonb90c8942020-11-08 12:59:04 +02004005 /* Restart BTL/BLR upon hard-reset */
4006 if (hdev->asic_prop.fw_security_disabled)
4007 WREG32(mmPSOC_GLOBAL_CONF_BOOT_SEQ_RE_START, 1);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03004008
Ofir Bittonb90c8942020-11-08 12:59:04 +02004009 WREG32(mmPSOC_GLOBAL_CONF_SW_ALL_RST,
Oded Gabbay12ae3132020-07-03 20:58:23 +03004010 1 << PSOC_GLOBAL_CONF_SW_ALL_RST_IND_SHIFT);
Ofir Bittonb90c8942020-11-08 12:59:04 +02004011
Oded Gabbay13d0ee12020-12-06 23:48:45 +02004012 dev_info(hdev->dev,
4013 "Issued HARD reset command, going to wait %dms\n",
4014 reset_timeout_ms);
4015 } else {
4016 dev_info(hdev->dev,
4017 "Firmware performs HARD reset, going to wait %dms\n",
4018 reset_timeout_ms);
4019 }
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03004020
4021 /*
4022 * After hard reset, we can't poll the BTM_FSM register because the PSOC
4023 * itself is in reset. Need to wait until the reset is deasserted
4024 */
4025 msleep(reset_timeout_ms);
4026
4027 status = RREG32(mmPSOC_GLOBAL_CONF_BTM_FSM);
4028 if (status & PSOC_GLOBAL_CONF_BTM_FSM_STATE_MASK)
4029 dev_err(hdev->dev,
4030 "Timeout while waiting for device to reset 0x%x\n",
4031 status);
4032
farah kassabrieb10b892020-10-14 15:17:36 +03004033 if (gaudi) {
4034 gaudi->hw_cap_initialized &= ~(HW_CAP_CPU | HW_CAP_CPU_Q |
4035 HW_CAP_HBM | HW_CAP_PCI_DMA |
4036 HW_CAP_MME | HW_CAP_TPC_MASK |
4037 HW_CAP_HBM_DMA | HW_CAP_PLL |
4038 HW_CAP_NIC_MASK | HW_CAP_MMU |
4039 HW_CAP_SRAM_SCRAMBLER |
4040 HW_CAP_HBM_SCRAMBLER |
4041 HW_CAP_CLK_GATE);
Oded Gabbaye38bfd32020-07-03 20:46:12 +03004042
farah kassabrieb10b892020-10-14 15:17:36 +03004043 memset(gaudi->events_stat, 0, sizeof(gaudi->events_stat));
4044 }
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03004045}
4046
4047static int gaudi_suspend(struct hl_device *hdev)
4048{
4049 int rc;
4050
Oded Gabbay2f553422020-08-15 16:28:10 +03004051 rc = hl_fw_send_pci_access_msg(hdev, CPUCP_PACKET_DISABLE_PCI_ACCESS);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03004052 if (rc)
4053 dev_err(hdev->dev, "Failed to disable PCI access from CPU\n");
4054
4055 return rc;
4056}
4057
4058static int gaudi_resume(struct hl_device *hdev)
4059{
4060 return gaudi_init_iatu(hdev);
4061}
4062
4063static int gaudi_cb_mmap(struct hl_device *hdev, struct vm_area_struct *vma,
Hillf Danton0db57532020-08-23 07:32:42 +08004064 void *cpu_addr, dma_addr_t dma_addr, size_t size)
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03004065{
4066 int rc;
4067
4068 vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP |
4069 VM_DONTCOPY | VM_NORESERVE;
4070
Oded Gabbaya9d4ef62021-01-11 13:49:38 +02004071 rc = dma_mmap_coherent(hdev->dev, vma, cpu_addr,
4072 (dma_addr - HOST_PHYS_BASE), size);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03004073 if (rc)
Hillf Danton0db57532020-08-23 07:32:42 +08004074 dev_err(hdev->dev, "dma_mmap_coherent error %d", rc);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03004075
4076 return rc;
4077}
4078
4079static void gaudi_ring_doorbell(struct hl_device *hdev, u32 hw_queue_id, u32 pi)
4080{
4081 struct gaudi_device *gaudi = hdev->asic_specific;
4082 u32 db_reg_offset, db_value, dma_qm_offset, q_off;
4083 int dma_id;
4084 bool invalid_queue = false;
4085
4086 switch (hw_queue_id) {
4087 case GAUDI_QUEUE_ID_DMA_0_0...GAUDI_QUEUE_ID_DMA_0_3:
4088 dma_id = gaudi_dma_assignment[GAUDI_PCI_DMA_1];
4089 dma_qm_offset = dma_id * DMA_QMAN_OFFSET;
4090 q_off = dma_qm_offset + (hw_queue_id & 0x3) * 4;
4091 db_reg_offset = mmDMA0_QM_PQ_PI_0 + q_off;
4092 break;
4093
4094 case GAUDI_QUEUE_ID_DMA_1_0...GAUDI_QUEUE_ID_DMA_1_3:
4095 dma_id = gaudi_dma_assignment[GAUDI_PCI_DMA_2];
4096 dma_qm_offset = dma_id * DMA_QMAN_OFFSET;
4097 q_off = dma_qm_offset + (hw_queue_id & 0x3) * 4;
4098 db_reg_offset = mmDMA0_QM_PQ_PI_0 + q_off;
4099 break;
4100
4101 case GAUDI_QUEUE_ID_DMA_2_0...GAUDI_QUEUE_ID_DMA_2_3:
4102 dma_id = gaudi_dma_assignment[GAUDI_HBM_DMA_1];
4103 dma_qm_offset = dma_id * DMA_QMAN_OFFSET;
4104 q_off = dma_qm_offset + ((hw_queue_id - 1) & 0x3) * 4;
4105 db_reg_offset = mmDMA0_QM_PQ_PI_0 + q_off;
4106 break;
4107
4108 case GAUDI_QUEUE_ID_DMA_3_0...GAUDI_QUEUE_ID_DMA_3_3:
4109 dma_id = gaudi_dma_assignment[GAUDI_HBM_DMA_2];
4110 dma_qm_offset = dma_id * DMA_QMAN_OFFSET;
4111 q_off = dma_qm_offset + ((hw_queue_id - 1) & 0x3) * 4;
4112 db_reg_offset = mmDMA0_QM_PQ_PI_0 + q_off;
4113 break;
4114
4115 case GAUDI_QUEUE_ID_DMA_4_0...GAUDI_QUEUE_ID_DMA_4_3:
4116 dma_id = gaudi_dma_assignment[GAUDI_HBM_DMA_3];
4117 dma_qm_offset = dma_id * DMA_QMAN_OFFSET;
4118 q_off = dma_qm_offset + ((hw_queue_id - 1) & 0x3) * 4;
4119 db_reg_offset = mmDMA0_QM_PQ_PI_0 + q_off;
4120 break;
4121
4122 case GAUDI_QUEUE_ID_DMA_5_0...GAUDI_QUEUE_ID_DMA_5_3:
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03004123 dma_id = gaudi_dma_assignment[GAUDI_HBM_DMA_4];
4124 dma_qm_offset = dma_id * DMA_QMAN_OFFSET;
4125 q_off = dma_qm_offset + ((hw_queue_id - 1) & 0x3) * 4;
4126 db_reg_offset = mmDMA0_QM_PQ_PI_0 + q_off;
4127 break;
4128
Ofir Bitton0940cab2020-08-31 08:52:56 +03004129 case GAUDI_QUEUE_ID_DMA_6_0...GAUDI_QUEUE_ID_DMA_6_3:
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03004130 dma_id = gaudi_dma_assignment[GAUDI_HBM_DMA_5];
4131 dma_qm_offset = dma_id * DMA_QMAN_OFFSET;
4132 q_off = dma_qm_offset + ((hw_queue_id - 1) & 0x3) * 4;
4133 db_reg_offset = mmDMA0_QM_PQ_PI_0 + q_off;
4134 break;
4135
Ofir Bitton0940cab2020-08-31 08:52:56 +03004136 case GAUDI_QUEUE_ID_DMA_7_0...GAUDI_QUEUE_ID_DMA_7_3:
4137 dma_id = gaudi_dma_assignment[GAUDI_HBM_DMA_6];
4138 dma_qm_offset = dma_id * DMA_QMAN_OFFSET;
4139 q_off = dma_qm_offset + ((hw_queue_id - 1) & 0x3) * 4;
4140 db_reg_offset = mmDMA0_QM_PQ_PI_0 + q_off;
4141 break;
4142
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03004143 case GAUDI_QUEUE_ID_CPU_PQ:
4144 if (gaudi->hw_cap_initialized & HW_CAP_CPU_Q)
4145 db_reg_offset = mmCPU_IF_PF_PQ_PI;
4146 else
4147 invalid_queue = true;
4148 break;
4149
4150 case GAUDI_QUEUE_ID_MME_0_0:
4151 db_reg_offset = mmMME2_QM_PQ_PI_0;
4152 break;
4153
4154 case GAUDI_QUEUE_ID_MME_0_1:
4155 db_reg_offset = mmMME2_QM_PQ_PI_1;
4156 break;
4157
4158 case GAUDI_QUEUE_ID_MME_0_2:
4159 db_reg_offset = mmMME2_QM_PQ_PI_2;
4160 break;
4161
4162 case GAUDI_QUEUE_ID_MME_0_3:
4163 db_reg_offset = mmMME2_QM_PQ_PI_3;
4164 break;
4165
4166 case GAUDI_QUEUE_ID_MME_1_0:
4167 db_reg_offset = mmMME0_QM_PQ_PI_0;
4168 break;
4169
4170 case GAUDI_QUEUE_ID_MME_1_1:
4171 db_reg_offset = mmMME0_QM_PQ_PI_1;
4172 break;
4173
4174 case GAUDI_QUEUE_ID_MME_1_2:
4175 db_reg_offset = mmMME0_QM_PQ_PI_2;
4176 break;
4177
4178 case GAUDI_QUEUE_ID_MME_1_3:
4179 db_reg_offset = mmMME0_QM_PQ_PI_3;
4180 break;
4181
4182 case GAUDI_QUEUE_ID_TPC_0_0:
4183 db_reg_offset = mmTPC0_QM_PQ_PI_0;
4184 break;
4185
4186 case GAUDI_QUEUE_ID_TPC_0_1:
4187 db_reg_offset = mmTPC0_QM_PQ_PI_1;
4188 break;
4189
4190 case GAUDI_QUEUE_ID_TPC_0_2:
4191 db_reg_offset = mmTPC0_QM_PQ_PI_2;
4192 break;
4193
4194 case GAUDI_QUEUE_ID_TPC_0_3:
4195 db_reg_offset = mmTPC0_QM_PQ_PI_3;
4196 break;
4197
4198 case GAUDI_QUEUE_ID_TPC_1_0:
4199 db_reg_offset = mmTPC1_QM_PQ_PI_0;
4200 break;
4201
4202 case GAUDI_QUEUE_ID_TPC_1_1:
4203 db_reg_offset = mmTPC1_QM_PQ_PI_1;
4204 break;
4205
4206 case GAUDI_QUEUE_ID_TPC_1_2:
4207 db_reg_offset = mmTPC1_QM_PQ_PI_2;
4208 break;
4209
4210 case GAUDI_QUEUE_ID_TPC_1_3:
4211 db_reg_offset = mmTPC1_QM_PQ_PI_3;
4212 break;
4213
4214 case GAUDI_QUEUE_ID_TPC_2_0:
4215 db_reg_offset = mmTPC2_QM_PQ_PI_0;
4216 break;
4217
4218 case GAUDI_QUEUE_ID_TPC_2_1:
4219 db_reg_offset = mmTPC2_QM_PQ_PI_1;
4220 break;
4221
4222 case GAUDI_QUEUE_ID_TPC_2_2:
4223 db_reg_offset = mmTPC2_QM_PQ_PI_2;
4224 break;
4225
4226 case GAUDI_QUEUE_ID_TPC_2_3:
4227 db_reg_offset = mmTPC2_QM_PQ_PI_3;
4228 break;
4229
4230 case GAUDI_QUEUE_ID_TPC_3_0:
4231 db_reg_offset = mmTPC3_QM_PQ_PI_0;
4232 break;
4233
4234 case GAUDI_QUEUE_ID_TPC_3_1:
4235 db_reg_offset = mmTPC3_QM_PQ_PI_1;
4236 break;
4237
4238 case GAUDI_QUEUE_ID_TPC_3_2:
4239 db_reg_offset = mmTPC3_QM_PQ_PI_2;
4240 break;
4241
4242 case GAUDI_QUEUE_ID_TPC_3_3:
4243 db_reg_offset = mmTPC3_QM_PQ_PI_3;
4244 break;
4245
4246 case GAUDI_QUEUE_ID_TPC_4_0:
4247 db_reg_offset = mmTPC4_QM_PQ_PI_0;
4248 break;
4249
4250 case GAUDI_QUEUE_ID_TPC_4_1:
4251 db_reg_offset = mmTPC4_QM_PQ_PI_1;
4252 break;
4253
4254 case GAUDI_QUEUE_ID_TPC_4_2:
4255 db_reg_offset = mmTPC4_QM_PQ_PI_2;
4256 break;
4257
4258 case GAUDI_QUEUE_ID_TPC_4_3:
4259 db_reg_offset = mmTPC4_QM_PQ_PI_3;
4260 break;
4261
4262 case GAUDI_QUEUE_ID_TPC_5_0:
4263 db_reg_offset = mmTPC5_QM_PQ_PI_0;
4264 break;
4265
4266 case GAUDI_QUEUE_ID_TPC_5_1:
4267 db_reg_offset = mmTPC5_QM_PQ_PI_1;
4268 break;
4269
4270 case GAUDI_QUEUE_ID_TPC_5_2:
4271 db_reg_offset = mmTPC5_QM_PQ_PI_2;
4272 break;
4273
4274 case GAUDI_QUEUE_ID_TPC_5_3:
4275 db_reg_offset = mmTPC5_QM_PQ_PI_3;
4276 break;
4277
4278 case GAUDI_QUEUE_ID_TPC_6_0:
4279 db_reg_offset = mmTPC6_QM_PQ_PI_0;
4280 break;
4281
4282 case GAUDI_QUEUE_ID_TPC_6_1:
4283 db_reg_offset = mmTPC6_QM_PQ_PI_1;
4284 break;
4285
4286 case GAUDI_QUEUE_ID_TPC_6_2:
4287 db_reg_offset = mmTPC6_QM_PQ_PI_2;
4288 break;
4289
4290 case GAUDI_QUEUE_ID_TPC_6_3:
4291 db_reg_offset = mmTPC6_QM_PQ_PI_3;
4292 break;
4293
4294 case GAUDI_QUEUE_ID_TPC_7_0:
4295 db_reg_offset = mmTPC7_QM_PQ_PI_0;
4296 break;
4297
4298 case GAUDI_QUEUE_ID_TPC_7_1:
4299 db_reg_offset = mmTPC7_QM_PQ_PI_1;
4300 break;
4301
4302 case GAUDI_QUEUE_ID_TPC_7_2:
4303 db_reg_offset = mmTPC7_QM_PQ_PI_2;
4304 break;
4305
4306 case GAUDI_QUEUE_ID_TPC_7_3:
4307 db_reg_offset = mmTPC7_QM_PQ_PI_3;
4308 break;
4309
Oded Gabbay3c681572020-11-02 21:10:39 +02004310 case GAUDI_QUEUE_ID_NIC_0_0:
4311 db_reg_offset = mmNIC0_QM0_PQ_PI_0;
4312 break;
4313
4314 case GAUDI_QUEUE_ID_NIC_0_1:
4315 db_reg_offset = mmNIC0_QM0_PQ_PI_1;
4316 break;
4317
4318 case GAUDI_QUEUE_ID_NIC_0_2:
4319 db_reg_offset = mmNIC0_QM0_PQ_PI_2;
4320 break;
4321
4322 case GAUDI_QUEUE_ID_NIC_0_3:
4323 db_reg_offset = mmNIC0_QM0_PQ_PI_3;
4324 break;
4325
4326 case GAUDI_QUEUE_ID_NIC_1_0:
4327 db_reg_offset = mmNIC0_QM1_PQ_PI_0;
4328 break;
4329
4330 case GAUDI_QUEUE_ID_NIC_1_1:
4331 db_reg_offset = mmNIC0_QM1_PQ_PI_1;
4332 break;
4333
4334 case GAUDI_QUEUE_ID_NIC_1_2:
4335 db_reg_offset = mmNIC0_QM1_PQ_PI_2;
4336 break;
4337
4338 case GAUDI_QUEUE_ID_NIC_1_3:
4339 db_reg_offset = mmNIC0_QM1_PQ_PI_3;
4340 break;
4341
4342 case GAUDI_QUEUE_ID_NIC_2_0:
4343 db_reg_offset = mmNIC1_QM0_PQ_PI_0;
4344 break;
4345
4346 case GAUDI_QUEUE_ID_NIC_2_1:
4347 db_reg_offset = mmNIC1_QM0_PQ_PI_1;
4348 break;
4349
4350 case GAUDI_QUEUE_ID_NIC_2_2:
4351 db_reg_offset = mmNIC1_QM0_PQ_PI_2;
4352 break;
4353
4354 case GAUDI_QUEUE_ID_NIC_2_3:
4355 db_reg_offset = mmNIC1_QM0_PQ_PI_3;
4356 break;
4357
4358 case GAUDI_QUEUE_ID_NIC_3_0:
4359 db_reg_offset = mmNIC1_QM1_PQ_PI_0;
4360 break;
4361
4362 case GAUDI_QUEUE_ID_NIC_3_1:
4363 db_reg_offset = mmNIC1_QM1_PQ_PI_1;
4364 break;
4365
4366 case GAUDI_QUEUE_ID_NIC_3_2:
4367 db_reg_offset = mmNIC1_QM1_PQ_PI_2;
4368 break;
4369
4370 case GAUDI_QUEUE_ID_NIC_3_3:
4371 db_reg_offset = mmNIC1_QM1_PQ_PI_3;
4372 break;
4373
4374 case GAUDI_QUEUE_ID_NIC_4_0:
4375 db_reg_offset = mmNIC2_QM0_PQ_PI_0;
4376 break;
4377
4378 case GAUDI_QUEUE_ID_NIC_4_1:
4379 db_reg_offset = mmNIC2_QM0_PQ_PI_1;
4380 break;
4381
4382 case GAUDI_QUEUE_ID_NIC_4_2:
4383 db_reg_offset = mmNIC2_QM0_PQ_PI_2;
4384 break;
4385
4386 case GAUDI_QUEUE_ID_NIC_4_3:
4387 db_reg_offset = mmNIC2_QM0_PQ_PI_3;
4388 break;
4389
4390 case GAUDI_QUEUE_ID_NIC_5_0:
4391 db_reg_offset = mmNIC2_QM1_PQ_PI_0;
4392 break;
4393
4394 case GAUDI_QUEUE_ID_NIC_5_1:
4395 db_reg_offset = mmNIC2_QM1_PQ_PI_1;
4396 break;
4397
4398 case GAUDI_QUEUE_ID_NIC_5_2:
4399 db_reg_offset = mmNIC2_QM1_PQ_PI_2;
4400 break;
4401
4402 case GAUDI_QUEUE_ID_NIC_5_3:
4403 db_reg_offset = mmNIC2_QM1_PQ_PI_3;
4404 break;
4405
4406 case GAUDI_QUEUE_ID_NIC_6_0:
4407 db_reg_offset = mmNIC3_QM0_PQ_PI_0;
4408 break;
4409
4410 case GAUDI_QUEUE_ID_NIC_6_1:
4411 db_reg_offset = mmNIC3_QM0_PQ_PI_1;
4412 break;
4413
4414 case GAUDI_QUEUE_ID_NIC_6_2:
4415 db_reg_offset = mmNIC3_QM0_PQ_PI_2;
4416 break;
4417
4418 case GAUDI_QUEUE_ID_NIC_6_3:
4419 db_reg_offset = mmNIC3_QM0_PQ_PI_3;
4420 break;
4421
4422 case GAUDI_QUEUE_ID_NIC_7_0:
4423 db_reg_offset = mmNIC3_QM1_PQ_PI_0;
4424 break;
4425
4426 case GAUDI_QUEUE_ID_NIC_7_1:
4427 db_reg_offset = mmNIC3_QM1_PQ_PI_1;
4428 break;
4429
4430 case GAUDI_QUEUE_ID_NIC_7_2:
4431 db_reg_offset = mmNIC3_QM1_PQ_PI_2;
4432 break;
4433
4434 case GAUDI_QUEUE_ID_NIC_7_3:
4435 db_reg_offset = mmNIC3_QM1_PQ_PI_3;
4436 break;
4437
4438 case GAUDI_QUEUE_ID_NIC_8_0:
4439 db_reg_offset = mmNIC4_QM0_PQ_PI_0;
4440 break;
4441
4442 case GAUDI_QUEUE_ID_NIC_8_1:
4443 db_reg_offset = mmNIC4_QM0_PQ_PI_1;
4444 break;
4445
4446 case GAUDI_QUEUE_ID_NIC_8_2:
4447 db_reg_offset = mmNIC4_QM0_PQ_PI_2;
4448 break;
4449
4450 case GAUDI_QUEUE_ID_NIC_8_3:
4451 db_reg_offset = mmNIC4_QM0_PQ_PI_3;
4452 break;
4453
4454 case GAUDI_QUEUE_ID_NIC_9_0:
4455 db_reg_offset = mmNIC4_QM1_PQ_PI_0;
4456 break;
4457
4458 case GAUDI_QUEUE_ID_NIC_9_1:
4459 db_reg_offset = mmNIC4_QM1_PQ_PI_1;
4460 break;
4461
4462 case GAUDI_QUEUE_ID_NIC_9_2:
4463 db_reg_offset = mmNIC4_QM1_PQ_PI_2;
4464 break;
4465
4466 case GAUDI_QUEUE_ID_NIC_9_3:
4467 db_reg_offset = mmNIC4_QM1_PQ_PI_3;
4468 break;
4469
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03004470 default:
4471 invalid_queue = true;
4472 }
4473
4474 if (invalid_queue) {
4475 /* Should never get here */
4476 dev_err(hdev->dev, "h/w queue %d is invalid. Can't set pi\n",
4477 hw_queue_id);
4478 return;
4479 }
4480
4481 db_value = pi;
4482
4483 /* ring the doorbell */
4484 WREG32(db_reg_offset, db_value);
4485
Ofir Bitton5dbd7b42021-01-28 16:30:25 +02004486 if (hw_queue_id == GAUDI_QUEUE_ID_CPU_PQ) {
4487 /* make sure device CPU will read latest data from host */
4488 mb();
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03004489 WREG32(mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR,
4490 GAUDI_EVENT_PI_UPDATE);
Ofir Bitton5dbd7b42021-01-28 16:30:25 +02004491 }
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03004492}
4493
4494static void gaudi_pqe_write(struct hl_device *hdev, __le64 *pqe,
4495 struct hl_bd *bd)
4496{
4497 __le64 *pbd = (__le64 *) bd;
4498
4499 /* The QMANs are on the host memory so a simple copy suffice */
4500 pqe[0] = pbd[0];
4501 pqe[1] = pbd[1];
4502}
4503
4504static void *gaudi_dma_alloc_coherent(struct hl_device *hdev, size_t size,
4505 dma_addr_t *dma_handle, gfp_t flags)
4506{
4507 void *kernel_addr = dma_alloc_coherent(&hdev->pdev->dev, size,
4508 dma_handle, flags);
4509
4510 /* Shift to the device's base physical address of host memory */
4511 if (kernel_addr)
4512 *dma_handle += HOST_PHYS_BASE;
4513
4514 return kernel_addr;
4515}
4516
4517static void gaudi_dma_free_coherent(struct hl_device *hdev, size_t size,
4518 void *cpu_addr, dma_addr_t dma_handle)
4519{
4520 /* Cancel the device's base physical address of host memory */
4521 dma_addr_t fixed_dma_handle = dma_handle - HOST_PHYS_BASE;
4522
4523 dma_free_coherent(&hdev->pdev->dev, size, cpu_addr, fixed_dma_handle);
4524}
4525
farah kassabri03df1362020-05-06 11:17:38 +03004526static int gaudi_hbm_scrubbing(struct hl_device *hdev)
4527{
4528 struct asic_fixed_properties *prop = &hdev->asic_prop;
4529 u64 cur_addr = DRAM_BASE_ADDR_USER;
4530 u32 val;
4531 u32 chunk_size;
4532 int rc, dma_id;
4533
4534 while (cur_addr < prop->dram_end_address) {
4535 for (dma_id = 0 ; dma_id < DMA_NUMBER_OF_CHANNELS ; dma_id++) {
4536 u32 dma_offset = dma_id * DMA_CORE_OFFSET;
4537
4538 chunk_size =
4539 min((u64)SZ_2G, prop->dram_end_address - cur_addr);
4540
4541 dev_dbg(hdev->dev,
4542 "Doing HBM scrubbing for 0x%09llx - 0x%09llx\n",
4543 cur_addr, cur_addr + chunk_size);
4544
4545 WREG32(mmDMA0_CORE_SRC_BASE_LO + dma_offset, 0);
4546 WREG32(mmDMA0_CORE_SRC_BASE_HI + dma_offset, 0);
4547 WREG32(mmDMA0_CORE_DST_BASE_LO + dma_offset,
4548 lower_32_bits(cur_addr));
4549 WREG32(mmDMA0_CORE_DST_BASE_HI + dma_offset,
4550 upper_32_bits(cur_addr));
4551 WREG32(mmDMA0_CORE_DST_TSIZE_0 + dma_offset,
4552 chunk_size);
4553 WREG32(mmDMA0_CORE_COMMIT + dma_offset,
4554 ((1 << DMA0_CORE_COMMIT_LIN_SHIFT) |
4555 (1 << DMA0_CORE_COMMIT_MEM_SET_SHIFT)));
4556
4557 cur_addr += chunk_size;
4558
4559 if (cur_addr == prop->dram_end_address)
4560 break;
4561 }
4562
4563 for (dma_id = 0 ; dma_id < DMA_NUMBER_OF_CHANNELS ; dma_id++) {
4564 u32 dma_offset = dma_id * DMA_CORE_OFFSET;
4565
4566 rc = hl_poll_timeout(
4567 hdev,
4568 mmDMA0_CORE_STS0 + dma_offset,
4569 val,
4570 ((val & DMA0_CORE_STS0_BUSY_MASK) == 0),
4571 1000,
4572 HBM_SCRUBBING_TIMEOUT_US);
4573
4574 if (rc) {
4575 dev_err(hdev->dev,
4576 "DMA Timeout during HBM scrubbing of DMA #%d\n",
4577 dma_id);
4578 return -EIO;
4579 }
4580 }
4581 }
4582
4583 return 0;
4584}
4585
4586static int gaudi_scrub_device_mem(struct hl_device *hdev, u64 addr, u64 size)
4587{
4588 struct asic_fixed_properties *prop = &hdev->asic_prop;
4589 struct gaudi_device *gaudi = hdev->asic_specific;
farah kassabri03df1362020-05-06 11:17:38 +03004590 int rc = 0;
4591 u64 val = 0;
4592
4593 if (!hdev->memory_scrub)
4594 return 0;
4595
4596 if (!addr && !size) {
4597 /* Wait till device is idle */
4598 rc = hl_poll_timeout(
4599 hdev,
4600 mmDMA0_CORE_STS0/* dummy */,
4601 val/* dummy */,
Ohad Sharabicf303392021-01-17 16:01:56 +02004602 (hdev->asic_funcs->is_device_idle(hdev, NULL,
4603 0, NULL)),
farah kassabri03df1362020-05-06 11:17:38 +03004604 1000,
4605 HBM_SCRUBBING_TIMEOUT_US);
4606 if (rc) {
4607 dev_err(hdev->dev, "waiting for idle timeout\n");
4608 return -EIO;
4609 }
4610
4611 /* Scrub SRAM */
4612 addr = prop->sram_user_base_address;
4613 size = hdev->pldm ? 0x10000 :
4614 (prop->sram_size - SRAM_USER_BASE_OFFSET);
4615 val = 0x7777777777777777ull;
4616
4617 rc = gaudi_memset_device_memory(hdev, addr, size, val);
4618 if (rc) {
4619 dev_err(hdev->dev,
4620 "Failed to clear SRAM in mem scrub all\n");
4621 return rc;
4622 }
4623
4624 mutex_lock(&gaudi->clk_gate_mutex);
4625 hdev->asic_funcs->disable_clock_gating(hdev);
4626
4627 /* Scrub HBM using all DMA channels in parallel */
4628 rc = gaudi_hbm_scrubbing(hdev);
4629 if (rc)
4630 dev_err(hdev->dev,
4631 "Failed to clear HBM in mem scrub all\n");
4632
4633 hdev->asic_funcs->set_clock_gating(hdev);
4634 mutex_unlock(&gaudi->clk_gate_mutex);
4635 }
4636
4637 return rc;
4638}
4639
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03004640static void *gaudi_get_int_queue_base(struct hl_device *hdev,
4641 u32 queue_id, dma_addr_t *dma_handle,
4642 u16 *queue_len)
4643{
4644 struct gaudi_device *gaudi = hdev->asic_specific;
4645 struct gaudi_internal_qman_info *q;
4646
4647 if (queue_id >= GAUDI_QUEUE_ID_SIZE ||
4648 gaudi_queue_type[queue_id] != QUEUE_TYPE_INT) {
4649 dev_err(hdev->dev, "Got invalid queue id %d\n", queue_id);
4650 return NULL;
4651 }
4652
4653 q = &gaudi->internal_qmans[queue_id];
4654 *dma_handle = q->pq_dma_addr;
4655 *queue_len = q->pq_size / QMAN_PQ_ENTRY_SIZE;
4656
4657 return q->pq_kernel_addr;
4658}
4659
4660static int gaudi_send_cpu_message(struct hl_device *hdev, u32 *msg,
Alon Mizrahi439bc472020-11-10 13:49:10 +02004661 u16 len, u32 timeout, u64 *result)
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03004662{
4663 struct gaudi_device *gaudi = hdev->asic_specific;
4664
4665 if (!(gaudi->hw_cap_initialized & HW_CAP_CPU_Q)) {
4666 if (result)
4667 *result = 0;
4668 return 0;
4669 }
4670
Oded Gabbay788cacf2020-07-07 17:30:13 +03004671 if (!timeout)
4672 timeout = GAUDI_MSG_TO_CPU_TIMEOUT_USEC;
4673
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03004674 return hl_fw_send_cpu_message(hdev, GAUDI_QUEUE_ID_CPU_PQ, msg, len,
4675 timeout, result);
4676}
4677
4678static int gaudi_test_queue(struct hl_device *hdev, u32 hw_queue_id)
4679{
4680 struct packet_msg_prot *fence_pkt;
4681 dma_addr_t pkt_dma_addr;
4682 u32 fence_val, tmp, timeout_usec;
4683 dma_addr_t fence_dma_addr;
4684 u32 *fence_ptr;
4685 int rc;
4686
4687 if (hdev->pldm)
4688 timeout_usec = GAUDI_PLDM_TEST_QUEUE_WAIT_USEC;
4689 else
4690 timeout_usec = GAUDI_TEST_QUEUE_WAIT_USEC;
4691
4692 fence_val = GAUDI_QMAN0_FENCE_VAL;
4693
4694 fence_ptr = hdev->asic_funcs->asic_dma_pool_zalloc(hdev, 4, GFP_KERNEL,
4695 &fence_dma_addr);
4696 if (!fence_ptr) {
4697 dev_err(hdev->dev,
Dotan Barakd6b045c2020-08-06 09:20:49 +03004698 "Failed to allocate memory for H/W queue %d testing\n",
4699 hw_queue_id);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03004700 return -ENOMEM;
4701 }
4702
4703 *fence_ptr = 0;
4704
4705 fence_pkt = hdev->asic_funcs->asic_dma_pool_zalloc(hdev,
4706 sizeof(struct packet_msg_prot),
4707 GFP_KERNEL, &pkt_dma_addr);
4708 if (!fence_pkt) {
4709 dev_err(hdev->dev,
Dotan Barakd6b045c2020-08-06 09:20:49 +03004710 "Failed to allocate packet for H/W queue %d testing\n",
4711 hw_queue_id);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03004712 rc = -ENOMEM;
4713 goto free_fence_ptr;
4714 }
4715
Oded Gabbay65887292020-08-12 11:21:01 +03004716 tmp = FIELD_PREP(GAUDI_PKT_CTL_OPCODE_MASK, PACKET_MSG_PROT);
4717 tmp |= FIELD_PREP(GAUDI_PKT_CTL_EB_MASK, 1);
4718 tmp |= FIELD_PREP(GAUDI_PKT_CTL_MB_MASK, 1);
4719
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03004720 fence_pkt->ctl = cpu_to_le32(tmp);
4721 fence_pkt->value = cpu_to_le32(fence_val);
4722 fence_pkt->addr = cpu_to_le64(fence_dma_addr);
4723
4724 rc = hl_hw_queue_send_cb_no_cmpl(hdev, hw_queue_id,
4725 sizeof(struct packet_msg_prot),
4726 pkt_dma_addr);
4727 if (rc) {
4728 dev_err(hdev->dev,
Dotan Barakd6b045c2020-08-06 09:20:49 +03004729 "Failed to send fence packet to H/W queue %d\n",
4730 hw_queue_id);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03004731 goto free_pkt;
4732 }
4733
4734 rc = hl_poll_timeout_memory(hdev, fence_ptr, tmp, (tmp == fence_val),
4735 1000, timeout_usec, true);
4736
4737 hl_hw_queue_inc_ci_kernel(hdev, hw_queue_id);
4738
4739 if (rc == -ETIMEDOUT) {
4740 dev_err(hdev->dev,
4741 "H/W queue %d test failed (scratch(0x%08llX) == 0x%08X)\n",
4742 hw_queue_id, (unsigned long long) fence_dma_addr, tmp);
4743 rc = -EIO;
4744 }
4745
4746free_pkt:
4747 hdev->asic_funcs->asic_dma_pool_free(hdev, (void *) fence_pkt,
4748 pkt_dma_addr);
4749free_fence_ptr:
4750 hdev->asic_funcs->asic_dma_pool_free(hdev, (void *) fence_ptr,
4751 fence_dma_addr);
4752 return rc;
4753}
4754
4755static int gaudi_test_cpu_queue(struct hl_device *hdev)
4756{
4757 struct gaudi_device *gaudi = hdev->asic_specific;
4758
4759 /*
4760 * check capability here as send_cpu_message() won't update the result
4761 * value if no capability
4762 */
4763 if (!(gaudi->hw_cap_initialized & HW_CAP_CPU_Q))
4764 return 0;
4765
4766 return hl_fw_test_cpu_queue(hdev);
4767}
4768
4769static int gaudi_test_queues(struct hl_device *hdev)
4770{
4771 int i, rc, ret_val = 0;
4772
Ofir Bitton3abc99b2020-06-23 14:50:39 +03004773 for (i = 0 ; i < hdev->asic_prop.max_queues ; i++) {
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03004774 if (hdev->asic_prop.hw_queues_props[i].type == QUEUE_TYPE_EXT) {
4775 rc = gaudi_test_queue(hdev, i);
4776 if (rc)
4777 ret_val = -EINVAL;
4778 }
4779 }
4780
4781 rc = gaudi_test_cpu_queue(hdev);
4782 if (rc)
4783 ret_val = -EINVAL;
4784
4785 return ret_val;
4786}
4787
4788static void *gaudi_dma_pool_zalloc(struct hl_device *hdev, size_t size,
4789 gfp_t mem_flags, dma_addr_t *dma_handle)
4790{
4791 void *kernel_addr;
4792
4793 if (size > GAUDI_DMA_POOL_BLK_SIZE)
4794 return NULL;
4795
4796 kernel_addr = dma_pool_zalloc(hdev->dma_pool, mem_flags, dma_handle);
4797
4798 /* Shift to the device's base physical address of host memory */
4799 if (kernel_addr)
4800 *dma_handle += HOST_PHYS_BASE;
4801
4802 return kernel_addr;
4803}
4804
4805static void gaudi_dma_pool_free(struct hl_device *hdev, void *vaddr,
4806 dma_addr_t dma_addr)
4807{
4808 /* Cancel the device's base physical address of host memory */
4809 dma_addr_t fixed_dma_addr = dma_addr - HOST_PHYS_BASE;
4810
4811 dma_pool_free(hdev->dma_pool, vaddr, fixed_dma_addr);
4812}
4813
4814static void *gaudi_cpu_accessible_dma_pool_alloc(struct hl_device *hdev,
4815 size_t size, dma_addr_t *dma_handle)
4816{
4817 return hl_fw_cpu_accessible_dma_pool_alloc(hdev, size, dma_handle);
4818}
4819
4820static void gaudi_cpu_accessible_dma_pool_free(struct hl_device *hdev,
4821 size_t size, void *vaddr)
4822{
4823 hl_fw_cpu_accessible_dma_pool_free(hdev, size, vaddr);
4824}
4825
4826static int gaudi_dma_map_sg(struct hl_device *hdev, struct scatterlist *sgl,
4827 int nents, enum dma_data_direction dir)
4828{
4829 struct scatterlist *sg;
4830 int i;
4831
4832 if (!dma_map_sg(&hdev->pdev->dev, sgl, nents, dir))
4833 return -ENOMEM;
4834
4835 /* Shift to the device's base physical address of host memory */
4836 for_each_sg(sgl, sg, nents, i)
4837 sg->dma_address += HOST_PHYS_BASE;
4838
4839 return 0;
4840}
4841
4842static void gaudi_dma_unmap_sg(struct hl_device *hdev, struct scatterlist *sgl,
4843 int nents, enum dma_data_direction dir)
4844{
4845 struct scatterlist *sg;
4846 int i;
4847
4848 /* Cancel the device's base physical address of host memory */
4849 for_each_sg(sgl, sg, nents, i)
4850 sg->dma_address -= HOST_PHYS_BASE;
4851
4852 dma_unmap_sg(&hdev->pdev->dev, sgl, nents, dir);
4853}
4854
4855static u32 gaudi_get_dma_desc_list_size(struct hl_device *hdev,
4856 struct sg_table *sgt)
4857{
4858 struct scatterlist *sg, *sg_next_iter;
4859 u32 count, dma_desc_cnt;
4860 u64 len, len_next;
4861 dma_addr_t addr, addr_next;
4862
4863 dma_desc_cnt = 0;
4864
4865 for_each_sg(sgt->sgl, sg, sgt->nents, count) {
4866
4867 len = sg_dma_len(sg);
4868 addr = sg_dma_address(sg);
4869
4870 if (len == 0)
4871 break;
4872
4873 while ((count + 1) < sgt->nents) {
4874 sg_next_iter = sg_next(sg);
4875 len_next = sg_dma_len(sg_next_iter);
4876 addr_next = sg_dma_address(sg_next_iter);
4877
4878 if (len_next == 0)
4879 break;
4880
4881 if ((addr + len == addr_next) &&
4882 (len + len_next <= DMA_MAX_TRANSFER_SIZE)) {
4883 len += len_next;
4884 count++;
4885 sg = sg_next_iter;
4886 } else {
4887 break;
4888 }
4889 }
4890
4891 dma_desc_cnt++;
4892 }
4893
4894 return dma_desc_cnt * sizeof(struct packet_lin_dma);
4895}
4896
4897static int gaudi_pin_memory_before_cs(struct hl_device *hdev,
4898 struct hl_cs_parser *parser,
4899 struct packet_lin_dma *user_dma_pkt,
4900 u64 addr, enum dma_data_direction dir)
4901{
4902 struct hl_userptr *userptr;
4903 int rc;
4904
4905 if (hl_userptr_is_pinned(hdev, addr, le32_to_cpu(user_dma_pkt->tsize),
4906 parser->job_userptr_list, &userptr))
4907 goto already_pinned;
4908
Ofir Bittond5eb8372021-02-14 15:35:56 +02004909 userptr = kzalloc(sizeof(*userptr), GFP_KERNEL);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03004910 if (!userptr)
4911 return -ENOMEM;
4912
4913 rc = hl_pin_host_memory(hdev, addr, le32_to_cpu(user_dma_pkt->tsize),
4914 userptr);
4915 if (rc)
4916 goto free_userptr;
4917
4918 list_add_tail(&userptr->job_node, parser->job_userptr_list);
4919
4920 rc = hdev->asic_funcs->asic_dma_map_sg(hdev, userptr->sgt->sgl,
4921 userptr->sgt->nents, dir);
4922 if (rc) {
4923 dev_err(hdev->dev, "failed to map sgt with DMA region\n");
4924 goto unpin_memory;
4925 }
4926
4927 userptr->dma_mapped = true;
4928 userptr->dir = dir;
4929
4930already_pinned:
4931 parser->patched_cb_size +=
4932 gaudi_get_dma_desc_list_size(hdev, userptr->sgt);
4933
4934 return 0;
4935
4936unpin_memory:
4937 hl_unpin_host_memory(hdev, userptr);
4938free_userptr:
4939 kfree(userptr);
4940 return rc;
4941}
4942
4943static int gaudi_validate_dma_pkt_host(struct hl_device *hdev,
4944 struct hl_cs_parser *parser,
4945 struct packet_lin_dma *user_dma_pkt,
4946 bool src_in_host)
4947{
4948 enum dma_data_direction dir;
4949 bool skip_host_mem_pin = false, user_memset;
4950 u64 addr;
4951 int rc = 0;
4952
4953 user_memset = (le32_to_cpu(user_dma_pkt->ctl) &
4954 GAUDI_PKT_LIN_DMA_CTL_MEMSET_MASK) >>
4955 GAUDI_PKT_LIN_DMA_CTL_MEMSET_SHIFT;
4956
4957 if (src_in_host) {
4958 if (user_memset)
4959 skip_host_mem_pin = true;
4960
4961 dev_dbg(hdev->dev, "DMA direction is HOST --> DEVICE\n");
4962 dir = DMA_TO_DEVICE;
4963 addr = le64_to_cpu(user_dma_pkt->src_addr);
4964 } else {
4965 dev_dbg(hdev->dev, "DMA direction is DEVICE --> HOST\n");
4966 dir = DMA_FROM_DEVICE;
4967 addr = (le64_to_cpu(user_dma_pkt->dst_addr) &
4968 GAUDI_PKT_LIN_DMA_DST_ADDR_MASK) >>
4969 GAUDI_PKT_LIN_DMA_DST_ADDR_SHIFT;
4970 }
4971
4972 if (skip_host_mem_pin)
4973 parser->patched_cb_size += sizeof(*user_dma_pkt);
4974 else
4975 rc = gaudi_pin_memory_before_cs(hdev, parser, user_dma_pkt,
4976 addr, dir);
4977
4978 return rc;
4979}
4980
4981static int gaudi_validate_dma_pkt_no_mmu(struct hl_device *hdev,
4982 struct hl_cs_parser *parser,
4983 struct packet_lin_dma *user_dma_pkt)
4984{
4985 bool src_in_host = false;
4986 u64 dst_addr = (le64_to_cpu(user_dma_pkt->dst_addr) &
4987 GAUDI_PKT_LIN_DMA_DST_ADDR_MASK) >>
4988 GAUDI_PKT_LIN_DMA_DST_ADDR_SHIFT;
4989
4990 dev_dbg(hdev->dev, "DMA packet details:\n");
4991 dev_dbg(hdev->dev, "source == 0x%llx\n",
4992 le64_to_cpu(user_dma_pkt->src_addr));
4993 dev_dbg(hdev->dev, "destination == 0x%llx\n", dst_addr);
4994 dev_dbg(hdev->dev, "size == %u\n", le32_to_cpu(user_dma_pkt->tsize));
4995
4996 /*
4997 * Special handling for DMA with size 0. Bypass all validations
4998 * because no transactions will be done except for WR_COMP, which
4999 * is not a security issue
5000 */
5001 if (!le32_to_cpu(user_dma_pkt->tsize)) {
5002 parser->patched_cb_size += sizeof(*user_dma_pkt);
5003 return 0;
5004 }
5005
5006 if (parser->hw_queue_id <= GAUDI_QUEUE_ID_DMA_0_3)
5007 src_in_host = true;
5008
5009 return gaudi_validate_dma_pkt_host(hdev, parser, user_dma_pkt,
5010 src_in_host);
5011}
5012
Oded Gabbay64536ab2020-05-27 12:38:16 +03005013static int gaudi_validate_load_and_exe_pkt(struct hl_device *hdev,
5014 struct hl_cs_parser *parser,
5015 struct packet_load_and_exe *user_pkt)
5016{
5017 u32 cfg;
5018
5019 cfg = le32_to_cpu(user_pkt->cfg);
5020
5021 if (cfg & GAUDI_PKT_LOAD_AND_EXE_CFG_DST_MASK) {
5022 dev_err(hdev->dev,
5023 "User not allowed to use Load and Execute\n");
5024 return -EPERM;
5025 }
5026
5027 parser->patched_cb_size += sizeof(struct packet_load_and_exe);
5028
5029 return 0;
5030}
5031
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005032static int gaudi_validate_cb(struct hl_device *hdev,
5033 struct hl_cs_parser *parser, bool is_mmu)
5034{
5035 u32 cb_parsed_length = 0;
5036 int rc = 0;
5037
5038 parser->patched_cb_size = 0;
5039
5040 /* cb_user_size is more than 0 so loop will always be executed */
5041 while (cb_parsed_length < parser->user_cb_size) {
5042 enum packet_id pkt_id;
5043 u16 pkt_size;
5044 struct gaudi_packet *user_pkt;
5045
Arnd Bergmann82948e62020-10-26 17:08:06 +01005046 user_pkt = parser->user_cb->kernel_address + cb_parsed_length;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005047
5048 pkt_id = (enum packet_id) (
5049 (le64_to_cpu(user_pkt->header) &
5050 PACKET_HEADER_PACKET_ID_MASK) >>
5051 PACKET_HEADER_PACKET_ID_SHIFT);
5052
Ofir Bittonbc75be22020-07-30 14:56:38 +03005053 if (!validate_packet_id(pkt_id)) {
5054 dev_err(hdev->dev, "Invalid packet id %u\n", pkt_id);
5055 rc = -EINVAL;
5056 break;
5057 }
5058
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005059 pkt_size = gaudi_packet_sizes[pkt_id];
5060 cb_parsed_length += pkt_size;
5061 if (cb_parsed_length > parser->user_cb_size) {
5062 dev_err(hdev->dev,
5063 "packet 0x%x is out of CB boundary\n", pkt_id);
5064 rc = -EINVAL;
5065 break;
5066 }
5067
5068 switch (pkt_id) {
5069 case PACKET_MSG_PROT:
5070 dev_err(hdev->dev,
5071 "User not allowed to use MSG_PROT\n");
5072 rc = -EPERM;
5073 break;
5074
5075 case PACKET_CP_DMA:
5076 dev_err(hdev->dev, "User not allowed to use CP_DMA\n");
5077 rc = -EPERM;
5078 break;
5079
5080 case PACKET_STOP:
5081 dev_err(hdev->dev, "User not allowed to use STOP\n");
5082 rc = -EPERM;
5083 break;
5084
Oded Gabbay2edc66e2020-07-03 19:28:54 +03005085 case PACKET_WREG_BULK:
5086 dev_err(hdev->dev,
5087 "User not allowed to use WREG_BULK\n");
5088 rc = -EPERM;
5089 break;
5090
Oded Gabbay64536ab2020-05-27 12:38:16 +03005091 case PACKET_LOAD_AND_EXE:
5092 rc = gaudi_validate_load_and_exe_pkt(hdev, parser,
5093 (struct packet_load_and_exe *) user_pkt);
5094 break;
5095
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005096 case PACKET_LIN_DMA:
5097 parser->contains_dma_pkt = true;
5098 if (is_mmu)
5099 parser->patched_cb_size += pkt_size;
5100 else
5101 rc = gaudi_validate_dma_pkt_no_mmu(hdev, parser,
5102 (struct packet_lin_dma *) user_pkt);
5103 break;
5104
5105 case PACKET_WREG_32:
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005106 case PACKET_MSG_LONG:
5107 case PACKET_MSG_SHORT:
5108 case PACKET_REPEAT:
5109 case PACKET_FENCE:
5110 case PACKET_NOP:
5111 case PACKET_ARB_POINT:
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005112 parser->patched_cb_size += pkt_size;
5113 break;
5114
5115 default:
5116 dev_err(hdev->dev, "Invalid packet header 0x%x\n",
5117 pkt_id);
5118 rc = -EINVAL;
5119 break;
5120 }
5121
5122 if (rc)
5123 break;
5124 }
5125
5126 /*
5127 * The new CB should have space at the end for two MSG_PROT packets:
5128 * 1. A packet that will act as a completion packet
5129 * 2. A packet that will generate MSI-X interrupt
5130 */
Ofir Bittonac6fdbf2020-12-03 16:59:28 +02005131 if (parser->completion)
5132 parser->patched_cb_size += sizeof(struct packet_msg_prot) * 2;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005133
5134 return rc;
5135}
5136
5137static int gaudi_patch_dma_packet(struct hl_device *hdev,
5138 struct hl_cs_parser *parser,
5139 struct packet_lin_dma *user_dma_pkt,
5140 struct packet_lin_dma *new_dma_pkt,
5141 u32 *new_dma_pkt_size)
5142{
5143 struct hl_userptr *userptr;
5144 struct scatterlist *sg, *sg_next_iter;
5145 u32 count, dma_desc_cnt, user_wrcomp_en_mask, ctl;
5146 u64 len, len_next;
5147 dma_addr_t dma_addr, dma_addr_next;
5148 u64 device_memory_addr, addr;
5149 enum dma_data_direction dir;
5150 struct sg_table *sgt;
5151 bool src_in_host = false;
5152 bool skip_host_mem_pin = false;
5153 bool user_memset;
5154
5155 ctl = le32_to_cpu(user_dma_pkt->ctl);
5156
5157 if (parser->hw_queue_id <= GAUDI_QUEUE_ID_DMA_0_3)
5158 src_in_host = true;
5159
5160 user_memset = (ctl & GAUDI_PKT_LIN_DMA_CTL_MEMSET_MASK) >>
5161 GAUDI_PKT_LIN_DMA_CTL_MEMSET_SHIFT;
5162
5163 if (src_in_host) {
5164 addr = le64_to_cpu(user_dma_pkt->src_addr);
5165 device_memory_addr = le64_to_cpu(user_dma_pkt->dst_addr);
5166 dir = DMA_TO_DEVICE;
5167 if (user_memset)
5168 skip_host_mem_pin = true;
5169 } else {
5170 addr = le64_to_cpu(user_dma_pkt->dst_addr);
5171 device_memory_addr = le64_to_cpu(user_dma_pkt->src_addr);
5172 dir = DMA_FROM_DEVICE;
5173 }
5174
5175 if ((!skip_host_mem_pin) &&
5176 (!hl_userptr_is_pinned(hdev, addr,
5177 le32_to_cpu(user_dma_pkt->tsize),
5178 parser->job_userptr_list, &userptr))) {
5179 dev_err(hdev->dev, "Userptr 0x%llx + 0x%x NOT mapped\n",
5180 addr, user_dma_pkt->tsize);
5181 return -EFAULT;
5182 }
5183
5184 if ((user_memset) && (dir == DMA_TO_DEVICE)) {
5185 memcpy(new_dma_pkt, user_dma_pkt, sizeof(*user_dma_pkt));
5186 *new_dma_pkt_size = sizeof(*user_dma_pkt);
5187 return 0;
5188 }
5189
5190 user_wrcomp_en_mask = ctl & GAUDI_PKT_LIN_DMA_CTL_WRCOMP_EN_MASK;
5191
5192 sgt = userptr->sgt;
5193 dma_desc_cnt = 0;
5194
5195 for_each_sg(sgt->sgl, sg, sgt->nents, count) {
5196 len = sg_dma_len(sg);
5197 dma_addr = sg_dma_address(sg);
5198
5199 if (len == 0)
5200 break;
5201
5202 while ((count + 1) < sgt->nents) {
5203 sg_next_iter = sg_next(sg);
5204 len_next = sg_dma_len(sg_next_iter);
5205 dma_addr_next = sg_dma_address(sg_next_iter);
5206
5207 if (len_next == 0)
5208 break;
5209
5210 if ((dma_addr + len == dma_addr_next) &&
5211 (len + len_next <= DMA_MAX_TRANSFER_SIZE)) {
5212 len += len_next;
5213 count++;
5214 sg = sg_next_iter;
5215 } else {
5216 break;
5217 }
5218 }
5219
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005220 ctl = le32_to_cpu(user_dma_pkt->ctl);
5221 if (likely(dma_desc_cnt))
5222 ctl &= ~GAUDI_PKT_CTL_EB_MASK;
5223 ctl &= ~GAUDI_PKT_LIN_DMA_CTL_WRCOMP_EN_MASK;
5224 new_dma_pkt->ctl = cpu_to_le32(ctl);
5225 new_dma_pkt->tsize = cpu_to_le32(len);
5226
5227 if (dir == DMA_TO_DEVICE) {
5228 new_dma_pkt->src_addr = cpu_to_le64(dma_addr);
5229 new_dma_pkt->dst_addr = cpu_to_le64(device_memory_addr);
5230 } else {
5231 new_dma_pkt->src_addr = cpu_to_le64(device_memory_addr);
5232 new_dma_pkt->dst_addr = cpu_to_le64(dma_addr);
5233 }
5234
5235 if (!user_memset)
5236 device_memory_addr += len;
5237 dma_desc_cnt++;
5238 new_dma_pkt++;
5239 }
5240
5241 if (!dma_desc_cnt) {
5242 dev_err(hdev->dev,
5243 "Error of 0 SG entries when patching DMA packet\n");
5244 return -EFAULT;
5245 }
5246
5247 /* Fix the last dma packet - wrcomp must be as user set it */
5248 new_dma_pkt--;
5249 new_dma_pkt->ctl |= cpu_to_le32(user_wrcomp_en_mask);
5250
5251 *new_dma_pkt_size = dma_desc_cnt * sizeof(struct packet_lin_dma);
5252
5253 return 0;
5254}
5255
5256static int gaudi_patch_cb(struct hl_device *hdev,
5257 struct hl_cs_parser *parser)
5258{
5259 u32 cb_parsed_length = 0;
5260 u32 cb_patched_cur_length = 0;
5261 int rc = 0;
5262
5263 /* cb_user_size is more than 0 so loop will always be executed */
5264 while (cb_parsed_length < parser->user_cb_size) {
5265 enum packet_id pkt_id;
5266 u16 pkt_size;
5267 u32 new_pkt_size = 0;
5268 struct gaudi_packet *user_pkt, *kernel_pkt;
5269
Arnd Bergmann82948e62020-10-26 17:08:06 +01005270 user_pkt = parser->user_cb->kernel_address + cb_parsed_length;
5271 kernel_pkt = parser->patched_cb->kernel_address +
5272 cb_patched_cur_length;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005273
5274 pkt_id = (enum packet_id) (
5275 (le64_to_cpu(user_pkt->header) &
5276 PACKET_HEADER_PACKET_ID_MASK) >>
5277 PACKET_HEADER_PACKET_ID_SHIFT);
5278
Ofir Bittonbc75be22020-07-30 14:56:38 +03005279 if (!validate_packet_id(pkt_id)) {
5280 dev_err(hdev->dev, "Invalid packet id %u\n", pkt_id);
5281 rc = -EINVAL;
5282 break;
5283 }
5284
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005285 pkt_size = gaudi_packet_sizes[pkt_id];
5286 cb_parsed_length += pkt_size;
5287 if (cb_parsed_length > parser->user_cb_size) {
5288 dev_err(hdev->dev,
5289 "packet 0x%x is out of CB boundary\n", pkt_id);
5290 rc = -EINVAL;
5291 break;
5292 }
5293
5294 switch (pkt_id) {
5295 case PACKET_LIN_DMA:
5296 rc = gaudi_patch_dma_packet(hdev, parser,
5297 (struct packet_lin_dma *) user_pkt,
5298 (struct packet_lin_dma *) kernel_pkt,
5299 &new_pkt_size);
5300 cb_patched_cur_length += new_pkt_size;
5301 break;
5302
5303 case PACKET_MSG_PROT:
5304 dev_err(hdev->dev,
5305 "User not allowed to use MSG_PROT\n");
5306 rc = -EPERM;
5307 break;
5308
5309 case PACKET_CP_DMA:
5310 dev_err(hdev->dev, "User not allowed to use CP_DMA\n");
5311 rc = -EPERM;
5312 break;
5313
5314 case PACKET_STOP:
5315 dev_err(hdev->dev, "User not allowed to use STOP\n");
5316 rc = -EPERM;
5317 break;
5318
5319 case PACKET_WREG_32:
5320 case PACKET_WREG_BULK:
5321 case PACKET_MSG_LONG:
5322 case PACKET_MSG_SHORT:
5323 case PACKET_REPEAT:
5324 case PACKET_FENCE:
5325 case PACKET_NOP:
5326 case PACKET_ARB_POINT:
5327 case PACKET_LOAD_AND_EXE:
5328 memcpy(kernel_pkt, user_pkt, pkt_size);
5329 cb_patched_cur_length += pkt_size;
5330 break;
5331
5332 default:
5333 dev_err(hdev->dev, "Invalid packet header 0x%x\n",
5334 pkt_id);
5335 rc = -EINVAL;
5336 break;
5337 }
5338
5339 if (rc)
5340 break;
5341 }
5342
5343 return rc;
5344}
5345
5346static int gaudi_parse_cb_mmu(struct hl_device *hdev,
5347 struct hl_cs_parser *parser)
5348{
5349 u64 patched_cb_handle;
5350 u32 patched_cb_size;
5351 struct hl_cb *user_cb;
5352 int rc;
5353
5354 /*
5355 * The new CB should have space at the end for two MSG_PROT pkt:
5356 * 1. A packet that will act as a completion packet
5357 * 2. A packet that will generate MSI interrupt
5358 */
Ofir Bittonac6fdbf2020-12-03 16:59:28 +02005359 if (parser->completion)
5360 parser->patched_cb_size = parser->user_cb_size +
5361 sizeof(struct packet_msg_prot) * 2;
5362 else
5363 parser->patched_cb_size = parser->user_cb_size;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005364
Tomer Tayarfa8641a12020-09-07 17:36:41 +03005365 rc = hl_cb_create(hdev, &hdev->kernel_cb_mgr, hdev->kernel_ctx,
Tomer Tayaref6a0f62020-07-09 16:17:48 +03005366 parser->patched_cb_size, false, false,
Tomer Tayarfa8641a12020-09-07 17:36:41 +03005367 &patched_cb_handle);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005368
5369 if (rc) {
5370 dev_err(hdev->dev,
5371 "Failed to allocate patched CB for DMA CS %d\n",
5372 rc);
5373 return rc;
5374 }
5375
5376 patched_cb_handle >>= PAGE_SHIFT;
5377 parser->patched_cb = hl_cb_get(hdev, &hdev->kernel_cb_mgr,
5378 (u32) patched_cb_handle);
Alon Mizrahi75d9a2a2020-12-03 17:32:19 +02005379 /* hl_cb_get should never fail */
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005380 if (!parser->patched_cb) {
Alon Mizrahi75d9a2a2020-12-03 17:32:19 +02005381 dev_crit(hdev->dev, "DMA CB handle invalid 0x%x\n",
5382 (u32) patched_cb_handle);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005383 rc = -EFAULT;
5384 goto out;
5385 }
5386
5387 /*
5388 * The check that parser->user_cb_size <= parser->user_cb->size was done
5389 * in validate_queue_index().
5390 */
Arnd Bergmann82948e62020-10-26 17:08:06 +01005391 memcpy(parser->patched_cb->kernel_address,
5392 parser->user_cb->kernel_address,
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005393 parser->user_cb_size);
5394
5395 patched_cb_size = parser->patched_cb_size;
5396
5397 /* Validate patched CB instead of user CB */
5398 user_cb = parser->user_cb;
5399 parser->user_cb = parser->patched_cb;
5400 rc = gaudi_validate_cb(hdev, parser, true);
5401 parser->user_cb = user_cb;
5402
5403 if (rc) {
5404 hl_cb_put(parser->patched_cb);
5405 goto out;
5406 }
5407
5408 if (patched_cb_size != parser->patched_cb_size) {
5409 dev_err(hdev->dev, "user CB size mismatch\n");
5410 hl_cb_put(parser->patched_cb);
5411 rc = -EINVAL;
5412 goto out;
5413 }
5414
5415out:
5416 /*
5417 * Always call cb destroy here because we still have 1 reference
5418 * to it by calling cb_get earlier. After the job will be completed,
5419 * cb_put will release it, but here we want to remove it from the
5420 * idr
5421 */
5422 hl_cb_destroy(hdev, &hdev->kernel_cb_mgr,
5423 patched_cb_handle << PAGE_SHIFT);
5424
5425 return rc;
5426}
5427
5428static int gaudi_parse_cb_no_mmu(struct hl_device *hdev,
5429 struct hl_cs_parser *parser)
5430{
5431 u64 patched_cb_handle;
5432 int rc;
5433
5434 rc = gaudi_validate_cb(hdev, parser, false);
5435
5436 if (rc)
5437 goto free_userptr;
5438
Tomer Tayarfa8641a12020-09-07 17:36:41 +03005439 rc = hl_cb_create(hdev, &hdev->kernel_cb_mgr, hdev->kernel_ctx,
Tomer Tayaref6a0f62020-07-09 16:17:48 +03005440 parser->patched_cb_size, false, false,
Tomer Tayarfa8641a12020-09-07 17:36:41 +03005441 &patched_cb_handle);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005442 if (rc) {
5443 dev_err(hdev->dev,
5444 "Failed to allocate patched CB for DMA CS %d\n", rc);
5445 goto free_userptr;
5446 }
5447
5448 patched_cb_handle >>= PAGE_SHIFT;
5449 parser->patched_cb = hl_cb_get(hdev, &hdev->kernel_cb_mgr,
5450 (u32) patched_cb_handle);
Alon Mizrahi75d9a2a2020-12-03 17:32:19 +02005451 /* hl_cb_get should never fail here */
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005452 if (!parser->patched_cb) {
Alon Mizrahi75d9a2a2020-12-03 17:32:19 +02005453 dev_crit(hdev->dev, "DMA CB handle invalid 0x%x\n",
5454 (u32) patched_cb_handle);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005455 rc = -EFAULT;
5456 goto out;
5457 }
5458
5459 rc = gaudi_patch_cb(hdev, parser);
5460
5461 if (rc)
5462 hl_cb_put(parser->patched_cb);
5463
5464out:
5465 /*
5466 * Always call cb destroy here because we still have 1 reference
5467 * to it by calling cb_get earlier. After the job will be completed,
5468 * cb_put will release it, but here we want to remove it from the
5469 * idr
5470 */
5471 hl_cb_destroy(hdev, &hdev->kernel_cb_mgr,
5472 patched_cb_handle << PAGE_SHIFT);
5473
5474free_userptr:
5475 if (rc)
5476 hl_userptr_delete_list(hdev, parser->job_userptr_list);
5477 return rc;
5478}
5479
5480static int gaudi_parse_cb_no_ext_queue(struct hl_device *hdev,
5481 struct hl_cs_parser *parser)
5482{
5483 struct asic_fixed_properties *asic_prop = &hdev->asic_prop;
Oded Gabbay3c681572020-11-02 21:10:39 +02005484 struct gaudi_device *gaudi = hdev->asic_specific;
5485 u32 nic_mask_q_id = 1 << (HW_CAP_NIC_SHIFT +
5486 ((parser->hw_queue_id - GAUDI_QUEUE_ID_NIC_0_0) >> 2));
5487
5488 if ((parser->hw_queue_id >= GAUDI_QUEUE_ID_NIC_0_0) &&
5489 (parser->hw_queue_id <= GAUDI_QUEUE_ID_NIC_9_3) &&
5490 (!(gaudi->hw_cap_initialized & nic_mask_q_id))) {
5491 dev_err(hdev->dev, "h/w queue %d is disabled\n",
5492 parser->hw_queue_id);
5493 return -EINVAL;
5494 }
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005495
5496 /* For internal queue jobs just check if CB address is valid */
5497 if (hl_mem_area_inside_range((u64) (uintptr_t) parser->user_cb,
5498 parser->user_cb_size,
5499 asic_prop->sram_user_base_address,
5500 asic_prop->sram_end_address))
5501 return 0;
5502
5503 if (hl_mem_area_inside_range((u64) (uintptr_t) parser->user_cb,
5504 parser->user_cb_size,
5505 asic_prop->dram_user_base_address,
5506 asic_prop->dram_end_address))
5507 return 0;
5508
5509 /* PMMU and HPMMU addresses are equal, check only one of them */
5510 if (hl_mem_area_inside_range((u64) (uintptr_t) parser->user_cb,
5511 parser->user_cb_size,
5512 asic_prop->pmmu.start_addr,
5513 asic_prop->pmmu.end_addr))
5514 return 0;
5515
5516 dev_err(hdev->dev,
5517 "CB address 0x%px + 0x%x for internal QMAN is not valid\n",
5518 parser->user_cb, parser->user_cb_size);
5519
5520 return -EFAULT;
5521}
5522
5523static int gaudi_cs_parser(struct hl_device *hdev, struct hl_cs_parser *parser)
5524{
5525 struct gaudi_device *gaudi = hdev->asic_specific;
5526
5527 if (parser->queue_type == QUEUE_TYPE_INT)
5528 return gaudi_parse_cb_no_ext_queue(hdev, parser);
5529
5530 if (gaudi->hw_cap_initialized & HW_CAP_MMU)
5531 return gaudi_parse_cb_mmu(hdev, parser);
5532 else
5533 return gaudi_parse_cb_no_mmu(hdev, parser);
5534}
5535
5536static void gaudi_add_end_of_cb_packets(struct hl_device *hdev,
Arnd Bergmann82948e62020-10-26 17:08:06 +01005537 void *kernel_address, u32 len,
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005538 u64 cq_addr, u32 cq_val, u32 msi_vec,
5539 bool eb)
5540{
5541 struct gaudi_device *gaudi = hdev->asic_specific;
5542 struct packet_msg_prot *cq_pkt;
5543 u32 tmp;
5544
Arnd Bergmann82948e62020-10-26 17:08:06 +01005545 cq_pkt = kernel_address + len - (sizeof(struct packet_msg_prot) * 2);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005546
Oded Gabbay65887292020-08-12 11:21:01 +03005547 tmp = FIELD_PREP(GAUDI_PKT_CTL_OPCODE_MASK, PACKET_MSG_PROT);
5548 tmp |= FIELD_PREP(GAUDI_PKT_CTL_MB_MASK, 1);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005549
5550 if (eb)
Oded Gabbay65887292020-08-12 11:21:01 +03005551 tmp |= FIELD_PREP(GAUDI_PKT_CTL_EB_MASK, 1);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005552
5553 cq_pkt->ctl = cpu_to_le32(tmp);
5554 cq_pkt->value = cpu_to_le32(cq_val);
5555 cq_pkt->addr = cpu_to_le64(cq_addr);
5556
5557 cq_pkt++;
5558
Oded Gabbay65887292020-08-12 11:21:01 +03005559 tmp = FIELD_PREP(GAUDI_PKT_CTL_OPCODE_MASK, PACKET_MSG_PROT);
5560 tmp |= FIELD_PREP(GAUDI_PKT_CTL_MB_MASK, 1);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005561 cq_pkt->ctl = cpu_to_le32(tmp);
5562 cq_pkt->value = cpu_to_le32(1);
5563
5564 if (!gaudi->multi_msi_mode)
5565 msi_vec = 0;
5566
5567 cq_pkt->addr = cpu_to_le64(CFG_BASE + mmPCIE_MSI_INTR_0 + msi_vec * 4);
5568}
5569
5570static void gaudi_update_eq_ci(struct hl_device *hdev, u32 val)
5571{
5572 WREG32(mmCPU_IF_EQ_RD_OFFS, val);
5573}
5574
5575static int gaudi_memset_device_memory(struct hl_device *hdev, u64 addr,
5576 u32 size, u64 val)
5577{
5578 struct packet_lin_dma *lin_dma_pkt;
5579 struct hl_cs_job *job;
Moti Haimovskia9855a2d92020-06-24 19:40:57 +03005580 u32 cb_size, ctl, err_cause;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005581 struct hl_cb *cb;
Lv Yunlong115726c2021-04-26 06:43:46 -07005582 u64 id;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005583 int rc;
5584
Ofir Bittona04b7cd2020-07-13 13:36:55 +03005585 cb = hl_cb_kernel_create(hdev, PAGE_SIZE, false);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005586 if (!cb)
5587 return -EFAULT;
5588
Arnd Bergmann82948e62020-10-26 17:08:06 +01005589 lin_dma_pkt = cb->kernel_address;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005590 memset(lin_dma_pkt, 0, sizeof(*lin_dma_pkt));
5591 cb_size = sizeof(*lin_dma_pkt);
5592
Oded Gabbay65887292020-08-12 11:21:01 +03005593 ctl = FIELD_PREP(GAUDI_PKT_CTL_OPCODE_MASK, PACKET_LIN_DMA);
5594 ctl |= FIELD_PREP(GAUDI_PKT_LIN_DMA_CTL_MEMSET_MASK, 1);
5595 ctl |= FIELD_PREP(GAUDI_PKT_LIN_DMA_CTL_LIN_MASK, 1);
5596 ctl |= FIELD_PREP(GAUDI_PKT_CTL_MB_MASK, 1);
5597 ctl |= FIELD_PREP(GAUDI_PKT_CTL_RB_MASK, 1);
5598
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005599 lin_dma_pkt->ctl = cpu_to_le32(ctl);
5600 lin_dma_pkt->src_addr = cpu_to_le64(val);
5601 lin_dma_pkt->dst_addr |= cpu_to_le64(addr);
5602 lin_dma_pkt->tsize = cpu_to_le32(size);
5603
5604 job = hl_cs_allocate_job(hdev, QUEUE_TYPE_EXT, true);
5605 if (!job) {
5606 dev_err(hdev->dev, "Failed to allocate a new job\n");
5607 rc = -ENOMEM;
5608 goto release_cb;
5609 }
5610
Moti Haimovskia9855a2d92020-06-24 19:40:57 +03005611 /* Verify DMA is OK */
5612 err_cause = RREG32(mmDMA0_CORE_ERR_CAUSE);
5613 if (err_cause && !hdev->init_done) {
5614 dev_dbg(hdev->dev,
5615 "Clearing DMA0 engine from errors (cause 0x%x)\n",
5616 err_cause);
5617 WREG32(mmDMA0_CORE_ERR_CAUSE, err_cause);
5618 }
5619
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005620 job->id = 0;
5621 job->user_cb = cb;
Tomer Tayarf07486742020-08-02 22:51:31 +03005622 atomic_inc(&job->user_cb->cs_cnt);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005623 job->user_cb_size = cb_size;
5624 job->hw_queue_id = GAUDI_QUEUE_ID_DMA_0_0;
5625 job->patched_cb = job->user_cb;
5626 job->job_cb_size = job->user_cb_size + sizeof(struct packet_msg_prot);
5627
5628 hl_debugfs_add_job(hdev, job);
5629
5630 rc = gaudi_send_job_on_qman0(hdev, job);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005631 hl_debugfs_remove_job(hdev, job);
5632 kfree(job);
Tomer Tayarf07486742020-08-02 22:51:31 +03005633 atomic_dec(&cb->cs_cnt);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005634
Moti Haimovskia9855a2d92020-06-24 19:40:57 +03005635 /* Verify DMA is OK */
5636 err_cause = RREG32(mmDMA0_CORE_ERR_CAUSE);
5637 if (err_cause) {
5638 dev_err(hdev->dev, "DMA Failed, cause 0x%x\n", err_cause);
5639 rc = -EIO;
5640 if (!hdev->init_done) {
5641 dev_dbg(hdev->dev,
5642 "Clearing DMA0 engine from errors (cause 0x%x)\n",
5643 err_cause);
5644 WREG32(mmDMA0_CORE_ERR_CAUSE, err_cause);
5645 }
5646 }
5647
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005648release_cb:
Lv Yunlong115726c2021-04-26 06:43:46 -07005649 id = cb->id;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005650 hl_cb_put(cb);
Lv Yunlong115726c2021-04-26 06:43:46 -07005651 hl_cb_destroy(hdev, &hdev->kernel_cb_mgr, id << PAGE_SHIFT);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005652
5653 return rc;
5654}
5655
Ofir Bitton423815b2021-01-05 09:04:07 +02005656static int gaudi_memset_registers(struct hl_device *hdev, u64 reg_base,
5657 u32 num_regs, u32 val)
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005658{
Ofir Bitton423815b2021-01-05 09:04:07 +02005659 struct packet_msg_long *pkt;
5660 struct hl_cs_job *job;
5661 u32 cb_size, ctl;
5662 struct hl_cb *cb;
5663 int i, rc;
5664
5665 cb_size = (sizeof(*pkt) * num_regs) + sizeof(struct packet_msg_prot);
5666
5667 if (cb_size > SZ_2M) {
5668 dev_err(hdev->dev, "CB size must be smaller than %uMB", SZ_2M);
5669 return -ENOMEM;
5670 }
5671
5672 cb = hl_cb_kernel_create(hdev, cb_size, false);
5673 if (!cb)
5674 return -EFAULT;
5675
5676 pkt = cb->kernel_address;
5677
5678 ctl = FIELD_PREP(GAUDI_PKT_LONG_CTL_OP_MASK, 0); /* write the value */
5679 ctl |= FIELD_PREP(GAUDI_PKT_CTL_OPCODE_MASK, PACKET_MSG_LONG);
5680 ctl |= FIELD_PREP(GAUDI_PKT_CTL_EB_MASK, 1);
5681 ctl |= FIELD_PREP(GAUDI_PKT_CTL_RB_MASK, 1);
5682 ctl |= FIELD_PREP(GAUDI_PKT_CTL_MB_MASK, 1);
5683
5684 for (i = 0; i < num_regs ; i++, pkt++) {
5685 pkt->ctl = cpu_to_le32(ctl);
5686 pkt->value = cpu_to_le32(val);
5687 pkt->addr = cpu_to_le64(reg_base + (i * 4));
5688 }
5689
5690 job = hl_cs_allocate_job(hdev, QUEUE_TYPE_EXT, true);
5691 if (!job) {
5692 dev_err(hdev->dev, "Failed to allocate a new job\n");
5693 rc = -ENOMEM;
5694 goto release_cb;
5695 }
5696
5697 job->id = 0;
5698 job->user_cb = cb;
5699 atomic_inc(&job->user_cb->cs_cnt);
5700 job->user_cb_size = cb_size;
5701 job->hw_queue_id = GAUDI_QUEUE_ID_DMA_0_0;
5702 job->patched_cb = job->user_cb;
5703 job->job_cb_size = cb_size;
5704
5705 hl_debugfs_add_job(hdev, job);
5706
5707 rc = gaudi_send_job_on_qman0(hdev, job);
5708 hl_debugfs_remove_job(hdev, job);
5709 kfree(job);
5710 atomic_dec(&cb->cs_cnt);
5711
5712release_cb:
5713 hl_cb_put(cb);
5714 hl_cb_destroy(hdev, &hdev->kernel_cb_mgr, cb->id << PAGE_SHIFT);
5715
5716 return rc;
5717}
5718
5719static int gaudi_schedule_register_memset(struct hl_device *hdev,
5720 u32 hw_queue_id, u64 reg_base, u32 num_regs, u32 val)
5721{
farah kassabrie65448f2021-03-30 16:38:02 +03005722 struct hl_ctx *ctx;
Ofir Bitton423815b2021-01-05 09:04:07 +02005723 struct hl_pending_cb *pending_cb;
5724 struct packet_msg_long *pkt;
5725 u32 cb_size, ctl;
5726 struct hl_cb *cb;
farah kassabrie65448f2021-03-30 16:38:02 +03005727 int i, rc;
5728
5729 mutex_lock(&hdev->fpriv_list_lock);
5730 ctx = hdev->compute_ctx;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005731
Ofir Bitton423815b2021-01-05 09:04:07 +02005732 /* If no compute context available or context is going down
5733 * memset registers directly
5734 */
farah kassabrie65448f2021-03-30 16:38:02 +03005735 if (!ctx || kref_read(&ctx->refcount) == 0) {
5736 rc = gaudi_memset_registers(hdev, reg_base, num_regs, val);
5737 mutex_unlock(&hdev->fpriv_list_lock);
5738 return rc;
5739 }
5740
5741 mutex_unlock(&hdev->fpriv_list_lock);
Ofir Bitton423815b2021-01-05 09:04:07 +02005742
5743 cb_size = (sizeof(*pkt) * num_regs) +
5744 sizeof(struct packet_msg_prot) * 2;
5745
5746 if (cb_size > SZ_2M) {
5747 dev_err(hdev->dev, "CB size must be smaller than %uMB", SZ_2M);
5748 return -ENOMEM;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005749 }
5750
Ofir Bitton423815b2021-01-05 09:04:07 +02005751 pending_cb = kzalloc(sizeof(*pending_cb), GFP_KERNEL);
5752 if (!pending_cb)
5753 return -ENOMEM;
5754
5755 cb = hl_cb_kernel_create(hdev, cb_size, false);
5756 if (!cb) {
5757 kfree(pending_cb);
5758 return -EFAULT;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005759 }
5760
Ofir Bitton423815b2021-01-05 09:04:07 +02005761 pkt = cb->kernel_address;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005762
Ofir Bitton423815b2021-01-05 09:04:07 +02005763 ctl = FIELD_PREP(GAUDI_PKT_LONG_CTL_OP_MASK, 0); /* write the value */
5764 ctl |= FIELD_PREP(GAUDI_PKT_CTL_OPCODE_MASK, PACKET_MSG_LONG);
5765 ctl |= FIELD_PREP(GAUDI_PKT_CTL_EB_MASK, 1);
5766 ctl |= FIELD_PREP(GAUDI_PKT_CTL_RB_MASK, 1);
5767 ctl |= FIELD_PREP(GAUDI_PKT_CTL_MB_MASK, 1);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005768
Ofir Bitton423815b2021-01-05 09:04:07 +02005769 for (i = 0; i < num_regs ; i++, pkt++) {
5770 pkt->ctl = cpu_to_le32(ctl);
5771 pkt->value = cpu_to_le32(val);
5772 pkt->addr = cpu_to_le64(reg_base + (i * 4));
5773 }
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005774
Ofir Bitton423815b2021-01-05 09:04:07 +02005775 hl_cb_destroy(hdev, &hdev->kernel_cb_mgr, cb->id << PAGE_SHIFT);
5776
5777 pending_cb->cb = cb;
5778 pending_cb->cb_size = cb_size;
5779 /* The queue ID MUST be an external queue ID. Otherwise, we will
5780 * have undefined behavior
5781 */
5782 pending_cb->hw_queue_id = hw_queue_id;
5783
5784 spin_lock(&ctx->pending_cb_lock);
5785 list_add_tail(&pending_cb->cb_node, &ctx->pending_cb_list);
5786 spin_unlock(&ctx->pending_cb_lock);
5787
5788 return 0;
5789}
5790
5791static int gaudi_restore_sm_registers(struct hl_device *hdev)
5792{
5793 u64 base_addr;
5794 u32 num_regs;
5795 int rc;
5796
5797 base_addr = CFG_BASE + mmSYNC_MNGR_E_N_SYNC_MNGR_OBJS_SOB_OBJ_0;
5798 num_regs = NUM_OF_SOB_IN_BLOCK;
5799 rc = gaudi_memset_registers(hdev, base_addr, num_regs, 0);
5800 if (rc) {
5801 dev_err(hdev->dev, "failed resetting SM registers");
5802 return -ENOMEM;
5803 }
5804
5805 base_addr = CFG_BASE + mmSYNC_MNGR_E_S_SYNC_MNGR_OBJS_SOB_OBJ_0;
5806 num_regs = NUM_OF_SOB_IN_BLOCK;
5807 rc = gaudi_memset_registers(hdev, base_addr, num_regs, 0);
5808 if (rc) {
5809 dev_err(hdev->dev, "failed resetting SM registers");
5810 return -ENOMEM;
5811 }
5812
5813 base_addr = CFG_BASE + mmSYNC_MNGR_W_N_SYNC_MNGR_OBJS_SOB_OBJ_0;
5814 num_regs = NUM_OF_SOB_IN_BLOCK;
5815 rc = gaudi_memset_registers(hdev, base_addr, num_regs, 0);
5816 if (rc) {
5817 dev_err(hdev->dev, "failed resetting SM registers");
5818 return -ENOMEM;
5819 }
5820
5821 base_addr = CFG_BASE + mmSYNC_MNGR_E_N_SYNC_MNGR_OBJS_MON_STATUS_0;
5822 num_regs = NUM_OF_MONITORS_IN_BLOCK;
5823 rc = gaudi_memset_registers(hdev, base_addr, num_regs, 0);
5824 if (rc) {
5825 dev_err(hdev->dev, "failed resetting SM registers");
5826 return -ENOMEM;
5827 }
5828
5829 base_addr = CFG_BASE + mmSYNC_MNGR_E_S_SYNC_MNGR_OBJS_MON_STATUS_0;
5830 num_regs = NUM_OF_MONITORS_IN_BLOCK;
5831 rc = gaudi_memset_registers(hdev, base_addr, num_regs, 0);
5832 if (rc) {
5833 dev_err(hdev->dev, "failed resetting SM registers");
5834 return -ENOMEM;
5835 }
5836
5837 base_addr = CFG_BASE + mmSYNC_MNGR_W_N_SYNC_MNGR_OBJS_MON_STATUS_0;
5838 num_regs = NUM_OF_MONITORS_IN_BLOCK;
5839 rc = gaudi_memset_registers(hdev, base_addr, num_regs, 0);
5840 if (rc) {
5841 dev_err(hdev->dev, "failed resetting SM registers");
5842 return -ENOMEM;
5843 }
5844
5845 base_addr = CFG_BASE + mmSYNC_MNGR_W_S_SYNC_MNGR_OBJS_SOB_OBJ_0 +
5846 (GAUDI_FIRST_AVAILABLE_W_S_SYNC_OBJECT * 4);
5847 num_regs = NUM_OF_SOB_IN_BLOCK - GAUDI_FIRST_AVAILABLE_W_S_SYNC_OBJECT;
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_MON_STATUS_0 +
5855 (GAUDI_FIRST_AVAILABLE_W_S_MONITOR * 4);
5856 num_regs = NUM_OF_MONITORS_IN_BLOCK - GAUDI_FIRST_AVAILABLE_W_S_MONITOR;
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 return 0;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005864}
5865
5866static void gaudi_restore_dma_registers(struct hl_device *hdev)
5867{
5868 u32 sob_delta = mmSYNC_MNGR_E_N_SYNC_MNGR_OBJS_SOB_OBJ_1 -
5869 mmSYNC_MNGR_E_N_SYNC_MNGR_OBJS_SOB_OBJ_0;
5870 int i;
5871
5872 for (i = 0 ; i < DMA_NUMBER_OF_CHANNELS ; i++) {
5873 u64 sob_addr = CFG_BASE +
5874 mmSYNC_MNGR_E_N_SYNC_MNGR_OBJS_SOB_OBJ_0 +
5875 (i * sob_delta);
5876 u32 dma_offset = i * DMA_CORE_OFFSET;
5877
5878 WREG32(mmDMA0_CORE_WR_COMP_ADDR_LO + dma_offset,
5879 lower_32_bits(sob_addr));
5880 WREG32(mmDMA0_CORE_WR_COMP_ADDR_HI + dma_offset,
5881 upper_32_bits(sob_addr));
5882 WREG32(mmDMA0_CORE_WR_COMP_WDATA + dma_offset, 0x80000001);
5883
5884 /* For DMAs 2-7, need to restore WR_AWUSER_31_11 as it can be
5885 * modified by the user for SRAM reduction
5886 */
5887 if (i > 1)
5888 WREG32(mmDMA0_CORE_WR_AWUSER_31_11 + dma_offset,
5889 0x00000001);
5890 }
5891}
5892
5893static void gaudi_restore_qm_registers(struct hl_device *hdev)
5894{
5895 u32 qman_offset;
5896 int i;
5897
5898 for (i = 0 ; i < DMA_NUMBER_OF_CHANNELS ; i++) {
5899 qman_offset = i * DMA_QMAN_OFFSET;
5900 WREG32(mmDMA0_QM_ARB_CFG_0 + qman_offset, 0);
5901 }
5902
5903 for (i = 0 ; i < MME_NUMBER_OF_MASTER_ENGINES ; i++) {
5904 qman_offset = i * (mmMME2_QM_BASE - mmMME0_QM_BASE);
5905 WREG32(mmMME0_QM_ARB_CFG_0 + qman_offset, 0);
5906 }
5907
5908 for (i = 0 ; i < TPC_NUMBER_OF_ENGINES ; i++) {
5909 qman_offset = i * TPC_QMAN_OFFSET;
5910 WREG32(mmTPC0_QM_ARB_CFG_0 + qman_offset, 0);
5911 }
Oded Gabbay3c681572020-11-02 21:10:39 +02005912
5913 for (i = 0 ; i < NIC_NUMBER_OF_ENGINES ; i++) {
5914 qman_offset = (i >> 1) * NIC_MACRO_QMAN_OFFSET +
5915 (i & 0x1) * NIC_ENGINE_QMAN_OFFSET;
5916 WREG32(mmNIC0_QM0_ARB_CFG_0 + qman_offset, 0);
5917 }
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005918}
5919
Ofir Bitton423815b2021-01-05 09:04:07 +02005920static int gaudi_restore_user_registers(struct hl_device *hdev)
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005921{
Ofir Bitton423815b2021-01-05 09:04:07 +02005922 int rc;
5923
5924 rc = gaudi_restore_sm_registers(hdev);
5925 if (rc)
5926 return rc;
5927
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005928 gaudi_restore_dma_registers(hdev);
5929 gaudi_restore_qm_registers(hdev);
Ofir Bitton423815b2021-01-05 09:04:07 +02005930
5931 return 0;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005932}
5933
5934static int gaudi_context_switch(struct hl_device *hdev, u32 asid)
5935{
Ofir Bitton423815b2021-01-05 09:04:07 +02005936 return gaudi_restore_user_registers(hdev);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005937}
5938
5939static int gaudi_mmu_clear_pgt_range(struct hl_device *hdev)
5940{
5941 struct asic_fixed_properties *prop = &hdev->asic_prop;
5942 struct gaudi_device *gaudi = hdev->asic_specific;
5943 u64 addr = prop->mmu_pgt_addr;
5944 u32 size = prop->mmu_pgt_size + MMU_CACHE_MNG_SIZE;
5945
5946 if (!(gaudi->hw_cap_initialized & HW_CAP_MMU))
5947 return 0;
5948
5949 return gaudi_memset_device_memory(hdev, addr, size, 0);
5950}
5951
5952static void gaudi_restore_phase_topology(struct hl_device *hdev)
5953{
5954
5955}
5956
Ofir Bittona5778d12021-02-24 11:51:40 +02005957static int gaudi_debugfs_read32(struct hl_device *hdev, u64 addr,
5958 bool user_address, u32 *val)
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005959{
5960 struct asic_fixed_properties *prop = &hdev->asic_prop;
5961 struct gaudi_device *gaudi = hdev->asic_specific;
Ofir Bittona5778d12021-02-24 11:51:40 +02005962 u64 hbm_bar_addr, host_phys_end;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005963 int rc = 0;
5964
Ofir Bittona5778d12021-02-24 11:51:40 +02005965 host_phys_end = HOST_PHYS_BASE + HOST_PHYS_SIZE;
5966
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005967 if ((addr >= CFG_BASE) && (addr < CFG_BASE + CFG_SIZE)) {
Oded Gabbaye38bfd32020-07-03 20:46:12 +03005968
5969 if ((gaudi->hw_cap_initialized & HW_CAP_CLK_GATE) &&
5970 (hdev->clock_gating_mask &
5971 GAUDI_CLK_GATE_DEBUGFS_MASK)) {
5972
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005973 dev_err_ratelimited(hdev->dev,
5974 "Can't read register - clock gating is enabled!\n");
5975 rc = -EFAULT;
5976 } else {
5977 *val = RREG32(addr - CFG_BASE);
5978 }
Oded Gabbaye38bfd32020-07-03 20:46:12 +03005979
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005980 } else if ((addr >= SRAM_BASE_ADDR) &&
5981 (addr < SRAM_BASE_ADDR + SRAM_BAR_SIZE)) {
5982 *val = readl(hdev->pcie_bar[SRAM_BAR_ID] +
5983 (addr - SRAM_BASE_ADDR));
5984 } else if (addr < DRAM_PHYS_BASE + hdev->asic_prop.dram_size) {
5985 u64 bar_base_addr = DRAM_PHYS_BASE +
5986 (addr & ~(prop->dram_pci_bar_size - 0x1ull));
5987
5988 hbm_bar_addr = gaudi_set_hbm_bar_base(hdev, bar_base_addr);
5989 if (hbm_bar_addr != U64_MAX) {
5990 *val = readl(hdev->pcie_bar[HBM_BAR_ID] +
5991 (addr - bar_base_addr));
5992
5993 hbm_bar_addr = gaudi_set_hbm_bar_base(hdev,
5994 hbm_bar_addr);
5995 }
5996 if (hbm_bar_addr == U64_MAX)
5997 rc = -EIO;
Ofir Bittona5778d12021-02-24 11:51:40 +02005998 } else if (addr >= HOST_PHYS_BASE && addr < host_phys_end &&
5999 user_address && !iommu_present(&pci_bus_type)) {
6000 *val = *(u32 *) phys_to_virt(addr - HOST_PHYS_BASE);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03006001 } else {
6002 rc = -EFAULT;
6003 }
6004
6005 return rc;
6006}
6007
Ofir Bittona5778d12021-02-24 11:51:40 +02006008static int gaudi_debugfs_write32(struct hl_device *hdev, u64 addr,
6009 bool user_address, u32 val)
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03006010{
6011 struct asic_fixed_properties *prop = &hdev->asic_prop;
6012 struct gaudi_device *gaudi = hdev->asic_specific;
Ofir Bittona5778d12021-02-24 11:51:40 +02006013 u64 hbm_bar_addr, host_phys_end;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03006014 int rc = 0;
6015
Ofir Bittona5778d12021-02-24 11:51:40 +02006016 host_phys_end = HOST_PHYS_BASE + HOST_PHYS_SIZE;
6017
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03006018 if ((addr >= CFG_BASE) && (addr < CFG_BASE + CFG_SIZE)) {
Oded Gabbaye38bfd32020-07-03 20:46:12 +03006019
6020 if ((gaudi->hw_cap_initialized & HW_CAP_CLK_GATE) &&
6021 (hdev->clock_gating_mask &
6022 GAUDI_CLK_GATE_DEBUGFS_MASK)) {
6023
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03006024 dev_err_ratelimited(hdev->dev,
6025 "Can't write register - clock gating is enabled!\n");
6026 rc = -EFAULT;
6027 } else {
6028 WREG32(addr - CFG_BASE, val);
6029 }
Oded Gabbaye38bfd32020-07-03 20:46:12 +03006030
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03006031 } else if ((addr >= SRAM_BASE_ADDR) &&
6032 (addr < SRAM_BASE_ADDR + SRAM_BAR_SIZE)) {
6033 writel(val, hdev->pcie_bar[SRAM_BAR_ID] +
6034 (addr - SRAM_BASE_ADDR));
6035 } else if (addr < DRAM_PHYS_BASE + hdev->asic_prop.dram_size) {
6036 u64 bar_base_addr = DRAM_PHYS_BASE +
6037 (addr & ~(prop->dram_pci_bar_size - 0x1ull));
6038
6039 hbm_bar_addr = gaudi_set_hbm_bar_base(hdev, bar_base_addr);
6040 if (hbm_bar_addr != U64_MAX) {
6041 writel(val, hdev->pcie_bar[HBM_BAR_ID] +
6042 (addr - bar_base_addr));
6043
6044 hbm_bar_addr = gaudi_set_hbm_bar_base(hdev,
6045 hbm_bar_addr);
6046 }
6047 if (hbm_bar_addr == U64_MAX)
6048 rc = -EIO;
Ofir Bittona5778d12021-02-24 11:51:40 +02006049 } else if (addr >= HOST_PHYS_BASE && addr < host_phys_end &&
6050 user_address && !iommu_present(&pci_bus_type)) {
6051 *(u32 *) phys_to_virt(addr - HOST_PHYS_BASE) = val;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03006052 } else {
6053 rc = -EFAULT;
6054 }
6055
6056 return rc;
6057}
6058
Ofir Bittona5778d12021-02-24 11:51:40 +02006059static int gaudi_debugfs_read64(struct hl_device *hdev, u64 addr,
6060 bool user_address, u64 *val)
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03006061{
6062 struct asic_fixed_properties *prop = &hdev->asic_prop;
6063 struct gaudi_device *gaudi = hdev->asic_specific;
Ofir Bittona5778d12021-02-24 11:51:40 +02006064 u64 hbm_bar_addr, host_phys_end;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03006065 int rc = 0;
6066
Ofir Bittona5778d12021-02-24 11:51:40 +02006067 host_phys_end = HOST_PHYS_BASE + HOST_PHYS_SIZE;
6068
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03006069 if ((addr >= CFG_BASE) && (addr <= CFG_BASE + CFG_SIZE - sizeof(u64))) {
Oded Gabbaye38bfd32020-07-03 20:46:12 +03006070
6071 if ((gaudi->hw_cap_initialized & HW_CAP_CLK_GATE) &&
6072 (hdev->clock_gating_mask &
6073 GAUDI_CLK_GATE_DEBUGFS_MASK)) {
6074
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03006075 dev_err_ratelimited(hdev->dev,
6076 "Can't read register - clock gating is enabled!\n");
6077 rc = -EFAULT;
6078 } else {
6079 u32 val_l = RREG32(addr - CFG_BASE);
6080 u32 val_h = RREG32(addr + sizeof(u32) - CFG_BASE);
6081
6082 *val = (((u64) val_h) << 32) | val_l;
6083 }
Oded Gabbaye38bfd32020-07-03 20:46:12 +03006084
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03006085 } else if ((addr >= SRAM_BASE_ADDR) &&
6086 (addr <= SRAM_BASE_ADDR + SRAM_BAR_SIZE - sizeof(u64))) {
6087 *val = readq(hdev->pcie_bar[SRAM_BAR_ID] +
6088 (addr - SRAM_BASE_ADDR));
6089 } else if (addr <=
6090 DRAM_PHYS_BASE + hdev->asic_prop.dram_size - sizeof(u64)) {
6091 u64 bar_base_addr = DRAM_PHYS_BASE +
6092 (addr & ~(prop->dram_pci_bar_size - 0x1ull));
6093
6094 hbm_bar_addr = gaudi_set_hbm_bar_base(hdev, bar_base_addr);
6095 if (hbm_bar_addr != U64_MAX) {
6096 *val = readq(hdev->pcie_bar[HBM_BAR_ID] +
6097 (addr - bar_base_addr));
6098
6099 hbm_bar_addr = gaudi_set_hbm_bar_base(hdev,
6100 hbm_bar_addr);
6101 }
6102 if (hbm_bar_addr == U64_MAX)
6103 rc = -EIO;
Ofir Bittona5778d12021-02-24 11:51:40 +02006104 } else if (addr >= HOST_PHYS_BASE && addr < host_phys_end &&
6105 user_address && !iommu_present(&pci_bus_type)) {
6106 *val = *(u64 *) phys_to_virt(addr - HOST_PHYS_BASE);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03006107 } else {
6108 rc = -EFAULT;
6109 }
6110
6111 return rc;
6112}
6113
Ofir Bittona5778d12021-02-24 11:51:40 +02006114static int gaudi_debugfs_write64(struct hl_device *hdev, u64 addr,
6115 bool user_address, u64 val)
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03006116{
6117 struct asic_fixed_properties *prop = &hdev->asic_prop;
6118 struct gaudi_device *gaudi = hdev->asic_specific;
Ofir Bittona5778d12021-02-24 11:51:40 +02006119 u64 hbm_bar_addr, host_phys_end;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03006120 int rc = 0;
6121
Ofir Bittona5778d12021-02-24 11:51:40 +02006122 host_phys_end = HOST_PHYS_BASE + HOST_PHYS_SIZE;
6123
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03006124 if ((addr >= CFG_BASE) && (addr <= CFG_BASE + CFG_SIZE - sizeof(u64))) {
Oded Gabbaye38bfd32020-07-03 20:46:12 +03006125
6126 if ((gaudi->hw_cap_initialized & HW_CAP_CLK_GATE) &&
6127 (hdev->clock_gating_mask &
6128 GAUDI_CLK_GATE_DEBUGFS_MASK)) {
6129
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03006130 dev_err_ratelimited(hdev->dev,
6131 "Can't write register - clock gating is enabled!\n");
6132 rc = -EFAULT;
6133 } else {
6134 WREG32(addr - CFG_BASE, lower_32_bits(val));
6135 WREG32(addr + sizeof(u32) - CFG_BASE,
6136 upper_32_bits(val));
6137 }
Oded Gabbaye38bfd32020-07-03 20:46:12 +03006138
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03006139 } else if ((addr >= SRAM_BASE_ADDR) &&
6140 (addr <= SRAM_BASE_ADDR + SRAM_BAR_SIZE - sizeof(u64))) {
6141 writeq(val, hdev->pcie_bar[SRAM_BAR_ID] +
6142 (addr - SRAM_BASE_ADDR));
6143 } else if (addr <=
6144 DRAM_PHYS_BASE + hdev->asic_prop.dram_size - sizeof(u64)) {
6145 u64 bar_base_addr = DRAM_PHYS_BASE +
6146 (addr & ~(prop->dram_pci_bar_size - 0x1ull));
6147
6148 hbm_bar_addr = gaudi_set_hbm_bar_base(hdev, bar_base_addr);
6149 if (hbm_bar_addr != U64_MAX) {
6150 writeq(val, hdev->pcie_bar[HBM_BAR_ID] +
6151 (addr - bar_base_addr));
6152
6153 hbm_bar_addr = gaudi_set_hbm_bar_base(hdev,
6154 hbm_bar_addr);
6155 }
6156 if (hbm_bar_addr == U64_MAX)
6157 rc = -EIO;
Ofir Bittona5778d12021-02-24 11:51:40 +02006158 } else if (addr >= HOST_PHYS_BASE && addr < host_phys_end &&
6159 user_address && !iommu_present(&pci_bus_type)) {
6160 *(u64 *) phys_to_virt(addr - HOST_PHYS_BASE) = val;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03006161 } else {
6162 rc = -EFAULT;
6163 }
6164
6165 return rc;
6166}
6167
Oded Gabbay639781d2021-04-02 01:43:18 +03006168static int gaudi_dma_core_transfer(struct hl_device *hdev, int dma_id, u64 addr,
6169 u32 size_to_dma, dma_addr_t dma_addr)
6170{
6171 u32 err_cause, val;
6172 u64 dma_offset;
6173 int rc;
6174
6175 dma_offset = dma_id * DMA_CORE_OFFSET;
6176
6177 WREG32(mmDMA0_CORE_SRC_BASE_LO + dma_offset, lower_32_bits(addr));
6178 WREG32(mmDMA0_CORE_SRC_BASE_HI + dma_offset, upper_32_bits(addr));
6179 WREG32(mmDMA0_CORE_DST_BASE_LO + dma_offset, lower_32_bits(dma_addr));
6180 WREG32(mmDMA0_CORE_DST_BASE_HI + dma_offset, upper_32_bits(dma_addr));
6181 WREG32(mmDMA0_CORE_DST_TSIZE_0 + dma_offset, size_to_dma);
6182 WREG32(mmDMA0_CORE_COMMIT + dma_offset,
6183 (1 << DMA0_CORE_COMMIT_LIN_SHIFT));
6184
6185 rc = hl_poll_timeout(
6186 hdev,
6187 mmDMA0_CORE_STS0 + dma_offset,
6188 val,
6189 ((val & DMA0_CORE_STS0_BUSY_MASK) == 0),
6190 0,
6191 1000000);
6192
6193 if (rc) {
6194 dev_err(hdev->dev,
6195 "DMA %d timed-out during reading of 0x%llx\n",
6196 dma_id, addr);
6197 return -EIO;
6198 }
6199
6200 /* Verify DMA is OK */
6201 err_cause = RREG32(mmDMA0_CORE_ERR_CAUSE + dma_offset);
6202 if (err_cause) {
6203 dev_err(hdev->dev, "DMA Failed, cause 0x%x\n", err_cause);
6204 dev_dbg(hdev->dev,
6205 "Clearing DMA0 engine from errors (cause 0x%x)\n",
6206 err_cause);
6207 WREG32(mmDMA0_CORE_ERR_CAUSE + dma_offset, err_cause);
6208
6209 return -EIO;
6210 }
6211
6212 return 0;
6213}
6214
6215static int gaudi_debugfs_read_dma(struct hl_device *hdev, u64 addr, u32 size,
6216 void *blob_addr)
6217{
6218 u32 dma_core_sts0, err_cause, cfg1, size_left, pos, size_to_dma;
6219 struct gaudi_device *gaudi = hdev->asic_specific;
6220 u64 dma_offset, qm_offset;
6221 dma_addr_t dma_addr;
6222 void *kernel_addr;
6223 bool is_eng_idle;
Colin Ian Kingb4e964f2021-04-12 17:10:12 +01006224 int rc = 0, dma_id;
Oded Gabbay639781d2021-04-02 01:43:18 +03006225
6226 kernel_addr = hdev->asic_funcs->asic_dma_alloc_coherent(
6227 hdev, SZ_2M,
6228 &dma_addr,
6229 GFP_KERNEL | __GFP_ZERO);
6230
6231 if (!kernel_addr)
6232 return -ENOMEM;
6233
6234 mutex_lock(&gaudi->clk_gate_mutex);
6235
6236 hdev->asic_funcs->disable_clock_gating(hdev);
6237
6238 hdev->asic_funcs->hw_queues_lock(hdev);
6239
6240 dma_id = gaudi_dma_assignment[GAUDI_PCI_DMA_1];
6241 dma_offset = dma_id * DMA_CORE_OFFSET;
6242 qm_offset = dma_id * DMA_QMAN_OFFSET;
6243 dma_core_sts0 = RREG32(mmDMA0_CORE_STS0 + dma_offset);
6244 is_eng_idle = IS_DMA_IDLE(dma_core_sts0);
6245
6246 if (!is_eng_idle) {
6247 dma_id = gaudi_dma_assignment[GAUDI_PCI_DMA_2];
6248 dma_offset = dma_id * DMA_CORE_OFFSET;
6249 qm_offset = dma_id * DMA_QMAN_OFFSET;
6250 dma_core_sts0 = RREG32(mmDMA0_CORE_STS0 + dma_offset);
6251 is_eng_idle = IS_DMA_IDLE(dma_core_sts0);
6252
6253 if (!is_eng_idle) {
6254 dev_err_ratelimited(hdev->dev,
6255 "Can't read via DMA because it is BUSY\n");
6256 rc = -EAGAIN;
6257 goto out;
6258 }
6259 }
6260
6261 cfg1 = RREG32(mmDMA0_QM_GLBL_CFG1 + qm_offset);
6262 WREG32(mmDMA0_QM_GLBL_CFG1 + qm_offset,
6263 0xF << DMA0_QM_GLBL_CFG1_CP_STOP_SHIFT);
6264
6265 /* TODO: remove this by mapping the DMA temporary buffer to the MMU
6266 * using the compute ctx ASID, if exists. If not, use the kernel ctx
6267 * ASID
6268 */
6269 WREG32_OR(mmDMA0_CORE_PROT + dma_offset, BIT(DMA0_CORE_PROT_VAL_SHIFT));
6270
6271 /* Verify DMA is OK */
6272 err_cause = RREG32(mmDMA0_CORE_ERR_CAUSE + dma_offset);
6273 if (err_cause) {
6274 dev_dbg(hdev->dev,
6275 "Clearing DMA0 engine from errors (cause 0x%x)\n",
6276 err_cause);
6277 WREG32(mmDMA0_CORE_ERR_CAUSE + dma_offset, err_cause);
6278 }
6279
6280 pos = 0;
6281 size_left = size;
6282 size_to_dma = SZ_2M;
6283
6284 while (size_left > 0) {
6285
6286 if (size_left < SZ_2M)
6287 size_to_dma = size_left;
6288
6289 rc = gaudi_dma_core_transfer(hdev, dma_id, addr, size_to_dma,
6290 dma_addr);
6291 if (rc)
6292 break;
6293
6294 memcpy(blob_addr + pos, kernel_addr, size_to_dma);
6295
6296 if (size_left <= SZ_2M)
6297 break;
6298
6299 pos += SZ_2M;
6300 addr += SZ_2M;
6301 size_left -= SZ_2M;
6302 }
6303
6304 /* TODO: remove this by mapping the DMA temporary buffer to the MMU
6305 * using the compute ctx ASID, if exists. If not, use the kernel ctx
6306 * ASID
6307 */
6308 WREG32_AND(mmDMA0_CORE_PROT + dma_offset,
6309 ~BIT(DMA0_CORE_PROT_VAL_SHIFT));
6310
6311 WREG32(mmDMA0_QM_GLBL_CFG1 + qm_offset, cfg1);
6312
6313out:
6314 hdev->asic_funcs->hw_queues_unlock(hdev);
6315
6316 hdev->asic_funcs->set_clock_gating(hdev);
6317
6318 mutex_unlock(&gaudi->clk_gate_mutex);
6319
6320 hdev->asic_funcs->asic_dma_free_coherent(hdev, SZ_2M, kernel_addr,
6321 dma_addr);
6322
6323 return rc;
6324}
6325
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03006326static u64 gaudi_read_pte(struct hl_device *hdev, u64 addr)
6327{
6328 struct gaudi_device *gaudi = hdev->asic_specific;
6329
6330 if (hdev->hard_reset_pending)
6331 return U64_MAX;
6332
6333 return readq(hdev->pcie_bar[HBM_BAR_ID] +
6334 (addr - gaudi->hbm_bar_cur_addr));
6335}
6336
6337static void gaudi_write_pte(struct hl_device *hdev, u64 addr, u64 val)
6338{
6339 struct gaudi_device *gaudi = hdev->asic_specific;
6340
6341 if (hdev->hard_reset_pending)
6342 return;
6343
6344 writeq(val, hdev->pcie_bar[HBM_BAR_ID] +
6345 (addr - gaudi->hbm_bar_cur_addr));
6346}
6347
Ofir Bitton1137e1e2020-09-30 18:43:52 +03006348void gaudi_mmu_prepare_reg(struct hl_device *hdev, u64 reg, u32 asid)
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03006349{
6350 /* mask to zero the MMBP and ASID bits */
6351 WREG32_AND(reg, ~0x7FF);
6352 WREG32_OR(reg, asid);
6353}
6354
6355static void gaudi_mmu_prepare(struct hl_device *hdev, u32 asid)
6356{
6357 struct gaudi_device *gaudi = hdev->asic_specific;
6358
6359 if (!(gaudi->hw_cap_initialized & HW_CAP_MMU))
6360 return;
6361
6362 if (asid & ~DMA0_QM_GLBL_NON_SECURE_PROPS_0_ASID_MASK) {
Alon Mizrahi75d9a2a2020-12-03 17:32:19 +02006363 dev_crit(hdev->dev, "asid %u is too big\n", asid);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03006364 return;
6365 }
6366
6367 mutex_lock(&gaudi->clk_gate_mutex);
6368
6369 hdev->asic_funcs->disable_clock_gating(hdev);
6370
6371 gaudi_mmu_prepare_reg(hdev, mmDMA0_QM_GLBL_NON_SECURE_PROPS_0, asid);
6372 gaudi_mmu_prepare_reg(hdev, mmDMA0_QM_GLBL_NON_SECURE_PROPS_1, asid);
6373 gaudi_mmu_prepare_reg(hdev, mmDMA0_QM_GLBL_NON_SECURE_PROPS_2, asid);
6374 gaudi_mmu_prepare_reg(hdev, mmDMA0_QM_GLBL_NON_SECURE_PROPS_3, asid);
6375 gaudi_mmu_prepare_reg(hdev, mmDMA0_QM_GLBL_NON_SECURE_PROPS_4, asid);
6376
6377 gaudi_mmu_prepare_reg(hdev, mmDMA1_QM_GLBL_NON_SECURE_PROPS_0, asid);
6378 gaudi_mmu_prepare_reg(hdev, mmDMA1_QM_GLBL_NON_SECURE_PROPS_1, asid);
6379 gaudi_mmu_prepare_reg(hdev, mmDMA1_QM_GLBL_NON_SECURE_PROPS_2, asid);
6380 gaudi_mmu_prepare_reg(hdev, mmDMA1_QM_GLBL_NON_SECURE_PROPS_3, asid);
6381 gaudi_mmu_prepare_reg(hdev, mmDMA1_QM_GLBL_NON_SECURE_PROPS_4, asid);
6382
6383 gaudi_mmu_prepare_reg(hdev, mmDMA2_QM_GLBL_NON_SECURE_PROPS_0, asid);
6384 gaudi_mmu_prepare_reg(hdev, mmDMA2_QM_GLBL_NON_SECURE_PROPS_1, asid);
6385 gaudi_mmu_prepare_reg(hdev, mmDMA2_QM_GLBL_NON_SECURE_PROPS_2, asid);
6386 gaudi_mmu_prepare_reg(hdev, mmDMA2_QM_GLBL_NON_SECURE_PROPS_3, asid);
6387 gaudi_mmu_prepare_reg(hdev, mmDMA2_QM_GLBL_NON_SECURE_PROPS_4, asid);
6388
6389 gaudi_mmu_prepare_reg(hdev, mmDMA3_QM_GLBL_NON_SECURE_PROPS_0, asid);
6390 gaudi_mmu_prepare_reg(hdev, mmDMA3_QM_GLBL_NON_SECURE_PROPS_1, asid);
6391 gaudi_mmu_prepare_reg(hdev, mmDMA3_QM_GLBL_NON_SECURE_PROPS_2, asid);
6392 gaudi_mmu_prepare_reg(hdev, mmDMA3_QM_GLBL_NON_SECURE_PROPS_3, asid);
6393 gaudi_mmu_prepare_reg(hdev, mmDMA3_QM_GLBL_NON_SECURE_PROPS_4, asid);
6394
6395 gaudi_mmu_prepare_reg(hdev, mmDMA4_QM_GLBL_NON_SECURE_PROPS_0, asid);
6396 gaudi_mmu_prepare_reg(hdev, mmDMA4_QM_GLBL_NON_SECURE_PROPS_1, asid);
6397 gaudi_mmu_prepare_reg(hdev, mmDMA4_QM_GLBL_NON_SECURE_PROPS_2, asid);
6398 gaudi_mmu_prepare_reg(hdev, mmDMA4_QM_GLBL_NON_SECURE_PROPS_3, asid);
6399 gaudi_mmu_prepare_reg(hdev, mmDMA4_QM_GLBL_NON_SECURE_PROPS_4, asid);
6400
6401 gaudi_mmu_prepare_reg(hdev, mmDMA5_QM_GLBL_NON_SECURE_PROPS_0, asid);
6402 gaudi_mmu_prepare_reg(hdev, mmDMA5_QM_GLBL_NON_SECURE_PROPS_1, asid);
6403 gaudi_mmu_prepare_reg(hdev, mmDMA5_QM_GLBL_NON_SECURE_PROPS_2, asid);
6404 gaudi_mmu_prepare_reg(hdev, mmDMA5_QM_GLBL_NON_SECURE_PROPS_3, asid);
6405 gaudi_mmu_prepare_reg(hdev, mmDMA5_QM_GLBL_NON_SECURE_PROPS_4, asid);
6406
6407 gaudi_mmu_prepare_reg(hdev, mmDMA6_QM_GLBL_NON_SECURE_PROPS_0, asid);
6408 gaudi_mmu_prepare_reg(hdev, mmDMA6_QM_GLBL_NON_SECURE_PROPS_1, asid);
6409 gaudi_mmu_prepare_reg(hdev, mmDMA6_QM_GLBL_NON_SECURE_PROPS_2, asid);
6410 gaudi_mmu_prepare_reg(hdev, mmDMA6_QM_GLBL_NON_SECURE_PROPS_3, asid);
6411 gaudi_mmu_prepare_reg(hdev, mmDMA6_QM_GLBL_NON_SECURE_PROPS_4, asid);
6412
6413 gaudi_mmu_prepare_reg(hdev, mmDMA7_QM_GLBL_NON_SECURE_PROPS_0, asid);
6414 gaudi_mmu_prepare_reg(hdev, mmDMA7_QM_GLBL_NON_SECURE_PROPS_1, asid);
6415 gaudi_mmu_prepare_reg(hdev, mmDMA7_QM_GLBL_NON_SECURE_PROPS_2, asid);
6416 gaudi_mmu_prepare_reg(hdev, mmDMA7_QM_GLBL_NON_SECURE_PROPS_3, asid);
6417 gaudi_mmu_prepare_reg(hdev, mmDMA7_QM_GLBL_NON_SECURE_PROPS_4, asid);
6418
6419 gaudi_mmu_prepare_reg(hdev, mmDMA0_CORE_NON_SECURE_PROPS, asid);
6420 gaudi_mmu_prepare_reg(hdev, mmDMA1_CORE_NON_SECURE_PROPS, asid);
6421 gaudi_mmu_prepare_reg(hdev, mmDMA2_CORE_NON_SECURE_PROPS, asid);
6422 gaudi_mmu_prepare_reg(hdev, mmDMA3_CORE_NON_SECURE_PROPS, asid);
6423 gaudi_mmu_prepare_reg(hdev, mmDMA4_CORE_NON_SECURE_PROPS, asid);
6424 gaudi_mmu_prepare_reg(hdev, mmDMA5_CORE_NON_SECURE_PROPS, asid);
6425 gaudi_mmu_prepare_reg(hdev, mmDMA6_CORE_NON_SECURE_PROPS, asid);
6426 gaudi_mmu_prepare_reg(hdev, mmDMA7_CORE_NON_SECURE_PROPS, asid);
6427
6428 gaudi_mmu_prepare_reg(hdev, mmTPC0_QM_GLBL_NON_SECURE_PROPS_0, asid);
6429 gaudi_mmu_prepare_reg(hdev, mmTPC0_QM_GLBL_NON_SECURE_PROPS_1, asid);
6430 gaudi_mmu_prepare_reg(hdev, mmTPC0_QM_GLBL_NON_SECURE_PROPS_2, asid);
6431 gaudi_mmu_prepare_reg(hdev, mmTPC0_QM_GLBL_NON_SECURE_PROPS_3, asid);
6432 gaudi_mmu_prepare_reg(hdev, mmTPC0_QM_GLBL_NON_SECURE_PROPS_4, asid);
6433 gaudi_mmu_prepare_reg(hdev, mmTPC0_CFG_ARUSER_LO, asid);
6434 gaudi_mmu_prepare_reg(hdev, mmTPC0_CFG_AWUSER_LO, asid);
6435
6436 gaudi_mmu_prepare_reg(hdev, mmTPC1_QM_GLBL_NON_SECURE_PROPS_0, asid);
6437 gaudi_mmu_prepare_reg(hdev, mmTPC1_QM_GLBL_NON_SECURE_PROPS_1, asid);
6438 gaudi_mmu_prepare_reg(hdev, mmTPC1_QM_GLBL_NON_SECURE_PROPS_2, asid);
6439 gaudi_mmu_prepare_reg(hdev, mmTPC1_QM_GLBL_NON_SECURE_PROPS_3, asid);
6440 gaudi_mmu_prepare_reg(hdev, mmTPC1_QM_GLBL_NON_SECURE_PROPS_4, asid);
6441 gaudi_mmu_prepare_reg(hdev, mmTPC1_CFG_ARUSER_LO, asid);
6442 gaudi_mmu_prepare_reg(hdev, mmTPC1_CFG_AWUSER_LO, asid);
6443
6444 gaudi_mmu_prepare_reg(hdev, mmTPC2_QM_GLBL_NON_SECURE_PROPS_0, asid);
6445 gaudi_mmu_prepare_reg(hdev, mmTPC2_QM_GLBL_NON_SECURE_PROPS_1, asid);
6446 gaudi_mmu_prepare_reg(hdev, mmTPC2_QM_GLBL_NON_SECURE_PROPS_2, asid);
6447 gaudi_mmu_prepare_reg(hdev, mmTPC2_QM_GLBL_NON_SECURE_PROPS_3, asid);
6448 gaudi_mmu_prepare_reg(hdev, mmTPC2_QM_GLBL_NON_SECURE_PROPS_4, asid);
6449 gaudi_mmu_prepare_reg(hdev, mmTPC2_CFG_ARUSER_LO, asid);
6450 gaudi_mmu_prepare_reg(hdev, mmTPC2_CFG_AWUSER_LO, asid);
6451
6452 gaudi_mmu_prepare_reg(hdev, mmTPC3_QM_GLBL_NON_SECURE_PROPS_0, asid);
6453 gaudi_mmu_prepare_reg(hdev, mmTPC3_QM_GLBL_NON_SECURE_PROPS_1, asid);
6454 gaudi_mmu_prepare_reg(hdev, mmTPC3_QM_GLBL_NON_SECURE_PROPS_2, asid);
6455 gaudi_mmu_prepare_reg(hdev, mmTPC3_QM_GLBL_NON_SECURE_PROPS_3, asid);
6456 gaudi_mmu_prepare_reg(hdev, mmTPC3_QM_GLBL_NON_SECURE_PROPS_4, asid);
6457 gaudi_mmu_prepare_reg(hdev, mmTPC3_CFG_ARUSER_LO, asid);
6458 gaudi_mmu_prepare_reg(hdev, mmTPC3_CFG_AWUSER_LO, asid);
6459
6460 gaudi_mmu_prepare_reg(hdev, mmTPC4_QM_GLBL_NON_SECURE_PROPS_0, asid);
6461 gaudi_mmu_prepare_reg(hdev, mmTPC4_QM_GLBL_NON_SECURE_PROPS_1, asid);
6462 gaudi_mmu_prepare_reg(hdev, mmTPC4_QM_GLBL_NON_SECURE_PROPS_2, asid);
6463 gaudi_mmu_prepare_reg(hdev, mmTPC4_QM_GLBL_NON_SECURE_PROPS_3, asid);
6464 gaudi_mmu_prepare_reg(hdev, mmTPC4_QM_GLBL_NON_SECURE_PROPS_4, asid);
6465 gaudi_mmu_prepare_reg(hdev, mmTPC4_CFG_ARUSER_LO, asid);
6466 gaudi_mmu_prepare_reg(hdev, mmTPC4_CFG_AWUSER_LO, asid);
6467
6468 gaudi_mmu_prepare_reg(hdev, mmTPC5_QM_GLBL_NON_SECURE_PROPS_0, asid);
6469 gaudi_mmu_prepare_reg(hdev, mmTPC5_QM_GLBL_NON_SECURE_PROPS_1, asid);
6470 gaudi_mmu_prepare_reg(hdev, mmTPC5_QM_GLBL_NON_SECURE_PROPS_2, asid);
6471 gaudi_mmu_prepare_reg(hdev, mmTPC5_QM_GLBL_NON_SECURE_PROPS_3, asid);
6472 gaudi_mmu_prepare_reg(hdev, mmTPC5_QM_GLBL_NON_SECURE_PROPS_4, asid);
6473 gaudi_mmu_prepare_reg(hdev, mmTPC5_CFG_ARUSER_LO, asid);
6474 gaudi_mmu_prepare_reg(hdev, mmTPC5_CFG_AWUSER_LO, asid);
6475
6476 gaudi_mmu_prepare_reg(hdev, mmTPC6_QM_GLBL_NON_SECURE_PROPS_0, asid);
6477 gaudi_mmu_prepare_reg(hdev, mmTPC6_QM_GLBL_NON_SECURE_PROPS_1, asid);
6478 gaudi_mmu_prepare_reg(hdev, mmTPC6_QM_GLBL_NON_SECURE_PROPS_2, asid);
6479 gaudi_mmu_prepare_reg(hdev, mmTPC6_QM_GLBL_NON_SECURE_PROPS_3, asid);
6480 gaudi_mmu_prepare_reg(hdev, mmTPC6_QM_GLBL_NON_SECURE_PROPS_4, asid);
6481 gaudi_mmu_prepare_reg(hdev, mmTPC6_CFG_ARUSER_LO, asid);
6482 gaudi_mmu_prepare_reg(hdev, mmTPC6_CFG_AWUSER_LO, asid);
6483
6484 gaudi_mmu_prepare_reg(hdev, mmTPC7_QM_GLBL_NON_SECURE_PROPS_0, asid);
6485 gaudi_mmu_prepare_reg(hdev, mmTPC7_QM_GLBL_NON_SECURE_PROPS_1, asid);
6486 gaudi_mmu_prepare_reg(hdev, mmTPC7_QM_GLBL_NON_SECURE_PROPS_2, asid);
6487 gaudi_mmu_prepare_reg(hdev, mmTPC7_QM_GLBL_NON_SECURE_PROPS_3, asid);
6488 gaudi_mmu_prepare_reg(hdev, mmTPC7_QM_GLBL_NON_SECURE_PROPS_4, asid);
6489 gaudi_mmu_prepare_reg(hdev, mmTPC7_CFG_ARUSER_LO, asid);
6490 gaudi_mmu_prepare_reg(hdev, mmTPC7_CFG_AWUSER_LO, asid);
6491
6492 gaudi_mmu_prepare_reg(hdev, mmMME0_QM_GLBL_NON_SECURE_PROPS_0, asid);
6493 gaudi_mmu_prepare_reg(hdev, mmMME0_QM_GLBL_NON_SECURE_PROPS_1, asid);
6494 gaudi_mmu_prepare_reg(hdev, mmMME0_QM_GLBL_NON_SECURE_PROPS_2, asid);
6495 gaudi_mmu_prepare_reg(hdev, mmMME0_QM_GLBL_NON_SECURE_PROPS_3, asid);
6496 gaudi_mmu_prepare_reg(hdev, mmMME0_QM_GLBL_NON_SECURE_PROPS_4, asid);
6497 gaudi_mmu_prepare_reg(hdev, mmMME2_QM_GLBL_NON_SECURE_PROPS_0, asid);
6498 gaudi_mmu_prepare_reg(hdev, mmMME2_QM_GLBL_NON_SECURE_PROPS_1, asid);
6499 gaudi_mmu_prepare_reg(hdev, mmMME2_QM_GLBL_NON_SECURE_PROPS_2, asid);
6500 gaudi_mmu_prepare_reg(hdev, mmMME2_QM_GLBL_NON_SECURE_PROPS_3, asid);
6501 gaudi_mmu_prepare_reg(hdev, mmMME2_QM_GLBL_NON_SECURE_PROPS_4, asid);
6502
6503 gaudi_mmu_prepare_reg(hdev, mmMME0_SBAB_ARUSER0, asid);
6504 gaudi_mmu_prepare_reg(hdev, mmMME0_SBAB_ARUSER1, asid);
6505 gaudi_mmu_prepare_reg(hdev, mmMME1_SBAB_ARUSER0, asid);
6506 gaudi_mmu_prepare_reg(hdev, mmMME1_SBAB_ARUSER1, asid);
6507 gaudi_mmu_prepare_reg(hdev, mmMME2_SBAB_ARUSER0, asid);
6508 gaudi_mmu_prepare_reg(hdev, mmMME2_SBAB_ARUSER1, asid);
6509 gaudi_mmu_prepare_reg(hdev, mmMME3_SBAB_ARUSER0, asid);
6510 gaudi_mmu_prepare_reg(hdev, mmMME3_SBAB_ARUSER1, asid);
6511 gaudi_mmu_prepare_reg(hdev, mmMME0_ACC_WBC, asid);
6512 gaudi_mmu_prepare_reg(hdev, mmMME1_ACC_WBC, asid);
6513 gaudi_mmu_prepare_reg(hdev, mmMME2_ACC_WBC, asid);
6514 gaudi_mmu_prepare_reg(hdev, mmMME3_ACC_WBC, asid);
6515
Oded Gabbay3c681572020-11-02 21:10:39 +02006516 if (hdev->nic_ports_mask & GAUDI_NIC_MASK_NIC0) {
6517 gaudi_mmu_prepare_reg(hdev, mmNIC0_QM0_GLBL_NON_SECURE_PROPS_0,
6518 asid);
6519 gaudi_mmu_prepare_reg(hdev, mmNIC0_QM0_GLBL_NON_SECURE_PROPS_1,
6520 asid);
6521 gaudi_mmu_prepare_reg(hdev, mmNIC0_QM0_GLBL_NON_SECURE_PROPS_2,
6522 asid);
6523 gaudi_mmu_prepare_reg(hdev, mmNIC0_QM0_GLBL_NON_SECURE_PROPS_3,
6524 asid);
6525 gaudi_mmu_prepare_reg(hdev, mmNIC0_QM0_GLBL_NON_SECURE_PROPS_4,
6526 asid);
6527 }
6528
6529 if (hdev->nic_ports_mask & GAUDI_NIC_MASK_NIC1) {
6530 gaudi_mmu_prepare_reg(hdev, mmNIC0_QM1_GLBL_NON_SECURE_PROPS_0,
6531 asid);
6532 gaudi_mmu_prepare_reg(hdev, mmNIC0_QM1_GLBL_NON_SECURE_PROPS_1,
6533 asid);
6534 gaudi_mmu_prepare_reg(hdev, mmNIC0_QM1_GLBL_NON_SECURE_PROPS_2,
6535 asid);
6536 gaudi_mmu_prepare_reg(hdev, mmNIC0_QM1_GLBL_NON_SECURE_PROPS_3,
6537 asid);
6538 gaudi_mmu_prepare_reg(hdev, mmNIC0_QM1_GLBL_NON_SECURE_PROPS_4,
6539 asid);
6540 }
6541
6542 if (hdev->nic_ports_mask & GAUDI_NIC_MASK_NIC2) {
6543 gaudi_mmu_prepare_reg(hdev, mmNIC1_QM0_GLBL_NON_SECURE_PROPS_0,
6544 asid);
6545 gaudi_mmu_prepare_reg(hdev, mmNIC1_QM0_GLBL_NON_SECURE_PROPS_1,
6546 asid);
6547 gaudi_mmu_prepare_reg(hdev, mmNIC1_QM0_GLBL_NON_SECURE_PROPS_2,
6548 asid);
6549 gaudi_mmu_prepare_reg(hdev, mmNIC1_QM0_GLBL_NON_SECURE_PROPS_3,
6550 asid);
6551 gaudi_mmu_prepare_reg(hdev, mmNIC1_QM0_GLBL_NON_SECURE_PROPS_4,
6552 asid);
6553 }
6554
6555 if (hdev->nic_ports_mask & GAUDI_NIC_MASK_NIC3) {
6556 gaudi_mmu_prepare_reg(hdev, mmNIC1_QM1_GLBL_NON_SECURE_PROPS_0,
6557 asid);
6558 gaudi_mmu_prepare_reg(hdev, mmNIC1_QM1_GLBL_NON_SECURE_PROPS_1,
6559 asid);
6560 gaudi_mmu_prepare_reg(hdev, mmNIC1_QM1_GLBL_NON_SECURE_PROPS_2,
6561 asid);
6562 gaudi_mmu_prepare_reg(hdev, mmNIC1_QM1_GLBL_NON_SECURE_PROPS_3,
6563 asid);
6564 gaudi_mmu_prepare_reg(hdev, mmNIC1_QM1_GLBL_NON_SECURE_PROPS_4,
6565 asid);
6566 }
6567
6568 if (hdev->nic_ports_mask & GAUDI_NIC_MASK_NIC4) {
6569 gaudi_mmu_prepare_reg(hdev, mmNIC2_QM0_GLBL_NON_SECURE_PROPS_0,
6570 asid);
6571 gaudi_mmu_prepare_reg(hdev, mmNIC2_QM0_GLBL_NON_SECURE_PROPS_1,
6572 asid);
6573 gaudi_mmu_prepare_reg(hdev, mmNIC2_QM0_GLBL_NON_SECURE_PROPS_2,
6574 asid);
6575 gaudi_mmu_prepare_reg(hdev, mmNIC2_QM0_GLBL_NON_SECURE_PROPS_3,
6576 asid);
6577 gaudi_mmu_prepare_reg(hdev, mmNIC2_QM0_GLBL_NON_SECURE_PROPS_4,
6578 asid);
6579 }
6580
6581 if (hdev->nic_ports_mask & GAUDI_NIC_MASK_NIC5) {
6582 gaudi_mmu_prepare_reg(hdev, mmNIC2_QM1_GLBL_NON_SECURE_PROPS_0,
6583 asid);
6584 gaudi_mmu_prepare_reg(hdev, mmNIC2_QM1_GLBL_NON_SECURE_PROPS_1,
6585 asid);
6586 gaudi_mmu_prepare_reg(hdev, mmNIC2_QM1_GLBL_NON_SECURE_PROPS_2,
6587 asid);
6588 gaudi_mmu_prepare_reg(hdev, mmNIC2_QM1_GLBL_NON_SECURE_PROPS_3,
6589 asid);
6590 gaudi_mmu_prepare_reg(hdev, mmNIC2_QM1_GLBL_NON_SECURE_PROPS_4,
6591 asid);
6592 }
6593
6594 if (hdev->nic_ports_mask & GAUDI_NIC_MASK_NIC6) {
6595 gaudi_mmu_prepare_reg(hdev, mmNIC3_QM0_GLBL_NON_SECURE_PROPS_0,
6596 asid);
6597 gaudi_mmu_prepare_reg(hdev, mmNIC3_QM0_GLBL_NON_SECURE_PROPS_1,
6598 asid);
6599 gaudi_mmu_prepare_reg(hdev, mmNIC3_QM0_GLBL_NON_SECURE_PROPS_2,
6600 asid);
6601 gaudi_mmu_prepare_reg(hdev, mmNIC3_QM0_GLBL_NON_SECURE_PROPS_3,
6602 asid);
6603 gaudi_mmu_prepare_reg(hdev, mmNIC3_QM0_GLBL_NON_SECURE_PROPS_4,
6604 asid);
6605 }
6606
6607 if (hdev->nic_ports_mask & GAUDI_NIC_MASK_NIC7) {
6608 gaudi_mmu_prepare_reg(hdev, mmNIC3_QM1_GLBL_NON_SECURE_PROPS_0,
6609 asid);
6610 gaudi_mmu_prepare_reg(hdev, mmNIC3_QM1_GLBL_NON_SECURE_PROPS_1,
6611 asid);
6612 gaudi_mmu_prepare_reg(hdev, mmNIC3_QM1_GLBL_NON_SECURE_PROPS_2,
6613 asid);
6614 gaudi_mmu_prepare_reg(hdev, mmNIC3_QM1_GLBL_NON_SECURE_PROPS_3,
6615 asid);
6616 gaudi_mmu_prepare_reg(hdev, mmNIC3_QM1_GLBL_NON_SECURE_PROPS_4,
6617 asid);
6618 }
6619
6620 if (hdev->nic_ports_mask & GAUDI_NIC_MASK_NIC8) {
6621 gaudi_mmu_prepare_reg(hdev, mmNIC4_QM0_GLBL_NON_SECURE_PROPS_0,
6622 asid);
6623 gaudi_mmu_prepare_reg(hdev, mmNIC4_QM0_GLBL_NON_SECURE_PROPS_1,
6624 asid);
6625 gaudi_mmu_prepare_reg(hdev, mmNIC4_QM0_GLBL_NON_SECURE_PROPS_2,
6626 asid);
6627 gaudi_mmu_prepare_reg(hdev, mmNIC4_QM0_GLBL_NON_SECURE_PROPS_3,
6628 asid);
6629 gaudi_mmu_prepare_reg(hdev, mmNIC4_QM0_GLBL_NON_SECURE_PROPS_4,
6630 asid);
6631 }
6632
6633 if (hdev->nic_ports_mask & GAUDI_NIC_MASK_NIC9) {
6634 gaudi_mmu_prepare_reg(hdev, mmNIC4_QM1_GLBL_NON_SECURE_PROPS_0,
6635 asid);
6636 gaudi_mmu_prepare_reg(hdev, mmNIC4_QM1_GLBL_NON_SECURE_PROPS_1,
6637 asid);
6638 gaudi_mmu_prepare_reg(hdev, mmNIC4_QM1_GLBL_NON_SECURE_PROPS_2,
6639 asid);
6640 gaudi_mmu_prepare_reg(hdev, mmNIC4_QM1_GLBL_NON_SECURE_PROPS_3,
6641 asid);
6642 gaudi_mmu_prepare_reg(hdev, mmNIC4_QM1_GLBL_NON_SECURE_PROPS_4,
6643 asid);
6644 }
6645
Oded Gabbaye38bfd32020-07-03 20:46:12 +03006646 hdev->asic_funcs->set_clock_gating(hdev);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03006647
6648 mutex_unlock(&gaudi->clk_gate_mutex);
6649}
6650
6651static int gaudi_send_job_on_qman0(struct hl_device *hdev,
6652 struct hl_cs_job *job)
6653{
6654 struct packet_msg_prot *fence_pkt;
6655 u32 *fence_ptr;
6656 dma_addr_t fence_dma_addr;
6657 struct hl_cb *cb;
6658 u32 tmp, timeout, dma_offset;
6659 int rc;
6660
6661 if (hdev->pldm)
6662 timeout = GAUDI_PLDM_QMAN0_TIMEOUT_USEC;
6663 else
6664 timeout = HL_DEVICE_TIMEOUT_USEC;
6665
Ohad Sharabicf303392021-01-17 16:01:56 +02006666 if (!hdev->asic_funcs->is_device_idle(hdev, NULL, 0, NULL)) {
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03006667 dev_err_ratelimited(hdev->dev,
6668 "Can't send driver job on QMAN0 because the device is not idle\n");
6669 return -EBUSY;
6670 }
6671
6672 fence_ptr = hdev->asic_funcs->asic_dma_pool_zalloc(hdev, 4, GFP_KERNEL,
6673 &fence_dma_addr);
6674 if (!fence_ptr) {
6675 dev_err(hdev->dev,
6676 "Failed to allocate fence memory for QMAN0\n");
6677 return -ENOMEM;
6678 }
6679
6680 cb = job->patched_cb;
6681
Arnd Bergmann82948e62020-10-26 17:08:06 +01006682 fence_pkt = cb->kernel_address +
6683 job->job_cb_size - sizeof(struct packet_msg_prot);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03006684
Oded Gabbay65887292020-08-12 11:21:01 +03006685 tmp = FIELD_PREP(GAUDI_PKT_CTL_OPCODE_MASK, PACKET_MSG_PROT);
6686 tmp |= FIELD_PREP(GAUDI_PKT_CTL_EB_MASK, 1);
6687 tmp |= FIELD_PREP(GAUDI_PKT_CTL_MB_MASK, 1);
6688
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03006689 fence_pkt->ctl = cpu_to_le32(tmp);
6690 fence_pkt->value = cpu_to_le32(GAUDI_QMAN0_FENCE_VAL);
6691 fence_pkt->addr = cpu_to_le64(fence_dma_addr);
6692
6693 dma_offset = gaudi_dma_assignment[GAUDI_PCI_DMA_1] * DMA_CORE_OFFSET;
6694
6695 WREG32_OR(mmDMA0_CORE_PROT + dma_offset, BIT(DMA0_CORE_PROT_VAL_SHIFT));
6696
6697 rc = hl_hw_queue_send_cb_no_cmpl(hdev, GAUDI_QUEUE_ID_DMA_0_0,
6698 job->job_cb_size, cb->bus_address);
6699 if (rc) {
6700 dev_err(hdev->dev, "Failed to send CB on QMAN0, %d\n", rc);
6701 goto free_fence_ptr;
6702 }
6703
6704 rc = hl_poll_timeout_memory(hdev, fence_ptr, tmp,
6705 (tmp == GAUDI_QMAN0_FENCE_VAL), 1000,
6706 timeout, true);
6707
6708 hl_hw_queue_inc_ci_kernel(hdev, GAUDI_QUEUE_ID_DMA_0_0);
6709
6710 if (rc == -ETIMEDOUT) {
6711 dev_err(hdev->dev, "QMAN0 Job timeout (0x%x)\n", tmp);
6712 goto free_fence_ptr;
6713 }
6714
6715free_fence_ptr:
6716 WREG32_AND(mmDMA0_CORE_PROT + dma_offset,
6717 ~BIT(DMA0_CORE_PROT_VAL_SHIFT));
6718
6719 hdev->asic_funcs->asic_dma_pool_free(hdev, (void *) fence_ptr,
6720 fence_dma_addr);
6721 return rc;
6722}
6723
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03006724static void gaudi_get_event_desc(u16 event_type, char *desc, size_t size)
6725{
Ofir Bittonebd8d122020-05-10 13:41:28 +03006726 if (event_type >= GAUDI_EVENT_SIZE)
6727 goto event_not_supported;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03006728
Ofir Bittonebd8d122020-05-10 13:41:28 +03006729 if (!gaudi_irq_map_table[event_type].valid)
6730 goto event_not_supported;
6731
6732 snprintf(desc, size, gaudi_irq_map_table[event_type].name);
6733
6734 return;
6735
6736event_not_supported:
6737 snprintf(desc, size, "N/A");
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03006738}
6739
6740static const char *gaudi_get_razwi_initiator_dma_name(struct hl_device *hdev,
6741 u32 x_y, bool is_write)
6742{
6743 u32 dma_id[2], dma_offset, err_cause[2], mask, i;
6744
6745 mask = is_write ? DMA0_CORE_ERR_CAUSE_HBW_WR_ERR_MASK :
6746 DMA0_CORE_ERR_CAUSE_HBW_RD_ERR_MASK;
6747
6748 switch (x_y) {
6749 case RAZWI_INITIATOR_ID_X_Y_DMA_IF_W_S_0:
6750 case RAZWI_INITIATOR_ID_X_Y_DMA_IF_W_S_1:
6751 dma_id[0] = 0;
6752 dma_id[1] = 2;
6753 break;
6754 case RAZWI_INITIATOR_ID_X_Y_DMA_IF_E_S_0:
6755 case RAZWI_INITIATOR_ID_X_Y_DMA_IF_E_S_1:
6756 dma_id[0] = 1;
6757 dma_id[1] = 3;
6758 break;
6759 case RAZWI_INITIATOR_ID_X_Y_DMA_IF_W_N_0:
6760 case RAZWI_INITIATOR_ID_X_Y_DMA_IF_W_N_1:
6761 dma_id[0] = 4;
6762 dma_id[1] = 6;
6763 break;
6764 case RAZWI_INITIATOR_ID_X_Y_DMA_IF_E_N_0:
6765 case RAZWI_INITIATOR_ID_X_Y_DMA_IF_E_N_1:
6766 dma_id[0] = 5;
6767 dma_id[1] = 7;
6768 break;
6769 default:
6770 goto unknown_initiator;
6771 }
6772
6773 for (i = 0 ; i < 2 ; i++) {
6774 dma_offset = dma_id[i] * DMA_CORE_OFFSET;
6775 err_cause[i] = RREG32(mmDMA0_CORE_ERR_CAUSE + dma_offset);
6776 }
6777
6778 switch (x_y) {
6779 case RAZWI_INITIATOR_ID_X_Y_DMA_IF_W_S_0:
6780 case RAZWI_INITIATOR_ID_X_Y_DMA_IF_W_S_1:
6781 if ((err_cause[0] & mask) && !(err_cause[1] & mask))
6782 return "DMA0";
6783 else if (!(err_cause[0] & mask) && (err_cause[1] & mask))
6784 return "DMA2";
6785 else
6786 return "DMA0 or DMA2";
6787 case RAZWI_INITIATOR_ID_X_Y_DMA_IF_E_S_0:
6788 case RAZWI_INITIATOR_ID_X_Y_DMA_IF_E_S_1:
6789 if ((err_cause[0] & mask) && !(err_cause[1] & mask))
6790 return "DMA1";
6791 else if (!(err_cause[0] & mask) && (err_cause[1] & mask))
6792 return "DMA3";
6793 else
6794 return "DMA1 or DMA3";
6795 case RAZWI_INITIATOR_ID_X_Y_DMA_IF_W_N_0:
6796 case RAZWI_INITIATOR_ID_X_Y_DMA_IF_W_N_1:
6797 if ((err_cause[0] & mask) && !(err_cause[1] & mask))
6798 return "DMA4";
6799 else if (!(err_cause[0] & mask) && (err_cause[1] & mask))
6800 return "DMA6";
6801 else
6802 return "DMA4 or DMA6";
6803 case RAZWI_INITIATOR_ID_X_Y_DMA_IF_E_N_0:
6804 case RAZWI_INITIATOR_ID_X_Y_DMA_IF_E_N_1:
6805 if ((err_cause[0] & mask) && !(err_cause[1] & mask))
6806 return "DMA5";
6807 else if (!(err_cause[0] & mask) && (err_cause[1] & mask))
6808 return "DMA7";
6809 else
6810 return "DMA5 or DMA7";
6811 }
6812
6813unknown_initiator:
6814 return "unknown initiator";
6815}
6816
6817static const char *gaudi_get_razwi_initiator_name(struct hl_device *hdev,
6818 bool is_write)
6819{
6820 u32 val, x_y, axi_id;
6821
6822 val = is_write ? RREG32(mmMMU_UP_RAZWI_WRITE_ID) :
6823 RREG32(mmMMU_UP_RAZWI_READ_ID);
6824 x_y = val & ((RAZWI_INITIATOR_Y_MASK << RAZWI_INITIATOR_Y_SHIFT) |
6825 (RAZWI_INITIATOR_X_MASK << RAZWI_INITIATOR_X_SHIFT));
6826 axi_id = val & (RAZWI_INITIATOR_AXI_ID_MASK <<
6827 RAZWI_INITIATOR_AXI_ID_SHIFT);
6828
6829 switch (x_y) {
6830 case RAZWI_INITIATOR_ID_X_Y_TPC0_NIC0:
6831 if (axi_id == RAZWI_INITIATOR_ID_AXI_ID(AXI_ID_TPC))
6832 return "TPC0";
6833 if (axi_id == RAZWI_INITIATOR_ID_AXI_ID(AXI_ID_NIC))
6834 return "NIC0";
6835 break;
6836 case RAZWI_INITIATOR_ID_X_Y_TPC1:
6837 return "TPC1";
6838 case RAZWI_INITIATOR_ID_X_Y_MME0_0:
6839 case RAZWI_INITIATOR_ID_X_Y_MME0_1:
6840 return "MME0";
6841 case RAZWI_INITIATOR_ID_X_Y_MME1_0:
6842 case RAZWI_INITIATOR_ID_X_Y_MME1_1:
6843 return "MME1";
6844 case RAZWI_INITIATOR_ID_X_Y_TPC2:
6845 return "TPC2";
6846 case RAZWI_INITIATOR_ID_X_Y_TPC3_PCI_CPU_PSOC:
6847 if (axi_id == RAZWI_INITIATOR_ID_AXI_ID(AXI_ID_TPC))
6848 return "TPC3";
6849 if (axi_id == RAZWI_INITIATOR_ID_AXI_ID(AXI_ID_PCI))
6850 return "PCI";
6851 if (axi_id == RAZWI_INITIATOR_ID_AXI_ID(AXI_ID_CPU))
6852 return "CPU";
6853 if (axi_id == RAZWI_INITIATOR_ID_AXI_ID(AXI_ID_PSOC))
6854 return "PSOC";
6855 break;
6856 case RAZWI_INITIATOR_ID_X_Y_DMA_IF_W_S_0:
6857 case RAZWI_INITIATOR_ID_X_Y_DMA_IF_W_S_1:
6858 case RAZWI_INITIATOR_ID_X_Y_DMA_IF_E_S_0:
6859 case RAZWI_INITIATOR_ID_X_Y_DMA_IF_E_S_1:
6860 case RAZWI_INITIATOR_ID_X_Y_DMA_IF_W_N_0:
6861 case RAZWI_INITIATOR_ID_X_Y_DMA_IF_W_N_1:
6862 case RAZWI_INITIATOR_ID_X_Y_DMA_IF_E_N_0:
6863 case RAZWI_INITIATOR_ID_X_Y_DMA_IF_E_N_1:
6864 return gaudi_get_razwi_initiator_dma_name(hdev, x_y, is_write);
6865 case RAZWI_INITIATOR_ID_X_Y_TPC4_NIC1_NIC2:
6866 if (axi_id == RAZWI_INITIATOR_ID_AXI_ID(AXI_ID_TPC))
6867 return "TPC4";
6868 if (axi_id == RAZWI_INITIATOR_ID_AXI_ID(AXI_ID_NIC))
6869 return "NIC1";
6870 if (axi_id == RAZWI_INITIATOR_ID_AXI_ID(AXI_ID_NIC_FT))
6871 return "NIC2";
6872 break;
6873 case RAZWI_INITIATOR_ID_X_Y_TPC5:
6874 return "TPC5";
6875 case RAZWI_INITIATOR_ID_X_Y_MME2_0:
6876 case RAZWI_INITIATOR_ID_X_Y_MME2_1:
6877 return "MME2";
6878 case RAZWI_INITIATOR_ID_X_Y_MME3_0:
6879 case RAZWI_INITIATOR_ID_X_Y_MME3_1:
6880 return "MME3";
6881 case RAZWI_INITIATOR_ID_X_Y_TPC6:
6882 return "TPC6";
6883 case RAZWI_INITIATOR_ID_X_Y_TPC7_NIC4_NIC5:
6884 if (axi_id == RAZWI_INITIATOR_ID_AXI_ID(AXI_ID_TPC))
6885 return "TPC7";
6886 if (axi_id == RAZWI_INITIATOR_ID_AXI_ID(AXI_ID_NIC))
6887 return "NIC4";
6888 if (axi_id == RAZWI_INITIATOR_ID_AXI_ID(AXI_ID_NIC_FT))
6889 return "NIC5";
6890 break;
6891 default:
6892 break;
6893 }
6894
6895 dev_err(hdev->dev,
6896 "Unknown RAZWI initiator ID 0x%x [Y=%d, X=%d, AXI_ID=%d]\n",
6897 val,
6898 (val >> RAZWI_INITIATOR_Y_SHIFT) & RAZWI_INITIATOR_Y_MASK,
6899 (val >> RAZWI_INITIATOR_X_SHIFT) & RAZWI_INITIATOR_X_MASK,
6900 (val >> RAZWI_INITIATOR_AXI_ID_SHIFT) &
6901 RAZWI_INITIATOR_AXI_ID_MASK);
6902
6903 return "unknown initiator";
6904}
6905
6906static void gaudi_print_razwi_info(struct hl_device *hdev)
6907{
6908 if (RREG32(mmMMU_UP_RAZWI_WRITE_VLD)) {
6909 dev_err_ratelimited(hdev->dev,
6910 "RAZWI event caused by illegal write of %s\n",
6911 gaudi_get_razwi_initiator_name(hdev, true));
6912 WREG32(mmMMU_UP_RAZWI_WRITE_VLD, 0);
6913 }
6914
6915 if (RREG32(mmMMU_UP_RAZWI_READ_VLD)) {
6916 dev_err_ratelimited(hdev->dev,
6917 "RAZWI event caused by illegal read of %s\n",
6918 gaudi_get_razwi_initiator_name(hdev, false));
6919 WREG32(mmMMU_UP_RAZWI_READ_VLD, 0);
6920 }
6921}
6922
6923static void gaudi_print_mmu_error_info(struct hl_device *hdev)
6924{
6925 struct gaudi_device *gaudi = hdev->asic_specific;
6926 u64 addr;
6927 u32 val;
6928
6929 if (!(gaudi->hw_cap_initialized & HW_CAP_MMU))
6930 return;
6931
6932 val = RREG32(mmMMU_UP_PAGE_ERROR_CAPTURE);
6933 if (val & MMU_UP_PAGE_ERROR_CAPTURE_ENTRY_VALID_MASK) {
6934 addr = val & MMU_UP_PAGE_ERROR_CAPTURE_VA_49_32_MASK;
6935 addr <<= 32;
6936 addr |= RREG32(mmMMU_UP_PAGE_ERROR_CAPTURE_VA);
6937
6938 dev_err_ratelimited(hdev->dev, "MMU page fault on va 0x%llx\n",
6939 addr);
6940
6941 WREG32(mmMMU_UP_PAGE_ERROR_CAPTURE, 0);
6942 }
6943
6944 val = RREG32(mmMMU_UP_ACCESS_ERROR_CAPTURE);
6945 if (val & MMU_UP_ACCESS_ERROR_CAPTURE_ENTRY_VALID_MASK) {
6946 addr = val & MMU_UP_ACCESS_ERROR_CAPTURE_VA_49_32_MASK;
6947 addr <<= 32;
6948 addr |= RREG32(mmMMU_UP_ACCESS_ERROR_CAPTURE_VA);
6949
6950 dev_err_ratelimited(hdev->dev,
6951 "MMU access error on va 0x%llx\n", addr);
6952
6953 WREG32(mmMMU_UP_ACCESS_ERROR_CAPTURE, 0);
6954 }
6955}
6956
6957/*
6958 * +-------------------+------------------------------------------------------+
6959 * | Configuration Reg | Description |
6960 * | Address | |
6961 * +-------------------+------------------------------------------------------+
6962 * | 0xF30 - 0xF3F |ECC single error indication (1 bit per memory wrapper)|
6963 * | |0xF30 memory wrappers 31:0 (MSB to LSB) |
6964 * | |0xF34 memory wrappers 63:32 |
6965 * | |0xF38 memory wrappers 95:64 |
6966 * | |0xF3C memory wrappers 127:96 |
6967 * +-------------------+------------------------------------------------------+
6968 * | 0xF40 - 0xF4F |ECC double error indication (1 bit per memory wrapper)|
6969 * | |0xF40 memory wrappers 31:0 (MSB to LSB) |
6970 * | |0xF44 memory wrappers 63:32 |
6971 * | |0xF48 memory wrappers 95:64 |
6972 * | |0xF4C memory wrappers 127:96 |
6973 * +-------------------+------------------------------------------------------+
6974 */
Oded Gabbayfcc6a4e2020-05-17 08:20:35 +03006975static int gaudi_extract_ecc_info(struct hl_device *hdev,
6976 struct ecc_info_extract_params *params, u64 *ecc_address,
6977 u64 *ecc_syndrom, u8 *memory_wrapper_idx)
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03006978{
6979 struct gaudi_device *gaudi = hdev->asic_specific;
Oded Gabbayfcc6a4e2020-05-17 08:20:35 +03006980 u32 i, num_mem_regs, reg, err_bit;
6981 u64 err_addr, err_word = 0;
6982 int rc = 0;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03006983
Oded Gabbayfcc6a4e2020-05-17 08:20:35 +03006984 num_mem_regs = params->num_memories / 32 +
6985 ((params->num_memories % 32) ? 1 : 0);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03006986
Oded Gabbayfcc6a4e2020-05-17 08:20:35 +03006987 if (params->block_address >= CFG_BASE)
6988 params->block_address -= CFG_BASE;
6989
6990 if (params->derr)
6991 err_addr = params->block_address + GAUDI_ECC_DERR0_OFFSET;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03006992 else
Oded Gabbayfcc6a4e2020-05-17 08:20:35 +03006993 err_addr = params->block_address + GAUDI_ECC_SERR0_OFFSET;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03006994
Oded Gabbayfcc6a4e2020-05-17 08:20:35 +03006995 if (params->disable_clock_gating) {
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03006996 mutex_lock(&gaudi->clk_gate_mutex);
6997 hdev->asic_funcs->disable_clock_gating(hdev);
6998 }
6999
Oded Gabbayfcc6a4e2020-05-17 08:20:35 +03007000 /* Set invalid wrapper index */
7001 *memory_wrapper_idx = 0xFF;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007002
Oded Gabbayfcc6a4e2020-05-17 08:20:35 +03007003 /* Iterate through memory wrappers, a single bit must be set */
Dan Carpenterb0353542020-08-05 12:51:05 +03007004 for (i = 0 ; i < num_mem_regs ; i++) {
Oded Gabbayfcc6a4e2020-05-17 08:20:35 +03007005 err_addr += i * 4;
7006 err_word = RREG32(err_addr);
7007 if (err_word) {
7008 err_bit = __ffs(err_word);
7009 *memory_wrapper_idx = err_bit + (32 * i);
7010 break;
7011 }
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007012 }
7013
Oded Gabbayfcc6a4e2020-05-17 08:20:35 +03007014 if (*memory_wrapper_idx == 0xFF) {
7015 dev_err(hdev->dev, "ECC error information cannot be found\n");
7016 rc = -EINVAL;
7017 goto enable_clk_gate;
7018 }
7019
7020 WREG32(params->block_address + GAUDI_ECC_MEM_SEL_OFFSET,
7021 *memory_wrapper_idx);
7022
7023 *ecc_address =
7024 RREG32(params->block_address + GAUDI_ECC_ADDRESS_OFFSET);
7025 *ecc_syndrom =
7026 RREG32(params->block_address + GAUDI_ECC_SYNDROME_OFFSET);
7027
7028 /* Clear error indication */
7029 reg = RREG32(params->block_address + GAUDI_ECC_MEM_INFO_CLR_OFFSET);
7030 if (params->derr)
7031 reg |= FIELD_PREP(GAUDI_ECC_MEM_INFO_CLR_DERR_MASK, 1);
7032 else
7033 reg |= FIELD_PREP(GAUDI_ECC_MEM_INFO_CLR_SERR_MASK, 1);
7034
7035 WREG32(params->block_address + GAUDI_ECC_MEM_INFO_CLR_OFFSET, reg);
7036
7037enable_clk_gate:
7038 if (params->disable_clock_gating) {
Oded Gabbaye38bfd32020-07-03 20:46:12 +03007039 hdev->asic_funcs->set_clock_gating(hdev);
Greg Kroah-Hartman65a9bde62020-07-27 11:49:37 +02007040
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007041 mutex_unlock(&gaudi->clk_gate_mutex);
7042 }
Oded Gabbayfcc6a4e2020-05-17 08:20:35 +03007043
7044 return rc;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007045}
7046
7047static void gaudi_handle_qman_err_generic(struct hl_device *hdev,
7048 const char *qm_name,
7049 u64 glbl_sts_addr,
7050 u64 arb_err_addr)
7051{
7052 u32 i, j, glbl_sts_val, arb_err_val, glbl_sts_clr_val;
7053 char reg_desc[32];
7054
7055 /* Iterate through all stream GLBL_STS1 registers + Lower CP */
7056 for (i = 0 ; i < QMAN_STREAMS + 1 ; i++) {
7057 glbl_sts_clr_val = 0;
7058 glbl_sts_val = RREG32(glbl_sts_addr + 4 * i);
7059
7060 if (!glbl_sts_val)
7061 continue;
7062
7063 if (i == QMAN_STREAMS)
7064 snprintf(reg_desc, ARRAY_SIZE(reg_desc), "LowerCP");
7065 else
7066 snprintf(reg_desc, ARRAY_SIZE(reg_desc), "stream%u", i);
7067
7068 for (j = 0 ; j < GAUDI_NUM_OF_QM_ERR_CAUSE ; j++) {
7069 if (glbl_sts_val & BIT(j)) {
7070 dev_err_ratelimited(hdev->dev,
7071 "%s %s. err cause: %s\n",
7072 qm_name, reg_desc,
7073 gaudi_qman_error_cause[j]);
7074 glbl_sts_clr_val |= BIT(j);
7075 }
7076 }
7077
7078 /* Write 1 clear errors */
Tomer Tayar1b497152021-04-06 13:32:20 +03007079 if (!hdev->stop_on_err)
7080 WREG32(glbl_sts_addr + 4 * i, glbl_sts_clr_val);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007081 }
7082
7083 arb_err_val = RREG32(arb_err_addr);
7084
7085 if (!arb_err_val)
7086 return;
7087
7088 for (j = 0 ; j < GAUDI_NUM_OF_QM_ARB_ERR_CAUSE ; j++) {
7089 if (arb_err_val & BIT(j)) {
7090 dev_err_ratelimited(hdev->dev,
7091 "%s ARB_ERR. err cause: %s\n",
7092 qm_name,
7093 gaudi_qman_arb_error_cause[j]);
7094 }
7095 }
7096}
7097
Ofir Bittonf8bc7f02021-01-03 20:52:40 +02007098static void gaudi_print_sm_sei_info(struct hl_device *hdev, u16 event_type,
7099 struct hl_eq_sm_sei_data *sei_data)
7100{
7101 u32 index = event_type - GAUDI_EVENT_DMA_IF_SEI_0;
7102
7103 switch (sei_data->sei_cause) {
Oded Gabbay78385042021-01-26 22:56:56 +02007104 case SM_SEI_SO_OVERFLOW:
Ofir Bittonf8bc7f02021-01-03 20:52:40 +02007105 dev_err(hdev->dev,
7106 "SM %u SEI Error: SO %u overflow/underflow",
Oded Gabbay78385042021-01-26 22:56:56 +02007107 index, le32_to_cpu(sei_data->sei_log));
Ofir Bittonf8bc7f02021-01-03 20:52:40 +02007108 break;
Oded Gabbay78385042021-01-26 22:56:56 +02007109 case SM_SEI_LBW_4B_UNALIGNED:
Ofir Bittonf8bc7f02021-01-03 20:52:40 +02007110 dev_err(hdev->dev,
7111 "SM %u SEI Error: Unaligned 4B LBW access, monitor agent address low - %#x",
Oded Gabbay78385042021-01-26 22:56:56 +02007112 index, le32_to_cpu(sei_data->sei_log));
Ofir Bittonf8bc7f02021-01-03 20:52:40 +02007113 break;
Oded Gabbay78385042021-01-26 22:56:56 +02007114 case SM_SEI_AXI_RESPONSE_ERR:
Ofir Bittonf8bc7f02021-01-03 20:52:40 +02007115 dev_err(hdev->dev,
7116 "SM %u SEI Error: AXI ID %u response error",
Oded Gabbay78385042021-01-26 22:56:56 +02007117 index, le32_to_cpu(sei_data->sei_log));
Ofir Bittonf8bc7f02021-01-03 20:52:40 +02007118 break;
7119 default:
7120 dev_err(hdev->dev, "Unknown SM SEI cause %u",
Oded Gabbay78385042021-01-26 22:56:56 +02007121 le32_to_cpu(sei_data->sei_log));
Ofir Bittonf8bc7f02021-01-03 20:52:40 +02007122 break;
7123 }
7124}
7125
Oded Gabbayfcc6a4e2020-05-17 08:20:35 +03007126static void gaudi_handle_ecc_event(struct hl_device *hdev, u16 event_type,
7127 struct hl_eq_ecc_data *ecc_data)
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007128{
Oded Gabbayfcc6a4e2020-05-17 08:20:35 +03007129 struct ecc_info_extract_params params;
7130 u64 ecc_address = 0, ecc_syndrom = 0;
7131 u8 index, memory_wrapper_idx = 0;
7132 bool extract_info_from_fw;
7133 int rc;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007134
7135 switch (event_type) {
Oded Gabbayfcc6a4e2020-05-17 08:20:35 +03007136 case GAUDI_EVENT_PCIE_CORE_SERR ... GAUDI_EVENT_PCIE_PHY_DERR:
7137 case GAUDI_EVENT_DMA0_SERR_ECC ... GAUDI_EVENT_MMU_DERR:
7138 extract_info_from_fw = true;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007139 break;
7140 case GAUDI_EVENT_TPC0_SERR ... GAUDI_EVENT_TPC7_SERR:
7141 index = event_type - GAUDI_EVENT_TPC0_SERR;
Oded Gabbayfcc6a4e2020-05-17 08:20:35 +03007142 params.block_address = mmTPC0_CFG_BASE + index * TPC_CFG_OFFSET;
7143 params.num_memories = 90;
7144 params.derr = false;
7145 params.disable_clock_gating = true;
7146 extract_info_from_fw = false;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007147 break;
7148 case GAUDI_EVENT_TPC0_DERR ... GAUDI_EVENT_TPC7_DERR:
7149 index = event_type - GAUDI_EVENT_TPC0_DERR;
Oded Gabbayfcc6a4e2020-05-17 08:20:35 +03007150 params.block_address =
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007151 mmTPC0_CFG_BASE + index * TPC_CFG_OFFSET;
Oded Gabbayfcc6a4e2020-05-17 08:20:35 +03007152 params.num_memories = 90;
7153 params.derr = true;
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_MME0_ACC_SERR:
7158 case GAUDI_EVENT_MME1_ACC_SERR:
7159 case GAUDI_EVENT_MME2_ACC_SERR:
7160 case GAUDI_EVENT_MME3_ACC_SERR:
7161 index = (event_type - GAUDI_EVENT_MME0_ACC_SERR) / 4;
Oded Gabbayfcc6a4e2020-05-17 08:20:35 +03007162 params.block_address = mmMME0_ACC_BASE + index * MME_ACC_OFFSET;
7163 params.num_memories = 128;
7164 params.derr = false;
7165 params.disable_clock_gating = true;
7166 extract_info_from_fw = false;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007167 break;
7168 case GAUDI_EVENT_MME0_ACC_DERR:
7169 case GAUDI_EVENT_MME1_ACC_DERR:
7170 case GAUDI_EVENT_MME2_ACC_DERR:
7171 case GAUDI_EVENT_MME3_ACC_DERR:
7172 index = (event_type - GAUDI_EVENT_MME0_ACC_DERR) / 4;
Oded Gabbayfcc6a4e2020-05-17 08:20:35 +03007173 params.block_address = mmMME0_ACC_BASE + index * MME_ACC_OFFSET;
7174 params.num_memories = 128;
7175 params.derr = true;
7176 params.disable_clock_gating = true;
7177 extract_info_from_fw = false;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007178 break;
7179 case GAUDI_EVENT_MME0_SBAB_SERR:
7180 case GAUDI_EVENT_MME1_SBAB_SERR:
7181 case GAUDI_EVENT_MME2_SBAB_SERR:
7182 case GAUDI_EVENT_MME3_SBAB_SERR:
7183 index = (event_type - GAUDI_EVENT_MME0_SBAB_SERR) / 4;
Oded Gabbayfcc6a4e2020-05-17 08:20:35 +03007184 params.block_address =
7185 mmMME0_SBAB_BASE + index * MME_ACC_OFFSET;
7186 params.num_memories = 33;
7187 params.derr = false;
7188 params.disable_clock_gating = true;
7189 extract_info_from_fw = false;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007190 break;
7191 case GAUDI_EVENT_MME0_SBAB_DERR:
7192 case GAUDI_EVENT_MME1_SBAB_DERR:
7193 case GAUDI_EVENT_MME2_SBAB_DERR:
7194 case GAUDI_EVENT_MME3_SBAB_DERR:
7195 index = (event_type - GAUDI_EVENT_MME0_SBAB_DERR) / 4;
Oded Gabbayfcc6a4e2020-05-17 08:20:35 +03007196 params.block_address =
7197 mmMME0_SBAB_BASE + index * MME_ACC_OFFSET;
7198 params.num_memories = 33;
7199 params.derr = true;
7200 params.disable_clock_gating = true;
Oded Gabbay652b4442020-11-21 14:35:35 +02007201 extract_info_from_fw = false;
7202 break;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007203 default:
7204 return;
7205 }
7206
Oded Gabbayfcc6a4e2020-05-17 08:20:35 +03007207 if (extract_info_from_fw) {
7208 ecc_address = le64_to_cpu(ecc_data->ecc_address);
7209 ecc_syndrom = le64_to_cpu(ecc_data->ecc_syndrom);
7210 memory_wrapper_idx = ecc_data->memory_wrapper_idx;
7211 } else {
7212 rc = gaudi_extract_ecc_info(hdev, &params, &ecc_address,
7213 &ecc_syndrom, &memory_wrapper_idx);
7214 if (rc)
7215 return;
7216 }
7217
7218 dev_err(hdev->dev,
7219 "ECC error detected. address: %#llx. Syndrom: %#llx. block id %u\n",
7220 ecc_address, ecc_syndrom, memory_wrapper_idx);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007221}
7222
7223static void gaudi_handle_qman_err(struct hl_device *hdev, u16 event_type)
7224{
7225 u64 glbl_sts_addr, arb_err_addr;
7226 u8 index;
7227 char desc[32];
7228
7229 switch (event_type) {
7230 case GAUDI_EVENT_TPC0_QM ... GAUDI_EVENT_TPC7_QM:
7231 index = event_type - GAUDI_EVENT_TPC0_QM;
7232 glbl_sts_addr =
7233 mmTPC0_QM_GLBL_STS1_0 + index * TPC_QMAN_OFFSET;
7234 arb_err_addr =
7235 mmTPC0_QM_ARB_ERR_CAUSE + index * TPC_QMAN_OFFSET;
7236 snprintf(desc, ARRAY_SIZE(desc), "%s%d", "TPC_QM", index);
7237 break;
7238 case GAUDI_EVENT_MME0_QM ... GAUDI_EVENT_MME2_QM:
7239 index = event_type - GAUDI_EVENT_MME0_QM;
7240 glbl_sts_addr =
7241 mmMME0_QM_GLBL_STS1_0 + index * MME_QMAN_OFFSET;
7242 arb_err_addr =
7243 mmMME0_QM_ARB_ERR_CAUSE + index * MME_QMAN_OFFSET;
7244 snprintf(desc, ARRAY_SIZE(desc), "%s%d", "MME_QM", index);
7245 break;
7246 case GAUDI_EVENT_DMA0_QM ... GAUDI_EVENT_DMA7_QM:
7247 index = event_type - GAUDI_EVENT_DMA0_QM;
7248 glbl_sts_addr =
7249 mmDMA0_QM_GLBL_STS1_0 + index * DMA_QMAN_OFFSET;
7250 arb_err_addr =
7251 mmDMA0_QM_ARB_ERR_CAUSE + index * DMA_QMAN_OFFSET;
7252 snprintf(desc, ARRAY_SIZE(desc), "%s%d", "DMA_QM", index);
7253 break;
Oded Gabbay3c681572020-11-02 21:10:39 +02007254 case GAUDI_EVENT_NIC0_QM0:
7255 glbl_sts_addr = mmNIC0_QM0_GLBL_STS1_0;
7256 arb_err_addr = mmNIC0_QM0_ARB_ERR_CAUSE;
7257 snprintf(desc, ARRAY_SIZE(desc), "NIC0_QM0");
7258 break;
7259 case GAUDI_EVENT_NIC0_QM1:
7260 glbl_sts_addr = mmNIC0_QM1_GLBL_STS1_0;
7261 arb_err_addr = mmNIC0_QM1_ARB_ERR_CAUSE;
7262 snprintf(desc, ARRAY_SIZE(desc), "NIC0_QM1");
7263 break;
7264 case GAUDI_EVENT_NIC1_QM0:
7265 glbl_sts_addr = mmNIC1_QM0_GLBL_STS1_0;
7266 arb_err_addr = mmNIC1_QM0_ARB_ERR_CAUSE;
7267 snprintf(desc, ARRAY_SIZE(desc), "NIC1_QM0");
7268 break;
7269 case GAUDI_EVENT_NIC1_QM1:
7270 glbl_sts_addr = mmNIC1_QM1_GLBL_STS1_0;
7271 arb_err_addr = mmNIC1_QM1_ARB_ERR_CAUSE;
7272 snprintf(desc, ARRAY_SIZE(desc), "NIC1_QM1");
7273 break;
7274 case GAUDI_EVENT_NIC2_QM0:
7275 glbl_sts_addr = mmNIC2_QM0_GLBL_STS1_0;
7276 arb_err_addr = mmNIC2_QM0_ARB_ERR_CAUSE;
7277 snprintf(desc, ARRAY_SIZE(desc), "NIC2_QM0");
7278 break;
7279 case GAUDI_EVENT_NIC2_QM1:
7280 glbl_sts_addr = mmNIC2_QM1_GLBL_STS1_0;
7281 arb_err_addr = mmNIC2_QM1_ARB_ERR_CAUSE;
7282 snprintf(desc, ARRAY_SIZE(desc), "NIC2_QM1");
7283 break;
7284 case GAUDI_EVENT_NIC3_QM0:
7285 glbl_sts_addr = mmNIC3_QM0_GLBL_STS1_0;
7286 arb_err_addr = mmNIC3_QM0_ARB_ERR_CAUSE;
7287 snprintf(desc, ARRAY_SIZE(desc), "NIC3_QM0");
7288 break;
7289 case GAUDI_EVENT_NIC3_QM1:
7290 glbl_sts_addr = mmNIC3_QM1_GLBL_STS1_0;
7291 arb_err_addr = mmNIC3_QM1_ARB_ERR_CAUSE;
7292 snprintf(desc, ARRAY_SIZE(desc), "NIC3_QM1");
7293 break;
7294 case GAUDI_EVENT_NIC4_QM0:
7295 glbl_sts_addr = mmNIC4_QM0_GLBL_STS1_0;
7296 arb_err_addr = mmNIC4_QM0_ARB_ERR_CAUSE;
7297 snprintf(desc, ARRAY_SIZE(desc), "NIC4_QM0");
7298 break;
7299 case GAUDI_EVENT_NIC4_QM1:
7300 glbl_sts_addr = mmNIC4_QM1_GLBL_STS1_0;
7301 arb_err_addr = mmNIC4_QM1_ARB_ERR_CAUSE;
7302 snprintf(desc, ARRAY_SIZE(desc), "NIC4_QM1");
7303 break;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007304 default:
7305 return;
7306 }
7307
7308 gaudi_handle_qman_err_generic(hdev, desc, glbl_sts_addr, arb_err_addr);
7309}
7310
7311static void gaudi_print_irq_info(struct hl_device *hdev, u16 event_type,
7312 bool razwi)
7313{
Ofir Bittonebd8d122020-05-10 13:41:28 +03007314 char desc[64] = "";
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007315
7316 gaudi_get_event_desc(event_type, desc, sizeof(desc));
7317 dev_err_ratelimited(hdev->dev, "Received H/W interrupt %d [\"%s\"]\n",
7318 event_type, desc);
7319
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007320 if (razwi) {
7321 gaudi_print_razwi_info(hdev);
7322 gaudi_print_mmu_error_info(hdev);
7323 }
7324}
7325
Ohad Sharabi5d6a1982021-02-08 14:53:56 +02007326static void gaudi_print_out_of_sync_info(struct hl_device *hdev,
7327 struct cpucp_pkt_sync_err *sync_err)
7328{
7329 struct hl_hw_queue *q = &hdev->kernel_queues[GAUDI_QUEUE_ID_CPU_PQ];
7330
7331 dev_err(hdev->dev, "Out of sync with FW, FW: pi=%u, ci=%u, LKD: pi=%u, ci=%u\n",
7332 sync_err->pi, sync_err->ci, q->pi, atomic_read(&q->ci));
7333}
7334
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007335static int gaudi_soft_reset_late_init(struct hl_device *hdev)
7336{
Ofir Bittonebd8d122020-05-10 13:41:28 +03007337 struct gaudi_device *gaudi = hdev->asic_specific;
7338
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007339 /* Unmask all IRQs since some could have been received
7340 * during the soft reset
7341 */
Ofir Bittonebd8d122020-05-10 13:41:28 +03007342 return hl_fw_unmask_irq_arr(hdev, gaudi->events, sizeof(gaudi->events));
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007343}
7344
Ofir Bitton5a2998f2020-10-05 13:44:59 +03007345static int gaudi_hbm_read_interrupts(struct hl_device *hdev, int device,
7346 struct hl_eq_hbm_ecc_data *hbm_ecc_data)
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007347{
Ofir Bitton5a2998f2020-10-05 13:44:59 +03007348 u32 base, val, val2, wr_par, rd_par, ca_par, derr, serr, type, ch;
7349 int err = 0;
7350
Ohad Sharabib520ca52021-01-27 15:42:53 +02007351 if (hdev->asic_prop.fw_security_status_valid &&
7352 (hdev->asic_prop.fw_app_security_map &
7353 CPU_BOOT_DEV_STS0_HBM_ECC_EN)) {
Ofir Bitton5a2998f2020-10-05 13:44:59 +03007354 if (!hbm_ecc_data) {
7355 dev_err(hdev->dev, "No FW ECC data");
7356 return 0;
7357 }
7358
7359 wr_par = FIELD_GET(CPUCP_PKT_HBM_ECC_INFO_WR_PAR_MASK,
7360 le32_to_cpu(hbm_ecc_data->hbm_ecc_info));
7361 rd_par = FIELD_GET(CPUCP_PKT_HBM_ECC_INFO_RD_PAR_MASK,
7362 le32_to_cpu(hbm_ecc_data->hbm_ecc_info));
7363 ca_par = FIELD_GET(CPUCP_PKT_HBM_ECC_INFO_CA_PAR_MASK,
7364 le32_to_cpu(hbm_ecc_data->hbm_ecc_info));
7365 derr = FIELD_GET(CPUCP_PKT_HBM_ECC_INFO_DERR_MASK,
7366 le32_to_cpu(hbm_ecc_data->hbm_ecc_info));
7367 serr = FIELD_GET(CPUCP_PKT_HBM_ECC_INFO_SERR_MASK,
7368 le32_to_cpu(hbm_ecc_data->hbm_ecc_info));
7369 type = FIELD_GET(CPUCP_PKT_HBM_ECC_INFO_TYPE_MASK,
7370 le32_to_cpu(hbm_ecc_data->hbm_ecc_info));
7371 ch = FIELD_GET(CPUCP_PKT_HBM_ECC_INFO_HBM_CH_MASK,
7372 le32_to_cpu(hbm_ecc_data->hbm_ecc_info));
7373
7374 dev_err(hdev->dev,
Ohad Sharabib520ca52021-01-27 15:42:53 +02007375 "HBM%d pc%d interrupts info: WR_PAR=%d, RD_PAR=%d, CA_PAR=%d, SERR=%d, DERR=%d\n",
7376 device, ch, wr_par, rd_par, ca_par, serr, derr);
7377 dev_err(hdev->dev,
7378 "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",
7379 device, ch, hbm_ecc_data->first_addr, type,
7380 hbm_ecc_data->sec_cont_cnt, hbm_ecc_data->sec_cnt,
7381 hbm_ecc_data->dec_cnt);
Ofir Bitton5a2998f2020-10-05 13:44:59 +03007382 return 0;
7383 }
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007384
Ohad Sharabib520ca52021-01-27 15:42:53 +02007385 if (!hdev->asic_prop.fw_security_disabled) {
7386 dev_info(hdev->dev, "Cannot access MC regs for ECC data while security is enabled\n");
7387 return 0;
7388 }
7389
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007390 base = GAUDI_HBM_CFG_BASE + device * GAUDI_HBM_CFG_OFFSET;
7391 for (ch = 0 ; ch < GAUDI_HBM_CHANNELS ; ch++) {
7392 val = RREG32_MASK(base + ch * 0x1000 + 0x06C, 0x0000FFFF);
7393 val = (val & 0xFF) | ((val >> 8) & 0xFF);
7394 if (val) {
7395 err = 1;
7396 dev_err(hdev->dev,
7397 "HBM%d pc%d interrupts info: WR_PAR=%d, RD_PAR=%d, CA_PAR=%d, SERR=%d, DERR=%d\n",
7398 device, ch * 2, val & 0x1, (val >> 1) & 0x1,
7399 (val >> 2) & 0x1, (val >> 3) & 0x1,
7400 (val >> 4) & 0x1);
7401
7402 val2 = RREG32(base + ch * 0x1000 + 0x060);
7403 dev_err(hdev->dev,
Ofir Bitton5a2998f2020-10-05 13:44:59 +03007404 "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 +03007405 device, ch * 2,
7406 RREG32(base + ch * 0x1000 + 0x064),
7407 (val2 & 0x200) >> 9, (val2 & 0xFC00) >> 10,
7408 (val2 & 0xFF0000) >> 16,
7409 (val2 & 0xFF000000) >> 24);
7410 }
7411
7412 val = RREG32_MASK(base + ch * 0x1000 + 0x07C, 0x0000FFFF);
7413 val = (val & 0xFF) | ((val >> 8) & 0xFF);
7414 if (val) {
7415 err = 1;
7416 dev_err(hdev->dev,
7417 "HBM%d pc%d interrupts info: WR_PAR=%d, RD_PAR=%d, CA_PAR=%d, SERR=%d, DERR=%d\n",
7418 device, ch * 2 + 1, val & 0x1, (val >> 1) & 0x1,
7419 (val >> 2) & 0x1, (val >> 3) & 0x1,
7420 (val >> 4) & 0x1);
7421
7422 val2 = RREG32(base + ch * 0x1000 + 0x070);
7423 dev_err(hdev->dev,
Ofir Bitton5a2998f2020-10-05 13:44:59 +03007424 "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 +03007425 device, ch * 2 + 1,
7426 RREG32(base + ch * 0x1000 + 0x074),
7427 (val2 & 0x200) >> 9, (val2 & 0xFC00) >> 10,
7428 (val2 & 0xFF0000) >> 16,
7429 (val2 & 0xFF000000) >> 24);
7430 }
7431
7432 /* Clear interrupts */
7433 RMWREG32(base + (ch * 0x1000) + 0x060, 0x1C8, 0x1FF);
7434 RMWREG32(base + (ch * 0x1000) + 0x070, 0x1C8, 0x1FF);
7435 WREG32(base + (ch * 0x1000) + 0x06C, 0x1F1F);
7436 WREG32(base + (ch * 0x1000) + 0x07C, 0x1F1F);
7437 RMWREG32(base + (ch * 0x1000) + 0x060, 0x0, 0xF);
7438 RMWREG32(base + (ch * 0x1000) + 0x070, 0x0, 0xF);
7439 }
7440
7441 val = RREG32(base + 0x8F30);
7442 val2 = RREG32(base + 0x8F34);
7443 if (val | val2) {
7444 err = 1;
7445 dev_err(hdev->dev,
7446 "HBM %d MC SRAM SERR info: Reg 0x8F30=0x%x, Reg 0x8F34=0x%x\n",
7447 device, val, val2);
7448 }
7449 val = RREG32(base + 0x8F40);
7450 val2 = RREG32(base + 0x8F44);
7451 if (val | val2) {
7452 err = 1;
7453 dev_err(hdev->dev,
7454 "HBM %d MC SRAM DERR info: Reg 0x8F40=0x%x, Reg 0x8F44=0x%x\n",
7455 device, val, val2);
7456 }
7457
7458 return err;
7459}
7460
7461static int gaudi_hbm_event_to_dev(u16 hbm_event_type)
7462{
7463 switch (hbm_event_type) {
7464 case GAUDI_EVENT_HBM0_SPI_0:
7465 case GAUDI_EVENT_HBM0_SPI_1:
7466 return 0;
7467 case GAUDI_EVENT_HBM1_SPI_0:
7468 case GAUDI_EVENT_HBM1_SPI_1:
7469 return 1;
7470 case GAUDI_EVENT_HBM2_SPI_0:
7471 case GAUDI_EVENT_HBM2_SPI_1:
7472 return 2;
7473 case GAUDI_EVENT_HBM3_SPI_0:
7474 case GAUDI_EVENT_HBM3_SPI_1:
7475 return 3;
7476 default:
7477 break;
7478 }
7479
7480 /* Should never happen */
7481 return 0;
7482}
7483
7484static bool gaudi_tpc_read_interrupts(struct hl_device *hdev, u8 tpc_id,
7485 char *interrupt_name)
7486{
7487 struct gaudi_device *gaudi = hdev->asic_specific;
7488 u32 tpc_offset = tpc_id * TPC_CFG_OFFSET, tpc_interrupts_cause, i;
7489 bool soft_reset_required = false;
7490
7491 /* Accessing the TPC_INTR_CAUSE registers requires disabling the clock
Oded Gabbay6138bbe2020-09-04 20:18:16 +03007492 * gating, and thus cannot be done in CPU-CP and should be done instead
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007493 * by the driver.
7494 */
7495
7496 mutex_lock(&gaudi->clk_gate_mutex);
7497
7498 hdev->asic_funcs->disable_clock_gating(hdev);
7499
7500 tpc_interrupts_cause = RREG32(mmTPC0_CFG_TPC_INTR_CAUSE + tpc_offset) &
7501 TPC0_CFG_TPC_INTR_CAUSE_CAUSE_MASK;
7502
7503 for (i = 0 ; i < GAUDI_NUM_OF_TPC_INTR_CAUSE ; i++)
7504 if (tpc_interrupts_cause & BIT(i)) {
7505 dev_err_ratelimited(hdev->dev,
7506 "TPC%d_%s interrupt cause: %s\n",
7507 tpc_id, interrupt_name,
7508 gaudi_tpc_interrupts_cause[i]);
7509 /* If this is QM error, we need to soft-reset */
7510 if (i == 15)
7511 soft_reset_required = true;
7512 }
7513
7514 /* Clear interrupts */
7515 WREG32(mmTPC0_CFG_TPC_INTR_CAUSE + tpc_offset, 0);
7516
Oded Gabbaye38bfd32020-07-03 20:46:12 +03007517 hdev->asic_funcs->set_clock_gating(hdev);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007518
7519 mutex_unlock(&gaudi->clk_gate_mutex);
7520
7521 return soft_reset_required;
7522}
7523
7524static int tpc_dec_event_to_tpc_id(u16 tpc_dec_event_type)
7525{
7526 return (tpc_dec_event_type - GAUDI_EVENT_TPC0_DEC) >> 1;
7527}
7528
7529static int tpc_krn_event_to_tpc_id(u16 tpc_dec_event_type)
7530{
7531 return (tpc_dec_event_type - GAUDI_EVENT_TPC0_KRN_ERR) / 6;
7532}
7533
7534static void gaudi_print_clk_change_info(struct hl_device *hdev,
7535 u16 event_type)
7536{
7537 switch (event_type) {
7538 case GAUDI_EVENT_FIX_POWER_ENV_S:
Ofir Bitton0a068ad2020-07-21 10:49:51 +03007539 hdev->clk_throttling_reason |= HL_CLK_THROTTLE_POWER;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007540 dev_info_ratelimited(hdev->dev,
7541 "Clock throttling due to power consumption\n");
7542 break;
7543
7544 case GAUDI_EVENT_FIX_POWER_ENV_E:
Ofir Bitton0a068ad2020-07-21 10:49:51 +03007545 hdev->clk_throttling_reason &= ~HL_CLK_THROTTLE_POWER;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007546 dev_info_ratelimited(hdev->dev,
7547 "Power envelop is safe, back to optimal clock\n");
7548 break;
7549
7550 case GAUDI_EVENT_FIX_THERMAL_ENV_S:
Ofir Bitton0a068ad2020-07-21 10:49:51 +03007551 hdev->clk_throttling_reason |= HL_CLK_THROTTLE_THERMAL;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007552 dev_info_ratelimited(hdev->dev,
7553 "Clock throttling due to overheating\n");
7554 break;
7555
7556 case GAUDI_EVENT_FIX_THERMAL_ENV_E:
Ofir Bitton0a068ad2020-07-21 10:49:51 +03007557 hdev->clk_throttling_reason &= ~HL_CLK_THROTTLE_THERMAL;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007558 dev_info_ratelimited(hdev->dev,
7559 "Thermal envelop is safe, back to optimal clock\n");
7560 break;
7561
7562 default:
7563 dev_err(hdev->dev, "Received invalid clock change event %d\n",
7564 event_type);
7565 break;
7566 }
7567}
7568
7569static void gaudi_handle_eqe(struct hl_device *hdev,
7570 struct hl_eq_entry *eq_entry)
7571{
7572 struct gaudi_device *gaudi = hdev->asic_specific;
7573 u32 ctl = le32_to_cpu(eq_entry->hdr.ctl);
7574 u16 event_type = ((ctl & EQ_CTL_EVENT_TYPE_MASK)
7575 >> EQ_CTL_EVENT_TYPE_SHIFT);
7576 u8 cause;
Oded Gabbay66446822020-05-18 16:48:01 +03007577 bool reset_required;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007578
7579 gaudi->events_stat[event_type]++;
7580 gaudi->events_stat_aggregate[event_type]++;
7581
7582 switch (event_type) {
7583 case GAUDI_EVENT_PCIE_CORE_DERR:
7584 case GAUDI_EVENT_PCIE_IF_DERR:
7585 case GAUDI_EVENT_PCIE_PHY_DERR:
7586 case GAUDI_EVENT_TPC0_DERR ... GAUDI_EVENT_TPC7_DERR:
7587 case GAUDI_EVENT_MME0_ACC_DERR:
7588 case GAUDI_EVENT_MME0_SBAB_DERR:
7589 case GAUDI_EVENT_MME1_ACC_DERR:
7590 case GAUDI_EVENT_MME1_SBAB_DERR:
7591 case GAUDI_EVENT_MME2_ACC_DERR:
7592 case GAUDI_EVENT_MME2_SBAB_DERR:
7593 case GAUDI_EVENT_MME3_ACC_DERR:
7594 case GAUDI_EVENT_MME3_SBAB_DERR:
7595 case GAUDI_EVENT_DMA0_DERR_ECC ... GAUDI_EVENT_DMA7_DERR_ECC:
7596 fallthrough;
7597 case GAUDI_EVENT_CPU_IF_ECC_DERR:
7598 case GAUDI_EVENT_PSOC_MEM_DERR:
7599 case GAUDI_EVENT_PSOC_CORESIGHT_DERR:
7600 case GAUDI_EVENT_SRAM0_DERR ... GAUDI_EVENT_SRAM28_DERR:
7601 case GAUDI_EVENT_DMA_IF0_DERR ... GAUDI_EVENT_DMA_IF3_DERR:
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007602 case GAUDI_EVENT_HBM_0_DERR ... GAUDI_EVENT_HBM_3_DERR:
7603 case GAUDI_EVENT_MMU_DERR:
Oded Gabbayfcc6a4e2020-05-17 08:20:35 +03007604 gaudi_print_irq_info(hdev, event_type, true);
7605 gaudi_handle_ecc_event(hdev, event_type, &eq_entry->ecc_data);
Ohad Sharabie42a6400f2021-02-17 20:42:48 +02007606 goto reset_device;
Oded Gabbayfcc6a4e2020-05-17 08:20:35 +03007607
7608 case GAUDI_EVENT_GIC500:
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007609 case GAUDI_EVENT_AXI_ECC:
7610 case GAUDI_EVENT_L2_RAM_ECC:
7611 case GAUDI_EVENT_PLL0 ... GAUDI_EVENT_PLL17:
7612 gaudi_print_irq_info(hdev, event_type, false);
Ohad Sharabie42a6400f2021-02-17 20:42:48 +02007613 goto reset_device;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007614
7615 case GAUDI_EVENT_HBM0_SPI_0:
7616 case GAUDI_EVENT_HBM1_SPI_0:
7617 case GAUDI_EVENT_HBM2_SPI_0:
7618 case GAUDI_EVENT_HBM3_SPI_0:
7619 gaudi_print_irq_info(hdev, event_type, false);
7620 gaudi_hbm_read_interrupts(hdev,
Ofir Bitton5a2998f2020-10-05 13:44:59 +03007621 gaudi_hbm_event_to_dev(event_type),
7622 &eq_entry->hbm_ecc_data);
Ohad Sharabie42a6400f2021-02-17 20:42:48 +02007623 goto reset_device;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007624
7625 case GAUDI_EVENT_HBM0_SPI_1:
7626 case GAUDI_EVENT_HBM1_SPI_1:
7627 case GAUDI_EVENT_HBM2_SPI_1:
7628 case GAUDI_EVENT_HBM3_SPI_1:
7629 gaudi_print_irq_info(hdev, event_type, false);
7630 gaudi_hbm_read_interrupts(hdev,
Ofir Bitton5a2998f2020-10-05 13:44:59 +03007631 gaudi_hbm_event_to_dev(event_type),
7632 &eq_entry->hbm_ecc_data);
Oded Gabbay230cd892021-01-26 22:58:13 +02007633 hl_fw_unmask_irq(hdev, event_type);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007634 break;
7635
7636 case GAUDI_EVENT_TPC0_DEC:
7637 case GAUDI_EVENT_TPC1_DEC:
7638 case GAUDI_EVENT_TPC2_DEC:
7639 case GAUDI_EVENT_TPC3_DEC:
7640 case GAUDI_EVENT_TPC4_DEC:
7641 case GAUDI_EVENT_TPC5_DEC:
7642 case GAUDI_EVENT_TPC6_DEC:
7643 case GAUDI_EVENT_TPC7_DEC:
7644 gaudi_print_irq_info(hdev, event_type, true);
Oded Gabbay66446822020-05-18 16:48:01 +03007645 reset_required = gaudi_tpc_read_interrupts(hdev,
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007646 tpc_dec_event_to_tpc_id(event_type),
7647 "AXI_SLV_DEC_Error");
Oded Gabbay66446822020-05-18 16:48:01 +03007648 if (reset_required) {
7649 dev_err(hdev->dev, "hard reset required due to %s\n",
7650 gaudi_irq_map_table[event_type].name);
7651
Ohad Sharabie42a6400f2021-02-17 20:42:48 +02007652 goto reset_device;
Oded Gabbay66446822020-05-18 16:48:01 +03007653 } else {
7654 hl_fw_unmask_irq(hdev, event_type);
Omer Shpigelmand7985072020-05-17 23:01:22 +03007655 }
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007656 break;
7657
7658 case GAUDI_EVENT_TPC0_KRN_ERR:
7659 case GAUDI_EVENT_TPC1_KRN_ERR:
7660 case GAUDI_EVENT_TPC2_KRN_ERR:
7661 case GAUDI_EVENT_TPC3_KRN_ERR:
7662 case GAUDI_EVENT_TPC4_KRN_ERR:
7663 case GAUDI_EVENT_TPC5_KRN_ERR:
7664 case GAUDI_EVENT_TPC6_KRN_ERR:
7665 case GAUDI_EVENT_TPC7_KRN_ERR:
7666 gaudi_print_irq_info(hdev, event_type, true);
Oded Gabbay66446822020-05-18 16:48:01 +03007667 reset_required = gaudi_tpc_read_interrupts(hdev,
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007668 tpc_krn_event_to_tpc_id(event_type),
7669 "KRN_ERR");
Oded Gabbay66446822020-05-18 16:48:01 +03007670 if (reset_required) {
7671 dev_err(hdev->dev, "hard reset required due to %s\n",
7672 gaudi_irq_map_table[event_type].name);
7673
Ohad Sharabie42a6400f2021-02-17 20:42:48 +02007674 goto reset_device;
Oded Gabbay66446822020-05-18 16:48:01 +03007675 } else {
7676 hl_fw_unmask_irq(hdev, event_type);
Omer Shpigelmand7985072020-05-17 23:01:22 +03007677 }
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007678 break;
7679
7680 case GAUDI_EVENT_PCIE_CORE_SERR:
7681 case GAUDI_EVENT_PCIE_IF_SERR:
7682 case GAUDI_EVENT_PCIE_PHY_SERR:
7683 case GAUDI_EVENT_TPC0_SERR ... GAUDI_EVENT_TPC7_SERR:
7684 case GAUDI_EVENT_MME0_ACC_SERR:
7685 case GAUDI_EVENT_MME0_SBAB_SERR:
7686 case GAUDI_EVENT_MME1_ACC_SERR:
7687 case GAUDI_EVENT_MME1_SBAB_SERR:
7688 case GAUDI_EVENT_MME2_ACC_SERR:
7689 case GAUDI_EVENT_MME2_SBAB_SERR:
7690 case GAUDI_EVENT_MME3_ACC_SERR:
7691 case GAUDI_EVENT_MME3_SBAB_SERR:
7692 case GAUDI_EVENT_DMA0_SERR_ECC ... GAUDI_EVENT_DMA7_SERR_ECC:
7693 case GAUDI_EVENT_CPU_IF_ECC_SERR:
7694 case GAUDI_EVENT_PSOC_MEM_SERR:
7695 case GAUDI_EVENT_PSOC_CORESIGHT_SERR:
7696 case GAUDI_EVENT_SRAM0_SERR ... GAUDI_EVENT_SRAM28_SERR:
7697 case GAUDI_EVENT_DMA_IF0_SERR ... GAUDI_EVENT_DMA_IF3_SERR:
7698 case GAUDI_EVENT_HBM_0_SERR ... GAUDI_EVENT_HBM_3_SERR:
7699 fallthrough;
7700 case GAUDI_EVENT_MMU_SERR:
Oded Gabbayfcc6a4e2020-05-17 08:20:35 +03007701 gaudi_print_irq_info(hdev, event_type, true);
7702 gaudi_handle_ecc_event(hdev, event_type, &eq_entry->ecc_data);
7703 hl_fw_unmask_irq(hdev, event_type);
7704 break;
7705
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007706 case GAUDI_EVENT_PCIE_DEC:
7707 case GAUDI_EVENT_MME0_WBC_RSP:
7708 case GAUDI_EVENT_MME0_SBAB0_RSP:
7709 case GAUDI_EVENT_MME1_WBC_RSP:
7710 case GAUDI_EVENT_MME1_SBAB0_RSP:
7711 case GAUDI_EVENT_MME2_WBC_RSP:
7712 case GAUDI_EVENT_MME2_SBAB0_RSP:
7713 case GAUDI_EVENT_MME3_WBC_RSP:
7714 case GAUDI_EVENT_MME3_SBAB0_RSP:
7715 case GAUDI_EVENT_CPU_AXI_SPLITTER:
7716 case GAUDI_EVENT_PSOC_AXI_DEC:
7717 case GAUDI_EVENT_PSOC_PRSTN_FALL:
7718 case GAUDI_EVENT_MMU_PAGE_FAULT:
7719 case GAUDI_EVENT_MMU_WR_PERM:
7720 case GAUDI_EVENT_RAZWI_OR_ADC:
7721 case GAUDI_EVENT_TPC0_QM ... GAUDI_EVENT_TPC7_QM:
7722 case GAUDI_EVENT_MME0_QM ... GAUDI_EVENT_MME2_QM:
7723 case GAUDI_EVENT_DMA0_QM ... GAUDI_EVENT_DMA7_QM:
7724 fallthrough;
Oded Gabbay3c681572020-11-02 21:10:39 +02007725 case GAUDI_EVENT_NIC0_QM0:
7726 case GAUDI_EVENT_NIC0_QM1:
7727 case GAUDI_EVENT_NIC1_QM0:
7728 case GAUDI_EVENT_NIC1_QM1:
7729 case GAUDI_EVENT_NIC2_QM0:
7730 case GAUDI_EVENT_NIC2_QM1:
7731 case GAUDI_EVENT_NIC3_QM0:
7732 case GAUDI_EVENT_NIC3_QM1:
7733 case GAUDI_EVENT_NIC4_QM0:
7734 case GAUDI_EVENT_NIC4_QM1:
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007735 case GAUDI_EVENT_DMA0_CORE ... GAUDI_EVENT_DMA7_CORE:
7736 gaudi_print_irq_info(hdev, event_type, true);
7737 gaudi_handle_qman_err(hdev, event_type);
Ofir Bittonebd8d122020-05-10 13:41:28 +03007738 hl_fw_unmask_irq(hdev, event_type);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007739 break;
7740
7741 case GAUDI_EVENT_RAZWI_OR_ADC_SW:
7742 gaudi_print_irq_info(hdev, event_type, true);
Ohad Sharabie42a6400f2021-02-17 20:42:48 +02007743 goto reset_device;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007744
7745 case GAUDI_EVENT_TPC0_BMON_SPMU:
7746 case GAUDI_EVENT_TPC1_BMON_SPMU:
7747 case GAUDI_EVENT_TPC2_BMON_SPMU:
7748 case GAUDI_EVENT_TPC3_BMON_SPMU:
7749 case GAUDI_EVENT_TPC4_BMON_SPMU:
7750 case GAUDI_EVENT_TPC5_BMON_SPMU:
7751 case GAUDI_EVENT_TPC6_BMON_SPMU:
7752 case GAUDI_EVENT_TPC7_BMON_SPMU:
7753 case GAUDI_EVENT_DMA_BM_CH0 ... GAUDI_EVENT_DMA_BM_CH7:
7754 gaudi_print_irq_info(hdev, event_type, false);
Ofir Bittonebd8d122020-05-10 13:41:28 +03007755 hl_fw_unmask_irq(hdev, event_type);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007756 break;
7757
Ofir Bittonf8bc7f02021-01-03 20:52:40 +02007758 case GAUDI_EVENT_DMA_IF_SEI_0 ... GAUDI_EVENT_DMA_IF_SEI_3:
7759 gaudi_print_irq_info(hdev, event_type, false);
7760 gaudi_print_sm_sei_info(hdev, event_type,
7761 &eq_entry->sm_sei_data);
7762 hl_fw_unmask_irq(hdev, event_type);
7763 break;
7764
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007765 case GAUDI_EVENT_FIX_POWER_ENV_S ... GAUDI_EVENT_FIX_THERMAL_ENV_E:
7766 gaudi_print_clk_change_info(hdev, event_type);
Ofir Bittonebd8d122020-05-10 13:41:28 +03007767 hl_fw_unmask_irq(hdev, event_type);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007768 break;
7769
7770 case GAUDI_EVENT_PSOC_GPIO_U16_0:
7771 cause = le64_to_cpu(eq_entry->data[0]) & 0xFF;
7772 dev_err(hdev->dev,
7773 "Received high temp H/W interrupt %d (cause %d)\n",
7774 event_type, cause);
7775 break;
7776
Ofir Bittond661d792021-03-09 14:45:04 +02007777 case GAUDI_EVENT_DEV_RESET_REQ:
Ofir Bitton2ea09532021-03-03 13:23:47 +02007778 gaudi_print_irq_info(hdev, event_type, false);
7779 goto reset_device;
7780
Ohad Sharabi5d6a1982021-02-08 14:53:56 +02007781 case GAUDI_EVENT_PKT_QUEUE_OUT_SYNC:
7782 gaudi_print_irq_info(hdev, event_type, false);
7783 gaudi_print_out_of_sync_info(hdev, &eq_entry->pkt_sync_err);
Ohad Sharabie42a6400f2021-02-17 20:42:48 +02007784 goto reset_device;
Ohad Sharabi5d6a1982021-02-08 14:53:56 +02007785
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007786 default:
7787 dev_err(hdev->dev, "Received invalid H/W interrupt %d\n",
7788 event_type);
7789 break;
7790 }
Ohad Sharabie42a6400f2021-02-17 20:42:48 +02007791
7792 return;
7793
7794reset_device:
7795 if (hdev->hard_reset_on_fw_events)
7796 hl_device_reset(hdev, HL_RESET_HARD);
7797 else
7798 hl_fw_unmask_irq(hdev, event_type);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007799}
7800
7801static void *gaudi_get_events_stat(struct hl_device *hdev, bool aggregate,
7802 u32 *size)
7803{
7804 struct gaudi_device *gaudi = hdev->asic_specific;
7805
7806 if (aggregate) {
7807 *size = (u32) sizeof(gaudi->events_stat_aggregate);
7808 return gaudi->events_stat_aggregate;
7809 }
7810
7811 *size = (u32) sizeof(gaudi->events_stat);
7812 return gaudi->events_stat;
7813}
7814
Omer Shpigelman8ff5f4f2020-05-24 23:06:59 +03007815static int gaudi_mmu_invalidate_cache(struct hl_device *hdev, bool is_hard,
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007816 u32 flags)
7817{
7818 struct gaudi_device *gaudi = hdev->asic_specific;
7819 u32 status, timeout_usec;
7820 int rc;
7821
7822 if (!(gaudi->hw_cap_initialized & HW_CAP_MMU) ||
7823 hdev->hard_reset_pending)
Omer Shpigelman8ff5f4f2020-05-24 23:06:59 +03007824 return 0;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007825
7826 if (hdev->pldm)
7827 timeout_usec = GAUDI_PLDM_MMU_TIMEOUT_USEC;
7828 else
7829 timeout_usec = MMU_CONFIG_TIMEOUT_USEC;
7830
7831 /* L0 & L1 invalidation */
Omer Shpigelmancfd41762020-06-03 13:03:35 +03007832 WREG32(mmSTLB_INV_PS, 3);
7833 WREG32(mmSTLB_CACHE_INV, gaudi->mmu_cache_inv_pi++);
Omer Shpigelman42d0b0b2020-05-17 17:35:39 +03007834 WREG32(mmSTLB_INV_PS, 2);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007835
7836 rc = hl_poll_timeout(
7837 hdev,
Omer Shpigelman42d0b0b2020-05-17 17:35:39 +03007838 mmSTLB_INV_PS,
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007839 status,
7840 !status,
7841 1000,
7842 timeout_usec);
7843
Omer Shpigelman42d0b0b2020-05-17 17:35:39 +03007844 WREG32(mmSTLB_INV_SET, 0);
7845
Omer Shpigelman8ff5f4f2020-05-24 23:06:59 +03007846 if (rc) {
7847 dev_err_ratelimited(hdev->dev,
7848 "MMU cache invalidation timeout\n");
Ohad Sharabie42a6400f2021-02-17 20:42:48 +02007849 hl_device_reset(hdev, HL_RESET_HARD);
Omer Shpigelman8ff5f4f2020-05-24 23:06:59 +03007850 }
7851
7852 return rc;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007853}
7854
Omer Shpigelman8ff5f4f2020-05-24 23:06:59 +03007855static int gaudi_mmu_invalidate_cache_range(struct hl_device *hdev,
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007856 bool is_hard, u32 asid, u64 va, u64 size)
7857{
7858 struct gaudi_device *gaudi = hdev->asic_specific;
7859 u32 status, timeout_usec;
7860 u32 inv_data;
7861 u32 pi;
7862 int rc;
7863
7864 if (!(gaudi->hw_cap_initialized & HW_CAP_MMU) ||
7865 hdev->hard_reset_pending)
Omer Shpigelman8ff5f4f2020-05-24 23:06:59 +03007866 return 0;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007867
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007868 if (hdev->pldm)
7869 timeout_usec = GAUDI_PLDM_MMU_TIMEOUT_USEC;
7870 else
7871 timeout_usec = MMU_CONFIG_TIMEOUT_USEC;
7872
7873 /*
7874 * TODO: currently invalidate entire L0 & L1 as in regular hard
7875 * invalidation. Need to apply invalidation of specific cache
7876 * lines with mask of ASID & VA & size.
7877 * Note that L1 with be flushed entirely in any case.
7878 */
7879
7880 /* L0 & L1 invalidation */
7881 inv_data = RREG32(mmSTLB_CACHE_INV);
7882 /* PI is 8 bit */
7883 pi = ((inv_data & STLB_CACHE_INV_PRODUCER_INDEX_MASK) + 1) & 0xFF;
7884 WREG32(mmSTLB_CACHE_INV,
7885 (inv_data & STLB_CACHE_INV_INDEX_MASK_MASK) | pi);
7886
7887 rc = hl_poll_timeout(
7888 hdev,
7889 mmSTLB_INV_CONSUMER_INDEX,
7890 status,
7891 status == pi,
7892 1000,
7893 timeout_usec);
7894
Omer Shpigelman8ff5f4f2020-05-24 23:06:59 +03007895 if (rc) {
7896 dev_err_ratelimited(hdev->dev,
7897 "MMU cache invalidation timeout\n");
Ohad Sharabie42a6400f2021-02-17 20:42:48 +02007898 hl_device_reset(hdev, HL_RESET_HARD);
Omer Shpigelman8ff5f4f2020-05-24 23:06:59 +03007899 }
7900
7901 return rc;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007902}
7903
7904static int gaudi_mmu_update_asid_hop0_addr(struct hl_device *hdev,
7905 u32 asid, u64 phys_addr)
7906{
7907 u32 status, timeout_usec;
7908 int rc;
7909
7910 if (hdev->pldm)
7911 timeout_usec = GAUDI_PLDM_MMU_TIMEOUT_USEC;
7912 else
7913 timeout_usec = MMU_CONFIG_TIMEOUT_USEC;
7914
7915 WREG32(MMU_ASID, asid);
7916 WREG32(MMU_HOP0_PA43_12, phys_addr >> MMU_HOP0_PA43_12_SHIFT);
7917 WREG32(MMU_HOP0_PA49_44, phys_addr >> MMU_HOP0_PA49_44_SHIFT);
7918 WREG32(MMU_BUSY, 0x80000000);
7919
7920 rc = hl_poll_timeout(
7921 hdev,
7922 MMU_BUSY,
7923 status,
7924 !(status & 0x80000000),
7925 1000,
7926 timeout_usec);
7927
7928 if (rc) {
7929 dev_err(hdev->dev,
7930 "Timeout during MMU hop0 config of asid %d\n", asid);
7931 return rc;
7932 }
7933
7934 return 0;
7935}
7936
7937static int gaudi_send_heartbeat(struct hl_device *hdev)
7938{
7939 struct gaudi_device *gaudi = hdev->asic_specific;
7940
7941 if (!(gaudi->hw_cap_initialized & HW_CAP_CPU_Q))
7942 return 0;
7943
7944 return hl_fw_send_heartbeat(hdev);
7945}
7946
Oded Gabbay2f553422020-08-15 16:28:10 +03007947static int gaudi_cpucp_info_get(struct hl_device *hdev)
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007948{
7949 struct gaudi_device *gaudi = hdev->asic_specific;
7950 struct asic_fixed_properties *prop = &hdev->asic_prop;
7951 int rc;
7952
7953 if (!(gaudi->hw_cap_initialized & HW_CAP_CPU_Q))
7954 return 0;
7955
Ohad Sharabie9c20032021-03-22 14:30:52 +02007956 rc = hl_fw_cpucp_handshake(hdev, mmCPU_BOOT_DEV_STS0, mmCPU_BOOT_ERR0);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007957 if (rc)
7958 return rc;
7959
Oded Gabbay2f553422020-08-15 16:28:10 +03007960 if (!strlen(prop->cpucp_info.card_name))
7961 strncpy(prop->cpucp_info.card_name, GAUDI_DEFAULT_CARD_NAME,
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007962 CARD_NAME_MAX_LEN);
7963
Oded Gabbay2f553422020-08-15 16:28:10 +03007964 hdev->card_type = le32_to_cpu(hdev->asic_prop.cpucp_info.card_type);
Oded Gabbay58361aa2020-08-08 23:34:47 +03007965
Koby Elbazcd5def82021-02-23 21:31:27 +02007966 set_default_power_values(hdev);
Oded Gabbay58361aa2020-08-08 23:34:47 +03007967
7968 hdev->max_power = prop->max_power_default;
7969
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007970 return 0;
7971}
7972
Ohad Sharabicf303392021-01-17 16:01:56 +02007973static bool gaudi_is_device_idle(struct hl_device *hdev, u64 *mask_arr,
7974 u8 mask_len, struct seq_file *s)
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007975{
7976 struct gaudi_device *gaudi = hdev->asic_specific;
7977 const char *fmt = "%-5d%-9s%#-14x%#-12x%#x\n";
7978 const char *mme_slave_fmt = "%-5d%-9s%-14s%-12s%#x\n";
Oded Gabbay3c681572020-11-02 21:10:39 +02007979 const char *nic_fmt = "%-5d%-9s%#-14x%#x\n";
Ohad Sharabicf303392021-01-17 16:01:56 +02007980 unsigned long *mask = (unsigned long *)mask_arr;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007981 u32 qm_glbl_sts0, qm_cgm_sts, dma_core_sts0, tpc_cfg_sts, mme_arch_sts;
7982 bool is_idle = true, is_eng_idle, is_slave;
7983 u64 offset;
Oded Gabbay3c681572020-11-02 21:10:39 +02007984 int i, dma_id, port;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007985
7986 mutex_lock(&gaudi->clk_gate_mutex);
7987
7988 hdev->asic_funcs->disable_clock_gating(hdev);
7989
7990 if (s)
7991 seq_puts(s,
7992 "\nDMA is_idle QM_GLBL_STS0 QM_CGM_STS DMA_CORE_STS0\n"
7993 "--- ------- ------------ ---------- -------------\n");
7994
7995 for (i = 0 ; i < DMA_NUMBER_OF_CHNLS ; i++) {
7996 dma_id = gaudi_dma_assignment[i];
7997 offset = dma_id * DMA_QMAN_OFFSET;
7998
7999 qm_glbl_sts0 = RREG32(mmDMA0_QM_GLBL_STS0 + offset);
8000 qm_cgm_sts = RREG32(mmDMA0_QM_CGM_STS + offset);
8001 dma_core_sts0 = RREG32(mmDMA0_CORE_STS0 + offset);
8002 is_eng_idle = IS_QM_IDLE(qm_glbl_sts0, qm_cgm_sts) &&
8003 IS_DMA_IDLE(dma_core_sts0);
8004 is_idle &= is_eng_idle;
8005
Ohad Sharabicf303392021-01-17 16:01:56 +02008006 if (mask && !is_eng_idle)
8007 set_bit(GAUDI_ENGINE_ID_DMA_0 + dma_id, mask);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008008 if (s)
8009 seq_printf(s, fmt, dma_id,
8010 is_eng_idle ? "Y" : "N", qm_glbl_sts0,
8011 qm_cgm_sts, dma_core_sts0);
8012 }
8013
8014 if (s)
8015 seq_puts(s,
8016 "\nTPC is_idle QM_GLBL_STS0 QM_CGM_STS CFG_STATUS\n"
8017 "--- ------- ------------ ---------- ----------\n");
8018
8019 for (i = 0 ; i < TPC_NUMBER_OF_ENGINES ; i++) {
8020 offset = i * TPC_QMAN_OFFSET;
8021 qm_glbl_sts0 = RREG32(mmTPC0_QM_GLBL_STS0 + offset);
8022 qm_cgm_sts = RREG32(mmTPC0_QM_CGM_STS + offset);
8023 tpc_cfg_sts = RREG32(mmTPC0_CFG_STATUS + offset);
8024 is_eng_idle = IS_QM_IDLE(qm_glbl_sts0, qm_cgm_sts) &&
8025 IS_TPC_IDLE(tpc_cfg_sts);
8026 is_idle &= is_eng_idle;
8027
Ohad Sharabicf303392021-01-17 16:01:56 +02008028 if (mask && !is_eng_idle)
8029 set_bit(GAUDI_ENGINE_ID_TPC_0 + i, mask);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008030 if (s)
8031 seq_printf(s, fmt, i,
8032 is_eng_idle ? "Y" : "N",
8033 qm_glbl_sts0, qm_cgm_sts, tpc_cfg_sts);
8034 }
8035
8036 if (s)
8037 seq_puts(s,
8038 "\nMME is_idle QM_GLBL_STS0 QM_CGM_STS ARCH_STATUS\n"
8039 "--- ------- ------------ ---------- -----------\n");
8040
8041 for (i = 0 ; i < MME_NUMBER_OF_ENGINES ; i++) {
8042 offset = i * MME_QMAN_OFFSET;
8043 mme_arch_sts = RREG32(mmMME0_CTRL_ARCH_STATUS + offset);
8044 is_eng_idle = IS_MME_IDLE(mme_arch_sts);
8045
8046 /* MME 1 & 3 are slaves, no need to check their QMANs */
8047 is_slave = i % 2;
8048 if (!is_slave) {
8049 qm_glbl_sts0 = RREG32(mmMME0_QM_GLBL_STS0 + offset);
8050 qm_cgm_sts = RREG32(mmMME0_QM_CGM_STS + offset);
8051 is_eng_idle &= IS_QM_IDLE(qm_glbl_sts0, qm_cgm_sts);
8052 }
8053
8054 is_idle &= is_eng_idle;
8055
Ohad Sharabicf303392021-01-17 16:01:56 +02008056 if (mask && !is_eng_idle)
8057 set_bit(GAUDI_ENGINE_ID_MME_0 + i, mask);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008058 if (s) {
8059 if (!is_slave)
8060 seq_printf(s, fmt, i,
8061 is_eng_idle ? "Y" : "N",
8062 qm_glbl_sts0, qm_cgm_sts, mme_arch_sts);
8063 else
8064 seq_printf(s, mme_slave_fmt, i,
8065 is_eng_idle ? "Y" : "N", "-",
8066 "-", mme_arch_sts);
8067 }
8068 }
8069
8070 if (s)
Oded Gabbay3c681572020-11-02 21:10:39 +02008071 seq_puts(s, "\nNIC is_idle QM_GLBL_STS0 QM_CGM_STS\n"
8072 "--- ------- ------------ ----------\n");
8073
8074 for (i = 0 ; i < (NIC_NUMBER_OF_ENGINES / 2) ; i++) {
8075 offset = i * NIC_MACRO_QMAN_OFFSET;
8076 port = 2 * i;
8077 if (hdev->nic_ports_mask & BIT(port)) {
8078 qm_glbl_sts0 = RREG32(mmNIC0_QM0_GLBL_STS0 + offset);
8079 qm_cgm_sts = RREG32(mmNIC0_QM0_CGM_STS + offset);
8080 is_eng_idle = IS_QM_IDLE(qm_glbl_sts0, qm_cgm_sts);
8081 is_idle &= is_eng_idle;
8082
Ohad Sharabicf303392021-01-17 16:01:56 +02008083 if (mask && !is_eng_idle)
8084 set_bit(GAUDI_ENGINE_ID_NIC_0 + port, mask);
Oded Gabbay3c681572020-11-02 21:10:39 +02008085 if (s)
8086 seq_printf(s, nic_fmt, port,
8087 is_eng_idle ? "Y" : "N",
8088 qm_glbl_sts0, qm_cgm_sts);
8089 }
8090
8091 port = 2 * i + 1;
8092 if (hdev->nic_ports_mask & BIT(port)) {
8093 qm_glbl_sts0 = RREG32(mmNIC0_QM1_GLBL_STS0 + offset);
8094 qm_cgm_sts = RREG32(mmNIC0_QM1_CGM_STS + offset);
8095 is_eng_idle = IS_QM_IDLE(qm_glbl_sts0, qm_cgm_sts);
8096 is_idle &= is_eng_idle;
8097
Ohad Sharabicf303392021-01-17 16:01:56 +02008098 if (mask && !is_eng_idle)
8099 set_bit(GAUDI_ENGINE_ID_NIC_0 + port, mask);
Oded Gabbay3c681572020-11-02 21:10:39 +02008100 if (s)
8101 seq_printf(s, nic_fmt, port,
8102 is_eng_idle ? "Y" : "N",
8103 qm_glbl_sts0, qm_cgm_sts);
8104 }
8105 }
8106
8107 if (s)
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008108 seq_puts(s, "\n");
8109
Oded Gabbaye38bfd32020-07-03 20:46:12 +03008110 hdev->asic_funcs->set_clock_gating(hdev);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008111
8112 mutex_unlock(&gaudi->clk_gate_mutex);
8113
8114 return is_idle;
8115}
8116
8117static void gaudi_hw_queues_lock(struct hl_device *hdev)
8118 __acquires(&gaudi->hw_queues_lock)
8119{
8120 struct gaudi_device *gaudi = hdev->asic_specific;
8121
8122 spin_lock(&gaudi->hw_queues_lock);
8123}
8124
8125static void gaudi_hw_queues_unlock(struct hl_device *hdev)
8126 __releases(&gaudi->hw_queues_lock)
8127{
8128 struct gaudi_device *gaudi = hdev->asic_specific;
8129
8130 spin_unlock(&gaudi->hw_queues_lock);
8131}
8132
8133static u32 gaudi_get_pci_id(struct hl_device *hdev)
8134{
8135 return hdev->pdev->device;
8136}
8137
8138static int gaudi_get_eeprom_data(struct hl_device *hdev, void *data,
8139 size_t max_size)
8140{
8141 struct gaudi_device *gaudi = hdev->asic_specific;
8142
8143 if (!(gaudi->hw_cap_initialized & HW_CAP_CPU_Q))
8144 return 0;
8145
8146 return hl_fw_get_eeprom_data(hdev, data, max_size);
8147}
8148
8149/*
8150 * this function should be used only during initialization and/or after reset,
8151 * when there are no active users.
8152 */
8153static int gaudi_run_tpc_kernel(struct hl_device *hdev, u64 tpc_kernel,
8154 u32 tpc_id)
8155{
8156 struct gaudi_device *gaudi = hdev->asic_specific;
8157 u64 kernel_timeout;
8158 u32 status, offset;
8159 int rc;
8160
8161 offset = tpc_id * (mmTPC1_CFG_STATUS - mmTPC0_CFG_STATUS);
8162
8163 if (hdev->pldm)
8164 kernel_timeout = GAUDI_PLDM_TPC_KERNEL_WAIT_USEC;
8165 else
8166 kernel_timeout = HL_DEVICE_TIMEOUT_USEC;
8167
8168 mutex_lock(&gaudi->clk_gate_mutex);
8169
8170 hdev->asic_funcs->disable_clock_gating(hdev);
8171
8172 WREG32(mmTPC0_CFG_QM_KERNEL_BASE_ADDRESS_LOW + offset,
8173 lower_32_bits(tpc_kernel));
8174 WREG32(mmTPC0_CFG_QM_KERNEL_BASE_ADDRESS_HIGH + offset,
8175 upper_32_bits(tpc_kernel));
8176
8177 WREG32(mmTPC0_CFG_ICACHE_BASE_ADDERESS_LOW + offset,
8178 lower_32_bits(tpc_kernel));
8179 WREG32(mmTPC0_CFG_ICACHE_BASE_ADDERESS_HIGH + offset,
8180 upper_32_bits(tpc_kernel));
8181 /* set a valid LUT pointer, content is of no significance */
8182 WREG32(mmTPC0_CFG_LUT_FUNC256_BASE_ADDR_LO + offset,
8183 lower_32_bits(tpc_kernel));
8184 WREG32(mmTPC0_CFG_LUT_FUNC256_BASE_ADDR_HI + offset,
8185 upper_32_bits(tpc_kernel));
8186
8187 WREG32(mmTPC0_CFG_QM_SYNC_OBJECT_ADDR + offset,
8188 lower_32_bits(CFG_BASE +
8189 mmSYNC_MNGR_E_N_SYNC_MNGR_OBJS_SOB_OBJ_0));
8190
8191 WREG32(mmTPC0_CFG_TPC_CMD + offset,
8192 (1 << TPC0_CFG_TPC_CMD_ICACHE_INVALIDATE_SHIFT |
8193 1 << TPC0_CFG_TPC_CMD_ICACHE_PREFETCH_64KB_SHIFT));
8194 /* wait a bit for the engine to start executing */
8195 usleep_range(1000, 1500);
8196
8197 /* wait until engine has finished executing */
8198 rc = hl_poll_timeout(
8199 hdev,
8200 mmTPC0_CFG_STATUS + offset,
8201 status,
8202 (status & TPC0_CFG_STATUS_VECTOR_PIPE_EMPTY_MASK) ==
8203 TPC0_CFG_STATUS_VECTOR_PIPE_EMPTY_MASK,
8204 1000,
8205 kernel_timeout);
8206
8207 if (rc) {
8208 dev_err(hdev->dev,
8209 "Timeout while waiting for TPC%d icache prefetch\n",
8210 tpc_id);
Oded Gabbaye38bfd32020-07-03 20:46:12 +03008211 hdev->asic_funcs->set_clock_gating(hdev);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008212 mutex_unlock(&gaudi->clk_gate_mutex);
8213 return -EIO;
8214 }
8215
8216 WREG32(mmTPC0_CFG_TPC_EXECUTE + offset,
8217 1 << TPC0_CFG_TPC_EXECUTE_V_SHIFT);
8218
8219 /* wait a bit for the engine to start executing */
8220 usleep_range(1000, 1500);
8221
8222 /* wait until engine has finished executing */
8223 rc = hl_poll_timeout(
8224 hdev,
8225 mmTPC0_CFG_STATUS + offset,
8226 status,
8227 (status & TPC0_CFG_STATUS_VECTOR_PIPE_EMPTY_MASK) ==
8228 TPC0_CFG_STATUS_VECTOR_PIPE_EMPTY_MASK,
8229 1000,
8230 kernel_timeout);
8231
Oded Gabbay31ac1f12020-08-12 11:28:13 +03008232 if (rc) {
8233 dev_err(hdev->dev,
8234 "Timeout while waiting for TPC%d vector pipe\n",
8235 tpc_id);
8236 hdev->asic_funcs->set_clock_gating(hdev);
8237 mutex_unlock(&gaudi->clk_gate_mutex);
8238 return -EIO;
8239 }
8240
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008241 rc = hl_poll_timeout(
8242 hdev,
8243 mmTPC0_CFG_WQ_INFLIGHT_CNTR + offset,
8244 status,
8245 (status == 0),
8246 1000,
8247 kernel_timeout);
8248
Oded Gabbaye38bfd32020-07-03 20:46:12 +03008249 hdev->asic_funcs->set_clock_gating(hdev);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008250 mutex_unlock(&gaudi->clk_gate_mutex);
8251
8252 if (rc) {
8253 dev_err(hdev->dev,
8254 "Timeout while waiting for TPC%d kernel to execute\n",
8255 tpc_id);
8256 return -EIO;
8257 }
8258
8259 return 0;
8260}
8261
Ofir Bitton5de406c2020-09-10 10:56:26 +03008262static int gaudi_internal_cb_pool_init(struct hl_device *hdev,
8263 struct hl_ctx *ctx)
8264{
8265 struct gaudi_device *gaudi = hdev->asic_specific;
Ofir Bitton5de406c2020-09-10 10:56:26 +03008266 int min_alloc_order, rc, collective_cb_size;
8267
8268 if (!(gaudi->hw_cap_initialized & HW_CAP_MMU))
8269 return 0;
8270
8271 hdev->internal_cb_pool_virt_addr =
8272 hdev->asic_funcs->asic_dma_alloc_coherent(hdev,
8273 HOST_SPACE_INTERNAL_CB_SZ,
8274 &hdev->internal_cb_pool_dma_addr,
8275 GFP_KERNEL | __GFP_ZERO);
8276
8277 if (!hdev->internal_cb_pool_virt_addr)
8278 return -ENOMEM;
8279
8280 collective_cb_size = sizeof(struct packet_msg_short) * 5 +
8281 sizeof(struct packet_fence);
8282 min_alloc_order = ilog2(collective_cb_size);
8283
8284 hdev->internal_cb_pool = gen_pool_create(min_alloc_order, -1);
8285 if (!hdev->internal_cb_pool) {
8286 dev_err(hdev->dev,
8287 "Failed to create internal CB pool\n");
8288 rc = -ENOMEM;
8289 goto free_internal_cb_pool;
8290 }
8291
8292 rc = gen_pool_add(hdev->internal_cb_pool,
8293 (uintptr_t) hdev->internal_cb_pool_virt_addr,
8294 HOST_SPACE_INTERNAL_CB_SZ, -1);
8295 if (rc) {
8296 dev_err(hdev->dev,
8297 "Failed to add memory to internal CB pool\n");
8298 rc = -EFAULT;
8299 goto destroy_internal_cb_pool;
8300 }
8301
Ofir Bittonbe91b912020-10-22 15:04:10 +03008302 hdev->internal_cb_va_base = hl_reserve_va_block(hdev, ctx,
Ofir Bitton412c41f2020-11-04 15:18:55 +02008303 HL_VA_RANGE_TYPE_HOST, HOST_SPACE_INTERNAL_CB_SZ,
8304 HL_MMU_VA_ALIGNMENT_NOT_NEEDED);
Ofir Bittonbe91b912020-10-22 15:04:10 +03008305
8306 if (!hdev->internal_cb_va_base)
8307 goto destroy_internal_cb_pool;
Ofir Bitton5de406c2020-09-10 10:56:26 +03008308
8309 mutex_lock(&ctx->mmu_lock);
Ofir Bitton5c054872020-10-22 15:13:10 +03008310 rc = hl_mmu_map_contiguous(ctx, hdev->internal_cb_va_base,
8311 hdev->internal_cb_pool_dma_addr,
8312 HOST_SPACE_INTERNAL_CB_SZ);
Ofir Bitton5de406c2020-09-10 10:56:26 +03008313
8314 hdev->asic_funcs->mmu_invalidate_cache(hdev, false, VM_TYPE_USERPTR);
Ofir Bitton5de406c2020-09-10 10:56:26 +03008315 mutex_unlock(&ctx->mmu_lock);
8316
Ofir Bitton5c054872020-10-22 15:13:10 +03008317 if (rc)
8318 goto unreserve_internal_cb_pool;
8319
Ofir Bitton5de406c2020-09-10 10:56:26 +03008320 return 0;
8321
Ofir Bitton5c054872020-10-22 15:13:10 +03008322unreserve_internal_cb_pool:
Ofir Bittonbe91b912020-10-22 15:04:10 +03008323 hl_unreserve_va_block(hdev, ctx, hdev->internal_cb_va_base,
8324 HOST_SPACE_INTERNAL_CB_SZ);
Ofir Bitton5de406c2020-09-10 10:56:26 +03008325destroy_internal_cb_pool:
8326 gen_pool_destroy(hdev->internal_cb_pool);
Ofir Bitton5de406c2020-09-10 10:56:26 +03008327free_internal_cb_pool:
8328 hdev->asic_funcs->asic_dma_free_coherent(hdev,
8329 HOST_SPACE_INTERNAL_CB_SZ,
8330 hdev->internal_cb_pool_virt_addr,
8331 hdev->internal_cb_pool_dma_addr);
8332
8333 return rc;
8334}
8335
8336static void gaudi_internal_cb_pool_fini(struct hl_device *hdev,
8337 struct hl_ctx *ctx)
8338{
8339 struct gaudi_device *gaudi = hdev->asic_specific;
Ofir Bitton5de406c2020-09-10 10:56:26 +03008340
8341 if (!(gaudi->hw_cap_initialized & HW_CAP_MMU))
8342 return;
8343
8344 mutex_lock(&ctx->mmu_lock);
Ofir Bitton5c054872020-10-22 15:13:10 +03008345 hl_mmu_unmap_contiguous(ctx, hdev->internal_cb_va_base,
8346 HOST_SPACE_INTERNAL_CB_SZ);
Ofir Bittonbe91b912020-10-22 15:04:10 +03008347 hl_unreserve_va_block(hdev, ctx, hdev->internal_cb_va_base,
8348 HOST_SPACE_INTERNAL_CB_SZ);
Ofir Bitton5de406c2020-09-10 10:56:26 +03008349 hdev->asic_funcs->mmu_invalidate_cache(hdev, true, VM_TYPE_USERPTR);
Ofir Bitton5de406c2020-09-10 10:56:26 +03008350 mutex_unlock(&ctx->mmu_lock);
8351
8352 gen_pool_destroy(hdev->internal_cb_pool);
8353
8354 hdev->asic_funcs->asic_dma_free_coherent(hdev,
8355 HOST_SPACE_INTERNAL_CB_SZ,
8356 hdev->internal_cb_pool_virt_addr,
8357 hdev->internal_cb_pool_dma_addr);
8358}
8359
kernel test robotbb34bf72020-07-29 08:03:13 +08008360static int gaudi_ctx_init(struct hl_ctx *ctx)
Ofir Bittona04b7cd2020-07-13 13:36:55 +03008361{
Ofir Bitton8e39e752020-11-12 11:03:32 +02008362 if (ctx->asid == HL_KERNEL_ASID_ID)
8363 return 0;
8364
Ofir Bitton20b75252020-09-30 15:51:10 +03008365 gaudi_mmu_prepare(ctx->hdev, ctx->asid);
Ofir Bitton5de406c2020-09-10 10:56:26 +03008366 return gaudi_internal_cb_pool_init(ctx->hdev, ctx);
8367}
Ofir Bitton20b75252020-09-30 15:51:10 +03008368
kernel test robot293744d2020-11-19 12:25:43 +08008369static void gaudi_ctx_fini(struct hl_ctx *ctx)
Ofir Bitton5de406c2020-09-10 10:56:26 +03008370{
Ofir Bitton8e39e752020-11-12 11:03:32 +02008371 if (ctx->asid == HL_KERNEL_ASID_ID)
Ofir Bitton5de406c2020-09-10 10:56:26 +03008372 return;
8373
8374 gaudi_internal_cb_pool_fini(ctx->hdev, ctx);
Ofir Bittona04b7cd2020-07-13 13:36:55 +03008375}
8376
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008377static u32 gaudi_get_queue_id_for_cq(struct hl_device *hdev, u32 cq_idx)
8378{
8379 return gaudi_cq_assignment[cq_idx];
8380}
8381
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008382static u32 gaudi_get_signal_cb_size(struct hl_device *hdev)
8383{
8384 return sizeof(struct packet_msg_short) +
8385 sizeof(struct packet_msg_prot) * 2;
8386}
8387
8388static u32 gaudi_get_wait_cb_size(struct hl_device *hdev)
8389{
8390 return sizeof(struct packet_msg_short) * 4 +
8391 sizeof(struct packet_fence) +
8392 sizeof(struct packet_msg_prot) * 2;
8393}
8394
Ofir Bitton2992c1d2020-09-10 09:40:35 +03008395static u32 gaudi_gen_signal_cb(struct hl_device *hdev, void *data, u16 sob_id,
Alon Mizrahi72ab9ca52020-12-02 19:55:30 +02008396 u32 size, bool eb)
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008397{
8398 struct hl_cb *cb = (struct hl_cb *) data;
8399 struct packet_msg_short *pkt;
Ofir Bitton2992c1d2020-09-10 09:40:35 +03008400 u32 value, ctl, pkt_size = sizeof(*pkt);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008401
Ofir Bitton2992c1d2020-09-10 09:40:35 +03008402 pkt = cb->kernel_address + size;
8403 memset(pkt, 0, pkt_size);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008404
Ofir Bitton6c07bab2020-06-01 10:38:46 +03008405 /* Inc by 1, Mode ADD */
8406 value = FIELD_PREP(GAUDI_PKT_SHORT_VAL_SOB_SYNC_VAL_MASK, 1);
8407 value |= FIELD_PREP(GAUDI_PKT_SHORT_VAL_SOB_MOD_MASK, 1);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008408
Ofir Bitton6c07bab2020-06-01 10:38:46 +03008409 ctl = FIELD_PREP(GAUDI_PKT_SHORT_CTL_ADDR_MASK, sob_id * 4);
8410 ctl |= FIELD_PREP(GAUDI_PKT_SHORT_CTL_OP_MASK, 0); /* write the value */
8411 ctl |= FIELD_PREP(GAUDI_PKT_SHORT_CTL_BASE_MASK, 3); /* W_S SOB base */
Ofir Bittonf8b0f2e2020-12-06 10:22:32 +02008412 ctl |= FIELD_PREP(GAUDI_PKT_CTL_OPCODE_MASK, PACKET_MSG_SHORT);
8413 ctl |= FIELD_PREP(GAUDI_PKT_CTL_EB_MASK, eb);
8414 ctl |= FIELD_PREP(GAUDI_PKT_CTL_RB_MASK, 1);
8415 ctl |= FIELD_PREP(GAUDI_PKT_CTL_MB_MASK, 1);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008416
8417 pkt->value = cpu_to_le32(value);
8418 pkt->ctl = cpu_to_le32(ctl);
Ofir Bitton2992c1d2020-09-10 09:40:35 +03008419
8420 return size + pkt_size;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008421}
8422
8423static u32 gaudi_add_mon_msg_short(struct packet_msg_short *pkt, u32 value,
8424 u16 addr)
8425{
8426 u32 ctl, pkt_size = sizeof(*pkt);
8427
8428 memset(pkt, 0, pkt_size);
8429
Ofir Bitton6c07bab2020-06-01 10:38:46 +03008430 ctl = FIELD_PREP(GAUDI_PKT_SHORT_CTL_ADDR_MASK, addr);
8431 ctl |= FIELD_PREP(GAUDI_PKT_SHORT_CTL_BASE_MASK, 2); /* W_S MON base */
Ofir Bittonf8b0f2e2020-12-06 10:22:32 +02008432 ctl |= FIELD_PREP(GAUDI_PKT_CTL_OPCODE_MASK, PACKET_MSG_SHORT);
8433 ctl |= FIELD_PREP(GAUDI_PKT_CTL_EB_MASK, 0);
8434 ctl |= FIELD_PREP(GAUDI_PKT_CTL_RB_MASK, 1);
8435 ctl |= FIELD_PREP(GAUDI_PKT_CTL_MB_MASK, 0); /* last pkt MB */
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008436
8437 pkt->value = cpu_to_le32(value);
8438 pkt->ctl = cpu_to_le32(ctl);
8439
8440 return pkt_size;
8441}
8442
Ofir Bitton2992c1d2020-09-10 09:40:35 +03008443static u32 gaudi_add_arm_monitor_pkt(struct hl_device *hdev,
8444 struct packet_msg_short *pkt, u16 sob_base, u8 sob_mask,
8445 u16 sob_val, u16 mon_id)
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008446{
Ofir Bitton2992c1d2020-09-10 09:40:35 +03008447 u64 monitor_base;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008448 u32 ctl, value, pkt_size = sizeof(*pkt);
Ofir Bitton2992c1d2020-09-10 09:40:35 +03008449 u16 msg_addr_offset;
8450 u8 mask;
8451
8452 if (hl_gen_sob_mask(sob_base, sob_mask, &mask)) {
8453 dev_err(hdev->dev,
8454 "sob_base %u (mask %#x) is not valid\n",
8455 sob_base, sob_mask);
8456 return 0;
8457 }
8458
8459 /*
8460 * monitor_base should be the content of the base0 address registers,
8461 * so it will be added to the msg short offsets
8462 */
8463 monitor_base = mmSYNC_MNGR_W_S_SYNC_MNGR_OBJS_MON_PAY_ADDRL_0;
8464
8465 msg_addr_offset =
8466 (mmSYNC_MNGR_W_S_SYNC_MNGR_OBJS_MON_ARM_0 + mon_id * 4) -
8467 monitor_base;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008468
8469 memset(pkt, 0, pkt_size);
8470
Ofir Bitton2992c1d2020-09-10 09:40:35 +03008471 /* Monitor config packet: bind the monitor to a sync object */
8472 value = FIELD_PREP(GAUDI_PKT_SHORT_VAL_MON_SYNC_GID_MASK, sob_base / 8);
Ofir Bitton6c07bab2020-06-01 10:38:46 +03008473 value |= FIELD_PREP(GAUDI_PKT_SHORT_VAL_MON_SYNC_VAL_MASK, sob_val);
8474 value |= FIELD_PREP(GAUDI_PKT_SHORT_VAL_MON_MODE_MASK,
8475 0); /* GREATER OR EQUAL*/
8476 value |= FIELD_PREP(GAUDI_PKT_SHORT_VAL_MON_MASK_MASK, mask);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008477
Ofir Bitton2992c1d2020-09-10 09:40:35 +03008478 ctl = FIELD_PREP(GAUDI_PKT_SHORT_CTL_ADDR_MASK, msg_addr_offset);
Ofir Bitton6c07bab2020-06-01 10:38:46 +03008479 ctl |= FIELD_PREP(GAUDI_PKT_SHORT_CTL_OP_MASK, 0); /* write the value */
8480 ctl |= FIELD_PREP(GAUDI_PKT_SHORT_CTL_BASE_MASK, 2); /* W_S MON base */
Ofir Bittonf8b0f2e2020-12-06 10:22:32 +02008481 ctl |= FIELD_PREP(GAUDI_PKT_CTL_OPCODE_MASK, PACKET_MSG_SHORT);
8482 ctl |= FIELD_PREP(GAUDI_PKT_CTL_EB_MASK, 0);
8483 ctl |= FIELD_PREP(GAUDI_PKT_CTL_RB_MASK, 1);
8484 ctl |= FIELD_PREP(GAUDI_PKT_CTL_MB_MASK, 1);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008485
8486 pkt->value = cpu_to_le32(value);
8487 pkt->ctl = cpu_to_le32(ctl);
8488
8489 return pkt_size;
8490}
8491
8492static u32 gaudi_add_fence_pkt(struct packet_fence *pkt)
8493{
8494 u32 ctl, cfg, pkt_size = sizeof(*pkt);
8495
8496 memset(pkt, 0, pkt_size);
8497
Ofir Bitton6c07bab2020-06-01 10:38:46 +03008498 cfg = FIELD_PREP(GAUDI_PKT_FENCE_CFG_DEC_VAL_MASK, 1);
8499 cfg |= FIELD_PREP(GAUDI_PKT_FENCE_CFG_TARGET_VAL_MASK, 1);
8500 cfg |= FIELD_PREP(GAUDI_PKT_FENCE_CFG_ID_MASK, 2);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008501
Ofir Bittonf8b0f2e2020-12-06 10:22:32 +02008502 ctl = FIELD_PREP(GAUDI_PKT_CTL_OPCODE_MASK, PACKET_FENCE);
8503 ctl |= FIELD_PREP(GAUDI_PKT_CTL_EB_MASK, 0);
8504 ctl |= FIELD_PREP(GAUDI_PKT_CTL_RB_MASK, 1);
8505 ctl |= FIELD_PREP(GAUDI_PKT_CTL_MB_MASK, 1);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008506
8507 pkt->cfg = cpu_to_le32(cfg);
8508 pkt->ctl = cpu_to_le32(ctl);
8509
8510 return pkt_size;
8511}
8512
Ofir Bitton2992c1d2020-09-10 09:40:35 +03008513static int gaudi_get_fence_addr(struct hl_device *hdev, u32 queue_id, u64 *addr)
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008514{
Ofir Bitton5de406c2020-09-10 10:56:26 +03008515 u32 offset, nic_index;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008516
Ofir Bitton2992c1d2020-09-10 09:40:35 +03008517 switch (queue_id) {
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008518 case GAUDI_QUEUE_ID_DMA_0_0:
Ofir Bitton2992c1d2020-09-10 09:40:35 +03008519 offset = mmDMA0_QM_CP_FENCE2_RDATA_0;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008520 break;
8521 case GAUDI_QUEUE_ID_DMA_0_1:
Ofir Bitton2992c1d2020-09-10 09:40:35 +03008522 offset = mmDMA0_QM_CP_FENCE2_RDATA_1;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008523 break;
8524 case GAUDI_QUEUE_ID_DMA_0_2:
Ofir Bitton2992c1d2020-09-10 09:40:35 +03008525 offset = mmDMA0_QM_CP_FENCE2_RDATA_2;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008526 break;
8527 case GAUDI_QUEUE_ID_DMA_0_3:
Ofir Bitton2992c1d2020-09-10 09:40:35 +03008528 offset = mmDMA0_QM_CP_FENCE2_RDATA_3;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008529 break;
8530 case GAUDI_QUEUE_ID_DMA_1_0:
Ofir Bitton2992c1d2020-09-10 09:40:35 +03008531 offset = mmDMA1_QM_CP_FENCE2_RDATA_0;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008532 break;
8533 case GAUDI_QUEUE_ID_DMA_1_1:
Ofir Bitton2992c1d2020-09-10 09:40:35 +03008534 offset = mmDMA1_QM_CP_FENCE2_RDATA_1;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008535 break;
8536 case GAUDI_QUEUE_ID_DMA_1_2:
Ofir Bitton2992c1d2020-09-10 09:40:35 +03008537 offset = mmDMA1_QM_CP_FENCE2_RDATA_2;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008538 break;
8539 case GAUDI_QUEUE_ID_DMA_1_3:
Ofir Bitton2992c1d2020-09-10 09:40:35 +03008540 offset = mmDMA1_QM_CP_FENCE2_RDATA_3;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008541 break;
8542 case GAUDI_QUEUE_ID_DMA_5_0:
Ofir Bitton2992c1d2020-09-10 09:40:35 +03008543 offset = mmDMA5_QM_CP_FENCE2_RDATA_0;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008544 break;
8545 case GAUDI_QUEUE_ID_DMA_5_1:
Ofir Bitton2992c1d2020-09-10 09:40:35 +03008546 offset = mmDMA5_QM_CP_FENCE2_RDATA_1;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008547 break;
8548 case GAUDI_QUEUE_ID_DMA_5_2:
Ofir Bitton2992c1d2020-09-10 09:40:35 +03008549 offset = mmDMA5_QM_CP_FENCE2_RDATA_2;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008550 break;
8551 case GAUDI_QUEUE_ID_DMA_5_3:
Ofir Bitton2992c1d2020-09-10 09:40:35 +03008552 offset = mmDMA5_QM_CP_FENCE2_RDATA_3;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008553 break;
Ofir Bitton5de406c2020-09-10 10:56:26 +03008554 case GAUDI_QUEUE_ID_TPC_7_0:
8555 offset = mmTPC7_QM_CP_FENCE2_RDATA_0;
8556 break;
8557 case GAUDI_QUEUE_ID_TPC_7_1:
8558 offset = mmTPC7_QM_CP_FENCE2_RDATA_1;
8559 break;
8560 case GAUDI_QUEUE_ID_TPC_7_2:
8561 offset = mmTPC7_QM_CP_FENCE2_RDATA_2;
8562 break;
8563 case GAUDI_QUEUE_ID_TPC_7_3:
8564 offset = mmTPC7_QM_CP_FENCE2_RDATA_3;
8565 break;
8566 case GAUDI_QUEUE_ID_NIC_0_0:
8567 case GAUDI_QUEUE_ID_NIC_1_0:
8568 case GAUDI_QUEUE_ID_NIC_2_0:
8569 case GAUDI_QUEUE_ID_NIC_3_0:
8570 case GAUDI_QUEUE_ID_NIC_4_0:
8571 case GAUDI_QUEUE_ID_NIC_5_0:
8572 case GAUDI_QUEUE_ID_NIC_6_0:
8573 case GAUDI_QUEUE_ID_NIC_7_0:
8574 case GAUDI_QUEUE_ID_NIC_8_0:
8575 case GAUDI_QUEUE_ID_NIC_9_0:
8576 nic_index = (queue_id - GAUDI_QUEUE_ID_NIC_0_0) >> 2;
8577 offset = mmNIC0_QM0_CP_FENCE2_RDATA_0 +
8578 (nic_index >> 1) * NIC_MACRO_QMAN_OFFSET +
8579 (nic_index & 0x1) * NIC_ENGINE_QMAN_OFFSET;
8580 break;
8581 case GAUDI_QUEUE_ID_NIC_0_1:
8582 case GAUDI_QUEUE_ID_NIC_1_1:
8583 case GAUDI_QUEUE_ID_NIC_2_1:
8584 case GAUDI_QUEUE_ID_NIC_3_1:
8585 case GAUDI_QUEUE_ID_NIC_4_1:
8586 case GAUDI_QUEUE_ID_NIC_5_1:
8587 case GAUDI_QUEUE_ID_NIC_6_1:
8588 case GAUDI_QUEUE_ID_NIC_7_1:
8589 case GAUDI_QUEUE_ID_NIC_8_1:
8590 case GAUDI_QUEUE_ID_NIC_9_1:
8591 nic_index = (queue_id - GAUDI_QUEUE_ID_NIC_0_1) >> 2;
8592 offset = mmNIC0_QM0_CP_FENCE2_RDATA_1 +
8593 (nic_index >> 1) * NIC_MACRO_QMAN_OFFSET +
8594 (nic_index & 0x1) * NIC_ENGINE_QMAN_OFFSET;
8595 break;
8596 case GAUDI_QUEUE_ID_NIC_0_2:
8597 case GAUDI_QUEUE_ID_NIC_1_2:
8598 case GAUDI_QUEUE_ID_NIC_2_2:
8599 case GAUDI_QUEUE_ID_NIC_3_2:
8600 case GAUDI_QUEUE_ID_NIC_4_2:
8601 case GAUDI_QUEUE_ID_NIC_5_2:
8602 case GAUDI_QUEUE_ID_NIC_6_2:
8603 case GAUDI_QUEUE_ID_NIC_7_2:
8604 case GAUDI_QUEUE_ID_NIC_8_2:
8605 case GAUDI_QUEUE_ID_NIC_9_2:
8606 nic_index = (queue_id - GAUDI_QUEUE_ID_NIC_0_2) >> 2;
8607 offset = mmNIC0_QM0_CP_FENCE2_RDATA_2 +
8608 (nic_index >> 1) * NIC_MACRO_QMAN_OFFSET +
8609 (nic_index & 0x1) * NIC_ENGINE_QMAN_OFFSET;
8610 break;
8611 case GAUDI_QUEUE_ID_NIC_0_3:
8612 case GAUDI_QUEUE_ID_NIC_1_3:
8613 case GAUDI_QUEUE_ID_NIC_2_3:
8614 case GAUDI_QUEUE_ID_NIC_3_3:
8615 case GAUDI_QUEUE_ID_NIC_4_3:
8616 case GAUDI_QUEUE_ID_NIC_5_3:
8617 case GAUDI_QUEUE_ID_NIC_6_3:
8618 case GAUDI_QUEUE_ID_NIC_7_3:
8619 case GAUDI_QUEUE_ID_NIC_8_3:
8620 case GAUDI_QUEUE_ID_NIC_9_3:
8621 nic_index = (queue_id - GAUDI_QUEUE_ID_NIC_0_3) >> 2;
8622 offset = mmNIC0_QM0_CP_FENCE2_RDATA_3 +
8623 (nic_index >> 1) * NIC_MACRO_QMAN_OFFSET +
8624 (nic_index & 0x1) * NIC_ENGINE_QMAN_OFFSET;
8625 break;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008626 default:
Ofir Bitton2992c1d2020-09-10 09:40:35 +03008627 return -EINVAL;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008628 }
8629
Ofir Bitton2992c1d2020-09-10 09:40:35 +03008630 *addr = CFG_BASE + offset;
8631
8632 return 0;
8633}
8634
8635static u32 gaudi_add_mon_pkts(void *buf, u16 mon_id, u64 fence_addr)
8636{
8637 u64 monitor_base;
8638 u32 size = 0;
8639 u16 msg_addr_offset;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008640
8641 /*
8642 * monitor_base should be the content of the base0 address registers,
8643 * so it will be added to the msg short offsets
8644 */
8645 monitor_base = mmSYNC_MNGR_W_S_SYNC_MNGR_OBJS_MON_PAY_ADDRL_0;
8646
8647 /* First monitor config packet: low address of the sync */
Ofir Bitton2992c1d2020-09-10 09:40:35 +03008648 msg_addr_offset =
8649 (mmSYNC_MNGR_W_S_SYNC_MNGR_OBJS_MON_PAY_ADDRL_0 + mon_id * 4) -
8650 monitor_base;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008651
8652 size += gaudi_add_mon_msg_short(buf + size, (u32) fence_addr,
8653 msg_addr_offset);
8654
8655 /* Second monitor config packet: high address of the sync */
Ofir Bitton2992c1d2020-09-10 09:40:35 +03008656 msg_addr_offset =
8657 (mmSYNC_MNGR_W_S_SYNC_MNGR_OBJS_MON_PAY_ADDRH_0 + mon_id * 4) -
8658 monitor_base;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008659
8660 size += gaudi_add_mon_msg_short(buf + size, (u32) (fence_addr >> 32),
8661 msg_addr_offset);
8662
8663 /*
8664 * Third monitor config packet: the payload, i.e. what to write when the
8665 * sync triggers
8666 */
Ofir Bitton2992c1d2020-09-10 09:40:35 +03008667 msg_addr_offset =
8668 (mmSYNC_MNGR_W_S_SYNC_MNGR_OBJS_MON_PAY_DATA_0 + mon_id * 4) -
8669 monitor_base;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008670
8671 size += gaudi_add_mon_msg_short(buf + size, 1, msg_addr_offset);
8672
Ofir Bitton2992c1d2020-09-10 09:40:35 +03008673 return size;
8674}
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008675
Oded Gabbay3c681572020-11-02 21:10:39 +02008676static u32 gaudi_gen_wait_cb(struct hl_device *hdev,
8677 struct hl_gen_wait_properties *prop)
Ofir Bitton2992c1d2020-09-10 09:40:35 +03008678{
8679 struct hl_cb *cb = (struct hl_cb *) prop->data;
8680 void *buf = cb->kernel_address;
8681 u64 fence_addr = 0;
8682 u32 size = prop->size;
8683
8684 if (gaudi_get_fence_addr(hdev, prop->q_idx, &fence_addr)) {
8685 dev_crit(hdev->dev, "wrong queue id %d for wait packet\n",
8686 prop->q_idx);
8687 return 0;
8688 }
8689
8690 size += gaudi_add_mon_pkts(buf + size, prop->mon_id, fence_addr);
8691 size += gaudi_add_arm_monitor_pkt(hdev, buf + size, prop->sob_base,
8692 prop->sob_mask, prop->sob_val, prop->mon_id);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008693 size += gaudi_add_fence_pkt(buf + size);
Ofir Bitton2992c1d2020-09-10 09:40:35 +03008694
8695 return size;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008696}
8697
8698static void gaudi_reset_sob(struct hl_device *hdev, void *data)
8699{
8700 struct hl_hw_sob *hw_sob = (struct hl_hw_sob *) data;
Ofir Bitton423815b2021-01-05 09:04:07 +02008701 int rc;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008702
8703 dev_dbg(hdev->dev, "reset SOB, q_idx: %d, sob_id: %d\n", hw_sob->q_idx,
8704 hw_sob->sob_id);
8705
Ofir Bitton423815b2021-01-05 09:04:07 +02008706 rc = gaudi_schedule_register_memset(hdev, hw_sob->q_idx,
8707 CFG_BASE + mmSYNC_MNGR_W_S_SYNC_MNGR_OBJS_SOB_OBJ_0 +
8708 hw_sob->sob_id * 4, 1, 0);
8709 if (rc)
8710 dev_err(hdev->dev, "failed resetting sob %u", hw_sob->sob_id);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008711
8712 kref_init(&hw_sob->kref);
8713}
8714
8715static void gaudi_set_dma_mask_from_fw(struct hl_device *hdev)
8716{
8717 if (RREG32(mmPSOC_GLOBAL_CONF_NON_RST_FLOPS_0) ==
8718 HL_POWER9_HOST_MAGIC) {
8719 hdev->power9_64bit_dma_enable = 1;
8720 hdev->dma_mask = 64;
8721 } else {
8722 hdev->power9_64bit_dma_enable = 0;
8723 hdev->dma_mask = 48;
8724 }
8725}
8726
8727static u64 gaudi_get_device_time(struct hl_device *hdev)
8728{
8729 u64 device_time = ((u64) RREG32(mmPSOC_TIMESTAMP_CNTCVU)) << 32;
8730
8731 return device_time | RREG32(mmPSOC_TIMESTAMP_CNTCVL);
8732}
8733
Ofir Bittond00697f2021-01-05 12:55:06 +02008734static int gaudi_get_hw_block_id(struct hl_device *hdev, u64 block_addr,
Oded Gabbay6df50d22021-02-05 16:04:34 +02008735 u32 *block_size, u32 *block_id)
Ofir Bittond00697f2021-01-05 12:55:06 +02008736{
8737 return -EPERM;
8738}
8739
8740static int gaudi_block_mmap(struct hl_device *hdev,
8741 struct vm_area_struct *vma,
8742 u32 block_id, u32 block_size)
8743{
8744 return -EPERM;
8745}
8746
Oded Gabbay28bcf1f2021-02-01 21:23:43 +02008747static void gaudi_enable_events_from_fw(struct hl_device *hdev)
8748{
8749 WREG32(mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR, GAUDI_EVENT_INTS_REGISTER);
8750}
8751
Bharat Jauhari285c0fa2021-03-25 18:15:40 +02008752static int gaudi_map_pll_idx_to_fw_idx(u32 pll_idx)
8753{
8754 switch (pll_idx) {
8755 case HL_GAUDI_CPU_PLL: return CPU_PLL;
8756 case HL_GAUDI_PCI_PLL: return PCI_PLL;
8757 case HL_GAUDI_NIC_PLL: return NIC_PLL;
8758 case HL_GAUDI_DMA_PLL: return DMA_PLL;
8759 case HL_GAUDI_MESH_PLL: return MESH_PLL;
8760 case HL_GAUDI_MME_PLL: return MME_PLL;
8761 case HL_GAUDI_TPC_PLL: return TPC_PLL;
8762 case HL_GAUDI_IF_PLL: return IF_PLL;
8763 case HL_GAUDI_SRAM_PLL: return SRAM_PLL;
8764 case HL_GAUDI_HBM_PLL: return HBM_PLL;
8765 default: return -EINVAL;
8766 }
8767}
8768
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008769static const struct hl_asic_funcs gaudi_funcs = {
8770 .early_init = gaudi_early_init,
8771 .early_fini = gaudi_early_fini,
8772 .late_init = gaudi_late_init,
8773 .late_fini = gaudi_late_fini,
8774 .sw_init = gaudi_sw_init,
8775 .sw_fini = gaudi_sw_fini,
8776 .hw_init = gaudi_hw_init,
8777 .hw_fini = gaudi_hw_fini,
8778 .halt_engines = gaudi_halt_engines,
8779 .suspend = gaudi_suspend,
8780 .resume = gaudi_resume,
8781 .cb_mmap = gaudi_cb_mmap,
8782 .ring_doorbell = gaudi_ring_doorbell,
8783 .pqe_write = gaudi_pqe_write,
8784 .asic_dma_alloc_coherent = gaudi_dma_alloc_coherent,
8785 .asic_dma_free_coherent = gaudi_dma_free_coherent,
farah kassabri03df1362020-05-06 11:17:38 +03008786 .scrub_device_mem = gaudi_scrub_device_mem,
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008787 .get_int_queue_base = gaudi_get_int_queue_base,
8788 .test_queues = gaudi_test_queues,
8789 .asic_dma_pool_zalloc = gaudi_dma_pool_zalloc,
8790 .asic_dma_pool_free = gaudi_dma_pool_free,
8791 .cpu_accessible_dma_pool_alloc = gaudi_cpu_accessible_dma_pool_alloc,
8792 .cpu_accessible_dma_pool_free = gaudi_cpu_accessible_dma_pool_free,
8793 .hl_dma_unmap_sg = gaudi_dma_unmap_sg,
8794 .cs_parser = gaudi_cs_parser,
8795 .asic_dma_map_sg = gaudi_dma_map_sg,
8796 .get_dma_desc_list_size = gaudi_get_dma_desc_list_size,
8797 .add_end_of_cb_packets = gaudi_add_end_of_cb_packets,
8798 .update_eq_ci = gaudi_update_eq_ci,
8799 .context_switch = gaudi_context_switch,
8800 .restore_phase_topology = gaudi_restore_phase_topology,
8801 .debugfs_read32 = gaudi_debugfs_read32,
8802 .debugfs_write32 = gaudi_debugfs_write32,
8803 .debugfs_read64 = gaudi_debugfs_read64,
8804 .debugfs_write64 = gaudi_debugfs_write64,
Oded Gabbay639781d2021-04-02 01:43:18 +03008805 .debugfs_read_dma = gaudi_debugfs_read_dma,
Oded Gabbaybcaf4152020-05-11 10:41:37 +03008806 .add_device_attr = gaudi_add_device_attr,
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008807 .handle_eqe = gaudi_handle_eqe,
Oded Gabbaybcaf4152020-05-11 10:41:37 +03008808 .set_pll_profile = gaudi_set_pll_profile,
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008809 .get_events_stat = gaudi_get_events_stat,
8810 .read_pte = gaudi_read_pte,
8811 .write_pte = gaudi_write_pte,
8812 .mmu_invalidate_cache = gaudi_mmu_invalidate_cache,
8813 .mmu_invalidate_cache_range = gaudi_mmu_invalidate_cache_range,
8814 .send_heartbeat = gaudi_send_heartbeat,
Oded Gabbaye38bfd32020-07-03 20:46:12 +03008815 .set_clock_gating = gaudi_set_clock_gating,
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008816 .disable_clock_gating = gaudi_disable_clock_gating,
Omer Shpigelman79fc7a92020-05-11 10:46:29 +03008817 .debug_coresight = gaudi_debug_coresight,
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008818 .is_device_idle = gaudi_is_device_idle,
8819 .soft_reset_late_init = gaudi_soft_reset_late_init,
8820 .hw_queues_lock = gaudi_hw_queues_lock,
8821 .hw_queues_unlock = gaudi_hw_queues_unlock,
8822 .get_pci_id = gaudi_get_pci_id,
8823 .get_eeprom_data = gaudi_get_eeprom_data,
8824 .send_cpu_message = gaudi_send_cpu_message,
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008825 .pci_bars_map = gaudi_pci_bars_map,
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008826 .init_iatu = gaudi_init_iatu,
8827 .rreg = hl_rreg,
8828 .wreg = hl_wreg,
Omer Shpigelman79fc7a92020-05-11 10:46:29 +03008829 .halt_coresight = gaudi_halt_coresight,
Ofir Bittona04b7cd2020-07-13 13:36:55 +03008830 .ctx_init = gaudi_ctx_init,
Ofir Bitton5de406c2020-09-10 10:56:26 +03008831 .ctx_fini = gaudi_ctx_fini,
Oded Gabbaybcaf4152020-05-11 10:41:37 +03008832 .get_clk_rate = gaudi_get_clk_rate,
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008833 .get_queue_id_for_cq = gaudi_get_queue_id_for_cq,
8834 .read_device_fw_version = gaudi_read_device_fw_version,
8835 .load_firmware_to_device = gaudi_load_firmware_to_device,
8836 .load_boot_fit_to_device = gaudi_load_boot_fit_to_device,
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008837 .get_signal_cb_size = gaudi_get_signal_cb_size,
8838 .get_wait_cb_size = gaudi_get_wait_cb_size,
8839 .gen_signal_cb = gaudi_gen_signal_cb,
8840 .gen_wait_cb = gaudi_gen_wait_cb,
8841 .reset_sob = gaudi_reset_sob,
Ofir Bitton5fe1c172020-09-10 10:10:55 +03008842 .reset_sob_group = gaudi_reset_sob_group,
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008843 .set_dma_mask_from_fw = gaudi_set_dma_mask_from_fw,
Ofir Bitton5fe1c172020-09-10 10:10:55 +03008844 .get_device_time = gaudi_get_device_time,
8845 .collective_wait_init_cs = gaudi_collective_wait_init_cs,
Moti Haimovskib19dc672020-11-18 20:15:29 +02008846 .collective_wait_create_jobs = gaudi_collective_wait_create_jobs,
farah kassabri89473a12021-01-12 17:24:00 +02008847 .scramble_addr = hl_mmu_scramble_addr,
8848 .descramble_addr = hl_mmu_descramble_addr,
Ofir Bittond00697f2021-01-05 12:55:06 +02008849 .ack_protection_bits_errors = gaudi_ack_protection_bits_errors,
8850 .get_hw_block_id = gaudi_get_hw_block_id,
Oded Gabbay28bcf1f2021-02-01 21:23:43 +02008851 .hw_block_mmap = gaudi_block_mmap,
Bharat Jauhari285c0fa2021-03-25 18:15:40 +02008852 .enable_events_from_fw = gaudi_enable_events_from_fw,
8853 .map_pll_idx_to_fw_idx = gaudi_map_pll_idx_to_fw_idx
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008854};
8855
8856/**
8857 * gaudi_set_asic_funcs - set GAUDI function pointers
8858 *
Lee Jonesf7d227c2020-07-01 09:58:42 +01008859 * @hdev: pointer to hl_device structure
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008860 *
8861 */
8862void gaudi_set_asic_funcs(struct hl_device *hdev)
8863{
8864 hdev->asic_funcs = &gaudi_funcs;
8865}