intel_guc.c 14 KB

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