Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / misc / habanalabs / common / firmware_if.c
blob20f77f58edef5573577592110289f3937202208f
1 // SPDX-License-Identifier: GPL-2.0
3 /*
4 * Copyright 2016-2019 HabanaLabs, Ltd.
5 * All Rights Reserved.
6 */
8 #include "habanalabs.h"
9 #include "../include/common/hl_boot_if.h"
11 #include <linux/firmware.h>
12 #include <linux/slab.h>
14 #define FW_FILE_MAX_SIZE 0x1400000 /* maximum size of 20MB */
15 /**
16 * hl_fw_load_fw_to_device() - Load F/W code to device's memory.
18 * @hdev: pointer to hl_device structure.
19 * @fw_name: the firmware image name
20 * @dst: IO memory mapped address space to copy firmware to
21 * @src_offset: offset in src FW to copy from
22 * @size: amount of bytes to copy (0 to copy the whole binary)
24 * Copy fw code from firmware file to device memory.
26 * Return: 0 on success, non-zero for failure.
28 int hl_fw_load_fw_to_device(struct hl_device *hdev, const char *fw_name,
29 void __iomem *dst, u32 src_offset, u32 size)
31 const struct firmware *fw;
32 const void *fw_data;
33 size_t fw_size;
34 int rc;
36 rc = request_firmware(&fw, fw_name, hdev->dev);
37 if (rc) {
38 dev_err(hdev->dev, "Firmware file %s is not found!\n", fw_name);
39 goto out;
42 fw_size = fw->size;
43 if ((fw_size % 4) != 0) {
44 dev_err(hdev->dev, "Illegal %s firmware size %zu\n",
45 fw_name, fw_size);
46 rc = -EINVAL;
47 goto out;
50 dev_dbg(hdev->dev, "%s firmware size == %zu\n", fw_name, fw_size);
52 if (fw_size > FW_FILE_MAX_SIZE) {
53 dev_err(hdev->dev,
54 "FW file size %zu exceeds maximum of %u bytes\n",
55 fw_size, FW_FILE_MAX_SIZE);
56 rc = -EINVAL;
57 goto out;
60 if (size - src_offset > fw_size) {
61 dev_err(hdev->dev,
62 "size to copy(%u) and offset(%u) are invalid\n",
63 size, src_offset);
64 rc = -EINVAL;
65 goto out;
68 if (size)
69 fw_size = size;
71 fw_data = (const void *) fw->data;
73 memcpy_toio(dst, fw_data + src_offset, fw_size);
75 out:
76 release_firmware(fw);
77 return rc;
80 int hl_fw_send_pci_access_msg(struct hl_device *hdev, u32 opcode)
82 struct cpucp_packet pkt = {};
84 pkt.ctl = cpu_to_le32(opcode << CPUCP_PKT_CTL_OPCODE_SHIFT);
86 return hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt,
87 sizeof(pkt), 0, NULL);
90 int hl_fw_send_cpu_message(struct hl_device *hdev, u32 hw_queue_id, u32 *msg,
91 u16 len, u32 timeout, u64 *result)
93 struct cpucp_packet *pkt;
94 dma_addr_t pkt_dma_addr;
95 u32 tmp;
96 int rc = 0;
98 pkt = hdev->asic_funcs->cpu_accessible_dma_pool_alloc(hdev, len,
99 &pkt_dma_addr);
100 if (!pkt) {
101 dev_err(hdev->dev,
102 "Failed to allocate DMA memory for packet to CPU\n");
103 return -ENOMEM;
106 memcpy(pkt, msg, len);
108 mutex_lock(&hdev->send_cpu_message_lock);
110 if (hdev->disabled)
111 goto out;
113 if (hdev->device_cpu_disabled) {
114 rc = -EIO;
115 goto out;
118 rc = hl_hw_queue_send_cb_no_cmpl(hdev, hw_queue_id, len, pkt_dma_addr);
119 if (rc) {
120 dev_err(hdev->dev, "Failed to send CB on CPU PQ (%d)\n", rc);
121 goto out;
124 rc = hl_poll_timeout_memory(hdev, &pkt->fence, tmp,
125 (tmp == CPUCP_PACKET_FENCE_VAL), 1000,
126 timeout, true);
128 hl_hw_queue_inc_ci_kernel(hdev, hw_queue_id);
130 if (rc == -ETIMEDOUT) {
131 dev_err(hdev->dev, "Device CPU packet timeout (0x%x)\n", tmp);
132 hdev->device_cpu_disabled = true;
133 goto out;
136 tmp = le32_to_cpu(pkt->ctl);
138 rc = (tmp & CPUCP_PKT_CTL_RC_MASK) >> CPUCP_PKT_CTL_RC_SHIFT;
139 if (rc) {
140 dev_err(hdev->dev, "F/W ERROR %d for CPU packet %d\n",
142 (tmp & CPUCP_PKT_CTL_OPCODE_MASK)
143 >> CPUCP_PKT_CTL_OPCODE_SHIFT);
144 rc = -EIO;
145 } else if (result) {
146 *result = le64_to_cpu(pkt->result);
149 out:
150 mutex_unlock(&hdev->send_cpu_message_lock);
152 hdev->asic_funcs->cpu_accessible_dma_pool_free(hdev, len, pkt);
154 return rc;
157 int hl_fw_unmask_irq(struct hl_device *hdev, u16 event_type)
159 struct cpucp_packet pkt;
160 u64 result;
161 int rc;
163 memset(&pkt, 0, sizeof(pkt));
165 pkt.ctl = cpu_to_le32(CPUCP_PACKET_UNMASK_RAZWI_IRQ <<
166 CPUCP_PKT_CTL_OPCODE_SHIFT);
167 pkt.value = cpu_to_le64(event_type);
169 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
170 0, &result);
172 if (rc)
173 dev_err(hdev->dev, "failed to unmask RAZWI IRQ %d", event_type);
175 return rc;
178 int hl_fw_unmask_irq_arr(struct hl_device *hdev, const u32 *irq_arr,
179 size_t irq_arr_size)
181 struct cpucp_unmask_irq_arr_packet *pkt;
182 size_t total_pkt_size;
183 u64 result;
184 int rc;
186 total_pkt_size = sizeof(struct cpucp_unmask_irq_arr_packet) +
187 irq_arr_size;
189 /* data should be aligned to 8 bytes in order to CPU-CP to copy it */
190 total_pkt_size = (total_pkt_size + 0x7) & ~0x7;
192 /* total_pkt_size is casted to u16 later on */
193 if (total_pkt_size > USHRT_MAX) {
194 dev_err(hdev->dev, "too many elements in IRQ array\n");
195 return -EINVAL;
198 pkt = kzalloc(total_pkt_size, GFP_KERNEL);
199 if (!pkt)
200 return -ENOMEM;
202 pkt->length = cpu_to_le32(irq_arr_size / sizeof(irq_arr[0]));
203 memcpy(&pkt->irqs, irq_arr, irq_arr_size);
205 pkt->cpucp_pkt.ctl = cpu_to_le32(CPUCP_PACKET_UNMASK_RAZWI_IRQ_ARRAY <<
206 CPUCP_PKT_CTL_OPCODE_SHIFT);
208 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) pkt,
209 total_pkt_size, 0, &result);
211 if (rc)
212 dev_err(hdev->dev, "failed to unmask IRQ array\n");
214 kfree(pkt);
216 return rc;
219 int hl_fw_test_cpu_queue(struct hl_device *hdev)
221 struct cpucp_packet test_pkt = {};
222 u64 result;
223 int rc;
225 test_pkt.ctl = cpu_to_le32(CPUCP_PACKET_TEST <<
226 CPUCP_PKT_CTL_OPCODE_SHIFT);
227 test_pkt.value = cpu_to_le64(CPUCP_PACKET_FENCE_VAL);
229 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &test_pkt,
230 sizeof(test_pkt), 0, &result);
232 if (!rc) {
233 if (result != CPUCP_PACKET_FENCE_VAL)
234 dev_err(hdev->dev,
235 "CPU queue test failed (%#08llx)\n", result);
236 } else {
237 dev_err(hdev->dev, "CPU queue test failed, error %d\n", rc);
240 return rc;
243 void *hl_fw_cpu_accessible_dma_pool_alloc(struct hl_device *hdev, size_t size,
244 dma_addr_t *dma_handle)
246 u64 kernel_addr;
248 kernel_addr = gen_pool_alloc(hdev->cpu_accessible_dma_pool, size);
250 *dma_handle = hdev->cpu_accessible_dma_address +
251 (kernel_addr - (u64) (uintptr_t) hdev->cpu_accessible_dma_mem);
253 return (void *) (uintptr_t) kernel_addr;
256 void hl_fw_cpu_accessible_dma_pool_free(struct hl_device *hdev, size_t size,
257 void *vaddr)
259 gen_pool_free(hdev->cpu_accessible_dma_pool, (u64) (uintptr_t) vaddr,
260 size);
263 int hl_fw_send_heartbeat(struct hl_device *hdev)
265 struct cpucp_packet hb_pkt = {};
266 u64 result;
267 int rc;
269 hb_pkt.ctl = cpu_to_le32(CPUCP_PACKET_TEST <<
270 CPUCP_PKT_CTL_OPCODE_SHIFT);
271 hb_pkt.value = cpu_to_le64(CPUCP_PACKET_FENCE_VAL);
273 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &hb_pkt,
274 sizeof(hb_pkt), 0, &result);
276 if ((rc) || (result != CPUCP_PACKET_FENCE_VAL))
277 rc = -EIO;
279 return rc;
282 int hl_fw_cpucp_info_get(struct hl_device *hdev,
283 u32 cpu_security_boot_status_reg)
285 struct asic_fixed_properties *prop = &hdev->asic_prop;
286 struct cpucp_packet pkt = {};
287 void *cpucp_info_cpu_addr;
288 dma_addr_t cpucp_info_dma_addr;
289 u64 result;
290 int rc;
292 cpucp_info_cpu_addr =
293 hdev->asic_funcs->cpu_accessible_dma_pool_alloc(hdev,
294 sizeof(struct cpucp_info),
295 &cpucp_info_dma_addr);
296 if (!cpucp_info_cpu_addr) {
297 dev_err(hdev->dev,
298 "Failed to allocate DMA memory for CPU-CP info packet\n");
299 return -ENOMEM;
302 memset(cpucp_info_cpu_addr, 0, sizeof(struct cpucp_info));
304 pkt.ctl = cpu_to_le32(CPUCP_PACKET_INFO_GET <<
305 CPUCP_PKT_CTL_OPCODE_SHIFT);
306 pkt.addr = cpu_to_le64(cpucp_info_dma_addr);
307 pkt.data_max_size = cpu_to_le32(sizeof(struct cpucp_info));
309 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
310 HL_CPUCP_INFO_TIMEOUT_USEC, &result);
311 if (rc) {
312 dev_err(hdev->dev,
313 "Failed to handle CPU-CP info pkt, error %d\n", rc);
314 goto out;
317 memcpy(&prop->cpucp_info, cpucp_info_cpu_addr,
318 sizeof(prop->cpucp_info));
320 rc = hl_build_hwmon_channel_info(hdev, prop->cpucp_info.sensors);
321 if (rc) {
322 dev_err(hdev->dev,
323 "Failed to build hwmon channel info, error %d\n", rc);
324 rc = -EFAULT;
325 goto out;
328 /* Read FW application security bits again */
329 if (hdev->asic_prop.fw_security_status_valid)
330 hdev->asic_prop.fw_app_security_map =
331 RREG32(cpu_security_boot_status_reg);
333 out:
334 hdev->asic_funcs->cpu_accessible_dma_pool_free(hdev,
335 sizeof(struct cpucp_info), cpucp_info_cpu_addr);
337 return rc;
340 int hl_fw_get_eeprom_data(struct hl_device *hdev, void *data, size_t max_size)
342 struct cpucp_packet pkt = {};
343 void *eeprom_info_cpu_addr;
344 dma_addr_t eeprom_info_dma_addr;
345 u64 result;
346 int rc;
348 eeprom_info_cpu_addr =
349 hdev->asic_funcs->cpu_accessible_dma_pool_alloc(hdev,
350 max_size, &eeprom_info_dma_addr);
351 if (!eeprom_info_cpu_addr) {
352 dev_err(hdev->dev,
353 "Failed to allocate DMA memory for CPU-CP EEPROM packet\n");
354 return -ENOMEM;
357 memset(eeprom_info_cpu_addr, 0, max_size);
359 pkt.ctl = cpu_to_le32(CPUCP_PACKET_EEPROM_DATA_GET <<
360 CPUCP_PKT_CTL_OPCODE_SHIFT);
361 pkt.addr = cpu_to_le64(eeprom_info_dma_addr);
362 pkt.data_max_size = cpu_to_le32(max_size);
364 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
365 HL_CPUCP_EEPROM_TIMEOUT_USEC, &result);
367 if (rc) {
368 dev_err(hdev->dev,
369 "Failed to handle CPU-CP EEPROM packet, error %d\n",
370 rc);
371 goto out;
374 /* result contains the actual size */
375 memcpy(data, eeprom_info_cpu_addr, min((size_t)result, max_size));
377 out:
378 hdev->asic_funcs->cpu_accessible_dma_pool_free(hdev, max_size,
379 eeprom_info_cpu_addr);
381 return rc;
384 int hl_fw_cpucp_pci_counters_get(struct hl_device *hdev,
385 struct hl_info_pci_counters *counters)
387 struct cpucp_packet pkt = {};
388 u64 result;
389 int rc;
391 pkt.ctl = cpu_to_le32(CPUCP_PACKET_PCIE_THROUGHPUT_GET <<
392 CPUCP_PKT_CTL_OPCODE_SHIFT);
394 /* Fetch PCI rx counter */
395 pkt.index = cpu_to_le32(cpucp_pcie_throughput_rx);
396 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
397 HL_CPUCP_INFO_TIMEOUT_USEC, &result);
398 if (rc) {
399 dev_err(hdev->dev,
400 "Failed to handle CPU-CP PCI info pkt, error %d\n", rc);
401 return rc;
403 counters->rx_throughput = result;
405 /* Fetch PCI tx counter */
406 pkt.index = cpu_to_le32(cpucp_pcie_throughput_tx);
407 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
408 HL_CPUCP_INFO_TIMEOUT_USEC, &result);
409 if (rc) {
410 dev_err(hdev->dev,
411 "Failed to handle CPU-CP PCI info pkt, error %d\n", rc);
412 return rc;
414 counters->tx_throughput = result;
416 /* Fetch PCI replay counter */
417 pkt.ctl = cpu_to_le32(CPUCP_PACKET_PCIE_REPLAY_CNT_GET <<
418 CPUCP_PKT_CTL_OPCODE_SHIFT);
420 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
421 HL_CPUCP_INFO_TIMEOUT_USEC, &result);
422 if (rc) {
423 dev_err(hdev->dev,
424 "Failed to handle CPU-CP PCI info pkt, error %d\n", rc);
425 return rc;
427 counters->replay_cnt = (u32) result;
429 return rc;
432 int hl_fw_cpucp_total_energy_get(struct hl_device *hdev, u64 *total_energy)
434 struct cpucp_packet pkt = {};
435 u64 result;
436 int rc;
438 pkt.ctl = cpu_to_le32(CPUCP_PACKET_TOTAL_ENERGY_GET <<
439 CPUCP_PKT_CTL_OPCODE_SHIFT);
441 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
442 HL_CPUCP_INFO_TIMEOUT_USEC, &result);
443 if (rc) {
444 dev_err(hdev->dev,
445 "Failed to handle CpuCP total energy pkt, error %d\n",
446 rc);
447 return rc;
450 *total_energy = result;
452 return rc;
455 int hl_fw_cpucp_pll_info_get(struct hl_device *hdev, u16 pll_index,
456 u16 *pll_freq_arr)
458 struct cpucp_packet pkt;
459 u64 result;
460 int rc;
462 memset(&pkt, 0, sizeof(pkt));
464 pkt.ctl = cpu_to_le32(CPUCP_PACKET_PLL_INFO_GET <<
465 CPUCP_PKT_CTL_OPCODE_SHIFT);
466 pkt.pll_type = __cpu_to_le16(pll_index);
468 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
469 HL_CPUCP_INFO_TIMEOUT_USEC, &result);
470 if (rc)
471 dev_err(hdev->dev, "Failed to read PLL info, error %d\n", rc);
473 pll_freq_arr[0] = FIELD_GET(CPUCP_PKT_RES_PLL_OUT0_MASK, result);
474 pll_freq_arr[1] = FIELD_GET(CPUCP_PKT_RES_PLL_OUT1_MASK, result);
475 pll_freq_arr[2] = FIELD_GET(CPUCP_PKT_RES_PLL_OUT2_MASK, result);
476 pll_freq_arr[3] = FIELD_GET(CPUCP_PKT_RES_PLL_OUT3_MASK, result);
478 return rc;
481 static void fw_read_errors(struct hl_device *hdev, u32 boot_err0_reg,
482 u32 cpu_security_boot_status_reg)
484 u32 err_val, security_val;
486 /* Some of the firmware status codes are deprecated in newer f/w
487 * versions. In those versions, the errors are reported
488 * in different registers. Therefore, we need to check those
489 * registers and print the exact errors. Moreover, there
490 * may be multiple errors, so we need to report on each error
491 * separately. Some of the error codes might indicate a state
492 * that is not an error per-se, but it is an error in production
493 * environment
495 err_val = RREG32(boot_err0_reg);
496 if (!(err_val & CPU_BOOT_ERR0_ENABLED))
497 return;
499 if (err_val & CPU_BOOT_ERR0_DRAM_INIT_FAIL)
500 dev_err(hdev->dev,
501 "Device boot error - DRAM initialization failed\n");
502 if (err_val & CPU_BOOT_ERR0_FIT_CORRUPTED)
503 dev_err(hdev->dev, "Device boot error - FIT image corrupted\n");
504 if (err_val & CPU_BOOT_ERR0_TS_INIT_FAIL)
505 dev_err(hdev->dev,
506 "Device boot error - Thermal Sensor initialization failed\n");
507 if (err_val & CPU_BOOT_ERR0_DRAM_SKIPPED)
508 dev_warn(hdev->dev,
509 "Device boot warning - Skipped DRAM initialization\n");
510 if (err_val & CPU_BOOT_ERR0_BMC_WAIT_SKIPPED)
511 dev_warn(hdev->dev,
512 "Device boot error - Skipped waiting for BMC\n");
513 if (err_val & CPU_BOOT_ERR0_NIC_DATA_NOT_RDY)
514 dev_err(hdev->dev,
515 "Device boot error - Serdes data from BMC not available\n");
516 if (err_val & CPU_BOOT_ERR0_NIC_FW_FAIL)
517 dev_err(hdev->dev,
518 "Device boot error - NIC F/W initialization failed\n");
519 if (err_val & CPU_BOOT_ERR0_SECURITY_NOT_RDY)
520 dev_warn(hdev->dev,
521 "Device boot warning - security not ready\n");
522 if (err_val & CPU_BOOT_ERR0_SECURITY_FAIL)
523 dev_err(hdev->dev, "Device boot error - security failure\n");
524 if (err_val & CPU_BOOT_ERR0_EFUSE_FAIL)
525 dev_err(hdev->dev, "Device boot error - eFuse failure\n");
527 security_val = RREG32(cpu_security_boot_status_reg);
528 if (security_val & CPU_BOOT_DEV_STS0_ENABLED)
529 dev_dbg(hdev->dev, "Device security status %#x\n",
530 security_val);
533 static void detect_cpu_boot_status(struct hl_device *hdev, u32 status)
535 /* Some of the status codes below are deprecated in newer f/w
536 * versions but we keep them here for backward compatibility
538 switch (status) {
539 case CPU_BOOT_STATUS_NA:
540 dev_err(hdev->dev,
541 "Device boot error - BTL did NOT run\n");
542 break;
543 case CPU_BOOT_STATUS_IN_WFE:
544 dev_err(hdev->dev,
545 "Device boot error - Stuck inside WFE loop\n");
546 break;
547 case CPU_BOOT_STATUS_IN_BTL:
548 dev_err(hdev->dev,
549 "Device boot error - Stuck in BTL\n");
550 break;
551 case CPU_BOOT_STATUS_IN_PREBOOT:
552 dev_err(hdev->dev,
553 "Device boot error - Stuck in Preboot\n");
554 break;
555 case CPU_BOOT_STATUS_IN_SPL:
556 dev_err(hdev->dev,
557 "Device boot error - Stuck in SPL\n");
558 break;
559 case CPU_BOOT_STATUS_IN_UBOOT:
560 dev_err(hdev->dev,
561 "Device boot error - Stuck in u-boot\n");
562 break;
563 case CPU_BOOT_STATUS_DRAM_INIT_FAIL:
564 dev_err(hdev->dev,
565 "Device boot error - DRAM initialization failed\n");
566 break;
567 case CPU_BOOT_STATUS_UBOOT_NOT_READY:
568 dev_err(hdev->dev,
569 "Device boot error - u-boot stopped by user\n");
570 break;
571 case CPU_BOOT_STATUS_TS_INIT_FAIL:
572 dev_err(hdev->dev,
573 "Device boot error - Thermal Sensor initialization failed\n");
574 break;
575 default:
576 dev_err(hdev->dev,
577 "Device boot error - Invalid status code %d\n",
578 status);
579 break;
583 int hl_fw_read_preboot_status(struct hl_device *hdev, u32 cpu_boot_status_reg,
584 u32 cpu_security_boot_status_reg, u32 boot_err0_reg,
585 u32 timeout)
587 struct asic_fixed_properties *prop = &hdev->asic_prop;
588 u32 status, security_status;
589 int rc;
591 if (!hdev->cpu_enable)
592 return 0;
594 /* Need to check two possible scenarios:
596 * CPU_BOOT_STATUS_WAITING_FOR_BOOT_FIT - for newer firmwares where
597 * the preboot is waiting for the boot fit
599 * All other status values - for older firmwares where the uboot was
600 * loaded from the FLASH
602 rc = hl_poll_timeout(
603 hdev,
604 cpu_boot_status_reg,
605 status,
606 (status == CPU_BOOT_STATUS_IN_UBOOT) ||
607 (status == CPU_BOOT_STATUS_DRAM_RDY) ||
608 (status == CPU_BOOT_STATUS_NIC_FW_RDY) ||
609 (status == CPU_BOOT_STATUS_READY_TO_BOOT) ||
610 (status == CPU_BOOT_STATUS_SRAM_AVAIL) ||
611 (status == CPU_BOOT_STATUS_WAITING_FOR_BOOT_FIT),
612 10000,
613 timeout);
615 if (rc) {
616 dev_err(hdev->dev, "Failed to read preboot version\n");
617 detect_cpu_boot_status(hdev, status);
618 fw_read_errors(hdev, boot_err0_reg,
619 cpu_security_boot_status_reg);
620 return -EIO;
623 rc = hdev->asic_funcs->read_device_fw_version(hdev, FW_COMP_PREBOOT);
624 if (rc)
625 return rc;
627 security_status = RREG32(cpu_security_boot_status_reg);
629 /* We read security status multiple times during boot:
630 * 1. preboot - a. Check whether the security status bits are valid
631 * b. Check whether fw security is enabled
632 * c. Check whether hard reset is done by preboot
633 * 2. boot cpu - a. Fetch boot cpu security status
634 * b. Check whether hard reset is done by boot cpu
635 * 3. FW application - a. Fetch fw application security status
636 * b. Check whether hard reset is done by fw app
638 * Preboot:
639 * Check security status bit (CPU_BOOT_DEV_STS0_ENABLED), if it is set
640 * check security enabled bit (CPU_BOOT_DEV_STS0_SECURITY_EN)
642 if (security_status & CPU_BOOT_DEV_STS0_ENABLED) {
643 prop->fw_security_status_valid = 1;
645 if (security_status & CPU_BOOT_DEV_STS0_SECURITY_EN)
646 prop->fw_security_disabled = false;
647 else
648 prop->fw_security_disabled = true;
650 if (security_status & CPU_BOOT_DEV_STS0_FW_HARD_RST_EN)
651 prop->hard_reset_done_by_fw = true;
652 } else {
653 prop->fw_security_status_valid = 0;
654 prop->fw_security_disabled = true;
657 dev_dbg(hdev->dev, "Firmware preboot hard-reset is %s\n",
658 prop->hard_reset_done_by_fw ? "enabled" : "disabled");
660 dev_info(hdev->dev, "firmware-level security is %s\n",
661 prop->fw_security_disabled ? "disabled" : "enabled");
663 return 0;
666 int hl_fw_init_cpu(struct hl_device *hdev, u32 cpu_boot_status_reg,
667 u32 msg_to_cpu_reg, u32 cpu_msg_status_reg,
668 u32 cpu_security_boot_status_reg, u32 boot_err0_reg,
669 bool skip_bmc, u32 cpu_timeout, u32 boot_fit_timeout)
671 struct asic_fixed_properties *prop = &hdev->asic_prop;
672 u32 status;
673 int rc;
675 if (!(hdev->fw_loading & FW_TYPE_BOOT_CPU))
676 return 0;
678 dev_info(hdev->dev, "Going to wait for device boot (up to %lds)\n",
679 cpu_timeout / USEC_PER_SEC);
681 /* Wait for boot FIT request */
682 rc = hl_poll_timeout(
683 hdev,
684 cpu_boot_status_reg,
685 status,
686 status == CPU_BOOT_STATUS_WAITING_FOR_BOOT_FIT,
687 10000,
688 boot_fit_timeout);
690 if (rc) {
691 dev_dbg(hdev->dev,
692 "No boot fit request received, resuming boot\n");
693 } else {
694 rc = hdev->asic_funcs->load_boot_fit_to_device(hdev);
695 if (rc)
696 goto out;
698 /* Clear device CPU message status */
699 WREG32(cpu_msg_status_reg, CPU_MSG_CLR);
701 /* Signal device CPU that boot loader is ready */
702 WREG32(msg_to_cpu_reg, KMD_MSG_FIT_RDY);
704 /* Poll for CPU device ack */
705 rc = hl_poll_timeout(
706 hdev,
707 cpu_msg_status_reg,
708 status,
709 status == CPU_MSG_OK,
710 10000,
711 boot_fit_timeout);
713 if (rc) {
714 dev_err(hdev->dev,
715 "Timeout waiting for boot fit load ack\n");
716 goto out;
719 /* Clear message */
720 WREG32(msg_to_cpu_reg, KMD_MSG_NA);
723 /* Make sure CPU boot-loader is running */
724 rc = hl_poll_timeout(
725 hdev,
726 cpu_boot_status_reg,
727 status,
728 (status == CPU_BOOT_STATUS_DRAM_RDY) ||
729 (status == CPU_BOOT_STATUS_NIC_FW_RDY) ||
730 (status == CPU_BOOT_STATUS_READY_TO_BOOT) ||
731 (status == CPU_BOOT_STATUS_SRAM_AVAIL),
732 10000,
733 cpu_timeout);
735 dev_dbg(hdev->dev, "uboot status = %d\n", status);
737 /* Read U-Boot version now in case we will later fail */
738 hdev->asic_funcs->read_device_fw_version(hdev, FW_COMP_UBOOT);
740 /* Clear reset status since we need to read it again from boot CPU */
741 prop->hard_reset_done_by_fw = false;
743 /* Read boot_cpu security bits */
744 if (prop->fw_security_status_valid) {
745 prop->fw_boot_cpu_security_map =
746 RREG32(cpu_security_boot_status_reg);
748 if (prop->fw_boot_cpu_security_map &
749 CPU_BOOT_DEV_STS0_FW_HARD_RST_EN)
750 prop->hard_reset_done_by_fw = true;
753 dev_dbg(hdev->dev, "Firmware boot CPU hard-reset is %s\n",
754 prop->hard_reset_done_by_fw ? "enabled" : "disabled");
756 if (rc) {
757 detect_cpu_boot_status(hdev, status);
758 rc = -EIO;
759 goto out;
762 if (!(hdev->fw_loading & FW_TYPE_LINUX)) {
763 dev_info(hdev->dev, "Skip loading Linux F/W\n");
764 goto out;
767 if (status == CPU_BOOT_STATUS_SRAM_AVAIL)
768 goto out;
770 dev_info(hdev->dev,
771 "Loading firmware to device, may take some time...\n");
773 rc = hdev->asic_funcs->load_firmware_to_device(hdev);
774 if (rc)
775 goto out;
777 if (skip_bmc) {
778 WREG32(msg_to_cpu_reg, KMD_MSG_SKIP_BMC);
780 rc = hl_poll_timeout(
781 hdev,
782 cpu_boot_status_reg,
783 status,
784 (status == CPU_BOOT_STATUS_BMC_WAITING_SKIPPED),
785 10000,
786 cpu_timeout);
788 if (rc) {
789 dev_err(hdev->dev,
790 "Failed to get ACK on skipping BMC, %d\n",
791 status);
792 WREG32(msg_to_cpu_reg, KMD_MSG_NA);
793 rc = -EIO;
794 goto out;
798 WREG32(msg_to_cpu_reg, KMD_MSG_FIT_RDY);
800 rc = hl_poll_timeout(
801 hdev,
802 cpu_boot_status_reg,
803 status,
804 (status == CPU_BOOT_STATUS_SRAM_AVAIL),
805 10000,
806 cpu_timeout);
808 /* Clear message */
809 WREG32(msg_to_cpu_reg, KMD_MSG_NA);
811 if (rc) {
812 if (status == CPU_BOOT_STATUS_FIT_CORRUPTED)
813 dev_err(hdev->dev,
814 "Device reports FIT image is corrupted\n");
815 else
816 dev_err(hdev->dev,
817 "Failed to load firmware to device, %d\n",
818 status);
820 rc = -EIO;
821 goto out;
824 /* Clear reset status since we need to read again from app */
825 prop->hard_reset_done_by_fw = false;
827 /* Read FW application security bits */
828 if (prop->fw_security_status_valid) {
829 prop->fw_app_security_map =
830 RREG32(cpu_security_boot_status_reg);
832 if (prop->fw_app_security_map &
833 CPU_BOOT_DEV_STS0_FW_HARD_RST_EN)
834 prop->hard_reset_done_by_fw = true;
837 dev_dbg(hdev->dev, "Firmware application CPU hard-reset is %s\n",
838 prop->hard_reset_done_by_fw ? "enabled" : "disabled");
840 dev_info(hdev->dev, "Successfully loaded firmware to device\n");
842 out:
843 fw_read_errors(hdev, boot_err0_reg, cpu_security_boot_status_reg);
845 return rc;