i915_gem_clflush.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. drm_clflush_sg(obj->mm.pages);
  63. obj->cache_dirty = false;
  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 (!obj->cache_dirty)
  71. goto out;
  72. if (i915_gem_object_pin_pages(obj)) {
  73. DRM_ERROR("Failed to acquire obj->pages for clflushing\n");
  74. goto out;
  75. }
  76. __i915_do_clflush(obj);
  77. i915_gem_object_unpin_pages(obj);
  78. out:
  79. i915_gem_object_put(obj);
  80. dma_fence_signal(&clflush->dma);
  81. dma_fence_put(&clflush->dma);
  82. }
  83. static int __i915_sw_fence_call
  84. i915_clflush_notify(struct i915_sw_fence *fence,
  85. enum i915_sw_fence_notify state)
  86. {
  87. struct clflush *clflush = container_of(fence, typeof(*clflush), wait);
  88. switch (state) {
  89. case FENCE_COMPLETE:
  90. schedule_work(&clflush->work);
  91. break;
  92. case FENCE_FREE:
  93. dma_fence_put(&clflush->dma);
  94. break;
  95. }
  96. return NOTIFY_DONE;
  97. }
  98. void i915_gem_clflush_object(struct drm_i915_gem_object *obj,
  99. unsigned int flags)
  100. {
  101. struct clflush *clflush;
  102. /*
  103. * Stolen memory is always coherent with the GPU as it is explicitly
  104. * marked as wc by the system, or the system is cache-coherent.
  105. * Similarly, we only access struct pages through the CPU cache, so
  106. * anything not backed by physical memory we consider to be always
  107. * coherent and not need clflushing.
  108. */
  109. if (!i915_gem_object_has_struct_page(obj))
  110. return;
  111. obj->cache_dirty = true;
  112. /* If the GPU is snooping the contents of the CPU cache,
  113. * we do not need to manually clear the CPU cache lines. However,
  114. * the caches are only snooped when the render cache is
  115. * flushed/invalidated. As we always have to emit invalidations
  116. * and flushes when moving into and out of the RENDER domain, correct
  117. * snooping behaviour occurs naturally as the result of our domain
  118. * tracking.
  119. */
  120. if (!(flags & I915_CLFLUSH_FORCE) && i915_gem_object_is_coherent(obj))
  121. return;
  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. dma_fence_init(&clflush->dma,
  128. &i915_clflush_ops,
  129. &clflush_lock,
  130. to_i915(obj->base.dev)->mm.unordered_timeline,
  131. 0);
  132. i915_sw_fence_init(&clflush->wait, i915_clflush_notify);
  133. clflush->obj = i915_gem_object_get(obj);
  134. INIT_WORK(&clflush->work, i915_clflush_work);
  135. dma_fence_get(&clflush->dma);
  136. i915_sw_fence_await_reservation(&clflush->wait,
  137. obj->resv, NULL,
  138. true, I915_FENCE_TIMEOUT,
  139. GFP_KERNEL);
  140. reservation_object_lock(obj->resv, NULL);
  141. reservation_object_add_excl_fence(obj->resv, &clflush->dma);
  142. reservation_object_unlock(obj->resv);
  143. i915_sw_fence_commit(&clflush->wait);
  144. } else if (obj->mm.pages) {
  145. __i915_do_clflush(obj);
  146. } else {
  147. GEM_BUG_ON(obj->base.write_domain != I915_GEM_DOMAIN_CPU);
  148. }
  149. }