i915_gem_clflush.c 5.2 KB

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