]>
Commit | Line | Data |
---|---|---|
673a394b | 1 | /* |
be6a0376 | 2 | * Copyright © 2008-2015 Intel Corporation |
673a394b EA |
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 | * | |
26 | */ | |
27 | ||
760285e7 | 28 | #include <drm/drmP.h> |
0de23977 | 29 | #include <drm/drm_vma_manager.h> |
760285e7 | 30 | #include <drm/i915_drm.h> |
673a394b | 31 | #include "i915_drv.h" |
c13d87ea | 32 | #include "i915_gem_dmabuf.h" |
eb82289a | 33 | #include "i915_vgpu.h" |
1c5d22f7 | 34 | #include "i915_trace.h" |
652c393a | 35 | #include "intel_drv.h" |
5d723d7a | 36 | #include "intel_frontbuffer.h" |
0ccdacf6 | 37 | #include "intel_mocs.h" |
c13d87ea | 38 | #include <linux/reservation.h> |
5949eac4 | 39 | #include <linux/shmem_fs.h> |
5a0e3ad6 | 40 | #include <linux/slab.h> |
673a394b | 41 | #include <linux/swap.h> |
79e53945 | 42 | #include <linux/pci.h> |
1286ff73 | 43 | #include <linux/dma-buf.h> |
673a394b | 44 | |
05394f39 | 45 | static void i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj); |
e62b59e4 | 46 | static void i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj); |
61050808 | 47 | |
c76ce038 CW |
48 | static bool cpu_cache_is_coherent(struct drm_device *dev, |
49 | enum i915_cache_level level) | |
50 | { | |
51 | return HAS_LLC(dev) || level != I915_CACHE_NONE; | |
52 | } | |
53 | ||
2c22569b CW |
54 | static bool cpu_write_needs_clflush(struct drm_i915_gem_object *obj) |
55 | { | |
b50a5371 AS |
56 | if (obj->base.write_domain == I915_GEM_DOMAIN_CPU) |
57 | return false; | |
58 | ||
2c22569b CW |
59 | if (!cpu_cache_is_coherent(obj->base.dev, obj->cache_level)) |
60 | return true; | |
61 | ||
62 | return obj->pin_display; | |
63 | } | |
64 | ||
4f1959ee AS |
65 | static int |
66 | insert_mappable_node(struct drm_i915_private *i915, | |
67 | struct drm_mm_node *node, u32 size) | |
68 | { | |
69 | memset(node, 0, sizeof(*node)); | |
70 | return drm_mm_insert_node_in_range_generic(&i915->ggtt.base.mm, node, | |
71 | size, 0, 0, 0, | |
72 | i915->ggtt.mappable_end, | |
73 | DRM_MM_SEARCH_DEFAULT, | |
74 | DRM_MM_CREATE_DEFAULT); | |
75 | } | |
76 | ||
77 | static void | |
78 | remove_mappable_node(struct drm_mm_node *node) | |
79 | { | |
80 | drm_mm_remove_node(node); | |
81 | } | |
82 | ||
73aa808f CW |
83 | /* some bookkeeping */ |
84 | static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv, | |
85 | size_t size) | |
86 | { | |
c20e8355 | 87 | spin_lock(&dev_priv->mm.object_stat_lock); |
73aa808f CW |
88 | dev_priv->mm.object_count++; |
89 | dev_priv->mm.object_memory += size; | |
c20e8355 | 90 | spin_unlock(&dev_priv->mm.object_stat_lock); |
73aa808f CW |
91 | } |
92 | ||
93 | static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv, | |
94 | size_t size) | |
95 | { | |
c20e8355 | 96 | spin_lock(&dev_priv->mm.object_stat_lock); |
73aa808f CW |
97 | dev_priv->mm.object_count--; |
98 | dev_priv->mm.object_memory -= size; | |
c20e8355 | 99 | spin_unlock(&dev_priv->mm.object_stat_lock); |
73aa808f CW |
100 | } |
101 | ||
21dd3734 | 102 | static int |
33196ded | 103 | i915_gem_wait_for_error(struct i915_gpu_error *error) |
30dbf0c0 | 104 | { |
30dbf0c0 CW |
105 | int ret; |
106 | ||
d98c52cf | 107 | if (!i915_reset_in_progress(error)) |
30dbf0c0 CW |
108 | return 0; |
109 | ||
0a6759c6 DV |
110 | /* |
111 | * Only wait 10 seconds for the gpu reset to complete to avoid hanging | |
112 | * userspace. If it takes that long something really bad is going on and | |
113 | * we should simply try to bail out and fail as gracefully as possible. | |
114 | */ | |
1f83fee0 | 115 | ret = wait_event_interruptible_timeout(error->reset_queue, |
d98c52cf | 116 | !i915_reset_in_progress(error), |
1f83fee0 | 117 | 10*HZ); |
0a6759c6 DV |
118 | if (ret == 0) { |
119 | DRM_ERROR("Timed out waiting for the gpu reset to complete\n"); | |
120 | return -EIO; | |
121 | } else if (ret < 0) { | |
30dbf0c0 | 122 | return ret; |
d98c52cf CW |
123 | } else { |
124 | return 0; | |
0a6759c6 | 125 | } |
30dbf0c0 CW |
126 | } |
127 | ||
54cf91dc | 128 | int i915_mutex_lock_interruptible(struct drm_device *dev) |
76c1dec1 | 129 | { |
fac5e23e | 130 | struct drm_i915_private *dev_priv = to_i915(dev); |
76c1dec1 CW |
131 | int ret; |
132 | ||
33196ded | 133 | ret = i915_gem_wait_for_error(&dev_priv->gpu_error); |
76c1dec1 CW |
134 | if (ret) |
135 | return ret; | |
136 | ||
137 | ret = mutex_lock_interruptible(&dev->struct_mutex); | |
138 | if (ret) | |
139 | return ret; | |
140 | ||
76c1dec1 CW |
141 | return 0; |
142 | } | |
30dbf0c0 | 143 | |
5a125c3c EA |
144 | int |
145 | i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data, | |
05394f39 | 146 | struct drm_file *file) |
5a125c3c | 147 | { |
72e96d64 | 148 | struct drm_i915_private *dev_priv = to_i915(dev); |
62106b4f | 149 | struct i915_ggtt *ggtt = &dev_priv->ggtt; |
72e96d64 | 150 | struct drm_i915_gem_get_aperture *args = data; |
ca1543be | 151 | struct i915_vma *vma; |
6299f992 | 152 | size_t pinned; |
5a125c3c | 153 | |
6299f992 | 154 | pinned = 0; |
73aa808f | 155 | mutex_lock(&dev->struct_mutex); |
1c7f4bca | 156 | list_for_each_entry(vma, &ggtt->base.active_list, vm_link) |
20dfbde4 | 157 | if (i915_vma_is_pinned(vma)) |
ca1543be | 158 | pinned += vma->node.size; |
1c7f4bca | 159 | list_for_each_entry(vma, &ggtt->base.inactive_list, vm_link) |
20dfbde4 | 160 | if (i915_vma_is_pinned(vma)) |
ca1543be | 161 | pinned += vma->node.size; |
73aa808f | 162 | mutex_unlock(&dev->struct_mutex); |
5a125c3c | 163 | |
72e96d64 | 164 | args->aper_size = ggtt->base.total; |
0206e353 | 165 | args->aper_available_size = args->aper_size - pinned; |
6299f992 | 166 | |
5a125c3c EA |
167 | return 0; |
168 | } | |
169 | ||
6a2c4232 CW |
170 | static int |
171 | i915_gem_object_get_pages_phys(struct drm_i915_gem_object *obj) | |
00731155 | 172 | { |
6a2c4232 CW |
173 | struct address_space *mapping = file_inode(obj->base.filp)->i_mapping; |
174 | char *vaddr = obj->phys_handle->vaddr; | |
175 | struct sg_table *st; | |
176 | struct scatterlist *sg; | |
177 | int i; | |
00731155 | 178 | |
6a2c4232 CW |
179 | if (WARN_ON(i915_gem_object_needs_bit17_swizzle(obj))) |
180 | return -EINVAL; | |
181 | ||
182 | for (i = 0; i < obj->base.size / PAGE_SIZE; i++) { | |
183 | struct page *page; | |
184 | char *src; | |
185 | ||
186 | page = shmem_read_mapping_page(mapping, i); | |
187 | if (IS_ERR(page)) | |
188 | return PTR_ERR(page); | |
189 | ||
190 | src = kmap_atomic(page); | |
191 | memcpy(vaddr, src, PAGE_SIZE); | |
192 | drm_clflush_virt_range(vaddr, PAGE_SIZE); | |
193 | kunmap_atomic(src); | |
194 | ||
09cbfeaf | 195 | put_page(page); |
6a2c4232 CW |
196 | vaddr += PAGE_SIZE; |
197 | } | |
198 | ||
c033666a | 199 | i915_gem_chipset_flush(to_i915(obj->base.dev)); |
6a2c4232 CW |
200 | |
201 | st = kmalloc(sizeof(*st), GFP_KERNEL); | |
202 | if (st == NULL) | |
203 | return -ENOMEM; | |
204 | ||
205 | if (sg_alloc_table(st, 1, GFP_KERNEL)) { | |
206 | kfree(st); | |
207 | return -ENOMEM; | |
208 | } | |
209 | ||
210 | sg = st->sgl; | |
211 | sg->offset = 0; | |
212 | sg->length = obj->base.size; | |
00731155 | 213 | |
6a2c4232 CW |
214 | sg_dma_address(sg) = obj->phys_handle->busaddr; |
215 | sg_dma_len(sg) = obj->base.size; | |
216 | ||
217 | obj->pages = st; | |
6a2c4232 CW |
218 | return 0; |
219 | } | |
220 | ||
221 | static void | |
222 | i915_gem_object_put_pages_phys(struct drm_i915_gem_object *obj) | |
223 | { | |
224 | int ret; | |
225 | ||
226 | BUG_ON(obj->madv == __I915_MADV_PURGED); | |
00731155 | 227 | |
6a2c4232 | 228 | ret = i915_gem_object_set_to_cpu_domain(obj, true); |
f4457ae7 | 229 | if (WARN_ON(ret)) { |
6a2c4232 CW |
230 | /* In the event of a disaster, abandon all caches and |
231 | * hope for the best. | |
232 | */ | |
6a2c4232 CW |
233 | obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU; |
234 | } | |
235 | ||
236 | if (obj->madv == I915_MADV_DONTNEED) | |
237 | obj->dirty = 0; | |
238 | ||
239 | if (obj->dirty) { | |
00731155 | 240 | struct address_space *mapping = file_inode(obj->base.filp)->i_mapping; |
6a2c4232 | 241 | char *vaddr = obj->phys_handle->vaddr; |
00731155 CW |
242 | int i; |
243 | ||
244 | for (i = 0; i < obj->base.size / PAGE_SIZE; i++) { | |
6a2c4232 CW |
245 | struct page *page; |
246 | char *dst; | |
247 | ||
248 | page = shmem_read_mapping_page(mapping, i); | |
249 | if (IS_ERR(page)) | |
250 | continue; | |
251 | ||
252 | dst = kmap_atomic(page); | |
253 | drm_clflush_virt_range(vaddr, PAGE_SIZE); | |
254 | memcpy(dst, vaddr, PAGE_SIZE); | |
255 | kunmap_atomic(dst); | |
256 | ||
257 | set_page_dirty(page); | |
258 | if (obj->madv == I915_MADV_WILLNEED) | |
00731155 | 259 | mark_page_accessed(page); |
09cbfeaf | 260 | put_page(page); |
00731155 CW |
261 | vaddr += PAGE_SIZE; |
262 | } | |
6a2c4232 | 263 | obj->dirty = 0; |
00731155 CW |
264 | } |
265 | ||
6a2c4232 CW |
266 | sg_free_table(obj->pages); |
267 | kfree(obj->pages); | |
6a2c4232 CW |
268 | } |
269 | ||
270 | static void | |
271 | i915_gem_object_release_phys(struct drm_i915_gem_object *obj) | |
272 | { | |
273 | drm_pci_free(obj->base.dev, obj->phys_handle); | |
274 | } | |
275 | ||
276 | static const struct drm_i915_gem_object_ops i915_gem_phys_ops = { | |
277 | .get_pages = i915_gem_object_get_pages_phys, | |
278 | .put_pages = i915_gem_object_put_pages_phys, | |
279 | .release = i915_gem_object_release_phys, | |
280 | }; | |
281 | ||
aa653a68 CW |
282 | int |
283 | i915_gem_object_unbind(struct drm_i915_gem_object *obj) | |
284 | { | |
285 | struct i915_vma *vma; | |
286 | LIST_HEAD(still_in_list); | |
287 | int ret; | |
288 | ||
289 | /* The vma will only be freed if it is marked as closed, and if we wait | |
290 | * upon rendering to the vma, we may unbind anything in the list. | |
291 | */ | |
292 | while ((vma = list_first_entry_or_null(&obj->vma_list, | |
293 | struct i915_vma, | |
294 | obj_link))) { | |
295 | list_move_tail(&vma->obj_link, &still_in_list); | |
296 | ret = i915_vma_unbind(vma); | |
297 | if (ret) | |
298 | break; | |
299 | } | |
300 | list_splice(&still_in_list, &obj->vma_list); | |
301 | ||
302 | return ret; | |
303 | } | |
304 | ||
00e60f26 CW |
305 | /** |
306 | * Ensures that all rendering to the object has completed and the object is | |
307 | * safe to unbind from the GTT or access from the CPU. | |
308 | * @obj: i915 gem object | |
309 | * @readonly: waiting for just read access or read-write access | |
310 | */ | |
311 | int | |
312 | i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj, | |
313 | bool readonly) | |
314 | { | |
315 | struct reservation_object *resv; | |
316 | struct i915_gem_active *active; | |
317 | unsigned long active_mask; | |
318 | int idx; | |
319 | ||
320 | lockdep_assert_held(&obj->base.dev->struct_mutex); | |
321 | ||
322 | if (!readonly) { | |
323 | active = obj->last_read; | |
324 | active_mask = i915_gem_object_get_active(obj); | |
325 | } else { | |
326 | active_mask = 1; | |
327 | active = &obj->last_write; | |
328 | } | |
329 | ||
330 | for_each_active(active_mask, idx) { | |
331 | int ret; | |
332 | ||
333 | ret = i915_gem_active_wait(&active[idx], | |
334 | &obj->base.dev->struct_mutex); | |
335 | if (ret) | |
336 | return ret; | |
337 | } | |
338 | ||
339 | resv = i915_gem_object_get_dmabuf_resv(obj); | |
340 | if (resv) { | |
341 | long err; | |
342 | ||
343 | err = reservation_object_wait_timeout_rcu(resv, !readonly, true, | |
344 | MAX_SCHEDULE_TIMEOUT); | |
345 | if (err < 0) | |
346 | return err; | |
347 | } | |
348 | ||
349 | return 0; | |
350 | } | |
351 | ||
b8f9096d CW |
352 | /* A nonblocking variant of the above wait. Must be called prior to |
353 | * acquiring the mutex for the object, as the object state may change | |
354 | * during this call. A reference must be held by the caller for the object. | |
00e60f26 CW |
355 | */ |
356 | static __must_check int | |
b8f9096d CW |
357 | __unsafe_wait_rendering(struct drm_i915_gem_object *obj, |
358 | struct intel_rps_client *rps, | |
359 | bool readonly) | |
00e60f26 | 360 | { |
00e60f26 CW |
361 | struct i915_gem_active *active; |
362 | unsigned long active_mask; | |
b8f9096d | 363 | int idx; |
00e60f26 | 364 | |
b8f9096d | 365 | active_mask = __I915_BO_ACTIVE(obj); |
00e60f26 CW |
366 | if (!active_mask) |
367 | return 0; | |
368 | ||
369 | if (!readonly) { | |
370 | active = obj->last_read; | |
371 | } else { | |
372 | active_mask = 1; | |
373 | active = &obj->last_write; | |
374 | } | |
375 | ||
b8f9096d CW |
376 | for_each_active(active_mask, idx) { |
377 | int ret; | |
00e60f26 | 378 | |
b8f9096d CW |
379 | ret = i915_gem_active_wait_unlocked(&active[idx], |
380 | true, NULL, rps); | |
381 | if (ret) | |
382 | return ret; | |
00e60f26 CW |
383 | } |
384 | ||
b8f9096d | 385 | return 0; |
00e60f26 CW |
386 | } |
387 | ||
388 | static struct intel_rps_client *to_rps_client(struct drm_file *file) | |
389 | { | |
390 | struct drm_i915_file_private *fpriv = file->driver_priv; | |
391 | ||
392 | return &fpriv->rps; | |
393 | } | |
394 | ||
00731155 CW |
395 | int |
396 | i915_gem_object_attach_phys(struct drm_i915_gem_object *obj, | |
397 | int align) | |
398 | { | |
399 | drm_dma_handle_t *phys; | |
6a2c4232 | 400 | int ret; |
00731155 CW |
401 | |
402 | if (obj->phys_handle) { | |
403 | if ((unsigned long)obj->phys_handle->vaddr & (align -1)) | |
404 | return -EBUSY; | |
405 | ||
406 | return 0; | |
407 | } | |
408 | ||
409 | if (obj->madv != I915_MADV_WILLNEED) | |
410 | return -EFAULT; | |
411 | ||
412 | if (obj->base.filp == NULL) | |
413 | return -EINVAL; | |
414 | ||
4717ca9e CW |
415 | ret = i915_gem_object_unbind(obj); |
416 | if (ret) | |
417 | return ret; | |
418 | ||
419 | ret = i915_gem_object_put_pages(obj); | |
6a2c4232 CW |
420 | if (ret) |
421 | return ret; | |
422 | ||
00731155 CW |
423 | /* create a new object */ |
424 | phys = drm_pci_alloc(obj->base.dev, obj->base.size, align); | |
425 | if (!phys) | |
426 | return -ENOMEM; | |
427 | ||
00731155 | 428 | obj->phys_handle = phys; |
6a2c4232 CW |
429 | obj->ops = &i915_gem_phys_ops; |
430 | ||
431 | return i915_gem_object_get_pages(obj); | |
00731155 CW |
432 | } |
433 | ||
434 | static int | |
435 | i915_gem_phys_pwrite(struct drm_i915_gem_object *obj, | |
436 | struct drm_i915_gem_pwrite *args, | |
437 | struct drm_file *file_priv) | |
438 | { | |
439 | struct drm_device *dev = obj->base.dev; | |
440 | void *vaddr = obj->phys_handle->vaddr + args->offset; | |
3ed605bc | 441 | char __user *user_data = u64_to_user_ptr(args->data_ptr); |
063e4e6b | 442 | int ret = 0; |
6a2c4232 CW |
443 | |
444 | /* We manually control the domain here and pretend that it | |
445 | * remains coherent i.e. in the GTT domain, like shmem_pwrite. | |
446 | */ | |
447 | ret = i915_gem_object_wait_rendering(obj, false); | |
448 | if (ret) | |
449 | return ret; | |
00731155 | 450 | |
77a0d1ca | 451 | intel_fb_obj_invalidate(obj, ORIGIN_CPU); |
00731155 CW |
452 | if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) { |
453 | unsigned long unwritten; | |
454 | ||
455 | /* The physical object once assigned is fixed for the lifetime | |
456 | * of the obj, so we can safely drop the lock and continue | |
457 | * to access vaddr. | |
458 | */ | |
459 | mutex_unlock(&dev->struct_mutex); | |
460 | unwritten = copy_from_user(vaddr, user_data, args->size); | |
461 | mutex_lock(&dev->struct_mutex); | |
063e4e6b PZ |
462 | if (unwritten) { |
463 | ret = -EFAULT; | |
464 | goto out; | |
465 | } | |
00731155 CW |
466 | } |
467 | ||
6a2c4232 | 468 | drm_clflush_virt_range(vaddr, args->size); |
c033666a | 469 | i915_gem_chipset_flush(to_i915(dev)); |
063e4e6b PZ |
470 | |
471 | out: | |
de152b62 | 472 | intel_fb_obj_flush(obj, false, ORIGIN_CPU); |
063e4e6b | 473 | return ret; |
00731155 CW |
474 | } |
475 | ||
42dcedd4 CW |
476 | void *i915_gem_object_alloc(struct drm_device *dev) |
477 | { | |
fac5e23e | 478 | struct drm_i915_private *dev_priv = to_i915(dev); |
efab6d8d | 479 | return kmem_cache_zalloc(dev_priv->objects, GFP_KERNEL); |
42dcedd4 CW |
480 | } |
481 | ||
482 | void i915_gem_object_free(struct drm_i915_gem_object *obj) | |
483 | { | |
fac5e23e | 484 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
efab6d8d | 485 | kmem_cache_free(dev_priv->objects, obj); |
42dcedd4 CW |
486 | } |
487 | ||
ff72145b DA |
488 | static int |
489 | i915_gem_create(struct drm_file *file, | |
490 | struct drm_device *dev, | |
491 | uint64_t size, | |
492 | uint32_t *handle_p) | |
673a394b | 493 | { |
05394f39 | 494 | struct drm_i915_gem_object *obj; |
a1a2d1d3 PP |
495 | int ret; |
496 | u32 handle; | |
673a394b | 497 | |
ff72145b | 498 | size = roundup(size, PAGE_SIZE); |
8ffc0246 CW |
499 | if (size == 0) |
500 | return -EINVAL; | |
673a394b EA |
501 | |
502 | /* Allocate the new object */ | |
d37cd8a8 | 503 | obj = i915_gem_object_create(dev, size); |
fe3db79b CW |
504 | if (IS_ERR(obj)) |
505 | return PTR_ERR(obj); | |
673a394b | 506 | |
05394f39 | 507 | ret = drm_gem_handle_create(file, &obj->base, &handle); |
202f2fef | 508 | /* drop reference from allocate - handle holds it now */ |
34911fd3 | 509 | i915_gem_object_put_unlocked(obj); |
d861e338 DV |
510 | if (ret) |
511 | return ret; | |
202f2fef | 512 | |
ff72145b | 513 | *handle_p = handle; |
673a394b EA |
514 | return 0; |
515 | } | |
516 | ||
ff72145b DA |
517 | int |
518 | i915_gem_dumb_create(struct drm_file *file, | |
519 | struct drm_device *dev, | |
520 | struct drm_mode_create_dumb *args) | |
521 | { | |
522 | /* have to work out size/pitch and return them */ | |
de45eaf7 | 523 | args->pitch = ALIGN(args->width * DIV_ROUND_UP(args->bpp, 8), 64); |
ff72145b DA |
524 | args->size = args->pitch * args->height; |
525 | return i915_gem_create(file, dev, | |
da6b51d0 | 526 | args->size, &args->handle); |
ff72145b DA |
527 | } |
528 | ||
ff72145b DA |
529 | /** |
530 | * Creates a new mm object and returns a handle to it. | |
14bb2c11 TU |
531 | * @dev: drm device pointer |
532 | * @data: ioctl data blob | |
533 | * @file: drm file pointer | |
ff72145b DA |
534 | */ |
535 | int | |
536 | i915_gem_create_ioctl(struct drm_device *dev, void *data, | |
537 | struct drm_file *file) | |
538 | { | |
539 | struct drm_i915_gem_create *args = data; | |
63ed2cb2 | 540 | |
ff72145b | 541 | return i915_gem_create(file, dev, |
da6b51d0 | 542 | args->size, &args->handle); |
ff72145b DA |
543 | } |
544 | ||
8461d226 DV |
545 | static inline int |
546 | __copy_to_user_swizzled(char __user *cpu_vaddr, | |
547 | const char *gpu_vaddr, int gpu_offset, | |
548 | int length) | |
549 | { | |
550 | int ret, cpu_offset = 0; | |
551 | ||
552 | while (length > 0) { | |
553 | int cacheline_end = ALIGN(gpu_offset + 1, 64); | |
554 | int this_length = min(cacheline_end - gpu_offset, length); | |
555 | int swizzled_gpu_offset = gpu_offset ^ 64; | |
556 | ||
557 | ret = __copy_to_user(cpu_vaddr + cpu_offset, | |
558 | gpu_vaddr + swizzled_gpu_offset, | |
559 | this_length); | |
560 | if (ret) | |
561 | return ret + length; | |
562 | ||
563 | cpu_offset += this_length; | |
564 | gpu_offset += this_length; | |
565 | length -= this_length; | |
566 | } | |
567 | ||
568 | return 0; | |
569 | } | |
570 | ||
8c59967c | 571 | static inline int |
4f0c7cfb BW |
572 | __copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset, |
573 | const char __user *cpu_vaddr, | |
8c59967c DV |
574 | int length) |
575 | { | |
576 | int ret, cpu_offset = 0; | |
577 | ||
578 | while (length > 0) { | |
579 | int cacheline_end = ALIGN(gpu_offset + 1, 64); | |
580 | int this_length = min(cacheline_end - gpu_offset, length); | |
581 | int swizzled_gpu_offset = gpu_offset ^ 64; | |
582 | ||
583 | ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset, | |
584 | cpu_vaddr + cpu_offset, | |
585 | this_length); | |
586 | if (ret) | |
587 | return ret + length; | |
588 | ||
589 | cpu_offset += this_length; | |
590 | gpu_offset += this_length; | |
591 | length -= this_length; | |
592 | } | |
593 | ||
594 | return 0; | |
595 | } | |
596 | ||
4c914c0c BV |
597 | /* |
598 | * Pins the specified object's pages and synchronizes the object with | |
599 | * GPU accesses. Sets needs_clflush to non-zero if the caller should | |
600 | * flush the object from the CPU cache. | |
601 | */ | |
602 | int i915_gem_obj_prepare_shmem_read(struct drm_i915_gem_object *obj, | |
603 | int *needs_clflush) | |
604 | { | |
605 | int ret; | |
606 | ||
607 | *needs_clflush = 0; | |
608 | ||
b9bcd14a | 609 | if (WARN_ON(!i915_gem_object_has_struct_page(obj))) |
4c914c0c BV |
610 | return -EINVAL; |
611 | ||
c13d87ea CW |
612 | ret = i915_gem_object_wait_rendering(obj, true); |
613 | if (ret) | |
614 | return ret; | |
615 | ||
4c914c0c BV |
616 | if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU)) { |
617 | /* If we're not in the cpu read domain, set ourself into the gtt | |
618 | * read domain and manually flush cachelines (if required). This | |
619 | * optimizes for the case when the gpu will dirty the data | |
620 | * anyway again before the next pread happens. */ | |
621 | *needs_clflush = !cpu_cache_is_coherent(obj->base.dev, | |
622 | obj->cache_level); | |
4c914c0c BV |
623 | } |
624 | ||
625 | ret = i915_gem_object_get_pages(obj); | |
626 | if (ret) | |
627 | return ret; | |
628 | ||
629 | i915_gem_object_pin_pages(obj); | |
630 | ||
631 | return ret; | |
632 | } | |
633 | ||
d174bd64 DV |
634 | /* Per-page copy function for the shmem pread fastpath. |
635 | * Flushes invalid cachelines before reading the target if | |
636 | * needs_clflush is set. */ | |
eb01459f | 637 | static int |
d174bd64 DV |
638 | shmem_pread_fast(struct page *page, int shmem_page_offset, int page_length, |
639 | char __user *user_data, | |
640 | bool page_do_bit17_swizzling, bool needs_clflush) | |
641 | { | |
642 | char *vaddr; | |
643 | int ret; | |
644 | ||
e7e58eb5 | 645 | if (unlikely(page_do_bit17_swizzling)) |
d174bd64 DV |
646 | return -EINVAL; |
647 | ||
648 | vaddr = kmap_atomic(page); | |
649 | if (needs_clflush) | |
650 | drm_clflush_virt_range(vaddr + shmem_page_offset, | |
651 | page_length); | |
652 | ret = __copy_to_user_inatomic(user_data, | |
653 | vaddr + shmem_page_offset, | |
654 | page_length); | |
655 | kunmap_atomic(vaddr); | |
656 | ||
f60d7f0c | 657 | return ret ? -EFAULT : 0; |
d174bd64 DV |
658 | } |
659 | ||
23c18c71 DV |
660 | static void |
661 | shmem_clflush_swizzled_range(char *addr, unsigned long length, | |
662 | bool swizzled) | |
663 | { | |
e7e58eb5 | 664 | if (unlikely(swizzled)) { |
23c18c71 DV |
665 | unsigned long start = (unsigned long) addr; |
666 | unsigned long end = (unsigned long) addr + length; | |
667 | ||
668 | /* For swizzling simply ensure that we always flush both | |
669 | * channels. Lame, but simple and it works. Swizzled | |
670 | * pwrite/pread is far from a hotpath - current userspace | |
671 | * doesn't use it at all. */ | |
672 | start = round_down(start, 128); | |
673 | end = round_up(end, 128); | |
674 | ||
675 | drm_clflush_virt_range((void *)start, end - start); | |
676 | } else { | |
677 | drm_clflush_virt_range(addr, length); | |
678 | } | |
679 | ||
680 | } | |
681 | ||
d174bd64 DV |
682 | /* Only difference to the fast-path function is that this can handle bit17 |
683 | * and uses non-atomic copy and kmap functions. */ | |
684 | static int | |
685 | shmem_pread_slow(struct page *page, int shmem_page_offset, int page_length, | |
686 | char __user *user_data, | |
687 | bool page_do_bit17_swizzling, bool needs_clflush) | |
688 | { | |
689 | char *vaddr; | |
690 | int ret; | |
691 | ||
692 | vaddr = kmap(page); | |
693 | if (needs_clflush) | |
23c18c71 DV |
694 | shmem_clflush_swizzled_range(vaddr + shmem_page_offset, |
695 | page_length, | |
696 | page_do_bit17_swizzling); | |
d174bd64 DV |
697 | |
698 | if (page_do_bit17_swizzling) | |
699 | ret = __copy_to_user_swizzled(user_data, | |
700 | vaddr, shmem_page_offset, | |
701 | page_length); | |
702 | else | |
703 | ret = __copy_to_user(user_data, | |
704 | vaddr + shmem_page_offset, | |
705 | page_length); | |
706 | kunmap(page); | |
707 | ||
f60d7f0c | 708 | return ret ? - EFAULT : 0; |
d174bd64 DV |
709 | } |
710 | ||
b50a5371 AS |
711 | static inline unsigned long |
712 | slow_user_access(struct io_mapping *mapping, | |
713 | uint64_t page_base, int page_offset, | |
714 | char __user *user_data, | |
715 | unsigned long length, bool pwrite) | |
716 | { | |
717 | void __iomem *ioaddr; | |
718 | void *vaddr; | |
719 | uint64_t unwritten; | |
720 | ||
721 | ioaddr = io_mapping_map_wc(mapping, page_base, PAGE_SIZE); | |
722 | /* We can use the cpu mem copy function because this is X86. */ | |
723 | vaddr = (void __force *)ioaddr + page_offset; | |
724 | if (pwrite) | |
725 | unwritten = __copy_from_user(vaddr, user_data, length); | |
726 | else | |
727 | unwritten = __copy_to_user(user_data, vaddr, length); | |
728 | ||
729 | io_mapping_unmap(ioaddr); | |
730 | return unwritten; | |
731 | } | |
732 | ||
733 | static int | |
734 | i915_gem_gtt_pread(struct drm_device *dev, | |
735 | struct drm_i915_gem_object *obj, uint64_t size, | |
736 | uint64_t data_offset, uint64_t data_ptr) | |
737 | { | |
fac5e23e | 738 | struct drm_i915_private *dev_priv = to_i915(dev); |
b50a5371 AS |
739 | struct i915_ggtt *ggtt = &dev_priv->ggtt; |
740 | struct drm_mm_node node; | |
741 | char __user *user_data; | |
742 | uint64_t remain; | |
743 | uint64_t offset; | |
744 | int ret; | |
745 | ||
de895082 | 746 | ret = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, PIN_MAPPABLE); |
b50a5371 AS |
747 | if (ret) { |
748 | ret = insert_mappable_node(dev_priv, &node, PAGE_SIZE); | |
749 | if (ret) | |
750 | goto out; | |
751 | ||
752 | ret = i915_gem_object_get_pages(obj); | |
753 | if (ret) { | |
754 | remove_mappable_node(&node); | |
755 | goto out; | |
756 | } | |
757 | ||
758 | i915_gem_object_pin_pages(obj); | |
759 | } else { | |
760 | node.start = i915_gem_obj_ggtt_offset(obj); | |
761 | node.allocated = false; | |
762 | ret = i915_gem_object_put_fence(obj); | |
763 | if (ret) | |
764 | goto out_unpin; | |
765 | } | |
766 | ||
767 | ret = i915_gem_object_set_to_gtt_domain(obj, false); | |
768 | if (ret) | |
769 | goto out_unpin; | |
770 | ||
771 | user_data = u64_to_user_ptr(data_ptr); | |
772 | remain = size; | |
773 | offset = data_offset; | |
774 | ||
775 | mutex_unlock(&dev->struct_mutex); | |
776 | if (likely(!i915.prefault_disable)) { | |
777 | ret = fault_in_multipages_writeable(user_data, remain); | |
778 | if (ret) { | |
779 | mutex_lock(&dev->struct_mutex); | |
780 | goto out_unpin; | |
781 | } | |
782 | } | |
783 | ||
784 | while (remain > 0) { | |
785 | /* Operation in this page | |
786 | * | |
787 | * page_base = page offset within aperture | |
788 | * page_offset = offset within page | |
789 | * page_length = bytes to copy for this page | |
790 | */ | |
791 | u32 page_base = node.start; | |
792 | unsigned page_offset = offset_in_page(offset); | |
793 | unsigned page_length = PAGE_SIZE - page_offset; | |
794 | page_length = remain < page_length ? remain : page_length; | |
795 | if (node.allocated) { | |
796 | wmb(); | |
797 | ggtt->base.insert_page(&ggtt->base, | |
798 | i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT), | |
799 | node.start, | |
800 | I915_CACHE_NONE, 0); | |
801 | wmb(); | |
802 | } else { | |
803 | page_base += offset & PAGE_MASK; | |
804 | } | |
805 | /* This is a slow read/write as it tries to read from | |
806 | * and write to user memory which may result into page | |
807 | * faults, and so we cannot perform this under struct_mutex. | |
808 | */ | |
809 | if (slow_user_access(ggtt->mappable, page_base, | |
810 | page_offset, user_data, | |
811 | page_length, false)) { | |
812 | ret = -EFAULT; | |
813 | break; | |
814 | } | |
815 | ||
816 | remain -= page_length; | |
817 | user_data += page_length; | |
818 | offset += page_length; | |
819 | } | |
820 | ||
821 | mutex_lock(&dev->struct_mutex); | |
822 | if (ret == 0 && (obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0) { | |
823 | /* The user has modified the object whilst we tried | |
824 | * reading from it, and we now have no idea what domain | |
825 | * the pages should be in. As we have just been touching | |
826 | * them directly, flush everything back to the GTT | |
827 | * domain. | |
828 | */ | |
829 | ret = i915_gem_object_set_to_gtt_domain(obj, false); | |
830 | } | |
831 | ||
832 | out_unpin: | |
833 | if (node.allocated) { | |
834 | wmb(); | |
835 | ggtt->base.clear_range(&ggtt->base, | |
836 | node.start, node.size, | |
837 | true); | |
838 | i915_gem_object_unpin_pages(obj); | |
839 | remove_mappable_node(&node); | |
840 | } else { | |
841 | i915_gem_object_ggtt_unpin(obj); | |
842 | } | |
843 | out: | |
844 | return ret; | |
845 | } | |
846 | ||
eb01459f | 847 | static int |
dbf7bff0 DV |
848 | i915_gem_shmem_pread(struct drm_device *dev, |
849 | struct drm_i915_gem_object *obj, | |
850 | struct drm_i915_gem_pread *args, | |
851 | struct drm_file *file) | |
eb01459f | 852 | { |
8461d226 | 853 | char __user *user_data; |
eb01459f | 854 | ssize_t remain; |
8461d226 | 855 | loff_t offset; |
eb2c0c81 | 856 | int shmem_page_offset, page_length, ret = 0; |
8461d226 | 857 | int obj_do_bit17_swizzling, page_do_bit17_swizzling; |
96d79b52 | 858 | int prefaulted = 0; |
8489731c | 859 | int needs_clflush = 0; |
67d5a50c | 860 | struct sg_page_iter sg_iter; |
eb01459f | 861 | |
6eae0059 | 862 | if (!i915_gem_object_has_struct_page(obj)) |
b50a5371 AS |
863 | return -ENODEV; |
864 | ||
3ed605bc | 865 | user_data = u64_to_user_ptr(args->data_ptr); |
eb01459f EA |
866 | remain = args->size; |
867 | ||
8461d226 | 868 | obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj); |
eb01459f | 869 | |
4c914c0c | 870 | ret = i915_gem_obj_prepare_shmem_read(obj, &needs_clflush); |
f60d7f0c CW |
871 | if (ret) |
872 | return ret; | |
873 | ||
8461d226 | 874 | offset = args->offset; |
eb01459f | 875 | |
67d5a50c ID |
876 | for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents, |
877 | offset >> PAGE_SHIFT) { | |
2db76d7c | 878 | struct page *page = sg_page_iter_page(&sg_iter); |
9da3da66 CW |
879 | |
880 | if (remain <= 0) | |
881 | break; | |
882 | ||
eb01459f EA |
883 | /* Operation in this page |
884 | * | |
eb01459f | 885 | * shmem_page_offset = offset within page in shmem file |
eb01459f EA |
886 | * page_length = bytes to copy for this page |
887 | */ | |
c8cbbb8b | 888 | shmem_page_offset = offset_in_page(offset); |
eb01459f EA |
889 | page_length = remain; |
890 | if ((shmem_page_offset + page_length) > PAGE_SIZE) | |
891 | page_length = PAGE_SIZE - shmem_page_offset; | |
eb01459f | 892 | |
8461d226 DV |
893 | page_do_bit17_swizzling = obj_do_bit17_swizzling && |
894 | (page_to_phys(page) & (1 << 17)) != 0; | |
895 | ||
d174bd64 DV |
896 | ret = shmem_pread_fast(page, shmem_page_offset, page_length, |
897 | user_data, page_do_bit17_swizzling, | |
898 | needs_clflush); | |
899 | if (ret == 0) | |
900 | goto next_page; | |
dbf7bff0 | 901 | |
dbf7bff0 DV |
902 | mutex_unlock(&dev->struct_mutex); |
903 | ||
d330a953 | 904 | if (likely(!i915.prefault_disable) && !prefaulted) { |
f56f821f | 905 | ret = fault_in_multipages_writeable(user_data, remain); |
96d79b52 DV |
906 | /* Userspace is tricking us, but we've already clobbered |
907 | * its pages with the prefault and promised to write the | |
908 | * data up to the first fault. Hence ignore any errors | |
909 | * and just continue. */ | |
910 | (void)ret; | |
911 | prefaulted = 1; | |
912 | } | |
eb01459f | 913 | |
d174bd64 DV |
914 | ret = shmem_pread_slow(page, shmem_page_offset, page_length, |
915 | user_data, page_do_bit17_swizzling, | |
916 | needs_clflush); | |
eb01459f | 917 | |
dbf7bff0 | 918 | mutex_lock(&dev->struct_mutex); |
f60d7f0c | 919 | |
f60d7f0c | 920 | if (ret) |
8461d226 | 921 | goto out; |
8461d226 | 922 | |
17793c9a | 923 | next_page: |
eb01459f | 924 | remain -= page_length; |
8461d226 | 925 | user_data += page_length; |
eb01459f EA |
926 | offset += page_length; |
927 | } | |
928 | ||
4f27b75d | 929 | out: |
f60d7f0c CW |
930 | i915_gem_object_unpin_pages(obj); |
931 | ||
eb01459f EA |
932 | return ret; |
933 | } | |
934 | ||
673a394b EA |
935 | /** |
936 | * Reads data from the object referenced by handle. | |
14bb2c11 TU |
937 | * @dev: drm device pointer |
938 | * @data: ioctl data blob | |
939 | * @file: drm file pointer | |
673a394b EA |
940 | * |
941 | * On error, the contents of *data are undefined. | |
942 | */ | |
943 | int | |
944 | i915_gem_pread_ioctl(struct drm_device *dev, void *data, | |
05394f39 | 945 | struct drm_file *file) |
673a394b EA |
946 | { |
947 | struct drm_i915_gem_pread *args = data; | |
05394f39 | 948 | struct drm_i915_gem_object *obj; |
35b62a89 | 949 | int ret = 0; |
673a394b | 950 | |
51311d0a CW |
951 | if (args->size == 0) |
952 | return 0; | |
953 | ||
954 | if (!access_ok(VERIFY_WRITE, | |
3ed605bc | 955 | u64_to_user_ptr(args->data_ptr), |
51311d0a CW |
956 | args->size)) |
957 | return -EFAULT; | |
958 | ||
4f27b75d | 959 | ret = i915_mutex_lock_interruptible(dev); |
1d7cfea1 | 960 | if (ret) |
4f27b75d | 961 | return ret; |
673a394b | 962 | |
03ac0642 CW |
963 | obj = i915_gem_object_lookup(file, args->handle); |
964 | if (!obj) { | |
1d7cfea1 CW |
965 | ret = -ENOENT; |
966 | goto unlock; | |
4f27b75d | 967 | } |
673a394b | 968 | |
7dcd2499 | 969 | /* Bounds check source. */ |
05394f39 CW |
970 | if (args->offset > obj->base.size || |
971 | args->size > obj->base.size - args->offset) { | |
ce9d419d | 972 | ret = -EINVAL; |
35b62a89 | 973 | goto out; |
ce9d419d CW |
974 | } |
975 | ||
db53a302 CW |
976 | trace_i915_gem_object_pread(obj, args->offset, args->size); |
977 | ||
dbf7bff0 | 978 | ret = i915_gem_shmem_pread(dev, obj, args, file); |
673a394b | 979 | |
b50a5371 | 980 | /* pread for non shmem backed objects */ |
1dd5b6f2 CW |
981 | if (ret == -EFAULT || ret == -ENODEV) { |
982 | intel_runtime_pm_get(to_i915(dev)); | |
b50a5371 AS |
983 | ret = i915_gem_gtt_pread(dev, obj, args->size, |
984 | args->offset, args->data_ptr); | |
1dd5b6f2 CW |
985 | intel_runtime_pm_put(to_i915(dev)); |
986 | } | |
b50a5371 | 987 | |
35b62a89 | 988 | out: |
f8c417cd | 989 | i915_gem_object_put(obj); |
1d7cfea1 | 990 | unlock: |
4f27b75d | 991 | mutex_unlock(&dev->struct_mutex); |
eb01459f | 992 | return ret; |
673a394b EA |
993 | } |
994 | ||
0839ccb8 KP |
995 | /* This is the fast write path which cannot handle |
996 | * page faults in the source data | |
9b7530cc | 997 | */ |
0839ccb8 KP |
998 | |
999 | static inline int | |
1000 | fast_user_write(struct io_mapping *mapping, | |
1001 | loff_t page_base, int page_offset, | |
1002 | char __user *user_data, | |
1003 | int length) | |
9b7530cc | 1004 | { |
4f0c7cfb BW |
1005 | void __iomem *vaddr_atomic; |
1006 | void *vaddr; | |
0839ccb8 | 1007 | unsigned long unwritten; |
9b7530cc | 1008 | |
3e4d3af5 | 1009 | vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base); |
4f0c7cfb BW |
1010 | /* We can use the cpu mem copy function because this is X86. */ |
1011 | vaddr = (void __force*)vaddr_atomic + page_offset; | |
1012 | unwritten = __copy_from_user_inatomic_nocache(vaddr, | |
0839ccb8 | 1013 | user_data, length); |
3e4d3af5 | 1014 | io_mapping_unmap_atomic(vaddr_atomic); |
fbd5a26d | 1015 | return unwritten; |
0839ccb8 KP |
1016 | } |
1017 | ||
3de09aa3 EA |
1018 | /** |
1019 | * This is the fast pwrite path, where we copy the data directly from the | |
1020 | * user into the GTT, uncached. | |
62f90b38 | 1021 | * @i915: i915 device private data |
14bb2c11 TU |
1022 | * @obj: i915 gem object |
1023 | * @args: pwrite arguments structure | |
1024 | * @file: drm file pointer | |
3de09aa3 | 1025 | */ |
673a394b | 1026 | static int |
4f1959ee | 1027 | i915_gem_gtt_pwrite_fast(struct drm_i915_private *i915, |
05394f39 | 1028 | struct drm_i915_gem_object *obj, |
3de09aa3 | 1029 | struct drm_i915_gem_pwrite *args, |
05394f39 | 1030 | struct drm_file *file) |
673a394b | 1031 | { |
4f1959ee | 1032 | struct i915_ggtt *ggtt = &i915->ggtt; |
b50a5371 | 1033 | struct drm_device *dev = obj->base.dev; |
4f1959ee AS |
1034 | struct drm_mm_node node; |
1035 | uint64_t remain, offset; | |
673a394b | 1036 | char __user *user_data; |
4f1959ee | 1037 | int ret; |
b50a5371 AS |
1038 | bool hit_slow_path = false; |
1039 | ||
1040 | if (obj->tiling_mode != I915_TILING_NONE) | |
1041 | return -EFAULT; | |
935aaa69 | 1042 | |
de895082 CW |
1043 | ret = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, |
1044 | PIN_MAPPABLE | PIN_NONBLOCK); | |
4f1959ee AS |
1045 | if (ret) { |
1046 | ret = insert_mappable_node(i915, &node, PAGE_SIZE); | |
1047 | if (ret) | |
1048 | goto out; | |
1049 | ||
1050 | ret = i915_gem_object_get_pages(obj); | |
1051 | if (ret) { | |
1052 | remove_mappable_node(&node); | |
1053 | goto out; | |
1054 | } | |
1055 | ||
1056 | i915_gem_object_pin_pages(obj); | |
1057 | } else { | |
1058 | node.start = i915_gem_obj_ggtt_offset(obj); | |
1059 | node.allocated = false; | |
b50a5371 AS |
1060 | ret = i915_gem_object_put_fence(obj); |
1061 | if (ret) | |
1062 | goto out_unpin; | |
4f1959ee | 1063 | } |
935aaa69 DV |
1064 | |
1065 | ret = i915_gem_object_set_to_gtt_domain(obj, true); | |
1066 | if (ret) | |
1067 | goto out_unpin; | |
1068 | ||
77a0d1ca | 1069 | intel_fb_obj_invalidate(obj, ORIGIN_GTT); |
4f1959ee | 1070 | obj->dirty = true; |
063e4e6b | 1071 | |
4f1959ee AS |
1072 | user_data = u64_to_user_ptr(args->data_ptr); |
1073 | offset = args->offset; | |
1074 | remain = args->size; | |
1075 | while (remain) { | |
673a394b EA |
1076 | /* Operation in this page |
1077 | * | |
0839ccb8 KP |
1078 | * page_base = page offset within aperture |
1079 | * page_offset = offset within page | |
1080 | * page_length = bytes to copy for this page | |
673a394b | 1081 | */ |
4f1959ee AS |
1082 | u32 page_base = node.start; |
1083 | unsigned page_offset = offset_in_page(offset); | |
1084 | unsigned page_length = PAGE_SIZE - page_offset; | |
1085 | page_length = remain < page_length ? remain : page_length; | |
1086 | if (node.allocated) { | |
1087 | wmb(); /* flush the write before we modify the GGTT */ | |
1088 | ggtt->base.insert_page(&ggtt->base, | |
1089 | i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT), | |
1090 | node.start, I915_CACHE_NONE, 0); | |
1091 | wmb(); /* flush modifications to the GGTT (insert_page) */ | |
1092 | } else { | |
1093 | page_base += offset & PAGE_MASK; | |
1094 | } | |
0839ccb8 | 1095 | /* If we get a fault while copying data, then (presumably) our |
3de09aa3 EA |
1096 | * source page isn't available. Return the error and we'll |
1097 | * retry in the slow path. | |
b50a5371 AS |
1098 | * If the object is non-shmem backed, we retry again with the |
1099 | * path that handles page fault. | |
0839ccb8 | 1100 | */ |
72e96d64 | 1101 | if (fast_user_write(ggtt->mappable, page_base, |
935aaa69 | 1102 | page_offset, user_data, page_length)) { |
b50a5371 AS |
1103 | hit_slow_path = true; |
1104 | mutex_unlock(&dev->struct_mutex); | |
1105 | if (slow_user_access(ggtt->mappable, | |
1106 | page_base, | |
1107 | page_offset, user_data, | |
1108 | page_length, true)) { | |
1109 | ret = -EFAULT; | |
1110 | mutex_lock(&dev->struct_mutex); | |
1111 | goto out_flush; | |
1112 | } | |
1113 | ||
1114 | mutex_lock(&dev->struct_mutex); | |
935aaa69 | 1115 | } |
673a394b | 1116 | |
0839ccb8 KP |
1117 | remain -= page_length; |
1118 | user_data += page_length; | |
1119 | offset += page_length; | |
673a394b | 1120 | } |
673a394b | 1121 | |
063e4e6b | 1122 | out_flush: |
b50a5371 AS |
1123 | if (hit_slow_path) { |
1124 | if (ret == 0 && | |
1125 | (obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0) { | |
1126 | /* The user has modified the object whilst we tried | |
1127 | * reading from it, and we now have no idea what domain | |
1128 | * the pages should be in. As we have just been touching | |
1129 | * them directly, flush everything back to the GTT | |
1130 | * domain. | |
1131 | */ | |
1132 | ret = i915_gem_object_set_to_gtt_domain(obj, false); | |
1133 | } | |
1134 | } | |
1135 | ||
de152b62 | 1136 | intel_fb_obj_flush(obj, false, ORIGIN_GTT); |
935aaa69 | 1137 | out_unpin: |
4f1959ee AS |
1138 | if (node.allocated) { |
1139 | wmb(); | |
1140 | ggtt->base.clear_range(&ggtt->base, | |
1141 | node.start, node.size, | |
1142 | true); | |
1143 | i915_gem_object_unpin_pages(obj); | |
1144 | remove_mappable_node(&node); | |
1145 | } else { | |
1146 | i915_gem_object_ggtt_unpin(obj); | |
1147 | } | |
935aaa69 | 1148 | out: |
3de09aa3 | 1149 | return ret; |
673a394b EA |
1150 | } |
1151 | ||
d174bd64 DV |
1152 | /* Per-page copy function for the shmem pwrite fastpath. |
1153 | * Flushes invalid cachelines before writing to the target if | |
1154 | * needs_clflush_before is set and flushes out any written cachelines after | |
1155 | * writing if needs_clflush is set. */ | |
3043c60c | 1156 | static int |
d174bd64 DV |
1157 | shmem_pwrite_fast(struct page *page, int shmem_page_offset, int page_length, |
1158 | char __user *user_data, | |
1159 | bool page_do_bit17_swizzling, | |
1160 | bool needs_clflush_before, | |
1161 | bool needs_clflush_after) | |
673a394b | 1162 | { |
d174bd64 | 1163 | char *vaddr; |
673a394b | 1164 | int ret; |
3de09aa3 | 1165 | |
e7e58eb5 | 1166 | if (unlikely(page_do_bit17_swizzling)) |
d174bd64 | 1167 | return -EINVAL; |
3de09aa3 | 1168 | |
d174bd64 DV |
1169 | vaddr = kmap_atomic(page); |
1170 | if (needs_clflush_before) | |
1171 | drm_clflush_virt_range(vaddr + shmem_page_offset, | |
1172 | page_length); | |
c2831a94 CW |
1173 | ret = __copy_from_user_inatomic(vaddr + shmem_page_offset, |
1174 | user_data, page_length); | |
d174bd64 DV |
1175 | if (needs_clflush_after) |
1176 | drm_clflush_virt_range(vaddr + shmem_page_offset, | |
1177 | page_length); | |
1178 | kunmap_atomic(vaddr); | |
3de09aa3 | 1179 | |
755d2218 | 1180 | return ret ? -EFAULT : 0; |
3de09aa3 EA |
1181 | } |
1182 | ||
d174bd64 DV |
1183 | /* Only difference to the fast-path function is that this can handle bit17 |
1184 | * and uses non-atomic copy and kmap functions. */ | |
3043c60c | 1185 | static int |
d174bd64 DV |
1186 | shmem_pwrite_slow(struct page *page, int shmem_page_offset, int page_length, |
1187 | char __user *user_data, | |
1188 | bool page_do_bit17_swizzling, | |
1189 | bool needs_clflush_before, | |
1190 | bool needs_clflush_after) | |
673a394b | 1191 | { |
d174bd64 DV |
1192 | char *vaddr; |
1193 | int ret; | |
e5281ccd | 1194 | |
d174bd64 | 1195 | vaddr = kmap(page); |
e7e58eb5 | 1196 | if (unlikely(needs_clflush_before || page_do_bit17_swizzling)) |
23c18c71 DV |
1197 | shmem_clflush_swizzled_range(vaddr + shmem_page_offset, |
1198 | page_length, | |
1199 | page_do_bit17_swizzling); | |
d174bd64 DV |
1200 | if (page_do_bit17_swizzling) |
1201 | ret = __copy_from_user_swizzled(vaddr, shmem_page_offset, | |
e5281ccd CW |
1202 | user_data, |
1203 | page_length); | |
d174bd64 DV |
1204 | else |
1205 | ret = __copy_from_user(vaddr + shmem_page_offset, | |
1206 | user_data, | |
1207 | page_length); | |
1208 | if (needs_clflush_after) | |
23c18c71 DV |
1209 | shmem_clflush_swizzled_range(vaddr + shmem_page_offset, |
1210 | page_length, | |
1211 | page_do_bit17_swizzling); | |
d174bd64 | 1212 | kunmap(page); |
40123c1f | 1213 | |
755d2218 | 1214 | return ret ? -EFAULT : 0; |
40123c1f EA |
1215 | } |
1216 | ||
40123c1f | 1217 | static int |
e244a443 DV |
1218 | i915_gem_shmem_pwrite(struct drm_device *dev, |
1219 | struct drm_i915_gem_object *obj, | |
1220 | struct drm_i915_gem_pwrite *args, | |
1221 | struct drm_file *file) | |
40123c1f | 1222 | { |
40123c1f | 1223 | ssize_t remain; |
8c59967c DV |
1224 | loff_t offset; |
1225 | char __user *user_data; | |
eb2c0c81 | 1226 | int shmem_page_offset, page_length, ret = 0; |
8c59967c | 1227 | int obj_do_bit17_swizzling, page_do_bit17_swizzling; |
e244a443 | 1228 | int hit_slowpath = 0; |
58642885 DV |
1229 | int needs_clflush_after = 0; |
1230 | int needs_clflush_before = 0; | |
67d5a50c | 1231 | struct sg_page_iter sg_iter; |
40123c1f | 1232 | |
3ed605bc | 1233 | user_data = u64_to_user_ptr(args->data_ptr); |
40123c1f EA |
1234 | remain = args->size; |
1235 | ||
8c59967c | 1236 | obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj); |
40123c1f | 1237 | |
c13d87ea CW |
1238 | ret = i915_gem_object_wait_rendering(obj, false); |
1239 | if (ret) | |
1240 | return ret; | |
1241 | ||
58642885 DV |
1242 | if (obj->base.write_domain != I915_GEM_DOMAIN_CPU) { |
1243 | /* If we're not in the cpu write domain, set ourself into the gtt | |
1244 | * write domain and manually flush cachelines (if required). This | |
1245 | * optimizes for the case when the gpu will use the data | |
1246 | * right away and we therefore have to clflush anyway. */ | |
2c22569b | 1247 | needs_clflush_after = cpu_write_needs_clflush(obj); |
58642885 | 1248 | } |
c76ce038 CW |
1249 | /* Same trick applies to invalidate partially written cachelines read |
1250 | * before writing. */ | |
1251 | if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) | |
1252 | needs_clflush_before = | |
1253 | !cpu_cache_is_coherent(dev, obj->cache_level); | |
58642885 | 1254 | |
755d2218 CW |
1255 | ret = i915_gem_object_get_pages(obj); |
1256 | if (ret) | |
1257 | return ret; | |
1258 | ||
77a0d1ca | 1259 | intel_fb_obj_invalidate(obj, ORIGIN_CPU); |
063e4e6b | 1260 | |
755d2218 CW |
1261 | i915_gem_object_pin_pages(obj); |
1262 | ||
673a394b | 1263 | offset = args->offset; |
05394f39 | 1264 | obj->dirty = 1; |
673a394b | 1265 | |
67d5a50c ID |
1266 | for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents, |
1267 | offset >> PAGE_SHIFT) { | |
2db76d7c | 1268 | struct page *page = sg_page_iter_page(&sg_iter); |
58642885 | 1269 | int partial_cacheline_write; |
e5281ccd | 1270 | |
9da3da66 CW |
1271 | if (remain <= 0) |
1272 | break; | |
1273 | ||
40123c1f EA |
1274 | /* Operation in this page |
1275 | * | |
40123c1f | 1276 | * shmem_page_offset = offset within page in shmem file |
40123c1f EA |
1277 | * page_length = bytes to copy for this page |
1278 | */ | |
c8cbbb8b | 1279 | shmem_page_offset = offset_in_page(offset); |
40123c1f EA |
1280 | |
1281 | page_length = remain; | |
1282 | if ((shmem_page_offset + page_length) > PAGE_SIZE) | |
1283 | page_length = PAGE_SIZE - shmem_page_offset; | |
40123c1f | 1284 | |
58642885 DV |
1285 | /* If we don't overwrite a cacheline completely we need to be |
1286 | * careful to have up-to-date data by first clflushing. Don't | |
1287 | * overcomplicate things and flush the entire patch. */ | |
1288 | partial_cacheline_write = needs_clflush_before && | |
1289 | ((shmem_page_offset | page_length) | |
1290 | & (boot_cpu_data.x86_clflush_size - 1)); | |
1291 | ||
8c59967c DV |
1292 | page_do_bit17_swizzling = obj_do_bit17_swizzling && |
1293 | (page_to_phys(page) & (1 << 17)) != 0; | |
1294 | ||
d174bd64 DV |
1295 | ret = shmem_pwrite_fast(page, shmem_page_offset, page_length, |
1296 | user_data, page_do_bit17_swizzling, | |
1297 | partial_cacheline_write, | |
1298 | needs_clflush_after); | |
1299 | if (ret == 0) | |
1300 | goto next_page; | |
e244a443 DV |
1301 | |
1302 | hit_slowpath = 1; | |
e244a443 | 1303 | mutex_unlock(&dev->struct_mutex); |
d174bd64 DV |
1304 | ret = shmem_pwrite_slow(page, shmem_page_offset, page_length, |
1305 | user_data, page_do_bit17_swizzling, | |
1306 | partial_cacheline_write, | |
1307 | needs_clflush_after); | |
40123c1f | 1308 | |
e244a443 | 1309 | mutex_lock(&dev->struct_mutex); |
755d2218 | 1310 | |
755d2218 | 1311 | if (ret) |
8c59967c | 1312 | goto out; |
8c59967c | 1313 | |
17793c9a | 1314 | next_page: |
40123c1f | 1315 | remain -= page_length; |
8c59967c | 1316 | user_data += page_length; |
40123c1f | 1317 | offset += page_length; |
673a394b EA |
1318 | } |
1319 | ||
fbd5a26d | 1320 | out: |
755d2218 CW |
1321 | i915_gem_object_unpin_pages(obj); |
1322 | ||
e244a443 | 1323 | if (hit_slowpath) { |
8dcf015e DV |
1324 | /* |
1325 | * Fixup: Flush cpu caches in case we didn't flush the dirty | |
1326 | * cachelines in-line while writing and the object moved | |
1327 | * out of the cpu write domain while we've dropped the lock. | |
1328 | */ | |
1329 | if (!needs_clflush_after && | |
1330 | obj->base.write_domain != I915_GEM_DOMAIN_CPU) { | |
000433b6 | 1331 | if (i915_gem_clflush_object(obj, obj->pin_display)) |
ed75a55b | 1332 | needs_clflush_after = true; |
e244a443 | 1333 | } |
8c59967c | 1334 | } |
673a394b | 1335 | |
58642885 | 1336 | if (needs_clflush_after) |
c033666a | 1337 | i915_gem_chipset_flush(to_i915(dev)); |
ed75a55b VS |
1338 | else |
1339 | obj->cache_dirty = true; | |
58642885 | 1340 | |
de152b62 | 1341 | intel_fb_obj_flush(obj, false, ORIGIN_CPU); |
40123c1f | 1342 | return ret; |
673a394b EA |
1343 | } |
1344 | ||
1345 | /** | |
1346 | * Writes data to the object referenced by handle. | |
14bb2c11 TU |
1347 | * @dev: drm device |
1348 | * @data: ioctl data blob | |
1349 | * @file: drm file | |
673a394b EA |
1350 | * |
1351 | * On error, the contents of the buffer that were to be modified are undefined. | |
1352 | */ | |
1353 | int | |
1354 | i915_gem_pwrite_ioctl(struct drm_device *dev, void *data, | |
fbd5a26d | 1355 | struct drm_file *file) |
673a394b | 1356 | { |
fac5e23e | 1357 | struct drm_i915_private *dev_priv = to_i915(dev); |
673a394b | 1358 | struct drm_i915_gem_pwrite *args = data; |
05394f39 | 1359 | struct drm_i915_gem_object *obj; |
51311d0a CW |
1360 | int ret; |
1361 | ||
1362 | if (args->size == 0) | |
1363 | return 0; | |
1364 | ||
1365 | if (!access_ok(VERIFY_READ, | |
3ed605bc | 1366 | u64_to_user_ptr(args->data_ptr), |
51311d0a CW |
1367 | args->size)) |
1368 | return -EFAULT; | |
1369 | ||
d330a953 | 1370 | if (likely(!i915.prefault_disable)) { |
3ed605bc | 1371 | ret = fault_in_multipages_readable(u64_to_user_ptr(args->data_ptr), |
0b74b508 XZ |
1372 | args->size); |
1373 | if (ret) | |
1374 | return -EFAULT; | |
1375 | } | |
673a394b | 1376 | |
5d77d9c5 ID |
1377 | intel_runtime_pm_get(dev_priv); |
1378 | ||
fbd5a26d | 1379 | ret = i915_mutex_lock_interruptible(dev); |
1d7cfea1 | 1380 | if (ret) |
5d77d9c5 | 1381 | goto put_rpm; |
1d7cfea1 | 1382 | |
03ac0642 CW |
1383 | obj = i915_gem_object_lookup(file, args->handle); |
1384 | if (!obj) { | |
1d7cfea1 CW |
1385 | ret = -ENOENT; |
1386 | goto unlock; | |
fbd5a26d | 1387 | } |
673a394b | 1388 | |
7dcd2499 | 1389 | /* Bounds check destination. */ |
05394f39 CW |
1390 | if (args->offset > obj->base.size || |
1391 | args->size > obj->base.size - args->offset) { | |
ce9d419d | 1392 | ret = -EINVAL; |
35b62a89 | 1393 | goto out; |
ce9d419d CW |
1394 | } |
1395 | ||
db53a302 CW |
1396 | trace_i915_gem_object_pwrite(obj, args->offset, args->size); |
1397 | ||
935aaa69 | 1398 | ret = -EFAULT; |
673a394b EA |
1399 | /* We can only do the GTT pwrite on untiled buffers, as otherwise |
1400 | * it would end up going through the fenced access, and we'll get | |
1401 | * different detiling behavior between reading and writing. | |
1402 | * pread/pwrite currently are reading and writing from the CPU | |
1403 | * perspective, requiring manual detiling by the client. | |
1404 | */ | |
6eae0059 CW |
1405 | if (!i915_gem_object_has_struct_page(obj) || |
1406 | cpu_write_needs_clflush(obj)) { | |
4f1959ee | 1407 | ret = i915_gem_gtt_pwrite_fast(dev_priv, obj, args, file); |
935aaa69 DV |
1408 | /* Note that the gtt paths might fail with non-page-backed user |
1409 | * pointers (e.g. gtt mappings when moving data between | |
1410 | * textures). Fallback to the shmem path in that case. */ | |
fbd5a26d | 1411 | } |
673a394b | 1412 | |
d1054ee4 | 1413 | if (ret == -EFAULT || ret == -ENOSPC) { |
6a2c4232 CW |
1414 | if (obj->phys_handle) |
1415 | ret = i915_gem_phys_pwrite(obj, args, file); | |
6eae0059 | 1416 | else if (i915_gem_object_has_struct_page(obj)) |
6a2c4232 | 1417 | ret = i915_gem_shmem_pwrite(dev, obj, args, file); |
b50a5371 AS |
1418 | else |
1419 | ret = -ENODEV; | |
6a2c4232 | 1420 | } |
5c0480f2 | 1421 | |
35b62a89 | 1422 | out: |
f8c417cd | 1423 | i915_gem_object_put(obj); |
1d7cfea1 | 1424 | unlock: |
fbd5a26d | 1425 | mutex_unlock(&dev->struct_mutex); |
5d77d9c5 ID |
1426 | put_rpm: |
1427 | intel_runtime_pm_put(dev_priv); | |
1428 | ||
673a394b EA |
1429 | return ret; |
1430 | } | |
1431 | ||
aeecc969 CW |
1432 | static enum fb_op_origin |
1433 | write_origin(struct drm_i915_gem_object *obj, unsigned domain) | |
1434 | { | |
1435 | return domain == I915_GEM_DOMAIN_GTT && !obj->has_wc_mmap ? | |
1436 | ORIGIN_GTT : ORIGIN_CPU; | |
1437 | } | |
1438 | ||
673a394b | 1439 | /** |
2ef7eeaa EA |
1440 | * Called when user space prepares to use an object with the CPU, either |
1441 | * through the mmap ioctl's mapping or a GTT mapping. | |
14bb2c11 TU |
1442 | * @dev: drm device |
1443 | * @data: ioctl data blob | |
1444 | * @file: drm file | |
673a394b EA |
1445 | */ |
1446 | int | |
1447 | i915_gem_set_domain_ioctl(struct drm_device *dev, void *data, | |
05394f39 | 1448 | struct drm_file *file) |
673a394b EA |
1449 | { |
1450 | struct drm_i915_gem_set_domain *args = data; | |
05394f39 | 1451 | struct drm_i915_gem_object *obj; |
2ef7eeaa EA |
1452 | uint32_t read_domains = args->read_domains; |
1453 | uint32_t write_domain = args->write_domain; | |
673a394b EA |
1454 | int ret; |
1455 | ||
2ef7eeaa | 1456 | /* Only handle setting domains to types used by the CPU. */ |
b8f9096d | 1457 | if ((write_domain | read_domains) & I915_GEM_GPU_DOMAINS) |
2ef7eeaa EA |
1458 | return -EINVAL; |
1459 | ||
1460 | /* Having something in the write domain implies it's in the read | |
1461 | * domain, and only that read domain. Enforce that in the request. | |
1462 | */ | |
1463 | if (write_domain != 0 && read_domains != write_domain) | |
1464 | return -EINVAL; | |
1465 | ||
03ac0642 | 1466 | obj = i915_gem_object_lookup(file, args->handle); |
b8f9096d CW |
1467 | if (!obj) |
1468 | return -ENOENT; | |
673a394b | 1469 | |
3236f57a CW |
1470 | /* Try to flush the object off the GPU without holding the lock. |
1471 | * We will repeat the flush holding the lock in the normal manner | |
1472 | * to catch cases where we are gazumped. | |
1473 | */ | |
b8f9096d CW |
1474 | ret = __unsafe_wait_rendering(obj, to_rps_client(file), !write_domain); |
1475 | if (ret) | |
1476 | goto err; | |
1477 | ||
1478 | ret = i915_mutex_lock_interruptible(dev); | |
3236f57a | 1479 | if (ret) |
b8f9096d | 1480 | goto err; |
3236f57a | 1481 | |
43566ded | 1482 | if (read_domains & I915_GEM_DOMAIN_GTT) |
2ef7eeaa | 1483 | ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0); |
43566ded | 1484 | else |
e47c68e9 | 1485 | ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0); |
2ef7eeaa | 1486 | |
031b698a | 1487 | if (write_domain != 0) |
aeecc969 | 1488 | intel_fb_obj_invalidate(obj, write_origin(obj, write_domain)); |
031b698a | 1489 | |
f8c417cd | 1490 | i915_gem_object_put(obj); |
673a394b EA |
1491 | mutex_unlock(&dev->struct_mutex); |
1492 | return ret; | |
b8f9096d CW |
1493 | |
1494 | err: | |
1495 | i915_gem_object_put_unlocked(obj); | |
1496 | return ret; | |
673a394b EA |
1497 | } |
1498 | ||
1499 | /** | |
1500 | * Called when user space has done writes to this buffer | |
14bb2c11 TU |
1501 | * @dev: drm device |
1502 | * @data: ioctl data blob | |
1503 | * @file: drm file | |
673a394b EA |
1504 | */ |
1505 | int | |
1506 | i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data, | |
05394f39 | 1507 | struct drm_file *file) |
673a394b EA |
1508 | { |
1509 | struct drm_i915_gem_sw_finish *args = data; | |
05394f39 | 1510 | struct drm_i915_gem_object *obj; |
673a394b EA |
1511 | int ret = 0; |
1512 | ||
76c1dec1 | 1513 | ret = i915_mutex_lock_interruptible(dev); |
1d7cfea1 | 1514 | if (ret) |
76c1dec1 | 1515 | return ret; |
1d7cfea1 | 1516 | |
03ac0642 CW |
1517 | obj = i915_gem_object_lookup(file, args->handle); |
1518 | if (!obj) { | |
1d7cfea1 CW |
1519 | ret = -ENOENT; |
1520 | goto unlock; | |
673a394b EA |
1521 | } |
1522 | ||
673a394b | 1523 | /* Pinned buffers may be scanout, so flush the cache */ |
2c22569b | 1524 | if (obj->pin_display) |
e62b59e4 | 1525 | i915_gem_object_flush_cpu_write_domain(obj); |
e47c68e9 | 1526 | |
f8c417cd | 1527 | i915_gem_object_put(obj); |
1d7cfea1 | 1528 | unlock: |
673a394b EA |
1529 | mutex_unlock(&dev->struct_mutex); |
1530 | return ret; | |
1531 | } | |
1532 | ||
1533 | /** | |
14bb2c11 TU |
1534 | * i915_gem_mmap_ioctl - Maps the contents of an object, returning the address |
1535 | * it is mapped to. | |
1536 | * @dev: drm device | |
1537 | * @data: ioctl data blob | |
1538 | * @file: drm file | |
673a394b EA |
1539 | * |
1540 | * While the mapping holds a reference on the contents of the object, it doesn't | |
1541 | * imply a ref on the object itself. | |
34367381 DV |
1542 | * |
1543 | * IMPORTANT: | |
1544 | * | |
1545 | * DRM driver writers who look a this function as an example for how to do GEM | |
1546 | * mmap support, please don't implement mmap support like here. The modern way | |
1547 | * to implement DRM mmap support is with an mmap offset ioctl (like | |
1548 | * i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly. | |
1549 | * That way debug tooling like valgrind will understand what's going on, hiding | |
1550 | * the mmap call in a driver private ioctl will break that. The i915 driver only | |
1551 | * does cpu mmaps this way because we didn't know better. | |
673a394b EA |
1552 | */ |
1553 | int | |
1554 | i915_gem_mmap_ioctl(struct drm_device *dev, void *data, | |
05394f39 | 1555 | struct drm_file *file) |
673a394b EA |
1556 | { |
1557 | struct drm_i915_gem_mmap *args = data; | |
03ac0642 | 1558 | struct drm_i915_gem_object *obj; |
673a394b EA |
1559 | unsigned long addr; |
1560 | ||
1816f923 AG |
1561 | if (args->flags & ~(I915_MMAP_WC)) |
1562 | return -EINVAL; | |
1563 | ||
568a58e5 | 1564 | if (args->flags & I915_MMAP_WC && !boot_cpu_has(X86_FEATURE_PAT)) |
1816f923 AG |
1565 | return -ENODEV; |
1566 | ||
03ac0642 CW |
1567 | obj = i915_gem_object_lookup(file, args->handle); |
1568 | if (!obj) | |
bf79cb91 | 1569 | return -ENOENT; |
673a394b | 1570 | |
1286ff73 DV |
1571 | /* prime objects have no backing filp to GEM mmap |
1572 | * pages from. | |
1573 | */ | |
03ac0642 | 1574 | if (!obj->base.filp) { |
34911fd3 | 1575 | i915_gem_object_put_unlocked(obj); |
1286ff73 DV |
1576 | return -EINVAL; |
1577 | } | |
1578 | ||
03ac0642 | 1579 | addr = vm_mmap(obj->base.filp, 0, args->size, |
673a394b EA |
1580 | PROT_READ | PROT_WRITE, MAP_SHARED, |
1581 | args->offset); | |
1816f923 AG |
1582 | if (args->flags & I915_MMAP_WC) { |
1583 | struct mm_struct *mm = current->mm; | |
1584 | struct vm_area_struct *vma; | |
1585 | ||
80a89a5e | 1586 | if (down_write_killable(&mm->mmap_sem)) { |
34911fd3 | 1587 | i915_gem_object_put_unlocked(obj); |
80a89a5e MH |
1588 | return -EINTR; |
1589 | } | |
1816f923 AG |
1590 | vma = find_vma(mm, addr); |
1591 | if (vma) | |
1592 | vma->vm_page_prot = | |
1593 | pgprot_writecombine(vm_get_page_prot(vma->vm_flags)); | |
1594 | else | |
1595 | addr = -ENOMEM; | |
1596 | up_write(&mm->mmap_sem); | |
aeecc969 CW |
1597 | |
1598 | /* This may race, but that's ok, it only gets set */ | |
03ac0642 | 1599 | WRITE_ONCE(obj->has_wc_mmap, true); |
1816f923 | 1600 | } |
34911fd3 | 1601 | i915_gem_object_put_unlocked(obj); |
673a394b EA |
1602 | if (IS_ERR((void *)addr)) |
1603 | return addr; | |
1604 | ||
1605 | args->addr_ptr = (uint64_t) addr; | |
1606 | ||
1607 | return 0; | |
1608 | } | |
1609 | ||
de151cf6 JB |
1610 | /** |
1611 | * i915_gem_fault - fault a page into the GTT | |
d9072a3e GT |
1612 | * @vma: VMA in question |
1613 | * @vmf: fault info | |
de151cf6 JB |
1614 | * |
1615 | * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped | |
1616 | * from userspace. The fault handler takes care of binding the object to | |
1617 | * the GTT (if needed), allocating and programming a fence register (again, | |
1618 | * only if needed based on whether the old reg is still valid or the object | |
1619 | * is tiled) and inserting a new PTE into the faulting process. | |
1620 | * | |
1621 | * Note that the faulting process may involve evicting existing objects | |
1622 | * from the GTT and/or fence registers to make room. So performance may | |
1623 | * suffer if the GTT working set is large or there are few fence registers | |
1624 | * left. | |
1625 | */ | |
1626 | int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf) | |
1627 | { | |
05394f39 CW |
1628 | struct drm_i915_gem_object *obj = to_intel_bo(vma->vm_private_data); |
1629 | struct drm_device *dev = obj->base.dev; | |
72e96d64 JL |
1630 | struct drm_i915_private *dev_priv = to_i915(dev); |
1631 | struct i915_ggtt *ggtt = &dev_priv->ggtt; | |
c5ad54cf | 1632 | struct i915_ggtt_view view = i915_ggtt_view_normal; |
b8f9096d | 1633 | bool write = !!(vmf->flags & FAULT_FLAG_WRITE); |
de151cf6 JB |
1634 | pgoff_t page_offset; |
1635 | unsigned long pfn; | |
b8f9096d | 1636 | int ret; |
f65c9168 | 1637 | |
de151cf6 JB |
1638 | /* We don't use vmf->pgoff since that has the fake offset */ |
1639 | page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >> | |
1640 | PAGE_SHIFT; | |
1641 | ||
db53a302 CW |
1642 | trace_i915_gem_object_fault(obj, page_offset, true, write); |
1643 | ||
6e4930f6 | 1644 | /* Try to flush the object off the GPU first without holding the lock. |
b8f9096d | 1645 | * Upon acquiring the lock, we will perform our sanity checks and then |
6e4930f6 CW |
1646 | * repeat the flush holding the lock in the normal manner to catch cases |
1647 | * where we are gazumped. | |
1648 | */ | |
b8f9096d | 1649 | ret = __unsafe_wait_rendering(obj, NULL, !write); |
6e4930f6 | 1650 | if (ret) |
b8f9096d CW |
1651 | goto err; |
1652 | ||
1653 | intel_runtime_pm_get(dev_priv); | |
1654 | ||
1655 | ret = i915_mutex_lock_interruptible(dev); | |
1656 | if (ret) | |
1657 | goto err_rpm; | |
6e4930f6 | 1658 | |
eb119bd6 CW |
1659 | /* Access to snoopable pages through the GTT is incoherent. */ |
1660 | if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev)) { | |
ddeff6ee | 1661 | ret = -EFAULT; |
b8f9096d | 1662 | goto err_unlock; |
eb119bd6 CW |
1663 | } |
1664 | ||
c5ad54cf | 1665 | /* Use a partial view if the object is bigger than the aperture. */ |
72e96d64 | 1666 | if (obj->base.size >= ggtt->mappable_end && |
e7ded2d7 | 1667 | obj->tiling_mode == I915_TILING_NONE) { |
c5ad54cf | 1668 | static const unsigned int chunk_size = 256; // 1 MiB |
e7ded2d7 | 1669 | |
c5ad54cf JL |
1670 | memset(&view, 0, sizeof(view)); |
1671 | view.type = I915_GGTT_VIEW_PARTIAL; | |
1672 | view.params.partial.offset = rounddown(page_offset, chunk_size); | |
1673 | view.params.partial.size = | |
1674 | min_t(unsigned int, | |
1675 | chunk_size, | |
1676 | (vma->vm_end - vma->vm_start)/PAGE_SIZE - | |
1677 | view.params.partial.offset); | |
1678 | } | |
1679 | ||
1680 | /* Now pin it into the GTT if needed */ | |
91b2db6f | 1681 | ret = i915_gem_object_ggtt_pin(obj, &view, 0, 0, PIN_MAPPABLE); |
c9839303 | 1682 | if (ret) |
b8f9096d | 1683 | goto err_unlock; |
4a684a41 | 1684 | |
c9839303 CW |
1685 | ret = i915_gem_object_set_to_gtt_domain(obj, write); |
1686 | if (ret) | |
b8f9096d | 1687 | goto err_unpin; |
74898d7e | 1688 | |
06d98131 | 1689 | ret = i915_gem_object_get_fence(obj); |
d9e86c0e | 1690 | if (ret) |
b8f9096d | 1691 | goto err_unpin; |
7d1c4804 | 1692 | |
b90b91d8 | 1693 | /* Finally, remap it using the new GTT offset */ |
72e96d64 | 1694 | pfn = ggtt->mappable_base + |
c5ad54cf | 1695 | i915_gem_obj_ggtt_offset_view(obj, &view); |
f343c5f6 | 1696 | pfn >>= PAGE_SHIFT; |
de151cf6 | 1697 | |
c5ad54cf JL |
1698 | if (unlikely(view.type == I915_GGTT_VIEW_PARTIAL)) { |
1699 | /* Overriding existing pages in partial view does not cause | |
1700 | * us any trouble as TLBs are still valid because the fault | |
1701 | * is due to userspace losing part of the mapping or never | |
1702 | * having accessed it before (at this partials' range). | |
1703 | */ | |
1704 | unsigned long base = vma->vm_start + | |
1705 | (view.params.partial.offset << PAGE_SHIFT); | |
1706 | unsigned int i; | |
b90b91d8 | 1707 | |
c5ad54cf JL |
1708 | for (i = 0; i < view.params.partial.size; i++) { |
1709 | ret = vm_insert_pfn(vma, base + i * PAGE_SIZE, pfn + i); | |
b90b91d8 CW |
1710 | if (ret) |
1711 | break; | |
1712 | } | |
1713 | ||
1714 | obj->fault_mappable = true; | |
c5ad54cf JL |
1715 | } else { |
1716 | if (!obj->fault_mappable) { | |
1717 | unsigned long size = min_t(unsigned long, | |
1718 | vma->vm_end - vma->vm_start, | |
1719 | obj->base.size); | |
1720 | int i; | |
1721 | ||
1722 | for (i = 0; i < size >> PAGE_SHIFT; i++) { | |
1723 | ret = vm_insert_pfn(vma, | |
1724 | (unsigned long)vma->vm_start + i * PAGE_SIZE, | |
1725 | pfn + i); | |
1726 | if (ret) | |
1727 | break; | |
1728 | } | |
1729 | ||
1730 | obj->fault_mappable = true; | |
1731 | } else | |
1732 | ret = vm_insert_pfn(vma, | |
1733 | (unsigned long)vmf->virtual_address, | |
1734 | pfn + page_offset); | |
1735 | } | |
b8f9096d | 1736 | err_unpin: |
c5ad54cf | 1737 | i915_gem_object_ggtt_unpin_view(obj, &view); |
b8f9096d | 1738 | err_unlock: |
de151cf6 | 1739 | mutex_unlock(&dev->struct_mutex); |
b8f9096d CW |
1740 | err_rpm: |
1741 | intel_runtime_pm_put(dev_priv); | |
1742 | err: | |
de151cf6 | 1743 | switch (ret) { |
d9bc7e9f | 1744 | case -EIO: |
2232f031 DV |
1745 | /* |
1746 | * We eat errors when the gpu is terminally wedged to avoid | |
1747 | * userspace unduly crashing (gl has no provisions for mmaps to | |
1748 | * fail). But any other -EIO isn't ours (e.g. swap in failure) | |
1749 | * and so needs to be reported. | |
1750 | */ | |
1751 | if (!i915_terminally_wedged(&dev_priv->gpu_error)) { | |
f65c9168 PZ |
1752 | ret = VM_FAULT_SIGBUS; |
1753 | break; | |
1754 | } | |
045e769a | 1755 | case -EAGAIN: |
571c608d DV |
1756 | /* |
1757 | * EAGAIN means the gpu is hung and we'll wait for the error | |
1758 | * handler to reset everything when re-faulting in | |
1759 | * i915_mutex_lock_interruptible. | |
d9bc7e9f | 1760 | */ |
c715089f CW |
1761 | case 0: |
1762 | case -ERESTARTSYS: | |
bed636ab | 1763 | case -EINTR: |
e79e0fe3 DR |
1764 | case -EBUSY: |
1765 | /* | |
1766 | * EBUSY is ok: this just means that another thread | |
1767 | * already did the job. | |
1768 | */ | |
f65c9168 PZ |
1769 | ret = VM_FAULT_NOPAGE; |
1770 | break; | |
de151cf6 | 1771 | case -ENOMEM: |
f65c9168 PZ |
1772 | ret = VM_FAULT_OOM; |
1773 | break; | |
a7c2e1aa | 1774 | case -ENOSPC: |
45d67817 | 1775 | case -EFAULT: |
f65c9168 PZ |
1776 | ret = VM_FAULT_SIGBUS; |
1777 | break; | |
de151cf6 | 1778 | default: |
a7c2e1aa | 1779 | WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret); |
f65c9168 PZ |
1780 | ret = VM_FAULT_SIGBUS; |
1781 | break; | |
de151cf6 | 1782 | } |
f65c9168 | 1783 | return ret; |
de151cf6 JB |
1784 | } |
1785 | ||
901782b2 CW |
1786 | /** |
1787 | * i915_gem_release_mmap - remove physical page mappings | |
1788 | * @obj: obj in question | |
1789 | * | |
af901ca1 | 1790 | * Preserve the reservation of the mmapping with the DRM core code, but |
901782b2 CW |
1791 | * relinquish ownership of the pages back to the system. |
1792 | * | |
1793 | * It is vital that we remove the page mapping if we have mapped a tiled | |
1794 | * object through the GTT and then lose the fence register due to | |
1795 | * resource pressure. Similarly if the object has been moved out of the | |
1796 | * aperture, than pages mapped into userspace must be revoked. Removing the | |
1797 | * mapping will then trigger a page fault on the next user access, allowing | |
1798 | * fixup by i915_gem_fault(). | |
1799 | */ | |
d05ca301 | 1800 | void |
05394f39 | 1801 | i915_gem_release_mmap(struct drm_i915_gem_object *obj) |
901782b2 | 1802 | { |
349f2ccf CW |
1803 | /* Serialisation between user GTT access and our code depends upon |
1804 | * revoking the CPU's PTE whilst the mutex is held. The next user | |
1805 | * pagefault then has to wait until we release the mutex. | |
1806 | */ | |
1807 | lockdep_assert_held(&obj->base.dev->struct_mutex); | |
1808 | ||
6299f992 CW |
1809 | if (!obj->fault_mappable) |
1810 | return; | |
901782b2 | 1811 | |
6796cb16 DR |
1812 | drm_vma_node_unmap(&obj->base.vma_node, |
1813 | obj->base.dev->anon_inode->i_mapping); | |
349f2ccf CW |
1814 | |
1815 | /* Ensure that the CPU's PTE are revoked and there are not outstanding | |
1816 | * memory transactions from userspace before we return. The TLB | |
1817 | * flushing implied above by changing the PTE above *should* be | |
1818 | * sufficient, an extra barrier here just provides us with a bit | |
1819 | * of paranoid documentation about our requirement to serialise | |
1820 | * memory writes before touching registers / GSM. | |
1821 | */ | |
1822 | wmb(); | |
1823 | ||
6299f992 | 1824 | obj->fault_mappable = false; |
901782b2 CW |
1825 | } |
1826 | ||
eedd10f4 CW |
1827 | void |
1828 | i915_gem_release_all_mmaps(struct drm_i915_private *dev_priv) | |
1829 | { | |
1830 | struct drm_i915_gem_object *obj; | |
1831 | ||
1832 | list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) | |
1833 | i915_gem_release_mmap(obj); | |
1834 | } | |
1835 | ||
ad1a7d20 CW |
1836 | /** |
1837 | * i915_gem_get_ggtt_size - return required global GTT size for an object | |
a9f1481f | 1838 | * @dev_priv: i915 device |
ad1a7d20 CW |
1839 | * @size: object size |
1840 | * @tiling_mode: tiling mode | |
1841 | * | |
1842 | * Return the required global GTT size for an object, taking into account | |
1843 | * potential fence register mapping. | |
1844 | */ | |
a9f1481f CW |
1845 | u64 i915_gem_get_ggtt_size(struct drm_i915_private *dev_priv, |
1846 | u64 size, int tiling_mode) | |
92b88aeb | 1847 | { |
ad1a7d20 | 1848 | u64 ggtt_size; |
92b88aeb | 1849 | |
ad1a7d20 CW |
1850 | GEM_BUG_ON(size == 0); |
1851 | ||
a9f1481f | 1852 | if (INTEL_GEN(dev_priv) >= 4 || |
e28f8711 CW |
1853 | tiling_mode == I915_TILING_NONE) |
1854 | return size; | |
92b88aeb CW |
1855 | |
1856 | /* Previous chips need a power-of-two fence region when tiling */ | |
a9f1481f | 1857 | if (IS_GEN3(dev_priv)) |
ad1a7d20 | 1858 | ggtt_size = 1024*1024; |
92b88aeb | 1859 | else |
ad1a7d20 | 1860 | ggtt_size = 512*1024; |
92b88aeb | 1861 | |
ad1a7d20 CW |
1862 | while (ggtt_size < size) |
1863 | ggtt_size <<= 1; | |
92b88aeb | 1864 | |
ad1a7d20 | 1865 | return ggtt_size; |
92b88aeb CW |
1866 | } |
1867 | ||
de151cf6 | 1868 | /** |
ad1a7d20 | 1869 | * i915_gem_get_ggtt_alignment - return required global GTT alignment |
a9f1481f | 1870 | * @dev_priv: i915 device |
14bb2c11 TU |
1871 | * @size: object size |
1872 | * @tiling_mode: tiling mode | |
ad1a7d20 | 1873 | * @fenced: is fenced alignment required or not |
de151cf6 | 1874 | * |
ad1a7d20 | 1875 | * Return the required global GTT alignment for an object, taking into account |
5e783301 | 1876 | * potential fence register mapping. |
de151cf6 | 1877 | */ |
a9f1481f | 1878 | u64 i915_gem_get_ggtt_alignment(struct drm_i915_private *dev_priv, u64 size, |
ad1a7d20 | 1879 | int tiling_mode, bool fenced) |
de151cf6 | 1880 | { |
ad1a7d20 CW |
1881 | GEM_BUG_ON(size == 0); |
1882 | ||
de151cf6 JB |
1883 | /* |
1884 | * Minimum alignment is 4k (GTT page size), but might be greater | |
1885 | * if a fence register is needed for the object. | |
1886 | */ | |
a9f1481f | 1887 | if (INTEL_GEN(dev_priv) >= 4 || (!fenced && IS_G33(dev_priv)) || |
e28f8711 | 1888 | tiling_mode == I915_TILING_NONE) |
de151cf6 JB |
1889 | return 4096; |
1890 | ||
a00b10c3 CW |
1891 | /* |
1892 | * Previous chips need to be aligned to the size of the smallest | |
1893 | * fence register that can contain the object. | |
1894 | */ | |
a9f1481f | 1895 | return i915_gem_get_ggtt_size(dev_priv, size, tiling_mode); |
a00b10c3 CW |
1896 | } |
1897 | ||
d8cb5086 CW |
1898 | static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj) |
1899 | { | |
fac5e23e | 1900 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
d8cb5086 CW |
1901 | int ret; |
1902 | ||
da494d7c DV |
1903 | dev_priv->mm.shrinker_no_lock_stealing = true; |
1904 | ||
d8cb5086 CW |
1905 | ret = drm_gem_create_mmap_offset(&obj->base); |
1906 | if (ret != -ENOSPC) | |
da494d7c | 1907 | goto out; |
d8cb5086 CW |
1908 | |
1909 | /* Badly fragmented mmap space? The only way we can recover | |
1910 | * space is by destroying unwanted objects. We can't randomly release | |
1911 | * mmap_offsets as userspace expects them to be persistent for the | |
1912 | * lifetime of the objects. The closest we can is to release the | |
1913 | * offsets on purgeable objects by truncating it and marking it purged, | |
1914 | * which prevents userspace from ever using that object again. | |
1915 | */ | |
21ab4e74 CW |
1916 | i915_gem_shrink(dev_priv, |
1917 | obj->base.size >> PAGE_SHIFT, | |
1918 | I915_SHRINK_BOUND | | |
1919 | I915_SHRINK_UNBOUND | | |
1920 | I915_SHRINK_PURGEABLE); | |
d8cb5086 CW |
1921 | ret = drm_gem_create_mmap_offset(&obj->base); |
1922 | if (ret != -ENOSPC) | |
da494d7c | 1923 | goto out; |
d8cb5086 CW |
1924 | |
1925 | i915_gem_shrink_all(dev_priv); | |
da494d7c DV |
1926 | ret = drm_gem_create_mmap_offset(&obj->base); |
1927 | out: | |
1928 | dev_priv->mm.shrinker_no_lock_stealing = false; | |
1929 | ||
1930 | return ret; | |
d8cb5086 CW |
1931 | } |
1932 | ||
1933 | static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj) | |
1934 | { | |
d8cb5086 CW |
1935 | drm_gem_free_mmap_offset(&obj->base); |
1936 | } | |
1937 | ||
da6b51d0 | 1938 | int |
ff72145b DA |
1939 | i915_gem_mmap_gtt(struct drm_file *file, |
1940 | struct drm_device *dev, | |
da6b51d0 | 1941 | uint32_t handle, |
ff72145b | 1942 | uint64_t *offset) |
de151cf6 | 1943 | { |
05394f39 | 1944 | struct drm_i915_gem_object *obj; |
de151cf6 JB |
1945 | int ret; |
1946 | ||
76c1dec1 | 1947 | ret = i915_mutex_lock_interruptible(dev); |
1d7cfea1 | 1948 | if (ret) |
76c1dec1 | 1949 | return ret; |
de151cf6 | 1950 | |
03ac0642 CW |
1951 | obj = i915_gem_object_lookup(file, handle); |
1952 | if (!obj) { | |
1d7cfea1 CW |
1953 | ret = -ENOENT; |
1954 | goto unlock; | |
1955 | } | |
de151cf6 | 1956 | |
05394f39 | 1957 | if (obj->madv != I915_MADV_WILLNEED) { |
bd9b6a4e | 1958 | DRM_DEBUG("Attempting to mmap a purgeable buffer\n"); |
8c99e57d | 1959 | ret = -EFAULT; |
1d7cfea1 | 1960 | goto out; |
ab18282d CW |
1961 | } |
1962 | ||
d8cb5086 CW |
1963 | ret = i915_gem_object_create_mmap_offset(obj); |
1964 | if (ret) | |
1965 | goto out; | |
de151cf6 | 1966 | |
0de23977 | 1967 | *offset = drm_vma_node_offset_addr(&obj->base.vma_node); |
de151cf6 | 1968 | |
1d7cfea1 | 1969 | out: |
f8c417cd | 1970 | i915_gem_object_put(obj); |
1d7cfea1 | 1971 | unlock: |
de151cf6 | 1972 | mutex_unlock(&dev->struct_mutex); |
1d7cfea1 | 1973 | return ret; |
de151cf6 JB |
1974 | } |
1975 | ||
ff72145b DA |
1976 | /** |
1977 | * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing | |
1978 | * @dev: DRM device | |
1979 | * @data: GTT mapping ioctl data | |
1980 | * @file: GEM object info | |
1981 | * | |
1982 | * Simply returns the fake offset to userspace so it can mmap it. | |
1983 | * The mmap call will end up in drm_gem_mmap(), which will set things | |
1984 | * up so we can get faults in the handler above. | |
1985 | * | |
1986 | * The fault handler will take care of binding the object into the GTT | |
1987 | * (since it may have been evicted to make room for something), allocating | |
1988 | * a fence register, and mapping the appropriate aperture address into | |
1989 | * userspace. | |
1990 | */ | |
1991 | int | |
1992 | i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data, | |
1993 | struct drm_file *file) | |
1994 | { | |
1995 | struct drm_i915_gem_mmap_gtt *args = data; | |
1996 | ||
da6b51d0 | 1997 | return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset); |
ff72145b DA |
1998 | } |
1999 | ||
225067ee DV |
2000 | /* Immediately discard the backing storage */ |
2001 | static void | |
2002 | i915_gem_object_truncate(struct drm_i915_gem_object *obj) | |
e5281ccd | 2003 | { |
4d6294bf | 2004 | i915_gem_object_free_mmap_offset(obj); |
1286ff73 | 2005 | |
4d6294bf CW |
2006 | if (obj->base.filp == NULL) |
2007 | return; | |
e5281ccd | 2008 | |
225067ee DV |
2009 | /* Our goal here is to return as much of the memory as |
2010 | * is possible back to the system as we are called from OOM. | |
2011 | * To do this we must instruct the shmfs to drop all of its | |
2012 | * backing pages, *now*. | |
2013 | */ | |
5537252b | 2014 | shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1); |
225067ee DV |
2015 | obj->madv = __I915_MADV_PURGED; |
2016 | } | |
e5281ccd | 2017 | |
5537252b CW |
2018 | /* Try to discard unwanted pages */ |
2019 | static void | |
2020 | i915_gem_object_invalidate(struct drm_i915_gem_object *obj) | |
225067ee | 2021 | { |
5537252b CW |
2022 | struct address_space *mapping; |
2023 | ||
2024 | switch (obj->madv) { | |
2025 | case I915_MADV_DONTNEED: | |
2026 | i915_gem_object_truncate(obj); | |
2027 | case __I915_MADV_PURGED: | |
2028 | return; | |
2029 | } | |
2030 | ||
2031 | if (obj->base.filp == NULL) | |
2032 | return; | |
2033 | ||
2034 | mapping = file_inode(obj->base.filp)->i_mapping, | |
2035 | invalidate_mapping_pages(mapping, 0, (loff_t)-1); | |
e5281ccd CW |
2036 | } |
2037 | ||
5cdf5881 | 2038 | static void |
05394f39 | 2039 | i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj) |
673a394b | 2040 | { |
85d1225e DG |
2041 | struct sgt_iter sgt_iter; |
2042 | struct page *page; | |
90797e6d | 2043 | int ret; |
1286ff73 | 2044 | |
05394f39 | 2045 | BUG_ON(obj->madv == __I915_MADV_PURGED); |
673a394b | 2046 | |
6c085a72 | 2047 | ret = i915_gem_object_set_to_cpu_domain(obj, true); |
f4457ae7 | 2048 | if (WARN_ON(ret)) { |
6c085a72 CW |
2049 | /* In the event of a disaster, abandon all caches and |
2050 | * hope for the best. | |
2051 | */ | |
2c22569b | 2052 | i915_gem_clflush_object(obj, true); |
6c085a72 CW |
2053 | obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU; |
2054 | } | |
2055 | ||
e2273302 ID |
2056 | i915_gem_gtt_finish_object(obj); |
2057 | ||
6dacfd2f | 2058 | if (i915_gem_object_needs_bit17_swizzle(obj)) |
280b713b EA |
2059 | i915_gem_object_save_bit_17_swizzle(obj); |
2060 | ||
05394f39 CW |
2061 | if (obj->madv == I915_MADV_DONTNEED) |
2062 | obj->dirty = 0; | |
3ef94daa | 2063 | |
85d1225e | 2064 | for_each_sgt_page(page, sgt_iter, obj->pages) { |
05394f39 | 2065 | if (obj->dirty) |
9da3da66 | 2066 | set_page_dirty(page); |
3ef94daa | 2067 | |
05394f39 | 2068 | if (obj->madv == I915_MADV_WILLNEED) |
9da3da66 | 2069 | mark_page_accessed(page); |
3ef94daa | 2070 | |
09cbfeaf | 2071 | put_page(page); |
3ef94daa | 2072 | } |
05394f39 | 2073 | obj->dirty = 0; |
673a394b | 2074 | |
9da3da66 CW |
2075 | sg_free_table(obj->pages); |
2076 | kfree(obj->pages); | |
37e680a1 | 2077 | } |
6c085a72 | 2078 | |
dd624afd | 2079 | int |
37e680a1 CW |
2080 | i915_gem_object_put_pages(struct drm_i915_gem_object *obj) |
2081 | { | |
2082 | const struct drm_i915_gem_object_ops *ops = obj->ops; | |
2083 | ||
2f745ad3 | 2084 | if (obj->pages == NULL) |
37e680a1 CW |
2085 | return 0; |
2086 | ||
a5570178 CW |
2087 | if (obj->pages_pin_count) |
2088 | return -EBUSY; | |
2089 | ||
15717de2 | 2090 | GEM_BUG_ON(obj->bind_count); |
3e123027 | 2091 | |
a2165e31 CW |
2092 | /* ->put_pages might need to allocate memory for the bit17 swizzle |
2093 | * array, hence protect them from being reaped by removing them from gtt | |
2094 | * lists early. */ | |
35c20a60 | 2095 | list_del(&obj->global_list); |
a2165e31 | 2096 | |
0a798eb9 | 2097 | if (obj->mapping) { |
fb8621d3 CW |
2098 | if (is_vmalloc_addr(obj->mapping)) |
2099 | vunmap(obj->mapping); | |
2100 | else | |
2101 | kunmap(kmap_to_page(obj->mapping)); | |
0a798eb9 CW |
2102 | obj->mapping = NULL; |
2103 | } | |
2104 | ||
37e680a1 | 2105 | ops->put_pages(obj); |
05394f39 | 2106 | obj->pages = NULL; |
37e680a1 | 2107 | |
5537252b | 2108 | i915_gem_object_invalidate(obj); |
6c085a72 CW |
2109 | |
2110 | return 0; | |
2111 | } | |
2112 | ||
37e680a1 | 2113 | static int |
6c085a72 | 2114 | i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj) |
e5281ccd | 2115 | { |
fac5e23e | 2116 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
e5281ccd CW |
2117 | int page_count, i; |
2118 | struct address_space *mapping; | |
9da3da66 CW |
2119 | struct sg_table *st; |
2120 | struct scatterlist *sg; | |
85d1225e | 2121 | struct sgt_iter sgt_iter; |
e5281ccd | 2122 | struct page *page; |
90797e6d | 2123 | unsigned long last_pfn = 0; /* suppress gcc warning */ |
e2273302 | 2124 | int ret; |
6c085a72 | 2125 | gfp_t gfp; |
e5281ccd | 2126 | |
6c085a72 CW |
2127 | /* Assert that the object is not currently in any GPU domain. As it |
2128 | * wasn't in the GTT, there shouldn't be any way it could have been in | |
2129 | * a GPU cache | |
2130 | */ | |
2131 | BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS); | |
2132 | BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS); | |
2133 | ||
9da3da66 CW |
2134 | st = kmalloc(sizeof(*st), GFP_KERNEL); |
2135 | if (st == NULL) | |
2136 | return -ENOMEM; | |
2137 | ||
05394f39 | 2138 | page_count = obj->base.size / PAGE_SIZE; |
9da3da66 | 2139 | if (sg_alloc_table(st, page_count, GFP_KERNEL)) { |
9da3da66 | 2140 | kfree(st); |
e5281ccd | 2141 | return -ENOMEM; |
9da3da66 | 2142 | } |
e5281ccd | 2143 | |
9da3da66 CW |
2144 | /* Get the list of pages out of our struct file. They'll be pinned |
2145 | * at this point until we release them. | |
2146 | * | |
2147 | * Fail silently without starting the shrinker | |
2148 | */ | |
496ad9aa | 2149 | mapping = file_inode(obj->base.filp)->i_mapping; |
c62d2555 | 2150 | gfp = mapping_gfp_constraint(mapping, ~(__GFP_IO | __GFP_RECLAIM)); |
d0164adc | 2151 | gfp |= __GFP_NORETRY | __GFP_NOWARN; |
90797e6d ID |
2152 | sg = st->sgl; |
2153 | st->nents = 0; | |
2154 | for (i = 0; i < page_count; i++) { | |
6c085a72 CW |
2155 | page = shmem_read_mapping_page_gfp(mapping, i, gfp); |
2156 | if (IS_ERR(page)) { | |
21ab4e74 CW |
2157 | i915_gem_shrink(dev_priv, |
2158 | page_count, | |
2159 | I915_SHRINK_BOUND | | |
2160 | I915_SHRINK_UNBOUND | | |
2161 | I915_SHRINK_PURGEABLE); | |
6c085a72 CW |
2162 | page = shmem_read_mapping_page_gfp(mapping, i, gfp); |
2163 | } | |
2164 | if (IS_ERR(page)) { | |
2165 | /* We've tried hard to allocate the memory by reaping | |
2166 | * our own buffer, now let the real VM do its job and | |
2167 | * go down in flames if truly OOM. | |
2168 | */ | |
6c085a72 | 2169 | i915_gem_shrink_all(dev_priv); |
f461d1be | 2170 | page = shmem_read_mapping_page(mapping, i); |
e2273302 ID |
2171 | if (IS_ERR(page)) { |
2172 | ret = PTR_ERR(page); | |
6c085a72 | 2173 | goto err_pages; |
e2273302 | 2174 | } |
6c085a72 | 2175 | } |
426729dc KRW |
2176 | #ifdef CONFIG_SWIOTLB |
2177 | if (swiotlb_nr_tbl()) { | |
2178 | st->nents++; | |
2179 | sg_set_page(sg, page, PAGE_SIZE, 0); | |
2180 | sg = sg_next(sg); | |
2181 | continue; | |
2182 | } | |
2183 | #endif | |
90797e6d ID |
2184 | if (!i || page_to_pfn(page) != last_pfn + 1) { |
2185 | if (i) | |
2186 | sg = sg_next(sg); | |
2187 | st->nents++; | |
2188 | sg_set_page(sg, page, PAGE_SIZE, 0); | |
2189 | } else { | |
2190 | sg->length += PAGE_SIZE; | |
2191 | } | |
2192 | last_pfn = page_to_pfn(page); | |
3bbbe706 DV |
2193 | |
2194 | /* Check that the i965g/gm workaround works. */ | |
2195 | WARN_ON((gfp & __GFP_DMA32) && (last_pfn >= 0x00100000UL)); | |
e5281ccd | 2196 | } |
426729dc KRW |
2197 | #ifdef CONFIG_SWIOTLB |
2198 | if (!swiotlb_nr_tbl()) | |
2199 | #endif | |
2200 | sg_mark_end(sg); | |
74ce6b6c CW |
2201 | obj->pages = st; |
2202 | ||
e2273302 ID |
2203 | ret = i915_gem_gtt_prepare_object(obj); |
2204 | if (ret) | |
2205 | goto err_pages; | |
2206 | ||
6dacfd2f | 2207 | if (i915_gem_object_needs_bit17_swizzle(obj)) |
e5281ccd CW |
2208 | i915_gem_object_do_bit_17_swizzle(obj); |
2209 | ||
656bfa3a DV |
2210 | if (obj->tiling_mode != I915_TILING_NONE && |
2211 | dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) | |
2212 | i915_gem_object_pin_pages(obj); | |
2213 | ||
e5281ccd CW |
2214 | return 0; |
2215 | ||
2216 | err_pages: | |
90797e6d | 2217 | sg_mark_end(sg); |
85d1225e DG |
2218 | for_each_sgt_page(page, sgt_iter, st) |
2219 | put_page(page); | |
9da3da66 CW |
2220 | sg_free_table(st); |
2221 | kfree(st); | |
0820baf3 CW |
2222 | |
2223 | /* shmemfs first checks if there is enough memory to allocate the page | |
2224 | * and reports ENOSPC should there be insufficient, along with the usual | |
2225 | * ENOMEM for a genuine allocation failure. | |
2226 | * | |
2227 | * We use ENOSPC in our driver to mean that we have run out of aperture | |
2228 | * space and so want to translate the error from shmemfs back to our | |
2229 | * usual understanding of ENOMEM. | |
2230 | */ | |
e2273302 ID |
2231 | if (ret == -ENOSPC) |
2232 | ret = -ENOMEM; | |
2233 | ||
2234 | return ret; | |
673a394b EA |
2235 | } |
2236 | ||
37e680a1 CW |
2237 | /* Ensure that the associated pages are gathered from the backing storage |
2238 | * and pinned into our object. i915_gem_object_get_pages() may be called | |
2239 | * multiple times before they are released by a single call to | |
2240 | * i915_gem_object_put_pages() - once the pages are no longer referenced | |
2241 | * either as a result of memory pressure (reaping pages under the shrinker) | |
2242 | * or as the object is itself released. | |
2243 | */ | |
2244 | int | |
2245 | i915_gem_object_get_pages(struct drm_i915_gem_object *obj) | |
2246 | { | |
fac5e23e | 2247 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
37e680a1 CW |
2248 | const struct drm_i915_gem_object_ops *ops = obj->ops; |
2249 | int ret; | |
2250 | ||
2f745ad3 | 2251 | if (obj->pages) |
37e680a1 CW |
2252 | return 0; |
2253 | ||
43e28f09 | 2254 | if (obj->madv != I915_MADV_WILLNEED) { |
bd9b6a4e | 2255 | DRM_DEBUG("Attempting to obtain a purgeable object\n"); |
8c99e57d | 2256 | return -EFAULT; |
43e28f09 CW |
2257 | } |
2258 | ||
a5570178 CW |
2259 | BUG_ON(obj->pages_pin_count); |
2260 | ||
37e680a1 CW |
2261 | ret = ops->get_pages(obj); |
2262 | if (ret) | |
2263 | return ret; | |
2264 | ||
35c20a60 | 2265 | list_add_tail(&obj->global_list, &dev_priv->mm.unbound_list); |
ee286370 CW |
2266 | |
2267 | obj->get_page.sg = obj->pages->sgl; | |
2268 | obj->get_page.last = 0; | |
2269 | ||
37e680a1 | 2270 | return 0; |
673a394b EA |
2271 | } |
2272 | ||
dd6034c6 DG |
2273 | /* The 'mapping' part of i915_gem_object_pin_map() below */ |
2274 | static void *i915_gem_object_map(const struct drm_i915_gem_object *obj) | |
2275 | { | |
2276 | unsigned long n_pages = obj->base.size >> PAGE_SHIFT; | |
2277 | struct sg_table *sgt = obj->pages; | |
85d1225e DG |
2278 | struct sgt_iter sgt_iter; |
2279 | struct page *page; | |
b338fa47 DG |
2280 | struct page *stack_pages[32]; |
2281 | struct page **pages = stack_pages; | |
dd6034c6 DG |
2282 | unsigned long i = 0; |
2283 | void *addr; | |
2284 | ||
2285 | /* A single page can always be kmapped */ | |
2286 | if (n_pages == 1) | |
2287 | return kmap(sg_page(sgt->sgl)); | |
2288 | ||
b338fa47 DG |
2289 | if (n_pages > ARRAY_SIZE(stack_pages)) { |
2290 | /* Too big for stack -- allocate temporary array instead */ | |
2291 | pages = drm_malloc_gfp(n_pages, sizeof(*pages), GFP_TEMPORARY); | |
2292 | if (!pages) | |
2293 | return NULL; | |
2294 | } | |
dd6034c6 | 2295 | |
85d1225e DG |
2296 | for_each_sgt_page(page, sgt_iter, sgt) |
2297 | pages[i++] = page; | |
dd6034c6 DG |
2298 | |
2299 | /* Check that we have the expected number of pages */ | |
2300 | GEM_BUG_ON(i != n_pages); | |
2301 | ||
2302 | addr = vmap(pages, n_pages, 0, PAGE_KERNEL); | |
2303 | ||
b338fa47 DG |
2304 | if (pages != stack_pages) |
2305 | drm_free_large(pages); | |
dd6034c6 DG |
2306 | |
2307 | return addr; | |
2308 | } | |
2309 | ||
2310 | /* get, pin, and map the pages of the object into kernel space */ | |
0a798eb9 CW |
2311 | void *i915_gem_object_pin_map(struct drm_i915_gem_object *obj) |
2312 | { | |
2313 | int ret; | |
2314 | ||
2315 | lockdep_assert_held(&obj->base.dev->struct_mutex); | |
2316 | ||
2317 | ret = i915_gem_object_get_pages(obj); | |
2318 | if (ret) | |
2319 | return ERR_PTR(ret); | |
2320 | ||
2321 | i915_gem_object_pin_pages(obj); | |
2322 | ||
dd6034c6 DG |
2323 | if (!obj->mapping) { |
2324 | obj->mapping = i915_gem_object_map(obj); | |
2325 | if (!obj->mapping) { | |
0a798eb9 CW |
2326 | i915_gem_object_unpin_pages(obj); |
2327 | return ERR_PTR(-ENOMEM); | |
2328 | } | |
2329 | } | |
2330 | ||
2331 | return obj->mapping; | |
2332 | } | |
2333 | ||
b4716185 | 2334 | static void |
fa545cbf CW |
2335 | i915_gem_object_retire__write(struct i915_gem_active *active, |
2336 | struct drm_i915_gem_request *request) | |
e2d05a8b | 2337 | { |
fa545cbf CW |
2338 | struct drm_i915_gem_object *obj = |
2339 | container_of(active, struct drm_i915_gem_object, last_write); | |
b4716185 | 2340 | |
de152b62 | 2341 | intel_fb_obj_flush(obj, true, ORIGIN_CS); |
e2d05a8b BW |
2342 | } |
2343 | ||
caea7476 | 2344 | static void |
fa545cbf CW |
2345 | i915_gem_object_retire__read(struct i915_gem_active *active, |
2346 | struct drm_i915_gem_request *request) | |
ce44b0ea | 2347 | { |
fa545cbf CW |
2348 | int idx = request->engine->id; |
2349 | struct drm_i915_gem_object *obj = | |
2350 | container_of(active, struct drm_i915_gem_object, last_read[idx]); | |
ce44b0ea | 2351 | |
573adb39 | 2352 | GEM_BUG_ON(!i915_gem_object_has_active_engine(obj, idx)); |
b4716185 | 2353 | |
573adb39 CW |
2354 | i915_gem_object_clear_active(obj, idx); |
2355 | if (i915_gem_object_is_active(obj)) | |
b4716185 | 2356 | return; |
caea7476 | 2357 | |
6c246959 CW |
2358 | /* Bump our place on the bound list to keep it roughly in LRU order |
2359 | * so that we don't steal from recently used but inactive objects | |
2360 | * (unless we are forced to ofc!) | |
2361 | */ | |
b0decaf7 CW |
2362 | if (obj->bind_count) |
2363 | list_move_tail(&obj->global_list, | |
2364 | &request->i915->mm.bound_list); | |
caea7476 | 2365 | |
f8c417cd | 2366 | i915_gem_object_put(obj); |
c8725f3d CW |
2367 | } |
2368 | ||
7b4d3a16 | 2369 | static bool i915_context_is_banned(const struct i915_gem_context *ctx) |
be62acb4 | 2370 | { |
44e2c070 | 2371 | unsigned long elapsed; |
be62acb4 | 2372 | |
44e2c070 | 2373 | if (ctx->hang_stats.banned) |
be62acb4 MK |
2374 | return true; |
2375 | ||
7b4d3a16 | 2376 | elapsed = get_seconds() - ctx->hang_stats.guilty_ts; |
676fa572 CW |
2377 | if (ctx->hang_stats.ban_period_seconds && |
2378 | elapsed <= ctx->hang_stats.ban_period_seconds) { | |
7b4d3a16 CW |
2379 | DRM_DEBUG("context hanging too fast, banning!\n"); |
2380 | return true; | |
be62acb4 MK |
2381 | } |
2382 | ||
2383 | return false; | |
2384 | } | |
2385 | ||
7b4d3a16 | 2386 | static void i915_set_reset_status(struct i915_gem_context *ctx, |
b6b0fac0 | 2387 | const bool guilty) |
aa60c664 | 2388 | { |
7b4d3a16 | 2389 | struct i915_ctx_hang_stats *hs = &ctx->hang_stats; |
44e2c070 MK |
2390 | |
2391 | if (guilty) { | |
7b4d3a16 | 2392 | hs->banned = i915_context_is_banned(ctx); |
44e2c070 MK |
2393 | hs->batch_active++; |
2394 | hs->guilty_ts = get_seconds(); | |
2395 | } else { | |
2396 | hs->batch_pending++; | |
aa60c664 MK |
2397 | } |
2398 | } | |
2399 | ||
8d9fc7fd | 2400 | struct drm_i915_gem_request * |
0bc40be8 | 2401 | i915_gem_find_active_request(struct intel_engine_cs *engine) |
9375e446 | 2402 | { |
4db080f9 CW |
2403 | struct drm_i915_gem_request *request; |
2404 | ||
f69a02c9 CW |
2405 | /* We are called by the error capture and reset at a random |
2406 | * point in time. In particular, note that neither is crucially | |
2407 | * ordered with an interrupt. After a hang, the GPU is dead and we | |
2408 | * assume that no more writes can happen (we waited long enough for | |
2409 | * all writes that were in transaction to be flushed) - adding an | |
2410 | * extra delay for a recent interrupt is pointless. Hence, we do | |
2411 | * not need an engine->irq_seqno_barrier() before the seqno reads. | |
2412 | */ | |
efdf7c06 | 2413 | list_for_each_entry(request, &engine->request_list, link) { |
f69a02c9 | 2414 | if (i915_gem_request_completed(request)) |
4db080f9 | 2415 | continue; |
aa60c664 | 2416 | |
b6b0fac0 | 2417 | return request; |
4db080f9 | 2418 | } |
b6b0fac0 MK |
2419 | |
2420 | return NULL; | |
2421 | } | |
2422 | ||
7b4d3a16 | 2423 | static void i915_gem_reset_engine_status(struct intel_engine_cs *engine) |
b6b0fac0 MK |
2424 | { |
2425 | struct drm_i915_gem_request *request; | |
2426 | bool ring_hung; | |
2427 | ||
0bc40be8 | 2428 | request = i915_gem_find_active_request(engine); |
b6b0fac0 MK |
2429 | if (request == NULL) |
2430 | return; | |
2431 | ||
0bc40be8 | 2432 | ring_hung = engine->hangcheck.score >= HANGCHECK_SCORE_RING_HUNG; |
b6b0fac0 | 2433 | |
7b4d3a16 | 2434 | i915_set_reset_status(request->ctx, ring_hung); |
efdf7c06 | 2435 | list_for_each_entry_continue(request, &engine->request_list, link) |
7b4d3a16 | 2436 | i915_set_reset_status(request->ctx, false); |
4db080f9 | 2437 | } |
aa60c664 | 2438 | |
7b4d3a16 | 2439 | static void i915_gem_reset_engine_cleanup(struct intel_engine_cs *engine) |
4db080f9 | 2440 | { |
7e37f889 | 2441 | struct intel_ring *ring; |
608c1a52 | 2442 | |
c4b0930b CW |
2443 | /* Mark all pending requests as complete so that any concurrent |
2444 | * (lockless) lookup doesn't try and wait upon the request as we | |
2445 | * reset it. | |
2446 | */ | |
7e37f889 | 2447 | intel_engine_init_seqno(engine, engine->last_submitted_seqno); |
c4b0930b | 2448 | |
dcb4c12a OM |
2449 | /* |
2450 | * Clear the execlists queue up before freeing the requests, as those | |
2451 | * are the ones that keep the context and ringbuffer backing objects | |
2452 | * pinned in place. | |
2453 | */ | |
dcb4c12a | 2454 | |
7de1691a | 2455 | if (i915.enable_execlists) { |
27af5eea TU |
2456 | /* Ensure irq handler finishes or is cancelled. */ |
2457 | tasklet_kill(&engine->irq_tasklet); | |
1197b4f2 | 2458 | |
e39d42fa | 2459 | intel_execlists_cancel_requests(engine); |
dcb4c12a OM |
2460 | } |
2461 | ||
1d62beea BW |
2462 | /* |
2463 | * We must free the requests after all the corresponding objects have | |
2464 | * been moved off active lists. Which is the same order as the normal | |
2465 | * retire_requests function does. This is important if object hold | |
2466 | * implicit references on things like e.g. ppgtt address spaces through | |
2467 | * the request. | |
2468 | */ | |
05235c53 | 2469 | if (!list_empty(&engine->request_list)) { |
1d62beea BW |
2470 | struct drm_i915_gem_request *request; |
2471 | ||
05235c53 CW |
2472 | request = list_last_entry(&engine->request_list, |
2473 | struct drm_i915_gem_request, | |
efdf7c06 | 2474 | link); |
1d62beea | 2475 | |
05235c53 | 2476 | i915_gem_request_retire_upto(request); |
1d62beea | 2477 | } |
608c1a52 CW |
2478 | |
2479 | /* Having flushed all requests from all queues, we know that all | |
2480 | * ringbuffers must now be empty. However, since we do not reclaim | |
2481 | * all space when retiring the request (to prevent HEADs colliding | |
2482 | * with rapid ringbuffer wraparound) the amount of available space | |
2483 | * upon reset is less than when we start. Do one more pass over | |
2484 | * all the ringbuffers to reset last_retired_head. | |
2485 | */ | |
7e37f889 CW |
2486 | list_for_each_entry(ring, &engine->buffers, link) { |
2487 | ring->last_retired_head = ring->tail; | |
2488 | intel_ring_update_space(ring); | |
608c1a52 | 2489 | } |
2ed53a94 | 2490 | |
b913b33c | 2491 | engine->i915->gt.active_engines &= ~intel_engine_flag(engine); |
673a394b EA |
2492 | } |
2493 | ||
069efc1d | 2494 | void i915_gem_reset(struct drm_device *dev) |
673a394b | 2495 | { |
fac5e23e | 2496 | struct drm_i915_private *dev_priv = to_i915(dev); |
e2f80391 | 2497 | struct intel_engine_cs *engine; |
673a394b | 2498 | |
4db080f9 CW |
2499 | /* |
2500 | * Before we free the objects from the requests, we need to inspect | |
2501 | * them for finding the guilty party. As the requests only borrow | |
2502 | * their reference to the objects, the inspection must be done first. | |
2503 | */ | |
b4ac5afc | 2504 | for_each_engine(engine, dev_priv) |
7b4d3a16 | 2505 | i915_gem_reset_engine_status(engine); |
4db080f9 | 2506 | |
b4ac5afc | 2507 | for_each_engine(engine, dev_priv) |
7b4d3a16 | 2508 | i915_gem_reset_engine_cleanup(engine); |
b913b33c | 2509 | mod_delayed_work(dev_priv->wq, &dev_priv->gt.idle_work, 0); |
dfaae392 | 2510 | |
acce9ffa BW |
2511 | i915_gem_context_reset(dev); |
2512 | ||
19b2dbde | 2513 | i915_gem_restore_fences(dev); |
673a394b EA |
2514 | } |
2515 | ||
75ef9da2 | 2516 | static void |
673a394b EA |
2517 | i915_gem_retire_work_handler(struct work_struct *work) |
2518 | { | |
b29c19b6 | 2519 | struct drm_i915_private *dev_priv = |
67d97da3 | 2520 | container_of(work, typeof(*dev_priv), gt.retire_work.work); |
91c8a326 | 2521 | struct drm_device *dev = &dev_priv->drm; |
673a394b | 2522 | |
891b48cf | 2523 | /* Come back later if the device is busy... */ |
b29c19b6 | 2524 | if (mutex_trylock(&dev->struct_mutex)) { |
67d97da3 | 2525 | i915_gem_retire_requests(dev_priv); |
b29c19b6 | 2526 | mutex_unlock(&dev->struct_mutex); |
673a394b | 2527 | } |
67d97da3 CW |
2528 | |
2529 | /* Keep the retire handler running until we are finally idle. | |
2530 | * We do not need to do this test under locking as in the worst-case | |
2531 | * we queue the retire worker once too often. | |
2532 | */ | |
c9615613 CW |
2533 | if (READ_ONCE(dev_priv->gt.awake)) { |
2534 | i915_queue_hangcheck(dev_priv); | |
67d97da3 CW |
2535 | queue_delayed_work(dev_priv->wq, |
2536 | &dev_priv->gt.retire_work, | |
bcb45086 | 2537 | round_jiffies_up_relative(HZ)); |
c9615613 | 2538 | } |
b29c19b6 | 2539 | } |
0a58705b | 2540 | |
b29c19b6 CW |
2541 | static void |
2542 | i915_gem_idle_work_handler(struct work_struct *work) | |
2543 | { | |
2544 | struct drm_i915_private *dev_priv = | |
67d97da3 | 2545 | container_of(work, typeof(*dev_priv), gt.idle_work.work); |
91c8a326 | 2546 | struct drm_device *dev = &dev_priv->drm; |
b4ac5afc | 2547 | struct intel_engine_cs *engine; |
67d97da3 CW |
2548 | unsigned int stuck_engines; |
2549 | bool rearm_hangcheck; | |
2550 | ||
2551 | if (!READ_ONCE(dev_priv->gt.awake)) | |
2552 | return; | |
2553 | ||
2554 | if (READ_ONCE(dev_priv->gt.active_engines)) | |
2555 | return; | |
2556 | ||
2557 | rearm_hangcheck = | |
2558 | cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work); | |
2559 | ||
2560 | if (!mutex_trylock(&dev->struct_mutex)) { | |
2561 | /* Currently busy, come back later */ | |
2562 | mod_delayed_work(dev_priv->wq, | |
2563 | &dev_priv->gt.idle_work, | |
2564 | msecs_to_jiffies(50)); | |
2565 | goto out_rearm; | |
2566 | } | |
2567 | ||
2568 | if (dev_priv->gt.active_engines) | |
2569 | goto out_unlock; | |
b29c19b6 | 2570 | |
b4ac5afc | 2571 | for_each_engine(engine, dev_priv) |
67d97da3 | 2572 | i915_gem_batch_pool_fini(&engine->batch_pool); |
35c94185 | 2573 | |
67d97da3 CW |
2574 | GEM_BUG_ON(!dev_priv->gt.awake); |
2575 | dev_priv->gt.awake = false; | |
2576 | rearm_hangcheck = false; | |
30ecad77 | 2577 | |
2529d570 CW |
2578 | /* As we have disabled hangcheck, we need to unstick any waiters still |
2579 | * hanging around. However, as we may be racing against the interrupt | |
2580 | * handler or the waiters themselves, we skip enabling the fake-irq. | |
2581 | */ | |
67d97da3 | 2582 | stuck_engines = intel_kick_waiters(dev_priv); |
2529d570 CW |
2583 | if (unlikely(stuck_engines)) |
2584 | DRM_DEBUG_DRIVER("kicked stuck waiters (%x)...missed irq?\n", | |
2585 | stuck_engines); | |
35c94185 | 2586 | |
67d97da3 CW |
2587 | if (INTEL_GEN(dev_priv) >= 6) |
2588 | gen6_rps_idle(dev_priv); | |
2589 | intel_runtime_pm_put(dev_priv); | |
2590 | out_unlock: | |
2591 | mutex_unlock(&dev->struct_mutex); | |
b29c19b6 | 2592 | |
67d97da3 CW |
2593 | out_rearm: |
2594 | if (rearm_hangcheck) { | |
2595 | GEM_BUG_ON(!dev_priv->gt.awake); | |
2596 | i915_queue_hangcheck(dev_priv); | |
35c94185 | 2597 | } |
673a394b EA |
2598 | } |
2599 | ||
b1f788c6 CW |
2600 | void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file) |
2601 | { | |
2602 | struct drm_i915_gem_object *obj = to_intel_bo(gem); | |
2603 | struct drm_i915_file_private *fpriv = file->driver_priv; | |
2604 | struct i915_vma *vma, *vn; | |
2605 | ||
2606 | mutex_lock(&obj->base.dev->struct_mutex); | |
2607 | list_for_each_entry_safe(vma, vn, &obj->vma_list, obj_link) | |
2608 | if (vma->vm->file == fpriv) | |
2609 | i915_vma_close(vma); | |
2610 | mutex_unlock(&obj->base.dev->struct_mutex); | |
2611 | } | |
2612 | ||
23ba4fd0 BW |
2613 | /** |
2614 | * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT | |
14bb2c11 TU |
2615 | * @dev: drm device pointer |
2616 | * @data: ioctl data blob | |
2617 | * @file: drm file pointer | |
23ba4fd0 BW |
2618 | * |
2619 | * Returns 0 if successful, else an error is returned with the remaining time in | |
2620 | * the timeout parameter. | |
2621 | * -ETIME: object is still busy after timeout | |
2622 | * -ERESTARTSYS: signal interrupted the wait | |
2623 | * -ENONENT: object doesn't exist | |
2624 | * Also possible, but rare: | |
2625 | * -EAGAIN: GPU wedged | |
2626 | * -ENOMEM: damn | |
2627 | * -ENODEV: Internal IRQ fail | |
2628 | * -E?: The add request failed | |
2629 | * | |
2630 | * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any | |
2631 | * non-zero timeout parameter the wait ioctl will wait for the given number of | |
2632 | * nanoseconds on an object becoming unbusy. Since the wait itself does so | |
2633 | * without holding struct_mutex the object may become re-busied before this | |
2634 | * function completes. A similar but shorter * race condition exists in the busy | |
2635 | * ioctl | |
2636 | */ | |
2637 | int | |
2638 | i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file) | |
2639 | { | |
2640 | struct drm_i915_gem_wait *args = data; | |
2641 | struct drm_i915_gem_object *obj; | |
27c01aae | 2642 | struct drm_i915_gem_request *requests[I915_NUM_ENGINES]; |
b4716185 CW |
2643 | int i, n = 0; |
2644 | int ret; | |
23ba4fd0 | 2645 | |
11b5d511 DV |
2646 | if (args->flags != 0) |
2647 | return -EINVAL; | |
2648 | ||
23ba4fd0 BW |
2649 | ret = i915_mutex_lock_interruptible(dev); |
2650 | if (ret) | |
2651 | return ret; | |
2652 | ||
03ac0642 CW |
2653 | obj = i915_gem_object_lookup(file, args->bo_handle); |
2654 | if (!obj) { | |
23ba4fd0 BW |
2655 | mutex_unlock(&dev->struct_mutex); |
2656 | return -ENOENT; | |
2657 | } | |
2658 | ||
573adb39 | 2659 | if (!i915_gem_object_is_active(obj)) |
97b2a6a1 | 2660 | goto out; |
23ba4fd0 | 2661 | |
666796da | 2662 | for (i = 0; i < I915_NUM_ENGINES; i++) { |
27c01aae | 2663 | struct drm_i915_gem_request *req; |
b4716185 | 2664 | |
d72d908b CW |
2665 | req = i915_gem_active_get(&obj->last_read[i], |
2666 | &obj->base.dev->struct_mutex); | |
27c01aae CW |
2667 | if (req) |
2668 | requests[n++] = req; | |
b4716185 CW |
2669 | } |
2670 | ||
21c310f2 CW |
2671 | out: |
2672 | i915_gem_object_put(obj); | |
23ba4fd0 BW |
2673 | mutex_unlock(&dev->struct_mutex); |
2674 | ||
b4716185 CW |
2675 | for (i = 0; i < n; i++) { |
2676 | if (ret == 0) | |
776f3236 CW |
2677 | ret = i915_wait_request(requests[i], true, |
2678 | args->timeout_ns > 0 ? &args->timeout_ns : NULL, | |
2679 | to_rps_client(file)); | |
27c01aae | 2680 | i915_gem_request_put(requests[i]); |
b4716185 | 2681 | } |
ff865885 | 2682 | return ret; |
23ba4fd0 BW |
2683 | } |
2684 | ||
b4716185 | 2685 | static int |
fa545cbf | 2686 | __i915_gem_object_sync(struct drm_i915_gem_request *to, |
8e637178 | 2687 | struct drm_i915_gem_request *from) |
b4716185 | 2688 | { |
b4716185 CW |
2689 | int ret; |
2690 | ||
8e637178 | 2691 | if (to->engine == from->engine) |
b4716185 CW |
2692 | return 0; |
2693 | ||
39df9190 | 2694 | if (!i915.semaphores) { |
776f3236 CW |
2695 | ret = i915_wait_request(from, |
2696 | from->i915->mm.interruptible, | |
2697 | NULL, | |
2698 | NO_WAITBOOST); | |
b4716185 CW |
2699 | if (ret) |
2700 | return ret; | |
b4716185 | 2701 | } else { |
8e637178 | 2702 | int idx = intel_engine_sync_index(from->engine, to->engine); |
ddf07be7 | 2703 | if (from->fence.seqno <= from->engine->semaphore.sync_seqno[idx]) |
b4716185 CW |
2704 | return 0; |
2705 | ||
8e637178 | 2706 | trace_i915_gem_ring_sync_to(to, from); |
ddf07be7 | 2707 | ret = to->engine->semaphore.sync_to(to, from); |
b4716185 CW |
2708 | if (ret) |
2709 | return ret; | |
2710 | ||
ddf07be7 | 2711 | from->engine->semaphore.sync_seqno[idx] = from->fence.seqno; |
b4716185 CW |
2712 | } |
2713 | ||
2714 | return 0; | |
2715 | } | |
2716 | ||
5816d648 BW |
2717 | /** |
2718 | * i915_gem_object_sync - sync an object to a ring. | |
2719 | * | |
2720 | * @obj: object which may be in use on another ring. | |
8e637178 | 2721 | * @to: request we are wishing to use |
5816d648 BW |
2722 | * |
2723 | * This code is meant to abstract object synchronization with the GPU. | |
8e637178 CW |
2724 | * Conceptually we serialise writes between engines inside the GPU. |
2725 | * We only allow one engine to write into a buffer at any time, but | |
2726 | * multiple readers. To ensure each has a coherent view of memory, we must: | |
b4716185 CW |
2727 | * |
2728 | * - If there is an outstanding write request to the object, the new | |
2729 | * request must wait for it to complete (either CPU or in hw, requests | |
2730 | * on the same ring will be naturally ordered). | |
2731 | * | |
2732 | * - If we are a write request (pending_write_domain is set), the new | |
2733 | * request must wait for outstanding read requests to complete. | |
5816d648 BW |
2734 | * |
2735 | * Returns 0 if successful, else propagates up the lower layer error. | |
2736 | */ | |
2911a35b BW |
2737 | int |
2738 | i915_gem_object_sync(struct drm_i915_gem_object *obj, | |
8e637178 | 2739 | struct drm_i915_gem_request *to) |
2911a35b | 2740 | { |
8cac6f6c CW |
2741 | struct i915_gem_active *active; |
2742 | unsigned long active_mask; | |
2743 | int idx; | |
41c52415 | 2744 | |
8cac6f6c | 2745 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
2911a35b | 2746 | |
573adb39 | 2747 | active_mask = i915_gem_object_get_active(obj); |
8cac6f6c CW |
2748 | if (!active_mask) |
2749 | return 0; | |
27c01aae | 2750 | |
8cac6f6c CW |
2751 | if (obj->base.pending_write_domain) { |
2752 | active = obj->last_read; | |
b4716185 | 2753 | } else { |
8cac6f6c CW |
2754 | active_mask = 1; |
2755 | active = &obj->last_write; | |
b4716185 | 2756 | } |
8cac6f6c CW |
2757 | |
2758 | for_each_active(active_mask, idx) { | |
2759 | struct drm_i915_gem_request *request; | |
2760 | int ret; | |
2761 | ||
2762 | request = i915_gem_active_peek(&active[idx], | |
2763 | &obj->base.dev->struct_mutex); | |
2764 | if (!request) | |
2765 | continue; | |
2766 | ||
fa545cbf | 2767 | ret = __i915_gem_object_sync(to, request); |
b4716185 CW |
2768 | if (ret) |
2769 | return ret; | |
2770 | } | |
2911a35b | 2771 | |
b4716185 | 2772 | return 0; |
2911a35b BW |
2773 | } |
2774 | ||
b5ffc9bc CW |
2775 | static void i915_gem_object_finish_gtt(struct drm_i915_gem_object *obj) |
2776 | { | |
2777 | u32 old_write_domain, old_read_domains; | |
2778 | ||
b5ffc9bc CW |
2779 | /* Force a pagefault for domain tracking on next user access */ |
2780 | i915_gem_release_mmap(obj); | |
2781 | ||
b97c3d9c KP |
2782 | if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0) |
2783 | return; | |
2784 | ||
b5ffc9bc CW |
2785 | old_read_domains = obj->base.read_domains; |
2786 | old_write_domain = obj->base.write_domain; | |
2787 | ||
2788 | obj->base.read_domains &= ~I915_GEM_DOMAIN_GTT; | |
2789 | obj->base.write_domain &= ~I915_GEM_DOMAIN_GTT; | |
2790 | ||
2791 | trace_i915_gem_object_change_domain(obj, | |
2792 | old_read_domains, | |
2793 | old_write_domain); | |
2794 | } | |
2795 | ||
8ef8561f CW |
2796 | static void __i915_vma_iounmap(struct i915_vma *vma) |
2797 | { | |
20dfbde4 | 2798 | GEM_BUG_ON(i915_vma_is_pinned(vma)); |
8ef8561f CW |
2799 | |
2800 | if (vma->iomap == NULL) | |
2801 | return; | |
2802 | ||
2803 | io_mapping_unmap(vma->iomap); | |
2804 | vma->iomap = NULL; | |
2805 | } | |
2806 | ||
df0e9a28 | 2807 | int i915_vma_unbind(struct i915_vma *vma) |
673a394b | 2808 | { |
07fe0b12 | 2809 | struct drm_i915_gem_object *obj = vma->obj; |
b0decaf7 | 2810 | unsigned long active; |
43e28f09 | 2811 | int ret; |
673a394b | 2812 | |
b0decaf7 CW |
2813 | /* First wait upon any activity as retiring the request may |
2814 | * have side-effects such as unpinning or even unbinding this vma. | |
2815 | */ | |
2816 | active = i915_vma_get_active(vma); | |
df0e9a28 | 2817 | if (active) { |
b0decaf7 CW |
2818 | int idx; |
2819 | ||
b1f788c6 CW |
2820 | /* When a closed VMA is retired, it is unbound - eek. |
2821 | * In order to prevent it from being recursively closed, | |
2822 | * take a pin on the vma so that the second unbind is | |
2823 | * aborted. | |
2824 | */ | |
20dfbde4 | 2825 | __i915_vma_pin(vma); |
b1f788c6 | 2826 | |
b0decaf7 CW |
2827 | for_each_active(active, idx) { |
2828 | ret = i915_gem_active_retire(&vma->last_read[idx], | |
2829 | &vma->vm->dev->struct_mutex); | |
2830 | if (ret) | |
b1f788c6 | 2831 | break; |
b0decaf7 CW |
2832 | } |
2833 | ||
20dfbde4 | 2834 | __i915_vma_unpin(vma); |
b1f788c6 CW |
2835 | if (ret) |
2836 | return ret; | |
2837 | ||
b0decaf7 CW |
2838 | GEM_BUG_ON(i915_vma_is_active(vma)); |
2839 | } | |
2840 | ||
20dfbde4 | 2841 | if (i915_vma_is_pinned(vma)) |
b0decaf7 CW |
2842 | return -EBUSY; |
2843 | ||
b1f788c6 CW |
2844 | if (!drm_mm_node_allocated(&vma->node)) |
2845 | goto destroy; | |
433544bd | 2846 | |
15717de2 CW |
2847 | GEM_BUG_ON(obj->bind_count == 0); |
2848 | GEM_BUG_ON(!obj->pages); | |
c4670ad0 | 2849 | |
3272db53 CW |
2850 | if (i915_vma_is_ggtt(vma) && |
2851 | vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL) { | |
8b1bc9b4 | 2852 | i915_gem_object_finish_gtt(obj); |
5323fd04 | 2853 | |
8b1bc9b4 DV |
2854 | /* release the fence reg _after_ flushing */ |
2855 | ret = i915_gem_object_put_fence(obj); | |
2856 | if (ret) | |
2857 | return ret; | |
8ef8561f CW |
2858 | |
2859 | __i915_vma_iounmap(vma); | |
8b1bc9b4 | 2860 | } |
96b47b65 | 2861 | |
50e046b6 CW |
2862 | if (likely(!vma->vm->closed)) { |
2863 | trace_i915_vma_unbind(vma); | |
2864 | vma->vm->unbind_vma(vma); | |
2865 | } | |
3272db53 | 2866 | vma->flags &= ~(I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND); |
6f65e29a | 2867 | |
50e046b6 CW |
2868 | drm_mm_remove_node(&vma->node); |
2869 | list_move_tail(&vma->vm_link, &vma->vm->unbound_list); | |
2870 | ||
3272db53 | 2871 | if (i915_vma_is_ggtt(vma)) { |
fe14d5f4 TU |
2872 | if (vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL) { |
2873 | obj->map_and_fenceable = false; | |
2874 | } else if (vma->ggtt_view.pages) { | |
2875 | sg_free_table(vma->ggtt_view.pages); | |
2876 | kfree(vma->ggtt_view.pages); | |
fe14d5f4 | 2877 | } |
016a65a3 | 2878 | vma->ggtt_view.pages = NULL; |
fe14d5f4 | 2879 | } |
673a394b | 2880 | |
2f633156 | 2881 | /* Since the unbound list is global, only move to that list if |
b93dab6e | 2882 | * no more VMAs exist. */ |
15717de2 CW |
2883 | if (--obj->bind_count == 0) |
2884 | list_move_tail(&obj->global_list, | |
2885 | &to_i915(obj->base.dev)->mm.unbound_list); | |
673a394b | 2886 | |
70903c3b CW |
2887 | /* And finally now the object is completely decoupled from this vma, |
2888 | * we can drop its hold on the backing storage and allow it to be | |
2889 | * reaped by the shrinker. | |
2890 | */ | |
2891 | i915_gem_object_unpin_pages(obj); | |
2892 | ||
b1f788c6 | 2893 | destroy: |
3272db53 | 2894 | if (unlikely(i915_vma_is_closed(vma))) |
b1f788c6 CW |
2895 | i915_vma_destroy(vma); |
2896 | ||
88241785 | 2897 | return 0; |
54cf91dc CW |
2898 | } |
2899 | ||
6e5a5beb | 2900 | int i915_gem_wait_for_idle(struct drm_i915_private *dev_priv) |
4df2faf4 | 2901 | { |
e2f80391 | 2902 | struct intel_engine_cs *engine; |
b4ac5afc | 2903 | int ret; |
4df2faf4 | 2904 | |
91c8a326 | 2905 | lockdep_assert_held(&dev_priv->drm.struct_mutex); |
6e5a5beb | 2906 | |
b4ac5afc | 2907 | for_each_engine(engine, dev_priv) { |
62e63007 CW |
2908 | if (engine->last_context == NULL) |
2909 | continue; | |
2910 | ||
666796da | 2911 | ret = intel_engine_idle(engine); |
1ec14ad3 CW |
2912 | if (ret) |
2913 | return ret; | |
2914 | } | |
4df2faf4 | 2915 | |
8a1a49f9 | 2916 | return 0; |
4df2faf4 DV |
2917 | } |
2918 | ||
4144f9b5 | 2919 | static bool i915_gem_valid_gtt_space(struct i915_vma *vma, |
42d6ab48 CW |
2920 | unsigned long cache_level) |
2921 | { | |
4144f9b5 | 2922 | struct drm_mm_node *gtt_space = &vma->node; |
42d6ab48 CW |
2923 | struct drm_mm_node *other; |
2924 | ||
4144f9b5 CW |
2925 | /* |
2926 | * On some machines we have to be careful when putting differing types | |
2927 | * of snoopable memory together to avoid the prefetcher crossing memory | |
2928 | * domains and dying. During vm initialisation, we decide whether or not | |
2929 | * these constraints apply and set the drm_mm.color_adjust | |
2930 | * appropriately. | |
42d6ab48 | 2931 | */ |
4144f9b5 | 2932 | if (vma->vm->mm.color_adjust == NULL) |
42d6ab48 CW |
2933 | return true; |
2934 | ||
c6cfb325 | 2935 | if (!drm_mm_node_allocated(gtt_space)) |
42d6ab48 CW |
2936 | return true; |
2937 | ||
2938 | if (list_empty(>t_space->node_list)) | |
2939 | return true; | |
2940 | ||
2941 | other = list_entry(gtt_space->node_list.prev, struct drm_mm_node, node_list); | |
2942 | if (other->allocated && !other->hole_follows && other->color != cache_level) | |
2943 | return false; | |
2944 | ||
2945 | other = list_entry(gtt_space->node_list.next, struct drm_mm_node, node_list); | |
2946 | if (other->allocated && !gtt_space->hole_follows && other->color != cache_level) | |
2947 | return false; | |
2948 | ||
2949 | return true; | |
2950 | } | |
2951 | ||
673a394b | 2952 | /** |
59bfa124 CW |
2953 | * i915_vma_insert - finds a slot for the vma in its address space |
2954 | * @vma: the vma | |
91b2db6f | 2955 | * @size: requested size in bytes (can be larger than the VMA) |
59bfa124 | 2956 | * @alignment: required alignment |
14bb2c11 | 2957 | * @flags: mask of PIN_* flags to use |
59bfa124 CW |
2958 | * |
2959 | * First we try to allocate some free space that meets the requirements for | |
2960 | * the VMA. Failiing that, if the flags permit, it will evict an old VMA, | |
2961 | * preferrably the oldest idle entry to make room for the new VMA. | |
2962 | * | |
2963 | * Returns: | |
2964 | * 0 on success, negative error code otherwise. | |
673a394b | 2965 | */ |
59bfa124 CW |
2966 | static int |
2967 | i915_vma_insert(struct i915_vma *vma, u64 size, u64 alignment, u64 flags) | |
673a394b | 2968 | { |
59bfa124 CW |
2969 | struct drm_i915_private *dev_priv = to_i915(vma->vm->dev); |
2970 | struct drm_i915_gem_object *obj = vma->obj; | |
de180033 CW |
2971 | u64 start, end; |
2972 | u64 min_alignment; | |
07f73f69 | 2973 | int ret; |
673a394b | 2974 | |
3272db53 | 2975 | GEM_BUG_ON(vma->flags & (I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND)); |
59bfa124 | 2976 | GEM_BUG_ON(drm_mm_node_allocated(&vma->node)); |
de180033 CW |
2977 | |
2978 | size = max(size, vma->size); | |
2979 | if (flags & PIN_MAPPABLE) | |
2980 | size = i915_gem_get_ggtt_size(dev_priv, size, obj->tiling_mode); | |
2981 | ||
2982 | min_alignment = | |
2983 | i915_gem_get_ggtt_alignment(dev_priv, size, obj->tiling_mode, | |
2984 | flags & PIN_MAPPABLE); | |
2985 | if (alignment == 0) | |
2986 | alignment = min_alignment; | |
2987 | if (alignment & (min_alignment - 1)) { | |
2988 | DRM_DEBUG("Invalid object alignment requested %llu, minimum %llu\n", | |
2989 | alignment, min_alignment); | |
59bfa124 | 2990 | return -EINVAL; |
91e6711e | 2991 | } |
a00b10c3 | 2992 | |
101b506a | 2993 | start = flags & PIN_OFFSET_BIAS ? flags & PIN_OFFSET_MASK : 0; |
de180033 CW |
2994 | |
2995 | end = vma->vm->total; | |
101b506a | 2996 | if (flags & PIN_MAPPABLE) |
91b2db6f | 2997 | end = min_t(u64, end, dev_priv->ggtt.mappable_end); |
101b506a | 2998 | if (flags & PIN_ZONE_4G) |
48ea1e32 | 2999 | end = min_t(u64, end, (1ULL << 32) - PAGE_SIZE); |
101b506a | 3000 | |
91e6711e JL |
3001 | /* If binding the object/GGTT view requires more space than the entire |
3002 | * aperture has, reject it early before evicting everything in a vain | |
3003 | * attempt to find space. | |
654fc607 | 3004 | */ |
91e6711e | 3005 | if (size > end) { |
de180033 | 3006 | DRM_DEBUG("Attempting to bind an object larger than the aperture: request=%llu [object=%zd] > %s aperture=%llu\n", |
91b2db6f | 3007 | size, obj->base.size, |
1ec9e26d | 3008 | flags & PIN_MAPPABLE ? "mappable" : "total", |
d23db88c | 3009 | end); |
59bfa124 | 3010 | return -E2BIG; |
654fc607 CW |
3011 | } |
3012 | ||
37e680a1 | 3013 | ret = i915_gem_object_get_pages(obj); |
6c085a72 | 3014 | if (ret) |
59bfa124 | 3015 | return ret; |
6c085a72 | 3016 | |
fbdda6fb CW |
3017 | i915_gem_object_pin_pages(obj); |
3018 | ||
506a8e87 | 3019 | if (flags & PIN_OFFSET_FIXED) { |
59bfa124 | 3020 | u64 offset = flags & PIN_OFFSET_MASK; |
de180033 | 3021 | if (offset & (alignment - 1) || offset > end - size) { |
506a8e87 | 3022 | ret = -EINVAL; |
de180033 | 3023 | goto err_unpin; |
506a8e87 | 3024 | } |
de180033 | 3025 | |
506a8e87 CW |
3026 | vma->node.start = offset; |
3027 | vma->node.size = size; | |
3028 | vma->node.color = obj->cache_level; | |
de180033 | 3029 | ret = drm_mm_reserve_node(&vma->vm->mm, &vma->node); |
506a8e87 CW |
3030 | if (ret) { |
3031 | ret = i915_gem_evict_for_vma(vma); | |
3032 | if (ret == 0) | |
de180033 CW |
3033 | ret = drm_mm_reserve_node(&vma->vm->mm, &vma->node); |
3034 | if (ret) | |
3035 | goto err_unpin; | |
506a8e87 | 3036 | } |
101b506a | 3037 | } else { |
de180033 CW |
3038 | u32 search_flag, alloc_flag; |
3039 | ||
506a8e87 CW |
3040 | if (flags & PIN_HIGH) { |
3041 | search_flag = DRM_MM_SEARCH_BELOW; | |
3042 | alloc_flag = DRM_MM_CREATE_TOP; | |
3043 | } else { | |
3044 | search_flag = DRM_MM_SEARCH_DEFAULT; | |
3045 | alloc_flag = DRM_MM_CREATE_DEFAULT; | |
3046 | } | |
101b506a | 3047 | |
954c4691 CW |
3048 | /* We only allocate in PAGE_SIZE/GTT_PAGE_SIZE (4096) chunks, |
3049 | * so we know that we always have a minimum alignment of 4096. | |
3050 | * The drm_mm range manager is optimised to return results | |
3051 | * with zero alignment, so where possible use the optimal | |
3052 | * path. | |
3053 | */ | |
3054 | if (alignment <= 4096) | |
3055 | alignment = 0; | |
3056 | ||
0a9ae0d7 | 3057 | search_free: |
de180033 CW |
3058 | ret = drm_mm_insert_node_in_range_generic(&vma->vm->mm, |
3059 | &vma->node, | |
506a8e87 CW |
3060 | size, alignment, |
3061 | obj->cache_level, | |
3062 | start, end, | |
3063 | search_flag, | |
3064 | alloc_flag); | |
3065 | if (ret) { | |
de180033 | 3066 | ret = i915_gem_evict_something(vma->vm, size, alignment, |
506a8e87 CW |
3067 | obj->cache_level, |
3068 | start, end, | |
3069 | flags); | |
3070 | if (ret == 0) | |
3071 | goto search_free; | |
9731129c | 3072 | |
de180033 | 3073 | goto err_unpin; |
506a8e87 | 3074 | } |
673a394b | 3075 | } |
37508589 | 3076 | GEM_BUG_ON(!i915_gem_valid_gtt_space(vma, obj->cache_level)); |
673a394b | 3077 | |
35c20a60 | 3078 | list_move_tail(&obj->global_list, &dev_priv->mm.bound_list); |
de180033 | 3079 | list_move_tail(&vma->vm_link, &vma->vm->inactive_list); |
15717de2 | 3080 | obj->bind_count++; |
bf1a1092 | 3081 | |
59bfa124 | 3082 | return 0; |
2f633156 | 3083 | |
bc6bc15b | 3084 | err_unpin: |
2f633156 | 3085 | i915_gem_object_unpin_pages(obj); |
59bfa124 | 3086 | return ret; |
673a394b EA |
3087 | } |
3088 | ||
000433b6 | 3089 | bool |
2c22569b CW |
3090 | i915_gem_clflush_object(struct drm_i915_gem_object *obj, |
3091 | bool force) | |
673a394b | 3092 | { |
673a394b EA |
3093 | /* If we don't have a page list set up, then we're not pinned |
3094 | * to GPU, and we can ignore the cache flush because it'll happen | |
3095 | * again at bind time. | |
3096 | */ | |
05394f39 | 3097 | if (obj->pages == NULL) |
000433b6 | 3098 | return false; |
673a394b | 3099 | |
769ce464 ID |
3100 | /* |
3101 | * Stolen memory is always coherent with the GPU as it is explicitly | |
3102 | * marked as wc by the system, or the system is cache-coherent. | |
3103 | */ | |
6a2c4232 | 3104 | if (obj->stolen || obj->phys_handle) |
000433b6 | 3105 | return false; |
769ce464 | 3106 | |
9c23f7fc CW |
3107 | /* If the GPU is snooping the contents of the CPU cache, |
3108 | * we do not need to manually clear the CPU cache lines. However, | |
3109 | * the caches are only snooped when the render cache is | |
3110 | * flushed/invalidated. As we always have to emit invalidations | |
3111 | * and flushes when moving into and out of the RENDER domain, correct | |
3112 | * snooping behaviour occurs naturally as the result of our domain | |
3113 | * tracking. | |
3114 | */ | |
0f71979a CW |
3115 | if (!force && cpu_cache_is_coherent(obj->base.dev, obj->cache_level)) { |
3116 | obj->cache_dirty = true; | |
000433b6 | 3117 | return false; |
0f71979a | 3118 | } |
9c23f7fc | 3119 | |
1c5d22f7 | 3120 | trace_i915_gem_object_clflush(obj); |
9da3da66 | 3121 | drm_clflush_sg(obj->pages); |
0f71979a | 3122 | obj->cache_dirty = false; |
000433b6 CW |
3123 | |
3124 | return true; | |
e47c68e9 EA |
3125 | } |
3126 | ||
3127 | /** Flushes the GTT write domain for the object if it's dirty. */ | |
3128 | static void | |
05394f39 | 3129 | i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj) |
e47c68e9 | 3130 | { |
1c5d22f7 CW |
3131 | uint32_t old_write_domain; |
3132 | ||
05394f39 | 3133 | if (obj->base.write_domain != I915_GEM_DOMAIN_GTT) |
e47c68e9 EA |
3134 | return; |
3135 | ||
63256ec5 | 3136 | /* No actual flushing is required for the GTT write domain. Writes |
e47c68e9 EA |
3137 | * to it immediately go to main memory as far as we know, so there's |
3138 | * no chipset flush. It also doesn't land in render cache. | |
63256ec5 CW |
3139 | * |
3140 | * However, we do have to enforce the order so that all writes through | |
3141 | * the GTT land before any writes to the device, such as updates to | |
3142 | * the GATT itself. | |
e47c68e9 | 3143 | */ |
63256ec5 CW |
3144 | wmb(); |
3145 | ||
05394f39 CW |
3146 | old_write_domain = obj->base.write_domain; |
3147 | obj->base.write_domain = 0; | |
1c5d22f7 | 3148 | |
de152b62 | 3149 | intel_fb_obj_flush(obj, false, ORIGIN_GTT); |
f99d7069 | 3150 | |
1c5d22f7 | 3151 | trace_i915_gem_object_change_domain(obj, |
05394f39 | 3152 | obj->base.read_domains, |
1c5d22f7 | 3153 | old_write_domain); |
e47c68e9 EA |
3154 | } |
3155 | ||
3156 | /** Flushes the CPU write domain for the object if it's dirty. */ | |
3157 | static void | |
e62b59e4 | 3158 | i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj) |
e47c68e9 | 3159 | { |
1c5d22f7 | 3160 | uint32_t old_write_domain; |
e47c68e9 | 3161 | |
05394f39 | 3162 | if (obj->base.write_domain != I915_GEM_DOMAIN_CPU) |
e47c68e9 EA |
3163 | return; |
3164 | ||
e62b59e4 | 3165 | if (i915_gem_clflush_object(obj, obj->pin_display)) |
c033666a | 3166 | i915_gem_chipset_flush(to_i915(obj->base.dev)); |
000433b6 | 3167 | |
05394f39 CW |
3168 | old_write_domain = obj->base.write_domain; |
3169 | obj->base.write_domain = 0; | |
1c5d22f7 | 3170 | |
de152b62 | 3171 | intel_fb_obj_flush(obj, false, ORIGIN_CPU); |
f99d7069 | 3172 | |
1c5d22f7 | 3173 | trace_i915_gem_object_change_domain(obj, |
05394f39 | 3174 | obj->base.read_domains, |
1c5d22f7 | 3175 | old_write_domain); |
e47c68e9 EA |
3176 | } |
3177 | ||
2ef7eeaa EA |
3178 | /** |
3179 | * Moves a single object to the GTT read, and possibly write domain. | |
14bb2c11 TU |
3180 | * @obj: object to act on |
3181 | * @write: ask for write access or read only | |
2ef7eeaa EA |
3182 | * |
3183 | * This function returns when the move is complete, including waiting on | |
3184 | * flushes to occur. | |
3185 | */ | |
79e53945 | 3186 | int |
2021746e | 3187 | i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write) |
2ef7eeaa | 3188 | { |
1c5d22f7 | 3189 | uint32_t old_write_domain, old_read_domains; |
43566ded | 3190 | struct i915_vma *vma; |
e47c68e9 | 3191 | int ret; |
2ef7eeaa | 3192 | |
0201f1ec | 3193 | ret = i915_gem_object_wait_rendering(obj, !write); |
88241785 CW |
3194 | if (ret) |
3195 | return ret; | |
3196 | ||
c13d87ea CW |
3197 | if (obj->base.write_domain == I915_GEM_DOMAIN_GTT) |
3198 | return 0; | |
3199 | ||
43566ded CW |
3200 | /* Flush and acquire obj->pages so that we are coherent through |
3201 | * direct access in memory with previous cached writes through | |
3202 | * shmemfs and that our cache domain tracking remains valid. | |
3203 | * For example, if the obj->filp was moved to swap without us | |
3204 | * being notified and releasing the pages, we would mistakenly | |
3205 | * continue to assume that the obj remained out of the CPU cached | |
3206 | * domain. | |
3207 | */ | |
3208 | ret = i915_gem_object_get_pages(obj); | |
3209 | if (ret) | |
3210 | return ret; | |
3211 | ||
e62b59e4 | 3212 | i915_gem_object_flush_cpu_write_domain(obj); |
1c5d22f7 | 3213 | |
d0a57789 CW |
3214 | /* Serialise direct access to this object with the barriers for |
3215 | * coherent writes from the GPU, by effectively invalidating the | |
3216 | * GTT domain upon first access. | |
3217 | */ | |
3218 | if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0) | |
3219 | mb(); | |
3220 | ||
05394f39 CW |
3221 | old_write_domain = obj->base.write_domain; |
3222 | old_read_domains = obj->base.read_domains; | |
1c5d22f7 | 3223 | |
e47c68e9 EA |
3224 | /* It should now be out of any other write domains, and we can update |
3225 | * the domain values for our changes. | |
3226 | */ | |
05394f39 CW |
3227 | BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0); |
3228 | obj->base.read_domains |= I915_GEM_DOMAIN_GTT; | |
e47c68e9 | 3229 | if (write) { |
05394f39 CW |
3230 | obj->base.read_domains = I915_GEM_DOMAIN_GTT; |
3231 | obj->base.write_domain = I915_GEM_DOMAIN_GTT; | |
3232 | obj->dirty = 1; | |
2ef7eeaa EA |
3233 | } |
3234 | ||
1c5d22f7 CW |
3235 | trace_i915_gem_object_change_domain(obj, |
3236 | old_read_domains, | |
3237 | old_write_domain); | |
3238 | ||
8325a09d | 3239 | /* And bump the LRU for this access */ |
43566ded | 3240 | vma = i915_gem_obj_to_ggtt(obj); |
b0decaf7 CW |
3241 | if (vma && |
3242 | drm_mm_node_allocated(&vma->node) && | |
3243 | !i915_vma_is_active(vma)) | |
3244 | list_move_tail(&vma->vm_link, &vma->vm->inactive_list); | |
8325a09d | 3245 | |
e47c68e9 EA |
3246 | return 0; |
3247 | } | |
3248 | ||
ef55f92a CW |
3249 | /** |
3250 | * Changes the cache-level of an object across all VMA. | |
14bb2c11 TU |
3251 | * @obj: object to act on |
3252 | * @cache_level: new cache level to set for the object | |
ef55f92a CW |
3253 | * |
3254 | * After this function returns, the object will be in the new cache-level | |
3255 | * across all GTT and the contents of the backing storage will be coherent, | |
3256 | * with respect to the new cache-level. In order to keep the backing storage | |
3257 | * coherent for all users, we only allow a single cache level to be set | |
3258 | * globally on the object and prevent it from being changed whilst the | |
3259 | * hardware is reading from the object. That is if the object is currently | |
3260 | * on the scanout it will be set to uncached (or equivalent display | |
3261 | * cache coherency) and all non-MOCS GPU access will also be uncached so | |
3262 | * that all direct access to the scanout remains coherent. | |
3263 | */ | |
e4ffd173 CW |
3264 | int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj, |
3265 | enum i915_cache_level cache_level) | |
3266 | { | |
aa653a68 | 3267 | struct i915_vma *vma; |
ed75a55b | 3268 | int ret = 0; |
e4ffd173 CW |
3269 | |
3270 | if (obj->cache_level == cache_level) | |
ed75a55b | 3271 | goto out; |
e4ffd173 | 3272 | |
ef55f92a CW |
3273 | /* Inspect the list of currently bound VMA and unbind any that would |
3274 | * be invalid given the new cache-level. This is principally to | |
3275 | * catch the issue of the CS prefetch crossing page boundaries and | |
3276 | * reading an invalid PTE on older architectures. | |
3277 | */ | |
aa653a68 CW |
3278 | restart: |
3279 | list_for_each_entry(vma, &obj->vma_list, obj_link) { | |
ef55f92a CW |
3280 | if (!drm_mm_node_allocated(&vma->node)) |
3281 | continue; | |
3282 | ||
20dfbde4 | 3283 | if (i915_vma_is_pinned(vma)) { |
ef55f92a CW |
3284 | DRM_DEBUG("can not change the cache level of pinned objects\n"); |
3285 | return -EBUSY; | |
3286 | } | |
3287 | ||
aa653a68 CW |
3288 | if (i915_gem_valid_gtt_space(vma, cache_level)) |
3289 | continue; | |
3290 | ||
3291 | ret = i915_vma_unbind(vma); | |
3292 | if (ret) | |
3293 | return ret; | |
3294 | ||
3295 | /* As unbinding may affect other elements in the | |
3296 | * obj->vma_list (due to side-effects from retiring | |
3297 | * an active vma), play safe and restart the iterator. | |
3298 | */ | |
3299 | goto restart; | |
42d6ab48 CW |
3300 | } |
3301 | ||
ef55f92a CW |
3302 | /* We can reuse the existing drm_mm nodes but need to change the |
3303 | * cache-level on the PTE. We could simply unbind them all and | |
3304 | * rebind with the correct cache-level on next use. However since | |
3305 | * we already have a valid slot, dma mapping, pages etc, we may as | |
3306 | * rewrite the PTE in the belief that doing so tramples upon less | |
3307 | * state and so involves less work. | |
3308 | */ | |
15717de2 | 3309 | if (obj->bind_count) { |
ef55f92a CW |
3310 | /* Before we change the PTE, the GPU must not be accessing it. |
3311 | * If we wait upon the object, we know that all the bound | |
3312 | * VMA are no longer active. | |
3313 | */ | |
2e2f351d | 3314 | ret = i915_gem_object_wait_rendering(obj, false); |
e4ffd173 CW |
3315 | if (ret) |
3316 | return ret; | |
3317 | ||
aa653a68 | 3318 | if (!HAS_LLC(obj->base.dev) && cache_level != I915_CACHE_NONE) { |
ef55f92a CW |
3319 | /* Access to snoopable pages through the GTT is |
3320 | * incoherent and on some machines causes a hard | |
3321 | * lockup. Relinquish the CPU mmaping to force | |
3322 | * userspace to refault in the pages and we can | |
3323 | * then double check if the GTT mapping is still | |
3324 | * valid for that pointer access. | |
3325 | */ | |
3326 | i915_gem_release_mmap(obj); | |
3327 | ||
3328 | /* As we no longer need a fence for GTT access, | |
3329 | * we can relinquish it now (and so prevent having | |
3330 | * to steal a fence from someone else on the next | |
3331 | * fence request). Note GPU activity would have | |
3332 | * dropped the fence as all snoopable access is | |
3333 | * supposed to be linear. | |
3334 | */ | |
e4ffd173 CW |
3335 | ret = i915_gem_object_put_fence(obj); |
3336 | if (ret) | |
3337 | return ret; | |
ef55f92a CW |
3338 | } else { |
3339 | /* We either have incoherent backing store and | |
3340 | * so no GTT access or the architecture is fully | |
3341 | * coherent. In such cases, existing GTT mmaps | |
3342 | * ignore the cache bit in the PTE and we can | |
3343 | * rewrite it without confusing the GPU or having | |
3344 | * to force userspace to fault back in its mmaps. | |
3345 | */ | |
e4ffd173 CW |
3346 | } |
3347 | ||
1c7f4bca | 3348 | list_for_each_entry(vma, &obj->vma_list, obj_link) { |
ef55f92a CW |
3349 | if (!drm_mm_node_allocated(&vma->node)) |
3350 | continue; | |
3351 | ||
3352 | ret = i915_vma_bind(vma, cache_level, PIN_UPDATE); | |
3353 | if (ret) | |
3354 | return ret; | |
3355 | } | |
e4ffd173 CW |
3356 | } |
3357 | ||
1c7f4bca | 3358 | list_for_each_entry(vma, &obj->vma_list, obj_link) |
2c22569b CW |
3359 | vma->node.color = cache_level; |
3360 | obj->cache_level = cache_level; | |
3361 | ||
ed75a55b | 3362 | out: |
ef55f92a CW |
3363 | /* Flush the dirty CPU caches to the backing storage so that the |
3364 | * object is now coherent at its new cache level (with respect | |
3365 | * to the access domain). | |
3366 | */ | |
b50a5371 | 3367 | if (obj->cache_dirty && cpu_write_needs_clflush(obj)) { |
0f71979a | 3368 | if (i915_gem_clflush_object(obj, true)) |
c033666a | 3369 | i915_gem_chipset_flush(to_i915(obj->base.dev)); |
e4ffd173 CW |
3370 | } |
3371 | ||
e4ffd173 CW |
3372 | return 0; |
3373 | } | |
3374 | ||
199adf40 BW |
3375 | int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data, |
3376 | struct drm_file *file) | |
e6994aee | 3377 | { |
199adf40 | 3378 | struct drm_i915_gem_caching *args = data; |
e6994aee | 3379 | struct drm_i915_gem_object *obj; |
e6994aee | 3380 | |
03ac0642 CW |
3381 | obj = i915_gem_object_lookup(file, args->handle); |
3382 | if (!obj) | |
432be69d | 3383 | return -ENOENT; |
e6994aee | 3384 | |
651d794f CW |
3385 | switch (obj->cache_level) { |
3386 | case I915_CACHE_LLC: | |
3387 | case I915_CACHE_L3_LLC: | |
3388 | args->caching = I915_CACHING_CACHED; | |
3389 | break; | |
3390 | ||
4257d3ba CW |
3391 | case I915_CACHE_WT: |
3392 | args->caching = I915_CACHING_DISPLAY; | |
3393 | break; | |
3394 | ||
651d794f CW |
3395 | default: |
3396 | args->caching = I915_CACHING_NONE; | |
3397 | break; | |
3398 | } | |
e6994aee | 3399 | |
34911fd3 | 3400 | i915_gem_object_put_unlocked(obj); |
432be69d | 3401 | return 0; |
e6994aee CW |
3402 | } |
3403 | ||
199adf40 BW |
3404 | int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data, |
3405 | struct drm_file *file) | |
e6994aee | 3406 | { |
fac5e23e | 3407 | struct drm_i915_private *dev_priv = to_i915(dev); |
199adf40 | 3408 | struct drm_i915_gem_caching *args = data; |
e6994aee CW |
3409 | struct drm_i915_gem_object *obj; |
3410 | enum i915_cache_level level; | |
3411 | int ret; | |
3412 | ||
199adf40 BW |
3413 | switch (args->caching) { |
3414 | case I915_CACHING_NONE: | |
e6994aee CW |
3415 | level = I915_CACHE_NONE; |
3416 | break; | |
199adf40 | 3417 | case I915_CACHING_CACHED: |
e5756c10 ID |
3418 | /* |
3419 | * Due to a HW issue on BXT A stepping, GPU stores via a | |
3420 | * snooped mapping may leave stale data in a corresponding CPU | |
3421 | * cacheline, whereas normally such cachelines would get | |
3422 | * invalidated. | |
3423 | */ | |
ca377809 | 3424 | if (!HAS_LLC(dev) && !HAS_SNOOP(dev)) |
e5756c10 ID |
3425 | return -ENODEV; |
3426 | ||
e6994aee CW |
3427 | level = I915_CACHE_LLC; |
3428 | break; | |
4257d3ba CW |
3429 | case I915_CACHING_DISPLAY: |
3430 | level = HAS_WT(dev) ? I915_CACHE_WT : I915_CACHE_NONE; | |
3431 | break; | |
e6994aee CW |
3432 | default: |
3433 | return -EINVAL; | |
3434 | } | |
3435 | ||
fd0fe6ac ID |
3436 | intel_runtime_pm_get(dev_priv); |
3437 | ||
3bc2913e BW |
3438 | ret = i915_mutex_lock_interruptible(dev); |
3439 | if (ret) | |
fd0fe6ac | 3440 | goto rpm_put; |
3bc2913e | 3441 | |
03ac0642 CW |
3442 | obj = i915_gem_object_lookup(file, args->handle); |
3443 | if (!obj) { | |
e6994aee CW |
3444 | ret = -ENOENT; |
3445 | goto unlock; | |
3446 | } | |
3447 | ||
3448 | ret = i915_gem_object_set_cache_level(obj, level); | |
3449 | ||
f8c417cd | 3450 | i915_gem_object_put(obj); |
e6994aee CW |
3451 | unlock: |
3452 | mutex_unlock(&dev->struct_mutex); | |
fd0fe6ac ID |
3453 | rpm_put: |
3454 | intel_runtime_pm_put(dev_priv); | |
3455 | ||
e6994aee CW |
3456 | return ret; |
3457 | } | |
3458 | ||
b9241ea3 | 3459 | /* |
2da3b9b9 CW |
3460 | * Prepare buffer for display plane (scanout, cursors, etc). |
3461 | * Can be called from an uninterruptible phase (modesetting) and allows | |
3462 | * any flushes to be pipelined (for pageflips). | |
b9241ea3 ZW |
3463 | */ |
3464 | int | |
2da3b9b9 CW |
3465 | i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj, |
3466 | u32 alignment, | |
e6617330 | 3467 | const struct i915_ggtt_view *view) |
b9241ea3 | 3468 | { |
2da3b9b9 | 3469 | u32 old_read_domains, old_write_domain; |
b9241ea3 ZW |
3470 | int ret; |
3471 | ||
cc98b413 CW |
3472 | /* Mark the pin_display early so that we account for the |
3473 | * display coherency whilst setting up the cache domains. | |
3474 | */ | |
8a0c39b1 | 3475 | obj->pin_display++; |
cc98b413 | 3476 | |
a7ef0640 EA |
3477 | /* The display engine is not coherent with the LLC cache on gen6. As |
3478 | * a result, we make sure that the pinning that is about to occur is | |
3479 | * done with uncached PTEs. This is lowest common denominator for all | |
3480 | * chipsets. | |
3481 | * | |
3482 | * However for gen6+, we could do better by using the GFDT bit instead | |
3483 | * of uncaching, which would allow us to flush all the LLC-cached data | |
3484 | * with that bit in the PTE to main memory with just one PIPE_CONTROL. | |
3485 | */ | |
651d794f CW |
3486 | ret = i915_gem_object_set_cache_level(obj, |
3487 | HAS_WT(obj->base.dev) ? I915_CACHE_WT : I915_CACHE_NONE); | |
a7ef0640 | 3488 | if (ret) |
cc98b413 | 3489 | goto err_unpin_display; |
a7ef0640 | 3490 | |
2da3b9b9 CW |
3491 | /* As the user may map the buffer once pinned in the display plane |
3492 | * (e.g. libkms for the bootup splash), we have to ensure that we | |
3493 | * always use map_and_fenceable for all scanout buffers. | |
3494 | */ | |
91b2db6f | 3495 | ret = i915_gem_object_ggtt_pin(obj, view, 0, alignment, |
50470bb0 TU |
3496 | view->type == I915_GGTT_VIEW_NORMAL ? |
3497 | PIN_MAPPABLE : 0); | |
2da3b9b9 | 3498 | if (ret) |
cc98b413 | 3499 | goto err_unpin_display; |
2da3b9b9 | 3500 | |
e62b59e4 | 3501 | i915_gem_object_flush_cpu_write_domain(obj); |
b118c1e3 | 3502 | |
2da3b9b9 | 3503 | old_write_domain = obj->base.write_domain; |
05394f39 | 3504 | old_read_domains = obj->base.read_domains; |
2da3b9b9 CW |
3505 | |
3506 | /* It should now be out of any other write domains, and we can update | |
3507 | * the domain values for our changes. | |
3508 | */ | |
e5f1d962 | 3509 | obj->base.write_domain = 0; |
05394f39 | 3510 | obj->base.read_domains |= I915_GEM_DOMAIN_GTT; |
b9241ea3 ZW |
3511 | |
3512 | trace_i915_gem_object_change_domain(obj, | |
3513 | old_read_domains, | |
2da3b9b9 | 3514 | old_write_domain); |
b9241ea3 ZW |
3515 | |
3516 | return 0; | |
cc98b413 CW |
3517 | |
3518 | err_unpin_display: | |
8a0c39b1 | 3519 | obj->pin_display--; |
cc98b413 CW |
3520 | return ret; |
3521 | } | |
3522 | ||
3523 | void | |
e6617330 TU |
3524 | i915_gem_object_unpin_from_display_plane(struct drm_i915_gem_object *obj, |
3525 | const struct i915_ggtt_view *view) | |
cc98b413 | 3526 | { |
8a0c39b1 TU |
3527 | if (WARN_ON(obj->pin_display == 0)) |
3528 | return; | |
3529 | ||
e6617330 TU |
3530 | i915_gem_object_ggtt_unpin_view(obj, view); |
3531 | ||
8a0c39b1 | 3532 | obj->pin_display--; |
b9241ea3 ZW |
3533 | } |
3534 | ||
e47c68e9 EA |
3535 | /** |
3536 | * Moves a single object to the CPU read, and possibly write domain. | |
14bb2c11 TU |
3537 | * @obj: object to act on |
3538 | * @write: requesting write or read-only access | |
e47c68e9 EA |
3539 | * |
3540 | * This function returns when the move is complete, including waiting on | |
3541 | * flushes to occur. | |
3542 | */ | |
dabdfe02 | 3543 | int |
919926ae | 3544 | i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write) |
e47c68e9 | 3545 | { |
1c5d22f7 | 3546 | uint32_t old_write_domain, old_read_domains; |
e47c68e9 EA |
3547 | int ret; |
3548 | ||
0201f1ec | 3549 | ret = i915_gem_object_wait_rendering(obj, !write); |
88241785 CW |
3550 | if (ret) |
3551 | return ret; | |
3552 | ||
c13d87ea CW |
3553 | if (obj->base.write_domain == I915_GEM_DOMAIN_CPU) |
3554 | return 0; | |
3555 | ||
e47c68e9 | 3556 | i915_gem_object_flush_gtt_write_domain(obj); |
2ef7eeaa | 3557 | |
05394f39 CW |
3558 | old_write_domain = obj->base.write_domain; |
3559 | old_read_domains = obj->base.read_domains; | |
1c5d22f7 | 3560 | |
e47c68e9 | 3561 | /* Flush the CPU cache if it's still invalid. */ |
05394f39 | 3562 | if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) { |
2c22569b | 3563 | i915_gem_clflush_object(obj, false); |
2ef7eeaa | 3564 | |
05394f39 | 3565 | obj->base.read_domains |= I915_GEM_DOMAIN_CPU; |
2ef7eeaa EA |
3566 | } |
3567 | ||
3568 | /* It should now be out of any other write domains, and we can update | |
3569 | * the domain values for our changes. | |
3570 | */ | |
05394f39 | 3571 | BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0); |
e47c68e9 EA |
3572 | |
3573 | /* If we're writing through the CPU, then the GPU read domains will | |
3574 | * need to be invalidated at next use. | |
3575 | */ | |
3576 | if (write) { | |
05394f39 CW |
3577 | obj->base.read_domains = I915_GEM_DOMAIN_CPU; |
3578 | obj->base.write_domain = I915_GEM_DOMAIN_CPU; | |
e47c68e9 | 3579 | } |
2ef7eeaa | 3580 | |
1c5d22f7 CW |
3581 | trace_i915_gem_object_change_domain(obj, |
3582 | old_read_domains, | |
3583 | old_write_domain); | |
3584 | ||
2ef7eeaa EA |
3585 | return 0; |
3586 | } | |
3587 | ||
673a394b EA |
3588 | /* Throttle our rendering by waiting until the ring has completed our requests |
3589 | * emitted over 20 msec ago. | |
3590 | * | |
b962442e EA |
3591 | * Note that if we were to use the current jiffies each time around the loop, |
3592 | * we wouldn't escape the function with any frames outstanding if the time to | |
3593 | * render a frame was over 20ms. | |
3594 | * | |
673a394b EA |
3595 | * This should get us reasonable parallelism between CPU and GPU but also |
3596 | * relatively low latency when blocking on a particular request to finish. | |
3597 | */ | |
40a5f0de | 3598 | static int |
f787a5f5 | 3599 | i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file) |
40a5f0de | 3600 | { |
fac5e23e | 3601 | struct drm_i915_private *dev_priv = to_i915(dev); |
f787a5f5 | 3602 | struct drm_i915_file_private *file_priv = file->driver_priv; |
d0bc54f2 | 3603 | unsigned long recent_enough = jiffies - DRM_I915_THROTTLE_JIFFIES; |
54fb2411 | 3604 | struct drm_i915_gem_request *request, *target = NULL; |
f787a5f5 | 3605 | int ret; |
93533c29 | 3606 | |
308887aa DV |
3607 | ret = i915_gem_wait_for_error(&dev_priv->gpu_error); |
3608 | if (ret) | |
3609 | return ret; | |
3610 | ||
f4457ae7 CW |
3611 | /* ABI: return -EIO if already wedged */ |
3612 | if (i915_terminally_wedged(&dev_priv->gpu_error)) | |
3613 | return -EIO; | |
e110e8d6 | 3614 | |
1c25595f | 3615 | spin_lock(&file_priv->mm.lock); |
f787a5f5 | 3616 | list_for_each_entry(request, &file_priv->mm.request_list, client_list) { |
b962442e EA |
3617 | if (time_after_eq(request->emitted_jiffies, recent_enough)) |
3618 | break; | |
40a5f0de | 3619 | |
fcfa423c JH |
3620 | /* |
3621 | * Note that the request might not have been submitted yet. | |
3622 | * In which case emitted_jiffies will be zero. | |
3623 | */ | |
3624 | if (!request->emitted_jiffies) | |
3625 | continue; | |
3626 | ||
54fb2411 | 3627 | target = request; |
b962442e | 3628 | } |
ff865885 | 3629 | if (target) |
e8a261ea | 3630 | i915_gem_request_get(target); |
1c25595f | 3631 | spin_unlock(&file_priv->mm.lock); |
40a5f0de | 3632 | |
54fb2411 | 3633 | if (target == NULL) |
f787a5f5 | 3634 | return 0; |
2bc43b5c | 3635 | |
776f3236 | 3636 | ret = i915_wait_request(target, true, NULL, NULL); |
e8a261ea | 3637 | i915_gem_request_put(target); |
ff865885 | 3638 | |
40a5f0de EA |
3639 | return ret; |
3640 | } | |
3641 | ||
d23db88c | 3642 | static bool |
91b2db6f | 3643 | i915_vma_misplaced(struct i915_vma *vma, u64 size, u64 alignment, u64 flags) |
d23db88c CW |
3644 | { |
3645 | struct drm_i915_gem_object *obj = vma->obj; | |
3646 | ||
59bfa124 CW |
3647 | if (!drm_mm_node_allocated(&vma->node)) |
3648 | return false; | |
3649 | ||
91b2db6f CW |
3650 | if (vma->node.size < size) |
3651 | return true; | |
3652 | ||
3653 | if (alignment && vma->node.start & (alignment - 1)) | |
d23db88c CW |
3654 | return true; |
3655 | ||
3656 | if (flags & PIN_MAPPABLE && !obj->map_and_fenceable) | |
3657 | return true; | |
3658 | ||
3659 | if (flags & PIN_OFFSET_BIAS && | |
3660 | vma->node.start < (flags & PIN_OFFSET_MASK)) | |
3661 | return true; | |
3662 | ||
506a8e87 CW |
3663 | if (flags & PIN_OFFSET_FIXED && |
3664 | vma->node.start != (flags & PIN_OFFSET_MASK)) | |
3665 | return true; | |
3666 | ||
d23db88c CW |
3667 | return false; |
3668 | } | |
3669 | ||
d0710abb CW |
3670 | void __i915_vma_set_map_and_fenceable(struct i915_vma *vma) |
3671 | { | |
3672 | struct drm_i915_gem_object *obj = vma->obj; | |
a9f1481f | 3673 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
d0710abb CW |
3674 | bool mappable, fenceable; |
3675 | u32 fence_size, fence_alignment; | |
3676 | ||
a9f1481f | 3677 | fence_size = i915_gem_get_ggtt_size(dev_priv, |
ad1a7d20 CW |
3678 | obj->base.size, |
3679 | obj->tiling_mode); | |
a9f1481f | 3680 | fence_alignment = i915_gem_get_ggtt_alignment(dev_priv, |
ad1a7d20 CW |
3681 | obj->base.size, |
3682 | obj->tiling_mode, | |
3683 | true); | |
d0710abb CW |
3684 | |
3685 | fenceable = (vma->node.size == fence_size && | |
3686 | (vma->node.start & (fence_alignment - 1)) == 0); | |
3687 | ||
3688 | mappable = (vma->node.start + fence_size <= | |
a9f1481f | 3689 | dev_priv->ggtt.mappable_end); |
d0710abb CW |
3690 | |
3691 | obj->map_and_fenceable = mappable && fenceable; | |
3692 | } | |
3693 | ||
305bc234 CW |
3694 | int __i915_vma_do_pin(struct i915_vma *vma, |
3695 | u64 size, u64 alignment, u64 flags) | |
673a394b | 3696 | { |
305bc234 | 3697 | unsigned int bound = vma->flags; |
673a394b EA |
3698 | int ret; |
3699 | ||
59bfa124 | 3700 | GEM_BUG_ON((flags & (PIN_GLOBAL | PIN_USER)) == 0); |
3272db53 | 3701 | GEM_BUG_ON((flags & PIN_GLOBAL) && !i915_vma_is_ggtt(vma)); |
d7f46fc4 | 3702 | |
305bc234 CW |
3703 | if (WARN_ON(bound & I915_VMA_PIN_OVERFLOW)) { |
3704 | ret = -EBUSY; | |
3705 | goto err; | |
3706 | } | |
ac0c6b5a | 3707 | |
de895082 | 3708 | if ((bound & I915_VMA_BIND_MASK) == 0) { |
59bfa124 CW |
3709 | ret = i915_vma_insert(vma, size, alignment, flags); |
3710 | if (ret) | |
3711 | goto err; | |
fe14d5f4 | 3712 | } |
74898d7e | 3713 | |
59bfa124 | 3714 | ret = i915_vma_bind(vma, vma->obj->cache_level, flags); |
3b16525c | 3715 | if (ret) |
59bfa124 | 3716 | goto err; |
3b16525c | 3717 | |
3272db53 | 3718 | if ((bound ^ vma->flags) & I915_VMA_GLOBAL_BIND) |
d0710abb | 3719 | __i915_vma_set_map_and_fenceable(vma); |
ef79e17c | 3720 | |
3b16525c | 3721 | GEM_BUG_ON(i915_vma_misplaced(vma, size, alignment, flags)); |
673a394b | 3722 | return 0; |
673a394b | 3723 | |
59bfa124 CW |
3724 | err: |
3725 | __i915_vma_unpin(vma); | |
3726 | return ret; | |
ec7adb6e JL |
3727 | } |
3728 | ||
3729 | int | |
3730 | i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj, | |
3731 | const struct i915_ggtt_view *view, | |
91b2db6f | 3732 | u64 size, |
2ffffd0f CW |
3733 | u64 alignment, |
3734 | u64 flags) | |
ec7adb6e | 3735 | { |
59bfa124 CW |
3736 | struct i915_vma *vma; |
3737 | int ret; | |
72e96d64 | 3738 | |
de895082 CW |
3739 | if (!view) |
3740 | view = &i915_ggtt_view_normal; | |
ec7adb6e | 3741 | |
59bfa124 CW |
3742 | vma = i915_gem_obj_lookup_or_create_ggtt_vma(obj, view); |
3743 | if (IS_ERR(vma)) | |
3744 | return PTR_ERR(vma); | |
3745 | ||
3746 | if (i915_vma_misplaced(vma, size, alignment, flags)) { | |
3747 | if (flags & PIN_NONBLOCK && | |
3748 | (i915_vma_is_pinned(vma) || i915_vma_is_active(vma))) | |
3749 | return -ENOSPC; | |
3750 | ||
3751 | WARN(i915_vma_is_pinned(vma), | |
3752 | "bo is already pinned in ggtt with incorrect alignment:" | |
3753 | " offset=%08x %08x, req.alignment=%llx, req.map_and_fenceable=%d," | |
3754 | " obj->map_and_fenceable=%d\n", | |
3755 | upper_32_bits(vma->node.start), | |
3756 | lower_32_bits(vma->node.start), | |
3757 | alignment, | |
3758 | !!(flags & PIN_MAPPABLE), | |
3759 | obj->map_and_fenceable); | |
3760 | ret = i915_vma_unbind(vma); | |
3761 | if (ret) | |
3762 | return ret; | |
3763 | } | |
3764 | ||
3765 | return i915_vma_pin(vma, size, alignment, flags | PIN_GLOBAL); | |
ec7adb6e JL |
3766 | } |
3767 | ||
673a394b | 3768 | void |
e6617330 TU |
3769 | i915_gem_object_ggtt_unpin_view(struct drm_i915_gem_object *obj, |
3770 | const struct i915_ggtt_view *view) | |
673a394b | 3771 | { |
de895082 | 3772 | i915_vma_unpin(i915_gem_obj_to_ggtt_view(obj, view)); |
673a394b EA |
3773 | } |
3774 | ||
673a394b EA |
3775 | int |
3776 | i915_gem_busy_ioctl(struct drm_device *dev, void *data, | |
05394f39 | 3777 | struct drm_file *file) |
673a394b EA |
3778 | { |
3779 | struct drm_i915_gem_busy *args = data; | |
05394f39 | 3780 | struct drm_i915_gem_object *obj; |
30dbf0c0 CW |
3781 | int ret; |
3782 | ||
76c1dec1 | 3783 | ret = i915_mutex_lock_interruptible(dev); |
1d7cfea1 | 3784 | if (ret) |
76c1dec1 | 3785 | return ret; |
673a394b | 3786 | |
03ac0642 CW |
3787 | obj = i915_gem_object_lookup(file, args->handle); |
3788 | if (!obj) { | |
1d7cfea1 CW |
3789 | ret = -ENOENT; |
3790 | goto unlock; | |
673a394b | 3791 | } |
d1b851fc | 3792 | |
0be555b6 CW |
3793 | /* Count all active objects as busy, even if they are currently not used |
3794 | * by the gpu. Users of this interface expect objects to eventually | |
21c310f2 | 3795 | * become non-busy without any further actions. |
c4de0a5d | 3796 | */ |
426960be | 3797 | args->busy = 0; |
573adb39 | 3798 | if (i915_gem_object_is_active(obj)) { |
27c01aae | 3799 | struct drm_i915_gem_request *req; |
426960be CW |
3800 | int i; |
3801 | ||
666796da | 3802 | for (i = 0; i < I915_NUM_ENGINES; i++) { |
d72d908b CW |
3803 | req = i915_gem_active_peek(&obj->last_read[i], |
3804 | &obj->base.dev->struct_mutex); | |
426960be | 3805 | if (req) |
4a570db5 | 3806 | args->busy |= 1 << (16 + req->engine->exec_id); |
426960be | 3807 | } |
d72d908b CW |
3808 | req = i915_gem_active_peek(&obj->last_write, |
3809 | &obj->base.dev->struct_mutex); | |
27c01aae CW |
3810 | if (req) |
3811 | args->busy |= req->engine->exec_id; | |
426960be | 3812 | } |
673a394b | 3813 | |
f8c417cd | 3814 | i915_gem_object_put(obj); |
1d7cfea1 | 3815 | unlock: |
673a394b | 3816 | mutex_unlock(&dev->struct_mutex); |
1d7cfea1 | 3817 | return ret; |
673a394b EA |
3818 | } |
3819 | ||
3820 | int | |
3821 | i915_gem_throttle_ioctl(struct drm_device *dev, void *data, | |
3822 | struct drm_file *file_priv) | |
3823 | { | |
0206e353 | 3824 | return i915_gem_ring_throttle(dev, file_priv); |
673a394b EA |
3825 | } |
3826 | ||
3ef94daa CW |
3827 | int |
3828 | i915_gem_madvise_ioctl(struct drm_device *dev, void *data, | |
3829 | struct drm_file *file_priv) | |
3830 | { | |
fac5e23e | 3831 | struct drm_i915_private *dev_priv = to_i915(dev); |
3ef94daa | 3832 | struct drm_i915_gem_madvise *args = data; |
05394f39 | 3833 | struct drm_i915_gem_object *obj; |
76c1dec1 | 3834 | int ret; |
3ef94daa CW |
3835 | |
3836 | switch (args->madv) { | |
3837 | case I915_MADV_DONTNEED: | |
3838 | case I915_MADV_WILLNEED: | |
3839 | break; | |
3840 | default: | |
3841 | return -EINVAL; | |
3842 | } | |
3843 | ||
1d7cfea1 CW |
3844 | ret = i915_mutex_lock_interruptible(dev); |
3845 | if (ret) | |
3846 | return ret; | |
3847 | ||
03ac0642 CW |
3848 | obj = i915_gem_object_lookup(file_priv, args->handle); |
3849 | if (!obj) { | |
1d7cfea1 CW |
3850 | ret = -ENOENT; |
3851 | goto unlock; | |
3ef94daa | 3852 | } |
3ef94daa | 3853 | |
d7f46fc4 | 3854 | if (i915_gem_obj_is_pinned(obj)) { |
1d7cfea1 CW |
3855 | ret = -EINVAL; |
3856 | goto out; | |
3ef94daa CW |
3857 | } |
3858 | ||
656bfa3a DV |
3859 | if (obj->pages && |
3860 | obj->tiling_mode != I915_TILING_NONE && | |
3861 | dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) { | |
3862 | if (obj->madv == I915_MADV_WILLNEED) | |
3863 | i915_gem_object_unpin_pages(obj); | |
3864 | if (args->madv == I915_MADV_WILLNEED) | |
3865 | i915_gem_object_pin_pages(obj); | |
3866 | } | |
3867 | ||
05394f39 CW |
3868 | if (obj->madv != __I915_MADV_PURGED) |
3869 | obj->madv = args->madv; | |
3ef94daa | 3870 | |
6c085a72 | 3871 | /* if the object is no longer attached, discard its backing storage */ |
be6a0376 | 3872 | if (obj->madv == I915_MADV_DONTNEED && obj->pages == NULL) |
2d7ef395 CW |
3873 | i915_gem_object_truncate(obj); |
3874 | ||
05394f39 | 3875 | args->retained = obj->madv != __I915_MADV_PURGED; |
bb6baf76 | 3876 | |
1d7cfea1 | 3877 | out: |
f8c417cd | 3878 | i915_gem_object_put(obj); |
1d7cfea1 | 3879 | unlock: |
3ef94daa | 3880 | mutex_unlock(&dev->struct_mutex); |
1d7cfea1 | 3881 | return ret; |
3ef94daa CW |
3882 | } |
3883 | ||
37e680a1 CW |
3884 | void i915_gem_object_init(struct drm_i915_gem_object *obj, |
3885 | const struct drm_i915_gem_object_ops *ops) | |
0327d6ba | 3886 | { |
b4716185 CW |
3887 | int i; |
3888 | ||
35c20a60 | 3889 | INIT_LIST_HEAD(&obj->global_list); |
666796da | 3890 | for (i = 0; i < I915_NUM_ENGINES; i++) |
fa545cbf CW |
3891 | init_request_active(&obj->last_read[i], |
3892 | i915_gem_object_retire__read); | |
3893 | init_request_active(&obj->last_write, | |
3894 | i915_gem_object_retire__write); | |
3895 | init_request_active(&obj->last_fence, NULL); | |
b25cb2f8 | 3896 | INIT_LIST_HEAD(&obj->obj_exec_link); |
2f633156 | 3897 | INIT_LIST_HEAD(&obj->vma_list); |
8d9d5744 | 3898 | INIT_LIST_HEAD(&obj->batch_pool_link); |
0327d6ba | 3899 | |
37e680a1 CW |
3900 | obj->ops = ops; |
3901 | ||
0327d6ba CW |
3902 | obj->fence_reg = I915_FENCE_REG_NONE; |
3903 | obj->madv = I915_MADV_WILLNEED; | |
0327d6ba | 3904 | |
f19ec8cb | 3905 | i915_gem_info_add_obj(to_i915(obj->base.dev), obj->base.size); |
0327d6ba CW |
3906 | } |
3907 | ||
37e680a1 | 3908 | static const struct drm_i915_gem_object_ops i915_gem_object_ops = { |
de472664 | 3909 | .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE, |
37e680a1 CW |
3910 | .get_pages = i915_gem_object_get_pages_gtt, |
3911 | .put_pages = i915_gem_object_put_pages_gtt, | |
3912 | }; | |
3913 | ||
d37cd8a8 | 3914 | struct drm_i915_gem_object *i915_gem_object_create(struct drm_device *dev, |
05394f39 | 3915 | size_t size) |
ac52bc56 | 3916 | { |
c397b908 | 3917 | struct drm_i915_gem_object *obj; |
5949eac4 | 3918 | struct address_space *mapping; |
1a240d4d | 3919 | gfp_t mask; |
fe3db79b | 3920 | int ret; |
ac52bc56 | 3921 | |
42dcedd4 | 3922 | obj = i915_gem_object_alloc(dev); |
c397b908 | 3923 | if (obj == NULL) |
fe3db79b | 3924 | return ERR_PTR(-ENOMEM); |
673a394b | 3925 | |
fe3db79b CW |
3926 | ret = drm_gem_object_init(dev, &obj->base, size); |
3927 | if (ret) | |
3928 | goto fail; | |
673a394b | 3929 | |
bed1ea95 CW |
3930 | mask = GFP_HIGHUSER | __GFP_RECLAIMABLE; |
3931 | if (IS_CRESTLINE(dev) || IS_BROADWATER(dev)) { | |
3932 | /* 965gm cannot relocate objects above 4GiB. */ | |
3933 | mask &= ~__GFP_HIGHMEM; | |
3934 | mask |= __GFP_DMA32; | |
3935 | } | |
3936 | ||
496ad9aa | 3937 | mapping = file_inode(obj->base.filp)->i_mapping; |
bed1ea95 | 3938 | mapping_set_gfp_mask(mapping, mask); |
5949eac4 | 3939 | |
37e680a1 | 3940 | i915_gem_object_init(obj, &i915_gem_object_ops); |
73aa808f | 3941 | |
c397b908 DV |
3942 | obj->base.write_domain = I915_GEM_DOMAIN_CPU; |
3943 | obj->base.read_domains = I915_GEM_DOMAIN_CPU; | |
673a394b | 3944 | |
3d29b842 ED |
3945 | if (HAS_LLC(dev)) { |
3946 | /* On some devices, we can have the GPU use the LLC (the CPU | |
a1871112 EA |
3947 | * cache) for about a 10% performance improvement |
3948 | * compared to uncached. Graphics requests other than | |
3949 | * display scanout are coherent with the CPU in | |
3950 | * accessing this cache. This means in this mode we | |
3951 | * don't need to clflush on the CPU side, and on the | |
3952 | * GPU side we only need to flush internal caches to | |
3953 | * get data visible to the CPU. | |
3954 | * | |
3955 | * However, we maintain the display planes as UC, and so | |
3956 | * need to rebind when first used as such. | |
3957 | */ | |
3958 | obj->cache_level = I915_CACHE_LLC; | |
3959 | } else | |
3960 | obj->cache_level = I915_CACHE_NONE; | |
3961 | ||
d861e338 DV |
3962 | trace_i915_gem_object_create(obj); |
3963 | ||
05394f39 | 3964 | return obj; |
fe3db79b CW |
3965 | |
3966 | fail: | |
3967 | i915_gem_object_free(obj); | |
3968 | ||
3969 | return ERR_PTR(ret); | |
c397b908 DV |
3970 | } |
3971 | ||
340fbd8c CW |
3972 | static bool discard_backing_storage(struct drm_i915_gem_object *obj) |
3973 | { | |
3974 | /* If we are the last user of the backing storage (be it shmemfs | |
3975 | * pages or stolen etc), we know that the pages are going to be | |
3976 | * immediately released. In this case, we can then skip copying | |
3977 | * back the contents from the GPU. | |
3978 | */ | |
3979 | ||
3980 | if (obj->madv != I915_MADV_WILLNEED) | |
3981 | return false; | |
3982 | ||
3983 | if (obj->base.filp == NULL) | |
3984 | return true; | |
3985 | ||
3986 | /* At first glance, this looks racy, but then again so would be | |
3987 | * userspace racing mmap against close. However, the first external | |
3988 | * reference to the filp can only be obtained through the | |
3989 | * i915_gem_mmap_ioctl() which safeguards us against the user | |
3990 | * acquiring such a reference whilst we are in the middle of | |
3991 | * freeing the object. | |
3992 | */ | |
3993 | return atomic_long_read(&obj->base.filp->f_count) == 1; | |
3994 | } | |
3995 | ||
1488fc08 | 3996 | void i915_gem_free_object(struct drm_gem_object *gem_obj) |
673a394b | 3997 | { |
1488fc08 | 3998 | struct drm_i915_gem_object *obj = to_intel_bo(gem_obj); |
05394f39 | 3999 | struct drm_device *dev = obj->base.dev; |
fac5e23e | 4000 | struct drm_i915_private *dev_priv = to_i915(dev); |
07fe0b12 | 4001 | struct i915_vma *vma, *next; |
673a394b | 4002 | |
f65c9168 PZ |
4003 | intel_runtime_pm_get(dev_priv); |
4004 | ||
26e12f89 CW |
4005 | trace_i915_gem_object_destroy(obj); |
4006 | ||
b1f788c6 CW |
4007 | /* All file-owned VMA should have been released by this point through |
4008 | * i915_gem_close_object(), or earlier by i915_gem_context_close(). | |
4009 | * However, the object may also be bound into the global GTT (e.g. | |
4010 | * older GPUs without per-process support, or for direct access through | |
4011 | * the GTT either for the user or for scanout). Those VMA still need to | |
4012 | * unbound now. | |
4013 | */ | |
1c7f4bca | 4014 | list_for_each_entry_safe(vma, next, &obj->vma_list, obj_link) { |
3272db53 | 4015 | GEM_BUG_ON(!i915_vma_is_ggtt(vma)); |
b1f788c6 | 4016 | GEM_BUG_ON(i915_vma_is_active(vma)); |
3272db53 | 4017 | vma->flags &= ~I915_VMA_PIN_MASK; |
b1f788c6 | 4018 | i915_vma_close(vma); |
1488fc08 | 4019 | } |
15717de2 | 4020 | GEM_BUG_ON(obj->bind_count); |
1488fc08 | 4021 | |
1d64ae71 BW |
4022 | /* Stolen objects don't hold a ref, but do hold pin count. Fix that up |
4023 | * before progressing. */ | |
4024 | if (obj->stolen) | |
4025 | i915_gem_object_unpin_pages(obj); | |
4026 | ||
faf5bf0a | 4027 | WARN_ON(atomic_read(&obj->frontbuffer_bits)); |
a071fa00 | 4028 | |
656bfa3a DV |
4029 | if (obj->pages && obj->madv == I915_MADV_WILLNEED && |
4030 | dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES && | |
4031 | obj->tiling_mode != I915_TILING_NONE) | |
4032 | i915_gem_object_unpin_pages(obj); | |
4033 | ||
401c29f6 BW |
4034 | if (WARN_ON(obj->pages_pin_count)) |
4035 | obj->pages_pin_count = 0; | |
340fbd8c | 4036 | if (discard_backing_storage(obj)) |
5537252b | 4037 | obj->madv = I915_MADV_DONTNEED; |
37e680a1 | 4038 | i915_gem_object_put_pages(obj); |
de151cf6 | 4039 | |
9da3da66 CW |
4040 | BUG_ON(obj->pages); |
4041 | ||
2f745ad3 CW |
4042 | if (obj->base.import_attach) |
4043 | drm_prime_gem_destroy(&obj->base, NULL); | |
de151cf6 | 4044 | |
5cc9ed4b CW |
4045 | if (obj->ops->release) |
4046 | obj->ops->release(obj); | |
4047 | ||
05394f39 CW |
4048 | drm_gem_object_release(&obj->base); |
4049 | i915_gem_info_remove_obj(dev_priv, obj->base.size); | |
c397b908 | 4050 | |
05394f39 | 4051 | kfree(obj->bit_17); |
42dcedd4 | 4052 | i915_gem_object_free(obj); |
f65c9168 PZ |
4053 | |
4054 | intel_runtime_pm_put(dev_priv); | |
673a394b EA |
4055 | } |
4056 | ||
ec7adb6e JL |
4057 | struct i915_vma *i915_gem_obj_to_vma(struct drm_i915_gem_object *obj, |
4058 | struct i915_address_space *vm) | |
e656a6cb DV |
4059 | { |
4060 | struct i915_vma *vma; | |
1c7f4bca | 4061 | list_for_each_entry(vma, &obj->vma_list, obj_link) { |
1b683729 TU |
4062 | if (vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL && |
4063 | vma->vm == vm) | |
e656a6cb | 4064 | return vma; |
ec7adb6e JL |
4065 | } |
4066 | return NULL; | |
4067 | } | |
4068 | ||
4069 | struct i915_vma *i915_gem_obj_to_ggtt_view(struct drm_i915_gem_object *obj, | |
4070 | const struct i915_ggtt_view *view) | |
4071 | { | |
ec7adb6e | 4072 | struct i915_vma *vma; |
e656a6cb | 4073 | |
598b9ec8 | 4074 | GEM_BUG_ON(!view); |
ec7adb6e | 4075 | |
1c7f4bca | 4076 | list_for_each_entry(vma, &obj->vma_list, obj_link) |
3272db53 CW |
4077 | if (i915_vma_is_ggtt(vma) && |
4078 | i915_ggtt_view_equal(&vma->ggtt_view, view)) | |
ec7adb6e | 4079 | return vma; |
e656a6cb DV |
4080 | return NULL; |
4081 | } | |
4082 | ||
29105ccc | 4083 | int |
45c5f202 | 4084 | i915_gem_suspend(struct drm_device *dev) |
29105ccc | 4085 | { |
fac5e23e | 4086 | struct drm_i915_private *dev_priv = to_i915(dev); |
45c5f202 | 4087 | int ret = 0; |
28dfe52a | 4088 | |
54b4f68f CW |
4089 | intel_suspend_gt_powersave(dev_priv); |
4090 | ||
45c5f202 | 4091 | mutex_lock(&dev->struct_mutex); |
5ab57c70 CW |
4092 | |
4093 | /* We have to flush all the executing contexts to main memory so | |
4094 | * that they can saved in the hibernation image. To ensure the last | |
4095 | * context image is coherent, we have to switch away from it. That | |
4096 | * leaves the dev_priv->kernel_context still active when | |
4097 | * we actually suspend, and its image in memory may not match the GPU | |
4098 | * state. Fortunately, the kernel_context is disposable and we do | |
4099 | * not rely on its state. | |
4100 | */ | |
4101 | ret = i915_gem_switch_to_kernel_context(dev_priv); | |
4102 | if (ret) | |
4103 | goto err; | |
4104 | ||
6e5a5beb | 4105 | ret = i915_gem_wait_for_idle(dev_priv); |
f7403347 | 4106 | if (ret) |
45c5f202 | 4107 | goto err; |
f7403347 | 4108 | |
c033666a | 4109 | i915_gem_retire_requests(dev_priv); |
673a394b | 4110 | |
b2e862d0 | 4111 | i915_gem_context_lost(dev_priv); |
45c5f202 CW |
4112 | mutex_unlock(&dev->struct_mutex); |
4113 | ||
737b1506 | 4114 | cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work); |
67d97da3 CW |
4115 | cancel_delayed_work_sync(&dev_priv->gt.retire_work); |
4116 | flush_delayed_work(&dev_priv->gt.idle_work); | |
29105ccc | 4117 | |
bdcf120b CW |
4118 | /* Assert that we sucessfully flushed all the work and |
4119 | * reset the GPU back to its idle, low power state. | |
4120 | */ | |
67d97da3 | 4121 | WARN_ON(dev_priv->gt.awake); |
bdcf120b | 4122 | |
673a394b | 4123 | return 0; |
45c5f202 CW |
4124 | |
4125 | err: | |
4126 | mutex_unlock(&dev->struct_mutex); | |
4127 | return ret; | |
673a394b EA |
4128 | } |
4129 | ||
5ab57c70 CW |
4130 | void i915_gem_resume(struct drm_device *dev) |
4131 | { | |
4132 | struct drm_i915_private *dev_priv = to_i915(dev); | |
4133 | ||
4134 | mutex_lock(&dev->struct_mutex); | |
4135 | i915_gem_restore_gtt_mappings(dev); | |
4136 | ||
4137 | /* As we didn't flush the kernel context before suspend, we cannot | |
4138 | * guarantee that the context image is complete. So let's just reset | |
4139 | * it and start again. | |
4140 | */ | |
4141 | if (i915.enable_execlists) | |
4142 | intel_lr_context_reset(dev_priv, dev_priv->kernel_context); | |
4143 | ||
4144 | mutex_unlock(&dev->struct_mutex); | |
4145 | } | |
4146 | ||
f691e2f4 DV |
4147 | void i915_gem_init_swizzling(struct drm_device *dev) |
4148 | { | |
fac5e23e | 4149 | struct drm_i915_private *dev_priv = to_i915(dev); |
f691e2f4 | 4150 | |
11782b02 | 4151 | if (INTEL_INFO(dev)->gen < 5 || |
f691e2f4 DV |
4152 | dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE) |
4153 | return; | |
4154 | ||
4155 | I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) | | |
4156 | DISP_TILE_SURFACE_SWIZZLING); | |
4157 | ||
11782b02 DV |
4158 | if (IS_GEN5(dev)) |
4159 | return; | |
4160 | ||
f691e2f4 DV |
4161 | I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL); |
4162 | if (IS_GEN6(dev)) | |
6b26c86d | 4163 | I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB)); |
8782e26c | 4164 | else if (IS_GEN7(dev)) |
6b26c86d | 4165 | I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB)); |
31a5336e BW |
4166 | else if (IS_GEN8(dev)) |
4167 | I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW)); | |
8782e26c BW |
4168 | else |
4169 | BUG(); | |
f691e2f4 | 4170 | } |
e21af88d | 4171 | |
81e7f200 VS |
4172 | static void init_unused_ring(struct drm_device *dev, u32 base) |
4173 | { | |
fac5e23e | 4174 | struct drm_i915_private *dev_priv = to_i915(dev); |
81e7f200 VS |
4175 | |
4176 | I915_WRITE(RING_CTL(base), 0); | |
4177 | I915_WRITE(RING_HEAD(base), 0); | |
4178 | I915_WRITE(RING_TAIL(base), 0); | |
4179 | I915_WRITE(RING_START(base), 0); | |
4180 | } | |
4181 | ||
4182 | static void init_unused_rings(struct drm_device *dev) | |
4183 | { | |
4184 | if (IS_I830(dev)) { | |
4185 | init_unused_ring(dev, PRB1_BASE); | |
4186 | init_unused_ring(dev, SRB0_BASE); | |
4187 | init_unused_ring(dev, SRB1_BASE); | |
4188 | init_unused_ring(dev, SRB2_BASE); | |
4189 | init_unused_ring(dev, SRB3_BASE); | |
4190 | } else if (IS_GEN2(dev)) { | |
4191 | init_unused_ring(dev, SRB0_BASE); | |
4192 | init_unused_ring(dev, SRB1_BASE); | |
4193 | } else if (IS_GEN3(dev)) { | |
4194 | init_unused_ring(dev, PRB1_BASE); | |
4195 | init_unused_ring(dev, PRB2_BASE); | |
4196 | } | |
4197 | } | |
4198 | ||
4fc7c971 BW |
4199 | int |
4200 | i915_gem_init_hw(struct drm_device *dev) | |
4201 | { | |
fac5e23e | 4202 | struct drm_i915_private *dev_priv = to_i915(dev); |
e2f80391 | 4203 | struct intel_engine_cs *engine; |
d200cda6 | 4204 | int ret; |
4fc7c971 | 4205 | |
5e4f5189 CW |
4206 | /* Double layer security blanket, see i915_gem_init() */ |
4207 | intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL); | |
4208 | ||
3accaf7e | 4209 | if (HAS_EDRAM(dev) && INTEL_GEN(dev_priv) < 9) |
05e21cc4 | 4210 | I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf)); |
4fc7c971 | 4211 | |
0bf21347 VS |
4212 | if (IS_HASWELL(dev)) |
4213 | I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev) ? | |
4214 | LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED); | |
9435373e | 4215 | |
88a2b2a3 | 4216 | if (HAS_PCH_NOP(dev)) { |
6ba844b0 DV |
4217 | if (IS_IVYBRIDGE(dev)) { |
4218 | u32 temp = I915_READ(GEN7_MSG_CTL); | |
4219 | temp &= ~(WAIT_FOR_PCH_FLR_ACK | WAIT_FOR_PCH_RESET_ACK); | |
4220 | I915_WRITE(GEN7_MSG_CTL, temp); | |
4221 | } else if (INTEL_INFO(dev)->gen >= 7) { | |
4222 | u32 temp = I915_READ(HSW_NDE_RSTWRN_OPT); | |
4223 | temp &= ~RESET_PCH_HANDSHAKE_ENABLE; | |
4224 | I915_WRITE(HSW_NDE_RSTWRN_OPT, temp); | |
4225 | } | |
88a2b2a3 BW |
4226 | } |
4227 | ||
4fc7c971 BW |
4228 | i915_gem_init_swizzling(dev); |
4229 | ||
d5abdfda DV |
4230 | /* |
4231 | * At least 830 can leave some of the unused rings | |
4232 | * "active" (ie. head != tail) after resume which | |
4233 | * will prevent c3 entry. Makes sure all unused rings | |
4234 | * are totally idle. | |
4235 | */ | |
4236 | init_unused_rings(dev); | |
4237 | ||
ed54c1a1 | 4238 | BUG_ON(!dev_priv->kernel_context); |
90638cc1 | 4239 | |
4ad2fd88 JH |
4240 | ret = i915_ppgtt_init_hw(dev); |
4241 | if (ret) { | |
4242 | DRM_ERROR("PPGTT enable HW failed %d\n", ret); | |
4243 | goto out; | |
4244 | } | |
4245 | ||
4246 | /* Need to do basic initialisation of all rings first: */ | |
b4ac5afc | 4247 | for_each_engine(engine, dev_priv) { |
e2f80391 | 4248 | ret = engine->init_hw(engine); |
35a57ffb | 4249 | if (ret) |
5e4f5189 | 4250 | goto out; |
35a57ffb | 4251 | } |
99433931 | 4252 | |
0ccdacf6 PA |
4253 | intel_mocs_init_l3cc_table(dev); |
4254 | ||
33a732f4 | 4255 | /* We can't enable contexts until all firmware is loaded */ |
e556f7c1 DG |
4256 | ret = intel_guc_setup(dev); |
4257 | if (ret) | |
4258 | goto out; | |
33a732f4 | 4259 | |
5e4f5189 CW |
4260 | out: |
4261 | intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL); | |
2fa48d8d | 4262 | return ret; |
8187a2b7 ZN |
4263 | } |
4264 | ||
39df9190 CW |
4265 | bool intel_sanitize_semaphores(struct drm_i915_private *dev_priv, int value) |
4266 | { | |
4267 | if (INTEL_INFO(dev_priv)->gen < 6) | |
4268 | return false; | |
4269 | ||
4270 | /* TODO: make semaphores and Execlists play nicely together */ | |
4271 | if (i915.enable_execlists) | |
4272 | return false; | |
4273 | ||
4274 | if (value >= 0) | |
4275 | return value; | |
4276 | ||
4277 | #ifdef CONFIG_INTEL_IOMMU | |
4278 | /* Enable semaphores on SNB when IO remapping is off */ | |
4279 | if (INTEL_INFO(dev_priv)->gen == 6 && intel_iommu_gfx_mapped) | |
4280 | return false; | |
4281 | #endif | |
4282 | ||
4283 | return true; | |
4284 | } | |
4285 | ||
1070a42b CW |
4286 | int i915_gem_init(struct drm_device *dev) |
4287 | { | |
fac5e23e | 4288 | struct drm_i915_private *dev_priv = to_i915(dev); |
1070a42b CW |
4289 | int ret; |
4290 | ||
1070a42b | 4291 | mutex_lock(&dev->struct_mutex); |
d62b4892 | 4292 | |
a83014d3 | 4293 | if (!i915.enable_execlists) { |
7e37f889 | 4294 | dev_priv->gt.cleanup_engine = intel_engine_cleanup; |
454afebd | 4295 | } else { |
117897f4 | 4296 | dev_priv->gt.cleanup_engine = intel_logical_ring_cleanup; |
a83014d3 OM |
4297 | } |
4298 | ||
5e4f5189 CW |
4299 | /* This is just a security blanket to placate dragons. |
4300 | * On some systems, we very sporadically observe that the first TLBs | |
4301 | * used by the CS may be stale, despite us poking the TLB reset. If | |
4302 | * we hold the forcewake during initialisation these problems | |
4303 | * just magically go away. | |
4304 | */ | |
4305 | intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL); | |
4306 | ||
72778cb2 | 4307 | i915_gem_init_userptr(dev_priv); |
f6b9d5ca CW |
4308 | |
4309 | ret = i915_gem_init_ggtt(dev_priv); | |
4310 | if (ret) | |
4311 | goto out_unlock; | |
d62b4892 | 4312 | |
2fa48d8d | 4313 | ret = i915_gem_context_init(dev); |
7bcc3777 JN |
4314 | if (ret) |
4315 | goto out_unlock; | |
2fa48d8d | 4316 | |
8b3e2d36 | 4317 | ret = intel_engines_init(dev); |
35a57ffb | 4318 | if (ret) |
7bcc3777 | 4319 | goto out_unlock; |
2fa48d8d | 4320 | |
1070a42b | 4321 | ret = i915_gem_init_hw(dev); |
60990320 | 4322 | if (ret == -EIO) { |
7e21d648 | 4323 | /* Allow engine initialisation to fail by marking the GPU as |
60990320 CW |
4324 | * wedged. But we only want to do this where the GPU is angry, |
4325 | * for all other failure, such as an allocation failure, bail. | |
4326 | */ | |
4327 | DRM_ERROR("Failed to initialize GPU, declaring it wedged\n"); | |
805de8f4 | 4328 | atomic_or(I915_WEDGED, &dev_priv->gpu_error.reset_counter); |
60990320 | 4329 | ret = 0; |
1070a42b | 4330 | } |
7bcc3777 JN |
4331 | |
4332 | out_unlock: | |
5e4f5189 | 4333 | intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL); |
60990320 | 4334 | mutex_unlock(&dev->struct_mutex); |
1070a42b | 4335 | |
60990320 | 4336 | return ret; |
1070a42b CW |
4337 | } |
4338 | ||
8187a2b7 | 4339 | void |
117897f4 | 4340 | i915_gem_cleanup_engines(struct drm_device *dev) |
8187a2b7 | 4341 | { |
fac5e23e | 4342 | struct drm_i915_private *dev_priv = to_i915(dev); |
e2f80391 | 4343 | struct intel_engine_cs *engine; |
8187a2b7 | 4344 | |
b4ac5afc | 4345 | for_each_engine(engine, dev_priv) |
117897f4 | 4346 | dev_priv->gt.cleanup_engine(engine); |
8187a2b7 ZN |
4347 | } |
4348 | ||
64193406 | 4349 | static void |
666796da | 4350 | init_engine_lists(struct intel_engine_cs *engine) |
64193406 | 4351 | { |
0bc40be8 | 4352 | INIT_LIST_HEAD(&engine->request_list); |
64193406 CW |
4353 | } |
4354 | ||
40ae4e16 ID |
4355 | void |
4356 | i915_gem_load_init_fences(struct drm_i915_private *dev_priv) | |
4357 | { | |
91c8a326 | 4358 | struct drm_device *dev = &dev_priv->drm; |
40ae4e16 ID |
4359 | |
4360 | if (INTEL_INFO(dev_priv)->gen >= 7 && !IS_VALLEYVIEW(dev_priv) && | |
4361 | !IS_CHERRYVIEW(dev_priv)) | |
4362 | dev_priv->num_fence_regs = 32; | |
4363 | else if (INTEL_INFO(dev_priv)->gen >= 4 || IS_I945G(dev_priv) || | |
4364 | IS_I945GM(dev_priv) || IS_G33(dev_priv)) | |
4365 | dev_priv->num_fence_regs = 16; | |
4366 | else | |
4367 | dev_priv->num_fence_regs = 8; | |
4368 | ||
c033666a | 4369 | if (intel_vgpu_active(dev_priv)) |
40ae4e16 ID |
4370 | dev_priv->num_fence_regs = |
4371 | I915_READ(vgtif_reg(avail_rs.fence_num)); | |
4372 | ||
4373 | /* Initialize fence registers to zero */ | |
4374 | i915_gem_restore_fences(dev); | |
4375 | ||
4376 | i915_gem_detect_bit_6_swizzle(dev); | |
4377 | } | |
4378 | ||
673a394b | 4379 | void |
d64aa096 | 4380 | i915_gem_load_init(struct drm_device *dev) |
673a394b | 4381 | { |
fac5e23e | 4382 | struct drm_i915_private *dev_priv = to_i915(dev); |
42dcedd4 CW |
4383 | int i; |
4384 | ||
efab6d8d | 4385 | dev_priv->objects = |
42dcedd4 CW |
4386 | kmem_cache_create("i915_gem_object", |
4387 | sizeof(struct drm_i915_gem_object), 0, | |
4388 | SLAB_HWCACHE_ALIGN, | |
4389 | NULL); | |
e20d2ab7 CW |
4390 | dev_priv->vmas = |
4391 | kmem_cache_create("i915_gem_vma", | |
4392 | sizeof(struct i915_vma), 0, | |
4393 | SLAB_HWCACHE_ALIGN, | |
4394 | NULL); | |
efab6d8d CW |
4395 | dev_priv->requests = |
4396 | kmem_cache_create("i915_gem_request", | |
4397 | sizeof(struct drm_i915_gem_request), 0, | |
0eafec6d CW |
4398 | SLAB_HWCACHE_ALIGN | |
4399 | SLAB_RECLAIM_ACCOUNT | | |
4400 | SLAB_DESTROY_BY_RCU, | |
efab6d8d | 4401 | NULL); |
673a394b | 4402 | |
a33afea5 | 4403 | INIT_LIST_HEAD(&dev_priv->context_list); |
6c085a72 CW |
4404 | INIT_LIST_HEAD(&dev_priv->mm.unbound_list); |
4405 | INIT_LIST_HEAD(&dev_priv->mm.bound_list); | |
a09ba7fa | 4406 | INIT_LIST_HEAD(&dev_priv->mm.fence_list); |
666796da TU |
4407 | for (i = 0; i < I915_NUM_ENGINES; i++) |
4408 | init_engine_lists(&dev_priv->engine[i]); | |
4b9de737 | 4409 | for (i = 0; i < I915_MAX_NUM_FENCES; i++) |
007cc8ac | 4410 | INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list); |
67d97da3 | 4411 | INIT_DELAYED_WORK(&dev_priv->gt.retire_work, |
673a394b | 4412 | i915_gem_retire_work_handler); |
67d97da3 | 4413 | INIT_DELAYED_WORK(&dev_priv->gt.idle_work, |
b29c19b6 | 4414 | i915_gem_idle_work_handler); |
1f15b76f | 4415 | init_waitqueue_head(&dev_priv->gpu_error.wait_queue); |
1f83fee0 | 4416 | init_waitqueue_head(&dev_priv->gpu_error.reset_queue); |
31169714 | 4417 | |
72bfa19c CW |
4418 | dev_priv->relative_constants_mode = I915_EXEC_CONSTANTS_REL_GENERAL; |
4419 | ||
19b2dbde | 4420 | INIT_LIST_HEAD(&dev_priv->mm.fence_list); |
10ed13e4 | 4421 | |
6b95a207 | 4422 | init_waitqueue_head(&dev_priv->pending_flip_queue); |
17250b71 | 4423 | |
ce453d81 CW |
4424 | dev_priv->mm.interruptible = true; |
4425 | ||
b5add959 | 4426 | spin_lock_init(&dev_priv->fb_tracking.lock); |
673a394b | 4427 | } |
71acb5eb | 4428 | |
d64aa096 ID |
4429 | void i915_gem_load_cleanup(struct drm_device *dev) |
4430 | { | |
4431 | struct drm_i915_private *dev_priv = to_i915(dev); | |
4432 | ||
4433 | kmem_cache_destroy(dev_priv->requests); | |
4434 | kmem_cache_destroy(dev_priv->vmas); | |
4435 | kmem_cache_destroy(dev_priv->objects); | |
0eafec6d CW |
4436 | |
4437 | /* And ensure that our DESTROY_BY_RCU slabs are truly destroyed */ | |
4438 | rcu_barrier(); | |
d64aa096 ID |
4439 | } |
4440 | ||
461fb99c CW |
4441 | int i915_gem_freeze_late(struct drm_i915_private *dev_priv) |
4442 | { | |
4443 | struct drm_i915_gem_object *obj; | |
4444 | ||
4445 | /* Called just before we write the hibernation image. | |
4446 | * | |
4447 | * We need to update the domain tracking to reflect that the CPU | |
4448 | * will be accessing all the pages to create and restore from the | |
4449 | * hibernation, and so upon restoration those pages will be in the | |
4450 | * CPU domain. | |
4451 | * | |
4452 | * To make sure the hibernation image contains the latest state, | |
4453 | * we update that state just before writing out the image. | |
4454 | */ | |
4455 | ||
4456 | list_for_each_entry(obj, &dev_priv->mm.unbound_list, global_list) { | |
4457 | obj->base.read_domains = I915_GEM_DOMAIN_CPU; | |
4458 | obj->base.write_domain = I915_GEM_DOMAIN_CPU; | |
4459 | } | |
4460 | ||
4461 | list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) { | |
4462 | obj->base.read_domains = I915_GEM_DOMAIN_CPU; | |
4463 | obj->base.write_domain = I915_GEM_DOMAIN_CPU; | |
4464 | } | |
4465 | ||
4466 | return 0; | |
4467 | } | |
4468 | ||
f787a5f5 | 4469 | void i915_gem_release(struct drm_device *dev, struct drm_file *file) |
b962442e | 4470 | { |
f787a5f5 | 4471 | struct drm_i915_file_private *file_priv = file->driver_priv; |
15f7bbc7 | 4472 | struct drm_i915_gem_request *request; |
b962442e EA |
4473 | |
4474 | /* Clean up our request list when the client is going away, so that | |
4475 | * later retire_requests won't dereference our soon-to-be-gone | |
4476 | * file_priv. | |
4477 | */ | |
1c25595f | 4478 | spin_lock(&file_priv->mm.lock); |
15f7bbc7 | 4479 | list_for_each_entry(request, &file_priv->mm.request_list, client_list) |
f787a5f5 | 4480 | request->file_priv = NULL; |
1c25595f | 4481 | spin_unlock(&file_priv->mm.lock); |
b29c19b6 | 4482 | |
2e1b8730 | 4483 | if (!list_empty(&file_priv->rps.link)) { |
8d3afd7d | 4484 | spin_lock(&to_i915(dev)->rps.client_lock); |
2e1b8730 | 4485 | list_del(&file_priv->rps.link); |
8d3afd7d | 4486 | spin_unlock(&to_i915(dev)->rps.client_lock); |
1854d5ca | 4487 | } |
b29c19b6 CW |
4488 | } |
4489 | ||
4490 | int i915_gem_open(struct drm_device *dev, struct drm_file *file) | |
4491 | { | |
4492 | struct drm_i915_file_private *file_priv; | |
e422b888 | 4493 | int ret; |
b29c19b6 CW |
4494 | |
4495 | DRM_DEBUG_DRIVER("\n"); | |
4496 | ||
4497 | file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL); | |
4498 | if (!file_priv) | |
4499 | return -ENOMEM; | |
4500 | ||
4501 | file->driver_priv = file_priv; | |
f19ec8cb | 4502 | file_priv->dev_priv = to_i915(dev); |
ab0e7ff9 | 4503 | file_priv->file = file; |
2e1b8730 | 4504 | INIT_LIST_HEAD(&file_priv->rps.link); |
b29c19b6 CW |
4505 | |
4506 | spin_lock_init(&file_priv->mm.lock); | |
4507 | INIT_LIST_HEAD(&file_priv->mm.request_list); | |
b29c19b6 | 4508 | |
c80ff16e | 4509 | file_priv->bsd_engine = -1; |
de1add36 | 4510 | |
e422b888 BW |
4511 | ret = i915_gem_context_open(dev, file); |
4512 | if (ret) | |
4513 | kfree(file_priv); | |
b29c19b6 | 4514 | |
e422b888 | 4515 | return ret; |
b29c19b6 CW |
4516 | } |
4517 | ||
b680c37a DV |
4518 | /** |
4519 | * i915_gem_track_fb - update frontbuffer tracking | |
d9072a3e GT |
4520 | * @old: current GEM buffer for the frontbuffer slots |
4521 | * @new: new GEM buffer for the frontbuffer slots | |
4522 | * @frontbuffer_bits: bitmask of frontbuffer slots | |
b680c37a DV |
4523 | * |
4524 | * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them | |
4525 | * from @old and setting them in @new. Both @old and @new can be NULL. | |
4526 | */ | |
a071fa00 DV |
4527 | void i915_gem_track_fb(struct drm_i915_gem_object *old, |
4528 | struct drm_i915_gem_object *new, | |
4529 | unsigned frontbuffer_bits) | |
4530 | { | |
faf5bf0a CW |
4531 | /* Control of individual bits within the mask are guarded by |
4532 | * the owning plane->mutex, i.e. we can never see concurrent | |
4533 | * manipulation of individual bits. But since the bitfield as a whole | |
4534 | * is updated using RMW, we need to use atomics in order to update | |
4535 | * the bits. | |
4536 | */ | |
4537 | BUILD_BUG_ON(INTEL_FRONTBUFFER_BITS_PER_PIPE * I915_MAX_PIPES > | |
4538 | sizeof(atomic_t) * BITS_PER_BYTE); | |
4539 | ||
a071fa00 | 4540 | if (old) { |
faf5bf0a CW |
4541 | WARN_ON(!(atomic_read(&old->frontbuffer_bits) & frontbuffer_bits)); |
4542 | atomic_andnot(frontbuffer_bits, &old->frontbuffer_bits); | |
a071fa00 DV |
4543 | } |
4544 | ||
4545 | if (new) { | |
faf5bf0a CW |
4546 | WARN_ON(atomic_read(&new->frontbuffer_bits) & frontbuffer_bits); |
4547 | atomic_or(frontbuffer_bits, &new->frontbuffer_bits); | |
a071fa00 DV |
4548 | } |
4549 | } | |
4550 | ||
a70a3148 | 4551 | /* All the new VM stuff */ |
088e0df4 MT |
4552 | u64 i915_gem_obj_offset(struct drm_i915_gem_object *o, |
4553 | struct i915_address_space *vm) | |
a70a3148 | 4554 | { |
fac5e23e | 4555 | struct drm_i915_private *dev_priv = to_i915(o->base.dev); |
a70a3148 BW |
4556 | struct i915_vma *vma; |
4557 | ||
896ab1a5 | 4558 | WARN_ON(vm == &dev_priv->mm.aliasing_ppgtt->base); |
a70a3148 | 4559 | |
1c7f4bca | 4560 | list_for_each_entry(vma, &o->vma_list, obj_link) { |
3272db53 | 4561 | if (i915_vma_is_ggtt(vma) && |
ec7adb6e JL |
4562 | vma->ggtt_view.type != I915_GGTT_VIEW_NORMAL) |
4563 | continue; | |
4564 | if (vma->vm == vm) | |
a70a3148 | 4565 | return vma->node.start; |
a70a3148 | 4566 | } |
ec7adb6e | 4567 | |
f25748ea DV |
4568 | WARN(1, "%s vma for this object not found.\n", |
4569 | i915_is_ggtt(vm) ? "global" : "ppgtt"); | |
a70a3148 BW |
4570 | return -1; |
4571 | } | |
4572 | ||
088e0df4 MT |
4573 | u64 i915_gem_obj_ggtt_offset_view(struct drm_i915_gem_object *o, |
4574 | const struct i915_ggtt_view *view) | |
a70a3148 BW |
4575 | { |
4576 | struct i915_vma *vma; | |
4577 | ||
1c7f4bca | 4578 | list_for_each_entry(vma, &o->vma_list, obj_link) |
3272db53 CW |
4579 | if (i915_vma_is_ggtt(vma) && |
4580 | i915_ggtt_view_equal(&vma->ggtt_view, view)) | |
ec7adb6e JL |
4581 | return vma->node.start; |
4582 | ||
5678ad73 | 4583 | WARN(1, "global vma for this object not found. (view=%u)\n", view->type); |
ec7adb6e JL |
4584 | return -1; |
4585 | } | |
4586 | ||
4587 | bool i915_gem_obj_bound(struct drm_i915_gem_object *o, | |
4588 | struct i915_address_space *vm) | |
4589 | { | |
4590 | struct i915_vma *vma; | |
4591 | ||
1c7f4bca | 4592 | list_for_each_entry(vma, &o->vma_list, obj_link) { |
3272db53 | 4593 | if (i915_vma_is_ggtt(vma) && |
ec7adb6e JL |
4594 | vma->ggtt_view.type != I915_GGTT_VIEW_NORMAL) |
4595 | continue; | |
4596 | if (vma->vm == vm && drm_mm_node_allocated(&vma->node)) | |
4597 | return true; | |
4598 | } | |
4599 | ||
4600 | return false; | |
4601 | } | |
4602 | ||
4603 | bool i915_gem_obj_ggtt_bound_view(struct drm_i915_gem_object *o, | |
9abc4648 | 4604 | const struct i915_ggtt_view *view) |
ec7adb6e | 4605 | { |
ec7adb6e JL |
4606 | struct i915_vma *vma; |
4607 | ||
1c7f4bca | 4608 | list_for_each_entry(vma, &o->vma_list, obj_link) |
3272db53 | 4609 | if (i915_vma_is_ggtt(vma) && |
9abc4648 | 4610 | i915_ggtt_view_equal(&vma->ggtt_view, view) && |
fe14d5f4 | 4611 | drm_mm_node_allocated(&vma->node)) |
a70a3148 BW |
4612 | return true; |
4613 | ||
4614 | return false; | |
4615 | } | |
4616 | ||
8da32727 | 4617 | unsigned long i915_gem_obj_ggtt_size(struct drm_i915_gem_object *o) |
a70a3148 | 4618 | { |
a70a3148 BW |
4619 | struct i915_vma *vma; |
4620 | ||
8da32727 | 4621 | GEM_BUG_ON(list_empty(&o->vma_list)); |
a70a3148 | 4622 | |
1c7f4bca | 4623 | list_for_each_entry(vma, &o->vma_list, obj_link) { |
3272db53 | 4624 | if (i915_vma_is_ggtt(vma) && |
8da32727 | 4625 | vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL) |
a70a3148 | 4626 | return vma->node.size; |
ec7adb6e | 4627 | } |
8da32727 | 4628 | |
a70a3148 BW |
4629 | return 0; |
4630 | } | |
4631 | ||
ec7adb6e | 4632 | bool i915_gem_obj_is_pinned(struct drm_i915_gem_object *obj) |
5c2abbea BW |
4633 | { |
4634 | struct i915_vma *vma; | |
1c7f4bca | 4635 | list_for_each_entry(vma, &obj->vma_list, obj_link) |
20dfbde4 | 4636 | if (i915_vma_is_pinned(vma)) |
ec7adb6e | 4637 | return true; |
a6631ae1 | 4638 | |
ec7adb6e | 4639 | return false; |
5c2abbea | 4640 | } |
ea70299d | 4641 | |
033908ae DG |
4642 | /* Like i915_gem_object_get_page(), but mark the returned page dirty */ |
4643 | struct page * | |
4644 | i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj, int n) | |
4645 | { | |
4646 | struct page *page; | |
4647 | ||
4648 | /* Only default objects have per-page dirty tracking */ | |
b9bcd14a | 4649 | if (WARN_ON(!i915_gem_object_has_struct_page(obj))) |
033908ae DG |
4650 | return NULL; |
4651 | ||
4652 | page = i915_gem_object_get_page(obj, n); | |
4653 | set_page_dirty(page); | |
4654 | return page; | |
4655 | } | |
4656 | ||
ea70299d DG |
4657 | /* Allocate a new GEM object and fill it with the supplied data */ |
4658 | struct drm_i915_gem_object * | |
4659 | i915_gem_object_create_from_data(struct drm_device *dev, | |
4660 | const void *data, size_t size) | |
4661 | { | |
4662 | struct drm_i915_gem_object *obj; | |
4663 | struct sg_table *sg; | |
4664 | size_t bytes; | |
4665 | int ret; | |
4666 | ||
d37cd8a8 | 4667 | obj = i915_gem_object_create(dev, round_up(size, PAGE_SIZE)); |
fe3db79b | 4668 | if (IS_ERR(obj)) |
ea70299d DG |
4669 | return obj; |
4670 | ||
4671 | ret = i915_gem_object_set_to_cpu_domain(obj, true); | |
4672 | if (ret) | |
4673 | goto fail; | |
4674 | ||
4675 | ret = i915_gem_object_get_pages(obj); | |
4676 | if (ret) | |
4677 | goto fail; | |
4678 | ||
4679 | i915_gem_object_pin_pages(obj); | |
4680 | sg = obj->pages; | |
4681 | bytes = sg_copy_from_buffer(sg->sgl, sg->nents, (void *)data, size); | |
9e7d18c0 | 4682 | obj->dirty = 1; /* Backing store is now out of date */ |
ea70299d DG |
4683 | i915_gem_object_unpin_pages(obj); |
4684 | ||
4685 | if (WARN_ON(bytes != size)) { | |
4686 | DRM_ERROR("Incomplete copy, wrote %zu of %zu", bytes, size); | |
4687 | ret = -EFAULT; | |
4688 | goto fail; | |
4689 | } | |
4690 | ||
4691 | return obj; | |
4692 | ||
4693 | fail: | |
f8c417cd | 4694 | i915_gem_object_put(obj); |
ea70299d DG |
4695 | return ERR_PTR(ret); |
4696 | } |