1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright 2016-2019 HabanaLabs, Ltd.
8 #include "habanalabs.h"
10 #include <linux/firmware.h>
11 #include <linux/genalloc.h>
12 #include <linux/io-64-nonatomic-lo-hi.h>
15 * hl_fw_push_fw_to_device() - Push FW code to device.
16 * @hdev: pointer to hl_device structure.
18 * Copy fw code from firmware file to device memory.
20 * Return: 0 on success, non-zero for failure.
22 int hl_fw_push_fw_to_device(struct hl_device *hdev, const char *fw_name,
25 const struct firmware *fw;
30 rc = request_firmware(&fw, fw_name, hdev->dev);
32 dev_err(hdev->dev, "Firmware file %s is not found!\n", fw_name);
37 if ((fw_size % 4) != 0) {
38 dev_err(hdev->dev, "Illegal %s firmware size %zu\n",
44 dev_dbg(hdev->dev, "%s firmware size == %zu\n", fw_name, fw_size);
46 fw_data = (const u64 *) fw->data;
48 memcpy_toio(dst, fw_data, fw_size);
55 int hl_fw_send_pci_access_msg(struct hl_device *hdev, u32 opcode)
57 struct armcp_packet pkt = {};
59 pkt.ctl = cpu_to_le32(opcode << ARMCP_PKT_CTL_OPCODE_SHIFT);
61 return hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt,
62 sizeof(pkt), HL_DEVICE_TIMEOUT_USEC, NULL);
65 int hl_fw_send_cpu_message(struct hl_device *hdev, u32 hw_queue_id, u32 *msg,
66 u16 len, u32 timeout, long *result)
68 struct armcp_packet *pkt;
69 dma_addr_t pkt_dma_addr;
73 pkt = hdev->asic_funcs->cpu_accessible_dma_pool_alloc(hdev, len,
77 "Failed to allocate DMA memory for packet to CPU\n");
81 memcpy(pkt, msg, len);
83 mutex_lock(&hdev->send_cpu_message_lock);
88 if (hdev->device_cpu_disabled) {
93 rc = hl_hw_queue_send_cb_no_cmpl(hdev, hw_queue_id, len, pkt_dma_addr);
95 dev_err(hdev->dev, "Failed to send CB on CPU PQ (%d)\n", rc);
99 rc = hl_poll_timeout_memory(hdev, &pkt->fence, tmp,
100 (tmp == ARMCP_PACKET_FENCE_VAL), 1000,
103 hl_hw_queue_inc_ci_kernel(hdev, hw_queue_id);
105 if (rc == -ETIMEDOUT) {
106 dev_err(hdev->dev, "Device CPU packet timeout (0x%x)\n", tmp);
107 hdev->device_cpu_disabled = true;
111 tmp = le32_to_cpu(pkt->ctl);
113 rc = (tmp & ARMCP_PKT_CTL_RC_MASK) >> ARMCP_PKT_CTL_RC_SHIFT;
115 dev_err(hdev->dev, "F/W ERROR %d for CPU packet %d\n",
117 (tmp & ARMCP_PKT_CTL_OPCODE_MASK)
118 >> ARMCP_PKT_CTL_OPCODE_SHIFT);
121 *result = (long) le64_to_cpu(pkt->result);
125 mutex_unlock(&hdev->send_cpu_message_lock);
127 hdev->asic_funcs->cpu_accessible_dma_pool_free(hdev, len, pkt);
132 int hl_fw_test_cpu_queue(struct hl_device *hdev)
134 struct armcp_packet test_pkt = {};
138 test_pkt.ctl = cpu_to_le32(ARMCP_PACKET_TEST <<
139 ARMCP_PKT_CTL_OPCODE_SHIFT);
140 test_pkt.value = cpu_to_le64(ARMCP_PACKET_FENCE_VAL);
142 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &test_pkt,
143 sizeof(test_pkt), HL_DEVICE_TIMEOUT_USEC, &result);
146 if (result == ARMCP_PACKET_FENCE_VAL)
148 "queue test on CPU queue succeeded\n");
151 "CPU queue test failed (0x%08lX)\n", result);
153 dev_err(hdev->dev, "CPU queue test failed, error %d\n", rc);
159 void *hl_fw_cpu_accessible_dma_pool_alloc(struct hl_device *hdev, size_t size,
160 dma_addr_t *dma_handle)
164 kernel_addr = gen_pool_alloc(hdev->cpu_accessible_dma_pool, size);
166 *dma_handle = hdev->cpu_accessible_dma_address +
167 (kernel_addr - (u64) (uintptr_t) hdev->cpu_accessible_dma_mem);
169 return (void *) (uintptr_t) kernel_addr;
172 void hl_fw_cpu_accessible_dma_pool_free(struct hl_device *hdev, size_t size,
175 gen_pool_free(hdev->cpu_accessible_dma_pool, (u64) (uintptr_t) vaddr,
179 int hl_fw_send_heartbeat(struct hl_device *hdev)
181 struct armcp_packet hb_pkt = {};
185 hb_pkt.ctl = cpu_to_le32(ARMCP_PACKET_TEST <<
186 ARMCP_PKT_CTL_OPCODE_SHIFT);
187 hb_pkt.value = cpu_to_le64(ARMCP_PACKET_FENCE_VAL);
189 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &hb_pkt,
190 sizeof(hb_pkt), HL_DEVICE_TIMEOUT_USEC, &result);
192 if ((rc) || (result != ARMCP_PACKET_FENCE_VAL))
198 int hl_fw_armcp_info_get(struct hl_device *hdev)
200 struct asic_fixed_properties *prop = &hdev->asic_prop;
201 struct armcp_packet pkt = {};
202 void *armcp_info_cpu_addr;
203 dma_addr_t armcp_info_dma_addr;
207 armcp_info_cpu_addr =
208 hdev->asic_funcs->cpu_accessible_dma_pool_alloc(hdev,
209 sizeof(struct armcp_info),
210 &armcp_info_dma_addr);
211 if (!armcp_info_cpu_addr) {
213 "Failed to allocate DMA memory for ArmCP info packet\n");
217 memset(armcp_info_cpu_addr, 0, sizeof(struct armcp_info));
219 pkt.ctl = cpu_to_le32(ARMCP_PACKET_INFO_GET <<
220 ARMCP_PKT_CTL_OPCODE_SHIFT);
221 pkt.addr = cpu_to_le64(armcp_info_dma_addr);
222 pkt.data_max_size = cpu_to_le32(sizeof(struct armcp_info));
224 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
225 HL_ARMCP_INFO_TIMEOUT_USEC, &result);
228 "Failed to send ArmCP info pkt, error %d\n", rc);
232 memcpy(&prop->armcp_info, armcp_info_cpu_addr,
233 sizeof(prop->armcp_info));
235 rc = hl_build_hwmon_channel_info(hdev, prop->armcp_info.sensors);
238 "Failed to build hwmon channel info, error %d\n", rc);
244 hdev->asic_funcs->cpu_accessible_dma_pool_free(hdev,
245 sizeof(struct armcp_info), armcp_info_cpu_addr);
250 int hl_fw_get_eeprom_data(struct hl_device *hdev, void *data, size_t max_size)
252 struct armcp_packet pkt = {};
253 void *eeprom_info_cpu_addr;
254 dma_addr_t eeprom_info_dma_addr;
258 eeprom_info_cpu_addr =
259 hdev->asic_funcs->cpu_accessible_dma_pool_alloc(hdev,
260 max_size, &eeprom_info_dma_addr);
261 if (!eeprom_info_cpu_addr) {
263 "Failed to allocate DMA memory for ArmCP EEPROM packet\n");
267 memset(eeprom_info_cpu_addr, 0, max_size);
269 pkt.ctl = cpu_to_le32(ARMCP_PACKET_EEPROM_DATA_GET <<
270 ARMCP_PKT_CTL_OPCODE_SHIFT);
271 pkt.addr = cpu_to_le64(eeprom_info_dma_addr);
272 pkt.data_max_size = cpu_to_le32(max_size);
274 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
275 HL_ARMCP_EEPROM_TIMEOUT_USEC, &result);
279 "Failed to send ArmCP EEPROM packet, error %d\n", rc);
283 /* result contains the actual size */
284 memcpy(data, eeprom_info_cpu_addr, min((size_t)result, max_size));
287 hdev->asic_funcs->cpu_accessible_dma_pool_free(hdev, max_size,
288 eeprom_info_cpu_addr);