intel_guc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /*
  2. * Copyright © 2014-2017 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_guc.h"
  25. #include "intel_guc_submission.h"
  26. #include "i915_drv.h"
  27. static void gen8_guc_raise_irq(struct intel_guc *guc)
  28. {
  29. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  30. I915_WRITE(GUC_SEND_INTERRUPT, GUC_SEND_TRIGGER);
  31. }
  32. static inline i915_reg_t guc_send_reg(struct intel_guc *guc, u32 i)
  33. {
  34. GEM_BUG_ON(!guc->send_regs.base);
  35. GEM_BUG_ON(!guc->send_regs.count);
  36. GEM_BUG_ON(i >= guc->send_regs.count);
  37. return _MMIO(guc->send_regs.base + 4 * i);
  38. }
  39. void intel_guc_init_send_regs(struct intel_guc *guc)
  40. {
  41. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  42. enum forcewake_domains fw_domains = 0;
  43. unsigned int i;
  44. guc->send_regs.base = i915_mmio_reg_offset(SOFT_SCRATCH(0));
  45. guc->send_regs.count = SOFT_SCRATCH_COUNT - 1;
  46. for (i = 0; i < guc->send_regs.count; i++) {
  47. fw_domains |= intel_uncore_forcewake_for_reg(dev_priv,
  48. guc_send_reg(guc, i),
  49. FW_REG_READ | FW_REG_WRITE);
  50. }
  51. guc->send_regs.fw_domains = fw_domains;
  52. }
  53. void intel_guc_init_early(struct intel_guc *guc)
  54. {
  55. intel_guc_fw_init_early(guc);
  56. intel_guc_ct_init_early(&guc->ct);
  57. mutex_init(&guc->send_mutex);
  58. guc->send = intel_guc_send_nop;
  59. guc->notify = gen8_guc_raise_irq;
  60. }
  61. int intel_guc_init_wq(struct intel_guc *guc)
  62. {
  63. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  64. /*
  65. * GuC log buffer flush work item has to do register access to
  66. * send the ack to GuC and this work item, if not synced before
  67. * suspend, can potentially get executed after the GFX device is
  68. * suspended.
  69. * By marking the WQ as freezable, we don't have to bother about
  70. * flushing of this work item from the suspend hooks, the pending
  71. * work item if any will be either executed before the suspend
  72. * or scheduled later on resume. This way the handling of work
  73. * item can be kept same between system suspend & rpm suspend.
  74. */
  75. guc->log.runtime.flush_wq = alloc_ordered_workqueue("i915-guc_log",
  76. WQ_HIGHPRI | WQ_FREEZABLE);
  77. if (!guc->log.runtime.flush_wq)
  78. return -ENOMEM;
  79. /*
  80. * Even though both sending GuC action, and adding a new workitem to
  81. * GuC workqueue are serialized (each with its own locking), since
  82. * we're using mutliple engines, it's possible that we're going to
  83. * issue a preempt request with two (or more - each for different
  84. * engine) workitems in GuC queue. In this situation, GuC may submit
  85. * all of them, which will make us very confused.
  86. * Our preemption contexts may even already be complete - before we
  87. * even had the chance to sent the preempt action to GuC!. Rather
  88. * than introducing yet another lock, we can just use ordered workqueue
  89. * to make sure we're always sending a single preemption request with a
  90. * single workitem.
  91. */
  92. if (HAS_LOGICAL_RING_PREEMPTION(dev_priv) &&
  93. USES_GUC_SUBMISSION(dev_priv)) {
  94. guc->preempt_wq = alloc_ordered_workqueue("i915-guc_preempt",
  95. WQ_HIGHPRI);
  96. if (!guc->preempt_wq) {
  97. destroy_workqueue(guc->log.runtime.flush_wq);
  98. return -ENOMEM;
  99. }
  100. }
  101. return 0;
  102. }
  103. void intel_guc_fini_wq(struct intel_guc *guc)
  104. {
  105. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  106. if (HAS_LOGICAL_RING_PREEMPTION(dev_priv) &&
  107. USES_GUC_SUBMISSION(dev_priv))
  108. destroy_workqueue(guc->preempt_wq);
  109. destroy_workqueue(guc->log.runtime.flush_wq);
  110. }
  111. static int guc_shared_data_create(struct intel_guc *guc)
  112. {
  113. struct i915_vma *vma;
  114. void *vaddr;
  115. vma = intel_guc_allocate_vma(guc, PAGE_SIZE);
  116. if (IS_ERR(vma))
  117. return PTR_ERR(vma);
  118. vaddr = i915_gem_object_pin_map(vma->obj, I915_MAP_WB);
  119. if (IS_ERR(vaddr)) {
  120. i915_vma_unpin_and_release(&vma);
  121. return PTR_ERR(vaddr);
  122. }
  123. guc->shared_data = vma;
  124. guc->shared_data_vaddr = vaddr;
  125. return 0;
  126. }
  127. static void guc_shared_data_destroy(struct intel_guc *guc)
  128. {
  129. i915_gem_object_unpin_map(guc->shared_data->obj);
  130. i915_vma_unpin_and_release(&guc->shared_data);
  131. }
  132. int intel_guc_init(struct intel_guc *guc)
  133. {
  134. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  135. int ret;
  136. ret = guc_shared_data_create(guc);
  137. if (ret)
  138. return ret;
  139. GEM_BUG_ON(!guc->shared_data);
  140. /* We need to notify the guc whenever we change the GGTT */
  141. i915_ggtt_enable_guc(dev_priv);
  142. return 0;
  143. }
  144. void intel_guc_fini(struct intel_guc *guc)
  145. {
  146. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  147. i915_ggtt_disable_guc(dev_priv);
  148. guc_shared_data_destroy(guc);
  149. }
  150. static u32 get_gt_type(struct drm_i915_private *dev_priv)
  151. {
  152. /* XXX: GT type based on PCI device ID? field seems unused by fw */
  153. return 0;
  154. }
  155. static u32 get_core_family(struct drm_i915_private *dev_priv)
  156. {
  157. u32 gen = INTEL_GEN(dev_priv);
  158. switch (gen) {
  159. case 9:
  160. return GUC_CORE_FAMILY_GEN9;
  161. default:
  162. MISSING_CASE(gen);
  163. return GUC_CORE_FAMILY_UNKNOWN;
  164. }
  165. }
  166. /*
  167. * Initialise the GuC parameter block before starting the firmware
  168. * transfer. These parameters are read by the firmware on startup
  169. * and cannot be changed thereafter.
  170. */
  171. void intel_guc_init_params(struct intel_guc *guc)
  172. {
  173. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  174. u32 params[GUC_CTL_MAX_DWORDS];
  175. int i;
  176. memset(params, 0, sizeof(params));
  177. params[GUC_CTL_DEVICE_INFO] |=
  178. (get_gt_type(dev_priv) << GUC_CTL_GT_TYPE_SHIFT) |
  179. (get_core_family(dev_priv) << GUC_CTL_CORE_FAMILY_SHIFT);
  180. /*
  181. * GuC ARAT increment is 10 ns. GuC default scheduler quantum is one
  182. * second. This ARAR is calculated by:
  183. * Scheduler-Quantum-in-ns / ARAT-increment-in-ns = 1000000000 / 10
  184. */
  185. params[GUC_CTL_ARAT_HIGH] = 0;
  186. params[GUC_CTL_ARAT_LOW] = 100000000;
  187. params[GUC_CTL_WA] |= GUC_CTL_WA_UK_BY_DRIVER;
  188. params[GUC_CTL_FEATURE] |= GUC_CTL_DISABLE_SCHEDULER |
  189. GUC_CTL_VCS2_ENABLED;
  190. params[GUC_CTL_LOG_PARAMS] = guc->log.flags;
  191. if (i915_modparams.guc_log_level >= 0) {
  192. params[GUC_CTL_DEBUG] =
  193. i915_modparams.guc_log_level << GUC_LOG_VERBOSITY_SHIFT;
  194. } else {
  195. params[GUC_CTL_DEBUG] = GUC_LOG_DISABLED;
  196. }
  197. /* If GuC submission is enabled, set up additional parameters here */
  198. if (USES_GUC_SUBMISSION(dev_priv)) {
  199. u32 ads = guc_ggtt_offset(guc->ads_vma) >> PAGE_SHIFT;
  200. u32 pgs = guc_ggtt_offset(dev_priv->guc.stage_desc_pool);
  201. u32 ctx_in_16 = GUC_MAX_STAGE_DESCRIPTORS / 16;
  202. params[GUC_CTL_DEBUG] |= ads << GUC_ADS_ADDR_SHIFT;
  203. params[GUC_CTL_DEBUG] |= GUC_ADS_ENABLED;
  204. pgs >>= PAGE_SHIFT;
  205. params[GUC_CTL_CTXINFO] = (pgs << GUC_CTL_BASE_ADDR_SHIFT) |
  206. (ctx_in_16 << GUC_CTL_CTXNUM_IN16_SHIFT);
  207. params[GUC_CTL_FEATURE] |= GUC_CTL_KERNEL_SUBMISSIONS;
  208. /* Unmask this bit to enable the GuC's internal scheduler */
  209. params[GUC_CTL_FEATURE] &= ~GUC_CTL_DISABLE_SCHEDULER;
  210. }
  211. /*
  212. * All SOFT_SCRATCH registers are in FORCEWAKE_BLITTER domain and
  213. * they are power context saved so it's ok to release forcewake
  214. * when we are done here and take it again at xfer time.
  215. */
  216. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_BLITTER);
  217. I915_WRITE(SOFT_SCRATCH(0), 0);
  218. for (i = 0; i < GUC_CTL_MAX_DWORDS; i++)
  219. I915_WRITE(SOFT_SCRATCH(1 + i), params[i]);
  220. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_BLITTER);
  221. }
  222. int intel_guc_send_nop(struct intel_guc *guc, const u32 *action, u32 len)
  223. {
  224. WARN(1, "Unexpected send: action=%#x\n", *action);
  225. return -ENODEV;
  226. }
  227. /*
  228. * This function implements the MMIO based host to GuC interface.
  229. */
  230. int intel_guc_send_mmio(struct intel_guc *guc, const u32 *action, u32 len)
  231. {
  232. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  233. u32 status;
  234. int i;
  235. int ret;
  236. GEM_BUG_ON(!len);
  237. GEM_BUG_ON(len > guc->send_regs.count);
  238. /* If CT is available, we expect to use MMIO only during init/fini */
  239. GEM_BUG_ON(HAS_GUC_CT(dev_priv) &&
  240. *action != INTEL_GUC_ACTION_REGISTER_COMMAND_TRANSPORT_BUFFER &&
  241. *action != INTEL_GUC_ACTION_DEREGISTER_COMMAND_TRANSPORT_BUFFER);
  242. mutex_lock(&guc->send_mutex);
  243. intel_uncore_forcewake_get(dev_priv, guc->send_regs.fw_domains);
  244. for (i = 0; i < len; i++)
  245. I915_WRITE(guc_send_reg(guc, i), action[i]);
  246. POSTING_READ(guc_send_reg(guc, i - 1));
  247. intel_guc_notify(guc);
  248. /*
  249. * No GuC command should ever take longer than 10ms.
  250. * Fast commands should still complete in 10us.
  251. */
  252. ret = __intel_wait_for_register_fw(dev_priv,
  253. guc_send_reg(guc, 0),
  254. INTEL_GUC_RECV_MASK,
  255. INTEL_GUC_RECV_MASK,
  256. 10, 10, &status);
  257. if (status != INTEL_GUC_STATUS_SUCCESS) {
  258. /*
  259. * Either the GuC explicitly returned an error (which
  260. * we convert to -EIO here) or no response at all was
  261. * received within the timeout limit (-ETIMEDOUT)
  262. */
  263. if (ret != -ETIMEDOUT)
  264. ret = -EIO;
  265. DRM_WARN("INTEL_GUC_SEND: Action 0x%X failed;"
  266. " ret=%d status=0x%08X response=0x%08X\n",
  267. action[0], ret, status, I915_READ(SOFT_SCRATCH(15)));
  268. }
  269. intel_uncore_forcewake_put(dev_priv, guc->send_regs.fw_domains);
  270. mutex_unlock(&guc->send_mutex);
  271. return ret;
  272. }
  273. int intel_guc_sample_forcewake(struct intel_guc *guc)
  274. {
  275. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  276. u32 action[2];
  277. action[0] = INTEL_GUC_ACTION_SAMPLE_FORCEWAKE;
  278. /* WaRsDisableCoarsePowerGating:skl,bxt */
  279. if (!HAS_RC6(dev_priv) || NEEDS_WaRsDisableCoarsePowerGating(dev_priv))
  280. action[1] = 0;
  281. else
  282. /* bit 0 and 1 are for Render and Media domain separately */
  283. action[1] = GUC_FORCEWAKE_RENDER | GUC_FORCEWAKE_MEDIA;
  284. return intel_guc_send(guc, action, ARRAY_SIZE(action));
  285. }
  286. /**
  287. * intel_guc_auth_huc() - Send action to GuC to authenticate HuC ucode
  288. * @guc: intel_guc structure
  289. * @rsa_offset: rsa offset w.r.t ggtt base of huc vma
  290. *
  291. * Triggers a HuC firmware authentication request to the GuC via intel_guc_send
  292. * INTEL_GUC_ACTION_AUTHENTICATE_HUC interface. This function is invoked by
  293. * intel_huc_auth().
  294. *
  295. * Return: non-zero code on error
  296. */
  297. int intel_guc_auth_huc(struct intel_guc *guc, u32 rsa_offset)
  298. {
  299. u32 action[] = {
  300. INTEL_GUC_ACTION_AUTHENTICATE_HUC,
  301. rsa_offset
  302. };
  303. return intel_guc_send(guc, action, ARRAY_SIZE(action));
  304. }
  305. /**
  306. * intel_guc_suspend() - notify GuC entering suspend state
  307. * @dev_priv: i915 device private
  308. */
  309. int intel_guc_suspend(struct drm_i915_private *dev_priv)
  310. {
  311. struct intel_guc *guc = &dev_priv->guc;
  312. u32 data[3];
  313. if (guc->fw.load_status != INTEL_UC_FIRMWARE_SUCCESS)
  314. return 0;
  315. gen9_disable_guc_interrupts(dev_priv);
  316. data[0] = INTEL_GUC_ACTION_ENTER_S_STATE;
  317. /* any value greater than GUC_POWER_D0 */
  318. data[1] = GUC_POWER_D1;
  319. data[2] = guc_ggtt_offset(guc->shared_data);
  320. return intel_guc_send(guc, data, ARRAY_SIZE(data));
  321. }
  322. /**
  323. * intel_guc_reset_engine() - ask GuC to reset an engine
  324. * @guc: intel_guc structure
  325. * @engine: engine to be reset
  326. */
  327. int intel_guc_reset_engine(struct intel_guc *guc,
  328. struct intel_engine_cs *engine)
  329. {
  330. u32 data[7];
  331. GEM_BUG_ON(!guc->execbuf_client);
  332. data[0] = INTEL_GUC_ACTION_REQUEST_ENGINE_RESET;
  333. data[1] = engine->guc_id;
  334. data[2] = 0;
  335. data[3] = 0;
  336. data[4] = 0;
  337. data[5] = guc->execbuf_client->stage_id;
  338. data[6] = guc_ggtt_offset(guc->shared_data);
  339. return intel_guc_send(guc, data, ARRAY_SIZE(data));
  340. }
  341. /**
  342. * intel_guc_resume() - notify GuC resuming from suspend state
  343. * @dev_priv: i915 device private
  344. */
  345. int intel_guc_resume(struct drm_i915_private *dev_priv)
  346. {
  347. struct intel_guc *guc = &dev_priv->guc;
  348. u32 data[3];
  349. if (guc->fw.load_status != INTEL_UC_FIRMWARE_SUCCESS)
  350. return 0;
  351. if (i915_modparams.guc_log_level >= 0)
  352. gen9_enable_guc_interrupts(dev_priv);
  353. data[0] = INTEL_GUC_ACTION_EXIT_S_STATE;
  354. data[1] = GUC_POWER_D0;
  355. data[2] = guc_ggtt_offset(guc->shared_data);
  356. return intel_guc_send(guc, data, ARRAY_SIZE(data));
  357. }
  358. /**
  359. * intel_guc_allocate_vma() - Allocate a GGTT VMA for GuC usage
  360. * @guc: the guc
  361. * @size: size of area to allocate (both virtual space and memory)
  362. *
  363. * This is a wrapper to create an object for use with the GuC. In order to
  364. * use it inside the GuC, an object needs to be pinned lifetime, so we allocate
  365. * both some backing storage and a range inside the Global GTT. We must pin
  366. * it in the GGTT somewhere other than than [0, GUC_WOPCM_TOP) because that
  367. * range is reserved inside GuC.
  368. *
  369. * Return: A i915_vma if successful, otherwise an ERR_PTR.
  370. */
  371. struct i915_vma *intel_guc_allocate_vma(struct intel_guc *guc, u32 size)
  372. {
  373. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  374. struct drm_i915_gem_object *obj;
  375. struct i915_vma *vma;
  376. int ret;
  377. obj = i915_gem_object_create(dev_priv, size);
  378. if (IS_ERR(obj))
  379. return ERR_CAST(obj);
  380. vma = i915_vma_instance(obj, &dev_priv->ggtt.base, NULL);
  381. if (IS_ERR(vma))
  382. goto err;
  383. ret = i915_vma_pin(vma, 0, PAGE_SIZE,
  384. PIN_GLOBAL | PIN_OFFSET_BIAS | GUC_WOPCM_TOP);
  385. if (ret) {
  386. vma = ERR_PTR(ret);
  387. goto err;
  388. }
  389. return vma;
  390. err:
  391. i915_gem_object_put(obj);
  392. return vma;
  393. }
  394. u32 intel_guc_wopcm_size(struct drm_i915_private *dev_priv)
  395. {
  396. u32 wopcm_size = GUC_WOPCM_TOP;
  397. /* On BXT, the top of WOPCM is reserved for RC6 context */
  398. if (IS_GEN9_LP(dev_priv))
  399. wopcm_size -= BXT_GUC_WOPCM_RC6_RESERVED;
  400. return wopcm_size;
  401. }