]>
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 | ||
28 | #include "drmP.h" | |
29 | #include "drm.h" | |
30 | #include "i915_drm.h" | |
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 CW |
40 | static void i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj); |
41 | static void i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj); | |
88241785 CW |
42 | static __must_check int i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj, |
43 | unsigned alignment, | |
44 | bool map_and_fenceable); | |
05394f39 CW |
45 | static int i915_gem_phys_pwrite(struct drm_device *dev, |
46 | struct drm_i915_gem_object *obj, | |
71acb5eb | 47 | struct drm_i915_gem_pwrite *args, |
05394f39 | 48 | struct drm_file *file); |
673a394b | 49 | |
61050808 CW |
50 | static void i915_gem_write_fence(struct drm_device *dev, int reg, |
51 | struct drm_i915_gem_object *obj); | |
52 | static void i915_gem_object_update_fence(struct drm_i915_gem_object *obj, | |
53 | struct drm_i915_fence_reg *fence, | |
54 | bool enable); | |
55 | ||
17250b71 | 56 | static int i915_gem_inactive_shrink(struct shrinker *shrinker, |
1495f230 | 57 | struct shrink_control *sc); |
6c085a72 CW |
58 | static long i915_gem_purge(struct drm_i915_private *dev_priv, long target); |
59 | static void i915_gem_shrink_all(struct drm_i915_private *dev_priv); | |
8c59967c | 60 | static void i915_gem_object_truncate(struct drm_i915_gem_object *obj); |
31169714 | 61 | |
61050808 CW |
62 | static inline void i915_gem_object_fence_lost(struct drm_i915_gem_object *obj) |
63 | { | |
64 | if (obj->tiling_mode) | |
65 | i915_gem_release_mmap(obj); | |
66 | ||
67 | /* As we do not have an associated fence register, we will force | |
68 | * a tiling change if we ever need to acquire one. | |
69 | */ | |
5d82e3e6 | 70 | obj->fence_dirty = false; |
61050808 CW |
71 | obj->fence_reg = I915_FENCE_REG_NONE; |
72 | } | |
73 | ||
73aa808f CW |
74 | /* some bookkeeping */ |
75 | static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv, | |
76 | size_t size) | |
77 | { | |
78 | dev_priv->mm.object_count++; | |
79 | dev_priv->mm.object_memory += size; | |
80 | } | |
81 | ||
82 | static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv, | |
83 | size_t size) | |
84 | { | |
85 | dev_priv->mm.object_count--; | |
86 | dev_priv->mm.object_memory -= size; | |
87 | } | |
88 | ||
21dd3734 CW |
89 | static int |
90 | i915_gem_wait_for_error(struct drm_device *dev) | |
30dbf0c0 CW |
91 | { |
92 | struct drm_i915_private *dev_priv = dev->dev_private; | |
93 | struct completion *x = &dev_priv->error_completion; | |
94 | unsigned long flags; | |
95 | int ret; | |
96 | ||
97 | if (!atomic_read(&dev_priv->mm.wedged)) | |
98 | return 0; | |
99 | ||
0a6759c6 DV |
100 | /* |
101 | * Only wait 10 seconds for the gpu reset to complete to avoid hanging | |
102 | * userspace. If it takes that long something really bad is going on and | |
103 | * we should simply try to bail out and fail as gracefully as possible. | |
104 | */ | |
105 | ret = wait_for_completion_interruptible_timeout(x, 10*HZ); | |
106 | if (ret == 0) { | |
107 | DRM_ERROR("Timed out waiting for the gpu reset to complete\n"); | |
108 | return -EIO; | |
109 | } else if (ret < 0) { | |
30dbf0c0 | 110 | return ret; |
0a6759c6 | 111 | } |
30dbf0c0 | 112 | |
21dd3734 CW |
113 | if (atomic_read(&dev_priv->mm.wedged)) { |
114 | /* GPU is hung, bump the completion count to account for | |
115 | * the token we just consumed so that we never hit zero and | |
116 | * end up waiting upon a subsequent completion event that | |
117 | * will never happen. | |
118 | */ | |
119 | spin_lock_irqsave(&x->wait.lock, flags); | |
120 | x->done++; | |
121 | spin_unlock_irqrestore(&x->wait.lock, flags); | |
122 | } | |
123 | return 0; | |
30dbf0c0 CW |
124 | } |
125 | ||
54cf91dc | 126 | int i915_mutex_lock_interruptible(struct drm_device *dev) |
76c1dec1 | 127 | { |
76c1dec1 CW |
128 | int ret; |
129 | ||
21dd3734 | 130 | ret = i915_gem_wait_for_error(dev); |
76c1dec1 CW |
131 | if (ret) |
132 | return ret; | |
133 | ||
134 | ret = mutex_lock_interruptible(&dev->struct_mutex); | |
135 | if (ret) | |
136 | return ret; | |
137 | ||
23bc5982 | 138 | WARN_ON(i915_verify_lists(dev)); |
76c1dec1 CW |
139 | return 0; |
140 | } | |
30dbf0c0 | 141 | |
7d1c4804 | 142 | static inline bool |
05394f39 | 143 | i915_gem_object_is_inactive(struct drm_i915_gem_object *obj) |
7d1c4804 | 144 | { |
6c085a72 | 145 | return obj->gtt_space && !obj->active; |
7d1c4804 CW |
146 | } |
147 | ||
79e53945 JB |
148 | int |
149 | i915_gem_init_ioctl(struct drm_device *dev, void *data, | |
05394f39 | 150 | struct drm_file *file) |
79e53945 JB |
151 | { |
152 | struct drm_i915_gem_init *args = data; | |
2021746e | 153 | |
7bb6fb8d DV |
154 | if (drm_core_check_feature(dev, DRIVER_MODESET)) |
155 | return -ENODEV; | |
156 | ||
2021746e CW |
157 | if (args->gtt_start >= args->gtt_end || |
158 | (args->gtt_end | args->gtt_start) & (PAGE_SIZE - 1)) | |
159 | return -EINVAL; | |
79e53945 | 160 | |
f534bc0b DV |
161 | /* GEM with user mode setting was never supported on ilk and later. */ |
162 | if (INTEL_INFO(dev)->gen >= 5) | |
163 | return -ENODEV; | |
164 | ||
79e53945 | 165 | mutex_lock(&dev->struct_mutex); |
644ec02b DV |
166 | i915_gem_init_global_gtt(dev, args->gtt_start, |
167 | args->gtt_end, args->gtt_end); | |
673a394b EA |
168 | mutex_unlock(&dev->struct_mutex); |
169 | ||
2021746e | 170 | return 0; |
673a394b EA |
171 | } |
172 | ||
5a125c3c EA |
173 | int |
174 | i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data, | |
05394f39 | 175 | struct drm_file *file) |
5a125c3c | 176 | { |
73aa808f | 177 | struct drm_i915_private *dev_priv = dev->dev_private; |
5a125c3c | 178 | struct drm_i915_gem_get_aperture *args = data; |
6299f992 CW |
179 | struct drm_i915_gem_object *obj; |
180 | size_t pinned; | |
5a125c3c | 181 | |
6299f992 | 182 | pinned = 0; |
73aa808f | 183 | mutex_lock(&dev->struct_mutex); |
6c085a72 | 184 | list_for_each_entry(obj, &dev_priv->mm.bound_list, gtt_list) |
1b50247a CW |
185 | if (obj->pin_count) |
186 | pinned += obj->gtt_space->size; | |
73aa808f | 187 | mutex_unlock(&dev->struct_mutex); |
5a125c3c | 188 | |
6299f992 | 189 | args->aper_size = dev_priv->mm.gtt_total; |
0206e353 | 190 | args->aper_available_size = args->aper_size - pinned; |
6299f992 | 191 | |
5a125c3c EA |
192 | return 0; |
193 | } | |
194 | ||
ff72145b DA |
195 | static int |
196 | i915_gem_create(struct drm_file *file, | |
197 | struct drm_device *dev, | |
198 | uint64_t size, | |
199 | uint32_t *handle_p) | |
673a394b | 200 | { |
05394f39 | 201 | struct drm_i915_gem_object *obj; |
a1a2d1d3 PP |
202 | int ret; |
203 | u32 handle; | |
673a394b | 204 | |
ff72145b | 205 | size = roundup(size, PAGE_SIZE); |
8ffc0246 CW |
206 | if (size == 0) |
207 | return -EINVAL; | |
673a394b EA |
208 | |
209 | /* Allocate the new object */ | |
ff72145b | 210 | obj = i915_gem_alloc_object(dev, size); |
673a394b EA |
211 | if (obj == NULL) |
212 | return -ENOMEM; | |
213 | ||
05394f39 | 214 | ret = drm_gem_handle_create(file, &obj->base, &handle); |
1dfd9754 | 215 | if (ret) { |
05394f39 CW |
216 | drm_gem_object_release(&obj->base); |
217 | i915_gem_info_remove_obj(dev->dev_private, obj->base.size); | |
202f2fef | 218 | kfree(obj); |
673a394b | 219 | return ret; |
1dfd9754 | 220 | } |
673a394b | 221 | |
202f2fef | 222 | /* drop reference from allocate - handle holds it now */ |
05394f39 | 223 | drm_gem_object_unreference(&obj->base); |
202f2fef CW |
224 | trace_i915_gem_object_create(obj); |
225 | ||
ff72145b | 226 | *handle_p = handle; |
673a394b EA |
227 | return 0; |
228 | } | |
229 | ||
ff72145b DA |
230 | int |
231 | i915_gem_dumb_create(struct drm_file *file, | |
232 | struct drm_device *dev, | |
233 | struct drm_mode_create_dumb *args) | |
234 | { | |
235 | /* have to work out size/pitch and return them */ | |
ed0291fd | 236 | args->pitch = ALIGN(args->width * ((args->bpp + 7) / 8), 64); |
ff72145b DA |
237 | args->size = args->pitch * args->height; |
238 | return i915_gem_create(file, dev, | |
239 | args->size, &args->handle); | |
240 | } | |
241 | ||
242 | int i915_gem_dumb_destroy(struct drm_file *file, | |
243 | struct drm_device *dev, | |
244 | uint32_t handle) | |
245 | { | |
246 | return drm_gem_handle_delete(file, handle); | |
247 | } | |
248 | ||
249 | /** | |
250 | * Creates a new mm object and returns a handle to it. | |
251 | */ | |
252 | int | |
253 | i915_gem_create_ioctl(struct drm_device *dev, void *data, | |
254 | struct drm_file *file) | |
255 | { | |
256 | struct drm_i915_gem_create *args = data; | |
63ed2cb2 | 257 | |
ff72145b DA |
258 | return i915_gem_create(file, dev, |
259 | args->size, &args->handle); | |
260 | } | |
261 | ||
05394f39 | 262 | static int i915_gem_object_needs_bit17_swizzle(struct drm_i915_gem_object *obj) |
280b713b | 263 | { |
05394f39 | 264 | drm_i915_private_t *dev_priv = obj->base.dev->dev_private; |
280b713b EA |
265 | |
266 | return dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_9_10_17 && | |
05394f39 | 267 | obj->tiling_mode != I915_TILING_NONE; |
280b713b EA |
268 | } |
269 | ||
8461d226 DV |
270 | static inline int |
271 | __copy_to_user_swizzled(char __user *cpu_vaddr, | |
272 | const char *gpu_vaddr, int gpu_offset, | |
273 | int length) | |
274 | { | |
275 | int ret, cpu_offset = 0; | |
276 | ||
277 | while (length > 0) { | |
278 | int cacheline_end = ALIGN(gpu_offset + 1, 64); | |
279 | int this_length = min(cacheline_end - gpu_offset, length); | |
280 | int swizzled_gpu_offset = gpu_offset ^ 64; | |
281 | ||
282 | ret = __copy_to_user(cpu_vaddr + cpu_offset, | |
283 | gpu_vaddr + swizzled_gpu_offset, | |
284 | this_length); | |
285 | if (ret) | |
286 | return ret + length; | |
287 | ||
288 | cpu_offset += this_length; | |
289 | gpu_offset += this_length; | |
290 | length -= this_length; | |
291 | } | |
292 | ||
293 | return 0; | |
294 | } | |
295 | ||
8c59967c | 296 | static inline int |
4f0c7cfb BW |
297 | __copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset, |
298 | const char __user *cpu_vaddr, | |
8c59967c DV |
299 | int length) |
300 | { | |
301 | int ret, cpu_offset = 0; | |
302 | ||
303 | while (length > 0) { | |
304 | int cacheline_end = ALIGN(gpu_offset + 1, 64); | |
305 | int this_length = min(cacheline_end - gpu_offset, length); | |
306 | int swizzled_gpu_offset = gpu_offset ^ 64; | |
307 | ||
308 | ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset, | |
309 | cpu_vaddr + cpu_offset, | |
310 | this_length); | |
311 | if (ret) | |
312 | return ret + length; | |
313 | ||
314 | cpu_offset += this_length; | |
315 | gpu_offset += this_length; | |
316 | length -= this_length; | |
317 | } | |
318 | ||
319 | return 0; | |
320 | } | |
321 | ||
d174bd64 DV |
322 | /* Per-page copy function for the shmem pread fastpath. |
323 | * Flushes invalid cachelines before reading the target if | |
324 | * needs_clflush is set. */ | |
eb01459f | 325 | static int |
d174bd64 DV |
326 | shmem_pread_fast(struct page *page, int shmem_page_offset, int page_length, |
327 | char __user *user_data, | |
328 | bool page_do_bit17_swizzling, bool needs_clflush) | |
329 | { | |
330 | char *vaddr; | |
331 | int ret; | |
332 | ||
e7e58eb5 | 333 | if (unlikely(page_do_bit17_swizzling)) |
d174bd64 DV |
334 | return -EINVAL; |
335 | ||
336 | vaddr = kmap_atomic(page); | |
337 | if (needs_clflush) | |
338 | drm_clflush_virt_range(vaddr + shmem_page_offset, | |
339 | page_length); | |
340 | ret = __copy_to_user_inatomic(user_data, | |
341 | vaddr + shmem_page_offset, | |
342 | page_length); | |
343 | kunmap_atomic(vaddr); | |
344 | ||
345 | return ret; | |
346 | } | |
347 | ||
23c18c71 DV |
348 | static void |
349 | shmem_clflush_swizzled_range(char *addr, unsigned long length, | |
350 | bool swizzled) | |
351 | { | |
e7e58eb5 | 352 | if (unlikely(swizzled)) { |
23c18c71 DV |
353 | unsigned long start = (unsigned long) addr; |
354 | unsigned long end = (unsigned long) addr + length; | |
355 | ||
356 | /* For swizzling simply ensure that we always flush both | |
357 | * channels. Lame, but simple and it works. Swizzled | |
358 | * pwrite/pread is far from a hotpath - current userspace | |
359 | * doesn't use it at all. */ | |
360 | start = round_down(start, 128); | |
361 | end = round_up(end, 128); | |
362 | ||
363 | drm_clflush_virt_range((void *)start, end - start); | |
364 | } else { | |
365 | drm_clflush_virt_range(addr, length); | |
366 | } | |
367 | ||
368 | } | |
369 | ||
d174bd64 DV |
370 | /* Only difference to the fast-path function is that this can handle bit17 |
371 | * and uses non-atomic copy and kmap functions. */ | |
372 | static int | |
373 | shmem_pread_slow(struct page *page, int shmem_page_offset, int page_length, | |
374 | char __user *user_data, | |
375 | bool page_do_bit17_swizzling, bool needs_clflush) | |
376 | { | |
377 | char *vaddr; | |
378 | int ret; | |
379 | ||
380 | vaddr = kmap(page); | |
381 | if (needs_clflush) | |
23c18c71 DV |
382 | shmem_clflush_swizzled_range(vaddr + shmem_page_offset, |
383 | page_length, | |
384 | page_do_bit17_swizzling); | |
d174bd64 DV |
385 | |
386 | if (page_do_bit17_swizzling) | |
387 | ret = __copy_to_user_swizzled(user_data, | |
388 | vaddr, shmem_page_offset, | |
389 | page_length); | |
390 | else | |
391 | ret = __copy_to_user(user_data, | |
392 | vaddr + shmem_page_offset, | |
393 | page_length); | |
394 | kunmap(page); | |
395 | ||
396 | return ret; | |
397 | } | |
398 | ||
eb01459f | 399 | static int |
dbf7bff0 DV |
400 | i915_gem_shmem_pread(struct drm_device *dev, |
401 | struct drm_i915_gem_object *obj, | |
402 | struct drm_i915_gem_pread *args, | |
403 | struct drm_file *file) | |
eb01459f | 404 | { |
05394f39 | 405 | struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping; |
8461d226 | 406 | char __user *user_data; |
eb01459f | 407 | ssize_t remain; |
8461d226 | 408 | loff_t offset; |
eb2c0c81 | 409 | int shmem_page_offset, page_length, ret = 0; |
8461d226 | 410 | int obj_do_bit17_swizzling, page_do_bit17_swizzling; |
dbf7bff0 | 411 | int hit_slowpath = 0; |
96d79b52 | 412 | int prefaulted = 0; |
8489731c | 413 | int needs_clflush = 0; |
692a576b | 414 | int release_page; |
eb01459f | 415 | |
8461d226 | 416 | user_data = (char __user *) (uintptr_t) args->data_ptr; |
eb01459f EA |
417 | remain = args->size; |
418 | ||
8461d226 | 419 | obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj); |
eb01459f | 420 | |
8489731c DV |
421 | if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU)) { |
422 | /* If we're not in the cpu read domain, set ourself into the gtt | |
423 | * read domain and manually flush cachelines (if required). This | |
424 | * optimizes for the case when the gpu will dirty the data | |
425 | * anyway again before the next pread happens. */ | |
426 | if (obj->cache_level == I915_CACHE_NONE) | |
427 | needs_clflush = 1; | |
6c085a72 CW |
428 | if (obj->gtt_space) { |
429 | ret = i915_gem_object_set_to_gtt_domain(obj, false); | |
430 | if (ret) | |
431 | return ret; | |
432 | } | |
8489731c | 433 | } |
eb01459f | 434 | |
8461d226 | 435 | offset = args->offset; |
eb01459f EA |
436 | |
437 | while (remain > 0) { | |
e5281ccd CW |
438 | struct page *page; |
439 | ||
eb01459f EA |
440 | /* Operation in this page |
441 | * | |
eb01459f | 442 | * shmem_page_offset = offset within page in shmem file |
eb01459f EA |
443 | * page_length = bytes to copy for this page |
444 | */ | |
c8cbbb8b | 445 | shmem_page_offset = offset_in_page(offset); |
eb01459f EA |
446 | page_length = remain; |
447 | if ((shmem_page_offset + page_length) > PAGE_SIZE) | |
448 | page_length = PAGE_SIZE - shmem_page_offset; | |
eb01459f | 449 | |
692a576b DV |
450 | if (obj->pages) { |
451 | page = obj->pages[offset >> PAGE_SHIFT]; | |
452 | release_page = 0; | |
453 | } else { | |
454 | page = shmem_read_mapping_page(mapping, offset >> PAGE_SHIFT); | |
455 | if (IS_ERR(page)) { | |
456 | ret = PTR_ERR(page); | |
457 | goto out; | |
458 | } | |
459 | release_page = 1; | |
b65552f0 | 460 | } |
e5281ccd | 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 DV |
470 | |
471 | hit_slowpath = 1; | |
692a576b | 472 | page_cache_get(page); |
dbf7bff0 DV |
473 | mutex_unlock(&dev->struct_mutex); |
474 | ||
96d79b52 | 475 | if (!prefaulted) { |
f56f821f | 476 | ret = fault_in_multipages_writeable(user_data, remain); |
96d79b52 DV |
477 | /* Userspace is tricking us, but we've already clobbered |
478 | * its pages with the prefault and promised to write the | |
479 | * data up to the first fault. Hence ignore any errors | |
480 | * and just continue. */ | |
481 | (void)ret; | |
482 | prefaulted = 1; | |
483 | } | |
eb01459f | 484 | |
d174bd64 DV |
485 | ret = shmem_pread_slow(page, shmem_page_offset, page_length, |
486 | user_data, page_do_bit17_swizzling, | |
487 | needs_clflush); | |
eb01459f | 488 | |
dbf7bff0 | 489 | mutex_lock(&dev->struct_mutex); |
e5281ccd | 490 | page_cache_release(page); |
dbf7bff0 | 491 | next_page: |
e5281ccd | 492 | mark_page_accessed(page); |
692a576b DV |
493 | if (release_page) |
494 | page_cache_release(page); | |
e5281ccd | 495 | |
8461d226 DV |
496 | if (ret) { |
497 | ret = -EFAULT; | |
498 | goto out; | |
499 | } | |
500 | ||
eb01459f | 501 | remain -= page_length; |
8461d226 | 502 | user_data += page_length; |
eb01459f EA |
503 | offset += page_length; |
504 | } | |
505 | ||
4f27b75d | 506 | out: |
dbf7bff0 DV |
507 | if (hit_slowpath) { |
508 | /* Fixup: Kill any reinstated backing storage pages */ | |
509 | if (obj->madv == __I915_MADV_PURGED) | |
510 | i915_gem_object_truncate(obj); | |
511 | } | |
eb01459f EA |
512 | |
513 | return ret; | |
514 | } | |
515 | ||
673a394b EA |
516 | /** |
517 | * Reads data from the object referenced by handle. | |
518 | * | |
519 | * On error, the contents of *data are undefined. | |
520 | */ | |
521 | int | |
522 | i915_gem_pread_ioctl(struct drm_device *dev, void *data, | |
05394f39 | 523 | struct drm_file *file) |
673a394b EA |
524 | { |
525 | struct drm_i915_gem_pread *args = data; | |
05394f39 | 526 | struct drm_i915_gem_object *obj; |
35b62a89 | 527 | int ret = 0; |
673a394b | 528 | |
51311d0a CW |
529 | if (args->size == 0) |
530 | return 0; | |
531 | ||
532 | if (!access_ok(VERIFY_WRITE, | |
533 | (char __user *)(uintptr_t)args->data_ptr, | |
534 | args->size)) | |
535 | return -EFAULT; | |
536 | ||
4f27b75d | 537 | ret = i915_mutex_lock_interruptible(dev); |
1d7cfea1 | 538 | if (ret) |
4f27b75d | 539 | return ret; |
673a394b | 540 | |
05394f39 | 541 | obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle)); |
c8725226 | 542 | if (&obj->base == NULL) { |
1d7cfea1 CW |
543 | ret = -ENOENT; |
544 | goto unlock; | |
4f27b75d | 545 | } |
673a394b | 546 | |
7dcd2499 | 547 | /* Bounds check source. */ |
05394f39 CW |
548 | if (args->offset > obj->base.size || |
549 | args->size > obj->base.size - args->offset) { | |
ce9d419d | 550 | ret = -EINVAL; |
35b62a89 | 551 | goto out; |
ce9d419d CW |
552 | } |
553 | ||
1286ff73 DV |
554 | /* prime objects have no backing filp to GEM pread/pwrite |
555 | * pages from. | |
556 | */ | |
557 | if (!obj->base.filp) { | |
558 | ret = -EINVAL; | |
559 | goto out; | |
560 | } | |
561 | ||
db53a302 CW |
562 | trace_i915_gem_object_pread(obj, args->offset, args->size); |
563 | ||
dbf7bff0 | 564 | ret = i915_gem_shmem_pread(dev, obj, args, file); |
673a394b | 565 | |
35b62a89 | 566 | out: |
05394f39 | 567 | drm_gem_object_unreference(&obj->base); |
1d7cfea1 | 568 | unlock: |
4f27b75d | 569 | mutex_unlock(&dev->struct_mutex); |
eb01459f | 570 | return ret; |
673a394b EA |
571 | } |
572 | ||
0839ccb8 KP |
573 | /* This is the fast write path which cannot handle |
574 | * page faults in the source data | |
9b7530cc | 575 | */ |
0839ccb8 KP |
576 | |
577 | static inline int | |
578 | fast_user_write(struct io_mapping *mapping, | |
579 | loff_t page_base, int page_offset, | |
580 | char __user *user_data, | |
581 | int length) | |
9b7530cc | 582 | { |
4f0c7cfb BW |
583 | void __iomem *vaddr_atomic; |
584 | void *vaddr; | |
0839ccb8 | 585 | unsigned long unwritten; |
9b7530cc | 586 | |
3e4d3af5 | 587 | vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base); |
4f0c7cfb BW |
588 | /* We can use the cpu mem copy function because this is X86. */ |
589 | vaddr = (void __force*)vaddr_atomic + page_offset; | |
590 | unwritten = __copy_from_user_inatomic_nocache(vaddr, | |
0839ccb8 | 591 | user_data, length); |
3e4d3af5 | 592 | io_mapping_unmap_atomic(vaddr_atomic); |
fbd5a26d | 593 | return unwritten; |
0839ccb8 KP |
594 | } |
595 | ||
3de09aa3 EA |
596 | /** |
597 | * This is the fast pwrite path, where we copy the data directly from the | |
598 | * user into the GTT, uncached. | |
599 | */ | |
673a394b | 600 | static int |
05394f39 CW |
601 | i915_gem_gtt_pwrite_fast(struct drm_device *dev, |
602 | struct drm_i915_gem_object *obj, | |
3de09aa3 | 603 | struct drm_i915_gem_pwrite *args, |
05394f39 | 604 | struct drm_file *file) |
673a394b | 605 | { |
0839ccb8 | 606 | drm_i915_private_t *dev_priv = dev->dev_private; |
673a394b | 607 | ssize_t remain; |
0839ccb8 | 608 | loff_t offset, page_base; |
673a394b | 609 | char __user *user_data; |
935aaa69 DV |
610 | int page_offset, page_length, ret; |
611 | ||
612 | ret = i915_gem_object_pin(obj, 0, true); | |
613 | if (ret) | |
614 | goto out; | |
615 | ||
616 | ret = i915_gem_object_set_to_gtt_domain(obj, true); | |
617 | if (ret) | |
618 | goto out_unpin; | |
619 | ||
620 | ret = i915_gem_object_put_fence(obj); | |
621 | if (ret) | |
622 | goto out_unpin; | |
673a394b EA |
623 | |
624 | user_data = (char __user *) (uintptr_t) args->data_ptr; | |
625 | remain = args->size; | |
673a394b | 626 | |
05394f39 | 627 | offset = obj->gtt_offset + args->offset; |
673a394b EA |
628 | |
629 | while (remain > 0) { | |
630 | /* Operation in this page | |
631 | * | |
0839ccb8 KP |
632 | * page_base = page offset within aperture |
633 | * page_offset = offset within page | |
634 | * page_length = bytes to copy for this page | |
673a394b | 635 | */ |
c8cbbb8b CW |
636 | page_base = offset & PAGE_MASK; |
637 | page_offset = offset_in_page(offset); | |
0839ccb8 KP |
638 | page_length = remain; |
639 | if ((page_offset + remain) > PAGE_SIZE) | |
640 | page_length = PAGE_SIZE - page_offset; | |
641 | ||
0839ccb8 | 642 | /* If we get a fault while copying data, then (presumably) our |
3de09aa3 EA |
643 | * source page isn't available. Return the error and we'll |
644 | * retry in the slow path. | |
0839ccb8 | 645 | */ |
fbd5a26d | 646 | if (fast_user_write(dev_priv->mm.gtt_mapping, page_base, |
935aaa69 DV |
647 | page_offset, user_data, page_length)) { |
648 | ret = -EFAULT; | |
649 | goto out_unpin; | |
650 | } | |
673a394b | 651 | |
0839ccb8 KP |
652 | remain -= page_length; |
653 | user_data += page_length; | |
654 | offset += page_length; | |
673a394b | 655 | } |
673a394b | 656 | |
935aaa69 DV |
657 | out_unpin: |
658 | i915_gem_object_unpin(obj); | |
659 | out: | |
3de09aa3 | 660 | return ret; |
673a394b EA |
661 | } |
662 | ||
d174bd64 DV |
663 | /* Per-page copy function for the shmem pwrite fastpath. |
664 | * Flushes invalid cachelines before writing to the target if | |
665 | * needs_clflush_before is set and flushes out any written cachelines after | |
666 | * writing if needs_clflush is set. */ | |
3043c60c | 667 | static int |
d174bd64 DV |
668 | shmem_pwrite_fast(struct page *page, int shmem_page_offset, int page_length, |
669 | char __user *user_data, | |
670 | bool page_do_bit17_swizzling, | |
671 | bool needs_clflush_before, | |
672 | bool needs_clflush_after) | |
673a394b | 673 | { |
d174bd64 | 674 | char *vaddr; |
673a394b | 675 | int ret; |
3de09aa3 | 676 | |
e7e58eb5 | 677 | if (unlikely(page_do_bit17_swizzling)) |
d174bd64 | 678 | return -EINVAL; |
3de09aa3 | 679 | |
d174bd64 DV |
680 | vaddr = kmap_atomic(page); |
681 | if (needs_clflush_before) | |
682 | drm_clflush_virt_range(vaddr + shmem_page_offset, | |
683 | page_length); | |
684 | ret = __copy_from_user_inatomic_nocache(vaddr + shmem_page_offset, | |
685 | user_data, | |
686 | page_length); | |
687 | if (needs_clflush_after) | |
688 | drm_clflush_virt_range(vaddr + shmem_page_offset, | |
689 | page_length); | |
690 | kunmap_atomic(vaddr); | |
3de09aa3 EA |
691 | |
692 | return ret; | |
693 | } | |
694 | ||
d174bd64 DV |
695 | /* Only difference to the fast-path function is that this can handle bit17 |
696 | * and uses non-atomic copy and kmap functions. */ | |
3043c60c | 697 | static int |
d174bd64 DV |
698 | shmem_pwrite_slow(struct page *page, int shmem_page_offset, int page_length, |
699 | char __user *user_data, | |
700 | bool page_do_bit17_swizzling, | |
701 | bool needs_clflush_before, | |
702 | bool needs_clflush_after) | |
673a394b | 703 | { |
d174bd64 DV |
704 | char *vaddr; |
705 | int ret; | |
e5281ccd | 706 | |
d174bd64 | 707 | vaddr = kmap(page); |
e7e58eb5 | 708 | if (unlikely(needs_clflush_before || page_do_bit17_swizzling)) |
23c18c71 DV |
709 | shmem_clflush_swizzled_range(vaddr + shmem_page_offset, |
710 | page_length, | |
711 | page_do_bit17_swizzling); | |
d174bd64 DV |
712 | if (page_do_bit17_swizzling) |
713 | ret = __copy_from_user_swizzled(vaddr, shmem_page_offset, | |
e5281ccd CW |
714 | user_data, |
715 | page_length); | |
d174bd64 DV |
716 | else |
717 | ret = __copy_from_user(vaddr + shmem_page_offset, | |
718 | user_data, | |
719 | page_length); | |
720 | if (needs_clflush_after) | |
23c18c71 DV |
721 | shmem_clflush_swizzled_range(vaddr + shmem_page_offset, |
722 | page_length, | |
723 | page_do_bit17_swizzling); | |
d174bd64 | 724 | kunmap(page); |
40123c1f | 725 | |
d174bd64 | 726 | return ret; |
40123c1f EA |
727 | } |
728 | ||
40123c1f | 729 | static int |
e244a443 DV |
730 | i915_gem_shmem_pwrite(struct drm_device *dev, |
731 | struct drm_i915_gem_object *obj, | |
732 | struct drm_i915_gem_pwrite *args, | |
733 | struct drm_file *file) | |
40123c1f | 734 | { |
05394f39 | 735 | struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping; |
40123c1f | 736 | ssize_t remain; |
8c59967c DV |
737 | loff_t offset; |
738 | char __user *user_data; | |
eb2c0c81 | 739 | int shmem_page_offset, page_length, ret = 0; |
8c59967c | 740 | int obj_do_bit17_swizzling, page_do_bit17_swizzling; |
e244a443 | 741 | int hit_slowpath = 0; |
58642885 DV |
742 | int needs_clflush_after = 0; |
743 | int needs_clflush_before = 0; | |
692a576b | 744 | int release_page; |
40123c1f | 745 | |
8c59967c | 746 | user_data = (char __user *) (uintptr_t) args->data_ptr; |
40123c1f EA |
747 | remain = args->size; |
748 | ||
8c59967c | 749 | obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj); |
40123c1f | 750 | |
58642885 DV |
751 | if (obj->base.write_domain != I915_GEM_DOMAIN_CPU) { |
752 | /* If we're not in the cpu write domain, set ourself into the gtt | |
753 | * write domain and manually flush cachelines (if required). This | |
754 | * optimizes for the case when the gpu will use the data | |
755 | * right away and we therefore have to clflush anyway. */ | |
756 | if (obj->cache_level == I915_CACHE_NONE) | |
757 | needs_clflush_after = 1; | |
6c085a72 CW |
758 | if (obj->gtt_space) { |
759 | ret = i915_gem_object_set_to_gtt_domain(obj, true); | |
760 | if (ret) | |
761 | return ret; | |
762 | } | |
58642885 DV |
763 | } |
764 | /* Same trick applies for invalidate partially written cachelines before | |
765 | * writing. */ | |
766 | if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU) | |
767 | && obj->cache_level == I915_CACHE_NONE) | |
768 | needs_clflush_before = 1; | |
769 | ||
673a394b | 770 | offset = args->offset; |
05394f39 | 771 | obj->dirty = 1; |
673a394b | 772 | |
40123c1f | 773 | while (remain > 0) { |
e5281ccd | 774 | struct page *page; |
58642885 | 775 | int partial_cacheline_write; |
e5281ccd | 776 | |
40123c1f EA |
777 | /* Operation in this page |
778 | * | |
40123c1f | 779 | * shmem_page_offset = offset within page in shmem file |
40123c1f EA |
780 | * page_length = bytes to copy for this page |
781 | */ | |
c8cbbb8b | 782 | shmem_page_offset = offset_in_page(offset); |
40123c1f EA |
783 | |
784 | page_length = remain; | |
785 | if ((shmem_page_offset + page_length) > PAGE_SIZE) | |
786 | page_length = PAGE_SIZE - shmem_page_offset; | |
40123c1f | 787 | |
58642885 DV |
788 | /* If we don't overwrite a cacheline completely we need to be |
789 | * careful to have up-to-date data by first clflushing. Don't | |
790 | * overcomplicate things and flush the entire patch. */ | |
791 | partial_cacheline_write = needs_clflush_before && | |
792 | ((shmem_page_offset | page_length) | |
793 | & (boot_cpu_data.x86_clflush_size - 1)); | |
794 | ||
692a576b DV |
795 | if (obj->pages) { |
796 | page = obj->pages[offset >> PAGE_SHIFT]; | |
797 | release_page = 0; | |
798 | } else { | |
799 | page = shmem_read_mapping_page(mapping, offset >> PAGE_SHIFT); | |
800 | if (IS_ERR(page)) { | |
801 | ret = PTR_ERR(page); | |
802 | goto out; | |
803 | } | |
804 | release_page = 1; | |
e5281ccd CW |
805 | } |
806 | ||
8c59967c DV |
807 | page_do_bit17_swizzling = obj_do_bit17_swizzling && |
808 | (page_to_phys(page) & (1 << 17)) != 0; | |
809 | ||
d174bd64 DV |
810 | ret = shmem_pwrite_fast(page, shmem_page_offset, page_length, |
811 | user_data, page_do_bit17_swizzling, | |
812 | partial_cacheline_write, | |
813 | needs_clflush_after); | |
814 | if (ret == 0) | |
815 | goto next_page; | |
e244a443 DV |
816 | |
817 | hit_slowpath = 1; | |
692a576b | 818 | page_cache_get(page); |
e244a443 DV |
819 | mutex_unlock(&dev->struct_mutex); |
820 | ||
d174bd64 DV |
821 | ret = shmem_pwrite_slow(page, shmem_page_offset, page_length, |
822 | user_data, page_do_bit17_swizzling, | |
823 | partial_cacheline_write, | |
824 | needs_clflush_after); | |
40123c1f | 825 | |
e244a443 | 826 | mutex_lock(&dev->struct_mutex); |
692a576b | 827 | page_cache_release(page); |
e244a443 | 828 | next_page: |
e5281ccd CW |
829 | set_page_dirty(page); |
830 | mark_page_accessed(page); | |
692a576b DV |
831 | if (release_page) |
832 | page_cache_release(page); | |
e5281ccd | 833 | |
8c59967c DV |
834 | if (ret) { |
835 | ret = -EFAULT; | |
836 | goto out; | |
837 | } | |
838 | ||
40123c1f | 839 | remain -= page_length; |
8c59967c | 840 | user_data += page_length; |
40123c1f | 841 | offset += page_length; |
673a394b EA |
842 | } |
843 | ||
fbd5a26d | 844 | out: |
e244a443 DV |
845 | if (hit_slowpath) { |
846 | /* Fixup: Kill any reinstated backing storage pages */ | |
847 | if (obj->madv == __I915_MADV_PURGED) | |
848 | i915_gem_object_truncate(obj); | |
849 | /* and flush dirty cachelines in case the object isn't in the cpu write | |
850 | * domain anymore. */ | |
851 | if (obj->base.write_domain != I915_GEM_DOMAIN_CPU) { | |
852 | i915_gem_clflush_object(obj); | |
853 | intel_gtt_chipset_flush(); | |
854 | } | |
8c59967c | 855 | } |
673a394b | 856 | |
58642885 DV |
857 | if (needs_clflush_after) |
858 | intel_gtt_chipset_flush(); | |
859 | ||
40123c1f | 860 | return ret; |
673a394b EA |
861 | } |
862 | ||
863 | /** | |
864 | * Writes data to the object referenced by handle. | |
865 | * | |
866 | * On error, the contents of the buffer that were to be modified are undefined. | |
867 | */ | |
868 | int | |
869 | i915_gem_pwrite_ioctl(struct drm_device *dev, void *data, | |
fbd5a26d | 870 | struct drm_file *file) |
673a394b EA |
871 | { |
872 | struct drm_i915_gem_pwrite *args = data; | |
05394f39 | 873 | struct drm_i915_gem_object *obj; |
51311d0a CW |
874 | int ret; |
875 | ||
876 | if (args->size == 0) | |
877 | return 0; | |
878 | ||
879 | if (!access_ok(VERIFY_READ, | |
880 | (char __user *)(uintptr_t)args->data_ptr, | |
881 | args->size)) | |
882 | return -EFAULT; | |
883 | ||
f56f821f DV |
884 | ret = fault_in_multipages_readable((char __user *)(uintptr_t)args->data_ptr, |
885 | args->size); | |
51311d0a CW |
886 | if (ret) |
887 | return -EFAULT; | |
673a394b | 888 | |
fbd5a26d | 889 | ret = i915_mutex_lock_interruptible(dev); |
1d7cfea1 | 890 | if (ret) |
fbd5a26d | 891 | return ret; |
1d7cfea1 | 892 | |
05394f39 | 893 | obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle)); |
c8725226 | 894 | if (&obj->base == NULL) { |
1d7cfea1 CW |
895 | ret = -ENOENT; |
896 | goto unlock; | |
fbd5a26d | 897 | } |
673a394b | 898 | |
7dcd2499 | 899 | /* Bounds check destination. */ |
05394f39 CW |
900 | if (args->offset > obj->base.size || |
901 | args->size > obj->base.size - args->offset) { | |
ce9d419d | 902 | ret = -EINVAL; |
35b62a89 | 903 | goto out; |
ce9d419d CW |
904 | } |
905 | ||
1286ff73 DV |
906 | /* prime objects have no backing filp to GEM pread/pwrite |
907 | * pages from. | |
908 | */ | |
909 | if (!obj->base.filp) { | |
910 | ret = -EINVAL; | |
911 | goto out; | |
912 | } | |
913 | ||
db53a302 CW |
914 | trace_i915_gem_object_pwrite(obj, args->offset, args->size); |
915 | ||
935aaa69 | 916 | ret = -EFAULT; |
673a394b EA |
917 | /* We can only do the GTT pwrite on untiled buffers, as otherwise |
918 | * it would end up going through the fenced access, and we'll get | |
919 | * different detiling behavior between reading and writing. | |
920 | * pread/pwrite currently are reading and writing from the CPU | |
921 | * perspective, requiring manual detiling by the client. | |
922 | */ | |
5c0480f2 | 923 | if (obj->phys_obj) { |
fbd5a26d | 924 | ret = i915_gem_phys_pwrite(dev, obj, args, file); |
5c0480f2 DV |
925 | goto out; |
926 | } | |
927 | ||
928 | if (obj->gtt_space && | |
3ae53783 | 929 | obj->cache_level == I915_CACHE_NONE && |
c07496fa | 930 | obj->tiling_mode == I915_TILING_NONE && |
ffc62976 | 931 | obj->map_and_fenceable && |
5c0480f2 | 932 | obj->base.write_domain != I915_GEM_DOMAIN_CPU) { |
fbd5a26d | 933 | ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file); |
935aaa69 DV |
934 | /* Note that the gtt paths might fail with non-page-backed user |
935 | * pointers (e.g. gtt mappings when moving data between | |
936 | * textures). Fallback to the shmem path in that case. */ | |
fbd5a26d | 937 | } |
673a394b | 938 | |
5c0480f2 | 939 | if (ret == -EFAULT) |
935aaa69 | 940 | ret = i915_gem_shmem_pwrite(dev, obj, args, file); |
5c0480f2 | 941 | |
35b62a89 | 942 | out: |
05394f39 | 943 | drm_gem_object_unreference(&obj->base); |
1d7cfea1 | 944 | unlock: |
fbd5a26d | 945 | mutex_unlock(&dev->struct_mutex); |
673a394b EA |
946 | return ret; |
947 | } | |
948 | ||
949 | /** | |
2ef7eeaa EA |
950 | * Called when user space prepares to use an object with the CPU, either |
951 | * through the mmap ioctl's mapping or a GTT mapping. | |
673a394b EA |
952 | */ |
953 | int | |
954 | i915_gem_set_domain_ioctl(struct drm_device *dev, void *data, | |
05394f39 | 955 | struct drm_file *file) |
673a394b EA |
956 | { |
957 | struct drm_i915_gem_set_domain *args = data; | |
05394f39 | 958 | struct drm_i915_gem_object *obj; |
2ef7eeaa EA |
959 | uint32_t read_domains = args->read_domains; |
960 | uint32_t write_domain = args->write_domain; | |
673a394b EA |
961 | int ret; |
962 | ||
2ef7eeaa | 963 | /* Only handle setting domains to types used by the CPU. */ |
21d509e3 | 964 | if (write_domain & I915_GEM_GPU_DOMAINS) |
2ef7eeaa EA |
965 | return -EINVAL; |
966 | ||
21d509e3 | 967 | if (read_domains & I915_GEM_GPU_DOMAINS) |
2ef7eeaa EA |
968 | return -EINVAL; |
969 | ||
970 | /* Having something in the write domain implies it's in the read | |
971 | * domain, and only that read domain. Enforce that in the request. | |
972 | */ | |
973 | if (write_domain != 0 && read_domains != write_domain) | |
974 | return -EINVAL; | |
975 | ||
76c1dec1 | 976 | ret = i915_mutex_lock_interruptible(dev); |
1d7cfea1 | 977 | if (ret) |
76c1dec1 | 978 | return ret; |
1d7cfea1 | 979 | |
05394f39 | 980 | obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle)); |
c8725226 | 981 | if (&obj->base == NULL) { |
1d7cfea1 CW |
982 | ret = -ENOENT; |
983 | goto unlock; | |
76c1dec1 | 984 | } |
673a394b | 985 | |
2ef7eeaa EA |
986 | if (read_domains & I915_GEM_DOMAIN_GTT) { |
987 | ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0); | |
02354392 EA |
988 | |
989 | /* Silently promote "you're not bound, there was nothing to do" | |
990 | * to success, since the client was just asking us to | |
991 | * make sure everything was done. | |
992 | */ | |
993 | if (ret == -EINVAL) | |
994 | ret = 0; | |
2ef7eeaa | 995 | } else { |
e47c68e9 | 996 | ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0); |
2ef7eeaa EA |
997 | } |
998 | ||
05394f39 | 999 | drm_gem_object_unreference(&obj->base); |
1d7cfea1 | 1000 | unlock: |
673a394b EA |
1001 | mutex_unlock(&dev->struct_mutex); |
1002 | return ret; | |
1003 | } | |
1004 | ||
1005 | /** | |
1006 | * Called when user space has done writes to this buffer | |
1007 | */ | |
1008 | int | |
1009 | i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data, | |
05394f39 | 1010 | struct drm_file *file) |
673a394b EA |
1011 | { |
1012 | struct drm_i915_gem_sw_finish *args = data; | |
05394f39 | 1013 | struct drm_i915_gem_object *obj; |
673a394b EA |
1014 | int ret = 0; |
1015 | ||
76c1dec1 | 1016 | ret = i915_mutex_lock_interruptible(dev); |
1d7cfea1 | 1017 | if (ret) |
76c1dec1 | 1018 | return ret; |
1d7cfea1 | 1019 | |
05394f39 | 1020 | obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle)); |
c8725226 | 1021 | if (&obj->base == NULL) { |
1d7cfea1 CW |
1022 | ret = -ENOENT; |
1023 | goto unlock; | |
673a394b EA |
1024 | } |
1025 | ||
673a394b | 1026 | /* Pinned buffers may be scanout, so flush the cache */ |
05394f39 | 1027 | if (obj->pin_count) |
e47c68e9 EA |
1028 | i915_gem_object_flush_cpu_write_domain(obj); |
1029 | ||
05394f39 | 1030 | drm_gem_object_unreference(&obj->base); |
1d7cfea1 | 1031 | unlock: |
673a394b EA |
1032 | mutex_unlock(&dev->struct_mutex); |
1033 | return ret; | |
1034 | } | |
1035 | ||
1036 | /** | |
1037 | * Maps the contents of an object, returning the address it is mapped | |
1038 | * into. | |
1039 | * | |
1040 | * While the mapping holds a reference on the contents of the object, it doesn't | |
1041 | * imply a ref on the object itself. | |
1042 | */ | |
1043 | int | |
1044 | i915_gem_mmap_ioctl(struct drm_device *dev, void *data, | |
05394f39 | 1045 | struct drm_file *file) |
673a394b EA |
1046 | { |
1047 | struct drm_i915_gem_mmap *args = data; | |
1048 | struct drm_gem_object *obj; | |
673a394b EA |
1049 | unsigned long addr; |
1050 | ||
05394f39 | 1051 | obj = drm_gem_object_lookup(dev, file, args->handle); |
673a394b | 1052 | if (obj == NULL) |
bf79cb91 | 1053 | return -ENOENT; |
673a394b | 1054 | |
1286ff73 DV |
1055 | /* prime objects have no backing filp to GEM mmap |
1056 | * pages from. | |
1057 | */ | |
1058 | if (!obj->filp) { | |
1059 | drm_gem_object_unreference_unlocked(obj); | |
1060 | return -EINVAL; | |
1061 | } | |
1062 | ||
6be5ceb0 | 1063 | addr = vm_mmap(obj->filp, 0, args->size, |
673a394b EA |
1064 | PROT_READ | PROT_WRITE, MAP_SHARED, |
1065 | args->offset); | |
bc9025bd | 1066 | drm_gem_object_unreference_unlocked(obj); |
673a394b EA |
1067 | if (IS_ERR((void *)addr)) |
1068 | return addr; | |
1069 | ||
1070 | args->addr_ptr = (uint64_t) addr; | |
1071 | ||
1072 | return 0; | |
1073 | } | |
1074 | ||
de151cf6 JB |
1075 | /** |
1076 | * i915_gem_fault - fault a page into the GTT | |
1077 | * vma: VMA in question | |
1078 | * vmf: fault info | |
1079 | * | |
1080 | * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped | |
1081 | * from userspace. The fault handler takes care of binding the object to | |
1082 | * the GTT (if needed), allocating and programming a fence register (again, | |
1083 | * only if needed based on whether the old reg is still valid or the object | |
1084 | * is tiled) and inserting a new PTE into the faulting process. | |
1085 | * | |
1086 | * Note that the faulting process may involve evicting existing objects | |
1087 | * from the GTT and/or fence registers to make room. So performance may | |
1088 | * suffer if the GTT working set is large or there are few fence registers | |
1089 | * left. | |
1090 | */ | |
1091 | int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf) | |
1092 | { | |
05394f39 CW |
1093 | struct drm_i915_gem_object *obj = to_intel_bo(vma->vm_private_data); |
1094 | struct drm_device *dev = obj->base.dev; | |
7d1c4804 | 1095 | drm_i915_private_t *dev_priv = dev->dev_private; |
de151cf6 JB |
1096 | pgoff_t page_offset; |
1097 | unsigned long pfn; | |
1098 | int ret = 0; | |
0f973f27 | 1099 | bool write = !!(vmf->flags & FAULT_FLAG_WRITE); |
de151cf6 JB |
1100 | |
1101 | /* We don't use vmf->pgoff since that has the fake offset */ | |
1102 | page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >> | |
1103 | PAGE_SHIFT; | |
1104 | ||
d9bc7e9f CW |
1105 | ret = i915_mutex_lock_interruptible(dev); |
1106 | if (ret) | |
1107 | goto out; | |
a00b10c3 | 1108 | |
db53a302 CW |
1109 | trace_i915_gem_object_fault(obj, page_offset, true, write); |
1110 | ||
d9bc7e9f | 1111 | /* Now bind it into the GTT if needed */ |
919926ae CW |
1112 | if (!obj->map_and_fenceable) { |
1113 | ret = i915_gem_object_unbind(obj); | |
1114 | if (ret) | |
1115 | goto unlock; | |
a00b10c3 | 1116 | } |
05394f39 | 1117 | if (!obj->gtt_space) { |
75e9e915 | 1118 | ret = i915_gem_object_bind_to_gtt(obj, 0, true); |
c715089f CW |
1119 | if (ret) |
1120 | goto unlock; | |
de151cf6 | 1121 | |
e92d03bf EA |
1122 | ret = i915_gem_object_set_to_gtt_domain(obj, write); |
1123 | if (ret) | |
1124 | goto unlock; | |
1125 | } | |
4a684a41 | 1126 | |
74898d7e DV |
1127 | if (!obj->has_global_gtt_mapping) |
1128 | i915_gem_gtt_bind_object(obj, obj->cache_level); | |
1129 | ||
06d98131 | 1130 | ret = i915_gem_object_get_fence(obj); |
d9e86c0e CW |
1131 | if (ret) |
1132 | goto unlock; | |
de151cf6 | 1133 | |
05394f39 CW |
1134 | if (i915_gem_object_is_inactive(obj)) |
1135 | list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list); | |
7d1c4804 | 1136 | |
6299f992 CW |
1137 | obj->fault_mappable = true; |
1138 | ||
dd2757f8 | 1139 | pfn = ((dev_priv->mm.gtt_base_addr + obj->gtt_offset) >> PAGE_SHIFT) + |
de151cf6 JB |
1140 | page_offset; |
1141 | ||
1142 | /* Finally, remap it using the new GTT offset */ | |
1143 | ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn); | |
c715089f | 1144 | unlock: |
de151cf6 | 1145 | mutex_unlock(&dev->struct_mutex); |
d9bc7e9f | 1146 | out: |
de151cf6 | 1147 | switch (ret) { |
d9bc7e9f | 1148 | case -EIO: |
a9340cca DV |
1149 | /* If this -EIO is due to a gpu hang, give the reset code a |
1150 | * chance to clean up the mess. Otherwise return the proper | |
1151 | * SIGBUS. */ | |
1152 | if (!atomic_read(&dev_priv->mm.wedged)) | |
1153 | return VM_FAULT_SIGBUS; | |
045e769a | 1154 | case -EAGAIN: |
d9bc7e9f CW |
1155 | /* Give the error handler a chance to run and move the |
1156 | * objects off the GPU active list. Next time we service the | |
1157 | * fault, we should be able to transition the page into the | |
1158 | * GTT without touching the GPU (and so avoid further | |
1159 | * EIO/EGAIN). If the GPU is wedged, then there is no issue | |
1160 | * with coherency, just lost writes. | |
1161 | */ | |
045e769a | 1162 | set_need_resched(); |
c715089f CW |
1163 | case 0: |
1164 | case -ERESTARTSYS: | |
bed636ab | 1165 | case -EINTR: |
c715089f | 1166 | return VM_FAULT_NOPAGE; |
de151cf6 | 1167 | case -ENOMEM: |
de151cf6 | 1168 | return VM_FAULT_OOM; |
de151cf6 | 1169 | default: |
c715089f | 1170 | return VM_FAULT_SIGBUS; |
de151cf6 JB |
1171 | } |
1172 | } | |
1173 | ||
901782b2 CW |
1174 | /** |
1175 | * i915_gem_release_mmap - remove physical page mappings | |
1176 | * @obj: obj in question | |
1177 | * | |
af901ca1 | 1178 | * Preserve the reservation of the mmapping with the DRM core code, but |
901782b2 CW |
1179 | * relinquish ownership of the pages back to the system. |
1180 | * | |
1181 | * It is vital that we remove the page mapping if we have mapped a tiled | |
1182 | * object through the GTT and then lose the fence register due to | |
1183 | * resource pressure. Similarly if the object has been moved out of the | |
1184 | * aperture, than pages mapped into userspace must be revoked. Removing the | |
1185 | * mapping will then trigger a page fault on the next user access, allowing | |
1186 | * fixup by i915_gem_fault(). | |
1187 | */ | |
d05ca301 | 1188 | void |
05394f39 | 1189 | i915_gem_release_mmap(struct drm_i915_gem_object *obj) |
901782b2 | 1190 | { |
6299f992 CW |
1191 | if (!obj->fault_mappable) |
1192 | return; | |
901782b2 | 1193 | |
f6e47884 CW |
1194 | if (obj->base.dev->dev_mapping) |
1195 | unmap_mapping_range(obj->base.dev->dev_mapping, | |
1196 | (loff_t)obj->base.map_list.hash.key<<PAGE_SHIFT, | |
1197 | obj->base.size, 1); | |
fb7d516a | 1198 | |
6299f992 | 1199 | obj->fault_mappable = false; |
901782b2 CW |
1200 | } |
1201 | ||
92b88aeb | 1202 | static uint32_t |
e28f8711 | 1203 | i915_gem_get_gtt_size(struct drm_device *dev, uint32_t size, int tiling_mode) |
92b88aeb | 1204 | { |
e28f8711 | 1205 | uint32_t gtt_size; |
92b88aeb CW |
1206 | |
1207 | if (INTEL_INFO(dev)->gen >= 4 || | |
e28f8711 CW |
1208 | tiling_mode == I915_TILING_NONE) |
1209 | return size; | |
92b88aeb CW |
1210 | |
1211 | /* Previous chips need a power-of-two fence region when tiling */ | |
1212 | if (INTEL_INFO(dev)->gen == 3) | |
e28f8711 | 1213 | gtt_size = 1024*1024; |
92b88aeb | 1214 | else |
e28f8711 | 1215 | gtt_size = 512*1024; |
92b88aeb | 1216 | |
e28f8711 CW |
1217 | while (gtt_size < size) |
1218 | gtt_size <<= 1; | |
92b88aeb | 1219 | |
e28f8711 | 1220 | return gtt_size; |
92b88aeb CW |
1221 | } |
1222 | ||
de151cf6 JB |
1223 | /** |
1224 | * i915_gem_get_gtt_alignment - return required GTT alignment for an object | |
1225 | * @obj: object to check | |
1226 | * | |
1227 | * Return the required GTT alignment for an object, taking into account | |
5e783301 | 1228 | * potential fence register mapping. |
de151cf6 JB |
1229 | */ |
1230 | static uint32_t | |
e28f8711 CW |
1231 | i915_gem_get_gtt_alignment(struct drm_device *dev, |
1232 | uint32_t size, | |
1233 | int tiling_mode) | |
de151cf6 | 1234 | { |
de151cf6 JB |
1235 | /* |
1236 | * Minimum alignment is 4k (GTT page size), but might be greater | |
1237 | * if a fence register is needed for the object. | |
1238 | */ | |
a00b10c3 | 1239 | if (INTEL_INFO(dev)->gen >= 4 || |
e28f8711 | 1240 | tiling_mode == I915_TILING_NONE) |
de151cf6 JB |
1241 | return 4096; |
1242 | ||
a00b10c3 CW |
1243 | /* |
1244 | * Previous chips need to be aligned to the size of the smallest | |
1245 | * fence register that can contain the object. | |
1246 | */ | |
e28f8711 | 1247 | return i915_gem_get_gtt_size(dev, size, tiling_mode); |
a00b10c3 CW |
1248 | } |
1249 | ||
5e783301 DV |
1250 | /** |
1251 | * i915_gem_get_unfenced_gtt_alignment - return required GTT alignment for an | |
1252 | * unfenced object | |
e28f8711 CW |
1253 | * @dev: the device |
1254 | * @size: size of the object | |
1255 | * @tiling_mode: tiling mode of the object | |
5e783301 DV |
1256 | * |
1257 | * Return the required GTT alignment for an object, only taking into account | |
1258 | * unfenced tiled surface requirements. | |
1259 | */ | |
467cffba | 1260 | uint32_t |
e28f8711 CW |
1261 | i915_gem_get_unfenced_gtt_alignment(struct drm_device *dev, |
1262 | uint32_t size, | |
1263 | int tiling_mode) | |
5e783301 | 1264 | { |
5e783301 DV |
1265 | /* |
1266 | * Minimum alignment is 4k (GTT page size) for sane hw. | |
1267 | */ | |
1268 | if (INTEL_INFO(dev)->gen >= 4 || IS_G33(dev) || | |
e28f8711 | 1269 | tiling_mode == I915_TILING_NONE) |
5e783301 DV |
1270 | return 4096; |
1271 | ||
e28f8711 CW |
1272 | /* Previous hardware however needs to be aligned to a power-of-two |
1273 | * tile height. The simplest method for determining this is to reuse | |
1274 | * the power-of-tile object size. | |
5e783301 | 1275 | */ |
e28f8711 | 1276 | return i915_gem_get_gtt_size(dev, size, tiling_mode); |
5e783301 DV |
1277 | } |
1278 | ||
de151cf6 | 1279 | int |
ff72145b DA |
1280 | i915_gem_mmap_gtt(struct drm_file *file, |
1281 | struct drm_device *dev, | |
1282 | uint32_t handle, | |
1283 | uint64_t *offset) | |
de151cf6 | 1284 | { |
da761a6e | 1285 | struct drm_i915_private *dev_priv = dev->dev_private; |
05394f39 | 1286 | struct drm_i915_gem_object *obj; |
de151cf6 JB |
1287 | int ret; |
1288 | ||
76c1dec1 | 1289 | ret = i915_mutex_lock_interruptible(dev); |
1d7cfea1 | 1290 | if (ret) |
76c1dec1 | 1291 | return ret; |
de151cf6 | 1292 | |
ff72145b | 1293 | obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle)); |
c8725226 | 1294 | if (&obj->base == NULL) { |
1d7cfea1 CW |
1295 | ret = -ENOENT; |
1296 | goto unlock; | |
1297 | } | |
de151cf6 | 1298 | |
05394f39 | 1299 | if (obj->base.size > dev_priv->mm.gtt_mappable_end) { |
da761a6e | 1300 | ret = -E2BIG; |
ff56b0bc | 1301 | goto out; |
da761a6e CW |
1302 | } |
1303 | ||
05394f39 | 1304 | if (obj->madv != I915_MADV_WILLNEED) { |
ab18282d | 1305 | DRM_ERROR("Attempting to mmap a purgeable buffer\n"); |
1d7cfea1 CW |
1306 | ret = -EINVAL; |
1307 | goto out; | |
ab18282d CW |
1308 | } |
1309 | ||
05394f39 | 1310 | if (!obj->base.map_list.map) { |
b464e9a2 | 1311 | ret = drm_gem_create_mmap_offset(&obj->base); |
1d7cfea1 CW |
1312 | if (ret) |
1313 | goto out; | |
de151cf6 JB |
1314 | } |
1315 | ||
ff72145b | 1316 | *offset = (u64)obj->base.map_list.hash.key << PAGE_SHIFT; |
de151cf6 | 1317 | |
1d7cfea1 | 1318 | out: |
05394f39 | 1319 | drm_gem_object_unreference(&obj->base); |
1d7cfea1 | 1320 | unlock: |
de151cf6 | 1321 | mutex_unlock(&dev->struct_mutex); |
1d7cfea1 | 1322 | return ret; |
de151cf6 JB |
1323 | } |
1324 | ||
ff72145b DA |
1325 | /** |
1326 | * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing | |
1327 | * @dev: DRM device | |
1328 | * @data: GTT mapping ioctl data | |
1329 | * @file: GEM object info | |
1330 | * | |
1331 | * Simply returns the fake offset to userspace so it can mmap it. | |
1332 | * The mmap call will end up in drm_gem_mmap(), which will set things | |
1333 | * up so we can get faults in the handler above. | |
1334 | * | |
1335 | * The fault handler will take care of binding the object into the GTT | |
1336 | * (since it may have been evicted to make room for something), allocating | |
1337 | * a fence register, and mapping the appropriate aperture address into | |
1338 | * userspace. | |
1339 | */ | |
1340 | int | |
1341 | i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data, | |
1342 | struct drm_file *file) | |
1343 | { | |
1344 | struct drm_i915_gem_mmap_gtt *args = data; | |
1345 | ||
ff72145b DA |
1346 | return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset); |
1347 | } | |
1348 | ||
225067ee DV |
1349 | /* Immediately discard the backing storage */ |
1350 | static void | |
1351 | i915_gem_object_truncate(struct drm_i915_gem_object *obj) | |
1352 | { | |
1353 | struct inode *inode; | |
1354 | ||
1355 | /* Our goal here is to return as much of the memory as | |
1356 | * is possible back to the system as we are called from OOM. | |
1357 | * To do this we must instruct the shmfs to drop all of its | |
1358 | * backing pages, *now*. | |
1359 | */ | |
1360 | inode = obj->base.filp->f_path.dentry->d_inode; | |
1361 | shmem_truncate_range(inode, 0, (loff_t)-1); | |
1362 | ||
1363 | if (obj->base.map_list.map) | |
1364 | drm_gem_free_mmap_offset(&obj->base); | |
1365 | ||
1366 | obj->madv = __I915_MADV_PURGED; | |
1367 | } | |
1368 | ||
1369 | static inline int | |
1370 | i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj) | |
1371 | { | |
1372 | return obj->madv == I915_MADV_DONTNEED; | |
1373 | } | |
1374 | ||
6c085a72 | 1375 | static int |
225067ee DV |
1376 | i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj) |
1377 | { | |
1378 | int page_count = obj->base.size / PAGE_SIZE; | |
6c085a72 | 1379 | int ret, i; |
225067ee | 1380 | |
c4670ad0 CW |
1381 | BUG_ON(obj->gtt_space); |
1382 | ||
6c085a72 CW |
1383 | if (obj->pages == NULL) |
1384 | return 0; | |
225067ee | 1385 | |
6c085a72 | 1386 | BUG_ON(obj->gtt_space); |
225067ee DV |
1387 | BUG_ON(obj->madv == __I915_MADV_PURGED); |
1388 | ||
6c085a72 CW |
1389 | ret = i915_gem_object_set_to_cpu_domain(obj, true); |
1390 | if (ret) { | |
1391 | /* In the event of a disaster, abandon all caches and | |
1392 | * hope for the best. | |
1393 | */ | |
1394 | WARN_ON(ret != -EIO); | |
1395 | i915_gem_clflush_object(obj); | |
1396 | obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU; | |
1397 | } | |
1398 | ||
225067ee DV |
1399 | if (i915_gem_object_needs_bit17_swizzle(obj)) |
1400 | i915_gem_object_save_bit_17_swizzle(obj); | |
1401 | ||
1402 | if (obj->madv == I915_MADV_DONTNEED) | |
1403 | obj->dirty = 0; | |
1404 | ||
1405 | for (i = 0; i < page_count; i++) { | |
1406 | if (obj->dirty) | |
1407 | set_page_dirty(obj->pages[i]); | |
1408 | ||
1409 | if (obj->madv == I915_MADV_WILLNEED) | |
1410 | mark_page_accessed(obj->pages[i]); | |
1411 | ||
1412 | page_cache_release(obj->pages[i]); | |
1413 | } | |
1414 | obj->dirty = 0; | |
1415 | ||
1416 | drm_free_large(obj->pages); | |
1417 | obj->pages = NULL; | |
6c085a72 CW |
1418 | |
1419 | list_del(&obj->gtt_list); | |
1420 | ||
1421 | if (i915_gem_object_is_purgeable(obj)) | |
1422 | i915_gem_object_truncate(obj); | |
1423 | ||
1424 | return 0; | |
1425 | } | |
1426 | ||
1427 | static long | |
1428 | i915_gem_purge(struct drm_i915_private *dev_priv, long target) | |
1429 | { | |
1430 | struct drm_i915_gem_object *obj, *next; | |
1431 | long count = 0; | |
1432 | ||
1433 | list_for_each_entry_safe(obj, next, | |
1434 | &dev_priv->mm.unbound_list, | |
1435 | gtt_list) { | |
1436 | if (i915_gem_object_is_purgeable(obj) && | |
1437 | i915_gem_object_put_pages_gtt(obj) == 0) { | |
1438 | count += obj->base.size >> PAGE_SHIFT; | |
1439 | if (count >= target) | |
1440 | return count; | |
1441 | } | |
1442 | } | |
1443 | ||
1444 | list_for_each_entry_safe(obj, next, | |
1445 | &dev_priv->mm.inactive_list, | |
1446 | mm_list) { | |
1447 | if (i915_gem_object_is_purgeable(obj) && | |
1448 | i915_gem_object_unbind(obj) == 0 && | |
1449 | i915_gem_object_put_pages_gtt(obj) == 0) { | |
1450 | count += obj->base.size >> PAGE_SHIFT; | |
1451 | if (count >= target) | |
1452 | return count; | |
1453 | } | |
1454 | } | |
1455 | ||
1456 | return count; | |
1457 | } | |
1458 | ||
1459 | static void | |
1460 | i915_gem_shrink_all(struct drm_i915_private *dev_priv) | |
1461 | { | |
1462 | struct drm_i915_gem_object *obj, *next; | |
1463 | ||
1464 | i915_gem_evict_everything(dev_priv->dev); | |
1465 | ||
1466 | list_for_each_entry_safe(obj, next, &dev_priv->mm.unbound_list, gtt_list) | |
1467 | i915_gem_object_put_pages_gtt(obj); | |
225067ee DV |
1468 | } |
1469 | ||
1286ff73 | 1470 | int |
6c085a72 | 1471 | i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj) |
e5281ccd | 1472 | { |
6c085a72 | 1473 | struct drm_i915_private *dev_priv = obj->base.dev->dev_private; |
e5281ccd CW |
1474 | int page_count, i; |
1475 | struct address_space *mapping; | |
e5281ccd | 1476 | struct page *page; |
6c085a72 | 1477 | gfp_t gfp; |
e5281ccd | 1478 | |
1286ff73 DV |
1479 | if (obj->pages || obj->sg_table) |
1480 | return 0; | |
1481 | ||
6c085a72 CW |
1482 | /* Assert that the object is not currently in any GPU domain. As it |
1483 | * wasn't in the GTT, there shouldn't be any way it could have been in | |
1484 | * a GPU cache | |
1485 | */ | |
1486 | BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS); | |
1487 | BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS); | |
1488 | ||
e5281ccd CW |
1489 | /* Get the list of pages out of our struct file. They'll be pinned |
1490 | * at this point until we release them. | |
1491 | */ | |
05394f39 | 1492 | page_count = obj->base.size / PAGE_SIZE; |
05394f39 CW |
1493 | obj->pages = drm_malloc_ab(page_count, sizeof(struct page *)); |
1494 | if (obj->pages == NULL) | |
e5281ccd CW |
1495 | return -ENOMEM; |
1496 | ||
6c085a72 CW |
1497 | /* Fail silently without starting the shrinker */ |
1498 | mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping; | |
1499 | gfp = mapping_gfp_mask(mapping); | |
1500 | gfp |= __GFP_NORETRY | __GFP_NOWARN | __GFP_NO_KSWAPD; | |
1501 | gfp &= ~(__GFP_IO | __GFP_WAIT); | |
e5281ccd | 1502 | for (i = 0; i < page_count; i++) { |
6c085a72 CW |
1503 | page = shmem_read_mapping_page_gfp(mapping, i, gfp); |
1504 | if (IS_ERR(page)) { | |
1505 | i915_gem_purge(dev_priv, page_count); | |
1506 | page = shmem_read_mapping_page_gfp(mapping, i, gfp); | |
1507 | } | |
1508 | if (IS_ERR(page)) { | |
1509 | /* We've tried hard to allocate the memory by reaping | |
1510 | * our own buffer, now let the real VM do its job and | |
1511 | * go down in flames if truly OOM. | |
1512 | */ | |
1513 | gfp &= ~(__GFP_NORETRY | __GFP_NOWARN | __GFP_NO_KSWAPD); | |
1514 | gfp |= __GFP_IO | __GFP_WAIT; | |
1515 | ||
1516 | i915_gem_shrink_all(dev_priv); | |
1517 | page = shmem_read_mapping_page_gfp(mapping, i, gfp); | |
1518 | if (IS_ERR(page)) | |
1519 | goto err_pages; | |
1520 | ||
1521 | gfp |= __GFP_NORETRY | __GFP_NOWARN | __GFP_NO_KSWAPD; | |
1522 | gfp &= ~(__GFP_IO | __GFP_WAIT); | |
1523 | } | |
e5281ccd | 1524 | |
05394f39 | 1525 | obj->pages[i] = page; |
e5281ccd CW |
1526 | } |
1527 | ||
6dacfd2f | 1528 | if (i915_gem_object_needs_bit17_swizzle(obj)) |
e5281ccd CW |
1529 | i915_gem_object_do_bit_17_swizzle(obj); |
1530 | ||
6c085a72 | 1531 | list_add_tail(&obj->gtt_list, &dev_priv->mm.unbound_list); |
e5281ccd CW |
1532 | return 0; |
1533 | ||
1534 | err_pages: | |
1535 | while (i--) | |
05394f39 | 1536 | page_cache_release(obj->pages[i]); |
e5281ccd | 1537 | |
05394f39 CW |
1538 | drm_free_large(obj->pages); |
1539 | obj->pages = NULL; | |
e5281ccd CW |
1540 | return PTR_ERR(page); |
1541 | } | |
1542 | ||
54cf91dc | 1543 | void |
05394f39 | 1544 | i915_gem_object_move_to_active(struct drm_i915_gem_object *obj, |
1ec14ad3 CW |
1545 | struct intel_ring_buffer *ring, |
1546 | u32 seqno) | |
673a394b | 1547 | { |
05394f39 | 1548 | struct drm_device *dev = obj->base.dev; |
69dc4987 | 1549 | struct drm_i915_private *dev_priv = dev->dev_private; |
617dbe27 | 1550 | |
852835f3 | 1551 | BUG_ON(ring == NULL); |
05394f39 | 1552 | obj->ring = ring; |
673a394b EA |
1553 | |
1554 | /* Add a reference if we're newly entering the active list. */ | |
05394f39 CW |
1555 | if (!obj->active) { |
1556 | drm_gem_object_reference(&obj->base); | |
1557 | obj->active = 1; | |
673a394b | 1558 | } |
e35a41de | 1559 | |
673a394b | 1560 | /* Move from whatever list we were on to the tail of execution. */ |
05394f39 CW |
1561 | list_move_tail(&obj->mm_list, &dev_priv->mm.active_list); |
1562 | list_move_tail(&obj->ring_list, &ring->active_list); | |
caea7476 | 1563 | |
0201f1ec | 1564 | obj->last_read_seqno = seqno; |
caea7476 | 1565 | |
7dd49065 | 1566 | if (obj->fenced_gpu_access) { |
caea7476 | 1567 | obj->last_fenced_seqno = seqno; |
caea7476 | 1568 | |
7dd49065 CW |
1569 | /* Bump MRU to take account of the delayed flush */ |
1570 | if (obj->fence_reg != I915_FENCE_REG_NONE) { | |
1571 | struct drm_i915_fence_reg *reg; | |
1572 | ||
1573 | reg = &dev_priv->fence_regs[obj->fence_reg]; | |
1574 | list_move_tail(®->lru_list, | |
1575 | &dev_priv->mm.fence_list); | |
1576 | } | |
caea7476 CW |
1577 | } |
1578 | } | |
1579 | ||
caea7476 CW |
1580 | static void |
1581 | i915_gem_object_move_to_inactive(struct drm_i915_gem_object *obj) | |
1582 | { | |
1583 | struct drm_device *dev = obj->base.dev; | |
1584 | struct drm_i915_private *dev_priv = dev->dev_private; | |
1585 | ||
65ce3027 | 1586 | BUG_ON(obj->base.write_domain & ~I915_GEM_GPU_DOMAINS); |
caea7476 | 1587 | BUG_ON(!obj->active); |
65ce3027 | 1588 | |
f047e395 CW |
1589 | if (obj->pin_count) /* are we a framebuffer? */ |
1590 | intel_mark_fb_idle(obj); | |
1591 | ||
1592 | list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list); | |
1593 | ||
65ce3027 | 1594 | list_del_init(&obj->ring_list); |
caea7476 CW |
1595 | obj->ring = NULL; |
1596 | ||
65ce3027 CW |
1597 | obj->last_read_seqno = 0; |
1598 | obj->last_write_seqno = 0; | |
1599 | obj->base.write_domain = 0; | |
1600 | ||
1601 | obj->last_fenced_seqno = 0; | |
caea7476 | 1602 | obj->fenced_gpu_access = false; |
caea7476 CW |
1603 | |
1604 | obj->active = 0; | |
1605 | drm_gem_object_unreference(&obj->base); | |
1606 | ||
1607 | WARN_ON(i915_verify_lists(dev)); | |
ce44b0ea | 1608 | } |
673a394b | 1609 | |
53d227f2 DV |
1610 | static u32 |
1611 | i915_gem_get_seqno(struct drm_device *dev) | |
1612 | { | |
1613 | drm_i915_private_t *dev_priv = dev->dev_private; | |
1614 | u32 seqno = dev_priv->next_seqno; | |
1615 | ||
1616 | /* reserve 0 for non-seqno */ | |
1617 | if (++dev_priv->next_seqno == 0) | |
1618 | dev_priv->next_seqno = 1; | |
1619 | ||
1620 | return seqno; | |
1621 | } | |
1622 | ||
1623 | u32 | |
1624 | i915_gem_next_request_seqno(struct intel_ring_buffer *ring) | |
1625 | { | |
1626 | if (ring->outstanding_lazy_request == 0) | |
1627 | ring->outstanding_lazy_request = i915_gem_get_seqno(ring->dev); | |
1628 | ||
1629 | return ring->outstanding_lazy_request; | |
1630 | } | |
1631 | ||
3cce469c | 1632 | int |
db53a302 | 1633 | i915_add_request(struct intel_ring_buffer *ring, |
f787a5f5 | 1634 | struct drm_file *file, |
db53a302 | 1635 | struct drm_i915_gem_request *request) |
673a394b | 1636 | { |
db53a302 | 1637 | drm_i915_private_t *dev_priv = ring->dev->dev_private; |
673a394b | 1638 | uint32_t seqno; |
a71d8d94 | 1639 | u32 request_ring_position; |
673a394b | 1640 | int was_empty; |
3cce469c CW |
1641 | int ret; |
1642 | ||
cc889e0f DV |
1643 | /* |
1644 | * Emit any outstanding flushes - execbuf can fail to emit the flush | |
1645 | * after having emitted the batchbuffer command. Hence we need to fix | |
1646 | * things up similar to emitting the lazy request. The difference here | |
1647 | * is that the flush _must_ happen before the next request, no matter | |
1648 | * what. | |
1649 | */ | |
a7b9761d CW |
1650 | ret = intel_ring_flush_all_caches(ring); |
1651 | if (ret) | |
1652 | return ret; | |
cc889e0f | 1653 | |
3bb73aba CW |
1654 | if (request == NULL) { |
1655 | request = kmalloc(sizeof(*request), GFP_KERNEL); | |
1656 | if (request == NULL) | |
1657 | return -ENOMEM; | |
1658 | } | |
1659 | ||
53d227f2 | 1660 | seqno = i915_gem_next_request_seqno(ring); |
673a394b | 1661 | |
a71d8d94 CW |
1662 | /* Record the position of the start of the request so that |
1663 | * should we detect the updated seqno part-way through the | |
1664 | * GPU processing the request, we never over-estimate the | |
1665 | * position of the head. | |
1666 | */ | |
1667 | request_ring_position = intel_ring_get_tail(ring); | |
1668 | ||
3cce469c | 1669 | ret = ring->add_request(ring, &seqno); |
3bb73aba CW |
1670 | if (ret) { |
1671 | kfree(request); | |
1672 | return ret; | |
1673 | } | |
673a394b | 1674 | |
db53a302 | 1675 | trace_i915_gem_request_add(ring, seqno); |
673a394b EA |
1676 | |
1677 | request->seqno = seqno; | |
852835f3 | 1678 | request->ring = ring; |
a71d8d94 | 1679 | request->tail = request_ring_position; |
673a394b | 1680 | request->emitted_jiffies = jiffies; |
852835f3 ZN |
1681 | was_empty = list_empty(&ring->request_list); |
1682 | list_add_tail(&request->list, &ring->request_list); | |
3bb73aba | 1683 | request->file_priv = NULL; |
852835f3 | 1684 | |
db53a302 CW |
1685 | if (file) { |
1686 | struct drm_i915_file_private *file_priv = file->driver_priv; | |
1687 | ||
1c25595f | 1688 | spin_lock(&file_priv->mm.lock); |
f787a5f5 | 1689 | request->file_priv = file_priv; |
b962442e | 1690 | list_add_tail(&request->client_list, |
f787a5f5 | 1691 | &file_priv->mm.request_list); |
1c25595f | 1692 | spin_unlock(&file_priv->mm.lock); |
b962442e | 1693 | } |
673a394b | 1694 | |
5391d0cf | 1695 | ring->outstanding_lazy_request = 0; |
db53a302 | 1696 | |
f65d9421 | 1697 | if (!dev_priv->mm.suspended) { |
3e0dc6b0 BW |
1698 | if (i915_enable_hangcheck) { |
1699 | mod_timer(&dev_priv->hangcheck_timer, | |
1700 | jiffies + | |
1701 | msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD)); | |
1702 | } | |
f047e395 | 1703 | if (was_empty) { |
b3b079db CW |
1704 | queue_delayed_work(dev_priv->wq, |
1705 | &dev_priv->mm.retire_work, HZ); | |
f047e395 CW |
1706 | intel_mark_busy(dev_priv->dev); |
1707 | } | |
f65d9421 | 1708 | } |
cc889e0f | 1709 | |
3cce469c | 1710 | return 0; |
673a394b EA |
1711 | } |
1712 | ||
f787a5f5 CW |
1713 | static inline void |
1714 | i915_gem_request_remove_from_client(struct drm_i915_gem_request *request) | |
673a394b | 1715 | { |
1c25595f | 1716 | struct drm_i915_file_private *file_priv = request->file_priv; |
673a394b | 1717 | |
1c25595f CW |
1718 | if (!file_priv) |
1719 | return; | |
1c5d22f7 | 1720 | |
1c25595f | 1721 | spin_lock(&file_priv->mm.lock); |
09bfa517 HRK |
1722 | if (request->file_priv) { |
1723 | list_del(&request->client_list); | |
1724 | request->file_priv = NULL; | |
1725 | } | |
1c25595f | 1726 | spin_unlock(&file_priv->mm.lock); |
673a394b | 1727 | } |
673a394b | 1728 | |
dfaae392 CW |
1729 | static void i915_gem_reset_ring_lists(struct drm_i915_private *dev_priv, |
1730 | struct intel_ring_buffer *ring) | |
9375e446 | 1731 | { |
dfaae392 CW |
1732 | while (!list_empty(&ring->request_list)) { |
1733 | struct drm_i915_gem_request *request; | |
673a394b | 1734 | |
dfaae392 CW |
1735 | request = list_first_entry(&ring->request_list, |
1736 | struct drm_i915_gem_request, | |
1737 | list); | |
de151cf6 | 1738 | |
dfaae392 | 1739 | list_del(&request->list); |
f787a5f5 | 1740 | i915_gem_request_remove_from_client(request); |
dfaae392 CW |
1741 | kfree(request); |
1742 | } | |
673a394b | 1743 | |
dfaae392 | 1744 | while (!list_empty(&ring->active_list)) { |
05394f39 | 1745 | struct drm_i915_gem_object *obj; |
9375e446 | 1746 | |
05394f39 CW |
1747 | obj = list_first_entry(&ring->active_list, |
1748 | struct drm_i915_gem_object, | |
1749 | ring_list); | |
9375e446 | 1750 | |
05394f39 | 1751 | i915_gem_object_move_to_inactive(obj); |
673a394b EA |
1752 | } |
1753 | } | |
1754 | ||
312817a3 CW |
1755 | static void i915_gem_reset_fences(struct drm_device *dev) |
1756 | { | |
1757 | struct drm_i915_private *dev_priv = dev->dev_private; | |
1758 | int i; | |
1759 | ||
4b9de737 | 1760 | for (i = 0; i < dev_priv->num_fence_regs; i++) { |
312817a3 | 1761 | struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i]; |
7d2cb39c | 1762 | |
ada726c7 | 1763 | i915_gem_write_fence(dev, i, NULL); |
7d2cb39c | 1764 | |
ada726c7 CW |
1765 | if (reg->obj) |
1766 | i915_gem_object_fence_lost(reg->obj); | |
7d2cb39c | 1767 | |
ada726c7 CW |
1768 | reg->pin_count = 0; |
1769 | reg->obj = NULL; | |
1770 | INIT_LIST_HEAD(®->lru_list); | |
312817a3 | 1771 | } |
ada726c7 CW |
1772 | |
1773 | INIT_LIST_HEAD(&dev_priv->mm.fence_list); | |
312817a3 CW |
1774 | } |
1775 | ||
069efc1d | 1776 | void i915_gem_reset(struct drm_device *dev) |
673a394b | 1777 | { |
77f01230 | 1778 | struct drm_i915_private *dev_priv = dev->dev_private; |
05394f39 | 1779 | struct drm_i915_gem_object *obj; |
b4519513 | 1780 | struct intel_ring_buffer *ring; |
1ec14ad3 | 1781 | int i; |
673a394b | 1782 | |
b4519513 CW |
1783 | for_each_ring(ring, dev_priv, i) |
1784 | i915_gem_reset_ring_lists(dev_priv, ring); | |
dfaae392 | 1785 | |
dfaae392 CW |
1786 | /* Move everything out of the GPU domains to ensure we do any |
1787 | * necessary invalidation upon reuse. | |
1788 | */ | |
05394f39 | 1789 | list_for_each_entry(obj, |
77f01230 | 1790 | &dev_priv->mm.inactive_list, |
69dc4987 | 1791 | mm_list) |
77f01230 | 1792 | { |
05394f39 | 1793 | obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS; |
77f01230 | 1794 | } |
069efc1d | 1795 | |
6c085a72 | 1796 | |
069efc1d | 1797 | /* The fence registers are invalidated so clear them out */ |
312817a3 | 1798 | i915_gem_reset_fences(dev); |
673a394b EA |
1799 | } |
1800 | ||
1801 | /** | |
1802 | * This function clears the request list as sequence numbers are passed. | |
1803 | */ | |
a71d8d94 | 1804 | void |
db53a302 | 1805 | i915_gem_retire_requests_ring(struct intel_ring_buffer *ring) |
673a394b | 1806 | { |
673a394b | 1807 | uint32_t seqno; |
1ec14ad3 | 1808 | int i; |
673a394b | 1809 | |
db53a302 | 1810 | if (list_empty(&ring->request_list)) |
6c0594a3 KW |
1811 | return; |
1812 | ||
db53a302 | 1813 | WARN_ON(i915_verify_lists(ring->dev)); |
673a394b | 1814 | |
b2eadbc8 | 1815 | seqno = ring->get_seqno(ring, true); |
1ec14ad3 | 1816 | |
076e2c0e | 1817 | for (i = 0; i < ARRAY_SIZE(ring->sync_seqno); i++) |
1ec14ad3 CW |
1818 | if (seqno >= ring->sync_seqno[i]) |
1819 | ring->sync_seqno[i] = 0; | |
1820 | ||
852835f3 | 1821 | while (!list_empty(&ring->request_list)) { |
673a394b | 1822 | struct drm_i915_gem_request *request; |
673a394b | 1823 | |
852835f3 | 1824 | request = list_first_entry(&ring->request_list, |
673a394b EA |
1825 | struct drm_i915_gem_request, |
1826 | list); | |
673a394b | 1827 | |
dfaae392 | 1828 | if (!i915_seqno_passed(seqno, request->seqno)) |
b84d5f0c CW |
1829 | break; |
1830 | ||
db53a302 | 1831 | trace_i915_gem_request_retire(ring, request->seqno); |
a71d8d94 CW |
1832 | /* We know the GPU must have read the request to have |
1833 | * sent us the seqno + interrupt, so use the position | |
1834 | * of tail of the request to update the last known position | |
1835 | * of the GPU head. | |
1836 | */ | |
1837 | ring->last_retired_head = request->tail; | |
b84d5f0c CW |
1838 | |
1839 | list_del(&request->list); | |
f787a5f5 | 1840 | i915_gem_request_remove_from_client(request); |
b84d5f0c CW |
1841 | kfree(request); |
1842 | } | |
673a394b | 1843 | |
b84d5f0c CW |
1844 | /* Move any buffers on the active list that are no longer referenced |
1845 | * by the ringbuffer to the flushing/inactive lists as appropriate. | |
1846 | */ | |
1847 | while (!list_empty(&ring->active_list)) { | |
05394f39 | 1848 | struct drm_i915_gem_object *obj; |
b84d5f0c | 1849 | |
0206e353 | 1850 | obj = list_first_entry(&ring->active_list, |
05394f39 CW |
1851 | struct drm_i915_gem_object, |
1852 | ring_list); | |
673a394b | 1853 | |
0201f1ec | 1854 | if (!i915_seqno_passed(seqno, obj->last_read_seqno)) |
673a394b | 1855 | break; |
b84d5f0c | 1856 | |
65ce3027 | 1857 | i915_gem_object_move_to_inactive(obj); |
673a394b | 1858 | } |
9d34e5db | 1859 | |
db53a302 CW |
1860 | if (unlikely(ring->trace_irq_seqno && |
1861 | i915_seqno_passed(seqno, ring->trace_irq_seqno))) { | |
1ec14ad3 | 1862 | ring->irq_put(ring); |
db53a302 | 1863 | ring->trace_irq_seqno = 0; |
9d34e5db | 1864 | } |
23bc5982 | 1865 | |
db53a302 | 1866 | WARN_ON(i915_verify_lists(ring->dev)); |
673a394b EA |
1867 | } |
1868 | ||
b09a1fec CW |
1869 | void |
1870 | i915_gem_retire_requests(struct drm_device *dev) | |
1871 | { | |
1872 | drm_i915_private_t *dev_priv = dev->dev_private; | |
b4519513 | 1873 | struct intel_ring_buffer *ring; |
1ec14ad3 | 1874 | int i; |
b09a1fec | 1875 | |
b4519513 CW |
1876 | for_each_ring(ring, dev_priv, i) |
1877 | i915_gem_retire_requests_ring(ring); | |
b09a1fec CW |
1878 | } |
1879 | ||
75ef9da2 | 1880 | static void |
673a394b EA |
1881 | i915_gem_retire_work_handler(struct work_struct *work) |
1882 | { | |
1883 | drm_i915_private_t *dev_priv; | |
1884 | struct drm_device *dev; | |
b4519513 | 1885 | struct intel_ring_buffer *ring; |
0a58705b CW |
1886 | bool idle; |
1887 | int i; | |
673a394b EA |
1888 | |
1889 | dev_priv = container_of(work, drm_i915_private_t, | |
1890 | mm.retire_work.work); | |
1891 | dev = dev_priv->dev; | |
1892 | ||
891b48cf CW |
1893 | /* Come back later if the device is busy... */ |
1894 | if (!mutex_trylock(&dev->struct_mutex)) { | |
1895 | queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ); | |
1896 | return; | |
1897 | } | |
1898 | ||
b09a1fec | 1899 | i915_gem_retire_requests(dev); |
d1b851fc | 1900 | |
0a58705b CW |
1901 | /* Send a periodic flush down the ring so we don't hold onto GEM |
1902 | * objects indefinitely. | |
1903 | */ | |
1904 | idle = true; | |
b4519513 | 1905 | for_each_ring(ring, dev_priv, i) { |
3bb73aba CW |
1906 | if (ring->gpu_caches_dirty) |
1907 | i915_add_request(ring, NULL, NULL); | |
0a58705b CW |
1908 | |
1909 | idle &= list_empty(&ring->request_list); | |
1910 | } | |
1911 | ||
1912 | if (!dev_priv->mm.suspended && !idle) | |
9c9fe1f8 | 1913 | queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ); |
f047e395 CW |
1914 | if (idle) |
1915 | intel_mark_idle(dev); | |
0a58705b | 1916 | |
673a394b EA |
1917 | mutex_unlock(&dev->struct_mutex); |
1918 | } | |
1919 | ||
d6b2c790 DV |
1920 | int |
1921 | i915_gem_check_wedge(struct drm_i915_private *dev_priv, | |
1922 | bool interruptible) | |
b4aca010 | 1923 | { |
b4aca010 BW |
1924 | if (atomic_read(&dev_priv->mm.wedged)) { |
1925 | struct completion *x = &dev_priv->error_completion; | |
1926 | bool recovery_complete; | |
1927 | unsigned long flags; | |
1928 | ||
1929 | /* Give the error handler a chance to run. */ | |
1930 | spin_lock_irqsave(&x->wait.lock, flags); | |
1931 | recovery_complete = x->done > 0; | |
1932 | spin_unlock_irqrestore(&x->wait.lock, flags); | |
1933 | ||
d6b2c790 DV |
1934 | /* Non-interruptible callers can't handle -EAGAIN, hence return |
1935 | * -EIO unconditionally for these. */ | |
1936 | if (!interruptible) | |
1937 | return -EIO; | |
1938 | ||
1939 | /* Recovery complete, but still wedged means reset failure. */ | |
1940 | if (recovery_complete) | |
1941 | return -EIO; | |
1942 | ||
1943 | return -EAGAIN; | |
b4aca010 BW |
1944 | } |
1945 | ||
1946 | return 0; | |
1947 | } | |
1948 | ||
1949 | /* | |
1950 | * Compare seqno against outstanding lazy request. Emit a request if they are | |
1951 | * equal. | |
1952 | */ | |
1953 | static int | |
1954 | i915_gem_check_olr(struct intel_ring_buffer *ring, u32 seqno) | |
1955 | { | |
3bb73aba | 1956 | int ret; |
b4aca010 BW |
1957 | |
1958 | BUG_ON(!mutex_is_locked(&ring->dev->struct_mutex)); | |
1959 | ||
3bb73aba CW |
1960 | ret = 0; |
1961 | if (seqno == ring->outstanding_lazy_request) | |
1962 | ret = i915_add_request(ring, NULL, NULL); | |
b4aca010 BW |
1963 | |
1964 | return ret; | |
1965 | } | |
1966 | ||
5c81fe85 BW |
1967 | /** |
1968 | * __wait_seqno - wait until execution of seqno has finished | |
1969 | * @ring: the ring expected to report seqno | |
1970 | * @seqno: duh! | |
1971 | * @interruptible: do an interruptible wait (normally yes) | |
1972 | * @timeout: in - how long to wait (NULL forever); out - how much time remaining | |
1973 | * | |
1974 | * Returns 0 if the seqno was found within the alloted time. Else returns the | |
1975 | * errno with remaining time filled in timeout argument. | |
1976 | */ | |
604dd3ec | 1977 | static int __wait_seqno(struct intel_ring_buffer *ring, u32 seqno, |
5c81fe85 | 1978 | bool interruptible, struct timespec *timeout) |
604dd3ec BW |
1979 | { |
1980 | drm_i915_private_t *dev_priv = ring->dev->dev_private; | |
5c81fe85 BW |
1981 | struct timespec before, now, wait_time={1,0}; |
1982 | unsigned long timeout_jiffies; | |
1983 | long end; | |
1984 | bool wait_forever = true; | |
d6b2c790 | 1985 | int ret; |
604dd3ec | 1986 | |
b2eadbc8 | 1987 | if (i915_seqno_passed(ring->get_seqno(ring, true), seqno)) |
604dd3ec BW |
1988 | return 0; |
1989 | ||
1990 | trace_i915_gem_request_wait_begin(ring, seqno); | |
5c81fe85 BW |
1991 | |
1992 | if (timeout != NULL) { | |
1993 | wait_time = *timeout; | |
1994 | wait_forever = false; | |
1995 | } | |
1996 | ||
1997 | timeout_jiffies = timespec_to_jiffies(&wait_time); | |
1998 | ||
604dd3ec BW |
1999 | if (WARN_ON(!ring->irq_get(ring))) |
2000 | return -ENODEV; | |
2001 | ||
5c81fe85 BW |
2002 | /* Record current time in case interrupted by signal, or wedged * */ |
2003 | getrawmonotonic(&before); | |
2004 | ||
604dd3ec | 2005 | #define EXIT_COND \ |
b2eadbc8 | 2006 | (i915_seqno_passed(ring->get_seqno(ring, false), seqno) || \ |
604dd3ec | 2007 | atomic_read(&dev_priv->mm.wedged)) |
5c81fe85 BW |
2008 | do { |
2009 | if (interruptible) | |
2010 | end = wait_event_interruptible_timeout(ring->irq_queue, | |
2011 | EXIT_COND, | |
2012 | timeout_jiffies); | |
2013 | else | |
2014 | end = wait_event_timeout(ring->irq_queue, EXIT_COND, | |
2015 | timeout_jiffies); | |
604dd3ec | 2016 | |
d6b2c790 DV |
2017 | ret = i915_gem_check_wedge(dev_priv, interruptible); |
2018 | if (ret) | |
2019 | end = ret; | |
5c81fe85 BW |
2020 | } while (end == 0 && wait_forever); |
2021 | ||
2022 | getrawmonotonic(&now); | |
604dd3ec BW |
2023 | |
2024 | ring->irq_put(ring); | |
2025 | trace_i915_gem_request_wait_end(ring, seqno); | |
2026 | #undef EXIT_COND | |
2027 | ||
5c81fe85 BW |
2028 | if (timeout) { |
2029 | struct timespec sleep_time = timespec_sub(now, before); | |
2030 | *timeout = timespec_sub(*timeout, sleep_time); | |
2031 | } | |
2032 | ||
2033 | switch (end) { | |
eeef9b38 | 2034 | case -EIO: |
5c81fe85 BW |
2035 | case -EAGAIN: /* Wedged */ |
2036 | case -ERESTARTSYS: /* Signal */ | |
2037 | return (int)end; | |
2038 | case 0: /* Timeout */ | |
2039 | if (timeout) | |
2040 | set_normalized_timespec(timeout, 0, 0); | |
2041 | return -ETIME; | |
2042 | default: /* Completed */ | |
2043 | WARN_ON(end < 0); /* We're not aware of other errors */ | |
2044 | return 0; | |
2045 | } | |
604dd3ec BW |
2046 | } |
2047 | ||
db53a302 CW |
2048 | /** |
2049 | * Waits for a sequence number to be signaled, and cleans up the | |
2050 | * request and object lists appropriately for that event. | |
2051 | */ | |
5a5a0c64 | 2052 | int |
199b2bc2 | 2053 | i915_wait_seqno(struct intel_ring_buffer *ring, uint32_t seqno) |
673a394b | 2054 | { |
db53a302 | 2055 | drm_i915_private_t *dev_priv = ring->dev->dev_private; |
673a394b EA |
2056 | int ret = 0; |
2057 | ||
2058 | BUG_ON(seqno == 0); | |
2059 | ||
d6b2c790 | 2060 | ret = i915_gem_check_wedge(dev_priv, dev_priv->mm.interruptible); |
b4aca010 BW |
2061 | if (ret) |
2062 | return ret; | |
3cce469c | 2063 | |
b4aca010 BW |
2064 | ret = i915_gem_check_olr(ring, seqno); |
2065 | if (ret) | |
2066 | return ret; | |
ffed1d09 | 2067 | |
5c81fe85 | 2068 | ret = __wait_seqno(ring, seqno, dev_priv->mm.interruptible, NULL); |
673a394b | 2069 | |
673a394b EA |
2070 | return ret; |
2071 | } | |
2072 | ||
673a394b EA |
2073 | /** |
2074 | * Ensures that all rendering to the object has completed and the object is | |
2075 | * safe to unbind from the GTT or access from the CPU. | |
2076 | */ | |
0201f1ec CW |
2077 | static __must_check int |
2078 | i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj, | |
2079 | bool readonly) | |
673a394b | 2080 | { |
0201f1ec | 2081 | u32 seqno; |
673a394b EA |
2082 | int ret; |
2083 | ||
673a394b EA |
2084 | /* If there is rendering queued on the buffer being evicted, wait for |
2085 | * it. | |
2086 | */ | |
0201f1ec CW |
2087 | if (readonly) |
2088 | seqno = obj->last_write_seqno; | |
2089 | else | |
2090 | seqno = obj->last_read_seqno; | |
2091 | if (seqno == 0) | |
2092 | return 0; | |
2093 | ||
2094 | ret = i915_wait_seqno(obj->ring, seqno); | |
2095 | if (ret) | |
2096 | return ret; | |
2097 | ||
2098 | /* Manually manage the write flush as we may have not yet retired | |
2099 | * the buffer. | |
2100 | */ | |
2101 | if (obj->last_write_seqno && | |
2102 | i915_seqno_passed(seqno, obj->last_write_seqno)) { | |
2103 | obj->last_write_seqno = 0; | |
2104 | obj->base.write_domain &= ~I915_GEM_GPU_DOMAINS; | |
673a394b EA |
2105 | } |
2106 | ||
0201f1ec | 2107 | i915_gem_retire_requests_ring(obj->ring); |
673a394b EA |
2108 | return 0; |
2109 | } | |
2110 | ||
30dfebf3 DV |
2111 | /** |
2112 | * Ensures that an object will eventually get non-busy by flushing any required | |
2113 | * write domains, emitting any outstanding lazy request and retiring and | |
2114 | * completed requests. | |
2115 | */ | |
2116 | static int | |
2117 | i915_gem_object_flush_active(struct drm_i915_gem_object *obj) | |
2118 | { | |
2119 | int ret; | |
2120 | ||
2121 | if (obj->active) { | |
0201f1ec | 2122 | ret = i915_gem_check_olr(obj->ring, obj->last_read_seqno); |
30dfebf3 DV |
2123 | if (ret) |
2124 | return ret; | |
0201f1ec | 2125 | |
30dfebf3 DV |
2126 | i915_gem_retire_requests_ring(obj->ring); |
2127 | } | |
2128 | ||
2129 | return 0; | |
2130 | } | |
2131 | ||
23ba4fd0 BW |
2132 | /** |
2133 | * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT | |
2134 | * @DRM_IOCTL_ARGS: standard ioctl arguments | |
2135 | * | |
2136 | * Returns 0 if successful, else an error is returned with the remaining time in | |
2137 | * the timeout parameter. | |
2138 | * -ETIME: object is still busy after timeout | |
2139 | * -ERESTARTSYS: signal interrupted the wait | |
2140 | * -ENONENT: object doesn't exist | |
2141 | * Also possible, but rare: | |
2142 | * -EAGAIN: GPU wedged | |
2143 | * -ENOMEM: damn | |
2144 | * -ENODEV: Internal IRQ fail | |
2145 | * -E?: The add request failed | |
2146 | * | |
2147 | * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any | |
2148 | * non-zero timeout parameter the wait ioctl will wait for the given number of | |
2149 | * nanoseconds on an object becoming unbusy. Since the wait itself does so | |
2150 | * without holding struct_mutex the object may become re-busied before this | |
2151 | * function completes. A similar but shorter * race condition exists in the busy | |
2152 | * ioctl | |
2153 | */ | |
2154 | int | |
2155 | i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file) | |
2156 | { | |
2157 | struct drm_i915_gem_wait *args = data; | |
2158 | struct drm_i915_gem_object *obj; | |
2159 | struct intel_ring_buffer *ring = NULL; | |
eac1f14f | 2160 | struct timespec timeout_stack, *timeout = NULL; |
23ba4fd0 BW |
2161 | u32 seqno = 0; |
2162 | int ret = 0; | |
2163 | ||
eac1f14f BW |
2164 | if (args->timeout_ns >= 0) { |
2165 | timeout_stack = ns_to_timespec(args->timeout_ns); | |
2166 | timeout = &timeout_stack; | |
2167 | } | |
23ba4fd0 BW |
2168 | |
2169 | ret = i915_mutex_lock_interruptible(dev); | |
2170 | if (ret) | |
2171 | return ret; | |
2172 | ||
2173 | obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->bo_handle)); | |
2174 | if (&obj->base == NULL) { | |
2175 | mutex_unlock(&dev->struct_mutex); | |
2176 | return -ENOENT; | |
2177 | } | |
2178 | ||
30dfebf3 DV |
2179 | /* Need to make sure the object gets inactive eventually. */ |
2180 | ret = i915_gem_object_flush_active(obj); | |
23ba4fd0 BW |
2181 | if (ret) |
2182 | goto out; | |
2183 | ||
2184 | if (obj->active) { | |
0201f1ec | 2185 | seqno = obj->last_read_seqno; |
23ba4fd0 BW |
2186 | ring = obj->ring; |
2187 | } | |
2188 | ||
2189 | if (seqno == 0) | |
2190 | goto out; | |
2191 | ||
23ba4fd0 BW |
2192 | /* Do this after OLR check to make sure we make forward progress polling |
2193 | * on this IOCTL with a 0 timeout (like busy ioctl) | |
2194 | */ | |
2195 | if (!args->timeout_ns) { | |
2196 | ret = -ETIME; | |
2197 | goto out; | |
2198 | } | |
2199 | ||
2200 | drm_gem_object_unreference(&obj->base); | |
2201 | mutex_unlock(&dev->struct_mutex); | |
2202 | ||
eac1f14f BW |
2203 | ret = __wait_seqno(ring, seqno, true, timeout); |
2204 | if (timeout) { | |
2205 | WARN_ON(!timespec_valid(timeout)); | |
2206 | args->timeout_ns = timespec_to_ns(timeout); | |
2207 | } | |
23ba4fd0 BW |
2208 | return ret; |
2209 | ||
2210 | out: | |
2211 | drm_gem_object_unreference(&obj->base); | |
2212 | mutex_unlock(&dev->struct_mutex); | |
2213 | return ret; | |
2214 | } | |
2215 | ||
5816d648 BW |
2216 | /** |
2217 | * i915_gem_object_sync - sync an object to a ring. | |
2218 | * | |
2219 | * @obj: object which may be in use on another ring. | |
2220 | * @to: ring we wish to use the object on. May be NULL. | |
2221 | * | |
2222 | * This code is meant to abstract object synchronization with the GPU. | |
2223 | * Calling with NULL implies synchronizing the object with the CPU | |
2224 | * rather than a particular GPU ring. | |
2225 | * | |
2226 | * Returns 0 if successful, else propagates up the lower layer error. | |
2227 | */ | |
2911a35b BW |
2228 | int |
2229 | i915_gem_object_sync(struct drm_i915_gem_object *obj, | |
2230 | struct intel_ring_buffer *to) | |
2231 | { | |
2232 | struct intel_ring_buffer *from = obj->ring; | |
2233 | u32 seqno; | |
2234 | int ret, idx; | |
2235 | ||
2236 | if (from == NULL || to == from) | |
2237 | return 0; | |
2238 | ||
5816d648 | 2239 | if (to == NULL || !i915_semaphore_is_enabled(obj->base.dev)) |
0201f1ec | 2240 | return i915_gem_object_wait_rendering(obj, false); |
2911a35b BW |
2241 | |
2242 | idx = intel_ring_sync_index(from, to); | |
2243 | ||
0201f1ec | 2244 | seqno = obj->last_read_seqno; |
2911a35b BW |
2245 | if (seqno <= from->sync_seqno[idx]) |
2246 | return 0; | |
2247 | ||
b4aca010 BW |
2248 | ret = i915_gem_check_olr(obj->ring, seqno); |
2249 | if (ret) | |
2250 | return ret; | |
2911a35b | 2251 | |
1500f7ea | 2252 | ret = to->sync_to(to, from, seqno); |
e3a5a225 BW |
2253 | if (!ret) |
2254 | from->sync_seqno[idx] = seqno; | |
2911a35b | 2255 | |
e3a5a225 | 2256 | return ret; |
2911a35b BW |
2257 | } |
2258 | ||
b5ffc9bc CW |
2259 | static void i915_gem_object_finish_gtt(struct drm_i915_gem_object *obj) |
2260 | { | |
2261 | u32 old_write_domain, old_read_domains; | |
2262 | ||
b5ffc9bc CW |
2263 | /* Act a barrier for all accesses through the GTT */ |
2264 | mb(); | |
2265 | ||
2266 | /* Force a pagefault for domain tracking on next user access */ | |
2267 | i915_gem_release_mmap(obj); | |
2268 | ||
b97c3d9c KP |
2269 | if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0) |
2270 | return; | |
2271 | ||
b5ffc9bc CW |
2272 | old_read_domains = obj->base.read_domains; |
2273 | old_write_domain = obj->base.write_domain; | |
2274 | ||
2275 | obj->base.read_domains &= ~I915_GEM_DOMAIN_GTT; | |
2276 | obj->base.write_domain &= ~I915_GEM_DOMAIN_GTT; | |
2277 | ||
2278 | trace_i915_gem_object_change_domain(obj, | |
2279 | old_read_domains, | |
2280 | old_write_domain); | |
2281 | } | |
2282 | ||
673a394b EA |
2283 | /** |
2284 | * Unbinds an object from the GTT aperture. | |
2285 | */ | |
0f973f27 | 2286 | int |
05394f39 | 2287 | i915_gem_object_unbind(struct drm_i915_gem_object *obj) |
673a394b | 2288 | { |
7bddb01f | 2289 | drm_i915_private_t *dev_priv = obj->base.dev->dev_private; |
673a394b EA |
2290 | int ret = 0; |
2291 | ||
05394f39 | 2292 | if (obj->gtt_space == NULL) |
673a394b EA |
2293 | return 0; |
2294 | ||
31d8d651 CW |
2295 | if (obj->pin_count) |
2296 | return -EBUSY; | |
673a394b | 2297 | |
c4670ad0 CW |
2298 | BUG_ON(obj->pages == NULL); |
2299 | ||
a8198eea | 2300 | ret = i915_gem_object_finish_gpu(obj); |
1488fc08 | 2301 | if (ret) |
a8198eea CW |
2302 | return ret; |
2303 | /* Continue on if we fail due to EIO, the GPU is hung so we | |
2304 | * should be safe and we need to cleanup or else we might | |
2305 | * cause memory corruption through use-after-free. | |
2306 | */ | |
2307 | ||
b5ffc9bc | 2308 | i915_gem_object_finish_gtt(obj); |
5323fd04 | 2309 | |
96b47b65 | 2310 | /* release the fence reg _after_ flushing */ |
d9e86c0e | 2311 | ret = i915_gem_object_put_fence(obj); |
1488fc08 | 2312 | if (ret) |
d9e86c0e | 2313 | return ret; |
96b47b65 | 2314 | |
db53a302 CW |
2315 | trace_i915_gem_object_unbind(obj); |
2316 | ||
74898d7e DV |
2317 | if (obj->has_global_gtt_mapping) |
2318 | i915_gem_gtt_unbind_object(obj); | |
7bddb01f DV |
2319 | if (obj->has_aliasing_ppgtt_mapping) { |
2320 | i915_ppgtt_unbind_object(dev_priv->mm.aliasing_ppgtt, obj); | |
2321 | obj->has_aliasing_ppgtt_mapping = 0; | |
2322 | } | |
74163907 | 2323 | i915_gem_gtt_finish_object(obj); |
7bddb01f | 2324 | |
6c085a72 CW |
2325 | list_del(&obj->mm_list); |
2326 | list_move_tail(&obj->gtt_list, &dev_priv->mm.unbound_list); | |
75e9e915 | 2327 | /* Avoid an unnecessary call to unbind on rebind. */ |
05394f39 | 2328 | obj->map_and_fenceable = true; |
673a394b | 2329 | |
05394f39 CW |
2330 | drm_mm_put_block(obj->gtt_space); |
2331 | obj->gtt_space = NULL; | |
2332 | obj->gtt_offset = 0; | |
673a394b | 2333 | |
6c085a72 | 2334 | return 0; |
673a394b EA |
2335 | } |
2336 | ||
b2da9fe5 | 2337 | static int i915_ring_idle(struct intel_ring_buffer *ring) |
a56ba56c | 2338 | { |
69c2fc89 | 2339 | if (list_empty(&ring->active_list)) |
64193406 CW |
2340 | return 0; |
2341 | ||
199b2bc2 | 2342 | return i915_wait_seqno(ring, i915_gem_next_request_seqno(ring)); |
a56ba56c CW |
2343 | } |
2344 | ||
b2da9fe5 | 2345 | int i915_gpu_idle(struct drm_device *dev) |
4df2faf4 DV |
2346 | { |
2347 | drm_i915_private_t *dev_priv = dev->dev_private; | |
b4519513 | 2348 | struct intel_ring_buffer *ring; |
1ec14ad3 | 2349 | int ret, i; |
4df2faf4 | 2350 | |
4df2faf4 | 2351 | /* Flush everything onto the inactive list. */ |
b4519513 CW |
2352 | for_each_ring(ring, dev_priv, i) { |
2353 | ret = i915_ring_idle(ring); | |
1ec14ad3 CW |
2354 | if (ret) |
2355 | return ret; | |
b4519513 | 2356 | |
f2ef6eb1 BW |
2357 | ret = i915_switch_context(ring, NULL, DEFAULT_CONTEXT_ID); |
2358 | if (ret) | |
2359 | return ret; | |
1ec14ad3 | 2360 | } |
4df2faf4 | 2361 | |
8a1a49f9 | 2362 | return 0; |
4df2faf4 DV |
2363 | } |
2364 | ||
9ce079e4 CW |
2365 | static void sandybridge_write_fence_reg(struct drm_device *dev, int reg, |
2366 | struct drm_i915_gem_object *obj) | |
4e901fdc | 2367 | { |
4e901fdc | 2368 | drm_i915_private_t *dev_priv = dev->dev_private; |
4e901fdc EA |
2369 | uint64_t val; |
2370 | ||
9ce079e4 CW |
2371 | if (obj) { |
2372 | u32 size = obj->gtt_space->size; | |
4e901fdc | 2373 | |
9ce079e4 CW |
2374 | val = (uint64_t)((obj->gtt_offset + size - 4096) & |
2375 | 0xfffff000) << 32; | |
2376 | val |= obj->gtt_offset & 0xfffff000; | |
2377 | val |= (uint64_t)((obj->stride / 128) - 1) << | |
2378 | SANDYBRIDGE_FENCE_PITCH_SHIFT; | |
4e901fdc | 2379 | |
9ce079e4 CW |
2380 | if (obj->tiling_mode == I915_TILING_Y) |
2381 | val |= 1 << I965_FENCE_TILING_Y_SHIFT; | |
2382 | val |= I965_FENCE_REG_VALID; | |
2383 | } else | |
2384 | val = 0; | |
c6642782 | 2385 | |
9ce079e4 CW |
2386 | I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + reg * 8, val); |
2387 | POSTING_READ(FENCE_REG_SANDYBRIDGE_0 + reg * 8); | |
4e901fdc EA |
2388 | } |
2389 | ||
9ce079e4 CW |
2390 | static void i965_write_fence_reg(struct drm_device *dev, int reg, |
2391 | struct drm_i915_gem_object *obj) | |
de151cf6 | 2392 | { |
de151cf6 | 2393 | drm_i915_private_t *dev_priv = dev->dev_private; |
de151cf6 JB |
2394 | uint64_t val; |
2395 | ||
9ce079e4 CW |
2396 | if (obj) { |
2397 | u32 size = obj->gtt_space->size; | |
de151cf6 | 2398 | |
9ce079e4 CW |
2399 | val = (uint64_t)((obj->gtt_offset + size - 4096) & |
2400 | 0xfffff000) << 32; | |
2401 | val |= obj->gtt_offset & 0xfffff000; | |
2402 | val |= ((obj->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT; | |
2403 | if (obj->tiling_mode == I915_TILING_Y) | |
2404 | val |= 1 << I965_FENCE_TILING_Y_SHIFT; | |
2405 | val |= I965_FENCE_REG_VALID; | |
2406 | } else | |
2407 | val = 0; | |
c6642782 | 2408 | |
9ce079e4 CW |
2409 | I915_WRITE64(FENCE_REG_965_0 + reg * 8, val); |
2410 | POSTING_READ(FENCE_REG_965_0 + reg * 8); | |
de151cf6 JB |
2411 | } |
2412 | ||
9ce079e4 CW |
2413 | static void i915_write_fence_reg(struct drm_device *dev, int reg, |
2414 | struct drm_i915_gem_object *obj) | |
de151cf6 | 2415 | { |
de151cf6 | 2416 | drm_i915_private_t *dev_priv = dev->dev_private; |
9ce079e4 | 2417 | u32 val; |
de151cf6 | 2418 | |
9ce079e4 CW |
2419 | if (obj) { |
2420 | u32 size = obj->gtt_space->size; | |
2421 | int pitch_val; | |
2422 | int tile_width; | |
c6642782 | 2423 | |
9ce079e4 CW |
2424 | WARN((obj->gtt_offset & ~I915_FENCE_START_MASK) || |
2425 | (size & -size) != size || | |
2426 | (obj->gtt_offset & (size - 1)), | |
2427 | "object 0x%08x [fenceable? %d] not 1M or pot-size (0x%08x) aligned\n", | |
2428 | obj->gtt_offset, obj->map_and_fenceable, size); | |
c6642782 | 2429 | |
9ce079e4 CW |
2430 | if (obj->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev)) |
2431 | tile_width = 128; | |
2432 | else | |
2433 | tile_width = 512; | |
2434 | ||
2435 | /* Note: pitch better be a power of two tile widths */ | |
2436 | pitch_val = obj->stride / tile_width; | |
2437 | pitch_val = ffs(pitch_val) - 1; | |
2438 | ||
2439 | val = obj->gtt_offset; | |
2440 | if (obj->tiling_mode == I915_TILING_Y) | |
2441 | val |= 1 << I830_FENCE_TILING_Y_SHIFT; | |
2442 | val |= I915_FENCE_SIZE_BITS(size); | |
2443 | val |= pitch_val << I830_FENCE_PITCH_SHIFT; | |
2444 | val |= I830_FENCE_REG_VALID; | |
2445 | } else | |
2446 | val = 0; | |
2447 | ||
2448 | if (reg < 8) | |
2449 | reg = FENCE_REG_830_0 + reg * 4; | |
2450 | else | |
2451 | reg = FENCE_REG_945_8 + (reg - 8) * 4; | |
2452 | ||
2453 | I915_WRITE(reg, val); | |
2454 | POSTING_READ(reg); | |
de151cf6 JB |
2455 | } |
2456 | ||
9ce079e4 CW |
2457 | static void i830_write_fence_reg(struct drm_device *dev, int reg, |
2458 | struct drm_i915_gem_object *obj) | |
de151cf6 | 2459 | { |
de151cf6 | 2460 | drm_i915_private_t *dev_priv = dev->dev_private; |
de151cf6 | 2461 | uint32_t val; |
de151cf6 | 2462 | |
9ce079e4 CW |
2463 | if (obj) { |
2464 | u32 size = obj->gtt_space->size; | |
2465 | uint32_t pitch_val; | |
de151cf6 | 2466 | |
9ce079e4 CW |
2467 | WARN((obj->gtt_offset & ~I830_FENCE_START_MASK) || |
2468 | (size & -size) != size || | |
2469 | (obj->gtt_offset & (size - 1)), | |
2470 | "object 0x%08x not 512K or pot-size 0x%08x aligned\n", | |
2471 | obj->gtt_offset, size); | |
e76a16de | 2472 | |
9ce079e4 CW |
2473 | pitch_val = obj->stride / 128; |
2474 | pitch_val = ffs(pitch_val) - 1; | |
de151cf6 | 2475 | |
9ce079e4 CW |
2476 | val = obj->gtt_offset; |
2477 | if (obj->tiling_mode == I915_TILING_Y) | |
2478 | val |= 1 << I830_FENCE_TILING_Y_SHIFT; | |
2479 | val |= I830_FENCE_SIZE_BITS(size); | |
2480 | val |= pitch_val << I830_FENCE_PITCH_SHIFT; | |
2481 | val |= I830_FENCE_REG_VALID; | |
2482 | } else | |
2483 | val = 0; | |
c6642782 | 2484 | |
9ce079e4 CW |
2485 | I915_WRITE(FENCE_REG_830_0 + reg * 4, val); |
2486 | POSTING_READ(FENCE_REG_830_0 + reg * 4); | |
2487 | } | |
2488 | ||
2489 | static void i915_gem_write_fence(struct drm_device *dev, int reg, | |
2490 | struct drm_i915_gem_object *obj) | |
2491 | { | |
2492 | switch (INTEL_INFO(dev)->gen) { | |
2493 | case 7: | |
2494 | case 6: sandybridge_write_fence_reg(dev, reg, obj); break; | |
2495 | case 5: | |
2496 | case 4: i965_write_fence_reg(dev, reg, obj); break; | |
2497 | case 3: i915_write_fence_reg(dev, reg, obj); break; | |
2498 | case 2: i830_write_fence_reg(dev, reg, obj); break; | |
2499 | default: break; | |
2500 | } | |
de151cf6 JB |
2501 | } |
2502 | ||
61050808 CW |
2503 | static inline int fence_number(struct drm_i915_private *dev_priv, |
2504 | struct drm_i915_fence_reg *fence) | |
2505 | { | |
2506 | return fence - dev_priv->fence_regs; | |
2507 | } | |
2508 | ||
2509 | static void i915_gem_object_update_fence(struct drm_i915_gem_object *obj, | |
2510 | struct drm_i915_fence_reg *fence, | |
2511 | bool enable) | |
2512 | { | |
2513 | struct drm_i915_private *dev_priv = obj->base.dev->dev_private; | |
2514 | int reg = fence_number(dev_priv, fence); | |
2515 | ||
2516 | i915_gem_write_fence(obj->base.dev, reg, enable ? obj : NULL); | |
2517 | ||
2518 | if (enable) { | |
2519 | obj->fence_reg = reg; | |
2520 | fence->obj = obj; | |
2521 | list_move_tail(&fence->lru_list, &dev_priv->mm.fence_list); | |
2522 | } else { | |
2523 | obj->fence_reg = I915_FENCE_REG_NONE; | |
2524 | fence->obj = NULL; | |
2525 | list_del_init(&fence->lru_list); | |
2526 | } | |
2527 | } | |
2528 | ||
d9e86c0e | 2529 | static int |
a360bb1a | 2530 | i915_gem_object_flush_fence(struct drm_i915_gem_object *obj) |
d9e86c0e | 2531 | { |
1c293ea3 | 2532 | if (obj->last_fenced_seqno) { |
86d5bc37 | 2533 | int ret = i915_wait_seqno(obj->ring, obj->last_fenced_seqno); |
18991845 CW |
2534 | if (ret) |
2535 | return ret; | |
d9e86c0e CW |
2536 | |
2537 | obj->last_fenced_seqno = 0; | |
d9e86c0e CW |
2538 | } |
2539 | ||
63256ec5 CW |
2540 | /* Ensure that all CPU reads are completed before installing a fence |
2541 | * and all writes before removing the fence. | |
2542 | */ | |
2543 | if (obj->base.read_domains & I915_GEM_DOMAIN_GTT) | |
2544 | mb(); | |
2545 | ||
86d5bc37 | 2546 | obj->fenced_gpu_access = false; |
d9e86c0e CW |
2547 | return 0; |
2548 | } | |
2549 | ||
2550 | int | |
2551 | i915_gem_object_put_fence(struct drm_i915_gem_object *obj) | |
2552 | { | |
61050808 | 2553 | struct drm_i915_private *dev_priv = obj->base.dev->dev_private; |
d9e86c0e CW |
2554 | int ret; |
2555 | ||
a360bb1a | 2556 | ret = i915_gem_object_flush_fence(obj); |
d9e86c0e CW |
2557 | if (ret) |
2558 | return ret; | |
2559 | ||
61050808 CW |
2560 | if (obj->fence_reg == I915_FENCE_REG_NONE) |
2561 | return 0; | |
d9e86c0e | 2562 | |
61050808 CW |
2563 | i915_gem_object_update_fence(obj, |
2564 | &dev_priv->fence_regs[obj->fence_reg], | |
2565 | false); | |
2566 | i915_gem_object_fence_lost(obj); | |
d9e86c0e CW |
2567 | |
2568 | return 0; | |
2569 | } | |
2570 | ||
2571 | static struct drm_i915_fence_reg * | |
a360bb1a | 2572 | i915_find_fence_reg(struct drm_device *dev) |
ae3db24a | 2573 | { |
ae3db24a | 2574 | struct drm_i915_private *dev_priv = dev->dev_private; |
8fe301ad | 2575 | struct drm_i915_fence_reg *reg, *avail; |
d9e86c0e | 2576 | int i; |
ae3db24a DV |
2577 | |
2578 | /* First try to find a free reg */ | |
d9e86c0e | 2579 | avail = NULL; |
ae3db24a DV |
2580 | for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) { |
2581 | reg = &dev_priv->fence_regs[i]; | |
2582 | if (!reg->obj) | |
d9e86c0e | 2583 | return reg; |
ae3db24a | 2584 | |
1690e1eb | 2585 | if (!reg->pin_count) |
d9e86c0e | 2586 | avail = reg; |
ae3db24a DV |
2587 | } |
2588 | ||
d9e86c0e CW |
2589 | if (avail == NULL) |
2590 | return NULL; | |
ae3db24a DV |
2591 | |
2592 | /* None available, try to steal one or wait for a user to finish */ | |
d9e86c0e | 2593 | list_for_each_entry(reg, &dev_priv->mm.fence_list, lru_list) { |
1690e1eb | 2594 | if (reg->pin_count) |
ae3db24a DV |
2595 | continue; |
2596 | ||
8fe301ad | 2597 | return reg; |
ae3db24a DV |
2598 | } |
2599 | ||
8fe301ad | 2600 | return NULL; |
ae3db24a DV |
2601 | } |
2602 | ||
de151cf6 | 2603 | /** |
9a5a53b3 | 2604 | * i915_gem_object_get_fence - set up fencing for an object |
de151cf6 JB |
2605 | * @obj: object to map through a fence reg |
2606 | * | |
2607 | * When mapping objects through the GTT, userspace wants to be able to write | |
2608 | * to them without having to worry about swizzling if the object is tiled. | |
de151cf6 JB |
2609 | * This function walks the fence regs looking for a free one for @obj, |
2610 | * stealing one if it can't find any. | |
2611 | * | |
2612 | * It then sets up the reg based on the object's properties: address, pitch | |
2613 | * and tiling format. | |
9a5a53b3 CW |
2614 | * |
2615 | * For an untiled surface, this removes any existing fence. | |
de151cf6 | 2616 | */ |
8c4b8c3f | 2617 | int |
06d98131 | 2618 | i915_gem_object_get_fence(struct drm_i915_gem_object *obj) |
de151cf6 | 2619 | { |
05394f39 | 2620 | struct drm_device *dev = obj->base.dev; |
79e53945 | 2621 | struct drm_i915_private *dev_priv = dev->dev_private; |
14415745 | 2622 | bool enable = obj->tiling_mode != I915_TILING_NONE; |
d9e86c0e | 2623 | struct drm_i915_fence_reg *reg; |
ae3db24a | 2624 | int ret; |
de151cf6 | 2625 | |
14415745 CW |
2626 | /* Have we updated the tiling parameters upon the object and so |
2627 | * will need to serialise the write to the associated fence register? | |
2628 | */ | |
5d82e3e6 | 2629 | if (obj->fence_dirty) { |
14415745 CW |
2630 | ret = i915_gem_object_flush_fence(obj); |
2631 | if (ret) | |
2632 | return ret; | |
2633 | } | |
9a5a53b3 | 2634 | |
d9e86c0e | 2635 | /* Just update our place in the LRU if our fence is getting reused. */ |
05394f39 CW |
2636 | if (obj->fence_reg != I915_FENCE_REG_NONE) { |
2637 | reg = &dev_priv->fence_regs[obj->fence_reg]; | |
5d82e3e6 | 2638 | if (!obj->fence_dirty) { |
14415745 CW |
2639 | list_move_tail(®->lru_list, |
2640 | &dev_priv->mm.fence_list); | |
2641 | return 0; | |
2642 | } | |
2643 | } else if (enable) { | |
2644 | reg = i915_find_fence_reg(dev); | |
2645 | if (reg == NULL) | |
2646 | return -EDEADLK; | |
d9e86c0e | 2647 | |
14415745 CW |
2648 | if (reg->obj) { |
2649 | struct drm_i915_gem_object *old = reg->obj; | |
2650 | ||
2651 | ret = i915_gem_object_flush_fence(old); | |
29c5a587 CW |
2652 | if (ret) |
2653 | return ret; | |
2654 | ||
14415745 | 2655 | i915_gem_object_fence_lost(old); |
29c5a587 | 2656 | } |
14415745 | 2657 | } else |
a09ba7fa | 2658 | return 0; |
a09ba7fa | 2659 | |
14415745 | 2660 | i915_gem_object_update_fence(obj, reg, enable); |
5d82e3e6 | 2661 | obj->fence_dirty = false; |
14415745 | 2662 | |
9ce079e4 | 2663 | return 0; |
de151cf6 JB |
2664 | } |
2665 | ||
42d6ab48 CW |
2666 | static bool i915_gem_valid_gtt_space(struct drm_device *dev, |
2667 | struct drm_mm_node *gtt_space, | |
2668 | unsigned long cache_level) | |
2669 | { | |
2670 | struct drm_mm_node *other; | |
2671 | ||
2672 | /* On non-LLC machines we have to be careful when putting differing | |
2673 | * types of snoopable memory together to avoid the prefetcher | |
2674 | * crossing memory domains and dieing. | |
2675 | */ | |
2676 | if (HAS_LLC(dev)) | |
2677 | return true; | |
2678 | ||
2679 | if (gtt_space == NULL) | |
2680 | return true; | |
2681 | ||
2682 | if (list_empty(>t_space->node_list)) | |
2683 | return true; | |
2684 | ||
2685 | other = list_entry(gtt_space->node_list.prev, struct drm_mm_node, node_list); | |
2686 | if (other->allocated && !other->hole_follows && other->color != cache_level) | |
2687 | return false; | |
2688 | ||
2689 | other = list_entry(gtt_space->node_list.next, struct drm_mm_node, node_list); | |
2690 | if (other->allocated && !gtt_space->hole_follows && other->color != cache_level) | |
2691 | return false; | |
2692 | ||
2693 | return true; | |
2694 | } | |
2695 | ||
2696 | static void i915_gem_verify_gtt(struct drm_device *dev) | |
2697 | { | |
2698 | #if WATCH_GTT | |
2699 | struct drm_i915_private *dev_priv = dev->dev_private; | |
2700 | struct drm_i915_gem_object *obj; | |
2701 | int err = 0; | |
2702 | ||
2703 | list_for_each_entry(obj, &dev_priv->mm.gtt_list, gtt_list) { | |
2704 | if (obj->gtt_space == NULL) { | |
2705 | printk(KERN_ERR "object found on GTT list with no space reserved\n"); | |
2706 | err++; | |
2707 | continue; | |
2708 | } | |
2709 | ||
2710 | if (obj->cache_level != obj->gtt_space->color) { | |
2711 | printk(KERN_ERR "object reserved space [%08lx, %08lx] with wrong color, cache_level=%x, color=%lx\n", | |
2712 | obj->gtt_space->start, | |
2713 | obj->gtt_space->start + obj->gtt_space->size, | |
2714 | obj->cache_level, | |
2715 | obj->gtt_space->color); | |
2716 | err++; | |
2717 | continue; | |
2718 | } | |
2719 | ||
2720 | if (!i915_gem_valid_gtt_space(dev, | |
2721 | obj->gtt_space, | |
2722 | obj->cache_level)) { | |
2723 | printk(KERN_ERR "invalid GTT space found at [%08lx, %08lx] - color=%x\n", | |
2724 | obj->gtt_space->start, | |
2725 | obj->gtt_space->start + obj->gtt_space->size, | |
2726 | obj->cache_level); | |
2727 | err++; | |
2728 | continue; | |
2729 | } | |
2730 | } | |
2731 | ||
2732 | WARN_ON(err); | |
2733 | #endif | |
2734 | } | |
2735 | ||
673a394b EA |
2736 | /** |
2737 | * Finds free space in the GTT aperture and binds the object there. | |
2738 | */ | |
2739 | static int | |
05394f39 | 2740 | i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj, |
920afa77 | 2741 | unsigned alignment, |
75e9e915 | 2742 | bool map_and_fenceable) |
673a394b | 2743 | { |
05394f39 | 2744 | struct drm_device *dev = obj->base.dev; |
673a394b | 2745 | drm_i915_private_t *dev_priv = dev->dev_private; |
673a394b | 2746 | struct drm_mm_node *free_space; |
5e783301 | 2747 | u32 size, fence_size, fence_alignment, unfenced_alignment; |
75e9e915 | 2748 | bool mappable, fenceable; |
07f73f69 | 2749 | int ret; |
673a394b | 2750 | |
05394f39 | 2751 | if (obj->madv != I915_MADV_WILLNEED) { |
3ef94daa CW |
2752 | DRM_ERROR("Attempting to bind a purgeable object\n"); |
2753 | return -EINVAL; | |
2754 | } | |
2755 | ||
e28f8711 CW |
2756 | fence_size = i915_gem_get_gtt_size(dev, |
2757 | obj->base.size, | |
2758 | obj->tiling_mode); | |
2759 | fence_alignment = i915_gem_get_gtt_alignment(dev, | |
2760 | obj->base.size, | |
2761 | obj->tiling_mode); | |
2762 | unfenced_alignment = | |
2763 | i915_gem_get_unfenced_gtt_alignment(dev, | |
2764 | obj->base.size, | |
2765 | obj->tiling_mode); | |
a00b10c3 | 2766 | |
673a394b | 2767 | if (alignment == 0) |
5e783301 DV |
2768 | alignment = map_and_fenceable ? fence_alignment : |
2769 | unfenced_alignment; | |
75e9e915 | 2770 | if (map_and_fenceable && alignment & (fence_alignment - 1)) { |
673a394b EA |
2771 | DRM_ERROR("Invalid object alignment requested %u\n", alignment); |
2772 | return -EINVAL; | |
2773 | } | |
2774 | ||
05394f39 | 2775 | size = map_and_fenceable ? fence_size : obj->base.size; |
a00b10c3 | 2776 | |
654fc607 CW |
2777 | /* If the object is bigger than the entire aperture, reject it early |
2778 | * before evicting everything in a vain attempt to find space. | |
2779 | */ | |
05394f39 | 2780 | if (obj->base.size > |
75e9e915 | 2781 | (map_and_fenceable ? dev_priv->mm.gtt_mappable_end : dev_priv->mm.gtt_total)) { |
654fc607 CW |
2782 | DRM_ERROR("Attempting to bind an object larger than the aperture\n"); |
2783 | return -E2BIG; | |
2784 | } | |
2785 | ||
6c085a72 CW |
2786 | ret = i915_gem_object_get_pages_gtt(obj); |
2787 | if (ret) | |
2788 | return ret; | |
2789 | ||
673a394b | 2790 | search_free: |
75e9e915 | 2791 | if (map_and_fenceable) |
920afa77 | 2792 | free_space = |
42d6ab48 CW |
2793 | drm_mm_search_free_in_range_color(&dev_priv->mm.gtt_space, |
2794 | size, alignment, obj->cache_level, | |
2795 | 0, dev_priv->mm.gtt_mappable_end, | |
2796 | false); | |
920afa77 | 2797 | else |
42d6ab48 CW |
2798 | free_space = drm_mm_search_free_color(&dev_priv->mm.gtt_space, |
2799 | size, alignment, obj->cache_level, | |
2800 | false); | |
920afa77 DV |
2801 | |
2802 | if (free_space != NULL) { | |
75e9e915 | 2803 | if (map_and_fenceable) |
05394f39 | 2804 | obj->gtt_space = |
920afa77 | 2805 | drm_mm_get_block_range_generic(free_space, |
42d6ab48 | 2806 | size, alignment, obj->cache_level, |
6b9d89b4 | 2807 | 0, dev_priv->mm.gtt_mappable_end, |
42d6ab48 | 2808 | false); |
920afa77 | 2809 | else |
05394f39 | 2810 | obj->gtt_space = |
42d6ab48 CW |
2811 | drm_mm_get_block_generic(free_space, |
2812 | size, alignment, obj->cache_level, | |
2813 | false); | |
920afa77 | 2814 | } |
05394f39 | 2815 | if (obj->gtt_space == NULL) { |
75e9e915 | 2816 | ret = i915_gem_evict_something(dev, size, alignment, |
42d6ab48 | 2817 | obj->cache_level, |
75e9e915 | 2818 | map_and_fenceable); |
9731129c | 2819 | if (ret) |
673a394b | 2820 | return ret; |
9731129c | 2821 | |
673a394b EA |
2822 | goto search_free; |
2823 | } | |
42d6ab48 CW |
2824 | if (WARN_ON(!i915_gem_valid_gtt_space(dev, |
2825 | obj->gtt_space, | |
2826 | obj->cache_level))) { | |
2827 | drm_mm_put_block(obj->gtt_space); | |
2828 | obj->gtt_space = NULL; | |
2829 | return -EINVAL; | |
2830 | } | |
673a394b | 2831 | |
673a394b | 2832 | |
74163907 | 2833 | ret = i915_gem_gtt_prepare_object(obj); |
7c2e6fdf | 2834 | if (ret) { |
05394f39 CW |
2835 | drm_mm_put_block(obj->gtt_space); |
2836 | obj->gtt_space = NULL; | |
6c085a72 | 2837 | return ret; |
673a394b | 2838 | } |
673a394b | 2839 | |
0ebb9829 DV |
2840 | if (!dev_priv->mm.aliasing_ppgtt) |
2841 | i915_gem_gtt_bind_object(obj, obj->cache_level); | |
673a394b | 2842 | |
6c085a72 | 2843 | list_move_tail(&obj->gtt_list, &dev_priv->mm.bound_list); |
05394f39 | 2844 | list_add_tail(&obj->mm_list, &dev_priv->mm.inactive_list); |
bf1a1092 | 2845 | |
6299f992 | 2846 | obj->gtt_offset = obj->gtt_space->start; |
1c5d22f7 | 2847 | |
75e9e915 | 2848 | fenceable = |
05394f39 | 2849 | obj->gtt_space->size == fence_size && |
0206e353 | 2850 | (obj->gtt_space->start & (fence_alignment - 1)) == 0; |
a00b10c3 | 2851 | |
75e9e915 | 2852 | mappable = |
05394f39 | 2853 | obj->gtt_offset + obj->base.size <= dev_priv->mm.gtt_mappable_end; |
a00b10c3 | 2854 | |
05394f39 | 2855 | obj->map_and_fenceable = mappable && fenceable; |
75e9e915 | 2856 | |
db53a302 | 2857 | trace_i915_gem_object_bind(obj, map_and_fenceable); |
42d6ab48 | 2858 | i915_gem_verify_gtt(dev); |
673a394b EA |
2859 | return 0; |
2860 | } | |
2861 | ||
2862 | void | |
05394f39 | 2863 | i915_gem_clflush_object(struct drm_i915_gem_object *obj) |
673a394b | 2864 | { |
673a394b EA |
2865 | /* If we don't have a page list set up, then we're not pinned |
2866 | * to GPU, and we can ignore the cache flush because it'll happen | |
2867 | * again at bind time. | |
2868 | */ | |
05394f39 | 2869 | if (obj->pages == NULL) |
673a394b EA |
2870 | return; |
2871 | ||
9c23f7fc CW |
2872 | /* If the GPU is snooping the contents of the CPU cache, |
2873 | * we do not need to manually clear the CPU cache lines. However, | |
2874 | * the caches are only snooped when the render cache is | |
2875 | * flushed/invalidated. As we always have to emit invalidations | |
2876 | * and flushes when moving into and out of the RENDER domain, correct | |
2877 | * snooping behaviour occurs naturally as the result of our domain | |
2878 | * tracking. | |
2879 | */ | |
2880 | if (obj->cache_level != I915_CACHE_NONE) | |
2881 | return; | |
2882 | ||
1c5d22f7 | 2883 | trace_i915_gem_object_clflush(obj); |
cfa16a0d | 2884 | |
05394f39 | 2885 | drm_clflush_pages(obj->pages, obj->base.size / PAGE_SIZE); |
673a394b EA |
2886 | } |
2887 | ||
e47c68e9 EA |
2888 | /** Flushes the GTT write domain for the object if it's dirty. */ |
2889 | static void | |
05394f39 | 2890 | i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj) |
e47c68e9 | 2891 | { |
1c5d22f7 CW |
2892 | uint32_t old_write_domain; |
2893 | ||
05394f39 | 2894 | if (obj->base.write_domain != I915_GEM_DOMAIN_GTT) |
e47c68e9 EA |
2895 | return; |
2896 | ||
63256ec5 | 2897 | /* No actual flushing is required for the GTT write domain. Writes |
e47c68e9 EA |
2898 | * to it immediately go to main memory as far as we know, so there's |
2899 | * no chipset flush. It also doesn't land in render cache. | |
63256ec5 CW |
2900 | * |
2901 | * However, we do have to enforce the order so that all writes through | |
2902 | * the GTT land before any writes to the device, such as updates to | |
2903 | * the GATT itself. | |
e47c68e9 | 2904 | */ |
63256ec5 CW |
2905 | wmb(); |
2906 | ||
05394f39 CW |
2907 | old_write_domain = obj->base.write_domain; |
2908 | obj->base.write_domain = 0; | |
1c5d22f7 CW |
2909 | |
2910 | trace_i915_gem_object_change_domain(obj, | |
05394f39 | 2911 | obj->base.read_domains, |
1c5d22f7 | 2912 | old_write_domain); |
e47c68e9 EA |
2913 | } |
2914 | ||
2915 | /** Flushes the CPU write domain for the object if it's dirty. */ | |
2916 | static void | |
05394f39 | 2917 | i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj) |
e47c68e9 | 2918 | { |
1c5d22f7 | 2919 | uint32_t old_write_domain; |
e47c68e9 | 2920 | |
05394f39 | 2921 | if (obj->base.write_domain != I915_GEM_DOMAIN_CPU) |
e47c68e9 EA |
2922 | return; |
2923 | ||
2924 | i915_gem_clflush_object(obj); | |
40ce6575 | 2925 | intel_gtt_chipset_flush(); |
05394f39 CW |
2926 | old_write_domain = obj->base.write_domain; |
2927 | obj->base.write_domain = 0; | |
1c5d22f7 CW |
2928 | |
2929 | trace_i915_gem_object_change_domain(obj, | |
05394f39 | 2930 | obj->base.read_domains, |
1c5d22f7 | 2931 | old_write_domain); |
e47c68e9 EA |
2932 | } |
2933 | ||
2ef7eeaa EA |
2934 | /** |
2935 | * Moves a single object to the GTT read, and possibly write domain. | |
2936 | * | |
2937 | * This function returns when the move is complete, including waiting on | |
2938 | * flushes to occur. | |
2939 | */ | |
79e53945 | 2940 | int |
2021746e | 2941 | i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write) |
2ef7eeaa | 2942 | { |
8325a09d | 2943 | drm_i915_private_t *dev_priv = obj->base.dev->dev_private; |
1c5d22f7 | 2944 | uint32_t old_write_domain, old_read_domains; |
e47c68e9 | 2945 | int ret; |
2ef7eeaa | 2946 | |
02354392 | 2947 | /* Not valid to be called on unbound objects. */ |
05394f39 | 2948 | if (obj->gtt_space == NULL) |
02354392 EA |
2949 | return -EINVAL; |
2950 | ||
8d7e3de1 CW |
2951 | if (obj->base.write_domain == I915_GEM_DOMAIN_GTT) |
2952 | return 0; | |
2953 | ||
0201f1ec CW |
2954 | ret = i915_gem_object_wait_rendering(obj, !write); |
2955 | if (ret) | |
2956 | return ret; | |
2dafb1e0 | 2957 | |
7213342d | 2958 | i915_gem_object_flush_cpu_write_domain(obj); |
1c5d22f7 | 2959 | |
05394f39 CW |
2960 | old_write_domain = obj->base.write_domain; |
2961 | old_read_domains = obj->base.read_domains; | |
1c5d22f7 | 2962 | |
e47c68e9 EA |
2963 | /* It should now be out of any other write domains, and we can update |
2964 | * the domain values for our changes. | |
2965 | */ | |
05394f39 CW |
2966 | BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0); |
2967 | obj->base.read_domains |= I915_GEM_DOMAIN_GTT; | |
e47c68e9 | 2968 | if (write) { |
05394f39 CW |
2969 | obj->base.read_domains = I915_GEM_DOMAIN_GTT; |
2970 | obj->base.write_domain = I915_GEM_DOMAIN_GTT; | |
2971 | obj->dirty = 1; | |
2ef7eeaa EA |
2972 | } |
2973 | ||
1c5d22f7 CW |
2974 | trace_i915_gem_object_change_domain(obj, |
2975 | old_read_domains, | |
2976 | old_write_domain); | |
2977 | ||
8325a09d CW |
2978 | /* And bump the LRU for this access */ |
2979 | if (i915_gem_object_is_inactive(obj)) | |
2980 | list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list); | |
2981 | ||
e47c68e9 EA |
2982 | return 0; |
2983 | } | |
2984 | ||
e4ffd173 CW |
2985 | int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj, |
2986 | enum i915_cache_level cache_level) | |
2987 | { | |
7bddb01f DV |
2988 | struct drm_device *dev = obj->base.dev; |
2989 | drm_i915_private_t *dev_priv = dev->dev_private; | |
e4ffd173 CW |
2990 | int ret; |
2991 | ||
2992 | if (obj->cache_level == cache_level) | |
2993 | return 0; | |
2994 | ||
2995 | if (obj->pin_count) { | |
2996 | DRM_DEBUG("can not change the cache level of pinned objects\n"); | |
2997 | return -EBUSY; | |
2998 | } | |
2999 | ||
42d6ab48 CW |
3000 | if (!i915_gem_valid_gtt_space(dev, obj->gtt_space, cache_level)) { |
3001 | ret = i915_gem_object_unbind(obj); | |
3002 | if (ret) | |
3003 | return ret; | |
3004 | } | |
3005 | ||
e4ffd173 CW |
3006 | if (obj->gtt_space) { |
3007 | ret = i915_gem_object_finish_gpu(obj); | |
3008 | if (ret) | |
3009 | return ret; | |
3010 | ||
3011 | i915_gem_object_finish_gtt(obj); | |
3012 | ||
3013 | /* Before SandyBridge, you could not use tiling or fence | |
3014 | * registers with snooped memory, so relinquish any fences | |
3015 | * currently pointing to our region in the aperture. | |
3016 | */ | |
42d6ab48 | 3017 | if (INTEL_INFO(dev)->gen < 6) { |
e4ffd173 CW |
3018 | ret = i915_gem_object_put_fence(obj); |
3019 | if (ret) | |
3020 | return ret; | |
3021 | } | |
3022 | ||
74898d7e DV |
3023 | if (obj->has_global_gtt_mapping) |
3024 | i915_gem_gtt_bind_object(obj, cache_level); | |
7bddb01f DV |
3025 | if (obj->has_aliasing_ppgtt_mapping) |
3026 | i915_ppgtt_bind_object(dev_priv->mm.aliasing_ppgtt, | |
3027 | obj, cache_level); | |
42d6ab48 CW |
3028 | |
3029 | obj->gtt_space->color = cache_level; | |
e4ffd173 CW |
3030 | } |
3031 | ||
3032 | if (cache_level == I915_CACHE_NONE) { | |
3033 | u32 old_read_domains, old_write_domain; | |
3034 | ||
3035 | /* If we're coming from LLC cached, then we haven't | |
3036 | * actually been tracking whether the data is in the | |
3037 | * CPU cache or not, since we only allow one bit set | |
3038 | * in obj->write_domain and have been skipping the clflushes. | |
3039 | * Just set it to the CPU cache for now. | |
3040 | */ | |
3041 | WARN_ON(obj->base.write_domain & ~I915_GEM_DOMAIN_CPU); | |
3042 | WARN_ON(obj->base.read_domains & ~I915_GEM_DOMAIN_CPU); | |
3043 | ||
3044 | old_read_domains = obj->base.read_domains; | |
3045 | old_write_domain = obj->base.write_domain; | |
3046 | ||
3047 | obj->base.read_domains = I915_GEM_DOMAIN_CPU; | |
3048 | obj->base.write_domain = I915_GEM_DOMAIN_CPU; | |
3049 | ||
3050 | trace_i915_gem_object_change_domain(obj, | |
3051 | old_read_domains, | |
3052 | old_write_domain); | |
3053 | } | |
3054 | ||
3055 | obj->cache_level = cache_level; | |
42d6ab48 | 3056 | i915_gem_verify_gtt(dev); |
e4ffd173 CW |
3057 | return 0; |
3058 | } | |
3059 | ||
e6994aee CW |
3060 | int i915_gem_get_cacheing_ioctl(struct drm_device *dev, void *data, |
3061 | struct drm_file *file) | |
3062 | { | |
3063 | struct drm_i915_gem_cacheing *args = data; | |
3064 | struct drm_i915_gem_object *obj; | |
3065 | int ret; | |
3066 | ||
3067 | ret = i915_mutex_lock_interruptible(dev); | |
3068 | if (ret) | |
3069 | return ret; | |
3070 | ||
3071 | obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle)); | |
3072 | if (&obj->base == NULL) { | |
3073 | ret = -ENOENT; | |
3074 | goto unlock; | |
3075 | } | |
3076 | ||
3077 | args->cacheing = obj->cache_level != I915_CACHE_NONE; | |
3078 | ||
3079 | drm_gem_object_unreference(&obj->base); | |
3080 | unlock: | |
3081 | mutex_unlock(&dev->struct_mutex); | |
3082 | return ret; | |
3083 | } | |
3084 | ||
3085 | int i915_gem_set_cacheing_ioctl(struct drm_device *dev, void *data, | |
3086 | struct drm_file *file) | |
3087 | { | |
3088 | struct drm_i915_gem_cacheing *args = data; | |
3089 | struct drm_i915_gem_object *obj; | |
3090 | enum i915_cache_level level; | |
3091 | int ret; | |
3092 | ||
3093 | ret = i915_mutex_lock_interruptible(dev); | |
3094 | if (ret) | |
3095 | return ret; | |
3096 | ||
3097 | switch (args->cacheing) { | |
3098 | case I915_CACHEING_NONE: | |
3099 | level = I915_CACHE_NONE; | |
3100 | break; | |
3101 | case I915_CACHEING_CACHED: | |
3102 | level = I915_CACHE_LLC; | |
3103 | break; | |
3104 | default: | |
3105 | return -EINVAL; | |
3106 | } | |
3107 | ||
3108 | obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle)); | |
3109 | if (&obj->base == NULL) { | |
3110 | ret = -ENOENT; | |
3111 | goto unlock; | |
3112 | } | |
3113 | ||
3114 | ret = i915_gem_object_set_cache_level(obj, level); | |
3115 | ||
3116 | drm_gem_object_unreference(&obj->base); | |
3117 | unlock: | |
3118 | mutex_unlock(&dev->struct_mutex); | |
3119 | return ret; | |
3120 | } | |
3121 | ||
b9241ea3 | 3122 | /* |
2da3b9b9 CW |
3123 | * Prepare buffer for display plane (scanout, cursors, etc). |
3124 | * Can be called from an uninterruptible phase (modesetting) and allows | |
3125 | * any flushes to be pipelined (for pageflips). | |
b9241ea3 ZW |
3126 | */ |
3127 | int | |
2da3b9b9 CW |
3128 | i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj, |
3129 | u32 alignment, | |
919926ae | 3130 | struct intel_ring_buffer *pipelined) |
b9241ea3 | 3131 | { |
2da3b9b9 | 3132 | u32 old_read_domains, old_write_domain; |
b9241ea3 ZW |
3133 | int ret; |
3134 | ||
0be73284 | 3135 | if (pipelined != obj->ring) { |
2911a35b BW |
3136 | ret = i915_gem_object_sync(obj, pipelined); |
3137 | if (ret) | |
b9241ea3 ZW |
3138 | return ret; |
3139 | } | |
3140 | ||
a7ef0640 EA |
3141 | /* The display engine is not coherent with the LLC cache on gen6. As |
3142 | * a result, we make sure that the pinning that is about to occur is | |
3143 | * done with uncached PTEs. This is lowest common denominator for all | |
3144 | * chipsets. | |
3145 | * | |
3146 | * However for gen6+, we could do better by using the GFDT bit instead | |
3147 | * of uncaching, which would allow us to flush all the LLC-cached data | |
3148 | * with that bit in the PTE to main memory with just one PIPE_CONTROL. | |
3149 | */ | |
3150 | ret = i915_gem_object_set_cache_level(obj, I915_CACHE_NONE); | |
3151 | if (ret) | |
3152 | return ret; | |
3153 | ||
2da3b9b9 CW |
3154 | /* As the user may map the buffer once pinned in the display plane |
3155 | * (e.g. libkms for the bootup splash), we have to ensure that we | |
3156 | * always use map_and_fenceable for all scanout buffers. | |
3157 | */ | |
3158 | ret = i915_gem_object_pin(obj, alignment, true); | |
3159 | if (ret) | |
3160 | return ret; | |
3161 | ||
b118c1e3 CW |
3162 | i915_gem_object_flush_cpu_write_domain(obj); |
3163 | ||
2da3b9b9 | 3164 | old_write_domain = obj->base.write_domain; |
05394f39 | 3165 | old_read_domains = obj->base.read_domains; |
2da3b9b9 CW |
3166 | |
3167 | /* It should now be out of any other write domains, and we can update | |
3168 | * the domain values for our changes. | |
3169 | */ | |
e5f1d962 | 3170 | obj->base.write_domain = 0; |
05394f39 | 3171 | obj->base.read_domains |= I915_GEM_DOMAIN_GTT; |
b9241ea3 ZW |
3172 | |
3173 | trace_i915_gem_object_change_domain(obj, | |
3174 | old_read_domains, | |
2da3b9b9 | 3175 | old_write_domain); |
b9241ea3 ZW |
3176 | |
3177 | return 0; | |
3178 | } | |
3179 | ||
85345517 | 3180 | int |
a8198eea | 3181 | i915_gem_object_finish_gpu(struct drm_i915_gem_object *obj) |
85345517 | 3182 | { |
88241785 CW |
3183 | int ret; |
3184 | ||
a8198eea | 3185 | if ((obj->base.read_domains & I915_GEM_GPU_DOMAINS) == 0) |
85345517 CW |
3186 | return 0; |
3187 | ||
0201f1ec | 3188 | ret = i915_gem_object_wait_rendering(obj, false); |
c501ae7f CW |
3189 | if (ret) |
3190 | return ret; | |
3191 | ||
a8198eea CW |
3192 | /* Ensure that we invalidate the GPU's caches and TLBs. */ |
3193 | obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS; | |
c501ae7f | 3194 | return 0; |
85345517 CW |
3195 | } |
3196 | ||
e47c68e9 EA |
3197 | /** |
3198 | * Moves a single object to the CPU read, and possibly write domain. | |
3199 | * | |
3200 | * This function returns when the move is complete, including waiting on | |
3201 | * flushes to occur. | |
3202 | */ | |
dabdfe02 | 3203 | int |
919926ae | 3204 | i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write) |
e47c68e9 | 3205 | { |
1c5d22f7 | 3206 | uint32_t old_write_domain, old_read_domains; |
e47c68e9 EA |
3207 | int ret; |
3208 | ||
8d7e3de1 CW |
3209 | if (obj->base.write_domain == I915_GEM_DOMAIN_CPU) |
3210 | return 0; | |
3211 | ||
0201f1ec CW |
3212 | ret = i915_gem_object_wait_rendering(obj, !write); |
3213 | if (ret) | |
3214 | return ret; | |
2ef7eeaa | 3215 | |
e47c68e9 | 3216 | i915_gem_object_flush_gtt_write_domain(obj); |
2ef7eeaa | 3217 | |
05394f39 CW |
3218 | old_write_domain = obj->base.write_domain; |
3219 | old_read_domains = obj->base.read_domains; | |
1c5d22f7 | 3220 | |
e47c68e9 | 3221 | /* Flush the CPU cache if it's still invalid. */ |
05394f39 | 3222 | if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) { |
2ef7eeaa | 3223 | i915_gem_clflush_object(obj); |
2ef7eeaa | 3224 | |
05394f39 | 3225 | obj->base.read_domains |= I915_GEM_DOMAIN_CPU; |
2ef7eeaa EA |
3226 | } |
3227 | ||
3228 | /* It should now be out of any other write domains, and we can update | |
3229 | * the domain values for our changes. | |
3230 | */ | |
05394f39 | 3231 | BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0); |
e47c68e9 EA |
3232 | |
3233 | /* If we're writing through the CPU, then the GPU read domains will | |
3234 | * need to be invalidated at next use. | |
3235 | */ | |
3236 | if (write) { | |
05394f39 CW |
3237 | obj->base.read_domains = I915_GEM_DOMAIN_CPU; |
3238 | obj->base.write_domain = I915_GEM_DOMAIN_CPU; | |
e47c68e9 | 3239 | } |
2ef7eeaa | 3240 | |
1c5d22f7 CW |
3241 | trace_i915_gem_object_change_domain(obj, |
3242 | old_read_domains, | |
3243 | old_write_domain); | |
3244 | ||
2ef7eeaa EA |
3245 | return 0; |
3246 | } | |
3247 | ||
673a394b EA |
3248 | /* Throttle our rendering by waiting until the ring has completed our requests |
3249 | * emitted over 20 msec ago. | |
3250 | * | |
b962442e EA |
3251 | * Note that if we were to use the current jiffies each time around the loop, |
3252 | * we wouldn't escape the function with any frames outstanding if the time to | |
3253 | * render a frame was over 20ms. | |
3254 | * | |
673a394b EA |
3255 | * This should get us reasonable parallelism between CPU and GPU but also |
3256 | * relatively low latency when blocking on a particular request to finish. | |
3257 | */ | |
40a5f0de | 3258 | static int |
f787a5f5 | 3259 | i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file) |
40a5f0de | 3260 | { |
f787a5f5 CW |
3261 | struct drm_i915_private *dev_priv = dev->dev_private; |
3262 | struct drm_i915_file_private *file_priv = file->driver_priv; | |
b962442e | 3263 | unsigned long recent_enough = jiffies - msecs_to_jiffies(20); |
f787a5f5 CW |
3264 | struct drm_i915_gem_request *request; |
3265 | struct intel_ring_buffer *ring = NULL; | |
3266 | u32 seqno = 0; | |
3267 | int ret; | |
93533c29 | 3268 | |
e110e8d6 CW |
3269 | if (atomic_read(&dev_priv->mm.wedged)) |
3270 | return -EIO; | |
3271 | ||
1c25595f | 3272 | spin_lock(&file_priv->mm.lock); |
f787a5f5 | 3273 | list_for_each_entry(request, &file_priv->mm.request_list, client_list) { |
b962442e EA |
3274 | if (time_after_eq(request->emitted_jiffies, recent_enough)) |
3275 | break; | |
40a5f0de | 3276 | |
f787a5f5 CW |
3277 | ring = request->ring; |
3278 | seqno = request->seqno; | |
b962442e | 3279 | } |
1c25595f | 3280 | spin_unlock(&file_priv->mm.lock); |
40a5f0de | 3281 | |
f787a5f5 CW |
3282 | if (seqno == 0) |
3283 | return 0; | |
2bc43b5c | 3284 | |
5c81fe85 | 3285 | ret = __wait_seqno(ring, seqno, true, NULL); |
f787a5f5 CW |
3286 | if (ret == 0) |
3287 | queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0); | |
40a5f0de EA |
3288 | |
3289 | return ret; | |
3290 | } | |
3291 | ||
673a394b | 3292 | int |
05394f39 CW |
3293 | i915_gem_object_pin(struct drm_i915_gem_object *obj, |
3294 | uint32_t alignment, | |
75e9e915 | 3295 | bool map_and_fenceable) |
673a394b | 3296 | { |
673a394b EA |
3297 | int ret; |
3298 | ||
05394f39 | 3299 | BUG_ON(obj->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT); |
ac0c6b5a | 3300 | |
05394f39 CW |
3301 | if (obj->gtt_space != NULL) { |
3302 | if ((alignment && obj->gtt_offset & (alignment - 1)) || | |
3303 | (map_and_fenceable && !obj->map_and_fenceable)) { | |
3304 | WARN(obj->pin_count, | |
ae7d49d8 | 3305 | "bo is already pinned with incorrect alignment:" |
75e9e915 DV |
3306 | " offset=%x, req.alignment=%x, req.map_and_fenceable=%d," |
3307 | " obj->map_and_fenceable=%d\n", | |
05394f39 | 3308 | obj->gtt_offset, alignment, |
75e9e915 | 3309 | map_and_fenceable, |
05394f39 | 3310 | obj->map_and_fenceable); |
ac0c6b5a CW |
3311 | ret = i915_gem_object_unbind(obj); |
3312 | if (ret) | |
3313 | return ret; | |
3314 | } | |
3315 | } | |
3316 | ||
05394f39 | 3317 | if (obj->gtt_space == NULL) { |
a00b10c3 | 3318 | ret = i915_gem_object_bind_to_gtt(obj, alignment, |
75e9e915 | 3319 | map_and_fenceable); |
9731129c | 3320 | if (ret) |
673a394b | 3321 | return ret; |
22c344e9 | 3322 | } |
76446cac | 3323 | |
74898d7e DV |
3324 | if (!obj->has_global_gtt_mapping && map_and_fenceable) |
3325 | i915_gem_gtt_bind_object(obj, obj->cache_level); | |
3326 | ||
1b50247a | 3327 | obj->pin_count++; |
6299f992 | 3328 | obj->pin_mappable |= map_and_fenceable; |
673a394b EA |
3329 | |
3330 | return 0; | |
3331 | } | |
3332 | ||
3333 | void | |
05394f39 | 3334 | i915_gem_object_unpin(struct drm_i915_gem_object *obj) |
673a394b | 3335 | { |
05394f39 CW |
3336 | BUG_ON(obj->pin_count == 0); |
3337 | BUG_ON(obj->gtt_space == NULL); | |
673a394b | 3338 | |
1b50247a | 3339 | if (--obj->pin_count == 0) |
6299f992 | 3340 | obj->pin_mappable = false; |
673a394b EA |
3341 | } |
3342 | ||
3343 | int | |
3344 | i915_gem_pin_ioctl(struct drm_device *dev, void *data, | |
05394f39 | 3345 | struct drm_file *file) |
673a394b EA |
3346 | { |
3347 | struct drm_i915_gem_pin *args = data; | |
05394f39 | 3348 | struct drm_i915_gem_object *obj; |
673a394b EA |
3349 | int ret; |
3350 | ||
1d7cfea1 CW |
3351 | ret = i915_mutex_lock_interruptible(dev); |
3352 | if (ret) | |
3353 | return ret; | |
673a394b | 3354 | |
05394f39 | 3355 | obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle)); |
c8725226 | 3356 | if (&obj->base == NULL) { |
1d7cfea1 CW |
3357 | ret = -ENOENT; |
3358 | goto unlock; | |
673a394b | 3359 | } |
673a394b | 3360 | |
05394f39 | 3361 | if (obj->madv != I915_MADV_WILLNEED) { |
bb6baf76 | 3362 | DRM_ERROR("Attempting to pin a purgeable buffer\n"); |
1d7cfea1 CW |
3363 | ret = -EINVAL; |
3364 | goto out; | |
3ef94daa CW |
3365 | } |
3366 | ||
05394f39 | 3367 | if (obj->pin_filp != NULL && obj->pin_filp != file) { |
79e53945 JB |
3368 | DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n", |
3369 | args->handle); | |
1d7cfea1 CW |
3370 | ret = -EINVAL; |
3371 | goto out; | |
79e53945 JB |
3372 | } |
3373 | ||
05394f39 CW |
3374 | obj->user_pin_count++; |
3375 | obj->pin_filp = file; | |
3376 | if (obj->user_pin_count == 1) { | |
75e9e915 | 3377 | ret = i915_gem_object_pin(obj, args->alignment, true); |
1d7cfea1 CW |
3378 | if (ret) |
3379 | goto out; | |
673a394b EA |
3380 | } |
3381 | ||
3382 | /* XXX - flush the CPU caches for pinned objects | |
3383 | * as the X server doesn't manage domains yet | |
3384 | */ | |
e47c68e9 | 3385 | i915_gem_object_flush_cpu_write_domain(obj); |
05394f39 | 3386 | args->offset = obj->gtt_offset; |
1d7cfea1 | 3387 | out: |
05394f39 | 3388 | drm_gem_object_unreference(&obj->base); |
1d7cfea1 | 3389 | unlock: |
673a394b | 3390 | mutex_unlock(&dev->struct_mutex); |
1d7cfea1 | 3391 | return ret; |
673a394b EA |
3392 | } |
3393 | ||
3394 | int | |
3395 | i915_gem_unpin_ioctl(struct drm_device *dev, void *data, | |
05394f39 | 3396 | struct drm_file *file) |
673a394b EA |
3397 | { |
3398 | struct drm_i915_gem_pin *args = data; | |
05394f39 | 3399 | struct drm_i915_gem_object *obj; |
76c1dec1 | 3400 | int ret; |
673a394b | 3401 | |
1d7cfea1 CW |
3402 | ret = i915_mutex_lock_interruptible(dev); |
3403 | if (ret) | |
3404 | return ret; | |
673a394b | 3405 | |
05394f39 | 3406 | obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle)); |
c8725226 | 3407 | if (&obj->base == NULL) { |
1d7cfea1 CW |
3408 | ret = -ENOENT; |
3409 | goto unlock; | |
673a394b | 3410 | } |
76c1dec1 | 3411 | |
05394f39 | 3412 | if (obj->pin_filp != file) { |
79e53945 JB |
3413 | DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n", |
3414 | args->handle); | |
1d7cfea1 CW |
3415 | ret = -EINVAL; |
3416 | goto out; | |
79e53945 | 3417 | } |
05394f39 CW |
3418 | obj->user_pin_count--; |
3419 | if (obj->user_pin_count == 0) { | |
3420 | obj->pin_filp = NULL; | |
79e53945 JB |
3421 | i915_gem_object_unpin(obj); |
3422 | } | |
673a394b | 3423 | |
1d7cfea1 | 3424 | out: |
05394f39 | 3425 | drm_gem_object_unreference(&obj->base); |
1d7cfea1 | 3426 | unlock: |
673a394b | 3427 | mutex_unlock(&dev->struct_mutex); |
1d7cfea1 | 3428 | return ret; |
673a394b EA |
3429 | } |
3430 | ||
3431 | int | |
3432 | i915_gem_busy_ioctl(struct drm_device *dev, void *data, | |
05394f39 | 3433 | struct drm_file *file) |
673a394b EA |
3434 | { |
3435 | struct drm_i915_gem_busy *args = data; | |
05394f39 | 3436 | struct drm_i915_gem_object *obj; |
30dbf0c0 CW |
3437 | int ret; |
3438 | ||
76c1dec1 | 3439 | ret = i915_mutex_lock_interruptible(dev); |
1d7cfea1 | 3440 | if (ret) |
76c1dec1 | 3441 | return ret; |
673a394b | 3442 | |
05394f39 | 3443 | obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle)); |
c8725226 | 3444 | if (&obj->base == NULL) { |
1d7cfea1 CW |
3445 | ret = -ENOENT; |
3446 | goto unlock; | |
673a394b | 3447 | } |
d1b851fc | 3448 | |
0be555b6 CW |
3449 | /* Count all active objects as busy, even if they are currently not used |
3450 | * by the gpu. Users of this interface expect objects to eventually | |
3451 | * become non-busy without any further actions, therefore emit any | |
3452 | * necessary flushes here. | |
c4de0a5d | 3453 | */ |
30dfebf3 | 3454 | ret = i915_gem_object_flush_active(obj); |
0be555b6 | 3455 | |
30dfebf3 | 3456 | args->busy = obj->active; |
e9808edd CW |
3457 | if (obj->ring) { |
3458 | BUILD_BUG_ON(I915_NUM_RINGS > 16); | |
3459 | args->busy |= intel_ring_flag(obj->ring) << 16; | |
3460 | } | |
673a394b | 3461 | |
05394f39 | 3462 | drm_gem_object_unreference(&obj->base); |
1d7cfea1 | 3463 | unlock: |
673a394b | 3464 | mutex_unlock(&dev->struct_mutex); |
1d7cfea1 | 3465 | return ret; |
673a394b EA |
3466 | } |
3467 | ||
3468 | int | |
3469 | i915_gem_throttle_ioctl(struct drm_device *dev, void *data, | |
3470 | struct drm_file *file_priv) | |
3471 | { | |
0206e353 | 3472 | return i915_gem_ring_throttle(dev, file_priv); |
673a394b EA |
3473 | } |
3474 | ||
3ef94daa CW |
3475 | int |
3476 | i915_gem_madvise_ioctl(struct drm_device *dev, void *data, | |
3477 | struct drm_file *file_priv) | |
3478 | { | |
3479 | struct drm_i915_gem_madvise *args = data; | |
05394f39 | 3480 | struct drm_i915_gem_object *obj; |
76c1dec1 | 3481 | int ret; |
3ef94daa CW |
3482 | |
3483 | switch (args->madv) { | |
3484 | case I915_MADV_DONTNEED: | |
3485 | case I915_MADV_WILLNEED: | |
3486 | break; | |
3487 | default: | |
3488 | return -EINVAL; | |
3489 | } | |
3490 | ||
1d7cfea1 CW |
3491 | ret = i915_mutex_lock_interruptible(dev); |
3492 | if (ret) | |
3493 | return ret; | |
3494 | ||
05394f39 | 3495 | obj = to_intel_bo(drm_gem_object_lookup(dev, file_priv, args->handle)); |
c8725226 | 3496 | if (&obj->base == NULL) { |
1d7cfea1 CW |
3497 | ret = -ENOENT; |
3498 | goto unlock; | |
3ef94daa | 3499 | } |
3ef94daa | 3500 | |
05394f39 | 3501 | if (obj->pin_count) { |
1d7cfea1 CW |
3502 | ret = -EINVAL; |
3503 | goto out; | |
3ef94daa CW |
3504 | } |
3505 | ||
05394f39 CW |
3506 | if (obj->madv != __I915_MADV_PURGED) |
3507 | obj->madv = args->madv; | |
3ef94daa | 3508 | |
6c085a72 CW |
3509 | /* if the object is no longer attached, discard its backing storage */ |
3510 | if (i915_gem_object_is_purgeable(obj) && obj->pages == NULL) | |
2d7ef395 CW |
3511 | i915_gem_object_truncate(obj); |
3512 | ||
05394f39 | 3513 | args->retained = obj->madv != __I915_MADV_PURGED; |
bb6baf76 | 3514 | |
1d7cfea1 | 3515 | out: |
05394f39 | 3516 | drm_gem_object_unreference(&obj->base); |
1d7cfea1 | 3517 | unlock: |
3ef94daa | 3518 | mutex_unlock(&dev->struct_mutex); |
1d7cfea1 | 3519 | return ret; |
3ef94daa CW |
3520 | } |
3521 | ||
05394f39 CW |
3522 | struct drm_i915_gem_object *i915_gem_alloc_object(struct drm_device *dev, |
3523 | size_t size) | |
ac52bc56 | 3524 | { |
73aa808f | 3525 | struct drm_i915_private *dev_priv = dev->dev_private; |
c397b908 | 3526 | struct drm_i915_gem_object *obj; |
5949eac4 | 3527 | struct address_space *mapping; |
bed1ea95 | 3528 | u32 mask; |
ac52bc56 | 3529 | |
c397b908 DV |
3530 | obj = kzalloc(sizeof(*obj), GFP_KERNEL); |
3531 | if (obj == NULL) | |
3532 | return NULL; | |
673a394b | 3533 | |
c397b908 DV |
3534 | if (drm_gem_object_init(dev, &obj->base, size) != 0) { |
3535 | kfree(obj); | |
3536 | return NULL; | |
3537 | } | |
673a394b | 3538 | |
bed1ea95 CW |
3539 | mask = GFP_HIGHUSER | __GFP_RECLAIMABLE; |
3540 | if (IS_CRESTLINE(dev) || IS_BROADWATER(dev)) { | |
3541 | /* 965gm cannot relocate objects above 4GiB. */ | |
3542 | mask &= ~__GFP_HIGHMEM; | |
3543 | mask |= __GFP_DMA32; | |
3544 | } | |
3545 | ||
5949eac4 | 3546 | mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping; |
bed1ea95 | 3547 | mapping_set_gfp_mask(mapping, mask); |
5949eac4 | 3548 | |
73aa808f CW |
3549 | i915_gem_info_add_obj(dev_priv, size); |
3550 | ||
c397b908 DV |
3551 | obj->base.write_domain = I915_GEM_DOMAIN_CPU; |
3552 | obj->base.read_domains = I915_GEM_DOMAIN_CPU; | |
673a394b | 3553 | |
3d29b842 ED |
3554 | if (HAS_LLC(dev)) { |
3555 | /* On some devices, we can have the GPU use the LLC (the CPU | |
a1871112 EA |
3556 | * cache) for about a 10% performance improvement |
3557 | * compared to uncached. Graphics requests other than | |
3558 | * display scanout are coherent with the CPU in | |
3559 | * accessing this cache. This means in this mode we | |
3560 | * don't need to clflush on the CPU side, and on the | |
3561 | * GPU side we only need to flush internal caches to | |
3562 | * get data visible to the CPU. | |
3563 | * | |
3564 | * However, we maintain the display planes as UC, and so | |
3565 | * need to rebind when first used as such. | |
3566 | */ | |
3567 | obj->cache_level = I915_CACHE_LLC; | |
3568 | } else | |
3569 | obj->cache_level = I915_CACHE_NONE; | |
3570 | ||
62b8b215 | 3571 | obj->base.driver_private = NULL; |
c397b908 | 3572 | obj->fence_reg = I915_FENCE_REG_NONE; |
69dc4987 | 3573 | INIT_LIST_HEAD(&obj->mm_list); |
93a37f20 | 3574 | INIT_LIST_HEAD(&obj->gtt_list); |
69dc4987 | 3575 | INIT_LIST_HEAD(&obj->ring_list); |
432e58ed | 3576 | INIT_LIST_HEAD(&obj->exec_list); |
c397b908 | 3577 | obj->madv = I915_MADV_WILLNEED; |
75e9e915 DV |
3578 | /* Avoid an unnecessary call to unbind on the first bind. */ |
3579 | obj->map_and_fenceable = true; | |
de151cf6 | 3580 | |
05394f39 | 3581 | return obj; |
c397b908 DV |
3582 | } |
3583 | ||
3584 | int i915_gem_init_object(struct drm_gem_object *obj) | |
3585 | { | |
3586 | BUG(); | |
de151cf6 | 3587 | |
673a394b EA |
3588 | return 0; |
3589 | } | |
3590 | ||
1488fc08 | 3591 | void i915_gem_free_object(struct drm_gem_object *gem_obj) |
673a394b | 3592 | { |
1488fc08 | 3593 | struct drm_i915_gem_object *obj = to_intel_bo(gem_obj); |
05394f39 | 3594 | struct drm_device *dev = obj->base.dev; |
be72615b | 3595 | drm_i915_private_t *dev_priv = dev->dev_private; |
673a394b | 3596 | |
26e12f89 CW |
3597 | trace_i915_gem_object_destroy(obj); |
3598 | ||
1286ff73 DV |
3599 | if (gem_obj->import_attach) |
3600 | drm_prime_gem_destroy(gem_obj, obj->sg_table); | |
3601 | ||
1488fc08 CW |
3602 | if (obj->phys_obj) |
3603 | i915_gem_detach_phys_object(dev, obj); | |
3604 | ||
3605 | obj->pin_count = 0; | |
3606 | if (WARN_ON(i915_gem_object_unbind(obj) == -ERESTARTSYS)) { | |
3607 | bool was_interruptible; | |
3608 | ||
3609 | was_interruptible = dev_priv->mm.interruptible; | |
3610 | dev_priv->mm.interruptible = false; | |
3611 | ||
3612 | WARN_ON(i915_gem_object_unbind(obj)); | |
3613 | ||
3614 | dev_priv->mm.interruptible = was_interruptible; | |
3615 | } | |
3616 | ||
6c085a72 | 3617 | i915_gem_object_put_pages_gtt(obj); |
05394f39 | 3618 | if (obj->base.map_list.map) |
b464e9a2 | 3619 | drm_gem_free_mmap_offset(&obj->base); |
de151cf6 | 3620 | |
05394f39 CW |
3621 | drm_gem_object_release(&obj->base); |
3622 | i915_gem_info_remove_obj(dev_priv, obj->base.size); | |
c397b908 | 3623 | |
05394f39 CW |
3624 | kfree(obj->bit_17); |
3625 | kfree(obj); | |
673a394b EA |
3626 | } |
3627 | ||
29105ccc CW |
3628 | int |
3629 | i915_gem_idle(struct drm_device *dev) | |
3630 | { | |
3631 | drm_i915_private_t *dev_priv = dev->dev_private; | |
3632 | int ret; | |
28dfe52a | 3633 | |
29105ccc | 3634 | mutex_lock(&dev->struct_mutex); |
1c5d22f7 | 3635 | |
87acb0a5 | 3636 | if (dev_priv->mm.suspended) { |
29105ccc CW |
3637 | mutex_unlock(&dev->struct_mutex); |
3638 | return 0; | |
28dfe52a EA |
3639 | } |
3640 | ||
b2da9fe5 | 3641 | ret = i915_gpu_idle(dev); |
6dbe2772 KP |
3642 | if (ret) { |
3643 | mutex_unlock(&dev->struct_mutex); | |
673a394b | 3644 | return ret; |
6dbe2772 | 3645 | } |
b2da9fe5 | 3646 | i915_gem_retire_requests(dev); |
673a394b | 3647 | |
29105ccc | 3648 | /* Under UMS, be paranoid and evict. */ |
a39d7efc | 3649 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) |
6c085a72 | 3650 | i915_gem_evict_everything(dev); |
29105ccc | 3651 | |
312817a3 CW |
3652 | i915_gem_reset_fences(dev); |
3653 | ||
29105ccc CW |
3654 | /* Hack! Don't let anybody do execbuf while we don't control the chip. |
3655 | * We need to replace this with a semaphore, or something. | |
3656 | * And not confound mm.suspended! | |
3657 | */ | |
3658 | dev_priv->mm.suspended = 1; | |
bc0c7f14 | 3659 | del_timer_sync(&dev_priv->hangcheck_timer); |
29105ccc CW |
3660 | |
3661 | i915_kernel_lost_context(dev); | |
6dbe2772 | 3662 | i915_gem_cleanup_ringbuffer(dev); |
29105ccc | 3663 | |
6dbe2772 KP |
3664 | mutex_unlock(&dev->struct_mutex); |
3665 | ||
29105ccc CW |
3666 | /* Cancel the retire work handler, which should be idle now. */ |
3667 | cancel_delayed_work_sync(&dev_priv->mm.retire_work); | |
3668 | ||
673a394b EA |
3669 | return 0; |
3670 | } | |
3671 | ||
b9524a1e BW |
3672 | void i915_gem_l3_remap(struct drm_device *dev) |
3673 | { | |
3674 | drm_i915_private_t *dev_priv = dev->dev_private; | |
3675 | u32 misccpctl; | |
3676 | int i; | |
3677 | ||
3678 | if (!IS_IVYBRIDGE(dev)) | |
3679 | return; | |
3680 | ||
3681 | if (!dev_priv->mm.l3_remap_info) | |
3682 | return; | |
3683 | ||
3684 | misccpctl = I915_READ(GEN7_MISCCPCTL); | |
3685 | I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE); | |
3686 | POSTING_READ(GEN7_MISCCPCTL); | |
3687 | ||
3688 | for (i = 0; i < GEN7_L3LOG_SIZE; i += 4) { | |
3689 | u32 remap = I915_READ(GEN7_L3LOG_BASE + i); | |
3690 | if (remap && remap != dev_priv->mm.l3_remap_info[i/4]) | |
3691 | DRM_DEBUG("0x%x was already programmed to %x\n", | |
3692 | GEN7_L3LOG_BASE + i, remap); | |
3693 | if (remap && !dev_priv->mm.l3_remap_info[i/4]) | |
3694 | DRM_DEBUG_DRIVER("Clearing remapped register\n"); | |
3695 | I915_WRITE(GEN7_L3LOG_BASE + i, dev_priv->mm.l3_remap_info[i/4]); | |
3696 | } | |
3697 | ||
3698 | /* Make sure all the writes land before disabling dop clock gating */ | |
3699 | POSTING_READ(GEN7_L3LOG_BASE); | |
3700 | ||
3701 | I915_WRITE(GEN7_MISCCPCTL, misccpctl); | |
3702 | } | |
3703 | ||
f691e2f4 DV |
3704 | void i915_gem_init_swizzling(struct drm_device *dev) |
3705 | { | |
3706 | drm_i915_private_t *dev_priv = dev->dev_private; | |
3707 | ||
11782b02 | 3708 | if (INTEL_INFO(dev)->gen < 5 || |
f691e2f4 DV |
3709 | dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE) |
3710 | return; | |
3711 | ||
3712 | I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) | | |
3713 | DISP_TILE_SURFACE_SWIZZLING); | |
3714 | ||
11782b02 DV |
3715 | if (IS_GEN5(dev)) |
3716 | return; | |
3717 | ||
f691e2f4 DV |
3718 | I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL); |
3719 | if (IS_GEN6(dev)) | |
6b26c86d | 3720 | I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB)); |
f691e2f4 | 3721 | else |
6b26c86d | 3722 | I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB)); |
f691e2f4 | 3723 | } |
e21af88d DV |
3724 | |
3725 | void i915_gem_init_ppgtt(struct drm_device *dev) | |
3726 | { | |
3727 | drm_i915_private_t *dev_priv = dev->dev_private; | |
3728 | uint32_t pd_offset; | |
3729 | struct intel_ring_buffer *ring; | |
55a254ac DV |
3730 | struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt; |
3731 | uint32_t __iomem *pd_addr; | |
3732 | uint32_t pd_entry; | |
e21af88d DV |
3733 | int i; |
3734 | ||
3735 | if (!dev_priv->mm.aliasing_ppgtt) | |
3736 | return; | |
3737 | ||
55a254ac DV |
3738 | |
3739 | pd_addr = dev_priv->mm.gtt->gtt + ppgtt->pd_offset/sizeof(uint32_t); | |
3740 | for (i = 0; i < ppgtt->num_pd_entries; i++) { | |
3741 | dma_addr_t pt_addr; | |
3742 | ||
3743 | if (dev_priv->mm.gtt->needs_dmar) | |
3744 | pt_addr = ppgtt->pt_dma_addr[i]; | |
3745 | else | |
3746 | pt_addr = page_to_phys(ppgtt->pt_pages[i]); | |
3747 | ||
3748 | pd_entry = GEN6_PDE_ADDR_ENCODE(pt_addr); | |
3749 | pd_entry |= GEN6_PDE_VALID; | |
3750 | ||
3751 | writel(pd_entry, pd_addr + i); | |
3752 | } | |
3753 | readl(pd_addr); | |
3754 | ||
3755 | pd_offset = ppgtt->pd_offset; | |
e21af88d DV |
3756 | pd_offset /= 64; /* in cachelines, */ |
3757 | pd_offset <<= 16; | |
3758 | ||
3759 | if (INTEL_INFO(dev)->gen == 6) { | |
48ecfa10 DV |
3760 | uint32_t ecochk, gab_ctl, ecobits; |
3761 | ||
3762 | ecobits = I915_READ(GAC_ECO_BITS); | |
3763 | I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B); | |
be901a5a DV |
3764 | |
3765 | gab_ctl = I915_READ(GAB_CTL); | |
3766 | I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT); | |
3767 | ||
3768 | ecochk = I915_READ(GAM_ECOCHK); | |
e21af88d DV |
3769 | I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT | |
3770 | ECOCHK_PPGTT_CACHE64B); | |
6b26c86d | 3771 | I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE)); |
e21af88d DV |
3772 | } else if (INTEL_INFO(dev)->gen >= 7) { |
3773 | I915_WRITE(GAM_ECOCHK, ECOCHK_PPGTT_CACHE64B); | |
3774 | /* GFX_MODE is per-ring on gen7+ */ | |
3775 | } | |
3776 | ||
b4519513 | 3777 | for_each_ring(ring, dev_priv, i) { |
e21af88d DV |
3778 | if (INTEL_INFO(dev)->gen >= 7) |
3779 | I915_WRITE(RING_MODE_GEN7(ring), | |
6b26c86d | 3780 | _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE)); |
e21af88d DV |
3781 | |
3782 | I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G); | |
3783 | I915_WRITE(RING_PP_DIR_BASE(ring), pd_offset); | |
3784 | } | |
3785 | } | |
3786 | ||
67b1b571 CW |
3787 | static bool |
3788 | intel_enable_blt(struct drm_device *dev) | |
3789 | { | |
3790 | if (!HAS_BLT(dev)) | |
3791 | return false; | |
3792 | ||
3793 | /* The blitter was dysfunctional on early prototypes */ | |
3794 | if (IS_GEN6(dev) && dev->pdev->revision < 8) { | |
3795 | DRM_INFO("BLT not supported on this pre-production hardware;" | |
3796 | " graphics performance will be degraded.\n"); | |
3797 | return false; | |
3798 | } | |
3799 | ||
3800 | return true; | |
3801 | } | |
3802 | ||
8187a2b7 | 3803 | int |
f691e2f4 | 3804 | i915_gem_init_hw(struct drm_device *dev) |
8187a2b7 ZN |
3805 | { |
3806 | drm_i915_private_t *dev_priv = dev->dev_private; | |
3807 | int ret; | |
68f95ba9 | 3808 | |
8ecd1a66 DV |
3809 | if (!intel_enable_gtt()) |
3810 | return -EIO; | |
3811 | ||
b9524a1e BW |
3812 | i915_gem_l3_remap(dev); |
3813 | ||
f691e2f4 DV |
3814 | i915_gem_init_swizzling(dev); |
3815 | ||
5c1143bb | 3816 | ret = intel_init_render_ring_buffer(dev); |
68f95ba9 | 3817 | if (ret) |
b6913e4b | 3818 | return ret; |
68f95ba9 CW |
3819 | |
3820 | if (HAS_BSD(dev)) { | |
5c1143bb | 3821 | ret = intel_init_bsd_ring_buffer(dev); |
68f95ba9 CW |
3822 | if (ret) |
3823 | goto cleanup_render_ring; | |
d1b851fc | 3824 | } |
68f95ba9 | 3825 | |
67b1b571 | 3826 | if (intel_enable_blt(dev)) { |
549f7365 CW |
3827 | ret = intel_init_blt_ring_buffer(dev); |
3828 | if (ret) | |
3829 | goto cleanup_bsd_ring; | |
3830 | } | |
3831 | ||
6f392d54 CW |
3832 | dev_priv->next_seqno = 1; |
3833 | ||
254f965c BW |
3834 | /* |
3835 | * XXX: There was some w/a described somewhere suggesting loading | |
3836 | * contexts before PPGTT. | |
3837 | */ | |
3838 | i915_gem_context_init(dev); | |
e21af88d DV |
3839 | i915_gem_init_ppgtt(dev); |
3840 | ||
68f95ba9 CW |
3841 | return 0; |
3842 | ||
549f7365 | 3843 | cleanup_bsd_ring: |
1ec14ad3 | 3844 | intel_cleanup_ring_buffer(&dev_priv->ring[VCS]); |
68f95ba9 | 3845 | cleanup_render_ring: |
1ec14ad3 | 3846 | intel_cleanup_ring_buffer(&dev_priv->ring[RCS]); |
8187a2b7 ZN |
3847 | return ret; |
3848 | } | |
3849 | ||
1070a42b CW |
3850 | static bool |
3851 | intel_enable_ppgtt(struct drm_device *dev) | |
3852 | { | |
3853 | if (i915_enable_ppgtt >= 0) | |
3854 | return i915_enable_ppgtt; | |
3855 | ||
3856 | #ifdef CONFIG_INTEL_IOMMU | |
3857 | /* Disable ppgtt on SNB if VT-d is on. */ | |
3858 | if (INTEL_INFO(dev)->gen == 6 && intel_iommu_gfx_mapped) | |
3859 | return false; | |
3860 | #endif | |
3861 | ||
3862 | return true; | |
3863 | } | |
3864 | ||
3865 | int i915_gem_init(struct drm_device *dev) | |
3866 | { | |
3867 | struct drm_i915_private *dev_priv = dev->dev_private; | |
3868 | unsigned long gtt_size, mappable_size; | |
3869 | int ret; | |
3870 | ||
3871 | gtt_size = dev_priv->mm.gtt->gtt_total_entries << PAGE_SHIFT; | |
3872 | mappable_size = dev_priv->mm.gtt->gtt_mappable_entries << PAGE_SHIFT; | |
3873 | ||
3874 | mutex_lock(&dev->struct_mutex); | |
3875 | if (intel_enable_ppgtt(dev) && HAS_ALIASING_PPGTT(dev)) { | |
3876 | /* PPGTT pdes are stolen from global gtt ptes, so shrink the | |
3877 | * aperture accordingly when using aliasing ppgtt. */ | |
3878 | gtt_size -= I915_PPGTT_PD_ENTRIES*PAGE_SIZE; | |
3879 | ||
3880 | i915_gem_init_global_gtt(dev, 0, mappable_size, gtt_size); | |
3881 | ||
3882 | ret = i915_gem_init_aliasing_ppgtt(dev); | |
3883 | if (ret) { | |
3884 | mutex_unlock(&dev->struct_mutex); | |
3885 | return ret; | |
3886 | } | |
3887 | } else { | |
3888 | /* Let GEM Manage all of the aperture. | |
3889 | * | |
3890 | * However, leave one page at the end still bound to the scratch | |
3891 | * page. There are a number of places where the hardware | |
3892 | * apparently prefetches past the end of the object, and we've | |
3893 | * seen multiple hangs with the GPU head pointer stuck in a | |
3894 | * batchbuffer bound at the last page of the aperture. One page | |
3895 | * should be enough to keep any prefetching inside of the | |
3896 | * aperture. | |
3897 | */ | |
3898 | i915_gem_init_global_gtt(dev, 0, mappable_size, | |
3899 | gtt_size); | |
3900 | } | |
3901 | ||
3902 | ret = i915_gem_init_hw(dev); | |
3903 | mutex_unlock(&dev->struct_mutex); | |
3904 | if (ret) { | |
3905 | i915_gem_cleanup_aliasing_ppgtt(dev); | |
3906 | return ret; | |
3907 | } | |
3908 | ||
53ca26ca DV |
3909 | /* Allow hardware batchbuffers unless told otherwise, but not for KMS. */ |
3910 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) | |
3911 | dev_priv->dri1.allow_batchbuffer = 1; | |
1070a42b CW |
3912 | return 0; |
3913 | } | |
3914 | ||
8187a2b7 ZN |
3915 | void |
3916 | i915_gem_cleanup_ringbuffer(struct drm_device *dev) | |
3917 | { | |
3918 | drm_i915_private_t *dev_priv = dev->dev_private; | |
b4519513 | 3919 | struct intel_ring_buffer *ring; |
1ec14ad3 | 3920 | int i; |
8187a2b7 | 3921 | |
b4519513 CW |
3922 | for_each_ring(ring, dev_priv, i) |
3923 | intel_cleanup_ring_buffer(ring); | |
8187a2b7 ZN |
3924 | } |
3925 | ||
673a394b EA |
3926 | int |
3927 | i915_gem_entervt_ioctl(struct drm_device *dev, void *data, | |
3928 | struct drm_file *file_priv) | |
3929 | { | |
3930 | drm_i915_private_t *dev_priv = dev->dev_private; | |
b4519513 | 3931 | int ret; |
673a394b | 3932 | |
79e53945 JB |
3933 | if (drm_core_check_feature(dev, DRIVER_MODESET)) |
3934 | return 0; | |
3935 | ||
ba1234d1 | 3936 | if (atomic_read(&dev_priv->mm.wedged)) { |
673a394b | 3937 | DRM_ERROR("Reenabling wedged hardware, good luck\n"); |
ba1234d1 | 3938 | atomic_set(&dev_priv->mm.wedged, 0); |
673a394b EA |
3939 | } |
3940 | ||
673a394b | 3941 | mutex_lock(&dev->struct_mutex); |
9bb2d6f9 EA |
3942 | dev_priv->mm.suspended = 0; |
3943 | ||
f691e2f4 | 3944 | ret = i915_gem_init_hw(dev); |
d816f6ac WF |
3945 | if (ret != 0) { |
3946 | mutex_unlock(&dev->struct_mutex); | |
9bb2d6f9 | 3947 | return ret; |
d816f6ac | 3948 | } |
9bb2d6f9 | 3949 | |
69dc4987 | 3950 | BUG_ON(!list_empty(&dev_priv->mm.active_list)); |
673a394b | 3951 | BUG_ON(!list_empty(&dev_priv->mm.inactive_list)); |
673a394b | 3952 | mutex_unlock(&dev->struct_mutex); |
dbb19d30 | 3953 | |
5f35308b CW |
3954 | ret = drm_irq_install(dev); |
3955 | if (ret) | |
3956 | goto cleanup_ringbuffer; | |
dbb19d30 | 3957 | |
673a394b | 3958 | return 0; |
5f35308b CW |
3959 | |
3960 | cleanup_ringbuffer: | |
3961 | mutex_lock(&dev->struct_mutex); | |
3962 | i915_gem_cleanup_ringbuffer(dev); | |
3963 | dev_priv->mm.suspended = 1; | |
3964 | mutex_unlock(&dev->struct_mutex); | |
3965 | ||
3966 | return ret; | |
673a394b EA |
3967 | } |
3968 | ||
3969 | int | |
3970 | i915_gem_leavevt_ioctl(struct drm_device *dev, void *data, | |
3971 | struct drm_file *file_priv) | |
3972 | { | |
79e53945 JB |
3973 | if (drm_core_check_feature(dev, DRIVER_MODESET)) |
3974 | return 0; | |
3975 | ||
dbb19d30 | 3976 | drm_irq_uninstall(dev); |
e6890f6f | 3977 | return i915_gem_idle(dev); |
673a394b EA |
3978 | } |
3979 | ||
3980 | void | |
3981 | i915_gem_lastclose(struct drm_device *dev) | |
3982 | { | |
3983 | int ret; | |
673a394b | 3984 | |
e806b495 EA |
3985 | if (drm_core_check_feature(dev, DRIVER_MODESET)) |
3986 | return; | |
3987 | ||
6dbe2772 KP |
3988 | ret = i915_gem_idle(dev); |
3989 | if (ret) | |
3990 | DRM_ERROR("failed to idle hardware: %d\n", ret); | |
673a394b EA |
3991 | } |
3992 | ||
64193406 CW |
3993 | static void |
3994 | init_ring_lists(struct intel_ring_buffer *ring) | |
3995 | { | |
3996 | INIT_LIST_HEAD(&ring->active_list); | |
3997 | INIT_LIST_HEAD(&ring->request_list); | |
64193406 CW |
3998 | } |
3999 | ||
673a394b EA |
4000 | void |
4001 | i915_gem_load(struct drm_device *dev) | |
4002 | { | |
b5aa8a0f | 4003 | int i; |
673a394b EA |
4004 | drm_i915_private_t *dev_priv = dev->dev_private; |
4005 | ||
69dc4987 | 4006 | INIT_LIST_HEAD(&dev_priv->mm.active_list); |
673a394b | 4007 | INIT_LIST_HEAD(&dev_priv->mm.inactive_list); |
6c085a72 CW |
4008 | INIT_LIST_HEAD(&dev_priv->mm.unbound_list); |
4009 | INIT_LIST_HEAD(&dev_priv->mm.bound_list); | |
a09ba7fa | 4010 | INIT_LIST_HEAD(&dev_priv->mm.fence_list); |
1ec14ad3 CW |
4011 | for (i = 0; i < I915_NUM_RINGS; i++) |
4012 | init_ring_lists(&dev_priv->ring[i]); | |
4b9de737 | 4013 | for (i = 0; i < I915_MAX_NUM_FENCES; i++) |
007cc8ac | 4014 | INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list); |
673a394b EA |
4015 | INIT_DELAYED_WORK(&dev_priv->mm.retire_work, |
4016 | i915_gem_retire_work_handler); | |
30dbf0c0 | 4017 | init_completion(&dev_priv->error_completion); |
31169714 | 4018 | |
94400120 DA |
4019 | /* On GEN3 we really need to make sure the ARB C3 LP bit is set */ |
4020 | if (IS_GEN3(dev)) { | |
50743298 DV |
4021 | I915_WRITE(MI_ARB_STATE, |
4022 | _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE)); | |
94400120 DA |
4023 | } |
4024 | ||
72bfa19c CW |
4025 | dev_priv->relative_constants_mode = I915_EXEC_CONSTANTS_REL_GENERAL; |
4026 | ||
de151cf6 | 4027 | /* Old X drivers will take 0-2 for front, back, depth buffers */ |
b397c836 EA |
4028 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) |
4029 | dev_priv->fence_reg_start = 3; | |
de151cf6 | 4030 | |
a6c45cf0 | 4031 | if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) |
de151cf6 JB |
4032 | dev_priv->num_fence_regs = 16; |
4033 | else | |
4034 | dev_priv->num_fence_regs = 8; | |
4035 | ||
b5aa8a0f | 4036 | /* Initialize fence registers to zero */ |
ada726c7 | 4037 | i915_gem_reset_fences(dev); |
10ed13e4 | 4038 | |
673a394b | 4039 | i915_gem_detect_bit_6_swizzle(dev); |
6b95a207 | 4040 | init_waitqueue_head(&dev_priv->pending_flip_queue); |
17250b71 | 4041 | |
ce453d81 CW |
4042 | dev_priv->mm.interruptible = true; |
4043 | ||
17250b71 CW |
4044 | dev_priv->mm.inactive_shrinker.shrink = i915_gem_inactive_shrink; |
4045 | dev_priv->mm.inactive_shrinker.seeks = DEFAULT_SEEKS; | |
4046 | register_shrinker(&dev_priv->mm.inactive_shrinker); | |
673a394b | 4047 | } |
71acb5eb DA |
4048 | |
4049 | /* | |
4050 | * Create a physically contiguous memory object for this object | |
4051 | * e.g. for cursor + overlay regs | |
4052 | */ | |
995b6762 CW |
4053 | static int i915_gem_init_phys_object(struct drm_device *dev, |
4054 | int id, int size, int align) | |
71acb5eb DA |
4055 | { |
4056 | drm_i915_private_t *dev_priv = dev->dev_private; | |
4057 | struct drm_i915_gem_phys_object *phys_obj; | |
4058 | int ret; | |
4059 | ||
4060 | if (dev_priv->mm.phys_objs[id - 1] || !size) | |
4061 | return 0; | |
4062 | ||
9a298b2a | 4063 | phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL); |
71acb5eb DA |
4064 | if (!phys_obj) |
4065 | return -ENOMEM; | |
4066 | ||
4067 | phys_obj->id = id; | |
4068 | ||
6eeefaf3 | 4069 | phys_obj->handle = drm_pci_alloc(dev, size, align); |
71acb5eb DA |
4070 | if (!phys_obj->handle) { |
4071 | ret = -ENOMEM; | |
4072 | goto kfree_obj; | |
4073 | } | |
4074 | #ifdef CONFIG_X86 | |
4075 | set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE); | |
4076 | #endif | |
4077 | ||
4078 | dev_priv->mm.phys_objs[id - 1] = phys_obj; | |
4079 | ||
4080 | return 0; | |
4081 | kfree_obj: | |
9a298b2a | 4082 | kfree(phys_obj); |
71acb5eb DA |
4083 | return ret; |
4084 | } | |
4085 | ||
995b6762 | 4086 | static void i915_gem_free_phys_object(struct drm_device *dev, int id) |
71acb5eb DA |
4087 | { |
4088 | drm_i915_private_t *dev_priv = dev->dev_private; | |
4089 | struct drm_i915_gem_phys_object *phys_obj; | |
4090 | ||
4091 | if (!dev_priv->mm.phys_objs[id - 1]) | |
4092 | return; | |
4093 | ||
4094 | phys_obj = dev_priv->mm.phys_objs[id - 1]; | |
4095 | if (phys_obj->cur_obj) { | |
4096 | i915_gem_detach_phys_object(dev, phys_obj->cur_obj); | |
4097 | } | |
4098 | ||
4099 | #ifdef CONFIG_X86 | |
4100 | set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE); | |
4101 | #endif | |
4102 | drm_pci_free(dev, phys_obj->handle); | |
4103 | kfree(phys_obj); | |
4104 | dev_priv->mm.phys_objs[id - 1] = NULL; | |
4105 | } | |
4106 | ||
4107 | void i915_gem_free_all_phys_object(struct drm_device *dev) | |
4108 | { | |
4109 | int i; | |
4110 | ||
260883c8 | 4111 | for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++) |
71acb5eb DA |
4112 | i915_gem_free_phys_object(dev, i); |
4113 | } | |
4114 | ||
4115 | void i915_gem_detach_phys_object(struct drm_device *dev, | |
05394f39 | 4116 | struct drm_i915_gem_object *obj) |
71acb5eb | 4117 | { |
05394f39 | 4118 | struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping; |
e5281ccd | 4119 | char *vaddr; |
71acb5eb | 4120 | int i; |
71acb5eb DA |
4121 | int page_count; |
4122 | ||
05394f39 | 4123 | if (!obj->phys_obj) |
71acb5eb | 4124 | return; |
05394f39 | 4125 | vaddr = obj->phys_obj->handle->vaddr; |
71acb5eb | 4126 | |
05394f39 | 4127 | page_count = obj->base.size / PAGE_SIZE; |
71acb5eb | 4128 | for (i = 0; i < page_count; i++) { |
5949eac4 | 4129 | struct page *page = shmem_read_mapping_page(mapping, i); |
e5281ccd CW |
4130 | if (!IS_ERR(page)) { |
4131 | char *dst = kmap_atomic(page); | |
4132 | memcpy(dst, vaddr + i*PAGE_SIZE, PAGE_SIZE); | |
4133 | kunmap_atomic(dst); | |
4134 | ||
4135 | drm_clflush_pages(&page, 1); | |
4136 | ||
4137 | set_page_dirty(page); | |
4138 | mark_page_accessed(page); | |
4139 | page_cache_release(page); | |
4140 | } | |
71acb5eb | 4141 | } |
40ce6575 | 4142 | intel_gtt_chipset_flush(); |
d78b47b9 | 4143 | |
05394f39 CW |
4144 | obj->phys_obj->cur_obj = NULL; |
4145 | obj->phys_obj = NULL; | |
71acb5eb DA |
4146 | } |
4147 | ||
4148 | int | |
4149 | i915_gem_attach_phys_object(struct drm_device *dev, | |
05394f39 | 4150 | struct drm_i915_gem_object *obj, |
6eeefaf3 CW |
4151 | int id, |
4152 | int align) | |
71acb5eb | 4153 | { |
05394f39 | 4154 | struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping; |
71acb5eb | 4155 | drm_i915_private_t *dev_priv = dev->dev_private; |
71acb5eb DA |
4156 | int ret = 0; |
4157 | int page_count; | |
4158 | int i; | |
4159 | ||
4160 | if (id > I915_MAX_PHYS_OBJECT) | |
4161 | return -EINVAL; | |
4162 | ||
05394f39 CW |
4163 | if (obj->phys_obj) { |
4164 | if (obj->phys_obj->id == id) | |
71acb5eb DA |
4165 | return 0; |
4166 | i915_gem_detach_phys_object(dev, obj); | |
4167 | } | |
4168 | ||
71acb5eb DA |
4169 | /* create a new object */ |
4170 | if (!dev_priv->mm.phys_objs[id - 1]) { | |
4171 | ret = i915_gem_init_phys_object(dev, id, | |
05394f39 | 4172 | obj->base.size, align); |
71acb5eb | 4173 | if (ret) { |
05394f39 CW |
4174 | DRM_ERROR("failed to init phys object %d size: %zu\n", |
4175 | id, obj->base.size); | |
e5281ccd | 4176 | return ret; |
71acb5eb DA |
4177 | } |
4178 | } | |
4179 | ||
4180 | /* bind to the object */ | |
05394f39 CW |
4181 | obj->phys_obj = dev_priv->mm.phys_objs[id - 1]; |
4182 | obj->phys_obj->cur_obj = obj; | |
71acb5eb | 4183 | |
05394f39 | 4184 | page_count = obj->base.size / PAGE_SIZE; |
71acb5eb DA |
4185 | |
4186 | for (i = 0; i < page_count; i++) { | |
e5281ccd CW |
4187 | struct page *page; |
4188 | char *dst, *src; | |
4189 | ||
5949eac4 | 4190 | page = shmem_read_mapping_page(mapping, i); |
e5281ccd CW |
4191 | if (IS_ERR(page)) |
4192 | return PTR_ERR(page); | |
71acb5eb | 4193 | |
ff75b9bc | 4194 | src = kmap_atomic(page); |
05394f39 | 4195 | dst = obj->phys_obj->handle->vaddr + (i * PAGE_SIZE); |
71acb5eb | 4196 | memcpy(dst, src, PAGE_SIZE); |
3e4d3af5 | 4197 | kunmap_atomic(src); |
71acb5eb | 4198 | |
e5281ccd CW |
4199 | mark_page_accessed(page); |
4200 | page_cache_release(page); | |
4201 | } | |
d78b47b9 | 4202 | |
71acb5eb | 4203 | return 0; |
71acb5eb DA |
4204 | } |
4205 | ||
4206 | static int | |
05394f39 CW |
4207 | i915_gem_phys_pwrite(struct drm_device *dev, |
4208 | struct drm_i915_gem_object *obj, | |
71acb5eb DA |
4209 | struct drm_i915_gem_pwrite *args, |
4210 | struct drm_file *file_priv) | |
4211 | { | |
05394f39 | 4212 | void *vaddr = obj->phys_obj->handle->vaddr + args->offset; |
b47b30cc | 4213 | char __user *user_data = (char __user *) (uintptr_t) args->data_ptr; |
71acb5eb | 4214 | |
b47b30cc CW |
4215 | if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) { |
4216 | unsigned long unwritten; | |
4217 | ||
4218 | /* The physical object once assigned is fixed for the lifetime | |
4219 | * of the obj, so we can safely drop the lock and continue | |
4220 | * to access vaddr. | |
4221 | */ | |
4222 | mutex_unlock(&dev->struct_mutex); | |
4223 | unwritten = copy_from_user(vaddr, user_data, args->size); | |
4224 | mutex_lock(&dev->struct_mutex); | |
4225 | if (unwritten) | |
4226 | return -EFAULT; | |
4227 | } | |
71acb5eb | 4228 | |
40ce6575 | 4229 | intel_gtt_chipset_flush(); |
71acb5eb DA |
4230 | return 0; |
4231 | } | |
b962442e | 4232 | |
f787a5f5 | 4233 | void i915_gem_release(struct drm_device *dev, struct drm_file *file) |
b962442e | 4234 | { |
f787a5f5 | 4235 | struct drm_i915_file_private *file_priv = file->driver_priv; |
b962442e EA |
4236 | |
4237 | /* Clean up our request list when the client is going away, so that | |
4238 | * later retire_requests won't dereference our soon-to-be-gone | |
4239 | * file_priv. | |
4240 | */ | |
1c25595f | 4241 | spin_lock(&file_priv->mm.lock); |
f787a5f5 CW |
4242 | while (!list_empty(&file_priv->mm.request_list)) { |
4243 | struct drm_i915_gem_request *request; | |
4244 | ||
4245 | request = list_first_entry(&file_priv->mm.request_list, | |
4246 | struct drm_i915_gem_request, | |
4247 | client_list); | |
4248 | list_del(&request->client_list); | |
4249 | request->file_priv = NULL; | |
4250 | } | |
1c25595f | 4251 | spin_unlock(&file_priv->mm.lock); |
b962442e | 4252 | } |
31169714 | 4253 | |
31169714 | 4254 | static int |
1495f230 | 4255 | i915_gem_inactive_shrink(struct shrinker *shrinker, struct shrink_control *sc) |
31169714 | 4256 | { |
17250b71 CW |
4257 | struct drm_i915_private *dev_priv = |
4258 | container_of(shrinker, | |
4259 | struct drm_i915_private, | |
4260 | mm.inactive_shrinker); | |
4261 | struct drm_device *dev = dev_priv->dev; | |
6c085a72 | 4262 | struct drm_i915_gem_object *obj; |
1495f230 | 4263 | int nr_to_scan = sc->nr_to_scan; |
17250b71 CW |
4264 | int cnt; |
4265 | ||
4266 | if (!mutex_trylock(&dev->struct_mutex)) | |
bbe2e11a | 4267 | return 0; |
31169714 | 4268 | |
6c085a72 CW |
4269 | if (nr_to_scan) { |
4270 | nr_to_scan -= i915_gem_purge(dev_priv, nr_to_scan); | |
4271 | if (nr_to_scan > 0) | |
4272 | i915_gem_shrink_all(dev_priv); | |
31169714 CW |
4273 | } |
4274 | ||
17250b71 | 4275 | cnt = 0; |
6c085a72 CW |
4276 | list_for_each_entry(obj, &dev_priv->mm.unbound_list, gtt_list) |
4277 | cnt += obj->base.size >> PAGE_SHIFT; | |
4278 | list_for_each_entry(obj, &dev_priv->mm.bound_list, gtt_list) | |
4279 | if (obj->pin_count == 0) | |
4280 | cnt += obj->base.size >> PAGE_SHIFT; | |
17250b71 | 4281 | |
17250b71 | 4282 | mutex_unlock(&dev->struct_mutex); |
6c085a72 | 4283 | return cnt; |
31169714 | 4284 | } |