i915_gem_render_state.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. struct drm_i915_gem_object *obj = so->vma->obj;
  72. unsigned int i = 0, reloc_index = 0;
  73. unsigned int needs_clflush;
  74. u32 *d;
  75. int ret;
  76. ret = i915_gem_obj_prepare_shmem_write(obj, &needs_clflush);
  77. if (ret)
  78. return ret;
  79. d = kmap_atomic(i915_gem_object_get_dirty_page(obj, 0));
  80. while (i < rodata->batch_items) {
  81. u32 s = rodata->batch[i];
  82. if (i * 4 == rodata->reloc[reloc_index]) {
  83. u64 r = s + so->vma->node.start;
  84. s = lower_32_bits(r);
  85. if (HAS_64BIT_RELOC(i915)) {
  86. if (i + 1 >= rodata->batch_items ||
  87. rodata->batch[i + 1] != 0)
  88. goto err;
  89. d[i++] = s;
  90. s = upper_32_bits(r);
  91. }
  92. reloc_index++;
  93. }
  94. d[i++] = s;
  95. }
  96. if (rodata->reloc[reloc_index] != -1) {
  97. DRM_ERROR("only %d relocs resolved\n", reloc_index);
  98. goto err;
  99. }
  100. so->batch_offset = so->vma->node.start;
  101. so->batch_size = rodata->batch_items * sizeof(u32);
  102. while (i % CACHELINE_DWORDS)
  103. OUT_BATCH(d, i, MI_NOOP);
  104. so->aux_offset = i * sizeof(u32);
  105. if (HAS_POOLED_EU(i915)) {
  106. /*
  107. * We always program 3x6 pool config but depending upon which
  108. * subslice is disabled HW drops down to appropriate config
  109. * shown below.
  110. *
  111. * In the below table 2x6 config always refers to
  112. * fused-down version, native 2x6 is not available and can
  113. * be ignored
  114. *
  115. * SNo subslices config eu pool configuration
  116. * -----------------------------------------------------------
  117. * 1 3 subslices enabled (3x6) - 0x00777000 (9+9)
  118. * 2 ss0 disabled (2x6) - 0x00777000 (3+9)
  119. * 3 ss1 disabled (2x6) - 0x00770000 (6+6)
  120. * 4 ss2 disabled (2x6) - 0x00007000 (9+3)
  121. */
  122. u32 eu_pool_config = 0x00777000;
  123. OUT_BATCH(d, i, GEN9_MEDIA_POOL_STATE);
  124. OUT_BATCH(d, i, GEN9_MEDIA_POOL_ENABLE);
  125. OUT_BATCH(d, i, eu_pool_config);
  126. OUT_BATCH(d, i, 0);
  127. OUT_BATCH(d, i, 0);
  128. OUT_BATCH(d, i, 0);
  129. }
  130. OUT_BATCH(d, i, MI_BATCH_BUFFER_END);
  131. so->aux_size = i * sizeof(u32) - so->aux_offset;
  132. so->aux_offset += so->batch_offset;
  133. /*
  134. * Since we are sending length, we need to strictly conform to
  135. * all requirements. For Gen2 this must be a multiple of 8.
  136. */
  137. so->aux_size = ALIGN(so->aux_size, 8);
  138. if (needs_clflush)
  139. drm_clflush_virt_range(d, i * sizeof(u32));
  140. kunmap_atomic(d);
  141. ret = i915_gem_object_set_to_gtt_domain(obj, false);
  142. out:
  143. i915_gem_obj_finish_shmem_access(obj);
  144. return ret;
  145. err:
  146. kunmap_atomic(d);
  147. ret = -EINVAL;
  148. goto out;
  149. }
  150. #undef OUT_BATCH
  151. int i915_gem_render_state_init(struct intel_engine_cs *engine)
  152. {
  153. struct intel_render_state *so;
  154. const struct intel_renderstate_rodata *rodata;
  155. struct drm_i915_gem_object *obj;
  156. int ret;
  157. if (engine->id != RCS)
  158. return 0;
  159. rodata = render_state_get_rodata(engine);
  160. if (!rodata)
  161. return 0;
  162. if (rodata->batch_items * 4 > PAGE_SIZE)
  163. return -EINVAL;
  164. so = kmalloc(sizeof(*so), GFP_KERNEL);
  165. if (!so)
  166. return -ENOMEM;
  167. obj = i915_gem_object_create_internal(engine->i915, PAGE_SIZE);
  168. if (IS_ERR(obj)) {
  169. ret = PTR_ERR(obj);
  170. goto err_free;
  171. }
  172. so->vma = i915_vma_instance(obj, &engine->i915->ggtt.base, NULL);
  173. if (IS_ERR(so->vma)) {
  174. ret = PTR_ERR(so->vma);
  175. goto err_obj;
  176. }
  177. so->rodata = rodata;
  178. engine->render_state = so;
  179. return 0;
  180. err_obj:
  181. i915_gem_object_put(obj);
  182. err_free:
  183. kfree(so);
  184. return ret;
  185. }
  186. int i915_gem_render_state_emit(struct drm_i915_gem_request *req)
  187. {
  188. struct intel_render_state *so;
  189. int ret;
  190. lockdep_assert_held(&req->i915->drm.struct_mutex);
  191. so = req->engine->render_state;
  192. if (!so)
  193. return 0;
  194. /* Recreate the page after shrinking */
  195. if (!so->vma->obj->mm.pages)
  196. so->batch_offset = -1;
  197. ret = i915_vma_pin(so->vma, 0, 0, PIN_GLOBAL | PIN_HIGH);
  198. if (ret)
  199. return ret;
  200. if (so->vma->node.start != so->batch_offset) {
  201. ret = render_state_setup(so, req->i915);
  202. if (ret)
  203. goto err_unpin;
  204. }
  205. ret = req->engine->emit_bb_start(req,
  206. so->batch_offset, so->batch_size,
  207. I915_DISPATCH_SECURE);
  208. if (ret)
  209. goto err_unpin;
  210. if (so->aux_size > 8) {
  211. ret = req->engine->emit_bb_start(req,
  212. so->aux_offset, so->aux_size,
  213. I915_DISPATCH_SECURE);
  214. if (ret)
  215. goto err_unpin;
  216. }
  217. i915_vma_move_to_active(so->vma, req, 0);
  218. err_unpin:
  219. i915_vma_unpin(so->vma);
  220. return ret;
  221. }
  222. void i915_gem_render_state_fini(struct intel_engine_cs *engine)
  223. {
  224. struct intel_render_state *so;
  225. struct drm_i915_gem_object *obj;
  226. so = fetch_and_zero(&engine->render_state);
  227. if (!so)
  228. return;
  229. obj = so->vma->obj;
  230. i915_vma_close(so->vma);
  231. __i915_gem_object_release_unless_active(obj);
  232. kfree(so);
  233. }