blob: c8e516d3f8bc76a4da62aa80dd72db6e8b22049c [file] [log] [blame]
Eric Anholt673a3942008-07-30 12:06:12 -07001/*
2 * Copyright © 2008 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 *
26 */
27
28#include "drmP.h"
29#include "drm.h"
30#include "i915_drm.h"
31#include "i915_drv.h"
Chris Wilson1c5d22f2009-08-25 11:15:50 +010032#include "i915_trace.h"
Jesse Barnes652c3932009-08-17 13:31:43 -070033#include "intel_drv.h"
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Eric Anholt673a3942008-07-30 12:06:12 -070035#include <linux/swap.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080036#include <linux/pci.h>
Zhenyu Wangf8f235e2010-08-27 11:08:57 +080037#include <linux/intel-gtt.h>
Eric Anholt673a3942008-07-30 12:06:12 -070038
Chris Wilsona00b10c2010-09-24 21:15:47 +010039static uint32_t i915_gem_get_gtt_alignment(struct drm_i915_gem_object *obj_priv);
40static uint32_t i915_gem_get_gtt_size(struct drm_i915_gem_object *obj_priv);
Daniel Vetterba3d8d72010-02-11 22:37:04 +010041
42static int i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj,
43 bool pipelined);
Eric Anholte47c68e2008-11-14 13:35:19 -080044static void i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj);
45static void i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj);
Eric Anholte47c68e2008-11-14 13:35:19 -080046static int i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj,
47 int write);
48static int i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
49 uint64_t offset,
50 uint64_t size);
51static void i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj);
Chris Wilson2cf34d72010-09-14 13:03:28 +010052static int i915_gem_object_wait_rendering(struct drm_gem_object *obj,
53 bool interruptible);
Jesse Barnesde151cf2008-11-12 10:03:55 -080054static int i915_gem_object_bind_to_gtt(struct drm_gem_object *obj,
Chris Wilsona00b10c2010-09-24 21:15:47 +010055 unsigned alignment,
56 bool mappable,
57 bool need_fence);
Jesse Barnesde151cf2008-11-12 10:03:55 -080058static void i915_gem_clear_fence_reg(struct drm_gem_object *obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +100059static int i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
60 struct drm_i915_gem_pwrite *args,
61 struct drm_file *file_priv);
Chris Wilsonbe726152010-07-23 23:18:50 +010062static void i915_gem_free_object_tail(struct drm_gem_object *obj);
Eric Anholt673a3942008-07-30 12:06:12 -070063
Chris Wilson17250b72010-10-28 12:51:39 +010064static int i915_gem_inactive_shrink(struct shrinker *shrinker,
65 int nr_to_scan,
66 gfp_t gfp_mask);
67
Chris Wilson31169712009-09-14 16:50:28 +010068
Chris Wilson73aa8082010-09-30 11:46:12 +010069/* some bookkeeping */
70static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
71 size_t size)
72{
73 dev_priv->mm.object_count++;
74 dev_priv->mm.object_memory += size;
75}
76
77static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
78 size_t size)
79{
80 dev_priv->mm.object_count--;
81 dev_priv->mm.object_memory -= size;
82}
83
84static void i915_gem_info_add_gtt(struct drm_i915_private *dev_priv,
Chris Wilsona00b10c2010-09-24 21:15:47 +010085 struct drm_i915_gem_object *obj)
Chris Wilson73aa8082010-09-30 11:46:12 +010086{
87 dev_priv->mm.gtt_count++;
Chris Wilsona00b10c2010-09-24 21:15:47 +010088 dev_priv->mm.gtt_memory += obj->gtt_space->size;
89 if (obj->gtt_offset < dev_priv->mm.gtt_mappable_end) {
Daniel Vetterfb7d5162010-10-01 22:05:20 +020090 dev_priv->mm.mappable_gtt_used +=
Chris Wilsona00b10c2010-09-24 21:15:47 +010091 min_t(size_t, obj->gtt_space->size,
92 dev_priv->mm.gtt_mappable_end - obj->gtt_offset);
Daniel Vetterfb7d5162010-10-01 22:05:20 +020093 }
Chris Wilson73aa8082010-09-30 11:46:12 +010094}
95
96static void i915_gem_info_remove_gtt(struct drm_i915_private *dev_priv,
Chris Wilsona00b10c2010-09-24 21:15:47 +010097 struct drm_i915_gem_object *obj)
Chris Wilson73aa8082010-09-30 11:46:12 +010098{
99 dev_priv->mm.gtt_count--;
Chris Wilsona00b10c2010-09-24 21:15:47 +0100100 dev_priv->mm.gtt_memory -= obj->gtt_space->size;
101 if (obj->gtt_offset < dev_priv->mm.gtt_mappable_end) {
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200102 dev_priv->mm.mappable_gtt_used -=
Chris Wilsona00b10c2010-09-24 21:15:47 +0100103 min_t(size_t, obj->gtt_space->size,
104 dev_priv->mm.gtt_mappable_end - obj->gtt_offset);
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200105 }
106}
107
108/**
109 * Update the mappable working set counters. Call _only_ when there is a change
110 * in one of (pin|fault)_mappable and update *_mappable _before_ calling.
111 * @mappable: new state the changed mappable flag (either pin_ or fault_).
112 */
113static void
114i915_gem_info_update_mappable(struct drm_i915_private *dev_priv,
Chris Wilsona00b10c2010-09-24 21:15:47 +0100115 struct drm_i915_gem_object *obj,
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200116 bool mappable)
117{
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200118 if (mappable) {
Chris Wilsona00b10c2010-09-24 21:15:47 +0100119 if (obj->pin_mappable && obj->fault_mappable)
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200120 /* Combined state was already mappable. */
121 return;
122 dev_priv->mm.gtt_mappable_count++;
Chris Wilsona00b10c2010-09-24 21:15:47 +0100123 dev_priv->mm.gtt_mappable_memory += obj->gtt_space->size;
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200124 } else {
Chris Wilsona00b10c2010-09-24 21:15:47 +0100125 if (obj->pin_mappable || obj->fault_mappable)
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200126 /* Combined state still mappable. */
127 return;
128 dev_priv->mm.gtt_mappable_count--;
Chris Wilsona00b10c2010-09-24 21:15:47 +0100129 dev_priv->mm.gtt_mappable_memory -= obj->gtt_space->size;
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200130 }
Chris Wilson73aa8082010-09-30 11:46:12 +0100131}
132
133static void i915_gem_info_add_pin(struct drm_i915_private *dev_priv,
Chris Wilsona00b10c2010-09-24 21:15:47 +0100134 struct drm_i915_gem_object *obj,
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200135 bool mappable)
Chris Wilson73aa8082010-09-30 11:46:12 +0100136{
137 dev_priv->mm.pin_count++;
Chris Wilsona00b10c2010-09-24 21:15:47 +0100138 dev_priv->mm.pin_memory += obj->gtt_space->size;
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200139 if (mappable) {
Chris Wilsona00b10c2010-09-24 21:15:47 +0100140 obj->pin_mappable = true;
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200141 i915_gem_info_update_mappable(dev_priv, obj, true);
142 }
Chris Wilson73aa8082010-09-30 11:46:12 +0100143}
144
145static void i915_gem_info_remove_pin(struct drm_i915_private *dev_priv,
Chris Wilsona00b10c2010-09-24 21:15:47 +0100146 struct drm_i915_gem_object *obj)
Chris Wilson73aa8082010-09-30 11:46:12 +0100147{
148 dev_priv->mm.pin_count--;
Chris Wilsona00b10c2010-09-24 21:15:47 +0100149 dev_priv->mm.pin_memory -= obj->gtt_space->size;
150 if (obj->pin_mappable) {
151 obj->pin_mappable = false;
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200152 i915_gem_info_update_mappable(dev_priv, obj, false);
153 }
Chris Wilson73aa8082010-09-30 11:46:12 +0100154}
155
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100156int
157i915_gem_check_is_wedged(struct drm_device *dev)
158{
159 struct drm_i915_private *dev_priv = dev->dev_private;
160 struct completion *x = &dev_priv->error_completion;
161 unsigned long flags;
162 int ret;
163
164 if (!atomic_read(&dev_priv->mm.wedged))
165 return 0;
166
167 ret = wait_for_completion_interruptible(x);
168 if (ret)
169 return ret;
170
171 /* Success, we reset the GPU! */
172 if (!atomic_read(&dev_priv->mm.wedged))
173 return 0;
174
175 /* GPU is hung, bump the completion count to account for
176 * the token we just consumed so that we never hit zero and
177 * end up waiting upon a subsequent completion event that
178 * will never happen.
179 */
180 spin_lock_irqsave(&x->wait.lock, flags);
181 x->done++;
182 spin_unlock_irqrestore(&x->wait.lock, flags);
183 return -EIO;
184}
185
Chris Wilson76c1dec2010-09-25 11:22:51 +0100186static int i915_mutex_lock_interruptible(struct drm_device *dev)
187{
188 struct drm_i915_private *dev_priv = dev->dev_private;
189 int ret;
190
191 ret = i915_gem_check_is_wedged(dev);
192 if (ret)
193 return ret;
194
195 ret = mutex_lock_interruptible(&dev->struct_mutex);
196 if (ret)
197 return ret;
198
199 if (atomic_read(&dev_priv->mm.wedged)) {
200 mutex_unlock(&dev->struct_mutex);
201 return -EAGAIN;
202 }
203
Chris Wilson23bc5982010-09-29 16:10:57 +0100204 WARN_ON(i915_verify_lists(dev));
Chris Wilson76c1dec2010-09-25 11:22:51 +0100205 return 0;
206}
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100207
Chris Wilson7d1c4802010-08-07 21:45:03 +0100208static inline bool
209i915_gem_object_is_inactive(struct drm_i915_gem_object *obj_priv)
210{
211 return obj_priv->gtt_space &&
212 !obj_priv->active &&
213 obj_priv->pin_count == 0;
214}
215
Chris Wilson73aa8082010-09-30 11:46:12 +0100216int i915_gem_do_init(struct drm_device *dev,
217 unsigned long start,
Daniel Vetter53984632010-09-22 23:44:24 +0200218 unsigned long mappable_end,
Jesse Barnes79e53942008-11-07 14:24:08 -0800219 unsigned long end)
220{
221 drm_i915_private_t *dev_priv = dev->dev_private;
222
223 if (start >= end ||
224 (start & (PAGE_SIZE - 1)) != 0 ||
225 (end & (PAGE_SIZE - 1)) != 0) {
226 return -EINVAL;
227 }
228
229 drm_mm_init(&dev_priv->mm.gtt_space, start,
230 end - start);
231
Chris Wilson73aa8082010-09-30 11:46:12 +0100232 dev_priv->mm.gtt_total = end - start;
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200233 dev_priv->mm.mappable_gtt_total = min(end, mappable_end) - start;
Daniel Vetter53984632010-09-22 23:44:24 +0200234 dev_priv->mm.gtt_mappable_end = mappable_end;
Jesse Barnes79e53942008-11-07 14:24:08 -0800235
236 return 0;
237}
Keith Packard6dbe2772008-10-14 21:41:13 -0700238
Eric Anholt673a3942008-07-30 12:06:12 -0700239int
240i915_gem_init_ioctl(struct drm_device *dev, void *data,
241 struct drm_file *file_priv)
242{
Eric Anholt673a3942008-07-30 12:06:12 -0700243 struct drm_i915_gem_init *args = data;
Jesse Barnes79e53942008-11-07 14:24:08 -0800244 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700245
246 mutex_lock(&dev->struct_mutex);
Daniel Vetter53984632010-09-22 23:44:24 +0200247 ret = i915_gem_do_init(dev, args->gtt_start, args->gtt_end, args->gtt_end);
Eric Anholt673a3942008-07-30 12:06:12 -0700248 mutex_unlock(&dev->struct_mutex);
249
Jesse Barnes79e53942008-11-07 14:24:08 -0800250 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700251}
252
Eric Anholt5a125c32008-10-22 21:40:13 -0700253int
254i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
255 struct drm_file *file_priv)
256{
Chris Wilson73aa8082010-09-30 11:46:12 +0100257 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt5a125c32008-10-22 21:40:13 -0700258 struct drm_i915_gem_get_aperture *args = data;
Eric Anholt5a125c32008-10-22 21:40:13 -0700259
260 if (!(dev->driver->driver_features & DRIVER_GEM))
261 return -ENODEV;
262
Chris Wilson73aa8082010-09-30 11:46:12 +0100263 mutex_lock(&dev->struct_mutex);
264 args->aper_size = dev_priv->mm.gtt_total;
265 args->aper_available_size = args->aper_size - dev_priv->mm.pin_memory;
266 mutex_unlock(&dev->struct_mutex);
Eric Anholt5a125c32008-10-22 21:40:13 -0700267
268 return 0;
269}
270
Eric Anholt673a3942008-07-30 12:06:12 -0700271
272/**
273 * Creates a new mm object and returns a handle to it.
274 */
275int
276i915_gem_create_ioctl(struct drm_device *dev, void *data,
277 struct drm_file *file_priv)
278{
279 struct drm_i915_gem_create *args = data;
280 struct drm_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300281 int ret;
282 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700283
284 args->size = roundup(args->size, PAGE_SIZE);
285
286 /* Allocate the new object */
Daniel Vetterac52bc52010-04-09 19:05:06 +0000287 obj = i915_gem_alloc_object(dev, args->size);
Eric Anholt673a3942008-07-30 12:06:12 -0700288 if (obj == NULL)
289 return -ENOMEM;
290
291 ret = drm_gem_handle_create(file_priv, obj, &handle);
Chris Wilson1dfd9752010-09-06 14:44:14 +0100292 if (ret) {
Chris Wilson202f2fe2010-10-14 13:20:40 +0100293 drm_gem_object_release(obj);
294 i915_gem_info_remove_obj(dev->dev_private, obj->size);
295 kfree(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700296 return ret;
Chris Wilson1dfd9752010-09-06 14:44:14 +0100297 }
298
Chris Wilson202f2fe2010-10-14 13:20:40 +0100299 /* drop reference from allocate - handle holds it now */
300 drm_gem_object_unreference(obj);
301 trace_i915_gem_object_create(obj);
302
Eric Anholt673a3942008-07-30 12:06:12 -0700303 args->handle = handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700304 return 0;
305}
306
Eric Anholt280b7132009-03-12 16:56:27 -0700307static int i915_gem_object_needs_bit17_swizzle(struct drm_gem_object *obj)
308{
309 drm_i915_private_t *dev_priv = obj->dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +0100310 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt280b7132009-03-12 16:56:27 -0700311
312 return dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_9_10_17 &&
313 obj_priv->tiling_mode != I915_TILING_NONE;
314}
315
Chris Wilson99a03df2010-05-27 14:15:34 +0100316static inline void
Eric Anholt40123c12009-03-09 13:42:30 -0700317slow_shmem_copy(struct page *dst_page,
318 int dst_offset,
319 struct page *src_page,
320 int src_offset,
321 int length)
322{
323 char *dst_vaddr, *src_vaddr;
324
Chris Wilson99a03df2010-05-27 14:15:34 +0100325 dst_vaddr = kmap(dst_page);
326 src_vaddr = kmap(src_page);
Eric Anholt40123c12009-03-09 13:42:30 -0700327
328 memcpy(dst_vaddr + dst_offset, src_vaddr + src_offset, length);
329
Chris Wilson99a03df2010-05-27 14:15:34 +0100330 kunmap(src_page);
331 kunmap(dst_page);
Eric Anholt40123c12009-03-09 13:42:30 -0700332}
333
Chris Wilson99a03df2010-05-27 14:15:34 +0100334static inline void
Eric Anholt280b7132009-03-12 16:56:27 -0700335slow_shmem_bit17_copy(struct page *gpu_page,
336 int gpu_offset,
337 struct page *cpu_page,
338 int cpu_offset,
339 int length,
340 int is_read)
341{
342 char *gpu_vaddr, *cpu_vaddr;
343
344 /* Use the unswizzled path if this page isn't affected. */
345 if ((page_to_phys(gpu_page) & (1 << 17)) == 0) {
346 if (is_read)
347 return slow_shmem_copy(cpu_page, cpu_offset,
348 gpu_page, gpu_offset, length);
349 else
350 return slow_shmem_copy(gpu_page, gpu_offset,
351 cpu_page, cpu_offset, length);
352 }
353
Chris Wilson99a03df2010-05-27 14:15:34 +0100354 gpu_vaddr = kmap(gpu_page);
355 cpu_vaddr = kmap(cpu_page);
Eric Anholt280b7132009-03-12 16:56:27 -0700356
357 /* Copy the data, XORing A6 with A17 (1). The user already knows he's
358 * XORing with the other bits (A9 for Y, A9 and A10 for X)
359 */
360 while (length > 0) {
361 int cacheline_end = ALIGN(gpu_offset + 1, 64);
362 int this_length = min(cacheline_end - gpu_offset, length);
363 int swizzled_gpu_offset = gpu_offset ^ 64;
364
365 if (is_read) {
366 memcpy(cpu_vaddr + cpu_offset,
367 gpu_vaddr + swizzled_gpu_offset,
368 this_length);
369 } else {
370 memcpy(gpu_vaddr + swizzled_gpu_offset,
371 cpu_vaddr + cpu_offset,
372 this_length);
373 }
374 cpu_offset += this_length;
375 gpu_offset += this_length;
376 length -= this_length;
377 }
378
Chris Wilson99a03df2010-05-27 14:15:34 +0100379 kunmap(cpu_page);
380 kunmap(gpu_page);
Eric Anholt280b7132009-03-12 16:56:27 -0700381}
382
Eric Anholt673a3942008-07-30 12:06:12 -0700383/**
Eric Anholteb014592009-03-10 11:44:52 -0700384 * This is the fast shmem pread path, which attempts to copy_from_user directly
385 * from the backing pages of the object to the user's address space. On a
386 * fault, it fails so we can fall back to i915_gem_shmem_pwrite_slow().
387 */
388static int
389i915_gem_shmem_pread_fast(struct drm_device *dev, struct drm_gem_object *obj,
390 struct drm_i915_gem_pread *args,
391 struct drm_file *file_priv)
392{
Daniel Vetter23010e42010-03-08 13:35:02 +0100393 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsone5281cc2010-10-28 13:45:36 +0100394 struct address_space *mapping = obj->filp->f_path.dentry->d_inode->i_mapping;
Eric Anholteb014592009-03-10 11:44:52 -0700395 ssize_t remain;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100396 loff_t offset;
Eric Anholteb014592009-03-10 11:44:52 -0700397 char __user *user_data;
398 int page_offset, page_length;
Eric Anholteb014592009-03-10 11:44:52 -0700399
400 user_data = (char __user *) (uintptr_t) args->data_ptr;
401 remain = args->size;
402
Daniel Vetter23010e42010-03-08 13:35:02 +0100403 obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700404 offset = args->offset;
405
406 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100407 struct page *page;
408 char *vaddr;
409 int ret;
410
Eric Anholteb014592009-03-10 11:44:52 -0700411 /* Operation in this page
412 *
Eric Anholteb014592009-03-10 11:44:52 -0700413 * page_offset = offset within page
414 * page_length = bytes to copy for this page
415 */
Eric Anholteb014592009-03-10 11:44:52 -0700416 page_offset = offset & (PAGE_SIZE-1);
417 page_length = remain;
418 if ((page_offset + remain) > PAGE_SIZE)
419 page_length = PAGE_SIZE - page_offset;
420
Chris Wilsone5281cc2010-10-28 13:45:36 +0100421 page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
422 GFP_HIGHUSER | __GFP_RECLAIMABLE);
423 if (IS_ERR(page))
424 return PTR_ERR(page);
425
426 vaddr = kmap_atomic(page);
427 ret = __copy_to_user_inatomic(user_data,
428 vaddr + page_offset,
429 page_length);
430 kunmap_atomic(vaddr);
431
432 mark_page_accessed(page);
433 page_cache_release(page);
434 if (ret)
Chris Wilson4f27b752010-10-14 15:26:45 +0100435 return -EFAULT;
Eric Anholteb014592009-03-10 11:44:52 -0700436
437 remain -= page_length;
438 user_data += page_length;
439 offset += page_length;
440 }
441
Chris Wilson4f27b752010-10-14 15:26:45 +0100442 return 0;
Eric Anholteb014592009-03-10 11:44:52 -0700443}
444
445/**
446 * This is the fallback shmem pread path, which allocates temporary storage
447 * in kernel space to copy_to_user into outside of the struct_mutex, so we
448 * can copy out of the object's backing pages while holding the struct mutex
449 * and not take page faults.
450 */
451static int
452i915_gem_shmem_pread_slow(struct drm_device *dev, struct drm_gem_object *obj,
453 struct drm_i915_gem_pread *args,
454 struct drm_file *file_priv)
455{
Chris Wilsone5281cc2010-10-28 13:45:36 +0100456 struct address_space *mapping = obj->filp->f_path.dentry->d_inode->i_mapping;
Daniel Vetter23010e42010-03-08 13:35:02 +0100457 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700458 struct mm_struct *mm = current->mm;
459 struct page **user_pages;
460 ssize_t remain;
461 loff_t offset, pinned_pages, i;
462 loff_t first_data_page, last_data_page, num_pages;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100463 int shmem_page_offset;
464 int data_page_index, data_page_offset;
Eric Anholteb014592009-03-10 11:44:52 -0700465 int page_length;
466 int ret;
467 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700468 int do_bit17_swizzling;
Eric Anholteb014592009-03-10 11:44:52 -0700469
470 remain = args->size;
471
472 /* Pin the user pages containing the data. We can't fault while
473 * holding the struct mutex, yet we want to hold it while
474 * dereferencing the user data.
475 */
476 first_data_page = data_ptr / PAGE_SIZE;
477 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
478 num_pages = last_data_page - first_data_page + 1;
479
Chris Wilson4f27b752010-10-14 15:26:45 +0100480 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
Eric Anholteb014592009-03-10 11:44:52 -0700481 if (user_pages == NULL)
482 return -ENOMEM;
483
Chris Wilson4f27b752010-10-14 15:26:45 +0100484 mutex_unlock(&dev->struct_mutex);
Eric Anholteb014592009-03-10 11:44:52 -0700485 down_read(&mm->mmap_sem);
486 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
Eric Anholte5e9ecd2009-04-07 16:01:22 -0700487 num_pages, 1, 0, user_pages, NULL);
Eric Anholteb014592009-03-10 11:44:52 -0700488 up_read(&mm->mmap_sem);
Chris Wilson4f27b752010-10-14 15:26:45 +0100489 mutex_lock(&dev->struct_mutex);
Eric Anholteb014592009-03-10 11:44:52 -0700490 if (pinned_pages < num_pages) {
491 ret = -EFAULT;
Chris Wilson4f27b752010-10-14 15:26:45 +0100492 goto out;
Eric Anholteb014592009-03-10 11:44:52 -0700493 }
494
Chris Wilson4f27b752010-10-14 15:26:45 +0100495 ret = i915_gem_object_set_cpu_read_domain_range(obj,
496 args->offset,
Eric Anholteb014592009-03-10 11:44:52 -0700497 args->size);
Chris Wilson4f27b752010-10-14 15:26:45 +0100498 if (ret)
499 goto out;
500
501 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700502
Daniel Vetter23010e42010-03-08 13:35:02 +0100503 obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700504 offset = args->offset;
505
506 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100507 struct page *page;
508
Eric Anholteb014592009-03-10 11:44:52 -0700509 /* Operation in this page
510 *
Eric Anholteb014592009-03-10 11:44:52 -0700511 * shmem_page_offset = offset within page in shmem file
512 * data_page_index = page number in get_user_pages return
513 * data_page_offset = offset with data_page_index page.
514 * page_length = bytes to copy for this page
515 */
Eric Anholteb014592009-03-10 11:44:52 -0700516 shmem_page_offset = offset & ~PAGE_MASK;
517 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
518 data_page_offset = data_ptr & ~PAGE_MASK;
519
520 page_length = remain;
521 if ((shmem_page_offset + page_length) > PAGE_SIZE)
522 page_length = PAGE_SIZE - shmem_page_offset;
523 if ((data_page_offset + page_length) > PAGE_SIZE)
524 page_length = PAGE_SIZE - data_page_offset;
525
Chris Wilsone5281cc2010-10-28 13:45:36 +0100526 page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
527 GFP_HIGHUSER | __GFP_RECLAIMABLE);
528 if (IS_ERR(page))
529 return PTR_ERR(page);
530
Eric Anholt280b7132009-03-12 16:56:27 -0700531 if (do_bit17_swizzling) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100532 slow_shmem_bit17_copy(page,
Eric Anholt280b7132009-03-12 16:56:27 -0700533 shmem_page_offset,
Chris Wilson99a03df2010-05-27 14:15:34 +0100534 user_pages[data_page_index],
535 data_page_offset,
536 page_length,
537 1);
538 } else {
539 slow_shmem_copy(user_pages[data_page_index],
540 data_page_offset,
Chris Wilsone5281cc2010-10-28 13:45:36 +0100541 page,
Chris Wilson99a03df2010-05-27 14:15:34 +0100542 shmem_page_offset,
543 page_length);
Eric Anholt280b7132009-03-12 16:56:27 -0700544 }
Eric Anholteb014592009-03-10 11:44:52 -0700545
Chris Wilsone5281cc2010-10-28 13:45:36 +0100546 mark_page_accessed(page);
547 page_cache_release(page);
548
Eric Anholteb014592009-03-10 11:44:52 -0700549 remain -= page_length;
550 data_ptr += page_length;
551 offset += page_length;
552 }
553
Chris Wilson4f27b752010-10-14 15:26:45 +0100554out:
Eric Anholteb014592009-03-10 11:44:52 -0700555 for (i = 0; i < pinned_pages; i++) {
556 SetPageDirty(user_pages[i]);
Chris Wilsone5281cc2010-10-28 13:45:36 +0100557 mark_page_accessed(user_pages[i]);
Eric Anholteb014592009-03-10 11:44:52 -0700558 page_cache_release(user_pages[i]);
559 }
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700560 drm_free_large(user_pages);
Eric Anholteb014592009-03-10 11:44:52 -0700561
562 return ret;
563}
564
Eric Anholt673a3942008-07-30 12:06:12 -0700565/**
566 * Reads data from the object referenced by handle.
567 *
568 * On error, the contents of *data are undefined.
569 */
570int
571i915_gem_pread_ioctl(struct drm_device *dev, void *data,
572 struct drm_file *file_priv)
573{
574 struct drm_i915_gem_pread *args = data;
575 struct drm_gem_object *obj;
576 struct drm_i915_gem_object *obj_priv;
Chris Wilson35b62a82010-09-26 20:23:38 +0100577 int ret = 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700578
Chris Wilson4f27b752010-10-14 15:26:45 +0100579 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100580 if (ret)
Chris Wilson4f27b752010-10-14 15:26:45 +0100581 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700582
583 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100584 if (obj == NULL) {
585 ret = -ENOENT;
586 goto unlock;
Chris Wilson4f27b752010-10-14 15:26:45 +0100587 }
Daniel Vetter23010e42010-03-08 13:35:02 +0100588 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700589
Chris Wilson7dcd2492010-09-26 20:21:44 +0100590 /* Bounds check source. */
591 if (args->offset > obj->size || args->size > obj->size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +0100592 ret = -EINVAL;
Chris Wilson35b62a82010-09-26 20:23:38 +0100593 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +0100594 }
595
Chris Wilson35b62a82010-09-26 20:23:38 +0100596 if (args->size == 0)
597 goto out;
598
Chris Wilsonce9d4192010-09-26 20:50:05 +0100599 if (!access_ok(VERIFY_WRITE,
600 (char __user *)(uintptr_t)args->data_ptr,
601 args->size)) {
602 ret = -EFAULT;
Chris Wilson35b62a82010-09-26 20:23:38 +0100603 goto out;
Eric Anholt673a3942008-07-30 12:06:12 -0700604 }
605
Chris Wilsonb5e4feb2010-10-14 13:47:43 +0100606 ret = fault_in_pages_writeable((char __user *)(uintptr_t)args->data_ptr,
607 args->size);
608 if (ret) {
609 ret = -EFAULT;
610 goto out;
611 }
612
Chris Wilson4f27b752010-10-14 15:26:45 +0100613 ret = i915_gem_object_set_cpu_read_domain_range(obj,
614 args->offset,
615 args->size);
616 if (ret)
Chris Wilsone5281cc2010-10-28 13:45:36 +0100617 goto out;
Chris Wilson4f27b752010-10-14 15:26:45 +0100618
619 ret = -EFAULT;
620 if (!i915_gem_object_needs_bit17_swizzle(obj))
Eric Anholt280b7132009-03-12 16:56:27 -0700621 ret = i915_gem_shmem_pread_fast(dev, obj, args, file_priv);
Chris Wilson4f27b752010-10-14 15:26:45 +0100622 if (ret == -EFAULT)
623 ret = i915_gem_shmem_pread_slow(dev, obj, args, file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -0700624
Chris Wilson35b62a82010-09-26 20:23:38 +0100625out:
Chris Wilson4f27b752010-10-14 15:26:45 +0100626 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100627unlock:
Chris Wilson4f27b752010-10-14 15:26:45 +0100628 mutex_unlock(&dev->struct_mutex);
Eric Anholteb014592009-03-10 11:44:52 -0700629 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700630}
631
Keith Packard0839ccb2008-10-30 19:38:48 -0700632/* This is the fast write path which cannot handle
633 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700634 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700635
Keith Packard0839ccb2008-10-30 19:38:48 -0700636static inline int
637fast_user_write(struct io_mapping *mapping,
638 loff_t page_base, int page_offset,
639 char __user *user_data,
640 int length)
641{
642 char *vaddr_atomic;
643 unsigned long unwritten;
644
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700645 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
Keith Packard0839ccb2008-10-30 19:38:48 -0700646 unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
647 user_data, length);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700648 io_mapping_unmap_atomic(vaddr_atomic);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100649 return unwritten;
Keith Packard0839ccb2008-10-30 19:38:48 -0700650}
651
652/* Here's the write path which can sleep for
653 * page faults
654 */
655
Chris Wilsonab34c222010-05-27 14:15:35 +0100656static inline void
Eric Anholt3de09aa2009-03-09 09:42:23 -0700657slow_kernel_write(struct io_mapping *mapping,
658 loff_t gtt_base, int gtt_offset,
659 struct page *user_page, int user_offset,
660 int length)
Keith Packard0839ccb2008-10-30 19:38:48 -0700661{
Chris Wilsonab34c222010-05-27 14:15:35 +0100662 char __iomem *dst_vaddr;
663 char *src_vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -0700664
Chris Wilsonab34c222010-05-27 14:15:35 +0100665 dst_vaddr = io_mapping_map_wc(mapping, gtt_base);
666 src_vaddr = kmap(user_page);
667
668 memcpy_toio(dst_vaddr + gtt_offset,
669 src_vaddr + user_offset,
670 length);
671
672 kunmap(user_page);
673 io_mapping_unmap(dst_vaddr);
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700674}
675
Eric Anholt3de09aa2009-03-09 09:42:23 -0700676/**
677 * This is the fast pwrite path, where we copy the data directly from the
678 * user into the GTT, uncached.
679 */
Eric Anholt673a3942008-07-30 12:06:12 -0700680static int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700681i915_gem_gtt_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
682 struct drm_i915_gem_pwrite *args,
683 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700684{
Daniel Vetter23010e42010-03-08 13:35:02 +0100685 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Keith Packard0839ccb2008-10-30 19:38:48 -0700686 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700687 ssize_t remain;
Keith Packard0839ccb2008-10-30 19:38:48 -0700688 loff_t offset, page_base;
Eric Anholt673a3942008-07-30 12:06:12 -0700689 char __user *user_data;
Keith Packard0839ccb2008-10-30 19:38:48 -0700690 int page_offset, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700691
692 user_data = (char __user *) (uintptr_t) args->data_ptr;
693 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700694
Daniel Vetter23010e42010-03-08 13:35:02 +0100695 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700696 offset = obj_priv->gtt_offset + args->offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700697
698 while (remain > 0) {
699 /* Operation in this page
700 *
Keith Packard0839ccb2008-10-30 19:38:48 -0700701 * page_base = page offset within aperture
702 * page_offset = offset within page
703 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -0700704 */
Keith Packard0839ccb2008-10-30 19:38:48 -0700705 page_base = (offset & ~(PAGE_SIZE-1));
706 page_offset = offset & (PAGE_SIZE-1);
707 page_length = remain;
708 if ((page_offset + remain) > PAGE_SIZE)
709 page_length = PAGE_SIZE - page_offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700710
Keith Packard0839ccb2008-10-30 19:38:48 -0700711 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -0700712 * source page isn't available. Return the error and we'll
713 * retry in the slow path.
Keith Packard0839ccb2008-10-30 19:38:48 -0700714 */
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100715 if (fast_user_write(dev_priv->mm.gtt_mapping, page_base,
716 page_offset, user_data, page_length))
717
718 return -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -0700719
Keith Packard0839ccb2008-10-30 19:38:48 -0700720 remain -= page_length;
721 user_data += page_length;
722 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700723 }
Eric Anholt673a3942008-07-30 12:06:12 -0700724
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100725 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700726}
727
Eric Anholt3de09aa2009-03-09 09:42:23 -0700728/**
729 * This is the fallback GTT pwrite path, which uses get_user_pages to pin
730 * the memory and maps it using kmap_atomic for copying.
731 *
732 * This code resulted in x11perf -rgb10text consuming about 10% more CPU
733 * than using i915_gem_gtt_pwrite_fast on a G45 (32-bit).
734 */
Eric Anholt3043c602008-10-02 12:24:47 -0700735static int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700736i915_gem_gtt_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
737 struct drm_i915_gem_pwrite *args,
738 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700739{
Daniel Vetter23010e42010-03-08 13:35:02 +0100740 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700741 drm_i915_private_t *dev_priv = dev->dev_private;
742 ssize_t remain;
743 loff_t gtt_page_base, offset;
744 loff_t first_data_page, last_data_page, num_pages;
745 loff_t pinned_pages, i;
746 struct page **user_pages;
747 struct mm_struct *mm = current->mm;
748 int gtt_page_offset, data_page_offset, data_page_index, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700749 int ret;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700750 uint64_t data_ptr = args->data_ptr;
751
752 remain = args->size;
753
754 /* Pin the user pages containing the data. We can't fault while
755 * holding the struct mutex, and all of the pwrite implementations
756 * want to hold it while dereferencing the user data.
757 */
758 first_data_page = data_ptr / PAGE_SIZE;
759 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
760 num_pages = last_data_page - first_data_page + 1;
761
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100762 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
Eric Anholt3de09aa2009-03-09 09:42:23 -0700763 if (user_pages == NULL)
764 return -ENOMEM;
765
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100766 mutex_unlock(&dev->struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700767 down_read(&mm->mmap_sem);
768 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
769 num_pages, 0, 0, user_pages, NULL);
770 up_read(&mm->mmap_sem);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100771 mutex_lock(&dev->struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700772 if (pinned_pages < num_pages) {
773 ret = -EFAULT;
774 goto out_unpin_pages;
775 }
776
Eric Anholt3de09aa2009-03-09 09:42:23 -0700777 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
778 if (ret)
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100779 goto out_unpin_pages;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700780
Daniel Vetter23010e42010-03-08 13:35:02 +0100781 obj_priv = to_intel_bo(obj);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700782 offset = obj_priv->gtt_offset + args->offset;
783
784 while (remain > 0) {
785 /* Operation in this page
786 *
787 * gtt_page_base = page offset within aperture
788 * gtt_page_offset = offset within page in aperture
789 * data_page_index = page number in get_user_pages return
790 * data_page_offset = offset with data_page_index page.
791 * page_length = bytes to copy for this page
792 */
793 gtt_page_base = offset & PAGE_MASK;
794 gtt_page_offset = offset & ~PAGE_MASK;
795 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
796 data_page_offset = data_ptr & ~PAGE_MASK;
797
798 page_length = remain;
799 if ((gtt_page_offset + page_length) > PAGE_SIZE)
800 page_length = PAGE_SIZE - gtt_page_offset;
801 if ((data_page_offset + page_length) > PAGE_SIZE)
802 page_length = PAGE_SIZE - data_page_offset;
803
Chris Wilsonab34c222010-05-27 14:15:35 +0100804 slow_kernel_write(dev_priv->mm.gtt_mapping,
805 gtt_page_base, gtt_page_offset,
806 user_pages[data_page_index],
807 data_page_offset,
808 page_length);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700809
810 remain -= page_length;
811 offset += page_length;
812 data_ptr += page_length;
813 }
814
Eric Anholt3de09aa2009-03-09 09:42:23 -0700815out_unpin_pages:
816 for (i = 0; i < pinned_pages; i++)
817 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700818 drm_free_large(user_pages);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700819
820 return ret;
821}
822
Eric Anholt40123c12009-03-09 13:42:30 -0700823/**
824 * This is the fast shmem pwrite path, which attempts to directly
825 * copy_from_user into the kmapped pages backing the object.
826 */
Eric Anholt673a3942008-07-30 12:06:12 -0700827static int
Eric Anholt40123c12009-03-09 13:42:30 -0700828i915_gem_shmem_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
829 struct drm_i915_gem_pwrite *args,
830 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700831{
Chris Wilsone5281cc2010-10-28 13:45:36 +0100832 struct address_space *mapping = obj->filp->f_path.dentry->d_inode->i_mapping;
Daniel Vetter23010e42010-03-08 13:35:02 +0100833 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700834 ssize_t remain;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100835 loff_t offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700836 char __user *user_data;
837 int page_offset, page_length;
Eric Anholt40123c12009-03-09 13:42:30 -0700838
839 user_data = (char __user *) (uintptr_t) args->data_ptr;
840 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700841
Daniel Vetter23010e42010-03-08 13:35:02 +0100842 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700843 offset = args->offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700844 obj_priv->dirty = 1;
Eric Anholt673a3942008-07-30 12:06:12 -0700845
Eric Anholt40123c12009-03-09 13:42:30 -0700846 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100847 struct page *page;
848 char *vaddr;
849 int ret;
850
Eric Anholt40123c12009-03-09 13:42:30 -0700851 /* Operation in this page
852 *
Eric Anholt40123c12009-03-09 13:42:30 -0700853 * page_offset = offset within page
854 * page_length = bytes to copy for this page
855 */
Eric Anholt40123c12009-03-09 13:42:30 -0700856 page_offset = offset & (PAGE_SIZE-1);
857 page_length = remain;
858 if ((page_offset + remain) > PAGE_SIZE)
859 page_length = PAGE_SIZE - page_offset;
860
Chris Wilsone5281cc2010-10-28 13:45:36 +0100861 page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
862 GFP_HIGHUSER | __GFP_RECLAIMABLE);
863 if (IS_ERR(page))
864 return PTR_ERR(page);
865
866 vaddr = kmap_atomic(page, KM_USER0);
867 ret = __copy_from_user_inatomic(vaddr + page_offset,
868 user_data,
869 page_length);
870 kunmap_atomic(vaddr, KM_USER0);
871
872 set_page_dirty(page);
873 mark_page_accessed(page);
874 page_cache_release(page);
875
876 /* If we get a fault while copying data, then (presumably) our
877 * source page isn't available. Return the error and we'll
878 * retry in the slow path.
879 */
880 if (ret)
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100881 return -EFAULT;
Eric Anholt40123c12009-03-09 13:42:30 -0700882
883 remain -= page_length;
884 user_data += page_length;
885 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700886 }
887
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100888 return 0;
Eric Anholt40123c12009-03-09 13:42:30 -0700889}
890
891/**
892 * This is the fallback shmem pwrite path, which uses get_user_pages to pin
893 * the memory and maps it using kmap_atomic for copying.
894 *
895 * This avoids taking mmap_sem for faulting on the user's address while the
896 * struct_mutex is held.
897 */
898static int
899i915_gem_shmem_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
900 struct drm_i915_gem_pwrite *args,
901 struct drm_file *file_priv)
902{
Chris Wilsone5281cc2010-10-28 13:45:36 +0100903 struct address_space *mapping = obj->filp->f_path.dentry->d_inode->i_mapping;
Daniel Vetter23010e42010-03-08 13:35:02 +0100904 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700905 struct mm_struct *mm = current->mm;
906 struct page **user_pages;
907 ssize_t remain;
908 loff_t offset, pinned_pages, i;
909 loff_t first_data_page, last_data_page, num_pages;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100910 int shmem_page_offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700911 int data_page_index, data_page_offset;
912 int page_length;
913 int ret;
914 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700915 int do_bit17_swizzling;
Eric Anholt40123c12009-03-09 13:42:30 -0700916
917 remain = args->size;
918
919 /* Pin the user pages containing the data. We can't fault while
920 * holding the struct mutex, and all of the pwrite implementations
921 * want to hold it while dereferencing the user data.
922 */
923 first_data_page = data_ptr / PAGE_SIZE;
924 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
925 num_pages = last_data_page - first_data_page + 1;
926
Chris Wilson4f27b752010-10-14 15:26:45 +0100927 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
Eric Anholt40123c12009-03-09 13:42:30 -0700928 if (user_pages == NULL)
929 return -ENOMEM;
930
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100931 mutex_unlock(&dev->struct_mutex);
Eric Anholt40123c12009-03-09 13:42:30 -0700932 down_read(&mm->mmap_sem);
933 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
934 num_pages, 0, 0, user_pages, NULL);
935 up_read(&mm->mmap_sem);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100936 mutex_lock(&dev->struct_mutex);
Eric Anholt40123c12009-03-09 13:42:30 -0700937 if (pinned_pages < num_pages) {
938 ret = -EFAULT;
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100939 goto out;
Eric Anholt40123c12009-03-09 13:42:30 -0700940 }
941
Eric Anholt40123c12009-03-09 13:42:30 -0700942 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100943 if (ret)
944 goto out;
945
946 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700947
Daniel Vetter23010e42010-03-08 13:35:02 +0100948 obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700949 offset = args->offset;
950 obj_priv->dirty = 1;
951
952 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100953 struct page *page;
954
Eric Anholt40123c12009-03-09 13:42:30 -0700955 /* Operation in this page
956 *
Eric Anholt40123c12009-03-09 13:42:30 -0700957 * shmem_page_offset = offset within page in shmem file
958 * data_page_index = page number in get_user_pages return
959 * data_page_offset = offset with data_page_index page.
960 * page_length = bytes to copy for this page
961 */
Eric Anholt40123c12009-03-09 13:42:30 -0700962 shmem_page_offset = offset & ~PAGE_MASK;
963 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
964 data_page_offset = data_ptr & ~PAGE_MASK;
965
966 page_length = remain;
967 if ((shmem_page_offset + page_length) > PAGE_SIZE)
968 page_length = PAGE_SIZE - shmem_page_offset;
969 if ((data_page_offset + page_length) > PAGE_SIZE)
970 page_length = PAGE_SIZE - data_page_offset;
971
Chris Wilsone5281cc2010-10-28 13:45:36 +0100972 page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
973 GFP_HIGHUSER | __GFP_RECLAIMABLE);
974 if (IS_ERR(page)) {
975 ret = PTR_ERR(page);
976 goto out;
977 }
978
Eric Anholt280b7132009-03-12 16:56:27 -0700979 if (do_bit17_swizzling) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100980 slow_shmem_bit17_copy(page,
Eric Anholt280b7132009-03-12 16:56:27 -0700981 shmem_page_offset,
982 user_pages[data_page_index],
983 data_page_offset,
Chris Wilson99a03df2010-05-27 14:15:34 +0100984 page_length,
985 0);
986 } else {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100987 slow_shmem_copy(page,
Chris Wilson99a03df2010-05-27 14:15:34 +0100988 shmem_page_offset,
989 user_pages[data_page_index],
990 data_page_offset,
991 page_length);
Eric Anholt280b7132009-03-12 16:56:27 -0700992 }
Eric Anholt40123c12009-03-09 13:42:30 -0700993
Chris Wilsone5281cc2010-10-28 13:45:36 +0100994 set_page_dirty(page);
995 mark_page_accessed(page);
996 page_cache_release(page);
997
Eric Anholt40123c12009-03-09 13:42:30 -0700998 remain -= page_length;
999 data_ptr += page_length;
1000 offset += page_length;
1001 }
1002
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001003out:
Eric Anholt40123c12009-03-09 13:42:30 -07001004 for (i = 0; i < pinned_pages; i++)
1005 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07001006 drm_free_large(user_pages);
Eric Anholt40123c12009-03-09 13:42:30 -07001007
1008 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001009}
1010
1011/**
1012 * Writes data to the object referenced by handle.
1013 *
1014 * On error, the contents of the buffer that were to be modified are undefined.
1015 */
1016int
1017i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001018 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001019{
1020 struct drm_i915_gem_pwrite *args = data;
1021 struct drm_gem_object *obj;
1022 struct drm_i915_gem_object *obj_priv;
1023 int ret = 0;
1024
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001025 ret = i915_mutex_lock_interruptible(dev);
1026 if (ret)
1027 return ret;
1028
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001029 obj = drm_gem_object_lookup(dev, file, args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001030 if (obj == NULL) {
1031 ret = -ENOENT;
1032 goto unlock;
1033 }
Daniel Vetter23010e42010-03-08 13:35:02 +01001034 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001035
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001036
Chris Wilson7dcd2492010-09-26 20:21:44 +01001037 /* Bounds check destination. */
1038 if (args->offset > obj->size || args->size > obj->size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001039 ret = -EINVAL;
Chris Wilson35b62a82010-09-26 20:23:38 +01001040 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001041 }
1042
Chris Wilson35b62a82010-09-26 20:23:38 +01001043 if (args->size == 0)
1044 goto out;
1045
Chris Wilsonce9d4192010-09-26 20:50:05 +01001046 if (!access_ok(VERIFY_READ,
1047 (char __user *)(uintptr_t)args->data_ptr,
1048 args->size)) {
1049 ret = -EFAULT;
Chris Wilson35b62a82010-09-26 20:23:38 +01001050 goto out;
Eric Anholt673a3942008-07-30 12:06:12 -07001051 }
1052
Chris Wilsonb5e4feb2010-10-14 13:47:43 +01001053 ret = fault_in_pages_readable((char __user *)(uintptr_t)args->data_ptr,
1054 args->size);
1055 if (ret) {
1056 ret = -EFAULT;
1057 goto out;
Eric Anholt673a3942008-07-30 12:06:12 -07001058 }
1059
1060 /* We can only do the GTT pwrite on untiled buffers, as otherwise
1061 * it would end up going through the fenced access, and we'll get
1062 * different detiling behavior between reading and writing.
1063 * pread/pwrite currently are reading and writing from the CPU
1064 * perspective, requiring manual detiling by the client.
1065 */
Dave Airlie71acb5e2008-12-30 20:31:46 +10001066 if (obj_priv->phys_obj)
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001067 ret = i915_gem_phys_pwrite(dev, obj, args, file);
Dave Airlie71acb5e2008-12-30 20:31:46 +10001068 else if (obj_priv->tiling_mode == I915_TILING_NONE &&
Chris Wilson5cdf5882010-09-27 15:51:07 +01001069 obj_priv->gtt_space &&
Chris Wilson9b8c4a02010-05-27 14:21:01 +01001070 obj->write_domain != I915_GEM_DOMAIN_CPU) {
Chris Wilsona00b10c2010-09-24 21:15:47 +01001071 ret = i915_gem_object_pin(obj, 0, true, false);
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001072 if (ret)
1073 goto out;
1074
1075 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
1076 if (ret)
1077 goto out_unpin;
1078
1079 ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file);
1080 if (ret == -EFAULT)
1081 ret = i915_gem_gtt_pwrite_slow(dev, obj, args, file);
1082
1083out_unpin:
1084 i915_gem_object_unpin(obj);
Eric Anholt40123c12009-03-09 13:42:30 -07001085 } else {
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001086 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
1087 if (ret)
Chris Wilsone5281cc2010-10-28 13:45:36 +01001088 goto out;
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001089
1090 ret = -EFAULT;
1091 if (!i915_gem_object_needs_bit17_swizzle(obj))
1092 ret = i915_gem_shmem_pwrite_fast(dev, obj, args, file);
1093 if (ret == -EFAULT)
1094 ret = i915_gem_shmem_pwrite_slow(dev, obj, args, file);
Eric Anholt40123c12009-03-09 13:42:30 -07001095 }
Eric Anholt673a3942008-07-30 12:06:12 -07001096
Chris Wilson35b62a82010-09-26 20:23:38 +01001097out:
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001098 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001099unlock:
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001100 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07001101 return ret;
1102}
1103
1104/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001105 * Called when user space prepares to use an object with the CPU, either
1106 * through the mmap ioctl's mapping or a GTT mapping.
Eric Anholt673a3942008-07-30 12:06:12 -07001107 */
1108int
1109i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
1110 struct drm_file *file_priv)
1111{
Eric Anholta09ba7f2009-08-29 12:49:51 -07001112 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07001113 struct drm_i915_gem_set_domain *args = data;
1114 struct drm_gem_object *obj;
Jesse Barnes652c3932009-08-17 13:31:43 -07001115 struct drm_i915_gem_object *obj_priv;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001116 uint32_t read_domains = args->read_domains;
1117 uint32_t write_domain = args->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07001118 int ret;
1119
1120 if (!(dev->driver->driver_features & DRIVER_GEM))
1121 return -ENODEV;
1122
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001123 /* Only handle setting domains to types used by the CPU. */
Chris Wilson21d509e2009-06-06 09:46:02 +01001124 if (write_domain & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001125 return -EINVAL;
1126
Chris Wilson21d509e2009-06-06 09:46:02 +01001127 if (read_domains & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001128 return -EINVAL;
1129
1130 /* Having something in the write domain implies it's in the read
1131 * domain, and only that read domain. Enforce that in the request.
1132 */
1133 if (write_domain != 0 && read_domains != write_domain)
1134 return -EINVAL;
1135
Chris Wilson76c1dec2010-09-25 11:22:51 +01001136 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001137 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001138 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001139
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001140 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1141 if (obj == NULL) {
1142 ret = -ENOENT;
1143 goto unlock;
Chris Wilson76c1dec2010-09-25 11:22:51 +01001144 }
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001145 obj_priv = to_intel_bo(obj);
Jesse Barnes652c3932009-08-17 13:31:43 -07001146
1147 intel_mark_busy(dev, obj);
1148
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001149 if (read_domains & I915_GEM_DOMAIN_GTT) {
1150 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
Eric Anholt02354392008-11-26 13:58:13 -08001151
Eric Anholta09ba7f2009-08-29 12:49:51 -07001152 /* Update the LRU on the fence for the CPU access that's
1153 * about to occur.
1154 */
1155 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001156 struct drm_i915_fence_reg *reg =
1157 &dev_priv->fence_regs[obj_priv->fence_reg];
1158 list_move_tail(&reg->lru_list,
Eric Anholta09ba7f2009-08-29 12:49:51 -07001159 &dev_priv->mm.fence_list);
1160 }
1161
Eric Anholt02354392008-11-26 13:58:13 -08001162 /* Silently promote "you're not bound, there was nothing to do"
1163 * to success, since the client was just asking us to
1164 * make sure everything was done.
1165 */
1166 if (ret == -EINVAL)
1167 ret = 0;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001168 } else {
Eric Anholte47c68e2008-11-14 13:35:19 -08001169 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001170 }
1171
Chris Wilson7d1c4802010-08-07 21:45:03 +01001172 /* Maintain LRU order of "inactive" objects */
1173 if (ret == 0 && i915_gem_object_is_inactive(obj_priv))
Chris Wilson69dc4982010-10-19 10:36:51 +01001174 list_move_tail(&obj_priv->mm_list, &dev_priv->mm.inactive_list);
Chris Wilson7d1c4802010-08-07 21:45:03 +01001175
Eric Anholt673a3942008-07-30 12:06:12 -07001176 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001177unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07001178 mutex_unlock(&dev->struct_mutex);
1179 return ret;
1180}
1181
1182/**
1183 * Called when user space has done writes to this buffer
1184 */
1185int
1186i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
1187 struct drm_file *file_priv)
1188{
1189 struct drm_i915_gem_sw_finish *args = data;
1190 struct drm_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001191 int ret = 0;
1192
1193 if (!(dev->driver->driver_features & DRIVER_GEM))
1194 return -ENODEV;
1195
Chris Wilson76c1dec2010-09-25 11:22:51 +01001196 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001197 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001198 return ret;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001199
Eric Anholt673a3942008-07-30 12:06:12 -07001200 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1201 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001202 ret = -ENOENT;
1203 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07001204 }
1205
Eric Anholt673a3942008-07-30 12:06:12 -07001206 /* Pinned buffers may be scanout, so flush the cache */
Chris Wilson3d2a8122010-09-29 11:39:53 +01001207 if (to_intel_bo(obj)->pin_count)
Eric Anholte47c68e2008-11-14 13:35:19 -08001208 i915_gem_object_flush_cpu_write_domain(obj);
1209
Eric Anholt673a3942008-07-30 12:06:12 -07001210 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001211unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07001212 mutex_unlock(&dev->struct_mutex);
1213 return ret;
1214}
1215
1216/**
1217 * Maps the contents of an object, returning the address it is mapped
1218 * into.
1219 *
1220 * While the mapping holds a reference on the contents of the object, it doesn't
1221 * imply a ref on the object itself.
1222 */
1223int
1224i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
1225 struct drm_file *file_priv)
1226{
Chris Wilsonda761a62010-10-27 17:37:08 +01001227 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07001228 struct drm_i915_gem_mmap *args = data;
1229 struct drm_gem_object *obj;
1230 loff_t offset;
1231 unsigned long addr;
1232
1233 if (!(dev->driver->driver_features & DRIVER_GEM))
1234 return -ENODEV;
1235
1236 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1237 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001238 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001239
Chris Wilsonda761a62010-10-27 17:37:08 +01001240 if (obj->size > dev_priv->mm.gtt_mappable_end) {
1241 drm_gem_object_unreference_unlocked(obj);
1242 return -E2BIG;
1243 }
1244
Eric Anholt673a3942008-07-30 12:06:12 -07001245 offset = args->offset;
1246
1247 down_write(&current->mm->mmap_sem);
1248 addr = do_mmap(obj->filp, 0, args->size,
1249 PROT_READ | PROT_WRITE, MAP_SHARED,
1250 args->offset);
1251 up_write(&current->mm->mmap_sem);
Luca Barbieribc9025b2010-02-09 05:49:12 +00001252 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001253 if (IS_ERR((void *)addr))
1254 return addr;
1255
1256 args->addr_ptr = (uint64_t) addr;
1257
1258 return 0;
1259}
1260
Jesse Barnesde151cf2008-11-12 10:03:55 -08001261/**
1262 * i915_gem_fault - fault a page into the GTT
1263 * vma: VMA in question
1264 * vmf: fault info
1265 *
1266 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1267 * from userspace. The fault handler takes care of binding the object to
1268 * the GTT (if needed), allocating and programming a fence register (again,
1269 * only if needed based on whether the old reg is still valid or the object
1270 * is tiled) and inserting a new PTE into the faulting process.
1271 *
1272 * Note that the faulting process may involve evicting existing objects
1273 * from the GTT and/or fence registers to make room. So performance may
1274 * suffer if the GTT working set is large or there are few fence registers
1275 * left.
1276 */
1277int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1278{
1279 struct drm_gem_object *obj = vma->vm_private_data;
1280 struct drm_device *dev = obj->dev;
Chris Wilson7d1c4802010-08-07 21:45:03 +01001281 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001282 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001283 pgoff_t page_offset;
1284 unsigned long pfn;
1285 int ret = 0;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001286 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001287
1288 /* We don't use vmf->pgoff since that has the fake offset */
1289 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1290 PAGE_SHIFT;
1291
1292 /* Now bind it into the GTT if needed */
1293 mutex_lock(&dev->struct_mutex);
Daniel Vetterfb7d5162010-10-01 22:05:20 +02001294 BUG_ON(obj_priv->pin_count && !obj_priv->pin_mappable);
Chris Wilsona00b10c2010-09-24 21:15:47 +01001295
1296 if (obj_priv->gtt_space) {
1297 if (!obj_priv->mappable ||
1298 (obj_priv->tiling_mode && !obj_priv->fenceable)) {
1299 ret = i915_gem_object_unbind(obj);
1300 if (ret)
1301 goto unlock;
1302 }
1303 }
Daniel Vetter16e809a2010-09-16 19:37:04 +02001304
Jesse Barnesde151cf2008-11-12 10:03:55 -08001305 if (!obj_priv->gtt_space) {
Chris Wilsona00b10c2010-09-24 21:15:47 +01001306 ret = i915_gem_object_bind_to_gtt(obj, 0,
1307 true, obj_priv->tiling_mode);
Chris Wilsonc7150892009-09-23 00:43:56 +01001308 if (ret)
1309 goto unlock;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001310 }
1311
Chris Wilson4a684a42010-10-28 14:44:08 +01001312 ret = i915_gem_object_set_to_gtt_domain(obj, write);
1313 if (ret)
1314 goto unlock;
1315
Daniel Vetterfb7d5162010-10-01 22:05:20 +02001316 if (!obj_priv->fault_mappable) {
1317 obj_priv->fault_mappable = true;
Chris Wilsona00b10c2010-09-24 21:15:47 +01001318 i915_gem_info_update_mappable(dev_priv, obj_priv, true);
Daniel Vetterfb7d5162010-10-01 22:05:20 +02001319 }
1320
Jesse Barnesde151cf2008-11-12 10:03:55 -08001321 /* Need a new fence register? */
Eric Anholta09ba7f2009-08-29 12:49:51 -07001322 if (obj_priv->tiling_mode != I915_TILING_NONE) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01001323 ret = i915_gem_object_get_fence_reg(obj, true);
Chris Wilsonc7150892009-09-23 00:43:56 +01001324 if (ret)
1325 goto unlock;
Eric Anholtd9ddcb92009-01-27 10:33:49 -08001326 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001327
Chris Wilson7d1c4802010-08-07 21:45:03 +01001328 if (i915_gem_object_is_inactive(obj_priv))
Chris Wilson69dc4982010-10-19 10:36:51 +01001329 list_move_tail(&obj_priv->mm_list, &dev_priv->mm.inactive_list);
Chris Wilson7d1c4802010-08-07 21:45:03 +01001330
Jesse Barnesde151cf2008-11-12 10:03:55 -08001331 pfn = ((dev->agp->base + obj_priv->gtt_offset) >> PAGE_SHIFT) +
1332 page_offset;
1333
1334 /* Finally, remap it using the new GTT offset */
1335 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
Chris Wilsonc7150892009-09-23 00:43:56 +01001336unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001337 mutex_unlock(&dev->struct_mutex);
1338
1339 switch (ret) {
Chris Wilsonc7150892009-09-23 00:43:56 +01001340 case 0:
1341 case -ERESTARTSYS:
1342 return VM_FAULT_NOPAGE;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001343 case -ENOMEM:
1344 case -EAGAIN:
1345 return VM_FAULT_OOM;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001346 default:
Chris Wilsonc7150892009-09-23 00:43:56 +01001347 return VM_FAULT_SIGBUS;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001348 }
1349}
1350
1351/**
1352 * i915_gem_create_mmap_offset - create a fake mmap offset for an object
1353 * @obj: obj in question
1354 *
1355 * GEM memory mapping works by handing back to userspace a fake mmap offset
1356 * it can use in a subsequent mmap(2) call. The DRM core code then looks
1357 * up the object based on the offset and sets up the various memory mapping
1358 * structures.
1359 *
1360 * This routine allocates and attaches a fake offset for @obj.
1361 */
1362static int
1363i915_gem_create_mmap_offset(struct drm_gem_object *obj)
1364{
1365 struct drm_device *dev = obj->dev;
1366 struct drm_gem_mm *mm = dev->mm_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001367 struct drm_map_list *list;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +11001368 struct drm_local_map *map;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001369 int ret = 0;
1370
1371 /* Set the object up for mmap'ing */
1372 list = &obj->map_list;
Eric Anholt9a298b22009-03-24 12:23:04 -07001373 list->map = kzalloc(sizeof(struct drm_map_list), GFP_KERNEL);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001374 if (!list->map)
1375 return -ENOMEM;
1376
1377 map = list->map;
1378 map->type = _DRM_GEM;
1379 map->size = obj->size;
1380 map->handle = obj;
1381
1382 /* Get a DRM GEM mmap offset allocated... */
1383 list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
1384 obj->size / PAGE_SIZE, 0, 0);
1385 if (!list->file_offset_node) {
1386 DRM_ERROR("failed to allocate offset for bo %d\n", obj->name);
Chris Wilson9e0ae5342010-09-21 15:05:24 +01001387 ret = -ENOSPC;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001388 goto out_free_list;
1389 }
1390
1391 list->file_offset_node = drm_mm_get_block(list->file_offset_node,
1392 obj->size / PAGE_SIZE, 0);
1393 if (!list->file_offset_node) {
1394 ret = -ENOMEM;
1395 goto out_free_list;
1396 }
1397
1398 list->hash.key = list->file_offset_node->start;
Chris Wilson9e0ae5342010-09-21 15:05:24 +01001399 ret = drm_ht_insert_item(&mm->offset_hash, &list->hash);
1400 if (ret) {
Jesse Barnesde151cf2008-11-12 10:03:55 -08001401 DRM_ERROR("failed to add to map hash\n");
1402 goto out_free_mm;
1403 }
1404
Jesse Barnesde151cf2008-11-12 10:03:55 -08001405 return 0;
1406
1407out_free_mm:
1408 drm_mm_put_block(list->file_offset_node);
1409out_free_list:
Eric Anholt9a298b22009-03-24 12:23:04 -07001410 kfree(list->map);
Chris Wilson39a01d12010-10-28 13:03:06 +01001411 list->map = NULL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001412
1413 return ret;
1414}
1415
Chris Wilson901782b2009-07-10 08:18:50 +01001416/**
1417 * i915_gem_release_mmap - remove physical page mappings
1418 * @obj: obj in question
1419 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001420 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01001421 * relinquish ownership of the pages back to the system.
1422 *
1423 * It is vital that we remove the page mapping if we have mapped a tiled
1424 * object through the GTT and then lose the fence register due to
1425 * resource pressure. Similarly if the object has been moved out of the
1426 * aperture, than pages mapped into userspace must be revoked. Removing the
1427 * mapping will then trigger a page fault on the next user access, allowing
1428 * fixup by i915_gem_fault().
1429 */
Eric Anholtd05ca302009-07-10 13:02:26 -07001430void
Chris Wilson901782b2009-07-10 08:18:50 +01001431i915_gem_release_mmap(struct drm_gem_object *obj)
1432{
1433 struct drm_device *dev = obj->dev;
Daniel Vetterfb7d5162010-10-01 22:05:20 +02001434 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001435 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson901782b2009-07-10 08:18:50 +01001436
Chris Wilson39a01d12010-10-28 13:03:06 +01001437 if (unlikely(obj->map_list.map && dev->dev_mapping))
Chris Wilson901782b2009-07-10 08:18:50 +01001438 unmap_mapping_range(dev->dev_mapping,
Chris Wilson39a01d12010-10-28 13:03:06 +01001439 (loff_t)obj->map_list.hash.key<<PAGE_SHIFT,
1440 obj->size, 1);
Daniel Vetterfb7d5162010-10-01 22:05:20 +02001441
1442 if (obj_priv->fault_mappable) {
1443 obj_priv->fault_mappable = false;
Chris Wilsona00b10c2010-09-24 21:15:47 +01001444 i915_gem_info_update_mappable(dev_priv, obj_priv, false);
Daniel Vetterfb7d5162010-10-01 22:05:20 +02001445 }
Chris Wilson901782b2009-07-10 08:18:50 +01001446}
1447
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001448static void
1449i915_gem_free_mmap_offset(struct drm_gem_object *obj)
1450{
1451 struct drm_device *dev = obj->dev;
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001452 struct drm_gem_mm *mm = dev->mm_private;
Chris Wilson39a01d12010-10-28 13:03:06 +01001453 struct drm_map_list *list = &obj->map_list;
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001454
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001455 drm_ht_remove_item(&mm->offset_hash, &list->hash);
Chris Wilson39a01d12010-10-28 13:03:06 +01001456 drm_mm_put_block(list->file_offset_node);
1457 kfree(list->map);
1458 list->map = NULL;
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001459}
1460
Jesse Barnesde151cf2008-11-12 10:03:55 -08001461/**
1462 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1463 * @obj: object to check
1464 *
1465 * Return the required GTT alignment for an object, taking into account
1466 * potential fence register mapping if needed.
1467 */
1468static uint32_t
Chris Wilsona00b10c2010-09-24 21:15:47 +01001469i915_gem_get_gtt_alignment(struct drm_i915_gem_object *obj_priv)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001470{
Chris Wilsona00b10c2010-09-24 21:15:47 +01001471 struct drm_device *dev = obj_priv->base.dev;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001472
1473 /*
1474 * Minimum alignment is 4k (GTT page size), but might be greater
1475 * if a fence register is needed for the object.
1476 */
Chris Wilsona00b10c2010-09-24 21:15:47 +01001477 if (INTEL_INFO(dev)->gen >= 4 ||
1478 obj_priv->tiling_mode == I915_TILING_NONE)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001479 return 4096;
1480
1481 /*
1482 * Previous chips need to be aligned to the size of the smallest
1483 * fence register that can contain the object.
1484 */
Chris Wilsona00b10c2010-09-24 21:15:47 +01001485 return i915_gem_get_gtt_size(obj_priv);
1486}
1487
1488static uint32_t
1489i915_gem_get_gtt_size(struct drm_i915_gem_object *obj_priv)
1490{
1491 struct drm_device *dev = obj_priv->base.dev;
1492 uint32_t size;
1493
1494 /*
1495 * Minimum alignment is 4k (GTT page size), but might be greater
1496 * if a fence register is needed for the object.
1497 */
1498 if (INTEL_INFO(dev)->gen >= 4)
1499 return obj_priv->base.size;
1500
1501 /*
1502 * Previous chips need to be aligned to the size of the smallest
1503 * fence register that can contain the object.
1504 */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001505 if (INTEL_INFO(dev)->gen == 3)
Chris Wilsona00b10c2010-09-24 21:15:47 +01001506 size = 1024*1024;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001507 else
Chris Wilsona00b10c2010-09-24 21:15:47 +01001508 size = 512*1024;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001509
Chris Wilsona00b10c2010-09-24 21:15:47 +01001510 while (size < obj_priv->base.size)
1511 size <<= 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001512
Chris Wilsona00b10c2010-09-24 21:15:47 +01001513 return size;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001514}
1515
1516/**
1517 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1518 * @dev: DRM device
1519 * @data: GTT mapping ioctl data
1520 * @file_priv: GEM object info
1521 *
1522 * Simply returns the fake offset to userspace so it can mmap it.
1523 * The mmap call will end up in drm_gem_mmap(), which will set things
1524 * up so we can get faults in the handler above.
1525 *
1526 * The fault handler will take care of binding the object into the GTT
1527 * (since it may have been evicted to make room for something), allocating
1528 * a fence register, and mapping the appropriate aperture address into
1529 * userspace.
1530 */
1531int
1532i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
1533 struct drm_file *file_priv)
1534{
Chris Wilsonda761a62010-10-27 17:37:08 +01001535 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001536 struct drm_i915_gem_mmap_gtt *args = data;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001537 struct drm_gem_object *obj;
1538 struct drm_i915_gem_object *obj_priv;
1539 int ret;
1540
1541 if (!(dev->driver->driver_features & DRIVER_GEM))
1542 return -ENODEV;
1543
Chris Wilson76c1dec2010-09-25 11:22:51 +01001544 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001545 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001546 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001547
Jesse Barnesde151cf2008-11-12 10:03:55 -08001548 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001549 if (obj == NULL) {
1550 ret = -ENOENT;
1551 goto unlock;
1552 }
Daniel Vetter23010e42010-03-08 13:35:02 +01001553 obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001554
Chris Wilsonda761a62010-10-27 17:37:08 +01001555 if (obj->size > dev_priv->mm.gtt_mappable_end) {
1556 ret = -E2BIG;
1557 goto unlock;
1558 }
1559
Chris Wilsonab182822009-09-22 18:46:17 +01001560 if (obj_priv->madv != I915_MADV_WILLNEED) {
1561 DRM_ERROR("Attempting to mmap a purgeable buffer\n");
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001562 ret = -EINVAL;
1563 goto out;
Chris Wilsonab182822009-09-22 18:46:17 +01001564 }
1565
Chris Wilson39a01d12010-10-28 13:03:06 +01001566 if (!obj->map_list.map) {
Jesse Barnesde151cf2008-11-12 10:03:55 -08001567 ret = i915_gem_create_mmap_offset(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001568 if (ret)
1569 goto out;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001570 }
1571
Chris Wilson39a01d12010-10-28 13:03:06 +01001572 args->offset = (u64)obj->map_list.hash.key << PAGE_SHIFT;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001573
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001574out:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001575 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001576unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001577 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001578 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001579}
1580
Chris Wilsone5281cc2010-10-28 13:45:36 +01001581static int
1582i915_gem_object_get_pages_gtt(struct drm_gem_object *obj,
1583 gfp_t gfpmask)
1584{
1585 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
1586 int page_count, i;
1587 struct address_space *mapping;
1588 struct inode *inode;
1589 struct page *page;
1590
1591 /* Get the list of pages out of our struct file. They'll be pinned
1592 * at this point until we release them.
1593 */
1594 page_count = obj->size / PAGE_SIZE;
1595 BUG_ON(obj_priv->pages != NULL);
1596 obj_priv->pages = drm_malloc_ab(page_count, sizeof(struct page *));
1597 if (obj_priv->pages == NULL)
1598 return -ENOMEM;
1599
1600 inode = obj->filp->f_path.dentry->d_inode;
1601 mapping = inode->i_mapping;
1602 for (i = 0; i < page_count; i++) {
1603 page = read_cache_page_gfp(mapping, i,
1604 GFP_HIGHUSER |
1605 __GFP_COLD |
1606 __GFP_RECLAIMABLE |
1607 gfpmask);
1608 if (IS_ERR(page))
1609 goto err_pages;
1610
1611 obj_priv->pages[i] = page;
1612 }
1613
1614 if (obj_priv->tiling_mode != I915_TILING_NONE)
1615 i915_gem_object_do_bit_17_swizzle(obj);
1616
1617 return 0;
1618
1619err_pages:
1620 while (i--)
1621 page_cache_release(obj_priv->pages[i]);
1622
1623 drm_free_large(obj_priv->pages);
1624 obj_priv->pages = NULL;
1625 return PTR_ERR(page);
1626}
1627
Chris Wilson5cdf5882010-09-27 15:51:07 +01001628static void
Chris Wilsone5281cc2010-10-28 13:45:36 +01001629i915_gem_object_put_pages_gtt(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07001630{
Daniel Vetter23010e42010-03-08 13:35:02 +01001631 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001632 int page_count = obj->size / PAGE_SIZE;
1633 int i;
1634
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001635 BUG_ON(obj_priv->madv == __I915_MADV_PURGED);
Eric Anholt856fa192009-03-19 14:10:50 -07001636
Eric Anholt280b7132009-03-12 16:56:27 -07001637 if (obj_priv->tiling_mode != I915_TILING_NONE)
1638 i915_gem_object_save_bit_17_swizzle(obj);
1639
Chris Wilson3ef94da2009-09-14 16:50:29 +01001640 if (obj_priv->madv == I915_MADV_DONTNEED)
Chris Wilson13a05fd2009-09-20 23:03:19 +01001641 obj_priv->dirty = 0;
Chris Wilson3ef94da2009-09-14 16:50:29 +01001642
1643 for (i = 0; i < page_count; i++) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01001644 if (obj_priv->dirty)
1645 set_page_dirty(obj_priv->pages[i]);
1646
1647 if (obj_priv->madv == I915_MADV_WILLNEED)
Eric Anholt856fa192009-03-19 14:10:50 -07001648 mark_page_accessed(obj_priv->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001649
1650 page_cache_release(obj_priv->pages[i]);
1651 }
Eric Anholt673a3942008-07-30 12:06:12 -07001652 obj_priv->dirty = 0;
1653
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07001654 drm_free_large(obj_priv->pages);
Eric Anholt856fa192009-03-19 14:10:50 -07001655 obj_priv->pages = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001656}
1657
Chris Wilsona56ba562010-09-28 10:07:56 +01001658static uint32_t
1659i915_gem_next_request_seqno(struct drm_device *dev,
1660 struct intel_ring_buffer *ring)
1661{
1662 drm_i915_private_t *dev_priv = dev->dev_private;
1663
1664 ring->outstanding_lazy_request = true;
1665 return dev_priv->next_seqno;
1666}
1667
Eric Anholt673a3942008-07-30 12:06:12 -07001668static void
Daniel Vetter617dbe22010-02-11 22:16:02 +01001669i915_gem_object_move_to_active(struct drm_gem_object *obj,
Zou Nan hai852835f2010-05-21 09:08:56 +08001670 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001671{
1672 struct drm_device *dev = obj->dev;
Chris Wilson69dc4982010-10-19 10:36:51 +01001673 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001674 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsona56ba562010-09-28 10:07:56 +01001675 uint32_t seqno = i915_gem_next_request_seqno(dev, ring);
Daniel Vetter617dbe22010-02-11 22:16:02 +01001676
Zou Nan hai852835f2010-05-21 09:08:56 +08001677 BUG_ON(ring == NULL);
1678 obj_priv->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001679
1680 /* Add a reference if we're newly entering the active list. */
1681 if (!obj_priv->active) {
1682 drm_gem_object_reference(obj);
1683 obj_priv->active = 1;
1684 }
Daniel Vettere35a41d2010-02-11 22:13:59 +01001685
Eric Anholt673a3942008-07-30 12:06:12 -07001686 /* Move from whatever list we were on to the tail of execution. */
Chris Wilson69dc4982010-10-19 10:36:51 +01001687 list_move_tail(&obj_priv->mm_list, &dev_priv->mm.active_list);
1688 list_move_tail(&obj_priv->ring_list, &ring->active_list);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001689 obj_priv->last_rendering_seqno = seqno;
Eric Anholt673a3942008-07-30 12:06:12 -07001690}
1691
Eric Anholtce44b0e2008-11-06 16:00:31 -08001692static void
1693i915_gem_object_move_to_flushing(struct drm_gem_object *obj)
1694{
1695 struct drm_device *dev = obj->dev;
1696 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001697 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001698
1699 BUG_ON(!obj_priv->active);
Chris Wilson69dc4982010-10-19 10:36:51 +01001700 list_move_tail(&obj_priv->mm_list, &dev_priv->mm.flushing_list);
1701 list_del_init(&obj_priv->ring_list);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001702 obj_priv->last_rendering_seqno = 0;
1703}
Eric Anholt673a3942008-07-30 12:06:12 -07001704
Chris Wilson963b4832009-09-20 23:03:54 +01001705/* Immediately discard the backing storage */
1706static void
1707i915_gem_object_truncate(struct drm_gem_object *obj)
1708{
Daniel Vetter23010e42010-03-08 13:35:02 +01001709 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001710 struct inode *inode;
Chris Wilson963b4832009-09-20 23:03:54 +01001711
Chris Wilsonae9fed62010-08-07 11:01:30 +01001712 /* Our goal here is to return as much of the memory as
1713 * is possible back to the system as we are called from OOM.
1714 * To do this we must instruct the shmfs to drop all of its
1715 * backing pages, *now*. Here we mirror the actions taken
1716 * when by shmem_delete_inode() to release the backing store.
1717 */
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001718 inode = obj->filp->f_path.dentry->d_inode;
Chris Wilsonae9fed62010-08-07 11:01:30 +01001719 truncate_inode_pages(inode->i_mapping, 0);
1720 if (inode->i_op->truncate_range)
1721 inode->i_op->truncate_range(inode, 0, (loff_t)-1);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001722
1723 obj_priv->madv = __I915_MADV_PURGED;
Chris Wilson963b4832009-09-20 23:03:54 +01001724}
1725
1726static inline int
1727i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj_priv)
1728{
1729 return obj_priv->madv == I915_MADV_DONTNEED;
1730}
1731
Eric Anholt673a3942008-07-30 12:06:12 -07001732static void
1733i915_gem_object_move_to_inactive(struct drm_gem_object *obj)
1734{
1735 struct drm_device *dev = obj->dev;
1736 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001737 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001738
Eric Anholt673a3942008-07-30 12:06:12 -07001739 if (obj_priv->pin_count != 0)
Chris Wilson69dc4982010-10-19 10:36:51 +01001740 list_move_tail(&obj_priv->mm_list, &dev_priv->mm.pinned_list);
Eric Anholt673a3942008-07-30 12:06:12 -07001741 else
Chris Wilson69dc4982010-10-19 10:36:51 +01001742 list_move_tail(&obj_priv->mm_list, &dev_priv->mm.inactive_list);
1743 list_del_init(&obj_priv->ring_list);
Eric Anholt673a3942008-07-30 12:06:12 -07001744
Daniel Vetter99fcb762010-02-07 16:20:18 +01001745 BUG_ON(!list_empty(&obj_priv->gpu_write_list));
1746
Eric Anholtce44b0e2008-11-06 16:00:31 -08001747 obj_priv->last_rendering_seqno = 0;
Zou Nan hai852835f2010-05-21 09:08:56 +08001748 obj_priv->ring = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001749 if (obj_priv->active) {
1750 obj_priv->active = 0;
1751 drm_gem_object_unreference(obj);
1752 }
Chris Wilson23bc5982010-09-29 16:10:57 +01001753 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07001754}
1755
Daniel Vetter63560392010-02-19 11:51:59 +01001756static void
1757i915_gem_process_flushing_list(struct drm_device *dev,
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001758 uint32_t flush_domains,
Zou Nan hai852835f2010-05-21 09:08:56 +08001759 struct intel_ring_buffer *ring)
Daniel Vetter63560392010-02-19 11:51:59 +01001760{
1761 drm_i915_private_t *dev_priv = dev->dev_private;
1762 struct drm_i915_gem_object *obj_priv, *next;
1763
1764 list_for_each_entry_safe(obj_priv, next,
Chris Wilson64193402010-10-24 12:38:05 +01001765 &ring->gpu_write_list,
Daniel Vetter63560392010-02-19 11:51:59 +01001766 gpu_write_list) {
Daniel Vettera8089e82010-04-09 19:05:09 +00001767 struct drm_gem_object *obj = &obj_priv->base;
Daniel Vetter63560392010-02-19 11:51:59 +01001768
Chris Wilson64193402010-10-24 12:38:05 +01001769 if (obj->write_domain & flush_domains) {
Daniel Vetter63560392010-02-19 11:51:59 +01001770 uint32_t old_write_domain = obj->write_domain;
1771
1772 obj->write_domain = 0;
1773 list_del_init(&obj_priv->gpu_write_list);
Daniel Vetter617dbe22010-02-11 22:16:02 +01001774 i915_gem_object_move_to_active(obj, ring);
Daniel Vetter63560392010-02-19 11:51:59 +01001775
1776 /* update the fence lru list */
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001777 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
1778 struct drm_i915_fence_reg *reg =
1779 &dev_priv->fence_regs[obj_priv->fence_reg];
1780 list_move_tail(&reg->lru_list,
Daniel Vetter63560392010-02-19 11:51:59 +01001781 &dev_priv->mm.fence_list);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001782 }
Daniel Vetter63560392010-02-19 11:51:59 +01001783
1784 trace_i915_gem_object_change_domain(obj,
1785 obj->read_domains,
1786 old_write_domain);
1787 }
1788 }
1789}
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001790
Chris Wilson3cce4692010-10-27 16:11:02 +01001791int
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001792i915_add_request(struct drm_device *dev,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001793 struct drm_file *file,
Chris Wilson8dc5d142010-08-12 12:36:12 +01001794 struct drm_i915_gem_request *request,
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001795 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001796{
1797 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001798 struct drm_i915_file_private *file_priv = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001799 uint32_t seqno;
1800 int was_empty;
Chris Wilson3cce4692010-10-27 16:11:02 +01001801 int ret;
1802
1803 BUG_ON(request == NULL);
Eric Anholt673a3942008-07-30 12:06:12 -07001804
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001805 if (file != NULL)
1806 file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00001807
Chris Wilson3cce4692010-10-27 16:11:02 +01001808 ret = ring->add_request(ring, &seqno);
1809 if (ret)
1810 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001811
Chris Wilsona56ba562010-09-28 10:07:56 +01001812 ring->outstanding_lazy_request = false;
Eric Anholt673a3942008-07-30 12:06:12 -07001813
1814 request->seqno = seqno;
Zou Nan hai852835f2010-05-21 09:08:56 +08001815 request->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001816 request->emitted_jiffies = jiffies;
Zou Nan hai852835f2010-05-21 09:08:56 +08001817 was_empty = list_empty(&ring->request_list);
1818 list_add_tail(&request->list, &ring->request_list);
1819
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001820 if (file_priv) {
Chris Wilson1c255952010-09-26 11:03:27 +01001821 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001822 request->file_priv = file_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00001823 list_add_tail(&request->client_list,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001824 &file_priv->mm.request_list);
Chris Wilson1c255952010-09-26 11:03:27 +01001825 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00001826 }
Eric Anholt673a3942008-07-30 12:06:12 -07001827
Ben Gamarif65d9422009-09-14 17:48:44 -04001828 if (!dev_priv->mm.suspended) {
Chris Wilsonb3b079d2010-09-13 23:44:34 +01001829 mod_timer(&dev_priv->hangcheck_timer,
1830 jiffies + msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
Ben Gamarif65d9422009-09-14 17:48:44 -04001831 if (was_empty)
Chris Wilsonb3b079d2010-09-13 23:44:34 +01001832 queue_delayed_work(dev_priv->wq,
1833 &dev_priv->mm.retire_work, HZ);
Ben Gamarif65d9422009-09-14 17:48:44 -04001834 }
Chris Wilson3cce4692010-10-27 16:11:02 +01001835 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001836}
1837
1838/**
1839 * Command execution barrier
1840 *
1841 * Ensures that all commands in the ring are finished
1842 * before signalling the CPU
1843 */
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001844static void
Zou Nan hai852835f2010-05-21 09:08:56 +08001845i915_retire_commands(struct drm_device *dev, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001846{
Eric Anholt673a3942008-07-30 12:06:12 -07001847 uint32_t flush_domains = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001848
1849 /* The sampler always gets flushed on i965 (sigh) */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001850 if (INTEL_INFO(dev)->gen >= 4)
Eric Anholt673a3942008-07-30 12:06:12 -07001851 flush_domains |= I915_GEM_DOMAIN_SAMPLER;
Zou Nan hai852835f2010-05-21 09:08:56 +08001852
Chris Wilson78501ea2010-10-27 12:18:21 +01001853 ring->flush(ring, I915_GEM_DOMAIN_COMMAND, flush_domains);
Eric Anholt673a3942008-07-30 12:06:12 -07001854}
1855
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001856static inline void
1857i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
Eric Anholt673a3942008-07-30 12:06:12 -07001858{
Chris Wilson1c255952010-09-26 11:03:27 +01001859 struct drm_i915_file_private *file_priv = request->file_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07001860
Chris Wilson1c255952010-09-26 11:03:27 +01001861 if (!file_priv)
1862 return;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001863
Chris Wilson1c255952010-09-26 11:03:27 +01001864 spin_lock(&file_priv->mm.lock);
1865 list_del(&request->client_list);
1866 request->file_priv = NULL;
1867 spin_unlock(&file_priv->mm.lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001868}
1869
Chris Wilsondfaae392010-09-22 10:31:52 +01001870static void i915_gem_reset_ring_lists(struct drm_i915_private *dev_priv,
1871 struct intel_ring_buffer *ring)
Chris Wilson9375e442010-09-19 12:21:28 +01001872{
Chris Wilsondfaae392010-09-22 10:31:52 +01001873 while (!list_empty(&ring->request_list)) {
1874 struct drm_i915_gem_request *request;
Chris Wilson9375e442010-09-19 12:21:28 +01001875
Chris Wilsondfaae392010-09-22 10:31:52 +01001876 request = list_first_entry(&ring->request_list,
1877 struct drm_i915_gem_request,
1878 list);
1879
1880 list_del(&request->list);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001881 i915_gem_request_remove_from_client(request);
Chris Wilsondfaae392010-09-22 10:31:52 +01001882 kfree(request);
1883 }
1884
1885 while (!list_empty(&ring->active_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001886 struct drm_i915_gem_object *obj_priv;
1887
Chris Wilsondfaae392010-09-22 10:31:52 +01001888 obj_priv = list_first_entry(&ring->active_list,
Eric Anholt673a3942008-07-30 12:06:12 -07001889 struct drm_i915_gem_object,
Chris Wilson69dc4982010-10-19 10:36:51 +01001890 ring_list);
Eric Anholt673a3942008-07-30 12:06:12 -07001891
Chris Wilsondfaae392010-09-22 10:31:52 +01001892 obj_priv->base.write_domain = 0;
1893 list_del_init(&obj_priv->gpu_write_list);
1894 i915_gem_object_move_to_inactive(&obj_priv->base);
Eric Anholt673a3942008-07-30 12:06:12 -07001895 }
Eric Anholt673a3942008-07-30 12:06:12 -07001896}
1897
Chris Wilson069efc12010-09-30 16:53:18 +01001898void i915_gem_reset(struct drm_device *dev)
Eric Anholt673a3942008-07-30 12:06:12 -07001899{
Chris Wilsondfaae392010-09-22 10:31:52 +01001900 struct drm_i915_private *dev_priv = dev->dev_private;
1901 struct drm_i915_gem_object *obj_priv;
Chris Wilson069efc12010-09-30 16:53:18 +01001902 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07001903
Chris Wilsondfaae392010-09-22 10:31:52 +01001904 i915_gem_reset_ring_lists(dev_priv, &dev_priv->render_ring);
Chris Wilson87acb0a2010-10-19 10:13:00 +01001905 i915_gem_reset_ring_lists(dev_priv, &dev_priv->bsd_ring);
Chris Wilson549f7362010-10-19 11:19:32 +01001906 i915_gem_reset_ring_lists(dev_priv, &dev_priv->blt_ring);
Chris Wilsondfaae392010-09-22 10:31:52 +01001907
1908 /* Remove anything from the flushing lists. The GPU cache is likely
1909 * to be lost on reset along with the data, so simply move the
1910 * lost bo to the inactive list.
1911 */
1912 while (!list_empty(&dev_priv->mm.flushing_list)) {
Chris Wilson9375e442010-09-19 12:21:28 +01001913 obj_priv = list_first_entry(&dev_priv->mm.flushing_list,
1914 struct drm_i915_gem_object,
Chris Wilson69dc4982010-10-19 10:36:51 +01001915 mm_list);
Chris Wilson9375e442010-09-19 12:21:28 +01001916
1917 obj_priv->base.write_domain = 0;
Chris Wilsondfaae392010-09-22 10:31:52 +01001918 list_del_init(&obj_priv->gpu_write_list);
Chris Wilson9375e442010-09-19 12:21:28 +01001919 i915_gem_object_move_to_inactive(&obj_priv->base);
1920 }
Chris Wilson9375e442010-09-19 12:21:28 +01001921
Chris Wilsondfaae392010-09-22 10:31:52 +01001922 /* Move everything out of the GPU domains to ensure we do any
1923 * necessary invalidation upon reuse.
1924 */
Chris Wilson77f01232010-09-19 12:31:36 +01001925 list_for_each_entry(obj_priv,
1926 &dev_priv->mm.inactive_list,
Chris Wilson69dc4982010-10-19 10:36:51 +01001927 mm_list)
Chris Wilson77f01232010-09-19 12:31:36 +01001928 {
1929 obj_priv->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
1930 }
Chris Wilson069efc12010-09-30 16:53:18 +01001931
1932 /* The fence registers are invalidated so clear them out */
1933 for (i = 0; i < 16; i++) {
1934 struct drm_i915_fence_reg *reg;
1935
1936 reg = &dev_priv->fence_regs[i];
1937 if (!reg->obj)
1938 continue;
1939
1940 i915_gem_clear_fence_reg(reg->obj);
1941 }
Eric Anholt673a3942008-07-30 12:06:12 -07001942}
1943
1944/**
1945 * This function clears the request list as sequence numbers are passed.
1946 */
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001947static void
1948i915_gem_retire_requests_ring(struct drm_device *dev,
1949 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001950{
1951 drm_i915_private_t *dev_priv = dev->dev_private;
1952 uint32_t seqno;
1953
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001954 if (!ring->status_page.page_addr ||
1955 list_empty(&ring->request_list))
Karsten Wiese6c0594a2009-02-23 15:07:57 +01001956 return;
1957
Chris Wilson23bc5982010-09-29 16:10:57 +01001958 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07001959
Chris Wilson78501ea2010-10-27 12:18:21 +01001960 seqno = ring->get_seqno(ring);
Zou Nan hai852835f2010-05-21 09:08:56 +08001961 while (!list_empty(&ring->request_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001962 struct drm_i915_gem_request *request;
Eric Anholt673a3942008-07-30 12:06:12 -07001963
Zou Nan hai852835f2010-05-21 09:08:56 +08001964 request = list_first_entry(&ring->request_list,
Eric Anholt673a3942008-07-30 12:06:12 -07001965 struct drm_i915_gem_request,
1966 list);
Eric Anholt673a3942008-07-30 12:06:12 -07001967
Chris Wilsondfaae392010-09-22 10:31:52 +01001968 if (!i915_seqno_passed(seqno, request->seqno))
Eric Anholt673a3942008-07-30 12:06:12 -07001969 break;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001970
1971 trace_i915_gem_request_retire(dev, request->seqno);
1972
1973 list_del(&request->list);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001974 i915_gem_request_remove_from_client(request);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001975 kfree(request);
1976 }
1977
1978 /* Move any buffers on the active list that are no longer referenced
1979 * by the ringbuffer to the flushing/inactive lists as appropriate.
1980 */
1981 while (!list_empty(&ring->active_list)) {
1982 struct drm_gem_object *obj;
1983 struct drm_i915_gem_object *obj_priv;
1984
1985 obj_priv = list_first_entry(&ring->active_list,
1986 struct drm_i915_gem_object,
Chris Wilson69dc4982010-10-19 10:36:51 +01001987 ring_list);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001988
Chris Wilsondfaae392010-09-22 10:31:52 +01001989 if (!i915_seqno_passed(seqno, obj_priv->last_rendering_seqno))
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001990 break;
1991
1992 obj = &obj_priv->base;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001993 if (obj->write_domain != 0)
1994 i915_gem_object_move_to_flushing(obj);
1995 else
1996 i915_gem_object_move_to_inactive(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001997 }
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001998
1999 if (unlikely (dev_priv->trace_irq_seqno &&
2000 i915_seqno_passed(dev_priv->trace_irq_seqno, seqno))) {
Chris Wilson78501ea2010-10-27 12:18:21 +01002001 ring->user_irq_put(ring);
Chris Wilson9d34e5d2009-09-24 05:26:06 +01002002 dev_priv->trace_irq_seqno = 0;
2003 }
Chris Wilson23bc5982010-09-29 16:10:57 +01002004
2005 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07002006}
2007
2008void
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002009i915_gem_retire_requests(struct drm_device *dev)
2010{
2011 drm_i915_private_t *dev_priv = dev->dev_private;
2012
Chris Wilsonbe726152010-07-23 23:18:50 +01002013 if (!list_empty(&dev_priv->mm.deferred_free_list)) {
2014 struct drm_i915_gem_object *obj_priv, *tmp;
2015
2016 /* We must be careful that during unbind() we do not
2017 * accidentally infinitely recurse into retire requests.
2018 * Currently:
2019 * retire -> free -> unbind -> wait -> retire_ring
2020 */
2021 list_for_each_entry_safe(obj_priv, tmp,
2022 &dev_priv->mm.deferred_free_list,
Chris Wilson69dc4982010-10-19 10:36:51 +01002023 mm_list)
Chris Wilsonbe726152010-07-23 23:18:50 +01002024 i915_gem_free_object_tail(&obj_priv->base);
2025 }
2026
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002027 i915_gem_retire_requests_ring(dev, &dev_priv->render_ring);
Chris Wilson87acb0a2010-10-19 10:13:00 +01002028 i915_gem_retire_requests_ring(dev, &dev_priv->bsd_ring);
Chris Wilson549f7362010-10-19 11:19:32 +01002029 i915_gem_retire_requests_ring(dev, &dev_priv->blt_ring);
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002030}
2031
Daniel Vetter75ef9da2010-08-21 00:25:16 +02002032static void
Eric Anholt673a3942008-07-30 12:06:12 -07002033i915_gem_retire_work_handler(struct work_struct *work)
2034{
2035 drm_i915_private_t *dev_priv;
2036 struct drm_device *dev;
2037
2038 dev_priv = container_of(work, drm_i915_private_t,
2039 mm.retire_work.work);
2040 dev = dev_priv->dev;
2041
Chris Wilson891b48c2010-09-29 12:26:37 +01002042 /* Come back later if the device is busy... */
2043 if (!mutex_trylock(&dev->struct_mutex)) {
2044 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
2045 return;
2046 }
2047
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002048 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08002049
Keith Packard6dbe2772008-10-14 21:41:13 -07002050 if (!dev_priv->mm.suspended &&
Zou Nan haid1b851f2010-05-21 09:08:57 +08002051 (!list_empty(&dev_priv->render_ring.request_list) ||
Chris Wilson549f7362010-10-19 11:19:32 +01002052 !list_empty(&dev_priv->bsd_ring.request_list) ||
2053 !list_empty(&dev_priv->blt_ring.request_list)))
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07002054 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
Eric Anholt673a3942008-07-30 12:06:12 -07002055 mutex_unlock(&dev->struct_mutex);
2056}
2057
Daniel Vetter5a5a0c62009-09-15 22:57:36 +02002058int
Zou Nan hai852835f2010-05-21 09:08:56 +08002059i915_do_wait_request(struct drm_device *dev, uint32_t seqno,
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002060 bool interruptible, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07002061{
2062 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes802c7eb2009-05-05 16:03:48 -07002063 u32 ier;
Eric Anholt673a3942008-07-30 12:06:12 -07002064 int ret = 0;
2065
2066 BUG_ON(seqno == 0);
2067
Ben Gamariba1234d2009-09-14 17:48:47 -04002068 if (atomic_read(&dev_priv->mm.wedged))
Chris Wilson30dbf0c2010-09-25 10:19:17 +01002069 return -EAGAIN;
Ben Gamariffed1d02009-09-14 17:48:41 -04002070
Chris Wilsona56ba562010-09-28 10:07:56 +01002071 if (ring->outstanding_lazy_request) {
Chris Wilson3cce4692010-10-27 16:11:02 +01002072 struct drm_i915_gem_request *request;
2073
2074 request = kzalloc(sizeof(*request), GFP_KERNEL);
2075 if (request == NULL)
Daniel Vettere35a41d2010-02-11 22:13:59 +01002076 return -ENOMEM;
Chris Wilson3cce4692010-10-27 16:11:02 +01002077
2078 ret = i915_add_request(dev, NULL, request, ring);
2079 if (ret) {
2080 kfree(request);
2081 return ret;
2082 }
2083
2084 seqno = request->seqno;
Daniel Vettere35a41d2010-02-11 22:13:59 +01002085 }
Chris Wilsona56ba562010-09-28 10:07:56 +01002086 BUG_ON(seqno == dev_priv->next_seqno);
Daniel Vettere35a41d2010-02-11 22:13:59 +01002087
Chris Wilson78501ea2010-10-27 12:18:21 +01002088 if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) {
Eric Anholtbad720f2009-10-22 16:11:14 -07002089 if (HAS_PCH_SPLIT(dev))
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002090 ier = I915_READ(DEIER) | I915_READ(GTIER);
2091 else
2092 ier = I915_READ(IER);
Jesse Barnes802c7eb2009-05-05 16:03:48 -07002093 if (!ier) {
2094 DRM_ERROR("something (likely vbetool) disabled "
2095 "interrupts, re-enabling\n");
2096 i915_driver_irq_preinstall(dev);
2097 i915_driver_irq_postinstall(dev);
2098 }
2099
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002100 trace_i915_gem_request_wait_begin(dev, seqno);
2101
Chris Wilsonb2223492010-10-27 15:27:33 +01002102 ring->waiting_seqno = seqno;
Chris Wilson78501ea2010-10-27 12:18:21 +01002103 ring->user_irq_get(ring);
Daniel Vetter48764bf2009-09-15 22:57:32 +02002104 if (interruptible)
Zou Nan hai852835f2010-05-21 09:08:56 +08002105 ret = wait_event_interruptible(ring->irq_queue,
Chris Wilson78501ea2010-10-27 12:18:21 +01002106 i915_seqno_passed(ring->get_seqno(ring), seqno)
Zou Nan hai852835f2010-05-21 09:08:56 +08002107 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02002108 else
Zou Nan hai852835f2010-05-21 09:08:56 +08002109 wait_event(ring->irq_queue,
Chris Wilson78501ea2010-10-27 12:18:21 +01002110 i915_seqno_passed(ring->get_seqno(ring), seqno)
Zou Nan hai852835f2010-05-21 09:08:56 +08002111 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02002112
Chris Wilson78501ea2010-10-27 12:18:21 +01002113 ring->user_irq_put(ring);
Chris Wilsonb2223492010-10-27 15:27:33 +01002114 ring->waiting_seqno = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002115
2116 trace_i915_gem_request_wait_end(dev, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07002117 }
Ben Gamariba1234d2009-09-14 17:48:47 -04002118 if (atomic_read(&dev_priv->mm.wedged))
Chris Wilson30dbf0c2010-09-25 10:19:17 +01002119 ret = -EAGAIN;
Eric Anholt673a3942008-07-30 12:06:12 -07002120
2121 if (ret && ret != -ERESTARTSYS)
Daniel Vetter8bff9172010-02-11 22:19:40 +01002122 DRM_ERROR("%s returns %d (awaiting %d at %d, next %d)\n",
Chris Wilson78501ea2010-10-27 12:18:21 +01002123 __func__, ret, seqno, ring->get_seqno(ring),
Daniel Vetter8bff9172010-02-11 22:19:40 +01002124 dev_priv->next_seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07002125
2126 /* Directly dispatch request retiring. While we have the work queue
2127 * to handle this, the waiter on a request often wants an associated
2128 * buffer to have made it to the inactive list, and we would need
2129 * a separate wait queue to handle that.
2130 */
2131 if (ret == 0)
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002132 i915_gem_retire_requests_ring(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07002133
2134 return ret;
2135}
2136
Daniel Vetter48764bf2009-09-15 22:57:32 +02002137/**
2138 * Waits for a sequence number to be signaled, and cleans up the
2139 * request and object lists appropriately for that event.
2140 */
2141static int
Zou Nan hai852835f2010-05-21 09:08:56 +08002142i915_wait_request(struct drm_device *dev, uint32_t seqno,
Chris Wilsona56ba562010-09-28 10:07:56 +01002143 struct intel_ring_buffer *ring)
Daniel Vetter48764bf2009-09-15 22:57:32 +02002144{
Zou Nan hai852835f2010-05-21 09:08:56 +08002145 return i915_do_wait_request(dev, seqno, 1, ring);
Daniel Vetter48764bf2009-09-15 22:57:32 +02002146}
2147
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002148static void
Chris Wilson92204342010-09-18 11:02:01 +01002149i915_gem_flush_ring(struct drm_device *dev,
Chris Wilsonc78ec302010-09-20 12:50:23 +01002150 struct drm_file *file_priv,
Chris Wilson92204342010-09-18 11:02:01 +01002151 struct intel_ring_buffer *ring,
2152 uint32_t invalidate_domains,
2153 uint32_t flush_domains)
2154{
Chris Wilson78501ea2010-10-27 12:18:21 +01002155 ring->flush(ring, invalidate_domains, flush_domains);
Chris Wilson92204342010-09-18 11:02:01 +01002156 i915_gem_process_flushing_list(dev, flush_domains, ring);
2157}
2158
2159static void
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002160i915_gem_flush(struct drm_device *dev,
Chris Wilsonc78ec302010-09-20 12:50:23 +01002161 struct drm_file *file_priv,
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002162 uint32_t invalidate_domains,
Chris Wilson92204342010-09-18 11:02:01 +01002163 uint32_t flush_domains,
2164 uint32_t flush_rings)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002165{
2166 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter8bff9172010-02-11 22:19:40 +01002167
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002168 if (flush_domains & I915_GEM_DOMAIN_CPU)
2169 drm_agp_chipset_flush(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08002170
Chris Wilson92204342010-09-18 11:02:01 +01002171 if ((flush_domains | invalidate_domains) & I915_GEM_GPU_DOMAINS) {
2172 if (flush_rings & RING_RENDER)
Chris Wilsonc78ec302010-09-20 12:50:23 +01002173 i915_gem_flush_ring(dev, file_priv,
Chris Wilson92204342010-09-18 11:02:01 +01002174 &dev_priv->render_ring,
2175 invalidate_domains, flush_domains);
2176 if (flush_rings & RING_BSD)
Chris Wilsonc78ec302010-09-20 12:50:23 +01002177 i915_gem_flush_ring(dev, file_priv,
Chris Wilson92204342010-09-18 11:02:01 +01002178 &dev_priv->bsd_ring,
2179 invalidate_domains, flush_domains);
Chris Wilson549f7362010-10-19 11:19:32 +01002180 if (flush_rings & RING_BLT)
2181 i915_gem_flush_ring(dev, file_priv,
2182 &dev_priv->blt_ring,
2183 invalidate_domains, flush_domains);
Chris Wilson92204342010-09-18 11:02:01 +01002184 }
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002185}
2186
Eric Anholt673a3942008-07-30 12:06:12 -07002187/**
2188 * Ensures that all rendering to the object has completed and the object is
2189 * safe to unbind from the GTT or access from the CPU.
2190 */
2191static int
Chris Wilson2cf34d72010-09-14 13:03:28 +01002192i915_gem_object_wait_rendering(struct drm_gem_object *obj,
2193 bool interruptible)
Eric Anholt673a3942008-07-30 12:06:12 -07002194{
2195 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01002196 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002197 int ret;
2198
Eric Anholte47c68e2008-11-14 13:35:19 -08002199 /* This function only exists to support waiting for existing rendering,
2200 * not for emitting required flushes.
Eric Anholt673a3942008-07-30 12:06:12 -07002201 */
Eric Anholte47c68e2008-11-14 13:35:19 -08002202 BUG_ON((obj->write_domain & I915_GEM_GPU_DOMAINS) != 0);
Eric Anholt673a3942008-07-30 12:06:12 -07002203
2204 /* If there is rendering queued on the buffer being evicted, wait for
2205 * it.
2206 */
2207 if (obj_priv->active) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01002208 ret = i915_do_wait_request(dev,
2209 obj_priv->last_rendering_seqno,
2210 interruptible,
2211 obj_priv->ring);
2212 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002213 return ret;
2214 }
2215
2216 return 0;
2217}
2218
2219/**
2220 * Unbinds an object from the GTT aperture.
2221 */
Jesse Barnes0f973f22009-01-26 17:10:45 -08002222int
Eric Anholt673a3942008-07-30 12:06:12 -07002223i915_gem_object_unbind(struct drm_gem_object *obj)
2224{
2225 struct drm_device *dev = obj->dev;
Chris Wilson73aa8082010-09-30 11:46:12 +01002226 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002227 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002228 int ret = 0;
2229
Eric Anholt673a3942008-07-30 12:06:12 -07002230 if (obj_priv->gtt_space == NULL)
2231 return 0;
2232
2233 if (obj_priv->pin_count != 0) {
2234 DRM_ERROR("Attempting to unbind pinned buffer\n");
2235 return -EINVAL;
2236 }
2237
Eric Anholt5323fd02009-09-09 11:50:45 -07002238 /* blow away mappings if mapped through GTT */
2239 i915_gem_release_mmap(obj);
2240
Eric Anholt673a3942008-07-30 12:06:12 -07002241 /* Move the object to the CPU domain to ensure that
2242 * any possible CPU writes while it's not in the GTT
2243 * are flushed when we go to remap it. This will
2244 * also ensure that all pending GPU writes are finished
2245 * before we unbind.
2246 */
Eric Anholte47c68e2008-11-14 13:35:19 -08002247 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Chris Wilson8dc17752010-07-23 23:18:51 +01002248 if (ret == -ERESTARTSYS)
Eric Anholt673a3942008-07-30 12:06:12 -07002249 return ret;
Chris Wilson8dc17752010-07-23 23:18:51 +01002250 /* Continue on if we fail due to EIO, the GPU is hung so we
2251 * should be safe and we need to cleanup or else we might
2252 * cause memory corruption through use-after-free.
2253 */
Chris Wilson812ed4922010-09-30 15:08:57 +01002254 if (ret) {
2255 i915_gem_clflush_object(obj);
2256 obj->read_domains = obj->write_domain = I915_GEM_DOMAIN_CPU;
2257 }
Eric Anholt673a3942008-07-30 12:06:12 -07002258
Daniel Vetter96b47b62009-12-15 17:50:00 +01002259 /* release the fence reg _after_ flushing */
2260 if (obj_priv->fence_reg != I915_FENCE_REG_NONE)
2261 i915_gem_clear_fence_reg(obj);
2262
Chris Wilson73aa8082010-09-30 11:46:12 +01002263 drm_unbind_agp(obj_priv->agp_mem);
2264 drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002265
Chris Wilsone5281cc2010-10-28 13:45:36 +01002266 i915_gem_object_put_pages_gtt(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002267
Chris Wilsona00b10c2010-09-24 21:15:47 +01002268 i915_gem_info_remove_gtt(dev_priv, obj_priv);
Chris Wilson69dc4982010-10-19 10:36:51 +01002269 list_del_init(&obj_priv->mm_list);
Chris Wilsona00b10c2010-09-24 21:15:47 +01002270 obj_priv->fenceable = true;
2271 obj_priv->mappable = true;
Eric Anholt673a3942008-07-30 12:06:12 -07002272
Chris Wilson73aa8082010-09-30 11:46:12 +01002273 drm_mm_put_block(obj_priv->gtt_space);
2274 obj_priv->gtt_space = NULL;
Chris Wilson9af90d12010-10-17 10:01:56 +01002275 obj_priv->gtt_offset = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002276
Chris Wilson963b4832009-09-20 23:03:54 +01002277 if (i915_gem_object_is_purgeable(obj_priv))
2278 i915_gem_object_truncate(obj);
2279
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002280 trace_i915_gem_object_unbind(obj);
2281
Chris Wilson8dc17752010-07-23 23:18:51 +01002282 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002283}
2284
Chris Wilsona56ba562010-09-28 10:07:56 +01002285static int i915_ring_idle(struct drm_device *dev,
2286 struct intel_ring_buffer *ring)
2287{
Chris Wilson64193402010-10-24 12:38:05 +01002288 if (list_empty(&ring->gpu_write_list))
2289 return 0;
2290
Chris Wilsona56ba562010-09-28 10:07:56 +01002291 i915_gem_flush_ring(dev, NULL, ring,
2292 I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
2293 return i915_wait_request(dev,
2294 i915_gem_next_request_seqno(dev, ring),
2295 ring);
2296}
2297
Chris Wilsonb47eb4a2010-08-07 11:01:23 +01002298int
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002299i915_gpu_idle(struct drm_device *dev)
2300{
2301 drm_i915_private_t *dev_priv = dev->dev_private;
2302 bool lists_empty;
Zou Nan hai852835f2010-05-21 09:08:56 +08002303 int ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002304
Zou Nan haid1b851f2010-05-21 09:08:57 +08002305 lists_empty = (list_empty(&dev_priv->mm.flushing_list) &&
2306 list_empty(&dev_priv->render_ring.active_list) &&
Chris Wilson549f7362010-10-19 11:19:32 +01002307 list_empty(&dev_priv->bsd_ring.active_list) &&
2308 list_empty(&dev_priv->blt_ring.active_list));
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002309 if (lists_empty)
2310 return 0;
2311
2312 /* Flush everything onto the inactive list. */
Chris Wilsona56ba562010-09-28 10:07:56 +01002313 ret = i915_ring_idle(dev, &dev_priv->render_ring);
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002314 if (ret)
2315 return ret;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002316
Chris Wilson87acb0a2010-10-19 10:13:00 +01002317 ret = i915_ring_idle(dev, &dev_priv->bsd_ring);
2318 if (ret)
2319 return ret;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002320
Chris Wilson549f7362010-10-19 11:19:32 +01002321 ret = i915_ring_idle(dev, &dev_priv->blt_ring);
2322 if (ret)
2323 return ret;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002324
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002325 return 0;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002326}
2327
Chris Wilsona00b10c2010-09-24 21:15:47 +01002328static void sandybridge_write_fence_reg(struct drm_gem_object *obj)
Eric Anholt4e901fd2009-10-26 16:44:17 -07002329{
Eric Anholt4e901fd2009-10-26 16:44:17 -07002330 struct drm_device *dev = obj->dev;
2331 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002332 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsona00b10c2010-09-24 21:15:47 +01002333 u32 size = i915_gem_get_gtt_size(obj_priv);
Eric Anholt4e901fd2009-10-26 16:44:17 -07002334 int regnum = obj_priv->fence_reg;
2335 uint64_t val;
2336
Chris Wilsona00b10c2010-09-24 21:15:47 +01002337 val = (uint64_t)((obj_priv->gtt_offset + size - 4096) &
Eric Anholt4e901fd2009-10-26 16:44:17 -07002338 0xfffff000) << 32;
2339 val |= obj_priv->gtt_offset & 0xfffff000;
2340 val |= (uint64_t)((obj_priv->stride / 128) - 1) <<
2341 SANDYBRIDGE_FENCE_PITCH_SHIFT;
2342
2343 if (obj_priv->tiling_mode == I915_TILING_Y)
2344 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2345 val |= I965_FENCE_REG_VALID;
2346
2347 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (regnum * 8), val);
2348}
2349
Chris Wilsona00b10c2010-09-24 21:15:47 +01002350static void i965_write_fence_reg(struct drm_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002351{
Jesse Barnesde151cf2008-11-12 10:03:55 -08002352 struct drm_device *dev = obj->dev;
2353 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002354 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsona00b10c2010-09-24 21:15:47 +01002355 u32 size = i915_gem_get_gtt_size(obj_priv);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002356 int regnum = obj_priv->fence_reg;
2357 uint64_t val;
2358
Chris Wilsona00b10c2010-09-24 21:15:47 +01002359 val = (uint64_t)((obj_priv->gtt_offset + size - 4096) &
Jesse Barnesde151cf2008-11-12 10:03:55 -08002360 0xfffff000) << 32;
2361 val |= obj_priv->gtt_offset & 0xfffff000;
2362 val |= ((obj_priv->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
2363 if (obj_priv->tiling_mode == I915_TILING_Y)
2364 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2365 val |= I965_FENCE_REG_VALID;
2366
2367 I915_WRITE64(FENCE_REG_965_0 + (regnum * 8), val);
2368}
2369
Chris Wilsona00b10c2010-09-24 21:15:47 +01002370static void i915_write_fence_reg(struct drm_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002371{
Jesse Barnesde151cf2008-11-12 10:03:55 -08002372 struct drm_device *dev = obj->dev;
2373 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002374 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsona00b10c2010-09-24 21:15:47 +01002375 u32 size = i915_gem_get_gtt_size(obj_priv);
2376 uint32_t fence_reg, val, pitch_val;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002377 int tile_width;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002378
2379 if ((obj_priv->gtt_offset & ~I915_FENCE_START_MASK) ||
Chris Wilsona00b10c2010-09-24 21:15:47 +01002380 (obj_priv->gtt_offset & (size - 1))) {
2381 WARN(1, "%s: object 0x%08x [fenceable? %d] not 1M or size (0x%08x) aligned [gtt_space offset=%lx, size=%lx]\n",
2382 __func__, obj_priv->gtt_offset, obj_priv->fenceable, size,
2383 obj_priv->gtt_space->start, obj_priv->gtt_space->size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002384 return;
2385 }
2386
Jesse Barnes0f973f22009-01-26 17:10:45 -08002387 if (obj_priv->tiling_mode == I915_TILING_Y &&
2388 HAS_128_BYTE_Y_TILING(dev))
2389 tile_width = 128;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002390 else
Jesse Barnes0f973f22009-01-26 17:10:45 -08002391 tile_width = 512;
2392
2393 /* Note: pitch better be a power of two tile widths */
2394 pitch_val = obj_priv->stride / tile_width;
2395 pitch_val = ffs(pitch_val) - 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002396
Daniel Vetterc36a2a62010-04-17 15:12:03 +02002397 if (obj_priv->tiling_mode == I915_TILING_Y &&
2398 HAS_128_BYTE_Y_TILING(dev))
2399 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2400 else
2401 WARN_ON(pitch_val > I915_FENCE_MAX_PITCH_VAL);
2402
Jesse Barnesde151cf2008-11-12 10:03:55 -08002403 val = obj_priv->gtt_offset;
2404 if (obj_priv->tiling_mode == I915_TILING_Y)
2405 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002406 val |= I915_FENCE_SIZE_BITS(size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002407 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2408 val |= I830_FENCE_REG_VALID;
2409
Chris Wilsona00b10c2010-09-24 21:15:47 +01002410 fence_reg = obj_priv->fence_reg;
2411 if (fence_reg < 8)
2412 fence_reg = FENCE_REG_830_0 + fence_reg * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002413 else
Chris Wilsona00b10c2010-09-24 21:15:47 +01002414 fence_reg = FENCE_REG_945_8 + (fence_reg - 8) * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002415 I915_WRITE(fence_reg, val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002416}
2417
Chris Wilsona00b10c2010-09-24 21:15:47 +01002418static void i830_write_fence_reg(struct drm_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002419{
Jesse Barnesde151cf2008-11-12 10:03:55 -08002420 struct drm_device *dev = obj->dev;
2421 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002422 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsona00b10c2010-09-24 21:15:47 +01002423 u32 size = i915_gem_get_gtt_size(obj_priv);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002424 int regnum = obj_priv->fence_reg;
2425 uint32_t val;
2426 uint32_t pitch_val;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002427 uint32_t fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002428
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002429 if ((obj_priv->gtt_offset & ~I830_FENCE_START_MASK) ||
Jesse Barnesde151cf2008-11-12 10:03:55 -08002430 (obj_priv->gtt_offset & (obj->size - 1))) {
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002431 WARN(1, "%s: object 0x%08x not 512K or size aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002432 __func__, obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002433 return;
2434 }
2435
Eric Anholte76a16d2009-05-26 17:44:56 -07002436 pitch_val = obj_priv->stride / 128;
2437 pitch_val = ffs(pitch_val) - 1;
2438 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2439
Jesse Barnesde151cf2008-11-12 10:03:55 -08002440 val = obj_priv->gtt_offset;
2441 if (obj_priv->tiling_mode == I915_TILING_Y)
2442 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002443 fence_size_bits = I830_FENCE_SIZE_BITS(size);
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002444 WARN_ON(fence_size_bits & ~0x00000f00);
2445 val |= fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002446 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2447 val |= I830_FENCE_REG_VALID;
2448
2449 I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002450}
2451
Chris Wilson2cf34d72010-09-14 13:03:28 +01002452static int i915_find_fence_reg(struct drm_device *dev,
2453 bool interruptible)
Daniel Vetterae3db242010-02-19 11:51:58 +01002454{
Daniel Vetterae3db242010-02-19 11:51:58 +01002455 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002456 struct drm_i915_fence_reg *reg;
2457 struct drm_i915_gem_object *obj_priv = NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002458 int i, avail, ret;
2459
2460 /* First try to find a free reg */
2461 avail = 0;
2462 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2463 reg = &dev_priv->fence_regs[i];
2464 if (!reg->obj)
2465 return i;
2466
Daniel Vetter23010e42010-03-08 13:35:02 +01002467 obj_priv = to_intel_bo(reg->obj);
Daniel Vetterae3db242010-02-19 11:51:58 +01002468 if (!obj_priv->pin_count)
2469 avail++;
2470 }
2471
2472 if (avail == 0)
2473 return -ENOSPC;
2474
2475 /* None available, try to steal one or wait for a user to finish */
Chris Wilsona00b10c2010-09-24 21:15:47 +01002476 avail = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002477 list_for_each_entry(reg, &dev_priv->mm.fence_list,
2478 lru_list) {
Chris Wilsona00b10c2010-09-24 21:15:47 +01002479 obj_priv = to_intel_bo(reg->obj);
Daniel Vetterae3db242010-02-19 11:51:58 +01002480 if (obj_priv->pin_count)
2481 continue;
2482
2483 /* found one! */
Chris Wilsona00b10c2010-09-24 21:15:47 +01002484 avail = obj_priv->fence_reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01002485 break;
2486 }
2487
Chris Wilsona00b10c2010-09-24 21:15:47 +01002488 BUG_ON(avail == I915_FENCE_REG_NONE);
Daniel Vetterae3db242010-02-19 11:51:58 +01002489
2490 /* We only have a reference on obj from the active list. put_fence_reg
2491 * might drop that one, causing a use-after-free in it. So hold a
2492 * private reference to obj like the other callers of put_fence_reg
2493 * (set_tiling ioctl) do. */
Chris Wilsona00b10c2010-09-24 21:15:47 +01002494 drm_gem_object_reference(&obj_priv->base);
2495 ret = i915_gem_object_put_fence_reg(&obj_priv->base, interruptible);
2496 drm_gem_object_unreference(&obj_priv->base);
Daniel Vetterae3db242010-02-19 11:51:58 +01002497 if (ret != 0)
2498 return ret;
2499
Chris Wilsona00b10c2010-09-24 21:15:47 +01002500 return avail;
Daniel Vetterae3db242010-02-19 11:51:58 +01002501}
2502
Jesse Barnesde151cf2008-11-12 10:03:55 -08002503/**
2504 * i915_gem_object_get_fence_reg - set up a fence reg for an object
2505 * @obj: object to map through a fence reg
2506 *
2507 * When mapping objects through the GTT, userspace wants to be able to write
2508 * to them without having to worry about swizzling if the object is tiled.
2509 *
2510 * This function walks the fence regs looking for a free one for @obj,
2511 * stealing one if it can't find any.
2512 *
2513 * It then sets up the reg based on the object's properties: address, pitch
2514 * and tiling format.
2515 */
Chris Wilson8c4b8c32009-06-17 22:08:52 +01002516int
Chris Wilson2cf34d72010-09-14 13:03:28 +01002517i915_gem_object_get_fence_reg(struct drm_gem_object *obj,
2518 bool interruptible)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002519{
2520 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002521 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002522 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002523 struct drm_i915_fence_reg *reg = NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002524 int ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002525
Eric Anholta09ba7f2009-08-29 12:49:51 -07002526 /* Just update our place in the LRU if our fence is getting used. */
2527 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002528 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
2529 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002530 return 0;
2531 }
2532
Jesse Barnesde151cf2008-11-12 10:03:55 -08002533 switch (obj_priv->tiling_mode) {
2534 case I915_TILING_NONE:
2535 WARN(1, "allocating a fence for non-tiled object?\n");
2536 break;
2537 case I915_TILING_X:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002538 if (!obj_priv->stride)
2539 return -EINVAL;
2540 WARN((obj_priv->stride & (512 - 1)),
2541 "object 0x%08x is X tiled but has non-512B pitch\n",
2542 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002543 break;
2544 case I915_TILING_Y:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002545 if (!obj_priv->stride)
2546 return -EINVAL;
2547 WARN((obj_priv->stride & (128 - 1)),
2548 "object 0x%08x is Y tiled but has non-128B pitch\n",
2549 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002550 break;
2551 }
2552
Chris Wilson2cf34d72010-09-14 13:03:28 +01002553 ret = i915_find_fence_reg(dev, interruptible);
Daniel Vetterae3db242010-02-19 11:51:58 +01002554 if (ret < 0)
2555 return ret;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002556
Daniel Vetterae3db242010-02-19 11:51:58 +01002557 obj_priv->fence_reg = ret;
2558 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002559 list_add_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002560
Jesse Barnesde151cf2008-11-12 10:03:55 -08002561 reg->obj = obj;
2562
Chris Wilsone259bef2010-09-17 00:32:02 +01002563 switch (INTEL_INFO(dev)->gen) {
2564 case 6:
Chris Wilsona00b10c2010-09-24 21:15:47 +01002565 sandybridge_write_fence_reg(obj);
Chris Wilsone259bef2010-09-17 00:32:02 +01002566 break;
2567 case 5:
2568 case 4:
Chris Wilsona00b10c2010-09-24 21:15:47 +01002569 i965_write_fence_reg(obj);
Chris Wilsone259bef2010-09-17 00:32:02 +01002570 break;
2571 case 3:
Chris Wilsona00b10c2010-09-24 21:15:47 +01002572 i915_write_fence_reg(obj);
Chris Wilsone259bef2010-09-17 00:32:02 +01002573 break;
2574 case 2:
Chris Wilsona00b10c2010-09-24 21:15:47 +01002575 i830_write_fence_reg(obj);
Chris Wilsone259bef2010-09-17 00:32:02 +01002576 break;
2577 }
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002578
Chris Wilsona00b10c2010-09-24 21:15:47 +01002579 trace_i915_gem_object_get_fence(obj,
2580 obj_priv->fence_reg,
2581 obj_priv->tiling_mode);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002582
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002583 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002584}
2585
2586/**
2587 * i915_gem_clear_fence_reg - clear out fence register info
2588 * @obj: object to clear
2589 *
2590 * Zeroes out the fence register itself and clears out the associated
2591 * data structures in dev_priv and obj_priv.
2592 */
2593static void
2594i915_gem_clear_fence_reg(struct drm_gem_object *obj)
2595{
2596 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002597 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002598 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002599 struct drm_i915_fence_reg *reg =
2600 &dev_priv->fence_regs[obj_priv->fence_reg];
Chris Wilsone259bef2010-09-17 00:32:02 +01002601 uint32_t fence_reg;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002602
Chris Wilsone259bef2010-09-17 00:32:02 +01002603 switch (INTEL_INFO(dev)->gen) {
2604 case 6:
Eric Anholt4e901fd2009-10-26 16:44:17 -07002605 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 +
2606 (obj_priv->fence_reg * 8), 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002607 break;
2608 case 5:
2609 case 4:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002610 I915_WRITE64(FENCE_REG_965_0 + (obj_priv->fence_reg * 8), 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002611 break;
2612 case 3:
Chris Wilson9b74f732010-09-22 19:10:44 +01002613 if (obj_priv->fence_reg >= 8)
Chris Wilsone259bef2010-09-17 00:32:02 +01002614 fence_reg = FENCE_REG_945_8 + (obj_priv->fence_reg - 8) * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002615 else
Chris Wilsone259bef2010-09-17 00:32:02 +01002616 case 2:
2617 fence_reg = FENCE_REG_830_0 + obj_priv->fence_reg * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002618
2619 I915_WRITE(fence_reg, 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002620 break;
Eric Anholtdc529a42009-03-10 22:34:49 -07002621 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002622
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002623 reg->obj = NULL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002624 obj_priv->fence_reg = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002625 list_del_init(&reg->lru_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002626}
2627
Eric Anholt673a3942008-07-30 12:06:12 -07002628/**
Chris Wilson52dc7d32009-06-06 09:46:01 +01002629 * i915_gem_object_put_fence_reg - waits on outstanding fenced access
2630 * to the buffer to finish, and then resets the fence register.
2631 * @obj: tiled object holding a fence register.
Chris Wilson2cf34d72010-09-14 13:03:28 +01002632 * @bool: whether the wait upon the fence is interruptible
Chris Wilson52dc7d32009-06-06 09:46:01 +01002633 *
2634 * Zeroes out the fence register itself and clears out the associated
2635 * data structures in dev_priv and obj_priv.
2636 */
2637int
Chris Wilson2cf34d72010-09-14 13:03:28 +01002638i915_gem_object_put_fence_reg(struct drm_gem_object *obj,
2639 bool interruptible)
Chris Wilson52dc7d32009-06-06 09:46:01 +01002640{
2641 struct drm_device *dev = obj->dev;
Chris Wilson53640e12010-09-20 11:40:50 +01002642 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002643 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson53640e12010-09-20 11:40:50 +01002644 struct drm_i915_fence_reg *reg;
Chris Wilson52dc7d32009-06-06 09:46:01 +01002645
2646 if (obj_priv->fence_reg == I915_FENCE_REG_NONE)
2647 return 0;
2648
Daniel Vetter10ae9bd2010-02-01 13:59:17 +01002649 /* If we've changed tiling, GTT-mappings of the object
2650 * need to re-fault to ensure that the correct fence register
2651 * setup is in place.
2652 */
2653 i915_gem_release_mmap(obj);
2654
Chris Wilson52dc7d32009-06-06 09:46:01 +01002655 /* On the i915, GPU access to tiled buffers is via a fence,
2656 * therefore we must wait for any outstanding access to complete
2657 * before clearing the fence.
2658 */
Chris Wilson53640e12010-09-20 11:40:50 +01002659 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
2660 if (reg->gpu) {
Chris Wilson52dc7d32009-06-06 09:46:01 +01002661 int ret;
2662
Chris Wilson2cf34d72010-09-14 13:03:28 +01002663 ret = i915_gem_object_flush_gpu_write_domain(obj, true);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002664 if (ret)
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002665 return ret;
2666
Chris Wilson2cf34d72010-09-14 13:03:28 +01002667 ret = i915_gem_object_wait_rendering(obj, interruptible);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002668 if (ret)
Chris Wilson52dc7d32009-06-06 09:46:01 +01002669 return ret;
Chris Wilson53640e12010-09-20 11:40:50 +01002670
2671 reg->gpu = false;
Chris Wilson52dc7d32009-06-06 09:46:01 +01002672 }
2673
Daniel Vetter4a726612010-02-01 13:59:16 +01002674 i915_gem_object_flush_gtt_write_domain(obj);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002675 i915_gem_clear_fence_reg(obj);
Chris Wilson52dc7d32009-06-06 09:46:01 +01002676
2677 return 0;
2678}
2679
2680/**
Eric Anholt673a3942008-07-30 12:06:12 -07002681 * Finds free space in the GTT aperture and binds the object there.
2682 */
2683static int
Daniel Vetter920afa72010-09-16 17:54:23 +02002684i915_gem_object_bind_to_gtt(struct drm_gem_object *obj,
2685 unsigned alignment,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002686 bool mappable,
2687 bool need_fence)
Eric Anholt673a3942008-07-30 12:06:12 -07002688{
2689 struct drm_device *dev = obj->dev;
2690 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002691 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002692 struct drm_mm_node *free_space;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002693 gfp_t gfpmask = __GFP_NORETRY | __GFP_NOWARN;
2694 u32 size, fence_size, fence_alignment;
Chris Wilson07f73f62009-09-14 16:50:30 +01002695 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002696
Chris Wilsonbb6baf72009-09-22 14:24:13 +01002697 if (obj_priv->madv != I915_MADV_WILLNEED) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01002698 DRM_ERROR("Attempting to bind a purgeable object\n");
2699 return -EINVAL;
2700 }
2701
Chris Wilsona00b10c2010-09-24 21:15:47 +01002702 fence_size = i915_gem_get_gtt_size(obj_priv);
2703 fence_alignment = i915_gem_get_gtt_alignment(obj_priv);
2704
Eric Anholt673a3942008-07-30 12:06:12 -07002705 if (alignment == 0)
Chris Wilsona00b10c2010-09-24 21:15:47 +01002706 alignment = need_fence ? fence_alignment : 4096;
2707 if (need_fence && alignment & (fence_alignment - 1)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002708 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2709 return -EINVAL;
2710 }
2711
Chris Wilsona00b10c2010-09-24 21:15:47 +01002712 size = need_fence ? fence_size : obj->size;
2713
Chris Wilson654fc602010-05-27 13:18:21 +01002714 /* If the object is bigger than the entire aperture, reject it early
2715 * before evicting everything in a vain attempt to find space.
2716 */
Daniel Vetter920afa72010-09-16 17:54:23 +02002717 if (obj->size >
2718 (mappable ? dev_priv->mm.gtt_mappable_end : dev_priv->mm.gtt_total)) {
Chris Wilson654fc602010-05-27 13:18:21 +01002719 DRM_ERROR("Attempting to bind an object larger than the aperture\n");
2720 return -E2BIG;
2721 }
2722
Eric Anholt673a3942008-07-30 12:06:12 -07002723 search_free:
Daniel Vetter920afa72010-09-16 17:54:23 +02002724 if (mappable)
2725 free_space =
2726 drm_mm_search_free_in_range(&dev_priv->mm.gtt_space,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002727 size, alignment, 0,
Daniel Vetter920afa72010-09-16 17:54:23 +02002728 dev_priv->mm.gtt_mappable_end,
2729 0);
2730 else
2731 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002732 size, alignment, 0);
Daniel Vetter920afa72010-09-16 17:54:23 +02002733
2734 if (free_space != NULL) {
2735 if (mappable)
2736 obj_priv->gtt_space =
2737 drm_mm_get_block_range_generic(free_space,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002738 size, alignment, 0,
Daniel Vetter920afa72010-09-16 17:54:23 +02002739 dev_priv->mm.gtt_mappable_end,
2740 0);
2741 else
2742 obj_priv->gtt_space =
Chris Wilsona00b10c2010-09-24 21:15:47 +01002743 drm_mm_get_block(free_space, size, alignment);
Daniel Vetter920afa72010-09-16 17:54:23 +02002744 }
Eric Anholt673a3942008-07-30 12:06:12 -07002745 if (obj_priv->gtt_space == NULL) {
2746 /* If the gtt is empty and we're still having trouble
2747 * fitting our object in, we're out of memory.
2748 */
Chris Wilsona00b10c2010-09-24 21:15:47 +01002749 ret = i915_gem_evict_something(dev, size, alignment, mappable);
Chris Wilson97311292009-09-21 00:22:34 +01002750 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002751 return ret;
Chris Wilson97311292009-09-21 00:22:34 +01002752
Eric Anholt673a3942008-07-30 12:06:12 -07002753 goto search_free;
2754 }
2755
Chris Wilsone5281cc2010-10-28 13:45:36 +01002756 ret = i915_gem_object_get_pages_gtt(obj, gfpmask);
Eric Anholt673a3942008-07-30 12:06:12 -07002757 if (ret) {
2758 drm_mm_put_block(obj_priv->gtt_space);
2759 obj_priv->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002760
2761 if (ret == -ENOMEM) {
2762 /* first try to clear up some space from the GTT */
Chris Wilsona00b10c2010-09-24 21:15:47 +01002763 ret = i915_gem_evict_something(dev, size,
Daniel Vetter920afa72010-09-16 17:54:23 +02002764 alignment, mappable);
Chris Wilson07f73f62009-09-14 16:50:30 +01002765 if (ret) {
Chris Wilson07f73f62009-09-14 16:50:30 +01002766 /* now try to shrink everyone else */
Chris Wilson4bdadb92010-01-27 13:36:32 +00002767 if (gfpmask) {
2768 gfpmask = 0;
2769 goto search_free;
Chris Wilson07f73f62009-09-14 16:50:30 +01002770 }
2771
2772 return ret;
2773 }
2774
2775 goto search_free;
2776 }
2777
Eric Anholt673a3942008-07-30 12:06:12 -07002778 return ret;
2779 }
2780
Eric Anholt673a3942008-07-30 12:06:12 -07002781 /* Create an AGP memory structure pointing at our pages, and bind it
2782 * into the GTT.
2783 */
2784 obj_priv->agp_mem = drm_agp_bind_pages(dev,
Eric Anholt856fa192009-03-19 14:10:50 -07002785 obj_priv->pages,
Chris Wilson07f73f62009-09-14 16:50:30 +01002786 obj->size >> PAGE_SHIFT,
Chris Wilson9af90d12010-10-17 10:01:56 +01002787 obj_priv->gtt_space->start,
Keith Packardba1eb1d2008-10-14 19:55:10 -07002788 obj_priv->agp_type);
Eric Anholt673a3942008-07-30 12:06:12 -07002789 if (obj_priv->agp_mem == NULL) {
Chris Wilsone5281cc2010-10-28 13:45:36 +01002790 i915_gem_object_put_pages_gtt(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002791 drm_mm_put_block(obj_priv->gtt_space);
2792 obj_priv->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002793
Chris Wilsona00b10c2010-09-24 21:15:47 +01002794 ret = i915_gem_evict_something(dev, size,
2795 alignment, mappable);
Chris Wilson97311292009-09-21 00:22:34 +01002796 if (ret)
Chris Wilson07f73f62009-09-14 16:50:30 +01002797 return ret;
Chris Wilson07f73f62009-09-14 16:50:30 +01002798
2799 goto search_free;
Eric Anholt673a3942008-07-30 12:06:12 -07002800 }
Eric Anholt673a3942008-07-30 12:06:12 -07002801
Daniel Vetterfb7d5162010-10-01 22:05:20 +02002802 obj_priv->gtt_offset = obj_priv->gtt_space->start;
2803
Chris Wilsonbf1a1092010-08-07 11:01:20 +01002804 /* keep track of bounds object by adding it to the inactive list */
Chris Wilson69dc4982010-10-19 10:36:51 +01002805 list_add_tail(&obj_priv->mm_list, &dev_priv->mm.inactive_list);
Chris Wilsona00b10c2010-09-24 21:15:47 +01002806 i915_gem_info_add_gtt(dev_priv, obj_priv);
Chris Wilsonbf1a1092010-08-07 11:01:20 +01002807
Eric Anholt673a3942008-07-30 12:06:12 -07002808 /* Assert that the object is not currently in any GPU domain. As it
2809 * wasn't in the GTT, there shouldn't be any way it could have been in
2810 * a GPU cache
2811 */
Chris Wilson21d509e2009-06-06 09:46:02 +01002812 BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS);
2813 BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS);
Eric Anholt673a3942008-07-30 12:06:12 -07002814
Daniel Vetterec57d262010-09-30 23:42:15 +02002815 trace_i915_gem_object_bind(obj, obj_priv->gtt_offset, mappable);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002816
Chris Wilsona00b10c2010-09-24 21:15:47 +01002817 obj_priv->fenceable =
2818 obj_priv->gtt_space->size == fence_size &&
2819 (obj_priv->gtt_space->start & (fence_alignment -1)) == 0;
2820
2821 obj_priv->mappable =
2822 obj_priv->gtt_offset + obj->size <= dev_priv->mm.gtt_mappable_end;
2823
Eric Anholt673a3942008-07-30 12:06:12 -07002824 return 0;
2825}
2826
2827void
2828i915_gem_clflush_object(struct drm_gem_object *obj)
2829{
Daniel Vetter23010e42010-03-08 13:35:02 +01002830 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002831
2832 /* If we don't have a page list set up, then we're not pinned
2833 * to GPU, and we can ignore the cache flush because it'll happen
2834 * again at bind time.
2835 */
Eric Anholt856fa192009-03-19 14:10:50 -07002836 if (obj_priv->pages == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002837 return;
2838
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002839 trace_i915_gem_object_clflush(obj);
Eric Anholtcfa16a02009-05-26 18:46:16 -07002840
Eric Anholt856fa192009-03-19 14:10:50 -07002841 drm_clflush_pages(obj_priv->pages, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002842}
2843
Eric Anholte47c68e2008-11-14 13:35:19 -08002844/** Flushes any GPU write domain for the object if it's dirty. */
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002845static int
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002846i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj,
2847 bool pipelined)
Eric Anholte47c68e2008-11-14 13:35:19 -08002848{
2849 struct drm_device *dev = obj->dev;
Eric Anholte47c68e2008-11-14 13:35:19 -08002850
2851 if ((obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002852 return 0;
Eric Anholte47c68e2008-11-14 13:35:19 -08002853
2854 /* Queue the GPU write cache flushing we need. */
Chris Wilsonc78ec302010-09-20 12:50:23 +01002855 i915_gem_flush_ring(dev, NULL,
Chris Wilson92204342010-09-18 11:02:01 +01002856 to_intel_bo(obj)->ring,
2857 0, obj->write_domain);
Chris Wilson48b956c2010-09-14 12:50:34 +01002858 BUG_ON(obj->write_domain);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002859
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002860 if (pipelined)
2861 return 0;
2862
Chris Wilson2cf34d72010-09-14 13:03:28 +01002863 return i915_gem_object_wait_rendering(obj, true);
Eric Anholte47c68e2008-11-14 13:35:19 -08002864}
2865
2866/** Flushes the GTT write domain for the object if it's dirty. */
2867static void
2868i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj)
2869{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002870 uint32_t old_write_domain;
2871
Eric Anholte47c68e2008-11-14 13:35:19 -08002872 if (obj->write_domain != I915_GEM_DOMAIN_GTT)
2873 return;
2874
2875 /* No actual flushing is required for the GTT write domain. Writes
2876 * to it immediately go to main memory as far as we know, so there's
2877 * no chipset flush. It also doesn't land in render cache.
2878 */
Chris Wilson4a684a42010-10-28 14:44:08 +01002879 i915_gem_release_mmap(obj);
2880
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002881 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002882 obj->write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002883
2884 trace_i915_gem_object_change_domain(obj,
2885 obj->read_domains,
2886 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002887}
2888
2889/** Flushes the CPU write domain for the object if it's dirty. */
2890static void
2891i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj)
2892{
2893 struct drm_device *dev = obj->dev;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002894 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002895
2896 if (obj->write_domain != I915_GEM_DOMAIN_CPU)
2897 return;
2898
2899 i915_gem_clflush_object(obj);
2900 drm_agp_chipset_flush(dev);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002901 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002902 obj->write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002903
2904 trace_i915_gem_object_change_domain(obj,
2905 obj->read_domains,
2906 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002907}
2908
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002909/**
2910 * Moves a single object to the GTT read, and possibly write domain.
2911 *
2912 * This function returns when the move is complete, including waiting on
2913 * flushes to occur.
2914 */
Jesse Barnes79e53942008-11-07 14:24:08 -08002915int
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002916i915_gem_object_set_to_gtt_domain(struct drm_gem_object *obj, int write)
2917{
Daniel Vetter23010e42010-03-08 13:35:02 +01002918 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002919 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002920 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002921
Eric Anholt02354392008-11-26 13:58:13 -08002922 /* Not valid to be called on unbound objects. */
2923 if (obj_priv->gtt_space == NULL)
2924 return -EINVAL;
2925
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002926 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002927 if (ret != 0)
2928 return ret;
2929
Chris Wilson72133422010-09-13 23:56:38 +01002930 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002931
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002932 if (write) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01002933 ret = i915_gem_object_wait_rendering(obj, true);
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002934 if (ret)
2935 return ret;
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002936 }
Eric Anholte47c68e2008-11-14 13:35:19 -08002937
2938 old_write_domain = obj->write_domain;
2939 old_read_domains = obj->read_domains;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002940
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002941 /* It should now be out of any other write domains, and we can update
2942 * the domain values for our changes.
2943 */
2944 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2945 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002946 if (write) {
Chris Wilson72133422010-09-13 23:56:38 +01002947 obj->read_domains = I915_GEM_DOMAIN_GTT;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002948 obj->write_domain = I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002949 obj_priv->dirty = 1;
2950 }
2951
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002952 trace_i915_gem_object_change_domain(obj,
2953 old_read_domains,
2954 old_write_domain);
2955
Eric Anholte47c68e2008-11-14 13:35:19 -08002956 return 0;
2957}
2958
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002959/*
2960 * Prepare buffer for display plane. Use uninterruptible for possible flush
2961 * wait, as in modesetting process we're not supposed to be interrupted.
2962 */
2963int
Chris Wilson48b956c2010-09-14 12:50:34 +01002964i915_gem_object_set_to_display_plane(struct drm_gem_object *obj,
2965 bool pipelined)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002966{
Daniel Vetter23010e42010-03-08 13:35:02 +01002967 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002968 uint32_t old_read_domains;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002969 int ret;
2970
2971 /* Not valid to be called on unbound objects. */
2972 if (obj_priv->gtt_space == NULL)
2973 return -EINVAL;
2974
Chris Wilsonced270f2010-09-26 22:47:46 +01002975 ret = i915_gem_object_flush_gpu_write_domain(obj, true);
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002976 if (ret)
2977 return ret;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002978
Chris Wilsonced270f2010-09-26 22:47:46 +01002979 /* Currently, we are always called from an non-interruptible context. */
2980 if (!pipelined) {
2981 ret = i915_gem_object_wait_rendering(obj, false);
2982 if (ret)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002983 return ret;
2984 }
2985
Chris Wilsonb118c1e2010-05-27 13:18:14 +01002986 i915_gem_object_flush_cpu_write_domain(obj);
2987
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002988 old_read_domains = obj->read_domains;
Chris Wilsonc78ec302010-09-20 12:50:23 +01002989 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002990
2991 trace_i915_gem_object_change_domain(obj,
2992 old_read_domains,
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002993 obj->write_domain);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002994
2995 return 0;
2996}
2997
Eric Anholte47c68e2008-11-14 13:35:19 -08002998/**
2999 * Moves a single object to the CPU read, and possibly write domain.
3000 *
3001 * This function returns when the move is complete, including waiting on
3002 * flushes to occur.
3003 */
3004static int
3005i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj, int write)
3006{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003007 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003008 int ret;
3009
Daniel Vetterba3d8d72010-02-11 22:37:04 +01003010 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Eric Anholte47c68e2008-11-14 13:35:19 -08003011 if (ret != 0)
3012 return ret;
3013
3014 i915_gem_object_flush_gtt_write_domain(obj);
3015
3016 /* If we have a partially-valid cache of the object in the CPU,
3017 * finish invalidating it and free the per-page flags.
3018 */
3019 i915_gem_object_set_to_full_cpu_read_domain(obj);
3020
Chris Wilson72133422010-09-13 23:56:38 +01003021 if (write) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01003022 ret = i915_gem_object_wait_rendering(obj, true);
Chris Wilson72133422010-09-13 23:56:38 +01003023 if (ret)
3024 return ret;
3025 }
3026
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003027 old_write_domain = obj->write_domain;
3028 old_read_domains = obj->read_domains;
3029
Eric Anholte47c68e2008-11-14 13:35:19 -08003030 /* Flush the CPU cache if it's still invalid. */
3031 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
3032 i915_gem_clflush_object(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003033
3034 obj->read_domains |= I915_GEM_DOMAIN_CPU;
3035 }
3036
3037 /* It should now be out of any other write domains, and we can update
3038 * the domain values for our changes.
3039 */
3040 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
3041
3042 /* If we're writing through the CPU, then the GPU read domains will
3043 * need to be invalidated at next use.
3044 */
3045 if (write) {
Chris Wilsonc78ec302010-09-20 12:50:23 +01003046 obj->read_domains = I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003047 obj->write_domain = I915_GEM_DOMAIN_CPU;
3048 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003049
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003050 trace_i915_gem_object_change_domain(obj,
3051 old_read_domains,
3052 old_write_domain);
3053
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003054 return 0;
3055}
3056
Eric Anholt673a3942008-07-30 12:06:12 -07003057/*
3058 * Set the next domain for the specified object. This
3059 * may not actually perform the necessary flushing/invaliding though,
3060 * as that may want to be batched with other set_domain operations
3061 *
3062 * This is (we hope) the only really tricky part of gem. The goal
3063 * is fairly simple -- track which caches hold bits of the object
3064 * and make sure they remain coherent. A few concrete examples may
3065 * help to explain how it works. For shorthand, we use the notation
3066 * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
3067 * a pair of read and write domain masks.
3068 *
3069 * Case 1: the batch buffer
3070 *
3071 * 1. Allocated
3072 * 2. Written by CPU
3073 * 3. Mapped to GTT
3074 * 4. Read by GPU
3075 * 5. Unmapped from GTT
3076 * 6. Freed
3077 *
3078 * Let's take these a step at a time
3079 *
3080 * 1. Allocated
3081 * Pages allocated from the kernel may still have
3082 * cache contents, so we set them to (CPU, CPU) always.
3083 * 2. Written by CPU (using pwrite)
3084 * The pwrite function calls set_domain (CPU, CPU) and
3085 * this function does nothing (as nothing changes)
3086 * 3. Mapped by GTT
3087 * This function asserts that the object is not
3088 * currently in any GPU-based read or write domains
3089 * 4. Read by GPU
3090 * i915_gem_execbuffer calls set_domain (COMMAND, 0).
3091 * As write_domain is zero, this function adds in the
3092 * current read domains (CPU+COMMAND, 0).
3093 * flush_domains is set to CPU.
3094 * invalidate_domains is set to COMMAND
3095 * clflush is run to get data out of the CPU caches
3096 * then i915_dev_set_domain calls i915_gem_flush to
3097 * emit an MI_FLUSH and drm_agp_chipset_flush
3098 * 5. Unmapped from GTT
3099 * i915_gem_object_unbind calls set_domain (CPU, CPU)
3100 * flush_domains and invalidate_domains end up both zero
3101 * so no flushing/invalidating happens
3102 * 6. Freed
3103 * yay, done
3104 *
3105 * Case 2: The shared render buffer
3106 *
3107 * 1. Allocated
3108 * 2. Mapped to GTT
3109 * 3. Read/written by GPU
3110 * 4. set_domain to (CPU,CPU)
3111 * 5. Read/written by CPU
3112 * 6. Read/written by GPU
3113 *
3114 * 1. Allocated
3115 * Same as last example, (CPU, CPU)
3116 * 2. Mapped to GTT
3117 * Nothing changes (assertions find that it is not in the GPU)
3118 * 3. Read/written by GPU
3119 * execbuffer calls set_domain (RENDER, RENDER)
3120 * flush_domains gets CPU
3121 * invalidate_domains gets GPU
3122 * clflush (obj)
3123 * MI_FLUSH and drm_agp_chipset_flush
3124 * 4. set_domain (CPU, CPU)
3125 * flush_domains gets GPU
3126 * invalidate_domains gets CPU
3127 * wait_rendering (obj) to make sure all drawing is complete.
3128 * This will include an MI_FLUSH to get the data from GPU
3129 * to memory
3130 * clflush (obj) to invalidate the CPU cache
3131 * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
3132 * 5. Read/written by CPU
3133 * cache lines are loaded and dirtied
3134 * 6. Read written by GPU
3135 * Same as last GPU access
3136 *
3137 * Case 3: The constant buffer
3138 *
3139 * 1. Allocated
3140 * 2. Written by CPU
3141 * 3. Read by GPU
3142 * 4. Updated (written) by CPU again
3143 * 5. Read by GPU
3144 *
3145 * 1. Allocated
3146 * (CPU, CPU)
3147 * 2. Written by CPU
3148 * (CPU, CPU)
3149 * 3. Read by GPU
3150 * (CPU+RENDER, 0)
3151 * flush_domains = CPU
3152 * invalidate_domains = RENDER
3153 * clflush (obj)
3154 * MI_FLUSH
3155 * drm_agp_chipset_flush
3156 * 4. Updated (written) by CPU again
3157 * (CPU, CPU)
3158 * flush_domains = 0 (no previous write domain)
3159 * invalidate_domains = 0 (no new read domains)
3160 * 5. Read by GPU
3161 * (CPU+RENDER, 0)
3162 * flush_domains = CPU
3163 * invalidate_domains = RENDER
3164 * clflush (obj)
3165 * MI_FLUSH
3166 * drm_agp_chipset_flush
3167 */
Keith Packardc0d90822008-11-20 23:11:08 -08003168static void
Chris Wilsonb6651452010-10-23 10:15:06 +01003169i915_gem_object_set_to_gpu_domain(struct drm_gem_object *obj,
3170 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07003171{
3172 struct drm_device *dev = obj->dev;
Chris Wilson92204342010-09-18 11:02:01 +01003173 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01003174 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003175 uint32_t invalidate_domains = 0;
3176 uint32_t flush_domains = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07003177
Eric Anholt673a3942008-07-30 12:06:12 -07003178 /*
3179 * If the object isn't moving to a new write domain,
3180 * let the object stay in multiple read domains
3181 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003182 if (obj->pending_write_domain == 0)
3183 obj->pending_read_domains |= obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07003184
3185 /*
3186 * Flush the current write domain if
3187 * the new read domains don't match. Invalidate
3188 * any read domains which differ from the old
3189 * write domain
3190 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003191 if (obj->write_domain &&
3192 obj->write_domain != obj->pending_read_domains) {
Eric Anholt673a3942008-07-30 12:06:12 -07003193 flush_domains |= obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08003194 invalidate_domains |=
3195 obj->pending_read_domains & ~obj->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07003196 }
3197 /*
3198 * Invalidate any read caches which may have
3199 * stale data. That is, any new read domains.
3200 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003201 invalidate_domains |= obj->pending_read_domains & ~obj->read_domains;
Chris Wilson3d2a8122010-09-29 11:39:53 +01003202 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU)
Eric Anholt673a3942008-07-30 12:06:12 -07003203 i915_gem_clflush_object(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003204
Chris Wilson4a684a42010-10-28 14:44:08 +01003205 /* blow away mappings if mapped through GTT */
3206 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_GTT)
3207 i915_gem_release_mmap(obj);
3208
Eric Anholtefbeed92009-02-19 14:54:51 -08003209 /* The actual obj->write_domain will be updated with
3210 * pending_write_domain after we emit the accumulated flush for all
3211 * of our domain changes in execbuffers (which clears objects'
3212 * write_domains). So if we have a current write domain that we
3213 * aren't changing, set pending_write_domain to that.
3214 */
3215 if (flush_domains == 0 && obj->pending_write_domain == 0)
3216 obj->pending_write_domain = obj->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07003217
3218 dev->invalidate_domains |= invalidate_domains;
3219 dev->flush_domains |= flush_domains;
Chris Wilsonb6651452010-10-23 10:15:06 +01003220 if (flush_domains & I915_GEM_GPU_DOMAINS)
Chris Wilson92204342010-09-18 11:02:01 +01003221 dev_priv->mm.flush_rings |= obj_priv->ring->id;
Chris Wilsonb6651452010-10-23 10:15:06 +01003222 if (invalidate_domains & I915_GEM_GPU_DOMAINS)
3223 dev_priv->mm.flush_rings |= ring->id;
Eric Anholt673a3942008-07-30 12:06:12 -07003224}
3225
3226/**
Eric Anholte47c68e2008-11-14 13:35:19 -08003227 * Moves the object from a partially CPU read to a full one.
Eric Anholt673a3942008-07-30 12:06:12 -07003228 *
Eric Anholte47c68e2008-11-14 13:35:19 -08003229 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
3230 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
3231 */
3232static void
3233i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj)
3234{
Daniel Vetter23010e42010-03-08 13:35:02 +01003235 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003236
3237 if (!obj_priv->page_cpu_valid)
3238 return;
3239
3240 /* If we're partially in the CPU read domain, finish moving it in.
3241 */
3242 if (obj->read_domains & I915_GEM_DOMAIN_CPU) {
3243 int i;
3244
3245 for (i = 0; i <= (obj->size - 1) / PAGE_SIZE; i++) {
3246 if (obj_priv->page_cpu_valid[i])
3247 continue;
Eric Anholt856fa192009-03-19 14:10:50 -07003248 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholte47c68e2008-11-14 13:35:19 -08003249 }
Eric Anholte47c68e2008-11-14 13:35:19 -08003250 }
3251
3252 /* Free the page_cpu_valid mappings which are now stale, whether
3253 * or not we've got I915_GEM_DOMAIN_CPU.
3254 */
Eric Anholt9a298b22009-03-24 12:23:04 -07003255 kfree(obj_priv->page_cpu_valid);
Eric Anholte47c68e2008-11-14 13:35:19 -08003256 obj_priv->page_cpu_valid = NULL;
3257}
3258
3259/**
3260 * Set the CPU read domain on a range of the object.
3261 *
3262 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
3263 * not entirely valid. The page_cpu_valid member of the object flags which
3264 * pages have been flushed, and will be respected by
3265 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
3266 * of the whole object.
3267 *
3268 * This function returns when the move is complete, including waiting on
3269 * flushes to occur.
Eric Anholt673a3942008-07-30 12:06:12 -07003270 */
3271static int
Eric Anholte47c68e2008-11-14 13:35:19 -08003272i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
3273 uint64_t offset, uint64_t size)
Eric Anholt673a3942008-07-30 12:06:12 -07003274{
Daniel Vetter23010e42010-03-08 13:35:02 +01003275 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003276 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003277 int i, ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003278
Eric Anholte47c68e2008-11-14 13:35:19 -08003279 if (offset == 0 && size == obj->size)
3280 return i915_gem_object_set_to_cpu_domain(obj, 0);
3281
Daniel Vetterba3d8d72010-02-11 22:37:04 +01003282 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Eric Anholte47c68e2008-11-14 13:35:19 -08003283 if (ret != 0)
3284 return ret;
3285 i915_gem_object_flush_gtt_write_domain(obj);
3286
3287 /* If we're already fully in the CPU read domain, we're done. */
3288 if (obj_priv->page_cpu_valid == NULL &&
3289 (obj->read_domains & I915_GEM_DOMAIN_CPU) != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07003290 return 0;
3291
Eric Anholte47c68e2008-11-14 13:35:19 -08003292 /* Otherwise, create/clear the per-page CPU read domain flag if we're
3293 * newly adding I915_GEM_DOMAIN_CPU
3294 */
Eric Anholt673a3942008-07-30 12:06:12 -07003295 if (obj_priv->page_cpu_valid == NULL) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003296 obj_priv->page_cpu_valid = kzalloc(obj->size / PAGE_SIZE,
3297 GFP_KERNEL);
Eric Anholte47c68e2008-11-14 13:35:19 -08003298 if (obj_priv->page_cpu_valid == NULL)
3299 return -ENOMEM;
3300 } else if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0)
3301 memset(obj_priv->page_cpu_valid, 0, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07003302
3303 /* Flush the cache on any pages that are still invalid from the CPU's
3304 * perspective.
3305 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003306 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
3307 i++) {
Eric Anholt673a3942008-07-30 12:06:12 -07003308 if (obj_priv->page_cpu_valid[i])
3309 continue;
3310
Eric Anholt856fa192009-03-19 14:10:50 -07003311 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07003312
3313 obj_priv->page_cpu_valid[i] = 1;
3314 }
3315
Eric Anholte47c68e2008-11-14 13:35:19 -08003316 /* It should now be out of any other write domains, and we can update
3317 * the domain values for our changes.
3318 */
3319 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
3320
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003321 old_read_domains = obj->read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003322 obj->read_domains |= I915_GEM_DOMAIN_CPU;
3323
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003324 trace_i915_gem_object_change_domain(obj,
3325 old_read_domains,
3326 obj->write_domain);
3327
Eric Anholt673a3942008-07-30 12:06:12 -07003328 return 0;
3329}
3330
3331/**
Eric Anholt673a3942008-07-30 12:06:12 -07003332 * Pin an object to the GTT and evaluate the relocations landing in it.
3333 */
3334static int
Chris Wilson9af90d12010-10-17 10:01:56 +01003335i915_gem_execbuffer_relocate(struct drm_i915_gem_object *obj,
3336 struct drm_file *file_priv,
3337 struct drm_i915_gem_exec_object2 *entry)
Eric Anholt673a3942008-07-30 12:06:12 -07003338{
Chris Wilson9af90d12010-10-17 10:01:56 +01003339 struct drm_device *dev = obj->base.dev;
Keith Packard0839ccb2008-10-30 19:38:48 -07003340 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson2549d6c2010-10-14 12:10:41 +01003341 struct drm_i915_gem_relocation_entry __user *user_relocs;
Chris Wilson9af90d12010-10-17 10:01:56 +01003342 struct drm_gem_object *target_obj = NULL;
3343 uint32_t target_handle = 0;
3344 int i, ret = 0;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003345
Chris Wilson2549d6c2010-10-14 12:10:41 +01003346 user_relocs = (void __user *)(uintptr_t)entry->relocs_ptr;
Eric Anholt673a3942008-07-30 12:06:12 -07003347 for (i = 0; i < entry->relocation_count; i++) {
Chris Wilson2549d6c2010-10-14 12:10:41 +01003348 struct drm_i915_gem_relocation_entry reloc;
Chris Wilson9af90d12010-10-17 10:01:56 +01003349 uint32_t target_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003350
Chris Wilson9af90d12010-10-17 10:01:56 +01003351 if (__copy_from_user_inatomic(&reloc,
3352 user_relocs+i,
3353 sizeof(reloc))) {
3354 ret = -EFAULT;
3355 break;
Eric Anholt673a3942008-07-30 12:06:12 -07003356 }
Chris Wilson2549d6c2010-10-14 12:10:41 +01003357
Chris Wilson9af90d12010-10-17 10:01:56 +01003358 if (reloc.target_handle != target_handle) {
3359 drm_gem_object_unreference(target_obj);
3360
3361 target_obj = drm_gem_object_lookup(dev, file_priv,
3362 reloc.target_handle);
3363 if (target_obj == NULL) {
3364 ret = -ENOENT;
3365 break;
3366 }
3367
3368 target_handle = reloc.target_handle;
Eric Anholt673a3942008-07-30 12:06:12 -07003369 }
Chris Wilson9af90d12010-10-17 10:01:56 +01003370 target_offset = to_intel_bo(target_obj)->gtt_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003371
Chris Wilson8542a0b2009-09-09 21:15:15 +01003372#if WATCH_RELOC
3373 DRM_INFO("%s: obj %p offset %08x target %d "
3374 "read %08x write %08x gtt %08x "
3375 "presumed %08x delta %08x\n",
3376 __func__,
3377 obj,
Chris Wilson2549d6c2010-10-14 12:10:41 +01003378 (int) reloc.offset,
3379 (int) reloc.target_handle,
3380 (int) reloc.read_domains,
3381 (int) reloc.write_domain,
Chris Wilson9af90d12010-10-17 10:01:56 +01003382 (int) target_offset,
Chris Wilson2549d6c2010-10-14 12:10:41 +01003383 (int) reloc.presumed_offset,
3384 reloc.delta);
Chris Wilson8542a0b2009-09-09 21:15:15 +01003385#endif
3386
Eric Anholt673a3942008-07-30 12:06:12 -07003387 /* The target buffer should have appeared before us in the
3388 * exec_object list, so it should have a GTT space bound by now.
3389 */
Chris Wilson9af90d12010-10-17 10:01:56 +01003390 if (target_offset == 0) {
Eric Anholt673a3942008-07-30 12:06:12 -07003391 DRM_ERROR("No GTT space found for object %d\n",
Chris Wilson2549d6c2010-10-14 12:10:41 +01003392 reloc.target_handle);
Chris Wilson9af90d12010-10-17 10:01:56 +01003393 ret = -EINVAL;
3394 break;
Eric Anholt673a3942008-07-30 12:06:12 -07003395 }
3396
Chris Wilson8542a0b2009-09-09 21:15:15 +01003397 /* Validate that the target is in a valid r/w GPU domain */
Chris Wilson2549d6c2010-10-14 12:10:41 +01003398 if (reloc.write_domain & (reloc.write_domain - 1)) {
Daniel Vetter16edd552010-02-19 11:52:02 +01003399 DRM_ERROR("reloc with multiple write domains: "
3400 "obj %p target %d offset %d "
3401 "read %08x write %08x",
Chris Wilson2549d6c2010-10-14 12:10:41 +01003402 obj, reloc.target_handle,
3403 (int) reloc.offset,
3404 reloc.read_domains,
3405 reloc.write_domain);
Chris Wilson9af90d12010-10-17 10:01:56 +01003406 ret = -EINVAL;
3407 break;
Daniel Vetter16edd552010-02-19 11:52:02 +01003408 }
Chris Wilson2549d6c2010-10-14 12:10:41 +01003409 if (reloc.write_domain & I915_GEM_DOMAIN_CPU ||
3410 reloc.read_domains & I915_GEM_DOMAIN_CPU) {
Chris Wilson8542a0b2009-09-09 21:15:15 +01003411 DRM_ERROR("reloc with read/write CPU domains: "
3412 "obj %p target %d offset %d "
3413 "read %08x write %08x",
Chris Wilson2549d6c2010-10-14 12:10:41 +01003414 obj, reloc.target_handle,
3415 (int) reloc.offset,
3416 reloc.read_domains,
3417 reloc.write_domain);
Chris Wilson9af90d12010-10-17 10:01:56 +01003418 ret = -EINVAL;
3419 break;
Chris Wilson8542a0b2009-09-09 21:15:15 +01003420 }
Chris Wilson2549d6c2010-10-14 12:10:41 +01003421 if (reloc.write_domain && target_obj->pending_write_domain &&
3422 reloc.write_domain != target_obj->pending_write_domain) {
Chris Wilson8542a0b2009-09-09 21:15:15 +01003423 DRM_ERROR("Write domain conflict: "
3424 "obj %p target %d offset %d "
3425 "new %08x old %08x\n",
Chris Wilson2549d6c2010-10-14 12:10:41 +01003426 obj, reloc.target_handle,
3427 (int) reloc.offset,
3428 reloc.write_domain,
Chris Wilson8542a0b2009-09-09 21:15:15 +01003429 target_obj->pending_write_domain);
Chris Wilson9af90d12010-10-17 10:01:56 +01003430 ret = -EINVAL;
3431 break;
Chris Wilson8542a0b2009-09-09 21:15:15 +01003432 }
3433
Chris Wilson2549d6c2010-10-14 12:10:41 +01003434 target_obj->pending_read_domains |= reloc.read_domains;
Chris Wilson878a3c32010-10-22 10:48:12 +01003435 target_obj->pending_write_domain |= reloc.write_domain;
Chris Wilson8542a0b2009-09-09 21:15:15 +01003436
3437 /* If the relocation already has the right value in it, no
3438 * more work needs to be done.
3439 */
Chris Wilson9af90d12010-10-17 10:01:56 +01003440 if (target_offset == reloc.presumed_offset)
Chris Wilson8542a0b2009-09-09 21:15:15 +01003441 continue;
Chris Wilson8542a0b2009-09-09 21:15:15 +01003442
3443 /* Check that the relocation address is valid... */
Chris Wilson9af90d12010-10-17 10:01:56 +01003444 if (reloc.offset > obj->base.size - 4) {
Eric Anholt673a3942008-07-30 12:06:12 -07003445 DRM_ERROR("Relocation beyond object bounds: "
3446 "obj %p target %d offset %d size %d.\n",
Chris Wilson2549d6c2010-10-14 12:10:41 +01003447 obj, reloc.target_handle,
Chris Wilson9af90d12010-10-17 10:01:56 +01003448 (int) reloc.offset, (int) obj->base.size);
3449 ret = -EINVAL;
3450 break;
Eric Anholt673a3942008-07-30 12:06:12 -07003451 }
Chris Wilson2549d6c2010-10-14 12:10:41 +01003452 if (reloc.offset & 3) {
Eric Anholt673a3942008-07-30 12:06:12 -07003453 DRM_ERROR("Relocation not 4-byte aligned: "
3454 "obj %p target %d offset %d.\n",
Chris Wilson2549d6c2010-10-14 12:10:41 +01003455 obj, reloc.target_handle,
3456 (int) reloc.offset);
Chris Wilson9af90d12010-10-17 10:01:56 +01003457 ret = -EINVAL;
3458 break;
Eric Anholt673a3942008-07-30 12:06:12 -07003459 }
3460
Chris Wilson8542a0b2009-09-09 21:15:15 +01003461 /* and points to somewhere within the target object. */
Chris Wilson2549d6c2010-10-14 12:10:41 +01003462 if (reloc.delta >= target_obj->size) {
Chris Wilsoncd0b9fb2009-09-15 23:23:18 +01003463 DRM_ERROR("Relocation beyond target object bounds: "
3464 "obj %p target %d delta %d size %d.\n",
Chris Wilson2549d6c2010-10-14 12:10:41 +01003465 obj, reloc.target_handle,
3466 (int) reloc.delta, (int) target_obj->size);
Chris Wilson9af90d12010-10-17 10:01:56 +01003467 ret = -EINVAL;
3468 break;
Eric Anholte47c68e2008-11-14 13:35:19 -08003469 }
3470
Chris Wilson9af90d12010-10-17 10:01:56 +01003471 reloc.delta += target_offset;
3472 if (obj->base.write_domain == I915_GEM_DOMAIN_CPU) {
Chris Wilsonf0c43d92010-10-14 12:44:48 +01003473 uint32_t page_offset = reloc.offset & ~PAGE_MASK;
3474 char *vaddr;
3475
Linus Torvaldsc48c43e2010-10-26 18:57:59 -07003476 vaddr = kmap_atomic(obj->pages[reloc.offset >> PAGE_SHIFT]);
Chris Wilsonf0c43d92010-10-14 12:44:48 +01003477 *(uint32_t *)(vaddr + page_offset) = reloc.delta;
Linus Torvaldsc48c43e2010-10-26 18:57:59 -07003478 kunmap_atomic(vaddr);
Chris Wilsonf0c43d92010-10-14 12:44:48 +01003479 } else {
3480 uint32_t __iomem *reloc_entry;
3481 void __iomem *reloc_page;
Chris Wilsonf0c43d92010-10-14 12:44:48 +01003482
Chris Wilson9af90d12010-10-17 10:01:56 +01003483 ret = i915_gem_object_set_to_gtt_domain(&obj->base, 1);
3484 if (ret)
3485 break;
Chris Wilsonf0c43d92010-10-14 12:44:48 +01003486
3487 /* Map the page containing the relocation we're going to perform. */
Chris Wilson9af90d12010-10-17 10:01:56 +01003488 reloc.offset += obj->gtt_offset;
Chris Wilsonf0c43d92010-10-14 12:44:48 +01003489 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
Linus Torvaldsc48c43e2010-10-26 18:57:59 -07003490 reloc.offset & PAGE_MASK);
Chris Wilsonf0c43d92010-10-14 12:44:48 +01003491 reloc_entry = (uint32_t __iomem *)
3492 (reloc_page + (reloc.offset & ~PAGE_MASK));
3493 iowrite32(reloc.delta, reloc_entry);
Linus Torvaldsc48c43e2010-10-26 18:57:59 -07003494 io_mapping_unmap_atomic(reloc_page);
Eric Anholt673a3942008-07-30 12:06:12 -07003495 }
3496
Chris Wilsonb5dc6082010-10-20 20:59:57 +01003497 /* and update the user's relocation entry */
3498 reloc.presumed_offset = target_offset;
3499 if (__copy_to_user_inatomic(&user_relocs[i].presumed_offset,
3500 &reloc.presumed_offset,
3501 sizeof(reloc.presumed_offset))) {
3502 ret = -EFAULT;
3503 break;
3504 }
Eric Anholt673a3942008-07-30 12:06:12 -07003505 }
3506
Chris Wilson9af90d12010-10-17 10:01:56 +01003507 drm_gem_object_unreference(target_obj);
3508 return ret;
3509}
3510
3511static int
3512i915_gem_execbuffer_pin(struct drm_device *dev,
3513 struct drm_file *file,
3514 struct drm_gem_object **object_list,
3515 struct drm_i915_gem_exec_object2 *exec_list,
3516 int count)
3517{
3518 struct drm_i915_private *dev_priv = dev->dev_private;
3519 int ret, i, retry;
3520
3521 /* attempt to pin all of the buffers into the GTT */
Chris Wilson5eac3ab2010-10-31 08:49:47 +00003522 retry = 0;
3523 do {
Chris Wilson9af90d12010-10-17 10:01:56 +01003524 ret = 0;
3525 for (i = 0; i < count; i++) {
3526 struct drm_i915_gem_exec_object2 *entry = &exec_list[i];
Daniel Vetter16e809a2010-09-16 19:37:04 +02003527 struct drm_i915_gem_object *obj = to_intel_bo(object_list[i]);
Chris Wilson9af90d12010-10-17 10:01:56 +01003528 bool need_fence =
3529 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
3530 obj->tiling_mode != I915_TILING_NONE;
3531
Daniel Vetter16e809a2010-09-16 19:37:04 +02003532 /* g33/pnv can't fence buffers in the unmappable part */
3533 bool need_mappable =
3534 entry->relocation_count ? true : need_fence;
3535
Chris Wilson9af90d12010-10-17 10:01:56 +01003536 /* Check fence reg constraints and rebind if necessary */
Chris Wilsona00b10c2010-09-24 21:15:47 +01003537 if ((need_fence && !obj->fenceable) ||
3538 (need_mappable && !obj->mappable)) {
Chris Wilson9af90d12010-10-17 10:01:56 +01003539 ret = i915_gem_object_unbind(&obj->base);
3540 if (ret)
3541 break;
3542 }
3543
Daniel Vetter920afa72010-09-16 17:54:23 +02003544 ret = i915_gem_object_pin(&obj->base,
Daniel Vetter16e809a2010-09-16 19:37:04 +02003545 entry->alignment,
Chris Wilsona00b10c2010-09-24 21:15:47 +01003546 need_mappable,
3547 need_fence);
Chris Wilson9af90d12010-10-17 10:01:56 +01003548 if (ret)
3549 break;
3550
3551 /*
3552 * Pre-965 chips need a fence register set up in order
3553 * to properly handle blits to/from tiled surfaces.
3554 */
3555 if (need_fence) {
3556 ret = i915_gem_object_get_fence_reg(&obj->base, true);
3557 if (ret) {
3558 i915_gem_object_unpin(&obj->base);
3559 break;
3560 }
3561
3562 dev_priv->fence_regs[obj->fence_reg].gpu = true;
3563 }
3564
3565 entry->offset = obj->gtt_offset;
3566 }
3567
3568 while (i--)
3569 i915_gem_object_unpin(object_list[i]);
3570
Chris Wilson5eac3ab2010-10-31 08:49:47 +00003571 if (ret != -ENOSPC || retry > 1)
Chris Wilson9af90d12010-10-17 10:01:56 +01003572 return ret;
3573
Chris Wilson5eac3ab2010-10-31 08:49:47 +00003574 /* First attempt, just clear anything that is purgeable.
3575 * Second attempt, clear the entire GTT.
3576 */
3577 ret = i915_gem_evict_everything(dev, retry == 0);
Chris Wilson9af90d12010-10-17 10:01:56 +01003578 if (ret)
3579 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003580
Chris Wilson5eac3ab2010-10-31 08:49:47 +00003581 retry++;
3582 } while (1);
Eric Anholt673a3942008-07-30 12:06:12 -07003583}
3584
Eric Anholt673a3942008-07-30 12:06:12 -07003585/* Throttle our rendering by waiting until the ring has completed our requests
3586 * emitted over 20 msec ago.
3587 *
Eric Anholtb9624422009-06-03 07:27:35 +00003588 * Note that if we were to use the current jiffies each time around the loop,
3589 * we wouldn't escape the function with any frames outstanding if the time to
3590 * render a frame was over 20ms.
3591 *
Eric Anholt673a3942008-07-30 12:06:12 -07003592 * This should get us reasonable parallelism between CPU and GPU but also
3593 * relatively low latency when blocking on a particular request to finish.
3594 */
3595static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003596i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003597{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003598 struct drm_i915_private *dev_priv = dev->dev_private;
3599 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00003600 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003601 struct drm_i915_gem_request *request;
3602 struct intel_ring_buffer *ring = NULL;
3603 u32 seqno = 0;
3604 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003605
Chris Wilson1c255952010-09-26 11:03:27 +01003606 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003607 list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
Eric Anholtb9624422009-06-03 07:27:35 +00003608 if (time_after_eq(request->emitted_jiffies, recent_enough))
3609 break;
3610
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003611 ring = request->ring;
3612 seqno = request->seqno;
Eric Anholtb9624422009-06-03 07:27:35 +00003613 }
Chris Wilson1c255952010-09-26 11:03:27 +01003614 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003615
3616 if (seqno == 0)
3617 return 0;
3618
3619 ret = 0;
Chris Wilson78501ea2010-10-27 12:18:21 +01003620 if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) {
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003621 /* And wait for the seqno passing without holding any locks and
3622 * causing extra latency for others. This is safe as the irq
3623 * generation is designed to be run atomically and so is
3624 * lockless.
3625 */
Chris Wilson78501ea2010-10-27 12:18:21 +01003626 ring->user_irq_get(ring);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003627 ret = wait_event_interruptible(ring->irq_queue,
Chris Wilson78501ea2010-10-27 12:18:21 +01003628 i915_seqno_passed(ring->get_seqno(ring), seqno)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003629 || atomic_read(&dev_priv->mm.wedged));
Chris Wilson78501ea2010-10-27 12:18:21 +01003630 ring->user_irq_put(ring);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003631
3632 if (ret == 0 && atomic_read(&dev_priv->mm.wedged))
3633 ret = -EIO;
3634 }
3635
3636 if (ret == 0)
3637 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
Eric Anholtb9624422009-06-03 07:27:35 +00003638
Eric Anholt673a3942008-07-30 12:06:12 -07003639 return ret;
3640}
3641
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003642static int
Chris Wilson2549d6c2010-10-14 12:10:41 +01003643i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec,
3644 uint64_t exec_offset)
Chris Wilson83d60792009-06-06 09:45:57 +01003645{
3646 uint32_t exec_start, exec_len;
3647
3648 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
3649 exec_len = (uint32_t) exec->batch_len;
3650
3651 if ((exec_start | exec_len) & 0x7)
3652 return -EINVAL;
3653
3654 if (!exec_start)
3655 return -EINVAL;
3656
3657 return 0;
3658}
3659
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003660static int
Chris Wilson2549d6c2010-10-14 12:10:41 +01003661validate_exec_list(struct drm_i915_gem_exec_object2 *exec,
3662 int count)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003663{
Chris Wilson2549d6c2010-10-14 12:10:41 +01003664 int i;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003665
Chris Wilson2549d6c2010-10-14 12:10:41 +01003666 for (i = 0; i < count; i++) {
3667 char __user *ptr = (char __user *)(uintptr_t)exec[i].relocs_ptr;
3668 size_t length = exec[i].relocation_count * sizeof(struct drm_i915_gem_relocation_entry);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003669
Chris Wilson2549d6c2010-10-14 12:10:41 +01003670 if (!access_ok(VERIFY_READ, ptr, length))
3671 return -EFAULT;
3672
Chris Wilsonb5dc6082010-10-20 20:59:57 +01003673 /* we may also need to update the presumed offsets */
3674 if (!access_ok(VERIFY_WRITE, ptr, length))
3675 return -EFAULT;
3676
Chris Wilson2549d6c2010-10-14 12:10:41 +01003677 if (fault_in_pages_readable(ptr, length))
3678 return -EFAULT;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003679 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003680
Chris Wilson2549d6c2010-10-14 12:10:41 +01003681 return 0;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003682}
3683
Chris Wilson2549d6c2010-10-14 12:10:41 +01003684static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003685i915_gem_do_execbuffer(struct drm_device *dev, void *data,
Chris Wilson9af90d12010-10-17 10:01:56 +01003686 struct drm_file *file,
Jesse Barnes76446ca2009-12-17 22:05:42 -05003687 struct drm_i915_gem_execbuffer2 *args,
3688 struct drm_i915_gem_exec_object2 *exec_list)
Eric Anholt673a3942008-07-30 12:06:12 -07003689{
3690 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003691 struct drm_gem_object **object_list = NULL;
3692 struct drm_gem_object *batch_obj;
Eric Anholt201361a2009-03-11 12:30:04 -07003693 struct drm_clip_rect *cliprects = NULL;
Chris Wilson8dc5d142010-08-12 12:36:12 +01003694 struct drm_i915_gem_request *request = NULL;
Chris Wilson9af90d12010-10-17 10:01:56 +01003695 int ret, i, flips;
Eric Anholt673a3942008-07-30 12:06:12 -07003696 uint64_t exec_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003697
Zou Nan hai852835f2010-05-21 09:08:56 +08003698 struct intel_ring_buffer *ring = NULL;
3699
Chris Wilson30dbf0c2010-09-25 10:19:17 +01003700 ret = i915_gem_check_is_wedged(dev);
3701 if (ret)
3702 return ret;
3703
Chris Wilson2549d6c2010-10-14 12:10:41 +01003704 ret = validate_exec_list(exec_list, args->buffer_count);
3705 if (ret)
3706 return ret;
3707
Eric Anholt673a3942008-07-30 12:06:12 -07003708#if WATCH_EXEC
3709 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3710 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3711#endif
Chris Wilson549f7362010-10-19 11:19:32 +01003712 switch (args->flags & I915_EXEC_RING_MASK) {
3713 case I915_EXEC_DEFAULT:
3714 case I915_EXEC_RENDER:
3715 ring = &dev_priv->render_ring;
3716 break;
3717 case I915_EXEC_BSD:
Zou Nan haid1b851f2010-05-21 09:08:57 +08003718 if (!HAS_BSD(dev)) {
Chris Wilson549f7362010-10-19 11:19:32 +01003719 DRM_ERROR("execbuf with invalid ring (BSD)\n");
Zou Nan haid1b851f2010-05-21 09:08:57 +08003720 return -EINVAL;
3721 }
3722 ring = &dev_priv->bsd_ring;
Chris Wilson549f7362010-10-19 11:19:32 +01003723 break;
3724 case I915_EXEC_BLT:
3725 if (!HAS_BLT(dev)) {
3726 DRM_ERROR("execbuf with invalid ring (BLT)\n");
3727 return -EINVAL;
3728 }
3729 ring = &dev_priv->blt_ring;
3730 break;
3731 default:
3732 DRM_ERROR("execbuf with unknown ring: %d\n",
3733 (int)(args->flags & I915_EXEC_RING_MASK));
3734 return -EINVAL;
Zou Nan haid1b851f2010-05-21 09:08:57 +08003735 }
3736
Eric Anholt4f481ed2008-09-10 14:22:49 -07003737 if (args->buffer_count < 1) {
3738 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3739 return -EINVAL;
3740 }
Eric Anholtc8e0f932009-11-22 03:49:37 +01003741 object_list = drm_malloc_ab(sizeof(*object_list), args->buffer_count);
Jesse Barnes76446ca2009-12-17 22:05:42 -05003742 if (object_list == NULL) {
3743 DRM_ERROR("Failed to allocate object list for %d buffers\n",
Eric Anholt673a3942008-07-30 12:06:12 -07003744 args->buffer_count);
3745 ret = -ENOMEM;
3746 goto pre_mutex_err;
3747 }
Eric Anholt673a3942008-07-30 12:06:12 -07003748
Eric Anholt201361a2009-03-11 12:30:04 -07003749 if (args->num_cliprects != 0) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003750 cliprects = kcalloc(args->num_cliprects, sizeof(*cliprects),
3751 GFP_KERNEL);
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003752 if (cliprects == NULL) {
3753 ret = -ENOMEM;
Eric Anholt201361a2009-03-11 12:30:04 -07003754 goto pre_mutex_err;
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003755 }
Eric Anholt201361a2009-03-11 12:30:04 -07003756
3757 ret = copy_from_user(cliprects,
3758 (struct drm_clip_rect __user *)
3759 (uintptr_t) args->cliprects_ptr,
3760 sizeof(*cliprects) * args->num_cliprects);
3761 if (ret != 0) {
3762 DRM_ERROR("copy %d cliprects failed: %d\n",
3763 args->num_cliprects, ret);
Dan Carpenterc877cdc2010-06-23 19:03:01 +02003764 ret = -EFAULT;
Eric Anholt201361a2009-03-11 12:30:04 -07003765 goto pre_mutex_err;
3766 }
3767 }
3768
Chris Wilson8dc5d142010-08-12 12:36:12 +01003769 request = kzalloc(sizeof(*request), GFP_KERNEL);
3770 if (request == NULL) {
3771 ret = -ENOMEM;
Chris Wilsona198bc82009-02-06 16:55:20 +00003772 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003773 }
3774
Chris Wilson76c1dec2010-09-25 11:22:51 +01003775 ret = i915_mutex_lock_interruptible(dev);
3776 if (ret)
3777 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003778
Eric Anholt673a3942008-07-30 12:06:12 -07003779 if (dev_priv->mm.suspended) {
Eric Anholt673a3942008-07-30 12:06:12 -07003780 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003781 ret = -EBUSY;
3782 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003783 }
3784
Keith Packardac94a962008-11-20 23:30:27 -08003785 /* Look up object handles */
Eric Anholt673a3942008-07-30 12:06:12 -07003786 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson7e318e12010-10-27 13:43:39 +01003787 struct drm_i915_gem_object *obj_priv;
3788
Chris Wilson9af90d12010-10-17 10:01:56 +01003789 object_list[i] = drm_gem_object_lookup(dev, file,
Eric Anholt673a3942008-07-30 12:06:12 -07003790 exec_list[i].handle);
3791 if (object_list[i] == NULL) {
3792 DRM_ERROR("Invalid object handle %d at index %d\n",
3793 exec_list[i].handle, i);
Chris Wilson0ce907f2010-01-23 20:26:35 +00003794 /* prevent error path from reading uninitialized data */
3795 args->buffer_count = i + 1;
Chris Wilsonbf79cb92010-08-04 14:19:46 +01003796 ret = -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07003797 goto err;
3798 }
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003799
Daniel Vetter23010e42010-03-08 13:35:02 +01003800 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003801 if (obj_priv->in_execbuffer) {
3802 DRM_ERROR("Object %p appears more than once in object list\n",
3803 object_list[i]);
Chris Wilson0ce907f2010-01-23 20:26:35 +00003804 /* prevent error path from reading uninitialized data */
3805 args->buffer_count = i + 1;
Chris Wilsonbf79cb92010-08-04 14:19:46 +01003806 ret = -EINVAL;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003807 goto err;
3808 }
3809 obj_priv->in_execbuffer = true;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003810 }
3811
Chris Wilson9af90d12010-10-17 10:01:56 +01003812 /* Move the objects en-masse into the GTT, evicting if necessary. */
3813 ret = i915_gem_execbuffer_pin(dev, file,
3814 object_list, exec_list,
3815 args->buffer_count);
3816 if (ret)
3817 goto err;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003818
Chris Wilson9af90d12010-10-17 10:01:56 +01003819 /* The objects are in their final locations, apply the relocations. */
3820 for (i = 0; i < args->buffer_count; i++) {
3821 struct drm_i915_gem_object *obj = to_intel_bo(object_list[i]);
3822 obj->base.pending_read_domains = 0;
3823 obj->base.pending_write_domain = 0;
3824 ret = i915_gem_execbuffer_relocate(obj, file, &exec_list[i]);
Eric Anholt673a3942008-07-30 12:06:12 -07003825 if (ret)
3826 goto err;
3827 }
3828
Eric Anholt673a3942008-07-30 12:06:12 -07003829 /* Set the pending read domains for the batch buffer to COMMAND */
3830 batch_obj = object_list[args->buffer_count-1];
Chris Wilson5f26a2c2009-06-06 09:45:58 +01003831 if (batch_obj->pending_write_domain) {
3832 DRM_ERROR("Attempting to use self-modifying batch buffer\n");
3833 ret = -EINVAL;
3834 goto err;
3835 }
3836 batch_obj->pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
Eric Anholt673a3942008-07-30 12:06:12 -07003837
Chris Wilson9af90d12010-10-17 10:01:56 +01003838 /* Sanity check the batch buffer */
3839 exec_offset = to_intel_bo(batch_obj)->gtt_offset;
3840 ret = i915_gem_check_execbuffer(args, exec_offset);
Chris Wilson83d60792009-06-06 09:45:57 +01003841 if (ret != 0) {
3842 DRM_ERROR("execbuf with invalid offset/length\n");
3843 goto err;
3844 }
3845
Keith Packard646f0f62008-11-20 23:23:03 -08003846 /* Zero the global flush/invalidate flags. These
3847 * will be modified as new domains are computed
3848 * for each object
3849 */
3850 dev->invalidate_domains = 0;
3851 dev->flush_domains = 0;
Chris Wilson92204342010-09-18 11:02:01 +01003852 dev_priv->mm.flush_rings = 0;
Chris Wilson7e318e12010-10-27 13:43:39 +01003853 for (i = 0; i < args->buffer_count; i++)
3854 i915_gem_object_set_to_gpu_domain(object_list[i], ring);
Eric Anholt673a3942008-07-30 12:06:12 -07003855
Keith Packard646f0f62008-11-20 23:23:03 -08003856 if (dev->invalidate_domains | dev->flush_domains) {
3857#if WATCH_EXEC
3858 DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
3859 __func__,
3860 dev->invalidate_domains,
3861 dev->flush_domains);
3862#endif
Chris Wilson9af90d12010-10-17 10:01:56 +01003863 i915_gem_flush(dev, file,
Keith Packard646f0f62008-11-20 23:23:03 -08003864 dev->invalidate_domains,
Chris Wilson92204342010-09-18 11:02:01 +01003865 dev->flush_domains,
3866 dev_priv->mm.flush_rings);
Keith Packard646f0f62008-11-20 23:23:03 -08003867 }
Eric Anholt673a3942008-07-30 12:06:12 -07003868
Eric Anholt673a3942008-07-30 12:06:12 -07003869#if WATCH_COHERENCY
3870 for (i = 0; i < args->buffer_count; i++) {
3871 i915_gem_object_check_coherency(object_list[i],
3872 exec_list[i].handle);
3873 }
3874#endif
3875
Eric Anholt673a3942008-07-30 12:06:12 -07003876#if WATCH_EXEC
Ben Gamari6911a9b2009-04-02 11:24:54 -07003877 i915_gem_dump_object(batch_obj,
Eric Anholt673a3942008-07-30 12:06:12 -07003878 args->batch_len,
3879 __func__,
3880 ~0);
3881#endif
3882
Chris Wilsone59f2ba2010-10-07 17:28:15 +01003883 /* Check for any pending flips. As we only maintain a flip queue depth
3884 * of 1, we can simply insert a WAIT for the next display flip prior
3885 * to executing the batch and avoid stalling the CPU.
3886 */
3887 flips = 0;
3888 for (i = 0; i < args->buffer_count; i++) {
3889 if (object_list[i]->write_domain)
3890 flips |= atomic_read(&to_intel_bo(object_list[i])->pending_flip);
3891 }
3892 if (flips) {
3893 int plane, flip_mask;
3894
3895 for (plane = 0; flips >> plane; plane++) {
3896 if (((flips >> plane) & 1) == 0)
3897 continue;
3898
3899 if (plane)
3900 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
3901 else
3902 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
3903
Chris Wilsone1f99ce2010-10-27 12:45:26 +01003904 ret = intel_ring_begin(ring, 2);
3905 if (ret)
3906 goto err;
3907
Chris Wilson78501ea2010-10-27 12:18:21 +01003908 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
3909 intel_ring_emit(ring, MI_NOOP);
3910 intel_ring_advance(ring);
Chris Wilsone59f2ba2010-10-07 17:28:15 +01003911 }
3912 }
3913
Eric Anholt673a3942008-07-30 12:06:12 -07003914 /* Exec the batchbuffer */
Chris Wilson78501ea2010-10-27 12:18:21 +01003915 ret = ring->dispatch_execbuffer(ring, args, cliprects, exec_offset);
Eric Anholt673a3942008-07-30 12:06:12 -07003916 if (ret) {
3917 DRM_ERROR("dispatch failed %d\n", ret);
3918 goto err;
3919 }
3920
Chris Wilson7e318e12010-10-27 13:43:39 +01003921 for (i = 0; i < args->buffer_count; i++) {
3922 struct drm_gem_object *obj = object_list[i];
3923
3924 obj->read_domains = obj->pending_read_domains;
3925 obj->write_domain = obj->pending_write_domain;
3926
3927 i915_gem_object_move_to_active(obj, ring);
3928 if (obj->write_domain) {
3929 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
3930 obj_priv->dirty = 1;
3931 list_move_tail(&obj_priv->gpu_write_list,
3932 &ring->gpu_write_list);
3933 intel_mark_busy(dev, obj);
3934 }
3935
3936 trace_i915_gem_object_change_domain(obj,
3937 obj->read_domains,
3938 obj->write_domain);
3939 }
3940
Eric Anholt673a3942008-07-30 12:06:12 -07003941 /*
3942 * Ensure that the commands in the batch buffer are
3943 * finished before the interrupt fires
3944 */
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01003945 i915_retire_commands(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07003946
Chris Wilson3cce4692010-10-27 16:11:02 +01003947 if (i915_add_request(dev, file, request, ring))
3948 ring->outstanding_lazy_request = true;
3949 else
3950 request = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07003951
Eric Anholt673a3942008-07-30 12:06:12 -07003952err:
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003953 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson7e318e12010-10-27 13:43:39 +01003954 if (object_list[i] == NULL)
3955 break;
3956
3957 to_intel_bo(object_list[i])->in_execbuffer = false;
Julia Lawallaad87df2008-12-21 16:28:47 +01003958 drm_gem_object_unreference(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003959 }
Julia Lawallaad87df2008-12-21 16:28:47 +01003960
Eric Anholt673a3942008-07-30 12:06:12 -07003961 mutex_unlock(&dev->struct_mutex);
3962
Chris Wilson93533c22010-01-31 10:40:48 +00003963pre_mutex_err:
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003964 drm_free_large(object_list);
Eric Anholt9a298b22009-03-24 12:23:04 -07003965 kfree(cliprects);
Chris Wilson8dc5d142010-08-12 12:36:12 +01003966 kfree(request);
Eric Anholt673a3942008-07-30 12:06:12 -07003967
3968 return ret;
3969}
3970
Jesse Barnes76446ca2009-12-17 22:05:42 -05003971/*
3972 * Legacy execbuffer just creates an exec2 list from the original exec object
3973 * list array and passes it to the real function.
3974 */
3975int
3976i915_gem_execbuffer(struct drm_device *dev, void *data,
3977 struct drm_file *file_priv)
3978{
3979 struct drm_i915_gem_execbuffer *args = data;
3980 struct drm_i915_gem_execbuffer2 exec2;
3981 struct drm_i915_gem_exec_object *exec_list = NULL;
3982 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
3983 int ret, i;
3984
3985#if WATCH_EXEC
3986 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3987 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3988#endif
3989
3990 if (args->buffer_count < 1) {
3991 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3992 return -EINVAL;
3993 }
3994
3995 /* Copy in the exec list from userland */
3996 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
3997 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
3998 if (exec_list == NULL || exec2_list == NULL) {
3999 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
4000 args->buffer_count);
4001 drm_free_large(exec_list);
4002 drm_free_large(exec2_list);
4003 return -ENOMEM;
4004 }
4005 ret = copy_from_user(exec_list,
4006 (struct drm_i915_relocation_entry __user *)
4007 (uintptr_t) args->buffers_ptr,
4008 sizeof(*exec_list) * args->buffer_count);
4009 if (ret != 0) {
4010 DRM_ERROR("copy %d exec entries failed %d\n",
4011 args->buffer_count, ret);
4012 drm_free_large(exec_list);
4013 drm_free_large(exec2_list);
4014 return -EFAULT;
4015 }
4016
4017 for (i = 0; i < args->buffer_count; i++) {
4018 exec2_list[i].handle = exec_list[i].handle;
4019 exec2_list[i].relocation_count = exec_list[i].relocation_count;
4020 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
4021 exec2_list[i].alignment = exec_list[i].alignment;
4022 exec2_list[i].offset = exec_list[i].offset;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004023 if (INTEL_INFO(dev)->gen < 4)
Jesse Barnes76446ca2009-12-17 22:05:42 -05004024 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
4025 else
4026 exec2_list[i].flags = 0;
4027 }
4028
4029 exec2.buffers_ptr = args->buffers_ptr;
4030 exec2.buffer_count = args->buffer_count;
4031 exec2.batch_start_offset = args->batch_start_offset;
4032 exec2.batch_len = args->batch_len;
4033 exec2.DR1 = args->DR1;
4034 exec2.DR4 = args->DR4;
4035 exec2.num_cliprects = args->num_cliprects;
4036 exec2.cliprects_ptr = args->cliprects_ptr;
Zou Nan hai852835f2010-05-21 09:08:56 +08004037 exec2.flags = I915_EXEC_RENDER;
Jesse Barnes76446ca2009-12-17 22:05:42 -05004038
4039 ret = i915_gem_do_execbuffer(dev, data, file_priv, &exec2, exec2_list);
4040 if (!ret) {
4041 /* Copy the new buffer offsets back to the user's exec list. */
4042 for (i = 0; i < args->buffer_count; i++)
4043 exec_list[i].offset = exec2_list[i].offset;
4044 /* ... and back out to userspace */
4045 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
4046 (uintptr_t) args->buffers_ptr,
4047 exec_list,
4048 sizeof(*exec_list) * args->buffer_count);
4049 if (ret) {
4050 ret = -EFAULT;
4051 DRM_ERROR("failed to copy %d exec entries "
4052 "back to user (%d)\n",
4053 args->buffer_count, ret);
4054 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05004055 }
4056
4057 drm_free_large(exec_list);
4058 drm_free_large(exec2_list);
4059 return ret;
4060}
4061
4062int
4063i915_gem_execbuffer2(struct drm_device *dev, void *data,
4064 struct drm_file *file_priv)
4065{
4066 struct drm_i915_gem_execbuffer2 *args = data;
4067 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
4068 int ret;
4069
4070#if WATCH_EXEC
4071 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
4072 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
4073#endif
4074
4075 if (args->buffer_count < 1) {
4076 DRM_ERROR("execbuf2 with %d buffers\n", args->buffer_count);
4077 return -EINVAL;
4078 }
4079
4080 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
4081 if (exec2_list == NULL) {
4082 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
4083 args->buffer_count);
4084 return -ENOMEM;
4085 }
4086 ret = copy_from_user(exec2_list,
4087 (struct drm_i915_relocation_entry __user *)
4088 (uintptr_t) args->buffers_ptr,
4089 sizeof(*exec2_list) * args->buffer_count);
4090 if (ret != 0) {
4091 DRM_ERROR("copy %d exec entries failed %d\n",
4092 args->buffer_count, ret);
4093 drm_free_large(exec2_list);
4094 return -EFAULT;
4095 }
4096
4097 ret = i915_gem_do_execbuffer(dev, data, file_priv, args, exec2_list);
4098 if (!ret) {
4099 /* Copy the new buffer offsets back to the user's exec list. */
4100 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
4101 (uintptr_t) args->buffers_ptr,
4102 exec2_list,
4103 sizeof(*exec2_list) * args->buffer_count);
4104 if (ret) {
4105 ret = -EFAULT;
4106 DRM_ERROR("failed to copy %d exec entries "
4107 "back to user (%d)\n",
4108 args->buffer_count, ret);
4109 }
4110 }
4111
4112 drm_free_large(exec2_list);
4113 return ret;
4114}
4115
Eric Anholt673a3942008-07-30 12:06:12 -07004116int
Daniel Vetter920afa72010-09-16 17:54:23 +02004117i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment,
Chris Wilsona00b10c2010-09-24 21:15:47 +01004118 bool mappable, bool need_fence)
Eric Anholt673a3942008-07-30 12:06:12 -07004119{
4120 struct drm_device *dev = obj->dev;
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004121 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01004122 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004123 int ret;
4124
Daniel Vetter778c3542010-05-13 11:49:44 +02004125 BUG_ON(obj_priv->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT);
Chris Wilson23bc5982010-09-29 16:10:57 +01004126 WARN_ON(i915_verify_lists(dev));
Chris Wilsonac0c6b52010-05-27 13:18:18 +01004127
4128 if (obj_priv->gtt_space != NULL) {
Chris Wilsona00b10c2010-09-24 21:15:47 +01004129 if ((alignment && obj_priv->gtt_offset & (alignment - 1)) ||
4130 (need_fence && !obj_priv->fenceable) ||
4131 (mappable && !obj_priv->mappable)) {
Chris Wilsonae7d49d2010-08-04 12:37:41 +01004132 WARN(obj_priv->pin_count,
4133 "bo is already pinned with incorrect alignment:"
Chris Wilsona00b10c2010-09-24 21:15:47 +01004134 " offset=%x, req.alignment=%x, need_fence=%d, fenceable=%d, mappable=%d, cpu_accessible=%d\n",
4135 obj_priv->gtt_offset, alignment,
4136 need_fence, obj_priv->fenceable,
4137 mappable, obj_priv->mappable);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01004138 ret = i915_gem_object_unbind(obj);
4139 if (ret)
4140 return ret;
4141 }
4142 }
4143
Eric Anholt673a3942008-07-30 12:06:12 -07004144 if (obj_priv->gtt_space == NULL) {
Chris Wilsona00b10c2010-09-24 21:15:47 +01004145 ret = i915_gem_object_bind_to_gtt(obj, alignment,
4146 mappable, need_fence);
Chris Wilson97311292009-09-21 00:22:34 +01004147 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07004148 return ret;
Chris Wilson22c344e2009-02-11 14:26:45 +00004149 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05004150
Chris Wilson74653782010-10-29 10:41:23 +01004151 if (obj_priv->pin_count++ == 0) {
Chris Wilsona00b10c2010-09-24 21:15:47 +01004152 i915_gem_info_add_pin(dev_priv, obj_priv, mappable);
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004153 if (!obj_priv->active)
Chris Wilson69dc4982010-10-19 10:36:51 +01004154 list_move_tail(&obj_priv->mm_list,
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004155 &dev_priv->mm.pinned_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004156 }
Daniel Vetterfb7d5162010-10-01 22:05:20 +02004157 BUG_ON(!obj_priv->pin_mappable && mappable);
Eric Anholt673a3942008-07-30 12:06:12 -07004158
Chris Wilson23bc5982010-09-29 16:10:57 +01004159 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07004160 return 0;
4161}
4162
4163void
4164i915_gem_object_unpin(struct drm_gem_object *obj)
4165{
4166 struct drm_device *dev = obj->dev;
4167 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01004168 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004169
Chris Wilson23bc5982010-09-29 16:10:57 +01004170 WARN_ON(i915_verify_lists(dev));
Chris Wilson74653782010-10-29 10:41:23 +01004171 BUG_ON(obj_priv->pin_count == 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004172 BUG_ON(obj_priv->gtt_space == NULL);
4173
Chris Wilson74653782010-10-29 10:41:23 +01004174 if (--obj_priv->pin_count == 0) {
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004175 if (!obj_priv->active)
Chris Wilson69dc4982010-10-19 10:36:51 +01004176 list_move_tail(&obj_priv->mm_list,
Eric Anholt673a3942008-07-30 12:06:12 -07004177 &dev_priv->mm.inactive_list);
Chris Wilsona00b10c2010-09-24 21:15:47 +01004178 i915_gem_info_remove_pin(dev_priv, obj_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07004179 }
Chris Wilson23bc5982010-09-29 16:10:57 +01004180 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07004181}
4182
4183int
4184i915_gem_pin_ioctl(struct drm_device *dev, void *data,
4185 struct drm_file *file_priv)
4186{
4187 struct drm_i915_gem_pin *args = data;
4188 struct drm_gem_object *obj;
4189 struct drm_i915_gem_object *obj_priv;
4190 int ret;
4191
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004192 ret = i915_mutex_lock_interruptible(dev);
4193 if (ret)
4194 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004195
4196 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4197 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004198 ret = -ENOENT;
4199 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07004200 }
Daniel Vetter23010e42010-03-08 13:35:02 +01004201 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004202
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004203 if (obj_priv->madv != I915_MADV_WILLNEED) {
4204 DRM_ERROR("Attempting to pin a purgeable buffer\n");
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004205 ret = -EINVAL;
4206 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004207 }
4208
Jesse Barnes79e53942008-11-07 14:24:08 -08004209 if (obj_priv->pin_filp != NULL && obj_priv->pin_filp != file_priv) {
4210 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
4211 args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004212 ret = -EINVAL;
4213 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08004214 }
4215
4216 obj_priv->user_pin_count++;
4217 obj_priv->pin_filp = file_priv;
4218 if (obj_priv->user_pin_count == 1) {
Chris Wilsona00b10c2010-09-24 21:15:47 +01004219 ret = i915_gem_object_pin(obj, args->alignment,
4220 true, obj_priv->tiling_mode);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004221 if (ret)
4222 goto out;
Eric Anholt673a3942008-07-30 12:06:12 -07004223 }
4224
4225 /* XXX - flush the CPU caches for pinned objects
4226 * as the X server doesn't manage domains yet
4227 */
Eric Anholte47c68e2008-11-14 13:35:19 -08004228 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004229 args->offset = obj_priv->gtt_offset;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004230out:
Eric Anholt673a3942008-07-30 12:06:12 -07004231 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004232unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07004233 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004234 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004235}
4236
4237int
4238i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
4239 struct drm_file *file_priv)
4240{
4241 struct drm_i915_gem_pin *args = data;
4242 struct drm_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08004243 struct drm_i915_gem_object *obj_priv;
Chris Wilson76c1dec2010-09-25 11:22:51 +01004244 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004245
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004246 ret = i915_mutex_lock_interruptible(dev);
4247 if (ret)
4248 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004249
4250 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4251 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004252 ret = -ENOENT;
4253 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07004254 }
Daniel Vetter23010e42010-03-08 13:35:02 +01004255 obj_priv = to_intel_bo(obj);
Chris Wilson76c1dec2010-09-25 11:22:51 +01004256
Jesse Barnes79e53942008-11-07 14:24:08 -08004257 if (obj_priv->pin_filp != file_priv) {
4258 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
4259 args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004260 ret = -EINVAL;
4261 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08004262 }
4263 obj_priv->user_pin_count--;
4264 if (obj_priv->user_pin_count == 0) {
4265 obj_priv->pin_filp = NULL;
4266 i915_gem_object_unpin(obj);
4267 }
Eric Anholt673a3942008-07-30 12:06:12 -07004268
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004269out:
Eric Anholt673a3942008-07-30 12:06:12 -07004270 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004271unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07004272 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004273 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004274}
4275
4276int
4277i915_gem_busy_ioctl(struct drm_device *dev, void *data,
4278 struct drm_file *file_priv)
4279{
4280 struct drm_i915_gem_busy *args = data;
4281 struct drm_gem_object *obj;
4282 struct drm_i915_gem_object *obj_priv;
Chris Wilson30dbf0c2010-09-25 10:19:17 +01004283 int ret;
4284
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004285 ret = i915_mutex_lock_interruptible(dev);
4286 if (ret)
4287 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004288
Eric Anholt673a3942008-07-30 12:06:12 -07004289 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4290 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004291 ret = -ENOENT;
4292 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07004293 }
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004294 obj_priv = to_intel_bo(obj);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004295
Chris Wilson0be555b2010-08-04 15:36:30 +01004296 /* Count all active objects as busy, even if they are currently not used
4297 * by the gpu. Users of this interface expect objects to eventually
4298 * become non-busy without any further actions, therefore emit any
4299 * necessary flushes here.
Eric Anholtc4de0a52008-12-14 19:05:04 -08004300 */
Chris Wilson0be555b2010-08-04 15:36:30 +01004301 args->busy = obj_priv->active;
4302 if (args->busy) {
4303 /* Unconditionally flush objects, even when the gpu still uses this
4304 * object. Userspace calling this function indicates that it wants to
4305 * use this buffer rather sooner than later, so issuing the required
4306 * flush earlier is beneficial.
4307 */
Chris Wilsonc78ec302010-09-20 12:50:23 +01004308 if (obj->write_domain & I915_GEM_GPU_DOMAINS)
4309 i915_gem_flush_ring(dev, file_priv,
Chris Wilson92204342010-09-18 11:02:01 +01004310 obj_priv->ring,
4311 0, obj->write_domain);
Chris Wilson0be555b2010-08-04 15:36:30 +01004312
4313 /* Update the active list for the hardware's current position.
4314 * Otherwise this only updates on a delayed timer or when irqs
4315 * are actually unmasked, and our working set ends up being
4316 * larger than required.
4317 */
4318 i915_gem_retire_requests_ring(dev, obj_priv->ring);
4319
4320 args->busy = obj_priv->active;
4321 }
Eric Anholt673a3942008-07-30 12:06:12 -07004322
4323 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004324unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07004325 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004326 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004327}
4328
4329int
4330i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4331 struct drm_file *file_priv)
4332{
4333 return i915_gem_ring_throttle(dev, file_priv);
4334}
4335
Chris Wilson3ef94da2009-09-14 16:50:29 +01004336int
4337i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4338 struct drm_file *file_priv)
4339{
4340 struct drm_i915_gem_madvise *args = data;
4341 struct drm_gem_object *obj;
4342 struct drm_i915_gem_object *obj_priv;
Chris Wilson76c1dec2010-09-25 11:22:51 +01004343 int ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004344
4345 switch (args->madv) {
4346 case I915_MADV_DONTNEED:
4347 case I915_MADV_WILLNEED:
4348 break;
4349 default:
4350 return -EINVAL;
4351 }
4352
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004353 ret = i915_mutex_lock_interruptible(dev);
4354 if (ret)
4355 return ret;
4356
Chris Wilson3ef94da2009-09-14 16:50:29 +01004357 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4358 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004359 ret = -ENOENT;
4360 goto unlock;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004361 }
Daniel Vetter23010e42010-03-08 13:35:02 +01004362 obj_priv = to_intel_bo(obj);
Chris Wilson3ef94da2009-09-14 16:50:29 +01004363
4364 if (obj_priv->pin_count) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004365 ret = -EINVAL;
4366 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004367 }
4368
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004369 if (obj_priv->madv != __I915_MADV_PURGED)
4370 obj_priv->madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004371
Chris Wilson2d7ef392009-09-20 23:13:10 +01004372 /* if the object is no longer bound, discard its backing storage */
4373 if (i915_gem_object_is_purgeable(obj_priv) &&
4374 obj_priv->gtt_space == NULL)
4375 i915_gem_object_truncate(obj);
4376
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004377 args->retained = obj_priv->madv != __I915_MADV_PURGED;
4378
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004379out:
Chris Wilson3ef94da2009-09-14 16:50:29 +01004380 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004381unlock:
Chris Wilson3ef94da2009-09-14 16:50:29 +01004382 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004383 return ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004384}
4385
Daniel Vetterac52bc52010-04-09 19:05:06 +00004386struct drm_gem_object * i915_gem_alloc_object(struct drm_device *dev,
4387 size_t size)
4388{
Chris Wilson73aa8082010-09-30 11:46:12 +01004389 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc397b902010-04-09 19:05:07 +00004390 struct drm_i915_gem_object *obj;
4391
4392 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
4393 if (obj == NULL)
4394 return NULL;
4395
4396 if (drm_gem_object_init(dev, &obj->base, size) != 0) {
4397 kfree(obj);
4398 return NULL;
4399 }
4400
Chris Wilson73aa8082010-09-30 11:46:12 +01004401 i915_gem_info_add_obj(dev_priv, size);
4402
Daniel Vetterc397b902010-04-09 19:05:07 +00004403 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4404 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4405
4406 obj->agp_type = AGP_USER_MEMORY;
Daniel Vetter62b8b212010-04-09 19:05:08 +00004407 obj->base.driver_private = NULL;
Daniel Vetterc397b902010-04-09 19:05:07 +00004408 obj->fence_reg = I915_FENCE_REG_NONE;
Chris Wilson69dc4982010-10-19 10:36:51 +01004409 INIT_LIST_HEAD(&obj->mm_list);
4410 INIT_LIST_HEAD(&obj->ring_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00004411 INIT_LIST_HEAD(&obj->gpu_write_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00004412 obj->madv = I915_MADV_WILLNEED;
Chris Wilsona00b10c2010-09-24 21:15:47 +01004413 obj->fenceable = true;
4414 obj->mappable = true;
Daniel Vetterc397b902010-04-09 19:05:07 +00004415
Daniel Vetterc397b902010-04-09 19:05:07 +00004416 return &obj->base;
Daniel Vetterac52bc52010-04-09 19:05:06 +00004417}
4418
Eric Anholt673a3942008-07-30 12:06:12 -07004419int i915_gem_init_object(struct drm_gem_object *obj)
4420{
Daniel Vetterc397b902010-04-09 19:05:07 +00004421 BUG();
Jesse Barnesde151cf2008-11-12 10:03:55 -08004422
Eric Anholt673a3942008-07-30 12:06:12 -07004423 return 0;
4424}
4425
Chris Wilsonbe726152010-07-23 23:18:50 +01004426static void i915_gem_free_object_tail(struct drm_gem_object *obj)
4427{
4428 struct drm_device *dev = obj->dev;
4429 drm_i915_private_t *dev_priv = dev->dev_private;
4430 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
4431 int ret;
4432
4433 ret = i915_gem_object_unbind(obj);
4434 if (ret == -ERESTARTSYS) {
Chris Wilson69dc4982010-10-19 10:36:51 +01004435 list_move(&obj_priv->mm_list,
Chris Wilsonbe726152010-07-23 23:18:50 +01004436 &dev_priv->mm.deferred_free_list);
4437 return;
4438 }
4439
Chris Wilson39a01d12010-10-28 13:03:06 +01004440 if (obj->map_list.map)
Chris Wilsonbe726152010-07-23 23:18:50 +01004441 i915_gem_free_mmap_offset(obj);
4442
4443 drm_gem_object_release(obj);
Chris Wilson73aa8082010-09-30 11:46:12 +01004444 i915_gem_info_remove_obj(dev_priv, obj->size);
Chris Wilsonbe726152010-07-23 23:18:50 +01004445
4446 kfree(obj_priv->page_cpu_valid);
4447 kfree(obj_priv->bit_17);
4448 kfree(obj_priv);
4449}
4450
Eric Anholt673a3942008-07-30 12:06:12 -07004451void i915_gem_free_object(struct drm_gem_object *obj)
4452{
Jesse Barnesde151cf2008-11-12 10:03:55 -08004453 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01004454 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004455
Chris Wilson1c5d22f2009-08-25 11:15:50 +01004456 trace_i915_gem_object_destroy(obj);
4457
Eric Anholt673a3942008-07-30 12:06:12 -07004458 while (obj_priv->pin_count > 0)
4459 i915_gem_object_unpin(obj);
4460
Dave Airlie71acb5e2008-12-30 20:31:46 +10004461 if (obj_priv->phys_obj)
4462 i915_gem_detach_phys_object(dev, obj);
4463
Chris Wilsonbe726152010-07-23 23:18:50 +01004464 i915_gem_free_object_tail(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004465}
4466
Jesse Barnes5669fca2009-02-17 15:13:31 -08004467int
Eric Anholt673a3942008-07-30 12:06:12 -07004468i915_gem_idle(struct drm_device *dev)
4469{
4470 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson29105cc2010-01-07 10:39:13 +00004471 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004472
Keith Packard6dbe2772008-10-14 21:41:13 -07004473 mutex_lock(&dev->struct_mutex);
4474
Chris Wilson87acb0a2010-10-19 10:13:00 +01004475 if (dev_priv->mm.suspended) {
Keith Packard6dbe2772008-10-14 21:41:13 -07004476 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004477 return 0;
Keith Packard6dbe2772008-10-14 21:41:13 -07004478 }
Eric Anholt673a3942008-07-30 12:06:12 -07004479
Chris Wilson29105cc2010-01-07 10:39:13 +00004480 ret = i915_gpu_idle(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004481 if (ret) {
4482 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004483 return ret;
Keith Packard6dbe2772008-10-14 21:41:13 -07004484 }
Eric Anholt673a3942008-07-30 12:06:12 -07004485
Chris Wilson29105cc2010-01-07 10:39:13 +00004486 /* Under UMS, be paranoid and evict. */
4487 if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
Chris Wilson5eac3ab2010-10-31 08:49:47 +00004488 ret = i915_gem_evict_inactive(dev, false);
Chris Wilson29105cc2010-01-07 10:39:13 +00004489 if (ret) {
4490 mutex_unlock(&dev->struct_mutex);
4491 return ret;
4492 }
4493 }
4494
4495 /* Hack! Don't let anybody do execbuf while we don't control the chip.
4496 * We need to replace this with a semaphore, or something.
4497 * And not confound mm.suspended!
4498 */
4499 dev_priv->mm.suspended = 1;
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02004500 del_timer_sync(&dev_priv->hangcheck_timer);
Chris Wilson29105cc2010-01-07 10:39:13 +00004501
4502 i915_kernel_lost_context(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004503 i915_gem_cleanup_ringbuffer(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00004504
Keith Packard6dbe2772008-10-14 21:41:13 -07004505 mutex_unlock(&dev->struct_mutex);
4506
Chris Wilson29105cc2010-01-07 10:39:13 +00004507 /* Cancel the retire work handler, which should be idle now. */
4508 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
4509
Eric Anholt673a3942008-07-30 12:06:12 -07004510 return 0;
4511}
4512
Jesse Barnese552eb72010-04-21 11:39:23 -07004513/*
4514 * 965+ support PIPE_CONTROL commands, which provide finer grained control
4515 * over cache flushing.
4516 */
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004517static int
Jesse Barnese552eb72010-04-21 11:39:23 -07004518i915_gem_init_pipe_control(struct drm_device *dev)
4519{
4520 drm_i915_private_t *dev_priv = dev->dev_private;
4521 struct drm_gem_object *obj;
4522 struct drm_i915_gem_object *obj_priv;
4523 int ret;
4524
Eric Anholt34dc4d42010-05-07 14:30:03 -07004525 obj = i915_gem_alloc_object(dev, 4096);
Jesse Barnese552eb72010-04-21 11:39:23 -07004526 if (obj == NULL) {
4527 DRM_ERROR("Failed to allocate seqno page\n");
4528 ret = -ENOMEM;
4529 goto err;
4530 }
4531 obj_priv = to_intel_bo(obj);
4532 obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
4533
Chris Wilsona00b10c2010-09-24 21:15:47 +01004534 ret = i915_gem_object_pin(obj, 4096, true, false);
Jesse Barnese552eb72010-04-21 11:39:23 -07004535 if (ret)
4536 goto err_unref;
4537
4538 dev_priv->seqno_gfx_addr = obj_priv->gtt_offset;
4539 dev_priv->seqno_page = kmap(obj_priv->pages[0]);
4540 if (dev_priv->seqno_page == NULL)
4541 goto err_unpin;
4542
4543 dev_priv->seqno_obj = obj;
4544 memset(dev_priv->seqno_page, 0, PAGE_SIZE);
4545
4546 return 0;
4547
4548err_unpin:
4549 i915_gem_object_unpin(obj);
4550err_unref:
4551 drm_gem_object_unreference(obj);
4552err:
4553 return ret;
4554}
4555
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004556
4557static void
Jesse Barnese552eb72010-04-21 11:39:23 -07004558i915_gem_cleanup_pipe_control(struct drm_device *dev)
4559{
4560 drm_i915_private_t *dev_priv = dev->dev_private;
4561 struct drm_gem_object *obj;
4562 struct drm_i915_gem_object *obj_priv;
4563
4564 obj = dev_priv->seqno_obj;
4565 obj_priv = to_intel_bo(obj);
4566 kunmap(obj_priv->pages[0]);
4567 i915_gem_object_unpin(obj);
4568 drm_gem_object_unreference(obj);
4569 dev_priv->seqno_obj = NULL;
4570
4571 dev_priv->seqno_page = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07004572}
4573
Eric Anholt673a3942008-07-30 12:06:12 -07004574int
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004575i915_gem_init_ringbuffer(struct drm_device *dev)
4576{
4577 drm_i915_private_t *dev_priv = dev->dev_private;
4578 int ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01004579
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004580 if (HAS_PIPE_CONTROL(dev)) {
4581 ret = i915_gem_init_pipe_control(dev);
4582 if (ret)
4583 return ret;
4584 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004585
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08004586 ret = intel_init_render_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004587 if (ret)
4588 goto cleanup_pipe_control;
4589
4590 if (HAS_BSD(dev)) {
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08004591 ret = intel_init_bsd_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004592 if (ret)
4593 goto cleanup_render_ring;
Zou Nan haid1b851f2010-05-21 09:08:57 +08004594 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004595
Chris Wilson549f7362010-10-19 11:19:32 +01004596 if (HAS_BLT(dev)) {
4597 ret = intel_init_blt_ring_buffer(dev);
4598 if (ret)
4599 goto cleanup_bsd_ring;
4600 }
4601
Chris Wilson6f392d5482010-08-07 11:01:22 +01004602 dev_priv->next_seqno = 1;
4603
Chris Wilson68f95ba2010-05-27 13:18:22 +01004604 return 0;
4605
Chris Wilson549f7362010-10-19 11:19:32 +01004606cleanup_bsd_ring:
Chris Wilson78501ea2010-10-27 12:18:21 +01004607 intel_cleanup_ring_buffer(&dev_priv->bsd_ring);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004608cleanup_render_ring:
Chris Wilson78501ea2010-10-27 12:18:21 +01004609 intel_cleanup_ring_buffer(&dev_priv->render_ring);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004610cleanup_pipe_control:
4611 if (HAS_PIPE_CONTROL(dev))
4612 i915_gem_cleanup_pipe_control(dev);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004613 return ret;
4614}
4615
4616void
4617i915_gem_cleanup_ringbuffer(struct drm_device *dev)
4618{
4619 drm_i915_private_t *dev_priv = dev->dev_private;
4620
Chris Wilson78501ea2010-10-27 12:18:21 +01004621 intel_cleanup_ring_buffer(&dev_priv->render_ring);
4622 intel_cleanup_ring_buffer(&dev_priv->bsd_ring);
4623 intel_cleanup_ring_buffer(&dev_priv->blt_ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004624 if (HAS_PIPE_CONTROL(dev))
4625 i915_gem_cleanup_pipe_control(dev);
4626}
4627
4628int
Eric Anholt673a3942008-07-30 12:06:12 -07004629i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
4630 struct drm_file *file_priv)
4631{
4632 drm_i915_private_t *dev_priv = dev->dev_private;
4633 int ret;
4634
Jesse Barnes79e53942008-11-07 14:24:08 -08004635 if (drm_core_check_feature(dev, DRIVER_MODESET))
4636 return 0;
4637
Ben Gamariba1234d2009-09-14 17:48:47 -04004638 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07004639 DRM_ERROR("Reenabling wedged hardware, good luck\n");
Ben Gamariba1234d2009-09-14 17:48:47 -04004640 atomic_set(&dev_priv->mm.wedged, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004641 }
4642
Eric Anholt673a3942008-07-30 12:06:12 -07004643 mutex_lock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004644 dev_priv->mm.suspended = 0;
4645
4646 ret = i915_gem_init_ringbuffer(dev);
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004647 if (ret != 0) {
4648 mutex_unlock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004649 return ret;
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004650 }
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004651
Chris Wilson69dc4982010-10-19 10:36:51 +01004652 BUG_ON(!list_empty(&dev_priv->mm.active_list));
Zou Nan hai852835f2010-05-21 09:08:56 +08004653 BUG_ON(!list_empty(&dev_priv->render_ring.active_list));
Chris Wilson87acb0a2010-10-19 10:13:00 +01004654 BUG_ON(!list_empty(&dev_priv->bsd_ring.active_list));
Chris Wilson549f7362010-10-19 11:19:32 +01004655 BUG_ON(!list_empty(&dev_priv->blt_ring.active_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004656 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
4657 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
Zou Nan hai852835f2010-05-21 09:08:56 +08004658 BUG_ON(!list_empty(&dev_priv->render_ring.request_list));
Chris Wilson87acb0a2010-10-19 10:13:00 +01004659 BUG_ON(!list_empty(&dev_priv->bsd_ring.request_list));
Chris Wilson549f7362010-10-19 11:19:32 +01004660 BUG_ON(!list_empty(&dev_priv->blt_ring.request_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004661 mutex_unlock(&dev->struct_mutex);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004662
Chris Wilson5f353082010-06-07 14:03:03 +01004663 ret = drm_irq_install(dev);
4664 if (ret)
4665 goto cleanup_ringbuffer;
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004666
Eric Anholt673a3942008-07-30 12:06:12 -07004667 return 0;
Chris Wilson5f353082010-06-07 14:03:03 +01004668
4669cleanup_ringbuffer:
4670 mutex_lock(&dev->struct_mutex);
4671 i915_gem_cleanup_ringbuffer(dev);
4672 dev_priv->mm.suspended = 1;
4673 mutex_unlock(&dev->struct_mutex);
4674
4675 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004676}
4677
4678int
4679i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
4680 struct drm_file *file_priv)
4681{
Jesse Barnes79e53942008-11-07 14:24:08 -08004682 if (drm_core_check_feature(dev, DRIVER_MODESET))
4683 return 0;
4684
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004685 drm_irq_uninstall(dev);
Linus Torvaldse6890f62009-09-08 17:09:24 -07004686 return i915_gem_idle(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004687}
4688
4689void
4690i915_gem_lastclose(struct drm_device *dev)
4691{
4692 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004693
Eric Anholte806b492009-01-22 09:56:58 -08004694 if (drm_core_check_feature(dev, DRIVER_MODESET))
4695 return;
4696
Keith Packard6dbe2772008-10-14 21:41:13 -07004697 ret = i915_gem_idle(dev);
4698 if (ret)
4699 DRM_ERROR("failed to idle hardware: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07004700}
4701
Chris Wilson64193402010-10-24 12:38:05 +01004702static void
4703init_ring_lists(struct intel_ring_buffer *ring)
4704{
4705 INIT_LIST_HEAD(&ring->active_list);
4706 INIT_LIST_HEAD(&ring->request_list);
4707 INIT_LIST_HEAD(&ring->gpu_write_list);
4708}
4709
Eric Anholt673a3942008-07-30 12:06:12 -07004710void
4711i915_gem_load(struct drm_device *dev)
4712{
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004713 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07004714 drm_i915_private_t *dev_priv = dev->dev_private;
4715
Chris Wilson69dc4982010-10-19 10:36:51 +01004716 INIT_LIST_HEAD(&dev_priv->mm.active_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004717 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
4718 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004719 INIT_LIST_HEAD(&dev_priv->mm.pinned_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07004720 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Chris Wilsonbe726152010-07-23 23:18:50 +01004721 INIT_LIST_HEAD(&dev_priv->mm.deferred_free_list);
Chris Wilson64193402010-10-24 12:38:05 +01004722 init_ring_lists(&dev_priv->render_ring);
4723 init_ring_lists(&dev_priv->bsd_ring);
4724 init_ring_lists(&dev_priv->blt_ring);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02004725 for (i = 0; i < 16; i++)
4726 INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004727 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
4728 i915_gem_retire_work_handler);
Chris Wilson30dbf0c2010-09-25 10:19:17 +01004729 init_completion(&dev_priv->error_completion);
Chris Wilson31169712009-09-14 16:50:28 +01004730
Dave Airlie94400122010-07-20 13:15:31 +10004731 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
4732 if (IS_GEN3(dev)) {
4733 u32 tmp = I915_READ(MI_ARB_STATE);
4734 if (!(tmp & MI_ARB_C3_LP_WRITE_ENABLE)) {
4735 /* arb state is a masked write, so set bit + bit in mask */
4736 tmp = MI_ARB_C3_LP_WRITE_ENABLE | (MI_ARB_C3_LP_WRITE_ENABLE << MI_ARB_MASK_SHIFT);
4737 I915_WRITE(MI_ARB_STATE, tmp);
4738 }
4739 }
4740
Jesse Barnesde151cf2008-11-12 10:03:55 -08004741 /* Old X drivers will take 0-2 for front, back, depth buffers */
Eric Anholtb397c832010-01-26 09:43:10 -08004742 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4743 dev_priv->fence_reg_start = 3;
Jesse Barnesde151cf2008-11-12 10:03:55 -08004744
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004745 if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08004746 dev_priv->num_fence_regs = 16;
4747 else
4748 dev_priv->num_fence_regs = 8;
4749
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004750 /* Initialize fence registers to zero */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004751 switch (INTEL_INFO(dev)->gen) {
4752 case 6:
4753 for (i = 0; i < 16; i++)
4754 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (i * 8), 0);
4755 break;
4756 case 5:
4757 case 4:
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004758 for (i = 0; i < 16; i++)
4759 I915_WRITE64(FENCE_REG_965_0 + (i * 8), 0);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004760 break;
4761 case 3:
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004762 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4763 for (i = 0; i < 8; i++)
4764 I915_WRITE(FENCE_REG_945_8 + (i * 4), 0);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004765 case 2:
4766 for (i = 0; i < 8; i++)
4767 I915_WRITE(FENCE_REG_830_0 + (i * 4), 0);
4768 break;
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004769 }
Eric Anholt673a3942008-07-30 12:06:12 -07004770 i915_gem_detect_bit_6_swizzle(dev);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05004771 init_waitqueue_head(&dev_priv->pending_flip_queue);
Chris Wilson17250b72010-10-28 12:51:39 +01004772
4773 dev_priv->mm.inactive_shrinker.shrink = i915_gem_inactive_shrink;
4774 dev_priv->mm.inactive_shrinker.seeks = DEFAULT_SEEKS;
4775 register_shrinker(&dev_priv->mm.inactive_shrinker);
Eric Anholt673a3942008-07-30 12:06:12 -07004776}
Dave Airlie71acb5e2008-12-30 20:31:46 +10004777
4778/*
4779 * Create a physically contiguous memory object for this object
4780 * e.g. for cursor + overlay regs
4781 */
Chris Wilson995b6762010-08-20 13:23:26 +01004782static int i915_gem_init_phys_object(struct drm_device *dev,
4783 int id, int size, int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004784{
4785 drm_i915_private_t *dev_priv = dev->dev_private;
4786 struct drm_i915_gem_phys_object *phys_obj;
4787 int ret;
4788
4789 if (dev_priv->mm.phys_objs[id - 1] || !size)
4790 return 0;
4791
Eric Anholt9a298b22009-03-24 12:23:04 -07004792 phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004793 if (!phys_obj)
4794 return -ENOMEM;
4795
4796 phys_obj->id = id;
4797
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004798 phys_obj->handle = drm_pci_alloc(dev, size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004799 if (!phys_obj->handle) {
4800 ret = -ENOMEM;
4801 goto kfree_obj;
4802 }
4803#ifdef CONFIG_X86
4804 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4805#endif
4806
4807 dev_priv->mm.phys_objs[id - 1] = phys_obj;
4808
4809 return 0;
4810kfree_obj:
Eric Anholt9a298b22009-03-24 12:23:04 -07004811 kfree(phys_obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004812 return ret;
4813}
4814
Chris Wilson995b6762010-08-20 13:23:26 +01004815static void i915_gem_free_phys_object(struct drm_device *dev, int id)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004816{
4817 drm_i915_private_t *dev_priv = dev->dev_private;
4818 struct drm_i915_gem_phys_object *phys_obj;
4819
4820 if (!dev_priv->mm.phys_objs[id - 1])
4821 return;
4822
4823 phys_obj = dev_priv->mm.phys_objs[id - 1];
4824 if (phys_obj->cur_obj) {
4825 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
4826 }
4827
4828#ifdef CONFIG_X86
4829 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4830#endif
4831 drm_pci_free(dev, phys_obj->handle);
4832 kfree(phys_obj);
4833 dev_priv->mm.phys_objs[id - 1] = NULL;
4834}
4835
4836void i915_gem_free_all_phys_object(struct drm_device *dev)
4837{
4838 int i;
4839
Dave Airlie260883c2009-01-22 17:58:49 +10004840 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004841 i915_gem_free_phys_object(dev, i);
4842}
4843
4844void i915_gem_detach_phys_object(struct drm_device *dev,
4845 struct drm_gem_object *obj)
4846{
Chris Wilsone5281cc2010-10-28 13:45:36 +01004847 struct address_space *mapping = obj->filp->f_path.dentry->d_inode->i_mapping;
4848 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
4849 char *vaddr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004850 int i;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004851 int page_count;
4852
Dave Airlie71acb5e2008-12-30 20:31:46 +10004853 if (!obj_priv->phys_obj)
4854 return;
Chris Wilsone5281cc2010-10-28 13:45:36 +01004855 vaddr = obj_priv->phys_obj->handle->vaddr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004856
4857 page_count = obj->size / PAGE_SIZE;
4858
4859 for (i = 0; i < page_count; i++) {
Chris Wilsone5281cc2010-10-28 13:45:36 +01004860 struct page *page = read_cache_page_gfp(mapping, i,
4861 GFP_HIGHUSER | __GFP_RECLAIMABLE);
4862 if (!IS_ERR(page)) {
4863 char *dst = kmap_atomic(page);
4864 memcpy(dst, vaddr + i*PAGE_SIZE, PAGE_SIZE);
4865 kunmap_atomic(dst);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004866
Chris Wilsone5281cc2010-10-28 13:45:36 +01004867 drm_clflush_pages(&page, 1);
4868
4869 set_page_dirty(page);
4870 mark_page_accessed(page);
4871 page_cache_release(page);
4872 }
Dave Airlie71acb5e2008-12-30 20:31:46 +10004873 }
Dave Airlie71acb5e2008-12-30 20:31:46 +10004874 drm_agp_chipset_flush(dev);
Chris Wilsond78b47b2009-06-17 21:52:49 +01004875
Dave Airlie71acb5e2008-12-30 20:31:46 +10004876 obj_priv->phys_obj->cur_obj = NULL;
4877 obj_priv->phys_obj = NULL;
4878}
4879
4880int
4881i915_gem_attach_phys_object(struct drm_device *dev,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004882 struct drm_gem_object *obj,
4883 int id,
4884 int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004885{
Chris Wilsone5281cc2010-10-28 13:45:36 +01004886 struct address_space *mapping = obj->filp->f_path.dentry->d_inode->i_mapping;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004887 drm_i915_private_t *dev_priv = dev->dev_private;
4888 struct drm_i915_gem_object *obj_priv;
4889 int ret = 0;
4890 int page_count;
4891 int i;
4892
4893 if (id > I915_MAX_PHYS_OBJECT)
4894 return -EINVAL;
4895
Daniel Vetter23010e42010-03-08 13:35:02 +01004896 obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004897
4898 if (obj_priv->phys_obj) {
4899 if (obj_priv->phys_obj->id == id)
4900 return 0;
4901 i915_gem_detach_phys_object(dev, obj);
4902 }
4903
Dave Airlie71acb5e2008-12-30 20:31:46 +10004904 /* create a new object */
4905 if (!dev_priv->mm.phys_objs[id - 1]) {
4906 ret = i915_gem_init_phys_object(dev, id,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004907 obj->size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004908 if (ret) {
Linus Torvaldsaeb565d2009-01-26 10:01:53 -08004909 DRM_ERROR("failed to init phys object %d size: %zu\n", id, obj->size);
Chris Wilsone5281cc2010-10-28 13:45:36 +01004910 return ret;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004911 }
4912 }
4913
4914 /* bind to the object */
4915 obj_priv->phys_obj = dev_priv->mm.phys_objs[id - 1];
4916 obj_priv->phys_obj->cur_obj = obj;
4917
Dave Airlie71acb5e2008-12-30 20:31:46 +10004918 page_count = obj->size / PAGE_SIZE;
4919
4920 for (i = 0; i < page_count; i++) {
Chris Wilsone5281cc2010-10-28 13:45:36 +01004921 struct page *page;
4922 char *dst, *src;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004923
Chris Wilsone5281cc2010-10-28 13:45:36 +01004924 page = read_cache_page_gfp(mapping, i,
4925 GFP_HIGHUSER | __GFP_RECLAIMABLE);
4926 if (IS_ERR(page))
4927 return PTR_ERR(page);
4928
Chris Wilsonff75b9b2010-10-30 22:52:31 +01004929 src = kmap_atomic(page);
Chris Wilsone5281cc2010-10-28 13:45:36 +01004930 dst = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004931 memcpy(dst, src, PAGE_SIZE);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -07004932 kunmap_atomic(src);
Chris Wilsone5281cc2010-10-28 13:45:36 +01004933
4934 mark_page_accessed(page);
4935 page_cache_release(page);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004936 }
4937
4938 return 0;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004939}
4940
4941static int
4942i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
4943 struct drm_i915_gem_pwrite *args,
4944 struct drm_file *file_priv)
4945{
Daniel Vetter23010e42010-03-08 13:35:02 +01004946 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004947 void *obj_addr;
4948 int ret;
4949 char __user *user_data;
4950
4951 user_data = (char __user *) (uintptr_t) args->data_ptr;
4952 obj_addr = obj_priv->phys_obj->handle->vaddr + args->offset;
4953
Zhao Yakui44d98a62009-10-09 11:39:40 +08004954 DRM_DEBUG_DRIVER("obj_addr %p, %lld\n", obj_addr, args->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004955 ret = copy_from_user(obj_addr, user_data, args->size);
4956 if (ret)
4957 return -EFAULT;
4958
4959 drm_agp_chipset_flush(dev);
4960 return 0;
4961}
Eric Anholtb9624422009-06-03 07:27:35 +00004962
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004963void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00004964{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004965 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00004966
4967 /* Clean up our request list when the client is going away, so that
4968 * later retire_requests won't dereference our soon-to-be-gone
4969 * file_priv.
4970 */
Chris Wilson1c255952010-09-26 11:03:27 +01004971 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004972 while (!list_empty(&file_priv->mm.request_list)) {
4973 struct drm_i915_gem_request *request;
4974
4975 request = list_first_entry(&file_priv->mm.request_list,
4976 struct drm_i915_gem_request,
4977 client_list);
4978 list_del(&request->client_list);
4979 request->file_priv = NULL;
4980 }
Chris Wilson1c255952010-09-26 11:03:27 +01004981 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00004982}
Chris Wilson31169712009-09-14 16:50:28 +01004983
Chris Wilson31169712009-09-14 16:50:28 +01004984static int
Chris Wilson1637ef42010-04-20 17:10:35 +01004985i915_gpu_is_active(struct drm_device *dev)
4986{
4987 drm_i915_private_t *dev_priv = dev->dev_private;
4988 int lists_empty;
4989
Chris Wilson1637ef42010-04-20 17:10:35 +01004990 lists_empty = list_empty(&dev_priv->mm.flushing_list) &&
Chris Wilson17250b72010-10-28 12:51:39 +01004991 list_empty(&dev_priv->mm.active_list);
Chris Wilson1637ef42010-04-20 17:10:35 +01004992
4993 return !lists_empty;
4994}
4995
4996static int
Chris Wilson17250b72010-10-28 12:51:39 +01004997i915_gem_inactive_shrink(struct shrinker *shrinker,
4998 int nr_to_scan,
4999 gfp_t gfp_mask)
Chris Wilson31169712009-09-14 16:50:28 +01005000{
Chris Wilson17250b72010-10-28 12:51:39 +01005001 struct drm_i915_private *dev_priv =
5002 container_of(shrinker,
5003 struct drm_i915_private,
5004 mm.inactive_shrinker);
5005 struct drm_device *dev = dev_priv->dev;
5006 struct drm_i915_gem_object *obj, *next;
5007 int cnt;
5008
5009 if (!mutex_trylock(&dev->struct_mutex))
Chris Wilsonbbe2e112010-10-28 22:35:07 +01005010 return 0;
Chris Wilson31169712009-09-14 16:50:28 +01005011
5012 /* "fast-path" to count number of available objects */
5013 if (nr_to_scan == 0) {
Chris Wilson17250b72010-10-28 12:51:39 +01005014 cnt = 0;
5015 list_for_each_entry(obj,
5016 &dev_priv->mm.inactive_list,
5017 mm_list)
5018 cnt++;
5019 mutex_unlock(&dev->struct_mutex);
5020 return cnt / 100 * sysctl_vfs_cache_pressure;
Chris Wilson31169712009-09-14 16:50:28 +01005021 }
5022
Chris Wilson1637ef42010-04-20 17:10:35 +01005023rescan:
Chris Wilson31169712009-09-14 16:50:28 +01005024 /* first scan for clean buffers */
Chris Wilson17250b72010-10-28 12:51:39 +01005025 i915_gem_retire_requests(dev);
Chris Wilson31169712009-09-14 16:50:28 +01005026
Chris Wilson17250b72010-10-28 12:51:39 +01005027 list_for_each_entry_safe(obj, next,
5028 &dev_priv->mm.inactive_list,
5029 mm_list) {
5030 if (i915_gem_object_is_purgeable(obj)) {
5031 i915_gem_object_unbind(&obj->base);
5032 if (--nr_to_scan == 0)
5033 break;
Chris Wilson31169712009-09-14 16:50:28 +01005034 }
Chris Wilson31169712009-09-14 16:50:28 +01005035 }
5036
5037 /* second pass, evict/count anything still on the inactive list */
Chris Wilson17250b72010-10-28 12:51:39 +01005038 cnt = 0;
5039 list_for_each_entry_safe(obj, next,
5040 &dev_priv->mm.inactive_list,
5041 mm_list) {
5042 if (nr_to_scan) {
5043 i915_gem_object_unbind(&obj->base);
5044 nr_to_scan--;
5045 } else
5046 cnt++;
Chris Wilson31169712009-09-14 16:50:28 +01005047 }
5048
Chris Wilson17250b72010-10-28 12:51:39 +01005049 if (nr_to_scan && i915_gpu_is_active(dev)) {
Chris Wilson1637ef42010-04-20 17:10:35 +01005050 /*
5051 * We are desperate for pages, so as a last resort, wait
5052 * for the GPU to finish and discard whatever we can.
5053 * This has a dramatic impact to reduce the number of
5054 * OOM-killer events whilst running the GPU aggressively.
5055 */
Chris Wilson17250b72010-10-28 12:51:39 +01005056 if (i915_gpu_idle(dev) == 0)
Chris Wilson1637ef42010-04-20 17:10:35 +01005057 goto rescan;
5058 }
Chris Wilson17250b72010-10-28 12:51:39 +01005059 mutex_unlock(&dev->struct_mutex);
5060 return cnt / 100 * sysctl_vfs_cache_pressure;
Chris Wilson31169712009-09-14 16:50:28 +01005061}