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 *i915)
  47. {
  48. struct intel_uc_fw *guc_fw = &i915->guc.fw;
  49. struct intel_uc_fw *huc_fw = &i915->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 *i915)
  60. {
  61. int guc_log_level;
  62. if (!HAS_GUC(i915) || !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. * @i915: 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 *i915)
  90. {
  91. struct intel_uc_fw *guc_fw = &i915->guc.fw;
  92. struct intel_uc_fw *huc_fw = &i915->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(i915);
  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(i915) ? "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(i915) ? "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(i915);
  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(i915) ? "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. }
  148. void intel_uc_cleanup_early(struct drm_i915_private *i915)
  149. {
  150. struct intel_guc *guc = &i915->guc;
  151. guc_free_load_err_log(guc);
  152. }
  153. /**
  154. * intel_uc_init_mmio - setup uC MMIO access
  155. * @i915: device private
  156. *
  157. * Setup minimal state necessary for MMIO accesses later in the
  158. * initialization sequence.
  159. */
  160. void intel_uc_init_mmio(struct drm_i915_private *i915)
  161. {
  162. intel_guc_init_send_regs(&i915->guc);
  163. }
  164. static void guc_capture_load_err_log(struct intel_guc *guc)
  165. {
  166. if (!guc->log.vma || !intel_guc_log_get_level(&guc->log))
  167. return;
  168. if (!guc->load_err_log)
  169. guc->load_err_log = i915_gem_object_get(guc->log.vma->obj);
  170. return;
  171. }
  172. static void guc_free_load_err_log(struct intel_guc *guc)
  173. {
  174. if (guc->load_err_log)
  175. i915_gem_object_put(guc->load_err_log);
  176. }
  177. static int guc_enable_communication(struct intel_guc *guc)
  178. {
  179. struct drm_i915_private *i915 = guc_to_i915(guc);
  180. gen9_enable_guc_interrupts(i915);
  181. if (HAS_GUC_CT(i915))
  182. return intel_guc_ct_enable(&guc->ct);
  183. guc->send = intel_guc_send_mmio;
  184. guc->handler = intel_guc_to_host_event_handler_mmio;
  185. return 0;
  186. }
  187. static void guc_disable_communication(struct intel_guc *guc)
  188. {
  189. struct drm_i915_private *i915 = guc_to_i915(guc);
  190. if (HAS_GUC_CT(i915))
  191. intel_guc_ct_disable(&guc->ct);
  192. gen9_disable_guc_interrupts(i915);
  193. guc->send = intel_guc_send_nop;
  194. guc->handler = intel_guc_to_host_event_handler_nop;
  195. }
  196. int intel_uc_init_misc(struct drm_i915_private *i915)
  197. {
  198. struct intel_guc *guc = &i915->guc;
  199. struct intel_huc *huc = &i915->huc;
  200. int ret;
  201. if (!USES_GUC(i915))
  202. return 0;
  203. ret = intel_guc_init_misc(guc);
  204. if (ret)
  205. return ret;
  206. if (USES_HUC(i915)) {
  207. ret = intel_huc_init_misc(huc);
  208. if (ret)
  209. goto err_guc;
  210. }
  211. return 0;
  212. err_guc:
  213. intel_guc_fini_misc(guc);
  214. return ret;
  215. }
  216. void intel_uc_fini_misc(struct drm_i915_private *i915)
  217. {
  218. struct intel_guc *guc = &i915->guc;
  219. struct intel_huc *huc = &i915->huc;
  220. if (!USES_GUC(i915))
  221. return;
  222. if (USES_HUC(i915))
  223. intel_huc_fini_misc(huc);
  224. intel_guc_fini_misc(guc);
  225. }
  226. int intel_uc_init(struct drm_i915_private *i915)
  227. {
  228. struct intel_guc *guc = &i915->guc;
  229. int ret;
  230. if (!USES_GUC(i915))
  231. return 0;
  232. if (!HAS_GUC(i915))
  233. return -ENODEV;
  234. ret = intel_guc_init(guc);
  235. if (ret)
  236. return ret;
  237. if (USES_GUC_SUBMISSION(i915)) {
  238. /*
  239. * This is stuff we need to have available at fw load time
  240. * if we are planning to enable submission later
  241. */
  242. ret = intel_guc_submission_init(guc);
  243. if (ret) {
  244. intel_guc_fini(guc);
  245. return ret;
  246. }
  247. }
  248. return 0;
  249. }
  250. void intel_uc_fini(struct drm_i915_private *i915)
  251. {
  252. struct intel_guc *guc = &i915->guc;
  253. if (!USES_GUC(i915))
  254. return;
  255. GEM_BUG_ON(!HAS_GUC(i915));
  256. if (USES_GUC_SUBMISSION(i915))
  257. intel_guc_submission_fini(guc);
  258. intel_guc_fini(guc);
  259. }
  260. void intel_uc_sanitize(struct drm_i915_private *i915)
  261. {
  262. struct intel_guc *guc = &i915->guc;
  263. struct intel_huc *huc = &i915->huc;
  264. if (!USES_GUC(i915))
  265. return;
  266. GEM_BUG_ON(!HAS_GUC(i915));
  267. guc_disable_communication(guc);
  268. intel_huc_sanitize(huc);
  269. intel_guc_sanitize(guc);
  270. __intel_uc_reset_hw(i915);
  271. }
  272. int intel_uc_init_hw(struct drm_i915_private *i915)
  273. {
  274. struct intel_guc *guc = &i915->guc;
  275. struct intel_huc *huc = &i915->huc;
  276. int ret, attempts;
  277. if (!USES_GUC(i915))
  278. return 0;
  279. GEM_BUG_ON(!HAS_GUC(i915));
  280. gen9_reset_guc_interrupts(i915);
  281. /* WaEnableuKernelHeaderValidFix:skl */
  282. /* WaEnableGuCBootHashCheckNotSet:skl,bxt,kbl */
  283. if (IS_GEN9(i915))
  284. attempts = 3;
  285. else
  286. attempts = 1;
  287. while (attempts--) {
  288. /*
  289. * Always reset the GuC just before (re)loading, so
  290. * that the state and timing are fairly predictable
  291. */
  292. ret = __intel_uc_reset_hw(i915);
  293. if (ret)
  294. goto err_out;
  295. if (USES_HUC(i915)) {
  296. ret = intel_huc_fw_upload(huc);
  297. if (ret)
  298. goto err_out;
  299. }
  300. intel_guc_init_params(guc);
  301. ret = intel_guc_fw_upload(guc);
  302. if (ret == 0 || ret != -EAGAIN)
  303. break;
  304. DRM_DEBUG_DRIVER("GuC fw load failed: %d; will reset and "
  305. "retry %d more time(s)\n", ret, attempts);
  306. }
  307. /* Did we succeded or run out of retries? */
  308. if (ret)
  309. goto err_log_capture;
  310. ret = guc_enable_communication(guc);
  311. if (ret)
  312. goto err_log_capture;
  313. if (USES_HUC(i915)) {
  314. ret = intel_huc_auth(huc);
  315. if (ret)
  316. goto err_communication;
  317. }
  318. if (USES_GUC_SUBMISSION(i915)) {
  319. ret = intel_guc_submission_enable(guc);
  320. if (ret)
  321. goto err_communication;
  322. }
  323. dev_info(i915->drm.dev, "GuC firmware version %u.%u\n",
  324. guc->fw.major_ver_found, guc->fw.minor_ver_found);
  325. dev_info(i915->drm.dev, "GuC submission %s\n",
  326. enableddisabled(USES_GUC_SUBMISSION(i915)));
  327. dev_info(i915->drm.dev, "HuC %s\n",
  328. enableddisabled(USES_HUC(i915)));
  329. return 0;
  330. /*
  331. * We've failed to load the firmware :(
  332. */
  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(i915->drm.dev, "GuC initialization failed %d\n", ret);
  345. return ret;
  346. }
  347. void intel_uc_fini_hw(struct drm_i915_private *i915)
  348. {
  349. struct intel_guc *guc = &i915->guc;
  350. if (!USES_GUC(i915))
  351. return;
  352. GEM_BUG_ON(!HAS_GUC(i915));
  353. if (USES_GUC_SUBMISSION(i915))
  354. intel_guc_submission_disable(guc);
  355. guc_disable_communication(guc);
  356. }
  357. int intel_uc_suspend(struct drm_i915_private *i915)
  358. {
  359. struct intel_guc *guc = &i915->guc;
  360. int err;
  361. if (!USES_GUC(i915))
  362. return 0;
  363. if (guc->fw.load_status != INTEL_UC_FIRMWARE_SUCCESS)
  364. return 0;
  365. err = intel_guc_suspend(guc);
  366. if (err) {
  367. DRM_DEBUG_DRIVER("Failed to suspend GuC, err=%d", err);
  368. return err;
  369. }
  370. gen9_disable_guc_interrupts(i915);
  371. return 0;
  372. }
  373. int intel_uc_resume(struct drm_i915_private *i915)
  374. {
  375. struct intel_guc *guc = &i915->guc;
  376. int err;
  377. if (!USES_GUC(i915))
  378. return 0;
  379. if (guc->fw.load_status != INTEL_UC_FIRMWARE_SUCCESS)
  380. return 0;
  381. gen9_enable_guc_interrupts(i915);
  382. err = intel_guc_resume(guc);
  383. if (err) {
  384. DRM_DEBUG_DRIVER("Failed to resume GuC, err=%d", err);
  385. return err;
  386. }
  387. return 0;
  388. }