i915_gem_render_state.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. }
  40. return NULL;
  41. }
  42. static int render_state_init(struct render_state *so, struct drm_device *dev)
  43. {
  44. int ret;
  45. so->gen = INTEL_INFO(dev)->gen;
  46. so->rodata = render_state_get_rodata(dev, so->gen);
  47. if (so->rodata == NULL)
  48. return 0;
  49. if (so->rodata->batch_items * 4 > 4096)
  50. return -EINVAL;
  51. so->obj = i915_gem_alloc_object(dev, 4096);
  52. if (so->obj == NULL)
  53. return -ENOMEM;
  54. ret = i915_gem_obj_ggtt_pin(so->obj, 4096, 0);
  55. if (ret)
  56. goto free_gem;
  57. so->ggtt_offset = i915_gem_obj_ggtt_offset(so->obj);
  58. return 0;
  59. free_gem:
  60. drm_gem_object_unreference(&so->obj->base);
  61. return ret;
  62. }
  63. static int render_state_setup(struct render_state *so)
  64. {
  65. const struct intel_renderstate_rodata *rodata = so->rodata;
  66. unsigned int i = 0, reloc_index = 0;
  67. struct page *page;
  68. u32 *d;
  69. int ret;
  70. ret = i915_gem_object_set_to_cpu_domain(so->obj, true);
  71. if (ret)
  72. return ret;
  73. page = sg_page(so->obj->pages->sgl);
  74. d = kmap(page);
  75. while (i < rodata->batch_items) {
  76. u32 s = rodata->batch[i];
  77. if (i * 4 == rodata->reloc[reloc_index]) {
  78. u64 r = s + so->ggtt_offset;
  79. s = lower_32_bits(r);
  80. if (so->gen >= 8) {
  81. if (i + 1 >= rodata->batch_items ||
  82. rodata->batch[i + 1] != 0)
  83. return -EINVAL;
  84. d[i++] = s;
  85. s = upper_32_bits(r);
  86. }
  87. reloc_index++;
  88. }
  89. d[i++] = s;
  90. }
  91. kunmap(page);
  92. ret = i915_gem_object_set_to_gtt_domain(so->obj, false);
  93. if (ret)
  94. return ret;
  95. if (rodata->reloc[reloc_index] != -1) {
  96. DRM_ERROR("only %d relocs resolved\n", reloc_index);
  97. return -EINVAL;
  98. }
  99. return 0;
  100. }
  101. void i915_gem_render_state_fini(struct render_state *so)
  102. {
  103. i915_gem_object_ggtt_unpin(so->obj);
  104. drm_gem_object_unreference(&so->obj->base);
  105. }
  106. int i915_gem_render_state_prepare(struct intel_engine_cs *ring,
  107. struct render_state *so)
  108. {
  109. int ret;
  110. if (WARN_ON(ring->id != RCS))
  111. return -ENOENT;
  112. ret = render_state_init(so, ring->dev);
  113. if (ret)
  114. return ret;
  115. if (so->rodata == NULL)
  116. return 0;
  117. ret = render_state_setup(so);
  118. if (ret) {
  119. i915_gem_render_state_fini(so);
  120. return ret;
  121. }
  122. return 0;
  123. }
  124. int i915_gem_render_state_init(struct intel_engine_cs *ring)
  125. {
  126. struct render_state so;
  127. int ret;
  128. ret = i915_gem_render_state_prepare(ring, &so);
  129. if (ret)
  130. return ret;
  131. if (so.rodata == NULL)
  132. return 0;
  133. ret = ring->dispatch_execbuffer(ring,
  134. so.ggtt_offset,
  135. so.rodata->batch_items * 4,
  136. I915_DISPATCH_SECURE);
  137. if (ret)
  138. goto out;
  139. i915_vma_move_to_active(i915_gem_obj_to_ggtt(so.obj), ring);
  140. ret = __i915_add_request(ring, NULL, so.obj, NULL);
  141. /* __i915_add_request moves object to inactive if it fails */
  142. out:
  143. i915_gem_render_state_fini(&so);
  144. return ret;
  145. }