cvp_fw_load.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2020-2021, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/of.h>
  6. #include <linux/pm_qos.h>
  7. #include <linux/platform_device.h>
  8. #include <linux/qcom_scm.h>
  9. #include "msm_cvp_debug.h"
  10. #include "cvp_comm_def.h"
  11. #include "cvp_core_hfi.h"
  12. #include "cvp_hfi.h"
  13. #include <linux/of_address.h>
  14. #include <linux/firmware.h>
  15. #include <linux/soc/qcom/mdt_loader.h>
  16. #include "cvp_dump.h"
  17. #define MAX_FIRMWARE_NAME_SIZE 128
  18. static int __load_fw_to_memory(struct platform_device *pdev,
  19. const char *fw_name)
  20. {
  21. int rc = 0;
  22. const struct firmware *firmware = NULL;
  23. char firmware_name[MAX_FIRMWARE_NAME_SIZE] = {0};
  24. struct device_node *node = NULL;
  25. struct resource res = {0};
  26. phys_addr_t phys = 0;
  27. size_t res_size = 0;
  28. ssize_t fw_size = 0;
  29. void *virt = NULL;
  30. int pas_id = 0;
  31. if (!fw_name || !(*fw_name) || !pdev) {
  32. dprintk(CVP_ERR, "%s: Invalid inputs\n", __func__);
  33. return -EINVAL;
  34. }
  35. if (strlen(fw_name) >= MAX_FIRMWARE_NAME_SIZE - 4) {
  36. dprintk(CVP_ERR, "%s: Invalid fw name\n", __func__);
  37. return -EINVAL;
  38. }
  39. scnprintf(firmware_name, ARRAY_SIZE(firmware_name), "%s.mbn", fw_name);
  40. rc = of_property_read_u32(pdev->dev.of_node, "pas-id", &pas_id);
  41. if (rc) {
  42. dprintk(CVP_ERR,
  43. "%s: error %d while reading DT for \"pas-id\"\n",
  44. __func__, rc);
  45. goto exit;
  46. }
  47. node = of_parse_phandle(pdev->dev.of_node, "memory-region", 0);
  48. if (!node) {
  49. dprintk(CVP_ERR,
  50. "%s: DT error getting \"memory-region\" property\n",
  51. __func__);
  52. return -EINVAL;
  53. }
  54. rc = of_address_to_resource(node, 0, &res);
  55. if (rc) {
  56. dprintk(CVP_ERR,
  57. "%s: error %d getting \"memory-region\" resource\n",
  58. __func__, rc);
  59. goto exit;
  60. }
  61. phys = res.start;
  62. res_size = (size_t)resource_size(&res);
  63. rc = request_firmware(&firmware, firmware_name, &pdev->dev);
  64. if (rc) {
  65. dprintk(CVP_ERR, "%s: error %d requesting \"%s\"\n",
  66. __func__, rc, firmware_name);
  67. goto exit;
  68. }
  69. fw_size = qcom_mdt_get_size(firmware);
  70. if (fw_size < 0 || res_size < (size_t)fw_size) {
  71. rc = -EINVAL;
  72. dprintk(CVP_ERR,
  73. "%s: Corrupted fw image. Alloc size: %lu, fw size: %ld",
  74. __func__, res_size, fw_size);
  75. goto exit;
  76. }
  77. virt = memremap(phys, res_size, MEMREMAP_WC);
  78. if (!virt) {
  79. rc = -ENOMEM;
  80. dprintk(CVP_ERR, "%s: unable to remap firmware memory\n",
  81. __func__);
  82. goto exit;
  83. }
  84. rc = qcom_mdt_load(&pdev->dev, firmware, firmware_name,
  85. pas_id, virt, phys, res_size, NULL);
  86. if (rc) {
  87. dprintk(CVP_ERR, "%s: error %d loading \"%s\"\n",
  88. __func__, rc, firmware_name);
  89. goto exit;
  90. }
  91. rc = qcom_scm_pas_auth_and_reset(pas_id);
  92. if (rc) {
  93. dprintk(CVP_ERR, "%s: error %d authenticating \"%s\"\n",
  94. __func__, rc, firmware_name);
  95. goto exit;
  96. }
  97. rc = md_eva_dump("evafwdata", (uintptr_t)virt, phys, EVAFW_IMAGE_SIZE);
  98. if (rc) {
  99. dprintk(CVP_ERR, "%s: error %d in dumping \"%s\"\n",
  100. __func__, rc, firmware_name);
  101. }
  102. memunmap(virt);
  103. release_firmware(firmware);
  104. dprintk(CVP_CORE, "%s: firmware \"%s\" loaded successfully\n",
  105. __func__, firmware_name);
  106. return pas_id;
  107. exit:
  108. if (virt)
  109. memunmap(virt);
  110. if (firmware)
  111. release_firmware(firmware);
  112. return rc;
  113. }
  114. int load_cvp_fw_impl(struct iris_hfi_device *device)
  115. {
  116. int rc = 0;
  117. if (!device->resources.fw.cookie) {
  118. device->resources.fw.cookie =
  119. __load_fw_to_memory(device->res->pdev,
  120. device->res->fw_name);
  121. if (device->resources.fw.cookie <= 0) {
  122. dprintk(CVP_ERR, "Failed to download firmware\n");
  123. device->resources.fw.cookie = 0;
  124. rc = -ENOMEM;
  125. }
  126. }
  127. return rc;
  128. }
  129. int unload_cvp_fw_impl(struct iris_hfi_device *device)
  130. {
  131. qcom_scm_pas_shutdown(device->resources.fw.cookie);
  132. device->resources.fw.cookie = 0;
  133. return 0;
  134. }