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, "Failed to request %s\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 if (len > HL_CPU_CB_SIZE) {
89 dev_err(hdev->dev, "Invalid CPU message size of %d bytes\n",
94 pkt = hdev->asic_funcs->cpu_accessible_dma_pool_alloc(hdev, len,
98 "Failed to allocate DMA memory for packet to CPU\n");
102 memcpy(pkt, msg, len);
104 mutex_lock(&hdev->send_cpu_message_lock);
109 if (hdev->device_cpu_disabled) {
114 rc = hl_hw_queue_send_cb_no_cmpl(hdev, hw_queue_id, len, pkt_dma_addr);
116 dev_err(hdev->dev, "Failed to send CB on CPU PQ (%d)\n", rc);
120 rc = hl_poll_timeout_memory(hdev, (u64) (uintptr_t) &pkt->fence,
123 hl_hw_queue_inc_ci_kernel(hdev, hw_queue_id);
125 if (rc == -ETIMEDOUT) {
126 dev_err(hdev->dev, "Timeout while waiting for device CPU\n");
127 hdev->device_cpu_disabled = true;
131 if (tmp == ARMCP_PACKET_FENCE_VAL) {
132 u32 ctl = le32_to_cpu(pkt->ctl);
134 rc = (ctl & ARMCP_PKT_CTL_RC_MASK) >> ARMCP_PKT_CTL_RC_SHIFT;
137 "F/W ERROR %d for CPU packet %d\n",
138 rc, (ctl & ARMCP_PKT_CTL_OPCODE_MASK)
139 >> ARMCP_PKT_CTL_OPCODE_SHIFT);
142 *result = (long) le64_to_cpu(pkt->result);
145 dev_err(hdev->dev, "CPU packet wrong fence value\n");
150 mutex_unlock(&hdev->send_cpu_message_lock);
152 hdev->asic_funcs->cpu_accessible_dma_pool_free(hdev, len, pkt);
157 int hl_fw_test_cpu_queue(struct hl_device *hdev)
159 struct armcp_packet test_pkt = {};
163 test_pkt.ctl = cpu_to_le32(ARMCP_PACKET_TEST <<
164 ARMCP_PKT_CTL_OPCODE_SHIFT);
165 test_pkt.value = cpu_to_le64(ARMCP_PACKET_FENCE_VAL);
167 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &test_pkt,
168 sizeof(test_pkt), HL_DEVICE_TIMEOUT_USEC, &result);
171 if (result == ARMCP_PACKET_FENCE_VAL)
173 "queue test on CPU queue succeeded\n");
176 "CPU queue test failed (0x%08lX)\n", result);
178 dev_err(hdev->dev, "CPU queue test failed, error %d\n", rc);
184 void *hl_fw_cpu_accessible_dma_pool_alloc(struct hl_device *hdev, size_t size,
185 dma_addr_t *dma_handle)
189 /* roundup to HL_CPU_PKT_SIZE */
190 size = (size + (HL_CPU_PKT_SIZE - 1)) & HL_CPU_PKT_MASK;
192 kernel_addr = gen_pool_alloc(hdev->cpu_accessible_dma_pool, size);
194 *dma_handle = hdev->cpu_accessible_dma_address +
195 (kernel_addr - (u64) (uintptr_t) hdev->cpu_accessible_dma_mem);
197 return (void *) (uintptr_t) kernel_addr;
200 void hl_fw_cpu_accessible_dma_pool_free(struct hl_device *hdev, size_t size,
203 /* roundup to HL_CPU_PKT_SIZE */
204 size = (size + (HL_CPU_PKT_SIZE - 1)) & HL_CPU_PKT_MASK;
206 gen_pool_free(hdev->cpu_accessible_dma_pool, (u64) (uintptr_t) vaddr,
210 int hl_fw_send_heartbeat(struct hl_device *hdev)
212 struct armcp_packet hb_pkt = {};
216 hb_pkt.ctl = cpu_to_le32(ARMCP_PACKET_TEST <<
217 ARMCP_PKT_CTL_OPCODE_SHIFT);
218 hb_pkt.value = cpu_to_le64(ARMCP_PACKET_FENCE_VAL);
220 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &hb_pkt,
221 sizeof(hb_pkt), HL_DEVICE_TIMEOUT_USEC, &result);
223 if ((rc) || (result != ARMCP_PACKET_FENCE_VAL))
229 int hl_fw_armcp_info_get(struct hl_device *hdev)
231 struct asic_fixed_properties *prop = &hdev->asic_prop;
232 struct armcp_packet pkt = {};
233 void *armcp_info_cpu_addr;
234 dma_addr_t armcp_info_dma_addr;
238 armcp_info_cpu_addr =
239 hdev->asic_funcs->cpu_accessible_dma_pool_alloc(hdev,
240 sizeof(struct armcp_info),
241 &armcp_info_dma_addr);
242 if (!armcp_info_cpu_addr) {
244 "Failed to allocate DMA memory for ArmCP info packet\n");
248 memset(armcp_info_cpu_addr, 0, sizeof(struct armcp_info));
250 pkt.ctl = cpu_to_le32(ARMCP_PACKET_INFO_GET <<
251 ARMCP_PKT_CTL_OPCODE_SHIFT);
252 pkt.addr = cpu_to_le64(armcp_info_dma_addr);
253 pkt.data_max_size = cpu_to_le32(sizeof(struct armcp_info));
255 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
256 HL_ARMCP_INFO_TIMEOUT_USEC, &result);
259 "Failed to send armcp info pkt, error %d\n", rc);
263 memcpy(&prop->armcp_info, armcp_info_cpu_addr,
264 sizeof(prop->armcp_info));
266 rc = hl_build_hwmon_channel_info(hdev, prop->armcp_info.sensors);
269 "Failed to build hwmon channel info, error %d\n", rc);
275 hdev->asic_funcs->cpu_accessible_dma_pool_free(hdev,
276 sizeof(struct armcp_info), armcp_info_cpu_addr);
281 int hl_fw_get_eeprom_data(struct hl_device *hdev, void *data, size_t max_size)
283 struct armcp_packet pkt = {};
284 void *eeprom_info_cpu_addr;
285 dma_addr_t eeprom_info_dma_addr;
289 eeprom_info_cpu_addr =
290 hdev->asic_funcs->cpu_accessible_dma_pool_alloc(hdev,
291 max_size, &eeprom_info_dma_addr);
292 if (!eeprom_info_cpu_addr) {
294 "Failed to allocate DMA memory for EEPROM info packet\n");
298 memset(eeprom_info_cpu_addr, 0, max_size);
300 pkt.ctl = cpu_to_le32(ARMCP_PACKET_EEPROM_DATA_GET <<
301 ARMCP_PKT_CTL_OPCODE_SHIFT);
302 pkt.addr = cpu_to_le64(eeprom_info_dma_addr);
303 pkt.data_max_size = cpu_to_le32(max_size);
305 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
306 HL_ARMCP_EEPROM_TIMEOUT_USEC, &result);
310 "Failed to send armcp EEPROM pkt, error %d\n", rc);
314 /* result contains the actual size */
315 memcpy(data, eeprom_info_cpu_addr, min((size_t)result, max_size));
318 hdev->asic_funcs->cpu_accessible_dma_pool_free(hdev, max_size,
319 eeprom_info_cpu_addr);