intel_uc.c 12 KB

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