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