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