i915_gem_clflush.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Copyright © 2016 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. */
  24. #include "i915_drv.h"
  25. #include "intel_frontbuffer.h"
  26. #include "i915_gem_clflush.h"
  27. static DEFINE_SPINLOCK(clflush_lock);
  28. struct clflush {
  29. struct dma_fence dma; /* Must be first for dma_fence_free() */
  30. struct i915_sw_fence wait;
  31. struct work_struct work;
  32. struct drm_i915_gem_object *obj;
  33. };
  34. static const char *i915_clflush_get_driver_name(struct dma_fence *fence)
  35. {
  36. return DRIVER_NAME;
  37. }
  38. static const char *i915_clflush_get_timeline_name(struct dma_fence *fence)
  39. {
  40. return "clflush";
  41. }
  42. static void i915_clflush_release(struct dma_fence *fence)
  43. {
  44. struct clflush *clflush = container_of(fence, typeof(*clflush), dma);
  45. i915_sw_fence_fini(&clflush->wait);
  46. BUILD_BUG_ON(offsetof(typeof(*clflush), dma));
  47. dma_fence_free(&clflush->dma);
  48. }
  49. static const struct dma_fence_ops i915_clflush_ops = {
  50. .get_driver_name = i915_clflush_get_driver_name,
  51. .get_timeline_name = i915_clflush_get_timeline_name,
  52. .release = i915_clflush_release,
  53. };
  54. static void __i915_do_clflush(struct drm_i915_gem_object *obj)
  55. {
  56. GEM_BUG_ON(!i915_gem_object_has_pages(obj));
  57. drm_clflush_sg(obj->mm.pages);
  58. intel_fb_obj_flush(obj, ORIGIN_CPU);
  59. }
  60. static void i915_clflush_work(struct work_struct *work)
  61. {
  62. struct clflush *clflush = container_of(work, typeof(*clflush), work);
  63. struct drm_i915_gem_object *obj = clflush->obj;
  64. if (i915_gem_object_pin_pages(obj)) {
  65. DRM_ERROR("Failed to acquire obj->pages for clflushing\n");
  66. goto out;
  67. }
  68. __i915_do_clflush(obj);
  69. i915_gem_object_unpin_pages(obj);
  70. out:
  71. i915_gem_object_put(obj);
  72. dma_fence_signal(&clflush->dma);
  73. dma_fence_put(&clflush->dma);
  74. }
  75. static int __i915_sw_fence_call
  76. i915_clflush_notify(struct i915_sw_fence *fence,
  77. enum i915_sw_fence_notify state)
  78. {
  79. struct clflush *clflush = container_of(fence, typeof(*clflush), wait);
  80. switch (state) {
  81. case FENCE_COMPLETE:
  82. schedule_work(&clflush->work);
  83. break;
  84. case FENCE_FREE:
  85. dma_fence_put(&clflush->dma);
  86. break;
  87. }
  88. return NOTIFY_DONE;
  89. }
  90. bool i915_gem_clflush_object(struct drm_i915_gem_object *obj,
  91. unsigned int flags)
  92. {
  93. struct clflush *clflush;
  94. /*
  95. * Stolen memory is always coherent with the GPU as it is explicitly
  96. * marked as wc by the system, or the system is cache-coherent.
  97. * Similarly, we only access struct pages through the CPU cache, so
  98. * anything not backed by physical memory we consider to be always
  99. * coherent and not need clflushing.
  100. */
  101. if (!i915_gem_object_has_struct_page(obj)) {
  102. obj->cache_dirty = false;
  103. return false;
  104. }
  105. /* If the GPU is snooping the contents of the CPU cache,
  106. * we do not need to manually clear the CPU cache lines. However,
  107. * the caches are only snooped when the render cache is
  108. * flushed/invalidated. As we always have to emit invalidations
  109. * and flushes when moving into and out of the RENDER domain, correct
  110. * snooping behaviour occurs naturally as the result of our domain
  111. * tracking.
  112. */
  113. if (!(flags & I915_CLFLUSH_FORCE) &&
  114. obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ)
  115. return false;
  116. trace_i915_gem_object_clflush(obj);
  117. clflush = NULL;
  118. if (!(flags & I915_CLFLUSH_SYNC))
  119. clflush = kmalloc(sizeof(*clflush), GFP_KERNEL);
  120. if (clflush) {
  121. GEM_BUG_ON(!obj->cache_dirty);
  122. dma_fence_init(&clflush->dma,
  123. &i915_clflush_ops,
  124. &clflush_lock,
  125. to_i915(obj->base.dev)->mm.unordered_timeline,
  126. 0);
  127. i915_sw_fence_init(&clflush->wait, i915_clflush_notify);
  128. clflush->obj = i915_gem_object_get(obj);
  129. INIT_WORK(&clflush->work, i915_clflush_work);
  130. dma_fence_get(&clflush->dma);
  131. i915_sw_fence_await_reservation(&clflush->wait,
  132. obj->resv, NULL,
  133. true, I915_FENCE_TIMEOUT,
  134. I915_FENCE_GFP);
  135. reservation_object_lock(obj->resv, NULL);
  136. reservation_object_add_excl_fence(obj->resv, &clflush->dma);
  137. reservation_object_unlock(obj->resv);
  138. i915_sw_fence_commit(&clflush->wait);
  139. } else if (obj->mm.pages) {
  140. __i915_do_clflush(obj);
  141. } else {
  142. GEM_BUG_ON(obj->write_domain != I915_GEM_DOMAIN_CPU);
  143. }
  144. obj->cache_dirty = false;
  145. return true;
  146. }