blob: 7569b77fb4030eeca006341a89fad1984a4271eb [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
Daniel Vetter0108a3e2010-08-07 11:01:21 +010039static uint32_t i915_gem_get_gtt_alignment(struct drm_gem_object *obj);
Daniel Vetterba3d8d72010-02-11 22:37:04 +010040
41static int i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj,
42 bool pipelined);
Eric Anholte47c68e2008-11-14 13:35:19 -080043static void i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj);
44static void i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj);
Eric Anholte47c68e2008-11-14 13:35:19 -080045static int i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj,
46 int write);
47static int i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
48 uint64_t offset,
49 uint64_t size);
50static void i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj);
Chris Wilson2cf34d72010-09-14 13:03:28 +010051static int i915_gem_object_wait_rendering(struct drm_gem_object *obj,
52 bool interruptible);
Jesse Barnesde151cf2008-11-12 10:03:55 -080053static int i915_gem_object_bind_to_gtt(struct drm_gem_object *obj,
Daniel Vetter920afa72010-09-16 17:54:23 +020054 unsigned alignment, bool mappable);
Jesse Barnesde151cf2008-11-12 10:03:55 -080055static void i915_gem_clear_fence_reg(struct drm_gem_object *obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +100056static int i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
57 struct drm_i915_gem_pwrite *args,
58 struct drm_file *file_priv);
Chris Wilsonbe726152010-07-23 23:18:50 +010059static void i915_gem_free_object_tail(struct drm_gem_object *obj);
Eric Anholt673a3942008-07-30 12:06:12 -070060
Chris Wilson17250b72010-10-28 12:51:39 +010061static int i915_gem_inactive_shrink(struct shrinker *shrinker,
62 int nr_to_scan,
63 gfp_t gfp_mask);
64
Chris Wilson31169712009-09-14 16:50:28 +010065
Chris Wilson73aa8082010-09-30 11:46:12 +010066/* some bookkeeping */
67static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
68 size_t size)
69{
70 dev_priv->mm.object_count++;
71 dev_priv->mm.object_memory += size;
72}
73
74static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
75 size_t size)
76{
77 dev_priv->mm.object_count--;
78 dev_priv->mm.object_memory -= size;
79}
80
81static void i915_gem_info_add_gtt(struct drm_i915_private *dev_priv,
Daniel Vetterfb7d5162010-10-01 22:05:20 +020082 struct drm_gem_object *obj)
Chris Wilson73aa8082010-09-30 11:46:12 +010083{
Daniel Vetterfb7d5162010-10-01 22:05:20 +020084 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson73aa8082010-09-30 11:46:12 +010085 dev_priv->mm.gtt_count++;
Daniel Vetterfb7d5162010-10-01 22:05:20 +020086 dev_priv->mm.gtt_memory += obj->size;
87 if (obj_priv->gtt_offset < dev_priv->mm.gtt_mappable_end) {
88 dev_priv->mm.mappable_gtt_used +=
89 min_t(size_t, obj->size,
90 dev_priv->mm.gtt_mappable_end
91 - obj_priv->gtt_offset);
92 }
Chris Wilson73aa8082010-09-30 11:46:12 +010093}
94
95static void i915_gem_info_remove_gtt(struct drm_i915_private *dev_priv,
Daniel Vetterfb7d5162010-10-01 22:05:20 +020096 struct drm_gem_object *obj)
Chris Wilson73aa8082010-09-30 11:46:12 +010097{
Daniel Vetterfb7d5162010-10-01 22:05:20 +020098 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson73aa8082010-09-30 11:46:12 +010099 dev_priv->mm.gtt_count--;
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200100 dev_priv->mm.gtt_memory -= obj->size;
101 if (obj_priv->gtt_offset < dev_priv->mm.gtt_mappable_end) {
102 dev_priv->mm.mappable_gtt_used -=
103 min_t(size_t, obj->size,
104 dev_priv->mm.gtt_mappable_end
105 - obj_priv->gtt_offset);
106 }
107}
108
109/**
110 * Update the mappable working set counters. Call _only_ when there is a change
111 * in one of (pin|fault)_mappable and update *_mappable _before_ calling.
112 * @mappable: new state the changed mappable flag (either pin_ or fault_).
113 */
114static void
115i915_gem_info_update_mappable(struct drm_i915_private *dev_priv,
116 struct drm_gem_object *obj,
117 bool mappable)
118{
119 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
120
121 if (mappable) {
122 if (obj_priv->pin_mappable && obj_priv->fault_mappable)
123 /* Combined state was already mappable. */
124 return;
125 dev_priv->mm.gtt_mappable_count++;
126 dev_priv->mm.gtt_mappable_memory += obj->size;
127 } else {
128 if (obj_priv->pin_mappable || obj_priv->fault_mappable)
129 /* Combined state still mappable. */
130 return;
131 dev_priv->mm.gtt_mappable_count--;
132 dev_priv->mm.gtt_mappable_memory -= obj->size;
133 }
Chris Wilson73aa8082010-09-30 11:46:12 +0100134}
135
136static void i915_gem_info_add_pin(struct drm_i915_private *dev_priv,
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200137 struct drm_gem_object *obj,
138 bool mappable)
Chris Wilson73aa8082010-09-30 11:46:12 +0100139{
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200140 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson73aa8082010-09-30 11:46:12 +0100141 dev_priv->mm.pin_count++;
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200142 dev_priv->mm.pin_memory += obj->size;
143 if (mappable) {
144 obj_priv->pin_mappable = true;
145 i915_gem_info_update_mappable(dev_priv, obj, true);
146 }
Chris Wilson73aa8082010-09-30 11:46:12 +0100147}
148
149static void i915_gem_info_remove_pin(struct drm_i915_private *dev_priv,
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200150 struct drm_gem_object *obj)
Chris Wilson73aa8082010-09-30 11:46:12 +0100151{
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200152 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson73aa8082010-09-30 11:46:12 +0100153 dev_priv->mm.pin_count--;
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200154 dev_priv->mm.pin_memory -= obj->size;
155 if (obj_priv->pin_mappable) {
156 obj_priv->pin_mappable = false;
157 i915_gem_info_update_mappable(dev_priv, obj, false);
158 }
Chris Wilson73aa8082010-09-30 11:46:12 +0100159}
160
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100161int
162i915_gem_check_is_wedged(struct drm_device *dev)
163{
164 struct drm_i915_private *dev_priv = dev->dev_private;
165 struct completion *x = &dev_priv->error_completion;
166 unsigned long flags;
167 int ret;
168
169 if (!atomic_read(&dev_priv->mm.wedged))
170 return 0;
171
172 ret = wait_for_completion_interruptible(x);
173 if (ret)
174 return ret;
175
176 /* Success, we reset the GPU! */
177 if (!atomic_read(&dev_priv->mm.wedged))
178 return 0;
179
180 /* GPU is hung, bump the completion count to account for
181 * the token we just consumed so that we never hit zero and
182 * end up waiting upon a subsequent completion event that
183 * will never happen.
184 */
185 spin_lock_irqsave(&x->wait.lock, flags);
186 x->done++;
187 spin_unlock_irqrestore(&x->wait.lock, flags);
188 return -EIO;
189}
190
Chris Wilson76c1dec2010-09-25 11:22:51 +0100191static int i915_mutex_lock_interruptible(struct drm_device *dev)
192{
193 struct drm_i915_private *dev_priv = dev->dev_private;
194 int ret;
195
196 ret = i915_gem_check_is_wedged(dev);
197 if (ret)
198 return ret;
199
200 ret = mutex_lock_interruptible(&dev->struct_mutex);
201 if (ret)
202 return ret;
203
204 if (atomic_read(&dev_priv->mm.wedged)) {
205 mutex_unlock(&dev->struct_mutex);
206 return -EAGAIN;
207 }
208
Chris Wilson23bc5982010-09-29 16:10:57 +0100209 WARN_ON(i915_verify_lists(dev));
Chris Wilson76c1dec2010-09-25 11:22:51 +0100210 return 0;
211}
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100212
Chris Wilson7d1c4802010-08-07 21:45:03 +0100213static inline bool
214i915_gem_object_is_inactive(struct drm_i915_gem_object *obj_priv)
215{
216 return obj_priv->gtt_space &&
217 !obj_priv->active &&
218 obj_priv->pin_count == 0;
219}
220
Chris Wilson73aa8082010-09-30 11:46:12 +0100221int i915_gem_do_init(struct drm_device *dev,
222 unsigned long start,
Daniel Vetter53984632010-09-22 23:44:24 +0200223 unsigned long mappable_end,
Jesse Barnes79e53942008-11-07 14:24:08 -0800224 unsigned long end)
225{
226 drm_i915_private_t *dev_priv = dev->dev_private;
227
228 if (start >= end ||
229 (start & (PAGE_SIZE - 1)) != 0 ||
230 (end & (PAGE_SIZE - 1)) != 0) {
231 return -EINVAL;
232 }
233
234 drm_mm_init(&dev_priv->mm.gtt_space, start,
235 end - start);
236
Chris Wilson73aa8082010-09-30 11:46:12 +0100237 dev_priv->mm.gtt_total = end - start;
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200238 dev_priv->mm.mappable_gtt_total = min(end, mappable_end) - start;
Daniel Vetter53984632010-09-22 23:44:24 +0200239 dev_priv->mm.gtt_mappable_end = mappable_end;
Jesse Barnes79e53942008-11-07 14:24:08 -0800240
241 return 0;
242}
Keith Packard6dbe2772008-10-14 21:41:13 -0700243
Eric Anholt673a3942008-07-30 12:06:12 -0700244int
245i915_gem_init_ioctl(struct drm_device *dev, void *data,
246 struct drm_file *file_priv)
247{
Eric Anholt673a3942008-07-30 12:06:12 -0700248 struct drm_i915_gem_init *args = data;
Jesse Barnes79e53942008-11-07 14:24:08 -0800249 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700250
251 mutex_lock(&dev->struct_mutex);
Daniel Vetter53984632010-09-22 23:44:24 +0200252 ret = i915_gem_do_init(dev, args->gtt_start, args->gtt_end, args->gtt_end);
Eric Anholt673a3942008-07-30 12:06:12 -0700253 mutex_unlock(&dev->struct_mutex);
254
Jesse Barnes79e53942008-11-07 14:24:08 -0800255 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700256}
257
Eric Anholt5a125c32008-10-22 21:40:13 -0700258int
259i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
260 struct drm_file *file_priv)
261{
Chris Wilson73aa8082010-09-30 11:46:12 +0100262 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt5a125c32008-10-22 21:40:13 -0700263 struct drm_i915_gem_get_aperture *args = data;
Eric Anholt5a125c32008-10-22 21:40:13 -0700264
265 if (!(dev->driver->driver_features & DRIVER_GEM))
266 return -ENODEV;
267
Chris Wilson73aa8082010-09-30 11:46:12 +0100268 mutex_lock(&dev->struct_mutex);
269 args->aper_size = dev_priv->mm.gtt_total;
270 args->aper_available_size = args->aper_size - dev_priv->mm.pin_memory;
271 mutex_unlock(&dev->struct_mutex);
Eric Anholt5a125c32008-10-22 21:40:13 -0700272
273 return 0;
274}
275
Eric Anholt673a3942008-07-30 12:06:12 -0700276
277/**
278 * Creates a new mm object and returns a handle to it.
279 */
280int
281i915_gem_create_ioctl(struct drm_device *dev, void *data,
282 struct drm_file *file_priv)
283{
284 struct drm_i915_gem_create *args = data;
285 struct drm_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300286 int ret;
287 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700288
289 args->size = roundup(args->size, PAGE_SIZE);
290
291 /* Allocate the new object */
Daniel Vetterac52bc52010-04-09 19:05:06 +0000292 obj = i915_gem_alloc_object(dev, args->size);
Eric Anholt673a3942008-07-30 12:06:12 -0700293 if (obj == NULL)
294 return -ENOMEM;
295
296 ret = drm_gem_handle_create(file_priv, obj, &handle);
Chris Wilson1dfd9752010-09-06 14:44:14 +0100297 if (ret) {
Chris Wilson202f2fe2010-10-14 13:20:40 +0100298 drm_gem_object_release(obj);
299 i915_gem_info_remove_obj(dev->dev_private, obj->size);
300 kfree(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700301 return ret;
Chris Wilson1dfd9752010-09-06 14:44:14 +0100302 }
303
Chris Wilson202f2fe2010-10-14 13:20:40 +0100304 /* drop reference from allocate - handle holds it now */
305 drm_gem_object_unreference(obj);
306 trace_i915_gem_object_create(obj);
307
Eric Anholt673a3942008-07-30 12:06:12 -0700308 args->handle = handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700309 return 0;
310}
311
Daniel Vetter16e809a2010-09-16 19:37:04 +0200312static bool
313i915_gem_object_cpu_accessible(struct drm_i915_gem_object *obj)
314{
315 struct drm_device *dev = obj->base.dev;
316 drm_i915_private_t *dev_priv = dev->dev_private;
317
318 return obj->gtt_space == NULL ||
319 obj->gtt_offset + obj->base.size <= dev_priv->mm.gtt_mappable_end;
320}
321
Eric Anholt280b7132009-03-12 16:56:27 -0700322static int i915_gem_object_needs_bit17_swizzle(struct drm_gem_object *obj)
323{
324 drm_i915_private_t *dev_priv = obj->dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +0100325 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt280b7132009-03-12 16:56:27 -0700326
327 return dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_9_10_17 &&
328 obj_priv->tiling_mode != I915_TILING_NONE;
329}
330
Chris Wilson99a03df2010-05-27 14:15:34 +0100331static inline void
Eric Anholt40123c12009-03-09 13:42:30 -0700332slow_shmem_copy(struct page *dst_page,
333 int dst_offset,
334 struct page *src_page,
335 int src_offset,
336 int length)
337{
338 char *dst_vaddr, *src_vaddr;
339
Chris Wilson99a03df2010-05-27 14:15:34 +0100340 dst_vaddr = kmap(dst_page);
341 src_vaddr = kmap(src_page);
Eric Anholt40123c12009-03-09 13:42:30 -0700342
343 memcpy(dst_vaddr + dst_offset, src_vaddr + src_offset, length);
344
Chris Wilson99a03df2010-05-27 14:15:34 +0100345 kunmap(src_page);
346 kunmap(dst_page);
Eric Anholt40123c12009-03-09 13:42:30 -0700347}
348
Chris Wilson99a03df2010-05-27 14:15:34 +0100349static inline void
Eric Anholt280b7132009-03-12 16:56:27 -0700350slow_shmem_bit17_copy(struct page *gpu_page,
351 int gpu_offset,
352 struct page *cpu_page,
353 int cpu_offset,
354 int length,
355 int is_read)
356{
357 char *gpu_vaddr, *cpu_vaddr;
358
359 /* Use the unswizzled path if this page isn't affected. */
360 if ((page_to_phys(gpu_page) & (1 << 17)) == 0) {
361 if (is_read)
362 return slow_shmem_copy(cpu_page, cpu_offset,
363 gpu_page, gpu_offset, length);
364 else
365 return slow_shmem_copy(gpu_page, gpu_offset,
366 cpu_page, cpu_offset, length);
367 }
368
Chris Wilson99a03df2010-05-27 14:15:34 +0100369 gpu_vaddr = kmap(gpu_page);
370 cpu_vaddr = kmap(cpu_page);
Eric Anholt280b7132009-03-12 16:56:27 -0700371
372 /* Copy the data, XORing A6 with A17 (1). The user already knows he's
373 * XORing with the other bits (A9 for Y, A9 and A10 for X)
374 */
375 while (length > 0) {
376 int cacheline_end = ALIGN(gpu_offset + 1, 64);
377 int this_length = min(cacheline_end - gpu_offset, length);
378 int swizzled_gpu_offset = gpu_offset ^ 64;
379
380 if (is_read) {
381 memcpy(cpu_vaddr + cpu_offset,
382 gpu_vaddr + swizzled_gpu_offset,
383 this_length);
384 } else {
385 memcpy(gpu_vaddr + swizzled_gpu_offset,
386 cpu_vaddr + cpu_offset,
387 this_length);
388 }
389 cpu_offset += this_length;
390 gpu_offset += this_length;
391 length -= this_length;
392 }
393
Chris Wilson99a03df2010-05-27 14:15:34 +0100394 kunmap(cpu_page);
395 kunmap(gpu_page);
Eric Anholt280b7132009-03-12 16:56:27 -0700396}
397
Eric Anholt673a3942008-07-30 12:06:12 -0700398/**
Eric Anholteb014592009-03-10 11:44:52 -0700399 * This is the fast shmem pread path, which attempts to copy_from_user directly
400 * from the backing pages of the object to the user's address space. On a
401 * fault, it fails so we can fall back to i915_gem_shmem_pwrite_slow().
402 */
403static int
404i915_gem_shmem_pread_fast(struct drm_device *dev, struct drm_gem_object *obj,
405 struct drm_i915_gem_pread *args,
406 struct drm_file *file_priv)
407{
Daniel Vetter23010e42010-03-08 13:35:02 +0100408 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsone5281cc2010-10-28 13:45:36 +0100409 struct address_space *mapping = obj->filp->f_path.dentry->d_inode->i_mapping;
Eric Anholteb014592009-03-10 11:44:52 -0700410 ssize_t remain;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100411 loff_t offset;
Eric Anholteb014592009-03-10 11:44:52 -0700412 char __user *user_data;
413 int page_offset, page_length;
Eric Anholteb014592009-03-10 11:44:52 -0700414
415 user_data = (char __user *) (uintptr_t) args->data_ptr;
416 remain = args->size;
417
Daniel Vetter23010e42010-03-08 13:35:02 +0100418 obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700419 offset = args->offset;
420
421 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100422 struct page *page;
423 char *vaddr;
424 int ret;
425
Eric Anholteb014592009-03-10 11:44:52 -0700426 /* Operation in this page
427 *
Eric Anholteb014592009-03-10 11:44:52 -0700428 * page_offset = offset within page
429 * page_length = bytes to copy for this page
430 */
Eric Anholteb014592009-03-10 11:44:52 -0700431 page_offset = offset & (PAGE_SIZE-1);
432 page_length = remain;
433 if ((page_offset + remain) > PAGE_SIZE)
434 page_length = PAGE_SIZE - page_offset;
435
Chris Wilsone5281cc2010-10-28 13:45:36 +0100436 page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
437 GFP_HIGHUSER | __GFP_RECLAIMABLE);
438 if (IS_ERR(page))
439 return PTR_ERR(page);
440
441 vaddr = kmap_atomic(page);
442 ret = __copy_to_user_inatomic(user_data,
443 vaddr + page_offset,
444 page_length);
445 kunmap_atomic(vaddr);
446
447 mark_page_accessed(page);
448 page_cache_release(page);
449 if (ret)
Chris Wilson4f27b752010-10-14 15:26:45 +0100450 return -EFAULT;
Eric Anholteb014592009-03-10 11:44:52 -0700451
452 remain -= page_length;
453 user_data += page_length;
454 offset += page_length;
455 }
456
Chris Wilson4f27b752010-10-14 15:26:45 +0100457 return 0;
Eric Anholteb014592009-03-10 11:44:52 -0700458}
459
460/**
461 * This is the fallback shmem pread path, which allocates temporary storage
462 * in kernel space to copy_to_user into outside of the struct_mutex, so we
463 * can copy out of the object's backing pages while holding the struct mutex
464 * and not take page faults.
465 */
466static int
467i915_gem_shmem_pread_slow(struct drm_device *dev, struct drm_gem_object *obj,
468 struct drm_i915_gem_pread *args,
469 struct drm_file *file_priv)
470{
Chris Wilsone5281cc2010-10-28 13:45:36 +0100471 struct address_space *mapping = obj->filp->f_path.dentry->d_inode->i_mapping;
Daniel Vetter23010e42010-03-08 13:35:02 +0100472 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700473 struct mm_struct *mm = current->mm;
474 struct page **user_pages;
475 ssize_t remain;
476 loff_t offset, pinned_pages, i;
477 loff_t first_data_page, last_data_page, num_pages;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100478 int shmem_page_offset;
479 int data_page_index, data_page_offset;
Eric Anholteb014592009-03-10 11:44:52 -0700480 int page_length;
481 int ret;
482 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700483 int do_bit17_swizzling;
Eric Anholteb014592009-03-10 11:44:52 -0700484
485 remain = args->size;
486
487 /* Pin the user pages containing the data. We can't fault while
488 * holding the struct mutex, yet we want to hold it while
489 * dereferencing the user data.
490 */
491 first_data_page = data_ptr / PAGE_SIZE;
492 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
493 num_pages = last_data_page - first_data_page + 1;
494
Chris Wilson4f27b752010-10-14 15:26:45 +0100495 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
Eric Anholteb014592009-03-10 11:44:52 -0700496 if (user_pages == NULL)
497 return -ENOMEM;
498
Chris Wilson4f27b752010-10-14 15:26:45 +0100499 mutex_unlock(&dev->struct_mutex);
Eric Anholteb014592009-03-10 11:44:52 -0700500 down_read(&mm->mmap_sem);
501 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
Eric Anholte5e9ecd2009-04-07 16:01:22 -0700502 num_pages, 1, 0, user_pages, NULL);
Eric Anholteb014592009-03-10 11:44:52 -0700503 up_read(&mm->mmap_sem);
Chris Wilson4f27b752010-10-14 15:26:45 +0100504 mutex_lock(&dev->struct_mutex);
Eric Anholteb014592009-03-10 11:44:52 -0700505 if (pinned_pages < num_pages) {
506 ret = -EFAULT;
Chris Wilson4f27b752010-10-14 15:26:45 +0100507 goto out;
Eric Anholteb014592009-03-10 11:44:52 -0700508 }
509
Chris Wilson4f27b752010-10-14 15:26:45 +0100510 ret = i915_gem_object_set_cpu_read_domain_range(obj,
511 args->offset,
Eric Anholteb014592009-03-10 11:44:52 -0700512 args->size);
Chris Wilson4f27b752010-10-14 15:26:45 +0100513 if (ret)
514 goto out;
515
516 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700517
Daniel Vetter23010e42010-03-08 13:35:02 +0100518 obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700519 offset = args->offset;
520
521 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100522 struct page *page;
523
Eric Anholteb014592009-03-10 11:44:52 -0700524 /* Operation in this page
525 *
Eric Anholteb014592009-03-10 11:44:52 -0700526 * shmem_page_offset = offset within page in shmem file
527 * data_page_index = page number in get_user_pages return
528 * data_page_offset = offset with data_page_index page.
529 * page_length = bytes to copy for this page
530 */
Eric Anholteb014592009-03-10 11:44:52 -0700531 shmem_page_offset = offset & ~PAGE_MASK;
532 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
533 data_page_offset = data_ptr & ~PAGE_MASK;
534
535 page_length = remain;
536 if ((shmem_page_offset + page_length) > PAGE_SIZE)
537 page_length = PAGE_SIZE - shmem_page_offset;
538 if ((data_page_offset + page_length) > PAGE_SIZE)
539 page_length = PAGE_SIZE - data_page_offset;
540
Chris Wilsone5281cc2010-10-28 13:45:36 +0100541 page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
542 GFP_HIGHUSER | __GFP_RECLAIMABLE);
543 if (IS_ERR(page))
544 return PTR_ERR(page);
545
Eric Anholt280b7132009-03-12 16:56:27 -0700546 if (do_bit17_swizzling) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100547 slow_shmem_bit17_copy(page,
Eric Anholt280b7132009-03-12 16:56:27 -0700548 shmem_page_offset,
Chris Wilson99a03df2010-05-27 14:15:34 +0100549 user_pages[data_page_index],
550 data_page_offset,
551 page_length,
552 1);
553 } else {
554 slow_shmem_copy(user_pages[data_page_index],
555 data_page_offset,
Chris Wilsone5281cc2010-10-28 13:45:36 +0100556 page,
Chris Wilson99a03df2010-05-27 14:15:34 +0100557 shmem_page_offset,
558 page_length);
Eric Anholt280b7132009-03-12 16:56:27 -0700559 }
Eric Anholteb014592009-03-10 11:44:52 -0700560
Chris Wilsone5281cc2010-10-28 13:45:36 +0100561 mark_page_accessed(page);
562 page_cache_release(page);
563
Eric Anholteb014592009-03-10 11:44:52 -0700564 remain -= page_length;
565 data_ptr += page_length;
566 offset += page_length;
567 }
568
Chris Wilson4f27b752010-10-14 15:26:45 +0100569out:
Eric Anholteb014592009-03-10 11:44:52 -0700570 for (i = 0; i < pinned_pages; i++) {
571 SetPageDirty(user_pages[i]);
Chris Wilsone5281cc2010-10-28 13:45:36 +0100572 mark_page_accessed(user_pages[i]);
Eric Anholteb014592009-03-10 11:44:52 -0700573 page_cache_release(user_pages[i]);
574 }
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700575 drm_free_large(user_pages);
Eric Anholteb014592009-03-10 11:44:52 -0700576
577 return ret;
578}
579
Eric Anholt673a3942008-07-30 12:06:12 -0700580/**
581 * Reads data from the object referenced by handle.
582 *
583 * On error, the contents of *data are undefined.
584 */
585int
586i915_gem_pread_ioctl(struct drm_device *dev, void *data,
587 struct drm_file *file_priv)
588{
589 struct drm_i915_gem_pread *args = data;
590 struct drm_gem_object *obj;
591 struct drm_i915_gem_object *obj_priv;
Chris Wilson35b62a82010-09-26 20:23:38 +0100592 int ret = 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700593
Chris Wilson4f27b752010-10-14 15:26:45 +0100594 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100595 if (ret)
Chris Wilson4f27b752010-10-14 15:26:45 +0100596 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700597
598 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100599 if (obj == NULL) {
600 ret = -ENOENT;
601 goto unlock;
Chris Wilson4f27b752010-10-14 15:26:45 +0100602 }
Daniel Vetter23010e42010-03-08 13:35:02 +0100603 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700604
Chris Wilson7dcd2492010-09-26 20:21:44 +0100605 /* Bounds check source. */
606 if (args->offset > obj->size || args->size > obj->size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +0100607 ret = -EINVAL;
Chris Wilson35b62a82010-09-26 20:23:38 +0100608 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +0100609 }
610
Chris Wilson35b62a82010-09-26 20:23:38 +0100611 if (args->size == 0)
612 goto out;
613
Chris Wilsonce9d4192010-09-26 20:50:05 +0100614 if (!access_ok(VERIFY_WRITE,
615 (char __user *)(uintptr_t)args->data_ptr,
616 args->size)) {
617 ret = -EFAULT;
Chris Wilson35b62a82010-09-26 20:23:38 +0100618 goto out;
Eric Anholt673a3942008-07-30 12:06:12 -0700619 }
620
Chris Wilsonb5e4feb2010-10-14 13:47:43 +0100621 ret = fault_in_pages_writeable((char __user *)(uintptr_t)args->data_ptr,
622 args->size);
623 if (ret) {
624 ret = -EFAULT;
625 goto out;
626 }
627
Chris Wilson4f27b752010-10-14 15:26:45 +0100628 ret = i915_gem_object_set_cpu_read_domain_range(obj,
629 args->offset,
630 args->size);
631 if (ret)
Chris Wilsone5281cc2010-10-28 13:45:36 +0100632 goto out;
Chris Wilson4f27b752010-10-14 15:26:45 +0100633
634 ret = -EFAULT;
635 if (!i915_gem_object_needs_bit17_swizzle(obj))
Eric Anholt280b7132009-03-12 16:56:27 -0700636 ret = i915_gem_shmem_pread_fast(dev, obj, args, file_priv);
Chris Wilson4f27b752010-10-14 15:26:45 +0100637 if (ret == -EFAULT)
638 ret = i915_gem_shmem_pread_slow(dev, obj, args, file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -0700639
Chris Wilson35b62a82010-09-26 20:23:38 +0100640out:
Chris Wilson4f27b752010-10-14 15:26:45 +0100641 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100642unlock:
Chris Wilson4f27b752010-10-14 15:26:45 +0100643 mutex_unlock(&dev->struct_mutex);
Eric Anholteb014592009-03-10 11:44:52 -0700644 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700645}
646
Keith Packard0839ccb2008-10-30 19:38:48 -0700647/* This is the fast write path which cannot handle
648 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700649 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700650
Keith Packard0839ccb2008-10-30 19:38:48 -0700651static inline int
652fast_user_write(struct io_mapping *mapping,
653 loff_t page_base, int page_offset,
654 char __user *user_data,
655 int length)
656{
657 char *vaddr_atomic;
658 unsigned long unwritten;
659
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700660 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
Keith Packard0839ccb2008-10-30 19:38:48 -0700661 unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
662 user_data, length);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700663 io_mapping_unmap_atomic(vaddr_atomic);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100664 return unwritten;
Keith Packard0839ccb2008-10-30 19:38:48 -0700665}
666
667/* Here's the write path which can sleep for
668 * page faults
669 */
670
Chris Wilsonab34c222010-05-27 14:15:35 +0100671static inline void
Eric Anholt3de09aa2009-03-09 09:42:23 -0700672slow_kernel_write(struct io_mapping *mapping,
673 loff_t gtt_base, int gtt_offset,
674 struct page *user_page, int user_offset,
675 int length)
Keith Packard0839ccb2008-10-30 19:38:48 -0700676{
Chris Wilsonab34c222010-05-27 14:15:35 +0100677 char __iomem *dst_vaddr;
678 char *src_vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -0700679
Chris Wilsonab34c222010-05-27 14:15:35 +0100680 dst_vaddr = io_mapping_map_wc(mapping, gtt_base);
681 src_vaddr = kmap(user_page);
682
683 memcpy_toio(dst_vaddr + gtt_offset,
684 src_vaddr + user_offset,
685 length);
686
687 kunmap(user_page);
688 io_mapping_unmap(dst_vaddr);
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700689}
690
Eric Anholt3de09aa2009-03-09 09:42:23 -0700691/**
692 * This is the fast pwrite path, where we copy the data directly from the
693 * user into the GTT, uncached.
694 */
Eric Anholt673a3942008-07-30 12:06:12 -0700695static int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700696i915_gem_gtt_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
697 struct drm_i915_gem_pwrite *args,
698 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700699{
Daniel Vetter23010e42010-03-08 13:35:02 +0100700 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Keith Packard0839ccb2008-10-30 19:38:48 -0700701 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700702 ssize_t remain;
Keith Packard0839ccb2008-10-30 19:38:48 -0700703 loff_t offset, page_base;
Eric Anholt673a3942008-07-30 12:06:12 -0700704 char __user *user_data;
Keith Packard0839ccb2008-10-30 19:38:48 -0700705 int page_offset, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700706
707 user_data = (char __user *) (uintptr_t) args->data_ptr;
708 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700709
Daniel Vetter23010e42010-03-08 13:35:02 +0100710 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700711 offset = obj_priv->gtt_offset + args->offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700712
713 while (remain > 0) {
714 /* Operation in this page
715 *
Keith Packard0839ccb2008-10-30 19:38:48 -0700716 * page_base = page offset within aperture
717 * page_offset = offset within page
718 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -0700719 */
Keith Packard0839ccb2008-10-30 19:38:48 -0700720 page_base = (offset & ~(PAGE_SIZE-1));
721 page_offset = offset & (PAGE_SIZE-1);
722 page_length = remain;
723 if ((page_offset + remain) > PAGE_SIZE)
724 page_length = PAGE_SIZE - page_offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700725
Keith Packard0839ccb2008-10-30 19:38:48 -0700726 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -0700727 * source page isn't available. Return the error and we'll
728 * retry in the slow path.
Keith Packard0839ccb2008-10-30 19:38:48 -0700729 */
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100730 if (fast_user_write(dev_priv->mm.gtt_mapping, page_base,
731 page_offset, user_data, page_length))
732
733 return -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -0700734
Keith Packard0839ccb2008-10-30 19:38:48 -0700735 remain -= page_length;
736 user_data += page_length;
737 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700738 }
Eric Anholt673a3942008-07-30 12:06:12 -0700739
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100740 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700741}
742
Eric Anholt3de09aa2009-03-09 09:42:23 -0700743/**
744 * This is the fallback GTT pwrite path, which uses get_user_pages to pin
745 * the memory and maps it using kmap_atomic for copying.
746 *
747 * This code resulted in x11perf -rgb10text consuming about 10% more CPU
748 * than using i915_gem_gtt_pwrite_fast on a G45 (32-bit).
749 */
Eric Anholt3043c602008-10-02 12:24:47 -0700750static int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700751i915_gem_gtt_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
752 struct drm_i915_gem_pwrite *args,
753 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700754{
Daniel Vetter23010e42010-03-08 13:35:02 +0100755 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700756 drm_i915_private_t *dev_priv = dev->dev_private;
757 ssize_t remain;
758 loff_t gtt_page_base, offset;
759 loff_t first_data_page, last_data_page, num_pages;
760 loff_t pinned_pages, i;
761 struct page **user_pages;
762 struct mm_struct *mm = current->mm;
763 int gtt_page_offset, data_page_offset, data_page_index, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700764 int ret;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700765 uint64_t data_ptr = args->data_ptr;
766
767 remain = args->size;
768
769 /* Pin the user pages containing the data. We can't fault while
770 * holding the struct mutex, and all of the pwrite implementations
771 * want to hold it while dereferencing the user data.
772 */
773 first_data_page = data_ptr / PAGE_SIZE;
774 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
775 num_pages = last_data_page - first_data_page + 1;
776
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100777 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
Eric Anholt3de09aa2009-03-09 09:42:23 -0700778 if (user_pages == NULL)
779 return -ENOMEM;
780
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100781 mutex_unlock(&dev->struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700782 down_read(&mm->mmap_sem);
783 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
784 num_pages, 0, 0, user_pages, NULL);
785 up_read(&mm->mmap_sem);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100786 mutex_lock(&dev->struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700787 if (pinned_pages < num_pages) {
788 ret = -EFAULT;
789 goto out_unpin_pages;
790 }
791
Eric Anholt3de09aa2009-03-09 09:42:23 -0700792 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
793 if (ret)
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100794 goto out_unpin_pages;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700795
Daniel Vetter23010e42010-03-08 13:35:02 +0100796 obj_priv = to_intel_bo(obj);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700797 offset = obj_priv->gtt_offset + args->offset;
798
799 while (remain > 0) {
800 /* Operation in this page
801 *
802 * gtt_page_base = page offset within aperture
803 * gtt_page_offset = offset within page in aperture
804 * data_page_index = page number in get_user_pages return
805 * data_page_offset = offset with data_page_index page.
806 * page_length = bytes to copy for this page
807 */
808 gtt_page_base = offset & PAGE_MASK;
809 gtt_page_offset = offset & ~PAGE_MASK;
810 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
811 data_page_offset = data_ptr & ~PAGE_MASK;
812
813 page_length = remain;
814 if ((gtt_page_offset + page_length) > PAGE_SIZE)
815 page_length = PAGE_SIZE - gtt_page_offset;
816 if ((data_page_offset + page_length) > PAGE_SIZE)
817 page_length = PAGE_SIZE - data_page_offset;
818
Chris Wilsonab34c222010-05-27 14:15:35 +0100819 slow_kernel_write(dev_priv->mm.gtt_mapping,
820 gtt_page_base, gtt_page_offset,
821 user_pages[data_page_index],
822 data_page_offset,
823 page_length);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700824
825 remain -= page_length;
826 offset += page_length;
827 data_ptr += page_length;
828 }
829
Eric Anholt3de09aa2009-03-09 09:42:23 -0700830out_unpin_pages:
831 for (i = 0; i < pinned_pages; i++)
832 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700833 drm_free_large(user_pages);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700834
835 return ret;
836}
837
Eric Anholt40123c12009-03-09 13:42:30 -0700838/**
839 * This is the fast shmem pwrite path, which attempts to directly
840 * copy_from_user into the kmapped pages backing the object.
841 */
Eric Anholt673a3942008-07-30 12:06:12 -0700842static int
Eric Anholt40123c12009-03-09 13:42:30 -0700843i915_gem_shmem_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
844 struct drm_i915_gem_pwrite *args,
845 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700846{
Chris Wilsone5281cc2010-10-28 13:45:36 +0100847 struct address_space *mapping = obj->filp->f_path.dentry->d_inode->i_mapping;
Daniel Vetter23010e42010-03-08 13:35:02 +0100848 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700849 ssize_t remain;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100850 loff_t offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700851 char __user *user_data;
852 int page_offset, page_length;
Eric Anholt40123c12009-03-09 13:42:30 -0700853
854 user_data = (char __user *) (uintptr_t) args->data_ptr;
855 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700856
Daniel Vetter23010e42010-03-08 13:35:02 +0100857 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700858 offset = args->offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700859 obj_priv->dirty = 1;
Eric Anholt673a3942008-07-30 12:06:12 -0700860
Eric Anholt40123c12009-03-09 13:42:30 -0700861 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100862 struct page *page;
863 char *vaddr;
864 int ret;
865
Eric Anholt40123c12009-03-09 13:42:30 -0700866 /* Operation in this page
867 *
Eric Anholt40123c12009-03-09 13:42:30 -0700868 * page_offset = offset within page
869 * page_length = bytes to copy for this page
870 */
Eric Anholt40123c12009-03-09 13:42:30 -0700871 page_offset = offset & (PAGE_SIZE-1);
872 page_length = remain;
873 if ((page_offset + remain) > PAGE_SIZE)
874 page_length = PAGE_SIZE - page_offset;
875
Chris Wilsone5281cc2010-10-28 13:45:36 +0100876 page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
877 GFP_HIGHUSER | __GFP_RECLAIMABLE);
878 if (IS_ERR(page))
879 return PTR_ERR(page);
880
881 vaddr = kmap_atomic(page, KM_USER0);
882 ret = __copy_from_user_inatomic(vaddr + page_offset,
883 user_data,
884 page_length);
885 kunmap_atomic(vaddr, KM_USER0);
886
887 set_page_dirty(page);
888 mark_page_accessed(page);
889 page_cache_release(page);
890
891 /* If we get a fault while copying data, then (presumably) our
892 * source page isn't available. Return the error and we'll
893 * retry in the slow path.
894 */
895 if (ret)
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100896 return -EFAULT;
Eric Anholt40123c12009-03-09 13:42:30 -0700897
898 remain -= page_length;
899 user_data += page_length;
900 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700901 }
902
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100903 return 0;
Eric Anholt40123c12009-03-09 13:42:30 -0700904}
905
906/**
907 * This is the fallback shmem pwrite path, which uses get_user_pages to pin
908 * the memory and maps it using kmap_atomic for copying.
909 *
910 * This avoids taking mmap_sem for faulting on the user's address while the
911 * struct_mutex is held.
912 */
913static int
914i915_gem_shmem_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
915 struct drm_i915_gem_pwrite *args,
916 struct drm_file *file_priv)
917{
Chris Wilsone5281cc2010-10-28 13:45:36 +0100918 struct address_space *mapping = obj->filp->f_path.dentry->d_inode->i_mapping;
Daniel Vetter23010e42010-03-08 13:35:02 +0100919 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700920 struct mm_struct *mm = current->mm;
921 struct page **user_pages;
922 ssize_t remain;
923 loff_t offset, pinned_pages, i;
924 loff_t first_data_page, last_data_page, num_pages;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100925 int shmem_page_offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700926 int data_page_index, data_page_offset;
927 int page_length;
928 int ret;
929 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700930 int do_bit17_swizzling;
Eric Anholt40123c12009-03-09 13:42:30 -0700931
932 remain = args->size;
933
934 /* Pin the user pages containing the data. We can't fault while
935 * holding the struct mutex, and all of the pwrite implementations
936 * want to hold it while dereferencing the user data.
937 */
938 first_data_page = data_ptr / PAGE_SIZE;
939 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
940 num_pages = last_data_page - first_data_page + 1;
941
Chris Wilson4f27b752010-10-14 15:26:45 +0100942 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
Eric Anholt40123c12009-03-09 13:42:30 -0700943 if (user_pages == NULL)
944 return -ENOMEM;
945
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100946 mutex_unlock(&dev->struct_mutex);
Eric Anholt40123c12009-03-09 13:42:30 -0700947 down_read(&mm->mmap_sem);
948 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
949 num_pages, 0, 0, user_pages, NULL);
950 up_read(&mm->mmap_sem);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100951 mutex_lock(&dev->struct_mutex);
Eric Anholt40123c12009-03-09 13:42:30 -0700952 if (pinned_pages < num_pages) {
953 ret = -EFAULT;
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100954 goto out;
Eric Anholt40123c12009-03-09 13:42:30 -0700955 }
956
Eric Anholt40123c12009-03-09 13:42:30 -0700957 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100958 if (ret)
959 goto out;
960
961 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700962
Daniel Vetter23010e42010-03-08 13:35:02 +0100963 obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700964 offset = args->offset;
965 obj_priv->dirty = 1;
966
967 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100968 struct page *page;
969
Eric Anholt40123c12009-03-09 13:42:30 -0700970 /* Operation in this page
971 *
Eric Anholt40123c12009-03-09 13:42:30 -0700972 * shmem_page_offset = offset within page in shmem file
973 * data_page_index = page number in get_user_pages return
974 * data_page_offset = offset with data_page_index page.
975 * page_length = bytes to copy for this page
976 */
Eric Anholt40123c12009-03-09 13:42:30 -0700977 shmem_page_offset = offset & ~PAGE_MASK;
978 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
979 data_page_offset = data_ptr & ~PAGE_MASK;
980
981 page_length = remain;
982 if ((shmem_page_offset + page_length) > PAGE_SIZE)
983 page_length = PAGE_SIZE - shmem_page_offset;
984 if ((data_page_offset + page_length) > PAGE_SIZE)
985 page_length = PAGE_SIZE - data_page_offset;
986
Chris Wilsone5281cc2010-10-28 13:45:36 +0100987 page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
988 GFP_HIGHUSER | __GFP_RECLAIMABLE);
989 if (IS_ERR(page)) {
990 ret = PTR_ERR(page);
991 goto out;
992 }
993
Eric Anholt280b7132009-03-12 16:56:27 -0700994 if (do_bit17_swizzling) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100995 slow_shmem_bit17_copy(page,
Eric Anholt280b7132009-03-12 16:56:27 -0700996 shmem_page_offset,
997 user_pages[data_page_index],
998 data_page_offset,
Chris Wilson99a03df2010-05-27 14:15:34 +0100999 page_length,
1000 0);
1001 } else {
Chris Wilsone5281cc2010-10-28 13:45:36 +01001002 slow_shmem_copy(page,
Chris Wilson99a03df2010-05-27 14:15:34 +01001003 shmem_page_offset,
1004 user_pages[data_page_index],
1005 data_page_offset,
1006 page_length);
Eric Anholt280b7132009-03-12 16:56:27 -07001007 }
Eric Anholt40123c12009-03-09 13:42:30 -07001008
Chris Wilsone5281cc2010-10-28 13:45:36 +01001009 set_page_dirty(page);
1010 mark_page_accessed(page);
1011 page_cache_release(page);
1012
Eric Anholt40123c12009-03-09 13:42:30 -07001013 remain -= page_length;
1014 data_ptr += page_length;
1015 offset += page_length;
1016 }
1017
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001018out:
Eric Anholt40123c12009-03-09 13:42:30 -07001019 for (i = 0; i < pinned_pages; i++)
1020 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07001021 drm_free_large(user_pages);
Eric Anholt40123c12009-03-09 13:42:30 -07001022
1023 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001024}
1025
1026/**
1027 * Writes data to the object referenced by handle.
1028 *
1029 * On error, the contents of the buffer that were to be modified are undefined.
1030 */
1031int
1032i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001033 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001034{
1035 struct drm_i915_gem_pwrite *args = data;
1036 struct drm_gem_object *obj;
1037 struct drm_i915_gem_object *obj_priv;
1038 int ret = 0;
1039
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001040 ret = i915_mutex_lock_interruptible(dev);
1041 if (ret)
1042 return ret;
1043
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001044 obj = drm_gem_object_lookup(dev, file, args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001045 if (obj == NULL) {
1046 ret = -ENOENT;
1047 goto unlock;
1048 }
Daniel Vetter23010e42010-03-08 13:35:02 +01001049 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001050
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001051
Chris Wilson7dcd2492010-09-26 20:21:44 +01001052 /* Bounds check destination. */
1053 if (args->offset > obj->size || args->size > obj->size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001054 ret = -EINVAL;
Chris Wilson35b62a82010-09-26 20:23:38 +01001055 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001056 }
1057
Chris Wilson35b62a82010-09-26 20:23:38 +01001058 if (args->size == 0)
1059 goto out;
1060
Chris Wilsonce9d4192010-09-26 20:50:05 +01001061 if (!access_ok(VERIFY_READ,
1062 (char __user *)(uintptr_t)args->data_ptr,
1063 args->size)) {
1064 ret = -EFAULT;
Chris Wilson35b62a82010-09-26 20:23:38 +01001065 goto out;
Eric Anholt673a3942008-07-30 12:06:12 -07001066 }
1067
Chris Wilsonb5e4feb2010-10-14 13:47:43 +01001068 ret = fault_in_pages_readable((char __user *)(uintptr_t)args->data_ptr,
1069 args->size);
1070 if (ret) {
1071 ret = -EFAULT;
1072 goto out;
Eric Anholt673a3942008-07-30 12:06:12 -07001073 }
1074
1075 /* We can only do the GTT pwrite on untiled buffers, as otherwise
1076 * it would end up going through the fenced access, and we'll get
1077 * different detiling behavior between reading and writing.
1078 * pread/pwrite currently are reading and writing from the CPU
1079 * perspective, requiring manual detiling by the client.
1080 */
Dave Airlie71acb5e2008-12-30 20:31:46 +10001081 if (obj_priv->phys_obj)
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001082 ret = i915_gem_phys_pwrite(dev, obj, args, file);
Dave Airlie71acb5e2008-12-30 20:31:46 +10001083 else if (obj_priv->tiling_mode == I915_TILING_NONE &&
Chris Wilson5cdf5882010-09-27 15:51:07 +01001084 obj_priv->gtt_space &&
Chris Wilson9b8c4a02010-05-27 14:21:01 +01001085 obj->write_domain != I915_GEM_DOMAIN_CPU) {
Daniel Vetter920afa72010-09-16 17:54:23 +02001086 ret = i915_gem_object_pin(obj, 0, true);
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001087 if (ret)
1088 goto out;
1089
1090 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
1091 if (ret)
1092 goto out_unpin;
1093
1094 ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file);
1095 if (ret == -EFAULT)
1096 ret = i915_gem_gtt_pwrite_slow(dev, obj, args, file);
1097
1098out_unpin:
1099 i915_gem_object_unpin(obj);
Eric Anholt40123c12009-03-09 13:42:30 -07001100 } else {
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001101 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
1102 if (ret)
Chris Wilsone5281cc2010-10-28 13:45:36 +01001103 goto out;
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001104
1105 ret = -EFAULT;
1106 if (!i915_gem_object_needs_bit17_swizzle(obj))
1107 ret = i915_gem_shmem_pwrite_fast(dev, obj, args, file);
1108 if (ret == -EFAULT)
1109 ret = i915_gem_shmem_pwrite_slow(dev, obj, args, file);
Eric Anholt40123c12009-03-09 13:42:30 -07001110 }
Eric Anholt673a3942008-07-30 12:06:12 -07001111
Chris Wilson35b62a82010-09-26 20:23:38 +01001112out:
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001113 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001114unlock:
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001115 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07001116 return ret;
1117}
1118
1119/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001120 * Called when user space prepares to use an object with the CPU, either
1121 * through the mmap ioctl's mapping or a GTT mapping.
Eric Anholt673a3942008-07-30 12:06:12 -07001122 */
1123int
1124i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
1125 struct drm_file *file_priv)
1126{
Eric Anholta09ba7f2009-08-29 12:49:51 -07001127 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07001128 struct drm_i915_gem_set_domain *args = data;
1129 struct drm_gem_object *obj;
Jesse Barnes652c3932009-08-17 13:31:43 -07001130 struct drm_i915_gem_object *obj_priv;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001131 uint32_t read_domains = args->read_domains;
1132 uint32_t write_domain = args->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07001133 int ret;
1134
1135 if (!(dev->driver->driver_features & DRIVER_GEM))
1136 return -ENODEV;
1137
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001138 /* Only handle setting domains to types used by the CPU. */
Chris Wilson21d509e2009-06-06 09:46:02 +01001139 if (write_domain & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001140 return -EINVAL;
1141
Chris Wilson21d509e2009-06-06 09:46:02 +01001142 if (read_domains & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001143 return -EINVAL;
1144
1145 /* Having something in the write domain implies it's in the read
1146 * domain, and only that read domain. Enforce that in the request.
1147 */
1148 if (write_domain != 0 && read_domains != write_domain)
1149 return -EINVAL;
1150
Chris Wilson76c1dec2010-09-25 11:22:51 +01001151 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001152 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001153 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001154
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001155 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1156 if (obj == NULL) {
1157 ret = -ENOENT;
1158 goto unlock;
Chris Wilson76c1dec2010-09-25 11:22:51 +01001159 }
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001160 obj_priv = to_intel_bo(obj);
Jesse Barnes652c3932009-08-17 13:31:43 -07001161
1162 intel_mark_busy(dev, obj);
1163
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001164 if (read_domains & I915_GEM_DOMAIN_GTT) {
1165 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
Eric Anholt02354392008-11-26 13:58:13 -08001166
Eric Anholta09ba7f2009-08-29 12:49:51 -07001167 /* Update the LRU on the fence for the CPU access that's
1168 * about to occur.
1169 */
1170 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001171 struct drm_i915_fence_reg *reg =
1172 &dev_priv->fence_regs[obj_priv->fence_reg];
1173 list_move_tail(&reg->lru_list,
Eric Anholta09ba7f2009-08-29 12:49:51 -07001174 &dev_priv->mm.fence_list);
1175 }
1176
Eric Anholt02354392008-11-26 13:58:13 -08001177 /* Silently promote "you're not bound, there was nothing to do"
1178 * to success, since the client was just asking us to
1179 * make sure everything was done.
1180 */
1181 if (ret == -EINVAL)
1182 ret = 0;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001183 } else {
Eric Anholte47c68e2008-11-14 13:35:19 -08001184 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001185 }
1186
Chris Wilson7d1c4802010-08-07 21:45:03 +01001187 /* Maintain LRU order of "inactive" objects */
1188 if (ret == 0 && i915_gem_object_is_inactive(obj_priv))
Chris Wilson69dc4982010-10-19 10:36:51 +01001189 list_move_tail(&obj_priv->mm_list, &dev_priv->mm.inactive_list);
Chris Wilson7d1c4802010-08-07 21:45:03 +01001190
Eric Anholt673a3942008-07-30 12:06:12 -07001191 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001192unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07001193 mutex_unlock(&dev->struct_mutex);
1194 return ret;
1195}
1196
1197/**
1198 * Called when user space has done writes to this buffer
1199 */
1200int
1201i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
1202 struct drm_file *file_priv)
1203{
1204 struct drm_i915_gem_sw_finish *args = data;
1205 struct drm_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001206 int ret = 0;
1207
1208 if (!(dev->driver->driver_features & DRIVER_GEM))
1209 return -ENODEV;
1210
Chris Wilson76c1dec2010-09-25 11:22:51 +01001211 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001212 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001213 return ret;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001214
Eric Anholt673a3942008-07-30 12:06:12 -07001215 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1216 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001217 ret = -ENOENT;
1218 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07001219 }
1220
Eric Anholt673a3942008-07-30 12:06:12 -07001221 /* Pinned buffers may be scanout, so flush the cache */
Chris Wilson3d2a8122010-09-29 11:39:53 +01001222 if (to_intel_bo(obj)->pin_count)
Eric Anholte47c68e2008-11-14 13:35:19 -08001223 i915_gem_object_flush_cpu_write_domain(obj);
1224
Eric Anholt673a3942008-07-30 12:06:12 -07001225 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001226unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07001227 mutex_unlock(&dev->struct_mutex);
1228 return ret;
1229}
1230
1231/**
1232 * Maps the contents of an object, returning the address it is mapped
1233 * into.
1234 *
1235 * While the mapping holds a reference on the contents of the object, it doesn't
1236 * imply a ref on the object itself.
1237 */
1238int
1239i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
1240 struct drm_file *file_priv)
1241{
Chris Wilsonda761a62010-10-27 17:37:08 +01001242 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07001243 struct drm_i915_gem_mmap *args = data;
1244 struct drm_gem_object *obj;
1245 loff_t offset;
1246 unsigned long addr;
1247
1248 if (!(dev->driver->driver_features & DRIVER_GEM))
1249 return -ENODEV;
1250
1251 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1252 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001253 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001254
Chris Wilsonda761a62010-10-27 17:37:08 +01001255 if (obj->size > dev_priv->mm.gtt_mappable_end) {
1256 drm_gem_object_unreference_unlocked(obj);
1257 return -E2BIG;
1258 }
1259
Eric Anholt673a3942008-07-30 12:06:12 -07001260 offset = args->offset;
1261
1262 down_write(&current->mm->mmap_sem);
1263 addr = do_mmap(obj->filp, 0, args->size,
1264 PROT_READ | PROT_WRITE, MAP_SHARED,
1265 args->offset);
1266 up_write(&current->mm->mmap_sem);
Luca Barbieribc9025b2010-02-09 05:49:12 +00001267 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001268 if (IS_ERR((void *)addr))
1269 return addr;
1270
1271 args->addr_ptr = (uint64_t) addr;
1272
1273 return 0;
1274}
1275
Jesse Barnesde151cf2008-11-12 10:03:55 -08001276/**
1277 * i915_gem_fault - fault a page into the GTT
1278 * vma: VMA in question
1279 * vmf: fault info
1280 *
1281 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1282 * from userspace. The fault handler takes care of binding the object to
1283 * the GTT (if needed), allocating and programming a fence register (again,
1284 * only if needed based on whether the old reg is still valid or the object
1285 * is tiled) and inserting a new PTE into the faulting process.
1286 *
1287 * Note that the faulting process may involve evicting existing objects
1288 * from the GTT and/or fence registers to make room. So performance may
1289 * suffer if the GTT working set is large or there are few fence registers
1290 * left.
1291 */
1292int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1293{
1294 struct drm_gem_object *obj = vma->vm_private_data;
1295 struct drm_device *dev = obj->dev;
Chris Wilson7d1c4802010-08-07 21:45:03 +01001296 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001297 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001298 pgoff_t page_offset;
1299 unsigned long pfn;
1300 int ret = 0;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001301 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001302
1303 /* We don't use vmf->pgoff since that has the fake offset */
1304 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1305 PAGE_SHIFT;
1306
1307 /* Now bind it into the GTT if needed */
1308 mutex_lock(&dev->struct_mutex);
Daniel Vetterfb7d5162010-10-01 22:05:20 +02001309 BUG_ON(obj_priv->pin_count && !obj_priv->pin_mappable);
Daniel Vetter16e809a2010-09-16 19:37:04 +02001310 if (!i915_gem_object_cpu_accessible(obj_priv))
1311 i915_gem_object_unbind(obj);
1312
Jesse Barnesde151cf2008-11-12 10:03:55 -08001313 if (!obj_priv->gtt_space) {
Daniel Vetter920afa72010-09-16 17:54:23 +02001314 ret = i915_gem_object_bind_to_gtt(obj, 0, true);
Chris Wilsonc7150892009-09-23 00:43:56 +01001315 if (ret)
1316 goto unlock;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001317 }
1318
Chris Wilson4a684a42010-10-28 14:44:08 +01001319 ret = i915_gem_object_set_to_gtt_domain(obj, write);
1320 if (ret)
1321 goto unlock;
1322
Daniel Vetterfb7d5162010-10-01 22:05:20 +02001323 if (!obj_priv->fault_mappable) {
1324 obj_priv->fault_mappable = true;
1325 i915_gem_info_update_mappable(dev_priv, obj, true);
1326 }
1327
Jesse Barnesde151cf2008-11-12 10:03:55 -08001328 /* Need a new fence register? */
Eric Anholta09ba7f2009-08-29 12:49:51 -07001329 if (obj_priv->tiling_mode != I915_TILING_NONE) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01001330 ret = i915_gem_object_get_fence_reg(obj, true);
Chris Wilsonc7150892009-09-23 00:43:56 +01001331 if (ret)
1332 goto unlock;
Eric Anholtd9ddcb92009-01-27 10:33:49 -08001333 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001334
Chris Wilson7d1c4802010-08-07 21:45:03 +01001335 if (i915_gem_object_is_inactive(obj_priv))
Chris Wilson69dc4982010-10-19 10:36:51 +01001336 list_move_tail(&obj_priv->mm_list, &dev_priv->mm.inactive_list);
Chris Wilson7d1c4802010-08-07 21:45:03 +01001337
Jesse Barnesde151cf2008-11-12 10:03:55 -08001338 pfn = ((dev->agp->base + obj_priv->gtt_offset) >> PAGE_SHIFT) +
1339 page_offset;
1340
1341 /* Finally, remap it using the new GTT offset */
1342 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
Chris Wilsonc7150892009-09-23 00:43:56 +01001343unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001344 mutex_unlock(&dev->struct_mutex);
1345
1346 switch (ret) {
Chris Wilsonc7150892009-09-23 00:43:56 +01001347 case 0:
1348 case -ERESTARTSYS:
1349 return VM_FAULT_NOPAGE;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001350 case -ENOMEM:
1351 case -EAGAIN:
1352 return VM_FAULT_OOM;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001353 default:
Chris Wilsonc7150892009-09-23 00:43:56 +01001354 return VM_FAULT_SIGBUS;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001355 }
1356}
1357
1358/**
1359 * i915_gem_create_mmap_offset - create a fake mmap offset for an object
1360 * @obj: obj in question
1361 *
1362 * GEM memory mapping works by handing back to userspace a fake mmap offset
1363 * it can use in a subsequent mmap(2) call. The DRM core code then looks
1364 * up the object based on the offset and sets up the various memory mapping
1365 * structures.
1366 *
1367 * This routine allocates and attaches a fake offset for @obj.
1368 */
1369static int
1370i915_gem_create_mmap_offset(struct drm_gem_object *obj)
1371{
1372 struct drm_device *dev = obj->dev;
1373 struct drm_gem_mm *mm = dev->mm_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001374 struct drm_map_list *list;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +11001375 struct drm_local_map *map;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001376 int ret = 0;
1377
1378 /* Set the object up for mmap'ing */
1379 list = &obj->map_list;
Eric Anholt9a298b22009-03-24 12:23:04 -07001380 list->map = kzalloc(sizeof(struct drm_map_list), GFP_KERNEL);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001381 if (!list->map)
1382 return -ENOMEM;
1383
1384 map = list->map;
1385 map->type = _DRM_GEM;
1386 map->size = obj->size;
1387 map->handle = obj;
1388
1389 /* Get a DRM GEM mmap offset allocated... */
1390 list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
1391 obj->size / PAGE_SIZE, 0, 0);
1392 if (!list->file_offset_node) {
1393 DRM_ERROR("failed to allocate offset for bo %d\n", obj->name);
Chris Wilson9e0ae5342010-09-21 15:05:24 +01001394 ret = -ENOSPC;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001395 goto out_free_list;
1396 }
1397
1398 list->file_offset_node = drm_mm_get_block(list->file_offset_node,
1399 obj->size / PAGE_SIZE, 0);
1400 if (!list->file_offset_node) {
1401 ret = -ENOMEM;
1402 goto out_free_list;
1403 }
1404
1405 list->hash.key = list->file_offset_node->start;
Chris Wilson9e0ae5342010-09-21 15:05:24 +01001406 ret = drm_ht_insert_item(&mm->offset_hash, &list->hash);
1407 if (ret) {
Jesse Barnesde151cf2008-11-12 10:03:55 -08001408 DRM_ERROR("failed to add to map hash\n");
1409 goto out_free_mm;
1410 }
1411
Jesse Barnesde151cf2008-11-12 10:03:55 -08001412 return 0;
1413
1414out_free_mm:
1415 drm_mm_put_block(list->file_offset_node);
1416out_free_list:
Eric Anholt9a298b22009-03-24 12:23:04 -07001417 kfree(list->map);
Chris Wilson39a01d12010-10-28 13:03:06 +01001418 list->map = NULL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001419
1420 return ret;
1421}
1422
Chris Wilson901782b2009-07-10 08:18:50 +01001423/**
1424 * i915_gem_release_mmap - remove physical page mappings
1425 * @obj: obj in question
1426 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001427 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01001428 * relinquish ownership of the pages back to the system.
1429 *
1430 * It is vital that we remove the page mapping if we have mapped a tiled
1431 * object through the GTT and then lose the fence register due to
1432 * resource pressure. Similarly if the object has been moved out of the
1433 * aperture, than pages mapped into userspace must be revoked. Removing the
1434 * mapping will then trigger a page fault on the next user access, allowing
1435 * fixup by i915_gem_fault().
1436 */
Eric Anholtd05ca302009-07-10 13:02:26 -07001437void
Chris Wilson901782b2009-07-10 08:18:50 +01001438i915_gem_release_mmap(struct drm_gem_object *obj)
1439{
1440 struct drm_device *dev = obj->dev;
Daniel Vetterfb7d5162010-10-01 22:05:20 +02001441 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001442 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson901782b2009-07-10 08:18:50 +01001443
Chris Wilson39a01d12010-10-28 13:03:06 +01001444 if (unlikely(obj->map_list.map && dev->dev_mapping))
Chris Wilson901782b2009-07-10 08:18:50 +01001445 unmap_mapping_range(dev->dev_mapping,
Chris Wilson39a01d12010-10-28 13:03:06 +01001446 (loff_t)obj->map_list.hash.key<<PAGE_SHIFT,
1447 obj->size, 1);
Daniel Vetterfb7d5162010-10-01 22:05:20 +02001448
1449 if (obj_priv->fault_mappable) {
1450 obj_priv->fault_mappable = false;
1451 i915_gem_info_update_mappable(dev_priv, obj, false);
1452 }
Chris Wilson901782b2009-07-10 08:18:50 +01001453}
1454
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001455static void
1456i915_gem_free_mmap_offset(struct drm_gem_object *obj)
1457{
1458 struct drm_device *dev = obj->dev;
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001459 struct drm_gem_mm *mm = dev->mm_private;
Chris Wilson39a01d12010-10-28 13:03:06 +01001460 struct drm_map_list *list = &obj->map_list;
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001461
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001462 drm_ht_remove_item(&mm->offset_hash, &list->hash);
Chris Wilson39a01d12010-10-28 13:03:06 +01001463 drm_mm_put_block(list->file_offset_node);
1464 kfree(list->map);
1465 list->map = NULL;
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001466}
1467
Jesse Barnesde151cf2008-11-12 10:03:55 -08001468/**
1469 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1470 * @obj: object to check
1471 *
1472 * Return the required GTT alignment for an object, taking into account
1473 * potential fence register mapping if needed.
1474 */
1475static uint32_t
1476i915_gem_get_gtt_alignment(struct drm_gem_object *obj)
1477{
1478 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001479 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001480 int start, i;
1481
1482 /*
1483 * Minimum alignment is 4k (GTT page size), but might be greater
1484 * if a fence register is needed for the object.
1485 */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001486 if (INTEL_INFO(dev)->gen >= 4 || obj_priv->tiling_mode == I915_TILING_NONE)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001487 return 4096;
1488
1489 /*
1490 * Previous chips need to be aligned to the size of the smallest
1491 * fence register that can contain the object.
1492 */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001493 if (INTEL_INFO(dev)->gen == 3)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001494 start = 1024*1024;
1495 else
1496 start = 512*1024;
1497
1498 for (i = start; i < obj->size; i <<= 1)
1499 ;
1500
1501 return i;
1502}
1503
1504/**
1505 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1506 * @dev: DRM device
1507 * @data: GTT mapping ioctl data
1508 * @file_priv: GEM object info
1509 *
1510 * Simply returns the fake offset to userspace so it can mmap it.
1511 * The mmap call will end up in drm_gem_mmap(), which will set things
1512 * up so we can get faults in the handler above.
1513 *
1514 * The fault handler will take care of binding the object into the GTT
1515 * (since it may have been evicted to make room for something), allocating
1516 * a fence register, and mapping the appropriate aperture address into
1517 * userspace.
1518 */
1519int
1520i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
1521 struct drm_file *file_priv)
1522{
Chris Wilsonda761a62010-10-27 17:37:08 +01001523 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001524 struct drm_i915_gem_mmap_gtt *args = data;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001525 struct drm_gem_object *obj;
1526 struct drm_i915_gem_object *obj_priv;
1527 int ret;
1528
1529 if (!(dev->driver->driver_features & DRIVER_GEM))
1530 return -ENODEV;
1531
Chris Wilson76c1dec2010-09-25 11:22:51 +01001532 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001533 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001534 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001535
Jesse Barnesde151cf2008-11-12 10:03:55 -08001536 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001537 if (obj == NULL) {
1538 ret = -ENOENT;
1539 goto unlock;
1540 }
Daniel Vetter23010e42010-03-08 13:35:02 +01001541 obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001542
Chris Wilsonda761a62010-10-27 17:37:08 +01001543 if (obj->size > dev_priv->mm.gtt_mappable_end) {
1544 ret = -E2BIG;
1545 goto unlock;
1546 }
1547
Chris Wilsonab182822009-09-22 18:46:17 +01001548 if (obj_priv->madv != I915_MADV_WILLNEED) {
1549 DRM_ERROR("Attempting to mmap a purgeable buffer\n");
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001550 ret = -EINVAL;
1551 goto out;
Chris Wilsonab182822009-09-22 18:46:17 +01001552 }
1553
Chris Wilson39a01d12010-10-28 13:03:06 +01001554 if (!obj->map_list.map) {
Jesse Barnesde151cf2008-11-12 10:03:55 -08001555 ret = i915_gem_create_mmap_offset(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001556 if (ret)
1557 goto out;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001558 }
1559
Chris Wilson39a01d12010-10-28 13:03:06 +01001560 args->offset = (u64)obj->map_list.hash.key << PAGE_SHIFT;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001561
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001562out:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001563 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001564unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001565 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001566 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001567}
1568
Chris Wilsone5281cc2010-10-28 13:45:36 +01001569static int
1570i915_gem_object_get_pages_gtt(struct drm_gem_object *obj,
1571 gfp_t gfpmask)
1572{
1573 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
1574 int page_count, i;
1575 struct address_space *mapping;
1576 struct inode *inode;
1577 struct page *page;
1578
1579 /* Get the list of pages out of our struct file. They'll be pinned
1580 * at this point until we release them.
1581 */
1582 page_count = obj->size / PAGE_SIZE;
1583 BUG_ON(obj_priv->pages != NULL);
1584 obj_priv->pages = drm_malloc_ab(page_count, sizeof(struct page *));
1585 if (obj_priv->pages == NULL)
1586 return -ENOMEM;
1587
1588 inode = obj->filp->f_path.dentry->d_inode;
1589 mapping = inode->i_mapping;
1590 for (i = 0; i < page_count; i++) {
1591 page = read_cache_page_gfp(mapping, i,
1592 GFP_HIGHUSER |
1593 __GFP_COLD |
1594 __GFP_RECLAIMABLE |
1595 gfpmask);
1596 if (IS_ERR(page))
1597 goto err_pages;
1598
1599 obj_priv->pages[i] = page;
1600 }
1601
1602 if (obj_priv->tiling_mode != I915_TILING_NONE)
1603 i915_gem_object_do_bit_17_swizzle(obj);
1604
1605 return 0;
1606
1607err_pages:
1608 while (i--)
1609 page_cache_release(obj_priv->pages[i]);
1610
1611 drm_free_large(obj_priv->pages);
1612 obj_priv->pages = NULL;
1613 return PTR_ERR(page);
1614}
1615
Chris Wilson5cdf5882010-09-27 15:51:07 +01001616static void
Chris Wilsone5281cc2010-10-28 13:45:36 +01001617i915_gem_object_put_pages_gtt(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07001618{
Daniel Vetter23010e42010-03-08 13:35:02 +01001619 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001620 int page_count = obj->size / PAGE_SIZE;
1621 int i;
1622
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001623 BUG_ON(obj_priv->madv == __I915_MADV_PURGED);
Eric Anholt856fa192009-03-19 14:10:50 -07001624
Eric Anholt280b7132009-03-12 16:56:27 -07001625 if (obj_priv->tiling_mode != I915_TILING_NONE)
1626 i915_gem_object_save_bit_17_swizzle(obj);
1627
Chris Wilson3ef94da2009-09-14 16:50:29 +01001628 if (obj_priv->madv == I915_MADV_DONTNEED)
Chris Wilson13a05fd2009-09-20 23:03:19 +01001629 obj_priv->dirty = 0;
Chris Wilson3ef94da2009-09-14 16:50:29 +01001630
1631 for (i = 0; i < page_count; i++) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01001632 if (obj_priv->dirty)
1633 set_page_dirty(obj_priv->pages[i]);
1634
1635 if (obj_priv->madv == I915_MADV_WILLNEED)
Eric Anholt856fa192009-03-19 14:10:50 -07001636 mark_page_accessed(obj_priv->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001637
1638 page_cache_release(obj_priv->pages[i]);
1639 }
Eric Anholt673a3942008-07-30 12:06:12 -07001640 obj_priv->dirty = 0;
1641
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07001642 drm_free_large(obj_priv->pages);
Eric Anholt856fa192009-03-19 14:10:50 -07001643 obj_priv->pages = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001644}
1645
Chris Wilsona56ba562010-09-28 10:07:56 +01001646static uint32_t
1647i915_gem_next_request_seqno(struct drm_device *dev,
1648 struct intel_ring_buffer *ring)
1649{
1650 drm_i915_private_t *dev_priv = dev->dev_private;
1651
1652 ring->outstanding_lazy_request = true;
1653 return dev_priv->next_seqno;
1654}
1655
Eric Anholt673a3942008-07-30 12:06:12 -07001656static void
Daniel Vetter617dbe22010-02-11 22:16:02 +01001657i915_gem_object_move_to_active(struct drm_gem_object *obj,
Zou Nan hai852835f2010-05-21 09:08:56 +08001658 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001659{
1660 struct drm_device *dev = obj->dev;
Chris Wilson69dc4982010-10-19 10:36:51 +01001661 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001662 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsona56ba562010-09-28 10:07:56 +01001663 uint32_t seqno = i915_gem_next_request_seqno(dev, ring);
Daniel Vetter617dbe22010-02-11 22:16:02 +01001664
Zou Nan hai852835f2010-05-21 09:08:56 +08001665 BUG_ON(ring == NULL);
1666 obj_priv->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001667
1668 /* Add a reference if we're newly entering the active list. */
1669 if (!obj_priv->active) {
1670 drm_gem_object_reference(obj);
1671 obj_priv->active = 1;
1672 }
Daniel Vettere35a41d2010-02-11 22:13:59 +01001673
Eric Anholt673a3942008-07-30 12:06:12 -07001674 /* Move from whatever list we were on to the tail of execution. */
Chris Wilson69dc4982010-10-19 10:36:51 +01001675 list_move_tail(&obj_priv->mm_list, &dev_priv->mm.active_list);
1676 list_move_tail(&obj_priv->ring_list, &ring->active_list);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001677 obj_priv->last_rendering_seqno = seqno;
Eric Anholt673a3942008-07-30 12:06:12 -07001678}
1679
Eric Anholtce44b0e2008-11-06 16:00:31 -08001680static void
1681i915_gem_object_move_to_flushing(struct drm_gem_object *obj)
1682{
1683 struct drm_device *dev = obj->dev;
1684 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001685 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001686
1687 BUG_ON(!obj_priv->active);
Chris Wilson69dc4982010-10-19 10:36:51 +01001688 list_move_tail(&obj_priv->mm_list, &dev_priv->mm.flushing_list);
1689 list_del_init(&obj_priv->ring_list);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001690 obj_priv->last_rendering_seqno = 0;
1691}
Eric Anholt673a3942008-07-30 12:06:12 -07001692
Chris Wilson963b4832009-09-20 23:03:54 +01001693/* Immediately discard the backing storage */
1694static void
1695i915_gem_object_truncate(struct drm_gem_object *obj)
1696{
Daniel Vetter23010e42010-03-08 13:35:02 +01001697 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001698 struct inode *inode;
Chris Wilson963b4832009-09-20 23:03:54 +01001699
Chris Wilsonae9fed62010-08-07 11:01:30 +01001700 /* Our goal here is to return as much of the memory as
1701 * is possible back to the system as we are called from OOM.
1702 * To do this we must instruct the shmfs to drop all of its
1703 * backing pages, *now*. Here we mirror the actions taken
1704 * when by shmem_delete_inode() to release the backing store.
1705 */
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001706 inode = obj->filp->f_path.dentry->d_inode;
Chris Wilsonae9fed62010-08-07 11:01:30 +01001707 truncate_inode_pages(inode->i_mapping, 0);
1708 if (inode->i_op->truncate_range)
1709 inode->i_op->truncate_range(inode, 0, (loff_t)-1);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001710
1711 obj_priv->madv = __I915_MADV_PURGED;
Chris Wilson963b4832009-09-20 23:03:54 +01001712}
1713
1714static inline int
1715i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj_priv)
1716{
1717 return obj_priv->madv == I915_MADV_DONTNEED;
1718}
1719
Eric Anholt673a3942008-07-30 12:06:12 -07001720static void
1721i915_gem_object_move_to_inactive(struct drm_gem_object *obj)
1722{
1723 struct drm_device *dev = obj->dev;
1724 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001725 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001726
Eric Anholt673a3942008-07-30 12:06:12 -07001727 if (obj_priv->pin_count != 0)
Chris Wilson69dc4982010-10-19 10:36:51 +01001728 list_move_tail(&obj_priv->mm_list, &dev_priv->mm.pinned_list);
Eric Anholt673a3942008-07-30 12:06:12 -07001729 else
Chris Wilson69dc4982010-10-19 10:36:51 +01001730 list_move_tail(&obj_priv->mm_list, &dev_priv->mm.inactive_list);
1731 list_del_init(&obj_priv->ring_list);
Eric Anholt673a3942008-07-30 12:06:12 -07001732
Daniel Vetter99fcb762010-02-07 16:20:18 +01001733 BUG_ON(!list_empty(&obj_priv->gpu_write_list));
1734
Eric Anholtce44b0e2008-11-06 16:00:31 -08001735 obj_priv->last_rendering_seqno = 0;
Zou Nan hai852835f2010-05-21 09:08:56 +08001736 obj_priv->ring = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001737 if (obj_priv->active) {
1738 obj_priv->active = 0;
1739 drm_gem_object_unreference(obj);
1740 }
Chris Wilson23bc5982010-09-29 16:10:57 +01001741 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07001742}
1743
Daniel Vetter63560392010-02-19 11:51:59 +01001744static void
1745i915_gem_process_flushing_list(struct drm_device *dev,
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001746 uint32_t flush_domains,
Zou Nan hai852835f2010-05-21 09:08:56 +08001747 struct intel_ring_buffer *ring)
Daniel Vetter63560392010-02-19 11:51:59 +01001748{
1749 drm_i915_private_t *dev_priv = dev->dev_private;
1750 struct drm_i915_gem_object *obj_priv, *next;
1751
1752 list_for_each_entry_safe(obj_priv, next,
Chris Wilson64193402010-10-24 12:38:05 +01001753 &ring->gpu_write_list,
Daniel Vetter63560392010-02-19 11:51:59 +01001754 gpu_write_list) {
Daniel Vettera8089e82010-04-09 19:05:09 +00001755 struct drm_gem_object *obj = &obj_priv->base;
Daniel Vetter63560392010-02-19 11:51:59 +01001756
Chris Wilson64193402010-10-24 12:38:05 +01001757 if (obj->write_domain & flush_domains) {
Daniel Vetter63560392010-02-19 11:51:59 +01001758 uint32_t old_write_domain = obj->write_domain;
1759
1760 obj->write_domain = 0;
1761 list_del_init(&obj_priv->gpu_write_list);
Daniel Vetter617dbe22010-02-11 22:16:02 +01001762 i915_gem_object_move_to_active(obj, ring);
Daniel Vetter63560392010-02-19 11:51:59 +01001763
1764 /* update the fence lru list */
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001765 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
1766 struct drm_i915_fence_reg *reg =
1767 &dev_priv->fence_regs[obj_priv->fence_reg];
1768 list_move_tail(&reg->lru_list,
Daniel Vetter63560392010-02-19 11:51:59 +01001769 &dev_priv->mm.fence_list);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001770 }
Daniel Vetter63560392010-02-19 11:51:59 +01001771
1772 trace_i915_gem_object_change_domain(obj,
1773 obj->read_domains,
1774 old_write_domain);
1775 }
1776 }
1777}
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001778
Chris Wilson3cce4692010-10-27 16:11:02 +01001779int
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001780i915_add_request(struct drm_device *dev,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001781 struct drm_file *file,
Chris Wilson8dc5d142010-08-12 12:36:12 +01001782 struct drm_i915_gem_request *request,
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001783 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001784{
1785 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001786 struct drm_i915_file_private *file_priv = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001787 uint32_t seqno;
1788 int was_empty;
Chris Wilson3cce4692010-10-27 16:11:02 +01001789 int ret;
1790
1791 BUG_ON(request == NULL);
Eric Anholt673a3942008-07-30 12:06:12 -07001792
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001793 if (file != NULL)
1794 file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00001795
Chris Wilson3cce4692010-10-27 16:11:02 +01001796 ret = ring->add_request(ring, &seqno);
1797 if (ret)
1798 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001799
Chris Wilsona56ba562010-09-28 10:07:56 +01001800 ring->outstanding_lazy_request = false;
Eric Anholt673a3942008-07-30 12:06:12 -07001801
1802 request->seqno = seqno;
Zou Nan hai852835f2010-05-21 09:08:56 +08001803 request->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001804 request->emitted_jiffies = jiffies;
Zou Nan hai852835f2010-05-21 09:08:56 +08001805 was_empty = list_empty(&ring->request_list);
1806 list_add_tail(&request->list, &ring->request_list);
1807
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001808 if (file_priv) {
Chris Wilson1c255952010-09-26 11:03:27 +01001809 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001810 request->file_priv = file_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00001811 list_add_tail(&request->client_list,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001812 &file_priv->mm.request_list);
Chris Wilson1c255952010-09-26 11:03:27 +01001813 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00001814 }
Eric Anholt673a3942008-07-30 12:06:12 -07001815
Ben Gamarif65d9422009-09-14 17:48:44 -04001816 if (!dev_priv->mm.suspended) {
Chris Wilsonb3b079d2010-09-13 23:44:34 +01001817 mod_timer(&dev_priv->hangcheck_timer,
1818 jiffies + msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
Ben Gamarif65d9422009-09-14 17:48:44 -04001819 if (was_empty)
Chris Wilsonb3b079d2010-09-13 23:44:34 +01001820 queue_delayed_work(dev_priv->wq,
1821 &dev_priv->mm.retire_work, HZ);
Ben Gamarif65d9422009-09-14 17:48:44 -04001822 }
Chris Wilson3cce4692010-10-27 16:11:02 +01001823 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001824}
1825
1826/**
1827 * Command execution barrier
1828 *
1829 * Ensures that all commands in the ring are finished
1830 * before signalling the CPU
1831 */
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001832static void
Zou Nan hai852835f2010-05-21 09:08:56 +08001833i915_retire_commands(struct drm_device *dev, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001834{
Eric Anholt673a3942008-07-30 12:06:12 -07001835 uint32_t flush_domains = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001836
1837 /* The sampler always gets flushed on i965 (sigh) */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001838 if (INTEL_INFO(dev)->gen >= 4)
Eric Anholt673a3942008-07-30 12:06:12 -07001839 flush_domains |= I915_GEM_DOMAIN_SAMPLER;
Zou Nan hai852835f2010-05-21 09:08:56 +08001840
Chris Wilson78501ea2010-10-27 12:18:21 +01001841 ring->flush(ring, I915_GEM_DOMAIN_COMMAND, flush_domains);
Eric Anholt673a3942008-07-30 12:06:12 -07001842}
1843
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001844static inline void
1845i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
Eric Anholt673a3942008-07-30 12:06:12 -07001846{
Chris Wilson1c255952010-09-26 11:03:27 +01001847 struct drm_i915_file_private *file_priv = request->file_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07001848
Chris Wilson1c255952010-09-26 11:03:27 +01001849 if (!file_priv)
1850 return;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001851
Chris Wilson1c255952010-09-26 11:03:27 +01001852 spin_lock(&file_priv->mm.lock);
1853 list_del(&request->client_list);
1854 request->file_priv = NULL;
1855 spin_unlock(&file_priv->mm.lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001856}
1857
Chris Wilsondfaae392010-09-22 10:31:52 +01001858static void i915_gem_reset_ring_lists(struct drm_i915_private *dev_priv,
1859 struct intel_ring_buffer *ring)
Chris Wilson9375e442010-09-19 12:21:28 +01001860{
Chris Wilsondfaae392010-09-22 10:31:52 +01001861 while (!list_empty(&ring->request_list)) {
1862 struct drm_i915_gem_request *request;
Chris Wilson9375e442010-09-19 12:21:28 +01001863
Chris Wilsondfaae392010-09-22 10:31:52 +01001864 request = list_first_entry(&ring->request_list,
1865 struct drm_i915_gem_request,
1866 list);
1867
1868 list_del(&request->list);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001869 i915_gem_request_remove_from_client(request);
Chris Wilsondfaae392010-09-22 10:31:52 +01001870 kfree(request);
1871 }
1872
1873 while (!list_empty(&ring->active_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001874 struct drm_i915_gem_object *obj_priv;
1875
Chris Wilsondfaae392010-09-22 10:31:52 +01001876 obj_priv = list_first_entry(&ring->active_list,
Eric Anholt673a3942008-07-30 12:06:12 -07001877 struct drm_i915_gem_object,
Chris Wilson69dc4982010-10-19 10:36:51 +01001878 ring_list);
Eric Anholt673a3942008-07-30 12:06:12 -07001879
Chris Wilsondfaae392010-09-22 10:31:52 +01001880 obj_priv->base.write_domain = 0;
1881 list_del_init(&obj_priv->gpu_write_list);
1882 i915_gem_object_move_to_inactive(&obj_priv->base);
Eric Anholt673a3942008-07-30 12:06:12 -07001883 }
Eric Anholt673a3942008-07-30 12:06:12 -07001884}
1885
Chris Wilson069efc12010-09-30 16:53:18 +01001886void i915_gem_reset(struct drm_device *dev)
Eric Anholt673a3942008-07-30 12:06:12 -07001887{
Chris Wilsondfaae392010-09-22 10:31:52 +01001888 struct drm_i915_private *dev_priv = dev->dev_private;
1889 struct drm_i915_gem_object *obj_priv;
Chris Wilson069efc12010-09-30 16:53:18 +01001890 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07001891
Chris Wilsondfaae392010-09-22 10:31:52 +01001892 i915_gem_reset_ring_lists(dev_priv, &dev_priv->render_ring);
Chris Wilson87acb0a2010-10-19 10:13:00 +01001893 i915_gem_reset_ring_lists(dev_priv, &dev_priv->bsd_ring);
Chris Wilson549f7362010-10-19 11:19:32 +01001894 i915_gem_reset_ring_lists(dev_priv, &dev_priv->blt_ring);
Chris Wilsondfaae392010-09-22 10:31:52 +01001895
1896 /* Remove anything from the flushing lists. The GPU cache is likely
1897 * to be lost on reset along with the data, so simply move the
1898 * lost bo to the inactive list.
1899 */
1900 while (!list_empty(&dev_priv->mm.flushing_list)) {
Chris Wilson9375e442010-09-19 12:21:28 +01001901 obj_priv = list_first_entry(&dev_priv->mm.flushing_list,
1902 struct drm_i915_gem_object,
Chris Wilson69dc4982010-10-19 10:36:51 +01001903 mm_list);
Chris Wilson9375e442010-09-19 12:21:28 +01001904
1905 obj_priv->base.write_domain = 0;
Chris Wilsondfaae392010-09-22 10:31:52 +01001906 list_del_init(&obj_priv->gpu_write_list);
Chris Wilson9375e442010-09-19 12:21:28 +01001907 i915_gem_object_move_to_inactive(&obj_priv->base);
1908 }
Chris Wilson9375e442010-09-19 12:21:28 +01001909
Chris Wilsondfaae392010-09-22 10:31:52 +01001910 /* Move everything out of the GPU domains to ensure we do any
1911 * necessary invalidation upon reuse.
1912 */
Chris Wilson77f01232010-09-19 12:31:36 +01001913 list_for_each_entry(obj_priv,
1914 &dev_priv->mm.inactive_list,
Chris Wilson69dc4982010-10-19 10:36:51 +01001915 mm_list)
Chris Wilson77f01232010-09-19 12:31:36 +01001916 {
1917 obj_priv->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
1918 }
Chris Wilson069efc12010-09-30 16:53:18 +01001919
1920 /* The fence registers are invalidated so clear them out */
1921 for (i = 0; i < 16; i++) {
1922 struct drm_i915_fence_reg *reg;
1923
1924 reg = &dev_priv->fence_regs[i];
1925 if (!reg->obj)
1926 continue;
1927
1928 i915_gem_clear_fence_reg(reg->obj);
1929 }
Eric Anholt673a3942008-07-30 12:06:12 -07001930}
1931
1932/**
1933 * This function clears the request list as sequence numbers are passed.
1934 */
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001935static void
1936i915_gem_retire_requests_ring(struct drm_device *dev,
1937 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001938{
1939 drm_i915_private_t *dev_priv = dev->dev_private;
1940 uint32_t seqno;
1941
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001942 if (!ring->status_page.page_addr ||
1943 list_empty(&ring->request_list))
Karsten Wiese6c0594a2009-02-23 15:07:57 +01001944 return;
1945
Chris Wilson23bc5982010-09-29 16:10:57 +01001946 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07001947
Chris Wilson78501ea2010-10-27 12:18:21 +01001948 seqno = ring->get_seqno(ring);
Zou Nan hai852835f2010-05-21 09:08:56 +08001949 while (!list_empty(&ring->request_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001950 struct drm_i915_gem_request *request;
Eric Anholt673a3942008-07-30 12:06:12 -07001951
Zou Nan hai852835f2010-05-21 09:08:56 +08001952 request = list_first_entry(&ring->request_list,
Eric Anholt673a3942008-07-30 12:06:12 -07001953 struct drm_i915_gem_request,
1954 list);
Eric Anholt673a3942008-07-30 12:06:12 -07001955
Chris Wilsondfaae392010-09-22 10:31:52 +01001956 if (!i915_seqno_passed(seqno, request->seqno))
Eric Anholt673a3942008-07-30 12:06:12 -07001957 break;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001958
1959 trace_i915_gem_request_retire(dev, request->seqno);
1960
1961 list_del(&request->list);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001962 i915_gem_request_remove_from_client(request);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001963 kfree(request);
1964 }
1965
1966 /* Move any buffers on the active list that are no longer referenced
1967 * by the ringbuffer to the flushing/inactive lists as appropriate.
1968 */
1969 while (!list_empty(&ring->active_list)) {
1970 struct drm_gem_object *obj;
1971 struct drm_i915_gem_object *obj_priv;
1972
1973 obj_priv = list_first_entry(&ring->active_list,
1974 struct drm_i915_gem_object,
Chris Wilson69dc4982010-10-19 10:36:51 +01001975 ring_list);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001976
Chris Wilsondfaae392010-09-22 10:31:52 +01001977 if (!i915_seqno_passed(seqno, obj_priv->last_rendering_seqno))
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001978 break;
1979
1980 obj = &obj_priv->base;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001981 if (obj->write_domain != 0)
1982 i915_gem_object_move_to_flushing(obj);
1983 else
1984 i915_gem_object_move_to_inactive(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001985 }
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001986
1987 if (unlikely (dev_priv->trace_irq_seqno &&
1988 i915_seqno_passed(dev_priv->trace_irq_seqno, seqno))) {
Chris Wilson78501ea2010-10-27 12:18:21 +01001989 ring->user_irq_put(ring);
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001990 dev_priv->trace_irq_seqno = 0;
1991 }
Chris Wilson23bc5982010-09-29 16:10:57 +01001992
1993 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07001994}
1995
1996void
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001997i915_gem_retire_requests(struct drm_device *dev)
1998{
1999 drm_i915_private_t *dev_priv = dev->dev_private;
2000
Chris Wilsonbe726152010-07-23 23:18:50 +01002001 if (!list_empty(&dev_priv->mm.deferred_free_list)) {
2002 struct drm_i915_gem_object *obj_priv, *tmp;
2003
2004 /* We must be careful that during unbind() we do not
2005 * accidentally infinitely recurse into retire requests.
2006 * Currently:
2007 * retire -> free -> unbind -> wait -> retire_ring
2008 */
2009 list_for_each_entry_safe(obj_priv, tmp,
2010 &dev_priv->mm.deferred_free_list,
Chris Wilson69dc4982010-10-19 10:36:51 +01002011 mm_list)
Chris Wilsonbe726152010-07-23 23:18:50 +01002012 i915_gem_free_object_tail(&obj_priv->base);
2013 }
2014
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002015 i915_gem_retire_requests_ring(dev, &dev_priv->render_ring);
Chris Wilson87acb0a2010-10-19 10:13:00 +01002016 i915_gem_retire_requests_ring(dev, &dev_priv->bsd_ring);
Chris Wilson549f7362010-10-19 11:19:32 +01002017 i915_gem_retire_requests_ring(dev, &dev_priv->blt_ring);
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002018}
2019
Daniel Vetter75ef9da2010-08-21 00:25:16 +02002020static void
Eric Anholt673a3942008-07-30 12:06:12 -07002021i915_gem_retire_work_handler(struct work_struct *work)
2022{
2023 drm_i915_private_t *dev_priv;
2024 struct drm_device *dev;
2025
2026 dev_priv = container_of(work, drm_i915_private_t,
2027 mm.retire_work.work);
2028 dev = dev_priv->dev;
2029
Chris Wilson891b48c2010-09-29 12:26:37 +01002030 /* Come back later if the device is busy... */
2031 if (!mutex_trylock(&dev->struct_mutex)) {
2032 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
2033 return;
2034 }
2035
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002036 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08002037
Keith Packard6dbe2772008-10-14 21:41:13 -07002038 if (!dev_priv->mm.suspended &&
Zou Nan haid1b851f2010-05-21 09:08:57 +08002039 (!list_empty(&dev_priv->render_ring.request_list) ||
Chris Wilson549f7362010-10-19 11:19:32 +01002040 !list_empty(&dev_priv->bsd_ring.request_list) ||
2041 !list_empty(&dev_priv->blt_ring.request_list)))
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07002042 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
Eric Anholt673a3942008-07-30 12:06:12 -07002043 mutex_unlock(&dev->struct_mutex);
2044}
2045
Daniel Vetter5a5a0c62009-09-15 22:57:36 +02002046int
Zou Nan hai852835f2010-05-21 09:08:56 +08002047i915_do_wait_request(struct drm_device *dev, uint32_t seqno,
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002048 bool interruptible, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07002049{
2050 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes802c7eb2009-05-05 16:03:48 -07002051 u32 ier;
Eric Anholt673a3942008-07-30 12:06:12 -07002052 int ret = 0;
2053
2054 BUG_ON(seqno == 0);
2055
Ben Gamariba1234d2009-09-14 17:48:47 -04002056 if (atomic_read(&dev_priv->mm.wedged))
Chris Wilson30dbf0c2010-09-25 10:19:17 +01002057 return -EAGAIN;
Ben Gamariffed1d02009-09-14 17:48:41 -04002058
Chris Wilsona56ba562010-09-28 10:07:56 +01002059 if (ring->outstanding_lazy_request) {
Chris Wilson3cce4692010-10-27 16:11:02 +01002060 struct drm_i915_gem_request *request;
2061
2062 request = kzalloc(sizeof(*request), GFP_KERNEL);
2063 if (request == NULL)
Daniel Vettere35a41d2010-02-11 22:13:59 +01002064 return -ENOMEM;
Chris Wilson3cce4692010-10-27 16:11:02 +01002065
2066 ret = i915_add_request(dev, NULL, request, ring);
2067 if (ret) {
2068 kfree(request);
2069 return ret;
2070 }
2071
2072 seqno = request->seqno;
Daniel Vettere35a41d2010-02-11 22:13:59 +01002073 }
Chris Wilsona56ba562010-09-28 10:07:56 +01002074 BUG_ON(seqno == dev_priv->next_seqno);
Daniel Vettere35a41d2010-02-11 22:13:59 +01002075
Chris Wilson78501ea2010-10-27 12:18:21 +01002076 if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) {
Eric Anholtbad720f2009-10-22 16:11:14 -07002077 if (HAS_PCH_SPLIT(dev))
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002078 ier = I915_READ(DEIER) | I915_READ(GTIER);
2079 else
2080 ier = I915_READ(IER);
Jesse Barnes802c7eb2009-05-05 16:03:48 -07002081 if (!ier) {
2082 DRM_ERROR("something (likely vbetool) disabled "
2083 "interrupts, re-enabling\n");
2084 i915_driver_irq_preinstall(dev);
2085 i915_driver_irq_postinstall(dev);
2086 }
2087
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002088 trace_i915_gem_request_wait_begin(dev, seqno);
2089
Chris Wilsonb2223492010-10-27 15:27:33 +01002090 ring->waiting_seqno = seqno;
Chris Wilson78501ea2010-10-27 12:18:21 +01002091 ring->user_irq_get(ring);
Daniel Vetter48764bf2009-09-15 22:57:32 +02002092 if (interruptible)
Zou Nan hai852835f2010-05-21 09:08:56 +08002093 ret = wait_event_interruptible(ring->irq_queue,
Chris Wilson78501ea2010-10-27 12:18:21 +01002094 i915_seqno_passed(ring->get_seqno(ring), seqno)
Zou Nan hai852835f2010-05-21 09:08:56 +08002095 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02002096 else
Zou Nan hai852835f2010-05-21 09:08:56 +08002097 wait_event(ring->irq_queue,
Chris Wilson78501ea2010-10-27 12:18:21 +01002098 i915_seqno_passed(ring->get_seqno(ring), seqno)
Zou Nan hai852835f2010-05-21 09:08:56 +08002099 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02002100
Chris Wilson78501ea2010-10-27 12:18:21 +01002101 ring->user_irq_put(ring);
Chris Wilsonb2223492010-10-27 15:27:33 +01002102 ring->waiting_seqno = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002103
2104 trace_i915_gem_request_wait_end(dev, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07002105 }
Ben Gamariba1234d2009-09-14 17:48:47 -04002106 if (atomic_read(&dev_priv->mm.wedged))
Chris Wilson30dbf0c2010-09-25 10:19:17 +01002107 ret = -EAGAIN;
Eric Anholt673a3942008-07-30 12:06:12 -07002108
2109 if (ret && ret != -ERESTARTSYS)
Daniel Vetter8bff9172010-02-11 22:19:40 +01002110 DRM_ERROR("%s returns %d (awaiting %d at %d, next %d)\n",
Chris Wilson78501ea2010-10-27 12:18:21 +01002111 __func__, ret, seqno, ring->get_seqno(ring),
Daniel Vetter8bff9172010-02-11 22:19:40 +01002112 dev_priv->next_seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07002113
2114 /* Directly dispatch request retiring. While we have the work queue
2115 * to handle this, the waiter on a request often wants an associated
2116 * buffer to have made it to the inactive list, and we would need
2117 * a separate wait queue to handle that.
2118 */
2119 if (ret == 0)
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002120 i915_gem_retire_requests_ring(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07002121
2122 return ret;
2123}
2124
Daniel Vetter48764bf2009-09-15 22:57:32 +02002125/**
2126 * Waits for a sequence number to be signaled, and cleans up the
2127 * request and object lists appropriately for that event.
2128 */
2129static int
Zou Nan hai852835f2010-05-21 09:08:56 +08002130i915_wait_request(struct drm_device *dev, uint32_t seqno,
Chris Wilsona56ba562010-09-28 10:07:56 +01002131 struct intel_ring_buffer *ring)
Daniel Vetter48764bf2009-09-15 22:57:32 +02002132{
Zou Nan hai852835f2010-05-21 09:08:56 +08002133 return i915_do_wait_request(dev, seqno, 1, ring);
Daniel Vetter48764bf2009-09-15 22:57:32 +02002134}
2135
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002136static void
Chris Wilson92204342010-09-18 11:02:01 +01002137i915_gem_flush_ring(struct drm_device *dev,
Chris Wilsonc78ec302010-09-20 12:50:23 +01002138 struct drm_file *file_priv,
Chris Wilson92204342010-09-18 11:02:01 +01002139 struct intel_ring_buffer *ring,
2140 uint32_t invalidate_domains,
2141 uint32_t flush_domains)
2142{
Chris Wilson78501ea2010-10-27 12:18:21 +01002143 ring->flush(ring, invalidate_domains, flush_domains);
Chris Wilson92204342010-09-18 11:02:01 +01002144 i915_gem_process_flushing_list(dev, flush_domains, ring);
2145}
2146
2147static void
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002148i915_gem_flush(struct drm_device *dev,
Chris Wilsonc78ec302010-09-20 12:50:23 +01002149 struct drm_file *file_priv,
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002150 uint32_t invalidate_domains,
Chris Wilson92204342010-09-18 11:02:01 +01002151 uint32_t flush_domains,
2152 uint32_t flush_rings)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002153{
2154 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter8bff9172010-02-11 22:19:40 +01002155
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002156 if (flush_domains & I915_GEM_DOMAIN_CPU)
2157 drm_agp_chipset_flush(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08002158
Chris Wilson92204342010-09-18 11:02:01 +01002159 if ((flush_domains | invalidate_domains) & I915_GEM_GPU_DOMAINS) {
2160 if (flush_rings & RING_RENDER)
Chris Wilsonc78ec302010-09-20 12:50:23 +01002161 i915_gem_flush_ring(dev, file_priv,
Chris Wilson92204342010-09-18 11:02:01 +01002162 &dev_priv->render_ring,
2163 invalidate_domains, flush_domains);
2164 if (flush_rings & RING_BSD)
Chris Wilsonc78ec302010-09-20 12:50:23 +01002165 i915_gem_flush_ring(dev, file_priv,
Chris Wilson92204342010-09-18 11:02:01 +01002166 &dev_priv->bsd_ring,
2167 invalidate_domains, flush_domains);
Chris Wilson549f7362010-10-19 11:19:32 +01002168 if (flush_rings & RING_BLT)
2169 i915_gem_flush_ring(dev, file_priv,
2170 &dev_priv->blt_ring,
2171 invalidate_domains, flush_domains);
Chris Wilson92204342010-09-18 11:02:01 +01002172 }
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002173}
2174
Eric Anholt673a3942008-07-30 12:06:12 -07002175/**
2176 * Ensures that all rendering to the object has completed and the object is
2177 * safe to unbind from the GTT or access from the CPU.
2178 */
2179static int
Chris Wilson2cf34d72010-09-14 13:03:28 +01002180i915_gem_object_wait_rendering(struct drm_gem_object *obj,
2181 bool interruptible)
Eric Anholt673a3942008-07-30 12:06:12 -07002182{
2183 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01002184 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002185 int ret;
2186
Eric Anholte47c68e2008-11-14 13:35:19 -08002187 /* This function only exists to support waiting for existing rendering,
2188 * not for emitting required flushes.
Eric Anholt673a3942008-07-30 12:06:12 -07002189 */
Eric Anholte47c68e2008-11-14 13:35:19 -08002190 BUG_ON((obj->write_domain & I915_GEM_GPU_DOMAINS) != 0);
Eric Anholt673a3942008-07-30 12:06:12 -07002191
2192 /* If there is rendering queued on the buffer being evicted, wait for
2193 * it.
2194 */
2195 if (obj_priv->active) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01002196 ret = i915_do_wait_request(dev,
2197 obj_priv->last_rendering_seqno,
2198 interruptible,
2199 obj_priv->ring);
2200 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002201 return ret;
2202 }
2203
2204 return 0;
2205}
2206
2207/**
2208 * Unbinds an object from the GTT aperture.
2209 */
Jesse Barnes0f973f22009-01-26 17:10:45 -08002210int
Eric Anholt673a3942008-07-30 12:06:12 -07002211i915_gem_object_unbind(struct drm_gem_object *obj)
2212{
2213 struct drm_device *dev = obj->dev;
Chris Wilson73aa8082010-09-30 11:46:12 +01002214 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002215 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002216 int ret = 0;
2217
Eric Anholt673a3942008-07-30 12:06:12 -07002218 if (obj_priv->gtt_space == NULL)
2219 return 0;
2220
2221 if (obj_priv->pin_count != 0) {
2222 DRM_ERROR("Attempting to unbind pinned buffer\n");
2223 return -EINVAL;
2224 }
2225
Eric Anholt5323fd02009-09-09 11:50:45 -07002226 /* blow away mappings if mapped through GTT */
2227 i915_gem_release_mmap(obj);
2228
Eric Anholt673a3942008-07-30 12:06:12 -07002229 /* Move the object to the CPU domain to ensure that
2230 * any possible CPU writes while it's not in the GTT
2231 * are flushed when we go to remap it. This will
2232 * also ensure that all pending GPU writes are finished
2233 * before we unbind.
2234 */
Eric Anholte47c68e2008-11-14 13:35:19 -08002235 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Chris Wilson8dc17752010-07-23 23:18:51 +01002236 if (ret == -ERESTARTSYS)
Eric Anholt673a3942008-07-30 12:06:12 -07002237 return ret;
Chris Wilson8dc17752010-07-23 23:18:51 +01002238 /* Continue on if we fail due to EIO, the GPU is hung so we
2239 * should be safe and we need to cleanup or else we might
2240 * cause memory corruption through use-after-free.
2241 */
Chris Wilson812ed4922010-09-30 15:08:57 +01002242 if (ret) {
2243 i915_gem_clflush_object(obj);
2244 obj->read_domains = obj->write_domain = I915_GEM_DOMAIN_CPU;
2245 }
Eric Anholt673a3942008-07-30 12:06:12 -07002246
Daniel Vetter96b47b62009-12-15 17:50:00 +01002247 /* release the fence reg _after_ flushing */
2248 if (obj_priv->fence_reg != I915_FENCE_REG_NONE)
2249 i915_gem_clear_fence_reg(obj);
2250
Chris Wilson73aa8082010-09-30 11:46:12 +01002251 drm_unbind_agp(obj_priv->agp_mem);
2252 drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002253
Chris Wilsone5281cc2010-10-28 13:45:36 +01002254 i915_gem_object_put_pages_gtt(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002255
Daniel Vetterfb7d5162010-10-01 22:05:20 +02002256 i915_gem_info_remove_gtt(dev_priv, obj);
Chris Wilson69dc4982010-10-19 10:36:51 +01002257 list_del_init(&obj_priv->mm_list);
Eric Anholt673a3942008-07-30 12:06:12 -07002258
Chris Wilson73aa8082010-09-30 11:46:12 +01002259 drm_mm_put_block(obj_priv->gtt_space);
2260 obj_priv->gtt_space = NULL;
Chris Wilson9af90d12010-10-17 10:01:56 +01002261 obj_priv->gtt_offset = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002262
Chris Wilson963b4832009-09-20 23:03:54 +01002263 if (i915_gem_object_is_purgeable(obj_priv))
2264 i915_gem_object_truncate(obj);
2265
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002266 trace_i915_gem_object_unbind(obj);
2267
Chris Wilson8dc17752010-07-23 23:18:51 +01002268 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002269}
2270
Chris Wilsona56ba562010-09-28 10:07:56 +01002271static int i915_ring_idle(struct drm_device *dev,
2272 struct intel_ring_buffer *ring)
2273{
Chris Wilson64193402010-10-24 12:38:05 +01002274 if (list_empty(&ring->gpu_write_list))
2275 return 0;
2276
Chris Wilsona56ba562010-09-28 10:07:56 +01002277 i915_gem_flush_ring(dev, NULL, ring,
2278 I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
2279 return i915_wait_request(dev,
2280 i915_gem_next_request_seqno(dev, ring),
2281 ring);
2282}
2283
Chris Wilsonb47eb4a2010-08-07 11:01:23 +01002284int
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002285i915_gpu_idle(struct drm_device *dev)
2286{
2287 drm_i915_private_t *dev_priv = dev->dev_private;
2288 bool lists_empty;
Zou Nan hai852835f2010-05-21 09:08:56 +08002289 int ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002290
Zou Nan haid1b851f2010-05-21 09:08:57 +08002291 lists_empty = (list_empty(&dev_priv->mm.flushing_list) &&
2292 list_empty(&dev_priv->render_ring.active_list) &&
Chris Wilson549f7362010-10-19 11:19:32 +01002293 list_empty(&dev_priv->bsd_ring.active_list) &&
2294 list_empty(&dev_priv->blt_ring.active_list));
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002295 if (lists_empty)
2296 return 0;
2297
2298 /* Flush everything onto the inactive list. */
Chris Wilsona56ba562010-09-28 10:07:56 +01002299 ret = i915_ring_idle(dev, &dev_priv->render_ring);
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002300 if (ret)
2301 return ret;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002302
Chris Wilson87acb0a2010-10-19 10:13:00 +01002303 ret = i915_ring_idle(dev, &dev_priv->bsd_ring);
2304 if (ret)
2305 return ret;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002306
Chris Wilson549f7362010-10-19 11:19:32 +01002307 ret = i915_ring_idle(dev, &dev_priv->blt_ring);
2308 if (ret)
2309 return ret;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002310
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002311 return 0;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002312}
2313
Eric Anholt4e901fd2009-10-26 16:44:17 -07002314static void sandybridge_write_fence_reg(struct drm_i915_fence_reg *reg)
2315{
2316 struct drm_gem_object *obj = reg->obj;
2317 struct drm_device *dev = obj->dev;
2318 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002319 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt4e901fd2009-10-26 16:44:17 -07002320 int regnum = obj_priv->fence_reg;
2321 uint64_t val;
2322
2323 val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
2324 0xfffff000) << 32;
2325 val |= obj_priv->gtt_offset & 0xfffff000;
2326 val |= (uint64_t)((obj_priv->stride / 128) - 1) <<
2327 SANDYBRIDGE_FENCE_PITCH_SHIFT;
2328
2329 if (obj_priv->tiling_mode == I915_TILING_Y)
2330 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2331 val |= I965_FENCE_REG_VALID;
2332
2333 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (regnum * 8), val);
2334}
2335
Jesse Barnesde151cf2008-11-12 10:03:55 -08002336static void i965_write_fence_reg(struct drm_i915_fence_reg *reg)
2337{
2338 struct drm_gem_object *obj = reg->obj;
2339 struct drm_device *dev = obj->dev;
2340 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002341 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002342 int regnum = obj_priv->fence_reg;
2343 uint64_t val;
2344
2345 val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
2346 0xfffff000) << 32;
2347 val |= obj_priv->gtt_offset & 0xfffff000;
2348 val |= ((obj_priv->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
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_965_0 + (regnum * 8), val);
2354}
2355
2356static void i915_write_fence_reg(struct drm_i915_fence_reg *reg)
2357{
2358 struct drm_gem_object *obj = reg->obj;
2359 struct drm_device *dev = obj->dev;
2360 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002361 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002362 int regnum = obj_priv->fence_reg;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002363 int tile_width;
Eric Anholtdc529a42009-03-10 22:34:49 -07002364 uint32_t fence_reg, val;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002365 uint32_t pitch_val;
2366
2367 if ((obj_priv->gtt_offset & ~I915_FENCE_START_MASK) ||
2368 (obj_priv->gtt_offset & (obj->size - 1))) {
Linus Torvaldsf06da262009-02-09 08:57:29 -08002369 WARN(1, "%s: object 0x%08x not 1M or size (0x%zx) aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002370 __func__, obj_priv->gtt_offset, obj->size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002371 return;
2372 }
2373
Jesse Barnes0f973f22009-01-26 17:10:45 -08002374 if (obj_priv->tiling_mode == I915_TILING_Y &&
2375 HAS_128_BYTE_Y_TILING(dev))
2376 tile_width = 128;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002377 else
Jesse Barnes0f973f22009-01-26 17:10:45 -08002378 tile_width = 512;
2379
2380 /* Note: pitch better be a power of two tile widths */
2381 pitch_val = obj_priv->stride / tile_width;
2382 pitch_val = ffs(pitch_val) - 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002383
Daniel Vetterc36a2a62010-04-17 15:12:03 +02002384 if (obj_priv->tiling_mode == I915_TILING_Y &&
2385 HAS_128_BYTE_Y_TILING(dev))
2386 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2387 else
2388 WARN_ON(pitch_val > I915_FENCE_MAX_PITCH_VAL);
2389
Jesse Barnesde151cf2008-11-12 10:03:55 -08002390 val = obj_priv->gtt_offset;
2391 if (obj_priv->tiling_mode == I915_TILING_Y)
2392 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2393 val |= I915_FENCE_SIZE_BITS(obj->size);
2394 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2395 val |= I830_FENCE_REG_VALID;
2396
Eric Anholtdc529a42009-03-10 22:34:49 -07002397 if (regnum < 8)
2398 fence_reg = FENCE_REG_830_0 + (regnum * 4);
2399 else
2400 fence_reg = FENCE_REG_945_8 + ((regnum - 8) * 4);
2401 I915_WRITE(fence_reg, val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002402}
2403
2404static void i830_write_fence_reg(struct drm_i915_fence_reg *reg)
2405{
2406 struct drm_gem_object *obj = reg->obj;
2407 struct drm_device *dev = obj->dev;
2408 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002409 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002410 int regnum = obj_priv->fence_reg;
2411 uint32_t val;
2412 uint32_t pitch_val;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002413 uint32_t fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002414
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002415 if ((obj_priv->gtt_offset & ~I830_FENCE_START_MASK) ||
Jesse Barnesde151cf2008-11-12 10:03:55 -08002416 (obj_priv->gtt_offset & (obj->size - 1))) {
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002417 WARN(1, "%s: object 0x%08x not 512K or size aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002418 __func__, obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002419 return;
2420 }
2421
Eric Anholte76a16d2009-05-26 17:44:56 -07002422 pitch_val = obj_priv->stride / 128;
2423 pitch_val = ffs(pitch_val) - 1;
2424 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2425
Jesse Barnesde151cf2008-11-12 10:03:55 -08002426 val = obj_priv->gtt_offset;
2427 if (obj_priv->tiling_mode == I915_TILING_Y)
2428 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002429 fence_size_bits = I830_FENCE_SIZE_BITS(obj->size);
2430 WARN_ON(fence_size_bits & ~0x00000f00);
2431 val |= fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002432 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2433 val |= I830_FENCE_REG_VALID;
2434
2435 I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002436}
2437
Chris Wilson2cf34d72010-09-14 13:03:28 +01002438static int i915_find_fence_reg(struct drm_device *dev,
2439 bool interruptible)
Daniel Vetterae3db242010-02-19 11:51:58 +01002440{
2441 struct drm_i915_fence_reg *reg = NULL;
2442 struct drm_i915_gem_object *obj_priv = NULL;
2443 struct drm_i915_private *dev_priv = dev->dev_private;
2444 struct drm_gem_object *obj = NULL;
2445 int i, avail, ret;
2446
2447 /* First try to find a free reg */
2448 avail = 0;
2449 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2450 reg = &dev_priv->fence_regs[i];
2451 if (!reg->obj)
2452 return i;
2453
Daniel Vetter23010e42010-03-08 13:35:02 +01002454 obj_priv = to_intel_bo(reg->obj);
Daniel Vetterae3db242010-02-19 11:51:58 +01002455 if (!obj_priv->pin_count)
2456 avail++;
2457 }
2458
2459 if (avail == 0)
2460 return -ENOSPC;
2461
2462 /* None available, try to steal one or wait for a user to finish */
2463 i = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002464 list_for_each_entry(reg, &dev_priv->mm.fence_list,
2465 lru_list) {
2466 obj = reg->obj;
2467 obj_priv = to_intel_bo(obj);
Daniel Vetterae3db242010-02-19 11:51:58 +01002468
2469 if (obj_priv->pin_count)
2470 continue;
2471
2472 /* found one! */
2473 i = obj_priv->fence_reg;
2474 break;
2475 }
2476
2477 BUG_ON(i == I915_FENCE_REG_NONE);
2478
2479 /* We only have a reference on obj from the active list. put_fence_reg
2480 * might drop that one, causing a use-after-free in it. So hold a
2481 * private reference to obj like the other callers of put_fence_reg
2482 * (set_tiling ioctl) do. */
2483 drm_gem_object_reference(obj);
Chris Wilson2cf34d72010-09-14 13:03:28 +01002484 ret = i915_gem_object_put_fence_reg(obj, interruptible);
Daniel Vetterae3db242010-02-19 11:51:58 +01002485 drm_gem_object_unreference(obj);
2486 if (ret != 0)
2487 return ret;
2488
2489 return i;
2490}
2491
Jesse Barnesde151cf2008-11-12 10:03:55 -08002492/**
2493 * i915_gem_object_get_fence_reg - set up a fence reg for an object
2494 * @obj: object to map through a fence reg
2495 *
2496 * When mapping objects through the GTT, userspace wants to be able to write
2497 * to them without having to worry about swizzling if the object is tiled.
2498 *
2499 * This function walks the fence regs looking for a free one for @obj,
2500 * stealing one if it can't find any.
2501 *
2502 * It then sets up the reg based on the object's properties: address, pitch
2503 * and tiling format.
2504 */
Chris Wilson8c4b8c32009-06-17 22:08:52 +01002505int
Chris Wilson2cf34d72010-09-14 13:03:28 +01002506i915_gem_object_get_fence_reg(struct drm_gem_object *obj,
2507 bool interruptible)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002508{
2509 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002510 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002511 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002512 struct drm_i915_fence_reg *reg = NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002513 int ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002514
Eric Anholta09ba7f2009-08-29 12:49:51 -07002515 /* Just update our place in the LRU if our fence is getting used. */
2516 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002517 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
2518 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002519 return 0;
2520 }
2521
Jesse Barnesde151cf2008-11-12 10:03:55 -08002522 switch (obj_priv->tiling_mode) {
2523 case I915_TILING_NONE:
2524 WARN(1, "allocating a fence for non-tiled object?\n");
2525 break;
2526 case I915_TILING_X:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002527 if (!obj_priv->stride)
2528 return -EINVAL;
2529 WARN((obj_priv->stride & (512 - 1)),
2530 "object 0x%08x is X tiled but has non-512B pitch\n",
2531 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002532 break;
2533 case I915_TILING_Y:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002534 if (!obj_priv->stride)
2535 return -EINVAL;
2536 WARN((obj_priv->stride & (128 - 1)),
2537 "object 0x%08x is Y tiled but has non-128B pitch\n",
2538 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002539 break;
2540 }
2541
Chris Wilson2cf34d72010-09-14 13:03:28 +01002542 ret = i915_find_fence_reg(dev, interruptible);
Daniel Vetterae3db242010-02-19 11:51:58 +01002543 if (ret < 0)
2544 return ret;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002545
Daniel Vetterae3db242010-02-19 11:51:58 +01002546 obj_priv->fence_reg = ret;
2547 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002548 list_add_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002549
Jesse Barnesde151cf2008-11-12 10:03:55 -08002550 reg->obj = obj;
2551
Chris Wilsone259bef2010-09-17 00:32:02 +01002552 switch (INTEL_INFO(dev)->gen) {
2553 case 6:
Eric Anholt4e901fd2009-10-26 16:44:17 -07002554 sandybridge_write_fence_reg(reg);
Chris Wilsone259bef2010-09-17 00:32:02 +01002555 break;
2556 case 5:
2557 case 4:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002558 i965_write_fence_reg(reg);
Chris Wilsone259bef2010-09-17 00:32:02 +01002559 break;
2560 case 3:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002561 i915_write_fence_reg(reg);
Chris Wilsone259bef2010-09-17 00:32:02 +01002562 break;
2563 case 2:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002564 i830_write_fence_reg(reg);
Chris Wilsone259bef2010-09-17 00:32:02 +01002565 break;
2566 }
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002567
Daniel Vetterae3db242010-02-19 11:51:58 +01002568 trace_i915_gem_object_get_fence(obj, obj_priv->fence_reg,
2569 obj_priv->tiling_mode);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002570
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002571 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002572}
2573
2574/**
2575 * i915_gem_clear_fence_reg - clear out fence register info
2576 * @obj: object to clear
2577 *
2578 * Zeroes out the fence register itself and clears out the associated
2579 * data structures in dev_priv and obj_priv.
2580 */
2581static void
2582i915_gem_clear_fence_reg(struct drm_gem_object *obj)
2583{
2584 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002585 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002586 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002587 struct drm_i915_fence_reg *reg =
2588 &dev_priv->fence_regs[obj_priv->fence_reg];
Chris Wilsone259bef2010-09-17 00:32:02 +01002589 uint32_t fence_reg;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002590
Chris Wilsone259bef2010-09-17 00:32:02 +01002591 switch (INTEL_INFO(dev)->gen) {
2592 case 6:
Eric Anholt4e901fd2009-10-26 16:44:17 -07002593 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 +
2594 (obj_priv->fence_reg * 8), 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002595 break;
2596 case 5:
2597 case 4:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002598 I915_WRITE64(FENCE_REG_965_0 + (obj_priv->fence_reg * 8), 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002599 break;
2600 case 3:
Chris Wilson9b74f732010-09-22 19:10:44 +01002601 if (obj_priv->fence_reg >= 8)
Chris Wilsone259bef2010-09-17 00:32:02 +01002602 fence_reg = FENCE_REG_945_8 + (obj_priv->fence_reg - 8) * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002603 else
Chris Wilsone259bef2010-09-17 00:32:02 +01002604 case 2:
2605 fence_reg = FENCE_REG_830_0 + obj_priv->fence_reg * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002606
2607 I915_WRITE(fence_reg, 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002608 break;
Eric Anholtdc529a42009-03-10 22:34:49 -07002609 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002610
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002611 reg->obj = NULL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002612 obj_priv->fence_reg = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002613 list_del_init(&reg->lru_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002614}
2615
Eric Anholt673a3942008-07-30 12:06:12 -07002616/**
Chris Wilson52dc7d32009-06-06 09:46:01 +01002617 * i915_gem_object_put_fence_reg - waits on outstanding fenced access
2618 * to the buffer to finish, and then resets the fence register.
2619 * @obj: tiled object holding a fence register.
Chris Wilson2cf34d72010-09-14 13:03:28 +01002620 * @bool: whether the wait upon the fence is interruptible
Chris Wilson52dc7d32009-06-06 09:46:01 +01002621 *
2622 * Zeroes out the fence register itself and clears out the associated
2623 * data structures in dev_priv and obj_priv.
2624 */
2625int
Chris Wilson2cf34d72010-09-14 13:03:28 +01002626i915_gem_object_put_fence_reg(struct drm_gem_object *obj,
2627 bool interruptible)
Chris Wilson52dc7d32009-06-06 09:46:01 +01002628{
2629 struct drm_device *dev = obj->dev;
Chris Wilson53640e12010-09-20 11:40:50 +01002630 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002631 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson53640e12010-09-20 11:40:50 +01002632 struct drm_i915_fence_reg *reg;
Chris Wilson52dc7d32009-06-06 09:46:01 +01002633
2634 if (obj_priv->fence_reg == I915_FENCE_REG_NONE)
2635 return 0;
2636
Daniel Vetter10ae9bd2010-02-01 13:59:17 +01002637 /* If we've changed tiling, GTT-mappings of the object
2638 * need to re-fault to ensure that the correct fence register
2639 * setup is in place.
2640 */
2641 i915_gem_release_mmap(obj);
2642
Chris Wilson52dc7d32009-06-06 09:46:01 +01002643 /* On the i915, GPU access to tiled buffers is via a fence,
2644 * therefore we must wait for any outstanding access to complete
2645 * before clearing the fence.
2646 */
Chris Wilson53640e12010-09-20 11:40:50 +01002647 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
2648 if (reg->gpu) {
Chris Wilson52dc7d32009-06-06 09:46:01 +01002649 int ret;
2650
Chris Wilson2cf34d72010-09-14 13:03:28 +01002651 ret = i915_gem_object_flush_gpu_write_domain(obj, true);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002652 if (ret)
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002653 return ret;
2654
Chris Wilson2cf34d72010-09-14 13:03:28 +01002655 ret = i915_gem_object_wait_rendering(obj, interruptible);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002656 if (ret)
Chris Wilson52dc7d32009-06-06 09:46:01 +01002657 return ret;
Chris Wilson53640e12010-09-20 11:40:50 +01002658
2659 reg->gpu = false;
Chris Wilson52dc7d32009-06-06 09:46:01 +01002660 }
2661
Daniel Vetter4a726612010-02-01 13:59:16 +01002662 i915_gem_object_flush_gtt_write_domain(obj);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002663 i915_gem_clear_fence_reg(obj);
Chris Wilson52dc7d32009-06-06 09:46:01 +01002664
2665 return 0;
2666}
2667
2668/**
Eric Anholt673a3942008-07-30 12:06:12 -07002669 * Finds free space in the GTT aperture and binds the object there.
2670 */
2671static int
Daniel Vetter920afa72010-09-16 17:54:23 +02002672i915_gem_object_bind_to_gtt(struct drm_gem_object *obj,
2673 unsigned alignment,
2674 bool mappable)
Eric Anholt673a3942008-07-30 12:06:12 -07002675{
2676 struct drm_device *dev = obj->dev;
2677 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002678 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002679 struct drm_mm_node *free_space;
Chris Wilson4bdadb92010-01-27 13:36:32 +00002680 gfp_t gfpmask = __GFP_NORETRY | __GFP_NOWARN;
Chris Wilson07f73f62009-09-14 16:50:30 +01002681 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002682
Chris Wilsonbb6baf72009-09-22 14:24:13 +01002683 if (obj_priv->madv != I915_MADV_WILLNEED) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01002684 DRM_ERROR("Attempting to bind a purgeable object\n");
2685 return -EINVAL;
2686 }
2687
Eric Anholt673a3942008-07-30 12:06:12 -07002688 if (alignment == 0)
Jesse Barnes0f973f22009-01-26 17:10:45 -08002689 alignment = i915_gem_get_gtt_alignment(obj);
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002690 if (alignment & (i915_gem_get_gtt_alignment(obj) - 1)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002691 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2692 return -EINVAL;
2693 }
2694
Chris Wilson654fc602010-05-27 13:18:21 +01002695 /* If the object is bigger than the entire aperture, reject it early
2696 * before evicting everything in a vain attempt to find space.
2697 */
Daniel Vetter920afa72010-09-16 17:54:23 +02002698 if (obj->size >
2699 (mappable ? dev_priv->mm.gtt_mappable_end : dev_priv->mm.gtt_total)) {
Chris Wilson654fc602010-05-27 13:18:21 +01002700 DRM_ERROR("Attempting to bind an object larger than the aperture\n");
2701 return -E2BIG;
2702 }
2703
Eric Anholt673a3942008-07-30 12:06:12 -07002704 search_free:
Daniel Vetter920afa72010-09-16 17:54:23 +02002705 if (mappable)
2706 free_space =
2707 drm_mm_search_free_in_range(&dev_priv->mm.gtt_space,
2708 obj->size, alignment, 0,
2709 dev_priv->mm.gtt_mappable_end,
2710 0);
2711 else
2712 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
2713 obj->size, alignment, 0);
2714
2715 if (free_space != NULL) {
2716 if (mappable)
2717 obj_priv->gtt_space =
2718 drm_mm_get_block_range_generic(free_space,
2719 obj->size,
2720 alignment, 0,
2721 dev_priv->mm.gtt_mappable_end,
2722 0);
2723 else
2724 obj_priv->gtt_space =
2725 drm_mm_get_block(free_space, obj->size,
2726 alignment);
2727 }
Eric Anholt673a3942008-07-30 12:06:12 -07002728 if (obj_priv->gtt_space == NULL) {
2729 /* If the gtt is empty and we're still having trouble
2730 * fitting our object in, we're out of memory.
2731 */
Daniel Vetter920afa72010-09-16 17:54:23 +02002732 ret = i915_gem_evict_something(dev, obj->size, alignment,
2733 mappable);
Chris Wilson97311292009-09-21 00:22:34 +01002734 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002735 return ret;
Chris Wilson97311292009-09-21 00:22:34 +01002736
Eric Anholt673a3942008-07-30 12:06:12 -07002737 goto search_free;
2738 }
2739
Chris Wilsone5281cc2010-10-28 13:45:36 +01002740 ret = i915_gem_object_get_pages_gtt(obj, gfpmask);
Eric Anholt673a3942008-07-30 12:06:12 -07002741 if (ret) {
2742 drm_mm_put_block(obj_priv->gtt_space);
2743 obj_priv->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002744
2745 if (ret == -ENOMEM) {
2746 /* first try to clear up some space from the GTT */
Daniel Vetter0108a3e2010-08-07 11:01:21 +01002747 ret = i915_gem_evict_something(dev, obj->size,
Daniel Vetter920afa72010-09-16 17:54:23 +02002748 alignment, mappable);
Chris Wilson07f73f62009-09-14 16:50:30 +01002749 if (ret) {
Chris Wilson07f73f62009-09-14 16:50:30 +01002750 /* now try to shrink everyone else */
Chris Wilson4bdadb92010-01-27 13:36:32 +00002751 if (gfpmask) {
2752 gfpmask = 0;
2753 goto search_free;
Chris Wilson07f73f62009-09-14 16:50:30 +01002754 }
2755
2756 return ret;
2757 }
2758
2759 goto search_free;
2760 }
2761
Eric Anholt673a3942008-07-30 12:06:12 -07002762 return ret;
2763 }
2764
Eric Anholt673a3942008-07-30 12:06:12 -07002765 /* Create an AGP memory structure pointing at our pages, and bind it
2766 * into the GTT.
2767 */
2768 obj_priv->agp_mem = drm_agp_bind_pages(dev,
Eric Anholt856fa192009-03-19 14:10:50 -07002769 obj_priv->pages,
Chris Wilson07f73f62009-09-14 16:50:30 +01002770 obj->size >> PAGE_SHIFT,
Chris Wilson9af90d12010-10-17 10:01:56 +01002771 obj_priv->gtt_space->start,
Keith Packardba1eb1d2008-10-14 19:55:10 -07002772 obj_priv->agp_type);
Eric Anholt673a3942008-07-30 12:06:12 -07002773 if (obj_priv->agp_mem == NULL) {
Chris Wilsone5281cc2010-10-28 13:45:36 +01002774 i915_gem_object_put_pages_gtt(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002775 drm_mm_put_block(obj_priv->gtt_space);
2776 obj_priv->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002777
Daniel Vetter920afa72010-09-16 17:54:23 +02002778 ret = i915_gem_evict_something(dev, obj->size, alignment,
2779 mappable);
Chris Wilson97311292009-09-21 00:22:34 +01002780 if (ret)
Chris Wilson07f73f62009-09-14 16:50:30 +01002781 return ret;
Chris Wilson07f73f62009-09-14 16:50:30 +01002782
2783 goto search_free;
Eric Anholt673a3942008-07-30 12:06:12 -07002784 }
Eric Anholt673a3942008-07-30 12:06:12 -07002785
Daniel Vetterfb7d5162010-10-01 22:05:20 +02002786 obj_priv->gtt_offset = obj_priv->gtt_space->start;
2787
Chris Wilsonbf1a1092010-08-07 11:01:20 +01002788 /* keep track of bounds object by adding it to the inactive list */
Chris Wilson69dc4982010-10-19 10:36:51 +01002789 list_add_tail(&obj_priv->mm_list, &dev_priv->mm.inactive_list);
Daniel Vetterfb7d5162010-10-01 22:05:20 +02002790 i915_gem_info_add_gtt(dev_priv, obj);
Chris Wilsonbf1a1092010-08-07 11:01:20 +01002791
Eric Anholt673a3942008-07-30 12:06:12 -07002792 /* Assert that the object is not currently in any GPU domain. As it
2793 * wasn't in the GTT, there shouldn't be any way it could have been in
2794 * a GPU cache
2795 */
Chris Wilson21d509e2009-06-06 09:46:02 +01002796 BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS);
2797 BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS);
Eric Anholt673a3942008-07-30 12:06:12 -07002798
Daniel Vetterec57d262010-09-30 23:42:15 +02002799 trace_i915_gem_object_bind(obj, obj_priv->gtt_offset, mappable);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002800
Eric Anholt673a3942008-07-30 12:06:12 -07002801 return 0;
2802}
2803
2804void
2805i915_gem_clflush_object(struct drm_gem_object *obj)
2806{
Daniel Vetter23010e42010-03-08 13:35:02 +01002807 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002808
2809 /* If we don't have a page list set up, then we're not pinned
2810 * to GPU, and we can ignore the cache flush because it'll happen
2811 * again at bind time.
2812 */
Eric Anholt856fa192009-03-19 14:10:50 -07002813 if (obj_priv->pages == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002814 return;
2815
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002816 trace_i915_gem_object_clflush(obj);
Eric Anholtcfa16a02009-05-26 18:46:16 -07002817
Eric Anholt856fa192009-03-19 14:10:50 -07002818 drm_clflush_pages(obj_priv->pages, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002819}
2820
Eric Anholte47c68e2008-11-14 13:35:19 -08002821/** Flushes any GPU write domain for the object if it's dirty. */
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002822static int
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002823i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj,
2824 bool pipelined)
Eric Anholte47c68e2008-11-14 13:35:19 -08002825{
2826 struct drm_device *dev = obj->dev;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002827 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002828
2829 if ((obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002830 return 0;
Eric Anholte47c68e2008-11-14 13:35:19 -08002831
2832 /* Queue the GPU write cache flushing we need. */
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002833 old_write_domain = obj->write_domain;
Chris Wilsonc78ec302010-09-20 12:50:23 +01002834 i915_gem_flush_ring(dev, NULL,
Chris Wilson92204342010-09-18 11:02:01 +01002835 to_intel_bo(obj)->ring,
2836 0, obj->write_domain);
Chris Wilson48b956c2010-09-14 12:50:34 +01002837 BUG_ON(obj->write_domain);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002838
2839 trace_i915_gem_object_change_domain(obj,
2840 obj->read_domains,
2841 old_write_domain);
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002842
2843 if (pipelined)
2844 return 0;
2845
Chris Wilson2cf34d72010-09-14 13:03:28 +01002846 return i915_gem_object_wait_rendering(obj, true);
Eric Anholte47c68e2008-11-14 13:35:19 -08002847}
2848
2849/** Flushes the GTT write domain for the object if it's dirty. */
2850static void
2851i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj)
2852{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002853 uint32_t old_write_domain;
2854
Eric Anholte47c68e2008-11-14 13:35:19 -08002855 if (obj->write_domain != I915_GEM_DOMAIN_GTT)
2856 return;
2857
2858 /* No actual flushing is required for the GTT write domain. Writes
2859 * to it immediately go to main memory as far as we know, so there's
2860 * no chipset flush. It also doesn't land in render cache.
2861 */
Chris Wilson4a684a42010-10-28 14:44:08 +01002862 i915_gem_release_mmap(obj);
2863
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002864 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002865 obj->write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002866
2867 trace_i915_gem_object_change_domain(obj,
2868 obj->read_domains,
2869 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002870}
2871
2872/** Flushes the CPU write domain for the object if it's dirty. */
2873static void
2874i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj)
2875{
2876 struct drm_device *dev = obj->dev;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002877 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002878
2879 if (obj->write_domain != I915_GEM_DOMAIN_CPU)
2880 return;
2881
2882 i915_gem_clflush_object(obj);
2883 drm_agp_chipset_flush(dev);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002884 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002885 obj->write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002886
2887 trace_i915_gem_object_change_domain(obj,
2888 obj->read_domains,
2889 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002890}
2891
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002892/**
2893 * Moves a single object to the GTT read, and possibly write domain.
2894 *
2895 * This function returns when the move is complete, including waiting on
2896 * flushes to occur.
2897 */
Jesse Barnes79e53942008-11-07 14:24:08 -08002898int
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002899i915_gem_object_set_to_gtt_domain(struct drm_gem_object *obj, int write)
2900{
Daniel Vetter23010e42010-03-08 13:35:02 +01002901 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002902 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002903 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002904
Eric Anholt02354392008-11-26 13:58:13 -08002905 /* Not valid to be called on unbound objects. */
2906 if (obj_priv->gtt_space == NULL)
2907 return -EINVAL;
2908
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002909 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002910 if (ret != 0)
2911 return ret;
2912
Chris Wilson72133422010-09-13 23:56:38 +01002913 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002914
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002915 if (write) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01002916 ret = i915_gem_object_wait_rendering(obj, true);
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002917 if (ret)
2918 return ret;
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002919 }
Eric Anholte47c68e2008-11-14 13:35:19 -08002920
2921 old_write_domain = obj->write_domain;
2922 old_read_domains = obj->read_domains;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002923
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002924 /* It should now be out of any other write domains, and we can update
2925 * the domain values for our changes.
2926 */
2927 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2928 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002929 if (write) {
Chris Wilson72133422010-09-13 23:56:38 +01002930 obj->read_domains = I915_GEM_DOMAIN_GTT;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002931 obj->write_domain = I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002932 obj_priv->dirty = 1;
2933 }
2934
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002935 trace_i915_gem_object_change_domain(obj,
2936 old_read_domains,
2937 old_write_domain);
2938
Eric Anholte47c68e2008-11-14 13:35:19 -08002939 return 0;
2940}
2941
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002942/*
2943 * Prepare buffer for display plane. Use uninterruptible for possible flush
2944 * wait, as in modesetting process we're not supposed to be interrupted.
2945 */
2946int
Chris Wilson48b956c2010-09-14 12:50:34 +01002947i915_gem_object_set_to_display_plane(struct drm_gem_object *obj,
2948 bool pipelined)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002949{
Daniel Vetter23010e42010-03-08 13:35:02 +01002950 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002951 uint32_t old_read_domains;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002952 int ret;
2953
2954 /* Not valid to be called on unbound objects. */
2955 if (obj_priv->gtt_space == NULL)
2956 return -EINVAL;
2957
Chris Wilsonced270f2010-09-26 22:47:46 +01002958 ret = i915_gem_object_flush_gpu_write_domain(obj, true);
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002959 if (ret)
2960 return ret;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002961
Chris Wilsonced270f2010-09-26 22:47:46 +01002962 /* Currently, we are always called from an non-interruptible context. */
2963 if (!pipelined) {
2964 ret = i915_gem_object_wait_rendering(obj, false);
2965 if (ret)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002966 return ret;
2967 }
2968
Chris Wilsonb118c1e2010-05-27 13:18:14 +01002969 i915_gem_object_flush_cpu_write_domain(obj);
2970
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002971 old_read_domains = obj->read_domains;
Chris Wilsonc78ec302010-09-20 12:50:23 +01002972 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002973
2974 trace_i915_gem_object_change_domain(obj,
2975 old_read_domains,
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002976 obj->write_domain);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002977
2978 return 0;
2979}
2980
Eric Anholte47c68e2008-11-14 13:35:19 -08002981/**
2982 * Moves a single object to the CPU read, and possibly write domain.
2983 *
2984 * This function returns when the move is complete, including waiting on
2985 * flushes to occur.
2986 */
2987static int
2988i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj, int write)
2989{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002990 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002991 int ret;
2992
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002993 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Eric Anholte47c68e2008-11-14 13:35:19 -08002994 if (ret != 0)
2995 return ret;
2996
2997 i915_gem_object_flush_gtt_write_domain(obj);
2998
2999 /* If we have a partially-valid cache of the object in the CPU,
3000 * finish invalidating it and free the per-page flags.
3001 */
3002 i915_gem_object_set_to_full_cpu_read_domain(obj);
3003
Chris Wilson72133422010-09-13 23:56:38 +01003004 if (write) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01003005 ret = i915_gem_object_wait_rendering(obj, true);
Chris Wilson72133422010-09-13 23:56:38 +01003006 if (ret)
3007 return ret;
3008 }
3009
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003010 old_write_domain = obj->write_domain;
3011 old_read_domains = obj->read_domains;
3012
Eric Anholte47c68e2008-11-14 13:35:19 -08003013 /* Flush the CPU cache if it's still invalid. */
3014 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
3015 i915_gem_clflush_object(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003016
3017 obj->read_domains |= I915_GEM_DOMAIN_CPU;
3018 }
3019
3020 /* It should now be out of any other write domains, and we can update
3021 * the domain values for our changes.
3022 */
3023 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
3024
3025 /* If we're writing through the CPU, then the GPU read domains will
3026 * need to be invalidated at next use.
3027 */
3028 if (write) {
Chris Wilsonc78ec302010-09-20 12:50:23 +01003029 obj->read_domains = I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003030 obj->write_domain = I915_GEM_DOMAIN_CPU;
3031 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003032
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003033 trace_i915_gem_object_change_domain(obj,
3034 old_read_domains,
3035 old_write_domain);
3036
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003037 return 0;
3038}
3039
Eric Anholt673a3942008-07-30 12:06:12 -07003040/*
3041 * Set the next domain for the specified object. This
3042 * may not actually perform the necessary flushing/invaliding though,
3043 * as that may want to be batched with other set_domain operations
3044 *
3045 * This is (we hope) the only really tricky part of gem. The goal
3046 * is fairly simple -- track which caches hold bits of the object
3047 * and make sure they remain coherent. A few concrete examples may
3048 * help to explain how it works. For shorthand, we use the notation
3049 * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
3050 * a pair of read and write domain masks.
3051 *
3052 * Case 1: the batch buffer
3053 *
3054 * 1. Allocated
3055 * 2. Written by CPU
3056 * 3. Mapped to GTT
3057 * 4. Read by GPU
3058 * 5. Unmapped from GTT
3059 * 6. Freed
3060 *
3061 * Let's take these a step at a time
3062 *
3063 * 1. Allocated
3064 * Pages allocated from the kernel may still have
3065 * cache contents, so we set them to (CPU, CPU) always.
3066 * 2. Written by CPU (using pwrite)
3067 * The pwrite function calls set_domain (CPU, CPU) and
3068 * this function does nothing (as nothing changes)
3069 * 3. Mapped by GTT
3070 * This function asserts that the object is not
3071 * currently in any GPU-based read or write domains
3072 * 4. Read by GPU
3073 * i915_gem_execbuffer calls set_domain (COMMAND, 0).
3074 * As write_domain is zero, this function adds in the
3075 * current read domains (CPU+COMMAND, 0).
3076 * flush_domains is set to CPU.
3077 * invalidate_domains is set to COMMAND
3078 * clflush is run to get data out of the CPU caches
3079 * then i915_dev_set_domain calls i915_gem_flush to
3080 * emit an MI_FLUSH and drm_agp_chipset_flush
3081 * 5. Unmapped from GTT
3082 * i915_gem_object_unbind calls set_domain (CPU, CPU)
3083 * flush_domains and invalidate_domains end up both zero
3084 * so no flushing/invalidating happens
3085 * 6. Freed
3086 * yay, done
3087 *
3088 * Case 2: The shared render buffer
3089 *
3090 * 1. Allocated
3091 * 2. Mapped to GTT
3092 * 3. Read/written by GPU
3093 * 4. set_domain to (CPU,CPU)
3094 * 5. Read/written by CPU
3095 * 6. Read/written by GPU
3096 *
3097 * 1. Allocated
3098 * Same as last example, (CPU, CPU)
3099 * 2. Mapped to GTT
3100 * Nothing changes (assertions find that it is not in the GPU)
3101 * 3. Read/written by GPU
3102 * execbuffer calls set_domain (RENDER, RENDER)
3103 * flush_domains gets CPU
3104 * invalidate_domains gets GPU
3105 * clflush (obj)
3106 * MI_FLUSH and drm_agp_chipset_flush
3107 * 4. set_domain (CPU, CPU)
3108 * flush_domains gets GPU
3109 * invalidate_domains gets CPU
3110 * wait_rendering (obj) to make sure all drawing is complete.
3111 * This will include an MI_FLUSH to get the data from GPU
3112 * to memory
3113 * clflush (obj) to invalidate the CPU cache
3114 * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
3115 * 5. Read/written by CPU
3116 * cache lines are loaded and dirtied
3117 * 6. Read written by GPU
3118 * Same as last GPU access
3119 *
3120 * Case 3: The constant buffer
3121 *
3122 * 1. Allocated
3123 * 2. Written by CPU
3124 * 3. Read by GPU
3125 * 4. Updated (written) by CPU again
3126 * 5. Read by GPU
3127 *
3128 * 1. Allocated
3129 * (CPU, CPU)
3130 * 2. Written by CPU
3131 * (CPU, CPU)
3132 * 3. Read by GPU
3133 * (CPU+RENDER, 0)
3134 * flush_domains = CPU
3135 * invalidate_domains = RENDER
3136 * clflush (obj)
3137 * MI_FLUSH
3138 * drm_agp_chipset_flush
3139 * 4. Updated (written) by CPU again
3140 * (CPU, CPU)
3141 * flush_domains = 0 (no previous write domain)
3142 * invalidate_domains = 0 (no new read domains)
3143 * 5. Read by GPU
3144 * (CPU+RENDER, 0)
3145 * flush_domains = CPU
3146 * invalidate_domains = RENDER
3147 * clflush (obj)
3148 * MI_FLUSH
3149 * drm_agp_chipset_flush
3150 */
Keith Packardc0d90822008-11-20 23:11:08 -08003151static void
Chris Wilsonb6651452010-10-23 10:15:06 +01003152i915_gem_object_set_to_gpu_domain(struct drm_gem_object *obj,
3153 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07003154{
3155 struct drm_device *dev = obj->dev;
Chris Wilson92204342010-09-18 11:02:01 +01003156 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01003157 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003158 uint32_t invalidate_domains = 0;
3159 uint32_t flush_domains = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07003160
Eric Anholt673a3942008-07-30 12:06:12 -07003161 /*
3162 * If the object isn't moving to a new write domain,
3163 * let the object stay in multiple read domains
3164 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003165 if (obj->pending_write_domain == 0)
3166 obj->pending_read_domains |= obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07003167
3168 /*
3169 * Flush the current write domain if
3170 * the new read domains don't match. Invalidate
3171 * any read domains which differ from the old
3172 * write domain
3173 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003174 if (obj->write_domain &&
3175 obj->write_domain != obj->pending_read_domains) {
Eric Anholt673a3942008-07-30 12:06:12 -07003176 flush_domains |= obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08003177 invalidate_domains |=
3178 obj->pending_read_domains & ~obj->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07003179 }
3180 /*
3181 * Invalidate any read caches which may have
3182 * stale data. That is, any new read domains.
3183 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003184 invalidate_domains |= obj->pending_read_domains & ~obj->read_domains;
Chris Wilson3d2a8122010-09-29 11:39:53 +01003185 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU)
Eric Anholt673a3942008-07-30 12:06:12 -07003186 i915_gem_clflush_object(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003187
Chris Wilson4a684a42010-10-28 14:44:08 +01003188 /* blow away mappings if mapped through GTT */
3189 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_GTT)
3190 i915_gem_release_mmap(obj);
3191
Eric Anholtefbeed92009-02-19 14:54:51 -08003192 /* The actual obj->write_domain will be updated with
3193 * pending_write_domain after we emit the accumulated flush for all
3194 * of our domain changes in execbuffers (which clears objects'
3195 * write_domains). So if we have a current write domain that we
3196 * aren't changing, set pending_write_domain to that.
3197 */
3198 if (flush_domains == 0 && obj->pending_write_domain == 0)
3199 obj->pending_write_domain = obj->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07003200
3201 dev->invalidate_domains |= invalidate_domains;
3202 dev->flush_domains |= flush_domains;
Chris Wilsonb6651452010-10-23 10:15:06 +01003203 if (flush_domains & I915_GEM_GPU_DOMAINS)
Chris Wilson92204342010-09-18 11:02:01 +01003204 dev_priv->mm.flush_rings |= obj_priv->ring->id;
Chris Wilsonb6651452010-10-23 10:15:06 +01003205 if (invalidate_domains & I915_GEM_GPU_DOMAINS)
3206 dev_priv->mm.flush_rings |= ring->id;
Eric Anholt673a3942008-07-30 12:06:12 -07003207}
3208
3209/**
Eric Anholte47c68e2008-11-14 13:35:19 -08003210 * Moves the object from a partially CPU read to a full one.
Eric Anholt673a3942008-07-30 12:06:12 -07003211 *
Eric Anholte47c68e2008-11-14 13:35:19 -08003212 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
3213 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
3214 */
3215static void
3216i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj)
3217{
Daniel Vetter23010e42010-03-08 13:35:02 +01003218 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003219
3220 if (!obj_priv->page_cpu_valid)
3221 return;
3222
3223 /* If we're partially in the CPU read domain, finish moving it in.
3224 */
3225 if (obj->read_domains & I915_GEM_DOMAIN_CPU) {
3226 int i;
3227
3228 for (i = 0; i <= (obj->size - 1) / PAGE_SIZE; i++) {
3229 if (obj_priv->page_cpu_valid[i])
3230 continue;
Eric Anholt856fa192009-03-19 14:10:50 -07003231 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholte47c68e2008-11-14 13:35:19 -08003232 }
Eric Anholte47c68e2008-11-14 13:35:19 -08003233 }
3234
3235 /* Free the page_cpu_valid mappings which are now stale, whether
3236 * or not we've got I915_GEM_DOMAIN_CPU.
3237 */
Eric Anholt9a298b22009-03-24 12:23:04 -07003238 kfree(obj_priv->page_cpu_valid);
Eric Anholte47c68e2008-11-14 13:35:19 -08003239 obj_priv->page_cpu_valid = NULL;
3240}
3241
3242/**
3243 * Set the CPU read domain on a range of the object.
3244 *
3245 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
3246 * not entirely valid. The page_cpu_valid member of the object flags which
3247 * pages have been flushed, and will be respected by
3248 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
3249 * of the whole object.
3250 *
3251 * This function returns when the move is complete, including waiting on
3252 * flushes to occur.
Eric Anholt673a3942008-07-30 12:06:12 -07003253 */
3254static int
Eric Anholte47c68e2008-11-14 13:35:19 -08003255i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
3256 uint64_t offset, uint64_t size)
Eric Anholt673a3942008-07-30 12:06:12 -07003257{
Daniel Vetter23010e42010-03-08 13:35:02 +01003258 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003259 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003260 int i, ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003261
Eric Anholte47c68e2008-11-14 13:35:19 -08003262 if (offset == 0 && size == obj->size)
3263 return i915_gem_object_set_to_cpu_domain(obj, 0);
3264
Daniel Vetterba3d8d72010-02-11 22:37:04 +01003265 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Eric Anholte47c68e2008-11-14 13:35:19 -08003266 if (ret != 0)
3267 return ret;
3268 i915_gem_object_flush_gtt_write_domain(obj);
3269
3270 /* If we're already fully in the CPU read domain, we're done. */
3271 if (obj_priv->page_cpu_valid == NULL &&
3272 (obj->read_domains & I915_GEM_DOMAIN_CPU) != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07003273 return 0;
3274
Eric Anholte47c68e2008-11-14 13:35:19 -08003275 /* Otherwise, create/clear the per-page CPU read domain flag if we're
3276 * newly adding I915_GEM_DOMAIN_CPU
3277 */
Eric Anholt673a3942008-07-30 12:06:12 -07003278 if (obj_priv->page_cpu_valid == NULL) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003279 obj_priv->page_cpu_valid = kzalloc(obj->size / PAGE_SIZE,
3280 GFP_KERNEL);
Eric Anholte47c68e2008-11-14 13:35:19 -08003281 if (obj_priv->page_cpu_valid == NULL)
3282 return -ENOMEM;
3283 } else if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0)
3284 memset(obj_priv->page_cpu_valid, 0, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07003285
3286 /* Flush the cache on any pages that are still invalid from the CPU's
3287 * perspective.
3288 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003289 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
3290 i++) {
Eric Anholt673a3942008-07-30 12:06:12 -07003291 if (obj_priv->page_cpu_valid[i])
3292 continue;
3293
Eric Anholt856fa192009-03-19 14:10:50 -07003294 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07003295
3296 obj_priv->page_cpu_valid[i] = 1;
3297 }
3298
Eric Anholte47c68e2008-11-14 13:35:19 -08003299 /* It should now be out of any other write domains, and we can update
3300 * the domain values for our changes.
3301 */
3302 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
3303
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003304 old_read_domains = obj->read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003305 obj->read_domains |= I915_GEM_DOMAIN_CPU;
3306
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003307 trace_i915_gem_object_change_domain(obj,
3308 old_read_domains,
3309 obj->write_domain);
3310
Eric Anholt673a3942008-07-30 12:06:12 -07003311 return 0;
3312}
3313
3314/**
Eric Anholt673a3942008-07-30 12:06:12 -07003315 * Pin an object to the GTT and evaluate the relocations landing in it.
3316 */
3317static int
Chris Wilson9af90d12010-10-17 10:01:56 +01003318i915_gem_execbuffer_relocate(struct drm_i915_gem_object *obj,
3319 struct drm_file *file_priv,
3320 struct drm_i915_gem_exec_object2 *entry)
Eric Anholt673a3942008-07-30 12:06:12 -07003321{
Chris Wilson9af90d12010-10-17 10:01:56 +01003322 struct drm_device *dev = obj->base.dev;
Keith Packard0839ccb2008-10-30 19:38:48 -07003323 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson2549d6c2010-10-14 12:10:41 +01003324 struct drm_i915_gem_relocation_entry __user *user_relocs;
Chris Wilson9af90d12010-10-17 10:01:56 +01003325 struct drm_gem_object *target_obj = NULL;
3326 uint32_t target_handle = 0;
3327 int i, ret = 0;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003328
Chris Wilson2549d6c2010-10-14 12:10:41 +01003329 user_relocs = (void __user *)(uintptr_t)entry->relocs_ptr;
Eric Anholt673a3942008-07-30 12:06:12 -07003330 for (i = 0; i < entry->relocation_count; i++) {
Chris Wilson2549d6c2010-10-14 12:10:41 +01003331 struct drm_i915_gem_relocation_entry reloc;
Chris Wilson9af90d12010-10-17 10:01:56 +01003332 uint32_t target_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003333
Chris Wilson9af90d12010-10-17 10:01:56 +01003334 if (__copy_from_user_inatomic(&reloc,
3335 user_relocs+i,
3336 sizeof(reloc))) {
3337 ret = -EFAULT;
3338 break;
Eric Anholt673a3942008-07-30 12:06:12 -07003339 }
Chris Wilson2549d6c2010-10-14 12:10:41 +01003340
Chris Wilson9af90d12010-10-17 10:01:56 +01003341 if (reloc.target_handle != target_handle) {
3342 drm_gem_object_unreference(target_obj);
3343
3344 target_obj = drm_gem_object_lookup(dev, file_priv,
3345 reloc.target_handle);
3346 if (target_obj == NULL) {
3347 ret = -ENOENT;
3348 break;
3349 }
3350
3351 target_handle = reloc.target_handle;
Eric Anholt673a3942008-07-30 12:06:12 -07003352 }
Chris Wilson9af90d12010-10-17 10:01:56 +01003353 target_offset = to_intel_bo(target_obj)->gtt_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003354
Chris Wilson8542a0b2009-09-09 21:15:15 +01003355#if WATCH_RELOC
3356 DRM_INFO("%s: obj %p offset %08x target %d "
3357 "read %08x write %08x gtt %08x "
3358 "presumed %08x delta %08x\n",
3359 __func__,
3360 obj,
Chris Wilson2549d6c2010-10-14 12:10:41 +01003361 (int) reloc.offset,
3362 (int) reloc.target_handle,
3363 (int) reloc.read_domains,
3364 (int) reloc.write_domain,
Chris Wilson9af90d12010-10-17 10:01:56 +01003365 (int) target_offset,
Chris Wilson2549d6c2010-10-14 12:10:41 +01003366 (int) reloc.presumed_offset,
3367 reloc.delta);
Chris Wilson8542a0b2009-09-09 21:15:15 +01003368#endif
3369
Eric Anholt673a3942008-07-30 12:06:12 -07003370 /* The target buffer should have appeared before us in the
3371 * exec_object list, so it should have a GTT space bound by now.
3372 */
Chris Wilson9af90d12010-10-17 10:01:56 +01003373 if (target_offset == 0) {
Eric Anholt673a3942008-07-30 12:06:12 -07003374 DRM_ERROR("No GTT space found for object %d\n",
Chris Wilson2549d6c2010-10-14 12:10:41 +01003375 reloc.target_handle);
Chris Wilson9af90d12010-10-17 10:01:56 +01003376 ret = -EINVAL;
3377 break;
Eric Anholt673a3942008-07-30 12:06:12 -07003378 }
3379
Chris Wilson8542a0b2009-09-09 21:15:15 +01003380 /* Validate that the target is in a valid r/w GPU domain */
Chris Wilson2549d6c2010-10-14 12:10:41 +01003381 if (reloc.write_domain & (reloc.write_domain - 1)) {
Daniel Vetter16edd552010-02-19 11:52:02 +01003382 DRM_ERROR("reloc with multiple write domains: "
3383 "obj %p target %d offset %d "
3384 "read %08x write %08x",
Chris Wilson2549d6c2010-10-14 12:10:41 +01003385 obj, reloc.target_handle,
3386 (int) reloc.offset,
3387 reloc.read_domains,
3388 reloc.write_domain);
Chris Wilson9af90d12010-10-17 10:01:56 +01003389 ret = -EINVAL;
3390 break;
Daniel Vetter16edd552010-02-19 11:52:02 +01003391 }
Chris Wilson2549d6c2010-10-14 12:10:41 +01003392 if (reloc.write_domain & I915_GEM_DOMAIN_CPU ||
3393 reloc.read_domains & I915_GEM_DOMAIN_CPU) {
Chris Wilson8542a0b2009-09-09 21:15:15 +01003394 DRM_ERROR("reloc with read/write CPU domains: "
3395 "obj %p target %d offset %d "
3396 "read %08x write %08x",
Chris Wilson2549d6c2010-10-14 12:10:41 +01003397 obj, reloc.target_handle,
3398 (int) reloc.offset,
3399 reloc.read_domains,
3400 reloc.write_domain);
Chris Wilson9af90d12010-10-17 10:01:56 +01003401 ret = -EINVAL;
3402 break;
Chris Wilson8542a0b2009-09-09 21:15:15 +01003403 }
Chris Wilson2549d6c2010-10-14 12:10:41 +01003404 if (reloc.write_domain && target_obj->pending_write_domain &&
3405 reloc.write_domain != target_obj->pending_write_domain) {
Chris Wilson8542a0b2009-09-09 21:15:15 +01003406 DRM_ERROR("Write domain conflict: "
3407 "obj %p target %d offset %d "
3408 "new %08x old %08x\n",
Chris Wilson2549d6c2010-10-14 12:10:41 +01003409 obj, reloc.target_handle,
3410 (int) reloc.offset,
3411 reloc.write_domain,
Chris Wilson8542a0b2009-09-09 21:15:15 +01003412 target_obj->pending_write_domain);
Chris Wilson9af90d12010-10-17 10:01:56 +01003413 ret = -EINVAL;
3414 break;
Chris Wilson8542a0b2009-09-09 21:15:15 +01003415 }
3416
Chris Wilson2549d6c2010-10-14 12:10:41 +01003417 target_obj->pending_read_domains |= reloc.read_domains;
Chris Wilson878a3c32010-10-22 10:48:12 +01003418 target_obj->pending_write_domain |= reloc.write_domain;
Chris Wilson8542a0b2009-09-09 21:15:15 +01003419
3420 /* If the relocation already has the right value in it, no
3421 * more work needs to be done.
3422 */
Chris Wilson9af90d12010-10-17 10:01:56 +01003423 if (target_offset == reloc.presumed_offset)
Chris Wilson8542a0b2009-09-09 21:15:15 +01003424 continue;
Chris Wilson8542a0b2009-09-09 21:15:15 +01003425
3426 /* Check that the relocation address is valid... */
Chris Wilson9af90d12010-10-17 10:01:56 +01003427 if (reloc.offset > obj->base.size - 4) {
Eric Anholt673a3942008-07-30 12:06:12 -07003428 DRM_ERROR("Relocation beyond object bounds: "
3429 "obj %p target %d offset %d size %d.\n",
Chris Wilson2549d6c2010-10-14 12:10:41 +01003430 obj, reloc.target_handle,
Chris Wilson9af90d12010-10-17 10:01:56 +01003431 (int) reloc.offset, (int) obj->base.size);
3432 ret = -EINVAL;
3433 break;
Eric Anholt673a3942008-07-30 12:06:12 -07003434 }
Chris Wilson2549d6c2010-10-14 12:10:41 +01003435 if (reloc.offset & 3) {
Eric Anholt673a3942008-07-30 12:06:12 -07003436 DRM_ERROR("Relocation not 4-byte aligned: "
3437 "obj %p target %d offset %d.\n",
Chris Wilson2549d6c2010-10-14 12:10:41 +01003438 obj, reloc.target_handle,
3439 (int) reloc.offset);
Chris Wilson9af90d12010-10-17 10:01:56 +01003440 ret = -EINVAL;
3441 break;
Eric Anholt673a3942008-07-30 12:06:12 -07003442 }
3443
Chris Wilson8542a0b2009-09-09 21:15:15 +01003444 /* and points to somewhere within the target object. */
Chris Wilson2549d6c2010-10-14 12:10:41 +01003445 if (reloc.delta >= target_obj->size) {
Chris Wilsoncd0b9fb2009-09-15 23:23:18 +01003446 DRM_ERROR("Relocation beyond target object bounds: "
3447 "obj %p target %d delta %d size %d.\n",
Chris Wilson2549d6c2010-10-14 12:10:41 +01003448 obj, reloc.target_handle,
3449 (int) reloc.delta, (int) target_obj->size);
Chris Wilson9af90d12010-10-17 10:01:56 +01003450 ret = -EINVAL;
3451 break;
Eric Anholte47c68e2008-11-14 13:35:19 -08003452 }
3453
Chris Wilson9af90d12010-10-17 10:01:56 +01003454 reloc.delta += target_offset;
3455 if (obj->base.write_domain == I915_GEM_DOMAIN_CPU) {
Chris Wilsonf0c43d92010-10-14 12:44:48 +01003456 uint32_t page_offset = reloc.offset & ~PAGE_MASK;
3457 char *vaddr;
3458
Linus Torvaldsc48c43e2010-10-26 18:57:59 -07003459 vaddr = kmap_atomic(obj->pages[reloc.offset >> PAGE_SHIFT]);
Chris Wilsonf0c43d92010-10-14 12:44:48 +01003460 *(uint32_t *)(vaddr + page_offset) = reloc.delta;
Linus Torvaldsc48c43e2010-10-26 18:57:59 -07003461 kunmap_atomic(vaddr);
Chris Wilsonf0c43d92010-10-14 12:44:48 +01003462 } else {
3463 uint32_t __iomem *reloc_entry;
3464 void __iomem *reloc_page;
Chris Wilsonf0c43d92010-10-14 12:44:48 +01003465
Chris Wilson9af90d12010-10-17 10:01:56 +01003466 ret = i915_gem_object_set_to_gtt_domain(&obj->base, 1);
3467 if (ret)
3468 break;
Chris Wilsonf0c43d92010-10-14 12:44:48 +01003469
3470 /* Map the page containing the relocation we're going to perform. */
Chris Wilson9af90d12010-10-17 10:01:56 +01003471 reloc.offset += obj->gtt_offset;
Chris Wilsonf0c43d92010-10-14 12:44:48 +01003472 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
Linus Torvaldsc48c43e2010-10-26 18:57:59 -07003473 reloc.offset & PAGE_MASK);
Chris Wilsonf0c43d92010-10-14 12:44:48 +01003474 reloc_entry = (uint32_t __iomem *)
3475 (reloc_page + (reloc.offset & ~PAGE_MASK));
3476 iowrite32(reloc.delta, reloc_entry);
Linus Torvaldsc48c43e2010-10-26 18:57:59 -07003477 io_mapping_unmap_atomic(reloc_page);
Eric Anholt673a3942008-07-30 12:06:12 -07003478 }
3479
Chris Wilsonb5dc6082010-10-20 20:59:57 +01003480 /* and update the user's relocation entry */
3481 reloc.presumed_offset = target_offset;
3482 if (__copy_to_user_inatomic(&user_relocs[i].presumed_offset,
3483 &reloc.presumed_offset,
3484 sizeof(reloc.presumed_offset))) {
3485 ret = -EFAULT;
3486 break;
3487 }
Eric Anholt673a3942008-07-30 12:06:12 -07003488 }
3489
Chris Wilson9af90d12010-10-17 10:01:56 +01003490 drm_gem_object_unreference(target_obj);
3491 return ret;
3492}
3493
3494static int
3495i915_gem_execbuffer_pin(struct drm_device *dev,
3496 struct drm_file *file,
3497 struct drm_gem_object **object_list,
3498 struct drm_i915_gem_exec_object2 *exec_list,
3499 int count)
3500{
3501 struct drm_i915_private *dev_priv = dev->dev_private;
3502 int ret, i, retry;
3503
3504 /* attempt to pin all of the buffers into the GTT */
3505 for (retry = 0; retry < 2; retry++) {
3506 ret = 0;
3507 for (i = 0; i < count; i++) {
3508 struct drm_i915_gem_exec_object2 *entry = &exec_list[i];
Daniel Vetter16e809a2010-09-16 19:37:04 +02003509 struct drm_i915_gem_object *obj = to_intel_bo(object_list[i]);
Chris Wilson9af90d12010-10-17 10:01:56 +01003510 bool need_fence =
3511 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
3512 obj->tiling_mode != I915_TILING_NONE;
3513
Daniel Vetter16e809a2010-09-16 19:37:04 +02003514 /* g33/pnv can't fence buffers in the unmappable part */
3515 bool need_mappable =
3516 entry->relocation_count ? true : need_fence;
3517
Chris Wilson9af90d12010-10-17 10:01:56 +01003518 /* Check fence reg constraints and rebind if necessary */
3519 if (need_fence &&
3520 !i915_gem_object_fence_offset_ok(&obj->base,
3521 obj->tiling_mode)) {
3522 ret = i915_gem_object_unbind(&obj->base);
3523 if (ret)
3524 break;
3525 }
3526
Daniel Vetter920afa72010-09-16 17:54:23 +02003527 ret = i915_gem_object_pin(&obj->base,
Daniel Vetter16e809a2010-09-16 19:37:04 +02003528 entry->alignment,
3529 need_mappable);
Chris Wilson9af90d12010-10-17 10:01:56 +01003530 if (ret)
3531 break;
3532
3533 /*
3534 * Pre-965 chips need a fence register set up in order
3535 * to properly handle blits to/from tiled surfaces.
3536 */
3537 if (need_fence) {
3538 ret = i915_gem_object_get_fence_reg(&obj->base, true);
3539 if (ret) {
3540 i915_gem_object_unpin(&obj->base);
3541 break;
3542 }
3543
3544 dev_priv->fence_regs[obj->fence_reg].gpu = true;
3545 }
3546
3547 entry->offset = obj->gtt_offset;
3548 }
3549
3550 while (i--)
3551 i915_gem_object_unpin(object_list[i]);
3552
3553 if (ret == 0)
3554 break;
3555
3556 if (ret != -ENOSPC || retry)
3557 return ret;
3558
3559 ret = i915_gem_evict_everything(dev);
3560 if (ret)
3561 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003562 }
3563
Eric Anholt673a3942008-07-30 12:06:12 -07003564 return 0;
3565}
3566
Eric Anholt673a3942008-07-30 12:06:12 -07003567/* Throttle our rendering by waiting until the ring has completed our requests
3568 * emitted over 20 msec ago.
3569 *
Eric Anholtb9624422009-06-03 07:27:35 +00003570 * Note that if we were to use the current jiffies each time around the loop,
3571 * we wouldn't escape the function with any frames outstanding if the time to
3572 * render a frame was over 20ms.
3573 *
Eric Anholt673a3942008-07-30 12:06:12 -07003574 * This should get us reasonable parallelism between CPU and GPU but also
3575 * relatively low latency when blocking on a particular request to finish.
3576 */
3577static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003578i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003579{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003580 struct drm_i915_private *dev_priv = dev->dev_private;
3581 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00003582 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003583 struct drm_i915_gem_request *request;
3584 struct intel_ring_buffer *ring = NULL;
3585 u32 seqno = 0;
3586 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003587
Chris Wilson1c255952010-09-26 11:03:27 +01003588 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003589 list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
Eric Anholtb9624422009-06-03 07:27:35 +00003590 if (time_after_eq(request->emitted_jiffies, recent_enough))
3591 break;
3592
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003593 ring = request->ring;
3594 seqno = request->seqno;
Eric Anholtb9624422009-06-03 07:27:35 +00003595 }
Chris Wilson1c255952010-09-26 11:03:27 +01003596 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003597
3598 if (seqno == 0)
3599 return 0;
3600
3601 ret = 0;
Chris Wilson78501ea2010-10-27 12:18:21 +01003602 if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) {
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003603 /* And wait for the seqno passing without holding any locks and
3604 * causing extra latency for others. This is safe as the irq
3605 * generation is designed to be run atomically and so is
3606 * lockless.
3607 */
Chris Wilson78501ea2010-10-27 12:18:21 +01003608 ring->user_irq_get(ring);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003609 ret = wait_event_interruptible(ring->irq_queue,
Chris Wilson78501ea2010-10-27 12:18:21 +01003610 i915_seqno_passed(ring->get_seqno(ring), seqno)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003611 || atomic_read(&dev_priv->mm.wedged));
Chris Wilson78501ea2010-10-27 12:18:21 +01003612 ring->user_irq_put(ring);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003613
3614 if (ret == 0 && atomic_read(&dev_priv->mm.wedged))
3615 ret = -EIO;
3616 }
3617
3618 if (ret == 0)
3619 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
Eric Anholtb9624422009-06-03 07:27:35 +00003620
Eric Anholt673a3942008-07-30 12:06:12 -07003621 return ret;
3622}
3623
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003624static int
Chris Wilson2549d6c2010-10-14 12:10:41 +01003625i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec,
3626 uint64_t exec_offset)
Chris Wilson83d60792009-06-06 09:45:57 +01003627{
3628 uint32_t exec_start, exec_len;
3629
3630 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
3631 exec_len = (uint32_t) exec->batch_len;
3632
3633 if ((exec_start | exec_len) & 0x7)
3634 return -EINVAL;
3635
3636 if (!exec_start)
3637 return -EINVAL;
3638
3639 return 0;
3640}
3641
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003642static int
Chris Wilson2549d6c2010-10-14 12:10:41 +01003643validate_exec_list(struct drm_i915_gem_exec_object2 *exec,
3644 int count)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003645{
Chris Wilson2549d6c2010-10-14 12:10:41 +01003646 int i;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003647
Chris Wilson2549d6c2010-10-14 12:10:41 +01003648 for (i = 0; i < count; i++) {
3649 char __user *ptr = (char __user *)(uintptr_t)exec[i].relocs_ptr;
3650 size_t length = exec[i].relocation_count * sizeof(struct drm_i915_gem_relocation_entry);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003651
Chris Wilson2549d6c2010-10-14 12:10:41 +01003652 if (!access_ok(VERIFY_READ, ptr, length))
3653 return -EFAULT;
3654
Chris Wilsonb5dc6082010-10-20 20:59:57 +01003655 /* we may also need to update the presumed offsets */
3656 if (!access_ok(VERIFY_WRITE, ptr, length))
3657 return -EFAULT;
3658
Chris Wilson2549d6c2010-10-14 12:10:41 +01003659 if (fault_in_pages_readable(ptr, length))
3660 return -EFAULT;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003661 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003662
Chris Wilson2549d6c2010-10-14 12:10:41 +01003663 return 0;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003664}
3665
Chris Wilson2549d6c2010-10-14 12:10:41 +01003666static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003667i915_gem_do_execbuffer(struct drm_device *dev, void *data,
Chris Wilson9af90d12010-10-17 10:01:56 +01003668 struct drm_file *file,
Jesse Barnes76446ca2009-12-17 22:05:42 -05003669 struct drm_i915_gem_execbuffer2 *args,
3670 struct drm_i915_gem_exec_object2 *exec_list)
Eric Anholt673a3942008-07-30 12:06:12 -07003671{
3672 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003673 struct drm_gem_object **object_list = NULL;
3674 struct drm_gem_object *batch_obj;
Eric Anholt201361a2009-03-11 12:30:04 -07003675 struct drm_clip_rect *cliprects = NULL;
Chris Wilson8dc5d142010-08-12 12:36:12 +01003676 struct drm_i915_gem_request *request = NULL;
Chris Wilson9af90d12010-10-17 10:01:56 +01003677 int ret, i, flips;
Eric Anholt673a3942008-07-30 12:06:12 -07003678 uint64_t exec_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003679
Zou Nan hai852835f2010-05-21 09:08:56 +08003680 struct intel_ring_buffer *ring = NULL;
3681
Chris Wilson30dbf0c2010-09-25 10:19:17 +01003682 ret = i915_gem_check_is_wedged(dev);
3683 if (ret)
3684 return ret;
3685
Chris Wilson2549d6c2010-10-14 12:10:41 +01003686 ret = validate_exec_list(exec_list, args->buffer_count);
3687 if (ret)
3688 return ret;
3689
Eric Anholt673a3942008-07-30 12:06:12 -07003690#if WATCH_EXEC
3691 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3692 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3693#endif
Chris Wilson549f7362010-10-19 11:19:32 +01003694 switch (args->flags & I915_EXEC_RING_MASK) {
3695 case I915_EXEC_DEFAULT:
3696 case I915_EXEC_RENDER:
3697 ring = &dev_priv->render_ring;
3698 break;
3699 case I915_EXEC_BSD:
Zou Nan haid1b851f2010-05-21 09:08:57 +08003700 if (!HAS_BSD(dev)) {
Chris Wilson549f7362010-10-19 11:19:32 +01003701 DRM_ERROR("execbuf with invalid ring (BSD)\n");
Zou Nan haid1b851f2010-05-21 09:08:57 +08003702 return -EINVAL;
3703 }
3704 ring = &dev_priv->bsd_ring;
Chris Wilson549f7362010-10-19 11:19:32 +01003705 break;
3706 case I915_EXEC_BLT:
3707 if (!HAS_BLT(dev)) {
3708 DRM_ERROR("execbuf with invalid ring (BLT)\n");
3709 return -EINVAL;
3710 }
3711 ring = &dev_priv->blt_ring;
3712 break;
3713 default:
3714 DRM_ERROR("execbuf with unknown ring: %d\n",
3715 (int)(args->flags & I915_EXEC_RING_MASK));
3716 return -EINVAL;
Zou Nan haid1b851f2010-05-21 09:08:57 +08003717 }
3718
Eric Anholt4f481ed2008-09-10 14:22:49 -07003719 if (args->buffer_count < 1) {
3720 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3721 return -EINVAL;
3722 }
Eric Anholtc8e0f932009-11-22 03:49:37 +01003723 object_list = drm_malloc_ab(sizeof(*object_list), args->buffer_count);
Jesse Barnes76446ca2009-12-17 22:05:42 -05003724 if (object_list == NULL) {
3725 DRM_ERROR("Failed to allocate object list for %d buffers\n",
Eric Anholt673a3942008-07-30 12:06:12 -07003726 args->buffer_count);
3727 ret = -ENOMEM;
3728 goto pre_mutex_err;
3729 }
Eric Anholt673a3942008-07-30 12:06:12 -07003730
Eric Anholt201361a2009-03-11 12:30:04 -07003731 if (args->num_cliprects != 0) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003732 cliprects = kcalloc(args->num_cliprects, sizeof(*cliprects),
3733 GFP_KERNEL);
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003734 if (cliprects == NULL) {
3735 ret = -ENOMEM;
Eric Anholt201361a2009-03-11 12:30:04 -07003736 goto pre_mutex_err;
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003737 }
Eric Anholt201361a2009-03-11 12:30:04 -07003738
3739 ret = copy_from_user(cliprects,
3740 (struct drm_clip_rect __user *)
3741 (uintptr_t) args->cliprects_ptr,
3742 sizeof(*cliprects) * args->num_cliprects);
3743 if (ret != 0) {
3744 DRM_ERROR("copy %d cliprects failed: %d\n",
3745 args->num_cliprects, ret);
Dan Carpenterc877cdc2010-06-23 19:03:01 +02003746 ret = -EFAULT;
Eric Anholt201361a2009-03-11 12:30:04 -07003747 goto pre_mutex_err;
3748 }
3749 }
3750
Chris Wilson8dc5d142010-08-12 12:36:12 +01003751 request = kzalloc(sizeof(*request), GFP_KERNEL);
3752 if (request == NULL) {
3753 ret = -ENOMEM;
Chris Wilsona198bc82009-02-06 16:55:20 +00003754 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003755 }
3756
Chris Wilson76c1dec2010-09-25 11:22:51 +01003757 ret = i915_mutex_lock_interruptible(dev);
3758 if (ret)
3759 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003760
Eric Anholt673a3942008-07-30 12:06:12 -07003761 if (dev_priv->mm.suspended) {
Eric Anholt673a3942008-07-30 12:06:12 -07003762 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003763 ret = -EBUSY;
3764 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003765 }
3766
Keith Packardac94a962008-11-20 23:30:27 -08003767 /* Look up object handles */
Eric Anholt673a3942008-07-30 12:06:12 -07003768 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson7e318e12010-10-27 13:43:39 +01003769 struct drm_i915_gem_object *obj_priv;
3770
Chris Wilson9af90d12010-10-17 10:01:56 +01003771 object_list[i] = drm_gem_object_lookup(dev, file,
Eric Anholt673a3942008-07-30 12:06:12 -07003772 exec_list[i].handle);
3773 if (object_list[i] == NULL) {
3774 DRM_ERROR("Invalid object handle %d at index %d\n",
3775 exec_list[i].handle, i);
Chris Wilson0ce907f2010-01-23 20:26:35 +00003776 /* prevent error path from reading uninitialized data */
3777 args->buffer_count = i + 1;
Chris Wilsonbf79cb92010-08-04 14:19:46 +01003778 ret = -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07003779 goto err;
3780 }
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003781
Daniel Vetter23010e42010-03-08 13:35:02 +01003782 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003783 if (obj_priv->in_execbuffer) {
3784 DRM_ERROR("Object %p appears more than once in object list\n",
3785 object_list[i]);
Chris Wilson0ce907f2010-01-23 20:26:35 +00003786 /* prevent error path from reading uninitialized data */
3787 args->buffer_count = i + 1;
Chris Wilsonbf79cb92010-08-04 14:19:46 +01003788 ret = -EINVAL;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003789 goto err;
3790 }
3791 obj_priv->in_execbuffer = true;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003792 }
3793
Chris Wilson9af90d12010-10-17 10:01:56 +01003794 /* Move the objects en-masse into the GTT, evicting if necessary. */
3795 ret = i915_gem_execbuffer_pin(dev, file,
3796 object_list, exec_list,
3797 args->buffer_count);
3798 if (ret)
3799 goto err;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003800
Chris Wilson9af90d12010-10-17 10:01:56 +01003801 /* The objects are in their final locations, apply the relocations. */
3802 for (i = 0; i < args->buffer_count; i++) {
3803 struct drm_i915_gem_object *obj = to_intel_bo(object_list[i]);
3804 obj->base.pending_read_domains = 0;
3805 obj->base.pending_write_domain = 0;
3806 ret = i915_gem_execbuffer_relocate(obj, file, &exec_list[i]);
Eric Anholt673a3942008-07-30 12:06:12 -07003807 if (ret)
3808 goto err;
3809 }
3810
Eric Anholt673a3942008-07-30 12:06:12 -07003811 /* Set the pending read domains for the batch buffer to COMMAND */
3812 batch_obj = object_list[args->buffer_count-1];
Chris Wilson5f26a2c2009-06-06 09:45:58 +01003813 if (batch_obj->pending_write_domain) {
3814 DRM_ERROR("Attempting to use self-modifying batch buffer\n");
3815 ret = -EINVAL;
3816 goto err;
3817 }
3818 batch_obj->pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
Eric Anholt673a3942008-07-30 12:06:12 -07003819
Chris Wilson9af90d12010-10-17 10:01:56 +01003820 /* Sanity check the batch buffer */
3821 exec_offset = to_intel_bo(batch_obj)->gtt_offset;
3822 ret = i915_gem_check_execbuffer(args, exec_offset);
Chris Wilson83d60792009-06-06 09:45:57 +01003823 if (ret != 0) {
3824 DRM_ERROR("execbuf with invalid offset/length\n");
3825 goto err;
3826 }
3827
Keith Packard646f0f62008-11-20 23:23:03 -08003828 /* Zero the global flush/invalidate flags. These
3829 * will be modified as new domains are computed
3830 * for each object
3831 */
3832 dev->invalidate_domains = 0;
3833 dev->flush_domains = 0;
Chris Wilson92204342010-09-18 11:02:01 +01003834 dev_priv->mm.flush_rings = 0;
Chris Wilson7e318e12010-10-27 13:43:39 +01003835 for (i = 0; i < args->buffer_count; i++)
3836 i915_gem_object_set_to_gpu_domain(object_list[i], ring);
Eric Anholt673a3942008-07-30 12:06:12 -07003837
Keith Packard646f0f62008-11-20 23:23:03 -08003838 if (dev->invalidate_domains | dev->flush_domains) {
3839#if WATCH_EXEC
3840 DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
3841 __func__,
3842 dev->invalidate_domains,
3843 dev->flush_domains);
3844#endif
Chris Wilson9af90d12010-10-17 10:01:56 +01003845 i915_gem_flush(dev, file,
Keith Packard646f0f62008-11-20 23:23:03 -08003846 dev->invalidate_domains,
Chris Wilson92204342010-09-18 11:02:01 +01003847 dev->flush_domains,
3848 dev_priv->mm.flush_rings);
Keith Packard646f0f62008-11-20 23:23:03 -08003849 }
Eric Anholt673a3942008-07-30 12:06:12 -07003850
Eric Anholt673a3942008-07-30 12:06:12 -07003851#if WATCH_COHERENCY
3852 for (i = 0; i < args->buffer_count; i++) {
3853 i915_gem_object_check_coherency(object_list[i],
3854 exec_list[i].handle);
3855 }
3856#endif
3857
Eric Anholt673a3942008-07-30 12:06:12 -07003858#if WATCH_EXEC
Ben Gamari6911a9b2009-04-02 11:24:54 -07003859 i915_gem_dump_object(batch_obj,
Eric Anholt673a3942008-07-30 12:06:12 -07003860 args->batch_len,
3861 __func__,
3862 ~0);
3863#endif
3864
Chris Wilsone59f2ba2010-10-07 17:28:15 +01003865 /* Check for any pending flips. As we only maintain a flip queue depth
3866 * of 1, we can simply insert a WAIT for the next display flip prior
3867 * to executing the batch and avoid stalling the CPU.
3868 */
3869 flips = 0;
3870 for (i = 0; i < args->buffer_count; i++) {
3871 if (object_list[i]->write_domain)
3872 flips |= atomic_read(&to_intel_bo(object_list[i])->pending_flip);
3873 }
3874 if (flips) {
3875 int plane, flip_mask;
3876
3877 for (plane = 0; flips >> plane; plane++) {
3878 if (((flips >> plane) & 1) == 0)
3879 continue;
3880
3881 if (plane)
3882 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
3883 else
3884 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
3885
Chris Wilsone1f99ce2010-10-27 12:45:26 +01003886 ret = intel_ring_begin(ring, 2);
3887 if (ret)
3888 goto err;
3889
Chris Wilson78501ea2010-10-27 12:18:21 +01003890 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
3891 intel_ring_emit(ring, MI_NOOP);
3892 intel_ring_advance(ring);
Chris Wilsone59f2ba2010-10-07 17:28:15 +01003893 }
3894 }
3895
Eric Anholt673a3942008-07-30 12:06:12 -07003896 /* Exec the batchbuffer */
Chris Wilson78501ea2010-10-27 12:18:21 +01003897 ret = ring->dispatch_execbuffer(ring, args, cliprects, exec_offset);
Eric Anholt673a3942008-07-30 12:06:12 -07003898 if (ret) {
3899 DRM_ERROR("dispatch failed %d\n", ret);
3900 goto err;
3901 }
3902
Chris Wilson7e318e12010-10-27 13:43:39 +01003903 for (i = 0; i < args->buffer_count; i++) {
3904 struct drm_gem_object *obj = object_list[i];
3905
3906 obj->read_domains = obj->pending_read_domains;
3907 obj->write_domain = obj->pending_write_domain;
3908
3909 i915_gem_object_move_to_active(obj, ring);
3910 if (obj->write_domain) {
3911 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
3912 obj_priv->dirty = 1;
3913 list_move_tail(&obj_priv->gpu_write_list,
3914 &ring->gpu_write_list);
3915 intel_mark_busy(dev, obj);
3916 }
3917
3918 trace_i915_gem_object_change_domain(obj,
3919 obj->read_domains,
3920 obj->write_domain);
3921 }
3922
Eric Anholt673a3942008-07-30 12:06:12 -07003923 /*
3924 * Ensure that the commands in the batch buffer are
3925 * finished before the interrupt fires
3926 */
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01003927 i915_retire_commands(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07003928
Chris Wilson3cce4692010-10-27 16:11:02 +01003929 if (i915_add_request(dev, file, request, ring))
3930 ring->outstanding_lazy_request = true;
3931 else
3932 request = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07003933
Eric Anholt673a3942008-07-30 12:06:12 -07003934err:
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003935 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson7e318e12010-10-27 13:43:39 +01003936 if (object_list[i] == NULL)
3937 break;
3938
3939 to_intel_bo(object_list[i])->in_execbuffer = false;
Julia Lawallaad87df2008-12-21 16:28:47 +01003940 drm_gem_object_unreference(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003941 }
Julia Lawallaad87df2008-12-21 16:28:47 +01003942
Eric Anholt673a3942008-07-30 12:06:12 -07003943 mutex_unlock(&dev->struct_mutex);
3944
Chris Wilson93533c22010-01-31 10:40:48 +00003945pre_mutex_err:
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003946 drm_free_large(object_list);
Eric Anholt9a298b22009-03-24 12:23:04 -07003947 kfree(cliprects);
Chris Wilson8dc5d142010-08-12 12:36:12 +01003948 kfree(request);
Eric Anholt673a3942008-07-30 12:06:12 -07003949
3950 return ret;
3951}
3952
Jesse Barnes76446ca2009-12-17 22:05:42 -05003953/*
3954 * Legacy execbuffer just creates an exec2 list from the original exec object
3955 * list array and passes it to the real function.
3956 */
3957int
3958i915_gem_execbuffer(struct drm_device *dev, void *data,
3959 struct drm_file *file_priv)
3960{
3961 struct drm_i915_gem_execbuffer *args = data;
3962 struct drm_i915_gem_execbuffer2 exec2;
3963 struct drm_i915_gem_exec_object *exec_list = NULL;
3964 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
3965 int ret, i;
3966
3967#if WATCH_EXEC
3968 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3969 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3970#endif
3971
3972 if (args->buffer_count < 1) {
3973 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3974 return -EINVAL;
3975 }
3976
3977 /* Copy in the exec list from userland */
3978 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
3979 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
3980 if (exec_list == NULL || exec2_list == NULL) {
3981 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
3982 args->buffer_count);
3983 drm_free_large(exec_list);
3984 drm_free_large(exec2_list);
3985 return -ENOMEM;
3986 }
3987 ret = copy_from_user(exec_list,
3988 (struct drm_i915_relocation_entry __user *)
3989 (uintptr_t) args->buffers_ptr,
3990 sizeof(*exec_list) * args->buffer_count);
3991 if (ret != 0) {
3992 DRM_ERROR("copy %d exec entries failed %d\n",
3993 args->buffer_count, ret);
3994 drm_free_large(exec_list);
3995 drm_free_large(exec2_list);
3996 return -EFAULT;
3997 }
3998
3999 for (i = 0; i < args->buffer_count; i++) {
4000 exec2_list[i].handle = exec_list[i].handle;
4001 exec2_list[i].relocation_count = exec_list[i].relocation_count;
4002 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
4003 exec2_list[i].alignment = exec_list[i].alignment;
4004 exec2_list[i].offset = exec_list[i].offset;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004005 if (INTEL_INFO(dev)->gen < 4)
Jesse Barnes76446ca2009-12-17 22:05:42 -05004006 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
4007 else
4008 exec2_list[i].flags = 0;
4009 }
4010
4011 exec2.buffers_ptr = args->buffers_ptr;
4012 exec2.buffer_count = args->buffer_count;
4013 exec2.batch_start_offset = args->batch_start_offset;
4014 exec2.batch_len = args->batch_len;
4015 exec2.DR1 = args->DR1;
4016 exec2.DR4 = args->DR4;
4017 exec2.num_cliprects = args->num_cliprects;
4018 exec2.cliprects_ptr = args->cliprects_ptr;
Zou Nan hai852835f2010-05-21 09:08:56 +08004019 exec2.flags = I915_EXEC_RENDER;
Jesse Barnes76446ca2009-12-17 22:05:42 -05004020
4021 ret = i915_gem_do_execbuffer(dev, data, file_priv, &exec2, exec2_list);
4022 if (!ret) {
4023 /* Copy the new buffer offsets back to the user's exec list. */
4024 for (i = 0; i < args->buffer_count; i++)
4025 exec_list[i].offset = exec2_list[i].offset;
4026 /* ... and back out to userspace */
4027 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
4028 (uintptr_t) args->buffers_ptr,
4029 exec_list,
4030 sizeof(*exec_list) * args->buffer_count);
4031 if (ret) {
4032 ret = -EFAULT;
4033 DRM_ERROR("failed to copy %d exec entries "
4034 "back to user (%d)\n",
4035 args->buffer_count, ret);
4036 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05004037 }
4038
4039 drm_free_large(exec_list);
4040 drm_free_large(exec2_list);
4041 return ret;
4042}
4043
4044int
4045i915_gem_execbuffer2(struct drm_device *dev, void *data,
4046 struct drm_file *file_priv)
4047{
4048 struct drm_i915_gem_execbuffer2 *args = data;
4049 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
4050 int ret;
4051
4052#if WATCH_EXEC
4053 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
4054 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
4055#endif
4056
4057 if (args->buffer_count < 1) {
4058 DRM_ERROR("execbuf2 with %d buffers\n", args->buffer_count);
4059 return -EINVAL;
4060 }
4061
4062 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
4063 if (exec2_list == NULL) {
4064 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
4065 args->buffer_count);
4066 return -ENOMEM;
4067 }
4068 ret = copy_from_user(exec2_list,
4069 (struct drm_i915_relocation_entry __user *)
4070 (uintptr_t) args->buffers_ptr,
4071 sizeof(*exec2_list) * args->buffer_count);
4072 if (ret != 0) {
4073 DRM_ERROR("copy %d exec entries failed %d\n",
4074 args->buffer_count, ret);
4075 drm_free_large(exec2_list);
4076 return -EFAULT;
4077 }
4078
4079 ret = i915_gem_do_execbuffer(dev, data, file_priv, args, exec2_list);
4080 if (!ret) {
4081 /* Copy the new buffer offsets back to the user's exec list. */
4082 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
4083 (uintptr_t) args->buffers_ptr,
4084 exec2_list,
4085 sizeof(*exec2_list) * args->buffer_count);
4086 if (ret) {
4087 ret = -EFAULT;
4088 DRM_ERROR("failed to copy %d exec entries "
4089 "back to user (%d)\n",
4090 args->buffer_count, ret);
4091 }
4092 }
4093
4094 drm_free_large(exec2_list);
4095 return ret;
4096}
4097
Eric Anholt673a3942008-07-30 12:06:12 -07004098int
Daniel Vetter920afa72010-09-16 17:54:23 +02004099i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment,
4100 bool mappable)
Eric Anholt673a3942008-07-30 12:06:12 -07004101{
4102 struct drm_device *dev = obj->dev;
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004103 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01004104 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004105 int ret;
4106
Daniel Vetter778c3542010-05-13 11:49:44 +02004107 BUG_ON(obj_priv->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT);
Chris Wilson23bc5982010-09-29 16:10:57 +01004108 WARN_ON(i915_verify_lists(dev));
Chris Wilsonac0c6b52010-05-27 13:18:18 +01004109
4110 if (obj_priv->gtt_space != NULL) {
4111 if (alignment == 0)
4112 alignment = i915_gem_get_gtt_alignment(obj);
Daniel Vetter16e809a2010-09-16 19:37:04 +02004113 if (obj_priv->gtt_offset & (alignment - 1) ||
4114 (mappable && !i915_gem_object_cpu_accessible(obj_priv))) {
Chris Wilsonae7d49d2010-08-04 12:37:41 +01004115 WARN(obj_priv->pin_count,
4116 "bo is already pinned with incorrect alignment:"
4117 " offset=%x, req.alignment=%x\n",
4118 obj_priv->gtt_offset, alignment);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01004119 ret = i915_gem_object_unbind(obj);
4120 if (ret)
4121 return ret;
4122 }
4123 }
4124
Eric Anholt673a3942008-07-30 12:06:12 -07004125 if (obj_priv->gtt_space == NULL) {
Daniel Vetter920afa72010-09-16 17:54:23 +02004126 ret = i915_gem_object_bind_to_gtt(obj, alignment, mappable);
Chris Wilson97311292009-09-21 00:22:34 +01004127 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07004128 return ret;
Chris Wilson22c344e2009-02-11 14:26:45 +00004129 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05004130
Eric Anholt673a3942008-07-30 12:06:12 -07004131 obj_priv->pin_count++;
4132
4133 /* If the object is not active and not pending a flush,
4134 * remove it from the inactive list
4135 */
4136 if (obj_priv->pin_count == 1) {
Daniel Vetterfb7d5162010-10-01 22:05:20 +02004137 i915_gem_info_add_pin(dev_priv, obj, mappable);
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004138 if (!obj_priv->active)
Chris Wilson69dc4982010-10-19 10:36:51 +01004139 list_move_tail(&obj_priv->mm_list,
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004140 &dev_priv->mm.pinned_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004141 }
Daniel Vetterfb7d5162010-10-01 22:05:20 +02004142 BUG_ON(!obj_priv->pin_mappable && mappable);
Eric Anholt673a3942008-07-30 12:06:12 -07004143
Chris Wilson23bc5982010-09-29 16:10:57 +01004144 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07004145 return 0;
4146}
4147
4148void
4149i915_gem_object_unpin(struct drm_gem_object *obj)
4150{
4151 struct drm_device *dev = obj->dev;
4152 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01004153 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004154
Chris Wilson23bc5982010-09-29 16:10:57 +01004155 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07004156 obj_priv->pin_count--;
4157 BUG_ON(obj_priv->pin_count < 0);
4158 BUG_ON(obj_priv->gtt_space == NULL);
4159
4160 /* If the object is no longer pinned, and is
4161 * neither active nor being flushed, then stick it on
4162 * the inactive list
4163 */
4164 if (obj_priv->pin_count == 0) {
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004165 if (!obj_priv->active)
Chris Wilson69dc4982010-10-19 10:36:51 +01004166 list_move_tail(&obj_priv->mm_list,
Eric Anholt673a3942008-07-30 12:06:12 -07004167 &dev_priv->mm.inactive_list);
Daniel Vetterfb7d5162010-10-01 22:05:20 +02004168 i915_gem_info_remove_pin(dev_priv, obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004169 }
Chris Wilson23bc5982010-09-29 16:10:57 +01004170 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07004171}
4172
4173int
4174i915_gem_pin_ioctl(struct drm_device *dev, void *data,
4175 struct drm_file *file_priv)
4176{
4177 struct drm_i915_gem_pin *args = data;
4178 struct drm_gem_object *obj;
4179 struct drm_i915_gem_object *obj_priv;
4180 int ret;
4181
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004182 ret = i915_mutex_lock_interruptible(dev);
4183 if (ret)
4184 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004185
4186 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4187 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004188 ret = -ENOENT;
4189 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07004190 }
Daniel Vetter23010e42010-03-08 13:35:02 +01004191 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004192
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004193 if (obj_priv->madv != I915_MADV_WILLNEED) {
4194 DRM_ERROR("Attempting to pin a purgeable buffer\n");
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004195 ret = -EINVAL;
4196 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004197 }
4198
Jesse Barnes79e53942008-11-07 14:24:08 -08004199 if (obj_priv->pin_filp != NULL && obj_priv->pin_filp != file_priv) {
4200 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
4201 args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004202 ret = -EINVAL;
4203 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08004204 }
4205
4206 obj_priv->user_pin_count++;
4207 obj_priv->pin_filp = file_priv;
4208 if (obj_priv->user_pin_count == 1) {
Daniel Vetter920afa72010-09-16 17:54:23 +02004209 ret = i915_gem_object_pin(obj, args->alignment, true);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004210 if (ret)
4211 goto out;
Eric Anholt673a3942008-07-30 12:06:12 -07004212 }
4213
4214 /* XXX - flush the CPU caches for pinned objects
4215 * as the X server doesn't manage domains yet
4216 */
Eric Anholte47c68e2008-11-14 13:35:19 -08004217 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004218 args->offset = obj_priv->gtt_offset;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004219out:
Eric Anholt673a3942008-07-30 12:06:12 -07004220 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004221unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07004222 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004223 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004224}
4225
4226int
4227i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
4228 struct drm_file *file_priv)
4229{
4230 struct drm_i915_gem_pin *args = data;
4231 struct drm_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08004232 struct drm_i915_gem_object *obj_priv;
Chris Wilson76c1dec2010-09-25 11:22:51 +01004233 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004234
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004235 ret = i915_mutex_lock_interruptible(dev);
4236 if (ret)
4237 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004238
4239 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4240 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004241 ret = -ENOENT;
4242 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07004243 }
Daniel Vetter23010e42010-03-08 13:35:02 +01004244 obj_priv = to_intel_bo(obj);
Chris Wilson76c1dec2010-09-25 11:22:51 +01004245
Jesse Barnes79e53942008-11-07 14:24:08 -08004246 if (obj_priv->pin_filp != file_priv) {
4247 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
4248 args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004249 ret = -EINVAL;
4250 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08004251 }
4252 obj_priv->user_pin_count--;
4253 if (obj_priv->user_pin_count == 0) {
4254 obj_priv->pin_filp = NULL;
4255 i915_gem_object_unpin(obj);
4256 }
Eric Anholt673a3942008-07-30 12:06:12 -07004257
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004258out:
Eric Anholt673a3942008-07-30 12:06:12 -07004259 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004260unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07004261 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004262 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004263}
4264
4265int
4266i915_gem_busy_ioctl(struct drm_device *dev, void *data,
4267 struct drm_file *file_priv)
4268{
4269 struct drm_i915_gem_busy *args = data;
4270 struct drm_gem_object *obj;
4271 struct drm_i915_gem_object *obj_priv;
Chris Wilson30dbf0c2010-09-25 10:19:17 +01004272 int ret;
4273
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004274 ret = i915_mutex_lock_interruptible(dev);
4275 if (ret)
4276 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004277
Eric Anholt673a3942008-07-30 12:06:12 -07004278 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4279 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004280 ret = -ENOENT;
4281 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07004282 }
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004283 obj_priv = to_intel_bo(obj);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004284
Chris Wilson0be555b2010-08-04 15:36:30 +01004285 /* Count all active objects as busy, even if they are currently not used
4286 * by the gpu. Users of this interface expect objects to eventually
4287 * become non-busy without any further actions, therefore emit any
4288 * necessary flushes here.
Eric Anholtc4de0a52008-12-14 19:05:04 -08004289 */
Chris Wilson0be555b2010-08-04 15:36:30 +01004290 args->busy = obj_priv->active;
4291 if (args->busy) {
4292 /* Unconditionally flush objects, even when the gpu still uses this
4293 * object. Userspace calling this function indicates that it wants to
4294 * use this buffer rather sooner than later, so issuing the required
4295 * flush earlier is beneficial.
4296 */
Chris Wilsonc78ec302010-09-20 12:50:23 +01004297 if (obj->write_domain & I915_GEM_GPU_DOMAINS)
4298 i915_gem_flush_ring(dev, file_priv,
Chris Wilson92204342010-09-18 11:02:01 +01004299 obj_priv->ring,
4300 0, obj->write_domain);
Chris Wilson0be555b2010-08-04 15:36:30 +01004301
4302 /* Update the active list for the hardware's current position.
4303 * Otherwise this only updates on a delayed timer or when irqs
4304 * are actually unmasked, and our working set ends up being
4305 * larger than required.
4306 */
4307 i915_gem_retire_requests_ring(dev, obj_priv->ring);
4308
4309 args->busy = obj_priv->active;
4310 }
Eric Anholt673a3942008-07-30 12:06:12 -07004311
4312 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004313unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07004314 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004315 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004316}
4317
4318int
4319i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4320 struct drm_file *file_priv)
4321{
4322 return i915_gem_ring_throttle(dev, file_priv);
4323}
4324
Chris Wilson3ef94da2009-09-14 16:50:29 +01004325int
4326i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4327 struct drm_file *file_priv)
4328{
4329 struct drm_i915_gem_madvise *args = data;
4330 struct drm_gem_object *obj;
4331 struct drm_i915_gem_object *obj_priv;
Chris Wilson76c1dec2010-09-25 11:22:51 +01004332 int ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004333
4334 switch (args->madv) {
4335 case I915_MADV_DONTNEED:
4336 case I915_MADV_WILLNEED:
4337 break;
4338 default:
4339 return -EINVAL;
4340 }
4341
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004342 ret = i915_mutex_lock_interruptible(dev);
4343 if (ret)
4344 return ret;
4345
Chris Wilson3ef94da2009-09-14 16:50:29 +01004346 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4347 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004348 ret = -ENOENT;
4349 goto unlock;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004350 }
Daniel Vetter23010e42010-03-08 13:35:02 +01004351 obj_priv = to_intel_bo(obj);
Chris Wilson3ef94da2009-09-14 16:50:29 +01004352
4353 if (obj_priv->pin_count) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004354 ret = -EINVAL;
4355 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004356 }
4357
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004358 if (obj_priv->madv != __I915_MADV_PURGED)
4359 obj_priv->madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004360
Chris Wilson2d7ef392009-09-20 23:13:10 +01004361 /* if the object is no longer bound, discard its backing storage */
4362 if (i915_gem_object_is_purgeable(obj_priv) &&
4363 obj_priv->gtt_space == NULL)
4364 i915_gem_object_truncate(obj);
4365
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004366 args->retained = obj_priv->madv != __I915_MADV_PURGED;
4367
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004368out:
Chris Wilson3ef94da2009-09-14 16:50:29 +01004369 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004370unlock:
Chris Wilson3ef94da2009-09-14 16:50:29 +01004371 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004372 return ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004373}
4374
Daniel Vetterac52bc52010-04-09 19:05:06 +00004375struct drm_gem_object * i915_gem_alloc_object(struct drm_device *dev,
4376 size_t size)
4377{
Chris Wilson73aa8082010-09-30 11:46:12 +01004378 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc397b902010-04-09 19:05:07 +00004379 struct drm_i915_gem_object *obj;
4380
4381 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
4382 if (obj == NULL)
4383 return NULL;
4384
4385 if (drm_gem_object_init(dev, &obj->base, size) != 0) {
4386 kfree(obj);
4387 return NULL;
4388 }
4389
Chris Wilson73aa8082010-09-30 11:46:12 +01004390 i915_gem_info_add_obj(dev_priv, size);
4391
Daniel Vetterc397b902010-04-09 19:05:07 +00004392 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4393 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4394
4395 obj->agp_type = AGP_USER_MEMORY;
Daniel Vetter62b8b212010-04-09 19:05:08 +00004396 obj->base.driver_private = NULL;
Daniel Vetterc397b902010-04-09 19:05:07 +00004397 obj->fence_reg = I915_FENCE_REG_NONE;
Chris Wilson69dc4982010-10-19 10:36:51 +01004398 INIT_LIST_HEAD(&obj->mm_list);
4399 INIT_LIST_HEAD(&obj->ring_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00004400 INIT_LIST_HEAD(&obj->gpu_write_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00004401 obj->madv = I915_MADV_WILLNEED;
4402
Daniel Vetterc397b902010-04-09 19:05:07 +00004403 return &obj->base;
Daniel Vetterac52bc52010-04-09 19:05:06 +00004404}
4405
Eric Anholt673a3942008-07-30 12:06:12 -07004406int i915_gem_init_object(struct drm_gem_object *obj)
4407{
Daniel Vetterc397b902010-04-09 19:05:07 +00004408 BUG();
Jesse Barnesde151cf2008-11-12 10:03:55 -08004409
Eric Anholt673a3942008-07-30 12:06:12 -07004410 return 0;
4411}
4412
Chris Wilsonbe726152010-07-23 23:18:50 +01004413static void i915_gem_free_object_tail(struct drm_gem_object *obj)
4414{
4415 struct drm_device *dev = obj->dev;
4416 drm_i915_private_t *dev_priv = dev->dev_private;
4417 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
4418 int ret;
4419
4420 ret = i915_gem_object_unbind(obj);
4421 if (ret == -ERESTARTSYS) {
Chris Wilson69dc4982010-10-19 10:36:51 +01004422 list_move(&obj_priv->mm_list,
Chris Wilsonbe726152010-07-23 23:18:50 +01004423 &dev_priv->mm.deferred_free_list);
4424 return;
4425 }
4426
Chris Wilson39a01d12010-10-28 13:03:06 +01004427 if (obj->map_list.map)
Chris Wilsonbe726152010-07-23 23:18:50 +01004428 i915_gem_free_mmap_offset(obj);
4429
4430 drm_gem_object_release(obj);
Chris Wilson73aa8082010-09-30 11:46:12 +01004431 i915_gem_info_remove_obj(dev_priv, obj->size);
Chris Wilsonbe726152010-07-23 23:18:50 +01004432
4433 kfree(obj_priv->page_cpu_valid);
4434 kfree(obj_priv->bit_17);
4435 kfree(obj_priv);
4436}
4437
Eric Anholt673a3942008-07-30 12:06:12 -07004438void i915_gem_free_object(struct drm_gem_object *obj)
4439{
Jesse Barnesde151cf2008-11-12 10:03:55 -08004440 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01004441 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004442
Chris Wilson1c5d22f2009-08-25 11:15:50 +01004443 trace_i915_gem_object_destroy(obj);
4444
Eric Anholt673a3942008-07-30 12:06:12 -07004445 while (obj_priv->pin_count > 0)
4446 i915_gem_object_unpin(obj);
4447
Dave Airlie71acb5e2008-12-30 20:31:46 +10004448 if (obj_priv->phys_obj)
4449 i915_gem_detach_phys_object(dev, obj);
4450
Chris Wilsonbe726152010-07-23 23:18:50 +01004451 i915_gem_free_object_tail(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004452}
4453
Jesse Barnes5669fca2009-02-17 15:13:31 -08004454int
Eric Anholt673a3942008-07-30 12:06:12 -07004455i915_gem_idle(struct drm_device *dev)
4456{
4457 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson29105cc2010-01-07 10:39:13 +00004458 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004459
Keith Packard6dbe2772008-10-14 21:41:13 -07004460 mutex_lock(&dev->struct_mutex);
4461
Chris Wilson87acb0a2010-10-19 10:13:00 +01004462 if (dev_priv->mm.suspended) {
Keith Packard6dbe2772008-10-14 21:41:13 -07004463 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004464 return 0;
Keith Packard6dbe2772008-10-14 21:41:13 -07004465 }
Eric Anholt673a3942008-07-30 12:06:12 -07004466
Chris Wilson29105cc2010-01-07 10:39:13 +00004467 ret = i915_gpu_idle(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004468 if (ret) {
4469 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004470 return ret;
Keith Packard6dbe2772008-10-14 21:41:13 -07004471 }
Eric Anholt673a3942008-07-30 12:06:12 -07004472
Chris Wilson29105cc2010-01-07 10:39:13 +00004473 /* Under UMS, be paranoid and evict. */
4474 if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
Chris Wilsonb47eb4a2010-08-07 11:01:23 +01004475 ret = i915_gem_evict_inactive(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00004476 if (ret) {
4477 mutex_unlock(&dev->struct_mutex);
4478 return ret;
4479 }
4480 }
4481
4482 /* Hack! Don't let anybody do execbuf while we don't control the chip.
4483 * We need to replace this with a semaphore, or something.
4484 * And not confound mm.suspended!
4485 */
4486 dev_priv->mm.suspended = 1;
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02004487 del_timer_sync(&dev_priv->hangcheck_timer);
Chris Wilson29105cc2010-01-07 10:39:13 +00004488
4489 i915_kernel_lost_context(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004490 i915_gem_cleanup_ringbuffer(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00004491
Keith Packard6dbe2772008-10-14 21:41:13 -07004492 mutex_unlock(&dev->struct_mutex);
4493
Chris Wilson29105cc2010-01-07 10:39:13 +00004494 /* Cancel the retire work handler, which should be idle now. */
4495 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
4496
Eric Anholt673a3942008-07-30 12:06:12 -07004497 return 0;
4498}
4499
Jesse Barnese552eb72010-04-21 11:39:23 -07004500/*
4501 * 965+ support PIPE_CONTROL commands, which provide finer grained control
4502 * over cache flushing.
4503 */
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004504static int
Jesse Barnese552eb72010-04-21 11:39:23 -07004505i915_gem_init_pipe_control(struct drm_device *dev)
4506{
4507 drm_i915_private_t *dev_priv = dev->dev_private;
4508 struct drm_gem_object *obj;
4509 struct drm_i915_gem_object *obj_priv;
4510 int ret;
4511
Eric Anholt34dc4d42010-05-07 14:30:03 -07004512 obj = i915_gem_alloc_object(dev, 4096);
Jesse Barnese552eb72010-04-21 11:39:23 -07004513 if (obj == NULL) {
4514 DRM_ERROR("Failed to allocate seqno page\n");
4515 ret = -ENOMEM;
4516 goto err;
4517 }
4518 obj_priv = to_intel_bo(obj);
4519 obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
4520
Daniel Vetter920afa72010-09-16 17:54:23 +02004521 ret = i915_gem_object_pin(obj, 4096, true);
Jesse Barnese552eb72010-04-21 11:39:23 -07004522 if (ret)
4523 goto err_unref;
4524
4525 dev_priv->seqno_gfx_addr = obj_priv->gtt_offset;
4526 dev_priv->seqno_page = kmap(obj_priv->pages[0]);
4527 if (dev_priv->seqno_page == NULL)
4528 goto err_unpin;
4529
4530 dev_priv->seqno_obj = obj;
4531 memset(dev_priv->seqno_page, 0, PAGE_SIZE);
4532
4533 return 0;
4534
4535err_unpin:
4536 i915_gem_object_unpin(obj);
4537err_unref:
4538 drm_gem_object_unreference(obj);
4539err:
4540 return ret;
4541}
4542
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004543
4544static void
Jesse Barnese552eb72010-04-21 11:39:23 -07004545i915_gem_cleanup_pipe_control(struct drm_device *dev)
4546{
4547 drm_i915_private_t *dev_priv = dev->dev_private;
4548 struct drm_gem_object *obj;
4549 struct drm_i915_gem_object *obj_priv;
4550
4551 obj = dev_priv->seqno_obj;
4552 obj_priv = to_intel_bo(obj);
4553 kunmap(obj_priv->pages[0]);
4554 i915_gem_object_unpin(obj);
4555 drm_gem_object_unreference(obj);
4556 dev_priv->seqno_obj = NULL;
4557
4558 dev_priv->seqno_page = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07004559}
4560
Eric Anholt673a3942008-07-30 12:06:12 -07004561int
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004562i915_gem_init_ringbuffer(struct drm_device *dev)
4563{
4564 drm_i915_private_t *dev_priv = dev->dev_private;
4565 int ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01004566
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004567 if (HAS_PIPE_CONTROL(dev)) {
4568 ret = i915_gem_init_pipe_control(dev);
4569 if (ret)
4570 return ret;
4571 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004572
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08004573 ret = intel_init_render_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004574 if (ret)
4575 goto cleanup_pipe_control;
4576
4577 if (HAS_BSD(dev)) {
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08004578 ret = intel_init_bsd_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004579 if (ret)
4580 goto cleanup_render_ring;
Zou Nan haid1b851f2010-05-21 09:08:57 +08004581 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004582
Chris Wilson549f7362010-10-19 11:19:32 +01004583 if (HAS_BLT(dev)) {
4584 ret = intel_init_blt_ring_buffer(dev);
4585 if (ret)
4586 goto cleanup_bsd_ring;
4587 }
4588
Chris Wilson6f392d52010-08-07 11:01:22 +01004589 dev_priv->next_seqno = 1;
4590
Chris Wilson68f95ba2010-05-27 13:18:22 +01004591 return 0;
4592
Chris Wilson549f7362010-10-19 11:19:32 +01004593cleanup_bsd_ring:
Chris Wilson78501ea2010-10-27 12:18:21 +01004594 intel_cleanup_ring_buffer(&dev_priv->bsd_ring);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004595cleanup_render_ring:
Chris Wilson78501ea2010-10-27 12:18:21 +01004596 intel_cleanup_ring_buffer(&dev_priv->render_ring);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004597cleanup_pipe_control:
4598 if (HAS_PIPE_CONTROL(dev))
4599 i915_gem_cleanup_pipe_control(dev);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004600 return ret;
4601}
4602
4603void
4604i915_gem_cleanup_ringbuffer(struct drm_device *dev)
4605{
4606 drm_i915_private_t *dev_priv = dev->dev_private;
4607
Chris Wilson78501ea2010-10-27 12:18:21 +01004608 intel_cleanup_ring_buffer(&dev_priv->render_ring);
4609 intel_cleanup_ring_buffer(&dev_priv->bsd_ring);
4610 intel_cleanup_ring_buffer(&dev_priv->blt_ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004611 if (HAS_PIPE_CONTROL(dev))
4612 i915_gem_cleanup_pipe_control(dev);
4613}
4614
4615int
Eric Anholt673a3942008-07-30 12:06:12 -07004616i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
4617 struct drm_file *file_priv)
4618{
4619 drm_i915_private_t *dev_priv = dev->dev_private;
4620 int ret;
4621
Jesse Barnes79e53942008-11-07 14:24:08 -08004622 if (drm_core_check_feature(dev, DRIVER_MODESET))
4623 return 0;
4624
Ben Gamariba1234d2009-09-14 17:48:47 -04004625 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07004626 DRM_ERROR("Reenabling wedged hardware, good luck\n");
Ben Gamariba1234d2009-09-14 17:48:47 -04004627 atomic_set(&dev_priv->mm.wedged, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004628 }
4629
Eric Anholt673a3942008-07-30 12:06:12 -07004630 mutex_lock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004631 dev_priv->mm.suspended = 0;
4632
4633 ret = i915_gem_init_ringbuffer(dev);
Wu Fengguangd816f6ac2009-04-18 10:43:32 +08004634 if (ret != 0) {
4635 mutex_unlock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004636 return ret;
Wu Fengguangd816f6ac2009-04-18 10:43:32 +08004637 }
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004638
Chris Wilson69dc4982010-10-19 10:36:51 +01004639 BUG_ON(!list_empty(&dev_priv->mm.active_list));
Zou Nan hai852835f2010-05-21 09:08:56 +08004640 BUG_ON(!list_empty(&dev_priv->render_ring.active_list));
Chris Wilson87acb0a2010-10-19 10:13:00 +01004641 BUG_ON(!list_empty(&dev_priv->bsd_ring.active_list));
Chris Wilson549f7362010-10-19 11:19:32 +01004642 BUG_ON(!list_empty(&dev_priv->blt_ring.active_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004643 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
4644 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
Zou Nan hai852835f2010-05-21 09:08:56 +08004645 BUG_ON(!list_empty(&dev_priv->render_ring.request_list));
Chris Wilson87acb0a2010-10-19 10:13:00 +01004646 BUG_ON(!list_empty(&dev_priv->bsd_ring.request_list));
Chris Wilson549f7362010-10-19 11:19:32 +01004647 BUG_ON(!list_empty(&dev_priv->blt_ring.request_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004648 mutex_unlock(&dev->struct_mutex);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004649
Chris Wilson5f353082010-06-07 14:03:03 +01004650 ret = drm_irq_install(dev);
4651 if (ret)
4652 goto cleanup_ringbuffer;
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004653
Eric Anholt673a3942008-07-30 12:06:12 -07004654 return 0;
Chris Wilson5f353082010-06-07 14:03:03 +01004655
4656cleanup_ringbuffer:
4657 mutex_lock(&dev->struct_mutex);
4658 i915_gem_cleanup_ringbuffer(dev);
4659 dev_priv->mm.suspended = 1;
4660 mutex_unlock(&dev->struct_mutex);
4661
4662 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004663}
4664
4665int
4666i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
4667 struct drm_file *file_priv)
4668{
Jesse Barnes79e53942008-11-07 14:24:08 -08004669 if (drm_core_check_feature(dev, DRIVER_MODESET))
4670 return 0;
4671
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004672 drm_irq_uninstall(dev);
Linus Torvaldse6890f62009-09-08 17:09:24 -07004673 return i915_gem_idle(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004674}
4675
4676void
4677i915_gem_lastclose(struct drm_device *dev)
4678{
4679 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004680
Eric Anholte806b492009-01-22 09:56:58 -08004681 if (drm_core_check_feature(dev, DRIVER_MODESET))
4682 return;
4683
Keith Packard6dbe2772008-10-14 21:41:13 -07004684 ret = i915_gem_idle(dev);
4685 if (ret)
4686 DRM_ERROR("failed to idle hardware: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07004687}
4688
Chris Wilson64193402010-10-24 12:38:05 +01004689static void
4690init_ring_lists(struct intel_ring_buffer *ring)
4691{
4692 INIT_LIST_HEAD(&ring->active_list);
4693 INIT_LIST_HEAD(&ring->request_list);
4694 INIT_LIST_HEAD(&ring->gpu_write_list);
4695}
4696
Eric Anholt673a3942008-07-30 12:06:12 -07004697void
4698i915_gem_load(struct drm_device *dev)
4699{
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004700 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07004701 drm_i915_private_t *dev_priv = dev->dev_private;
4702
Chris Wilson69dc4982010-10-19 10:36:51 +01004703 INIT_LIST_HEAD(&dev_priv->mm.active_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004704 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
4705 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004706 INIT_LIST_HEAD(&dev_priv->mm.pinned_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07004707 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Chris Wilsonbe726152010-07-23 23:18:50 +01004708 INIT_LIST_HEAD(&dev_priv->mm.deferred_free_list);
Chris Wilson64193402010-10-24 12:38:05 +01004709 init_ring_lists(&dev_priv->render_ring);
4710 init_ring_lists(&dev_priv->bsd_ring);
4711 init_ring_lists(&dev_priv->blt_ring);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02004712 for (i = 0; i < 16; i++)
4713 INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004714 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
4715 i915_gem_retire_work_handler);
Chris Wilson30dbf0c2010-09-25 10:19:17 +01004716 init_completion(&dev_priv->error_completion);
Chris Wilson31169712009-09-14 16:50:28 +01004717
Dave Airlie94400122010-07-20 13:15:31 +10004718 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
4719 if (IS_GEN3(dev)) {
4720 u32 tmp = I915_READ(MI_ARB_STATE);
4721 if (!(tmp & MI_ARB_C3_LP_WRITE_ENABLE)) {
4722 /* arb state is a masked write, so set bit + bit in mask */
4723 tmp = MI_ARB_C3_LP_WRITE_ENABLE | (MI_ARB_C3_LP_WRITE_ENABLE << MI_ARB_MASK_SHIFT);
4724 I915_WRITE(MI_ARB_STATE, tmp);
4725 }
4726 }
4727
Jesse Barnesde151cf2008-11-12 10:03:55 -08004728 /* Old X drivers will take 0-2 for front, back, depth buffers */
Eric Anholtb397c832010-01-26 09:43:10 -08004729 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4730 dev_priv->fence_reg_start = 3;
Jesse Barnesde151cf2008-11-12 10:03:55 -08004731
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004732 if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08004733 dev_priv->num_fence_regs = 16;
4734 else
4735 dev_priv->num_fence_regs = 8;
4736
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004737 /* Initialize fence registers to zero */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004738 switch (INTEL_INFO(dev)->gen) {
4739 case 6:
4740 for (i = 0; i < 16; i++)
4741 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (i * 8), 0);
4742 break;
4743 case 5:
4744 case 4:
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004745 for (i = 0; i < 16; i++)
4746 I915_WRITE64(FENCE_REG_965_0 + (i * 8), 0);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004747 break;
4748 case 3:
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004749 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4750 for (i = 0; i < 8; i++)
4751 I915_WRITE(FENCE_REG_945_8 + (i * 4), 0);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004752 case 2:
4753 for (i = 0; i < 8; i++)
4754 I915_WRITE(FENCE_REG_830_0 + (i * 4), 0);
4755 break;
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004756 }
Eric Anholt673a3942008-07-30 12:06:12 -07004757 i915_gem_detect_bit_6_swizzle(dev);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05004758 init_waitqueue_head(&dev_priv->pending_flip_queue);
Chris Wilson17250b72010-10-28 12:51:39 +01004759
4760 dev_priv->mm.inactive_shrinker.shrink = i915_gem_inactive_shrink;
4761 dev_priv->mm.inactive_shrinker.seeks = DEFAULT_SEEKS;
4762 register_shrinker(&dev_priv->mm.inactive_shrinker);
Eric Anholt673a3942008-07-30 12:06:12 -07004763}
Dave Airlie71acb5e2008-12-30 20:31:46 +10004764
4765/*
4766 * Create a physically contiguous memory object for this object
4767 * e.g. for cursor + overlay regs
4768 */
Chris Wilson995b67622010-08-20 13:23:26 +01004769static int i915_gem_init_phys_object(struct drm_device *dev,
4770 int id, int size, int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004771{
4772 drm_i915_private_t *dev_priv = dev->dev_private;
4773 struct drm_i915_gem_phys_object *phys_obj;
4774 int ret;
4775
4776 if (dev_priv->mm.phys_objs[id - 1] || !size)
4777 return 0;
4778
Eric Anholt9a298b22009-03-24 12:23:04 -07004779 phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004780 if (!phys_obj)
4781 return -ENOMEM;
4782
4783 phys_obj->id = id;
4784
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004785 phys_obj->handle = drm_pci_alloc(dev, size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004786 if (!phys_obj->handle) {
4787 ret = -ENOMEM;
4788 goto kfree_obj;
4789 }
4790#ifdef CONFIG_X86
4791 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4792#endif
4793
4794 dev_priv->mm.phys_objs[id - 1] = phys_obj;
4795
4796 return 0;
4797kfree_obj:
Eric Anholt9a298b22009-03-24 12:23:04 -07004798 kfree(phys_obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004799 return ret;
4800}
4801
Chris Wilson995b67622010-08-20 13:23:26 +01004802static void i915_gem_free_phys_object(struct drm_device *dev, int id)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004803{
4804 drm_i915_private_t *dev_priv = dev->dev_private;
4805 struct drm_i915_gem_phys_object *phys_obj;
4806
4807 if (!dev_priv->mm.phys_objs[id - 1])
4808 return;
4809
4810 phys_obj = dev_priv->mm.phys_objs[id - 1];
4811 if (phys_obj->cur_obj) {
4812 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
4813 }
4814
4815#ifdef CONFIG_X86
4816 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4817#endif
4818 drm_pci_free(dev, phys_obj->handle);
4819 kfree(phys_obj);
4820 dev_priv->mm.phys_objs[id - 1] = NULL;
4821}
4822
4823void i915_gem_free_all_phys_object(struct drm_device *dev)
4824{
4825 int i;
4826
Dave Airlie260883c2009-01-22 17:58:49 +10004827 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004828 i915_gem_free_phys_object(dev, i);
4829}
4830
4831void i915_gem_detach_phys_object(struct drm_device *dev,
4832 struct drm_gem_object *obj)
4833{
Chris Wilsone5281cc2010-10-28 13:45:36 +01004834 struct address_space *mapping = obj->filp->f_path.dentry->d_inode->i_mapping;
4835 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
4836 char *vaddr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004837 int i;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004838 int page_count;
4839
Dave Airlie71acb5e2008-12-30 20:31:46 +10004840 if (!obj_priv->phys_obj)
4841 return;
Chris Wilsone5281cc2010-10-28 13:45:36 +01004842 vaddr = obj_priv->phys_obj->handle->vaddr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004843
4844 page_count = obj->size / PAGE_SIZE;
4845
4846 for (i = 0; i < page_count; i++) {
Chris Wilsone5281cc2010-10-28 13:45:36 +01004847 struct page *page = read_cache_page_gfp(mapping, i,
4848 GFP_HIGHUSER | __GFP_RECLAIMABLE);
4849 if (!IS_ERR(page)) {
4850 char *dst = kmap_atomic(page);
4851 memcpy(dst, vaddr + i*PAGE_SIZE, PAGE_SIZE);
4852 kunmap_atomic(dst);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004853
Chris Wilsone5281cc2010-10-28 13:45:36 +01004854 drm_clflush_pages(&page, 1);
4855
4856 set_page_dirty(page);
4857 mark_page_accessed(page);
4858 page_cache_release(page);
4859 }
Dave Airlie71acb5e2008-12-30 20:31:46 +10004860 }
Dave Airlie71acb5e2008-12-30 20:31:46 +10004861 drm_agp_chipset_flush(dev);
Chris Wilsond78b47b2009-06-17 21:52:49 +01004862
Dave Airlie71acb5e2008-12-30 20:31:46 +10004863 obj_priv->phys_obj->cur_obj = NULL;
4864 obj_priv->phys_obj = NULL;
4865}
4866
4867int
4868i915_gem_attach_phys_object(struct drm_device *dev,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004869 struct drm_gem_object *obj,
4870 int id,
4871 int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004872{
Chris Wilsone5281cc2010-10-28 13:45:36 +01004873 struct address_space *mapping = obj->filp->f_path.dentry->d_inode->i_mapping;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004874 drm_i915_private_t *dev_priv = dev->dev_private;
4875 struct drm_i915_gem_object *obj_priv;
4876 int ret = 0;
4877 int page_count;
4878 int i;
4879
4880 if (id > I915_MAX_PHYS_OBJECT)
4881 return -EINVAL;
4882
Daniel Vetter23010e42010-03-08 13:35:02 +01004883 obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004884
4885 if (obj_priv->phys_obj) {
4886 if (obj_priv->phys_obj->id == id)
4887 return 0;
4888 i915_gem_detach_phys_object(dev, obj);
4889 }
4890
Dave Airlie71acb5e2008-12-30 20:31:46 +10004891 /* create a new object */
4892 if (!dev_priv->mm.phys_objs[id - 1]) {
4893 ret = i915_gem_init_phys_object(dev, id,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004894 obj->size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004895 if (ret) {
Linus Torvaldsaeb565d2009-01-26 10:01:53 -08004896 DRM_ERROR("failed to init phys object %d size: %zu\n", id, obj->size);
Chris Wilsone5281cc2010-10-28 13:45:36 +01004897 return ret;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004898 }
4899 }
4900
4901 /* bind to the object */
4902 obj_priv->phys_obj = dev_priv->mm.phys_objs[id - 1];
4903 obj_priv->phys_obj->cur_obj = obj;
4904
Dave Airlie71acb5e2008-12-30 20:31:46 +10004905 page_count = obj->size / PAGE_SIZE;
4906
4907 for (i = 0; i < page_count; i++) {
Chris Wilsone5281cc2010-10-28 13:45:36 +01004908 struct page *page;
4909 char *dst, *src;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004910
Chris Wilsone5281cc2010-10-28 13:45:36 +01004911 page = read_cache_page_gfp(mapping, i,
4912 GFP_HIGHUSER | __GFP_RECLAIMABLE);
4913 if (IS_ERR(page))
4914 return PTR_ERR(page);
4915
4916 src = kmap_atomic(obj_priv->pages[i]);
4917 dst = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004918 memcpy(dst, src, PAGE_SIZE);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -07004919 kunmap_atomic(src);
Chris Wilsone5281cc2010-10-28 13:45:36 +01004920
4921 mark_page_accessed(page);
4922 page_cache_release(page);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004923 }
4924
4925 return 0;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004926}
4927
4928static int
4929i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
4930 struct drm_i915_gem_pwrite *args,
4931 struct drm_file *file_priv)
4932{
Daniel Vetter23010e42010-03-08 13:35:02 +01004933 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004934 void *obj_addr;
4935 int ret;
4936 char __user *user_data;
4937
4938 user_data = (char __user *) (uintptr_t) args->data_ptr;
4939 obj_addr = obj_priv->phys_obj->handle->vaddr + args->offset;
4940
Zhao Yakui44d98a62009-10-09 11:39:40 +08004941 DRM_DEBUG_DRIVER("obj_addr %p, %lld\n", obj_addr, args->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004942 ret = copy_from_user(obj_addr, user_data, args->size);
4943 if (ret)
4944 return -EFAULT;
4945
4946 drm_agp_chipset_flush(dev);
4947 return 0;
4948}
Eric Anholtb9624422009-06-03 07:27:35 +00004949
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004950void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00004951{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004952 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00004953
4954 /* Clean up our request list when the client is going away, so that
4955 * later retire_requests won't dereference our soon-to-be-gone
4956 * file_priv.
4957 */
Chris Wilson1c255952010-09-26 11:03:27 +01004958 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004959 while (!list_empty(&file_priv->mm.request_list)) {
4960 struct drm_i915_gem_request *request;
4961
4962 request = list_first_entry(&file_priv->mm.request_list,
4963 struct drm_i915_gem_request,
4964 client_list);
4965 list_del(&request->client_list);
4966 request->file_priv = NULL;
4967 }
Chris Wilson1c255952010-09-26 11:03:27 +01004968 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00004969}
Chris Wilson31169712009-09-14 16:50:28 +01004970
Chris Wilson31169712009-09-14 16:50:28 +01004971static int
Chris Wilson1637ef42010-04-20 17:10:35 +01004972i915_gpu_is_active(struct drm_device *dev)
4973{
4974 drm_i915_private_t *dev_priv = dev->dev_private;
4975 int lists_empty;
4976
Chris Wilson1637ef42010-04-20 17:10:35 +01004977 lists_empty = list_empty(&dev_priv->mm.flushing_list) &&
Chris Wilson17250b72010-10-28 12:51:39 +01004978 list_empty(&dev_priv->mm.active_list);
Chris Wilson1637ef42010-04-20 17:10:35 +01004979
4980 return !lists_empty;
4981}
4982
4983static int
Chris Wilson17250b72010-10-28 12:51:39 +01004984i915_gem_inactive_shrink(struct shrinker *shrinker,
4985 int nr_to_scan,
4986 gfp_t gfp_mask)
Chris Wilson31169712009-09-14 16:50:28 +01004987{
Chris Wilson17250b72010-10-28 12:51:39 +01004988 struct drm_i915_private *dev_priv =
4989 container_of(shrinker,
4990 struct drm_i915_private,
4991 mm.inactive_shrinker);
4992 struct drm_device *dev = dev_priv->dev;
4993 struct drm_i915_gem_object *obj, *next;
4994 int cnt;
4995
4996 if (!mutex_trylock(&dev->struct_mutex))
Chris Wilsonbbe2e112010-10-28 22:35:07 +01004997 return 0;
Chris Wilson31169712009-09-14 16:50:28 +01004998
4999 /* "fast-path" to count number of available objects */
5000 if (nr_to_scan == 0) {
Chris Wilson17250b72010-10-28 12:51:39 +01005001 cnt = 0;
5002 list_for_each_entry(obj,
5003 &dev_priv->mm.inactive_list,
5004 mm_list)
5005 cnt++;
5006 mutex_unlock(&dev->struct_mutex);
5007 return cnt / 100 * sysctl_vfs_cache_pressure;
Chris Wilson31169712009-09-14 16:50:28 +01005008 }
5009
Chris Wilson1637ef42010-04-20 17:10:35 +01005010rescan:
Chris Wilson31169712009-09-14 16:50:28 +01005011 /* first scan for clean buffers */
Chris Wilson17250b72010-10-28 12:51:39 +01005012 i915_gem_retire_requests(dev);
Chris Wilson31169712009-09-14 16:50:28 +01005013
Chris Wilson17250b72010-10-28 12:51:39 +01005014 list_for_each_entry_safe(obj, next,
5015 &dev_priv->mm.inactive_list,
5016 mm_list) {
5017 if (i915_gem_object_is_purgeable(obj)) {
5018 i915_gem_object_unbind(&obj->base);
5019 if (--nr_to_scan == 0)
5020 break;
Chris Wilson31169712009-09-14 16:50:28 +01005021 }
Chris Wilson31169712009-09-14 16:50:28 +01005022 }
5023
5024 /* second pass, evict/count anything still on the inactive list */
Chris Wilson17250b72010-10-28 12:51:39 +01005025 cnt = 0;
5026 list_for_each_entry_safe(obj, next,
5027 &dev_priv->mm.inactive_list,
5028 mm_list) {
5029 if (nr_to_scan) {
5030 i915_gem_object_unbind(&obj->base);
5031 nr_to_scan--;
5032 } else
5033 cnt++;
Chris Wilson31169712009-09-14 16:50:28 +01005034 }
5035
Chris Wilson17250b72010-10-28 12:51:39 +01005036 if (nr_to_scan && i915_gpu_is_active(dev)) {
Chris Wilson1637ef42010-04-20 17:10:35 +01005037 /*
5038 * We are desperate for pages, so as a last resort, wait
5039 * for the GPU to finish and discard whatever we can.
5040 * This has a dramatic impact to reduce the number of
5041 * OOM-killer events whilst running the GPU aggressively.
5042 */
Chris Wilson17250b72010-10-28 12:51:39 +01005043 if (i915_gpu_idle(dev) == 0)
Chris Wilson1637ef42010-04-20 17:10:35 +01005044 goto rescan;
5045 }
Chris Wilson17250b72010-10-28 12:51:39 +01005046 mutex_unlock(&dev->struct_mutex);
5047 return cnt / 100 * sysctl_vfs_cache_pressure;
Chris Wilson31169712009-09-14 16:50:28 +01005048}