blob: ffea847c8a0d98f631be615717234e894e01430f [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"
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Eric Anholt673a3942008-07-30 12:06:12 -070035#include <linux/swap.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080036#include <linux/pci.h>
Zhenyu Wangf8f235e2010-08-27 11:08:57 +080037#include <linux/intel-gtt.h>
Eric Anholt673a3942008-07-30 12:06:12 -070038
Chris Wilson0f8c6d72010-11-01 12:38:44 +000039struct change_domains {
40 uint32_t invalidate_domains;
41 uint32_t flush_domains;
42 uint32_t flush_rings;
43};
44
Chris Wilsona00b10c2010-09-24 21:15:47 +010045static uint32_t i915_gem_get_gtt_alignment(struct drm_i915_gem_object *obj_priv);
46static uint32_t i915_gem_get_gtt_size(struct drm_i915_gem_object *obj_priv);
Daniel Vetterba3d8d72010-02-11 22:37:04 +010047
48static int i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj,
49 bool pipelined);
Eric Anholte47c68e2008-11-14 13:35:19 -080050static void i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj);
51static void i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj);
Eric Anholte47c68e2008-11-14 13:35:19 -080052static int i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj,
53 int write);
54static int i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
55 uint64_t offset,
56 uint64_t size);
57static void i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj);
Chris Wilson2cf34d72010-09-14 13:03:28 +010058static int i915_gem_object_wait_rendering(struct drm_gem_object *obj,
59 bool interruptible);
Jesse Barnesde151cf2008-11-12 10:03:55 -080060static int i915_gem_object_bind_to_gtt(struct drm_gem_object *obj,
Chris Wilsona00b10c2010-09-24 21:15:47 +010061 unsigned alignment,
62 bool mappable,
63 bool need_fence);
Jesse Barnesde151cf2008-11-12 10:03:55 -080064static void i915_gem_clear_fence_reg(struct drm_gem_object *obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +100065static int i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
66 struct drm_i915_gem_pwrite *args,
67 struct drm_file *file_priv);
Chris Wilsonbe726152010-07-23 23:18:50 +010068static void i915_gem_free_object_tail(struct drm_gem_object *obj);
Eric Anholt673a3942008-07-30 12:06:12 -070069
Chris Wilson17250b72010-10-28 12:51:39 +010070static int i915_gem_inactive_shrink(struct shrinker *shrinker,
71 int nr_to_scan,
72 gfp_t gfp_mask);
73
Chris Wilson31169712009-09-14 16:50:28 +010074
Chris Wilson73aa8082010-09-30 11:46:12 +010075/* some bookkeeping */
76static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
77 size_t size)
78{
79 dev_priv->mm.object_count++;
80 dev_priv->mm.object_memory += size;
81}
82
83static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
84 size_t size)
85{
86 dev_priv->mm.object_count--;
87 dev_priv->mm.object_memory -= size;
88}
89
90static void i915_gem_info_add_gtt(struct drm_i915_private *dev_priv,
Chris Wilsona00b10c2010-09-24 21:15:47 +010091 struct drm_i915_gem_object *obj)
Chris Wilson73aa8082010-09-30 11:46:12 +010092{
93 dev_priv->mm.gtt_count++;
Chris Wilsona00b10c2010-09-24 21:15:47 +010094 dev_priv->mm.gtt_memory += obj->gtt_space->size;
95 if (obj->gtt_offset < dev_priv->mm.gtt_mappable_end) {
Daniel Vetterfb7d5162010-10-01 22:05:20 +020096 dev_priv->mm.mappable_gtt_used +=
Chris Wilsona00b10c2010-09-24 21:15:47 +010097 min_t(size_t, obj->gtt_space->size,
98 dev_priv->mm.gtt_mappable_end - obj->gtt_offset);
Daniel Vetterfb7d5162010-10-01 22:05:20 +020099 }
Chris Wilson73aa8082010-09-30 11:46:12 +0100100}
101
102static void i915_gem_info_remove_gtt(struct drm_i915_private *dev_priv,
Chris Wilsona00b10c2010-09-24 21:15:47 +0100103 struct drm_i915_gem_object *obj)
Chris Wilson73aa8082010-09-30 11:46:12 +0100104{
105 dev_priv->mm.gtt_count--;
Chris Wilsona00b10c2010-09-24 21:15:47 +0100106 dev_priv->mm.gtt_memory -= obj->gtt_space->size;
107 if (obj->gtt_offset < dev_priv->mm.gtt_mappable_end) {
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200108 dev_priv->mm.mappable_gtt_used -=
Chris Wilsona00b10c2010-09-24 21:15:47 +0100109 min_t(size_t, obj->gtt_space->size,
110 dev_priv->mm.gtt_mappable_end - obj->gtt_offset);
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200111 }
112}
113
114/**
115 * Update the mappable working set counters. Call _only_ when there is a change
116 * in one of (pin|fault)_mappable and update *_mappable _before_ calling.
117 * @mappable: new state the changed mappable flag (either pin_ or fault_).
118 */
119static void
120i915_gem_info_update_mappable(struct drm_i915_private *dev_priv,
Chris Wilsona00b10c2010-09-24 21:15:47 +0100121 struct drm_i915_gem_object *obj,
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200122 bool mappable)
123{
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200124 if (mappable) {
Chris Wilsona00b10c2010-09-24 21:15:47 +0100125 if (obj->pin_mappable && obj->fault_mappable)
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200126 /* Combined state was already mappable. */
127 return;
128 dev_priv->mm.gtt_mappable_count++;
Chris Wilsona00b10c2010-09-24 21:15:47 +0100129 dev_priv->mm.gtt_mappable_memory += obj->gtt_space->size;
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200130 } else {
Chris Wilsona00b10c2010-09-24 21:15:47 +0100131 if (obj->pin_mappable || obj->fault_mappable)
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200132 /* Combined state still mappable. */
133 return;
134 dev_priv->mm.gtt_mappable_count--;
Chris Wilsona00b10c2010-09-24 21:15:47 +0100135 dev_priv->mm.gtt_mappable_memory -= obj->gtt_space->size;
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200136 }
Chris Wilson73aa8082010-09-30 11:46:12 +0100137}
138
139static void i915_gem_info_add_pin(struct drm_i915_private *dev_priv,
Chris Wilsona00b10c2010-09-24 21:15:47 +0100140 struct drm_i915_gem_object *obj,
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200141 bool mappable)
Chris Wilson73aa8082010-09-30 11:46:12 +0100142{
143 dev_priv->mm.pin_count++;
Chris Wilsona00b10c2010-09-24 21:15:47 +0100144 dev_priv->mm.pin_memory += obj->gtt_space->size;
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200145 if (mappable) {
Chris Wilsona00b10c2010-09-24 21:15:47 +0100146 obj->pin_mappable = true;
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200147 i915_gem_info_update_mappable(dev_priv, obj, true);
148 }
Chris Wilson73aa8082010-09-30 11:46:12 +0100149}
150
151static void i915_gem_info_remove_pin(struct drm_i915_private *dev_priv,
Chris Wilsona00b10c2010-09-24 21:15:47 +0100152 struct drm_i915_gem_object *obj)
Chris Wilson73aa8082010-09-30 11:46:12 +0100153{
154 dev_priv->mm.pin_count--;
Chris Wilsona00b10c2010-09-24 21:15:47 +0100155 dev_priv->mm.pin_memory -= obj->gtt_space->size;
156 if (obj->pin_mappable) {
157 obj->pin_mappable = false;
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200158 i915_gem_info_update_mappable(dev_priv, obj, false);
159 }
Chris Wilson73aa8082010-09-30 11:46:12 +0100160}
161
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100162int
163i915_gem_check_is_wedged(struct drm_device *dev)
164{
165 struct drm_i915_private *dev_priv = dev->dev_private;
166 struct completion *x = &dev_priv->error_completion;
167 unsigned long flags;
168 int ret;
169
170 if (!atomic_read(&dev_priv->mm.wedged))
171 return 0;
172
173 ret = wait_for_completion_interruptible(x);
174 if (ret)
175 return ret;
176
177 /* Success, we reset the GPU! */
178 if (!atomic_read(&dev_priv->mm.wedged))
179 return 0;
180
181 /* GPU is hung, bump the completion count to account for
182 * the token we just consumed so that we never hit zero and
183 * end up waiting upon a subsequent completion event that
184 * will never happen.
185 */
186 spin_lock_irqsave(&x->wait.lock, flags);
187 x->done++;
188 spin_unlock_irqrestore(&x->wait.lock, flags);
189 return -EIO;
190}
191
Chris Wilson76c1dec2010-09-25 11:22:51 +0100192static int i915_mutex_lock_interruptible(struct drm_device *dev)
193{
194 struct drm_i915_private *dev_priv = dev->dev_private;
195 int ret;
196
197 ret = i915_gem_check_is_wedged(dev);
198 if (ret)
199 return ret;
200
201 ret = mutex_lock_interruptible(&dev->struct_mutex);
202 if (ret)
203 return ret;
204
205 if (atomic_read(&dev_priv->mm.wedged)) {
206 mutex_unlock(&dev->struct_mutex);
207 return -EAGAIN;
208 }
209
Chris Wilson23bc5982010-09-29 16:10:57 +0100210 WARN_ON(i915_verify_lists(dev));
Chris Wilson76c1dec2010-09-25 11:22:51 +0100211 return 0;
212}
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100213
Chris Wilson7d1c4802010-08-07 21:45:03 +0100214static inline bool
215i915_gem_object_is_inactive(struct drm_i915_gem_object *obj_priv)
216{
217 return obj_priv->gtt_space &&
218 !obj_priv->active &&
219 obj_priv->pin_count == 0;
220}
221
Chris Wilson73aa8082010-09-30 11:46:12 +0100222int i915_gem_do_init(struct drm_device *dev,
223 unsigned long start,
Daniel Vetter53984632010-09-22 23:44:24 +0200224 unsigned long mappable_end,
Jesse Barnes79e53942008-11-07 14:24:08 -0800225 unsigned long end)
226{
227 drm_i915_private_t *dev_priv = dev->dev_private;
228
229 if (start >= end ||
230 (start & (PAGE_SIZE - 1)) != 0 ||
231 (end & (PAGE_SIZE - 1)) != 0) {
232 return -EINVAL;
233 }
234
235 drm_mm_init(&dev_priv->mm.gtt_space, start,
236 end - start);
237
Chris Wilson73aa8082010-09-30 11:46:12 +0100238 dev_priv->mm.gtt_total = end - start;
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200239 dev_priv->mm.mappable_gtt_total = min(end, mappable_end) - start;
Daniel Vetter53984632010-09-22 23:44:24 +0200240 dev_priv->mm.gtt_mappable_end = mappable_end;
Jesse Barnes79e53942008-11-07 14:24:08 -0800241
242 return 0;
243}
Keith Packard6dbe2772008-10-14 21:41:13 -0700244
Eric Anholt673a3942008-07-30 12:06:12 -0700245int
246i915_gem_init_ioctl(struct drm_device *dev, void *data,
247 struct drm_file *file_priv)
248{
Eric Anholt673a3942008-07-30 12:06:12 -0700249 struct drm_i915_gem_init *args = data;
Jesse Barnes79e53942008-11-07 14:24:08 -0800250 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700251
252 mutex_lock(&dev->struct_mutex);
Daniel Vetter53984632010-09-22 23:44:24 +0200253 ret = i915_gem_do_init(dev, args->gtt_start, args->gtt_end, args->gtt_end);
Eric Anholt673a3942008-07-30 12:06:12 -0700254 mutex_unlock(&dev->struct_mutex);
255
Jesse Barnes79e53942008-11-07 14:24:08 -0800256 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700257}
258
Eric Anholt5a125c32008-10-22 21:40:13 -0700259int
260i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
261 struct drm_file *file_priv)
262{
Chris Wilson73aa8082010-09-30 11:46:12 +0100263 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt5a125c32008-10-22 21:40:13 -0700264 struct drm_i915_gem_get_aperture *args = data;
Eric Anholt5a125c32008-10-22 21:40:13 -0700265
266 if (!(dev->driver->driver_features & DRIVER_GEM))
267 return -ENODEV;
268
Chris Wilson73aa8082010-09-30 11:46:12 +0100269 mutex_lock(&dev->struct_mutex);
270 args->aper_size = dev_priv->mm.gtt_total;
271 args->aper_available_size = args->aper_size - dev_priv->mm.pin_memory;
272 mutex_unlock(&dev->struct_mutex);
Eric Anholt5a125c32008-10-22 21:40:13 -0700273
274 return 0;
275}
276
Eric Anholt673a3942008-07-30 12:06:12 -0700277
278/**
279 * Creates a new mm object and returns a handle to it.
280 */
281int
282i915_gem_create_ioctl(struct drm_device *dev, void *data,
283 struct drm_file *file_priv)
284{
285 struct drm_i915_gem_create *args = data;
286 struct drm_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300287 int ret;
288 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700289
290 args->size = roundup(args->size, PAGE_SIZE);
291
292 /* Allocate the new object */
Daniel Vetterac52bc52010-04-09 19:05:06 +0000293 obj = i915_gem_alloc_object(dev, args->size);
Eric Anholt673a3942008-07-30 12:06:12 -0700294 if (obj == NULL)
295 return -ENOMEM;
296
297 ret = drm_gem_handle_create(file_priv, obj, &handle);
Chris Wilson1dfd9752010-09-06 14:44:14 +0100298 if (ret) {
Chris Wilson202f2fe2010-10-14 13:20:40 +0100299 drm_gem_object_release(obj);
300 i915_gem_info_remove_obj(dev->dev_private, obj->size);
301 kfree(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700302 return ret;
Chris Wilson1dfd9752010-09-06 14:44:14 +0100303 }
304
Chris Wilson202f2fe2010-10-14 13:20:40 +0100305 /* drop reference from allocate - handle holds it now */
306 drm_gem_object_unreference(obj);
307 trace_i915_gem_object_create(obj);
308
Eric Anholt673a3942008-07-30 12:06:12 -0700309 args->handle = handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700310 return 0;
311}
312
Eric Anholt280b7132009-03-12 16:56:27 -0700313static int i915_gem_object_needs_bit17_swizzle(struct drm_gem_object *obj)
314{
315 drm_i915_private_t *dev_priv = obj->dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +0100316 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt280b7132009-03-12 16:56:27 -0700317
318 return dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_9_10_17 &&
319 obj_priv->tiling_mode != I915_TILING_NONE;
320}
321
Chris Wilson99a03df2010-05-27 14:15:34 +0100322static inline void
Eric Anholt40123c12009-03-09 13:42:30 -0700323slow_shmem_copy(struct page *dst_page,
324 int dst_offset,
325 struct page *src_page,
326 int src_offset,
327 int length)
328{
329 char *dst_vaddr, *src_vaddr;
330
Chris Wilson99a03df2010-05-27 14:15:34 +0100331 dst_vaddr = kmap(dst_page);
332 src_vaddr = kmap(src_page);
Eric Anholt40123c12009-03-09 13:42:30 -0700333
334 memcpy(dst_vaddr + dst_offset, src_vaddr + src_offset, length);
335
Chris Wilson99a03df2010-05-27 14:15:34 +0100336 kunmap(src_page);
337 kunmap(dst_page);
Eric Anholt40123c12009-03-09 13:42:30 -0700338}
339
Chris Wilson99a03df2010-05-27 14:15:34 +0100340static inline void
Eric Anholt280b7132009-03-12 16:56:27 -0700341slow_shmem_bit17_copy(struct page *gpu_page,
342 int gpu_offset,
343 struct page *cpu_page,
344 int cpu_offset,
345 int length,
346 int is_read)
347{
348 char *gpu_vaddr, *cpu_vaddr;
349
350 /* Use the unswizzled path if this page isn't affected. */
351 if ((page_to_phys(gpu_page) & (1 << 17)) == 0) {
352 if (is_read)
353 return slow_shmem_copy(cpu_page, cpu_offset,
354 gpu_page, gpu_offset, length);
355 else
356 return slow_shmem_copy(gpu_page, gpu_offset,
357 cpu_page, cpu_offset, length);
358 }
359
Chris Wilson99a03df2010-05-27 14:15:34 +0100360 gpu_vaddr = kmap(gpu_page);
361 cpu_vaddr = kmap(cpu_page);
Eric Anholt280b7132009-03-12 16:56:27 -0700362
363 /* Copy the data, XORing A6 with A17 (1). The user already knows he's
364 * XORing with the other bits (A9 for Y, A9 and A10 for X)
365 */
366 while (length > 0) {
367 int cacheline_end = ALIGN(gpu_offset + 1, 64);
368 int this_length = min(cacheline_end - gpu_offset, length);
369 int swizzled_gpu_offset = gpu_offset ^ 64;
370
371 if (is_read) {
372 memcpy(cpu_vaddr + cpu_offset,
373 gpu_vaddr + swizzled_gpu_offset,
374 this_length);
375 } else {
376 memcpy(gpu_vaddr + swizzled_gpu_offset,
377 cpu_vaddr + cpu_offset,
378 this_length);
379 }
380 cpu_offset += this_length;
381 gpu_offset += this_length;
382 length -= this_length;
383 }
384
Chris Wilson99a03df2010-05-27 14:15:34 +0100385 kunmap(cpu_page);
386 kunmap(gpu_page);
Eric Anholt280b7132009-03-12 16:56:27 -0700387}
388
Eric Anholt673a3942008-07-30 12:06:12 -0700389/**
Eric Anholteb014592009-03-10 11:44:52 -0700390 * This is the fast shmem pread path, which attempts to copy_from_user directly
391 * from the backing pages of the object to the user's address space. On a
392 * fault, it fails so we can fall back to i915_gem_shmem_pwrite_slow().
393 */
394static int
395i915_gem_shmem_pread_fast(struct drm_device *dev, struct drm_gem_object *obj,
396 struct drm_i915_gem_pread *args,
397 struct drm_file *file_priv)
398{
Daniel Vetter23010e42010-03-08 13:35:02 +0100399 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsone5281cc2010-10-28 13:45:36 +0100400 struct address_space *mapping = obj->filp->f_path.dentry->d_inode->i_mapping;
Eric Anholteb014592009-03-10 11:44:52 -0700401 ssize_t remain;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100402 loff_t offset;
Eric Anholteb014592009-03-10 11:44:52 -0700403 char __user *user_data;
404 int page_offset, page_length;
Eric Anholteb014592009-03-10 11:44:52 -0700405
406 user_data = (char __user *) (uintptr_t) args->data_ptr;
407 remain = args->size;
408
Daniel Vetter23010e42010-03-08 13:35:02 +0100409 obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700410 offset = args->offset;
411
412 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100413 struct page *page;
414 char *vaddr;
415 int ret;
416
Eric Anholteb014592009-03-10 11:44:52 -0700417 /* Operation in this page
418 *
Eric Anholteb014592009-03-10 11:44:52 -0700419 * page_offset = offset within page
420 * page_length = bytes to copy for this page
421 */
Eric Anholteb014592009-03-10 11:44:52 -0700422 page_offset = offset & (PAGE_SIZE-1);
423 page_length = remain;
424 if ((page_offset + remain) > PAGE_SIZE)
425 page_length = PAGE_SIZE - page_offset;
426
Chris Wilsone5281cc2010-10-28 13:45:36 +0100427 page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
428 GFP_HIGHUSER | __GFP_RECLAIMABLE);
429 if (IS_ERR(page))
430 return PTR_ERR(page);
431
432 vaddr = kmap_atomic(page);
433 ret = __copy_to_user_inatomic(user_data,
434 vaddr + page_offset,
435 page_length);
436 kunmap_atomic(vaddr);
437
438 mark_page_accessed(page);
439 page_cache_release(page);
440 if (ret)
Chris Wilson4f27b752010-10-14 15:26:45 +0100441 return -EFAULT;
Eric Anholteb014592009-03-10 11:44:52 -0700442
443 remain -= page_length;
444 user_data += page_length;
445 offset += page_length;
446 }
447
Chris Wilson4f27b752010-10-14 15:26:45 +0100448 return 0;
Eric Anholteb014592009-03-10 11:44:52 -0700449}
450
451/**
452 * This is the fallback shmem pread path, which allocates temporary storage
453 * in kernel space to copy_to_user into outside of the struct_mutex, so we
454 * can copy out of the object's backing pages while holding the struct mutex
455 * and not take page faults.
456 */
457static int
458i915_gem_shmem_pread_slow(struct drm_device *dev, struct drm_gem_object *obj,
459 struct drm_i915_gem_pread *args,
460 struct drm_file *file_priv)
461{
Chris Wilsone5281cc2010-10-28 13:45:36 +0100462 struct address_space *mapping = obj->filp->f_path.dentry->d_inode->i_mapping;
Daniel Vetter23010e42010-03-08 13:35:02 +0100463 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700464 struct mm_struct *mm = current->mm;
465 struct page **user_pages;
466 ssize_t remain;
467 loff_t offset, pinned_pages, i;
468 loff_t first_data_page, last_data_page, num_pages;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100469 int shmem_page_offset;
470 int data_page_index, data_page_offset;
Eric Anholteb014592009-03-10 11:44:52 -0700471 int page_length;
472 int ret;
473 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700474 int do_bit17_swizzling;
Eric Anholteb014592009-03-10 11:44:52 -0700475
476 remain = args->size;
477
478 /* Pin the user pages containing the data. We can't fault while
479 * holding the struct mutex, yet we want to hold it while
480 * dereferencing the user data.
481 */
482 first_data_page = data_ptr / PAGE_SIZE;
483 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
484 num_pages = last_data_page - first_data_page + 1;
485
Chris Wilson4f27b752010-10-14 15:26:45 +0100486 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
Eric Anholteb014592009-03-10 11:44:52 -0700487 if (user_pages == NULL)
488 return -ENOMEM;
489
Chris Wilson4f27b752010-10-14 15:26:45 +0100490 mutex_unlock(&dev->struct_mutex);
Eric Anholteb014592009-03-10 11:44:52 -0700491 down_read(&mm->mmap_sem);
492 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
Eric Anholte5e9ecd2009-04-07 16:01:22 -0700493 num_pages, 1, 0, user_pages, NULL);
Eric Anholteb014592009-03-10 11:44:52 -0700494 up_read(&mm->mmap_sem);
Chris Wilson4f27b752010-10-14 15:26:45 +0100495 mutex_lock(&dev->struct_mutex);
Eric Anholteb014592009-03-10 11:44:52 -0700496 if (pinned_pages < num_pages) {
497 ret = -EFAULT;
Chris Wilson4f27b752010-10-14 15:26:45 +0100498 goto out;
Eric Anholteb014592009-03-10 11:44:52 -0700499 }
500
Chris Wilson4f27b752010-10-14 15:26:45 +0100501 ret = i915_gem_object_set_cpu_read_domain_range(obj,
502 args->offset,
Eric Anholteb014592009-03-10 11:44:52 -0700503 args->size);
Chris Wilson4f27b752010-10-14 15:26:45 +0100504 if (ret)
505 goto out;
506
507 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700508
Daniel Vetter23010e42010-03-08 13:35:02 +0100509 obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700510 offset = args->offset;
511
512 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100513 struct page *page;
514
Eric Anholteb014592009-03-10 11:44:52 -0700515 /* Operation in this page
516 *
Eric Anholteb014592009-03-10 11:44:52 -0700517 * shmem_page_offset = offset within page in shmem file
518 * data_page_index = page number in get_user_pages return
519 * data_page_offset = offset with data_page_index page.
520 * page_length = bytes to copy for this page
521 */
Eric Anholteb014592009-03-10 11:44:52 -0700522 shmem_page_offset = offset & ~PAGE_MASK;
523 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
524 data_page_offset = data_ptr & ~PAGE_MASK;
525
526 page_length = remain;
527 if ((shmem_page_offset + page_length) > PAGE_SIZE)
528 page_length = PAGE_SIZE - shmem_page_offset;
529 if ((data_page_offset + page_length) > PAGE_SIZE)
530 page_length = PAGE_SIZE - data_page_offset;
531
Chris Wilsone5281cc2010-10-28 13:45:36 +0100532 page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
533 GFP_HIGHUSER | __GFP_RECLAIMABLE);
534 if (IS_ERR(page))
535 return PTR_ERR(page);
536
Eric Anholt280b7132009-03-12 16:56:27 -0700537 if (do_bit17_swizzling) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100538 slow_shmem_bit17_copy(page,
Eric Anholt280b7132009-03-12 16:56:27 -0700539 shmem_page_offset,
Chris Wilson99a03df2010-05-27 14:15:34 +0100540 user_pages[data_page_index],
541 data_page_offset,
542 page_length,
543 1);
544 } else {
545 slow_shmem_copy(user_pages[data_page_index],
546 data_page_offset,
Chris Wilsone5281cc2010-10-28 13:45:36 +0100547 page,
Chris Wilson99a03df2010-05-27 14:15:34 +0100548 shmem_page_offset,
549 page_length);
Eric Anholt280b7132009-03-12 16:56:27 -0700550 }
Eric Anholteb014592009-03-10 11:44:52 -0700551
Chris Wilsone5281cc2010-10-28 13:45:36 +0100552 mark_page_accessed(page);
553 page_cache_release(page);
554
Eric Anholteb014592009-03-10 11:44:52 -0700555 remain -= page_length;
556 data_ptr += page_length;
557 offset += page_length;
558 }
559
Chris Wilson4f27b752010-10-14 15:26:45 +0100560out:
Eric Anholteb014592009-03-10 11:44:52 -0700561 for (i = 0; i < pinned_pages; i++) {
562 SetPageDirty(user_pages[i]);
Chris Wilsone5281cc2010-10-28 13:45:36 +0100563 mark_page_accessed(user_pages[i]);
Eric Anholteb014592009-03-10 11:44:52 -0700564 page_cache_release(user_pages[i]);
565 }
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700566 drm_free_large(user_pages);
Eric Anholteb014592009-03-10 11:44:52 -0700567
568 return ret;
569}
570
Eric Anholt673a3942008-07-30 12:06:12 -0700571/**
572 * Reads data from the object referenced by handle.
573 *
574 * On error, the contents of *data are undefined.
575 */
576int
577i915_gem_pread_ioctl(struct drm_device *dev, void *data,
578 struct drm_file *file_priv)
579{
580 struct drm_i915_gem_pread *args = data;
581 struct drm_gem_object *obj;
582 struct drm_i915_gem_object *obj_priv;
Chris Wilson35b62a82010-09-26 20:23:38 +0100583 int ret = 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700584
Chris Wilson4f27b752010-10-14 15:26:45 +0100585 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100586 if (ret)
Chris Wilson4f27b752010-10-14 15:26:45 +0100587 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700588
589 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100590 if (obj == NULL) {
591 ret = -ENOENT;
592 goto unlock;
Chris Wilson4f27b752010-10-14 15:26:45 +0100593 }
Daniel Vetter23010e42010-03-08 13:35:02 +0100594 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700595
Chris Wilson7dcd2492010-09-26 20:21:44 +0100596 /* Bounds check source. */
597 if (args->offset > obj->size || args->size > obj->size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +0100598 ret = -EINVAL;
Chris Wilson35b62a82010-09-26 20:23:38 +0100599 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +0100600 }
601
Chris Wilson35b62a82010-09-26 20:23:38 +0100602 if (args->size == 0)
603 goto out;
604
Chris Wilsonce9d4192010-09-26 20:50:05 +0100605 if (!access_ok(VERIFY_WRITE,
606 (char __user *)(uintptr_t)args->data_ptr,
607 args->size)) {
608 ret = -EFAULT;
Chris Wilson35b62a82010-09-26 20:23:38 +0100609 goto out;
Eric Anholt673a3942008-07-30 12:06:12 -0700610 }
611
Chris Wilsonb5e4feb2010-10-14 13:47:43 +0100612 ret = fault_in_pages_writeable((char __user *)(uintptr_t)args->data_ptr,
613 args->size);
614 if (ret) {
615 ret = -EFAULT;
616 goto out;
617 }
618
Chris Wilson4f27b752010-10-14 15:26:45 +0100619 ret = i915_gem_object_set_cpu_read_domain_range(obj,
620 args->offset,
621 args->size);
622 if (ret)
Chris Wilsone5281cc2010-10-28 13:45:36 +0100623 goto out;
Chris Wilson4f27b752010-10-14 15:26:45 +0100624
625 ret = -EFAULT;
626 if (!i915_gem_object_needs_bit17_swizzle(obj))
Eric Anholt280b7132009-03-12 16:56:27 -0700627 ret = i915_gem_shmem_pread_fast(dev, obj, args, file_priv);
Chris Wilson4f27b752010-10-14 15:26:45 +0100628 if (ret == -EFAULT)
629 ret = i915_gem_shmem_pread_slow(dev, obj, args, file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -0700630
Chris Wilson35b62a82010-09-26 20:23:38 +0100631out:
Chris Wilson4f27b752010-10-14 15:26:45 +0100632 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100633unlock:
Chris Wilson4f27b752010-10-14 15:26:45 +0100634 mutex_unlock(&dev->struct_mutex);
Eric Anholteb014592009-03-10 11:44:52 -0700635 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700636}
637
Keith Packard0839ccb2008-10-30 19:38:48 -0700638/* This is the fast write path which cannot handle
639 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700640 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700641
Keith Packard0839ccb2008-10-30 19:38:48 -0700642static inline int
643fast_user_write(struct io_mapping *mapping,
644 loff_t page_base, int page_offset,
645 char __user *user_data,
646 int length)
647{
648 char *vaddr_atomic;
649 unsigned long unwritten;
650
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700651 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
Keith Packard0839ccb2008-10-30 19:38:48 -0700652 unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
653 user_data, length);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700654 io_mapping_unmap_atomic(vaddr_atomic);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100655 return unwritten;
Keith Packard0839ccb2008-10-30 19:38:48 -0700656}
657
658/* Here's the write path which can sleep for
659 * page faults
660 */
661
Chris Wilsonab34c222010-05-27 14:15:35 +0100662static inline void
Eric Anholt3de09aa2009-03-09 09:42:23 -0700663slow_kernel_write(struct io_mapping *mapping,
664 loff_t gtt_base, int gtt_offset,
665 struct page *user_page, int user_offset,
666 int length)
Keith Packard0839ccb2008-10-30 19:38:48 -0700667{
Chris Wilsonab34c222010-05-27 14:15:35 +0100668 char __iomem *dst_vaddr;
669 char *src_vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -0700670
Chris Wilsonab34c222010-05-27 14:15:35 +0100671 dst_vaddr = io_mapping_map_wc(mapping, gtt_base);
672 src_vaddr = kmap(user_page);
673
674 memcpy_toio(dst_vaddr + gtt_offset,
675 src_vaddr + user_offset,
676 length);
677
678 kunmap(user_page);
679 io_mapping_unmap(dst_vaddr);
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700680}
681
Eric Anholt3de09aa2009-03-09 09:42:23 -0700682/**
683 * This is the fast pwrite path, where we copy the data directly from the
684 * user into the GTT, uncached.
685 */
Eric Anholt673a3942008-07-30 12:06:12 -0700686static int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700687i915_gem_gtt_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
688 struct drm_i915_gem_pwrite *args,
689 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700690{
Daniel Vetter23010e42010-03-08 13:35:02 +0100691 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Keith Packard0839ccb2008-10-30 19:38:48 -0700692 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700693 ssize_t remain;
Keith Packard0839ccb2008-10-30 19:38:48 -0700694 loff_t offset, page_base;
Eric Anholt673a3942008-07-30 12:06:12 -0700695 char __user *user_data;
Keith Packard0839ccb2008-10-30 19:38:48 -0700696 int page_offset, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700697
698 user_data = (char __user *) (uintptr_t) args->data_ptr;
699 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700700
Daniel Vetter23010e42010-03-08 13:35:02 +0100701 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700702 offset = obj_priv->gtt_offset + args->offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700703
704 while (remain > 0) {
705 /* Operation in this page
706 *
Keith Packard0839ccb2008-10-30 19:38:48 -0700707 * page_base = page offset within aperture
708 * page_offset = offset within page
709 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -0700710 */
Keith Packard0839ccb2008-10-30 19:38:48 -0700711 page_base = (offset & ~(PAGE_SIZE-1));
712 page_offset = offset & (PAGE_SIZE-1);
713 page_length = remain;
714 if ((page_offset + remain) > PAGE_SIZE)
715 page_length = PAGE_SIZE - page_offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700716
Keith Packard0839ccb2008-10-30 19:38:48 -0700717 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -0700718 * source page isn't available. Return the error and we'll
719 * retry in the slow path.
Keith Packard0839ccb2008-10-30 19:38:48 -0700720 */
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100721 if (fast_user_write(dev_priv->mm.gtt_mapping, page_base,
722 page_offset, user_data, page_length))
723
724 return -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -0700725
Keith Packard0839ccb2008-10-30 19:38:48 -0700726 remain -= page_length;
727 user_data += page_length;
728 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700729 }
Eric Anholt673a3942008-07-30 12:06:12 -0700730
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100731 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700732}
733
Eric Anholt3de09aa2009-03-09 09:42:23 -0700734/**
735 * This is the fallback GTT pwrite path, which uses get_user_pages to pin
736 * the memory and maps it using kmap_atomic for copying.
737 *
738 * This code resulted in x11perf -rgb10text consuming about 10% more CPU
739 * than using i915_gem_gtt_pwrite_fast on a G45 (32-bit).
740 */
Eric Anholt3043c602008-10-02 12:24:47 -0700741static int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700742i915_gem_gtt_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
743 struct drm_i915_gem_pwrite *args,
744 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700745{
Daniel Vetter23010e42010-03-08 13:35:02 +0100746 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700747 drm_i915_private_t *dev_priv = dev->dev_private;
748 ssize_t remain;
749 loff_t gtt_page_base, offset;
750 loff_t first_data_page, last_data_page, num_pages;
751 loff_t pinned_pages, i;
752 struct page **user_pages;
753 struct mm_struct *mm = current->mm;
754 int gtt_page_offset, data_page_offset, data_page_index, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700755 int ret;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700756 uint64_t data_ptr = args->data_ptr;
757
758 remain = args->size;
759
760 /* Pin the user pages containing the data. We can't fault while
761 * holding the struct mutex, and all of the pwrite implementations
762 * want to hold it while dereferencing the user data.
763 */
764 first_data_page = data_ptr / PAGE_SIZE;
765 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
766 num_pages = last_data_page - first_data_page + 1;
767
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100768 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
Eric Anholt3de09aa2009-03-09 09:42:23 -0700769 if (user_pages == NULL)
770 return -ENOMEM;
771
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100772 mutex_unlock(&dev->struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700773 down_read(&mm->mmap_sem);
774 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
775 num_pages, 0, 0, user_pages, NULL);
776 up_read(&mm->mmap_sem);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100777 mutex_lock(&dev->struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700778 if (pinned_pages < num_pages) {
779 ret = -EFAULT;
780 goto out_unpin_pages;
781 }
782
Eric Anholt3de09aa2009-03-09 09:42:23 -0700783 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
784 if (ret)
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100785 goto out_unpin_pages;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700786
Daniel Vetter23010e42010-03-08 13:35:02 +0100787 obj_priv = to_intel_bo(obj);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700788 offset = obj_priv->gtt_offset + args->offset;
789
790 while (remain > 0) {
791 /* Operation in this page
792 *
793 * gtt_page_base = page offset within aperture
794 * gtt_page_offset = offset within page in aperture
795 * data_page_index = page number in get_user_pages return
796 * data_page_offset = offset with data_page_index page.
797 * page_length = bytes to copy for this page
798 */
799 gtt_page_base = offset & PAGE_MASK;
800 gtt_page_offset = offset & ~PAGE_MASK;
801 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
802 data_page_offset = data_ptr & ~PAGE_MASK;
803
804 page_length = remain;
805 if ((gtt_page_offset + page_length) > PAGE_SIZE)
806 page_length = PAGE_SIZE - gtt_page_offset;
807 if ((data_page_offset + page_length) > PAGE_SIZE)
808 page_length = PAGE_SIZE - data_page_offset;
809
Chris Wilsonab34c222010-05-27 14:15:35 +0100810 slow_kernel_write(dev_priv->mm.gtt_mapping,
811 gtt_page_base, gtt_page_offset,
812 user_pages[data_page_index],
813 data_page_offset,
814 page_length);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700815
816 remain -= page_length;
817 offset += page_length;
818 data_ptr += page_length;
819 }
820
Eric Anholt3de09aa2009-03-09 09:42:23 -0700821out_unpin_pages:
822 for (i = 0; i < pinned_pages; i++)
823 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700824 drm_free_large(user_pages);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700825
826 return ret;
827}
828
Eric Anholt40123c12009-03-09 13:42:30 -0700829/**
830 * This is the fast shmem pwrite path, which attempts to directly
831 * copy_from_user into the kmapped pages backing the object.
832 */
Eric Anholt673a3942008-07-30 12:06:12 -0700833static int
Eric Anholt40123c12009-03-09 13:42:30 -0700834i915_gem_shmem_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
835 struct drm_i915_gem_pwrite *args,
836 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700837{
Chris Wilsone5281cc2010-10-28 13:45:36 +0100838 struct address_space *mapping = obj->filp->f_path.dentry->d_inode->i_mapping;
Daniel Vetter23010e42010-03-08 13:35:02 +0100839 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700840 ssize_t remain;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100841 loff_t offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700842 char __user *user_data;
843 int page_offset, page_length;
Eric Anholt40123c12009-03-09 13:42:30 -0700844
845 user_data = (char __user *) (uintptr_t) args->data_ptr;
846 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700847
Daniel Vetter23010e42010-03-08 13:35:02 +0100848 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700849 offset = args->offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700850 obj_priv->dirty = 1;
Eric Anholt673a3942008-07-30 12:06:12 -0700851
Eric Anholt40123c12009-03-09 13:42:30 -0700852 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100853 struct page *page;
854 char *vaddr;
855 int ret;
856
Eric Anholt40123c12009-03-09 13:42:30 -0700857 /* Operation in this page
858 *
Eric Anholt40123c12009-03-09 13:42:30 -0700859 * page_offset = offset within page
860 * page_length = bytes to copy for this page
861 */
Eric Anholt40123c12009-03-09 13:42:30 -0700862 page_offset = offset & (PAGE_SIZE-1);
863 page_length = remain;
864 if ((page_offset + remain) > PAGE_SIZE)
865 page_length = PAGE_SIZE - page_offset;
866
Chris Wilsone5281cc2010-10-28 13:45:36 +0100867 page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
868 GFP_HIGHUSER | __GFP_RECLAIMABLE);
869 if (IS_ERR(page))
870 return PTR_ERR(page);
871
872 vaddr = kmap_atomic(page, KM_USER0);
873 ret = __copy_from_user_inatomic(vaddr + page_offset,
874 user_data,
875 page_length);
876 kunmap_atomic(vaddr, KM_USER0);
877
878 set_page_dirty(page);
879 mark_page_accessed(page);
880 page_cache_release(page);
881
882 /* If we get a fault while copying data, then (presumably) our
883 * source page isn't available. Return the error and we'll
884 * retry in the slow path.
885 */
886 if (ret)
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100887 return -EFAULT;
Eric Anholt40123c12009-03-09 13:42:30 -0700888
889 remain -= page_length;
890 user_data += page_length;
891 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700892 }
893
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100894 return 0;
Eric Anholt40123c12009-03-09 13:42:30 -0700895}
896
897/**
898 * This is the fallback shmem pwrite path, which uses get_user_pages to pin
899 * the memory and maps it using kmap_atomic for copying.
900 *
901 * This avoids taking mmap_sem for faulting on the user's address while the
902 * struct_mutex is held.
903 */
904static int
905i915_gem_shmem_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
906 struct drm_i915_gem_pwrite *args,
907 struct drm_file *file_priv)
908{
Chris Wilsone5281cc2010-10-28 13:45:36 +0100909 struct address_space *mapping = obj->filp->f_path.dentry->d_inode->i_mapping;
Daniel Vetter23010e42010-03-08 13:35:02 +0100910 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700911 struct mm_struct *mm = current->mm;
912 struct page **user_pages;
913 ssize_t remain;
914 loff_t offset, pinned_pages, i;
915 loff_t first_data_page, last_data_page, num_pages;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100916 int shmem_page_offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700917 int data_page_index, data_page_offset;
918 int page_length;
919 int ret;
920 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700921 int do_bit17_swizzling;
Eric Anholt40123c12009-03-09 13:42:30 -0700922
923 remain = args->size;
924
925 /* Pin the user pages containing the data. We can't fault while
926 * holding the struct mutex, and all of the pwrite implementations
927 * want to hold it while dereferencing the user data.
928 */
929 first_data_page = data_ptr / PAGE_SIZE;
930 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
931 num_pages = last_data_page - first_data_page + 1;
932
Chris Wilson4f27b752010-10-14 15:26:45 +0100933 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
Eric Anholt40123c12009-03-09 13:42:30 -0700934 if (user_pages == NULL)
935 return -ENOMEM;
936
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100937 mutex_unlock(&dev->struct_mutex);
Eric Anholt40123c12009-03-09 13:42:30 -0700938 down_read(&mm->mmap_sem);
939 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
940 num_pages, 0, 0, user_pages, NULL);
941 up_read(&mm->mmap_sem);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100942 mutex_lock(&dev->struct_mutex);
Eric Anholt40123c12009-03-09 13:42:30 -0700943 if (pinned_pages < num_pages) {
944 ret = -EFAULT;
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100945 goto out;
Eric Anholt40123c12009-03-09 13:42:30 -0700946 }
947
Eric Anholt40123c12009-03-09 13:42:30 -0700948 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100949 if (ret)
950 goto out;
951
952 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700953
Daniel Vetter23010e42010-03-08 13:35:02 +0100954 obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700955 offset = args->offset;
956 obj_priv->dirty = 1;
957
958 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100959 struct page *page;
960
Eric Anholt40123c12009-03-09 13:42:30 -0700961 /* Operation in this page
962 *
Eric Anholt40123c12009-03-09 13:42:30 -0700963 * shmem_page_offset = offset within page in shmem file
964 * data_page_index = page number in get_user_pages return
965 * data_page_offset = offset with data_page_index page.
966 * page_length = bytes to copy for this page
967 */
Eric Anholt40123c12009-03-09 13:42:30 -0700968 shmem_page_offset = offset & ~PAGE_MASK;
969 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
970 data_page_offset = data_ptr & ~PAGE_MASK;
971
972 page_length = remain;
973 if ((shmem_page_offset + page_length) > PAGE_SIZE)
974 page_length = PAGE_SIZE - shmem_page_offset;
975 if ((data_page_offset + page_length) > PAGE_SIZE)
976 page_length = PAGE_SIZE - data_page_offset;
977
Chris Wilsone5281cc2010-10-28 13:45:36 +0100978 page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
979 GFP_HIGHUSER | __GFP_RECLAIMABLE);
980 if (IS_ERR(page)) {
981 ret = PTR_ERR(page);
982 goto out;
983 }
984
Eric Anholt280b7132009-03-12 16:56:27 -0700985 if (do_bit17_swizzling) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100986 slow_shmem_bit17_copy(page,
Eric Anholt280b7132009-03-12 16:56:27 -0700987 shmem_page_offset,
988 user_pages[data_page_index],
989 data_page_offset,
Chris Wilson99a03df2010-05-27 14:15:34 +0100990 page_length,
991 0);
992 } else {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100993 slow_shmem_copy(page,
Chris Wilson99a03df2010-05-27 14:15:34 +0100994 shmem_page_offset,
995 user_pages[data_page_index],
996 data_page_offset,
997 page_length);
Eric Anholt280b7132009-03-12 16:56:27 -0700998 }
Eric Anholt40123c12009-03-09 13:42:30 -0700999
Chris Wilsone5281cc2010-10-28 13:45:36 +01001000 set_page_dirty(page);
1001 mark_page_accessed(page);
1002 page_cache_release(page);
1003
Eric Anholt40123c12009-03-09 13:42:30 -07001004 remain -= page_length;
1005 data_ptr += page_length;
1006 offset += page_length;
1007 }
1008
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001009out:
Eric Anholt40123c12009-03-09 13:42:30 -07001010 for (i = 0; i < pinned_pages; i++)
1011 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07001012 drm_free_large(user_pages);
Eric Anholt40123c12009-03-09 13:42:30 -07001013
1014 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001015}
1016
1017/**
1018 * Writes data to the object referenced by handle.
1019 *
1020 * On error, the contents of the buffer that were to be modified are undefined.
1021 */
1022int
1023i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001024 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001025{
1026 struct drm_i915_gem_pwrite *args = data;
1027 struct drm_gem_object *obj;
1028 struct drm_i915_gem_object *obj_priv;
1029 int ret = 0;
1030
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001031 ret = i915_mutex_lock_interruptible(dev);
1032 if (ret)
1033 return ret;
1034
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001035 obj = drm_gem_object_lookup(dev, file, args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001036 if (obj == NULL) {
1037 ret = -ENOENT;
1038 goto unlock;
1039 }
Daniel Vetter23010e42010-03-08 13:35:02 +01001040 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001041
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001042
Chris Wilson7dcd2492010-09-26 20:21:44 +01001043 /* Bounds check destination. */
1044 if (args->offset > obj->size || args->size > obj->size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001045 ret = -EINVAL;
Chris Wilson35b62a82010-09-26 20:23:38 +01001046 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001047 }
1048
Chris Wilson35b62a82010-09-26 20:23:38 +01001049 if (args->size == 0)
1050 goto out;
1051
Chris Wilsonce9d4192010-09-26 20:50:05 +01001052 if (!access_ok(VERIFY_READ,
1053 (char __user *)(uintptr_t)args->data_ptr,
1054 args->size)) {
1055 ret = -EFAULT;
Chris Wilson35b62a82010-09-26 20:23:38 +01001056 goto out;
Eric Anholt673a3942008-07-30 12:06:12 -07001057 }
1058
Chris Wilsonb5e4feb2010-10-14 13:47:43 +01001059 ret = fault_in_pages_readable((char __user *)(uintptr_t)args->data_ptr,
1060 args->size);
1061 if (ret) {
1062 ret = -EFAULT;
1063 goto out;
Eric Anholt673a3942008-07-30 12:06:12 -07001064 }
1065
1066 /* We can only do the GTT pwrite on untiled buffers, as otherwise
1067 * it would end up going through the fenced access, and we'll get
1068 * different detiling behavior between reading and writing.
1069 * pread/pwrite currently are reading and writing from the CPU
1070 * perspective, requiring manual detiling by the client.
1071 */
Dave Airlie71acb5e2008-12-30 20:31:46 +10001072 if (obj_priv->phys_obj)
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001073 ret = i915_gem_phys_pwrite(dev, obj, args, file);
Dave Airlie71acb5e2008-12-30 20:31:46 +10001074 else if (obj_priv->tiling_mode == I915_TILING_NONE &&
Chris Wilson5cdf5882010-09-27 15:51:07 +01001075 obj_priv->gtt_space &&
Chris Wilson9b8c4a02010-05-27 14:21:01 +01001076 obj->write_domain != I915_GEM_DOMAIN_CPU) {
Chris Wilsona00b10c2010-09-24 21:15:47 +01001077 ret = i915_gem_object_pin(obj, 0, true, false);
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001078 if (ret)
1079 goto out;
1080
1081 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
1082 if (ret)
1083 goto out_unpin;
1084
1085 ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file);
1086 if (ret == -EFAULT)
1087 ret = i915_gem_gtt_pwrite_slow(dev, obj, args, file);
1088
1089out_unpin:
1090 i915_gem_object_unpin(obj);
Eric Anholt40123c12009-03-09 13:42:30 -07001091 } else {
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001092 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
1093 if (ret)
Chris Wilsone5281cc2010-10-28 13:45:36 +01001094 goto out;
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001095
1096 ret = -EFAULT;
1097 if (!i915_gem_object_needs_bit17_swizzle(obj))
1098 ret = i915_gem_shmem_pwrite_fast(dev, obj, args, file);
1099 if (ret == -EFAULT)
1100 ret = i915_gem_shmem_pwrite_slow(dev, obj, args, file);
Eric Anholt40123c12009-03-09 13:42:30 -07001101 }
Eric Anholt673a3942008-07-30 12:06:12 -07001102
Chris Wilson35b62a82010-09-26 20:23:38 +01001103out:
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001104 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001105unlock:
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001106 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07001107 return ret;
1108}
1109
1110/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001111 * Called when user space prepares to use an object with the CPU, either
1112 * through the mmap ioctl's mapping or a GTT mapping.
Eric Anholt673a3942008-07-30 12:06:12 -07001113 */
1114int
1115i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
1116 struct drm_file *file_priv)
1117{
Eric Anholta09ba7f2009-08-29 12:49:51 -07001118 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07001119 struct drm_i915_gem_set_domain *args = data;
1120 struct drm_gem_object *obj;
Jesse Barnes652c3932009-08-17 13:31:43 -07001121 struct drm_i915_gem_object *obj_priv;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001122 uint32_t read_domains = args->read_domains;
1123 uint32_t write_domain = args->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07001124 int ret;
1125
1126 if (!(dev->driver->driver_features & DRIVER_GEM))
1127 return -ENODEV;
1128
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001129 /* Only handle setting domains to types used by the CPU. */
Chris Wilson21d509e2009-06-06 09:46:02 +01001130 if (write_domain & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001131 return -EINVAL;
1132
Chris Wilson21d509e2009-06-06 09:46:02 +01001133 if (read_domains & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001134 return -EINVAL;
1135
1136 /* Having something in the write domain implies it's in the read
1137 * domain, and only that read domain. Enforce that in the request.
1138 */
1139 if (write_domain != 0 && read_domains != write_domain)
1140 return -EINVAL;
1141
Chris Wilson76c1dec2010-09-25 11:22:51 +01001142 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001143 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001144 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001145
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001146 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1147 if (obj == NULL) {
1148 ret = -ENOENT;
1149 goto unlock;
Chris Wilson76c1dec2010-09-25 11:22:51 +01001150 }
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001151 obj_priv = to_intel_bo(obj);
Jesse Barnes652c3932009-08-17 13:31:43 -07001152
1153 intel_mark_busy(dev, obj);
1154
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001155 if (read_domains & I915_GEM_DOMAIN_GTT) {
1156 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
Eric Anholt02354392008-11-26 13:58:13 -08001157
Eric Anholta09ba7f2009-08-29 12:49:51 -07001158 /* Update the LRU on the fence for the CPU access that's
1159 * about to occur.
1160 */
1161 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001162 struct drm_i915_fence_reg *reg =
1163 &dev_priv->fence_regs[obj_priv->fence_reg];
1164 list_move_tail(&reg->lru_list,
Eric Anholta09ba7f2009-08-29 12:49:51 -07001165 &dev_priv->mm.fence_list);
1166 }
1167
Eric Anholt02354392008-11-26 13:58:13 -08001168 /* Silently promote "you're not bound, there was nothing to do"
1169 * to success, since the client was just asking us to
1170 * make sure everything was done.
1171 */
1172 if (ret == -EINVAL)
1173 ret = 0;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001174 } else {
Eric Anholte47c68e2008-11-14 13:35:19 -08001175 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001176 }
1177
Chris Wilson7d1c4802010-08-07 21:45:03 +01001178 /* Maintain LRU order of "inactive" objects */
1179 if (ret == 0 && i915_gem_object_is_inactive(obj_priv))
Chris Wilson69dc4982010-10-19 10:36:51 +01001180 list_move_tail(&obj_priv->mm_list, &dev_priv->mm.inactive_list);
Chris Wilson7d1c4802010-08-07 21:45:03 +01001181
Eric Anholt673a3942008-07-30 12:06:12 -07001182 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001183unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07001184 mutex_unlock(&dev->struct_mutex);
1185 return ret;
1186}
1187
1188/**
1189 * Called when user space has done writes to this buffer
1190 */
1191int
1192i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
1193 struct drm_file *file_priv)
1194{
1195 struct drm_i915_gem_sw_finish *args = data;
1196 struct drm_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001197 int ret = 0;
1198
1199 if (!(dev->driver->driver_features & DRIVER_GEM))
1200 return -ENODEV;
1201
Chris Wilson76c1dec2010-09-25 11:22:51 +01001202 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001203 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001204 return ret;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001205
Eric Anholt673a3942008-07-30 12:06:12 -07001206 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1207 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001208 ret = -ENOENT;
1209 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07001210 }
1211
Eric Anholt673a3942008-07-30 12:06:12 -07001212 /* Pinned buffers may be scanout, so flush the cache */
Chris Wilson3d2a8122010-09-29 11:39:53 +01001213 if (to_intel_bo(obj)->pin_count)
Eric Anholte47c68e2008-11-14 13:35:19 -08001214 i915_gem_object_flush_cpu_write_domain(obj);
1215
Eric Anholt673a3942008-07-30 12:06:12 -07001216 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001217unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07001218 mutex_unlock(&dev->struct_mutex);
1219 return ret;
1220}
1221
1222/**
1223 * Maps the contents of an object, returning the address it is mapped
1224 * into.
1225 *
1226 * While the mapping holds a reference on the contents of the object, it doesn't
1227 * imply a ref on the object itself.
1228 */
1229int
1230i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
1231 struct drm_file *file_priv)
1232{
Chris Wilsonda761a62010-10-27 17:37:08 +01001233 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07001234 struct drm_i915_gem_mmap *args = data;
1235 struct drm_gem_object *obj;
1236 loff_t offset;
1237 unsigned long addr;
1238
1239 if (!(dev->driver->driver_features & DRIVER_GEM))
1240 return -ENODEV;
1241
1242 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1243 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001244 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001245
Chris Wilsonda761a62010-10-27 17:37:08 +01001246 if (obj->size > dev_priv->mm.gtt_mappable_end) {
1247 drm_gem_object_unreference_unlocked(obj);
1248 return -E2BIG;
1249 }
1250
Eric Anholt673a3942008-07-30 12:06:12 -07001251 offset = args->offset;
1252
1253 down_write(&current->mm->mmap_sem);
1254 addr = do_mmap(obj->filp, 0, args->size,
1255 PROT_READ | PROT_WRITE, MAP_SHARED,
1256 args->offset);
1257 up_write(&current->mm->mmap_sem);
Luca Barbieribc9025b2010-02-09 05:49:12 +00001258 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001259 if (IS_ERR((void *)addr))
1260 return addr;
1261
1262 args->addr_ptr = (uint64_t) addr;
1263
1264 return 0;
1265}
1266
Jesse Barnesde151cf2008-11-12 10:03:55 -08001267/**
1268 * i915_gem_fault - fault a page into the GTT
1269 * vma: VMA in question
1270 * vmf: fault info
1271 *
1272 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1273 * from userspace. The fault handler takes care of binding the object to
1274 * the GTT (if needed), allocating and programming a fence register (again,
1275 * only if needed based on whether the old reg is still valid or the object
1276 * is tiled) and inserting a new PTE into the faulting process.
1277 *
1278 * Note that the faulting process may involve evicting existing objects
1279 * from the GTT and/or fence registers to make room. So performance may
1280 * suffer if the GTT working set is large or there are few fence registers
1281 * left.
1282 */
1283int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1284{
1285 struct drm_gem_object *obj = vma->vm_private_data;
1286 struct drm_device *dev = obj->dev;
Chris Wilson7d1c4802010-08-07 21:45:03 +01001287 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001288 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001289 pgoff_t page_offset;
1290 unsigned long pfn;
1291 int ret = 0;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001292 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001293
1294 /* We don't use vmf->pgoff since that has the fake offset */
1295 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1296 PAGE_SHIFT;
1297
1298 /* Now bind it into the GTT if needed */
1299 mutex_lock(&dev->struct_mutex);
Daniel Vetterfb7d5162010-10-01 22:05:20 +02001300 BUG_ON(obj_priv->pin_count && !obj_priv->pin_mappable);
Chris Wilsona00b10c2010-09-24 21:15:47 +01001301
1302 if (obj_priv->gtt_space) {
1303 if (!obj_priv->mappable ||
1304 (obj_priv->tiling_mode && !obj_priv->fenceable)) {
1305 ret = i915_gem_object_unbind(obj);
1306 if (ret)
1307 goto unlock;
1308 }
1309 }
Daniel Vetter16e809a2010-09-16 19:37:04 +02001310
Jesse Barnesde151cf2008-11-12 10:03:55 -08001311 if (!obj_priv->gtt_space) {
Chris Wilsona00b10c2010-09-24 21:15:47 +01001312 ret = i915_gem_object_bind_to_gtt(obj, 0,
1313 true, obj_priv->tiling_mode);
Chris Wilsonc7150892009-09-23 00:43:56 +01001314 if (ret)
1315 goto unlock;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001316 }
1317
Chris Wilson4a684a42010-10-28 14:44:08 +01001318 ret = i915_gem_object_set_to_gtt_domain(obj, write);
1319 if (ret)
1320 goto unlock;
1321
Daniel Vetterfb7d5162010-10-01 22:05:20 +02001322 if (!obj_priv->fault_mappable) {
1323 obj_priv->fault_mappable = true;
Chris Wilsona00b10c2010-09-24 21:15:47 +01001324 i915_gem_info_update_mappable(dev_priv, obj_priv, true);
Daniel Vetterfb7d5162010-10-01 22:05:20 +02001325 }
1326
Jesse Barnesde151cf2008-11-12 10:03:55 -08001327 /* Need a new fence register? */
Eric Anholta09ba7f2009-08-29 12:49:51 -07001328 if (obj_priv->tiling_mode != I915_TILING_NONE) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01001329 ret = i915_gem_object_get_fence_reg(obj, true);
Chris Wilsonc7150892009-09-23 00:43:56 +01001330 if (ret)
1331 goto unlock;
Eric Anholtd9ddcb92009-01-27 10:33:49 -08001332 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001333
Chris Wilson7d1c4802010-08-07 21:45:03 +01001334 if (i915_gem_object_is_inactive(obj_priv))
Chris Wilson69dc4982010-10-19 10:36:51 +01001335 list_move_tail(&obj_priv->mm_list, &dev_priv->mm.inactive_list);
Chris Wilson7d1c4802010-08-07 21:45:03 +01001336
Jesse Barnesde151cf2008-11-12 10:03:55 -08001337 pfn = ((dev->agp->base + obj_priv->gtt_offset) >> PAGE_SHIFT) +
1338 page_offset;
1339
1340 /* Finally, remap it using the new GTT offset */
1341 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
Chris Wilsonc7150892009-09-23 00:43:56 +01001342unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001343 mutex_unlock(&dev->struct_mutex);
1344
1345 switch (ret) {
Chris Wilsonc7150892009-09-23 00:43:56 +01001346 case 0:
1347 case -ERESTARTSYS:
1348 return VM_FAULT_NOPAGE;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001349 case -ENOMEM:
1350 case -EAGAIN:
1351 return VM_FAULT_OOM;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001352 default:
Chris Wilsonc7150892009-09-23 00:43:56 +01001353 return VM_FAULT_SIGBUS;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001354 }
1355}
1356
1357/**
1358 * i915_gem_create_mmap_offset - create a fake mmap offset for an object
1359 * @obj: obj in question
1360 *
1361 * GEM memory mapping works by handing back to userspace a fake mmap offset
1362 * it can use in a subsequent mmap(2) call. The DRM core code then looks
1363 * up the object based on the offset and sets up the various memory mapping
1364 * structures.
1365 *
1366 * This routine allocates and attaches a fake offset for @obj.
1367 */
1368static int
1369i915_gem_create_mmap_offset(struct drm_gem_object *obj)
1370{
1371 struct drm_device *dev = obj->dev;
1372 struct drm_gem_mm *mm = dev->mm_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001373 struct drm_map_list *list;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +11001374 struct drm_local_map *map;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001375 int ret = 0;
1376
1377 /* Set the object up for mmap'ing */
1378 list = &obj->map_list;
Eric Anholt9a298b22009-03-24 12:23:04 -07001379 list->map = kzalloc(sizeof(struct drm_map_list), GFP_KERNEL);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001380 if (!list->map)
1381 return -ENOMEM;
1382
1383 map = list->map;
1384 map->type = _DRM_GEM;
1385 map->size = obj->size;
1386 map->handle = obj;
1387
1388 /* Get a DRM GEM mmap offset allocated... */
1389 list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
1390 obj->size / PAGE_SIZE, 0, 0);
1391 if (!list->file_offset_node) {
1392 DRM_ERROR("failed to allocate offset for bo %d\n", obj->name);
Chris Wilson9e0ae5342010-09-21 15:05:24 +01001393 ret = -ENOSPC;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001394 goto out_free_list;
1395 }
1396
1397 list->file_offset_node = drm_mm_get_block(list->file_offset_node,
1398 obj->size / PAGE_SIZE, 0);
1399 if (!list->file_offset_node) {
1400 ret = -ENOMEM;
1401 goto out_free_list;
1402 }
1403
1404 list->hash.key = list->file_offset_node->start;
Chris Wilson9e0ae5342010-09-21 15:05:24 +01001405 ret = drm_ht_insert_item(&mm->offset_hash, &list->hash);
1406 if (ret) {
Jesse Barnesde151cf2008-11-12 10:03:55 -08001407 DRM_ERROR("failed to add to map hash\n");
1408 goto out_free_mm;
1409 }
1410
Jesse Barnesde151cf2008-11-12 10:03:55 -08001411 return 0;
1412
1413out_free_mm:
1414 drm_mm_put_block(list->file_offset_node);
1415out_free_list:
Eric Anholt9a298b22009-03-24 12:23:04 -07001416 kfree(list->map);
Chris Wilson39a01d12010-10-28 13:03:06 +01001417 list->map = NULL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001418
1419 return ret;
1420}
1421
Chris Wilson901782b2009-07-10 08:18:50 +01001422/**
1423 * i915_gem_release_mmap - remove physical page mappings
1424 * @obj: obj in question
1425 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001426 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01001427 * relinquish ownership of the pages back to the system.
1428 *
1429 * It is vital that we remove the page mapping if we have mapped a tiled
1430 * object through the GTT and then lose the fence register due to
1431 * resource pressure. Similarly if the object has been moved out of the
1432 * aperture, than pages mapped into userspace must be revoked. Removing the
1433 * mapping will then trigger a page fault on the next user access, allowing
1434 * fixup by i915_gem_fault().
1435 */
Eric Anholtd05ca302009-07-10 13:02:26 -07001436void
Chris Wilson901782b2009-07-10 08:18:50 +01001437i915_gem_release_mmap(struct drm_gem_object *obj)
1438{
1439 struct drm_device *dev = obj->dev;
Daniel Vetterfb7d5162010-10-01 22:05:20 +02001440 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001441 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson901782b2009-07-10 08:18:50 +01001442
Chris Wilson39a01d12010-10-28 13:03:06 +01001443 if (unlikely(obj->map_list.map && dev->dev_mapping))
Chris Wilson901782b2009-07-10 08:18:50 +01001444 unmap_mapping_range(dev->dev_mapping,
Chris Wilson39a01d12010-10-28 13:03:06 +01001445 (loff_t)obj->map_list.hash.key<<PAGE_SHIFT,
1446 obj->size, 1);
Daniel Vetterfb7d5162010-10-01 22:05:20 +02001447
1448 if (obj_priv->fault_mappable) {
1449 obj_priv->fault_mappable = false;
Chris Wilsona00b10c2010-09-24 21:15:47 +01001450 i915_gem_info_update_mappable(dev_priv, obj_priv, false);
Daniel Vetterfb7d5162010-10-01 22:05:20 +02001451 }
Chris Wilson901782b2009-07-10 08:18:50 +01001452}
1453
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001454static void
1455i915_gem_free_mmap_offset(struct drm_gem_object *obj)
1456{
1457 struct drm_device *dev = obj->dev;
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001458 struct drm_gem_mm *mm = dev->mm_private;
Chris Wilson39a01d12010-10-28 13:03:06 +01001459 struct drm_map_list *list = &obj->map_list;
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001460
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001461 drm_ht_remove_item(&mm->offset_hash, &list->hash);
Chris Wilson39a01d12010-10-28 13:03:06 +01001462 drm_mm_put_block(list->file_offset_node);
1463 kfree(list->map);
1464 list->map = NULL;
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001465}
1466
Jesse Barnesde151cf2008-11-12 10:03:55 -08001467/**
1468 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1469 * @obj: object to check
1470 *
1471 * Return the required GTT alignment for an object, taking into account
1472 * potential fence register mapping if needed.
1473 */
1474static uint32_t
Chris Wilsona00b10c2010-09-24 21:15:47 +01001475i915_gem_get_gtt_alignment(struct drm_i915_gem_object *obj_priv)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001476{
Chris Wilsona00b10c2010-09-24 21:15:47 +01001477 struct drm_device *dev = obj_priv->base.dev;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001478
1479 /*
1480 * Minimum alignment is 4k (GTT page size), but might be greater
1481 * if a fence register is needed for the object.
1482 */
Chris Wilsona00b10c2010-09-24 21:15:47 +01001483 if (INTEL_INFO(dev)->gen >= 4 ||
1484 obj_priv->tiling_mode == I915_TILING_NONE)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001485 return 4096;
1486
1487 /*
1488 * Previous chips need to be aligned to the size of the smallest
1489 * fence register that can contain the object.
1490 */
Chris Wilsona00b10c2010-09-24 21:15:47 +01001491 return i915_gem_get_gtt_size(obj_priv);
1492}
1493
1494static uint32_t
1495i915_gem_get_gtt_size(struct drm_i915_gem_object *obj_priv)
1496{
1497 struct drm_device *dev = obj_priv->base.dev;
1498 uint32_t size;
1499
1500 /*
1501 * Minimum alignment is 4k (GTT page size), but might be greater
1502 * if a fence register is needed for the object.
1503 */
1504 if (INTEL_INFO(dev)->gen >= 4)
1505 return obj_priv->base.size;
1506
1507 /*
1508 * Previous chips need to be aligned to the size of the smallest
1509 * fence register that can contain the object.
1510 */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001511 if (INTEL_INFO(dev)->gen == 3)
Chris Wilsona00b10c2010-09-24 21:15:47 +01001512 size = 1024*1024;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001513 else
Chris Wilsona00b10c2010-09-24 21:15:47 +01001514 size = 512*1024;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001515
Chris Wilsona00b10c2010-09-24 21:15:47 +01001516 while (size < obj_priv->base.size)
1517 size <<= 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001518
Chris Wilsona00b10c2010-09-24 21:15:47 +01001519 return size;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001520}
1521
1522/**
1523 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1524 * @dev: DRM device
1525 * @data: GTT mapping ioctl data
1526 * @file_priv: GEM object info
1527 *
1528 * Simply returns the fake offset to userspace so it can mmap it.
1529 * The mmap call will end up in drm_gem_mmap(), which will set things
1530 * up so we can get faults in the handler above.
1531 *
1532 * The fault handler will take care of binding the object into the GTT
1533 * (since it may have been evicted to make room for something), allocating
1534 * a fence register, and mapping the appropriate aperture address into
1535 * userspace.
1536 */
1537int
1538i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
1539 struct drm_file *file_priv)
1540{
Chris Wilsonda761a62010-10-27 17:37:08 +01001541 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001542 struct drm_i915_gem_mmap_gtt *args = data;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001543 struct drm_gem_object *obj;
1544 struct drm_i915_gem_object *obj_priv;
1545 int ret;
1546
1547 if (!(dev->driver->driver_features & DRIVER_GEM))
1548 return -ENODEV;
1549
Chris Wilson76c1dec2010-09-25 11:22:51 +01001550 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001551 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001552 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001553
Jesse Barnesde151cf2008-11-12 10:03:55 -08001554 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001555 if (obj == NULL) {
1556 ret = -ENOENT;
1557 goto unlock;
1558 }
Daniel Vetter23010e42010-03-08 13:35:02 +01001559 obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001560
Chris Wilsonda761a62010-10-27 17:37:08 +01001561 if (obj->size > dev_priv->mm.gtt_mappable_end) {
1562 ret = -E2BIG;
1563 goto unlock;
1564 }
1565
Chris Wilsonab182822009-09-22 18:46:17 +01001566 if (obj_priv->madv != I915_MADV_WILLNEED) {
1567 DRM_ERROR("Attempting to mmap a purgeable buffer\n");
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001568 ret = -EINVAL;
1569 goto out;
Chris Wilsonab182822009-09-22 18:46:17 +01001570 }
1571
Chris Wilson39a01d12010-10-28 13:03:06 +01001572 if (!obj->map_list.map) {
Jesse Barnesde151cf2008-11-12 10:03:55 -08001573 ret = i915_gem_create_mmap_offset(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001574 if (ret)
1575 goto out;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001576 }
1577
Chris Wilson39a01d12010-10-28 13:03:06 +01001578 args->offset = (u64)obj->map_list.hash.key << PAGE_SHIFT;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001579
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001580out:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001581 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001582unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001583 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001584 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001585}
1586
Chris Wilsone5281cc2010-10-28 13:45:36 +01001587static int
1588i915_gem_object_get_pages_gtt(struct drm_gem_object *obj,
1589 gfp_t gfpmask)
1590{
1591 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
1592 int page_count, i;
1593 struct address_space *mapping;
1594 struct inode *inode;
1595 struct page *page;
1596
1597 /* Get the list of pages out of our struct file. They'll be pinned
1598 * at this point until we release them.
1599 */
1600 page_count = obj->size / PAGE_SIZE;
1601 BUG_ON(obj_priv->pages != NULL);
1602 obj_priv->pages = drm_malloc_ab(page_count, sizeof(struct page *));
1603 if (obj_priv->pages == NULL)
1604 return -ENOMEM;
1605
1606 inode = obj->filp->f_path.dentry->d_inode;
1607 mapping = inode->i_mapping;
1608 for (i = 0; i < page_count; i++) {
1609 page = read_cache_page_gfp(mapping, i,
1610 GFP_HIGHUSER |
1611 __GFP_COLD |
1612 __GFP_RECLAIMABLE |
1613 gfpmask);
1614 if (IS_ERR(page))
1615 goto err_pages;
1616
1617 obj_priv->pages[i] = page;
1618 }
1619
1620 if (obj_priv->tiling_mode != I915_TILING_NONE)
1621 i915_gem_object_do_bit_17_swizzle(obj);
1622
1623 return 0;
1624
1625err_pages:
1626 while (i--)
1627 page_cache_release(obj_priv->pages[i]);
1628
1629 drm_free_large(obj_priv->pages);
1630 obj_priv->pages = NULL;
1631 return PTR_ERR(page);
1632}
1633
Chris Wilson5cdf5882010-09-27 15:51:07 +01001634static void
Chris Wilsone5281cc2010-10-28 13:45:36 +01001635i915_gem_object_put_pages_gtt(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07001636{
Daniel Vetter23010e42010-03-08 13:35:02 +01001637 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001638 int page_count = obj->size / PAGE_SIZE;
1639 int i;
1640
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001641 BUG_ON(obj_priv->madv == __I915_MADV_PURGED);
Eric Anholt856fa192009-03-19 14:10:50 -07001642
Eric Anholt280b7132009-03-12 16:56:27 -07001643 if (obj_priv->tiling_mode != I915_TILING_NONE)
1644 i915_gem_object_save_bit_17_swizzle(obj);
1645
Chris Wilson3ef94da2009-09-14 16:50:29 +01001646 if (obj_priv->madv == I915_MADV_DONTNEED)
Chris Wilson13a05fd2009-09-20 23:03:19 +01001647 obj_priv->dirty = 0;
Chris Wilson3ef94da2009-09-14 16:50:29 +01001648
1649 for (i = 0; i < page_count; i++) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01001650 if (obj_priv->dirty)
1651 set_page_dirty(obj_priv->pages[i]);
1652
1653 if (obj_priv->madv == I915_MADV_WILLNEED)
Eric Anholt856fa192009-03-19 14:10:50 -07001654 mark_page_accessed(obj_priv->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001655
1656 page_cache_release(obj_priv->pages[i]);
1657 }
Eric Anholt673a3942008-07-30 12:06:12 -07001658 obj_priv->dirty = 0;
1659
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07001660 drm_free_large(obj_priv->pages);
Eric Anholt856fa192009-03-19 14:10:50 -07001661 obj_priv->pages = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001662}
1663
Chris Wilsona56ba562010-09-28 10:07:56 +01001664static uint32_t
1665i915_gem_next_request_seqno(struct drm_device *dev,
1666 struct intel_ring_buffer *ring)
1667{
1668 drm_i915_private_t *dev_priv = dev->dev_private;
1669
1670 ring->outstanding_lazy_request = true;
1671 return dev_priv->next_seqno;
1672}
1673
Eric Anholt673a3942008-07-30 12:06:12 -07001674static void
Daniel Vetter617dbe22010-02-11 22:16:02 +01001675i915_gem_object_move_to_active(struct drm_gem_object *obj,
Zou Nan hai852835f2010-05-21 09:08:56 +08001676 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001677{
1678 struct drm_device *dev = obj->dev;
Chris Wilson69dc4982010-10-19 10:36:51 +01001679 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001680 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsona56ba562010-09-28 10:07:56 +01001681 uint32_t seqno = i915_gem_next_request_seqno(dev, ring);
Daniel Vetter617dbe22010-02-11 22:16:02 +01001682
Zou Nan hai852835f2010-05-21 09:08:56 +08001683 BUG_ON(ring == NULL);
1684 obj_priv->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001685
1686 /* Add a reference if we're newly entering the active list. */
1687 if (!obj_priv->active) {
1688 drm_gem_object_reference(obj);
1689 obj_priv->active = 1;
1690 }
Daniel Vettere35a41d2010-02-11 22:13:59 +01001691
Eric Anholt673a3942008-07-30 12:06:12 -07001692 /* Move from whatever list we were on to the tail of execution. */
Chris Wilson69dc4982010-10-19 10:36:51 +01001693 list_move_tail(&obj_priv->mm_list, &dev_priv->mm.active_list);
1694 list_move_tail(&obj_priv->ring_list, &ring->active_list);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001695 obj_priv->last_rendering_seqno = seqno;
Eric Anholt673a3942008-07-30 12:06:12 -07001696}
1697
Eric Anholtce44b0e2008-11-06 16:00:31 -08001698static void
1699i915_gem_object_move_to_flushing(struct drm_gem_object *obj)
1700{
1701 struct drm_device *dev = obj->dev;
1702 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001703 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001704
1705 BUG_ON(!obj_priv->active);
Chris Wilson69dc4982010-10-19 10:36:51 +01001706 list_move_tail(&obj_priv->mm_list, &dev_priv->mm.flushing_list);
1707 list_del_init(&obj_priv->ring_list);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001708 obj_priv->last_rendering_seqno = 0;
1709}
Eric Anholt673a3942008-07-30 12:06:12 -07001710
Chris Wilson963b4832009-09-20 23:03:54 +01001711/* Immediately discard the backing storage */
1712static void
1713i915_gem_object_truncate(struct drm_gem_object *obj)
1714{
Daniel Vetter23010e42010-03-08 13:35:02 +01001715 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001716 struct inode *inode;
Chris Wilson963b4832009-09-20 23:03:54 +01001717
Chris Wilsonae9fed62010-08-07 11:01:30 +01001718 /* Our goal here is to return as much of the memory as
1719 * is possible back to the system as we are called from OOM.
1720 * To do this we must instruct the shmfs to drop all of its
1721 * backing pages, *now*. Here we mirror the actions taken
1722 * when by shmem_delete_inode() to release the backing store.
1723 */
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001724 inode = obj->filp->f_path.dentry->d_inode;
Chris Wilsonae9fed62010-08-07 11:01:30 +01001725 truncate_inode_pages(inode->i_mapping, 0);
1726 if (inode->i_op->truncate_range)
1727 inode->i_op->truncate_range(inode, 0, (loff_t)-1);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001728
1729 obj_priv->madv = __I915_MADV_PURGED;
Chris Wilson963b4832009-09-20 23:03:54 +01001730}
1731
1732static inline int
1733i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj_priv)
1734{
1735 return obj_priv->madv == I915_MADV_DONTNEED;
1736}
1737
Eric Anholt673a3942008-07-30 12:06:12 -07001738static void
1739i915_gem_object_move_to_inactive(struct drm_gem_object *obj)
1740{
1741 struct drm_device *dev = obj->dev;
1742 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001743 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001744
Eric Anholt673a3942008-07-30 12:06:12 -07001745 if (obj_priv->pin_count != 0)
Chris Wilson69dc4982010-10-19 10:36:51 +01001746 list_move_tail(&obj_priv->mm_list, &dev_priv->mm.pinned_list);
Eric Anholt673a3942008-07-30 12:06:12 -07001747 else
Chris Wilson69dc4982010-10-19 10:36:51 +01001748 list_move_tail(&obj_priv->mm_list, &dev_priv->mm.inactive_list);
1749 list_del_init(&obj_priv->ring_list);
Eric Anholt673a3942008-07-30 12:06:12 -07001750
Daniel Vetter99fcb762010-02-07 16:20:18 +01001751 BUG_ON(!list_empty(&obj_priv->gpu_write_list));
1752
Eric Anholtce44b0e2008-11-06 16:00:31 -08001753 obj_priv->last_rendering_seqno = 0;
Zou Nan hai852835f2010-05-21 09:08:56 +08001754 obj_priv->ring = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001755 if (obj_priv->active) {
1756 obj_priv->active = 0;
1757 drm_gem_object_unreference(obj);
1758 }
Chris Wilson23bc5982010-09-29 16:10:57 +01001759 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07001760}
1761
Daniel Vetter63560392010-02-19 11:51:59 +01001762static void
1763i915_gem_process_flushing_list(struct drm_device *dev,
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001764 uint32_t flush_domains,
Zou Nan hai852835f2010-05-21 09:08:56 +08001765 struct intel_ring_buffer *ring)
Daniel Vetter63560392010-02-19 11:51:59 +01001766{
1767 drm_i915_private_t *dev_priv = dev->dev_private;
1768 struct drm_i915_gem_object *obj_priv, *next;
1769
1770 list_for_each_entry_safe(obj_priv, next,
Chris Wilson64193402010-10-24 12:38:05 +01001771 &ring->gpu_write_list,
Daniel Vetter63560392010-02-19 11:51:59 +01001772 gpu_write_list) {
Daniel Vettera8089e82010-04-09 19:05:09 +00001773 struct drm_gem_object *obj = &obj_priv->base;
Daniel Vetter63560392010-02-19 11:51:59 +01001774
Chris Wilson64193402010-10-24 12:38:05 +01001775 if (obj->write_domain & flush_domains) {
Daniel Vetter63560392010-02-19 11:51:59 +01001776 uint32_t old_write_domain = obj->write_domain;
1777
1778 obj->write_domain = 0;
1779 list_del_init(&obj_priv->gpu_write_list);
Daniel Vetter617dbe22010-02-11 22:16:02 +01001780 i915_gem_object_move_to_active(obj, ring);
Daniel Vetter63560392010-02-19 11:51:59 +01001781
1782 /* update the fence lru list */
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001783 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
1784 struct drm_i915_fence_reg *reg =
1785 &dev_priv->fence_regs[obj_priv->fence_reg];
1786 list_move_tail(&reg->lru_list,
Daniel Vetter63560392010-02-19 11:51:59 +01001787 &dev_priv->mm.fence_list);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001788 }
Daniel Vetter63560392010-02-19 11:51:59 +01001789
1790 trace_i915_gem_object_change_domain(obj,
1791 obj->read_domains,
1792 old_write_domain);
1793 }
1794 }
1795}
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001796
Chris Wilson3cce4692010-10-27 16:11:02 +01001797int
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001798i915_add_request(struct drm_device *dev,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001799 struct drm_file *file,
Chris Wilson8dc5d142010-08-12 12:36:12 +01001800 struct drm_i915_gem_request *request,
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001801 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001802{
1803 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001804 struct drm_i915_file_private *file_priv = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001805 uint32_t seqno;
1806 int was_empty;
Chris Wilson3cce4692010-10-27 16:11:02 +01001807 int ret;
1808
1809 BUG_ON(request == NULL);
Eric Anholt673a3942008-07-30 12:06:12 -07001810
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001811 if (file != NULL)
1812 file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00001813
Chris Wilson3cce4692010-10-27 16:11:02 +01001814 ret = ring->add_request(ring, &seqno);
1815 if (ret)
1816 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001817
Chris Wilsona56ba562010-09-28 10:07:56 +01001818 ring->outstanding_lazy_request = false;
Eric Anholt673a3942008-07-30 12:06:12 -07001819
1820 request->seqno = seqno;
Zou Nan hai852835f2010-05-21 09:08:56 +08001821 request->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001822 request->emitted_jiffies = jiffies;
Zou Nan hai852835f2010-05-21 09:08:56 +08001823 was_empty = list_empty(&ring->request_list);
1824 list_add_tail(&request->list, &ring->request_list);
1825
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001826 if (file_priv) {
Chris Wilson1c255952010-09-26 11:03:27 +01001827 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001828 request->file_priv = file_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00001829 list_add_tail(&request->client_list,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001830 &file_priv->mm.request_list);
Chris Wilson1c255952010-09-26 11:03:27 +01001831 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00001832 }
Eric Anholt673a3942008-07-30 12:06:12 -07001833
Ben Gamarif65d9422009-09-14 17:48:44 -04001834 if (!dev_priv->mm.suspended) {
Chris Wilsonb3b079d2010-09-13 23:44:34 +01001835 mod_timer(&dev_priv->hangcheck_timer,
1836 jiffies + msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
Ben Gamarif65d9422009-09-14 17:48:44 -04001837 if (was_empty)
Chris Wilsonb3b079d2010-09-13 23:44:34 +01001838 queue_delayed_work(dev_priv->wq,
1839 &dev_priv->mm.retire_work, HZ);
Ben Gamarif65d9422009-09-14 17:48:44 -04001840 }
Chris Wilson3cce4692010-10-27 16:11:02 +01001841 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001842}
1843
1844/**
1845 * Command execution barrier
1846 *
1847 * Ensures that all commands in the ring are finished
1848 * before signalling the CPU
1849 */
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001850static void
Zou Nan hai852835f2010-05-21 09:08:56 +08001851i915_retire_commands(struct drm_device *dev, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001852{
Eric Anholt673a3942008-07-30 12:06:12 -07001853 uint32_t flush_domains = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001854
1855 /* The sampler always gets flushed on i965 (sigh) */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001856 if (INTEL_INFO(dev)->gen >= 4)
Eric Anholt673a3942008-07-30 12:06:12 -07001857 flush_domains |= I915_GEM_DOMAIN_SAMPLER;
Zou Nan hai852835f2010-05-21 09:08:56 +08001858
Chris Wilson78501ea2010-10-27 12:18:21 +01001859 ring->flush(ring, I915_GEM_DOMAIN_COMMAND, flush_domains);
Eric Anholt673a3942008-07-30 12:06:12 -07001860}
1861
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001862static inline void
1863i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
Eric Anholt673a3942008-07-30 12:06:12 -07001864{
Chris Wilson1c255952010-09-26 11:03:27 +01001865 struct drm_i915_file_private *file_priv = request->file_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07001866
Chris Wilson1c255952010-09-26 11:03:27 +01001867 if (!file_priv)
1868 return;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001869
Chris Wilson1c255952010-09-26 11:03:27 +01001870 spin_lock(&file_priv->mm.lock);
1871 list_del(&request->client_list);
1872 request->file_priv = NULL;
1873 spin_unlock(&file_priv->mm.lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001874}
1875
Chris Wilsondfaae392010-09-22 10:31:52 +01001876static void i915_gem_reset_ring_lists(struct drm_i915_private *dev_priv,
1877 struct intel_ring_buffer *ring)
Chris Wilson9375e442010-09-19 12:21:28 +01001878{
Chris Wilsondfaae392010-09-22 10:31:52 +01001879 while (!list_empty(&ring->request_list)) {
1880 struct drm_i915_gem_request *request;
Chris Wilson9375e442010-09-19 12:21:28 +01001881
Chris Wilsondfaae392010-09-22 10:31:52 +01001882 request = list_first_entry(&ring->request_list,
1883 struct drm_i915_gem_request,
1884 list);
1885
1886 list_del(&request->list);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001887 i915_gem_request_remove_from_client(request);
Chris Wilsondfaae392010-09-22 10:31:52 +01001888 kfree(request);
1889 }
1890
1891 while (!list_empty(&ring->active_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001892 struct drm_i915_gem_object *obj_priv;
1893
Chris Wilsondfaae392010-09-22 10:31:52 +01001894 obj_priv = list_first_entry(&ring->active_list,
Eric Anholt673a3942008-07-30 12:06:12 -07001895 struct drm_i915_gem_object,
Chris Wilson69dc4982010-10-19 10:36:51 +01001896 ring_list);
Eric Anholt673a3942008-07-30 12:06:12 -07001897
Chris Wilsondfaae392010-09-22 10:31:52 +01001898 obj_priv->base.write_domain = 0;
1899 list_del_init(&obj_priv->gpu_write_list);
1900 i915_gem_object_move_to_inactive(&obj_priv->base);
Eric Anholt673a3942008-07-30 12:06:12 -07001901 }
Eric Anholt673a3942008-07-30 12:06:12 -07001902}
1903
Chris Wilson069efc12010-09-30 16:53:18 +01001904void i915_gem_reset(struct drm_device *dev)
Eric Anholt673a3942008-07-30 12:06:12 -07001905{
Chris Wilsondfaae392010-09-22 10:31:52 +01001906 struct drm_i915_private *dev_priv = dev->dev_private;
1907 struct drm_i915_gem_object *obj_priv;
Chris Wilson069efc12010-09-30 16:53:18 +01001908 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07001909
Chris Wilsondfaae392010-09-22 10:31:52 +01001910 i915_gem_reset_ring_lists(dev_priv, &dev_priv->render_ring);
Chris Wilson87acb0a2010-10-19 10:13:00 +01001911 i915_gem_reset_ring_lists(dev_priv, &dev_priv->bsd_ring);
Chris Wilson549f7362010-10-19 11:19:32 +01001912 i915_gem_reset_ring_lists(dev_priv, &dev_priv->blt_ring);
Chris Wilsondfaae392010-09-22 10:31:52 +01001913
1914 /* Remove anything from the flushing lists. The GPU cache is likely
1915 * to be lost on reset along with the data, so simply move the
1916 * lost bo to the inactive list.
1917 */
1918 while (!list_empty(&dev_priv->mm.flushing_list)) {
Chris Wilson9375e442010-09-19 12:21:28 +01001919 obj_priv = list_first_entry(&dev_priv->mm.flushing_list,
1920 struct drm_i915_gem_object,
Chris Wilson69dc4982010-10-19 10:36:51 +01001921 mm_list);
Chris Wilson9375e442010-09-19 12:21:28 +01001922
1923 obj_priv->base.write_domain = 0;
Chris Wilsondfaae392010-09-22 10:31:52 +01001924 list_del_init(&obj_priv->gpu_write_list);
Chris Wilson9375e442010-09-19 12:21:28 +01001925 i915_gem_object_move_to_inactive(&obj_priv->base);
1926 }
Chris Wilson9375e442010-09-19 12:21:28 +01001927
Chris Wilsondfaae392010-09-22 10:31:52 +01001928 /* Move everything out of the GPU domains to ensure we do any
1929 * necessary invalidation upon reuse.
1930 */
Chris Wilson77f01232010-09-19 12:31:36 +01001931 list_for_each_entry(obj_priv,
1932 &dev_priv->mm.inactive_list,
Chris Wilson69dc4982010-10-19 10:36:51 +01001933 mm_list)
Chris Wilson77f01232010-09-19 12:31:36 +01001934 {
1935 obj_priv->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
1936 }
Chris Wilson069efc12010-09-30 16:53:18 +01001937
1938 /* The fence registers are invalidated so clear them out */
1939 for (i = 0; i < 16; i++) {
1940 struct drm_i915_fence_reg *reg;
1941
1942 reg = &dev_priv->fence_regs[i];
1943 if (!reg->obj)
1944 continue;
1945
1946 i915_gem_clear_fence_reg(reg->obj);
1947 }
Eric Anholt673a3942008-07-30 12:06:12 -07001948}
1949
1950/**
1951 * This function clears the request list as sequence numbers are passed.
1952 */
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001953static void
1954i915_gem_retire_requests_ring(struct drm_device *dev,
1955 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001956{
1957 drm_i915_private_t *dev_priv = dev->dev_private;
1958 uint32_t seqno;
1959
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001960 if (!ring->status_page.page_addr ||
1961 list_empty(&ring->request_list))
Karsten Wiese6c0594a2009-02-23 15:07:57 +01001962 return;
1963
Chris Wilson23bc5982010-09-29 16:10:57 +01001964 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07001965
Chris Wilson78501ea2010-10-27 12:18:21 +01001966 seqno = ring->get_seqno(ring);
Zou Nan hai852835f2010-05-21 09:08:56 +08001967 while (!list_empty(&ring->request_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001968 struct drm_i915_gem_request *request;
Eric Anholt673a3942008-07-30 12:06:12 -07001969
Zou Nan hai852835f2010-05-21 09:08:56 +08001970 request = list_first_entry(&ring->request_list,
Eric Anholt673a3942008-07-30 12:06:12 -07001971 struct drm_i915_gem_request,
1972 list);
Eric Anholt673a3942008-07-30 12:06:12 -07001973
Chris Wilsondfaae392010-09-22 10:31:52 +01001974 if (!i915_seqno_passed(seqno, request->seqno))
Eric Anholt673a3942008-07-30 12:06:12 -07001975 break;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001976
1977 trace_i915_gem_request_retire(dev, request->seqno);
1978
1979 list_del(&request->list);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001980 i915_gem_request_remove_from_client(request);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001981 kfree(request);
1982 }
1983
1984 /* Move any buffers on the active list that are no longer referenced
1985 * by the ringbuffer to the flushing/inactive lists as appropriate.
1986 */
1987 while (!list_empty(&ring->active_list)) {
1988 struct drm_gem_object *obj;
1989 struct drm_i915_gem_object *obj_priv;
1990
1991 obj_priv = list_first_entry(&ring->active_list,
1992 struct drm_i915_gem_object,
Chris Wilson69dc4982010-10-19 10:36:51 +01001993 ring_list);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001994
Chris Wilsondfaae392010-09-22 10:31:52 +01001995 if (!i915_seqno_passed(seqno, obj_priv->last_rendering_seqno))
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001996 break;
1997
1998 obj = &obj_priv->base;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001999 if (obj->write_domain != 0)
2000 i915_gem_object_move_to_flushing(obj);
2001 else
2002 i915_gem_object_move_to_inactive(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002003 }
Chris Wilson9d34e5d2009-09-24 05:26:06 +01002004
2005 if (unlikely (dev_priv->trace_irq_seqno &&
2006 i915_seqno_passed(dev_priv->trace_irq_seqno, seqno))) {
Chris Wilson78501ea2010-10-27 12:18:21 +01002007 ring->user_irq_put(ring);
Chris Wilson9d34e5d2009-09-24 05:26:06 +01002008 dev_priv->trace_irq_seqno = 0;
2009 }
Chris Wilson23bc5982010-09-29 16:10:57 +01002010
2011 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07002012}
2013
2014void
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002015i915_gem_retire_requests(struct drm_device *dev)
2016{
2017 drm_i915_private_t *dev_priv = dev->dev_private;
2018
Chris Wilsonbe726152010-07-23 23:18:50 +01002019 if (!list_empty(&dev_priv->mm.deferred_free_list)) {
2020 struct drm_i915_gem_object *obj_priv, *tmp;
2021
2022 /* We must be careful that during unbind() we do not
2023 * accidentally infinitely recurse into retire requests.
2024 * Currently:
2025 * retire -> free -> unbind -> wait -> retire_ring
2026 */
2027 list_for_each_entry_safe(obj_priv, tmp,
2028 &dev_priv->mm.deferred_free_list,
Chris Wilson69dc4982010-10-19 10:36:51 +01002029 mm_list)
Chris Wilsonbe726152010-07-23 23:18:50 +01002030 i915_gem_free_object_tail(&obj_priv->base);
2031 }
2032
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002033 i915_gem_retire_requests_ring(dev, &dev_priv->render_ring);
Chris Wilson87acb0a2010-10-19 10:13:00 +01002034 i915_gem_retire_requests_ring(dev, &dev_priv->bsd_ring);
Chris Wilson549f7362010-10-19 11:19:32 +01002035 i915_gem_retire_requests_ring(dev, &dev_priv->blt_ring);
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002036}
2037
Daniel Vetter75ef9da2010-08-21 00:25:16 +02002038static void
Eric Anholt673a3942008-07-30 12:06:12 -07002039i915_gem_retire_work_handler(struct work_struct *work)
2040{
2041 drm_i915_private_t *dev_priv;
2042 struct drm_device *dev;
2043
2044 dev_priv = container_of(work, drm_i915_private_t,
2045 mm.retire_work.work);
2046 dev = dev_priv->dev;
2047
Chris Wilson891b48c2010-09-29 12:26:37 +01002048 /* Come back later if the device is busy... */
2049 if (!mutex_trylock(&dev->struct_mutex)) {
2050 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
2051 return;
2052 }
2053
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002054 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08002055
Keith Packard6dbe2772008-10-14 21:41:13 -07002056 if (!dev_priv->mm.suspended &&
Zou Nan haid1b851f2010-05-21 09:08:57 +08002057 (!list_empty(&dev_priv->render_ring.request_list) ||
Chris Wilson549f7362010-10-19 11:19:32 +01002058 !list_empty(&dev_priv->bsd_ring.request_list) ||
2059 !list_empty(&dev_priv->blt_ring.request_list)))
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07002060 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
Eric Anholt673a3942008-07-30 12:06:12 -07002061 mutex_unlock(&dev->struct_mutex);
2062}
2063
Daniel Vetter5a5a0c62009-09-15 22:57:36 +02002064int
Zou Nan hai852835f2010-05-21 09:08:56 +08002065i915_do_wait_request(struct drm_device *dev, uint32_t seqno,
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002066 bool interruptible, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07002067{
2068 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes802c7eb2009-05-05 16:03:48 -07002069 u32 ier;
Eric Anholt673a3942008-07-30 12:06:12 -07002070 int ret = 0;
2071
2072 BUG_ON(seqno == 0);
2073
Ben Gamariba1234d2009-09-14 17:48:47 -04002074 if (atomic_read(&dev_priv->mm.wedged))
Chris Wilson30dbf0c2010-09-25 10:19:17 +01002075 return -EAGAIN;
Ben Gamariffed1d02009-09-14 17:48:41 -04002076
Chris Wilsona56ba562010-09-28 10:07:56 +01002077 if (ring->outstanding_lazy_request) {
Chris Wilson3cce4692010-10-27 16:11:02 +01002078 struct drm_i915_gem_request *request;
2079
2080 request = kzalloc(sizeof(*request), GFP_KERNEL);
2081 if (request == NULL)
Daniel Vettere35a41d2010-02-11 22:13:59 +01002082 return -ENOMEM;
Chris Wilson3cce4692010-10-27 16:11:02 +01002083
2084 ret = i915_add_request(dev, NULL, request, ring);
2085 if (ret) {
2086 kfree(request);
2087 return ret;
2088 }
2089
2090 seqno = request->seqno;
Daniel Vettere35a41d2010-02-11 22:13:59 +01002091 }
Chris Wilsona56ba562010-09-28 10:07:56 +01002092 BUG_ON(seqno == dev_priv->next_seqno);
Daniel Vettere35a41d2010-02-11 22:13:59 +01002093
Chris Wilson78501ea2010-10-27 12:18:21 +01002094 if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) {
Eric Anholtbad720f2009-10-22 16:11:14 -07002095 if (HAS_PCH_SPLIT(dev))
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002096 ier = I915_READ(DEIER) | I915_READ(GTIER);
2097 else
2098 ier = I915_READ(IER);
Jesse Barnes802c7eb2009-05-05 16:03:48 -07002099 if (!ier) {
2100 DRM_ERROR("something (likely vbetool) disabled "
2101 "interrupts, re-enabling\n");
2102 i915_driver_irq_preinstall(dev);
2103 i915_driver_irq_postinstall(dev);
2104 }
2105
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002106 trace_i915_gem_request_wait_begin(dev, seqno);
2107
Chris Wilsonb2223492010-10-27 15:27:33 +01002108 ring->waiting_seqno = seqno;
Chris Wilson78501ea2010-10-27 12:18:21 +01002109 ring->user_irq_get(ring);
Daniel Vetter48764bf2009-09-15 22:57:32 +02002110 if (interruptible)
Zou Nan hai852835f2010-05-21 09:08:56 +08002111 ret = wait_event_interruptible(ring->irq_queue,
Chris Wilson78501ea2010-10-27 12:18:21 +01002112 i915_seqno_passed(ring->get_seqno(ring), seqno)
Zou Nan hai852835f2010-05-21 09:08:56 +08002113 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02002114 else
Zou Nan hai852835f2010-05-21 09:08:56 +08002115 wait_event(ring->irq_queue,
Chris Wilson78501ea2010-10-27 12:18:21 +01002116 i915_seqno_passed(ring->get_seqno(ring), seqno)
Zou Nan hai852835f2010-05-21 09:08:56 +08002117 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02002118
Chris Wilson78501ea2010-10-27 12:18:21 +01002119 ring->user_irq_put(ring);
Chris Wilsonb2223492010-10-27 15:27:33 +01002120 ring->waiting_seqno = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002121
2122 trace_i915_gem_request_wait_end(dev, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07002123 }
Ben Gamariba1234d2009-09-14 17:48:47 -04002124 if (atomic_read(&dev_priv->mm.wedged))
Chris Wilson30dbf0c2010-09-25 10:19:17 +01002125 ret = -EAGAIN;
Eric Anholt673a3942008-07-30 12:06:12 -07002126
2127 if (ret && ret != -ERESTARTSYS)
Daniel Vetter8bff9172010-02-11 22:19:40 +01002128 DRM_ERROR("%s returns %d (awaiting %d at %d, next %d)\n",
Chris Wilson78501ea2010-10-27 12:18:21 +01002129 __func__, ret, seqno, ring->get_seqno(ring),
Daniel Vetter8bff9172010-02-11 22:19:40 +01002130 dev_priv->next_seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07002131
2132 /* Directly dispatch request retiring. While we have the work queue
2133 * to handle this, the waiter on a request often wants an associated
2134 * buffer to have made it to the inactive list, and we would need
2135 * a separate wait queue to handle that.
2136 */
2137 if (ret == 0)
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002138 i915_gem_retire_requests_ring(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07002139
2140 return ret;
2141}
2142
Daniel Vetter48764bf2009-09-15 22:57:32 +02002143/**
2144 * Waits for a sequence number to be signaled, and cleans up the
2145 * request and object lists appropriately for that event.
2146 */
2147static int
Zou Nan hai852835f2010-05-21 09:08:56 +08002148i915_wait_request(struct drm_device *dev, uint32_t seqno,
Chris Wilsona56ba562010-09-28 10:07:56 +01002149 struct intel_ring_buffer *ring)
Daniel Vetter48764bf2009-09-15 22:57:32 +02002150{
Zou Nan hai852835f2010-05-21 09:08:56 +08002151 return i915_do_wait_request(dev, seqno, 1, ring);
Daniel Vetter48764bf2009-09-15 22:57:32 +02002152}
2153
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002154static void
Chris Wilson92204342010-09-18 11:02:01 +01002155i915_gem_flush_ring(struct drm_device *dev,
Chris Wilsonc78ec302010-09-20 12:50:23 +01002156 struct drm_file *file_priv,
Chris Wilson92204342010-09-18 11:02:01 +01002157 struct intel_ring_buffer *ring,
2158 uint32_t invalidate_domains,
2159 uint32_t flush_domains)
2160{
Chris Wilson78501ea2010-10-27 12:18:21 +01002161 ring->flush(ring, invalidate_domains, flush_domains);
Chris Wilson92204342010-09-18 11:02:01 +01002162 i915_gem_process_flushing_list(dev, flush_domains, ring);
2163}
2164
2165static void
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002166i915_gem_flush(struct drm_device *dev,
Chris Wilsonc78ec302010-09-20 12:50:23 +01002167 struct drm_file *file_priv,
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002168 uint32_t invalidate_domains,
Chris Wilson92204342010-09-18 11:02:01 +01002169 uint32_t flush_domains,
2170 uint32_t flush_rings)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002171{
2172 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter8bff9172010-02-11 22:19:40 +01002173
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002174 if (flush_domains & I915_GEM_DOMAIN_CPU)
2175 drm_agp_chipset_flush(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08002176
Chris Wilson92204342010-09-18 11:02:01 +01002177 if ((flush_domains | invalidate_domains) & I915_GEM_GPU_DOMAINS) {
2178 if (flush_rings & RING_RENDER)
Chris Wilsonc78ec302010-09-20 12:50:23 +01002179 i915_gem_flush_ring(dev, file_priv,
Chris Wilson92204342010-09-18 11:02:01 +01002180 &dev_priv->render_ring,
2181 invalidate_domains, flush_domains);
2182 if (flush_rings & RING_BSD)
Chris Wilsonc78ec302010-09-20 12:50:23 +01002183 i915_gem_flush_ring(dev, file_priv,
Chris Wilson92204342010-09-18 11:02:01 +01002184 &dev_priv->bsd_ring,
2185 invalidate_domains, flush_domains);
Chris Wilson549f7362010-10-19 11:19:32 +01002186 if (flush_rings & RING_BLT)
2187 i915_gem_flush_ring(dev, file_priv,
2188 &dev_priv->blt_ring,
2189 invalidate_domains, flush_domains);
Chris Wilson92204342010-09-18 11:02:01 +01002190 }
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002191}
2192
Eric Anholt673a3942008-07-30 12:06:12 -07002193/**
2194 * Ensures that all rendering to the object has completed and the object is
2195 * safe to unbind from the GTT or access from the CPU.
2196 */
2197static int
Chris Wilson2cf34d72010-09-14 13:03:28 +01002198i915_gem_object_wait_rendering(struct drm_gem_object *obj,
2199 bool interruptible)
Eric Anholt673a3942008-07-30 12:06:12 -07002200{
2201 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01002202 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002203 int ret;
2204
Eric Anholte47c68e2008-11-14 13:35:19 -08002205 /* This function only exists to support waiting for existing rendering,
2206 * not for emitting required flushes.
Eric Anholt673a3942008-07-30 12:06:12 -07002207 */
Eric Anholte47c68e2008-11-14 13:35:19 -08002208 BUG_ON((obj->write_domain & I915_GEM_GPU_DOMAINS) != 0);
Eric Anholt673a3942008-07-30 12:06:12 -07002209
2210 /* If there is rendering queued on the buffer being evicted, wait for
2211 * it.
2212 */
2213 if (obj_priv->active) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01002214 ret = i915_do_wait_request(dev,
2215 obj_priv->last_rendering_seqno,
2216 interruptible,
2217 obj_priv->ring);
2218 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002219 return ret;
2220 }
2221
2222 return 0;
2223}
2224
2225/**
2226 * Unbinds an object from the GTT aperture.
2227 */
Jesse Barnes0f973f22009-01-26 17:10:45 -08002228int
Eric Anholt673a3942008-07-30 12:06:12 -07002229i915_gem_object_unbind(struct drm_gem_object *obj)
2230{
2231 struct drm_device *dev = obj->dev;
Chris Wilson73aa8082010-09-30 11:46:12 +01002232 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002233 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002234 int ret = 0;
2235
Eric Anholt673a3942008-07-30 12:06:12 -07002236 if (obj_priv->gtt_space == NULL)
2237 return 0;
2238
2239 if (obj_priv->pin_count != 0) {
2240 DRM_ERROR("Attempting to unbind pinned buffer\n");
2241 return -EINVAL;
2242 }
2243
Eric Anholt5323fd02009-09-09 11:50:45 -07002244 /* blow away mappings if mapped through GTT */
2245 i915_gem_release_mmap(obj);
2246
Eric Anholt673a3942008-07-30 12:06:12 -07002247 /* Move the object to the CPU domain to ensure that
2248 * any possible CPU writes while it's not in the GTT
2249 * are flushed when we go to remap it. This will
2250 * also ensure that all pending GPU writes are finished
2251 * before we unbind.
2252 */
Eric Anholte47c68e2008-11-14 13:35:19 -08002253 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Chris Wilson8dc17752010-07-23 23:18:51 +01002254 if (ret == -ERESTARTSYS)
Eric Anholt673a3942008-07-30 12:06:12 -07002255 return ret;
Chris Wilson8dc17752010-07-23 23:18:51 +01002256 /* Continue on if we fail due to EIO, the GPU is hung so we
2257 * should be safe and we need to cleanup or else we might
2258 * cause memory corruption through use-after-free.
2259 */
Chris Wilson812ed4922010-09-30 15:08:57 +01002260 if (ret) {
2261 i915_gem_clflush_object(obj);
2262 obj->read_domains = obj->write_domain = I915_GEM_DOMAIN_CPU;
2263 }
Eric Anholt673a3942008-07-30 12:06:12 -07002264
Daniel Vetter96b47b62009-12-15 17:50:00 +01002265 /* release the fence reg _after_ flushing */
2266 if (obj_priv->fence_reg != I915_FENCE_REG_NONE)
2267 i915_gem_clear_fence_reg(obj);
2268
Chris Wilson73aa8082010-09-30 11:46:12 +01002269 drm_unbind_agp(obj_priv->agp_mem);
2270 drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002271
Chris Wilsone5281cc2010-10-28 13:45:36 +01002272 i915_gem_object_put_pages_gtt(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002273
Chris Wilsona00b10c2010-09-24 21:15:47 +01002274 i915_gem_info_remove_gtt(dev_priv, obj_priv);
Chris Wilson69dc4982010-10-19 10:36:51 +01002275 list_del_init(&obj_priv->mm_list);
Chris Wilsona00b10c2010-09-24 21:15:47 +01002276 obj_priv->fenceable = true;
2277 obj_priv->mappable = true;
Eric Anholt673a3942008-07-30 12:06:12 -07002278
Chris Wilson73aa8082010-09-30 11:46:12 +01002279 drm_mm_put_block(obj_priv->gtt_space);
2280 obj_priv->gtt_space = NULL;
Chris Wilson9af90d12010-10-17 10:01:56 +01002281 obj_priv->gtt_offset = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002282
Chris Wilson963b4832009-09-20 23:03:54 +01002283 if (i915_gem_object_is_purgeable(obj_priv))
2284 i915_gem_object_truncate(obj);
2285
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002286 trace_i915_gem_object_unbind(obj);
2287
Chris Wilson8dc17752010-07-23 23:18:51 +01002288 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002289}
2290
Chris Wilsona56ba562010-09-28 10:07:56 +01002291static int i915_ring_idle(struct drm_device *dev,
2292 struct intel_ring_buffer *ring)
2293{
Chris Wilson64193402010-10-24 12:38:05 +01002294 if (list_empty(&ring->gpu_write_list))
2295 return 0;
2296
Chris Wilsona56ba562010-09-28 10:07:56 +01002297 i915_gem_flush_ring(dev, NULL, ring,
2298 I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
2299 return i915_wait_request(dev,
2300 i915_gem_next_request_seqno(dev, ring),
2301 ring);
2302}
2303
Chris Wilsonb47eb4a2010-08-07 11:01:23 +01002304int
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002305i915_gpu_idle(struct drm_device *dev)
2306{
2307 drm_i915_private_t *dev_priv = dev->dev_private;
2308 bool lists_empty;
Zou Nan hai852835f2010-05-21 09:08:56 +08002309 int ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002310
Zou Nan haid1b851f2010-05-21 09:08:57 +08002311 lists_empty = (list_empty(&dev_priv->mm.flushing_list) &&
2312 list_empty(&dev_priv->render_ring.active_list) &&
Chris Wilson549f7362010-10-19 11:19:32 +01002313 list_empty(&dev_priv->bsd_ring.active_list) &&
2314 list_empty(&dev_priv->blt_ring.active_list));
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002315 if (lists_empty)
2316 return 0;
2317
2318 /* Flush everything onto the inactive list. */
Chris Wilsona56ba562010-09-28 10:07:56 +01002319 ret = i915_ring_idle(dev, &dev_priv->render_ring);
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002320 if (ret)
2321 return ret;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002322
Chris Wilson87acb0a2010-10-19 10:13:00 +01002323 ret = i915_ring_idle(dev, &dev_priv->bsd_ring);
2324 if (ret)
2325 return ret;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002326
Chris Wilson549f7362010-10-19 11:19:32 +01002327 ret = i915_ring_idle(dev, &dev_priv->blt_ring);
2328 if (ret)
2329 return ret;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002330
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002331 return 0;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002332}
2333
Chris Wilsona00b10c2010-09-24 21:15:47 +01002334static void sandybridge_write_fence_reg(struct drm_gem_object *obj)
Eric Anholt4e901fd2009-10-26 16:44:17 -07002335{
Eric Anholt4e901fd2009-10-26 16:44:17 -07002336 struct drm_device *dev = obj->dev;
2337 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002338 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsona00b10c2010-09-24 21:15:47 +01002339 u32 size = i915_gem_get_gtt_size(obj_priv);
Eric Anholt4e901fd2009-10-26 16:44:17 -07002340 int regnum = obj_priv->fence_reg;
2341 uint64_t val;
2342
Chris Wilsona00b10c2010-09-24 21:15:47 +01002343 val = (uint64_t)((obj_priv->gtt_offset + size - 4096) &
Eric Anholt4e901fd2009-10-26 16:44:17 -07002344 0xfffff000) << 32;
2345 val |= obj_priv->gtt_offset & 0xfffff000;
2346 val |= (uint64_t)((obj_priv->stride / 128) - 1) <<
2347 SANDYBRIDGE_FENCE_PITCH_SHIFT;
2348
2349 if (obj_priv->tiling_mode == I915_TILING_Y)
2350 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2351 val |= I965_FENCE_REG_VALID;
2352
2353 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (regnum * 8), val);
2354}
2355
Chris Wilsona00b10c2010-09-24 21:15:47 +01002356static void i965_write_fence_reg(struct drm_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002357{
Jesse Barnesde151cf2008-11-12 10:03:55 -08002358 struct drm_device *dev = obj->dev;
2359 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002360 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsona00b10c2010-09-24 21:15:47 +01002361 u32 size = i915_gem_get_gtt_size(obj_priv);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002362 int regnum = obj_priv->fence_reg;
2363 uint64_t val;
2364
Chris Wilsona00b10c2010-09-24 21:15:47 +01002365 val = (uint64_t)((obj_priv->gtt_offset + size - 4096) &
Jesse Barnesde151cf2008-11-12 10:03:55 -08002366 0xfffff000) << 32;
2367 val |= obj_priv->gtt_offset & 0xfffff000;
2368 val |= ((obj_priv->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
2369 if (obj_priv->tiling_mode == I915_TILING_Y)
2370 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2371 val |= I965_FENCE_REG_VALID;
2372
2373 I915_WRITE64(FENCE_REG_965_0 + (regnum * 8), val);
2374}
2375
Chris Wilsona00b10c2010-09-24 21:15:47 +01002376static void i915_write_fence_reg(struct drm_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002377{
Jesse Barnesde151cf2008-11-12 10:03:55 -08002378 struct drm_device *dev = obj->dev;
2379 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002380 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsona00b10c2010-09-24 21:15:47 +01002381 u32 size = i915_gem_get_gtt_size(obj_priv);
2382 uint32_t fence_reg, val, pitch_val;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002383 int tile_width;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002384
2385 if ((obj_priv->gtt_offset & ~I915_FENCE_START_MASK) ||
Chris Wilsona00b10c2010-09-24 21:15:47 +01002386 (obj_priv->gtt_offset & (size - 1))) {
2387 WARN(1, "%s: object 0x%08x [fenceable? %d] not 1M or size (0x%08x) aligned [gtt_space offset=%lx, size=%lx]\n",
2388 __func__, obj_priv->gtt_offset, obj_priv->fenceable, size,
2389 obj_priv->gtt_space->start, obj_priv->gtt_space->size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002390 return;
2391 }
2392
Jesse Barnes0f973f22009-01-26 17:10:45 -08002393 if (obj_priv->tiling_mode == I915_TILING_Y &&
2394 HAS_128_BYTE_Y_TILING(dev))
2395 tile_width = 128;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002396 else
Jesse Barnes0f973f22009-01-26 17:10:45 -08002397 tile_width = 512;
2398
2399 /* Note: pitch better be a power of two tile widths */
2400 pitch_val = obj_priv->stride / tile_width;
2401 pitch_val = ffs(pitch_val) - 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002402
Daniel Vetterc36a2a62010-04-17 15:12:03 +02002403 if (obj_priv->tiling_mode == I915_TILING_Y &&
2404 HAS_128_BYTE_Y_TILING(dev))
2405 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2406 else
2407 WARN_ON(pitch_val > I915_FENCE_MAX_PITCH_VAL);
2408
Jesse Barnesde151cf2008-11-12 10:03:55 -08002409 val = obj_priv->gtt_offset;
2410 if (obj_priv->tiling_mode == I915_TILING_Y)
2411 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002412 val |= I915_FENCE_SIZE_BITS(size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002413 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2414 val |= I830_FENCE_REG_VALID;
2415
Chris Wilsona00b10c2010-09-24 21:15:47 +01002416 fence_reg = obj_priv->fence_reg;
2417 if (fence_reg < 8)
2418 fence_reg = FENCE_REG_830_0 + fence_reg * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002419 else
Chris Wilsona00b10c2010-09-24 21:15:47 +01002420 fence_reg = FENCE_REG_945_8 + (fence_reg - 8) * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002421 I915_WRITE(fence_reg, val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002422}
2423
Chris Wilsona00b10c2010-09-24 21:15:47 +01002424static void i830_write_fence_reg(struct drm_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002425{
Jesse Barnesde151cf2008-11-12 10:03:55 -08002426 struct drm_device *dev = obj->dev;
2427 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002428 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsona00b10c2010-09-24 21:15:47 +01002429 u32 size = i915_gem_get_gtt_size(obj_priv);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002430 int regnum = obj_priv->fence_reg;
2431 uint32_t val;
2432 uint32_t pitch_val;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002433 uint32_t fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002434
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002435 if ((obj_priv->gtt_offset & ~I830_FENCE_START_MASK) ||
Jesse Barnesde151cf2008-11-12 10:03:55 -08002436 (obj_priv->gtt_offset & (obj->size - 1))) {
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002437 WARN(1, "%s: object 0x%08x not 512K or size aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002438 __func__, obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002439 return;
2440 }
2441
Eric Anholte76a16d2009-05-26 17:44:56 -07002442 pitch_val = obj_priv->stride / 128;
2443 pitch_val = ffs(pitch_val) - 1;
2444 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2445
Jesse Barnesde151cf2008-11-12 10:03:55 -08002446 val = obj_priv->gtt_offset;
2447 if (obj_priv->tiling_mode == I915_TILING_Y)
2448 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002449 fence_size_bits = I830_FENCE_SIZE_BITS(size);
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002450 WARN_ON(fence_size_bits & ~0x00000f00);
2451 val |= fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002452 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2453 val |= I830_FENCE_REG_VALID;
2454
2455 I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002456}
2457
Chris Wilson2cf34d72010-09-14 13:03:28 +01002458static int i915_find_fence_reg(struct drm_device *dev,
2459 bool interruptible)
Daniel Vetterae3db242010-02-19 11:51:58 +01002460{
Daniel Vetterae3db242010-02-19 11:51:58 +01002461 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002462 struct drm_i915_fence_reg *reg;
2463 struct drm_i915_gem_object *obj_priv = NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002464 int i, avail, ret;
2465
2466 /* First try to find a free reg */
2467 avail = 0;
2468 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2469 reg = &dev_priv->fence_regs[i];
2470 if (!reg->obj)
2471 return i;
2472
Daniel Vetter23010e42010-03-08 13:35:02 +01002473 obj_priv = to_intel_bo(reg->obj);
Daniel Vetterae3db242010-02-19 11:51:58 +01002474 if (!obj_priv->pin_count)
2475 avail++;
2476 }
2477
2478 if (avail == 0)
2479 return -ENOSPC;
2480
2481 /* None available, try to steal one or wait for a user to finish */
Chris Wilsona00b10c2010-09-24 21:15:47 +01002482 avail = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002483 list_for_each_entry(reg, &dev_priv->mm.fence_list,
2484 lru_list) {
Chris Wilsona00b10c2010-09-24 21:15:47 +01002485 obj_priv = to_intel_bo(reg->obj);
Daniel Vetterae3db242010-02-19 11:51:58 +01002486 if (obj_priv->pin_count)
2487 continue;
2488
2489 /* found one! */
Chris Wilsona00b10c2010-09-24 21:15:47 +01002490 avail = obj_priv->fence_reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01002491 break;
2492 }
2493
Chris Wilsona00b10c2010-09-24 21:15:47 +01002494 BUG_ON(avail == I915_FENCE_REG_NONE);
Daniel Vetterae3db242010-02-19 11:51:58 +01002495
2496 /* We only have a reference on obj from the active list. put_fence_reg
2497 * might drop that one, causing a use-after-free in it. So hold a
2498 * private reference to obj like the other callers of put_fence_reg
2499 * (set_tiling ioctl) do. */
Chris Wilsona00b10c2010-09-24 21:15:47 +01002500 drm_gem_object_reference(&obj_priv->base);
2501 ret = i915_gem_object_put_fence_reg(&obj_priv->base, interruptible);
2502 drm_gem_object_unreference(&obj_priv->base);
Daniel Vetterae3db242010-02-19 11:51:58 +01002503 if (ret != 0)
2504 return ret;
2505
Chris Wilsona00b10c2010-09-24 21:15:47 +01002506 return avail;
Daniel Vetterae3db242010-02-19 11:51:58 +01002507}
2508
Jesse Barnesde151cf2008-11-12 10:03:55 -08002509/**
2510 * i915_gem_object_get_fence_reg - set up a fence reg for an object
2511 * @obj: object to map through a fence reg
2512 *
2513 * When mapping objects through the GTT, userspace wants to be able to write
2514 * to them without having to worry about swizzling if the object is tiled.
2515 *
2516 * This function walks the fence regs looking for a free one for @obj,
2517 * stealing one if it can't find any.
2518 *
2519 * It then sets up the reg based on the object's properties: address, pitch
2520 * and tiling format.
2521 */
Chris Wilson8c4b8c32009-06-17 22:08:52 +01002522int
Chris Wilson2cf34d72010-09-14 13:03:28 +01002523i915_gem_object_get_fence_reg(struct drm_gem_object *obj,
2524 bool interruptible)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002525{
2526 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002527 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002528 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002529 struct drm_i915_fence_reg *reg = NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002530 int ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002531
Eric Anholta09ba7f2009-08-29 12:49:51 -07002532 /* Just update our place in the LRU if our fence is getting used. */
2533 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002534 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
2535 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002536 return 0;
2537 }
2538
Jesse Barnesde151cf2008-11-12 10:03:55 -08002539 switch (obj_priv->tiling_mode) {
2540 case I915_TILING_NONE:
2541 WARN(1, "allocating a fence for non-tiled object?\n");
2542 break;
2543 case I915_TILING_X:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002544 if (!obj_priv->stride)
2545 return -EINVAL;
2546 WARN((obj_priv->stride & (512 - 1)),
2547 "object 0x%08x is X tiled but has non-512B pitch\n",
2548 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002549 break;
2550 case I915_TILING_Y:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002551 if (!obj_priv->stride)
2552 return -EINVAL;
2553 WARN((obj_priv->stride & (128 - 1)),
2554 "object 0x%08x is Y tiled but has non-128B pitch\n",
2555 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002556 break;
2557 }
2558
Chris Wilson2cf34d72010-09-14 13:03:28 +01002559 ret = i915_find_fence_reg(dev, interruptible);
Daniel Vetterae3db242010-02-19 11:51:58 +01002560 if (ret < 0)
2561 return ret;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002562
Daniel Vetterae3db242010-02-19 11:51:58 +01002563 obj_priv->fence_reg = ret;
2564 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002565 list_add_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002566
Jesse Barnesde151cf2008-11-12 10:03:55 -08002567 reg->obj = obj;
2568
Chris Wilsone259bef2010-09-17 00:32:02 +01002569 switch (INTEL_INFO(dev)->gen) {
2570 case 6:
Chris Wilsona00b10c2010-09-24 21:15:47 +01002571 sandybridge_write_fence_reg(obj);
Chris Wilsone259bef2010-09-17 00:32:02 +01002572 break;
2573 case 5:
2574 case 4:
Chris Wilsona00b10c2010-09-24 21:15:47 +01002575 i965_write_fence_reg(obj);
Chris Wilsone259bef2010-09-17 00:32:02 +01002576 break;
2577 case 3:
Chris Wilsona00b10c2010-09-24 21:15:47 +01002578 i915_write_fence_reg(obj);
Chris Wilsone259bef2010-09-17 00:32:02 +01002579 break;
2580 case 2:
Chris Wilsona00b10c2010-09-24 21:15:47 +01002581 i830_write_fence_reg(obj);
Chris Wilsone259bef2010-09-17 00:32:02 +01002582 break;
2583 }
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002584
Chris Wilsona00b10c2010-09-24 21:15:47 +01002585 trace_i915_gem_object_get_fence(obj,
2586 obj_priv->fence_reg,
2587 obj_priv->tiling_mode);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002588
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002589 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002590}
2591
2592/**
2593 * i915_gem_clear_fence_reg - clear out fence register info
2594 * @obj: object to clear
2595 *
2596 * Zeroes out the fence register itself and clears out the associated
2597 * data structures in dev_priv and obj_priv.
2598 */
2599static void
2600i915_gem_clear_fence_reg(struct drm_gem_object *obj)
2601{
2602 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002603 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002604 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002605 struct drm_i915_fence_reg *reg =
2606 &dev_priv->fence_regs[obj_priv->fence_reg];
Chris Wilsone259bef2010-09-17 00:32:02 +01002607 uint32_t fence_reg;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002608
Chris Wilsone259bef2010-09-17 00:32:02 +01002609 switch (INTEL_INFO(dev)->gen) {
2610 case 6:
Eric Anholt4e901fd2009-10-26 16:44:17 -07002611 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 +
2612 (obj_priv->fence_reg * 8), 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002613 break;
2614 case 5:
2615 case 4:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002616 I915_WRITE64(FENCE_REG_965_0 + (obj_priv->fence_reg * 8), 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002617 break;
2618 case 3:
Chris Wilson9b74f732010-09-22 19:10:44 +01002619 if (obj_priv->fence_reg >= 8)
Chris Wilsone259bef2010-09-17 00:32:02 +01002620 fence_reg = FENCE_REG_945_8 + (obj_priv->fence_reg - 8) * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002621 else
Chris Wilsone259bef2010-09-17 00:32:02 +01002622 case 2:
2623 fence_reg = FENCE_REG_830_0 + obj_priv->fence_reg * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002624
2625 I915_WRITE(fence_reg, 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002626 break;
Eric Anholtdc529a42009-03-10 22:34:49 -07002627 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002628
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002629 reg->obj = NULL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002630 obj_priv->fence_reg = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002631 list_del_init(&reg->lru_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002632}
2633
Eric Anholt673a3942008-07-30 12:06:12 -07002634/**
Chris Wilson52dc7d32009-06-06 09:46:01 +01002635 * i915_gem_object_put_fence_reg - waits on outstanding fenced access
2636 * to the buffer to finish, and then resets the fence register.
2637 * @obj: tiled object holding a fence register.
Chris Wilson2cf34d72010-09-14 13:03:28 +01002638 * @bool: whether the wait upon the fence is interruptible
Chris Wilson52dc7d32009-06-06 09:46:01 +01002639 *
2640 * Zeroes out the fence register itself and clears out the associated
2641 * data structures in dev_priv and obj_priv.
2642 */
2643int
Chris Wilson2cf34d72010-09-14 13:03:28 +01002644i915_gem_object_put_fence_reg(struct drm_gem_object *obj,
2645 bool interruptible)
Chris Wilson52dc7d32009-06-06 09:46:01 +01002646{
2647 struct drm_device *dev = obj->dev;
Chris Wilson53640e12010-09-20 11:40:50 +01002648 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002649 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson53640e12010-09-20 11:40:50 +01002650 struct drm_i915_fence_reg *reg;
Chris Wilson52dc7d32009-06-06 09:46:01 +01002651
2652 if (obj_priv->fence_reg == I915_FENCE_REG_NONE)
2653 return 0;
2654
Daniel Vetter10ae9bd2010-02-01 13:59:17 +01002655 /* If we've changed tiling, GTT-mappings of the object
2656 * need to re-fault to ensure that the correct fence register
2657 * setup is in place.
2658 */
2659 i915_gem_release_mmap(obj);
2660
Chris Wilson52dc7d32009-06-06 09:46:01 +01002661 /* On the i915, GPU access to tiled buffers is via a fence,
2662 * therefore we must wait for any outstanding access to complete
2663 * before clearing the fence.
2664 */
Chris Wilson53640e12010-09-20 11:40:50 +01002665 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
2666 if (reg->gpu) {
Chris Wilson52dc7d32009-06-06 09:46:01 +01002667 int ret;
2668
Chris Wilson2cf34d72010-09-14 13:03:28 +01002669 ret = i915_gem_object_flush_gpu_write_domain(obj, true);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002670 if (ret)
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002671 return ret;
2672
Chris Wilson2cf34d72010-09-14 13:03:28 +01002673 ret = i915_gem_object_wait_rendering(obj, interruptible);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002674 if (ret)
Chris Wilson52dc7d32009-06-06 09:46:01 +01002675 return ret;
Chris Wilson53640e12010-09-20 11:40:50 +01002676
2677 reg->gpu = false;
Chris Wilson52dc7d32009-06-06 09:46:01 +01002678 }
2679
Daniel Vetter4a726612010-02-01 13:59:16 +01002680 i915_gem_object_flush_gtt_write_domain(obj);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002681 i915_gem_clear_fence_reg(obj);
Chris Wilson52dc7d32009-06-06 09:46:01 +01002682
2683 return 0;
2684}
2685
2686/**
Eric Anholt673a3942008-07-30 12:06:12 -07002687 * Finds free space in the GTT aperture and binds the object there.
2688 */
2689static int
Daniel Vetter920afa72010-09-16 17:54:23 +02002690i915_gem_object_bind_to_gtt(struct drm_gem_object *obj,
2691 unsigned alignment,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002692 bool mappable,
2693 bool need_fence)
Eric Anholt673a3942008-07-30 12:06:12 -07002694{
2695 struct drm_device *dev = obj->dev;
2696 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002697 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002698 struct drm_mm_node *free_space;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002699 gfp_t gfpmask = __GFP_NORETRY | __GFP_NOWARN;
2700 u32 size, fence_size, fence_alignment;
Chris Wilson07f73f62009-09-14 16:50:30 +01002701 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002702
Chris Wilsonbb6baf72009-09-22 14:24:13 +01002703 if (obj_priv->madv != I915_MADV_WILLNEED) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01002704 DRM_ERROR("Attempting to bind a purgeable object\n");
2705 return -EINVAL;
2706 }
2707
Chris Wilsona00b10c2010-09-24 21:15:47 +01002708 fence_size = i915_gem_get_gtt_size(obj_priv);
2709 fence_alignment = i915_gem_get_gtt_alignment(obj_priv);
2710
Eric Anholt673a3942008-07-30 12:06:12 -07002711 if (alignment == 0)
Chris Wilsona00b10c2010-09-24 21:15:47 +01002712 alignment = need_fence ? fence_alignment : 4096;
2713 if (need_fence && alignment & (fence_alignment - 1)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002714 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2715 return -EINVAL;
2716 }
2717
Chris Wilsona00b10c2010-09-24 21:15:47 +01002718 size = need_fence ? fence_size : obj->size;
2719
Chris Wilson654fc602010-05-27 13:18:21 +01002720 /* If the object is bigger than the entire aperture, reject it early
2721 * before evicting everything in a vain attempt to find space.
2722 */
Daniel Vetter920afa72010-09-16 17:54:23 +02002723 if (obj->size >
2724 (mappable ? dev_priv->mm.gtt_mappable_end : dev_priv->mm.gtt_total)) {
Chris Wilson654fc602010-05-27 13:18:21 +01002725 DRM_ERROR("Attempting to bind an object larger than the aperture\n");
2726 return -E2BIG;
2727 }
2728
Eric Anholt673a3942008-07-30 12:06:12 -07002729 search_free:
Daniel Vetter920afa72010-09-16 17:54:23 +02002730 if (mappable)
2731 free_space =
2732 drm_mm_search_free_in_range(&dev_priv->mm.gtt_space,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002733 size, alignment, 0,
Daniel Vetter920afa72010-09-16 17:54:23 +02002734 dev_priv->mm.gtt_mappable_end,
2735 0);
2736 else
2737 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002738 size, alignment, 0);
Daniel Vetter920afa72010-09-16 17:54:23 +02002739
2740 if (free_space != NULL) {
2741 if (mappable)
2742 obj_priv->gtt_space =
2743 drm_mm_get_block_range_generic(free_space,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002744 size, alignment, 0,
Daniel Vetter920afa72010-09-16 17:54:23 +02002745 dev_priv->mm.gtt_mappable_end,
2746 0);
2747 else
2748 obj_priv->gtt_space =
Chris Wilsona00b10c2010-09-24 21:15:47 +01002749 drm_mm_get_block(free_space, size, alignment);
Daniel Vetter920afa72010-09-16 17:54:23 +02002750 }
Eric Anholt673a3942008-07-30 12:06:12 -07002751 if (obj_priv->gtt_space == NULL) {
2752 /* If the gtt is empty and we're still having trouble
2753 * fitting our object in, we're out of memory.
2754 */
Chris Wilsona00b10c2010-09-24 21:15:47 +01002755 ret = i915_gem_evict_something(dev, size, alignment, mappable);
Chris Wilson97311292009-09-21 00:22:34 +01002756 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002757 return ret;
Chris Wilson97311292009-09-21 00:22:34 +01002758
Eric Anholt673a3942008-07-30 12:06:12 -07002759 goto search_free;
2760 }
2761
Chris Wilsone5281cc2010-10-28 13:45:36 +01002762 ret = i915_gem_object_get_pages_gtt(obj, gfpmask);
Eric Anholt673a3942008-07-30 12:06:12 -07002763 if (ret) {
2764 drm_mm_put_block(obj_priv->gtt_space);
2765 obj_priv->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002766
2767 if (ret == -ENOMEM) {
2768 /* first try to clear up some space from the GTT */
Chris Wilsona00b10c2010-09-24 21:15:47 +01002769 ret = i915_gem_evict_something(dev, size,
Daniel Vetter920afa72010-09-16 17:54:23 +02002770 alignment, mappable);
Chris Wilson07f73f62009-09-14 16:50:30 +01002771 if (ret) {
Chris Wilson07f73f62009-09-14 16:50:30 +01002772 /* now try to shrink everyone else */
Chris Wilson4bdadb92010-01-27 13:36:32 +00002773 if (gfpmask) {
2774 gfpmask = 0;
2775 goto search_free;
Chris Wilson07f73f62009-09-14 16:50:30 +01002776 }
2777
2778 return ret;
2779 }
2780
2781 goto search_free;
2782 }
2783
Eric Anholt673a3942008-07-30 12:06:12 -07002784 return ret;
2785 }
2786
Eric Anholt673a3942008-07-30 12:06:12 -07002787 /* Create an AGP memory structure pointing at our pages, and bind it
2788 * into the GTT.
2789 */
2790 obj_priv->agp_mem = drm_agp_bind_pages(dev,
Eric Anholt856fa192009-03-19 14:10:50 -07002791 obj_priv->pages,
Chris Wilson07f73f62009-09-14 16:50:30 +01002792 obj->size >> PAGE_SHIFT,
Chris Wilson9af90d12010-10-17 10:01:56 +01002793 obj_priv->gtt_space->start,
Keith Packardba1eb1d2008-10-14 19:55:10 -07002794 obj_priv->agp_type);
Eric Anholt673a3942008-07-30 12:06:12 -07002795 if (obj_priv->agp_mem == NULL) {
Chris Wilsone5281cc2010-10-28 13:45:36 +01002796 i915_gem_object_put_pages_gtt(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002797 drm_mm_put_block(obj_priv->gtt_space);
2798 obj_priv->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002799
Chris Wilsona00b10c2010-09-24 21:15:47 +01002800 ret = i915_gem_evict_something(dev, size,
2801 alignment, mappable);
Chris Wilson97311292009-09-21 00:22:34 +01002802 if (ret)
Chris Wilson07f73f62009-09-14 16:50:30 +01002803 return ret;
Chris Wilson07f73f62009-09-14 16:50:30 +01002804
2805 goto search_free;
Eric Anholt673a3942008-07-30 12:06:12 -07002806 }
Eric Anholt673a3942008-07-30 12:06:12 -07002807
Daniel Vetterfb7d5162010-10-01 22:05:20 +02002808 obj_priv->gtt_offset = obj_priv->gtt_space->start;
2809
Chris Wilsonbf1a1092010-08-07 11:01:20 +01002810 /* keep track of bounds object by adding it to the inactive list */
Chris Wilson69dc4982010-10-19 10:36:51 +01002811 list_add_tail(&obj_priv->mm_list, &dev_priv->mm.inactive_list);
Chris Wilsona00b10c2010-09-24 21:15:47 +01002812 i915_gem_info_add_gtt(dev_priv, obj_priv);
Chris Wilsonbf1a1092010-08-07 11:01:20 +01002813
Eric Anholt673a3942008-07-30 12:06:12 -07002814 /* Assert that the object is not currently in any GPU domain. As it
2815 * wasn't in the GTT, there shouldn't be any way it could have been in
2816 * a GPU cache
2817 */
Chris Wilson21d509e2009-06-06 09:46:02 +01002818 BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS);
2819 BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS);
Eric Anholt673a3942008-07-30 12:06:12 -07002820
Daniel Vetterec57d262010-09-30 23:42:15 +02002821 trace_i915_gem_object_bind(obj, obj_priv->gtt_offset, mappable);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002822
Chris Wilsona00b10c2010-09-24 21:15:47 +01002823 obj_priv->fenceable =
2824 obj_priv->gtt_space->size == fence_size &&
2825 (obj_priv->gtt_space->start & (fence_alignment -1)) == 0;
2826
2827 obj_priv->mappable =
2828 obj_priv->gtt_offset + obj->size <= dev_priv->mm.gtt_mappable_end;
2829
Eric Anholt673a3942008-07-30 12:06:12 -07002830 return 0;
2831}
2832
2833void
2834i915_gem_clflush_object(struct drm_gem_object *obj)
2835{
Daniel Vetter23010e42010-03-08 13:35:02 +01002836 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002837
2838 /* If we don't have a page list set up, then we're not pinned
2839 * to GPU, and we can ignore the cache flush because it'll happen
2840 * again at bind time.
2841 */
Eric Anholt856fa192009-03-19 14:10:50 -07002842 if (obj_priv->pages == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002843 return;
2844
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002845 trace_i915_gem_object_clflush(obj);
Eric Anholtcfa16a02009-05-26 18:46:16 -07002846
Eric Anholt856fa192009-03-19 14:10:50 -07002847 drm_clflush_pages(obj_priv->pages, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002848}
2849
Eric Anholte47c68e2008-11-14 13:35:19 -08002850/** Flushes any GPU write domain for the object if it's dirty. */
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002851static int
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002852i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj,
2853 bool pipelined)
Eric Anholte47c68e2008-11-14 13:35:19 -08002854{
2855 struct drm_device *dev = obj->dev;
Eric Anholte47c68e2008-11-14 13:35:19 -08002856
2857 if ((obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002858 return 0;
Eric Anholte47c68e2008-11-14 13:35:19 -08002859
2860 /* Queue the GPU write cache flushing we need. */
Chris Wilsonc78ec302010-09-20 12:50:23 +01002861 i915_gem_flush_ring(dev, NULL,
Chris Wilson92204342010-09-18 11:02:01 +01002862 to_intel_bo(obj)->ring,
2863 0, obj->write_domain);
Chris Wilson48b956c2010-09-14 12:50:34 +01002864 BUG_ON(obj->write_domain);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002865
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002866 if (pipelined)
2867 return 0;
2868
Chris Wilson2cf34d72010-09-14 13:03:28 +01002869 return i915_gem_object_wait_rendering(obj, true);
Eric Anholte47c68e2008-11-14 13:35:19 -08002870}
2871
2872/** Flushes the GTT write domain for the object if it's dirty. */
2873static void
2874i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj)
2875{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002876 uint32_t old_write_domain;
2877
Eric Anholte47c68e2008-11-14 13:35:19 -08002878 if (obj->write_domain != I915_GEM_DOMAIN_GTT)
2879 return;
2880
2881 /* No actual flushing is required for the GTT write domain. Writes
2882 * to it immediately go to main memory as far as we know, so there's
2883 * no chipset flush. It also doesn't land in render cache.
2884 */
Chris Wilson4a684a42010-10-28 14:44:08 +01002885 i915_gem_release_mmap(obj);
2886
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002887 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002888 obj->write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002889
2890 trace_i915_gem_object_change_domain(obj,
2891 obj->read_domains,
2892 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002893}
2894
2895/** Flushes the CPU write domain for the object if it's dirty. */
2896static void
2897i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj)
2898{
2899 struct drm_device *dev = obj->dev;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002900 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002901
2902 if (obj->write_domain != I915_GEM_DOMAIN_CPU)
2903 return;
2904
2905 i915_gem_clflush_object(obj);
2906 drm_agp_chipset_flush(dev);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002907 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002908 obj->write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002909
2910 trace_i915_gem_object_change_domain(obj,
2911 obj->read_domains,
2912 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002913}
2914
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002915/**
2916 * Moves a single object to the GTT read, and possibly write domain.
2917 *
2918 * This function returns when the move is complete, including waiting on
2919 * flushes to occur.
2920 */
Jesse Barnes79e53942008-11-07 14:24:08 -08002921int
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002922i915_gem_object_set_to_gtt_domain(struct drm_gem_object *obj, int write)
2923{
Daniel Vetter23010e42010-03-08 13:35:02 +01002924 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002925 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002926 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002927
Eric Anholt02354392008-11-26 13:58:13 -08002928 /* Not valid to be called on unbound objects. */
2929 if (obj_priv->gtt_space == NULL)
2930 return -EINVAL;
2931
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002932 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002933 if (ret != 0)
2934 return ret;
2935
Chris Wilson72133422010-09-13 23:56:38 +01002936 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002937
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002938 if (write) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01002939 ret = i915_gem_object_wait_rendering(obj, true);
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002940 if (ret)
2941 return ret;
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002942 }
Eric Anholte47c68e2008-11-14 13:35:19 -08002943
2944 old_write_domain = obj->write_domain;
2945 old_read_domains = obj->read_domains;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002946
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002947 /* It should now be out of any other write domains, and we can update
2948 * the domain values for our changes.
2949 */
2950 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2951 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002952 if (write) {
Chris Wilson72133422010-09-13 23:56:38 +01002953 obj->read_domains = I915_GEM_DOMAIN_GTT;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002954 obj->write_domain = I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002955 obj_priv->dirty = 1;
2956 }
2957
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002958 trace_i915_gem_object_change_domain(obj,
2959 old_read_domains,
2960 old_write_domain);
2961
Eric Anholte47c68e2008-11-14 13:35:19 -08002962 return 0;
2963}
2964
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002965/*
2966 * Prepare buffer for display plane. Use uninterruptible for possible flush
2967 * wait, as in modesetting process we're not supposed to be interrupted.
2968 */
2969int
Chris Wilson48b956c2010-09-14 12:50:34 +01002970i915_gem_object_set_to_display_plane(struct drm_gem_object *obj,
2971 bool pipelined)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002972{
Daniel Vetter23010e42010-03-08 13:35:02 +01002973 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002974 uint32_t old_read_domains;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002975 int ret;
2976
2977 /* Not valid to be called on unbound objects. */
2978 if (obj_priv->gtt_space == NULL)
2979 return -EINVAL;
2980
Chris Wilsonced270f2010-09-26 22:47:46 +01002981 ret = i915_gem_object_flush_gpu_write_domain(obj, true);
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002982 if (ret)
2983 return ret;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002984
Chris Wilsonced270f2010-09-26 22:47:46 +01002985 /* Currently, we are always called from an non-interruptible context. */
2986 if (!pipelined) {
2987 ret = i915_gem_object_wait_rendering(obj, false);
2988 if (ret)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002989 return ret;
2990 }
2991
Chris Wilsonb118c1e2010-05-27 13:18:14 +01002992 i915_gem_object_flush_cpu_write_domain(obj);
2993
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002994 old_read_domains = obj->read_domains;
Chris Wilsonc78ec302010-09-20 12:50:23 +01002995 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002996
2997 trace_i915_gem_object_change_domain(obj,
2998 old_read_domains,
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002999 obj->write_domain);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003000
3001 return 0;
3002}
3003
Eric Anholte47c68e2008-11-14 13:35:19 -08003004/**
3005 * Moves a single object to the CPU read, and possibly write domain.
3006 *
3007 * This function returns when the move is complete, including waiting on
3008 * flushes to occur.
3009 */
3010static int
3011i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj, int write)
3012{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003013 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003014 int ret;
3015
Daniel Vetterba3d8d72010-02-11 22:37:04 +01003016 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Eric Anholte47c68e2008-11-14 13:35:19 -08003017 if (ret != 0)
3018 return ret;
3019
3020 i915_gem_object_flush_gtt_write_domain(obj);
3021
3022 /* If we have a partially-valid cache of the object in the CPU,
3023 * finish invalidating it and free the per-page flags.
3024 */
3025 i915_gem_object_set_to_full_cpu_read_domain(obj);
3026
Chris Wilson72133422010-09-13 23:56:38 +01003027 if (write) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01003028 ret = i915_gem_object_wait_rendering(obj, true);
Chris Wilson72133422010-09-13 23:56:38 +01003029 if (ret)
3030 return ret;
3031 }
3032
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003033 old_write_domain = obj->write_domain;
3034 old_read_domains = obj->read_domains;
3035
Eric Anholte47c68e2008-11-14 13:35:19 -08003036 /* Flush the CPU cache if it's still invalid. */
3037 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
3038 i915_gem_clflush_object(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003039
3040 obj->read_domains |= I915_GEM_DOMAIN_CPU;
3041 }
3042
3043 /* It should now be out of any other write domains, and we can update
3044 * the domain values for our changes.
3045 */
3046 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
3047
3048 /* If we're writing through the CPU, then the GPU read domains will
3049 * need to be invalidated at next use.
3050 */
3051 if (write) {
Chris Wilsonc78ec302010-09-20 12:50:23 +01003052 obj->read_domains = I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003053 obj->write_domain = I915_GEM_DOMAIN_CPU;
3054 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003055
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003056 trace_i915_gem_object_change_domain(obj,
3057 old_read_domains,
3058 old_write_domain);
3059
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003060 return 0;
3061}
3062
Eric Anholt673a3942008-07-30 12:06:12 -07003063/*
3064 * Set the next domain for the specified object. This
3065 * may not actually perform the necessary flushing/invaliding though,
3066 * as that may want to be batched with other set_domain operations
3067 *
3068 * This is (we hope) the only really tricky part of gem. The goal
3069 * is fairly simple -- track which caches hold bits of the object
3070 * and make sure they remain coherent. A few concrete examples may
3071 * help to explain how it works. For shorthand, we use the notation
3072 * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
3073 * a pair of read and write domain masks.
3074 *
3075 * Case 1: the batch buffer
3076 *
3077 * 1. Allocated
3078 * 2. Written by CPU
3079 * 3. Mapped to GTT
3080 * 4. Read by GPU
3081 * 5. Unmapped from GTT
3082 * 6. Freed
3083 *
3084 * Let's take these a step at a time
3085 *
3086 * 1. Allocated
3087 * Pages allocated from the kernel may still have
3088 * cache contents, so we set them to (CPU, CPU) always.
3089 * 2. Written by CPU (using pwrite)
3090 * The pwrite function calls set_domain (CPU, CPU) and
3091 * this function does nothing (as nothing changes)
3092 * 3. Mapped by GTT
3093 * This function asserts that the object is not
3094 * currently in any GPU-based read or write domains
3095 * 4. Read by GPU
3096 * i915_gem_execbuffer calls set_domain (COMMAND, 0).
3097 * As write_domain is zero, this function adds in the
3098 * current read domains (CPU+COMMAND, 0).
3099 * flush_domains is set to CPU.
3100 * invalidate_domains is set to COMMAND
3101 * clflush is run to get data out of the CPU caches
3102 * then i915_dev_set_domain calls i915_gem_flush to
3103 * emit an MI_FLUSH and drm_agp_chipset_flush
3104 * 5. Unmapped from GTT
3105 * i915_gem_object_unbind calls set_domain (CPU, CPU)
3106 * flush_domains and invalidate_domains end up both zero
3107 * so no flushing/invalidating happens
3108 * 6. Freed
3109 * yay, done
3110 *
3111 * Case 2: The shared render buffer
3112 *
3113 * 1. Allocated
3114 * 2. Mapped to GTT
3115 * 3. Read/written by GPU
3116 * 4. set_domain to (CPU,CPU)
3117 * 5. Read/written by CPU
3118 * 6. Read/written by GPU
3119 *
3120 * 1. Allocated
3121 * Same as last example, (CPU, CPU)
3122 * 2. Mapped to GTT
3123 * Nothing changes (assertions find that it is not in the GPU)
3124 * 3. Read/written by GPU
3125 * execbuffer calls set_domain (RENDER, RENDER)
3126 * flush_domains gets CPU
3127 * invalidate_domains gets GPU
3128 * clflush (obj)
3129 * MI_FLUSH and drm_agp_chipset_flush
3130 * 4. set_domain (CPU, CPU)
3131 * flush_domains gets GPU
3132 * invalidate_domains gets CPU
3133 * wait_rendering (obj) to make sure all drawing is complete.
3134 * This will include an MI_FLUSH to get the data from GPU
3135 * to memory
3136 * clflush (obj) to invalidate the CPU cache
3137 * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
3138 * 5. Read/written by CPU
3139 * cache lines are loaded and dirtied
3140 * 6. Read written by GPU
3141 * Same as last GPU access
3142 *
3143 * Case 3: The constant buffer
3144 *
3145 * 1. Allocated
3146 * 2. Written by CPU
3147 * 3. Read by GPU
3148 * 4. Updated (written) by CPU again
3149 * 5. Read by GPU
3150 *
3151 * 1. Allocated
3152 * (CPU, CPU)
3153 * 2. Written by CPU
3154 * (CPU, CPU)
3155 * 3. Read by GPU
3156 * (CPU+RENDER, 0)
3157 * flush_domains = CPU
3158 * invalidate_domains = RENDER
3159 * clflush (obj)
3160 * MI_FLUSH
3161 * drm_agp_chipset_flush
3162 * 4. Updated (written) by CPU again
3163 * (CPU, CPU)
3164 * flush_domains = 0 (no previous write domain)
3165 * invalidate_domains = 0 (no new read domains)
3166 * 5. Read by GPU
3167 * (CPU+RENDER, 0)
3168 * flush_domains = CPU
3169 * invalidate_domains = RENDER
3170 * clflush (obj)
3171 * MI_FLUSH
3172 * drm_agp_chipset_flush
3173 */
Keith Packardc0d90822008-11-20 23:11:08 -08003174static void
Chris Wilsonb6651452010-10-23 10:15:06 +01003175i915_gem_object_set_to_gpu_domain(struct drm_gem_object *obj,
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003176 struct intel_ring_buffer *ring,
3177 struct change_domains *cd)
Eric Anholt673a3942008-07-30 12:06:12 -07003178{
Daniel Vetter23010e42010-03-08 13:35:02 +01003179 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003180 uint32_t invalidate_domains = 0;
3181 uint32_t flush_domains = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07003182
Eric Anholt673a3942008-07-30 12:06:12 -07003183 /*
3184 * If the object isn't moving to a new write domain,
3185 * let the object stay in multiple read domains
3186 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003187 if (obj->pending_write_domain == 0)
3188 obj->pending_read_domains |= obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07003189
3190 /*
3191 * Flush the current write domain if
3192 * the new read domains don't match. Invalidate
3193 * any read domains which differ from the old
3194 * write domain
3195 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003196 if (obj->write_domain &&
Chris Wilson13b29282010-11-01 12:22:48 +00003197 (obj->write_domain != obj->pending_read_domains ||
3198 obj_priv->ring != ring)) {
Eric Anholt673a3942008-07-30 12:06:12 -07003199 flush_domains |= obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08003200 invalidate_domains |=
3201 obj->pending_read_domains & ~obj->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07003202 }
3203 /*
3204 * Invalidate any read caches which may have
3205 * stale data. That is, any new read domains.
3206 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003207 invalidate_domains |= obj->pending_read_domains & ~obj->read_domains;
Chris Wilson3d2a8122010-09-29 11:39:53 +01003208 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU)
Eric Anholt673a3942008-07-30 12:06:12 -07003209 i915_gem_clflush_object(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003210
Chris Wilson4a684a42010-10-28 14:44:08 +01003211 /* blow away mappings if mapped through GTT */
3212 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_GTT)
3213 i915_gem_release_mmap(obj);
3214
Eric Anholtefbeed92009-02-19 14:54:51 -08003215 /* The actual obj->write_domain will be updated with
3216 * pending_write_domain after we emit the accumulated flush for all
3217 * of our domain changes in execbuffers (which clears objects'
3218 * write_domains). So if we have a current write domain that we
3219 * aren't changing, set pending_write_domain to that.
3220 */
3221 if (flush_domains == 0 && obj->pending_write_domain == 0)
3222 obj->pending_write_domain = obj->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07003223
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003224 cd->invalidate_domains |= invalidate_domains;
3225 cd->flush_domains |= flush_domains;
Chris Wilsonb6651452010-10-23 10:15:06 +01003226 if (flush_domains & I915_GEM_GPU_DOMAINS)
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003227 cd->flush_rings |= obj_priv->ring->id;
Chris Wilsonb6651452010-10-23 10:15:06 +01003228 if (invalidate_domains & I915_GEM_GPU_DOMAINS)
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003229 cd->flush_rings |= ring->id;
Eric Anholt673a3942008-07-30 12:06:12 -07003230}
3231
3232/**
Eric Anholte47c68e2008-11-14 13:35:19 -08003233 * Moves the object from a partially CPU read to a full one.
Eric Anholt673a3942008-07-30 12:06:12 -07003234 *
Eric Anholte47c68e2008-11-14 13:35:19 -08003235 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
3236 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
3237 */
3238static void
3239i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj)
3240{
Daniel Vetter23010e42010-03-08 13:35:02 +01003241 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003242
3243 if (!obj_priv->page_cpu_valid)
3244 return;
3245
3246 /* If we're partially in the CPU read domain, finish moving it in.
3247 */
3248 if (obj->read_domains & I915_GEM_DOMAIN_CPU) {
3249 int i;
3250
3251 for (i = 0; i <= (obj->size - 1) / PAGE_SIZE; i++) {
3252 if (obj_priv->page_cpu_valid[i])
3253 continue;
Eric Anholt856fa192009-03-19 14:10:50 -07003254 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholte47c68e2008-11-14 13:35:19 -08003255 }
Eric Anholte47c68e2008-11-14 13:35:19 -08003256 }
3257
3258 /* Free the page_cpu_valid mappings which are now stale, whether
3259 * or not we've got I915_GEM_DOMAIN_CPU.
3260 */
Eric Anholt9a298b22009-03-24 12:23:04 -07003261 kfree(obj_priv->page_cpu_valid);
Eric Anholte47c68e2008-11-14 13:35:19 -08003262 obj_priv->page_cpu_valid = NULL;
3263}
3264
3265/**
3266 * Set the CPU read domain on a range of the object.
3267 *
3268 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
3269 * not entirely valid. The page_cpu_valid member of the object flags which
3270 * pages have been flushed, and will be respected by
3271 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
3272 * of the whole object.
3273 *
3274 * This function returns when the move is complete, including waiting on
3275 * flushes to occur.
Eric Anholt673a3942008-07-30 12:06:12 -07003276 */
3277static int
Eric Anholte47c68e2008-11-14 13:35:19 -08003278i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
3279 uint64_t offset, uint64_t size)
Eric Anholt673a3942008-07-30 12:06:12 -07003280{
Daniel Vetter23010e42010-03-08 13:35:02 +01003281 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003282 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003283 int i, ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003284
Eric Anholte47c68e2008-11-14 13:35:19 -08003285 if (offset == 0 && size == obj->size)
3286 return i915_gem_object_set_to_cpu_domain(obj, 0);
3287
Daniel Vetterba3d8d72010-02-11 22:37:04 +01003288 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Eric Anholte47c68e2008-11-14 13:35:19 -08003289 if (ret != 0)
3290 return ret;
3291 i915_gem_object_flush_gtt_write_domain(obj);
3292
3293 /* If we're already fully in the CPU read domain, we're done. */
3294 if (obj_priv->page_cpu_valid == NULL &&
3295 (obj->read_domains & I915_GEM_DOMAIN_CPU) != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07003296 return 0;
3297
Eric Anholte47c68e2008-11-14 13:35:19 -08003298 /* Otherwise, create/clear the per-page CPU read domain flag if we're
3299 * newly adding I915_GEM_DOMAIN_CPU
3300 */
Eric Anholt673a3942008-07-30 12:06:12 -07003301 if (obj_priv->page_cpu_valid == NULL) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003302 obj_priv->page_cpu_valid = kzalloc(obj->size / PAGE_SIZE,
3303 GFP_KERNEL);
Eric Anholte47c68e2008-11-14 13:35:19 -08003304 if (obj_priv->page_cpu_valid == NULL)
3305 return -ENOMEM;
3306 } else if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0)
3307 memset(obj_priv->page_cpu_valid, 0, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07003308
3309 /* Flush the cache on any pages that are still invalid from the CPU's
3310 * perspective.
3311 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003312 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
3313 i++) {
Eric Anholt673a3942008-07-30 12:06:12 -07003314 if (obj_priv->page_cpu_valid[i])
3315 continue;
3316
Eric Anholt856fa192009-03-19 14:10:50 -07003317 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07003318
3319 obj_priv->page_cpu_valid[i] = 1;
3320 }
3321
Eric Anholte47c68e2008-11-14 13:35:19 -08003322 /* It should now be out of any other write domains, and we can update
3323 * the domain values for our changes.
3324 */
3325 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
3326
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003327 old_read_domains = obj->read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003328 obj->read_domains |= I915_GEM_DOMAIN_CPU;
3329
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003330 trace_i915_gem_object_change_domain(obj,
3331 old_read_domains,
3332 obj->write_domain);
3333
Eric Anholt673a3942008-07-30 12:06:12 -07003334 return 0;
3335}
3336
3337/**
Eric Anholt673a3942008-07-30 12:06:12 -07003338 * Pin an object to the GTT and evaluate the relocations landing in it.
3339 */
3340static int
Chris Wilson9af90d12010-10-17 10:01:56 +01003341i915_gem_execbuffer_relocate(struct drm_i915_gem_object *obj,
3342 struct drm_file *file_priv,
3343 struct drm_i915_gem_exec_object2 *entry)
Eric Anholt673a3942008-07-30 12:06:12 -07003344{
Chris Wilson9af90d12010-10-17 10:01:56 +01003345 struct drm_device *dev = obj->base.dev;
Keith Packard0839ccb2008-10-30 19:38:48 -07003346 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson2549d6c2010-10-14 12:10:41 +01003347 struct drm_i915_gem_relocation_entry __user *user_relocs;
Chris Wilson9af90d12010-10-17 10:01:56 +01003348 struct drm_gem_object *target_obj = NULL;
3349 uint32_t target_handle = 0;
3350 int i, ret = 0;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003351
Chris Wilson2549d6c2010-10-14 12:10:41 +01003352 user_relocs = (void __user *)(uintptr_t)entry->relocs_ptr;
Eric Anholt673a3942008-07-30 12:06:12 -07003353 for (i = 0; i < entry->relocation_count; i++) {
Chris Wilson2549d6c2010-10-14 12:10:41 +01003354 struct drm_i915_gem_relocation_entry reloc;
Chris Wilson9af90d12010-10-17 10:01:56 +01003355 uint32_t target_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003356
Chris Wilson9af90d12010-10-17 10:01:56 +01003357 if (__copy_from_user_inatomic(&reloc,
3358 user_relocs+i,
3359 sizeof(reloc))) {
3360 ret = -EFAULT;
3361 break;
Eric Anholt673a3942008-07-30 12:06:12 -07003362 }
Chris Wilson2549d6c2010-10-14 12:10:41 +01003363
Chris Wilson9af90d12010-10-17 10:01:56 +01003364 if (reloc.target_handle != target_handle) {
3365 drm_gem_object_unreference(target_obj);
3366
3367 target_obj = drm_gem_object_lookup(dev, file_priv,
3368 reloc.target_handle);
3369 if (target_obj == NULL) {
3370 ret = -ENOENT;
3371 break;
3372 }
3373
3374 target_handle = reloc.target_handle;
Eric Anholt673a3942008-07-30 12:06:12 -07003375 }
Chris Wilson9af90d12010-10-17 10:01:56 +01003376 target_offset = to_intel_bo(target_obj)->gtt_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003377
Chris Wilson8542a0b2009-09-09 21:15:15 +01003378#if WATCH_RELOC
3379 DRM_INFO("%s: obj %p offset %08x target %d "
3380 "read %08x write %08x gtt %08x "
3381 "presumed %08x delta %08x\n",
3382 __func__,
3383 obj,
Chris Wilson2549d6c2010-10-14 12:10:41 +01003384 (int) reloc.offset,
3385 (int) reloc.target_handle,
3386 (int) reloc.read_domains,
3387 (int) reloc.write_domain,
Chris Wilson9af90d12010-10-17 10:01:56 +01003388 (int) target_offset,
Chris Wilson2549d6c2010-10-14 12:10:41 +01003389 (int) reloc.presumed_offset,
3390 reloc.delta);
Chris Wilson8542a0b2009-09-09 21:15:15 +01003391#endif
3392
Eric Anholt673a3942008-07-30 12:06:12 -07003393 /* The target buffer should have appeared before us in the
3394 * exec_object list, so it should have a GTT space bound by now.
3395 */
Chris Wilson9af90d12010-10-17 10:01:56 +01003396 if (target_offset == 0) {
Eric Anholt673a3942008-07-30 12:06:12 -07003397 DRM_ERROR("No GTT space found for object %d\n",
Chris Wilson2549d6c2010-10-14 12:10:41 +01003398 reloc.target_handle);
Chris Wilson9af90d12010-10-17 10:01:56 +01003399 ret = -EINVAL;
3400 break;
Eric Anholt673a3942008-07-30 12:06:12 -07003401 }
3402
Chris Wilson8542a0b2009-09-09 21:15:15 +01003403 /* Validate that the target is in a valid r/w GPU domain */
Chris Wilson2549d6c2010-10-14 12:10:41 +01003404 if (reloc.write_domain & (reloc.write_domain - 1)) {
Daniel Vetter16edd552010-02-19 11:52:02 +01003405 DRM_ERROR("reloc with multiple write domains: "
3406 "obj %p target %d offset %d "
3407 "read %08x write %08x",
Chris Wilson2549d6c2010-10-14 12:10:41 +01003408 obj, reloc.target_handle,
3409 (int) reloc.offset,
3410 reloc.read_domains,
3411 reloc.write_domain);
Chris Wilson9af90d12010-10-17 10:01:56 +01003412 ret = -EINVAL;
3413 break;
Daniel Vetter16edd552010-02-19 11:52:02 +01003414 }
Chris Wilson2549d6c2010-10-14 12:10:41 +01003415 if (reloc.write_domain & I915_GEM_DOMAIN_CPU ||
3416 reloc.read_domains & I915_GEM_DOMAIN_CPU) {
Chris Wilson8542a0b2009-09-09 21:15:15 +01003417 DRM_ERROR("reloc with read/write CPU domains: "
3418 "obj %p target %d offset %d "
3419 "read %08x write %08x",
Chris Wilson2549d6c2010-10-14 12:10:41 +01003420 obj, reloc.target_handle,
3421 (int) reloc.offset,
3422 reloc.read_domains,
3423 reloc.write_domain);
Chris Wilson9af90d12010-10-17 10:01:56 +01003424 ret = -EINVAL;
3425 break;
Chris Wilson8542a0b2009-09-09 21:15:15 +01003426 }
Chris Wilson2549d6c2010-10-14 12:10:41 +01003427 if (reloc.write_domain && target_obj->pending_write_domain &&
3428 reloc.write_domain != target_obj->pending_write_domain) {
Chris Wilson8542a0b2009-09-09 21:15:15 +01003429 DRM_ERROR("Write domain conflict: "
3430 "obj %p target %d offset %d "
3431 "new %08x old %08x\n",
Chris Wilson2549d6c2010-10-14 12:10:41 +01003432 obj, reloc.target_handle,
3433 (int) reloc.offset,
3434 reloc.write_domain,
Chris Wilson8542a0b2009-09-09 21:15:15 +01003435 target_obj->pending_write_domain);
Chris Wilson9af90d12010-10-17 10:01:56 +01003436 ret = -EINVAL;
3437 break;
Chris Wilson8542a0b2009-09-09 21:15:15 +01003438 }
3439
Chris Wilson2549d6c2010-10-14 12:10:41 +01003440 target_obj->pending_read_domains |= reloc.read_domains;
Chris Wilson878a3c32010-10-22 10:48:12 +01003441 target_obj->pending_write_domain |= reloc.write_domain;
Chris Wilson8542a0b2009-09-09 21:15:15 +01003442
3443 /* If the relocation already has the right value in it, no
3444 * more work needs to be done.
3445 */
Chris Wilson9af90d12010-10-17 10:01:56 +01003446 if (target_offset == reloc.presumed_offset)
Chris Wilson8542a0b2009-09-09 21:15:15 +01003447 continue;
Chris Wilson8542a0b2009-09-09 21:15:15 +01003448
3449 /* Check that the relocation address is valid... */
Chris Wilson9af90d12010-10-17 10:01:56 +01003450 if (reloc.offset > obj->base.size - 4) {
Eric Anholt673a3942008-07-30 12:06:12 -07003451 DRM_ERROR("Relocation beyond object bounds: "
3452 "obj %p target %d offset %d size %d.\n",
Chris Wilson2549d6c2010-10-14 12:10:41 +01003453 obj, reloc.target_handle,
Chris Wilson9af90d12010-10-17 10:01:56 +01003454 (int) reloc.offset, (int) obj->base.size);
3455 ret = -EINVAL;
3456 break;
Eric Anholt673a3942008-07-30 12:06:12 -07003457 }
Chris Wilson2549d6c2010-10-14 12:10:41 +01003458 if (reloc.offset & 3) {
Eric Anholt673a3942008-07-30 12:06:12 -07003459 DRM_ERROR("Relocation not 4-byte aligned: "
3460 "obj %p target %d offset %d.\n",
Chris Wilson2549d6c2010-10-14 12:10:41 +01003461 obj, reloc.target_handle,
3462 (int) reloc.offset);
Chris Wilson9af90d12010-10-17 10:01:56 +01003463 ret = -EINVAL;
3464 break;
Eric Anholt673a3942008-07-30 12:06:12 -07003465 }
3466
Chris Wilson8542a0b2009-09-09 21:15:15 +01003467 /* and points to somewhere within the target object. */
Chris Wilson2549d6c2010-10-14 12:10:41 +01003468 if (reloc.delta >= target_obj->size) {
Chris Wilsoncd0b9fb2009-09-15 23:23:18 +01003469 DRM_ERROR("Relocation beyond target object bounds: "
3470 "obj %p target %d delta %d size %d.\n",
Chris Wilson2549d6c2010-10-14 12:10:41 +01003471 obj, reloc.target_handle,
3472 (int) reloc.delta, (int) target_obj->size);
Chris Wilson9af90d12010-10-17 10:01:56 +01003473 ret = -EINVAL;
3474 break;
Eric Anholte47c68e2008-11-14 13:35:19 -08003475 }
3476
Chris Wilson9af90d12010-10-17 10:01:56 +01003477 reloc.delta += target_offset;
3478 if (obj->base.write_domain == I915_GEM_DOMAIN_CPU) {
Chris Wilsonf0c43d92010-10-14 12:44:48 +01003479 uint32_t page_offset = reloc.offset & ~PAGE_MASK;
3480 char *vaddr;
3481
Linus Torvaldsc48c43e2010-10-26 18:57:59 -07003482 vaddr = kmap_atomic(obj->pages[reloc.offset >> PAGE_SHIFT]);
Chris Wilsonf0c43d92010-10-14 12:44:48 +01003483 *(uint32_t *)(vaddr + page_offset) = reloc.delta;
Linus Torvaldsc48c43e2010-10-26 18:57:59 -07003484 kunmap_atomic(vaddr);
Chris Wilsonf0c43d92010-10-14 12:44:48 +01003485 } else {
3486 uint32_t __iomem *reloc_entry;
3487 void __iomem *reloc_page;
Chris Wilsonf0c43d92010-10-14 12:44:48 +01003488
Chris Wilson9af90d12010-10-17 10:01:56 +01003489 ret = i915_gem_object_set_to_gtt_domain(&obj->base, 1);
3490 if (ret)
3491 break;
Chris Wilsonf0c43d92010-10-14 12:44:48 +01003492
3493 /* Map the page containing the relocation we're going to perform. */
Chris Wilson9af90d12010-10-17 10:01:56 +01003494 reloc.offset += obj->gtt_offset;
Chris Wilsonf0c43d92010-10-14 12:44:48 +01003495 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
Linus Torvaldsc48c43e2010-10-26 18:57:59 -07003496 reloc.offset & PAGE_MASK);
Chris Wilsonf0c43d92010-10-14 12:44:48 +01003497 reloc_entry = (uint32_t __iomem *)
3498 (reloc_page + (reloc.offset & ~PAGE_MASK));
3499 iowrite32(reloc.delta, reloc_entry);
Linus Torvaldsc48c43e2010-10-26 18:57:59 -07003500 io_mapping_unmap_atomic(reloc_page);
Eric Anholt673a3942008-07-30 12:06:12 -07003501 }
3502
Chris Wilsonb5dc6082010-10-20 20:59:57 +01003503 /* and update the user's relocation entry */
3504 reloc.presumed_offset = target_offset;
3505 if (__copy_to_user_inatomic(&user_relocs[i].presumed_offset,
3506 &reloc.presumed_offset,
3507 sizeof(reloc.presumed_offset))) {
3508 ret = -EFAULT;
3509 break;
3510 }
Eric Anholt673a3942008-07-30 12:06:12 -07003511 }
3512
Chris Wilson9af90d12010-10-17 10:01:56 +01003513 drm_gem_object_unreference(target_obj);
3514 return ret;
3515}
3516
3517static int
3518i915_gem_execbuffer_pin(struct drm_device *dev,
3519 struct drm_file *file,
3520 struct drm_gem_object **object_list,
3521 struct drm_i915_gem_exec_object2 *exec_list,
3522 int count)
3523{
3524 struct drm_i915_private *dev_priv = dev->dev_private;
3525 int ret, i, retry;
3526
3527 /* attempt to pin all of the buffers into the GTT */
Chris Wilson5eac3ab2010-10-31 08:49:47 +00003528 retry = 0;
3529 do {
Chris Wilson9af90d12010-10-17 10:01:56 +01003530 ret = 0;
3531 for (i = 0; i < count; i++) {
3532 struct drm_i915_gem_exec_object2 *entry = &exec_list[i];
Daniel Vetter16e809a2010-09-16 19:37:04 +02003533 struct drm_i915_gem_object *obj = to_intel_bo(object_list[i]);
Chris Wilson9af90d12010-10-17 10:01:56 +01003534 bool need_fence =
3535 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
3536 obj->tiling_mode != I915_TILING_NONE;
3537
Daniel Vetter16e809a2010-09-16 19:37:04 +02003538 /* g33/pnv can't fence buffers in the unmappable part */
3539 bool need_mappable =
3540 entry->relocation_count ? true : need_fence;
3541
Chris Wilson9af90d12010-10-17 10:01:56 +01003542 /* Check fence reg constraints and rebind if necessary */
Chris Wilsona00b10c2010-09-24 21:15:47 +01003543 if ((need_fence && !obj->fenceable) ||
3544 (need_mappable && !obj->mappable)) {
Chris Wilson9af90d12010-10-17 10:01:56 +01003545 ret = i915_gem_object_unbind(&obj->base);
3546 if (ret)
3547 break;
3548 }
3549
Daniel Vetter920afa72010-09-16 17:54:23 +02003550 ret = i915_gem_object_pin(&obj->base,
Daniel Vetter16e809a2010-09-16 19:37:04 +02003551 entry->alignment,
Chris Wilsona00b10c2010-09-24 21:15:47 +01003552 need_mappable,
3553 need_fence);
Chris Wilson9af90d12010-10-17 10:01:56 +01003554 if (ret)
3555 break;
3556
3557 /*
3558 * Pre-965 chips need a fence register set up in order
3559 * to properly handle blits to/from tiled surfaces.
3560 */
3561 if (need_fence) {
3562 ret = i915_gem_object_get_fence_reg(&obj->base, true);
3563 if (ret) {
3564 i915_gem_object_unpin(&obj->base);
3565 break;
3566 }
3567
3568 dev_priv->fence_regs[obj->fence_reg].gpu = true;
3569 }
3570
3571 entry->offset = obj->gtt_offset;
3572 }
3573
3574 while (i--)
3575 i915_gem_object_unpin(object_list[i]);
3576
Chris Wilson5eac3ab2010-10-31 08:49:47 +00003577 if (ret != -ENOSPC || retry > 1)
Chris Wilson9af90d12010-10-17 10:01:56 +01003578 return ret;
3579
Chris Wilson5eac3ab2010-10-31 08:49:47 +00003580 /* First attempt, just clear anything that is purgeable.
3581 * Second attempt, clear the entire GTT.
3582 */
3583 ret = i915_gem_evict_everything(dev, retry == 0);
Chris Wilson9af90d12010-10-17 10:01:56 +01003584 if (ret)
3585 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003586
Chris Wilson5eac3ab2010-10-31 08:49:47 +00003587 retry++;
3588 } while (1);
Eric Anholt673a3942008-07-30 12:06:12 -07003589}
3590
Chris Wilson13b29282010-11-01 12:22:48 +00003591static int
3592i915_gem_execbuffer_move_to_gpu(struct drm_device *dev,
3593 struct drm_file *file,
3594 struct intel_ring_buffer *ring,
3595 struct drm_gem_object **objects,
3596 int count)
3597{
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003598 struct change_domains cd;
Chris Wilson13b29282010-11-01 12:22:48 +00003599 int ret, i;
3600
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003601 cd.invalidate_domains = 0;
3602 cd.flush_domains = 0;
3603 cd.flush_rings = 0;
Chris Wilson13b29282010-11-01 12:22:48 +00003604 for (i = 0; i < count; i++)
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003605 i915_gem_object_set_to_gpu_domain(objects[i], ring, &cd);
Chris Wilson13b29282010-11-01 12:22:48 +00003606
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003607 if (cd.invalidate_domains | cd.flush_domains) {
Chris Wilson13b29282010-11-01 12:22:48 +00003608#if WATCH_EXEC
3609 DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
3610 __func__,
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003611 cd.invalidate_domains,
3612 cd.flush_domains);
Chris Wilson13b29282010-11-01 12:22:48 +00003613#endif
3614 i915_gem_flush(dev, file,
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003615 cd.invalidate_domains,
3616 cd.flush_domains,
3617 cd.flush_rings);
Chris Wilson13b29282010-11-01 12:22:48 +00003618 }
3619
3620 for (i = 0; i < count; i++) {
3621 struct drm_i915_gem_object *obj = to_intel_bo(objects[i]);
3622 /* XXX replace with semaphores */
3623 if (obj->ring && ring != obj->ring) {
3624 ret = i915_gem_object_wait_rendering(&obj->base, true);
3625 if (ret)
3626 return ret;
3627 }
3628 }
3629
3630 return 0;
3631}
3632
Eric Anholt673a3942008-07-30 12:06:12 -07003633/* Throttle our rendering by waiting until the ring has completed our requests
3634 * emitted over 20 msec ago.
3635 *
Eric Anholtb9624422009-06-03 07:27:35 +00003636 * Note that if we were to use the current jiffies each time around the loop,
3637 * we wouldn't escape the function with any frames outstanding if the time to
3638 * render a frame was over 20ms.
3639 *
Eric Anholt673a3942008-07-30 12:06:12 -07003640 * This should get us reasonable parallelism between CPU and GPU but also
3641 * relatively low latency when blocking on a particular request to finish.
3642 */
3643static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003644i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003645{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003646 struct drm_i915_private *dev_priv = dev->dev_private;
3647 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00003648 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003649 struct drm_i915_gem_request *request;
3650 struct intel_ring_buffer *ring = NULL;
3651 u32 seqno = 0;
3652 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003653
Chris Wilson1c255952010-09-26 11:03:27 +01003654 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003655 list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
Eric Anholtb9624422009-06-03 07:27:35 +00003656 if (time_after_eq(request->emitted_jiffies, recent_enough))
3657 break;
3658
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003659 ring = request->ring;
3660 seqno = request->seqno;
Eric Anholtb9624422009-06-03 07:27:35 +00003661 }
Chris Wilson1c255952010-09-26 11:03:27 +01003662 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003663
3664 if (seqno == 0)
3665 return 0;
3666
3667 ret = 0;
Chris Wilson78501ea2010-10-27 12:18:21 +01003668 if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) {
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003669 /* And wait for the seqno passing without holding any locks and
3670 * causing extra latency for others. This is safe as the irq
3671 * generation is designed to be run atomically and so is
3672 * lockless.
3673 */
Chris Wilson78501ea2010-10-27 12:18:21 +01003674 ring->user_irq_get(ring);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003675 ret = wait_event_interruptible(ring->irq_queue,
Chris Wilson78501ea2010-10-27 12:18:21 +01003676 i915_seqno_passed(ring->get_seqno(ring), seqno)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003677 || atomic_read(&dev_priv->mm.wedged));
Chris Wilson78501ea2010-10-27 12:18:21 +01003678 ring->user_irq_put(ring);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003679
3680 if (ret == 0 && atomic_read(&dev_priv->mm.wedged))
3681 ret = -EIO;
3682 }
3683
3684 if (ret == 0)
3685 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
Eric Anholtb9624422009-06-03 07:27:35 +00003686
Eric Anholt673a3942008-07-30 12:06:12 -07003687 return ret;
3688}
3689
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003690static int
Chris Wilson2549d6c2010-10-14 12:10:41 +01003691i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec,
3692 uint64_t exec_offset)
Chris Wilson83d60792009-06-06 09:45:57 +01003693{
3694 uint32_t exec_start, exec_len;
3695
3696 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
3697 exec_len = (uint32_t) exec->batch_len;
3698
3699 if ((exec_start | exec_len) & 0x7)
3700 return -EINVAL;
3701
3702 if (!exec_start)
3703 return -EINVAL;
3704
3705 return 0;
3706}
3707
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003708static int
Chris Wilson2549d6c2010-10-14 12:10:41 +01003709validate_exec_list(struct drm_i915_gem_exec_object2 *exec,
3710 int count)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003711{
Chris Wilson2549d6c2010-10-14 12:10:41 +01003712 int i;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003713
Chris Wilson2549d6c2010-10-14 12:10:41 +01003714 for (i = 0; i < count; i++) {
3715 char __user *ptr = (char __user *)(uintptr_t)exec[i].relocs_ptr;
3716 size_t length = exec[i].relocation_count * sizeof(struct drm_i915_gem_relocation_entry);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003717
Chris Wilson2549d6c2010-10-14 12:10:41 +01003718 if (!access_ok(VERIFY_READ, ptr, length))
3719 return -EFAULT;
3720
Chris Wilsonb5dc6082010-10-20 20:59:57 +01003721 /* we may also need to update the presumed offsets */
3722 if (!access_ok(VERIFY_WRITE, ptr, length))
3723 return -EFAULT;
3724
Chris Wilson2549d6c2010-10-14 12:10:41 +01003725 if (fault_in_pages_readable(ptr, length))
3726 return -EFAULT;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003727 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003728
Chris Wilson2549d6c2010-10-14 12:10:41 +01003729 return 0;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003730}
3731
Chris Wilson2549d6c2010-10-14 12:10:41 +01003732static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003733i915_gem_do_execbuffer(struct drm_device *dev, void *data,
Chris Wilson9af90d12010-10-17 10:01:56 +01003734 struct drm_file *file,
Jesse Barnes76446ca2009-12-17 22:05:42 -05003735 struct drm_i915_gem_execbuffer2 *args,
3736 struct drm_i915_gem_exec_object2 *exec_list)
Eric Anholt673a3942008-07-30 12:06:12 -07003737{
3738 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003739 struct drm_gem_object **object_list = NULL;
3740 struct drm_gem_object *batch_obj;
Eric Anholt201361a2009-03-11 12:30:04 -07003741 struct drm_clip_rect *cliprects = NULL;
Chris Wilson8dc5d142010-08-12 12:36:12 +01003742 struct drm_i915_gem_request *request = NULL;
Chris Wilson9af90d12010-10-17 10:01:56 +01003743 int ret, i, flips;
Eric Anholt673a3942008-07-30 12:06:12 -07003744 uint64_t exec_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003745
Zou Nan hai852835f2010-05-21 09:08:56 +08003746 struct intel_ring_buffer *ring = NULL;
3747
Chris Wilson30dbf0c2010-09-25 10:19:17 +01003748 ret = i915_gem_check_is_wedged(dev);
3749 if (ret)
3750 return ret;
3751
Chris Wilson2549d6c2010-10-14 12:10:41 +01003752 ret = validate_exec_list(exec_list, args->buffer_count);
3753 if (ret)
3754 return ret;
3755
Eric Anholt673a3942008-07-30 12:06:12 -07003756#if WATCH_EXEC
3757 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3758 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3759#endif
Chris Wilson549f7362010-10-19 11:19:32 +01003760 switch (args->flags & I915_EXEC_RING_MASK) {
3761 case I915_EXEC_DEFAULT:
3762 case I915_EXEC_RENDER:
3763 ring = &dev_priv->render_ring;
3764 break;
3765 case I915_EXEC_BSD:
Zou Nan haid1b851f2010-05-21 09:08:57 +08003766 if (!HAS_BSD(dev)) {
Chris Wilson549f7362010-10-19 11:19:32 +01003767 DRM_ERROR("execbuf with invalid ring (BSD)\n");
Zou Nan haid1b851f2010-05-21 09:08:57 +08003768 return -EINVAL;
3769 }
3770 ring = &dev_priv->bsd_ring;
Chris Wilson549f7362010-10-19 11:19:32 +01003771 break;
3772 case I915_EXEC_BLT:
3773 if (!HAS_BLT(dev)) {
3774 DRM_ERROR("execbuf with invalid ring (BLT)\n");
3775 return -EINVAL;
3776 }
3777 ring = &dev_priv->blt_ring;
3778 break;
3779 default:
3780 DRM_ERROR("execbuf with unknown ring: %d\n",
3781 (int)(args->flags & I915_EXEC_RING_MASK));
3782 return -EINVAL;
Zou Nan haid1b851f2010-05-21 09:08:57 +08003783 }
3784
Eric Anholt4f481ed2008-09-10 14:22:49 -07003785 if (args->buffer_count < 1) {
3786 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3787 return -EINVAL;
3788 }
Eric Anholtc8e0f932009-11-22 03:49:37 +01003789 object_list = drm_malloc_ab(sizeof(*object_list), args->buffer_count);
Jesse Barnes76446ca2009-12-17 22:05:42 -05003790 if (object_list == NULL) {
3791 DRM_ERROR("Failed to allocate object list for %d buffers\n",
Eric Anholt673a3942008-07-30 12:06:12 -07003792 args->buffer_count);
3793 ret = -ENOMEM;
3794 goto pre_mutex_err;
3795 }
Eric Anholt673a3942008-07-30 12:06:12 -07003796
Eric Anholt201361a2009-03-11 12:30:04 -07003797 if (args->num_cliprects != 0) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003798 cliprects = kcalloc(args->num_cliprects, sizeof(*cliprects),
3799 GFP_KERNEL);
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003800 if (cliprects == NULL) {
3801 ret = -ENOMEM;
Eric Anholt201361a2009-03-11 12:30:04 -07003802 goto pre_mutex_err;
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003803 }
Eric Anholt201361a2009-03-11 12:30:04 -07003804
3805 ret = copy_from_user(cliprects,
3806 (struct drm_clip_rect __user *)
3807 (uintptr_t) args->cliprects_ptr,
3808 sizeof(*cliprects) * args->num_cliprects);
3809 if (ret != 0) {
3810 DRM_ERROR("copy %d cliprects failed: %d\n",
3811 args->num_cliprects, ret);
Dan Carpenterc877cdce2010-06-23 19:03:01 +02003812 ret = -EFAULT;
Eric Anholt201361a2009-03-11 12:30:04 -07003813 goto pre_mutex_err;
3814 }
3815 }
3816
Chris Wilson8dc5d142010-08-12 12:36:12 +01003817 request = kzalloc(sizeof(*request), GFP_KERNEL);
3818 if (request == NULL) {
3819 ret = -ENOMEM;
Chris Wilsona198bc82009-02-06 16:55:20 +00003820 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003821 }
3822
Chris Wilson76c1dec2010-09-25 11:22:51 +01003823 ret = i915_mutex_lock_interruptible(dev);
3824 if (ret)
3825 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003826
Eric Anholt673a3942008-07-30 12:06:12 -07003827 if (dev_priv->mm.suspended) {
Eric Anholt673a3942008-07-30 12:06:12 -07003828 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003829 ret = -EBUSY;
3830 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003831 }
3832
Keith Packardac94a962008-11-20 23:30:27 -08003833 /* Look up object handles */
Eric Anholt673a3942008-07-30 12:06:12 -07003834 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson7e318e12010-10-27 13:43:39 +01003835 struct drm_i915_gem_object *obj_priv;
3836
Chris Wilson9af90d12010-10-17 10:01:56 +01003837 object_list[i] = drm_gem_object_lookup(dev, file,
Eric Anholt673a3942008-07-30 12:06:12 -07003838 exec_list[i].handle);
3839 if (object_list[i] == NULL) {
3840 DRM_ERROR("Invalid object handle %d at index %d\n",
3841 exec_list[i].handle, i);
Chris Wilson0ce907f2010-01-23 20:26:35 +00003842 /* prevent error path from reading uninitialized data */
3843 args->buffer_count = i + 1;
Chris Wilsonbf79cb92010-08-04 14:19:46 +01003844 ret = -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07003845 goto err;
3846 }
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003847
Daniel Vetter23010e42010-03-08 13:35:02 +01003848 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003849 if (obj_priv->in_execbuffer) {
3850 DRM_ERROR("Object %p appears more than once in object list\n",
3851 object_list[i]);
Chris Wilson0ce907f2010-01-23 20:26:35 +00003852 /* prevent error path from reading uninitialized data */
3853 args->buffer_count = i + 1;
Chris Wilsonbf79cb92010-08-04 14:19:46 +01003854 ret = -EINVAL;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003855 goto err;
3856 }
3857 obj_priv->in_execbuffer = true;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003858 }
3859
Chris Wilson9af90d12010-10-17 10:01:56 +01003860 /* Move the objects en-masse into the GTT, evicting if necessary. */
3861 ret = i915_gem_execbuffer_pin(dev, file,
3862 object_list, exec_list,
3863 args->buffer_count);
3864 if (ret)
3865 goto err;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003866
Chris Wilson9af90d12010-10-17 10:01:56 +01003867 /* The objects are in their final locations, apply the relocations. */
3868 for (i = 0; i < args->buffer_count; i++) {
3869 struct drm_i915_gem_object *obj = to_intel_bo(object_list[i]);
3870 obj->base.pending_read_domains = 0;
3871 obj->base.pending_write_domain = 0;
3872 ret = i915_gem_execbuffer_relocate(obj, file, &exec_list[i]);
Eric Anholt673a3942008-07-30 12:06:12 -07003873 if (ret)
3874 goto err;
3875 }
3876
Eric Anholt673a3942008-07-30 12:06:12 -07003877 /* Set the pending read domains for the batch buffer to COMMAND */
3878 batch_obj = object_list[args->buffer_count-1];
Chris Wilson5f26a2c2009-06-06 09:45:58 +01003879 if (batch_obj->pending_write_domain) {
3880 DRM_ERROR("Attempting to use self-modifying batch buffer\n");
3881 ret = -EINVAL;
3882 goto err;
3883 }
3884 batch_obj->pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
Eric Anholt673a3942008-07-30 12:06:12 -07003885
Chris Wilson9af90d12010-10-17 10:01:56 +01003886 /* Sanity check the batch buffer */
3887 exec_offset = to_intel_bo(batch_obj)->gtt_offset;
3888 ret = i915_gem_check_execbuffer(args, exec_offset);
Chris Wilson83d60792009-06-06 09:45:57 +01003889 if (ret != 0) {
3890 DRM_ERROR("execbuf with invalid offset/length\n");
3891 goto err;
3892 }
3893
Chris Wilson13b29282010-11-01 12:22:48 +00003894 ret = i915_gem_execbuffer_move_to_gpu(dev, file, ring,
3895 object_list, args->buffer_count);
3896 if (ret)
3897 goto err;
Eric Anholt673a3942008-07-30 12:06:12 -07003898
Eric Anholt673a3942008-07-30 12:06:12 -07003899#if WATCH_COHERENCY
3900 for (i = 0; i < args->buffer_count; i++) {
3901 i915_gem_object_check_coherency(object_list[i],
3902 exec_list[i].handle);
3903 }
3904#endif
3905
Eric Anholt673a3942008-07-30 12:06:12 -07003906#if WATCH_EXEC
Ben Gamari6911a9b2009-04-02 11:24:54 -07003907 i915_gem_dump_object(batch_obj,
Eric Anholt673a3942008-07-30 12:06:12 -07003908 args->batch_len,
3909 __func__,
3910 ~0);
3911#endif
3912
Chris Wilsone59f2ba2010-10-07 17:28:15 +01003913 /* Check for any pending flips. As we only maintain a flip queue depth
3914 * of 1, we can simply insert a WAIT for the next display flip prior
3915 * to executing the batch and avoid stalling the CPU.
3916 */
3917 flips = 0;
3918 for (i = 0; i < args->buffer_count; i++) {
3919 if (object_list[i]->write_domain)
3920 flips |= atomic_read(&to_intel_bo(object_list[i])->pending_flip);
3921 }
3922 if (flips) {
3923 int plane, flip_mask;
3924
3925 for (plane = 0; flips >> plane; plane++) {
3926 if (((flips >> plane) & 1) == 0)
3927 continue;
3928
3929 if (plane)
3930 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
3931 else
3932 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
3933
Chris Wilsone1f99ce2010-10-27 12:45:26 +01003934 ret = intel_ring_begin(ring, 2);
3935 if (ret)
3936 goto err;
3937
Chris Wilson78501ea2010-10-27 12:18:21 +01003938 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
3939 intel_ring_emit(ring, MI_NOOP);
3940 intel_ring_advance(ring);
Chris Wilsone59f2ba2010-10-07 17:28:15 +01003941 }
3942 }
3943
Eric Anholt673a3942008-07-30 12:06:12 -07003944 /* Exec the batchbuffer */
Chris Wilson78501ea2010-10-27 12:18:21 +01003945 ret = ring->dispatch_execbuffer(ring, args, cliprects, exec_offset);
Eric Anholt673a3942008-07-30 12:06:12 -07003946 if (ret) {
3947 DRM_ERROR("dispatch failed %d\n", ret);
3948 goto err;
3949 }
3950
Chris Wilson7e318e12010-10-27 13:43:39 +01003951 for (i = 0; i < args->buffer_count; i++) {
3952 struct drm_gem_object *obj = object_list[i];
3953
3954 obj->read_domains = obj->pending_read_domains;
3955 obj->write_domain = obj->pending_write_domain;
3956
3957 i915_gem_object_move_to_active(obj, ring);
3958 if (obj->write_domain) {
3959 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
3960 obj_priv->dirty = 1;
3961 list_move_tail(&obj_priv->gpu_write_list,
3962 &ring->gpu_write_list);
3963 intel_mark_busy(dev, obj);
3964 }
3965
3966 trace_i915_gem_object_change_domain(obj,
3967 obj->read_domains,
3968 obj->write_domain);
3969 }
3970
Eric Anholt673a3942008-07-30 12:06:12 -07003971 /*
3972 * Ensure that the commands in the batch buffer are
3973 * finished before the interrupt fires
3974 */
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01003975 i915_retire_commands(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07003976
Chris Wilson3cce4692010-10-27 16:11:02 +01003977 if (i915_add_request(dev, file, request, ring))
3978 ring->outstanding_lazy_request = true;
3979 else
3980 request = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07003981
Eric Anholt673a3942008-07-30 12:06:12 -07003982err:
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003983 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson7e318e12010-10-27 13:43:39 +01003984 if (object_list[i] == NULL)
3985 break;
3986
3987 to_intel_bo(object_list[i])->in_execbuffer = false;
Julia Lawallaad87df2008-12-21 16:28:47 +01003988 drm_gem_object_unreference(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003989 }
Julia Lawallaad87df2008-12-21 16:28:47 +01003990
Eric Anholt673a3942008-07-30 12:06:12 -07003991 mutex_unlock(&dev->struct_mutex);
3992
Chris Wilson93533c22010-01-31 10:40:48 +00003993pre_mutex_err:
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003994 drm_free_large(object_list);
Eric Anholt9a298b22009-03-24 12:23:04 -07003995 kfree(cliprects);
Chris Wilson8dc5d142010-08-12 12:36:12 +01003996 kfree(request);
Eric Anholt673a3942008-07-30 12:06:12 -07003997
3998 return ret;
3999}
4000
Jesse Barnes76446ca2009-12-17 22:05:42 -05004001/*
4002 * Legacy execbuffer just creates an exec2 list from the original exec object
4003 * list array and passes it to the real function.
4004 */
4005int
4006i915_gem_execbuffer(struct drm_device *dev, void *data,
4007 struct drm_file *file_priv)
4008{
4009 struct drm_i915_gem_execbuffer *args = data;
4010 struct drm_i915_gem_execbuffer2 exec2;
4011 struct drm_i915_gem_exec_object *exec_list = NULL;
4012 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
4013 int ret, i;
4014
4015#if WATCH_EXEC
4016 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
4017 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
4018#endif
4019
4020 if (args->buffer_count < 1) {
4021 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
4022 return -EINVAL;
4023 }
4024
4025 /* Copy in the exec list from userland */
4026 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
4027 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
4028 if (exec_list == NULL || exec2_list == NULL) {
4029 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
4030 args->buffer_count);
4031 drm_free_large(exec_list);
4032 drm_free_large(exec2_list);
4033 return -ENOMEM;
4034 }
4035 ret = copy_from_user(exec_list,
4036 (struct drm_i915_relocation_entry __user *)
4037 (uintptr_t) args->buffers_ptr,
4038 sizeof(*exec_list) * args->buffer_count);
4039 if (ret != 0) {
4040 DRM_ERROR("copy %d exec entries failed %d\n",
4041 args->buffer_count, ret);
4042 drm_free_large(exec_list);
4043 drm_free_large(exec2_list);
4044 return -EFAULT;
4045 }
4046
4047 for (i = 0; i < args->buffer_count; i++) {
4048 exec2_list[i].handle = exec_list[i].handle;
4049 exec2_list[i].relocation_count = exec_list[i].relocation_count;
4050 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
4051 exec2_list[i].alignment = exec_list[i].alignment;
4052 exec2_list[i].offset = exec_list[i].offset;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004053 if (INTEL_INFO(dev)->gen < 4)
Jesse Barnes76446ca2009-12-17 22:05:42 -05004054 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
4055 else
4056 exec2_list[i].flags = 0;
4057 }
4058
4059 exec2.buffers_ptr = args->buffers_ptr;
4060 exec2.buffer_count = args->buffer_count;
4061 exec2.batch_start_offset = args->batch_start_offset;
4062 exec2.batch_len = args->batch_len;
4063 exec2.DR1 = args->DR1;
4064 exec2.DR4 = args->DR4;
4065 exec2.num_cliprects = args->num_cliprects;
4066 exec2.cliprects_ptr = args->cliprects_ptr;
Zou Nan hai852835f2010-05-21 09:08:56 +08004067 exec2.flags = I915_EXEC_RENDER;
Jesse Barnes76446ca2009-12-17 22:05:42 -05004068
4069 ret = i915_gem_do_execbuffer(dev, data, file_priv, &exec2, exec2_list);
4070 if (!ret) {
4071 /* Copy the new buffer offsets back to the user's exec list. */
4072 for (i = 0; i < args->buffer_count; i++)
4073 exec_list[i].offset = exec2_list[i].offset;
4074 /* ... and back out to userspace */
4075 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
4076 (uintptr_t) args->buffers_ptr,
4077 exec_list,
4078 sizeof(*exec_list) * args->buffer_count);
4079 if (ret) {
4080 ret = -EFAULT;
4081 DRM_ERROR("failed to copy %d exec entries "
4082 "back to user (%d)\n",
4083 args->buffer_count, ret);
4084 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05004085 }
4086
4087 drm_free_large(exec_list);
4088 drm_free_large(exec2_list);
4089 return ret;
4090}
4091
4092int
4093i915_gem_execbuffer2(struct drm_device *dev, void *data,
4094 struct drm_file *file_priv)
4095{
4096 struct drm_i915_gem_execbuffer2 *args = data;
4097 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
4098 int ret;
4099
4100#if WATCH_EXEC
4101 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
4102 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
4103#endif
4104
4105 if (args->buffer_count < 1) {
4106 DRM_ERROR("execbuf2 with %d buffers\n", args->buffer_count);
4107 return -EINVAL;
4108 }
4109
4110 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
4111 if (exec2_list == NULL) {
4112 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
4113 args->buffer_count);
4114 return -ENOMEM;
4115 }
4116 ret = copy_from_user(exec2_list,
4117 (struct drm_i915_relocation_entry __user *)
4118 (uintptr_t) args->buffers_ptr,
4119 sizeof(*exec2_list) * args->buffer_count);
4120 if (ret != 0) {
4121 DRM_ERROR("copy %d exec entries failed %d\n",
4122 args->buffer_count, ret);
4123 drm_free_large(exec2_list);
4124 return -EFAULT;
4125 }
4126
4127 ret = i915_gem_do_execbuffer(dev, data, file_priv, args, exec2_list);
4128 if (!ret) {
4129 /* Copy the new buffer offsets back to the user's exec list. */
4130 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
4131 (uintptr_t) args->buffers_ptr,
4132 exec2_list,
4133 sizeof(*exec2_list) * args->buffer_count);
4134 if (ret) {
4135 ret = -EFAULT;
4136 DRM_ERROR("failed to copy %d exec entries "
4137 "back to user (%d)\n",
4138 args->buffer_count, ret);
4139 }
4140 }
4141
4142 drm_free_large(exec2_list);
4143 return ret;
4144}
4145
Eric Anholt673a3942008-07-30 12:06:12 -07004146int
Daniel Vetter920afa72010-09-16 17:54:23 +02004147i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment,
Chris Wilsona00b10c2010-09-24 21:15:47 +01004148 bool mappable, bool need_fence)
Eric Anholt673a3942008-07-30 12:06:12 -07004149{
4150 struct drm_device *dev = obj->dev;
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004151 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01004152 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004153 int ret;
4154
Daniel Vetter778c3542010-05-13 11:49:44 +02004155 BUG_ON(obj_priv->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT);
Chris Wilson23bc5982010-09-29 16:10:57 +01004156 WARN_ON(i915_verify_lists(dev));
Chris Wilsonac0c6b52010-05-27 13:18:18 +01004157
4158 if (obj_priv->gtt_space != NULL) {
Chris Wilsona00b10c2010-09-24 21:15:47 +01004159 if ((alignment && obj_priv->gtt_offset & (alignment - 1)) ||
4160 (need_fence && !obj_priv->fenceable) ||
4161 (mappable && !obj_priv->mappable)) {
Chris Wilsonae7d49d2010-08-04 12:37:41 +01004162 WARN(obj_priv->pin_count,
4163 "bo is already pinned with incorrect alignment:"
Chris Wilsona00b10c2010-09-24 21:15:47 +01004164 " offset=%x, req.alignment=%x, need_fence=%d, fenceable=%d, mappable=%d, cpu_accessible=%d\n",
4165 obj_priv->gtt_offset, alignment,
4166 need_fence, obj_priv->fenceable,
4167 mappable, obj_priv->mappable);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01004168 ret = i915_gem_object_unbind(obj);
4169 if (ret)
4170 return ret;
4171 }
4172 }
4173
Eric Anholt673a3942008-07-30 12:06:12 -07004174 if (obj_priv->gtt_space == NULL) {
Chris Wilsona00b10c2010-09-24 21:15:47 +01004175 ret = i915_gem_object_bind_to_gtt(obj, alignment,
4176 mappable, need_fence);
Chris Wilson97311292009-09-21 00:22:34 +01004177 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07004178 return ret;
Chris Wilson22c344e2009-02-11 14:26:45 +00004179 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05004180
Chris Wilson74653782010-10-29 10:41:23 +01004181 if (obj_priv->pin_count++ == 0) {
Chris Wilsona00b10c2010-09-24 21:15:47 +01004182 i915_gem_info_add_pin(dev_priv, obj_priv, mappable);
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004183 if (!obj_priv->active)
Chris Wilson69dc4982010-10-19 10:36:51 +01004184 list_move_tail(&obj_priv->mm_list,
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004185 &dev_priv->mm.pinned_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004186 }
Daniel Vetterfb7d5162010-10-01 22:05:20 +02004187 BUG_ON(!obj_priv->pin_mappable && mappable);
Eric Anholt673a3942008-07-30 12:06:12 -07004188
Chris Wilson23bc5982010-09-29 16:10:57 +01004189 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07004190 return 0;
4191}
4192
4193void
4194i915_gem_object_unpin(struct drm_gem_object *obj)
4195{
4196 struct drm_device *dev = obj->dev;
4197 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01004198 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004199
Chris Wilson23bc5982010-09-29 16:10:57 +01004200 WARN_ON(i915_verify_lists(dev));
Chris Wilson74653782010-10-29 10:41:23 +01004201 BUG_ON(obj_priv->pin_count == 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004202 BUG_ON(obj_priv->gtt_space == NULL);
4203
Chris Wilson74653782010-10-29 10:41:23 +01004204 if (--obj_priv->pin_count == 0) {
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004205 if (!obj_priv->active)
Chris Wilson69dc4982010-10-19 10:36:51 +01004206 list_move_tail(&obj_priv->mm_list,
Eric Anholt673a3942008-07-30 12:06:12 -07004207 &dev_priv->mm.inactive_list);
Chris Wilsona00b10c2010-09-24 21:15:47 +01004208 i915_gem_info_remove_pin(dev_priv, obj_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07004209 }
Chris Wilson23bc5982010-09-29 16:10:57 +01004210 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07004211}
4212
4213int
4214i915_gem_pin_ioctl(struct drm_device *dev, void *data,
4215 struct drm_file *file_priv)
4216{
4217 struct drm_i915_gem_pin *args = data;
4218 struct drm_gem_object *obj;
4219 struct drm_i915_gem_object *obj_priv;
4220 int ret;
4221
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004222 ret = i915_mutex_lock_interruptible(dev);
4223 if (ret)
4224 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004225
4226 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4227 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004228 ret = -ENOENT;
4229 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07004230 }
Daniel Vetter23010e42010-03-08 13:35:02 +01004231 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004232
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004233 if (obj_priv->madv != I915_MADV_WILLNEED) {
4234 DRM_ERROR("Attempting to pin a purgeable buffer\n");
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004235 ret = -EINVAL;
4236 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004237 }
4238
Jesse Barnes79e53942008-11-07 14:24:08 -08004239 if (obj_priv->pin_filp != NULL && obj_priv->pin_filp != file_priv) {
4240 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
4241 args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004242 ret = -EINVAL;
4243 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08004244 }
4245
4246 obj_priv->user_pin_count++;
4247 obj_priv->pin_filp = file_priv;
4248 if (obj_priv->user_pin_count == 1) {
Chris Wilsona00b10c2010-09-24 21:15:47 +01004249 ret = i915_gem_object_pin(obj, args->alignment,
4250 true, obj_priv->tiling_mode);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004251 if (ret)
4252 goto out;
Eric Anholt673a3942008-07-30 12:06:12 -07004253 }
4254
4255 /* XXX - flush the CPU caches for pinned objects
4256 * as the X server doesn't manage domains yet
4257 */
Eric Anholte47c68e2008-11-14 13:35:19 -08004258 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004259 args->offset = obj_priv->gtt_offset;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004260out:
Eric Anholt673a3942008-07-30 12:06:12 -07004261 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004262unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07004263 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004264 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004265}
4266
4267int
4268i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
4269 struct drm_file *file_priv)
4270{
4271 struct drm_i915_gem_pin *args = data;
4272 struct drm_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08004273 struct drm_i915_gem_object *obj_priv;
Chris Wilson76c1dec2010-09-25 11:22:51 +01004274 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004275
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004276 ret = i915_mutex_lock_interruptible(dev);
4277 if (ret)
4278 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004279
4280 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4281 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004282 ret = -ENOENT;
4283 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07004284 }
Daniel Vetter23010e42010-03-08 13:35:02 +01004285 obj_priv = to_intel_bo(obj);
Chris Wilson76c1dec2010-09-25 11:22:51 +01004286
Jesse Barnes79e53942008-11-07 14:24:08 -08004287 if (obj_priv->pin_filp != file_priv) {
4288 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
4289 args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004290 ret = -EINVAL;
4291 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08004292 }
4293 obj_priv->user_pin_count--;
4294 if (obj_priv->user_pin_count == 0) {
4295 obj_priv->pin_filp = NULL;
4296 i915_gem_object_unpin(obj);
4297 }
Eric Anholt673a3942008-07-30 12:06:12 -07004298
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004299out:
Eric Anholt673a3942008-07-30 12:06:12 -07004300 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004301unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07004302 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004303 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004304}
4305
4306int
4307i915_gem_busy_ioctl(struct drm_device *dev, void *data,
4308 struct drm_file *file_priv)
4309{
4310 struct drm_i915_gem_busy *args = data;
4311 struct drm_gem_object *obj;
4312 struct drm_i915_gem_object *obj_priv;
Chris Wilson30dbf0c2010-09-25 10:19:17 +01004313 int ret;
4314
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004315 ret = i915_mutex_lock_interruptible(dev);
4316 if (ret)
4317 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004318
Eric Anholt673a3942008-07-30 12:06:12 -07004319 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4320 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004321 ret = -ENOENT;
4322 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07004323 }
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004324 obj_priv = to_intel_bo(obj);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004325
Chris Wilson0be555b2010-08-04 15:36:30 +01004326 /* Count all active objects as busy, even if they are currently not used
4327 * by the gpu. Users of this interface expect objects to eventually
4328 * become non-busy without any further actions, therefore emit any
4329 * necessary flushes here.
Eric Anholtc4de0a52008-12-14 19:05:04 -08004330 */
Chris Wilson0be555b2010-08-04 15:36:30 +01004331 args->busy = obj_priv->active;
4332 if (args->busy) {
4333 /* Unconditionally flush objects, even when the gpu still uses this
4334 * object. Userspace calling this function indicates that it wants to
4335 * use this buffer rather sooner than later, so issuing the required
4336 * flush earlier is beneficial.
4337 */
Chris Wilsonc78ec302010-09-20 12:50:23 +01004338 if (obj->write_domain & I915_GEM_GPU_DOMAINS)
4339 i915_gem_flush_ring(dev, file_priv,
Chris Wilson92204342010-09-18 11:02:01 +01004340 obj_priv->ring,
4341 0, obj->write_domain);
Chris Wilson0be555b2010-08-04 15:36:30 +01004342
4343 /* Update the active list for the hardware's current position.
4344 * Otherwise this only updates on a delayed timer or when irqs
4345 * are actually unmasked, and our working set ends up being
4346 * larger than required.
4347 */
4348 i915_gem_retire_requests_ring(dev, obj_priv->ring);
4349
4350 args->busy = obj_priv->active;
4351 }
Eric Anholt673a3942008-07-30 12:06:12 -07004352
4353 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004354unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07004355 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004356 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004357}
4358
4359int
4360i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4361 struct drm_file *file_priv)
4362{
4363 return i915_gem_ring_throttle(dev, file_priv);
4364}
4365
Chris Wilson3ef94da2009-09-14 16:50:29 +01004366int
4367i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4368 struct drm_file *file_priv)
4369{
4370 struct drm_i915_gem_madvise *args = data;
4371 struct drm_gem_object *obj;
4372 struct drm_i915_gem_object *obj_priv;
Chris Wilson76c1dec2010-09-25 11:22:51 +01004373 int ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004374
4375 switch (args->madv) {
4376 case I915_MADV_DONTNEED:
4377 case I915_MADV_WILLNEED:
4378 break;
4379 default:
4380 return -EINVAL;
4381 }
4382
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004383 ret = i915_mutex_lock_interruptible(dev);
4384 if (ret)
4385 return ret;
4386
Chris Wilson3ef94da2009-09-14 16:50:29 +01004387 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4388 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004389 ret = -ENOENT;
4390 goto unlock;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004391 }
Daniel Vetter23010e42010-03-08 13:35:02 +01004392 obj_priv = to_intel_bo(obj);
Chris Wilson3ef94da2009-09-14 16:50:29 +01004393
4394 if (obj_priv->pin_count) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004395 ret = -EINVAL;
4396 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004397 }
4398
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004399 if (obj_priv->madv != __I915_MADV_PURGED)
4400 obj_priv->madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004401
Chris Wilson2d7ef392009-09-20 23:13:10 +01004402 /* if the object is no longer bound, discard its backing storage */
4403 if (i915_gem_object_is_purgeable(obj_priv) &&
4404 obj_priv->gtt_space == NULL)
4405 i915_gem_object_truncate(obj);
4406
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004407 args->retained = obj_priv->madv != __I915_MADV_PURGED;
4408
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004409out:
Chris Wilson3ef94da2009-09-14 16:50:29 +01004410 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004411unlock:
Chris Wilson3ef94da2009-09-14 16:50:29 +01004412 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004413 return ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004414}
4415
Daniel Vetterac52bc52010-04-09 19:05:06 +00004416struct drm_gem_object * i915_gem_alloc_object(struct drm_device *dev,
4417 size_t size)
4418{
Chris Wilson73aa8082010-09-30 11:46:12 +01004419 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc397b902010-04-09 19:05:07 +00004420 struct drm_i915_gem_object *obj;
4421
4422 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
4423 if (obj == NULL)
4424 return NULL;
4425
4426 if (drm_gem_object_init(dev, &obj->base, size) != 0) {
4427 kfree(obj);
4428 return NULL;
4429 }
4430
Chris Wilson73aa8082010-09-30 11:46:12 +01004431 i915_gem_info_add_obj(dev_priv, size);
4432
Daniel Vetterc397b902010-04-09 19:05:07 +00004433 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4434 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4435
4436 obj->agp_type = AGP_USER_MEMORY;
Daniel Vetter62b8b212010-04-09 19:05:08 +00004437 obj->base.driver_private = NULL;
Daniel Vetterc397b902010-04-09 19:05:07 +00004438 obj->fence_reg = I915_FENCE_REG_NONE;
Chris Wilson69dc4982010-10-19 10:36:51 +01004439 INIT_LIST_HEAD(&obj->mm_list);
4440 INIT_LIST_HEAD(&obj->ring_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00004441 INIT_LIST_HEAD(&obj->gpu_write_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00004442 obj->madv = I915_MADV_WILLNEED;
Chris Wilsona00b10c2010-09-24 21:15:47 +01004443 obj->fenceable = true;
4444 obj->mappable = true;
Daniel Vetterc397b902010-04-09 19:05:07 +00004445
Daniel Vetterc397b902010-04-09 19:05:07 +00004446 return &obj->base;
Daniel Vetterac52bc52010-04-09 19:05:06 +00004447}
4448
Eric Anholt673a3942008-07-30 12:06:12 -07004449int i915_gem_init_object(struct drm_gem_object *obj)
4450{
Daniel Vetterc397b902010-04-09 19:05:07 +00004451 BUG();
Jesse Barnesde151cf2008-11-12 10:03:55 -08004452
Eric Anholt673a3942008-07-30 12:06:12 -07004453 return 0;
4454}
4455
Chris Wilsonbe726152010-07-23 23:18:50 +01004456static void i915_gem_free_object_tail(struct drm_gem_object *obj)
4457{
4458 struct drm_device *dev = obj->dev;
4459 drm_i915_private_t *dev_priv = dev->dev_private;
4460 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
4461 int ret;
4462
4463 ret = i915_gem_object_unbind(obj);
4464 if (ret == -ERESTARTSYS) {
Chris Wilson69dc4982010-10-19 10:36:51 +01004465 list_move(&obj_priv->mm_list,
Chris Wilsonbe726152010-07-23 23:18:50 +01004466 &dev_priv->mm.deferred_free_list);
4467 return;
4468 }
4469
Chris Wilson39a01d12010-10-28 13:03:06 +01004470 if (obj->map_list.map)
Chris Wilsonbe726152010-07-23 23:18:50 +01004471 i915_gem_free_mmap_offset(obj);
4472
4473 drm_gem_object_release(obj);
Chris Wilson73aa8082010-09-30 11:46:12 +01004474 i915_gem_info_remove_obj(dev_priv, obj->size);
Chris Wilsonbe726152010-07-23 23:18:50 +01004475
4476 kfree(obj_priv->page_cpu_valid);
4477 kfree(obj_priv->bit_17);
4478 kfree(obj_priv);
4479}
4480
Eric Anholt673a3942008-07-30 12:06:12 -07004481void i915_gem_free_object(struct drm_gem_object *obj)
4482{
Jesse Barnesde151cf2008-11-12 10:03:55 -08004483 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01004484 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004485
Chris Wilson1c5d22f2009-08-25 11:15:50 +01004486 trace_i915_gem_object_destroy(obj);
4487
Eric Anholt673a3942008-07-30 12:06:12 -07004488 while (obj_priv->pin_count > 0)
4489 i915_gem_object_unpin(obj);
4490
Dave Airlie71acb5e2008-12-30 20:31:46 +10004491 if (obj_priv->phys_obj)
4492 i915_gem_detach_phys_object(dev, obj);
4493
Chris Wilsonbe726152010-07-23 23:18:50 +01004494 i915_gem_free_object_tail(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004495}
4496
Jesse Barnes5669fca2009-02-17 15:13:31 -08004497int
Eric Anholt673a3942008-07-30 12:06:12 -07004498i915_gem_idle(struct drm_device *dev)
4499{
4500 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson29105cc2010-01-07 10:39:13 +00004501 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004502
Keith Packard6dbe2772008-10-14 21:41:13 -07004503 mutex_lock(&dev->struct_mutex);
4504
Chris Wilson87acb0a2010-10-19 10:13:00 +01004505 if (dev_priv->mm.suspended) {
Keith Packard6dbe2772008-10-14 21:41:13 -07004506 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004507 return 0;
Keith Packard6dbe2772008-10-14 21:41:13 -07004508 }
Eric Anholt673a3942008-07-30 12:06:12 -07004509
Chris Wilson29105cc2010-01-07 10:39:13 +00004510 ret = i915_gpu_idle(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004511 if (ret) {
4512 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004513 return ret;
Keith Packard6dbe2772008-10-14 21:41:13 -07004514 }
Eric Anholt673a3942008-07-30 12:06:12 -07004515
Chris Wilson29105cc2010-01-07 10:39:13 +00004516 /* Under UMS, be paranoid and evict. */
4517 if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
Chris Wilson5eac3ab2010-10-31 08:49:47 +00004518 ret = i915_gem_evict_inactive(dev, false);
Chris Wilson29105cc2010-01-07 10:39:13 +00004519 if (ret) {
4520 mutex_unlock(&dev->struct_mutex);
4521 return ret;
4522 }
4523 }
4524
4525 /* Hack! Don't let anybody do execbuf while we don't control the chip.
4526 * We need to replace this with a semaphore, or something.
4527 * And not confound mm.suspended!
4528 */
4529 dev_priv->mm.suspended = 1;
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02004530 del_timer_sync(&dev_priv->hangcheck_timer);
Chris Wilson29105cc2010-01-07 10:39:13 +00004531
4532 i915_kernel_lost_context(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004533 i915_gem_cleanup_ringbuffer(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00004534
Keith Packard6dbe2772008-10-14 21:41:13 -07004535 mutex_unlock(&dev->struct_mutex);
4536
Chris Wilson29105cc2010-01-07 10:39:13 +00004537 /* Cancel the retire work handler, which should be idle now. */
4538 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
4539
Eric Anholt673a3942008-07-30 12:06:12 -07004540 return 0;
4541}
4542
Jesse Barnese552eb72010-04-21 11:39:23 -07004543/*
4544 * 965+ support PIPE_CONTROL commands, which provide finer grained control
4545 * over cache flushing.
4546 */
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004547static int
Jesse Barnese552eb72010-04-21 11:39:23 -07004548i915_gem_init_pipe_control(struct drm_device *dev)
4549{
4550 drm_i915_private_t *dev_priv = dev->dev_private;
4551 struct drm_gem_object *obj;
4552 struct drm_i915_gem_object *obj_priv;
4553 int ret;
4554
Eric Anholt34dc4d42010-05-07 14:30:03 -07004555 obj = i915_gem_alloc_object(dev, 4096);
Jesse Barnese552eb72010-04-21 11:39:23 -07004556 if (obj == NULL) {
4557 DRM_ERROR("Failed to allocate seqno page\n");
4558 ret = -ENOMEM;
4559 goto err;
4560 }
4561 obj_priv = to_intel_bo(obj);
4562 obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
4563
Chris Wilsona00b10c2010-09-24 21:15:47 +01004564 ret = i915_gem_object_pin(obj, 4096, true, false);
Jesse Barnese552eb72010-04-21 11:39:23 -07004565 if (ret)
4566 goto err_unref;
4567
4568 dev_priv->seqno_gfx_addr = obj_priv->gtt_offset;
4569 dev_priv->seqno_page = kmap(obj_priv->pages[0]);
4570 if (dev_priv->seqno_page == NULL)
4571 goto err_unpin;
4572
4573 dev_priv->seqno_obj = obj;
4574 memset(dev_priv->seqno_page, 0, PAGE_SIZE);
4575
4576 return 0;
4577
4578err_unpin:
4579 i915_gem_object_unpin(obj);
4580err_unref:
4581 drm_gem_object_unreference(obj);
4582err:
4583 return ret;
4584}
4585
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004586
4587static void
Jesse Barnese552eb72010-04-21 11:39:23 -07004588i915_gem_cleanup_pipe_control(struct drm_device *dev)
4589{
4590 drm_i915_private_t *dev_priv = dev->dev_private;
4591 struct drm_gem_object *obj;
4592 struct drm_i915_gem_object *obj_priv;
4593
4594 obj = dev_priv->seqno_obj;
4595 obj_priv = to_intel_bo(obj);
4596 kunmap(obj_priv->pages[0]);
4597 i915_gem_object_unpin(obj);
4598 drm_gem_object_unreference(obj);
4599 dev_priv->seqno_obj = NULL;
4600
4601 dev_priv->seqno_page = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07004602}
4603
Eric Anholt673a3942008-07-30 12:06:12 -07004604int
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004605i915_gem_init_ringbuffer(struct drm_device *dev)
4606{
4607 drm_i915_private_t *dev_priv = dev->dev_private;
4608 int ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01004609
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004610 if (HAS_PIPE_CONTROL(dev)) {
4611 ret = i915_gem_init_pipe_control(dev);
4612 if (ret)
4613 return ret;
4614 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004615
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08004616 ret = intel_init_render_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004617 if (ret)
4618 goto cleanup_pipe_control;
4619
4620 if (HAS_BSD(dev)) {
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08004621 ret = intel_init_bsd_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004622 if (ret)
4623 goto cleanup_render_ring;
Zou Nan haid1b851f2010-05-21 09:08:57 +08004624 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004625
Chris Wilson549f7362010-10-19 11:19:32 +01004626 if (HAS_BLT(dev)) {
4627 ret = intel_init_blt_ring_buffer(dev);
4628 if (ret)
4629 goto cleanup_bsd_ring;
4630 }
4631
Chris Wilson6f392d52010-08-07 11:01:22 +01004632 dev_priv->next_seqno = 1;
4633
Chris Wilson68f95ba2010-05-27 13:18:22 +01004634 return 0;
4635
Chris Wilson549f7362010-10-19 11:19:32 +01004636cleanup_bsd_ring:
Chris Wilson78501ea2010-10-27 12:18:21 +01004637 intel_cleanup_ring_buffer(&dev_priv->bsd_ring);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004638cleanup_render_ring:
Chris Wilson78501ea2010-10-27 12:18:21 +01004639 intel_cleanup_ring_buffer(&dev_priv->render_ring);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004640cleanup_pipe_control:
4641 if (HAS_PIPE_CONTROL(dev))
4642 i915_gem_cleanup_pipe_control(dev);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004643 return ret;
4644}
4645
4646void
4647i915_gem_cleanup_ringbuffer(struct drm_device *dev)
4648{
4649 drm_i915_private_t *dev_priv = dev->dev_private;
4650
Chris Wilson78501ea2010-10-27 12:18:21 +01004651 intel_cleanup_ring_buffer(&dev_priv->render_ring);
4652 intel_cleanup_ring_buffer(&dev_priv->bsd_ring);
4653 intel_cleanup_ring_buffer(&dev_priv->blt_ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004654 if (HAS_PIPE_CONTROL(dev))
4655 i915_gem_cleanup_pipe_control(dev);
4656}
4657
4658int
Eric Anholt673a3942008-07-30 12:06:12 -07004659i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
4660 struct drm_file *file_priv)
4661{
4662 drm_i915_private_t *dev_priv = dev->dev_private;
4663 int ret;
4664
Jesse Barnes79e53942008-11-07 14:24:08 -08004665 if (drm_core_check_feature(dev, DRIVER_MODESET))
4666 return 0;
4667
Ben Gamariba1234d2009-09-14 17:48:47 -04004668 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07004669 DRM_ERROR("Reenabling wedged hardware, good luck\n");
Ben Gamariba1234d2009-09-14 17:48:47 -04004670 atomic_set(&dev_priv->mm.wedged, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004671 }
4672
Eric Anholt673a3942008-07-30 12:06:12 -07004673 mutex_lock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004674 dev_priv->mm.suspended = 0;
4675
4676 ret = i915_gem_init_ringbuffer(dev);
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004677 if (ret != 0) {
4678 mutex_unlock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004679 return ret;
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004680 }
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004681
Chris Wilson69dc4982010-10-19 10:36:51 +01004682 BUG_ON(!list_empty(&dev_priv->mm.active_list));
Zou Nan hai852835f2010-05-21 09:08:56 +08004683 BUG_ON(!list_empty(&dev_priv->render_ring.active_list));
Chris Wilson87acb0a2010-10-19 10:13:00 +01004684 BUG_ON(!list_empty(&dev_priv->bsd_ring.active_list));
Chris Wilson549f7362010-10-19 11:19:32 +01004685 BUG_ON(!list_empty(&dev_priv->blt_ring.active_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004686 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
4687 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
Zou Nan hai852835f2010-05-21 09:08:56 +08004688 BUG_ON(!list_empty(&dev_priv->render_ring.request_list));
Chris Wilson87acb0a2010-10-19 10:13:00 +01004689 BUG_ON(!list_empty(&dev_priv->bsd_ring.request_list));
Chris Wilson549f7362010-10-19 11:19:32 +01004690 BUG_ON(!list_empty(&dev_priv->blt_ring.request_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004691 mutex_unlock(&dev->struct_mutex);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004692
Chris Wilson5f353082010-06-07 14:03:03 +01004693 ret = drm_irq_install(dev);
4694 if (ret)
4695 goto cleanup_ringbuffer;
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004696
Eric Anholt673a3942008-07-30 12:06:12 -07004697 return 0;
Chris Wilson5f353082010-06-07 14:03:03 +01004698
4699cleanup_ringbuffer:
4700 mutex_lock(&dev->struct_mutex);
4701 i915_gem_cleanup_ringbuffer(dev);
4702 dev_priv->mm.suspended = 1;
4703 mutex_unlock(&dev->struct_mutex);
4704
4705 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004706}
4707
4708int
4709i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
4710 struct drm_file *file_priv)
4711{
Jesse Barnes79e53942008-11-07 14:24:08 -08004712 if (drm_core_check_feature(dev, DRIVER_MODESET))
4713 return 0;
4714
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004715 drm_irq_uninstall(dev);
Linus Torvaldse6890f62009-09-08 17:09:24 -07004716 return i915_gem_idle(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004717}
4718
4719void
4720i915_gem_lastclose(struct drm_device *dev)
4721{
4722 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004723
Eric Anholte806b492009-01-22 09:56:58 -08004724 if (drm_core_check_feature(dev, DRIVER_MODESET))
4725 return;
4726
Keith Packard6dbe2772008-10-14 21:41:13 -07004727 ret = i915_gem_idle(dev);
4728 if (ret)
4729 DRM_ERROR("failed to idle hardware: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07004730}
4731
Chris Wilson64193402010-10-24 12:38:05 +01004732static void
4733init_ring_lists(struct intel_ring_buffer *ring)
4734{
4735 INIT_LIST_HEAD(&ring->active_list);
4736 INIT_LIST_HEAD(&ring->request_list);
4737 INIT_LIST_HEAD(&ring->gpu_write_list);
4738}
4739
Eric Anholt673a3942008-07-30 12:06:12 -07004740void
4741i915_gem_load(struct drm_device *dev)
4742{
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004743 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07004744 drm_i915_private_t *dev_priv = dev->dev_private;
4745
Chris Wilson69dc4982010-10-19 10:36:51 +01004746 INIT_LIST_HEAD(&dev_priv->mm.active_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004747 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
4748 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004749 INIT_LIST_HEAD(&dev_priv->mm.pinned_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07004750 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Chris Wilsonbe726152010-07-23 23:18:50 +01004751 INIT_LIST_HEAD(&dev_priv->mm.deferred_free_list);
Chris Wilson64193402010-10-24 12:38:05 +01004752 init_ring_lists(&dev_priv->render_ring);
4753 init_ring_lists(&dev_priv->bsd_ring);
4754 init_ring_lists(&dev_priv->blt_ring);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02004755 for (i = 0; i < 16; i++)
4756 INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004757 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
4758 i915_gem_retire_work_handler);
Chris Wilson30dbf0c2010-09-25 10:19:17 +01004759 init_completion(&dev_priv->error_completion);
Chris Wilson31169712009-09-14 16:50:28 +01004760
Dave Airlie94400122010-07-20 13:15:31 +10004761 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
4762 if (IS_GEN3(dev)) {
4763 u32 tmp = I915_READ(MI_ARB_STATE);
4764 if (!(tmp & MI_ARB_C3_LP_WRITE_ENABLE)) {
4765 /* arb state is a masked write, so set bit + bit in mask */
4766 tmp = MI_ARB_C3_LP_WRITE_ENABLE | (MI_ARB_C3_LP_WRITE_ENABLE << MI_ARB_MASK_SHIFT);
4767 I915_WRITE(MI_ARB_STATE, tmp);
4768 }
4769 }
4770
Jesse Barnesde151cf2008-11-12 10:03:55 -08004771 /* Old X drivers will take 0-2 for front, back, depth buffers */
Eric Anholtb397c832010-01-26 09:43:10 -08004772 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4773 dev_priv->fence_reg_start = 3;
Jesse Barnesde151cf2008-11-12 10:03:55 -08004774
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004775 if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08004776 dev_priv->num_fence_regs = 16;
4777 else
4778 dev_priv->num_fence_regs = 8;
4779
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004780 /* Initialize fence registers to zero */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004781 switch (INTEL_INFO(dev)->gen) {
4782 case 6:
4783 for (i = 0; i < 16; i++)
4784 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (i * 8), 0);
4785 break;
4786 case 5:
4787 case 4:
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004788 for (i = 0; i < 16; i++)
4789 I915_WRITE64(FENCE_REG_965_0 + (i * 8), 0);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004790 break;
4791 case 3:
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004792 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4793 for (i = 0; i < 8; i++)
4794 I915_WRITE(FENCE_REG_945_8 + (i * 4), 0);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004795 case 2:
4796 for (i = 0; i < 8; i++)
4797 I915_WRITE(FENCE_REG_830_0 + (i * 4), 0);
4798 break;
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004799 }
Eric Anholt673a3942008-07-30 12:06:12 -07004800 i915_gem_detect_bit_6_swizzle(dev);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05004801 init_waitqueue_head(&dev_priv->pending_flip_queue);
Chris Wilson17250b72010-10-28 12:51:39 +01004802
4803 dev_priv->mm.inactive_shrinker.shrink = i915_gem_inactive_shrink;
4804 dev_priv->mm.inactive_shrinker.seeks = DEFAULT_SEEKS;
4805 register_shrinker(&dev_priv->mm.inactive_shrinker);
Eric Anholt673a3942008-07-30 12:06:12 -07004806}
Dave Airlie71acb5e2008-12-30 20:31:46 +10004807
4808/*
4809 * Create a physically contiguous memory object for this object
4810 * e.g. for cursor + overlay regs
4811 */
Chris Wilson995b6762010-08-20 13:23:26 +01004812static int i915_gem_init_phys_object(struct drm_device *dev,
4813 int id, int size, int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004814{
4815 drm_i915_private_t *dev_priv = dev->dev_private;
4816 struct drm_i915_gem_phys_object *phys_obj;
4817 int ret;
4818
4819 if (dev_priv->mm.phys_objs[id - 1] || !size)
4820 return 0;
4821
Eric Anholt9a298b22009-03-24 12:23:04 -07004822 phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004823 if (!phys_obj)
4824 return -ENOMEM;
4825
4826 phys_obj->id = id;
4827
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004828 phys_obj->handle = drm_pci_alloc(dev, size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004829 if (!phys_obj->handle) {
4830 ret = -ENOMEM;
4831 goto kfree_obj;
4832 }
4833#ifdef CONFIG_X86
4834 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4835#endif
4836
4837 dev_priv->mm.phys_objs[id - 1] = phys_obj;
4838
4839 return 0;
4840kfree_obj:
Eric Anholt9a298b22009-03-24 12:23:04 -07004841 kfree(phys_obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004842 return ret;
4843}
4844
Chris Wilson995b6762010-08-20 13:23:26 +01004845static void i915_gem_free_phys_object(struct drm_device *dev, int id)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004846{
4847 drm_i915_private_t *dev_priv = dev->dev_private;
4848 struct drm_i915_gem_phys_object *phys_obj;
4849
4850 if (!dev_priv->mm.phys_objs[id - 1])
4851 return;
4852
4853 phys_obj = dev_priv->mm.phys_objs[id - 1];
4854 if (phys_obj->cur_obj) {
4855 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
4856 }
4857
4858#ifdef CONFIG_X86
4859 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4860#endif
4861 drm_pci_free(dev, phys_obj->handle);
4862 kfree(phys_obj);
4863 dev_priv->mm.phys_objs[id - 1] = NULL;
4864}
4865
4866void i915_gem_free_all_phys_object(struct drm_device *dev)
4867{
4868 int i;
4869
Dave Airlie260883c2009-01-22 17:58:49 +10004870 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004871 i915_gem_free_phys_object(dev, i);
4872}
4873
4874void i915_gem_detach_phys_object(struct drm_device *dev,
4875 struct drm_gem_object *obj)
4876{
Chris Wilsone5281cc2010-10-28 13:45:36 +01004877 struct address_space *mapping = obj->filp->f_path.dentry->d_inode->i_mapping;
4878 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
4879 char *vaddr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004880 int i;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004881 int page_count;
4882
Dave Airlie71acb5e2008-12-30 20:31:46 +10004883 if (!obj_priv->phys_obj)
4884 return;
Chris Wilsone5281cc2010-10-28 13:45:36 +01004885 vaddr = obj_priv->phys_obj->handle->vaddr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004886
4887 page_count = obj->size / PAGE_SIZE;
4888
4889 for (i = 0; i < page_count; i++) {
Chris Wilsone5281cc2010-10-28 13:45:36 +01004890 struct page *page = read_cache_page_gfp(mapping, i,
4891 GFP_HIGHUSER | __GFP_RECLAIMABLE);
4892 if (!IS_ERR(page)) {
4893 char *dst = kmap_atomic(page);
4894 memcpy(dst, vaddr + i*PAGE_SIZE, PAGE_SIZE);
4895 kunmap_atomic(dst);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004896
Chris Wilsone5281cc2010-10-28 13:45:36 +01004897 drm_clflush_pages(&page, 1);
4898
4899 set_page_dirty(page);
4900 mark_page_accessed(page);
4901 page_cache_release(page);
4902 }
Dave Airlie71acb5e2008-12-30 20:31:46 +10004903 }
Dave Airlie71acb5e2008-12-30 20:31:46 +10004904 drm_agp_chipset_flush(dev);
Chris Wilsond78b47b2009-06-17 21:52:49 +01004905
Dave Airlie71acb5e2008-12-30 20:31:46 +10004906 obj_priv->phys_obj->cur_obj = NULL;
4907 obj_priv->phys_obj = NULL;
4908}
4909
4910int
4911i915_gem_attach_phys_object(struct drm_device *dev,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004912 struct drm_gem_object *obj,
4913 int id,
4914 int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004915{
Chris Wilsone5281cc2010-10-28 13:45:36 +01004916 struct address_space *mapping = obj->filp->f_path.dentry->d_inode->i_mapping;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004917 drm_i915_private_t *dev_priv = dev->dev_private;
4918 struct drm_i915_gem_object *obj_priv;
4919 int ret = 0;
4920 int page_count;
4921 int i;
4922
4923 if (id > I915_MAX_PHYS_OBJECT)
4924 return -EINVAL;
4925
Daniel Vetter23010e42010-03-08 13:35:02 +01004926 obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004927
4928 if (obj_priv->phys_obj) {
4929 if (obj_priv->phys_obj->id == id)
4930 return 0;
4931 i915_gem_detach_phys_object(dev, obj);
4932 }
4933
Dave Airlie71acb5e2008-12-30 20:31:46 +10004934 /* create a new object */
4935 if (!dev_priv->mm.phys_objs[id - 1]) {
4936 ret = i915_gem_init_phys_object(dev, id,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004937 obj->size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004938 if (ret) {
Linus Torvaldsaeb565d2009-01-26 10:01:53 -08004939 DRM_ERROR("failed to init phys object %d size: %zu\n", id, obj->size);
Chris Wilsone5281cc2010-10-28 13:45:36 +01004940 return ret;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004941 }
4942 }
4943
4944 /* bind to the object */
4945 obj_priv->phys_obj = dev_priv->mm.phys_objs[id - 1];
4946 obj_priv->phys_obj->cur_obj = obj;
4947
Dave Airlie71acb5e2008-12-30 20:31:46 +10004948 page_count = obj->size / PAGE_SIZE;
4949
4950 for (i = 0; i < page_count; i++) {
Chris Wilsone5281cc2010-10-28 13:45:36 +01004951 struct page *page;
4952 char *dst, *src;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004953
Chris Wilsone5281cc2010-10-28 13:45:36 +01004954 page = read_cache_page_gfp(mapping, i,
4955 GFP_HIGHUSER | __GFP_RECLAIMABLE);
4956 if (IS_ERR(page))
4957 return PTR_ERR(page);
4958
Chris Wilsonff75b9b2010-10-30 22:52:31 +01004959 src = kmap_atomic(page);
Chris Wilsone5281cc2010-10-28 13:45:36 +01004960 dst = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004961 memcpy(dst, src, PAGE_SIZE);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -07004962 kunmap_atomic(src);
Chris Wilsone5281cc2010-10-28 13:45:36 +01004963
4964 mark_page_accessed(page);
4965 page_cache_release(page);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004966 }
4967
4968 return 0;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004969}
4970
4971static int
4972i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
4973 struct drm_i915_gem_pwrite *args,
4974 struct drm_file *file_priv)
4975{
Daniel Vetter23010e42010-03-08 13:35:02 +01004976 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004977 void *obj_addr;
4978 int ret;
4979 char __user *user_data;
4980
4981 user_data = (char __user *) (uintptr_t) args->data_ptr;
4982 obj_addr = obj_priv->phys_obj->handle->vaddr + args->offset;
4983
Zhao Yakui44d98a62009-10-09 11:39:40 +08004984 DRM_DEBUG_DRIVER("obj_addr %p, %lld\n", obj_addr, args->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004985 ret = copy_from_user(obj_addr, user_data, args->size);
4986 if (ret)
4987 return -EFAULT;
4988
4989 drm_agp_chipset_flush(dev);
4990 return 0;
4991}
Eric Anholtb9624422009-06-03 07:27:35 +00004992
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004993void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00004994{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004995 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00004996
4997 /* Clean up our request list when the client is going away, so that
4998 * later retire_requests won't dereference our soon-to-be-gone
4999 * file_priv.
5000 */
Chris Wilson1c255952010-09-26 11:03:27 +01005001 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005002 while (!list_empty(&file_priv->mm.request_list)) {
5003 struct drm_i915_gem_request *request;
5004
5005 request = list_first_entry(&file_priv->mm.request_list,
5006 struct drm_i915_gem_request,
5007 client_list);
5008 list_del(&request->client_list);
5009 request->file_priv = NULL;
5010 }
Chris Wilson1c255952010-09-26 11:03:27 +01005011 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00005012}
Chris Wilson31169712009-09-14 16:50:28 +01005013
Chris Wilson31169712009-09-14 16:50:28 +01005014static int
Chris Wilson1637ef42010-04-20 17:10:35 +01005015i915_gpu_is_active(struct drm_device *dev)
5016{
5017 drm_i915_private_t *dev_priv = dev->dev_private;
5018 int lists_empty;
5019
Chris Wilson1637ef42010-04-20 17:10:35 +01005020 lists_empty = list_empty(&dev_priv->mm.flushing_list) &&
Chris Wilson17250b72010-10-28 12:51:39 +01005021 list_empty(&dev_priv->mm.active_list);
Chris Wilson1637ef42010-04-20 17:10:35 +01005022
5023 return !lists_empty;
5024}
5025
5026static int
Chris Wilson17250b72010-10-28 12:51:39 +01005027i915_gem_inactive_shrink(struct shrinker *shrinker,
5028 int nr_to_scan,
5029 gfp_t gfp_mask)
Chris Wilson31169712009-09-14 16:50:28 +01005030{
Chris Wilson17250b72010-10-28 12:51:39 +01005031 struct drm_i915_private *dev_priv =
5032 container_of(shrinker,
5033 struct drm_i915_private,
5034 mm.inactive_shrinker);
5035 struct drm_device *dev = dev_priv->dev;
5036 struct drm_i915_gem_object *obj, *next;
5037 int cnt;
5038
5039 if (!mutex_trylock(&dev->struct_mutex))
Chris Wilsonbbe2e112010-10-28 22:35:07 +01005040 return 0;
Chris Wilson31169712009-09-14 16:50:28 +01005041
5042 /* "fast-path" to count number of available objects */
5043 if (nr_to_scan == 0) {
Chris Wilson17250b72010-10-28 12:51:39 +01005044 cnt = 0;
5045 list_for_each_entry(obj,
5046 &dev_priv->mm.inactive_list,
5047 mm_list)
5048 cnt++;
5049 mutex_unlock(&dev->struct_mutex);
5050 return cnt / 100 * sysctl_vfs_cache_pressure;
Chris Wilson31169712009-09-14 16:50:28 +01005051 }
5052
Chris Wilson1637ef42010-04-20 17:10:35 +01005053rescan:
Chris Wilson31169712009-09-14 16:50:28 +01005054 /* first scan for clean buffers */
Chris Wilson17250b72010-10-28 12:51:39 +01005055 i915_gem_retire_requests(dev);
Chris Wilson31169712009-09-14 16:50:28 +01005056
Chris Wilson17250b72010-10-28 12:51:39 +01005057 list_for_each_entry_safe(obj, next,
5058 &dev_priv->mm.inactive_list,
5059 mm_list) {
5060 if (i915_gem_object_is_purgeable(obj)) {
5061 i915_gem_object_unbind(&obj->base);
5062 if (--nr_to_scan == 0)
5063 break;
Chris Wilson31169712009-09-14 16:50:28 +01005064 }
Chris Wilson31169712009-09-14 16:50:28 +01005065 }
5066
5067 /* second pass, evict/count anything still on the inactive list */
Chris Wilson17250b72010-10-28 12:51:39 +01005068 cnt = 0;
5069 list_for_each_entry_safe(obj, next,
5070 &dev_priv->mm.inactive_list,
5071 mm_list) {
5072 if (nr_to_scan) {
5073 i915_gem_object_unbind(&obj->base);
5074 nr_to_scan--;
5075 } else
5076 cnt++;
Chris Wilson31169712009-09-14 16:50:28 +01005077 }
5078
Chris Wilson17250b72010-10-28 12:51:39 +01005079 if (nr_to_scan && i915_gpu_is_active(dev)) {
Chris Wilson1637ef42010-04-20 17:10:35 +01005080 /*
5081 * We are desperate for pages, so as a last resort, wait
5082 * for the GPU to finish and discard whatever we can.
5083 * This has a dramatic impact to reduce the number of
5084 * OOM-killer events whilst running the GPU aggressively.
5085 */
Chris Wilson17250b72010-10-28 12:51:39 +01005086 if (i915_gpu_idle(dev) == 0)
Chris Wilson1637ef42010-04-20 17:10:35 +01005087 goto rescan;
5088 }
Chris Wilson17250b72010-10-28 12:51:39 +01005089 mutex_unlock(&dev->struct_mutex);
5090 return cnt / 100 * sysctl_vfs_cache_pressure;
Chris Wilson31169712009-09-14 16:50:28 +01005091}