intel_huc.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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/firmware.h>
  25. #include "i915_drv.h"
  26. #include "intel_uc.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 HUC_FW_PATH(platform, major, minor, bld_num) \
  51. "i915/" __stringify(platform) "_huc_ver" __stringify(major) "_" \
  52. __stringify(minor) "_" __stringify(bld_num) ".bin"
  53. #define I915_SKL_HUC_UCODE HUC_FW_PATH(skl, SKL_HUC_FW_MAJOR, \
  54. SKL_HUC_FW_MINOR, SKL_BLD_NUM)
  55. MODULE_FIRMWARE(I915_SKL_HUC_UCODE);
  56. #define I915_BXT_HUC_UCODE HUC_FW_PATH(bxt, BXT_HUC_FW_MAJOR, \
  57. BXT_HUC_FW_MINOR, BXT_BLD_NUM)
  58. MODULE_FIRMWARE(I915_BXT_HUC_UCODE);
  59. #define I915_KBL_HUC_UCODE HUC_FW_PATH(kbl, KBL_HUC_FW_MAJOR, \
  60. KBL_HUC_FW_MINOR, KBL_BLD_NUM)
  61. MODULE_FIRMWARE(I915_KBL_HUC_UCODE);
  62. /**
  63. * huc_ucode_xfer() - DMA's the firmware
  64. * @dev_priv: the drm_i915_private device
  65. *
  66. * Transfer the firmware image to RAM for execution by the microcontroller.
  67. *
  68. * Return: 0 on success, non-zero on failure
  69. */
  70. static int huc_ucode_xfer(struct drm_i915_private *dev_priv)
  71. {
  72. struct intel_uc_fw *huc_fw = &dev_priv->huc.fw;
  73. struct i915_vma *vma;
  74. unsigned long offset = 0;
  75. u32 size;
  76. int ret;
  77. ret = i915_gem_object_set_to_gtt_domain(huc_fw->obj, false);
  78. if (ret) {
  79. DRM_DEBUG_DRIVER("set-domain failed %d\n", ret);
  80. return ret;
  81. }
  82. vma = i915_gem_object_ggtt_pin(huc_fw->obj, NULL, 0, 0,
  83. PIN_OFFSET_BIAS | GUC_WOPCM_TOP);
  84. if (IS_ERR(vma)) {
  85. DRM_DEBUG_DRIVER("pin failed %d\n", (int)PTR_ERR(vma));
  86. return PTR_ERR(vma);
  87. }
  88. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  89. /* init WOPCM */
  90. I915_WRITE(GUC_WOPCM_SIZE, intel_guc_wopcm_size(dev_priv));
  91. I915_WRITE(DMA_GUC_WOPCM_OFFSET, GUC_WOPCM_OFFSET_VALUE |
  92. HUC_LOADING_AGENT_GUC);
  93. /* Set the source address for the uCode */
  94. offset = guc_ggtt_offset(vma) + huc_fw->header_offset;
  95. I915_WRITE(DMA_ADDR_0_LOW, lower_32_bits(offset));
  96. I915_WRITE(DMA_ADDR_0_HIGH, upper_32_bits(offset) & 0xFFFF);
  97. /* Hardware doesn't look at destination address for HuC. Set it to 0,
  98. * but still program the correct address space.
  99. */
  100. I915_WRITE(DMA_ADDR_1_LOW, 0);
  101. I915_WRITE(DMA_ADDR_1_HIGH, DMA_ADDRESS_SPACE_WOPCM);
  102. size = huc_fw->header_size + huc_fw->ucode_size;
  103. I915_WRITE(DMA_COPY_SIZE, size);
  104. /* Start the DMA */
  105. I915_WRITE(DMA_CTRL, _MASKED_BIT_ENABLE(HUC_UKERNEL | START_DMA));
  106. /* Wait for DMA to finish */
  107. ret = wait_for((I915_READ(DMA_CTRL) & START_DMA) == 0, 100);
  108. DRM_DEBUG_DRIVER("HuC DMA transfer wait over with ret %d\n", ret);
  109. /* Disable the bits once DMA is over */
  110. I915_WRITE(DMA_CTRL, _MASKED_BIT_DISABLE(HUC_UKERNEL));
  111. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  112. /*
  113. * We keep the object pages for reuse during resume. But we can unpin it
  114. * now that DMA has completed, so it doesn't continue to take up space.
  115. */
  116. i915_vma_unpin(vma);
  117. return ret;
  118. }
  119. /**
  120. * intel_huc_init() - initiate HuC firmware loading request
  121. * @dev_priv: the drm_i915_private device
  122. *
  123. * Called early during driver load, but after GEM is initialised. The loading
  124. * will continue only when driver explicitly specify firmware name and version.
  125. * All other cases are considered as INTEL_UC_FIRMWARE_NONE either because HW
  126. * is not capable or driver yet support it. And there will be no error message
  127. * for INTEL_UC_FIRMWARE_NONE cases.
  128. *
  129. * The DMA-copying to HW is done later when intel_huc_load() is called.
  130. */
  131. void intel_huc_init(struct drm_i915_private *dev_priv)
  132. {
  133. struct intel_huc *huc = &dev_priv->huc;
  134. struct intel_uc_fw *huc_fw = &huc->fw;
  135. const char *fw_path = NULL;
  136. huc_fw->path = NULL;
  137. huc_fw->fetch_status = INTEL_UC_FIRMWARE_NONE;
  138. huc_fw->load_status = INTEL_UC_FIRMWARE_NONE;
  139. huc_fw->fw = INTEL_UC_FW_TYPE_HUC;
  140. if (!HAS_HUC_UCODE(dev_priv))
  141. return;
  142. if (IS_SKYLAKE(dev_priv)) {
  143. fw_path = I915_SKL_HUC_UCODE;
  144. huc_fw->major_ver_wanted = SKL_HUC_FW_MAJOR;
  145. huc_fw->minor_ver_wanted = SKL_HUC_FW_MINOR;
  146. } else if (IS_BROXTON(dev_priv)) {
  147. fw_path = I915_BXT_HUC_UCODE;
  148. huc_fw->major_ver_wanted = BXT_HUC_FW_MAJOR;
  149. huc_fw->minor_ver_wanted = BXT_HUC_FW_MINOR;
  150. } else if (IS_KABYLAKE(dev_priv)) {
  151. fw_path = I915_KBL_HUC_UCODE;
  152. huc_fw->major_ver_wanted = KBL_HUC_FW_MAJOR;
  153. huc_fw->minor_ver_wanted = KBL_HUC_FW_MINOR;
  154. }
  155. huc_fw->path = fw_path;
  156. huc_fw->fetch_status = INTEL_UC_FIRMWARE_PENDING;
  157. DRM_DEBUG_DRIVER("HuC firmware pending, path %s\n", fw_path);
  158. WARN(huc_fw->path == NULL, "HuC present but no fw path\n");
  159. intel_uc_fw_fetch(dev_priv, huc_fw);
  160. }
  161. /**
  162. * intel_huc_load() - load HuC uCode to device
  163. * @dev_priv: the drm_i915_private device
  164. *
  165. * Called from guc_setup() during driver loading and also after a GPU reset.
  166. * Be note that HuC loading must be done before GuC loading.
  167. *
  168. * The firmware image should have already been fetched into memory by the
  169. * earlier call to intel_huc_init(), so here we need only check that
  170. * is succeeded, and then transfer the image to the h/w.
  171. *
  172. * Return: non-zero code on error
  173. */
  174. int intel_huc_load(struct drm_i915_private *dev_priv)
  175. {
  176. struct intel_uc_fw *huc_fw = &dev_priv->huc.fw;
  177. int err;
  178. if (huc_fw->fetch_status == INTEL_UC_FIRMWARE_NONE)
  179. return 0;
  180. DRM_DEBUG_DRIVER("%s fw status: fetch %s, load %s\n",
  181. huc_fw->path,
  182. intel_uc_fw_status_repr(huc_fw->fetch_status),
  183. intel_uc_fw_status_repr(huc_fw->load_status));
  184. if (huc_fw->fetch_status == INTEL_UC_FIRMWARE_SUCCESS &&
  185. huc_fw->load_status == INTEL_UC_FIRMWARE_FAIL)
  186. return -ENOEXEC;
  187. huc_fw->load_status = INTEL_UC_FIRMWARE_PENDING;
  188. switch (huc_fw->fetch_status) {
  189. case INTEL_UC_FIRMWARE_FAIL:
  190. /* something went wrong :( */
  191. err = -EIO;
  192. goto fail;
  193. case INTEL_UC_FIRMWARE_NONE:
  194. case INTEL_UC_FIRMWARE_PENDING:
  195. default:
  196. /* "can't happen" */
  197. WARN_ONCE(1, "HuC fw %s invalid fetch_status %s [%d]\n",
  198. huc_fw->path,
  199. intel_uc_fw_status_repr(huc_fw->fetch_status),
  200. huc_fw->fetch_status);
  201. err = -ENXIO;
  202. goto fail;
  203. case INTEL_UC_FIRMWARE_SUCCESS:
  204. break;
  205. }
  206. err = huc_ucode_xfer(dev_priv);
  207. if (err)
  208. goto fail;
  209. huc_fw->load_status = INTEL_UC_FIRMWARE_SUCCESS;
  210. DRM_DEBUG_DRIVER("%s fw status: fetch %s, load %s\n",
  211. huc_fw->path,
  212. intel_uc_fw_status_repr(huc_fw->fetch_status),
  213. intel_uc_fw_status_repr(huc_fw->load_status));
  214. return 0;
  215. fail:
  216. if (huc_fw->load_status == INTEL_UC_FIRMWARE_PENDING)
  217. huc_fw->load_status = INTEL_UC_FIRMWARE_FAIL;
  218. DRM_ERROR("Failed to complete HuC uCode load with ret %d\n", err);
  219. return err;
  220. }
  221. /**
  222. * intel_huc_fini() - clean up resources allocated for HuC
  223. * @dev_priv: the drm_i915_private device
  224. *
  225. * Cleans up by releasing the huc firmware GEM obj.
  226. */
  227. void intel_huc_fini(struct drm_i915_private *dev_priv)
  228. {
  229. struct intel_uc_fw *huc_fw = &dev_priv->huc.fw;
  230. mutex_lock(&dev_priv->drm.struct_mutex);
  231. if (huc_fw->obj)
  232. i915_gem_object_put(huc_fw->obj);
  233. huc_fw->obj = NULL;
  234. mutex_unlock(&dev_priv->drm.struct_mutex);
  235. huc_fw->fetch_status = INTEL_UC_FIRMWARE_NONE;
  236. }
  237. /**
  238. * intel_guc_auth_huc() - authenticate ucode
  239. * @dev_priv: the drm_i915_device
  240. *
  241. * Triggers a HuC fw authentication request to the GuC via intel_guc_action_
  242. * authenticate_huc interface.
  243. */
  244. void intel_guc_auth_huc(struct drm_i915_private *dev_priv)
  245. {
  246. struct intel_guc *guc = &dev_priv->guc;
  247. struct intel_huc *huc = &dev_priv->huc;
  248. struct i915_vma *vma;
  249. int ret;
  250. u32 data[2];
  251. if (huc->fw.load_status != INTEL_UC_FIRMWARE_SUCCESS)
  252. return;
  253. vma = i915_gem_object_ggtt_pin(huc->fw.obj, NULL, 0, 0,
  254. PIN_OFFSET_BIAS | GUC_WOPCM_TOP);
  255. if (IS_ERR(vma)) {
  256. DRM_ERROR("failed to pin huc fw object %d\n",
  257. (int)PTR_ERR(vma));
  258. return;
  259. }
  260. /* Specify auth action and where public signature is. */
  261. data[0] = INTEL_GUC_ACTION_AUTHENTICATE_HUC;
  262. data[1] = guc_ggtt_offset(vma) + huc->fw.rsa_offset;
  263. ret = intel_guc_send(guc, data, ARRAY_SIZE(data));
  264. if (ret) {
  265. DRM_ERROR("HuC: GuC did not ack Auth request %d\n", ret);
  266. goto out;
  267. }
  268. /* Check authentication status, it should be done by now */
  269. ret = intel_wait_for_register(dev_priv,
  270. HUC_STATUS2,
  271. HUC_FW_VERIFIED,
  272. HUC_FW_VERIFIED,
  273. 50);
  274. if (ret) {
  275. DRM_ERROR("HuC: Authentication failed %d\n", ret);
  276. goto out;
  277. }
  278. out:
  279. i915_vma_unpin(vma);
  280. }