i915_gem_evict.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. static struct drm_i915_gem_object *
  33. i915_gem_next_active_object(struct drm_device *dev,
  34. struct list_head **render_iter,
  35. struct list_head **bsd_iter)
  36. {
  37. drm_i915_private_t *dev_priv = dev->dev_private;
  38. struct drm_i915_gem_object *render_obj = NULL, *bsd_obj = NULL;
  39. if (*render_iter != &dev_priv->render_ring.active_list)
  40. render_obj = list_entry(*render_iter,
  41. struct drm_i915_gem_object,
  42. list);
  43. if (HAS_BSD(dev)) {
  44. if (*bsd_iter != &dev_priv->bsd_ring.active_list)
  45. bsd_obj = list_entry(*bsd_iter,
  46. struct drm_i915_gem_object,
  47. list);
  48. if (render_obj == NULL) {
  49. *bsd_iter = (*bsd_iter)->next;
  50. return bsd_obj;
  51. }
  52. if (bsd_obj == NULL) {
  53. *render_iter = (*render_iter)->next;
  54. return render_obj;
  55. }
  56. /* XXX can we handle seqno wrapping? */
  57. if (render_obj->last_rendering_seqno < bsd_obj->last_rendering_seqno) {
  58. *render_iter = (*render_iter)->next;
  59. return render_obj;
  60. } else {
  61. *bsd_iter = (*bsd_iter)->next;
  62. return bsd_obj;
  63. }
  64. } else {
  65. *render_iter = (*render_iter)->next;
  66. return render_obj;
  67. }
  68. }
  69. static bool
  70. mark_free(struct drm_i915_gem_object *obj_priv,
  71. struct list_head *unwind)
  72. {
  73. list_add(&obj_priv->evict_list, unwind);
  74. return drm_mm_scan_add_block(obj_priv->gtt_space);
  75. }
  76. #define i915_for_each_active_object(OBJ, R, B) \
  77. *(R) = dev_priv->render_ring.active_list.next; \
  78. *(B) = dev_priv->bsd_ring.active_list.next; \
  79. while (((OBJ) = i915_gem_next_active_object(dev, (R), (B))) != NULL)
  80. int
  81. i915_gem_evict_something(struct drm_device *dev, int min_size, unsigned alignment)
  82. {
  83. drm_i915_private_t *dev_priv = dev->dev_private;
  84. struct list_head eviction_list, unwind_list;
  85. struct drm_i915_gem_object *obj_priv, *tmp_obj_priv;
  86. struct list_head *render_iter, *bsd_iter;
  87. int ret = 0;
  88. i915_gem_retire_requests(dev);
  89. /* Re-check for free space after retiring requests */
  90. if (drm_mm_search_free(&dev_priv->mm.gtt_space,
  91. min_size, alignment, 0))
  92. return 0;
  93. /*
  94. * The goal is to evict objects and amalgamate space in LRU order.
  95. * The oldest idle objects reside on the inactive list, which is in
  96. * retirement order. The next objects to retire are those on the (per
  97. * ring) active list that do not have an outstanding flush. Once the
  98. * hardware reports completion (the seqno is updated after the
  99. * batchbuffer has been finished) the clean buffer objects would
  100. * be retired to the inactive list. Any dirty objects would be added
  101. * to the tail of the flushing list. So after processing the clean
  102. * active objects we need to emit a MI_FLUSH to retire the flushing
  103. * list, hence the retirement order of the flushing list is in
  104. * advance of the dirty objects on the active lists.
  105. *
  106. * The retirement sequence is thus:
  107. * 1. Inactive objects (already retired)
  108. * 2. Clean active objects
  109. * 3. Flushing list
  110. * 4. Dirty active objects.
  111. *
  112. * On each list, the oldest objects lie at the HEAD with the freshest
  113. * object on the TAIL.
  114. */
  115. INIT_LIST_HEAD(&unwind_list);
  116. drm_mm_init_scan(&dev_priv->mm.gtt_space, min_size, alignment);
  117. /* First see if there is a large enough contiguous idle region... */
  118. list_for_each_entry(obj_priv, &dev_priv->mm.inactive_list, list) {
  119. if (mark_free(obj_priv, &unwind_list))
  120. goto found;
  121. }
  122. /* Now merge in the soon-to-be-expired objects... */
  123. i915_for_each_active_object(obj_priv, &render_iter, &bsd_iter) {
  124. /* Does the object require an outstanding flush? */
  125. if (obj_priv->base.write_domain || obj_priv->pin_count)
  126. continue;
  127. if (mark_free(obj_priv, &unwind_list))
  128. goto found;
  129. }
  130. /* Finally add anything with a pending flush (in order of retirement) */
  131. list_for_each_entry(obj_priv, &dev_priv->mm.flushing_list, list) {
  132. if (obj_priv->pin_count)
  133. continue;
  134. if (mark_free(obj_priv, &unwind_list))
  135. goto found;
  136. }
  137. i915_for_each_active_object(obj_priv, &render_iter, &bsd_iter) {
  138. if (! obj_priv->base.write_domain || obj_priv->pin_count)
  139. continue;
  140. if (mark_free(obj_priv, &unwind_list))
  141. goto found;
  142. }
  143. /* Nothing found, clean up and bail out! */
  144. list_for_each_entry(obj_priv, &unwind_list, evict_list) {
  145. ret = drm_mm_scan_remove_block(obj_priv->gtt_space);
  146. BUG_ON(ret);
  147. }
  148. /* We expect the caller to unpin, evict all and try again, or give up.
  149. * So calling i915_gem_evict_everything() is unnecessary.
  150. */
  151. return -ENOSPC;
  152. found:
  153. INIT_LIST_HEAD(&eviction_list);
  154. list_for_each_entry_safe(obj_priv, tmp_obj_priv,
  155. &unwind_list, evict_list) {
  156. if (drm_mm_scan_remove_block(obj_priv->gtt_space)) {
  157. /* drm_mm doesn't allow any other other operations while
  158. * scanning, therefore store to be evicted objects on a
  159. * temporary list. */
  160. list_move(&obj_priv->evict_list, &eviction_list);
  161. }
  162. }
  163. /* Unbinding will emit any required flushes */
  164. list_for_each_entry_safe(obj_priv, tmp_obj_priv,
  165. &eviction_list, evict_list) {
  166. #if WATCH_LRU
  167. DRM_INFO("%s: evicting %p\n", __func__, obj);
  168. #endif
  169. ret = i915_gem_object_unbind(&obj_priv->base);
  170. if (ret)
  171. return ret;
  172. }
  173. /* The just created free hole should be on the top of the free stack
  174. * maintained by drm_mm, so this BUG_ON actually executes in O(1).
  175. * Furthermore all accessed data has just recently been used, so it
  176. * should be really fast, too. */
  177. BUG_ON(!drm_mm_search_free(&dev_priv->mm.gtt_space, min_size,
  178. alignment, 0));
  179. return 0;
  180. }
  181. int
  182. i915_gem_evict_everything(struct drm_device *dev)
  183. {
  184. drm_i915_private_t *dev_priv = dev->dev_private;
  185. int ret;
  186. bool lists_empty;
  187. spin_lock(&dev_priv->mm.active_list_lock);
  188. lists_empty = (list_empty(&dev_priv->mm.inactive_list) &&
  189. list_empty(&dev_priv->mm.flushing_list) &&
  190. list_empty(&dev_priv->render_ring.active_list) &&
  191. (!HAS_BSD(dev)
  192. || list_empty(&dev_priv->bsd_ring.active_list)));
  193. spin_unlock(&dev_priv->mm.active_list_lock);
  194. if (lists_empty)
  195. return -ENOSPC;
  196. /* Flush everything (on to the inactive lists) and evict */
  197. ret = i915_gpu_idle(dev);
  198. if (ret)
  199. return ret;
  200. BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
  201. ret = i915_gem_evict_inactive(dev);
  202. if (ret)
  203. return ret;
  204. spin_lock(&dev_priv->mm.active_list_lock);
  205. lists_empty = (list_empty(&dev_priv->mm.inactive_list) &&
  206. list_empty(&dev_priv->mm.flushing_list) &&
  207. list_empty(&dev_priv->render_ring.active_list) &&
  208. (!HAS_BSD(dev)
  209. || list_empty(&dev_priv->bsd_ring.active_list)));
  210. spin_unlock(&dev_priv->mm.active_list_lock);
  211. BUG_ON(!lists_empty);
  212. return 0;
  213. }
  214. /** Unbinds all inactive objects. */
  215. int
  216. i915_gem_evict_inactive(struct drm_device *dev)
  217. {
  218. drm_i915_private_t *dev_priv = dev->dev_private;
  219. while (!list_empty(&dev_priv->mm.inactive_list)) {
  220. struct drm_gem_object *obj;
  221. int ret;
  222. obj = &list_first_entry(&dev_priv->mm.inactive_list,
  223. struct drm_i915_gem_object,
  224. list)->base;
  225. ret = i915_gem_object_unbind(obj);
  226. if (ret != 0) {
  227. DRM_ERROR("Error unbinding object: %d\n", ret);
  228. return ret;
  229. }
  230. }
  231. return 0;
  232. }