linux/drivers/misc/habanalabs/firmware_if.c
<<
>>
Prefs
   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 */
  22int 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, "Failed to request %s\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
  65out:
  66        release_firmware(fw);
  67        return rc;
  68}
  69
  70int 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
  80int 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        if (len > HL_CPU_CB_SIZE) {
  89                dev_err(hdev->dev, "Invalid CPU message size of %d bytes\n",
  90                        len);
  91                return -ENOMEM;
  92        }
  93
  94        pkt = hdev->asic_funcs->cpu_accessible_dma_pool_alloc(hdev, len,
  95                                                                &pkt_dma_addr);
  96        if (!pkt) {
  97                dev_err(hdev->dev,
  98                        "Failed to allocate DMA memory for packet to CPU\n");
  99                return -ENOMEM;
 100        }
 101
 102        memcpy(pkt, msg, len);
 103
 104        mutex_lock(&hdev->send_cpu_message_lock);
 105
 106        if (hdev->disabled)
 107                goto out;
 108
 109        if (hdev->device_cpu_disabled) {
 110                rc = -EIO;
 111                goto out;
 112        }
 113
 114        rc = hl_hw_queue_send_cb_no_cmpl(hdev, hw_queue_id, len, pkt_dma_addr);
 115        if (rc) {
 116                dev_err(hdev->dev, "Failed to send CB on CPU PQ (%d)\n", rc);
 117                goto out;
 118        }
 119
 120        rc = hl_poll_timeout_memory(hdev, (u64) (uintptr_t) &pkt->fence,
 121                                        timeout, &tmp);
 122
 123        hl_hw_queue_inc_ci_kernel(hdev, hw_queue_id);
 124
 125        if (rc == -ETIMEDOUT) {
 126                dev_err(hdev->dev, "Timeout while waiting for device CPU\n");
 127                hdev->device_cpu_disabled = true;
 128                goto out;
 129        }
 130
 131        if (tmp == ARMCP_PACKET_FENCE_VAL) {
 132                u32 ctl = le32_to_cpu(pkt->ctl);
 133
 134                rc = (ctl & ARMCP_PKT_CTL_RC_MASK) >> ARMCP_PKT_CTL_RC_SHIFT;
 135                if (rc) {
 136                        dev_err(hdev->dev,
 137                                "F/W ERROR %d for CPU packet %d\n",
 138                                rc, (ctl & ARMCP_PKT_CTL_OPCODE_MASK)
 139                                                >> ARMCP_PKT_CTL_OPCODE_SHIFT);
 140                        rc = -EINVAL;
 141                } else if (result) {
 142                        *result = (long) le64_to_cpu(pkt->result);
 143                }
 144        } else {
 145                dev_err(hdev->dev, "CPU packet wrong fence value\n");
 146                rc = -EINVAL;
 147        }
 148
 149out:
 150        mutex_unlock(&hdev->send_cpu_message_lock);
 151
 152        hdev->asic_funcs->cpu_accessible_dma_pool_free(hdev, len, pkt);
 153
 154        return rc;
 155}
 156
 157int hl_fw_test_cpu_queue(struct hl_device *hdev)
 158{
 159        struct armcp_packet test_pkt = {};
 160        long result;
 161        int rc;
 162
 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);
 166
 167        rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &test_pkt,
 168                        sizeof(test_pkt), HL_DEVICE_TIMEOUT_USEC, &result);
 169
 170        if (!rc) {
 171                if (result == ARMCP_PACKET_FENCE_VAL)
 172                        dev_info(hdev->dev,
 173                                "queue test on CPU queue succeeded\n");
 174                else
 175                        dev_err(hdev->dev,
 176                                "CPU queue test failed (0x%08lX)\n", result);
 177        } else {
 178                dev_err(hdev->dev, "CPU queue test failed, error %d\n", rc);
 179        }
 180
 181        return rc;
 182}
 183
 184void *hl_fw_cpu_accessible_dma_pool_alloc(struct hl_device *hdev, size_t size,
 185                                                dma_addr_t *dma_handle)
 186{
 187        u64 kernel_addr;
 188
 189        /* roundup to HL_CPU_PKT_SIZE */
 190        size = (size + (HL_CPU_PKT_SIZE - 1)) & HL_CPU_PKT_MASK;
 191
 192        kernel_addr = gen_pool_alloc(hdev->cpu_accessible_dma_pool, size);
 193
 194        *dma_handle = hdev->cpu_accessible_dma_address +
 195                (kernel_addr - (u64) (uintptr_t) hdev->cpu_accessible_dma_mem);
 196
 197        return (void *) (uintptr_t) kernel_addr;
 198}
 199
 200void hl_fw_cpu_accessible_dma_pool_free(struct hl_device *hdev, size_t size,
 201                                        void *vaddr)
 202{
 203        /* roundup to HL_CPU_PKT_SIZE */
 204        size = (size + (HL_CPU_PKT_SIZE - 1)) & HL_CPU_PKT_MASK;
 205
 206        gen_pool_free(hdev->cpu_accessible_dma_pool, (u64) (uintptr_t) vaddr,
 207                        size);
 208}
 209
 210int hl_fw_send_heartbeat(struct hl_device *hdev)
 211{
 212        struct armcp_packet hb_pkt = {};
 213        long result;
 214        int rc;
 215
 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);
 219
 220        rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &hb_pkt,
 221                        sizeof(hb_pkt), HL_DEVICE_TIMEOUT_USEC, &result);
 222
 223        if ((rc) || (result != ARMCP_PACKET_FENCE_VAL))
 224                rc = -EIO;
 225
 226        return rc;
 227}
 228
 229int hl_fw_armcp_info_get(struct hl_device *hdev)
 230{
 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;
 235        long result;
 236        int rc;
 237
 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) {
 243                dev_err(hdev->dev,
 244                        "Failed to allocate DMA memory for ArmCP info packet\n");
 245                return -ENOMEM;
 246        }
 247
 248        memset(armcp_info_cpu_addr, 0, sizeof(struct armcp_info));
 249
 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));
 254
 255        rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
 256                                        HL_ARMCP_INFO_TIMEOUT_USEC, &result);
 257        if (rc) {
 258                dev_err(hdev->dev,
 259                        "Failed to send armcp info pkt, error %d\n", rc);
 260                goto out;
 261        }
 262
 263        memcpy(&prop->armcp_info, armcp_info_cpu_addr,
 264                        sizeof(prop->armcp_info));
 265
 266        rc = hl_build_hwmon_channel_info(hdev, prop->armcp_info.sensors);
 267        if (rc) {
 268                dev_err(hdev->dev,
 269                        "Failed to build hwmon channel info, error %d\n", rc);
 270                rc = -EFAULT;
 271                goto out;
 272        }
 273
 274out:
 275        hdev->asic_funcs->cpu_accessible_dma_pool_free(hdev,
 276                        sizeof(struct armcp_info), armcp_info_cpu_addr);
 277
 278        return rc;
 279}
 280
 281int hl_fw_get_eeprom_data(struct hl_device *hdev, void *data, size_t max_size)
 282{
 283        struct armcp_packet pkt = {};
 284        void *eeprom_info_cpu_addr;
 285        dma_addr_t eeprom_info_dma_addr;
 286        long result;
 287        int rc;
 288
 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) {
 293                dev_err(hdev->dev,
 294                        "Failed to allocate DMA memory for EEPROM info packet\n");
 295                return -ENOMEM;
 296        }
 297
 298        memset(eeprom_info_cpu_addr, 0, max_size);
 299
 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);
 304
 305        rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
 306                        HL_ARMCP_EEPROM_TIMEOUT_USEC, &result);
 307
 308        if (rc) {
 309                dev_err(hdev->dev,
 310                        "Failed to send armcp EEPROM pkt, error %d\n", rc);
 311                goto out;
 312        }
 313
 314        /* result contains the actual size */
 315        memcpy(data, eeprom_info_cpu_addr, min((size_t)result, max_size));
 316
 317out:
 318        hdev->asic_funcs->cpu_accessible_dma_pool_free(hdev, max_size,
 319                        eeprom_info_cpu_addr);
 320
 321        return rc;
 322}
 323