Path: blob/master/drivers/gpu/drm/i915/i915_gem_evict.c
15113 views
/*1* Copyright © 2008-2010 Intel Corporation2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20* IN THE SOFTWARE.21*22* Authors:23* Eric Anholt <[email protected]>24* Chris Wilson <[email protected]>25*26*/2728#include "drmP.h"29#include "drm.h"30#include "i915_drv.h"31#include "i915_drm.h"32#include "i915_trace.h"3334static bool35mark_free(struct drm_i915_gem_object *obj, struct list_head *unwind)36{37list_add(&obj->exec_list, unwind);38drm_gem_object_reference(&obj->base);39return drm_mm_scan_add_block(obj->gtt_space);40}4142int43i915_gem_evict_something(struct drm_device *dev, int min_size,44unsigned alignment, bool mappable)45{46drm_i915_private_t *dev_priv = dev->dev_private;47struct list_head eviction_list, unwind_list;48struct drm_i915_gem_object *obj;49int ret = 0;5051i915_gem_retire_requests(dev);5253/* Re-check for free space after retiring requests */54if (mappable) {55if (drm_mm_search_free_in_range(&dev_priv->mm.gtt_space,56min_size, alignment, 0,57dev_priv->mm.gtt_mappable_end,580))59return 0;60} else {61if (drm_mm_search_free(&dev_priv->mm.gtt_space,62min_size, alignment, 0))63return 0;64}6566trace_i915_gem_evict(dev, min_size, alignment, mappable);6768/*69* The goal is to evict objects and amalgamate space in LRU order.70* The oldest idle objects reside on the inactive list, which is in71* retirement order. The next objects to retire are those on the (per72* ring) active list that do not have an outstanding flush. Once the73* hardware reports completion (the seqno is updated after the74* batchbuffer has been finished) the clean buffer objects would75* be retired to the inactive list. Any dirty objects would be added76* to the tail of the flushing list. So after processing the clean77* active objects we need to emit a MI_FLUSH to retire the flushing78* list, hence the retirement order of the flushing list is in79* advance of the dirty objects on the active lists.80*81* The retirement sequence is thus:82* 1. Inactive objects (already retired)83* 2. Clean active objects84* 3. Flushing list85* 4. Dirty active objects.86*87* On each list, the oldest objects lie at the HEAD with the freshest88* object on the TAIL.89*/9091INIT_LIST_HEAD(&unwind_list);92if (mappable)93drm_mm_init_scan_with_range(&dev_priv->mm.gtt_space, min_size,94alignment, 0,95dev_priv->mm.gtt_mappable_end);96else97drm_mm_init_scan(&dev_priv->mm.gtt_space, min_size, alignment);9899/* First see if there is a large enough contiguous idle region... */100list_for_each_entry(obj, &dev_priv->mm.inactive_list, mm_list) {101if (mark_free(obj, &unwind_list))102goto found;103}104105/* Now merge in the soon-to-be-expired objects... */106list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list) {107/* Does the object require an outstanding flush? */108if (obj->base.write_domain || obj->pin_count)109continue;110111if (mark_free(obj, &unwind_list))112goto found;113}114115/* Finally add anything with a pending flush (in order of retirement) */116list_for_each_entry(obj, &dev_priv->mm.flushing_list, mm_list) {117if (obj->pin_count)118continue;119120if (mark_free(obj, &unwind_list))121goto found;122}123list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list) {124if (! obj->base.write_domain || obj->pin_count)125continue;126127if (mark_free(obj, &unwind_list))128goto found;129}130131/* Nothing found, clean up and bail out! */132while (!list_empty(&unwind_list)) {133obj = list_first_entry(&unwind_list,134struct drm_i915_gem_object,135exec_list);136137ret = drm_mm_scan_remove_block(obj->gtt_space);138BUG_ON(ret);139140list_del_init(&obj->exec_list);141drm_gem_object_unreference(&obj->base);142}143144/* We expect the caller to unpin, evict all and try again, or give up.145* So calling i915_gem_evict_everything() is unnecessary.146*/147return -ENOSPC;148149found:150/* drm_mm doesn't allow any other other operations while151* scanning, therefore store to be evicted objects on a152* temporary list. */153INIT_LIST_HEAD(&eviction_list);154while (!list_empty(&unwind_list)) {155obj = list_first_entry(&unwind_list,156struct drm_i915_gem_object,157exec_list);158if (drm_mm_scan_remove_block(obj->gtt_space)) {159list_move(&obj->exec_list, &eviction_list);160continue;161}162list_del_init(&obj->exec_list);163drm_gem_object_unreference(&obj->base);164}165166/* Unbinding will emit any required flushes */167while (!list_empty(&eviction_list)) {168obj = list_first_entry(&eviction_list,169struct drm_i915_gem_object,170exec_list);171if (ret == 0)172ret = i915_gem_object_unbind(obj);173174list_del_init(&obj->exec_list);175drm_gem_object_unreference(&obj->base);176}177178return ret;179}180181int182i915_gem_evict_everything(struct drm_device *dev, bool purgeable_only)183{184drm_i915_private_t *dev_priv = dev->dev_private;185int ret;186bool lists_empty;187188lists_empty = (list_empty(&dev_priv->mm.inactive_list) &&189list_empty(&dev_priv->mm.flushing_list) &&190list_empty(&dev_priv->mm.active_list));191if (lists_empty)192return -ENOSPC;193194trace_i915_gem_evict_everything(dev, purgeable_only);195196/* Flush everything (on to the inactive lists) and evict */197ret = i915_gpu_idle(dev);198if (ret)199return ret;200201BUG_ON(!list_empty(&dev_priv->mm.flushing_list));202203return i915_gem_evict_inactive(dev, purgeable_only);204}205206/** Unbinds all inactive objects. */207int208i915_gem_evict_inactive(struct drm_device *dev, bool purgeable_only)209{210drm_i915_private_t *dev_priv = dev->dev_private;211struct drm_i915_gem_object *obj, *next;212213list_for_each_entry_safe(obj, next,214&dev_priv->mm.inactive_list, mm_list) {215if (!purgeable_only || obj->madv != I915_MADV_WILLNEED) {216int ret = i915_gem_object_unbind(obj);217if (ret)218return ret;219}220}221222return 0;223}224225226