intel_uc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /*
  2. * Copyright © 2016 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 "i915_drv.h"
  25. #include "intel_uc.h"
  26. #include <linux/firmware.h>
  27. /* Reset GuC providing us with fresh state for both GuC and HuC.
  28. */
  29. static int __intel_uc_reset_hw(struct drm_i915_private *dev_priv)
  30. {
  31. int ret;
  32. u32 guc_status;
  33. ret = intel_guc_reset(dev_priv);
  34. if (ret) {
  35. DRM_ERROR("GuC reset failed, ret = %d\n", ret);
  36. return ret;
  37. }
  38. guc_status = I915_READ(GUC_STATUS);
  39. WARN(!(guc_status & GS_MIA_IN_RESET),
  40. "GuC status: 0x%x, MIA core expected to be in reset\n",
  41. guc_status);
  42. return ret;
  43. }
  44. void intel_uc_sanitize_options(struct drm_i915_private *dev_priv)
  45. {
  46. if (!HAS_GUC(dev_priv)) {
  47. if (i915.enable_guc_loading > 0 ||
  48. i915.enable_guc_submission > 0)
  49. DRM_INFO("Ignoring GuC options, no hardware\n");
  50. i915.enable_guc_loading = 0;
  51. i915.enable_guc_submission = 0;
  52. return;
  53. }
  54. /* A negative value means "use platform default" */
  55. if (i915.enable_guc_loading < 0)
  56. i915.enable_guc_loading = HAS_GUC_UCODE(dev_priv);
  57. /* Verify firmware version */
  58. if (i915.enable_guc_loading) {
  59. if (HAS_HUC_UCODE(dev_priv))
  60. intel_huc_select_fw(&dev_priv->huc);
  61. if (intel_guc_select_fw(&dev_priv->guc))
  62. i915.enable_guc_loading = 0;
  63. }
  64. /* Can't enable guc submission without guc loaded */
  65. if (!i915.enable_guc_loading)
  66. i915.enable_guc_submission = 0;
  67. /* A negative value means "use platform default" */
  68. if (i915.enable_guc_submission < 0)
  69. i915.enable_guc_submission = HAS_GUC_SCHED(dev_priv);
  70. }
  71. void intel_uc_init_early(struct drm_i915_private *dev_priv)
  72. {
  73. mutex_init(&dev_priv->guc.send_mutex);
  74. }
  75. void intel_uc_init_fw(struct drm_i915_private *dev_priv)
  76. {
  77. if (dev_priv->huc.fw.path)
  78. intel_uc_prepare_fw(dev_priv, &dev_priv->huc.fw);
  79. if (dev_priv->guc.fw.path)
  80. intel_uc_prepare_fw(dev_priv, &dev_priv->guc.fw);
  81. }
  82. int intel_uc_init_hw(struct drm_i915_private *dev_priv)
  83. {
  84. int ret, attempts;
  85. /* GuC not enabled, nothing to do */
  86. if (!i915.enable_guc_loading)
  87. return 0;
  88. gen9_reset_guc_interrupts(dev_priv);
  89. /* We need to notify the guc whenever we change the GGTT */
  90. i915_ggtt_enable_guc(dev_priv);
  91. if (i915.enable_guc_submission) {
  92. ret = i915_guc_submission_init(dev_priv);
  93. if (ret)
  94. goto err;
  95. }
  96. /* WaEnableuKernelHeaderValidFix:skl */
  97. /* WaEnableGuCBootHashCheckNotSet:skl,bxt,kbl */
  98. if (IS_GEN9(dev_priv))
  99. attempts = 3;
  100. else
  101. attempts = 1;
  102. while (attempts--) {
  103. /*
  104. * Always reset the GuC just before (re)loading, so
  105. * that the state and timing are fairly predictable
  106. */
  107. ret = __intel_uc_reset_hw(dev_priv);
  108. if (ret)
  109. goto err_submission;
  110. intel_huc_init_hw(&dev_priv->huc);
  111. ret = intel_guc_init_hw(&dev_priv->guc);
  112. if (ret == 0 || ret != -EAGAIN)
  113. break;
  114. DRM_DEBUG_DRIVER("GuC fw load failed: %d; will reset and "
  115. "retry %d more time(s)\n", ret, attempts);
  116. }
  117. /* Did we succeded or run out of retries? */
  118. if (ret)
  119. goto err_submission;
  120. intel_guc_auth_huc(dev_priv);
  121. if (i915.enable_guc_submission) {
  122. if (i915.guc_log_level >= 0)
  123. gen9_enable_guc_interrupts(dev_priv);
  124. ret = i915_guc_submission_enable(dev_priv);
  125. if (ret)
  126. goto err_submission;
  127. }
  128. return 0;
  129. /*
  130. * We've failed to load the firmware :(
  131. *
  132. * Decide whether to disable GuC submission and fall back to
  133. * execlist mode, and whether to hide the error by returning
  134. * zero or to return -EIO, which the caller will treat as a
  135. * nonfatal error (i.e. it doesn't prevent driver load, but
  136. * marks the GPU as wedged until reset).
  137. */
  138. err_submission:
  139. if (i915.enable_guc_submission)
  140. i915_guc_submission_fini(dev_priv);
  141. err:
  142. i915_ggtt_disable_guc(dev_priv);
  143. DRM_ERROR("GuC init failed\n");
  144. if (i915.enable_guc_loading > 1 || i915.enable_guc_submission > 1)
  145. ret = -EIO;
  146. else
  147. ret = 0;
  148. if (i915.enable_guc_submission) {
  149. i915.enable_guc_submission = 0;
  150. DRM_NOTE("Falling back from GuC submission to execlist mode\n");
  151. }
  152. return ret;
  153. }
  154. /*
  155. * Read GuC command/status register (SOFT_SCRATCH_0)
  156. * Return true if it contains a response rather than a command
  157. */
  158. static bool intel_guc_recv(struct intel_guc *guc, u32 *status)
  159. {
  160. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  161. u32 val = I915_READ(SOFT_SCRATCH(0));
  162. *status = val;
  163. return INTEL_GUC_RECV_IS_RESPONSE(val);
  164. }
  165. int intel_guc_send(struct intel_guc *guc, const u32 *action, u32 len)
  166. {
  167. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  168. u32 status;
  169. int i;
  170. int ret;
  171. if (WARN_ON(len < 1 || len > 15))
  172. return -EINVAL;
  173. mutex_lock(&guc->send_mutex);
  174. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  175. dev_priv->guc.action_count += 1;
  176. dev_priv->guc.action_cmd = action[0];
  177. for (i = 0; i < len; i++)
  178. I915_WRITE(SOFT_SCRATCH(i), action[i]);
  179. POSTING_READ(SOFT_SCRATCH(i - 1));
  180. I915_WRITE(GUC_SEND_INTERRUPT, GUC_SEND_TRIGGER);
  181. /*
  182. * Fast commands should complete in less than 10us, so sample quickly
  183. * up to that length of time, then switch to a slower sleep-wait loop.
  184. * No inte_guc_send command should ever take longer than 10ms.
  185. */
  186. ret = wait_for_us(intel_guc_recv(guc, &status), 10);
  187. if (ret)
  188. ret = wait_for(intel_guc_recv(guc, &status), 10);
  189. if (status != INTEL_GUC_STATUS_SUCCESS) {
  190. /*
  191. * Either the GuC explicitly returned an error (which
  192. * we convert to -EIO here) or no response at all was
  193. * received within the timeout limit (-ETIMEDOUT)
  194. */
  195. if (ret != -ETIMEDOUT)
  196. ret = -EIO;
  197. DRM_WARN("INTEL_GUC_SEND: Action 0x%X failed;"
  198. " ret=%d status=0x%08X response=0x%08X\n",
  199. action[0], ret, status, I915_READ(SOFT_SCRATCH(15)));
  200. dev_priv->guc.action_fail += 1;
  201. dev_priv->guc.action_err = ret;
  202. }
  203. dev_priv->guc.action_status = status;
  204. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  205. mutex_unlock(&guc->send_mutex);
  206. return ret;
  207. }
  208. int intel_guc_sample_forcewake(struct intel_guc *guc)
  209. {
  210. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  211. u32 action[2];
  212. action[0] = INTEL_GUC_ACTION_SAMPLE_FORCEWAKE;
  213. /* WaRsDisableCoarsePowerGating:skl,bxt */
  214. if (!intel_enable_rc6() || NEEDS_WaRsDisableCoarsePowerGating(dev_priv))
  215. action[1] = 0;
  216. else
  217. /* bit 0 and 1 are for Render and Media domain separately */
  218. action[1] = GUC_FORCEWAKE_RENDER | GUC_FORCEWAKE_MEDIA;
  219. return intel_guc_send(guc, action, ARRAY_SIZE(action));
  220. }
  221. void intel_uc_prepare_fw(struct drm_i915_private *dev_priv,
  222. struct intel_uc_fw *uc_fw)
  223. {
  224. struct pci_dev *pdev = dev_priv->drm.pdev;
  225. struct drm_i915_gem_object *obj;
  226. const struct firmware *fw = NULL;
  227. struct uc_css_header *css;
  228. size_t size;
  229. int err;
  230. uc_fw->fetch_status = INTEL_UC_FIRMWARE_PENDING;
  231. DRM_DEBUG_DRIVER("before requesting firmware: uC fw fetch status %s\n",
  232. intel_uc_fw_status_repr(uc_fw->fetch_status));
  233. err = request_firmware(&fw, uc_fw->path, &pdev->dev);
  234. if (err)
  235. goto fail;
  236. if (!fw)
  237. goto fail;
  238. DRM_DEBUG_DRIVER("fetch uC fw from %s succeeded, fw %p\n",
  239. uc_fw->path, fw);
  240. /* Check the size of the blob before examining buffer contents */
  241. if (fw->size < sizeof(struct uc_css_header)) {
  242. DRM_NOTE("Firmware header is missing\n");
  243. goto fail;
  244. }
  245. css = (struct uc_css_header *)fw->data;
  246. /* Firmware bits always start from header */
  247. uc_fw->header_offset = 0;
  248. uc_fw->header_size = (css->header_size_dw - css->modulus_size_dw -
  249. css->key_size_dw - css->exponent_size_dw) * sizeof(u32);
  250. if (uc_fw->header_size != sizeof(struct uc_css_header)) {
  251. DRM_NOTE("CSS header definition mismatch\n");
  252. goto fail;
  253. }
  254. /* then, uCode */
  255. uc_fw->ucode_offset = uc_fw->header_offset + uc_fw->header_size;
  256. uc_fw->ucode_size = (css->size_dw - css->header_size_dw) * sizeof(u32);
  257. /* now RSA */
  258. if (css->key_size_dw != UOS_RSA_SCRATCH_MAX_COUNT) {
  259. DRM_NOTE("RSA key size is bad\n");
  260. goto fail;
  261. }
  262. uc_fw->rsa_offset = uc_fw->ucode_offset + uc_fw->ucode_size;
  263. uc_fw->rsa_size = css->key_size_dw * sizeof(u32);
  264. /* At least, it should have header, uCode and RSA. Size of all three. */
  265. size = uc_fw->header_size + uc_fw->ucode_size + uc_fw->rsa_size;
  266. if (fw->size < size) {
  267. DRM_NOTE("Missing firmware components\n");
  268. goto fail;
  269. }
  270. /*
  271. * The GuC firmware image has the version number embedded at a
  272. * well-known offset within the firmware blob; note that major / minor
  273. * version are TWO bytes each (i.e. u16), although all pointers and
  274. * offsets are defined in terms of bytes (u8).
  275. */
  276. switch (uc_fw->type) {
  277. case INTEL_UC_FW_TYPE_GUC:
  278. /* Header and uCode will be loaded to WOPCM. Size of the two. */
  279. size = uc_fw->header_size + uc_fw->ucode_size;
  280. /* Top 32k of WOPCM is reserved (8K stack + 24k RC6 context). */
  281. if (size > intel_guc_wopcm_size(dev_priv)) {
  282. DRM_ERROR("Firmware is too large to fit in WOPCM\n");
  283. goto fail;
  284. }
  285. uc_fw->major_ver_found = css->guc.sw_version >> 16;
  286. uc_fw->minor_ver_found = css->guc.sw_version & 0xFFFF;
  287. break;
  288. case INTEL_UC_FW_TYPE_HUC:
  289. uc_fw->major_ver_found = css->huc.sw_version >> 16;
  290. uc_fw->minor_ver_found = css->huc.sw_version & 0xFFFF;
  291. break;
  292. default:
  293. DRM_ERROR("Unknown firmware type %d\n", uc_fw->type);
  294. err = -ENOEXEC;
  295. goto fail;
  296. }
  297. if (uc_fw->major_ver_wanted == 0 && uc_fw->minor_ver_wanted == 0) {
  298. DRM_NOTE("Skipping uC firmware version check\n");
  299. } else if (uc_fw->major_ver_found != uc_fw->major_ver_wanted ||
  300. uc_fw->minor_ver_found < uc_fw->minor_ver_wanted) {
  301. DRM_NOTE("uC firmware version %d.%d, required %d.%d\n",
  302. uc_fw->major_ver_found, uc_fw->minor_ver_found,
  303. uc_fw->major_ver_wanted, uc_fw->minor_ver_wanted);
  304. err = -ENOEXEC;
  305. goto fail;
  306. }
  307. DRM_DEBUG_DRIVER("firmware version %d.%d OK (minimum %d.%d)\n",
  308. uc_fw->major_ver_found, uc_fw->minor_ver_found,
  309. uc_fw->major_ver_wanted, uc_fw->minor_ver_wanted);
  310. obj = i915_gem_object_create_from_data(dev_priv, fw->data, fw->size);
  311. if (IS_ERR(obj)) {
  312. err = PTR_ERR(obj);
  313. goto fail;
  314. }
  315. uc_fw->obj = obj;
  316. uc_fw->size = fw->size;
  317. DRM_DEBUG_DRIVER("uC fw fetch status SUCCESS, obj %p\n",
  318. uc_fw->obj);
  319. release_firmware(fw);
  320. uc_fw->fetch_status = INTEL_UC_FIRMWARE_SUCCESS;
  321. return;
  322. fail:
  323. DRM_WARN("Failed to fetch valid uC firmware from %s (error %d)\n",
  324. uc_fw->path, err);
  325. DRM_DEBUG_DRIVER("uC fw fetch status FAIL; err %d, fw %p, obj %p\n",
  326. err, fw, uc_fw->obj);
  327. release_firmware(fw); /* OK even if fw is NULL */
  328. uc_fw->fetch_status = INTEL_UC_FIRMWARE_FAIL;
  329. }