i915_gem_clflush.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. static u64 clflush_context;
  29. struct clflush {
  30. struct dma_fence dma; /* Must be first for dma_fence_free() */
  31. struct i915_sw_fence wait;
  32. struct work_struct work;
  33. struct drm_i915_gem_object *obj;
  34. };
  35. static const char *i915_clflush_get_driver_name(struct dma_fence *fence)
  36. {
  37. return DRIVER_NAME;
  38. }
  39. static const char *i915_clflush_get_timeline_name(struct dma_fence *fence)
  40. {
  41. return "clflush";
  42. }
  43. static bool i915_clflush_enable_signaling(struct dma_fence *fence)
  44. {
  45. return true;
  46. }
  47. static void i915_clflush_release(struct dma_fence *fence)
  48. {
  49. struct clflush *clflush = container_of(fence, typeof(*clflush), dma);
  50. i915_sw_fence_fini(&clflush->wait);
  51. BUILD_BUG_ON(offsetof(typeof(*clflush), dma));
  52. dma_fence_free(&clflush->dma);
  53. }
  54. static const struct dma_fence_ops i915_clflush_ops = {
  55. .get_driver_name = i915_clflush_get_driver_name,
  56. .get_timeline_name = i915_clflush_get_timeline_name,
  57. .enable_signaling = i915_clflush_enable_signaling,
  58. .wait = dma_fence_default_wait,
  59. .release = i915_clflush_release,
  60. };
  61. static void __i915_do_clflush(struct drm_i915_gem_object *obj)
  62. {
  63. drm_clflush_sg(obj->mm.pages);
  64. obj->cache_dirty = false;
  65. intel_fb_obj_flush(obj, ORIGIN_CPU);
  66. }
  67. static void i915_clflush_work(struct work_struct *work)
  68. {
  69. struct clflush *clflush = container_of(work, typeof(*clflush), work);
  70. struct drm_i915_gem_object *obj = clflush->obj;
  71. if (!obj->cache_dirty)
  72. goto out;
  73. if (i915_gem_object_pin_pages(obj)) {
  74. DRM_ERROR("Failed to acquire obj->pages for clflushing\n");
  75. goto out;
  76. }
  77. __i915_do_clflush(obj);
  78. i915_gem_object_unpin_pages(obj);
  79. out:
  80. i915_gem_object_put(obj);
  81. dma_fence_signal(&clflush->dma);
  82. dma_fence_put(&clflush->dma);
  83. }
  84. static int __i915_sw_fence_call
  85. i915_clflush_notify(struct i915_sw_fence *fence,
  86. enum i915_sw_fence_notify state)
  87. {
  88. struct clflush *clflush = container_of(fence, typeof(*clflush), wait);
  89. switch (state) {
  90. case FENCE_COMPLETE:
  91. schedule_work(&clflush->work);
  92. break;
  93. case FENCE_FREE:
  94. dma_fence_put(&clflush->dma);
  95. break;
  96. }
  97. return NOTIFY_DONE;
  98. }
  99. void i915_gem_clflush_object(struct drm_i915_gem_object *obj,
  100. unsigned int flags)
  101. {
  102. struct clflush *clflush;
  103. /*
  104. * Stolen memory is always coherent with the GPU as it is explicitly
  105. * marked as wc by the system, or the system is cache-coherent.
  106. * Similarly, we only access struct pages through the CPU cache, so
  107. * anything not backed by physical memory we consider to be always
  108. * coherent and not need clflushing.
  109. */
  110. if (!i915_gem_object_has_struct_page(obj))
  111. return;
  112. obj->cache_dirty = true;
  113. /* If the GPU is snooping the contents of the CPU cache,
  114. * we do not need to manually clear the CPU cache lines. However,
  115. * the caches are only snooped when the render cache is
  116. * flushed/invalidated. As we always have to emit invalidations
  117. * and flushes when moving into and out of the RENDER domain, correct
  118. * snooping behaviour occurs naturally as the result of our domain
  119. * tracking.
  120. */
  121. if (!(flags & I915_CLFLUSH_FORCE) && i915_gem_object_is_coherent(obj))
  122. return;
  123. trace_i915_gem_object_clflush(obj);
  124. clflush = NULL;
  125. if (!(flags & I915_CLFLUSH_SYNC))
  126. clflush = kmalloc(sizeof(*clflush), GFP_KERNEL);
  127. if (clflush) {
  128. dma_fence_init(&clflush->dma,
  129. &i915_clflush_ops,
  130. &clflush_lock,
  131. clflush_context,
  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. GFP_KERNEL);
  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. }
  151. void i915_gem_clflush_init(struct drm_i915_private *i915)
  152. {
  153. clflush_context = dma_fence_context_alloc(1);
  154. }