i915_gem_render_state.c 5.3 KB

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