1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29#include <drm/drmP.h>
30#include <drm/i915_drm.h>
31
32#include "i915_drv.h"
33#include "intel_drv.h"
34#include "i915_trace.h"
35
36static int switch_to_pinned_context(struct drm_i915_private *dev_priv)
37{
38 struct intel_engine_cs *engine;
39
40 if (i915.enable_execlists)
41 return 0;
42
43 for_each_engine(engine, dev_priv) {
44 struct drm_i915_gem_request *req;
45 int ret;
46
47 if (engine->last_context == NULL)
48 continue;
49
50 if (engine->last_context == dev_priv->kernel_context)
51 continue;
52
53 req = i915_gem_request_alloc(engine, dev_priv->kernel_context);
54 if (IS_ERR(req))
55 return PTR_ERR(req);
56
57 ret = i915_switch_context(req);
58 i915_add_request_no_flush(req);
59 if (ret)
60 return ret;
61 }
62
63 return 0;
64}
65
66
67static bool
68mark_free(struct i915_vma *vma, struct list_head *unwind)
69{
70 if (vma->pin_count)
71 return false;
72
73 if (WARN_ON(!list_empty(&vma->exec_list)))
74 return false;
75
76 list_add(&vma->exec_list, unwind);
77 return drm_mm_scan_add_block(&vma->node);
78}
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104int
105i915_gem_evict_something(struct drm_device *dev, struct i915_address_space *vm,
106 int min_size, unsigned alignment, unsigned cache_level,
107 unsigned long start, unsigned long end,
108 unsigned flags)
109{
110 struct list_head eviction_list, unwind_list;
111 struct i915_vma *vma;
112 int ret = 0;
113 int pass = 0;
114
115 trace_i915_gem_evict(dev, min_size, alignment, flags);
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140 INIT_LIST_HEAD(&unwind_list);
141 if (start != 0 || end != vm->total) {
142 drm_mm_init_scan_with_range(&vm->mm, min_size,
143 alignment, cache_level,
144 start, end);
145 } else
146 drm_mm_init_scan(&vm->mm, min_size, alignment, cache_level);
147
148search_again:
149
150 list_for_each_entry(vma, &vm->inactive_list, vm_link) {
151 if (mark_free(vma, &unwind_list))
152 goto found;
153 }
154
155 if (flags & PIN_NONBLOCK)
156 goto none;
157
158
159 list_for_each_entry(vma, &vm->active_list, vm_link) {
160 if (mark_free(vma, &unwind_list))
161 goto found;
162 }
163
164none:
165
166 while (!list_empty(&unwind_list)) {
167 vma = list_first_entry(&unwind_list,
168 struct i915_vma,
169 exec_list);
170 ret = drm_mm_scan_remove_block(&vma->node);
171 BUG_ON(ret);
172
173 list_del_init(&vma->exec_list);
174 }
175
176
177
178
179 if (flags & PIN_NONBLOCK)
180 return -ENOSPC;
181
182
183 if (pass++ == 0) {
184 struct drm_i915_private *dev_priv = to_i915(dev);
185
186 if (i915_is_ggtt(vm)) {
187 ret = switch_to_pinned_context(dev_priv);
188 if (ret)
189 return ret;
190 }
191
192 ret = i915_gem_wait_for_idle(dev_priv);
193 if (ret)
194 return ret;
195
196 i915_gem_retire_requests(dev_priv);
197 goto search_again;
198 }
199
200
201
202
203
204 return intel_has_pending_fb_unpin(dev) ? -EAGAIN : -ENOSPC;
205
206found:
207
208
209
210 INIT_LIST_HEAD(&eviction_list);
211 while (!list_empty(&unwind_list)) {
212 vma = list_first_entry(&unwind_list,
213 struct i915_vma,
214 exec_list);
215 if (drm_mm_scan_remove_block(&vma->node)) {
216 list_move(&vma->exec_list, &eviction_list);
217 drm_gem_object_reference(&vma->obj->base);
218 continue;
219 }
220 list_del_init(&vma->exec_list);
221 }
222
223
224 while (!list_empty(&eviction_list)) {
225 struct drm_gem_object *obj;
226 vma = list_first_entry(&eviction_list,
227 struct i915_vma,
228 exec_list);
229
230 obj = &vma->obj->base;
231 list_del_init(&vma->exec_list);
232 if (ret == 0)
233 ret = i915_vma_unbind(vma);
234
235 drm_gem_object_unreference(obj);
236 }
237
238 return ret;
239}
240
241int
242i915_gem_evict_for_vma(struct i915_vma *target)
243{
244 struct drm_mm_node *node, *next;
245
246 list_for_each_entry_safe(node, next,
247 &target->vm->mm.head_node.node_list,
248 node_list) {
249 struct i915_vma *vma;
250 int ret;
251
252 if (node->start + node->size <= target->node.start)
253 continue;
254 if (node->start >= target->node.start + target->node.size)
255 break;
256
257 vma = container_of(node, typeof(*vma), node);
258
259 if (vma->pin_count) {
260 if (!vma->exec_entry || (vma->pin_count > 1))
261
262 return -EBUSY;
263
264
265 if (vma->exec_entry->flags & EXEC_OBJECT_PINNED)
266
267 return -EINVAL;
268
269 return -ENOSPC;
270 }
271
272 ret = i915_vma_unbind(vma);
273 if (ret)
274 return ret;
275 }
276
277 return 0;
278}
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294int i915_gem_evict_vm(struct i915_address_space *vm, bool do_idle)
295{
296 struct i915_vma *vma, *next;
297 int ret;
298
299 WARN_ON(!mutex_is_locked(&vm->dev->struct_mutex));
300 trace_i915_gem_evict_vm(vm);
301
302 if (do_idle) {
303 struct drm_i915_private *dev_priv = to_i915(vm->dev);
304
305 if (i915_is_ggtt(vm)) {
306 ret = switch_to_pinned_context(dev_priv);
307 if (ret)
308 return ret;
309 }
310
311 ret = i915_gem_wait_for_idle(dev_priv);
312 if (ret)
313 return ret;
314
315 i915_gem_retire_requests(dev_priv);
316
317 WARN_ON(!list_empty(&vm->active_list));
318 }
319
320 list_for_each_entry_safe(vma, next, &vm->inactive_list, vm_link)
321 if (vma->pin_count == 0)
322 WARN_ON(i915_vma_unbind(vma));
323
324 return 0;
325}
326