i915_gem_render_state.c 6.9 KB

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