i915_gem_render_state.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 render_state {
  30. const struct intel_renderstate_rodata *rodata;
  31. struct i915_vma *vma;
  32. u32 aux_batch_size;
  33. u32 aux_batch_offset;
  34. };
  35. static const struct intel_renderstate_rodata *
  36. render_state_get_rodata(const struct drm_i915_gem_request *req)
  37. {
  38. switch (INTEL_GEN(req->i915)) {
  39. case 6:
  40. return &gen6_null_state;
  41. case 7:
  42. return &gen7_null_state;
  43. case 8:
  44. return &gen8_null_state;
  45. case 9:
  46. return &gen9_null_state;
  47. }
  48. return NULL;
  49. }
  50. /*
  51. * Macro to add commands to auxiliary batch.
  52. * This macro only checks for page overflow before inserting the commands,
  53. * this is sufficient as the null state generator makes the final batch
  54. * with two passes to build command and state separately. At this point
  55. * the size of both are known and it compacts them by relocating the state
  56. * right after the commands taking care of aligment so we should sufficient
  57. * space below them for adding new commands.
  58. */
  59. #define OUT_BATCH(batch, i, val) \
  60. do { \
  61. if (WARN_ON((i) >= PAGE_SIZE / sizeof(u32))) { \
  62. ret = -ENOSPC; \
  63. goto err_out; \
  64. } \
  65. (batch)[(i)++] = (val); \
  66. } while(0)
  67. static int render_state_setup(struct render_state *so)
  68. {
  69. struct drm_device *dev = so->vma->vm->dev;
  70. const struct intel_renderstate_rodata *rodata = so->rodata;
  71. const bool has_64bit_reloc = INTEL_GEN(dev) >= 8;
  72. unsigned int i = 0, reloc_index = 0;
  73. struct page *page;
  74. u32 *d;
  75. int ret;
  76. ret = i915_gem_object_set_to_cpu_domain(so->vma->obj, true);
  77. if (ret)
  78. return ret;
  79. page = i915_gem_object_get_dirty_page(so->vma->obj, 0);
  80. d = kmap(page);
  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. ret = -EINVAL;
  90. goto err_out;
  91. }
  92. d[i++] = s;
  93. s = upper_32_bits(r);
  94. }
  95. reloc_index++;
  96. }
  97. d[i++] = s;
  98. }
  99. while (i % CACHELINE_DWORDS)
  100. OUT_BATCH(d, i, MI_NOOP);
  101. so->aux_batch_offset = i * sizeof(u32);
  102. if (HAS_POOLED_EU(dev)) {
  103. /*
  104. * We always program 3x6 pool config but depending upon which
  105. * subslice is disabled HW drops down to appropriate config
  106. * shown below.
  107. *
  108. * In the below table 2x6 config always refers to
  109. * fused-down version, native 2x6 is not available and can
  110. * be ignored
  111. *
  112. * SNo subslices config eu pool configuration
  113. * -----------------------------------------------------------
  114. * 1 3 subslices enabled (3x6) - 0x00777000 (9+9)
  115. * 2 ss0 disabled (2x6) - 0x00777000 (3+9)
  116. * 3 ss1 disabled (2x6) - 0x00770000 (6+6)
  117. * 4 ss2 disabled (2x6) - 0x00007000 (9+3)
  118. */
  119. u32 eu_pool_config = 0x00777000;
  120. OUT_BATCH(d, i, GEN9_MEDIA_POOL_STATE);
  121. OUT_BATCH(d, i, GEN9_MEDIA_POOL_ENABLE);
  122. OUT_BATCH(d, i, eu_pool_config);
  123. OUT_BATCH(d, i, 0);
  124. OUT_BATCH(d, i, 0);
  125. OUT_BATCH(d, i, 0);
  126. }
  127. OUT_BATCH(d, i, MI_BATCH_BUFFER_END);
  128. so->aux_batch_size = (i * sizeof(u32)) - so->aux_batch_offset;
  129. /*
  130. * Since we are sending length, we need to strictly conform to
  131. * all requirements. For Gen2 this must be a multiple of 8.
  132. */
  133. so->aux_batch_size = ALIGN(so->aux_batch_size, 8);
  134. kunmap(page);
  135. ret = i915_gem_object_set_to_gtt_domain(so->vma->obj, false);
  136. if (ret)
  137. return ret;
  138. if (rodata->reloc[reloc_index] != -1) {
  139. DRM_ERROR("only %d relocs resolved\n", reloc_index);
  140. return -EINVAL;
  141. }
  142. return 0;
  143. err_out:
  144. kunmap(page);
  145. return ret;
  146. }
  147. #undef OUT_BATCH
  148. int i915_gem_render_state_init(struct drm_i915_gem_request *req)
  149. {
  150. struct render_state so;
  151. struct drm_i915_gem_object *obj;
  152. int ret;
  153. if (WARN_ON(req->engine->id != RCS))
  154. return -ENOENT;
  155. so.rodata = render_state_get_rodata(req);
  156. if (!so.rodata)
  157. return 0;
  158. if (so.rodata->batch_items * 4 > 4096)
  159. return -EINVAL;
  160. obj = i915_gem_object_create(&req->i915->drm, 4096);
  161. if (IS_ERR(obj))
  162. return PTR_ERR(obj);
  163. so.vma = i915_vma_create(obj, &req->i915->ggtt.base, NULL);
  164. if (IS_ERR(so.vma)) {
  165. ret = PTR_ERR(so.vma);
  166. goto err_obj;
  167. }
  168. ret = i915_vma_pin(so.vma, 0, 0, PIN_GLOBAL);
  169. if (ret)
  170. goto err_obj;
  171. ret = render_state_setup(&so);
  172. if (ret)
  173. goto err_unpin;
  174. ret = req->engine->emit_bb_start(req, so.vma->node.start,
  175. so.rodata->batch_items * 4,
  176. I915_DISPATCH_SECURE);
  177. if (ret)
  178. goto err_unpin;
  179. if (so.aux_batch_size > 8) {
  180. ret = req->engine->emit_bb_start(req,
  181. (so.vma->node.start +
  182. so.aux_batch_offset),
  183. so.aux_batch_size,
  184. I915_DISPATCH_SECURE);
  185. if (ret)
  186. goto err_unpin;
  187. }
  188. i915_vma_move_to_active(so.vma, req, 0);
  189. err_unpin:
  190. i915_vma_unpin(so.vma);
  191. err_obj:
  192. i915_gem_object_put(obj);
  193. return ret;
  194. }