firmware.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2022-2023. Qualcomm Innovation Center, Inc. All rights reserved.
  4. */
  5. #include <linux/types.h>
  6. #include <linux/list.h>
  7. #include <linux/of_address.h>
  8. #include <linux/firmware.h>
  9. #include <linux/qcom_scm.h>
  10. #include <linux/soc/qcom/mdt_loader.h>
  11. #include <linux/soc/qcom/smem.h>
  12. #include "msm_vidc_core.h"
  13. #include "msm_vidc_debug.h"
  14. #include "msm_vidc_events.h"
  15. #include "msm_vidc_platform.h"
  16. #include "firmware.h"
  17. #define MAX_FIRMWARE_NAME_SIZE 128
  18. struct tzbsp_memprot {
  19. u32 cp_start;
  20. u32 cp_size;
  21. u32 cp_nonpixel_start;
  22. u32 cp_nonpixel_size;
  23. };
  24. enum tzbsp_video_state {
  25. TZBSP_VIDEO_STATE_SUSPEND = 0,
  26. TZBSP_VIDEO_STATE_RESUME = 1,
  27. TZBSP_VIDEO_STATE_RESTORE_THRESHOLD = 2,
  28. };
  29. static int protect_cp_mem(struct msm_vidc_core *core)
  30. {
  31. struct tzbsp_memprot memprot;
  32. int rc = 0;
  33. struct context_bank_info *cb;
  34. if (!core)
  35. return -EINVAL;
  36. memprot.cp_start = 0x0;
  37. memprot.cp_size = 0x0;
  38. memprot.cp_nonpixel_start = 0x0;
  39. memprot.cp_nonpixel_size = 0x0;
  40. venus_hfi_for_each_context_bank(core, cb) {
  41. if (cb->region == MSM_VIDC_NON_SECURE) {
  42. memprot.cp_size = cb->addr_range.start;
  43. d_vpr_h("%s: memprot.cp_size: %#x\n",
  44. __func__, memprot.cp_size);
  45. }
  46. if (cb->region == MSM_VIDC_SECURE_NONPIXEL) {
  47. memprot.cp_nonpixel_start = cb->addr_range.start;
  48. memprot.cp_nonpixel_size = cb->addr_range.size;
  49. d_vpr_h("%s: cp_nonpixel_start: %#x size: %#x\n",
  50. __func__, memprot.cp_nonpixel_start,
  51. memprot.cp_nonpixel_size);
  52. }
  53. }
  54. rc = qcom_scm_mem_protect_video_var(memprot.cp_start, memprot.cp_size,
  55. memprot.cp_nonpixel_start, memprot.cp_nonpixel_size);
  56. if (rc)
  57. d_vpr_e("Failed to protect memory(%d)\n", rc);
  58. trace_venus_hfi_var_done(
  59. memprot.cp_start, memprot.cp_size,
  60. memprot.cp_nonpixel_start, memprot.cp_nonpixel_size);
  61. return rc;
  62. }
  63. static int __load_fw_to_memory(struct platform_device *pdev,
  64. const char *fw_name)
  65. {
  66. int rc = 0;
  67. const struct firmware *firmware = NULL;
  68. struct msm_vidc_core *core;
  69. char firmware_name[MAX_FIRMWARE_NAME_SIZE] = { 0 };
  70. struct device_node *node = NULL;
  71. struct resource res = { 0 };
  72. phys_addr_t phys = 0;
  73. size_t res_size = 0;
  74. ssize_t fw_size = 0;
  75. void *virt = NULL;
  76. int pas_id = 0;
  77. if (!fw_name || !(*fw_name) || !pdev) {
  78. d_vpr_e("%s: Invalid inputs\n", __func__);
  79. return -EINVAL;
  80. }
  81. if (strlen(fw_name) >= MAX_FIRMWARE_NAME_SIZE - 4) {
  82. d_vpr_e("%s: Invalid fw name\n", __func__);
  83. return -EINVAL;
  84. }
  85. core = dev_get_drvdata(&pdev->dev);
  86. if (!core) {
  87. d_vpr_e("%s: core not found in device %s",
  88. __func__, dev_name(&pdev->dev));
  89. return -EINVAL;
  90. }
  91. scnprintf(firmware_name, ARRAY_SIZE(firmware_name), "%s.mbn", fw_name);
  92. pas_id = core->platform->data.pas_id;
  93. node = of_parse_phandle(pdev->dev.of_node, "memory-region", 0);
  94. if (!node) {
  95. d_vpr_e("%s: failed to read \"memory-region\"\n",
  96. __func__);
  97. return -EINVAL;
  98. }
  99. rc = of_address_to_resource(node, 0, &res);
  100. if (rc) {
  101. d_vpr_e("%s: failed to read \"memory-region\", error %d\n",
  102. __func__, rc);
  103. goto exit;
  104. }
  105. phys = res.start;
  106. res_size = (size_t)resource_size(&res);
  107. rc = request_firmware(&firmware, firmware_name, &pdev->dev);
  108. if (rc) {
  109. d_vpr_e("%s: failed to request fw \"%s\", error %d\n",
  110. __func__, firmware_name, rc);
  111. goto exit;
  112. }
  113. fw_size = qcom_mdt_get_size(firmware);
  114. if (fw_size < 0 || res_size < (size_t)fw_size) {
  115. rc = -EINVAL;
  116. d_vpr_e("%s: out of bound fw image fw size: %ld, res_size: %lu",
  117. __func__, fw_size, res_size);
  118. goto exit;
  119. }
  120. virt = memremap(phys, res_size, MEMREMAP_WC);
  121. if (!virt) {
  122. d_vpr_e("%s: failed to remap fw memory phys %pa[p]\n",
  123. __func__, &phys);
  124. return -ENOMEM;
  125. }
  126. /* prevent system suspend during fw_load */
  127. pm_stay_awake(pdev->dev.parent);
  128. rc = qcom_mdt_load(&pdev->dev, firmware, firmware_name,
  129. pas_id, virt, phys, res_size, NULL);
  130. pm_relax(pdev->dev.parent);
  131. if (rc) {
  132. d_vpr_e("%s: error %d loading fw \"%s\"\n",
  133. __func__, rc, firmware_name);
  134. goto exit;
  135. }
  136. rc = qcom_scm_pas_auth_and_reset(pas_id);
  137. if (rc) {
  138. d_vpr_e("%s: error %d authenticating fw \"%s\"\n",
  139. __func__, rc, firmware_name);
  140. goto exit;
  141. }
  142. memunmap(virt);
  143. release_firmware(firmware);
  144. d_vpr_h("%s: firmware \"%s\" loaded successfully\n",
  145. __func__, firmware_name);
  146. return pas_id;
  147. exit:
  148. if (virt)
  149. memunmap(virt);
  150. if (firmware)
  151. release_firmware(firmware);
  152. return rc;
  153. }
  154. int fw_load(struct msm_vidc_core *core)
  155. {
  156. int rc;
  157. if (!core->resource->fw_cookie) {
  158. core->resource->fw_cookie = __load_fw_to_memory(core->pdev,
  159. core->platform->data.fwname);
  160. if (core->resource->fw_cookie <= 0) {
  161. d_vpr_e("%s: firmware download failed %d\n",
  162. __func__, core->resource->fw_cookie);
  163. core->resource->fw_cookie = 0;
  164. return -ENOMEM;
  165. }
  166. }
  167. rc = protect_cp_mem(core);
  168. if (rc) {
  169. d_vpr_e("%s: protect memory failed\n", __func__);
  170. goto fail_protect_mem;
  171. }
  172. return rc;
  173. fail_protect_mem:
  174. if (core->resource->fw_cookie)
  175. qcom_scm_pas_shutdown(core->resource->fw_cookie);
  176. core->resource->fw_cookie = 0;
  177. return rc;
  178. }
  179. int fw_unload(struct msm_vidc_core *core)
  180. {
  181. int ret;
  182. if (!core->resource->fw_cookie)
  183. return -EINVAL;
  184. ret = qcom_scm_pas_shutdown(core->resource->fw_cookie);
  185. if (ret)
  186. d_vpr_e("Firmware unload failed rc=%d\n", ret);
  187. core->resource->fw_cookie = 0;
  188. return ret;
  189. }
  190. int fw_suspend(struct msm_vidc_core *core)
  191. {
  192. return qcom_scm_set_remote_state(TZBSP_VIDEO_STATE_SUSPEND, 0);
  193. }
  194. int fw_resume(struct msm_vidc_core *core)
  195. {
  196. return qcom_scm_set_remote_state(TZBSP_VIDEO_STATE_RESUME, 0);
  197. }