1 // SPDX-License-Identifier: MIT
3 * Copyright © 2022 Intel Corporation
6 #include "xe_bo_evict.h"
14 * xe_bo_evict_all - evict all BOs from VRAM
18 * Evict non-pinned user BOs first (via GPU), evict pinned external BOs next
19 * (via GPU), wait for evictions, and finally evict pinned kernel BOs via CPU.
20 * All eviction magic done via TTM calls.
22 * Evict == move VRAM BOs to temporary (typically system) memory.
24 * This function should be called before the device goes into a suspend state
25 * where the VRAM loses power.
27 int xe_bo_evict_all(struct xe_device *xe)
29 struct ttm_device *bdev = &xe->ttm;
32 struct list_head still_in_list;
41 for (mem_type = XE_PL_VRAM0; mem_type <= XE_PL_VRAM1; ++mem_type) {
42 struct ttm_resource_manager *man =
43 ttm_manager_type(bdev, mem_type);
46 ret = ttm_resource_manager_evict_all(bdev, man);
52 /* Pinned user memory in VRAM */
53 INIT_LIST_HEAD(&still_in_list);
54 spin_lock(&xe->pinned.lock);
56 bo = list_first_entry_or_null(&xe->pinned.external_vram,
57 typeof(*bo), pinned_link);
61 list_move_tail(&bo->pinned_link, &still_in_list);
62 spin_unlock(&xe->pinned.lock);
64 xe_bo_lock(bo, false);
65 ret = xe_bo_evict_pinned(bo);
69 spin_lock(&xe->pinned.lock);
70 list_splice_tail(&still_in_list,
71 &xe->pinned.external_vram);
72 spin_unlock(&xe->pinned.lock);
76 spin_lock(&xe->pinned.lock);
78 list_splice_tail(&still_in_list, &xe->pinned.external_vram);
79 spin_unlock(&xe->pinned.lock);
82 * Wait for all user BO to be evicted as those evictions depend on the
85 for_each_tile(tile, xe, id)
86 xe_tile_migrate_wait(tile);
88 spin_lock(&xe->pinned.lock);
90 bo = list_first_entry_or_null(&xe->pinned.kernel_bo_present,
91 typeof(*bo), pinned_link);
95 list_move_tail(&bo->pinned_link, &xe->pinned.evicted);
96 spin_unlock(&xe->pinned.lock);
98 xe_bo_lock(bo, false);
99 ret = xe_bo_evict_pinned(bo);
105 spin_lock(&xe->pinned.lock);
107 spin_unlock(&xe->pinned.lock);
113 * xe_bo_restore_kernel - restore kernel BOs to VRAM
117 * Move kernel BOs from temporary (typically system) memory to VRAM via CPU. All
118 * moves done via TTM calls.
120 * This function should be called early, before trying to init the GT, on device
123 int xe_bo_restore_kernel(struct xe_device *xe)
131 spin_lock(&xe->pinned.lock);
133 bo = list_first_entry_or_null(&xe->pinned.evicted,
134 typeof(*bo), pinned_link);
138 list_move_tail(&bo->pinned_link, &xe->pinned.kernel_bo_present);
139 spin_unlock(&xe->pinned.lock);
141 xe_bo_lock(bo, false);
142 ret = xe_bo_restore_pinned(bo);
149 if (bo->flags & XE_BO_CREATE_GGTT_BIT) {
150 struct xe_tile *tile = bo->tile;
152 mutex_lock(&tile->mem.ggtt->lock);
153 xe_ggtt_map_bo(tile->mem.ggtt, bo);
154 mutex_unlock(&tile->mem.ggtt->lock);
158 * We expect validate to trigger a move VRAM and our move code
159 * should setup the iosys map.
161 xe_assert(xe, !iosys_map_is_null(&bo->vmap));
162 xe_assert(xe, xe_bo_is_vram(bo));
166 spin_lock(&xe->pinned.lock);
168 spin_unlock(&xe->pinned.lock);
174 * xe_bo_restore_user - restore pinned user BOs to VRAM
178 * Move pinned user BOs from temporary (typically system) memory to VRAM via
179 * CPU. All moves done via TTM calls.
181 * This function should be called late, after GT init, on device resume.
183 int xe_bo_restore_user(struct xe_device *xe)
186 struct xe_tile *tile;
187 struct list_head still_in_list;
194 /* Pinned user memory in VRAM should be validated on resume */
195 INIT_LIST_HEAD(&still_in_list);
196 spin_lock(&xe->pinned.lock);
198 bo = list_first_entry_or_null(&xe->pinned.external_vram,
199 typeof(*bo), pinned_link);
202 list_move_tail(&bo->pinned_link, &still_in_list);
204 spin_unlock(&xe->pinned.lock);
206 xe_bo_lock(bo, false);
207 ret = xe_bo_restore_pinned(bo);
211 spin_lock(&xe->pinned.lock);
212 list_splice_tail(&still_in_list,
213 &xe->pinned.external_vram);
214 spin_unlock(&xe->pinned.lock);
218 spin_lock(&xe->pinned.lock);
220 list_splice_tail(&still_in_list, &xe->pinned.external_vram);
221 spin_unlock(&xe->pinned.lock);
223 /* Wait for validate to complete */
224 for_each_tile(tile, xe, id)
225 xe_tile_migrate_wait(tile);