intel_engine_cs.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. enum intel_engine_hw_id hw_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. .hw_id = RCS_HW,
  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. .hw_id = BCS_HW,
  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. .hw_id = VCS_HW,
  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. .hw_id = VCS2_HW,
  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. .hw_id = VECS_HW,
  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->hw_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. }
  190. static void intel_engine_init_requests(struct intel_engine_cs *engine)
  191. {
  192. init_request_active(&engine->last_request, NULL);
  193. INIT_LIST_HEAD(&engine->request_list);
  194. }
  195. /**
  196. * intel_engines_setup_common - setup engine state not requiring hw access
  197. * @engine: Engine to setup.
  198. *
  199. * Initializes @engine@ structure members shared between legacy and execlists
  200. * submission modes which do not require hardware access.
  201. *
  202. * Typically done early in the submission mode specific engine setup stage.
  203. */
  204. void intel_engine_setup_common(struct intel_engine_cs *engine)
  205. {
  206. INIT_LIST_HEAD(&engine->execlist_queue);
  207. spin_lock_init(&engine->execlist_lock);
  208. engine->fence_context = fence_context_alloc(1);
  209. intel_engine_init_requests(engine);
  210. intel_engine_init_hangcheck(engine);
  211. i915_gem_batch_pool_init(engine, &engine->batch_pool);
  212. intel_engine_init_cmd_parser(engine);
  213. }
  214. int intel_engine_create_scratch(struct intel_engine_cs *engine, int size)
  215. {
  216. struct drm_i915_gem_object *obj;
  217. struct i915_vma *vma;
  218. int ret;
  219. WARN_ON(engine->scratch);
  220. obj = i915_gem_object_create_stolen(&engine->i915->drm, size);
  221. if (!obj)
  222. obj = i915_gem_object_create(&engine->i915->drm, size);
  223. if (IS_ERR(obj)) {
  224. DRM_ERROR("Failed to allocate scratch page\n");
  225. return PTR_ERR(obj);
  226. }
  227. vma = i915_vma_create(obj, &engine->i915->ggtt.base, NULL);
  228. if (IS_ERR(vma)) {
  229. ret = PTR_ERR(vma);
  230. goto err_unref;
  231. }
  232. ret = i915_vma_pin(vma, 0, 4096, PIN_GLOBAL | PIN_HIGH);
  233. if (ret)
  234. goto err_unref;
  235. engine->scratch = vma;
  236. DRM_DEBUG_DRIVER("%s pipe control offset: 0x%08x\n",
  237. engine->name, i915_ggtt_offset(vma));
  238. return 0;
  239. err_unref:
  240. i915_gem_object_put(obj);
  241. return ret;
  242. }
  243. static void intel_engine_cleanup_scratch(struct intel_engine_cs *engine)
  244. {
  245. i915_vma_unpin_and_release(&engine->scratch);
  246. }
  247. /**
  248. * intel_engines_init_common - initialize cengine state which might require hw access
  249. * @engine: Engine to initialize.
  250. *
  251. * Initializes @engine@ structure members shared between legacy and execlists
  252. * submission modes which do require hardware access.
  253. *
  254. * Typcally done at later stages of submission mode specific engine setup.
  255. *
  256. * Returns zero on success or an error code on failure.
  257. */
  258. int intel_engine_init_common(struct intel_engine_cs *engine)
  259. {
  260. int ret;
  261. ret = intel_engine_init_breadcrumbs(engine);
  262. if (ret)
  263. return ret;
  264. return 0;
  265. }
  266. /**
  267. * intel_engines_cleanup_common - cleans up the engine state created by
  268. * the common initiailizers.
  269. * @engine: Engine to cleanup.
  270. *
  271. * This cleans up everything created by the common helpers.
  272. */
  273. void intel_engine_cleanup_common(struct intel_engine_cs *engine)
  274. {
  275. intel_engine_cleanup_scratch(engine);
  276. intel_engine_fini_breadcrumbs(engine);
  277. intel_engine_cleanup_cmd_parser(engine);
  278. i915_gem_batch_pool_fini(&engine->batch_pool);
  279. }