/** * hl_fw_version_cmp() - compares the FW version to a specific version * * @hdev: pointer to hl_device structure * @major: major number of a reference version * @minor: minor number of a reference version * @subminor: sub-minor number of a reference version * * Return 1 if FW version greater than the reference version, -1 if it's * smaller and 0 if versions are identical.
*/ int hl_fw_version_cmp(struct hl_device *hdev, u32 major, u32 minor, u32 subminor)
{ if (hdev->fw_sw_major_ver != major) return (hdev->fw_sw_major_ver > major) ? 1 : -1;
fw_ver = kmalloc(VERSION_MAX_LEN, GFP_KERNEL); if (!fw_ver) return NULL;
str = strnstr(fw_str, "fw-", VERSION_MAX_LEN); if (!str) goto free_fw_ver;
/* Skip the fw- part */
str += 3;
ver_offset = str - fw_str;
/* Copy until the next whitespace */
whitespace = strnstr(str, " ", VERSION_MAX_LEN - ver_offset); if (!whitespace) goto free_fw_ver;
strscpy(fw_ver, str, whitespace - str + 1);
return fw_ver;
free_fw_ver:
kfree(fw_ver); return NULL;
}
/** * extract_u32_until_given_char() - given a string of the format "<u32><char>*", extract the u32. * @str: the given string * @ver_num: the pointer to the extracted u32 to be returned to the caller. * @given_char: the given char at the end of the u32 in the string * * Return: Upon success, return a pointer to the given_char in the string. Upon failure, return NULL
*/ staticchar *extract_u32_until_given_char(char *str, u32 *ver_num, char given_char)
{ char num_str[8] = {}, *ch;
/** * hl_get_sw_major_minor_subminor() - extract the FW's SW version major, minor, sub-minor * from the version string * @hdev: pointer to the hl_device * @fw_str: the FW's version string * * The extracted version is set in the hdev fields: fw_sw_{major/minor/sub_minor}_ver. * * fw_str is expected to have one of two possible formats, examples: * 1) 'Preboot version hl-gaudi2-1.9.0-fw-42.0.1-sec-3' * 2) 'Preboot version hl-gaudi2-1.9.0-rc-fw-42.0.1-sec-3' * In those examples, the SW major,minor,subminor are correspondingly: 1,9,0. * * Return: 0 for success or a negative error code for failure.
*/ staticint hl_get_sw_major_minor_subminor(struct hl_device *hdev, constchar *fw_str)
{ char *end, *start;
end = strnstr(fw_str, "-rc-", VERSION_MAX_LEN); if (end == fw_str) return -EINVAL;
if (!end)
end = strnstr(fw_str, "-fw-", VERSION_MAX_LEN);
if (end == fw_str) return -EINVAL;
if (!end) return -EINVAL;
for (start = end - 1; start != fw_str; start--) { if (*start == '-') break;
}
if (start == fw_str) return -EINVAL;
/* start/end point each to the starting and ending hyphen of the sw version e.g. -1.9.0- */
start++;
start = extract_u32_until_given_char(start, &hdev->fw_sw_major_ver, '.'); if (!start) goto err_zero_ver;
start++;
start = extract_u32_until_given_char(start, &hdev->fw_sw_minor_ver, '.'); if (!start) goto err_zero_ver;
start++;
start = extract_u32_until_given_char(start, &hdev->fw_sw_sub_minor_ver, '-'); if (!start) goto err_zero_ver;
/** * hl_get_preboot_major_minor() - extract the FW's version major, minor from the version string. * @hdev: pointer to the hl_device * @preboot_ver: the FW's version string * * preboot_ver is expected to be the format of <major>.<minor>.<sub minor>*, e.g: 42.0.1-sec-3 * The extracted version is set in the hdev fields: fw_inner_{major/minor}_ver. * * Return: 0 on success, negative error code for failure.
*/ staticint hl_get_preboot_major_minor(struct hl_device *hdev, char *preboot_ver)
{
preboot_ver = extract_u32_until_given_char(preboot_ver, &hdev->fw_inner_major_ver, '.'); if (!preboot_ver) {
dev_err(hdev->dev, "Error parsing preboot major version\n"); goto err_zero_ver;
}
preboot_ver++;
preboot_ver = extract_u32_until_given_char(preboot_ver, &hdev->fw_inner_minor_ver, '.'); if (!preboot_ver) {
dev_err(hdev->dev, "Error parsing preboot minor version\n"); goto err_zero_ver;
} return 0;
/** * hl_release_firmware() - release FW * * @fw: fw descriptor * * note: this inline function added to serve as a comprehensive mirror for the * hl_request_fw function.
*/ staticinlinevoid hl_release_firmware(conststruct firmware *fw)
{
release_firmware(fw);
}
/** * hl_fw_copy_fw_to_device() - copy FW to device * * @hdev: pointer to hl_device structure. * @fw: fw descriptor * @dst: IO memory mapped address space to copy firmware to * @src_offset: offset in src FW to copy from * @size: amount of bytes to copy (0 to copy the whole binary) * * actual copy of FW binary data to device, shared by static and dynamic loaders
*/ staticint hl_fw_copy_fw_to_device(struct hl_device *hdev, conststruct firmware *fw, void __iomem *dst,
u32 src_offset, u32 size)
{ constvoid *fw_data;
/* size 0 indicates to copy the whole file */ if (!size)
size = fw->size;
if (src_offset + size > fw->size) {
dev_err(hdev->dev, "size to copy(%u) and offset(%u) are invalid\n",
size, src_offset); return -EINVAL;
}
/** * hl_fw_copy_msg_to_device() - copy message to device * * @hdev: pointer to hl_device structure. * @msg: message * @dst: IO memory mapped address space to copy firmware to * @src_offset: offset in src message to copy from * @size: amount of bytes to copy (0 to copy the whole binary) * * actual copy of message data to device.
*/ staticint hl_fw_copy_msg_to_device(struct hl_device *hdev, struct lkd_msg_comms *msg, void __iomem *dst,
u32 src_offset, u32 size)
{ void *msg_data;
/* size 0 indicates to copy the whole file */ if (!size)
size = sizeof(struct lkd_msg_comms);
if (src_offset + size > sizeof(struct lkd_msg_comms)) {
dev_err(hdev->dev, "size to copy(%u) and offset(%u) are invalid\n",
size, src_offset); return -EINVAL;
}
msg_data = (void *) msg;
memcpy_toio(dst, msg_data + src_offset, size);
return 0;
}
/** * hl_fw_load_fw_to_device() - Load F/W code to device's memory. * * @hdev: pointer to hl_device structure. * @fw_name: the firmware image name * @dst: IO memory mapped address space to copy firmware to * @src_offset: offset in src FW to copy from * @size: amount of bytes to copy (0 to copy the whole binary) * * Copy fw code from firmware file to device memory. * * Return: 0 on success, non-zero for failure.
*/ int hl_fw_load_fw_to_device(struct hl_device *hdev, constchar *fw_name, void __iomem *dst, u32 src_offset, u32 size)
{ conststruct firmware *fw; int rc;
rc = hl_request_fw(hdev, &fw, fw_name); if (rc) return rc;
rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt), 0, NULL); if (rc)
dev_err(hdev->dev, "Failed to disable FW's PCI access\n");
return rc;
}
/** * hl_fw_send_cpu_message() - send CPU message to the device. * * @hdev: pointer to hl_device structure. * @hw_queue_id: HW queue ID * @msg: raw data of the message/packet * @size: size of @msg in bytes * @timeout_us: timeout in usec to wait for CPU reply on the message * @result: return code reported by FW * * send message to the device CPU. * * Return: 0 on success, non-zero for failure. * -ENOMEM: memory allocation failure * -EAGAIN: CPU is disabled (try again when enabled) * -ETIMEDOUT: timeout waiting for FW response * -EIO: protocol error
*/ int hl_fw_send_cpu_message(struct hl_device *hdev, u32 hw_queue_id, u32 *msg,
u16 size, u32 timeout_us, u64 *result)
{ struct hl_hw_queue *queue = &hdev->kernel_queues[hw_queue_id]; struct asic_fixed_properties *prop = &hdev->asic_prop;
u32 tmp, expected_ack_val, pi, opcode; struct cpucp_packet *pkt;
dma_addr_t pkt_dma_addr; struct hl_bd *sent_bd; int rc = 0, fw_rc;
pkt = hl_cpu_accessible_dma_pool_alloc(hdev, size, &pkt_dma_addr); if (!pkt) {
dev_err(hdev->dev, "Failed to allocate DMA memory for packet to CPU\n"); return -ENOMEM;
}
memcpy(pkt, msg, size);
mutex_lock(&hdev->send_cpu_message_lock);
/* CPU-CP messages can be sent during soft-reset */ if (hdev->disabled && !hdev->reset_info.in_compute_reset) goto out;
if (hdev->device_cpu_disabled) {
rc = -EAGAIN; goto out;
}
/* set fence to a non valid value */
pkt->fence = cpu_to_le32(UINT_MAX);
pi = queue->pi;
/* * The CPU queue is a synchronous queue with an effective depth of * a single entry (although it is allocated with room for multiple * entries). We lock on it using 'send_cpu_message_lock' which * serializes accesses to the CPU queue. * Which means that we don't need to lock the access to the entire H/W * queues module when submitting a JOB to the CPU queue.
*/
hl_hw_queue_submit_bd(hdev, queue, hl_queue_inc_ptr(queue->pi), size, pkt_dma_addr);
if (rc == -ETIMEDOUT) { /* If FW performed reset just before sending it a packet, we will get a timeout. * This is expected behavior, hence no need for error message.
*/ if (!hl_device_operational(hdev, NULL) && !hdev->reset_info.in_compute_reset) {
dev_dbg(hdev->dev, "Device CPU packet timeout (0x%x) due to FW reset\n",
tmp);
} else { struct hl_bd *bd = queue->kernel_address;
if (!prop->supports_advanced_cpucp_rc) {
dev_dbg(hdev->dev, "F/W ERROR %d for CPU packet %d\n", rc, opcode);
rc = -EIO; goto scrub_descriptor;
}
switch (fw_rc) { case cpucp_packet_invalid:
dev_err(hdev->dev, "CPU packet %d is not supported by F/W\n", opcode); break; case cpucp_packet_fault:
dev_err(hdev->dev, "F/W failed processing CPU packet %d\n", opcode); break; case cpucp_packet_invalid_pkt:
dev_dbg(hdev->dev, "CPU packet %d is not supported by F/W\n", opcode); break; case cpucp_packet_invalid_params:
dev_err(hdev->dev, "F/W reports invalid parameters for CPU packet %d\n", opcode); break;
default:
dev_err(hdev->dev, "Unknown F/W ERROR %d for CPU packet %d\n", rc, opcode);
}
/* propagate the return code from the f/w to the callers who want to check it */ if (result)
*result = fw_rc;
scrub_descriptor: /* Scrub previous buffer descriptor 'ctl' field which contains the * previous PI value written during packet submission. * We must do this or else F/W can read an old value upon queue wraparound.
*/
sent_bd = queue->kernel_address;
sent_bd += hl_pi_2_offset(pi);
sent_bd->ctl = cpu_to_le32(UINT_MAX);
out:
mutex_unlock(&hdev->send_cpu_message_lock);
hl_cpu_accessible_dma_pool_free(hdev, size, pkt);
return rc;
}
int hl_fw_unmask_irq(struct hl_device *hdev, u16 event_type)
{ struct cpucp_packet pkt;
u64 result; int rc;
/* data should be aligned to 8 bytes in order to CPU-CP to copy it */
total_pkt_size = (total_pkt_size + 0x7) & ~0x7;
/* total_pkt_size is casted to u16 later on */ if (total_pkt_size > USHRT_MAX) {
dev_err(hdev->dev, "too many elements in IRQ array\n"); return -EINVAL;
}
pkt = kzalloc(total_pkt_size, GFP_KERNEL); if (!pkt) return -ENOMEM;
if (err_val & CPU_BOOT_ERR0_BMC_WAIT_SKIPPED) { if (hdev->bmc_enable) {
dev_err(hdev->dev, "Device boot error - Skipped waiting for BMC\n");
} else {
dev_info(hdev->dev, "Device boot message - Skipped waiting for BMC\n"); /* This is an info so we don't want it to disable the * device
*/
err_val &= ~CPU_BOOT_ERR0_BMC_WAIT_SKIPPED;
}
}
if (err_val & CPU_BOOT_ERR0_NIC_DATA_NOT_RDY)
dev_err(hdev->dev, "Device boot error - Serdes data from BMC not available\n");
if (err_val & CPU_BOOT_ERR0_NIC_FW_FAIL)
dev_err(hdev->dev, "Device boot error - NIC F/W initialization failed\n");
if (err_val & CPU_BOOT_ERR0_SECURITY_NOT_RDY)
dev_err(hdev->dev, "Device boot warning - security not ready\n");
/* All warnings should go here in order not to reach the unknown error validation */ if (err_val & CPU_BOOT_ERR0_EEPROM_FAIL) {
dev_err(hdev->dev, "Device boot error - EEPROM failure detected\n");
err_exists = true;
}
if (err_val & CPU_BOOT_ERR0_PRI_IMG_VER_FAIL)
dev_warn(hdev->dev, "Device boot warning - Failed to load preboot primary image\n");
if (err_val & CPU_BOOT_ERR_FATAL_MASK)
err_exists = true;
/* return error only if it's in the predefined mask */ if (err_exists && ((err_val & ~CPU_BOOT_ERR0_ENABLED) &
lower_32_bits(hdev->boot_error_status_mask))) returntrue;
returnfalse;
}
/* placeholder for ERR1 as no errors defined there yet */ staticbool fw_report_boot_dev1(struct hl_device *hdev, u32 err_val,
u32 sts_val)
{ /* * keep this variable to preserve the logic of the function. * this way it would require less modifications when error will be * added to DEV_ERR1
*/ bool err_exists = false;
if (!(err_val & CPU_BOOT_ERR1_ENABLED)) returnfalse;
if (sts_val & CPU_BOOT_DEV_STS1_ENABLED)
dev_dbg(hdev->dev, "Device status1 %#x\n", sts_val);
/* return error only if it's in the predefined mask */ if (err_exists && ((err_val & ~CPU_BOOT_ERR1_ENABLED) &
upper_32_bits(hdev->boot_error_status_mask))) returntrue;
/* Some of the firmware status codes are deprecated in newer f/w * versions. In those versions, the errors are reported * in different registers. Therefore, we need to check those * registers and print the exact errors. Moreover, there * may be multiple errors, so we need to report on each error * separately. Some of the error codes might indicate a state * that is not an error per-se, but it is an error in production * environment
*/
err_val = RREG32(boot_err0_reg);
status_val = RREG32(cpu_boot_dev_status0_reg);
err_exists = fw_report_boot_dev0(hdev, err_val, status_val);
/* data should be aligned to 8 bytes in order to CPU-CP to copy it */
total_pkt_size = (total_pkt_size + 0x7) & ~0x7;
/* total_pkt_size is casted to u16 later on */ if (total_pkt_size > USHRT_MAX) {
dev_err(hdev->dev, "CPUCP array data is too big\n"); return -EINVAL;
}
pkt = kzalloc(total_pkt_size, GFP_KERNEL); if (!pkt) return -ENOMEM;
/* * in case packet result is invalid it means that FW does not support * this feature and will use default/hard coded MSI values. no reason * to stop the boot
*/ if (rc && result == cpucp_packet_invalid)
rc = 0;
if (rc)
dev_err(hdev->dev, "failed to send CPUCP array data\n");
kfree(pkt);
return rc;
}
int hl_fw_cpucp_handshake(struct hl_device *hdev,
u32 sts_boot_dev_sts0_reg,
u32 sts_boot_dev_sts1_reg, u32 boot_err0_reg,
u32 boot_err1_reg)
{ int rc;
if (!dynamic_pll) { /* * in case we are working with legacy FW (each asic has unique * PLL numbering) use the driver based index as they are * aligned with fw legacy numbering
*/
*pll_index = input_pll_index; return 0;
}
/* retrieve a FW compatible PLL index based on * ASIC specific user request
*/
fw_pll_idx = hdev->asic_funcs->map_pll_idx_to_fw_idx(input_pll_index); if (fw_pll_idx < 0) {
dev_err(hdev->dev, "Invalid PLL index (%u) error %d\n",
input_pll_index, fw_pll_idx); return -EINVAL;
}
/* PLL map is a u8 array */
pll_byte = prop->cpucp_info.pll_map[fw_pll_idx >> 3];
pll_bit_off = fw_pll_idx & 0x7;
if (!(pll_byte & BIT(pll_bit_off))) {
dev_err(hdev->dev, "PLL index %d is not supported\n",
fw_pll_idx); return -EINVAL;
}
*pll_index = fw_pll_idx;
return 0;
}
int hl_fw_cpucp_pll_info_get(struct hl_device *hdev, u32 pll_index,
u16 *pll_freq_arr)
{ struct cpucp_packet pkt; enum pll_index used_pll_idx;
u64 result; int rc;
rc = get_used_pll_index(hdev, pll_index, &used_pll_idx); if (rc) return rc;
/* Stop device CPU to make sure nothing bad happens */ if (hdev->asic_prop.dynamic_fw_load) {
pre_fw_load = &fw_loader->pre_fw_load;
cpu_timeout = fw_loader->cpu_timeout;
cpu_boot_status_reg = pre_fw_load->cpu_boot_status_reg;
/* Must clear this register in order to prevent preboot * from reading WFE after reboot
*/
WREG32(static_loader->kmd_msg_to_cpu_reg, KMD_MSG_NA);
}
hdev->device_cpu_is_halted = true;
}
staticvoid detect_cpu_boot_status(struct hl_device *hdev, u32 status)
{ /* Some of the status codes below are deprecated in newer f/w * versions but we keep them here for backward compatibility
*/ switch (status) { case CPU_BOOT_STATUS_NA:
dev_err(hdev->dev, "Device boot progress - BTL/ROM did NOT run\n"); break; case CPU_BOOT_STATUS_IN_WFE:
dev_err(hdev->dev, "Device boot progress - Stuck inside WFE loop\n"); break; case CPU_BOOT_STATUS_IN_BTL:
dev_err(hdev->dev, "Device boot progress - Stuck in BTL\n"); break; case CPU_BOOT_STATUS_IN_PREBOOT:
dev_err(hdev->dev, "Device boot progress - Stuck in Preboot\n"); break; case CPU_BOOT_STATUS_IN_SPL:
dev_err(hdev->dev, "Device boot progress - Stuck in SPL\n"); break; case CPU_BOOT_STATUS_IN_UBOOT:
dev_err(hdev->dev, "Device boot progress - Stuck in u-boot\n"); break; case CPU_BOOT_STATUS_DRAM_INIT_FAIL:
dev_err(hdev->dev, "Device boot progress - DRAM initialization failed\n"); break; case CPU_BOOT_STATUS_UBOOT_NOT_READY:
dev_err(hdev->dev, "Device boot progress - Cannot boot\n"); break; case CPU_BOOT_STATUS_TS_INIT_FAIL:
dev_err(hdev->dev, "Device boot progress - Thermal Sensor initialization failed\n"); break; case CPU_BOOT_STATUS_SECURITY_READY:
dev_err(hdev->dev, "Device boot progress - Stuck in preboot after security initialization\n"); break; case CPU_BOOT_STATUS_FW_SHUTDOWN_PREP:
dev_err(hdev->dev, "Device boot progress - Stuck in preparation for shutdown\n"); break; default:
dev_err(hdev->dev, "Device boot progress - Invalid or unexpected status code %d\n", status); break;
}
}
int hl_fw_wait_preboot_ready(struct hl_device *hdev)
{ struct pre_fw_load_props *pre_fw_load = &hdev->fw_loader.pre_fw_load;
u32 status = 0, timeout; int rc, tries = 1, fw_err = 0; bool preboot_still_runs;
/* Need to check two possible scenarios: * * CPU_BOOT_STATUS_WAITING_FOR_BOOT_FIT - for newer firmwares where * the preboot is waiting for the boot fit * * All other status values - for older firmwares where the uboot was * loaded from the FLASH
*/
timeout = pre_fw_load->wait_for_preboot_timeout;
retry:
rc = hl_poll_timeout(
hdev,
pre_fw_load->cpu_boot_status_reg,
status,
(status == CPU_BOOT_STATUS_NIC_FW_RDY) ||
(status == CPU_BOOT_STATUS_READY_TO_BOOT) ||
(status == CPU_BOOT_STATUS_WAITING_FOR_BOOT_FIT),
hdev->fw_poll_interval_usec,
timeout); /* * if F/W reports "security-ready" it means preboot might take longer. * If the field 'wait_for_preboot_extended_timeout' is non 0 we wait again * with that timeout
*/
preboot_still_runs = (status == CPU_BOOT_STATUS_SECURITY_READY ||
status == CPU_BOOT_STATUS_IN_PREBOOT ||
status == CPU_BOOT_STATUS_FW_SHUTDOWN_PREP ||
status == CPU_BOOT_STATUS_DRAM_RDY);
if (rc && tries && preboot_still_runs) {
tries--; if (pre_fw_load->wait_for_preboot_extended_timeout) {
timeout = pre_fw_load->wait_for_preboot_extended_timeout; goto retry;
}
}
/* If we read all FF, then something is totally wrong, no point * of reading specific errors
*/ if (status != -1)
fw_err = fw_read_errors(hdev, pre_fw_load->boot_err0_reg,
pre_fw_load->boot_err1_reg,
pre_fw_load->sts_boot_dev_sts0_reg,
pre_fw_load->sts_boot_dev_sts1_reg); if (rc || fw_err) {
detect_cpu_boot_status(hdev, status);
dev_err(hdev->dev, "CPU boot %s (status = %d)\n",
fw_err ? "failed due to an error" : "ready timeout", status); return -EIO;
}
rc = hl_fw_wait_preboot_ready(hdev); if (rc) return rc;
/* * the registers DEV_STS* contain FW capabilities/features. * We can rely on this registers only if bit CPU_BOOT_DEV_STS*_ENABLED * is set. * In the first read of this register we store the value of this * register ONLY if the register is enabled (which will be propagated * to next stages) and also mark the register as valid. * In case it is not enabled the stored value will be left 0- all * caps/features are off
*/
reg_val = RREG32(pre_fw_load->sts_boot_dev_sts0_reg); if (reg_val & CPU_BOOT_DEV_STS0_ENABLED) {
prop->fw_cpu_boot_dev_sts0_valid = true;
prop->fw_preboot_cpu_boot_dev_sts0 = reg_val;
}
/* We read boot_dev_sts registers multiple times during boot: * 1. preboot - a. Check whether the security status bits are valid * b. Check whether fw security is enabled * c. Check whether hard reset is done by preboot * 2. boot cpu - a. Fetch boot cpu security status * b. Check whether hard reset is done by boot cpu * 3. FW application - a. Fetch fw application security status * b. Check whether hard reset is done by fw app
*/
prop->hard_reset_done_by_fw = !!(cpu_boot_dev_sts0 & CPU_BOOT_DEV_STS0_FW_HARD_RST_EN);
staticint hl_fw_static_read_preboot_status(struct hl_device *hdev)
{ int rc;
rc = hl_fw_static_read_device_fw_version(hdev, FW_COMP_PREBOOT); if (rc) return rc;
return 0;
}
int hl_fw_read_preboot_status(struct hl_device *hdev)
{ int rc;
if (!(hdev->fw_components & FW_TYPE_PREBOOT_CPU)) return 0;
/* get FW pre-load parameters */
hdev->asic_funcs->init_firmware_preload_params(hdev);
/* * In order to determine boot method (static VS dynamic) we need to * read the boot caps register
*/
rc = hl_fw_read_preboot_caps(hdev); if (rc) return rc;
hl_fw_preboot_update_state(hdev);
/* no need to read preboot status in dynamic load */ if (hdev->asic_prop.dynamic_fw_load) return 0;
return hl_fw_static_read_preboot_status(hdev);
}
/* associate string with COMM status */ staticchar *hl_dynamic_fw_status_str[COMMS_STS_INVLD_LAST] = {
[COMMS_STS_NOOP] = "NOOP",
[COMMS_STS_ACK] = "ACK",
[COMMS_STS_OK] = "OK",
[COMMS_STS_ERR] = "ERR",
[COMMS_STS_VALID_ERR] = "VALID_ERR",
[COMMS_STS_TIMEOUT_ERR] = "TIMEOUT_ERR",
};
/** * hl_fw_dynamic_report_error_status - report error status * * @hdev: pointer to the habanalabs device structure * @status: value of FW status register * @expected_status: the expected status
*/ staticvoid hl_fw_dynamic_report_error_status(struct hl_device *hdev,
u32 status, enum comms_sts expected_status)
{ enum comms_sts comm_status =
FIELD_GET(COMMS_STATUS_STATUS_MASK, status);
if (comm_status < COMMS_STS_INVLD_LAST)
dev_err(hdev->dev, "Device status %s, expected status: %s\n",
hl_dynamic_fw_status_str[comm_status],
hl_dynamic_fw_status_str[expected_status]); else
dev_err(hdev->dev, "Device status unknown %d, expected status: %s\n",
comm_status,
hl_dynamic_fw_status_str[expected_status]);
}
/** * hl_fw_dynamic_send_cmd - send LKD to FW cmd * * @hdev: pointer to the habanalabs device structure * @fw_loader: managing structure for loading device's FW * @cmd: LKD to FW cmd code * @size: size of next FW component to be loaded (0 if not necessary) * * LDK to FW exact command layout is defined at struct comms_command. * note: the size argument is used only when the next FW component should be * loaded, otherwise it shall be 0. the size is used by the FW in later * protocol stages and when sending only indicating the amount of memory * to be allocated by the FW to receive the next boot component.
*/ staticvoid hl_fw_dynamic_send_cmd(struct hl_device *hdev, struct fw_load_mgr *fw_loader, enum comms_cmd cmd, unsignedint size)
{ struct cpu_dyn_regs *dyn_regs;
u32 val;
/** * hl_fw_dynamic_extract_fw_response - update the FW response * * @hdev: pointer to the habanalabs device structure * @fw_loader: managing structure for loading device's FW * @response: FW response * @status: the status read from CPU status register * * @return 0 on success, otherwise non-zero error code
*/ staticint hl_fw_dynamic_extract_fw_response(struct hl_device *hdev, struct fw_load_mgr *fw_loader, struct fw_response *response,
u32 status)
{
response->status = FIELD_GET(COMMS_STATUS_STATUS_MASK, status);
response->ram_offset = FIELD_GET(COMMS_STATUS_OFFSET_MASK, status) <<
COMMS_STATUS_OFFSET_ALIGN_SHIFT;
response->ram_type = FIELD_GET(COMMS_STATUS_RAM_TYPE_MASK, status);
if ((response->ram_type != COMMS_SRAM) &&
(response->ram_type != COMMS_DRAM)) {
dev_err(hdev->dev, "FW status: invalid RAM type %u\n",
response->ram_type); return -EIO;
}
return 0;
}
/** * hl_fw_dynamic_wait_for_status - wait for status in dynamic FW load * * @hdev: pointer to the habanalabs device structure * @fw_loader: managing structure for loading device's FW * @expected_status: expected status to wait for * @timeout: timeout for status wait * * @return 0 on success, otherwise non-zero error code * * waiting for status from FW include polling the FW status register until * expected status is received or timeout occurs (whatever occurs first).
*/ staticint hl_fw_dynamic_wait_for_status(struct hl_device *hdev, struct fw_load_mgr *fw_loader, enum comms_sts expected_status,
u32 timeout)
{ struct cpu_dyn_regs *dyn_regs;
u32 status; int rc;
/** * hl_fw_dynamic_send_clear_cmd - send clear command to FW * * @hdev: pointer to the habanalabs device structure * @fw_loader: managing structure for loading device's FW * * @return 0 on success, otherwise non-zero error code * * after command cycle between LKD to FW CPU (i.e. LKD got an expected status * from FW) we need to clear the CPU status register in order to avoid garbage * between command cycles. * This is done by sending clear command and polling the CPU to LKD status * register to hold the status NOOP
*/ staticint hl_fw_dynamic_send_clear_cmd(struct hl_device *hdev, struct fw_load_mgr *fw_loader)
{
hl_fw_dynamic_send_cmd(hdev, fw_loader, COMMS_CLR_STS, 0);
/** * hl_fw_dynamic_send_protocol_cmd - send LKD to FW cmd and wait for ACK * * @hdev: pointer to the habanalabs device structure * @fw_loader: managing structure for loading device's FW * @cmd: LKD to FW cmd code * @size: size of next FW component to be loaded (0 if not necessary) * @wait_ok: if true also wait for OK response from FW * @timeout: timeout for status wait * * @return 0 on success, otherwise non-zero error code * * brief: * when sending protocol command we have the following steps: * - send clear (clear command and verify clear status register) * - send the actual protocol command * - wait for ACK on the protocol command * - send clear * - send NOOP * if, in addition, the specific protocol command should wait for OK then: * - wait for OK * - send clear * - send NOOP * * NOTES: * send clear: this is necessary in order to clear the status register to avoid * leftovers between command * NOOP command: necessary to avoid loop on the clear command by the FW
*/ int hl_fw_dynamic_send_protocol_cmd(struct hl_device *hdev, struct fw_load_mgr *fw_loader, enum comms_cmd cmd, unsignedint size, bool wait_ok, u32 timeout)
{ int rc;
/* first send clear command to clean former commands */
rc = hl_fw_dynamic_send_clear_cmd(hdev, fw_loader); if (rc) return rc;
/* send the actual command */
hl_fw_dynamic_send_cmd(hdev, fw_loader, cmd, size);
/* wait for ACK for the command */
rc = hl_fw_dynamic_wait_for_status(hdev, fw_loader, COMMS_STS_ACK,
timeout); if (rc) return rc;
/* clear command to prepare for NOOP command */
rc = hl_fw_dynamic_send_clear_cmd(hdev, fw_loader); if (rc) return rc;
/* send the actual NOOP command */
hl_fw_dynamic_send_cmd(hdev, fw_loader, COMMS_NOOP, 0);
if (!wait_ok) return 0;
rc = hl_fw_dynamic_wait_for_status(hdev, fw_loader, COMMS_STS_OK,
timeout); if (rc) return rc;
/* clear command to prepare for NOOP command */
rc = hl_fw_dynamic_send_clear_cmd(hdev, fw_loader); if (rc) return rc;
/* send the actual NOOP command */
hl_fw_dynamic_send_cmd(hdev, fw_loader, COMMS_NOOP, 0);
return 0;
}
/** * hl_fw_compat_crc32 - CRC compatible with FW * * @data: pointer to the data * @size: size of the data * * @return the CRC32 result * * NOTE: kernel's CRC32 differs from standard CRC32 calculation. * in order to be aligned we need to flip the bits of both the input * initial CRC and kernel's CRC32 result. * in addition both sides use initial CRC of 0,
*/ static u32 hl_fw_compat_crc32(u8 *data, size_t size)
{ return ~crc32_le(~((u32)0), data, size);
}
/** * hl_fw_dynamic_validate_memory_bound - validate memory bounds for memory * transfer (image or descriptor) between * host and FW * * @hdev: pointer to the habanalabs device structure * @addr: device address of memory transfer * @size: memory transfer size * @region: PCI memory region * * @return 0 on success, otherwise non-zero error code
*/ staticint hl_fw_dynamic_validate_memory_bound(struct hl_device *hdev,
u64 addr, size_t size, struct pci_mem_region *region)
{
u64 end_addr;
/* now make sure that the memory transfer is within region's bounds */
end_addr = addr + size; if (end_addr >= region->region_base + region->region_size) {
dev_err(hdev->dev, "dynamic FW load: memory transfer end address out of memory region bounds. addr: %llx\n",
end_addr); return -EIO;
}
/* * now make sure memory transfer is within predefined BAR bounds. * this is to make sure we do not need to set the bar (e.g. for DRAM * memory transfers)
*/ if (end_addr >= region->region_base - region->offset_in_bar +
region->bar_size) {
dev_err(hdev->dev, "FW image beyond PCI BAR bounds\n"); return -EIO;
}
return 0;
}
/** * hl_fw_dynamic_validate_descriptor - validate FW descriptor * * @hdev: pointer to the habanalabs device structure * @fw_loader: managing structure for loading device's FW * @fw_desc: the descriptor from FW * * @return 0 on success, otherwise non-zero error code
*/ staticint hl_fw_dynamic_validate_descriptor(struct hl_device *hdev, struct fw_load_mgr *fw_loader, struct lkd_fw_comms_desc *fw_desc)
{ struct pci_mem_region *region; enum pci_region region_id;
size_t data_size;
u32 data_crc32;
u8 *data_ptr;
u64 addr; int rc;
if (le32_to_cpu(fw_desc->header.magic) != HL_COMMS_DESC_MAGIC)
dev_dbg(hdev->dev, "Invalid magic for dynamic FW descriptor (%x)\n",
fw_desc->header.magic);
if (fw_desc->header.version != HL_COMMS_DESC_VER)
dev_dbg(hdev->dev, "Invalid version for dynamic FW descriptor (%x)\n",
fw_desc->header.version);
/* * Calc CRC32 of data without header. use the size of the descriptor * reported by firmware, without calculating it ourself, to allow adding * more fields to the lkd_fw_comms_desc structure. * note that no alignment/stride address issues here as all structures * are 64 bit padded.
*/
data_ptr = (u8 *)fw_desc + sizeof(struct comms_msg_header);
data_size = le16_to_cpu(fw_desc->header.size);
/* find memory region to which to copy the image */
addr = le64_to_cpu(fw_desc->img_addr);
region_id = hl_get_pci_memory_region(hdev, addr); if ((region_id != PCI_REGION_SRAM) && ((region_id != PCI_REGION_DRAM))) {
dev_err(hdev->dev, "Invalid region to copy FW image address=%llx\n", addr); return -EIO;
}
region = &hdev->pci_mem_region[region_id];
/* store the region for the copy stage */
fw_loader->dynamic_loader.image_region = region;
/* * here we know that the start address is valid, now make sure that the * image is within region's bounds
*/
rc = hl_fw_dynamic_validate_memory_bound(hdev, addr,
fw_loader->dynamic_loader.fw_image_size,
region); if (rc) {
dev_err(hdev->dev, "invalid mem transfer request for FW image\n"); return rc;
}
/* here we can mark the descriptor as valid as the content has been validated */
fw_loader->dynamic_loader.fw_desc_valid = true;
/* * validate that the descriptor is within region's bounds * Note that as the start address was supplied according to the RAM * type- testing only the end address is enough
*/
rc = hl_fw_dynamic_validate_memory_bound(hdev, device_addr, sizeof(struct lkd_fw_comms_desc),
region); return rc;
}
/* * hl_fw_dynamic_read_descriptor_msg - read and show the ascii msg that sent by fw * * @hdev: pointer to the habanalabs device structure * @fw_desc: the descriptor from FW
*/ staticvoid hl_fw_dynamic_read_descriptor_msg(struct hl_device *hdev, struct lkd_fw_comms_desc *fw_desc)
{ int i; char *msg;
for (i = 0 ; i < LKD_FW_ASCII_MSG_MAX ; i++) { if (!fw_desc->ascii_msg[i].valid) return;
rc = hl_fw_dynamic_validate_response(hdev, response, region); if (rc) {
dev_err(hdev->dev, "invalid mem transfer request for FW descriptor\n"); return rc;
}
/* * extract address to copy the descriptor from * in addition, as the descriptor value is going to be over-ridden by new data- we mark it * as invalid. * it will be marked again as valid once validated
*/
fw_loader->dynamic_loader.fw_desc_valid = false;
src = hdev->pcie_bar[region->bar_id] + region->offset_in_bar +
response->ram_offset;
/* * We do the copy of the fw descriptor in 2 phases: * 1. copy the header + data info according to our lkd_fw_comms_desc definition. * then we're able to read the actual data size provided by fw. * this is needed for cases where data in descriptor was changed(add/remove) * in embedded specs header file before updating lkd copy of the header file * 2. copy descriptor to temporary buffer with aligned size and send it to validation
*/
memcpy_fromio(fw_desc, src, sizeof(struct lkd_fw_comms_desc));
fw_data_size = le16_to_cpu(fw_desc->header.size);
temp_fw_desc = vzalloc(sizeof(struct comms_msg_header) + fw_data_size); if (!temp_fw_desc) return -ENOMEM;
if (!rc)
hl_fw_dynamic_read_descriptor_msg(hdev, temp_fw_desc);
vfree(temp_fw_desc);
return rc;
}
/** * hl_fw_dynamic_request_descriptor - handshake with CPU to get FW descriptor * * @hdev: pointer to the habanalabs device structure * @fw_loader: managing structure for loading device's FW * @next_image_size: size to allocate for next FW component * * @return 0 on success, otherwise non-zero error code
*/ staticint hl_fw_dynamic_request_descriptor(struct hl_device *hdev, struct fw_load_mgr *fw_loader,
size_t next_image_size)
{ int rc;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.