1 // SPDX-License-Identifier: GPL-2.0
2
3 /*
4 * Copyright 2016-2019 HabanaLabs, Ltd.
5 * All Rights Reserved.
6 */
7
8 #include "goyaP.h"
9 #include "include/hw_ip/mmu/mmu_general.h"
10 #include "include/hw_ip/mmu/mmu_v1_0.h"
11 #include "include/goya/asic_reg/goya_masks.h"
12 #include "include/goya/goya_reg_map.h"
13
14 #include <linux/pci.h>
15 #include <linux/genalloc.h>
16 #include <linux/hwmon.h>
17 #include <linux/io-64-nonatomic-lo-hi.h>
18 #include <linux/iommu.h>
19 #include <linux/seq_file.h>
20
21 /*
22 * GOYA security scheme:
23 *
24 * 1. Host is protected by:
25 * - Range registers (When MMU is enabled, DMA RR does NOT protect host)
26 * - MMU
27 *
28 * 2. DRAM is protected by:
29 * - Range registers (protect the first 512MB)
30 * - MMU (isolation between users)
31 *
32 * 3. Configuration is protected by:
33 * - Range registers
34 * - Protection bits
35 *
36 * When MMU is disabled:
37 *
38 * QMAN DMA: PQ, CQ, CP, DMA are secured.
39 * PQ, CB and the data are on the host.
40 *
41 * QMAN TPC/MME:
42 * PQ, CQ and CP are not secured.
43 * PQ, CB and the data are on the SRAM/DRAM.
44 *
45 * Since QMAN DMA is secured, the driver is parsing the DMA CB:
46 * - checks DMA pointer
47 * - WREG, MSG_PROT are not allowed.
48 * - MSG_LONG/SHORT are allowed.
49 *
50 * A read/write transaction by the QMAN to a protected area will succeed if
51 * and only if the QMAN's CP is secured and MSG_PROT is used
52 *
53 *
54 * When MMU is enabled:
55 *
56 * QMAN DMA: PQ, CQ and CP are secured.
57 * MMU is set to bypass on the Secure props register of the QMAN.
58 * The reasons we don't enable MMU for PQ, CQ and CP are:
59 * - PQ entry is in kernel address space and the driver doesn't map it.
60 * - CP writes to MSIX register and to kernel address space (completion
61 * queue).
62 *
63 * DMA is not secured but because CP is secured, the driver still needs to parse
64 * the CB, but doesn't need to check the DMA addresses.
65 *
66 * For QMAN DMA 0, DMA is also secured because only the driver uses this DMA and
67 * the driver doesn't map memory in MMU.
68 *
69 * QMAN TPC/MME: PQ, CQ and CP aren't secured (no change from MMU disabled mode)
70 *
71 * DMA RR does NOT protect host because DMA is not secured
72 *
73 */
74
75 #define GOYA_MMU_REGS_NUM 63
76
77 #define GOYA_DMA_POOL_BLK_SIZE 0x100 /* 256 bytes */
78
79 #define GOYA_RESET_TIMEOUT_MSEC 500 /* 500ms */
80 #define GOYA_PLDM_RESET_TIMEOUT_MSEC 20000 /* 20s */
81 #define GOYA_RESET_WAIT_MSEC 1 /* 1ms */
82 #define GOYA_CPU_RESET_WAIT_MSEC 100 /* 100ms */
83 #define GOYA_PLDM_RESET_WAIT_MSEC 1000 /* 1s */
84 #define GOYA_TEST_QUEUE_WAIT_USEC 100000 /* 100ms */
85 #define GOYA_PLDM_MMU_TIMEOUT_USEC (MMU_CONFIG_TIMEOUT_USEC * 100)
86 #define GOYA_PLDM_QMAN0_TIMEOUT_USEC (HL_DEVICE_TIMEOUT_USEC * 30)
87
88 #define GOYA_QMAN0_FENCE_VAL 0xD169B243
89
90 #define GOYA_MAX_STRING_LEN 20
91
92 #define GOYA_CB_POOL_CB_CNT 512
93 #define GOYA_CB_POOL_CB_SIZE 0x20000 /* 128KB */
94
95 #define IS_QM_IDLE(engine, qm_glbl_sts0) \
96 (((qm_glbl_sts0) & engine##_QM_IDLE_MASK) == engine##_QM_IDLE_MASK)
97 #define IS_DMA_QM_IDLE(qm_glbl_sts0) IS_QM_IDLE(DMA, qm_glbl_sts0)
98 #define IS_TPC_QM_IDLE(qm_glbl_sts0) IS_QM_IDLE(TPC, qm_glbl_sts0)
99 #define IS_MME_QM_IDLE(qm_glbl_sts0) IS_QM_IDLE(MME, qm_glbl_sts0)
100
101 #define IS_CMDQ_IDLE(engine, cmdq_glbl_sts0) \
102 (((cmdq_glbl_sts0) & engine##_CMDQ_IDLE_MASK) == \
103 engine##_CMDQ_IDLE_MASK)
104 #define IS_TPC_CMDQ_IDLE(cmdq_glbl_sts0) \
105 IS_CMDQ_IDLE(TPC, cmdq_glbl_sts0)
106 #define IS_MME_CMDQ_IDLE(cmdq_glbl_sts0) \
107 IS_CMDQ_IDLE(MME, cmdq_glbl_sts0)
108
109 #define IS_DMA_IDLE(dma_core_sts0) \
110 !((dma_core_sts0) & DMA_CH_0_STS0_DMA_BUSY_MASK)
111
112 #define IS_TPC_IDLE(tpc_cfg_sts) \
113 (((tpc_cfg_sts) & TPC_CFG_IDLE_MASK) == TPC_CFG_IDLE_MASK)
114
115 #define IS_MME_IDLE(mme_arch_sts) \
116 (((mme_arch_sts) & MME_ARCH_IDLE_MASK) == MME_ARCH_IDLE_MASK)
117
118
119 static const char goya_irq_name[GOYA_MSIX_ENTRIES][GOYA_MAX_STRING_LEN] = {
120 "goya cq 0", "goya cq 1", "goya cq 2", "goya cq 3",
121 "goya cq 4", "goya cpu eq"
122 };
123
124 static u16 goya_packet_sizes[MAX_PACKET_ID] = {
125 [PACKET_WREG_32] = sizeof(struct packet_wreg32),
126 [PACKET_WREG_BULK] = sizeof(struct packet_wreg_bulk),
127 [PACKET_MSG_LONG] = sizeof(struct packet_msg_long),
128 [PACKET_MSG_SHORT] = sizeof(struct packet_msg_short),
129 [PACKET_CP_DMA] = sizeof(struct packet_cp_dma),
130 [PACKET_MSG_PROT] = sizeof(struct packet_msg_prot),
131 [PACKET_FENCE] = sizeof(struct packet_fence),
132 [PACKET_LIN_DMA] = sizeof(struct packet_lin_dma),
133 [PACKET_NOP] = sizeof(struct packet_nop),
134 [PACKET_STOP] = sizeof(struct packet_stop)
135 };
136
137 static u64 goya_mmu_regs[GOYA_MMU_REGS_NUM] = {
138 mmDMA_QM_0_GLBL_NON_SECURE_PROPS,
139 mmDMA_QM_1_GLBL_NON_SECURE_PROPS,
140 mmDMA_QM_2_GLBL_NON_SECURE_PROPS,
141 mmDMA_QM_3_GLBL_NON_SECURE_PROPS,
142 mmDMA_QM_4_GLBL_NON_SECURE_PROPS,
143 mmTPC0_QM_GLBL_SECURE_PROPS,
144 mmTPC0_QM_GLBL_NON_SECURE_PROPS,
145 mmTPC0_CMDQ_GLBL_SECURE_PROPS,
146 mmTPC0_CMDQ_GLBL_NON_SECURE_PROPS,
147 mmTPC0_CFG_ARUSER,
148 mmTPC0_CFG_AWUSER,
149 mmTPC1_QM_GLBL_SECURE_PROPS,
150 mmTPC1_QM_GLBL_NON_SECURE_PROPS,
151 mmTPC1_CMDQ_GLBL_SECURE_PROPS,
152 mmTPC1_CMDQ_GLBL_NON_SECURE_PROPS,
153 mmTPC1_CFG_ARUSER,
154 mmTPC1_CFG_AWUSER,
155 mmTPC2_QM_GLBL_SECURE_PROPS,
156 mmTPC2_QM_GLBL_NON_SECURE_PROPS,
157 mmTPC2_CMDQ_GLBL_SECURE_PROPS,
158 mmTPC2_CMDQ_GLBL_NON_SECURE_PROPS,
159 mmTPC2_CFG_ARUSER,
160 mmTPC2_CFG_AWUSER,
161 mmTPC3_QM_GLBL_SECURE_PROPS,
162 mmTPC3_QM_GLBL_NON_SECURE_PROPS,
163 mmTPC3_CMDQ_GLBL_SECURE_PROPS,
164 mmTPC3_CMDQ_GLBL_NON_SECURE_PROPS,
165 mmTPC3_CFG_ARUSER,
166 mmTPC3_CFG_AWUSER,
167 mmTPC4_QM_GLBL_SECURE_PROPS,
168 mmTPC4_QM_GLBL_NON_SECURE_PROPS,
169 mmTPC4_CMDQ_GLBL_SECURE_PROPS,
170 mmTPC4_CMDQ_GLBL_NON_SECURE_PROPS,
171 mmTPC4_CFG_ARUSER,
172 mmTPC4_CFG_AWUSER,
173 mmTPC5_QM_GLBL_SECURE_PROPS,
174 mmTPC5_QM_GLBL_NON_SECURE_PROPS,
175 mmTPC5_CMDQ_GLBL_SECURE_PROPS,
176 mmTPC5_CMDQ_GLBL_NON_SECURE_PROPS,
177 mmTPC5_CFG_ARUSER,
178 mmTPC5_CFG_AWUSER,
179 mmTPC6_QM_GLBL_SECURE_PROPS,
180 mmTPC6_QM_GLBL_NON_SECURE_PROPS,
181 mmTPC6_CMDQ_GLBL_SECURE_PROPS,
182 mmTPC6_CMDQ_GLBL_NON_SECURE_PROPS,
183 mmTPC6_CFG_ARUSER,
184 mmTPC6_CFG_AWUSER,
185 mmTPC7_QM_GLBL_SECURE_PROPS,
186 mmTPC7_QM_GLBL_NON_SECURE_PROPS,
187 mmTPC7_CMDQ_GLBL_SECURE_PROPS,
188 mmTPC7_CMDQ_GLBL_NON_SECURE_PROPS,
189 mmTPC7_CFG_ARUSER,
190 mmTPC7_CFG_AWUSER,
191 mmMME_QM_GLBL_SECURE_PROPS,
192 mmMME_QM_GLBL_NON_SECURE_PROPS,
193 mmMME_CMDQ_GLBL_SECURE_PROPS,
194 mmMME_CMDQ_GLBL_NON_SECURE_PROPS,
195 mmMME_SBA_CONTROL_DATA,
196 mmMME_SBB_CONTROL_DATA,
197 mmMME_SBC_CONTROL_DATA,
198 mmMME_WBC_CONTROL_DATA,
199 mmPCIE_WRAP_PSOC_ARUSER,
200 mmPCIE_WRAP_PSOC_AWUSER
201 };
202
203 static u32 goya_all_events[] = {
204 GOYA_ASYNC_EVENT_ID_PCIE_IF,
205 GOYA_ASYNC_EVENT_ID_TPC0_ECC,
206 GOYA_ASYNC_EVENT_ID_TPC1_ECC,
207 GOYA_ASYNC_EVENT_ID_TPC2_ECC,
208 GOYA_ASYNC_EVENT_ID_TPC3_ECC,
209 GOYA_ASYNC_EVENT_ID_TPC4_ECC,
210 GOYA_ASYNC_EVENT_ID_TPC5_ECC,
211 GOYA_ASYNC_EVENT_ID_TPC6_ECC,
212 GOYA_ASYNC_EVENT_ID_TPC7_ECC,
213 GOYA_ASYNC_EVENT_ID_MME_ECC,
214 GOYA_ASYNC_EVENT_ID_MME_ECC_EXT,
215 GOYA_ASYNC_EVENT_ID_MMU_ECC,
216 GOYA_ASYNC_EVENT_ID_DMA_MACRO,
217 GOYA_ASYNC_EVENT_ID_DMA_ECC,
218 GOYA_ASYNC_EVENT_ID_CPU_IF_ECC,
219 GOYA_ASYNC_EVENT_ID_PSOC_MEM,
220 GOYA_ASYNC_EVENT_ID_PSOC_CORESIGHT,
221 GOYA_ASYNC_EVENT_ID_SRAM0,
222 GOYA_ASYNC_EVENT_ID_SRAM1,
223 GOYA_ASYNC_EVENT_ID_SRAM2,
224 GOYA_ASYNC_EVENT_ID_SRAM3,
225 GOYA_ASYNC_EVENT_ID_SRAM4,
226 GOYA_ASYNC_EVENT_ID_SRAM5,
227 GOYA_ASYNC_EVENT_ID_SRAM6,
228 GOYA_ASYNC_EVENT_ID_SRAM7,
229 GOYA_ASYNC_EVENT_ID_SRAM8,
230 GOYA_ASYNC_EVENT_ID_SRAM9,
231 GOYA_ASYNC_EVENT_ID_SRAM10,
232 GOYA_ASYNC_EVENT_ID_SRAM11,
233 GOYA_ASYNC_EVENT_ID_SRAM12,
234 GOYA_ASYNC_EVENT_ID_SRAM13,
235 GOYA_ASYNC_EVENT_ID_SRAM14,
236 GOYA_ASYNC_EVENT_ID_SRAM15,
237 GOYA_ASYNC_EVENT_ID_SRAM16,
238 GOYA_ASYNC_EVENT_ID_SRAM17,
239 GOYA_ASYNC_EVENT_ID_SRAM18,
240 GOYA_ASYNC_EVENT_ID_SRAM19,
241 GOYA_ASYNC_EVENT_ID_SRAM20,
242 GOYA_ASYNC_EVENT_ID_SRAM21,
243 GOYA_ASYNC_EVENT_ID_SRAM22,
244 GOYA_ASYNC_EVENT_ID_SRAM23,
245 GOYA_ASYNC_EVENT_ID_SRAM24,
246 GOYA_ASYNC_EVENT_ID_SRAM25,
247 GOYA_ASYNC_EVENT_ID_SRAM26,
248 GOYA_ASYNC_EVENT_ID_SRAM27,
249 GOYA_ASYNC_EVENT_ID_SRAM28,
250 GOYA_ASYNC_EVENT_ID_SRAM29,
251 GOYA_ASYNC_EVENT_ID_GIC500,
252 GOYA_ASYNC_EVENT_ID_PLL0,
253 GOYA_ASYNC_EVENT_ID_PLL1,
254 GOYA_ASYNC_EVENT_ID_PLL3,
255 GOYA_ASYNC_EVENT_ID_PLL4,
256 GOYA_ASYNC_EVENT_ID_PLL5,
257 GOYA_ASYNC_EVENT_ID_PLL6,
258 GOYA_ASYNC_EVENT_ID_AXI_ECC,
259 GOYA_ASYNC_EVENT_ID_L2_RAM_ECC,
260 GOYA_ASYNC_EVENT_ID_PSOC_GPIO_05_SW_RESET,
261 GOYA_ASYNC_EVENT_ID_PSOC_GPIO_10_VRHOT_ICRIT,
262 GOYA_ASYNC_EVENT_ID_PCIE_DEC,
263 GOYA_ASYNC_EVENT_ID_TPC0_DEC,
264 GOYA_ASYNC_EVENT_ID_TPC1_DEC,
265 GOYA_ASYNC_EVENT_ID_TPC2_DEC,
266 GOYA_ASYNC_EVENT_ID_TPC3_DEC,
267 GOYA_ASYNC_EVENT_ID_TPC4_DEC,
268 GOYA_ASYNC_EVENT_ID_TPC5_DEC,
269 GOYA_ASYNC_EVENT_ID_TPC6_DEC,
270 GOYA_ASYNC_EVENT_ID_TPC7_DEC,
271 GOYA_ASYNC_EVENT_ID_MME_WACS,
272 GOYA_ASYNC_EVENT_ID_MME_WACSD,
273 GOYA_ASYNC_EVENT_ID_CPU_AXI_SPLITTER,
274 GOYA_ASYNC_EVENT_ID_PSOC_AXI_DEC,
275 GOYA_ASYNC_EVENT_ID_PSOC,
276 GOYA_ASYNC_EVENT_ID_TPC0_KRN_ERR,
277 GOYA_ASYNC_EVENT_ID_TPC1_KRN_ERR,
278 GOYA_ASYNC_EVENT_ID_TPC2_KRN_ERR,
279 GOYA_ASYNC_EVENT_ID_TPC3_KRN_ERR,
280 GOYA_ASYNC_EVENT_ID_TPC4_KRN_ERR,
281 GOYA_ASYNC_EVENT_ID_TPC5_KRN_ERR,
282 GOYA_ASYNC_EVENT_ID_TPC6_KRN_ERR,
283 GOYA_ASYNC_EVENT_ID_TPC7_KRN_ERR,
284 GOYA_ASYNC_EVENT_ID_TPC0_CMDQ,
285 GOYA_ASYNC_EVENT_ID_TPC1_CMDQ,
286 GOYA_ASYNC_EVENT_ID_TPC2_CMDQ,
287 GOYA_ASYNC_EVENT_ID_TPC3_CMDQ,
288 GOYA_ASYNC_EVENT_ID_TPC4_CMDQ,
289 GOYA_ASYNC_EVENT_ID_TPC5_CMDQ,
290 GOYA_ASYNC_EVENT_ID_TPC6_CMDQ,
291 GOYA_ASYNC_EVENT_ID_TPC7_CMDQ,
292 GOYA_ASYNC_EVENT_ID_TPC0_QM,
293 GOYA_ASYNC_EVENT_ID_TPC1_QM,
294 GOYA_ASYNC_EVENT_ID_TPC2_QM,
295 GOYA_ASYNC_EVENT_ID_TPC3_QM,
296 GOYA_ASYNC_EVENT_ID_TPC4_QM,
297 GOYA_ASYNC_EVENT_ID_TPC5_QM,
298 GOYA_ASYNC_EVENT_ID_TPC6_QM,
299 GOYA_ASYNC_EVENT_ID_TPC7_QM,
300 GOYA_ASYNC_EVENT_ID_MME_QM,
301 GOYA_ASYNC_EVENT_ID_MME_CMDQ,
302 GOYA_ASYNC_EVENT_ID_DMA0_QM,
303 GOYA_ASYNC_EVENT_ID_DMA1_QM,
304 GOYA_ASYNC_EVENT_ID_DMA2_QM,
305 GOYA_ASYNC_EVENT_ID_DMA3_QM,
306 GOYA_ASYNC_EVENT_ID_DMA4_QM,
307 GOYA_ASYNC_EVENT_ID_DMA0_CH,
308 GOYA_ASYNC_EVENT_ID_DMA1_CH,
309 GOYA_ASYNC_EVENT_ID_DMA2_CH,
310 GOYA_ASYNC_EVENT_ID_DMA3_CH,
311 GOYA_ASYNC_EVENT_ID_DMA4_CH,
312 GOYA_ASYNC_EVENT_ID_TPC0_BMON_SPMU,
313 GOYA_ASYNC_EVENT_ID_TPC1_BMON_SPMU,
314 GOYA_ASYNC_EVENT_ID_TPC2_BMON_SPMU,
315 GOYA_ASYNC_EVENT_ID_TPC3_BMON_SPMU,
316 GOYA_ASYNC_EVENT_ID_TPC4_BMON_SPMU,
317 GOYA_ASYNC_EVENT_ID_TPC5_BMON_SPMU,
318 GOYA_ASYNC_EVENT_ID_TPC6_BMON_SPMU,
319 GOYA_ASYNC_EVENT_ID_TPC7_BMON_SPMU,
320 GOYA_ASYNC_EVENT_ID_DMA_BM_CH0,
321 GOYA_ASYNC_EVENT_ID_DMA_BM_CH1,
322 GOYA_ASYNC_EVENT_ID_DMA_BM_CH2,
323 GOYA_ASYNC_EVENT_ID_DMA_BM_CH3,
324 GOYA_ASYNC_EVENT_ID_DMA_BM_CH4
325 };
326
327 static int goya_mmu_clear_pgt_range(struct hl_device *hdev);
328 static int goya_mmu_set_dram_default_page(struct hl_device *hdev);
329 static int goya_mmu_add_mappings_for_device_cpu(struct hl_device *hdev);
330 static void goya_mmu_prepare(struct hl_device *hdev, u32 asid);
331
goya_get_fixed_properties(struct hl_device * hdev)332 void goya_get_fixed_properties(struct hl_device *hdev)
333 {
334 struct asic_fixed_properties *prop = &hdev->asic_prop;
335 int i;
336
337 for (i = 0 ; i < NUMBER_OF_EXT_HW_QUEUES ; i++) {
338 prop->hw_queues_props[i].type = QUEUE_TYPE_EXT;
339 prop->hw_queues_props[i].driver_only = 0;
340 }
341
342 for (; i < NUMBER_OF_EXT_HW_QUEUES + NUMBER_OF_CPU_HW_QUEUES ; i++) {
343 prop->hw_queues_props[i].type = QUEUE_TYPE_CPU;
344 prop->hw_queues_props[i].driver_only = 1;
345 }
346
347 for (; i < NUMBER_OF_EXT_HW_QUEUES + NUMBER_OF_CPU_HW_QUEUES +
348 NUMBER_OF_INT_HW_QUEUES; i++) {
349 prop->hw_queues_props[i].type = QUEUE_TYPE_INT;
350 prop->hw_queues_props[i].driver_only = 0;
351 }
352
353 for (; i < HL_MAX_QUEUES; i++)
354 prop->hw_queues_props[i].type = QUEUE_TYPE_NA;
355
356 prop->completion_queues_count = NUMBER_OF_CMPLT_QUEUES;
357
358 prop->dram_base_address = DRAM_PHYS_BASE;
359 prop->dram_size = DRAM_PHYS_DEFAULT_SIZE;
360 prop->dram_end_address = prop->dram_base_address + prop->dram_size;
361 prop->dram_user_base_address = DRAM_BASE_ADDR_USER;
362
363 prop->sram_base_address = SRAM_BASE_ADDR;
364 prop->sram_size = SRAM_SIZE;
365 prop->sram_end_address = prop->sram_base_address + prop->sram_size;
366 prop->sram_user_base_address = prop->sram_base_address +
367 SRAM_USER_BASE_OFFSET;
368
369 prop->mmu_pgt_addr = MMU_PAGE_TABLES_ADDR;
370 prop->mmu_dram_default_page_addr = MMU_DRAM_DEFAULT_PAGE_ADDR;
371 if (hdev->pldm)
372 prop->mmu_pgt_size = 0x800000; /* 8MB */
373 else
374 prop->mmu_pgt_size = MMU_PAGE_TABLES_SIZE;
375 prop->mmu_pte_size = HL_PTE_SIZE;
376 prop->mmu_hop_table_size = HOP_TABLE_SIZE;
377 prop->mmu_hop0_tables_total_size = HOP0_TABLES_TOTAL_SIZE;
378 prop->dram_page_size = PAGE_SIZE_2MB;
379
380 prop->va_space_host_start_address = VA_HOST_SPACE_START;
381 prop->va_space_host_end_address = VA_HOST_SPACE_END;
382 prop->va_space_dram_start_address = VA_DDR_SPACE_START;
383 prop->va_space_dram_end_address = VA_DDR_SPACE_END;
384 prop->dram_size_for_default_page_mapping =
385 prop->va_space_dram_end_address;
386 prop->cfg_size = CFG_SIZE;
387 prop->max_asid = MAX_ASID;
388 prop->num_of_events = GOYA_ASYNC_EVENT_ID_SIZE;
389 prop->high_pll = PLL_HIGH_DEFAULT;
390 prop->cb_pool_cb_cnt = GOYA_CB_POOL_CB_CNT;
391 prop->cb_pool_cb_size = GOYA_CB_POOL_CB_SIZE;
392 prop->max_power_default = MAX_POWER_DEFAULT;
393 prop->tpc_enabled_mask = TPC_ENABLED_MASK;
394 prop->pcie_dbi_base_address = mmPCIE_DBI_BASE;
395 prop->pcie_aux_dbi_reg_addr = CFG_BASE + mmPCIE_AUX_DBI;
396 }
397
398 /*
399 * goya_pci_bars_map - Map PCI BARS of Goya device
400 *
401 * @hdev: pointer to hl_device structure
402 *
403 * Request PCI regions and map them to kernel virtual addresses.
404 * Returns 0 on success
405 *
406 */
goya_pci_bars_map(struct hl_device * hdev)407 static int goya_pci_bars_map(struct hl_device *hdev)
408 {
409 static const char * const name[] = {"SRAM_CFG", "MSIX", "DDR"};
410 bool is_wc[3] = {false, false, true};
411 int rc;
412
413 rc = hl_pci_bars_map(hdev, name, is_wc);
414 if (rc)
415 return rc;
416
417 hdev->rmmio = hdev->pcie_bar[SRAM_CFG_BAR_ID] +
418 (CFG_BASE - SRAM_BASE_ADDR);
419
420 return 0;
421 }
422
goya_set_ddr_bar_base(struct hl_device * hdev,u64 addr)423 static u64 goya_set_ddr_bar_base(struct hl_device *hdev, u64 addr)
424 {
425 struct goya_device *goya = hdev->asic_specific;
426 u64 old_addr = addr;
427 int rc;
428
429 if ((goya) && (goya->ddr_bar_cur_addr == addr))
430 return old_addr;
431
432 /* Inbound Region 1 - Bar 4 - Point to DDR */
433 rc = hl_pci_set_dram_bar_base(hdev, 1, 4, addr);
434 if (rc)
435 return U64_MAX;
436
437 if (goya) {
438 old_addr = goya->ddr_bar_cur_addr;
439 goya->ddr_bar_cur_addr = addr;
440 }
441
442 return old_addr;
443 }
444
445 /*
446 * goya_init_iatu - Initialize the iATU unit inside the PCI controller
447 *
448 * @hdev: pointer to hl_device structure
449 *
450 * This is needed in case the firmware doesn't initialize the iATU
451 *
452 */
goya_init_iatu(struct hl_device * hdev)453 static int goya_init_iatu(struct hl_device *hdev)
454 {
455 return hl_pci_init_iatu(hdev, SRAM_BASE_ADDR, DRAM_PHYS_BASE,
456 HOST_PHYS_BASE, HOST_PHYS_SIZE);
457 }
458
459 /*
460 * goya_early_init - GOYA early initialization code
461 *
462 * @hdev: pointer to hl_device structure
463 *
464 * Verify PCI bars
465 * Set DMA masks
466 * PCI controller initialization
467 * Map PCI bars
468 *
469 */
goya_early_init(struct hl_device * hdev)470 static int goya_early_init(struct hl_device *hdev)
471 {
472 struct asic_fixed_properties *prop = &hdev->asic_prop;
473 struct pci_dev *pdev = hdev->pdev;
474 u32 val;
475 int rc;
476
477 goya_get_fixed_properties(hdev);
478
479 /* Check BAR sizes */
480 if (pci_resource_len(pdev, SRAM_CFG_BAR_ID) != CFG_BAR_SIZE) {
481 dev_err(hdev->dev,
482 "Not " HL_NAME "? BAR %d size %llu, expecting %llu\n",
483 SRAM_CFG_BAR_ID,
484 (unsigned long long) pci_resource_len(pdev,
485 SRAM_CFG_BAR_ID),
486 CFG_BAR_SIZE);
487 return -ENODEV;
488 }
489
490 if (pci_resource_len(pdev, MSIX_BAR_ID) != MSIX_BAR_SIZE) {
491 dev_err(hdev->dev,
492 "Not " HL_NAME "? BAR %d size %llu, expecting %llu\n",
493 MSIX_BAR_ID,
494 (unsigned long long) pci_resource_len(pdev,
495 MSIX_BAR_ID),
496 MSIX_BAR_SIZE);
497 return -ENODEV;
498 }
499
500 prop->dram_pci_bar_size = pci_resource_len(pdev, DDR_BAR_ID);
501
502 rc = hl_pci_init(hdev, 48);
503 if (rc)
504 return rc;
505
506 if (!hdev->pldm) {
507 val = RREG32(mmPSOC_GLOBAL_CONF_BOOT_STRAP_PINS);
508 if (val & PSOC_GLOBAL_CONF_BOOT_STRAP_PINS_SRIOV_EN_MASK)
509 dev_warn(hdev->dev,
510 "PCI strap is not configured correctly, PCI bus errors may occur\n");
511 }
512
513 return 0;
514 }
515
516 /*
517 * goya_early_fini - GOYA early finalization code
518 *
519 * @hdev: pointer to hl_device structure
520 *
521 * Unmap PCI bars
522 *
523 */
goya_early_fini(struct hl_device * hdev)524 static int goya_early_fini(struct hl_device *hdev)
525 {
526 hl_pci_fini(hdev);
527
528 return 0;
529 }
530
goya_mmu_prepare_reg(struct hl_device * hdev,u64 reg,u32 asid)531 static void goya_mmu_prepare_reg(struct hl_device *hdev, u64 reg, u32 asid)
532 {
533 /* mask to zero the MMBP and ASID bits */
534 WREG32_AND(reg, ~0x7FF);
535 WREG32_OR(reg, asid);
536 }
537
goya_qman0_set_security(struct hl_device * hdev,bool secure)538 static void goya_qman0_set_security(struct hl_device *hdev, bool secure)
539 {
540 struct goya_device *goya = hdev->asic_specific;
541
542 if (!(goya->hw_cap_initialized & HW_CAP_MMU))
543 return;
544
545 if (secure)
546 WREG32(mmDMA_QM_0_GLBL_PROT, QMAN_DMA_FULLY_TRUSTED);
547 else
548 WREG32(mmDMA_QM_0_GLBL_PROT, QMAN_DMA_PARTLY_TRUSTED);
549
550 RREG32(mmDMA_QM_0_GLBL_PROT);
551 }
552
553 /*
554 * goya_fetch_psoc_frequency - Fetch PSOC frequency values
555 *
556 * @hdev: pointer to hl_device structure
557 *
558 */
goya_fetch_psoc_frequency(struct hl_device * hdev)559 static void goya_fetch_psoc_frequency(struct hl_device *hdev)
560 {
561 struct asic_fixed_properties *prop = &hdev->asic_prop;
562
563 prop->psoc_pci_pll_nr = RREG32(mmPSOC_PCI_PLL_NR);
564 prop->psoc_pci_pll_nf = RREG32(mmPSOC_PCI_PLL_NF);
565 prop->psoc_pci_pll_od = RREG32(mmPSOC_PCI_PLL_OD);
566 prop->psoc_pci_pll_div_factor = RREG32(mmPSOC_PCI_PLL_DIV_FACTOR_1);
567 }
568
goya_late_init(struct hl_device * hdev)569 int goya_late_init(struct hl_device *hdev)
570 {
571 struct asic_fixed_properties *prop = &hdev->asic_prop;
572 int rc;
573
574 goya_fetch_psoc_frequency(hdev);
575
576 rc = goya_mmu_clear_pgt_range(hdev);
577 if (rc) {
578 dev_err(hdev->dev,
579 "Failed to clear MMU page tables range %d\n", rc);
580 return rc;
581 }
582
583 rc = goya_mmu_set_dram_default_page(hdev);
584 if (rc) {
585 dev_err(hdev->dev, "Failed to set DRAM default page %d\n", rc);
586 return rc;
587 }
588
589 rc = goya_mmu_add_mappings_for_device_cpu(hdev);
590 if (rc)
591 return rc;
592
593 rc = goya_init_cpu_queues(hdev);
594 if (rc)
595 return rc;
596
597 rc = goya_test_cpu_queue(hdev);
598 if (rc)
599 return rc;
600
601 rc = goya_armcp_info_get(hdev);
602 if (rc) {
603 dev_err(hdev->dev, "Failed to get armcp info %d\n", rc);
604 return rc;
605 }
606
607 /* Now that we have the DRAM size in ASIC prop, we need to check
608 * its size and configure the DMA_IF DDR wrap protection (which is in
609 * the MMU block) accordingly. The value is the log2 of the DRAM size
610 */
611 WREG32(mmMMU_LOG2_DDR_SIZE, ilog2(prop->dram_size));
612
613 rc = hl_fw_send_pci_access_msg(hdev, ARMCP_PACKET_ENABLE_PCI_ACCESS);
614 if (rc) {
615 dev_err(hdev->dev,
616 "Failed to enable PCI access from CPU %d\n", rc);
617 return rc;
618 }
619
620 WREG32(mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR,
621 GOYA_ASYNC_EVENT_ID_INTS_REGISTER);
622
623 return 0;
624 }
625
626 /*
627 * goya_late_fini - GOYA late tear-down code
628 *
629 * @hdev: pointer to hl_device structure
630 *
631 * Free sensors allocated structures
632 */
goya_late_fini(struct hl_device * hdev)633 void goya_late_fini(struct hl_device *hdev)
634 {
635 const struct hwmon_channel_info **channel_info_arr;
636 int i = 0;
637
638 if (!hdev->hl_chip_info->info)
639 return;
640
641 channel_info_arr = hdev->hl_chip_info->info;
642
643 while (channel_info_arr[i]) {
644 kfree(channel_info_arr[i]->config);
645 kfree(channel_info_arr[i]);
646 i++;
647 }
648
649 kfree(channel_info_arr);
650
651 hdev->hl_chip_info->info = NULL;
652 }
653
654 /*
655 * goya_sw_init - Goya software initialization code
656 *
657 * @hdev: pointer to hl_device structure
658 *
659 */
goya_sw_init(struct hl_device * hdev)660 static int goya_sw_init(struct hl_device *hdev)
661 {
662 struct goya_device *goya;
663 int rc;
664
665 /* Allocate device structure */
666 goya = kzalloc(sizeof(*goya), GFP_KERNEL);
667 if (!goya)
668 return -ENOMEM;
669
670 /* according to goya_init_iatu */
671 goya->ddr_bar_cur_addr = DRAM_PHYS_BASE;
672
673 goya->mme_clk = GOYA_PLL_FREQ_LOW;
674 goya->tpc_clk = GOYA_PLL_FREQ_LOW;
675 goya->ic_clk = GOYA_PLL_FREQ_LOW;
676
677 hdev->asic_specific = goya;
678
679 /* Create DMA pool for small allocations */
680 hdev->dma_pool = dma_pool_create(dev_name(hdev->dev),
681 &hdev->pdev->dev, GOYA_DMA_POOL_BLK_SIZE, 8, 0);
682 if (!hdev->dma_pool) {
683 dev_err(hdev->dev, "failed to create DMA pool\n");
684 rc = -ENOMEM;
685 goto free_goya_device;
686 }
687
688 hdev->cpu_accessible_dma_mem =
689 hdev->asic_funcs->asic_dma_alloc_coherent(hdev,
690 HL_CPU_ACCESSIBLE_MEM_SIZE,
691 &hdev->cpu_accessible_dma_address,
692 GFP_KERNEL | __GFP_ZERO);
693
694 if (!hdev->cpu_accessible_dma_mem) {
695 rc = -ENOMEM;
696 goto free_dma_pool;
697 }
698
699 dev_dbg(hdev->dev, "cpu accessible memory at bus address %pad\n",
700 &hdev->cpu_accessible_dma_address);
701
702 hdev->cpu_accessible_dma_pool = gen_pool_create(ilog2(32), -1);
703 if (!hdev->cpu_accessible_dma_pool) {
704 dev_err(hdev->dev,
705 "Failed to create CPU accessible DMA pool\n");
706 rc = -ENOMEM;
707 goto free_cpu_dma_mem;
708 }
709
710 rc = gen_pool_add(hdev->cpu_accessible_dma_pool,
711 (uintptr_t) hdev->cpu_accessible_dma_mem,
712 HL_CPU_ACCESSIBLE_MEM_SIZE, -1);
713 if (rc) {
714 dev_err(hdev->dev,
715 "Failed to add memory to CPU accessible DMA pool\n");
716 rc = -EFAULT;
717 goto free_cpu_accessible_dma_pool;
718 }
719
720 spin_lock_init(&goya->hw_queues_lock);
721
722 return 0;
723
724 free_cpu_accessible_dma_pool:
725 gen_pool_destroy(hdev->cpu_accessible_dma_pool);
726 free_cpu_dma_mem:
727 hdev->asic_funcs->asic_dma_free_coherent(hdev,
728 HL_CPU_ACCESSIBLE_MEM_SIZE,
729 hdev->cpu_accessible_dma_mem,
730 hdev->cpu_accessible_dma_address);
731 free_dma_pool:
732 dma_pool_destroy(hdev->dma_pool);
733 free_goya_device:
734 kfree(goya);
735
736 return rc;
737 }
738
739 /*
740 * goya_sw_fini - Goya software tear-down code
741 *
742 * @hdev: pointer to hl_device structure
743 *
744 */
goya_sw_fini(struct hl_device * hdev)745 static int goya_sw_fini(struct hl_device *hdev)
746 {
747 struct goya_device *goya = hdev->asic_specific;
748
749 gen_pool_destroy(hdev->cpu_accessible_dma_pool);
750
751 hdev->asic_funcs->asic_dma_free_coherent(hdev,
752 HL_CPU_ACCESSIBLE_MEM_SIZE,
753 hdev->cpu_accessible_dma_mem,
754 hdev->cpu_accessible_dma_address);
755
756 dma_pool_destroy(hdev->dma_pool);
757
758 kfree(goya);
759
760 return 0;
761 }
762
goya_init_dma_qman(struct hl_device * hdev,int dma_id,dma_addr_t bus_address)763 static void goya_init_dma_qman(struct hl_device *hdev, int dma_id,
764 dma_addr_t bus_address)
765 {
766 struct goya_device *goya = hdev->asic_specific;
767 u32 mtr_base_lo, mtr_base_hi;
768 u32 so_base_lo, so_base_hi;
769 u32 gic_base_lo, gic_base_hi;
770 u32 reg_off = dma_id * (mmDMA_QM_1_PQ_PI - mmDMA_QM_0_PQ_PI);
771
772 mtr_base_lo = lower_32_bits(CFG_BASE + mmSYNC_MNGR_MON_PAY_ADDRL_0);
773 mtr_base_hi = upper_32_bits(CFG_BASE + mmSYNC_MNGR_MON_PAY_ADDRL_0);
774 so_base_lo = lower_32_bits(CFG_BASE + mmSYNC_MNGR_SOB_OBJ_0);
775 so_base_hi = upper_32_bits(CFG_BASE + mmSYNC_MNGR_SOB_OBJ_0);
776
777 gic_base_lo =
778 lower_32_bits(CFG_BASE + mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR);
779 gic_base_hi =
780 upper_32_bits(CFG_BASE + mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR);
781
782 WREG32(mmDMA_QM_0_PQ_BASE_LO + reg_off, lower_32_bits(bus_address));
783 WREG32(mmDMA_QM_0_PQ_BASE_HI + reg_off, upper_32_bits(bus_address));
784
785 WREG32(mmDMA_QM_0_PQ_SIZE + reg_off, ilog2(HL_QUEUE_LENGTH));
786 WREG32(mmDMA_QM_0_PQ_PI + reg_off, 0);
787 WREG32(mmDMA_QM_0_PQ_CI + reg_off, 0);
788
789 WREG32(mmDMA_QM_0_CP_MSG_BASE0_ADDR_LO + reg_off, mtr_base_lo);
790 WREG32(mmDMA_QM_0_CP_MSG_BASE0_ADDR_HI + reg_off, mtr_base_hi);
791 WREG32(mmDMA_QM_0_CP_MSG_BASE1_ADDR_LO + reg_off, so_base_lo);
792 WREG32(mmDMA_QM_0_CP_MSG_BASE1_ADDR_HI + reg_off, so_base_hi);
793 WREG32(mmDMA_QM_0_GLBL_ERR_ADDR_LO + reg_off, gic_base_lo);
794 WREG32(mmDMA_QM_0_GLBL_ERR_ADDR_HI + reg_off, gic_base_hi);
795 WREG32(mmDMA_QM_0_GLBL_ERR_WDATA + reg_off,
796 GOYA_ASYNC_EVENT_ID_DMA0_QM + dma_id);
797
798 /* PQ has buffer of 2 cache lines, while CQ has 8 lines */
799 WREG32(mmDMA_QM_0_PQ_CFG1 + reg_off, 0x00020002);
800 WREG32(mmDMA_QM_0_CQ_CFG1 + reg_off, 0x00080008);
801
802 if (goya->hw_cap_initialized & HW_CAP_MMU)
803 WREG32(mmDMA_QM_0_GLBL_PROT + reg_off, QMAN_DMA_PARTLY_TRUSTED);
804 else
805 WREG32(mmDMA_QM_0_GLBL_PROT + reg_off, QMAN_DMA_FULLY_TRUSTED);
806
807 WREG32(mmDMA_QM_0_GLBL_ERR_CFG + reg_off, QMAN_DMA_ERR_MSG_EN);
808 WREG32(mmDMA_QM_0_GLBL_CFG0 + reg_off, QMAN_DMA_ENABLE);
809 }
810
goya_init_dma_ch(struct hl_device * hdev,int dma_id)811 static void goya_init_dma_ch(struct hl_device *hdev, int dma_id)
812 {
813 u32 gic_base_lo, gic_base_hi;
814 u64 sob_addr;
815 u32 reg_off = dma_id * (mmDMA_CH_1_CFG1 - mmDMA_CH_0_CFG1);
816
817 gic_base_lo =
818 lower_32_bits(CFG_BASE + mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR);
819 gic_base_hi =
820 upper_32_bits(CFG_BASE + mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR);
821
822 WREG32(mmDMA_CH_0_ERRMSG_ADDR_LO + reg_off, gic_base_lo);
823 WREG32(mmDMA_CH_0_ERRMSG_ADDR_HI + reg_off, gic_base_hi);
824 WREG32(mmDMA_CH_0_ERRMSG_WDATA + reg_off,
825 GOYA_ASYNC_EVENT_ID_DMA0_CH + dma_id);
826
827 if (dma_id)
828 sob_addr = CFG_BASE + mmSYNC_MNGR_SOB_OBJ_1000 +
829 (dma_id - 1) * 4;
830 else
831 sob_addr = CFG_BASE + mmSYNC_MNGR_SOB_OBJ_1007;
832
833 WREG32(mmDMA_CH_0_WR_COMP_ADDR_HI + reg_off, upper_32_bits(sob_addr));
834 WREG32(mmDMA_CH_0_WR_COMP_WDATA + reg_off, 0x80000001);
835 }
836
837 /*
838 * goya_init_dma_qmans - Initialize QMAN DMA registers
839 *
840 * @hdev: pointer to hl_device structure
841 *
842 * Initialize the H/W registers of the QMAN DMA channels
843 *
844 */
goya_init_dma_qmans(struct hl_device * hdev)845 void goya_init_dma_qmans(struct hl_device *hdev)
846 {
847 struct goya_device *goya = hdev->asic_specific;
848 struct hl_hw_queue *q;
849 int i;
850
851 if (goya->hw_cap_initialized & HW_CAP_DMA)
852 return;
853
854 q = &hdev->kernel_queues[0];
855
856 for (i = 0 ; i < NUMBER_OF_EXT_HW_QUEUES ; i++, q++) {
857 goya_init_dma_qman(hdev, i, q->bus_address);
858 goya_init_dma_ch(hdev, i);
859 }
860
861 goya->hw_cap_initialized |= HW_CAP_DMA;
862 }
863
864 /*
865 * goya_disable_external_queues - Disable external queues
866 *
867 * @hdev: pointer to hl_device structure
868 *
869 */
goya_disable_external_queues(struct hl_device * hdev)870 static void goya_disable_external_queues(struct hl_device *hdev)
871 {
872 WREG32(mmDMA_QM_0_GLBL_CFG0, 0);
873 WREG32(mmDMA_QM_1_GLBL_CFG0, 0);
874 WREG32(mmDMA_QM_2_GLBL_CFG0, 0);
875 WREG32(mmDMA_QM_3_GLBL_CFG0, 0);
876 WREG32(mmDMA_QM_4_GLBL_CFG0, 0);
877 }
878
goya_stop_queue(struct hl_device * hdev,u32 cfg_reg,u32 cp_sts_reg,u32 glbl_sts0_reg)879 static int goya_stop_queue(struct hl_device *hdev, u32 cfg_reg,
880 u32 cp_sts_reg, u32 glbl_sts0_reg)
881 {
882 int rc;
883 u32 status;
884
885 /* use the values of TPC0 as they are all the same*/
886
887 WREG32(cfg_reg, 1 << TPC0_QM_GLBL_CFG1_CP_STOP_SHIFT);
888
889 status = RREG32(cp_sts_reg);
890 if (status & TPC0_QM_CP_STS_FENCE_IN_PROGRESS_MASK) {
891 rc = hl_poll_timeout(
892 hdev,
893 cp_sts_reg,
894 status,
895 !(status & TPC0_QM_CP_STS_FENCE_IN_PROGRESS_MASK),
896 1000,
897 QMAN_FENCE_TIMEOUT_USEC);
898
899 /* if QMAN is stuck in fence no need to check for stop */
900 if (rc)
901 return 0;
902 }
903
904 rc = hl_poll_timeout(
905 hdev,
906 glbl_sts0_reg,
907 status,
908 (status & TPC0_QM_GLBL_STS0_CP_IS_STOP_MASK),
909 1000,
910 QMAN_STOP_TIMEOUT_USEC);
911
912 if (rc) {
913 dev_err(hdev->dev,
914 "Timeout while waiting for QMAN to stop\n");
915 return -EINVAL;
916 }
917
918 return 0;
919 }
920
921 /*
922 * goya_stop_external_queues - Stop external queues
923 *
924 * @hdev: pointer to hl_device structure
925 *
926 * Returns 0 on success
927 *
928 */
goya_stop_external_queues(struct hl_device * hdev)929 static int goya_stop_external_queues(struct hl_device *hdev)
930 {
931 int rc, retval = 0;
932
933 rc = goya_stop_queue(hdev,
934 mmDMA_QM_0_GLBL_CFG1,
935 mmDMA_QM_0_CP_STS,
936 mmDMA_QM_0_GLBL_STS0);
937
938 if (rc) {
939 dev_err(hdev->dev, "failed to stop DMA QMAN 0\n");
940 retval = -EIO;
941 }
942
943 rc = goya_stop_queue(hdev,
944 mmDMA_QM_1_GLBL_CFG1,
945 mmDMA_QM_1_CP_STS,
946 mmDMA_QM_1_GLBL_STS0);
947
948 if (rc) {
949 dev_err(hdev->dev, "failed to stop DMA QMAN 1\n");
950 retval = -EIO;
951 }
952
953 rc = goya_stop_queue(hdev,
954 mmDMA_QM_2_GLBL_CFG1,
955 mmDMA_QM_2_CP_STS,
956 mmDMA_QM_2_GLBL_STS0);
957
958 if (rc) {
959 dev_err(hdev->dev, "failed to stop DMA QMAN 2\n");
960 retval = -EIO;
961 }
962
963 rc = goya_stop_queue(hdev,
964 mmDMA_QM_3_GLBL_CFG1,
965 mmDMA_QM_3_CP_STS,
966 mmDMA_QM_3_GLBL_STS0);
967
968 if (rc) {
969 dev_err(hdev->dev, "failed to stop DMA QMAN 3\n");
970 retval = -EIO;
971 }
972
973 rc = goya_stop_queue(hdev,
974 mmDMA_QM_4_GLBL_CFG1,
975 mmDMA_QM_4_CP_STS,
976 mmDMA_QM_4_GLBL_STS0);
977
978 if (rc) {
979 dev_err(hdev->dev, "failed to stop DMA QMAN 4\n");
980 retval = -EIO;
981 }
982
983 return retval;
984 }
985
986 /*
987 * goya_init_cpu_queues - Initialize PQ/CQ/EQ of CPU
988 *
989 * @hdev: pointer to hl_device structure
990 *
991 * Returns 0 on success
992 *
993 */
goya_init_cpu_queues(struct hl_device * hdev)994 int goya_init_cpu_queues(struct hl_device *hdev)
995 {
996 struct goya_device *goya = hdev->asic_specific;
997 struct hl_eq *eq;
998 u32 status;
999 struct hl_hw_queue *cpu_pq = &hdev->kernel_queues[GOYA_QUEUE_ID_CPU_PQ];
1000 int err;
1001
1002 if (!hdev->cpu_queues_enable)
1003 return 0;
1004
1005 if (goya->hw_cap_initialized & HW_CAP_CPU_Q)
1006 return 0;
1007
1008 eq = &hdev->event_queue;
1009
1010 WREG32(mmCPU_PQ_BASE_ADDR_LOW, lower_32_bits(cpu_pq->bus_address));
1011 WREG32(mmCPU_PQ_BASE_ADDR_HIGH, upper_32_bits(cpu_pq->bus_address));
1012
1013 WREG32(mmCPU_EQ_BASE_ADDR_LOW, lower_32_bits(eq->bus_address));
1014 WREG32(mmCPU_EQ_BASE_ADDR_HIGH, upper_32_bits(eq->bus_address));
1015
1016 WREG32(mmCPU_CQ_BASE_ADDR_LOW,
1017 lower_32_bits(VA_CPU_ACCESSIBLE_MEM_ADDR));
1018 WREG32(mmCPU_CQ_BASE_ADDR_HIGH,
1019 upper_32_bits(VA_CPU_ACCESSIBLE_MEM_ADDR));
1020
1021 WREG32(mmCPU_PQ_LENGTH, HL_QUEUE_SIZE_IN_BYTES);
1022 WREG32(mmCPU_EQ_LENGTH, HL_EQ_SIZE_IN_BYTES);
1023 WREG32(mmCPU_CQ_LENGTH, HL_CPU_ACCESSIBLE_MEM_SIZE);
1024
1025 /* Used for EQ CI */
1026 WREG32(mmCPU_EQ_CI, 0);
1027
1028 WREG32(mmCPU_IF_PF_PQ_PI, 0);
1029
1030 WREG32(mmCPU_PQ_INIT_STATUS, PQ_INIT_STATUS_READY_FOR_CP);
1031
1032 WREG32(mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR,
1033 GOYA_ASYNC_EVENT_ID_PI_UPDATE);
1034
1035 err = hl_poll_timeout(
1036 hdev,
1037 mmCPU_PQ_INIT_STATUS,
1038 status,
1039 (status == PQ_INIT_STATUS_READY_FOR_HOST),
1040 1000,
1041 GOYA_CPU_TIMEOUT_USEC);
1042
1043 if (err) {
1044 dev_err(hdev->dev,
1045 "Failed to setup communication with device CPU\n");
1046 return -EIO;
1047 }
1048
1049 goya->hw_cap_initialized |= HW_CAP_CPU_Q;
1050 return 0;
1051 }
1052
goya_set_pll_refclk(struct hl_device * hdev)1053 static void goya_set_pll_refclk(struct hl_device *hdev)
1054 {
1055 WREG32(mmCPU_PLL_DIV_SEL_0, 0x0);
1056 WREG32(mmCPU_PLL_DIV_SEL_1, 0x0);
1057 WREG32(mmCPU_PLL_DIV_SEL_2, 0x0);
1058 WREG32(mmCPU_PLL_DIV_SEL_3, 0x0);
1059
1060 WREG32(mmIC_PLL_DIV_SEL_0, 0x0);
1061 WREG32(mmIC_PLL_DIV_SEL_1, 0x0);
1062 WREG32(mmIC_PLL_DIV_SEL_2, 0x0);
1063 WREG32(mmIC_PLL_DIV_SEL_3, 0x0);
1064
1065 WREG32(mmMC_PLL_DIV_SEL_0, 0x0);
1066 WREG32(mmMC_PLL_DIV_SEL_1, 0x0);
1067 WREG32(mmMC_PLL_DIV_SEL_2, 0x0);
1068 WREG32(mmMC_PLL_DIV_SEL_3, 0x0);
1069
1070 WREG32(mmPSOC_MME_PLL_DIV_SEL_0, 0x0);
1071 WREG32(mmPSOC_MME_PLL_DIV_SEL_1, 0x0);
1072 WREG32(mmPSOC_MME_PLL_DIV_SEL_2, 0x0);
1073 WREG32(mmPSOC_MME_PLL_DIV_SEL_3, 0x0);
1074
1075 WREG32(mmPSOC_PCI_PLL_DIV_SEL_0, 0x0);
1076 WREG32(mmPSOC_PCI_PLL_DIV_SEL_1, 0x0);
1077 WREG32(mmPSOC_PCI_PLL_DIV_SEL_2, 0x0);
1078 WREG32(mmPSOC_PCI_PLL_DIV_SEL_3, 0x0);
1079
1080 WREG32(mmPSOC_EMMC_PLL_DIV_SEL_0, 0x0);
1081 WREG32(mmPSOC_EMMC_PLL_DIV_SEL_1, 0x0);
1082 WREG32(mmPSOC_EMMC_PLL_DIV_SEL_2, 0x0);
1083 WREG32(mmPSOC_EMMC_PLL_DIV_SEL_3, 0x0);
1084
1085 WREG32(mmTPC_PLL_DIV_SEL_0, 0x0);
1086 WREG32(mmTPC_PLL_DIV_SEL_1, 0x0);
1087 WREG32(mmTPC_PLL_DIV_SEL_2, 0x0);
1088 WREG32(mmTPC_PLL_DIV_SEL_3, 0x0);
1089 }
1090
goya_disable_clk_rlx(struct hl_device * hdev)1091 static void goya_disable_clk_rlx(struct hl_device *hdev)
1092 {
1093 WREG32(mmPSOC_MME_PLL_CLK_RLX_0, 0x100010);
1094 WREG32(mmIC_PLL_CLK_RLX_0, 0x100010);
1095 }
1096
_goya_tpc_mbist_workaround(struct hl_device * hdev,u8 tpc_id)1097 static void _goya_tpc_mbist_workaround(struct hl_device *hdev, u8 tpc_id)
1098 {
1099 u64 tpc_eml_address;
1100 u32 val, tpc_offset, tpc_eml_offset, tpc_slm_offset;
1101 int err, slm_index;
1102
1103 tpc_offset = tpc_id * 0x40000;
1104 tpc_eml_offset = tpc_id * 0x200000;
1105 tpc_eml_address = (mmTPC0_EML_CFG_BASE + tpc_eml_offset - CFG_BASE);
1106 tpc_slm_offset = tpc_eml_address + 0x100000;
1107
1108 /*
1109 * Workaround for Bug H2 #2443 :
1110 * "TPC SB is not initialized on chip reset"
1111 */
1112
1113 val = RREG32(mmTPC0_CFG_FUNC_MBIST_CNTRL + tpc_offset);
1114 if (val & TPC0_CFG_FUNC_MBIST_CNTRL_MBIST_ACTIVE_MASK)
1115 dev_warn(hdev->dev, "TPC%d MBIST ACTIVE is not cleared\n",
1116 tpc_id);
1117
1118 WREG32(mmTPC0_CFG_FUNC_MBIST_PAT + tpc_offset, val & 0xFFFFF000);
1119
1120 WREG32(mmTPC0_CFG_FUNC_MBIST_MEM_0 + tpc_offset, 0x37FF);
1121 WREG32(mmTPC0_CFG_FUNC_MBIST_MEM_1 + tpc_offset, 0x303F);
1122 WREG32(mmTPC0_CFG_FUNC_MBIST_MEM_2 + tpc_offset, 0x71FF);
1123 WREG32(mmTPC0_CFG_FUNC_MBIST_MEM_3 + tpc_offset, 0x71FF);
1124 WREG32(mmTPC0_CFG_FUNC_MBIST_MEM_4 + tpc_offset, 0x70FF);
1125 WREG32(mmTPC0_CFG_FUNC_MBIST_MEM_5 + tpc_offset, 0x70FF);
1126 WREG32(mmTPC0_CFG_FUNC_MBIST_MEM_6 + tpc_offset, 0x70FF);
1127 WREG32(mmTPC0_CFG_FUNC_MBIST_MEM_7 + tpc_offset, 0x70FF);
1128 WREG32(mmTPC0_CFG_FUNC_MBIST_MEM_8 + tpc_offset, 0x70FF);
1129 WREG32(mmTPC0_CFG_FUNC_MBIST_MEM_9 + tpc_offset, 0x70FF);
1130
1131 WREG32_OR(mmTPC0_CFG_FUNC_MBIST_CNTRL + tpc_offset,
1132 1 << TPC0_CFG_FUNC_MBIST_CNTRL_MBIST_START_SHIFT);
1133
1134 err = hl_poll_timeout(
1135 hdev,
1136 mmTPC0_CFG_FUNC_MBIST_CNTRL + tpc_offset,
1137 val,
1138 (val & TPC0_CFG_FUNC_MBIST_CNTRL_MBIST_DONE_MASK),
1139 1000,
1140 HL_DEVICE_TIMEOUT_USEC);
1141
1142 if (err)
1143 dev_err(hdev->dev,
1144 "Timeout while waiting for TPC%d MBIST DONE\n", tpc_id);
1145
1146 WREG32_OR(mmTPC0_EML_CFG_DBG_CNT + tpc_eml_offset,
1147 1 << TPC0_EML_CFG_DBG_CNT_CORE_RST_SHIFT);
1148
1149 msleep(GOYA_RESET_WAIT_MSEC);
1150
1151 WREG32_AND(mmTPC0_EML_CFG_DBG_CNT + tpc_eml_offset,
1152 ~(1 << TPC0_EML_CFG_DBG_CNT_CORE_RST_SHIFT));
1153
1154 msleep(GOYA_RESET_WAIT_MSEC);
1155
1156 for (slm_index = 0 ; slm_index < 256 ; slm_index++)
1157 WREG32(tpc_slm_offset + (slm_index << 2), 0);
1158
1159 val = RREG32(tpc_slm_offset);
1160 }
1161
goya_tpc_mbist_workaround(struct hl_device * hdev)1162 static void goya_tpc_mbist_workaround(struct hl_device *hdev)
1163 {
1164 struct goya_device *goya = hdev->asic_specific;
1165 int i;
1166
1167 if (hdev->pldm)
1168 return;
1169
1170 if (goya->hw_cap_initialized & HW_CAP_TPC_MBIST)
1171 return;
1172
1173 /* Workaround for H2 #2443 */
1174
1175 for (i = 0 ; i < TPC_MAX_NUM ; i++)
1176 _goya_tpc_mbist_workaround(hdev, i);
1177
1178 goya->hw_cap_initialized |= HW_CAP_TPC_MBIST;
1179 }
1180
1181 /*
1182 * goya_init_golden_registers - Initialize golden registers
1183 *
1184 * @hdev: pointer to hl_device structure
1185 *
1186 * Initialize the H/W registers of the device
1187 *
1188 */
goya_init_golden_registers(struct hl_device * hdev)1189 static void goya_init_golden_registers(struct hl_device *hdev)
1190 {
1191 struct goya_device *goya = hdev->asic_specific;
1192 u32 polynom[10], tpc_intr_mask, offset;
1193 int i;
1194
1195 if (goya->hw_cap_initialized & HW_CAP_GOLDEN)
1196 return;
1197
1198 polynom[0] = 0x00020080;
1199 polynom[1] = 0x00401000;
1200 polynom[2] = 0x00200800;
1201 polynom[3] = 0x00002000;
1202 polynom[4] = 0x00080200;
1203 polynom[5] = 0x00040100;
1204 polynom[6] = 0x00100400;
1205 polynom[7] = 0x00004000;
1206 polynom[8] = 0x00010000;
1207 polynom[9] = 0x00008000;
1208
1209 /* Mask all arithmetic interrupts from TPC */
1210 tpc_intr_mask = 0x7FFF;
1211
1212 for (i = 0, offset = 0 ; i < 6 ; i++, offset += 0x20000) {
1213 WREG32(mmSRAM_Y0_X0_RTR_HBW_RD_RQ_L_ARB + offset, 0x302);
1214 WREG32(mmSRAM_Y0_X1_RTR_HBW_RD_RQ_L_ARB + offset, 0x302);
1215 WREG32(mmSRAM_Y0_X2_RTR_HBW_RD_RQ_L_ARB + offset, 0x302);
1216 WREG32(mmSRAM_Y0_X3_RTR_HBW_RD_RQ_L_ARB + offset, 0x302);
1217 WREG32(mmSRAM_Y0_X4_RTR_HBW_RD_RQ_L_ARB + offset, 0x302);
1218
1219 WREG32(mmSRAM_Y0_X0_RTR_HBW_DATA_L_ARB + offset, 0x204);
1220 WREG32(mmSRAM_Y0_X1_RTR_HBW_DATA_L_ARB + offset, 0x204);
1221 WREG32(mmSRAM_Y0_X2_RTR_HBW_DATA_L_ARB + offset, 0x204);
1222 WREG32(mmSRAM_Y0_X3_RTR_HBW_DATA_L_ARB + offset, 0x204);
1223 WREG32(mmSRAM_Y0_X4_RTR_HBW_DATA_L_ARB + offset, 0x204);
1224
1225
1226 WREG32(mmSRAM_Y0_X0_RTR_HBW_DATA_E_ARB + offset, 0x206);
1227 WREG32(mmSRAM_Y0_X1_RTR_HBW_DATA_E_ARB + offset, 0x206);
1228 WREG32(mmSRAM_Y0_X2_RTR_HBW_DATA_E_ARB + offset, 0x206);
1229 WREG32(mmSRAM_Y0_X3_RTR_HBW_DATA_E_ARB + offset, 0x207);
1230 WREG32(mmSRAM_Y0_X4_RTR_HBW_DATA_E_ARB + offset, 0x207);
1231
1232 WREG32(mmSRAM_Y0_X0_RTR_HBW_DATA_W_ARB + offset, 0x207);
1233 WREG32(mmSRAM_Y0_X1_RTR_HBW_DATA_W_ARB + offset, 0x207);
1234 WREG32(mmSRAM_Y0_X2_RTR_HBW_DATA_W_ARB + offset, 0x206);
1235 WREG32(mmSRAM_Y0_X3_RTR_HBW_DATA_W_ARB + offset, 0x206);
1236 WREG32(mmSRAM_Y0_X4_RTR_HBW_DATA_W_ARB + offset, 0x206);
1237
1238 WREG32(mmSRAM_Y0_X0_RTR_HBW_WR_RS_E_ARB + offset, 0x101);
1239 WREG32(mmSRAM_Y0_X1_RTR_HBW_WR_RS_E_ARB + offset, 0x102);
1240 WREG32(mmSRAM_Y0_X2_RTR_HBW_WR_RS_E_ARB + offset, 0x103);
1241 WREG32(mmSRAM_Y0_X3_RTR_HBW_WR_RS_E_ARB + offset, 0x104);
1242 WREG32(mmSRAM_Y0_X4_RTR_HBW_WR_RS_E_ARB + offset, 0x105);
1243
1244 WREG32(mmSRAM_Y0_X0_RTR_HBW_WR_RS_W_ARB + offset, 0x105);
1245 WREG32(mmSRAM_Y0_X1_RTR_HBW_WR_RS_W_ARB + offset, 0x104);
1246 WREG32(mmSRAM_Y0_X2_RTR_HBW_WR_RS_W_ARB + offset, 0x103);
1247 WREG32(mmSRAM_Y0_X3_RTR_HBW_WR_RS_W_ARB + offset, 0x102);
1248 WREG32(mmSRAM_Y0_X4_RTR_HBW_WR_RS_W_ARB + offset, 0x101);
1249 }
1250
1251 WREG32(mmMME_STORE_MAX_CREDIT, 0x21);
1252 WREG32(mmMME_AGU, 0x0f0f0f10);
1253 WREG32(mmMME_SEI_MASK, ~0x0);
1254
1255 WREG32(mmMME6_RTR_HBW_RD_RQ_N_ARB, 0x01010101);
1256 WREG32(mmMME5_RTR_HBW_RD_RQ_N_ARB, 0x01040101);
1257 WREG32(mmMME4_RTR_HBW_RD_RQ_N_ARB, 0x01030101);
1258 WREG32(mmMME3_RTR_HBW_RD_RQ_N_ARB, 0x01020101);
1259 WREG32(mmMME2_RTR_HBW_RD_RQ_N_ARB, 0x01010101);
1260 WREG32(mmMME1_RTR_HBW_RD_RQ_N_ARB, 0x07010701);
1261 WREG32(mmMME6_RTR_HBW_RD_RQ_S_ARB, 0x04010401);
1262 WREG32(mmMME5_RTR_HBW_RD_RQ_S_ARB, 0x04050401);
1263 WREG32(mmMME4_RTR_HBW_RD_RQ_S_ARB, 0x03070301);
1264 WREG32(mmMME3_RTR_HBW_RD_RQ_S_ARB, 0x01030101);
1265 WREG32(mmMME2_RTR_HBW_RD_RQ_S_ARB, 0x01040101);
1266 WREG32(mmMME1_RTR_HBW_RD_RQ_S_ARB, 0x01050105);
1267 WREG32(mmMME6_RTR_HBW_RD_RQ_W_ARB, 0x01010501);
1268 WREG32(mmMME5_RTR_HBW_RD_RQ_W_ARB, 0x01010501);
1269 WREG32(mmMME4_RTR_HBW_RD_RQ_W_ARB, 0x01040301);
1270 WREG32(mmMME3_RTR_HBW_RD_RQ_W_ARB, 0x01030401);
1271 WREG32(mmMME2_RTR_HBW_RD_RQ_W_ARB, 0x01040101);
1272 WREG32(mmMME1_RTR_HBW_RD_RQ_W_ARB, 0x01050101);
1273 WREG32(mmMME6_RTR_HBW_WR_RQ_N_ARB, 0x02020202);
1274 WREG32(mmMME5_RTR_HBW_WR_RQ_N_ARB, 0x01070101);
1275 WREG32(mmMME4_RTR_HBW_WR_RQ_N_ARB, 0x02020201);
1276 WREG32(mmMME3_RTR_HBW_WR_RQ_N_ARB, 0x07020701);
1277 WREG32(mmMME2_RTR_HBW_WR_RQ_N_ARB, 0x01020101);
1278 WREG32(mmMME1_RTR_HBW_WR_RQ_S_ARB, 0x01010101);
1279 WREG32(mmMME6_RTR_HBW_WR_RQ_S_ARB, 0x01070101);
1280 WREG32(mmMME5_RTR_HBW_WR_RQ_S_ARB, 0x01070101);
1281 WREG32(mmMME4_RTR_HBW_WR_RQ_S_ARB, 0x07020701);
1282 WREG32(mmMME3_RTR_HBW_WR_RQ_S_ARB, 0x02020201);
1283 WREG32(mmMME2_RTR_HBW_WR_RQ_S_ARB, 0x01070101);
1284 WREG32(mmMME1_RTR_HBW_WR_RQ_S_ARB, 0x01020102);
1285 WREG32(mmMME6_RTR_HBW_WR_RQ_W_ARB, 0x01020701);
1286 WREG32(mmMME5_RTR_HBW_WR_RQ_W_ARB, 0x01020701);
1287 WREG32(mmMME4_RTR_HBW_WR_RQ_W_ARB, 0x07020707);
1288 WREG32(mmMME3_RTR_HBW_WR_RQ_W_ARB, 0x01020201);
1289 WREG32(mmMME2_RTR_HBW_WR_RQ_W_ARB, 0x01070201);
1290 WREG32(mmMME1_RTR_HBW_WR_RQ_W_ARB, 0x01070201);
1291 WREG32(mmMME6_RTR_HBW_RD_RS_N_ARB, 0x01070102);
1292 WREG32(mmMME5_RTR_HBW_RD_RS_N_ARB, 0x01070102);
1293 WREG32(mmMME4_RTR_HBW_RD_RS_N_ARB, 0x01060102);
1294 WREG32(mmMME3_RTR_HBW_RD_RS_N_ARB, 0x01040102);
1295 WREG32(mmMME2_RTR_HBW_RD_RS_N_ARB, 0x01020102);
1296 WREG32(mmMME1_RTR_HBW_RD_RS_N_ARB, 0x01020107);
1297 WREG32(mmMME6_RTR_HBW_RD_RS_S_ARB, 0x01020106);
1298 WREG32(mmMME5_RTR_HBW_RD_RS_S_ARB, 0x01020102);
1299 WREG32(mmMME4_RTR_HBW_RD_RS_S_ARB, 0x01040102);
1300 WREG32(mmMME3_RTR_HBW_RD_RS_S_ARB, 0x01060102);
1301 WREG32(mmMME2_RTR_HBW_RD_RS_S_ARB, 0x01070102);
1302 WREG32(mmMME1_RTR_HBW_RD_RS_S_ARB, 0x01070102);
1303 WREG32(mmMME6_RTR_HBW_RD_RS_E_ARB, 0x01020702);
1304 WREG32(mmMME5_RTR_HBW_RD_RS_E_ARB, 0x01020702);
1305 WREG32(mmMME4_RTR_HBW_RD_RS_E_ARB, 0x01040602);
1306 WREG32(mmMME3_RTR_HBW_RD_RS_E_ARB, 0x01060402);
1307 WREG32(mmMME2_RTR_HBW_RD_RS_E_ARB, 0x01070202);
1308 WREG32(mmMME1_RTR_HBW_RD_RS_E_ARB, 0x01070102);
1309 WREG32(mmMME6_RTR_HBW_RD_RS_W_ARB, 0x01060401);
1310 WREG32(mmMME5_RTR_HBW_RD_RS_W_ARB, 0x01060401);
1311 WREG32(mmMME4_RTR_HBW_RD_RS_W_ARB, 0x01060401);
1312 WREG32(mmMME3_RTR_HBW_RD_RS_W_ARB, 0x01060401);
1313 WREG32(mmMME2_RTR_HBW_RD_RS_W_ARB, 0x01060401);
1314 WREG32(mmMME1_RTR_HBW_RD_RS_W_ARB, 0x01060401);
1315 WREG32(mmMME6_RTR_HBW_WR_RS_N_ARB, 0x01050101);
1316 WREG32(mmMME5_RTR_HBW_WR_RS_N_ARB, 0x01040101);
1317 WREG32(mmMME4_RTR_HBW_WR_RS_N_ARB, 0x01030101);
1318 WREG32(mmMME3_RTR_HBW_WR_RS_N_ARB, 0x01020101);
1319 WREG32(mmMME2_RTR_HBW_WR_RS_N_ARB, 0x01010101);
1320 WREG32(mmMME1_RTR_HBW_WR_RS_N_ARB, 0x01010107);
1321 WREG32(mmMME6_RTR_HBW_WR_RS_S_ARB, 0x01010107);
1322 WREG32(mmMME5_RTR_HBW_WR_RS_S_ARB, 0x01010101);
1323 WREG32(mmMME4_RTR_HBW_WR_RS_S_ARB, 0x01020101);
1324 WREG32(mmMME3_RTR_HBW_WR_RS_S_ARB, 0x01030101);
1325 WREG32(mmMME2_RTR_HBW_WR_RS_S_ARB, 0x01040101);
1326 WREG32(mmMME1_RTR_HBW_WR_RS_S_ARB, 0x01050101);
1327 WREG32(mmMME6_RTR_HBW_WR_RS_E_ARB, 0x01010501);
1328 WREG32(mmMME5_RTR_HBW_WR_RS_E_ARB, 0x01010501);
1329 WREG32(mmMME4_RTR_HBW_WR_RS_E_ARB, 0x01040301);
1330 WREG32(mmMME3_RTR_HBW_WR_RS_E_ARB, 0x01030401);
1331 WREG32(mmMME2_RTR_HBW_WR_RS_E_ARB, 0x01040101);
1332 WREG32(mmMME1_RTR_HBW_WR_RS_E_ARB, 0x01050101);
1333 WREG32(mmMME6_RTR_HBW_WR_RS_W_ARB, 0x01010101);
1334 WREG32(mmMME5_RTR_HBW_WR_RS_W_ARB, 0x01010101);
1335 WREG32(mmMME4_RTR_HBW_WR_RS_W_ARB, 0x01010101);
1336 WREG32(mmMME3_RTR_HBW_WR_RS_W_ARB, 0x01010101);
1337 WREG32(mmMME2_RTR_HBW_WR_RS_W_ARB, 0x01010101);
1338 WREG32(mmMME1_RTR_HBW_WR_RS_W_ARB, 0x01010101);
1339
1340 WREG32(mmTPC1_RTR_HBW_RD_RQ_N_ARB, 0x01010101);
1341 WREG32(mmTPC1_RTR_HBW_RD_RQ_S_ARB, 0x01010101);
1342 WREG32(mmTPC1_RTR_HBW_RD_RQ_E_ARB, 0x01060101);
1343 WREG32(mmTPC1_RTR_HBW_WR_RQ_N_ARB, 0x02020102);
1344 WREG32(mmTPC1_RTR_HBW_WR_RQ_S_ARB, 0x01010101);
1345 WREG32(mmTPC1_RTR_HBW_WR_RQ_E_ARB, 0x02070202);
1346 WREG32(mmTPC1_RTR_HBW_RD_RS_N_ARB, 0x01020201);
1347 WREG32(mmTPC1_RTR_HBW_RD_RS_S_ARB, 0x01070201);
1348 WREG32(mmTPC1_RTR_HBW_RD_RS_W_ARB, 0x01070202);
1349 WREG32(mmTPC1_RTR_HBW_WR_RS_N_ARB, 0x01010101);
1350 WREG32(mmTPC1_RTR_HBW_WR_RS_S_ARB, 0x01050101);
1351 WREG32(mmTPC1_RTR_HBW_WR_RS_W_ARB, 0x01050101);
1352
1353 WREG32(mmTPC2_RTR_HBW_RD_RQ_N_ARB, 0x01020101);
1354 WREG32(mmTPC2_RTR_HBW_RD_RQ_S_ARB, 0x01050101);
1355 WREG32(mmTPC2_RTR_HBW_RD_RQ_E_ARB, 0x01010201);
1356 WREG32(mmTPC2_RTR_HBW_WR_RQ_N_ARB, 0x02040102);
1357 WREG32(mmTPC2_RTR_HBW_WR_RQ_S_ARB, 0x01050101);
1358 WREG32(mmTPC2_RTR_HBW_WR_RQ_E_ARB, 0x02060202);
1359 WREG32(mmTPC2_RTR_HBW_RD_RS_N_ARB, 0x01020201);
1360 WREG32(mmTPC2_RTR_HBW_RD_RS_S_ARB, 0x01070201);
1361 WREG32(mmTPC2_RTR_HBW_RD_RS_W_ARB, 0x01070202);
1362 WREG32(mmTPC2_RTR_HBW_WR_RS_N_ARB, 0x01010101);
1363 WREG32(mmTPC2_RTR_HBW_WR_RS_S_ARB, 0x01040101);
1364 WREG32(mmTPC2_RTR_HBW_WR_RS_W_ARB, 0x01040101);
1365
1366 WREG32(mmTPC3_RTR_HBW_RD_RQ_N_ARB, 0x01030101);
1367 WREG32(mmTPC3_RTR_HBW_RD_RQ_S_ARB, 0x01040101);
1368 WREG32(mmTPC3_RTR_HBW_RD_RQ_E_ARB, 0x01040301);
1369 WREG32(mmTPC3_RTR_HBW_WR_RQ_N_ARB, 0x02060102);
1370 WREG32(mmTPC3_RTR_HBW_WR_RQ_S_ARB, 0x01040101);
1371 WREG32(mmTPC3_RTR_HBW_WR_RQ_E_ARB, 0x01040301);
1372 WREG32(mmTPC3_RTR_HBW_RD_RS_N_ARB, 0x01040201);
1373 WREG32(mmTPC3_RTR_HBW_RD_RS_S_ARB, 0x01060201);
1374 WREG32(mmTPC3_RTR_HBW_RD_RS_W_ARB, 0x01060402);
1375 WREG32(mmTPC3_RTR_HBW_WR_RS_N_ARB, 0x01020101);
1376 WREG32(mmTPC3_RTR_HBW_WR_RS_S_ARB, 0x01030101);
1377 WREG32(mmTPC3_RTR_HBW_WR_RS_W_ARB, 0x01030401);
1378
1379 WREG32(mmTPC4_RTR_HBW_RD_RQ_N_ARB, 0x01040101);
1380 WREG32(mmTPC4_RTR_HBW_RD_RQ_S_ARB, 0x01030101);
1381 WREG32(mmTPC4_RTR_HBW_RD_RQ_E_ARB, 0x01030401);
1382 WREG32(mmTPC4_RTR_HBW_WR_RQ_N_ARB, 0x02070102);
1383 WREG32(mmTPC4_RTR_HBW_WR_RQ_S_ARB, 0x01030101);
1384 WREG32(mmTPC4_RTR_HBW_WR_RQ_E_ARB, 0x02060702);
1385 WREG32(mmTPC4_RTR_HBW_RD_RS_N_ARB, 0x01060201);
1386 WREG32(mmTPC4_RTR_HBW_RD_RS_S_ARB, 0x01040201);
1387 WREG32(mmTPC4_RTR_HBW_RD_RS_W_ARB, 0x01040602);
1388 WREG32(mmTPC4_RTR_HBW_WR_RS_N_ARB, 0x01030101);
1389 WREG32(mmTPC4_RTR_HBW_WR_RS_S_ARB, 0x01020101);
1390 WREG32(mmTPC4_RTR_HBW_WR_RS_W_ARB, 0x01040301);
1391
1392 WREG32(mmTPC5_RTR_HBW_RD_RQ_N_ARB, 0x01050101);
1393 WREG32(mmTPC5_RTR_HBW_RD_RQ_S_ARB, 0x01020101);
1394 WREG32(mmTPC5_RTR_HBW_RD_RQ_E_ARB, 0x01200501);
1395 WREG32(mmTPC5_RTR_HBW_WR_RQ_N_ARB, 0x02070102);
1396 WREG32(mmTPC5_RTR_HBW_WR_RQ_S_ARB, 0x01020101);
1397 WREG32(mmTPC5_RTR_HBW_WR_RQ_E_ARB, 0x02020602);
1398 WREG32(mmTPC5_RTR_HBW_RD_RS_N_ARB, 0x01070201);
1399 WREG32(mmTPC5_RTR_HBW_RD_RS_S_ARB, 0x01020201);
1400 WREG32(mmTPC5_RTR_HBW_RD_RS_W_ARB, 0x01020702);
1401 WREG32(mmTPC5_RTR_HBW_WR_RS_N_ARB, 0x01040101);
1402 WREG32(mmTPC5_RTR_HBW_WR_RS_S_ARB, 0x01010101);
1403 WREG32(mmTPC5_RTR_HBW_WR_RS_W_ARB, 0x01010501);
1404
1405 WREG32(mmTPC6_RTR_HBW_RD_RQ_N_ARB, 0x01010101);
1406 WREG32(mmTPC6_RTR_HBW_RD_RQ_S_ARB, 0x01010101);
1407 WREG32(mmTPC6_RTR_HBW_RD_RQ_E_ARB, 0x01010601);
1408 WREG32(mmTPC6_RTR_HBW_WR_RQ_N_ARB, 0x01010101);
1409 WREG32(mmTPC6_RTR_HBW_WR_RQ_S_ARB, 0x01010101);
1410 WREG32(mmTPC6_RTR_HBW_WR_RQ_E_ARB, 0x02020702);
1411 WREG32(mmTPC6_RTR_HBW_RD_RS_N_ARB, 0x01010101);
1412 WREG32(mmTPC6_RTR_HBW_RD_RS_S_ARB, 0x01010101);
1413 WREG32(mmTPC6_RTR_HBW_RD_RS_W_ARB, 0x01020702);
1414 WREG32(mmTPC6_RTR_HBW_WR_RS_N_ARB, 0x01050101);
1415 WREG32(mmTPC6_RTR_HBW_WR_RS_S_ARB, 0x01010101);
1416 WREG32(mmTPC6_RTR_HBW_WR_RS_W_ARB, 0x01010501);
1417
1418 for (i = 0, offset = 0 ; i < 10 ; i++, offset += 4) {
1419 WREG32(mmMME1_RTR_SPLIT_COEF_0 + offset, polynom[i] >> 7);
1420 WREG32(mmMME2_RTR_SPLIT_COEF_0 + offset, polynom[i] >> 7);
1421 WREG32(mmMME3_RTR_SPLIT_COEF_0 + offset, polynom[i] >> 7);
1422 WREG32(mmMME4_RTR_SPLIT_COEF_0 + offset, polynom[i] >> 7);
1423 WREG32(mmMME5_RTR_SPLIT_COEF_0 + offset, polynom[i] >> 7);
1424 WREG32(mmMME6_RTR_SPLIT_COEF_0 + offset, polynom[i] >> 7);
1425
1426 WREG32(mmTPC0_NRTR_SPLIT_COEF_0 + offset, polynom[i] >> 7);
1427 WREG32(mmTPC1_RTR_SPLIT_COEF_0 + offset, polynom[i] >> 7);
1428 WREG32(mmTPC2_RTR_SPLIT_COEF_0 + offset, polynom[i] >> 7);
1429 WREG32(mmTPC3_RTR_SPLIT_COEF_0 + offset, polynom[i] >> 7);
1430 WREG32(mmTPC4_RTR_SPLIT_COEF_0 + offset, polynom[i] >> 7);
1431 WREG32(mmTPC5_RTR_SPLIT_COEF_0 + offset, polynom[i] >> 7);
1432 WREG32(mmTPC6_RTR_SPLIT_COEF_0 + offset, polynom[i] >> 7);
1433 WREG32(mmTPC7_NRTR_SPLIT_COEF_0 + offset, polynom[i] >> 7);
1434
1435 WREG32(mmPCI_NRTR_SPLIT_COEF_0 + offset, polynom[i] >> 7);
1436 WREG32(mmDMA_NRTR_SPLIT_COEF_0 + offset, polynom[i] >> 7);
1437 }
1438
1439 for (i = 0, offset = 0 ; i < 6 ; i++, offset += 0x40000) {
1440 WREG32(mmMME1_RTR_SCRAMB_EN + offset,
1441 1 << MME1_RTR_SCRAMB_EN_VAL_SHIFT);
1442 WREG32(mmMME1_RTR_NON_LIN_SCRAMB + offset,
1443 1 << MME1_RTR_NON_LIN_SCRAMB_EN_SHIFT);
1444 }
1445
1446 for (i = 0, offset = 0 ; i < 8 ; i++, offset += 0x40000) {
1447 /*
1448 * Workaround for Bug H2 #2441 :
1449 * "ST.NOP set trace event illegal opcode"
1450 */
1451 WREG32(mmTPC0_CFG_TPC_INTR_MASK + offset, tpc_intr_mask);
1452
1453 WREG32(mmTPC0_NRTR_SCRAMB_EN + offset,
1454 1 << TPC0_NRTR_SCRAMB_EN_VAL_SHIFT);
1455 WREG32(mmTPC0_NRTR_NON_LIN_SCRAMB + offset,
1456 1 << TPC0_NRTR_NON_LIN_SCRAMB_EN_SHIFT);
1457 }
1458
1459 WREG32(mmDMA_NRTR_SCRAMB_EN, 1 << DMA_NRTR_SCRAMB_EN_VAL_SHIFT);
1460 WREG32(mmDMA_NRTR_NON_LIN_SCRAMB,
1461 1 << DMA_NRTR_NON_LIN_SCRAMB_EN_SHIFT);
1462
1463 WREG32(mmPCI_NRTR_SCRAMB_EN, 1 << PCI_NRTR_SCRAMB_EN_VAL_SHIFT);
1464 WREG32(mmPCI_NRTR_NON_LIN_SCRAMB,
1465 1 << PCI_NRTR_NON_LIN_SCRAMB_EN_SHIFT);
1466
1467 /*
1468 * Workaround for H2 #HW-23 bug
1469 * Set DMA max outstanding read requests to 240 on DMA CH 1.
1470 * This limitation is still large enough to not affect Gen4 bandwidth.
1471 * We need to only limit that DMA channel because the user can only read
1472 * from Host using DMA CH 1
1473 */
1474 WREG32(mmDMA_CH_1_CFG0, 0x0fff00F0);
1475
1476 WREG32(mmTPC_PLL_CLK_RLX_0, 0x200020);
1477
1478 goya->hw_cap_initialized |= HW_CAP_GOLDEN;
1479 }
1480
goya_init_mme_qman(struct hl_device * hdev)1481 static void goya_init_mme_qman(struct hl_device *hdev)
1482 {
1483 u32 mtr_base_lo, mtr_base_hi;
1484 u32 so_base_lo, so_base_hi;
1485 u32 gic_base_lo, gic_base_hi;
1486 u64 qman_base_addr;
1487
1488 mtr_base_lo = lower_32_bits(CFG_BASE + mmSYNC_MNGR_MON_PAY_ADDRL_0);
1489 mtr_base_hi = upper_32_bits(CFG_BASE + mmSYNC_MNGR_MON_PAY_ADDRL_0);
1490 so_base_lo = lower_32_bits(CFG_BASE + mmSYNC_MNGR_SOB_OBJ_0);
1491 so_base_hi = upper_32_bits(CFG_BASE + mmSYNC_MNGR_SOB_OBJ_0);
1492
1493 gic_base_lo =
1494 lower_32_bits(CFG_BASE + mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR);
1495 gic_base_hi =
1496 upper_32_bits(CFG_BASE + mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR);
1497
1498 qman_base_addr = hdev->asic_prop.sram_base_address +
1499 MME_QMAN_BASE_OFFSET;
1500
1501 WREG32(mmMME_QM_PQ_BASE_LO, lower_32_bits(qman_base_addr));
1502 WREG32(mmMME_QM_PQ_BASE_HI, upper_32_bits(qman_base_addr));
1503 WREG32(mmMME_QM_PQ_SIZE, ilog2(MME_QMAN_LENGTH));
1504 WREG32(mmMME_QM_PQ_PI, 0);
1505 WREG32(mmMME_QM_PQ_CI, 0);
1506 WREG32(mmMME_QM_CP_LDMA_SRC_BASE_LO_OFFSET, 0x10C0);
1507 WREG32(mmMME_QM_CP_LDMA_SRC_BASE_HI_OFFSET, 0x10C4);
1508 WREG32(mmMME_QM_CP_LDMA_TSIZE_OFFSET, 0x10C8);
1509 WREG32(mmMME_QM_CP_LDMA_COMMIT_OFFSET, 0x10CC);
1510
1511 WREG32(mmMME_QM_CP_MSG_BASE0_ADDR_LO, mtr_base_lo);
1512 WREG32(mmMME_QM_CP_MSG_BASE0_ADDR_HI, mtr_base_hi);
1513 WREG32(mmMME_QM_CP_MSG_BASE1_ADDR_LO, so_base_lo);
1514 WREG32(mmMME_QM_CP_MSG_BASE1_ADDR_HI, so_base_hi);
1515
1516 /* QMAN CQ has 8 cache lines */
1517 WREG32(mmMME_QM_CQ_CFG1, 0x00080008);
1518
1519 WREG32(mmMME_QM_GLBL_ERR_ADDR_LO, gic_base_lo);
1520 WREG32(mmMME_QM_GLBL_ERR_ADDR_HI, gic_base_hi);
1521
1522 WREG32(mmMME_QM_GLBL_ERR_WDATA, GOYA_ASYNC_EVENT_ID_MME_QM);
1523
1524 WREG32(mmMME_QM_GLBL_ERR_CFG, QMAN_MME_ERR_MSG_EN);
1525
1526 WREG32(mmMME_QM_GLBL_PROT, QMAN_MME_ERR_PROT);
1527
1528 WREG32(mmMME_QM_GLBL_CFG0, QMAN_MME_ENABLE);
1529 }
1530
goya_init_mme_cmdq(struct hl_device * hdev)1531 static void goya_init_mme_cmdq(struct hl_device *hdev)
1532 {
1533 u32 mtr_base_lo, mtr_base_hi;
1534 u32 so_base_lo, so_base_hi;
1535 u32 gic_base_lo, gic_base_hi;
1536 u64 qman_base_addr;
1537
1538 mtr_base_lo = lower_32_bits(CFG_BASE + mmSYNC_MNGR_MON_PAY_ADDRL_0);
1539 mtr_base_hi = upper_32_bits(CFG_BASE + mmSYNC_MNGR_MON_PAY_ADDRL_0);
1540 so_base_lo = lower_32_bits(CFG_BASE + mmSYNC_MNGR_SOB_OBJ_0);
1541 so_base_hi = upper_32_bits(CFG_BASE + mmSYNC_MNGR_SOB_OBJ_0);
1542
1543 gic_base_lo =
1544 lower_32_bits(CFG_BASE + mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR);
1545 gic_base_hi =
1546 upper_32_bits(CFG_BASE + mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR);
1547
1548 qman_base_addr = hdev->asic_prop.sram_base_address +
1549 MME_QMAN_BASE_OFFSET;
1550
1551 WREG32(mmMME_CMDQ_CP_MSG_BASE0_ADDR_LO, mtr_base_lo);
1552 WREG32(mmMME_CMDQ_CP_MSG_BASE0_ADDR_HI, mtr_base_hi);
1553 WREG32(mmMME_CMDQ_CP_MSG_BASE1_ADDR_LO, so_base_lo);
1554 WREG32(mmMME_CMDQ_CP_MSG_BASE1_ADDR_HI, so_base_hi);
1555
1556 /* CMDQ CQ has 20 cache lines */
1557 WREG32(mmMME_CMDQ_CQ_CFG1, 0x00140014);
1558
1559 WREG32(mmMME_CMDQ_GLBL_ERR_ADDR_LO, gic_base_lo);
1560 WREG32(mmMME_CMDQ_GLBL_ERR_ADDR_HI, gic_base_hi);
1561
1562 WREG32(mmMME_CMDQ_GLBL_ERR_WDATA, GOYA_ASYNC_EVENT_ID_MME_CMDQ);
1563
1564 WREG32(mmMME_CMDQ_GLBL_ERR_CFG, CMDQ_MME_ERR_MSG_EN);
1565
1566 WREG32(mmMME_CMDQ_GLBL_PROT, CMDQ_MME_ERR_PROT);
1567
1568 WREG32(mmMME_CMDQ_GLBL_CFG0, CMDQ_MME_ENABLE);
1569 }
1570
goya_init_mme_qmans(struct hl_device * hdev)1571 void goya_init_mme_qmans(struct hl_device *hdev)
1572 {
1573 struct goya_device *goya = hdev->asic_specific;
1574 u32 so_base_lo, so_base_hi;
1575
1576 if (goya->hw_cap_initialized & HW_CAP_MME)
1577 return;
1578
1579 so_base_lo = lower_32_bits(CFG_BASE + mmSYNC_MNGR_SOB_OBJ_0);
1580 so_base_hi = upper_32_bits(CFG_BASE + mmSYNC_MNGR_SOB_OBJ_0);
1581
1582 WREG32(mmMME_SM_BASE_ADDRESS_LOW, so_base_lo);
1583 WREG32(mmMME_SM_BASE_ADDRESS_HIGH, so_base_hi);
1584
1585 goya_init_mme_qman(hdev);
1586 goya_init_mme_cmdq(hdev);
1587
1588 goya->hw_cap_initialized |= HW_CAP_MME;
1589 }
1590
goya_init_tpc_qman(struct hl_device * hdev,u32 base_off,int tpc_id)1591 static void goya_init_tpc_qman(struct hl_device *hdev, u32 base_off, int tpc_id)
1592 {
1593 u32 mtr_base_lo, mtr_base_hi;
1594 u32 so_base_lo, so_base_hi;
1595 u32 gic_base_lo, gic_base_hi;
1596 u64 qman_base_addr;
1597 u32 reg_off = tpc_id * (mmTPC1_QM_PQ_PI - mmTPC0_QM_PQ_PI);
1598
1599 mtr_base_lo = lower_32_bits(CFG_BASE + mmSYNC_MNGR_MON_PAY_ADDRL_0);
1600 mtr_base_hi = upper_32_bits(CFG_BASE + mmSYNC_MNGR_MON_PAY_ADDRL_0);
1601 so_base_lo = lower_32_bits(CFG_BASE + mmSYNC_MNGR_SOB_OBJ_0);
1602 so_base_hi = upper_32_bits(CFG_BASE + mmSYNC_MNGR_SOB_OBJ_0);
1603
1604 gic_base_lo =
1605 lower_32_bits(CFG_BASE + mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR);
1606 gic_base_hi =
1607 upper_32_bits(CFG_BASE + mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR);
1608
1609 qman_base_addr = hdev->asic_prop.sram_base_address + base_off;
1610
1611 WREG32(mmTPC0_QM_PQ_BASE_LO + reg_off, lower_32_bits(qman_base_addr));
1612 WREG32(mmTPC0_QM_PQ_BASE_HI + reg_off, upper_32_bits(qman_base_addr));
1613 WREG32(mmTPC0_QM_PQ_SIZE + reg_off, ilog2(TPC_QMAN_LENGTH));
1614 WREG32(mmTPC0_QM_PQ_PI + reg_off, 0);
1615 WREG32(mmTPC0_QM_PQ_CI + reg_off, 0);
1616 WREG32(mmTPC0_QM_CP_LDMA_SRC_BASE_LO_OFFSET + reg_off, 0x10C0);
1617 WREG32(mmTPC0_QM_CP_LDMA_SRC_BASE_HI_OFFSET + reg_off, 0x10C4);
1618 WREG32(mmTPC0_QM_CP_LDMA_TSIZE_OFFSET + reg_off, 0x10C8);
1619 WREG32(mmTPC0_QM_CP_LDMA_COMMIT_OFFSET + reg_off, 0x10CC);
1620
1621 WREG32(mmTPC0_QM_CP_MSG_BASE0_ADDR_LO + reg_off, mtr_base_lo);
1622 WREG32(mmTPC0_QM_CP_MSG_BASE0_ADDR_HI + reg_off, mtr_base_hi);
1623 WREG32(mmTPC0_QM_CP_MSG_BASE1_ADDR_LO + reg_off, so_base_lo);
1624 WREG32(mmTPC0_QM_CP_MSG_BASE1_ADDR_HI + reg_off, so_base_hi);
1625
1626 WREG32(mmTPC0_QM_CQ_CFG1 + reg_off, 0x00080008);
1627
1628 WREG32(mmTPC0_QM_GLBL_ERR_ADDR_LO + reg_off, gic_base_lo);
1629 WREG32(mmTPC0_QM_GLBL_ERR_ADDR_HI + reg_off, gic_base_hi);
1630
1631 WREG32(mmTPC0_QM_GLBL_ERR_WDATA + reg_off,
1632 GOYA_ASYNC_EVENT_ID_TPC0_QM + tpc_id);
1633
1634 WREG32(mmTPC0_QM_GLBL_ERR_CFG + reg_off, QMAN_TPC_ERR_MSG_EN);
1635
1636 WREG32(mmTPC0_QM_GLBL_PROT + reg_off, QMAN_TPC_ERR_PROT);
1637
1638 WREG32(mmTPC0_QM_GLBL_CFG0 + reg_off, QMAN_TPC_ENABLE);
1639 }
1640
goya_init_tpc_cmdq(struct hl_device * hdev,int tpc_id)1641 static void goya_init_tpc_cmdq(struct hl_device *hdev, int tpc_id)
1642 {
1643 u32 mtr_base_lo, mtr_base_hi;
1644 u32 so_base_lo, so_base_hi;
1645 u32 gic_base_lo, gic_base_hi;
1646 u32 reg_off = tpc_id * (mmTPC1_CMDQ_CQ_CFG1 - mmTPC0_CMDQ_CQ_CFG1);
1647
1648 mtr_base_lo = lower_32_bits(CFG_BASE + mmSYNC_MNGR_MON_PAY_ADDRL_0);
1649 mtr_base_hi = upper_32_bits(CFG_BASE + mmSYNC_MNGR_MON_PAY_ADDRL_0);
1650 so_base_lo = lower_32_bits(CFG_BASE + mmSYNC_MNGR_SOB_OBJ_0);
1651 so_base_hi = upper_32_bits(CFG_BASE + mmSYNC_MNGR_SOB_OBJ_0);
1652
1653 gic_base_lo =
1654 lower_32_bits(CFG_BASE + mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR);
1655 gic_base_hi =
1656 upper_32_bits(CFG_BASE + mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR);
1657
1658 WREG32(mmTPC0_CMDQ_CP_MSG_BASE0_ADDR_LO + reg_off, mtr_base_lo);
1659 WREG32(mmTPC0_CMDQ_CP_MSG_BASE0_ADDR_HI + reg_off, mtr_base_hi);
1660 WREG32(mmTPC0_CMDQ_CP_MSG_BASE1_ADDR_LO + reg_off, so_base_lo);
1661 WREG32(mmTPC0_CMDQ_CP_MSG_BASE1_ADDR_HI + reg_off, so_base_hi);
1662
1663 WREG32(mmTPC0_CMDQ_CQ_CFG1 + reg_off, 0x00140014);
1664
1665 WREG32(mmTPC0_CMDQ_GLBL_ERR_ADDR_LO + reg_off, gic_base_lo);
1666 WREG32(mmTPC0_CMDQ_GLBL_ERR_ADDR_HI + reg_off, gic_base_hi);
1667
1668 WREG32(mmTPC0_CMDQ_GLBL_ERR_WDATA + reg_off,
1669 GOYA_ASYNC_EVENT_ID_TPC0_CMDQ + tpc_id);
1670
1671 WREG32(mmTPC0_CMDQ_GLBL_ERR_CFG + reg_off, CMDQ_TPC_ERR_MSG_EN);
1672
1673 WREG32(mmTPC0_CMDQ_GLBL_PROT + reg_off, CMDQ_TPC_ERR_PROT);
1674
1675 WREG32(mmTPC0_CMDQ_GLBL_CFG0 + reg_off, CMDQ_TPC_ENABLE);
1676 }
1677
goya_init_tpc_qmans(struct hl_device * hdev)1678 void goya_init_tpc_qmans(struct hl_device *hdev)
1679 {
1680 struct goya_device *goya = hdev->asic_specific;
1681 u32 so_base_lo, so_base_hi;
1682 u32 cfg_off = mmTPC1_CFG_SM_BASE_ADDRESS_LOW -
1683 mmTPC0_CFG_SM_BASE_ADDRESS_LOW;
1684 int i;
1685
1686 if (goya->hw_cap_initialized & HW_CAP_TPC)
1687 return;
1688
1689 so_base_lo = lower_32_bits(CFG_BASE + mmSYNC_MNGR_SOB_OBJ_0);
1690 so_base_hi = upper_32_bits(CFG_BASE + mmSYNC_MNGR_SOB_OBJ_0);
1691
1692 for (i = 0 ; i < TPC_MAX_NUM ; i++) {
1693 WREG32(mmTPC0_CFG_SM_BASE_ADDRESS_LOW + i * cfg_off,
1694 so_base_lo);
1695 WREG32(mmTPC0_CFG_SM_BASE_ADDRESS_HIGH + i * cfg_off,
1696 so_base_hi);
1697 }
1698
1699 goya_init_tpc_qman(hdev, TPC0_QMAN_BASE_OFFSET, 0);
1700 goya_init_tpc_qman(hdev, TPC1_QMAN_BASE_OFFSET, 1);
1701 goya_init_tpc_qman(hdev, TPC2_QMAN_BASE_OFFSET, 2);
1702 goya_init_tpc_qman(hdev, TPC3_QMAN_BASE_OFFSET, 3);
1703 goya_init_tpc_qman(hdev, TPC4_QMAN_BASE_OFFSET, 4);
1704 goya_init_tpc_qman(hdev, TPC5_QMAN_BASE_OFFSET, 5);
1705 goya_init_tpc_qman(hdev, TPC6_QMAN_BASE_OFFSET, 6);
1706 goya_init_tpc_qman(hdev, TPC7_QMAN_BASE_OFFSET, 7);
1707
1708 for (i = 0 ; i < TPC_MAX_NUM ; i++)
1709 goya_init_tpc_cmdq(hdev, i);
1710
1711 goya->hw_cap_initialized |= HW_CAP_TPC;
1712 }
1713
1714 /*
1715 * goya_disable_internal_queues - Disable internal queues
1716 *
1717 * @hdev: pointer to hl_device structure
1718 *
1719 */
goya_disable_internal_queues(struct hl_device * hdev)1720 static void goya_disable_internal_queues(struct hl_device *hdev)
1721 {
1722 WREG32(mmMME_QM_GLBL_CFG0, 0);
1723 WREG32(mmMME_CMDQ_GLBL_CFG0, 0);
1724
1725 WREG32(mmTPC0_QM_GLBL_CFG0, 0);
1726 WREG32(mmTPC0_CMDQ_GLBL_CFG0, 0);
1727
1728 WREG32(mmTPC1_QM_GLBL_CFG0, 0);
1729 WREG32(mmTPC1_CMDQ_GLBL_CFG0, 0);
1730
1731 WREG32(mmTPC2_QM_GLBL_CFG0, 0);
1732 WREG32(mmTPC2_CMDQ_GLBL_CFG0, 0);
1733
1734 WREG32(mmTPC3_QM_GLBL_CFG0, 0);
1735 WREG32(mmTPC3_CMDQ_GLBL_CFG0, 0);
1736
1737 WREG32(mmTPC4_QM_GLBL_CFG0, 0);
1738 WREG32(mmTPC4_CMDQ_GLBL_CFG0, 0);
1739
1740 WREG32(mmTPC5_QM_GLBL_CFG0, 0);
1741 WREG32(mmTPC5_CMDQ_GLBL_CFG0, 0);
1742
1743 WREG32(mmTPC6_QM_GLBL_CFG0, 0);
1744 WREG32(mmTPC6_CMDQ_GLBL_CFG0, 0);
1745
1746 WREG32(mmTPC7_QM_GLBL_CFG0, 0);
1747 WREG32(mmTPC7_CMDQ_GLBL_CFG0, 0);
1748 }
1749
1750 /*
1751 * goya_stop_internal_queues - Stop internal queues
1752 *
1753 * @hdev: pointer to hl_device structure
1754 *
1755 * Returns 0 on success
1756 *
1757 */
goya_stop_internal_queues(struct hl_device * hdev)1758 static int goya_stop_internal_queues(struct hl_device *hdev)
1759 {
1760 int rc, retval = 0;
1761
1762 /*
1763 * Each queue (QMAN) is a separate H/W logic. That means that each
1764 * QMAN can be stopped independently and failure to stop one does NOT
1765 * mandate we should not try to stop other QMANs
1766 */
1767
1768 rc = goya_stop_queue(hdev,
1769 mmMME_QM_GLBL_CFG1,
1770 mmMME_QM_CP_STS,
1771 mmMME_QM_GLBL_STS0);
1772
1773 if (rc) {
1774 dev_err(hdev->dev, "failed to stop MME QMAN\n");
1775 retval = -EIO;
1776 }
1777
1778 rc = goya_stop_queue(hdev,
1779 mmMME_CMDQ_GLBL_CFG1,
1780 mmMME_CMDQ_CP_STS,
1781 mmMME_CMDQ_GLBL_STS0);
1782
1783 if (rc) {
1784 dev_err(hdev->dev, "failed to stop MME CMDQ\n");
1785 retval = -EIO;
1786 }
1787
1788 rc = goya_stop_queue(hdev,
1789 mmTPC0_QM_GLBL_CFG1,
1790 mmTPC0_QM_CP_STS,
1791 mmTPC0_QM_GLBL_STS0);
1792
1793 if (rc) {
1794 dev_err(hdev->dev, "failed to stop TPC 0 QMAN\n");
1795 retval = -EIO;
1796 }
1797
1798 rc = goya_stop_queue(hdev,
1799 mmTPC0_CMDQ_GLBL_CFG1,
1800 mmTPC0_CMDQ_CP_STS,
1801 mmTPC0_CMDQ_GLBL_STS0);
1802
1803 if (rc) {
1804 dev_err(hdev->dev, "failed to stop TPC 0 CMDQ\n");
1805 retval = -EIO;
1806 }
1807
1808 rc = goya_stop_queue(hdev,
1809 mmTPC1_QM_GLBL_CFG1,
1810 mmTPC1_QM_CP_STS,
1811 mmTPC1_QM_GLBL_STS0);
1812
1813 if (rc) {
1814 dev_err(hdev->dev, "failed to stop TPC 1 QMAN\n");
1815 retval = -EIO;
1816 }
1817
1818 rc = goya_stop_queue(hdev,
1819 mmTPC1_CMDQ_GLBL_CFG1,
1820 mmTPC1_CMDQ_CP_STS,
1821 mmTPC1_CMDQ_GLBL_STS0);
1822
1823 if (rc) {
1824 dev_err(hdev->dev, "failed to stop TPC 1 CMDQ\n");
1825 retval = -EIO;
1826 }
1827
1828 rc = goya_stop_queue(hdev,
1829 mmTPC2_QM_GLBL_CFG1,
1830 mmTPC2_QM_CP_STS,
1831 mmTPC2_QM_GLBL_STS0);
1832
1833 if (rc) {
1834 dev_err(hdev->dev, "failed to stop TPC 2 QMAN\n");
1835 retval = -EIO;
1836 }
1837
1838 rc = goya_stop_queue(hdev,
1839 mmTPC2_CMDQ_GLBL_CFG1,
1840 mmTPC2_CMDQ_CP_STS,
1841 mmTPC2_CMDQ_GLBL_STS0);
1842
1843 if (rc) {
1844 dev_err(hdev->dev, "failed to stop TPC 2 CMDQ\n");
1845 retval = -EIO;
1846 }
1847
1848 rc = goya_stop_queue(hdev,
1849 mmTPC3_QM_GLBL_CFG1,
1850 mmTPC3_QM_CP_STS,
1851 mmTPC3_QM_GLBL_STS0);
1852
1853 if (rc) {
1854 dev_err(hdev->dev, "failed to stop TPC 3 QMAN\n");
1855 retval = -EIO;
1856 }
1857
1858 rc = goya_stop_queue(hdev,
1859 mmTPC3_CMDQ_GLBL_CFG1,
1860 mmTPC3_CMDQ_CP_STS,
1861 mmTPC3_CMDQ_GLBL_STS0);
1862
1863 if (rc) {
1864 dev_err(hdev->dev, "failed to stop TPC 3 CMDQ\n");
1865 retval = -EIO;
1866 }
1867
1868 rc = goya_stop_queue(hdev,
1869 mmTPC4_QM_GLBL_CFG1,
1870 mmTPC4_QM_CP_STS,
1871 mmTPC4_QM_GLBL_STS0);
1872
1873 if (rc) {
1874 dev_err(hdev->dev, "failed to stop TPC 4 QMAN\n");
1875 retval = -EIO;
1876 }
1877
1878 rc = goya_stop_queue(hdev,
1879 mmTPC4_CMDQ_GLBL_CFG1,
1880 mmTPC4_CMDQ_CP_STS,
1881 mmTPC4_CMDQ_GLBL_STS0);
1882
1883 if (rc) {
1884 dev_err(hdev->dev, "failed to stop TPC 4 CMDQ\n");
1885 retval = -EIO;
1886 }
1887
1888 rc = goya_stop_queue(hdev,
1889 mmTPC5_QM_GLBL_CFG1,
1890 mmTPC5_QM_CP_STS,
1891 mmTPC5_QM_GLBL_STS0);
1892
1893 if (rc) {
1894 dev_err(hdev->dev, "failed to stop TPC 5 QMAN\n");
1895 retval = -EIO;
1896 }
1897
1898 rc = goya_stop_queue(hdev,
1899 mmTPC5_CMDQ_GLBL_CFG1,
1900 mmTPC5_CMDQ_CP_STS,
1901 mmTPC5_CMDQ_GLBL_STS0);
1902
1903 if (rc) {
1904 dev_err(hdev->dev, "failed to stop TPC 5 CMDQ\n");
1905 retval = -EIO;
1906 }
1907
1908 rc = goya_stop_queue(hdev,
1909 mmTPC6_QM_GLBL_CFG1,
1910 mmTPC6_QM_CP_STS,
1911 mmTPC6_QM_GLBL_STS0);
1912
1913 if (rc) {
1914 dev_err(hdev->dev, "failed to stop TPC 6 QMAN\n");
1915 retval = -EIO;
1916 }
1917
1918 rc = goya_stop_queue(hdev,
1919 mmTPC6_CMDQ_GLBL_CFG1,
1920 mmTPC6_CMDQ_CP_STS,
1921 mmTPC6_CMDQ_GLBL_STS0);
1922
1923 if (rc) {
1924 dev_err(hdev->dev, "failed to stop TPC 6 CMDQ\n");
1925 retval = -EIO;
1926 }
1927
1928 rc = goya_stop_queue(hdev,
1929 mmTPC7_QM_GLBL_CFG1,
1930 mmTPC7_QM_CP_STS,
1931 mmTPC7_QM_GLBL_STS0);
1932
1933 if (rc) {
1934 dev_err(hdev->dev, "failed to stop TPC 7 QMAN\n");
1935 retval = -EIO;
1936 }
1937
1938 rc = goya_stop_queue(hdev,
1939 mmTPC7_CMDQ_GLBL_CFG1,
1940 mmTPC7_CMDQ_CP_STS,
1941 mmTPC7_CMDQ_GLBL_STS0);
1942
1943 if (rc) {
1944 dev_err(hdev->dev, "failed to stop TPC 7 CMDQ\n");
1945 retval = -EIO;
1946 }
1947
1948 return retval;
1949 }
1950
goya_dma_stall(struct hl_device * hdev)1951 static void goya_dma_stall(struct hl_device *hdev)
1952 {
1953 WREG32(mmDMA_QM_0_GLBL_CFG1, 1 << DMA_QM_0_GLBL_CFG1_DMA_STOP_SHIFT);
1954 WREG32(mmDMA_QM_1_GLBL_CFG1, 1 << DMA_QM_1_GLBL_CFG1_DMA_STOP_SHIFT);
1955 WREG32(mmDMA_QM_2_GLBL_CFG1, 1 << DMA_QM_2_GLBL_CFG1_DMA_STOP_SHIFT);
1956 WREG32(mmDMA_QM_3_GLBL_CFG1, 1 << DMA_QM_3_GLBL_CFG1_DMA_STOP_SHIFT);
1957 WREG32(mmDMA_QM_4_GLBL_CFG1, 1 << DMA_QM_4_GLBL_CFG1_DMA_STOP_SHIFT);
1958 }
1959
goya_tpc_stall(struct hl_device * hdev)1960 static void goya_tpc_stall(struct hl_device *hdev)
1961 {
1962 WREG32(mmTPC0_CFG_TPC_STALL, 1 << TPC0_CFG_TPC_STALL_V_SHIFT);
1963 WREG32(mmTPC1_CFG_TPC_STALL, 1 << TPC1_CFG_TPC_STALL_V_SHIFT);
1964 WREG32(mmTPC2_CFG_TPC_STALL, 1 << TPC2_CFG_TPC_STALL_V_SHIFT);
1965 WREG32(mmTPC3_CFG_TPC_STALL, 1 << TPC3_CFG_TPC_STALL_V_SHIFT);
1966 WREG32(mmTPC4_CFG_TPC_STALL, 1 << TPC4_CFG_TPC_STALL_V_SHIFT);
1967 WREG32(mmTPC5_CFG_TPC_STALL, 1 << TPC5_CFG_TPC_STALL_V_SHIFT);
1968 WREG32(mmTPC6_CFG_TPC_STALL, 1 << TPC6_CFG_TPC_STALL_V_SHIFT);
1969 WREG32(mmTPC7_CFG_TPC_STALL, 1 << TPC7_CFG_TPC_STALL_V_SHIFT);
1970 }
1971
goya_mme_stall(struct hl_device * hdev)1972 static void goya_mme_stall(struct hl_device *hdev)
1973 {
1974 WREG32(mmMME_STALL, 0xFFFFFFFF);
1975 }
1976
goya_enable_msix(struct hl_device * hdev)1977 static int goya_enable_msix(struct hl_device *hdev)
1978 {
1979 struct goya_device *goya = hdev->asic_specific;
1980 int cq_cnt = hdev->asic_prop.completion_queues_count;
1981 int rc, i, irq_cnt_init, irq;
1982
1983 if (goya->hw_cap_initialized & HW_CAP_MSIX)
1984 return 0;
1985
1986 rc = pci_alloc_irq_vectors(hdev->pdev, GOYA_MSIX_ENTRIES,
1987 GOYA_MSIX_ENTRIES, PCI_IRQ_MSIX);
1988 if (rc < 0) {
1989 dev_err(hdev->dev,
1990 "MSI-X: Failed to enable support -- %d/%d\n",
1991 GOYA_MSIX_ENTRIES, rc);
1992 return rc;
1993 }
1994
1995 for (i = 0, irq_cnt_init = 0 ; i < cq_cnt ; i++, irq_cnt_init++) {
1996 irq = pci_irq_vector(hdev->pdev, i);
1997 rc = request_irq(irq, hl_irq_handler_cq, 0, goya_irq_name[i],
1998 &hdev->completion_queue[i]);
1999 if (rc) {
2000 dev_err(hdev->dev, "Failed to request IRQ %d", irq);
2001 goto free_irqs;
2002 }
2003 }
2004
2005 irq = pci_irq_vector(hdev->pdev, GOYA_EVENT_QUEUE_MSIX_IDX);
2006
2007 rc = request_irq(irq, hl_irq_handler_eq, 0,
2008 goya_irq_name[GOYA_EVENT_QUEUE_MSIX_IDX],
2009 &hdev->event_queue);
2010 if (rc) {
2011 dev_err(hdev->dev, "Failed to request IRQ %d", irq);
2012 goto free_irqs;
2013 }
2014
2015 goya->hw_cap_initialized |= HW_CAP_MSIX;
2016 return 0;
2017
2018 free_irqs:
2019 for (i = 0 ; i < irq_cnt_init ; i++)
2020 free_irq(pci_irq_vector(hdev->pdev, i),
2021 &hdev->completion_queue[i]);
2022
2023 pci_free_irq_vectors(hdev->pdev);
2024 return rc;
2025 }
2026
goya_sync_irqs(struct hl_device * hdev)2027 static void goya_sync_irqs(struct hl_device *hdev)
2028 {
2029 struct goya_device *goya = hdev->asic_specific;
2030 int i;
2031
2032 if (!(goya->hw_cap_initialized & HW_CAP_MSIX))
2033 return;
2034
2035 /* Wait for all pending IRQs to be finished */
2036 for (i = 0 ; i < hdev->asic_prop.completion_queues_count ; i++)
2037 synchronize_irq(pci_irq_vector(hdev->pdev, i));
2038
2039 synchronize_irq(pci_irq_vector(hdev->pdev, GOYA_EVENT_QUEUE_MSIX_IDX));
2040 }
2041
goya_disable_msix(struct hl_device * hdev)2042 static void goya_disable_msix(struct hl_device *hdev)
2043 {
2044 struct goya_device *goya = hdev->asic_specific;
2045 int i, irq;
2046
2047 if (!(goya->hw_cap_initialized & HW_CAP_MSIX))
2048 return;
2049
2050 goya_sync_irqs(hdev);
2051
2052 irq = pci_irq_vector(hdev->pdev, GOYA_EVENT_QUEUE_MSIX_IDX);
2053 free_irq(irq, &hdev->event_queue);
2054
2055 for (i = 0 ; i < hdev->asic_prop.completion_queues_count ; i++) {
2056 irq = pci_irq_vector(hdev->pdev, i);
2057 free_irq(irq, &hdev->completion_queue[i]);
2058 }
2059
2060 pci_free_irq_vectors(hdev->pdev);
2061
2062 goya->hw_cap_initialized &= ~HW_CAP_MSIX;
2063 }
2064
goya_enable_timestamp(struct hl_device * hdev)2065 static void goya_enable_timestamp(struct hl_device *hdev)
2066 {
2067 /* Disable the timestamp counter */
2068 WREG32(mmPSOC_TIMESTAMP_BASE - CFG_BASE, 0);
2069
2070 /* Zero the lower/upper parts of the 64-bit counter */
2071 WREG32(mmPSOC_TIMESTAMP_BASE - CFG_BASE + 0xC, 0);
2072 WREG32(mmPSOC_TIMESTAMP_BASE - CFG_BASE + 0x8, 0);
2073
2074 /* Enable the counter */
2075 WREG32(mmPSOC_TIMESTAMP_BASE - CFG_BASE, 1);
2076 }
2077
goya_disable_timestamp(struct hl_device * hdev)2078 static void goya_disable_timestamp(struct hl_device *hdev)
2079 {
2080 /* Disable the timestamp counter */
2081 WREG32(mmPSOC_TIMESTAMP_BASE - CFG_BASE, 0);
2082 }
2083
goya_halt_engines(struct hl_device * hdev,bool hard_reset)2084 static void goya_halt_engines(struct hl_device *hdev, bool hard_reset)
2085 {
2086 u32 wait_timeout_ms, cpu_timeout_ms;
2087
2088 dev_info(hdev->dev,
2089 "Halting compute engines and disabling interrupts\n");
2090
2091 if (hdev->pldm) {
2092 wait_timeout_ms = GOYA_PLDM_RESET_WAIT_MSEC;
2093 cpu_timeout_ms = GOYA_PLDM_RESET_WAIT_MSEC;
2094 } else {
2095 wait_timeout_ms = GOYA_RESET_WAIT_MSEC;
2096 cpu_timeout_ms = GOYA_CPU_RESET_WAIT_MSEC;
2097 }
2098
2099 if (hard_reset) {
2100 /*
2101 * I don't know what is the state of the CPU so make sure it is
2102 * stopped in any means necessary
2103 */
2104 WREG32(mmPSOC_GLOBAL_CONF_UBOOT_MAGIC, KMD_MSG_GOTO_WFE);
2105 WREG32(mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR,
2106 GOYA_ASYNC_EVENT_ID_HALT_MACHINE);
2107 msleep(cpu_timeout_ms);
2108 }
2109
2110 goya_stop_external_queues(hdev);
2111 goya_stop_internal_queues(hdev);
2112
2113 msleep(wait_timeout_ms);
2114
2115 goya_dma_stall(hdev);
2116 goya_tpc_stall(hdev);
2117 goya_mme_stall(hdev);
2118
2119 msleep(wait_timeout_ms);
2120
2121 goya_disable_external_queues(hdev);
2122 goya_disable_internal_queues(hdev);
2123
2124 goya_disable_timestamp(hdev);
2125
2126 if (hard_reset) {
2127 goya_disable_msix(hdev);
2128 goya_mmu_remove_device_cpu_mappings(hdev);
2129 } else {
2130 goya_sync_irqs(hdev);
2131 }
2132 }
2133
2134 /*
2135 * goya_push_uboot_to_device() - Push u-boot FW code to device.
2136 * @hdev: Pointer to hl_device structure.
2137 *
2138 * Copy u-boot fw code from firmware file to SRAM BAR.
2139 *
2140 * Return: 0 on success, non-zero for failure.
2141 */
goya_push_uboot_to_device(struct hl_device * hdev)2142 static int goya_push_uboot_to_device(struct hl_device *hdev)
2143 {
2144 char fw_name[200];
2145 void __iomem *dst;
2146
2147 snprintf(fw_name, sizeof(fw_name), "habanalabs/goya/goya-u-boot.bin");
2148 dst = hdev->pcie_bar[SRAM_CFG_BAR_ID] + UBOOT_FW_OFFSET;
2149
2150 return hl_fw_push_fw_to_device(hdev, fw_name, dst);
2151 }
2152
2153 /*
2154 * goya_push_linux_to_device() - Push LINUX FW code to device.
2155 * @hdev: Pointer to hl_device structure.
2156 *
2157 * Copy LINUX fw code from firmware file to HBM BAR.
2158 *
2159 * Return: 0 on success, non-zero for failure.
2160 */
goya_push_linux_to_device(struct hl_device * hdev)2161 static int goya_push_linux_to_device(struct hl_device *hdev)
2162 {
2163 char fw_name[200];
2164 void __iomem *dst;
2165
2166 snprintf(fw_name, sizeof(fw_name), "habanalabs/goya/goya-fit.itb");
2167 dst = hdev->pcie_bar[DDR_BAR_ID] + LINUX_FW_OFFSET;
2168
2169 return hl_fw_push_fw_to_device(hdev, fw_name, dst);
2170 }
2171
goya_pldm_init_cpu(struct hl_device * hdev)2172 static int goya_pldm_init_cpu(struct hl_device *hdev)
2173 {
2174 u32 unit_rst_val;
2175 int rc;
2176
2177 /* Must initialize SRAM scrambler before pushing u-boot to SRAM */
2178 goya_init_golden_registers(hdev);
2179
2180 /* Put ARM cores into reset */
2181 WREG32(mmCPU_CA53_CFG_ARM_RST_CONTROL, CPU_RESET_ASSERT);
2182 RREG32(mmCPU_CA53_CFG_ARM_RST_CONTROL);
2183
2184 /* Reset the CA53 MACRO */
2185 unit_rst_val = RREG32(mmPSOC_GLOBAL_CONF_UNIT_RST_N);
2186 WREG32(mmPSOC_GLOBAL_CONF_UNIT_RST_N, CA53_RESET);
2187 RREG32(mmPSOC_GLOBAL_CONF_UNIT_RST_N);
2188 WREG32(mmPSOC_GLOBAL_CONF_UNIT_RST_N, unit_rst_val);
2189 RREG32(mmPSOC_GLOBAL_CONF_UNIT_RST_N);
2190
2191 rc = goya_push_uboot_to_device(hdev);
2192 if (rc)
2193 return rc;
2194
2195 rc = goya_push_linux_to_device(hdev);
2196 if (rc)
2197 return rc;
2198
2199 WREG32(mmPSOC_GLOBAL_CONF_UBOOT_MAGIC, KMD_MSG_FIT_RDY);
2200 WREG32(mmPSOC_GLOBAL_CONF_WARM_REBOOT, CPU_BOOT_STATUS_NA);
2201
2202 WREG32(mmCPU_CA53_CFG_RST_ADDR_LSB_0,
2203 lower_32_bits(SRAM_BASE_ADDR + UBOOT_FW_OFFSET));
2204 WREG32(mmCPU_CA53_CFG_RST_ADDR_MSB_0,
2205 upper_32_bits(SRAM_BASE_ADDR + UBOOT_FW_OFFSET));
2206
2207 /* Release ARM core 0 from reset */
2208 WREG32(mmCPU_CA53_CFG_ARM_RST_CONTROL,
2209 CPU_RESET_CORE0_DEASSERT);
2210 RREG32(mmCPU_CA53_CFG_ARM_RST_CONTROL);
2211
2212 return 0;
2213 }
2214
2215 /*
2216 * FW component passes an offset from SRAM_BASE_ADDR in SCRATCHPAD_xx.
2217 * The version string should be located by that offset.
2218 */
goya_read_device_fw_version(struct hl_device * hdev,enum goya_fw_component fwc)2219 static void goya_read_device_fw_version(struct hl_device *hdev,
2220 enum goya_fw_component fwc)
2221 {
2222 const char *name;
2223 u32 ver_off;
2224 char *dest;
2225
2226 switch (fwc) {
2227 case FW_COMP_UBOOT:
2228 ver_off = RREG32(mmUBOOT_VER_OFFSET);
2229 dest = hdev->asic_prop.uboot_ver;
2230 name = "U-Boot";
2231 break;
2232 case FW_COMP_PREBOOT:
2233 ver_off = RREG32(mmPREBOOT_VER_OFFSET);
2234 dest = hdev->asic_prop.preboot_ver;
2235 name = "Preboot";
2236 break;
2237 default:
2238 dev_warn(hdev->dev, "Undefined FW component: %d\n", fwc);
2239 return;
2240 }
2241
2242 ver_off &= ~((u32)SRAM_BASE_ADDR);
2243
2244 if (ver_off < SRAM_SIZE - VERSION_MAX_LEN) {
2245 memcpy_fromio(dest, hdev->pcie_bar[SRAM_CFG_BAR_ID] + ver_off,
2246 VERSION_MAX_LEN);
2247 } else {
2248 dev_err(hdev->dev, "%s version offset (0x%x) is above SRAM\n",
2249 name, ver_off);
2250 strcpy(dest, "unavailable");
2251 }
2252 }
2253
goya_init_cpu(struct hl_device * hdev,u32 cpu_timeout)2254 static int goya_init_cpu(struct hl_device *hdev, u32 cpu_timeout)
2255 {
2256 struct goya_device *goya = hdev->asic_specific;
2257 u32 status;
2258 int rc;
2259
2260 if (!hdev->cpu_enable)
2261 return 0;
2262
2263 if (goya->hw_cap_initialized & HW_CAP_CPU)
2264 return 0;
2265
2266 /*
2267 * Before pushing u-boot/linux to device, need to set the ddr bar to
2268 * base address of dram
2269 */
2270 if (goya_set_ddr_bar_base(hdev, DRAM_PHYS_BASE) == U64_MAX) {
2271 dev_err(hdev->dev,
2272 "failed to map DDR bar to DRAM base address\n");
2273 return -EIO;
2274 }
2275
2276 if (hdev->pldm) {
2277 rc = goya_pldm_init_cpu(hdev);
2278 if (rc)
2279 return rc;
2280
2281 goto out;
2282 }
2283
2284 /* Make sure CPU boot-loader is running */
2285 rc = hl_poll_timeout(
2286 hdev,
2287 mmPSOC_GLOBAL_CONF_WARM_REBOOT,
2288 status,
2289 (status == CPU_BOOT_STATUS_DRAM_RDY) ||
2290 (status == CPU_BOOT_STATUS_SRAM_AVAIL),
2291 10000,
2292 cpu_timeout);
2293
2294 if (rc) {
2295 dev_err(hdev->dev, "Error in ARM u-boot!");
2296 switch (status) {
2297 case CPU_BOOT_STATUS_NA:
2298 dev_err(hdev->dev,
2299 "ARM status %d - BTL did NOT run\n", status);
2300 break;
2301 case CPU_BOOT_STATUS_IN_WFE:
2302 dev_err(hdev->dev,
2303 "ARM status %d - Inside WFE loop\n", status);
2304 break;
2305 case CPU_BOOT_STATUS_IN_BTL:
2306 dev_err(hdev->dev,
2307 "ARM status %d - Stuck in BTL\n", status);
2308 break;
2309 case CPU_BOOT_STATUS_IN_PREBOOT:
2310 dev_err(hdev->dev,
2311 "ARM status %d - Stuck in Preboot\n", status);
2312 break;
2313 case CPU_BOOT_STATUS_IN_SPL:
2314 dev_err(hdev->dev,
2315 "ARM status %d - Stuck in SPL\n", status);
2316 break;
2317 case CPU_BOOT_STATUS_IN_UBOOT:
2318 dev_err(hdev->dev,
2319 "ARM status %d - Stuck in u-boot\n", status);
2320 break;
2321 case CPU_BOOT_STATUS_DRAM_INIT_FAIL:
2322 dev_err(hdev->dev,
2323 "ARM status %d - DDR initialization failed\n",
2324 status);
2325 break;
2326 case CPU_BOOT_STATUS_UBOOT_NOT_READY:
2327 dev_err(hdev->dev,
2328 "ARM status %d - u-boot stopped by user\n",
2329 status);
2330 break;
2331 default:
2332 dev_err(hdev->dev,
2333 "ARM status %d - Invalid status code\n",
2334 status);
2335 break;
2336 }
2337 return -EIO;
2338 }
2339
2340 /* Read U-Boot version now in case we will later fail */
2341 goya_read_device_fw_version(hdev, FW_COMP_UBOOT);
2342 goya_read_device_fw_version(hdev, FW_COMP_PREBOOT);
2343
2344 if (!hdev->fw_loading) {
2345 dev_info(hdev->dev, "Skip loading FW\n");
2346 goto out;
2347 }
2348
2349 if (status == CPU_BOOT_STATUS_SRAM_AVAIL)
2350 goto out;
2351
2352 rc = goya_push_linux_to_device(hdev);
2353 if (rc)
2354 return rc;
2355
2356 WREG32(mmPSOC_GLOBAL_CONF_UBOOT_MAGIC, KMD_MSG_FIT_RDY);
2357
2358 rc = hl_poll_timeout(
2359 hdev,
2360 mmPSOC_GLOBAL_CONF_WARM_REBOOT,
2361 status,
2362 (status == CPU_BOOT_STATUS_SRAM_AVAIL),
2363 10000,
2364 cpu_timeout);
2365
2366 if (rc) {
2367 if (status == CPU_BOOT_STATUS_FIT_CORRUPTED)
2368 dev_err(hdev->dev,
2369 "ARM u-boot reports FIT image is corrupted\n");
2370 else
2371 dev_err(hdev->dev,
2372 "ARM Linux failed to load, %d\n", status);
2373 WREG32(mmPSOC_GLOBAL_CONF_UBOOT_MAGIC, KMD_MSG_NA);
2374 return -EIO;
2375 }
2376
2377 dev_info(hdev->dev, "Successfully loaded firmware to device\n");
2378
2379 out:
2380 goya->hw_cap_initialized |= HW_CAP_CPU;
2381
2382 return 0;
2383 }
2384
goya_mmu_update_asid_hop0_addr(struct hl_device * hdev,u32 asid,u64 phys_addr)2385 static int goya_mmu_update_asid_hop0_addr(struct hl_device *hdev, u32 asid,
2386 u64 phys_addr)
2387 {
2388 u32 status, timeout_usec;
2389 int rc;
2390
2391 if (hdev->pldm)
2392 timeout_usec = GOYA_PLDM_MMU_TIMEOUT_USEC;
2393 else
2394 timeout_usec = MMU_CONFIG_TIMEOUT_USEC;
2395
2396 WREG32(MMU_HOP0_PA43_12, phys_addr >> MMU_HOP0_PA43_12_SHIFT);
2397 WREG32(MMU_HOP0_PA49_44, phys_addr >> MMU_HOP0_PA49_44_SHIFT);
2398 WREG32(MMU_ASID_BUSY, 0x80000000 | asid);
2399
2400 rc = hl_poll_timeout(
2401 hdev,
2402 MMU_ASID_BUSY,
2403 status,
2404 !(status & 0x80000000),
2405 1000,
2406 timeout_usec);
2407
2408 if (rc) {
2409 dev_err(hdev->dev,
2410 "Timeout during MMU hop0 config of asid %d\n", asid);
2411 return rc;
2412 }
2413
2414 return 0;
2415 }
2416
goya_mmu_init(struct hl_device * hdev)2417 int goya_mmu_init(struct hl_device *hdev)
2418 {
2419 struct asic_fixed_properties *prop = &hdev->asic_prop;
2420 struct goya_device *goya = hdev->asic_specific;
2421 u64 hop0_addr;
2422 int rc, i;
2423
2424 if (!hdev->mmu_enable)
2425 return 0;
2426
2427 if (goya->hw_cap_initialized & HW_CAP_MMU)
2428 return 0;
2429
2430 hdev->dram_supports_virtual_memory = true;
2431 hdev->dram_default_page_mapping = true;
2432
2433 for (i = 0 ; i < prop->max_asid ; i++) {
2434 hop0_addr = prop->mmu_pgt_addr +
2435 (i * prop->mmu_hop_table_size);
2436
2437 rc = goya_mmu_update_asid_hop0_addr(hdev, i, hop0_addr);
2438 if (rc) {
2439 dev_err(hdev->dev,
2440 "failed to set hop0 addr for asid %d\n", i);
2441 goto err;
2442 }
2443 }
2444
2445 goya->hw_cap_initialized |= HW_CAP_MMU;
2446
2447 /* init MMU cache manage page */
2448 WREG32(mmSTLB_CACHE_INV_BASE_39_8,
2449 lower_32_bits(MMU_CACHE_MNG_ADDR >> 8));
2450 WREG32(mmSTLB_CACHE_INV_BASE_49_40, MMU_CACHE_MNG_ADDR >> 40);
2451
2452 /* Remove follower feature due to performance bug */
2453 WREG32_AND(mmSTLB_STLB_FEATURE_EN,
2454 (~STLB_STLB_FEATURE_EN_FOLLOWER_EN_MASK));
2455
2456 hdev->asic_funcs->mmu_invalidate_cache(hdev, true);
2457
2458 WREG32(mmMMU_MMU_ENABLE, 1);
2459 WREG32(mmMMU_SPI_MASK, 0xF);
2460
2461 return 0;
2462
2463 err:
2464 return rc;
2465 }
2466
2467 /*
2468 * goya_hw_init - Goya hardware initialization code
2469 *
2470 * @hdev: pointer to hl_device structure
2471 *
2472 * Returns 0 on success
2473 *
2474 */
goya_hw_init(struct hl_device * hdev)2475 static int goya_hw_init(struct hl_device *hdev)
2476 {
2477 struct asic_fixed_properties *prop = &hdev->asic_prop;
2478 int rc;
2479
2480 dev_info(hdev->dev, "Starting initialization of H/W\n");
2481
2482 /* Perform read from the device to make sure device is up */
2483 RREG32(mmPCIE_DBI_DEVICE_ID_VENDOR_ID_REG);
2484
2485 /*
2486 * Let's mark in the H/W that we have reached this point. We check
2487 * this value in the reset_before_init function to understand whether
2488 * we need to reset the chip before doing H/W init. This register is
2489 * cleared by the H/W upon H/W reset
2490 */
2491 WREG32(mmHW_STATE, HL_DEVICE_HW_STATE_DIRTY);
2492
2493 rc = goya_init_cpu(hdev, GOYA_CPU_TIMEOUT_USEC);
2494 if (rc) {
2495 dev_err(hdev->dev, "failed to initialize CPU\n");
2496 return rc;
2497 }
2498
2499 goya_tpc_mbist_workaround(hdev);
2500
2501 goya_init_golden_registers(hdev);
2502
2503 /*
2504 * After CPU initialization is finished, change DDR bar mapping inside
2505 * iATU to point to the start address of the MMU page tables
2506 */
2507 if (goya_set_ddr_bar_base(hdev, DRAM_PHYS_BASE +
2508 (MMU_PAGE_TABLES_ADDR &
2509 ~(prop->dram_pci_bar_size - 0x1ull))) == U64_MAX) {
2510 dev_err(hdev->dev,
2511 "failed to map DDR bar to MMU page tables\n");
2512 return -EIO;
2513 }
2514
2515 rc = goya_mmu_init(hdev);
2516 if (rc)
2517 return rc;
2518
2519 goya_init_security(hdev);
2520
2521 goya_init_dma_qmans(hdev);
2522
2523 goya_init_mme_qmans(hdev);
2524
2525 goya_init_tpc_qmans(hdev);
2526
2527 goya_enable_timestamp(hdev);
2528
2529 /* MSI-X must be enabled before CPU queues are initialized */
2530 rc = goya_enable_msix(hdev);
2531 if (rc)
2532 goto disable_queues;
2533
2534 /* Perform read from the device to flush all MSI-X configuration */
2535 RREG32(mmPCIE_DBI_DEVICE_ID_VENDOR_ID_REG);
2536
2537 return 0;
2538
2539 disable_queues:
2540 goya_disable_internal_queues(hdev);
2541 goya_disable_external_queues(hdev);
2542
2543 return rc;
2544 }
2545
2546 /*
2547 * goya_hw_fini - Goya hardware tear-down code
2548 *
2549 * @hdev: pointer to hl_device structure
2550 * @hard_reset: should we do hard reset to all engines or just reset the
2551 * compute/dma engines
2552 */
goya_hw_fini(struct hl_device * hdev,bool hard_reset)2553 static void goya_hw_fini(struct hl_device *hdev, bool hard_reset)
2554 {
2555 struct goya_device *goya = hdev->asic_specific;
2556 u32 reset_timeout_ms, status;
2557
2558 if (hdev->pldm)
2559 reset_timeout_ms = GOYA_PLDM_RESET_TIMEOUT_MSEC;
2560 else
2561 reset_timeout_ms = GOYA_RESET_TIMEOUT_MSEC;
2562
2563 if (hard_reset) {
2564 goya_set_ddr_bar_base(hdev, DRAM_PHYS_BASE);
2565 goya_disable_clk_rlx(hdev);
2566 goya_set_pll_refclk(hdev);
2567
2568 WREG32(mmPSOC_GLOBAL_CONF_SW_ALL_RST_CFG, RESET_ALL);
2569 dev_info(hdev->dev,
2570 "Issued HARD reset command, going to wait %dms\n",
2571 reset_timeout_ms);
2572 } else {
2573 WREG32(mmPSOC_GLOBAL_CONF_SW_ALL_RST_CFG, DMA_MME_TPC_RESET);
2574 dev_info(hdev->dev,
2575 "Issued SOFT reset command, going to wait %dms\n",
2576 reset_timeout_ms);
2577 }
2578
2579 /*
2580 * After hard reset, we can't poll the BTM_FSM register because the PSOC
2581 * itself is in reset. In either reset we need to wait until the reset
2582 * is deasserted
2583 */
2584 msleep(reset_timeout_ms);
2585
2586 status = RREG32(mmPSOC_GLOBAL_CONF_BTM_FSM);
2587 if (status & PSOC_GLOBAL_CONF_BTM_FSM_STATE_MASK)
2588 dev_err(hdev->dev,
2589 "Timeout while waiting for device to reset 0x%x\n",
2590 status);
2591
2592 if (!hard_reset) {
2593 goya->hw_cap_initialized &= ~(HW_CAP_DMA | HW_CAP_MME |
2594 HW_CAP_GOLDEN | HW_CAP_TPC);
2595 WREG32(mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR,
2596 GOYA_ASYNC_EVENT_ID_SOFT_RESET);
2597 return;
2598 }
2599
2600 /* Chicken bit to re-initiate boot sequencer flow */
2601 WREG32(mmPSOC_GLOBAL_CONF_BOOT_SEQ_RE_START,
2602 1 << PSOC_GLOBAL_CONF_BOOT_SEQ_RE_START_IND_SHIFT);
2603 /* Move boot manager FSM to pre boot sequencer init state */
2604 WREG32(mmPSOC_GLOBAL_CONF_SW_BTM_FSM,
2605 0xA << PSOC_GLOBAL_CONF_SW_BTM_FSM_CTRL_SHIFT);
2606
2607 goya->hw_cap_initialized &= ~(HW_CAP_CPU | HW_CAP_CPU_Q |
2608 HW_CAP_DDR_0 | HW_CAP_DDR_1 |
2609 HW_CAP_DMA | HW_CAP_MME |
2610 HW_CAP_MMU | HW_CAP_TPC_MBIST |
2611 HW_CAP_GOLDEN | HW_CAP_TPC);
2612 memset(goya->events_stat, 0, sizeof(goya->events_stat));
2613
2614 if (!hdev->pldm) {
2615 int rc;
2616 /* In case we are running inside VM and the VM is
2617 * shutting down, we need to make sure CPU boot-loader
2618 * is running before we can continue the VM shutdown.
2619 * That is because the VM will send an FLR signal that
2620 * we must answer
2621 */
2622 dev_info(hdev->dev,
2623 "Going to wait up to %ds for CPU boot loader\n",
2624 GOYA_CPU_TIMEOUT_USEC / 1000 / 1000);
2625
2626 rc = hl_poll_timeout(
2627 hdev,
2628 mmPSOC_GLOBAL_CONF_WARM_REBOOT,
2629 status,
2630 (status == CPU_BOOT_STATUS_DRAM_RDY),
2631 10000,
2632 GOYA_CPU_TIMEOUT_USEC);
2633 if (rc)
2634 dev_err(hdev->dev,
2635 "failed to wait for CPU boot loader\n");
2636 }
2637 }
2638
goya_suspend(struct hl_device * hdev)2639 int goya_suspend(struct hl_device *hdev)
2640 {
2641 int rc;
2642
2643 rc = hl_fw_send_pci_access_msg(hdev, ARMCP_PACKET_DISABLE_PCI_ACCESS);
2644 if (rc)
2645 dev_err(hdev->dev, "Failed to disable PCI access from CPU\n");
2646
2647 return rc;
2648 }
2649
goya_resume(struct hl_device * hdev)2650 int goya_resume(struct hl_device *hdev)
2651 {
2652 return goya_init_iatu(hdev);
2653 }
2654
goya_cb_mmap(struct hl_device * hdev,struct vm_area_struct * vma,u64 kaddress,phys_addr_t paddress,u32 size)2655 static int goya_cb_mmap(struct hl_device *hdev, struct vm_area_struct *vma,
2656 u64 kaddress, phys_addr_t paddress, u32 size)
2657 {
2658 int rc;
2659
2660 vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP |
2661 VM_DONTCOPY | VM_NORESERVE;
2662
2663 rc = remap_pfn_range(vma, vma->vm_start, paddress >> PAGE_SHIFT,
2664 size, vma->vm_page_prot);
2665 if (rc)
2666 dev_err(hdev->dev, "remap_pfn_range error %d", rc);
2667
2668 return rc;
2669 }
2670
goya_ring_doorbell(struct hl_device * hdev,u32 hw_queue_id,u32 pi)2671 void goya_ring_doorbell(struct hl_device *hdev, u32 hw_queue_id, u32 pi)
2672 {
2673 u32 db_reg_offset, db_value;
2674
2675 switch (hw_queue_id) {
2676 case GOYA_QUEUE_ID_DMA_0:
2677 db_reg_offset = mmDMA_QM_0_PQ_PI;
2678 break;
2679
2680 case GOYA_QUEUE_ID_DMA_1:
2681 db_reg_offset = mmDMA_QM_1_PQ_PI;
2682 break;
2683
2684 case GOYA_QUEUE_ID_DMA_2:
2685 db_reg_offset = mmDMA_QM_2_PQ_PI;
2686 break;
2687
2688 case GOYA_QUEUE_ID_DMA_3:
2689 db_reg_offset = mmDMA_QM_3_PQ_PI;
2690 break;
2691
2692 case GOYA_QUEUE_ID_DMA_4:
2693 db_reg_offset = mmDMA_QM_4_PQ_PI;
2694 break;
2695
2696 case GOYA_QUEUE_ID_CPU_PQ:
2697 db_reg_offset = mmCPU_IF_PF_PQ_PI;
2698 break;
2699
2700 case GOYA_QUEUE_ID_MME:
2701 db_reg_offset = mmMME_QM_PQ_PI;
2702 break;
2703
2704 case GOYA_QUEUE_ID_TPC0:
2705 db_reg_offset = mmTPC0_QM_PQ_PI;
2706 break;
2707
2708 case GOYA_QUEUE_ID_TPC1:
2709 db_reg_offset = mmTPC1_QM_PQ_PI;
2710 break;
2711
2712 case GOYA_QUEUE_ID_TPC2:
2713 db_reg_offset = mmTPC2_QM_PQ_PI;
2714 break;
2715
2716 case GOYA_QUEUE_ID_TPC3:
2717 db_reg_offset = mmTPC3_QM_PQ_PI;
2718 break;
2719
2720 case GOYA_QUEUE_ID_TPC4:
2721 db_reg_offset = mmTPC4_QM_PQ_PI;
2722 break;
2723
2724 case GOYA_QUEUE_ID_TPC5:
2725 db_reg_offset = mmTPC5_QM_PQ_PI;
2726 break;
2727
2728 case GOYA_QUEUE_ID_TPC6:
2729 db_reg_offset = mmTPC6_QM_PQ_PI;
2730 break;
2731
2732 case GOYA_QUEUE_ID_TPC7:
2733 db_reg_offset = mmTPC7_QM_PQ_PI;
2734 break;
2735
2736 default:
2737 /* Should never get here */
2738 dev_err(hdev->dev, "H/W queue %d is invalid. Can't set pi\n",
2739 hw_queue_id);
2740 return;
2741 }
2742
2743 db_value = pi;
2744
2745 /* ring the doorbell */
2746 WREG32(db_reg_offset, db_value);
2747
2748 if (hw_queue_id == GOYA_QUEUE_ID_CPU_PQ)
2749 WREG32(mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR,
2750 GOYA_ASYNC_EVENT_ID_PI_UPDATE);
2751 }
2752
goya_pqe_write(struct hl_device * hdev,__le64 * pqe,struct hl_bd * bd)2753 void goya_pqe_write(struct hl_device *hdev, __le64 *pqe, struct hl_bd *bd)
2754 {
2755 /* The QMANs are on the SRAM so need to copy to IO space */
2756 memcpy_toio((void __iomem *) pqe, bd, sizeof(struct hl_bd));
2757 }
2758
goya_dma_alloc_coherent(struct hl_device * hdev,size_t size,dma_addr_t * dma_handle,gfp_t flags)2759 static void *goya_dma_alloc_coherent(struct hl_device *hdev, size_t size,
2760 dma_addr_t *dma_handle, gfp_t flags)
2761 {
2762 void *kernel_addr = dma_alloc_coherent(&hdev->pdev->dev, size,
2763 dma_handle, flags);
2764
2765 /* Shift to the device's base physical address of host memory */
2766 if (kernel_addr)
2767 *dma_handle += HOST_PHYS_BASE;
2768
2769 return kernel_addr;
2770 }
2771
goya_dma_free_coherent(struct hl_device * hdev,size_t size,void * cpu_addr,dma_addr_t dma_handle)2772 static void goya_dma_free_coherent(struct hl_device *hdev, size_t size,
2773 void *cpu_addr, dma_addr_t dma_handle)
2774 {
2775 /* Cancel the device's base physical address of host memory */
2776 dma_addr_t fixed_dma_handle = dma_handle - HOST_PHYS_BASE;
2777
2778 dma_free_coherent(&hdev->pdev->dev, size, cpu_addr, fixed_dma_handle);
2779 }
2780
goya_get_int_queue_base(struct hl_device * hdev,u32 queue_id,dma_addr_t * dma_handle,u16 * queue_len)2781 void *goya_get_int_queue_base(struct hl_device *hdev, u32 queue_id,
2782 dma_addr_t *dma_handle, u16 *queue_len)
2783 {
2784 void *base;
2785 u32 offset;
2786
2787 *dma_handle = hdev->asic_prop.sram_base_address;
2788
2789 base = (void *) hdev->pcie_bar[SRAM_CFG_BAR_ID];
2790
2791 switch (queue_id) {
2792 case GOYA_QUEUE_ID_MME:
2793 offset = MME_QMAN_BASE_OFFSET;
2794 *queue_len = MME_QMAN_LENGTH;
2795 break;
2796 case GOYA_QUEUE_ID_TPC0:
2797 offset = TPC0_QMAN_BASE_OFFSET;
2798 *queue_len = TPC_QMAN_LENGTH;
2799 break;
2800 case GOYA_QUEUE_ID_TPC1:
2801 offset = TPC1_QMAN_BASE_OFFSET;
2802 *queue_len = TPC_QMAN_LENGTH;
2803 break;
2804 case GOYA_QUEUE_ID_TPC2:
2805 offset = TPC2_QMAN_BASE_OFFSET;
2806 *queue_len = TPC_QMAN_LENGTH;
2807 break;
2808 case GOYA_QUEUE_ID_TPC3:
2809 offset = TPC3_QMAN_BASE_OFFSET;
2810 *queue_len = TPC_QMAN_LENGTH;
2811 break;
2812 case GOYA_QUEUE_ID_TPC4:
2813 offset = TPC4_QMAN_BASE_OFFSET;
2814 *queue_len = TPC_QMAN_LENGTH;
2815 break;
2816 case GOYA_QUEUE_ID_TPC5:
2817 offset = TPC5_QMAN_BASE_OFFSET;
2818 *queue_len = TPC_QMAN_LENGTH;
2819 break;
2820 case GOYA_QUEUE_ID_TPC6:
2821 offset = TPC6_QMAN_BASE_OFFSET;
2822 *queue_len = TPC_QMAN_LENGTH;
2823 break;
2824 case GOYA_QUEUE_ID_TPC7:
2825 offset = TPC7_QMAN_BASE_OFFSET;
2826 *queue_len = TPC_QMAN_LENGTH;
2827 break;
2828 default:
2829 dev_err(hdev->dev, "Got invalid queue id %d\n", queue_id);
2830 return NULL;
2831 }
2832
2833 base += offset;
2834 *dma_handle += offset;
2835
2836 return base;
2837 }
2838
goya_send_job_on_qman0(struct hl_device * hdev,struct hl_cs_job * job)2839 static int goya_send_job_on_qman0(struct hl_device *hdev, struct hl_cs_job *job)
2840 {
2841 struct packet_msg_prot *fence_pkt;
2842 u32 *fence_ptr;
2843 dma_addr_t fence_dma_addr;
2844 struct hl_cb *cb;
2845 u32 tmp, timeout;
2846 int rc;
2847
2848 if (hdev->pldm)
2849 timeout = GOYA_PLDM_QMAN0_TIMEOUT_USEC;
2850 else
2851 timeout = HL_DEVICE_TIMEOUT_USEC;
2852
2853 if (!hdev->asic_funcs->is_device_idle(hdev, NULL, NULL)) {
2854 dev_err_ratelimited(hdev->dev,
2855 "Can't send driver job on QMAN0 because the device is not idle\n");
2856 return -EBUSY;
2857 }
2858
2859 fence_ptr = hdev->asic_funcs->asic_dma_pool_zalloc(hdev, 4, GFP_KERNEL,
2860 &fence_dma_addr);
2861 if (!fence_ptr) {
2862 dev_err(hdev->dev,
2863 "Failed to allocate fence memory for QMAN0\n");
2864 return -ENOMEM;
2865 }
2866
2867 goya_qman0_set_security(hdev, true);
2868
2869 cb = job->patched_cb;
2870
2871 fence_pkt = (struct packet_msg_prot *) (uintptr_t) (cb->kernel_address +
2872 job->job_cb_size - sizeof(struct packet_msg_prot));
2873
2874 tmp = (PACKET_MSG_PROT << GOYA_PKT_CTL_OPCODE_SHIFT) |
2875 (1 << GOYA_PKT_CTL_EB_SHIFT) |
2876 (1 << GOYA_PKT_CTL_MB_SHIFT);
2877 fence_pkt->ctl = cpu_to_le32(tmp);
2878 fence_pkt->value = cpu_to_le32(GOYA_QMAN0_FENCE_VAL);
2879 fence_pkt->addr = cpu_to_le64(fence_dma_addr);
2880
2881 rc = hl_hw_queue_send_cb_no_cmpl(hdev, GOYA_QUEUE_ID_DMA_0,
2882 job->job_cb_size, cb->bus_address);
2883 if (rc) {
2884 dev_err(hdev->dev, "Failed to send CB on QMAN0, %d\n", rc);
2885 goto free_fence_ptr;
2886 }
2887
2888 rc = hl_poll_timeout_memory(hdev, fence_ptr, tmp,
2889 (tmp == GOYA_QMAN0_FENCE_VAL), 1000,
2890 timeout, true);
2891
2892 hl_hw_queue_inc_ci_kernel(hdev, GOYA_QUEUE_ID_DMA_0);
2893
2894 if (rc == -ETIMEDOUT) {
2895 dev_err(hdev->dev, "QMAN0 Job timeout (0x%x)\n", tmp);
2896 goto free_fence_ptr;
2897 }
2898
2899 free_fence_ptr:
2900 hdev->asic_funcs->asic_dma_pool_free(hdev, (void *) fence_ptr,
2901 fence_dma_addr);
2902
2903 goya_qman0_set_security(hdev, false);
2904
2905 return rc;
2906 }
2907
goya_send_cpu_message(struct hl_device * hdev,u32 * msg,u16 len,u32 timeout,long * result)2908 int goya_send_cpu_message(struct hl_device *hdev, u32 *msg, u16 len,
2909 u32 timeout, long *result)
2910 {
2911 struct goya_device *goya = hdev->asic_specific;
2912
2913 if (!(goya->hw_cap_initialized & HW_CAP_CPU_Q)) {
2914 if (result)
2915 *result = 0;
2916 return 0;
2917 }
2918
2919 return hl_fw_send_cpu_message(hdev, GOYA_QUEUE_ID_CPU_PQ, msg, len,
2920 timeout, result);
2921 }
2922
goya_test_queue(struct hl_device * hdev,u32 hw_queue_id)2923 int goya_test_queue(struct hl_device *hdev, u32 hw_queue_id)
2924 {
2925 struct packet_msg_prot *fence_pkt;
2926 dma_addr_t pkt_dma_addr;
2927 u32 fence_val, tmp;
2928 dma_addr_t fence_dma_addr;
2929 u32 *fence_ptr;
2930 int rc;
2931
2932 fence_val = GOYA_QMAN0_FENCE_VAL;
2933
2934 fence_ptr = hdev->asic_funcs->asic_dma_pool_zalloc(hdev, 4, GFP_KERNEL,
2935 &fence_dma_addr);
2936 if (!fence_ptr) {
2937 dev_err(hdev->dev,
2938 "Failed to allocate memory for queue testing\n");
2939 return -ENOMEM;
2940 }
2941
2942 *fence_ptr = 0;
2943
2944 fence_pkt = hdev->asic_funcs->asic_dma_pool_zalloc(hdev,
2945 sizeof(struct packet_msg_prot),
2946 GFP_KERNEL, &pkt_dma_addr);
2947 if (!fence_pkt) {
2948 dev_err(hdev->dev,
2949 "Failed to allocate packet for queue testing\n");
2950 rc = -ENOMEM;
2951 goto free_fence_ptr;
2952 }
2953
2954 tmp = (PACKET_MSG_PROT << GOYA_PKT_CTL_OPCODE_SHIFT) |
2955 (1 << GOYA_PKT_CTL_EB_SHIFT) |
2956 (1 << GOYA_PKT_CTL_MB_SHIFT);
2957 fence_pkt->ctl = cpu_to_le32(tmp);
2958 fence_pkt->value = cpu_to_le32(fence_val);
2959 fence_pkt->addr = cpu_to_le64(fence_dma_addr);
2960
2961 rc = hl_hw_queue_send_cb_no_cmpl(hdev, hw_queue_id,
2962 sizeof(struct packet_msg_prot),
2963 pkt_dma_addr);
2964 if (rc) {
2965 dev_err(hdev->dev,
2966 "Failed to send fence packet\n");
2967 goto free_pkt;
2968 }
2969
2970 rc = hl_poll_timeout_memory(hdev, fence_ptr, tmp, (tmp == fence_val),
2971 1000, GOYA_TEST_QUEUE_WAIT_USEC, true);
2972
2973 hl_hw_queue_inc_ci_kernel(hdev, hw_queue_id);
2974
2975 if (rc == -ETIMEDOUT) {
2976 dev_err(hdev->dev,
2977 "H/W queue %d test failed (scratch(0x%08llX) == 0x%08X)\n",
2978 hw_queue_id, (unsigned long long) fence_dma_addr, tmp);
2979 rc = -EIO;
2980 } else {
2981 dev_info(hdev->dev, "queue test on H/W queue %d succeeded\n",
2982 hw_queue_id);
2983 }
2984
2985 free_pkt:
2986 hdev->asic_funcs->asic_dma_pool_free(hdev, (void *) fence_pkt,
2987 pkt_dma_addr);
2988 free_fence_ptr:
2989 hdev->asic_funcs->asic_dma_pool_free(hdev, (void *) fence_ptr,
2990 fence_dma_addr);
2991 return rc;
2992 }
2993
goya_test_cpu_queue(struct hl_device * hdev)2994 int goya_test_cpu_queue(struct hl_device *hdev)
2995 {
2996 struct goya_device *goya = hdev->asic_specific;
2997
2998 /*
2999 * check capability here as send_cpu_message() won't update the result
3000 * value if no capability
3001 */
3002 if (!(goya->hw_cap_initialized & HW_CAP_CPU_Q))
3003 return 0;
3004
3005 return hl_fw_test_cpu_queue(hdev);
3006 }
3007
goya_test_queues(struct hl_device * hdev)3008 int goya_test_queues(struct hl_device *hdev)
3009 {
3010 int i, rc, ret_val = 0;
3011
3012 for (i = 0 ; i < NUMBER_OF_EXT_HW_QUEUES ; i++) {
3013 rc = goya_test_queue(hdev, i);
3014 if (rc)
3015 ret_val = -EINVAL;
3016 }
3017
3018 return ret_val;
3019 }
3020
goya_dma_pool_zalloc(struct hl_device * hdev,size_t size,gfp_t mem_flags,dma_addr_t * dma_handle)3021 static void *goya_dma_pool_zalloc(struct hl_device *hdev, size_t size,
3022 gfp_t mem_flags, dma_addr_t *dma_handle)
3023 {
3024 void *kernel_addr;
3025
3026 if (size > GOYA_DMA_POOL_BLK_SIZE)
3027 return NULL;
3028
3029 kernel_addr = dma_pool_zalloc(hdev->dma_pool, mem_flags, dma_handle);
3030
3031 /* Shift to the device's base physical address of host memory */
3032 if (kernel_addr)
3033 *dma_handle += HOST_PHYS_BASE;
3034
3035 return kernel_addr;
3036 }
3037
goya_dma_pool_free(struct hl_device * hdev,void * vaddr,dma_addr_t dma_addr)3038 static void goya_dma_pool_free(struct hl_device *hdev, void *vaddr,
3039 dma_addr_t dma_addr)
3040 {
3041 /* Cancel the device's base physical address of host memory */
3042 dma_addr_t fixed_dma_addr = dma_addr - HOST_PHYS_BASE;
3043
3044 dma_pool_free(hdev->dma_pool, vaddr, fixed_dma_addr);
3045 }
3046
goya_cpu_accessible_dma_pool_alloc(struct hl_device * hdev,size_t size,dma_addr_t * dma_handle)3047 void *goya_cpu_accessible_dma_pool_alloc(struct hl_device *hdev, size_t size,
3048 dma_addr_t *dma_handle)
3049 {
3050 void *vaddr;
3051
3052 vaddr = hl_fw_cpu_accessible_dma_pool_alloc(hdev, size, dma_handle);
3053 *dma_handle = (*dma_handle) - hdev->cpu_accessible_dma_address +
3054 VA_CPU_ACCESSIBLE_MEM_ADDR;
3055
3056 return vaddr;
3057 }
3058
goya_cpu_accessible_dma_pool_free(struct hl_device * hdev,size_t size,void * vaddr)3059 void goya_cpu_accessible_dma_pool_free(struct hl_device *hdev, size_t size,
3060 void *vaddr)
3061 {
3062 hl_fw_cpu_accessible_dma_pool_free(hdev, size, vaddr);
3063 }
3064
goya_dma_map_sg(struct hl_device * hdev,struct scatterlist * sgl,int nents,enum dma_data_direction dir)3065 static int goya_dma_map_sg(struct hl_device *hdev, struct scatterlist *sgl,
3066 int nents, enum dma_data_direction dir)
3067 {
3068 struct scatterlist *sg;
3069 int i;
3070
3071 if (!dma_map_sg(&hdev->pdev->dev, sgl, nents, dir))
3072 return -ENOMEM;
3073
3074 /* Shift to the device's base physical address of host memory */
3075 for_each_sg(sgl, sg, nents, i)
3076 sg->dma_address += HOST_PHYS_BASE;
3077
3078 return 0;
3079 }
3080
goya_dma_unmap_sg(struct hl_device * hdev,struct scatterlist * sgl,int nents,enum dma_data_direction dir)3081 static void goya_dma_unmap_sg(struct hl_device *hdev, struct scatterlist *sgl,
3082 int nents, enum dma_data_direction dir)
3083 {
3084 struct scatterlist *sg;
3085 int i;
3086
3087 /* Cancel the device's base physical address of host memory */
3088 for_each_sg(sgl, sg, nents, i)
3089 sg->dma_address -= HOST_PHYS_BASE;
3090
3091 dma_unmap_sg(&hdev->pdev->dev, sgl, nents, dir);
3092 }
3093
goya_get_dma_desc_list_size(struct hl_device * hdev,struct sg_table * sgt)3094 u32 goya_get_dma_desc_list_size(struct hl_device *hdev, struct sg_table *sgt)
3095 {
3096 struct scatterlist *sg, *sg_next_iter;
3097 u32 count, dma_desc_cnt;
3098 u64 len, len_next;
3099 dma_addr_t addr, addr_next;
3100
3101 dma_desc_cnt = 0;
3102
3103 for_each_sg(sgt->sgl, sg, sgt->nents, count) {
3104
3105 len = sg_dma_len(sg);
3106 addr = sg_dma_address(sg);
3107
3108 if (len == 0)
3109 break;
3110
3111 while ((count + 1) < sgt->nents) {
3112 sg_next_iter = sg_next(sg);
3113 len_next = sg_dma_len(sg_next_iter);
3114 addr_next = sg_dma_address(sg_next_iter);
3115
3116 if (len_next == 0)
3117 break;
3118
3119 if ((addr + len == addr_next) &&
3120 (len + len_next <= DMA_MAX_TRANSFER_SIZE)) {
3121 len += len_next;
3122 count++;
3123 sg = sg_next_iter;
3124 } else {
3125 break;
3126 }
3127 }
3128
3129 dma_desc_cnt++;
3130 }
3131
3132 return dma_desc_cnt * sizeof(struct packet_lin_dma);
3133 }
3134
goya_pin_memory_before_cs(struct hl_device * hdev,struct hl_cs_parser * parser,struct packet_lin_dma * user_dma_pkt,u64 addr,enum dma_data_direction dir)3135 static int goya_pin_memory_before_cs(struct hl_device *hdev,
3136 struct hl_cs_parser *parser,
3137 struct packet_lin_dma *user_dma_pkt,
3138 u64 addr, enum dma_data_direction dir)
3139 {
3140 struct hl_userptr *userptr;
3141 int rc;
3142
3143 if (hl_userptr_is_pinned(hdev, addr, le32_to_cpu(user_dma_pkt->tsize),
3144 parser->job_userptr_list, &userptr))
3145 goto already_pinned;
3146
3147 userptr = kzalloc(sizeof(*userptr), GFP_ATOMIC);
3148 if (!userptr)
3149 return -ENOMEM;
3150
3151 rc = hl_pin_host_memory(hdev, addr, le32_to_cpu(user_dma_pkt->tsize),
3152 userptr);
3153 if (rc)
3154 goto free_userptr;
3155
3156 list_add_tail(&userptr->job_node, parser->job_userptr_list);
3157
3158 rc = hdev->asic_funcs->asic_dma_map_sg(hdev, userptr->sgt->sgl,
3159 userptr->sgt->nents, dir);
3160 if (rc) {
3161 dev_err(hdev->dev, "failed to map sgt with DMA region\n");
3162 goto unpin_memory;
3163 }
3164
3165 userptr->dma_mapped = true;
3166 userptr->dir = dir;
3167
3168 already_pinned:
3169 parser->patched_cb_size +=
3170 goya_get_dma_desc_list_size(hdev, userptr->sgt);
3171
3172 return 0;
3173
3174 unpin_memory:
3175 hl_unpin_host_memory(hdev, userptr);
3176 free_userptr:
3177 kfree(userptr);
3178 return rc;
3179 }
3180
goya_validate_dma_pkt_host(struct hl_device * hdev,struct hl_cs_parser * parser,struct packet_lin_dma * user_dma_pkt)3181 static int goya_validate_dma_pkt_host(struct hl_device *hdev,
3182 struct hl_cs_parser *parser,
3183 struct packet_lin_dma *user_dma_pkt)
3184 {
3185 u64 device_memory_addr, addr;
3186 enum dma_data_direction dir;
3187 enum goya_dma_direction user_dir;
3188 bool sram_addr = true;
3189 bool skip_host_mem_pin = false;
3190 bool user_memset;
3191 u32 ctl;
3192 int rc = 0;
3193
3194 ctl = le32_to_cpu(user_dma_pkt->ctl);
3195
3196 user_dir = (ctl & GOYA_PKT_LIN_DMA_CTL_DMA_DIR_MASK) >>
3197 GOYA_PKT_LIN_DMA_CTL_DMA_DIR_SHIFT;
3198
3199 user_memset = (ctl & GOYA_PKT_LIN_DMA_CTL_MEMSET_MASK) >>
3200 GOYA_PKT_LIN_DMA_CTL_MEMSET_SHIFT;
3201
3202 switch (user_dir) {
3203 case DMA_HOST_TO_DRAM:
3204 dev_dbg(hdev->dev, "DMA direction is HOST --> DRAM\n");
3205 dir = DMA_TO_DEVICE;
3206 sram_addr = false;
3207 addr = le64_to_cpu(user_dma_pkt->src_addr);
3208 device_memory_addr = le64_to_cpu(user_dma_pkt->dst_addr);
3209 if (user_memset)
3210 skip_host_mem_pin = true;
3211 break;
3212
3213 case DMA_DRAM_TO_HOST:
3214 dev_dbg(hdev->dev, "DMA direction is DRAM --> HOST\n");
3215 dir = DMA_FROM_DEVICE;
3216 sram_addr = false;
3217 addr = le64_to_cpu(user_dma_pkt->dst_addr);
3218 device_memory_addr = le64_to_cpu(user_dma_pkt->src_addr);
3219 break;
3220
3221 case DMA_HOST_TO_SRAM:
3222 dev_dbg(hdev->dev, "DMA direction is HOST --> SRAM\n");
3223 dir = DMA_TO_DEVICE;
3224 addr = le64_to_cpu(user_dma_pkt->src_addr);
3225 device_memory_addr = le64_to_cpu(user_dma_pkt->dst_addr);
3226 if (user_memset)
3227 skip_host_mem_pin = true;
3228 break;
3229
3230 case DMA_SRAM_TO_HOST:
3231 dev_dbg(hdev->dev, "DMA direction is SRAM --> HOST\n");
3232 dir = DMA_FROM_DEVICE;
3233 addr = le64_to_cpu(user_dma_pkt->dst_addr);
3234 device_memory_addr = le64_to_cpu(user_dma_pkt->src_addr);
3235 break;
3236 default:
3237 dev_err(hdev->dev, "DMA direction is undefined\n");
3238 return -EFAULT;
3239 }
3240
3241 if (sram_addr) {
3242 if (!hl_mem_area_inside_range(device_memory_addr,
3243 le32_to_cpu(user_dma_pkt->tsize),
3244 hdev->asic_prop.sram_user_base_address,
3245 hdev->asic_prop.sram_end_address)) {
3246
3247 dev_err(hdev->dev,
3248 "SRAM address 0x%llx + 0x%x is invalid\n",
3249 device_memory_addr,
3250 user_dma_pkt->tsize);
3251 return -EFAULT;
3252 }
3253 } else {
3254 if (!hl_mem_area_inside_range(device_memory_addr,
3255 le32_to_cpu(user_dma_pkt->tsize),
3256 hdev->asic_prop.dram_user_base_address,
3257 hdev->asic_prop.dram_end_address)) {
3258
3259 dev_err(hdev->dev,
3260 "DRAM address 0x%llx + 0x%x is invalid\n",
3261 device_memory_addr,
3262 user_dma_pkt->tsize);
3263 return -EFAULT;
3264 }
3265 }
3266
3267 if (skip_host_mem_pin)
3268 parser->patched_cb_size += sizeof(*user_dma_pkt);
3269 else {
3270 if ((dir == DMA_TO_DEVICE) &&
3271 (parser->hw_queue_id > GOYA_QUEUE_ID_DMA_1)) {
3272 dev_err(hdev->dev,
3273 "Can't DMA from host on queue other then 1\n");
3274 return -EFAULT;
3275 }
3276
3277 rc = goya_pin_memory_before_cs(hdev, parser, user_dma_pkt,
3278 addr, dir);
3279 }
3280
3281 return rc;
3282 }
3283
goya_validate_dma_pkt_no_host(struct hl_device * hdev,struct hl_cs_parser * parser,struct packet_lin_dma * user_dma_pkt)3284 static int goya_validate_dma_pkt_no_host(struct hl_device *hdev,
3285 struct hl_cs_parser *parser,
3286 struct packet_lin_dma *user_dma_pkt)
3287 {
3288 u64 sram_memory_addr, dram_memory_addr;
3289 enum goya_dma_direction user_dir;
3290 u32 ctl;
3291
3292 ctl = le32_to_cpu(user_dma_pkt->ctl);
3293 user_dir = (ctl & GOYA_PKT_LIN_DMA_CTL_DMA_DIR_MASK) >>
3294 GOYA_PKT_LIN_DMA_CTL_DMA_DIR_SHIFT;
3295
3296 if (user_dir == DMA_DRAM_TO_SRAM) {
3297 dev_dbg(hdev->dev, "DMA direction is DRAM --> SRAM\n");
3298 dram_memory_addr = le64_to_cpu(user_dma_pkt->src_addr);
3299 sram_memory_addr = le64_to_cpu(user_dma_pkt->dst_addr);
3300 } else {
3301 dev_dbg(hdev->dev, "DMA direction is SRAM --> DRAM\n");
3302 sram_memory_addr = le64_to_cpu(user_dma_pkt->src_addr);
3303 dram_memory_addr = le64_to_cpu(user_dma_pkt->dst_addr);
3304 }
3305
3306 if (!hl_mem_area_inside_range(sram_memory_addr,
3307 le32_to_cpu(user_dma_pkt->tsize),
3308 hdev->asic_prop.sram_user_base_address,
3309 hdev->asic_prop.sram_end_address)) {
3310 dev_err(hdev->dev, "SRAM address 0x%llx + 0x%x is invalid\n",
3311 sram_memory_addr, user_dma_pkt->tsize);
3312 return -EFAULT;
3313 }
3314
3315 if (!hl_mem_area_inside_range(dram_memory_addr,
3316 le32_to_cpu(user_dma_pkt->tsize),
3317 hdev->asic_prop.dram_user_base_address,
3318 hdev->asic_prop.dram_end_address)) {
3319 dev_err(hdev->dev, "DRAM address 0x%llx + 0x%x is invalid\n",
3320 dram_memory_addr, user_dma_pkt->tsize);
3321 return -EFAULT;
3322 }
3323
3324 parser->patched_cb_size += sizeof(*user_dma_pkt);
3325
3326 return 0;
3327 }
3328
goya_validate_dma_pkt_no_mmu(struct hl_device * hdev,struct hl_cs_parser * parser,struct packet_lin_dma * user_dma_pkt)3329 static int goya_validate_dma_pkt_no_mmu(struct hl_device *hdev,
3330 struct hl_cs_parser *parser,
3331 struct packet_lin_dma *user_dma_pkt)
3332 {
3333 enum goya_dma_direction user_dir;
3334 u32 ctl;
3335 int rc;
3336
3337 dev_dbg(hdev->dev, "DMA packet details:\n");
3338 dev_dbg(hdev->dev, "source == 0x%llx\n",
3339 le64_to_cpu(user_dma_pkt->src_addr));
3340 dev_dbg(hdev->dev, "destination == 0x%llx\n",
3341 le64_to_cpu(user_dma_pkt->dst_addr));
3342 dev_dbg(hdev->dev, "size == %u\n", le32_to_cpu(user_dma_pkt->tsize));
3343
3344 ctl = le32_to_cpu(user_dma_pkt->ctl);
3345 user_dir = (ctl & GOYA_PKT_LIN_DMA_CTL_DMA_DIR_MASK) >>
3346 GOYA_PKT_LIN_DMA_CTL_DMA_DIR_SHIFT;
3347
3348 /*
3349 * Special handling for DMA with size 0. The H/W has a bug where
3350 * this can cause the QMAN DMA to get stuck, so block it here.
3351 */
3352 if (user_dma_pkt->tsize == 0) {
3353 dev_err(hdev->dev,
3354 "Got DMA with size 0, might reset the device\n");
3355 return -EINVAL;
3356 }
3357
3358 if ((user_dir == DMA_DRAM_TO_SRAM) || (user_dir == DMA_SRAM_TO_DRAM))
3359 rc = goya_validate_dma_pkt_no_host(hdev, parser, user_dma_pkt);
3360 else
3361 rc = goya_validate_dma_pkt_host(hdev, parser, user_dma_pkt);
3362
3363 return rc;
3364 }
3365
goya_validate_dma_pkt_mmu(struct hl_device * hdev,struct hl_cs_parser * parser,struct packet_lin_dma * user_dma_pkt)3366 static int goya_validate_dma_pkt_mmu(struct hl_device *hdev,
3367 struct hl_cs_parser *parser,
3368 struct packet_lin_dma *user_dma_pkt)
3369 {
3370 dev_dbg(hdev->dev, "DMA packet details:\n");
3371 dev_dbg(hdev->dev, "source == 0x%llx\n",
3372 le64_to_cpu(user_dma_pkt->src_addr));
3373 dev_dbg(hdev->dev, "destination == 0x%llx\n",
3374 le64_to_cpu(user_dma_pkt->dst_addr));
3375 dev_dbg(hdev->dev, "size == %u\n", le32_to_cpu(user_dma_pkt->tsize));
3376
3377 /*
3378 * WA for HW-23.
3379 * We can't allow user to read from Host using QMANs other than 1.
3380 */
3381 if (parser->hw_queue_id != GOYA_QUEUE_ID_DMA_1 &&
3382 hl_mem_area_inside_range(le64_to_cpu(user_dma_pkt->src_addr),
3383 le32_to_cpu(user_dma_pkt->tsize),
3384 hdev->asic_prop.va_space_host_start_address,
3385 hdev->asic_prop.va_space_host_end_address)) {
3386 dev_err(hdev->dev,
3387 "Can't DMA from host on queue other then 1\n");
3388 return -EFAULT;
3389 }
3390
3391 if (user_dma_pkt->tsize == 0) {
3392 dev_err(hdev->dev,
3393 "Got DMA with size 0, might reset the device\n");
3394 return -EINVAL;
3395 }
3396
3397 parser->patched_cb_size += sizeof(*user_dma_pkt);
3398
3399 return 0;
3400 }
3401
goya_validate_wreg32(struct hl_device * hdev,struct hl_cs_parser * parser,struct packet_wreg32 * wreg_pkt)3402 static int goya_validate_wreg32(struct hl_device *hdev,
3403 struct hl_cs_parser *parser,
3404 struct packet_wreg32 *wreg_pkt)
3405 {
3406 struct goya_device *goya = hdev->asic_specific;
3407 u32 sob_start_addr, sob_end_addr;
3408 u16 reg_offset;
3409
3410 reg_offset = le32_to_cpu(wreg_pkt->ctl) &
3411 GOYA_PKT_WREG32_CTL_REG_OFFSET_MASK;
3412
3413 dev_dbg(hdev->dev, "WREG32 packet details:\n");
3414 dev_dbg(hdev->dev, "reg_offset == 0x%x\n", reg_offset);
3415 dev_dbg(hdev->dev, "value == 0x%x\n",
3416 le32_to_cpu(wreg_pkt->value));
3417
3418 if (reg_offset != (mmDMA_CH_0_WR_COMP_ADDR_LO & 0x1FFF)) {
3419 dev_err(hdev->dev, "WREG32 packet with illegal address 0x%x\n",
3420 reg_offset);
3421 return -EPERM;
3422 }
3423
3424 /*
3425 * With MMU, DMA channels are not secured, so it doesn't matter where
3426 * the WR COMP will be written to because it will go out with
3427 * non-secured property
3428 */
3429 if (goya->hw_cap_initialized & HW_CAP_MMU)
3430 return 0;
3431
3432 sob_start_addr = lower_32_bits(CFG_BASE + mmSYNC_MNGR_SOB_OBJ_0);
3433 sob_end_addr = lower_32_bits(CFG_BASE + mmSYNC_MNGR_SOB_OBJ_1023);
3434
3435 if ((le32_to_cpu(wreg_pkt->value) < sob_start_addr) ||
3436 (le32_to_cpu(wreg_pkt->value) > sob_end_addr)) {
3437
3438 dev_err(hdev->dev, "WREG32 packet with illegal value 0x%x\n",
3439 wreg_pkt->value);
3440 return -EPERM;
3441 }
3442
3443 return 0;
3444 }
3445
goya_validate_cb(struct hl_device * hdev,struct hl_cs_parser * parser,bool is_mmu)3446 static int goya_validate_cb(struct hl_device *hdev,
3447 struct hl_cs_parser *parser, bool is_mmu)
3448 {
3449 u32 cb_parsed_length = 0;
3450 int rc = 0;
3451
3452 parser->patched_cb_size = 0;
3453
3454 /* cb_user_size is more than 0 so loop will always be executed */
3455 while (cb_parsed_length < parser->user_cb_size) {
3456 enum packet_id pkt_id;
3457 u16 pkt_size;
3458 struct goya_packet *user_pkt;
3459
3460 user_pkt = (struct goya_packet *) (uintptr_t)
3461 (parser->user_cb->kernel_address + cb_parsed_length);
3462
3463 pkt_id = (enum packet_id) (
3464 (le64_to_cpu(user_pkt->header) &
3465 PACKET_HEADER_PACKET_ID_MASK) >>
3466 PACKET_HEADER_PACKET_ID_SHIFT);
3467
3468 pkt_size = goya_packet_sizes[pkt_id];
3469 cb_parsed_length += pkt_size;
3470 if (cb_parsed_length > parser->user_cb_size) {
3471 dev_err(hdev->dev,
3472 "packet 0x%x is out of CB boundary\n", pkt_id);
3473 rc = -EINVAL;
3474 break;
3475 }
3476
3477 switch (pkt_id) {
3478 case PACKET_WREG_32:
3479 /*
3480 * Although it is validated after copy in patch_cb(),
3481 * need to validate here as well because patch_cb() is
3482 * not called in MMU path while this function is called
3483 */
3484 rc = goya_validate_wreg32(hdev,
3485 parser, (struct packet_wreg32 *) user_pkt);
3486 break;
3487
3488 case PACKET_WREG_BULK:
3489 dev_err(hdev->dev,
3490 "User not allowed to use WREG_BULK\n");
3491 rc = -EPERM;
3492 break;
3493
3494 case PACKET_MSG_PROT:
3495 dev_err(hdev->dev,
3496 "User not allowed to use MSG_PROT\n");
3497 rc = -EPERM;
3498 break;
3499
3500 case PACKET_CP_DMA:
3501 dev_err(hdev->dev, "User not allowed to use CP_DMA\n");
3502 rc = -EPERM;
3503 break;
3504
3505 case PACKET_STOP:
3506 dev_err(hdev->dev, "User not allowed to use STOP\n");
3507 rc = -EPERM;
3508 break;
3509
3510 case PACKET_LIN_DMA:
3511 if (is_mmu)
3512 rc = goya_validate_dma_pkt_mmu(hdev, parser,
3513 (struct packet_lin_dma *) user_pkt);
3514 else
3515 rc = goya_validate_dma_pkt_no_mmu(hdev, parser,
3516 (struct packet_lin_dma *) user_pkt);
3517 break;
3518
3519 case PACKET_MSG_LONG:
3520 case PACKET_MSG_SHORT:
3521 case PACKET_FENCE:
3522 case PACKET_NOP:
3523 parser->patched_cb_size += pkt_size;
3524 break;
3525
3526 default:
3527 dev_err(hdev->dev, "Invalid packet header 0x%x\n",
3528 pkt_id);
3529 rc = -EINVAL;
3530 break;
3531 }
3532
3533 if (rc)
3534 break;
3535 }
3536
3537 /*
3538 * The new CB should have space at the end for two MSG_PROT packets:
3539 * 1. A packet that will act as a completion packet
3540 * 2. A packet that will generate MSI-X interrupt
3541 */
3542 parser->patched_cb_size += sizeof(struct packet_msg_prot) * 2;
3543
3544 return rc;
3545 }
3546
goya_patch_dma_packet(struct hl_device * hdev,struct hl_cs_parser * parser,struct packet_lin_dma * user_dma_pkt,struct packet_lin_dma * new_dma_pkt,u32 * new_dma_pkt_size)3547 static int goya_patch_dma_packet(struct hl_device *hdev,
3548 struct hl_cs_parser *parser,
3549 struct packet_lin_dma *user_dma_pkt,
3550 struct packet_lin_dma *new_dma_pkt,
3551 u32 *new_dma_pkt_size)
3552 {
3553 struct hl_userptr *userptr;
3554 struct scatterlist *sg, *sg_next_iter;
3555 u32 count, dma_desc_cnt;
3556 u64 len, len_next;
3557 dma_addr_t dma_addr, dma_addr_next;
3558 enum goya_dma_direction user_dir;
3559 u64 device_memory_addr, addr;
3560 enum dma_data_direction dir;
3561 struct sg_table *sgt;
3562 bool skip_host_mem_pin = false;
3563 bool user_memset;
3564 u32 user_rdcomp_mask, user_wrcomp_mask, ctl;
3565
3566 ctl = le32_to_cpu(user_dma_pkt->ctl);
3567
3568 user_dir = (ctl & GOYA_PKT_LIN_DMA_CTL_DMA_DIR_MASK) >>
3569 GOYA_PKT_LIN_DMA_CTL_DMA_DIR_SHIFT;
3570
3571 user_memset = (ctl & GOYA_PKT_LIN_DMA_CTL_MEMSET_MASK) >>
3572 GOYA_PKT_LIN_DMA_CTL_MEMSET_SHIFT;
3573
3574 if ((user_dir == DMA_DRAM_TO_SRAM) || (user_dir == DMA_SRAM_TO_DRAM) ||
3575 (user_dma_pkt->tsize == 0)) {
3576 memcpy(new_dma_pkt, user_dma_pkt, sizeof(*new_dma_pkt));
3577 *new_dma_pkt_size = sizeof(*new_dma_pkt);
3578 return 0;
3579 }
3580
3581 if ((user_dir == DMA_HOST_TO_DRAM) || (user_dir == DMA_HOST_TO_SRAM)) {
3582 addr = le64_to_cpu(user_dma_pkt->src_addr);
3583 device_memory_addr = le64_to_cpu(user_dma_pkt->dst_addr);
3584 dir = DMA_TO_DEVICE;
3585 if (user_memset)
3586 skip_host_mem_pin = true;
3587 } else {
3588 addr = le64_to_cpu(user_dma_pkt->dst_addr);
3589 device_memory_addr = le64_to_cpu(user_dma_pkt->src_addr);
3590 dir = DMA_FROM_DEVICE;
3591 }
3592
3593 if ((!skip_host_mem_pin) &&
3594 (hl_userptr_is_pinned(hdev, addr,
3595 le32_to_cpu(user_dma_pkt->tsize),
3596 parser->job_userptr_list, &userptr) == false)) {
3597 dev_err(hdev->dev, "Userptr 0x%llx + 0x%x NOT mapped\n",
3598 addr, user_dma_pkt->tsize);
3599 return -EFAULT;
3600 }
3601
3602 if ((user_memset) && (dir == DMA_TO_DEVICE)) {
3603 memcpy(new_dma_pkt, user_dma_pkt, sizeof(*user_dma_pkt));
3604 *new_dma_pkt_size = sizeof(*user_dma_pkt);
3605 return 0;
3606 }
3607
3608 user_rdcomp_mask = ctl & GOYA_PKT_LIN_DMA_CTL_RDCOMP_MASK;
3609
3610 user_wrcomp_mask = ctl & GOYA_PKT_LIN_DMA_CTL_WRCOMP_MASK;
3611
3612 sgt = userptr->sgt;
3613 dma_desc_cnt = 0;
3614
3615 for_each_sg(sgt->sgl, sg, sgt->nents, count) {
3616 len = sg_dma_len(sg);
3617 dma_addr = sg_dma_address(sg);
3618
3619 if (len == 0)
3620 break;
3621
3622 while ((count + 1) < sgt->nents) {
3623 sg_next_iter = sg_next(sg);
3624 len_next = sg_dma_len(sg_next_iter);
3625 dma_addr_next = sg_dma_address(sg_next_iter);
3626
3627 if (len_next == 0)
3628 break;
3629
3630 if ((dma_addr + len == dma_addr_next) &&
3631 (len + len_next <= DMA_MAX_TRANSFER_SIZE)) {
3632 len += len_next;
3633 count++;
3634 sg = sg_next_iter;
3635 } else {
3636 break;
3637 }
3638 }
3639
3640 ctl = le32_to_cpu(user_dma_pkt->ctl);
3641 if (likely(dma_desc_cnt))
3642 ctl &= ~GOYA_PKT_CTL_EB_MASK;
3643 ctl &= ~(GOYA_PKT_LIN_DMA_CTL_RDCOMP_MASK |
3644 GOYA_PKT_LIN_DMA_CTL_WRCOMP_MASK);
3645 new_dma_pkt->ctl = cpu_to_le32(ctl);
3646 new_dma_pkt->tsize = cpu_to_le32((u32) len);
3647
3648 if (dir == DMA_TO_DEVICE) {
3649 new_dma_pkt->src_addr = cpu_to_le64(dma_addr);
3650 new_dma_pkt->dst_addr = cpu_to_le64(device_memory_addr);
3651 } else {
3652 new_dma_pkt->src_addr = cpu_to_le64(device_memory_addr);
3653 new_dma_pkt->dst_addr = cpu_to_le64(dma_addr);
3654 }
3655
3656 if (!user_memset)
3657 device_memory_addr += len;
3658 dma_desc_cnt++;
3659 new_dma_pkt++;
3660 }
3661
3662 if (!dma_desc_cnt) {
3663 dev_err(hdev->dev,
3664 "Error of 0 SG entries when patching DMA packet\n");
3665 return -EFAULT;
3666 }
3667
3668 /* Fix the last dma packet - rdcomp/wrcomp must be as user set them */
3669 new_dma_pkt--;
3670 new_dma_pkt->ctl |= cpu_to_le32(user_rdcomp_mask | user_wrcomp_mask);
3671
3672 *new_dma_pkt_size = dma_desc_cnt * sizeof(struct packet_lin_dma);
3673
3674 return 0;
3675 }
3676
goya_patch_cb(struct hl_device * hdev,struct hl_cs_parser * parser)3677 static int goya_patch_cb(struct hl_device *hdev,
3678 struct hl_cs_parser *parser)
3679 {
3680 u32 cb_parsed_length = 0;
3681 u32 cb_patched_cur_length = 0;
3682 int rc = 0;
3683
3684 /* cb_user_size is more than 0 so loop will always be executed */
3685 while (cb_parsed_length < parser->user_cb_size) {
3686 enum packet_id pkt_id;
3687 u16 pkt_size;
3688 u32 new_pkt_size = 0;
3689 struct goya_packet *user_pkt, *kernel_pkt;
3690
3691 user_pkt = (struct goya_packet *) (uintptr_t)
3692 (parser->user_cb->kernel_address + cb_parsed_length);
3693 kernel_pkt = (struct goya_packet *) (uintptr_t)
3694 (parser->patched_cb->kernel_address +
3695 cb_patched_cur_length);
3696
3697 pkt_id = (enum packet_id) (
3698 (le64_to_cpu(user_pkt->header) &
3699 PACKET_HEADER_PACKET_ID_MASK) >>
3700 PACKET_HEADER_PACKET_ID_SHIFT);
3701
3702 pkt_size = goya_packet_sizes[pkt_id];
3703 cb_parsed_length += pkt_size;
3704 if (cb_parsed_length > parser->user_cb_size) {
3705 dev_err(hdev->dev,
3706 "packet 0x%x is out of CB boundary\n", pkt_id);
3707 rc = -EINVAL;
3708 break;
3709 }
3710
3711 switch (pkt_id) {
3712 case PACKET_LIN_DMA:
3713 rc = goya_patch_dma_packet(hdev, parser,
3714 (struct packet_lin_dma *) user_pkt,
3715 (struct packet_lin_dma *) kernel_pkt,
3716 &new_pkt_size);
3717 cb_patched_cur_length += new_pkt_size;
3718 break;
3719
3720 case PACKET_WREG_32:
3721 memcpy(kernel_pkt, user_pkt, pkt_size);
3722 cb_patched_cur_length += pkt_size;
3723 rc = goya_validate_wreg32(hdev, parser,
3724 (struct packet_wreg32 *) kernel_pkt);
3725 break;
3726
3727 case PACKET_WREG_BULK:
3728 dev_err(hdev->dev,
3729 "User not allowed to use WREG_BULK\n");
3730 rc = -EPERM;
3731 break;
3732
3733 case PACKET_MSG_PROT:
3734 dev_err(hdev->dev,
3735 "User not allowed to use MSG_PROT\n");
3736 rc = -EPERM;
3737 break;
3738
3739 case PACKET_CP_DMA:
3740 dev_err(hdev->dev, "User not allowed to use CP_DMA\n");
3741 rc = -EPERM;
3742 break;
3743
3744 case PACKET_STOP:
3745 dev_err(hdev->dev, "User not allowed to use STOP\n");
3746 rc = -EPERM;
3747 break;
3748
3749 case PACKET_MSG_LONG:
3750 case PACKET_MSG_SHORT:
3751 case PACKET_FENCE:
3752 case PACKET_NOP:
3753 memcpy(kernel_pkt, user_pkt, pkt_size);
3754 cb_patched_cur_length += pkt_size;
3755 break;
3756
3757 default:
3758 dev_err(hdev->dev, "Invalid packet header 0x%x\n",
3759 pkt_id);
3760 rc = -EINVAL;
3761 break;
3762 }
3763
3764 if (rc)
3765 break;
3766 }
3767
3768 return rc;
3769 }
3770
goya_parse_cb_mmu(struct hl_device * hdev,struct hl_cs_parser * parser)3771 static int goya_parse_cb_mmu(struct hl_device *hdev,
3772 struct hl_cs_parser *parser)
3773 {
3774 u64 patched_cb_handle;
3775 u32 patched_cb_size;
3776 struct hl_cb *user_cb;
3777 int rc;
3778
3779 /*
3780 * The new CB should have space at the end for two MSG_PROT pkt:
3781 * 1. A packet that will act as a completion packet
3782 * 2. A packet that will generate MSI-X interrupt
3783 */
3784 parser->patched_cb_size = parser->user_cb_size +
3785 sizeof(struct packet_msg_prot) * 2;
3786
3787 rc = hl_cb_create(hdev, &hdev->kernel_cb_mgr,
3788 parser->patched_cb_size,
3789 &patched_cb_handle, HL_KERNEL_ASID_ID);
3790
3791 if (rc) {
3792 dev_err(hdev->dev,
3793 "Failed to allocate patched CB for DMA CS %d\n",
3794 rc);
3795 return rc;
3796 }
3797
3798 patched_cb_handle >>= PAGE_SHIFT;
3799 parser->patched_cb = hl_cb_get(hdev, &hdev->kernel_cb_mgr,
3800 (u32) patched_cb_handle);
3801 /* hl_cb_get should never fail here so use kernel WARN */
3802 WARN(!parser->patched_cb, "DMA CB handle invalid 0x%x\n",
3803 (u32) patched_cb_handle);
3804 if (!parser->patched_cb) {
3805 rc = -EFAULT;
3806 goto out;
3807 }
3808
3809 /*
3810 * The check that parser->user_cb_size <= parser->user_cb->size was done
3811 * in validate_queue_index().
3812 */
3813 memcpy((void *) (uintptr_t) parser->patched_cb->kernel_address,
3814 (void *) (uintptr_t) parser->user_cb->kernel_address,
3815 parser->user_cb_size);
3816
3817 patched_cb_size = parser->patched_cb_size;
3818
3819 /* validate patched CB instead of user CB */
3820 user_cb = parser->user_cb;
3821 parser->user_cb = parser->patched_cb;
3822 rc = goya_validate_cb(hdev, parser, true);
3823 parser->user_cb = user_cb;
3824
3825 if (rc) {
3826 hl_cb_put(parser->patched_cb);
3827 goto out;
3828 }
3829
3830 if (patched_cb_size != parser->patched_cb_size) {
3831 dev_err(hdev->dev, "user CB size mismatch\n");
3832 hl_cb_put(parser->patched_cb);
3833 rc = -EINVAL;
3834 goto out;
3835 }
3836
3837 out:
3838 /*
3839 * Always call cb destroy here because we still have 1 reference
3840 * to it by calling cb_get earlier. After the job will be completed,
3841 * cb_put will release it, but here we want to remove it from the
3842 * idr
3843 */
3844 hl_cb_destroy(hdev, &hdev->kernel_cb_mgr,
3845 patched_cb_handle << PAGE_SHIFT);
3846
3847 return rc;
3848 }
3849
goya_parse_cb_no_mmu(struct hl_device * hdev,struct hl_cs_parser * parser)3850 static int goya_parse_cb_no_mmu(struct hl_device *hdev,
3851 struct hl_cs_parser *parser)
3852 {
3853 u64 patched_cb_handle;
3854 int rc;
3855
3856 rc = goya_validate_cb(hdev, parser, false);
3857
3858 if (rc)
3859 goto free_userptr;
3860
3861 rc = hl_cb_create(hdev, &hdev->kernel_cb_mgr,
3862 parser->patched_cb_size,
3863 &patched_cb_handle, HL_KERNEL_ASID_ID);
3864 if (rc) {
3865 dev_err(hdev->dev,
3866 "Failed to allocate patched CB for DMA CS %d\n", rc);
3867 goto free_userptr;
3868 }
3869
3870 patched_cb_handle >>= PAGE_SHIFT;
3871 parser->patched_cb = hl_cb_get(hdev, &hdev->kernel_cb_mgr,
3872 (u32) patched_cb_handle);
3873 /* hl_cb_get should never fail here so use kernel WARN */
3874 WARN(!parser->patched_cb, "DMA CB handle invalid 0x%x\n",
3875 (u32) patched_cb_handle);
3876 if (!parser->patched_cb) {
3877 rc = -EFAULT;
3878 goto out;
3879 }
3880
3881 rc = goya_patch_cb(hdev, parser);
3882
3883 if (rc)
3884 hl_cb_put(parser->patched_cb);
3885
3886 out:
3887 /*
3888 * Always call cb destroy here because we still have 1 reference
3889 * to it by calling cb_get earlier. After the job will be completed,
3890 * cb_put will release it, but here we want to remove it from the
3891 * idr
3892 */
3893 hl_cb_destroy(hdev, &hdev->kernel_cb_mgr,
3894 patched_cb_handle << PAGE_SHIFT);
3895
3896 free_userptr:
3897 if (rc)
3898 hl_userptr_delete_list(hdev, parser->job_userptr_list);
3899 return rc;
3900 }
3901
goya_parse_cb_no_ext_queue(struct hl_device * hdev,struct hl_cs_parser * parser)3902 static int goya_parse_cb_no_ext_queue(struct hl_device *hdev,
3903 struct hl_cs_parser *parser)
3904 {
3905 struct asic_fixed_properties *asic_prop = &hdev->asic_prop;
3906 struct goya_device *goya = hdev->asic_specific;
3907
3908 if (goya->hw_cap_initialized & HW_CAP_MMU)
3909 return 0;
3910
3911 /* For internal queue jobs, just check if CB address is valid */
3912 if (hl_mem_area_inside_range(
3913 (u64) (uintptr_t) parser->user_cb,
3914 parser->user_cb_size,
3915 asic_prop->sram_user_base_address,
3916 asic_prop->sram_end_address))
3917 return 0;
3918
3919 if (hl_mem_area_inside_range(
3920 (u64) (uintptr_t) parser->user_cb,
3921 parser->user_cb_size,
3922 asic_prop->dram_user_base_address,
3923 asic_prop->dram_end_address))
3924 return 0;
3925
3926 dev_err(hdev->dev,
3927 "Internal CB address %px + 0x%x is not in SRAM nor in DRAM\n",
3928 parser->user_cb, parser->user_cb_size);
3929
3930 return -EFAULT;
3931 }
3932
goya_cs_parser(struct hl_device * hdev,struct hl_cs_parser * parser)3933 int goya_cs_parser(struct hl_device *hdev, struct hl_cs_parser *parser)
3934 {
3935 struct goya_device *goya = hdev->asic_specific;
3936
3937 if (!parser->ext_queue)
3938 return goya_parse_cb_no_ext_queue(hdev, parser);
3939
3940 if (goya->hw_cap_initialized & HW_CAP_MMU)
3941 return goya_parse_cb_mmu(hdev, parser);
3942 else
3943 return goya_parse_cb_no_mmu(hdev, parser);
3944 }
3945
goya_add_end_of_cb_packets(struct hl_device * hdev,u64 kernel_address,u32 len,u64 cq_addr,u32 cq_val,u32 msix_vec)3946 void goya_add_end_of_cb_packets(struct hl_device *hdev, u64 kernel_address,
3947 u32 len, u64 cq_addr, u32 cq_val, u32 msix_vec)
3948 {
3949 struct packet_msg_prot *cq_pkt;
3950 u32 tmp;
3951
3952 cq_pkt = (struct packet_msg_prot *) (uintptr_t)
3953 (kernel_address + len - (sizeof(struct packet_msg_prot) * 2));
3954
3955 tmp = (PACKET_MSG_PROT << GOYA_PKT_CTL_OPCODE_SHIFT) |
3956 (1 << GOYA_PKT_CTL_EB_SHIFT) |
3957 (1 << GOYA_PKT_CTL_MB_SHIFT);
3958 cq_pkt->ctl = cpu_to_le32(tmp);
3959 cq_pkt->value = cpu_to_le32(cq_val);
3960 cq_pkt->addr = cpu_to_le64(cq_addr);
3961
3962 cq_pkt++;
3963
3964 tmp = (PACKET_MSG_PROT << GOYA_PKT_CTL_OPCODE_SHIFT) |
3965 (1 << GOYA_PKT_CTL_MB_SHIFT);
3966 cq_pkt->ctl = cpu_to_le32(tmp);
3967 cq_pkt->value = cpu_to_le32(msix_vec & 0x7FF);
3968 cq_pkt->addr = cpu_to_le64(CFG_BASE + mmPCIE_DBI_MSIX_DOORBELL_OFF);
3969 }
3970
goya_update_eq_ci(struct hl_device * hdev,u32 val)3971 void goya_update_eq_ci(struct hl_device *hdev, u32 val)
3972 {
3973 WREG32(mmCPU_EQ_CI, val);
3974 }
3975
goya_restore_phase_topology(struct hl_device * hdev)3976 void goya_restore_phase_topology(struct hl_device *hdev)
3977 {
3978
3979 }
3980
goya_clear_sm_regs(struct hl_device * hdev)3981 static void goya_clear_sm_regs(struct hl_device *hdev)
3982 {
3983 int i, num_of_sob_in_longs, num_of_mon_in_longs;
3984
3985 num_of_sob_in_longs =
3986 ((mmSYNC_MNGR_SOB_OBJ_1023 - mmSYNC_MNGR_SOB_OBJ_0) + 4);
3987
3988 num_of_mon_in_longs =
3989 ((mmSYNC_MNGR_MON_STATUS_255 - mmSYNC_MNGR_MON_STATUS_0) + 4);
3990
3991 for (i = 0 ; i < num_of_sob_in_longs ; i += 4)
3992 WREG32(mmSYNC_MNGR_SOB_OBJ_0 + i, 0);
3993
3994 for (i = 0 ; i < num_of_mon_in_longs ; i += 4)
3995 WREG32(mmSYNC_MNGR_MON_STATUS_0 + i, 0);
3996
3997 /* Flush all WREG to prevent race */
3998 i = RREG32(mmSYNC_MNGR_SOB_OBJ_0);
3999 }
4000
4001 /*
4002 * goya_debugfs_read32 - read a 32bit value from a given device or a host mapped
4003 * address.
4004 *
4005 * @hdev: pointer to hl_device structure
4006 * @addr: device or host mapped address
4007 * @val: returned value
4008 *
4009 * In case of DDR address that is not mapped into the default aperture that
4010 * the DDR bar exposes, the function will configure the iATU so that the DDR
4011 * bar will be positioned at a base address that allows reading from the
4012 * required address. Configuring the iATU during normal operation can
4013 * lead to undefined behavior and therefore, should be done with extreme care
4014 *
4015 */
goya_debugfs_read32(struct hl_device * hdev,u64 addr,u32 * val)4016 static int goya_debugfs_read32(struct hl_device *hdev, u64 addr, u32 *val)
4017 {
4018 struct asic_fixed_properties *prop = &hdev->asic_prop;
4019 u64 ddr_bar_addr;
4020 int rc = 0;
4021
4022 if ((addr >= CFG_BASE) && (addr < CFG_BASE + CFG_SIZE)) {
4023 *val = RREG32(addr - CFG_BASE);
4024
4025 } else if ((addr >= SRAM_BASE_ADDR) &&
4026 (addr < SRAM_BASE_ADDR + SRAM_SIZE)) {
4027
4028 *val = readl(hdev->pcie_bar[SRAM_CFG_BAR_ID] +
4029 (addr - SRAM_BASE_ADDR));
4030
4031 } else if ((addr >= DRAM_PHYS_BASE) &&
4032 (addr < DRAM_PHYS_BASE + hdev->asic_prop.dram_size)) {
4033
4034 u64 bar_base_addr = DRAM_PHYS_BASE +
4035 (addr & ~(prop->dram_pci_bar_size - 0x1ull));
4036
4037 ddr_bar_addr = goya_set_ddr_bar_base(hdev, bar_base_addr);
4038 if (ddr_bar_addr != U64_MAX) {
4039 *val = readl(hdev->pcie_bar[DDR_BAR_ID] +
4040 (addr - bar_base_addr));
4041
4042 ddr_bar_addr = goya_set_ddr_bar_base(hdev,
4043 ddr_bar_addr);
4044 }
4045 if (ddr_bar_addr == U64_MAX)
4046 rc = -EIO;
4047
4048 } else if (addr >= HOST_PHYS_BASE && !iommu_present(&pci_bus_type)) {
4049 *val = *(u32 *) phys_to_virt(addr - HOST_PHYS_BASE);
4050
4051 } else {
4052 rc = -EFAULT;
4053 }
4054
4055 return rc;
4056 }
4057
4058 /*
4059 * goya_debugfs_write32 - write a 32bit value to a given device or a host mapped
4060 * address.
4061 *
4062 * @hdev: pointer to hl_device structure
4063 * @addr: device or host mapped address
4064 * @val: returned value
4065 *
4066 * In case of DDR address that is not mapped into the default aperture that
4067 * the DDR bar exposes, the function will configure the iATU so that the DDR
4068 * bar will be positioned at a base address that allows writing to the
4069 * required address. Configuring the iATU during normal operation can
4070 * lead to undefined behavior and therefore, should be done with extreme care
4071 *
4072 */
goya_debugfs_write32(struct hl_device * hdev,u64 addr,u32 val)4073 static int goya_debugfs_write32(struct hl_device *hdev, u64 addr, u32 val)
4074 {
4075 struct asic_fixed_properties *prop = &hdev->asic_prop;
4076 u64 ddr_bar_addr;
4077 int rc = 0;
4078
4079 if ((addr >= CFG_BASE) && (addr < CFG_BASE + CFG_SIZE)) {
4080 WREG32(addr - CFG_BASE, val);
4081
4082 } else if ((addr >= SRAM_BASE_ADDR) &&
4083 (addr < SRAM_BASE_ADDR + SRAM_SIZE)) {
4084
4085 writel(val, hdev->pcie_bar[SRAM_CFG_BAR_ID] +
4086 (addr - SRAM_BASE_ADDR));
4087
4088 } else if ((addr >= DRAM_PHYS_BASE) &&
4089 (addr < DRAM_PHYS_BASE + hdev->asic_prop.dram_size)) {
4090
4091 u64 bar_base_addr = DRAM_PHYS_BASE +
4092 (addr & ~(prop->dram_pci_bar_size - 0x1ull));
4093
4094 ddr_bar_addr = goya_set_ddr_bar_base(hdev, bar_base_addr);
4095 if (ddr_bar_addr != U64_MAX) {
4096 writel(val, hdev->pcie_bar[DDR_BAR_ID] +
4097 (addr - bar_base_addr));
4098
4099 ddr_bar_addr = goya_set_ddr_bar_base(hdev,
4100 ddr_bar_addr);
4101 }
4102 if (ddr_bar_addr == U64_MAX)
4103 rc = -EIO;
4104
4105 } else if (addr >= HOST_PHYS_BASE && !iommu_present(&pci_bus_type)) {
4106 *(u32 *) phys_to_virt(addr - HOST_PHYS_BASE) = val;
4107
4108 } else {
4109 rc = -EFAULT;
4110 }
4111
4112 return rc;
4113 }
4114
goya_read_pte(struct hl_device * hdev,u64 addr)4115 static u64 goya_read_pte(struct hl_device *hdev, u64 addr)
4116 {
4117 struct goya_device *goya = hdev->asic_specific;
4118
4119 if (hdev->hard_reset_pending)
4120 return U64_MAX;
4121
4122 return readq(hdev->pcie_bar[DDR_BAR_ID] +
4123 (addr - goya->ddr_bar_cur_addr));
4124 }
4125
goya_write_pte(struct hl_device * hdev,u64 addr,u64 val)4126 static void goya_write_pte(struct hl_device *hdev, u64 addr, u64 val)
4127 {
4128 struct goya_device *goya = hdev->asic_specific;
4129
4130 if (hdev->hard_reset_pending)
4131 return;
4132
4133 writeq(val, hdev->pcie_bar[DDR_BAR_ID] +
4134 (addr - goya->ddr_bar_cur_addr));
4135 }
4136
_goya_get_event_desc(u16 event_type)4137 static const char *_goya_get_event_desc(u16 event_type)
4138 {
4139 switch (event_type) {
4140 case GOYA_ASYNC_EVENT_ID_PCIE_IF:
4141 return "PCIe_if";
4142 case GOYA_ASYNC_EVENT_ID_TPC0_ECC:
4143 case GOYA_ASYNC_EVENT_ID_TPC1_ECC:
4144 case GOYA_ASYNC_EVENT_ID_TPC2_ECC:
4145 case GOYA_ASYNC_EVENT_ID_TPC3_ECC:
4146 case GOYA_ASYNC_EVENT_ID_TPC4_ECC:
4147 case GOYA_ASYNC_EVENT_ID_TPC5_ECC:
4148 case GOYA_ASYNC_EVENT_ID_TPC6_ECC:
4149 case GOYA_ASYNC_EVENT_ID_TPC7_ECC:
4150 return "TPC%d_ecc";
4151 case GOYA_ASYNC_EVENT_ID_MME_ECC:
4152 return "MME_ecc";
4153 case GOYA_ASYNC_EVENT_ID_MME_ECC_EXT:
4154 return "MME_ecc_ext";
4155 case GOYA_ASYNC_EVENT_ID_MMU_ECC:
4156 return "MMU_ecc";
4157 case GOYA_ASYNC_EVENT_ID_DMA_MACRO:
4158 return "DMA_macro";
4159 case GOYA_ASYNC_EVENT_ID_DMA_ECC:
4160 return "DMA_ecc";
4161 case GOYA_ASYNC_EVENT_ID_CPU_IF_ECC:
4162 return "CPU_if_ecc";
4163 case GOYA_ASYNC_EVENT_ID_PSOC_MEM:
4164 return "PSOC_mem";
4165 case GOYA_ASYNC_EVENT_ID_PSOC_CORESIGHT:
4166 return "PSOC_coresight";
4167 case GOYA_ASYNC_EVENT_ID_SRAM0 ... GOYA_ASYNC_EVENT_ID_SRAM29:
4168 return "SRAM%d";
4169 case GOYA_ASYNC_EVENT_ID_GIC500:
4170 return "GIC500";
4171 case GOYA_ASYNC_EVENT_ID_PLL0 ... GOYA_ASYNC_EVENT_ID_PLL6:
4172 return "PLL%d";
4173 case GOYA_ASYNC_EVENT_ID_AXI_ECC:
4174 return "AXI_ecc";
4175 case GOYA_ASYNC_EVENT_ID_L2_RAM_ECC:
4176 return "L2_ram_ecc";
4177 case GOYA_ASYNC_EVENT_ID_PSOC_GPIO_05_SW_RESET:
4178 return "PSOC_gpio_05_sw_reset";
4179 case GOYA_ASYNC_EVENT_ID_PSOC_GPIO_10_VRHOT_ICRIT:
4180 return "PSOC_gpio_10_vrhot_icrit";
4181 case GOYA_ASYNC_EVENT_ID_PCIE_DEC:
4182 return "PCIe_dec";
4183 case GOYA_ASYNC_EVENT_ID_TPC0_DEC:
4184 case GOYA_ASYNC_EVENT_ID_TPC1_DEC:
4185 case GOYA_ASYNC_EVENT_ID_TPC2_DEC:
4186 case GOYA_ASYNC_EVENT_ID_TPC3_DEC:
4187 case GOYA_ASYNC_EVENT_ID_TPC4_DEC:
4188 case GOYA_ASYNC_EVENT_ID_TPC5_DEC:
4189 case GOYA_ASYNC_EVENT_ID_TPC6_DEC:
4190 case GOYA_ASYNC_EVENT_ID_TPC7_DEC:
4191 return "TPC%d_dec";
4192 case GOYA_ASYNC_EVENT_ID_MME_WACS:
4193 return "MME_wacs";
4194 case GOYA_ASYNC_EVENT_ID_MME_WACSD:
4195 return "MME_wacsd";
4196 case GOYA_ASYNC_EVENT_ID_CPU_AXI_SPLITTER:
4197 return "CPU_axi_splitter";
4198 case GOYA_ASYNC_EVENT_ID_PSOC_AXI_DEC:
4199 return "PSOC_axi_dec";
4200 case GOYA_ASYNC_EVENT_ID_PSOC:
4201 return "PSOC";
4202 case GOYA_ASYNC_EVENT_ID_TPC0_KRN_ERR:
4203 case GOYA_ASYNC_EVENT_ID_TPC1_KRN_ERR:
4204 case GOYA_ASYNC_EVENT_ID_TPC2_KRN_ERR:
4205 case GOYA_ASYNC_EVENT_ID_TPC3_KRN_ERR:
4206 case GOYA_ASYNC_EVENT_ID_TPC4_KRN_ERR:
4207 case GOYA_ASYNC_EVENT_ID_TPC5_KRN_ERR:
4208 case GOYA_ASYNC_EVENT_ID_TPC6_KRN_ERR:
4209 case GOYA_ASYNC_EVENT_ID_TPC7_KRN_ERR:
4210 return "TPC%d_krn_err";
4211 case GOYA_ASYNC_EVENT_ID_TPC0_CMDQ ... GOYA_ASYNC_EVENT_ID_TPC7_CMDQ:
4212 return "TPC%d_cq";
4213 case GOYA_ASYNC_EVENT_ID_TPC0_QM ... GOYA_ASYNC_EVENT_ID_TPC7_QM:
4214 return "TPC%d_qm";
4215 case GOYA_ASYNC_EVENT_ID_MME_QM:
4216 return "MME_qm";
4217 case GOYA_ASYNC_EVENT_ID_MME_CMDQ:
4218 return "MME_cq";
4219 case GOYA_ASYNC_EVENT_ID_DMA0_QM ... GOYA_ASYNC_EVENT_ID_DMA4_QM:
4220 return "DMA%d_qm";
4221 case GOYA_ASYNC_EVENT_ID_DMA0_CH ... GOYA_ASYNC_EVENT_ID_DMA4_CH:
4222 return "DMA%d_ch";
4223 case GOYA_ASYNC_EVENT_ID_TPC0_BMON_SPMU:
4224 case GOYA_ASYNC_EVENT_ID_TPC1_BMON_SPMU:
4225 case GOYA_ASYNC_EVENT_ID_TPC2_BMON_SPMU:
4226 case GOYA_ASYNC_EVENT_ID_TPC3_BMON_SPMU:
4227 case GOYA_ASYNC_EVENT_ID_TPC4_BMON_SPMU:
4228 case GOYA_ASYNC_EVENT_ID_TPC5_BMON_SPMU:
4229 case GOYA_ASYNC_EVENT_ID_TPC6_BMON_SPMU:
4230 case GOYA_ASYNC_EVENT_ID_TPC7_BMON_SPMU:
4231 return "TPC%d_bmon_spmu";
4232 case GOYA_ASYNC_EVENT_ID_DMA_BM_CH0 ... GOYA_ASYNC_EVENT_ID_DMA_BM_CH4:
4233 return "DMA_bm_ch%d";
4234 default:
4235 return "N/A";
4236 }
4237 }
4238
goya_get_event_desc(u16 event_type,char * desc,size_t size)4239 static void goya_get_event_desc(u16 event_type, char *desc, size_t size)
4240 {
4241 u8 index;
4242
4243 switch (event_type) {
4244 case GOYA_ASYNC_EVENT_ID_TPC0_ECC:
4245 case GOYA_ASYNC_EVENT_ID_TPC1_ECC:
4246 case GOYA_ASYNC_EVENT_ID_TPC2_ECC:
4247 case GOYA_ASYNC_EVENT_ID_TPC3_ECC:
4248 case GOYA_ASYNC_EVENT_ID_TPC4_ECC:
4249 case GOYA_ASYNC_EVENT_ID_TPC5_ECC:
4250 case GOYA_ASYNC_EVENT_ID_TPC6_ECC:
4251 case GOYA_ASYNC_EVENT_ID_TPC7_ECC:
4252 index = (event_type - GOYA_ASYNC_EVENT_ID_TPC0_ECC) / 3;
4253 snprintf(desc, size, _goya_get_event_desc(event_type), index);
4254 break;
4255 case GOYA_ASYNC_EVENT_ID_SRAM0 ... GOYA_ASYNC_EVENT_ID_SRAM29:
4256 index = event_type - GOYA_ASYNC_EVENT_ID_SRAM0;
4257 snprintf(desc, size, _goya_get_event_desc(event_type), index);
4258 break;
4259 case GOYA_ASYNC_EVENT_ID_PLL0 ... GOYA_ASYNC_EVENT_ID_PLL6:
4260 index = event_type - GOYA_ASYNC_EVENT_ID_PLL0;
4261 snprintf(desc, size, _goya_get_event_desc(event_type), index);
4262 break;
4263 case GOYA_ASYNC_EVENT_ID_TPC0_DEC:
4264 case GOYA_ASYNC_EVENT_ID_TPC1_DEC:
4265 case GOYA_ASYNC_EVENT_ID_TPC2_DEC:
4266 case GOYA_ASYNC_EVENT_ID_TPC3_DEC:
4267 case GOYA_ASYNC_EVENT_ID_TPC4_DEC:
4268 case GOYA_ASYNC_EVENT_ID_TPC5_DEC:
4269 case GOYA_ASYNC_EVENT_ID_TPC6_DEC:
4270 case GOYA_ASYNC_EVENT_ID_TPC7_DEC:
4271 index = (event_type - GOYA_ASYNC_EVENT_ID_TPC0_DEC) / 3;
4272 snprintf(desc, size, _goya_get_event_desc(event_type), index);
4273 break;
4274 case GOYA_ASYNC_EVENT_ID_TPC0_KRN_ERR:
4275 case GOYA_ASYNC_EVENT_ID_TPC1_KRN_ERR:
4276 case GOYA_ASYNC_EVENT_ID_TPC2_KRN_ERR:
4277 case GOYA_ASYNC_EVENT_ID_TPC3_KRN_ERR:
4278 case GOYA_ASYNC_EVENT_ID_TPC4_KRN_ERR:
4279 case GOYA_ASYNC_EVENT_ID_TPC5_KRN_ERR:
4280 case GOYA_ASYNC_EVENT_ID_TPC6_KRN_ERR:
4281 case GOYA_ASYNC_EVENT_ID_TPC7_KRN_ERR:
4282 index = (event_type - GOYA_ASYNC_EVENT_ID_TPC0_KRN_ERR) / 10;
4283 snprintf(desc, size, _goya_get_event_desc(event_type), index);
4284 break;
4285 case GOYA_ASYNC_EVENT_ID_TPC0_CMDQ ... GOYA_ASYNC_EVENT_ID_TPC7_CMDQ:
4286 index = event_type - GOYA_ASYNC_EVENT_ID_TPC0_CMDQ;
4287 snprintf(desc, size, _goya_get_event_desc(event_type), index);
4288 break;
4289 case GOYA_ASYNC_EVENT_ID_TPC0_QM ... GOYA_ASYNC_EVENT_ID_TPC7_QM:
4290 index = event_type - GOYA_ASYNC_EVENT_ID_TPC0_QM;
4291 snprintf(desc, size, _goya_get_event_desc(event_type), index);
4292 break;
4293 case GOYA_ASYNC_EVENT_ID_DMA0_QM ... GOYA_ASYNC_EVENT_ID_DMA4_QM:
4294 index = event_type - GOYA_ASYNC_EVENT_ID_DMA0_QM;
4295 snprintf(desc, size, _goya_get_event_desc(event_type), index);
4296 break;
4297 case GOYA_ASYNC_EVENT_ID_DMA0_CH ... GOYA_ASYNC_EVENT_ID_DMA4_CH:
4298 index = event_type - GOYA_ASYNC_EVENT_ID_DMA0_CH;
4299 snprintf(desc, size, _goya_get_event_desc(event_type), index);
4300 break;
4301 case GOYA_ASYNC_EVENT_ID_TPC0_BMON_SPMU:
4302 case GOYA_ASYNC_EVENT_ID_TPC1_BMON_SPMU:
4303 case GOYA_ASYNC_EVENT_ID_TPC2_BMON_SPMU:
4304 case GOYA_ASYNC_EVENT_ID_TPC3_BMON_SPMU:
4305 case GOYA_ASYNC_EVENT_ID_TPC4_BMON_SPMU:
4306 case GOYA_ASYNC_EVENT_ID_TPC5_BMON_SPMU:
4307 case GOYA_ASYNC_EVENT_ID_TPC6_BMON_SPMU:
4308 case GOYA_ASYNC_EVENT_ID_TPC7_BMON_SPMU:
4309 index = (event_type - GOYA_ASYNC_EVENT_ID_TPC0_BMON_SPMU) / 10;
4310 snprintf(desc, size, _goya_get_event_desc(event_type), index);
4311 break;
4312 case GOYA_ASYNC_EVENT_ID_DMA_BM_CH0 ... GOYA_ASYNC_EVENT_ID_DMA_BM_CH4:
4313 index = event_type - GOYA_ASYNC_EVENT_ID_DMA_BM_CH0;
4314 snprintf(desc, size, _goya_get_event_desc(event_type), index);
4315 break;
4316 default:
4317 snprintf(desc, size, _goya_get_event_desc(event_type));
4318 break;
4319 }
4320 }
4321
goya_print_razwi_info(struct hl_device * hdev)4322 static void goya_print_razwi_info(struct hl_device *hdev)
4323 {
4324 if (RREG32(mmDMA_MACRO_RAZWI_LBW_WT_VLD)) {
4325 dev_err(hdev->dev, "Illegal write to LBW\n");
4326 WREG32(mmDMA_MACRO_RAZWI_LBW_WT_VLD, 0);
4327 }
4328
4329 if (RREG32(mmDMA_MACRO_RAZWI_LBW_RD_VLD)) {
4330 dev_err(hdev->dev, "Illegal read from LBW\n");
4331 WREG32(mmDMA_MACRO_RAZWI_LBW_RD_VLD, 0);
4332 }
4333
4334 if (RREG32(mmDMA_MACRO_RAZWI_HBW_WT_VLD)) {
4335 dev_err(hdev->dev, "Illegal write to HBW\n");
4336 WREG32(mmDMA_MACRO_RAZWI_HBW_WT_VLD, 0);
4337 }
4338
4339 if (RREG32(mmDMA_MACRO_RAZWI_HBW_RD_VLD)) {
4340 dev_err(hdev->dev, "Illegal read from HBW\n");
4341 WREG32(mmDMA_MACRO_RAZWI_HBW_RD_VLD, 0);
4342 }
4343 }
4344
goya_print_mmu_error_info(struct hl_device * hdev)4345 static void goya_print_mmu_error_info(struct hl_device *hdev)
4346 {
4347 struct goya_device *goya = hdev->asic_specific;
4348 u64 addr;
4349 u32 val;
4350
4351 if (!(goya->hw_cap_initialized & HW_CAP_MMU))
4352 return;
4353
4354 val = RREG32(mmMMU_PAGE_ERROR_CAPTURE);
4355 if (val & MMU_PAGE_ERROR_CAPTURE_ENTRY_VALID_MASK) {
4356 addr = val & MMU_PAGE_ERROR_CAPTURE_VA_49_32_MASK;
4357 addr <<= 32;
4358 addr |= RREG32(mmMMU_PAGE_ERROR_CAPTURE_VA);
4359
4360 dev_err(hdev->dev, "MMU page fault on va 0x%llx\n", addr);
4361
4362 WREG32(mmMMU_PAGE_ERROR_CAPTURE, 0);
4363 }
4364 }
4365
goya_print_irq_info(struct hl_device * hdev,u16 event_type,bool razwi)4366 static void goya_print_irq_info(struct hl_device *hdev, u16 event_type,
4367 bool razwi)
4368 {
4369 char desc[20] = "";
4370
4371 goya_get_event_desc(event_type, desc, sizeof(desc));
4372 dev_err(hdev->dev, "Received H/W interrupt %d [\"%s\"]\n",
4373 event_type, desc);
4374
4375 if (razwi) {
4376 goya_print_razwi_info(hdev);
4377 goya_print_mmu_error_info(hdev);
4378 }
4379 }
4380
goya_unmask_irq_arr(struct hl_device * hdev,u32 * irq_arr,size_t irq_arr_size)4381 static int goya_unmask_irq_arr(struct hl_device *hdev, u32 *irq_arr,
4382 size_t irq_arr_size)
4383 {
4384 struct armcp_unmask_irq_arr_packet *pkt;
4385 size_t total_pkt_size;
4386 long result;
4387 int rc;
4388 int irq_num_entries, irq_arr_index;
4389 __le32 *goya_irq_arr;
4390
4391 total_pkt_size = sizeof(struct armcp_unmask_irq_arr_packet) +
4392 irq_arr_size;
4393
4394 /* data should be aligned to 8 bytes in order to ArmCP to copy it */
4395 total_pkt_size = (total_pkt_size + 0x7) & ~0x7;
4396
4397 /* total_pkt_size is casted to u16 later on */
4398 if (total_pkt_size > USHRT_MAX) {
4399 dev_err(hdev->dev, "too many elements in IRQ array\n");
4400 return -EINVAL;
4401 }
4402
4403 pkt = kzalloc(total_pkt_size, GFP_KERNEL);
4404 if (!pkt)
4405 return -ENOMEM;
4406
4407 irq_num_entries = irq_arr_size / sizeof(irq_arr[0]);
4408 pkt->length = cpu_to_le32(irq_num_entries);
4409
4410 /* We must perform any necessary endianness conversation on the irq
4411 * array being passed to the goya hardware
4412 */
4413 for (irq_arr_index = 0, goya_irq_arr = (__le32 *) &pkt->irqs;
4414 irq_arr_index < irq_num_entries ; irq_arr_index++)
4415 goya_irq_arr[irq_arr_index] =
4416 cpu_to_le32(irq_arr[irq_arr_index]);
4417
4418 pkt->armcp_pkt.ctl = cpu_to_le32(ARMCP_PACKET_UNMASK_RAZWI_IRQ_ARRAY <<
4419 ARMCP_PKT_CTL_OPCODE_SHIFT);
4420
4421 rc = goya_send_cpu_message(hdev, (u32 *) pkt, total_pkt_size,
4422 HL_DEVICE_TIMEOUT_USEC, &result);
4423
4424 if (rc)
4425 dev_err(hdev->dev, "failed to unmask IRQ array\n");
4426
4427 kfree(pkt);
4428
4429 return rc;
4430 }
4431
goya_soft_reset_late_init(struct hl_device * hdev)4432 static int goya_soft_reset_late_init(struct hl_device *hdev)
4433 {
4434 /*
4435 * Unmask all IRQs since some could have been received
4436 * during the soft reset
4437 */
4438 return goya_unmask_irq_arr(hdev, goya_all_events,
4439 sizeof(goya_all_events));
4440 }
4441
goya_unmask_irq(struct hl_device * hdev,u16 event_type)4442 static int goya_unmask_irq(struct hl_device *hdev, u16 event_type)
4443 {
4444 struct armcp_packet pkt;
4445 long result;
4446 int rc;
4447
4448 memset(&pkt, 0, sizeof(pkt));
4449
4450 pkt.ctl = cpu_to_le32(ARMCP_PACKET_UNMASK_RAZWI_IRQ <<
4451 ARMCP_PKT_CTL_OPCODE_SHIFT);
4452 pkt.value = cpu_to_le64(event_type);
4453
4454 rc = goya_send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
4455 HL_DEVICE_TIMEOUT_USEC, &result);
4456
4457 if (rc)
4458 dev_err(hdev->dev, "failed to unmask RAZWI IRQ %d", event_type);
4459
4460 return rc;
4461 }
4462
goya_handle_eqe(struct hl_device * hdev,struct hl_eq_entry * eq_entry)4463 void goya_handle_eqe(struct hl_device *hdev, struct hl_eq_entry *eq_entry)
4464 {
4465 u32 ctl = le32_to_cpu(eq_entry->hdr.ctl);
4466 u16 event_type = ((ctl & EQ_CTL_EVENT_TYPE_MASK)
4467 >> EQ_CTL_EVENT_TYPE_SHIFT);
4468 struct goya_device *goya = hdev->asic_specific;
4469
4470 goya->events_stat[event_type]++;
4471 goya->events_stat_aggregate[event_type]++;
4472
4473 switch (event_type) {
4474 case GOYA_ASYNC_EVENT_ID_PCIE_IF:
4475 case GOYA_ASYNC_EVENT_ID_TPC0_ECC:
4476 case GOYA_ASYNC_EVENT_ID_TPC1_ECC:
4477 case GOYA_ASYNC_EVENT_ID_TPC2_ECC:
4478 case GOYA_ASYNC_EVENT_ID_TPC3_ECC:
4479 case GOYA_ASYNC_EVENT_ID_TPC4_ECC:
4480 case GOYA_ASYNC_EVENT_ID_TPC5_ECC:
4481 case GOYA_ASYNC_EVENT_ID_TPC6_ECC:
4482 case GOYA_ASYNC_EVENT_ID_TPC7_ECC:
4483 case GOYA_ASYNC_EVENT_ID_MME_ECC:
4484 case GOYA_ASYNC_EVENT_ID_MME_ECC_EXT:
4485 case GOYA_ASYNC_EVENT_ID_MMU_ECC:
4486 case GOYA_ASYNC_EVENT_ID_DMA_MACRO:
4487 case GOYA_ASYNC_EVENT_ID_DMA_ECC:
4488 case GOYA_ASYNC_EVENT_ID_CPU_IF_ECC:
4489 case GOYA_ASYNC_EVENT_ID_PSOC_MEM:
4490 case GOYA_ASYNC_EVENT_ID_PSOC_CORESIGHT:
4491 case GOYA_ASYNC_EVENT_ID_SRAM0 ... GOYA_ASYNC_EVENT_ID_SRAM29:
4492 case GOYA_ASYNC_EVENT_ID_GIC500:
4493 case GOYA_ASYNC_EVENT_ID_PLL0 ... GOYA_ASYNC_EVENT_ID_PLL6:
4494 case GOYA_ASYNC_EVENT_ID_AXI_ECC:
4495 case GOYA_ASYNC_EVENT_ID_L2_RAM_ECC:
4496 case GOYA_ASYNC_EVENT_ID_PSOC_GPIO_05_SW_RESET:
4497 goya_print_irq_info(hdev, event_type, false);
4498 hl_device_reset(hdev, true, false);
4499 break;
4500
4501 case GOYA_ASYNC_EVENT_ID_PCIE_DEC:
4502 case GOYA_ASYNC_EVENT_ID_TPC0_DEC:
4503 case GOYA_ASYNC_EVENT_ID_TPC1_DEC:
4504 case GOYA_ASYNC_EVENT_ID_TPC2_DEC:
4505 case GOYA_ASYNC_EVENT_ID_TPC3_DEC:
4506 case GOYA_ASYNC_EVENT_ID_TPC4_DEC:
4507 case GOYA_ASYNC_EVENT_ID_TPC5_DEC:
4508 case GOYA_ASYNC_EVENT_ID_TPC6_DEC:
4509 case GOYA_ASYNC_EVENT_ID_TPC7_DEC:
4510 case GOYA_ASYNC_EVENT_ID_MME_WACS:
4511 case GOYA_ASYNC_EVENT_ID_MME_WACSD:
4512 case GOYA_ASYNC_EVENT_ID_CPU_AXI_SPLITTER:
4513 case GOYA_ASYNC_EVENT_ID_PSOC_AXI_DEC:
4514 case GOYA_ASYNC_EVENT_ID_PSOC:
4515 case GOYA_ASYNC_EVENT_ID_TPC0_KRN_ERR:
4516 case GOYA_ASYNC_EVENT_ID_TPC1_KRN_ERR:
4517 case GOYA_ASYNC_EVENT_ID_TPC2_KRN_ERR:
4518 case GOYA_ASYNC_EVENT_ID_TPC3_KRN_ERR:
4519 case GOYA_ASYNC_EVENT_ID_TPC4_KRN_ERR:
4520 case GOYA_ASYNC_EVENT_ID_TPC5_KRN_ERR:
4521 case GOYA_ASYNC_EVENT_ID_TPC6_KRN_ERR:
4522 case GOYA_ASYNC_EVENT_ID_TPC7_KRN_ERR:
4523 case GOYA_ASYNC_EVENT_ID_TPC0_CMDQ ... GOYA_ASYNC_EVENT_ID_TPC7_QM:
4524 case GOYA_ASYNC_EVENT_ID_MME_QM:
4525 case GOYA_ASYNC_EVENT_ID_MME_CMDQ:
4526 case GOYA_ASYNC_EVENT_ID_DMA0_QM ... GOYA_ASYNC_EVENT_ID_DMA4_QM:
4527 case GOYA_ASYNC_EVENT_ID_DMA0_CH ... GOYA_ASYNC_EVENT_ID_DMA4_CH:
4528 goya_print_irq_info(hdev, event_type, true);
4529 goya_unmask_irq(hdev, event_type);
4530 break;
4531
4532 case GOYA_ASYNC_EVENT_ID_PSOC_GPIO_10_VRHOT_ICRIT:
4533 case GOYA_ASYNC_EVENT_ID_TPC0_BMON_SPMU:
4534 case GOYA_ASYNC_EVENT_ID_TPC1_BMON_SPMU:
4535 case GOYA_ASYNC_EVENT_ID_TPC2_BMON_SPMU:
4536 case GOYA_ASYNC_EVENT_ID_TPC3_BMON_SPMU:
4537 case GOYA_ASYNC_EVENT_ID_TPC4_BMON_SPMU:
4538 case GOYA_ASYNC_EVENT_ID_TPC5_BMON_SPMU:
4539 case GOYA_ASYNC_EVENT_ID_TPC6_BMON_SPMU:
4540 case GOYA_ASYNC_EVENT_ID_TPC7_BMON_SPMU:
4541 case GOYA_ASYNC_EVENT_ID_DMA_BM_CH0 ... GOYA_ASYNC_EVENT_ID_DMA_BM_CH4:
4542 goya_print_irq_info(hdev, event_type, false);
4543 goya_unmask_irq(hdev, event_type);
4544 break;
4545
4546 default:
4547 dev_err(hdev->dev, "Received invalid H/W interrupt %d\n",
4548 event_type);
4549 break;
4550 }
4551 }
4552
goya_get_events_stat(struct hl_device * hdev,bool aggregate,u32 * size)4553 void *goya_get_events_stat(struct hl_device *hdev, bool aggregate, u32 *size)
4554 {
4555 struct goya_device *goya = hdev->asic_specific;
4556
4557 if (aggregate) {
4558 *size = (u32) sizeof(goya->events_stat_aggregate);
4559 return goya->events_stat_aggregate;
4560 }
4561
4562 *size = (u32) sizeof(goya->events_stat);
4563 return goya->events_stat;
4564 }
4565
goya_memset_device_memory(struct hl_device * hdev,u64 addr,u64 size,u64 val,bool is_dram)4566 static int goya_memset_device_memory(struct hl_device *hdev, u64 addr, u64 size,
4567 u64 val, bool is_dram)
4568 {
4569 struct packet_lin_dma *lin_dma_pkt;
4570 struct hl_cs_job *job;
4571 u32 cb_size, ctl;
4572 struct hl_cb *cb;
4573 int rc, lin_dma_pkts_cnt;
4574
4575 lin_dma_pkts_cnt = DIV_ROUND_UP_ULL(size, SZ_2G);
4576 cb_size = lin_dma_pkts_cnt * sizeof(struct packet_lin_dma) +
4577 sizeof(struct packet_msg_prot);
4578 cb = hl_cb_kernel_create(hdev, cb_size);
4579 if (!cb)
4580 return -ENOMEM;
4581
4582 lin_dma_pkt = (struct packet_lin_dma *) (uintptr_t) cb->kernel_address;
4583
4584 do {
4585 memset(lin_dma_pkt, 0, sizeof(*lin_dma_pkt));
4586
4587 ctl = ((PACKET_LIN_DMA << GOYA_PKT_CTL_OPCODE_SHIFT) |
4588 (1 << GOYA_PKT_LIN_DMA_CTL_MEMSET_SHIFT) |
4589 (1 << GOYA_PKT_LIN_DMA_CTL_WO_SHIFT) |
4590 (1 << GOYA_PKT_CTL_RB_SHIFT) |
4591 (1 << GOYA_PKT_CTL_MB_SHIFT));
4592 ctl |= (is_dram ? DMA_HOST_TO_DRAM : DMA_HOST_TO_SRAM) <<
4593 GOYA_PKT_LIN_DMA_CTL_DMA_DIR_SHIFT;
4594 lin_dma_pkt->ctl = cpu_to_le32(ctl);
4595
4596 lin_dma_pkt->src_addr = cpu_to_le64(val);
4597 lin_dma_pkt->dst_addr = cpu_to_le64(addr);
4598 if (lin_dma_pkts_cnt > 1)
4599 lin_dma_pkt->tsize = cpu_to_le32(SZ_2G);
4600 else
4601 lin_dma_pkt->tsize = cpu_to_le32(size);
4602
4603 size -= SZ_2G;
4604 addr += SZ_2G;
4605 lin_dma_pkt++;
4606 } while (--lin_dma_pkts_cnt);
4607
4608 job = hl_cs_allocate_job(hdev, true);
4609 if (!job) {
4610 dev_err(hdev->dev, "Failed to allocate a new job\n");
4611 rc = -ENOMEM;
4612 goto release_cb;
4613 }
4614
4615 job->id = 0;
4616 job->user_cb = cb;
4617 job->user_cb->cs_cnt++;
4618 job->user_cb_size = cb_size;
4619 job->hw_queue_id = GOYA_QUEUE_ID_DMA_0;
4620 job->patched_cb = job->user_cb;
4621 job->job_cb_size = job->user_cb_size;
4622
4623 hl_debugfs_add_job(hdev, job);
4624
4625 rc = goya_send_job_on_qman0(hdev, job);
4626
4627 hl_cb_put(job->patched_cb);
4628
4629 hl_debugfs_remove_job(hdev, job);
4630 kfree(job);
4631 cb->cs_cnt--;
4632
4633 release_cb:
4634 hl_cb_put(cb);
4635 hl_cb_destroy(hdev, &hdev->kernel_cb_mgr, cb->id << PAGE_SHIFT);
4636
4637 return rc;
4638 }
4639
goya_context_switch(struct hl_device * hdev,u32 asid)4640 int goya_context_switch(struct hl_device *hdev, u32 asid)
4641 {
4642 struct asic_fixed_properties *prop = &hdev->asic_prop;
4643 u64 addr = prop->sram_base_address, sob_addr;
4644 u32 size = hdev->pldm ? 0x10000 : prop->sram_size;
4645 u64 val = 0x7777777777777777ull;
4646 int rc, dma_id;
4647 u32 channel_off = mmDMA_CH_1_WR_COMP_ADDR_LO -
4648 mmDMA_CH_0_WR_COMP_ADDR_LO;
4649
4650 rc = goya_memset_device_memory(hdev, addr, size, val, false);
4651 if (rc) {
4652 dev_err(hdev->dev, "Failed to clear SRAM in context switch\n");
4653 return rc;
4654 }
4655
4656 /* we need to reset registers that the user is allowed to change */
4657 sob_addr = CFG_BASE + mmSYNC_MNGR_SOB_OBJ_1007;
4658 WREG32(mmDMA_CH_0_WR_COMP_ADDR_LO, lower_32_bits(sob_addr));
4659
4660 for (dma_id = 1 ; dma_id < NUMBER_OF_EXT_HW_QUEUES ; dma_id++) {
4661 sob_addr = CFG_BASE + mmSYNC_MNGR_SOB_OBJ_1000 +
4662 (dma_id - 1) * 4;
4663 WREG32(mmDMA_CH_0_WR_COMP_ADDR_LO + channel_off * dma_id,
4664 lower_32_bits(sob_addr));
4665 }
4666
4667 WREG32(mmTPC_PLL_CLK_RLX_0, 0x200020);
4668
4669 goya_mmu_prepare(hdev, asid);
4670
4671 goya_clear_sm_regs(hdev);
4672
4673 return 0;
4674 }
4675
goya_mmu_clear_pgt_range(struct hl_device * hdev)4676 static int goya_mmu_clear_pgt_range(struct hl_device *hdev)
4677 {
4678 struct asic_fixed_properties *prop = &hdev->asic_prop;
4679 struct goya_device *goya = hdev->asic_specific;
4680 u64 addr = prop->mmu_pgt_addr;
4681 u32 size = prop->mmu_pgt_size + MMU_DRAM_DEFAULT_PAGE_SIZE +
4682 MMU_CACHE_MNG_SIZE;
4683
4684 if (!(goya->hw_cap_initialized & HW_CAP_MMU))
4685 return 0;
4686
4687 return goya_memset_device_memory(hdev, addr, size, 0, true);
4688 }
4689
goya_mmu_set_dram_default_page(struct hl_device * hdev)4690 static int goya_mmu_set_dram_default_page(struct hl_device *hdev)
4691 {
4692 struct goya_device *goya = hdev->asic_specific;
4693 u64 addr = hdev->asic_prop.mmu_dram_default_page_addr;
4694 u32 size = MMU_DRAM_DEFAULT_PAGE_SIZE;
4695 u64 val = 0x9999999999999999ull;
4696
4697 if (!(goya->hw_cap_initialized & HW_CAP_MMU))
4698 return 0;
4699
4700 return goya_memset_device_memory(hdev, addr, size, val, true);
4701 }
4702
goya_mmu_add_mappings_for_device_cpu(struct hl_device * hdev)4703 static int goya_mmu_add_mappings_for_device_cpu(struct hl_device *hdev)
4704 {
4705 struct asic_fixed_properties *prop = &hdev->asic_prop;
4706 struct goya_device *goya = hdev->asic_specific;
4707 s64 off, cpu_off;
4708 int rc;
4709
4710 if (!(goya->hw_cap_initialized & HW_CAP_MMU))
4711 return 0;
4712
4713 for (off = 0 ; off < CPU_FW_IMAGE_SIZE ; off += PAGE_SIZE_2MB) {
4714 rc = hl_mmu_map(hdev->kernel_ctx, prop->dram_base_address + off,
4715 prop->dram_base_address + off, PAGE_SIZE_2MB);
4716 if (rc) {
4717 dev_err(hdev->dev, "Map failed for address 0x%llx\n",
4718 prop->dram_base_address + off);
4719 goto unmap;
4720 }
4721 }
4722
4723 if (!(hdev->cpu_accessible_dma_address & (PAGE_SIZE_2MB - 1))) {
4724 rc = hl_mmu_map(hdev->kernel_ctx, VA_CPU_ACCESSIBLE_MEM_ADDR,
4725 hdev->cpu_accessible_dma_address, PAGE_SIZE_2MB);
4726
4727 if (rc) {
4728 dev_err(hdev->dev,
4729 "Map failed for CPU accessible memory\n");
4730 off -= PAGE_SIZE_2MB;
4731 goto unmap;
4732 }
4733 } else {
4734 for (cpu_off = 0 ; cpu_off < SZ_2M ; cpu_off += PAGE_SIZE_4KB) {
4735 rc = hl_mmu_map(hdev->kernel_ctx,
4736 VA_CPU_ACCESSIBLE_MEM_ADDR + cpu_off,
4737 hdev->cpu_accessible_dma_address + cpu_off,
4738 PAGE_SIZE_4KB);
4739 if (rc) {
4740 dev_err(hdev->dev,
4741 "Map failed for CPU accessible memory\n");
4742 cpu_off -= PAGE_SIZE_4KB;
4743 goto unmap_cpu;
4744 }
4745 }
4746 }
4747
4748 goya_mmu_prepare_reg(hdev, mmCPU_IF_ARUSER_OVR, HL_KERNEL_ASID_ID);
4749 goya_mmu_prepare_reg(hdev, mmCPU_IF_AWUSER_OVR, HL_KERNEL_ASID_ID);
4750 WREG32(mmCPU_IF_ARUSER_OVR_EN, 0x7FF);
4751 WREG32(mmCPU_IF_AWUSER_OVR_EN, 0x7FF);
4752
4753 /* Make sure configuration is flushed to device */
4754 RREG32(mmCPU_IF_AWUSER_OVR_EN);
4755
4756 goya->device_cpu_mmu_mappings_done = true;
4757
4758 return 0;
4759
4760 unmap_cpu:
4761 for (; cpu_off >= 0 ; cpu_off -= PAGE_SIZE_4KB)
4762 if (hl_mmu_unmap(hdev->kernel_ctx,
4763 VA_CPU_ACCESSIBLE_MEM_ADDR + cpu_off,
4764 PAGE_SIZE_4KB))
4765 dev_warn_ratelimited(hdev->dev,
4766 "failed to unmap address 0x%llx\n",
4767 VA_CPU_ACCESSIBLE_MEM_ADDR + cpu_off);
4768 unmap:
4769 for (; off >= 0 ; off -= PAGE_SIZE_2MB)
4770 if (hl_mmu_unmap(hdev->kernel_ctx,
4771 prop->dram_base_address + off, PAGE_SIZE_2MB))
4772 dev_warn_ratelimited(hdev->dev,
4773 "failed to unmap address 0x%llx\n",
4774 prop->dram_base_address + off);
4775
4776 return rc;
4777 }
4778
goya_mmu_remove_device_cpu_mappings(struct hl_device * hdev)4779 void goya_mmu_remove_device_cpu_mappings(struct hl_device *hdev)
4780 {
4781 struct asic_fixed_properties *prop = &hdev->asic_prop;
4782 struct goya_device *goya = hdev->asic_specific;
4783 u32 off, cpu_off;
4784
4785 if (!(goya->hw_cap_initialized & HW_CAP_MMU))
4786 return;
4787
4788 if (!goya->device_cpu_mmu_mappings_done)
4789 return;
4790
4791 WREG32(mmCPU_IF_ARUSER_OVR_EN, 0);
4792 WREG32(mmCPU_IF_AWUSER_OVR_EN, 0);
4793
4794 if (!(hdev->cpu_accessible_dma_address & (PAGE_SIZE_2MB - 1))) {
4795 if (hl_mmu_unmap(hdev->kernel_ctx, VA_CPU_ACCESSIBLE_MEM_ADDR,
4796 PAGE_SIZE_2MB))
4797 dev_warn(hdev->dev,
4798 "Failed to unmap CPU accessible memory\n");
4799 } else {
4800 for (cpu_off = 0 ; cpu_off < SZ_2M ; cpu_off += PAGE_SIZE_4KB)
4801 if (hl_mmu_unmap(hdev->kernel_ctx,
4802 VA_CPU_ACCESSIBLE_MEM_ADDR + cpu_off,
4803 PAGE_SIZE_4KB))
4804 dev_warn_ratelimited(hdev->dev,
4805 "failed to unmap address 0x%llx\n",
4806 VA_CPU_ACCESSIBLE_MEM_ADDR + cpu_off);
4807 }
4808
4809 for (off = 0 ; off < CPU_FW_IMAGE_SIZE ; off += PAGE_SIZE_2MB)
4810 if (hl_mmu_unmap(hdev->kernel_ctx,
4811 prop->dram_base_address + off, PAGE_SIZE_2MB))
4812 dev_warn_ratelimited(hdev->dev,
4813 "Failed to unmap address 0x%llx\n",
4814 prop->dram_base_address + off);
4815
4816 goya->device_cpu_mmu_mappings_done = false;
4817 }
4818
goya_mmu_prepare(struct hl_device * hdev,u32 asid)4819 static void goya_mmu_prepare(struct hl_device *hdev, u32 asid)
4820 {
4821 struct goya_device *goya = hdev->asic_specific;
4822 int i;
4823
4824 if (!(goya->hw_cap_initialized & HW_CAP_MMU))
4825 return;
4826
4827 if (asid & ~MME_QM_GLBL_SECURE_PROPS_ASID_MASK) {
4828 WARN(1, "asid %u is too big\n", asid);
4829 return;
4830 }
4831
4832 /* zero the MMBP and ASID bits and then set the ASID */
4833 for (i = 0 ; i < GOYA_MMU_REGS_NUM ; i++)
4834 goya_mmu_prepare_reg(hdev, goya_mmu_regs[i], asid);
4835 }
4836
goya_mmu_invalidate_cache(struct hl_device * hdev,bool is_hard)4837 static void goya_mmu_invalidate_cache(struct hl_device *hdev, bool is_hard)
4838 {
4839 struct goya_device *goya = hdev->asic_specific;
4840 u32 status, timeout_usec;
4841 int rc;
4842
4843 if (!(goya->hw_cap_initialized & HW_CAP_MMU))
4844 return;
4845
4846 /* no need in L1 only invalidation in Goya */
4847 if (!is_hard)
4848 return;
4849
4850 if (hdev->pldm)
4851 timeout_usec = GOYA_PLDM_MMU_TIMEOUT_USEC;
4852 else
4853 timeout_usec = MMU_CONFIG_TIMEOUT_USEC;
4854
4855 mutex_lock(&hdev->mmu_cache_lock);
4856
4857 /* L0 & L1 invalidation */
4858 WREG32(mmSTLB_INV_ALL_START, 1);
4859
4860 rc = hl_poll_timeout(
4861 hdev,
4862 mmSTLB_INV_ALL_START,
4863 status,
4864 !status,
4865 1000,
4866 timeout_usec);
4867
4868 mutex_unlock(&hdev->mmu_cache_lock);
4869
4870 if (rc)
4871 dev_notice_ratelimited(hdev->dev,
4872 "Timeout when waiting for MMU cache invalidation\n");
4873 }
4874
goya_mmu_invalidate_cache_range(struct hl_device * hdev,bool is_hard,u32 asid,u64 va,u64 size)4875 static void goya_mmu_invalidate_cache_range(struct hl_device *hdev,
4876 bool is_hard, u32 asid, u64 va, u64 size)
4877 {
4878 struct goya_device *goya = hdev->asic_specific;
4879 u32 status, timeout_usec, inv_data, pi;
4880 int rc;
4881
4882 if (!(goya->hw_cap_initialized & HW_CAP_MMU))
4883 return;
4884
4885 /* no need in L1 only invalidation in Goya */
4886 if (!is_hard)
4887 return;
4888
4889 if (hdev->pldm)
4890 timeout_usec = GOYA_PLDM_MMU_TIMEOUT_USEC;
4891 else
4892 timeout_usec = MMU_CONFIG_TIMEOUT_USEC;
4893
4894 mutex_lock(&hdev->mmu_cache_lock);
4895
4896 /*
4897 * TODO: currently invalidate entire L0 & L1 as in regular hard
4898 * invalidation. Need to apply invalidation of specific cache lines with
4899 * mask of ASID & VA & size.
4900 * Note that L1 with be flushed entirely in any case.
4901 */
4902
4903 /* L0 & L1 invalidation */
4904 inv_data = RREG32(mmSTLB_CACHE_INV);
4905 /* PI is 8 bit */
4906 pi = ((inv_data & STLB_CACHE_INV_PRODUCER_INDEX_MASK) + 1) & 0xFF;
4907 WREG32(mmSTLB_CACHE_INV,
4908 (inv_data & STLB_CACHE_INV_INDEX_MASK_MASK) | pi);
4909
4910 rc = hl_poll_timeout(
4911 hdev,
4912 mmSTLB_INV_CONSUMER_INDEX,
4913 status,
4914 status == pi,
4915 1000,
4916 timeout_usec);
4917
4918 mutex_unlock(&hdev->mmu_cache_lock);
4919
4920 if (rc)
4921 dev_notice_ratelimited(hdev->dev,
4922 "Timeout when waiting for MMU cache invalidation\n");
4923 }
4924
goya_send_heartbeat(struct hl_device * hdev)4925 int goya_send_heartbeat(struct hl_device *hdev)
4926 {
4927 struct goya_device *goya = hdev->asic_specific;
4928
4929 if (!(goya->hw_cap_initialized & HW_CAP_CPU_Q))
4930 return 0;
4931
4932 return hl_fw_send_heartbeat(hdev);
4933 }
4934
goya_armcp_info_get(struct hl_device * hdev)4935 int goya_armcp_info_get(struct hl_device *hdev)
4936 {
4937 struct goya_device *goya = hdev->asic_specific;
4938 struct asic_fixed_properties *prop = &hdev->asic_prop;
4939 u64 dram_size;
4940 int rc;
4941
4942 if (!(goya->hw_cap_initialized & HW_CAP_CPU_Q))
4943 return 0;
4944
4945 rc = hl_fw_armcp_info_get(hdev);
4946 if (rc)
4947 return rc;
4948
4949 dram_size = le64_to_cpu(prop->armcp_info.dram_size);
4950 if (dram_size) {
4951 if ((!is_power_of_2(dram_size)) ||
4952 (dram_size < DRAM_PHYS_DEFAULT_SIZE)) {
4953 dev_err(hdev->dev,
4954 "F/W reported invalid DRAM size %llu. Trying to use default size\n",
4955 dram_size);
4956 dram_size = DRAM_PHYS_DEFAULT_SIZE;
4957 }
4958
4959 prop->dram_size = dram_size;
4960 prop->dram_end_address = prop->dram_base_address + dram_size;
4961 }
4962
4963 if (!strlen(prop->armcp_info.card_name))
4964 strncpy(prop->armcp_info.card_name, GOYA_DEFAULT_CARD_NAME,
4965 CARD_NAME_MAX_LEN);
4966
4967 return 0;
4968 }
4969
goya_is_device_idle(struct hl_device * hdev,u32 * mask,struct seq_file * s)4970 static bool goya_is_device_idle(struct hl_device *hdev, u32 *mask,
4971 struct seq_file *s)
4972 {
4973 const char *fmt = "%-5d%-9s%#-14x%#-16x%#x\n";
4974 const char *dma_fmt = "%-5d%-9s%#-14x%#x\n";
4975 u32 qm_glbl_sts0, cmdq_glbl_sts0, dma_core_sts0, tpc_cfg_sts,
4976 mme_arch_sts;
4977 bool is_idle = true, is_eng_idle;
4978 u64 offset;
4979 int i;
4980
4981 if (s)
4982 seq_puts(s, "\nDMA is_idle QM_GLBL_STS0 DMA_CORE_STS0\n"
4983 "--- ------- ------------ -------------\n");
4984
4985 offset = mmDMA_QM_1_GLBL_STS0 - mmDMA_QM_0_GLBL_STS0;
4986
4987 for (i = 0 ; i < DMA_MAX_NUM ; i++) {
4988 qm_glbl_sts0 = RREG32(mmDMA_QM_0_GLBL_STS0 + i * offset);
4989 dma_core_sts0 = RREG32(mmDMA_CH_0_STS0 + i * offset);
4990 is_eng_idle = IS_DMA_QM_IDLE(qm_glbl_sts0) &&
4991 IS_DMA_IDLE(dma_core_sts0);
4992 is_idle &= is_eng_idle;
4993
4994 if (mask)
4995 *mask |= !is_eng_idle << (GOYA_ENGINE_ID_DMA_0 + i);
4996 if (s)
4997 seq_printf(s, dma_fmt, i, is_eng_idle ? "Y" : "N",
4998 qm_glbl_sts0, dma_core_sts0);
4999 }
5000
5001 if (s)
5002 seq_puts(s,
5003 "\nTPC is_idle QM_GLBL_STS0 CMDQ_GLBL_STS0 CFG_STATUS\n"
5004 "--- ------- ------------ -------------- ----------\n");
5005
5006 offset = mmTPC1_QM_GLBL_STS0 - mmTPC0_QM_GLBL_STS0;
5007
5008 for (i = 0 ; i < TPC_MAX_NUM ; i++) {
5009 qm_glbl_sts0 = RREG32(mmTPC0_QM_GLBL_STS0 + i * offset);
5010 cmdq_glbl_sts0 = RREG32(mmTPC0_CMDQ_GLBL_STS0 + i * offset);
5011 tpc_cfg_sts = RREG32(mmTPC0_CFG_STATUS + i * offset);
5012 is_eng_idle = IS_TPC_QM_IDLE(qm_glbl_sts0) &&
5013 IS_TPC_CMDQ_IDLE(cmdq_glbl_sts0) &&
5014 IS_TPC_IDLE(tpc_cfg_sts);
5015 is_idle &= is_eng_idle;
5016
5017 if (mask)
5018 *mask |= !is_eng_idle << (GOYA_ENGINE_ID_TPC_0 + i);
5019 if (s)
5020 seq_printf(s, fmt, i, is_eng_idle ? "Y" : "N",
5021 qm_glbl_sts0, cmdq_glbl_sts0, tpc_cfg_sts);
5022 }
5023
5024 if (s)
5025 seq_puts(s,
5026 "\nMME is_idle QM_GLBL_STS0 CMDQ_GLBL_STS0 ARCH_STATUS\n"
5027 "--- ------- ------------ -------------- -----------\n");
5028
5029 qm_glbl_sts0 = RREG32(mmMME_QM_GLBL_STS0);
5030 cmdq_glbl_sts0 = RREG32(mmMME_CMDQ_GLBL_STS0);
5031 mme_arch_sts = RREG32(mmMME_ARCH_STATUS);
5032 is_eng_idle = IS_MME_QM_IDLE(qm_glbl_sts0) &&
5033 IS_MME_CMDQ_IDLE(cmdq_glbl_sts0) &&
5034 IS_MME_IDLE(mme_arch_sts);
5035 is_idle &= is_eng_idle;
5036
5037 if (mask)
5038 *mask |= !is_eng_idle << GOYA_ENGINE_ID_MME_0;
5039 if (s) {
5040 seq_printf(s, fmt, 0, is_eng_idle ? "Y" : "N", qm_glbl_sts0,
5041 cmdq_glbl_sts0, mme_arch_sts);
5042 seq_puts(s, "\n");
5043 }
5044
5045 return is_idle;
5046 }
5047
goya_hw_queues_lock(struct hl_device * hdev)5048 static void goya_hw_queues_lock(struct hl_device *hdev)
5049 {
5050 struct goya_device *goya = hdev->asic_specific;
5051
5052 spin_lock(&goya->hw_queues_lock);
5053 }
5054
goya_hw_queues_unlock(struct hl_device * hdev)5055 static void goya_hw_queues_unlock(struct hl_device *hdev)
5056 {
5057 struct goya_device *goya = hdev->asic_specific;
5058
5059 spin_unlock(&goya->hw_queues_lock);
5060 }
5061
goya_get_pci_id(struct hl_device * hdev)5062 static u32 goya_get_pci_id(struct hl_device *hdev)
5063 {
5064 return hdev->pdev->device;
5065 }
5066
goya_get_eeprom_data(struct hl_device * hdev,void * data,size_t max_size)5067 static int goya_get_eeprom_data(struct hl_device *hdev, void *data,
5068 size_t max_size)
5069 {
5070 struct goya_device *goya = hdev->asic_specific;
5071
5072 if (!(goya->hw_cap_initialized & HW_CAP_CPU_Q))
5073 return 0;
5074
5075 return hl_fw_get_eeprom_data(hdev, data, max_size);
5076 }
5077
goya_get_hw_state(struct hl_device * hdev)5078 static enum hl_device_hw_state goya_get_hw_state(struct hl_device *hdev)
5079 {
5080 return RREG32(mmHW_STATE);
5081 }
5082
5083 static const struct hl_asic_funcs goya_funcs = {
5084 .early_init = goya_early_init,
5085 .early_fini = goya_early_fini,
5086 .late_init = goya_late_init,
5087 .late_fini = goya_late_fini,
5088 .sw_init = goya_sw_init,
5089 .sw_fini = goya_sw_fini,
5090 .hw_init = goya_hw_init,
5091 .hw_fini = goya_hw_fini,
5092 .halt_engines = goya_halt_engines,
5093 .suspend = goya_suspend,
5094 .resume = goya_resume,
5095 .cb_mmap = goya_cb_mmap,
5096 .ring_doorbell = goya_ring_doorbell,
5097 .pqe_write = goya_pqe_write,
5098 .asic_dma_alloc_coherent = goya_dma_alloc_coherent,
5099 .asic_dma_free_coherent = goya_dma_free_coherent,
5100 .get_int_queue_base = goya_get_int_queue_base,
5101 .test_queues = goya_test_queues,
5102 .asic_dma_pool_zalloc = goya_dma_pool_zalloc,
5103 .asic_dma_pool_free = goya_dma_pool_free,
5104 .cpu_accessible_dma_pool_alloc = goya_cpu_accessible_dma_pool_alloc,
5105 .cpu_accessible_dma_pool_free = goya_cpu_accessible_dma_pool_free,
5106 .hl_dma_unmap_sg = goya_dma_unmap_sg,
5107 .cs_parser = goya_cs_parser,
5108 .asic_dma_map_sg = goya_dma_map_sg,
5109 .get_dma_desc_list_size = goya_get_dma_desc_list_size,
5110 .add_end_of_cb_packets = goya_add_end_of_cb_packets,
5111 .update_eq_ci = goya_update_eq_ci,
5112 .context_switch = goya_context_switch,
5113 .restore_phase_topology = goya_restore_phase_topology,
5114 .debugfs_read32 = goya_debugfs_read32,
5115 .debugfs_write32 = goya_debugfs_write32,
5116 .add_device_attr = goya_add_device_attr,
5117 .handle_eqe = goya_handle_eqe,
5118 .set_pll_profile = goya_set_pll_profile,
5119 .get_events_stat = goya_get_events_stat,
5120 .read_pte = goya_read_pte,
5121 .write_pte = goya_write_pte,
5122 .mmu_invalidate_cache = goya_mmu_invalidate_cache,
5123 .mmu_invalidate_cache_range = goya_mmu_invalidate_cache_range,
5124 .send_heartbeat = goya_send_heartbeat,
5125 .debug_coresight = goya_debug_coresight,
5126 .is_device_idle = goya_is_device_idle,
5127 .soft_reset_late_init = goya_soft_reset_late_init,
5128 .hw_queues_lock = goya_hw_queues_lock,
5129 .hw_queues_unlock = goya_hw_queues_unlock,
5130 .get_pci_id = goya_get_pci_id,
5131 .get_eeprom_data = goya_get_eeprom_data,
5132 .send_cpu_message = goya_send_cpu_message,
5133 .get_hw_state = goya_get_hw_state,
5134 .pci_bars_map = goya_pci_bars_map,
5135 .set_dram_bar_base = goya_set_ddr_bar_base,
5136 .init_iatu = goya_init_iatu,
5137 .rreg = hl_rreg,
5138 .wreg = hl_wreg,
5139 .halt_coresight = goya_halt_coresight
5140 };
5141
5142 /*
5143 * goya_set_asic_funcs - set Goya function pointers
5144 *
5145 * @*hdev: pointer to hl_device structure
5146 *
5147 */
goya_set_asic_funcs(struct hl_device * hdev)5148 void goya_set_asic_funcs(struct hl_device *hdev)
5149 {
5150 hdev->asic_funcs = &goya_funcs;
5151 }
5152