intel_huc.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * Copyright © 2016-2017 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. */
  24. #include <linux/types.h>
  25. #include "intel_huc.h"
  26. #include "i915_drv.h"
  27. /**
  28. * DOC: HuC Firmware
  29. *
  30. * Motivation:
  31. * GEN9 introduces a new dedicated firmware for usage in media HEVC (High
  32. * Efficiency Video Coding) operations. Userspace can use the firmware
  33. * capabilities by adding HuC specific commands to batch buffers.
  34. *
  35. * Implementation:
  36. * The same firmware loader is used as the GuC. However, the actual
  37. * loading to HW is deferred until GEM initialization is done.
  38. *
  39. * Note that HuC firmware loading must be done before GuC loading.
  40. */
  41. #define BXT_HUC_FW_MAJOR 01
  42. #define BXT_HUC_FW_MINOR 07
  43. #define BXT_BLD_NUM 1398
  44. #define SKL_HUC_FW_MAJOR 01
  45. #define SKL_HUC_FW_MINOR 07
  46. #define SKL_BLD_NUM 1398
  47. #define KBL_HUC_FW_MAJOR 02
  48. #define KBL_HUC_FW_MINOR 00
  49. #define KBL_BLD_NUM 1810
  50. #define GLK_HUC_FW_MAJOR 02
  51. #define GLK_HUC_FW_MINOR 00
  52. #define GLK_BLD_NUM 1748
  53. #define HUC_FW_PATH(platform, major, minor, bld_num) \
  54. "i915/" __stringify(platform) "_huc_ver" __stringify(major) "_" \
  55. __stringify(minor) "_" __stringify(bld_num) ".bin"
  56. #define I915_SKL_HUC_UCODE HUC_FW_PATH(skl, SKL_HUC_FW_MAJOR, \
  57. SKL_HUC_FW_MINOR, SKL_BLD_NUM)
  58. MODULE_FIRMWARE(I915_SKL_HUC_UCODE);
  59. #define I915_BXT_HUC_UCODE HUC_FW_PATH(bxt, BXT_HUC_FW_MAJOR, \
  60. BXT_HUC_FW_MINOR, BXT_BLD_NUM)
  61. MODULE_FIRMWARE(I915_BXT_HUC_UCODE);
  62. #define I915_KBL_HUC_UCODE HUC_FW_PATH(kbl, KBL_HUC_FW_MAJOR, \
  63. KBL_HUC_FW_MINOR, KBL_BLD_NUM)
  64. MODULE_FIRMWARE(I915_KBL_HUC_UCODE);
  65. #define I915_GLK_HUC_UCODE HUC_FW_PATH(glk, GLK_HUC_FW_MAJOR, \
  66. GLK_HUC_FW_MINOR, GLK_BLD_NUM)
  67. static void huc_fw_select(struct intel_uc_fw *huc_fw)
  68. {
  69. struct intel_huc *huc = container_of(huc_fw, struct intel_huc, fw);
  70. struct drm_i915_private *dev_priv = huc_to_i915(huc);
  71. GEM_BUG_ON(huc_fw->type != INTEL_UC_FW_TYPE_HUC);
  72. if (!HAS_HUC(dev_priv))
  73. return;
  74. if (i915_modparams.huc_firmware_path) {
  75. huc_fw->path = i915_modparams.huc_firmware_path;
  76. huc_fw->major_ver_wanted = 0;
  77. huc_fw->minor_ver_wanted = 0;
  78. } else if (IS_SKYLAKE(dev_priv)) {
  79. huc_fw->path = I915_SKL_HUC_UCODE;
  80. huc_fw->major_ver_wanted = SKL_HUC_FW_MAJOR;
  81. huc_fw->minor_ver_wanted = SKL_HUC_FW_MINOR;
  82. } else if (IS_BROXTON(dev_priv)) {
  83. huc_fw->path = I915_BXT_HUC_UCODE;
  84. huc_fw->major_ver_wanted = BXT_HUC_FW_MAJOR;
  85. huc_fw->minor_ver_wanted = BXT_HUC_FW_MINOR;
  86. } else if (IS_KABYLAKE(dev_priv) || IS_COFFEELAKE(dev_priv)) {
  87. huc_fw->path = I915_KBL_HUC_UCODE;
  88. huc_fw->major_ver_wanted = KBL_HUC_FW_MAJOR;
  89. huc_fw->minor_ver_wanted = KBL_HUC_FW_MINOR;
  90. } else if (IS_GEMINILAKE(dev_priv)) {
  91. huc_fw->path = I915_GLK_HUC_UCODE;
  92. huc_fw->major_ver_wanted = GLK_HUC_FW_MAJOR;
  93. huc_fw->minor_ver_wanted = GLK_HUC_FW_MINOR;
  94. } else {
  95. DRM_WARN("%s: No firmware known for this platform!\n",
  96. intel_uc_fw_type_repr(huc_fw->type));
  97. }
  98. }
  99. /**
  100. * intel_huc_init_early() - initializes HuC struct
  101. * @huc: intel_huc struct
  102. *
  103. * On platforms with HuC selects firmware for uploading
  104. */
  105. void intel_huc_init_early(struct intel_huc *huc)
  106. {
  107. struct intel_uc_fw *huc_fw = &huc->fw;
  108. intel_uc_fw_init(huc_fw, INTEL_UC_FW_TYPE_HUC);
  109. huc_fw_select(huc_fw);
  110. }
  111. /**
  112. * huc_ucode_xfer() - DMA's the firmware
  113. * @dev_priv: the drm_i915_private device
  114. *
  115. * Transfer the firmware image to RAM for execution by the microcontroller.
  116. *
  117. * Return: 0 on success, non-zero on failure
  118. */
  119. static int huc_ucode_xfer(struct intel_uc_fw *huc_fw, struct i915_vma *vma)
  120. {
  121. struct intel_huc *huc = container_of(huc_fw, struct intel_huc, fw);
  122. struct drm_i915_private *dev_priv = huc_to_i915(huc);
  123. unsigned long offset = 0;
  124. u32 size;
  125. int ret;
  126. GEM_BUG_ON(huc_fw->type != INTEL_UC_FW_TYPE_HUC);
  127. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  128. /* Set the source address for the uCode */
  129. offset = guc_ggtt_offset(vma) + huc_fw->header_offset;
  130. I915_WRITE(DMA_ADDR_0_LOW, lower_32_bits(offset));
  131. I915_WRITE(DMA_ADDR_0_HIGH, upper_32_bits(offset) & 0xFFFF);
  132. /* Hardware doesn't look at destination address for HuC. Set it to 0,
  133. * but still program the correct address space.
  134. */
  135. I915_WRITE(DMA_ADDR_1_LOW, 0);
  136. I915_WRITE(DMA_ADDR_1_HIGH, DMA_ADDRESS_SPACE_WOPCM);
  137. size = huc_fw->header_size + huc_fw->ucode_size;
  138. I915_WRITE(DMA_COPY_SIZE, size);
  139. /* Start the DMA */
  140. I915_WRITE(DMA_CTRL, _MASKED_BIT_ENABLE(HUC_UKERNEL | START_DMA));
  141. /* Wait for DMA to finish */
  142. ret = intel_wait_for_register_fw(dev_priv, DMA_CTRL, START_DMA, 0, 100);
  143. DRM_DEBUG_DRIVER("HuC DMA transfer wait over with ret %d\n", ret);
  144. /* Disable the bits once DMA is over */
  145. I915_WRITE(DMA_CTRL, _MASKED_BIT_DISABLE(HUC_UKERNEL));
  146. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  147. return ret;
  148. }
  149. /**
  150. * intel_huc_init_hw() - load HuC uCode to device
  151. * @huc: intel_huc structure
  152. *
  153. * Called from intel_uc_init_hw() during driver loading and also after a GPU
  154. * reset. Be note that HuC loading must be done before GuC loading.
  155. *
  156. * The firmware image should have already been fetched into memory by the
  157. * earlier call to intel_uc_init_fw(), so here we need only check that
  158. * is succeeded, and then transfer the image to the h/w.
  159. *
  160. */
  161. int intel_huc_init_hw(struct intel_huc *huc)
  162. {
  163. return intel_uc_fw_upload(&huc->fw, huc_ucode_xfer);
  164. }
  165. /**
  166. * intel_huc_auth() - Authenticate HuC uCode
  167. * @huc: intel_huc structure
  168. *
  169. * Called after HuC and GuC firmware loading during intel_uc_init_hw().
  170. *
  171. * This function pins HuC firmware image object into GGTT.
  172. * Then it invokes GuC action to authenticate passing the offset to RSA
  173. * signature through intel_guc_auth_huc(). It then waits for 50ms for
  174. * firmware verification ACK and unpins the object.
  175. */
  176. int intel_huc_auth(struct intel_huc *huc)
  177. {
  178. struct drm_i915_private *i915 = huc_to_i915(huc);
  179. struct intel_guc *guc = &i915->guc;
  180. struct i915_vma *vma;
  181. int ret;
  182. if (huc->fw.load_status != INTEL_UC_FIRMWARE_SUCCESS)
  183. return -ENOEXEC;
  184. vma = i915_gem_object_ggtt_pin(huc->fw.obj, NULL, 0, 0,
  185. PIN_OFFSET_BIAS | GUC_WOPCM_TOP);
  186. if (IS_ERR(vma)) {
  187. ret = PTR_ERR(vma);
  188. DRM_ERROR("HuC: Failed to pin huc fw object %d\n", ret);
  189. return ret;
  190. }
  191. ret = intel_guc_auth_huc(guc,
  192. guc_ggtt_offset(vma) + huc->fw.rsa_offset);
  193. if (ret) {
  194. DRM_ERROR("HuC: GuC did not ack Auth request %d\n", ret);
  195. goto out;
  196. }
  197. /* Check authentication status, it should be done by now */
  198. ret = intel_wait_for_register(i915,
  199. HUC_STATUS2,
  200. HUC_FW_VERIFIED,
  201. HUC_FW_VERIFIED,
  202. 50);
  203. if (ret) {
  204. DRM_ERROR("HuC: Authentication failed %d\n", ret);
  205. goto out;
  206. }
  207. out:
  208. i915_vma_unpin(vma);
  209. return ret;
  210. }