]>
Commit | Line | Data |
---|---|---|
673a394b EA |
1 | /* |
2 | * Copyright © 2008 Intel Corporation | |
3 | * | |
4 | * Permission is hereby granted, free of charge, to any person obtaining a | |
5 | * copy of this software and associated documentation files (the "Software"), | |
6 | * to deal in the Software without restriction, including without limitation | |
7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, | |
8 | * and/or sell copies of the Software, and to permit persons to whom the | |
9 | * Software is furnished to do so, subject to the following conditions: | |
10 | * | |
11 | * The above copyright notice and this permission notice (including the next | |
12 | * paragraph) shall be included in all copies or substantial portions of the | |
13 | * Software. | |
14 | * | |
15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
18 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
20 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | |
21 | * IN THE SOFTWARE. | |
22 | * | |
23 | * Authors: | |
24 | * Eric Anholt <eric@anholt.net> | |
25 | * | |
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" |
1c5d22f7 | 32 | #include "i915_trace.h" |
652c393a | 33 | #include "intel_drv.h" |
5949eac4 | 34 | #include <linux/shmem_fs.h> |
5a0e3ad6 | 35 | #include <linux/slab.h> |
673a394b | 36 | #include <linux/swap.h> |
79e53945 | 37 | #include <linux/pci.h> |
1286ff73 | 38 | #include <linux/dma-buf.h> |
673a394b | 39 | |
05394f39 | 40 | static void i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj); |
2c22569b CW |
41 | static void i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj, |
42 | bool force); | |
07fe0b12 | 43 | static __must_check int |
23f54483 BW |
44 | i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj, |
45 | bool readonly); | |
05394f39 CW |
46 | static int i915_gem_phys_pwrite(struct drm_device *dev, |
47 | struct drm_i915_gem_object *obj, | |
71acb5eb | 48 | struct drm_i915_gem_pwrite *args, |
05394f39 | 49 | struct drm_file *file); |
673a394b | 50 | |
61050808 CW |
51 | static void i915_gem_write_fence(struct drm_device *dev, int reg, |
52 | struct drm_i915_gem_object *obj); | |
53 | static void i915_gem_object_update_fence(struct drm_i915_gem_object *obj, | |
54 | struct drm_i915_fence_reg *fence, | |
55 | bool enable); | |
56 | ||
7dc19d5a DC |
57 | static unsigned long i915_gem_inactive_count(struct shrinker *shrinker, |
58 | struct shrink_control *sc); | |
59 | static unsigned long i915_gem_inactive_scan(struct shrinker *shrinker, | |
60 | struct shrink_control *sc); | |
d9973b43 CW |
61 | static unsigned long i915_gem_purge(struct drm_i915_private *dev_priv, long target); |
62 | static unsigned long i915_gem_shrink_all(struct drm_i915_private *dev_priv); | |
8c59967c | 63 | static void i915_gem_object_truncate(struct drm_i915_gem_object *obj); |
31169714 | 64 | |
c76ce038 CW |
65 | static bool cpu_cache_is_coherent(struct drm_device *dev, |
66 | enum i915_cache_level level) | |
67 | { | |
68 | return HAS_LLC(dev) || level != I915_CACHE_NONE; | |
69 | } | |
70 | ||
2c22569b CW |
71 | static bool cpu_write_needs_clflush(struct drm_i915_gem_object *obj) |
72 | { | |
73 | if (!cpu_cache_is_coherent(obj->base.dev, obj->cache_level)) | |
74 | return true; | |
75 | ||
76 | return obj->pin_display; | |
77 | } | |
78 | ||
61050808 CW |
79 | static inline void i915_gem_object_fence_lost(struct drm_i915_gem_object *obj) |
80 | { | |
81 | if (obj->tiling_mode) | |
82 | i915_gem_release_mmap(obj); | |
83 | ||
84 | /* As we do not have an associated fence register, we will force | |
85 | * a tiling change if we ever need to acquire one. | |
86 | */ | |
5d82e3e6 | 87 | obj->fence_dirty = false; |
61050808 CW |
88 | obj->fence_reg = I915_FENCE_REG_NONE; |
89 | } | |
90 | ||
73aa808f CW |
91 | /* some bookkeeping */ |
92 | static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv, | |
93 | size_t size) | |
94 | { | |
c20e8355 | 95 | spin_lock(&dev_priv->mm.object_stat_lock); |
73aa808f CW |
96 | dev_priv->mm.object_count++; |
97 | dev_priv->mm.object_memory += size; | |
c20e8355 | 98 | spin_unlock(&dev_priv->mm.object_stat_lock); |
73aa808f CW |
99 | } |
100 | ||
101 | static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv, | |
102 | size_t size) | |
103 | { | |
c20e8355 | 104 | spin_lock(&dev_priv->mm.object_stat_lock); |
73aa808f CW |
105 | dev_priv->mm.object_count--; |
106 | dev_priv->mm.object_memory -= size; | |
c20e8355 | 107 | spin_unlock(&dev_priv->mm.object_stat_lock); |
73aa808f CW |
108 | } |
109 | ||
21dd3734 | 110 | static int |
33196ded | 111 | i915_gem_wait_for_error(struct i915_gpu_error *error) |
30dbf0c0 | 112 | { |
30dbf0c0 CW |
113 | int ret; |
114 | ||
7abb690a DV |
115 | #define EXIT_COND (!i915_reset_in_progress(error) || \ |
116 | i915_terminally_wedged(error)) | |
1f83fee0 | 117 | if (EXIT_COND) |
30dbf0c0 CW |
118 | return 0; |
119 | ||
0a6759c6 DV |
120 | /* |
121 | * Only wait 10 seconds for the gpu reset to complete to avoid hanging | |
122 | * userspace. If it takes that long something really bad is going on and | |
123 | * we should simply try to bail out and fail as gracefully as possible. | |
124 | */ | |
1f83fee0 DV |
125 | ret = wait_event_interruptible_timeout(error->reset_queue, |
126 | EXIT_COND, | |
127 | 10*HZ); | |
0a6759c6 DV |
128 | if (ret == 0) { |
129 | DRM_ERROR("Timed out waiting for the gpu reset to complete\n"); | |
130 | return -EIO; | |
131 | } else if (ret < 0) { | |
30dbf0c0 | 132 | return ret; |
0a6759c6 | 133 | } |
1f83fee0 | 134 | #undef EXIT_COND |
30dbf0c0 | 135 | |
21dd3734 | 136 | return 0; |
30dbf0c0 CW |
137 | } |
138 | ||
54cf91dc | 139 | int i915_mutex_lock_interruptible(struct drm_device *dev) |
76c1dec1 | 140 | { |
33196ded | 141 | struct drm_i915_private *dev_priv = dev->dev_private; |
76c1dec1 CW |
142 | int ret; |
143 | ||
33196ded | 144 | ret = i915_gem_wait_for_error(&dev_priv->gpu_error); |
76c1dec1 CW |
145 | if (ret) |
146 | return ret; | |
147 | ||
148 | ret = mutex_lock_interruptible(&dev->struct_mutex); | |
149 | if (ret) | |
150 | return ret; | |
151 | ||
23bc5982 | 152 | WARN_ON(i915_verify_lists(dev)); |
76c1dec1 CW |
153 | return 0; |
154 | } | |
30dbf0c0 | 155 | |
7d1c4804 | 156 | static inline bool |
05394f39 | 157 | i915_gem_object_is_inactive(struct drm_i915_gem_object *obj) |
7d1c4804 | 158 | { |
9843877d | 159 | return i915_gem_obj_bound_any(obj) && !obj->active; |
7d1c4804 CW |
160 | } |
161 | ||
79e53945 JB |
162 | int |
163 | i915_gem_init_ioctl(struct drm_device *dev, void *data, | |
05394f39 | 164 | struct drm_file *file) |
79e53945 | 165 | { |
93d18799 | 166 | struct drm_i915_private *dev_priv = dev->dev_private; |
79e53945 | 167 | struct drm_i915_gem_init *args = data; |
2021746e | 168 | |
7bb6fb8d DV |
169 | if (drm_core_check_feature(dev, DRIVER_MODESET)) |
170 | return -ENODEV; | |
171 | ||
2021746e CW |
172 | if (args->gtt_start >= args->gtt_end || |
173 | (args->gtt_end | args->gtt_start) & (PAGE_SIZE - 1)) | |
174 | return -EINVAL; | |
79e53945 | 175 | |
f534bc0b DV |
176 | /* GEM with user mode setting was never supported on ilk and later. */ |
177 | if (INTEL_INFO(dev)->gen >= 5) | |
178 | return -ENODEV; | |
179 | ||
79e53945 | 180 | mutex_lock(&dev->struct_mutex); |
d7e5008f BW |
181 | i915_gem_setup_global_gtt(dev, args->gtt_start, args->gtt_end, |
182 | args->gtt_end); | |
93d18799 | 183 | dev_priv->gtt.mappable_end = args->gtt_end; |
673a394b EA |
184 | mutex_unlock(&dev->struct_mutex); |
185 | ||
2021746e | 186 | return 0; |
673a394b EA |
187 | } |
188 | ||
5a125c3c EA |
189 | int |
190 | i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data, | |
05394f39 | 191 | struct drm_file *file) |
5a125c3c | 192 | { |
73aa808f | 193 | struct drm_i915_private *dev_priv = dev->dev_private; |
5a125c3c | 194 | struct drm_i915_gem_get_aperture *args = data; |
6299f992 CW |
195 | struct drm_i915_gem_object *obj; |
196 | size_t pinned; | |
5a125c3c | 197 | |
6299f992 | 198 | pinned = 0; |
73aa808f | 199 | mutex_lock(&dev->struct_mutex); |
35c20a60 | 200 | list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) |
d7f46fc4 | 201 | if (i915_gem_obj_is_pinned(obj)) |
f343c5f6 | 202 | pinned += i915_gem_obj_ggtt_size(obj); |
73aa808f | 203 | mutex_unlock(&dev->struct_mutex); |
5a125c3c | 204 | |
853ba5d2 | 205 | args->aper_size = dev_priv->gtt.base.total; |
0206e353 | 206 | args->aper_available_size = args->aper_size - pinned; |
6299f992 | 207 | |
5a125c3c EA |
208 | return 0; |
209 | } | |
210 | ||
42dcedd4 CW |
211 | void *i915_gem_object_alloc(struct drm_device *dev) |
212 | { | |
213 | struct drm_i915_private *dev_priv = dev->dev_private; | |
fac15c10 | 214 | return kmem_cache_zalloc(dev_priv->slab, GFP_KERNEL); |
42dcedd4 CW |
215 | } |
216 | ||
217 | void i915_gem_object_free(struct drm_i915_gem_object *obj) | |
218 | { | |
219 | struct drm_i915_private *dev_priv = obj->base.dev->dev_private; | |
220 | kmem_cache_free(dev_priv->slab, obj); | |
221 | } | |
222 | ||
ff72145b DA |
223 | static int |
224 | i915_gem_create(struct drm_file *file, | |
225 | struct drm_device *dev, | |
226 | uint64_t size, | |
227 | uint32_t *handle_p) | |
673a394b | 228 | { |
05394f39 | 229 | struct drm_i915_gem_object *obj; |
a1a2d1d3 PP |
230 | int ret; |
231 | u32 handle; | |
673a394b | 232 | |
ff72145b | 233 | size = roundup(size, PAGE_SIZE); |
8ffc0246 CW |
234 | if (size == 0) |
235 | return -EINVAL; | |
673a394b EA |
236 | |
237 | /* Allocate the new object */ | |
ff72145b | 238 | obj = i915_gem_alloc_object(dev, size); |
673a394b EA |
239 | if (obj == NULL) |
240 | return -ENOMEM; | |
241 | ||
05394f39 | 242 | ret = drm_gem_handle_create(file, &obj->base, &handle); |
202f2fef | 243 | /* drop reference from allocate - handle holds it now */ |
d861e338 DV |
244 | drm_gem_object_unreference_unlocked(&obj->base); |
245 | if (ret) | |
246 | return ret; | |
202f2fef | 247 | |
ff72145b | 248 | *handle_p = handle; |
673a394b EA |
249 | return 0; |
250 | } | |
251 | ||
ff72145b DA |
252 | int |
253 | i915_gem_dumb_create(struct drm_file *file, | |
254 | struct drm_device *dev, | |
255 | struct drm_mode_create_dumb *args) | |
256 | { | |
257 | /* have to work out size/pitch and return them */ | |
de45eaf7 | 258 | args->pitch = ALIGN(args->width * DIV_ROUND_UP(args->bpp, 8), 64); |
ff72145b DA |
259 | args->size = args->pitch * args->height; |
260 | return i915_gem_create(file, dev, | |
261 | args->size, &args->handle); | |
262 | } | |
263 | ||
ff72145b DA |
264 | /** |
265 | * Creates a new mm object and returns a handle to it. | |
266 | */ | |
267 | int | |
268 | i915_gem_create_ioctl(struct drm_device *dev, void *data, | |
269 | struct drm_file *file) | |
270 | { | |
271 | struct drm_i915_gem_create *args = data; | |
63ed2cb2 | 272 | |
ff72145b DA |
273 | return i915_gem_create(file, dev, |
274 | args->size, &args->handle); | |
275 | } | |
276 | ||
8461d226 DV |
277 | static inline int |
278 | __copy_to_user_swizzled(char __user *cpu_vaddr, | |
279 | const char *gpu_vaddr, int gpu_offset, | |
280 | int length) | |
281 | { | |
282 | int ret, cpu_offset = 0; | |
283 | ||
284 | while (length > 0) { | |
285 | int cacheline_end = ALIGN(gpu_offset + 1, 64); | |
286 | int this_length = min(cacheline_end - gpu_offset, length); | |
287 | int swizzled_gpu_offset = gpu_offset ^ 64; | |
288 | ||
289 | ret = __copy_to_user(cpu_vaddr + cpu_offset, | |
290 | gpu_vaddr + swizzled_gpu_offset, | |
291 | this_length); | |
292 | if (ret) | |
293 | return ret + length; | |
294 | ||
295 | cpu_offset += this_length; | |
296 | gpu_offset += this_length; | |
297 | length -= this_length; | |
298 | } | |
299 | ||
300 | return 0; | |
301 | } | |
302 | ||
8c59967c | 303 | static inline int |
4f0c7cfb BW |
304 | __copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset, |
305 | const char __user *cpu_vaddr, | |
8c59967c DV |
306 | int length) |
307 | { | |
308 | int ret, cpu_offset = 0; | |
309 | ||
310 | while (length > 0) { | |
311 | int cacheline_end = ALIGN(gpu_offset + 1, 64); | |
312 | int this_length = min(cacheline_end - gpu_offset, length); | |
313 | int swizzled_gpu_offset = gpu_offset ^ 64; | |
314 | ||
315 | ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset, | |
316 | cpu_vaddr + cpu_offset, | |
317 | this_length); | |
318 | if (ret) | |
319 | return ret + length; | |
320 | ||
321 | cpu_offset += this_length; | |
322 | gpu_offset += this_length; | |
323 | length -= this_length; | |
324 | } | |
325 | ||
326 | return 0; | |
327 | } | |
328 | ||
d174bd64 DV |
329 | /* Per-page copy function for the shmem pread fastpath. |
330 | * Flushes invalid cachelines before reading the target if | |
331 | * needs_clflush is set. */ | |
eb01459f | 332 | static int |
d174bd64 DV |
333 | shmem_pread_fast(struct page *page, int shmem_page_offset, int page_length, |
334 | char __user *user_data, | |
335 | bool page_do_bit17_swizzling, bool needs_clflush) | |
336 | { | |
337 | char *vaddr; | |
338 | int ret; | |
339 | ||
e7e58eb5 | 340 | if (unlikely(page_do_bit17_swizzling)) |
d174bd64 DV |
341 | return -EINVAL; |
342 | ||
343 | vaddr = kmap_atomic(page); | |
344 | if (needs_clflush) | |
345 | drm_clflush_virt_range(vaddr + shmem_page_offset, | |
346 | page_length); | |
347 | ret = __copy_to_user_inatomic(user_data, | |
348 | vaddr + shmem_page_offset, | |
349 | page_length); | |
350 | kunmap_atomic(vaddr); | |
351 | ||
f60d7f0c | 352 | return ret ? -EFAULT : 0; |
d174bd64 DV |
353 | } |
354 | ||
23c18c71 DV |
355 | static void |
356 | shmem_clflush_swizzled_range(char *addr, unsigned long length, | |
357 | bool swizzled) | |
358 | { | |
e7e58eb5 | 359 | if (unlikely(swizzled)) { |
23c18c71 DV |
360 | unsigned long start = (unsigned long) addr; |
361 | unsigned long end = (unsigned long) addr + length; | |
362 | ||
363 | /* For swizzling simply ensure that we always flush both | |
364 | * channels. Lame, but simple and it works. Swizzled | |
365 | * pwrite/pread is far from a hotpath - current userspace | |
366 | * doesn't use it at all. */ | |
367 | start = round_down(start, 128); | |
368 | end = round_up(end, 128); | |
369 | ||
370 | drm_clflush_virt_range((void *)start, end - start); | |
371 | } else { | |
372 | drm_clflush_virt_range(addr, length); | |
373 | } | |
374 | ||
375 | } | |
376 | ||
d174bd64 DV |
377 | /* Only difference to the fast-path function is that this can handle bit17 |
378 | * and uses non-atomic copy and kmap functions. */ | |
379 | static int | |
380 | shmem_pread_slow(struct page *page, int shmem_page_offset, int page_length, | |
381 | char __user *user_data, | |
382 | bool page_do_bit17_swizzling, bool needs_clflush) | |
383 | { | |
384 | char *vaddr; | |
385 | int ret; | |
386 | ||
387 | vaddr = kmap(page); | |
388 | if (needs_clflush) | |
23c18c71 DV |
389 | shmem_clflush_swizzled_range(vaddr + shmem_page_offset, |
390 | page_length, | |
391 | page_do_bit17_swizzling); | |
d174bd64 DV |
392 | |
393 | if (page_do_bit17_swizzling) | |
394 | ret = __copy_to_user_swizzled(user_data, | |
395 | vaddr, shmem_page_offset, | |
396 | page_length); | |
397 | else | |
398 | ret = __copy_to_user(user_data, | |
399 | vaddr + shmem_page_offset, | |
400 | page_length); | |
401 | kunmap(page); | |
402 | ||
f60d7f0c | 403 | return ret ? - EFAULT : 0; |
d174bd64 DV |
404 | } |
405 | ||
eb01459f | 406 | static int |
dbf7bff0 DV |
407 | i915_gem_shmem_pread(struct drm_device *dev, |
408 | struct drm_i915_gem_object *obj, | |
409 | struct drm_i915_gem_pread *args, | |
410 | struct drm_file *file) | |
eb01459f | 411 | { |
8461d226 | 412 | char __user *user_data; |
eb01459f | 413 | ssize_t remain; |
8461d226 | 414 | loff_t offset; |
eb2c0c81 | 415 | int shmem_page_offset, page_length, ret = 0; |
8461d226 | 416 | int obj_do_bit17_swizzling, page_do_bit17_swizzling; |
96d79b52 | 417 | int prefaulted = 0; |
8489731c | 418 | int needs_clflush = 0; |
67d5a50c | 419 | struct sg_page_iter sg_iter; |
eb01459f | 420 | |
2bb4629a | 421 | user_data = to_user_ptr(args->data_ptr); |
eb01459f EA |
422 | remain = args->size; |
423 | ||
8461d226 | 424 | obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj); |
eb01459f | 425 | |
8489731c DV |
426 | if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU)) { |
427 | /* If we're not in the cpu read domain, set ourself into the gtt | |
428 | * read domain and manually flush cachelines (if required). This | |
429 | * optimizes for the case when the gpu will dirty the data | |
430 | * anyway again before the next pread happens. */ | |
c76ce038 | 431 | needs_clflush = !cpu_cache_is_coherent(dev, obj->cache_level); |
23f54483 BW |
432 | ret = i915_gem_object_wait_rendering(obj, true); |
433 | if (ret) | |
434 | return ret; | |
8489731c | 435 | } |
eb01459f | 436 | |
f60d7f0c CW |
437 | ret = i915_gem_object_get_pages(obj); |
438 | if (ret) | |
439 | return ret; | |
440 | ||
441 | i915_gem_object_pin_pages(obj); | |
442 | ||
8461d226 | 443 | offset = args->offset; |
eb01459f | 444 | |
67d5a50c ID |
445 | for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents, |
446 | offset >> PAGE_SHIFT) { | |
2db76d7c | 447 | struct page *page = sg_page_iter_page(&sg_iter); |
9da3da66 CW |
448 | |
449 | if (remain <= 0) | |
450 | break; | |
451 | ||
eb01459f EA |
452 | /* Operation in this page |
453 | * | |
eb01459f | 454 | * shmem_page_offset = offset within page in shmem file |
eb01459f EA |
455 | * page_length = bytes to copy for this page |
456 | */ | |
c8cbbb8b | 457 | shmem_page_offset = offset_in_page(offset); |
eb01459f EA |
458 | page_length = remain; |
459 | if ((shmem_page_offset + page_length) > PAGE_SIZE) | |
460 | page_length = PAGE_SIZE - shmem_page_offset; | |
eb01459f | 461 | |
8461d226 DV |
462 | page_do_bit17_swizzling = obj_do_bit17_swizzling && |
463 | (page_to_phys(page) & (1 << 17)) != 0; | |
464 | ||
d174bd64 DV |
465 | ret = shmem_pread_fast(page, shmem_page_offset, page_length, |
466 | user_data, page_do_bit17_swizzling, | |
467 | needs_clflush); | |
468 | if (ret == 0) | |
469 | goto next_page; | |
dbf7bff0 | 470 | |
dbf7bff0 DV |
471 | mutex_unlock(&dev->struct_mutex); |
472 | ||
d330a953 | 473 | if (likely(!i915.prefault_disable) && !prefaulted) { |
f56f821f | 474 | ret = fault_in_multipages_writeable(user_data, remain); |
96d79b52 DV |
475 | /* Userspace is tricking us, but we've already clobbered |
476 | * its pages with the prefault and promised to write the | |
477 | * data up to the first fault. Hence ignore any errors | |
478 | * and just continue. */ | |
479 | (void)ret; | |
480 | prefaulted = 1; | |
481 | } | |
eb01459f | 482 | |
d174bd64 DV |
483 | ret = shmem_pread_slow(page, shmem_page_offset, page_length, |
484 | user_data, page_do_bit17_swizzling, | |
485 | needs_clflush); | |
eb01459f | 486 | |
dbf7bff0 | 487 | mutex_lock(&dev->struct_mutex); |
f60d7f0c | 488 | |
dbf7bff0 | 489 | next_page: |
e5281ccd | 490 | mark_page_accessed(page); |
e5281ccd | 491 | |
f60d7f0c | 492 | if (ret) |
8461d226 | 493 | goto out; |
8461d226 | 494 | |
eb01459f | 495 | remain -= page_length; |
8461d226 | 496 | user_data += page_length; |
eb01459f EA |
497 | offset += page_length; |
498 | } | |
499 | ||
4f27b75d | 500 | out: |
f60d7f0c CW |
501 | i915_gem_object_unpin_pages(obj); |
502 | ||
eb01459f EA |
503 | return ret; |
504 | } | |
505 | ||
673a394b EA |
506 | /** |
507 | * Reads data from the object referenced by handle. | |
508 | * | |
509 | * On error, the contents of *data are undefined. | |
510 | */ | |
511 | int | |
512 | i915_gem_pread_ioctl(struct drm_device *dev, void *data, | |
05394f39 | 513 | struct drm_file *file) |
673a394b EA |
514 | { |
515 | struct drm_i915_gem_pread *args = data; | |
05394f39 | 516 | struct drm_i915_gem_object *obj; |
35b62a89 | 517 | int ret = 0; |
673a394b | 518 | |
51311d0a CW |
519 | if (args->size == 0) |
520 | return 0; | |
521 | ||
522 | if (!access_ok(VERIFY_WRITE, | |
2bb4629a | 523 | to_user_ptr(args->data_ptr), |
51311d0a CW |
524 | args->size)) |
525 | return -EFAULT; | |
526 | ||
4f27b75d | 527 | ret = i915_mutex_lock_interruptible(dev); |
1d7cfea1 | 528 | if (ret) |
4f27b75d | 529 | return ret; |
673a394b | 530 | |
05394f39 | 531 | obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle)); |
c8725226 | 532 | if (&obj->base == NULL) { |
1d7cfea1 CW |
533 | ret = -ENOENT; |
534 | goto unlock; | |
4f27b75d | 535 | } |
673a394b | 536 | |
7dcd2499 | 537 | /* Bounds check source. */ |
05394f39 CW |
538 | if (args->offset > obj->base.size || |
539 | args->size > obj->base.size - args->offset) { | |
ce9d419d | 540 | ret = -EINVAL; |
35b62a89 | 541 | goto out; |
ce9d419d CW |
542 | } |
543 | ||
1286ff73 DV |
544 | /* prime objects have no backing filp to GEM pread/pwrite |
545 | * pages from. | |
546 | */ | |
547 | if (!obj->base.filp) { | |
548 | ret = -EINVAL; | |
549 | goto out; | |
550 | } | |
551 | ||
db53a302 CW |
552 | trace_i915_gem_object_pread(obj, args->offset, args->size); |
553 | ||
dbf7bff0 | 554 | ret = i915_gem_shmem_pread(dev, obj, args, file); |
673a394b | 555 | |
35b62a89 | 556 | out: |
05394f39 | 557 | drm_gem_object_unreference(&obj->base); |
1d7cfea1 | 558 | unlock: |
4f27b75d | 559 | mutex_unlock(&dev->struct_mutex); |
eb01459f | 560 | return ret; |
673a394b EA |
561 | } |
562 | ||
0839ccb8 KP |
563 | /* This is the fast write path which cannot handle |
564 | * page faults in the source data | |
9b7530cc | 565 | */ |
0839ccb8 KP |
566 | |
567 | static inline int | |
568 | fast_user_write(struct io_mapping *mapping, | |
569 | loff_t page_base, int page_offset, | |
570 | char __user *user_data, | |
571 | int length) | |
9b7530cc | 572 | { |
4f0c7cfb BW |
573 | void __iomem *vaddr_atomic; |
574 | void *vaddr; | |
0839ccb8 | 575 | unsigned long unwritten; |
9b7530cc | 576 | |
3e4d3af5 | 577 | vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base); |
4f0c7cfb BW |
578 | /* We can use the cpu mem copy function because this is X86. */ |
579 | vaddr = (void __force*)vaddr_atomic + page_offset; | |
580 | unwritten = __copy_from_user_inatomic_nocache(vaddr, | |
0839ccb8 | 581 | user_data, length); |
3e4d3af5 | 582 | io_mapping_unmap_atomic(vaddr_atomic); |
fbd5a26d | 583 | return unwritten; |
0839ccb8 KP |
584 | } |
585 | ||
3de09aa3 EA |
586 | /** |
587 | * This is the fast pwrite path, where we copy the data directly from the | |
588 | * user into the GTT, uncached. | |
589 | */ | |
673a394b | 590 | static int |
05394f39 CW |
591 | i915_gem_gtt_pwrite_fast(struct drm_device *dev, |
592 | struct drm_i915_gem_object *obj, | |
3de09aa3 | 593 | struct drm_i915_gem_pwrite *args, |
05394f39 | 594 | struct drm_file *file) |
673a394b | 595 | { |
0839ccb8 | 596 | drm_i915_private_t *dev_priv = dev->dev_private; |
673a394b | 597 | ssize_t remain; |
0839ccb8 | 598 | loff_t offset, page_base; |
673a394b | 599 | char __user *user_data; |
935aaa69 DV |
600 | int page_offset, page_length, ret; |
601 | ||
1ec9e26d | 602 | ret = i915_gem_obj_ggtt_pin(obj, 0, PIN_MAPPABLE | PIN_NONBLOCK); |
935aaa69 DV |
603 | if (ret) |
604 | goto out; | |
605 | ||
606 | ret = i915_gem_object_set_to_gtt_domain(obj, true); | |
607 | if (ret) | |
608 | goto out_unpin; | |
609 | ||
610 | ret = i915_gem_object_put_fence(obj); | |
611 | if (ret) | |
612 | goto out_unpin; | |
673a394b | 613 | |
2bb4629a | 614 | user_data = to_user_ptr(args->data_ptr); |
673a394b | 615 | remain = args->size; |
673a394b | 616 | |
f343c5f6 | 617 | offset = i915_gem_obj_ggtt_offset(obj) + args->offset; |
673a394b EA |
618 | |
619 | while (remain > 0) { | |
620 | /* Operation in this page | |
621 | * | |
0839ccb8 KP |
622 | * page_base = page offset within aperture |
623 | * page_offset = offset within page | |
624 | * page_length = bytes to copy for this page | |
673a394b | 625 | */ |
c8cbbb8b CW |
626 | page_base = offset & PAGE_MASK; |
627 | page_offset = offset_in_page(offset); | |
0839ccb8 KP |
628 | page_length = remain; |
629 | if ((page_offset + remain) > PAGE_SIZE) | |
630 | page_length = PAGE_SIZE - page_offset; | |
631 | ||
0839ccb8 | 632 | /* If we get a fault while copying data, then (presumably) our |
3de09aa3 EA |
633 | * source page isn't available. Return the error and we'll |
634 | * retry in the slow path. | |
0839ccb8 | 635 | */ |
5d4545ae | 636 | if (fast_user_write(dev_priv->gtt.mappable, page_base, |
935aaa69 DV |
637 | page_offset, user_data, page_length)) { |
638 | ret = -EFAULT; | |
639 | goto out_unpin; | |
640 | } | |
673a394b | 641 | |
0839ccb8 KP |
642 | remain -= page_length; |
643 | user_data += page_length; | |
644 | offset += page_length; | |
673a394b | 645 | } |
673a394b | 646 | |
935aaa69 | 647 | out_unpin: |
d7f46fc4 | 648 | i915_gem_object_ggtt_unpin(obj); |
935aaa69 | 649 | out: |
3de09aa3 | 650 | return ret; |
673a394b EA |
651 | } |
652 | ||
d174bd64 DV |
653 | /* Per-page copy function for the shmem pwrite fastpath. |
654 | * Flushes invalid cachelines before writing to the target if | |
655 | * needs_clflush_before is set and flushes out any written cachelines after | |
656 | * writing if needs_clflush is set. */ | |
3043c60c | 657 | static int |
d174bd64 DV |
658 | shmem_pwrite_fast(struct page *page, int shmem_page_offset, int page_length, |
659 | char __user *user_data, | |
660 | bool page_do_bit17_swizzling, | |
661 | bool needs_clflush_before, | |
662 | bool needs_clflush_after) | |
673a394b | 663 | { |
d174bd64 | 664 | char *vaddr; |
673a394b | 665 | int ret; |
3de09aa3 | 666 | |
e7e58eb5 | 667 | if (unlikely(page_do_bit17_swizzling)) |
d174bd64 | 668 | return -EINVAL; |
3de09aa3 | 669 | |
d174bd64 DV |
670 | vaddr = kmap_atomic(page); |
671 | if (needs_clflush_before) | |
672 | drm_clflush_virt_range(vaddr + shmem_page_offset, | |
673 | page_length); | |
674 | ret = __copy_from_user_inatomic_nocache(vaddr + shmem_page_offset, | |
675 | user_data, | |
676 | page_length); | |
677 | if (needs_clflush_after) | |
678 | drm_clflush_virt_range(vaddr + shmem_page_offset, | |
679 | page_length); | |
680 | kunmap_atomic(vaddr); | |
3de09aa3 | 681 | |
755d2218 | 682 | return ret ? -EFAULT : 0; |
3de09aa3 EA |
683 | } |
684 | ||
d174bd64 DV |
685 | /* Only difference to the fast-path function is that this can handle bit17 |
686 | * and uses non-atomic copy and kmap functions. */ | |
3043c60c | 687 | static int |
d174bd64 DV |
688 | shmem_pwrite_slow(struct page *page, int shmem_page_offset, int page_length, |
689 | char __user *user_data, | |
690 | bool page_do_bit17_swizzling, | |
691 | bool needs_clflush_before, | |
692 | bool needs_clflush_after) | |
673a394b | 693 | { |
d174bd64 DV |
694 | char *vaddr; |
695 | int ret; | |
e5281ccd | 696 | |
d174bd64 | 697 | vaddr = kmap(page); |
e7e58eb5 | 698 | if (unlikely(needs_clflush_before || page_do_bit17_swizzling)) |
23c18c71 DV |
699 | shmem_clflush_swizzled_range(vaddr + shmem_page_offset, |
700 | page_length, | |
701 | page_do_bit17_swizzling); | |
d174bd64 DV |
702 | if (page_do_bit17_swizzling) |
703 | ret = __copy_from_user_swizzled(vaddr, shmem_page_offset, | |
e5281ccd CW |
704 | user_data, |
705 | page_length); | |
d174bd64 DV |
706 | else |
707 | ret = __copy_from_user(vaddr + shmem_page_offset, | |
708 | user_data, | |
709 | page_length); | |
710 | if (needs_clflush_after) | |
23c18c71 DV |
711 | shmem_clflush_swizzled_range(vaddr + shmem_page_offset, |
712 | page_length, | |
713 | page_do_bit17_swizzling); | |
d174bd64 | 714 | kunmap(page); |
40123c1f | 715 | |
755d2218 | 716 | return ret ? -EFAULT : 0; |
40123c1f EA |
717 | } |
718 | ||
40123c1f | 719 | static int |
e244a443 DV |
720 | i915_gem_shmem_pwrite(struct drm_device *dev, |
721 | struct drm_i915_gem_object *obj, | |
722 | struct drm_i915_gem_pwrite *args, | |
723 | struct drm_file *file) | |
40123c1f | 724 | { |
40123c1f | 725 | ssize_t remain; |
8c59967c DV |
726 | loff_t offset; |
727 | char __user *user_data; | |
eb2c0c81 | 728 | int shmem_page_offset, page_length, ret = 0; |
8c59967c | 729 | int obj_do_bit17_swizzling, page_do_bit17_swizzling; |
e244a443 | 730 | int hit_slowpath = 0; |
58642885 DV |
731 | int needs_clflush_after = 0; |
732 | int needs_clflush_before = 0; | |
67d5a50c | 733 | struct sg_page_iter sg_iter; |
40123c1f | 734 | |
2bb4629a | 735 | user_data = to_user_ptr(args->data_ptr); |
40123c1f EA |
736 | remain = args->size; |
737 | ||
8c59967c | 738 | obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj); |
40123c1f | 739 | |
58642885 DV |
740 | if (obj->base.write_domain != I915_GEM_DOMAIN_CPU) { |
741 | /* If we're not in the cpu write domain, set ourself into the gtt | |
742 | * write domain and manually flush cachelines (if required). This | |
743 | * optimizes for the case when the gpu will use the data | |
744 | * right away and we therefore have to clflush anyway. */ | |
2c22569b | 745 | needs_clflush_after = cpu_write_needs_clflush(obj); |
23f54483 BW |
746 | ret = i915_gem_object_wait_rendering(obj, false); |
747 | if (ret) | |
748 | return ret; | |
58642885 | 749 | } |
c76ce038 CW |
750 | /* Same trick applies to invalidate partially written cachelines read |
751 | * before writing. */ | |
752 | if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) | |
753 | needs_clflush_before = | |
754 | !cpu_cache_is_coherent(dev, obj->cache_level); | |
58642885 | 755 | |
755d2218 CW |
756 | ret = i915_gem_object_get_pages(obj); |
757 | if (ret) | |
758 | return ret; | |
759 | ||
760 | i915_gem_object_pin_pages(obj); | |
761 | ||
673a394b | 762 | offset = args->offset; |
05394f39 | 763 | obj->dirty = 1; |
673a394b | 764 | |
67d5a50c ID |
765 | for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents, |
766 | offset >> PAGE_SHIFT) { | |
2db76d7c | 767 | struct page *page = sg_page_iter_page(&sg_iter); |
58642885 | 768 | int partial_cacheline_write; |
e5281ccd | 769 | |
9da3da66 CW |
770 | if (remain <= 0) |
771 | break; | |
772 | ||
40123c1f EA |
773 | /* Operation in this page |
774 | * | |
40123c1f | 775 | * shmem_page_offset = offset within page in shmem file |
40123c1f EA |
776 | * page_length = bytes to copy for this page |
777 | */ | |
c8cbbb8b | 778 | shmem_page_offset = offset_in_page(offset); |
40123c1f EA |
779 | |
780 | page_length = remain; | |
781 | if ((shmem_page_offset + page_length) > PAGE_SIZE) | |
782 | page_length = PAGE_SIZE - shmem_page_offset; | |
40123c1f | 783 | |
58642885 DV |
784 | /* If we don't overwrite a cacheline completely we need to be |
785 | * careful to have up-to-date data by first clflushing. Don't | |
786 | * overcomplicate things and flush the entire patch. */ | |
787 | partial_cacheline_write = needs_clflush_before && | |
788 | ((shmem_page_offset | page_length) | |
789 | & (boot_cpu_data.x86_clflush_size - 1)); | |
790 | ||
8c59967c DV |
791 | page_do_bit17_swizzling = obj_do_bit17_swizzling && |
792 | (page_to_phys(page) & (1 << 17)) != 0; | |
793 | ||
d174bd64 DV |
794 | ret = shmem_pwrite_fast(page, shmem_page_offset, page_length, |
795 | user_data, page_do_bit17_swizzling, | |
796 | partial_cacheline_write, | |
797 | needs_clflush_after); | |
798 | if (ret == 0) | |
799 | goto next_page; | |
e244a443 DV |
800 | |
801 | hit_slowpath = 1; | |
e244a443 | 802 | mutex_unlock(&dev->struct_mutex); |
d174bd64 DV |
803 | ret = shmem_pwrite_slow(page, shmem_page_offset, page_length, |
804 | user_data, page_do_bit17_swizzling, | |
805 | partial_cacheline_write, | |
806 | needs_clflush_after); | |
40123c1f | 807 | |
e244a443 | 808 | mutex_lock(&dev->struct_mutex); |
755d2218 | 809 | |
e244a443 | 810 | next_page: |
e5281ccd CW |
811 | set_page_dirty(page); |
812 | mark_page_accessed(page); | |
e5281ccd | 813 | |
755d2218 | 814 | if (ret) |
8c59967c | 815 | goto out; |
8c59967c | 816 | |
40123c1f | 817 | remain -= page_length; |
8c59967c | 818 | user_data += page_length; |
40123c1f | 819 | offset += page_length; |
673a394b EA |
820 | } |
821 | ||
fbd5a26d | 822 | out: |
755d2218 CW |
823 | i915_gem_object_unpin_pages(obj); |
824 | ||
e244a443 | 825 | if (hit_slowpath) { |
8dcf015e DV |
826 | /* |
827 | * Fixup: Flush cpu caches in case we didn't flush the dirty | |
828 | * cachelines in-line while writing and the object moved | |
829 | * out of the cpu write domain while we've dropped the lock. | |
830 | */ | |
831 | if (!needs_clflush_after && | |
832 | obj->base.write_domain != I915_GEM_DOMAIN_CPU) { | |
000433b6 CW |
833 | if (i915_gem_clflush_object(obj, obj->pin_display)) |
834 | i915_gem_chipset_flush(dev); | |
e244a443 | 835 | } |
8c59967c | 836 | } |
673a394b | 837 | |
58642885 | 838 | if (needs_clflush_after) |
e76e9aeb | 839 | i915_gem_chipset_flush(dev); |
58642885 | 840 | |
40123c1f | 841 | return ret; |
673a394b EA |
842 | } |
843 | ||
844 | /** | |
845 | * Writes data to the object referenced by handle. | |
846 | * | |
847 | * On error, the contents of the buffer that were to be modified are undefined. | |
848 | */ | |
849 | int | |
850 | i915_gem_pwrite_ioctl(struct drm_device *dev, void *data, | |
fbd5a26d | 851 | struct drm_file *file) |
673a394b EA |
852 | { |
853 | struct drm_i915_gem_pwrite *args = data; | |
05394f39 | 854 | struct drm_i915_gem_object *obj; |
51311d0a CW |
855 | int ret; |
856 | ||
857 | if (args->size == 0) | |
858 | return 0; | |
859 | ||
860 | if (!access_ok(VERIFY_READ, | |
2bb4629a | 861 | to_user_ptr(args->data_ptr), |
51311d0a CW |
862 | args->size)) |
863 | return -EFAULT; | |
864 | ||
d330a953 | 865 | if (likely(!i915.prefault_disable)) { |
0b74b508 XZ |
866 | ret = fault_in_multipages_readable(to_user_ptr(args->data_ptr), |
867 | args->size); | |
868 | if (ret) | |
869 | return -EFAULT; | |
870 | } | |
673a394b | 871 | |
fbd5a26d | 872 | ret = i915_mutex_lock_interruptible(dev); |
1d7cfea1 | 873 | if (ret) |
fbd5a26d | 874 | return ret; |
1d7cfea1 | 875 | |
05394f39 | 876 | obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle)); |
c8725226 | 877 | if (&obj->base == NULL) { |
1d7cfea1 CW |
878 | ret = -ENOENT; |
879 | goto unlock; | |
fbd5a26d | 880 | } |
673a394b | 881 | |
7dcd2499 | 882 | /* Bounds check destination. */ |
05394f39 CW |
883 | if (args->offset > obj->base.size || |
884 | args->size > obj->base.size - args->offset) { | |
ce9d419d | 885 | ret = -EINVAL; |
35b62a89 | 886 | goto out; |
ce9d419d CW |
887 | } |
888 | ||
1286ff73 DV |
889 | /* prime objects have no backing filp to GEM pread/pwrite |
890 | * pages from. | |
891 | */ | |
892 | if (!obj->base.filp) { | |
893 | ret = -EINVAL; | |
894 | goto out; | |
895 | } | |
896 | ||
db53a302 CW |
897 | trace_i915_gem_object_pwrite(obj, args->offset, args->size); |
898 | ||
935aaa69 | 899 | ret = -EFAULT; |
673a394b EA |
900 | /* We can only do the GTT pwrite on untiled buffers, as otherwise |
901 | * it would end up going through the fenced access, and we'll get | |
902 | * different detiling behavior between reading and writing. | |
903 | * pread/pwrite currently are reading and writing from the CPU | |
904 | * perspective, requiring manual detiling by the client. | |
905 | */ | |
5c0480f2 | 906 | if (obj->phys_obj) { |
fbd5a26d | 907 | ret = i915_gem_phys_pwrite(dev, obj, args, file); |
5c0480f2 DV |
908 | goto out; |
909 | } | |
910 | ||
2c22569b CW |
911 | if (obj->tiling_mode == I915_TILING_NONE && |
912 | obj->base.write_domain != I915_GEM_DOMAIN_CPU && | |
913 | cpu_write_needs_clflush(obj)) { | |
fbd5a26d | 914 | ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file); |
935aaa69 DV |
915 | /* Note that the gtt paths might fail with non-page-backed user |
916 | * pointers (e.g. gtt mappings when moving data between | |
917 | * textures). Fallback to the shmem path in that case. */ | |
fbd5a26d | 918 | } |
673a394b | 919 | |
86a1ee26 | 920 | if (ret == -EFAULT || ret == -ENOSPC) |
935aaa69 | 921 | ret = i915_gem_shmem_pwrite(dev, obj, args, file); |
5c0480f2 | 922 | |
35b62a89 | 923 | out: |
05394f39 | 924 | drm_gem_object_unreference(&obj->base); |
1d7cfea1 | 925 | unlock: |
fbd5a26d | 926 | mutex_unlock(&dev->struct_mutex); |
673a394b EA |
927 | return ret; |
928 | } | |
929 | ||
b361237b | 930 | int |
33196ded | 931 | i915_gem_check_wedge(struct i915_gpu_error *error, |
b361237b CW |
932 | bool interruptible) |
933 | { | |
1f83fee0 | 934 | if (i915_reset_in_progress(error)) { |
b361237b CW |
935 | /* Non-interruptible callers can't handle -EAGAIN, hence return |
936 | * -EIO unconditionally for these. */ | |
937 | if (!interruptible) | |
938 | return -EIO; | |
939 | ||
1f83fee0 DV |
940 | /* Recovery complete, but the reset failed ... */ |
941 | if (i915_terminally_wedged(error)) | |
b361237b CW |
942 | return -EIO; |
943 | ||
944 | return -EAGAIN; | |
945 | } | |
946 | ||
947 | return 0; | |
948 | } | |
949 | ||
950 | /* | |
951 | * Compare seqno against outstanding lazy request. Emit a request if they are | |
952 | * equal. | |
953 | */ | |
954 | static int | |
955 | i915_gem_check_olr(struct intel_ring_buffer *ring, u32 seqno) | |
956 | { | |
957 | int ret; | |
958 | ||
959 | BUG_ON(!mutex_is_locked(&ring->dev->struct_mutex)); | |
960 | ||
961 | ret = 0; | |
1823521d | 962 | if (seqno == ring->outstanding_lazy_seqno) |
0025c077 | 963 | ret = i915_add_request(ring, NULL); |
b361237b CW |
964 | |
965 | return ret; | |
966 | } | |
967 | ||
094f9a54 CW |
968 | static void fake_irq(unsigned long data) |
969 | { | |
970 | wake_up_process((struct task_struct *)data); | |
971 | } | |
972 | ||
973 | static bool missed_irq(struct drm_i915_private *dev_priv, | |
974 | struct intel_ring_buffer *ring) | |
975 | { | |
976 | return test_bit(ring->id, &dev_priv->gpu_error.missed_irq_rings); | |
977 | } | |
978 | ||
b29c19b6 CW |
979 | static bool can_wait_boost(struct drm_i915_file_private *file_priv) |
980 | { | |
981 | if (file_priv == NULL) | |
982 | return true; | |
983 | ||
984 | return !atomic_xchg(&file_priv->rps_wait_boost, true); | |
985 | } | |
986 | ||
b361237b CW |
987 | /** |
988 | * __wait_seqno - wait until execution of seqno has finished | |
989 | * @ring: the ring expected to report seqno | |
990 | * @seqno: duh! | |
f69061be | 991 | * @reset_counter: reset sequence associated with the given seqno |
b361237b CW |
992 | * @interruptible: do an interruptible wait (normally yes) |
993 | * @timeout: in - how long to wait (NULL forever); out - how much time remaining | |
994 | * | |
f69061be DV |
995 | * Note: It is of utmost importance that the passed in seqno and reset_counter |
996 | * values have been read by the caller in an smp safe manner. Where read-side | |
997 | * locks are involved, it is sufficient to read the reset_counter before | |
998 | * unlocking the lock that protects the seqno. For lockless tricks, the | |
999 | * reset_counter _must_ be read before, and an appropriate smp_rmb must be | |
1000 | * inserted. | |
1001 | * | |
b361237b CW |
1002 | * Returns 0 if the seqno was found within the alloted time. Else returns the |
1003 | * errno with remaining time filled in timeout argument. | |
1004 | */ | |
1005 | static int __wait_seqno(struct intel_ring_buffer *ring, u32 seqno, | |
f69061be | 1006 | unsigned reset_counter, |
b29c19b6 CW |
1007 | bool interruptible, |
1008 | struct timespec *timeout, | |
1009 | struct drm_i915_file_private *file_priv) | |
b361237b | 1010 | { |
3d13ef2e DL |
1011 | struct drm_device *dev = ring->dev; |
1012 | drm_i915_private_t *dev_priv = dev->dev_private; | |
168c3f21 MK |
1013 | const bool irq_test_in_progress = |
1014 | ACCESS_ONCE(dev_priv->gpu_error.test_irq_rings) & intel_ring_flag(ring); | |
094f9a54 CW |
1015 | struct timespec before, now; |
1016 | DEFINE_WAIT(wait); | |
47e9766d | 1017 | unsigned long timeout_expire; |
b361237b CW |
1018 | int ret; |
1019 | ||
c67a470b PZ |
1020 | WARN(dev_priv->pc8.irqs_disabled, "IRQs disabled\n"); |
1021 | ||
b361237b CW |
1022 | if (i915_seqno_passed(ring->get_seqno(ring, true), seqno)) |
1023 | return 0; | |
1024 | ||
47e9766d | 1025 | timeout_expire = timeout ? jiffies + timespec_to_jiffies_timeout(timeout) : 0; |
b361237b | 1026 | |
3d13ef2e | 1027 | if (INTEL_INFO(dev)->gen >= 6 && can_wait_boost(file_priv)) { |
b29c19b6 CW |
1028 | gen6_rps_boost(dev_priv); |
1029 | if (file_priv) | |
1030 | mod_delayed_work(dev_priv->wq, | |
1031 | &file_priv->mm.idle_work, | |
1032 | msecs_to_jiffies(100)); | |
1033 | } | |
1034 | ||
168c3f21 | 1035 | if (!irq_test_in_progress && WARN_ON(!ring->irq_get(ring))) |
b361237b CW |
1036 | return -ENODEV; |
1037 | ||
094f9a54 CW |
1038 | /* Record current time in case interrupted by signal, or wedged */ |
1039 | trace_i915_gem_request_wait_begin(ring, seqno); | |
b361237b | 1040 | getrawmonotonic(&before); |
094f9a54 CW |
1041 | for (;;) { |
1042 | struct timer_list timer; | |
b361237b | 1043 | |
094f9a54 CW |
1044 | prepare_to_wait(&ring->irq_queue, &wait, |
1045 | interruptible ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE); | |
b361237b | 1046 | |
f69061be DV |
1047 | /* We need to check whether any gpu reset happened in between |
1048 | * the caller grabbing the seqno and now ... */ | |
094f9a54 CW |
1049 | if (reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter)) { |
1050 | /* ... but upgrade the -EAGAIN to an -EIO if the gpu | |
1051 | * is truely gone. */ | |
1052 | ret = i915_gem_check_wedge(&dev_priv->gpu_error, interruptible); | |
1053 | if (ret == 0) | |
1054 | ret = -EAGAIN; | |
1055 | break; | |
1056 | } | |
f69061be | 1057 | |
094f9a54 CW |
1058 | if (i915_seqno_passed(ring->get_seqno(ring, false), seqno)) { |
1059 | ret = 0; | |
1060 | break; | |
1061 | } | |
b361237b | 1062 | |
094f9a54 CW |
1063 | if (interruptible && signal_pending(current)) { |
1064 | ret = -ERESTARTSYS; | |
1065 | break; | |
1066 | } | |
1067 | ||
47e9766d | 1068 | if (timeout && time_after_eq(jiffies, timeout_expire)) { |
094f9a54 CW |
1069 | ret = -ETIME; |
1070 | break; | |
1071 | } | |
1072 | ||
1073 | timer.function = NULL; | |
1074 | if (timeout || missed_irq(dev_priv, ring)) { | |
47e9766d MK |
1075 | unsigned long expire; |
1076 | ||
094f9a54 | 1077 | setup_timer_on_stack(&timer, fake_irq, (unsigned long)current); |
47e9766d | 1078 | expire = missed_irq(dev_priv, ring) ? jiffies + 1 : timeout_expire; |
094f9a54 CW |
1079 | mod_timer(&timer, expire); |
1080 | } | |
1081 | ||
5035c275 | 1082 | io_schedule(); |
094f9a54 | 1083 | |
094f9a54 CW |
1084 | if (timer.function) { |
1085 | del_singleshot_timer_sync(&timer); | |
1086 | destroy_timer_on_stack(&timer); | |
1087 | } | |
1088 | } | |
b361237b | 1089 | getrawmonotonic(&now); |
094f9a54 | 1090 | trace_i915_gem_request_wait_end(ring, seqno); |
b361237b | 1091 | |
168c3f21 MK |
1092 | if (!irq_test_in_progress) |
1093 | ring->irq_put(ring); | |
094f9a54 CW |
1094 | |
1095 | finish_wait(&ring->irq_queue, &wait); | |
b361237b CW |
1096 | |
1097 | if (timeout) { | |
1098 | struct timespec sleep_time = timespec_sub(now, before); | |
1099 | *timeout = timespec_sub(*timeout, sleep_time); | |
4f42f4ef CW |
1100 | if (!timespec_valid(timeout)) /* i.e. negative time remains */ |
1101 | set_normalized_timespec(timeout, 0, 0); | |
b361237b CW |
1102 | } |
1103 | ||
094f9a54 | 1104 | return ret; |
b361237b CW |
1105 | } |
1106 | ||
1107 | /** | |
1108 | * Waits for a sequence number to be signaled, and cleans up the | |
1109 | * request and object lists appropriately for that event. | |
1110 | */ | |
1111 | int | |
1112 | i915_wait_seqno(struct intel_ring_buffer *ring, uint32_t seqno) | |
1113 | { | |
1114 | struct drm_device *dev = ring->dev; | |
1115 | struct drm_i915_private *dev_priv = dev->dev_private; | |
1116 | bool interruptible = dev_priv->mm.interruptible; | |
1117 | int ret; | |
1118 | ||
1119 | BUG_ON(!mutex_is_locked(&dev->struct_mutex)); | |
1120 | BUG_ON(seqno == 0); | |
1121 | ||
33196ded | 1122 | ret = i915_gem_check_wedge(&dev_priv->gpu_error, interruptible); |
b361237b CW |
1123 | if (ret) |
1124 | return ret; | |
1125 | ||
1126 | ret = i915_gem_check_olr(ring, seqno); | |
1127 | if (ret) | |
1128 | return ret; | |
1129 | ||
f69061be DV |
1130 | return __wait_seqno(ring, seqno, |
1131 | atomic_read(&dev_priv->gpu_error.reset_counter), | |
b29c19b6 | 1132 | interruptible, NULL, NULL); |
b361237b CW |
1133 | } |
1134 | ||
d26e3af8 CW |
1135 | static int |
1136 | i915_gem_object_wait_rendering__tail(struct drm_i915_gem_object *obj, | |
1137 | struct intel_ring_buffer *ring) | |
1138 | { | |
1139 | i915_gem_retire_requests_ring(ring); | |
1140 | ||
1141 | /* Manually manage the write flush as we may have not yet | |
1142 | * retired the buffer. | |
1143 | * | |
1144 | * Note that the last_write_seqno is always the earlier of | |
1145 | * the two (read/write) seqno, so if we haved successfully waited, | |
1146 | * we know we have passed the last write. | |
1147 | */ | |
1148 | obj->last_write_seqno = 0; | |
1149 | obj->base.write_domain &= ~I915_GEM_GPU_DOMAINS; | |
1150 | ||
1151 | return 0; | |
1152 | } | |
1153 | ||
b361237b CW |
1154 | /** |
1155 | * Ensures that all rendering to the object has completed and the object is | |
1156 | * safe to unbind from the GTT or access from the CPU. | |
1157 | */ | |
1158 | static __must_check int | |
1159 | i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj, | |
1160 | bool readonly) | |
1161 | { | |
1162 | struct intel_ring_buffer *ring = obj->ring; | |
1163 | u32 seqno; | |
1164 | int ret; | |
1165 | ||
1166 | seqno = readonly ? obj->last_write_seqno : obj->last_read_seqno; | |
1167 | if (seqno == 0) | |
1168 | return 0; | |
1169 | ||
1170 | ret = i915_wait_seqno(ring, seqno); | |
1171 | if (ret) | |
1172 | return ret; | |
1173 | ||
d26e3af8 | 1174 | return i915_gem_object_wait_rendering__tail(obj, ring); |
b361237b CW |
1175 | } |
1176 | ||
3236f57a CW |
1177 | /* A nonblocking variant of the above wait. This is a highly dangerous routine |
1178 | * as the object state may change during this call. | |
1179 | */ | |
1180 | static __must_check int | |
1181 | i915_gem_object_wait_rendering__nonblocking(struct drm_i915_gem_object *obj, | |
6e4930f6 | 1182 | struct drm_i915_file_private *file_priv, |
3236f57a CW |
1183 | bool readonly) |
1184 | { | |
1185 | struct drm_device *dev = obj->base.dev; | |
1186 | struct drm_i915_private *dev_priv = dev->dev_private; | |
1187 | struct intel_ring_buffer *ring = obj->ring; | |
f69061be | 1188 | unsigned reset_counter; |
3236f57a CW |
1189 | u32 seqno; |
1190 | int ret; | |
1191 | ||
1192 | BUG_ON(!mutex_is_locked(&dev->struct_mutex)); | |
1193 | BUG_ON(!dev_priv->mm.interruptible); | |
1194 | ||
1195 | seqno = readonly ? obj->last_write_seqno : obj->last_read_seqno; | |
1196 | if (seqno == 0) | |
1197 | return 0; | |
1198 | ||
33196ded | 1199 | ret = i915_gem_check_wedge(&dev_priv->gpu_error, true); |
3236f57a CW |
1200 | if (ret) |
1201 | return ret; | |
1202 | ||
1203 | ret = i915_gem_check_olr(ring, seqno); | |
1204 | if (ret) | |
1205 | return ret; | |
1206 | ||
f69061be | 1207 | reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter); |
3236f57a | 1208 | mutex_unlock(&dev->struct_mutex); |
6e4930f6 | 1209 | ret = __wait_seqno(ring, seqno, reset_counter, true, NULL, file_priv); |
3236f57a | 1210 | mutex_lock(&dev->struct_mutex); |
d26e3af8 CW |
1211 | if (ret) |
1212 | return ret; | |
3236f57a | 1213 | |
d26e3af8 | 1214 | return i915_gem_object_wait_rendering__tail(obj, ring); |
3236f57a CW |
1215 | } |
1216 | ||
673a394b | 1217 | /** |
2ef7eeaa EA |
1218 | * Called when user space prepares to use an object with the CPU, either |
1219 | * through the mmap ioctl's mapping or a GTT mapping. | |
673a394b EA |
1220 | */ |
1221 | int | |
1222 | i915_gem_set_domain_ioctl(struct drm_device *dev, void *data, | |
05394f39 | 1223 | struct drm_file *file) |
673a394b EA |
1224 | { |
1225 | struct drm_i915_gem_set_domain *args = data; | |
05394f39 | 1226 | struct drm_i915_gem_object *obj; |
2ef7eeaa EA |
1227 | uint32_t read_domains = args->read_domains; |
1228 | uint32_t write_domain = args->write_domain; | |
673a394b EA |
1229 | int ret; |
1230 | ||
2ef7eeaa | 1231 | /* Only handle setting domains to types used by the CPU. */ |
21d509e3 | 1232 | if (write_domain & I915_GEM_GPU_DOMAINS) |
2ef7eeaa EA |
1233 | return -EINVAL; |
1234 | ||
21d509e3 | 1235 | if (read_domains & I915_GEM_GPU_DOMAINS) |
2ef7eeaa EA |
1236 | return -EINVAL; |
1237 | ||
1238 | /* Having something in the write domain implies it's in the read | |
1239 | * domain, and only that read domain. Enforce that in the request. | |
1240 | */ | |
1241 | if (write_domain != 0 && read_domains != write_domain) | |
1242 | return -EINVAL; | |
1243 | ||
76c1dec1 | 1244 | ret = i915_mutex_lock_interruptible(dev); |
1d7cfea1 | 1245 | if (ret) |
76c1dec1 | 1246 | return ret; |
1d7cfea1 | 1247 | |
05394f39 | 1248 | obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle)); |
c8725226 | 1249 | if (&obj->base == NULL) { |
1d7cfea1 CW |
1250 | ret = -ENOENT; |
1251 | goto unlock; | |
76c1dec1 | 1252 | } |
673a394b | 1253 | |
3236f57a CW |
1254 | /* Try to flush the object off the GPU without holding the lock. |
1255 | * We will repeat the flush holding the lock in the normal manner | |
1256 | * to catch cases where we are gazumped. | |
1257 | */ | |
6e4930f6 CW |
1258 | ret = i915_gem_object_wait_rendering__nonblocking(obj, |
1259 | file->driver_priv, | |
1260 | !write_domain); | |
3236f57a CW |
1261 | if (ret) |
1262 | goto unref; | |
1263 | ||
2ef7eeaa EA |
1264 | if (read_domains & I915_GEM_DOMAIN_GTT) { |
1265 | ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0); | |
02354392 EA |
1266 | |
1267 | /* Silently promote "you're not bound, there was nothing to do" | |
1268 | * to success, since the client was just asking us to | |
1269 | * make sure everything was done. | |
1270 | */ | |
1271 | if (ret == -EINVAL) | |
1272 | ret = 0; | |
2ef7eeaa | 1273 | } else { |
e47c68e9 | 1274 | ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0); |
2ef7eeaa EA |
1275 | } |
1276 | ||
3236f57a | 1277 | unref: |
05394f39 | 1278 | drm_gem_object_unreference(&obj->base); |
1d7cfea1 | 1279 | unlock: |
673a394b EA |
1280 | mutex_unlock(&dev->struct_mutex); |
1281 | return ret; | |
1282 | } | |
1283 | ||
1284 | /** | |
1285 | * Called when user space has done writes to this buffer | |
1286 | */ | |
1287 | int | |
1288 | i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data, | |
05394f39 | 1289 | struct drm_file *file) |
673a394b EA |
1290 | { |
1291 | struct drm_i915_gem_sw_finish *args = data; | |
05394f39 | 1292 | struct drm_i915_gem_object *obj; |
673a394b EA |
1293 | int ret = 0; |
1294 | ||
76c1dec1 | 1295 | ret = i915_mutex_lock_interruptible(dev); |
1d7cfea1 | 1296 | if (ret) |
76c1dec1 | 1297 | return ret; |
1d7cfea1 | 1298 | |
05394f39 | 1299 | obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle)); |
c8725226 | 1300 | if (&obj->base == NULL) { |
1d7cfea1 CW |
1301 | ret = -ENOENT; |
1302 | goto unlock; | |
673a394b EA |
1303 | } |
1304 | ||
673a394b | 1305 | /* Pinned buffers may be scanout, so flush the cache */ |
2c22569b CW |
1306 | if (obj->pin_display) |
1307 | i915_gem_object_flush_cpu_write_domain(obj, true); | |
e47c68e9 | 1308 | |
05394f39 | 1309 | drm_gem_object_unreference(&obj->base); |
1d7cfea1 | 1310 | unlock: |
673a394b EA |
1311 | mutex_unlock(&dev->struct_mutex); |
1312 | return ret; | |
1313 | } | |
1314 | ||
1315 | /** | |
1316 | * Maps the contents of an object, returning the address it is mapped | |
1317 | * into. | |
1318 | * | |
1319 | * While the mapping holds a reference on the contents of the object, it doesn't | |
1320 | * imply a ref on the object itself. | |
1321 | */ | |
1322 | int | |
1323 | i915_gem_mmap_ioctl(struct drm_device *dev, void *data, | |
05394f39 | 1324 | struct drm_file *file) |
673a394b EA |
1325 | { |
1326 | struct drm_i915_gem_mmap *args = data; | |
1327 | struct drm_gem_object *obj; | |
673a394b EA |
1328 | unsigned long addr; |
1329 | ||
05394f39 | 1330 | obj = drm_gem_object_lookup(dev, file, args->handle); |
673a394b | 1331 | if (obj == NULL) |
bf79cb91 | 1332 | return -ENOENT; |
673a394b | 1333 | |
1286ff73 DV |
1334 | /* prime objects have no backing filp to GEM mmap |
1335 | * pages from. | |
1336 | */ | |
1337 | if (!obj->filp) { | |
1338 | drm_gem_object_unreference_unlocked(obj); | |
1339 | return -EINVAL; | |
1340 | } | |
1341 | ||
6be5ceb0 | 1342 | addr = vm_mmap(obj->filp, 0, args->size, |
673a394b EA |
1343 | PROT_READ | PROT_WRITE, MAP_SHARED, |
1344 | args->offset); | |
bc9025bd | 1345 | drm_gem_object_unreference_unlocked(obj); |
673a394b EA |
1346 | if (IS_ERR((void *)addr)) |
1347 | return addr; | |
1348 | ||
1349 | args->addr_ptr = (uint64_t) addr; | |
1350 | ||
1351 | return 0; | |
1352 | } | |
1353 | ||
de151cf6 JB |
1354 | /** |
1355 | * i915_gem_fault - fault a page into the GTT | |
1356 | * vma: VMA in question | |
1357 | * vmf: fault info | |
1358 | * | |
1359 | * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped | |
1360 | * from userspace. The fault handler takes care of binding the object to | |
1361 | * the GTT (if needed), allocating and programming a fence register (again, | |
1362 | * only if needed based on whether the old reg is still valid or the object | |
1363 | * is tiled) and inserting a new PTE into the faulting process. | |
1364 | * | |
1365 | * Note that the faulting process may involve evicting existing objects | |
1366 | * from the GTT and/or fence registers to make room. So performance may | |
1367 | * suffer if the GTT working set is large or there are few fence registers | |
1368 | * left. | |
1369 | */ | |
1370 | int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf) | |
1371 | { | |
05394f39 CW |
1372 | struct drm_i915_gem_object *obj = to_intel_bo(vma->vm_private_data); |
1373 | struct drm_device *dev = obj->base.dev; | |
7d1c4804 | 1374 | drm_i915_private_t *dev_priv = dev->dev_private; |
de151cf6 JB |
1375 | pgoff_t page_offset; |
1376 | unsigned long pfn; | |
1377 | int ret = 0; | |
0f973f27 | 1378 | bool write = !!(vmf->flags & FAULT_FLAG_WRITE); |
de151cf6 | 1379 | |
f65c9168 PZ |
1380 | intel_runtime_pm_get(dev_priv); |
1381 | ||
de151cf6 JB |
1382 | /* We don't use vmf->pgoff since that has the fake offset */ |
1383 | page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >> | |
1384 | PAGE_SHIFT; | |
1385 | ||
d9bc7e9f CW |
1386 | ret = i915_mutex_lock_interruptible(dev); |
1387 | if (ret) | |
1388 | goto out; | |
a00b10c3 | 1389 | |
db53a302 CW |
1390 | trace_i915_gem_object_fault(obj, page_offset, true, write); |
1391 | ||
6e4930f6 CW |
1392 | /* Try to flush the object off the GPU first without holding the lock. |
1393 | * Upon reacquiring the lock, we will perform our sanity checks and then | |
1394 | * repeat the flush holding the lock in the normal manner to catch cases | |
1395 | * where we are gazumped. | |
1396 | */ | |
1397 | ret = i915_gem_object_wait_rendering__nonblocking(obj, NULL, !write); | |
1398 | if (ret) | |
1399 | goto unlock; | |
1400 | ||
eb119bd6 CW |
1401 | /* Access to snoopable pages through the GTT is incoherent. */ |
1402 | if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev)) { | |
1403 | ret = -EINVAL; | |
1404 | goto unlock; | |
1405 | } | |
1406 | ||
d9bc7e9f | 1407 | /* Now bind it into the GTT if needed */ |
1ec9e26d | 1408 | ret = i915_gem_obj_ggtt_pin(obj, 0, PIN_MAPPABLE); |
c9839303 CW |
1409 | if (ret) |
1410 | goto unlock; | |
4a684a41 | 1411 | |
c9839303 CW |
1412 | ret = i915_gem_object_set_to_gtt_domain(obj, write); |
1413 | if (ret) | |
1414 | goto unpin; | |
74898d7e | 1415 | |
06d98131 | 1416 | ret = i915_gem_object_get_fence(obj); |
d9e86c0e | 1417 | if (ret) |
c9839303 | 1418 | goto unpin; |
7d1c4804 | 1419 | |
6299f992 CW |
1420 | obj->fault_mappable = true; |
1421 | ||
f343c5f6 BW |
1422 | pfn = dev_priv->gtt.mappable_base + i915_gem_obj_ggtt_offset(obj); |
1423 | pfn >>= PAGE_SHIFT; | |
1424 | pfn += page_offset; | |
de151cf6 JB |
1425 | |
1426 | /* Finally, remap it using the new GTT offset */ | |
1427 | ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn); | |
c9839303 | 1428 | unpin: |
d7f46fc4 | 1429 | i915_gem_object_ggtt_unpin(obj); |
c715089f | 1430 | unlock: |
de151cf6 | 1431 | mutex_unlock(&dev->struct_mutex); |
d9bc7e9f | 1432 | out: |
de151cf6 | 1433 | switch (ret) { |
d9bc7e9f | 1434 | case -EIO: |
a9340cca DV |
1435 | /* If this -EIO is due to a gpu hang, give the reset code a |
1436 | * chance to clean up the mess. Otherwise return the proper | |
1437 | * SIGBUS. */ | |
f65c9168 PZ |
1438 | if (i915_terminally_wedged(&dev_priv->gpu_error)) { |
1439 | ret = VM_FAULT_SIGBUS; | |
1440 | break; | |
1441 | } | |
045e769a | 1442 | case -EAGAIN: |
571c608d DV |
1443 | /* |
1444 | * EAGAIN means the gpu is hung and we'll wait for the error | |
1445 | * handler to reset everything when re-faulting in | |
1446 | * i915_mutex_lock_interruptible. | |
d9bc7e9f | 1447 | */ |
c715089f CW |
1448 | case 0: |
1449 | case -ERESTARTSYS: | |
bed636ab | 1450 | case -EINTR: |
e79e0fe3 DR |
1451 | case -EBUSY: |
1452 | /* | |
1453 | * EBUSY is ok: this just means that another thread | |
1454 | * already did the job. | |
1455 | */ | |
f65c9168 PZ |
1456 | ret = VM_FAULT_NOPAGE; |
1457 | break; | |
de151cf6 | 1458 | case -ENOMEM: |
f65c9168 PZ |
1459 | ret = VM_FAULT_OOM; |
1460 | break; | |
a7c2e1aa | 1461 | case -ENOSPC: |
45d67817 | 1462 | case -EFAULT: |
f65c9168 PZ |
1463 | ret = VM_FAULT_SIGBUS; |
1464 | break; | |
de151cf6 | 1465 | default: |
a7c2e1aa | 1466 | WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret); |
f65c9168 PZ |
1467 | ret = VM_FAULT_SIGBUS; |
1468 | break; | |
de151cf6 | 1469 | } |
f65c9168 PZ |
1470 | |
1471 | intel_runtime_pm_put(dev_priv); | |
1472 | return ret; | |
de151cf6 JB |
1473 | } |
1474 | ||
48018a57 PZ |
1475 | void i915_gem_release_all_mmaps(struct drm_i915_private *dev_priv) |
1476 | { | |
1477 | struct i915_vma *vma; | |
1478 | ||
1479 | /* | |
1480 | * Only the global gtt is relevant for gtt memory mappings, so restrict | |
1481 | * list traversal to objects bound into the global address space. Note | |
1482 | * that the active list should be empty, but better safe than sorry. | |
1483 | */ | |
1484 | WARN_ON(!list_empty(&dev_priv->gtt.base.active_list)); | |
1485 | list_for_each_entry(vma, &dev_priv->gtt.base.active_list, mm_list) | |
1486 | i915_gem_release_mmap(vma->obj); | |
1487 | list_for_each_entry(vma, &dev_priv->gtt.base.inactive_list, mm_list) | |
1488 | i915_gem_release_mmap(vma->obj); | |
1489 | } | |
1490 | ||
901782b2 CW |
1491 | /** |
1492 | * i915_gem_release_mmap - remove physical page mappings | |
1493 | * @obj: obj in question | |
1494 | * | |
af901ca1 | 1495 | * Preserve the reservation of the mmapping with the DRM core code, but |
901782b2 CW |
1496 | * relinquish ownership of the pages back to the system. |
1497 | * | |
1498 | * It is vital that we remove the page mapping if we have mapped a tiled | |
1499 | * object through the GTT and then lose the fence register due to | |
1500 | * resource pressure. Similarly if the object has been moved out of the | |
1501 | * aperture, than pages mapped into userspace must be revoked. Removing the | |
1502 | * mapping will then trigger a page fault on the next user access, allowing | |
1503 | * fixup by i915_gem_fault(). | |
1504 | */ | |
d05ca301 | 1505 | void |
05394f39 | 1506 | i915_gem_release_mmap(struct drm_i915_gem_object *obj) |
901782b2 | 1507 | { |
6299f992 CW |
1508 | if (!obj->fault_mappable) |
1509 | return; | |
901782b2 | 1510 | |
51335df9 | 1511 | drm_vma_node_unmap(&obj->base.vma_node, obj->base.dev->dev_mapping); |
6299f992 | 1512 | obj->fault_mappable = false; |
901782b2 CW |
1513 | } |
1514 | ||
0fa87796 | 1515 | uint32_t |
e28f8711 | 1516 | i915_gem_get_gtt_size(struct drm_device *dev, uint32_t size, int tiling_mode) |
92b88aeb | 1517 | { |
e28f8711 | 1518 | uint32_t gtt_size; |
92b88aeb CW |
1519 | |
1520 | if (INTEL_INFO(dev)->gen >= 4 || | |
e28f8711 CW |
1521 | tiling_mode == I915_TILING_NONE) |
1522 | return size; | |
92b88aeb CW |
1523 | |
1524 | /* Previous chips need a power-of-two fence region when tiling */ | |
1525 | if (INTEL_INFO(dev)->gen == 3) | |
e28f8711 | 1526 | gtt_size = 1024*1024; |
92b88aeb | 1527 | else |
e28f8711 | 1528 | gtt_size = 512*1024; |
92b88aeb | 1529 | |
e28f8711 CW |
1530 | while (gtt_size < size) |
1531 | gtt_size <<= 1; | |
92b88aeb | 1532 | |
e28f8711 | 1533 | return gtt_size; |
92b88aeb CW |
1534 | } |
1535 | ||
de151cf6 JB |
1536 | /** |
1537 | * i915_gem_get_gtt_alignment - return required GTT alignment for an object | |
1538 | * @obj: object to check | |
1539 | * | |
1540 | * Return the required GTT alignment for an object, taking into account | |
5e783301 | 1541 | * potential fence register mapping. |
de151cf6 | 1542 | */ |
d865110c ID |
1543 | uint32_t |
1544 | i915_gem_get_gtt_alignment(struct drm_device *dev, uint32_t size, | |
1545 | int tiling_mode, bool fenced) | |
de151cf6 | 1546 | { |
de151cf6 JB |
1547 | /* |
1548 | * Minimum alignment is 4k (GTT page size), but might be greater | |
1549 | * if a fence register is needed for the object. | |
1550 | */ | |
d865110c | 1551 | if (INTEL_INFO(dev)->gen >= 4 || (!fenced && IS_G33(dev)) || |
e28f8711 | 1552 | tiling_mode == I915_TILING_NONE) |
de151cf6 JB |
1553 | return 4096; |
1554 | ||
a00b10c3 CW |
1555 | /* |
1556 | * Previous chips need to be aligned to the size of the smallest | |
1557 | * fence register that can contain the object. | |
1558 | */ | |
e28f8711 | 1559 | return i915_gem_get_gtt_size(dev, size, tiling_mode); |
a00b10c3 CW |
1560 | } |
1561 | ||
d8cb5086 CW |
1562 | static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj) |
1563 | { | |
1564 | struct drm_i915_private *dev_priv = obj->base.dev->dev_private; | |
1565 | int ret; | |
1566 | ||
0de23977 | 1567 | if (drm_vma_node_has_offset(&obj->base.vma_node)) |
d8cb5086 CW |
1568 | return 0; |
1569 | ||
da494d7c DV |
1570 | dev_priv->mm.shrinker_no_lock_stealing = true; |
1571 | ||
d8cb5086 CW |
1572 | ret = drm_gem_create_mmap_offset(&obj->base); |
1573 | if (ret != -ENOSPC) | |
da494d7c | 1574 | goto out; |
d8cb5086 CW |
1575 | |
1576 | /* Badly fragmented mmap space? The only way we can recover | |
1577 | * space is by destroying unwanted objects. We can't randomly release | |
1578 | * mmap_offsets as userspace expects them to be persistent for the | |
1579 | * lifetime of the objects. The closest we can is to release the | |
1580 | * offsets on purgeable objects by truncating it and marking it purged, | |
1581 | * which prevents userspace from ever using that object again. | |
1582 | */ | |
1583 | i915_gem_purge(dev_priv, obj->base.size >> PAGE_SHIFT); | |
1584 | ret = drm_gem_create_mmap_offset(&obj->base); | |
1585 | if (ret != -ENOSPC) | |
da494d7c | 1586 | goto out; |
d8cb5086 CW |
1587 | |
1588 | i915_gem_shrink_all(dev_priv); | |
da494d7c DV |
1589 | ret = drm_gem_create_mmap_offset(&obj->base); |
1590 | out: | |
1591 | dev_priv->mm.shrinker_no_lock_stealing = false; | |
1592 | ||
1593 | return ret; | |
d8cb5086 CW |
1594 | } |
1595 | ||
1596 | static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj) | |
1597 | { | |
d8cb5086 CW |
1598 | drm_gem_free_mmap_offset(&obj->base); |
1599 | } | |
1600 | ||
de151cf6 | 1601 | int |
ff72145b DA |
1602 | i915_gem_mmap_gtt(struct drm_file *file, |
1603 | struct drm_device *dev, | |
1604 | uint32_t handle, | |
1605 | uint64_t *offset) | |
de151cf6 | 1606 | { |
da761a6e | 1607 | struct drm_i915_private *dev_priv = dev->dev_private; |
05394f39 | 1608 | struct drm_i915_gem_object *obj; |
de151cf6 JB |
1609 | int ret; |
1610 | ||
76c1dec1 | 1611 | ret = i915_mutex_lock_interruptible(dev); |
1d7cfea1 | 1612 | if (ret) |
76c1dec1 | 1613 | return ret; |
de151cf6 | 1614 | |
ff72145b | 1615 | obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle)); |
c8725226 | 1616 | if (&obj->base == NULL) { |
1d7cfea1 CW |
1617 | ret = -ENOENT; |
1618 | goto unlock; | |
1619 | } | |
de151cf6 | 1620 | |
5d4545ae | 1621 | if (obj->base.size > dev_priv->gtt.mappable_end) { |
da761a6e | 1622 | ret = -E2BIG; |
ff56b0bc | 1623 | goto out; |
da761a6e CW |
1624 | } |
1625 | ||
05394f39 | 1626 | if (obj->madv != I915_MADV_WILLNEED) { |
bd9b6a4e | 1627 | DRM_DEBUG("Attempting to mmap a purgeable buffer\n"); |
8c99e57d | 1628 | ret = -EFAULT; |
1d7cfea1 | 1629 | goto out; |
ab18282d CW |
1630 | } |
1631 | ||
d8cb5086 CW |
1632 | ret = i915_gem_object_create_mmap_offset(obj); |
1633 | if (ret) | |
1634 | goto out; | |
de151cf6 | 1635 | |
0de23977 | 1636 | *offset = drm_vma_node_offset_addr(&obj->base.vma_node); |
de151cf6 | 1637 | |
1d7cfea1 | 1638 | out: |
05394f39 | 1639 | drm_gem_object_unreference(&obj->base); |
1d7cfea1 | 1640 | unlock: |
de151cf6 | 1641 | mutex_unlock(&dev->struct_mutex); |
1d7cfea1 | 1642 | return ret; |
de151cf6 JB |
1643 | } |
1644 | ||
ff72145b DA |
1645 | /** |
1646 | * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing | |
1647 | * @dev: DRM device | |
1648 | * @data: GTT mapping ioctl data | |
1649 | * @file: GEM object info | |
1650 | * | |
1651 | * Simply returns the fake offset to userspace so it can mmap it. | |
1652 | * The mmap call will end up in drm_gem_mmap(), which will set things | |
1653 | * up so we can get faults in the handler above. | |
1654 | * | |
1655 | * The fault handler will take care of binding the object into the GTT | |
1656 | * (since it may have been evicted to make room for something), allocating | |
1657 | * a fence register, and mapping the appropriate aperture address into | |
1658 | * userspace. | |
1659 | */ | |
1660 | int | |
1661 | i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data, | |
1662 | struct drm_file *file) | |
1663 | { | |
1664 | struct drm_i915_gem_mmap_gtt *args = data; | |
1665 | ||
ff72145b DA |
1666 | return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset); |
1667 | } | |
1668 | ||
225067ee DV |
1669 | /* Immediately discard the backing storage */ |
1670 | static void | |
1671 | i915_gem_object_truncate(struct drm_i915_gem_object *obj) | |
e5281ccd | 1672 | { |
e5281ccd | 1673 | struct inode *inode; |
e5281ccd | 1674 | |
4d6294bf | 1675 | i915_gem_object_free_mmap_offset(obj); |
1286ff73 | 1676 | |
4d6294bf CW |
1677 | if (obj->base.filp == NULL) |
1678 | return; | |
e5281ccd | 1679 | |
225067ee DV |
1680 | /* Our goal here is to return as much of the memory as |
1681 | * is possible back to the system as we are called from OOM. | |
1682 | * To do this we must instruct the shmfs to drop all of its | |
1683 | * backing pages, *now*. | |
1684 | */ | |
496ad9aa | 1685 | inode = file_inode(obj->base.filp); |
225067ee | 1686 | shmem_truncate_range(inode, 0, (loff_t)-1); |
e5281ccd | 1687 | |
225067ee DV |
1688 | obj->madv = __I915_MADV_PURGED; |
1689 | } | |
e5281ccd | 1690 | |
225067ee DV |
1691 | static inline int |
1692 | i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj) | |
1693 | { | |
1694 | return obj->madv == I915_MADV_DONTNEED; | |
e5281ccd CW |
1695 | } |
1696 | ||
5cdf5881 | 1697 | static void |
05394f39 | 1698 | i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj) |
673a394b | 1699 | { |
90797e6d ID |
1700 | struct sg_page_iter sg_iter; |
1701 | int ret; | |
1286ff73 | 1702 | |
05394f39 | 1703 | BUG_ON(obj->madv == __I915_MADV_PURGED); |
673a394b | 1704 | |
6c085a72 CW |
1705 | ret = i915_gem_object_set_to_cpu_domain(obj, true); |
1706 | if (ret) { | |
1707 | /* In the event of a disaster, abandon all caches and | |
1708 | * hope for the best. | |
1709 | */ | |
1710 | WARN_ON(ret != -EIO); | |
2c22569b | 1711 | i915_gem_clflush_object(obj, true); |
6c085a72 CW |
1712 | obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU; |
1713 | } | |
1714 | ||
6dacfd2f | 1715 | if (i915_gem_object_needs_bit17_swizzle(obj)) |
280b713b EA |
1716 | i915_gem_object_save_bit_17_swizzle(obj); |
1717 | ||
05394f39 CW |
1718 | if (obj->madv == I915_MADV_DONTNEED) |
1719 | obj->dirty = 0; | |
3ef94daa | 1720 | |
90797e6d | 1721 | for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents, 0) { |
2db76d7c | 1722 | struct page *page = sg_page_iter_page(&sg_iter); |
9da3da66 | 1723 | |
05394f39 | 1724 | if (obj->dirty) |
9da3da66 | 1725 | set_page_dirty(page); |
3ef94daa | 1726 | |
05394f39 | 1727 | if (obj->madv == I915_MADV_WILLNEED) |
9da3da66 | 1728 | mark_page_accessed(page); |
3ef94daa | 1729 | |
9da3da66 | 1730 | page_cache_release(page); |
3ef94daa | 1731 | } |
05394f39 | 1732 | obj->dirty = 0; |
673a394b | 1733 | |
9da3da66 CW |
1734 | sg_free_table(obj->pages); |
1735 | kfree(obj->pages); | |
37e680a1 | 1736 | } |
6c085a72 | 1737 | |
dd624afd | 1738 | int |
37e680a1 CW |
1739 | i915_gem_object_put_pages(struct drm_i915_gem_object *obj) |
1740 | { | |
1741 | const struct drm_i915_gem_object_ops *ops = obj->ops; | |
1742 | ||
2f745ad3 | 1743 | if (obj->pages == NULL) |
37e680a1 CW |
1744 | return 0; |
1745 | ||
a5570178 CW |
1746 | if (obj->pages_pin_count) |
1747 | return -EBUSY; | |
1748 | ||
9843877d | 1749 | BUG_ON(i915_gem_obj_bound_any(obj)); |
3e123027 | 1750 | |
a2165e31 CW |
1751 | /* ->put_pages might need to allocate memory for the bit17 swizzle |
1752 | * array, hence protect them from being reaped by removing them from gtt | |
1753 | * lists early. */ | |
35c20a60 | 1754 | list_del(&obj->global_list); |
a2165e31 | 1755 | |
37e680a1 | 1756 | ops->put_pages(obj); |
05394f39 | 1757 | obj->pages = NULL; |
37e680a1 | 1758 | |
6c085a72 CW |
1759 | if (i915_gem_object_is_purgeable(obj)) |
1760 | i915_gem_object_truncate(obj); | |
1761 | ||
1762 | return 0; | |
1763 | } | |
1764 | ||
d9973b43 | 1765 | static unsigned long |
93927ca5 DV |
1766 | __i915_gem_shrink(struct drm_i915_private *dev_priv, long target, |
1767 | bool purgeable_only) | |
6c085a72 | 1768 | { |
57094f82 | 1769 | struct list_head still_bound_list; |
6c085a72 | 1770 | struct drm_i915_gem_object *obj, *next; |
d9973b43 | 1771 | unsigned long count = 0; |
6c085a72 CW |
1772 | |
1773 | list_for_each_entry_safe(obj, next, | |
1774 | &dev_priv->mm.unbound_list, | |
35c20a60 | 1775 | global_list) { |
93927ca5 | 1776 | if ((i915_gem_object_is_purgeable(obj) || !purgeable_only) && |
37e680a1 | 1777 | i915_gem_object_put_pages(obj) == 0) { |
6c085a72 CW |
1778 | count += obj->base.size >> PAGE_SHIFT; |
1779 | if (count >= target) | |
1780 | return count; | |
1781 | } | |
1782 | } | |
1783 | ||
57094f82 CW |
1784 | /* |
1785 | * As we may completely rewrite the bound list whilst unbinding | |
1786 | * (due to retiring requests) we have to strictly process only | |
1787 | * one element of the list at the time, and recheck the list | |
1788 | * on every iteration. | |
1789 | */ | |
1790 | INIT_LIST_HEAD(&still_bound_list); | |
1791 | while (count < target && !list_empty(&dev_priv->mm.bound_list)) { | |
07fe0b12 | 1792 | struct i915_vma *vma, *v; |
80dcfdbd | 1793 | |
57094f82 CW |
1794 | obj = list_first_entry(&dev_priv->mm.bound_list, |
1795 | typeof(*obj), global_list); | |
1796 | list_move_tail(&obj->global_list, &still_bound_list); | |
1797 | ||
80dcfdbd BW |
1798 | if (!i915_gem_object_is_purgeable(obj) && purgeable_only) |
1799 | continue; | |
1800 | ||
57094f82 CW |
1801 | /* |
1802 | * Hold a reference whilst we unbind this object, as we may | |
1803 | * end up waiting for and retiring requests. This might | |
1804 | * release the final reference (held by the active list) | |
1805 | * and result in the object being freed from under us. | |
1806 | * in this object being freed. | |
1807 | * | |
1808 | * Note 1: Shrinking the bound list is special since only active | |
1809 | * (and hence bound objects) can contain such limbo objects, so | |
1810 | * we don't need special tricks for shrinking the unbound list. | |
1811 | * The only other place where we have to be careful with active | |
1812 | * objects suddenly disappearing due to retiring requests is the | |
1813 | * eviction code. | |
1814 | * | |
1815 | * Note 2: Even though the bound list doesn't hold a reference | |
1816 | * to the object we can safely grab one here: The final object | |
1817 | * unreferencing and the bound_list are both protected by the | |
1818 | * dev->struct_mutex and so we won't ever be able to observe an | |
1819 | * object on the bound_list with a reference count equals 0. | |
1820 | */ | |
1821 | drm_gem_object_reference(&obj->base); | |
1822 | ||
07fe0b12 BW |
1823 | list_for_each_entry_safe(vma, v, &obj->vma_list, vma_link) |
1824 | if (i915_vma_unbind(vma)) | |
1825 | break; | |
80dcfdbd | 1826 | |
57094f82 | 1827 | if (i915_gem_object_put_pages(obj) == 0) |
6c085a72 | 1828 | count += obj->base.size >> PAGE_SHIFT; |
57094f82 CW |
1829 | |
1830 | drm_gem_object_unreference(&obj->base); | |
6c085a72 | 1831 | } |
57094f82 | 1832 | list_splice(&still_bound_list, &dev_priv->mm.bound_list); |
6c085a72 CW |
1833 | |
1834 | return count; | |
1835 | } | |
1836 | ||
d9973b43 | 1837 | static unsigned long |
93927ca5 DV |
1838 | i915_gem_purge(struct drm_i915_private *dev_priv, long target) |
1839 | { | |
1840 | return __i915_gem_shrink(dev_priv, target, true); | |
1841 | } | |
1842 | ||
d9973b43 | 1843 | static unsigned long |
6c085a72 CW |
1844 | i915_gem_shrink_all(struct drm_i915_private *dev_priv) |
1845 | { | |
1846 | struct drm_i915_gem_object *obj, *next; | |
7dc19d5a | 1847 | long freed = 0; |
6c085a72 CW |
1848 | |
1849 | i915_gem_evict_everything(dev_priv->dev); | |
1850 | ||
35c20a60 | 1851 | list_for_each_entry_safe(obj, next, &dev_priv->mm.unbound_list, |
7dc19d5a | 1852 | global_list) { |
d9973b43 | 1853 | if (i915_gem_object_put_pages(obj) == 0) |
7dc19d5a | 1854 | freed += obj->base.size >> PAGE_SHIFT; |
7dc19d5a DC |
1855 | } |
1856 | return freed; | |
225067ee DV |
1857 | } |
1858 | ||
37e680a1 | 1859 | static int |
6c085a72 | 1860 | i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj) |
e5281ccd | 1861 | { |
6c085a72 | 1862 | struct drm_i915_private *dev_priv = obj->base.dev->dev_private; |
e5281ccd CW |
1863 | int page_count, i; |
1864 | struct address_space *mapping; | |
9da3da66 CW |
1865 | struct sg_table *st; |
1866 | struct scatterlist *sg; | |
90797e6d | 1867 | struct sg_page_iter sg_iter; |
e5281ccd | 1868 | struct page *page; |
90797e6d | 1869 | unsigned long last_pfn = 0; /* suppress gcc warning */ |
6c085a72 | 1870 | gfp_t gfp; |
e5281ccd | 1871 | |
6c085a72 CW |
1872 | /* Assert that the object is not currently in any GPU domain. As it |
1873 | * wasn't in the GTT, there shouldn't be any way it could have been in | |
1874 | * a GPU cache | |
1875 | */ | |
1876 | BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS); | |
1877 | BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS); | |
1878 | ||
9da3da66 CW |
1879 | st = kmalloc(sizeof(*st), GFP_KERNEL); |
1880 | if (st == NULL) | |
1881 | return -ENOMEM; | |
1882 | ||
05394f39 | 1883 | page_count = obj->base.size / PAGE_SIZE; |
9da3da66 | 1884 | if (sg_alloc_table(st, page_count, GFP_KERNEL)) { |
9da3da66 | 1885 | kfree(st); |
e5281ccd | 1886 | return -ENOMEM; |
9da3da66 | 1887 | } |
e5281ccd | 1888 | |
9da3da66 CW |
1889 | /* Get the list of pages out of our struct file. They'll be pinned |
1890 | * at this point until we release them. | |
1891 | * | |
1892 | * Fail silently without starting the shrinker | |
1893 | */ | |
496ad9aa | 1894 | mapping = file_inode(obj->base.filp)->i_mapping; |
6c085a72 | 1895 | gfp = mapping_gfp_mask(mapping); |
caf49191 | 1896 | gfp |= __GFP_NORETRY | __GFP_NOWARN | __GFP_NO_KSWAPD; |
6c085a72 | 1897 | gfp &= ~(__GFP_IO | __GFP_WAIT); |
90797e6d ID |
1898 | sg = st->sgl; |
1899 | st->nents = 0; | |
1900 | for (i = 0; i < page_count; i++) { | |
6c085a72 CW |
1901 | page = shmem_read_mapping_page_gfp(mapping, i, gfp); |
1902 | if (IS_ERR(page)) { | |
1903 | i915_gem_purge(dev_priv, page_count); | |
1904 | page = shmem_read_mapping_page_gfp(mapping, i, gfp); | |
1905 | } | |
1906 | if (IS_ERR(page)) { | |
1907 | /* We've tried hard to allocate the memory by reaping | |
1908 | * our own buffer, now let the real VM do its job and | |
1909 | * go down in flames if truly OOM. | |
1910 | */ | |
caf49191 | 1911 | gfp &= ~(__GFP_NORETRY | __GFP_NOWARN | __GFP_NO_KSWAPD); |
6c085a72 CW |
1912 | gfp |= __GFP_IO | __GFP_WAIT; |
1913 | ||
1914 | i915_gem_shrink_all(dev_priv); | |
1915 | page = shmem_read_mapping_page_gfp(mapping, i, gfp); | |
1916 | if (IS_ERR(page)) | |
1917 | goto err_pages; | |
1918 | ||
caf49191 | 1919 | gfp |= __GFP_NORETRY | __GFP_NOWARN | __GFP_NO_KSWAPD; |
6c085a72 CW |
1920 | gfp &= ~(__GFP_IO | __GFP_WAIT); |
1921 | } | |
426729dc KRW |
1922 | #ifdef CONFIG_SWIOTLB |
1923 | if (swiotlb_nr_tbl()) { | |
1924 | st->nents++; | |
1925 | sg_set_page(sg, page, PAGE_SIZE, 0); | |
1926 | sg = sg_next(sg); | |
1927 | continue; | |
1928 | } | |
1929 | #endif | |
90797e6d ID |
1930 | if (!i || page_to_pfn(page) != last_pfn + 1) { |
1931 | if (i) | |
1932 | sg = sg_next(sg); | |
1933 | st->nents++; | |
1934 | sg_set_page(sg, page, PAGE_SIZE, 0); | |
1935 | } else { | |
1936 | sg->length += PAGE_SIZE; | |
1937 | } | |
1938 | last_pfn = page_to_pfn(page); | |
3bbbe706 DV |
1939 | |
1940 | /* Check that the i965g/gm workaround works. */ | |
1941 | WARN_ON((gfp & __GFP_DMA32) && (last_pfn >= 0x00100000UL)); | |
e5281ccd | 1942 | } |
426729dc KRW |
1943 | #ifdef CONFIG_SWIOTLB |
1944 | if (!swiotlb_nr_tbl()) | |
1945 | #endif | |
1946 | sg_mark_end(sg); | |
74ce6b6c CW |
1947 | obj->pages = st; |
1948 | ||
6dacfd2f | 1949 | if (i915_gem_object_needs_bit17_swizzle(obj)) |
e5281ccd CW |
1950 | i915_gem_object_do_bit_17_swizzle(obj); |
1951 | ||
1952 | return 0; | |
1953 | ||
1954 | err_pages: | |
90797e6d ID |
1955 | sg_mark_end(sg); |
1956 | for_each_sg_page(st->sgl, &sg_iter, st->nents, 0) | |
2db76d7c | 1957 | page_cache_release(sg_page_iter_page(&sg_iter)); |
9da3da66 CW |
1958 | sg_free_table(st); |
1959 | kfree(st); | |
e5281ccd | 1960 | return PTR_ERR(page); |
673a394b EA |
1961 | } |
1962 | ||
37e680a1 CW |
1963 | /* Ensure that the associated pages are gathered from the backing storage |
1964 | * and pinned into our object. i915_gem_object_get_pages() may be called | |
1965 | * multiple times before they are released by a single call to | |
1966 | * i915_gem_object_put_pages() - once the pages are no longer referenced | |
1967 | * either as a result of memory pressure (reaping pages under the shrinker) | |
1968 | * or as the object is itself released. | |
1969 | */ | |
1970 | int | |
1971 | i915_gem_object_get_pages(struct drm_i915_gem_object *obj) | |
1972 | { | |
1973 | struct drm_i915_private *dev_priv = obj->base.dev->dev_private; | |
1974 | const struct drm_i915_gem_object_ops *ops = obj->ops; | |
1975 | int ret; | |
1976 | ||
2f745ad3 | 1977 | if (obj->pages) |
37e680a1 CW |
1978 | return 0; |
1979 | ||
43e28f09 | 1980 | if (obj->madv != I915_MADV_WILLNEED) { |
bd9b6a4e | 1981 | DRM_DEBUG("Attempting to obtain a purgeable object\n"); |
8c99e57d | 1982 | return -EFAULT; |
43e28f09 CW |
1983 | } |
1984 | ||
a5570178 CW |
1985 | BUG_ON(obj->pages_pin_count); |
1986 | ||
37e680a1 CW |
1987 | ret = ops->get_pages(obj); |
1988 | if (ret) | |
1989 | return ret; | |
1990 | ||
35c20a60 | 1991 | list_add_tail(&obj->global_list, &dev_priv->mm.unbound_list); |
37e680a1 | 1992 | return 0; |
673a394b EA |
1993 | } |
1994 | ||
e2d05a8b | 1995 | static void |
05394f39 | 1996 | i915_gem_object_move_to_active(struct drm_i915_gem_object *obj, |
9d773091 | 1997 | struct intel_ring_buffer *ring) |
673a394b | 1998 | { |
05394f39 | 1999 | struct drm_device *dev = obj->base.dev; |
69dc4987 | 2000 | struct drm_i915_private *dev_priv = dev->dev_private; |
9d773091 | 2001 | u32 seqno = intel_ring_get_seqno(ring); |
617dbe27 | 2002 | |
852835f3 | 2003 | BUG_ON(ring == NULL); |
02978ff5 CW |
2004 | if (obj->ring != ring && obj->last_write_seqno) { |
2005 | /* Keep the seqno relative to the current ring */ | |
2006 | obj->last_write_seqno = seqno; | |
2007 | } | |
05394f39 | 2008 | obj->ring = ring; |
673a394b EA |
2009 | |
2010 | /* Add a reference if we're newly entering the active list. */ | |
05394f39 CW |
2011 | if (!obj->active) { |
2012 | drm_gem_object_reference(&obj->base); | |
2013 | obj->active = 1; | |
673a394b | 2014 | } |
e35a41de | 2015 | |
05394f39 | 2016 | list_move_tail(&obj->ring_list, &ring->active_list); |
caea7476 | 2017 | |
0201f1ec | 2018 | obj->last_read_seqno = seqno; |
caea7476 | 2019 | |
7dd49065 | 2020 | if (obj->fenced_gpu_access) { |
caea7476 | 2021 | obj->last_fenced_seqno = seqno; |
caea7476 | 2022 | |
7dd49065 CW |
2023 | /* Bump MRU to take account of the delayed flush */ |
2024 | if (obj->fence_reg != I915_FENCE_REG_NONE) { | |
2025 | struct drm_i915_fence_reg *reg; | |
2026 | ||
2027 | reg = &dev_priv->fence_regs[obj->fence_reg]; | |
2028 | list_move_tail(®->lru_list, | |
2029 | &dev_priv->mm.fence_list); | |
2030 | } | |
caea7476 CW |
2031 | } |
2032 | } | |
2033 | ||
e2d05a8b BW |
2034 | void i915_vma_move_to_active(struct i915_vma *vma, |
2035 | struct intel_ring_buffer *ring) | |
2036 | { | |
2037 | list_move_tail(&vma->mm_list, &vma->vm->active_list); | |
2038 | return i915_gem_object_move_to_active(vma->obj, ring); | |
2039 | } | |
2040 | ||
caea7476 | 2041 | static void |
caea7476 | 2042 | i915_gem_object_move_to_inactive(struct drm_i915_gem_object *obj) |
ce44b0ea | 2043 | { |
ca191b13 | 2044 | struct drm_i915_private *dev_priv = obj->base.dev->dev_private; |
feb822cf BW |
2045 | struct i915_address_space *vm; |
2046 | struct i915_vma *vma; | |
ce44b0ea | 2047 | |
65ce3027 | 2048 | BUG_ON(obj->base.write_domain & ~I915_GEM_GPU_DOMAINS); |
05394f39 | 2049 | BUG_ON(!obj->active); |
caea7476 | 2050 | |
feb822cf BW |
2051 | list_for_each_entry(vm, &dev_priv->vm_list, global_link) { |
2052 | vma = i915_gem_obj_to_vma(obj, vm); | |
2053 | if (vma && !list_empty(&vma->mm_list)) | |
2054 | list_move_tail(&vma->mm_list, &vm->inactive_list); | |
2055 | } | |
caea7476 | 2056 | |
65ce3027 | 2057 | list_del_init(&obj->ring_list); |
caea7476 CW |
2058 | obj->ring = NULL; |
2059 | ||
65ce3027 CW |
2060 | obj->last_read_seqno = 0; |
2061 | obj->last_write_seqno = 0; | |
2062 | obj->base.write_domain = 0; | |
2063 | ||
2064 | obj->last_fenced_seqno = 0; | |
caea7476 | 2065 | obj->fenced_gpu_access = false; |
caea7476 CW |
2066 | |
2067 | obj->active = 0; | |
2068 | drm_gem_object_unreference(&obj->base); | |
2069 | ||
2070 | WARN_ON(i915_verify_lists(dev)); | |
ce44b0ea | 2071 | } |
673a394b | 2072 | |
9d773091 | 2073 | static int |
fca26bb4 | 2074 | i915_gem_init_seqno(struct drm_device *dev, u32 seqno) |
53d227f2 | 2075 | { |
9d773091 CW |
2076 | struct drm_i915_private *dev_priv = dev->dev_private; |
2077 | struct intel_ring_buffer *ring; | |
2078 | int ret, i, j; | |
53d227f2 | 2079 | |
107f27a5 | 2080 | /* Carefully retire all requests without writing to the rings */ |
9d773091 | 2081 | for_each_ring(ring, dev_priv, i) { |
107f27a5 CW |
2082 | ret = intel_ring_idle(ring); |
2083 | if (ret) | |
2084 | return ret; | |
9d773091 | 2085 | } |
9d773091 | 2086 | i915_gem_retire_requests(dev); |
107f27a5 CW |
2087 | |
2088 | /* Finally reset hw state */ | |
9d773091 | 2089 | for_each_ring(ring, dev_priv, i) { |
fca26bb4 | 2090 | intel_ring_init_seqno(ring, seqno); |
498d2ac1 | 2091 | |
9d773091 CW |
2092 | for (j = 0; j < ARRAY_SIZE(ring->sync_seqno); j++) |
2093 | ring->sync_seqno[j] = 0; | |
2094 | } | |
53d227f2 | 2095 | |
9d773091 | 2096 | return 0; |
53d227f2 DV |
2097 | } |
2098 | ||
fca26bb4 MK |
2099 | int i915_gem_set_seqno(struct drm_device *dev, u32 seqno) |
2100 | { | |
2101 | struct drm_i915_private *dev_priv = dev->dev_private; | |
2102 | int ret; | |
2103 | ||
2104 | if (seqno == 0) | |
2105 | return -EINVAL; | |
2106 | ||
2107 | /* HWS page needs to be set less than what we | |
2108 | * will inject to ring | |
2109 | */ | |
2110 | ret = i915_gem_init_seqno(dev, seqno - 1); | |
2111 | if (ret) | |
2112 | return ret; | |
2113 | ||
2114 | /* Carefully set the last_seqno value so that wrap | |
2115 | * detection still works | |
2116 | */ | |
2117 | dev_priv->next_seqno = seqno; | |
2118 | dev_priv->last_seqno = seqno - 1; | |
2119 | if (dev_priv->last_seqno == 0) | |
2120 | dev_priv->last_seqno--; | |
2121 | ||
2122 | return 0; | |
2123 | } | |
2124 | ||
9d773091 CW |
2125 | int |
2126 | i915_gem_get_seqno(struct drm_device *dev, u32 *seqno) | |
53d227f2 | 2127 | { |
9d773091 CW |
2128 | struct drm_i915_private *dev_priv = dev->dev_private; |
2129 | ||
2130 | /* reserve 0 for non-seqno */ | |
2131 | if (dev_priv->next_seqno == 0) { | |
fca26bb4 | 2132 | int ret = i915_gem_init_seqno(dev, 0); |
9d773091 CW |
2133 | if (ret) |
2134 | return ret; | |
53d227f2 | 2135 | |
9d773091 CW |
2136 | dev_priv->next_seqno = 1; |
2137 | } | |
53d227f2 | 2138 | |
f72b3435 | 2139 | *seqno = dev_priv->last_seqno = dev_priv->next_seqno++; |
9d773091 | 2140 | return 0; |
53d227f2 DV |
2141 | } |
2142 | ||
0025c077 MK |
2143 | int __i915_add_request(struct intel_ring_buffer *ring, |
2144 | struct drm_file *file, | |
7d736f4f | 2145 | struct drm_i915_gem_object *obj, |
0025c077 | 2146 | u32 *out_seqno) |
673a394b | 2147 | { |
db53a302 | 2148 | drm_i915_private_t *dev_priv = ring->dev->dev_private; |
acb868d3 | 2149 | struct drm_i915_gem_request *request; |
7d736f4f | 2150 | u32 request_ring_position, request_start; |
673a394b | 2151 | int was_empty; |
3cce469c CW |
2152 | int ret; |
2153 | ||
7d736f4f | 2154 | request_start = intel_ring_get_tail(ring); |
cc889e0f DV |
2155 | /* |
2156 | * Emit any outstanding flushes - execbuf can fail to emit the flush | |
2157 | * after having emitted the batchbuffer command. Hence we need to fix | |
2158 | * things up similar to emitting the lazy request. The difference here | |
2159 | * is that the flush _must_ happen before the next request, no matter | |
2160 | * what. | |
2161 | */ | |
a7b9761d CW |
2162 | ret = intel_ring_flush_all_caches(ring); |
2163 | if (ret) | |
2164 | return ret; | |
cc889e0f | 2165 | |
3c0e234c CW |
2166 | request = ring->preallocated_lazy_request; |
2167 | if (WARN_ON(request == NULL)) | |
acb868d3 | 2168 | return -ENOMEM; |
cc889e0f | 2169 | |
a71d8d94 CW |
2170 | /* Record the position of the start of the request so that |
2171 | * should we detect the updated seqno part-way through the | |
2172 | * GPU processing the request, we never over-estimate the | |
2173 | * position of the head. | |
2174 | */ | |
2175 | request_ring_position = intel_ring_get_tail(ring); | |
2176 | ||
9d773091 | 2177 | ret = ring->add_request(ring); |
3c0e234c | 2178 | if (ret) |
3bb73aba | 2179 | return ret; |
673a394b | 2180 | |
9d773091 | 2181 | request->seqno = intel_ring_get_seqno(ring); |
852835f3 | 2182 | request->ring = ring; |
7d736f4f | 2183 | request->head = request_start; |
a71d8d94 | 2184 | request->tail = request_ring_position; |
7d736f4f MK |
2185 | |
2186 | /* Whilst this request exists, batch_obj will be on the | |
2187 | * active_list, and so will hold the active reference. Only when this | |
2188 | * request is retired will the the batch_obj be moved onto the | |
2189 | * inactive_list and lose its active reference. Hence we do not need | |
2190 | * to explicitly hold another reference here. | |
2191 | */ | |
9a7e0c2a | 2192 | request->batch_obj = obj; |
0e50e96b | 2193 | |
9a7e0c2a CW |
2194 | /* Hold a reference to the current context so that we can inspect |
2195 | * it later in case a hangcheck error event fires. | |
2196 | */ | |
2197 | request->ctx = ring->last_context; | |
0e50e96b MK |
2198 | if (request->ctx) |
2199 | i915_gem_context_reference(request->ctx); | |
2200 | ||
673a394b | 2201 | request->emitted_jiffies = jiffies; |
852835f3 ZN |
2202 | was_empty = list_empty(&ring->request_list); |
2203 | list_add_tail(&request->list, &ring->request_list); | |
3bb73aba | 2204 | request->file_priv = NULL; |
852835f3 | 2205 | |
db53a302 CW |
2206 | if (file) { |
2207 | struct drm_i915_file_private *file_priv = file->driver_priv; | |
2208 | ||
1c25595f | 2209 | spin_lock(&file_priv->mm.lock); |
f787a5f5 | 2210 | request->file_priv = file_priv; |
b962442e | 2211 | list_add_tail(&request->client_list, |
f787a5f5 | 2212 | &file_priv->mm.request_list); |
1c25595f | 2213 | spin_unlock(&file_priv->mm.lock); |
b962442e | 2214 | } |
673a394b | 2215 | |
9d773091 | 2216 | trace_i915_gem_request_add(ring, request->seqno); |
1823521d | 2217 | ring->outstanding_lazy_seqno = 0; |
3c0e234c | 2218 | ring->preallocated_lazy_request = NULL; |
db53a302 | 2219 | |
db1b76ca | 2220 | if (!dev_priv->ums.mm_suspended) { |
10cd45b6 MK |
2221 | i915_queue_hangcheck(ring->dev); |
2222 | ||
f047e395 | 2223 | if (was_empty) { |
b29c19b6 | 2224 | cancel_delayed_work_sync(&dev_priv->mm.idle_work); |
b3b079db | 2225 | queue_delayed_work(dev_priv->wq, |
bcb45086 CW |
2226 | &dev_priv->mm.retire_work, |
2227 | round_jiffies_up_relative(HZ)); | |
f047e395 CW |
2228 | intel_mark_busy(dev_priv->dev); |
2229 | } | |
f65d9421 | 2230 | } |
cc889e0f | 2231 | |
acb868d3 | 2232 | if (out_seqno) |
9d773091 | 2233 | *out_seqno = request->seqno; |
3cce469c | 2234 | return 0; |
673a394b EA |
2235 | } |
2236 | ||
f787a5f5 CW |
2237 | static inline void |
2238 | i915_gem_request_remove_from_client(struct drm_i915_gem_request *request) | |
673a394b | 2239 | { |
1c25595f | 2240 | struct drm_i915_file_private *file_priv = request->file_priv; |
673a394b | 2241 | |
1c25595f CW |
2242 | if (!file_priv) |
2243 | return; | |
1c5d22f7 | 2244 | |
1c25595f | 2245 | spin_lock(&file_priv->mm.lock); |
b29c19b6 CW |
2246 | list_del(&request->client_list); |
2247 | request->file_priv = NULL; | |
1c25595f | 2248 | spin_unlock(&file_priv->mm.lock); |
673a394b | 2249 | } |
673a394b | 2250 | |
939fd762 | 2251 | static bool i915_context_is_banned(struct drm_i915_private *dev_priv, |
44e2c070 | 2252 | const struct i915_hw_context *ctx) |
be62acb4 | 2253 | { |
44e2c070 | 2254 | unsigned long elapsed; |
be62acb4 | 2255 | |
44e2c070 MK |
2256 | elapsed = get_seconds() - ctx->hang_stats.guilty_ts; |
2257 | ||
2258 | if (ctx->hang_stats.banned) | |
be62acb4 MK |
2259 | return true; |
2260 | ||
2261 | if (elapsed <= DRM_I915_CTX_BAN_PERIOD) { | |
3fac8978 MK |
2262 | if (dev_priv->gpu_error.stop_rings == 0 && |
2263 | i915_gem_context_is_default(ctx)) { | |
2264 | DRM_ERROR("gpu hanging too fast, banning!\n"); | |
2265 | } else { | |
2266 | DRM_DEBUG("context hanging too fast, banning!\n"); | |
2267 | } | |
2268 | ||
be62acb4 MK |
2269 | return true; |
2270 | } | |
2271 | ||
2272 | return false; | |
2273 | } | |
2274 | ||
939fd762 MK |
2275 | static void i915_set_reset_status(struct drm_i915_private *dev_priv, |
2276 | struct i915_hw_context *ctx, | |
b6b0fac0 | 2277 | const bool guilty) |
aa60c664 | 2278 | { |
44e2c070 MK |
2279 | struct i915_ctx_hang_stats *hs; |
2280 | ||
2281 | if (WARN_ON(!ctx)) | |
2282 | return; | |
aa60c664 | 2283 | |
44e2c070 MK |
2284 | hs = &ctx->hang_stats; |
2285 | ||
2286 | if (guilty) { | |
939fd762 | 2287 | hs->banned = i915_context_is_banned(dev_priv, ctx); |
44e2c070 MK |
2288 | hs->batch_active++; |
2289 | hs->guilty_ts = get_seconds(); | |
2290 | } else { | |
2291 | hs->batch_pending++; | |
aa60c664 MK |
2292 | } |
2293 | } | |
2294 | ||
0e50e96b MK |
2295 | static void i915_gem_free_request(struct drm_i915_gem_request *request) |
2296 | { | |
2297 | list_del(&request->list); | |
2298 | i915_gem_request_remove_from_client(request); | |
2299 | ||
2300 | if (request->ctx) | |
2301 | i915_gem_context_unreference(request->ctx); | |
2302 | ||
2303 | kfree(request); | |
2304 | } | |
2305 | ||
b6b0fac0 MK |
2306 | static struct drm_i915_gem_request * |
2307 | i915_gem_find_first_non_complete(struct intel_ring_buffer *ring) | |
9375e446 | 2308 | { |
4db080f9 | 2309 | struct drm_i915_gem_request *request; |
b6b0fac0 | 2310 | const u32 completed_seqno = ring->get_seqno(ring, false); |
4db080f9 CW |
2311 | |
2312 | list_for_each_entry(request, &ring->request_list, list) { | |
2313 | if (i915_seqno_passed(completed_seqno, request->seqno)) | |
2314 | continue; | |
aa60c664 | 2315 | |
b6b0fac0 | 2316 | return request; |
4db080f9 | 2317 | } |
b6b0fac0 MK |
2318 | |
2319 | return NULL; | |
2320 | } | |
2321 | ||
2322 | static void i915_gem_reset_ring_status(struct drm_i915_private *dev_priv, | |
2323 | struct intel_ring_buffer *ring) | |
2324 | { | |
2325 | struct drm_i915_gem_request *request; | |
2326 | bool ring_hung; | |
2327 | ||
2328 | request = i915_gem_find_first_non_complete(ring); | |
2329 | ||
2330 | if (request == NULL) | |
2331 | return; | |
2332 | ||
2333 | ring_hung = ring->hangcheck.score >= HANGCHECK_SCORE_RING_HUNG; | |
2334 | ||
939fd762 | 2335 | i915_set_reset_status(dev_priv, request->ctx, ring_hung); |
b6b0fac0 MK |
2336 | |
2337 | list_for_each_entry_continue(request, &ring->request_list, list) | |
939fd762 | 2338 | i915_set_reset_status(dev_priv, request->ctx, false); |
4db080f9 | 2339 | } |
aa60c664 | 2340 | |
4db080f9 CW |
2341 | static void i915_gem_reset_ring_cleanup(struct drm_i915_private *dev_priv, |
2342 | struct intel_ring_buffer *ring) | |
2343 | { | |
dfaae392 | 2344 | while (!list_empty(&ring->active_list)) { |
05394f39 | 2345 | struct drm_i915_gem_object *obj; |
9375e446 | 2346 | |
05394f39 CW |
2347 | obj = list_first_entry(&ring->active_list, |
2348 | struct drm_i915_gem_object, | |
2349 | ring_list); | |
9375e446 | 2350 | |
05394f39 | 2351 | i915_gem_object_move_to_inactive(obj); |
673a394b | 2352 | } |
1d62beea BW |
2353 | |
2354 | /* | |
2355 | * We must free the requests after all the corresponding objects have | |
2356 | * been moved off active lists. Which is the same order as the normal | |
2357 | * retire_requests function does. This is important if object hold | |
2358 | * implicit references on things like e.g. ppgtt address spaces through | |
2359 | * the request. | |
2360 | */ | |
2361 | while (!list_empty(&ring->request_list)) { | |
2362 | struct drm_i915_gem_request *request; | |
2363 | ||
2364 | request = list_first_entry(&ring->request_list, | |
2365 | struct drm_i915_gem_request, | |
2366 | list); | |
2367 | ||
2368 | i915_gem_free_request(request); | |
2369 | } | |
673a394b EA |
2370 | } |
2371 | ||
19b2dbde | 2372 | void i915_gem_restore_fences(struct drm_device *dev) |
312817a3 CW |
2373 | { |
2374 | struct drm_i915_private *dev_priv = dev->dev_private; | |
2375 | int i; | |
2376 | ||
4b9de737 | 2377 | for (i = 0; i < dev_priv->num_fence_regs; i++) { |
312817a3 | 2378 | struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i]; |
7d2cb39c | 2379 | |
94a335db DV |
2380 | /* |
2381 | * Commit delayed tiling changes if we have an object still | |
2382 | * attached to the fence, otherwise just clear the fence. | |
2383 | */ | |
2384 | if (reg->obj) { | |
2385 | i915_gem_object_update_fence(reg->obj, reg, | |
2386 | reg->obj->tiling_mode); | |
2387 | } else { | |
2388 | i915_gem_write_fence(dev, i, NULL); | |
2389 | } | |
312817a3 CW |
2390 | } |
2391 | } | |
2392 | ||
069efc1d | 2393 | void i915_gem_reset(struct drm_device *dev) |
673a394b | 2394 | { |
77f01230 | 2395 | struct drm_i915_private *dev_priv = dev->dev_private; |
b4519513 | 2396 | struct intel_ring_buffer *ring; |
1ec14ad3 | 2397 | int i; |
673a394b | 2398 | |
4db080f9 CW |
2399 | /* |
2400 | * Before we free the objects from the requests, we need to inspect | |
2401 | * them for finding the guilty party. As the requests only borrow | |
2402 | * their reference to the objects, the inspection must be done first. | |
2403 | */ | |
2404 | for_each_ring(ring, dev_priv, i) | |
2405 | i915_gem_reset_ring_status(dev_priv, ring); | |
2406 | ||
b4519513 | 2407 | for_each_ring(ring, dev_priv, i) |
4db080f9 | 2408 | i915_gem_reset_ring_cleanup(dev_priv, ring); |
dfaae392 | 2409 | |
3d57e5bd BW |
2410 | i915_gem_cleanup_ringbuffer(dev); |
2411 | ||
acce9ffa BW |
2412 | i915_gem_context_reset(dev); |
2413 | ||
19b2dbde | 2414 | i915_gem_restore_fences(dev); |
673a394b EA |
2415 | } |
2416 | ||
2417 | /** | |
2418 | * This function clears the request list as sequence numbers are passed. | |
2419 | */ | |
a71d8d94 | 2420 | void |
db53a302 | 2421 | i915_gem_retire_requests_ring(struct intel_ring_buffer *ring) |
673a394b | 2422 | { |
673a394b EA |
2423 | uint32_t seqno; |
2424 | ||
db53a302 | 2425 | if (list_empty(&ring->request_list)) |
6c0594a3 KW |
2426 | return; |
2427 | ||
db53a302 | 2428 | WARN_ON(i915_verify_lists(ring->dev)); |
673a394b | 2429 | |
b2eadbc8 | 2430 | seqno = ring->get_seqno(ring, true); |
1ec14ad3 | 2431 | |
e9103038 CW |
2432 | /* Move any buffers on the active list that are no longer referenced |
2433 | * by the ringbuffer to the flushing/inactive lists as appropriate, | |
2434 | * before we free the context associated with the requests. | |
2435 | */ | |
2436 | while (!list_empty(&ring->active_list)) { | |
2437 | struct drm_i915_gem_object *obj; | |
2438 | ||
2439 | obj = list_first_entry(&ring->active_list, | |
2440 | struct drm_i915_gem_object, | |
2441 | ring_list); | |
2442 | ||
2443 | if (!i915_seqno_passed(seqno, obj->last_read_seqno)) | |
2444 | break; | |
2445 | ||
2446 | i915_gem_object_move_to_inactive(obj); | |
2447 | } | |
2448 | ||
2449 | ||
852835f3 | 2450 | while (!list_empty(&ring->request_list)) { |
673a394b | 2451 | struct drm_i915_gem_request *request; |
673a394b | 2452 | |
852835f3 | 2453 | request = list_first_entry(&ring->request_list, |
673a394b EA |
2454 | struct drm_i915_gem_request, |
2455 | list); | |
673a394b | 2456 | |
dfaae392 | 2457 | if (!i915_seqno_passed(seqno, request->seqno)) |
b84d5f0c CW |
2458 | break; |
2459 | ||
db53a302 | 2460 | trace_i915_gem_request_retire(ring, request->seqno); |
a71d8d94 CW |
2461 | /* We know the GPU must have read the request to have |
2462 | * sent us the seqno + interrupt, so use the position | |
2463 | * of tail of the request to update the last known position | |
2464 | * of the GPU head. | |
2465 | */ | |
2466 | ring->last_retired_head = request->tail; | |
b84d5f0c | 2467 | |
0e50e96b | 2468 | i915_gem_free_request(request); |
b84d5f0c | 2469 | } |
673a394b | 2470 | |
db53a302 CW |
2471 | if (unlikely(ring->trace_irq_seqno && |
2472 | i915_seqno_passed(seqno, ring->trace_irq_seqno))) { | |
1ec14ad3 | 2473 | ring->irq_put(ring); |
db53a302 | 2474 | ring->trace_irq_seqno = 0; |
9d34e5db | 2475 | } |
23bc5982 | 2476 | |
db53a302 | 2477 | WARN_ON(i915_verify_lists(ring->dev)); |
673a394b EA |
2478 | } |
2479 | ||
b29c19b6 | 2480 | bool |
b09a1fec CW |
2481 | i915_gem_retire_requests(struct drm_device *dev) |
2482 | { | |
2483 | drm_i915_private_t *dev_priv = dev->dev_private; | |
b4519513 | 2484 | struct intel_ring_buffer *ring; |
b29c19b6 | 2485 | bool idle = true; |
1ec14ad3 | 2486 | int i; |
b09a1fec | 2487 | |
b29c19b6 | 2488 | for_each_ring(ring, dev_priv, i) { |
b4519513 | 2489 | i915_gem_retire_requests_ring(ring); |
b29c19b6 CW |
2490 | idle &= list_empty(&ring->request_list); |
2491 | } | |
2492 | ||
2493 | if (idle) | |
2494 | mod_delayed_work(dev_priv->wq, | |
2495 | &dev_priv->mm.idle_work, | |
2496 | msecs_to_jiffies(100)); | |
2497 | ||
2498 | return idle; | |
b09a1fec CW |
2499 | } |
2500 | ||
75ef9da2 | 2501 | static void |
673a394b EA |
2502 | i915_gem_retire_work_handler(struct work_struct *work) |
2503 | { | |
b29c19b6 CW |
2504 | struct drm_i915_private *dev_priv = |
2505 | container_of(work, typeof(*dev_priv), mm.retire_work.work); | |
2506 | struct drm_device *dev = dev_priv->dev; | |
0a58705b | 2507 | bool idle; |
673a394b | 2508 | |
891b48cf | 2509 | /* Come back later if the device is busy... */ |
b29c19b6 CW |
2510 | idle = false; |
2511 | if (mutex_trylock(&dev->struct_mutex)) { | |
2512 | idle = i915_gem_retire_requests(dev); | |
2513 | mutex_unlock(&dev->struct_mutex); | |
673a394b | 2514 | } |
b29c19b6 | 2515 | if (!idle) |
bcb45086 CW |
2516 | queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, |
2517 | round_jiffies_up_relative(HZ)); | |
b29c19b6 | 2518 | } |
0a58705b | 2519 | |
b29c19b6 CW |
2520 | static void |
2521 | i915_gem_idle_work_handler(struct work_struct *work) | |
2522 | { | |
2523 | struct drm_i915_private *dev_priv = | |
2524 | container_of(work, typeof(*dev_priv), mm.idle_work.work); | |
2525 | ||
2526 | intel_mark_idle(dev_priv->dev); | |
673a394b EA |
2527 | } |
2528 | ||
30dfebf3 DV |
2529 | /** |
2530 | * Ensures that an object will eventually get non-busy by flushing any required | |
2531 | * write domains, emitting any outstanding lazy request and retiring and | |
2532 | * completed requests. | |
2533 | */ | |
2534 | static int | |
2535 | i915_gem_object_flush_active(struct drm_i915_gem_object *obj) | |
2536 | { | |
2537 | int ret; | |
2538 | ||
2539 | if (obj->active) { | |
0201f1ec | 2540 | ret = i915_gem_check_olr(obj->ring, obj->last_read_seqno); |
30dfebf3 DV |
2541 | if (ret) |
2542 | return ret; | |
2543 | ||
30dfebf3 DV |
2544 | i915_gem_retire_requests_ring(obj->ring); |
2545 | } | |
2546 | ||
2547 | return 0; | |
2548 | } | |
2549 | ||
23ba4fd0 BW |
2550 | /** |
2551 | * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT | |
2552 | * @DRM_IOCTL_ARGS: standard ioctl arguments | |
2553 | * | |
2554 | * Returns 0 if successful, else an error is returned with the remaining time in | |
2555 | * the timeout parameter. | |
2556 | * -ETIME: object is still busy after timeout | |
2557 | * -ERESTARTSYS: signal interrupted the wait | |
2558 | * -ENONENT: object doesn't exist | |
2559 | * Also possible, but rare: | |
2560 | * -EAGAIN: GPU wedged | |
2561 | * -ENOMEM: damn | |
2562 | * -ENODEV: Internal IRQ fail | |
2563 | * -E?: The add request failed | |
2564 | * | |
2565 | * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any | |
2566 | * non-zero timeout parameter the wait ioctl will wait for the given number of | |
2567 | * nanoseconds on an object becoming unbusy. Since the wait itself does so | |
2568 | * without holding struct_mutex the object may become re-busied before this | |
2569 | * function completes. A similar but shorter * race condition exists in the busy | |
2570 | * ioctl | |
2571 | */ | |
2572 | int | |
2573 | i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file) | |
2574 | { | |
f69061be | 2575 | drm_i915_private_t *dev_priv = dev->dev_private; |
23ba4fd0 BW |
2576 | struct drm_i915_gem_wait *args = data; |
2577 | struct drm_i915_gem_object *obj; | |
2578 | struct intel_ring_buffer *ring = NULL; | |
eac1f14f | 2579 | struct timespec timeout_stack, *timeout = NULL; |
f69061be | 2580 | unsigned reset_counter; |
23ba4fd0 BW |
2581 | u32 seqno = 0; |
2582 | int ret = 0; | |
2583 | ||
eac1f14f BW |
2584 | if (args->timeout_ns >= 0) { |
2585 | timeout_stack = ns_to_timespec(args->timeout_ns); | |
2586 | timeout = &timeout_stack; | |
2587 | } | |
23ba4fd0 BW |
2588 | |
2589 | ret = i915_mutex_lock_interruptible(dev); | |
2590 | if (ret) | |
2591 | return ret; | |
2592 | ||
2593 | obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->bo_handle)); | |
2594 | if (&obj->base == NULL) { | |
2595 | mutex_unlock(&dev->struct_mutex); | |
2596 | return -ENOENT; | |
2597 | } | |
2598 | ||
30dfebf3 DV |
2599 | /* Need to make sure the object gets inactive eventually. */ |
2600 | ret = i915_gem_object_flush_active(obj); | |
23ba4fd0 BW |
2601 | if (ret) |
2602 | goto out; | |
2603 | ||
2604 | if (obj->active) { | |
0201f1ec | 2605 | seqno = obj->last_read_seqno; |
23ba4fd0 BW |
2606 | ring = obj->ring; |
2607 | } | |
2608 | ||
2609 | if (seqno == 0) | |
2610 | goto out; | |
2611 | ||
23ba4fd0 BW |
2612 | /* Do this after OLR check to make sure we make forward progress polling |
2613 | * on this IOCTL with a 0 timeout (like busy ioctl) | |
2614 | */ | |
2615 | if (!args->timeout_ns) { | |
2616 | ret = -ETIME; | |
2617 | goto out; | |
2618 | } | |
2619 | ||
2620 | drm_gem_object_unreference(&obj->base); | |
f69061be | 2621 | reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter); |
23ba4fd0 BW |
2622 | mutex_unlock(&dev->struct_mutex); |
2623 | ||
b29c19b6 | 2624 | ret = __wait_seqno(ring, seqno, reset_counter, true, timeout, file->driver_priv); |
4f42f4ef | 2625 | if (timeout) |
eac1f14f | 2626 | args->timeout_ns = timespec_to_ns(timeout); |
23ba4fd0 BW |
2627 | return ret; |
2628 | ||
2629 | out: | |
2630 | drm_gem_object_unreference(&obj->base); | |
2631 | mutex_unlock(&dev->struct_mutex); | |
2632 | return ret; | |
2633 | } | |
2634 | ||
5816d648 BW |
2635 | /** |
2636 | * i915_gem_object_sync - sync an object to a ring. | |
2637 | * | |
2638 | * @obj: object which may be in use on another ring. | |
2639 | * @to: ring we wish to use the object on. May be NULL. | |
2640 | * | |
2641 | * This code is meant to abstract object synchronization with the GPU. | |
2642 | * Calling with NULL implies synchronizing the object with the CPU | |
2643 | * rather than a particular GPU ring. | |
2644 | * | |
2645 | * Returns 0 if successful, else propagates up the lower layer error. | |
2646 | */ | |
2911a35b BW |
2647 | int |
2648 | i915_gem_object_sync(struct drm_i915_gem_object *obj, | |
2649 | struct intel_ring_buffer *to) | |
2650 | { | |
2651 | struct intel_ring_buffer *from = obj->ring; | |
2652 | u32 seqno; | |
2653 | int ret, idx; | |
2654 | ||
2655 | if (from == NULL || to == from) | |
2656 | return 0; | |
2657 | ||
5816d648 | 2658 | if (to == NULL || !i915_semaphore_is_enabled(obj->base.dev)) |
0201f1ec | 2659 | return i915_gem_object_wait_rendering(obj, false); |
2911a35b BW |
2660 | |
2661 | idx = intel_ring_sync_index(from, to); | |
2662 | ||
0201f1ec | 2663 | seqno = obj->last_read_seqno; |
2911a35b BW |
2664 | if (seqno <= from->sync_seqno[idx]) |
2665 | return 0; | |
2666 | ||
b4aca010 BW |
2667 | ret = i915_gem_check_olr(obj->ring, seqno); |
2668 | if (ret) | |
2669 | return ret; | |
2911a35b | 2670 | |
b52b89da | 2671 | trace_i915_gem_ring_sync_to(from, to, seqno); |
1500f7ea | 2672 | ret = to->sync_to(to, from, seqno); |
e3a5a225 | 2673 | if (!ret) |
7b01e260 MK |
2674 | /* We use last_read_seqno because sync_to() |
2675 | * might have just caused seqno wrap under | |
2676 | * the radar. | |
2677 | */ | |
2678 | from->sync_seqno[idx] = obj->last_read_seqno; | |
2911a35b | 2679 | |
e3a5a225 | 2680 | return ret; |
2911a35b BW |
2681 | } |
2682 | ||
b5ffc9bc CW |
2683 | static void i915_gem_object_finish_gtt(struct drm_i915_gem_object *obj) |
2684 | { | |
2685 | u32 old_write_domain, old_read_domains; | |
2686 | ||
b5ffc9bc CW |
2687 | /* Force a pagefault for domain tracking on next user access */ |
2688 | i915_gem_release_mmap(obj); | |
2689 | ||
b97c3d9c KP |
2690 | if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0) |
2691 | return; | |
2692 | ||
97c809fd CW |
2693 | /* Wait for any direct GTT access to complete */ |
2694 | mb(); | |
2695 | ||
b5ffc9bc CW |
2696 | old_read_domains = obj->base.read_domains; |
2697 | old_write_domain = obj->base.write_domain; | |
2698 | ||
2699 | obj->base.read_domains &= ~I915_GEM_DOMAIN_GTT; | |
2700 | obj->base.write_domain &= ~I915_GEM_DOMAIN_GTT; | |
2701 | ||
2702 | trace_i915_gem_object_change_domain(obj, | |
2703 | old_read_domains, | |
2704 | old_write_domain); | |
2705 | } | |
2706 | ||
07fe0b12 | 2707 | int i915_vma_unbind(struct i915_vma *vma) |
673a394b | 2708 | { |
07fe0b12 | 2709 | struct drm_i915_gem_object *obj = vma->obj; |
7bddb01f | 2710 | drm_i915_private_t *dev_priv = obj->base.dev->dev_private; |
43e28f09 | 2711 | int ret; |
673a394b | 2712 | |
07fe0b12 | 2713 | if (list_empty(&vma->vma_link)) |
673a394b EA |
2714 | return 0; |
2715 | ||
0ff501cb DV |
2716 | if (!drm_mm_node_allocated(&vma->node)) { |
2717 | i915_gem_vma_destroy(vma); | |
0ff501cb DV |
2718 | return 0; |
2719 | } | |
433544bd | 2720 | |
d7f46fc4 | 2721 | if (vma->pin_count) |
31d8d651 | 2722 | return -EBUSY; |
673a394b | 2723 | |
c4670ad0 CW |
2724 | BUG_ON(obj->pages == NULL); |
2725 | ||
a8198eea | 2726 | ret = i915_gem_object_finish_gpu(obj); |
1488fc08 | 2727 | if (ret) |
a8198eea CW |
2728 | return ret; |
2729 | /* Continue on if we fail due to EIO, the GPU is hung so we | |
2730 | * should be safe and we need to cleanup or else we might | |
2731 | * cause memory corruption through use-after-free. | |
2732 | */ | |
2733 | ||
b5ffc9bc | 2734 | i915_gem_object_finish_gtt(obj); |
5323fd04 | 2735 | |
96b47b65 | 2736 | /* release the fence reg _after_ flushing */ |
d9e86c0e | 2737 | ret = i915_gem_object_put_fence(obj); |
1488fc08 | 2738 | if (ret) |
d9e86c0e | 2739 | return ret; |
96b47b65 | 2740 | |
07fe0b12 | 2741 | trace_i915_vma_unbind(vma); |
db53a302 | 2742 | |
6f65e29a BW |
2743 | vma->unbind_vma(vma); |
2744 | ||
74163907 | 2745 | i915_gem_gtt_finish_object(obj); |
7bddb01f | 2746 | |
ca191b13 | 2747 | list_del(&vma->mm_list); |
75e9e915 | 2748 | /* Avoid an unnecessary call to unbind on rebind. */ |
5cacaac7 BW |
2749 | if (i915_is_ggtt(vma->vm)) |
2750 | obj->map_and_fenceable = true; | |
673a394b | 2751 | |
2f633156 BW |
2752 | drm_mm_remove_node(&vma->node); |
2753 | i915_gem_vma_destroy(vma); | |
2754 | ||
2755 | /* Since the unbound list is global, only move to that list if | |
b93dab6e | 2756 | * no more VMAs exist. */ |
2f633156 BW |
2757 | if (list_empty(&obj->vma_list)) |
2758 | list_move_tail(&obj->global_list, &dev_priv->mm.unbound_list); | |
673a394b | 2759 | |
70903c3b CW |
2760 | /* And finally now the object is completely decoupled from this vma, |
2761 | * we can drop its hold on the backing storage and allow it to be | |
2762 | * reaped by the shrinker. | |
2763 | */ | |
2764 | i915_gem_object_unpin_pages(obj); | |
2765 | ||
88241785 | 2766 | return 0; |
54cf91dc CW |
2767 | } |
2768 | ||
b2da9fe5 | 2769 | int i915_gpu_idle(struct drm_device *dev) |
4df2faf4 DV |
2770 | { |
2771 | drm_i915_private_t *dev_priv = dev->dev_private; | |
b4519513 | 2772 | struct intel_ring_buffer *ring; |
1ec14ad3 | 2773 | int ret, i; |
4df2faf4 | 2774 | |
4df2faf4 | 2775 | /* Flush everything onto the inactive list. */ |
b4519513 | 2776 | for_each_ring(ring, dev_priv, i) { |
41bde553 | 2777 | ret = i915_switch_context(ring, NULL, ring->default_context); |
b6c7488d BW |
2778 | if (ret) |
2779 | return ret; | |
2780 | ||
3e960501 | 2781 | ret = intel_ring_idle(ring); |
1ec14ad3 CW |
2782 | if (ret) |
2783 | return ret; | |
2784 | } | |
4df2faf4 | 2785 | |
8a1a49f9 | 2786 | return 0; |
4df2faf4 DV |
2787 | } |
2788 | ||
9ce079e4 CW |
2789 | static void i965_write_fence_reg(struct drm_device *dev, int reg, |
2790 | struct drm_i915_gem_object *obj) | |
de151cf6 | 2791 | { |
de151cf6 | 2792 | drm_i915_private_t *dev_priv = dev->dev_private; |
56c844e5 ID |
2793 | int fence_reg; |
2794 | int fence_pitch_shift; | |
de151cf6 | 2795 | |
56c844e5 ID |
2796 | if (INTEL_INFO(dev)->gen >= 6) { |
2797 | fence_reg = FENCE_REG_SANDYBRIDGE_0; | |
2798 | fence_pitch_shift = SANDYBRIDGE_FENCE_PITCH_SHIFT; | |
2799 | } else { | |
2800 | fence_reg = FENCE_REG_965_0; | |
2801 | fence_pitch_shift = I965_FENCE_PITCH_SHIFT; | |
2802 | } | |
2803 | ||
d18b9619 CW |
2804 | fence_reg += reg * 8; |
2805 | ||
2806 | /* To w/a incoherency with non-atomic 64-bit register updates, | |
2807 | * we split the 64-bit update into two 32-bit writes. In order | |
2808 | * for a partial fence not to be evaluated between writes, we | |
2809 | * precede the update with write to turn off the fence register, | |
2810 | * and only enable the fence as the last step. | |
2811 | * | |
2812 | * For extra levels of paranoia, we make sure each step lands | |
2813 | * before applying the next step. | |
2814 | */ | |
2815 | I915_WRITE(fence_reg, 0); | |
2816 | POSTING_READ(fence_reg); | |
2817 | ||
9ce079e4 | 2818 | if (obj) { |
f343c5f6 | 2819 | u32 size = i915_gem_obj_ggtt_size(obj); |
d18b9619 | 2820 | uint64_t val; |
de151cf6 | 2821 | |
f343c5f6 | 2822 | val = (uint64_t)((i915_gem_obj_ggtt_offset(obj) + size - 4096) & |
9ce079e4 | 2823 | 0xfffff000) << 32; |
f343c5f6 | 2824 | val |= i915_gem_obj_ggtt_offset(obj) & 0xfffff000; |
56c844e5 | 2825 | val |= (uint64_t)((obj->stride / 128) - 1) << fence_pitch_shift; |
9ce079e4 CW |
2826 | if (obj->tiling_mode == I915_TILING_Y) |
2827 | val |= 1 << I965_FENCE_TILING_Y_SHIFT; | |
2828 | val |= I965_FENCE_REG_VALID; | |
c6642782 | 2829 | |
d18b9619 CW |
2830 | I915_WRITE(fence_reg + 4, val >> 32); |
2831 | POSTING_READ(fence_reg + 4); | |
2832 | ||
2833 | I915_WRITE(fence_reg + 0, val); | |
2834 | POSTING_READ(fence_reg); | |
2835 | } else { | |
2836 | I915_WRITE(fence_reg + 4, 0); | |
2837 | POSTING_READ(fence_reg + 4); | |
2838 | } | |
de151cf6 JB |
2839 | } |
2840 | ||
9ce079e4 CW |
2841 | static void i915_write_fence_reg(struct drm_device *dev, int reg, |
2842 | struct drm_i915_gem_object *obj) | |
de151cf6 | 2843 | { |
de151cf6 | 2844 | drm_i915_private_t *dev_priv = dev->dev_private; |
9ce079e4 | 2845 | u32 val; |
de151cf6 | 2846 | |
9ce079e4 | 2847 | if (obj) { |
f343c5f6 | 2848 | u32 size = i915_gem_obj_ggtt_size(obj); |
9ce079e4 CW |
2849 | int pitch_val; |
2850 | int tile_width; | |
c6642782 | 2851 | |
f343c5f6 | 2852 | WARN((i915_gem_obj_ggtt_offset(obj) & ~I915_FENCE_START_MASK) || |
9ce079e4 | 2853 | (size & -size) != size || |
f343c5f6 BW |
2854 | (i915_gem_obj_ggtt_offset(obj) & (size - 1)), |
2855 | "object 0x%08lx [fenceable? %d] not 1M or pot-size (0x%08x) aligned\n", | |
2856 | i915_gem_obj_ggtt_offset(obj), obj->map_and_fenceable, size); | |
c6642782 | 2857 | |
9ce079e4 CW |
2858 | if (obj->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev)) |
2859 | tile_width = 128; | |
2860 | else | |
2861 | tile_width = 512; | |
2862 | ||
2863 | /* Note: pitch better be a power of two tile widths */ | |
2864 | pitch_val = obj->stride / tile_width; | |
2865 | pitch_val = ffs(pitch_val) - 1; | |
2866 | ||
f343c5f6 | 2867 | val = i915_gem_obj_ggtt_offset(obj); |
9ce079e4 CW |
2868 | if (obj->tiling_mode == I915_TILING_Y) |
2869 | val |= 1 << I830_FENCE_TILING_Y_SHIFT; | |
2870 | val |= I915_FENCE_SIZE_BITS(size); | |
2871 | val |= pitch_val << I830_FENCE_PITCH_SHIFT; | |
2872 | val |= I830_FENCE_REG_VALID; | |
2873 | } else | |
2874 | val = 0; | |
2875 | ||
2876 | if (reg < 8) | |
2877 | reg = FENCE_REG_830_0 + reg * 4; | |
2878 | else | |
2879 | reg = FENCE_REG_945_8 + (reg - 8) * 4; | |
2880 | ||
2881 | I915_WRITE(reg, val); | |
2882 | POSTING_READ(reg); | |
de151cf6 JB |
2883 | } |
2884 | ||
9ce079e4 CW |
2885 | static void i830_write_fence_reg(struct drm_device *dev, int reg, |
2886 | struct drm_i915_gem_object *obj) | |
de151cf6 | 2887 | { |
de151cf6 | 2888 | drm_i915_private_t *dev_priv = dev->dev_private; |
de151cf6 | 2889 | uint32_t val; |
de151cf6 | 2890 | |
9ce079e4 | 2891 | if (obj) { |
f343c5f6 | 2892 | u32 size = i915_gem_obj_ggtt_size(obj); |
9ce079e4 | 2893 | uint32_t pitch_val; |
de151cf6 | 2894 | |
f343c5f6 | 2895 | WARN((i915_gem_obj_ggtt_offset(obj) & ~I830_FENCE_START_MASK) || |
9ce079e4 | 2896 | (size & -size) != size || |
f343c5f6 BW |
2897 | (i915_gem_obj_ggtt_offset(obj) & (size - 1)), |
2898 | "object 0x%08lx not 512K or pot-size 0x%08x aligned\n", | |
2899 | i915_gem_obj_ggtt_offset(obj), size); | |
e76a16de | 2900 | |
9ce079e4 CW |
2901 | pitch_val = obj->stride / 128; |
2902 | pitch_val = ffs(pitch_val) - 1; | |
de151cf6 | 2903 | |
f343c5f6 | 2904 | val = i915_gem_obj_ggtt_offset(obj); |
9ce079e4 CW |
2905 | if (obj->tiling_mode == I915_TILING_Y) |
2906 | val |= 1 << I830_FENCE_TILING_Y_SHIFT; | |
2907 | val |= I830_FENCE_SIZE_BITS(size); | |
2908 | val |= pitch_val << I830_FENCE_PITCH_SHIFT; | |
2909 | val |= I830_FENCE_REG_VALID; | |
2910 | } else | |
2911 | val = 0; | |
c6642782 | 2912 | |
9ce079e4 CW |
2913 | I915_WRITE(FENCE_REG_830_0 + reg * 4, val); |
2914 | POSTING_READ(FENCE_REG_830_0 + reg * 4); | |
2915 | } | |
2916 | ||
d0a57789 CW |
2917 | inline static bool i915_gem_object_needs_mb(struct drm_i915_gem_object *obj) |
2918 | { | |
2919 | return obj && obj->base.read_domains & I915_GEM_DOMAIN_GTT; | |
2920 | } | |
2921 | ||
9ce079e4 CW |
2922 | static void i915_gem_write_fence(struct drm_device *dev, int reg, |
2923 | struct drm_i915_gem_object *obj) | |
2924 | { | |
d0a57789 CW |
2925 | struct drm_i915_private *dev_priv = dev->dev_private; |
2926 | ||
2927 | /* Ensure that all CPU reads are completed before installing a fence | |
2928 | * and all writes before removing the fence. | |
2929 | */ | |
2930 | if (i915_gem_object_needs_mb(dev_priv->fence_regs[reg].obj)) | |
2931 | mb(); | |
2932 | ||
94a335db DV |
2933 | WARN(obj && (!obj->stride || !obj->tiling_mode), |
2934 | "bogus fence setup with stride: 0x%x, tiling mode: %i\n", | |
2935 | obj->stride, obj->tiling_mode); | |
2936 | ||
9ce079e4 | 2937 | switch (INTEL_INFO(dev)->gen) { |
5ab31333 | 2938 | case 8: |
9ce079e4 | 2939 | case 7: |
56c844e5 | 2940 | case 6: |
9ce079e4 CW |
2941 | case 5: |
2942 | case 4: i965_write_fence_reg(dev, reg, obj); break; | |
2943 | case 3: i915_write_fence_reg(dev, reg, obj); break; | |
2944 | case 2: i830_write_fence_reg(dev, reg, obj); break; | |
7dbf9d6e | 2945 | default: BUG(); |
9ce079e4 | 2946 | } |
d0a57789 CW |
2947 | |
2948 | /* And similarly be paranoid that no direct access to this region | |
2949 | * is reordered to before the fence is installed. | |
2950 | */ | |
2951 | if (i915_gem_object_needs_mb(obj)) | |
2952 | mb(); | |
de151cf6 JB |
2953 | } |
2954 | ||
61050808 CW |
2955 | static inline int fence_number(struct drm_i915_private *dev_priv, |
2956 | struct drm_i915_fence_reg *fence) | |
2957 | { | |
2958 | return fence - dev_priv->fence_regs; | |
2959 | } | |
2960 | ||
2961 | static void i915_gem_object_update_fence(struct drm_i915_gem_object *obj, | |
2962 | struct drm_i915_fence_reg *fence, | |
2963 | bool enable) | |
2964 | { | |
2dc8aae0 | 2965 | struct drm_i915_private *dev_priv = obj->base.dev->dev_private; |
46a0b638 CW |
2966 | int reg = fence_number(dev_priv, fence); |
2967 | ||
2968 | i915_gem_write_fence(obj->base.dev, reg, enable ? obj : NULL); | |
61050808 CW |
2969 | |
2970 | if (enable) { | |
46a0b638 | 2971 | obj->fence_reg = reg; |
61050808 CW |
2972 | fence->obj = obj; |
2973 | list_move_tail(&fence->lru_list, &dev_priv->mm.fence_list); | |
2974 | } else { | |
2975 | obj->fence_reg = I915_FENCE_REG_NONE; | |
2976 | fence->obj = NULL; | |
2977 | list_del_init(&fence->lru_list); | |
2978 | } | |
94a335db | 2979 | obj->fence_dirty = false; |
61050808 CW |
2980 | } |
2981 | ||
d9e86c0e | 2982 | static int |
d0a57789 | 2983 | i915_gem_object_wait_fence(struct drm_i915_gem_object *obj) |
d9e86c0e | 2984 | { |
1c293ea3 | 2985 | if (obj->last_fenced_seqno) { |
86d5bc37 | 2986 | int ret = i915_wait_seqno(obj->ring, obj->last_fenced_seqno); |
18991845 CW |
2987 | if (ret) |
2988 | return ret; | |
d9e86c0e CW |
2989 | |
2990 | obj->last_fenced_seqno = 0; | |
d9e86c0e CW |
2991 | } |
2992 | ||
86d5bc37 | 2993 | obj->fenced_gpu_access = false; |
d9e86c0e CW |
2994 | return 0; |
2995 | } | |
2996 | ||
2997 | int | |
2998 | i915_gem_object_put_fence(struct drm_i915_gem_object *obj) | |
2999 | { | |
61050808 | 3000 | struct drm_i915_private *dev_priv = obj->base.dev->dev_private; |
f9c513e9 | 3001 | struct drm_i915_fence_reg *fence; |
d9e86c0e CW |
3002 | int ret; |
3003 | ||
d0a57789 | 3004 | ret = i915_gem_object_wait_fence(obj); |
d9e86c0e CW |
3005 | if (ret) |
3006 | return ret; | |
3007 | ||
61050808 CW |
3008 | if (obj->fence_reg == I915_FENCE_REG_NONE) |
3009 | return 0; | |
d9e86c0e | 3010 | |
f9c513e9 CW |
3011 | fence = &dev_priv->fence_regs[obj->fence_reg]; |
3012 | ||
61050808 | 3013 | i915_gem_object_fence_lost(obj); |
f9c513e9 | 3014 | i915_gem_object_update_fence(obj, fence, false); |
d9e86c0e CW |
3015 | |
3016 | return 0; | |
3017 | } | |
3018 | ||
3019 | static struct drm_i915_fence_reg * | |
a360bb1a | 3020 | i915_find_fence_reg(struct drm_device *dev) |
ae3db24a | 3021 | { |
ae3db24a | 3022 | struct drm_i915_private *dev_priv = dev->dev_private; |
8fe301ad | 3023 | struct drm_i915_fence_reg *reg, *avail; |
d9e86c0e | 3024 | int i; |
ae3db24a DV |
3025 | |
3026 | /* First try to find a free reg */ | |
d9e86c0e | 3027 | avail = NULL; |
ae3db24a DV |
3028 | for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) { |
3029 | reg = &dev_priv->fence_regs[i]; | |
3030 | if (!reg->obj) | |
d9e86c0e | 3031 | return reg; |
ae3db24a | 3032 | |
1690e1eb | 3033 | if (!reg->pin_count) |
d9e86c0e | 3034 | avail = reg; |
ae3db24a DV |
3035 | } |
3036 | ||
d9e86c0e | 3037 | if (avail == NULL) |
5dce5b93 | 3038 | goto deadlock; |
ae3db24a DV |
3039 | |
3040 | /* None available, try to steal one or wait for a user to finish */ | |
d9e86c0e | 3041 | list_for_each_entry(reg, &dev_priv->mm.fence_list, lru_list) { |
1690e1eb | 3042 | if (reg->pin_count) |
ae3db24a DV |
3043 | continue; |
3044 | ||
8fe301ad | 3045 | return reg; |
ae3db24a DV |
3046 | } |
3047 | ||
5dce5b93 CW |
3048 | deadlock: |
3049 | /* Wait for completion of pending flips which consume fences */ | |
3050 | if (intel_has_pending_fb_unpin(dev)) | |
3051 | return ERR_PTR(-EAGAIN); | |
3052 | ||
3053 | return ERR_PTR(-EDEADLK); | |
ae3db24a DV |
3054 | } |
3055 | ||
de151cf6 | 3056 | /** |
9a5a53b3 | 3057 | * i915_gem_object_get_fence - set up fencing for an object |
de151cf6 JB |
3058 | * @obj: object to map through a fence reg |
3059 | * | |
3060 | * When mapping objects through the GTT, userspace wants to be able to write | |
3061 | * to them without having to worry about swizzling if the object is tiled. | |
de151cf6 JB |
3062 | * This function walks the fence regs looking for a free one for @obj, |
3063 | * stealing one if it can't find any. | |
3064 | * | |
3065 | * It then sets up the reg based on the object's properties: address, pitch | |
3066 | * and tiling format. | |
9a5a53b3 CW |
3067 | * |
3068 | * For an untiled surface, this removes any existing fence. | |
de151cf6 | 3069 | */ |
8c4b8c3f | 3070 | int |
06d98131 | 3071 | i915_gem_object_get_fence(struct drm_i915_gem_object *obj) |
de151cf6 | 3072 | { |
05394f39 | 3073 | struct drm_device *dev = obj->base.dev; |
79e53945 | 3074 | struct drm_i915_private *dev_priv = dev->dev_private; |
14415745 | 3075 | bool enable = obj->tiling_mode != I915_TILING_NONE; |
d9e86c0e | 3076 | struct drm_i915_fence_reg *reg; |
ae3db24a | 3077 | int ret; |
de151cf6 | 3078 | |
14415745 CW |
3079 | /* Have we updated the tiling parameters upon the object and so |
3080 | * will need to serialise the write to the associated fence register? | |
3081 | */ | |
5d82e3e6 | 3082 | if (obj->fence_dirty) { |
d0a57789 | 3083 | ret = i915_gem_object_wait_fence(obj); |
14415745 CW |
3084 | if (ret) |
3085 | return ret; | |
3086 | } | |
9a5a53b3 | 3087 | |
d9e86c0e | 3088 | /* Just update our place in the LRU if our fence is getting reused. */ |
05394f39 CW |
3089 | if (obj->fence_reg != I915_FENCE_REG_NONE) { |
3090 | reg = &dev_priv->fence_regs[obj->fence_reg]; | |
5d82e3e6 | 3091 | if (!obj->fence_dirty) { |
14415745 CW |
3092 | list_move_tail(®->lru_list, |
3093 | &dev_priv->mm.fence_list); | |
3094 | return 0; | |
3095 | } | |
3096 | } else if (enable) { | |
3097 | reg = i915_find_fence_reg(dev); | |
5dce5b93 CW |
3098 | if (IS_ERR(reg)) |
3099 | return PTR_ERR(reg); | |
d9e86c0e | 3100 | |
14415745 CW |
3101 | if (reg->obj) { |
3102 | struct drm_i915_gem_object *old = reg->obj; | |
3103 | ||
d0a57789 | 3104 | ret = i915_gem_object_wait_fence(old); |
29c5a587 CW |
3105 | if (ret) |
3106 | return ret; | |
3107 | ||
14415745 | 3108 | i915_gem_object_fence_lost(old); |
29c5a587 | 3109 | } |
14415745 | 3110 | } else |
a09ba7fa | 3111 | return 0; |
a09ba7fa | 3112 | |
14415745 | 3113 | i915_gem_object_update_fence(obj, reg, enable); |
14415745 | 3114 | |
9ce079e4 | 3115 | return 0; |
de151cf6 JB |
3116 | } |
3117 | ||
42d6ab48 CW |
3118 | static bool i915_gem_valid_gtt_space(struct drm_device *dev, |
3119 | struct drm_mm_node *gtt_space, | |
3120 | unsigned long cache_level) | |
3121 | { | |
3122 | struct drm_mm_node *other; | |
3123 | ||
3124 | /* On non-LLC machines we have to be careful when putting differing | |
3125 | * types of snoopable memory together to avoid the prefetcher | |
4239ca77 | 3126 | * crossing memory domains and dying. |
42d6ab48 CW |
3127 | */ |
3128 | if (HAS_LLC(dev)) | |
3129 | return true; | |
3130 | ||
c6cfb325 | 3131 | if (!drm_mm_node_allocated(gtt_space)) |
42d6ab48 CW |
3132 | return true; |
3133 | ||
3134 | if (list_empty(>t_space->node_list)) | |
3135 | return true; | |
3136 | ||
3137 | other = list_entry(gtt_space->node_list.prev, struct drm_mm_node, node_list); | |
3138 | if (other->allocated && !other->hole_follows && other->color != cache_level) | |
3139 | return false; | |
3140 | ||
3141 | other = list_entry(gtt_space->node_list.next, struct drm_mm_node, node_list); | |
3142 | if (other->allocated && !gtt_space->hole_follows && other->color != cache_level) | |
3143 | return false; | |
3144 | ||
3145 | return true; | |
3146 | } | |
3147 | ||
3148 | static void i915_gem_verify_gtt(struct drm_device *dev) | |
3149 | { | |
3150 | #if WATCH_GTT | |
3151 | struct drm_i915_private *dev_priv = dev->dev_private; | |
3152 | struct drm_i915_gem_object *obj; | |
3153 | int err = 0; | |
3154 | ||
35c20a60 | 3155 | list_for_each_entry(obj, &dev_priv->mm.gtt_list, global_list) { |
42d6ab48 CW |
3156 | if (obj->gtt_space == NULL) { |
3157 | printk(KERN_ERR "object found on GTT list with no space reserved\n"); | |
3158 | err++; | |
3159 | continue; | |
3160 | } | |
3161 | ||
3162 | if (obj->cache_level != obj->gtt_space->color) { | |
3163 | printk(KERN_ERR "object reserved space [%08lx, %08lx] with wrong color, cache_level=%x, color=%lx\n", | |
f343c5f6 BW |
3164 | i915_gem_obj_ggtt_offset(obj), |
3165 | i915_gem_obj_ggtt_offset(obj) + i915_gem_obj_ggtt_size(obj), | |
42d6ab48 CW |
3166 | obj->cache_level, |
3167 | obj->gtt_space->color); | |
3168 | err++; | |
3169 | continue; | |
3170 | } | |
3171 | ||
3172 | if (!i915_gem_valid_gtt_space(dev, | |
3173 | obj->gtt_space, | |
3174 | obj->cache_level)) { | |
3175 | printk(KERN_ERR "invalid GTT space found at [%08lx, %08lx] - color=%x\n", | |
f343c5f6 BW |
3176 | i915_gem_obj_ggtt_offset(obj), |
3177 | i915_gem_obj_ggtt_offset(obj) + i915_gem_obj_ggtt_size(obj), | |
42d6ab48 CW |
3178 | obj->cache_level); |
3179 | err++; | |
3180 | continue; | |
3181 | } | |
3182 | } | |
3183 | ||
3184 | WARN_ON(err); | |
3185 | #endif | |
3186 | } | |
3187 | ||
673a394b EA |
3188 | /** |
3189 | * Finds free space in the GTT aperture and binds the object there. | |
3190 | */ | |
262de145 | 3191 | static struct i915_vma * |
07fe0b12 BW |
3192 | i915_gem_object_bind_to_vm(struct drm_i915_gem_object *obj, |
3193 | struct i915_address_space *vm, | |
3194 | unsigned alignment, | |
1ec9e26d | 3195 | unsigned flags) |
673a394b | 3196 | { |
05394f39 | 3197 | struct drm_device *dev = obj->base.dev; |
673a394b | 3198 | drm_i915_private_t *dev_priv = dev->dev_private; |
5e783301 | 3199 | u32 size, fence_size, fence_alignment, unfenced_alignment; |
07fe0b12 | 3200 | size_t gtt_max = |
1ec9e26d | 3201 | flags & PIN_MAPPABLE ? dev_priv->gtt.mappable_end : vm->total; |
2f633156 | 3202 | struct i915_vma *vma; |
07f73f69 | 3203 | int ret; |
673a394b | 3204 | |
e28f8711 CW |
3205 | fence_size = i915_gem_get_gtt_size(dev, |
3206 | obj->base.size, | |
3207 | obj->tiling_mode); | |
3208 | fence_alignment = i915_gem_get_gtt_alignment(dev, | |
3209 | obj->base.size, | |
d865110c | 3210 | obj->tiling_mode, true); |
e28f8711 | 3211 | unfenced_alignment = |
d865110c | 3212 | i915_gem_get_gtt_alignment(dev, |
1ec9e26d DV |
3213 | obj->base.size, |
3214 | obj->tiling_mode, false); | |
a00b10c3 | 3215 | |
673a394b | 3216 | if (alignment == 0) |
1ec9e26d | 3217 | alignment = flags & PIN_MAPPABLE ? fence_alignment : |
5e783301 | 3218 | unfenced_alignment; |
1ec9e26d | 3219 | if (flags & PIN_MAPPABLE && alignment & (fence_alignment - 1)) { |
bd9b6a4e | 3220 | DRM_DEBUG("Invalid object alignment requested %u\n", alignment); |
262de145 | 3221 | return ERR_PTR(-EINVAL); |
673a394b EA |
3222 | } |
3223 | ||
1ec9e26d | 3224 | size = flags & PIN_MAPPABLE ? fence_size : obj->base.size; |
a00b10c3 | 3225 | |
654fc607 CW |
3226 | /* If the object is bigger than the entire aperture, reject it early |
3227 | * before evicting everything in a vain attempt to find space. | |
3228 | */ | |
0a9ae0d7 | 3229 | if (obj->base.size > gtt_max) { |
bd9b6a4e | 3230 | DRM_DEBUG("Attempting to bind an object larger than the aperture: object=%zd > %s aperture=%zu\n", |
a36689cb | 3231 | obj->base.size, |
1ec9e26d | 3232 | flags & PIN_MAPPABLE ? "mappable" : "total", |
0a9ae0d7 | 3233 | gtt_max); |
262de145 | 3234 | return ERR_PTR(-E2BIG); |
654fc607 CW |
3235 | } |
3236 | ||
37e680a1 | 3237 | ret = i915_gem_object_get_pages(obj); |
6c085a72 | 3238 | if (ret) |
262de145 | 3239 | return ERR_PTR(ret); |
6c085a72 | 3240 | |
fbdda6fb CW |
3241 | i915_gem_object_pin_pages(obj); |
3242 | ||
accfef2e | 3243 | vma = i915_gem_obj_lookup_or_create_vma(obj, vm); |
262de145 | 3244 | if (IS_ERR(vma)) |
bc6bc15b | 3245 | goto err_unpin; |
2f633156 | 3246 | |
0a9ae0d7 | 3247 | search_free: |
07fe0b12 | 3248 | ret = drm_mm_insert_node_in_range_generic(&vm->mm, &vma->node, |
0a9ae0d7 | 3249 | size, alignment, |
31e5d7c6 DH |
3250 | obj->cache_level, 0, gtt_max, |
3251 | DRM_MM_SEARCH_DEFAULT); | |
dc9dd7a2 | 3252 | if (ret) { |
f6cd1f15 | 3253 | ret = i915_gem_evict_something(dev, vm, size, alignment, |
1ec9e26d | 3254 | obj->cache_level, flags); |
dc9dd7a2 CW |
3255 | if (ret == 0) |
3256 | goto search_free; | |
9731129c | 3257 | |
bc6bc15b | 3258 | goto err_free_vma; |
673a394b | 3259 | } |
2f633156 | 3260 | if (WARN_ON(!i915_gem_valid_gtt_space(dev, &vma->node, |
c6cfb325 | 3261 | obj->cache_level))) { |
2f633156 | 3262 | ret = -EINVAL; |
bc6bc15b | 3263 | goto err_remove_node; |
673a394b EA |
3264 | } |
3265 | ||
74163907 | 3266 | ret = i915_gem_gtt_prepare_object(obj); |
2f633156 | 3267 | if (ret) |
bc6bc15b | 3268 | goto err_remove_node; |
673a394b | 3269 | |
35c20a60 | 3270 | list_move_tail(&obj->global_list, &dev_priv->mm.bound_list); |
ca191b13 | 3271 | list_add_tail(&vma->mm_list, &vm->inactive_list); |
bf1a1092 | 3272 | |
4bd561b3 BW |
3273 | if (i915_is_ggtt(vm)) { |
3274 | bool mappable, fenceable; | |
a00b10c3 | 3275 | |
49987099 DV |
3276 | fenceable = (vma->node.size == fence_size && |
3277 | (vma->node.start & (fence_alignment - 1)) == 0); | |
4bd561b3 | 3278 | |
49987099 DV |
3279 | mappable = (vma->node.start + obj->base.size <= |
3280 | dev_priv->gtt.mappable_end); | |
a00b10c3 | 3281 | |
5cacaac7 | 3282 | obj->map_and_fenceable = mappable && fenceable; |
4bd561b3 | 3283 | } |
75e9e915 | 3284 | |
1ec9e26d | 3285 | WARN_ON(flags & PIN_MAPPABLE && !obj->map_and_fenceable); |
75e9e915 | 3286 | |
1ec9e26d | 3287 | trace_i915_vma_bind(vma, flags); |
8ea99c92 DV |
3288 | vma->bind_vma(vma, obj->cache_level, |
3289 | flags & (PIN_MAPPABLE | PIN_GLOBAL) ? GLOBAL_BIND : 0); | |
3290 | ||
42d6ab48 | 3291 | i915_gem_verify_gtt(dev); |
262de145 | 3292 | return vma; |
2f633156 | 3293 | |
bc6bc15b | 3294 | err_remove_node: |
6286ef9b | 3295 | drm_mm_remove_node(&vma->node); |
bc6bc15b | 3296 | err_free_vma: |
2f633156 | 3297 | i915_gem_vma_destroy(vma); |
262de145 | 3298 | vma = ERR_PTR(ret); |
bc6bc15b | 3299 | err_unpin: |
2f633156 | 3300 | i915_gem_object_unpin_pages(obj); |
262de145 | 3301 | return vma; |
673a394b EA |
3302 | } |
3303 | ||
000433b6 | 3304 | bool |
2c22569b CW |
3305 | i915_gem_clflush_object(struct drm_i915_gem_object *obj, |
3306 | bool force) | |
673a394b | 3307 | { |
673a394b EA |
3308 | /* If we don't have a page list set up, then we're not pinned |
3309 | * to GPU, and we can ignore the cache flush because it'll happen | |
3310 | * again at bind time. | |
3311 | */ | |
05394f39 | 3312 | if (obj->pages == NULL) |
000433b6 | 3313 | return false; |
673a394b | 3314 | |
769ce464 ID |
3315 | /* |
3316 | * Stolen memory is always coherent with the GPU as it is explicitly | |
3317 | * marked as wc by the system, or the system is cache-coherent. | |
3318 | */ | |
3319 | if (obj->stolen) | |
000433b6 | 3320 | return false; |
769ce464 | 3321 | |
9c23f7fc CW |
3322 | /* If the GPU is snooping the contents of the CPU cache, |
3323 | * we do not need to manually clear the CPU cache lines. However, | |
3324 | * the caches are only snooped when the render cache is | |
3325 | * flushed/invalidated. As we always have to emit invalidations | |
3326 | * and flushes when moving into and out of the RENDER domain, correct | |
3327 | * snooping behaviour occurs naturally as the result of our domain | |
3328 | * tracking. | |
3329 | */ | |
2c22569b | 3330 | if (!force && cpu_cache_is_coherent(obj->base.dev, obj->cache_level)) |
000433b6 | 3331 | return false; |
9c23f7fc | 3332 | |
1c5d22f7 | 3333 | trace_i915_gem_object_clflush(obj); |
9da3da66 | 3334 | drm_clflush_sg(obj->pages); |
000433b6 CW |
3335 | |
3336 | return true; | |
e47c68e9 EA |
3337 | } |
3338 | ||
3339 | /** Flushes the GTT write domain for the object if it's dirty. */ | |
3340 | static void | |
05394f39 | 3341 | i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj) |
e47c68e9 | 3342 | { |
1c5d22f7 CW |
3343 | uint32_t old_write_domain; |
3344 | ||
05394f39 | 3345 | if (obj->base.write_domain != I915_GEM_DOMAIN_GTT) |
e47c68e9 EA |
3346 | return; |
3347 | ||
63256ec5 | 3348 | /* No actual flushing is required for the GTT write domain. Writes |
e47c68e9 EA |
3349 | * to it immediately go to main memory as far as we know, so there's |
3350 | * no chipset flush. It also doesn't land in render cache. | |
63256ec5 CW |
3351 | * |
3352 | * However, we do have to enforce the order so that all writes through | |
3353 | * the GTT land before any writes to the device, such as updates to | |
3354 | * the GATT itself. | |
e47c68e9 | 3355 | */ |
63256ec5 CW |
3356 | wmb(); |
3357 | ||
05394f39 CW |
3358 | old_write_domain = obj->base.write_domain; |
3359 | obj->base.write_domain = 0; | |
1c5d22f7 CW |
3360 | |
3361 | trace_i915_gem_object_change_domain(obj, | |
05394f39 | 3362 | obj->base.read_domains, |
1c5d22f7 | 3363 | old_write_domain); |
e47c68e9 EA |
3364 | } |
3365 | ||
3366 | /** Flushes the CPU write domain for the object if it's dirty. */ | |
3367 | static void | |
2c22569b CW |
3368 | i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj, |
3369 | bool force) | |
e47c68e9 | 3370 | { |
1c5d22f7 | 3371 | uint32_t old_write_domain; |
e47c68e9 | 3372 | |
05394f39 | 3373 | if (obj->base.write_domain != I915_GEM_DOMAIN_CPU) |
e47c68e9 EA |
3374 | return; |
3375 | ||
000433b6 CW |
3376 | if (i915_gem_clflush_object(obj, force)) |
3377 | i915_gem_chipset_flush(obj->base.dev); | |
3378 | ||
05394f39 CW |
3379 | old_write_domain = obj->base.write_domain; |
3380 | obj->base.write_domain = 0; | |
1c5d22f7 CW |
3381 | |
3382 | trace_i915_gem_object_change_domain(obj, | |
05394f39 | 3383 | obj->base.read_domains, |
1c5d22f7 | 3384 | old_write_domain); |
e47c68e9 EA |
3385 | } |
3386 | ||
2ef7eeaa EA |
3387 | /** |
3388 | * Moves a single object to the GTT read, and possibly write domain. | |
3389 | * | |
3390 | * This function returns when the move is complete, including waiting on | |
3391 | * flushes to occur. | |
3392 | */ | |
79e53945 | 3393 | int |
2021746e | 3394 | i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write) |
2ef7eeaa | 3395 | { |
8325a09d | 3396 | drm_i915_private_t *dev_priv = obj->base.dev->dev_private; |
1c5d22f7 | 3397 | uint32_t old_write_domain, old_read_domains; |
e47c68e9 | 3398 | int ret; |
2ef7eeaa | 3399 | |
02354392 | 3400 | /* Not valid to be called on unbound objects. */ |
9843877d | 3401 | if (!i915_gem_obj_bound_any(obj)) |
02354392 EA |
3402 | return -EINVAL; |
3403 | ||
8d7e3de1 CW |
3404 | if (obj->base.write_domain == I915_GEM_DOMAIN_GTT) |
3405 | return 0; | |
3406 | ||
0201f1ec | 3407 | ret = i915_gem_object_wait_rendering(obj, !write); |
88241785 CW |
3408 | if (ret) |
3409 | return ret; | |
3410 | ||
2c22569b | 3411 | i915_gem_object_flush_cpu_write_domain(obj, false); |
1c5d22f7 | 3412 | |
d0a57789 CW |
3413 | /* Serialise direct access to this object with the barriers for |
3414 | * coherent writes from the GPU, by effectively invalidating the | |
3415 | * GTT domain upon first access. | |
3416 | */ | |
3417 | if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0) | |
3418 | mb(); | |
3419 | ||
05394f39 CW |
3420 | old_write_domain = obj->base.write_domain; |
3421 | old_read_domains = obj->base.read_domains; | |
1c5d22f7 | 3422 | |
e47c68e9 EA |
3423 | /* It should now be out of any other write domains, and we can update |
3424 | * the domain values for our changes. | |
3425 | */ | |
05394f39 CW |
3426 | BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0); |
3427 | obj->base.read_domains |= I915_GEM_DOMAIN_GTT; | |
e47c68e9 | 3428 | if (write) { |
05394f39 CW |
3429 | obj->base.read_domains = I915_GEM_DOMAIN_GTT; |
3430 | obj->base.write_domain = I915_GEM_DOMAIN_GTT; | |
3431 | obj->dirty = 1; | |
2ef7eeaa EA |
3432 | } |
3433 | ||
1c5d22f7 CW |
3434 | trace_i915_gem_object_change_domain(obj, |
3435 | old_read_domains, | |
3436 | old_write_domain); | |
3437 | ||
8325a09d | 3438 | /* And bump the LRU for this access */ |
ca191b13 | 3439 | if (i915_gem_object_is_inactive(obj)) { |
5c2abbea | 3440 | struct i915_vma *vma = i915_gem_obj_to_ggtt(obj); |
ca191b13 BW |
3441 | if (vma) |
3442 | list_move_tail(&vma->mm_list, | |
3443 | &dev_priv->gtt.base.inactive_list); | |
3444 | ||
3445 | } | |
8325a09d | 3446 | |
e47c68e9 EA |
3447 | return 0; |
3448 | } | |
3449 | ||
e4ffd173 CW |
3450 | int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj, |
3451 | enum i915_cache_level cache_level) | |
3452 | { | |
7bddb01f | 3453 | struct drm_device *dev = obj->base.dev; |
3089c6f2 | 3454 | struct i915_vma *vma; |
e4ffd173 CW |
3455 | int ret; |
3456 | ||
3457 | if (obj->cache_level == cache_level) | |
3458 | return 0; | |
3459 | ||
d7f46fc4 | 3460 | if (i915_gem_obj_is_pinned(obj)) { |
e4ffd173 CW |
3461 | DRM_DEBUG("can not change the cache level of pinned objects\n"); |
3462 | return -EBUSY; | |
3463 | } | |
3464 | ||
3089c6f2 BW |
3465 | list_for_each_entry(vma, &obj->vma_list, vma_link) { |
3466 | if (!i915_gem_valid_gtt_space(dev, &vma->node, cache_level)) { | |
07fe0b12 | 3467 | ret = i915_vma_unbind(vma); |
3089c6f2 BW |
3468 | if (ret) |
3469 | return ret; | |
3470 | ||
3471 | break; | |
3472 | } | |
42d6ab48 CW |
3473 | } |
3474 | ||
3089c6f2 | 3475 | if (i915_gem_obj_bound_any(obj)) { |
e4ffd173 CW |
3476 | ret = i915_gem_object_finish_gpu(obj); |
3477 | if (ret) | |
3478 | return ret; | |
3479 | ||
3480 | i915_gem_object_finish_gtt(obj); | |
3481 | ||
3482 | /* Before SandyBridge, you could not use tiling or fence | |
3483 | * registers with snooped memory, so relinquish any fences | |
3484 | * currently pointing to our region in the aperture. | |
3485 | */ | |
42d6ab48 | 3486 | if (INTEL_INFO(dev)->gen < 6) { |
e4ffd173 CW |
3487 | ret = i915_gem_object_put_fence(obj); |
3488 | if (ret) | |
3489 | return ret; | |
3490 | } | |
3491 | ||
6f65e29a | 3492 | list_for_each_entry(vma, &obj->vma_list, vma_link) |
8ea99c92 DV |
3493 | if (drm_mm_node_allocated(&vma->node)) |
3494 | vma->bind_vma(vma, cache_level, | |
3495 | obj->has_global_gtt_mapping ? GLOBAL_BIND : 0); | |
e4ffd173 CW |
3496 | } |
3497 | ||
2c22569b CW |
3498 | list_for_each_entry(vma, &obj->vma_list, vma_link) |
3499 | vma->node.color = cache_level; | |
3500 | obj->cache_level = cache_level; | |
3501 | ||
3502 | if (cpu_write_needs_clflush(obj)) { | |
e4ffd173 CW |
3503 | u32 old_read_domains, old_write_domain; |
3504 | ||
3505 | /* If we're coming from LLC cached, then we haven't | |
3506 | * actually been tracking whether the data is in the | |
3507 | * CPU cache or not, since we only allow one bit set | |
3508 | * in obj->write_domain and have been skipping the clflushes. | |
3509 | * Just set it to the CPU cache for now. | |
3510 | */ | |
3511 | WARN_ON(obj->base.write_domain & ~I915_GEM_DOMAIN_CPU); | |
e4ffd173 CW |
3512 | |
3513 | old_read_domains = obj->base.read_domains; | |
3514 | old_write_domain = obj->base.write_domain; | |
3515 | ||
3516 | obj->base.read_domains = I915_GEM_DOMAIN_CPU; | |
3517 | obj->base.write_domain = I915_GEM_DOMAIN_CPU; | |
3518 | ||
3519 | trace_i915_gem_object_change_domain(obj, | |
3520 | old_read_domains, | |
3521 | old_write_domain); | |
3522 | } | |
3523 | ||
42d6ab48 | 3524 | i915_gem_verify_gtt(dev); |
e4ffd173 CW |
3525 | return 0; |
3526 | } | |
3527 | ||
199adf40 BW |
3528 | int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data, |
3529 | struct drm_file *file) | |
e6994aee | 3530 | { |
199adf40 | 3531 | struct drm_i915_gem_caching *args = data; |
e6994aee CW |
3532 | struct drm_i915_gem_object *obj; |
3533 | int ret; | |
3534 | ||
3535 | ret = i915_mutex_lock_interruptible(dev); | |
3536 | if (ret) | |
3537 | return ret; | |
3538 | ||
3539 | obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle)); | |
3540 | if (&obj->base == NULL) { | |
3541 | ret = -ENOENT; | |
3542 | goto unlock; | |
3543 | } | |
3544 | ||
651d794f CW |
3545 | switch (obj->cache_level) { |
3546 | case I915_CACHE_LLC: | |
3547 | case I915_CACHE_L3_LLC: | |
3548 | args->caching = I915_CACHING_CACHED; | |
3549 | break; | |
3550 | ||
4257d3ba CW |
3551 | case I915_CACHE_WT: |
3552 | args->caching = I915_CACHING_DISPLAY; | |
3553 | break; | |
3554 | ||
651d794f CW |
3555 | default: |
3556 | args->caching = I915_CACHING_NONE; | |
3557 | break; | |
3558 | } | |
e6994aee CW |
3559 | |
3560 | drm_gem_object_unreference(&obj->base); | |
3561 | unlock: | |
3562 | mutex_unlock(&dev->struct_mutex); | |
3563 | return ret; | |
3564 | } | |
3565 | ||
199adf40 BW |
3566 | int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data, |
3567 | struct drm_file *file) | |
e6994aee | 3568 | { |
199adf40 | 3569 | struct drm_i915_gem_caching *args = data; |
e6994aee CW |
3570 | struct drm_i915_gem_object *obj; |
3571 | enum i915_cache_level level; | |
3572 | int ret; | |
3573 | ||
199adf40 BW |
3574 | switch (args->caching) { |
3575 | case I915_CACHING_NONE: | |
e6994aee CW |
3576 | level = I915_CACHE_NONE; |
3577 | break; | |
199adf40 | 3578 | case I915_CACHING_CACHED: |
e6994aee CW |
3579 | level = I915_CACHE_LLC; |
3580 | break; | |
4257d3ba CW |
3581 | case I915_CACHING_DISPLAY: |
3582 | level = HAS_WT(dev) ? I915_CACHE_WT : I915_CACHE_NONE; | |
3583 | break; | |
e6994aee CW |
3584 | default: |
3585 | return -EINVAL; | |
3586 | } | |
3587 | ||
3bc2913e BW |
3588 | ret = i915_mutex_lock_interruptible(dev); |
3589 | if (ret) | |
3590 | return ret; | |
3591 | ||
e6994aee CW |
3592 | obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle)); |
3593 | if (&obj->base == NULL) { | |
3594 | ret = -ENOENT; | |
3595 | goto unlock; | |
3596 | } | |
3597 | ||
3598 | ret = i915_gem_object_set_cache_level(obj, level); | |
3599 | ||
3600 | drm_gem_object_unreference(&obj->base); | |
3601 | unlock: | |
3602 | mutex_unlock(&dev->struct_mutex); | |
3603 | return ret; | |
3604 | } | |
3605 | ||
cc98b413 CW |
3606 | static bool is_pin_display(struct drm_i915_gem_object *obj) |
3607 | { | |
3608 | /* There are 3 sources that pin objects: | |
3609 | * 1. The display engine (scanouts, sprites, cursors); | |
3610 | * 2. Reservations for execbuffer; | |
3611 | * 3. The user. | |
3612 | * | |
3613 | * We can ignore reservations as we hold the struct_mutex and | |
3614 | * are only called outside of the reservation path. The user | |
3615 | * can only increment pin_count once, and so if after | |
3616 | * subtracting the potential reference by the user, any pin_count | |
3617 | * remains, it must be due to another use by the display engine. | |
3618 | */ | |
d7f46fc4 | 3619 | return i915_gem_obj_to_ggtt(obj)->pin_count - !!obj->user_pin_count; |
cc98b413 CW |
3620 | } |
3621 | ||
b9241ea3 | 3622 | /* |
2da3b9b9 CW |
3623 | * Prepare buffer for display plane (scanout, cursors, etc). |
3624 | * Can be called from an uninterruptible phase (modesetting) and allows | |
3625 | * any flushes to be pipelined (for pageflips). | |
b9241ea3 ZW |
3626 | */ |
3627 | int | |
2da3b9b9 CW |
3628 | i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj, |
3629 | u32 alignment, | |
919926ae | 3630 | struct intel_ring_buffer *pipelined) |
b9241ea3 | 3631 | { |
2da3b9b9 | 3632 | u32 old_read_domains, old_write_domain; |
b9241ea3 ZW |
3633 | int ret; |
3634 | ||
0be73284 | 3635 | if (pipelined != obj->ring) { |
2911a35b BW |
3636 | ret = i915_gem_object_sync(obj, pipelined); |
3637 | if (ret) | |
b9241ea3 ZW |
3638 | return ret; |
3639 | } | |
3640 | ||
cc98b413 CW |
3641 | /* Mark the pin_display early so that we account for the |
3642 | * display coherency whilst setting up the cache domains. | |
3643 | */ | |
3644 | obj->pin_display = true; | |
3645 | ||
a7ef0640 EA |
3646 | /* The display engine is not coherent with the LLC cache on gen6. As |
3647 | * a result, we make sure that the pinning that is about to occur is | |
3648 | * done with uncached PTEs. This is lowest common denominator for all | |
3649 | * chipsets. | |
3650 | * | |
3651 | * However for gen6+, we could do better by using the GFDT bit instead | |
3652 | * of uncaching, which would allow us to flush all the LLC-cached data | |
3653 | * with that bit in the PTE to main memory with just one PIPE_CONTROL. | |
3654 | */ | |
651d794f CW |
3655 | ret = i915_gem_object_set_cache_level(obj, |
3656 | HAS_WT(obj->base.dev) ? I915_CACHE_WT : I915_CACHE_NONE); | |
a7ef0640 | 3657 | if (ret) |
cc98b413 | 3658 | goto err_unpin_display; |
a7ef0640 | 3659 | |
2da3b9b9 CW |
3660 | /* As the user may map the buffer once pinned in the display plane |
3661 | * (e.g. libkms for the bootup splash), we have to ensure that we | |
3662 | * always use map_and_fenceable for all scanout buffers. | |
3663 | */ | |
1ec9e26d | 3664 | ret = i915_gem_obj_ggtt_pin(obj, alignment, PIN_MAPPABLE); |
2da3b9b9 | 3665 | if (ret) |
cc98b413 | 3666 | goto err_unpin_display; |
2da3b9b9 | 3667 | |
2c22569b | 3668 | i915_gem_object_flush_cpu_write_domain(obj, true); |
b118c1e3 | 3669 | |
2da3b9b9 | 3670 | old_write_domain = obj->base.write_domain; |
05394f39 | 3671 | old_read_domains = obj->base.read_domains; |
2da3b9b9 CW |
3672 | |
3673 | /* It should now be out of any other write domains, and we can update | |
3674 | * the domain values for our changes. | |
3675 | */ | |
e5f1d962 | 3676 | obj->base.write_domain = 0; |
05394f39 | 3677 | obj->base.read_domains |= I915_GEM_DOMAIN_GTT; |
b9241ea3 ZW |
3678 | |
3679 | trace_i915_gem_object_change_domain(obj, | |
3680 | old_read_domains, | |
2da3b9b9 | 3681 | old_write_domain); |
b9241ea3 ZW |
3682 | |
3683 | return 0; | |
cc98b413 CW |
3684 | |
3685 | err_unpin_display: | |
3686 | obj->pin_display = is_pin_display(obj); | |
3687 | return ret; | |
3688 | } | |
3689 | ||
3690 | void | |
3691 | i915_gem_object_unpin_from_display_plane(struct drm_i915_gem_object *obj) | |
3692 | { | |
d7f46fc4 | 3693 | i915_gem_object_ggtt_unpin(obj); |
cc98b413 | 3694 | obj->pin_display = is_pin_display(obj); |
b9241ea3 ZW |
3695 | } |
3696 | ||
85345517 | 3697 | int |
a8198eea | 3698 | i915_gem_object_finish_gpu(struct drm_i915_gem_object *obj) |
85345517 | 3699 | { |
88241785 CW |
3700 | int ret; |
3701 | ||
a8198eea | 3702 | if ((obj->base.read_domains & I915_GEM_GPU_DOMAINS) == 0) |
85345517 CW |
3703 | return 0; |
3704 | ||
0201f1ec | 3705 | ret = i915_gem_object_wait_rendering(obj, false); |
c501ae7f CW |
3706 | if (ret) |
3707 | return ret; | |
3708 | ||
a8198eea CW |
3709 | /* Ensure that we invalidate the GPU's caches and TLBs. */ |
3710 | obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS; | |
c501ae7f | 3711 | return 0; |
85345517 CW |
3712 | } |
3713 | ||
e47c68e9 EA |
3714 | /** |
3715 | * Moves a single object to the CPU read, and possibly write domain. | |
3716 | * | |
3717 | * This function returns when the move is complete, including waiting on | |
3718 | * flushes to occur. | |
3719 | */ | |
dabdfe02 | 3720 | int |
919926ae | 3721 | i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write) |
e47c68e9 | 3722 | { |
1c5d22f7 | 3723 | uint32_t old_write_domain, old_read_domains; |
e47c68e9 EA |
3724 | int ret; |
3725 | ||
8d7e3de1 CW |
3726 | if (obj->base.write_domain == I915_GEM_DOMAIN_CPU) |
3727 | return 0; | |
3728 | ||
0201f1ec | 3729 | ret = i915_gem_object_wait_rendering(obj, !write); |
88241785 CW |
3730 | if (ret) |
3731 | return ret; | |
3732 | ||
e47c68e9 | 3733 | i915_gem_object_flush_gtt_write_domain(obj); |
2ef7eeaa | 3734 | |
05394f39 CW |
3735 | old_write_domain = obj->base.write_domain; |
3736 | old_read_domains = obj->base.read_domains; | |
1c5d22f7 | 3737 | |
e47c68e9 | 3738 | /* Flush the CPU cache if it's still invalid. */ |
05394f39 | 3739 | if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) { |
2c22569b | 3740 | i915_gem_clflush_object(obj, false); |
2ef7eeaa | 3741 | |
05394f39 | 3742 | obj->base.read_domains |= I915_GEM_DOMAIN_CPU; |
2ef7eeaa EA |
3743 | } |
3744 | ||
3745 | /* It should now be out of any other write domains, and we can update | |
3746 | * the domain values for our changes. | |
3747 | */ | |
05394f39 | 3748 | BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0); |
e47c68e9 EA |
3749 | |
3750 | /* If we're writing through the CPU, then the GPU read domains will | |
3751 | * need to be invalidated at next use. | |
3752 | */ | |
3753 | if (write) { | |
05394f39 CW |
3754 | obj->base.read_domains = I915_GEM_DOMAIN_CPU; |
3755 | obj->base.write_domain = I915_GEM_DOMAIN_CPU; | |
e47c68e9 | 3756 | } |
2ef7eeaa | 3757 | |
1c5d22f7 CW |
3758 | trace_i915_gem_object_change_domain(obj, |
3759 | old_read_domains, | |
3760 | old_write_domain); | |
3761 | ||
2ef7eeaa EA |
3762 | return 0; |
3763 | } | |
3764 | ||
673a394b EA |
3765 | /* Throttle our rendering by waiting until the ring has completed our requests |
3766 | * emitted over 20 msec ago. | |
3767 | * | |
b962442e EA |
3768 | * Note that if we were to use the current jiffies each time around the loop, |
3769 | * we wouldn't escape the function with any frames outstanding if the time to | |
3770 | * render a frame was over 20ms. | |
3771 | * | |
673a394b EA |
3772 | * This should get us reasonable parallelism between CPU and GPU but also |
3773 | * relatively low latency when blocking on a particular request to finish. | |
3774 | */ | |
40a5f0de | 3775 | static int |
f787a5f5 | 3776 | i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file) |
40a5f0de | 3777 | { |
f787a5f5 CW |
3778 | struct drm_i915_private *dev_priv = dev->dev_private; |
3779 | struct drm_i915_file_private *file_priv = file->driver_priv; | |
b962442e | 3780 | unsigned long recent_enough = jiffies - msecs_to_jiffies(20); |
f787a5f5 CW |
3781 | struct drm_i915_gem_request *request; |
3782 | struct intel_ring_buffer *ring = NULL; | |
f69061be | 3783 | unsigned reset_counter; |
f787a5f5 CW |
3784 | u32 seqno = 0; |
3785 | int ret; | |
93533c29 | 3786 | |
308887aa DV |
3787 | ret = i915_gem_wait_for_error(&dev_priv->gpu_error); |
3788 | if (ret) | |
3789 | return ret; | |
3790 | ||
3791 | ret = i915_gem_check_wedge(&dev_priv->gpu_error, false); | |
3792 | if (ret) | |
3793 | return ret; | |
e110e8d6 | 3794 | |
1c25595f | 3795 | spin_lock(&file_priv->mm.lock); |
f787a5f5 | 3796 | list_for_each_entry(request, &file_priv->mm.request_list, client_list) { |
b962442e EA |
3797 | if (time_after_eq(request->emitted_jiffies, recent_enough)) |
3798 | break; | |
40a5f0de | 3799 | |
f787a5f5 CW |
3800 | ring = request->ring; |
3801 | seqno = request->seqno; | |
b962442e | 3802 | } |
f69061be | 3803 | reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter); |
1c25595f | 3804 | spin_unlock(&file_priv->mm.lock); |
40a5f0de | 3805 | |
f787a5f5 CW |
3806 | if (seqno == 0) |
3807 | return 0; | |
2bc43b5c | 3808 | |
b29c19b6 | 3809 | ret = __wait_seqno(ring, seqno, reset_counter, true, NULL, NULL); |
f787a5f5 CW |
3810 | if (ret == 0) |
3811 | queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0); | |
40a5f0de EA |
3812 | |
3813 | return ret; | |
3814 | } | |
3815 | ||
673a394b | 3816 | int |
05394f39 | 3817 | i915_gem_object_pin(struct drm_i915_gem_object *obj, |
c37e2204 | 3818 | struct i915_address_space *vm, |
05394f39 | 3819 | uint32_t alignment, |
1ec9e26d | 3820 | unsigned flags) |
673a394b | 3821 | { |
07fe0b12 | 3822 | struct i915_vma *vma; |
673a394b EA |
3823 | int ret; |
3824 | ||
bf3d149b | 3825 | if (WARN_ON(flags & (PIN_GLOBAL | PIN_MAPPABLE) && !i915_is_ggtt(vm))) |
1ec9e26d | 3826 | return -EINVAL; |
07fe0b12 BW |
3827 | |
3828 | vma = i915_gem_obj_to_vma(obj, vm); | |
07fe0b12 | 3829 | if (vma) { |
d7f46fc4 BW |
3830 | if (WARN_ON(vma->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT)) |
3831 | return -EBUSY; | |
3832 | ||
07fe0b12 BW |
3833 | if ((alignment && |
3834 | vma->node.start & (alignment - 1)) || | |
1ec9e26d | 3835 | (flags & PIN_MAPPABLE && !obj->map_and_fenceable)) { |
d7f46fc4 | 3836 | WARN(vma->pin_count, |
ae7d49d8 | 3837 | "bo is already pinned with incorrect alignment:" |
f343c5f6 | 3838 | " offset=%lx, req.alignment=%x, req.map_and_fenceable=%d," |
75e9e915 | 3839 | " obj->map_and_fenceable=%d\n", |
07fe0b12 | 3840 | i915_gem_obj_offset(obj, vm), alignment, |
1ec9e26d | 3841 | flags & PIN_MAPPABLE, |
05394f39 | 3842 | obj->map_and_fenceable); |
07fe0b12 | 3843 | ret = i915_vma_unbind(vma); |
ac0c6b5a CW |
3844 | if (ret) |
3845 | return ret; | |
8ea99c92 DV |
3846 | |
3847 | vma = NULL; | |
ac0c6b5a CW |
3848 | } |
3849 | } | |
3850 | ||
8ea99c92 | 3851 | if (vma == NULL || !drm_mm_node_allocated(&vma->node)) { |
262de145 DV |
3852 | vma = i915_gem_object_bind_to_vm(obj, vm, alignment, flags); |
3853 | if (IS_ERR(vma)) | |
3854 | return PTR_ERR(vma); | |
22c344e9 | 3855 | } |
76446cac | 3856 | |
8ea99c92 DV |
3857 | if (flags & PIN_GLOBAL && !obj->has_global_gtt_mapping) |
3858 | vma->bind_vma(vma, obj->cache_level, GLOBAL_BIND); | |
74898d7e | 3859 | |
8ea99c92 | 3860 | vma->pin_count++; |
1ec9e26d DV |
3861 | if (flags & PIN_MAPPABLE) |
3862 | obj->pin_mappable |= true; | |
673a394b EA |
3863 | |
3864 | return 0; | |
3865 | } | |
3866 | ||
3867 | void | |
d7f46fc4 | 3868 | i915_gem_object_ggtt_unpin(struct drm_i915_gem_object *obj) |
673a394b | 3869 | { |
d7f46fc4 | 3870 | struct i915_vma *vma = i915_gem_obj_to_ggtt(obj); |
673a394b | 3871 | |
d7f46fc4 BW |
3872 | BUG_ON(!vma); |
3873 | BUG_ON(vma->pin_count == 0); | |
3874 | BUG_ON(!i915_gem_obj_ggtt_bound(obj)); | |
3875 | ||
3876 | if (--vma->pin_count == 0) | |
6299f992 | 3877 | obj->pin_mappable = false; |
673a394b EA |
3878 | } |
3879 | ||
3880 | int | |
3881 | i915_gem_pin_ioctl(struct drm_device *dev, void *data, | |
05394f39 | 3882 | struct drm_file *file) |
673a394b EA |
3883 | { |
3884 | struct drm_i915_gem_pin *args = data; | |
05394f39 | 3885 | struct drm_i915_gem_object *obj; |
673a394b EA |
3886 | int ret; |
3887 | ||
02f6bccc DV |
3888 | if (INTEL_INFO(dev)->gen >= 6) |
3889 | return -ENODEV; | |
3890 | ||
1d7cfea1 CW |
3891 | ret = i915_mutex_lock_interruptible(dev); |
3892 | if (ret) | |
3893 | return ret; | |
673a394b | 3894 | |
05394f39 | 3895 | obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle)); |
c8725226 | 3896 | if (&obj->base == NULL) { |
1d7cfea1 CW |
3897 | ret = -ENOENT; |
3898 | goto unlock; | |
673a394b | 3899 | } |
673a394b | 3900 | |
05394f39 | 3901 | if (obj->madv != I915_MADV_WILLNEED) { |
bd9b6a4e | 3902 | DRM_DEBUG("Attempting to pin a purgeable buffer\n"); |
8c99e57d | 3903 | ret = -EFAULT; |
1d7cfea1 | 3904 | goto out; |
3ef94daa CW |
3905 | } |
3906 | ||
05394f39 | 3907 | if (obj->pin_filp != NULL && obj->pin_filp != file) { |
bd9b6a4e | 3908 | DRM_DEBUG("Already pinned in i915_gem_pin_ioctl(): %d\n", |
79e53945 | 3909 | args->handle); |
1d7cfea1 CW |
3910 | ret = -EINVAL; |
3911 | goto out; | |
79e53945 JB |
3912 | } |
3913 | ||
aa5f8021 DV |
3914 | if (obj->user_pin_count == ULONG_MAX) { |
3915 | ret = -EBUSY; | |
3916 | goto out; | |
3917 | } | |
3918 | ||
93be8788 | 3919 | if (obj->user_pin_count == 0) { |
1ec9e26d | 3920 | ret = i915_gem_obj_ggtt_pin(obj, args->alignment, PIN_MAPPABLE); |
1d7cfea1 CW |
3921 | if (ret) |
3922 | goto out; | |
673a394b EA |
3923 | } |
3924 | ||
93be8788 CW |
3925 | obj->user_pin_count++; |
3926 | obj->pin_filp = file; | |
3927 | ||
f343c5f6 | 3928 | args->offset = i915_gem_obj_ggtt_offset(obj); |
1d7cfea1 | 3929 | out: |
05394f39 | 3930 | drm_gem_object_unreference(&obj->base); |
1d7cfea1 | 3931 | unlock: |
673a394b | 3932 | mutex_unlock(&dev->struct_mutex); |
1d7cfea1 | 3933 | return ret; |
673a394b EA |
3934 | } |
3935 | ||
3936 | int | |
3937 | i915_gem_unpin_ioctl(struct drm_device *dev, void *data, | |
05394f39 | 3938 | struct drm_file *file) |
673a394b EA |
3939 | { |
3940 | struct drm_i915_gem_pin *args = data; | |
05394f39 | 3941 | struct drm_i915_gem_object *obj; |
76c1dec1 | 3942 | int ret; |
673a394b | 3943 | |
1d7cfea1 CW |
3944 | ret = i915_mutex_lock_interruptible(dev); |
3945 | if (ret) | |
3946 | return ret; | |
673a394b | 3947 | |
05394f39 | 3948 | obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle)); |
c8725226 | 3949 | if (&obj->base == NULL) { |
1d7cfea1 CW |
3950 | ret = -ENOENT; |
3951 | goto unlock; | |
673a394b | 3952 | } |
76c1dec1 | 3953 | |
05394f39 | 3954 | if (obj->pin_filp != file) { |
bd9b6a4e | 3955 | DRM_DEBUG("Not pinned by caller in i915_gem_pin_ioctl(): %d\n", |
79e53945 | 3956 | args->handle); |
1d7cfea1 CW |
3957 | ret = -EINVAL; |
3958 | goto out; | |
79e53945 | 3959 | } |
05394f39 CW |
3960 | obj->user_pin_count--; |
3961 | if (obj->user_pin_count == 0) { | |
3962 | obj->pin_filp = NULL; | |
d7f46fc4 | 3963 | i915_gem_object_ggtt_unpin(obj); |
79e53945 | 3964 | } |
673a394b | 3965 | |
1d7cfea1 | 3966 | out: |
05394f39 | 3967 | drm_gem_object_unreference(&obj->base); |
1d7cfea1 | 3968 | unlock: |
673a394b | 3969 | mutex_unlock(&dev->struct_mutex); |
1d7cfea1 | 3970 | return ret; |
673a394b EA |
3971 | } |
3972 | ||
3973 | int | |
3974 | i915_gem_busy_ioctl(struct drm_device *dev, void *data, | |
05394f39 | 3975 | struct drm_file *file) |
673a394b EA |
3976 | { |
3977 | struct drm_i915_gem_busy *args = data; | |
05394f39 | 3978 | struct drm_i915_gem_object *obj; |
30dbf0c0 CW |
3979 | int ret; |
3980 | ||
76c1dec1 | 3981 | ret = i915_mutex_lock_interruptible(dev); |
1d7cfea1 | 3982 | if (ret) |
76c1dec1 | 3983 | return ret; |
673a394b | 3984 | |
05394f39 | 3985 | obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle)); |
c8725226 | 3986 | if (&obj->base == NULL) { |
1d7cfea1 CW |
3987 | ret = -ENOENT; |
3988 | goto unlock; | |
673a394b | 3989 | } |
d1b851fc | 3990 | |
0be555b6 CW |
3991 | /* Count all active objects as busy, even if they are currently not used |
3992 | * by the gpu. Users of this interface expect objects to eventually | |
3993 | * become non-busy without any further actions, therefore emit any | |
3994 | * necessary flushes here. | |
c4de0a5d | 3995 | */ |
30dfebf3 | 3996 | ret = i915_gem_object_flush_active(obj); |
0be555b6 | 3997 | |
30dfebf3 | 3998 | args->busy = obj->active; |
e9808edd CW |
3999 | if (obj->ring) { |
4000 | BUILD_BUG_ON(I915_NUM_RINGS > 16); | |
4001 | args->busy |= intel_ring_flag(obj->ring) << 16; | |
4002 | } | |
673a394b | 4003 | |
05394f39 | 4004 | drm_gem_object_unreference(&obj->base); |
1d7cfea1 | 4005 | unlock: |
673a394b | 4006 | mutex_unlock(&dev->struct_mutex); |
1d7cfea1 | 4007 | return ret; |
673a394b EA |
4008 | } |
4009 | ||
4010 | int | |
4011 | i915_gem_throttle_ioctl(struct drm_device *dev, void *data, | |
4012 | struct drm_file *file_priv) | |
4013 | { | |
0206e353 | 4014 | return i915_gem_ring_throttle(dev, file_priv); |
673a394b EA |
4015 | } |
4016 | ||
3ef94daa CW |
4017 | int |
4018 | i915_gem_madvise_ioctl(struct drm_device *dev, void *data, | |
4019 | struct drm_file *file_priv) | |
4020 | { | |
4021 | struct drm_i915_gem_madvise *args = data; | |
05394f39 | 4022 | struct drm_i915_gem_object *obj; |
76c1dec1 | 4023 | int ret; |
3ef94daa CW |
4024 | |
4025 | switch (args->madv) { | |
4026 | case I915_MADV_DONTNEED: | |
4027 | case I915_MADV_WILLNEED: | |
4028 | break; | |
4029 | default: | |
4030 | return -EINVAL; | |
4031 | } | |
4032 | ||
1d7cfea1 CW |
4033 | ret = i915_mutex_lock_interruptible(dev); |
4034 | if (ret) | |
4035 | return ret; | |
4036 | ||
05394f39 | 4037 | obj = to_intel_bo(drm_gem_object_lookup(dev, file_priv, args->handle)); |
c8725226 | 4038 | if (&obj->base == NULL) { |
1d7cfea1 CW |
4039 | ret = -ENOENT; |
4040 | goto unlock; | |
3ef94daa | 4041 | } |
3ef94daa | 4042 | |
d7f46fc4 | 4043 | if (i915_gem_obj_is_pinned(obj)) { |
1d7cfea1 CW |
4044 | ret = -EINVAL; |
4045 | goto out; | |
3ef94daa CW |
4046 | } |
4047 | ||
05394f39 CW |
4048 | if (obj->madv != __I915_MADV_PURGED) |
4049 | obj->madv = args->madv; | |
3ef94daa | 4050 | |
6c085a72 CW |
4051 | /* if the object is no longer attached, discard its backing storage */ |
4052 | if (i915_gem_object_is_purgeable(obj) && obj->pages == NULL) | |
2d7ef395 CW |
4053 | i915_gem_object_truncate(obj); |
4054 | ||
05394f39 | 4055 | args->retained = obj->madv != __I915_MADV_PURGED; |
bb6baf76 | 4056 | |
1d7cfea1 | 4057 | out: |
05394f39 | 4058 | drm_gem_object_unreference(&obj->base); |
1d7cfea1 | 4059 | unlock: |
3ef94daa | 4060 | mutex_unlock(&dev->struct_mutex); |
1d7cfea1 | 4061 | return ret; |
3ef94daa CW |
4062 | } |
4063 | ||
37e680a1 CW |
4064 | void i915_gem_object_init(struct drm_i915_gem_object *obj, |
4065 | const struct drm_i915_gem_object_ops *ops) | |
0327d6ba | 4066 | { |
35c20a60 | 4067 | INIT_LIST_HEAD(&obj->global_list); |
0327d6ba | 4068 | INIT_LIST_HEAD(&obj->ring_list); |
b25cb2f8 | 4069 | INIT_LIST_HEAD(&obj->obj_exec_link); |
2f633156 | 4070 | INIT_LIST_HEAD(&obj->vma_list); |
0327d6ba | 4071 | |
37e680a1 CW |
4072 | obj->ops = ops; |
4073 | ||
0327d6ba CW |
4074 | obj->fence_reg = I915_FENCE_REG_NONE; |
4075 | obj->madv = I915_MADV_WILLNEED; | |
4076 | /* Avoid an unnecessary call to unbind on the first bind. */ | |
4077 | obj->map_and_fenceable = true; | |
4078 | ||
4079 | i915_gem_info_add_obj(obj->base.dev->dev_private, obj->base.size); | |
4080 | } | |
4081 | ||
37e680a1 CW |
4082 | static const struct drm_i915_gem_object_ops i915_gem_object_ops = { |
4083 | .get_pages = i915_gem_object_get_pages_gtt, | |
4084 | .put_pages = i915_gem_object_put_pages_gtt, | |
4085 | }; | |
4086 | ||
05394f39 CW |
4087 | struct drm_i915_gem_object *i915_gem_alloc_object(struct drm_device *dev, |
4088 | size_t size) | |
ac52bc56 | 4089 | { |
c397b908 | 4090 | struct drm_i915_gem_object *obj; |
5949eac4 | 4091 | struct address_space *mapping; |
1a240d4d | 4092 | gfp_t mask; |
ac52bc56 | 4093 | |
42dcedd4 | 4094 | obj = i915_gem_object_alloc(dev); |
c397b908 DV |
4095 | if (obj == NULL) |
4096 | return NULL; | |
673a394b | 4097 | |
c397b908 | 4098 | if (drm_gem_object_init(dev, &obj->base, size) != 0) { |
42dcedd4 | 4099 | i915_gem_object_free(obj); |
c397b908 DV |
4100 | return NULL; |
4101 | } | |
673a394b | 4102 | |
bed1ea95 CW |
4103 | mask = GFP_HIGHUSER | __GFP_RECLAIMABLE; |
4104 | if (IS_CRESTLINE(dev) || IS_BROADWATER(dev)) { | |
4105 | /* 965gm cannot relocate objects above 4GiB. */ | |
4106 | mask &= ~__GFP_HIGHMEM; | |
4107 | mask |= __GFP_DMA32; | |
4108 | } | |
4109 | ||
496ad9aa | 4110 | mapping = file_inode(obj->base.filp)->i_mapping; |
bed1ea95 | 4111 | mapping_set_gfp_mask(mapping, mask); |
5949eac4 | 4112 | |
37e680a1 | 4113 | i915_gem_object_init(obj, &i915_gem_object_ops); |
73aa808f | 4114 | |
c397b908 DV |
4115 | obj->base.write_domain = I915_GEM_DOMAIN_CPU; |
4116 | obj->base.read_domains = I915_GEM_DOMAIN_CPU; | |
673a394b | 4117 | |
3d29b842 ED |
4118 | if (HAS_LLC(dev)) { |
4119 | /* On some devices, we can have the GPU use the LLC (the CPU | |
a1871112 EA |
4120 | * cache) for about a 10% performance improvement |
4121 | * compared to uncached. Graphics requests other than | |
4122 | * display scanout are coherent with the CPU in | |
4123 | * accessing this cache. This means in this mode we | |
4124 | * don't need to clflush on the CPU side, and on the | |
4125 | * GPU side we only need to flush internal caches to | |
4126 | * get data visible to the CPU. | |
4127 | * | |
4128 | * However, we maintain the display planes as UC, and so | |
4129 | * need to rebind when first used as such. | |
4130 | */ | |
4131 | obj->cache_level = I915_CACHE_LLC; | |
4132 | } else | |
4133 | obj->cache_level = I915_CACHE_NONE; | |
4134 | ||
d861e338 DV |
4135 | trace_i915_gem_object_create(obj); |
4136 | ||
05394f39 | 4137 | return obj; |
c397b908 DV |
4138 | } |
4139 | ||
1488fc08 | 4140 | void i915_gem_free_object(struct drm_gem_object *gem_obj) |
673a394b | 4141 | { |
1488fc08 | 4142 | struct drm_i915_gem_object *obj = to_intel_bo(gem_obj); |
05394f39 | 4143 | struct drm_device *dev = obj->base.dev; |
be72615b | 4144 | drm_i915_private_t *dev_priv = dev->dev_private; |
07fe0b12 | 4145 | struct i915_vma *vma, *next; |
673a394b | 4146 | |
f65c9168 PZ |
4147 | intel_runtime_pm_get(dev_priv); |
4148 | ||
26e12f89 CW |
4149 | trace_i915_gem_object_destroy(obj); |
4150 | ||
1488fc08 CW |
4151 | if (obj->phys_obj) |
4152 | i915_gem_detach_phys_object(dev, obj); | |
4153 | ||
07fe0b12 | 4154 | list_for_each_entry_safe(vma, next, &obj->vma_list, vma_link) { |
d7f46fc4 BW |
4155 | int ret; |
4156 | ||
4157 | vma->pin_count = 0; | |
4158 | ret = i915_vma_unbind(vma); | |
07fe0b12 BW |
4159 | if (WARN_ON(ret == -ERESTARTSYS)) { |
4160 | bool was_interruptible; | |
1488fc08 | 4161 | |
07fe0b12 BW |
4162 | was_interruptible = dev_priv->mm.interruptible; |
4163 | dev_priv->mm.interruptible = false; | |
1488fc08 | 4164 | |
07fe0b12 | 4165 | WARN_ON(i915_vma_unbind(vma)); |
1488fc08 | 4166 | |
07fe0b12 BW |
4167 | dev_priv->mm.interruptible = was_interruptible; |
4168 | } | |
1488fc08 CW |
4169 | } |
4170 | ||
1d64ae71 BW |
4171 | /* Stolen objects don't hold a ref, but do hold pin count. Fix that up |
4172 | * before progressing. */ | |
4173 | if (obj->stolen) | |
4174 | i915_gem_object_unpin_pages(obj); | |
4175 | ||
401c29f6 BW |
4176 | if (WARN_ON(obj->pages_pin_count)) |
4177 | obj->pages_pin_count = 0; | |
37e680a1 | 4178 | i915_gem_object_put_pages(obj); |
d8cb5086 | 4179 | i915_gem_object_free_mmap_offset(obj); |
0104fdbb | 4180 | i915_gem_object_release_stolen(obj); |
de151cf6 | 4181 | |
9da3da66 CW |
4182 | BUG_ON(obj->pages); |
4183 | ||
2f745ad3 CW |
4184 | if (obj->base.import_attach) |
4185 | drm_prime_gem_destroy(&obj->base, NULL); | |
de151cf6 | 4186 | |
05394f39 CW |
4187 | drm_gem_object_release(&obj->base); |
4188 | i915_gem_info_remove_obj(dev_priv, obj->base.size); | |
c397b908 | 4189 | |
05394f39 | 4190 | kfree(obj->bit_17); |
42dcedd4 | 4191 | i915_gem_object_free(obj); |
f65c9168 PZ |
4192 | |
4193 | intel_runtime_pm_put(dev_priv); | |
673a394b EA |
4194 | } |
4195 | ||
e656a6cb | 4196 | struct i915_vma *i915_gem_obj_to_vma(struct drm_i915_gem_object *obj, |
2f633156 | 4197 | struct i915_address_space *vm) |
e656a6cb DV |
4198 | { |
4199 | struct i915_vma *vma; | |
4200 | list_for_each_entry(vma, &obj->vma_list, vma_link) | |
4201 | if (vma->vm == vm) | |
4202 | return vma; | |
4203 | ||
4204 | return NULL; | |
4205 | } | |
4206 | ||
2f633156 BW |
4207 | void i915_gem_vma_destroy(struct i915_vma *vma) |
4208 | { | |
4209 | WARN_ON(vma->node.allocated); | |
aaa05667 CW |
4210 | |
4211 | /* Keep the vma as a placeholder in the execbuffer reservation lists */ | |
4212 | if (!list_empty(&vma->exec_list)) | |
4213 | return; | |
4214 | ||
8b9c2b94 | 4215 | list_del(&vma->vma_link); |
b93dab6e | 4216 | |
2f633156 BW |
4217 | kfree(vma); |
4218 | } | |
4219 | ||
29105ccc | 4220 | int |
45c5f202 | 4221 | i915_gem_suspend(struct drm_device *dev) |
29105ccc CW |
4222 | { |
4223 | drm_i915_private_t *dev_priv = dev->dev_private; | |
45c5f202 | 4224 | int ret = 0; |
28dfe52a | 4225 | |
45c5f202 | 4226 | mutex_lock(&dev->struct_mutex); |
f7403347 | 4227 | if (dev_priv->ums.mm_suspended) |
45c5f202 | 4228 | goto err; |
28dfe52a | 4229 | |
b2da9fe5 | 4230 | ret = i915_gpu_idle(dev); |
f7403347 | 4231 | if (ret) |
45c5f202 | 4232 | goto err; |
f7403347 | 4233 | |
b2da9fe5 | 4234 | i915_gem_retire_requests(dev); |
673a394b | 4235 | |
29105ccc | 4236 | /* Under UMS, be paranoid and evict. */ |
a39d7efc | 4237 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) |
6c085a72 | 4238 | i915_gem_evict_everything(dev); |
29105ccc | 4239 | |
29105ccc | 4240 | i915_kernel_lost_context(dev); |
6dbe2772 | 4241 | i915_gem_cleanup_ringbuffer(dev); |
29105ccc | 4242 | |
45c5f202 CW |
4243 | /* Hack! Don't let anybody do execbuf while we don't control the chip. |
4244 | * We need to replace this with a semaphore, or something. | |
4245 | * And not confound ums.mm_suspended! | |
4246 | */ | |
4247 | dev_priv->ums.mm_suspended = !drm_core_check_feature(dev, | |
4248 | DRIVER_MODESET); | |
4249 | mutex_unlock(&dev->struct_mutex); | |
4250 | ||
4251 | del_timer_sync(&dev_priv->gpu_error.hangcheck_timer); | |
29105ccc | 4252 | cancel_delayed_work_sync(&dev_priv->mm.retire_work); |
b29c19b6 | 4253 | cancel_delayed_work_sync(&dev_priv->mm.idle_work); |
29105ccc | 4254 | |
673a394b | 4255 | return 0; |
45c5f202 CW |
4256 | |
4257 | err: | |
4258 | mutex_unlock(&dev->struct_mutex); | |
4259 | return ret; | |
673a394b EA |
4260 | } |
4261 | ||
c3787e2e | 4262 | int i915_gem_l3_remap(struct intel_ring_buffer *ring, int slice) |
b9524a1e | 4263 | { |
c3787e2e | 4264 | struct drm_device *dev = ring->dev; |
b9524a1e | 4265 | drm_i915_private_t *dev_priv = dev->dev_private; |
35a85ac6 BW |
4266 | u32 reg_base = GEN7_L3LOG_BASE + (slice * 0x200); |
4267 | u32 *remap_info = dev_priv->l3_parity.remap_info[slice]; | |
c3787e2e | 4268 | int i, ret; |
b9524a1e | 4269 | |
040d2baa | 4270 | if (!HAS_L3_DPF(dev) || !remap_info) |
c3787e2e | 4271 | return 0; |
b9524a1e | 4272 | |
c3787e2e BW |
4273 | ret = intel_ring_begin(ring, GEN7_L3LOG_SIZE / 4 * 3); |
4274 | if (ret) | |
4275 | return ret; | |
b9524a1e | 4276 | |
c3787e2e BW |
4277 | /* |
4278 | * Note: We do not worry about the concurrent register cacheline hang | |
4279 | * here because no other code should access these registers other than | |
4280 | * at initialization time. | |
4281 | */ | |
b9524a1e | 4282 | for (i = 0; i < GEN7_L3LOG_SIZE; i += 4) { |
c3787e2e BW |
4283 | intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1)); |
4284 | intel_ring_emit(ring, reg_base + i); | |
4285 | intel_ring_emit(ring, remap_info[i/4]); | |
b9524a1e BW |
4286 | } |
4287 | ||
c3787e2e | 4288 | intel_ring_advance(ring); |
b9524a1e | 4289 | |
c3787e2e | 4290 | return ret; |
b9524a1e BW |
4291 | } |
4292 | ||
f691e2f4 DV |
4293 | void i915_gem_init_swizzling(struct drm_device *dev) |
4294 | { | |
4295 | drm_i915_private_t *dev_priv = dev->dev_private; | |
4296 | ||
11782b02 | 4297 | if (INTEL_INFO(dev)->gen < 5 || |
f691e2f4 DV |
4298 | dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE) |
4299 | return; | |
4300 | ||
4301 | I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) | | |
4302 | DISP_TILE_SURFACE_SWIZZLING); | |
4303 | ||
11782b02 DV |
4304 | if (IS_GEN5(dev)) |
4305 | return; | |
4306 | ||
f691e2f4 DV |
4307 | I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL); |
4308 | if (IS_GEN6(dev)) | |
6b26c86d | 4309 | I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB)); |
8782e26c | 4310 | else if (IS_GEN7(dev)) |
6b26c86d | 4311 | I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB)); |
31a5336e BW |
4312 | else if (IS_GEN8(dev)) |
4313 | I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW)); | |
8782e26c BW |
4314 | else |
4315 | BUG(); | |
f691e2f4 | 4316 | } |
e21af88d | 4317 | |
67b1b571 CW |
4318 | static bool |
4319 | intel_enable_blt(struct drm_device *dev) | |
4320 | { | |
4321 | if (!HAS_BLT(dev)) | |
4322 | return false; | |
4323 | ||
4324 | /* The blitter was dysfunctional on early prototypes */ | |
4325 | if (IS_GEN6(dev) && dev->pdev->revision < 8) { | |
4326 | DRM_INFO("BLT not supported on this pre-production hardware;" | |
4327 | " graphics performance will be degraded.\n"); | |
4328 | return false; | |
4329 | } | |
4330 | ||
4331 | return true; | |
4332 | } | |
4333 | ||
4fc7c971 | 4334 | static int i915_gem_init_rings(struct drm_device *dev) |
8187a2b7 | 4335 | { |
4fc7c971 | 4336 | struct drm_i915_private *dev_priv = dev->dev_private; |
8187a2b7 | 4337 | int ret; |
68f95ba9 | 4338 | |
5c1143bb | 4339 | ret = intel_init_render_ring_buffer(dev); |
68f95ba9 | 4340 | if (ret) |
b6913e4b | 4341 | return ret; |
68f95ba9 CW |
4342 | |
4343 | if (HAS_BSD(dev)) { | |
5c1143bb | 4344 | ret = intel_init_bsd_ring_buffer(dev); |
68f95ba9 CW |
4345 | if (ret) |
4346 | goto cleanup_render_ring; | |
d1b851fc | 4347 | } |
68f95ba9 | 4348 | |
67b1b571 | 4349 | if (intel_enable_blt(dev)) { |
549f7365 CW |
4350 | ret = intel_init_blt_ring_buffer(dev); |
4351 | if (ret) | |
4352 | goto cleanup_bsd_ring; | |
4353 | } | |
4354 | ||
9a8a2213 BW |
4355 | if (HAS_VEBOX(dev)) { |
4356 | ret = intel_init_vebox_ring_buffer(dev); | |
4357 | if (ret) | |
4358 | goto cleanup_blt_ring; | |
4359 | } | |
4360 | ||
4361 | ||
99433931 | 4362 | ret = i915_gem_set_seqno(dev, ((u32)~0 - 0x1000)); |
4fc7c971 | 4363 | if (ret) |
9a8a2213 | 4364 | goto cleanup_vebox_ring; |
4fc7c971 BW |
4365 | |
4366 | return 0; | |
4367 | ||
9a8a2213 BW |
4368 | cleanup_vebox_ring: |
4369 | intel_cleanup_ring_buffer(&dev_priv->ring[VECS]); | |
4fc7c971 BW |
4370 | cleanup_blt_ring: |
4371 | intel_cleanup_ring_buffer(&dev_priv->ring[BCS]); | |
4372 | cleanup_bsd_ring: | |
4373 | intel_cleanup_ring_buffer(&dev_priv->ring[VCS]); | |
4374 | cleanup_render_ring: | |
4375 | intel_cleanup_ring_buffer(&dev_priv->ring[RCS]); | |
4376 | ||
4377 | return ret; | |
4378 | } | |
4379 | ||
4380 | int | |
4381 | i915_gem_init_hw(struct drm_device *dev) | |
4382 | { | |
4383 | drm_i915_private_t *dev_priv = dev->dev_private; | |
35a85ac6 | 4384 | int ret, i; |
4fc7c971 BW |
4385 | |
4386 | if (INTEL_INFO(dev)->gen < 6 && !intel_enable_gtt()) | |
4387 | return -EIO; | |
4388 | ||
59124506 | 4389 | if (dev_priv->ellc_size) |
05e21cc4 | 4390 | I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf)); |
4fc7c971 | 4391 | |
0bf21347 VS |
4392 | if (IS_HASWELL(dev)) |
4393 | I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev) ? | |
4394 | LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED); | |
9435373e | 4395 | |
88a2b2a3 | 4396 | if (HAS_PCH_NOP(dev)) { |
6ba844b0 DV |
4397 | if (IS_IVYBRIDGE(dev)) { |
4398 | u32 temp = I915_READ(GEN7_MSG_CTL); | |
4399 | temp &= ~(WAIT_FOR_PCH_FLR_ACK | WAIT_FOR_PCH_RESET_ACK); | |
4400 | I915_WRITE(GEN7_MSG_CTL, temp); | |
4401 | } else if (INTEL_INFO(dev)->gen >= 7) { | |
4402 | u32 temp = I915_READ(HSW_NDE_RSTWRN_OPT); | |
4403 | temp &= ~RESET_PCH_HANDSHAKE_ENABLE; | |
4404 | I915_WRITE(HSW_NDE_RSTWRN_OPT, temp); | |
4405 | } | |
88a2b2a3 BW |
4406 | } |
4407 | ||
4fc7c971 BW |
4408 | i915_gem_init_swizzling(dev); |
4409 | ||
4410 | ret = i915_gem_init_rings(dev); | |
99433931 MK |
4411 | if (ret) |
4412 | return ret; | |
4413 | ||
c3787e2e BW |
4414 | for (i = 0; i < NUM_L3_SLICES(dev); i++) |
4415 | i915_gem_l3_remap(&dev_priv->ring[RCS], i); | |
4416 | ||
254f965c | 4417 | /* |
2fa48d8d BW |
4418 | * XXX: Contexts should only be initialized once. Doing a switch to the |
4419 | * default context switch however is something we'd like to do after | |
4420 | * reset or thaw (the latter may not actually be necessary for HW, but | |
4421 | * goes with our code better). Context switching requires rings (for | |
4422 | * the do_switch), but before enabling PPGTT. So don't move this. | |
254f965c | 4423 | */ |
2fa48d8d | 4424 | ret = i915_gem_context_enable(dev_priv); |
8245be31 | 4425 | if (ret) { |
2fa48d8d BW |
4426 | DRM_ERROR("Context enable failed %d\n", ret); |
4427 | goto err_out; | |
b7c36d25 | 4428 | } |
e21af88d | 4429 | |
68f95ba9 | 4430 | return 0; |
2fa48d8d BW |
4431 | |
4432 | err_out: | |
4433 | i915_gem_cleanup_ringbuffer(dev); | |
4434 | return ret; | |
8187a2b7 ZN |
4435 | } |
4436 | ||
1070a42b CW |
4437 | int i915_gem_init(struct drm_device *dev) |
4438 | { | |
4439 | struct drm_i915_private *dev_priv = dev->dev_private; | |
1070a42b CW |
4440 | int ret; |
4441 | ||
1070a42b | 4442 | mutex_lock(&dev->struct_mutex); |
d62b4892 JB |
4443 | |
4444 | if (IS_VALLEYVIEW(dev)) { | |
4445 | /* VLVA0 (potential hack), BIOS isn't actually waking us */ | |
4446 | I915_WRITE(VLV_GTLC_WAKE_CTRL, 1); | |
4447 | if (wait_for((I915_READ(VLV_GTLC_PW_STATUS) & 1) == 1, 10)) | |
4448 | DRM_DEBUG_DRIVER("allow wake ack timed out\n"); | |
4449 | } | |
4450 | ||
d7e5008f | 4451 | i915_gem_init_global_gtt(dev); |
d62b4892 | 4452 | |
2fa48d8d | 4453 | ret = i915_gem_context_init(dev); |
e3848694 MK |
4454 | if (ret) { |
4455 | mutex_unlock(&dev->struct_mutex); | |
2fa48d8d | 4456 | return ret; |
e3848694 | 4457 | } |
2fa48d8d | 4458 | |
1070a42b CW |
4459 | ret = i915_gem_init_hw(dev); |
4460 | mutex_unlock(&dev->struct_mutex); | |
4461 | if (ret) { | |
bdf4fd7e | 4462 | WARN_ON(dev_priv->mm.aliasing_ppgtt); |
2fa48d8d | 4463 | i915_gem_context_fini(dev); |
c39538a8 | 4464 | drm_mm_takedown(&dev_priv->gtt.base.mm); |
1070a42b CW |
4465 | return ret; |
4466 | } | |
4467 | ||
53ca26ca DV |
4468 | /* Allow hardware batchbuffers unless told otherwise, but not for KMS. */ |
4469 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) | |
4470 | dev_priv->dri1.allow_batchbuffer = 1; | |
1070a42b CW |
4471 | return 0; |
4472 | } | |
4473 | ||
8187a2b7 ZN |
4474 | void |
4475 | i915_gem_cleanup_ringbuffer(struct drm_device *dev) | |
4476 | { | |
4477 | drm_i915_private_t *dev_priv = dev->dev_private; | |
b4519513 | 4478 | struct intel_ring_buffer *ring; |
1ec14ad3 | 4479 | int i; |
8187a2b7 | 4480 | |
b4519513 CW |
4481 | for_each_ring(ring, dev_priv, i) |
4482 | intel_cleanup_ring_buffer(ring); | |
8187a2b7 ZN |
4483 | } |
4484 | ||
673a394b EA |
4485 | int |
4486 | i915_gem_entervt_ioctl(struct drm_device *dev, void *data, | |
4487 | struct drm_file *file_priv) | |
4488 | { | |
db1b76ca | 4489 | struct drm_i915_private *dev_priv = dev->dev_private; |
b4519513 | 4490 | int ret; |
673a394b | 4491 | |
79e53945 JB |
4492 | if (drm_core_check_feature(dev, DRIVER_MODESET)) |
4493 | return 0; | |
4494 | ||
1f83fee0 | 4495 | if (i915_reset_in_progress(&dev_priv->gpu_error)) { |
673a394b | 4496 | DRM_ERROR("Reenabling wedged hardware, good luck\n"); |
1f83fee0 | 4497 | atomic_set(&dev_priv->gpu_error.reset_counter, 0); |
673a394b EA |
4498 | } |
4499 | ||
673a394b | 4500 | mutex_lock(&dev->struct_mutex); |
db1b76ca | 4501 | dev_priv->ums.mm_suspended = 0; |
9bb2d6f9 | 4502 | |
f691e2f4 | 4503 | ret = i915_gem_init_hw(dev); |
d816f6ac WF |
4504 | if (ret != 0) { |
4505 | mutex_unlock(&dev->struct_mutex); | |
9bb2d6f9 | 4506 | return ret; |
d816f6ac | 4507 | } |
9bb2d6f9 | 4508 | |
5cef07e1 | 4509 | BUG_ON(!list_empty(&dev_priv->gtt.base.active_list)); |
673a394b | 4510 | mutex_unlock(&dev->struct_mutex); |
dbb19d30 | 4511 | |
5f35308b CW |
4512 | ret = drm_irq_install(dev); |
4513 | if (ret) | |
4514 | goto cleanup_ringbuffer; | |
dbb19d30 | 4515 | |
673a394b | 4516 | return 0; |
5f35308b CW |
4517 | |
4518 | cleanup_ringbuffer: | |
4519 | mutex_lock(&dev->struct_mutex); | |
4520 | i915_gem_cleanup_ringbuffer(dev); | |
db1b76ca | 4521 | dev_priv->ums.mm_suspended = 1; |
5f35308b CW |
4522 | mutex_unlock(&dev->struct_mutex); |
4523 | ||
4524 | return ret; | |
673a394b EA |
4525 | } |
4526 | ||
4527 | int | |
4528 | i915_gem_leavevt_ioctl(struct drm_device *dev, void *data, | |
4529 | struct drm_file *file_priv) | |
4530 | { | |
79e53945 JB |
4531 | if (drm_core_check_feature(dev, DRIVER_MODESET)) |
4532 | return 0; | |
4533 | ||
dbb19d30 | 4534 | drm_irq_uninstall(dev); |
db1b76ca | 4535 | |
45c5f202 | 4536 | return i915_gem_suspend(dev); |
673a394b EA |
4537 | } |
4538 | ||
4539 | void | |
4540 | i915_gem_lastclose(struct drm_device *dev) | |
4541 | { | |
4542 | int ret; | |
673a394b | 4543 | |
e806b495 EA |
4544 | if (drm_core_check_feature(dev, DRIVER_MODESET)) |
4545 | return; | |
4546 | ||
45c5f202 | 4547 | ret = i915_gem_suspend(dev); |
6dbe2772 KP |
4548 | if (ret) |
4549 | DRM_ERROR("failed to idle hardware: %d\n", ret); | |
673a394b EA |
4550 | } |
4551 | ||
64193406 CW |
4552 | static void |
4553 | init_ring_lists(struct intel_ring_buffer *ring) | |
4554 | { | |
4555 | INIT_LIST_HEAD(&ring->active_list); | |
4556 | INIT_LIST_HEAD(&ring->request_list); | |
64193406 CW |
4557 | } |
4558 | ||
7e0d96bc BW |
4559 | void i915_init_vm(struct drm_i915_private *dev_priv, |
4560 | struct i915_address_space *vm) | |
fc8c067e | 4561 | { |
7e0d96bc BW |
4562 | if (!i915_is_ggtt(vm)) |
4563 | drm_mm_init(&vm->mm, vm->start, vm->total); | |
fc8c067e BW |
4564 | vm->dev = dev_priv->dev; |
4565 | INIT_LIST_HEAD(&vm->active_list); | |
4566 | INIT_LIST_HEAD(&vm->inactive_list); | |
4567 | INIT_LIST_HEAD(&vm->global_link); | |
f72d21ed | 4568 | list_add_tail(&vm->global_link, &dev_priv->vm_list); |
fc8c067e BW |
4569 | } |
4570 | ||
673a394b EA |
4571 | void |
4572 | i915_gem_load(struct drm_device *dev) | |
4573 | { | |
4574 | drm_i915_private_t *dev_priv = dev->dev_private; | |
42dcedd4 CW |
4575 | int i; |
4576 | ||
4577 | dev_priv->slab = | |
4578 | kmem_cache_create("i915_gem_object", | |
4579 | sizeof(struct drm_i915_gem_object), 0, | |
4580 | SLAB_HWCACHE_ALIGN, | |
4581 | NULL); | |
673a394b | 4582 | |
fc8c067e BW |
4583 | INIT_LIST_HEAD(&dev_priv->vm_list); |
4584 | i915_init_vm(dev_priv, &dev_priv->gtt.base); | |
4585 | ||
a33afea5 | 4586 | INIT_LIST_HEAD(&dev_priv->context_list); |
6c085a72 CW |
4587 | INIT_LIST_HEAD(&dev_priv->mm.unbound_list); |
4588 | INIT_LIST_HEAD(&dev_priv->mm.bound_list); | |
a09ba7fa | 4589 | INIT_LIST_HEAD(&dev_priv->mm.fence_list); |
1ec14ad3 CW |
4590 | for (i = 0; i < I915_NUM_RINGS; i++) |
4591 | init_ring_lists(&dev_priv->ring[i]); | |
4b9de737 | 4592 | for (i = 0; i < I915_MAX_NUM_FENCES; i++) |
007cc8ac | 4593 | INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list); |
673a394b EA |
4594 | INIT_DELAYED_WORK(&dev_priv->mm.retire_work, |
4595 | i915_gem_retire_work_handler); | |
b29c19b6 CW |
4596 | INIT_DELAYED_WORK(&dev_priv->mm.idle_work, |
4597 | i915_gem_idle_work_handler); | |
1f83fee0 | 4598 | init_waitqueue_head(&dev_priv->gpu_error.reset_queue); |
31169714 | 4599 | |
94400120 DA |
4600 | /* On GEN3 we really need to make sure the ARB C3 LP bit is set */ |
4601 | if (IS_GEN3(dev)) { | |
50743298 DV |
4602 | I915_WRITE(MI_ARB_STATE, |
4603 | _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE)); | |
94400120 DA |
4604 | } |
4605 | ||
72bfa19c CW |
4606 | dev_priv->relative_constants_mode = I915_EXEC_CONSTANTS_REL_GENERAL; |
4607 | ||
de151cf6 | 4608 | /* Old X drivers will take 0-2 for front, back, depth buffers */ |
b397c836 EA |
4609 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) |
4610 | dev_priv->fence_reg_start = 3; | |
de151cf6 | 4611 | |
42b5aeab VS |
4612 | if (INTEL_INFO(dev)->gen >= 7 && !IS_VALLEYVIEW(dev)) |
4613 | dev_priv->num_fence_regs = 32; | |
4614 | else if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) | |
de151cf6 JB |
4615 | dev_priv->num_fence_regs = 16; |
4616 | else | |
4617 | dev_priv->num_fence_regs = 8; | |
4618 | ||
b5aa8a0f | 4619 | /* Initialize fence registers to zero */ |
19b2dbde CW |
4620 | INIT_LIST_HEAD(&dev_priv->mm.fence_list); |
4621 | i915_gem_restore_fences(dev); | |
10ed13e4 | 4622 | |
673a394b | 4623 | i915_gem_detect_bit_6_swizzle(dev); |
6b95a207 | 4624 | init_waitqueue_head(&dev_priv->pending_flip_queue); |
17250b71 | 4625 | |
ce453d81 CW |
4626 | dev_priv->mm.interruptible = true; |
4627 | ||
7dc19d5a DC |
4628 | dev_priv->mm.inactive_shrinker.scan_objects = i915_gem_inactive_scan; |
4629 | dev_priv->mm.inactive_shrinker.count_objects = i915_gem_inactive_count; | |
17250b71 CW |
4630 | dev_priv->mm.inactive_shrinker.seeks = DEFAULT_SEEKS; |
4631 | register_shrinker(&dev_priv->mm.inactive_shrinker); | |
673a394b | 4632 | } |
71acb5eb DA |
4633 | |
4634 | /* | |
4635 | * Create a physically contiguous memory object for this object | |
4636 | * e.g. for cursor + overlay regs | |
4637 | */ | |
995b6762 CW |
4638 | static int i915_gem_init_phys_object(struct drm_device *dev, |
4639 | int id, int size, int align) | |
71acb5eb DA |
4640 | { |
4641 | drm_i915_private_t *dev_priv = dev->dev_private; | |
4642 | struct drm_i915_gem_phys_object *phys_obj; | |
4643 | int ret; | |
4644 | ||
4645 | if (dev_priv->mm.phys_objs[id - 1] || !size) | |
4646 | return 0; | |
4647 | ||
b14c5679 | 4648 | phys_obj = kzalloc(sizeof(*phys_obj), GFP_KERNEL); |
71acb5eb DA |
4649 | if (!phys_obj) |
4650 | return -ENOMEM; | |
4651 | ||
4652 | phys_obj->id = id; | |
4653 | ||
6eeefaf3 | 4654 | phys_obj->handle = drm_pci_alloc(dev, size, align); |
71acb5eb DA |
4655 | if (!phys_obj->handle) { |
4656 | ret = -ENOMEM; | |
4657 | goto kfree_obj; | |
4658 | } | |
4659 | #ifdef CONFIG_X86 | |
4660 | set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE); | |
4661 | #endif | |
4662 | ||
4663 | dev_priv->mm.phys_objs[id - 1] = phys_obj; | |
4664 | ||
4665 | return 0; | |
4666 | kfree_obj: | |
9a298b2a | 4667 | kfree(phys_obj); |
71acb5eb DA |
4668 | return ret; |
4669 | } | |
4670 | ||
995b6762 | 4671 | static void i915_gem_free_phys_object(struct drm_device *dev, int id) |
71acb5eb DA |
4672 | { |
4673 | drm_i915_private_t *dev_priv = dev->dev_private; | |
4674 | struct drm_i915_gem_phys_object *phys_obj; | |
4675 | ||
4676 | if (!dev_priv->mm.phys_objs[id - 1]) | |
4677 | return; | |
4678 | ||
4679 | phys_obj = dev_priv->mm.phys_objs[id - 1]; | |
4680 | if (phys_obj->cur_obj) { | |
4681 | i915_gem_detach_phys_object(dev, phys_obj->cur_obj); | |
4682 | } | |
4683 | ||
4684 | #ifdef CONFIG_X86 | |
4685 | set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE); | |
4686 | #endif | |
4687 | drm_pci_free(dev, phys_obj->handle); | |
4688 | kfree(phys_obj); | |
4689 | dev_priv->mm.phys_objs[id - 1] = NULL; | |
4690 | } | |
4691 | ||
4692 | void i915_gem_free_all_phys_object(struct drm_device *dev) | |
4693 | { | |
4694 | int i; | |
4695 | ||
260883c8 | 4696 | for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++) |
71acb5eb DA |
4697 | i915_gem_free_phys_object(dev, i); |
4698 | } | |
4699 | ||
4700 | void i915_gem_detach_phys_object(struct drm_device *dev, | |
05394f39 | 4701 | struct drm_i915_gem_object *obj) |
71acb5eb | 4702 | { |
496ad9aa | 4703 | struct address_space *mapping = file_inode(obj->base.filp)->i_mapping; |
e5281ccd | 4704 | char *vaddr; |
71acb5eb | 4705 | int i; |
71acb5eb DA |
4706 | int page_count; |
4707 | ||
05394f39 | 4708 | if (!obj->phys_obj) |
71acb5eb | 4709 | return; |
05394f39 | 4710 | vaddr = obj->phys_obj->handle->vaddr; |
71acb5eb | 4711 | |
05394f39 | 4712 | page_count = obj->base.size / PAGE_SIZE; |
71acb5eb | 4713 | for (i = 0; i < page_count; i++) { |
5949eac4 | 4714 | struct page *page = shmem_read_mapping_page(mapping, i); |
e5281ccd CW |
4715 | if (!IS_ERR(page)) { |
4716 | char *dst = kmap_atomic(page); | |
4717 | memcpy(dst, vaddr + i*PAGE_SIZE, PAGE_SIZE); | |
4718 | kunmap_atomic(dst); | |
4719 | ||
4720 | drm_clflush_pages(&page, 1); | |
4721 | ||
4722 | set_page_dirty(page); | |
4723 | mark_page_accessed(page); | |
4724 | page_cache_release(page); | |
4725 | } | |
71acb5eb | 4726 | } |
e76e9aeb | 4727 | i915_gem_chipset_flush(dev); |
d78b47b9 | 4728 | |
05394f39 CW |
4729 | obj->phys_obj->cur_obj = NULL; |
4730 | obj->phys_obj = NULL; | |
71acb5eb DA |
4731 | } |
4732 | ||
4733 | int | |
4734 | i915_gem_attach_phys_object(struct drm_device *dev, | |
05394f39 | 4735 | struct drm_i915_gem_object *obj, |
6eeefaf3 CW |
4736 | int id, |
4737 | int align) | |
71acb5eb | 4738 | { |
496ad9aa | 4739 | struct address_space *mapping = file_inode(obj->base.filp)->i_mapping; |
71acb5eb | 4740 | drm_i915_private_t *dev_priv = dev->dev_private; |
71acb5eb DA |
4741 | int ret = 0; |
4742 | int page_count; | |
4743 | int i; | |
4744 | ||
4745 | if (id > I915_MAX_PHYS_OBJECT) | |
4746 | return -EINVAL; | |
4747 | ||
05394f39 CW |
4748 | if (obj->phys_obj) { |
4749 | if (obj->phys_obj->id == id) | |
71acb5eb DA |
4750 | return 0; |
4751 | i915_gem_detach_phys_object(dev, obj); | |
4752 | } | |
4753 | ||
71acb5eb DA |
4754 | /* create a new object */ |
4755 | if (!dev_priv->mm.phys_objs[id - 1]) { | |
4756 | ret = i915_gem_init_phys_object(dev, id, | |
05394f39 | 4757 | obj->base.size, align); |
71acb5eb | 4758 | if (ret) { |
05394f39 CW |
4759 | DRM_ERROR("failed to init phys object %d size: %zu\n", |
4760 | id, obj->base.size); | |
e5281ccd | 4761 | return ret; |
71acb5eb DA |
4762 | } |
4763 | } | |
4764 | ||
4765 | /* bind to the object */ | |
05394f39 CW |
4766 | obj->phys_obj = dev_priv->mm.phys_objs[id - 1]; |
4767 | obj->phys_obj->cur_obj = obj; | |
71acb5eb | 4768 | |
05394f39 | 4769 | page_count = obj->base.size / PAGE_SIZE; |
71acb5eb DA |
4770 | |
4771 | for (i = 0; i < page_count; i++) { | |
e5281ccd CW |
4772 | struct page *page; |
4773 | char *dst, *src; | |
4774 | ||
5949eac4 | 4775 | page = shmem_read_mapping_page(mapping, i); |
e5281ccd CW |
4776 | if (IS_ERR(page)) |
4777 | return PTR_ERR(page); | |
71acb5eb | 4778 | |
ff75b9bc | 4779 | src = kmap_atomic(page); |
05394f39 | 4780 | dst = obj->phys_obj->handle->vaddr + (i * PAGE_SIZE); |
71acb5eb | 4781 | memcpy(dst, src, PAGE_SIZE); |
3e4d3af5 | 4782 | kunmap_atomic(src); |
71acb5eb | 4783 | |
e5281ccd CW |
4784 | mark_page_accessed(page); |
4785 | page_cache_release(page); | |
4786 | } | |
d78b47b9 | 4787 | |
71acb5eb | 4788 | return 0; |
71acb5eb DA |
4789 | } |
4790 | ||
4791 | static int | |
05394f39 CW |
4792 | i915_gem_phys_pwrite(struct drm_device *dev, |
4793 | struct drm_i915_gem_object *obj, | |
71acb5eb DA |
4794 | struct drm_i915_gem_pwrite *args, |
4795 | struct drm_file *file_priv) | |
4796 | { | |
05394f39 | 4797 | void *vaddr = obj->phys_obj->handle->vaddr + args->offset; |
2bb4629a | 4798 | char __user *user_data = to_user_ptr(args->data_ptr); |
71acb5eb | 4799 | |
b47b30cc CW |
4800 | if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) { |
4801 | unsigned long unwritten; | |
4802 | ||
4803 | /* The physical object once assigned is fixed for the lifetime | |
4804 | * of the obj, so we can safely drop the lock and continue | |
4805 | * to access vaddr. | |
4806 | */ | |
4807 | mutex_unlock(&dev->struct_mutex); | |
4808 | unwritten = copy_from_user(vaddr, user_data, args->size); | |
4809 | mutex_lock(&dev->struct_mutex); | |
4810 | if (unwritten) | |
4811 | return -EFAULT; | |
4812 | } | |
71acb5eb | 4813 | |
e76e9aeb | 4814 | i915_gem_chipset_flush(dev); |
71acb5eb DA |
4815 | return 0; |
4816 | } | |
b962442e | 4817 | |
f787a5f5 | 4818 | void i915_gem_release(struct drm_device *dev, struct drm_file *file) |
b962442e | 4819 | { |
f787a5f5 | 4820 | struct drm_i915_file_private *file_priv = file->driver_priv; |
b962442e | 4821 | |
b29c19b6 CW |
4822 | cancel_delayed_work_sync(&file_priv->mm.idle_work); |
4823 | ||
b962442e EA |
4824 | /* Clean up our request list when the client is going away, so that |
4825 | * later retire_requests won't dereference our soon-to-be-gone | |
4826 | * file_priv. | |
4827 | */ | |
1c25595f | 4828 | spin_lock(&file_priv->mm.lock); |
f787a5f5 CW |
4829 | while (!list_empty(&file_priv->mm.request_list)) { |
4830 | struct drm_i915_gem_request *request; | |
4831 | ||
4832 | request = list_first_entry(&file_priv->mm.request_list, | |
4833 | struct drm_i915_gem_request, | |
4834 | client_list); | |
4835 | list_del(&request->client_list); | |
4836 | request->file_priv = NULL; | |
4837 | } | |
1c25595f | 4838 | spin_unlock(&file_priv->mm.lock); |
b962442e | 4839 | } |
31169714 | 4840 | |
b29c19b6 CW |
4841 | static void |
4842 | i915_gem_file_idle_work_handler(struct work_struct *work) | |
4843 | { | |
4844 | struct drm_i915_file_private *file_priv = | |
4845 | container_of(work, typeof(*file_priv), mm.idle_work.work); | |
4846 | ||
4847 | atomic_set(&file_priv->rps_wait_boost, false); | |
4848 | } | |
4849 | ||
4850 | int i915_gem_open(struct drm_device *dev, struct drm_file *file) | |
4851 | { | |
4852 | struct drm_i915_file_private *file_priv; | |
e422b888 | 4853 | int ret; |
b29c19b6 CW |
4854 | |
4855 | DRM_DEBUG_DRIVER("\n"); | |
4856 | ||
4857 | file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL); | |
4858 | if (!file_priv) | |
4859 | return -ENOMEM; | |
4860 | ||
4861 | file->driver_priv = file_priv; | |
4862 | file_priv->dev_priv = dev->dev_private; | |
4863 | ||
4864 | spin_lock_init(&file_priv->mm.lock); | |
4865 | INIT_LIST_HEAD(&file_priv->mm.request_list); | |
4866 | INIT_DELAYED_WORK(&file_priv->mm.idle_work, | |
4867 | i915_gem_file_idle_work_handler); | |
4868 | ||
e422b888 BW |
4869 | ret = i915_gem_context_open(dev, file); |
4870 | if (ret) | |
4871 | kfree(file_priv); | |
b29c19b6 | 4872 | |
e422b888 | 4873 | return ret; |
b29c19b6 CW |
4874 | } |
4875 | ||
5774506f CW |
4876 | static bool mutex_is_locked_by(struct mutex *mutex, struct task_struct *task) |
4877 | { | |
4878 | if (!mutex_is_locked(mutex)) | |
4879 | return false; | |
4880 | ||
4881 | #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_MUTEXES) | |
4882 | return mutex->owner == task; | |
4883 | #else | |
4884 | /* Since UP may be pre-empted, we cannot assume that we own the lock */ | |
4885 | return false; | |
4886 | #endif | |
4887 | } | |
4888 | ||
7dc19d5a DC |
4889 | static unsigned long |
4890 | i915_gem_inactive_count(struct shrinker *shrinker, struct shrink_control *sc) | |
31169714 | 4891 | { |
17250b71 CW |
4892 | struct drm_i915_private *dev_priv = |
4893 | container_of(shrinker, | |
4894 | struct drm_i915_private, | |
4895 | mm.inactive_shrinker); | |
4896 | struct drm_device *dev = dev_priv->dev; | |
6c085a72 | 4897 | struct drm_i915_gem_object *obj; |
5774506f | 4898 | bool unlock = true; |
7dc19d5a | 4899 | unsigned long count; |
17250b71 | 4900 | |
5774506f CW |
4901 | if (!mutex_trylock(&dev->struct_mutex)) { |
4902 | if (!mutex_is_locked_by(&dev->struct_mutex, current)) | |
d3227046 | 4903 | return 0; |
5774506f | 4904 | |
677feac2 | 4905 | if (dev_priv->mm.shrinker_no_lock_stealing) |
d3227046 | 4906 | return 0; |
677feac2 | 4907 | |
5774506f CW |
4908 | unlock = false; |
4909 | } | |
31169714 | 4910 | |
7dc19d5a | 4911 | count = 0; |
35c20a60 | 4912 | list_for_each_entry(obj, &dev_priv->mm.unbound_list, global_list) |
a5570178 | 4913 | if (obj->pages_pin_count == 0) |
7dc19d5a | 4914 | count += obj->base.size >> PAGE_SHIFT; |
fcb4a578 BW |
4915 | |
4916 | list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) { | |
4917 | if (obj->active) | |
4918 | continue; | |
4919 | ||
d7f46fc4 | 4920 | if (!i915_gem_obj_is_pinned(obj) && obj->pages_pin_count == 0) |
7dc19d5a | 4921 | count += obj->base.size >> PAGE_SHIFT; |
fcb4a578 | 4922 | } |
17250b71 | 4923 | |
5774506f CW |
4924 | if (unlock) |
4925 | mutex_unlock(&dev->struct_mutex); | |
d9973b43 | 4926 | |
7dc19d5a | 4927 | return count; |
31169714 | 4928 | } |
a70a3148 BW |
4929 | |
4930 | /* All the new VM stuff */ | |
4931 | unsigned long i915_gem_obj_offset(struct drm_i915_gem_object *o, | |
4932 | struct i915_address_space *vm) | |
4933 | { | |
4934 | struct drm_i915_private *dev_priv = o->base.dev->dev_private; | |
4935 | struct i915_vma *vma; | |
4936 | ||
6f425321 BW |
4937 | if (!dev_priv->mm.aliasing_ppgtt || |
4938 | vm == &dev_priv->mm.aliasing_ppgtt->base) | |
a70a3148 BW |
4939 | vm = &dev_priv->gtt.base; |
4940 | ||
4941 | BUG_ON(list_empty(&o->vma_list)); | |
4942 | list_for_each_entry(vma, &o->vma_list, vma_link) { | |
4943 | if (vma->vm == vm) | |
4944 | return vma->node.start; | |
4945 | ||
4946 | } | |
4947 | return -1; | |
4948 | } | |
4949 | ||
4950 | bool i915_gem_obj_bound(struct drm_i915_gem_object *o, | |
4951 | struct i915_address_space *vm) | |
4952 | { | |
4953 | struct i915_vma *vma; | |
4954 | ||
4955 | list_for_each_entry(vma, &o->vma_list, vma_link) | |
8b9c2b94 | 4956 | if (vma->vm == vm && drm_mm_node_allocated(&vma->node)) |
a70a3148 BW |
4957 | return true; |
4958 | ||
4959 | return false; | |
4960 | } | |
4961 | ||
4962 | bool i915_gem_obj_bound_any(struct drm_i915_gem_object *o) | |
4963 | { | |
5a1d5eb0 | 4964 | struct i915_vma *vma; |
a70a3148 | 4965 | |
5a1d5eb0 CW |
4966 | list_for_each_entry(vma, &o->vma_list, vma_link) |
4967 | if (drm_mm_node_allocated(&vma->node)) | |
a70a3148 BW |
4968 | return true; |
4969 | ||
4970 | return false; | |
4971 | } | |
4972 | ||
4973 | unsigned long i915_gem_obj_size(struct drm_i915_gem_object *o, | |
4974 | struct i915_address_space *vm) | |
4975 | { | |
4976 | struct drm_i915_private *dev_priv = o->base.dev->dev_private; | |
4977 | struct i915_vma *vma; | |
4978 | ||
6f425321 BW |
4979 | if (!dev_priv->mm.aliasing_ppgtt || |
4980 | vm == &dev_priv->mm.aliasing_ppgtt->base) | |
a70a3148 BW |
4981 | vm = &dev_priv->gtt.base; |
4982 | ||
4983 | BUG_ON(list_empty(&o->vma_list)); | |
4984 | ||
4985 | list_for_each_entry(vma, &o->vma_list, vma_link) | |
4986 | if (vma->vm == vm) | |
4987 | return vma->node.size; | |
4988 | ||
4989 | return 0; | |
4990 | } | |
4991 | ||
7dc19d5a DC |
4992 | static unsigned long |
4993 | i915_gem_inactive_scan(struct shrinker *shrinker, struct shrink_control *sc) | |
4994 | { | |
4995 | struct drm_i915_private *dev_priv = | |
4996 | container_of(shrinker, | |
4997 | struct drm_i915_private, | |
4998 | mm.inactive_shrinker); | |
4999 | struct drm_device *dev = dev_priv->dev; | |
7dc19d5a DC |
5000 | unsigned long freed; |
5001 | bool unlock = true; | |
5002 | ||
5003 | if (!mutex_trylock(&dev->struct_mutex)) { | |
5004 | if (!mutex_is_locked_by(&dev->struct_mutex, current)) | |
d3227046 | 5005 | return SHRINK_STOP; |
7dc19d5a DC |
5006 | |
5007 | if (dev_priv->mm.shrinker_no_lock_stealing) | |
d3227046 | 5008 | return SHRINK_STOP; |
7dc19d5a DC |
5009 | |
5010 | unlock = false; | |
5011 | } | |
5012 | ||
d9973b43 CW |
5013 | freed = i915_gem_purge(dev_priv, sc->nr_to_scan); |
5014 | if (freed < sc->nr_to_scan) | |
5015 | freed += __i915_gem_shrink(dev_priv, | |
5016 | sc->nr_to_scan - freed, | |
5017 | false); | |
5018 | if (freed < sc->nr_to_scan) | |
7dc19d5a DC |
5019 | freed += i915_gem_shrink_all(dev_priv); |
5020 | ||
5021 | if (unlock) | |
5022 | mutex_unlock(&dev->struct_mutex); | |
d9973b43 | 5023 | |
7dc19d5a DC |
5024 | return freed; |
5025 | } | |
5c2abbea BW |
5026 | |
5027 | struct i915_vma *i915_gem_obj_to_ggtt(struct drm_i915_gem_object *obj) | |
5028 | { | |
5029 | struct i915_vma *vma; | |
5030 | ||
5031 | if (WARN_ON(list_empty(&obj->vma_list))) | |
5032 | return NULL; | |
5033 | ||
5034 | vma = list_first_entry(&obj->vma_list, typeof(*vma), vma_link); | |
6e164c33 | 5035 | if (vma->vm != obj_to_ggtt(obj)) |
5c2abbea BW |
5036 | return NULL; |
5037 | ||
5038 | return vma; | |
5039 | } |