intel_uc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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 "intel_uc.h"
  25. #include "intel_guc_submission.h"
  26. #include "intel_guc.h"
  27. #include "i915_drv.h"
  28. static void guc_free_load_err_log(struct intel_guc *guc);
  29. /* Reset GuC providing us with fresh state for both GuC and HuC.
  30. */
  31. static int __intel_uc_reset_hw(struct drm_i915_private *dev_priv)
  32. {
  33. int ret;
  34. u32 guc_status;
  35. ret = intel_reset_guc(dev_priv);
  36. if (ret) {
  37. DRM_ERROR("Failed to reset GuC, ret = %d\n", ret);
  38. return ret;
  39. }
  40. guc_status = I915_READ(GUC_STATUS);
  41. WARN(!(guc_status & GS_MIA_IN_RESET),
  42. "GuC status: 0x%x, MIA core expected to be in reset\n",
  43. guc_status);
  44. return ret;
  45. }
  46. static int __get_platform_enable_guc(struct drm_i915_private *dev_priv)
  47. {
  48. struct intel_uc_fw *guc_fw = &dev_priv->guc.fw;
  49. struct intel_uc_fw *huc_fw = &dev_priv->huc.fw;
  50. int enable_guc = 0;
  51. /* Default is to enable GuC/HuC if we know their firmwares */
  52. if (intel_uc_fw_is_selected(guc_fw))
  53. enable_guc |= ENABLE_GUC_SUBMISSION;
  54. if (intel_uc_fw_is_selected(huc_fw))
  55. enable_guc |= ENABLE_GUC_LOAD_HUC;
  56. /* Any platform specific fine-tuning can be done here */
  57. return enable_guc;
  58. }
  59. static int __get_default_guc_log_level(struct drm_i915_private *dev_priv)
  60. {
  61. int guc_log_level = 0; /* disabled */
  62. /* Enable if we're running on platform with GuC and debug config */
  63. if (HAS_GUC(dev_priv) && intel_uc_is_using_guc() &&
  64. (IS_ENABLED(CONFIG_DRM_I915_DEBUG) ||
  65. IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)))
  66. guc_log_level = 1 + GUC_LOG_VERBOSITY_MAX;
  67. /* Any platform specific fine-tuning can be done here */
  68. return guc_log_level;
  69. }
  70. /**
  71. * intel_uc_sanitize_options - sanitize uC related modparam options
  72. * @dev_priv: device private
  73. *
  74. * In case of "enable_guc" option this function will attempt to modify
  75. * it only if it was initially set to "auto(-1)". Default value for this
  76. * modparam varies between platforms and it is hardcoded in driver code.
  77. * Any other modparam value is only monitored against availability of the
  78. * related hardware or firmware definitions.
  79. *
  80. * In case of "guc_log_level" option this function will attempt to modify
  81. * it only if it was initially set to "auto(-1)" or if initial value was
  82. * "enable(1..4)" on platforms without the GuC. Default value for this
  83. * modparam varies between platforms and is usually set to "disable(0)"
  84. * unless GuC is enabled on given platform and the driver is compiled with
  85. * debug config when this modparam will default to "enable(1..4)".
  86. */
  87. void intel_uc_sanitize_options(struct drm_i915_private *dev_priv)
  88. {
  89. struct intel_uc_fw *guc_fw = &dev_priv->guc.fw;
  90. struct intel_uc_fw *huc_fw = &dev_priv->huc.fw;
  91. /* A negative value means "use platform default" */
  92. if (i915_modparams.enable_guc < 0)
  93. i915_modparams.enable_guc = __get_platform_enable_guc(dev_priv);
  94. DRM_DEBUG_DRIVER("enable_guc=%d (submission:%s huc:%s)\n",
  95. i915_modparams.enable_guc,
  96. yesno(intel_uc_is_using_guc_submission()),
  97. yesno(intel_uc_is_using_huc()));
  98. /* Verify GuC firmware availability */
  99. if (intel_uc_is_using_guc() && !intel_uc_fw_is_selected(guc_fw)) {
  100. DRM_WARN("Incompatible option detected: %s=%d, %s!\n",
  101. "enable_guc", i915_modparams.enable_guc,
  102. !HAS_GUC(dev_priv) ? "no GuC hardware" :
  103. "no GuC firmware");
  104. }
  105. /* Verify HuC firmware availability */
  106. if (intel_uc_is_using_huc() && !intel_uc_fw_is_selected(huc_fw)) {
  107. DRM_WARN("Incompatible option detected: %s=%d, %s!\n",
  108. "enable_guc", i915_modparams.enable_guc,
  109. !HAS_HUC(dev_priv) ? "no HuC hardware" :
  110. "no HuC firmware");
  111. }
  112. /* A negative value means "use platform/config default" */
  113. if (i915_modparams.guc_log_level < 0)
  114. i915_modparams.guc_log_level =
  115. __get_default_guc_log_level(dev_priv);
  116. if (i915_modparams.guc_log_level > 0 && !intel_uc_is_using_guc()) {
  117. DRM_WARN("Incompatible option detected: %s=%d, %s!\n",
  118. "guc_log_level", i915_modparams.guc_log_level,
  119. !HAS_GUC(dev_priv) ? "no GuC hardware" :
  120. "GuC not enabled");
  121. i915_modparams.guc_log_level = 0;
  122. }
  123. if (i915_modparams.guc_log_level > 1 + GUC_LOG_VERBOSITY_MAX) {
  124. DRM_WARN("Incompatible option detected: %s=%d, %s!\n",
  125. "guc_log_level", i915_modparams.guc_log_level,
  126. "verbosity too high");
  127. i915_modparams.guc_log_level = 1 + GUC_LOG_VERBOSITY_MAX;
  128. }
  129. DRM_DEBUG_DRIVER("guc_log_level=%d (enabled:%s verbosity:%d)\n",
  130. i915_modparams.guc_log_level,
  131. yesno(i915_modparams.guc_log_level),
  132. i915_modparams.guc_log_level - 1);
  133. /* Make sure that sanitization was done */
  134. GEM_BUG_ON(i915_modparams.enable_guc < 0);
  135. GEM_BUG_ON(i915_modparams.guc_log_level < 0);
  136. }
  137. void intel_uc_init_early(struct drm_i915_private *dev_priv)
  138. {
  139. intel_guc_init_early(&dev_priv->guc);
  140. intel_huc_init_early(&dev_priv->huc);
  141. }
  142. void intel_uc_init_fw(struct drm_i915_private *dev_priv)
  143. {
  144. if (!USES_GUC(dev_priv))
  145. return;
  146. if (USES_HUC(dev_priv))
  147. intel_uc_fw_fetch(dev_priv, &dev_priv->huc.fw);
  148. intel_uc_fw_fetch(dev_priv, &dev_priv->guc.fw);
  149. }
  150. void intel_uc_fini_fw(struct drm_i915_private *dev_priv)
  151. {
  152. if (!USES_GUC(dev_priv))
  153. return;
  154. intel_uc_fw_fini(&dev_priv->guc.fw);
  155. if (USES_HUC(dev_priv))
  156. intel_uc_fw_fini(&dev_priv->huc.fw);
  157. guc_free_load_err_log(&dev_priv->guc);
  158. }
  159. /**
  160. * intel_uc_init_mmio - setup uC MMIO access
  161. *
  162. * @dev_priv: device private
  163. *
  164. * Setup minimal state necessary for MMIO accesses later in the
  165. * initialization sequence.
  166. */
  167. void intel_uc_init_mmio(struct drm_i915_private *dev_priv)
  168. {
  169. intel_guc_init_send_regs(&dev_priv->guc);
  170. }
  171. static void guc_capture_load_err_log(struct intel_guc *guc)
  172. {
  173. if (!guc->log.vma || !i915_modparams.guc_log_level)
  174. return;
  175. if (!guc->load_err_log)
  176. guc->load_err_log = i915_gem_object_get(guc->log.vma->obj);
  177. return;
  178. }
  179. static void guc_free_load_err_log(struct intel_guc *guc)
  180. {
  181. if (guc->load_err_log)
  182. i915_gem_object_put(guc->load_err_log);
  183. }
  184. static int guc_enable_communication(struct intel_guc *guc)
  185. {
  186. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  187. if (HAS_GUC_CT(dev_priv))
  188. return intel_guc_enable_ct(guc);
  189. guc->send = intel_guc_send_mmio;
  190. return 0;
  191. }
  192. static void guc_disable_communication(struct intel_guc *guc)
  193. {
  194. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  195. if (HAS_GUC_CT(dev_priv))
  196. intel_guc_disable_ct(guc);
  197. guc->send = intel_guc_send_nop;
  198. }
  199. int intel_uc_init_misc(struct drm_i915_private *dev_priv)
  200. {
  201. struct intel_guc *guc = &dev_priv->guc;
  202. int ret;
  203. if (!USES_GUC(dev_priv))
  204. return 0;
  205. ret = intel_guc_init_wq(guc);
  206. if (ret) {
  207. DRM_ERROR("Couldn't allocate workqueues for GuC\n");
  208. goto err;
  209. }
  210. ret = intel_guc_log_relay_create(guc);
  211. if (ret) {
  212. DRM_ERROR("Couldn't allocate relay for GuC log\n");
  213. goto err_relay;
  214. }
  215. return 0;
  216. err_relay:
  217. intel_guc_fini_wq(guc);
  218. err:
  219. return ret;
  220. }
  221. void intel_uc_fini_misc(struct drm_i915_private *dev_priv)
  222. {
  223. struct intel_guc *guc = &dev_priv->guc;
  224. if (!USES_GUC(dev_priv))
  225. return;
  226. intel_guc_fini_wq(guc);
  227. intel_guc_log_relay_destroy(guc);
  228. }
  229. int intel_uc_init(struct drm_i915_private *dev_priv)
  230. {
  231. struct intel_guc *guc = &dev_priv->guc;
  232. int ret;
  233. if (!USES_GUC(dev_priv))
  234. return 0;
  235. if (!HAS_GUC(dev_priv))
  236. return -ENODEV;
  237. ret = intel_guc_init(guc);
  238. if (ret)
  239. return ret;
  240. if (USES_GUC_SUBMISSION(dev_priv)) {
  241. /*
  242. * This is stuff we need to have available at fw load time
  243. * if we are planning to enable submission later
  244. */
  245. ret = intel_guc_submission_init(guc);
  246. if (ret) {
  247. intel_guc_fini(guc);
  248. return ret;
  249. }
  250. }
  251. return 0;
  252. }
  253. void intel_uc_fini(struct drm_i915_private *dev_priv)
  254. {
  255. struct intel_guc *guc = &dev_priv->guc;
  256. if (!USES_GUC(dev_priv))
  257. return;
  258. GEM_BUG_ON(!HAS_GUC(dev_priv));
  259. if (USES_GUC_SUBMISSION(dev_priv))
  260. intel_guc_submission_fini(guc);
  261. intel_guc_fini(guc);
  262. }
  263. int intel_uc_init_hw(struct drm_i915_private *dev_priv)
  264. {
  265. struct intel_guc *guc = &dev_priv->guc;
  266. struct intel_huc *huc = &dev_priv->huc;
  267. int ret, attempts;
  268. if (!USES_GUC(dev_priv))
  269. return 0;
  270. GEM_BUG_ON(!HAS_GUC(dev_priv));
  271. guc_disable_communication(guc);
  272. gen9_reset_guc_interrupts(dev_priv);
  273. /* init WOPCM */
  274. I915_WRITE(GUC_WOPCM_SIZE, intel_guc_wopcm_size(dev_priv));
  275. I915_WRITE(DMA_GUC_WOPCM_OFFSET,
  276. GUC_WOPCM_OFFSET_VALUE | HUC_LOADING_AGENT_GUC);
  277. /* WaEnableuKernelHeaderValidFix:skl */
  278. /* WaEnableGuCBootHashCheckNotSet:skl,bxt,kbl */
  279. if (IS_GEN9(dev_priv))
  280. attempts = 3;
  281. else
  282. attempts = 1;
  283. while (attempts--) {
  284. /*
  285. * Always reset the GuC just before (re)loading, so
  286. * that the state and timing are fairly predictable
  287. */
  288. ret = __intel_uc_reset_hw(dev_priv);
  289. if (ret)
  290. goto err_out;
  291. if (USES_HUC(dev_priv)) {
  292. ret = intel_huc_fw_upload(huc);
  293. if (ret)
  294. goto err_out;
  295. }
  296. intel_guc_init_params(guc);
  297. ret = intel_guc_fw_upload(guc);
  298. if (ret == 0 || ret != -EAGAIN)
  299. break;
  300. DRM_DEBUG_DRIVER("GuC fw load failed: %d; will reset and "
  301. "retry %d more time(s)\n", ret, attempts);
  302. }
  303. /* Did we succeded or run out of retries? */
  304. if (ret)
  305. goto err_log_capture;
  306. ret = guc_enable_communication(guc);
  307. if (ret)
  308. goto err_log_capture;
  309. if (USES_HUC(dev_priv)) {
  310. ret = intel_huc_auth(huc);
  311. if (ret)
  312. goto err_communication;
  313. }
  314. if (USES_GUC_SUBMISSION(dev_priv)) {
  315. if (i915_modparams.guc_log_level)
  316. gen9_enable_guc_interrupts(dev_priv);
  317. ret = intel_guc_submission_enable(guc);
  318. if (ret)
  319. goto err_interrupts;
  320. }
  321. dev_info(dev_priv->drm.dev, "GuC firmware version %u.%u\n",
  322. guc->fw.major_ver_found, guc->fw.minor_ver_found);
  323. dev_info(dev_priv->drm.dev, "GuC submission %s\n",
  324. enableddisabled(USES_GUC_SUBMISSION(dev_priv)));
  325. dev_info(dev_priv->drm.dev, "HuC %s\n",
  326. enableddisabled(USES_HUC(dev_priv)));
  327. return 0;
  328. /*
  329. * We've failed to load the firmware :(
  330. */
  331. err_interrupts:
  332. gen9_disable_guc_interrupts(dev_priv);
  333. err_communication:
  334. guc_disable_communication(guc);
  335. err_log_capture:
  336. guc_capture_load_err_log(guc);
  337. err_out:
  338. /*
  339. * Note that there is no fallback as either user explicitly asked for
  340. * the GuC or driver default option was to run with the GuC enabled.
  341. */
  342. if (GEM_WARN_ON(ret == -EIO))
  343. ret = -EINVAL;
  344. dev_err(dev_priv->drm.dev, "GuC initialization failed %d\n", ret);
  345. return ret;
  346. }
  347. void intel_uc_fini_hw(struct drm_i915_private *dev_priv)
  348. {
  349. struct intel_guc *guc = &dev_priv->guc;
  350. if (!USES_GUC(dev_priv))
  351. return;
  352. GEM_BUG_ON(!HAS_GUC(dev_priv));
  353. if (USES_GUC_SUBMISSION(dev_priv))
  354. intel_guc_submission_disable(guc);
  355. guc_disable_communication(guc);
  356. if (USES_GUC_SUBMISSION(dev_priv))
  357. gen9_disable_guc_interrupts(dev_priv);
  358. }
  359. int intel_uc_suspend(struct drm_i915_private *i915)
  360. {
  361. struct intel_guc *guc = &i915->guc;
  362. int err;
  363. if (!USES_GUC(i915))
  364. return 0;
  365. if (guc->fw.load_status != INTEL_UC_FIRMWARE_SUCCESS)
  366. return 0;
  367. err = intel_guc_suspend(guc);
  368. if (err) {
  369. DRM_DEBUG_DRIVER("Failed to suspend GuC, err=%d", err);
  370. return err;
  371. }
  372. gen9_disable_guc_interrupts(i915);
  373. return 0;
  374. }
  375. int intel_uc_resume(struct drm_i915_private *i915)
  376. {
  377. struct intel_guc *guc = &i915->guc;
  378. int err;
  379. if (!USES_GUC(i915))
  380. return 0;
  381. if (guc->fw.load_status != INTEL_UC_FIRMWARE_SUCCESS)
  382. return 0;
  383. if (i915_modparams.guc_log_level)
  384. gen9_enable_guc_interrupts(i915);
  385. err = intel_guc_resume(guc);
  386. if (err) {
  387. DRM_DEBUG_DRIVER("Failed to resume GuC, err=%d", err);
  388. return err;
  389. }
  390. return 0;
  391. }