intel_engine_cs.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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 "i915_drv.h"
  25. #include "intel_ringbuffer.h"
  26. #include "intel_lrc.h"
  27. static const struct engine_info {
  28. const char *name;
  29. unsigned exec_id;
  30. unsigned guc_id;
  31. u32 mmio_base;
  32. unsigned irq_shift;
  33. int (*init_legacy)(struct intel_engine_cs *engine);
  34. int (*init_execlists)(struct intel_engine_cs *engine);
  35. } intel_engines[] = {
  36. [RCS] = {
  37. .name = "render ring",
  38. .exec_id = I915_EXEC_RENDER,
  39. .guc_id = GUC_RENDER_ENGINE,
  40. .mmio_base = RENDER_RING_BASE,
  41. .irq_shift = GEN8_RCS_IRQ_SHIFT,
  42. .init_execlists = logical_render_ring_init,
  43. .init_legacy = intel_init_render_ring_buffer,
  44. },
  45. [BCS] = {
  46. .name = "blitter ring",
  47. .exec_id = I915_EXEC_BLT,
  48. .guc_id = GUC_BLITTER_ENGINE,
  49. .mmio_base = BLT_RING_BASE,
  50. .irq_shift = GEN8_BCS_IRQ_SHIFT,
  51. .init_execlists = logical_xcs_ring_init,
  52. .init_legacy = intel_init_blt_ring_buffer,
  53. },
  54. [VCS] = {
  55. .name = "bsd ring",
  56. .exec_id = I915_EXEC_BSD,
  57. .guc_id = GUC_VIDEO_ENGINE,
  58. .mmio_base = GEN6_BSD_RING_BASE,
  59. .irq_shift = GEN8_VCS1_IRQ_SHIFT,
  60. .init_execlists = logical_xcs_ring_init,
  61. .init_legacy = intel_init_bsd_ring_buffer,
  62. },
  63. [VCS2] = {
  64. .name = "bsd2 ring",
  65. .exec_id = I915_EXEC_BSD,
  66. .guc_id = GUC_VIDEO_ENGINE2,
  67. .mmio_base = GEN8_BSD2_RING_BASE,
  68. .irq_shift = GEN8_VCS2_IRQ_SHIFT,
  69. .init_execlists = logical_xcs_ring_init,
  70. .init_legacy = intel_init_bsd2_ring_buffer,
  71. },
  72. [VECS] = {
  73. .name = "video enhancement ring",
  74. .exec_id = I915_EXEC_VEBOX,
  75. .guc_id = GUC_VIDEOENHANCE_ENGINE,
  76. .mmio_base = VEBOX_RING_BASE,
  77. .irq_shift = GEN8_VECS_IRQ_SHIFT,
  78. .init_execlists = logical_xcs_ring_init,
  79. .init_legacy = intel_init_vebox_ring_buffer,
  80. },
  81. };
  82. static struct intel_engine_cs *
  83. intel_engine_setup(struct drm_i915_private *dev_priv,
  84. enum intel_engine_id id)
  85. {
  86. const struct engine_info *info = &intel_engines[id];
  87. struct intel_engine_cs *engine = &dev_priv->engine[id];
  88. engine->id = id;
  89. engine->i915 = dev_priv;
  90. engine->name = info->name;
  91. engine->exec_id = info->exec_id;
  92. engine->hw_id = engine->guc_id = info->guc_id;
  93. engine->mmio_base = info->mmio_base;
  94. engine->irq_shift = info->irq_shift;
  95. return engine;
  96. }
  97. /**
  98. * intel_engines_init() - allocate, populate and init the Engine Command Streamers
  99. * @dev: DRM device.
  100. *
  101. * Return: non-zero if the initialization failed.
  102. */
  103. int intel_engines_init(struct drm_device *dev)
  104. {
  105. struct drm_i915_private *dev_priv = to_i915(dev);
  106. struct intel_device_info *device_info = mkwrite_device_info(dev_priv);
  107. unsigned int mask = 0;
  108. int (*init)(struct intel_engine_cs *engine);
  109. unsigned int i;
  110. int ret;
  111. WARN_ON(INTEL_INFO(dev_priv)->ring_mask == 0);
  112. WARN_ON(INTEL_INFO(dev_priv)->ring_mask &
  113. GENMASK(sizeof(mask) * BITS_PER_BYTE - 1, I915_NUM_ENGINES));
  114. for (i = 0; i < ARRAY_SIZE(intel_engines); i++) {
  115. if (!HAS_ENGINE(dev_priv, i))
  116. continue;
  117. if (i915.enable_execlists)
  118. init = intel_engines[i].init_execlists;
  119. else
  120. init = intel_engines[i].init_legacy;
  121. if (!init)
  122. continue;
  123. ret = init(intel_engine_setup(dev_priv, i));
  124. if (ret)
  125. goto cleanup;
  126. mask |= ENGINE_MASK(i);
  127. }
  128. /*
  129. * Catch failures to update intel_engines table when the new engines
  130. * are added to the driver by a warning and disabling the forgotten
  131. * engines.
  132. */
  133. if (WARN_ON(mask != INTEL_INFO(dev_priv)->ring_mask))
  134. device_info->ring_mask = mask;
  135. device_info->num_rings = hweight32(mask);
  136. return 0;
  137. cleanup:
  138. for (i = 0; i < I915_NUM_ENGINES; i++) {
  139. if (i915.enable_execlists)
  140. intel_logical_ring_cleanup(&dev_priv->engine[i]);
  141. else
  142. intel_engine_cleanup(&dev_priv->engine[i]);
  143. }
  144. return ret;
  145. }
  146. void intel_engine_init_seqno(struct intel_engine_cs *engine, u32 seqno)
  147. {
  148. struct drm_i915_private *dev_priv = engine->i915;
  149. /* Our semaphore implementation is strictly monotonic (i.e. we proceed
  150. * so long as the semaphore value in the register/page is greater
  151. * than the sync value), so whenever we reset the seqno,
  152. * so long as we reset the tracking semaphore value to 0, it will
  153. * always be before the next request's seqno. If we don't reset
  154. * the semaphore value, then when the seqno moves backwards all
  155. * future waits will complete instantly (causing rendering corruption).
  156. */
  157. if (IS_GEN6(dev_priv) || IS_GEN7(dev_priv)) {
  158. I915_WRITE(RING_SYNC_0(engine->mmio_base), 0);
  159. I915_WRITE(RING_SYNC_1(engine->mmio_base), 0);
  160. if (HAS_VEBOX(dev_priv))
  161. I915_WRITE(RING_SYNC_2(engine->mmio_base), 0);
  162. }
  163. if (dev_priv->semaphore) {
  164. struct page *page = i915_vma_first_page(dev_priv->semaphore);
  165. void *semaphores;
  166. /* Semaphores are in noncoherent memory, flush to be safe */
  167. semaphores = kmap(page);
  168. memset(semaphores + GEN8_SEMAPHORE_OFFSET(engine->id, 0),
  169. 0, I915_NUM_ENGINES * gen8_semaphore_seqno_size);
  170. drm_clflush_virt_range(semaphores + GEN8_SEMAPHORE_OFFSET(engine->id, 0),
  171. I915_NUM_ENGINES * gen8_semaphore_seqno_size);
  172. kunmap(page);
  173. }
  174. memset(engine->semaphore.sync_seqno, 0,
  175. sizeof(engine->semaphore.sync_seqno));
  176. intel_write_status_page(engine, I915_GEM_HWS_INDEX, seqno);
  177. if (engine->irq_seqno_barrier)
  178. engine->irq_seqno_barrier(engine);
  179. engine->last_submitted_seqno = seqno;
  180. engine->hangcheck.seqno = seqno;
  181. /* After manually advancing the seqno, fake the interrupt in case
  182. * there are any waiters for that seqno.
  183. */
  184. intel_engine_wakeup(engine);
  185. }
  186. void intel_engine_init_hangcheck(struct intel_engine_cs *engine)
  187. {
  188. memset(&engine->hangcheck, 0, sizeof(engine->hangcheck));
  189. clear_bit(engine->id, &engine->i915->gpu_error.missed_irq_rings);
  190. }
  191. static void intel_engine_init_requests(struct intel_engine_cs *engine)
  192. {
  193. init_request_active(&engine->last_request, NULL);
  194. INIT_LIST_HEAD(&engine->request_list);
  195. }
  196. /**
  197. * intel_engines_setup_common - setup engine state not requiring hw access
  198. * @engine: Engine to setup.
  199. *
  200. * Initializes @engine@ structure members shared between legacy and execlists
  201. * submission modes which do not require hardware access.
  202. *
  203. * Typically done early in the submission mode specific engine setup stage.
  204. */
  205. void intel_engine_setup_common(struct intel_engine_cs *engine)
  206. {
  207. INIT_LIST_HEAD(&engine->buffers);
  208. INIT_LIST_HEAD(&engine->execlist_queue);
  209. spin_lock_init(&engine->execlist_lock);
  210. engine->fence_context = fence_context_alloc(1);
  211. intel_engine_init_requests(engine);
  212. intel_engine_init_hangcheck(engine);
  213. i915_gem_batch_pool_init(engine, &engine->batch_pool);
  214. }
  215. int intel_engine_create_scratch(struct intel_engine_cs *engine, int size)
  216. {
  217. struct drm_i915_gem_object *obj;
  218. struct i915_vma *vma;
  219. int ret;
  220. WARN_ON(engine->scratch);
  221. obj = i915_gem_object_create_stolen(&engine->i915->drm, size);
  222. if (!obj)
  223. obj = i915_gem_object_create(&engine->i915->drm, size);
  224. if (IS_ERR(obj)) {
  225. DRM_ERROR("Failed to allocate scratch page\n");
  226. return PTR_ERR(obj);
  227. }
  228. vma = i915_vma_create(obj, &engine->i915->ggtt.base, NULL);
  229. if (IS_ERR(vma)) {
  230. ret = PTR_ERR(vma);
  231. goto err_unref;
  232. }
  233. ret = i915_vma_pin(vma, 0, 4096, PIN_GLOBAL | PIN_HIGH);
  234. if (ret)
  235. goto err_unref;
  236. engine->scratch = vma;
  237. DRM_DEBUG_DRIVER("%s pipe control offset: 0x%08llx\n",
  238. engine->name, vma->node.start);
  239. return 0;
  240. err_unref:
  241. i915_gem_object_put(obj);
  242. return ret;
  243. }
  244. static void intel_engine_cleanup_scratch(struct intel_engine_cs *engine)
  245. {
  246. struct i915_vma *vma;
  247. vma = fetch_and_zero(&engine->scratch);
  248. if (!vma)
  249. return;
  250. i915_vma_unpin(vma);
  251. i915_vma_put(vma);
  252. }
  253. /**
  254. * intel_engines_init_common - initialize cengine state which might require hw access
  255. * @engine: Engine to initialize.
  256. *
  257. * Initializes @engine@ structure members shared between legacy and execlists
  258. * submission modes which do require hardware access.
  259. *
  260. * Typcally done at later stages of submission mode specific engine setup.
  261. *
  262. * Returns zero on success or an error code on failure.
  263. */
  264. int intel_engine_init_common(struct intel_engine_cs *engine)
  265. {
  266. int ret;
  267. ret = intel_engine_init_breadcrumbs(engine);
  268. if (ret)
  269. return ret;
  270. return intel_engine_init_cmd_parser(engine);
  271. }
  272. /**
  273. * intel_engines_cleanup_common - cleans up the engine state created by
  274. * the common initiailizers.
  275. * @engine: Engine to cleanup.
  276. *
  277. * This cleans up everything created by the common helpers.
  278. */
  279. void intel_engine_cleanup_common(struct intel_engine_cs *engine)
  280. {
  281. intel_engine_cleanup_scratch(engine);
  282. intel_engine_cleanup_cmd_parser(engine);
  283. intel_engine_fini_breadcrumbs(engine);
  284. i915_gem_batch_pool_fini(&engine->batch_pool);
  285. }