i915_gem_evict.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * Copyright © 2008-2010 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. * Eric Anholt <eric@anholt.net>
  25. * Chris Wilson <chris@chris-wilson.co.uuk>
  26. *
  27. */
  28. #include "drmP.h"
  29. #include "drm.h"
  30. #include "i915_drv.h"
  31. #include "i915_drm.h"
  32. #include "i915_trace.h"
  33. static bool
  34. mark_free(struct drm_i915_gem_object *obj, struct list_head *unwind)
  35. {
  36. list_add(&obj->exec_list, unwind);
  37. drm_gem_object_reference(&obj->base);
  38. return drm_mm_scan_add_block(obj->gtt_space);
  39. }
  40. int
  41. i915_gem_evict_something(struct drm_device *dev, int min_size,
  42. unsigned alignment, bool mappable)
  43. {
  44. drm_i915_private_t *dev_priv = dev->dev_private;
  45. struct list_head eviction_list, unwind_list;
  46. struct drm_i915_gem_object *obj;
  47. int ret = 0;
  48. i915_gem_retire_requests(dev);
  49. /* Re-check for free space after retiring requests */
  50. if (mappable) {
  51. if (drm_mm_search_free_in_range(&dev_priv->mm.gtt_space,
  52. min_size, alignment, 0,
  53. dev_priv->mm.gtt_mappable_end,
  54. 0))
  55. return 0;
  56. } else {
  57. if (drm_mm_search_free(&dev_priv->mm.gtt_space,
  58. min_size, alignment, 0))
  59. return 0;
  60. }
  61. trace_i915_gem_evict(dev, min_size, alignment, mappable);
  62. /*
  63. * The goal is to evict objects and amalgamate space in LRU order.
  64. * The oldest idle objects reside on the inactive list, which is in
  65. * retirement order. The next objects to retire are those on the (per
  66. * ring) active list that do not have an outstanding flush. Once the
  67. * hardware reports completion (the seqno is updated after the
  68. * batchbuffer has been finished) the clean buffer objects would
  69. * be retired to the inactive list. Any dirty objects would be added
  70. * to the tail of the flushing list. So after processing the clean
  71. * active objects we need to emit a MI_FLUSH to retire the flushing
  72. * list, hence the retirement order of the flushing list is in
  73. * advance of the dirty objects on the active lists.
  74. *
  75. * The retirement sequence is thus:
  76. * 1. Inactive objects (already retired)
  77. * 2. Clean active objects
  78. * 3. Flushing list
  79. * 4. Dirty active objects.
  80. *
  81. * On each list, the oldest objects lie at the HEAD with the freshest
  82. * object on the TAIL.
  83. */
  84. INIT_LIST_HEAD(&unwind_list);
  85. if (mappable)
  86. drm_mm_init_scan_with_range(&dev_priv->mm.gtt_space, min_size,
  87. alignment, 0,
  88. dev_priv->mm.gtt_mappable_end);
  89. else
  90. drm_mm_init_scan(&dev_priv->mm.gtt_space, min_size, alignment);
  91. /* First see if there is a large enough contiguous idle region... */
  92. list_for_each_entry(obj, &dev_priv->mm.inactive_list, mm_list) {
  93. if (mark_free(obj, &unwind_list))
  94. goto found;
  95. }
  96. /* Now merge in the soon-to-be-expired objects... */
  97. list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list) {
  98. /* Does the object require an outstanding flush? */
  99. if (obj->base.write_domain || obj->pin_count)
  100. continue;
  101. if (mark_free(obj, &unwind_list))
  102. goto found;
  103. }
  104. /* Finally add anything with a pending flush (in order of retirement) */
  105. list_for_each_entry(obj, &dev_priv->mm.flushing_list, mm_list) {
  106. if (obj->pin_count)
  107. continue;
  108. if (mark_free(obj, &unwind_list))
  109. goto found;
  110. }
  111. list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list) {
  112. if (! obj->base.write_domain || obj->pin_count)
  113. continue;
  114. if (mark_free(obj, &unwind_list))
  115. goto found;
  116. }
  117. /* Nothing found, clean up and bail out! */
  118. while (!list_empty(&unwind_list)) {
  119. obj = list_first_entry(&unwind_list,
  120. struct drm_i915_gem_object,
  121. exec_list);
  122. ret = drm_mm_scan_remove_block(obj->gtt_space);
  123. BUG_ON(ret);
  124. list_del_init(&obj->exec_list);
  125. drm_gem_object_unreference(&obj->base);
  126. }
  127. /* We expect the caller to unpin, evict all and try again, or give up.
  128. * So calling i915_gem_evict_everything() is unnecessary.
  129. */
  130. return -ENOSPC;
  131. found:
  132. /* drm_mm doesn't allow any other other operations while
  133. * scanning, therefore store to be evicted objects on a
  134. * temporary list. */
  135. INIT_LIST_HEAD(&eviction_list);
  136. while (!list_empty(&unwind_list)) {
  137. obj = list_first_entry(&unwind_list,
  138. struct drm_i915_gem_object,
  139. exec_list);
  140. if (drm_mm_scan_remove_block(obj->gtt_space)) {
  141. list_move(&obj->exec_list, &eviction_list);
  142. continue;
  143. }
  144. list_del_init(&obj->exec_list);
  145. drm_gem_object_unreference(&obj->base);
  146. }
  147. /* Unbinding will emit any required flushes */
  148. while (!list_empty(&eviction_list)) {
  149. obj = list_first_entry(&eviction_list,
  150. struct drm_i915_gem_object,
  151. exec_list);
  152. if (ret == 0)
  153. ret = i915_gem_object_unbind(obj);
  154. list_del_init(&obj->exec_list);
  155. drm_gem_object_unreference(&obj->base);
  156. }
  157. return ret;
  158. }
  159. int
  160. i915_gem_evict_everything(struct drm_device *dev, bool purgeable_only)
  161. {
  162. drm_i915_private_t *dev_priv = dev->dev_private;
  163. int ret;
  164. bool lists_empty;
  165. lists_empty = (list_empty(&dev_priv->mm.inactive_list) &&
  166. list_empty(&dev_priv->mm.flushing_list) &&
  167. list_empty(&dev_priv->mm.active_list));
  168. if (lists_empty)
  169. return -ENOSPC;
  170. trace_i915_gem_evict_everything(dev, purgeable_only);
  171. /* Flush everything (on to the inactive lists) and evict */
  172. ret = i915_gpu_idle(dev);
  173. if (ret)
  174. return ret;
  175. BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
  176. return i915_gem_evict_inactive(dev, purgeable_only);
  177. }
  178. /** Unbinds all inactive objects. */
  179. int
  180. i915_gem_evict_inactive(struct drm_device *dev, bool purgeable_only)
  181. {
  182. drm_i915_private_t *dev_priv = dev->dev_private;
  183. struct drm_i915_gem_object *obj, *next;
  184. list_for_each_entry_safe(obj, next,
  185. &dev_priv->mm.inactive_list, mm_list) {
  186. if (!purgeable_only || obj->madv != I915_MADV_WILLNEED) {
  187. int ret = i915_gem_object_unbind(obj);
  188. if (ret)
  189. return ret;
  190. }
  191. }
  192. return 0;
  193. }