1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright 2016-2022 HabanaLabs, Ltd.
8 #include "habanalabs.h"
9 #include "../include/common/hl_boot_if.h"
11 #include <linux/firmware.h>
12 #include <linux/crc32.h>
13 #include <linux/slab.h>
14 #include <linux/ctype.h>
16 #define FW_FILE_MAX_SIZE 0x1400000 /* maximum size of 20MB */
18 static char *extract_fw_ver_from_str(const char *fw_str)
20 char *str, *fw_ver, *whitespace;
22 fw_ver = kmalloc(16, GFP_KERNEL);
26 str = strnstr(fw_str, "fw-", VERSION_MAX_LEN);
30 /* Skip the fw- part */
33 /* Copy until the next whitespace */
34 whitespace = strnstr(str, " ", 15);
38 strscpy(fw_ver, str, whitespace - str + 1);
47 static int hl_request_fw(struct hl_device *hdev,
48 const struct firmware **firmware_p,
54 rc = request_firmware(firmware_p, fw_name, hdev->dev);
56 dev_err(hdev->dev, "Firmware file %s is not found! (error %d)\n",
61 fw_size = (*firmware_p)->size;
62 if ((fw_size % 4) != 0) {
63 dev_err(hdev->dev, "Illegal %s firmware size %zu\n",
69 dev_dbg(hdev->dev, "%s firmware size == %zu\n", fw_name, fw_size);
71 if (fw_size > FW_FILE_MAX_SIZE) {
73 "FW file size %zu exceeds maximum of %u bytes\n",
74 fw_size, FW_FILE_MAX_SIZE);
82 release_firmware(*firmware_p);
88 * hl_release_firmware() - release FW
92 * note: this inline function added to serve as a comprehensive mirror for the
93 * hl_request_fw function.
95 static inline void hl_release_firmware(const struct firmware *fw)
101 * hl_fw_copy_fw_to_device() - copy FW to device
103 * @hdev: pointer to hl_device structure.
105 * @dst: IO memory mapped address space to copy firmware to
106 * @src_offset: offset in src FW to copy from
107 * @size: amount of bytes to copy (0 to copy the whole binary)
109 * actual copy of FW binary data to device, shared by static and dynamic loaders
111 static int hl_fw_copy_fw_to_device(struct hl_device *hdev,
112 const struct firmware *fw, void __iomem *dst,
113 u32 src_offset, u32 size)
117 /* size 0 indicates to copy the whole file */
121 if (src_offset + size > fw->size) {
123 "size to copy(%u) and offset(%u) are invalid\n",
128 fw_data = (const void *) fw->data;
130 memcpy_toio(dst, fw_data + src_offset, size);
135 * hl_fw_copy_msg_to_device() - copy message to device
137 * @hdev: pointer to hl_device structure.
139 * @dst: IO memory mapped address space to copy firmware to
140 * @src_offset: offset in src message to copy from
141 * @size: amount of bytes to copy (0 to copy the whole binary)
143 * actual copy of message data to device.
145 static int hl_fw_copy_msg_to_device(struct hl_device *hdev,
146 struct lkd_msg_comms *msg, void __iomem *dst,
147 u32 src_offset, u32 size)
151 /* size 0 indicates to copy the whole file */
153 size = sizeof(struct lkd_msg_comms);
155 if (src_offset + size > sizeof(struct lkd_msg_comms)) {
157 "size to copy(%u) and offset(%u) are invalid\n",
162 msg_data = (void *) msg;
164 memcpy_toio(dst, msg_data + src_offset, size);
170 * hl_fw_load_fw_to_device() - Load F/W code to device's memory.
172 * @hdev: pointer to hl_device structure.
173 * @fw_name: the firmware image name
174 * @dst: IO memory mapped address space to copy firmware to
175 * @src_offset: offset in src FW to copy from
176 * @size: amount of bytes to copy (0 to copy the whole binary)
178 * Copy fw code from firmware file to device memory.
180 * Return: 0 on success, non-zero for failure.
182 int hl_fw_load_fw_to_device(struct hl_device *hdev, const char *fw_name,
183 void __iomem *dst, u32 src_offset, u32 size)
185 const struct firmware *fw;
188 rc = hl_request_fw(hdev, &fw, fw_name);
192 rc = hl_fw_copy_fw_to_device(hdev, fw, dst, src_offset, size);
194 hl_release_firmware(fw);
198 int hl_fw_send_pci_access_msg(struct hl_device *hdev, u32 opcode)
200 struct cpucp_packet pkt = {};
202 pkt.ctl = cpu_to_le32(opcode << CPUCP_PKT_CTL_OPCODE_SHIFT);
204 return hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt,
205 sizeof(pkt), 0, NULL);
208 int hl_fw_send_cpu_message(struct hl_device *hdev, u32 hw_queue_id, u32 *msg,
209 u16 len, u32 timeout, u64 *result)
211 struct hl_hw_queue *queue = &hdev->kernel_queues[hw_queue_id];
212 struct asic_fixed_properties *prop = &hdev->asic_prop;
213 struct cpucp_packet *pkt;
214 dma_addr_t pkt_dma_addr;
215 struct hl_bd *sent_bd;
216 u32 tmp, expected_ack_val, pi;
219 pkt = hdev->asic_funcs->cpu_accessible_dma_pool_alloc(hdev, len,
223 "Failed to allocate DMA memory for packet to CPU\n");
227 memcpy(pkt, msg, len);
229 mutex_lock(&hdev->send_cpu_message_lock);
231 /* CPU-CP messages can be sent during soft-reset */
232 if (hdev->disabled && !hdev->reset_info.is_in_soft_reset) {
237 if (hdev->device_cpu_disabled) {
242 /* set fence to a non valid value */
243 pkt->fence = cpu_to_le32(UINT_MAX);
247 * The CPU queue is a synchronous queue with an effective depth of
248 * a single entry (although it is allocated with room for multiple
249 * entries). We lock on it using 'send_cpu_message_lock' which
250 * serializes accesses to the CPU queue.
251 * Which means that we don't need to lock the access to the entire H/W
252 * queues module when submitting a JOB to the CPU queue.
254 hl_hw_queue_submit_bd(hdev, queue, hl_queue_inc_ptr(queue->pi), len, pkt_dma_addr);
256 if (prop->fw_app_cpu_boot_dev_sts0 & CPU_BOOT_DEV_STS0_PKT_PI_ACK_EN)
257 expected_ack_val = queue->pi;
259 expected_ack_val = CPUCP_PACKET_FENCE_VAL;
261 rc = hl_poll_timeout_memory(hdev, &pkt->fence, tmp,
262 (tmp == expected_ack_val), 1000,
265 hl_hw_queue_inc_ci_kernel(hdev, hw_queue_id);
267 if (rc == -ETIMEDOUT) {
268 dev_err(hdev->dev, "Device CPU packet timeout (0x%x)\n", tmp);
269 hdev->device_cpu_disabled = true;
273 tmp = le32_to_cpu(pkt->ctl);
275 rc = (tmp & CPUCP_PKT_CTL_RC_MASK) >> CPUCP_PKT_CTL_RC_SHIFT;
277 dev_err(hdev->dev, "F/W ERROR %d for CPU packet %d\n",
279 (tmp & CPUCP_PKT_CTL_OPCODE_MASK)
280 >> CPUCP_PKT_CTL_OPCODE_SHIFT);
283 *result = le64_to_cpu(pkt->result);
286 /* Scrub previous buffer descriptor 'ctl' field which contains the
287 * previous PI value written during packet submission.
288 * We must do this or else F/W can read an old value upon queue wraparound.
290 sent_bd = queue->kernel_address;
291 sent_bd += hl_pi_2_offset(pi);
292 sent_bd->ctl = cpu_to_le32(UINT_MAX);
295 mutex_unlock(&hdev->send_cpu_message_lock);
297 hdev->asic_funcs->cpu_accessible_dma_pool_free(hdev, len, pkt);
302 int hl_fw_unmask_irq(struct hl_device *hdev, u16 event_type)
304 struct cpucp_packet pkt;
308 memset(&pkt, 0, sizeof(pkt));
310 pkt.ctl = cpu_to_le32(CPUCP_PACKET_UNMASK_RAZWI_IRQ <<
311 CPUCP_PKT_CTL_OPCODE_SHIFT);
312 pkt.value = cpu_to_le64(event_type);
314 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
318 dev_err(hdev->dev, "failed to unmask RAZWI IRQ %d", event_type);
323 int hl_fw_unmask_irq_arr(struct hl_device *hdev, const u32 *irq_arr,
326 struct cpucp_unmask_irq_arr_packet *pkt;
327 size_t total_pkt_size;
331 total_pkt_size = sizeof(struct cpucp_unmask_irq_arr_packet) +
334 /* data should be aligned to 8 bytes in order to CPU-CP to copy it */
335 total_pkt_size = (total_pkt_size + 0x7) & ~0x7;
337 /* total_pkt_size is casted to u16 later on */
338 if (total_pkt_size > USHRT_MAX) {
339 dev_err(hdev->dev, "too many elements in IRQ array\n");
343 pkt = kzalloc(total_pkt_size, GFP_KERNEL);
347 pkt->length = cpu_to_le32(irq_arr_size / sizeof(irq_arr[0]));
348 memcpy(&pkt->irqs, irq_arr, irq_arr_size);
350 pkt->cpucp_pkt.ctl = cpu_to_le32(CPUCP_PACKET_UNMASK_RAZWI_IRQ_ARRAY <<
351 CPUCP_PKT_CTL_OPCODE_SHIFT);
353 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) pkt,
354 total_pkt_size, 0, &result);
357 dev_err(hdev->dev, "failed to unmask IRQ array\n");
364 int hl_fw_test_cpu_queue(struct hl_device *hdev)
366 struct cpucp_packet test_pkt = {};
370 test_pkt.ctl = cpu_to_le32(CPUCP_PACKET_TEST <<
371 CPUCP_PKT_CTL_OPCODE_SHIFT);
372 test_pkt.value = cpu_to_le64(CPUCP_PACKET_FENCE_VAL);
374 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &test_pkt,
375 sizeof(test_pkt), 0, &result);
378 if (result != CPUCP_PACKET_FENCE_VAL)
380 "CPU queue test failed (%#08llx)\n", result);
382 dev_err(hdev->dev, "CPU queue test failed, error %d\n", rc);
388 void *hl_fw_cpu_accessible_dma_pool_alloc(struct hl_device *hdev, size_t size,
389 dma_addr_t *dma_handle)
393 kernel_addr = gen_pool_alloc(hdev->cpu_accessible_dma_pool, size);
395 *dma_handle = hdev->cpu_accessible_dma_address +
396 (kernel_addr - (u64) (uintptr_t) hdev->cpu_accessible_dma_mem);
398 return (void *) (uintptr_t) kernel_addr;
401 void hl_fw_cpu_accessible_dma_pool_free(struct hl_device *hdev, size_t size,
404 gen_pool_free(hdev->cpu_accessible_dma_pool, (u64) (uintptr_t) vaddr,
408 int hl_fw_send_heartbeat(struct hl_device *hdev)
410 struct cpucp_packet hb_pkt;
414 memset(&hb_pkt, 0, sizeof(hb_pkt));
415 hb_pkt.ctl = cpu_to_le32(CPUCP_PACKET_TEST <<
416 CPUCP_PKT_CTL_OPCODE_SHIFT);
417 hb_pkt.value = cpu_to_le64(CPUCP_PACKET_FENCE_VAL);
419 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &hb_pkt,
420 sizeof(hb_pkt), 0, &result);
422 if ((rc) || (result != CPUCP_PACKET_FENCE_VAL))
425 if (le32_to_cpu(hb_pkt.status_mask) &
426 CPUCP_PKT_HB_STATUS_EQ_FAULT_MASK) {
427 dev_warn(hdev->dev, "FW reported EQ fault during heartbeat\n");
434 static bool fw_report_boot_dev0(struct hl_device *hdev, u32 err_val,
437 bool err_exists = false;
439 if (!(err_val & CPU_BOOT_ERR0_ENABLED))
442 if (err_val & CPU_BOOT_ERR0_DRAM_INIT_FAIL) {
444 "Device boot error - DRAM initialization failed\n");
448 if (err_val & CPU_BOOT_ERR0_FIT_CORRUPTED) {
449 dev_err(hdev->dev, "Device boot error - FIT image corrupted\n");
453 if (err_val & CPU_BOOT_ERR0_TS_INIT_FAIL) {
455 "Device boot error - Thermal Sensor initialization failed\n");
459 if (err_val & CPU_BOOT_ERR0_BMC_WAIT_SKIPPED) {
460 if (hdev->bmc_enable) {
462 "Device boot error - Skipped waiting for BMC\n");
466 "Device boot message - Skipped waiting for BMC\n");
467 /* This is an info so we don't want it to disable the
470 err_val &= ~CPU_BOOT_ERR0_BMC_WAIT_SKIPPED;
474 if (err_val & CPU_BOOT_ERR0_NIC_DATA_NOT_RDY) {
476 "Device boot error - Serdes data from BMC not available\n");
480 if (err_val & CPU_BOOT_ERR0_NIC_FW_FAIL) {
482 "Device boot error - NIC F/W initialization failed\n");
486 if (err_val & CPU_BOOT_ERR0_SECURITY_NOT_RDY) {
488 "Device boot warning - security not ready\n");
492 if (err_val & CPU_BOOT_ERR0_SECURITY_FAIL) {
493 dev_err(hdev->dev, "Device boot error - security failure\n");
497 if (err_val & CPU_BOOT_ERR0_EFUSE_FAIL) {
498 dev_err(hdev->dev, "Device boot error - eFuse failure\n");
502 if (err_val & CPU_BOOT_ERR0_SEC_IMG_VER_FAIL) {
503 dev_err(hdev->dev, "Device boot error - Failed to load preboot secondary image\n");
507 if (err_val & CPU_BOOT_ERR0_PLL_FAIL) {
508 dev_err(hdev->dev, "Device boot error - PLL failure\n");
512 if (err_val & CPU_BOOT_ERR0_DEVICE_UNUSABLE_FAIL) {
513 /* Ignore this bit, don't prevent driver loading */
514 dev_dbg(hdev->dev, "device unusable status is set\n");
515 err_val &= ~CPU_BOOT_ERR0_DEVICE_UNUSABLE_FAIL;
518 if (sts_val & CPU_BOOT_DEV_STS0_ENABLED)
519 dev_dbg(hdev->dev, "Device status0 %#x\n", sts_val);
521 /* All warnings should go here in order not to reach the unknown error validation */
522 if (err_val & CPU_BOOT_ERR0_DRAM_SKIPPED) {
524 "Device boot warning - Skipped DRAM initialization\n");
525 /* This is a warning so we don't want it to disable the
528 err_val &= ~CPU_BOOT_ERR0_DRAM_SKIPPED;
531 if (err_val & CPU_BOOT_ERR0_PRI_IMG_VER_FAIL) {
533 "Device boot warning - Failed to load preboot primary image\n");
534 /* This is a warning so we don't want it to disable the
535 * device as we have a secondary preboot image
537 err_val &= ~CPU_BOOT_ERR0_PRI_IMG_VER_FAIL;
540 if (err_val & CPU_BOOT_ERR0_TPM_FAIL) {
542 "Device boot warning - TPM failure\n");
543 /* This is a warning so we don't want it to disable the
546 err_val &= ~CPU_BOOT_ERR0_TPM_FAIL;
549 if (!err_exists && (err_val & ~CPU_BOOT_ERR0_ENABLED)) {
551 "Device boot error - unknown ERR0 error 0x%08x\n", err_val);
555 /* return error only if it's in the predefined mask */
556 if (err_exists && ((err_val & ~CPU_BOOT_ERR0_ENABLED) &
557 lower_32_bits(hdev->boot_error_status_mask)))
563 /* placeholder for ERR1 as no errors defined there yet */
564 static bool fw_report_boot_dev1(struct hl_device *hdev, u32 err_val,
568 * keep this variable to preserve the logic of the function.
569 * this way it would require less modifications when error will be
572 bool err_exists = false;
574 if (!(err_val & CPU_BOOT_ERR1_ENABLED))
577 if (sts_val & CPU_BOOT_DEV_STS1_ENABLED)
578 dev_dbg(hdev->dev, "Device status1 %#x\n", sts_val);
580 if (!err_exists && (err_val & ~CPU_BOOT_ERR1_ENABLED)) {
582 "Device boot error - unknown ERR1 error 0x%08x\n",
587 /* return error only if it's in the predefined mask */
588 if (err_exists && ((err_val & ~CPU_BOOT_ERR1_ENABLED) &
589 upper_32_bits(hdev->boot_error_status_mask)))
595 static int fw_read_errors(struct hl_device *hdev, u32 boot_err0_reg,
596 u32 boot_err1_reg, u32 cpu_boot_dev_status0_reg,
597 u32 cpu_boot_dev_status1_reg)
599 u32 err_val, status_val;
600 bool err_exists = false;
602 /* Some of the firmware status codes are deprecated in newer f/w
603 * versions. In those versions, the errors are reported
604 * in different registers. Therefore, we need to check those
605 * registers and print the exact errors. Moreover, there
606 * may be multiple errors, so we need to report on each error
607 * separately. Some of the error codes might indicate a state
608 * that is not an error per-se, but it is an error in production
611 err_val = RREG32(boot_err0_reg);
612 status_val = RREG32(cpu_boot_dev_status0_reg);
613 err_exists = fw_report_boot_dev0(hdev, err_val, status_val);
615 err_val = RREG32(boot_err1_reg);
616 status_val = RREG32(cpu_boot_dev_status1_reg);
617 err_exists |= fw_report_boot_dev1(hdev, err_val, status_val);
625 int hl_fw_cpucp_info_get(struct hl_device *hdev,
626 u32 sts_boot_dev_sts0_reg,
627 u32 sts_boot_dev_sts1_reg, u32 boot_err0_reg,
630 struct asic_fixed_properties *prop = &hdev->asic_prop;
631 struct cpucp_packet pkt = {};
632 dma_addr_t cpucp_info_dma_addr;
633 void *cpucp_info_cpu_addr;
638 cpucp_info_cpu_addr =
639 hdev->asic_funcs->cpu_accessible_dma_pool_alloc(hdev,
640 sizeof(struct cpucp_info),
641 &cpucp_info_dma_addr);
642 if (!cpucp_info_cpu_addr) {
644 "Failed to allocate DMA memory for CPU-CP info packet\n");
648 memset(cpucp_info_cpu_addr, 0, sizeof(struct cpucp_info));
650 pkt.ctl = cpu_to_le32(CPUCP_PACKET_INFO_GET <<
651 CPUCP_PKT_CTL_OPCODE_SHIFT);
652 pkt.addr = cpu_to_le64(cpucp_info_dma_addr);
653 pkt.data_max_size = cpu_to_le32(sizeof(struct cpucp_info));
655 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
656 HL_CPUCP_INFO_TIMEOUT_USEC, &result);
659 "Failed to handle CPU-CP info pkt, error %d\n", rc);
663 rc = fw_read_errors(hdev, boot_err0_reg, boot_err1_reg,
664 sts_boot_dev_sts0_reg, sts_boot_dev_sts1_reg);
666 dev_err(hdev->dev, "Errors in device boot\n");
670 memcpy(&prop->cpucp_info, cpucp_info_cpu_addr,
671 sizeof(prop->cpucp_info));
673 rc = hl_build_hwmon_channel_info(hdev, prop->cpucp_info.sensors);
676 "Failed to build hwmon channel info, error %d\n", rc);
681 kernel_ver = extract_fw_ver_from_str(prop->cpucp_info.kernel_version);
683 dev_info(hdev->dev, "Linux version %s", kernel_ver);
687 /* assume EQ code doesn't need to check eqe index */
688 hdev->event_queue.check_eqe_index = false;
690 /* Read FW application security bits again */
691 if (prop->fw_cpu_boot_dev_sts0_valid) {
692 prop->fw_app_cpu_boot_dev_sts0 = RREG32(sts_boot_dev_sts0_reg);
693 if (prop->fw_app_cpu_boot_dev_sts0 &
694 CPU_BOOT_DEV_STS0_EQ_INDEX_EN)
695 hdev->event_queue.check_eqe_index = true;
698 if (prop->fw_cpu_boot_dev_sts1_valid)
699 prop->fw_app_cpu_boot_dev_sts1 = RREG32(sts_boot_dev_sts1_reg);
702 hdev->asic_funcs->cpu_accessible_dma_pool_free(hdev,
703 sizeof(struct cpucp_info), cpucp_info_cpu_addr);
708 static int hl_fw_send_msi_info_msg(struct hl_device *hdev)
710 struct cpucp_array_data_packet *pkt;
711 size_t total_pkt_size, data_size;
715 /* skip sending this info for unsupported ASICs */
716 if (!hdev->asic_funcs->get_msi_info)
719 data_size = CPUCP_NUM_OF_MSI_TYPES * sizeof(u32);
720 total_pkt_size = sizeof(struct cpucp_array_data_packet) + data_size;
722 /* data should be aligned to 8 bytes in order to CPU-CP to copy it */
723 total_pkt_size = (total_pkt_size + 0x7) & ~0x7;
725 /* total_pkt_size is casted to u16 later on */
726 if (total_pkt_size > USHRT_MAX) {
727 dev_err(hdev->dev, "CPUCP array data is too big\n");
731 pkt = kzalloc(total_pkt_size, GFP_KERNEL);
735 pkt->length = cpu_to_le32(CPUCP_NUM_OF_MSI_TYPES);
737 memset((void *) &pkt->data, 0xFF, data_size);
738 hdev->asic_funcs->get_msi_info(pkt->data);
740 pkt->cpucp_pkt.ctl = cpu_to_le32(CPUCP_PACKET_MSI_INFO_SET <<
741 CPUCP_PKT_CTL_OPCODE_SHIFT);
743 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *)pkt,
744 total_pkt_size, 0, &result);
747 * in case packet result is invalid it means that FW does not support
748 * this feature and will use default/hard coded MSI values. no reason
751 if (rc && result == cpucp_packet_invalid)
755 dev_err(hdev->dev, "failed to send CPUCP array data\n");
762 int hl_fw_cpucp_handshake(struct hl_device *hdev,
763 u32 sts_boot_dev_sts0_reg,
764 u32 sts_boot_dev_sts1_reg, u32 boot_err0_reg,
769 rc = hl_fw_cpucp_info_get(hdev, sts_boot_dev_sts0_reg,
770 sts_boot_dev_sts1_reg, boot_err0_reg,
775 return hl_fw_send_msi_info_msg(hdev);
778 int hl_fw_get_eeprom_data(struct hl_device *hdev, void *data, size_t max_size)
780 struct cpucp_packet pkt = {};
781 void *eeprom_info_cpu_addr;
782 dma_addr_t eeprom_info_dma_addr;
786 eeprom_info_cpu_addr =
787 hdev->asic_funcs->cpu_accessible_dma_pool_alloc(hdev,
788 max_size, &eeprom_info_dma_addr);
789 if (!eeprom_info_cpu_addr) {
791 "Failed to allocate DMA memory for CPU-CP EEPROM packet\n");
795 memset(eeprom_info_cpu_addr, 0, max_size);
797 pkt.ctl = cpu_to_le32(CPUCP_PACKET_EEPROM_DATA_GET <<
798 CPUCP_PKT_CTL_OPCODE_SHIFT);
799 pkt.addr = cpu_to_le64(eeprom_info_dma_addr);
800 pkt.data_max_size = cpu_to_le32(max_size);
802 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
803 HL_CPUCP_EEPROM_TIMEOUT_USEC, &result);
807 "Failed to handle CPU-CP EEPROM packet, error %d\n",
812 /* result contains the actual size */
813 memcpy(data, eeprom_info_cpu_addr, min((size_t)result, max_size));
816 hdev->asic_funcs->cpu_accessible_dma_pool_free(hdev, max_size,
817 eeprom_info_cpu_addr);
822 int hl_fw_cpucp_pci_counters_get(struct hl_device *hdev,
823 struct hl_info_pci_counters *counters)
825 struct cpucp_packet pkt = {};
829 pkt.ctl = cpu_to_le32(CPUCP_PACKET_PCIE_THROUGHPUT_GET <<
830 CPUCP_PKT_CTL_OPCODE_SHIFT);
832 /* Fetch PCI rx counter */
833 pkt.index = cpu_to_le32(cpucp_pcie_throughput_rx);
834 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
835 HL_CPUCP_INFO_TIMEOUT_USEC, &result);
838 "Failed to handle CPU-CP PCI info pkt, error %d\n", rc);
841 counters->rx_throughput = result;
843 memset(&pkt, 0, sizeof(pkt));
844 pkt.ctl = cpu_to_le32(CPUCP_PACKET_PCIE_THROUGHPUT_GET <<
845 CPUCP_PKT_CTL_OPCODE_SHIFT);
847 /* Fetch PCI tx counter */
848 pkt.index = cpu_to_le32(cpucp_pcie_throughput_tx);
849 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
850 HL_CPUCP_INFO_TIMEOUT_USEC, &result);
853 "Failed to handle CPU-CP PCI info pkt, error %d\n", rc);
856 counters->tx_throughput = result;
858 /* Fetch PCI replay counter */
859 memset(&pkt, 0, sizeof(pkt));
860 pkt.ctl = cpu_to_le32(CPUCP_PACKET_PCIE_REPLAY_CNT_GET <<
861 CPUCP_PKT_CTL_OPCODE_SHIFT);
863 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
864 HL_CPUCP_INFO_TIMEOUT_USEC, &result);
867 "Failed to handle CPU-CP PCI info pkt, error %d\n", rc);
870 counters->replay_cnt = (u32) result;
875 int hl_fw_cpucp_total_energy_get(struct hl_device *hdev, u64 *total_energy)
877 struct cpucp_packet pkt = {};
881 pkt.ctl = cpu_to_le32(CPUCP_PACKET_TOTAL_ENERGY_GET <<
882 CPUCP_PKT_CTL_OPCODE_SHIFT);
884 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
885 HL_CPUCP_INFO_TIMEOUT_USEC, &result);
888 "Failed to handle CpuCP total energy pkt, error %d\n",
893 *total_energy = result;
898 int get_used_pll_index(struct hl_device *hdev, u32 input_pll_index,
899 enum pll_index *pll_index)
901 struct asic_fixed_properties *prop = &hdev->asic_prop;
902 u8 pll_byte, pll_bit_off;
906 dynamic_pll = !!(prop->fw_app_cpu_boot_dev_sts0 &
907 CPU_BOOT_DEV_STS0_DYN_PLL_EN);
911 * in case we are working with legacy FW (each asic has unique
912 * PLL numbering) use the driver based index as they are
913 * aligned with fw legacy numbering
915 *pll_index = input_pll_index;
919 /* retrieve a FW compatible PLL index based on
920 * ASIC specific user request
922 fw_pll_idx = hdev->asic_funcs->map_pll_idx_to_fw_idx(input_pll_index);
923 if (fw_pll_idx < 0) {
924 dev_err(hdev->dev, "Invalid PLL index (%u) error %d\n",
925 input_pll_index, fw_pll_idx);
929 /* PLL map is a u8 array */
930 pll_byte = prop->cpucp_info.pll_map[fw_pll_idx >> 3];
931 pll_bit_off = fw_pll_idx & 0x7;
933 if (!(pll_byte & BIT(pll_bit_off))) {
934 dev_err(hdev->dev, "PLL index %d is not supported\n",
939 *pll_index = fw_pll_idx;
944 int hl_fw_cpucp_pll_info_get(struct hl_device *hdev, u32 pll_index,
947 struct cpucp_packet pkt;
948 enum pll_index used_pll_idx;
952 rc = get_used_pll_index(hdev, pll_index, &used_pll_idx);
956 memset(&pkt, 0, sizeof(pkt));
958 pkt.ctl = cpu_to_le32(CPUCP_PACKET_PLL_INFO_GET <<
959 CPUCP_PKT_CTL_OPCODE_SHIFT);
960 pkt.pll_type = __cpu_to_le16((u16)used_pll_idx);
962 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
963 HL_CPUCP_INFO_TIMEOUT_USEC, &result);
965 dev_err(hdev->dev, "Failed to read PLL info, error %d\n", rc);
969 pll_freq_arr[0] = FIELD_GET(CPUCP_PKT_RES_PLL_OUT0_MASK, result);
970 pll_freq_arr[1] = FIELD_GET(CPUCP_PKT_RES_PLL_OUT1_MASK, result);
971 pll_freq_arr[2] = FIELD_GET(CPUCP_PKT_RES_PLL_OUT2_MASK, result);
972 pll_freq_arr[3] = FIELD_GET(CPUCP_PKT_RES_PLL_OUT3_MASK, result);
977 int hl_fw_cpucp_power_get(struct hl_device *hdev, u64 *power)
979 struct cpucp_packet pkt;
983 memset(&pkt, 0, sizeof(pkt));
985 pkt.ctl = cpu_to_le32(CPUCP_PACKET_POWER_GET <<
986 CPUCP_PKT_CTL_OPCODE_SHIFT);
987 pkt.type = cpu_to_le16(CPUCP_POWER_INPUT);
989 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
990 HL_CPUCP_INFO_TIMEOUT_USEC, &result);
992 dev_err(hdev->dev, "Failed to read power, error %d\n", rc);
1001 int hl_fw_dram_replaced_row_get(struct hl_device *hdev,
1002 struct cpucp_hbm_row_info *info)
1004 struct cpucp_hbm_row_info *cpucp_repl_rows_info_cpu_addr;
1005 dma_addr_t cpucp_repl_rows_info_dma_addr;
1006 struct cpucp_packet pkt = {};
1010 cpucp_repl_rows_info_cpu_addr =
1011 hdev->asic_funcs->cpu_accessible_dma_pool_alloc(hdev,
1012 sizeof(struct cpucp_hbm_row_info),
1013 &cpucp_repl_rows_info_dma_addr);
1014 if (!cpucp_repl_rows_info_cpu_addr) {
1016 "Failed to allocate DMA memory for CPU-CP replaced rows info packet\n");
1020 memset(cpucp_repl_rows_info_cpu_addr, 0, sizeof(struct cpucp_hbm_row_info));
1022 pkt.ctl = cpu_to_le32(CPUCP_PACKET_HBM_REPLACED_ROWS_INFO_GET <<
1023 CPUCP_PKT_CTL_OPCODE_SHIFT);
1024 pkt.addr = cpu_to_le64(cpucp_repl_rows_info_dma_addr);
1025 pkt.data_max_size = cpu_to_le32(sizeof(struct cpucp_hbm_row_info));
1027 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
1028 HL_CPUCP_INFO_TIMEOUT_USEC, &result);
1031 "Failed to handle CPU-CP replaced rows info pkt, error %d\n", rc);
1035 memcpy(info, cpucp_repl_rows_info_cpu_addr, sizeof(*info));
1038 hdev->asic_funcs->cpu_accessible_dma_pool_free(hdev,
1039 sizeof(struct cpucp_hbm_row_info),
1040 cpucp_repl_rows_info_cpu_addr);
1045 int hl_fw_dram_pending_row_get(struct hl_device *hdev, u32 *pend_rows_num)
1047 struct cpucp_packet pkt;
1051 memset(&pkt, 0, sizeof(pkt));
1053 pkt.ctl = cpu_to_le32(CPUCP_PACKET_HBM_PENDING_ROWS_STATUS << CPUCP_PKT_CTL_OPCODE_SHIFT);
1055 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt), 0, &result);
1058 "Failed to handle CPU-CP pending rows info pkt, error %d\n", rc);
1062 *pend_rows_num = (u32) result;
1067 int hl_fw_cpucp_engine_core_asid_set(struct hl_device *hdev, u32 asid)
1069 struct cpucp_packet pkt;
1072 memset(&pkt, 0, sizeof(pkt));
1074 pkt.ctl = cpu_to_le32(CPUCP_PACKET_ENGINE_CORE_ASID_SET << CPUCP_PKT_CTL_OPCODE_SHIFT);
1075 pkt.value = cpu_to_le64(asid);
1077 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
1078 HL_CPUCP_INFO_TIMEOUT_USEC, NULL);
1081 "Failed on ASID configuration request for engine core, error %d\n",
1087 void hl_fw_ask_hard_reset_without_linux(struct hl_device *hdev)
1089 struct static_fw_load_mgr *static_loader =
1090 &hdev->fw_loader.static_loader;
1093 if (hdev->asic_prop.dynamic_fw_load) {
1094 rc = hl_fw_dynamic_send_protocol_cmd(hdev, &hdev->fw_loader,
1095 COMMS_RST_DEV, 0, false,
1096 hdev->fw_loader.cpu_timeout);
1098 dev_warn(hdev->dev, "Failed sending COMMS_RST_DEV\n");
1100 WREG32(static_loader->kmd_msg_to_cpu_reg, KMD_MSG_RST_DEV);
1104 void hl_fw_ask_halt_machine_without_linux(struct hl_device *hdev)
1106 struct static_fw_load_mgr *static_loader =
1107 &hdev->fw_loader.static_loader;
1110 if (hdev->device_cpu_is_halted)
1113 /* Stop device CPU to make sure nothing bad happens */
1114 if (hdev->asic_prop.dynamic_fw_load) {
1115 rc = hl_fw_dynamic_send_protocol_cmd(hdev, &hdev->fw_loader,
1116 COMMS_GOTO_WFE, 0, true,
1117 hdev->fw_loader.cpu_timeout);
1119 dev_warn(hdev->dev, "Failed sending COMMS_GOTO_WFE\n");
1121 WREG32(static_loader->kmd_msg_to_cpu_reg, KMD_MSG_GOTO_WFE);
1122 msleep(static_loader->cpu_reset_wait_msec);
1124 /* Must clear this register in order to prevent preboot
1125 * from reading WFE after reboot
1127 WREG32(static_loader->kmd_msg_to_cpu_reg, KMD_MSG_NA);
1130 hdev->device_cpu_is_halted = true;
1133 static void detect_cpu_boot_status(struct hl_device *hdev, u32 status)
1135 /* Some of the status codes below are deprecated in newer f/w
1136 * versions but we keep them here for backward compatibility
1139 case CPU_BOOT_STATUS_NA:
1141 "Device boot progress - BTL/ROM did NOT run\n");
1143 case CPU_BOOT_STATUS_IN_WFE:
1145 "Device boot progress - Stuck inside WFE loop\n");
1147 case CPU_BOOT_STATUS_IN_BTL:
1149 "Device boot progress - Stuck in BTL\n");
1151 case CPU_BOOT_STATUS_IN_PREBOOT:
1153 "Device boot progress - Stuck in Preboot\n");
1155 case CPU_BOOT_STATUS_IN_SPL:
1157 "Device boot progress - Stuck in SPL\n");
1159 case CPU_BOOT_STATUS_IN_UBOOT:
1161 "Device boot progress - Stuck in u-boot\n");
1163 case CPU_BOOT_STATUS_DRAM_INIT_FAIL:
1165 "Device boot progress - DRAM initialization failed\n");
1167 case CPU_BOOT_STATUS_UBOOT_NOT_READY:
1169 "Device boot progress - Cannot boot\n");
1171 case CPU_BOOT_STATUS_TS_INIT_FAIL:
1173 "Device boot progress - Thermal Sensor initialization failed\n");
1175 case CPU_BOOT_STATUS_SECURITY_READY:
1177 "Device boot progress - Stuck in preboot after security initialization\n");
1181 "Device boot progress - Invalid status code %d\n",
1187 static int hl_fw_read_preboot_caps(struct hl_device *hdev,
1188 u32 cpu_boot_status_reg,
1189 u32 sts_boot_dev_sts0_reg,
1190 u32 sts_boot_dev_sts1_reg,
1191 u32 boot_err0_reg, u32 boot_err1_reg,
1194 struct asic_fixed_properties *prop = &hdev->asic_prop;
1195 u32 status, reg_val;
1198 /* Need to check two possible scenarios:
1200 * CPU_BOOT_STATUS_WAITING_FOR_BOOT_FIT - for newer firmwares where
1201 * the preboot is waiting for the boot fit
1203 * All other status values - for older firmwares where the uboot was
1204 * loaded from the FLASH
1206 rc = hl_poll_timeout(
1208 cpu_boot_status_reg,
1210 (status == CPU_BOOT_STATUS_NIC_FW_RDY) ||
1211 (status == CPU_BOOT_STATUS_READY_TO_BOOT) ||
1212 (status == CPU_BOOT_STATUS_WAITING_FOR_BOOT_FIT),
1213 hdev->fw_poll_interval_usec,
1217 dev_err(hdev->dev, "CPU boot ready status timeout\n");
1218 detect_cpu_boot_status(hdev, status);
1220 /* If we read all FF, then something is totally wrong, no point
1221 * of reading specific errors
1224 fw_read_errors(hdev, boot_err0_reg, boot_err1_reg,
1225 sts_boot_dev_sts0_reg,
1226 sts_boot_dev_sts1_reg);
1231 * the registers DEV_STS* contain FW capabilities/features.
1232 * We can rely on this registers only if bit CPU_BOOT_DEV_STS*_ENABLED
1234 * In the first read of this register we store the value of this
1235 * register ONLY if the register is enabled (which will be propagated
1236 * to next stages) and also mark the register as valid.
1237 * In case it is not enabled the stored value will be left 0- all
1238 * caps/features are off
1240 reg_val = RREG32(sts_boot_dev_sts0_reg);
1241 if (reg_val & CPU_BOOT_DEV_STS0_ENABLED) {
1242 prop->fw_cpu_boot_dev_sts0_valid = true;
1243 prop->fw_preboot_cpu_boot_dev_sts0 = reg_val;
1246 reg_val = RREG32(sts_boot_dev_sts1_reg);
1247 if (reg_val & CPU_BOOT_DEV_STS1_ENABLED) {
1248 prop->fw_cpu_boot_dev_sts1_valid = true;
1249 prop->fw_preboot_cpu_boot_dev_sts1 = reg_val;
1252 prop->dynamic_fw_load = !!(prop->fw_preboot_cpu_boot_dev_sts0 &
1253 CPU_BOOT_DEV_STS0_FW_LD_COM_EN);
1255 /* initialize FW loader once we know what load protocol is used */
1256 hdev->asic_funcs->init_firmware_loader(hdev);
1258 dev_dbg(hdev->dev, "Attempting %s FW load\n",
1259 prop->dynamic_fw_load ? "dynamic" : "legacy");
1263 static int hl_fw_static_read_device_fw_version(struct hl_device *hdev,
1264 enum hl_fw_component fwc)
1266 struct asic_fixed_properties *prop = &hdev->asic_prop;
1267 struct fw_load_mgr *fw_loader = &hdev->fw_loader;
1268 struct static_fw_load_mgr *static_loader;
1269 char *dest, *boot_ver, *preboot_ver;
1274 static_loader = &hdev->fw_loader.static_loader;
1277 case FW_COMP_BOOT_FIT:
1278 ver_off = RREG32(static_loader->boot_fit_version_offset_reg);
1279 dest = prop->uboot_ver;
1281 limit = static_loader->boot_fit_version_max_off;
1283 case FW_COMP_PREBOOT:
1284 ver_off = RREG32(static_loader->preboot_version_offset_reg);
1285 dest = prop->preboot_ver;
1287 limit = static_loader->preboot_version_max_off;
1290 dev_warn(hdev->dev, "Undefined FW component: %d\n", fwc);
1294 ver_off &= static_loader->sram_offset_mask;
1296 if (ver_off < limit) {
1298 hdev->pcie_bar[fw_loader->sram_bar_id] + ver_off,
1301 dev_err(hdev->dev, "%s version offset (0x%x) is above SRAM\n",
1303 strscpy(dest, "unavailable", VERSION_MAX_LEN);
1307 if (fwc == FW_COMP_BOOT_FIT) {
1308 boot_ver = extract_fw_ver_from_str(prop->uboot_ver);
1310 dev_info(hdev->dev, "boot-fit version %s\n", boot_ver);
1313 } else if (fwc == FW_COMP_PREBOOT) {
1314 preboot_ver = strnstr(prop->preboot_ver, "Preboot",
1316 if (preboot_ver && preboot_ver != prop->preboot_ver) {
1317 strscpy(btl_ver, prop->preboot_ver,
1318 min((int) (preboot_ver - prop->preboot_ver),
1320 dev_info(hdev->dev, "%s\n", btl_ver);
1323 preboot_ver = extract_fw_ver_from_str(prop->preboot_ver);
1325 dev_info(hdev->dev, "preboot version %s\n",
1335 * hl_fw_preboot_update_state - update internal data structures during
1336 * handshake with preboot
1339 * @hdev: pointer to the habanalabs device structure
1341 * @return 0 on success, otherwise non-zero error code
1343 static void hl_fw_preboot_update_state(struct hl_device *hdev)
1345 struct asic_fixed_properties *prop = &hdev->asic_prop;
1346 u32 cpu_boot_dev_sts0, cpu_boot_dev_sts1;
1348 cpu_boot_dev_sts0 = prop->fw_preboot_cpu_boot_dev_sts0;
1349 cpu_boot_dev_sts1 = prop->fw_preboot_cpu_boot_dev_sts1;
1351 /* We read boot_dev_sts registers multiple times during boot:
1352 * 1. preboot - a. Check whether the security status bits are valid
1353 * b. Check whether fw security is enabled
1354 * c. Check whether hard reset is done by preboot
1355 * 2. boot cpu - a. Fetch boot cpu security status
1356 * b. Check whether hard reset is done by boot cpu
1357 * 3. FW application - a. Fetch fw application security status
1358 * b. Check whether hard reset is done by fw app
1360 prop->hard_reset_done_by_fw = !!(cpu_boot_dev_sts0 & CPU_BOOT_DEV_STS0_FW_HARD_RST_EN);
1362 dev_dbg(hdev->dev, "Firmware preboot boot device status0 %#x\n",
1365 dev_dbg(hdev->dev, "Firmware preboot boot device status1 %#x\n",
1368 dev_dbg(hdev->dev, "Firmware preboot hard-reset is %s\n",
1369 prop->hard_reset_done_by_fw ? "enabled" : "disabled");
1371 dev_dbg(hdev->dev, "firmware-level security is %s\n",
1372 prop->fw_security_enabled ? "enabled" : "disabled");
1374 dev_dbg(hdev->dev, "GIC controller is %s\n",
1375 prop->gic_interrupts_enable ? "enabled" : "disabled");
1378 static int hl_fw_static_read_preboot_status(struct hl_device *hdev)
1382 rc = hl_fw_static_read_device_fw_version(hdev, FW_COMP_PREBOOT);
1389 int hl_fw_read_preboot_status(struct hl_device *hdev, u32 cpu_boot_status_reg,
1390 u32 sts_boot_dev_sts0_reg,
1391 u32 sts_boot_dev_sts1_reg, u32 boot_err0_reg,
1392 u32 boot_err1_reg, u32 timeout)
1396 if (!(hdev->fw_components & FW_TYPE_PREBOOT_CPU))
1400 * In order to determine boot method (static VS dymanic) we need to
1401 * read the boot caps register
1403 rc = hl_fw_read_preboot_caps(hdev, cpu_boot_status_reg,
1404 sts_boot_dev_sts0_reg,
1405 sts_boot_dev_sts1_reg, boot_err0_reg,
1406 boot_err1_reg, timeout);
1410 hl_fw_preboot_update_state(hdev);
1412 /* no need to read preboot status in dynamic load */
1413 if (hdev->asic_prop.dynamic_fw_load)
1416 return hl_fw_static_read_preboot_status(hdev);
1419 /* associate string with COMM status */
1420 static char *hl_dynamic_fw_status_str[COMMS_STS_INVLD_LAST] = {
1421 [COMMS_STS_NOOP] = "NOOP",
1422 [COMMS_STS_ACK] = "ACK",
1423 [COMMS_STS_OK] = "OK",
1424 [COMMS_STS_ERR] = "ERR",
1425 [COMMS_STS_VALID_ERR] = "VALID_ERR",
1426 [COMMS_STS_TIMEOUT_ERR] = "TIMEOUT_ERR",
1430 * hl_fw_dynamic_report_error_status - report error status
1432 * @hdev: pointer to the habanalabs device structure
1433 * @status: value of FW status register
1434 * @expected_status: the expected status
1436 static void hl_fw_dynamic_report_error_status(struct hl_device *hdev,
1438 enum comms_sts expected_status)
1440 enum comms_sts comm_status =
1441 FIELD_GET(COMMS_STATUS_STATUS_MASK, status);
1443 if (comm_status < COMMS_STS_INVLD_LAST)
1444 dev_err(hdev->dev, "Device status %s, expected status: %s\n",
1445 hl_dynamic_fw_status_str[comm_status],
1446 hl_dynamic_fw_status_str[expected_status]);
1448 dev_err(hdev->dev, "Device status unknown %d, expected status: %s\n",
1450 hl_dynamic_fw_status_str[expected_status]);
1454 * hl_fw_dynamic_send_cmd - send LKD to FW cmd
1456 * @hdev: pointer to the habanalabs device structure
1457 * @fw_loader: managing structure for loading device's FW
1458 * @cmd: LKD to FW cmd code
1459 * @size: size of next FW component to be loaded (0 if not necessary)
1461 * LDK to FW exact command layout is defined at struct comms_command.
1462 * note: the size argument is used only when the next FW component should be
1463 * loaded, otherwise it shall be 0. the size is used by the FW in later
1464 * protocol stages and when sending only indicating the amount of memory
1465 * to be allocated by the FW to receive the next boot component.
1467 static void hl_fw_dynamic_send_cmd(struct hl_device *hdev,
1468 struct fw_load_mgr *fw_loader,
1469 enum comms_cmd cmd, unsigned int size)
1471 struct cpu_dyn_regs *dyn_regs;
1474 dyn_regs = &fw_loader->dynamic_loader.comm_desc.cpu_dyn_regs;
1476 val = FIELD_PREP(COMMS_COMMAND_CMD_MASK, cmd);
1477 val |= FIELD_PREP(COMMS_COMMAND_SIZE_MASK, size);
1479 WREG32(le32_to_cpu(dyn_regs->kmd_msg_to_cpu), val);
1483 * hl_fw_dynamic_extract_fw_response - update the FW response
1485 * @hdev: pointer to the habanalabs device structure
1486 * @fw_loader: managing structure for loading device's FW
1487 * @response: FW response
1488 * @status: the status read from CPU status register
1490 * @return 0 on success, otherwise non-zero error code
1492 static int hl_fw_dynamic_extract_fw_response(struct hl_device *hdev,
1493 struct fw_load_mgr *fw_loader,
1494 struct fw_response *response,
1497 response->status = FIELD_GET(COMMS_STATUS_STATUS_MASK, status);
1498 response->ram_offset = FIELD_GET(COMMS_STATUS_OFFSET_MASK, status) <<
1499 COMMS_STATUS_OFFSET_ALIGN_SHIFT;
1500 response->ram_type = FIELD_GET(COMMS_STATUS_RAM_TYPE_MASK, status);
1502 if ((response->ram_type != COMMS_SRAM) &&
1503 (response->ram_type != COMMS_DRAM)) {
1504 dev_err(hdev->dev, "FW status: invalid RAM type %u\n",
1505 response->ram_type);
1513 * hl_fw_dynamic_wait_for_status - wait for status in dynamic FW load
1515 * @hdev: pointer to the habanalabs device structure
1516 * @fw_loader: managing structure for loading device's FW
1517 * @expected_status: expected status to wait for
1518 * @timeout: timeout for status wait
1520 * @return 0 on success, otherwise non-zero error code
1522 * waiting for status from FW include polling the FW status register until
1523 * expected status is received or timeout occurs (whatever occurs first).
1525 static int hl_fw_dynamic_wait_for_status(struct hl_device *hdev,
1526 struct fw_load_mgr *fw_loader,
1527 enum comms_sts expected_status,
1530 struct cpu_dyn_regs *dyn_regs;
1534 dyn_regs = &fw_loader->dynamic_loader.comm_desc.cpu_dyn_regs;
1536 /* Wait for expected status */
1537 rc = hl_poll_timeout(
1539 le32_to_cpu(dyn_regs->cpu_cmd_status_to_host),
1541 FIELD_GET(COMMS_STATUS_STATUS_MASK, status) == expected_status,
1542 hdev->fw_poll_interval_usec,
1546 hl_fw_dynamic_report_error_status(hdev, status,
1552 * skip storing FW response for NOOP to preserve the actual desired
1555 if (expected_status == COMMS_STS_NOOP)
1558 rc = hl_fw_dynamic_extract_fw_response(hdev, fw_loader,
1559 &fw_loader->dynamic_loader.response,
1565 * hl_fw_dynamic_send_clear_cmd - send clear command to FW
1567 * @hdev: pointer to the habanalabs device structure
1568 * @fw_loader: managing structure for loading device's FW
1570 * @return 0 on success, otherwise non-zero error code
1572 * after command cycle between LKD to FW CPU (i.e. LKD got an expected status
1573 * from FW) we need to clear the CPU status register in order to avoid garbage
1574 * between command cycles.
1575 * This is done by sending clear command and polling the CPU to LKD status
1576 * register to hold the status NOOP
1578 static int hl_fw_dynamic_send_clear_cmd(struct hl_device *hdev,
1579 struct fw_load_mgr *fw_loader)
1581 hl_fw_dynamic_send_cmd(hdev, fw_loader, COMMS_CLR_STS, 0);
1583 return hl_fw_dynamic_wait_for_status(hdev, fw_loader, COMMS_STS_NOOP,
1584 fw_loader->cpu_timeout);
1588 * hl_fw_dynamic_send_protocol_cmd - send LKD to FW cmd and wait for ACK
1590 * @hdev: pointer to the habanalabs device structure
1591 * @fw_loader: managing structure for loading device's FW
1592 * @cmd: LKD to FW cmd code
1593 * @size: size of next FW component to be loaded (0 if not necessary)
1594 * @wait_ok: if true also wait for OK response from FW
1595 * @timeout: timeout for status wait
1597 * @return 0 on success, otherwise non-zero error code
1600 * when sending protocol command we have the following steps:
1601 * - send clear (clear command and verify clear status register)
1602 * - send the actual protocol command
1603 * - wait for ACK on the protocol command
1606 * if, in addition, the specific protocol command should wait for OK then:
1612 * send clear: this is necessary in order to clear the status register to avoid
1613 * leftovers between command
1614 * NOOP command: necessary to avoid loop on the clear command by the FW
1616 int hl_fw_dynamic_send_protocol_cmd(struct hl_device *hdev,
1617 struct fw_load_mgr *fw_loader,
1618 enum comms_cmd cmd, unsigned int size,
1619 bool wait_ok, u32 timeout)
1623 /* first send clear command to clean former commands */
1624 rc = hl_fw_dynamic_send_clear_cmd(hdev, fw_loader);
1626 /* send the actual command */
1627 hl_fw_dynamic_send_cmd(hdev, fw_loader, cmd, size);
1629 /* wait for ACK for the command */
1630 rc = hl_fw_dynamic_wait_for_status(hdev, fw_loader, COMMS_STS_ACK,
1635 /* clear command to prepare for NOOP command */
1636 rc = hl_fw_dynamic_send_clear_cmd(hdev, fw_loader);
1640 /* send the actual NOOP command */
1641 hl_fw_dynamic_send_cmd(hdev, fw_loader, COMMS_NOOP, 0);
1646 rc = hl_fw_dynamic_wait_for_status(hdev, fw_loader, COMMS_STS_OK,
1651 /* clear command to prepare for NOOP command */
1652 rc = hl_fw_dynamic_send_clear_cmd(hdev, fw_loader);
1656 /* send the actual NOOP command */
1657 hl_fw_dynamic_send_cmd(hdev, fw_loader, COMMS_NOOP, 0);
1663 * hl_fw_compat_crc32 - CRC compatible with FW
1665 * @data: pointer to the data
1666 * @size: size of the data
1668 * @return the CRC32 result
1670 * NOTE: kernel's CRC32 differ's from standard CRC32 calculation.
1671 * in order to be aligned we need to flip the bits of both the input
1672 * initial CRC and kernel's CRC32 result.
1673 * in addition both sides use initial CRC of 0,
1675 static u32 hl_fw_compat_crc32(u8 *data, size_t size)
1677 return ~crc32_le(~((u32)0), data, size);
1681 * hl_fw_dynamic_validate_memory_bound - validate memory bounds for memory
1682 * transfer (image or descriptor) between
1685 * @hdev: pointer to the habanalabs device structure
1686 * @addr: device address of memory transfer
1687 * @size: memory transter size
1688 * @region: PCI memory region
1690 * @return 0 on success, otherwise non-zero error code
1692 static int hl_fw_dynamic_validate_memory_bound(struct hl_device *hdev,
1693 u64 addr, size_t size,
1694 struct pci_mem_region *region)
1698 /* now make sure that the memory transfer is within region's bounds */
1699 end_addr = addr + size;
1700 if (end_addr >= region->region_base + region->region_size) {
1702 "dynamic FW load: memory transfer end address out of memory region bounds. addr: %llx\n",
1708 * now make sure memory transfer is within predefined BAR bounds.
1709 * this is to make sure we do not need to set the bar (e.g. for DRAM
1712 if (end_addr >= region->region_base - region->offset_in_bar +
1715 "FW image beyond PCI BAR bounds\n");
1723 * hl_fw_dynamic_validate_descriptor - validate FW descriptor
1725 * @hdev: pointer to the habanalabs device structure
1726 * @fw_loader: managing structure for loading device's FW
1727 * @fw_desc: the descriptor form FW
1729 * @return 0 on success, otherwise non-zero error code
1731 static int hl_fw_dynamic_validate_descriptor(struct hl_device *hdev,
1732 struct fw_load_mgr *fw_loader,
1733 struct lkd_fw_comms_desc *fw_desc)
1735 struct pci_mem_region *region;
1736 enum pci_region region_id;
1743 if (le32_to_cpu(fw_desc->header.magic) != HL_COMMS_DESC_MAGIC) {
1744 dev_err(hdev->dev, "Invalid magic for dynamic FW descriptor (%x)\n",
1745 fw_desc->header.magic);
1749 if (fw_desc->header.version != HL_COMMS_DESC_VER) {
1750 dev_err(hdev->dev, "Invalid version for dynamic FW descriptor (%x)\n",
1751 fw_desc->header.version);
1756 * calc CRC32 of data without header.
1757 * note that no alignment/stride address issues here as all structures
1760 data_size = sizeof(struct lkd_fw_comms_desc) -
1761 sizeof(struct comms_desc_header);
1762 data_ptr = (u8 *)fw_desc + sizeof(struct comms_desc_header);
1764 if (le16_to_cpu(fw_desc->header.size) != data_size) {
1766 "Invalid descriptor size 0x%x, expected size 0x%zx\n",
1767 le16_to_cpu(fw_desc->header.size), data_size);
1771 data_crc32 = hl_fw_compat_crc32(data_ptr, data_size);
1773 if (data_crc32 != le32_to_cpu(fw_desc->header.crc32)) {
1775 "CRC32 mismatch for dynamic FW descriptor (%x:%x)\n",
1776 data_crc32, fw_desc->header.crc32);
1780 /* find memory region to which to copy the image */
1781 addr = le64_to_cpu(fw_desc->img_addr);
1782 region_id = hl_get_pci_memory_region(hdev, addr);
1783 if ((region_id != PCI_REGION_SRAM) &&
1784 ((region_id != PCI_REGION_DRAM))) {
1786 "Invalid region to copy FW image address=%llx\n", addr);
1790 region = &hdev->pci_mem_region[region_id];
1792 /* store the region for the copy stage */
1793 fw_loader->dynamic_loader.image_region = region;
1796 * here we know that the start address is valid, now make sure that the
1797 * image is within region's bounds
1799 rc = hl_fw_dynamic_validate_memory_bound(hdev, addr,
1800 fw_loader->dynamic_loader.fw_image_size,
1804 "invalid mem transfer request for FW image\n");
1808 /* here we can mark the descriptor as valid as the content has been validated */
1809 fw_loader->dynamic_loader.fw_desc_valid = true;
1814 static int hl_fw_dynamic_validate_response(struct hl_device *hdev,
1815 struct fw_response *response,
1816 struct pci_mem_region *region)
1821 device_addr = region->region_base + response->ram_offset;
1824 * validate that the descriptor is within region's bounds
1825 * Note that as the start address was supplied according to the RAM
1826 * type- testing only the end address is enough
1828 rc = hl_fw_dynamic_validate_memory_bound(hdev, device_addr,
1829 sizeof(struct lkd_fw_comms_desc),
1835 * hl_fw_dynamic_read_and_validate_descriptor - read and validate FW descriptor
1837 * @hdev: pointer to the habanalabs device structure
1838 * @fw_loader: managing structure for loading device's FW
1840 * @return 0 on success, otherwise non-zero error code
1842 static int hl_fw_dynamic_read_and_validate_descriptor(struct hl_device *hdev,
1843 struct fw_load_mgr *fw_loader)
1845 struct lkd_fw_comms_desc *fw_desc;
1846 struct pci_mem_region *region;
1847 struct fw_response *response;
1848 enum pci_region region_id;
1852 fw_desc = &fw_loader->dynamic_loader.comm_desc;
1853 response = &fw_loader->dynamic_loader.response;
1855 region_id = (response->ram_type == COMMS_SRAM) ?
1856 PCI_REGION_SRAM : PCI_REGION_DRAM;
1858 region = &hdev->pci_mem_region[region_id];
1860 rc = hl_fw_dynamic_validate_response(hdev, response, region);
1863 "invalid mem transfer request for FW descriptor\n");
1868 * extract address to copy the descriptor from
1869 * in addition, as the descriptor value is going to be over-ridden by new data- we mark it
1871 * it will be marked again as valid once validated
1873 fw_loader->dynamic_loader.fw_desc_valid = false;
1874 src = hdev->pcie_bar[region->bar_id] + region->offset_in_bar +
1875 response->ram_offset;
1876 memcpy_fromio(fw_desc, src, sizeof(struct lkd_fw_comms_desc));
1878 return hl_fw_dynamic_validate_descriptor(hdev, fw_loader, fw_desc);
1882 * hl_fw_dynamic_request_descriptor - handshake with CPU to get FW descriptor
1884 * @hdev: pointer to the habanalabs device structure
1885 * @fw_loader: managing structure for loading device's FW
1886 * @next_image_size: size to allocate for next FW component
1888 * @return 0 on success, otherwise non-zero error code
1890 static int hl_fw_dynamic_request_descriptor(struct hl_device *hdev,
1891 struct fw_load_mgr *fw_loader,
1892 size_t next_image_size)
1896 rc = hl_fw_dynamic_send_protocol_cmd(hdev, fw_loader, COMMS_PREP_DESC,
1897 next_image_size, true,
1898 fw_loader->cpu_timeout);
1902 return hl_fw_dynamic_read_and_validate_descriptor(hdev, fw_loader);
1906 * hl_fw_dynamic_read_device_fw_version - read FW version to exposed properties
1908 * @hdev: pointer to the habanalabs device structure
1909 * @fwc: the firmware component
1910 * @fw_version: fw component's version string
1912 static void hl_fw_dynamic_read_device_fw_version(struct hl_device *hdev,
1913 enum hl_fw_component fwc,
1914 const char *fw_version)
1916 struct asic_fixed_properties *prop = &hdev->asic_prop;
1917 char *preboot_ver, *boot_ver;
1921 case FW_COMP_BOOT_FIT:
1922 strscpy(prop->uboot_ver, fw_version, VERSION_MAX_LEN);
1923 boot_ver = extract_fw_ver_from_str(prop->uboot_ver);
1925 dev_info(hdev->dev, "boot-fit version %s\n", boot_ver);
1930 case FW_COMP_PREBOOT:
1931 strscpy(prop->preboot_ver, fw_version, VERSION_MAX_LEN);
1932 preboot_ver = strnstr(prop->preboot_ver, "Preboot",
1934 if (preboot_ver && preboot_ver != prop->preboot_ver) {
1935 strscpy(btl_ver, prop->preboot_ver,
1936 min((int) (preboot_ver - prop->preboot_ver),
1938 dev_info(hdev->dev, "%s\n", btl_ver);
1941 preboot_ver = extract_fw_ver_from_str(prop->preboot_ver);
1943 dev_info(hdev->dev, "preboot version %s\n",
1950 dev_warn(hdev->dev, "Undefined FW component: %d\n", fwc);
1956 * hl_fw_dynamic_copy_image - copy image to memory allocated by the FW
1958 * @hdev: pointer to the habanalabs device structure
1959 * @fw: fw descriptor
1960 * @fw_loader: managing structure for loading device's FW
1962 static int hl_fw_dynamic_copy_image(struct hl_device *hdev,
1963 const struct firmware *fw,
1964 struct fw_load_mgr *fw_loader)
1966 struct lkd_fw_comms_desc *fw_desc;
1967 struct pci_mem_region *region;
1972 fw_desc = &fw_loader->dynamic_loader.comm_desc;
1973 addr = le64_to_cpu(fw_desc->img_addr);
1975 /* find memory region to which to copy the image */
1976 region = fw_loader->dynamic_loader.image_region;
1978 dest = hdev->pcie_bar[region->bar_id] + region->offset_in_bar +
1979 (addr - region->region_base);
1981 rc = hl_fw_copy_fw_to_device(hdev, fw, dest,
1982 fw_loader->boot_fit_img.src_off,
1983 fw_loader->boot_fit_img.copy_size);
1989 * hl_fw_dynamic_copy_msg - copy msg to memory allocated by the FW
1991 * @hdev: pointer to the habanalabs device structure
1993 * @fw_loader: managing structure for loading device's FW
1995 static int hl_fw_dynamic_copy_msg(struct hl_device *hdev,
1996 struct lkd_msg_comms *msg, struct fw_load_mgr *fw_loader)
1998 struct lkd_fw_comms_desc *fw_desc;
1999 struct pci_mem_region *region;
2004 fw_desc = &fw_loader->dynamic_loader.comm_desc;
2005 addr = le64_to_cpu(fw_desc->img_addr);
2007 /* find memory region to which to copy the image */
2008 region = fw_loader->dynamic_loader.image_region;
2010 dest = hdev->pcie_bar[region->bar_id] + region->offset_in_bar +
2011 (addr - region->region_base);
2013 rc = hl_fw_copy_msg_to_device(hdev, msg, dest, 0, 0);
2019 * hl_fw_boot_fit_update_state - update internal data structures after boot-fit
2022 * @hdev: pointer to the habanalabs device structure
2023 * @cpu_boot_dev_sts0_reg: register holding CPU boot dev status 0
2024 * @cpu_boot_dev_sts1_reg: register holding CPU boot dev status 1
2026 * @return 0 on success, otherwise non-zero error code
2028 static void hl_fw_boot_fit_update_state(struct hl_device *hdev,
2029 u32 cpu_boot_dev_sts0_reg,
2030 u32 cpu_boot_dev_sts1_reg)
2032 struct asic_fixed_properties *prop = &hdev->asic_prop;
2034 hdev->fw_loader.fw_comp_loaded |= FW_TYPE_BOOT_CPU;
2036 /* Read boot_cpu status bits */
2037 if (prop->fw_preboot_cpu_boot_dev_sts0 & CPU_BOOT_DEV_STS0_ENABLED) {
2038 prop->fw_bootfit_cpu_boot_dev_sts0 =
2039 RREG32(cpu_boot_dev_sts0_reg);
2041 prop->hard_reset_done_by_fw = !!(prop->fw_bootfit_cpu_boot_dev_sts0 &
2042 CPU_BOOT_DEV_STS0_FW_HARD_RST_EN);
2044 dev_dbg(hdev->dev, "Firmware boot CPU status0 %#x\n",
2045 prop->fw_bootfit_cpu_boot_dev_sts0);
2048 if (prop->fw_cpu_boot_dev_sts1_valid) {
2049 prop->fw_bootfit_cpu_boot_dev_sts1 =
2050 RREG32(cpu_boot_dev_sts1_reg);
2052 dev_dbg(hdev->dev, "Firmware boot CPU status1 %#x\n",
2053 prop->fw_bootfit_cpu_boot_dev_sts1);
2056 dev_dbg(hdev->dev, "Firmware boot CPU hard-reset is %s\n",
2057 prop->hard_reset_done_by_fw ? "enabled" : "disabled");
2060 static void hl_fw_dynamic_update_linux_interrupt_if(struct hl_device *hdev)
2062 struct cpu_dyn_regs *dyn_regs =
2063 &hdev->fw_loader.dynamic_loader.comm_desc.cpu_dyn_regs;
2065 /* Check whether all 3 interrupt interfaces are set, if not use a
2068 if (!hdev->asic_prop.gic_interrupts_enable &&
2069 !(hdev->asic_prop.fw_app_cpu_boot_dev_sts0 &
2070 CPU_BOOT_DEV_STS0_MULTI_IRQ_POLL_EN)) {
2071 dyn_regs->gic_host_halt_irq = dyn_regs->gic_host_pi_upd_irq;
2072 dyn_regs->gic_host_ints_irq = dyn_regs->gic_host_pi_upd_irq;
2075 "Using a single interrupt interface towards cpucp");
2079 * hl_fw_dynamic_load_image - load FW image using dynamic protocol
2081 * @hdev: pointer to the habanalabs device structure
2082 * @fw_loader: managing structure for loading device's FW
2083 * @load_fwc: the FW component to be loaded
2084 * @img_ld_timeout: image load timeout
2086 * @return 0 on success, otherwise non-zero error code
2088 static int hl_fw_dynamic_load_image(struct hl_device *hdev,
2089 struct fw_load_mgr *fw_loader,
2090 enum hl_fw_component load_fwc,
2093 enum hl_fw_component cur_fwc;
2094 const struct firmware *fw;
2099 * when loading image we have one of 2 scenarios:
2100 * 1. current FW component is preboot and we want to load boot-fit
2101 * 2. current FW component is boot-fit and we want to load linux
2103 if (load_fwc == FW_COMP_BOOT_FIT) {
2104 cur_fwc = FW_COMP_PREBOOT;
2105 fw_name = fw_loader->boot_fit_img.image_name;
2107 cur_fwc = FW_COMP_BOOT_FIT;
2108 fw_name = fw_loader->linux_img.image_name;
2111 /* request FW in order to communicate to FW the size to be allocated */
2112 rc = hl_request_fw(hdev, &fw, fw_name);
2116 /* store the image size for future validation */
2117 fw_loader->dynamic_loader.fw_image_size = fw->size;
2119 rc = hl_fw_dynamic_request_descriptor(hdev, fw_loader, fw->size);
2123 /* read preboot version */
2124 hl_fw_dynamic_read_device_fw_version(hdev, cur_fwc,
2125 fw_loader->dynamic_loader.comm_desc.cur_fw_ver);
2128 /* update state according to boot stage */
2129 if (cur_fwc == FW_COMP_BOOT_FIT) {
2130 struct cpu_dyn_regs *dyn_regs;
2132 dyn_regs = &fw_loader->dynamic_loader.comm_desc.cpu_dyn_regs;
2133 hl_fw_boot_fit_update_state(hdev,
2134 le32_to_cpu(dyn_regs->cpu_boot_dev_sts0),
2135 le32_to_cpu(dyn_regs->cpu_boot_dev_sts1));
2138 /* copy boot fit to space allocated by FW */
2139 rc = hl_fw_dynamic_copy_image(hdev, fw, fw_loader);
2143 rc = hl_fw_dynamic_send_protocol_cmd(hdev, fw_loader, COMMS_DATA_RDY,
2145 fw_loader->cpu_timeout);
2149 rc = hl_fw_dynamic_send_protocol_cmd(hdev, fw_loader, COMMS_EXEC,
2154 hl_release_firmware(fw);
2158 static int hl_fw_dynamic_wait_for_boot_fit_active(struct hl_device *hdev,
2159 struct fw_load_mgr *fw_loader)
2161 struct dynamic_fw_load_mgr *dyn_loader;
2165 dyn_loader = &fw_loader->dynamic_loader;
2168 * Make sure CPU boot-loader is running
2169 * Note that the CPU_BOOT_STATUS_SRAM_AVAIL is generally set by Linux
2170 * yet there is a debug scenario in which we loading uboot (without Linux)
2171 * which at later stage is relocated to DRAM. In this case we expect
2172 * uboot to set the CPU_BOOT_STATUS_SRAM_AVAIL and so we add it to the
2175 rc = hl_poll_timeout(
2177 le32_to_cpu(dyn_loader->comm_desc.cpu_dyn_regs.cpu_boot_status),
2179 (status == CPU_BOOT_STATUS_READY_TO_BOOT) ||
2180 (status == CPU_BOOT_STATUS_SRAM_AVAIL),
2181 hdev->fw_poll_interval_usec,
2182 dyn_loader->wait_for_bl_timeout);
2184 dev_err(hdev->dev, "failed to wait for boot\n");
2188 dev_dbg(hdev->dev, "uboot status = %d\n", status);
2192 static int hl_fw_dynamic_wait_for_linux_active(struct hl_device *hdev,
2193 struct fw_load_mgr *fw_loader)
2195 struct dynamic_fw_load_mgr *dyn_loader;
2199 dyn_loader = &fw_loader->dynamic_loader;
2201 /* Make sure CPU linux is running */
2203 rc = hl_poll_timeout(
2205 le32_to_cpu(dyn_loader->comm_desc.cpu_dyn_regs.cpu_boot_status),
2207 (status == CPU_BOOT_STATUS_SRAM_AVAIL),
2208 hdev->fw_poll_interval_usec,
2209 fw_loader->cpu_timeout);
2211 dev_err(hdev->dev, "failed to wait for Linux\n");
2215 dev_dbg(hdev->dev, "Boot status = %d\n", status);
2220 * hl_fw_linux_update_state - update internal data structures after Linux
2222 * Note: Linux initialization is comprised mainly
2223 * of two stages - loading kernel (SRAM_AVAIL)
2225 * Therefore reading boot device status in any of
2226 * these stages might result in different values.
2228 * @hdev: pointer to the habanalabs device structure
2229 * @cpu_boot_dev_sts0_reg: register holding CPU boot dev status 0
2230 * @cpu_boot_dev_sts1_reg: register holding CPU boot dev status 1
2232 * @return 0 on success, otherwise non-zero error code
2234 static void hl_fw_linux_update_state(struct hl_device *hdev,
2235 u32 cpu_boot_dev_sts0_reg,
2236 u32 cpu_boot_dev_sts1_reg)
2238 struct asic_fixed_properties *prop = &hdev->asic_prop;
2240 hdev->fw_loader.fw_comp_loaded |= FW_TYPE_LINUX;
2242 /* Read FW application security bits */
2243 if (prop->fw_cpu_boot_dev_sts0_valid) {
2244 prop->fw_app_cpu_boot_dev_sts0 = RREG32(cpu_boot_dev_sts0_reg);
2246 prop->hard_reset_done_by_fw = !!(prop->fw_app_cpu_boot_dev_sts0 &
2247 CPU_BOOT_DEV_STS0_FW_HARD_RST_EN);
2249 if (prop->fw_app_cpu_boot_dev_sts0 &
2250 CPU_BOOT_DEV_STS0_GIC_PRIVILEGED_EN)
2251 prop->gic_interrupts_enable = false;
2254 "Firmware application CPU status0 %#x\n",
2255 prop->fw_app_cpu_boot_dev_sts0);
2257 dev_dbg(hdev->dev, "GIC controller is %s\n",
2258 prop->gic_interrupts_enable ?
2259 "enabled" : "disabled");
2262 if (prop->fw_cpu_boot_dev_sts1_valid) {
2263 prop->fw_app_cpu_boot_dev_sts1 = RREG32(cpu_boot_dev_sts1_reg);
2266 "Firmware application CPU status1 %#x\n",
2267 prop->fw_app_cpu_boot_dev_sts1);
2270 dev_dbg(hdev->dev, "Firmware application CPU hard-reset is %s\n",
2271 prop->hard_reset_done_by_fw ? "enabled" : "disabled");
2273 dev_info(hdev->dev, "Successfully loaded firmware to device\n");
2277 * hl_fw_dynamic_send_msg - send a COMMS message with attached data
2279 * @hdev: pointer to the habanalabs device structure
2280 * @fw_loader: managing structure for loading device's FW
2281 * @msg_type: message type
2282 * @data: data to be sent
2284 * @return 0 on success, otherwise non-zero error code
2286 static int hl_fw_dynamic_send_msg(struct hl_device *hdev,
2287 struct fw_load_mgr *fw_loader, u8 msg_type, void *data)
2289 struct lkd_msg_comms msg;
2292 memset(&msg, 0, sizeof(msg));
2294 /* create message to be sent */
2295 msg.header.type = msg_type;
2296 msg.header.size = cpu_to_le16(sizeof(struct comms_msg_header));
2297 msg.header.magic = cpu_to_le32(HL_COMMS_MSG_MAGIC);
2300 case HL_COMMS_RESET_CAUSE_TYPE:
2301 msg.reset_cause = *(__u8 *) data;
2305 "Send COMMS message - invalid message type %u\n",
2310 rc = hl_fw_dynamic_request_descriptor(hdev, fw_loader,
2311 sizeof(struct lkd_msg_comms));
2315 /* copy message to space allocated by FW */
2316 rc = hl_fw_dynamic_copy_msg(hdev, &msg, fw_loader);
2320 rc = hl_fw_dynamic_send_protocol_cmd(hdev, fw_loader, COMMS_DATA_RDY,
2322 fw_loader->cpu_timeout);
2326 rc = hl_fw_dynamic_send_protocol_cmd(hdev, fw_loader, COMMS_EXEC,
2328 fw_loader->cpu_timeout);
2336 * hl_fw_dynamic_init_cpu - initialize the device CPU using dynamic protocol
2338 * @hdev: pointer to the habanalabs device structure
2339 * @fw_loader: managing structure for loading device's FW
2341 * @return 0 on success, otherwise non-zero error code
2343 * brief: the dynamic protocol is master (LKD) slave (FW CPU) protocol.
2344 * the communication is done using registers:
2345 * - LKD command register
2346 * - FW status register
2347 * the protocol is race free. this goal is achieved by splitting the requests
2348 * and response to known synchronization points between the LKD and the FW.
2349 * each response to LKD request is known and bound to a predefined timeout.
2350 * in case of timeout expiration without the desired status from FW- the
2351 * protocol (and hence the boot) will fail.
2353 static int hl_fw_dynamic_init_cpu(struct hl_device *hdev,
2354 struct fw_load_mgr *fw_loader)
2356 struct cpu_dyn_regs *dyn_regs;
2360 "Loading firmware to device, may take some time...\n");
2362 /* initialize FW descriptor as invalid */
2363 fw_loader->dynamic_loader.fw_desc_valid = false;
2366 * In this stage, "cpu_dyn_regs" contains only LKD's hard coded values!
2367 * It will be updated from FW after hl_fw_dynamic_request_descriptor().
2369 dyn_regs = &fw_loader->dynamic_loader.comm_desc.cpu_dyn_regs;
2371 rc = hl_fw_dynamic_send_protocol_cmd(hdev, fw_loader, COMMS_RST_STATE,
2373 fw_loader->cpu_timeout);
2377 if (hdev->reset_info.curr_reset_cause) {
2378 rc = hl_fw_dynamic_send_msg(hdev, fw_loader,
2379 HL_COMMS_RESET_CAUSE_TYPE, &hdev->reset_info.curr_reset_cause);
2383 /* Clear current reset cause */
2384 hdev->reset_info.curr_reset_cause = HL_RESET_CAUSE_UNKNOWN;
2387 if (!(hdev->fw_components & FW_TYPE_BOOT_CPU)) {
2388 rc = hl_fw_dynamic_request_descriptor(hdev, fw_loader, 0);
2392 /* read preboot version */
2393 hl_fw_dynamic_read_device_fw_version(hdev, FW_COMP_PREBOOT,
2394 fw_loader->dynamic_loader.comm_desc.cur_fw_ver);
2398 /* load boot fit to FW */
2399 rc = hl_fw_dynamic_load_image(hdev, fw_loader, FW_COMP_BOOT_FIT,
2400 fw_loader->boot_fit_timeout);
2402 dev_err(hdev->dev, "failed to load boot fit\n");
2407 * when testing FW load (without Linux) on PLDM we don't want to
2408 * wait until boot fit is active as it may take several hours.
2409 * instead, we load the bootfit and let it do all initializations in
2412 if (hdev->pldm && !(hdev->fw_components & FW_TYPE_LINUX))
2415 rc = hl_fw_dynamic_wait_for_boot_fit_active(hdev, fw_loader);
2419 /* Enable DRAM scrambling before Linux boot and after successful
2422 hdev->asic_funcs->init_cpu_scrambler_dram(hdev);
2424 if (!(hdev->fw_components & FW_TYPE_LINUX)) {
2425 dev_info(hdev->dev, "Skip loading Linux F/W\n");
2429 if (fw_loader->skip_bmc) {
2430 rc = hl_fw_dynamic_send_protocol_cmd(hdev, fw_loader,
2433 fw_loader->cpu_timeout);
2435 dev_err(hdev->dev, "failed to load boot fit\n");
2440 /* load Linux image to FW */
2441 rc = hl_fw_dynamic_load_image(hdev, fw_loader, FW_COMP_LINUX,
2442 fw_loader->cpu_timeout);
2444 dev_err(hdev->dev, "failed to load Linux\n");
2448 rc = hl_fw_dynamic_wait_for_linux_active(hdev, fw_loader);
2452 hl_fw_linux_update_state(hdev, le32_to_cpu(dyn_regs->cpu_boot_dev_sts0),
2453 le32_to_cpu(dyn_regs->cpu_boot_dev_sts1));
2455 hl_fw_dynamic_update_linux_interrupt_if(hdev);
2460 if (fw_loader->dynamic_loader.fw_desc_valid)
2461 fw_read_errors(hdev, le32_to_cpu(dyn_regs->cpu_boot_err0),
2462 le32_to_cpu(dyn_regs->cpu_boot_err1),
2463 le32_to_cpu(dyn_regs->cpu_boot_dev_sts0),
2464 le32_to_cpu(dyn_regs->cpu_boot_dev_sts1));
2469 * hl_fw_static_init_cpu - initialize the device CPU using static protocol
2471 * @hdev: pointer to the habanalabs device structure
2472 * @fw_loader: managing structure for loading device's FW
2474 * @return 0 on success, otherwise non-zero error code
2476 static int hl_fw_static_init_cpu(struct hl_device *hdev,
2477 struct fw_load_mgr *fw_loader)
2479 u32 cpu_msg_status_reg, cpu_timeout, msg_to_cpu_reg, status;
2480 u32 cpu_boot_dev_status0_reg, cpu_boot_dev_status1_reg;
2481 struct static_fw_load_mgr *static_loader;
2482 u32 cpu_boot_status_reg;
2485 if (!(hdev->fw_components & FW_TYPE_BOOT_CPU))
2488 /* init common loader parameters */
2489 cpu_timeout = fw_loader->cpu_timeout;
2491 /* init static loader parameters */
2492 static_loader = &fw_loader->static_loader;
2493 cpu_msg_status_reg = static_loader->cpu_cmd_status_to_host_reg;
2494 msg_to_cpu_reg = static_loader->kmd_msg_to_cpu_reg;
2495 cpu_boot_dev_status0_reg = static_loader->cpu_boot_dev_status0_reg;
2496 cpu_boot_dev_status1_reg = static_loader->cpu_boot_dev_status1_reg;
2497 cpu_boot_status_reg = static_loader->cpu_boot_status_reg;
2499 dev_info(hdev->dev, "Going to wait for device boot (up to %lds)\n",
2500 cpu_timeout / USEC_PER_SEC);
2502 /* Wait for boot FIT request */
2503 rc = hl_poll_timeout(
2505 cpu_boot_status_reg,
2507 status == CPU_BOOT_STATUS_WAITING_FOR_BOOT_FIT,
2508 hdev->fw_poll_interval_usec,
2509 fw_loader->boot_fit_timeout);
2513 "No boot fit request received, resuming boot\n");
2515 rc = hdev->asic_funcs->load_boot_fit_to_device(hdev);
2519 /* Clear device CPU message status */
2520 WREG32(cpu_msg_status_reg, CPU_MSG_CLR);
2522 /* Signal device CPU that boot loader is ready */
2523 WREG32(msg_to_cpu_reg, KMD_MSG_FIT_RDY);
2525 /* Poll for CPU device ack */
2526 rc = hl_poll_timeout(
2530 status == CPU_MSG_OK,
2531 hdev->fw_poll_interval_usec,
2532 fw_loader->boot_fit_timeout);
2536 "Timeout waiting for boot fit load ack\n");
2541 WREG32(msg_to_cpu_reg, KMD_MSG_NA);
2545 * Make sure CPU boot-loader is running
2546 * Note that the CPU_BOOT_STATUS_SRAM_AVAIL is generally set by Linux
2547 * yet there is a debug scenario in which we loading uboot (without Linux)
2548 * which at later stage is relocated to DRAM. In this case we expect
2549 * uboot to set the CPU_BOOT_STATUS_SRAM_AVAIL and so we add it to the
2552 rc = hl_poll_timeout(
2554 cpu_boot_status_reg,
2556 (status == CPU_BOOT_STATUS_DRAM_RDY) ||
2557 (status == CPU_BOOT_STATUS_NIC_FW_RDY) ||
2558 (status == CPU_BOOT_STATUS_READY_TO_BOOT) ||
2559 (status == CPU_BOOT_STATUS_SRAM_AVAIL),
2560 hdev->fw_poll_interval_usec,
2563 dev_dbg(hdev->dev, "uboot status = %d\n", status);
2565 /* Read U-Boot version now in case we will later fail */
2566 hl_fw_static_read_device_fw_version(hdev, FW_COMP_BOOT_FIT);
2568 /* update state according to boot stage */
2569 hl_fw_boot_fit_update_state(hdev, cpu_boot_dev_status0_reg,
2570 cpu_boot_dev_status1_reg);
2573 detect_cpu_boot_status(hdev, status);
2578 /* Enable DRAM scrambling before Linux boot and after successful
2581 hdev->asic_funcs->init_cpu_scrambler_dram(hdev);
2583 if (!(hdev->fw_components & FW_TYPE_LINUX)) {
2584 dev_info(hdev->dev, "Skip loading Linux F/W\n");
2589 if (status == CPU_BOOT_STATUS_SRAM_AVAIL) {
2595 "Loading firmware to device, may take some time...\n");
2597 rc = hdev->asic_funcs->load_firmware_to_device(hdev);
2601 if (fw_loader->skip_bmc) {
2602 WREG32(msg_to_cpu_reg, KMD_MSG_SKIP_BMC);
2604 rc = hl_poll_timeout(
2606 cpu_boot_status_reg,
2608 (status == CPU_BOOT_STATUS_BMC_WAITING_SKIPPED),
2609 hdev->fw_poll_interval_usec,
2614 "Failed to get ACK on skipping BMC, %d\n",
2616 WREG32(msg_to_cpu_reg, KMD_MSG_NA);
2622 WREG32(msg_to_cpu_reg, KMD_MSG_FIT_RDY);
2624 rc = hl_poll_timeout(
2626 cpu_boot_status_reg,
2628 (status == CPU_BOOT_STATUS_SRAM_AVAIL),
2629 hdev->fw_poll_interval_usec,
2633 WREG32(msg_to_cpu_reg, KMD_MSG_NA);
2636 if (status == CPU_BOOT_STATUS_FIT_CORRUPTED)
2638 "Device reports FIT image is corrupted\n");
2641 "Failed to load firmware to device, %d\n",
2648 rc = fw_read_errors(hdev, fw_loader->static_loader.boot_err0_reg,
2649 fw_loader->static_loader.boot_err1_reg,
2650 cpu_boot_dev_status0_reg,
2651 cpu_boot_dev_status1_reg);
2655 hl_fw_linux_update_state(hdev, cpu_boot_dev_status0_reg,
2656 cpu_boot_dev_status1_reg);
2661 fw_read_errors(hdev, fw_loader->static_loader.boot_err0_reg,
2662 fw_loader->static_loader.boot_err1_reg,
2663 cpu_boot_dev_status0_reg,
2664 cpu_boot_dev_status1_reg);
2670 * hl_fw_init_cpu - initialize the device CPU
2672 * @hdev: pointer to the habanalabs device structure
2674 * @return 0 on success, otherwise non-zero error code
2676 * perform necessary initializations for device's CPU. takes into account if
2677 * init protocol is static or dynamic.
2679 int hl_fw_init_cpu(struct hl_device *hdev)
2681 struct asic_fixed_properties *prop = &hdev->asic_prop;
2682 struct fw_load_mgr *fw_loader = &hdev->fw_loader;
2684 return prop->dynamic_fw_load ?
2685 hl_fw_dynamic_init_cpu(hdev, fw_loader) :
2686 hl_fw_static_init_cpu(hdev, fw_loader);
2689 void hl_fw_set_pll_profile(struct hl_device *hdev)
2691 hl_fw_set_frequency(hdev, hdev->asic_prop.clk_pll_index,
2692 hdev->asic_prop.max_freq_value);
2695 int hl_fw_get_clk_rate(struct hl_device *hdev, u32 *cur_clk, u32 *max_clk)
2699 if (!hl_device_operational(hdev, NULL))
2708 value = hl_fw_get_frequency(hdev, hdev->asic_prop.clk_pll_index, false);
2711 dev_err(hdev->dev, "Failed to retrieve device max clock %ld\n", value);
2715 *max_clk = (value / 1000 / 1000);
2717 value = hl_fw_get_frequency(hdev, hdev->asic_prop.clk_pll_index, true);
2720 dev_err(hdev->dev, "Failed to retrieve device current clock %ld\n", value);
2724 *cur_clk = (value / 1000 / 1000);
2729 long hl_fw_get_frequency(struct hl_device *hdev, u32 pll_index, bool curr)
2731 struct cpucp_packet pkt;
2736 rc = get_used_pll_index(hdev, pll_index, &used_pll_idx);
2740 memset(&pkt, 0, sizeof(pkt));
2743 pkt.ctl = cpu_to_le32(CPUCP_PACKET_FREQUENCY_CURR_GET <<
2744 CPUCP_PKT_CTL_OPCODE_SHIFT);
2746 pkt.ctl = cpu_to_le32(CPUCP_PACKET_FREQUENCY_GET << CPUCP_PKT_CTL_OPCODE_SHIFT);
2748 pkt.pll_index = cpu_to_le32((u32)used_pll_idx);
2750 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt), 0, &result);
2753 dev_err(hdev->dev, "Failed to get frequency of PLL %d, error %d\n",
2758 return (long) result;
2761 void hl_fw_set_frequency(struct hl_device *hdev, u32 pll_index, u64 freq)
2763 struct cpucp_packet pkt;
2767 rc = get_used_pll_index(hdev, pll_index, &used_pll_idx);
2771 memset(&pkt, 0, sizeof(pkt));
2773 pkt.ctl = cpu_to_le32(CPUCP_PACKET_FREQUENCY_SET << CPUCP_PKT_CTL_OPCODE_SHIFT);
2774 pkt.pll_index = cpu_to_le32((u32)used_pll_idx);
2775 pkt.value = cpu_to_le64(freq);
2777 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt), 0, NULL);
2780 dev_err(hdev->dev, "Failed to set frequency to PLL %d, error %d\n",
2784 long hl_fw_get_max_power(struct hl_device *hdev)
2786 struct cpucp_packet pkt;
2790 memset(&pkt, 0, sizeof(pkt));
2792 pkt.ctl = cpu_to_le32(CPUCP_PACKET_MAX_POWER_GET << CPUCP_PKT_CTL_OPCODE_SHIFT);
2794 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt), 0, &result);
2797 dev_err(hdev->dev, "Failed to get max power, error %d\n", rc);
2804 void hl_fw_set_max_power(struct hl_device *hdev)
2806 struct cpucp_packet pkt;
2809 /* TODO: remove this after simulator supports this packet */
2813 memset(&pkt, 0, sizeof(pkt));
2815 pkt.ctl = cpu_to_le32(CPUCP_PACKET_MAX_POWER_SET << CPUCP_PKT_CTL_OPCODE_SHIFT);
2816 pkt.value = cpu_to_le64(hdev->max_power);
2818 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt), 0, NULL);
2821 dev_err(hdev->dev, "Failed to set max power, error %d\n", rc);