1 // SPDX-License-Identifier: GPL-2.0
2 
3 /*
4  * Copyright 2016-2022 HabanaLabs, Ltd.
5  * All Rights Reserved.
6  */
7 
8 #include "habanalabs.h"
9 #include "../include/common/hl_boot_if.h"
10 
11 #include <linux/firmware.h>
12 #include <linux/crc32.h>
13 #include <linux/slab.h>
14 #include <linux/ctype.h>
15 #include <linux/vmalloc.h>
16 
17 #include <trace/events/habanalabs.h>
18 
19 #define FW_FILE_MAX_SIZE		0x1400000 /* maximum size of 20MB */
20 
21 static char *comms_cmd_str_arr[COMMS_INVLD_LAST] = {
22 	[COMMS_NOOP] = __stringify(COMMS_NOOP),
23 	[COMMS_CLR_STS] = __stringify(COMMS_CLR_STS),
24 	[COMMS_RST_STATE] = __stringify(COMMS_RST_STATE),
25 	[COMMS_PREP_DESC] = __stringify(COMMS_PREP_DESC),
26 	[COMMS_DATA_RDY] = __stringify(COMMS_DATA_RDY),
27 	[COMMS_EXEC] = __stringify(COMMS_EXEC),
28 	[COMMS_RST_DEV] = __stringify(COMMS_RST_DEV),
29 	[COMMS_GOTO_WFE] = __stringify(COMMS_GOTO_WFE),
30 	[COMMS_SKIP_BMC] = __stringify(COMMS_SKIP_BMC),
31 	[COMMS_PREP_DESC_ELBI] = __stringify(COMMS_PREP_DESC_ELBI),
32 };
33 
34 static char *comms_sts_str_arr[COMMS_STS_INVLD_LAST] = {
35 	[COMMS_STS_NOOP] = __stringify(COMMS_STS_NOOP),
36 	[COMMS_STS_ACK] = __stringify(COMMS_STS_ACK),
37 	[COMMS_STS_OK] = __stringify(COMMS_STS_OK),
38 	[COMMS_STS_ERR] = __stringify(COMMS_STS_ERR),
39 	[COMMS_STS_VALID_ERR] = __stringify(COMMS_STS_VALID_ERR),
40 	[COMMS_STS_TIMEOUT_ERR] = __stringify(COMMS_STS_TIMEOUT_ERR),
41 };
42 
43 static char *extract_fw_ver_from_str(const char *fw_str)
44 {
45 	char *str, *fw_ver, *whitespace;
46 	u32 ver_offset;
47 
48 	fw_ver = kmalloc(VERSION_MAX_LEN, GFP_KERNEL);
49 	if (!fw_ver)
50 		return NULL;
51 
52 	str = strnstr(fw_str, "fw-", VERSION_MAX_LEN);
53 	if (!str)
54 		goto free_fw_ver;
55 
56 	/* Skip the fw- part */
57 	str += 3;
58 	ver_offset = str - fw_str;
59 
60 	/* Copy until the next whitespace */
61 	whitespace = strnstr(str, " ", VERSION_MAX_LEN - ver_offset);
62 	if (!whitespace)
63 		goto free_fw_ver;
64 
65 	strscpy(fw_ver, str, whitespace - str + 1);
66 
67 	return fw_ver;
68 
69 free_fw_ver:
70 	kfree(fw_ver);
71 	return NULL;
72 }
73 
74 static int extract_fw_sub_versions(struct hl_device *hdev, char *preboot_ver)
75 {
76 	char major[8], minor[8], *first_dot, *second_dot;
77 	int rc;
78 
79 	first_dot = strnstr(preboot_ver, ".", 10);
80 	if (first_dot) {
81 		strscpy(major, preboot_ver, first_dot - preboot_ver + 1);
82 		rc = kstrtou32(major, 10, &hdev->fw_major_version);
83 	} else {
84 		rc = -EINVAL;
85 	}
86 
87 	if (rc) {
88 		dev_err(hdev->dev, "Error %d parsing preboot major version\n", rc);
89 		goto out;
90 	}
91 
92 	/* skip the first dot */
93 	first_dot++;
94 
95 	second_dot = strnstr(first_dot, ".", 10);
96 	if (second_dot) {
97 		strscpy(minor, first_dot, second_dot - first_dot + 1);
98 		rc = kstrtou32(minor, 10, &hdev->fw_minor_version);
99 	} else {
100 		rc = -EINVAL;
101 	}
102 
103 	if (rc)
104 		dev_err(hdev->dev, "Error %d parsing preboot minor version\n", rc);
105 
106 out:
107 	kfree(preboot_ver);
108 	return rc;
109 }
110 
111 static int hl_request_fw(struct hl_device *hdev,
112 				const struct firmware **firmware_p,
113 				const char *fw_name)
114 {
115 	size_t fw_size;
116 	int rc;
117 
118 	rc = request_firmware(firmware_p, fw_name, hdev->dev);
119 	if (rc) {
120 		dev_err(hdev->dev, "Firmware file %s is not found! (error %d)\n",
121 				fw_name, rc);
122 		goto out;
123 	}
124 
125 	fw_size = (*firmware_p)->size;
126 	if ((fw_size % 4) != 0) {
127 		dev_err(hdev->dev, "Illegal %s firmware size %zu\n",
128 				fw_name, fw_size);
129 		rc = -EINVAL;
130 		goto release_fw;
131 	}
132 
133 	dev_dbg(hdev->dev, "%s firmware size == %zu\n", fw_name, fw_size);
134 
135 	if (fw_size > FW_FILE_MAX_SIZE) {
136 		dev_err(hdev->dev,
137 			"FW file size %zu exceeds maximum of %u bytes\n",
138 			fw_size, FW_FILE_MAX_SIZE);
139 		rc = -EINVAL;
140 		goto release_fw;
141 	}
142 
143 	return 0;
144 
145 release_fw:
146 	release_firmware(*firmware_p);
147 out:
148 	return rc;
149 }
150 
151 /**
152  * hl_release_firmware() - release FW
153  *
154  * @fw: fw descriptor
155  *
156  * note: this inline function added to serve as a comprehensive mirror for the
157  *       hl_request_fw function.
158  */
159 static inline void hl_release_firmware(const struct firmware *fw)
160 {
161 	release_firmware(fw);
162 }
163 
164 /**
165  * hl_fw_copy_fw_to_device() - copy FW to device
166  *
167  * @hdev: pointer to hl_device structure.
168  * @fw: fw descriptor
169  * @dst: IO memory mapped address space to copy firmware to
170  * @src_offset: offset in src FW to copy from
171  * @size: amount of bytes to copy (0 to copy the whole binary)
172  *
173  * actual copy of FW binary data to device, shared by static and dynamic loaders
174  */
175 static int hl_fw_copy_fw_to_device(struct hl_device *hdev,
176 				const struct firmware *fw, void __iomem *dst,
177 				u32 src_offset, u32 size)
178 {
179 	const void *fw_data;
180 
181 	/* size 0 indicates to copy the whole file */
182 	if (!size)
183 		size = fw->size;
184 
185 	if (src_offset + size > fw->size) {
186 		dev_err(hdev->dev,
187 			"size to copy(%u) and offset(%u) are invalid\n",
188 			size, src_offset);
189 		return -EINVAL;
190 	}
191 
192 	fw_data = (const void *) fw->data;
193 
194 	memcpy_toio(dst, fw_data + src_offset, size);
195 	return 0;
196 }
197 
198 /**
199  * hl_fw_copy_msg_to_device() - copy message to device
200  *
201  * @hdev: pointer to hl_device structure.
202  * @msg: message
203  * @dst: IO memory mapped address space to copy firmware to
204  * @src_offset: offset in src message to copy from
205  * @size: amount of bytes to copy (0 to copy the whole binary)
206  *
207  * actual copy of message data to device.
208  */
209 static int hl_fw_copy_msg_to_device(struct hl_device *hdev,
210 		struct lkd_msg_comms *msg, void __iomem *dst,
211 		u32 src_offset, u32 size)
212 {
213 	void *msg_data;
214 
215 	/* size 0 indicates to copy the whole file */
216 	if (!size)
217 		size = sizeof(struct lkd_msg_comms);
218 
219 	if (src_offset + size > sizeof(struct lkd_msg_comms)) {
220 		dev_err(hdev->dev,
221 			"size to copy(%u) and offset(%u) are invalid\n",
222 			size, src_offset);
223 		return -EINVAL;
224 	}
225 
226 	msg_data = (void *) msg;
227 
228 	memcpy_toio(dst, msg_data + src_offset, size);
229 
230 	return 0;
231 }
232 
233 /**
234  * hl_fw_load_fw_to_device() - Load F/W code to device's memory.
235  *
236  * @hdev: pointer to hl_device structure.
237  * @fw_name: the firmware image name
238  * @dst: IO memory mapped address space to copy firmware to
239  * @src_offset: offset in src FW to copy from
240  * @size: amount of bytes to copy (0 to copy the whole binary)
241  *
242  * Copy fw code from firmware file to device memory.
243  *
244  * Return: 0 on success, non-zero for failure.
245  */
246 int hl_fw_load_fw_to_device(struct hl_device *hdev, const char *fw_name,
247 				void __iomem *dst, u32 src_offset, u32 size)
248 {
249 	const struct firmware *fw;
250 	int rc;
251 
252 	rc = hl_request_fw(hdev, &fw, fw_name);
253 	if (rc)
254 		return rc;
255 
256 	rc = hl_fw_copy_fw_to_device(hdev, fw, dst, src_offset, size);
257 
258 	hl_release_firmware(fw);
259 	return rc;
260 }
261 
262 int hl_fw_send_pci_access_msg(struct hl_device *hdev, u32 opcode, u64 value)
263 {
264 	struct cpucp_packet pkt = {};
265 
266 	pkt.ctl = cpu_to_le32(opcode << CPUCP_PKT_CTL_OPCODE_SHIFT);
267 	pkt.value = cpu_to_le64(value);
268 
269 	return hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt), 0, NULL);
270 }
271 
272 int hl_fw_send_cpu_message(struct hl_device *hdev, u32 hw_queue_id, u32 *msg,
273 				u16 len, u32 timeout, u64 *result)
274 {
275 	struct hl_hw_queue *queue = &hdev->kernel_queues[hw_queue_id];
276 	struct asic_fixed_properties *prop = &hdev->asic_prop;
277 	struct cpucp_packet *pkt;
278 	dma_addr_t pkt_dma_addr;
279 	struct hl_bd *sent_bd;
280 	u32 tmp, expected_ack_val, pi, opcode;
281 	int rc;
282 
283 	pkt = hl_cpu_accessible_dma_pool_alloc(hdev, len, &pkt_dma_addr);
284 	if (!pkt) {
285 		dev_err(hdev->dev,
286 			"Failed to allocate DMA memory for packet to CPU\n");
287 		return -ENOMEM;
288 	}
289 
290 	memcpy(pkt, msg, len);
291 
292 	mutex_lock(&hdev->send_cpu_message_lock);
293 
294 	/* CPU-CP messages can be sent during soft-reset */
295 	if (hdev->disabled && !hdev->reset_info.in_compute_reset) {
296 		rc = 0;
297 		goto out;
298 	}
299 
300 	if (hdev->device_cpu_disabled) {
301 		rc = -EIO;
302 		goto out;
303 	}
304 
305 	/* set fence to a non valid value */
306 	pkt->fence = cpu_to_le32(UINT_MAX);
307 	pi = queue->pi;
308 
309 	/*
310 	 * The CPU queue is a synchronous queue with an effective depth of
311 	 * a single entry (although it is allocated with room for multiple
312 	 * entries). We lock on it using 'send_cpu_message_lock' which
313 	 * serializes accesses to the CPU queue.
314 	 * Which means that we don't need to lock the access to the entire H/W
315 	 * queues module when submitting a JOB to the CPU queue.
316 	 */
317 	hl_hw_queue_submit_bd(hdev, queue, hl_queue_inc_ptr(queue->pi), len, pkt_dma_addr);
318 
319 	if (prop->fw_app_cpu_boot_dev_sts0 & CPU_BOOT_DEV_STS0_PKT_PI_ACK_EN)
320 		expected_ack_val = queue->pi;
321 	else
322 		expected_ack_val = CPUCP_PACKET_FENCE_VAL;
323 
324 	rc = hl_poll_timeout_memory(hdev, &pkt->fence, tmp,
325 				(tmp == expected_ack_val), 1000,
326 				timeout, true);
327 
328 	hl_hw_queue_inc_ci_kernel(hdev, hw_queue_id);
329 
330 	if (rc == -ETIMEDOUT) {
331 		/* If FW performed reset just before sending it a packet, we will get a timeout.
332 		 * This is expected behavior, hence no need for error message.
333 		 */
334 		if (!hl_device_operational(hdev, NULL) && !hdev->reset_info.in_compute_reset)
335 			dev_dbg(hdev->dev, "Device CPU packet timeout (0x%x) due to FW reset\n",
336 					tmp);
337 		else
338 			dev_err(hdev->dev, "Device CPU packet timeout (0x%x)\n", tmp);
339 		hdev->device_cpu_disabled = true;
340 		goto out;
341 	}
342 
343 	tmp = le32_to_cpu(pkt->ctl);
344 
345 	rc = (tmp & CPUCP_PKT_CTL_RC_MASK) >> CPUCP_PKT_CTL_RC_SHIFT;
346 	if (rc) {
347 		opcode = (tmp & CPUCP_PKT_CTL_OPCODE_MASK) >> CPUCP_PKT_CTL_OPCODE_SHIFT;
348 
349 		if (!prop->supports_advanced_cpucp_rc) {
350 			dev_dbg(hdev->dev, "F/W ERROR %d for CPU packet %d\n", rc, opcode);
351 			rc = -EIO;
352 			goto scrub_descriptor;
353 		}
354 
355 		switch (rc) {
356 		case cpucp_packet_invalid:
357 			dev_err(hdev->dev,
358 				"CPU packet %d is not supported by F/W\n", opcode);
359 			break;
360 		case cpucp_packet_fault:
361 			dev_err(hdev->dev,
362 				"F/W failed processing CPU packet %d\n", opcode);
363 			break;
364 		case cpucp_packet_invalid_pkt:
365 			dev_dbg(hdev->dev,
366 				"CPU packet %d is not supported by F/W\n", opcode);
367 			break;
368 		case cpucp_packet_invalid_params:
369 			dev_err(hdev->dev,
370 				"F/W reports invalid parameters for CPU packet %d\n", opcode);
371 			break;
372 
373 		default:
374 			dev_err(hdev->dev,
375 				"Unknown F/W ERROR %d for CPU packet %d\n", rc, opcode);
376 		}
377 
378 		/* propagate the return code from the f/w to the callers who want to check it */
379 		if (result)
380 			*result = rc;
381 
382 		rc = -EIO;
383 
384 	} else if (result) {
385 		*result = le64_to_cpu(pkt->result);
386 	}
387 
388 scrub_descriptor:
389 	/* Scrub previous buffer descriptor 'ctl' field which contains the
390 	 * previous PI value written during packet submission.
391 	 * We must do this or else F/W can read an old value upon queue wraparound.
392 	 */
393 	sent_bd = queue->kernel_address;
394 	sent_bd += hl_pi_2_offset(pi);
395 	sent_bd->ctl = cpu_to_le32(UINT_MAX);
396 
397 out:
398 	mutex_unlock(&hdev->send_cpu_message_lock);
399 
400 	hl_cpu_accessible_dma_pool_free(hdev, len, pkt);
401 
402 	return rc;
403 }
404 
405 int hl_fw_unmask_irq(struct hl_device *hdev, u16 event_type)
406 {
407 	struct cpucp_packet pkt;
408 	u64 result;
409 	int rc;
410 
411 	memset(&pkt, 0, sizeof(pkt));
412 
413 	pkt.ctl = cpu_to_le32(CPUCP_PACKET_UNMASK_RAZWI_IRQ <<
414 				CPUCP_PKT_CTL_OPCODE_SHIFT);
415 	pkt.value = cpu_to_le64(event_type);
416 
417 	rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
418 						0, &result);
419 
420 	if (rc)
421 		dev_err(hdev->dev, "failed to unmask RAZWI IRQ %d", event_type);
422 
423 	return rc;
424 }
425 
426 int hl_fw_unmask_irq_arr(struct hl_device *hdev, const u32 *irq_arr,
427 		size_t irq_arr_size)
428 {
429 	struct cpucp_unmask_irq_arr_packet *pkt;
430 	size_t total_pkt_size;
431 	u64 result;
432 	int rc;
433 
434 	total_pkt_size = sizeof(struct cpucp_unmask_irq_arr_packet) +
435 			irq_arr_size;
436 
437 	/* data should be aligned to 8 bytes in order to CPU-CP to copy it */
438 	total_pkt_size = (total_pkt_size + 0x7) & ~0x7;
439 
440 	/* total_pkt_size is casted to u16 later on */
441 	if (total_pkt_size > USHRT_MAX) {
442 		dev_err(hdev->dev, "too many elements in IRQ array\n");
443 		return -EINVAL;
444 	}
445 
446 	pkt = kzalloc(total_pkt_size, GFP_KERNEL);
447 	if (!pkt)
448 		return -ENOMEM;
449 
450 	pkt->length = cpu_to_le32(irq_arr_size / sizeof(irq_arr[0]));
451 	memcpy(&pkt->irqs, irq_arr, irq_arr_size);
452 
453 	pkt->cpucp_pkt.ctl = cpu_to_le32(CPUCP_PACKET_UNMASK_RAZWI_IRQ_ARRAY <<
454 						CPUCP_PKT_CTL_OPCODE_SHIFT);
455 
456 	rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) pkt,
457 						total_pkt_size, 0, &result);
458 
459 	if (rc)
460 		dev_err(hdev->dev, "failed to unmask IRQ array\n");
461 
462 	kfree(pkt);
463 
464 	return rc;
465 }
466 
467 int hl_fw_test_cpu_queue(struct hl_device *hdev)
468 {
469 	struct cpucp_packet test_pkt = {};
470 	u64 result;
471 	int rc;
472 
473 	test_pkt.ctl = cpu_to_le32(CPUCP_PACKET_TEST <<
474 					CPUCP_PKT_CTL_OPCODE_SHIFT);
475 	test_pkt.value = cpu_to_le64(CPUCP_PACKET_FENCE_VAL);
476 
477 	rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &test_pkt,
478 						sizeof(test_pkt), 0, &result);
479 
480 	if (!rc) {
481 		if (result != CPUCP_PACKET_FENCE_VAL)
482 			dev_err(hdev->dev,
483 				"CPU queue test failed (%#08llx)\n", result);
484 	} else {
485 		dev_err(hdev->dev, "CPU queue test failed, error %d\n", rc);
486 	}
487 
488 	return rc;
489 }
490 
491 void *hl_fw_cpu_accessible_dma_pool_alloc(struct hl_device *hdev, size_t size,
492 						dma_addr_t *dma_handle)
493 {
494 	u64 kernel_addr;
495 
496 	kernel_addr = gen_pool_alloc(hdev->cpu_accessible_dma_pool, size);
497 
498 	*dma_handle = hdev->cpu_accessible_dma_address +
499 		(kernel_addr - (u64) (uintptr_t) hdev->cpu_accessible_dma_mem);
500 
501 	return (void *) (uintptr_t) kernel_addr;
502 }
503 
504 void hl_fw_cpu_accessible_dma_pool_free(struct hl_device *hdev, size_t size,
505 					void *vaddr)
506 {
507 	gen_pool_free(hdev->cpu_accessible_dma_pool, (u64) (uintptr_t) vaddr,
508 			size);
509 }
510 
511 int hl_fw_send_device_activity(struct hl_device *hdev, bool open)
512 {
513 	struct cpucp_packet pkt;
514 	int rc;
515 
516 	memset(&pkt, 0, sizeof(pkt));
517 	pkt.ctl = cpu_to_le32(CPUCP_PACKET_ACTIVE_STATUS_SET <<	CPUCP_PKT_CTL_OPCODE_SHIFT);
518 	pkt.value = cpu_to_le64(open);
519 	rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt), 0, NULL);
520 	if (rc)
521 		dev_err(hdev->dev, "failed to send device activity msg(%u)\n", open);
522 
523 	return rc;
524 }
525 
526 int hl_fw_send_heartbeat(struct hl_device *hdev)
527 {
528 	struct cpucp_packet hb_pkt;
529 	u64 result;
530 	int rc;
531 
532 	memset(&hb_pkt, 0, sizeof(hb_pkt));
533 	hb_pkt.ctl = cpu_to_le32(CPUCP_PACKET_TEST <<
534 					CPUCP_PKT_CTL_OPCODE_SHIFT);
535 	hb_pkt.value = cpu_to_le64(CPUCP_PACKET_FENCE_VAL);
536 
537 	rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &hb_pkt,
538 						sizeof(hb_pkt), 0, &result);
539 
540 	if ((rc) || (result != CPUCP_PACKET_FENCE_VAL))
541 		return -EIO;
542 
543 	if (le32_to_cpu(hb_pkt.status_mask) &
544 					CPUCP_PKT_HB_STATUS_EQ_FAULT_MASK) {
545 		dev_warn(hdev->dev, "FW reported EQ fault during heartbeat\n");
546 		rc = -EIO;
547 	}
548 
549 	return rc;
550 }
551 
552 static bool fw_report_boot_dev0(struct hl_device *hdev, u32 err_val,
553 								u32 sts_val)
554 {
555 	bool err_exists = false;
556 
557 	if (!(err_val & CPU_BOOT_ERR0_ENABLED))
558 		return false;
559 
560 	if (err_val & CPU_BOOT_ERR0_DRAM_INIT_FAIL) {
561 		dev_err(hdev->dev,
562 			"Device boot error - DRAM initialization failed\n");
563 		err_exists = true;
564 	}
565 
566 	if (err_val & CPU_BOOT_ERR0_FIT_CORRUPTED) {
567 		dev_err(hdev->dev, "Device boot error - FIT image corrupted\n");
568 		err_exists = true;
569 	}
570 
571 	if (err_val & CPU_BOOT_ERR0_TS_INIT_FAIL) {
572 		dev_err(hdev->dev,
573 			"Device boot error - Thermal Sensor initialization failed\n");
574 		err_exists = true;
575 	}
576 
577 	if (err_val & CPU_BOOT_ERR0_BMC_WAIT_SKIPPED) {
578 		if (hdev->bmc_enable) {
579 			dev_err(hdev->dev,
580 				"Device boot error - Skipped waiting for BMC\n");
581 			err_exists = true;
582 		} else {
583 			dev_info(hdev->dev,
584 				"Device boot message - Skipped waiting for BMC\n");
585 			/* This is an info so we don't want it to disable the
586 			 * device
587 			 */
588 			err_val &= ~CPU_BOOT_ERR0_BMC_WAIT_SKIPPED;
589 		}
590 	}
591 
592 	if (err_val & CPU_BOOT_ERR0_NIC_DATA_NOT_RDY) {
593 		dev_err(hdev->dev,
594 			"Device boot error - Serdes data from BMC not available\n");
595 		err_exists = true;
596 	}
597 
598 	if (err_val & CPU_BOOT_ERR0_NIC_FW_FAIL) {
599 		dev_err(hdev->dev,
600 			"Device boot error - NIC F/W initialization failed\n");
601 		err_exists = true;
602 	}
603 
604 	if (err_val & CPU_BOOT_ERR0_SECURITY_NOT_RDY) {
605 		dev_err(hdev->dev,
606 			"Device boot warning - security not ready\n");
607 		err_exists = true;
608 	}
609 
610 	if (err_val & CPU_BOOT_ERR0_SECURITY_FAIL) {
611 		dev_err(hdev->dev, "Device boot error - security failure\n");
612 		err_exists = true;
613 	}
614 
615 	if (err_val & CPU_BOOT_ERR0_EFUSE_FAIL) {
616 		dev_err(hdev->dev, "Device boot error - eFuse failure\n");
617 		err_exists = true;
618 	}
619 
620 	if (err_val & CPU_BOOT_ERR0_SEC_IMG_VER_FAIL) {
621 		dev_err(hdev->dev, "Device boot error - Failed to load preboot secondary image\n");
622 		err_exists = true;
623 	}
624 
625 	if (err_val & CPU_BOOT_ERR0_PLL_FAIL) {
626 		dev_err(hdev->dev, "Device boot error - PLL failure\n");
627 		err_exists = true;
628 	}
629 
630 	if (err_val & CPU_BOOT_ERR0_DEVICE_UNUSABLE_FAIL) {
631 		/* Ignore this bit, don't prevent driver loading */
632 		dev_dbg(hdev->dev, "device unusable status is set\n");
633 		err_val &= ~CPU_BOOT_ERR0_DEVICE_UNUSABLE_FAIL;
634 	}
635 
636 	if (err_val & CPU_BOOT_ERR0_BINNING_FAIL) {
637 		dev_err(hdev->dev, "Device boot error - binning failure\n");
638 		err_exists = true;
639 	}
640 
641 	if (sts_val & CPU_BOOT_DEV_STS0_ENABLED)
642 		dev_dbg(hdev->dev, "Device status0 %#x\n", sts_val);
643 
644 	if (err_val & CPU_BOOT_ERR0_EEPROM_FAIL) {
645 		dev_err(hdev->dev, "Device boot error - EEPROM failure detected\n");
646 		err_exists = true;
647 	}
648 
649 	/* All warnings should go here in order not to reach the unknown error validation */
650 	if (err_val & CPU_BOOT_ERR0_DRAM_SKIPPED) {
651 		dev_warn(hdev->dev,
652 			"Device boot warning - Skipped DRAM initialization\n");
653 		/* This is a warning so we don't want it to disable the
654 		 * device
655 		 */
656 		err_val &= ~CPU_BOOT_ERR0_DRAM_SKIPPED;
657 	}
658 
659 	if (err_val & CPU_BOOT_ERR0_PRI_IMG_VER_FAIL) {
660 		dev_warn(hdev->dev,
661 			"Device boot warning - Failed to load preboot primary image\n");
662 		/* This is a warning so we don't want it to disable the
663 		 * device as we have a secondary preboot image
664 		 */
665 		err_val &= ~CPU_BOOT_ERR0_PRI_IMG_VER_FAIL;
666 	}
667 
668 	if (err_val & CPU_BOOT_ERR0_TPM_FAIL) {
669 		dev_warn(hdev->dev,
670 			"Device boot warning - TPM failure\n");
671 		/* This is a warning so we don't want it to disable the
672 		 * device
673 		 */
674 		err_val &= ~CPU_BOOT_ERR0_TPM_FAIL;
675 	}
676 
677 	if (!err_exists && (err_val & ~CPU_BOOT_ERR0_ENABLED)) {
678 		dev_err(hdev->dev,
679 			"Device boot error - unknown ERR0 error 0x%08x\n", err_val);
680 		err_exists = true;
681 	}
682 
683 	/* return error only if it's in the predefined mask */
684 	if (err_exists && ((err_val & ~CPU_BOOT_ERR0_ENABLED) &
685 				lower_32_bits(hdev->boot_error_status_mask)))
686 		return true;
687 
688 	return false;
689 }
690 
691 /* placeholder for ERR1 as no errors defined there yet */
692 static bool fw_report_boot_dev1(struct hl_device *hdev, u32 err_val,
693 								u32 sts_val)
694 {
695 	/*
696 	 * keep this variable to preserve the logic of the function.
697 	 * this way it would require less modifications when error will be
698 	 * added to DEV_ERR1
699 	 */
700 	bool err_exists = false;
701 
702 	if (!(err_val & CPU_BOOT_ERR1_ENABLED))
703 		return false;
704 
705 	if (sts_val & CPU_BOOT_DEV_STS1_ENABLED)
706 		dev_dbg(hdev->dev, "Device status1 %#x\n", sts_val);
707 
708 	if (!err_exists && (err_val & ~CPU_BOOT_ERR1_ENABLED)) {
709 		dev_err(hdev->dev,
710 			"Device boot error - unknown ERR1 error 0x%08x\n",
711 								err_val);
712 		err_exists = true;
713 	}
714 
715 	/* return error only if it's in the predefined mask */
716 	if (err_exists && ((err_val & ~CPU_BOOT_ERR1_ENABLED) &
717 				upper_32_bits(hdev->boot_error_status_mask)))
718 		return true;
719 
720 	return false;
721 }
722 
723 static int fw_read_errors(struct hl_device *hdev, u32 boot_err0_reg,
724 				u32 boot_err1_reg, u32 cpu_boot_dev_status0_reg,
725 				u32 cpu_boot_dev_status1_reg)
726 {
727 	u32 err_val, status_val;
728 	bool err_exists = false;
729 
730 	/* Some of the firmware status codes are deprecated in newer f/w
731 	 * versions. In those versions, the errors are reported
732 	 * in different registers. Therefore, we need to check those
733 	 * registers and print the exact errors. Moreover, there
734 	 * may be multiple errors, so we need to report on each error
735 	 * separately. Some of the error codes might indicate a state
736 	 * that is not an error per-se, but it is an error in production
737 	 * environment
738 	 */
739 	err_val = RREG32(boot_err0_reg);
740 	status_val = RREG32(cpu_boot_dev_status0_reg);
741 	err_exists = fw_report_boot_dev0(hdev, err_val, status_val);
742 
743 	err_val = RREG32(boot_err1_reg);
744 	status_val = RREG32(cpu_boot_dev_status1_reg);
745 	err_exists |= fw_report_boot_dev1(hdev, err_val, status_val);
746 
747 	if (err_exists)
748 		return -EIO;
749 
750 	return 0;
751 }
752 
753 int hl_fw_cpucp_info_get(struct hl_device *hdev,
754 				u32 sts_boot_dev_sts0_reg,
755 				u32 sts_boot_dev_sts1_reg, u32 boot_err0_reg,
756 				u32 boot_err1_reg)
757 {
758 	struct asic_fixed_properties *prop = &hdev->asic_prop;
759 	struct cpucp_packet pkt = {};
760 	dma_addr_t cpucp_info_dma_addr;
761 	void *cpucp_info_cpu_addr;
762 	char *kernel_ver;
763 	u64 result;
764 	int rc;
765 
766 	cpucp_info_cpu_addr = hl_cpu_accessible_dma_pool_alloc(hdev, sizeof(struct cpucp_info),
767 								&cpucp_info_dma_addr);
768 	if (!cpucp_info_cpu_addr) {
769 		dev_err(hdev->dev,
770 			"Failed to allocate DMA memory for CPU-CP info packet\n");
771 		return -ENOMEM;
772 	}
773 
774 	memset(cpucp_info_cpu_addr, 0, sizeof(struct cpucp_info));
775 
776 	pkt.ctl = cpu_to_le32(CPUCP_PACKET_INFO_GET <<
777 				CPUCP_PKT_CTL_OPCODE_SHIFT);
778 	pkt.addr = cpu_to_le64(cpucp_info_dma_addr);
779 	pkt.data_max_size = cpu_to_le32(sizeof(struct cpucp_info));
780 
781 	rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
782 					HL_CPUCP_INFO_TIMEOUT_USEC, &result);
783 	if (rc) {
784 		dev_err(hdev->dev,
785 			"Failed to handle CPU-CP info pkt, error %d\n", rc);
786 		goto out;
787 	}
788 
789 	rc = fw_read_errors(hdev, boot_err0_reg, boot_err1_reg,
790 				sts_boot_dev_sts0_reg, sts_boot_dev_sts1_reg);
791 	if (rc) {
792 		dev_err(hdev->dev, "Errors in device boot\n");
793 		goto out;
794 	}
795 
796 	memcpy(&prop->cpucp_info, cpucp_info_cpu_addr,
797 			sizeof(prop->cpucp_info));
798 
799 	rc = hl_build_hwmon_channel_info(hdev, prop->cpucp_info.sensors);
800 	if (rc) {
801 		dev_err(hdev->dev,
802 			"Failed to build hwmon channel info, error %d\n", rc);
803 		rc = -EFAULT;
804 		goto out;
805 	}
806 
807 	kernel_ver = extract_fw_ver_from_str(prop->cpucp_info.kernel_version);
808 	if (kernel_ver) {
809 		dev_info(hdev->dev, "Linux version %s", kernel_ver);
810 		kfree(kernel_ver);
811 	}
812 
813 	/* assume EQ code doesn't need to check eqe index */
814 	hdev->event_queue.check_eqe_index = false;
815 
816 	/* Read FW application security bits again */
817 	if (prop->fw_cpu_boot_dev_sts0_valid) {
818 		prop->fw_app_cpu_boot_dev_sts0 = RREG32(sts_boot_dev_sts0_reg);
819 		if (prop->fw_app_cpu_boot_dev_sts0 &
820 				CPU_BOOT_DEV_STS0_EQ_INDEX_EN)
821 			hdev->event_queue.check_eqe_index = true;
822 	}
823 
824 	if (prop->fw_cpu_boot_dev_sts1_valid)
825 		prop->fw_app_cpu_boot_dev_sts1 = RREG32(sts_boot_dev_sts1_reg);
826 
827 out:
828 	hl_cpu_accessible_dma_pool_free(hdev, sizeof(struct cpucp_info), cpucp_info_cpu_addr);
829 
830 	return rc;
831 }
832 
833 static int hl_fw_send_msi_info_msg(struct hl_device *hdev)
834 {
835 	struct cpucp_array_data_packet *pkt;
836 	size_t total_pkt_size, data_size;
837 	u64 result;
838 	int rc;
839 
840 	/* skip sending this info for unsupported ASICs */
841 	if (!hdev->asic_funcs->get_msi_info)
842 		return 0;
843 
844 	data_size = CPUCP_NUM_OF_MSI_TYPES * sizeof(u32);
845 	total_pkt_size = sizeof(struct cpucp_array_data_packet) + data_size;
846 
847 	/* data should be aligned to 8 bytes in order to CPU-CP to copy it */
848 	total_pkt_size = (total_pkt_size + 0x7) & ~0x7;
849 
850 	/* total_pkt_size is casted to u16 later on */
851 	if (total_pkt_size > USHRT_MAX) {
852 		dev_err(hdev->dev, "CPUCP array data is too big\n");
853 		return -EINVAL;
854 	}
855 
856 	pkt = kzalloc(total_pkt_size, GFP_KERNEL);
857 	if (!pkt)
858 		return -ENOMEM;
859 
860 	pkt->length = cpu_to_le32(CPUCP_NUM_OF_MSI_TYPES);
861 
862 	memset((void *) &pkt->data, 0xFF, data_size);
863 	hdev->asic_funcs->get_msi_info(pkt->data);
864 
865 	pkt->cpucp_pkt.ctl = cpu_to_le32(CPUCP_PACKET_MSI_INFO_SET <<
866 						CPUCP_PKT_CTL_OPCODE_SHIFT);
867 
868 	rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *)pkt,
869 						total_pkt_size, 0, &result);
870 
871 	/*
872 	 * in case packet result is invalid it means that FW does not support
873 	 * this feature and will use default/hard coded MSI values. no reason
874 	 * to stop the boot
875 	 */
876 	if (rc && result == cpucp_packet_invalid)
877 		rc = 0;
878 
879 	if (rc)
880 		dev_err(hdev->dev, "failed to send CPUCP array data\n");
881 
882 	kfree(pkt);
883 
884 	return rc;
885 }
886 
887 int hl_fw_cpucp_handshake(struct hl_device *hdev,
888 				u32 sts_boot_dev_sts0_reg,
889 				u32 sts_boot_dev_sts1_reg, u32 boot_err0_reg,
890 				u32 boot_err1_reg)
891 {
892 	int rc;
893 
894 	rc = hl_fw_cpucp_info_get(hdev, sts_boot_dev_sts0_reg,
895 					sts_boot_dev_sts1_reg, boot_err0_reg,
896 					boot_err1_reg);
897 	if (rc)
898 		return rc;
899 
900 	return hl_fw_send_msi_info_msg(hdev);
901 }
902 
903 int hl_fw_get_eeprom_data(struct hl_device *hdev, void *data, size_t max_size)
904 {
905 	struct cpucp_packet pkt = {};
906 	void *eeprom_info_cpu_addr;
907 	dma_addr_t eeprom_info_dma_addr;
908 	u64 result;
909 	int rc;
910 
911 	eeprom_info_cpu_addr = hl_cpu_accessible_dma_pool_alloc(hdev, max_size,
912 									&eeprom_info_dma_addr);
913 	if (!eeprom_info_cpu_addr) {
914 		dev_err(hdev->dev,
915 			"Failed to allocate DMA memory for CPU-CP EEPROM packet\n");
916 		return -ENOMEM;
917 	}
918 
919 	memset(eeprom_info_cpu_addr, 0, max_size);
920 
921 	pkt.ctl = cpu_to_le32(CPUCP_PACKET_EEPROM_DATA_GET <<
922 				CPUCP_PKT_CTL_OPCODE_SHIFT);
923 	pkt.addr = cpu_to_le64(eeprom_info_dma_addr);
924 	pkt.data_max_size = cpu_to_le32(max_size);
925 
926 	rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
927 			HL_CPUCP_EEPROM_TIMEOUT_USEC, &result);
928 
929 	if (rc) {
930 		dev_err(hdev->dev,
931 			"Failed to handle CPU-CP EEPROM packet, error %d\n",
932 			rc);
933 		goto out;
934 	}
935 
936 	/* result contains the actual size */
937 	memcpy(data, eeprom_info_cpu_addr, min((size_t)result, max_size));
938 
939 out:
940 	hl_cpu_accessible_dma_pool_free(hdev, max_size, eeprom_info_cpu_addr);
941 
942 	return rc;
943 }
944 
945 int hl_fw_get_monitor_dump(struct hl_device *hdev, void *data)
946 {
947 	struct cpucp_monitor_dump *mon_dump_cpu_addr;
948 	dma_addr_t mon_dump_dma_addr;
949 	struct cpucp_packet pkt = {};
950 	size_t data_size;
951 	__le32 *src_ptr;
952 	u32 *dst_ptr;
953 	u64 result;
954 	int i, rc;
955 
956 	data_size = sizeof(struct cpucp_monitor_dump);
957 	mon_dump_cpu_addr = hl_cpu_accessible_dma_pool_alloc(hdev, data_size, &mon_dump_dma_addr);
958 	if (!mon_dump_cpu_addr) {
959 		dev_err(hdev->dev,
960 			"Failed to allocate DMA memory for CPU-CP monitor-dump packet\n");
961 		return -ENOMEM;
962 	}
963 
964 	memset(mon_dump_cpu_addr, 0, data_size);
965 
966 	pkt.ctl = cpu_to_le32(CPUCP_PACKET_MONITOR_DUMP_GET << CPUCP_PKT_CTL_OPCODE_SHIFT);
967 	pkt.addr = cpu_to_le64(mon_dump_dma_addr);
968 	pkt.data_max_size = cpu_to_le32(data_size);
969 
970 	rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
971 							HL_CPUCP_MON_DUMP_TIMEOUT_USEC, &result);
972 	if (rc) {
973 		dev_err(hdev->dev, "Failed to handle CPU-CP monitor-dump packet, error %d\n", rc);
974 		goto out;
975 	}
976 
977 	/* result contains the actual size */
978 	src_ptr = (__le32 *) mon_dump_cpu_addr;
979 	dst_ptr = data;
980 	for (i = 0; i < (data_size / sizeof(u32)); i++) {
981 		*dst_ptr = le32_to_cpu(*src_ptr);
982 		src_ptr++;
983 		dst_ptr++;
984 	}
985 
986 out:
987 	hl_cpu_accessible_dma_pool_free(hdev, data_size, mon_dump_cpu_addr);
988 
989 	return rc;
990 }
991 
992 int hl_fw_cpucp_pci_counters_get(struct hl_device *hdev,
993 		struct hl_info_pci_counters *counters)
994 {
995 	struct cpucp_packet pkt = {};
996 	u64 result;
997 	int rc;
998 
999 	pkt.ctl = cpu_to_le32(CPUCP_PACKET_PCIE_THROUGHPUT_GET <<
1000 			CPUCP_PKT_CTL_OPCODE_SHIFT);
1001 
1002 	/* Fetch PCI rx counter */
1003 	pkt.index = cpu_to_le32(cpucp_pcie_throughput_rx);
1004 	rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
1005 					HL_CPUCP_INFO_TIMEOUT_USEC, &result);
1006 	if (rc) {
1007 		dev_err(hdev->dev,
1008 			"Failed to handle CPU-CP PCI info pkt, error %d\n", rc);
1009 		return rc;
1010 	}
1011 	counters->rx_throughput = result;
1012 
1013 	memset(&pkt, 0, sizeof(pkt));
1014 	pkt.ctl = cpu_to_le32(CPUCP_PACKET_PCIE_THROUGHPUT_GET <<
1015 			CPUCP_PKT_CTL_OPCODE_SHIFT);
1016 
1017 	/* Fetch PCI tx counter */
1018 	pkt.index = cpu_to_le32(cpucp_pcie_throughput_tx);
1019 	rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
1020 					HL_CPUCP_INFO_TIMEOUT_USEC, &result);
1021 	if (rc) {
1022 		dev_err(hdev->dev,
1023 			"Failed to handle CPU-CP PCI info pkt, error %d\n", rc);
1024 		return rc;
1025 	}
1026 	counters->tx_throughput = result;
1027 
1028 	/* Fetch PCI replay counter */
1029 	memset(&pkt, 0, sizeof(pkt));
1030 	pkt.ctl = cpu_to_le32(CPUCP_PACKET_PCIE_REPLAY_CNT_GET <<
1031 			CPUCP_PKT_CTL_OPCODE_SHIFT);
1032 
1033 	rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
1034 			HL_CPUCP_INFO_TIMEOUT_USEC, &result);
1035 	if (rc) {
1036 		dev_err(hdev->dev,
1037 			"Failed to handle CPU-CP PCI info pkt, error %d\n", rc);
1038 		return rc;
1039 	}
1040 	counters->replay_cnt = (u32) result;
1041 
1042 	return rc;
1043 }
1044 
1045 int hl_fw_cpucp_total_energy_get(struct hl_device *hdev, u64 *total_energy)
1046 {
1047 	struct cpucp_packet pkt = {};
1048 	u64 result;
1049 	int rc;
1050 
1051 	pkt.ctl = cpu_to_le32(CPUCP_PACKET_TOTAL_ENERGY_GET <<
1052 				CPUCP_PKT_CTL_OPCODE_SHIFT);
1053 
1054 	rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
1055 					HL_CPUCP_INFO_TIMEOUT_USEC, &result);
1056 	if (rc) {
1057 		dev_err(hdev->dev,
1058 			"Failed to handle CpuCP total energy pkt, error %d\n",
1059 				rc);
1060 		return rc;
1061 	}
1062 
1063 	*total_energy = result;
1064 
1065 	return rc;
1066 }
1067 
1068 int get_used_pll_index(struct hl_device *hdev, u32 input_pll_index,
1069 						enum pll_index *pll_index)
1070 {
1071 	struct asic_fixed_properties *prop = &hdev->asic_prop;
1072 	u8 pll_byte, pll_bit_off;
1073 	bool dynamic_pll;
1074 	int fw_pll_idx;
1075 
1076 	dynamic_pll = !!(prop->fw_app_cpu_boot_dev_sts0 &
1077 						CPU_BOOT_DEV_STS0_DYN_PLL_EN);
1078 
1079 	if (!dynamic_pll) {
1080 		/*
1081 		 * in case we are working with legacy FW (each asic has unique
1082 		 * PLL numbering) use the driver based index as they are
1083 		 * aligned with fw legacy numbering
1084 		 */
1085 		*pll_index = input_pll_index;
1086 		return 0;
1087 	}
1088 
1089 	/* retrieve a FW compatible PLL index based on
1090 	 * ASIC specific user request
1091 	 */
1092 	fw_pll_idx = hdev->asic_funcs->map_pll_idx_to_fw_idx(input_pll_index);
1093 	if (fw_pll_idx < 0) {
1094 		dev_err(hdev->dev, "Invalid PLL index (%u) error %d\n",
1095 			input_pll_index, fw_pll_idx);
1096 		return -EINVAL;
1097 	}
1098 
1099 	/* PLL map is a u8 array */
1100 	pll_byte = prop->cpucp_info.pll_map[fw_pll_idx >> 3];
1101 	pll_bit_off = fw_pll_idx & 0x7;
1102 
1103 	if (!(pll_byte & BIT(pll_bit_off))) {
1104 		dev_err(hdev->dev, "PLL index %d is not supported\n",
1105 			fw_pll_idx);
1106 		return -EINVAL;
1107 	}
1108 
1109 	*pll_index = fw_pll_idx;
1110 
1111 	return 0;
1112 }
1113 
1114 int hl_fw_cpucp_pll_info_get(struct hl_device *hdev, u32 pll_index,
1115 		u16 *pll_freq_arr)
1116 {
1117 	struct cpucp_packet pkt;
1118 	enum pll_index used_pll_idx;
1119 	u64 result;
1120 	int rc;
1121 
1122 	rc = get_used_pll_index(hdev, pll_index, &used_pll_idx);
1123 	if (rc)
1124 		return rc;
1125 
1126 	memset(&pkt, 0, sizeof(pkt));
1127 
1128 	pkt.ctl = cpu_to_le32(CPUCP_PACKET_PLL_INFO_GET <<
1129 				CPUCP_PKT_CTL_OPCODE_SHIFT);
1130 	pkt.pll_type = __cpu_to_le16((u16)used_pll_idx);
1131 
1132 	rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
1133 			HL_CPUCP_INFO_TIMEOUT_USEC, &result);
1134 	if (rc) {
1135 		dev_err(hdev->dev, "Failed to read PLL info, error %d\n", rc);
1136 		return rc;
1137 	}
1138 
1139 	pll_freq_arr[0] = FIELD_GET(CPUCP_PKT_RES_PLL_OUT0_MASK, result);
1140 	pll_freq_arr[1] = FIELD_GET(CPUCP_PKT_RES_PLL_OUT1_MASK, result);
1141 	pll_freq_arr[2] = FIELD_GET(CPUCP_PKT_RES_PLL_OUT2_MASK, result);
1142 	pll_freq_arr[3] = FIELD_GET(CPUCP_PKT_RES_PLL_OUT3_MASK, result);
1143 
1144 	return 0;
1145 }
1146 
1147 int hl_fw_cpucp_power_get(struct hl_device *hdev, u64 *power)
1148 {
1149 	struct cpucp_packet pkt;
1150 	u64 result;
1151 	int rc;
1152 
1153 	memset(&pkt, 0, sizeof(pkt));
1154 
1155 	pkt.ctl = cpu_to_le32(CPUCP_PACKET_POWER_GET <<
1156 				CPUCP_PKT_CTL_OPCODE_SHIFT);
1157 	pkt.type = cpu_to_le16(CPUCP_POWER_INPUT);
1158 
1159 	rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
1160 			HL_CPUCP_INFO_TIMEOUT_USEC, &result);
1161 	if (rc) {
1162 		dev_err(hdev->dev, "Failed to read power, error %d\n", rc);
1163 		return rc;
1164 	}
1165 
1166 	*power = result;
1167 
1168 	return rc;
1169 }
1170 
1171 int hl_fw_dram_replaced_row_get(struct hl_device *hdev,
1172 				struct cpucp_hbm_row_info *info)
1173 {
1174 	struct cpucp_hbm_row_info *cpucp_repl_rows_info_cpu_addr;
1175 	dma_addr_t cpucp_repl_rows_info_dma_addr;
1176 	struct cpucp_packet pkt = {};
1177 	u64 result;
1178 	int rc;
1179 
1180 	cpucp_repl_rows_info_cpu_addr = hl_cpu_accessible_dma_pool_alloc(hdev,
1181 							sizeof(struct cpucp_hbm_row_info),
1182 							&cpucp_repl_rows_info_dma_addr);
1183 	if (!cpucp_repl_rows_info_cpu_addr) {
1184 		dev_err(hdev->dev,
1185 			"Failed to allocate DMA memory for CPU-CP replaced rows info packet\n");
1186 		return -ENOMEM;
1187 	}
1188 
1189 	memset(cpucp_repl_rows_info_cpu_addr, 0, sizeof(struct cpucp_hbm_row_info));
1190 
1191 	pkt.ctl = cpu_to_le32(CPUCP_PACKET_HBM_REPLACED_ROWS_INFO_GET <<
1192 					CPUCP_PKT_CTL_OPCODE_SHIFT);
1193 	pkt.addr = cpu_to_le64(cpucp_repl_rows_info_dma_addr);
1194 	pkt.data_max_size = cpu_to_le32(sizeof(struct cpucp_hbm_row_info));
1195 
1196 	rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
1197 					HL_CPUCP_INFO_TIMEOUT_USEC, &result);
1198 	if (rc) {
1199 		dev_err(hdev->dev,
1200 			"Failed to handle CPU-CP replaced rows info pkt, error %d\n", rc);
1201 		goto out;
1202 	}
1203 
1204 	memcpy(info, cpucp_repl_rows_info_cpu_addr, sizeof(*info));
1205 
1206 out:
1207 	hl_cpu_accessible_dma_pool_free(hdev, sizeof(struct cpucp_hbm_row_info),
1208 						cpucp_repl_rows_info_cpu_addr);
1209 
1210 	return rc;
1211 }
1212 
1213 int hl_fw_dram_pending_row_get(struct hl_device *hdev, u32 *pend_rows_num)
1214 {
1215 	struct cpucp_packet pkt;
1216 	u64 result;
1217 	int rc;
1218 
1219 	memset(&pkt, 0, sizeof(pkt));
1220 
1221 	pkt.ctl = cpu_to_le32(CPUCP_PACKET_HBM_PENDING_ROWS_STATUS << CPUCP_PKT_CTL_OPCODE_SHIFT);
1222 
1223 	rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt), 0, &result);
1224 	if (rc) {
1225 		dev_err(hdev->dev,
1226 				"Failed to handle CPU-CP pending rows info pkt, error %d\n", rc);
1227 		goto out;
1228 	}
1229 
1230 	*pend_rows_num = (u32) result;
1231 out:
1232 	return rc;
1233 }
1234 
1235 int hl_fw_cpucp_engine_core_asid_set(struct hl_device *hdev, u32 asid)
1236 {
1237 	struct cpucp_packet pkt;
1238 	int rc;
1239 
1240 	memset(&pkt, 0, sizeof(pkt));
1241 
1242 	pkt.ctl = cpu_to_le32(CPUCP_PACKET_ENGINE_CORE_ASID_SET << CPUCP_PKT_CTL_OPCODE_SHIFT);
1243 	pkt.value = cpu_to_le64(asid);
1244 
1245 	rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
1246 						HL_CPUCP_INFO_TIMEOUT_USEC, NULL);
1247 	if (rc)
1248 		dev_err(hdev->dev,
1249 			"Failed on ASID configuration request for engine core, error %d\n",
1250 			rc);
1251 
1252 	return rc;
1253 }
1254 
1255 void hl_fw_ask_hard_reset_without_linux(struct hl_device *hdev)
1256 {
1257 	struct static_fw_load_mgr *static_loader =
1258 			&hdev->fw_loader.static_loader;
1259 	int rc;
1260 
1261 	if (hdev->asic_prop.dynamic_fw_load) {
1262 		rc = hl_fw_dynamic_send_protocol_cmd(hdev, &hdev->fw_loader,
1263 				COMMS_RST_DEV, 0, false,
1264 				hdev->fw_loader.cpu_timeout);
1265 		if (rc)
1266 			dev_warn(hdev->dev, "Failed sending COMMS_RST_DEV\n");
1267 	} else {
1268 		WREG32(static_loader->kmd_msg_to_cpu_reg, KMD_MSG_RST_DEV);
1269 	}
1270 }
1271 
1272 void hl_fw_ask_halt_machine_without_linux(struct hl_device *hdev)
1273 {
1274 	struct static_fw_load_mgr *static_loader =
1275 			&hdev->fw_loader.static_loader;
1276 	int rc;
1277 
1278 	if (hdev->device_cpu_is_halted)
1279 		return;
1280 
1281 	/* Stop device CPU to make sure nothing bad happens */
1282 	if (hdev->asic_prop.dynamic_fw_load) {
1283 		rc = hl_fw_dynamic_send_protocol_cmd(hdev, &hdev->fw_loader,
1284 				COMMS_GOTO_WFE, 0, true,
1285 				hdev->fw_loader.cpu_timeout);
1286 		if (rc)
1287 			dev_warn(hdev->dev, "Failed sending COMMS_GOTO_WFE\n");
1288 	} else {
1289 		WREG32(static_loader->kmd_msg_to_cpu_reg, KMD_MSG_GOTO_WFE);
1290 		msleep(static_loader->cpu_reset_wait_msec);
1291 
1292 		/* Must clear this register in order to prevent preboot
1293 		 * from reading WFE after reboot
1294 		 */
1295 		WREG32(static_loader->kmd_msg_to_cpu_reg, KMD_MSG_NA);
1296 	}
1297 
1298 	hdev->device_cpu_is_halted = true;
1299 }
1300 
1301 static void detect_cpu_boot_status(struct hl_device *hdev, u32 status)
1302 {
1303 	/* Some of the status codes below are deprecated in newer f/w
1304 	 * versions but we keep them here for backward compatibility
1305 	 */
1306 	switch (status) {
1307 	case CPU_BOOT_STATUS_NA:
1308 		dev_err(hdev->dev,
1309 			"Device boot progress - BTL/ROM did NOT run\n");
1310 		break;
1311 	case CPU_BOOT_STATUS_IN_WFE:
1312 		dev_err(hdev->dev,
1313 			"Device boot progress - Stuck inside WFE loop\n");
1314 		break;
1315 	case CPU_BOOT_STATUS_IN_BTL:
1316 		dev_err(hdev->dev,
1317 			"Device boot progress - Stuck in BTL\n");
1318 		break;
1319 	case CPU_BOOT_STATUS_IN_PREBOOT:
1320 		dev_err(hdev->dev,
1321 			"Device boot progress - Stuck in Preboot\n");
1322 		break;
1323 	case CPU_BOOT_STATUS_IN_SPL:
1324 		dev_err(hdev->dev,
1325 			"Device boot progress - Stuck in SPL\n");
1326 		break;
1327 	case CPU_BOOT_STATUS_IN_UBOOT:
1328 		dev_err(hdev->dev,
1329 			"Device boot progress - Stuck in u-boot\n");
1330 		break;
1331 	case CPU_BOOT_STATUS_DRAM_INIT_FAIL:
1332 		dev_err(hdev->dev,
1333 			"Device boot progress - DRAM initialization failed\n");
1334 		break;
1335 	case CPU_BOOT_STATUS_UBOOT_NOT_READY:
1336 		dev_err(hdev->dev,
1337 			"Device boot progress - Cannot boot\n");
1338 		break;
1339 	case CPU_BOOT_STATUS_TS_INIT_FAIL:
1340 		dev_err(hdev->dev,
1341 			"Device boot progress - Thermal Sensor initialization failed\n");
1342 		break;
1343 	case CPU_BOOT_STATUS_SECURITY_READY:
1344 		dev_err(hdev->dev,
1345 			"Device boot progress - Stuck in preboot after security initialization\n");
1346 		break;
1347 	default:
1348 		dev_err(hdev->dev,
1349 			"Device boot progress - Invalid status code %d\n",
1350 			status);
1351 		break;
1352 	}
1353 }
1354 
1355 int hl_fw_wait_preboot_ready(struct hl_device *hdev)
1356 {
1357 	struct pre_fw_load_props *pre_fw_load = &hdev->fw_loader.pre_fw_load;
1358 	u32 status;
1359 	int rc;
1360 
1361 	/* Need to check two possible scenarios:
1362 	 *
1363 	 * CPU_BOOT_STATUS_WAITING_FOR_BOOT_FIT - for newer firmwares where
1364 	 * the preboot is waiting for the boot fit
1365 	 *
1366 	 * All other status values - for older firmwares where the uboot was
1367 	 * loaded from the FLASH
1368 	 */
1369 	rc = hl_poll_timeout(
1370 		hdev,
1371 		pre_fw_load->cpu_boot_status_reg,
1372 		status,
1373 		(status == CPU_BOOT_STATUS_NIC_FW_RDY) ||
1374 		(status == CPU_BOOT_STATUS_READY_TO_BOOT) ||
1375 		(status == CPU_BOOT_STATUS_WAITING_FOR_BOOT_FIT),
1376 		hdev->fw_poll_interval_usec,
1377 		pre_fw_load->wait_for_preboot_timeout);
1378 
1379 	if (rc) {
1380 		dev_err(hdev->dev, "CPU boot ready status timeout\n");
1381 		detect_cpu_boot_status(hdev, status);
1382 
1383 		/* If we read all FF, then something is totally wrong, no point
1384 		 * of reading specific errors
1385 		 */
1386 		if (status != -1)
1387 			fw_read_errors(hdev, pre_fw_load->boot_err0_reg,
1388 						pre_fw_load->boot_err1_reg,
1389 						pre_fw_load->sts_boot_dev_sts0_reg,
1390 						pre_fw_load->sts_boot_dev_sts1_reg);
1391 		return -EIO;
1392 	}
1393 
1394 	hdev->fw_loader.fw_comp_loaded |= FW_TYPE_PREBOOT_CPU;
1395 
1396 	return 0;
1397 }
1398 
1399 static int hl_fw_read_preboot_caps(struct hl_device *hdev)
1400 {
1401 	struct pre_fw_load_props *pre_fw_load;
1402 	struct asic_fixed_properties *prop;
1403 	u32 reg_val;
1404 	int rc;
1405 
1406 	prop = &hdev->asic_prop;
1407 	pre_fw_load = &hdev->fw_loader.pre_fw_load;
1408 
1409 	rc = hl_fw_wait_preboot_ready(hdev);
1410 	if (rc)
1411 		return rc;
1412 
1413 	/*
1414 	 * the registers DEV_STS* contain FW capabilities/features.
1415 	 * We can rely on this registers only if bit CPU_BOOT_DEV_STS*_ENABLED
1416 	 * is set.
1417 	 * In the first read of this register we store the value of this
1418 	 * register ONLY if the register is enabled (which will be propagated
1419 	 * to next stages) and also mark the register as valid.
1420 	 * In case it is not enabled the stored value will be left 0- all
1421 	 * caps/features are off
1422 	 */
1423 	reg_val = RREG32(pre_fw_load->sts_boot_dev_sts0_reg);
1424 	if (reg_val & CPU_BOOT_DEV_STS0_ENABLED) {
1425 		prop->fw_cpu_boot_dev_sts0_valid = true;
1426 		prop->fw_preboot_cpu_boot_dev_sts0 = reg_val;
1427 	}
1428 
1429 	reg_val = RREG32(pre_fw_load->sts_boot_dev_sts1_reg);
1430 	if (reg_val & CPU_BOOT_DEV_STS1_ENABLED) {
1431 		prop->fw_cpu_boot_dev_sts1_valid = true;
1432 		prop->fw_preboot_cpu_boot_dev_sts1 = reg_val;
1433 	}
1434 
1435 	prop->dynamic_fw_load = !!(prop->fw_preboot_cpu_boot_dev_sts0 &
1436 						CPU_BOOT_DEV_STS0_FW_LD_COM_EN);
1437 
1438 	/* initialize FW loader once we know what load protocol is used */
1439 	hdev->asic_funcs->init_firmware_loader(hdev);
1440 
1441 	dev_dbg(hdev->dev, "Attempting %s FW load\n",
1442 			prop->dynamic_fw_load ? "dynamic" : "legacy");
1443 	return 0;
1444 }
1445 
1446 static int hl_fw_static_read_device_fw_version(struct hl_device *hdev,
1447 					enum hl_fw_component fwc)
1448 {
1449 	struct asic_fixed_properties *prop = &hdev->asic_prop;
1450 	struct fw_load_mgr *fw_loader = &hdev->fw_loader;
1451 	struct static_fw_load_mgr *static_loader;
1452 	char *dest, *boot_ver, *preboot_ver;
1453 	u32 ver_off, limit;
1454 	const char *name;
1455 	char btl_ver[32];
1456 
1457 	static_loader = &hdev->fw_loader.static_loader;
1458 
1459 	switch (fwc) {
1460 	case FW_COMP_BOOT_FIT:
1461 		ver_off = RREG32(static_loader->boot_fit_version_offset_reg);
1462 		dest = prop->uboot_ver;
1463 		name = "Boot-fit";
1464 		limit = static_loader->boot_fit_version_max_off;
1465 		break;
1466 	case FW_COMP_PREBOOT:
1467 		ver_off = RREG32(static_loader->preboot_version_offset_reg);
1468 		dest = prop->preboot_ver;
1469 		name = "Preboot";
1470 		limit = static_loader->preboot_version_max_off;
1471 		break;
1472 	default:
1473 		dev_warn(hdev->dev, "Undefined FW component: %d\n", fwc);
1474 		return -EIO;
1475 	}
1476 
1477 	ver_off &= static_loader->sram_offset_mask;
1478 
1479 	if (ver_off < limit) {
1480 		memcpy_fromio(dest,
1481 			hdev->pcie_bar[fw_loader->sram_bar_id] + ver_off,
1482 			VERSION_MAX_LEN);
1483 	} else {
1484 		dev_err(hdev->dev, "%s version offset (0x%x) is above SRAM\n",
1485 								name, ver_off);
1486 		strscpy(dest, "unavailable", VERSION_MAX_LEN);
1487 		return -EIO;
1488 	}
1489 
1490 	if (fwc == FW_COMP_BOOT_FIT) {
1491 		boot_ver = extract_fw_ver_from_str(prop->uboot_ver);
1492 		if (boot_ver) {
1493 			dev_info(hdev->dev, "boot-fit version %s\n", boot_ver);
1494 			kfree(boot_ver);
1495 		}
1496 	} else if (fwc == FW_COMP_PREBOOT) {
1497 		preboot_ver = strnstr(prop->preboot_ver, "Preboot",
1498 						VERSION_MAX_LEN);
1499 		if (preboot_ver && preboot_ver != prop->preboot_ver) {
1500 			strscpy(btl_ver, prop->preboot_ver,
1501 				min((int) (preboot_ver - prop->preboot_ver),
1502 									31));
1503 			dev_info(hdev->dev, "%s\n", btl_ver);
1504 		}
1505 
1506 		preboot_ver = extract_fw_ver_from_str(prop->preboot_ver);
1507 		if (preboot_ver) {
1508 			dev_info(hdev->dev, "preboot version %s\n",
1509 								preboot_ver);
1510 			kfree(preboot_ver);
1511 		}
1512 	}
1513 
1514 	return 0;
1515 }
1516 
1517 /**
1518  * hl_fw_preboot_update_state - update internal data structures during
1519  *                              handshake with preboot
1520  *
1521  *
1522  * @hdev: pointer to the habanalabs device structure
1523  *
1524  * @return 0 on success, otherwise non-zero error code
1525  */
1526 static void hl_fw_preboot_update_state(struct hl_device *hdev)
1527 {
1528 	struct asic_fixed_properties *prop = &hdev->asic_prop;
1529 	u32 cpu_boot_dev_sts0, cpu_boot_dev_sts1;
1530 
1531 	cpu_boot_dev_sts0 = prop->fw_preboot_cpu_boot_dev_sts0;
1532 	cpu_boot_dev_sts1 = prop->fw_preboot_cpu_boot_dev_sts1;
1533 
1534 	/* We read boot_dev_sts registers multiple times during boot:
1535 	 * 1. preboot - a. Check whether the security status bits are valid
1536 	 *              b. Check whether fw security is enabled
1537 	 *              c. Check whether hard reset is done by preboot
1538 	 * 2. boot cpu - a. Fetch boot cpu security status
1539 	 *               b. Check whether hard reset is done by boot cpu
1540 	 * 3. FW application - a. Fetch fw application security status
1541 	 *                     b. Check whether hard reset is done by fw app
1542 	 */
1543 	prop->hard_reset_done_by_fw = !!(cpu_boot_dev_sts0 & CPU_BOOT_DEV_STS0_FW_HARD_RST_EN);
1544 
1545 	prop->fw_security_enabled = !!(cpu_boot_dev_sts0 & CPU_BOOT_DEV_STS0_SECURITY_EN);
1546 
1547 	dev_dbg(hdev->dev, "Firmware preboot boot device status0 %#x\n",
1548 							cpu_boot_dev_sts0);
1549 
1550 	dev_dbg(hdev->dev, "Firmware preboot boot device status1 %#x\n",
1551 							cpu_boot_dev_sts1);
1552 
1553 	dev_dbg(hdev->dev, "Firmware preboot hard-reset is %s\n",
1554 			prop->hard_reset_done_by_fw ? "enabled" : "disabled");
1555 
1556 	dev_dbg(hdev->dev, "firmware-level security is %s\n",
1557 			prop->fw_security_enabled ? "enabled" : "disabled");
1558 
1559 	dev_dbg(hdev->dev, "GIC controller is %s\n",
1560 			prop->gic_interrupts_enable ? "enabled" : "disabled");
1561 }
1562 
1563 static int hl_fw_static_read_preboot_status(struct hl_device *hdev)
1564 {
1565 	int rc;
1566 
1567 	rc = hl_fw_static_read_device_fw_version(hdev, FW_COMP_PREBOOT);
1568 	if (rc)
1569 		return rc;
1570 
1571 	return 0;
1572 }
1573 
1574 int hl_fw_read_preboot_status(struct hl_device *hdev)
1575 {
1576 	int rc;
1577 
1578 	if (!(hdev->fw_components & FW_TYPE_PREBOOT_CPU))
1579 		return 0;
1580 
1581 	/* get FW pre-load parameters  */
1582 	hdev->asic_funcs->init_firmware_preload_params(hdev);
1583 
1584 	/*
1585 	 * In order to determine boot method (static VS dynamic) we need to
1586 	 * read the boot caps register
1587 	 */
1588 	rc = hl_fw_read_preboot_caps(hdev);
1589 	if (rc)
1590 		return rc;
1591 
1592 	hl_fw_preboot_update_state(hdev);
1593 
1594 	/* no need to read preboot status in dynamic load */
1595 	if (hdev->asic_prop.dynamic_fw_load)
1596 		return 0;
1597 
1598 	return hl_fw_static_read_preboot_status(hdev);
1599 }
1600 
1601 /* associate string with COMM status */
1602 static char *hl_dynamic_fw_status_str[COMMS_STS_INVLD_LAST] = {
1603 	[COMMS_STS_NOOP] = "NOOP",
1604 	[COMMS_STS_ACK] = "ACK",
1605 	[COMMS_STS_OK] = "OK",
1606 	[COMMS_STS_ERR] = "ERR",
1607 	[COMMS_STS_VALID_ERR] = "VALID_ERR",
1608 	[COMMS_STS_TIMEOUT_ERR] = "TIMEOUT_ERR",
1609 };
1610 
1611 /**
1612  * hl_fw_dynamic_report_error_status - report error status
1613  *
1614  * @hdev: pointer to the habanalabs device structure
1615  * @status: value of FW status register
1616  * @expected_status: the expected status
1617  */
1618 static void hl_fw_dynamic_report_error_status(struct hl_device *hdev,
1619 						u32 status,
1620 						enum comms_sts expected_status)
1621 {
1622 	enum comms_sts comm_status =
1623 				FIELD_GET(COMMS_STATUS_STATUS_MASK, status);
1624 
1625 	if (comm_status < COMMS_STS_INVLD_LAST)
1626 		dev_err(hdev->dev, "Device status %s, expected status: %s\n",
1627 				hl_dynamic_fw_status_str[comm_status],
1628 				hl_dynamic_fw_status_str[expected_status]);
1629 	else
1630 		dev_err(hdev->dev, "Device status unknown %d, expected status: %s\n",
1631 				comm_status,
1632 				hl_dynamic_fw_status_str[expected_status]);
1633 }
1634 
1635 /**
1636  * hl_fw_dynamic_send_cmd - send LKD to FW cmd
1637  *
1638  * @hdev: pointer to the habanalabs device structure
1639  * @fw_loader: managing structure for loading device's FW
1640  * @cmd: LKD to FW cmd code
1641  * @size: size of next FW component to be loaded (0 if not necessary)
1642  *
1643  * LDK to FW exact command layout is defined at struct comms_command.
1644  * note: the size argument is used only when the next FW component should be
1645  *       loaded, otherwise it shall be 0. the size is used by the FW in later
1646  *       protocol stages and when sending only indicating the amount of memory
1647  *       to be allocated by the FW to receive the next boot component.
1648  */
1649 static void hl_fw_dynamic_send_cmd(struct hl_device *hdev,
1650 				struct fw_load_mgr *fw_loader,
1651 				enum comms_cmd cmd, unsigned int size)
1652 {
1653 	struct cpu_dyn_regs *dyn_regs;
1654 	u32 val;
1655 
1656 	dyn_regs = &fw_loader->dynamic_loader.comm_desc.cpu_dyn_regs;
1657 
1658 	val = FIELD_PREP(COMMS_COMMAND_CMD_MASK, cmd);
1659 	val |= FIELD_PREP(COMMS_COMMAND_SIZE_MASK, size);
1660 
1661 	trace_habanalabs_comms_send_cmd(hdev->dev, comms_cmd_str_arr[cmd]);
1662 	WREG32(le32_to_cpu(dyn_regs->kmd_msg_to_cpu), val);
1663 }
1664 
1665 /**
1666  * hl_fw_dynamic_extract_fw_response - update the FW response
1667  *
1668  * @hdev: pointer to the habanalabs device structure
1669  * @fw_loader: managing structure for loading device's FW
1670  * @response: FW response
1671  * @status: the status read from CPU status register
1672  *
1673  * @return 0 on success, otherwise non-zero error code
1674  */
1675 static int hl_fw_dynamic_extract_fw_response(struct hl_device *hdev,
1676 						struct fw_load_mgr *fw_loader,
1677 						struct fw_response *response,
1678 						u32 status)
1679 {
1680 	response->status = FIELD_GET(COMMS_STATUS_STATUS_MASK, status);
1681 	response->ram_offset = FIELD_GET(COMMS_STATUS_OFFSET_MASK, status) <<
1682 						COMMS_STATUS_OFFSET_ALIGN_SHIFT;
1683 	response->ram_type = FIELD_GET(COMMS_STATUS_RAM_TYPE_MASK, status);
1684 
1685 	if ((response->ram_type != COMMS_SRAM) &&
1686 					(response->ram_type != COMMS_DRAM)) {
1687 		dev_err(hdev->dev, "FW status: invalid RAM type %u\n",
1688 							response->ram_type);
1689 		return -EIO;
1690 	}
1691 
1692 	return 0;
1693 }
1694 
1695 /**
1696  * hl_fw_dynamic_wait_for_status - wait for status in dynamic FW load
1697  *
1698  * @hdev: pointer to the habanalabs device structure
1699  * @fw_loader: managing structure for loading device's FW
1700  * @expected_status: expected status to wait for
1701  * @timeout: timeout for status wait
1702  *
1703  * @return 0 on success, otherwise non-zero error code
1704  *
1705  * waiting for status from FW include polling the FW status register until
1706  * expected status is received or timeout occurs (whatever occurs first).
1707  */
1708 static int hl_fw_dynamic_wait_for_status(struct hl_device *hdev,
1709 						struct fw_load_mgr *fw_loader,
1710 						enum comms_sts expected_status,
1711 						u32 timeout)
1712 {
1713 	struct cpu_dyn_regs *dyn_regs;
1714 	u32 status;
1715 	int rc;
1716 
1717 	dyn_regs = &fw_loader->dynamic_loader.comm_desc.cpu_dyn_regs;
1718 
1719 	trace_habanalabs_comms_wait_status(hdev->dev, comms_sts_str_arr[expected_status]);
1720 
1721 	/* Wait for expected status */
1722 	rc = hl_poll_timeout(
1723 		hdev,
1724 		le32_to_cpu(dyn_regs->cpu_cmd_status_to_host),
1725 		status,
1726 		FIELD_GET(COMMS_STATUS_STATUS_MASK, status) == expected_status,
1727 		hdev->fw_comms_poll_interval_usec,
1728 		timeout);
1729 
1730 	if (rc) {
1731 		hl_fw_dynamic_report_error_status(hdev, status,
1732 							expected_status);
1733 		return -EIO;
1734 	}
1735 
1736 	trace_habanalabs_comms_wait_status_done(hdev->dev, comms_sts_str_arr[expected_status]);
1737 
1738 	/*
1739 	 * skip storing FW response for NOOP to preserve the actual desired
1740 	 * FW status
1741 	 */
1742 	if (expected_status == COMMS_STS_NOOP)
1743 		return 0;
1744 
1745 	rc = hl_fw_dynamic_extract_fw_response(hdev, fw_loader,
1746 					&fw_loader->dynamic_loader.response,
1747 					status);
1748 	return rc;
1749 }
1750 
1751 /**
1752  * hl_fw_dynamic_send_clear_cmd - send clear command to FW
1753  *
1754  * @hdev: pointer to the habanalabs device structure
1755  * @fw_loader: managing structure for loading device's FW
1756  *
1757  * @return 0 on success, otherwise non-zero error code
1758  *
1759  * after command cycle between LKD to FW CPU (i.e. LKD got an expected status
1760  * from FW) we need to clear the CPU status register in order to avoid garbage
1761  * between command cycles.
1762  * This is done by sending clear command and polling the CPU to LKD status
1763  * register to hold the status NOOP
1764  */
1765 static int hl_fw_dynamic_send_clear_cmd(struct hl_device *hdev,
1766 						struct fw_load_mgr *fw_loader)
1767 {
1768 	hl_fw_dynamic_send_cmd(hdev, fw_loader, COMMS_CLR_STS, 0);
1769 
1770 	return hl_fw_dynamic_wait_for_status(hdev, fw_loader, COMMS_STS_NOOP,
1771 							fw_loader->cpu_timeout);
1772 }
1773 
1774 /**
1775  * hl_fw_dynamic_send_protocol_cmd - send LKD to FW cmd and wait for ACK
1776  *
1777  * @hdev: pointer to the habanalabs device structure
1778  * @fw_loader: managing structure for loading device's FW
1779  * @cmd: LKD to FW cmd code
1780  * @size: size of next FW component to be loaded (0 if not necessary)
1781  * @wait_ok: if true also wait for OK response from FW
1782  * @timeout: timeout for status wait
1783  *
1784  * @return 0 on success, otherwise non-zero error code
1785  *
1786  * brief:
1787  * when sending protocol command we have the following steps:
1788  * - send clear (clear command and verify clear status register)
1789  * - send the actual protocol command
1790  * - wait for ACK on the protocol command
1791  * - send clear
1792  * - send NOOP
1793  * if, in addition, the specific protocol command should wait for OK then:
1794  * - wait for OK
1795  * - send clear
1796  * - send NOOP
1797  *
1798  * NOTES:
1799  * send clear: this is necessary in order to clear the status register to avoid
1800  *             leftovers between command
1801  * NOOP command: necessary to avoid loop on the clear command by the FW
1802  */
1803 int hl_fw_dynamic_send_protocol_cmd(struct hl_device *hdev,
1804 				struct fw_load_mgr *fw_loader,
1805 				enum comms_cmd cmd, unsigned int size,
1806 				bool wait_ok, u32 timeout)
1807 {
1808 	int rc;
1809 
1810 	trace_habanalabs_comms_protocol_cmd(hdev->dev, comms_cmd_str_arr[cmd]);
1811 
1812 	/* first send clear command to clean former commands */
1813 	rc = hl_fw_dynamic_send_clear_cmd(hdev, fw_loader);
1814 	if (rc)
1815 		return rc;
1816 
1817 	/* send the actual command */
1818 	hl_fw_dynamic_send_cmd(hdev, fw_loader, cmd, size);
1819 
1820 	/* wait for ACK for the command */
1821 	rc = hl_fw_dynamic_wait_for_status(hdev, fw_loader, COMMS_STS_ACK,
1822 								timeout);
1823 	if (rc)
1824 		return rc;
1825 
1826 	/* clear command to prepare for NOOP command */
1827 	rc = hl_fw_dynamic_send_clear_cmd(hdev, fw_loader);
1828 	if (rc)
1829 		return rc;
1830 
1831 	/* send the actual NOOP command */
1832 	hl_fw_dynamic_send_cmd(hdev, fw_loader, COMMS_NOOP, 0);
1833 
1834 	if (!wait_ok)
1835 		return 0;
1836 
1837 	rc = hl_fw_dynamic_wait_for_status(hdev, fw_loader, COMMS_STS_OK,
1838 								timeout);
1839 	if (rc)
1840 		return rc;
1841 
1842 	/* clear command to prepare for NOOP command */
1843 	rc = hl_fw_dynamic_send_clear_cmd(hdev, fw_loader);
1844 	if (rc)
1845 		return rc;
1846 
1847 	/* send the actual NOOP command */
1848 	hl_fw_dynamic_send_cmd(hdev, fw_loader, COMMS_NOOP, 0);
1849 
1850 	return 0;
1851 }
1852 
1853 /**
1854  * hl_fw_compat_crc32 - CRC compatible with FW
1855  *
1856  * @data: pointer to the data
1857  * @size: size of the data
1858  *
1859  * @return the CRC32 result
1860  *
1861  * NOTE: kernel's CRC32 differs from standard CRC32 calculation.
1862  *       in order to be aligned we need to flip the bits of both the input
1863  *       initial CRC and kernel's CRC32 result.
1864  *       in addition both sides use initial CRC of 0,
1865  */
1866 static u32 hl_fw_compat_crc32(u8 *data, size_t size)
1867 {
1868 	return ~crc32_le(~((u32)0), data, size);
1869 }
1870 
1871 /**
1872  * hl_fw_dynamic_validate_memory_bound - validate memory bounds for memory
1873  *                                        transfer (image or descriptor) between
1874  *                                        host and FW
1875  *
1876  * @hdev: pointer to the habanalabs device structure
1877  * @addr: device address of memory transfer
1878  * @size: memory transfer size
1879  * @region: PCI memory region
1880  *
1881  * @return 0 on success, otherwise non-zero error code
1882  */
1883 static int hl_fw_dynamic_validate_memory_bound(struct hl_device *hdev,
1884 						u64 addr, size_t size,
1885 						struct pci_mem_region *region)
1886 {
1887 	u64 end_addr;
1888 
1889 	/* now make sure that the memory transfer is within region's bounds */
1890 	end_addr = addr + size;
1891 	if (end_addr >= region->region_base + region->region_size) {
1892 		dev_err(hdev->dev,
1893 			"dynamic FW load: memory transfer end address out of memory region bounds. addr: %llx\n",
1894 							end_addr);
1895 		return -EIO;
1896 	}
1897 
1898 	/*
1899 	 * now make sure memory transfer is within predefined BAR bounds.
1900 	 * this is to make sure we do not need to set the bar (e.g. for DRAM
1901 	 * memory transfers)
1902 	 */
1903 	if (end_addr >= region->region_base - region->offset_in_bar +
1904 							region->bar_size) {
1905 		dev_err(hdev->dev,
1906 			"FW image beyond PCI BAR bounds\n");
1907 		return -EIO;
1908 	}
1909 
1910 	return 0;
1911 }
1912 
1913 /**
1914  * hl_fw_dynamic_validate_descriptor - validate FW descriptor
1915  *
1916  * @hdev: pointer to the habanalabs device structure
1917  * @fw_loader: managing structure for loading device's FW
1918  * @fw_desc: the descriptor from FW
1919  *
1920  * @return 0 on success, otherwise non-zero error code
1921  */
1922 static int hl_fw_dynamic_validate_descriptor(struct hl_device *hdev,
1923 					struct fw_load_mgr *fw_loader,
1924 					struct lkd_fw_comms_desc *fw_desc)
1925 {
1926 	struct pci_mem_region *region;
1927 	enum pci_region region_id;
1928 	size_t data_size;
1929 	u32 data_crc32;
1930 	u8 *data_ptr;
1931 	u64 addr;
1932 	int rc;
1933 
1934 	if (le32_to_cpu(fw_desc->header.magic) != HL_COMMS_DESC_MAGIC)
1935 		dev_dbg(hdev->dev, "Invalid magic for dynamic FW descriptor (%x)\n",
1936 				fw_desc->header.magic);
1937 
1938 	if (fw_desc->header.version != HL_COMMS_DESC_VER)
1939 		dev_dbg(hdev->dev, "Invalid version for dynamic FW descriptor (%x)\n",
1940 				fw_desc->header.version);
1941 
1942 	/*
1943 	 * Calc CRC32 of data without header. use the size of the descriptor
1944 	 * reported by firmware, without calculating it ourself, to allow adding
1945 	 * more fields to the lkd_fw_comms_desc structure.
1946 	 * note that no alignment/stride address issues here as all structures
1947 	 * are 64 bit padded.
1948 	 */
1949 	data_ptr = (u8 *)fw_desc + sizeof(struct comms_desc_header);
1950 	data_size = le16_to_cpu(fw_desc->header.size);
1951 
1952 	data_crc32 = hl_fw_compat_crc32(data_ptr, data_size);
1953 	if (data_crc32 != le32_to_cpu(fw_desc->header.crc32)) {
1954 		dev_err(hdev->dev, "CRC32 mismatch for dynamic FW descriptor (%x:%x)\n",
1955 			data_crc32, fw_desc->header.crc32);
1956 		return -EIO;
1957 	}
1958 
1959 	/* find memory region to which to copy the image */
1960 	addr = le64_to_cpu(fw_desc->img_addr);
1961 	region_id = hl_get_pci_memory_region(hdev, addr);
1962 	if ((region_id != PCI_REGION_SRAM) && ((region_id != PCI_REGION_DRAM))) {
1963 		dev_err(hdev->dev, "Invalid region to copy FW image address=%llx\n", addr);
1964 		return -EIO;
1965 	}
1966 
1967 	region = &hdev->pci_mem_region[region_id];
1968 
1969 	/* store the region for the copy stage */
1970 	fw_loader->dynamic_loader.image_region = region;
1971 
1972 	/*
1973 	 * here we know that the start address is valid, now make sure that the
1974 	 * image is within region's bounds
1975 	 */
1976 	rc = hl_fw_dynamic_validate_memory_bound(hdev, addr,
1977 					fw_loader->dynamic_loader.fw_image_size,
1978 					region);
1979 	if (rc) {
1980 		dev_err(hdev->dev, "invalid mem transfer request for FW image\n");
1981 		return rc;
1982 	}
1983 
1984 	/* here we can mark the descriptor as valid as the content has been validated */
1985 	fw_loader->dynamic_loader.fw_desc_valid = true;
1986 
1987 	return 0;
1988 }
1989 
1990 static int hl_fw_dynamic_validate_response(struct hl_device *hdev,
1991 						struct fw_response *response,
1992 						struct pci_mem_region *region)
1993 {
1994 	u64 device_addr;
1995 	int rc;
1996 
1997 	device_addr = region->region_base + response->ram_offset;
1998 
1999 	/*
2000 	 * validate that the descriptor is within region's bounds
2001 	 * Note that as the start address was supplied according to the RAM
2002 	 * type- testing only the end address is enough
2003 	 */
2004 	rc = hl_fw_dynamic_validate_memory_bound(hdev, device_addr,
2005 					sizeof(struct lkd_fw_comms_desc),
2006 					region);
2007 	return rc;
2008 }
2009 
2010 /*
2011  * hl_fw_dynamic_read_descriptor_msg - read and show the ascii msg that sent by fw
2012  *
2013  * @hdev: pointer to the habanalabs device structure
2014  * @fw_desc: the descriptor from FW
2015  */
2016 static void hl_fw_dynamic_read_descriptor_msg(struct hl_device *hdev,
2017 					struct lkd_fw_comms_desc *fw_desc)
2018 {
2019 	int i;
2020 	char *msg;
2021 
2022 	for (i = 0 ; i < LKD_FW_ASCII_MSG_MAX ; i++) {
2023 		if (!fw_desc->ascii_msg[i].valid)
2024 			return;
2025 
2026 		/* force NULL termination */
2027 		msg = fw_desc->ascii_msg[i].msg;
2028 		msg[LKD_FW_ASCII_MSG_MAX_LEN - 1] = '\0';
2029 
2030 		switch (fw_desc->ascii_msg[i].msg_lvl) {
2031 		case LKD_FW_ASCII_MSG_ERR:
2032 			dev_err(hdev->dev, "fw: %s", fw_desc->ascii_msg[i].msg);
2033 			break;
2034 		case LKD_FW_ASCII_MSG_WRN:
2035 			dev_warn(hdev->dev, "fw: %s", fw_desc->ascii_msg[i].msg);
2036 			break;
2037 		case LKD_FW_ASCII_MSG_INF:
2038 			dev_info(hdev->dev, "fw: %s", fw_desc->ascii_msg[i].msg);
2039 			break;
2040 		default:
2041 			dev_dbg(hdev->dev, "fw: %s", fw_desc->ascii_msg[i].msg);
2042 			break;
2043 		}
2044 	}
2045 }
2046 
2047 /**
2048  * hl_fw_dynamic_read_and_validate_descriptor - read and validate FW descriptor
2049  *
2050  * @hdev: pointer to the habanalabs device structure
2051  * @fw_loader: managing structure for loading device's FW
2052  *
2053  * @return 0 on success, otherwise non-zero error code
2054  */
2055 static int hl_fw_dynamic_read_and_validate_descriptor(struct hl_device *hdev,
2056 						struct fw_load_mgr *fw_loader)
2057 {
2058 	struct lkd_fw_comms_desc *fw_desc;
2059 	struct pci_mem_region *region;
2060 	struct fw_response *response;
2061 	void *temp_fw_desc;
2062 	void __iomem *src;
2063 	u16 fw_data_size;
2064 	enum pci_region region_id;
2065 	int rc;
2066 
2067 	fw_desc = &fw_loader->dynamic_loader.comm_desc;
2068 	response = &fw_loader->dynamic_loader.response;
2069 
2070 	region_id = (response->ram_type == COMMS_SRAM) ?
2071 					PCI_REGION_SRAM : PCI_REGION_DRAM;
2072 
2073 	region = &hdev->pci_mem_region[region_id];
2074 
2075 	rc = hl_fw_dynamic_validate_response(hdev, response, region);
2076 	if (rc) {
2077 		dev_err(hdev->dev,
2078 			"invalid mem transfer request for FW descriptor\n");
2079 		return rc;
2080 	}
2081 
2082 	/*
2083 	 * extract address to copy the descriptor from
2084 	 * in addition, as the descriptor value is going to be over-ridden by new data- we mark it
2085 	 * as invalid.
2086 	 * it will be marked again as valid once validated
2087 	 */
2088 	fw_loader->dynamic_loader.fw_desc_valid = false;
2089 	src = hdev->pcie_bar[region->bar_id] + region->offset_in_bar +
2090 							response->ram_offset;
2091 
2092 	/*
2093 	 * We do the copy of the fw descriptor in 2 phases:
2094 	 * 1. copy the header + data info according to our lkd_fw_comms_desc definition.
2095 	 *    then we're able to read the actual data size provided by fw.
2096 	 *    this is needed for cases where data in descriptor was changed(add/remove)
2097 	 *    in embedded specs header file before updating lkd copy of the header file
2098 	 * 2. copy descriptor to temporary buffer with aligned size and send it to validation
2099 	 */
2100 	memcpy_fromio(fw_desc, src, sizeof(struct lkd_fw_comms_desc));
2101 	fw_data_size = le16_to_cpu(fw_desc->header.size);
2102 
2103 	temp_fw_desc = vzalloc(sizeof(struct comms_desc_header) + fw_data_size);
2104 	if (!temp_fw_desc)
2105 		return -ENOMEM;
2106 
2107 	memcpy_fromio(temp_fw_desc, src, sizeof(struct comms_desc_header) + fw_data_size);
2108 
2109 	rc = hl_fw_dynamic_validate_descriptor(hdev, fw_loader,
2110 					(struct lkd_fw_comms_desc *) temp_fw_desc);
2111 
2112 	if (!rc)
2113 		hl_fw_dynamic_read_descriptor_msg(hdev, temp_fw_desc);
2114 
2115 	vfree(temp_fw_desc);
2116 
2117 	return rc;
2118 }
2119 
2120 /**
2121  * hl_fw_dynamic_request_descriptor - handshake with CPU to get FW descriptor
2122  *
2123  * @hdev: pointer to the habanalabs device structure
2124  * @fw_loader: managing structure for loading device's FW
2125  * @next_image_size: size to allocate for next FW component
2126  *
2127  * @return 0 on success, otherwise non-zero error code
2128  */
2129 static int hl_fw_dynamic_request_descriptor(struct hl_device *hdev,
2130 						struct fw_load_mgr *fw_loader,
2131 						size_t next_image_size)
2132 {
2133 	int rc;
2134 
2135 	rc = hl_fw_dynamic_send_protocol_cmd(hdev, fw_loader, COMMS_PREP_DESC,
2136 						next_image_size, true,
2137 						fw_loader->cpu_timeout);
2138 	if (rc)
2139 		return rc;
2140 
2141 	return hl_fw_dynamic_read_and_validate_descriptor(hdev, fw_loader);
2142 }
2143 
2144 /**
2145  * hl_fw_dynamic_read_device_fw_version - read FW version to exposed properties
2146  *
2147  * @hdev: pointer to the habanalabs device structure
2148  * @fwc: the firmware component
2149  * @fw_version: fw component's version string
2150  */
2151 static int hl_fw_dynamic_read_device_fw_version(struct hl_device *hdev,
2152 					enum hl_fw_component fwc,
2153 					const char *fw_version)
2154 {
2155 	struct asic_fixed_properties *prop = &hdev->asic_prop;
2156 	char *preboot_ver, *boot_ver;
2157 	char btl_ver[32];
2158 
2159 	switch (fwc) {
2160 	case FW_COMP_BOOT_FIT:
2161 		strscpy(prop->uboot_ver, fw_version, VERSION_MAX_LEN);
2162 		boot_ver = extract_fw_ver_from_str(prop->uboot_ver);
2163 		if (boot_ver) {
2164 			dev_info(hdev->dev, "boot-fit version %s\n", boot_ver);
2165 			kfree(boot_ver);
2166 		}
2167 
2168 		break;
2169 	case FW_COMP_PREBOOT:
2170 		strscpy(prop->preboot_ver, fw_version, VERSION_MAX_LEN);
2171 		preboot_ver = strnstr(prop->preboot_ver, "Preboot",
2172 						VERSION_MAX_LEN);
2173 		if (preboot_ver && preboot_ver != prop->preboot_ver) {
2174 			strscpy(btl_ver, prop->preboot_ver,
2175 				min((int) (preboot_ver - prop->preboot_ver), 31));
2176 			dev_info(hdev->dev, "%s\n", btl_ver);
2177 		}
2178 
2179 		preboot_ver = extract_fw_ver_from_str(prop->preboot_ver);
2180 		if (preboot_ver) {
2181 			int rc;
2182 
2183 			dev_info(hdev->dev, "preboot version %s\n", preboot_ver);
2184 
2185 			/* This function takes care of freeing preboot_ver */
2186 			rc = extract_fw_sub_versions(hdev, preboot_ver);
2187 			if (rc)
2188 				return rc;
2189 		}
2190 
2191 		break;
2192 	default:
2193 		dev_warn(hdev->dev, "Undefined FW component: %d\n", fwc);
2194 		return -EINVAL;
2195 	}
2196 
2197 	return 0;
2198 }
2199 
2200 /**
2201  * hl_fw_dynamic_copy_image - copy image to memory allocated by the FW
2202  *
2203  * @hdev: pointer to the habanalabs device structure
2204  * @fw: fw descriptor
2205  * @fw_loader: managing structure for loading device's FW
2206  */
2207 static int hl_fw_dynamic_copy_image(struct hl_device *hdev,
2208 						const struct firmware *fw,
2209 						struct fw_load_mgr *fw_loader)
2210 {
2211 	struct lkd_fw_comms_desc *fw_desc;
2212 	struct pci_mem_region *region;
2213 	void __iomem *dest;
2214 	u64 addr;
2215 	int rc;
2216 
2217 	fw_desc = &fw_loader->dynamic_loader.comm_desc;
2218 	addr = le64_to_cpu(fw_desc->img_addr);
2219 
2220 	/* find memory region to which to copy the image */
2221 	region = fw_loader->dynamic_loader.image_region;
2222 
2223 	dest = hdev->pcie_bar[region->bar_id] + region->offset_in_bar +
2224 					(addr - region->region_base);
2225 
2226 	rc = hl_fw_copy_fw_to_device(hdev, fw, dest,
2227 					fw_loader->boot_fit_img.src_off,
2228 					fw_loader->boot_fit_img.copy_size);
2229 
2230 	return rc;
2231 }
2232 
2233 /**
2234  * hl_fw_dynamic_copy_msg - copy msg to memory allocated by the FW
2235  *
2236  * @hdev: pointer to the habanalabs device structure
2237  * @msg: message
2238  * @fw_loader: managing structure for loading device's FW
2239  */
2240 static int hl_fw_dynamic_copy_msg(struct hl_device *hdev,
2241 		struct lkd_msg_comms *msg, struct fw_load_mgr *fw_loader)
2242 {
2243 	struct lkd_fw_comms_desc *fw_desc;
2244 	struct pci_mem_region *region;
2245 	void __iomem *dest;
2246 	u64 addr;
2247 	int rc;
2248 
2249 	fw_desc = &fw_loader->dynamic_loader.comm_desc;
2250 	addr = le64_to_cpu(fw_desc->img_addr);
2251 
2252 	/* find memory region to which to copy the image */
2253 	region = fw_loader->dynamic_loader.image_region;
2254 
2255 	dest = hdev->pcie_bar[region->bar_id] + region->offset_in_bar +
2256 					(addr - region->region_base);
2257 
2258 	rc = hl_fw_copy_msg_to_device(hdev, msg, dest, 0, 0);
2259 
2260 	return rc;
2261 }
2262 
2263 /**
2264  * hl_fw_boot_fit_update_state - update internal data structures after boot-fit
2265  *                               is loaded
2266  *
2267  * @hdev: pointer to the habanalabs device structure
2268  * @cpu_boot_dev_sts0_reg: register holding CPU boot dev status 0
2269  * @cpu_boot_dev_sts1_reg: register holding CPU boot dev status 1
2270  *
2271  * @return 0 on success, otherwise non-zero error code
2272  */
2273 static void hl_fw_boot_fit_update_state(struct hl_device *hdev,
2274 						u32 cpu_boot_dev_sts0_reg,
2275 						u32 cpu_boot_dev_sts1_reg)
2276 {
2277 	struct asic_fixed_properties *prop = &hdev->asic_prop;
2278 
2279 	hdev->fw_loader.fw_comp_loaded |= FW_TYPE_BOOT_CPU;
2280 
2281 	/* Read boot_cpu status bits */
2282 	if (prop->fw_preboot_cpu_boot_dev_sts0 & CPU_BOOT_DEV_STS0_ENABLED) {
2283 		prop->fw_bootfit_cpu_boot_dev_sts0 =
2284 				RREG32(cpu_boot_dev_sts0_reg);
2285 
2286 		prop->hard_reset_done_by_fw = !!(prop->fw_bootfit_cpu_boot_dev_sts0 &
2287 							CPU_BOOT_DEV_STS0_FW_HARD_RST_EN);
2288 
2289 		dev_dbg(hdev->dev, "Firmware boot CPU status0 %#x\n",
2290 					prop->fw_bootfit_cpu_boot_dev_sts0);
2291 	}
2292 
2293 	if (prop->fw_cpu_boot_dev_sts1_valid) {
2294 		prop->fw_bootfit_cpu_boot_dev_sts1 =
2295 				RREG32(cpu_boot_dev_sts1_reg);
2296 
2297 		dev_dbg(hdev->dev, "Firmware boot CPU status1 %#x\n",
2298 					prop->fw_bootfit_cpu_boot_dev_sts1);
2299 	}
2300 
2301 	dev_dbg(hdev->dev, "Firmware boot CPU hard-reset is %s\n",
2302 			prop->hard_reset_done_by_fw ? "enabled" : "disabled");
2303 }
2304 
2305 static void hl_fw_dynamic_update_linux_interrupt_if(struct hl_device *hdev)
2306 {
2307 	struct cpu_dyn_regs *dyn_regs =
2308 			&hdev->fw_loader.dynamic_loader.comm_desc.cpu_dyn_regs;
2309 
2310 	/* Check whether all 3 interrupt interfaces are set, if not use a
2311 	 * single interface
2312 	 */
2313 	if (!hdev->asic_prop.gic_interrupts_enable &&
2314 			!(hdev->asic_prop.fw_app_cpu_boot_dev_sts0 &
2315 				CPU_BOOT_DEV_STS0_MULTI_IRQ_POLL_EN)) {
2316 		dyn_regs->gic_host_halt_irq = dyn_regs->gic_host_pi_upd_irq;
2317 		dyn_regs->gic_host_ints_irq = dyn_regs->gic_host_pi_upd_irq;
2318 
2319 		dev_warn(hdev->dev,
2320 			"Using a single interrupt interface towards cpucp");
2321 	}
2322 }
2323 /**
2324  * hl_fw_dynamic_load_image - load FW image using dynamic protocol
2325  *
2326  * @hdev: pointer to the habanalabs device structure
2327  * @fw_loader: managing structure for loading device's FW
2328  * @load_fwc: the FW component to be loaded
2329  * @img_ld_timeout: image load timeout
2330  *
2331  * @return 0 on success, otherwise non-zero error code
2332  */
2333 static int hl_fw_dynamic_load_image(struct hl_device *hdev,
2334 						struct fw_load_mgr *fw_loader,
2335 						enum hl_fw_component load_fwc,
2336 						u32 img_ld_timeout)
2337 {
2338 	enum hl_fw_component cur_fwc;
2339 	const struct firmware *fw;
2340 	char *fw_name;
2341 	int rc = 0;
2342 
2343 	/*
2344 	 * when loading image we have one of 2 scenarios:
2345 	 * 1. current FW component is preboot and we want to load boot-fit
2346 	 * 2. current FW component is boot-fit and we want to load linux
2347 	 */
2348 	if (load_fwc == FW_COMP_BOOT_FIT) {
2349 		cur_fwc = FW_COMP_PREBOOT;
2350 		fw_name = fw_loader->boot_fit_img.image_name;
2351 	} else {
2352 		cur_fwc = FW_COMP_BOOT_FIT;
2353 		fw_name = fw_loader->linux_img.image_name;
2354 	}
2355 
2356 	/* request FW in order to communicate to FW the size to be allocated */
2357 	rc = hl_request_fw(hdev, &fw, fw_name);
2358 	if (rc)
2359 		return rc;
2360 
2361 	/* store the image size for future validation */
2362 	fw_loader->dynamic_loader.fw_image_size = fw->size;
2363 
2364 	rc = hl_fw_dynamic_request_descriptor(hdev, fw_loader, fw->size);
2365 	if (rc)
2366 		goto release_fw;
2367 
2368 	/* read preboot version */
2369 	rc = hl_fw_dynamic_read_device_fw_version(hdev, cur_fwc,
2370 				fw_loader->dynamic_loader.comm_desc.cur_fw_ver);
2371 	if (rc)
2372 		goto release_fw;
2373 
2374 	/* update state according to boot stage */
2375 	if (cur_fwc == FW_COMP_BOOT_FIT) {
2376 		struct cpu_dyn_regs *dyn_regs;
2377 
2378 		dyn_regs = &fw_loader->dynamic_loader.comm_desc.cpu_dyn_regs;
2379 		hl_fw_boot_fit_update_state(hdev,
2380 				le32_to_cpu(dyn_regs->cpu_boot_dev_sts0),
2381 				le32_to_cpu(dyn_regs->cpu_boot_dev_sts1));
2382 	}
2383 
2384 	/* copy boot fit to space allocated by FW */
2385 	rc = hl_fw_dynamic_copy_image(hdev, fw, fw_loader);
2386 	if (rc)
2387 		goto release_fw;
2388 
2389 	rc = hl_fw_dynamic_send_protocol_cmd(hdev, fw_loader, COMMS_DATA_RDY,
2390 						0, true,
2391 						fw_loader->cpu_timeout);
2392 	if (rc)
2393 		goto release_fw;
2394 
2395 	rc = hl_fw_dynamic_send_protocol_cmd(hdev, fw_loader, COMMS_EXEC,
2396 						0, false,
2397 						img_ld_timeout);
2398 
2399 release_fw:
2400 	hl_release_firmware(fw);
2401 	return rc;
2402 }
2403 
2404 static int hl_fw_dynamic_wait_for_boot_fit_active(struct hl_device *hdev,
2405 					struct fw_load_mgr *fw_loader)
2406 {
2407 	struct dynamic_fw_load_mgr *dyn_loader;
2408 	u32 status;
2409 	int rc;
2410 
2411 	dyn_loader = &fw_loader->dynamic_loader;
2412 
2413 	/*
2414 	 * Make sure CPU boot-loader is running
2415 	 * Note that the CPU_BOOT_STATUS_SRAM_AVAIL is generally set by Linux
2416 	 * yet there is a debug scenario in which we loading uboot (without Linux)
2417 	 * which at later stage is relocated to DRAM. In this case we expect
2418 	 * uboot to set the CPU_BOOT_STATUS_SRAM_AVAIL and so we add it to the
2419 	 * poll flags
2420 	 */
2421 	rc = hl_poll_timeout(
2422 		hdev,
2423 		le32_to_cpu(dyn_loader->comm_desc.cpu_dyn_regs.cpu_boot_status),
2424 		status,
2425 		(status == CPU_BOOT_STATUS_READY_TO_BOOT) ||
2426 		(status == CPU_BOOT_STATUS_SRAM_AVAIL),
2427 		hdev->fw_poll_interval_usec,
2428 		dyn_loader->wait_for_bl_timeout);
2429 	if (rc) {
2430 		dev_err(hdev->dev, "failed to wait for boot\n");
2431 		return rc;
2432 	}
2433 
2434 	dev_dbg(hdev->dev, "uboot status = %d\n", status);
2435 	return 0;
2436 }
2437 
2438 static int hl_fw_dynamic_wait_for_linux_active(struct hl_device *hdev,
2439 						struct fw_load_mgr *fw_loader)
2440 {
2441 	struct dynamic_fw_load_mgr *dyn_loader;
2442 	u32 status;
2443 	int rc;
2444 
2445 	dyn_loader = &fw_loader->dynamic_loader;
2446 
2447 	/* Make sure CPU linux is running */
2448 
2449 	rc = hl_poll_timeout(
2450 		hdev,
2451 		le32_to_cpu(dyn_loader->comm_desc.cpu_dyn_regs.cpu_boot_status),
2452 		status,
2453 		(status == CPU_BOOT_STATUS_SRAM_AVAIL),
2454 		hdev->fw_poll_interval_usec,
2455 		fw_loader->cpu_timeout);
2456 	if (rc) {
2457 		dev_err(hdev->dev, "failed to wait for Linux\n");
2458 		return rc;
2459 	}
2460 
2461 	dev_dbg(hdev->dev, "Boot status = %d\n", status);
2462 	return 0;
2463 }
2464 
2465 /**
2466  * hl_fw_linux_update_state -	update internal data structures after Linux
2467  *				is loaded.
2468  *				Note: Linux initialization is comprised mainly
2469  *				of two stages - loading kernel (SRAM_AVAIL)
2470  *				& loading ARMCP.
2471  *				Therefore reading boot device status in any of
2472  *				these stages might result in different values.
2473  *
2474  * @hdev: pointer to the habanalabs device structure
2475  * @cpu_boot_dev_sts0_reg: register holding CPU boot dev status 0
2476  * @cpu_boot_dev_sts1_reg: register holding CPU boot dev status 1
2477  *
2478  * @return 0 on success, otherwise non-zero error code
2479  */
2480 static void hl_fw_linux_update_state(struct hl_device *hdev,
2481 						u32 cpu_boot_dev_sts0_reg,
2482 						u32 cpu_boot_dev_sts1_reg)
2483 {
2484 	struct asic_fixed_properties *prop = &hdev->asic_prop;
2485 
2486 	hdev->fw_loader.fw_comp_loaded |= FW_TYPE_LINUX;
2487 
2488 	/* Read FW application security bits */
2489 	if (prop->fw_cpu_boot_dev_sts0_valid) {
2490 		prop->fw_app_cpu_boot_dev_sts0 = RREG32(cpu_boot_dev_sts0_reg);
2491 
2492 		prop->hard_reset_done_by_fw = !!(prop->fw_app_cpu_boot_dev_sts0 &
2493 							CPU_BOOT_DEV_STS0_FW_HARD_RST_EN);
2494 
2495 		if (prop->fw_app_cpu_boot_dev_sts0 &
2496 				CPU_BOOT_DEV_STS0_GIC_PRIVILEGED_EN)
2497 			prop->gic_interrupts_enable = false;
2498 
2499 		dev_dbg(hdev->dev,
2500 			"Firmware application CPU status0 %#x\n",
2501 			prop->fw_app_cpu_boot_dev_sts0);
2502 
2503 		dev_dbg(hdev->dev, "GIC controller is %s\n",
2504 				prop->gic_interrupts_enable ?
2505 						"enabled" : "disabled");
2506 	}
2507 
2508 	if (prop->fw_cpu_boot_dev_sts1_valid) {
2509 		prop->fw_app_cpu_boot_dev_sts1 = RREG32(cpu_boot_dev_sts1_reg);
2510 
2511 		dev_dbg(hdev->dev,
2512 			"Firmware application CPU status1 %#x\n",
2513 			prop->fw_app_cpu_boot_dev_sts1);
2514 	}
2515 
2516 	dev_dbg(hdev->dev, "Firmware application CPU hard-reset is %s\n",
2517 			prop->hard_reset_done_by_fw ? "enabled" : "disabled");
2518 
2519 	dev_info(hdev->dev, "Successfully loaded firmware to device\n");
2520 }
2521 
2522 /**
2523  * hl_fw_dynamic_send_msg - send a COMMS message with attached data
2524  *
2525  * @hdev: pointer to the habanalabs device structure
2526  * @fw_loader: managing structure for loading device's FW
2527  * @msg_type: message type
2528  * @data: data to be sent
2529  *
2530  * @return 0 on success, otherwise non-zero error code
2531  */
2532 static int hl_fw_dynamic_send_msg(struct hl_device *hdev,
2533 		struct fw_load_mgr *fw_loader, u8 msg_type, void *data)
2534 {
2535 	struct lkd_msg_comms *msg;
2536 	int rc;
2537 
2538 	msg = kzalloc(sizeof(*msg), GFP_KERNEL);
2539 	if (!msg)
2540 		return -ENOMEM;
2541 
2542 	/* create message to be sent */
2543 	msg->header.type = msg_type;
2544 	msg->header.size = cpu_to_le16(sizeof(struct comms_msg_header));
2545 	msg->header.magic = cpu_to_le32(HL_COMMS_MSG_MAGIC);
2546 
2547 	switch (msg_type) {
2548 	case HL_COMMS_RESET_CAUSE_TYPE:
2549 		msg->reset_cause = *(__u8 *) data;
2550 		break;
2551 
2552 	default:
2553 		dev_err(hdev->dev,
2554 			"Send COMMS message - invalid message type %u\n",
2555 			msg_type);
2556 		rc = -EINVAL;
2557 		goto out;
2558 	}
2559 
2560 	rc = hl_fw_dynamic_request_descriptor(hdev, fw_loader,
2561 			sizeof(struct lkd_msg_comms));
2562 	if (rc)
2563 		goto out;
2564 
2565 	/* copy message to space allocated by FW */
2566 	rc = hl_fw_dynamic_copy_msg(hdev, msg, fw_loader);
2567 	if (rc)
2568 		goto out;
2569 
2570 	rc = hl_fw_dynamic_send_protocol_cmd(hdev, fw_loader, COMMS_DATA_RDY,
2571 						0, true,
2572 						fw_loader->cpu_timeout);
2573 	if (rc)
2574 		goto out;
2575 
2576 	rc = hl_fw_dynamic_send_protocol_cmd(hdev, fw_loader, COMMS_EXEC,
2577 						0, true,
2578 						fw_loader->cpu_timeout);
2579 
2580 out:
2581 	kfree(msg);
2582 	return rc;
2583 }
2584 
2585 /**
2586  * hl_fw_dynamic_init_cpu - initialize the device CPU using dynamic protocol
2587  *
2588  * @hdev: pointer to the habanalabs device structure
2589  * @fw_loader: managing structure for loading device's FW
2590  *
2591  * @return 0 on success, otherwise non-zero error code
2592  *
2593  * brief: the dynamic protocol is master (LKD) slave (FW CPU) protocol.
2594  * the communication is done using registers:
2595  * - LKD command register
2596  * - FW status register
2597  * the protocol is race free. this goal is achieved by splitting the requests
2598  * and response to known synchronization points between the LKD and the FW.
2599  * each response to LKD request is known and bound to a predefined timeout.
2600  * in case of timeout expiration without the desired status from FW- the
2601  * protocol (and hence the boot) will fail.
2602  */
2603 static int hl_fw_dynamic_init_cpu(struct hl_device *hdev,
2604 					struct fw_load_mgr *fw_loader)
2605 {
2606 	struct cpu_dyn_regs *dyn_regs;
2607 	int rc, fw_error_rc;
2608 
2609 	dev_info(hdev->dev,
2610 		"Loading %sfirmware to device, may take some time...\n",
2611 		hdev->asic_prop.fw_security_enabled ? "secured " : "");
2612 
2613 	/* initialize FW descriptor as invalid */
2614 	fw_loader->dynamic_loader.fw_desc_valid = false;
2615 
2616 	/*
2617 	 * In this stage, "cpu_dyn_regs" contains only LKD's hard coded values!
2618 	 * It will be updated from FW after hl_fw_dynamic_request_descriptor().
2619 	 */
2620 	dyn_regs = &fw_loader->dynamic_loader.comm_desc.cpu_dyn_regs;
2621 
2622 	rc = hl_fw_dynamic_send_protocol_cmd(hdev, fw_loader, COMMS_RST_STATE,
2623 						0, true,
2624 						fw_loader->cpu_timeout);
2625 	if (rc)
2626 		goto protocol_err;
2627 
2628 	if (hdev->reset_info.curr_reset_cause) {
2629 		rc = hl_fw_dynamic_send_msg(hdev, fw_loader,
2630 				HL_COMMS_RESET_CAUSE_TYPE, &hdev->reset_info.curr_reset_cause);
2631 		if (rc)
2632 			goto protocol_err;
2633 
2634 		/* Clear current reset cause */
2635 		hdev->reset_info.curr_reset_cause = HL_RESET_CAUSE_UNKNOWN;
2636 	}
2637 
2638 	if (!(hdev->fw_components & FW_TYPE_BOOT_CPU)) {
2639 		struct lkd_fw_binning_info *binning_info;
2640 
2641 		rc = hl_fw_dynamic_request_descriptor(hdev, fw_loader, 0);
2642 		if (rc)
2643 			goto protocol_err;
2644 
2645 		/* read preboot version */
2646 		rc = hl_fw_dynamic_read_device_fw_version(hdev, FW_COMP_PREBOOT,
2647 				fw_loader->dynamic_loader.comm_desc.cur_fw_ver);
2648 
2649 		if (rc)
2650 			goto out;
2651 
2652 		/* read binning info from preboot */
2653 		if (hdev->support_preboot_binning) {
2654 			binning_info = &fw_loader->dynamic_loader.comm_desc.binning_info;
2655 			hdev->tpc_binning = le64_to_cpu(binning_info->tpc_mask_l);
2656 			hdev->dram_binning = le32_to_cpu(binning_info->dram_mask);
2657 			hdev->edma_binning = le32_to_cpu(binning_info->edma_mask);
2658 			hdev->decoder_binning = le32_to_cpu(binning_info->dec_mask);
2659 			hdev->rotator_binning = le32_to_cpu(binning_info->rot_mask);
2660 
2661 			rc = hdev->asic_funcs->set_dram_properties(hdev);
2662 			if (rc)
2663 				goto out;
2664 
2665 			dev_dbg(hdev->dev,
2666 				"Read binning masks: tpc: 0x%llx, dram: 0x%llx, edma: 0x%x, dec: 0x%x, rot:0x%x\n",
2667 				hdev->tpc_binning, hdev->dram_binning, hdev->edma_binning,
2668 				hdev->decoder_binning, hdev->rotator_binning);
2669 		}
2670 out:
2671 		return rc;
2672 	}
2673 
2674 	/* load boot fit to FW */
2675 	rc = hl_fw_dynamic_load_image(hdev, fw_loader, FW_COMP_BOOT_FIT,
2676 						fw_loader->boot_fit_timeout);
2677 	if (rc) {
2678 		dev_err(hdev->dev, "failed to load boot fit\n");
2679 		goto protocol_err;
2680 	}
2681 
2682 	/*
2683 	 * when testing FW load (without Linux) on PLDM we don't want to
2684 	 * wait until boot fit is active as it may take several hours.
2685 	 * instead, we load the bootfit and let it do all initialization in
2686 	 * the background.
2687 	 */
2688 	if (hdev->pldm && !(hdev->fw_components & FW_TYPE_LINUX))
2689 		return 0;
2690 
2691 	rc = hl_fw_dynamic_wait_for_boot_fit_active(hdev, fw_loader);
2692 	if (rc)
2693 		goto protocol_err;
2694 
2695 	/* Enable DRAM scrambling before Linux boot and after successful
2696 	 *  UBoot
2697 	 */
2698 	hdev->asic_funcs->init_cpu_scrambler_dram(hdev);
2699 
2700 	if (!(hdev->fw_components & FW_TYPE_LINUX)) {
2701 		dev_info(hdev->dev, "Skip loading Linux F/W\n");
2702 		return 0;
2703 	}
2704 
2705 	if (fw_loader->skip_bmc) {
2706 		rc = hl_fw_dynamic_send_protocol_cmd(hdev, fw_loader,
2707 							COMMS_SKIP_BMC, 0,
2708 							true,
2709 							fw_loader->cpu_timeout);
2710 		if (rc) {
2711 			dev_err(hdev->dev, "failed to load boot fit\n");
2712 			goto protocol_err;
2713 		}
2714 	}
2715 
2716 	/* load Linux image to FW */
2717 	rc = hl_fw_dynamic_load_image(hdev, fw_loader, FW_COMP_LINUX,
2718 							fw_loader->cpu_timeout);
2719 	if (rc) {
2720 		dev_err(hdev->dev, "failed to load Linux\n");
2721 		goto protocol_err;
2722 	}
2723 
2724 	rc = hl_fw_dynamic_wait_for_linux_active(hdev, fw_loader);
2725 	if (rc)
2726 		goto protocol_err;
2727 
2728 	hl_fw_linux_update_state(hdev, le32_to_cpu(dyn_regs->cpu_boot_dev_sts0),
2729 				le32_to_cpu(dyn_regs->cpu_boot_dev_sts1));
2730 
2731 	hl_fw_dynamic_update_linux_interrupt_if(hdev);
2732 
2733 protocol_err:
2734 	if (fw_loader->dynamic_loader.fw_desc_valid) {
2735 		fw_error_rc = fw_read_errors(hdev, le32_to_cpu(dyn_regs->cpu_boot_err0),
2736 				le32_to_cpu(dyn_regs->cpu_boot_err1),
2737 				le32_to_cpu(dyn_regs->cpu_boot_dev_sts0),
2738 				le32_to_cpu(dyn_regs->cpu_boot_dev_sts1));
2739 
2740 		if (fw_error_rc)
2741 			return fw_error_rc;
2742 	}
2743 
2744 	return rc;
2745 }
2746 
2747 /**
2748  * hl_fw_static_init_cpu - initialize the device CPU using static protocol
2749  *
2750  * @hdev: pointer to the habanalabs device structure
2751  * @fw_loader: managing structure for loading device's FW
2752  *
2753  * @return 0 on success, otherwise non-zero error code
2754  */
2755 static int hl_fw_static_init_cpu(struct hl_device *hdev,
2756 					struct fw_load_mgr *fw_loader)
2757 {
2758 	u32 cpu_msg_status_reg, cpu_timeout, msg_to_cpu_reg, status;
2759 	u32 cpu_boot_dev_status0_reg, cpu_boot_dev_status1_reg;
2760 	struct static_fw_load_mgr *static_loader;
2761 	u32 cpu_boot_status_reg;
2762 	int rc;
2763 
2764 	if (!(hdev->fw_components & FW_TYPE_BOOT_CPU))
2765 		return 0;
2766 
2767 	/* init common loader parameters */
2768 	cpu_timeout = fw_loader->cpu_timeout;
2769 
2770 	/* init static loader parameters */
2771 	static_loader = &fw_loader->static_loader;
2772 	cpu_msg_status_reg = static_loader->cpu_cmd_status_to_host_reg;
2773 	msg_to_cpu_reg = static_loader->kmd_msg_to_cpu_reg;
2774 	cpu_boot_dev_status0_reg = static_loader->cpu_boot_dev_status0_reg;
2775 	cpu_boot_dev_status1_reg = static_loader->cpu_boot_dev_status1_reg;
2776 	cpu_boot_status_reg = static_loader->cpu_boot_status_reg;
2777 
2778 	dev_info(hdev->dev, "Going to wait for device boot (up to %lds)\n",
2779 		cpu_timeout / USEC_PER_SEC);
2780 
2781 	/* Wait for boot FIT request */
2782 	rc = hl_poll_timeout(
2783 		hdev,
2784 		cpu_boot_status_reg,
2785 		status,
2786 		status == CPU_BOOT_STATUS_WAITING_FOR_BOOT_FIT,
2787 		hdev->fw_poll_interval_usec,
2788 		fw_loader->boot_fit_timeout);
2789 
2790 	if (rc) {
2791 		dev_dbg(hdev->dev,
2792 			"No boot fit request received, resuming boot\n");
2793 	} else {
2794 		rc = hdev->asic_funcs->load_boot_fit_to_device(hdev);
2795 		if (rc)
2796 			goto out;
2797 
2798 		/* Clear device CPU message status */
2799 		WREG32(cpu_msg_status_reg, CPU_MSG_CLR);
2800 
2801 		/* Signal device CPU that boot loader is ready */
2802 		WREG32(msg_to_cpu_reg, KMD_MSG_FIT_RDY);
2803 
2804 		/* Poll for CPU device ack */
2805 		rc = hl_poll_timeout(
2806 			hdev,
2807 			cpu_msg_status_reg,
2808 			status,
2809 			status == CPU_MSG_OK,
2810 			hdev->fw_poll_interval_usec,
2811 			fw_loader->boot_fit_timeout);
2812 
2813 		if (rc) {
2814 			dev_err(hdev->dev,
2815 				"Timeout waiting for boot fit load ack\n");
2816 			goto out;
2817 		}
2818 
2819 		/* Clear message */
2820 		WREG32(msg_to_cpu_reg, KMD_MSG_NA);
2821 	}
2822 
2823 	/*
2824 	 * Make sure CPU boot-loader is running
2825 	 * Note that the CPU_BOOT_STATUS_SRAM_AVAIL is generally set by Linux
2826 	 * yet there is a debug scenario in which we loading uboot (without Linux)
2827 	 * which at later stage is relocated to DRAM. In this case we expect
2828 	 * uboot to set the CPU_BOOT_STATUS_SRAM_AVAIL and so we add it to the
2829 	 * poll flags
2830 	 */
2831 	rc = hl_poll_timeout(
2832 		hdev,
2833 		cpu_boot_status_reg,
2834 		status,
2835 		(status == CPU_BOOT_STATUS_DRAM_RDY) ||
2836 		(status == CPU_BOOT_STATUS_NIC_FW_RDY) ||
2837 		(status == CPU_BOOT_STATUS_READY_TO_BOOT) ||
2838 		(status == CPU_BOOT_STATUS_SRAM_AVAIL),
2839 		hdev->fw_poll_interval_usec,
2840 		cpu_timeout);
2841 
2842 	dev_dbg(hdev->dev, "uboot status = %d\n", status);
2843 
2844 	/* Read U-Boot version now in case we will later fail */
2845 	hl_fw_static_read_device_fw_version(hdev, FW_COMP_BOOT_FIT);
2846 
2847 	/* update state according to boot stage */
2848 	hl_fw_boot_fit_update_state(hdev, cpu_boot_dev_status0_reg,
2849 						cpu_boot_dev_status1_reg);
2850 
2851 	if (rc) {
2852 		detect_cpu_boot_status(hdev, status);
2853 		rc = -EIO;
2854 		goto out;
2855 	}
2856 
2857 	/* Enable DRAM scrambling before Linux boot and after successful
2858 	 *  UBoot
2859 	 */
2860 	hdev->asic_funcs->init_cpu_scrambler_dram(hdev);
2861 
2862 	if (!(hdev->fw_components & FW_TYPE_LINUX)) {
2863 		dev_info(hdev->dev, "Skip loading Linux F/W\n");
2864 		rc = 0;
2865 		goto out;
2866 	}
2867 
2868 	if (status == CPU_BOOT_STATUS_SRAM_AVAIL) {
2869 		rc = 0;
2870 		goto out;
2871 	}
2872 
2873 	dev_info(hdev->dev,
2874 		"Loading firmware to device, may take some time...\n");
2875 
2876 	rc = hdev->asic_funcs->load_firmware_to_device(hdev);
2877 	if (rc)
2878 		goto out;
2879 
2880 	if (fw_loader->skip_bmc) {
2881 		WREG32(msg_to_cpu_reg, KMD_MSG_SKIP_BMC);
2882 
2883 		rc = hl_poll_timeout(
2884 			hdev,
2885 			cpu_boot_status_reg,
2886 			status,
2887 			(status == CPU_BOOT_STATUS_BMC_WAITING_SKIPPED),
2888 			hdev->fw_poll_interval_usec,
2889 			cpu_timeout);
2890 
2891 		if (rc) {
2892 			dev_err(hdev->dev,
2893 				"Failed to get ACK on skipping BMC, %d\n",
2894 				status);
2895 			WREG32(msg_to_cpu_reg, KMD_MSG_NA);
2896 			rc = -EIO;
2897 			goto out;
2898 		}
2899 	}
2900 
2901 	WREG32(msg_to_cpu_reg, KMD_MSG_FIT_RDY);
2902 
2903 	rc = hl_poll_timeout(
2904 		hdev,
2905 		cpu_boot_status_reg,
2906 		status,
2907 		(status == CPU_BOOT_STATUS_SRAM_AVAIL),
2908 		hdev->fw_poll_interval_usec,
2909 		cpu_timeout);
2910 
2911 	/* Clear message */
2912 	WREG32(msg_to_cpu_reg, KMD_MSG_NA);
2913 
2914 	if (rc) {
2915 		if (status == CPU_BOOT_STATUS_FIT_CORRUPTED)
2916 			dev_err(hdev->dev,
2917 				"Device reports FIT image is corrupted\n");
2918 		else
2919 			dev_err(hdev->dev,
2920 				"Failed to load firmware to device, %d\n",
2921 				status);
2922 
2923 		rc = -EIO;
2924 		goto out;
2925 	}
2926 
2927 	rc = fw_read_errors(hdev, fw_loader->static_loader.boot_err0_reg,
2928 					fw_loader->static_loader.boot_err1_reg,
2929 					cpu_boot_dev_status0_reg,
2930 					cpu_boot_dev_status1_reg);
2931 	if (rc)
2932 		return rc;
2933 
2934 	hl_fw_linux_update_state(hdev, cpu_boot_dev_status0_reg,
2935 						cpu_boot_dev_status1_reg);
2936 
2937 	return 0;
2938 
2939 out:
2940 	fw_read_errors(hdev, fw_loader->static_loader.boot_err0_reg,
2941 					fw_loader->static_loader.boot_err1_reg,
2942 					cpu_boot_dev_status0_reg,
2943 					cpu_boot_dev_status1_reg);
2944 
2945 	return rc;
2946 }
2947 
2948 /**
2949  * hl_fw_init_cpu - initialize the device CPU
2950  *
2951  * @hdev: pointer to the habanalabs device structure
2952  *
2953  * @return 0 on success, otherwise non-zero error code
2954  *
2955  * perform necessary initializations for device's CPU. takes into account if
2956  * init protocol is static or dynamic.
2957  */
2958 int hl_fw_init_cpu(struct hl_device *hdev)
2959 {
2960 	struct asic_fixed_properties *prop = &hdev->asic_prop;
2961 	struct fw_load_mgr *fw_loader = &hdev->fw_loader;
2962 
2963 	return  prop->dynamic_fw_load ?
2964 			hl_fw_dynamic_init_cpu(hdev, fw_loader) :
2965 			hl_fw_static_init_cpu(hdev, fw_loader);
2966 }
2967 
2968 void hl_fw_set_pll_profile(struct hl_device *hdev)
2969 {
2970 	hl_fw_set_frequency(hdev, hdev->asic_prop.clk_pll_index,
2971 				hdev->asic_prop.max_freq_value);
2972 }
2973 
2974 int hl_fw_get_clk_rate(struct hl_device *hdev, u32 *cur_clk, u32 *max_clk)
2975 {
2976 	long value;
2977 
2978 	if (!hl_device_operational(hdev, NULL))
2979 		return -ENODEV;
2980 
2981 	if (!hdev->pdev) {
2982 		*cur_clk = 0;
2983 		*max_clk = 0;
2984 		return 0;
2985 	}
2986 
2987 	value = hl_fw_get_frequency(hdev, hdev->asic_prop.clk_pll_index, false);
2988 
2989 	if (value < 0) {
2990 		dev_err(hdev->dev, "Failed to retrieve device max clock %ld\n", value);
2991 		return value;
2992 	}
2993 
2994 	*max_clk = (value / 1000 / 1000);
2995 
2996 	value = hl_fw_get_frequency(hdev, hdev->asic_prop.clk_pll_index, true);
2997 
2998 	if (value < 0) {
2999 		dev_err(hdev->dev, "Failed to retrieve device current clock %ld\n", value);
3000 		return value;
3001 	}
3002 
3003 	*cur_clk = (value / 1000 / 1000);
3004 
3005 	return 0;
3006 }
3007 
3008 long hl_fw_get_frequency(struct hl_device *hdev, u32 pll_index, bool curr)
3009 {
3010 	struct cpucp_packet pkt;
3011 	u32 used_pll_idx;
3012 	u64 result;
3013 	int rc;
3014 
3015 	rc = get_used_pll_index(hdev, pll_index, &used_pll_idx);
3016 	if (rc)
3017 		return rc;
3018 
3019 	memset(&pkt, 0, sizeof(pkt));
3020 
3021 	if (curr)
3022 		pkt.ctl = cpu_to_le32(CPUCP_PACKET_FREQUENCY_CURR_GET <<
3023 						CPUCP_PKT_CTL_OPCODE_SHIFT);
3024 	else
3025 		pkt.ctl = cpu_to_le32(CPUCP_PACKET_FREQUENCY_GET << CPUCP_PKT_CTL_OPCODE_SHIFT);
3026 
3027 	pkt.pll_index = cpu_to_le32((u32)used_pll_idx);
3028 
3029 	rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt), 0, &result);
3030 
3031 	if (rc) {
3032 		dev_err(hdev->dev, "Failed to get frequency of PLL %d, error %d\n",
3033 			used_pll_idx, rc);
3034 		return rc;
3035 	}
3036 
3037 	return (long) result;
3038 }
3039 
3040 void hl_fw_set_frequency(struct hl_device *hdev, u32 pll_index, u64 freq)
3041 {
3042 	struct cpucp_packet pkt;
3043 	u32 used_pll_idx;
3044 	int rc;
3045 
3046 	rc = get_used_pll_index(hdev, pll_index, &used_pll_idx);
3047 	if (rc)
3048 		return;
3049 
3050 	memset(&pkt, 0, sizeof(pkt));
3051 
3052 	pkt.ctl = cpu_to_le32(CPUCP_PACKET_FREQUENCY_SET << CPUCP_PKT_CTL_OPCODE_SHIFT);
3053 	pkt.pll_index = cpu_to_le32((u32)used_pll_idx);
3054 	pkt.value = cpu_to_le64(freq);
3055 
3056 	rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt), 0, NULL);
3057 
3058 	if (rc)
3059 		dev_err(hdev->dev, "Failed to set frequency to PLL %d, error %d\n",
3060 			used_pll_idx, rc);
3061 }
3062 
3063 long hl_fw_get_max_power(struct hl_device *hdev)
3064 {
3065 	struct cpucp_packet pkt;
3066 	u64 result;
3067 	int rc;
3068 
3069 	memset(&pkt, 0, sizeof(pkt));
3070 
3071 	pkt.ctl = cpu_to_le32(CPUCP_PACKET_MAX_POWER_GET << CPUCP_PKT_CTL_OPCODE_SHIFT);
3072 
3073 	rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt), 0, &result);
3074 
3075 	if (rc) {
3076 		dev_err(hdev->dev, "Failed to get max power, error %d\n", rc);
3077 		return rc;
3078 	}
3079 
3080 	return result;
3081 }
3082 
3083 void hl_fw_set_max_power(struct hl_device *hdev)
3084 {
3085 	struct cpucp_packet pkt;
3086 	int rc;
3087 
3088 	/* TODO: remove this after simulator supports this packet */
3089 	if (!hdev->pdev)
3090 		return;
3091 
3092 	memset(&pkt, 0, sizeof(pkt));
3093 
3094 	pkt.ctl = cpu_to_le32(CPUCP_PACKET_MAX_POWER_SET << CPUCP_PKT_CTL_OPCODE_SHIFT);
3095 	pkt.value = cpu_to_le64(hdev->max_power);
3096 
3097 	rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt), 0, NULL);
3098 
3099 	if (rc)
3100 		dev_err(hdev->dev, "Failed to set max power, error %d\n", rc);
3101 }
3102 
3103 static int hl_fw_get_sec_attest_data(struct hl_device *hdev, u32 packet_id, void *data, u32 size,
3104 					u32 nonce, u32 timeout)
3105 {
3106 	struct cpucp_packet pkt = {};
3107 	dma_addr_t req_dma_addr;
3108 	void *req_cpu_addr;
3109 	int rc;
3110 
3111 	req_cpu_addr = hl_cpu_accessible_dma_pool_alloc(hdev, size, &req_dma_addr);
3112 	if (!req_cpu_addr) {
3113 		dev_err(hdev->dev,
3114 			"Failed to allocate DMA memory for CPU-CP packet %u\n", packet_id);
3115 		return -ENOMEM;
3116 	}
3117 
3118 	memset(data, 0, size);
3119 
3120 	pkt.ctl = cpu_to_le32(packet_id << CPUCP_PKT_CTL_OPCODE_SHIFT);
3121 	pkt.addr = cpu_to_le64(req_dma_addr);
3122 	pkt.data_max_size = cpu_to_le32(size);
3123 	pkt.nonce = cpu_to_le32(nonce);
3124 
3125 	rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
3126 					timeout, NULL);
3127 	if (rc) {
3128 		dev_err(hdev->dev,
3129 			"Failed to handle CPU-CP pkt %u, error %d\n", packet_id, rc);
3130 		goto out;
3131 	}
3132 
3133 	memcpy(data, req_cpu_addr, size);
3134 
3135 out:
3136 	hl_cpu_accessible_dma_pool_free(hdev, size, req_cpu_addr);
3137 
3138 	return rc;
3139 }
3140 
3141 int hl_fw_get_sec_attest_info(struct hl_device *hdev, struct cpucp_sec_attest_info *sec_attest_info,
3142 				u32 nonce)
3143 {
3144 	return hl_fw_get_sec_attest_data(hdev, CPUCP_PACKET_SEC_ATTEST_GET, sec_attest_info,
3145 					sizeof(struct cpucp_sec_attest_info), nonce,
3146 					HL_CPUCP_SEC_ATTEST_INFO_TINEOUT_USEC);
3147 }
3148 
3149 int hl_fw_send_generic_request(struct hl_device *hdev, enum hl_passthrough_type sub_opcode,
3150 						dma_addr_t buff, u32 *size)
3151 {
3152 	struct cpucp_packet pkt = {0};
3153 	u64 result;
3154 	int rc = 0;
3155 
3156 	pkt.ctl = cpu_to_le32(CPUCP_PACKET_GENERIC_PASSTHROUGH << CPUCP_PKT_CTL_OPCODE_SHIFT);
3157 	pkt.addr = cpu_to_le64(buff);
3158 	pkt.data_max_size = cpu_to_le32(*size);
3159 	pkt.pkt_subidx = cpu_to_le32(sub_opcode);
3160 
3161 	rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *)&pkt, sizeof(pkt),
3162 						HL_CPUCP_INFO_TIMEOUT_USEC, &result);
3163 	if (rc)
3164 		dev_err(hdev->dev, "failed to send CPUCP data of generic fw pkt\n");
3165 	else
3166 		dev_dbg(hdev->dev, "generic pkt was successful, result: 0x%llx\n", result);
3167 
3168 	*size = (u32)result;
3169 
3170 	return rc;
3171 }
3172