]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/misc/habanalabs/firmware_if.c
arm64: dts: bitmain: Modify pin controller memory map
[linux.git] / drivers / misc / habanalabs / firmware_if.c
1 // SPDX-License-Identifier: GPL-2.0
2
3 /*
4  * Copyright 2016-2019 HabanaLabs, Ltd.
5  * All Rights Reserved.
6  */
7
8 #include "habanalabs.h"
9
10 #include <linux/firmware.h>
11 #include <linux/genalloc.h>
12 #include <linux/io-64-nonatomic-lo-hi.h>
13
14 /**
15  * hl_fw_push_fw_to_device() - Push FW code to device.
16  * @hdev: pointer to hl_device structure.
17  *
18  * Copy fw code from firmware file to device memory.
19  *
20  * Return: 0 on success, non-zero for failure.
21  */
22 int hl_fw_push_fw_to_device(struct hl_device *hdev, const char *fw_name,
23                                 void __iomem *dst)
24 {
25         const struct firmware *fw;
26         const u64 *fw_data;
27         size_t fw_size, i;
28         int rc;
29
30         rc = request_firmware(&fw, fw_name, hdev->dev);
31         if (rc) {
32                 dev_err(hdev->dev, "Firmware file %s is not found!\n", fw_name);
33                 goto out;
34         }
35
36         fw_size = fw->size;
37         if ((fw_size % 4) != 0) {
38                 dev_err(hdev->dev, "Illegal %s firmware size %zu\n",
39                         fw_name, fw_size);
40                 rc = -EINVAL;
41                 goto out;
42         }
43
44         dev_dbg(hdev->dev, "%s firmware size == %zu\n", fw_name, fw_size);
45
46         fw_data = (const u64 *) fw->data;
47
48         if ((fw->size % 8) != 0)
49                 fw_size -= 8;
50
51         for (i = 0 ; i < fw_size ; i += 8, fw_data++, dst += 8) {
52                 if (!(i & (0x80000 - 1))) {
53                         dev_dbg(hdev->dev,
54                                 "copied so far %zu out of %zu for %s firmware",
55                                 i, fw_size, fw_name);
56                         usleep_range(20, 100);
57                 }
58
59                 writeq(*fw_data, dst);
60         }
61
62         if ((fw->size % 8) != 0)
63                 writel(*(const u32 *) fw_data, dst);
64
65 out:
66         release_firmware(fw);
67         return rc;
68 }
69
70 int hl_fw_send_pci_access_msg(struct hl_device *hdev, u32 opcode)
71 {
72         struct armcp_packet pkt = {};
73
74         pkt.ctl = cpu_to_le32(opcode << ARMCP_PKT_CTL_OPCODE_SHIFT);
75
76         return hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt,
77                                 sizeof(pkt), HL_DEVICE_TIMEOUT_USEC, NULL);
78 }
79
80 int hl_fw_send_cpu_message(struct hl_device *hdev, u32 hw_queue_id, u32 *msg,
81                                 u16 len, u32 timeout, long *result)
82 {
83         struct armcp_packet *pkt;
84         dma_addr_t pkt_dma_addr;
85         u32 tmp;
86         int rc = 0;
87
88         pkt = hdev->asic_funcs->cpu_accessible_dma_pool_alloc(hdev, len,
89                                                                 &pkt_dma_addr);
90         if (!pkt) {
91                 dev_err(hdev->dev,
92                         "Failed to allocate DMA memory for packet to CPU\n");
93                 return -ENOMEM;
94         }
95
96         memcpy(pkt, msg, len);
97
98         mutex_lock(&hdev->send_cpu_message_lock);
99
100         if (hdev->disabled)
101                 goto out;
102
103         if (hdev->device_cpu_disabled) {
104                 rc = -EIO;
105                 goto out;
106         }
107
108         rc = hl_hw_queue_send_cb_no_cmpl(hdev, hw_queue_id, len, pkt_dma_addr);
109         if (rc) {
110                 dev_err(hdev->dev, "Failed to send CB on CPU PQ (%d)\n", rc);
111                 goto out;
112         }
113
114         rc = hl_poll_timeout_memory(hdev, &pkt->fence, tmp,
115                                 (tmp == ARMCP_PACKET_FENCE_VAL), 1000, timeout);
116
117         hl_hw_queue_inc_ci_kernel(hdev, hw_queue_id);
118
119         if (rc == -ETIMEDOUT) {
120                 dev_err(hdev->dev, "Device CPU packet timeout (0x%x)\n", tmp);
121                 hdev->device_cpu_disabled = true;
122                 goto out;
123         }
124
125         tmp = le32_to_cpu(pkt->ctl);
126
127         rc = (tmp & ARMCP_PKT_CTL_RC_MASK) >> ARMCP_PKT_CTL_RC_SHIFT;
128         if (rc) {
129                 dev_err(hdev->dev, "F/W ERROR %d for CPU packet %d\n",
130                         rc,
131                         (tmp & ARMCP_PKT_CTL_OPCODE_MASK)
132                                                 >> ARMCP_PKT_CTL_OPCODE_SHIFT);
133                 rc = -EIO;
134         } else if (result) {
135                 *result = (long) le64_to_cpu(pkt->result);
136         }
137
138 out:
139         mutex_unlock(&hdev->send_cpu_message_lock);
140
141         hdev->asic_funcs->cpu_accessible_dma_pool_free(hdev, len, pkt);
142
143         return rc;
144 }
145
146 int hl_fw_test_cpu_queue(struct hl_device *hdev)
147 {
148         struct armcp_packet test_pkt = {};
149         long result;
150         int rc;
151
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);
155
156         rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &test_pkt,
157                         sizeof(test_pkt), HL_DEVICE_TIMEOUT_USEC, &result);
158
159         if (!rc) {
160                 if (result == ARMCP_PACKET_FENCE_VAL)
161                         dev_info(hdev->dev,
162                                 "queue test on CPU queue succeeded\n");
163                 else
164                         dev_err(hdev->dev,
165                                 "CPU queue test failed (0x%08lX)\n", result);
166         } else {
167                 dev_err(hdev->dev, "CPU queue test failed, error %d\n", rc);
168         }
169
170         return rc;
171 }
172
173 void *hl_fw_cpu_accessible_dma_pool_alloc(struct hl_device *hdev, size_t size,
174                                                 dma_addr_t *dma_handle)
175 {
176         u64 kernel_addr;
177
178         kernel_addr = gen_pool_alloc(hdev->cpu_accessible_dma_pool, size);
179
180         *dma_handle = hdev->cpu_accessible_dma_address +
181                 (kernel_addr - (u64) (uintptr_t) hdev->cpu_accessible_dma_mem);
182
183         return (void *) (uintptr_t) kernel_addr;
184 }
185
186 void hl_fw_cpu_accessible_dma_pool_free(struct hl_device *hdev, size_t size,
187                                         void *vaddr)
188 {
189         gen_pool_free(hdev->cpu_accessible_dma_pool, (u64) (uintptr_t) vaddr,
190                         size);
191 }
192
193 int hl_fw_send_heartbeat(struct hl_device *hdev)
194 {
195         struct armcp_packet hb_pkt = {};
196         long result;
197         int rc;
198
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);
202
203         rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &hb_pkt,
204                         sizeof(hb_pkt), HL_DEVICE_TIMEOUT_USEC, &result);
205
206         if ((rc) || (result != ARMCP_PACKET_FENCE_VAL))
207                 rc = -EIO;
208
209         return rc;
210 }
211
212 int hl_fw_armcp_info_get(struct hl_device *hdev)
213 {
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;
218         long result;
219         int rc;
220
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) {
226                 dev_err(hdev->dev,
227                         "Failed to allocate DMA memory for ArmCP info packet\n");
228                 return -ENOMEM;
229         }
230
231         memset(armcp_info_cpu_addr, 0, sizeof(struct armcp_info));
232
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));
237
238         rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
239                                         HL_ARMCP_INFO_TIMEOUT_USEC, &result);
240         if (rc) {
241                 dev_err(hdev->dev,
242                         "Failed to send ArmCP info pkt, error %d\n", rc);
243                 goto out;
244         }
245
246         memcpy(&prop->armcp_info, armcp_info_cpu_addr,
247                         sizeof(prop->armcp_info));
248
249         rc = hl_build_hwmon_channel_info(hdev, prop->armcp_info.sensors);
250         if (rc) {
251                 dev_err(hdev->dev,
252                         "Failed to build hwmon channel info, error %d\n", rc);
253                 rc = -EFAULT;
254                 goto out;
255         }
256
257 out:
258         hdev->asic_funcs->cpu_accessible_dma_pool_free(hdev,
259                         sizeof(struct armcp_info), armcp_info_cpu_addr);
260
261         return rc;
262 }
263
264 int hl_fw_get_eeprom_data(struct hl_device *hdev, void *data, size_t max_size)
265 {
266         struct armcp_packet pkt = {};
267         void *eeprom_info_cpu_addr;
268         dma_addr_t eeprom_info_dma_addr;
269         long result;
270         int rc;
271
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) {
276                 dev_err(hdev->dev,
277                         "Failed to allocate DMA memory for ArmCP EEPROM packet\n");
278                 return -ENOMEM;
279         }
280
281         memset(eeprom_info_cpu_addr, 0, max_size);
282
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);
287
288         rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
289                         HL_ARMCP_EEPROM_TIMEOUT_USEC, &result);
290
291         if (rc) {
292                 dev_err(hdev->dev,
293                         "Failed to send ArmCP EEPROM packet, error %d\n", rc);
294                 goto out;
295         }
296
297         /* result contains the actual size */
298         memcpy(data, eeprom_info_cpu_addr, min((size_t)result, max_size));
299
300 out:
301         hdev->asic_funcs->cpu_accessible_dma_pool_free(hdev, max_size,
302                         eeprom_info_cpu_addr);
303
304         return rc;
305 }