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 if ((fw->size % 8) != 0)
51 for (i = 0 ; i < fw_size ; i += 8, fw_data++, dst += 8) {
52 if (!(i & (0x80000 - 1))) {
54 "copied so far %zu out of %zu for %s firmware",
56 usleep_range(20, 100);
59 writeq(*fw_data, dst);
62 if ((fw->size % 8) != 0)
63 writel(*(const u32 *) fw_data, dst);
70 int hl_fw_send_pci_access_msg(struct hl_device *hdev, u32 opcode)
72 struct armcp_packet pkt = {};
74 pkt.ctl = cpu_to_le32(opcode << ARMCP_PKT_CTL_OPCODE_SHIFT);
76 return hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt,
77 sizeof(pkt), HL_DEVICE_TIMEOUT_USEC, NULL);
80 int hl_fw_send_cpu_message(struct hl_device *hdev, u32 hw_queue_id, u32 *msg,
81 u16 len, u32 timeout, long *result)
83 struct armcp_packet *pkt;
84 dma_addr_t pkt_dma_addr;
88 pkt = hdev->asic_funcs->cpu_accessible_dma_pool_alloc(hdev, len,
92 "Failed to allocate DMA memory for packet to CPU\n");
96 memcpy(pkt, msg, len);
98 mutex_lock(&hdev->send_cpu_message_lock);
103 if (hdev->device_cpu_disabled) {
108 rc = hl_hw_queue_send_cb_no_cmpl(hdev, hw_queue_id, len, pkt_dma_addr);
110 dev_err(hdev->dev, "Failed to send CB on CPU PQ (%d)\n", rc);
114 rc = hl_poll_timeout_memory(hdev, &pkt->fence, tmp,
115 (tmp == ARMCP_PACKET_FENCE_VAL), 1000, timeout);
117 hl_hw_queue_inc_ci_kernel(hdev, hw_queue_id);
119 if (rc == -ETIMEDOUT) {
120 dev_err(hdev->dev, "Device CPU packet timeout (0x%x)\n", tmp);
121 hdev->device_cpu_disabled = true;
125 tmp = le32_to_cpu(pkt->ctl);
127 rc = (tmp & ARMCP_PKT_CTL_RC_MASK) >> ARMCP_PKT_CTL_RC_SHIFT;
129 dev_err(hdev->dev, "F/W ERROR %d for CPU packet %d\n",
131 (tmp & ARMCP_PKT_CTL_OPCODE_MASK)
132 >> ARMCP_PKT_CTL_OPCODE_SHIFT);
135 *result = (long) le64_to_cpu(pkt->result);
139 mutex_unlock(&hdev->send_cpu_message_lock);
141 hdev->asic_funcs->cpu_accessible_dma_pool_free(hdev, len, pkt);
146 int hl_fw_test_cpu_queue(struct hl_device *hdev)
148 struct armcp_packet test_pkt = {};
152 test_pkt.ctl = cpu_to_le32(ARMCP_PACKET_TEST <<
153 ARMCP_PKT_CTL_OPCODE_SHIFT);
154 test_pkt.value = cpu_to_le64(ARMCP_PACKET_FENCE_VAL);
156 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &test_pkt,
157 sizeof(test_pkt), HL_DEVICE_TIMEOUT_USEC, &result);
160 if (result == ARMCP_PACKET_FENCE_VAL)
162 "queue test on CPU queue succeeded\n");
165 "CPU queue test failed (0x%08lX)\n", result);
167 dev_err(hdev->dev, "CPU queue test failed, error %d\n", rc);
173 void *hl_fw_cpu_accessible_dma_pool_alloc(struct hl_device *hdev, size_t size,
174 dma_addr_t *dma_handle)
178 kernel_addr = gen_pool_alloc(hdev->cpu_accessible_dma_pool, size);
180 *dma_handle = hdev->cpu_accessible_dma_address +
181 (kernel_addr - (u64) (uintptr_t) hdev->cpu_accessible_dma_mem);
183 return (void *) (uintptr_t) kernel_addr;
186 void hl_fw_cpu_accessible_dma_pool_free(struct hl_device *hdev, size_t size,
189 gen_pool_free(hdev->cpu_accessible_dma_pool, (u64) (uintptr_t) vaddr,
193 int hl_fw_send_heartbeat(struct hl_device *hdev)
195 struct armcp_packet hb_pkt = {};
199 hb_pkt.ctl = cpu_to_le32(ARMCP_PACKET_TEST <<
200 ARMCP_PKT_CTL_OPCODE_SHIFT);
201 hb_pkt.value = cpu_to_le64(ARMCP_PACKET_FENCE_VAL);
203 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &hb_pkt,
204 sizeof(hb_pkt), HL_DEVICE_TIMEOUT_USEC, &result);
206 if ((rc) || (result != ARMCP_PACKET_FENCE_VAL))
212 int hl_fw_armcp_info_get(struct hl_device *hdev)
214 struct asic_fixed_properties *prop = &hdev->asic_prop;
215 struct armcp_packet pkt = {};
216 void *armcp_info_cpu_addr;
217 dma_addr_t armcp_info_dma_addr;
221 armcp_info_cpu_addr =
222 hdev->asic_funcs->cpu_accessible_dma_pool_alloc(hdev,
223 sizeof(struct armcp_info),
224 &armcp_info_dma_addr);
225 if (!armcp_info_cpu_addr) {
227 "Failed to allocate DMA memory for ArmCP info packet\n");
231 memset(armcp_info_cpu_addr, 0, sizeof(struct armcp_info));
233 pkt.ctl = cpu_to_le32(ARMCP_PACKET_INFO_GET <<
234 ARMCP_PKT_CTL_OPCODE_SHIFT);
235 pkt.addr = cpu_to_le64(armcp_info_dma_addr);
236 pkt.data_max_size = cpu_to_le32(sizeof(struct armcp_info));
238 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
239 HL_ARMCP_INFO_TIMEOUT_USEC, &result);
242 "Failed to send ArmCP info pkt, error %d\n", rc);
246 memcpy(&prop->armcp_info, armcp_info_cpu_addr,
247 sizeof(prop->armcp_info));
249 rc = hl_build_hwmon_channel_info(hdev, prop->armcp_info.sensors);
252 "Failed to build hwmon channel info, error %d\n", rc);
258 hdev->asic_funcs->cpu_accessible_dma_pool_free(hdev,
259 sizeof(struct armcp_info), armcp_info_cpu_addr);
264 int hl_fw_get_eeprom_data(struct hl_device *hdev, void *data, size_t max_size)
266 struct armcp_packet pkt = {};
267 void *eeprom_info_cpu_addr;
268 dma_addr_t eeprom_info_dma_addr;
272 eeprom_info_cpu_addr =
273 hdev->asic_funcs->cpu_accessible_dma_pool_alloc(hdev,
274 max_size, &eeprom_info_dma_addr);
275 if (!eeprom_info_cpu_addr) {
277 "Failed to allocate DMA memory for ArmCP EEPROM packet\n");
281 memset(eeprom_info_cpu_addr, 0, max_size);
283 pkt.ctl = cpu_to_le32(ARMCP_PACKET_EEPROM_DATA_GET <<
284 ARMCP_PKT_CTL_OPCODE_SHIFT);
285 pkt.addr = cpu_to_le64(eeprom_info_dma_addr);
286 pkt.data_max_size = cpu_to_le32(max_size);
288 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
289 HL_ARMCP_EEPROM_TIMEOUT_USEC, &result);
293 "Failed to send ArmCP EEPROM packet, error %d\n", rc);
297 /* result contains the actual size */
298 memcpy(data, eeprom_info_cpu_addr, min((size_t)result, max_size));
301 hdev->asic_funcs->cpu_accessible_dma_pool_free(hdev, max_size,
302 eeprom_info_cpu_addr);