i915_gem_render_state.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. static const struct intel_renderstate_rodata *
  30. render_state_get_rodata(const int gen)
  31. {
  32. switch (gen) {
  33. case 6:
  34. return &gen6_null_state;
  35. case 7:
  36. return &gen7_null_state;
  37. case 8:
  38. return &gen8_null_state;
  39. case 9:
  40. return &gen9_null_state;
  41. }
  42. return NULL;
  43. }
  44. static int render_state_init(struct render_state *so,
  45. struct drm_i915_private *dev_priv)
  46. {
  47. int ret;
  48. so->gen = INTEL_GEN(dev_priv);
  49. so->rodata = render_state_get_rodata(so->gen);
  50. if (so->rodata == NULL)
  51. return 0;
  52. if (so->rodata->batch_items * 4 > 4096)
  53. return -EINVAL;
  54. so->obj = i915_gem_object_create(dev_priv->dev, 4096);
  55. if (IS_ERR(so->obj))
  56. return PTR_ERR(so->obj);
  57. ret = i915_gem_obj_ggtt_pin(so->obj, 4096, 0);
  58. if (ret)
  59. goto free_gem;
  60. so->ggtt_offset = i915_gem_obj_ggtt_offset(so->obj);
  61. return 0;
  62. free_gem:
  63. drm_gem_object_unreference(&so->obj->base);
  64. return ret;
  65. }
  66. /*
  67. * Macro to add commands to auxiliary batch.
  68. * This macro only checks for page overflow before inserting the commands,
  69. * this is sufficient as the null state generator makes the final batch
  70. * with two passes to build command and state separately. At this point
  71. * the size of both are known and it compacts them by relocating the state
  72. * right after the commands taking care of aligment so we should sufficient
  73. * space below them for adding new commands.
  74. */
  75. #define OUT_BATCH(batch, i, val) \
  76. do { \
  77. if (WARN_ON((i) >= PAGE_SIZE / sizeof(u32))) { \
  78. ret = -ENOSPC; \
  79. goto err_out; \
  80. } \
  81. (batch)[(i)++] = (val); \
  82. } while(0)
  83. static int render_state_setup(struct render_state *so)
  84. {
  85. const struct intel_renderstate_rodata *rodata = so->rodata;
  86. unsigned int i = 0, reloc_index = 0;
  87. struct page *page;
  88. u32 *d;
  89. int ret;
  90. ret = i915_gem_object_set_to_cpu_domain(so->obj, true);
  91. if (ret)
  92. return ret;
  93. page = i915_gem_object_get_dirty_page(so->obj, 0);
  94. d = kmap(page);
  95. while (i < rodata->batch_items) {
  96. u32 s = rodata->batch[i];
  97. if (i * 4 == rodata->reloc[reloc_index]) {
  98. u64 r = s + so->ggtt_offset;
  99. s = lower_32_bits(r);
  100. if (so->gen >= 8) {
  101. if (i + 1 >= rodata->batch_items ||
  102. rodata->batch[i + 1] != 0) {
  103. ret = -EINVAL;
  104. goto err_out;
  105. }
  106. d[i++] = s;
  107. s = upper_32_bits(r);
  108. }
  109. reloc_index++;
  110. }
  111. d[i++] = s;
  112. }
  113. while (i % CACHELINE_DWORDS)
  114. OUT_BATCH(d, i, MI_NOOP);
  115. so->aux_batch_offset = i * sizeof(u32);
  116. OUT_BATCH(d, i, MI_BATCH_BUFFER_END);
  117. so->aux_batch_size = (i * sizeof(u32)) - so->aux_batch_offset;
  118. /*
  119. * Since we are sending length, we need to strictly conform to
  120. * all requirements. For Gen2 this must be a multiple of 8.
  121. */
  122. so->aux_batch_size = ALIGN(so->aux_batch_size, 8);
  123. kunmap(page);
  124. ret = i915_gem_object_set_to_gtt_domain(so->obj, false);
  125. if (ret)
  126. return ret;
  127. if (rodata->reloc[reloc_index] != -1) {
  128. DRM_ERROR("only %d relocs resolved\n", reloc_index);
  129. return -EINVAL;
  130. }
  131. return 0;
  132. err_out:
  133. kunmap(page);
  134. return ret;
  135. }
  136. #undef OUT_BATCH
  137. void i915_gem_render_state_fini(struct render_state *so)
  138. {
  139. i915_gem_object_ggtt_unpin(so->obj);
  140. drm_gem_object_unreference(&so->obj->base);
  141. }
  142. int i915_gem_render_state_prepare(struct intel_engine_cs *engine,
  143. struct render_state *so)
  144. {
  145. int ret;
  146. if (WARN_ON(engine->id != RCS))
  147. return -ENOENT;
  148. ret = render_state_init(so, engine->i915);
  149. if (ret)
  150. return ret;
  151. if (so->rodata == NULL)
  152. return 0;
  153. ret = render_state_setup(so);
  154. if (ret) {
  155. i915_gem_render_state_fini(so);
  156. return ret;
  157. }
  158. return 0;
  159. }
  160. int i915_gem_render_state_init(struct drm_i915_gem_request *req)
  161. {
  162. struct render_state so;
  163. int ret;
  164. ret = i915_gem_render_state_prepare(req->engine, &so);
  165. if (ret)
  166. return ret;
  167. if (so.rodata == NULL)
  168. return 0;
  169. ret = req->engine->dispatch_execbuffer(req, so.ggtt_offset,
  170. so.rodata->batch_items * 4,
  171. I915_DISPATCH_SECURE);
  172. if (ret)
  173. goto out;
  174. if (so.aux_batch_size > 8) {
  175. ret = req->engine->dispatch_execbuffer(req,
  176. (so.ggtt_offset +
  177. so.aux_batch_offset),
  178. so.aux_batch_size,
  179. I915_DISPATCH_SECURE);
  180. if (ret)
  181. goto out;
  182. }
  183. i915_vma_move_to_active(i915_gem_obj_to_ggtt(so.obj), req);
  184. out:
  185. i915_gem_render_state_fini(&so);
  186. return ret;
  187. }