i915_gem_render_state.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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->drm, 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. struct drm_device *dev = so->obj->base.dev;
  86. const struct intel_renderstate_rodata *rodata = so->rodata;
  87. unsigned int i = 0, reloc_index = 0;
  88. struct page *page;
  89. u32 *d;
  90. int ret;
  91. ret = i915_gem_object_set_to_cpu_domain(so->obj, true);
  92. if (ret)
  93. return ret;
  94. page = i915_gem_object_get_dirty_page(so->obj, 0);
  95. d = kmap(page);
  96. while (i < rodata->batch_items) {
  97. u32 s = rodata->batch[i];
  98. if (i * 4 == rodata->reloc[reloc_index]) {
  99. u64 r = s + so->ggtt_offset;
  100. s = lower_32_bits(r);
  101. if (so->gen >= 8) {
  102. if (i + 1 >= rodata->batch_items ||
  103. rodata->batch[i + 1] != 0) {
  104. ret = -EINVAL;
  105. goto err_out;
  106. }
  107. d[i++] = s;
  108. s = upper_32_bits(r);
  109. }
  110. reloc_index++;
  111. }
  112. d[i++] = s;
  113. }
  114. while (i % CACHELINE_DWORDS)
  115. OUT_BATCH(d, i, MI_NOOP);
  116. so->aux_batch_offset = i * sizeof(u32);
  117. if (HAS_POOLED_EU(dev)) {
  118. /*
  119. * We always program 3x6 pool config but depending upon which
  120. * subslice is disabled HW drops down to appropriate config
  121. * shown below.
  122. *
  123. * In the below table 2x6 config always refers to
  124. * fused-down version, native 2x6 is not available and can
  125. * be ignored
  126. *
  127. * SNo subslices config eu pool configuration
  128. * -----------------------------------------------------------
  129. * 1 3 subslices enabled (3x6) - 0x00777000 (9+9)
  130. * 2 ss0 disabled (2x6) - 0x00777000 (3+9)
  131. * 3 ss1 disabled (2x6) - 0x00770000 (6+6)
  132. * 4 ss2 disabled (2x6) - 0x00007000 (9+3)
  133. */
  134. u32 eu_pool_config = 0x00777000;
  135. OUT_BATCH(d, i, GEN9_MEDIA_POOL_STATE);
  136. OUT_BATCH(d, i, GEN9_MEDIA_POOL_ENABLE);
  137. OUT_BATCH(d, i, eu_pool_config);
  138. OUT_BATCH(d, i, 0);
  139. OUT_BATCH(d, i, 0);
  140. OUT_BATCH(d, i, 0);
  141. }
  142. OUT_BATCH(d, i, MI_BATCH_BUFFER_END);
  143. so->aux_batch_size = (i * sizeof(u32)) - so->aux_batch_offset;
  144. /*
  145. * Since we are sending length, we need to strictly conform to
  146. * all requirements. For Gen2 this must be a multiple of 8.
  147. */
  148. so->aux_batch_size = ALIGN(so->aux_batch_size, 8);
  149. kunmap(page);
  150. ret = i915_gem_object_set_to_gtt_domain(so->obj, false);
  151. if (ret)
  152. return ret;
  153. if (rodata->reloc[reloc_index] != -1) {
  154. DRM_ERROR("only %d relocs resolved\n", reloc_index);
  155. return -EINVAL;
  156. }
  157. return 0;
  158. err_out:
  159. kunmap(page);
  160. return ret;
  161. }
  162. #undef OUT_BATCH
  163. void i915_gem_render_state_fini(struct render_state *so)
  164. {
  165. i915_gem_object_ggtt_unpin(so->obj);
  166. drm_gem_object_unreference(&so->obj->base);
  167. }
  168. int i915_gem_render_state_prepare(struct intel_engine_cs *engine,
  169. struct render_state *so)
  170. {
  171. int ret;
  172. if (WARN_ON(engine->id != RCS))
  173. return -ENOENT;
  174. ret = render_state_init(so, engine->i915);
  175. if (ret)
  176. return ret;
  177. if (so->rodata == NULL)
  178. return 0;
  179. ret = render_state_setup(so);
  180. if (ret) {
  181. i915_gem_render_state_fini(so);
  182. return ret;
  183. }
  184. return 0;
  185. }
  186. int i915_gem_render_state_init(struct drm_i915_gem_request *req)
  187. {
  188. struct render_state so;
  189. int ret;
  190. ret = i915_gem_render_state_prepare(req->engine, &so);
  191. if (ret)
  192. return ret;
  193. if (so.rodata == NULL)
  194. return 0;
  195. ret = req->engine->dispatch_execbuffer(req, so.ggtt_offset,
  196. so.rodata->batch_items * 4,
  197. I915_DISPATCH_SECURE);
  198. if (ret)
  199. goto out;
  200. if (so.aux_batch_size > 8) {
  201. ret = req->engine->dispatch_execbuffer(req,
  202. (so.ggtt_offset +
  203. so.aux_batch_offset),
  204. so.aux_batch_size,
  205. I915_DISPATCH_SECURE);
  206. if (ret)
  207. goto out;
  208. }
  209. i915_vma_move_to_active(i915_gem_obj_to_ggtt(so.obj), req);
  210. out:
  211. i915_gem_render_state_fini(&so);
  212. return ret;
  213. }