blob: 292a74f2fa878f3029d74664f6e85a07b167270f [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
Eric Anholteb014592009-03-10 11:44:52 -0700290static int
Daniel Vetterdbf7bff2012-03-25 19:47:29 +0200291i915_gem_shmem_pread(struct drm_device *dev,
292 struct drm_i915_gem_object *obj,
293 struct drm_i915_gem_pread *args,
294 struct drm_file *file)
Eric Anholteb014592009-03-10 11:44:52 -0700295{
Chris Wilson05394f32010-11-08 19:18:58 +0000296 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Daniel Vetter8461d222011-12-14 13:57:32 +0100297 char __user *user_data;
Eric Anholteb014592009-03-10 11:44:52 -0700298 ssize_t remain;
Daniel Vetter8461d222011-12-14 13:57:32 +0100299 loff_t offset;
Ben Widawskyeb2c0c82012-02-15 14:42:43 +0100300 int shmem_page_offset, page_length, ret = 0;
Daniel Vetter8461d222011-12-14 13:57:32 +0100301 int obj_do_bit17_swizzling, page_do_bit17_swizzling;
Daniel Vetterdbf7bff2012-03-25 19:47:29 +0200302 int hit_slowpath = 0;
Daniel Vetter96d79b52012-03-25 19:47:36 +0200303 int prefaulted = 0;
Daniel Vetter84897312012-03-25 19:47:31 +0200304 int needs_clflush = 0;
Daniel Vetter692a5762012-03-25 19:47:34 +0200305 int release_page;
Eric Anholteb014592009-03-10 11:44:52 -0700306
Daniel Vetter8461d222011-12-14 13:57:32 +0100307 user_data = (char __user *) (uintptr_t) args->data_ptr;
Eric Anholteb014592009-03-10 11:44:52 -0700308 remain = args->size;
309
Daniel Vetter8461d222011-12-14 13:57:32 +0100310 obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700311
Daniel Vetter84897312012-03-25 19:47:31 +0200312 if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU)) {
313 /* If we're not in the cpu read domain, set ourself into the gtt
314 * read domain and manually flush cachelines (if required). This
315 * optimizes for the case when the gpu will dirty the data
316 * anyway again before the next pread happens. */
317 if (obj->cache_level == I915_CACHE_NONE)
318 needs_clflush = 1;
319 ret = i915_gem_object_set_to_gtt_domain(obj, false);
320 if (ret)
321 return ret;
322 }
323
Eric Anholteb014592009-03-10 11:44:52 -0700324 offset = args->offset;
325
326 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100327 struct page *page;
Daniel Vetter8461d222011-12-14 13:57:32 +0100328 char *vaddr;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100329
Eric Anholteb014592009-03-10 11:44:52 -0700330 /* Operation in this page
331 *
Eric Anholteb014592009-03-10 11:44:52 -0700332 * shmem_page_offset = offset within page in shmem file
Eric Anholteb014592009-03-10 11:44:52 -0700333 * page_length = bytes to copy for this page
334 */
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100335 shmem_page_offset = offset_in_page(offset);
Eric Anholteb014592009-03-10 11:44:52 -0700336 page_length = remain;
337 if ((shmem_page_offset + page_length) > PAGE_SIZE)
338 page_length = PAGE_SIZE - shmem_page_offset;
Eric Anholteb014592009-03-10 11:44:52 -0700339
Daniel Vetter692a5762012-03-25 19:47:34 +0200340 if (obj->pages) {
341 page = obj->pages[offset >> PAGE_SHIFT];
342 release_page = 0;
343 } else {
344 page = shmem_read_mapping_page(mapping, offset >> PAGE_SHIFT);
345 if (IS_ERR(page)) {
346 ret = PTR_ERR(page);
347 goto out;
348 }
349 release_page = 1;
Jesper Juhlb65552f2011-06-12 20:53:44 +0000350 }
Chris Wilsone5281cc2010-10-28 13:45:36 +0100351
Daniel Vetter8461d222011-12-14 13:57:32 +0100352 page_do_bit17_swizzling = obj_do_bit17_swizzling &&
353 (page_to_phys(page) & (1 << 17)) != 0;
354
Daniel Vetterdbf7bff2012-03-25 19:47:29 +0200355 if (!page_do_bit17_swizzling) {
356 vaddr = kmap_atomic(page);
Daniel Vetter84897312012-03-25 19:47:31 +0200357 if (needs_clflush)
358 drm_clflush_virt_range(vaddr + shmem_page_offset,
359 page_length);
Daniel Vetterdbf7bff2012-03-25 19:47:29 +0200360 ret = __copy_to_user_inatomic(user_data,
361 vaddr + shmem_page_offset,
362 page_length);
363 kunmap_atomic(vaddr);
364 if (ret == 0)
365 goto next_page;
366 }
367
368 hit_slowpath = 1;
Daniel Vetter692a5762012-03-25 19:47:34 +0200369 page_cache_get(page);
Daniel Vetterdbf7bff2012-03-25 19:47:29 +0200370 mutex_unlock(&dev->struct_mutex);
371
Daniel Vetter96d79b52012-03-25 19:47:36 +0200372 if (!prefaulted) {
373 ret = fault_in_pages_writeable(user_data, remain);
374 /* Userspace is tricking us, but we've already clobbered
375 * its pages with the prefault and promised to write the
376 * data up to the first fault. Hence ignore any errors
377 * and just continue. */
378 (void)ret;
379 prefaulted = 1;
380 }
381
Daniel Vetter8461d222011-12-14 13:57:32 +0100382 vaddr = kmap(page);
Daniel Vetter84897312012-03-25 19:47:31 +0200383 if (needs_clflush)
384 drm_clflush_virt_range(vaddr + shmem_page_offset,
385 page_length);
386
Daniel Vetter8461d222011-12-14 13:57:32 +0100387 if (page_do_bit17_swizzling)
388 ret = __copy_to_user_swizzled(user_data,
389 vaddr, shmem_page_offset,
390 page_length);
391 else
392 ret = __copy_to_user(user_data,
393 vaddr + shmem_page_offset,
394 page_length);
395 kunmap(page);
Eric Anholteb014592009-03-10 11:44:52 -0700396
Daniel Vetterdbf7bff2012-03-25 19:47:29 +0200397 mutex_lock(&dev->struct_mutex);
Daniel Vetter692a5762012-03-25 19:47:34 +0200398 page_cache_release(page);
Daniel Vetterdbf7bff2012-03-25 19:47:29 +0200399next_page:
Chris Wilsone5281cc2010-10-28 13:45:36 +0100400 mark_page_accessed(page);
Daniel Vetter692a5762012-03-25 19:47:34 +0200401 if (release_page)
402 page_cache_release(page);
Chris Wilsone5281cc2010-10-28 13:45:36 +0100403
Daniel Vetter8461d222011-12-14 13:57:32 +0100404 if (ret) {
405 ret = -EFAULT;
406 goto out;
407 }
408
Eric Anholteb014592009-03-10 11:44:52 -0700409 remain -= page_length;
Daniel Vetter8461d222011-12-14 13:57:32 +0100410 user_data += page_length;
Eric Anholteb014592009-03-10 11:44:52 -0700411 offset += page_length;
412 }
413
Chris Wilson4f27b752010-10-14 15:26:45 +0100414out:
Daniel Vetterdbf7bff2012-03-25 19:47:29 +0200415 if (hit_slowpath) {
416 /* Fixup: Kill any reinstated backing storage pages */
417 if (obj->madv == __I915_MADV_PURGED)
418 i915_gem_object_truncate(obj);
419 }
Eric Anholteb014592009-03-10 11:44:52 -0700420
421 return ret;
422}
423
Eric Anholt673a3942008-07-30 12:06:12 -0700424/**
425 * Reads data from the object referenced by handle.
426 *
427 * On error, the contents of *data are undefined.
428 */
429int
430i915_gem_pread_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000431 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700432{
433 struct drm_i915_gem_pread *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +0000434 struct drm_i915_gem_object *obj;
Chris Wilson35b62a82010-09-26 20:23:38 +0100435 int ret = 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700436
Chris Wilson51311d02010-11-17 09:10:42 +0000437 if (args->size == 0)
438 return 0;
439
440 if (!access_ok(VERIFY_WRITE,
441 (char __user *)(uintptr_t)args->data_ptr,
442 args->size))
443 return -EFAULT;
444
Chris Wilson4f27b752010-10-14 15:26:45 +0100445 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100446 if (ret)
Chris Wilson4f27b752010-10-14 15:26:45 +0100447 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700448
Chris Wilson05394f32010-11-08 19:18:58 +0000449 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +0000450 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100451 ret = -ENOENT;
452 goto unlock;
Chris Wilson4f27b752010-10-14 15:26:45 +0100453 }
Eric Anholt673a3942008-07-30 12:06:12 -0700454
Chris Wilson7dcd2492010-09-26 20:21:44 +0100455 /* Bounds check source. */
Chris Wilson05394f32010-11-08 19:18:58 +0000456 if (args->offset > obj->base.size ||
457 args->size > obj->base.size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +0100458 ret = -EINVAL;
Chris Wilson35b62a82010-09-26 20:23:38 +0100459 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +0100460 }
461
Chris Wilsondb53a302011-02-03 11:57:46 +0000462 trace_i915_gem_object_pread(obj, args->offset, args->size);
463
Daniel Vetterdbf7bff2012-03-25 19:47:29 +0200464 ret = i915_gem_shmem_pread(dev, obj, args, file);
Eric Anholt673a3942008-07-30 12:06:12 -0700465
Chris Wilson35b62a82010-09-26 20:23:38 +0100466out:
Chris Wilson05394f32010-11-08 19:18:58 +0000467 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100468unlock:
Chris Wilson4f27b752010-10-14 15:26:45 +0100469 mutex_unlock(&dev->struct_mutex);
Eric Anholteb014592009-03-10 11:44:52 -0700470 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700471}
472
Keith Packard0839ccb2008-10-30 19:38:48 -0700473/* This is the fast write path which cannot handle
474 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700475 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700476
Keith Packard0839ccb2008-10-30 19:38:48 -0700477static inline int
478fast_user_write(struct io_mapping *mapping,
479 loff_t page_base, int page_offset,
480 char __user *user_data,
481 int length)
482{
483 char *vaddr_atomic;
484 unsigned long unwritten;
485
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700486 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
Keith Packard0839ccb2008-10-30 19:38:48 -0700487 unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
488 user_data, length);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700489 io_mapping_unmap_atomic(vaddr_atomic);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100490 return unwritten;
Keith Packard0839ccb2008-10-30 19:38:48 -0700491}
492
Eric Anholt3de09aa2009-03-09 09:42:23 -0700493/**
494 * This is the fast pwrite path, where we copy the data directly from the
495 * user into the GTT, uncached.
496 */
Eric Anholt673a3942008-07-30 12:06:12 -0700497static int
Chris Wilson05394f32010-11-08 19:18:58 +0000498i915_gem_gtt_pwrite_fast(struct drm_device *dev,
499 struct drm_i915_gem_object *obj,
Eric Anholt3de09aa2009-03-09 09:42:23 -0700500 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000501 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700502{
Keith Packard0839ccb2008-10-30 19:38:48 -0700503 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700504 ssize_t remain;
Keith Packard0839ccb2008-10-30 19:38:48 -0700505 loff_t offset, page_base;
Eric Anholt673a3942008-07-30 12:06:12 -0700506 char __user *user_data;
Daniel Vetter935aaa62012-03-25 19:47:35 +0200507 int page_offset, page_length, ret;
508
509 ret = i915_gem_object_pin(obj, 0, true);
510 if (ret)
511 goto out;
512
513 ret = i915_gem_object_set_to_gtt_domain(obj, true);
514 if (ret)
515 goto out_unpin;
516
517 ret = i915_gem_object_put_fence(obj);
518 if (ret)
519 goto out_unpin;
Eric Anholt673a3942008-07-30 12:06:12 -0700520
521 user_data = (char __user *) (uintptr_t) args->data_ptr;
522 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700523
Chris Wilson05394f32010-11-08 19:18:58 +0000524 offset = obj->gtt_offset + args->offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700525
526 while (remain > 0) {
527 /* Operation in this page
528 *
Keith Packard0839ccb2008-10-30 19:38:48 -0700529 * page_base = page offset within aperture
530 * page_offset = offset within page
531 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -0700532 */
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100533 page_base = offset & PAGE_MASK;
534 page_offset = offset_in_page(offset);
Keith Packard0839ccb2008-10-30 19:38:48 -0700535 page_length = remain;
536 if ((page_offset + remain) > PAGE_SIZE)
537 page_length = PAGE_SIZE - page_offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700538
Keith Packard0839ccb2008-10-30 19:38:48 -0700539 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -0700540 * source page isn't available. Return the error and we'll
541 * retry in the slow path.
Keith Packard0839ccb2008-10-30 19:38:48 -0700542 */
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100543 if (fast_user_write(dev_priv->mm.gtt_mapping, page_base,
Daniel Vetter935aaa62012-03-25 19:47:35 +0200544 page_offset, user_data, page_length)) {
545 ret = -EFAULT;
546 goto out_unpin;
547 }
Eric Anholt673a3942008-07-30 12:06:12 -0700548
Keith Packard0839ccb2008-10-30 19:38:48 -0700549 remain -= page_length;
550 user_data += page_length;
551 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700552 }
Eric Anholt673a3942008-07-30 12:06:12 -0700553
Daniel Vetter935aaa62012-03-25 19:47:35 +0200554out_unpin:
555 i915_gem_object_unpin(obj);
556out:
Eric Anholt3de09aa2009-03-09 09:42:23 -0700557 return ret;
558}
559
Eric Anholt3043c602008-10-02 12:24:47 -0700560static int
Daniel Vettere244a442012-03-25 19:47:28 +0200561i915_gem_shmem_pwrite(struct drm_device *dev,
562 struct drm_i915_gem_object *obj,
563 struct drm_i915_gem_pwrite *args,
564 struct drm_file *file)
Eric Anholt40123c12009-03-09 13:42:30 -0700565{
Chris Wilson05394f32010-11-08 19:18:58 +0000566 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Eric Anholt40123c12009-03-09 13:42:30 -0700567 ssize_t remain;
Daniel Vetter8c599672011-12-14 13:57:31 +0100568 loff_t offset;
569 char __user *user_data;
Ben Widawskyeb2c0c82012-02-15 14:42:43 +0100570 int shmem_page_offset, page_length, ret = 0;
Daniel Vetter8c599672011-12-14 13:57:31 +0100571 int obj_do_bit17_swizzling, page_do_bit17_swizzling;
Daniel Vettere244a442012-03-25 19:47:28 +0200572 int hit_slowpath = 0;
Daniel Vetter692a5762012-03-25 19:47:34 +0200573 int release_page;
Eric Anholt40123c12009-03-09 13:42:30 -0700574
Daniel Vetter935aaa62012-03-25 19:47:35 +0200575 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
576 if (ret)
577 return ret;
578
Daniel Vetter8c599672011-12-14 13:57:31 +0100579 user_data = (char __user *) (uintptr_t) args->data_ptr;
Eric Anholt40123c12009-03-09 13:42:30 -0700580 remain = args->size;
581
Daniel Vetter8c599672011-12-14 13:57:31 +0100582 obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700583
Eric Anholt40123c12009-03-09 13:42:30 -0700584 offset = args->offset;
Chris Wilson05394f32010-11-08 19:18:58 +0000585 obj->dirty = 1;
Eric Anholt40123c12009-03-09 13:42:30 -0700586
587 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100588 struct page *page;
Daniel Vetter8c599672011-12-14 13:57:31 +0100589 char *vaddr;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100590
Eric Anholt40123c12009-03-09 13:42:30 -0700591 /* Operation in this page
592 *
Eric Anholt40123c12009-03-09 13:42:30 -0700593 * shmem_page_offset = offset within page in shmem file
Eric Anholt40123c12009-03-09 13:42:30 -0700594 * page_length = bytes to copy for this page
595 */
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100596 shmem_page_offset = offset_in_page(offset);
Eric Anholt40123c12009-03-09 13:42:30 -0700597
598 page_length = remain;
599 if ((shmem_page_offset + page_length) > PAGE_SIZE)
600 page_length = PAGE_SIZE - shmem_page_offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700601
Daniel Vetter692a5762012-03-25 19:47:34 +0200602 if (obj->pages) {
603 page = obj->pages[offset >> PAGE_SHIFT];
604 release_page = 0;
605 } else {
606 page = shmem_read_mapping_page(mapping, offset >> PAGE_SHIFT);
607 if (IS_ERR(page)) {
608 ret = PTR_ERR(page);
609 goto out;
610 }
611 release_page = 1;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100612 }
613
Daniel Vetter8c599672011-12-14 13:57:31 +0100614 page_do_bit17_swizzling = obj_do_bit17_swizzling &&
615 (page_to_phys(page) & (1 << 17)) != 0;
616
Daniel Vettere244a442012-03-25 19:47:28 +0200617 if (!page_do_bit17_swizzling) {
618 vaddr = kmap_atomic(page);
619 ret = __copy_from_user_inatomic(vaddr + shmem_page_offset,
620 user_data,
621 page_length);
622 kunmap_atomic(vaddr);
623
624 if (ret == 0)
625 goto next_page;
626 }
627
628 hit_slowpath = 1;
Daniel Vetter692a5762012-03-25 19:47:34 +0200629 page_cache_get(page);
Daniel Vettere244a442012-03-25 19:47:28 +0200630 mutex_unlock(&dev->struct_mutex);
631
Daniel Vetter8c599672011-12-14 13:57:31 +0100632 vaddr = kmap(page);
633 if (page_do_bit17_swizzling)
634 ret = __copy_from_user_swizzled(vaddr, shmem_page_offset,
635 user_data,
636 page_length);
637 else
638 ret = __copy_from_user(vaddr + shmem_page_offset,
639 user_data,
640 page_length);
641 kunmap(page);
Eric Anholt40123c12009-03-09 13:42:30 -0700642
Daniel Vettere244a442012-03-25 19:47:28 +0200643 mutex_lock(&dev->struct_mutex);
Daniel Vetter692a5762012-03-25 19:47:34 +0200644 page_cache_release(page);
Daniel Vettere244a442012-03-25 19:47:28 +0200645next_page:
Chris Wilsone5281cc2010-10-28 13:45:36 +0100646 set_page_dirty(page);
647 mark_page_accessed(page);
Daniel Vetter692a5762012-03-25 19:47:34 +0200648 if (release_page)
649 page_cache_release(page);
Chris Wilsone5281cc2010-10-28 13:45:36 +0100650
Daniel Vetter8c599672011-12-14 13:57:31 +0100651 if (ret) {
652 ret = -EFAULT;
653 goto out;
654 }
655
Eric Anholt40123c12009-03-09 13:42:30 -0700656 remain -= page_length;
Daniel Vetter8c599672011-12-14 13:57:31 +0100657 user_data += page_length;
Eric Anholt40123c12009-03-09 13:42:30 -0700658 offset += page_length;
659 }
660
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100661out:
Daniel Vettere244a442012-03-25 19:47:28 +0200662 if (hit_slowpath) {
663 /* Fixup: Kill any reinstated backing storage pages */
664 if (obj->madv == __I915_MADV_PURGED)
665 i915_gem_object_truncate(obj);
666 /* and flush dirty cachelines in case the object isn't in the cpu write
667 * domain anymore. */
668 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
669 i915_gem_clflush_object(obj);
670 intel_gtt_chipset_flush();
671 }
Daniel Vetter8c599672011-12-14 13:57:31 +0100672 }
Eric Anholt40123c12009-03-09 13:42:30 -0700673
674 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700675}
676
677/**
678 * Writes data to the object referenced by handle.
679 *
680 * On error, the contents of the buffer that were to be modified are undefined.
681 */
682int
683i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100684 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700685{
686 struct drm_i915_gem_pwrite *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +0000687 struct drm_i915_gem_object *obj;
Chris Wilson51311d02010-11-17 09:10:42 +0000688 int ret;
689
690 if (args->size == 0)
691 return 0;
692
693 if (!access_ok(VERIFY_READ,
694 (char __user *)(uintptr_t)args->data_ptr,
695 args->size))
696 return -EFAULT;
697
698 ret = fault_in_pages_readable((char __user *)(uintptr_t)args->data_ptr,
699 args->size);
700 if (ret)
701 return -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -0700702
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100703 ret = i915_mutex_lock_interruptible(dev);
704 if (ret)
705 return ret;
706
Chris Wilson05394f32010-11-08 19:18:58 +0000707 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +0000708 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100709 ret = -ENOENT;
710 goto unlock;
711 }
Eric Anholt673a3942008-07-30 12:06:12 -0700712
Chris Wilson7dcd2492010-09-26 20:21:44 +0100713 /* Bounds check destination. */
Chris Wilson05394f32010-11-08 19:18:58 +0000714 if (args->offset > obj->base.size ||
715 args->size > obj->base.size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +0100716 ret = -EINVAL;
Chris Wilson35b62a82010-09-26 20:23:38 +0100717 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +0100718 }
719
Chris Wilsondb53a302011-02-03 11:57:46 +0000720 trace_i915_gem_object_pwrite(obj, args->offset, args->size);
721
Daniel Vetter935aaa62012-03-25 19:47:35 +0200722 ret = -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -0700723 /* We can only do the GTT pwrite on untiled buffers, as otherwise
724 * it would end up going through the fenced access, and we'll get
725 * different detiling behavior between reading and writing.
726 * pread/pwrite currently are reading and writing from the CPU
727 * perspective, requiring manual detiling by the client.
728 */
Daniel Vetter5c0480f2011-12-14 13:57:30 +0100729 if (obj->phys_obj) {
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100730 ret = i915_gem_phys_pwrite(dev, obj, args, file);
Daniel Vetter5c0480f2011-12-14 13:57:30 +0100731 goto out;
732 }
733
734 if (obj->gtt_space &&
Daniel Vetter3ae53782012-03-25 19:47:33 +0200735 obj->cache_level == I915_CACHE_NONE &&
Daniel Vetter5c0480f2011-12-14 13:57:30 +0100736 obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100737 ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file);
Daniel Vetter935aaa62012-03-25 19:47:35 +0200738 /* Note that the gtt paths might fail with non-page-backed user
739 * pointers (e.g. gtt mappings when moving data between
740 * textures). Fallback to the shmem path in that case. */
Eric Anholt40123c12009-03-09 13:42:30 -0700741 }
Eric Anholt673a3942008-07-30 12:06:12 -0700742
Daniel Vetter935aaa62012-03-25 19:47:35 +0200743 if (ret == -EFAULT)
744 ret = i915_gem_shmem_pwrite(dev, obj, args, file);
Daniel Vetter5c0480f2011-12-14 13:57:30 +0100745
Chris Wilson35b62a82010-09-26 20:23:38 +0100746out:
Chris Wilson05394f32010-11-08 19:18:58 +0000747 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100748unlock:
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100749 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -0700750 return ret;
751}
752
753/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800754 * Called when user space prepares to use an object with the CPU, either
755 * through the mmap ioctl's mapping or a GTT mapping.
Eric Anholt673a3942008-07-30 12:06:12 -0700756 */
757int
758i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000759 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700760{
761 struct drm_i915_gem_set_domain *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +0000762 struct drm_i915_gem_object *obj;
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800763 uint32_t read_domains = args->read_domains;
764 uint32_t write_domain = args->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -0700765 int ret;
766
767 if (!(dev->driver->driver_features & DRIVER_GEM))
768 return -ENODEV;
769
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800770 /* Only handle setting domains to types used by the CPU. */
Chris Wilson21d509e2009-06-06 09:46:02 +0100771 if (write_domain & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800772 return -EINVAL;
773
Chris Wilson21d509e2009-06-06 09:46:02 +0100774 if (read_domains & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800775 return -EINVAL;
776
777 /* Having something in the write domain implies it's in the read
778 * domain, and only that read domain. Enforce that in the request.
779 */
780 if (write_domain != 0 && read_domains != write_domain)
781 return -EINVAL;
782
Chris Wilson76c1dec2010-09-25 11:22:51 +0100783 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100784 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +0100785 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700786
Chris Wilson05394f32010-11-08 19:18:58 +0000787 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +0000788 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100789 ret = -ENOENT;
790 goto unlock;
Chris Wilson76c1dec2010-09-25 11:22:51 +0100791 }
Jesse Barnes652c3932009-08-17 13:31:43 -0700792
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800793 if (read_domains & I915_GEM_DOMAIN_GTT) {
794 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
Eric Anholt02354392008-11-26 13:58:13 -0800795
796 /* Silently promote "you're not bound, there was nothing to do"
797 * to success, since the client was just asking us to
798 * make sure everything was done.
799 */
800 if (ret == -EINVAL)
801 ret = 0;
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800802 } else {
Eric Anholte47c68e2008-11-14 13:35:19 -0800803 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800804 }
805
Chris Wilson05394f32010-11-08 19:18:58 +0000806 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100807unlock:
Eric Anholt673a3942008-07-30 12:06:12 -0700808 mutex_unlock(&dev->struct_mutex);
809 return ret;
810}
811
812/**
813 * Called when user space has done writes to this buffer
814 */
815int
816i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000817 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700818{
819 struct drm_i915_gem_sw_finish *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +0000820 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -0700821 int ret = 0;
822
823 if (!(dev->driver->driver_features & DRIVER_GEM))
824 return -ENODEV;
825
Chris Wilson76c1dec2010-09-25 11:22:51 +0100826 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100827 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +0100828 return ret;
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100829
Chris Wilson05394f32010-11-08 19:18:58 +0000830 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +0000831 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100832 ret = -ENOENT;
833 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -0700834 }
835
Eric Anholt673a3942008-07-30 12:06:12 -0700836 /* Pinned buffers may be scanout, so flush the cache */
Chris Wilson05394f32010-11-08 19:18:58 +0000837 if (obj->pin_count)
Eric Anholte47c68e2008-11-14 13:35:19 -0800838 i915_gem_object_flush_cpu_write_domain(obj);
839
Chris Wilson05394f32010-11-08 19:18:58 +0000840 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100841unlock:
Eric Anholt673a3942008-07-30 12:06:12 -0700842 mutex_unlock(&dev->struct_mutex);
843 return ret;
844}
845
846/**
847 * Maps the contents of an object, returning the address it is mapped
848 * into.
849 *
850 * While the mapping holds a reference on the contents of the object, it doesn't
851 * imply a ref on the object itself.
852 */
853int
854i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000855 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700856{
857 struct drm_i915_gem_mmap *args = data;
858 struct drm_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -0700859 unsigned long addr;
860
861 if (!(dev->driver->driver_features & DRIVER_GEM))
862 return -ENODEV;
863
Chris Wilson05394f32010-11-08 19:18:58 +0000864 obj = drm_gem_object_lookup(dev, file, args->handle);
Eric Anholt673a3942008-07-30 12:06:12 -0700865 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100866 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -0700867
Eric Anholt673a3942008-07-30 12:06:12 -0700868 down_write(&current->mm->mmap_sem);
869 addr = do_mmap(obj->filp, 0, args->size,
870 PROT_READ | PROT_WRITE, MAP_SHARED,
871 args->offset);
872 up_write(&current->mm->mmap_sem);
Luca Barbieribc9025b2010-02-09 05:49:12 +0000873 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700874 if (IS_ERR((void *)addr))
875 return addr;
876
877 args->addr_ptr = (uint64_t) addr;
878
879 return 0;
880}
881
Jesse Barnesde151cf2008-11-12 10:03:55 -0800882/**
883 * i915_gem_fault - fault a page into the GTT
884 * vma: VMA in question
885 * vmf: fault info
886 *
887 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
888 * from userspace. The fault handler takes care of binding the object to
889 * the GTT (if needed), allocating and programming a fence register (again,
890 * only if needed based on whether the old reg is still valid or the object
891 * is tiled) and inserting a new PTE into the faulting process.
892 *
893 * Note that the faulting process may involve evicting existing objects
894 * from the GTT and/or fence registers to make room. So performance may
895 * suffer if the GTT working set is large or there are few fence registers
896 * left.
897 */
898int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
899{
Chris Wilson05394f32010-11-08 19:18:58 +0000900 struct drm_i915_gem_object *obj = to_intel_bo(vma->vm_private_data);
901 struct drm_device *dev = obj->base.dev;
Chris Wilson7d1c4802010-08-07 21:45:03 +0100902 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -0800903 pgoff_t page_offset;
904 unsigned long pfn;
905 int ret = 0;
Jesse Barnes0f973f22009-01-26 17:10:45 -0800906 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Jesse Barnesde151cf2008-11-12 10:03:55 -0800907
908 /* We don't use vmf->pgoff since that has the fake offset */
909 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
910 PAGE_SHIFT;
911
Chris Wilsond9bc7e92011-02-07 13:09:31 +0000912 ret = i915_mutex_lock_interruptible(dev);
913 if (ret)
914 goto out;
Chris Wilsona00b10c2010-09-24 21:15:47 +0100915
Chris Wilsondb53a302011-02-03 11:57:46 +0000916 trace_i915_gem_object_fault(obj, page_offset, true, write);
917
Chris Wilsond9bc7e92011-02-07 13:09:31 +0000918 /* Now bind it into the GTT if needed */
Chris Wilson919926a2010-11-12 13:42:53 +0000919 if (!obj->map_and_fenceable) {
920 ret = i915_gem_object_unbind(obj);
921 if (ret)
922 goto unlock;
Chris Wilsona00b10c2010-09-24 21:15:47 +0100923 }
Chris Wilson05394f32010-11-08 19:18:58 +0000924 if (!obj->gtt_space) {
Daniel Vetter75e9e912010-11-04 17:11:09 +0100925 ret = i915_gem_object_bind_to_gtt(obj, 0, true);
Chris Wilsonc7150892009-09-23 00:43:56 +0100926 if (ret)
927 goto unlock;
Jesse Barnesde151cf2008-11-12 10:03:55 -0800928
Eric Anholte92d03b2011-06-14 16:43:09 -0700929 ret = i915_gem_object_set_to_gtt_domain(obj, write);
930 if (ret)
931 goto unlock;
932 }
Chris Wilson4a684a42010-10-28 14:44:08 +0100933
Daniel Vetter74898d72012-02-15 23:50:22 +0100934 if (!obj->has_global_gtt_mapping)
935 i915_gem_gtt_bind_object(obj, obj->cache_level);
936
Chris Wilsond9e86c02010-11-10 16:40:20 +0000937 if (obj->tiling_mode == I915_TILING_NONE)
938 ret = i915_gem_object_put_fence(obj);
939 else
Chris Wilsonce453d82011-02-21 14:43:56 +0000940 ret = i915_gem_object_get_fence(obj, NULL);
Chris Wilsond9e86c02010-11-10 16:40:20 +0000941 if (ret)
942 goto unlock;
Jesse Barnesde151cf2008-11-12 10:03:55 -0800943
Chris Wilson05394f32010-11-08 19:18:58 +0000944 if (i915_gem_object_is_inactive(obj))
945 list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
Chris Wilson7d1c4802010-08-07 21:45:03 +0100946
Chris Wilson6299f992010-11-24 12:23:44 +0000947 obj->fault_mappable = true;
948
Chris Wilson05394f32010-11-08 19:18:58 +0000949 pfn = ((dev->agp->base + obj->gtt_offset) >> PAGE_SHIFT) +
Jesse Barnesde151cf2008-11-12 10:03:55 -0800950 page_offset;
951
952 /* Finally, remap it using the new GTT offset */
953 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
Chris Wilsonc7150892009-09-23 00:43:56 +0100954unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -0800955 mutex_unlock(&dev->struct_mutex);
Chris Wilsond9bc7e92011-02-07 13:09:31 +0000956out:
Jesse Barnesde151cf2008-11-12 10:03:55 -0800957 switch (ret) {
Chris Wilsond9bc7e92011-02-07 13:09:31 +0000958 case -EIO:
Chris Wilson045e7692010-11-07 09:18:22 +0000959 case -EAGAIN:
Chris Wilsond9bc7e92011-02-07 13:09:31 +0000960 /* Give the error handler a chance to run and move the
961 * objects off the GPU active list. Next time we service the
962 * fault, we should be able to transition the page into the
963 * GTT without touching the GPU (and so avoid further
964 * EIO/EGAIN). If the GPU is wedged, then there is no issue
965 * with coherency, just lost writes.
966 */
Chris Wilson045e7692010-11-07 09:18:22 +0000967 set_need_resched();
Chris Wilsonc7150892009-09-23 00:43:56 +0100968 case 0:
969 case -ERESTARTSYS:
Chris Wilsonbed636a2011-02-11 20:31:19 +0000970 case -EINTR:
Chris Wilsonc7150892009-09-23 00:43:56 +0100971 return VM_FAULT_NOPAGE;
Jesse Barnesde151cf2008-11-12 10:03:55 -0800972 case -ENOMEM:
Jesse Barnesde151cf2008-11-12 10:03:55 -0800973 return VM_FAULT_OOM;
Jesse Barnesde151cf2008-11-12 10:03:55 -0800974 default:
Chris Wilsonc7150892009-09-23 00:43:56 +0100975 return VM_FAULT_SIGBUS;
Jesse Barnesde151cf2008-11-12 10:03:55 -0800976 }
977}
978
979/**
Chris Wilson901782b2009-07-10 08:18:50 +0100980 * i915_gem_release_mmap - remove physical page mappings
981 * @obj: obj in question
982 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200983 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +0100984 * relinquish ownership of the pages back to the system.
985 *
986 * It is vital that we remove the page mapping if we have mapped a tiled
987 * object through the GTT and then lose the fence register due to
988 * resource pressure. Similarly if the object has been moved out of the
989 * aperture, than pages mapped into userspace must be revoked. Removing the
990 * mapping will then trigger a page fault on the next user access, allowing
991 * fixup by i915_gem_fault().
992 */
Eric Anholtd05ca302009-07-10 13:02:26 -0700993void
Chris Wilson05394f32010-11-08 19:18:58 +0000994i915_gem_release_mmap(struct drm_i915_gem_object *obj)
Chris Wilson901782b2009-07-10 08:18:50 +0100995{
Chris Wilson6299f992010-11-24 12:23:44 +0000996 if (!obj->fault_mappable)
997 return;
Chris Wilson901782b2009-07-10 08:18:50 +0100998
Chris Wilsonf6e47882011-03-20 21:09:12 +0000999 if (obj->base.dev->dev_mapping)
1000 unmap_mapping_range(obj->base.dev->dev_mapping,
1001 (loff_t)obj->base.map_list.hash.key<<PAGE_SHIFT,
1002 obj->base.size, 1);
Daniel Vetterfb7d5162010-10-01 22:05:20 +02001003
Chris Wilson6299f992010-11-24 12:23:44 +00001004 obj->fault_mappable = false;
Chris Wilson901782b2009-07-10 08:18:50 +01001005}
1006
Chris Wilson92b88ae2010-11-09 11:47:32 +00001007static uint32_t
Chris Wilsone28f8712011-07-18 13:11:49 -07001008i915_gem_get_gtt_size(struct drm_device *dev, uint32_t size, int tiling_mode)
Chris Wilson92b88ae2010-11-09 11:47:32 +00001009{
Chris Wilsone28f8712011-07-18 13:11:49 -07001010 uint32_t gtt_size;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001011
1012 if (INTEL_INFO(dev)->gen >= 4 ||
Chris Wilsone28f8712011-07-18 13:11:49 -07001013 tiling_mode == I915_TILING_NONE)
1014 return size;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001015
1016 /* Previous chips need a power-of-two fence region when tiling */
1017 if (INTEL_INFO(dev)->gen == 3)
Chris Wilsone28f8712011-07-18 13:11:49 -07001018 gtt_size = 1024*1024;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001019 else
Chris Wilsone28f8712011-07-18 13:11:49 -07001020 gtt_size = 512*1024;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001021
Chris Wilsone28f8712011-07-18 13:11:49 -07001022 while (gtt_size < size)
1023 gtt_size <<= 1;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001024
Chris Wilsone28f8712011-07-18 13:11:49 -07001025 return gtt_size;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001026}
1027
Jesse Barnesde151cf2008-11-12 10:03:55 -08001028/**
1029 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1030 * @obj: object to check
1031 *
1032 * Return the required GTT alignment for an object, taking into account
Daniel Vetter5e783302010-11-14 22:32:36 +01001033 * potential fence register mapping.
Jesse Barnesde151cf2008-11-12 10:03:55 -08001034 */
1035static uint32_t
Chris Wilsone28f8712011-07-18 13:11:49 -07001036i915_gem_get_gtt_alignment(struct drm_device *dev,
1037 uint32_t size,
1038 int tiling_mode)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001039{
Jesse Barnesde151cf2008-11-12 10:03:55 -08001040 /*
1041 * Minimum alignment is 4k (GTT page size), but might be greater
1042 * if a fence register is needed for the object.
1043 */
Chris Wilsona00b10c2010-09-24 21:15:47 +01001044 if (INTEL_INFO(dev)->gen >= 4 ||
Chris Wilsone28f8712011-07-18 13:11:49 -07001045 tiling_mode == I915_TILING_NONE)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001046 return 4096;
1047
1048 /*
1049 * Previous chips need to be aligned to the size of the smallest
1050 * fence register that can contain the object.
1051 */
Chris Wilsone28f8712011-07-18 13:11:49 -07001052 return i915_gem_get_gtt_size(dev, size, tiling_mode);
Chris Wilsona00b10c2010-09-24 21:15:47 +01001053}
1054
Daniel Vetter5e783302010-11-14 22:32:36 +01001055/**
1056 * i915_gem_get_unfenced_gtt_alignment - return required GTT alignment for an
1057 * unfenced object
Chris Wilsone28f8712011-07-18 13:11:49 -07001058 * @dev: the device
1059 * @size: size of the object
1060 * @tiling_mode: tiling mode of the object
Daniel Vetter5e783302010-11-14 22:32:36 +01001061 *
1062 * Return the required GTT alignment for an object, only taking into account
1063 * unfenced tiled surface requirements.
1064 */
Chris Wilson467cffb2011-03-07 10:42:03 +00001065uint32_t
Chris Wilsone28f8712011-07-18 13:11:49 -07001066i915_gem_get_unfenced_gtt_alignment(struct drm_device *dev,
1067 uint32_t size,
1068 int tiling_mode)
Daniel Vetter5e783302010-11-14 22:32:36 +01001069{
Daniel Vetter5e783302010-11-14 22:32:36 +01001070 /*
1071 * Minimum alignment is 4k (GTT page size) for sane hw.
1072 */
1073 if (INTEL_INFO(dev)->gen >= 4 || IS_G33(dev) ||
Chris Wilsone28f8712011-07-18 13:11:49 -07001074 tiling_mode == I915_TILING_NONE)
Daniel Vetter5e783302010-11-14 22:32:36 +01001075 return 4096;
1076
Chris Wilsone28f8712011-07-18 13:11:49 -07001077 /* Previous hardware however needs to be aligned to a power-of-two
1078 * tile height. The simplest method for determining this is to reuse
1079 * the power-of-tile object size.
Daniel Vetter5e783302010-11-14 22:32:36 +01001080 */
Chris Wilsone28f8712011-07-18 13:11:49 -07001081 return i915_gem_get_gtt_size(dev, size, tiling_mode);
Daniel Vetter5e783302010-11-14 22:32:36 +01001082}
1083
Jesse Barnesde151cf2008-11-12 10:03:55 -08001084int
Dave Airlieff72145b2011-02-07 12:16:14 +10001085i915_gem_mmap_gtt(struct drm_file *file,
1086 struct drm_device *dev,
1087 uint32_t handle,
1088 uint64_t *offset)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001089{
Chris Wilsonda761a62010-10-27 17:37:08 +01001090 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00001091 struct drm_i915_gem_object *obj;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001092 int ret;
1093
1094 if (!(dev->driver->driver_features & DRIVER_GEM))
1095 return -ENODEV;
1096
Chris Wilson76c1dec2010-09-25 11:22:51 +01001097 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001098 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001099 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001100
Dave Airlieff72145b2011-02-07 12:16:14 +10001101 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00001102 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001103 ret = -ENOENT;
1104 goto unlock;
1105 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001106
Chris Wilson05394f32010-11-08 19:18:58 +00001107 if (obj->base.size > dev_priv->mm.gtt_mappable_end) {
Chris Wilsonda761a62010-10-27 17:37:08 +01001108 ret = -E2BIG;
Eric Anholtff56b0b2011-10-31 23:16:21 -07001109 goto out;
Chris Wilsonda761a62010-10-27 17:37:08 +01001110 }
1111
Chris Wilson05394f32010-11-08 19:18:58 +00001112 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilsonab182822009-09-22 18:46:17 +01001113 DRM_ERROR("Attempting to mmap a purgeable buffer\n");
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001114 ret = -EINVAL;
1115 goto out;
Chris Wilsonab182822009-09-22 18:46:17 +01001116 }
1117
Chris Wilson05394f32010-11-08 19:18:58 +00001118 if (!obj->base.map_list.map) {
Rob Clarkb464e9a2011-08-10 08:09:08 -05001119 ret = drm_gem_create_mmap_offset(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001120 if (ret)
1121 goto out;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001122 }
1123
Dave Airlieff72145b2011-02-07 12:16:14 +10001124 *offset = (u64)obj->base.map_list.hash.key << PAGE_SHIFT;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001125
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001126out:
Chris Wilson05394f32010-11-08 19:18:58 +00001127 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001128unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001129 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001130 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001131}
1132
Dave Airlieff72145b2011-02-07 12:16:14 +10001133/**
1134 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1135 * @dev: DRM device
1136 * @data: GTT mapping ioctl data
1137 * @file: GEM object info
1138 *
1139 * Simply returns the fake offset to userspace so it can mmap it.
1140 * The mmap call will end up in drm_gem_mmap(), which will set things
1141 * up so we can get faults in the handler above.
1142 *
1143 * The fault handler will take care of binding the object into the GTT
1144 * (since it may have been evicted to make room for something), allocating
1145 * a fence register, and mapping the appropriate aperture address into
1146 * userspace.
1147 */
1148int
1149i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
1150 struct drm_file *file)
1151{
1152 struct drm_i915_gem_mmap_gtt *args = data;
1153
1154 if (!(dev->driver->driver_features & DRIVER_GEM))
1155 return -ENODEV;
1156
1157 return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
1158}
1159
1160
Chris Wilsone5281cc2010-10-28 13:45:36 +01001161static int
Chris Wilson05394f32010-11-08 19:18:58 +00001162i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj,
Chris Wilsone5281cc2010-10-28 13:45:36 +01001163 gfp_t gfpmask)
1164{
Chris Wilsone5281cc2010-10-28 13:45:36 +01001165 int page_count, i;
1166 struct address_space *mapping;
1167 struct inode *inode;
1168 struct page *page;
1169
1170 /* Get the list of pages out of our struct file. They'll be pinned
1171 * at this point until we release them.
1172 */
Chris Wilson05394f32010-11-08 19:18:58 +00001173 page_count = obj->base.size / PAGE_SIZE;
1174 BUG_ON(obj->pages != NULL);
1175 obj->pages = drm_malloc_ab(page_count, sizeof(struct page *));
1176 if (obj->pages == NULL)
Chris Wilsone5281cc2010-10-28 13:45:36 +01001177 return -ENOMEM;
1178
Chris Wilson05394f32010-11-08 19:18:58 +00001179 inode = obj->base.filp->f_path.dentry->d_inode;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001180 mapping = inode->i_mapping;
Hugh Dickins5949eac2011-06-27 16:18:18 -07001181 gfpmask |= mapping_gfp_mask(mapping);
1182
Chris Wilsone5281cc2010-10-28 13:45:36 +01001183 for (i = 0; i < page_count; i++) {
Hugh Dickins5949eac2011-06-27 16:18:18 -07001184 page = shmem_read_mapping_page_gfp(mapping, i, gfpmask);
Chris Wilsone5281cc2010-10-28 13:45:36 +01001185 if (IS_ERR(page))
1186 goto err_pages;
1187
Chris Wilson05394f32010-11-08 19:18:58 +00001188 obj->pages[i] = page;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001189 }
1190
Daniel Vetter6dacfd22011-09-12 21:30:02 +02001191 if (i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilsone5281cc2010-10-28 13:45:36 +01001192 i915_gem_object_do_bit_17_swizzle(obj);
1193
1194 return 0;
1195
1196err_pages:
1197 while (i--)
Chris Wilson05394f32010-11-08 19:18:58 +00001198 page_cache_release(obj->pages[i]);
Chris Wilsone5281cc2010-10-28 13:45:36 +01001199
Chris Wilson05394f32010-11-08 19:18:58 +00001200 drm_free_large(obj->pages);
1201 obj->pages = NULL;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001202 return PTR_ERR(page);
1203}
1204
Chris Wilson5cdf5882010-09-27 15:51:07 +01001205static void
Chris Wilson05394f32010-11-08 19:18:58 +00001206i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07001207{
Chris Wilson05394f32010-11-08 19:18:58 +00001208 int page_count = obj->base.size / PAGE_SIZE;
Eric Anholt673a3942008-07-30 12:06:12 -07001209 int i;
1210
Chris Wilson05394f32010-11-08 19:18:58 +00001211 BUG_ON(obj->madv == __I915_MADV_PURGED);
Eric Anholt856fa192009-03-19 14:10:50 -07001212
Daniel Vetter6dacfd22011-09-12 21:30:02 +02001213 if (i915_gem_object_needs_bit17_swizzle(obj))
Eric Anholt280b7132009-03-12 16:56:27 -07001214 i915_gem_object_save_bit_17_swizzle(obj);
1215
Chris Wilson05394f32010-11-08 19:18:58 +00001216 if (obj->madv == I915_MADV_DONTNEED)
1217 obj->dirty = 0;
Chris Wilson3ef94da2009-09-14 16:50:29 +01001218
1219 for (i = 0; i < page_count; i++) {
Chris Wilson05394f32010-11-08 19:18:58 +00001220 if (obj->dirty)
1221 set_page_dirty(obj->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001222
Chris Wilson05394f32010-11-08 19:18:58 +00001223 if (obj->madv == I915_MADV_WILLNEED)
1224 mark_page_accessed(obj->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001225
Chris Wilson05394f32010-11-08 19:18:58 +00001226 page_cache_release(obj->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001227 }
Chris Wilson05394f32010-11-08 19:18:58 +00001228 obj->dirty = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001229
Chris Wilson05394f32010-11-08 19:18:58 +00001230 drm_free_large(obj->pages);
1231 obj->pages = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001232}
1233
Chris Wilson54cf91d2010-11-25 18:00:26 +00001234void
Chris Wilson05394f32010-11-08 19:18:58 +00001235i915_gem_object_move_to_active(struct drm_i915_gem_object *obj,
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001236 struct intel_ring_buffer *ring,
1237 u32 seqno)
Eric Anholt673a3942008-07-30 12:06:12 -07001238{
Chris Wilson05394f32010-11-08 19:18:58 +00001239 struct drm_device *dev = obj->base.dev;
Chris Wilson69dc4982010-10-19 10:36:51 +01001240 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter617dbe22010-02-11 22:16:02 +01001241
Zou Nan hai852835f2010-05-21 09:08:56 +08001242 BUG_ON(ring == NULL);
Chris Wilson05394f32010-11-08 19:18:58 +00001243 obj->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001244
1245 /* Add a reference if we're newly entering the active list. */
Chris Wilson05394f32010-11-08 19:18:58 +00001246 if (!obj->active) {
1247 drm_gem_object_reference(&obj->base);
1248 obj->active = 1;
Eric Anholt673a3942008-07-30 12:06:12 -07001249 }
Daniel Vettere35a41d2010-02-11 22:13:59 +01001250
Eric Anholt673a3942008-07-30 12:06:12 -07001251 /* Move from whatever list we were on to the tail of execution. */
Chris Wilson05394f32010-11-08 19:18:58 +00001252 list_move_tail(&obj->mm_list, &dev_priv->mm.active_list);
1253 list_move_tail(&obj->ring_list, &ring->active_list);
Chris Wilsoncaea7472010-11-12 13:53:37 +00001254
Chris Wilson05394f32010-11-08 19:18:58 +00001255 obj->last_rendering_seqno = seqno;
Chris Wilsoncaea7472010-11-12 13:53:37 +00001256 if (obj->fenced_gpu_access) {
1257 struct drm_i915_fence_reg *reg;
1258
1259 BUG_ON(obj->fence_reg == I915_FENCE_REG_NONE);
1260
1261 obj->last_fenced_seqno = seqno;
1262 obj->last_fenced_ring = ring;
1263
1264 reg = &dev_priv->fence_regs[obj->fence_reg];
1265 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
1266 }
1267}
1268
1269static void
1270i915_gem_object_move_off_active(struct drm_i915_gem_object *obj)
1271{
1272 list_del_init(&obj->ring_list);
1273 obj->last_rendering_seqno = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001274}
1275
Eric Anholtce44b0e2008-11-06 16:00:31 -08001276static void
Chris Wilson05394f32010-11-08 19:18:58 +00001277i915_gem_object_move_to_flushing(struct drm_i915_gem_object *obj)
Eric Anholtce44b0e2008-11-06 16:00:31 -08001278{
Chris Wilson05394f32010-11-08 19:18:58 +00001279 struct drm_device *dev = obj->base.dev;
Eric Anholtce44b0e2008-11-06 16:00:31 -08001280 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtce44b0e2008-11-06 16:00:31 -08001281
Chris Wilson05394f32010-11-08 19:18:58 +00001282 BUG_ON(!obj->active);
1283 list_move_tail(&obj->mm_list, &dev_priv->mm.flushing_list);
Chris Wilsoncaea7472010-11-12 13:53:37 +00001284
1285 i915_gem_object_move_off_active(obj);
1286}
1287
1288static void
1289i915_gem_object_move_to_inactive(struct drm_i915_gem_object *obj)
1290{
1291 struct drm_device *dev = obj->base.dev;
1292 struct drm_i915_private *dev_priv = dev->dev_private;
1293
1294 if (obj->pin_count != 0)
1295 list_move_tail(&obj->mm_list, &dev_priv->mm.pinned_list);
1296 else
1297 list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
1298
1299 BUG_ON(!list_empty(&obj->gpu_write_list));
1300 BUG_ON(!obj->active);
1301 obj->ring = NULL;
1302
1303 i915_gem_object_move_off_active(obj);
1304 obj->fenced_gpu_access = false;
Chris Wilsoncaea7472010-11-12 13:53:37 +00001305
1306 obj->active = 0;
Chris Wilson87ca9c82010-12-02 09:42:56 +00001307 obj->pending_gpu_write = false;
Chris Wilsoncaea7472010-11-12 13:53:37 +00001308 drm_gem_object_unreference(&obj->base);
1309
1310 WARN_ON(i915_verify_lists(dev));
Eric Anholtce44b0e2008-11-06 16:00:31 -08001311}
Eric Anholt673a3942008-07-30 12:06:12 -07001312
Chris Wilson963b4832009-09-20 23:03:54 +01001313/* Immediately discard the backing storage */
1314static void
Chris Wilson05394f32010-11-08 19:18:58 +00001315i915_gem_object_truncate(struct drm_i915_gem_object *obj)
Chris Wilson963b4832009-09-20 23:03:54 +01001316{
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001317 struct inode *inode;
Chris Wilson963b4832009-09-20 23:03:54 +01001318
Chris Wilsonae9fed62010-08-07 11:01:30 +01001319 /* Our goal here is to return as much of the memory as
1320 * is possible back to the system as we are called from OOM.
1321 * To do this we must instruct the shmfs to drop all of its
Hugh Dickinse2377fe2011-06-27 16:18:19 -07001322 * backing pages, *now*.
Chris Wilsonae9fed62010-08-07 11:01:30 +01001323 */
Chris Wilson05394f32010-11-08 19:18:58 +00001324 inode = obj->base.filp->f_path.dentry->d_inode;
Hugh Dickinse2377fe2011-06-27 16:18:19 -07001325 shmem_truncate_range(inode, 0, (loff_t)-1);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001326
Chris Wilsona14917e2012-02-24 21:13:38 +00001327 if (obj->base.map_list.map)
1328 drm_gem_free_mmap_offset(&obj->base);
1329
Chris Wilson05394f32010-11-08 19:18:58 +00001330 obj->madv = __I915_MADV_PURGED;
Chris Wilson963b4832009-09-20 23:03:54 +01001331}
1332
1333static inline int
Chris Wilson05394f32010-11-08 19:18:58 +00001334i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj)
Chris Wilson963b4832009-09-20 23:03:54 +01001335{
Chris Wilson05394f32010-11-08 19:18:58 +00001336 return obj->madv == I915_MADV_DONTNEED;
Chris Wilson963b4832009-09-20 23:03:54 +01001337}
1338
Eric Anholt673a3942008-07-30 12:06:12 -07001339static void
Chris Wilsondb53a302011-02-03 11:57:46 +00001340i915_gem_process_flushing_list(struct intel_ring_buffer *ring,
1341 uint32_t flush_domains)
Daniel Vetter63560392010-02-19 11:51:59 +01001342{
Chris Wilson05394f32010-11-08 19:18:58 +00001343 struct drm_i915_gem_object *obj, *next;
Daniel Vetter63560392010-02-19 11:51:59 +01001344
Chris Wilson05394f32010-11-08 19:18:58 +00001345 list_for_each_entry_safe(obj, next,
Chris Wilson64193402010-10-24 12:38:05 +01001346 &ring->gpu_write_list,
Daniel Vetter63560392010-02-19 11:51:59 +01001347 gpu_write_list) {
Chris Wilson05394f32010-11-08 19:18:58 +00001348 if (obj->base.write_domain & flush_domains) {
1349 uint32_t old_write_domain = obj->base.write_domain;
Daniel Vetter63560392010-02-19 11:51:59 +01001350
Chris Wilson05394f32010-11-08 19:18:58 +00001351 obj->base.write_domain = 0;
1352 list_del_init(&obj->gpu_write_list);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001353 i915_gem_object_move_to_active(obj, ring,
Chris Wilsondb53a302011-02-03 11:57:46 +00001354 i915_gem_next_request_seqno(ring));
Daniel Vetter63560392010-02-19 11:51:59 +01001355
Daniel Vetter63560392010-02-19 11:51:59 +01001356 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00001357 obj->base.read_domains,
Daniel Vetter63560392010-02-19 11:51:59 +01001358 old_write_domain);
1359 }
1360 }
1361}
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001362
Daniel Vetter53d227f2012-01-25 16:32:49 +01001363static u32
1364i915_gem_get_seqno(struct drm_device *dev)
1365{
1366 drm_i915_private_t *dev_priv = dev->dev_private;
1367 u32 seqno = dev_priv->next_seqno;
1368
1369 /* reserve 0 for non-seqno */
1370 if (++dev_priv->next_seqno == 0)
1371 dev_priv->next_seqno = 1;
1372
1373 return seqno;
1374}
1375
1376u32
1377i915_gem_next_request_seqno(struct intel_ring_buffer *ring)
1378{
1379 if (ring->outstanding_lazy_request == 0)
1380 ring->outstanding_lazy_request = i915_gem_get_seqno(ring->dev);
1381
1382 return ring->outstanding_lazy_request;
1383}
1384
Chris Wilson3cce4692010-10-27 16:11:02 +01001385int
Chris Wilsondb53a302011-02-03 11:57:46 +00001386i915_add_request(struct intel_ring_buffer *ring,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001387 struct drm_file *file,
Chris Wilsondb53a302011-02-03 11:57:46 +00001388 struct drm_i915_gem_request *request)
Eric Anholt673a3942008-07-30 12:06:12 -07001389{
Chris Wilsondb53a302011-02-03 11:57:46 +00001390 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07001391 uint32_t seqno;
Chris Wilsona71d8d92012-02-15 11:25:36 +00001392 u32 request_ring_position;
Eric Anholt673a3942008-07-30 12:06:12 -07001393 int was_empty;
Chris Wilson3cce4692010-10-27 16:11:02 +01001394 int ret;
1395
1396 BUG_ON(request == NULL);
Daniel Vetter53d227f2012-01-25 16:32:49 +01001397 seqno = i915_gem_next_request_seqno(ring);
Eric Anholt673a3942008-07-30 12:06:12 -07001398
Chris Wilsona71d8d92012-02-15 11:25:36 +00001399 /* Record the position of the start of the request so that
1400 * should we detect the updated seqno part-way through the
1401 * GPU processing the request, we never over-estimate the
1402 * position of the head.
1403 */
1404 request_ring_position = intel_ring_get_tail(ring);
1405
Chris Wilson3cce4692010-10-27 16:11:02 +01001406 ret = ring->add_request(ring, &seqno);
1407 if (ret)
1408 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001409
Chris Wilsondb53a302011-02-03 11:57:46 +00001410 trace_i915_gem_request_add(ring, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07001411
1412 request->seqno = seqno;
Zou Nan hai852835f2010-05-21 09:08:56 +08001413 request->ring = ring;
Chris Wilsona71d8d92012-02-15 11:25:36 +00001414 request->tail = request_ring_position;
Eric Anholt673a3942008-07-30 12:06:12 -07001415 request->emitted_jiffies = jiffies;
Zou Nan hai852835f2010-05-21 09:08:56 +08001416 was_empty = list_empty(&ring->request_list);
1417 list_add_tail(&request->list, &ring->request_list);
1418
Chris Wilsondb53a302011-02-03 11:57:46 +00001419 if (file) {
1420 struct drm_i915_file_private *file_priv = file->driver_priv;
1421
Chris Wilson1c255952010-09-26 11:03:27 +01001422 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001423 request->file_priv = file_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00001424 list_add_tail(&request->client_list,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001425 &file_priv->mm.request_list);
Chris Wilson1c255952010-09-26 11:03:27 +01001426 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00001427 }
Eric Anholt673a3942008-07-30 12:06:12 -07001428
Daniel Vetter5391d0c2012-01-25 14:03:57 +01001429 ring->outstanding_lazy_request = 0;
Chris Wilsondb53a302011-02-03 11:57:46 +00001430
Ben Gamarif65d9422009-09-14 17:48:44 -04001431 if (!dev_priv->mm.suspended) {
Ben Widawsky3e0dc6b2011-06-29 10:26:42 -07001432 if (i915_enable_hangcheck) {
1433 mod_timer(&dev_priv->hangcheck_timer,
1434 jiffies +
1435 msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
1436 }
Ben Gamarif65d9422009-09-14 17:48:44 -04001437 if (was_empty)
Chris Wilsonb3b079d2010-09-13 23:44:34 +01001438 queue_delayed_work(dev_priv->wq,
1439 &dev_priv->mm.retire_work, HZ);
Ben Gamarif65d9422009-09-14 17:48:44 -04001440 }
Chris Wilson3cce4692010-10-27 16:11:02 +01001441 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001442}
1443
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001444static inline void
1445i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
Eric Anholt673a3942008-07-30 12:06:12 -07001446{
Chris Wilson1c255952010-09-26 11:03:27 +01001447 struct drm_i915_file_private *file_priv = request->file_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07001448
Chris Wilson1c255952010-09-26 11:03:27 +01001449 if (!file_priv)
1450 return;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001451
Chris Wilson1c255952010-09-26 11:03:27 +01001452 spin_lock(&file_priv->mm.lock);
Herton Ronaldo Krzesinski09bfa512011-03-17 13:45:12 +00001453 if (request->file_priv) {
1454 list_del(&request->client_list);
1455 request->file_priv = NULL;
1456 }
Chris Wilson1c255952010-09-26 11:03:27 +01001457 spin_unlock(&file_priv->mm.lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001458}
1459
Chris Wilsondfaae392010-09-22 10:31:52 +01001460static void i915_gem_reset_ring_lists(struct drm_i915_private *dev_priv,
1461 struct intel_ring_buffer *ring)
Chris Wilson9375e442010-09-19 12:21:28 +01001462{
Chris Wilsondfaae392010-09-22 10:31:52 +01001463 while (!list_empty(&ring->request_list)) {
1464 struct drm_i915_gem_request *request;
Chris Wilson9375e442010-09-19 12:21:28 +01001465
Chris Wilsondfaae392010-09-22 10:31:52 +01001466 request = list_first_entry(&ring->request_list,
1467 struct drm_i915_gem_request,
1468 list);
1469
1470 list_del(&request->list);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001471 i915_gem_request_remove_from_client(request);
Chris Wilsondfaae392010-09-22 10:31:52 +01001472 kfree(request);
1473 }
1474
1475 while (!list_empty(&ring->active_list)) {
Chris Wilson05394f32010-11-08 19:18:58 +00001476 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001477
Chris Wilson05394f32010-11-08 19:18:58 +00001478 obj = list_first_entry(&ring->active_list,
1479 struct drm_i915_gem_object,
1480 ring_list);
Eric Anholt673a3942008-07-30 12:06:12 -07001481
Chris Wilson05394f32010-11-08 19:18:58 +00001482 obj->base.write_domain = 0;
1483 list_del_init(&obj->gpu_write_list);
1484 i915_gem_object_move_to_inactive(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001485 }
Eric Anholt673a3942008-07-30 12:06:12 -07001486}
1487
Chris Wilson312817a2010-11-22 11:50:11 +00001488static void i915_gem_reset_fences(struct drm_device *dev)
1489{
1490 struct drm_i915_private *dev_priv = dev->dev_private;
1491 int i;
1492
Daniel Vetter4b9de732011-10-09 21:52:02 +02001493 for (i = 0; i < dev_priv->num_fence_regs; i++) {
Chris Wilson312817a2010-11-22 11:50:11 +00001494 struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
Chris Wilson7d2cb392010-11-27 17:38:29 +00001495 struct drm_i915_gem_object *obj = reg->obj;
1496
1497 if (!obj)
1498 continue;
1499
1500 if (obj->tiling_mode)
1501 i915_gem_release_mmap(obj);
1502
Chris Wilsond9e86c02010-11-10 16:40:20 +00001503 reg->obj->fence_reg = I915_FENCE_REG_NONE;
1504 reg->obj->fenced_gpu_access = false;
1505 reg->obj->last_fenced_seqno = 0;
1506 reg->obj->last_fenced_ring = NULL;
1507 i915_gem_clear_fence_reg(dev, reg);
Chris Wilson312817a2010-11-22 11:50:11 +00001508 }
1509}
1510
Chris Wilson069efc12010-09-30 16:53:18 +01001511void i915_gem_reset(struct drm_device *dev)
Eric Anholt673a3942008-07-30 12:06:12 -07001512{
Chris Wilsondfaae392010-09-22 10:31:52 +01001513 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00001514 struct drm_i915_gem_object *obj;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001515 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07001516
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001517 for (i = 0; i < I915_NUM_RINGS; i++)
1518 i915_gem_reset_ring_lists(dev_priv, &dev_priv->ring[i]);
Chris Wilsondfaae392010-09-22 10:31:52 +01001519
1520 /* Remove anything from the flushing lists. The GPU cache is likely
1521 * to be lost on reset along with the data, so simply move the
1522 * lost bo to the inactive list.
1523 */
1524 while (!list_empty(&dev_priv->mm.flushing_list)) {
Akshay Joshi0206e352011-08-16 15:34:10 -04001525 obj = list_first_entry(&dev_priv->mm.flushing_list,
Chris Wilson05394f32010-11-08 19:18:58 +00001526 struct drm_i915_gem_object,
1527 mm_list);
Chris Wilson9375e442010-09-19 12:21:28 +01001528
Chris Wilson05394f32010-11-08 19:18:58 +00001529 obj->base.write_domain = 0;
1530 list_del_init(&obj->gpu_write_list);
1531 i915_gem_object_move_to_inactive(obj);
Chris Wilson9375e442010-09-19 12:21:28 +01001532 }
Chris Wilson9375e442010-09-19 12:21:28 +01001533
Chris Wilsondfaae392010-09-22 10:31:52 +01001534 /* Move everything out of the GPU domains to ensure we do any
1535 * necessary invalidation upon reuse.
1536 */
Chris Wilson05394f32010-11-08 19:18:58 +00001537 list_for_each_entry(obj,
Chris Wilson77f01232010-09-19 12:31:36 +01001538 &dev_priv->mm.inactive_list,
Chris Wilson69dc4982010-10-19 10:36:51 +01001539 mm_list)
Chris Wilson77f01232010-09-19 12:31:36 +01001540 {
Chris Wilson05394f32010-11-08 19:18:58 +00001541 obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
Chris Wilson77f01232010-09-19 12:31:36 +01001542 }
Chris Wilson069efc12010-09-30 16:53:18 +01001543
1544 /* The fence registers are invalidated so clear them out */
Chris Wilson312817a2010-11-22 11:50:11 +00001545 i915_gem_reset_fences(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07001546}
1547
1548/**
1549 * This function clears the request list as sequence numbers are passed.
1550 */
Chris Wilsona71d8d92012-02-15 11:25:36 +00001551void
Chris Wilsondb53a302011-02-03 11:57:46 +00001552i915_gem_retire_requests_ring(struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001553{
Eric Anholt673a3942008-07-30 12:06:12 -07001554 uint32_t seqno;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001555 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07001556
Chris Wilsondb53a302011-02-03 11:57:46 +00001557 if (list_empty(&ring->request_list))
Karsten Wiese6c0594a2009-02-23 15:07:57 +01001558 return;
1559
Chris Wilsondb53a302011-02-03 11:57:46 +00001560 WARN_ON(i915_verify_lists(ring->dev));
Eric Anholt673a3942008-07-30 12:06:12 -07001561
Chris Wilson78501ea2010-10-27 12:18:21 +01001562 seqno = ring->get_seqno(ring);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001563
Chris Wilson076e2c02011-01-21 10:07:18 +00001564 for (i = 0; i < ARRAY_SIZE(ring->sync_seqno); i++)
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001565 if (seqno >= ring->sync_seqno[i])
1566 ring->sync_seqno[i] = 0;
1567
Zou Nan hai852835f2010-05-21 09:08:56 +08001568 while (!list_empty(&ring->request_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001569 struct drm_i915_gem_request *request;
Eric Anholt673a3942008-07-30 12:06:12 -07001570
Zou Nan hai852835f2010-05-21 09:08:56 +08001571 request = list_first_entry(&ring->request_list,
Eric Anholt673a3942008-07-30 12:06:12 -07001572 struct drm_i915_gem_request,
1573 list);
Eric Anholt673a3942008-07-30 12:06:12 -07001574
Chris Wilsondfaae392010-09-22 10:31:52 +01001575 if (!i915_seqno_passed(seqno, request->seqno))
Eric Anholt673a3942008-07-30 12:06:12 -07001576 break;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001577
Chris Wilsondb53a302011-02-03 11:57:46 +00001578 trace_i915_gem_request_retire(ring, request->seqno);
Chris Wilsona71d8d92012-02-15 11:25:36 +00001579 /* We know the GPU must have read the request to have
1580 * sent us the seqno + interrupt, so use the position
1581 * of tail of the request to update the last known position
1582 * of the GPU head.
1583 */
1584 ring->last_retired_head = request->tail;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001585
1586 list_del(&request->list);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001587 i915_gem_request_remove_from_client(request);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001588 kfree(request);
1589 }
1590
1591 /* Move any buffers on the active list that are no longer referenced
1592 * by the ringbuffer to the flushing/inactive lists as appropriate.
1593 */
1594 while (!list_empty(&ring->active_list)) {
Chris Wilson05394f32010-11-08 19:18:58 +00001595 struct drm_i915_gem_object *obj;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001596
Akshay Joshi0206e352011-08-16 15:34:10 -04001597 obj = list_first_entry(&ring->active_list,
Chris Wilson05394f32010-11-08 19:18:58 +00001598 struct drm_i915_gem_object,
1599 ring_list);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001600
Chris Wilson05394f32010-11-08 19:18:58 +00001601 if (!i915_seqno_passed(seqno, obj->last_rendering_seqno))
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001602 break;
1603
Chris Wilson05394f32010-11-08 19:18:58 +00001604 if (obj->base.write_domain != 0)
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001605 i915_gem_object_move_to_flushing(obj);
1606 else
1607 i915_gem_object_move_to_inactive(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001608 }
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001609
Chris Wilsondb53a302011-02-03 11:57:46 +00001610 if (unlikely(ring->trace_irq_seqno &&
1611 i915_seqno_passed(seqno, ring->trace_irq_seqno))) {
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001612 ring->irq_put(ring);
Chris Wilsondb53a302011-02-03 11:57:46 +00001613 ring->trace_irq_seqno = 0;
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001614 }
Chris Wilson23bc5982010-09-29 16:10:57 +01001615
Chris Wilsondb53a302011-02-03 11:57:46 +00001616 WARN_ON(i915_verify_lists(ring->dev));
Eric Anholt673a3942008-07-30 12:06:12 -07001617}
1618
1619void
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001620i915_gem_retire_requests(struct drm_device *dev)
1621{
1622 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001623 int i;
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001624
Chris Wilsonbe726152010-07-23 23:18:50 +01001625 if (!list_empty(&dev_priv->mm.deferred_free_list)) {
Chris Wilson05394f32010-11-08 19:18:58 +00001626 struct drm_i915_gem_object *obj, *next;
Chris Wilsonbe726152010-07-23 23:18:50 +01001627
1628 /* We must be careful that during unbind() we do not
1629 * accidentally infinitely recurse into retire requests.
1630 * Currently:
1631 * retire -> free -> unbind -> wait -> retire_ring
1632 */
Chris Wilson05394f32010-11-08 19:18:58 +00001633 list_for_each_entry_safe(obj, next,
Chris Wilsonbe726152010-07-23 23:18:50 +01001634 &dev_priv->mm.deferred_free_list,
Chris Wilson69dc4982010-10-19 10:36:51 +01001635 mm_list)
Chris Wilson05394f32010-11-08 19:18:58 +00001636 i915_gem_free_object_tail(obj);
Chris Wilsonbe726152010-07-23 23:18:50 +01001637 }
1638
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001639 for (i = 0; i < I915_NUM_RINGS; i++)
Chris Wilsondb53a302011-02-03 11:57:46 +00001640 i915_gem_retire_requests_ring(&dev_priv->ring[i]);
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001641}
1642
Daniel Vetter75ef9da2010-08-21 00:25:16 +02001643static void
Eric Anholt673a3942008-07-30 12:06:12 -07001644i915_gem_retire_work_handler(struct work_struct *work)
1645{
1646 drm_i915_private_t *dev_priv;
1647 struct drm_device *dev;
Chris Wilson0a587052011-01-09 21:05:44 +00001648 bool idle;
1649 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07001650
1651 dev_priv = container_of(work, drm_i915_private_t,
1652 mm.retire_work.work);
1653 dev = dev_priv->dev;
1654
Chris Wilson891b48c2010-09-29 12:26:37 +01001655 /* Come back later if the device is busy... */
1656 if (!mutex_trylock(&dev->struct_mutex)) {
1657 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
1658 return;
1659 }
1660
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001661 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08001662
Chris Wilson0a587052011-01-09 21:05:44 +00001663 /* Send a periodic flush down the ring so we don't hold onto GEM
1664 * objects indefinitely.
1665 */
1666 idle = true;
1667 for (i = 0; i < I915_NUM_RINGS; i++) {
1668 struct intel_ring_buffer *ring = &dev_priv->ring[i];
1669
1670 if (!list_empty(&ring->gpu_write_list)) {
1671 struct drm_i915_gem_request *request;
1672 int ret;
1673
Chris Wilsondb53a302011-02-03 11:57:46 +00001674 ret = i915_gem_flush_ring(ring,
1675 0, I915_GEM_GPU_DOMAINS);
Chris Wilson0a587052011-01-09 21:05:44 +00001676 request = kzalloc(sizeof(*request), GFP_KERNEL);
1677 if (ret || request == NULL ||
Chris Wilsondb53a302011-02-03 11:57:46 +00001678 i915_add_request(ring, NULL, request))
Chris Wilson0a587052011-01-09 21:05:44 +00001679 kfree(request);
1680 }
1681
1682 idle &= list_empty(&ring->request_list);
1683 }
1684
1685 if (!dev_priv->mm.suspended && !idle)
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001686 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
Chris Wilson0a587052011-01-09 21:05:44 +00001687
Eric Anholt673a3942008-07-30 12:06:12 -07001688 mutex_unlock(&dev->struct_mutex);
1689}
1690
Chris Wilsondb53a302011-02-03 11:57:46 +00001691/**
1692 * Waits for a sequence number to be signaled, and cleans up the
1693 * request and object lists appropriately for that event.
1694 */
Daniel Vetter5a5a0c62009-09-15 22:57:36 +02001695int
Chris Wilsondb53a302011-02-03 11:57:46 +00001696i915_wait_request(struct intel_ring_buffer *ring,
Ben Widawskyb93f9cf2012-01-25 15:39:34 -08001697 uint32_t seqno,
1698 bool do_retire)
Eric Anholt673a3942008-07-30 12:06:12 -07001699{
Chris Wilsondb53a302011-02-03 11:57:46 +00001700 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001701 u32 ier;
Eric Anholt673a3942008-07-30 12:06:12 -07001702 int ret = 0;
1703
1704 BUG_ON(seqno == 0);
1705
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001706 if (atomic_read(&dev_priv->mm.wedged)) {
1707 struct completion *x = &dev_priv->error_completion;
1708 bool recovery_complete;
1709 unsigned long flags;
1710
1711 /* Give the error handler a chance to run. */
1712 spin_lock_irqsave(&x->wait.lock, flags);
1713 recovery_complete = x->done > 0;
1714 spin_unlock_irqrestore(&x->wait.lock, flags);
1715
1716 return recovery_complete ? -EIO : -EAGAIN;
1717 }
Ben Gamariffed1d02009-09-14 17:48:41 -04001718
Chris Wilson5d97eb62010-11-10 20:40:02 +00001719 if (seqno == ring->outstanding_lazy_request) {
Chris Wilson3cce4692010-10-27 16:11:02 +01001720 struct drm_i915_gem_request *request;
1721
1722 request = kzalloc(sizeof(*request), GFP_KERNEL);
1723 if (request == NULL)
Daniel Vettere35a41d2010-02-11 22:13:59 +01001724 return -ENOMEM;
Chris Wilson3cce4692010-10-27 16:11:02 +01001725
Chris Wilsondb53a302011-02-03 11:57:46 +00001726 ret = i915_add_request(ring, NULL, request);
Chris Wilson3cce4692010-10-27 16:11:02 +01001727 if (ret) {
1728 kfree(request);
1729 return ret;
1730 }
1731
1732 seqno = request->seqno;
Daniel Vettere35a41d2010-02-11 22:13:59 +01001733 }
1734
Chris Wilson78501ea2010-10-27 12:18:21 +01001735 if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) {
Chris Wilsondb53a302011-02-03 11:57:46 +00001736 if (HAS_PCH_SPLIT(ring->dev))
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001737 ier = I915_READ(DEIER) | I915_READ(GTIER);
1738 else
1739 ier = I915_READ(IER);
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001740 if (!ier) {
1741 DRM_ERROR("something (likely vbetool) disabled "
1742 "interrupts, re-enabling\n");
Chris Wilsonf01c22f2011-06-28 11:48:51 +01001743 ring->dev->driver->irq_preinstall(ring->dev);
1744 ring->dev->driver->irq_postinstall(ring->dev);
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001745 }
1746
Chris Wilsondb53a302011-02-03 11:57:46 +00001747 trace_i915_gem_request_wait_begin(ring, seqno);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001748
Chris Wilsonb2223492010-10-27 15:27:33 +01001749 ring->waiting_seqno = seqno;
Chris Wilsonb13c2b92010-12-13 16:54:50 +00001750 if (ring->irq_get(ring)) {
Chris Wilsonce453d82011-02-21 14:43:56 +00001751 if (dev_priv->mm.interruptible)
Chris Wilsonb13c2b92010-12-13 16:54:50 +00001752 ret = wait_event_interruptible(ring->irq_queue,
1753 i915_seqno_passed(ring->get_seqno(ring), seqno)
1754 || atomic_read(&dev_priv->mm.wedged));
1755 else
1756 wait_event(ring->irq_queue,
1757 i915_seqno_passed(ring->get_seqno(ring), seqno)
1758 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02001759
Chris Wilsonb13c2b92010-12-13 16:54:50 +00001760 ring->irq_put(ring);
Eric Anholte959b5d2011-12-22 14:55:01 -08001761 } else if (wait_for_atomic(i915_seqno_passed(ring->get_seqno(ring),
1762 seqno) ||
1763 atomic_read(&dev_priv->mm.wedged), 3000))
Chris Wilsonb5ba1772010-12-14 12:17:15 +00001764 ret = -EBUSY;
Chris Wilsonb2223492010-10-27 15:27:33 +01001765 ring->waiting_seqno = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001766
Chris Wilsondb53a302011-02-03 11:57:46 +00001767 trace_i915_gem_request_wait_end(ring, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07001768 }
Ben Gamariba1234d2009-09-14 17:48:47 -04001769 if (atomic_read(&dev_priv->mm.wedged))
Chris Wilson30dbf0c2010-09-25 10:19:17 +01001770 ret = -EAGAIN;
Eric Anholt673a3942008-07-30 12:06:12 -07001771
Eric Anholt673a3942008-07-30 12:06:12 -07001772 /* Directly dispatch request retiring. While we have the work queue
1773 * to handle this, the waiter on a request often wants an associated
1774 * buffer to have made it to the inactive list, and we would need
1775 * a separate wait queue to handle that.
1776 */
Ben Widawskyb93f9cf2012-01-25 15:39:34 -08001777 if (ret == 0 && do_retire)
Chris Wilsondb53a302011-02-03 11:57:46 +00001778 i915_gem_retire_requests_ring(ring);
Eric Anholt673a3942008-07-30 12:06:12 -07001779
1780 return ret;
1781}
1782
Daniel Vetter48764bf2009-09-15 22:57:32 +02001783/**
Eric Anholt673a3942008-07-30 12:06:12 -07001784 * Ensures that all rendering to the object has completed and the object is
1785 * safe to unbind from the GTT or access from the CPU.
1786 */
Chris Wilson54cf91d2010-11-25 18:00:26 +00001787int
Chris Wilsonce453d82011-02-21 14:43:56 +00001788i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07001789{
Eric Anholt673a3942008-07-30 12:06:12 -07001790 int ret;
1791
Eric Anholte47c68e2008-11-14 13:35:19 -08001792 /* This function only exists to support waiting for existing rendering,
1793 * not for emitting required flushes.
Eric Anholt673a3942008-07-30 12:06:12 -07001794 */
Chris Wilson05394f32010-11-08 19:18:58 +00001795 BUG_ON((obj->base.write_domain & I915_GEM_GPU_DOMAINS) != 0);
Eric Anholt673a3942008-07-30 12:06:12 -07001796
1797 /* If there is rendering queued on the buffer being evicted, wait for
1798 * it.
1799 */
Chris Wilson05394f32010-11-08 19:18:58 +00001800 if (obj->active) {
Ben Widawskyb93f9cf2012-01-25 15:39:34 -08001801 ret = i915_wait_request(obj->ring, obj->last_rendering_seqno,
1802 true);
Chris Wilson2cf34d72010-09-14 13:03:28 +01001803 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07001804 return ret;
1805 }
1806
1807 return 0;
1808}
1809
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01001810static void i915_gem_object_finish_gtt(struct drm_i915_gem_object *obj)
1811{
1812 u32 old_write_domain, old_read_domains;
1813
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01001814 /* Act a barrier for all accesses through the GTT */
1815 mb();
1816
1817 /* Force a pagefault for domain tracking on next user access */
1818 i915_gem_release_mmap(obj);
1819
Keith Packardb97c3d92011-06-24 21:02:59 -07001820 if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
1821 return;
1822
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01001823 old_read_domains = obj->base.read_domains;
1824 old_write_domain = obj->base.write_domain;
1825
1826 obj->base.read_domains &= ~I915_GEM_DOMAIN_GTT;
1827 obj->base.write_domain &= ~I915_GEM_DOMAIN_GTT;
1828
1829 trace_i915_gem_object_change_domain(obj,
1830 old_read_domains,
1831 old_write_domain);
1832}
1833
Eric Anholt673a3942008-07-30 12:06:12 -07001834/**
1835 * Unbinds an object from the GTT aperture.
1836 */
Jesse Barnes0f973f22009-01-26 17:10:45 -08001837int
Chris Wilson05394f32010-11-08 19:18:58 +00001838i915_gem_object_unbind(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07001839{
Daniel Vetter7bddb012012-02-09 17:15:47 +01001840 drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07001841 int ret = 0;
1842
Chris Wilson05394f32010-11-08 19:18:58 +00001843 if (obj->gtt_space == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07001844 return 0;
1845
Chris Wilson05394f32010-11-08 19:18:58 +00001846 if (obj->pin_count != 0) {
Eric Anholt673a3942008-07-30 12:06:12 -07001847 DRM_ERROR("Attempting to unbind pinned buffer\n");
1848 return -EINVAL;
1849 }
1850
Chris Wilsona8198ee2011-04-13 22:04:09 +01001851 ret = i915_gem_object_finish_gpu(obj);
Chris Wilson8dc17752010-07-23 23:18:51 +01001852 if (ret == -ERESTARTSYS)
Eric Anholt673a3942008-07-30 12:06:12 -07001853 return ret;
Chris Wilson8dc17752010-07-23 23:18:51 +01001854 /* Continue on if we fail due to EIO, the GPU is hung so we
1855 * should be safe and we need to cleanup or else we might
1856 * cause memory corruption through use-after-free.
1857 */
Chris Wilsona8198ee2011-04-13 22:04:09 +01001858
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01001859 i915_gem_object_finish_gtt(obj);
Chris Wilsona8198ee2011-04-13 22:04:09 +01001860
1861 /* Move the object to the CPU domain to ensure that
1862 * any possible CPU writes while it's not in the GTT
1863 * are flushed when we go to remap it.
1864 */
1865 if (ret == 0)
1866 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
1867 if (ret == -ERESTARTSYS)
1868 return ret;
Chris Wilson812ed4922010-09-30 15:08:57 +01001869 if (ret) {
Chris Wilsona8198ee2011-04-13 22:04:09 +01001870 /* In the event of a disaster, abandon all caches and
1871 * hope for the best.
1872 */
Chris Wilson812ed4922010-09-30 15:08:57 +01001873 i915_gem_clflush_object(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00001874 obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
Chris Wilson812ed4922010-09-30 15:08:57 +01001875 }
Eric Anholt673a3942008-07-30 12:06:12 -07001876
Daniel Vetter96b47b62009-12-15 17:50:00 +01001877 /* release the fence reg _after_ flushing */
Chris Wilsond9e86c02010-11-10 16:40:20 +00001878 ret = i915_gem_object_put_fence(obj);
1879 if (ret == -ERESTARTSYS)
1880 return ret;
Daniel Vetter96b47b62009-12-15 17:50:00 +01001881
Chris Wilsondb53a302011-02-03 11:57:46 +00001882 trace_i915_gem_object_unbind(obj);
1883
Daniel Vetter74898d72012-02-15 23:50:22 +01001884 if (obj->has_global_gtt_mapping)
1885 i915_gem_gtt_unbind_object(obj);
Daniel Vetter7bddb012012-02-09 17:15:47 +01001886 if (obj->has_aliasing_ppgtt_mapping) {
1887 i915_ppgtt_unbind_object(dev_priv->mm.aliasing_ppgtt, obj);
1888 obj->has_aliasing_ppgtt_mapping = 0;
1889 }
Daniel Vetter74163902012-02-15 23:50:21 +01001890 i915_gem_gtt_finish_object(obj);
Daniel Vetter7bddb012012-02-09 17:15:47 +01001891
Chris Wilsone5281cc2010-10-28 13:45:36 +01001892 i915_gem_object_put_pages_gtt(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001893
Chris Wilson6299f992010-11-24 12:23:44 +00001894 list_del_init(&obj->gtt_list);
Chris Wilson05394f32010-11-08 19:18:58 +00001895 list_del_init(&obj->mm_list);
Daniel Vetter75e9e912010-11-04 17:11:09 +01001896 /* Avoid an unnecessary call to unbind on rebind. */
Chris Wilson05394f32010-11-08 19:18:58 +00001897 obj->map_and_fenceable = true;
Eric Anholt673a3942008-07-30 12:06:12 -07001898
Chris Wilson05394f32010-11-08 19:18:58 +00001899 drm_mm_put_block(obj->gtt_space);
1900 obj->gtt_space = NULL;
1901 obj->gtt_offset = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001902
Chris Wilson05394f32010-11-08 19:18:58 +00001903 if (i915_gem_object_is_purgeable(obj))
Chris Wilson963b4832009-09-20 23:03:54 +01001904 i915_gem_object_truncate(obj);
1905
Chris Wilson8dc17752010-07-23 23:18:51 +01001906 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001907}
1908
Chris Wilson88241782011-01-07 17:09:48 +00001909int
Chris Wilsondb53a302011-02-03 11:57:46 +00001910i915_gem_flush_ring(struct intel_ring_buffer *ring,
Chris Wilson54cf91d2010-11-25 18:00:26 +00001911 uint32_t invalidate_domains,
1912 uint32_t flush_domains)
1913{
Chris Wilson88241782011-01-07 17:09:48 +00001914 int ret;
1915
Chris Wilson36d527d2011-03-19 22:26:49 +00001916 if (((invalidate_domains | flush_domains) & I915_GEM_GPU_DOMAINS) == 0)
1917 return 0;
1918
Chris Wilsondb53a302011-02-03 11:57:46 +00001919 trace_i915_gem_ring_flush(ring, invalidate_domains, flush_domains);
1920
Chris Wilson88241782011-01-07 17:09:48 +00001921 ret = ring->flush(ring, invalidate_domains, flush_domains);
1922 if (ret)
1923 return ret;
1924
Chris Wilson36d527d2011-03-19 22:26:49 +00001925 if (flush_domains & I915_GEM_GPU_DOMAINS)
1926 i915_gem_process_flushing_list(ring, flush_domains);
1927
Chris Wilson88241782011-01-07 17:09:48 +00001928 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001929}
1930
Ben Widawskyb93f9cf2012-01-25 15:39:34 -08001931static int i915_ring_idle(struct intel_ring_buffer *ring, bool do_retire)
Chris Wilsona56ba562010-09-28 10:07:56 +01001932{
Chris Wilson88241782011-01-07 17:09:48 +00001933 int ret;
1934
Chris Wilson395b70b2010-10-28 21:28:46 +01001935 if (list_empty(&ring->gpu_write_list) && list_empty(&ring->active_list))
Chris Wilson64193402010-10-24 12:38:05 +01001936 return 0;
1937
Chris Wilson88241782011-01-07 17:09:48 +00001938 if (!list_empty(&ring->gpu_write_list)) {
Chris Wilsondb53a302011-02-03 11:57:46 +00001939 ret = i915_gem_flush_ring(ring,
Chris Wilson0ac74c62010-12-06 14:36:02 +00001940 I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
Chris Wilson88241782011-01-07 17:09:48 +00001941 if (ret)
1942 return ret;
1943 }
1944
Ben Widawskyb93f9cf2012-01-25 15:39:34 -08001945 return i915_wait_request(ring, i915_gem_next_request_seqno(ring),
1946 do_retire);
Chris Wilsona56ba562010-09-28 10:07:56 +01001947}
1948
Ben Widawskyb93f9cf2012-01-25 15:39:34 -08001949int i915_gpu_idle(struct drm_device *dev, bool do_retire)
Daniel Vetter4df2faf2010-02-19 11:52:00 +01001950{
1951 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001952 int ret, i;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01001953
Daniel Vetter4df2faf2010-02-19 11:52:00 +01001954 /* Flush everything onto the inactive list. */
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001955 for (i = 0; i < I915_NUM_RINGS; i++) {
Ben Widawskyb93f9cf2012-01-25 15:39:34 -08001956 ret = i915_ring_idle(&dev_priv->ring[i], do_retire);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001957 if (ret)
1958 return ret;
1959 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08001960
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001961 return 0;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01001962}
1963
Daniel Vetterc6642782010-11-12 13:46:18 +00001964static int sandybridge_write_fence_reg(struct drm_i915_gem_object *obj,
1965 struct intel_ring_buffer *pipelined)
Eric Anholt4e901fd2009-10-26 16:44:17 -07001966{
Chris Wilson05394f32010-11-08 19:18:58 +00001967 struct drm_device *dev = obj->base.dev;
Eric Anholt4e901fd2009-10-26 16:44:17 -07001968 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00001969 u32 size = obj->gtt_space->size;
1970 int regnum = obj->fence_reg;
Eric Anholt4e901fd2009-10-26 16:44:17 -07001971 uint64_t val;
1972
Chris Wilson05394f32010-11-08 19:18:58 +00001973 val = (uint64_t)((obj->gtt_offset + size - 4096) &
Daniel Vetterc6642782010-11-12 13:46:18 +00001974 0xfffff000) << 32;
Chris Wilson05394f32010-11-08 19:18:58 +00001975 val |= obj->gtt_offset & 0xfffff000;
1976 val |= (uint64_t)((obj->stride / 128) - 1) <<
Eric Anholt4e901fd2009-10-26 16:44:17 -07001977 SANDYBRIDGE_FENCE_PITCH_SHIFT;
1978
Chris Wilson05394f32010-11-08 19:18:58 +00001979 if (obj->tiling_mode == I915_TILING_Y)
Eric Anholt4e901fd2009-10-26 16:44:17 -07001980 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
1981 val |= I965_FENCE_REG_VALID;
1982
Daniel Vetterc6642782010-11-12 13:46:18 +00001983 if (pipelined) {
1984 int ret = intel_ring_begin(pipelined, 6);
1985 if (ret)
1986 return ret;
1987
1988 intel_ring_emit(pipelined, MI_NOOP);
1989 intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(2));
1990 intel_ring_emit(pipelined, FENCE_REG_SANDYBRIDGE_0 + regnum*8);
1991 intel_ring_emit(pipelined, (u32)val);
1992 intel_ring_emit(pipelined, FENCE_REG_SANDYBRIDGE_0 + regnum*8 + 4);
1993 intel_ring_emit(pipelined, (u32)(val >> 32));
1994 intel_ring_advance(pipelined);
1995 } else
1996 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + regnum * 8, val);
1997
1998 return 0;
Eric Anholt4e901fd2009-10-26 16:44:17 -07001999}
2000
Daniel Vetterc6642782010-11-12 13:46:18 +00002001static int i965_write_fence_reg(struct drm_i915_gem_object *obj,
2002 struct intel_ring_buffer *pipelined)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002003{
Chris Wilson05394f32010-11-08 19:18:58 +00002004 struct drm_device *dev = obj->base.dev;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002005 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00002006 u32 size = obj->gtt_space->size;
2007 int regnum = obj->fence_reg;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002008 uint64_t val;
2009
Chris Wilson05394f32010-11-08 19:18:58 +00002010 val = (uint64_t)((obj->gtt_offset + size - 4096) &
Jesse Barnesde151cf2008-11-12 10:03:55 -08002011 0xfffff000) << 32;
Chris Wilson05394f32010-11-08 19:18:58 +00002012 val |= obj->gtt_offset & 0xfffff000;
2013 val |= ((obj->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
2014 if (obj->tiling_mode == I915_TILING_Y)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002015 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2016 val |= I965_FENCE_REG_VALID;
2017
Daniel Vetterc6642782010-11-12 13:46:18 +00002018 if (pipelined) {
2019 int ret = intel_ring_begin(pipelined, 6);
2020 if (ret)
2021 return ret;
2022
2023 intel_ring_emit(pipelined, MI_NOOP);
2024 intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(2));
2025 intel_ring_emit(pipelined, FENCE_REG_965_0 + regnum*8);
2026 intel_ring_emit(pipelined, (u32)val);
2027 intel_ring_emit(pipelined, FENCE_REG_965_0 + regnum*8 + 4);
2028 intel_ring_emit(pipelined, (u32)(val >> 32));
2029 intel_ring_advance(pipelined);
2030 } else
2031 I915_WRITE64(FENCE_REG_965_0 + regnum * 8, val);
2032
2033 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002034}
2035
Daniel Vetterc6642782010-11-12 13:46:18 +00002036static int i915_write_fence_reg(struct drm_i915_gem_object *obj,
2037 struct intel_ring_buffer *pipelined)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002038{
Chris Wilson05394f32010-11-08 19:18:58 +00002039 struct drm_device *dev = obj->base.dev;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002040 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00002041 u32 size = obj->gtt_space->size;
Daniel Vetterc6642782010-11-12 13:46:18 +00002042 u32 fence_reg, val, pitch_val;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002043 int tile_width;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002044
Daniel Vetterc6642782010-11-12 13:46:18 +00002045 if (WARN((obj->gtt_offset & ~I915_FENCE_START_MASK) ||
2046 (size & -size) != size ||
2047 (obj->gtt_offset & (size - 1)),
2048 "object 0x%08x [fenceable? %d] not 1M or pot-size (0x%08x) aligned\n",
2049 obj->gtt_offset, obj->map_and_fenceable, size))
2050 return -EINVAL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002051
Daniel Vetterc6642782010-11-12 13:46:18 +00002052 if (obj->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev))
Jesse Barnes0f973f22009-01-26 17:10:45 -08002053 tile_width = 128;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002054 else
Jesse Barnes0f973f22009-01-26 17:10:45 -08002055 tile_width = 512;
2056
2057 /* Note: pitch better be a power of two tile widths */
Chris Wilson05394f32010-11-08 19:18:58 +00002058 pitch_val = obj->stride / tile_width;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002059 pitch_val = ffs(pitch_val) - 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002060
Chris Wilson05394f32010-11-08 19:18:58 +00002061 val = obj->gtt_offset;
2062 if (obj->tiling_mode == I915_TILING_Y)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002063 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002064 val |= I915_FENCE_SIZE_BITS(size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002065 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2066 val |= I830_FENCE_REG_VALID;
2067
Chris Wilson05394f32010-11-08 19:18:58 +00002068 fence_reg = obj->fence_reg;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002069 if (fence_reg < 8)
2070 fence_reg = FENCE_REG_830_0 + fence_reg * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002071 else
Chris Wilsona00b10c2010-09-24 21:15:47 +01002072 fence_reg = FENCE_REG_945_8 + (fence_reg - 8) * 4;
Daniel Vetterc6642782010-11-12 13:46:18 +00002073
2074 if (pipelined) {
2075 int ret = intel_ring_begin(pipelined, 4);
2076 if (ret)
2077 return ret;
2078
2079 intel_ring_emit(pipelined, MI_NOOP);
2080 intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(1));
2081 intel_ring_emit(pipelined, fence_reg);
2082 intel_ring_emit(pipelined, val);
2083 intel_ring_advance(pipelined);
2084 } else
2085 I915_WRITE(fence_reg, val);
2086
2087 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002088}
2089
Daniel Vetterc6642782010-11-12 13:46:18 +00002090static int i830_write_fence_reg(struct drm_i915_gem_object *obj,
2091 struct intel_ring_buffer *pipelined)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002092{
Chris Wilson05394f32010-11-08 19:18:58 +00002093 struct drm_device *dev = obj->base.dev;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002094 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00002095 u32 size = obj->gtt_space->size;
2096 int regnum = obj->fence_reg;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002097 uint32_t val;
2098 uint32_t pitch_val;
2099
Daniel Vetterc6642782010-11-12 13:46:18 +00002100 if (WARN((obj->gtt_offset & ~I830_FENCE_START_MASK) ||
2101 (size & -size) != size ||
2102 (obj->gtt_offset & (size - 1)),
2103 "object 0x%08x not 512K or pot-size 0x%08x aligned\n",
2104 obj->gtt_offset, size))
2105 return -EINVAL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002106
Chris Wilson05394f32010-11-08 19:18:58 +00002107 pitch_val = obj->stride / 128;
Eric Anholte76a16d2009-05-26 17:44:56 -07002108 pitch_val = ffs(pitch_val) - 1;
Eric Anholte76a16d2009-05-26 17:44:56 -07002109
Chris Wilson05394f32010-11-08 19:18:58 +00002110 val = obj->gtt_offset;
2111 if (obj->tiling_mode == I915_TILING_Y)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002112 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Daniel Vetterc6642782010-11-12 13:46:18 +00002113 val |= I830_FENCE_SIZE_BITS(size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002114 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2115 val |= I830_FENCE_REG_VALID;
2116
Daniel Vetterc6642782010-11-12 13:46:18 +00002117 if (pipelined) {
2118 int ret = intel_ring_begin(pipelined, 4);
2119 if (ret)
2120 return ret;
2121
2122 intel_ring_emit(pipelined, MI_NOOP);
2123 intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(1));
2124 intel_ring_emit(pipelined, FENCE_REG_830_0 + regnum*4);
2125 intel_ring_emit(pipelined, val);
2126 intel_ring_advance(pipelined);
2127 } else
2128 I915_WRITE(FENCE_REG_830_0 + regnum * 4, val);
2129
2130 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002131}
2132
Chris Wilsond9e86c02010-11-10 16:40:20 +00002133static bool ring_passed_seqno(struct intel_ring_buffer *ring, u32 seqno)
2134{
2135 return i915_seqno_passed(ring->get_seqno(ring), seqno);
2136}
2137
2138static int
2139i915_gem_object_flush_fence(struct drm_i915_gem_object *obj,
Chris Wilsonce453d82011-02-21 14:43:56 +00002140 struct intel_ring_buffer *pipelined)
Chris Wilsond9e86c02010-11-10 16:40:20 +00002141{
2142 int ret;
2143
2144 if (obj->fenced_gpu_access) {
Chris Wilson88241782011-01-07 17:09:48 +00002145 if (obj->base.write_domain & I915_GEM_GPU_DOMAINS) {
Chris Wilsondb53a302011-02-03 11:57:46 +00002146 ret = i915_gem_flush_ring(obj->last_fenced_ring,
Chris Wilson88241782011-01-07 17:09:48 +00002147 0, obj->base.write_domain);
2148 if (ret)
2149 return ret;
2150 }
Chris Wilsond9e86c02010-11-10 16:40:20 +00002151
2152 obj->fenced_gpu_access = false;
2153 }
2154
2155 if (obj->last_fenced_seqno && pipelined != obj->last_fenced_ring) {
2156 if (!ring_passed_seqno(obj->last_fenced_ring,
2157 obj->last_fenced_seqno)) {
Chris Wilsondb53a302011-02-03 11:57:46 +00002158 ret = i915_wait_request(obj->last_fenced_ring,
Ben Widawskyb93f9cf2012-01-25 15:39:34 -08002159 obj->last_fenced_seqno,
2160 true);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002161 if (ret)
2162 return ret;
2163 }
2164
2165 obj->last_fenced_seqno = 0;
2166 obj->last_fenced_ring = NULL;
2167 }
2168
Chris Wilson63256ec2011-01-04 18:42:07 +00002169 /* Ensure that all CPU reads are completed before installing a fence
2170 * and all writes before removing the fence.
2171 */
2172 if (obj->base.read_domains & I915_GEM_DOMAIN_GTT)
2173 mb();
2174
Chris Wilsond9e86c02010-11-10 16:40:20 +00002175 return 0;
2176}
2177
2178int
2179i915_gem_object_put_fence(struct drm_i915_gem_object *obj)
2180{
2181 int ret;
2182
2183 if (obj->tiling_mode)
2184 i915_gem_release_mmap(obj);
2185
Chris Wilsonce453d82011-02-21 14:43:56 +00002186 ret = i915_gem_object_flush_fence(obj, NULL);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002187 if (ret)
2188 return ret;
2189
2190 if (obj->fence_reg != I915_FENCE_REG_NONE) {
2191 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002192
2193 WARN_ON(dev_priv->fence_regs[obj->fence_reg].pin_count);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002194 i915_gem_clear_fence_reg(obj->base.dev,
2195 &dev_priv->fence_regs[obj->fence_reg]);
2196
2197 obj->fence_reg = I915_FENCE_REG_NONE;
2198 }
2199
2200 return 0;
2201}
2202
2203static struct drm_i915_fence_reg *
2204i915_find_fence_reg(struct drm_device *dev,
2205 struct intel_ring_buffer *pipelined)
Daniel Vetterae3db242010-02-19 11:51:58 +01002206{
Daniel Vetterae3db242010-02-19 11:51:58 +01002207 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002208 struct drm_i915_fence_reg *reg, *first, *avail;
2209 int i;
Daniel Vetterae3db242010-02-19 11:51:58 +01002210
2211 /* First try to find a free reg */
Chris Wilsond9e86c02010-11-10 16:40:20 +00002212 avail = NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002213 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2214 reg = &dev_priv->fence_regs[i];
2215 if (!reg->obj)
Chris Wilsond9e86c02010-11-10 16:40:20 +00002216 return reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01002217
Chris Wilson1690e1e2011-12-14 13:57:08 +01002218 if (!reg->pin_count)
Chris Wilsond9e86c02010-11-10 16:40:20 +00002219 avail = reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01002220 }
2221
Chris Wilsond9e86c02010-11-10 16:40:20 +00002222 if (avail == NULL)
2223 return NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002224
2225 /* None available, try to steal one or wait for a user to finish */
Chris Wilsond9e86c02010-11-10 16:40:20 +00002226 avail = first = NULL;
2227 list_for_each_entry(reg, &dev_priv->mm.fence_list, lru_list) {
Chris Wilson1690e1e2011-12-14 13:57:08 +01002228 if (reg->pin_count)
Daniel Vetterae3db242010-02-19 11:51:58 +01002229 continue;
2230
Chris Wilsond9e86c02010-11-10 16:40:20 +00002231 if (first == NULL)
2232 first = reg;
2233
2234 if (!pipelined ||
2235 !reg->obj->last_fenced_ring ||
2236 reg->obj->last_fenced_ring == pipelined) {
2237 avail = reg;
2238 break;
2239 }
Daniel Vetterae3db242010-02-19 11:51:58 +01002240 }
2241
Chris Wilsond9e86c02010-11-10 16:40:20 +00002242 if (avail == NULL)
2243 avail = first;
Daniel Vetterae3db242010-02-19 11:51:58 +01002244
Chris Wilsona00b10c2010-09-24 21:15:47 +01002245 return avail;
Daniel Vetterae3db242010-02-19 11:51:58 +01002246}
2247
Jesse Barnesde151cf2008-11-12 10:03:55 -08002248/**
Chris Wilsond9e86c02010-11-10 16:40:20 +00002249 * i915_gem_object_get_fence - set up a fence reg for an object
Jesse Barnesde151cf2008-11-12 10:03:55 -08002250 * @obj: object to map through a fence reg
Chris Wilsond9e86c02010-11-10 16:40:20 +00002251 * @pipelined: ring on which to queue the change, or NULL for CPU access
2252 * @interruptible: must we wait uninterruptibly for the register to retire?
Jesse Barnesde151cf2008-11-12 10:03:55 -08002253 *
2254 * When mapping objects through the GTT, userspace wants to be able to write
2255 * to them without having to worry about swizzling if the object is tiled.
2256 *
2257 * This function walks the fence regs looking for a free one for @obj,
2258 * stealing one if it can't find any.
2259 *
2260 * It then sets up the reg based on the object's properties: address, pitch
2261 * and tiling format.
2262 */
Chris Wilson8c4b8c32009-06-17 22:08:52 +01002263int
Chris Wilsond9e86c02010-11-10 16:40:20 +00002264i915_gem_object_get_fence(struct drm_i915_gem_object *obj,
Chris Wilsonce453d82011-02-21 14:43:56 +00002265 struct intel_ring_buffer *pipelined)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002266{
Chris Wilson05394f32010-11-08 19:18:58 +00002267 struct drm_device *dev = obj->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002268 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002269 struct drm_i915_fence_reg *reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01002270 int ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002271
Chris Wilson6bda10d2010-12-05 21:04:18 +00002272 /* XXX disable pipelining. There are bugs. Shocking. */
2273 pipelined = NULL;
2274
Chris Wilsond9e86c02010-11-10 16:40:20 +00002275 /* Just update our place in the LRU if our fence is getting reused. */
Chris Wilson05394f32010-11-08 19:18:58 +00002276 if (obj->fence_reg != I915_FENCE_REG_NONE) {
2277 reg = &dev_priv->fence_regs[obj->fence_reg];
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002278 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002279
Chris Wilson29c5a582011-03-17 15:23:22 +00002280 if (obj->tiling_changed) {
2281 ret = i915_gem_object_flush_fence(obj, pipelined);
2282 if (ret)
2283 return ret;
2284
2285 if (!obj->fenced_gpu_access && !obj->last_fenced_seqno)
2286 pipelined = NULL;
2287
2288 if (pipelined) {
2289 reg->setup_seqno =
2290 i915_gem_next_request_seqno(pipelined);
2291 obj->last_fenced_seqno = reg->setup_seqno;
2292 obj->last_fenced_ring = pipelined;
2293 }
2294
2295 goto update;
2296 }
Chris Wilsond9e86c02010-11-10 16:40:20 +00002297
2298 if (!pipelined) {
2299 if (reg->setup_seqno) {
2300 if (!ring_passed_seqno(obj->last_fenced_ring,
2301 reg->setup_seqno)) {
Chris Wilsondb53a302011-02-03 11:57:46 +00002302 ret = i915_wait_request(obj->last_fenced_ring,
Ben Widawskyb93f9cf2012-01-25 15:39:34 -08002303 reg->setup_seqno,
2304 true);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002305 if (ret)
2306 return ret;
2307 }
2308
2309 reg->setup_seqno = 0;
2310 }
2311 } else if (obj->last_fenced_ring &&
2312 obj->last_fenced_ring != pipelined) {
Chris Wilsonce453d82011-02-21 14:43:56 +00002313 ret = i915_gem_object_flush_fence(obj, pipelined);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002314 if (ret)
2315 return ret;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002316 }
2317
Eric Anholta09ba7f2009-08-29 12:49:51 -07002318 return 0;
2319 }
2320
Chris Wilsond9e86c02010-11-10 16:40:20 +00002321 reg = i915_find_fence_reg(dev, pipelined);
2322 if (reg == NULL)
Daniel Vetter39965b32011-12-14 13:57:09 +01002323 return -EDEADLK;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002324
Chris Wilsonce453d82011-02-21 14:43:56 +00002325 ret = i915_gem_object_flush_fence(obj, pipelined);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002326 if (ret)
Daniel Vetterae3db242010-02-19 11:51:58 +01002327 return ret;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002328
Chris Wilsond9e86c02010-11-10 16:40:20 +00002329 if (reg->obj) {
2330 struct drm_i915_gem_object *old = reg->obj;
2331
2332 drm_gem_object_reference(&old->base);
2333
2334 if (old->tiling_mode)
2335 i915_gem_release_mmap(old);
2336
Chris Wilsonce453d82011-02-21 14:43:56 +00002337 ret = i915_gem_object_flush_fence(old, pipelined);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002338 if (ret) {
2339 drm_gem_object_unreference(&old->base);
2340 return ret;
2341 }
2342
2343 if (old->last_fenced_seqno == 0 && obj->last_fenced_seqno == 0)
2344 pipelined = NULL;
2345
2346 old->fence_reg = I915_FENCE_REG_NONE;
2347 old->last_fenced_ring = pipelined;
2348 old->last_fenced_seqno =
Chris Wilsondb53a302011-02-03 11:57:46 +00002349 pipelined ? i915_gem_next_request_seqno(pipelined) : 0;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002350
2351 drm_gem_object_unreference(&old->base);
2352 } else if (obj->last_fenced_seqno == 0)
2353 pipelined = NULL;
Eric Anholta09ba7f2009-08-29 12:49:51 -07002354
Jesse Barnesde151cf2008-11-12 10:03:55 -08002355 reg->obj = obj;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002356 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
2357 obj->fence_reg = reg - dev_priv->fence_regs;
2358 obj->last_fenced_ring = pipelined;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002359
Chris Wilsond9e86c02010-11-10 16:40:20 +00002360 reg->setup_seqno =
Chris Wilsondb53a302011-02-03 11:57:46 +00002361 pipelined ? i915_gem_next_request_seqno(pipelined) : 0;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002362 obj->last_fenced_seqno = reg->setup_seqno;
2363
2364update:
2365 obj->tiling_changed = false;
Chris Wilsone259bef2010-09-17 00:32:02 +01002366 switch (INTEL_INFO(dev)->gen) {
Eric Anholt25aebfc32011-05-06 13:55:53 -07002367 case 7:
Chris Wilsone259bef2010-09-17 00:32:02 +01002368 case 6:
Daniel Vetterc6642782010-11-12 13:46:18 +00002369 ret = sandybridge_write_fence_reg(obj, pipelined);
Chris Wilsone259bef2010-09-17 00:32:02 +01002370 break;
2371 case 5:
2372 case 4:
Daniel Vetterc6642782010-11-12 13:46:18 +00002373 ret = i965_write_fence_reg(obj, pipelined);
Chris Wilsone259bef2010-09-17 00:32:02 +01002374 break;
2375 case 3:
Daniel Vetterc6642782010-11-12 13:46:18 +00002376 ret = i915_write_fence_reg(obj, pipelined);
Chris Wilsone259bef2010-09-17 00:32:02 +01002377 break;
2378 case 2:
Daniel Vetterc6642782010-11-12 13:46:18 +00002379 ret = i830_write_fence_reg(obj, pipelined);
Chris Wilsone259bef2010-09-17 00:32:02 +01002380 break;
2381 }
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002382
Daniel Vetterc6642782010-11-12 13:46:18 +00002383 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002384}
2385
2386/**
2387 * i915_gem_clear_fence_reg - clear out fence register info
2388 * @obj: object to clear
2389 *
2390 * Zeroes out the fence register itself and clears out the associated
Chris Wilson05394f32010-11-08 19:18:58 +00002391 * data structures in dev_priv and obj.
Jesse Barnesde151cf2008-11-12 10:03:55 -08002392 */
2393static void
Chris Wilsond9e86c02010-11-10 16:40:20 +00002394i915_gem_clear_fence_reg(struct drm_device *dev,
2395 struct drm_i915_fence_reg *reg)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002396{
Jesse Barnes79e53942008-11-07 14:24:08 -08002397 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002398 uint32_t fence_reg = reg - dev_priv->fence_regs;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002399
Chris Wilsone259bef2010-09-17 00:32:02 +01002400 switch (INTEL_INFO(dev)->gen) {
Eric Anholt25aebfc32011-05-06 13:55:53 -07002401 case 7:
Chris Wilsone259bef2010-09-17 00:32:02 +01002402 case 6:
Chris Wilsond9e86c02010-11-10 16:40:20 +00002403 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + fence_reg*8, 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002404 break;
2405 case 5:
2406 case 4:
Chris Wilsond9e86c02010-11-10 16:40:20 +00002407 I915_WRITE64(FENCE_REG_965_0 + fence_reg*8, 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002408 break;
2409 case 3:
Chris Wilsond9e86c02010-11-10 16:40:20 +00002410 if (fence_reg >= 8)
2411 fence_reg = FENCE_REG_945_8 + (fence_reg - 8) * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002412 else
Chris Wilsone259bef2010-09-17 00:32:02 +01002413 case 2:
Chris Wilsond9e86c02010-11-10 16:40:20 +00002414 fence_reg = FENCE_REG_830_0 + fence_reg * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002415
2416 I915_WRITE(fence_reg, 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002417 break;
Eric Anholtdc529a42009-03-10 22:34:49 -07002418 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002419
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002420 list_del_init(&reg->lru_list);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002421 reg->obj = NULL;
2422 reg->setup_seqno = 0;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002423 reg->pin_count = 0;
Chris Wilson52dc7d32009-06-06 09:46:01 +01002424}
2425
2426/**
Eric Anholt673a3942008-07-30 12:06:12 -07002427 * Finds free space in the GTT aperture and binds the object there.
2428 */
2429static int
Chris Wilson05394f32010-11-08 19:18:58 +00002430i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
Daniel Vetter920afa72010-09-16 17:54:23 +02002431 unsigned alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01002432 bool map_and_fenceable)
Eric Anholt673a3942008-07-30 12:06:12 -07002433{
Chris Wilson05394f32010-11-08 19:18:58 +00002434 struct drm_device *dev = obj->base.dev;
Eric Anholt673a3942008-07-30 12:06:12 -07002435 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07002436 struct drm_mm_node *free_space;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002437 gfp_t gfpmask = __GFP_NORETRY | __GFP_NOWARN;
Daniel Vetter5e783302010-11-14 22:32:36 +01002438 u32 size, fence_size, fence_alignment, unfenced_alignment;
Daniel Vetter75e9e912010-11-04 17:11:09 +01002439 bool mappable, fenceable;
Chris Wilson07f73f62009-09-14 16:50:30 +01002440 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002441
Chris Wilson05394f32010-11-08 19:18:58 +00002442 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01002443 DRM_ERROR("Attempting to bind a purgeable object\n");
2444 return -EINVAL;
2445 }
2446
Chris Wilsone28f8712011-07-18 13:11:49 -07002447 fence_size = i915_gem_get_gtt_size(dev,
2448 obj->base.size,
2449 obj->tiling_mode);
2450 fence_alignment = i915_gem_get_gtt_alignment(dev,
2451 obj->base.size,
2452 obj->tiling_mode);
2453 unfenced_alignment =
2454 i915_gem_get_unfenced_gtt_alignment(dev,
2455 obj->base.size,
2456 obj->tiling_mode);
Chris Wilsona00b10c2010-09-24 21:15:47 +01002457
Eric Anholt673a3942008-07-30 12:06:12 -07002458 if (alignment == 0)
Daniel Vetter5e783302010-11-14 22:32:36 +01002459 alignment = map_and_fenceable ? fence_alignment :
2460 unfenced_alignment;
Daniel Vetter75e9e912010-11-04 17:11:09 +01002461 if (map_and_fenceable && alignment & (fence_alignment - 1)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002462 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2463 return -EINVAL;
2464 }
2465
Chris Wilson05394f32010-11-08 19:18:58 +00002466 size = map_and_fenceable ? fence_size : obj->base.size;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002467
Chris Wilson654fc602010-05-27 13:18:21 +01002468 /* If the object is bigger than the entire aperture, reject it early
2469 * before evicting everything in a vain attempt to find space.
2470 */
Chris Wilson05394f32010-11-08 19:18:58 +00002471 if (obj->base.size >
Daniel Vetter75e9e912010-11-04 17:11:09 +01002472 (map_and_fenceable ? dev_priv->mm.gtt_mappable_end : dev_priv->mm.gtt_total)) {
Chris Wilson654fc602010-05-27 13:18:21 +01002473 DRM_ERROR("Attempting to bind an object larger than the aperture\n");
2474 return -E2BIG;
2475 }
2476
Eric Anholt673a3942008-07-30 12:06:12 -07002477 search_free:
Daniel Vetter75e9e912010-11-04 17:11:09 +01002478 if (map_and_fenceable)
Daniel Vetter920afa72010-09-16 17:54:23 +02002479 free_space =
2480 drm_mm_search_free_in_range(&dev_priv->mm.gtt_space,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002481 size, alignment, 0,
Daniel Vetter920afa72010-09-16 17:54:23 +02002482 dev_priv->mm.gtt_mappable_end,
2483 0);
2484 else
2485 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002486 size, alignment, 0);
Daniel Vetter920afa72010-09-16 17:54:23 +02002487
2488 if (free_space != NULL) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01002489 if (map_and_fenceable)
Chris Wilson05394f32010-11-08 19:18:58 +00002490 obj->gtt_space =
Daniel Vetter920afa72010-09-16 17:54:23 +02002491 drm_mm_get_block_range_generic(free_space,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002492 size, alignment, 0,
Daniel Vetter920afa72010-09-16 17:54:23 +02002493 dev_priv->mm.gtt_mappable_end,
2494 0);
2495 else
Chris Wilson05394f32010-11-08 19:18:58 +00002496 obj->gtt_space =
Chris Wilsona00b10c2010-09-24 21:15:47 +01002497 drm_mm_get_block(free_space, size, alignment);
Daniel Vetter920afa72010-09-16 17:54:23 +02002498 }
Chris Wilson05394f32010-11-08 19:18:58 +00002499 if (obj->gtt_space == NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -07002500 /* If the gtt is empty and we're still having trouble
2501 * fitting our object in, we're out of memory.
2502 */
Daniel Vetter75e9e912010-11-04 17:11:09 +01002503 ret = i915_gem_evict_something(dev, size, alignment,
2504 map_and_fenceable);
Chris Wilson97311292009-09-21 00:22:34 +01002505 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002506 return ret;
Chris Wilson97311292009-09-21 00:22:34 +01002507
Eric Anholt673a3942008-07-30 12:06:12 -07002508 goto search_free;
2509 }
2510
Chris Wilsone5281cc2010-10-28 13:45:36 +01002511 ret = i915_gem_object_get_pages_gtt(obj, gfpmask);
Eric Anholt673a3942008-07-30 12:06:12 -07002512 if (ret) {
Chris Wilson05394f32010-11-08 19:18:58 +00002513 drm_mm_put_block(obj->gtt_space);
2514 obj->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002515
2516 if (ret == -ENOMEM) {
Chris Wilson809b6332011-01-10 17:33:15 +00002517 /* first try to reclaim some memory by clearing the GTT */
2518 ret = i915_gem_evict_everything(dev, false);
Chris Wilson07f73f62009-09-14 16:50:30 +01002519 if (ret) {
Chris Wilson07f73f62009-09-14 16:50:30 +01002520 /* now try to shrink everyone else */
Chris Wilson4bdadb92010-01-27 13:36:32 +00002521 if (gfpmask) {
2522 gfpmask = 0;
2523 goto search_free;
Chris Wilson07f73f62009-09-14 16:50:30 +01002524 }
2525
Chris Wilson809b6332011-01-10 17:33:15 +00002526 return -ENOMEM;
Chris Wilson07f73f62009-09-14 16:50:30 +01002527 }
2528
2529 goto search_free;
2530 }
2531
Eric Anholt673a3942008-07-30 12:06:12 -07002532 return ret;
2533 }
2534
Daniel Vetter74163902012-02-15 23:50:21 +01002535 ret = i915_gem_gtt_prepare_object(obj);
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002536 if (ret) {
Chris Wilsone5281cc2010-10-28 13:45:36 +01002537 i915_gem_object_put_pages_gtt(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00002538 drm_mm_put_block(obj->gtt_space);
2539 obj->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002540
Chris Wilson809b6332011-01-10 17:33:15 +00002541 if (i915_gem_evict_everything(dev, false))
Chris Wilson07f73f62009-09-14 16:50:30 +01002542 return ret;
Chris Wilson07f73f62009-09-14 16:50:30 +01002543
2544 goto search_free;
Eric Anholt673a3942008-07-30 12:06:12 -07002545 }
Daniel Vetter0ebb9822012-02-15 23:50:24 +01002546
2547 if (!dev_priv->mm.aliasing_ppgtt)
2548 i915_gem_gtt_bind_object(obj, obj->cache_level);
Eric Anholt673a3942008-07-30 12:06:12 -07002549
Chris Wilson6299f992010-11-24 12:23:44 +00002550 list_add_tail(&obj->gtt_list, &dev_priv->mm.gtt_list);
Chris Wilson05394f32010-11-08 19:18:58 +00002551 list_add_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
Chris Wilsonbf1a1092010-08-07 11:01:20 +01002552
Eric Anholt673a3942008-07-30 12:06:12 -07002553 /* Assert that the object is not currently in any GPU domain. As it
2554 * wasn't in the GTT, there shouldn't be any way it could have been in
2555 * a GPU cache
2556 */
Chris Wilson05394f32010-11-08 19:18:58 +00002557 BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
2558 BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
Eric Anholt673a3942008-07-30 12:06:12 -07002559
Chris Wilson6299f992010-11-24 12:23:44 +00002560 obj->gtt_offset = obj->gtt_space->start;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002561
Daniel Vetter75e9e912010-11-04 17:11:09 +01002562 fenceable =
Chris Wilson05394f32010-11-08 19:18:58 +00002563 obj->gtt_space->size == fence_size &&
Akshay Joshi0206e352011-08-16 15:34:10 -04002564 (obj->gtt_space->start & (fence_alignment - 1)) == 0;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002565
Daniel Vetter75e9e912010-11-04 17:11:09 +01002566 mappable =
Chris Wilson05394f32010-11-08 19:18:58 +00002567 obj->gtt_offset + obj->base.size <= dev_priv->mm.gtt_mappable_end;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002568
Chris Wilson05394f32010-11-08 19:18:58 +00002569 obj->map_and_fenceable = mappable && fenceable;
Daniel Vetter75e9e912010-11-04 17:11:09 +01002570
Chris Wilsondb53a302011-02-03 11:57:46 +00002571 trace_i915_gem_object_bind(obj, map_and_fenceable);
Eric Anholt673a3942008-07-30 12:06:12 -07002572 return 0;
2573}
2574
2575void
Chris Wilson05394f32010-11-08 19:18:58 +00002576i915_gem_clflush_object(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002577{
Eric Anholt673a3942008-07-30 12:06:12 -07002578 /* If we don't have a page list set up, then we're not pinned
2579 * to GPU, and we can ignore the cache flush because it'll happen
2580 * again at bind time.
2581 */
Chris Wilson05394f32010-11-08 19:18:58 +00002582 if (obj->pages == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002583 return;
2584
Chris Wilson9c23f7f2011-03-29 16:59:52 -07002585 /* If the GPU is snooping the contents of the CPU cache,
2586 * we do not need to manually clear the CPU cache lines. However,
2587 * the caches are only snooped when the render cache is
2588 * flushed/invalidated. As we always have to emit invalidations
2589 * and flushes when moving into and out of the RENDER domain, correct
2590 * snooping behaviour occurs naturally as the result of our domain
2591 * tracking.
2592 */
2593 if (obj->cache_level != I915_CACHE_NONE)
2594 return;
2595
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002596 trace_i915_gem_object_clflush(obj);
Eric Anholtcfa16a02009-05-26 18:46:16 -07002597
Chris Wilson05394f32010-11-08 19:18:58 +00002598 drm_clflush_pages(obj->pages, obj->base.size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002599}
2600
Eric Anholte47c68e2008-11-14 13:35:19 -08002601/** Flushes any GPU write domain for the object if it's dirty. */
Chris Wilson88241782011-01-07 17:09:48 +00002602static int
Chris Wilson3619df02010-11-28 15:37:17 +00002603i915_gem_object_flush_gpu_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08002604{
Chris Wilson05394f32010-11-08 19:18:58 +00002605 if ((obj->base.write_domain & I915_GEM_GPU_DOMAINS) == 0)
Chris Wilson88241782011-01-07 17:09:48 +00002606 return 0;
Eric Anholte47c68e2008-11-14 13:35:19 -08002607
2608 /* Queue the GPU write cache flushing we need. */
Chris Wilsondb53a302011-02-03 11:57:46 +00002609 return i915_gem_flush_ring(obj->ring, 0, obj->base.write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002610}
2611
2612/** Flushes the GTT write domain for the object if it's dirty. */
2613static void
Chris Wilson05394f32010-11-08 19:18:58 +00002614i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08002615{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002616 uint32_t old_write_domain;
2617
Chris Wilson05394f32010-11-08 19:18:58 +00002618 if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
Eric Anholte47c68e2008-11-14 13:35:19 -08002619 return;
2620
Chris Wilson63256ec2011-01-04 18:42:07 +00002621 /* No actual flushing is required for the GTT write domain. Writes
Eric Anholte47c68e2008-11-14 13:35:19 -08002622 * to it immediately go to main memory as far as we know, so there's
2623 * no chipset flush. It also doesn't land in render cache.
Chris Wilson63256ec2011-01-04 18:42:07 +00002624 *
2625 * However, we do have to enforce the order so that all writes through
2626 * the GTT land before any writes to the device, such as updates to
2627 * the GATT itself.
Eric Anholte47c68e2008-11-14 13:35:19 -08002628 */
Chris Wilson63256ec2011-01-04 18:42:07 +00002629 wmb();
2630
Chris Wilson05394f32010-11-08 19:18:58 +00002631 old_write_domain = obj->base.write_domain;
2632 obj->base.write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002633
2634 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00002635 obj->base.read_domains,
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002636 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002637}
2638
2639/** Flushes the CPU write domain for the object if it's dirty. */
2640static void
Chris Wilson05394f32010-11-08 19:18:58 +00002641i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08002642{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002643 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002644
Chris Wilson05394f32010-11-08 19:18:58 +00002645 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
Eric Anholte47c68e2008-11-14 13:35:19 -08002646 return;
2647
2648 i915_gem_clflush_object(obj);
Daniel Vetter40ce6572010-11-05 18:12:18 +01002649 intel_gtt_chipset_flush();
Chris Wilson05394f32010-11-08 19:18:58 +00002650 old_write_domain = obj->base.write_domain;
2651 obj->base.write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002652
2653 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00002654 obj->base.read_domains,
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002655 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002656}
2657
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002658/**
2659 * Moves a single object to the GTT read, and possibly write domain.
2660 *
2661 * This function returns when the move is complete, including waiting on
2662 * flushes to occur.
2663 */
Jesse Barnes79e53942008-11-07 14:24:08 -08002664int
Chris Wilson20217462010-11-23 15:26:33 +00002665i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002666{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002667 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002668 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002669
Eric Anholt02354392008-11-26 13:58:13 -08002670 /* Not valid to be called on unbound objects. */
Chris Wilson05394f32010-11-08 19:18:58 +00002671 if (obj->gtt_space == NULL)
Eric Anholt02354392008-11-26 13:58:13 -08002672 return -EINVAL;
2673
Chris Wilson8d7e3de2011-02-07 15:23:02 +00002674 if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
2675 return 0;
2676
Chris Wilson88241782011-01-07 17:09:48 +00002677 ret = i915_gem_object_flush_gpu_write_domain(obj);
2678 if (ret)
2679 return ret;
2680
Chris Wilson87ca9c82010-12-02 09:42:56 +00002681 if (obj->pending_gpu_write || write) {
Chris Wilsonce453d82011-02-21 14:43:56 +00002682 ret = i915_gem_object_wait_rendering(obj);
Chris Wilson87ca9c82010-12-02 09:42:56 +00002683 if (ret)
2684 return ret;
2685 }
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002686
Chris Wilson72133422010-09-13 23:56:38 +01002687 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002688
Chris Wilson05394f32010-11-08 19:18:58 +00002689 old_write_domain = obj->base.write_domain;
2690 old_read_domains = obj->base.read_domains;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002691
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002692 /* It should now be out of any other write domains, and we can update
2693 * the domain values for our changes.
2694 */
Chris Wilson05394f32010-11-08 19:18:58 +00002695 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2696 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002697 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00002698 obj->base.read_domains = I915_GEM_DOMAIN_GTT;
2699 obj->base.write_domain = I915_GEM_DOMAIN_GTT;
2700 obj->dirty = 1;
Eric Anholte47c68e2008-11-14 13:35:19 -08002701 }
2702
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002703 trace_i915_gem_object_change_domain(obj,
2704 old_read_domains,
2705 old_write_domain);
2706
Eric Anholte47c68e2008-11-14 13:35:19 -08002707 return 0;
2708}
2709
Chris Wilsone4ffd172011-04-04 09:44:39 +01002710int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
2711 enum i915_cache_level cache_level)
2712{
Daniel Vetter7bddb012012-02-09 17:15:47 +01002713 struct drm_device *dev = obj->base.dev;
2714 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsone4ffd172011-04-04 09:44:39 +01002715 int ret;
2716
2717 if (obj->cache_level == cache_level)
2718 return 0;
2719
2720 if (obj->pin_count) {
2721 DRM_DEBUG("can not change the cache level of pinned objects\n");
2722 return -EBUSY;
2723 }
2724
2725 if (obj->gtt_space) {
2726 ret = i915_gem_object_finish_gpu(obj);
2727 if (ret)
2728 return ret;
2729
2730 i915_gem_object_finish_gtt(obj);
2731
2732 /* Before SandyBridge, you could not use tiling or fence
2733 * registers with snooped memory, so relinquish any fences
2734 * currently pointing to our region in the aperture.
2735 */
2736 if (INTEL_INFO(obj->base.dev)->gen < 6) {
2737 ret = i915_gem_object_put_fence(obj);
2738 if (ret)
2739 return ret;
2740 }
2741
Daniel Vetter74898d72012-02-15 23:50:22 +01002742 if (obj->has_global_gtt_mapping)
2743 i915_gem_gtt_bind_object(obj, cache_level);
Daniel Vetter7bddb012012-02-09 17:15:47 +01002744 if (obj->has_aliasing_ppgtt_mapping)
2745 i915_ppgtt_bind_object(dev_priv->mm.aliasing_ppgtt,
2746 obj, cache_level);
Chris Wilsone4ffd172011-04-04 09:44:39 +01002747 }
2748
2749 if (cache_level == I915_CACHE_NONE) {
2750 u32 old_read_domains, old_write_domain;
2751
2752 /* If we're coming from LLC cached, then we haven't
2753 * actually been tracking whether the data is in the
2754 * CPU cache or not, since we only allow one bit set
2755 * in obj->write_domain and have been skipping the clflushes.
2756 * Just set it to the CPU cache for now.
2757 */
2758 WARN_ON(obj->base.write_domain & ~I915_GEM_DOMAIN_CPU);
2759 WARN_ON(obj->base.read_domains & ~I915_GEM_DOMAIN_CPU);
2760
2761 old_read_domains = obj->base.read_domains;
2762 old_write_domain = obj->base.write_domain;
2763
2764 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
2765 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
2766
2767 trace_i915_gem_object_change_domain(obj,
2768 old_read_domains,
2769 old_write_domain);
2770 }
2771
2772 obj->cache_level = cache_level;
2773 return 0;
2774}
2775
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002776/*
Chris Wilson2da3b9b2011-04-14 09:41:17 +01002777 * Prepare buffer for display plane (scanout, cursors, etc).
2778 * Can be called from an uninterruptible phase (modesetting) and allows
2779 * any flushes to be pipelined (for pageflips).
2780 *
2781 * For the display plane, we want to be in the GTT but out of any write
2782 * domains. So in many ways this looks like set_to_gtt_domain() apart from the
2783 * ability to pipeline the waits, pinning and any additional subtleties
2784 * that may differentiate the display plane from ordinary buffers.
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002785 */
2786int
Chris Wilson2da3b9b2011-04-14 09:41:17 +01002787i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
2788 u32 alignment,
Chris Wilson919926a2010-11-12 13:42:53 +00002789 struct intel_ring_buffer *pipelined)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002790{
Chris Wilson2da3b9b2011-04-14 09:41:17 +01002791 u32 old_read_domains, old_write_domain;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002792 int ret;
2793
Chris Wilson88241782011-01-07 17:09:48 +00002794 ret = i915_gem_object_flush_gpu_write_domain(obj);
2795 if (ret)
2796 return ret;
2797
Chris Wilson0be73282010-12-06 14:36:27 +00002798 if (pipelined != obj->ring) {
Chris Wilsonce453d82011-02-21 14:43:56 +00002799 ret = i915_gem_object_wait_rendering(obj);
Keith Packardf0b69ef2011-07-19 16:21:40 -07002800 if (ret == -ERESTARTSYS)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002801 return ret;
2802 }
2803
Eric Anholta7ef0642011-03-29 16:59:54 -07002804 /* The display engine is not coherent with the LLC cache on gen6. As
2805 * a result, we make sure that the pinning that is about to occur is
2806 * done with uncached PTEs. This is lowest common denominator for all
2807 * chipsets.
2808 *
2809 * However for gen6+, we could do better by using the GFDT bit instead
2810 * of uncaching, which would allow us to flush all the LLC-cached data
2811 * with that bit in the PTE to main memory with just one PIPE_CONTROL.
2812 */
2813 ret = i915_gem_object_set_cache_level(obj, I915_CACHE_NONE);
2814 if (ret)
2815 return ret;
2816
Chris Wilson2da3b9b2011-04-14 09:41:17 +01002817 /* As the user may map the buffer once pinned in the display plane
2818 * (e.g. libkms for the bootup splash), we have to ensure that we
2819 * always use map_and_fenceable for all scanout buffers.
2820 */
2821 ret = i915_gem_object_pin(obj, alignment, true);
2822 if (ret)
2823 return ret;
2824
Chris Wilsonb118c1e2010-05-27 13:18:14 +01002825 i915_gem_object_flush_cpu_write_domain(obj);
2826
Chris Wilson2da3b9b2011-04-14 09:41:17 +01002827 old_write_domain = obj->base.write_domain;
Chris Wilson05394f32010-11-08 19:18:58 +00002828 old_read_domains = obj->base.read_domains;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01002829
2830 /* It should now be out of any other write domains, and we can update
2831 * the domain values for our changes.
2832 */
2833 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
Chris Wilson05394f32010-11-08 19:18:58 +00002834 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002835
2836 trace_i915_gem_object_change_domain(obj,
2837 old_read_domains,
Chris Wilson2da3b9b2011-04-14 09:41:17 +01002838 old_write_domain);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002839
2840 return 0;
2841}
2842
Chris Wilson85345512010-11-13 09:49:11 +00002843int
Chris Wilsona8198ee2011-04-13 22:04:09 +01002844i915_gem_object_finish_gpu(struct drm_i915_gem_object *obj)
Chris Wilson85345512010-11-13 09:49:11 +00002845{
Chris Wilson88241782011-01-07 17:09:48 +00002846 int ret;
2847
Chris Wilsona8198ee2011-04-13 22:04:09 +01002848 if ((obj->base.read_domains & I915_GEM_GPU_DOMAINS) == 0)
Chris Wilson85345512010-11-13 09:49:11 +00002849 return 0;
2850
Chris Wilson88241782011-01-07 17:09:48 +00002851 if (obj->base.write_domain & I915_GEM_GPU_DOMAINS) {
Chris Wilsondb53a302011-02-03 11:57:46 +00002852 ret = i915_gem_flush_ring(obj->ring, 0, obj->base.write_domain);
Chris Wilson88241782011-01-07 17:09:48 +00002853 if (ret)
2854 return ret;
2855 }
Chris Wilson85345512010-11-13 09:49:11 +00002856
Chris Wilsonc501ae72011-12-14 13:57:23 +01002857 ret = i915_gem_object_wait_rendering(obj);
2858 if (ret)
2859 return ret;
2860
Chris Wilsona8198ee2011-04-13 22:04:09 +01002861 /* Ensure that we invalidate the GPU's caches and TLBs. */
2862 obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
Chris Wilsonc501ae72011-12-14 13:57:23 +01002863 return 0;
Chris Wilson85345512010-11-13 09:49:11 +00002864}
2865
Eric Anholte47c68e2008-11-14 13:35:19 -08002866/**
2867 * Moves a single object to the CPU read, and possibly write domain.
2868 *
2869 * This function returns when the move is complete, including waiting on
2870 * flushes to occur.
2871 */
Chris Wilsondabdfe02012-03-26 10:10:27 +02002872int
Chris Wilson919926a2010-11-12 13:42:53 +00002873i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholte47c68e2008-11-14 13:35:19 -08002874{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002875 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002876 int ret;
2877
Chris Wilson8d7e3de2011-02-07 15:23:02 +00002878 if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
2879 return 0;
2880
Chris Wilson88241782011-01-07 17:09:48 +00002881 ret = i915_gem_object_flush_gpu_write_domain(obj);
2882 if (ret)
2883 return ret;
2884
Chris Wilsonce453d82011-02-21 14:43:56 +00002885 ret = i915_gem_object_wait_rendering(obj);
Daniel Vetterde18a292010-11-27 22:30:41 +01002886 if (ret)
Eric Anholte47c68e2008-11-14 13:35:19 -08002887 return ret;
2888
2889 i915_gem_object_flush_gtt_write_domain(obj);
2890
Chris Wilson05394f32010-11-08 19:18:58 +00002891 old_write_domain = obj->base.write_domain;
2892 old_read_domains = obj->base.read_domains;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002893
Eric Anholte47c68e2008-11-14 13:35:19 -08002894 /* Flush the CPU cache if it's still invalid. */
Chris Wilson05394f32010-11-08 19:18:58 +00002895 if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
Eric Anholte47c68e2008-11-14 13:35:19 -08002896 i915_gem_clflush_object(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08002897
Chris Wilson05394f32010-11-08 19:18:58 +00002898 obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08002899 }
2900
2901 /* It should now be out of any other write domains, and we can update
2902 * the domain values for our changes.
2903 */
Chris Wilson05394f32010-11-08 19:18:58 +00002904 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
Eric Anholte47c68e2008-11-14 13:35:19 -08002905
2906 /* If we're writing through the CPU, then the GPU read domains will
2907 * need to be invalidated at next use.
2908 */
2909 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00002910 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
2911 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08002912 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002913
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002914 trace_i915_gem_object_change_domain(obj,
2915 old_read_domains,
2916 old_write_domain);
2917
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002918 return 0;
2919}
2920
Eric Anholt673a3942008-07-30 12:06:12 -07002921/* Throttle our rendering by waiting until the ring has completed our requests
2922 * emitted over 20 msec ago.
2923 *
Eric Anholtb9624422009-06-03 07:27:35 +00002924 * Note that if we were to use the current jiffies each time around the loop,
2925 * we wouldn't escape the function with any frames outstanding if the time to
2926 * render a frame was over 20ms.
2927 *
Eric Anholt673a3942008-07-30 12:06:12 -07002928 * This should get us reasonable parallelism between CPU and GPU but also
2929 * relatively low latency when blocking on a particular request to finish.
2930 */
2931static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01002932i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07002933{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01002934 struct drm_i915_private *dev_priv = dev->dev_private;
2935 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00002936 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01002937 struct drm_i915_gem_request *request;
2938 struct intel_ring_buffer *ring = NULL;
2939 u32 seqno = 0;
2940 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002941
Chris Wilsone110e8d2011-01-26 15:39:14 +00002942 if (atomic_read(&dev_priv->mm.wedged))
2943 return -EIO;
2944
Chris Wilson1c255952010-09-26 11:03:27 +01002945 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01002946 list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
Eric Anholtb9624422009-06-03 07:27:35 +00002947 if (time_after_eq(request->emitted_jiffies, recent_enough))
2948 break;
2949
Chris Wilsonf787a5f2010-09-24 16:02:42 +01002950 ring = request->ring;
2951 seqno = request->seqno;
Eric Anholtb9624422009-06-03 07:27:35 +00002952 }
Chris Wilson1c255952010-09-26 11:03:27 +01002953 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01002954
2955 if (seqno == 0)
2956 return 0;
2957
2958 ret = 0;
Chris Wilson78501ea2010-10-27 12:18:21 +01002959 if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) {
Chris Wilsonf787a5f2010-09-24 16:02:42 +01002960 /* And wait for the seqno passing without holding any locks and
2961 * causing extra latency for others. This is safe as the irq
2962 * generation is designed to be run atomically and so is
2963 * lockless.
2964 */
Chris Wilsonb13c2b92010-12-13 16:54:50 +00002965 if (ring->irq_get(ring)) {
2966 ret = wait_event_interruptible(ring->irq_queue,
2967 i915_seqno_passed(ring->get_seqno(ring), seqno)
2968 || atomic_read(&dev_priv->mm.wedged));
2969 ring->irq_put(ring);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01002970
Chris Wilsonb13c2b92010-12-13 16:54:50 +00002971 if (ret == 0 && atomic_read(&dev_priv->mm.wedged))
2972 ret = -EIO;
Eric Anholte959b5d2011-12-22 14:55:01 -08002973 } else if (wait_for_atomic(i915_seqno_passed(ring->get_seqno(ring),
2974 seqno) ||
Eric Anholt7ea29b12011-12-22 14:54:59 -08002975 atomic_read(&dev_priv->mm.wedged), 3000)) {
2976 ret = -EBUSY;
Chris Wilsonb13c2b92010-12-13 16:54:50 +00002977 }
Chris Wilsonf787a5f2010-09-24 16:02:42 +01002978 }
2979
2980 if (ret == 0)
2981 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
Eric Anholtb9624422009-06-03 07:27:35 +00002982
Eric Anholt673a3942008-07-30 12:06:12 -07002983 return ret;
2984}
2985
Eric Anholt673a3942008-07-30 12:06:12 -07002986int
Chris Wilson05394f32010-11-08 19:18:58 +00002987i915_gem_object_pin(struct drm_i915_gem_object *obj,
2988 uint32_t alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01002989 bool map_and_fenceable)
Eric Anholt673a3942008-07-30 12:06:12 -07002990{
Chris Wilson05394f32010-11-08 19:18:58 +00002991 struct drm_device *dev = obj->base.dev;
Chris Wilsonf13d3f72010-09-20 17:36:15 +01002992 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07002993 int ret;
2994
Chris Wilson05394f32010-11-08 19:18:58 +00002995 BUG_ON(obj->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT);
Chris Wilson23bc5982010-09-29 16:10:57 +01002996 WARN_ON(i915_verify_lists(dev));
Chris Wilsonac0c6b52010-05-27 13:18:18 +01002997
Chris Wilson05394f32010-11-08 19:18:58 +00002998 if (obj->gtt_space != NULL) {
2999 if ((alignment && obj->gtt_offset & (alignment - 1)) ||
3000 (map_and_fenceable && !obj->map_and_fenceable)) {
3001 WARN(obj->pin_count,
Chris Wilsonae7d49d2010-08-04 12:37:41 +01003002 "bo is already pinned with incorrect alignment:"
Daniel Vetter75e9e912010-11-04 17:11:09 +01003003 " offset=%x, req.alignment=%x, req.map_and_fenceable=%d,"
3004 " obj->map_and_fenceable=%d\n",
Chris Wilson05394f32010-11-08 19:18:58 +00003005 obj->gtt_offset, alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01003006 map_and_fenceable,
Chris Wilson05394f32010-11-08 19:18:58 +00003007 obj->map_and_fenceable);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01003008 ret = i915_gem_object_unbind(obj);
3009 if (ret)
3010 return ret;
3011 }
3012 }
3013
Chris Wilson05394f32010-11-08 19:18:58 +00003014 if (obj->gtt_space == NULL) {
Chris Wilsona00b10c2010-09-24 21:15:47 +01003015 ret = i915_gem_object_bind_to_gtt(obj, alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01003016 map_and_fenceable);
Chris Wilson97311292009-09-21 00:22:34 +01003017 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07003018 return ret;
Chris Wilson22c344e2009-02-11 14:26:45 +00003019 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05003020
Daniel Vetter74898d72012-02-15 23:50:22 +01003021 if (!obj->has_global_gtt_mapping && map_and_fenceable)
3022 i915_gem_gtt_bind_object(obj, obj->cache_level);
3023
Chris Wilson05394f32010-11-08 19:18:58 +00003024 if (obj->pin_count++ == 0) {
Chris Wilson05394f32010-11-08 19:18:58 +00003025 if (!obj->active)
3026 list_move_tail(&obj->mm_list,
Chris Wilsonf13d3f72010-09-20 17:36:15 +01003027 &dev_priv->mm.pinned_list);
Eric Anholt673a3942008-07-30 12:06:12 -07003028 }
Chris Wilson6299f992010-11-24 12:23:44 +00003029 obj->pin_mappable |= map_and_fenceable;
Eric Anholt673a3942008-07-30 12:06:12 -07003030
Chris Wilson23bc5982010-09-29 16:10:57 +01003031 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07003032 return 0;
3033}
3034
3035void
Chris Wilson05394f32010-11-08 19:18:58 +00003036i915_gem_object_unpin(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07003037{
Chris Wilson05394f32010-11-08 19:18:58 +00003038 struct drm_device *dev = obj->base.dev;
Eric Anholt673a3942008-07-30 12:06:12 -07003039 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003040
Chris Wilson23bc5982010-09-29 16:10:57 +01003041 WARN_ON(i915_verify_lists(dev));
Chris Wilson05394f32010-11-08 19:18:58 +00003042 BUG_ON(obj->pin_count == 0);
3043 BUG_ON(obj->gtt_space == NULL);
Eric Anholt673a3942008-07-30 12:06:12 -07003044
Chris Wilson05394f32010-11-08 19:18:58 +00003045 if (--obj->pin_count == 0) {
3046 if (!obj->active)
3047 list_move_tail(&obj->mm_list,
Eric Anholt673a3942008-07-30 12:06:12 -07003048 &dev_priv->mm.inactive_list);
Chris Wilson6299f992010-11-24 12:23:44 +00003049 obj->pin_mappable = false;
Eric Anholt673a3942008-07-30 12:06:12 -07003050 }
Chris Wilson23bc5982010-09-29 16:10:57 +01003051 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07003052}
3053
3054int
3055i915_gem_pin_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00003056 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003057{
3058 struct drm_i915_gem_pin *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003059 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07003060 int ret;
3061
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003062 ret = i915_mutex_lock_interruptible(dev);
3063 if (ret)
3064 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003065
Chris Wilson05394f32010-11-08 19:18:58 +00003066 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00003067 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003068 ret = -ENOENT;
3069 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07003070 }
Eric Anholt673a3942008-07-30 12:06:12 -07003071
Chris Wilson05394f32010-11-08 19:18:58 +00003072 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilsonbb6baf72009-09-22 14:24:13 +01003073 DRM_ERROR("Attempting to pin a purgeable buffer\n");
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003074 ret = -EINVAL;
3075 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003076 }
3077
Chris Wilson05394f32010-11-08 19:18:58 +00003078 if (obj->pin_filp != NULL && obj->pin_filp != file) {
Jesse Barnes79e53942008-11-07 14:24:08 -08003079 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
3080 args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003081 ret = -EINVAL;
3082 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08003083 }
3084
Chris Wilson05394f32010-11-08 19:18:58 +00003085 obj->user_pin_count++;
3086 obj->pin_filp = file;
3087 if (obj->user_pin_count == 1) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01003088 ret = i915_gem_object_pin(obj, args->alignment, true);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003089 if (ret)
3090 goto out;
Eric Anholt673a3942008-07-30 12:06:12 -07003091 }
3092
3093 /* XXX - flush the CPU caches for pinned objects
3094 * as the X server doesn't manage domains yet
3095 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003096 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00003097 args->offset = obj->gtt_offset;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003098out:
Chris Wilson05394f32010-11-08 19:18:58 +00003099 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003100unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07003101 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003102 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003103}
3104
3105int
3106i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00003107 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003108{
3109 struct drm_i915_gem_pin *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003110 struct drm_i915_gem_object *obj;
Chris Wilson76c1dec2010-09-25 11:22:51 +01003111 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003112
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003113 ret = i915_mutex_lock_interruptible(dev);
3114 if (ret)
3115 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003116
Chris Wilson05394f32010-11-08 19:18:58 +00003117 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00003118 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003119 ret = -ENOENT;
3120 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07003121 }
Chris Wilson76c1dec2010-09-25 11:22:51 +01003122
Chris Wilson05394f32010-11-08 19:18:58 +00003123 if (obj->pin_filp != file) {
Jesse Barnes79e53942008-11-07 14:24:08 -08003124 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
3125 args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003126 ret = -EINVAL;
3127 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08003128 }
Chris Wilson05394f32010-11-08 19:18:58 +00003129 obj->user_pin_count--;
3130 if (obj->user_pin_count == 0) {
3131 obj->pin_filp = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08003132 i915_gem_object_unpin(obj);
3133 }
Eric Anholt673a3942008-07-30 12:06:12 -07003134
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003135out:
Chris Wilson05394f32010-11-08 19:18:58 +00003136 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003137unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07003138 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003139 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003140}
3141
3142int
3143i915_gem_busy_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00003144 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003145{
3146 struct drm_i915_gem_busy *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003147 struct drm_i915_gem_object *obj;
Chris Wilson30dbf0c2010-09-25 10:19:17 +01003148 int ret;
3149
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003150 ret = i915_mutex_lock_interruptible(dev);
3151 if (ret)
3152 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003153
Chris Wilson05394f32010-11-08 19:18:58 +00003154 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00003155 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003156 ret = -ENOENT;
3157 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07003158 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08003159
Chris Wilson0be555b2010-08-04 15:36:30 +01003160 /* Count all active objects as busy, even if they are currently not used
3161 * by the gpu. Users of this interface expect objects to eventually
3162 * become non-busy without any further actions, therefore emit any
3163 * necessary flushes here.
Eric Anholtc4de0a52008-12-14 19:05:04 -08003164 */
Chris Wilson05394f32010-11-08 19:18:58 +00003165 args->busy = obj->active;
Chris Wilson0be555b2010-08-04 15:36:30 +01003166 if (args->busy) {
3167 /* Unconditionally flush objects, even when the gpu still uses this
3168 * object. Userspace calling this function indicates that it wants to
3169 * use this buffer rather sooner than later, so issuing the required
3170 * flush earlier is beneficial.
3171 */
Chris Wilson1a1c6972010-12-07 23:00:20 +00003172 if (obj->base.write_domain & I915_GEM_GPU_DOMAINS) {
Chris Wilsondb53a302011-02-03 11:57:46 +00003173 ret = i915_gem_flush_ring(obj->ring,
Chris Wilson88241782011-01-07 17:09:48 +00003174 0, obj->base.write_domain);
Chris Wilson1a1c6972010-12-07 23:00:20 +00003175 } else if (obj->ring->outstanding_lazy_request ==
3176 obj->last_rendering_seqno) {
3177 struct drm_i915_gem_request *request;
3178
Chris Wilson7a194872010-12-07 10:38:40 +00003179 /* This ring is not being cleared by active usage,
3180 * so emit a request to do so.
3181 */
Chris Wilson1a1c6972010-12-07 23:00:20 +00003182 request = kzalloc(sizeof(*request), GFP_KERNEL);
Rakib Mullick457eafc2011-11-16 00:49:28 +06003183 if (request) {
Akshay Joshi0206e352011-08-16 15:34:10 -04003184 ret = i915_add_request(obj->ring, NULL, request);
Rakib Mullick457eafc2011-11-16 00:49:28 +06003185 if (ret)
3186 kfree(request);
3187 } else
Chris Wilson7a194872010-12-07 10:38:40 +00003188 ret = -ENOMEM;
3189 }
Chris Wilson0be555b2010-08-04 15:36:30 +01003190
3191 /* Update the active list for the hardware's current position.
3192 * Otherwise this only updates on a delayed timer or when irqs
3193 * are actually unmasked, and our working set ends up being
3194 * larger than required.
3195 */
Chris Wilsondb53a302011-02-03 11:57:46 +00003196 i915_gem_retire_requests_ring(obj->ring);
Chris Wilson0be555b2010-08-04 15:36:30 +01003197
Chris Wilson05394f32010-11-08 19:18:58 +00003198 args->busy = obj->active;
Chris Wilson0be555b2010-08-04 15:36:30 +01003199 }
Eric Anholt673a3942008-07-30 12:06:12 -07003200
Chris Wilson05394f32010-11-08 19:18:58 +00003201 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003202unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07003203 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003204 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003205}
3206
3207int
3208i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
3209 struct drm_file *file_priv)
3210{
Akshay Joshi0206e352011-08-16 15:34:10 -04003211 return i915_gem_ring_throttle(dev, file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07003212}
3213
Chris Wilson3ef94da2009-09-14 16:50:29 +01003214int
3215i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
3216 struct drm_file *file_priv)
3217{
3218 struct drm_i915_gem_madvise *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003219 struct drm_i915_gem_object *obj;
Chris Wilson76c1dec2010-09-25 11:22:51 +01003220 int ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003221
3222 switch (args->madv) {
3223 case I915_MADV_DONTNEED:
3224 case I915_MADV_WILLNEED:
3225 break;
3226 default:
3227 return -EINVAL;
3228 }
3229
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003230 ret = i915_mutex_lock_interruptible(dev);
3231 if (ret)
3232 return ret;
3233
Chris Wilson05394f32010-11-08 19:18:58 +00003234 obj = to_intel_bo(drm_gem_object_lookup(dev, file_priv, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00003235 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003236 ret = -ENOENT;
3237 goto unlock;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003238 }
Chris Wilson3ef94da2009-09-14 16:50:29 +01003239
Chris Wilson05394f32010-11-08 19:18:58 +00003240 if (obj->pin_count) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003241 ret = -EINVAL;
3242 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003243 }
3244
Chris Wilson05394f32010-11-08 19:18:58 +00003245 if (obj->madv != __I915_MADV_PURGED)
3246 obj->madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003247
Chris Wilson2d7ef392009-09-20 23:13:10 +01003248 /* if the object is no longer bound, discard its backing storage */
Chris Wilson05394f32010-11-08 19:18:58 +00003249 if (i915_gem_object_is_purgeable(obj) &&
3250 obj->gtt_space == NULL)
Chris Wilson2d7ef392009-09-20 23:13:10 +01003251 i915_gem_object_truncate(obj);
3252
Chris Wilson05394f32010-11-08 19:18:58 +00003253 args->retained = obj->madv != __I915_MADV_PURGED;
Chris Wilsonbb6baf72009-09-22 14:24:13 +01003254
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003255out:
Chris Wilson05394f32010-11-08 19:18:58 +00003256 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003257unlock:
Chris Wilson3ef94da2009-09-14 16:50:29 +01003258 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003259 return ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003260}
3261
Chris Wilson05394f32010-11-08 19:18:58 +00003262struct drm_i915_gem_object *i915_gem_alloc_object(struct drm_device *dev,
3263 size_t size)
Daniel Vetterac52bc52010-04-09 19:05:06 +00003264{
Chris Wilson73aa8082010-09-30 11:46:12 +01003265 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc397b902010-04-09 19:05:07 +00003266 struct drm_i915_gem_object *obj;
Hugh Dickins5949eac2011-06-27 16:18:18 -07003267 struct address_space *mapping;
Daniel Vetterc397b902010-04-09 19:05:07 +00003268
3269 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
3270 if (obj == NULL)
3271 return NULL;
3272
3273 if (drm_gem_object_init(dev, &obj->base, size) != 0) {
3274 kfree(obj);
3275 return NULL;
3276 }
3277
Hugh Dickins5949eac2011-06-27 16:18:18 -07003278 mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
3279 mapping_set_gfp_mask(mapping, GFP_HIGHUSER | __GFP_RECLAIMABLE);
3280
Chris Wilson73aa8082010-09-30 11:46:12 +01003281 i915_gem_info_add_obj(dev_priv, size);
3282
Daniel Vetterc397b902010-04-09 19:05:07 +00003283 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
3284 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3285
Eugeni Dodonov3d29b842012-01-17 14:43:53 -02003286 if (HAS_LLC(dev)) {
3287 /* On some devices, we can have the GPU use the LLC (the CPU
Eric Anholta1871112011-03-29 16:59:55 -07003288 * cache) for about a 10% performance improvement
3289 * compared to uncached. Graphics requests other than
3290 * display scanout are coherent with the CPU in
3291 * accessing this cache. This means in this mode we
3292 * don't need to clflush on the CPU side, and on the
3293 * GPU side we only need to flush internal caches to
3294 * get data visible to the CPU.
3295 *
3296 * However, we maintain the display planes as UC, and so
3297 * need to rebind when first used as such.
3298 */
3299 obj->cache_level = I915_CACHE_LLC;
3300 } else
3301 obj->cache_level = I915_CACHE_NONE;
3302
Daniel Vetter62b8b212010-04-09 19:05:08 +00003303 obj->base.driver_private = NULL;
Daniel Vetterc397b902010-04-09 19:05:07 +00003304 obj->fence_reg = I915_FENCE_REG_NONE;
Chris Wilson69dc4982010-10-19 10:36:51 +01003305 INIT_LIST_HEAD(&obj->mm_list);
Daniel Vetter93a37f22010-11-05 20:24:53 +01003306 INIT_LIST_HEAD(&obj->gtt_list);
Chris Wilson69dc4982010-10-19 10:36:51 +01003307 INIT_LIST_HEAD(&obj->ring_list);
Chris Wilson432e58e2010-11-25 19:32:06 +00003308 INIT_LIST_HEAD(&obj->exec_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00003309 INIT_LIST_HEAD(&obj->gpu_write_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00003310 obj->madv = I915_MADV_WILLNEED;
Daniel Vetter75e9e912010-11-04 17:11:09 +01003311 /* Avoid an unnecessary call to unbind on the first bind. */
3312 obj->map_and_fenceable = true;
Daniel Vetterc397b902010-04-09 19:05:07 +00003313
Chris Wilson05394f32010-11-08 19:18:58 +00003314 return obj;
Daniel Vetterac52bc52010-04-09 19:05:06 +00003315}
3316
Eric Anholt673a3942008-07-30 12:06:12 -07003317int i915_gem_init_object(struct drm_gem_object *obj)
3318{
Daniel Vetterc397b902010-04-09 19:05:07 +00003319 BUG();
Jesse Barnesde151cf2008-11-12 10:03:55 -08003320
Eric Anholt673a3942008-07-30 12:06:12 -07003321 return 0;
3322}
3323
Chris Wilson05394f32010-11-08 19:18:58 +00003324static void i915_gem_free_object_tail(struct drm_i915_gem_object *obj)
Chris Wilsonbe726152010-07-23 23:18:50 +01003325{
Chris Wilson05394f32010-11-08 19:18:58 +00003326 struct drm_device *dev = obj->base.dev;
Chris Wilsonbe726152010-07-23 23:18:50 +01003327 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonbe726152010-07-23 23:18:50 +01003328 int ret;
3329
3330 ret = i915_gem_object_unbind(obj);
3331 if (ret == -ERESTARTSYS) {
Chris Wilson05394f32010-11-08 19:18:58 +00003332 list_move(&obj->mm_list,
Chris Wilsonbe726152010-07-23 23:18:50 +01003333 &dev_priv->mm.deferred_free_list);
3334 return;
3335 }
3336
Chris Wilson26e12f892011-03-20 11:20:19 +00003337 trace_i915_gem_object_destroy(obj);
3338
Chris Wilson05394f32010-11-08 19:18:58 +00003339 if (obj->base.map_list.map)
Rob Clarkb464e9a2011-08-10 08:09:08 -05003340 drm_gem_free_mmap_offset(&obj->base);
Chris Wilsonbe726152010-07-23 23:18:50 +01003341
Chris Wilson05394f32010-11-08 19:18:58 +00003342 drm_gem_object_release(&obj->base);
3343 i915_gem_info_remove_obj(dev_priv, obj->base.size);
Chris Wilsonbe726152010-07-23 23:18:50 +01003344
Chris Wilson05394f32010-11-08 19:18:58 +00003345 kfree(obj->bit_17);
3346 kfree(obj);
Chris Wilsonbe726152010-07-23 23:18:50 +01003347}
3348
Chris Wilson05394f32010-11-08 19:18:58 +00003349void i915_gem_free_object(struct drm_gem_object *gem_obj)
Eric Anholt673a3942008-07-30 12:06:12 -07003350{
Chris Wilson05394f32010-11-08 19:18:58 +00003351 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
3352 struct drm_device *dev = obj->base.dev;
Eric Anholt673a3942008-07-30 12:06:12 -07003353
Chris Wilson05394f32010-11-08 19:18:58 +00003354 while (obj->pin_count > 0)
Eric Anholt673a3942008-07-30 12:06:12 -07003355 i915_gem_object_unpin(obj);
3356
Chris Wilson05394f32010-11-08 19:18:58 +00003357 if (obj->phys_obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10003358 i915_gem_detach_phys_object(dev, obj);
3359
Chris Wilsonbe726152010-07-23 23:18:50 +01003360 i915_gem_free_object_tail(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003361}
3362
Jesse Barnes5669fca2009-02-17 15:13:31 -08003363int
Eric Anholt673a3942008-07-30 12:06:12 -07003364i915_gem_idle(struct drm_device *dev)
3365{
3366 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson29105cc2010-01-07 10:39:13 +00003367 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003368
Keith Packard6dbe2772008-10-14 21:41:13 -07003369 mutex_lock(&dev->struct_mutex);
3370
Chris Wilson87acb0a2010-10-19 10:13:00 +01003371 if (dev_priv->mm.suspended) {
Keith Packard6dbe2772008-10-14 21:41:13 -07003372 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07003373 return 0;
Keith Packard6dbe2772008-10-14 21:41:13 -07003374 }
Eric Anholt673a3942008-07-30 12:06:12 -07003375
Ben Widawskyb93f9cf2012-01-25 15:39:34 -08003376 ret = i915_gpu_idle(dev, true);
Keith Packard6dbe2772008-10-14 21:41:13 -07003377 if (ret) {
3378 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07003379 return ret;
Keith Packard6dbe2772008-10-14 21:41:13 -07003380 }
Eric Anholt673a3942008-07-30 12:06:12 -07003381
Chris Wilson29105cc2010-01-07 10:39:13 +00003382 /* Under UMS, be paranoid and evict. */
3383 if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
Chris Wilson5eac3ab2010-10-31 08:49:47 +00003384 ret = i915_gem_evict_inactive(dev, false);
Chris Wilson29105cc2010-01-07 10:39:13 +00003385 if (ret) {
3386 mutex_unlock(&dev->struct_mutex);
3387 return ret;
3388 }
3389 }
3390
Chris Wilson312817a2010-11-22 11:50:11 +00003391 i915_gem_reset_fences(dev);
3392
Chris Wilson29105cc2010-01-07 10:39:13 +00003393 /* Hack! Don't let anybody do execbuf while we don't control the chip.
3394 * We need to replace this with a semaphore, or something.
3395 * And not confound mm.suspended!
3396 */
3397 dev_priv->mm.suspended = 1;
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02003398 del_timer_sync(&dev_priv->hangcheck_timer);
Chris Wilson29105cc2010-01-07 10:39:13 +00003399
3400 i915_kernel_lost_context(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07003401 i915_gem_cleanup_ringbuffer(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00003402
Keith Packard6dbe2772008-10-14 21:41:13 -07003403 mutex_unlock(&dev->struct_mutex);
3404
Chris Wilson29105cc2010-01-07 10:39:13 +00003405 /* Cancel the retire work handler, which should be idle now. */
3406 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
3407
Eric Anholt673a3942008-07-30 12:06:12 -07003408 return 0;
3409}
3410
Daniel Vetterf691e2f2012-02-02 09:58:12 +01003411void i915_gem_init_swizzling(struct drm_device *dev)
3412{
3413 drm_i915_private_t *dev_priv = dev->dev_private;
3414
Daniel Vetter11782b02012-01-31 16:47:55 +01003415 if (INTEL_INFO(dev)->gen < 5 ||
Daniel Vetterf691e2f2012-02-02 09:58:12 +01003416 dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
3417 return;
3418
3419 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
3420 DISP_TILE_SURFACE_SWIZZLING);
3421
Daniel Vetter11782b02012-01-31 16:47:55 +01003422 if (IS_GEN5(dev))
3423 return;
3424
Daniel Vetterf691e2f2012-02-02 09:58:12 +01003425 I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
3426 if (IS_GEN6(dev))
3427 I915_WRITE(ARB_MODE, ARB_MODE_ENABLE(ARB_MODE_SWIZZLE_SNB));
3428 else
3429 I915_WRITE(ARB_MODE, ARB_MODE_ENABLE(ARB_MODE_SWIZZLE_IVB));
3430}
Daniel Vettere21af882012-02-09 20:53:27 +01003431
3432void i915_gem_init_ppgtt(struct drm_device *dev)
3433{
3434 drm_i915_private_t *dev_priv = dev->dev_private;
3435 uint32_t pd_offset;
3436 struct intel_ring_buffer *ring;
3437 int i;
3438
3439 if (!dev_priv->mm.aliasing_ppgtt)
3440 return;
3441
3442 pd_offset = dev_priv->mm.aliasing_ppgtt->pd_offset;
3443 pd_offset /= 64; /* in cachelines, */
3444 pd_offset <<= 16;
3445
3446 if (INTEL_INFO(dev)->gen == 6) {
3447 uint32_t ecochk = I915_READ(GAM_ECOCHK);
3448 I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT |
3449 ECOCHK_PPGTT_CACHE64B);
3450 I915_WRITE(GFX_MODE, GFX_MODE_ENABLE(GFX_PPGTT_ENABLE));
3451 } else if (INTEL_INFO(dev)->gen >= 7) {
3452 I915_WRITE(GAM_ECOCHK, ECOCHK_PPGTT_CACHE64B);
3453 /* GFX_MODE is per-ring on gen7+ */
3454 }
3455
3456 for (i = 0; i < I915_NUM_RINGS; i++) {
3457 ring = &dev_priv->ring[i];
3458
3459 if (INTEL_INFO(dev)->gen >= 7)
3460 I915_WRITE(RING_MODE_GEN7(ring),
3461 GFX_MODE_ENABLE(GFX_PPGTT_ENABLE));
3462
3463 I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
3464 I915_WRITE(RING_PP_DIR_BASE(ring), pd_offset);
3465 }
3466}
3467
Eric Anholt673a3942008-07-30 12:06:12 -07003468int
Daniel Vetterf691e2f2012-02-02 09:58:12 +01003469i915_gem_init_hw(struct drm_device *dev)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08003470{
3471 drm_i915_private_t *dev_priv = dev->dev_private;
3472 int ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01003473
Daniel Vetterf691e2f2012-02-02 09:58:12 +01003474 i915_gem_init_swizzling(dev);
3475
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08003476 ret = intel_init_render_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01003477 if (ret)
Chris Wilsonb6913e42010-11-12 10:46:37 +00003478 return ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01003479
3480 if (HAS_BSD(dev)) {
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08003481 ret = intel_init_bsd_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01003482 if (ret)
3483 goto cleanup_render_ring;
Zou Nan haid1b851f2010-05-21 09:08:57 +08003484 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01003485
Chris Wilson549f7362010-10-19 11:19:32 +01003486 if (HAS_BLT(dev)) {
3487 ret = intel_init_blt_ring_buffer(dev);
3488 if (ret)
3489 goto cleanup_bsd_ring;
3490 }
3491
Chris Wilson6f392d5482010-08-07 11:01:22 +01003492 dev_priv->next_seqno = 1;
3493
Daniel Vettere21af882012-02-09 20:53:27 +01003494 i915_gem_init_ppgtt(dev);
3495
Chris Wilson68f95ba2010-05-27 13:18:22 +01003496 return 0;
3497
Chris Wilson549f7362010-10-19 11:19:32 +01003498cleanup_bsd_ring:
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003499 intel_cleanup_ring_buffer(&dev_priv->ring[VCS]);
Chris Wilson68f95ba2010-05-27 13:18:22 +01003500cleanup_render_ring:
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003501 intel_cleanup_ring_buffer(&dev_priv->ring[RCS]);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08003502 return ret;
3503}
3504
3505void
3506i915_gem_cleanup_ringbuffer(struct drm_device *dev)
3507{
3508 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003509 int i;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08003510
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003511 for (i = 0; i < I915_NUM_RINGS; i++)
3512 intel_cleanup_ring_buffer(&dev_priv->ring[i]);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08003513}
3514
3515int
Eric Anholt673a3942008-07-30 12:06:12 -07003516i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
3517 struct drm_file *file_priv)
3518{
3519 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003520 int ret, i;
Eric Anholt673a3942008-07-30 12:06:12 -07003521
Jesse Barnes79e53942008-11-07 14:24:08 -08003522 if (drm_core_check_feature(dev, DRIVER_MODESET))
3523 return 0;
3524
Ben Gamariba1234d2009-09-14 17:48:47 -04003525 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07003526 DRM_ERROR("Reenabling wedged hardware, good luck\n");
Ben Gamariba1234d2009-09-14 17:48:47 -04003527 atomic_set(&dev_priv->mm.wedged, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07003528 }
3529
Eric Anholt673a3942008-07-30 12:06:12 -07003530 mutex_lock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08003531 dev_priv->mm.suspended = 0;
3532
Daniel Vetterf691e2f2012-02-02 09:58:12 +01003533 ret = i915_gem_init_hw(dev);
Wu Fengguangd816f6a2009-04-18 10:43:32 +08003534 if (ret != 0) {
3535 mutex_unlock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08003536 return ret;
Wu Fengguangd816f6a2009-04-18 10:43:32 +08003537 }
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08003538
Chris Wilson69dc4982010-10-19 10:36:51 +01003539 BUG_ON(!list_empty(&dev_priv->mm.active_list));
Eric Anholt673a3942008-07-30 12:06:12 -07003540 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
3541 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003542 for (i = 0; i < I915_NUM_RINGS; i++) {
3543 BUG_ON(!list_empty(&dev_priv->ring[i].active_list));
3544 BUG_ON(!list_empty(&dev_priv->ring[i].request_list));
3545 }
Eric Anholt673a3942008-07-30 12:06:12 -07003546 mutex_unlock(&dev->struct_mutex);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04003547
Chris Wilson5f353082010-06-07 14:03:03 +01003548 ret = drm_irq_install(dev);
3549 if (ret)
3550 goto cleanup_ringbuffer;
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04003551
Eric Anholt673a3942008-07-30 12:06:12 -07003552 return 0;
Chris Wilson5f353082010-06-07 14:03:03 +01003553
3554cleanup_ringbuffer:
3555 mutex_lock(&dev->struct_mutex);
3556 i915_gem_cleanup_ringbuffer(dev);
3557 dev_priv->mm.suspended = 1;
3558 mutex_unlock(&dev->struct_mutex);
3559
3560 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003561}
3562
3563int
3564i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
3565 struct drm_file *file_priv)
3566{
Jesse Barnes79e53942008-11-07 14:24:08 -08003567 if (drm_core_check_feature(dev, DRIVER_MODESET))
3568 return 0;
3569
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04003570 drm_irq_uninstall(dev);
Linus Torvaldse6890f62009-09-08 17:09:24 -07003571 return i915_gem_idle(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07003572}
3573
3574void
3575i915_gem_lastclose(struct drm_device *dev)
3576{
3577 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003578
Eric Anholte806b492009-01-22 09:56:58 -08003579 if (drm_core_check_feature(dev, DRIVER_MODESET))
3580 return;
3581
Keith Packard6dbe2772008-10-14 21:41:13 -07003582 ret = i915_gem_idle(dev);
3583 if (ret)
3584 DRM_ERROR("failed to idle hardware: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07003585}
3586
Chris Wilson64193402010-10-24 12:38:05 +01003587static void
3588init_ring_lists(struct intel_ring_buffer *ring)
3589{
3590 INIT_LIST_HEAD(&ring->active_list);
3591 INIT_LIST_HEAD(&ring->request_list);
3592 INIT_LIST_HEAD(&ring->gpu_write_list);
3593}
3594
Eric Anholt673a3942008-07-30 12:06:12 -07003595void
3596i915_gem_load(struct drm_device *dev)
3597{
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02003598 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07003599 drm_i915_private_t *dev_priv = dev->dev_private;
3600
Chris Wilson69dc4982010-10-19 10:36:51 +01003601 INIT_LIST_HEAD(&dev_priv->mm.active_list);
Eric Anholt673a3942008-07-30 12:06:12 -07003602 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
3603 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
Chris Wilsonf13d3f72010-09-20 17:36:15 +01003604 INIT_LIST_HEAD(&dev_priv->mm.pinned_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07003605 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Chris Wilsonbe726152010-07-23 23:18:50 +01003606 INIT_LIST_HEAD(&dev_priv->mm.deferred_free_list);
Daniel Vetter93a37f22010-11-05 20:24:53 +01003607 INIT_LIST_HEAD(&dev_priv->mm.gtt_list);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003608 for (i = 0; i < I915_NUM_RINGS; i++)
3609 init_ring_lists(&dev_priv->ring[i]);
Daniel Vetter4b9de732011-10-09 21:52:02 +02003610 for (i = 0; i < I915_MAX_NUM_FENCES; i++)
Daniel Vetter007cc8a2010-04-28 11:02:31 +02003611 INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
Eric Anholt673a3942008-07-30 12:06:12 -07003612 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
3613 i915_gem_retire_work_handler);
Chris Wilson30dbf0c2010-09-25 10:19:17 +01003614 init_completion(&dev_priv->error_completion);
Chris Wilson31169712009-09-14 16:50:28 +01003615
Dave Airlie94400122010-07-20 13:15:31 +10003616 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
3617 if (IS_GEN3(dev)) {
3618 u32 tmp = I915_READ(MI_ARB_STATE);
3619 if (!(tmp & MI_ARB_C3_LP_WRITE_ENABLE)) {
3620 /* arb state is a masked write, so set bit + bit in mask */
3621 tmp = MI_ARB_C3_LP_WRITE_ENABLE | (MI_ARB_C3_LP_WRITE_ENABLE << MI_ARB_MASK_SHIFT);
3622 I915_WRITE(MI_ARB_STATE, tmp);
3623 }
3624 }
3625
Chris Wilson72bfa192010-12-19 11:42:05 +00003626 dev_priv->relative_constants_mode = I915_EXEC_CONSTANTS_REL_GENERAL;
3627
Jesse Barnesde151cf2008-11-12 10:03:55 -08003628 /* Old X drivers will take 0-2 for front, back, depth buffers */
Eric Anholtb397c832010-01-26 09:43:10 -08003629 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3630 dev_priv->fence_reg_start = 3;
Jesse Barnesde151cf2008-11-12 10:03:55 -08003631
Chris Wilsona6c45cf2010-09-17 00:32:17 +01003632 if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08003633 dev_priv->num_fence_regs = 16;
3634 else
3635 dev_priv->num_fence_regs = 8;
3636
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02003637 /* Initialize fence registers to zero */
Eric Anholt10ed13e2011-05-06 13:53:49 -07003638 for (i = 0; i < dev_priv->num_fence_regs; i++) {
3639 i915_gem_clear_fence_reg(dev, &dev_priv->fence_regs[i]);
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02003640 }
Eric Anholt10ed13e2011-05-06 13:53:49 -07003641
Eric Anholt673a3942008-07-30 12:06:12 -07003642 i915_gem_detect_bit_6_swizzle(dev);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003643 init_waitqueue_head(&dev_priv->pending_flip_queue);
Chris Wilson17250b72010-10-28 12:51:39 +01003644
Chris Wilsonce453d82011-02-21 14:43:56 +00003645 dev_priv->mm.interruptible = true;
3646
Chris Wilson17250b72010-10-28 12:51:39 +01003647 dev_priv->mm.inactive_shrinker.shrink = i915_gem_inactive_shrink;
3648 dev_priv->mm.inactive_shrinker.seeks = DEFAULT_SEEKS;
3649 register_shrinker(&dev_priv->mm.inactive_shrinker);
Eric Anholt673a3942008-07-30 12:06:12 -07003650}
Dave Airlie71acb5e2008-12-30 20:31:46 +10003651
3652/*
3653 * Create a physically contiguous memory object for this object
3654 * e.g. for cursor + overlay regs
3655 */
Chris Wilson995b6762010-08-20 13:23:26 +01003656static int i915_gem_init_phys_object(struct drm_device *dev,
3657 int id, int size, int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10003658{
3659 drm_i915_private_t *dev_priv = dev->dev_private;
3660 struct drm_i915_gem_phys_object *phys_obj;
3661 int ret;
3662
3663 if (dev_priv->mm.phys_objs[id - 1] || !size)
3664 return 0;
3665
Eric Anholt9a298b22009-03-24 12:23:04 -07003666 phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
Dave Airlie71acb5e2008-12-30 20:31:46 +10003667 if (!phys_obj)
3668 return -ENOMEM;
3669
3670 phys_obj->id = id;
3671
Chris Wilson6eeefaf2010-08-07 11:01:39 +01003672 phys_obj->handle = drm_pci_alloc(dev, size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10003673 if (!phys_obj->handle) {
3674 ret = -ENOMEM;
3675 goto kfree_obj;
3676 }
3677#ifdef CONFIG_X86
3678 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
3679#endif
3680
3681 dev_priv->mm.phys_objs[id - 1] = phys_obj;
3682
3683 return 0;
3684kfree_obj:
Eric Anholt9a298b22009-03-24 12:23:04 -07003685 kfree(phys_obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10003686 return ret;
3687}
3688
Chris Wilson995b6762010-08-20 13:23:26 +01003689static void i915_gem_free_phys_object(struct drm_device *dev, int id)
Dave Airlie71acb5e2008-12-30 20:31:46 +10003690{
3691 drm_i915_private_t *dev_priv = dev->dev_private;
3692 struct drm_i915_gem_phys_object *phys_obj;
3693
3694 if (!dev_priv->mm.phys_objs[id - 1])
3695 return;
3696
3697 phys_obj = dev_priv->mm.phys_objs[id - 1];
3698 if (phys_obj->cur_obj) {
3699 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
3700 }
3701
3702#ifdef CONFIG_X86
3703 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
3704#endif
3705 drm_pci_free(dev, phys_obj->handle);
3706 kfree(phys_obj);
3707 dev_priv->mm.phys_objs[id - 1] = NULL;
3708}
3709
3710void i915_gem_free_all_phys_object(struct drm_device *dev)
3711{
3712 int i;
3713
Dave Airlie260883c2009-01-22 17:58:49 +10003714 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
Dave Airlie71acb5e2008-12-30 20:31:46 +10003715 i915_gem_free_phys_object(dev, i);
3716}
3717
3718void i915_gem_detach_phys_object(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00003719 struct drm_i915_gem_object *obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10003720{
Chris Wilson05394f32010-11-08 19:18:58 +00003721 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Chris Wilsone5281cc2010-10-28 13:45:36 +01003722 char *vaddr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10003723 int i;
Dave Airlie71acb5e2008-12-30 20:31:46 +10003724 int page_count;
3725
Chris Wilson05394f32010-11-08 19:18:58 +00003726 if (!obj->phys_obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10003727 return;
Chris Wilson05394f32010-11-08 19:18:58 +00003728 vaddr = obj->phys_obj->handle->vaddr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10003729
Chris Wilson05394f32010-11-08 19:18:58 +00003730 page_count = obj->base.size / PAGE_SIZE;
Dave Airlie71acb5e2008-12-30 20:31:46 +10003731 for (i = 0; i < page_count; i++) {
Hugh Dickins5949eac2011-06-27 16:18:18 -07003732 struct page *page = shmem_read_mapping_page(mapping, i);
Chris Wilsone5281cc2010-10-28 13:45:36 +01003733 if (!IS_ERR(page)) {
3734 char *dst = kmap_atomic(page);
3735 memcpy(dst, vaddr + i*PAGE_SIZE, PAGE_SIZE);
3736 kunmap_atomic(dst);
Dave Airlie71acb5e2008-12-30 20:31:46 +10003737
Chris Wilsone5281cc2010-10-28 13:45:36 +01003738 drm_clflush_pages(&page, 1);
3739
3740 set_page_dirty(page);
3741 mark_page_accessed(page);
3742 page_cache_release(page);
3743 }
Dave Airlie71acb5e2008-12-30 20:31:46 +10003744 }
Daniel Vetter40ce6572010-11-05 18:12:18 +01003745 intel_gtt_chipset_flush();
Chris Wilsond78b47b2009-06-17 21:52:49 +01003746
Chris Wilson05394f32010-11-08 19:18:58 +00003747 obj->phys_obj->cur_obj = NULL;
3748 obj->phys_obj = NULL;
Dave Airlie71acb5e2008-12-30 20:31:46 +10003749}
3750
3751int
3752i915_gem_attach_phys_object(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00003753 struct drm_i915_gem_object *obj,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01003754 int id,
3755 int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10003756{
Chris Wilson05394f32010-11-08 19:18:58 +00003757 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Dave Airlie71acb5e2008-12-30 20:31:46 +10003758 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie71acb5e2008-12-30 20:31:46 +10003759 int ret = 0;
3760 int page_count;
3761 int i;
3762
3763 if (id > I915_MAX_PHYS_OBJECT)
3764 return -EINVAL;
3765
Chris Wilson05394f32010-11-08 19:18:58 +00003766 if (obj->phys_obj) {
3767 if (obj->phys_obj->id == id)
Dave Airlie71acb5e2008-12-30 20:31:46 +10003768 return 0;
3769 i915_gem_detach_phys_object(dev, obj);
3770 }
3771
Dave Airlie71acb5e2008-12-30 20:31:46 +10003772 /* create a new object */
3773 if (!dev_priv->mm.phys_objs[id - 1]) {
3774 ret = i915_gem_init_phys_object(dev, id,
Chris Wilson05394f32010-11-08 19:18:58 +00003775 obj->base.size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10003776 if (ret) {
Chris Wilson05394f32010-11-08 19:18:58 +00003777 DRM_ERROR("failed to init phys object %d size: %zu\n",
3778 id, obj->base.size);
Chris Wilsone5281cc2010-10-28 13:45:36 +01003779 return ret;
Dave Airlie71acb5e2008-12-30 20:31:46 +10003780 }
3781 }
3782
3783 /* bind to the object */
Chris Wilson05394f32010-11-08 19:18:58 +00003784 obj->phys_obj = dev_priv->mm.phys_objs[id - 1];
3785 obj->phys_obj->cur_obj = obj;
Dave Airlie71acb5e2008-12-30 20:31:46 +10003786
Chris Wilson05394f32010-11-08 19:18:58 +00003787 page_count = obj->base.size / PAGE_SIZE;
Dave Airlie71acb5e2008-12-30 20:31:46 +10003788
3789 for (i = 0; i < page_count; i++) {
Chris Wilsone5281cc2010-10-28 13:45:36 +01003790 struct page *page;
3791 char *dst, *src;
Dave Airlie71acb5e2008-12-30 20:31:46 +10003792
Hugh Dickins5949eac2011-06-27 16:18:18 -07003793 page = shmem_read_mapping_page(mapping, i);
Chris Wilsone5281cc2010-10-28 13:45:36 +01003794 if (IS_ERR(page))
3795 return PTR_ERR(page);
3796
Chris Wilsonff75b9b2010-10-30 22:52:31 +01003797 src = kmap_atomic(page);
Chris Wilson05394f32010-11-08 19:18:58 +00003798 dst = obj->phys_obj->handle->vaddr + (i * PAGE_SIZE);
Dave Airlie71acb5e2008-12-30 20:31:46 +10003799 memcpy(dst, src, PAGE_SIZE);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -07003800 kunmap_atomic(src);
Chris Wilsone5281cc2010-10-28 13:45:36 +01003801
3802 mark_page_accessed(page);
3803 page_cache_release(page);
Dave Airlie71acb5e2008-12-30 20:31:46 +10003804 }
3805
3806 return 0;
Dave Airlie71acb5e2008-12-30 20:31:46 +10003807}
3808
3809static int
Chris Wilson05394f32010-11-08 19:18:58 +00003810i915_gem_phys_pwrite(struct drm_device *dev,
3811 struct drm_i915_gem_object *obj,
Dave Airlie71acb5e2008-12-30 20:31:46 +10003812 struct drm_i915_gem_pwrite *args,
3813 struct drm_file *file_priv)
3814{
Chris Wilson05394f32010-11-08 19:18:58 +00003815 void *vaddr = obj->phys_obj->handle->vaddr + args->offset;
Chris Wilsonb47b30c2010-11-08 01:12:29 +00003816 char __user *user_data = (char __user *) (uintptr_t) args->data_ptr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10003817
Chris Wilsonb47b30c2010-11-08 01:12:29 +00003818 if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) {
3819 unsigned long unwritten;
3820
3821 /* The physical object once assigned is fixed for the lifetime
3822 * of the obj, so we can safely drop the lock and continue
3823 * to access vaddr.
3824 */
3825 mutex_unlock(&dev->struct_mutex);
3826 unwritten = copy_from_user(vaddr, user_data, args->size);
3827 mutex_lock(&dev->struct_mutex);
3828 if (unwritten)
3829 return -EFAULT;
3830 }
Dave Airlie71acb5e2008-12-30 20:31:46 +10003831
Daniel Vetter40ce6572010-11-05 18:12:18 +01003832 intel_gtt_chipset_flush();
Dave Airlie71acb5e2008-12-30 20:31:46 +10003833 return 0;
3834}
Eric Anholtb9624422009-06-03 07:27:35 +00003835
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003836void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00003837{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003838 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00003839
3840 /* Clean up our request list when the client is going away, so that
3841 * later retire_requests won't dereference our soon-to-be-gone
3842 * file_priv.
3843 */
Chris Wilson1c255952010-09-26 11:03:27 +01003844 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003845 while (!list_empty(&file_priv->mm.request_list)) {
3846 struct drm_i915_gem_request *request;
3847
3848 request = list_first_entry(&file_priv->mm.request_list,
3849 struct drm_i915_gem_request,
3850 client_list);
3851 list_del(&request->client_list);
3852 request->file_priv = NULL;
3853 }
Chris Wilson1c255952010-09-26 11:03:27 +01003854 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00003855}
Chris Wilson31169712009-09-14 16:50:28 +01003856
Chris Wilson31169712009-09-14 16:50:28 +01003857static int
Chris Wilson1637ef42010-04-20 17:10:35 +01003858i915_gpu_is_active(struct drm_device *dev)
3859{
3860 drm_i915_private_t *dev_priv = dev->dev_private;
3861 int lists_empty;
3862
Chris Wilson1637ef42010-04-20 17:10:35 +01003863 lists_empty = list_empty(&dev_priv->mm.flushing_list) &&
Chris Wilson17250b72010-10-28 12:51:39 +01003864 list_empty(&dev_priv->mm.active_list);
Chris Wilson1637ef42010-04-20 17:10:35 +01003865
3866 return !lists_empty;
3867}
3868
3869static int
Ying Han1495f232011-05-24 17:12:27 -07003870i915_gem_inactive_shrink(struct shrinker *shrinker, struct shrink_control *sc)
Chris Wilson31169712009-09-14 16:50:28 +01003871{
Chris Wilson17250b72010-10-28 12:51:39 +01003872 struct drm_i915_private *dev_priv =
3873 container_of(shrinker,
3874 struct drm_i915_private,
3875 mm.inactive_shrinker);
3876 struct drm_device *dev = dev_priv->dev;
3877 struct drm_i915_gem_object *obj, *next;
Ying Han1495f232011-05-24 17:12:27 -07003878 int nr_to_scan = sc->nr_to_scan;
Chris Wilson17250b72010-10-28 12:51:39 +01003879 int cnt;
3880
3881 if (!mutex_trylock(&dev->struct_mutex))
Chris Wilsonbbe2e112010-10-28 22:35:07 +01003882 return 0;
Chris Wilson31169712009-09-14 16:50:28 +01003883
3884 /* "fast-path" to count number of available objects */
3885 if (nr_to_scan == 0) {
Chris Wilson17250b72010-10-28 12:51:39 +01003886 cnt = 0;
3887 list_for_each_entry(obj,
3888 &dev_priv->mm.inactive_list,
3889 mm_list)
3890 cnt++;
3891 mutex_unlock(&dev->struct_mutex);
3892 return cnt / 100 * sysctl_vfs_cache_pressure;
Chris Wilson31169712009-09-14 16:50:28 +01003893 }
3894
Chris Wilson1637ef42010-04-20 17:10:35 +01003895rescan:
Chris Wilson31169712009-09-14 16:50:28 +01003896 /* first scan for clean buffers */
Chris Wilson17250b72010-10-28 12:51:39 +01003897 i915_gem_retire_requests(dev);
Chris Wilson31169712009-09-14 16:50:28 +01003898
Chris Wilson17250b72010-10-28 12:51:39 +01003899 list_for_each_entry_safe(obj, next,
3900 &dev_priv->mm.inactive_list,
3901 mm_list) {
3902 if (i915_gem_object_is_purgeable(obj)) {
Chris Wilson20217462010-11-23 15:26:33 +00003903 if (i915_gem_object_unbind(obj) == 0 &&
3904 --nr_to_scan == 0)
Chris Wilson17250b72010-10-28 12:51:39 +01003905 break;
Chris Wilson31169712009-09-14 16:50:28 +01003906 }
Chris Wilson31169712009-09-14 16:50:28 +01003907 }
3908
3909 /* second pass, evict/count anything still on the inactive list */
Chris Wilson17250b72010-10-28 12:51:39 +01003910 cnt = 0;
3911 list_for_each_entry_safe(obj, next,
3912 &dev_priv->mm.inactive_list,
3913 mm_list) {
Chris Wilson20217462010-11-23 15:26:33 +00003914 if (nr_to_scan &&
3915 i915_gem_object_unbind(obj) == 0)
Chris Wilson17250b72010-10-28 12:51:39 +01003916 nr_to_scan--;
Chris Wilson20217462010-11-23 15:26:33 +00003917 else
Chris Wilson17250b72010-10-28 12:51:39 +01003918 cnt++;
Chris Wilson31169712009-09-14 16:50:28 +01003919 }
3920
Chris Wilson17250b72010-10-28 12:51:39 +01003921 if (nr_to_scan && i915_gpu_is_active(dev)) {
Chris Wilson1637ef42010-04-20 17:10:35 +01003922 /*
3923 * We are desperate for pages, so as a last resort, wait
3924 * for the GPU to finish and discard whatever we can.
3925 * This has a dramatic impact to reduce the number of
3926 * OOM-killer events whilst running the GPU aggressively.
3927 */
Ben Widawskyb93f9cf2012-01-25 15:39:34 -08003928 if (i915_gpu_idle(dev, true) == 0)
Chris Wilson1637ef42010-04-20 17:10:35 +01003929 goto rescan;
3930 }
Chris Wilson17250b72010-10-28 12:51:39 +01003931 mutex_unlock(&dev->struct_mutex);
3932 return cnt / 100 * sysctl_vfs_cache_pressure;
Chris Wilson31169712009-09-14 16:50:28 +01003933}