i915_gem_render_state.c 3.9 KB

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