blob: 83dfb4407c8f78ae15f66badad5910fc5729c801 [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 Vetter58642882012-03-25 19:47:37 +0200573 int needs_clflush_after = 0;
574 int needs_clflush_before = 0;
Daniel Vetter692a5762012-03-25 19:47:34 +0200575 int release_page;
Eric Anholt40123c12009-03-09 13:42:30 -0700576
Daniel Vetter8c599672011-12-14 13:57:31 +0100577 user_data = (char __user *) (uintptr_t) args->data_ptr;
Eric Anholt40123c12009-03-09 13:42:30 -0700578 remain = args->size;
579
Daniel Vetter8c599672011-12-14 13:57:31 +0100580 obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700581
Daniel Vetter58642882012-03-25 19:47:37 +0200582 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
583 /* If we're not in the cpu write domain, set ourself into the gtt
584 * write domain and manually flush cachelines (if required). This
585 * optimizes for the case when the gpu will use the data
586 * right away and we therefore have to clflush anyway. */
587 if (obj->cache_level == I915_CACHE_NONE)
588 needs_clflush_after = 1;
589 ret = i915_gem_object_set_to_gtt_domain(obj, true);
590 if (ret)
591 return ret;
592 }
593 /* Same trick applies for invalidate partially written cachelines before
594 * writing. */
595 if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU)
596 && obj->cache_level == I915_CACHE_NONE)
597 needs_clflush_before = 1;
598
Eric Anholt40123c12009-03-09 13:42:30 -0700599 offset = args->offset;
Chris Wilson05394f32010-11-08 19:18:58 +0000600 obj->dirty = 1;
Eric Anholt40123c12009-03-09 13:42:30 -0700601
602 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100603 struct page *page;
Daniel Vetter8c599672011-12-14 13:57:31 +0100604 char *vaddr;
Daniel Vetter58642882012-03-25 19:47:37 +0200605 int partial_cacheline_write;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100606
Eric Anholt40123c12009-03-09 13:42:30 -0700607 /* Operation in this page
608 *
Eric Anholt40123c12009-03-09 13:42:30 -0700609 * shmem_page_offset = offset within page in shmem file
Eric Anholt40123c12009-03-09 13:42:30 -0700610 * page_length = bytes to copy for this page
611 */
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100612 shmem_page_offset = offset_in_page(offset);
Eric Anholt40123c12009-03-09 13:42:30 -0700613
614 page_length = remain;
615 if ((shmem_page_offset + page_length) > PAGE_SIZE)
616 page_length = PAGE_SIZE - shmem_page_offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700617
Daniel Vetter58642882012-03-25 19:47:37 +0200618 /* If we don't overwrite a cacheline completely we need to be
619 * careful to have up-to-date data by first clflushing. Don't
620 * overcomplicate things and flush the entire patch. */
621 partial_cacheline_write = needs_clflush_before &&
622 ((shmem_page_offset | page_length)
623 & (boot_cpu_data.x86_clflush_size - 1));
624
Daniel Vetter692a5762012-03-25 19:47:34 +0200625 if (obj->pages) {
626 page = obj->pages[offset >> PAGE_SHIFT];
627 release_page = 0;
628 } else {
629 page = shmem_read_mapping_page(mapping, offset >> PAGE_SHIFT);
630 if (IS_ERR(page)) {
631 ret = PTR_ERR(page);
632 goto out;
633 }
634 release_page = 1;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100635 }
636
Daniel Vetter8c599672011-12-14 13:57:31 +0100637 page_do_bit17_swizzling = obj_do_bit17_swizzling &&
638 (page_to_phys(page) & (1 << 17)) != 0;
639
Daniel Vettere244a442012-03-25 19:47:28 +0200640 if (!page_do_bit17_swizzling) {
641 vaddr = kmap_atomic(page);
Daniel Vetter58642882012-03-25 19:47:37 +0200642 if (partial_cacheline_write)
643 drm_clflush_virt_range(vaddr + shmem_page_offset,
644 page_length);
Daniel Vettere244a442012-03-25 19:47:28 +0200645 ret = __copy_from_user_inatomic(vaddr + shmem_page_offset,
646 user_data,
647 page_length);
Daniel Vetter58642882012-03-25 19:47:37 +0200648 if (needs_clflush_after)
649 drm_clflush_virt_range(vaddr + shmem_page_offset,
650 page_length);
Daniel Vettere244a442012-03-25 19:47:28 +0200651 kunmap_atomic(vaddr);
652
653 if (ret == 0)
654 goto next_page;
655 }
656
657 hit_slowpath = 1;
Daniel Vetter692a5762012-03-25 19:47:34 +0200658 page_cache_get(page);
Daniel Vettere244a442012-03-25 19:47:28 +0200659 mutex_unlock(&dev->struct_mutex);
660
Daniel Vetter8c599672011-12-14 13:57:31 +0100661 vaddr = kmap(page);
Daniel Vetter58642882012-03-25 19:47:37 +0200662 if (partial_cacheline_write)
663 drm_clflush_virt_range(vaddr + shmem_page_offset,
664 page_length);
Daniel Vetter8c599672011-12-14 13:57:31 +0100665 if (page_do_bit17_swizzling)
666 ret = __copy_from_user_swizzled(vaddr, shmem_page_offset,
667 user_data,
668 page_length);
669 else
670 ret = __copy_from_user(vaddr + shmem_page_offset,
671 user_data,
672 page_length);
Daniel Vetter58642882012-03-25 19:47:37 +0200673 if (needs_clflush_after)
674 drm_clflush_virt_range(vaddr + shmem_page_offset,
675 page_length);
Daniel Vetter8c599672011-12-14 13:57:31 +0100676 kunmap(page);
Eric Anholt40123c12009-03-09 13:42:30 -0700677
Daniel Vettere244a442012-03-25 19:47:28 +0200678 mutex_lock(&dev->struct_mutex);
Daniel Vetter692a5762012-03-25 19:47:34 +0200679 page_cache_release(page);
Daniel Vettere244a442012-03-25 19:47:28 +0200680next_page:
Chris Wilsone5281cc2010-10-28 13:45:36 +0100681 set_page_dirty(page);
682 mark_page_accessed(page);
Daniel Vetter692a5762012-03-25 19:47:34 +0200683 if (release_page)
684 page_cache_release(page);
Chris Wilsone5281cc2010-10-28 13:45:36 +0100685
Daniel Vetter8c599672011-12-14 13:57:31 +0100686 if (ret) {
687 ret = -EFAULT;
688 goto out;
689 }
690
Eric Anholt40123c12009-03-09 13:42:30 -0700691 remain -= page_length;
Daniel Vetter8c599672011-12-14 13:57:31 +0100692 user_data += page_length;
Eric Anholt40123c12009-03-09 13:42:30 -0700693 offset += page_length;
694 }
695
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100696out:
Daniel Vettere244a442012-03-25 19:47:28 +0200697 if (hit_slowpath) {
698 /* Fixup: Kill any reinstated backing storage pages */
699 if (obj->madv == __I915_MADV_PURGED)
700 i915_gem_object_truncate(obj);
701 /* and flush dirty cachelines in case the object isn't in the cpu write
702 * domain anymore. */
703 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
704 i915_gem_clflush_object(obj);
705 intel_gtt_chipset_flush();
706 }
Daniel Vetter8c599672011-12-14 13:57:31 +0100707 }
Eric Anholt40123c12009-03-09 13:42:30 -0700708
Daniel Vetter58642882012-03-25 19:47:37 +0200709 if (needs_clflush_after)
710 intel_gtt_chipset_flush();
711
Eric Anholt40123c12009-03-09 13:42:30 -0700712 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700713}
714
715/**
716 * Writes data to the object referenced by handle.
717 *
718 * On error, the contents of the buffer that were to be modified are undefined.
719 */
720int
721i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100722 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700723{
724 struct drm_i915_gem_pwrite *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +0000725 struct drm_i915_gem_object *obj;
Chris Wilson51311d02010-11-17 09:10:42 +0000726 int ret;
727
728 if (args->size == 0)
729 return 0;
730
731 if (!access_ok(VERIFY_READ,
732 (char __user *)(uintptr_t)args->data_ptr,
733 args->size))
734 return -EFAULT;
735
736 ret = fault_in_pages_readable((char __user *)(uintptr_t)args->data_ptr,
737 args->size);
738 if (ret)
739 return -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -0700740
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100741 ret = i915_mutex_lock_interruptible(dev);
742 if (ret)
743 return ret;
744
Chris Wilson05394f32010-11-08 19:18:58 +0000745 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +0000746 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100747 ret = -ENOENT;
748 goto unlock;
749 }
Eric Anholt673a3942008-07-30 12:06:12 -0700750
Chris Wilson7dcd2492010-09-26 20:21:44 +0100751 /* Bounds check destination. */
Chris Wilson05394f32010-11-08 19:18:58 +0000752 if (args->offset > obj->base.size ||
753 args->size > obj->base.size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +0100754 ret = -EINVAL;
Chris Wilson35b62a82010-09-26 20:23:38 +0100755 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +0100756 }
757
Chris Wilsondb53a302011-02-03 11:57:46 +0000758 trace_i915_gem_object_pwrite(obj, args->offset, args->size);
759
Daniel Vetter935aaa62012-03-25 19:47:35 +0200760 ret = -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -0700761 /* We can only do the GTT pwrite on untiled buffers, as otherwise
762 * it would end up going through the fenced access, and we'll get
763 * different detiling behavior between reading and writing.
764 * pread/pwrite currently are reading and writing from the CPU
765 * perspective, requiring manual detiling by the client.
766 */
Daniel Vetter5c0480f2011-12-14 13:57:30 +0100767 if (obj->phys_obj) {
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100768 ret = i915_gem_phys_pwrite(dev, obj, args, file);
Daniel Vetter5c0480f2011-12-14 13:57:30 +0100769 goto out;
770 }
771
772 if (obj->gtt_space &&
Daniel Vetter3ae53782012-03-25 19:47:33 +0200773 obj->cache_level == I915_CACHE_NONE &&
Daniel Vetter5c0480f2011-12-14 13:57:30 +0100774 obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100775 ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file);
Daniel Vetter935aaa62012-03-25 19:47:35 +0200776 /* Note that the gtt paths might fail with non-page-backed user
777 * pointers (e.g. gtt mappings when moving data between
778 * textures). Fallback to the shmem path in that case. */
Eric Anholt40123c12009-03-09 13:42:30 -0700779 }
Eric Anholt673a3942008-07-30 12:06:12 -0700780
Daniel Vetter935aaa62012-03-25 19:47:35 +0200781 if (ret == -EFAULT)
782 ret = i915_gem_shmem_pwrite(dev, obj, args, file);
Daniel Vetter5c0480f2011-12-14 13:57:30 +0100783
Chris Wilson35b62a82010-09-26 20:23:38 +0100784out:
Chris Wilson05394f32010-11-08 19:18:58 +0000785 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100786unlock:
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100787 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -0700788 return ret;
789}
790
791/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800792 * Called when user space prepares to use an object with the CPU, either
793 * through the mmap ioctl's mapping or a GTT mapping.
Eric Anholt673a3942008-07-30 12:06:12 -0700794 */
795int
796i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000797 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700798{
799 struct drm_i915_gem_set_domain *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +0000800 struct drm_i915_gem_object *obj;
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800801 uint32_t read_domains = args->read_domains;
802 uint32_t write_domain = args->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -0700803 int ret;
804
805 if (!(dev->driver->driver_features & DRIVER_GEM))
806 return -ENODEV;
807
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800808 /* Only handle setting domains to types used by the CPU. */
Chris Wilson21d509e2009-06-06 09:46:02 +0100809 if (write_domain & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800810 return -EINVAL;
811
Chris Wilson21d509e2009-06-06 09:46:02 +0100812 if (read_domains & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800813 return -EINVAL;
814
815 /* Having something in the write domain implies it's in the read
816 * domain, and only that read domain. Enforce that in the request.
817 */
818 if (write_domain != 0 && read_domains != write_domain)
819 return -EINVAL;
820
Chris Wilson76c1dec2010-09-25 11:22:51 +0100821 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100822 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +0100823 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700824
Chris Wilson05394f32010-11-08 19:18:58 +0000825 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +0000826 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100827 ret = -ENOENT;
828 goto unlock;
Chris Wilson76c1dec2010-09-25 11:22:51 +0100829 }
Jesse Barnes652c3932009-08-17 13:31:43 -0700830
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800831 if (read_domains & I915_GEM_DOMAIN_GTT) {
832 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
Eric Anholt02354392008-11-26 13:58:13 -0800833
834 /* Silently promote "you're not bound, there was nothing to do"
835 * to success, since the client was just asking us to
836 * make sure everything was done.
837 */
838 if (ret == -EINVAL)
839 ret = 0;
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800840 } else {
Eric Anholte47c68e2008-11-14 13:35:19 -0800841 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800842 }
843
Chris Wilson05394f32010-11-08 19:18:58 +0000844 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100845unlock:
Eric Anholt673a3942008-07-30 12:06:12 -0700846 mutex_unlock(&dev->struct_mutex);
847 return ret;
848}
849
850/**
851 * Called when user space has done writes to this buffer
852 */
853int
854i915_gem_sw_finish_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_sw_finish *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +0000858 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -0700859 int ret = 0;
860
861 if (!(dev->driver->driver_features & DRIVER_GEM))
862 return -ENODEV;
863
Chris Wilson76c1dec2010-09-25 11:22:51 +0100864 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100865 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +0100866 return ret;
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100867
Chris Wilson05394f32010-11-08 19:18:58 +0000868 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +0000869 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100870 ret = -ENOENT;
871 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -0700872 }
873
Eric Anholt673a3942008-07-30 12:06:12 -0700874 /* Pinned buffers may be scanout, so flush the cache */
Chris Wilson05394f32010-11-08 19:18:58 +0000875 if (obj->pin_count)
Eric Anholte47c68e2008-11-14 13:35:19 -0800876 i915_gem_object_flush_cpu_write_domain(obj);
877
Chris Wilson05394f32010-11-08 19:18:58 +0000878 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100879unlock:
Eric Anholt673a3942008-07-30 12:06:12 -0700880 mutex_unlock(&dev->struct_mutex);
881 return ret;
882}
883
884/**
885 * Maps the contents of an object, returning the address it is mapped
886 * into.
887 *
888 * While the mapping holds a reference on the contents of the object, it doesn't
889 * imply a ref on the object itself.
890 */
891int
892i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000893 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700894{
895 struct drm_i915_gem_mmap *args = data;
896 struct drm_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -0700897 unsigned long addr;
898
899 if (!(dev->driver->driver_features & DRIVER_GEM))
900 return -ENODEV;
901
Chris Wilson05394f32010-11-08 19:18:58 +0000902 obj = drm_gem_object_lookup(dev, file, args->handle);
Eric Anholt673a3942008-07-30 12:06:12 -0700903 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100904 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -0700905
Eric Anholt673a3942008-07-30 12:06:12 -0700906 down_write(&current->mm->mmap_sem);
907 addr = do_mmap(obj->filp, 0, args->size,
908 PROT_READ | PROT_WRITE, MAP_SHARED,
909 args->offset);
910 up_write(&current->mm->mmap_sem);
Luca Barbieribc9025b2010-02-09 05:49:12 +0000911 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700912 if (IS_ERR((void *)addr))
913 return addr;
914
915 args->addr_ptr = (uint64_t) addr;
916
917 return 0;
918}
919
Jesse Barnesde151cf2008-11-12 10:03:55 -0800920/**
921 * i915_gem_fault - fault a page into the GTT
922 * vma: VMA in question
923 * vmf: fault info
924 *
925 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
926 * from userspace. The fault handler takes care of binding the object to
927 * the GTT (if needed), allocating and programming a fence register (again,
928 * only if needed based on whether the old reg is still valid or the object
929 * is tiled) and inserting a new PTE into the faulting process.
930 *
931 * Note that the faulting process may involve evicting existing objects
932 * from the GTT and/or fence registers to make room. So performance may
933 * suffer if the GTT working set is large or there are few fence registers
934 * left.
935 */
936int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
937{
Chris Wilson05394f32010-11-08 19:18:58 +0000938 struct drm_i915_gem_object *obj = to_intel_bo(vma->vm_private_data);
939 struct drm_device *dev = obj->base.dev;
Chris Wilson7d1c4802010-08-07 21:45:03 +0100940 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -0800941 pgoff_t page_offset;
942 unsigned long pfn;
943 int ret = 0;
Jesse Barnes0f973f22009-01-26 17:10:45 -0800944 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Jesse Barnesde151cf2008-11-12 10:03:55 -0800945
946 /* We don't use vmf->pgoff since that has the fake offset */
947 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
948 PAGE_SHIFT;
949
Chris Wilsond9bc7e92011-02-07 13:09:31 +0000950 ret = i915_mutex_lock_interruptible(dev);
951 if (ret)
952 goto out;
Chris Wilsona00b10c2010-09-24 21:15:47 +0100953
Chris Wilsondb53a302011-02-03 11:57:46 +0000954 trace_i915_gem_object_fault(obj, page_offset, true, write);
955
Chris Wilsond9bc7e92011-02-07 13:09:31 +0000956 /* Now bind it into the GTT if needed */
Chris Wilson919926a2010-11-12 13:42:53 +0000957 if (!obj->map_and_fenceable) {
958 ret = i915_gem_object_unbind(obj);
959 if (ret)
960 goto unlock;
Chris Wilsona00b10c2010-09-24 21:15:47 +0100961 }
Chris Wilson05394f32010-11-08 19:18:58 +0000962 if (!obj->gtt_space) {
Daniel Vetter75e9e912010-11-04 17:11:09 +0100963 ret = i915_gem_object_bind_to_gtt(obj, 0, true);
Chris Wilsonc7150892009-09-23 00:43:56 +0100964 if (ret)
965 goto unlock;
Jesse Barnesde151cf2008-11-12 10:03:55 -0800966
Eric Anholte92d03b2011-06-14 16:43:09 -0700967 ret = i915_gem_object_set_to_gtt_domain(obj, write);
968 if (ret)
969 goto unlock;
970 }
Chris Wilson4a684a42010-10-28 14:44:08 +0100971
Daniel Vetter74898d72012-02-15 23:50:22 +0100972 if (!obj->has_global_gtt_mapping)
973 i915_gem_gtt_bind_object(obj, obj->cache_level);
974
Chris Wilsond9e86c02010-11-10 16:40:20 +0000975 if (obj->tiling_mode == I915_TILING_NONE)
976 ret = i915_gem_object_put_fence(obj);
977 else
Chris Wilsonce453d82011-02-21 14:43:56 +0000978 ret = i915_gem_object_get_fence(obj, NULL);
Chris Wilsond9e86c02010-11-10 16:40:20 +0000979 if (ret)
980 goto unlock;
Jesse Barnesde151cf2008-11-12 10:03:55 -0800981
Chris Wilson05394f32010-11-08 19:18:58 +0000982 if (i915_gem_object_is_inactive(obj))
983 list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
Chris Wilson7d1c4802010-08-07 21:45:03 +0100984
Chris Wilson6299f992010-11-24 12:23:44 +0000985 obj->fault_mappable = true;
986
Chris Wilson05394f32010-11-08 19:18:58 +0000987 pfn = ((dev->agp->base + obj->gtt_offset) >> PAGE_SHIFT) +
Jesse Barnesde151cf2008-11-12 10:03:55 -0800988 page_offset;
989
990 /* Finally, remap it using the new GTT offset */
991 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
Chris Wilsonc7150892009-09-23 00:43:56 +0100992unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -0800993 mutex_unlock(&dev->struct_mutex);
Chris Wilsond9bc7e92011-02-07 13:09:31 +0000994out:
Jesse Barnesde151cf2008-11-12 10:03:55 -0800995 switch (ret) {
Chris Wilsond9bc7e92011-02-07 13:09:31 +0000996 case -EIO:
Chris Wilson045e7692010-11-07 09:18:22 +0000997 case -EAGAIN:
Chris Wilsond9bc7e92011-02-07 13:09:31 +0000998 /* Give the error handler a chance to run and move the
999 * objects off the GPU active list. Next time we service the
1000 * fault, we should be able to transition the page into the
1001 * GTT without touching the GPU (and so avoid further
1002 * EIO/EGAIN). If the GPU is wedged, then there is no issue
1003 * with coherency, just lost writes.
1004 */
Chris Wilson045e7692010-11-07 09:18:22 +00001005 set_need_resched();
Chris Wilsonc7150892009-09-23 00:43:56 +01001006 case 0:
1007 case -ERESTARTSYS:
Chris Wilsonbed636a2011-02-11 20:31:19 +00001008 case -EINTR:
Chris Wilsonc7150892009-09-23 00:43:56 +01001009 return VM_FAULT_NOPAGE;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001010 case -ENOMEM:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001011 return VM_FAULT_OOM;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001012 default:
Chris Wilsonc7150892009-09-23 00:43:56 +01001013 return VM_FAULT_SIGBUS;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001014 }
1015}
1016
1017/**
Chris Wilson901782b2009-07-10 08:18:50 +01001018 * i915_gem_release_mmap - remove physical page mappings
1019 * @obj: obj in question
1020 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001021 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01001022 * relinquish ownership of the pages back to the system.
1023 *
1024 * It is vital that we remove the page mapping if we have mapped a tiled
1025 * object through the GTT and then lose the fence register due to
1026 * resource pressure. Similarly if the object has been moved out of the
1027 * aperture, than pages mapped into userspace must be revoked. Removing the
1028 * mapping will then trigger a page fault on the next user access, allowing
1029 * fixup by i915_gem_fault().
1030 */
Eric Anholtd05ca302009-07-10 13:02:26 -07001031void
Chris Wilson05394f32010-11-08 19:18:58 +00001032i915_gem_release_mmap(struct drm_i915_gem_object *obj)
Chris Wilson901782b2009-07-10 08:18:50 +01001033{
Chris Wilson6299f992010-11-24 12:23:44 +00001034 if (!obj->fault_mappable)
1035 return;
Chris Wilson901782b2009-07-10 08:18:50 +01001036
Chris Wilsonf6e47882011-03-20 21:09:12 +00001037 if (obj->base.dev->dev_mapping)
1038 unmap_mapping_range(obj->base.dev->dev_mapping,
1039 (loff_t)obj->base.map_list.hash.key<<PAGE_SHIFT,
1040 obj->base.size, 1);
Daniel Vetterfb7d5162010-10-01 22:05:20 +02001041
Chris Wilson6299f992010-11-24 12:23:44 +00001042 obj->fault_mappable = false;
Chris Wilson901782b2009-07-10 08:18:50 +01001043}
1044
Chris Wilson92b88ae2010-11-09 11:47:32 +00001045static uint32_t
Chris Wilsone28f8712011-07-18 13:11:49 -07001046i915_gem_get_gtt_size(struct drm_device *dev, uint32_t size, int tiling_mode)
Chris Wilson92b88ae2010-11-09 11:47:32 +00001047{
Chris Wilsone28f8712011-07-18 13:11:49 -07001048 uint32_t gtt_size;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001049
1050 if (INTEL_INFO(dev)->gen >= 4 ||
Chris Wilsone28f8712011-07-18 13:11:49 -07001051 tiling_mode == I915_TILING_NONE)
1052 return size;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001053
1054 /* Previous chips need a power-of-two fence region when tiling */
1055 if (INTEL_INFO(dev)->gen == 3)
Chris Wilsone28f8712011-07-18 13:11:49 -07001056 gtt_size = 1024*1024;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001057 else
Chris Wilsone28f8712011-07-18 13:11:49 -07001058 gtt_size = 512*1024;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001059
Chris Wilsone28f8712011-07-18 13:11:49 -07001060 while (gtt_size < size)
1061 gtt_size <<= 1;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001062
Chris Wilsone28f8712011-07-18 13:11:49 -07001063 return gtt_size;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001064}
1065
Jesse Barnesde151cf2008-11-12 10:03:55 -08001066/**
1067 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1068 * @obj: object to check
1069 *
1070 * Return the required GTT alignment for an object, taking into account
Daniel Vetter5e783302010-11-14 22:32:36 +01001071 * potential fence register mapping.
Jesse Barnesde151cf2008-11-12 10:03:55 -08001072 */
1073static uint32_t
Chris Wilsone28f8712011-07-18 13:11:49 -07001074i915_gem_get_gtt_alignment(struct drm_device *dev,
1075 uint32_t size,
1076 int tiling_mode)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001077{
Jesse Barnesde151cf2008-11-12 10:03:55 -08001078 /*
1079 * Minimum alignment is 4k (GTT page size), but might be greater
1080 * if a fence register is needed for the object.
1081 */
Chris Wilsona00b10c2010-09-24 21:15:47 +01001082 if (INTEL_INFO(dev)->gen >= 4 ||
Chris Wilsone28f8712011-07-18 13:11:49 -07001083 tiling_mode == I915_TILING_NONE)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001084 return 4096;
1085
1086 /*
1087 * Previous chips need to be aligned to the size of the smallest
1088 * fence register that can contain the object.
1089 */
Chris Wilsone28f8712011-07-18 13:11:49 -07001090 return i915_gem_get_gtt_size(dev, size, tiling_mode);
Chris Wilsona00b10c2010-09-24 21:15:47 +01001091}
1092
Daniel Vetter5e783302010-11-14 22:32:36 +01001093/**
1094 * i915_gem_get_unfenced_gtt_alignment - return required GTT alignment for an
1095 * unfenced object
Chris Wilsone28f8712011-07-18 13:11:49 -07001096 * @dev: the device
1097 * @size: size of the object
1098 * @tiling_mode: tiling mode of the object
Daniel Vetter5e783302010-11-14 22:32:36 +01001099 *
1100 * Return the required GTT alignment for an object, only taking into account
1101 * unfenced tiled surface requirements.
1102 */
Chris Wilson467cffb2011-03-07 10:42:03 +00001103uint32_t
Chris Wilsone28f8712011-07-18 13:11:49 -07001104i915_gem_get_unfenced_gtt_alignment(struct drm_device *dev,
1105 uint32_t size,
1106 int tiling_mode)
Daniel Vetter5e783302010-11-14 22:32:36 +01001107{
Daniel Vetter5e783302010-11-14 22:32:36 +01001108 /*
1109 * Minimum alignment is 4k (GTT page size) for sane hw.
1110 */
1111 if (INTEL_INFO(dev)->gen >= 4 || IS_G33(dev) ||
Chris Wilsone28f8712011-07-18 13:11:49 -07001112 tiling_mode == I915_TILING_NONE)
Daniel Vetter5e783302010-11-14 22:32:36 +01001113 return 4096;
1114
Chris Wilsone28f8712011-07-18 13:11:49 -07001115 /* Previous hardware however needs to be aligned to a power-of-two
1116 * tile height. The simplest method for determining this is to reuse
1117 * the power-of-tile object size.
Daniel Vetter5e783302010-11-14 22:32:36 +01001118 */
Chris Wilsone28f8712011-07-18 13:11:49 -07001119 return i915_gem_get_gtt_size(dev, size, tiling_mode);
Daniel Vetter5e783302010-11-14 22:32:36 +01001120}
1121
Jesse Barnesde151cf2008-11-12 10:03:55 -08001122int
Dave Airlieff72145b2011-02-07 12:16:14 +10001123i915_gem_mmap_gtt(struct drm_file *file,
1124 struct drm_device *dev,
1125 uint32_t handle,
1126 uint64_t *offset)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001127{
Chris Wilsonda761a62010-10-27 17:37:08 +01001128 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00001129 struct drm_i915_gem_object *obj;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001130 int ret;
1131
1132 if (!(dev->driver->driver_features & DRIVER_GEM))
1133 return -ENODEV;
1134
Chris Wilson76c1dec2010-09-25 11:22:51 +01001135 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001136 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001137 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001138
Dave Airlieff72145b2011-02-07 12:16:14 +10001139 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00001140 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001141 ret = -ENOENT;
1142 goto unlock;
1143 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001144
Chris Wilson05394f32010-11-08 19:18:58 +00001145 if (obj->base.size > dev_priv->mm.gtt_mappable_end) {
Chris Wilsonda761a62010-10-27 17:37:08 +01001146 ret = -E2BIG;
Eric Anholtff56b0b2011-10-31 23:16:21 -07001147 goto out;
Chris Wilsonda761a62010-10-27 17:37:08 +01001148 }
1149
Chris Wilson05394f32010-11-08 19:18:58 +00001150 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilsonab182822009-09-22 18:46:17 +01001151 DRM_ERROR("Attempting to mmap a purgeable buffer\n");
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001152 ret = -EINVAL;
1153 goto out;
Chris Wilsonab182822009-09-22 18:46:17 +01001154 }
1155
Chris Wilson05394f32010-11-08 19:18:58 +00001156 if (!obj->base.map_list.map) {
Rob Clarkb464e9a2011-08-10 08:09:08 -05001157 ret = drm_gem_create_mmap_offset(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001158 if (ret)
1159 goto out;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001160 }
1161
Dave Airlieff72145b2011-02-07 12:16:14 +10001162 *offset = (u64)obj->base.map_list.hash.key << PAGE_SHIFT;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001163
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001164out:
Chris Wilson05394f32010-11-08 19:18:58 +00001165 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001166unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001167 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001168 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001169}
1170
Dave Airlieff72145b2011-02-07 12:16:14 +10001171/**
1172 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1173 * @dev: DRM device
1174 * @data: GTT mapping ioctl data
1175 * @file: GEM object info
1176 *
1177 * Simply returns the fake offset to userspace so it can mmap it.
1178 * The mmap call will end up in drm_gem_mmap(), which will set things
1179 * up so we can get faults in the handler above.
1180 *
1181 * The fault handler will take care of binding the object into the GTT
1182 * (since it may have been evicted to make room for something), allocating
1183 * a fence register, and mapping the appropriate aperture address into
1184 * userspace.
1185 */
1186int
1187i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
1188 struct drm_file *file)
1189{
1190 struct drm_i915_gem_mmap_gtt *args = data;
1191
1192 if (!(dev->driver->driver_features & DRIVER_GEM))
1193 return -ENODEV;
1194
1195 return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
1196}
1197
1198
Chris Wilsone5281cc2010-10-28 13:45:36 +01001199static int
Chris Wilson05394f32010-11-08 19:18:58 +00001200i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj,
Chris Wilsone5281cc2010-10-28 13:45:36 +01001201 gfp_t gfpmask)
1202{
Chris Wilsone5281cc2010-10-28 13:45:36 +01001203 int page_count, i;
1204 struct address_space *mapping;
1205 struct inode *inode;
1206 struct page *page;
1207
1208 /* Get the list of pages out of our struct file. They'll be pinned
1209 * at this point until we release them.
1210 */
Chris Wilson05394f32010-11-08 19:18:58 +00001211 page_count = obj->base.size / PAGE_SIZE;
1212 BUG_ON(obj->pages != NULL);
1213 obj->pages = drm_malloc_ab(page_count, sizeof(struct page *));
1214 if (obj->pages == NULL)
Chris Wilsone5281cc2010-10-28 13:45:36 +01001215 return -ENOMEM;
1216
Chris Wilson05394f32010-11-08 19:18:58 +00001217 inode = obj->base.filp->f_path.dentry->d_inode;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001218 mapping = inode->i_mapping;
Hugh Dickins5949eac2011-06-27 16:18:18 -07001219 gfpmask |= mapping_gfp_mask(mapping);
1220
Chris Wilsone5281cc2010-10-28 13:45:36 +01001221 for (i = 0; i < page_count; i++) {
Hugh Dickins5949eac2011-06-27 16:18:18 -07001222 page = shmem_read_mapping_page_gfp(mapping, i, gfpmask);
Chris Wilsone5281cc2010-10-28 13:45:36 +01001223 if (IS_ERR(page))
1224 goto err_pages;
1225
Chris Wilson05394f32010-11-08 19:18:58 +00001226 obj->pages[i] = page;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001227 }
1228
Daniel Vetter6dacfd22011-09-12 21:30:02 +02001229 if (i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilsone5281cc2010-10-28 13:45:36 +01001230 i915_gem_object_do_bit_17_swizzle(obj);
1231
1232 return 0;
1233
1234err_pages:
1235 while (i--)
Chris Wilson05394f32010-11-08 19:18:58 +00001236 page_cache_release(obj->pages[i]);
Chris Wilsone5281cc2010-10-28 13:45:36 +01001237
Chris Wilson05394f32010-11-08 19:18:58 +00001238 drm_free_large(obj->pages);
1239 obj->pages = NULL;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001240 return PTR_ERR(page);
1241}
1242
Chris Wilson5cdf5882010-09-27 15:51:07 +01001243static void
Chris Wilson05394f32010-11-08 19:18:58 +00001244i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07001245{
Chris Wilson05394f32010-11-08 19:18:58 +00001246 int page_count = obj->base.size / PAGE_SIZE;
Eric Anholt673a3942008-07-30 12:06:12 -07001247 int i;
1248
Chris Wilson05394f32010-11-08 19:18:58 +00001249 BUG_ON(obj->madv == __I915_MADV_PURGED);
Eric Anholt856fa192009-03-19 14:10:50 -07001250
Daniel Vetter6dacfd22011-09-12 21:30:02 +02001251 if (i915_gem_object_needs_bit17_swizzle(obj))
Eric Anholt280b7132009-03-12 16:56:27 -07001252 i915_gem_object_save_bit_17_swizzle(obj);
1253
Chris Wilson05394f32010-11-08 19:18:58 +00001254 if (obj->madv == I915_MADV_DONTNEED)
1255 obj->dirty = 0;
Chris Wilson3ef94da2009-09-14 16:50:29 +01001256
1257 for (i = 0; i < page_count; i++) {
Chris Wilson05394f32010-11-08 19:18:58 +00001258 if (obj->dirty)
1259 set_page_dirty(obj->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001260
Chris Wilson05394f32010-11-08 19:18:58 +00001261 if (obj->madv == I915_MADV_WILLNEED)
1262 mark_page_accessed(obj->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001263
Chris Wilson05394f32010-11-08 19:18:58 +00001264 page_cache_release(obj->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001265 }
Chris Wilson05394f32010-11-08 19:18:58 +00001266 obj->dirty = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001267
Chris Wilson05394f32010-11-08 19:18:58 +00001268 drm_free_large(obj->pages);
1269 obj->pages = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001270}
1271
Chris Wilson54cf91d2010-11-25 18:00:26 +00001272void
Chris Wilson05394f32010-11-08 19:18:58 +00001273i915_gem_object_move_to_active(struct drm_i915_gem_object *obj,
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001274 struct intel_ring_buffer *ring,
1275 u32 seqno)
Eric Anholt673a3942008-07-30 12:06:12 -07001276{
Chris Wilson05394f32010-11-08 19:18:58 +00001277 struct drm_device *dev = obj->base.dev;
Chris Wilson69dc4982010-10-19 10:36:51 +01001278 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter617dbe22010-02-11 22:16:02 +01001279
Zou Nan hai852835f2010-05-21 09:08:56 +08001280 BUG_ON(ring == NULL);
Chris Wilson05394f32010-11-08 19:18:58 +00001281 obj->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001282
1283 /* Add a reference if we're newly entering the active list. */
Chris Wilson05394f32010-11-08 19:18:58 +00001284 if (!obj->active) {
1285 drm_gem_object_reference(&obj->base);
1286 obj->active = 1;
Eric Anholt673a3942008-07-30 12:06:12 -07001287 }
Daniel Vettere35a41d2010-02-11 22:13:59 +01001288
Eric Anholt673a3942008-07-30 12:06:12 -07001289 /* Move from whatever list we were on to the tail of execution. */
Chris Wilson05394f32010-11-08 19:18:58 +00001290 list_move_tail(&obj->mm_list, &dev_priv->mm.active_list);
1291 list_move_tail(&obj->ring_list, &ring->active_list);
Chris Wilsoncaea7472010-11-12 13:53:37 +00001292
Chris Wilson05394f32010-11-08 19:18:58 +00001293 obj->last_rendering_seqno = seqno;
Chris Wilsoncaea7472010-11-12 13:53:37 +00001294 if (obj->fenced_gpu_access) {
1295 struct drm_i915_fence_reg *reg;
1296
1297 BUG_ON(obj->fence_reg == I915_FENCE_REG_NONE);
1298
1299 obj->last_fenced_seqno = seqno;
1300 obj->last_fenced_ring = ring;
1301
1302 reg = &dev_priv->fence_regs[obj->fence_reg];
1303 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
1304 }
1305}
1306
1307static void
1308i915_gem_object_move_off_active(struct drm_i915_gem_object *obj)
1309{
1310 list_del_init(&obj->ring_list);
1311 obj->last_rendering_seqno = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001312}
1313
Eric Anholtce44b0e2008-11-06 16:00:31 -08001314static void
Chris Wilson05394f32010-11-08 19:18:58 +00001315i915_gem_object_move_to_flushing(struct drm_i915_gem_object *obj)
Eric Anholtce44b0e2008-11-06 16:00:31 -08001316{
Chris Wilson05394f32010-11-08 19:18:58 +00001317 struct drm_device *dev = obj->base.dev;
Eric Anholtce44b0e2008-11-06 16:00:31 -08001318 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtce44b0e2008-11-06 16:00:31 -08001319
Chris Wilson05394f32010-11-08 19:18:58 +00001320 BUG_ON(!obj->active);
1321 list_move_tail(&obj->mm_list, &dev_priv->mm.flushing_list);
Chris Wilsoncaea7472010-11-12 13:53:37 +00001322
1323 i915_gem_object_move_off_active(obj);
1324}
1325
1326static void
1327i915_gem_object_move_to_inactive(struct drm_i915_gem_object *obj)
1328{
1329 struct drm_device *dev = obj->base.dev;
1330 struct drm_i915_private *dev_priv = dev->dev_private;
1331
1332 if (obj->pin_count != 0)
1333 list_move_tail(&obj->mm_list, &dev_priv->mm.pinned_list);
1334 else
1335 list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
1336
1337 BUG_ON(!list_empty(&obj->gpu_write_list));
1338 BUG_ON(!obj->active);
1339 obj->ring = NULL;
1340
1341 i915_gem_object_move_off_active(obj);
1342 obj->fenced_gpu_access = false;
Chris Wilsoncaea7472010-11-12 13:53:37 +00001343
1344 obj->active = 0;
Chris Wilson87ca9c82010-12-02 09:42:56 +00001345 obj->pending_gpu_write = false;
Chris Wilsoncaea7472010-11-12 13:53:37 +00001346 drm_gem_object_unreference(&obj->base);
1347
1348 WARN_ON(i915_verify_lists(dev));
Eric Anholtce44b0e2008-11-06 16:00:31 -08001349}
Eric Anholt673a3942008-07-30 12:06:12 -07001350
Chris Wilson963b4832009-09-20 23:03:54 +01001351/* Immediately discard the backing storage */
1352static void
Chris Wilson05394f32010-11-08 19:18:58 +00001353i915_gem_object_truncate(struct drm_i915_gem_object *obj)
Chris Wilson963b4832009-09-20 23:03:54 +01001354{
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001355 struct inode *inode;
Chris Wilson963b4832009-09-20 23:03:54 +01001356
Chris Wilsonae9fed62010-08-07 11:01:30 +01001357 /* Our goal here is to return as much of the memory as
1358 * is possible back to the system as we are called from OOM.
1359 * To do this we must instruct the shmfs to drop all of its
Hugh Dickinse2377fe2011-06-27 16:18:19 -07001360 * backing pages, *now*.
Chris Wilsonae9fed62010-08-07 11:01:30 +01001361 */
Chris Wilson05394f32010-11-08 19:18:58 +00001362 inode = obj->base.filp->f_path.dentry->d_inode;
Hugh Dickinse2377fe2011-06-27 16:18:19 -07001363 shmem_truncate_range(inode, 0, (loff_t)-1);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001364
Chris Wilsona14917e2012-02-24 21:13:38 +00001365 if (obj->base.map_list.map)
1366 drm_gem_free_mmap_offset(&obj->base);
1367
Chris Wilson05394f32010-11-08 19:18:58 +00001368 obj->madv = __I915_MADV_PURGED;
Chris Wilson963b4832009-09-20 23:03:54 +01001369}
1370
1371static inline int
Chris Wilson05394f32010-11-08 19:18:58 +00001372i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj)
Chris Wilson963b4832009-09-20 23:03:54 +01001373{
Chris Wilson05394f32010-11-08 19:18:58 +00001374 return obj->madv == I915_MADV_DONTNEED;
Chris Wilson963b4832009-09-20 23:03:54 +01001375}
1376
Eric Anholt673a3942008-07-30 12:06:12 -07001377static void
Chris Wilsondb53a302011-02-03 11:57:46 +00001378i915_gem_process_flushing_list(struct intel_ring_buffer *ring,
1379 uint32_t flush_domains)
Daniel Vetter63560392010-02-19 11:51:59 +01001380{
Chris Wilson05394f32010-11-08 19:18:58 +00001381 struct drm_i915_gem_object *obj, *next;
Daniel Vetter63560392010-02-19 11:51:59 +01001382
Chris Wilson05394f32010-11-08 19:18:58 +00001383 list_for_each_entry_safe(obj, next,
Chris Wilson64193402010-10-24 12:38:05 +01001384 &ring->gpu_write_list,
Daniel Vetter63560392010-02-19 11:51:59 +01001385 gpu_write_list) {
Chris Wilson05394f32010-11-08 19:18:58 +00001386 if (obj->base.write_domain & flush_domains) {
1387 uint32_t old_write_domain = obj->base.write_domain;
Daniel Vetter63560392010-02-19 11:51:59 +01001388
Chris Wilson05394f32010-11-08 19:18:58 +00001389 obj->base.write_domain = 0;
1390 list_del_init(&obj->gpu_write_list);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001391 i915_gem_object_move_to_active(obj, ring,
Chris Wilsondb53a302011-02-03 11:57:46 +00001392 i915_gem_next_request_seqno(ring));
Daniel Vetter63560392010-02-19 11:51:59 +01001393
Daniel Vetter63560392010-02-19 11:51:59 +01001394 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00001395 obj->base.read_domains,
Daniel Vetter63560392010-02-19 11:51:59 +01001396 old_write_domain);
1397 }
1398 }
1399}
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001400
Daniel Vetter53d227f2012-01-25 16:32:49 +01001401static u32
1402i915_gem_get_seqno(struct drm_device *dev)
1403{
1404 drm_i915_private_t *dev_priv = dev->dev_private;
1405 u32 seqno = dev_priv->next_seqno;
1406
1407 /* reserve 0 for non-seqno */
1408 if (++dev_priv->next_seqno == 0)
1409 dev_priv->next_seqno = 1;
1410
1411 return seqno;
1412}
1413
1414u32
1415i915_gem_next_request_seqno(struct intel_ring_buffer *ring)
1416{
1417 if (ring->outstanding_lazy_request == 0)
1418 ring->outstanding_lazy_request = i915_gem_get_seqno(ring->dev);
1419
1420 return ring->outstanding_lazy_request;
1421}
1422
Chris Wilson3cce4692010-10-27 16:11:02 +01001423int
Chris Wilsondb53a302011-02-03 11:57:46 +00001424i915_add_request(struct intel_ring_buffer *ring,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001425 struct drm_file *file,
Chris Wilsondb53a302011-02-03 11:57:46 +00001426 struct drm_i915_gem_request *request)
Eric Anholt673a3942008-07-30 12:06:12 -07001427{
Chris Wilsondb53a302011-02-03 11:57:46 +00001428 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07001429 uint32_t seqno;
Chris Wilsona71d8d92012-02-15 11:25:36 +00001430 u32 request_ring_position;
Eric Anholt673a3942008-07-30 12:06:12 -07001431 int was_empty;
Chris Wilson3cce4692010-10-27 16:11:02 +01001432 int ret;
1433
1434 BUG_ON(request == NULL);
Daniel Vetter53d227f2012-01-25 16:32:49 +01001435 seqno = i915_gem_next_request_seqno(ring);
Eric Anholt673a3942008-07-30 12:06:12 -07001436
Chris Wilsona71d8d92012-02-15 11:25:36 +00001437 /* Record the position of the start of the request so that
1438 * should we detect the updated seqno part-way through the
1439 * GPU processing the request, we never over-estimate the
1440 * position of the head.
1441 */
1442 request_ring_position = intel_ring_get_tail(ring);
1443
Chris Wilson3cce4692010-10-27 16:11:02 +01001444 ret = ring->add_request(ring, &seqno);
1445 if (ret)
1446 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001447
Chris Wilsondb53a302011-02-03 11:57:46 +00001448 trace_i915_gem_request_add(ring, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07001449
1450 request->seqno = seqno;
Zou Nan hai852835f2010-05-21 09:08:56 +08001451 request->ring = ring;
Chris Wilsona71d8d92012-02-15 11:25:36 +00001452 request->tail = request_ring_position;
Eric Anholt673a3942008-07-30 12:06:12 -07001453 request->emitted_jiffies = jiffies;
Zou Nan hai852835f2010-05-21 09:08:56 +08001454 was_empty = list_empty(&ring->request_list);
1455 list_add_tail(&request->list, &ring->request_list);
1456
Chris Wilsondb53a302011-02-03 11:57:46 +00001457 if (file) {
1458 struct drm_i915_file_private *file_priv = file->driver_priv;
1459
Chris Wilson1c255952010-09-26 11:03:27 +01001460 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001461 request->file_priv = file_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00001462 list_add_tail(&request->client_list,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001463 &file_priv->mm.request_list);
Chris Wilson1c255952010-09-26 11:03:27 +01001464 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00001465 }
Eric Anholt673a3942008-07-30 12:06:12 -07001466
Daniel Vetter5391d0c2012-01-25 14:03:57 +01001467 ring->outstanding_lazy_request = 0;
Chris Wilsondb53a302011-02-03 11:57:46 +00001468
Ben Gamarif65d9422009-09-14 17:48:44 -04001469 if (!dev_priv->mm.suspended) {
Ben Widawsky3e0dc6b2011-06-29 10:26:42 -07001470 if (i915_enable_hangcheck) {
1471 mod_timer(&dev_priv->hangcheck_timer,
1472 jiffies +
1473 msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
1474 }
Ben Gamarif65d9422009-09-14 17:48:44 -04001475 if (was_empty)
Chris Wilsonb3b079d2010-09-13 23:44:34 +01001476 queue_delayed_work(dev_priv->wq,
1477 &dev_priv->mm.retire_work, HZ);
Ben Gamarif65d9422009-09-14 17:48:44 -04001478 }
Chris Wilson3cce4692010-10-27 16:11:02 +01001479 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001480}
1481
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001482static inline void
1483i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
Eric Anholt673a3942008-07-30 12:06:12 -07001484{
Chris Wilson1c255952010-09-26 11:03:27 +01001485 struct drm_i915_file_private *file_priv = request->file_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07001486
Chris Wilson1c255952010-09-26 11:03:27 +01001487 if (!file_priv)
1488 return;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001489
Chris Wilson1c255952010-09-26 11:03:27 +01001490 spin_lock(&file_priv->mm.lock);
Herton Ronaldo Krzesinski09bfa512011-03-17 13:45:12 +00001491 if (request->file_priv) {
1492 list_del(&request->client_list);
1493 request->file_priv = NULL;
1494 }
Chris Wilson1c255952010-09-26 11:03:27 +01001495 spin_unlock(&file_priv->mm.lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001496}
1497
Chris Wilsondfaae392010-09-22 10:31:52 +01001498static void i915_gem_reset_ring_lists(struct drm_i915_private *dev_priv,
1499 struct intel_ring_buffer *ring)
Chris Wilson9375e442010-09-19 12:21:28 +01001500{
Chris Wilsondfaae392010-09-22 10:31:52 +01001501 while (!list_empty(&ring->request_list)) {
1502 struct drm_i915_gem_request *request;
Chris Wilson9375e442010-09-19 12:21:28 +01001503
Chris Wilsondfaae392010-09-22 10:31:52 +01001504 request = list_first_entry(&ring->request_list,
1505 struct drm_i915_gem_request,
1506 list);
1507
1508 list_del(&request->list);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001509 i915_gem_request_remove_from_client(request);
Chris Wilsondfaae392010-09-22 10:31:52 +01001510 kfree(request);
1511 }
1512
1513 while (!list_empty(&ring->active_list)) {
Chris Wilson05394f32010-11-08 19:18:58 +00001514 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001515
Chris Wilson05394f32010-11-08 19:18:58 +00001516 obj = list_first_entry(&ring->active_list,
1517 struct drm_i915_gem_object,
1518 ring_list);
Eric Anholt673a3942008-07-30 12:06:12 -07001519
Chris Wilson05394f32010-11-08 19:18:58 +00001520 obj->base.write_domain = 0;
1521 list_del_init(&obj->gpu_write_list);
1522 i915_gem_object_move_to_inactive(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001523 }
Eric Anholt673a3942008-07-30 12:06:12 -07001524}
1525
Chris Wilson312817a2010-11-22 11:50:11 +00001526static void i915_gem_reset_fences(struct drm_device *dev)
1527{
1528 struct drm_i915_private *dev_priv = dev->dev_private;
1529 int i;
1530
Daniel Vetter4b9de732011-10-09 21:52:02 +02001531 for (i = 0; i < dev_priv->num_fence_regs; i++) {
Chris Wilson312817a2010-11-22 11:50:11 +00001532 struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
Chris Wilson7d2cb392010-11-27 17:38:29 +00001533 struct drm_i915_gem_object *obj = reg->obj;
1534
1535 if (!obj)
1536 continue;
1537
1538 if (obj->tiling_mode)
1539 i915_gem_release_mmap(obj);
1540
Chris Wilsond9e86c02010-11-10 16:40:20 +00001541 reg->obj->fence_reg = I915_FENCE_REG_NONE;
1542 reg->obj->fenced_gpu_access = false;
1543 reg->obj->last_fenced_seqno = 0;
1544 reg->obj->last_fenced_ring = NULL;
1545 i915_gem_clear_fence_reg(dev, reg);
Chris Wilson312817a2010-11-22 11:50:11 +00001546 }
1547}
1548
Chris Wilson069efc12010-09-30 16:53:18 +01001549void i915_gem_reset(struct drm_device *dev)
Eric Anholt673a3942008-07-30 12:06:12 -07001550{
Chris Wilsondfaae392010-09-22 10:31:52 +01001551 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00001552 struct drm_i915_gem_object *obj;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001553 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07001554
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001555 for (i = 0; i < I915_NUM_RINGS; i++)
1556 i915_gem_reset_ring_lists(dev_priv, &dev_priv->ring[i]);
Chris Wilsondfaae392010-09-22 10:31:52 +01001557
1558 /* Remove anything from the flushing lists. The GPU cache is likely
1559 * to be lost on reset along with the data, so simply move the
1560 * lost bo to the inactive list.
1561 */
1562 while (!list_empty(&dev_priv->mm.flushing_list)) {
Akshay Joshi0206e352011-08-16 15:34:10 -04001563 obj = list_first_entry(&dev_priv->mm.flushing_list,
Chris Wilson05394f32010-11-08 19:18:58 +00001564 struct drm_i915_gem_object,
1565 mm_list);
Chris Wilson9375e442010-09-19 12:21:28 +01001566
Chris Wilson05394f32010-11-08 19:18:58 +00001567 obj->base.write_domain = 0;
1568 list_del_init(&obj->gpu_write_list);
1569 i915_gem_object_move_to_inactive(obj);
Chris Wilson9375e442010-09-19 12:21:28 +01001570 }
Chris Wilson9375e442010-09-19 12:21:28 +01001571
Chris Wilsondfaae392010-09-22 10:31:52 +01001572 /* Move everything out of the GPU domains to ensure we do any
1573 * necessary invalidation upon reuse.
1574 */
Chris Wilson05394f32010-11-08 19:18:58 +00001575 list_for_each_entry(obj,
Chris Wilson77f01232010-09-19 12:31:36 +01001576 &dev_priv->mm.inactive_list,
Chris Wilson69dc4982010-10-19 10:36:51 +01001577 mm_list)
Chris Wilson77f01232010-09-19 12:31:36 +01001578 {
Chris Wilson05394f32010-11-08 19:18:58 +00001579 obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
Chris Wilson77f01232010-09-19 12:31:36 +01001580 }
Chris Wilson069efc12010-09-30 16:53:18 +01001581
1582 /* The fence registers are invalidated so clear them out */
Chris Wilson312817a2010-11-22 11:50:11 +00001583 i915_gem_reset_fences(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07001584}
1585
1586/**
1587 * This function clears the request list as sequence numbers are passed.
1588 */
Chris Wilsona71d8d92012-02-15 11:25:36 +00001589void
Chris Wilsondb53a302011-02-03 11:57:46 +00001590i915_gem_retire_requests_ring(struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001591{
Eric Anholt673a3942008-07-30 12:06:12 -07001592 uint32_t seqno;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001593 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07001594
Chris Wilsondb53a302011-02-03 11:57:46 +00001595 if (list_empty(&ring->request_list))
Karsten Wiese6c0594a2009-02-23 15:07:57 +01001596 return;
1597
Chris Wilsondb53a302011-02-03 11:57:46 +00001598 WARN_ON(i915_verify_lists(ring->dev));
Eric Anholt673a3942008-07-30 12:06:12 -07001599
Chris Wilson78501ea2010-10-27 12:18:21 +01001600 seqno = ring->get_seqno(ring);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001601
Chris Wilson076e2c02011-01-21 10:07:18 +00001602 for (i = 0; i < ARRAY_SIZE(ring->sync_seqno); i++)
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001603 if (seqno >= ring->sync_seqno[i])
1604 ring->sync_seqno[i] = 0;
1605
Zou Nan hai852835f2010-05-21 09:08:56 +08001606 while (!list_empty(&ring->request_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001607 struct drm_i915_gem_request *request;
Eric Anholt673a3942008-07-30 12:06:12 -07001608
Zou Nan hai852835f2010-05-21 09:08:56 +08001609 request = list_first_entry(&ring->request_list,
Eric Anholt673a3942008-07-30 12:06:12 -07001610 struct drm_i915_gem_request,
1611 list);
Eric Anholt673a3942008-07-30 12:06:12 -07001612
Chris Wilsondfaae392010-09-22 10:31:52 +01001613 if (!i915_seqno_passed(seqno, request->seqno))
Eric Anholt673a3942008-07-30 12:06:12 -07001614 break;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001615
Chris Wilsondb53a302011-02-03 11:57:46 +00001616 trace_i915_gem_request_retire(ring, request->seqno);
Chris Wilsona71d8d92012-02-15 11:25:36 +00001617 /* We know the GPU must have read the request to have
1618 * sent us the seqno + interrupt, so use the position
1619 * of tail of the request to update the last known position
1620 * of the GPU head.
1621 */
1622 ring->last_retired_head = request->tail;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001623
1624 list_del(&request->list);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001625 i915_gem_request_remove_from_client(request);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001626 kfree(request);
1627 }
1628
1629 /* Move any buffers on the active list that are no longer referenced
1630 * by the ringbuffer to the flushing/inactive lists as appropriate.
1631 */
1632 while (!list_empty(&ring->active_list)) {
Chris Wilson05394f32010-11-08 19:18:58 +00001633 struct drm_i915_gem_object *obj;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001634
Akshay Joshi0206e352011-08-16 15:34:10 -04001635 obj = list_first_entry(&ring->active_list,
Chris Wilson05394f32010-11-08 19:18:58 +00001636 struct drm_i915_gem_object,
1637 ring_list);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001638
Chris Wilson05394f32010-11-08 19:18:58 +00001639 if (!i915_seqno_passed(seqno, obj->last_rendering_seqno))
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001640 break;
1641
Chris Wilson05394f32010-11-08 19:18:58 +00001642 if (obj->base.write_domain != 0)
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001643 i915_gem_object_move_to_flushing(obj);
1644 else
1645 i915_gem_object_move_to_inactive(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001646 }
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001647
Chris Wilsondb53a302011-02-03 11:57:46 +00001648 if (unlikely(ring->trace_irq_seqno &&
1649 i915_seqno_passed(seqno, ring->trace_irq_seqno))) {
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001650 ring->irq_put(ring);
Chris Wilsondb53a302011-02-03 11:57:46 +00001651 ring->trace_irq_seqno = 0;
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001652 }
Chris Wilson23bc5982010-09-29 16:10:57 +01001653
Chris Wilsondb53a302011-02-03 11:57:46 +00001654 WARN_ON(i915_verify_lists(ring->dev));
Eric Anholt673a3942008-07-30 12:06:12 -07001655}
1656
1657void
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001658i915_gem_retire_requests(struct drm_device *dev)
1659{
1660 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001661 int i;
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001662
Chris Wilsonbe726152010-07-23 23:18:50 +01001663 if (!list_empty(&dev_priv->mm.deferred_free_list)) {
Chris Wilson05394f32010-11-08 19:18:58 +00001664 struct drm_i915_gem_object *obj, *next;
Chris Wilsonbe726152010-07-23 23:18:50 +01001665
1666 /* We must be careful that during unbind() we do not
1667 * accidentally infinitely recurse into retire requests.
1668 * Currently:
1669 * retire -> free -> unbind -> wait -> retire_ring
1670 */
Chris Wilson05394f32010-11-08 19:18:58 +00001671 list_for_each_entry_safe(obj, next,
Chris Wilsonbe726152010-07-23 23:18:50 +01001672 &dev_priv->mm.deferred_free_list,
Chris Wilson69dc4982010-10-19 10:36:51 +01001673 mm_list)
Chris Wilson05394f32010-11-08 19:18:58 +00001674 i915_gem_free_object_tail(obj);
Chris Wilsonbe726152010-07-23 23:18:50 +01001675 }
1676
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001677 for (i = 0; i < I915_NUM_RINGS; i++)
Chris Wilsondb53a302011-02-03 11:57:46 +00001678 i915_gem_retire_requests_ring(&dev_priv->ring[i]);
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001679}
1680
Daniel Vetter75ef9da2010-08-21 00:25:16 +02001681static void
Eric Anholt673a3942008-07-30 12:06:12 -07001682i915_gem_retire_work_handler(struct work_struct *work)
1683{
1684 drm_i915_private_t *dev_priv;
1685 struct drm_device *dev;
Chris Wilson0a587052011-01-09 21:05:44 +00001686 bool idle;
1687 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07001688
1689 dev_priv = container_of(work, drm_i915_private_t,
1690 mm.retire_work.work);
1691 dev = dev_priv->dev;
1692
Chris Wilson891b48c2010-09-29 12:26:37 +01001693 /* Come back later if the device is busy... */
1694 if (!mutex_trylock(&dev->struct_mutex)) {
1695 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
1696 return;
1697 }
1698
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001699 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08001700
Chris Wilson0a587052011-01-09 21:05:44 +00001701 /* Send a periodic flush down the ring so we don't hold onto GEM
1702 * objects indefinitely.
1703 */
1704 idle = true;
1705 for (i = 0; i < I915_NUM_RINGS; i++) {
1706 struct intel_ring_buffer *ring = &dev_priv->ring[i];
1707
1708 if (!list_empty(&ring->gpu_write_list)) {
1709 struct drm_i915_gem_request *request;
1710 int ret;
1711
Chris Wilsondb53a302011-02-03 11:57:46 +00001712 ret = i915_gem_flush_ring(ring,
1713 0, I915_GEM_GPU_DOMAINS);
Chris Wilson0a587052011-01-09 21:05:44 +00001714 request = kzalloc(sizeof(*request), GFP_KERNEL);
1715 if (ret || request == NULL ||
Chris Wilsondb53a302011-02-03 11:57:46 +00001716 i915_add_request(ring, NULL, request))
Chris Wilson0a587052011-01-09 21:05:44 +00001717 kfree(request);
1718 }
1719
1720 idle &= list_empty(&ring->request_list);
1721 }
1722
1723 if (!dev_priv->mm.suspended && !idle)
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001724 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
Chris Wilson0a587052011-01-09 21:05:44 +00001725
Eric Anholt673a3942008-07-30 12:06:12 -07001726 mutex_unlock(&dev->struct_mutex);
1727}
1728
Chris Wilsondb53a302011-02-03 11:57:46 +00001729/**
1730 * Waits for a sequence number to be signaled, and cleans up the
1731 * request and object lists appropriately for that event.
1732 */
Daniel Vetter5a5a0c62009-09-15 22:57:36 +02001733int
Chris Wilsondb53a302011-02-03 11:57:46 +00001734i915_wait_request(struct intel_ring_buffer *ring,
Ben Widawskyb93f9cf2012-01-25 15:39:34 -08001735 uint32_t seqno,
1736 bool do_retire)
Eric Anholt673a3942008-07-30 12:06:12 -07001737{
Chris Wilsondb53a302011-02-03 11:57:46 +00001738 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001739 u32 ier;
Eric Anholt673a3942008-07-30 12:06:12 -07001740 int ret = 0;
1741
1742 BUG_ON(seqno == 0);
1743
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001744 if (atomic_read(&dev_priv->mm.wedged)) {
1745 struct completion *x = &dev_priv->error_completion;
1746 bool recovery_complete;
1747 unsigned long flags;
1748
1749 /* Give the error handler a chance to run. */
1750 spin_lock_irqsave(&x->wait.lock, flags);
1751 recovery_complete = x->done > 0;
1752 spin_unlock_irqrestore(&x->wait.lock, flags);
1753
1754 return recovery_complete ? -EIO : -EAGAIN;
1755 }
Ben Gamariffed1d02009-09-14 17:48:41 -04001756
Chris Wilson5d97eb62010-11-10 20:40:02 +00001757 if (seqno == ring->outstanding_lazy_request) {
Chris Wilson3cce4692010-10-27 16:11:02 +01001758 struct drm_i915_gem_request *request;
1759
1760 request = kzalloc(sizeof(*request), GFP_KERNEL);
1761 if (request == NULL)
Daniel Vettere35a41d2010-02-11 22:13:59 +01001762 return -ENOMEM;
Chris Wilson3cce4692010-10-27 16:11:02 +01001763
Chris Wilsondb53a302011-02-03 11:57:46 +00001764 ret = i915_add_request(ring, NULL, request);
Chris Wilson3cce4692010-10-27 16:11:02 +01001765 if (ret) {
1766 kfree(request);
1767 return ret;
1768 }
1769
1770 seqno = request->seqno;
Daniel Vettere35a41d2010-02-11 22:13:59 +01001771 }
1772
Chris Wilson78501ea2010-10-27 12:18:21 +01001773 if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) {
Chris Wilsondb53a302011-02-03 11:57:46 +00001774 if (HAS_PCH_SPLIT(ring->dev))
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001775 ier = I915_READ(DEIER) | I915_READ(GTIER);
1776 else
1777 ier = I915_READ(IER);
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001778 if (!ier) {
1779 DRM_ERROR("something (likely vbetool) disabled "
1780 "interrupts, re-enabling\n");
Chris Wilsonf01c22f2011-06-28 11:48:51 +01001781 ring->dev->driver->irq_preinstall(ring->dev);
1782 ring->dev->driver->irq_postinstall(ring->dev);
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001783 }
1784
Chris Wilsondb53a302011-02-03 11:57:46 +00001785 trace_i915_gem_request_wait_begin(ring, seqno);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001786
Chris Wilsonb2223492010-10-27 15:27:33 +01001787 ring->waiting_seqno = seqno;
Chris Wilsonb13c2b92010-12-13 16:54:50 +00001788 if (ring->irq_get(ring)) {
Chris Wilsonce453d82011-02-21 14:43:56 +00001789 if (dev_priv->mm.interruptible)
Chris Wilsonb13c2b92010-12-13 16:54:50 +00001790 ret = wait_event_interruptible(ring->irq_queue,
1791 i915_seqno_passed(ring->get_seqno(ring), seqno)
1792 || atomic_read(&dev_priv->mm.wedged));
1793 else
1794 wait_event(ring->irq_queue,
1795 i915_seqno_passed(ring->get_seqno(ring), seqno)
1796 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02001797
Chris Wilsonb13c2b92010-12-13 16:54:50 +00001798 ring->irq_put(ring);
Eric Anholte959b5d2011-12-22 14:55:01 -08001799 } else if (wait_for_atomic(i915_seqno_passed(ring->get_seqno(ring),
1800 seqno) ||
1801 atomic_read(&dev_priv->mm.wedged), 3000))
Chris Wilsonb5ba1772010-12-14 12:17:15 +00001802 ret = -EBUSY;
Chris Wilsonb2223492010-10-27 15:27:33 +01001803 ring->waiting_seqno = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001804
Chris Wilsondb53a302011-02-03 11:57:46 +00001805 trace_i915_gem_request_wait_end(ring, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07001806 }
Ben Gamariba1234d2009-09-14 17:48:47 -04001807 if (atomic_read(&dev_priv->mm.wedged))
Chris Wilson30dbf0c2010-09-25 10:19:17 +01001808 ret = -EAGAIN;
Eric Anholt673a3942008-07-30 12:06:12 -07001809
Eric Anholt673a3942008-07-30 12:06:12 -07001810 /* Directly dispatch request retiring. While we have the work queue
1811 * to handle this, the waiter on a request often wants an associated
1812 * buffer to have made it to the inactive list, and we would need
1813 * a separate wait queue to handle that.
1814 */
Ben Widawskyb93f9cf2012-01-25 15:39:34 -08001815 if (ret == 0 && do_retire)
Chris Wilsondb53a302011-02-03 11:57:46 +00001816 i915_gem_retire_requests_ring(ring);
Eric Anholt673a3942008-07-30 12:06:12 -07001817
1818 return ret;
1819}
1820
Daniel Vetter48764bf2009-09-15 22:57:32 +02001821/**
Eric Anholt673a3942008-07-30 12:06:12 -07001822 * Ensures that all rendering to the object has completed and the object is
1823 * safe to unbind from the GTT or access from the CPU.
1824 */
Chris Wilson54cf91d2010-11-25 18:00:26 +00001825int
Chris Wilsonce453d82011-02-21 14:43:56 +00001826i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07001827{
Eric Anholt673a3942008-07-30 12:06:12 -07001828 int ret;
1829
Eric Anholte47c68e2008-11-14 13:35:19 -08001830 /* This function only exists to support waiting for existing rendering,
1831 * not for emitting required flushes.
Eric Anholt673a3942008-07-30 12:06:12 -07001832 */
Chris Wilson05394f32010-11-08 19:18:58 +00001833 BUG_ON((obj->base.write_domain & I915_GEM_GPU_DOMAINS) != 0);
Eric Anholt673a3942008-07-30 12:06:12 -07001834
1835 /* If there is rendering queued on the buffer being evicted, wait for
1836 * it.
1837 */
Chris Wilson05394f32010-11-08 19:18:58 +00001838 if (obj->active) {
Ben Widawskyb93f9cf2012-01-25 15:39:34 -08001839 ret = i915_wait_request(obj->ring, obj->last_rendering_seqno,
1840 true);
Chris Wilson2cf34d72010-09-14 13:03:28 +01001841 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07001842 return ret;
1843 }
1844
1845 return 0;
1846}
1847
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01001848static void i915_gem_object_finish_gtt(struct drm_i915_gem_object *obj)
1849{
1850 u32 old_write_domain, old_read_domains;
1851
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01001852 /* Act a barrier for all accesses through the GTT */
1853 mb();
1854
1855 /* Force a pagefault for domain tracking on next user access */
1856 i915_gem_release_mmap(obj);
1857
Keith Packardb97c3d92011-06-24 21:02:59 -07001858 if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
1859 return;
1860
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01001861 old_read_domains = obj->base.read_domains;
1862 old_write_domain = obj->base.write_domain;
1863
1864 obj->base.read_domains &= ~I915_GEM_DOMAIN_GTT;
1865 obj->base.write_domain &= ~I915_GEM_DOMAIN_GTT;
1866
1867 trace_i915_gem_object_change_domain(obj,
1868 old_read_domains,
1869 old_write_domain);
1870}
1871
Eric Anholt673a3942008-07-30 12:06:12 -07001872/**
1873 * Unbinds an object from the GTT aperture.
1874 */
Jesse Barnes0f973f22009-01-26 17:10:45 -08001875int
Chris Wilson05394f32010-11-08 19:18:58 +00001876i915_gem_object_unbind(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07001877{
Daniel Vetter7bddb012012-02-09 17:15:47 +01001878 drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07001879 int ret = 0;
1880
Chris Wilson05394f32010-11-08 19:18:58 +00001881 if (obj->gtt_space == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07001882 return 0;
1883
Chris Wilson05394f32010-11-08 19:18:58 +00001884 if (obj->pin_count != 0) {
Eric Anholt673a3942008-07-30 12:06:12 -07001885 DRM_ERROR("Attempting to unbind pinned buffer\n");
1886 return -EINVAL;
1887 }
1888
Chris Wilsona8198ee2011-04-13 22:04:09 +01001889 ret = i915_gem_object_finish_gpu(obj);
Chris Wilson8dc17752010-07-23 23:18:51 +01001890 if (ret == -ERESTARTSYS)
Eric Anholt673a3942008-07-30 12:06:12 -07001891 return ret;
Chris Wilson8dc17752010-07-23 23:18:51 +01001892 /* Continue on if we fail due to EIO, the GPU is hung so we
1893 * should be safe and we need to cleanup or else we might
1894 * cause memory corruption through use-after-free.
1895 */
Chris Wilsona8198ee2011-04-13 22:04:09 +01001896
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01001897 i915_gem_object_finish_gtt(obj);
Chris Wilsona8198ee2011-04-13 22:04:09 +01001898
1899 /* Move the object to the CPU domain to ensure that
1900 * any possible CPU writes while it's not in the GTT
1901 * are flushed when we go to remap it.
1902 */
1903 if (ret == 0)
1904 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
1905 if (ret == -ERESTARTSYS)
1906 return ret;
Chris Wilson812ed4922010-09-30 15:08:57 +01001907 if (ret) {
Chris Wilsona8198ee2011-04-13 22:04:09 +01001908 /* In the event of a disaster, abandon all caches and
1909 * hope for the best.
1910 */
Chris Wilson812ed4922010-09-30 15:08:57 +01001911 i915_gem_clflush_object(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00001912 obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
Chris Wilson812ed4922010-09-30 15:08:57 +01001913 }
Eric Anholt673a3942008-07-30 12:06:12 -07001914
Daniel Vetter96b47b62009-12-15 17:50:00 +01001915 /* release the fence reg _after_ flushing */
Chris Wilsond9e86c02010-11-10 16:40:20 +00001916 ret = i915_gem_object_put_fence(obj);
1917 if (ret == -ERESTARTSYS)
1918 return ret;
Daniel Vetter96b47b62009-12-15 17:50:00 +01001919
Chris Wilsondb53a302011-02-03 11:57:46 +00001920 trace_i915_gem_object_unbind(obj);
1921
Daniel Vetter74898d72012-02-15 23:50:22 +01001922 if (obj->has_global_gtt_mapping)
1923 i915_gem_gtt_unbind_object(obj);
Daniel Vetter7bddb012012-02-09 17:15:47 +01001924 if (obj->has_aliasing_ppgtt_mapping) {
1925 i915_ppgtt_unbind_object(dev_priv->mm.aliasing_ppgtt, obj);
1926 obj->has_aliasing_ppgtt_mapping = 0;
1927 }
Daniel Vetter74163902012-02-15 23:50:21 +01001928 i915_gem_gtt_finish_object(obj);
Daniel Vetter7bddb012012-02-09 17:15:47 +01001929
Chris Wilsone5281cc2010-10-28 13:45:36 +01001930 i915_gem_object_put_pages_gtt(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001931
Chris Wilson6299f992010-11-24 12:23:44 +00001932 list_del_init(&obj->gtt_list);
Chris Wilson05394f32010-11-08 19:18:58 +00001933 list_del_init(&obj->mm_list);
Daniel Vetter75e9e912010-11-04 17:11:09 +01001934 /* Avoid an unnecessary call to unbind on rebind. */
Chris Wilson05394f32010-11-08 19:18:58 +00001935 obj->map_and_fenceable = true;
Eric Anholt673a3942008-07-30 12:06:12 -07001936
Chris Wilson05394f32010-11-08 19:18:58 +00001937 drm_mm_put_block(obj->gtt_space);
1938 obj->gtt_space = NULL;
1939 obj->gtt_offset = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001940
Chris Wilson05394f32010-11-08 19:18:58 +00001941 if (i915_gem_object_is_purgeable(obj))
Chris Wilson963b4832009-09-20 23:03:54 +01001942 i915_gem_object_truncate(obj);
1943
Chris Wilson8dc17752010-07-23 23:18:51 +01001944 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001945}
1946
Chris Wilson88241782011-01-07 17:09:48 +00001947int
Chris Wilsondb53a302011-02-03 11:57:46 +00001948i915_gem_flush_ring(struct intel_ring_buffer *ring,
Chris Wilson54cf91d2010-11-25 18:00:26 +00001949 uint32_t invalidate_domains,
1950 uint32_t flush_domains)
1951{
Chris Wilson88241782011-01-07 17:09:48 +00001952 int ret;
1953
Chris Wilson36d527d2011-03-19 22:26:49 +00001954 if (((invalidate_domains | flush_domains) & I915_GEM_GPU_DOMAINS) == 0)
1955 return 0;
1956
Chris Wilsondb53a302011-02-03 11:57:46 +00001957 trace_i915_gem_ring_flush(ring, invalidate_domains, flush_domains);
1958
Chris Wilson88241782011-01-07 17:09:48 +00001959 ret = ring->flush(ring, invalidate_domains, flush_domains);
1960 if (ret)
1961 return ret;
1962
Chris Wilson36d527d2011-03-19 22:26:49 +00001963 if (flush_domains & I915_GEM_GPU_DOMAINS)
1964 i915_gem_process_flushing_list(ring, flush_domains);
1965
Chris Wilson88241782011-01-07 17:09:48 +00001966 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001967}
1968
Ben Widawskyb93f9cf2012-01-25 15:39:34 -08001969static int i915_ring_idle(struct intel_ring_buffer *ring, bool do_retire)
Chris Wilsona56ba562010-09-28 10:07:56 +01001970{
Chris Wilson88241782011-01-07 17:09:48 +00001971 int ret;
1972
Chris Wilson395b70b2010-10-28 21:28:46 +01001973 if (list_empty(&ring->gpu_write_list) && list_empty(&ring->active_list))
Chris Wilson64193402010-10-24 12:38:05 +01001974 return 0;
1975
Chris Wilson88241782011-01-07 17:09:48 +00001976 if (!list_empty(&ring->gpu_write_list)) {
Chris Wilsondb53a302011-02-03 11:57:46 +00001977 ret = i915_gem_flush_ring(ring,
Chris Wilson0ac74c62010-12-06 14:36:02 +00001978 I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
Chris Wilson88241782011-01-07 17:09:48 +00001979 if (ret)
1980 return ret;
1981 }
1982
Ben Widawskyb93f9cf2012-01-25 15:39:34 -08001983 return i915_wait_request(ring, i915_gem_next_request_seqno(ring),
1984 do_retire);
Chris Wilsona56ba562010-09-28 10:07:56 +01001985}
1986
Ben Widawskyb93f9cf2012-01-25 15:39:34 -08001987int i915_gpu_idle(struct drm_device *dev, bool do_retire)
Daniel Vetter4df2faf2010-02-19 11:52:00 +01001988{
1989 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001990 int ret, i;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01001991
Daniel Vetter4df2faf2010-02-19 11:52:00 +01001992 /* Flush everything onto the inactive list. */
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001993 for (i = 0; i < I915_NUM_RINGS; i++) {
Ben Widawskyb93f9cf2012-01-25 15:39:34 -08001994 ret = i915_ring_idle(&dev_priv->ring[i], do_retire);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001995 if (ret)
1996 return ret;
1997 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08001998
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001999 return 0;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002000}
2001
Daniel Vetterc6642782010-11-12 13:46:18 +00002002static int sandybridge_write_fence_reg(struct drm_i915_gem_object *obj,
2003 struct intel_ring_buffer *pipelined)
Eric Anholt4e901fd2009-10-26 16:44:17 -07002004{
Chris Wilson05394f32010-11-08 19:18:58 +00002005 struct drm_device *dev = obj->base.dev;
Eric Anholt4e901fd2009-10-26 16:44:17 -07002006 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00002007 u32 size = obj->gtt_space->size;
2008 int regnum = obj->fence_reg;
Eric Anholt4e901fd2009-10-26 16:44:17 -07002009 uint64_t val;
2010
Chris Wilson05394f32010-11-08 19:18:58 +00002011 val = (uint64_t)((obj->gtt_offset + size - 4096) &
Daniel Vetterc6642782010-11-12 13:46:18 +00002012 0xfffff000) << 32;
Chris Wilson05394f32010-11-08 19:18:58 +00002013 val |= obj->gtt_offset & 0xfffff000;
2014 val |= (uint64_t)((obj->stride / 128) - 1) <<
Eric Anholt4e901fd2009-10-26 16:44:17 -07002015 SANDYBRIDGE_FENCE_PITCH_SHIFT;
2016
Chris Wilson05394f32010-11-08 19:18:58 +00002017 if (obj->tiling_mode == I915_TILING_Y)
Eric Anholt4e901fd2009-10-26 16:44:17 -07002018 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2019 val |= I965_FENCE_REG_VALID;
2020
Daniel Vetterc6642782010-11-12 13:46:18 +00002021 if (pipelined) {
2022 int ret = intel_ring_begin(pipelined, 6);
2023 if (ret)
2024 return ret;
2025
2026 intel_ring_emit(pipelined, MI_NOOP);
2027 intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(2));
2028 intel_ring_emit(pipelined, FENCE_REG_SANDYBRIDGE_0 + regnum*8);
2029 intel_ring_emit(pipelined, (u32)val);
2030 intel_ring_emit(pipelined, FENCE_REG_SANDYBRIDGE_0 + regnum*8 + 4);
2031 intel_ring_emit(pipelined, (u32)(val >> 32));
2032 intel_ring_advance(pipelined);
2033 } else
2034 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + regnum * 8, val);
2035
2036 return 0;
Eric Anholt4e901fd2009-10-26 16:44:17 -07002037}
2038
Daniel Vetterc6642782010-11-12 13:46:18 +00002039static int i965_write_fence_reg(struct drm_i915_gem_object *obj,
2040 struct intel_ring_buffer *pipelined)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002041{
Chris Wilson05394f32010-11-08 19:18:58 +00002042 struct drm_device *dev = obj->base.dev;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002043 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00002044 u32 size = obj->gtt_space->size;
2045 int regnum = obj->fence_reg;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002046 uint64_t val;
2047
Chris Wilson05394f32010-11-08 19:18:58 +00002048 val = (uint64_t)((obj->gtt_offset + size - 4096) &
Jesse Barnesde151cf2008-11-12 10:03:55 -08002049 0xfffff000) << 32;
Chris Wilson05394f32010-11-08 19:18:58 +00002050 val |= obj->gtt_offset & 0xfffff000;
2051 val |= ((obj->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
2052 if (obj->tiling_mode == I915_TILING_Y)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002053 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2054 val |= I965_FENCE_REG_VALID;
2055
Daniel Vetterc6642782010-11-12 13:46:18 +00002056 if (pipelined) {
2057 int ret = intel_ring_begin(pipelined, 6);
2058 if (ret)
2059 return ret;
2060
2061 intel_ring_emit(pipelined, MI_NOOP);
2062 intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(2));
2063 intel_ring_emit(pipelined, FENCE_REG_965_0 + regnum*8);
2064 intel_ring_emit(pipelined, (u32)val);
2065 intel_ring_emit(pipelined, FENCE_REG_965_0 + regnum*8 + 4);
2066 intel_ring_emit(pipelined, (u32)(val >> 32));
2067 intel_ring_advance(pipelined);
2068 } else
2069 I915_WRITE64(FENCE_REG_965_0 + regnum * 8, val);
2070
2071 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002072}
2073
Daniel Vetterc6642782010-11-12 13:46:18 +00002074static int i915_write_fence_reg(struct drm_i915_gem_object *obj,
2075 struct intel_ring_buffer *pipelined)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002076{
Chris Wilson05394f32010-11-08 19:18:58 +00002077 struct drm_device *dev = obj->base.dev;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002078 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00002079 u32 size = obj->gtt_space->size;
Daniel Vetterc6642782010-11-12 13:46:18 +00002080 u32 fence_reg, val, pitch_val;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002081 int tile_width;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002082
Daniel Vetterc6642782010-11-12 13:46:18 +00002083 if (WARN((obj->gtt_offset & ~I915_FENCE_START_MASK) ||
2084 (size & -size) != size ||
2085 (obj->gtt_offset & (size - 1)),
2086 "object 0x%08x [fenceable? %d] not 1M or pot-size (0x%08x) aligned\n",
2087 obj->gtt_offset, obj->map_and_fenceable, size))
2088 return -EINVAL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002089
Daniel Vetterc6642782010-11-12 13:46:18 +00002090 if (obj->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev))
Jesse Barnes0f973f22009-01-26 17:10:45 -08002091 tile_width = 128;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002092 else
Jesse Barnes0f973f22009-01-26 17:10:45 -08002093 tile_width = 512;
2094
2095 /* Note: pitch better be a power of two tile widths */
Chris Wilson05394f32010-11-08 19:18:58 +00002096 pitch_val = obj->stride / tile_width;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002097 pitch_val = ffs(pitch_val) - 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002098
Chris Wilson05394f32010-11-08 19:18:58 +00002099 val = obj->gtt_offset;
2100 if (obj->tiling_mode == I915_TILING_Y)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002101 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002102 val |= I915_FENCE_SIZE_BITS(size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002103 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2104 val |= I830_FENCE_REG_VALID;
2105
Chris Wilson05394f32010-11-08 19:18:58 +00002106 fence_reg = obj->fence_reg;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002107 if (fence_reg < 8)
2108 fence_reg = FENCE_REG_830_0 + fence_reg * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002109 else
Chris Wilsona00b10c2010-09-24 21:15:47 +01002110 fence_reg = FENCE_REG_945_8 + (fence_reg - 8) * 4;
Daniel Vetterc6642782010-11-12 13:46:18 +00002111
2112 if (pipelined) {
2113 int ret = intel_ring_begin(pipelined, 4);
2114 if (ret)
2115 return ret;
2116
2117 intel_ring_emit(pipelined, MI_NOOP);
2118 intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(1));
2119 intel_ring_emit(pipelined, fence_reg);
2120 intel_ring_emit(pipelined, val);
2121 intel_ring_advance(pipelined);
2122 } else
2123 I915_WRITE(fence_reg, val);
2124
2125 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002126}
2127
Daniel Vetterc6642782010-11-12 13:46:18 +00002128static int i830_write_fence_reg(struct drm_i915_gem_object *obj,
2129 struct intel_ring_buffer *pipelined)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002130{
Chris Wilson05394f32010-11-08 19:18:58 +00002131 struct drm_device *dev = obj->base.dev;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002132 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00002133 u32 size = obj->gtt_space->size;
2134 int regnum = obj->fence_reg;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002135 uint32_t val;
2136 uint32_t pitch_val;
2137
Daniel Vetterc6642782010-11-12 13:46:18 +00002138 if (WARN((obj->gtt_offset & ~I830_FENCE_START_MASK) ||
2139 (size & -size) != size ||
2140 (obj->gtt_offset & (size - 1)),
2141 "object 0x%08x not 512K or pot-size 0x%08x aligned\n",
2142 obj->gtt_offset, size))
2143 return -EINVAL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002144
Chris Wilson05394f32010-11-08 19:18:58 +00002145 pitch_val = obj->stride / 128;
Eric Anholte76a16d2009-05-26 17:44:56 -07002146 pitch_val = ffs(pitch_val) - 1;
Eric Anholte76a16d2009-05-26 17:44:56 -07002147
Chris Wilson05394f32010-11-08 19:18:58 +00002148 val = obj->gtt_offset;
2149 if (obj->tiling_mode == I915_TILING_Y)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002150 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Daniel Vetterc6642782010-11-12 13:46:18 +00002151 val |= I830_FENCE_SIZE_BITS(size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002152 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2153 val |= I830_FENCE_REG_VALID;
2154
Daniel Vetterc6642782010-11-12 13:46:18 +00002155 if (pipelined) {
2156 int ret = intel_ring_begin(pipelined, 4);
2157 if (ret)
2158 return ret;
2159
2160 intel_ring_emit(pipelined, MI_NOOP);
2161 intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(1));
2162 intel_ring_emit(pipelined, FENCE_REG_830_0 + regnum*4);
2163 intel_ring_emit(pipelined, val);
2164 intel_ring_advance(pipelined);
2165 } else
2166 I915_WRITE(FENCE_REG_830_0 + regnum * 4, val);
2167
2168 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002169}
2170
Chris Wilsond9e86c02010-11-10 16:40:20 +00002171static bool ring_passed_seqno(struct intel_ring_buffer *ring, u32 seqno)
2172{
2173 return i915_seqno_passed(ring->get_seqno(ring), seqno);
2174}
2175
2176static int
2177i915_gem_object_flush_fence(struct drm_i915_gem_object *obj,
Chris Wilsonce453d82011-02-21 14:43:56 +00002178 struct intel_ring_buffer *pipelined)
Chris Wilsond9e86c02010-11-10 16:40:20 +00002179{
2180 int ret;
2181
2182 if (obj->fenced_gpu_access) {
Chris Wilson88241782011-01-07 17:09:48 +00002183 if (obj->base.write_domain & I915_GEM_GPU_DOMAINS) {
Chris Wilsondb53a302011-02-03 11:57:46 +00002184 ret = i915_gem_flush_ring(obj->last_fenced_ring,
Chris Wilson88241782011-01-07 17:09:48 +00002185 0, obj->base.write_domain);
2186 if (ret)
2187 return ret;
2188 }
Chris Wilsond9e86c02010-11-10 16:40:20 +00002189
2190 obj->fenced_gpu_access = false;
2191 }
2192
2193 if (obj->last_fenced_seqno && pipelined != obj->last_fenced_ring) {
2194 if (!ring_passed_seqno(obj->last_fenced_ring,
2195 obj->last_fenced_seqno)) {
Chris Wilsondb53a302011-02-03 11:57:46 +00002196 ret = i915_wait_request(obj->last_fenced_ring,
Ben Widawskyb93f9cf2012-01-25 15:39:34 -08002197 obj->last_fenced_seqno,
2198 true);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002199 if (ret)
2200 return ret;
2201 }
2202
2203 obj->last_fenced_seqno = 0;
2204 obj->last_fenced_ring = NULL;
2205 }
2206
Chris Wilson63256ec2011-01-04 18:42:07 +00002207 /* Ensure that all CPU reads are completed before installing a fence
2208 * and all writes before removing the fence.
2209 */
2210 if (obj->base.read_domains & I915_GEM_DOMAIN_GTT)
2211 mb();
2212
Chris Wilsond9e86c02010-11-10 16:40:20 +00002213 return 0;
2214}
2215
2216int
2217i915_gem_object_put_fence(struct drm_i915_gem_object *obj)
2218{
2219 int ret;
2220
2221 if (obj->tiling_mode)
2222 i915_gem_release_mmap(obj);
2223
Chris Wilsonce453d82011-02-21 14:43:56 +00002224 ret = i915_gem_object_flush_fence(obj, NULL);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002225 if (ret)
2226 return ret;
2227
2228 if (obj->fence_reg != I915_FENCE_REG_NONE) {
2229 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002230
2231 WARN_ON(dev_priv->fence_regs[obj->fence_reg].pin_count);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002232 i915_gem_clear_fence_reg(obj->base.dev,
2233 &dev_priv->fence_regs[obj->fence_reg]);
2234
2235 obj->fence_reg = I915_FENCE_REG_NONE;
2236 }
2237
2238 return 0;
2239}
2240
2241static struct drm_i915_fence_reg *
2242i915_find_fence_reg(struct drm_device *dev,
2243 struct intel_ring_buffer *pipelined)
Daniel Vetterae3db242010-02-19 11:51:58 +01002244{
Daniel Vetterae3db242010-02-19 11:51:58 +01002245 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002246 struct drm_i915_fence_reg *reg, *first, *avail;
2247 int i;
Daniel Vetterae3db242010-02-19 11:51:58 +01002248
2249 /* First try to find a free reg */
Chris Wilsond9e86c02010-11-10 16:40:20 +00002250 avail = NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002251 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2252 reg = &dev_priv->fence_regs[i];
2253 if (!reg->obj)
Chris Wilsond9e86c02010-11-10 16:40:20 +00002254 return reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01002255
Chris Wilson1690e1e2011-12-14 13:57:08 +01002256 if (!reg->pin_count)
Chris Wilsond9e86c02010-11-10 16:40:20 +00002257 avail = reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01002258 }
2259
Chris Wilsond9e86c02010-11-10 16:40:20 +00002260 if (avail == NULL)
2261 return NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002262
2263 /* None available, try to steal one or wait for a user to finish */
Chris Wilsond9e86c02010-11-10 16:40:20 +00002264 avail = first = NULL;
2265 list_for_each_entry(reg, &dev_priv->mm.fence_list, lru_list) {
Chris Wilson1690e1e2011-12-14 13:57:08 +01002266 if (reg->pin_count)
Daniel Vetterae3db242010-02-19 11:51:58 +01002267 continue;
2268
Chris Wilsond9e86c02010-11-10 16:40:20 +00002269 if (first == NULL)
2270 first = reg;
2271
2272 if (!pipelined ||
2273 !reg->obj->last_fenced_ring ||
2274 reg->obj->last_fenced_ring == pipelined) {
2275 avail = reg;
2276 break;
2277 }
Daniel Vetterae3db242010-02-19 11:51:58 +01002278 }
2279
Chris Wilsond9e86c02010-11-10 16:40:20 +00002280 if (avail == NULL)
2281 avail = first;
Daniel Vetterae3db242010-02-19 11:51:58 +01002282
Chris Wilsona00b10c2010-09-24 21:15:47 +01002283 return avail;
Daniel Vetterae3db242010-02-19 11:51:58 +01002284}
2285
Jesse Barnesde151cf2008-11-12 10:03:55 -08002286/**
Chris Wilsond9e86c02010-11-10 16:40:20 +00002287 * i915_gem_object_get_fence - set up a fence reg for an object
Jesse Barnesde151cf2008-11-12 10:03:55 -08002288 * @obj: object to map through a fence reg
Chris Wilsond9e86c02010-11-10 16:40:20 +00002289 * @pipelined: ring on which to queue the change, or NULL for CPU access
2290 * @interruptible: must we wait uninterruptibly for the register to retire?
Jesse Barnesde151cf2008-11-12 10:03:55 -08002291 *
2292 * When mapping objects through the GTT, userspace wants to be able to write
2293 * to them without having to worry about swizzling if the object is tiled.
2294 *
2295 * This function walks the fence regs looking for a free one for @obj,
2296 * stealing one if it can't find any.
2297 *
2298 * It then sets up the reg based on the object's properties: address, pitch
2299 * and tiling format.
2300 */
Chris Wilson8c4b8c32009-06-17 22:08:52 +01002301int
Chris Wilsond9e86c02010-11-10 16:40:20 +00002302i915_gem_object_get_fence(struct drm_i915_gem_object *obj,
Chris Wilsonce453d82011-02-21 14:43:56 +00002303 struct intel_ring_buffer *pipelined)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002304{
Chris Wilson05394f32010-11-08 19:18:58 +00002305 struct drm_device *dev = obj->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002306 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002307 struct drm_i915_fence_reg *reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01002308 int ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002309
Chris Wilson6bda10d2010-12-05 21:04:18 +00002310 /* XXX disable pipelining. There are bugs. Shocking. */
2311 pipelined = NULL;
2312
Chris Wilsond9e86c02010-11-10 16:40:20 +00002313 /* Just update our place in the LRU if our fence is getting reused. */
Chris Wilson05394f32010-11-08 19:18:58 +00002314 if (obj->fence_reg != I915_FENCE_REG_NONE) {
2315 reg = &dev_priv->fence_regs[obj->fence_reg];
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002316 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002317
Chris Wilson29c5a582011-03-17 15:23:22 +00002318 if (obj->tiling_changed) {
2319 ret = i915_gem_object_flush_fence(obj, pipelined);
2320 if (ret)
2321 return ret;
2322
2323 if (!obj->fenced_gpu_access && !obj->last_fenced_seqno)
2324 pipelined = NULL;
2325
2326 if (pipelined) {
2327 reg->setup_seqno =
2328 i915_gem_next_request_seqno(pipelined);
2329 obj->last_fenced_seqno = reg->setup_seqno;
2330 obj->last_fenced_ring = pipelined;
2331 }
2332
2333 goto update;
2334 }
Chris Wilsond9e86c02010-11-10 16:40:20 +00002335
2336 if (!pipelined) {
2337 if (reg->setup_seqno) {
2338 if (!ring_passed_seqno(obj->last_fenced_ring,
2339 reg->setup_seqno)) {
Chris Wilsondb53a302011-02-03 11:57:46 +00002340 ret = i915_wait_request(obj->last_fenced_ring,
Ben Widawskyb93f9cf2012-01-25 15:39:34 -08002341 reg->setup_seqno,
2342 true);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002343 if (ret)
2344 return ret;
2345 }
2346
2347 reg->setup_seqno = 0;
2348 }
2349 } else if (obj->last_fenced_ring &&
2350 obj->last_fenced_ring != pipelined) {
Chris Wilsonce453d82011-02-21 14:43:56 +00002351 ret = i915_gem_object_flush_fence(obj, pipelined);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002352 if (ret)
2353 return ret;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002354 }
2355
Eric Anholta09ba7f2009-08-29 12:49:51 -07002356 return 0;
2357 }
2358
Chris Wilsond9e86c02010-11-10 16:40:20 +00002359 reg = i915_find_fence_reg(dev, pipelined);
2360 if (reg == NULL)
Daniel Vetter39965b32011-12-14 13:57:09 +01002361 return -EDEADLK;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002362
Chris Wilsonce453d82011-02-21 14:43:56 +00002363 ret = i915_gem_object_flush_fence(obj, pipelined);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002364 if (ret)
Daniel Vetterae3db242010-02-19 11:51:58 +01002365 return ret;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002366
Chris Wilsond9e86c02010-11-10 16:40:20 +00002367 if (reg->obj) {
2368 struct drm_i915_gem_object *old = reg->obj;
2369
2370 drm_gem_object_reference(&old->base);
2371
2372 if (old->tiling_mode)
2373 i915_gem_release_mmap(old);
2374
Chris Wilsonce453d82011-02-21 14:43:56 +00002375 ret = i915_gem_object_flush_fence(old, pipelined);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002376 if (ret) {
2377 drm_gem_object_unreference(&old->base);
2378 return ret;
2379 }
2380
2381 if (old->last_fenced_seqno == 0 && obj->last_fenced_seqno == 0)
2382 pipelined = NULL;
2383
2384 old->fence_reg = I915_FENCE_REG_NONE;
2385 old->last_fenced_ring = pipelined;
2386 old->last_fenced_seqno =
Chris Wilsondb53a302011-02-03 11:57:46 +00002387 pipelined ? i915_gem_next_request_seqno(pipelined) : 0;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002388
2389 drm_gem_object_unreference(&old->base);
2390 } else if (obj->last_fenced_seqno == 0)
2391 pipelined = NULL;
Eric Anholta09ba7f2009-08-29 12:49:51 -07002392
Jesse Barnesde151cf2008-11-12 10:03:55 -08002393 reg->obj = obj;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002394 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
2395 obj->fence_reg = reg - dev_priv->fence_regs;
2396 obj->last_fenced_ring = pipelined;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002397
Chris Wilsond9e86c02010-11-10 16:40:20 +00002398 reg->setup_seqno =
Chris Wilsondb53a302011-02-03 11:57:46 +00002399 pipelined ? i915_gem_next_request_seqno(pipelined) : 0;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002400 obj->last_fenced_seqno = reg->setup_seqno;
2401
2402update:
2403 obj->tiling_changed = false;
Chris Wilsone259bef2010-09-17 00:32:02 +01002404 switch (INTEL_INFO(dev)->gen) {
Eric Anholt25aebfc32011-05-06 13:55:53 -07002405 case 7:
Chris Wilsone259bef2010-09-17 00:32:02 +01002406 case 6:
Daniel Vetterc6642782010-11-12 13:46:18 +00002407 ret = sandybridge_write_fence_reg(obj, pipelined);
Chris Wilsone259bef2010-09-17 00:32:02 +01002408 break;
2409 case 5:
2410 case 4:
Daniel Vetterc6642782010-11-12 13:46:18 +00002411 ret = i965_write_fence_reg(obj, pipelined);
Chris Wilsone259bef2010-09-17 00:32:02 +01002412 break;
2413 case 3:
Daniel Vetterc6642782010-11-12 13:46:18 +00002414 ret = i915_write_fence_reg(obj, pipelined);
Chris Wilsone259bef2010-09-17 00:32:02 +01002415 break;
2416 case 2:
Daniel Vetterc6642782010-11-12 13:46:18 +00002417 ret = i830_write_fence_reg(obj, pipelined);
Chris Wilsone259bef2010-09-17 00:32:02 +01002418 break;
2419 }
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002420
Daniel Vetterc6642782010-11-12 13:46:18 +00002421 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002422}
2423
2424/**
2425 * i915_gem_clear_fence_reg - clear out fence register info
2426 * @obj: object to clear
2427 *
2428 * Zeroes out the fence register itself and clears out the associated
Chris Wilson05394f32010-11-08 19:18:58 +00002429 * data structures in dev_priv and obj.
Jesse Barnesde151cf2008-11-12 10:03:55 -08002430 */
2431static void
Chris Wilsond9e86c02010-11-10 16:40:20 +00002432i915_gem_clear_fence_reg(struct drm_device *dev,
2433 struct drm_i915_fence_reg *reg)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002434{
Jesse Barnes79e53942008-11-07 14:24:08 -08002435 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002436 uint32_t fence_reg = reg - dev_priv->fence_regs;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002437
Chris Wilsone259bef2010-09-17 00:32:02 +01002438 switch (INTEL_INFO(dev)->gen) {
Eric Anholt25aebfc32011-05-06 13:55:53 -07002439 case 7:
Chris Wilsone259bef2010-09-17 00:32:02 +01002440 case 6:
Chris Wilsond9e86c02010-11-10 16:40:20 +00002441 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + fence_reg*8, 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002442 break;
2443 case 5:
2444 case 4:
Chris Wilsond9e86c02010-11-10 16:40:20 +00002445 I915_WRITE64(FENCE_REG_965_0 + fence_reg*8, 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002446 break;
2447 case 3:
Chris Wilsond9e86c02010-11-10 16:40:20 +00002448 if (fence_reg >= 8)
2449 fence_reg = FENCE_REG_945_8 + (fence_reg - 8) * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002450 else
Chris Wilsone259bef2010-09-17 00:32:02 +01002451 case 2:
Chris Wilsond9e86c02010-11-10 16:40:20 +00002452 fence_reg = FENCE_REG_830_0 + fence_reg * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002453
2454 I915_WRITE(fence_reg, 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002455 break;
Eric Anholtdc529a42009-03-10 22:34:49 -07002456 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002457
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002458 list_del_init(&reg->lru_list);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002459 reg->obj = NULL;
2460 reg->setup_seqno = 0;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002461 reg->pin_count = 0;
Chris Wilson52dc7d32009-06-06 09:46:01 +01002462}
2463
2464/**
Eric Anholt673a3942008-07-30 12:06:12 -07002465 * Finds free space in the GTT aperture and binds the object there.
2466 */
2467static int
Chris Wilson05394f32010-11-08 19:18:58 +00002468i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
Daniel Vetter920afa72010-09-16 17:54:23 +02002469 unsigned alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01002470 bool map_and_fenceable)
Eric Anholt673a3942008-07-30 12:06:12 -07002471{
Chris Wilson05394f32010-11-08 19:18:58 +00002472 struct drm_device *dev = obj->base.dev;
Eric Anholt673a3942008-07-30 12:06:12 -07002473 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07002474 struct drm_mm_node *free_space;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002475 gfp_t gfpmask = __GFP_NORETRY | __GFP_NOWARN;
Daniel Vetter5e783302010-11-14 22:32:36 +01002476 u32 size, fence_size, fence_alignment, unfenced_alignment;
Daniel Vetter75e9e912010-11-04 17:11:09 +01002477 bool mappable, fenceable;
Chris Wilson07f73f62009-09-14 16:50:30 +01002478 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002479
Chris Wilson05394f32010-11-08 19:18:58 +00002480 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01002481 DRM_ERROR("Attempting to bind a purgeable object\n");
2482 return -EINVAL;
2483 }
2484
Chris Wilsone28f8712011-07-18 13:11:49 -07002485 fence_size = i915_gem_get_gtt_size(dev,
2486 obj->base.size,
2487 obj->tiling_mode);
2488 fence_alignment = i915_gem_get_gtt_alignment(dev,
2489 obj->base.size,
2490 obj->tiling_mode);
2491 unfenced_alignment =
2492 i915_gem_get_unfenced_gtt_alignment(dev,
2493 obj->base.size,
2494 obj->tiling_mode);
Chris Wilsona00b10c2010-09-24 21:15:47 +01002495
Eric Anholt673a3942008-07-30 12:06:12 -07002496 if (alignment == 0)
Daniel Vetter5e783302010-11-14 22:32:36 +01002497 alignment = map_and_fenceable ? fence_alignment :
2498 unfenced_alignment;
Daniel Vetter75e9e912010-11-04 17:11:09 +01002499 if (map_and_fenceable && alignment & (fence_alignment - 1)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002500 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2501 return -EINVAL;
2502 }
2503
Chris Wilson05394f32010-11-08 19:18:58 +00002504 size = map_and_fenceable ? fence_size : obj->base.size;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002505
Chris Wilson654fc602010-05-27 13:18:21 +01002506 /* If the object is bigger than the entire aperture, reject it early
2507 * before evicting everything in a vain attempt to find space.
2508 */
Chris Wilson05394f32010-11-08 19:18:58 +00002509 if (obj->base.size >
Daniel Vetter75e9e912010-11-04 17:11:09 +01002510 (map_and_fenceable ? dev_priv->mm.gtt_mappable_end : dev_priv->mm.gtt_total)) {
Chris Wilson654fc602010-05-27 13:18:21 +01002511 DRM_ERROR("Attempting to bind an object larger than the aperture\n");
2512 return -E2BIG;
2513 }
2514
Eric Anholt673a3942008-07-30 12:06:12 -07002515 search_free:
Daniel Vetter75e9e912010-11-04 17:11:09 +01002516 if (map_and_fenceable)
Daniel Vetter920afa72010-09-16 17:54:23 +02002517 free_space =
2518 drm_mm_search_free_in_range(&dev_priv->mm.gtt_space,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002519 size, alignment, 0,
Daniel Vetter920afa72010-09-16 17:54:23 +02002520 dev_priv->mm.gtt_mappable_end,
2521 0);
2522 else
2523 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002524 size, alignment, 0);
Daniel Vetter920afa72010-09-16 17:54:23 +02002525
2526 if (free_space != NULL) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01002527 if (map_and_fenceable)
Chris Wilson05394f32010-11-08 19:18:58 +00002528 obj->gtt_space =
Daniel Vetter920afa72010-09-16 17:54:23 +02002529 drm_mm_get_block_range_generic(free_space,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002530 size, alignment, 0,
Daniel Vetter920afa72010-09-16 17:54:23 +02002531 dev_priv->mm.gtt_mappable_end,
2532 0);
2533 else
Chris Wilson05394f32010-11-08 19:18:58 +00002534 obj->gtt_space =
Chris Wilsona00b10c2010-09-24 21:15:47 +01002535 drm_mm_get_block(free_space, size, alignment);
Daniel Vetter920afa72010-09-16 17:54:23 +02002536 }
Chris Wilson05394f32010-11-08 19:18:58 +00002537 if (obj->gtt_space == NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -07002538 /* If the gtt is empty and we're still having trouble
2539 * fitting our object in, we're out of memory.
2540 */
Daniel Vetter75e9e912010-11-04 17:11:09 +01002541 ret = i915_gem_evict_something(dev, size, alignment,
2542 map_and_fenceable);
Chris Wilson97311292009-09-21 00:22:34 +01002543 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002544 return ret;
Chris Wilson97311292009-09-21 00:22:34 +01002545
Eric Anholt673a3942008-07-30 12:06:12 -07002546 goto search_free;
2547 }
2548
Chris Wilsone5281cc2010-10-28 13:45:36 +01002549 ret = i915_gem_object_get_pages_gtt(obj, gfpmask);
Eric Anholt673a3942008-07-30 12:06:12 -07002550 if (ret) {
Chris Wilson05394f32010-11-08 19:18:58 +00002551 drm_mm_put_block(obj->gtt_space);
2552 obj->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002553
2554 if (ret == -ENOMEM) {
Chris Wilson809b6332011-01-10 17:33:15 +00002555 /* first try to reclaim some memory by clearing the GTT */
2556 ret = i915_gem_evict_everything(dev, false);
Chris Wilson07f73f62009-09-14 16:50:30 +01002557 if (ret) {
Chris Wilson07f73f62009-09-14 16:50:30 +01002558 /* now try to shrink everyone else */
Chris Wilson4bdadb92010-01-27 13:36:32 +00002559 if (gfpmask) {
2560 gfpmask = 0;
2561 goto search_free;
Chris Wilson07f73f62009-09-14 16:50:30 +01002562 }
2563
Chris Wilson809b6332011-01-10 17:33:15 +00002564 return -ENOMEM;
Chris Wilson07f73f62009-09-14 16:50:30 +01002565 }
2566
2567 goto search_free;
2568 }
2569
Eric Anholt673a3942008-07-30 12:06:12 -07002570 return ret;
2571 }
2572
Daniel Vetter74163902012-02-15 23:50:21 +01002573 ret = i915_gem_gtt_prepare_object(obj);
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002574 if (ret) {
Chris Wilsone5281cc2010-10-28 13:45:36 +01002575 i915_gem_object_put_pages_gtt(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00002576 drm_mm_put_block(obj->gtt_space);
2577 obj->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002578
Chris Wilson809b6332011-01-10 17:33:15 +00002579 if (i915_gem_evict_everything(dev, false))
Chris Wilson07f73f62009-09-14 16:50:30 +01002580 return ret;
Chris Wilson07f73f62009-09-14 16:50:30 +01002581
2582 goto search_free;
Eric Anholt673a3942008-07-30 12:06:12 -07002583 }
Daniel Vetter0ebb9822012-02-15 23:50:24 +01002584
2585 if (!dev_priv->mm.aliasing_ppgtt)
2586 i915_gem_gtt_bind_object(obj, obj->cache_level);
Eric Anholt673a3942008-07-30 12:06:12 -07002587
Chris Wilson6299f992010-11-24 12:23:44 +00002588 list_add_tail(&obj->gtt_list, &dev_priv->mm.gtt_list);
Chris Wilson05394f32010-11-08 19:18:58 +00002589 list_add_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
Chris Wilsonbf1a1092010-08-07 11:01:20 +01002590
Eric Anholt673a3942008-07-30 12:06:12 -07002591 /* Assert that the object is not currently in any GPU domain. As it
2592 * wasn't in the GTT, there shouldn't be any way it could have been in
2593 * a GPU cache
2594 */
Chris Wilson05394f32010-11-08 19:18:58 +00002595 BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
2596 BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
Eric Anholt673a3942008-07-30 12:06:12 -07002597
Chris Wilson6299f992010-11-24 12:23:44 +00002598 obj->gtt_offset = obj->gtt_space->start;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002599
Daniel Vetter75e9e912010-11-04 17:11:09 +01002600 fenceable =
Chris Wilson05394f32010-11-08 19:18:58 +00002601 obj->gtt_space->size == fence_size &&
Akshay Joshi0206e352011-08-16 15:34:10 -04002602 (obj->gtt_space->start & (fence_alignment - 1)) == 0;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002603
Daniel Vetter75e9e912010-11-04 17:11:09 +01002604 mappable =
Chris Wilson05394f32010-11-08 19:18:58 +00002605 obj->gtt_offset + obj->base.size <= dev_priv->mm.gtt_mappable_end;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002606
Chris Wilson05394f32010-11-08 19:18:58 +00002607 obj->map_and_fenceable = mappable && fenceable;
Daniel Vetter75e9e912010-11-04 17:11:09 +01002608
Chris Wilsondb53a302011-02-03 11:57:46 +00002609 trace_i915_gem_object_bind(obj, map_and_fenceable);
Eric Anholt673a3942008-07-30 12:06:12 -07002610 return 0;
2611}
2612
2613void
Chris Wilson05394f32010-11-08 19:18:58 +00002614i915_gem_clflush_object(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002615{
Eric Anholt673a3942008-07-30 12:06:12 -07002616 /* If we don't have a page list set up, then we're not pinned
2617 * to GPU, and we can ignore the cache flush because it'll happen
2618 * again at bind time.
2619 */
Chris Wilson05394f32010-11-08 19:18:58 +00002620 if (obj->pages == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002621 return;
2622
Chris Wilson9c23f7f2011-03-29 16:59:52 -07002623 /* If the GPU is snooping the contents of the CPU cache,
2624 * we do not need to manually clear the CPU cache lines. However,
2625 * the caches are only snooped when the render cache is
2626 * flushed/invalidated. As we always have to emit invalidations
2627 * and flushes when moving into and out of the RENDER domain, correct
2628 * snooping behaviour occurs naturally as the result of our domain
2629 * tracking.
2630 */
2631 if (obj->cache_level != I915_CACHE_NONE)
2632 return;
2633
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002634 trace_i915_gem_object_clflush(obj);
Eric Anholtcfa16a02009-05-26 18:46:16 -07002635
Chris Wilson05394f32010-11-08 19:18:58 +00002636 drm_clflush_pages(obj->pages, obj->base.size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002637}
2638
Eric Anholte47c68e2008-11-14 13:35:19 -08002639/** Flushes any GPU write domain for the object if it's dirty. */
Chris Wilson88241782011-01-07 17:09:48 +00002640static int
Chris Wilson3619df02010-11-28 15:37:17 +00002641i915_gem_object_flush_gpu_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08002642{
Chris Wilson05394f32010-11-08 19:18:58 +00002643 if ((obj->base.write_domain & I915_GEM_GPU_DOMAINS) == 0)
Chris Wilson88241782011-01-07 17:09:48 +00002644 return 0;
Eric Anholte47c68e2008-11-14 13:35:19 -08002645
2646 /* Queue the GPU write cache flushing we need. */
Chris Wilsondb53a302011-02-03 11:57:46 +00002647 return i915_gem_flush_ring(obj->ring, 0, obj->base.write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002648}
2649
2650/** Flushes the GTT write domain for the object if it's dirty. */
2651static void
Chris Wilson05394f32010-11-08 19:18:58 +00002652i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08002653{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002654 uint32_t old_write_domain;
2655
Chris Wilson05394f32010-11-08 19:18:58 +00002656 if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
Eric Anholte47c68e2008-11-14 13:35:19 -08002657 return;
2658
Chris Wilson63256ec2011-01-04 18:42:07 +00002659 /* No actual flushing is required for the GTT write domain. Writes
Eric Anholte47c68e2008-11-14 13:35:19 -08002660 * to it immediately go to main memory as far as we know, so there's
2661 * no chipset flush. It also doesn't land in render cache.
Chris Wilson63256ec2011-01-04 18:42:07 +00002662 *
2663 * However, we do have to enforce the order so that all writes through
2664 * the GTT land before any writes to the device, such as updates to
2665 * the GATT itself.
Eric Anholte47c68e2008-11-14 13:35:19 -08002666 */
Chris Wilson63256ec2011-01-04 18:42:07 +00002667 wmb();
2668
Chris Wilson05394f32010-11-08 19:18:58 +00002669 old_write_domain = obj->base.write_domain;
2670 obj->base.write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002671
2672 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00002673 obj->base.read_domains,
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002674 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002675}
2676
2677/** Flushes the CPU write domain for the object if it's dirty. */
2678static void
Chris Wilson05394f32010-11-08 19:18:58 +00002679i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08002680{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002681 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002682
Chris Wilson05394f32010-11-08 19:18:58 +00002683 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
Eric Anholte47c68e2008-11-14 13:35:19 -08002684 return;
2685
2686 i915_gem_clflush_object(obj);
Daniel Vetter40ce6572010-11-05 18:12:18 +01002687 intel_gtt_chipset_flush();
Chris Wilson05394f32010-11-08 19:18:58 +00002688 old_write_domain = obj->base.write_domain;
2689 obj->base.write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002690
2691 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00002692 obj->base.read_domains,
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002693 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002694}
2695
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002696/**
2697 * Moves a single object to the GTT read, and possibly write domain.
2698 *
2699 * This function returns when the move is complete, including waiting on
2700 * flushes to occur.
2701 */
Jesse Barnes79e53942008-11-07 14:24:08 -08002702int
Chris Wilson20217462010-11-23 15:26:33 +00002703i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002704{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002705 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002706 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002707
Eric Anholt02354392008-11-26 13:58:13 -08002708 /* Not valid to be called on unbound objects. */
Chris Wilson05394f32010-11-08 19:18:58 +00002709 if (obj->gtt_space == NULL)
Eric Anholt02354392008-11-26 13:58:13 -08002710 return -EINVAL;
2711
Chris Wilson8d7e3de2011-02-07 15:23:02 +00002712 if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
2713 return 0;
2714
Chris Wilson88241782011-01-07 17:09:48 +00002715 ret = i915_gem_object_flush_gpu_write_domain(obj);
2716 if (ret)
2717 return ret;
2718
Chris Wilson87ca9c82010-12-02 09:42:56 +00002719 if (obj->pending_gpu_write || write) {
Chris Wilsonce453d82011-02-21 14:43:56 +00002720 ret = i915_gem_object_wait_rendering(obj);
Chris Wilson87ca9c82010-12-02 09:42:56 +00002721 if (ret)
2722 return ret;
2723 }
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002724
Chris Wilson72133422010-09-13 23:56:38 +01002725 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002726
Chris Wilson05394f32010-11-08 19:18:58 +00002727 old_write_domain = obj->base.write_domain;
2728 old_read_domains = obj->base.read_domains;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002729
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002730 /* It should now be out of any other write domains, and we can update
2731 * the domain values for our changes.
2732 */
Chris Wilson05394f32010-11-08 19:18:58 +00002733 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2734 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002735 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00002736 obj->base.read_domains = I915_GEM_DOMAIN_GTT;
2737 obj->base.write_domain = I915_GEM_DOMAIN_GTT;
2738 obj->dirty = 1;
Eric Anholte47c68e2008-11-14 13:35:19 -08002739 }
2740
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002741 trace_i915_gem_object_change_domain(obj,
2742 old_read_domains,
2743 old_write_domain);
2744
Eric Anholte47c68e2008-11-14 13:35:19 -08002745 return 0;
2746}
2747
Chris Wilsone4ffd172011-04-04 09:44:39 +01002748int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
2749 enum i915_cache_level cache_level)
2750{
Daniel Vetter7bddb012012-02-09 17:15:47 +01002751 struct drm_device *dev = obj->base.dev;
2752 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsone4ffd172011-04-04 09:44:39 +01002753 int ret;
2754
2755 if (obj->cache_level == cache_level)
2756 return 0;
2757
2758 if (obj->pin_count) {
2759 DRM_DEBUG("can not change the cache level of pinned objects\n");
2760 return -EBUSY;
2761 }
2762
2763 if (obj->gtt_space) {
2764 ret = i915_gem_object_finish_gpu(obj);
2765 if (ret)
2766 return ret;
2767
2768 i915_gem_object_finish_gtt(obj);
2769
2770 /* Before SandyBridge, you could not use tiling or fence
2771 * registers with snooped memory, so relinquish any fences
2772 * currently pointing to our region in the aperture.
2773 */
2774 if (INTEL_INFO(obj->base.dev)->gen < 6) {
2775 ret = i915_gem_object_put_fence(obj);
2776 if (ret)
2777 return ret;
2778 }
2779
Daniel Vetter74898d72012-02-15 23:50:22 +01002780 if (obj->has_global_gtt_mapping)
2781 i915_gem_gtt_bind_object(obj, cache_level);
Daniel Vetter7bddb012012-02-09 17:15:47 +01002782 if (obj->has_aliasing_ppgtt_mapping)
2783 i915_ppgtt_bind_object(dev_priv->mm.aliasing_ppgtt,
2784 obj, cache_level);
Chris Wilsone4ffd172011-04-04 09:44:39 +01002785 }
2786
2787 if (cache_level == I915_CACHE_NONE) {
2788 u32 old_read_domains, old_write_domain;
2789
2790 /* If we're coming from LLC cached, then we haven't
2791 * actually been tracking whether the data is in the
2792 * CPU cache or not, since we only allow one bit set
2793 * in obj->write_domain and have been skipping the clflushes.
2794 * Just set it to the CPU cache for now.
2795 */
2796 WARN_ON(obj->base.write_domain & ~I915_GEM_DOMAIN_CPU);
2797 WARN_ON(obj->base.read_domains & ~I915_GEM_DOMAIN_CPU);
2798
2799 old_read_domains = obj->base.read_domains;
2800 old_write_domain = obj->base.write_domain;
2801
2802 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
2803 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
2804
2805 trace_i915_gem_object_change_domain(obj,
2806 old_read_domains,
2807 old_write_domain);
2808 }
2809
2810 obj->cache_level = cache_level;
2811 return 0;
2812}
2813
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002814/*
Chris Wilson2da3b9b2011-04-14 09:41:17 +01002815 * Prepare buffer for display plane (scanout, cursors, etc).
2816 * Can be called from an uninterruptible phase (modesetting) and allows
2817 * any flushes to be pipelined (for pageflips).
2818 *
2819 * For the display plane, we want to be in the GTT but out of any write
2820 * domains. So in many ways this looks like set_to_gtt_domain() apart from the
2821 * ability to pipeline the waits, pinning and any additional subtleties
2822 * that may differentiate the display plane from ordinary buffers.
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002823 */
2824int
Chris Wilson2da3b9b2011-04-14 09:41:17 +01002825i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
2826 u32 alignment,
Chris Wilson919926a2010-11-12 13:42:53 +00002827 struct intel_ring_buffer *pipelined)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002828{
Chris Wilson2da3b9b2011-04-14 09:41:17 +01002829 u32 old_read_domains, old_write_domain;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002830 int ret;
2831
Chris Wilson88241782011-01-07 17:09:48 +00002832 ret = i915_gem_object_flush_gpu_write_domain(obj);
2833 if (ret)
2834 return ret;
2835
Chris Wilson0be73282010-12-06 14:36:27 +00002836 if (pipelined != obj->ring) {
Chris Wilsonce453d82011-02-21 14:43:56 +00002837 ret = i915_gem_object_wait_rendering(obj);
Keith Packardf0b69ef2011-07-19 16:21:40 -07002838 if (ret == -ERESTARTSYS)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002839 return ret;
2840 }
2841
Eric Anholta7ef0642011-03-29 16:59:54 -07002842 /* The display engine is not coherent with the LLC cache on gen6. As
2843 * a result, we make sure that the pinning that is about to occur is
2844 * done with uncached PTEs. This is lowest common denominator for all
2845 * chipsets.
2846 *
2847 * However for gen6+, we could do better by using the GFDT bit instead
2848 * of uncaching, which would allow us to flush all the LLC-cached data
2849 * with that bit in the PTE to main memory with just one PIPE_CONTROL.
2850 */
2851 ret = i915_gem_object_set_cache_level(obj, I915_CACHE_NONE);
2852 if (ret)
2853 return ret;
2854
Chris Wilson2da3b9b2011-04-14 09:41:17 +01002855 /* As the user may map the buffer once pinned in the display plane
2856 * (e.g. libkms for the bootup splash), we have to ensure that we
2857 * always use map_and_fenceable for all scanout buffers.
2858 */
2859 ret = i915_gem_object_pin(obj, alignment, true);
2860 if (ret)
2861 return ret;
2862
Chris Wilsonb118c1e2010-05-27 13:18:14 +01002863 i915_gem_object_flush_cpu_write_domain(obj);
2864
Chris Wilson2da3b9b2011-04-14 09:41:17 +01002865 old_write_domain = obj->base.write_domain;
Chris Wilson05394f32010-11-08 19:18:58 +00002866 old_read_domains = obj->base.read_domains;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01002867
2868 /* It should now be out of any other write domains, and we can update
2869 * the domain values for our changes.
2870 */
2871 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
Chris Wilson05394f32010-11-08 19:18:58 +00002872 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002873
2874 trace_i915_gem_object_change_domain(obj,
2875 old_read_domains,
Chris Wilson2da3b9b2011-04-14 09:41:17 +01002876 old_write_domain);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002877
2878 return 0;
2879}
2880
Chris Wilson85345512010-11-13 09:49:11 +00002881int
Chris Wilsona8198ee2011-04-13 22:04:09 +01002882i915_gem_object_finish_gpu(struct drm_i915_gem_object *obj)
Chris Wilson85345512010-11-13 09:49:11 +00002883{
Chris Wilson88241782011-01-07 17:09:48 +00002884 int ret;
2885
Chris Wilsona8198ee2011-04-13 22:04:09 +01002886 if ((obj->base.read_domains & I915_GEM_GPU_DOMAINS) == 0)
Chris Wilson85345512010-11-13 09:49:11 +00002887 return 0;
2888
Chris Wilson88241782011-01-07 17:09:48 +00002889 if (obj->base.write_domain & I915_GEM_GPU_DOMAINS) {
Chris Wilsondb53a302011-02-03 11:57:46 +00002890 ret = i915_gem_flush_ring(obj->ring, 0, obj->base.write_domain);
Chris Wilson88241782011-01-07 17:09:48 +00002891 if (ret)
2892 return ret;
2893 }
Chris Wilson85345512010-11-13 09:49:11 +00002894
Chris Wilsonc501ae72011-12-14 13:57:23 +01002895 ret = i915_gem_object_wait_rendering(obj);
2896 if (ret)
2897 return ret;
2898
Chris Wilsona8198ee2011-04-13 22:04:09 +01002899 /* Ensure that we invalidate the GPU's caches and TLBs. */
2900 obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
Chris Wilsonc501ae72011-12-14 13:57:23 +01002901 return 0;
Chris Wilson85345512010-11-13 09:49:11 +00002902}
2903
Eric Anholte47c68e2008-11-14 13:35:19 -08002904/**
2905 * Moves a single object to the CPU read, and possibly write domain.
2906 *
2907 * This function returns when the move is complete, including waiting on
2908 * flushes to occur.
2909 */
Chris Wilsondabdfe02012-03-26 10:10:27 +02002910int
Chris Wilson919926a2010-11-12 13:42:53 +00002911i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholte47c68e2008-11-14 13:35:19 -08002912{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002913 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002914 int ret;
2915
Chris Wilson8d7e3de2011-02-07 15:23:02 +00002916 if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
2917 return 0;
2918
Chris Wilson88241782011-01-07 17:09:48 +00002919 ret = i915_gem_object_flush_gpu_write_domain(obj);
2920 if (ret)
2921 return ret;
2922
Chris Wilsonce453d82011-02-21 14:43:56 +00002923 ret = i915_gem_object_wait_rendering(obj);
Daniel Vetterde18a292010-11-27 22:30:41 +01002924 if (ret)
Eric Anholte47c68e2008-11-14 13:35:19 -08002925 return ret;
2926
2927 i915_gem_object_flush_gtt_write_domain(obj);
2928
Chris Wilson05394f32010-11-08 19:18:58 +00002929 old_write_domain = obj->base.write_domain;
2930 old_read_domains = obj->base.read_domains;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002931
Eric Anholte47c68e2008-11-14 13:35:19 -08002932 /* Flush the CPU cache if it's still invalid. */
Chris Wilson05394f32010-11-08 19:18:58 +00002933 if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
Eric Anholte47c68e2008-11-14 13:35:19 -08002934 i915_gem_clflush_object(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08002935
Chris Wilson05394f32010-11-08 19:18:58 +00002936 obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08002937 }
2938
2939 /* It should now be out of any other write domains, and we can update
2940 * the domain values for our changes.
2941 */
Chris Wilson05394f32010-11-08 19:18:58 +00002942 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
Eric Anholte47c68e2008-11-14 13:35:19 -08002943
2944 /* If we're writing through the CPU, then the GPU read domains will
2945 * need to be invalidated at next use.
2946 */
2947 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00002948 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
2949 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08002950 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002951
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002952 trace_i915_gem_object_change_domain(obj,
2953 old_read_domains,
2954 old_write_domain);
2955
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002956 return 0;
2957}
2958
Eric Anholt673a3942008-07-30 12:06:12 -07002959/* Throttle our rendering by waiting until the ring has completed our requests
2960 * emitted over 20 msec ago.
2961 *
Eric Anholtb9624422009-06-03 07:27:35 +00002962 * Note that if we were to use the current jiffies each time around the loop,
2963 * we wouldn't escape the function with any frames outstanding if the time to
2964 * render a frame was over 20ms.
2965 *
Eric Anholt673a3942008-07-30 12:06:12 -07002966 * This should get us reasonable parallelism between CPU and GPU but also
2967 * relatively low latency when blocking on a particular request to finish.
2968 */
2969static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01002970i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07002971{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01002972 struct drm_i915_private *dev_priv = dev->dev_private;
2973 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00002974 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01002975 struct drm_i915_gem_request *request;
2976 struct intel_ring_buffer *ring = NULL;
2977 u32 seqno = 0;
2978 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002979
Chris Wilsone110e8d2011-01-26 15:39:14 +00002980 if (atomic_read(&dev_priv->mm.wedged))
2981 return -EIO;
2982
Chris Wilson1c255952010-09-26 11:03:27 +01002983 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01002984 list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
Eric Anholtb9624422009-06-03 07:27:35 +00002985 if (time_after_eq(request->emitted_jiffies, recent_enough))
2986 break;
2987
Chris Wilsonf787a5f2010-09-24 16:02:42 +01002988 ring = request->ring;
2989 seqno = request->seqno;
Eric Anholtb9624422009-06-03 07:27:35 +00002990 }
Chris Wilson1c255952010-09-26 11:03:27 +01002991 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01002992
2993 if (seqno == 0)
2994 return 0;
2995
2996 ret = 0;
Chris Wilson78501ea2010-10-27 12:18:21 +01002997 if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) {
Chris Wilsonf787a5f2010-09-24 16:02:42 +01002998 /* And wait for the seqno passing without holding any locks and
2999 * causing extra latency for others. This is safe as the irq
3000 * generation is designed to be run atomically and so is
3001 * lockless.
3002 */
Chris Wilsonb13c2b92010-12-13 16:54:50 +00003003 if (ring->irq_get(ring)) {
3004 ret = wait_event_interruptible(ring->irq_queue,
3005 i915_seqno_passed(ring->get_seqno(ring), seqno)
3006 || atomic_read(&dev_priv->mm.wedged));
3007 ring->irq_put(ring);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003008
Chris Wilsonb13c2b92010-12-13 16:54:50 +00003009 if (ret == 0 && atomic_read(&dev_priv->mm.wedged))
3010 ret = -EIO;
Eric Anholte959b5d2011-12-22 14:55:01 -08003011 } else if (wait_for_atomic(i915_seqno_passed(ring->get_seqno(ring),
3012 seqno) ||
Eric Anholt7ea29b12011-12-22 14:54:59 -08003013 atomic_read(&dev_priv->mm.wedged), 3000)) {
3014 ret = -EBUSY;
Chris Wilsonb13c2b92010-12-13 16:54:50 +00003015 }
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003016 }
3017
3018 if (ret == 0)
3019 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
Eric Anholtb9624422009-06-03 07:27:35 +00003020
Eric Anholt673a3942008-07-30 12:06:12 -07003021 return ret;
3022}
3023
Eric Anholt673a3942008-07-30 12:06:12 -07003024int
Chris Wilson05394f32010-11-08 19:18:58 +00003025i915_gem_object_pin(struct drm_i915_gem_object *obj,
3026 uint32_t alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01003027 bool map_and_fenceable)
Eric Anholt673a3942008-07-30 12:06:12 -07003028{
Chris Wilson05394f32010-11-08 19:18:58 +00003029 struct drm_device *dev = obj->base.dev;
Chris Wilsonf13d3f72010-09-20 17:36:15 +01003030 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003031 int ret;
3032
Chris Wilson05394f32010-11-08 19:18:58 +00003033 BUG_ON(obj->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT);
Chris Wilson23bc5982010-09-29 16:10:57 +01003034 WARN_ON(i915_verify_lists(dev));
Chris Wilsonac0c6b52010-05-27 13:18:18 +01003035
Chris Wilson05394f32010-11-08 19:18:58 +00003036 if (obj->gtt_space != NULL) {
3037 if ((alignment && obj->gtt_offset & (alignment - 1)) ||
3038 (map_and_fenceable && !obj->map_and_fenceable)) {
3039 WARN(obj->pin_count,
Chris Wilsonae7d49d2010-08-04 12:37:41 +01003040 "bo is already pinned with incorrect alignment:"
Daniel Vetter75e9e912010-11-04 17:11:09 +01003041 " offset=%x, req.alignment=%x, req.map_and_fenceable=%d,"
3042 " obj->map_and_fenceable=%d\n",
Chris Wilson05394f32010-11-08 19:18:58 +00003043 obj->gtt_offset, alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01003044 map_and_fenceable,
Chris Wilson05394f32010-11-08 19:18:58 +00003045 obj->map_and_fenceable);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01003046 ret = i915_gem_object_unbind(obj);
3047 if (ret)
3048 return ret;
3049 }
3050 }
3051
Chris Wilson05394f32010-11-08 19:18:58 +00003052 if (obj->gtt_space == NULL) {
Chris Wilsona00b10c2010-09-24 21:15:47 +01003053 ret = i915_gem_object_bind_to_gtt(obj, alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01003054 map_and_fenceable);
Chris Wilson97311292009-09-21 00:22:34 +01003055 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07003056 return ret;
Chris Wilson22c344e2009-02-11 14:26:45 +00003057 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05003058
Daniel Vetter74898d72012-02-15 23:50:22 +01003059 if (!obj->has_global_gtt_mapping && map_and_fenceable)
3060 i915_gem_gtt_bind_object(obj, obj->cache_level);
3061
Chris Wilson05394f32010-11-08 19:18:58 +00003062 if (obj->pin_count++ == 0) {
Chris Wilson05394f32010-11-08 19:18:58 +00003063 if (!obj->active)
3064 list_move_tail(&obj->mm_list,
Chris Wilsonf13d3f72010-09-20 17:36:15 +01003065 &dev_priv->mm.pinned_list);
Eric Anholt673a3942008-07-30 12:06:12 -07003066 }
Chris Wilson6299f992010-11-24 12:23:44 +00003067 obj->pin_mappable |= map_and_fenceable;
Eric Anholt673a3942008-07-30 12:06:12 -07003068
Chris Wilson23bc5982010-09-29 16:10:57 +01003069 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07003070 return 0;
3071}
3072
3073void
Chris Wilson05394f32010-11-08 19:18:58 +00003074i915_gem_object_unpin(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07003075{
Chris Wilson05394f32010-11-08 19:18:58 +00003076 struct drm_device *dev = obj->base.dev;
Eric Anholt673a3942008-07-30 12:06:12 -07003077 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003078
Chris Wilson23bc5982010-09-29 16:10:57 +01003079 WARN_ON(i915_verify_lists(dev));
Chris Wilson05394f32010-11-08 19:18:58 +00003080 BUG_ON(obj->pin_count == 0);
3081 BUG_ON(obj->gtt_space == NULL);
Eric Anholt673a3942008-07-30 12:06:12 -07003082
Chris Wilson05394f32010-11-08 19:18:58 +00003083 if (--obj->pin_count == 0) {
3084 if (!obj->active)
3085 list_move_tail(&obj->mm_list,
Eric Anholt673a3942008-07-30 12:06:12 -07003086 &dev_priv->mm.inactive_list);
Chris Wilson6299f992010-11-24 12:23:44 +00003087 obj->pin_mappable = false;
Eric Anholt673a3942008-07-30 12:06:12 -07003088 }
Chris Wilson23bc5982010-09-29 16:10:57 +01003089 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07003090}
3091
3092int
3093i915_gem_pin_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00003094 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003095{
3096 struct drm_i915_gem_pin *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003097 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07003098 int ret;
3099
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003100 ret = i915_mutex_lock_interruptible(dev);
3101 if (ret)
3102 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003103
Chris Wilson05394f32010-11-08 19:18:58 +00003104 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00003105 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003106 ret = -ENOENT;
3107 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07003108 }
Eric Anholt673a3942008-07-30 12:06:12 -07003109
Chris Wilson05394f32010-11-08 19:18:58 +00003110 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilsonbb6baf72009-09-22 14:24:13 +01003111 DRM_ERROR("Attempting to pin a purgeable buffer\n");
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003112 ret = -EINVAL;
3113 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003114 }
3115
Chris Wilson05394f32010-11-08 19:18:58 +00003116 if (obj->pin_filp != NULL && obj->pin_filp != file) {
Jesse Barnes79e53942008-11-07 14:24:08 -08003117 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
3118 args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003119 ret = -EINVAL;
3120 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08003121 }
3122
Chris Wilson05394f32010-11-08 19:18:58 +00003123 obj->user_pin_count++;
3124 obj->pin_filp = file;
3125 if (obj->user_pin_count == 1) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01003126 ret = i915_gem_object_pin(obj, args->alignment, true);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003127 if (ret)
3128 goto out;
Eric Anholt673a3942008-07-30 12:06:12 -07003129 }
3130
3131 /* XXX - flush the CPU caches for pinned objects
3132 * as the X server doesn't manage domains yet
3133 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003134 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00003135 args->offset = obj->gtt_offset;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003136out:
Chris Wilson05394f32010-11-08 19:18:58 +00003137 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003138unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07003139 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003140 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003141}
3142
3143int
3144i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00003145 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003146{
3147 struct drm_i915_gem_pin *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003148 struct drm_i915_gem_object *obj;
Chris Wilson76c1dec2010-09-25 11:22:51 +01003149 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003150
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003151 ret = i915_mutex_lock_interruptible(dev);
3152 if (ret)
3153 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003154
Chris Wilson05394f32010-11-08 19:18:58 +00003155 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00003156 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003157 ret = -ENOENT;
3158 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07003159 }
Chris Wilson76c1dec2010-09-25 11:22:51 +01003160
Chris Wilson05394f32010-11-08 19:18:58 +00003161 if (obj->pin_filp != file) {
Jesse Barnes79e53942008-11-07 14:24:08 -08003162 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
3163 args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003164 ret = -EINVAL;
3165 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08003166 }
Chris Wilson05394f32010-11-08 19:18:58 +00003167 obj->user_pin_count--;
3168 if (obj->user_pin_count == 0) {
3169 obj->pin_filp = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08003170 i915_gem_object_unpin(obj);
3171 }
Eric Anholt673a3942008-07-30 12:06:12 -07003172
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003173out:
Chris Wilson05394f32010-11-08 19:18:58 +00003174 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003175unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07003176 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003177 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003178}
3179
3180int
3181i915_gem_busy_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00003182 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003183{
3184 struct drm_i915_gem_busy *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003185 struct drm_i915_gem_object *obj;
Chris Wilson30dbf0c2010-09-25 10:19:17 +01003186 int ret;
3187
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003188 ret = i915_mutex_lock_interruptible(dev);
3189 if (ret)
3190 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003191
Chris Wilson05394f32010-11-08 19:18:58 +00003192 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00003193 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003194 ret = -ENOENT;
3195 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07003196 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08003197
Chris Wilson0be555b2010-08-04 15:36:30 +01003198 /* Count all active objects as busy, even if they are currently not used
3199 * by the gpu. Users of this interface expect objects to eventually
3200 * become non-busy without any further actions, therefore emit any
3201 * necessary flushes here.
Eric Anholtc4de0a52008-12-14 19:05:04 -08003202 */
Chris Wilson05394f32010-11-08 19:18:58 +00003203 args->busy = obj->active;
Chris Wilson0be555b2010-08-04 15:36:30 +01003204 if (args->busy) {
3205 /* Unconditionally flush objects, even when the gpu still uses this
3206 * object. Userspace calling this function indicates that it wants to
3207 * use this buffer rather sooner than later, so issuing the required
3208 * flush earlier is beneficial.
3209 */
Chris Wilson1a1c6972010-12-07 23:00:20 +00003210 if (obj->base.write_domain & I915_GEM_GPU_DOMAINS) {
Chris Wilsondb53a302011-02-03 11:57:46 +00003211 ret = i915_gem_flush_ring(obj->ring,
Chris Wilson88241782011-01-07 17:09:48 +00003212 0, obj->base.write_domain);
Chris Wilson1a1c6972010-12-07 23:00:20 +00003213 } else if (obj->ring->outstanding_lazy_request ==
3214 obj->last_rendering_seqno) {
3215 struct drm_i915_gem_request *request;
3216
Chris Wilson7a194872010-12-07 10:38:40 +00003217 /* This ring is not being cleared by active usage,
3218 * so emit a request to do so.
3219 */
Chris Wilson1a1c6972010-12-07 23:00:20 +00003220 request = kzalloc(sizeof(*request), GFP_KERNEL);
Rakib Mullick457eafc2011-11-16 00:49:28 +06003221 if (request) {
Akshay Joshi0206e352011-08-16 15:34:10 -04003222 ret = i915_add_request(obj->ring, NULL, request);
Rakib Mullick457eafc2011-11-16 00:49:28 +06003223 if (ret)
3224 kfree(request);
3225 } else
Chris Wilson7a194872010-12-07 10:38:40 +00003226 ret = -ENOMEM;
3227 }
Chris Wilson0be555b2010-08-04 15:36:30 +01003228
3229 /* Update the active list for the hardware's current position.
3230 * Otherwise this only updates on a delayed timer or when irqs
3231 * are actually unmasked, and our working set ends up being
3232 * larger than required.
3233 */
Chris Wilsondb53a302011-02-03 11:57:46 +00003234 i915_gem_retire_requests_ring(obj->ring);
Chris Wilson0be555b2010-08-04 15:36:30 +01003235
Chris Wilson05394f32010-11-08 19:18:58 +00003236 args->busy = obj->active;
Chris Wilson0be555b2010-08-04 15:36:30 +01003237 }
Eric Anholt673a3942008-07-30 12:06:12 -07003238
Chris Wilson05394f32010-11-08 19:18:58 +00003239 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003240unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07003241 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003242 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003243}
3244
3245int
3246i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
3247 struct drm_file *file_priv)
3248{
Akshay Joshi0206e352011-08-16 15:34:10 -04003249 return i915_gem_ring_throttle(dev, file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07003250}
3251
Chris Wilson3ef94da2009-09-14 16:50:29 +01003252int
3253i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
3254 struct drm_file *file_priv)
3255{
3256 struct drm_i915_gem_madvise *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003257 struct drm_i915_gem_object *obj;
Chris Wilson76c1dec2010-09-25 11:22:51 +01003258 int ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003259
3260 switch (args->madv) {
3261 case I915_MADV_DONTNEED:
3262 case I915_MADV_WILLNEED:
3263 break;
3264 default:
3265 return -EINVAL;
3266 }
3267
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003268 ret = i915_mutex_lock_interruptible(dev);
3269 if (ret)
3270 return ret;
3271
Chris Wilson05394f32010-11-08 19:18:58 +00003272 obj = to_intel_bo(drm_gem_object_lookup(dev, file_priv, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00003273 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003274 ret = -ENOENT;
3275 goto unlock;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003276 }
Chris Wilson3ef94da2009-09-14 16:50:29 +01003277
Chris Wilson05394f32010-11-08 19:18:58 +00003278 if (obj->pin_count) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003279 ret = -EINVAL;
3280 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003281 }
3282
Chris Wilson05394f32010-11-08 19:18:58 +00003283 if (obj->madv != __I915_MADV_PURGED)
3284 obj->madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003285
Chris Wilson2d7ef392009-09-20 23:13:10 +01003286 /* if the object is no longer bound, discard its backing storage */
Chris Wilson05394f32010-11-08 19:18:58 +00003287 if (i915_gem_object_is_purgeable(obj) &&
3288 obj->gtt_space == NULL)
Chris Wilson2d7ef392009-09-20 23:13:10 +01003289 i915_gem_object_truncate(obj);
3290
Chris Wilson05394f32010-11-08 19:18:58 +00003291 args->retained = obj->madv != __I915_MADV_PURGED;
Chris Wilsonbb6baf72009-09-22 14:24:13 +01003292
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003293out:
Chris Wilson05394f32010-11-08 19:18:58 +00003294 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003295unlock:
Chris Wilson3ef94da2009-09-14 16:50:29 +01003296 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003297 return ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003298}
3299
Chris Wilson05394f32010-11-08 19:18:58 +00003300struct drm_i915_gem_object *i915_gem_alloc_object(struct drm_device *dev,
3301 size_t size)
Daniel Vetterac52bc52010-04-09 19:05:06 +00003302{
Chris Wilson73aa8082010-09-30 11:46:12 +01003303 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc397b902010-04-09 19:05:07 +00003304 struct drm_i915_gem_object *obj;
Hugh Dickins5949eac2011-06-27 16:18:18 -07003305 struct address_space *mapping;
Daniel Vetterc397b902010-04-09 19:05:07 +00003306
3307 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
3308 if (obj == NULL)
3309 return NULL;
3310
3311 if (drm_gem_object_init(dev, &obj->base, size) != 0) {
3312 kfree(obj);
3313 return NULL;
3314 }
3315
Hugh Dickins5949eac2011-06-27 16:18:18 -07003316 mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
3317 mapping_set_gfp_mask(mapping, GFP_HIGHUSER | __GFP_RECLAIMABLE);
3318
Chris Wilson73aa8082010-09-30 11:46:12 +01003319 i915_gem_info_add_obj(dev_priv, size);
3320
Daniel Vetterc397b902010-04-09 19:05:07 +00003321 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
3322 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3323
Eugeni Dodonov3d29b842012-01-17 14:43:53 -02003324 if (HAS_LLC(dev)) {
3325 /* On some devices, we can have the GPU use the LLC (the CPU
Eric Anholta1871112011-03-29 16:59:55 -07003326 * cache) for about a 10% performance improvement
3327 * compared to uncached. Graphics requests other than
3328 * display scanout are coherent with the CPU in
3329 * accessing this cache. This means in this mode we
3330 * don't need to clflush on the CPU side, and on the
3331 * GPU side we only need to flush internal caches to
3332 * get data visible to the CPU.
3333 *
3334 * However, we maintain the display planes as UC, and so
3335 * need to rebind when first used as such.
3336 */
3337 obj->cache_level = I915_CACHE_LLC;
3338 } else
3339 obj->cache_level = I915_CACHE_NONE;
3340
Daniel Vetter62b8b212010-04-09 19:05:08 +00003341 obj->base.driver_private = NULL;
Daniel Vetterc397b902010-04-09 19:05:07 +00003342 obj->fence_reg = I915_FENCE_REG_NONE;
Chris Wilson69dc4982010-10-19 10:36:51 +01003343 INIT_LIST_HEAD(&obj->mm_list);
Daniel Vetter93a37f22010-11-05 20:24:53 +01003344 INIT_LIST_HEAD(&obj->gtt_list);
Chris Wilson69dc4982010-10-19 10:36:51 +01003345 INIT_LIST_HEAD(&obj->ring_list);
Chris Wilson432e58e2010-11-25 19:32:06 +00003346 INIT_LIST_HEAD(&obj->exec_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00003347 INIT_LIST_HEAD(&obj->gpu_write_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00003348 obj->madv = I915_MADV_WILLNEED;
Daniel Vetter75e9e912010-11-04 17:11:09 +01003349 /* Avoid an unnecessary call to unbind on the first bind. */
3350 obj->map_and_fenceable = true;
Daniel Vetterc397b902010-04-09 19:05:07 +00003351
Chris Wilson05394f32010-11-08 19:18:58 +00003352 return obj;
Daniel Vetterac52bc52010-04-09 19:05:06 +00003353}
3354
Eric Anholt673a3942008-07-30 12:06:12 -07003355int i915_gem_init_object(struct drm_gem_object *obj)
3356{
Daniel Vetterc397b902010-04-09 19:05:07 +00003357 BUG();
Jesse Barnesde151cf2008-11-12 10:03:55 -08003358
Eric Anholt673a3942008-07-30 12:06:12 -07003359 return 0;
3360}
3361
Chris Wilson05394f32010-11-08 19:18:58 +00003362static void i915_gem_free_object_tail(struct drm_i915_gem_object *obj)
Chris Wilsonbe726152010-07-23 23:18:50 +01003363{
Chris Wilson05394f32010-11-08 19:18:58 +00003364 struct drm_device *dev = obj->base.dev;
Chris Wilsonbe726152010-07-23 23:18:50 +01003365 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonbe726152010-07-23 23:18:50 +01003366 int ret;
3367
3368 ret = i915_gem_object_unbind(obj);
3369 if (ret == -ERESTARTSYS) {
Chris Wilson05394f32010-11-08 19:18:58 +00003370 list_move(&obj->mm_list,
Chris Wilsonbe726152010-07-23 23:18:50 +01003371 &dev_priv->mm.deferred_free_list);
3372 return;
3373 }
3374
Chris Wilson26e12f82011-03-20 11:20:19 +00003375 trace_i915_gem_object_destroy(obj);
3376
Chris Wilson05394f32010-11-08 19:18:58 +00003377 if (obj->base.map_list.map)
Rob Clarkb464e9a2011-08-10 08:09:08 -05003378 drm_gem_free_mmap_offset(&obj->base);
Chris Wilsonbe726152010-07-23 23:18:50 +01003379
Chris Wilson05394f32010-11-08 19:18:58 +00003380 drm_gem_object_release(&obj->base);
3381 i915_gem_info_remove_obj(dev_priv, obj->base.size);
Chris Wilsonbe726152010-07-23 23:18:50 +01003382
Chris Wilson05394f32010-11-08 19:18:58 +00003383 kfree(obj->bit_17);
3384 kfree(obj);
Chris Wilsonbe726152010-07-23 23:18:50 +01003385}
3386
Chris Wilson05394f32010-11-08 19:18:58 +00003387void i915_gem_free_object(struct drm_gem_object *gem_obj)
Eric Anholt673a3942008-07-30 12:06:12 -07003388{
Chris Wilson05394f32010-11-08 19:18:58 +00003389 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
3390 struct drm_device *dev = obj->base.dev;
Eric Anholt673a3942008-07-30 12:06:12 -07003391
Chris Wilson05394f32010-11-08 19:18:58 +00003392 while (obj->pin_count > 0)
Eric Anholt673a3942008-07-30 12:06:12 -07003393 i915_gem_object_unpin(obj);
3394
Chris Wilson05394f32010-11-08 19:18:58 +00003395 if (obj->phys_obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10003396 i915_gem_detach_phys_object(dev, obj);
3397
Chris Wilsonbe726152010-07-23 23:18:50 +01003398 i915_gem_free_object_tail(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003399}
3400
Jesse Barnes5669fca2009-02-17 15:13:31 -08003401int
Eric Anholt673a3942008-07-30 12:06:12 -07003402i915_gem_idle(struct drm_device *dev)
3403{
3404 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson29105cc2010-01-07 10:39:13 +00003405 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003406
Keith Packard6dbe2772008-10-14 21:41:13 -07003407 mutex_lock(&dev->struct_mutex);
3408
Chris Wilson87acb0a2010-10-19 10:13:00 +01003409 if (dev_priv->mm.suspended) {
Keith Packard6dbe2772008-10-14 21:41:13 -07003410 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07003411 return 0;
Keith Packard6dbe2772008-10-14 21:41:13 -07003412 }
Eric Anholt673a3942008-07-30 12:06:12 -07003413
Ben Widawskyb93f9cf2012-01-25 15:39:34 -08003414 ret = i915_gpu_idle(dev, true);
Keith Packard6dbe2772008-10-14 21:41:13 -07003415 if (ret) {
3416 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07003417 return ret;
Keith Packard6dbe2772008-10-14 21:41:13 -07003418 }
Eric Anholt673a3942008-07-30 12:06:12 -07003419
Chris Wilson29105cc2010-01-07 10:39:13 +00003420 /* Under UMS, be paranoid and evict. */
3421 if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
Chris Wilson5eac3ab2010-10-31 08:49:47 +00003422 ret = i915_gem_evict_inactive(dev, false);
Chris Wilson29105cc2010-01-07 10:39:13 +00003423 if (ret) {
3424 mutex_unlock(&dev->struct_mutex);
3425 return ret;
3426 }
3427 }
3428
Chris Wilson312817a2010-11-22 11:50:11 +00003429 i915_gem_reset_fences(dev);
3430
Chris Wilson29105cc2010-01-07 10:39:13 +00003431 /* Hack! Don't let anybody do execbuf while we don't control the chip.
3432 * We need to replace this with a semaphore, or something.
3433 * And not confound mm.suspended!
3434 */
3435 dev_priv->mm.suspended = 1;
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02003436 del_timer_sync(&dev_priv->hangcheck_timer);
Chris Wilson29105cc2010-01-07 10:39:13 +00003437
3438 i915_kernel_lost_context(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07003439 i915_gem_cleanup_ringbuffer(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00003440
Keith Packard6dbe2772008-10-14 21:41:13 -07003441 mutex_unlock(&dev->struct_mutex);
3442
Chris Wilson29105cc2010-01-07 10:39:13 +00003443 /* Cancel the retire work handler, which should be idle now. */
3444 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
3445
Eric Anholt673a3942008-07-30 12:06:12 -07003446 return 0;
3447}
3448
Daniel Vetterf691e2f2012-02-02 09:58:12 +01003449void i915_gem_init_swizzling(struct drm_device *dev)
3450{
3451 drm_i915_private_t *dev_priv = dev->dev_private;
3452
Daniel Vetter11782b02012-01-31 16:47:55 +01003453 if (INTEL_INFO(dev)->gen < 5 ||
Daniel Vetterf691e2f2012-02-02 09:58:12 +01003454 dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
3455 return;
3456
3457 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
3458 DISP_TILE_SURFACE_SWIZZLING);
3459
Daniel Vetter11782b02012-01-31 16:47:55 +01003460 if (IS_GEN5(dev))
3461 return;
3462
Daniel Vetterf691e2f2012-02-02 09:58:12 +01003463 I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
3464 if (IS_GEN6(dev))
3465 I915_WRITE(ARB_MODE, ARB_MODE_ENABLE(ARB_MODE_SWIZZLE_SNB));
3466 else
3467 I915_WRITE(ARB_MODE, ARB_MODE_ENABLE(ARB_MODE_SWIZZLE_IVB));
3468}
Daniel Vettere21af882012-02-09 20:53:27 +01003469
3470void i915_gem_init_ppgtt(struct drm_device *dev)
3471{
3472 drm_i915_private_t *dev_priv = dev->dev_private;
3473 uint32_t pd_offset;
3474 struct intel_ring_buffer *ring;
3475 int i;
3476
3477 if (!dev_priv->mm.aliasing_ppgtt)
3478 return;
3479
3480 pd_offset = dev_priv->mm.aliasing_ppgtt->pd_offset;
3481 pd_offset /= 64; /* in cachelines, */
3482 pd_offset <<= 16;
3483
3484 if (INTEL_INFO(dev)->gen == 6) {
3485 uint32_t ecochk = I915_READ(GAM_ECOCHK);
3486 I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT |
3487 ECOCHK_PPGTT_CACHE64B);
3488 I915_WRITE(GFX_MODE, GFX_MODE_ENABLE(GFX_PPGTT_ENABLE));
3489 } else if (INTEL_INFO(dev)->gen >= 7) {
3490 I915_WRITE(GAM_ECOCHK, ECOCHK_PPGTT_CACHE64B);
3491 /* GFX_MODE is per-ring on gen7+ */
3492 }
3493
3494 for (i = 0; i < I915_NUM_RINGS; i++) {
3495 ring = &dev_priv->ring[i];
3496
3497 if (INTEL_INFO(dev)->gen >= 7)
3498 I915_WRITE(RING_MODE_GEN7(ring),
3499 GFX_MODE_ENABLE(GFX_PPGTT_ENABLE));
3500
3501 I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
3502 I915_WRITE(RING_PP_DIR_BASE(ring), pd_offset);
3503 }
3504}
3505
Eric Anholt673a3942008-07-30 12:06:12 -07003506int
Daniel Vetterf691e2f2012-02-02 09:58:12 +01003507i915_gem_init_hw(struct drm_device *dev)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08003508{
3509 drm_i915_private_t *dev_priv = dev->dev_private;
3510 int ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01003511
Daniel Vetterf691e2f2012-02-02 09:58:12 +01003512 i915_gem_init_swizzling(dev);
3513
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08003514 ret = intel_init_render_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01003515 if (ret)
Chris Wilsonb6913e42010-11-12 10:46:37 +00003516 return ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01003517
3518 if (HAS_BSD(dev)) {
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08003519 ret = intel_init_bsd_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01003520 if (ret)
3521 goto cleanup_render_ring;
Zou Nan haid1b851f2010-05-21 09:08:57 +08003522 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01003523
Chris Wilson549f7362010-10-19 11:19:32 +01003524 if (HAS_BLT(dev)) {
3525 ret = intel_init_blt_ring_buffer(dev);
3526 if (ret)
3527 goto cleanup_bsd_ring;
3528 }
3529
Chris Wilson6f392d52010-08-07 11:01:22 +01003530 dev_priv->next_seqno = 1;
3531
Daniel Vettere21af882012-02-09 20:53:27 +01003532 i915_gem_init_ppgtt(dev);
3533
Chris Wilson68f95ba2010-05-27 13:18:22 +01003534 return 0;
3535
Chris Wilson549f7362010-10-19 11:19:32 +01003536cleanup_bsd_ring:
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003537 intel_cleanup_ring_buffer(&dev_priv->ring[VCS]);
Chris Wilson68f95ba2010-05-27 13:18:22 +01003538cleanup_render_ring:
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003539 intel_cleanup_ring_buffer(&dev_priv->ring[RCS]);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08003540 return ret;
3541}
3542
3543void
3544i915_gem_cleanup_ringbuffer(struct drm_device *dev)
3545{
3546 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003547 int i;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08003548
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003549 for (i = 0; i < I915_NUM_RINGS; i++)
3550 intel_cleanup_ring_buffer(&dev_priv->ring[i]);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08003551}
3552
3553int
Eric Anholt673a3942008-07-30 12:06:12 -07003554i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
3555 struct drm_file *file_priv)
3556{
3557 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003558 int ret, i;
Eric Anholt673a3942008-07-30 12:06:12 -07003559
Jesse Barnes79e53942008-11-07 14:24:08 -08003560 if (drm_core_check_feature(dev, DRIVER_MODESET))
3561 return 0;
3562
Ben Gamariba1234d2009-09-14 17:48:47 -04003563 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07003564 DRM_ERROR("Reenabling wedged hardware, good luck\n");
Ben Gamariba1234d2009-09-14 17:48:47 -04003565 atomic_set(&dev_priv->mm.wedged, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07003566 }
3567
Eric Anholt673a3942008-07-30 12:06:12 -07003568 mutex_lock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08003569 dev_priv->mm.suspended = 0;
3570
Daniel Vetterf691e2f2012-02-02 09:58:12 +01003571 ret = i915_gem_init_hw(dev);
Wu Fengguangd816f6a2009-04-18 10:43:32 +08003572 if (ret != 0) {
3573 mutex_unlock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08003574 return ret;
Wu Fengguangd816f6a2009-04-18 10:43:32 +08003575 }
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08003576
Chris Wilson69dc4982010-10-19 10:36:51 +01003577 BUG_ON(!list_empty(&dev_priv->mm.active_list));
Eric Anholt673a3942008-07-30 12:06:12 -07003578 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
3579 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003580 for (i = 0; i < I915_NUM_RINGS; i++) {
3581 BUG_ON(!list_empty(&dev_priv->ring[i].active_list));
3582 BUG_ON(!list_empty(&dev_priv->ring[i].request_list));
3583 }
Eric Anholt673a3942008-07-30 12:06:12 -07003584 mutex_unlock(&dev->struct_mutex);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04003585
Chris Wilson5f353082010-06-07 14:03:03 +01003586 ret = drm_irq_install(dev);
3587 if (ret)
3588 goto cleanup_ringbuffer;
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04003589
Eric Anholt673a3942008-07-30 12:06:12 -07003590 return 0;
Chris Wilson5f353082010-06-07 14:03:03 +01003591
3592cleanup_ringbuffer:
3593 mutex_lock(&dev->struct_mutex);
3594 i915_gem_cleanup_ringbuffer(dev);
3595 dev_priv->mm.suspended = 1;
3596 mutex_unlock(&dev->struct_mutex);
3597
3598 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003599}
3600
3601int
3602i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
3603 struct drm_file *file_priv)
3604{
Jesse Barnes79e53942008-11-07 14:24:08 -08003605 if (drm_core_check_feature(dev, DRIVER_MODESET))
3606 return 0;
3607
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04003608 drm_irq_uninstall(dev);
Linus Torvaldse6890f62009-09-08 17:09:24 -07003609 return i915_gem_idle(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07003610}
3611
3612void
3613i915_gem_lastclose(struct drm_device *dev)
3614{
3615 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003616
Eric Anholte806b492009-01-22 09:56:58 -08003617 if (drm_core_check_feature(dev, DRIVER_MODESET))
3618 return;
3619
Keith Packard6dbe2772008-10-14 21:41:13 -07003620 ret = i915_gem_idle(dev);
3621 if (ret)
3622 DRM_ERROR("failed to idle hardware: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07003623}
3624
Chris Wilson64193402010-10-24 12:38:05 +01003625static void
3626init_ring_lists(struct intel_ring_buffer *ring)
3627{
3628 INIT_LIST_HEAD(&ring->active_list);
3629 INIT_LIST_HEAD(&ring->request_list);
3630 INIT_LIST_HEAD(&ring->gpu_write_list);
3631}
3632
Eric Anholt673a3942008-07-30 12:06:12 -07003633void
3634i915_gem_load(struct drm_device *dev)
3635{
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02003636 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07003637 drm_i915_private_t *dev_priv = dev->dev_private;
3638
Chris Wilson69dc4982010-10-19 10:36:51 +01003639 INIT_LIST_HEAD(&dev_priv->mm.active_list);
Eric Anholt673a3942008-07-30 12:06:12 -07003640 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
3641 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
Chris Wilsonf13d3f72010-09-20 17:36:15 +01003642 INIT_LIST_HEAD(&dev_priv->mm.pinned_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07003643 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Chris Wilsonbe726152010-07-23 23:18:50 +01003644 INIT_LIST_HEAD(&dev_priv->mm.deferred_free_list);
Daniel Vetter93a37f22010-11-05 20:24:53 +01003645 INIT_LIST_HEAD(&dev_priv->mm.gtt_list);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003646 for (i = 0; i < I915_NUM_RINGS; i++)
3647 init_ring_lists(&dev_priv->ring[i]);
Daniel Vetter4b9de732011-10-09 21:52:02 +02003648 for (i = 0; i < I915_MAX_NUM_FENCES; i++)
Daniel Vetter007cc8a2010-04-28 11:02:31 +02003649 INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
Eric Anholt673a3942008-07-30 12:06:12 -07003650 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
3651 i915_gem_retire_work_handler);
Chris Wilson30dbf0c2010-09-25 10:19:17 +01003652 init_completion(&dev_priv->error_completion);
Chris Wilson31169712009-09-14 16:50:28 +01003653
Dave Airlie94400122010-07-20 13:15:31 +10003654 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
3655 if (IS_GEN3(dev)) {
3656 u32 tmp = I915_READ(MI_ARB_STATE);
3657 if (!(tmp & MI_ARB_C3_LP_WRITE_ENABLE)) {
3658 /* arb state is a masked write, so set bit + bit in mask */
3659 tmp = MI_ARB_C3_LP_WRITE_ENABLE | (MI_ARB_C3_LP_WRITE_ENABLE << MI_ARB_MASK_SHIFT);
3660 I915_WRITE(MI_ARB_STATE, tmp);
3661 }
3662 }
3663
Chris Wilson72bfa192010-12-19 11:42:05 +00003664 dev_priv->relative_constants_mode = I915_EXEC_CONSTANTS_REL_GENERAL;
3665
Jesse Barnesde151cf2008-11-12 10:03:55 -08003666 /* Old X drivers will take 0-2 for front, back, depth buffers */
Eric Anholtb397c832010-01-26 09:43:10 -08003667 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3668 dev_priv->fence_reg_start = 3;
Jesse Barnesde151cf2008-11-12 10:03:55 -08003669
Chris Wilsona6c45cf2010-09-17 00:32:17 +01003670 if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08003671 dev_priv->num_fence_regs = 16;
3672 else
3673 dev_priv->num_fence_regs = 8;
3674
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02003675 /* Initialize fence registers to zero */
Eric Anholt10ed13e2011-05-06 13:53:49 -07003676 for (i = 0; i < dev_priv->num_fence_regs; i++) {
3677 i915_gem_clear_fence_reg(dev, &dev_priv->fence_regs[i]);
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02003678 }
Eric Anholt10ed13e2011-05-06 13:53:49 -07003679
Eric Anholt673a3942008-07-30 12:06:12 -07003680 i915_gem_detect_bit_6_swizzle(dev);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003681 init_waitqueue_head(&dev_priv->pending_flip_queue);
Chris Wilson17250b72010-10-28 12:51:39 +01003682
Chris Wilsonce453d82011-02-21 14:43:56 +00003683 dev_priv->mm.interruptible = true;
3684
Chris Wilson17250b72010-10-28 12:51:39 +01003685 dev_priv->mm.inactive_shrinker.shrink = i915_gem_inactive_shrink;
3686 dev_priv->mm.inactive_shrinker.seeks = DEFAULT_SEEKS;
3687 register_shrinker(&dev_priv->mm.inactive_shrinker);
Eric Anholt673a3942008-07-30 12:06:12 -07003688}
Dave Airlie71acb5e2008-12-30 20:31:46 +10003689
3690/*
3691 * Create a physically contiguous memory object for this object
3692 * e.g. for cursor + overlay regs
3693 */
Chris Wilson995b6762010-08-20 13:23:26 +01003694static int i915_gem_init_phys_object(struct drm_device *dev,
3695 int id, int size, int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10003696{
3697 drm_i915_private_t *dev_priv = dev->dev_private;
3698 struct drm_i915_gem_phys_object *phys_obj;
3699 int ret;
3700
3701 if (dev_priv->mm.phys_objs[id - 1] || !size)
3702 return 0;
3703
Eric Anholt9a298b22009-03-24 12:23:04 -07003704 phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
Dave Airlie71acb5e2008-12-30 20:31:46 +10003705 if (!phys_obj)
3706 return -ENOMEM;
3707
3708 phys_obj->id = id;
3709
Chris Wilson6eeefaf2010-08-07 11:01:39 +01003710 phys_obj->handle = drm_pci_alloc(dev, size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10003711 if (!phys_obj->handle) {
3712 ret = -ENOMEM;
3713 goto kfree_obj;
3714 }
3715#ifdef CONFIG_X86
3716 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
3717#endif
3718
3719 dev_priv->mm.phys_objs[id - 1] = phys_obj;
3720
3721 return 0;
3722kfree_obj:
Eric Anholt9a298b22009-03-24 12:23:04 -07003723 kfree(phys_obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10003724 return ret;
3725}
3726
Chris Wilson995b6762010-08-20 13:23:26 +01003727static void i915_gem_free_phys_object(struct drm_device *dev, int id)
Dave Airlie71acb5e2008-12-30 20:31:46 +10003728{
3729 drm_i915_private_t *dev_priv = dev->dev_private;
3730 struct drm_i915_gem_phys_object *phys_obj;
3731
3732 if (!dev_priv->mm.phys_objs[id - 1])
3733 return;
3734
3735 phys_obj = dev_priv->mm.phys_objs[id - 1];
3736 if (phys_obj->cur_obj) {
3737 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
3738 }
3739
3740#ifdef CONFIG_X86
3741 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
3742#endif
3743 drm_pci_free(dev, phys_obj->handle);
3744 kfree(phys_obj);
3745 dev_priv->mm.phys_objs[id - 1] = NULL;
3746}
3747
3748void i915_gem_free_all_phys_object(struct drm_device *dev)
3749{
3750 int i;
3751
Dave Airlie260883c2009-01-22 17:58:49 +10003752 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
Dave Airlie71acb5e2008-12-30 20:31:46 +10003753 i915_gem_free_phys_object(dev, i);
3754}
3755
3756void i915_gem_detach_phys_object(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00003757 struct drm_i915_gem_object *obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10003758{
Chris Wilson05394f32010-11-08 19:18:58 +00003759 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Chris Wilsone5281cc2010-10-28 13:45:36 +01003760 char *vaddr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10003761 int i;
Dave Airlie71acb5e2008-12-30 20:31:46 +10003762 int page_count;
3763
Chris Wilson05394f32010-11-08 19:18:58 +00003764 if (!obj->phys_obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10003765 return;
Chris Wilson05394f32010-11-08 19:18:58 +00003766 vaddr = obj->phys_obj->handle->vaddr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10003767
Chris Wilson05394f32010-11-08 19:18:58 +00003768 page_count = obj->base.size / PAGE_SIZE;
Dave Airlie71acb5e2008-12-30 20:31:46 +10003769 for (i = 0; i < page_count; i++) {
Hugh Dickins5949eac2011-06-27 16:18:18 -07003770 struct page *page = shmem_read_mapping_page(mapping, i);
Chris Wilsone5281cc2010-10-28 13:45:36 +01003771 if (!IS_ERR(page)) {
3772 char *dst = kmap_atomic(page);
3773 memcpy(dst, vaddr + i*PAGE_SIZE, PAGE_SIZE);
3774 kunmap_atomic(dst);
Dave Airlie71acb5e2008-12-30 20:31:46 +10003775
Chris Wilsone5281cc2010-10-28 13:45:36 +01003776 drm_clflush_pages(&page, 1);
3777
3778 set_page_dirty(page);
3779 mark_page_accessed(page);
3780 page_cache_release(page);
3781 }
Dave Airlie71acb5e2008-12-30 20:31:46 +10003782 }
Daniel Vetter40ce6572010-11-05 18:12:18 +01003783 intel_gtt_chipset_flush();
Chris Wilsond78b47b2009-06-17 21:52:49 +01003784
Chris Wilson05394f32010-11-08 19:18:58 +00003785 obj->phys_obj->cur_obj = NULL;
3786 obj->phys_obj = NULL;
Dave Airlie71acb5e2008-12-30 20:31:46 +10003787}
3788
3789int
3790i915_gem_attach_phys_object(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00003791 struct drm_i915_gem_object *obj,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01003792 int id,
3793 int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10003794{
Chris Wilson05394f32010-11-08 19:18:58 +00003795 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Dave Airlie71acb5e2008-12-30 20:31:46 +10003796 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie71acb5e2008-12-30 20:31:46 +10003797 int ret = 0;
3798 int page_count;
3799 int i;
3800
3801 if (id > I915_MAX_PHYS_OBJECT)
3802 return -EINVAL;
3803
Chris Wilson05394f32010-11-08 19:18:58 +00003804 if (obj->phys_obj) {
3805 if (obj->phys_obj->id == id)
Dave Airlie71acb5e2008-12-30 20:31:46 +10003806 return 0;
3807 i915_gem_detach_phys_object(dev, obj);
3808 }
3809
Dave Airlie71acb5e2008-12-30 20:31:46 +10003810 /* create a new object */
3811 if (!dev_priv->mm.phys_objs[id - 1]) {
3812 ret = i915_gem_init_phys_object(dev, id,
Chris Wilson05394f32010-11-08 19:18:58 +00003813 obj->base.size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10003814 if (ret) {
Chris Wilson05394f32010-11-08 19:18:58 +00003815 DRM_ERROR("failed to init phys object %d size: %zu\n",
3816 id, obj->base.size);
Chris Wilsone5281cc2010-10-28 13:45:36 +01003817 return ret;
Dave Airlie71acb5e2008-12-30 20:31:46 +10003818 }
3819 }
3820
3821 /* bind to the object */
Chris Wilson05394f32010-11-08 19:18:58 +00003822 obj->phys_obj = dev_priv->mm.phys_objs[id - 1];
3823 obj->phys_obj->cur_obj = obj;
Dave Airlie71acb5e2008-12-30 20:31:46 +10003824
Chris Wilson05394f32010-11-08 19:18:58 +00003825 page_count = obj->base.size / PAGE_SIZE;
Dave Airlie71acb5e2008-12-30 20:31:46 +10003826
3827 for (i = 0; i < page_count; i++) {
Chris Wilsone5281cc2010-10-28 13:45:36 +01003828 struct page *page;
3829 char *dst, *src;
Dave Airlie71acb5e2008-12-30 20:31:46 +10003830
Hugh Dickins5949eac2011-06-27 16:18:18 -07003831 page = shmem_read_mapping_page(mapping, i);
Chris Wilsone5281cc2010-10-28 13:45:36 +01003832 if (IS_ERR(page))
3833 return PTR_ERR(page);
3834
Chris Wilsonff75b9b2010-10-30 22:52:31 +01003835 src = kmap_atomic(page);
Chris Wilson05394f32010-11-08 19:18:58 +00003836 dst = obj->phys_obj->handle->vaddr + (i * PAGE_SIZE);
Dave Airlie71acb5e2008-12-30 20:31:46 +10003837 memcpy(dst, src, PAGE_SIZE);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -07003838 kunmap_atomic(src);
Chris Wilsone5281cc2010-10-28 13:45:36 +01003839
3840 mark_page_accessed(page);
3841 page_cache_release(page);
Dave Airlie71acb5e2008-12-30 20:31:46 +10003842 }
3843
3844 return 0;
Dave Airlie71acb5e2008-12-30 20:31:46 +10003845}
3846
3847static int
Chris Wilson05394f32010-11-08 19:18:58 +00003848i915_gem_phys_pwrite(struct drm_device *dev,
3849 struct drm_i915_gem_object *obj,
Dave Airlie71acb5e2008-12-30 20:31:46 +10003850 struct drm_i915_gem_pwrite *args,
3851 struct drm_file *file_priv)
3852{
Chris Wilson05394f32010-11-08 19:18:58 +00003853 void *vaddr = obj->phys_obj->handle->vaddr + args->offset;
Chris Wilsonb47b30c2010-11-08 01:12:29 +00003854 char __user *user_data = (char __user *) (uintptr_t) args->data_ptr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10003855
Chris Wilsonb47b30c2010-11-08 01:12:29 +00003856 if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) {
3857 unsigned long unwritten;
3858
3859 /* The physical object once assigned is fixed for the lifetime
3860 * of the obj, so we can safely drop the lock and continue
3861 * to access vaddr.
3862 */
3863 mutex_unlock(&dev->struct_mutex);
3864 unwritten = copy_from_user(vaddr, user_data, args->size);
3865 mutex_lock(&dev->struct_mutex);
3866 if (unwritten)
3867 return -EFAULT;
3868 }
Dave Airlie71acb5e2008-12-30 20:31:46 +10003869
Daniel Vetter40ce6572010-11-05 18:12:18 +01003870 intel_gtt_chipset_flush();
Dave Airlie71acb5e2008-12-30 20:31:46 +10003871 return 0;
3872}
Eric Anholtb9624422009-06-03 07:27:35 +00003873
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003874void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00003875{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003876 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00003877
3878 /* Clean up our request list when the client is going away, so that
3879 * later retire_requests won't dereference our soon-to-be-gone
3880 * file_priv.
3881 */
Chris Wilson1c255952010-09-26 11:03:27 +01003882 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003883 while (!list_empty(&file_priv->mm.request_list)) {
3884 struct drm_i915_gem_request *request;
3885
3886 request = list_first_entry(&file_priv->mm.request_list,
3887 struct drm_i915_gem_request,
3888 client_list);
3889 list_del(&request->client_list);
3890 request->file_priv = NULL;
3891 }
Chris Wilson1c255952010-09-26 11:03:27 +01003892 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00003893}
Chris Wilson31169712009-09-14 16:50:28 +01003894
Chris Wilson31169712009-09-14 16:50:28 +01003895static int
Chris Wilson1637ef42010-04-20 17:10:35 +01003896i915_gpu_is_active(struct drm_device *dev)
3897{
3898 drm_i915_private_t *dev_priv = dev->dev_private;
3899 int lists_empty;
3900
Chris Wilson1637ef42010-04-20 17:10:35 +01003901 lists_empty = list_empty(&dev_priv->mm.flushing_list) &&
Chris Wilson17250b72010-10-28 12:51:39 +01003902 list_empty(&dev_priv->mm.active_list);
Chris Wilson1637ef42010-04-20 17:10:35 +01003903
3904 return !lists_empty;
3905}
3906
3907static int
Ying Han1495f232011-05-24 17:12:27 -07003908i915_gem_inactive_shrink(struct shrinker *shrinker, struct shrink_control *sc)
Chris Wilson31169712009-09-14 16:50:28 +01003909{
Chris Wilson17250b72010-10-28 12:51:39 +01003910 struct drm_i915_private *dev_priv =
3911 container_of(shrinker,
3912 struct drm_i915_private,
3913 mm.inactive_shrinker);
3914 struct drm_device *dev = dev_priv->dev;
3915 struct drm_i915_gem_object *obj, *next;
Ying Han1495f232011-05-24 17:12:27 -07003916 int nr_to_scan = sc->nr_to_scan;
Chris Wilson17250b72010-10-28 12:51:39 +01003917 int cnt;
3918
3919 if (!mutex_trylock(&dev->struct_mutex))
Chris Wilsonbbe2e112010-10-28 22:35:07 +01003920 return 0;
Chris Wilson31169712009-09-14 16:50:28 +01003921
3922 /* "fast-path" to count number of available objects */
3923 if (nr_to_scan == 0) {
Chris Wilson17250b72010-10-28 12:51:39 +01003924 cnt = 0;
3925 list_for_each_entry(obj,
3926 &dev_priv->mm.inactive_list,
3927 mm_list)
3928 cnt++;
3929 mutex_unlock(&dev->struct_mutex);
3930 return cnt / 100 * sysctl_vfs_cache_pressure;
Chris Wilson31169712009-09-14 16:50:28 +01003931 }
3932
Chris Wilson1637ef42010-04-20 17:10:35 +01003933rescan:
Chris Wilson31169712009-09-14 16:50:28 +01003934 /* first scan for clean buffers */
Chris Wilson17250b72010-10-28 12:51:39 +01003935 i915_gem_retire_requests(dev);
Chris Wilson31169712009-09-14 16:50:28 +01003936
Chris Wilson17250b72010-10-28 12:51:39 +01003937 list_for_each_entry_safe(obj, next,
3938 &dev_priv->mm.inactive_list,
3939 mm_list) {
3940 if (i915_gem_object_is_purgeable(obj)) {
Chris Wilson20217462010-11-23 15:26:33 +00003941 if (i915_gem_object_unbind(obj) == 0 &&
3942 --nr_to_scan == 0)
Chris Wilson17250b72010-10-28 12:51:39 +01003943 break;
Chris Wilson31169712009-09-14 16:50:28 +01003944 }
Chris Wilson31169712009-09-14 16:50:28 +01003945 }
3946
3947 /* second pass, evict/count anything still on the inactive list */
Chris Wilson17250b72010-10-28 12:51:39 +01003948 cnt = 0;
3949 list_for_each_entry_safe(obj, next,
3950 &dev_priv->mm.inactive_list,
3951 mm_list) {
Chris Wilson20217462010-11-23 15:26:33 +00003952 if (nr_to_scan &&
3953 i915_gem_object_unbind(obj) == 0)
Chris Wilson17250b72010-10-28 12:51:39 +01003954 nr_to_scan--;
Chris Wilson20217462010-11-23 15:26:33 +00003955 else
Chris Wilson17250b72010-10-28 12:51:39 +01003956 cnt++;
Chris Wilson31169712009-09-14 16:50:28 +01003957 }
3958
Chris Wilson17250b72010-10-28 12:51:39 +01003959 if (nr_to_scan && i915_gpu_is_active(dev)) {
Chris Wilson1637ef42010-04-20 17:10:35 +01003960 /*
3961 * We are desperate for pages, so as a last resort, wait
3962 * for the GPU to finish and discard whatever we can.
3963 * This has a dramatic impact to reduce the number of
3964 * OOM-killer events whilst running the GPU aggressively.
3965 */
Ben Widawskyb93f9cf2012-01-25 15:39:34 -08003966 if (i915_gpu_idle(dev, true) == 0)
Chris Wilson1637ef42010-04-20 17:10:35 +01003967 goto rescan;
3968 }
Chris Wilson17250b72010-10-28 12:51:39 +01003969 mutex_unlock(&dev->struct_mutex);
3970 return cnt / 100 * sysctl_vfs_cache_pressure;
Chris Wilson31169712009-09-14 16:50:28 +01003971}