i915_gem_render_state.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * Copyright © 2014 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. * Authors:
  24. * Mika Kuoppala <mika.kuoppala@intel.com>
  25. *
  26. */
  27. #include "i915_drv.h"
  28. #include "i915_gem_render_state.h"
  29. #include "intel_renderstate.h"
  30. struct intel_render_state {
  31. const struct intel_renderstate_rodata *rodata;
  32. struct drm_i915_gem_object *obj;
  33. struct i915_vma *vma;
  34. u32 batch_offset;
  35. u32 batch_size;
  36. u32 aux_offset;
  37. u32 aux_size;
  38. };
  39. static const struct intel_renderstate_rodata *
  40. render_state_get_rodata(const struct intel_engine_cs *engine)
  41. {
  42. if (engine->id != RCS)
  43. return NULL;
  44. switch (INTEL_GEN(engine->i915)) {
  45. case 6:
  46. return &gen6_null_state;
  47. case 7:
  48. return &gen7_null_state;
  49. case 8:
  50. return &gen8_null_state;
  51. case 9:
  52. return &gen9_null_state;
  53. }
  54. return NULL;
  55. }
  56. /*
  57. * Macro to add commands to auxiliary batch.
  58. * This macro only checks for page overflow before inserting the commands,
  59. * this is sufficient as the null state generator makes the final batch
  60. * with two passes to build command and state separately. At this point
  61. * the size of both are known and it compacts them by relocating the state
  62. * right after the commands taking care of alignment so we should sufficient
  63. * space below them for adding new commands.
  64. */
  65. #define OUT_BATCH(batch, i, val) \
  66. do { \
  67. if ((i) >= PAGE_SIZE / sizeof(u32)) \
  68. goto err; \
  69. (batch)[(i)++] = (val); \
  70. } while(0)
  71. static int render_state_setup(struct intel_render_state *so,
  72. struct drm_i915_private *i915)
  73. {
  74. const struct intel_renderstate_rodata *rodata = so->rodata;
  75. unsigned int i = 0, reloc_index = 0;
  76. unsigned int needs_clflush;
  77. u32 *d;
  78. int ret;
  79. ret = i915_gem_obj_prepare_shmem_write(so->obj, &needs_clflush);
  80. if (ret)
  81. return ret;
  82. d = kmap_atomic(i915_gem_object_get_dirty_page(so->obj, 0));
  83. while (i < rodata->batch_items) {
  84. u32 s = rodata->batch[i];
  85. if (i * 4 == rodata->reloc[reloc_index]) {
  86. u64 r = s + so->vma->node.start;
  87. s = lower_32_bits(r);
  88. if (HAS_64BIT_RELOC(i915)) {
  89. if (i + 1 >= rodata->batch_items ||
  90. rodata->batch[i + 1] != 0)
  91. goto err;
  92. d[i++] = s;
  93. s = upper_32_bits(r);
  94. }
  95. reloc_index++;
  96. }
  97. d[i++] = s;
  98. }
  99. if (rodata->reloc[reloc_index] != -1) {
  100. DRM_ERROR("only %d relocs resolved\n", reloc_index);
  101. goto err;
  102. }
  103. so->batch_offset = i915_ggtt_offset(so->vma);
  104. so->batch_size = rodata->batch_items * sizeof(u32);
  105. while (i % CACHELINE_DWORDS)
  106. OUT_BATCH(d, i, MI_NOOP);
  107. so->aux_offset = i * sizeof(u32);
  108. if (HAS_POOLED_EU(i915)) {
  109. /*
  110. * We always program 3x6 pool config but depending upon which
  111. * subslice is disabled HW drops down to appropriate config
  112. * shown below.
  113. *
  114. * In the below table 2x6 config always refers to
  115. * fused-down version, native 2x6 is not available and can
  116. * be ignored
  117. *
  118. * SNo subslices config eu pool configuration
  119. * -----------------------------------------------------------
  120. * 1 3 subslices enabled (3x6) - 0x00777000 (9+9)
  121. * 2 ss0 disabled (2x6) - 0x00777000 (3+9)
  122. * 3 ss1 disabled (2x6) - 0x00770000 (6+6)
  123. * 4 ss2 disabled (2x6) - 0x00007000 (9+3)
  124. */
  125. u32 eu_pool_config = 0x00777000;
  126. OUT_BATCH(d, i, GEN9_MEDIA_POOL_STATE);
  127. OUT_BATCH(d, i, GEN9_MEDIA_POOL_ENABLE);
  128. OUT_BATCH(d, i, eu_pool_config);
  129. OUT_BATCH(d, i, 0);
  130. OUT_BATCH(d, i, 0);
  131. OUT_BATCH(d, i, 0);
  132. }
  133. OUT_BATCH(d, i, MI_BATCH_BUFFER_END);
  134. so->aux_size = i * sizeof(u32) - so->aux_offset;
  135. so->aux_offset += so->batch_offset;
  136. /*
  137. * Since we are sending length, we need to strictly conform to
  138. * all requirements. For Gen2 this must be a multiple of 8.
  139. */
  140. so->aux_size = ALIGN(so->aux_size, 8);
  141. if (needs_clflush)
  142. drm_clflush_virt_range(d, i * sizeof(u32));
  143. kunmap_atomic(d);
  144. ret = i915_gem_object_set_to_gtt_domain(so->obj, false);
  145. out:
  146. i915_gem_obj_finish_shmem_access(so->obj);
  147. return ret;
  148. err:
  149. kunmap_atomic(d);
  150. ret = -EINVAL;
  151. goto out;
  152. }
  153. #undef OUT_BATCH
  154. int i915_gem_render_state_emit(struct i915_request *rq)
  155. {
  156. struct intel_engine_cs *engine = rq->engine;
  157. struct intel_render_state so = {}; /* keep the compiler happy */
  158. int err;
  159. so.rodata = render_state_get_rodata(engine);
  160. if (!so.rodata)
  161. return 0;
  162. if (so.rodata->batch_items * 4 > PAGE_SIZE)
  163. return -EINVAL;
  164. so.obj = i915_gem_object_create_internal(engine->i915, PAGE_SIZE);
  165. if (IS_ERR(so.obj))
  166. return PTR_ERR(so.obj);
  167. so.vma = i915_vma_instance(so.obj, &engine->i915->ggtt.vm, NULL);
  168. if (IS_ERR(so.vma)) {
  169. err = PTR_ERR(so.vma);
  170. goto err_obj;
  171. }
  172. err = i915_vma_pin(so.vma, 0, 0, PIN_GLOBAL | PIN_HIGH);
  173. if (err)
  174. goto err_vma;
  175. err = render_state_setup(&so, rq->i915);
  176. if (err)
  177. goto err_unpin;
  178. err = engine->emit_bb_start(rq,
  179. so.batch_offset, so.batch_size,
  180. I915_DISPATCH_SECURE);
  181. if (err)
  182. goto err_unpin;
  183. if (so.aux_size > 8) {
  184. err = engine->emit_bb_start(rq,
  185. so.aux_offset, so.aux_size,
  186. I915_DISPATCH_SECURE);
  187. if (err)
  188. goto err_unpin;
  189. }
  190. err = i915_vma_move_to_active(so.vma, rq, 0);
  191. err_unpin:
  192. i915_vma_unpin(so.vma);
  193. err_vma:
  194. i915_vma_close(so.vma);
  195. err_obj:
  196. __i915_gem_object_release_unless_active(so.obj);
  197. return err;
  198. }