blob: 6dc832902f53fe26318788f9bed6522b457f5097 [file] [log] [blame]
Eric Anholt673a3942008-07-30 12:06:12 -07001/*
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"
Chris Wilson1c5d22f2009-08-25 11:15:50 +010032#include "i915_trace.h"
Jesse Barnes652c3932009-08-17 13:31:43 -070033#include "intel_drv.h"
Hugh Dickins5949eac2011-06-27 16:18:18 -070034#include <linux/shmem_fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
Eric Anholt673a3942008-07-30 12:06:12 -070036#include <linux/swap.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080037#include <linux/pci.h>
Eric Anholt673a3942008-07-30 12:06:12 -070038
Chris Wilson88241782011-01-07 17:09:48 +000039static __must_check int i915_gem_object_flush_gpu_write_domain(struct drm_i915_gem_object *obj);
Chris Wilson05394f32010-11-08 19:18:58 +000040static void i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj);
41static void i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj);
Chris Wilson88241782011-01-07 17:09:48 +000042static __must_check int i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
43 unsigned alignment,
44 bool map_and_fenceable);
Chris Wilsond9e86c02010-11-10 16:40:20 +000045static void i915_gem_clear_fence_reg(struct drm_device *dev,
46 struct drm_i915_fence_reg *reg);
Chris Wilson05394f32010-11-08 19:18:58 +000047static int i915_gem_phys_pwrite(struct drm_device *dev,
48 struct drm_i915_gem_object *obj,
Dave Airlie71acb5e2008-12-30 20:31:46 +100049 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +000050 struct drm_file *file);
51static void i915_gem_free_object_tail(struct drm_i915_gem_object *obj);
Eric Anholt673a3942008-07-30 12:06:12 -070052
Chris Wilson17250b72010-10-28 12:51:39 +010053static int i915_gem_inactive_shrink(struct shrinker *shrinker,
Ying Han1495f232011-05-24 17:12:27 -070054 struct shrink_control *sc);
Daniel Vetter8c599672011-12-14 13:57:31 +010055static void i915_gem_object_truncate(struct drm_i915_gem_object *obj);
Chris Wilson31169712009-09-14 16:50:28 +010056
Chris Wilson73aa8082010-09-30 11:46:12 +010057/* some bookkeeping */
58static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
59 size_t size)
60{
61 dev_priv->mm.object_count++;
62 dev_priv->mm.object_memory += size;
63}
64
65static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
66 size_t size)
67{
68 dev_priv->mm.object_count--;
69 dev_priv->mm.object_memory -= size;
70}
71
Chris Wilson21dd3732011-01-26 15:55:56 +000072static int
73i915_gem_wait_for_error(struct drm_device *dev)
Chris Wilson30dbf0c2010-09-25 10:19:17 +010074{
75 struct drm_i915_private *dev_priv = dev->dev_private;
76 struct completion *x = &dev_priv->error_completion;
77 unsigned long flags;
78 int ret;
79
80 if (!atomic_read(&dev_priv->mm.wedged))
81 return 0;
82
83 ret = wait_for_completion_interruptible(x);
84 if (ret)
85 return ret;
86
Chris Wilson21dd3732011-01-26 15:55:56 +000087 if (atomic_read(&dev_priv->mm.wedged)) {
88 /* GPU is hung, bump the completion count to account for
89 * the token we just consumed so that we never hit zero and
90 * end up waiting upon a subsequent completion event that
91 * will never happen.
92 */
93 spin_lock_irqsave(&x->wait.lock, flags);
94 x->done++;
95 spin_unlock_irqrestore(&x->wait.lock, flags);
96 }
97 return 0;
Chris Wilson30dbf0c2010-09-25 10:19:17 +010098}
99
Chris Wilson54cf91d2010-11-25 18:00:26 +0000100int i915_mutex_lock_interruptible(struct drm_device *dev)
Chris Wilson76c1dec2010-09-25 11:22:51 +0100101{
Chris Wilson76c1dec2010-09-25 11:22:51 +0100102 int ret;
103
Chris Wilson21dd3732011-01-26 15:55:56 +0000104 ret = i915_gem_wait_for_error(dev);
Chris Wilson76c1dec2010-09-25 11:22:51 +0100105 if (ret)
106 return ret;
107
108 ret = mutex_lock_interruptible(&dev->struct_mutex);
109 if (ret)
110 return ret;
111
Chris Wilson23bc5982010-09-29 16:10:57 +0100112 WARN_ON(i915_verify_lists(dev));
Chris Wilson76c1dec2010-09-25 11:22:51 +0100113 return 0;
114}
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100115
Chris Wilson7d1c4802010-08-07 21:45:03 +0100116static inline bool
Chris Wilson05394f32010-11-08 19:18:58 +0000117i915_gem_object_is_inactive(struct drm_i915_gem_object *obj)
Chris Wilson7d1c4802010-08-07 21:45:03 +0100118{
Chris Wilson05394f32010-11-08 19:18:58 +0000119 return obj->gtt_space && !obj->active && obj->pin_count == 0;
Chris Wilson7d1c4802010-08-07 21:45:03 +0100120}
121
Eric Anholt673a3942008-07-30 12:06:12 -0700122int
123i915_gem_init_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000124 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700125{
Eric Anholt673a3942008-07-30 12:06:12 -0700126 struct drm_i915_gem_init *args = data;
Chris Wilson20217462010-11-23 15:26:33 +0000127
128 if (args->gtt_start >= args->gtt_end ||
129 (args->gtt_end | args->gtt_start) & (PAGE_SIZE - 1))
130 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -0700131
132 mutex_lock(&dev->struct_mutex);
Daniel Vetter644ec022012-03-26 09:45:40 +0200133 i915_gem_init_global_gtt(dev, args->gtt_start,
134 args->gtt_end, args->gtt_end);
Eric Anholt673a3942008-07-30 12:06:12 -0700135 mutex_unlock(&dev->struct_mutex);
136
Chris Wilson20217462010-11-23 15:26:33 +0000137 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700138}
139
Eric Anholt5a125c32008-10-22 21:40:13 -0700140int
141i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000142 struct drm_file *file)
Eric Anholt5a125c32008-10-22 21:40:13 -0700143{
Chris Wilson73aa8082010-09-30 11:46:12 +0100144 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt5a125c32008-10-22 21:40:13 -0700145 struct drm_i915_gem_get_aperture *args = data;
Chris Wilson6299f992010-11-24 12:23:44 +0000146 struct drm_i915_gem_object *obj;
147 size_t pinned;
Eric Anholt5a125c32008-10-22 21:40:13 -0700148
149 if (!(dev->driver->driver_features & DRIVER_GEM))
150 return -ENODEV;
151
Chris Wilson6299f992010-11-24 12:23:44 +0000152 pinned = 0;
Chris Wilson73aa8082010-09-30 11:46:12 +0100153 mutex_lock(&dev->struct_mutex);
Chris Wilson6299f992010-11-24 12:23:44 +0000154 list_for_each_entry(obj, &dev_priv->mm.pinned_list, mm_list)
155 pinned += obj->gtt_space->size;
Chris Wilson73aa8082010-09-30 11:46:12 +0100156 mutex_unlock(&dev->struct_mutex);
Eric Anholt5a125c32008-10-22 21:40:13 -0700157
Chris Wilson6299f992010-11-24 12:23:44 +0000158 args->aper_size = dev_priv->mm.gtt_total;
Akshay Joshi0206e352011-08-16 15:34:10 -0400159 args->aper_available_size = args->aper_size - pinned;
Chris Wilson6299f992010-11-24 12:23:44 +0000160
Eric Anholt5a125c32008-10-22 21:40:13 -0700161 return 0;
162}
163
Dave Airlieff72145b2011-02-07 12:16:14 +1000164static int
165i915_gem_create(struct drm_file *file,
166 struct drm_device *dev,
167 uint64_t size,
168 uint32_t *handle_p)
Eric Anholt673a3942008-07-30 12:06:12 -0700169{
Chris Wilson05394f32010-11-08 19:18:58 +0000170 struct drm_i915_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300171 int ret;
172 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700173
Dave Airlieff72145b2011-02-07 12:16:14 +1000174 size = roundup(size, PAGE_SIZE);
Chris Wilson8ffc0242011-09-14 14:14:28 +0200175 if (size == 0)
176 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -0700177
178 /* Allocate the new object */
Dave Airlieff72145b2011-02-07 12:16:14 +1000179 obj = i915_gem_alloc_object(dev, size);
Eric Anholt673a3942008-07-30 12:06:12 -0700180 if (obj == NULL)
181 return -ENOMEM;
182
Chris Wilson05394f32010-11-08 19:18:58 +0000183 ret = drm_gem_handle_create(file, &obj->base, &handle);
Chris Wilson1dfd9752010-09-06 14:44:14 +0100184 if (ret) {
Chris Wilson05394f32010-11-08 19:18:58 +0000185 drm_gem_object_release(&obj->base);
186 i915_gem_info_remove_obj(dev->dev_private, obj->base.size);
Chris Wilson202f2fe2010-10-14 13:20:40 +0100187 kfree(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700188 return ret;
Chris Wilson1dfd9752010-09-06 14:44:14 +0100189 }
190
Chris Wilson202f2fe2010-10-14 13:20:40 +0100191 /* drop reference from allocate - handle holds it now */
Chris Wilson05394f32010-11-08 19:18:58 +0000192 drm_gem_object_unreference(&obj->base);
Chris Wilson202f2fe2010-10-14 13:20:40 +0100193 trace_i915_gem_object_create(obj);
194
Dave Airlieff72145b2011-02-07 12:16:14 +1000195 *handle_p = handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700196 return 0;
197}
198
Dave Airlieff72145b2011-02-07 12:16:14 +1000199int
200i915_gem_dumb_create(struct drm_file *file,
201 struct drm_device *dev,
202 struct drm_mode_create_dumb *args)
203{
204 /* have to work out size/pitch and return them */
Chris Wilsoned0291f2011-03-19 08:21:45 +0000205 args->pitch = ALIGN(args->width * ((args->bpp + 7) / 8), 64);
Dave Airlieff72145b2011-02-07 12:16:14 +1000206 args->size = args->pitch * args->height;
207 return i915_gem_create(file, dev,
208 args->size, &args->handle);
209}
210
211int i915_gem_dumb_destroy(struct drm_file *file,
212 struct drm_device *dev,
213 uint32_t handle)
214{
215 return drm_gem_handle_delete(file, handle);
216}
217
218/**
219 * Creates a new mm object and returns a handle to it.
220 */
221int
222i915_gem_create_ioctl(struct drm_device *dev, void *data,
223 struct drm_file *file)
224{
225 struct drm_i915_gem_create *args = data;
226 return i915_gem_create(file, dev,
227 args->size, &args->handle);
228}
229
Chris Wilson05394f32010-11-08 19:18:58 +0000230static int i915_gem_object_needs_bit17_swizzle(struct drm_i915_gem_object *obj)
Eric Anholt280b7132009-03-12 16:56:27 -0700231{
Chris Wilson05394f32010-11-08 19:18:58 +0000232 drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
Eric Anholt280b7132009-03-12 16:56:27 -0700233
234 return dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_9_10_17 &&
Chris Wilson05394f32010-11-08 19:18:58 +0000235 obj->tiling_mode != I915_TILING_NONE;
Eric Anholt280b7132009-03-12 16:56:27 -0700236}
237
Daniel Vetter8c599672011-12-14 13:57:31 +0100238static inline int
Daniel Vetter8461d222011-12-14 13:57:32 +0100239__copy_to_user_swizzled(char __user *cpu_vaddr,
240 const char *gpu_vaddr, int gpu_offset,
241 int length)
242{
243 int ret, cpu_offset = 0;
244
245 while (length > 0) {
246 int cacheline_end = ALIGN(gpu_offset + 1, 64);
247 int this_length = min(cacheline_end - gpu_offset, length);
248 int swizzled_gpu_offset = gpu_offset ^ 64;
249
250 ret = __copy_to_user(cpu_vaddr + cpu_offset,
251 gpu_vaddr + swizzled_gpu_offset,
252 this_length);
253 if (ret)
254 return ret + length;
255
256 cpu_offset += this_length;
257 gpu_offset += this_length;
258 length -= this_length;
259 }
260
261 return 0;
262}
263
264static inline int
Daniel Vetter8c599672011-12-14 13:57:31 +0100265__copy_from_user_swizzled(char __user *gpu_vaddr, int gpu_offset,
266 const char *cpu_vaddr,
267 int length)
268{
269 int ret, cpu_offset = 0;
270
271 while (length > 0) {
272 int cacheline_end = ALIGN(gpu_offset + 1, 64);
273 int this_length = min(cacheline_end - gpu_offset, length);
274 int swizzled_gpu_offset = gpu_offset ^ 64;
275
276 ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset,
277 cpu_vaddr + cpu_offset,
278 this_length);
279 if (ret)
280 return ret + length;
281
282 cpu_offset += this_length;
283 gpu_offset += this_length;
284 length -= this_length;
285 }
286
287 return 0;
288}
289
Daniel Vetterd174bd62012-03-25 19:47:40 +0200290/* Per-page copy function for the shmem pread fastpath.
291 * Flushes invalid cachelines before reading the target if
292 * needs_clflush is set. */
293static int
294shmem_pread_fast(struct page *page, int shmem_page_offset, int page_length,
295 char __user *user_data,
296 bool page_do_bit17_swizzling, bool needs_clflush)
297{
298 char *vaddr;
299 int ret;
300
301 if (page_do_bit17_swizzling)
302 return -EINVAL;
303
304 vaddr = kmap_atomic(page);
305 if (needs_clflush)
306 drm_clflush_virt_range(vaddr + shmem_page_offset,
307 page_length);
308 ret = __copy_to_user_inatomic(user_data,
309 vaddr + shmem_page_offset,
310 page_length);
311 kunmap_atomic(vaddr);
312
313 return ret;
314}
315
316/* Only difference to the fast-path function is that this can handle bit17
317 * and uses non-atomic copy and kmap functions. */
318static int
319shmem_pread_slow(struct page *page, int shmem_page_offset, int page_length,
320 char __user *user_data,
321 bool page_do_bit17_swizzling, bool needs_clflush)
322{
323 char *vaddr;
324 int ret;
325
326 vaddr = kmap(page);
327 if (needs_clflush)
328 drm_clflush_virt_range(vaddr + shmem_page_offset,
329 page_length);
330
331 if (page_do_bit17_swizzling)
332 ret = __copy_to_user_swizzled(user_data,
333 vaddr, shmem_page_offset,
334 page_length);
335 else
336 ret = __copy_to_user(user_data,
337 vaddr + shmem_page_offset,
338 page_length);
339 kunmap(page);
340
341 return ret;
342}
343
Eric Anholteb014592009-03-10 11:44:52 -0700344static int
Daniel Vetterdbf7bff2012-03-25 19:47:29 +0200345i915_gem_shmem_pread(struct drm_device *dev,
346 struct drm_i915_gem_object *obj,
347 struct drm_i915_gem_pread *args,
348 struct drm_file *file)
Eric Anholteb014592009-03-10 11:44:52 -0700349{
Chris Wilson05394f32010-11-08 19:18:58 +0000350 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Daniel Vetter8461d222011-12-14 13:57:32 +0100351 char __user *user_data;
Eric Anholteb014592009-03-10 11:44:52 -0700352 ssize_t remain;
Daniel Vetter8461d222011-12-14 13:57:32 +0100353 loff_t offset;
Ben Widawskyeb2c0c82012-02-15 14:42:43 +0100354 int shmem_page_offset, page_length, ret = 0;
Daniel Vetter8461d222011-12-14 13:57:32 +0100355 int obj_do_bit17_swizzling, page_do_bit17_swizzling;
Daniel Vetterdbf7bff2012-03-25 19:47:29 +0200356 int hit_slowpath = 0;
Daniel Vetter96d79b52012-03-25 19:47:36 +0200357 int prefaulted = 0;
Daniel Vetter84897312012-03-25 19:47:31 +0200358 int needs_clflush = 0;
Daniel Vetter692a5762012-03-25 19:47:34 +0200359 int release_page;
Eric Anholteb014592009-03-10 11:44:52 -0700360
Daniel Vetter8461d222011-12-14 13:57:32 +0100361 user_data = (char __user *) (uintptr_t) args->data_ptr;
Eric Anholteb014592009-03-10 11:44:52 -0700362 remain = args->size;
363
Daniel Vetter8461d222011-12-14 13:57:32 +0100364 obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700365
Daniel Vetter84897312012-03-25 19:47:31 +0200366 if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU)) {
367 /* If we're not in the cpu read domain, set ourself into the gtt
368 * read domain and manually flush cachelines (if required). This
369 * optimizes for the case when the gpu will dirty the data
370 * anyway again before the next pread happens. */
371 if (obj->cache_level == I915_CACHE_NONE)
372 needs_clflush = 1;
373 ret = i915_gem_object_set_to_gtt_domain(obj, false);
374 if (ret)
375 return ret;
376 }
377
Eric Anholteb014592009-03-10 11:44:52 -0700378 offset = args->offset;
379
380 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100381 struct page *page;
382
Eric Anholteb014592009-03-10 11:44:52 -0700383 /* Operation in this page
384 *
Eric Anholteb014592009-03-10 11:44:52 -0700385 * shmem_page_offset = offset within page in shmem file
Eric Anholteb014592009-03-10 11:44:52 -0700386 * page_length = bytes to copy for this page
387 */
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100388 shmem_page_offset = offset_in_page(offset);
Eric Anholteb014592009-03-10 11:44:52 -0700389 page_length = remain;
390 if ((shmem_page_offset + page_length) > PAGE_SIZE)
391 page_length = PAGE_SIZE - shmem_page_offset;
Eric Anholteb014592009-03-10 11:44:52 -0700392
Daniel Vetter692a5762012-03-25 19:47:34 +0200393 if (obj->pages) {
394 page = obj->pages[offset >> PAGE_SHIFT];
395 release_page = 0;
396 } else {
397 page = shmem_read_mapping_page(mapping, offset >> PAGE_SHIFT);
398 if (IS_ERR(page)) {
399 ret = PTR_ERR(page);
400 goto out;
401 }
402 release_page = 1;
Jesper Juhlb65552f2011-06-12 20:53:44 +0000403 }
Chris Wilsone5281cc2010-10-28 13:45:36 +0100404
Daniel Vetter8461d222011-12-14 13:57:32 +0100405 page_do_bit17_swizzling = obj_do_bit17_swizzling &&
406 (page_to_phys(page) & (1 << 17)) != 0;
407
Daniel Vetterd174bd62012-03-25 19:47:40 +0200408 ret = shmem_pread_fast(page, shmem_page_offset, page_length,
409 user_data, page_do_bit17_swizzling,
410 needs_clflush);
411 if (ret == 0)
412 goto next_page;
Daniel Vetterdbf7bff2012-03-25 19:47:29 +0200413
414 hit_slowpath = 1;
Daniel Vetter692a5762012-03-25 19:47:34 +0200415 page_cache_get(page);
Daniel Vetterdbf7bff2012-03-25 19:47:29 +0200416 mutex_unlock(&dev->struct_mutex);
417
Daniel Vetter96d79b52012-03-25 19:47:36 +0200418 if (!prefaulted) {
Daniel Vetterf56f8212012-03-25 19:47:41 +0200419 ret = fault_in_multipages_writeable(user_data, remain);
Daniel Vetter96d79b52012-03-25 19:47:36 +0200420 /* Userspace is tricking us, but we've already clobbered
421 * its pages with the prefault and promised to write the
422 * data up to the first fault. Hence ignore any errors
423 * and just continue. */
424 (void)ret;
425 prefaulted = 1;
426 }
427
Daniel Vetterd174bd62012-03-25 19:47:40 +0200428 ret = shmem_pread_slow(page, shmem_page_offset, page_length,
429 user_data, page_do_bit17_swizzling,
430 needs_clflush);
Eric Anholteb014592009-03-10 11:44:52 -0700431
Daniel Vetterdbf7bff2012-03-25 19:47:29 +0200432 mutex_lock(&dev->struct_mutex);
Daniel Vetter692a5762012-03-25 19:47:34 +0200433 page_cache_release(page);
Daniel Vetterdbf7bff2012-03-25 19:47:29 +0200434next_page:
Chris Wilsone5281cc2010-10-28 13:45:36 +0100435 mark_page_accessed(page);
Daniel Vetter692a5762012-03-25 19:47:34 +0200436 if (release_page)
437 page_cache_release(page);
Chris Wilsone5281cc2010-10-28 13:45:36 +0100438
Daniel Vetter8461d222011-12-14 13:57:32 +0100439 if (ret) {
440 ret = -EFAULT;
441 goto out;
442 }
443
Eric Anholteb014592009-03-10 11:44:52 -0700444 remain -= page_length;
Daniel Vetter8461d222011-12-14 13:57:32 +0100445 user_data += page_length;
Eric Anholteb014592009-03-10 11:44:52 -0700446 offset += page_length;
447 }
448
Chris Wilson4f27b752010-10-14 15:26:45 +0100449out:
Daniel Vetterdbf7bff2012-03-25 19:47:29 +0200450 if (hit_slowpath) {
451 /* Fixup: Kill any reinstated backing storage pages */
452 if (obj->madv == __I915_MADV_PURGED)
453 i915_gem_object_truncate(obj);
454 }
Eric Anholteb014592009-03-10 11:44:52 -0700455
456 return ret;
457}
458
Eric Anholt673a3942008-07-30 12:06:12 -0700459/**
460 * Reads data from the object referenced by handle.
461 *
462 * On error, the contents of *data are undefined.
463 */
464int
465i915_gem_pread_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000466 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700467{
468 struct drm_i915_gem_pread *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +0000469 struct drm_i915_gem_object *obj;
Chris Wilson35b62a82010-09-26 20:23:38 +0100470 int ret = 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700471
Chris Wilson51311d02010-11-17 09:10:42 +0000472 if (args->size == 0)
473 return 0;
474
475 if (!access_ok(VERIFY_WRITE,
476 (char __user *)(uintptr_t)args->data_ptr,
477 args->size))
478 return -EFAULT;
479
Chris Wilson4f27b752010-10-14 15:26:45 +0100480 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100481 if (ret)
Chris Wilson4f27b752010-10-14 15:26:45 +0100482 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700483
Chris Wilson05394f32010-11-08 19:18:58 +0000484 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +0000485 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100486 ret = -ENOENT;
487 goto unlock;
Chris Wilson4f27b752010-10-14 15:26:45 +0100488 }
Eric Anholt673a3942008-07-30 12:06:12 -0700489
Chris Wilson7dcd2492010-09-26 20:21:44 +0100490 /* Bounds check source. */
Chris Wilson05394f32010-11-08 19:18:58 +0000491 if (args->offset > obj->base.size ||
492 args->size > obj->base.size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +0100493 ret = -EINVAL;
Chris Wilson35b62a82010-09-26 20:23:38 +0100494 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +0100495 }
496
Chris Wilsondb53a302011-02-03 11:57:46 +0000497 trace_i915_gem_object_pread(obj, args->offset, args->size);
498
Daniel Vetterdbf7bff2012-03-25 19:47:29 +0200499 ret = i915_gem_shmem_pread(dev, obj, args, file);
Eric Anholt673a3942008-07-30 12:06:12 -0700500
Chris Wilson35b62a82010-09-26 20:23:38 +0100501out:
Chris Wilson05394f32010-11-08 19:18:58 +0000502 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100503unlock:
Chris Wilson4f27b752010-10-14 15:26:45 +0100504 mutex_unlock(&dev->struct_mutex);
Eric Anholteb014592009-03-10 11:44:52 -0700505 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700506}
507
Keith Packard0839ccb2008-10-30 19:38:48 -0700508/* This is the fast write path which cannot handle
509 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700510 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700511
Keith Packard0839ccb2008-10-30 19:38:48 -0700512static inline int
513fast_user_write(struct io_mapping *mapping,
514 loff_t page_base, int page_offset,
515 char __user *user_data,
516 int length)
517{
518 char *vaddr_atomic;
519 unsigned long unwritten;
520
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700521 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
Keith Packard0839ccb2008-10-30 19:38:48 -0700522 unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
523 user_data, length);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700524 io_mapping_unmap_atomic(vaddr_atomic);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100525 return unwritten;
Keith Packard0839ccb2008-10-30 19:38:48 -0700526}
527
Eric Anholt3de09aa2009-03-09 09:42:23 -0700528/**
529 * This is the fast pwrite path, where we copy the data directly from the
530 * user into the GTT, uncached.
531 */
Eric Anholt673a3942008-07-30 12:06:12 -0700532static int
Chris Wilson05394f32010-11-08 19:18:58 +0000533i915_gem_gtt_pwrite_fast(struct drm_device *dev,
534 struct drm_i915_gem_object *obj,
Eric Anholt3de09aa2009-03-09 09:42:23 -0700535 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000536 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700537{
Keith Packard0839ccb2008-10-30 19:38:48 -0700538 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700539 ssize_t remain;
Keith Packard0839ccb2008-10-30 19:38:48 -0700540 loff_t offset, page_base;
Eric Anholt673a3942008-07-30 12:06:12 -0700541 char __user *user_data;
Daniel Vetter935aaa62012-03-25 19:47:35 +0200542 int page_offset, page_length, ret;
543
544 ret = i915_gem_object_pin(obj, 0, true);
545 if (ret)
546 goto out;
547
548 ret = i915_gem_object_set_to_gtt_domain(obj, true);
549 if (ret)
550 goto out_unpin;
551
552 ret = i915_gem_object_put_fence(obj);
553 if (ret)
554 goto out_unpin;
Eric Anholt673a3942008-07-30 12:06:12 -0700555
556 user_data = (char __user *) (uintptr_t) args->data_ptr;
557 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700558
Chris Wilson05394f32010-11-08 19:18:58 +0000559 offset = obj->gtt_offset + args->offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700560
561 while (remain > 0) {
562 /* Operation in this page
563 *
Keith Packard0839ccb2008-10-30 19:38:48 -0700564 * page_base = page offset within aperture
565 * page_offset = offset within page
566 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -0700567 */
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100568 page_base = offset & PAGE_MASK;
569 page_offset = offset_in_page(offset);
Keith Packard0839ccb2008-10-30 19:38:48 -0700570 page_length = remain;
571 if ((page_offset + remain) > PAGE_SIZE)
572 page_length = PAGE_SIZE - page_offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700573
Keith Packard0839ccb2008-10-30 19:38:48 -0700574 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -0700575 * source page isn't available. Return the error and we'll
576 * retry in the slow path.
Keith Packard0839ccb2008-10-30 19:38:48 -0700577 */
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100578 if (fast_user_write(dev_priv->mm.gtt_mapping, page_base,
Daniel Vetter935aaa62012-03-25 19:47:35 +0200579 page_offset, user_data, page_length)) {
580 ret = -EFAULT;
581 goto out_unpin;
582 }
Eric Anholt673a3942008-07-30 12:06:12 -0700583
Keith Packard0839ccb2008-10-30 19:38:48 -0700584 remain -= page_length;
585 user_data += page_length;
586 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700587 }
Eric Anholt673a3942008-07-30 12:06:12 -0700588
Daniel Vetter935aaa62012-03-25 19:47:35 +0200589out_unpin:
590 i915_gem_object_unpin(obj);
591out:
Eric Anholt3de09aa2009-03-09 09:42:23 -0700592 return ret;
593}
594
Daniel Vetterd174bd62012-03-25 19:47:40 +0200595/* Per-page copy function for the shmem pwrite fastpath.
596 * Flushes invalid cachelines before writing to the target if
597 * needs_clflush_before is set and flushes out any written cachelines after
598 * writing if needs_clflush is set. */
599static int
600shmem_pwrite_fast(struct page *page, int shmem_page_offset, int page_length,
601 char __user *user_data,
602 bool page_do_bit17_swizzling,
603 bool needs_clflush_before,
604 bool needs_clflush_after)
605{
606 char *vaddr;
607 int ret;
608
609 if (page_do_bit17_swizzling)
610 return -EINVAL;
611
612 vaddr = kmap_atomic(page);
613 if (needs_clflush_before)
614 drm_clflush_virt_range(vaddr + shmem_page_offset,
615 page_length);
616 ret = __copy_from_user_inatomic_nocache(vaddr + shmem_page_offset,
617 user_data,
618 page_length);
619 if (needs_clflush_after)
620 drm_clflush_virt_range(vaddr + shmem_page_offset,
621 page_length);
622 kunmap_atomic(vaddr);
623
624 return ret;
625}
626
627/* Only difference to the fast-path function is that this can handle bit17
628 * and uses non-atomic copy and kmap functions. */
629static int
630shmem_pwrite_slow(struct page *page, int shmem_page_offset, int page_length,
631 char __user *user_data,
632 bool page_do_bit17_swizzling,
633 bool needs_clflush_before,
634 bool needs_clflush_after)
635{
636 char *vaddr;
637 int ret;
638
639 vaddr = kmap(page);
640 if (needs_clflush_before)
641 drm_clflush_virt_range(vaddr + shmem_page_offset,
642 page_length);
643 if (page_do_bit17_swizzling)
644 ret = __copy_from_user_swizzled(vaddr, shmem_page_offset,
645 user_data,
646 page_length);
647 else
648 ret = __copy_from_user(vaddr + shmem_page_offset,
649 user_data,
650 page_length);
651 if (needs_clflush_after)
652 drm_clflush_virt_range(vaddr + shmem_page_offset,
653 page_length);
654 kunmap(page);
655
656 return ret;
657}
658
Eric Anholt3043c602008-10-02 12:24:47 -0700659static int
Daniel Vettere244a442012-03-25 19:47:28 +0200660i915_gem_shmem_pwrite(struct drm_device *dev,
661 struct drm_i915_gem_object *obj,
662 struct drm_i915_gem_pwrite *args,
663 struct drm_file *file)
Eric Anholt40123c12009-03-09 13:42:30 -0700664{
Chris Wilson05394f32010-11-08 19:18:58 +0000665 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Eric Anholt40123c12009-03-09 13:42:30 -0700666 ssize_t remain;
Daniel Vetter8c599672011-12-14 13:57:31 +0100667 loff_t offset;
668 char __user *user_data;
Ben Widawskyeb2c0c82012-02-15 14:42:43 +0100669 int shmem_page_offset, page_length, ret = 0;
Daniel Vetter8c599672011-12-14 13:57:31 +0100670 int obj_do_bit17_swizzling, page_do_bit17_swizzling;
Daniel Vettere244a442012-03-25 19:47:28 +0200671 int hit_slowpath = 0;
Daniel Vetter58642882012-03-25 19:47:37 +0200672 int needs_clflush_after = 0;
673 int needs_clflush_before = 0;
Daniel Vetter692a5762012-03-25 19:47:34 +0200674 int release_page;
Eric Anholt40123c12009-03-09 13:42:30 -0700675
Daniel Vetter8c599672011-12-14 13:57:31 +0100676 user_data = (char __user *) (uintptr_t) args->data_ptr;
Eric Anholt40123c12009-03-09 13:42:30 -0700677 remain = args->size;
678
Daniel Vetter8c599672011-12-14 13:57:31 +0100679 obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700680
Daniel Vetter58642882012-03-25 19:47:37 +0200681 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
682 /* If we're not in the cpu write domain, set ourself into the gtt
683 * write domain and manually flush cachelines (if required). This
684 * optimizes for the case when the gpu will use the data
685 * right away and we therefore have to clflush anyway. */
686 if (obj->cache_level == I915_CACHE_NONE)
687 needs_clflush_after = 1;
688 ret = i915_gem_object_set_to_gtt_domain(obj, true);
689 if (ret)
690 return ret;
691 }
692 /* Same trick applies for invalidate partially written cachelines before
693 * writing. */
694 if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU)
695 && obj->cache_level == I915_CACHE_NONE)
696 needs_clflush_before = 1;
697
Eric Anholt40123c12009-03-09 13:42:30 -0700698 offset = args->offset;
Chris Wilson05394f32010-11-08 19:18:58 +0000699 obj->dirty = 1;
Eric Anholt40123c12009-03-09 13:42:30 -0700700
701 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100702 struct page *page;
Daniel Vetter58642882012-03-25 19:47:37 +0200703 int partial_cacheline_write;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100704
Eric Anholt40123c12009-03-09 13:42:30 -0700705 /* Operation in this page
706 *
Eric Anholt40123c12009-03-09 13:42:30 -0700707 * shmem_page_offset = offset within page in shmem file
Eric Anholt40123c12009-03-09 13:42:30 -0700708 * page_length = bytes to copy for this page
709 */
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100710 shmem_page_offset = offset_in_page(offset);
Eric Anholt40123c12009-03-09 13:42:30 -0700711
712 page_length = remain;
713 if ((shmem_page_offset + page_length) > PAGE_SIZE)
714 page_length = PAGE_SIZE - shmem_page_offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700715
Daniel Vetter58642882012-03-25 19:47:37 +0200716 /* If we don't overwrite a cacheline completely we need to be
717 * careful to have up-to-date data by first clflushing. Don't
718 * overcomplicate things and flush the entire patch. */
719 partial_cacheline_write = needs_clflush_before &&
720 ((shmem_page_offset | page_length)
721 & (boot_cpu_data.x86_clflush_size - 1));
722
Daniel Vetter692a5762012-03-25 19:47:34 +0200723 if (obj->pages) {
724 page = obj->pages[offset >> PAGE_SHIFT];
725 release_page = 0;
726 } else {
727 page = shmem_read_mapping_page(mapping, offset >> PAGE_SHIFT);
728 if (IS_ERR(page)) {
729 ret = PTR_ERR(page);
730 goto out;
731 }
732 release_page = 1;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100733 }
734
Daniel Vetter8c599672011-12-14 13:57:31 +0100735 page_do_bit17_swizzling = obj_do_bit17_swizzling &&
736 (page_to_phys(page) & (1 << 17)) != 0;
737
Daniel Vetterd174bd62012-03-25 19:47:40 +0200738 ret = shmem_pwrite_fast(page, shmem_page_offset, page_length,
739 user_data, page_do_bit17_swizzling,
740 partial_cacheline_write,
741 needs_clflush_after);
742 if (ret == 0)
743 goto next_page;
Daniel Vettere244a442012-03-25 19:47:28 +0200744
745 hit_slowpath = 1;
Daniel Vetter692a5762012-03-25 19:47:34 +0200746 page_cache_get(page);
Daniel Vettere244a442012-03-25 19:47:28 +0200747 mutex_unlock(&dev->struct_mutex);
748
Daniel Vetterd174bd62012-03-25 19:47:40 +0200749 ret = shmem_pwrite_slow(page, shmem_page_offset, page_length,
750 user_data, page_do_bit17_swizzling,
751 partial_cacheline_write,
752 needs_clflush_after);
Eric Anholt40123c12009-03-09 13:42:30 -0700753
Daniel Vettere244a442012-03-25 19:47:28 +0200754 mutex_lock(&dev->struct_mutex);
Daniel Vetter692a5762012-03-25 19:47:34 +0200755 page_cache_release(page);
Daniel Vettere244a442012-03-25 19:47:28 +0200756next_page:
Chris Wilsone5281cc2010-10-28 13:45:36 +0100757 set_page_dirty(page);
758 mark_page_accessed(page);
Daniel Vetter692a5762012-03-25 19:47:34 +0200759 if (release_page)
760 page_cache_release(page);
Chris Wilsone5281cc2010-10-28 13:45:36 +0100761
Daniel Vetter8c599672011-12-14 13:57:31 +0100762 if (ret) {
763 ret = -EFAULT;
764 goto out;
765 }
766
Eric Anholt40123c12009-03-09 13:42:30 -0700767 remain -= page_length;
Daniel Vetter8c599672011-12-14 13:57:31 +0100768 user_data += page_length;
Eric Anholt40123c12009-03-09 13:42:30 -0700769 offset += page_length;
770 }
771
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100772out:
Daniel Vettere244a442012-03-25 19:47:28 +0200773 if (hit_slowpath) {
774 /* Fixup: Kill any reinstated backing storage pages */
775 if (obj->madv == __I915_MADV_PURGED)
776 i915_gem_object_truncate(obj);
777 /* and flush dirty cachelines in case the object isn't in the cpu write
778 * domain anymore. */
779 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
780 i915_gem_clflush_object(obj);
781 intel_gtt_chipset_flush();
782 }
Daniel Vetter8c599672011-12-14 13:57:31 +0100783 }
Eric Anholt40123c12009-03-09 13:42:30 -0700784
Daniel Vetter58642882012-03-25 19:47:37 +0200785 if (needs_clflush_after)
786 intel_gtt_chipset_flush();
787
Eric Anholt40123c12009-03-09 13:42:30 -0700788 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700789}
790
791/**
792 * Writes data to the object referenced by handle.
793 *
794 * On error, the contents of the buffer that were to be modified are undefined.
795 */
796int
797i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100798 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700799{
800 struct drm_i915_gem_pwrite *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +0000801 struct drm_i915_gem_object *obj;
Chris Wilson51311d02010-11-17 09:10:42 +0000802 int ret;
803
804 if (args->size == 0)
805 return 0;
806
807 if (!access_ok(VERIFY_READ,
808 (char __user *)(uintptr_t)args->data_ptr,
809 args->size))
810 return -EFAULT;
811
Daniel Vetterf56f8212012-03-25 19:47:41 +0200812 ret = fault_in_multipages_readable((char __user *)(uintptr_t)args->data_ptr,
813 args->size);
Chris Wilson51311d02010-11-17 09:10:42 +0000814 if (ret)
815 return -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -0700816
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100817 ret = i915_mutex_lock_interruptible(dev);
818 if (ret)
819 return ret;
820
Chris Wilson05394f32010-11-08 19:18:58 +0000821 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +0000822 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100823 ret = -ENOENT;
824 goto unlock;
825 }
Eric Anholt673a3942008-07-30 12:06:12 -0700826
Chris Wilson7dcd2492010-09-26 20:21:44 +0100827 /* Bounds check destination. */
Chris Wilson05394f32010-11-08 19:18:58 +0000828 if (args->offset > obj->base.size ||
829 args->size > obj->base.size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +0100830 ret = -EINVAL;
Chris Wilson35b62a82010-09-26 20:23:38 +0100831 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +0100832 }
833
Chris Wilsondb53a302011-02-03 11:57:46 +0000834 trace_i915_gem_object_pwrite(obj, args->offset, args->size);
835
Daniel Vetter935aaa62012-03-25 19:47:35 +0200836 ret = -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -0700837 /* We can only do the GTT pwrite on untiled buffers, as otherwise
838 * it would end up going through the fenced access, and we'll get
839 * different detiling behavior between reading and writing.
840 * pread/pwrite currently are reading and writing from the CPU
841 * perspective, requiring manual detiling by the client.
842 */
Daniel Vetter5c0480f2011-12-14 13:57:30 +0100843 if (obj->phys_obj) {
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100844 ret = i915_gem_phys_pwrite(dev, obj, args, file);
Daniel Vetter5c0480f2011-12-14 13:57:30 +0100845 goto out;
846 }
847
848 if (obj->gtt_space &&
Daniel Vetter3ae53782012-03-25 19:47:33 +0200849 obj->cache_level == I915_CACHE_NONE &&
Daniel Vetterffc62972012-03-25 19:47:38 +0200850 obj->map_and_fenceable &&
Daniel Vetter5c0480f2011-12-14 13:57:30 +0100851 obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100852 ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file);
Daniel Vetter935aaa62012-03-25 19:47:35 +0200853 /* Note that the gtt paths might fail with non-page-backed user
854 * pointers (e.g. gtt mappings when moving data between
855 * textures). Fallback to the shmem path in that case. */
Eric Anholt40123c12009-03-09 13:42:30 -0700856 }
Eric Anholt673a3942008-07-30 12:06:12 -0700857
Daniel Vetter935aaa62012-03-25 19:47:35 +0200858 if (ret == -EFAULT)
859 ret = i915_gem_shmem_pwrite(dev, obj, args, file);
Daniel Vetter5c0480f2011-12-14 13:57:30 +0100860
Chris Wilson35b62a82010-09-26 20:23:38 +0100861out:
Chris Wilson05394f32010-11-08 19:18:58 +0000862 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100863unlock:
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100864 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -0700865 return ret;
866}
867
868/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800869 * Called when user space prepares to use an object with the CPU, either
870 * through the mmap ioctl's mapping or a GTT mapping.
Eric Anholt673a3942008-07-30 12:06:12 -0700871 */
872int
873i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000874 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700875{
876 struct drm_i915_gem_set_domain *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +0000877 struct drm_i915_gem_object *obj;
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800878 uint32_t read_domains = args->read_domains;
879 uint32_t write_domain = args->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -0700880 int ret;
881
882 if (!(dev->driver->driver_features & DRIVER_GEM))
883 return -ENODEV;
884
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800885 /* Only handle setting domains to types used by the CPU. */
Chris Wilson21d509e2009-06-06 09:46:02 +0100886 if (write_domain & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800887 return -EINVAL;
888
Chris Wilson21d509e2009-06-06 09:46:02 +0100889 if (read_domains & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800890 return -EINVAL;
891
892 /* Having something in the write domain implies it's in the read
893 * domain, and only that read domain. Enforce that in the request.
894 */
895 if (write_domain != 0 && read_domains != write_domain)
896 return -EINVAL;
897
Chris Wilson76c1dec2010-09-25 11:22:51 +0100898 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100899 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +0100900 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700901
Chris Wilson05394f32010-11-08 19:18:58 +0000902 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +0000903 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100904 ret = -ENOENT;
905 goto unlock;
Chris Wilson76c1dec2010-09-25 11:22:51 +0100906 }
Jesse Barnes652c3932009-08-17 13:31:43 -0700907
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800908 if (read_domains & I915_GEM_DOMAIN_GTT) {
909 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
Eric Anholt02354392008-11-26 13:58:13 -0800910
911 /* Silently promote "you're not bound, there was nothing to do"
912 * to success, since the client was just asking us to
913 * make sure everything was done.
914 */
915 if (ret == -EINVAL)
916 ret = 0;
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800917 } else {
Eric Anholte47c68e2008-11-14 13:35:19 -0800918 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800919 }
920
Chris Wilson05394f32010-11-08 19:18:58 +0000921 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100922unlock:
Eric Anholt673a3942008-07-30 12:06:12 -0700923 mutex_unlock(&dev->struct_mutex);
924 return ret;
925}
926
927/**
928 * Called when user space has done writes to this buffer
929 */
930int
931i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000932 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700933{
934 struct drm_i915_gem_sw_finish *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +0000935 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -0700936 int ret = 0;
937
938 if (!(dev->driver->driver_features & DRIVER_GEM))
939 return -ENODEV;
940
Chris Wilson76c1dec2010-09-25 11:22:51 +0100941 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100942 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +0100943 return ret;
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100944
Chris Wilson05394f32010-11-08 19:18:58 +0000945 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +0000946 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100947 ret = -ENOENT;
948 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -0700949 }
950
Eric Anholt673a3942008-07-30 12:06:12 -0700951 /* Pinned buffers may be scanout, so flush the cache */
Chris Wilson05394f32010-11-08 19:18:58 +0000952 if (obj->pin_count)
Eric Anholte47c68e2008-11-14 13:35:19 -0800953 i915_gem_object_flush_cpu_write_domain(obj);
954
Chris Wilson05394f32010-11-08 19:18:58 +0000955 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100956unlock:
Eric Anholt673a3942008-07-30 12:06:12 -0700957 mutex_unlock(&dev->struct_mutex);
958 return ret;
959}
960
961/**
962 * Maps the contents of an object, returning the address it is mapped
963 * into.
964 *
965 * While the mapping holds a reference on the contents of the object, it doesn't
966 * imply a ref on the object itself.
967 */
968int
969i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000970 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700971{
972 struct drm_i915_gem_mmap *args = data;
973 struct drm_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -0700974 unsigned long addr;
975
976 if (!(dev->driver->driver_features & DRIVER_GEM))
977 return -ENODEV;
978
Chris Wilson05394f32010-11-08 19:18:58 +0000979 obj = drm_gem_object_lookup(dev, file, args->handle);
Eric Anholt673a3942008-07-30 12:06:12 -0700980 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100981 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -0700982
Eric Anholt673a3942008-07-30 12:06:12 -0700983 down_write(&current->mm->mmap_sem);
984 addr = do_mmap(obj->filp, 0, args->size,
985 PROT_READ | PROT_WRITE, MAP_SHARED,
986 args->offset);
987 up_write(&current->mm->mmap_sem);
Luca Barbieribc9025b2010-02-09 05:49:12 +0000988 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700989 if (IS_ERR((void *)addr))
990 return addr;
991
992 args->addr_ptr = (uint64_t) addr;
993
994 return 0;
995}
996
Jesse Barnesde151cf2008-11-12 10:03:55 -0800997/**
998 * i915_gem_fault - fault a page into the GTT
999 * vma: VMA in question
1000 * vmf: fault info
1001 *
1002 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1003 * from userspace. The fault handler takes care of binding the object to
1004 * the GTT (if needed), allocating and programming a fence register (again,
1005 * only if needed based on whether the old reg is still valid or the object
1006 * is tiled) and inserting a new PTE into the faulting process.
1007 *
1008 * Note that the faulting process may involve evicting existing objects
1009 * from the GTT and/or fence registers to make room. So performance may
1010 * suffer if the GTT working set is large or there are few fence registers
1011 * left.
1012 */
1013int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1014{
Chris Wilson05394f32010-11-08 19:18:58 +00001015 struct drm_i915_gem_object *obj = to_intel_bo(vma->vm_private_data);
1016 struct drm_device *dev = obj->base.dev;
Chris Wilson7d1c4802010-08-07 21:45:03 +01001017 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001018 pgoff_t page_offset;
1019 unsigned long pfn;
1020 int ret = 0;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001021 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001022
1023 /* We don't use vmf->pgoff since that has the fake offset */
1024 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1025 PAGE_SHIFT;
1026
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001027 ret = i915_mutex_lock_interruptible(dev);
1028 if (ret)
1029 goto out;
Chris Wilsona00b10c2010-09-24 21:15:47 +01001030
Chris Wilsondb53a302011-02-03 11:57:46 +00001031 trace_i915_gem_object_fault(obj, page_offset, true, write);
1032
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001033 /* Now bind it into the GTT if needed */
Chris Wilson919926a2010-11-12 13:42:53 +00001034 if (!obj->map_and_fenceable) {
1035 ret = i915_gem_object_unbind(obj);
1036 if (ret)
1037 goto unlock;
Chris Wilsona00b10c2010-09-24 21:15:47 +01001038 }
Chris Wilson05394f32010-11-08 19:18:58 +00001039 if (!obj->gtt_space) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01001040 ret = i915_gem_object_bind_to_gtt(obj, 0, true);
Chris Wilsonc7150892009-09-23 00:43:56 +01001041 if (ret)
1042 goto unlock;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001043
Eric Anholte92d03b2011-06-14 16:43:09 -07001044 ret = i915_gem_object_set_to_gtt_domain(obj, write);
1045 if (ret)
1046 goto unlock;
1047 }
Chris Wilson4a684a42010-10-28 14:44:08 +01001048
Daniel Vetter74898d72012-02-15 23:50:22 +01001049 if (!obj->has_global_gtt_mapping)
1050 i915_gem_gtt_bind_object(obj, obj->cache_level);
1051
Chris Wilsond9e86c02010-11-10 16:40:20 +00001052 if (obj->tiling_mode == I915_TILING_NONE)
1053 ret = i915_gem_object_put_fence(obj);
1054 else
Chris Wilsonce453d82011-02-21 14:43:56 +00001055 ret = i915_gem_object_get_fence(obj, NULL);
Chris Wilsond9e86c02010-11-10 16:40:20 +00001056 if (ret)
1057 goto unlock;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001058
Chris Wilson05394f32010-11-08 19:18:58 +00001059 if (i915_gem_object_is_inactive(obj))
1060 list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
Chris Wilson7d1c4802010-08-07 21:45:03 +01001061
Chris Wilson6299f992010-11-24 12:23:44 +00001062 obj->fault_mappable = true;
1063
Chris Wilson05394f32010-11-08 19:18:58 +00001064 pfn = ((dev->agp->base + obj->gtt_offset) >> PAGE_SHIFT) +
Jesse Barnesde151cf2008-11-12 10:03:55 -08001065 page_offset;
1066
1067 /* Finally, remap it using the new GTT offset */
1068 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
Chris Wilsonc7150892009-09-23 00:43:56 +01001069unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001070 mutex_unlock(&dev->struct_mutex);
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001071out:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001072 switch (ret) {
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001073 case -EIO:
Chris Wilson045e7692010-11-07 09:18:22 +00001074 case -EAGAIN:
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001075 /* Give the error handler a chance to run and move the
1076 * objects off the GPU active list. Next time we service the
1077 * fault, we should be able to transition the page into the
1078 * GTT without touching the GPU (and so avoid further
1079 * EIO/EGAIN). If the GPU is wedged, then there is no issue
1080 * with coherency, just lost writes.
1081 */
Chris Wilson045e7692010-11-07 09:18:22 +00001082 set_need_resched();
Chris Wilsonc7150892009-09-23 00:43:56 +01001083 case 0:
1084 case -ERESTARTSYS:
Chris Wilsonbed636a2011-02-11 20:31:19 +00001085 case -EINTR:
Chris Wilsonc7150892009-09-23 00:43:56 +01001086 return VM_FAULT_NOPAGE;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001087 case -ENOMEM:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001088 return VM_FAULT_OOM;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001089 default:
Chris Wilsonc7150892009-09-23 00:43:56 +01001090 return VM_FAULT_SIGBUS;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001091 }
1092}
1093
1094/**
Chris Wilson901782b2009-07-10 08:18:50 +01001095 * i915_gem_release_mmap - remove physical page mappings
1096 * @obj: obj in question
1097 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001098 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01001099 * relinquish ownership of the pages back to the system.
1100 *
1101 * It is vital that we remove the page mapping if we have mapped a tiled
1102 * object through the GTT and then lose the fence register due to
1103 * resource pressure. Similarly if the object has been moved out of the
1104 * aperture, than pages mapped into userspace must be revoked. Removing the
1105 * mapping will then trigger a page fault on the next user access, allowing
1106 * fixup by i915_gem_fault().
1107 */
Eric Anholtd05ca302009-07-10 13:02:26 -07001108void
Chris Wilson05394f32010-11-08 19:18:58 +00001109i915_gem_release_mmap(struct drm_i915_gem_object *obj)
Chris Wilson901782b2009-07-10 08:18:50 +01001110{
Chris Wilson6299f992010-11-24 12:23:44 +00001111 if (!obj->fault_mappable)
1112 return;
Chris Wilson901782b2009-07-10 08:18:50 +01001113
Chris Wilsonf6e47882011-03-20 21:09:12 +00001114 if (obj->base.dev->dev_mapping)
1115 unmap_mapping_range(obj->base.dev->dev_mapping,
1116 (loff_t)obj->base.map_list.hash.key<<PAGE_SHIFT,
1117 obj->base.size, 1);
Daniel Vetterfb7d5162010-10-01 22:05:20 +02001118
Chris Wilson6299f992010-11-24 12:23:44 +00001119 obj->fault_mappable = false;
Chris Wilson901782b2009-07-10 08:18:50 +01001120}
1121
Chris Wilson92b88ae2010-11-09 11:47:32 +00001122static uint32_t
Chris Wilsone28f8712011-07-18 13:11:49 -07001123i915_gem_get_gtt_size(struct drm_device *dev, uint32_t size, int tiling_mode)
Chris Wilson92b88ae2010-11-09 11:47:32 +00001124{
Chris Wilsone28f8712011-07-18 13:11:49 -07001125 uint32_t gtt_size;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001126
1127 if (INTEL_INFO(dev)->gen >= 4 ||
Chris Wilsone28f8712011-07-18 13:11:49 -07001128 tiling_mode == I915_TILING_NONE)
1129 return size;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001130
1131 /* Previous chips need a power-of-two fence region when tiling */
1132 if (INTEL_INFO(dev)->gen == 3)
Chris Wilsone28f8712011-07-18 13:11:49 -07001133 gtt_size = 1024*1024;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001134 else
Chris Wilsone28f8712011-07-18 13:11:49 -07001135 gtt_size = 512*1024;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001136
Chris Wilsone28f8712011-07-18 13:11:49 -07001137 while (gtt_size < size)
1138 gtt_size <<= 1;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001139
Chris Wilsone28f8712011-07-18 13:11:49 -07001140 return gtt_size;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001141}
1142
Jesse Barnesde151cf2008-11-12 10:03:55 -08001143/**
1144 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1145 * @obj: object to check
1146 *
1147 * Return the required GTT alignment for an object, taking into account
Daniel Vetter5e783302010-11-14 22:32:36 +01001148 * potential fence register mapping.
Jesse Barnesde151cf2008-11-12 10:03:55 -08001149 */
1150static uint32_t
Chris Wilsone28f8712011-07-18 13:11:49 -07001151i915_gem_get_gtt_alignment(struct drm_device *dev,
1152 uint32_t size,
1153 int tiling_mode)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001154{
Jesse Barnesde151cf2008-11-12 10:03:55 -08001155 /*
1156 * Minimum alignment is 4k (GTT page size), but might be greater
1157 * if a fence register is needed for the object.
1158 */
Chris Wilsona00b10c2010-09-24 21:15:47 +01001159 if (INTEL_INFO(dev)->gen >= 4 ||
Chris Wilsone28f8712011-07-18 13:11:49 -07001160 tiling_mode == I915_TILING_NONE)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001161 return 4096;
1162
1163 /*
1164 * Previous chips need to be aligned to the size of the smallest
1165 * fence register that can contain the object.
1166 */
Chris Wilsone28f8712011-07-18 13:11:49 -07001167 return i915_gem_get_gtt_size(dev, size, tiling_mode);
Chris Wilsona00b10c2010-09-24 21:15:47 +01001168}
1169
Daniel Vetter5e783302010-11-14 22:32:36 +01001170/**
1171 * i915_gem_get_unfenced_gtt_alignment - return required GTT alignment for an
1172 * unfenced object
Chris Wilsone28f8712011-07-18 13:11:49 -07001173 * @dev: the device
1174 * @size: size of the object
1175 * @tiling_mode: tiling mode of the object
Daniel Vetter5e783302010-11-14 22:32:36 +01001176 *
1177 * Return the required GTT alignment for an object, only taking into account
1178 * unfenced tiled surface requirements.
1179 */
Chris Wilson467cffb2011-03-07 10:42:03 +00001180uint32_t
Chris Wilsone28f8712011-07-18 13:11:49 -07001181i915_gem_get_unfenced_gtt_alignment(struct drm_device *dev,
1182 uint32_t size,
1183 int tiling_mode)
Daniel Vetter5e783302010-11-14 22:32:36 +01001184{
Daniel Vetter5e783302010-11-14 22:32:36 +01001185 /*
1186 * Minimum alignment is 4k (GTT page size) for sane hw.
1187 */
1188 if (INTEL_INFO(dev)->gen >= 4 || IS_G33(dev) ||
Chris Wilsone28f8712011-07-18 13:11:49 -07001189 tiling_mode == I915_TILING_NONE)
Daniel Vetter5e783302010-11-14 22:32:36 +01001190 return 4096;
1191
Chris Wilsone28f8712011-07-18 13:11:49 -07001192 /* Previous hardware however needs to be aligned to a power-of-two
1193 * tile height. The simplest method for determining this is to reuse
1194 * the power-of-tile object size.
Daniel Vetter5e783302010-11-14 22:32:36 +01001195 */
Chris Wilsone28f8712011-07-18 13:11:49 -07001196 return i915_gem_get_gtt_size(dev, size, tiling_mode);
Daniel Vetter5e783302010-11-14 22:32:36 +01001197}
1198
Jesse Barnesde151cf2008-11-12 10:03:55 -08001199int
Dave Airlieff72145b2011-02-07 12:16:14 +10001200i915_gem_mmap_gtt(struct drm_file *file,
1201 struct drm_device *dev,
1202 uint32_t handle,
1203 uint64_t *offset)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001204{
Chris Wilsonda761a62010-10-27 17:37:08 +01001205 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00001206 struct drm_i915_gem_object *obj;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001207 int ret;
1208
1209 if (!(dev->driver->driver_features & DRIVER_GEM))
1210 return -ENODEV;
1211
Chris Wilson76c1dec2010-09-25 11:22:51 +01001212 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001213 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001214 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001215
Dave Airlieff72145b2011-02-07 12:16:14 +10001216 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00001217 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001218 ret = -ENOENT;
1219 goto unlock;
1220 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001221
Chris Wilson05394f32010-11-08 19:18:58 +00001222 if (obj->base.size > dev_priv->mm.gtt_mappable_end) {
Chris Wilsonda761a62010-10-27 17:37:08 +01001223 ret = -E2BIG;
Eric Anholtff56b0b2011-10-31 23:16:21 -07001224 goto out;
Chris Wilsonda761a62010-10-27 17:37:08 +01001225 }
1226
Chris Wilson05394f32010-11-08 19:18:58 +00001227 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilsonab182822009-09-22 18:46:17 +01001228 DRM_ERROR("Attempting to mmap a purgeable buffer\n");
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001229 ret = -EINVAL;
1230 goto out;
Chris Wilsonab182822009-09-22 18:46:17 +01001231 }
1232
Chris Wilson05394f32010-11-08 19:18:58 +00001233 if (!obj->base.map_list.map) {
Rob Clarkb464e9a2011-08-10 08:09:08 -05001234 ret = drm_gem_create_mmap_offset(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001235 if (ret)
1236 goto out;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001237 }
1238
Dave Airlieff72145b2011-02-07 12:16:14 +10001239 *offset = (u64)obj->base.map_list.hash.key << PAGE_SHIFT;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001240
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001241out:
Chris Wilson05394f32010-11-08 19:18:58 +00001242 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001243unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001244 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001245 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001246}
1247
Dave Airlieff72145b2011-02-07 12:16:14 +10001248/**
1249 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1250 * @dev: DRM device
1251 * @data: GTT mapping ioctl data
1252 * @file: GEM object info
1253 *
1254 * Simply returns the fake offset to userspace so it can mmap it.
1255 * The mmap call will end up in drm_gem_mmap(), which will set things
1256 * up so we can get faults in the handler above.
1257 *
1258 * The fault handler will take care of binding the object into the GTT
1259 * (since it may have been evicted to make room for something), allocating
1260 * a fence register, and mapping the appropriate aperture address into
1261 * userspace.
1262 */
1263int
1264i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
1265 struct drm_file *file)
1266{
1267 struct drm_i915_gem_mmap_gtt *args = data;
1268
1269 if (!(dev->driver->driver_features & DRIVER_GEM))
1270 return -ENODEV;
1271
1272 return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
1273}
1274
1275
Chris Wilsone5281cc2010-10-28 13:45:36 +01001276static int
Chris Wilson05394f32010-11-08 19:18:58 +00001277i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj,
Chris Wilsone5281cc2010-10-28 13:45:36 +01001278 gfp_t gfpmask)
1279{
Chris Wilsone5281cc2010-10-28 13:45:36 +01001280 int page_count, i;
1281 struct address_space *mapping;
1282 struct inode *inode;
1283 struct page *page;
1284
1285 /* Get the list of pages out of our struct file. They'll be pinned
1286 * at this point until we release them.
1287 */
Chris Wilson05394f32010-11-08 19:18:58 +00001288 page_count = obj->base.size / PAGE_SIZE;
1289 BUG_ON(obj->pages != NULL);
1290 obj->pages = drm_malloc_ab(page_count, sizeof(struct page *));
1291 if (obj->pages == NULL)
Chris Wilsone5281cc2010-10-28 13:45:36 +01001292 return -ENOMEM;
1293
Chris Wilson05394f32010-11-08 19:18:58 +00001294 inode = obj->base.filp->f_path.dentry->d_inode;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001295 mapping = inode->i_mapping;
Hugh Dickins5949eac2011-06-27 16:18:18 -07001296 gfpmask |= mapping_gfp_mask(mapping);
1297
Chris Wilsone5281cc2010-10-28 13:45:36 +01001298 for (i = 0; i < page_count; i++) {
Hugh Dickins5949eac2011-06-27 16:18:18 -07001299 page = shmem_read_mapping_page_gfp(mapping, i, gfpmask);
Chris Wilsone5281cc2010-10-28 13:45:36 +01001300 if (IS_ERR(page))
1301 goto err_pages;
1302
Chris Wilson05394f32010-11-08 19:18:58 +00001303 obj->pages[i] = page;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001304 }
1305
Daniel Vetter6dacfd22011-09-12 21:30:02 +02001306 if (i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilsone5281cc2010-10-28 13:45:36 +01001307 i915_gem_object_do_bit_17_swizzle(obj);
1308
1309 return 0;
1310
1311err_pages:
1312 while (i--)
Chris Wilson05394f32010-11-08 19:18:58 +00001313 page_cache_release(obj->pages[i]);
Chris Wilsone5281cc2010-10-28 13:45:36 +01001314
Chris Wilson05394f32010-11-08 19:18:58 +00001315 drm_free_large(obj->pages);
1316 obj->pages = NULL;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001317 return PTR_ERR(page);
1318}
1319
Chris Wilson5cdf5882010-09-27 15:51:07 +01001320static void
Chris Wilson05394f32010-11-08 19:18:58 +00001321i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07001322{
Chris Wilson05394f32010-11-08 19:18:58 +00001323 int page_count = obj->base.size / PAGE_SIZE;
Eric Anholt673a3942008-07-30 12:06:12 -07001324 int i;
1325
Chris Wilson05394f32010-11-08 19:18:58 +00001326 BUG_ON(obj->madv == __I915_MADV_PURGED);
Eric Anholt856fa192009-03-19 14:10:50 -07001327
Daniel Vetter6dacfd22011-09-12 21:30:02 +02001328 if (i915_gem_object_needs_bit17_swizzle(obj))
Eric Anholt280b7132009-03-12 16:56:27 -07001329 i915_gem_object_save_bit_17_swizzle(obj);
1330
Chris Wilson05394f32010-11-08 19:18:58 +00001331 if (obj->madv == I915_MADV_DONTNEED)
1332 obj->dirty = 0;
Chris Wilson3ef94da2009-09-14 16:50:29 +01001333
1334 for (i = 0; i < page_count; i++) {
Chris Wilson05394f32010-11-08 19:18:58 +00001335 if (obj->dirty)
1336 set_page_dirty(obj->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001337
Chris Wilson05394f32010-11-08 19:18:58 +00001338 if (obj->madv == I915_MADV_WILLNEED)
1339 mark_page_accessed(obj->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001340
Chris Wilson05394f32010-11-08 19:18:58 +00001341 page_cache_release(obj->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001342 }
Chris Wilson05394f32010-11-08 19:18:58 +00001343 obj->dirty = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001344
Chris Wilson05394f32010-11-08 19:18:58 +00001345 drm_free_large(obj->pages);
1346 obj->pages = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001347}
1348
Chris Wilson54cf91d2010-11-25 18:00:26 +00001349void
Chris Wilson05394f32010-11-08 19:18:58 +00001350i915_gem_object_move_to_active(struct drm_i915_gem_object *obj,
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001351 struct intel_ring_buffer *ring,
1352 u32 seqno)
Eric Anholt673a3942008-07-30 12:06:12 -07001353{
Chris Wilson05394f32010-11-08 19:18:58 +00001354 struct drm_device *dev = obj->base.dev;
Chris Wilson69dc4982010-10-19 10:36:51 +01001355 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter617dbe22010-02-11 22:16:02 +01001356
Zou Nan hai852835f2010-05-21 09:08:56 +08001357 BUG_ON(ring == NULL);
Chris Wilson05394f32010-11-08 19:18:58 +00001358 obj->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001359
1360 /* Add a reference if we're newly entering the active list. */
Chris Wilson05394f32010-11-08 19:18:58 +00001361 if (!obj->active) {
1362 drm_gem_object_reference(&obj->base);
1363 obj->active = 1;
Eric Anholt673a3942008-07-30 12:06:12 -07001364 }
Daniel Vettere35a41d2010-02-11 22:13:59 +01001365
Eric Anholt673a3942008-07-30 12:06:12 -07001366 /* Move from whatever list we were on to the tail of execution. */
Chris Wilson05394f32010-11-08 19:18:58 +00001367 list_move_tail(&obj->mm_list, &dev_priv->mm.active_list);
1368 list_move_tail(&obj->ring_list, &ring->active_list);
Chris Wilsoncaea7472010-11-12 13:53:37 +00001369
Chris Wilson05394f32010-11-08 19:18:58 +00001370 obj->last_rendering_seqno = seqno;
Chris Wilsoncaea7472010-11-12 13:53:37 +00001371 if (obj->fenced_gpu_access) {
1372 struct drm_i915_fence_reg *reg;
1373
1374 BUG_ON(obj->fence_reg == I915_FENCE_REG_NONE);
1375
1376 obj->last_fenced_seqno = seqno;
1377 obj->last_fenced_ring = ring;
1378
1379 reg = &dev_priv->fence_regs[obj->fence_reg];
1380 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
1381 }
1382}
1383
1384static void
1385i915_gem_object_move_off_active(struct drm_i915_gem_object *obj)
1386{
1387 list_del_init(&obj->ring_list);
1388 obj->last_rendering_seqno = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001389}
1390
Eric Anholtce44b0e2008-11-06 16:00:31 -08001391static void
Chris Wilson05394f32010-11-08 19:18:58 +00001392i915_gem_object_move_to_flushing(struct drm_i915_gem_object *obj)
Eric Anholtce44b0e2008-11-06 16:00:31 -08001393{
Chris Wilson05394f32010-11-08 19:18:58 +00001394 struct drm_device *dev = obj->base.dev;
Eric Anholtce44b0e2008-11-06 16:00:31 -08001395 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtce44b0e2008-11-06 16:00:31 -08001396
Chris Wilson05394f32010-11-08 19:18:58 +00001397 BUG_ON(!obj->active);
1398 list_move_tail(&obj->mm_list, &dev_priv->mm.flushing_list);
Chris Wilsoncaea7472010-11-12 13:53:37 +00001399
1400 i915_gem_object_move_off_active(obj);
1401}
1402
1403static void
1404i915_gem_object_move_to_inactive(struct drm_i915_gem_object *obj)
1405{
1406 struct drm_device *dev = obj->base.dev;
1407 struct drm_i915_private *dev_priv = dev->dev_private;
1408
1409 if (obj->pin_count != 0)
1410 list_move_tail(&obj->mm_list, &dev_priv->mm.pinned_list);
1411 else
1412 list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
1413
1414 BUG_ON(!list_empty(&obj->gpu_write_list));
1415 BUG_ON(!obj->active);
1416 obj->ring = NULL;
1417
1418 i915_gem_object_move_off_active(obj);
1419 obj->fenced_gpu_access = false;
Chris Wilsoncaea7472010-11-12 13:53:37 +00001420
1421 obj->active = 0;
Chris Wilson87ca9c82010-12-02 09:42:56 +00001422 obj->pending_gpu_write = false;
Chris Wilsoncaea7472010-11-12 13:53:37 +00001423 drm_gem_object_unreference(&obj->base);
1424
1425 WARN_ON(i915_verify_lists(dev));
Eric Anholtce44b0e2008-11-06 16:00:31 -08001426}
Eric Anholt673a3942008-07-30 12:06:12 -07001427
Chris Wilson963b4832009-09-20 23:03:54 +01001428/* Immediately discard the backing storage */
1429static void
Chris Wilson05394f32010-11-08 19:18:58 +00001430i915_gem_object_truncate(struct drm_i915_gem_object *obj)
Chris Wilson963b4832009-09-20 23:03:54 +01001431{
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001432 struct inode *inode;
Chris Wilson963b4832009-09-20 23:03:54 +01001433
Chris Wilsonae9fed62010-08-07 11:01:30 +01001434 /* Our goal here is to return as much of the memory as
1435 * is possible back to the system as we are called from OOM.
1436 * To do this we must instruct the shmfs to drop all of its
Hugh Dickinse2377fe2011-06-27 16:18:19 -07001437 * backing pages, *now*.
Chris Wilsonae9fed62010-08-07 11:01:30 +01001438 */
Chris Wilson05394f32010-11-08 19:18:58 +00001439 inode = obj->base.filp->f_path.dentry->d_inode;
Hugh Dickinse2377fe2011-06-27 16:18:19 -07001440 shmem_truncate_range(inode, 0, (loff_t)-1);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001441
Chris Wilsona14917e2012-02-24 21:13:38 +00001442 if (obj->base.map_list.map)
1443 drm_gem_free_mmap_offset(&obj->base);
1444
Chris Wilson05394f32010-11-08 19:18:58 +00001445 obj->madv = __I915_MADV_PURGED;
Chris Wilson963b4832009-09-20 23:03:54 +01001446}
1447
1448static inline int
Chris Wilson05394f32010-11-08 19:18:58 +00001449i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj)
Chris Wilson963b4832009-09-20 23:03:54 +01001450{
Chris Wilson05394f32010-11-08 19:18:58 +00001451 return obj->madv == I915_MADV_DONTNEED;
Chris Wilson963b4832009-09-20 23:03:54 +01001452}
1453
Eric Anholt673a3942008-07-30 12:06:12 -07001454static void
Chris Wilsondb53a302011-02-03 11:57:46 +00001455i915_gem_process_flushing_list(struct intel_ring_buffer *ring,
1456 uint32_t flush_domains)
Daniel Vetter63560392010-02-19 11:51:59 +01001457{
Chris Wilson05394f32010-11-08 19:18:58 +00001458 struct drm_i915_gem_object *obj, *next;
Daniel Vetter63560392010-02-19 11:51:59 +01001459
Chris Wilson05394f32010-11-08 19:18:58 +00001460 list_for_each_entry_safe(obj, next,
Chris Wilson64193402010-10-24 12:38:05 +01001461 &ring->gpu_write_list,
Daniel Vetter63560392010-02-19 11:51:59 +01001462 gpu_write_list) {
Chris Wilson05394f32010-11-08 19:18:58 +00001463 if (obj->base.write_domain & flush_domains) {
1464 uint32_t old_write_domain = obj->base.write_domain;
Daniel Vetter63560392010-02-19 11:51:59 +01001465
Chris Wilson05394f32010-11-08 19:18:58 +00001466 obj->base.write_domain = 0;
1467 list_del_init(&obj->gpu_write_list);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001468 i915_gem_object_move_to_active(obj, ring,
Chris Wilsondb53a302011-02-03 11:57:46 +00001469 i915_gem_next_request_seqno(ring));
Daniel Vetter63560392010-02-19 11:51:59 +01001470
Daniel Vetter63560392010-02-19 11:51:59 +01001471 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00001472 obj->base.read_domains,
Daniel Vetter63560392010-02-19 11:51:59 +01001473 old_write_domain);
1474 }
1475 }
1476}
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001477
Daniel Vetter53d227f2012-01-25 16:32:49 +01001478static u32
1479i915_gem_get_seqno(struct drm_device *dev)
1480{
1481 drm_i915_private_t *dev_priv = dev->dev_private;
1482 u32 seqno = dev_priv->next_seqno;
1483
1484 /* reserve 0 for non-seqno */
1485 if (++dev_priv->next_seqno == 0)
1486 dev_priv->next_seqno = 1;
1487
1488 return seqno;
1489}
1490
1491u32
1492i915_gem_next_request_seqno(struct intel_ring_buffer *ring)
1493{
1494 if (ring->outstanding_lazy_request == 0)
1495 ring->outstanding_lazy_request = i915_gem_get_seqno(ring->dev);
1496
1497 return ring->outstanding_lazy_request;
1498}
1499
Chris Wilson3cce4692010-10-27 16:11:02 +01001500int
Chris Wilsondb53a302011-02-03 11:57:46 +00001501i915_add_request(struct intel_ring_buffer *ring,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001502 struct drm_file *file,
Chris Wilsondb53a302011-02-03 11:57:46 +00001503 struct drm_i915_gem_request *request)
Eric Anholt673a3942008-07-30 12:06:12 -07001504{
Chris Wilsondb53a302011-02-03 11:57:46 +00001505 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07001506 uint32_t seqno;
Chris Wilsona71d8d92012-02-15 11:25:36 +00001507 u32 request_ring_position;
Eric Anholt673a3942008-07-30 12:06:12 -07001508 int was_empty;
Chris Wilson3cce4692010-10-27 16:11:02 +01001509 int ret;
1510
1511 BUG_ON(request == NULL);
Daniel Vetter53d227f2012-01-25 16:32:49 +01001512 seqno = i915_gem_next_request_seqno(ring);
Eric Anholt673a3942008-07-30 12:06:12 -07001513
Chris Wilsona71d8d92012-02-15 11:25:36 +00001514 /* Record the position of the start of the request so that
1515 * should we detect the updated seqno part-way through the
1516 * GPU processing the request, we never over-estimate the
1517 * position of the head.
1518 */
1519 request_ring_position = intel_ring_get_tail(ring);
1520
Chris Wilson3cce4692010-10-27 16:11:02 +01001521 ret = ring->add_request(ring, &seqno);
1522 if (ret)
1523 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001524
Chris Wilsondb53a302011-02-03 11:57:46 +00001525 trace_i915_gem_request_add(ring, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07001526
1527 request->seqno = seqno;
Zou Nan hai852835f2010-05-21 09:08:56 +08001528 request->ring = ring;
Chris Wilsona71d8d92012-02-15 11:25:36 +00001529 request->tail = request_ring_position;
Eric Anholt673a3942008-07-30 12:06:12 -07001530 request->emitted_jiffies = jiffies;
Zou Nan hai852835f2010-05-21 09:08:56 +08001531 was_empty = list_empty(&ring->request_list);
1532 list_add_tail(&request->list, &ring->request_list);
1533
Chris Wilsondb53a302011-02-03 11:57:46 +00001534 if (file) {
1535 struct drm_i915_file_private *file_priv = file->driver_priv;
1536
Chris Wilson1c255952010-09-26 11:03:27 +01001537 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001538 request->file_priv = file_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00001539 list_add_tail(&request->client_list,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001540 &file_priv->mm.request_list);
Chris Wilson1c255952010-09-26 11:03:27 +01001541 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00001542 }
Eric Anholt673a3942008-07-30 12:06:12 -07001543
Daniel Vetter5391d0c2012-01-25 14:03:57 +01001544 ring->outstanding_lazy_request = 0;
Chris Wilsondb53a302011-02-03 11:57:46 +00001545
Ben Gamarif65d9422009-09-14 17:48:44 -04001546 if (!dev_priv->mm.suspended) {
Ben Widawsky3e0dc6b2011-06-29 10:26:42 -07001547 if (i915_enable_hangcheck) {
1548 mod_timer(&dev_priv->hangcheck_timer,
1549 jiffies +
1550 msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
1551 }
Ben Gamarif65d9422009-09-14 17:48:44 -04001552 if (was_empty)
Chris Wilsonb3b079d2010-09-13 23:44:34 +01001553 queue_delayed_work(dev_priv->wq,
1554 &dev_priv->mm.retire_work, HZ);
Ben Gamarif65d9422009-09-14 17:48:44 -04001555 }
Chris Wilson3cce4692010-10-27 16:11:02 +01001556 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001557}
1558
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001559static inline void
1560i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
Eric Anholt673a3942008-07-30 12:06:12 -07001561{
Chris Wilson1c255952010-09-26 11:03:27 +01001562 struct drm_i915_file_private *file_priv = request->file_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07001563
Chris Wilson1c255952010-09-26 11:03:27 +01001564 if (!file_priv)
1565 return;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001566
Chris Wilson1c255952010-09-26 11:03:27 +01001567 spin_lock(&file_priv->mm.lock);
Herton Ronaldo Krzesinski09bfa512011-03-17 13:45:12 +00001568 if (request->file_priv) {
1569 list_del(&request->client_list);
1570 request->file_priv = NULL;
1571 }
Chris Wilson1c255952010-09-26 11:03:27 +01001572 spin_unlock(&file_priv->mm.lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001573}
1574
Chris Wilsondfaae392010-09-22 10:31:52 +01001575static void i915_gem_reset_ring_lists(struct drm_i915_private *dev_priv,
1576 struct intel_ring_buffer *ring)
Chris Wilson9375e442010-09-19 12:21:28 +01001577{
Chris Wilsondfaae392010-09-22 10:31:52 +01001578 while (!list_empty(&ring->request_list)) {
1579 struct drm_i915_gem_request *request;
Chris Wilson9375e442010-09-19 12:21:28 +01001580
Chris Wilsondfaae392010-09-22 10:31:52 +01001581 request = list_first_entry(&ring->request_list,
1582 struct drm_i915_gem_request,
1583 list);
1584
1585 list_del(&request->list);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001586 i915_gem_request_remove_from_client(request);
Chris Wilsondfaae392010-09-22 10:31:52 +01001587 kfree(request);
1588 }
1589
1590 while (!list_empty(&ring->active_list)) {
Chris Wilson05394f32010-11-08 19:18:58 +00001591 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001592
Chris Wilson05394f32010-11-08 19:18:58 +00001593 obj = list_first_entry(&ring->active_list,
1594 struct drm_i915_gem_object,
1595 ring_list);
Eric Anholt673a3942008-07-30 12:06:12 -07001596
Chris Wilson05394f32010-11-08 19:18:58 +00001597 obj->base.write_domain = 0;
1598 list_del_init(&obj->gpu_write_list);
1599 i915_gem_object_move_to_inactive(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001600 }
Eric Anholt673a3942008-07-30 12:06:12 -07001601}
1602
Chris Wilson312817a2010-11-22 11:50:11 +00001603static void i915_gem_reset_fences(struct drm_device *dev)
1604{
1605 struct drm_i915_private *dev_priv = dev->dev_private;
1606 int i;
1607
Daniel Vetter4b9de732011-10-09 21:52:02 +02001608 for (i = 0; i < dev_priv->num_fence_regs; i++) {
Chris Wilson312817a2010-11-22 11:50:11 +00001609 struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
Chris Wilson7d2cb392010-11-27 17:38:29 +00001610 struct drm_i915_gem_object *obj = reg->obj;
1611
1612 if (!obj)
1613 continue;
1614
1615 if (obj->tiling_mode)
1616 i915_gem_release_mmap(obj);
1617
Chris Wilsond9e86c02010-11-10 16:40:20 +00001618 reg->obj->fence_reg = I915_FENCE_REG_NONE;
1619 reg->obj->fenced_gpu_access = false;
1620 reg->obj->last_fenced_seqno = 0;
1621 reg->obj->last_fenced_ring = NULL;
1622 i915_gem_clear_fence_reg(dev, reg);
Chris Wilson312817a2010-11-22 11:50:11 +00001623 }
1624}
1625
Chris Wilson069efc12010-09-30 16:53:18 +01001626void i915_gem_reset(struct drm_device *dev)
Eric Anholt673a3942008-07-30 12:06:12 -07001627{
Chris Wilsondfaae392010-09-22 10:31:52 +01001628 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00001629 struct drm_i915_gem_object *obj;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001630 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07001631
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001632 for (i = 0; i < I915_NUM_RINGS; i++)
1633 i915_gem_reset_ring_lists(dev_priv, &dev_priv->ring[i]);
Chris Wilsondfaae392010-09-22 10:31:52 +01001634
1635 /* Remove anything from the flushing lists. The GPU cache is likely
1636 * to be lost on reset along with the data, so simply move the
1637 * lost bo to the inactive list.
1638 */
1639 while (!list_empty(&dev_priv->mm.flushing_list)) {
Akshay Joshi0206e352011-08-16 15:34:10 -04001640 obj = list_first_entry(&dev_priv->mm.flushing_list,
Chris Wilson05394f32010-11-08 19:18:58 +00001641 struct drm_i915_gem_object,
1642 mm_list);
Chris Wilson9375e442010-09-19 12:21:28 +01001643
Chris Wilson05394f32010-11-08 19:18:58 +00001644 obj->base.write_domain = 0;
1645 list_del_init(&obj->gpu_write_list);
1646 i915_gem_object_move_to_inactive(obj);
Chris Wilson9375e442010-09-19 12:21:28 +01001647 }
Chris Wilson9375e442010-09-19 12:21:28 +01001648
Chris Wilsondfaae392010-09-22 10:31:52 +01001649 /* Move everything out of the GPU domains to ensure we do any
1650 * necessary invalidation upon reuse.
1651 */
Chris Wilson05394f32010-11-08 19:18:58 +00001652 list_for_each_entry(obj,
Chris Wilson77f01232010-09-19 12:31:36 +01001653 &dev_priv->mm.inactive_list,
Chris Wilson69dc4982010-10-19 10:36:51 +01001654 mm_list)
Chris Wilson77f01232010-09-19 12:31:36 +01001655 {
Chris Wilson05394f32010-11-08 19:18:58 +00001656 obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
Chris Wilson77f01232010-09-19 12:31:36 +01001657 }
Chris Wilson069efc12010-09-30 16:53:18 +01001658
1659 /* The fence registers are invalidated so clear them out */
Chris Wilson312817a2010-11-22 11:50:11 +00001660 i915_gem_reset_fences(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07001661}
1662
1663/**
1664 * This function clears the request list as sequence numbers are passed.
1665 */
Chris Wilsona71d8d92012-02-15 11:25:36 +00001666void
Chris Wilsondb53a302011-02-03 11:57:46 +00001667i915_gem_retire_requests_ring(struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001668{
Eric Anholt673a3942008-07-30 12:06:12 -07001669 uint32_t seqno;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001670 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07001671
Chris Wilsondb53a302011-02-03 11:57:46 +00001672 if (list_empty(&ring->request_list))
Karsten Wiese6c0594a2009-02-23 15:07:57 +01001673 return;
1674
Chris Wilsondb53a302011-02-03 11:57:46 +00001675 WARN_ON(i915_verify_lists(ring->dev));
Eric Anholt673a3942008-07-30 12:06:12 -07001676
Chris Wilson78501ea2010-10-27 12:18:21 +01001677 seqno = ring->get_seqno(ring);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001678
Chris Wilson076e2c02011-01-21 10:07:18 +00001679 for (i = 0; i < ARRAY_SIZE(ring->sync_seqno); i++)
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001680 if (seqno >= ring->sync_seqno[i])
1681 ring->sync_seqno[i] = 0;
1682
Zou Nan hai852835f2010-05-21 09:08:56 +08001683 while (!list_empty(&ring->request_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001684 struct drm_i915_gem_request *request;
Eric Anholt673a3942008-07-30 12:06:12 -07001685
Zou Nan hai852835f2010-05-21 09:08:56 +08001686 request = list_first_entry(&ring->request_list,
Eric Anholt673a3942008-07-30 12:06:12 -07001687 struct drm_i915_gem_request,
1688 list);
Eric Anholt673a3942008-07-30 12:06:12 -07001689
Chris Wilsondfaae392010-09-22 10:31:52 +01001690 if (!i915_seqno_passed(seqno, request->seqno))
Eric Anholt673a3942008-07-30 12:06:12 -07001691 break;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001692
Chris Wilsondb53a302011-02-03 11:57:46 +00001693 trace_i915_gem_request_retire(ring, request->seqno);
Chris Wilsona71d8d92012-02-15 11:25:36 +00001694 /* We know the GPU must have read the request to have
1695 * sent us the seqno + interrupt, so use the position
1696 * of tail of the request to update the last known position
1697 * of the GPU head.
1698 */
1699 ring->last_retired_head = request->tail;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001700
1701 list_del(&request->list);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001702 i915_gem_request_remove_from_client(request);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001703 kfree(request);
1704 }
1705
1706 /* Move any buffers on the active list that are no longer referenced
1707 * by the ringbuffer to the flushing/inactive lists as appropriate.
1708 */
1709 while (!list_empty(&ring->active_list)) {
Chris Wilson05394f32010-11-08 19:18:58 +00001710 struct drm_i915_gem_object *obj;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001711
Akshay Joshi0206e352011-08-16 15:34:10 -04001712 obj = list_first_entry(&ring->active_list,
Chris Wilson05394f32010-11-08 19:18:58 +00001713 struct drm_i915_gem_object,
1714 ring_list);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001715
Chris Wilson05394f32010-11-08 19:18:58 +00001716 if (!i915_seqno_passed(seqno, obj->last_rendering_seqno))
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001717 break;
1718
Chris Wilson05394f32010-11-08 19:18:58 +00001719 if (obj->base.write_domain != 0)
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001720 i915_gem_object_move_to_flushing(obj);
1721 else
1722 i915_gem_object_move_to_inactive(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001723 }
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001724
Chris Wilsondb53a302011-02-03 11:57:46 +00001725 if (unlikely(ring->trace_irq_seqno &&
1726 i915_seqno_passed(seqno, ring->trace_irq_seqno))) {
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001727 ring->irq_put(ring);
Chris Wilsondb53a302011-02-03 11:57:46 +00001728 ring->trace_irq_seqno = 0;
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001729 }
Chris Wilson23bc5982010-09-29 16:10:57 +01001730
Chris Wilsondb53a302011-02-03 11:57:46 +00001731 WARN_ON(i915_verify_lists(ring->dev));
Eric Anholt673a3942008-07-30 12:06:12 -07001732}
1733
1734void
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001735i915_gem_retire_requests(struct drm_device *dev)
1736{
1737 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001738 int i;
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001739
Chris Wilsonbe726152010-07-23 23:18:50 +01001740 if (!list_empty(&dev_priv->mm.deferred_free_list)) {
Chris Wilson05394f32010-11-08 19:18:58 +00001741 struct drm_i915_gem_object *obj, *next;
Chris Wilsonbe726152010-07-23 23:18:50 +01001742
1743 /* We must be careful that during unbind() we do not
1744 * accidentally infinitely recurse into retire requests.
1745 * Currently:
1746 * retire -> free -> unbind -> wait -> retire_ring
1747 */
Chris Wilson05394f32010-11-08 19:18:58 +00001748 list_for_each_entry_safe(obj, next,
Chris Wilsonbe726152010-07-23 23:18:50 +01001749 &dev_priv->mm.deferred_free_list,
Chris Wilson69dc4982010-10-19 10:36:51 +01001750 mm_list)
Chris Wilson05394f32010-11-08 19:18:58 +00001751 i915_gem_free_object_tail(obj);
Chris Wilsonbe726152010-07-23 23:18:50 +01001752 }
1753
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001754 for (i = 0; i < I915_NUM_RINGS; i++)
Chris Wilsondb53a302011-02-03 11:57:46 +00001755 i915_gem_retire_requests_ring(&dev_priv->ring[i]);
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001756}
1757
Daniel Vetter75ef9da2010-08-21 00:25:16 +02001758static void
Eric Anholt673a3942008-07-30 12:06:12 -07001759i915_gem_retire_work_handler(struct work_struct *work)
1760{
1761 drm_i915_private_t *dev_priv;
1762 struct drm_device *dev;
Chris Wilson0a587052011-01-09 21:05:44 +00001763 bool idle;
1764 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07001765
1766 dev_priv = container_of(work, drm_i915_private_t,
1767 mm.retire_work.work);
1768 dev = dev_priv->dev;
1769
Chris Wilson891b48c2010-09-29 12:26:37 +01001770 /* Come back later if the device is busy... */
1771 if (!mutex_trylock(&dev->struct_mutex)) {
1772 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
1773 return;
1774 }
1775
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001776 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08001777
Chris Wilson0a587052011-01-09 21:05:44 +00001778 /* Send a periodic flush down the ring so we don't hold onto GEM
1779 * objects indefinitely.
1780 */
1781 idle = true;
1782 for (i = 0; i < I915_NUM_RINGS; i++) {
1783 struct intel_ring_buffer *ring = &dev_priv->ring[i];
1784
1785 if (!list_empty(&ring->gpu_write_list)) {
1786 struct drm_i915_gem_request *request;
1787 int ret;
1788
Chris Wilsondb53a302011-02-03 11:57:46 +00001789 ret = i915_gem_flush_ring(ring,
1790 0, I915_GEM_GPU_DOMAINS);
Chris Wilson0a587052011-01-09 21:05:44 +00001791 request = kzalloc(sizeof(*request), GFP_KERNEL);
1792 if (ret || request == NULL ||
Chris Wilsondb53a302011-02-03 11:57:46 +00001793 i915_add_request(ring, NULL, request))
Chris Wilson0a587052011-01-09 21:05:44 +00001794 kfree(request);
1795 }
1796
1797 idle &= list_empty(&ring->request_list);
1798 }
1799
1800 if (!dev_priv->mm.suspended && !idle)
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001801 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
Chris Wilson0a587052011-01-09 21:05:44 +00001802
Eric Anholt673a3942008-07-30 12:06:12 -07001803 mutex_unlock(&dev->struct_mutex);
1804}
1805
Chris Wilsondb53a302011-02-03 11:57:46 +00001806/**
1807 * Waits for a sequence number to be signaled, and cleans up the
1808 * request and object lists appropriately for that event.
1809 */
Daniel Vetter5a5a0c62009-09-15 22:57:36 +02001810int
Chris Wilsondb53a302011-02-03 11:57:46 +00001811i915_wait_request(struct intel_ring_buffer *ring,
Ben Widawskyb93f9cf2012-01-25 15:39:34 -08001812 uint32_t seqno,
1813 bool do_retire)
Eric Anholt673a3942008-07-30 12:06:12 -07001814{
Chris Wilsondb53a302011-02-03 11:57:46 +00001815 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001816 u32 ier;
Eric Anholt673a3942008-07-30 12:06:12 -07001817 int ret = 0;
1818
1819 BUG_ON(seqno == 0);
1820
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001821 if (atomic_read(&dev_priv->mm.wedged)) {
1822 struct completion *x = &dev_priv->error_completion;
1823 bool recovery_complete;
1824 unsigned long flags;
1825
1826 /* Give the error handler a chance to run. */
1827 spin_lock_irqsave(&x->wait.lock, flags);
1828 recovery_complete = x->done > 0;
1829 spin_unlock_irqrestore(&x->wait.lock, flags);
1830
1831 return recovery_complete ? -EIO : -EAGAIN;
1832 }
Ben Gamariffed1d02009-09-14 17:48:41 -04001833
Chris Wilson5d97eb62010-11-10 20:40:02 +00001834 if (seqno == ring->outstanding_lazy_request) {
Chris Wilson3cce4692010-10-27 16:11:02 +01001835 struct drm_i915_gem_request *request;
1836
1837 request = kzalloc(sizeof(*request), GFP_KERNEL);
1838 if (request == NULL)
Daniel Vettere35a41d2010-02-11 22:13:59 +01001839 return -ENOMEM;
Chris Wilson3cce4692010-10-27 16:11:02 +01001840
Chris Wilsondb53a302011-02-03 11:57:46 +00001841 ret = i915_add_request(ring, NULL, request);
Chris Wilson3cce4692010-10-27 16:11:02 +01001842 if (ret) {
1843 kfree(request);
1844 return ret;
1845 }
1846
1847 seqno = request->seqno;
Daniel Vettere35a41d2010-02-11 22:13:59 +01001848 }
1849
Chris Wilson78501ea2010-10-27 12:18:21 +01001850 if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) {
Chris Wilsondb53a302011-02-03 11:57:46 +00001851 if (HAS_PCH_SPLIT(ring->dev))
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001852 ier = I915_READ(DEIER) | I915_READ(GTIER);
1853 else
1854 ier = I915_READ(IER);
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001855 if (!ier) {
1856 DRM_ERROR("something (likely vbetool) disabled "
1857 "interrupts, re-enabling\n");
Chris Wilsonf01c22f2011-06-28 11:48:51 +01001858 ring->dev->driver->irq_preinstall(ring->dev);
1859 ring->dev->driver->irq_postinstall(ring->dev);
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001860 }
1861
Chris Wilsondb53a302011-02-03 11:57:46 +00001862 trace_i915_gem_request_wait_begin(ring, seqno);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001863
Chris Wilsonb2223492010-10-27 15:27:33 +01001864 ring->waiting_seqno = seqno;
Chris Wilsonb13c2b92010-12-13 16:54:50 +00001865 if (ring->irq_get(ring)) {
Chris Wilsonce453d82011-02-21 14:43:56 +00001866 if (dev_priv->mm.interruptible)
Chris Wilsonb13c2b92010-12-13 16:54:50 +00001867 ret = wait_event_interruptible(ring->irq_queue,
1868 i915_seqno_passed(ring->get_seqno(ring), seqno)
1869 || atomic_read(&dev_priv->mm.wedged));
1870 else
1871 wait_event(ring->irq_queue,
1872 i915_seqno_passed(ring->get_seqno(ring), seqno)
1873 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02001874
Chris Wilsonb13c2b92010-12-13 16:54:50 +00001875 ring->irq_put(ring);
Eric Anholte959b5d2011-12-22 14:55:01 -08001876 } else if (wait_for_atomic(i915_seqno_passed(ring->get_seqno(ring),
1877 seqno) ||
1878 atomic_read(&dev_priv->mm.wedged), 3000))
Chris Wilsonb5ba1772010-12-14 12:17:15 +00001879 ret = -EBUSY;
Chris Wilsonb2223492010-10-27 15:27:33 +01001880 ring->waiting_seqno = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001881
Chris Wilsondb53a302011-02-03 11:57:46 +00001882 trace_i915_gem_request_wait_end(ring, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07001883 }
Ben Gamariba1234d2009-09-14 17:48:47 -04001884 if (atomic_read(&dev_priv->mm.wedged))
Chris Wilson30dbf0c2010-09-25 10:19:17 +01001885 ret = -EAGAIN;
Eric Anholt673a3942008-07-30 12:06:12 -07001886
Eric Anholt673a3942008-07-30 12:06:12 -07001887 /* Directly dispatch request retiring. While we have the work queue
1888 * to handle this, the waiter on a request often wants an associated
1889 * buffer to have made it to the inactive list, and we would need
1890 * a separate wait queue to handle that.
1891 */
Ben Widawskyb93f9cf2012-01-25 15:39:34 -08001892 if (ret == 0 && do_retire)
Chris Wilsondb53a302011-02-03 11:57:46 +00001893 i915_gem_retire_requests_ring(ring);
Eric Anholt673a3942008-07-30 12:06:12 -07001894
1895 return ret;
1896}
1897
Daniel Vetter48764bf2009-09-15 22:57:32 +02001898/**
Eric Anholt673a3942008-07-30 12:06:12 -07001899 * Ensures that all rendering to the object has completed and the object is
1900 * safe to unbind from the GTT or access from the CPU.
1901 */
Chris Wilson54cf91d2010-11-25 18:00:26 +00001902int
Chris Wilsonce453d82011-02-21 14:43:56 +00001903i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07001904{
Eric Anholt673a3942008-07-30 12:06:12 -07001905 int ret;
1906
Eric Anholte47c68e2008-11-14 13:35:19 -08001907 /* This function only exists to support waiting for existing rendering,
1908 * not for emitting required flushes.
Eric Anholt673a3942008-07-30 12:06:12 -07001909 */
Chris Wilson05394f32010-11-08 19:18:58 +00001910 BUG_ON((obj->base.write_domain & I915_GEM_GPU_DOMAINS) != 0);
Eric Anholt673a3942008-07-30 12:06:12 -07001911
1912 /* If there is rendering queued on the buffer being evicted, wait for
1913 * it.
1914 */
Chris Wilson05394f32010-11-08 19:18:58 +00001915 if (obj->active) {
Ben Widawskyb93f9cf2012-01-25 15:39:34 -08001916 ret = i915_wait_request(obj->ring, obj->last_rendering_seqno,
1917 true);
Chris Wilson2cf34d72010-09-14 13:03:28 +01001918 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07001919 return ret;
1920 }
1921
1922 return 0;
1923}
1924
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01001925static void i915_gem_object_finish_gtt(struct drm_i915_gem_object *obj)
1926{
1927 u32 old_write_domain, old_read_domains;
1928
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01001929 /* Act a barrier for all accesses through the GTT */
1930 mb();
1931
1932 /* Force a pagefault for domain tracking on next user access */
1933 i915_gem_release_mmap(obj);
1934
Keith Packardb97c3d92011-06-24 21:02:59 -07001935 if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
1936 return;
1937
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01001938 old_read_domains = obj->base.read_domains;
1939 old_write_domain = obj->base.write_domain;
1940
1941 obj->base.read_domains &= ~I915_GEM_DOMAIN_GTT;
1942 obj->base.write_domain &= ~I915_GEM_DOMAIN_GTT;
1943
1944 trace_i915_gem_object_change_domain(obj,
1945 old_read_domains,
1946 old_write_domain);
1947}
1948
Eric Anholt673a3942008-07-30 12:06:12 -07001949/**
1950 * Unbinds an object from the GTT aperture.
1951 */
Jesse Barnes0f973f22009-01-26 17:10:45 -08001952int
Chris Wilson05394f32010-11-08 19:18:58 +00001953i915_gem_object_unbind(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07001954{
Daniel Vetter7bddb012012-02-09 17:15:47 +01001955 drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07001956 int ret = 0;
1957
Chris Wilson05394f32010-11-08 19:18:58 +00001958 if (obj->gtt_space == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07001959 return 0;
1960
Chris Wilson05394f32010-11-08 19:18:58 +00001961 if (obj->pin_count != 0) {
Eric Anholt673a3942008-07-30 12:06:12 -07001962 DRM_ERROR("Attempting to unbind pinned buffer\n");
1963 return -EINVAL;
1964 }
1965
Chris Wilsona8198ee2011-04-13 22:04:09 +01001966 ret = i915_gem_object_finish_gpu(obj);
Chris Wilson8dc17752010-07-23 23:18:51 +01001967 if (ret == -ERESTARTSYS)
Eric Anholt673a3942008-07-30 12:06:12 -07001968 return ret;
Chris Wilson8dc17752010-07-23 23:18:51 +01001969 /* Continue on if we fail due to EIO, the GPU is hung so we
1970 * should be safe and we need to cleanup or else we might
1971 * cause memory corruption through use-after-free.
1972 */
Chris Wilsona8198ee2011-04-13 22:04:09 +01001973
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01001974 i915_gem_object_finish_gtt(obj);
Chris Wilsona8198ee2011-04-13 22:04:09 +01001975
1976 /* Move the object to the CPU domain to ensure that
1977 * any possible CPU writes while it's not in the GTT
1978 * are flushed when we go to remap it.
1979 */
1980 if (ret == 0)
1981 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
1982 if (ret == -ERESTARTSYS)
1983 return ret;
Chris Wilson812ed4922010-09-30 15:08:57 +01001984 if (ret) {
Chris Wilsona8198ee2011-04-13 22:04:09 +01001985 /* In the event of a disaster, abandon all caches and
1986 * hope for the best.
1987 */
Chris Wilson812ed4922010-09-30 15:08:57 +01001988 i915_gem_clflush_object(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00001989 obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
Chris Wilson812ed4922010-09-30 15:08:57 +01001990 }
Eric Anholt673a3942008-07-30 12:06:12 -07001991
Daniel Vetter96b47b62009-12-15 17:50:00 +01001992 /* release the fence reg _after_ flushing */
Chris Wilsond9e86c02010-11-10 16:40:20 +00001993 ret = i915_gem_object_put_fence(obj);
1994 if (ret == -ERESTARTSYS)
1995 return ret;
Daniel Vetter96b47b62009-12-15 17:50:00 +01001996
Chris Wilsondb53a302011-02-03 11:57:46 +00001997 trace_i915_gem_object_unbind(obj);
1998
Daniel Vetter74898d72012-02-15 23:50:22 +01001999 if (obj->has_global_gtt_mapping)
2000 i915_gem_gtt_unbind_object(obj);
Daniel Vetter7bddb012012-02-09 17:15:47 +01002001 if (obj->has_aliasing_ppgtt_mapping) {
2002 i915_ppgtt_unbind_object(dev_priv->mm.aliasing_ppgtt, obj);
2003 obj->has_aliasing_ppgtt_mapping = 0;
2004 }
Daniel Vetter74163902012-02-15 23:50:21 +01002005 i915_gem_gtt_finish_object(obj);
Daniel Vetter7bddb012012-02-09 17:15:47 +01002006
Chris Wilsone5281cc2010-10-28 13:45:36 +01002007 i915_gem_object_put_pages_gtt(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002008
Chris Wilson6299f992010-11-24 12:23:44 +00002009 list_del_init(&obj->gtt_list);
Chris Wilson05394f32010-11-08 19:18:58 +00002010 list_del_init(&obj->mm_list);
Daniel Vetter75e9e912010-11-04 17:11:09 +01002011 /* Avoid an unnecessary call to unbind on rebind. */
Chris Wilson05394f32010-11-08 19:18:58 +00002012 obj->map_and_fenceable = true;
Eric Anholt673a3942008-07-30 12:06:12 -07002013
Chris Wilson05394f32010-11-08 19:18:58 +00002014 drm_mm_put_block(obj->gtt_space);
2015 obj->gtt_space = NULL;
2016 obj->gtt_offset = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002017
Chris Wilson05394f32010-11-08 19:18:58 +00002018 if (i915_gem_object_is_purgeable(obj))
Chris Wilson963b4832009-09-20 23:03:54 +01002019 i915_gem_object_truncate(obj);
2020
Chris Wilson8dc17752010-07-23 23:18:51 +01002021 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002022}
2023
Chris Wilson88241782011-01-07 17:09:48 +00002024int
Chris Wilsondb53a302011-02-03 11:57:46 +00002025i915_gem_flush_ring(struct intel_ring_buffer *ring,
Chris Wilson54cf91d2010-11-25 18:00:26 +00002026 uint32_t invalidate_domains,
2027 uint32_t flush_domains)
2028{
Chris Wilson88241782011-01-07 17:09:48 +00002029 int ret;
2030
Chris Wilson36d527d2011-03-19 22:26:49 +00002031 if (((invalidate_domains | flush_domains) & I915_GEM_GPU_DOMAINS) == 0)
2032 return 0;
2033
Chris Wilsondb53a302011-02-03 11:57:46 +00002034 trace_i915_gem_ring_flush(ring, invalidate_domains, flush_domains);
2035
Chris Wilson88241782011-01-07 17:09:48 +00002036 ret = ring->flush(ring, invalidate_domains, flush_domains);
2037 if (ret)
2038 return ret;
2039
Chris Wilson36d527d2011-03-19 22:26:49 +00002040 if (flush_domains & I915_GEM_GPU_DOMAINS)
2041 i915_gem_process_flushing_list(ring, flush_domains);
2042
Chris Wilson88241782011-01-07 17:09:48 +00002043 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002044}
2045
Ben Widawskyb93f9cf2012-01-25 15:39:34 -08002046static int i915_ring_idle(struct intel_ring_buffer *ring, bool do_retire)
Chris Wilsona56ba562010-09-28 10:07:56 +01002047{
Chris Wilson88241782011-01-07 17:09:48 +00002048 int ret;
2049
Chris Wilson395b70b2010-10-28 21:28:46 +01002050 if (list_empty(&ring->gpu_write_list) && list_empty(&ring->active_list))
Chris Wilson64193402010-10-24 12:38:05 +01002051 return 0;
2052
Chris Wilson88241782011-01-07 17:09:48 +00002053 if (!list_empty(&ring->gpu_write_list)) {
Chris Wilsondb53a302011-02-03 11:57:46 +00002054 ret = i915_gem_flush_ring(ring,
Chris Wilson0ac74c62010-12-06 14:36:02 +00002055 I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
Chris Wilson88241782011-01-07 17:09:48 +00002056 if (ret)
2057 return ret;
2058 }
2059
Ben Widawskyb93f9cf2012-01-25 15:39:34 -08002060 return i915_wait_request(ring, i915_gem_next_request_seqno(ring),
2061 do_retire);
Chris Wilsona56ba562010-09-28 10:07:56 +01002062}
2063
Ben Widawskyb93f9cf2012-01-25 15:39:34 -08002064int i915_gpu_idle(struct drm_device *dev, bool do_retire)
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002065{
2066 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002067 int ret, i;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002068
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002069 /* Flush everything onto the inactive list. */
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002070 for (i = 0; i < I915_NUM_RINGS; i++) {
Ben Widawskyb93f9cf2012-01-25 15:39:34 -08002071 ret = i915_ring_idle(&dev_priv->ring[i], do_retire);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002072 if (ret)
2073 return ret;
2074 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08002075
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002076 return 0;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002077}
2078
Daniel Vetterc6642782010-11-12 13:46:18 +00002079static int sandybridge_write_fence_reg(struct drm_i915_gem_object *obj,
2080 struct intel_ring_buffer *pipelined)
Eric Anholt4e901fd2009-10-26 16:44:17 -07002081{
Chris Wilson05394f32010-11-08 19:18:58 +00002082 struct drm_device *dev = obj->base.dev;
Eric Anholt4e901fd2009-10-26 16:44:17 -07002083 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00002084 u32 size = obj->gtt_space->size;
2085 int regnum = obj->fence_reg;
Eric Anholt4e901fd2009-10-26 16:44:17 -07002086 uint64_t val;
2087
Chris Wilson05394f32010-11-08 19:18:58 +00002088 val = (uint64_t)((obj->gtt_offset + size - 4096) &
Daniel Vetterc6642782010-11-12 13:46:18 +00002089 0xfffff000) << 32;
Chris Wilson05394f32010-11-08 19:18:58 +00002090 val |= obj->gtt_offset & 0xfffff000;
2091 val |= (uint64_t)((obj->stride / 128) - 1) <<
Eric Anholt4e901fd2009-10-26 16:44:17 -07002092 SANDYBRIDGE_FENCE_PITCH_SHIFT;
2093
Chris Wilson05394f32010-11-08 19:18:58 +00002094 if (obj->tiling_mode == I915_TILING_Y)
Eric Anholt4e901fd2009-10-26 16:44:17 -07002095 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2096 val |= I965_FENCE_REG_VALID;
2097
Daniel Vetterc6642782010-11-12 13:46:18 +00002098 if (pipelined) {
2099 int ret = intel_ring_begin(pipelined, 6);
2100 if (ret)
2101 return ret;
2102
2103 intel_ring_emit(pipelined, MI_NOOP);
2104 intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(2));
2105 intel_ring_emit(pipelined, FENCE_REG_SANDYBRIDGE_0 + regnum*8);
2106 intel_ring_emit(pipelined, (u32)val);
2107 intel_ring_emit(pipelined, FENCE_REG_SANDYBRIDGE_0 + regnum*8 + 4);
2108 intel_ring_emit(pipelined, (u32)(val >> 32));
2109 intel_ring_advance(pipelined);
2110 } else
2111 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + regnum * 8, val);
2112
2113 return 0;
Eric Anholt4e901fd2009-10-26 16:44:17 -07002114}
2115
Daniel Vetterc6642782010-11-12 13:46:18 +00002116static int i965_write_fence_reg(struct drm_i915_gem_object *obj,
2117 struct intel_ring_buffer *pipelined)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002118{
Chris Wilson05394f32010-11-08 19:18:58 +00002119 struct drm_device *dev = obj->base.dev;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002120 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00002121 u32 size = obj->gtt_space->size;
2122 int regnum = obj->fence_reg;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002123 uint64_t val;
2124
Chris Wilson05394f32010-11-08 19:18:58 +00002125 val = (uint64_t)((obj->gtt_offset + size - 4096) &
Jesse Barnesde151cf2008-11-12 10:03:55 -08002126 0xfffff000) << 32;
Chris Wilson05394f32010-11-08 19:18:58 +00002127 val |= obj->gtt_offset & 0xfffff000;
2128 val |= ((obj->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
2129 if (obj->tiling_mode == I915_TILING_Y)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002130 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2131 val |= I965_FENCE_REG_VALID;
2132
Daniel Vetterc6642782010-11-12 13:46:18 +00002133 if (pipelined) {
2134 int ret = intel_ring_begin(pipelined, 6);
2135 if (ret)
2136 return ret;
2137
2138 intel_ring_emit(pipelined, MI_NOOP);
2139 intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(2));
2140 intel_ring_emit(pipelined, FENCE_REG_965_0 + regnum*8);
2141 intel_ring_emit(pipelined, (u32)val);
2142 intel_ring_emit(pipelined, FENCE_REG_965_0 + regnum*8 + 4);
2143 intel_ring_emit(pipelined, (u32)(val >> 32));
2144 intel_ring_advance(pipelined);
2145 } else
2146 I915_WRITE64(FENCE_REG_965_0 + regnum * 8, val);
2147
2148 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002149}
2150
Daniel Vetterc6642782010-11-12 13:46:18 +00002151static int i915_write_fence_reg(struct drm_i915_gem_object *obj,
2152 struct intel_ring_buffer *pipelined)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002153{
Chris Wilson05394f32010-11-08 19:18:58 +00002154 struct drm_device *dev = obj->base.dev;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002155 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00002156 u32 size = obj->gtt_space->size;
Daniel Vetterc6642782010-11-12 13:46:18 +00002157 u32 fence_reg, val, pitch_val;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002158 int tile_width;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002159
Daniel Vetterc6642782010-11-12 13:46:18 +00002160 if (WARN((obj->gtt_offset & ~I915_FENCE_START_MASK) ||
2161 (size & -size) != size ||
2162 (obj->gtt_offset & (size - 1)),
2163 "object 0x%08x [fenceable? %d] not 1M or pot-size (0x%08x) aligned\n",
2164 obj->gtt_offset, obj->map_and_fenceable, size))
2165 return -EINVAL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002166
Daniel Vetterc6642782010-11-12 13:46:18 +00002167 if (obj->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev))
Jesse Barnes0f973f22009-01-26 17:10:45 -08002168 tile_width = 128;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002169 else
Jesse Barnes0f973f22009-01-26 17:10:45 -08002170 tile_width = 512;
2171
2172 /* Note: pitch better be a power of two tile widths */
Chris Wilson05394f32010-11-08 19:18:58 +00002173 pitch_val = obj->stride / tile_width;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002174 pitch_val = ffs(pitch_val) - 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002175
Chris Wilson05394f32010-11-08 19:18:58 +00002176 val = obj->gtt_offset;
2177 if (obj->tiling_mode == I915_TILING_Y)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002178 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002179 val |= I915_FENCE_SIZE_BITS(size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002180 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2181 val |= I830_FENCE_REG_VALID;
2182
Chris Wilson05394f32010-11-08 19:18:58 +00002183 fence_reg = obj->fence_reg;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002184 if (fence_reg < 8)
2185 fence_reg = FENCE_REG_830_0 + fence_reg * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002186 else
Chris Wilsona00b10c2010-09-24 21:15:47 +01002187 fence_reg = FENCE_REG_945_8 + (fence_reg - 8) * 4;
Daniel Vetterc6642782010-11-12 13:46:18 +00002188
2189 if (pipelined) {
2190 int ret = intel_ring_begin(pipelined, 4);
2191 if (ret)
2192 return ret;
2193
2194 intel_ring_emit(pipelined, MI_NOOP);
2195 intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(1));
2196 intel_ring_emit(pipelined, fence_reg);
2197 intel_ring_emit(pipelined, val);
2198 intel_ring_advance(pipelined);
2199 } else
2200 I915_WRITE(fence_reg, val);
2201
2202 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002203}
2204
Daniel Vetterc6642782010-11-12 13:46:18 +00002205static int i830_write_fence_reg(struct drm_i915_gem_object *obj,
2206 struct intel_ring_buffer *pipelined)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002207{
Chris Wilson05394f32010-11-08 19:18:58 +00002208 struct drm_device *dev = obj->base.dev;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002209 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00002210 u32 size = obj->gtt_space->size;
2211 int regnum = obj->fence_reg;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002212 uint32_t val;
2213 uint32_t pitch_val;
2214
Daniel Vetterc6642782010-11-12 13:46:18 +00002215 if (WARN((obj->gtt_offset & ~I830_FENCE_START_MASK) ||
2216 (size & -size) != size ||
2217 (obj->gtt_offset & (size - 1)),
2218 "object 0x%08x not 512K or pot-size 0x%08x aligned\n",
2219 obj->gtt_offset, size))
2220 return -EINVAL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002221
Chris Wilson05394f32010-11-08 19:18:58 +00002222 pitch_val = obj->stride / 128;
Eric Anholte76a16d2009-05-26 17:44:56 -07002223 pitch_val = ffs(pitch_val) - 1;
Eric Anholte76a16d2009-05-26 17:44:56 -07002224
Chris Wilson05394f32010-11-08 19:18:58 +00002225 val = obj->gtt_offset;
2226 if (obj->tiling_mode == I915_TILING_Y)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002227 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Daniel Vetterc6642782010-11-12 13:46:18 +00002228 val |= I830_FENCE_SIZE_BITS(size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002229 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2230 val |= I830_FENCE_REG_VALID;
2231
Daniel Vetterc6642782010-11-12 13:46:18 +00002232 if (pipelined) {
2233 int ret = intel_ring_begin(pipelined, 4);
2234 if (ret)
2235 return ret;
2236
2237 intel_ring_emit(pipelined, MI_NOOP);
2238 intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(1));
2239 intel_ring_emit(pipelined, FENCE_REG_830_0 + regnum*4);
2240 intel_ring_emit(pipelined, val);
2241 intel_ring_advance(pipelined);
2242 } else
2243 I915_WRITE(FENCE_REG_830_0 + regnum * 4, val);
2244
2245 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002246}
2247
Chris Wilsond9e86c02010-11-10 16:40:20 +00002248static bool ring_passed_seqno(struct intel_ring_buffer *ring, u32 seqno)
2249{
2250 return i915_seqno_passed(ring->get_seqno(ring), seqno);
2251}
2252
2253static int
2254i915_gem_object_flush_fence(struct drm_i915_gem_object *obj,
Chris Wilsonce453d82011-02-21 14:43:56 +00002255 struct intel_ring_buffer *pipelined)
Chris Wilsond9e86c02010-11-10 16:40:20 +00002256{
2257 int ret;
2258
2259 if (obj->fenced_gpu_access) {
Chris Wilson88241782011-01-07 17:09:48 +00002260 if (obj->base.write_domain & I915_GEM_GPU_DOMAINS) {
Chris Wilsondb53a302011-02-03 11:57:46 +00002261 ret = i915_gem_flush_ring(obj->last_fenced_ring,
Chris Wilson88241782011-01-07 17:09:48 +00002262 0, obj->base.write_domain);
2263 if (ret)
2264 return ret;
2265 }
Chris Wilsond9e86c02010-11-10 16:40:20 +00002266
2267 obj->fenced_gpu_access = false;
2268 }
2269
2270 if (obj->last_fenced_seqno && pipelined != obj->last_fenced_ring) {
2271 if (!ring_passed_seqno(obj->last_fenced_ring,
2272 obj->last_fenced_seqno)) {
Chris Wilsondb53a302011-02-03 11:57:46 +00002273 ret = i915_wait_request(obj->last_fenced_ring,
Ben Widawskyb93f9cf2012-01-25 15:39:34 -08002274 obj->last_fenced_seqno,
2275 true);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002276 if (ret)
2277 return ret;
2278 }
2279
2280 obj->last_fenced_seqno = 0;
2281 obj->last_fenced_ring = NULL;
2282 }
2283
Chris Wilson63256ec2011-01-04 18:42:07 +00002284 /* Ensure that all CPU reads are completed before installing a fence
2285 * and all writes before removing the fence.
2286 */
2287 if (obj->base.read_domains & I915_GEM_DOMAIN_GTT)
2288 mb();
2289
Chris Wilsond9e86c02010-11-10 16:40:20 +00002290 return 0;
2291}
2292
2293int
2294i915_gem_object_put_fence(struct drm_i915_gem_object *obj)
2295{
2296 int ret;
2297
2298 if (obj->tiling_mode)
2299 i915_gem_release_mmap(obj);
2300
Chris Wilsonce453d82011-02-21 14:43:56 +00002301 ret = i915_gem_object_flush_fence(obj, NULL);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002302 if (ret)
2303 return ret;
2304
2305 if (obj->fence_reg != I915_FENCE_REG_NONE) {
2306 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002307
2308 WARN_ON(dev_priv->fence_regs[obj->fence_reg].pin_count);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002309 i915_gem_clear_fence_reg(obj->base.dev,
2310 &dev_priv->fence_regs[obj->fence_reg]);
2311
2312 obj->fence_reg = I915_FENCE_REG_NONE;
2313 }
2314
2315 return 0;
2316}
2317
2318static struct drm_i915_fence_reg *
2319i915_find_fence_reg(struct drm_device *dev,
2320 struct intel_ring_buffer *pipelined)
Daniel Vetterae3db242010-02-19 11:51:58 +01002321{
Daniel Vetterae3db242010-02-19 11:51:58 +01002322 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002323 struct drm_i915_fence_reg *reg, *first, *avail;
2324 int i;
Daniel Vetterae3db242010-02-19 11:51:58 +01002325
2326 /* First try to find a free reg */
Chris Wilsond9e86c02010-11-10 16:40:20 +00002327 avail = NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002328 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2329 reg = &dev_priv->fence_regs[i];
2330 if (!reg->obj)
Chris Wilsond9e86c02010-11-10 16:40:20 +00002331 return reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01002332
Chris Wilson1690e1e2011-12-14 13:57:08 +01002333 if (!reg->pin_count)
Chris Wilsond9e86c02010-11-10 16:40:20 +00002334 avail = reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01002335 }
2336
Chris Wilsond9e86c02010-11-10 16:40:20 +00002337 if (avail == NULL)
2338 return NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002339
2340 /* None available, try to steal one or wait for a user to finish */
Chris Wilsond9e86c02010-11-10 16:40:20 +00002341 avail = first = NULL;
2342 list_for_each_entry(reg, &dev_priv->mm.fence_list, lru_list) {
Chris Wilson1690e1e2011-12-14 13:57:08 +01002343 if (reg->pin_count)
Daniel Vetterae3db242010-02-19 11:51:58 +01002344 continue;
2345
Chris Wilsond9e86c02010-11-10 16:40:20 +00002346 if (first == NULL)
2347 first = reg;
2348
2349 if (!pipelined ||
2350 !reg->obj->last_fenced_ring ||
2351 reg->obj->last_fenced_ring == pipelined) {
2352 avail = reg;
2353 break;
2354 }
Daniel Vetterae3db242010-02-19 11:51:58 +01002355 }
2356
Chris Wilsond9e86c02010-11-10 16:40:20 +00002357 if (avail == NULL)
2358 avail = first;
Daniel Vetterae3db242010-02-19 11:51:58 +01002359
Chris Wilsona00b10c2010-09-24 21:15:47 +01002360 return avail;
Daniel Vetterae3db242010-02-19 11:51:58 +01002361}
2362
Jesse Barnesde151cf2008-11-12 10:03:55 -08002363/**
Chris Wilsond9e86c02010-11-10 16:40:20 +00002364 * i915_gem_object_get_fence - set up a fence reg for an object
Jesse Barnesde151cf2008-11-12 10:03:55 -08002365 * @obj: object to map through a fence reg
Chris Wilsond9e86c02010-11-10 16:40:20 +00002366 * @pipelined: ring on which to queue the change, or NULL for CPU access
2367 * @interruptible: must we wait uninterruptibly for the register to retire?
Jesse Barnesde151cf2008-11-12 10:03:55 -08002368 *
2369 * When mapping objects through the GTT, userspace wants to be able to write
2370 * to them without having to worry about swizzling if the object is tiled.
2371 *
2372 * This function walks the fence regs looking for a free one for @obj,
2373 * stealing one if it can't find any.
2374 *
2375 * It then sets up the reg based on the object's properties: address, pitch
2376 * and tiling format.
2377 */
Chris Wilson8c4b8c32009-06-17 22:08:52 +01002378int
Chris Wilsond9e86c02010-11-10 16:40:20 +00002379i915_gem_object_get_fence(struct drm_i915_gem_object *obj,
Chris Wilsonce453d82011-02-21 14:43:56 +00002380 struct intel_ring_buffer *pipelined)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002381{
Chris Wilson05394f32010-11-08 19:18:58 +00002382 struct drm_device *dev = obj->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002383 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002384 struct drm_i915_fence_reg *reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01002385 int ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002386
Chris Wilson6bda10d2010-12-05 21:04:18 +00002387 /* XXX disable pipelining. There are bugs. Shocking. */
2388 pipelined = NULL;
2389
Chris Wilsond9e86c02010-11-10 16:40:20 +00002390 /* Just update our place in the LRU if our fence is getting reused. */
Chris Wilson05394f32010-11-08 19:18:58 +00002391 if (obj->fence_reg != I915_FENCE_REG_NONE) {
2392 reg = &dev_priv->fence_regs[obj->fence_reg];
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002393 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002394
Chris Wilson29c5a582011-03-17 15:23:22 +00002395 if (obj->tiling_changed) {
2396 ret = i915_gem_object_flush_fence(obj, pipelined);
2397 if (ret)
2398 return ret;
2399
2400 if (!obj->fenced_gpu_access && !obj->last_fenced_seqno)
2401 pipelined = NULL;
2402
2403 if (pipelined) {
2404 reg->setup_seqno =
2405 i915_gem_next_request_seqno(pipelined);
2406 obj->last_fenced_seqno = reg->setup_seqno;
2407 obj->last_fenced_ring = pipelined;
2408 }
2409
2410 goto update;
2411 }
Chris Wilsond9e86c02010-11-10 16:40:20 +00002412
2413 if (!pipelined) {
2414 if (reg->setup_seqno) {
2415 if (!ring_passed_seqno(obj->last_fenced_ring,
2416 reg->setup_seqno)) {
Chris Wilsondb53a302011-02-03 11:57:46 +00002417 ret = i915_wait_request(obj->last_fenced_ring,
Ben Widawskyb93f9cf2012-01-25 15:39:34 -08002418 reg->setup_seqno,
2419 true);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002420 if (ret)
2421 return ret;
2422 }
2423
2424 reg->setup_seqno = 0;
2425 }
2426 } else if (obj->last_fenced_ring &&
2427 obj->last_fenced_ring != pipelined) {
Chris Wilsonce453d82011-02-21 14:43:56 +00002428 ret = i915_gem_object_flush_fence(obj, pipelined);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002429 if (ret)
2430 return ret;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002431 }
2432
Eric Anholta09ba7f2009-08-29 12:49:51 -07002433 return 0;
2434 }
2435
Chris Wilsond9e86c02010-11-10 16:40:20 +00002436 reg = i915_find_fence_reg(dev, pipelined);
2437 if (reg == NULL)
Daniel Vetter39965b32011-12-14 13:57:09 +01002438 return -EDEADLK;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002439
Chris Wilsonce453d82011-02-21 14:43:56 +00002440 ret = i915_gem_object_flush_fence(obj, pipelined);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002441 if (ret)
Daniel Vetterae3db242010-02-19 11:51:58 +01002442 return ret;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002443
Chris Wilsond9e86c02010-11-10 16:40:20 +00002444 if (reg->obj) {
2445 struct drm_i915_gem_object *old = reg->obj;
2446
2447 drm_gem_object_reference(&old->base);
2448
2449 if (old->tiling_mode)
2450 i915_gem_release_mmap(old);
2451
Chris Wilsonce453d82011-02-21 14:43:56 +00002452 ret = i915_gem_object_flush_fence(old, pipelined);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002453 if (ret) {
2454 drm_gem_object_unreference(&old->base);
2455 return ret;
2456 }
2457
2458 if (old->last_fenced_seqno == 0 && obj->last_fenced_seqno == 0)
2459 pipelined = NULL;
2460
2461 old->fence_reg = I915_FENCE_REG_NONE;
2462 old->last_fenced_ring = pipelined;
2463 old->last_fenced_seqno =
Chris Wilsondb53a302011-02-03 11:57:46 +00002464 pipelined ? i915_gem_next_request_seqno(pipelined) : 0;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002465
2466 drm_gem_object_unreference(&old->base);
2467 } else if (obj->last_fenced_seqno == 0)
2468 pipelined = NULL;
Eric Anholta09ba7f2009-08-29 12:49:51 -07002469
Jesse Barnesde151cf2008-11-12 10:03:55 -08002470 reg->obj = obj;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002471 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
2472 obj->fence_reg = reg - dev_priv->fence_regs;
2473 obj->last_fenced_ring = pipelined;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002474
Chris Wilsond9e86c02010-11-10 16:40:20 +00002475 reg->setup_seqno =
Chris Wilsondb53a302011-02-03 11:57:46 +00002476 pipelined ? i915_gem_next_request_seqno(pipelined) : 0;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002477 obj->last_fenced_seqno = reg->setup_seqno;
2478
2479update:
2480 obj->tiling_changed = false;
Chris Wilsone259bef2010-09-17 00:32:02 +01002481 switch (INTEL_INFO(dev)->gen) {
Eric Anholt25aebfc32011-05-06 13:55:53 -07002482 case 7:
Chris Wilsone259bef2010-09-17 00:32:02 +01002483 case 6:
Daniel Vetterc6642782010-11-12 13:46:18 +00002484 ret = sandybridge_write_fence_reg(obj, pipelined);
Chris Wilsone259bef2010-09-17 00:32:02 +01002485 break;
2486 case 5:
2487 case 4:
Daniel Vetterc6642782010-11-12 13:46:18 +00002488 ret = i965_write_fence_reg(obj, pipelined);
Chris Wilsone259bef2010-09-17 00:32:02 +01002489 break;
2490 case 3:
Daniel Vetterc6642782010-11-12 13:46:18 +00002491 ret = i915_write_fence_reg(obj, pipelined);
Chris Wilsone259bef2010-09-17 00:32:02 +01002492 break;
2493 case 2:
Daniel Vetterc6642782010-11-12 13:46:18 +00002494 ret = i830_write_fence_reg(obj, pipelined);
Chris Wilsone259bef2010-09-17 00:32:02 +01002495 break;
2496 }
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002497
Daniel Vetterc6642782010-11-12 13:46:18 +00002498 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002499}
2500
2501/**
2502 * i915_gem_clear_fence_reg - clear out fence register info
2503 * @obj: object to clear
2504 *
2505 * Zeroes out the fence register itself and clears out the associated
Chris Wilson05394f32010-11-08 19:18:58 +00002506 * data structures in dev_priv and obj.
Jesse Barnesde151cf2008-11-12 10:03:55 -08002507 */
2508static void
Chris Wilsond9e86c02010-11-10 16:40:20 +00002509i915_gem_clear_fence_reg(struct drm_device *dev,
2510 struct drm_i915_fence_reg *reg)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002511{
Jesse Barnes79e53942008-11-07 14:24:08 -08002512 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002513 uint32_t fence_reg = reg - dev_priv->fence_regs;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002514
Chris Wilsone259bef2010-09-17 00:32:02 +01002515 switch (INTEL_INFO(dev)->gen) {
Eric Anholt25aebfc32011-05-06 13:55:53 -07002516 case 7:
Chris Wilsone259bef2010-09-17 00:32:02 +01002517 case 6:
Chris Wilsond9e86c02010-11-10 16:40:20 +00002518 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + fence_reg*8, 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002519 break;
2520 case 5:
2521 case 4:
Chris Wilsond9e86c02010-11-10 16:40:20 +00002522 I915_WRITE64(FENCE_REG_965_0 + fence_reg*8, 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002523 break;
2524 case 3:
Chris Wilsond9e86c02010-11-10 16:40:20 +00002525 if (fence_reg >= 8)
2526 fence_reg = FENCE_REG_945_8 + (fence_reg - 8) * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002527 else
Chris Wilsone259bef2010-09-17 00:32:02 +01002528 case 2:
Chris Wilsond9e86c02010-11-10 16:40:20 +00002529 fence_reg = FENCE_REG_830_0 + fence_reg * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002530
2531 I915_WRITE(fence_reg, 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002532 break;
Eric Anholtdc529a42009-03-10 22:34:49 -07002533 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002534
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002535 list_del_init(&reg->lru_list);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002536 reg->obj = NULL;
2537 reg->setup_seqno = 0;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002538 reg->pin_count = 0;
Chris Wilson52dc7d32009-06-06 09:46:01 +01002539}
2540
2541/**
Eric Anholt673a3942008-07-30 12:06:12 -07002542 * Finds free space in the GTT aperture and binds the object there.
2543 */
2544static int
Chris Wilson05394f32010-11-08 19:18:58 +00002545i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
Daniel Vetter920afa72010-09-16 17:54:23 +02002546 unsigned alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01002547 bool map_and_fenceable)
Eric Anholt673a3942008-07-30 12:06:12 -07002548{
Chris Wilson05394f32010-11-08 19:18:58 +00002549 struct drm_device *dev = obj->base.dev;
Eric Anholt673a3942008-07-30 12:06:12 -07002550 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07002551 struct drm_mm_node *free_space;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002552 gfp_t gfpmask = __GFP_NORETRY | __GFP_NOWARN;
Daniel Vetter5e783302010-11-14 22:32:36 +01002553 u32 size, fence_size, fence_alignment, unfenced_alignment;
Daniel Vetter75e9e912010-11-04 17:11:09 +01002554 bool mappable, fenceable;
Chris Wilson07f73f62009-09-14 16:50:30 +01002555 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002556
Chris Wilson05394f32010-11-08 19:18:58 +00002557 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01002558 DRM_ERROR("Attempting to bind a purgeable object\n");
2559 return -EINVAL;
2560 }
2561
Chris Wilsone28f8712011-07-18 13:11:49 -07002562 fence_size = i915_gem_get_gtt_size(dev,
2563 obj->base.size,
2564 obj->tiling_mode);
2565 fence_alignment = i915_gem_get_gtt_alignment(dev,
2566 obj->base.size,
2567 obj->tiling_mode);
2568 unfenced_alignment =
2569 i915_gem_get_unfenced_gtt_alignment(dev,
2570 obj->base.size,
2571 obj->tiling_mode);
Chris Wilsona00b10c2010-09-24 21:15:47 +01002572
Eric Anholt673a3942008-07-30 12:06:12 -07002573 if (alignment == 0)
Daniel Vetter5e783302010-11-14 22:32:36 +01002574 alignment = map_and_fenceable ? fence_alignment :
2575 unfenced_alignment;
Daniel Vetter75e9e912010-11-04 17:11:09 +01002576 if (map_and_fenceable && alignment & (fence_alignment - 1)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002577 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2578 return -EINVAL;
2579 }
2580
Chris Wilson05394f32010-11-08 19:18:58 +00002581 size = map_and_fenceable ? fence_size : obj->base.size;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002582
Chris Wilson654fc602010-05-27 13:18:21 +01002583 /* If the object is bigger than the entire aperture, reject it early
2584 * before evicting everything in a vain attempt to find space.
2585 */
Chris Wilson05394f32010-11-08 19:18:58 +00002586 if (obj->base.size >
Daniel Vetter75e9e912010-11-04 17:11:09 +01002587 (map_and_fenceable ? dev_priv->mm.gtt_mappable_end : dev_priv->mm.gtt_total)) {
Chris Wilson654fc602010-05-27 13:18:21 +01002588 DRM_ERROR("Attempting to bind an object larger than the aperture\n");
2589 return -E2BIG;
2590 }
2591
Eric Anholt673a3942008-07-30 12:06:12 -07002592 search_free:
Daniel Vetter75e9e912010-11-04 17:11:09 +01002593 if (map_and_fenceable)
Daniel Vetter920afa72010-09-16 17:54:23 +02002594 free_space =
2595 drm_mm_search_free_in_range(&dev_priv->mm.gtt_space,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002596 size, alignment, 0,
Daniel Vetter920afa72010-09-16 17:54:23 +02002597 dev_priv->mm.gtt_mappable_end,
2598 0);
2599 else
2600 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002601 size, alignment, 0);
Daniel Vetter920afa72010-09-16 17:54:23 +02002602
2603 if (free_space != NULL) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01002604 if (map_and_fenceable)
Chris Wilson05394f32010-11-08 19:18:58 +00002605 obj->gtt_space =
Daniel Vetter920afa72010-09-16 17:54:23 +02002606 drm_mm_get_block_range_generic(free_space,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002607 size, alignment, 0,
Daniel Vetter920afa72010-09-16 17:54:23 +02002608 dev_priv->mm.gtt_mappable_end,
2609 0);
2610 else
Chris Wilson05394f32010-11-08 19:18:58 +00002611 obj->gtt_space =
Chris Wilsona00b10c2010-09-24 21:15:47 +01002612 drm_mm_get_block(free_space, size, alignment);
Daniel Vetter920afa72010-09-16 17:54:23 +02002613 }
Chris Wilson05394f32010-11-08 19:18:58 +00002614 if (obj->gtt_space == NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -07002615 /* If the gtt is empty and we're still having trouble
2616 * fitting our object in, we're out of memory.
2617 */
Daniel Vetter75e9e912010-11-04 17:11:09 +01002618 ret = i915_gem_evict_something(dev, size, alignment,
2619 map_and_fenceable);
Chris Wilson97311292009-09-21 00:22:34 +01002620 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002621 return ret;
Chris Wilson97311292009-09-21 00:22:34 +01002622
Eric Anholt673a3942008-07-30 12:06:12 -07002623 goto search_free;
2624 }
2625
Chris Wilsone5281cc2010-10-28 13:45:36 +01002626 ret = i915_gem_object_get_pages_gtt(obj, gfpmask);
Eric Anholt673a3942008-07-30 12:06:12 -07002627 if (ret) {
Chris Wilson05394f32010-11-08 19:18:58 +00002628 drm_mm_put_block(obj->gtt_space);
2629 obj->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002630
2631 if (ret == -ENOMEM) {
Chris Wilson809b6332011-01-10 17:33:15 +00002632 /* first try to reclaim some memory by clearing the GTT */
2633 ret = i915_gem_evict_everything(dev, false);
Chris Wilson07f73f62009-09-14 16:50:30 +01002634 if (ret) {
Chris Wilson07f73f62009-09-14 16:50:30 +01002635 /* now try to shrink everyone else */
Chris Wilson4bdadb92010-01-27 13:36:32 +00002636 if (gfpmask) {
2637 gfpmask = 0;
2638 goto search_free;
Chris Wilson07f73f62009-09-14 16:50:30 +01002639 }
2640
Chris Wilson809b6332011-01-10 17:33:15 +00002641 return -ENOMEM;
Chris Wilson07f73f62009-09-14 16:50:30 +01002642 }
2643
2644 goto search_free;
2645 }
2646
Eric Anholt673a3942008-07-30 12:06:12 -07002647 return ret;
2648 }
2649
Daniel Vetter74163902012-02-15 23:50:21 +01002650 ret = i915_gem_gtt_prepare_object(obj);
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002651 if (ret) {
Chris Wilsone5281cc2010-10-28 13:45:36 +01002652 i915_gem_object_put_pages_gtt(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00002653 drm_mm_put_block(obj->gtt_space);
2654 obj->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002655
Chris Wilson809b6332011-01-10 17:33:15 +00002656 if (i915_gem_evict_everything(dev, false))
Chris Wilson07f73f62009-09-14 16:50:30 +01002657 return ret;
Chris Wilson07f73f62009-09-14 16:50:30 +01002658
2659 goto search_free;
Eric Anholt673a3942008-07-30 12:06:12 -07002660 }
Daniel Vetter0ebb9822012-02-15 23:50:24 +01002661
2662 if (!dev_priv->mm.aliasing_ppgtt)
2663 i915_gem_gtt_bind_object(obj, obj->cache_level);
Eric Anholt673a3942008-07-30 12:06:12 -07002664
Chris Wilson6299f992010-11-24 12:23:44 +00002665 list_add_tail(&obj->gtt_list, &dev_priv->mm.gtt_list);
Chris Wilson05394f32010-11-08 19:18:58 +00002666 list_add_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
Chris Wilsonbf1a1092010-08-07 11:01:20 +01002667
Eric Anholt673a3942008-07-30 12:06:12 -07002668 /* Assert that the object is not currently in any GPU domain. As it
2669 * wasn't in the GTT, there shouldn't be any way it could have been in
2670 * a GPU cache
2671 */
Chris Wilson05394f32010-11-08 19:18:58 +00002672 BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
2673 BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
Eric Anholt673a3942008-07-30 12:06:12 -07002674
Chris Wilson6299f992010-11-24 12:23:44 +00002675 obj->gtt_offset = obj->gtt_space->start;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002676
Daniel Vetter75e9e912010-11-04 17:11:09 +01002677 fenceable =
Chris Wilson05394f32010-11-08 19:18:58 +00002678 obj->gtt_space->size == fence_size &&
Akshay Joshi0206e352011-08-16 15:34:10 -04002679 (obj->gtt_space->start & (fence_alignment - 1)) == 0;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002680
Daniel Vetter75e9e912010-11-04 17:11:09 +01002681 mappable =
Chris Wilson05394f32010-11-08 19:18:58 +00002682 obj->gtt_offset + obj->base.size <= dev_priv->mm.gtt_mappable_end;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002683
Chris Wilson05394f32010-11-08 19:18:58 +00002684 obj->map_and_fenceable = mappable && fenceable;
Daniel Vetter75e9e912010-11-04 17:11:09 +01002685
Chris Wilsondb53a302011-02-03 11:57:46 +00002686 trace_i915_gem_object_bind(obj, map_and_fenceable);
Eric Anholt673a3942008-07-30 12:06:12 -07002687 return 0;
2688}
2689
2690void
Chris Wilson05394f32010-11-08 19:18:58 +00002691i915_gem_clflush_object(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002692{
Eric Anholt673a3942008-07-30 12:06:12 -07002693 /* If we don't have a page list set up, then we're not pinned
2694 * to GPU, and we can ignore the cache flush because it'll happen
2695 * again at bind time.
2696 */
Chris Wilson05394f32010-11-08 19:18:58 +00002697 if (obj->pages == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002698 return;
2699
Chris Wilson9c23f7f2011-03-29 16:59:52 -07002700 /* If the GPU is snooping the contents of the CPU cache,
2701 * we do not need to manually clear the CPU cache lines. However,
2702 * the caches are only snooped when the render cache is
2703 * flushed/invalidated. As we always have to emit invalidations
2704 * and flushes when moving into and out of the RENDER domain, correct
2705 * snooping behaviour occurs naturally as the result of our domain
2706 * tracking.
2707 */
2708 if (obj->cache_level != I915_CACHE_NONE)
2709 return;
2710
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002711 trace_i915_gem_object_clflush(obj);
Eric Anholtcfa16a02009-05-26 18:46:16 -07002712
Chris Wilson05394f32010-11-08 19:18:58 +00002713 drm_clflush_pages(obj->pages, obj->base.size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002714}
2715
Eric Anholte47c68e2008-11-14 13:35:19 -08002716/** Flushes any GPU write domain for the object if it's dirty. */
Chris Wilson88241782011-01-07 17:09:48 +00002717static int
Chris Wilson3619df02010-11-28 15:37:17 +00002718i915_gem_object_flush_gpu_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08002719{
Chris Wilson05394f32010-11-08 19:18:58 +00002720 if ((obj->base.write_domain & I915_GEM_GPU_DOMAINS) == 0)
Chris Wilson88241782011-01-07 17:09:48 +00002721 return 0;
Eric Anholte47c68e2008-11-14 13:35:19 -08002722
2723 /* Queue the GPU write cache flushing we need. */
Chris Wilsondb53a302011-02-03 11:57:46 +00002724 return i915_gem_flush_ring(obj->ring, 0, obj->base.write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002725}
2726
2727/** Flushes the GTT write domain for the object if it's dirty. */
2728static void
Chris Wilson05394f32010-11-08 19:18:58 +00002729i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08002730{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002731 uint32_t old_write_domain;
2732
Chris Wilson05394f32010-11-08 19:18:58 +00002733 if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
Eric Anholte47c68e2008-11-14 13:35:19 -08002734 return;
2735
Chris Wilson63256ec2011-01-04 18:42:07 +00002736 /* No actual flushing is required for the GTT write domain. Writes
Eric Anholte47c68e2008-11-14 13:35:19 -08002737 * to it immediately go to main memory as far as we know, so there's
2738 * no chipset flush. It also doesn't land in render cache.
Chris Wilson63256ec2011-01-04 18:42:07 +00002739 *
2740 * However, we do have to enforce the order so that all writes through
2741 * the GTT land before any writes to the device, such as updates to
2742 * the GATT itself.
Eric Anholte47c68e2008-11-14 13:35:19 -08002743 */
Chris Wilson63256ec2011-01-04 18:42:07 +00002744 wmb();
2745
Chris Wilson05394f32010-11-08 19:18:58 +00002746 old_write_domain = obj->base.write_domain;
2747 obj->base.write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002748
2749 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00002750 obj->base.read_domains,
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002751 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002752}
2753
2754/** Flushes the CPU write domain for the object if it's dirty. */
2755static void
Chris Wilson05394f32010-11-08 19:18:58 +00002756i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08002757{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002758 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002759
Chris Wilson05394f32010-11-08 19:18:58 +00002760 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
Eric Anholte47c68e2008-11-14 13:35:19 -08002761 return;
2762
2763 i915_gem_clflush_object(obj);
Daniel Vetter40ce6572010-11-05 18:12:18 +01002764 intel_gtt_chipset_flush();
Chris Wilson05394f32010-11-08 19:18:58 +00002765 old_write_domain = obj->base.write_domain;
2766 obj->base.write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002767
2768 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00002769 obj->base.read_domains,
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002770 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002771}
2772
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002773/**
2774 * Moves a single object to the GTT read, and possibly write domain.
2775 *
2776 * This function returns when the move is complete, including waiting on
2777 * flushes to occur.
2778 */
Jesse Barnes79e53942008-11-07 14:24:08 -08002779int
Chris Wilson20217462010-11-23 15:26:33 +00002780i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002781{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002782 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002783 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002784
Eric Anholt02354392008-11-26 13:58:13 -08002785 /* Not valid to be called on unbound objects. */
Chris Wilson05394f32010-11-08 19:18:58 +00002786 if (obj->gtt_space == NULL)
Eric Anholt02354392008-11-26 13:58:13 -08002787 return -EINVAL;
2788
Chris Wilson8d7e3de2011-02-07 15:23:02 +00002789 if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
2790 return 0;
2791
Chris Wilson88241782011-01-07 17:09:48 +00002792 ret = i915_gem_object_flush_gpu_write_domain(obj);
2793 if (ret)
2794 return ret;
2795
Chris Wilson87ca9c82010-12-02 09:42:56 +00002796 if (obj->pending_gpu_write || write) {
Chris Wilsonce453d82011-02-21 14:43:56 +00002797 ret = i915_gem_object_wait_rendering(obj);
Chris Wilson87ca9c82010-12-02 09:42:56 +00002798 if (ret)
2799 return ret;
2800 }
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002801
Chris Wilson72133422010-09-13 23:56:38 +01002802 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002803
Chris Wilson05394f32010-11-08 19:18:58 +00002804 old_write_domain = obj->base.write_domain;
2805 old_read_domains = obj->base.read_domains;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002806
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002807 /* It should now be out of any other write domains, and we can update
2808 * the domain values for our changes.
2809 */
Chris Wilson05394f32010-11-08 19:18:58 +00002810 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2811 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002812 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00002813 obj->base.read_domains = I915_GEM_DOMAIN_GTT;
2814 obj->base.write_domain = I915_GEM_DOMAIN_GTT;
2815 obj->dirty = 1;
Eric Anholte47c68e2008-11-14 13:35:19 -08002816 }
2817
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002818 trace_i915_gem_object_change_domain(obj,
2819 old_read_domains,
2820 old_write_domain);
2821
Eric Anholte47c68e2008-11-14 13:35:19 -08002822 return 0;
2823}
2824
Chris Wilsone4ffd172011-04-04 09:44:39 +01002825int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
2826 enum i915_cache_level cache_level)
2827{
Daniel Vetter7bddb012012-02-09 17:15:47 +01002828 struct drm_device *dev = obj->base.dev;
2829 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsone4ffd172011-04-04 09:44:39 +01002830 int ret;
2831
2832 if (obj->cache_level == cache_level)
2833 return 0;
2834
2835 if (obj->pin_count) {
2836 DRM_DEBUG("can not change the cache level of pinned objects\n");
2837 return -EBUSY;
2838 }
2839
2840 if (obj->gtt_space) {
2841 ret = i915_gem_object_finish_gpu(obj);
2842 if (ret)
2843 return ret;
2844
2845 i915_gem_object_finish_gtt(obj);
2846
2847 /* Before SandyBridge, you could not use tiling or fence
2848 * registers with snooped memory, so relinquish any fences
2849 * currently pointing to our region in the aperture.
2850 */
2851 if (INTEL_INFO(obj->base.dev)->gen < 6) {
2852 ret = i915_gem_object_put_fence(obj);
2853 if (ret)
2854 return ret;
2855 }
2856
Daniel Vetter74898d72012-02-15 23:50:22 +01002857 if (obj->has_global_gtt_mapping)
2858 i915_gem_gtt_bind_object(obj, cache_level);
Daniel Vetter7bddb012012-02-09 17:15:47 +01002859 if (obj->has_aliasing_ppgtt_mapping)
2860 i915_ppgtt_bind_object(dev_priv->mm.aliasing_ppgtt,
2861 obj, cache_level);
Chris Wilsone4ffd172011-04-04 09:44:39 +01002862 }
2863
2864 if (cache_level == I915_CACHE_NONE) {
2865 u32 old_read_domains, old_write_domain;
2866
2867 /* If we're coming from LLC cached, then we haven't
2868 * actually been tracking whether the data is in the
2869 * CPU cache or not, since we only allow one bit set
2870 * in obj->write_domain and have been skipping the clflushes.
2871 * Just set it to the CPU cache for now.
2872 */
2873 WARN_ON(obj->base.write_domain & ~I915_GEM_DOMAIN_CPU);
2874 WARN_ON(obj->base.read_domains & ~I915_GEM_DOMAIN_CPU);
2875
2876 old_read_domains = obj->base.read_domains;
2877 old_write_domain = obj->base.write_domain;
2878
2879 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
2880 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
2881
2882 trace_i915_gem_object_change_domain(obj,
2883 old_read_domains,
2884 old_write_domain);
2885 }
2886
2887 obj->cache_level = cache_level;
2888 return 0;
2889}
2890
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002891/*
Chris Wilson2da3b9b2011-04-14 09:41:17 +01002892 * Prepare buffer for display plane (scanout, cursors, etc).
2893 * Can be called from an uninterruptible phase (modesetting) and allows
2894 * any flushes to be pipelined (for pageflips).
2895 *
2896 * For the display plane, we want to be in the GTT but out of any write
2897 * domains. So in many ways this looks like set_to_gtt_domain() apart from the
2898 * ability to pipeline the waits, pinning and any additional subtleties
2899 * that may differentiate the display plane from ordinary buffers.
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002900 */
2901int
Chris Wilson2da3b9b2011-04-14 09:41:17 +01002902i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
2903 u32 alignment,
Chris Wilson919926a2010-11-12 13:42:53 +00002904 struct intel_ring_buffer *pipelined)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002905{
Chris Wilson2da3b9b2011-04-14 09:41:17 +01002906 u32 old_read_domains, old_write_domain;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002907 int ret;
2908
Chris Wilson88241782011-01-07 17:09:48 +00002909 ret = i915_gem_object_flush_gpu_write_domain(obj);
2910 if (ret)
2911 return ret;
2912
Chris Wilson0be73282010-12-06 14:36:27 +00002913 if (pipelined != obj->ring) {
Chris Wilsonce453d82011-02-21 14:43:56 +00002914 ret = i915_gem_object_wait_rendering(obj);
Keith Packardf0b69ef2011-07-19 16:21:40 -07002915 if (ret == -ERESTARTSYS)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002916 return ret;
2917 }
2918
Eric Anholta7ef0642011-03-29 16:59:54 -07002919 /* The display engine is not coherent with the LLC cache on gen6. As
2920 * a result, we make sure that the pinning that is about to occur is
2921 * done with uncached PTEs. This is lowest common denominator for all
2922 * chipsets.
2923 *
2924 * However for gen6+, we could do better by using the GFDT bit instead
2925 * of uncaching, which would allow us to flush all the LLC-cached data
2926 * with that bit in the PTE to main memory with just one PIPE_CONTROL.
2927 */
2928 ret = i915_gem_object_set_cache_level(obj, I915_CACHE_NONE);
2929 if (ret)
2930 return ret;
2931
Chris Wilson2da3b9b2011-04-14 09:41:17 +01002932 /* As the user may map the buffer once pinned in the display plane
2933 * (e.g. libkms for the bootup splash), we have to ensure that we
2934 * always use map_and_fenceable for all scanout buffers.
2935 */
2936 ret = i915_gem_object_pin(obj, alignment, true);
2937 if (ret)
2938 return ret;
2939
Chris Wilsonb118c1e2010-05-27 13:18:14 +01002940 i915_gem_object_flush_cpu_write_domain(obj);
2941
Chris Wilson2da3b9b2011-04-14 09:41:17 +01002942 old_write_domain = obj->base.write_domain;
Chris Wilson05394f32010-11-08 19:18:58 +00002943 old_read_domains = obj->base.read_domains;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01002944
2945 /* It should now be out of any other write domains, and we can update
2946 * the domain values for our changes.
2947 */
2948 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
Chris Wilson05394f32010-11-08 19:18:58 +00002949 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002950
2951 trace_i915_gem_object_change_domain(obj,
2952 old_read_domains,
Chris Wilson2da3b9b2011-04-14 09:41:17 +01002953 old_write_domain);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002954
2955 return 0;
2956}
2957
Chris Wilson85345512010-11-13 09:49:11 +00002958int
Chris Wilsona8198ee2011-04-13 22:04:09 +01002959i915_gem_object_finish_gpu(struct drm_i915_gem_object *obj)
Chris Wilson85345512010-11-13 09:49:11 +00002960{
Chris Wilson88241782011-01-07 17:09:48 +00002961 int ret;
2962
Chris Wilsona8198ee2011-04-13 22:04:09 +01002963 if ((obj->base.read_domains & I915_GEM_GPU_DOMAINS) == 0)
Chris Wilson85345512010-11-13 09:49:11 +00002964 return 0;
2965
Chris Wilson88241782011-01-07 17:09:48 +00002966 if (obj->base.write_domain & I915_GEM_GPU_DOMAINS) {
Chris Wilsondb53a302011-02-03 11:57:46 +00002967 ret = i915_gem_flush_ring(obj->ring, 0, obj->base.write_domain);
Chris Wilson88241782011-01-07 17:09:48 +00002968 if (ret)
2969 return ret;
2970 }
Chris Wilson85345512010-11-13 09:49:11 +00002971
Chris Wilsonc501ae72011-12-14 13:57:23 +01002972 ret = i915_gem_object_wait_rendering(obj);
2973 if (ret)
2974 return ret;
2975
Chris Wilsona8198ee2011-04-13 22:04:09 +01002976 /* Ensure that we invalidate the GPU's caches and TLBs. */
2977 obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
Chris Wilsonc501ae72011-12-14 13:57:23 +01002978 return 0;
Chris Wilson85345512010-11-13 09:49:11 +00002979}
2980
Eric Anholte47c68e2008-11-14 13:35:19 -08002981/**
2982 * Moves a single object to the CPU read, and possibly write domain.
2983 *
2984 * This function returns when the move is complete, including waiting on
2985 * flushes to occur.
2986 */
Chris Wilsondabdfe02012-03-26 10:10:27 +02002987int
Chris Wilson919926a2010-11-12 13:42:53 +00002988i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholte47c68e2008-11-14 13:35:19 -08002989{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002990 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002991 int ret;
2992
Chris Wilson8d7e3de2011-02-07 15:23:02 +00002993 if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
2994 return 0;
2995
Chris Wilson88241782011-01-07 17:09:48 +00002996 ret = i915_gem_object_flush_gpu_write_domain(obj);
2997 if (ret)
2998 return ret;
2999
Chris Wilsonce453d82011-02-21 14:43:56 +00003000 ret = i915_gem_object_wait_rendering(obj);
Daniel Vetterde18a292010-11-27 22:30:41 +01003001 if (ret)
Eric Anholte47c68e2008-11-14 13:35:19 -08003002 return ret;
3003
3004 i915_gem_object_flush_gtt_write_domain(obj);
3005
Chris Wilson05394f32010-11-08 19:18:58 +00003006 old_write_domain = obj->base.write_domain;
3007 old_read_domains = obj->base.read_domains;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003008
Eric Anholte47c68e2008-11-14 13:35:19 -08003009 /* Flush the CPU cache if it's still invalid. */
Chris Wilson05394f32010-11-08 19:18:58 +00003010 if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
Eric Anholte47c68e2008-11-14 13:35:19 -08003011 i915_gem_clflush_object(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003012
Chris Wilson05394f32010-11-08 19:18:58 +00003013 obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003014 }
3015
3016 /* It should now be out of any other write domains, and we can update
3017 * the domain values for our changes.
3018 */
Chris Wilson05394f32010-11-08 19:18:58 +00003019 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
Eric Anholte47c68e2008-11-14 13:35:19 -08003020
3021 /* If we're writing through the CPU, then the GPU read domains will
3022 * need to be invalidated at next use.
3023 */
3024 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00003025 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3026 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003027 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003028
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003029 trace_i915_gem_object_change_domain(obj,
3030 old_read_domains,
3031 old_write_domain);
3032
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003033 return 0;
3034}
3035
Eric Anholt673a3942008-07-30 12:06:12 -07003036/* Throttle our rendering by waiting until the ring has completed our requests
3037 * emitted over 20 msec ago.
3038 *
Eric Anholtb9624422009-06-03 07:27:35 +00003039 * Note that if we were to use the current jiffies each time around the loop,
3040 * we wouldn't escape the function with any frames outstanding if the time to
3041 * render a frame was over 20ms.
3042 *
Eric Anholt673a3942008-07-30 12:06:12 -07003043 * This should get us reasonable parallelism between CPU and GPU but also
3044 * relatively low latency when blocking on a particular request to finish.
3045 */
3046static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003047i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003048{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003049 struct drm_i915_private *dev_priv = dev->dev_private;
3050 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00003051 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003052 struct drm_i915_gem_request *request;
3053 struct intel_ring_buffer *ring = NULL;
3054 u32 seqno = 0;
3055 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003056
Chris Wilsone110e8d2011-01-26 15:39:14 +00003057 if (atomic_read(&dev_priv->mm.wedged))
3058 return -EIO;
3059
Chris Wilson1c255952010-09-26 11:03:27 +01003060 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003061 list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
Eric Anholtb9624422009-06-03 07:27:35 +00003062 if (time_after_eq(request->emitted_jiffies, recent_enough))
3063 break;
3064
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003065 ring = request->ring;
3066 seqno = request->seqno;
Eric Anholtb9624422009-06-03 07:27:35 +00003067 }
Chris Wilson1c255952010-09-26 11:03:27 +01003068 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003069
3070 if (seqno == 0)
3071 return 0;
3072
3073 ret = 0;
Chris Wilson78501ea2010-10-27 12:18:21 +01003074 if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) {
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003075 /* And wait for the seqno passing without holding any locks and
3076 * causing extra latency for others. This is safe as the irq
3077 * generation is designed to be run atomically and so is
3078 * lockless.
3079 */
Chris Wilsonb13c2b92010-12-13 16:54:50 +00003080 if (ring->irq_get(ring)) {
3081 ret = wait_event_interruptible(ring->irq_queue,
3082 i915_seqno_passed(ring->get_seqno(ring), seqno)
3083 || atomic_read(&dev_priv->mm.wedged));
3084 ring->irq_put(ring);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003085
Chris Wilsonb13c2b92010-12-13 16:54:50 +00003086 if (ret == 0 && atomic_read(&dev_priv->mm.wedged))
3087 ret = -EIO;
Eric Anholte959b5d2011-12-22 14:55:01 -08003088 } else if (wait_for_atomic(i915_seqno_passed(ring->get_seqno(ring),
3089 seqno) ||
Eric Anholt7ea29b12011-12-22 14:54:59 -08003090 atomic_read(&dev_priv->mm.wedged), 3000)) {
3091 ret = -EBUSY;
Chris Wilsonb13c2b92010-12-13 16:54:50 +00003092 }
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003093 }
3094
3095 if (ret == 0)
3096 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
Eric Anholtb9624422009-06-03 07:27:35 +00003097
Eric Anholt673a3942008-07-30 12:06:12 -07003098 return ret;
3099}
3100
Eric Anholt673a3942008-07-30 12:06:12 -07003101int
Chris Wilson05394f32010-11-08 19:18:58 +00003102i915_gem_object_pin(struct drm_i915_gem_object *obj,
3103 uint32_t alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01003104 bool map_and_fenceable)
Eric Anholt673a3942008-07-30 12:06:12 -07003105{
Chris Wilson05394f32010-11-08 19:18:58 +00003106 struct drm_device *dev = obj->base.dev;
Chris Wilsonf13d3f72010-09-20 17:36:15 +01003107 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003108 int ret;
3109
Chris Wilson05394f32010-11-08 19:18:58 +00003110 BUG_ON(obj->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT);
Chris Wilson23bc5982010-09-29 16:10:57 +01003111 WARN_ON(i915_verify_lists(dev));
Chris Wilsonac0c6b52010-05-27 13:18:18 +01003112
Chris Wilson05394f32010-11-08 19:18:58 +00003113 if (obj->gtt_space != NULL) {
3114 if ((alignment && obj->gtt_offset & (alignment - 1)) ||
3115 (map_and_fenceable && !obj->map_and_fenceable)) {
3116 WARN(obj->pin_count,
Chris Wilsonae7d49d2010-08-04 12:37:41 +01003117 "bo is already pinned with incorrect alignment:"
Daniel Vetter75e9e912010-11-04 17:11:09 +01003118 " offset=%x, req.alignment=%x, req.map_and_fenceable=%d,"
3119 " obj->map_and_fenceable=%d\n",
Chris Wilson05394f32010-11-08 19:18:58 +00003120 obj->gtt_offset, alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01003121 map_and_fenceable,
Chris Wilson05394f32010-11-08 19:18:58 +00003122 obj->map_and_fenceable);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01003123 ret = i915_gem_object_unbind(obj);
3124 if (ret)
3125 return ret;
3126 }
3127 }
3128
Chris Wilson05394f32010-11-08 19:18:58 +00003129 if (obj->gtt_space == NULL) {
Chris Wilsona00b10c2010-09-24 21:15:47 +01003130 ret = i915_gem_object_bind_to_gtt(obj, alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01003131 map_and_fenceable);
Chris Wilson97311292009-09-21 00:22:34 +01003132 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07003133 return ret;
Chris Wilson22c344e2009-02-11 14:26:45 +00003134 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05003135
Daniel Vetter74898d72012-02-15 23:50:22 +01003136 if (!obj->has_global_gtt_mapping && map_and_fenceable)
3137 i915_gem_gtt_bind_object(obj, obj->cache_level);
3138
Chris Wilson05394f32010-11-08 19:18:58 +00003139 if (obj->pin_count++ == 0) {
Chris Wilson05394f32010-11-08 19:18:58 +00003140 if (!obj->active)
3141 list_move_tail(&obj->mm_list,
Chris Wilsonf13d3f72010-09-20 17:36:15 +01003142 &dev_priv->mm.pinned_list);
Eric Anholt673a3942008-07-30 12:06:12 -07003143 }
Chris Wilson6299f992010-11-24 12:23:44 +00003144 obj->pin_mappable |= map_and_fenceable;
Eric Anholt673a3942008-07-30 12:06:12 -07003145
Chris Wilson23bc5982010-09-29 16:10:57 +01003146 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07003147 return 0;
3148}
3149
3150void
Chris Wilson05394f32010-11-08 19:18:58 +00003151i915_gem_object_unpin(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07003152{
Chris Wilson05394f32010-11-08 19:18:58 +00003153 struct drm_device *dev = obj->base.dev;
Eric Anholt673a3942008-07-30 12:06:12 -07003154 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003155
Chris Wilson23bc5982010-09-29 16:10:57 +01003156 WARN_ON(i915_verify_lists(dev));
Chris Wilson05394f32010-11-08 19:18:58 +00003157 BUG_ON(obj->pin_count == 0);
3158 BUG_ON(obj->gtt_space == NULL);
Eric Anholt673a3942008-07-30 12:06:12 -07003159
Chris Wilson05394f32010-11-08 19:18:58 +00003160 if (--obj->pin_count == 0) {
3161 if (!obj->active)
3162 list_move_tail(&obj->mm_list,
Eric Anholt673a3942008-07-30 12:06:12 -07003163 &dev_priv->mm.inactive_list);
Chris Wilson6299f992010-11-24 12:23:44 +00003164 obj->pin_mappable = false;
Eric Anholt673a3942008-07-30 12:06:12 -07003165 }
Chris Wilson23bc5982010-09-29 16:10:57 +01003166 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07003167}
3168
3169int
3170i915_gem_pin_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00003171 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003172{
3173 struct drm_i915_gem_pin *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003174 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07003175 int ret;
3176
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003177 ret = i915_mutex_lock_interruptible(dev);
3178 if (ret)
3179 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003180
Chris Wilson05394f32010-11-08 19:18:58 +00003181 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00003182 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003183 ret = -ENOENT;
3184 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07003185 }
Eric Anholt673a3942008-07-30 12:06:12 -07003186
Chris Wilson05394f32010-11-08 19:18:58 +00003187 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilsonbb6baf72009-09-22 14:24:13 +01003188 DRM_ERROR("Attempting to pin a purgeable buffer\n");
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003189 ret = -EINVAL;
3190 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003191 }
3192
Chris Wilson05394f32010-11-08 19:18:58 +00003193 if (obj->pin_filp != NULL && obj->pin_filp != file) {
Jesse Barnes79e53942008-11-07 14:24:08 -08003194 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
3195 args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003196 ret = -EINVAL;
3197 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08003198 }
3199
Chris Wilson05394f32010-11-08 19:18:58 +00003200 obj->user_pin_count++;
3201 obj->pin_filp = file;
3202 if (obj->user_pin_count == 1) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01003203 ret = i915_gem_object_pin(obj, args->alignment, true);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003204 if (ret)
3205 goto out;
Eric Anholt673a3942008-07-30 12:06:12 -07003206 }
3207
3208 /* XXX - flush the CPU caches for pinned objects
3209 * as the X server doesn't manage domains yet
3210 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003211 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00003212 args->offset = obj->gtt_offset;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003213out:
Chris Wilson05394f32010-11-08 19:18:58 +00003214 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003215unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07003216 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003217 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003218}
3219
3220int
3221i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00003222 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003223{
3224 struct drm_i915_gem_pin *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003225 struct drm_i915_gem_object *obj;
Chris Wilson76c1dec2010-09-25 11:22:51 +01003226 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003227
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003228 ret = i915_mutex_lock_interruptible(dev);
3229 if (ret)
3230 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003231
Chris Wilson05394f32010-11-08 19:18:58 +00003232 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00003233 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003234 ret = -ENOENT;
3235 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07003236 }
Chris Wilson76c1dec2010-09-25 11:22:51 +01003237
Chris Wilson05394f32010-11-08 19:18:58 +00003238 if (obj->pin_filp != file) {
Jesse Barnes79e53942008-11-07 14:24:08 -08003239 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
3240 args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003241 ret = -EINVAL;
3242 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08003243 }
Chris Wilson05394f32010-11-08 19:18:58 +00003244 obj->user_pin_count--;
3245 if (obj->user_pin_count == 0) {
3246 obj->pin_filp = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08003247 i915_gem_object_unpin(obj);
3248 }
Eric Anholt673a3942008-07-30 12:06:12 -07003249
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003250out:
Chris Wilson05394f32010-11-08 19:18:58 +00003251 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003252unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07003253 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003254 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003255}
3256
3257int
3258i915_gem_busy_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00003259 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003260{
3261 struct drm_i915_gem_busy *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003262 struct drm_i915_gem_object *obj;
Chris Wilson30dbf0c2010-09-25 10:19:17 +01003263 int ret;
3264
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003265 ret = i915_mutex_lock_interruptible(dev);
3266 if (ret)
3267 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003268
Chris Wilson05394f32010-11-08 19:18:58 +00003269 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00003270 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003271 ret = -ENOENT;
3272 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07003273 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08003274
Chris Wilson0be555b2010-08-04 15:36:30 +01003275 /* Count all active objects as busy, even if they are currently not used
3276 * by the gpu. Users of this interface expect objects to eventually
3277 * become non-busy without any further actions, therefore emit any
3278 * necessary flushes here.
Eric Anholtc4de0a52008-12-14 19:05:04 -08003279 */
Chris Wilson05394f32010-11-08 19:18:58 +00003280 args->busy = obj->active;
Chris Wilson0be555b2010-08-04 15:36:30 +01003281 if (args->busy) {
3282 /* Unconditionally flush objects, even when the gpu still uses this
3283 * object. Userspace calling this function indicates that it wants to
3284 * use this buffer rather sooner than later, so issuing the required
3285 * flush earlier is beneficial.
3286 */
Chris Wilson1a1c6972010-12-07 23:00:20 +00003287 if (obj->base.write_domain & I915_GEM_GPU_DOMAINS) {
Chris Wilsondb53a302011-02-03 11:57:46 +00003288 ret = i915_gem_flush_ring(obj->ring,
Chris Wilson88241782011-01-07 17:09:48 +00003289 0, obj->base.write_domain);
Chris Wilson1a1c6972010-12-07 23:00:20 +00003290 } else if (obj->ring->outstanding_lazy_request ==
3291 obj->last_rendering_seqno) {
3292 struct drm_i915_gem_request *request;
3293
Chris Wilson7a194872010-12-07 10:38:40 +00003294 /* This ring is not being cleared by active usage,
3295 * so emit a request to do so.
3296 */
Chris Wilson1a1c6972010-12-07 23:00:20 +00003297 request = kzalloc(sizeof(*request), GFP_KERNEL);
Rakib Mullick457eafc2011-11-16 00:49:28 +06003298 if (request) {
Akshay Joshi0206e352011-08-16 15:34:10 -04003299 ret = i915_add_request(obj->ring, NULL, request);
Rakib Mullick457eafc2011-11-16 00:49:28 +06003300 if (ret)
3301 kfree(request);
3302 } else
Chris Wilson7a194872010-12-07 10:38:40 +00003303 ret = -ENOMEM;
3304 }
Chris Wilson0be555b2010-08-04 15:36:30 +01003305
3306 /* Update the active list for the hardware's current position.
3307 * Otherwise this only updates on a delayed timer or when irqs
3308 * are actually unmasked, and our working set ends up being
3309 * larger than required.
3310 */
Chris Wilsondb53a302011-02-03 11:57:46 +00003311 i915_gem_retire_requests_ring(obj->ring);
Chris Wilson0be555b2010-08-04 15:36:30 +01003312
Chris Wilson05394f32010-11-08 19:18:58 +00003313 args->busy = obj->active;
Chris Wilson0be555b2010-08-04 15:36:30 +01003314 }
Eric Anholt673a3942008-07-30 12:06:12 -07003315
Chris Wilson05394f32010-11-08 19:18:58 +00003316 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003317unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07003318 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003319 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003320}
3321
3322int
3323i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
3324 struct drm_file *file_priv)
3325{
Akshay Joshi0206e352011-08-16 15:34:10 -04003326 return i915_gem_ring_throttle(dev, file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07003327}
3328
Chris Wilson3ef94da2009-09-14 16:50:29 +01003329int
3330i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
3331 struct drm_file *file_priv)
3332{
3333 struct drm_i915_gem_madvise *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003334 struct drm_i915_gem_object *obj;
Chris Wilson76c1dec2010-09-25 11:22:51 +01003335 int ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003336
3337 switch (args->madv) {
3338 case I915_MADV_DONTNEED:
3339 case I915_MADV_WILLNEED:
3340 break;
3341 default:
3342 return -EINVAL;
3343 }
3344
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003345 ret = i915_mutex_lock_interruptible(dev);
3346 if (ret)
3347 return ret;
3348
Chris Wilson05394f32010-11-08 19:18:58 +00003349 obj = to_intel_bo(drm_gem_object_lookup(dev, file_priv, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00003350 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003351 ret = -ENOENT;
3352 goto unlock;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003353 }
Chris Wilson3ef94da2009-09-14 16:50:29 +01003354
Chris Wilson05394f32010-11-08 19:18:58 +00003355 if (obj->pin_count) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003356 ret = -EINVAL;
3357 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003358 }
3359
Chris Wilson05394f32010-11-08 19:18:58 +00003360 if (obj->madv != __I915_MADV_PURGED)
3361 obj->madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003362
Chris Wilson2d7ef392009-09-20 23:13:10 +01003363 /* if the object is no longer bound, discard its backing storage */
Chris Wilson05394f32010-11-08 19:18:58 +00003364 if (i915_gem_object_is_purgeable(obj) &&
3365 obj->gtt_space == NULL)
Chris Wilson2d7ef392009-09-20 23:13:10 +01003366 i915_gem_object_truncate(obj);
3367
Chris Wilson05394f32010-11-08 19:18:58 +00003368 args->retained = obj->madv != __I915_MADV_PURGED;
Chris Wilsonbb6baf72009-09-22 14:24:13 +01003369
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003370out:
Chris Wilson05394f32010-11-08 19:18:58 +00003371 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003372unlock:
Chris Wilson3ef94da2009-09-14 16:50:29 +01003373 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003374 return ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003375}
3376
Chris Wilson05394f32010-11-08 19:18:58 +00003377struct drm_i915_gem_object *i915_gem_alloc_object(struct drm_device *dev,
3378 size_t size)
Daniel Vetterac52bc52010-04-09 19:05:06 +00003379{
Chris Wilson73aa8082010-09-30 11:46:12 +01003380 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc397b902010-04-09 19:05:07 +00003381 struct drm_i915_gem_object *obj;
Hugh Dickins5949eac2011-06-27 16:18:18 -07003382 struct address_space *mapping;
Daniel Vetterc397b902010-04-09 19:05:07 +00003383
3384 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
3385 if (obj == NULL)
3386 return NULL;
3387
3388 if (drm_gem_object_init(dev, &obj->base, size) != 0) {
3389 kfree(obj);
3390 return NULL;
3391 }
3392
Hugh Dickins5949eac2011-06-27 16:18:18 -07003393 mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
3394 mapping_set_gfp_mask(mapping, GFP_HIGHUSER | __GFP_RECLAIMABLE);
3395
Chris Wilson73aa8082010-09-30 11:46:12 +01003396 i915_gem_info_add_obj(dev_priv, size);
3397
Daniel Vetterc397b902010-04-09 19:05:07 +00003398 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
3399 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3400
Eugeni Dodonov3d29b842012-01-17 14:43:53 -02003401 if (HAS_LLC(dev)) {
3402 /* On some devices, we can have the GPU use the LLC (the CPU
Eric Anholta1871112011-03-29 16:59:55 -07003403 * cache) for about a 10% performance improvement
3404 * compared to uncached. Graphics requests other than
3405 * display scanout are coherent with the CPU in
3406 * accessing this cache. This means in this mode we
3407 * don't need to clflush on the CPU side, and on the
3408 * GPU side we only need to flush internal caches to
3409 * get data visible to the CPU.
3410 *
3411 * However, we maintain the display planes as UC, and so
3412 * need to rebind when first used as such.
3413 */
3414 obj->cache_level = I915_CACHE_LLC;
3415 } else
3416 obj->cache_level = I915_CACHE_NONE;
3417
Daniel Vetter62b8b212010-04-09 19:05:08 +00003418 obj->base.driver_private = NULL;
Daniel Vetterc397b902010-04-09 19:05:07 +00003419 obj->fence_reg = I915_FENCE_REG_NONE;
Chris Wilson69dc4982010-10-19 10:36:51 +01003420 INIT_LIST_HEAD(&obj->mm_list);
Daniel Vetter93a37f22010-11-05 20:24:53 +01003421 INIT_LIST_HEAD(&obj->gtt_list);
Chris Wilson69dc4982010-10-19 10:36:51 +01003422 INIT_LIST_HEAD(&obj->ring_list);
Chris Wilson432e58e2010-11-25 19:32:06 +00003423 INIT_LIST_HEAD(&obj->exec_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00003424 INIT_LIST_HEAD(&obj->gpu_write_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00003425 obj->madv = I915_MADV_WILLNEED;
Daniel Vetter75e9e912010-11-04 17:11:09 +01003426 /* Avoid an unnecessary call to unbind on the first bind. */
3427 obj->map_and_fenceable = true;
Daniel Vetterc397b902010-04-09 19:05:07 +00003428
Chris Wilson05394f32010-11-08 19:18:58 +00003429 return obj;
Daniel Vetterac52bc52010-04-09 19:05:06 +00003430}
3431
Eric Anholt673a3942008-07-30 12:06:12 -07003432int i915_gem_init_object(struct drm_gem_object *obj)
3433{
Daniel Vetterc397b902010-04-09 19:05:07 +00003434 BUG();
Jesse Barnesde151cf2008-11-12 10:03:55 -08003435
Eric Anholt673a3942008-07-30 12:06:12 -07003436 return 0;
3437}
3438
Chris Wilson05394f32010-11-08 19:18:58 +00003439static void i915_gem_free_object_tail(struct drm_i915_gem_object *obj)
Chris Wilsonbe726152010-07-23 23:18:50 +01003440{
Chris Wilson05394f32010-11-08 19:18:58 +00003441 struct drm_device *dev = obj->base.dev;
Chris Wilsonbe726152010-07-23 23:18:50 +01003442 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonbe726152010-07-23 23:18:50 +01003443 int ret;
3444
3445 ret = i915_gem_object_unbind(obj);
3446 if (ret == -ERESTARTSYS) {
Chris Wilson05394f32010-11-08 19:18:58 +00003447 list_move(&obj->mm_list,
Chris Wilsonbe726152010-07-23 23:18:50 +01003448 &dev_priv->mm.deferred_free_list);
3449 return;
3450 }
3451
Chris Wilson26e12f892011-03-20 11:20:19 +00003452 trace_i915_gem_object_destroy(obj);
3453
Chris Wilson05394f32010-11-08 19:18:58 +00003454 if (obj->base.map_list.map)
Rob Clarkb464e9a2011-08-10 08:09:08 -05003455 drm_gem_free_mmap_offset(&obj->base);
Chris Wilsonbe726152010-07-23 23:18:50 +01003456
Chris Wilson05394f32010-11-08 19:18:58 +00003457 drm_gem_object_release(&obj->base);
3458 i915_gem_info_remove_obj(dev_priv, obj->base.size);
Chris Wilsonbe726152010-07-23 23:18:50 +01003459
Chris Wilson05394f32010-11-08 19:18:58 +00003460 kfree(obj->bit_17);
3461 kfree(obj);
Chris Wilsonbe726152010-07-23 23:18:50 +01003462}
3463
Chris Wilson05394f32010-11-08 19:18:58 +00003464void i915_gem_free_object(struct drm_gem_object *gem_obj)
Eric Anholt673a3942008-07-30 12:06:12 -07003465{
Chris Wilson05394f32010-11-08 19:18:58 +00003466 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
3467 struct drm_device *dev = obj->base.dev;
Eric Anholt673a3942008-07-30 12:06:12 -07003468
Chris Wilson05394f32010-11-08 19:18:58 +00003469 while (obj->pin_count > 0)
Eric Anholt673a3942008-07-30 12:06:12 -07003470 i915_gem_object_unpin(obj);
3471
Chris Wilson05394f32010-11-08 19:18:58 +00003472 if (obj->phys_obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10003473 i915_gem_detach_phys_object(dev, obj);
3474
Chris Wilsonbe726152010-07-23 23:18:50 +01003475 i915_gem_free_object_tail(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003476}
3477
Jesse Barnes5669fca2009-02-17 15:13:31 -08003478int
Eric Anholt673a3942008-07-30 12:06:12 -07003479i915_gem_idle(struct drm_device *dev)
3480{
3481 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson29105cc2010-01-07 10:39:13 +00003482 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003483
Keith Packard6dbe2772008-10-14 21:41:13 -07003484 mutex_lock(&dev->struct_mutex);
3485
Chris Wilson87acb0a2010-10-19 10:13:00 +01003486 if (dev_priv->mm.suspended) {
Keith Packard6dbe2772008-10-14 21:41:13 -07003487 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07003488 return 0;
Keith Packard6dbe2772008-10-14 21:41:13 -07003489 }
Eric Anholt673a3942008-07-30 12:06:12 -07003490
Ben Widawskyb93f9cf2012-01-25 15:39:34 -08003491 ret = i915_gpu_idle(dev, true);
Keith Packard6dbe2772008-10-14 21:41:13 -07003492 if (ret) {
3493 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07003494 return ret;
Keith Packard6dbe2772008-10-14 21:41:13 -07003495 }
Eric Anholt673a3942008-07-30 12:06:12 -07003496
Chris Wilson29105cc2010-01-07 10:39:13 +00003497 /* Under UMS, be paranoid and evict. */
3498 if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
Chris Wilson5eac3ab2010-10-31 08:49:47 +00003499 ret = i915_gem_evict_inactive(dev, false);
Chris Wilson29105cc2010-01-07 10:39:13 +00003500 if (ret) {
3501 mutex_unlock(&dev->struct_mutex);
3502 return ret;
3503 }
3504 }
3505
Chris Wilson312817a2010-11-22 11:50:11 +00003506 i915_gem_reset_fences(dev);
3507
Chris Wilson29105cc2010-01-07 10:39:13 +00003508 /* Hack! Don't let anybody do execbuf while we don't control the chip.
3509 * We need to replace this with a semaphore, or something.
3510 * And not confound mm.suspended!
3511 */
3512 dev_priv->mm.suspended = 1;
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02003513 del_timer_sync(&dev_priv->hangcheck_timer);
Chris Wilson29105cc2010-01-07 10:39:13 +00003514
3515 i915_kernel_lost_context(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07003516 i915_gem_cleanup_ringbuffer(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00003517
Keith Packard6dbe2772008-10-14 21:41:13 -07003518 mutex_unlock(&dev->struct_mutex);
3519
Chris Wilson29105cc2010-01-07 10:39:13 +00003520 /* Cancel the retire work handler, which should be idle now. */
3521 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
3522
Eric Anholt673a3942008-07-30 12:06:12 -07003523 return 0;
3524}
3525
Daniel Vetterf691e2f2012-02-02 09:58:12 +01003526void i915_gem_init_swizzling(struct drm_device *dev)
3527{
3528 drm_i915_private_t *dev_priv = dev->dev_private;
3529
Daniel Vetter11782b02012-01-31 16:47:55 +01003530 if (INTEL_INFO(dev)->gen < 5 ||
Daniel Vetterf691e2f2012-02-02 09:58:12 +01003531 dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
3532 return;
3533
3534 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
3535 DISP_TILE_SURFACE_SWIZZLING);
3536
Daniel Vetter11782b02012-01-31 16:47:55 +01003537 if (IS_GEN5(dev))
3538 return;
3539
Daniel Vetterf691e2f2012-02-02 09:58:12 +01003540 I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
3541 if (IS_GEN6(dev))
3542 I915_WRITE(ARB_MODE, ARB_MODE_ENABLE(ARB_MODE_SWIZZLE_SNB));
3543 else
3544 I915_WRITE(ARB_MODE, ARB_MODE_ENABLE(ARB_MODE_SWIZZLE_IVB));
3545}
Daniel Vettere21af882012-02-09 20:53:27 +01003546
3547void i915_gem_init_ppgtt(struct drm_device *dev)
3548{
3549 drm_i915_private_t *dev_priv = dev->dev_private;
3550 uint32_t pd_offset;
3551 struct intel_ring_buffer *ring;
3552 int i;
3553
3554 if (!dev_priv->mm.aliasing_ppgtt)
3555 return;
3556
3557 pd_offset = dev_priv->mm.aliasing_ppgtt->pd_offset;
3558 pd_offset /= 64; /* in cachelines, */
3559 pd_offset <<= 16;
3560
3561 if (INTEL_INFO(dev)->gen == 6) {
3562 uint32_t ecochk = I915_READ(GAM_ECOCHK);
3563 I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT |
3564 ECOCHK_PPGTT_CACHE64B);
3565 I915_WRITE(GFX_MODE, GFX_MODE_ENABLE(GFX_PPGTT_ENABLE));
3566 } else if (INTEL_INFO(dev)->gen >= 7) {
3567 I915_WRITE(GAM_ECOCHK, ECOCHK_PPGTT_CACHE64B);
3568 /* GFX_MODE is per-ring on gen7+ */
3569 }
3570
3571 for (i = 0; i < I915_NUM_RINGS; i++) {
3572 ring = &dev_priv->ring[i];
3573
3574 if (INTEL_INFO(dev)->gen >= 7)
3575 I915_WRITE(RING_MODE_GEN7(ring),
3576 GFX_MODE_ENABLE(GFX_PPGTT_ENABLE));
3577
3578 I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
3579 I915_WRITE(RING_PP_DIR_BASE(ring), pd_offset);
3580 }
3581}
3582
Eric Anholt673a3942008-07-30 12:06:12 -07003583int
Daniel Vetterf691e2f2012-02-02 09:58:12 +01003584i915_gem_init_hw(struct drm_device *dev)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08003585{
3586 drm_i915_private_t *dev_priv = dev->dev_private;
3587 int ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01003588
Daniel Vetterf691e2f2012-02-02 09:58:12 +01003589 i915_gem_init_swizzling(dev);
3590
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08003591 ret = intel_init_render_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01003592 if (ret)
Chris Wilsonb6913e42010-11-12 10:46:37 +00003593 return ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01003594
3595 if (HAS_BSD(dev)) {
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08003596 ret = intel_init_bsd_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01003597 if (ret)
3598 goto cleanup_render_ring;
Zou Nan haid1b851f2010-05-21 09:08:57 +08003599 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01003600
Chris Wilson549f7362010-10-19 11:19:32 +01003601 if (HAS_BLT(dev)) {
3602 ret = intel_init_blt_ring_buffer(dev);
3603 if (ret)
3604 goto cleanup_bsd_ring;
3605 }
3606
Chris Wilson6f392d5482010-08-07 11:01:22 +01003607 dev_priv->next_seqno = 1;
3608
Daniel Vettere21af882012-02-09 20:53:27 +01003609 i915_gem_init_ppgtt(dev);
3610
Chris Wilson68f95ba2010-05-27 13:18:22 +01003611 return 0;
3612
Chris Wilson549f7362010-10-19 11:19:32 +01003613cleanup_bsd_ring:
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003614 intel_cleanup_ring_buffer(&dev_priv->ring[VCS]);
Chris Wilson68f95ba2010-05-27 13:18:22 +01003615cleanup_render_ring:
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003616 intel_cleanup_ring_buffer(&dev_priv->ring[RCS]);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08003617 return ret;
3618}
3619
3620void
3621i915_gem_cleanup_ringbuffer(struct drm_device *dev)
3622{
3623 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003624 int i;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08003625
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003626 for (i = 0; i < I915_NUM_RINGS; i++)
3627 intel_cleanup_ring_buffer(&dev_priv->ring[i]);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08003628}
3629
3630int
Eric Anholt673a3942008-07-30 12:06:12 -07003631i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
3632 struct drm_file *file_priv)
3633{
3634 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003635 int ret, i;
Eric Anholt673a3942008-07-30 12:06:12 -07003636
Jesse Barnes79e53942008-11-07 14:24:08 -08003637 if (drm_core_check_feature(dev, DRIVER_MODESET))
3638 return 0;
3639
Ben Gamariba1234d2009-09-14 17:48:47 -04003640 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07003641 DRM_ERROR("Reenabling wedged hardware, good luck\n");
Ben Gamariba1234d2009-09-14 17:48:47 -04003642 atomic_set(&dev_priv->mm.wedged, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07003643 }
3644
Eric Anholt673a3942008-07-30 12:06:12 -07003645 mutex_lock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08003646 dev_priv->mm.suspended = 0;
3647
Daniel Vetterf691e2f2012-02-02 09:58:12 +01003648 ret = i915_gem_init_hw(dev);
Wu Fengguangd816f6a2009-04-18 10:43:32 +08003649 if (ret != 0) {
3650 mutex_unlock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08003651 return ret;
Wu Fengguangd816f6a2009-04-18 10:43:32 +08003652 }
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08003653
Chris Wilson69dc4982010-10-19 10:36:51 +01003654 BUG_ON(!list_empty(&dev_priv->mm.active_list));
Eric Anholt673a3942008-07-30 12:06:12 -07003655 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
3656 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003657 for (i = 0; i < I915_NUM_RINGS; i++) {
3658 BUG_ON(!list_empty(&dev_priv->ring[i].active_list));
3659 BUG_ON(!list_empty(&dev_priv->ring[i].request_list));
3660 }
Eric Anholt673a3942008-07-30 12:06:12 -07003661 mutex_unlock(&dev->struct_mutex);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04003662
Chris Wilson5f353082010-06-07 14:03:03 +01003663 ret = drm_irq_install(dev);
3664 if (ret)
3665 goto cleanup_ringbuffer;
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04003666
Eric Anholt673a3942008-07-30 12:06:12 -07003667 return 0;
Chris Wilson5f353082010-06-07 14:03:03 +01003668
3669cleanup_ringbuffer:
3670 mutex_lock(&dev->struct_mutex);
3671 i915_gem_cleanup_ringbuffer(dev);
3672 dev_priv->mm.suspended = 1;
3673 mutex_unlock(&dev->struct_mutex);
3674
3675 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003676}
3677
3678int
3679i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
3680 struct drm_file *file_priv)
3681{
Jesse Barnes79e53942008-11-07 14:24:08 -08003682 if (drm_core_check_feature(dev, DRIVER_MODESET))
3683 return 0;
3684
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04003685 drm_irq_uninstall(dev);
Linus Torvaldse6890f62009-09-08 17:09:24 -07003686 return i915_gem_idle(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07003687}
3688
3689void
3690i915_gem_lastclose(struct drm_device *dev)
3691{
3692 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003693
Eric Anholte806b492009-01-22 09:56:58 -08003694 if (drm_core_check_feature(dev, DRIVER_MODESET))
3695 return;
3696
Keith Packard6dbe2772008-10-14 21:41:13 -07003697 ret = i915_gem_idle(dev);
3698 if (ret)
3699 DRM_ERROR("failed to idle hardware: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07003700}
3701
Chris Wilson64193402010-10-24 12:38:05 +01003702static void
3703init_ring_lists(struct intel_ring_buffer *ring)
3704{
3705 INIT_LIST_HEAD(&ring->active_list);
3706 INIT_LIST_HEAD(&ring->request_list);
3707 INIT_LIST_HEAD(&ring->gpu_write_list);
3708}
3709
Eric Anholt673a3942008-07-30 12:06:12 -07003710void
3711i915_gem_load(struct drm_device *dev)
3712{
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02003713 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07003714 drm_i915_private_t *dev_priv = dev->dev_private;
3715
Chris Wilson69dc4982010-10-19 10:36:51 +01003716 INIT_LIST_HEAD(&dev_priv->mm.active_list);
Eric Anholt673a3942008-07-30 12:06:12 -07003717 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
3718 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
Chris Wilsonf13d3f72010-09-20 17:36:15 +01003719 INIT_LIST_HEAD(&dev_priv->mm.pinned_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07003720 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Chris Wilsonbe726152010-07-23 23:18:50 +01003721 INIT_LIST_HEAD(&dev_priv->mm.deferred_free_list);
Daniel Vetter93a37f22010-11-05 20:24:53 +01003722 INIT_LIST_HEAD(&dev_priv->mm.gtt_list);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003723 for (i = 0; i < I915_NUM_RINGS; i++)
3724 init_ring_lists(&dev_priv->ring[i]);
Daniel Vetter4b9de732011-10-09 21:52:02 +02003725 for (i = 0; i < I915_MAX_NUM_FENCES; i++)
Daniel Vetter007cc8a2010-04-28 11:02:31 +02003726 INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
Eric Anholt673a3942008-07-30 12:06:12 -07003727 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
3728 i915_gem_retire_work_handler);
Chris Wilson30dbf0c2010-09-25 10:19:17 +01003729 init_completion(&dev_priv->error_completion);
Chris Wilson31169712009-09-14 16:50:28 +01003730
Dave Airlie94400122010-07-20 13:15:31 +10003731 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
3732 if (IS_GEN3(dev)) {
3733 u32 tmp = I915_READ(MI_ARB_STATE);
3734 if (!(tmp & MI_ARB_C3_LP_WRITE_ENABLE)) {
3735 /* arb state is a masked write, so set bit + bit in mask */
3736 tmp = MI_ARB_C3_LP_WRITE_ENABLE | (MI_ARB_C3_LP_WRITE_ENABLE << MI_ARB_MASK_SHIFT);
3737 I915_WRITE(MI_ARB_STATE, tmp);
3738 }
3739 }
3740
Chris Wilson72bfa192010-12-19 11:42:05 +00003741 dev_priv->relative_constants_mode = I915_EXEC_CONSTANTS_REL_GENERAL;
3742
Jesse Barnesde151cf2008-11-12 10:03:55 -08003743 /* Old X drivers will take 0-2 for front, back, depth buffers */
Eric Anholtb397c832010-01-26 09:43:10 -08003744 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3745 dev_priv->fence_reg_start = 3;
Jesse Barnesde151cf2008-11-12 10:03:55 -08003746
Chris Wilsona6c45cf2010-09-17 00:32:17 +01003747 if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08003748 dev_priv->num_fence_regs = 16;
3749 else
3750 dev_priv->num_fence_regs = 8;
3751
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02003752 /* Initialize fence registers to zero */
Eric Anholt10ed13e2011-05-06 13:53:49 -07003753 for (i = 0; i < dev_priv->num_fence_regs; i++) {
3754 i915_gem_clear_fence_reg(dev, &dev_priv->fence_regs[i]);
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02003755 }
Eric Anholt10ed13e2011-05-06 13:53:49 -07003756
Eric Anholt673a3942008-07-30 12:06:12 -07003757 i915_gem_detect_bit_6_swizzle(dev);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003758 init_waitqueue_head(&dev_priv->pending_flip_queue);
Chris Wilson17250b72010-10-28 12:51:39 +01003759
Chris Wilsonce453d82011-02-21 14:43:56 +00003760 dev_priv->mm.interruptible = true;
3761
Chris Wilson17250b72010-10-28 12:51:39 +01003762 dev_priv->mm.inactive_shrinker.shrink = i915_gem_inactive_shrink;
3763 dev_priv->mm.inactive_shrinker.seeks = DEFAULT_SEEKS;
3764 register_shrinker(&dev_priv->mm.inactive_shrinker);
Eric Anholt673a3942008-07-30 12:06:12 -07003765}
Dave Airlie71acb5e2008-12-30 20:31:46 +10003766
3767/*
3768 * Create a physically contiguous memory object for this object
3769 * e.g. for cursor + overlay regs
3770 */
Chris Wilson995b6762010-08-20 13:23:26 +01003771static int i915_gem_init_phys_object(struct drm_device *dev,
3772 int id, int size, int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10003773{
3774 drm_i915_private_t *dev_priv = dev->dev_private;
3775 struct drm_i915_gem_phys_object *phys_obj;
3776 int ret;
3777
3778 if (dev_priv->mm.phys_objs[id - 1] || !size)
3779 return 0;
3780
Eric Anholt9a298b22009-03-24 12:23:04 -07003781 phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
Dave Airlie71acb5e2008-12-30 20:31:46 +10003782 if (!phys_obj)
3783 return -ENOMEM;
3784
3785 phys_obj->id = id;
3786
Chris Wilson6eeefaf2010-08-07 11:01:39 +01003787 phys_obj->handle = drm_pci_alloc(dev, size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10003788 if (!phys_obj->handle) {
3789 ret = -ENOMEM;
3790 goto kfree_obj;
3791 }
3792#ifdef CONFIG_X86
3793 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
3794#endif
3795
3796 dev_priv->mm.phys_objs[id - 1] = phys_obj;
3797
3798 return 0;
3799kfree_obj:
Eric Anholt9a298b22009-03-24 12:23:04 -07003800 kfree(phys_obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10003801 return ret;
3802}
3803
Chris Wilson995b6762010-08-20 13:23:26 +01003804static void i915_gem_free_phys_object(struct drm_device *dev, int id)
Dave Airlie71acb5e2008-12-30 20:31:46 +10003805{
3806 drm_i915_private_t *dev_priv = dev->dev_private;
3807 struct drm_i915_gem_phys_object *phys_obj;
3808
3809 if (!dev_priv->mm.phys_objs[id - 1])
3810 return;
3811
3812 phys_obj = dev_priv->mm.phys_objs[id - 1];
3813 if (phys_obj->cur_obj) {
3814 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
3815 }
3816
3817#ifdef CONFIG_X86
3818 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
3819#endif
3820 drm_pci_free(dev, phys_obj->handle);
3821 kfree(phys_obj);
3822 dev_priv->mm.phys_objs[id - 1] = NULL;
3823}
3824
3825void i915_gem_free_all_phys_object(struct drm_device *dev)
3826{
3827 int i;
3828
Dave Airlie260883c2009-01-22 17:58:49 +10003829 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
Dave Airlie71acb5e2008-12-30 20:31:46 +10003830 i915_gem_free_phys_object(dev, i);
3831}
3832
3833void i915_gem_detach_phys_object(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00003834 struct drm_i915_gem_object *obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10003835{
Chris Wilson05394f32010-11-08 19:18:58 +00003836 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Chris Wilsone5281cc2010-10-28 13:45:36 +01003837 char *vaddr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10003838 int i;
Dave Airlie71acb5e2008-12-30 20:31:46 +10003839 int page_count;
3840
Chris Wilson05394f32010-11-08 19:18:58 +00003841 if (!obj->phys_obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10003842 return;
Chris Wilson05394f32010-11-08 19:18:58 +00003843 vaddr = obj->phys_obj->handle->vaddr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10003844
Chris Wilson05394f32010-11-08 19:18:58 +00003845 page_count = obj->base.size / PAGE_SIZE;
Dave Airlie71acb5e2008-12-30 20:31:46 +10003846 for (i = 0; i < page_count; i++) {
Hugh Dickins5949eac2011-06-27 16:18:18 -07003847 struct page *page = shmem_read_mapping_page(mapping, i);
Chris Wilsone5281cc2010-10-28 13:45:36 +01003848 if (!IS_ERR(page)) {
3849 char *dst = kmap_atomic(page);
3850 memcpy(dst, vaddr + i*PAGE_SIZE, PAGE_SIZE);
3851 kunmap_atomic(dst);
Dave Airlie71acb5e2008-12-30 20:31:46 +10003852
Chris Wilsone5281cc2010-10-28 13:45:36 +01003853 drm_clflush_pages(&page, 1);
3854
3855 set_page_dirty(page);
3856 mark_page_accessed(page);
3857 page_cache_release(page);
3858 }
Dave Airlie71acb5e2008-12-30 20:31:46 +10003859 }
Daniel Vetter40ce6572010-11-05 18:12:18 +01003860 intel_gtt_chipset_flush();
Chris Wilsond78b47b2009-06-17 21:52:49 +01003861
Chris Wilson05394f32010-11-08 19:18:58 +00003862 obj->phys_obj->cur_obj = NULL;
3863 obj->phys_obj = NULL;
Dave Airlie71acb5e2008-12-30 20:31:46 +10003864}
3865
3866int
3867i915_gem_attach_phys_object(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00003868 struct drm_i915_gem_object *obj,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01003869 int id,
3870 int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10003871{
Chris Wilson05394f32010-11-08 19:18:58 +00003872 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Dave Airlie71acb5e2008-12-30 20:31:46 +10003873 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie71acb5e2008-12-30 20:31:46 +10003874 int ret = 0;
3875 int page_count;
3876 int i;
3877
3878 if (id > I915_MAX_PHYS_OBJECT)
3879 return -EINVAL;
3880
Chris Wilson05394f32010-11-08 19:18:58 +00003881 if (obj->phys_obj) {
3882 if (obj->phys_obj->id == id)
Dave Airlie71acb5e2008-12-30 20:31:46 +10003883 return 0;
3884 i915_gem_detach_phys_object(dev, obj);
3885 }
3886
Dave Airlie71acb5e2008-12-30 20:31:46 +10003887 /* create a new object */
3888 if (!dev_priv->mm.phys_objs[id - 1]) {
3889 ret = i915_gem_init_phys_object(dev, id,
Chris Wilson05394f32010-11-08 19:18:58 +00003890 obj->base.size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10003891 if (ret) {
Chris Wilson05394f32010-11-08 19:18:58 +00003892 DRM_ERROR("failed to init phys object %d size: %zu\n",
3893 id, obj->base.size);
Chris Wilsone5281cc2010-10-28 13:45:36 +01003894 return ret;
Dave Airlie71acb5e2008-12-30 20:31:46 +10003895 }
3896 }
3897
3898 /* bind to the object */
Chris Wilson05394f32010-11-08 19:18:58 +00003899 obj->phys_obj = dev_priv->mm.phys_objs[id - 1];
3900 obj->phys_obj->cur_obj = obj;
Dave Airlie71acb5e2008-12-30 20:31:46 +10003901
Chris Wilson05394f32010-11-08 19:18:58 +00003902 page_count = obj->base.size / PAGE_SIZE;
Dave Airlie71acb5e2008-12-30 20:31:46 +10003903
3904 for (i = 0; i < page_count; i++) {
Chris Wilsone5281cc2010-10-28 13:45:36 +01003905 struct page *page;
3906 char *dst, *src;
Dave Airlie71acb5e2008-12-30 20:31:46 +10003907
Hugh Dickins5949eac2011-06-27 16:18:18 -07003908 page = shmem_read_mapping_page(mapping, i);
Chris Wilsone5281cc2010-10-28 13:45:36 +01003909 if (IS_ERR(page))
3910 return PTR_ERR(page);
3911
Chris Wilsonff75b9b2010-10-30 22:52:31 +01003912 src = kmap_atomic(page);
Chris Wilson05394f32010-11-08 19:18:58 +00003913 dst = obj->phys_obj->handle->vaddr + (i * PAGE_SIZE);
Dave Airlie71acb5e2008-12-30 20:31:46 +10003914 memcpy(dst, src, PAGE_SIZE);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -07003915 kunmap_atomic(src);
Chris Wilsone5281cc2010-10-28 13:45:36 +01003916
3917 mark_page_accessed(page);
3918 page_cache_release(page);
Dave Airlie71acb5e2008-12-30 20:31:46 +10003919 }
3920
3921 return 0;
Dave Airlie71acb5e2008-12-30 20:31:46 +10003922}
3923
3924static int
Chris Wilson05394f32010-11-08 19:18:58 +00003925i915_gem_phys_pwrite(struct drm_device *dev,
3926 struct drm_i915_gem_object *obj,
Dave Airlie71acb5e2008-12-30 20:31:46 +10003927 struct drm_i915_gem_pwrite *args,
3928 struct drm_file *file_priv)
3929{
Chris Wilson05394f32010-11-08 19:18:58 +00003930 void *vaddr = obj->phys_obj->handle->vaddr + args->offset;
Chris Wilsonb47b30c2010-11-08 01:12:29 +00003931 char __user *user_data = (char __user *) (uintptr_t) args->data_ptr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10003932
Chris Wilsonb47b30c2010-11-08 01:12:29 +00003933 if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) {
3934 unsigned long unwritten;
3935
3936 /* The physical object once assigned is fixed for the lifetime
3937 * of the obj, so we can safely drop the lock and continue
3938 * to access vaddr.
3939 */
3940 mutex_unlock(&dev->struct_mutex);
3941 unwritten = copy_from_user(vaddr, user_data, args->size);
3942 mutex_lock(&dev->struct_mutex);
3943 if (unwritten)
3944 return -EFAULT;
3945 }
Dave Airlie71acb5e2008-12-30 20:31:46 +10003946
Daniel Vetter40ce6572010-11-05 18:12:18 +01003947 intel_gtt_chipset_flush();
Dave Airlie71acb5e2008-12-30 20:31:46 +10003948 return 0;
3949}
Eric Anholtb9624422009-06-03 07:27:35 +00003950
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003951void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00003952{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003953 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00003954
3955 /* Clean up our request list when the client is going away, so that
3956 * later retire_requests won't dereference our soon-to-be-gone
3957 * file_priv.
3958 */
Chris Wilson1c255952010-09-26 11:03:27 +01003959 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003960 while (!list_empty(&file_priv->mm.request_list)) {
3961 struct drm_i915_gem_request *request;
3962
3963 request = list_first_entry(&file_priv->mm.request_list,
3964 struct drm_i915_gem_request,
3965 client_list);
3966 list_del(&request->client_list);
3967 request->file_priv = NULL;
3968 }
Chris Wilson1c255952010-09-26 11:03:27 +01003969 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00003970}
Chris Wilson31169712009-09-14 16:50:28 +01003971
Chris Wilson31169712009-09-14 16:50:28 +01003972static int
Chris Wilson1637ef42010-04-20 17:10:35 +01003973i915_gpu_is_active(struct drm_device *dev)
3974{
3975 drm_i915_private_t *dev_priv = dev->dev_private;
3976 int lists_empty;
3977
Chris Wilson1637ef42010-04-20 17:10:35 +01003978 lists_empty = list_empty(&dev_priv->mm.flushing_list) &&
Chris Wilson17250b72010-10-28 12:51:39 +01003979 list_empty(&dev_priv->mm.active_list);
Chris Wilson1637ef42010-04-20 17:10:35 +01003980
3981 return !lists_empty;
3982}
3983
3984static int
Ying Han1495f232011-05-24 17:12:27 -07003985i915_gem_inactive_shrink(struct shrinker *shrinker, struct shrink_control *sc)
Chris Wilson31169712009-09-14 16:50:28 +01003986{
Chris Wilson17250b72010-10-28 12:51:39 +01003987 struct drm_i915_private *dev_priv =
3988 container_of(shrinker,
3989 struct drm_i915_private,
3990 mm.inactive_shrinker);
3991 struct drm_device *dev = dev_priv->dev;
3992 struct drm_i915_gem_object *obj, *next;
Ying Han1495f232011-05-24 17:12:27 -07003993 int nr_to_scan = sc->nr_to_scan;
Chris Wilson17250b72010-10-28 12:51:39 +01003994 int cnt;
3995
3996 if (!mutex_trylock(&dev->struct_mutex))
Chris Wilsonbbe2e112010-10-28 22:35:07 +01003997 return 0;
Chris Wilson31169712009-09-14 16:50:28 +01003998
3999 /* "fast-path" to count number of available objects */
4000 if (nr_to_scan == 0) {
Chris Wilson17250b72010-10-28 12:51:39 +01004001 cnt = 0;
4002 list_for_each_entry(obj,
4003 &dev_priv->mm.inactive_list,
4004 mm_list)
4005 cnt++;
4006 mutex_unlock(&dev->struct_mutex);
4007 return cnt / 100 * sysctl_vfs_cache_pressure;
Chris Wilson31169712009-09-14 16:50:28 +01004008 }
4009
Chris Wilson1637ef42010-04-20 17:10:35 +01004010rescan:
Chris Wilson31169712009-09-14 16:50:28 +01004011 /* first scan for clean buffers */
Chris Wilson17250b72010-10-28 12:51:39 +01004012 i915_gem_retire_requests(dev);
Chris Wilson31169712009-09-14 16:50:28 +01004013
Chris Wilson17250b72010-10-28 12:51:39 +01004014 list_for_each_entry_safe(obj, next,
4015 &dev_priv->mm.inactive_list,
4016 mm_list) {
4017 if (i915_gem_object_is_purgeable(obj)) {
Chris Wilson20217462010-11-23 15:26:33 +00004018 if (i915_gem_object_unbind(obj) == 0 &&
4019 --nr_to_scan == 0)
Chris Wilson17250b72010-10-28 12:51:39 +01004020 break;
Chris Wilson31169712009-09-14 16:50:28 +01004021 }
Chris Wilson31169712009-09-14 16:50:28 +01004022 }
4023
4024 /* second pass, evict/count anything still on the inactive list */
Chris Wilson17250b72010-10-28 12:51:39 +01004025 cnt = 0;
4026 list_for_each_entry_safe(obj, next,
4027 &dev_priv->mm.inactive_list,
4028 mm_list) {
Chris Wilson20217462010-11-23 15:26:33 +00004029 if (nr_to_scan &&
4030 i915_gem_object_unbind(obj) == 0)
Chris Wilson17250b72010-10-28 12:51:39 +01004031 nr_to_scan--;
Chris Wilson20217462010-11-23 15:26:33 +00004032 else
Chris Wilson17250b72010-10-28 12:51:39 +01004033 cnt++;
Chris Wilson31169712009-09-14 16:50:28 +01004034 }
4035
Chris Wilson17250b72010-10-28 12:51:39 +01004036 if (nr_to_scan && i915_gpu_is_active(dev)) {
Chris Wilson1637ef42010-04-20 17:10:35 +01004037 /*
4038 * We are desperate for pages, so as a last resort, wait
4039 * for the GPU to finish and discard whatever we can.
4040 * This has a dramatic impact to reduce the number of
4041 * OOM-killer events whilst running the GPU aggressively.
4042 */
Ben Widawskyb93f9cf2012-01-25 15:39:34 -08004043 if (i915_gpu_idle(dev, true) == 0)
Chris Wilson1637ef42010-04-20 17:10:35 +01004044 goto rescan;
4045 }
Chris Wilson17250b72010-10-28 12:51:39 +01004046 mutex_unlock(&dev->struct_mutex);
4047 return cnt / 100 * sysctl_vfs_cache_pressure;
Chris Wilson31169712009-09-14 16:50:28 +01004048}