blob: 061426e1bbf44869321528ddf0329dc253aa649d [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>
Eric Anholt673a3942008-07-30 12:06:12 -070037
Chris Wilson0f8c6d72010-11-01 12:38:44 +000038struct change_domains {
39 uint32_t invalidate_domains;
40 uint32_t flush_domains;
41 uint32_t flush_rings;
42};
43
Chris Wilson05394f32010-11-08 19:18:58 +000044static int i915_gem_object_flush_gpu_write_domain(struct drm_i915_gem_object *obj,
Chris Wilson919926a2010-11-12 13:42:53 +000045 struct intel_ring_buffer *pipelined);
Chris Wilson05394f32010-11-08 19:18:58 +000046static void i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj);
47static void i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj);
48static int i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj,
Chris Wilson919926a2010-11-12 13:42:53 +000049 bool write);
Chris Wilson05394f32010-11-08 19:18:58 +000050static int i915_gem_object_set_cpu_read_domain_range(struct drm_i915_gem_object *obj,
Eric Anholte47c68e2008-11-14 13:35:19 -080051 uint64_t offset,
52 uint64_t size);
Chris Wilson05394f32010-11-08 19:18:58 +000053static void i915_gem_object_set_to_full_cpu_read_domain(struct drm_i915_gem_object *obj);
54static int i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj,
Chris Wilson2cf34d72010-09-14 13:03:28 +010055 bool interruptible);
Chris Wilson05394f32010-11-08 19:18:58 +000056static int i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
Chris Wilsona00b10c2010-09-24 21:15:47 +010057 unsigned alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +010058 bool map_and_fenceable);
Chris Wilson05394f32010-11-08 19:18:58 +000059static void i915_gem_clear_fence_reg(struct drm_i915_gem_object *obj);
60static int i915_gem_phys_pwrite(struct drm_device *dev,
61 struct drm_i915_gem_object *obj,
Dave Airlie71acb5e2008-12-30 20:31:46 +100062 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +000063 struct drm_file *file);
64static void i915_gem_free_object_tail(struct drm_i915_gem_object *obj);
Eric Anholt673a3942008-07-30 12:06:12 -070065
Chris Wilson17250b72010-10-28 12:51:39 +010066static int i915_gem_inactive_shrink(struct shrinker *shrinker,
67 int nr_to_scan,
68 gfp_t gfp_mask);
69
Chris Wilson31169712009-09-14 16:50:28 +010070
Chris Wilson73aa8082010-09-30 11:46:12 +010071/* some bookkeeping */
72static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
73 size_t size)
74{
75 dev_priv->mm.object_count++;
76 dev_priv->mm.object_memory += size;
77}
78
79static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
80 size_t size)
81{
82 dev_priv->mm.object_count--;
83 dev_priv->mm.object_memory -= size;
84}
85
86static void i915_gem_info_add_gtt(struct drm_i915_private *dev_priv,
Chris Wilsona00b10c2010-09-24 21:15:47 +010087 struct drm_i915_gem_object *obj)
Chris Wilson73aa8082010-09-30 11:46:12 +010088{
89 dev_priv->mm.gtt_count++;
Chris Wilsona00b10c2010-09-24 21:15:47 +010090 dev_priv->mm.gtt_memory += obj->gtt_space->size;
91 if (obj->gtt_offset < dev_priv->mm.gtt_mappable_end) {
Daniel Vetterfb7d5162010-10-01 22:05:20 +020092 dev_priv->mm.mappable_gtt_used +=
Chris Wilsona00b10c2010-09-24 21:15:47 +010093 min_t(size_t, obj->gtt_space->size,
94 dev_priv->mm.gtt_mappable_end - obj->gtt_offset);
Daniel Vetterfb7d5162010-10-01 22:05:20 +020095 }
Daniel Vetter93a37f22010-11-05 20:24:53 +010096 list_add_tail(&obj->gtt_list, &dev_priv->mm.gtt_list);
Chris Wilson73aa8082010-09-30 11:46:12 +010097}
98
99static void i915_gem_info_remove_gtt(struct drm_i915_private *dev_priv,
Chris Wilsona00b10c2010-09-24 21:15:47 +0100100 struct drm_i915_gem_object *obj)
Chris Wilson73aa8082010-09-30 11:46:12 +0100101{
102 dev_priv->mm.gtt_count--;
Chris Wilsona00b10c2010-09-24 21:15:47 +0100103 dev_priv->mm.gtt_memory -= obj->gtt_space->size;
104 if (obj->gtt_offset < dev_priv->mm.gtt_mappable_end) {
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200105 dev_priv->mm.mappable_gtt_used -=
Chris Wilsona00b10c2010-09-24 21:15:47 +0100106 min_t(size_t, obj->gtt_space->size,
107 dev_priv->mm.gtt_mappable_end - obj->gtt_offset);
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200108 }
Daniel Vetter93a37f22010-11-05 20:24:53 +0100109 list_del_init(&obj->gtt_list);
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200110}
111
112/**
113 * Update the mappable working set counters. Call _only_ when there is a change
114 * in one of (pin|fault)_mappable and update *_mappable _before_ calling.
115 * @mappable: new state the changed mappable flag (either pin_ or fault_).
116 */
117static void
118i915_gem_info_update_mappable(struct drm_i915_private *dev_priv,
Chris Wilsona00b10c2010-09-24 21:15:47 +0100119 struct drm_i915_gem_object *obj,
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200120 bool mappable)
121{
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200122 if (mappable) {
Chris Wilsona00b10c2010-09-24 21:15:47 +0100123 if (obj->pin_mappable && obj->fault_mappable)
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200124 /* Combined state was already mappable. */
125 return;
126 dev_priv->mm.gtt_mappable_count++;
Chris Wilsona00b10c2010-09-24 21:15:47 +0100127 dev_priv->mm.gtt_mappable_memory += obj->gtt_space->size;
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200128 } else {
Chris Wilsona00b10c2010-09-24 21:15:47 +0100129 if (obj->pin_mappable || obj->fault_mappable)
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200130 /* Combined state still mappable. */
131 return;
132 dev_priv->mm.gtt_mappable_count--;
Chris Wilsona00b10c2010-09-24 21:15:47 +0100133 dev_priv->mm.gtt_mappable_memory -= obj->gtt_space->size;
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200134 }
Chris Wilson73aa8082010-09-30 11:46:12 +0100135}
136
137static void i915_gem_info_add_pin(struct drm_i915_private *dev_priv,
Chris Wilsona00b10c2010-09-24 21:15:47 +0100138 struct drm_i915_gem_object *obj,
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200139 bool mappable)
Chris Wilson73aa8082010-09-30 11:46:12 +0100140{
141 dev_priv->mm.pin_count++;
Chris Wilsona00b10c2010-09-24 21:15:47 +0100142 dev_priv->mm.pin_memory += obj->gtt_space->size;
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200143 if (mappable) {
Chris Wilsona00b10c2010-09-24 21:15:47 +0100144 obj->pin_mappable = true;
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200145 i915_gem_info_update_mappable(dev_priv, obj, true);
146 }
Chris Wilson73aa8082010-09-30 11:46:12 +0100147}
148
149static void i915_gem_info_remove_pin(struct drm_i915_private *dev_priv,
Chris Wilsona00b10c2010-09-24 21:15:47 +0100150 struct drm_i915_gem_object *obj)
Chris Wilson73aa8082010-09-30 11:46:12 +0100151{
152 dev_priv->mm.pin_count--;
Chris Wilsona00b10c2010-09-24 21:15:47 +0100153 dev_priv->mm.pin_memory -= obj->gtt_space->size;
154 if (obj->pin_mappable) {
155 obj->pin_mappable = false;
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200156 i915_gem_info_update_mappable(dev_priv, obj, false);
157 }
Chris Wilson73aa8082010-09-30 11:46:12 +0100158}
159
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100160int
161i915_gem_check_is_wedged(struct drm_device *dev)
162{
163 struct drm_i915_private *dev_priv = dev->dev_private;
164 struct completion *x = &dev_priv->error_completion;
165 unsigned long flags;
166 int ret;
167
168 if (!atomic_read(&dev_priv->mm.wedged))
169 return 0;
170
171 ret = wait_for_completion_interruptible(x);
172 if (ret)
173 return ret;
174
175 /* Success, we reset the GPU! */
176 if (!atomic_read(&dev_priv->mm.wedged))
177 return 0;
178
179 /* GPU is hung, bump the completion count to account for
180 * the token we just consumed so that we never hit zero and
181 * end up waiting upon a subsequent completion event that
182 * will never happen.
183 */
184 spin_lock_irqsave(&x->wait.lock, flags);
185 x->done++;
186 spin_unlock_irqrestore(&x->wait.lock, flags);
187 return -EIO;
188}
189
Chris Wilson76c1dec2010-09-25 11:22:51 +0100190static int i915_mutex_lock_interruptible(struct drm_device *dev)
191{
192 struct drm_i915_private *dev_priv = dev->dev_private;
193 int ret;
194
195 ret = i915_gem_check_is_wedged(dev);
196 if (ret)
197 return ret;
198
199 ret = mutex_lock_interruptible(&dev->struct_mutex);
200 if (ret)
201 return ret;
202
203 if (atomic_read(&dev_priv->mm.wedged)) {
204 mutex_unlock(&dev->struct_mutex);
205 return -EAGAIN;
206 }
207
Chris Wilson23bc5982010-09-29 16:10:57 +0100208 WARN_ON(i915_verify_lists(dev));
Chris Wilson76c1dec2010-09-25 11:22:51 +0100209 return 0;
210}
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100211
Chris Wilson7d1c4802010-08-07 21:45:03 +0100212static inline bool
Chris Wilson05394f32010-11-08 19:18:58 +0000213i915_gem_object_is_inactive(struct drm_i915_gem_object *obj)
Chris Wilson7d1c4802010-08-07 21:45:03 +0100214{
Chris Wilson05394f32010-11-08 19:18:58 +0000215 return obj->gtt_space && !obj->active && obj->pin_count == 0;
Chris Wilson7d1c4802010-08-07 21:45:03 +0100216}
217
Chris Wilson73aa8082010-09-30 11:46:12 +0100218int i915_gem_do_init(struct drm_device *dev,
219 unsigned long start,
Daniel Vetter53984632010-09-22 23:44:24 +0200220 unsigned long mappable_end,
Jesse Barnes79e53942008-11-07 14:24:08 -0800221 unsigned long end)
222{
223 drm_i915_private_t *dev_priv = dev->dev_private;
224
225 if (start >= end ||
226 (start & (PAGE_SIZE - 1)) != 0 ||
227 (end & (PAGE_SIZE - 1)) != 0) {
228 return -EINVAL;
229 }
230
231 drm_mm_init(&dev_priv->mm.gtt_space, start,
232 end - start);
233
Chris Wilson73aa8082010-09-30 11:46:12 +0100234 dev_priv->mm.gtt_total = end - start;
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200235 dev_priv->mm.mappable_gtt_total = min(end, mappable_end) - start;
Daniel Vetter53984632010-09-22 23:44:24 +0200236 dev_priv->mm.gtt_mappable_end = mappable_end;
Jesse Barnes79e53942008-11-07 14:24:08 -0800237
238 return 0;
239}
Keith Packard6dbe2772008-10-14 21:41:13 -0700240
Eric Anholt673a3942008-07-30 12:06:12 -0700241int
242i915_gem_init_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000243 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700244{
Eric Anholt673a3942008-07-30 12:06:12 -0700245 struct drm_i915_gem_init *args = data;
Jesse Barnes79e53942008-11-07 14:24:08 -0800246 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700247
248 mutex_lock(&dev->struct_mutex);
Daniel Vetter53984632010-09-22 23:44:24 +0200249 ret = i915_gem_do_init(dev, args->gtt_start, args->gtt_end, args->gtt_end);
Eric Anholt673a3942008-07-30 12:06:12 -0700250 mutex_unlock(&dev->struct_mutex);
251
Jesse Barnes79e53942008-11-07 14:24:08 -0800252 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700253}
254
Eric Anholt5a125c32008-10-22 21:40:13 -0700255int
256i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000257 struct drm_file *file)
Eric Anholt5a125c32008-10-22 21:40:13 -0700258{
Chris Wilson73aa8082010-09-30 11:46:12 +0100259 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt5a125c32008-10-22 21:40:13 -0700260 struct drm_i915_gem_get_aperture *args = data;
Eric Anholt5a125c32008-10-22 21:40:13 -0700261
262 if (!(dev->driver->driver_features & DRIVER_GEM))
263 return -ENODEV;
264
Chris Wilson73aa8082010-09-30 11:46:12 +0100265 mutex_lock(&dev->struct_mutex);
266 args->aper_size = dev_priv->mm.gtt_total;
267 args->aper_available_size = args->aper_size - dev_priv->mm.pin_memory;
268 mutex_unlock(&dev->struct_mutex);
Eric Anholt5a125c32008-10-22 21:40:13 -0700269
270 return 0;
271}
272
Eric Anholt673a3942008-07-30 12:06:12 -0700273
274/**
275 * Creates a new mm object and returns a handle to it.
276 */
277int
278i915_gem_create_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000279 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700280{
281 struct drm_i915_gem_create *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +0000282 struct drm_i915_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300283 int ret;
284 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700285
286 args->size = roundup(args->size, PAGE_SIZE);
287
288 /* Allocate the new object */
Daniel Vetterac52bc52010-04-09 19:05:06 +0000289 obj = i915_gem_alloc_object(dev, args->size);
Eric Anholt673a3942008-07-30 12:06:12 -0700290 if (obj == NULL)
291 return -ENOMEM;
292
Chris Wilson05394f32010-11-08 19:18:58 +0000293 ret = drm_gem_handle_create(file, &obj->base, &handle);
Chris Wilson1dfd9752010-09-06 14:44:14 +0100294 if (ret) {
Chris Wilson05394f32010-11-08 19:18:58 +0000295 drm_gem_object_release(&obj->base);
296 i915_gem_info_remove_obj(dev->dev_private, obj->base.size);
Chris Wilson202f2fe2010-10-14 13:20:40 +0100297 kfree(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700298 return ret;
Chris Wilson1dfd9752010-09-06 14:44:14 +0100299 }
300
Chris Wilson202f2fe2010-10-14 13:20:40 +0100301 /* drop reference from allocate - handle holds it now */
Chris Wilson05394f32010-11-08 19:18:58 +0000302 drm_gem_object_unreference(&obj->base);
Chris Wilson202f2fe2010-10-14 13:20:40 +0100303 trace_i915_gem_object_create(obj);
304
Eric Anholt673a3942008-07-30 12:06:12 -0700305 args->handle = handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700306 return 0;
307}
308
Chris Wilson05394f32010-11-08 19:18:58 +0000309static int i915_gem_object_needs_bit17_swizzle(struct drm_i915_gem_object *obj)
Eric Anholt280b7132009-03-12 16:56:27 -0700310{
Chris Wilson05394f32010-11-08 19:18:58 +0000311 drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
Eric Anholt280b7132009-03-12 16:56:27 -0700312
313 return dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_9_10_17 &&
Chris Wilson05394f32010-11-08 19:18:58 +0000314 obj->tiling_mode != I915_TILING_NONE;
Eric Anholt280b7132009-03-12 16:56:27 -0700315}
316
Chris Wilson99a03df2010-05-27 14:15:34 +0100317static inline void
Eric Anholt40123c12009-03-09 13:42:30 -0700318slow_shmem_copy(struct page *dst_page,
319 int dst_offset,
320 struct page *src_page,
321 int src_offset,
322 int length)
323{
324 char *dst_vaddr, *src_vaddr;
325
Chris Wilson99a03df2010-05-27 14:15:34 +0100326 dst_vaddr = kmap(dst_page);
327 src_vaddr = kmap(src_page);
Eric Anholt40123c12009-03-09 13:42:30 -0700328
329 memcpy(dst_vaddr + dst_offset, src_vaddr + src_offset, length);
330
Chris Wilson99a03df2010-05-27 14:15:34 +0100331 kunmap(src_page);
332 kunmap(dst_page);
Eric Anholt40123c12009-03-09 13:42:30 -0700333}
334
Chris Wilson99a03df2010-05-27 14:15:34 +0100335static inline void
Eric Anholt280b7132009-03-12 16:56:27 -0700336slow_shmem_bit17_copy(struct page *gpu_page,
337 int gpu_offset,
338 struct page *cpu_page,
339 int cpu_offset,
340 int length,
341 int is_read)
342{
343 char *gpu_vaddr, *cpu_vaddr;
344
345 /* Use the unswizzled path if this page isn't affected. */
346 if ((page_to_phys(gpu_page) & (1 << 17)) == 0) {
347 if (is_read)
348 return slow_shmem_copy(cpu_page, cpu_offset,
349 gpu_page, gpu_offset, length);
350 else
351 return slow_shmem_copy(gpu_page, gpu_offset,
352 cpu_page, cpu_offset, length);
353 }
354
Chris Wilson99a03df2010-05-27 14:15:34 +0100355 gpu_vaddr = kmap(gpu_page);
356 cpu_vaddr = kmap(cpu_page);
Eric Anholt280b7132009-03-12 16:56:27 -0700357
358 /* Copy the data, XORing A6 with A17 (1). The user already knows he's
359 * XORing with the other bits (A9 for Y, A9 and A10 for X)
360 */
361 while (length > 0) {
362 int cacheline_end = ALIGN(gpu_offset + 1, 64);
363 int this_length = min(cacheline_end - gpu_offset, length);
364 int swizzled_gpu_offset = gpu_offset ^ 64;
365
366 if (is_read) {
367 memcpy(cpu_vaddr + cpu_offset,
368 gpu_vaddr + swizzled_gpu_offset,
369 this_length);
370 } else {
371 memcpy(gpu_vaddr + swizzled_gpu_offset,
372 cpu_vaddr + cpu_offset,
373 this_length);
374 }
375 cpu_offset += this_length;
376 gpu_offset += this_length;
377 length -= this_length;
378 }
379
Chris Wilson99a03df2010-05-27 14:15:34 +0100380 kunmap(cpu_page);
381 kunmap(gpu_page);
Eric Anholt280b7132009-03-12 16:56:27 -0700382}
383
Eric Anholt673a3942008-07-30 12:06:12 -0700384/**
Eric Anholteb014592009-03-10 11:44:52 -0700385 * This is the fast shmem pread path, which attempts to copy_from_user directly
386 * from the backing pages of the object to the user's address space. On a
387 * fault, it fails so we can fall back to i915_gem_shmem_pwrite_slow().
388 */
389static int
Chris Wilson05394f32010-11-08 19:18:58 +0000390i915_gem_shmem_pread_fast(struct drm_device *dev,
391 struct drm_i915_gem_object *obj,
Eric Anholteb014592009-03-10 11:44:52 -0700392 struct drm_i915_gem_pread *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000393 struct drm_file *file)
Eric Anholteb014592009-03-10 11:44:52 -0700394{
Chris Wilson05394f32010-11-08 19:18:58 +0000395 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Eric Anholteb014592009-03-10 11:44:52 -0700396 ssize_t remain;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100397 loff_t offset;
Eric Anholteb014592009-03-10 11:44:52 -0700398 char __user *user_data;
399 int page_offset, page_length;
Eric Anholteb014592009-03-10 11:44:52 -0700400
401 user_data = (char __user *) (uintptr_t) args->data_ptr;
402 remain = args->size;
403
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
Chris Wilson05394f32010-11-08 19:18:58 +0000452i915_gem_shmem_pread_slow(struct drm_device *dev,
453 struct drm_i915_gem_object *obj,
Eric Anholteb014592009-03-10 11:44:52 -0700454 struct drm_i915_gem_pread *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000455 struct drm_file *file)
Eric Anholteb014592009-03-10 11:44:52 -0700456{
Chris Wilson05394f32010-11-08 19:18:58 +0000457 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
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
Eric Anholteb014592009-03-10 11:44:52 -0700503 offset = args->offset;
504
505 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100506 struct page *page;
507
Eric Anholteb014592009-03-10 11:44:52 -0700508 /* Operation in this page
509 *
Eric Anholteb014592009-03-10 11:44:52 -0700510 * shmem_page_offset = offset within page in shmem file
511 * data_page_index = page number in get_user_pages return
512 * data_page_offset = offset with data_page_index page.
513 * page_length = bytes to copy for this page
514 */
Eric Anholteb014592009-03-10 11:44:52 -0700515 shmem_page_offset = offset & ~PAGE_MASK;
516 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
517 data_page_offset = data_ptr & ~PAGE_MASK;
518
519 page_length = remain;
520 if ((shmem_page_offset + page_length) > PAGE_SIZE)
521 page_length = PAGE_SIZE - shmem_page_offset;
522 if ((data_page_offset + page_length) > PAGE_SIZE)
523 page_length = PAGE_SIZE - data_page_offset;
524
Chris Wilsone5281cc2010-10-28 13:45:36 +0100525 page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
526 GFP_HIGHUSER | __GFP_RECLAIMABLE);
527 if (IS_ERR(page))
528 return PTR_ERR(page);
529
Eric Anholt280b7132009-03-12 16:56:27 -0700530 if (do_bit17_swizzling) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100531 slow_shmem_bit17_copy(page,
Eric Anholt280b7132009-03-12 16:56:27 -0700532 shmem_page_offset,
Chris Wilson99a03df2010-05-27 14:15:34 +0100533 user_pages[data_page_index],
534 data_page_offset,
535 page_length,
536 1);
537 } else {
538 slow_shmem_copy(user_pages[data_page_index],
539 data_page_offset,
Chris Wilsone5281cc2010-10-28 13:45:36 +0100540 page,
Chris Wilson99a03df2010-05-27 14:15:34 +0100541 shmem_page_offset,
542 page_length);
Eric Anholt280b7132009-03-12 16:56:27 -0700543 }
Eric Anholteb014592009-03-10 11:44:52 -0700544
Chris Wilsone5281cc2010-10-28 13:45:36 +0100545 mark_page_accessed(page);
546 page_cache_release(page);
547
Eric Anholteb014592009-03-10 11:44:52 -0700548 remain -= page_length;
549 data_ptr += page_length;
550 offset += page_length;
551 }
552
Chris Wilson4f27b752010-10-14 15:26:45 +0100553out:
Eric Anholteb014592009-03-10 11:44:52 -0700554 for (i = 0; i < pinned_pages; i++) {
555 SetPageDirty(user_pages[i]);
Chris Wilsone5281cc2010-10-28 13:45:36 +0100556 mark_page_accessed(user_pages[i]);
Eric Anholteb014592009-03-10 11:44:52 -0700557 page_cache_release(user_pages[i]);
558 }
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700559 drm_free_large(user_pages);
Eric Anholteb014592009-03-10 11:44:52 -0700560
561 return ret;
562}
563
Eric Anholt673a3942008-07-30 12:06:12 -0700564/**
565 * Reads data from the object referenced by handle.
566 *
567 * On error, the contents of *data are undefined.
568 */
569int
570i915_gem_pread_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000571 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700572{
573 struct drm_i915_gem_pread *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +0000574 struct drm_i915_gem_object *obj;
Chris Wilson35b62a82010-09-26 20:23:38 +0100575 int ret = 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700576
Chris Wilson51311d02010-11-17 09:10:42 +0000577 if (args->size == 0)
578 return 0;
579
580 if (!access_ok(VERIFY_WRITE,
581 (char __user *)(uintptr_t)args->data_ptr,
582 args->size))
583 return -EFAULT;
584
585 ret = fault_in_pages_writeable((char __user *)(uintptr_t)args->data_ptr,
586 args->size);
587 if (ret)
588 return -EFAULT;
589
Chris Wilson4f27b752010-10-14 15:26:45 +0100590 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100591 if (ret)
Chris Wilson4f27b752010-10-14 15:26:45 +0100592 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700593
Chris Wilson05394f32010-11-08 19:18:58 +0000594 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100595 if (obj == NULL) {
596 ret = -ENOENT;
597 goto unlock;
Chris Wilson4f27b752010-10-14 15:26:45 +0100598 }
Eric Anholt673a3942008-07-30 12:06:12 -0700599
Chris Wilson7dcd2492010-09-26 20:21:44 +0100600 /* Bounds check source. */
Chris Wilson05394f32010-11-08 19:18:58 +0000601 if (args->offset > obj->base.size ||
602 args->size > obj->base.size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +0100603 ret = -EINVAL;
Chris Wilson35b62a82010-09-26 20:23:38 +0100604 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +0100605 }
606
Chris Wilson4f27b752010-10-14 15:26:45 +0100607 ret = i915_gem_object_set_cpu_read_domain_range(obj,
608 args->offset,
609 args->size);
610 if (ret)
Chris Wilsone5281cc2010-10-28 13:45:36 +0100611 goto out;
Chris Wilson4f27b752010-10-14 15:26:45 +0100612
613 ret = -EFAULT;
614 if (!i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilson05394f32010-11-08 19:18:58 +0000615 ret = i915_gem_shmem_pread_fast(dev, obj, args, file);
Chris Wilson4f27b752010-10-14 15:26:45 +0100616 if (ret == -EFAULT)
Chris Wilson05394f32010-11-08 19:18:58 +0000617 ret = i915_gem_shmem_pread_slow(dev, obj, args, file);
Eric Anholt673a3942008-07-30 12:06:12 -0700618
Chris Wilson35b62a82010-09-26 20:23:38 +0100619out:
Chris Wilson05394f32010-11-08 19:18:58 +0000620 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100621unlock:
Chris Wilson4f27b752010-10-14 15:26:45 +0100622 mutex_unlock(&dev->struct_mutex);
Eric Anholteb014592009-03-10 11:44:52 -0700623 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700624}
625
Keith Packard0839ccb2008-10-30 19:38:48 -0700626/* This is the fast write path which cannot handle
627 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700628 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700629
Keith Packard0839ccb2008-10-30 19:38:48 -0700630static inline int
631fast_user_write(struct io_mapping *mapping,
632 loff_t page_base, int page_offset,
633 char __user *user_data,
634 int length)
635{
636 char *vaddr_atomic;
637 unsigned long unwritten;
638
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700639 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
Keith Packard0839ccb2008-10-30 19:38:48 -0700640 unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
641 user_data, length);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700642 io_mapping_unmap_atomic(vaddr_atomic);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100643 return unwritten;
Keith Packard0839ccb2008-10-30 19:38:48 -0700644}
645
646/* Here's the write path which can sleep for
647 * page faults
648 */
649
Chris Wilsonab34c222010-05-27 14:15:35 +0100650static inline void
Eric Anholt3de09aa2009-03-09 09:42:23 -0700651slow_kernel_write(struct io_mapping *mapping,
652 loff_t gtt_base, int gtt_offset,
653 struct page *user_page, int user_offset,
654 int length)
Keith Packard0839ccb2008-10-30 19:38:48 -0700655{
Chris Wilsonab34c222010-05-27 14:15:35 +0100656 char __iomem *dst_vaddr;
657 char *src_vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -0700658
Chris Wilsonab34c222010-05-27 14:15:35 +0100659 dst_vaddr = io_mapping_map_wc(mapping, gtt_base);
660 src_vaddr = kmap(user_page);
661
662 memcpy_toio(dst_vaddr + gtt_offset,
663 src_vaddr + user_offset,
664 length);
665
666 kunmap(user_page);
667 io_mapping_unmap(dst_vaddr);
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700668}
669
Eric Anholt3de09aa2009-03-09 09:42:23 -0700670/**
671 * This is the fast pwrite path, where we copy the data directly from the
672 * user into the GTT, uncached.
673 */
Eric Anholt673a3942008-07-30 12:06:12 -0700674static int
Chris Wilson05394f32010-11-08 19:18:58 +0000675i915_gem_gtt_pwrite_fast(struct drm_device *dev,
676 struct drm_i915_gem_object *obj,
Eric Anholt3de09aa2009-03-09 09:42:23 -0700677 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000678 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700679{
Keith Packard0839ccb2008-10-30 19:38:48 -0700680 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700681 ssize_t remain;
Keith Packard0839ccb2008-10-30 19:38:48 -0700682 loff_t offset, page_base;
Eric Anholt673a3942008-07-30 12:06:12 -0700683 char __user *user_data;
Keith Packard0839ccb2008-10-30 19:38:48 -0700684 int page_offset, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700685
686 user_data = (char __user *) (uintptr_t) args->data_ptr;
687 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700688
Chris Wilson05394f32010-11-08 19:18:58 +0000689 offset = obj->gtt_offset + args->offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700690
691 while (remain > 0) {
692 /* Operation in this page
693 *
Keith Packard0839ccb2008-10-30 19:38:48 -0700694 * page_base = page offset within aperture
695 * page_offset = offset within page
696 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -0700697 */
Keith Packard0839ccb2008-10-30 19:38:48 -0700698 page_base = (offset & ~(PAGE_SIZE-1));
699 page_offset = offset & (PAGE_SIZE-1);
700 page_length = remain;
701 if ((page_offset + remain) > PAGE_SIZE)
702 page_length = PAGE_SIZE - page_offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700703
Keith Packard0839ccb2008-10-30 19:38:48 -0700704 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -0700705 * source page isn't available. Return the error and we'll
706 * retry in the slow path.
Keith Packard0839ccb2008-10-30 19:38:48 -0700707 */
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100708 if (fast_user_write(dev_priv->mm.gtt_mapping, page_base,
709 page_offset, user_data, page_length))
710
711 return -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -0700712
Keith Packard0839ccb2008-10-30 19:38:48 -0700713 remain -= page_length;
714 user_data += page_length;
715 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700716 }
Eric Anholt673a3942008-07-30 12:06:12 -0700717
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100718 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700719}
720
Eric Anholt3de09aa2009-03-09 09:42:23 -0700721/**
722 * This is the fallback GTT pwrite path, which uses get_user_pages to pin
723 * the memory and maps it using kmap_atomic for copying.
724 *
725 * This code resulted in x11perf -rgb10text consuming about 10% more CPU
726 * than using i915_gem_gtt_pwrite_fast on a G45 (32-bit).
727 */
Eric Anholt3043c602008-10-02 12:24:47 -0700728static int
Chris Wilson05394f32010-11-08 19:18:58 +0000729i915_gem_gtt_pwrite_slow(struct drm_device *dev,
730 struct drm_i915_gem_object *obj,
Eric Anholt3de09aa2009-03-09 09:42:23 -0700731 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000732 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700733{
Eric Anholt3de09aa2009-03-09 09:42:23 -0700734 drm_i915_private_t *dev_priv = dev->dev_private;
735 ssize_t remain;
736 loff_t gtt_page_base, offset;
737 loff_t first_data_page, last_data_page, num_pages;
738 loff_t pinned_pages, i;
739 struct page **user_pages;
740 struct mm_struct *mm = current->mm;
741 int gtt_page_offset, data_page_offset, data_page_index, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700742 int ret;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700743 uint64_t data_ptr = args->data_ptr;
744
745 remain = args->size;
746
747 /* Pin the user pages containing the data. We can't fault while
748 * holding the struct mutex, and all of the pwrite implementations
749 * want to hold it while dereferencing the user data.
750 */
751 first_data_page = data_ptr / PAGE_SIZE;
752 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
753 num_pages = last_data_page - first_data_page + 1;
754
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100755 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
Eric Anholt3de09aa2009-03-09 09:42:23 -0700756 if (user_pages == NULL)
757 return -ENOMEM;
758
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100759 mutex_unlock(&dev->struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700760 down_read(&mm->mmap_sem);
761 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
762 num_pages, 0, 0, user_pages, NULL);
763 up_read(&mm->mmap_sem);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100764 mutex_lock(&dev->struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700765 if (pinned_pages < num_pages) {
766 ret = -EFAULT;
767 goto out_unpin_pages;
768 }
769
Eric Anholt3de09aa2009-03-09 09:42:23 -0700770 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
771 if (ret)
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100772 goto out_unpin_pages;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700773
Chris Wilson05394f32010-11-08 19:18:58 +0000774 offset = obj->gtt_offset + args->offset;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700775
776 while (remain > 0) {
777 /* Operation in this page
778 *
779 * gtt_page_base = page offset within aperture
780 * gtt_page_offset = offset within page in aperture
781 * data_page_index = page number in get_user_pages return
782 * data_page_offset = offset with data_page_index page.
783 * page_length = bytes to copy for this page
784 */
785 gtt_page_base = offset & PAGE_MASK;
786 gtt_page_offset = offset & ~PAGE_MASK;
787 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
788 data_page_offset = data_ptr & ~PAGE_MASK;
789
790 page_length = remain;
791 if ((gtt_page_offset + page_length) > PAGE_SIZE)
792 page_length = PAGE_SIZE - gtt_page_offset;
793 if ((data_page_offset + page_length) > PAGE_SIZE)
794 page_length = PAGE_SIZE - data_page_offset;
795
Chris Wilsonab34c222010-05-27 14:15:35 +0100796 slow_kernel_write(dev_priv->mm.gtt_mapping,
797 gtt_page_base, gtt_page_offset,
798 user_pages[data_page_index],
799 data_page_offset,
800 page_length);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700801
802 remain -= page_length;
803 offset += page_length;
804 data_ptr += page_length;
805 }
806
Eric Anholt3de09aa2009-03-09 09:42:23 -0700807out_unpin_pages:
808 for (i = 0; i < pinned_pages; i++)
809 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700810 drm_free_large(user_pages);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700811
812 return ret;
813}
814
Eric Anholt40123c12009-03-09 13:42:30 -0700815/**
816 * This is the fast shmem pwrite path, which attempts to directly
817 * copy_from_user into the kmapped pages backing the object.
818 */
Eric Anholt673a3942008-07-30 12:06:12 -0700819static int
Chris Wilson05394f32010-11-08 19:18:58 +0000820i915_gem_shmem_pwrite_fast(struct drm_device *dev,
821 struct drm_i915_gem_object *obj,
Eric Anholt40123c12009-03-09 13:42:30 -0700822 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000823 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700824{
Chris Wilson05394f32010-11-08 19:18:58 +0000825 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Eric Anholt40123c12009-03-09 13:42:30 -0700826 ssize_t remain;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100827 loff_t offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700828 char __user *user_data;
829 int page_offset, page_length;
Eric Anholt40123c12009-03-09 13:42:30 -0700830
831 user_data = (char __user *) (uintptr_t) args->data_ptr;
832 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700833
Eric Anholt673a3942008-07-30 12:06:12 -0700834 offset = args->offset;
Chris Wilson05394f32010-11-08 19:18:58 +0000835 obj->dirty = 1;
Eric Anholt673a3942008-07-30 12:06:12 -0700836
Eric Anholt40123c12009-03-09 13:42:30 -0700837 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100838 struct page *page;
839 char *vaddr;
840 int ret;
841
Eric Anholt40123c12009-03-09 13:42:30 -0700842 /* Operation in this page
843 *
Eric Anholt40123c12009-03-09 13:42:30 -0700844 * page_offset = offset within page
845 * page_length = bytes to copy for this page
846 */
Eric Anholt40123c12009-03-09 13:42:30 -0700847 page_offset = offset & (PAGE_SIZE-1);
848 page_length = remain;
849 if ((page_offset + remain) > PAGE_SIZE)
850 page_length = PAGE_SIZE - page_offset;
851
Chris Wilsone5281cc2010-10-28 13:45:36 +0100852 page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
853 GFP_HIGHUSER | __GFP_RECLAIMABLE);
854 if (IS_ERR(page))
855 return PTR_ERR(page);
856
857 vaddr = kmap_atomic(page, KM_USER0);
858 ret = __copy_from_user_inatomic(vaddr + page_offset,
859 user_data,
860 page_length);
861 kunmap_atomic(vaddr, KM_USER0);
862
863 set_page_dirty(page);
864 mark_page_accessed(page);
865 page_cache_release(page);
866
867 /* If we get a fault while copying data, then (presumably) our
868 * source page isn't available. Return the error and we'll
869 * retry in the slow path.
870 */
871 if (ret)
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100872 return -EFAULT;
Eric Anholt40123c12009-03-09 13:42:30 -0700873
874 remain -= page_length;
875 user_data += page_length;
876 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700877 }
878
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100879 return 0;
Eric Anholt40123c12009-03-09 13:42:30 -0700880}
881
882/**
883 * This is the fallback shmem pwrite path, which uses get_user_pages to pin
884 * the memory and maps it using kmap_atomic for copying.
885 *
886 * This avoids taking mmap_sem for faulting on the user's address while the
887 * struct_mutex is held.
888 */
889static int
Chris Wilson05394f32010-11-08 19:18:58 +0000890i915_gem_shmem_pwrite_slow(struct drm_device *dev,
891 struct drm_i915_gem_object *obj,
Eric Anholt40123c12009-03-09 13:42:30 -0700892 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000893 struct drm_file *file)
Eric Anholt40123c12009-03-09 13:42:30 -0700894{
Chris Wilson05394f32010-11-08 19:18:58 +0000895 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Eric Anholt40123c12009-03-09 13:42:30 -0700896 struct mm_struct *mm = current->mm;
897 struct page **user_pages;
898 ssize_t remain;
899 loff_t offset, pinned_pages, i;
900 loff_t first_data_page, last_data_page, num_pages;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100901 int shmem_page_offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700902 int data_page_index, data_page_offset;
903 int page_length;
904 int ret;
905 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700906 int do_bit17_swizzling;
Eric Anholt40123c12009-03-09 13:42:30 -0700907
908 remain = args->size;
909
910 /* Pin the user pages containing the data. We can't fault while
911 * holding the struct mutex, and all of the pwrite implementations
912 * want to hold it while dereferencing the user data.
913 */
914 first_data_page = data_ptr / PAGE_SIZE;
915 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
916 num_pages = last_data_page - first_data_page + 1;
917
Chris Wilson4f27b752010-10-14 15:26:45 +0100918 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
Eric Anholt40123c12009-03-09 13:42:30 -0700919 if (user_pages == NULL)
920 return -ENOMEM;
921
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100922 mutex_unlock(&dev->struct_mutex);
Eric Anholt40123c12009-03-09 13:42:30 -0700923 down_read(&mm->mmap_sem);
924 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
925 num_pages, 0, 0, user_pages, NULL);
926 up_read(&mm->mmap_sem);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100927 mutex_lock(&dev->struct_mutex);
Eric Anholt40123c12009-03-09 13:42:30 -0700928 if (pinned_pages < num_pages) {
929 ret = -EFAULT;
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100930 goto out;
Eric Anholt40123c12009-03-09 13:42:30 -0700931 }
932
Eric Anholt40123c12009-03-09 13:42:30 -0700933 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100934 if (ret)
935 goto out;
936
937 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700938
Eric Anholt40123c12009-03-09 13:42:30 -0700939 offset = args->offset;
Chris Wilson05394f32010-11-08 19:18:58 +0000940 obj->dirty = 1;
Eric Anholt40123c12009-03-09 13:42:30 -0700941
942 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100943 struct page *page;
944
Eric Anholt40123c12009-03-09 13:42:30 -0700945 /* Operation in this page
946 *
Eric Anholt40123c12009-03-09 13:42:30 -0700947 * shmem_page_offset = offset within page in shmem file
948 * data_page_index = page number in get_user_pages return
949 * data_page_offset = offset with data_page_index page.
950 * page_length = bytes to copy for this page
951 */
Eric Anholt40123c12009-03-09 13:42:30 -0700952 shmem_page_offset = offset & ~PAGE_MASK;
953 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
954 data_page_offset = data_ptr & ~PAGE_MASK;
955
956 page_length = remain;
957 if ((shmem_page_offset + page_length) > PAGE_SIZE)
958 page_length = PAGE_SIZE - shmem_page_offset;
959 if ((data_page_offset + page_length) > PAGE_SIZE)
960 page_length = PAGE_SIZE - data_page_offset;
961
Chris Wilsone5281cc2010-10-28 13:45:36 +0100962 page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
963 GFP_HIGHUSER | __GFP_RECLAIMABLE);
964 if (IS_ERR(page)) {
965 ret = PTR_ERR(page);
966 goto out;
967 }
968
Eric Anholt280b7132009-03-12 16:56:27 -0700969 if (do_bit17_swizzling) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100970 slow_shmem_bit17_copy(page,
Eric Anholt280b7132009-03-12 16:56:27 -0700971 shmem_page_offset,
972 user_pages[data_page_index],
973 data_page_offset,
Chris Wilson99a03df2010-05-27 14:15:34 +0100974 page_length,
975 0);
976 } else {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100977 slow_shmem_copy(page,
Chris Wilson99a03df2010-05-27 14:15:34 +0100978 shmem_page_offset,
979 user_pages[data_page_index],
980 data_page_offset,
981 page_length);
Eric Anholt280b7132009-03-12 16:56:27 -0700982 }
Eric Anholt40123c12009-03-09 13:42:30 -0700983
Chris Wilsone5281cc2010-10-28 13:45:36 +0100984 set_page_dirty(page);
985 mark_page_accessed(page);
986 page_cache_release(page);
987
Eric Anholt40123c12009-03-09 13:42:30 -0700988 remain -= page_length;
989 data_ptr += page_length;
990 offset += page_length;
991 }
992
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100993out:
Eric Anholt40123c12009-03-09 13:42:30 -0700994 for (i = 0; i < pinned_pages; i++)
995 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700996 drm_free_large(user_pages);
Eric Anholt40123c12009-03-09 13:42:30 -0700997
998 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700999}
1000
1001/**
1002 * Writes data to the object referenced by handle.
1003 *
1004 * On error, the contents of the buffer that were to be modified are undefined.
1005 */
1006int
1007i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001008 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001009{
1010 struct drm_i915_gem_pwrite *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001011 struct drm_i915_gem_object *obj;
Chris Wilson51311d02010-11-17 09:10:42 +00001012 int ret;
1013
1014 if (args->size == 0)
1015 return 0;
1016
1017 if (!access_ok(VERIFY_READ,
1018 (char __user *)(uintptr_t)args->data_ptr,
1019 args->size))
1020 return -EFAULT;
1021
1022 ret = fault_in_pages_readable((char __user *)(uintptr_t)args->data_ptr,
1023 args->size);
1024 if (ret)
1025 return -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -07001026
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001027 ret = i915_mutex_lock_interruptible(dev);
1028 if (ret)
1029 return ret;
1030
Chris Wilson05394f32010-11-08 19:18:58 +00001031 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001032 if (obj == NULL) {
1033 ret = -ENOENT;
1034 goto unlock;
1035 }
Eric Anholt673a3942008-07-30 12:06:12 -07001036
Chris Wilson7dcd2492010-09-26 20:21:44 +01001037 /* Bounds check destination. */
Chris Wilson05394f32010-11-08 19:18:58 +00001038 if (args->offset > obj->base.size ||
1039 args->size > obj->base.size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001040 ret = -EINVAL;
Chris Wilson35b62a82010-09-26 20:23:38 +01001041 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001042 }
1043
Eric Anholt673a3942008-07-30 12:06:12 -07001044 /* We can only do the GTT pwrite on untiled buffers, as otherwise
1045 * it would end up going through the fenced access, and we'll get
1046 * different detiling behavior between reading and writing.
1047 * pread/pwrite currently are reading and writing from the CPU
1048 * perspective, requiring manual detiling by the client.
1049 */
Chris Wilson05394f32010-11-08 19:18:58 +00001050 if (obj->phys_obj)
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001051 ret = i915_gem_phys_pwrite(dev, obj, args, file);
Chris Wilson05394f32010-11-08 19:18:58 +00001052 else if (obj->tiling_mode == I915_TILING_NONE &&
1053 obj->gtt_space &&
1054 obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01001055 ret = i915_gem_object_pin(obj, 0, true);
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001056 if (ret)
1057 goto out;
1058
1059 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
1060 if (ret)
1061 goto out_unpin;
1062
1063 ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file);
1064 if (ret == -EFAULT)
1065 ret = i915_gem_gtt_pwrite_slow(dev, obj, args, file);
1066
1067out_unpin:
1068 i915_gem_object_unpin(obj);
Eric Anholt40123c12009-03-09 13:42:30 -07001069 } else {
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001070 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
1071 if (ret)
Chris Wilsone5281cc2010-10-28 13:45:36 +01001072 goto out;
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001073
1074 ret = -EFAULT;
1075 if (!i915_gem_object_needs_bit17_swizzle(obj))
1076 ret = i915_gem_shmem_pwrite_fast(dev, obj, args, file);
1077 if (ret == -EFAULT)
1078 ret = i915_gem_shmem_pwrite_slow(dev, obj, args, file);
Eric Anholt40123c12009-03-09 13:42:30 -07001079 }
Eric Anholt673a3942008-07-30 12:06:12 -07001080
Chris Wilson35b62a82010-09-26 20:23:38 +01001081out:
Chris Wilson05394f32010-11-08 19:18:58 +00001082 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001083unlock:
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001084 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07001085 return ret;
1086}
1087
1088/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001089 * Called when user space prepares to use an object with the CPU, either
1090 * through the mmap ioctl's mapping or a GTT mapping.
Eric Anholt673a3942008-07-30 12:06:12 -07001091 */
1092int
1093i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001094 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001095{
Eric Anholta09ba7f2009-08-29 12:49:51 -07001096 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07001097 struct drm_i915_gem_set_domain *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001098 struct drm_i915_gem_object *obj;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001099 uint32_t read_domains = args->read_domains;
1100 uint32_t write_domain = args->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07001101 int ret;
1102
1103 if (!(dev->driver->driver_features & DRIVER_GEM))
1104 return -ENODEV;
1105
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001106 /* Only handle setting domains to types used by the CPU. */
Chris Wilson21d509e2009-06-06 09:46:02 +01001107 if (write_domain & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001108 return -EINVAL;
1109
Chris Wilson21d509e2009-06-06 09:46:02 +01001110 if (read_domains & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001111 return -EINVAL;
1112
1113 /* Having something in the write domain implies it's in the read
1114 * domain, and only that read domain. Enforce that in the request.
1115 */
1116 if (write_domain != 0 && read_domains != write_domain)
1117 return -EINVAL;
1118
Chris Wilson76c1dec2010-09-25 11:22:51 +01001119 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001120 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001121 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001122
Chris Wilson05394f32010-11-08 19:18:58 +00001123 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001124 if (obj == NULL) {
1125 ret = -ENOENT;
1126 goto unlock;
Chris Wilson76c1dec2010-09-25 11:22:51 +01001127 }
Jesse Barnes652c3932009-08-17 13:31:43 -07001128
1129 intel_mark_busy(dev, obj);
1130
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001131 if (read_domains & I915_GEM_DOMAIN_GTT) {
1132 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
Eric Anholt02354392008-11-26 13:58:13 -08001133
Eric Anholta09ba7f2009-08-29 12:49:51 -07001134 /* Update the LRU on the fence for the CPU access that's
1135 * about to occur.
1136 */
Chris Wilson05394f32010-11-08 19:18:58 +00001137 if (obj->fence_reg != I915_FENCE_REG_NONE) {
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001138 struct drm_i915_fence_reg *reg =
Chris Wilson05394f32010-11-08 19:18:58 +00001139 &dev_priv->fence_regs[obj->fence_reg];
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001140 list_move_tail(&reg->lru_list,
Eric Anholta09ba7f2009-08-29 12:49:51 -07001141 &dev_priv->mm.fence_list);
1142 }
1143
Eric Anholt02354392008-11-26 13:58:13 -08001144 /* Silently promote "you're not bound, there was nothing to do"
1145 * to success, since the client was just asking us to
1146 * make sure everything was done.
1147 */
1148 if (ret == -EINVAL)
1149 ret = 0;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001150 } else {
Eric Anholte47c68e2008-11-14 13:35:19 -08001151 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001152 }
1153
Chris Wilson7d1c4802010-08-07 21:45:03 +01001154 /* Maintain LRU order of "inactive" objects */
Chris Wilson05394f32010-11-08 19:18:58 +00001155 if (ret == 0 && i915_gem_object_is_inactive(obj))
1156 list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
Chris Wilson7d1c4802010-08-07 21:45:03 +01001157
Chris Wilson05394f32010-11-08 19:18:58 +00001158 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001159unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07001160 mutex_unlock(&dev->struct_mutex);
1161 return ret;
1162}
1163
1164/**
1165 * Called when user space has done writes to this buffer
1166 */
1167int
1168i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001169 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001170{
1171 struct drm_i915_gem_sw_finish *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001172 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001173 int ret = 0;
1174
1175 if (!(dev->driver->driver_features & DRIVER_GEM))
1176 return -ENODEV;
1177
Chris Wilson76c1dec2010-09-25 11:22:51 +01001178 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001179 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001180 return ret;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001181
Chris Wilson05394f32010-11-08 19:18:58 +00001182 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Eric Anholt673a3942008-07-30 12:06:12 -07001183 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001184 ret = -ENOENT;
1185 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07001186 }
1187
Eric Anholt673a3942008-07-30 12:06:12 -07001188 /* Pinned buffers may be scanout, so flush the cache */
Chris Wilson05394f32010-11-08 19:18:58 +00001189 if (obj->pin_count)
Eric Anholte47c68e2008-11-14 13:35:19 -08001190 i915_gem_object_flush_cpu_write_domain(obj);
1191
Chris Wilson05394f32010-11-08 19:18:58 +00001192 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001193unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07001194 mutex_unlock(&dev->struct_mutex);
1195 return ret;
1196}
1197
1198/**
1199 * Maps the contents of an object, returning the address it is mapped
1200 * into.
1201 *
1202 * While the mapping holds a reference on the contents of the object, it doesn't
1203 * imply a ref on the object itself.
1204 */
1205int
1206i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001207 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001208{
Chris Wilsonda761a62010-10-27 17:37:08 +01001209 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07001210 struct drm_i915_gem_mmap *args = data;
1211 struct drm_gem_object *obj;
1212 loff_t offset;
1213 unsigned long addr;
1214
1215 if (!(dev->driver->driver_features & DRIVER_GEM))
1216 return -ENODEV;
1217
Chris Wilson05394f32010-11-08 19:18:58 +00001218 obj = drm_gem_object_lookup(dev, file, args->handle);
Eric Anholt673a3942008-07-30 12:06:12 -07001219 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001220 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001221
Chris Wilsonda761a62010-10-27 17:37:08 +01001222 if (obj->size > dev_priv->mm.gtt_mappable_end) {
1223 drm_gem_object_unreference_unlocked(obj);
1224 return -E2BIG;
1225 }
1226
Eric Anholt673a3942008-07-30 12:06:12 -07001227 offset = args->offset;
1228
1229 down_write(&current->mm->mmap_sem);
1230 addr = do_mmap(obj->filp, 0, args->size,
1231 PROT_READ | PROT_WRITE, MAP_SHARED,
1232 args->offset);
1233 up_write(&current->mm->mmap_sem);
Luca Barbieribc9025b2010-02-09 05:49:12 +00001234 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001235 if (IS_ERR((void *)addr))
1236 return addr;
1237
1238 args->addr_ptr = (uint64_t) addr;
1239
1240 return 0;
1241}
1242
Jesse Barnesde151cf2008-11-12 10:03:55 -08001243/**
1244 * i915_gem_fault - fault a page into the GTT
1245 * vma: VMA in question
1246 * vmf: fault info
1247 *
1248 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1249 * from userspace. The fault handler takes care of binding the object to
1250 * the GTT (if needed), allocating and programming a fence register (again,
1251 * only if needed based on whether the old reg is still valid or the object
1252 * is tiled) and inserting a new PTE into the faulting process.
1253 *
1254 * Note that the faulting process may involve evicting existing objects
1255 * from the GTT and/or fence registers to make room. So performance may
1256 * suffer if the GTT working set is large or there are few fence registers
1257 * left.
1258 */
1259int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1260{
Chris Wilson05394f32010-11-08 19:18:58 +00001261 struct drm_i915_gem_object *obj = to_intel_bo(vma->vm_private_data);
1262 struct drm_device *dev = obj->base.dev;
Chris Wilson7d1c4802010-08-07 21:45:03 +01001263 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001264 pgoff_t page_offset;
1265 unsigned long pfn;
1266 int ret = 0;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001267 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001268
1269 /* We don't use vmf->pgoff since that has the fake offset */
1270 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1271 PAGE_SHIFT;
1272
1273 /* Now bind it into the GTT if needed */
1274 mutex_lock(&dev->struct_mutex);
Chris Wilson05394f32010-11-08 19:18:58 +00001275 BUG_ON(obj->pin_count && !obj->pin_mappable);
Chris Wilsona00b10c2010-09-24 21:15:47 +01001276
Chris Wilson919926a2010-11-12 13:42:53 +00001277 if (!obj->map_and_fenceable) {
1278 ret = i915_gem_object_unbind(obj);
1279 if (ret)
1280 goto unlock;
Chris Wilsona00b10c2010-09-24 21:15:47 +01001281 }
Daniel Vetter16e809a2010-09-16 19:37:04 +02001282
Chris Wilson05394f32010-11-08 19:18:58 +00001283 if (!obj->gtt_space) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01001284 ret = i915_gem_object_bind_to_gtt(obj, 0, true);
Chris Wilsonc7150892009-09-23 00:43:56 +01001285 if (ret)
1286 goto unlock;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001287 }
1288
Chris Wilson4a684a42010-10-28 14:44:08 +01001289 ret = i915_gem_object_set_to_gtt_domain(obj, write);
1290 if (ret)
1291 goto unlock;
1292
Chris Wilson05394f32010-11-08 19:18:58 +00001293 if (!obj->fault_mappable) {
1294 obj->fault_mappable = true;
1295 i915_gem_info_update_mappable(dev_priv, obj, true);
Daniel Vetterfb7d5162010-10-01 22:05:20 +02001296 }
1297
Jesse Barnesde151cf2008-11-12 10:03:55 -08001298 /* Need a new fence register? */
Chris Wilson05394f32010-11-08 19:18:58 +00001299 if (obj->tiling_mode != I915_TILING_NONE) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01001300 ret = i915_gem_object_get_fence_reg(obj, true);
Chris Wilsonc7150892009-09-23 00:43:56 +01001301 if (ret)
1302 goto unlock;
Eric Anholtd9ddcb92009-01-27 10:33:49 -08001303 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001304
Chris Wilson05394f32010-11-08 19:18:58 +00001305 if (i915_gem_object_is_inactive(obj))
1306 list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
Chris Wilson7d1c4802010-08-07 21:45:03 +01001307
Chris Wilson05394f32010-11-08 19:18:58 +00001308 pfn = ((dev->agp->base + obj->gtt_offset) >> PAGE_SHIFT) +
Jesse Barnesde151cf2008-11-12 10:03:55 -08001309 page_offset;
1310
1311 /* Finally, remap it using the new GTT offset */
1312 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
Chris Wilsonc7150892009-09-23 00:43:56 +01001313unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001314 mutex_unlock(&dev->struct_mutex);
1315
1316 switch (ret) {
Chris Wilson045e7692010-11-07 09:18:22 +00001317 case -EAGAIN:
1318 set_need_resched();
Chris Wilsonc7150892009-09-23 00:43:56 +01001319 case 0:
1320 case -ERESTARTSYS:
1321 return VM_FAULT_NOPAGE;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001322 case -ENOMEM:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001323 return VM_FAULT_OOM;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001324 default:
Chris Wilsonc7150892009-09-23 00:43:56 +01001325 return VM_FAULT_SIGBUS;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001326 }
1327}
1328
1329/**
1330 * i915_gem_create_mmap_offset - create a fake mmap offset for an object
1331 * @obj: obj in question
1332 *
1333 * GEM memory mapping works by handing back to userspace a fake mmap offset
1334 * it can use in a subsequent mmap(2) call. The DRM core code then looks
1335 * up the object based on the offset and sets up the various memory mapping
1336 * structures.
1337 *
1338 * This routine allocates and attaches a fake offset for @obj.
1339 */
1340static int
Chris Wilson05394f32010-11-08 19:18:58 +00001341i915_gem_create_mmap_offset(struct drm_i915_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001342{
Chris Wilson05394f32010-11-08 19:18:58 +00001343 struct drm_device *dev = obj->base.dev;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001344 struct drm_gem_mm *mm = dev->mm_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001345 struct drm_map_list *list;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +11001346 struct drm_local_map *map;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001347 int ret = 0;
1348
1349 /* Set the object up for mmap'ing */
Chris Wilson05394f32010-11-08 19:18:58 +00001350 list = &obj->base.map_list;
Eric Anholt9a298b22009-03-24 12:23:04 -07001351 list->map = kzalloc(sizeof(struct drm_map_list), GFP_KERNEL);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001352 if (!list->map)
1353 return -ENOMEM;
1354
1355 map = list->map;
1356 map->type = _DRM_GEM;
Chris Wilson05394f32010-11-08 19:18:58 +00001357 map->size = obj->base.size;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001358 map->handle = obj;
1359
1360 /* Get a DRM GEM mmap offset allocated... */
1361 list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
Chris Wilson05394f32010-11-08 19:18:58 +00001362 obj->base.size / PAGE_SIZE,
1363 0, 0);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001364 if (!list->file_offset_node) {
Chris Wilson05394f32010-11-08 19:18:58 +00001365 DRM_ERROR("failed to allocate offset for bo %d\n",
1366 obj->base.name);
Chris Wilson9e0ae5342010-09-21 15:05:24 +01001367 ret = -ENOSPC;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001368 goto out_free_list;
1369 }
1370
1371 list->file_offset_node = drm_mm_get_block(list->file_offset_node,
Chris Wilson05394f32010-11-08 19:18:58 +00001372 obj->base.size / PAGE_SIZE,
1373 0);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001374 if (!list->file_offset_node) {
1375 ret = -ENOMEM;
1376 goto out_free_list;
1377 }
1378
1379 list->hash.key = list->file_offset_node->start;
Chris Wilson9e0ae5342010-09-21 15:05:24 +01001380 ret = drm_ht_insert_item(&mm->offset_hash, &list->hash);
1381 if (ret) {
Jesse Barnesde151cf2008-11-12 10:03:55 -08001382 DRM_ERROR("failed to add to map hash\n");
1383 goto out_free_mm;
1384 }
1385
Jesse Barnesde151cf2008-11-12 10:03:55 -08001386 return 0;
1387
1388out_free_mm:
1389 drm_mm_put_block(list->file_offset_node);
1390out_free_list:
Eric Anholt9a298b22009-03-24 12:23:04 -07001391 kfree(list->map);
Chris Wilson39a01d12010-10-28 13:03:06 +01001392 list->map = NULL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001393
1394 return ret;
1395}
1396
Chris Wilson901782b2009-07-10 08:18:50 +01001397/**
1398 * i915_gem_release_mmap - remove physical page mappings
1399 * @obj: obj in question
1400 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001401 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01001402 * relinquish ownership of the pages back to the system.
1403 *
1404 * It is vital that we remove the page mapping if we have mapped a tiled
1405 * object through the GTT and then lose the fence register due to
1406 * resource pressure. Similarly if the object has been moved out of the
1407 * aperture, than pages mapped into userspace must be revoked. Removing the
1408 * mapping will then trigger a page fault on the next user access, allowing
1409 * fixup by i915_gem_fault().
1410 */
Eric Anholtd05ca302009-07-10 13:02:26 -07001411void
Chris Wilson05394f32010-11-08 19:18:58 +00001412i915_gem_release_mmap(struct drm_i915_gem_object *obj)
Chris Wilson901782b2009-07-10 08:18:50 +01001413{
Chris Wilson05394f32010-11-08 19:18:58 +00001414 struct drm_device *dev = obj->base.dev;
Daniel Vetterfb7d5162010-10-01 22:05:20 +02001415 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson901782b2009-07-10 08:18:50 +01001416
Chris Wilson05394f32010-11-08 19:18:58 +00001417 if (unlikely(obj->base.map_list.map && dev->dev_mapping))
Chris Wilson901782b2009-07-10 08:18:50 +01001418 unmap_mapping_range(dev->dev_mapping,
Chris Wilson05394f32010-11-08 19:18:58 +00001419 (loff_t)obj->base.map_list.hash.key<<PAGE_SHIFT,
1420 obj->base.size, 1);
Daniel Vetterfb7d5162010-10-01 22:05:20 +02001421
Chris Wilson05394f32010-11-08 19:18:58 +00001422 if (obj->fault_mappable) {
1423 obj->fault_mappable = false;
1424 i915_gem_info_update_mappable(dev_priv, obj, false);
Daniel Vetterfb7d5162010-10-01 22:05:20 +02001425 }
Chris Wilson901782b2009-07-10 08:18:50 +01001426}
1427
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001428static void
Chris Wilson05394f32010-11-08 19:18:58 +00001429i915_gem_free_mmap_offset(struct drm_i915_gem_object *obj)
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001430{
Chris Wilson05394f32010-11-08 19:18:58 +00001431 struct drm_device *dev = obj->base.dev;
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001432 struct drm_gem_mm *mm = dev->mm_private;
Chris Wilson05394f32010-11-08 19:18:58 +00001433 struct drm_map_list *list = &obj->base.map_list;
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001434
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001435 drm_ht_remove_item(&mm->offset_hash, &list->hash);
Chris Wilson39a01d12010-10-28 13:03:06 +01001436 drm_mm_put_block(list->file_offset_node);
1437 kfree(list->map);
1438 list->map = NULL;
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001439}
1440
Chris Wilson92b88ae2010-11-09 11:47:32 +00001441static uint32_t
1442i915_gem_get_gtt_size(struct drm_i915_gem_object *obj)
1443{
1444 struct drm_device *dev = obj->base.dev;
1445 uint32_t size;
1446
1447 if (INTEL_INFO(dev)->gen >= 4 ||
1448 obj->tiling_mode == I915_TILING_NONE)
1449 return obj->base.size;
1450
1451 /* Previous chips need a power-of-two fence region when tiling */
1452 if (INTEL_INFO(dev)->gen == 3)
1453 size = 1024*1024;
1454 else
1455 size = 512*1024;
1456
1457 while (size < obj->base.size)
1458 size <<= 1;
1459
1460 return size;
1461}
1462
Jesse Barnesde151cf2008-11-12 10:03:55 -08001463/**
1464 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1465 * @obj: object to check
1466 *
1467 * Return the required GTT alignment for an object, taking into account
Daniel Vetter5e783302010-11-14 22:32:36 +01001468 * potential fence register mapping.
Jesse Barnesde151cf2008-11-12 10:03:55 -08001469 */
1470static uint32_t
Chris Wilson05394f32010-11-08 19:18:58 +00001471i915_gem_get_gtt_alignment(struct drm_i915_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001472{
Chris Wilson05394f32010-11-08 19:18:58 +00001473 struct drm_device *dev = obj->base.dev;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001474
1475 /*
1476 * Minimum alignment is 4k (GTT page size), but might be greater
1477 * if a fence register is needed for the object.
1478 */
Chris Wilsona00b10c2010-09-24 21:15:47 +01001479 if (INTEL_INFO(dev)->gen >= 4 ||
Chris Wilson05394f32010-11-08 19:18:58 +00001480 obj->tiling_mode == I915_TILING_NONE)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001481 return 4096;
1482
1483 /*
1484 * Previous chips need to be aligned to the size of the smallest
1485 * fence register that can contain the object.
1486 */
Chris Wilson05394f32010-11-08 19:18:58 +00001487 return i915_gem_get_gtt_size(obj);
Chris Wilsona00b10c2010-09-24 21:15:47 +01001488}
1489
Daniel Vetter5e783302010-11-14 22:32:36 +01001490/**
1491 * i915_gem_get_unfenced_gtt_alignment - return required GTT alignment for an
1492 * unfenced object
1493 * @obj: object to check
1494 *
1495 * Return the required GTT alignment for an object, only taking into account
1496 * unfenced tiled surface requirements.
1497 */
1498static uint32_t
Chris Wilson05394f32010-11-08 19:18:58 +00001499i915_gem_get_unfenced_gtt_alignment(struct drm_i915_gem_object *obj)
Daniel Vetter5e783302010-11-14 22:32:36 +01001500{
Chris Wilson05394f32010-11-08 19:18:58 +00001501 struct drm_device *dev = obj->base.dev;
Daniel Vetter5e783302010-11-14 22:32:36 +01001502 int tile_height;
1503
1504 /*
1505 * Minimum alignment is 4k (GTT page size) for sane hw.
1506 */
1507 if (INTEL_INFO(dev)->gen >= 4 || IS_G33(dev) ||
Chris Wilson05394f32010-11-08 19:18:58 +00001508 obj->tiling_mode == I915_TILING_NONE)
Daniel Vetter5e783302010-11-14 22:32:36 +01001509 return 4096;
1510
1511 /*
1512 * Older chips need unfenced tiled buffers to be aligned to the left
1513 * edge of an even tile row (where tile rows are counted as if the bo is
1514 * placed in a fenced gtt region).
1515 */
1516 if (IS_GEN2(dev) ||
Chris Wilson05394f32010-11-08 19:18:58 +00001517 (obj->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev)))
Daniel Vetter5e783302010-11-14 22:32:36 +01001518 tile_height = 32;
1519 else
1520 tile_height = 8;
1521
Chris Wilson05394f32010-11-08 19:18:58 +00001522 return tile_height * obj->stride * 2;
Daniel Vetter5e783302010-11-14 22:32:36 +01001523}
1524
Jesse Barnesde151cf2008-11-12 10:03:55 -08001525/**
1526 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1527 * @dev: DRM device
1528 * @data: GTT mapping ioctl data
Chris Wilson05394f32010-11-08 19:18:58 +00001529 * @file: GEM object info
Jesse Barnesde151cf2008-11-12 10:03:55 -08001530 *
1531 * Simply returns the fake offset to userspace so it can mmap it.
1532 * The mmap call will end up in drm_gem_mmap(), which will set things
1533 * up so we can get faults in the handler above.
1534 *
1535 * The fault handler will take care of binding the object into the GTT
1536 * (since it may have been evicted to make room for something), allocating
1537 * a fence register, and mapping the appropriate aperture address into
1538 * userspace.
1539 */
1540int
1541i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001542 struct drm_file *file)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001543{
Chris Wilsonda761a62010-10-27 17:37:08 +01001544 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001545 struct drm_i915_gem_mmap_gtt *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001546 struct drm_i915_gem_object *obj;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001547 int ret;
1548
1549 if (!(dev->driver->driver_features & DRIVER_GEM))
1550 return -ENODEV;
1551
Chris Wilson76c1dec2010-09-25 11:22:51 +01001552 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001553 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001554 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001555
Chris Wilson05394f32010-11-08 19:18:58 +00001556 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001557 if (obj == NULL) {
1558 ret = -ENOENT;
1559 goto unlock;
1560 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001561
Chris Wilson05394f32010-11-08 19:18:58 +00001562 if (obj->base.size > dev_priv->mm.gtt_mappable_end) {
Chris Wilsonda761a62010-10-27 17:37:08 +01001563 ret = -E2BIG;
1564 goto unlock;
1565 }
1566
Chris Wilson05394f32010-11-08 19:18:58 +00001567 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilsonab182822009-09-22 18:46:17 +01001568 DRM_ERROR("Attempting to mmap a purgeable buffer\n");
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001569 ret = -EINVAL;
1570 goto out;
Chris Wilsonab182822009-09-22 18:46:17 +01001571 }
1572
Chris Wilson05394f32010-11-08 19:18:58 +00001573 if (!obj->base.map_list.map) {
Jesse Barnesde151cf2008-11-12 10:03:55 -08001574 ret = i915_gem_create_mmap_offset(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001575 if (ret)
1576 goto out;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001577 }
1578
Chris Wilson05394f32010-11-08 19:18:58 +00001579 args->offset = (u64)obj->base.map_list.hash.key << PAGE_SHIFT;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001580
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001581out:
Chris Wilson05394f32010-11-08 19:18:58 +00001582 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001583unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001584 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001585 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001586}
1587
Chris Wilsone5281cc2010-10-28 13:45:36 +01001588static int
Chris Wilson05394f32010-11-08 19:18:58 +00001589i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj,
Chris Wilsone5281cc2010-10-28 13:45:36 +01001590 gfp_t gfpmask)
1591{
Chris Wilsone5281cc2010-10-28 13:45:36 +01001592 int page_count, i;
1593 struct address_space *mapping;
1594 struct inode *inode;
1595 struct page *page;
1596
1597 /* Get the list of pages out of our struct file. They'll be pinned
1598 * at this point until we release them.
1599 */
Chris Wilson05394f32010-11-08 19:18:58 +00001600 page_count = obj->base.size / PAGE_SIZE;
1601 BUG_ON(obj->pages != NULL);
1602 obj->pages = drm_malloc_ab(page_count, sizeof(struct page *));
1603 if (obj->pages == NULL)
Chris Wilsone5281cc2010-10-28 13:45:36 +01001604 return -ENOMEM;
1605
Chris Wilson05394f32010-11-08 19:18:58 +00001606 inode = obj->base.filp->f_path.dentry->d_inode;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001607 mapping = inode->i_mapping;
1608 for (i = 0; i < page_count; i++) {
1609 page = read_cache_page_gfp(mapping, i,
1610 GFP_HIGHUSER |
1611 __GFP_COLD |
1612 __GFP_RECLAIMABLE |
1613 gfpmask);
1614 if (IS_ERR(page))
1615 goto err_pages;
1616
Chris Wilson05394f32010-11-08 19:18:58 +00001617 obj->pages[i] = page;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001618 }
1619
Chris Wilson05394f32010-11-08 19:18:58 +00001620 if (obj->tiling_mode != I915_TILING_NONE)
Chris Wilsone5281cc2010-10-28 13:45:36 +01001621 i915_gem_object_do_bit_17_swizzle(obj);
1622
1623 return 0;
1624
1625err_pages:
1626 while (i--)
Chris Wilson05394f32010-11-08 19:18:58 +00001627 page_cache_release(obj->pages[i]);
Chris Wilsone5281cc2010-10-28 13:45:36 +01001628
Chris Wilson05394f32010-11-08 19:18:58 +00001629 drm_free_large(obj->pages);
1630 obj->pages = NULL;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001631 return PTR_ERR(page);
1632}
1633
Chris Wilson5cdf5882010-09-27 15:51:07 +01001634static void
Chris Wilson05394f32010-11-08 19:18:58 +00001635i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07001636{
Chris Wilson05394f32010-11-08 19:18:58 +00001637 int page_count = obj->base.size / PAGE_SIZE;
Eric Anholt673a3942008-07-30 12:06:12 -07001638 int i;
1639
Chris Wilson05394f32010-11-08 19:18:58 +00001640 BUG_ON(obj->madv == __I915_MADV_PURGED);
Eric Anholt856fa192009-03-19 14:10:50 -07001641
Chris Wilson05394f32010-11-08 19:18:58 +00001642 if (obj->tiling_mode != I915_TILING_NONE)
Eric Anholt280b7132009-03-12 16:56:27 -07001643 i915_gem_object_save_bit_17_swizzle(obj);
1644
Chris Wilson05394f32010-11-08 19:18:58 +00001645 if (obj->madv == I915_MADV_DONTNEED)
1646 obj->dirty = 0;
Chris Wilson3ef94da2009-09-14 16:50:29 +01001647
1648 for (i = 0; i < page_count; i++) {
Chris Wilson05394f32010-11-08 19:18:58 +00001649 if (obj->dirty)
1650 set_page_dirty(obj->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001651
Chris Wilson05394f32010-11-08 19:18:58 +00001652 if (obj->madv == I915_MADV_WILLNEED)
1653 mark_page_accessed(obj->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001654
Chris Wilson05394f32010-11-08 19:18:58 +00001655 page_cache_release(obj->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001656 }
Chris Wilson05394f32010-11-08 19:18:58 +00001657 obj->dirty = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001658
Chris Wilson05394f32010-11-08 19:18:58 +00001659 drm_free_large(obj->pages);
1660 obj->pages = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001661}
1662
Chris Wilsona56ba562010-09-28 10:07:56 +01001663static uint32_t
1664i915_gem_next_request_seqno(struct drm_device *dev,
1665 struct intel_ring_buffer *ring)
1666{
1667 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson5d97eb62010-11-10 20:40:02 +00001668 return ring->outstanding_lazy_request = dev_priv->next_seqno;
Chris Wilsona56ba562010-09-28 10:07:56 +01001669}
1670
Eric Anholt673a3942008-07-30 12:06:12 -07001671static void
Chris Wilson05394f32010-11-08 19:18:58 +00001672i915_gem_object_move_to_active(struct drm_i915_gem_object *obj,
Zou Nan hai852835f2010-05-21 09:08:56 +08001673 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001674{
Chris Wilson05394f32010-11-08 19:18:58 +00001675 struct drm_device *dev = obj->base.dev;
Chris Wilson69dc4982010-10-19 10:36:51 +01001676 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona56ba562010-09-28 10:07:56 +01001677 uint32_t seqno = i915_gem_next_request_seqno(dev, ring);
Daniel Vetter617dbe22010-02-11 22:16:02 +01001678
Zou Nan hai852835f2010-05-21 09:08:56 +08001679 BUG_ON(ring == NULL);
Chris Wilson05394f32010-11-08 19:18:58 +00001680 obj->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001681
1682 /* Add a reference if we're newly entering the active list. */
Chris Wilson05394f32010-11-08 19:18:58 +00001683 if (!obj->active) {
1684 drm_gem_object_reference(&obj->base);
1685 obj->active = 1;
Eric Anholt673a3942008-07-30 12:06:12 -07001686 }
Daniel Vettere35a41d2010-02-11 22:13:59 +01001687
Eric Anholt673a3942008-07-30 12:06:12 -07001688 /* Move from whatever list we were on to the tail of execution. */
Chris Wilson05394f32010-11-08 19:18:58 +00001689 list_move_tail(&obj->mm_list, &dev_priv->mm.active_list);
1690 list_move_tail(&obj->ring_list, &ring->active_list);
1691 obj->last_rendering_seqno = seqno;
Eric Anholt673a3942008-07-30 12:06:12 -07001692}
1693
Eric Anholtce44b0e2008-11-06 16:00:31 -08001694static void
Chris Wilson05394f32010-11-08 19:18:58 +00001695i915_gem_object_move_to_flushing(struct drm_i915_gem_object *obj)
Eric Anholtce44b0e2008-11-06 16:00:31 -08001696{
Chris Wilson05394f32010-11-08 19:18:58 +00001697 struct drm_device *dev = obj->base.dev;
Eric Anholtce44b0e2008-11-06 16:00:31 -08001698 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtce44b0e2008-11-06 16:00:31 -08001699
Chris Wilson05394f32010-11-08 19:18:58 +00001700 BUG_ON(!obj->active);
1701 list_move_tail(&obj->mm_list, &dev_priv->mm.flushing_list);
1702 list_del_init(&obj->ring_list);
1703 obj->last_rendering_seqno = 0;
Eric Anholtce44b0e2008-11-06 16:00:31 -08001704}
Eric Anholt673a3942008-07-30 12:06:12 -07001705
Chris Wilson963b4832009-09-20 23:03:54 +01001706/* Immediately discard the backing storage */
1707static void
Chris Wilson05394f32010-11-08 19:18:58 +00001708i915_gem_object_truncate(struct drm_i915_gem_object *obj)
Chris Wilson963b4832009-09-20 23:03:54 +01001709{
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 Wilson05394f32010-11-08 19:18:58 +00001718 inode = obj->base.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
Chris Wilson05394f32010-11-08 19:18:58 +00001723 obj->madv = __I915_MADV_PURGED;
Chris Wilson963b4832009-09-20 23:03:54 +01001724}
1725
1726static inline int
Chris Wilson05394f32010-11-08 19:18:58 +00001727i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj)
Chris Wilson963b4832009-09-20 23:03:54 +01001728{
Chris Wilson05394f32010-11-08 19:18:58 +00001729 return obj->madv == I915_MADV_DONTNEED;
Chris Wilson963b4832009-09-20 23:03:54 +01001730}
1731
Eric Anholt673a3942008-07-30 12:06:12 -07001732static void
Chris Wilson05394f32010-11-08 19:18:58 +00001733i915_gem_object_move_to_inactive(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07001734{
Chris Wilson05394f32010-11-08 19:18:58 +00001735 struct drm_device *dev = obj->base.dev;
Eric Anholt673a3942008-07-30 12:06:12 -07001736 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07001737
Chris Wilson05394f32010-11-08 19:18:58 +00001738 if (obj->pin_count != 0)
1739 list_move_tail(&obj->mm_list, &dev_priv->mm.pinned_list);
Eric Anholt673a3942008-07-30 12:06:12 -07001740 else
Chris Wilson05394f32010-11-08 19:18:58 +00001741 list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
1742 list_del_init(&obj->ring_list);
Eric Anholt673a3942008-07-30 12:06:12 -07001743
Chris Wilson05394f32010-11-08 19:18:58 +00001744 BUG_ON(!list_empty(&obj->gpu_write_list));
Daniel Vetter99fcb762010-02-07 16:20:18 +01001745
Chris Wilson05394f32010-11-08 19:18:58 +00001746 obj->last_rendering_seqno = 0;
1747 obj->ring = NULL;
1748 if (obj->active) {
1749 obj->active = 0;
1750 drm_gem_object_unreference(&obj->base);
Eric Anholt673a3942008-07-30 12:06:12 -07001751 }
Chris Wilson23bc5982010-09-29 16:10:57 +01001752 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07001753}
1754
Daniel Vetter63560392010-02-19 11:51:59 +01001755static void
1756i915_gem_process_flushing_list(struct drm_device *dev,
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001757 uint32_t flush_domains,
Zou Nan hai852835f2010-05-21 09:08:56 +08001758 struct intel_ring_buffer *ring)
Daniel Vetter63560392010-02-19 11:51:59 +01001759{
1760 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00001761 struct drm_i915_gem_object *obj, *next;
Daniel Vetter63560392010-02-19 11:51:59 +01001762
Chris Wilson05394f32010-11-08 19:18:58 +00001763 list_for_each_entry_safe(obj, next,
Chris Wilson64193402010-10-24 12:38:05 +01001764 &ring->gpu_write_list,
Daniel Vetter63560392010-02-19 11:51:59 +01001765 gpu_write_list) {
Chris Wilson05394f32010-11-08 19:18:58 +00001766 if (obj->base.write_domain & flush_domains) {
1767 uint32_t old_write_domain = obj->base.write_domain;
Daniel Vetter63560392010-02-19 11:51:59 +01001768
Chris Wilson05394f32010-11-08 19:18:58 +00001769 obj->base.write_domain = 0;
1770 list_del_init(&obj->gpu_write_list);
Daniel Vetter617dbe22010-02-11 22:16:02 +01001771 i915_gem_object_move_to_active(obj, ring);
Daniel Vetter63560392010-02-19 11:51:59 +01001772
1773 /* update the fence lru list */
Chris Wilson05394f32010-11-08 19:18:58 +00001774 if (obj->fence_reg != I915_FENCE_REG_NONE) {
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001775 struct drm_i915_fence_reg *reg =
Chris Wilson05394f32010-11-08 19:18:58 +00001776 &dev_priv->fence_regs[obj->fence_reg];
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001777 list_move_tail(&reg->lru_list,
Daniel Vetter63560392010-02-19 11:51:59 +01001778 &dev_priv->mm.fence_list);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001779 }
Daniel Vetter63560392010-02-19 11:51:59 +01001780
1781 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00001782 obj->base.read_domains,
Daniel Vetter63560392010-02-19 11:51:59 +01001783 old_write_domain);
1784 }
1785 }
1786}
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001787
Chris Wilson3cce4692010-10-27 16:11:02 +01001788int
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001789i915_add_request(struct drm_device *dev,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001790 struct drm_file *file,
Chris Wilson8dc5d142010-08-12 12:36:12 +01001791 struct drm_i915_gem_request *request,
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001792 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001793{
1794 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001795 struct drm_i915_file_private *file_priv = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001796 uint32_t seqno;
1797 int was_empty;
Chris Wilson3cce4692010-10-27 16:11:02 +01001798 int ret;
1799
1800 BUG_ON(request == NULL);
Eric Anholt673a3942008-07-30 12:06:12 -07001801
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001802 if (file != NULL)
1803 file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00001804
Chris Wilson3cce4692010-10-27 16:11:02 +01001805 ret = ring->add_request(ring, &seqno);
1806 if (ret)
1807 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001808
Chris Wilsona56ba562010-09-28 10:07:56 +01001809 ring->outstanding_lazy_request = false;
Eric Anholt673a3942008-07-30 12:06:12 -07001810
1811 request->seqno = seqno;
Zou Nan hai852835f2010-05-21 09:08:56 +08001812 request->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001813 request->emitted_jiffies = jiffies;
Zou Nan hai852835f2010-05-21 09:08:56 +08001814 was_empty = list_empty(&ring->request_list);
1815 list_add_tail(&request->list, &ring->request_list);
1816
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001817 if (file_priv) {
Chris Wilson1c255952010-09-26 11:03:27 +01001818 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001819 request->file_priv = file_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00001820 list_add_tail(&request->client_list,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001821 &file_priv->mm.request_list);
Chris Wilson1c255952010-09-26 11:03:27 +01001822 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00001823 }
Eric Anholt673a3942008-07-30 12:06:12 -07001824
Ben Gamarif65d9422009-09-14 17:48:44 -04001825 if (!dev_priv->mm.suspended) {
Chris Wilsonb3b079d2010-09-13 23:44:34 +01001826 mod_timer(&dev_priv->hangcheck_timer,
1827 jiffies + msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
Ben Gamarif65d9422009-09-14 17:48:44 -04001828 if (was_empty)
Chris Wilsonb3b079d2010-09-13 23:44:34 +01001829 queue_delayed_work(dev_priv->wq,
1830 &dev_priv->mm.retire_work, HZ);
Ben Gamarif65d9422009-09-14 17:48:44 -04001831 }
Chris Wilson3cce4692010-10-27 16:11:02 +01001832 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001833}
1834
1835/**
1836 * Command execution barrier
1837 *
1838 * Ensures that all commands in the ring are finished
1839 * before signalling the CPU
1840 */
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001841static void
Zou Nan hai852835f2010-05-21 09:08:56 +08001842i915_retire_commands(struct drm_device *dev, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001843{
Eric Anholt673a3942008-07-30 12:06:12 -07001844 uint32_t flush_domains = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001845
1846 /* The sampler always gets flushed on i965 (sigh) */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001847 if (INTEL_INFO(dev)->gen >= 4)
Eric Anholt673a3942008-07-30 12:06:12 -07001848 flush_domains |= I915_GEM_DOMAIN_SAMPLER;
Zou Nan hai852835f2010-05-21 09:08:56 +08001849
Chris Wilson78501ea2010-10-27 12:18:21 +01001850 ring->flush(ring, I915_GEM_DOMAIN_COMMAND, flush_domains);
Eric Anholt673a3942008-07-30 12:06:12 -07001851}
1852
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001853static inline void
1854i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
Eric Anholt673a3942008-07-30 12:06:12 -07001855{
Chris Wilson1c255952010-09-26 11:03:27 +01001856 struct drm_i915_file_private *file_priv = request->file_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07001857
Chris Wilson1c255952010-09-26 11:03:27 +01001858 if (!file_priv)
1859 return;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001860
Chris Wilson1c255952010-09-26 11:03:27 +01001861 spin_lock(&file_priv->mm.lock);
1862 list_del(&request->client_list);
1863 request->file_priv = NULL;
1864 spin_unlock(&file_priv->mm.lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001865}
1866
Chris Wilsondfaae392010-09-22 10:31:52 +01001867static void i915_gem_reset_ring_lists(struct drm_i915_private *dev_priv,
1868 struct intel_ring_buffer *ring)
Chris Wilson9375e442010-09-19 12:21:28 +01001869{
Chris Wilsondfaae392010-09-22 10:31:52 +01001870 while (!list_empty(&ring->request_list)) {
1871 struct drm_i915_gem_request *request;
Chris Wilson9375e442010-09-19 12:21:28 +01001872
Chris Wilsondfaae392010-09-22 10:31:52 +01001873 request = list_first_entry(&ring->request_list,
1874 struct drm_i915_gem_request,
1875 list);
1876
1877 list_del(&request->list);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001878 i915_gem_request_remove_from_client(request);
Chris Wilsondfaae392010-09-22 10:31:52 +01001879 kfree(request);
1880 }
1881
1882 while (!list_empty(&ring->active_list)) {
Chris Wilson05394f32010-11-08 19:18:58 +00001883 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001884
Chris Wilson05394f32010-11-08 19:18:58 +00001885 obj = list_first_entry(&ring->active_list,
1886 struct drm_i915_gem_object,
1887 ring_list);
Eric Anholt673a3942008-07-30 12:06:12 -07001888
Chris Wilson05394f32010-11-08 19:18:58 +00001889 obj->base.write_domain = 0;
1890 list_del_init(&obj->gpu_write_list);
1891 i915_gem_object_move_to_inactive(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001892 }
Eric Anholt673a3942008-07-30 12:06:12 -07001893}
1894
Chris Wilson069efc12010-09-30 16:53:18 +01001895void i915_gem_reset(struct drm_device *dev)
Eric Anholt673a3942008-07-30 12:06:12 -07001896{
Chris Wilsondfaae392010-09-22 10:31:52 +01001897 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00001898 struct drm_i915_gem_object *obj;
Chris Wilson069efc12010-09-30 16:53:18 +01001899 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07001900
Chris Wilsondfaae392010-09-22 10:31:52 +01001901 i915_gem_reset_ring_lists(dev_priv, &dev_priv->render_ring);
Chris Wilson87acb0a2010-10-19 10:13:00 +01001902 i915_gem_reset_ring_lists(dev_priv, &dev_priv->bsd_ring);
Chris Wilson549f7362010-10-19 11:19:32 +01001903 i915_gem_reset_ring_lists(dev_priv, &dev_priv->blt_ring);
Chris Wilsondfaae392010-09-22 10:31:52 +01001904
1905 /* Remove anything from the flushing lists. The GPU cache is likely
1906 * to be lost on reset along with the data, so simply move the
1907 * lost bo to the inactive list.
1908 */
1909 while (!list_empty(&dev_priv->mm.flushing_list)) {
Chris Wilson05394f32010-11-08 19:18:58 +00001910 obj= list_first_entry(&dev_priv->mm.flushing_list,
1911 struct drm_i915_gem_object,
1912 mm_list);
Chris Wilson9375e442010-09-19 12:21:28 +01001913
Chris Wilson05394f32010-11-08 19:18:58 +00001914 obj->base.write_domain = 0;
1915 list_del_init(&obj->gpu_write_list);
1916 i915_gem_object_move_to_inactive(obj);
Chris Wilson9375e442010-09-19 12:21:28 +01001917 }
Chris Wilson9375e442010-09-19 12:21:28 +01001918
Chris Wilsondfaae392010-09-22 10:31:52 +01001919 /* Move everything out of the GPU domains to ensure we do any
1920 * necessary invalidation upon reuse.
1921 */
Chris Wilson05394f32010-11-08 19:18:58 +00001922 list_for_each_entry(obj,
Chris Wilson77f01232010-09-19 12:31:36 +01001923 &dev_priv->mm.inactive_list,
Chris Wilson69dc4982010-10-19 10:36:51 +01001924 mm_list)
Chris Wilson77f01232010-09-19 12:31:36 +01001925 {
Chris Wilson05394f32010-11-08 19:18:58 +00001926 obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
Chris Wilson77f01232010-09-19 12:31:36 +01001927 }
Chris Wilson069efc12010-09-30 16:53:18 +01001928
1929 /* The fence registers are invalidated so clear them out */
1930 for (i = 0; i < 16; i++) {
1931 struct drm_i915_fence_reg *reg;
1932
1933 reg = &dev_priv->fence_regs[i];
1934 if (!reg->obj)
1935 continue;
1936
1937 i915_gem_clear_fence_reg(reg->obj);
1938 }
Eric Anholt673a3942008-07-30 12:06:12 -07001939}
1940
1941/**
1942 * This function clears the request list as sequence numbers are passed.
1943 */
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001944static void
1945i915_gem_retire_requests_ring(struct drm_device *dev,
1946 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001947{
1948 drm_i915_private_t *dev_priv = dev->dev_private;
1949 uint32_t seqno;
1950
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001951 if (!ring->status_page.page_addr ||
1952 list_empty(&ring->request_list))
Karsten Wiese6c0594a2009-02-23 15:07:57 +01001953 return;
1954
Chris Wilson23bc5982010-09-29 16:10:57 +01001955 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07001956
Chris Wilson78501ea2010-10-27 12:18:21 +01001957 seqno = ring->get_seqno(ring);
Zou Nan hai852835f2010-05-21 09:08:56 +08001958 while (!list_empty(&ring->request_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001959 struct drm_i915_gem_request *request;
Eric Anholt673a3942008-07-30 12:06:12 -07001960
Zou Nan hai852835f2010-05-21 09:08:56 +08001961 request = list_first_entry(&ring->request_list,
Eric Anholt673a3942008-07-30 12:06:12 -07001962 struct drm_i915_gem_request,
1963 list);
Eric Anholt673a3942008-07-30 12:06:12 -07001964
Chris Wilsondfaae392010-09-22 10:31:52 +01001965 if (!i915_seqno_passed(seqno, request->seqno))
Eric Anholt673a3942008-07-30 12:06:12 -07001966 break;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001967
1968 trace_i915_gem_request_retire(dev, request->seqno);
1969
1970 list_del(&request->list);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001971 i915_gem_request_remove_from_client(request);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001972 kfree(request);
1973 }
1974
1975 /* Move any buffers on the active list that are no longer referenced
1976 * by the ringbuffer to the flushing/inactive lists as appropriate.
1977 */
1978 while (!list_empty(&ring->active_list)) {
Chris Wilson05394f32010-11-08 19:18:58 +00001979 struct drm_i915_gem_object *obj;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001980
Chris Wilson05394f32010-11-08 19:18:58 +00001981 obj= list_first_entry(&ring->active_list,
1982 struct drm_i915_gem_object,
1983 ring_list);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001984
Chris Wilson05394f32010-11-08 19:18:58 +00001985 if (!i915_seqno_passed(seqno, obj->last_rendering_seqno))
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001986 break;
1987
Chris Wilson05394f32010-11-08 19:18:58 +00001988 if (obj->base.write_domain != 0)
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001989 i915_gem_object_move_to_flushing(obj);
1990 else
1991 i915_gem_object_move_to_inactive(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001992 }
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001993
1994 if (unlikely (dev_priv->trace_irq_seqno &&
1995 i915_seqno_passed(dev_priv->trace_irq_seqno, seqno))) {
Chris Wilson78501ea2010-10-27 12:18:21 +01001996 ring->user_irq_put(ring);
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001997 dev_priv->trace_irq_seqno = 0;
1998 }
Chris Wilson23bc5982010-09-29 16:10:57 +01001999
2000 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07002001}
2002
2003void
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002004i915_gem_retire_requests(struct drm_device *dev)
2005{
2006 drm_i915_private_t *dev_priv = dev->dev_private;
2007
Chris Wilsonbe726152010-07-23 23:18:50 +01002008 if (!list_empty(&dev_priv->mm.deferred_free_list)) {
Chris Wilson05394f32010-11-08 19:18:58 +00002009 struct drm_i915_gem_object *obj, *next;
Chris Wilsonbe726152010-07-23 23:18:50 +01002010
2011 /* We must be careful that during unbind() we do not
2012 * accidentally infinitely recurse into retire requests.
2013 * Currently:
2014 * retire -> free -> unbind -> wait -> retire_ring
2015 */
Chris Wilson05394f32010-11-08 19:18:58 +00002016 list_for_each_entry_safe(obj, next,
Chris Wilsonbe726152010-07-23 23:18:50 +01002017 &dev_priv->mm.deferred_free_list,
Chris Wilson69dc4982010-10-19 10:36:51 +01002018 mm_list)
Chris Wilson05394f32010-11-08 19:18:58 +00002019 i915_gem_free_object_tail(obj);
Chris Wilsonbe726152010-07-23 23:18:50 +01002020 }
2021
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002022 i915_gem_retire_requests_ring(dev, &dev_priv->render_ring);
Chris Wilson87acb0a2010-10-19 10:13:00 +01002023 i915_gem_retire_requests_ring(dev, &dev_priv->bsd_ring);
Chris Wilson549f7362010-10-19 11:19:32 +01002024 i915_gem_retire_requests_ring(dev, &dev_priv->blt_ring);
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002025}
2026
Daniel Vetter75ef9da2010-08-21 00:25:16 +02002027static void
Eric Anholt673a3942008-07-30 12:06:12 -07002028i915_gem_retire_work_handler(struct work_struct *work)
2029{
2030 drm_i915_private_t *dev_priv;
2031 struct drm_device *dev;
2032
2033 dev_priv = container_of(work, drm_i915_private_t,
2034 mm.retire_work.work);
2035 dev = dev_priv->dev;
2036
Chris Wilson891b48c2010-09-29 12:26:37 +01002037 /* Come back later if the device is busy... */
2038 if (!mutex_trylock(&dev->struct_mutex)) {
2039 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
2040 return;
2041 }
2042
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002043 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08002044
Keith Packard6dbe2772008-10-14 21:41:13 -07002045 if (!dev_priv->mm.suspended &&
Zou Nan haid1b851f2010-05-21 09:08:57 +08002046 (!list_empty(&dev_priv->render_ring.request_list) ||
Chris Wilson549f7362010-10-19 11:19:32 +01002047 !list_empty(&dev_priv->bsd_ring.request_list) ||
2048 !list_empty(&dev_priv->blt_ring.request_list)))
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07002049 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
Eric Anholt673a3942008-07-30 12:06:12 -07002050 mutex_unlock(&dev->struct_mutex);
2051}
2052
Daniel Vetter5a5a0c62009-09-15 22:57:36 +02002053int
Zou Nan hai852835f2010-05-21 09:08:56 +08002054i915_do_wait_request(struct drm_device *dev, uint32_t seqno,
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002055 bool interruptible, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07002056{
2057 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes802c7eb2009-05-05 16:03:48 -07002058 u32 ier;
Eric Anholt673a3942008-07-30 12:06:12 -07002059 int ret = 0;
2060
2061 BUG_ON(seqno == 0);
2062
Ben Gamariba1234d2009-09-14 17:48:47 -04002063 if (atomic_read(&dev_priv->mm.wedged))
Chris Wilson30dbf0c2010-09-25 10:19:17 +01002064 return -EAGAIN;
Ben Gamariffed1d02009-09-14 17:48:41 -04002065
Chris Wilson5d97eb62010-11-10 20:40:02 +00002066 if (seqno == ring->outstanding_lazy_request) {
Chris Wilson3cce4692010-10-27 16:11:02 +01002067 struct drm_i915_gem_request *request;
2068
2069 request = kzalloc(sizeof(*request), GFP_KERNEL);
2070 if (request == NULL)
Daniel Vettere35a41d2010-02-11 22:13:59 +01002071 return -ENOMEM;
Chris Wilson3cce4692010-10-27 16:11:02 +01002072
2073 ret = i915_add_request(dev, NULL, request, ring);
2074 if (ret) {
2075 kfree(request);
2076 return ret;
2077 }
2078
2079 seqno = request->seqno;
Daniel Vettere35a41d2010-02-11 22:13:59 +01002080 }
2081
Chris Wilson78501ea2010-10-27 12:18:21 +01002082 if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) {
Eric Anholtbad720f2009-10-22 16:11:14 -07002083 if (HAS_PCH_SPLIT(dev))
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002084 ier = I915_READ(DEIER) | I915_READ(GTIER);
2085 else
2086 ier = I915_READ(IER);
Jesse Barnes802c7eb2009-05-05 16:03:48 -07002087 if (!ier) {
2088 DRM_ERROR("something (likely vbetool) disabled "
2089 "interrupts, re-enabling\n");
2090 i915_driver_irq_preinstall(dev);
2091 i915_driver_irq_postinstall(dev);
2092 }
2093
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002094 trace_i915_gem_request_wait_begin(dev, seqno);
2095
Chris Wilsonb2223492010-10-27 15:27:33 +01002096 ring->waiting_seqno = seqno;
Chris Wilson78501ea2010-10-27 12:18:21 +01002097 ring->user_irq_get(ring);
Daniel Vetter48764bf2009-09-15 22:57:32 +02002098 if (interruptible)
Zou Nan hai852835f2010-05-21 09:08:56 +08002099 ret = wait_event_interruptible(ring->irq_queue,
Chris Wilson78501ea2010-10-27 12:18:21 +01002100 i915_seqno_passed(ring->get_seqno(ring), seqno)
Zou Nan hai852835f2010-05-21 09:08:56 +08002101 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02002102 else
Zou Nan hai852835f2010-05-21 09:08:56 +08002103 wait_event(ring->irq_queue,
Chris Wilson78501ea2010-10-27 12:18:21 +01002104 i915_seqno_passed(ring->get_seqno(ring), seqno)
Zou Nan hai852835f2010-05-21 09:08:56 +08002105 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02002106
Chris Wilson78501ea2010-10-27 12:18:21 +01002107 ring->user_irq_put(ring);
Chris Wilsonb2223492010-10-27 15:27:33 +01002108 ring->waiting_seqno = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002109
2110 trace_i915_gem_request_wait_end(dev, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07002111 }
Ben Gamariba1234d2009-09-14 17:48:47 -04002112 if (atomic_read(&dev_priv->mm.wedged))
Chris Wilson30dbf0c2010-09-25 10:19:17 +01002113 ret = -EAGAIN;
Eric Anholt673a3942008-07-30 12:06:12 -07002114
2115 if (ret && ret != -ERESTARTSYS)
Daniel Vetter8bff9172010-02-11 22:19:40 +01002116 DRM_ERROR("%s returns %d (awaiting %d at %d, next %d)\n",
Chris Wilson78501ea2010-10-27 12:18:21 +01002117 __func__, ret, seqno, ring->get_seqno(ring),
Daniel Vetter8bff9172010-02-11 22:19:40 +01002118 dev_priv->next_seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07002119
2120 /* Directly dispatch request retiring. While we have the work queue
2121 * to handle this, the waiter on a request often wants an associated
2122 * buffer to have made it to the inactive list, and we would need
2123 * a separate wait queue to handle that.
2124 */
2125 if (ret == 0)
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002126 i915_gem_retire_requests_ring(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07002127
2128 return ret;
2129}
2130
Daniel Vetter48764bf2009-09-15 22:57:32 +02002131/**
2132 * Waits for a sequence number to be signaled, and cleans up the
2133 * request and object lists appropriately for that event.
2134 */
2135static int
Zou Nan hai852835f2010-05-21 09:08:56 +08002136i915_wait_request(struct drm_device *dev, uint32_t seqno,
Chris Wilsona56ba562010-09-28 10:07:56 +01002137 struct intel_ring_buffer *ring)
Daniel Vetter48764bf2009-09-15 22:57:32 +02002138{
Zou Nan hai852835f2010-05-21 09:08:56 +08002139 return i915_do_wait_request(dev, seqno, 1, ring);
Daniel Vetter48764bf2009-09-15 22:57:32 +02002140}
2141
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002142static void
Chris Wilson92204342010-09-18 11:02:01 +01002143i915_gem_flush_ring(struct drm_device *dev,
2144 struct intel_ring_buffer *ring,
2145 uint32_t invalidate_domains,
2146 uint32_t flush_domains)
2147{
Chris Wilson78501ea2010-10-27 12:18:21 +01002148 ring->flush(ring, invalidate_domains, flush_domains);
Chris Wilson92204342010-09-18 11:02:01 +01002149 i915_gem_process_flushing_list(dev, flush_domains, ring);
2150}
2151
2152static void
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002153i915_gem_flush(struct drm_device *dev,
2154 uint32_t invalidate_domains,
Chris Wilson92204342010-09-18 11:02:01 +01002155 uint32_t flush_domains,
2156 uint32_t flush_rings)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002157{
2158 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter8bff9172010-02-11 22:19:40 +01002159
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002160 if (flush_domains & I915_GEM_DOMAIN_CPU)
Daniel Vetter40ce6572010-11-05 18:12:18 +01002161 intel_gtt_chipset_flush();
Zou Nan haid1b851f2010-05-21 09:08:57 +08002162
Chris Wilson92204342010-09-18 11:02:01 +01002163 if ((flush_domains | invalidate_domains) & I915_GEM_GPU_DOMAINS) {
2164 if (flush_rings & RING_RENDER)
Chris Wilson05394f32010-11-08 19:18:58 +00002165 i915_gem_flush_ring(dev, &dev_priv->render_ring,
Chris Wilson92204342010-09-18 11:02:01 +01002166 invalidate_domains, flush_domains);
2167 if (flush_rings & RING_BSD)
Chris Wilson05394f32010-11-08 19:18:58 +00002168 i915_gem_flush_ring(dev, &dev_priv->bsd_ring,
Chris Wilson92204342010-09-18 11:02:01 +01002169 invalidate_domains, flush_domains);
Chris Wilson549f7362010-10-19 11:19:32 +01002170 if (flush_rings & RING_BLT)
Chris Wilson05394f32010-11-08 19:18:58 +00002171 i915_gem_flush_ring(dev, &dev_priv->blt_ring,
Chris Wilson549f7362010-10-19 11:19:32 +01002172 invalidate_domains, flush_domains);
Chris Wilson92204342010-09-18 11:02:01 +01002173 }
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002174}
2175
Eric Anholt673a3942008-07-30 12:06:12 -07002176/**
2177 * Ensures that all rendering to the object has completed and the object is
2178 * safe to unbind from the GTT or access from the CPU.
2179 */
2180static int
Chris Wilson05394f32010-11-08 19:18:58 +00002181i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj,
Chris Wilson2cf34d72010-09-14 13:03:28 +01002182 bool interruptible)
Eric Anholt673a3942008-07-30 12:06:12 -07002183{
Chris Wilson05394f32010-11-08 19:18:58 +00002184 struct drm_device *dev = obj->base.dev;
Eric Anholt673a3942008-07-30 12:06:12 -07002185 int ret;
2186
Eric Anholte47c68e2008-11-14 13:35:19 -08002187 /* This function only exists to support waiting for existing rendering,
2188 * not for emitting required flushes.
Eric Anholt673a3942008-07-30 12:06:12 -07002189 */
Chris Wilson05394f32010-11-08 19:18:58 +00002190 BUG_ON((obj->base.write_domain & I915_GEM_GPU_DOMAINS) != 0);
Eric Anholt673a3942008-07-30 12:06:12 -07002191
2192 /* If there is rendering queued on the buffer being evicted, wait for
2193 * it.
2194 */
Chris Wilson05394f32010-11-08 19:18:58 +00002195 if (obj->active) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01002196 ret = i915_do_wait_request(dev,
Chris Wilson05394f32010-11-08 19:18:58 +00002197 obj->last_rendering_seqno,
Chris Wilson2cf34d72010-09-14 13:03:28 +01002198 interruptible,
Chris Wilson05394f32010-11-08 19:18:58 +00002199 obj->ring);
Chris Wilson2cf34d72010-09-14 13:03:28 +01002200 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002201 return ret;
2202 }
2203
2204 return 0;
2205}
2206
2207/**
2208 * Unbinds an object from the GTT aperture.
2209 */
Jesse Barnes0f973f22009-01-26 17:10:45 -08002210int
Chris Wilson05394f32010-11-08 19:18:58 +00002211i915_gem_object_unbind(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002212{
Chris Wilson05394f32010-11-08 19:18:58 +00002213 struct drm_device *dev = obj->base.dev;
Chris Wilson73aa8082010-09-30 11:46:12 +01002214 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07002215 int ret = 0;
2216
Chris Wilson05394f32010-11-08 19:18:58 +00002217 if (obj->gtt_space == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002218 return 0;
2219
Chris Wilson05394f32010-11-08 19:18:58 +00002220 if (obj->pin_count != 0) {
Eric Anholt673a3942008-07-30 12:06:12 -07002221 DRM_ERROR("Attempting to unbind pinned buffer\n");
2222 return -EINVAL;
2223 }
2224
Eric Anholt5323fd02009-09-09 11:50:45 -07002225 /* blow away mappings if mapped through GTT */
2226 i915_gem_release_mmap(obj);
2227
Eric Anholt673a3942008-07-30 12:06:12 -07002228 /* Move the object to the CPU domain to ensure that
2229 * any possible CPU writes while it's not in the GTT
2230 * are flushed when we go to remap it. This will
2231 * also ensure that all pending GPU writes are finished
2232 * before we unbind.
2233 */
Eric Anholte47c68e2008-11-14 13:35:19 -08002234 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Chris Wilson8dc17752010-07-23 23:18:51 +01002235 if (ret == -ERESTARTSYS)
Eric Anholt673a3942008-07-30 12:06:12 -07002236 return ret;
Chris Wilson8dc17752010-07-23 23:18:51 +01002237 /* Continue on if we fail due to EIO, the GPU is hung so we
2238 * should be safe and we need to cleanup or else we might
2239 * cause memory corruption through use-after-free.
2240 */
Chris Wilson812ed4922010-09-30 15:08:57 +01002241 if (ret) {
2242 i915_gem_clflush_object(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00002243 obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
Chris Wilson812ed4922010-09-30 15:08:57 +01002244 }
Eric Anholt673a3942008-07-30 12:06:12 -07002245
Daniel Vetter96b47b62009-12-15 17:50:00 +01002246 /* release the fence reg _after_ flushing */
Chris Wilson05394f32010-11-08 19:18:58 +00002247 if (obj->fence_reg != I915_FENCE_REG_NONE)
Daniel Vetter96b47b62009-12-15 17:50:00 +01002248 i915_gem_clear_fence_reg(obj);
2249
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002250 i915_gem_gtt_unbind_object(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002251
Chris Wilsone5281cc2010-10-28 13:45:36 +01002252 i915_gem_object_put_pages_gtt(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002253
Chris Wilson05394f32010-11-08 19:18:58 +00002254 i915_gem_info_remove_gtt(dev_priv, obj);
2255 list_del_init(&obj->mm_list);
Daniel Vetter75e9e912010-11-04 17:11:09 +01002256 /* Avoid an unnecessary call to unbind on rebind. */
Chris Wilson05394f32010-11-08 19:18:58 +00002257 obj->map_and_fenceable = true;
Eric Anholt673a3942008-07-30 12:06:12 -07002258
Chris Wilson05394f32010-11-08 19:18:58 +00002259 drm_mm_put_block(obj->gtt_space);
2260 obj->gtt_space = NULL;
2261 obj->gtt_offset = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002262
Chris Wilson05394f32010-11-08 19:18:58 +00002263 if (i915_gem_object_is_purgeable(obj))
Chris Wilson963b4832009-09-20 23:03:54 +01002264 i915_gem_object_truncate(obj);
2265
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002266 trace_i915_gem_object_unbind(obj);
2267
Chris Wilson8dc17752010-07-23 23:18:51 +01002268 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002269}
2270
Chris Wilsona56ba562010-09-28 10:07:56 +01002271static int i915_ring_idle(struct drm_device *dev,
2272 struct intel_ring_buffer *ring)
2273{
Chris Wilson395b70b2010-10-28 21:28:46 +01002274 if (list_empty(&ring->gpu_write_list) && list_empty(&ring->active_list))
Chris Wilson64193402010-10-24 12:38:05 +01002275 return 0;
2276
Chris Wilson05394f32010-11-08 19:18:58 +00002277 i915_gem_flush_ring(dev, ring,
Chris Wilsona56ba562010-09-28 10:07:56 +01002278 I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
2279 return i915_wait_request(dev,
2280 i915_gem_next_request_seqno(dev, ring),
2281 ring);
2282}
2283
Chris Wilsonb47eb4a2010-08-07 11:01:23 +01002284int
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002285i915_gpu_idle(struct drm_device *dev)
2286{
2287 drm_i915_private_t *dev_priv = dev->dev_private;
2288 bool lists_empty;
Zou Nan hai852835f2010-05-21 09:08:56 +08002289 int ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002290
Zou Nan haid1b851f2010-05-21 09:08:57 +08002291 lists_empty = (list_empty(&dev_priv->mm.flushing_list) &&
Chris Wilson395b70b2010-10-28 21:28:46 +01002292 list_empty(&dev_priv->mm.active_list));
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002293 if (lists_empty)
2294 return 0;
2295
2296 /* Flush everything onto the inactive list. */
Chris Wilsona56ba562010-09-28 10:07:56 +01002297 ret = i915_ring_idle(dev, &dev_priv->render_ring);
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002298 if (ret)
2299 return ret;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002300
Chris Wilson87acb0a2010-10-19 10:13:00 +01002301 ret = i915_ring_idle(dev, &dev_priv->bsd_ring);
2302 if (ret)
2303 return ret;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002304
Chris Wilson549f7362010-10-19 11:19:32 +01002305 ret = i915_ring_idle(dev, &dev_priv->blt_ring);
2306 if (ret)
2307 return ret;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002308
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002309 return 0;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002310}
2311
Chris Wilson05394f32010-11-08 19:18:58 +00002312static void sandybridge_write_fence_reg(struct drm_i915_gem_object *obj)
Eric Anholt4e901fd2009-10-26 16:44:17 -07002313{
Chris Wilson05394f32010-11-08 19:18:58 +00002314 struct drm_device *dev = obj->base.dev;
Eric Anholt4e901fd2009-10-26 16:44:17 -07002315 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00002316 u32 size = obj->gtt_space->size;
2317 int regnum = obj->fence_reg;
Eric Anholt4e901fd2009-10-26 16:44:17 -07002318 uint64_t val;
2319
Chris Wilson05394f32010-11-08 19:18:58 +00002320 val = (uint64_t)((obj->gtt_offset + size - 4096) &
Eric Anholt4e901fd2009-10-26 16:44:17 -07002321 0xfffff000) << 32;
Chris Wilson05394f32010-11-08 19:18:58 +00002322 val |= obj->gtt_offset & 0xfffff000;
2323 val |= (uint64_t)((obj->stride / 128) - 1) <<
Eric Anholt4e901fd2009-10-26 16:44:17 -07002324 SANDYBRIDGE_FENCE_PITCH_SHIFT;
2325
Chris Wilson05394f32010-11-08 19:18:58 +00002326 if (obj->tiling_mode == I915_TILING_Y)
Eric Anholt4e901fd2009-10-26 16:44:17 -07002327 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2328 val |= I965_FENCE_REG_VALID;
2329
2330 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (regnum * 8), val);
2331}
2332
Chris Wilson05394f32010-11-08 19:18:58 +00002333static void i965_write_fence_reg(struct drm_i915_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002334{
Chris Wilson05394f32010-11-08 19:18:58 +00002335 struct drm_device *dev = obj->base.dev;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002336 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00002337 u32 size = obj->gtt_space->size;
2338 int regnum = obj->fence_reg;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002339 uint64_t val;
2340
Chris Wilson05394f32010-11-08 19:18:58 +00002341 val = (uint64_t)((obj->gtt_offset + size - 4096) &
Jesse Barnesde151cf2008-11-12 10:03:55 -08002342 0xfffff000) << 32;
Chris Wilson05394f32010-11-08 19:18:58 +00002343 val |= obj->gtt_offset & 0xfffff000;
2344 val |= ((obj->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
2345 if (obj->tiling_mode == I915_TILING_Y)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002346 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2347 val |= I965_FENCE_REG_VALID;
2348
2349 I915_WRITE64(FENCE_REG_965_0 + (regnum * 8), val);
2350}
2351
Chris Wilson05394f32010-11-08 19:18:58 +00002352static void i915_write_fence_reg(struct drm_i915_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002353{
Chris Wilson05394f32010-11-08 19:18:58 +00002354 struct drm_device *dev = obj->base.dev;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002355 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00002356 u32 size = obj->gtt_space->size;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002357 uint32_t fence_reg, val, pitch_val;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002358 int tile_width;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002359
Chris Wilson05394f32010-11-08 19:18:58 +00002360 if ((obj->gtt_offset & ~I915_FENCE_START_MASK) ||
2361 (obj->gtt_offset & (size - 1))) {
Chris Wilsona00b10c2010-09-24 21:15:47 +01002362 WARN(1, "%s: object 0x%08x [fenceable? %d] not 1M or size (0x%08x) aligned [gtt_space offset=%lx, size=%lx]\n",
Chris Wilson05394f32010-11-08 19:18:58 +00002363 __func__, obj->gtt_offset, obj->map_and_fenceable, size,
2364 obj->gtt_space->start, obj->gtt_space->size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002365 return;
2366 }
2367
Chris Wilson05394f32010-11-08 19:18:58 +00002368 if (obj->tiling_mode == I915_TILING_Y &&
Jesse Barnes0f973f22009-01-26 17:10:45 -08002369 HAS_128_BYTE_Y_TILING(dev))
2370 tile_width = 128;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002371 else
Jesse Barnes0f973f22009-01-26 17:10:45 -08002372 tile_width = 512;
2373
2374 /* Note: pitch better be a power of two tile widths */
Chris Wilson05394f32010-11-08 19:18:58 +00002375 pitch_val = obj->stride / tile_width;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002376 pitch_val = ffs(pitch_val) - 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002377
Chris Wilson05394f32010-11-08 19:18:58 +00002378 if (obj->tiling_mode == I915_TILING_Y &&
Daniel Vetterc36a2a62010-04-17 15:12:03 +02002379 HAS_128_BYTE_Y_TILING(dev))
2380 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2381 else
2382 WARN_ON(pitch_val > I915_FENCE_MAX_PITCH_VAL);
2383
Chris Wilson05394f32010-11-08 19:18:58 +00002384 val = obj->gtt_offset;
2385 if (obj->tiling_mode == I915_TILING_Y)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002386 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002387 val |= I915_FENCE_SIZE_BITS(size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002388 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2389 val |= I830_FENCE_REG_VALID;
2390
Chris Wilson05394f32010-11-08 19:18:58 +00002391 fence_reg = obj->fence_reg;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002392 if (fence_reg < 8)
2393 fence_reg = FENCE_REG_830_0 + fence_reg * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002394 else
Chris Wilsona00b10c2010-09-24 21:15:47 +01002395 fence_reg = FENCE_REG_945_8 + (fence_reg - 8) * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002396 I915_WRITE(fence_reg, val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002397}
2398
Chris Wilson05394f32010-11-08 19:18:58 +00002399static void i830_write_fence_reg(struct drm_i915_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002400{
Chris Wilson05394f32010-11-08 19:18:58 +00002401 struct drm_device *dev = obj->base.dev;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002402 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00002403 u32 size = obj->gtt_space->size;
2404 int regnum = obj->fence_reg;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002405 uint32_t val;
2406 uint32_t pitch_val;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002407 uint32_t fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002408
Chris Wilson05394f32010-11-08 19:18:58 +00002409 if ((obj->gtt_offset & ~I830_FENCE_START_MASK) ||
2410 (obj->gtt_offset & (obj->base.size - 1))) {
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002411 WARN(1, "%s: object 0x%08x not 512K or size aligned\n",
Chris Wilson05394f32010-11-08 19:18:58 +00002412 __func__, obj->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002413 return;
2414 }
2415
Chris Wilson05394f32010-11-08 19:18:58 +00002416 pitch_val = obj->stride / 128;
Eric Anholte76a16d2009-05-26 17:44:56 -07002417 pitch_val = ffs(pitch_val) - 1;
2418 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2419
Chris Wilson05394f32010-11-08 19:18:58 +00002420 val = obj->gtt_offset;
2421 if (obj->tiling_mode == I915_TILING_Y)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002422 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002423 fence_size_bits = I830_FENCE_SIZE_BITS(size);
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002424 WARN_ON(fence_size_bits & ~0x00000f00);
2425 val |= fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002426 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2427 val |= I830_FENCE_REG_VALID;
2428
2429 I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002430}
2431
Chris Wilson2cf34d72010-09-14 13:03:28 +01002432static int i915_find_fence_reg(struct drm_device *dev,
2433 bool interruptible)
Daniel Vetterae3db242010-02-19 11:51:58 +01002434{
Daniel Vetterae3db242010-02-19 11:51:58 +01002435 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002436 struct drm_i915_fence_reg *reg;
Chris Wilson05394f32010-11-08 19:18:58 +00002437 struct drm_i915_gem_object *obj = NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002438 int i, avail, ret;
2439
2440 /* First try to find a free reg */
2441 avail = 0;
2442 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2443 reg = &dev_priv->fence_regs[i];
2444 if (!reg->obj)
2445 return i;
2446
Chris Wilson05394f32010-11-08 19:18:58 +00002447 if (!reg->obj->pin_count)
2448 avail++;
Daniel Vetterae3db242010-02-19 11:51:58 +01002449 }
2450
2451 if (avail == 0)
2452 return -ENOSPC;
2453
2454 /* None available, try to steal one or wait for a user to finish */
Chris Wilsona00b10c2010-09-24 21:15:47 +01002455 avail = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002456 list_for_each_entry(reg, &dev_priv->mm.fence_list,
2457 lru_list) {
Chris Wilson05394f32010-11-08 19:18:58 +00002458 obj = reg->obj;
2459 if (obj->pin_count)
Daniel Vetterae3db242010-02-19 11:51:58 +01002460 continue;
2461
2462 /* found one! */
Chris Wilson05394f32010-11-08 19:18:58 +00002463 avail = obj->fence_reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01002464 break;
2465 }
2466
Chris Wilsona00b10c2010-09-24 21:15:47 +01002467 BUG_ON(avail == I915_FENCE_REG_NONE);
Daniel Vetterae3db242010-02-19 11:51:58 +01002468
2469 /* We only have a reference on obj from the active list. put_fence_reg
2470 * might drop that one, causing a use-after-free in it. So hold a
2471 * private reference to obj like the other callers of put_fence_reg
2472 * (set_tiling ioctl) do. */
Chris Wilson05394f32010-11-08 19:18:58 +00002473 drm_gem_object_reference(&obj->base);
2474 ret = i915_gem_object_put_fence_reg(obj, interruptible);
2475 drm_gem_object_unreference(&obj->base);
Daniel Vetterae3db242010-02-19 11:51:58 +01002476 if (ret != 0)
2477 return ret;
2478
Chris Wilsona00b10c2010-09-24 21:15:47 +01002479 return avail;
Daniel Vetterae3db242010-02-19 11:51:58 +01002480}
2481
Jesse Barnesde151cf2008-11-12 10:03:55 -08002482/**
2483 * i915_gem_object_get_fence_reg - set up a fence reg for an object
2484 * @obj: object to map through a fence reg
2485 *
2486 * When mapping objects through the GTT, userspace wants to be able to write
2487 * to them without having to worry about swizzling if the object is tiled.
2488 *
2489 * This function walks the fence regs looking for a free one for @obj,
2490 * stealing one if it can't find any.
2491 *
2492 * It then sets up the reg based on the object's properties: address, pitch
2493 * and tiling format.
2494 */
Chris Wilson8c4b8c32009-06-17 22:08:52 +01002495int
Chris Wilson05394f32010-11-08 19:18:58 +00002496i915_gem_object_get_fence_reg(struct drm_i915_gem_object *obj,
Chris Wilson2cf34d72010-09-14 13:03:28 +01002497 bool interruptible)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002498{
Chris Wilson05394f32010-11-08 19:18:58 +00002499 struct drm_device *dev = obj->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002500 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002501 struct drm_i915_fence_reg *reg = NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002502 int ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002503
Eric Anholta09ba7f2009-08-29 12:49:51 -07002504 /* Just update our place in the LRU if our fence is getting used. */
Chris Wilson05394f32010-11-08 19:18:58 +00002505 if (obj->fence_reg != I915_FENCE_REG_NONE) {
2506 reg = &dev_priv->fence_regs[obj->fence_reg];
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002507 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002508 return 0;
2509 }
2510
Chris Wilson05394f32010-11-08 19:18:58 +00002511 switch (obj->tiling_mode) {
Jesse Barnesde151cf2008-11-12 10:03:55 -08002512 case I915_TILING_NONE:
2513 WARN(1, "allocating a fence for non-tiled object?\n");
2514 break;
2515 case I915_TILING_X:
Chris Wilson05394f32010-11-08 19:18:58 +00002516 if (!obj->stride)
Jesse Barnes0f973f22009-01-26 17:10:45 -08002517 return -EINVAL;
Chris Wilson05394f32010-11-08 19:18:58 +00002518 WARN((obj->stride & (512 - 1)),
Jesse Barnes0f973f22009-01-26 17:10:45 -08002519 "object 0x%08x is X tiled but has non-512B pitch\n",
Chris Wilson05394f32010-11-08 19:18:58 +00002520 obj->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002521 break;
2522 case I915_TILING_Y:
Chris Wilson05394f32010-11-08 19:18:58 +00002523 if (!obj->stride)
Jesse Barnes0f973f22009-01-26 17:10:45 -08002524 return -EINVAL;
Chris Wilson05394f32010-11-08 19:18:58 +00002525 WARN((obj->stride & (128 - 1)),
Jesse Barnes0f973f22009-01-26 17:10:45 -08002526 "object 0x%08x is Y tiled but has non-128B pitch\n",
Chris Wilson05394f32010-11-08 19:18:58 +00002527 obj->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002528 break;
2529 }
2530
Chris Wilson2cf34d72010-09-14 13:03:28 +01002531 ret = i915_find_fence_reg(dev, interruptible);
Daniel Vetterae3db242010-02-19 11:51:58 +01002532 if (ret < 0)
2533 return ret;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002534
Chris Wilson05394f32010-11-08 19:18:58 +00002535 obj->fence_reg = ret;
2536 reg = &dev_priv->fence_regs[obj->fence_reg];
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002537 list_add_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002538
Jesse Barnesde151cf2008-11-12 10:03:55 -08002539 reg->obj = obj;
2540
Chris Wilsone259bef2010-09-17 00:32:02 +01002541 switch (INTEL_INFO(dev)->gen) {
2542 case 6:
Chris Wilsona00b10c2010-09-24 21:15:47 +01002543 sandybridge_write_fence_reg(obj);
Chris Wilsone259bef2010-09-17 00:32:02 +01002544 break;
2545 case 5:
2546 case 4:
Chris Wilsona00b10c2010-09-24 21:15:47 +01002547 i965_write_fence_reg(obj);
Chris Wilsone259bef2010-09-17 00:32:02 +01002548 break;
2549 case 3:
Chris Wilsona00b10c2010-09-24 21:15:47 +01002550 i915_write_fence_reg(obj);
Chris Wilsone259bef2010-09-17 00:32:02 +01002551 break;
2552 case 2:
Chris Wilsona00b10c2010-09-24 21:15:47 +01002553 i830_write_fence_reg(obj);
Chris Wilsone259bef2010-09-17 00:32:02 +01002554 break;
2555 }
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002556
Chris Wilsona00b10c2010-09-24 21:15:47 +01002557 trace_i915_gem_object_get_fence(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00002558 obj->fence_reg,
2559 obj->tiling_mode);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002560
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002561 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002562}
2563
2564/**
2565 * i915_gem_clear_fence_reg - clear out fence register info
2566 * @obj: object to clear
2567 *
2568 * Zeroes out the fence register itself and clears out the associated
Chris Wilson05394f32010-11-08 19:18:58 +00002569 * data structures in dev_priv and obj.
Jesse Barnesde151cf2008-11-12 10:03:55 -08002570 */
2571static void
Chris Wilson05394f32010-11-08 19:18:58 +00002572i915_gem_clear_fence_reg(struct drm_i915_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002573{
Chris Wilson05394f32010-11-08 19:18:58 +00002574 struct drm_device *dev = obj->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002575 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00002576 struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[obj->fence_reg];
Chris Wilsone259bef2010-09-17 00:32:02 +01002577 uint32_t fence_reg;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002578
Chris Wilsone259bef2010-09-17 00:32:02 +01002579 switch (INTEL_INFO(dev)->gen) {
2580 case 6:
Eric Anholt4e901fd2009-10-26 16:44:17 -07002581 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 +
Chris Wilson05394f32010-11-08 19:18:58 +00002582 (obj->fence_reg * 8), 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002583 break;
2584 case 5:
2585 case 4:
Chris Wilson05394f32010-11-08 19:18:58 +00002586 I915_WRITE64(FENCE_REG_965_0 + (obj->fence_reg * 8), 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002587 break;
2588 case 3:
Chris Wilson05394f32010-11-08 19:18:58 +00002589 if (obj->fence_reg >= 8)
2590 fence_reg = FENCE_REG_945_8 + (obj->fence_reg - 8) * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002591 else
Chris Wilsone259bef2010-09-17 00:32:02 +01002592 case 2:
Chris Wilson05394f32010-11-08 19:18:58 +00002593 fence_reg = FENCE_REG_830_0 + obj->fence_reg * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002594
2595 I915_WRITE(fence_reg, 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002596 break;
Eric Anholtdc529a42009-03-10 22:34:49 -07002597 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002598
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002599 reg->obj = NULL;
Chris Wilson05394f32010-11-08 19:18:58 +00002600 obj->fence_reg = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002601 list_del_init(&reg->lru_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002602}
2603
Eric Anholt673a3942008-07-30 12:06:12 -07002604/**
Chris Wilson52dc7d32009-06-06 09:46:01 +01002605 * i915_gem_object_put_fence_reg - waits on outstanding fenced access
2606 * to the buffer to finish, and then resets the fence register.
2607 * @obj: tiled object holding a fence register.
Chris Wilson2cf34d72010-09-14 13:03:28 +01002608 * @bool: whether the wait upon the fence is interruptible
Chris Wilson52dc7d32009-06-06 09:46:01 +01002609 *
2610 * Zeroes out the fence register itself and clears out the associated
Chris Wilson05394f32010-11-08 19:18:58 +00002611 * data structures in dev_priv and obj.
Chris Wilson52dc7d32009-06-06 09:46:01 +01002612 */
2613int
Chris Wilson05394f32010-11-08 19:18:58 +00002614i915_gem_object_put_fence_reg(struct drm_i915_gem_object *obj,
Chris Wilson2cf34d72010-09-14 13:03:28 +01002615 bool interruptible)
Chris Wilson52dc7d32009-06-06 09:46:01 +01002616{
Chris Wilson05394f32010-11-08 19:18:58 +00002617 struct drm_device *dev = obj->base.dev;
Chris Wilson53640e12010-09-20 11:40:50 +01002618 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson53640e12010-09-20 11:40:50 +01002619 struct drm_i915_fence_reg *reg;
Chris Wilson52dc7d32009-06-06 09:46:01 +01002620
Chris Wilson05394f32010-11-08 19:18:58 +00002621 if (obj->fence_reg == I915_FENCE_REG_NONE)
Chris Wilson52dc7d32009-06-06 09:46:01 +01002622 return 0;
2623
Daniel Vetter10ae9bd2010-02-01 13:59:17 +01002624 /* If we've changed tiling, GTT-mappings of the object
2625 * need to re-fault to ensure that the correct fence register
2626 * setup is in place.
2627 */
2628 i915_gem_release_mmap(obj);
2629
Chris Wilson52dc7d32009-06-06 09:46:01 +01002630 /* On the i915, GPU access to tiled buffers is via a fence,
2631 * therefore we must wait for any outstanding access to complete
2632 * before clearing the fence.
2633 */
Chris Wilson05394f32010-11-08 19:18:58 +00002634 reg = &dev_priv->fence_regs[obj->fence_reg];
Chris Wilson53640e12010-09-20 11:40:50 +01002635 if (reg->gpu) {
Chris Wilson52dc7d32009-06-06 09:46:01 +01002636 int ret;
2637
Chris Wilson919926a2010-11-12 13:42:53 +00002638 ret = i915_gem_object_flush_gpu_write_domain(obj, NULL);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002639 if (ret)
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002640 return ret;
2641
Chris Wilson2cf34d72010-09-14 13:03:28 +01002642 ret = i915_gem_object_wait_rendering(obj, interruptible);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002643 if (ret)
Chris Wilson52dc7d32009-06-06 09:46:01 +01002644 return ret;
Chris Wilson53640e12010-09-20 11:40:50 +01002645
2646 reg->gpu = false;
Chris Wilson52dc7d32009-06-06 09:46:01 +01002647 }
2648
Daniel Vetter4a726612010-02-01 13:59:16 +01002649 i915_gem_object_flush_gtt_write_domain(obj);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002650 i915_gem_clear_fence_reg(obj);
Chris Wilson52dc7d32009-06-06 09:46:01 +01002651
2652 return 0;
2653}
2654
2655/**
Eric Anholt673a3942008-07-30 12:06:12 -07002656 * Finds free space in the GTT aperture and binds the object there.
2657 */
2658static int
Chris Wilson05394f32010-11-08 19:18:58 +00002659i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
Daniel Vetter920afa72010-09-16 17:54:23 +02002660 unsigned alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01002661 bool map_and_fenceable)
Eric Anholt673a3942008-07-30 12:06:12 -07002662{
Chris Wilson05394f32010-11-08 19:18:58 +00002663 struct drm_device *dev = obj->base.dev;
Eric Anholt673a3942008-07-30 12:06:12 -07002664 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07002665 struct drm_mm_node *free_space;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002666 gfp_t gfpmask = __GFP_NORETRY | __GFP_NOWARN;
Daniel Vetter5e783302010-11-14 22:32:36 +01002667 u32 size, fence_size, fence_alignment, unfenced_alignment;
Daniel Vetter75e9e912010-11-04 17:11:09 +01002668 bool mappable, fenceable;
Chris Wilson07f73f62009-09-14 16:50:30 +01002669 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002670
Chris Wilson05394f32010-11-08 19:18:58 +00002671 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01002672 DRM_ERROR("Attempting to bind a purgeable object\n");
2673 return -EINVAL;
2674 }
2675
Chris Wilson05394f32010-11-08 19:18:58 +00002676 fence_size = i915_gem_get_gtt_size(obj);
2677 fence_alignment = i915_gem_get_gtt_alignment(obj);
2678 unfenced_alignment = i915_gem_get_unfenced_gtt_alignment(obj);
Chris Wilsona00b10c2010-09-24 21:15:47 +01002679
Eric Anholt673a3942008-07-30 12:06:12 -07002680 if (alignment == 0)
Daniel Vetter5e783302010-11-14 22:32:36 +01002681 alignment = map_and_fenceable ? fence_alignment :
2682 unfenced_alignment;
Daniel Vetter75e9e912010-11-04 17:11:09 +01002683 if (map_and_fenceable && alignment & (fence_alignment - 1)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002684 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2685 return -EINVAL;
2686 }
2687
Chris Wilson05394f32010-11-08 19:18:58 +00002688 size = map_and_fenceable ? fence_size : obj->base.size;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002689
Chris Wilson654fc602010-05-27 13:18:21 +01002690 /* If the object is bigger than the entire aperture, reject it early
2691 * before evicting everything in a vain attempt to find space.
2692 */
Chris Wilson05394f32010-11-08 19:18:58 +00002693 if (obj->base.size >
Daniel Vetter75e9e912010-11-04 17:11:09 +01002694 (map_and_fenceable ? dev_priv->mm.gtt_mappable_end : dev_priv->mm.gtt_total)) {
Chris Wilson654fc602010-05-27 13:18:21 +01002695 DRM_ERROR("Attempting to bind an object larger than the aperture\n");
2696 return -E2BIG;
2697 }
2698
Eric Anholt673a3942008-07-30 12:06:12 -07002699 search_free:
Daniel Vetter75e9e912010-11-04 17:11:09 +01002700 if (map_and_fenceable)
Daniel Vetter920afa72010-09-16 17:54:23 +02002701 free_space =
2702 drm_mm_search_free_in_range(&dev_priv->mm.gtt_space,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002703 size, alignment, 0,
Daniel Vetter920afa72010-09-16 17:54:23 +02002704 dev_priv->mm.gtt_mappable_end,
2705 0);
2706 else
2707 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002708 size, alignment, 0);
Daniel Vetter920afa72010-09-16 17:54:23 +02002709
2710 if (free_space != NULL) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01002711 if (map_and_fenceable)
Chris Wilson05394f32010-11-08 19:18:58 +00002712 obj->gtt_space =
Daniel Vetter920afa72010-09-16 17:54:23 +02002713 drm_mm_get_block_range_generic(free_space,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002714 size, alignment, 0,
Daniel Vetter920afa72010-09-16 17:54:23 +02002715 dev_priv->mm.gtt_mappable_end,
2716 0);
2717 else
Chris Wilson05394f32010-11-08 19:18:58 +00002718 obj->gtt_space =
Chris Wilsona00b10c2010-09-24 21:15:47 +01002719 drm_mm_get_block(free_space, size, alignment);
Daniel Vetter920afa72010-09-16 17:54:23 +02002720 }
Chris Wilson05394f32010-11-08 19:18:58 +00002721 if (obj->gtt_space == NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -07002722 /* If the gtt is empty and we're still having trouble
2723 * fitting our object in, we're out of memory.
2724 */
Daniel Vetter75e9e912010-11-04 17:11:09 +01002725 ret = i915_gem_evict_something(dev, size, alignment,
2726 map_and_fenceable);
Chris Wilson97311292009-09-21 00:22:34 +01002727 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002728 return ret;
Chris Wilson97311292009-09-21 00:22:34 +01002729
Eric Anholt673a3942008-07-30 12:06:12 -07002730 goto search_free;
2731 }
2732
Chris Wilsone5281cc2010-10-28 13:45:36 +01002733 ret = i915_gem_object_get_pages_gtt(obj, gfpmask);
Eric Anholt673a3942008-07-30 12:06:12 -07002734 if (ret) {
Chris Wilson05394f32010-11-08 19:18:58 +00002735 drm_mm_put_block(obj->gtt_space);
2736 obj->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002737
2738 if (ret == -ENOMEM) {
2739 /* first try to clear up some space from the GTT */
Chris Wilsona00b10c2010-09-24 21:15:47 +01002740 ret = i915_gem_evict_something(dev, size,
Daniel Vetter75e9e912010-11-04 17:11:09 +01002741 alignment,
2742 map_and_fenceable);
Chris Wilson07f73f62009-09-14 16:50:30 +01002743 if (ret) {
Chris Wilson07f73f62009-09-14 16:50:30 +01002744 /* now try to shrink everyone else */
Chris Wilson4bdadb92010-01-27 13:36:32 +00002745 if (gfpmask) {
2746 gfpmask = 0;
2747 goto search_free;
Chris Wilson07f73f62009-09-14 16:50:30 +01002748 }
2749
2750 return ret;
2751 }
2752
2753 goto search_free;
2754 }
2755
Eric Anholt673a3942008-07-30 12:06:12 -07002756 return ret;
2757 }
2758
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002759 ret = i915_gem_gtt_bind_object(obj);
2760 if (ret) {
Chris Wilsone5281cc2010-10-28 13:45:36 +01002761 i915_gem_object_put_pages_gtt(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00002762 drm_mm_put_block(obj->gtt_space);
2763 obj->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002764
Chris Wilsona00b10c2010-09-24 21:15:47 +01002765 ret = i915_gem_evict_something(dev, size,
Daniel Vetter75e9e912010-11-04 17:11:09 +01002766 alignment, map_and_fenceable);
Chris Wilson97311292009-09-21 00:22:34 +01002767 if (ret)
Chris Wilson07f73f62009-09-14 16:50:30 +01002768 return ret;
Chris Wilson07f73f62009-09-14 16:50:30 +01002769
2770 goto search_free;
Eric Anholt673a3942008-07-30 12:06:12 -07002771 }
Eric Anholt673a3942008-07-30 12:06:12 -07002772
Chris Wilson05394f32010-11-08 19:18:58 +00002773 obj->gtt_offset = obj->gtt_space->start;
Daniel Vetterfb7d5162010-10-01 22:05:20 +02002774
Chris Wilsonbf1a1092010-08-07 11:01:20 +01002775 /* keep track of bounds object by adding it to the inactive list */
Chris Wilson05394f32010-11-08 19:18:58 +00002776 list_add_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
2777 i915_gem_info_add_gtt(dev_priv, obj);
Chris Wilsonbf1a1092010-08-07 11:01:20 +01002778
Eric Anholt673a3942008-07-30 12:06:12 -07002779 /* Assert that the object is not currently in any GPU domain. As it
2780 * wasn't in the GTT, there shouldn't be any way it could have been in
2781 * a GPU cache
2782 */
Chris Wilson05394f32010-11-08 19:18:58 +00002783 BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
2784 BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
Eric Anholt673a3942008-07-30 12:06:12 -07002785
Chris Wilson05394f32010-11-08 19:18:58 +00002786 trace_i915_gem_object_bind(obj, obj->gtt_offset, map_and_fenceable);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002787
Daniel Vetter75e9e912010-11-04 17:11:09 +01002788 fenceable =
Chris Wilson05394f32010-11-08 19:18:58 +00002789 obj->gtt_space->size == fence_size &&
2790 (obj->gtt_space->start & (fence_alignment -1)) == 0;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002791
Daniel Vetter75e9e912010-11-04 17:11:09 +01002792 mappable =
Chris Wilson05394f32010-11-08 19:18:58 +00002793 obj->gtt_offset + obj->base.size <= dev_priv->mm.gtt_mappable_end;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002794
Chris Wilson05394f32010-11-08 19:18:58 +00002795 obj->map_and_fenceable = mappable && fenceable;
Daniel Vetter75e9e912010-11-04 17:11:09 +01002796
Eric Anholt673a3942008-07-30 12:06:12 -07002797 return 0;
2798}
2799
2800void
Chris Wilson05394f32010-11-08 19:18:58 +00002801i915_gem_clflush_object(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002802{
Eric Anholt673a3942008-07-30 12:06:12 -07002803 /* If we don't have a page list set up, then we're not pinned
2804 * to GPU, and we can ignore the cache flush because it'll happen
2805 * again at bind time.
2806 */
Chris Wilson05394f32010-11-08 19:18:58 +00002807 if (obj->pages == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002808 return;
2809
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002810 trace_i915_gem_object_clflush(obj);
Eric Anholtcfa16a02009-05-26 18:46:16 -07002811
Chris Wilson05394f32010-11-08 19:18:58 +00002812 drm_clflush_pages(obj->pages, obj->base.size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002813}
2814
Eric Anholte47c68e2008-11-14 13:35:19 -08002815/** Flushes any GPU write domain for the object if it's dirty. */
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002816static int
Chris Wilson05394f32010-11-08 19:18:58 +00002817i915_gem_object_flush_gpu_write_domain(struct drm_i915_gem_object *obj,
Chris Wilson919926a2010-11-12 13:42:53 +00002818 struct intel_ring_buffer *pipelined)
Eric Anholte47c68e2008-11-14 13:35:19 -08002819{
Chris Wilson05394f32010-11-08 19:18:58 +00002820 struct drm_device *dev = obj->base.dev;
Eric Anholte47c68e2008-11-14 13:35:19 -08002821
Chris Wilson05394f32010-11-08 19:18:58 +00002822 if ((obj->base.write_domain & I915_GEM_GPU_DOMAINS) == 0)
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002823 return 0;
Eric Anholte47c68e2008-11-14 13:35:19 -08002824
2825 /* Queue the GPU write cache flushing we need. */
Chris Wilson05394f32010-11-08 19:18:58 +00002826 i915_gem_flush_ring(dev, obj->ring, 0, obj->base.write_domain);
2827 BUG_ON(obj->base.write_domain);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002828
Chris Wilson919926a2010-11-12 13:42:53 +00002829 if (pipelined && pipelined == obj->ring)
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002830 return 0;
2831
Chris Wilson2cf34d72010-09-14 13:03:28 +01002832 return i915_gem_object_wait_rendering(obj, true);
Eric Anholte47c68e2008-11-14 13:35:19 -08002833}
2834
2835/** Flushes the GTT write domain for the object if it's dirty. */
2836static void
Chris Wilson05394f32010-11-08 19:18:58 +00002837i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08002838{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002839 uint32_t old_write_domain;
2840
Chris Wilson05394f32010-11-08 19:18:58 +00002841 if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
Eric Anholte47c68e2008-11-14 13:35:19 -08002842 return;
2843
2844 /* No actual flushing is required for the GTT write domain. Writes
2845 * to it immediately go to main memory as far as we know, so there's
2846 * no chipset flush. It also doesn't land in render cache.
2847 */
Chris Wilson4a684a42010-10-28 14:44:08 +01002848 i915_gem_release_mmap(obj);
2849
Chris Wilson05394f32010-11-08 19:18:58 +00002850 old_write_domain = obj->base.write_domain;
2851 obj->base.write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002852
2853 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00002854 obj->base.read_domains,
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002855 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002856}
2857
2858/** Flushes the CPU write domain for the object if it's dirty. */
2859static void
Chris Wilson05394f32010-11-08 19:18:58 +00002860i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08002861{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002862 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002863
Chris Wilson05394f32010-11-08 19:18:58 +00002864 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
Eric Anholte47c68e2008-11-14 13:35:19 -08002865 return;
2866
2867 i915_gem_clflush_object(obj);
Daniel Vetter40ce6572010-11-05 18:12:18 +01002868 intel_gtt_chipset_flush();
Chris Wilson05394f32010-11-08 19:18:58 +00002869 old_write_domain = obj->base.write_domain;
2870 obj->base.write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002871
2872 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00002873 obj->base.read_domains,
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002874 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002875}
2876
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002877/**
2878 * Moves a single object to the GTT read, and possibly write domain.
2879 *
2880 * This function returns when the move is complete, including waiting on
2881 * flushes to occur.
2882 */
Jesse Barnes79e53942008-11-07 14:24:08 -08002883int
Chris Wilson05394f32010-11-08 19:18:58 +00002884i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, int write)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002885{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002886 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002887 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002888
Eric Anholt02354392008-11-26 13:58:13 -08002889 /* Not valid to be called on unbound objects. */
Chris Wilson05394f32010-11-08 19:18:58 +00002890 if (obj->gtt_space == NULL)
Eric Anholt02354392008-11-26 13:58:13 -08002891 return -EINVAL;
2892
Chris Wilson919926a2010-11-12 13:42:53 +00002893 ret = i915_gem_object_flush_gpu_write_domain(obj, NULL);
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002894 if (ret != 0)
2895 return ret;
2896
Chris Wilson72133422010-09-13 23:56:38 +01002897 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002898
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002899 if (write) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01002900 ret = i915_gem_object_wait_rendering(obj, true);
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002901 if (ret)
2902 return ret;
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002903 }
Eric Anholte47c68e2008-11-14 13:35:19 -08002904
Chris Wilson05394f32010-11-08 19:18:58 +00002905 old_write_domain = obj->base.write_domain;
2906 old_read_domains = obj->base.read_domains;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002907
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002908 /* It should now be out of any other write domains, and we can update
2909 * the domain values for our changes.
2910 */
Chris Wilson05394f32010-11-08 19:18:58 +00002911 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2912 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002913 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00002914 obj->base.read_domains = I915_GEM_DOMAIN_GTT;
2915 obj->base.write_domain = I915_GEM_DOMAIN_GTT;
2916 obj->dirty = 1;
Eric Anholte47c68e2008-11-14 13:35:19 -08002917 }
2918
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002919 trace_i915_gem_object_change_domain(obj,
2920 old_read_domains,
2921 old_write_domain);
2922
Eric Anholte47c68e2008-11-14 13:35:19 -08002923 return 0;
2924}
2925
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002926/*
2927 * Prepare buffer for display plane. Use uninterruptible for possible flush
2928 * wait, as in modesetting process we're not supposed to be interrupted.
2929 */
2930int
Chris Wilson05394f32010-11-08 19:18:58 +00002931i915_gem_object_set_to_display_plane(struct drm_i915_gem_object *obj,
Chris Wilson919926a2010-11-12 13:42:53 +00002932 struct intel_ring_buffer *pipelined)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002933{
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002934 uint32_t old_read_domains;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002935 int ret;
2936
2937 /* Not valid to be called on unbound objects. */
Chris Wilson05394f32010-11-08 19:18:58 +00002938 if (obj->gtt_space == NULL)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002939 return -EINVAL;
2940
Chris Wilson919926a2010-11-12 13:42:53 +00002941 ret = i915_gem_object_flush_gpu_write_domain(obj, pipelined);
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002942 if (ret)
2943 return ret;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002944
Chris Wilsonced270f2010-09-26 22:47:46 +01002945 /* Currently, we are always called from an non-interruptible context. */
2946 if (!pipelined) {
2947 ret = i915_gem_object_wait_rendering(obj, false);
2948 if (ret)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002949 return ret;
2950 }
2951
Chris Wilsonb118c1e2010-05-27 13:18:14 +01002952 i915_gem_object_flush_cpu_write_domain(obj);
2953
Chris Wilson05394f32010-11-08 19:18:58 +00002954 old_read_domains = obj->base.read_domains;
2955 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002956
2957 trace_i915_gem_object_change_domain(obj,
2958 old_read_domains,
Chris Wilson05394f32010-11-08 19:18:58 +00002959 obj->base.write_domain);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002960
2961 return 0;
2962}
2963
Chris Wilson85345512010-11-13 09:49:11 +00002964int
2965i915_gem_object_flush_gpu(struct drm_i915_gem_object *obj,
2966 bool interruptible)
2967{
2968 if (!obj->active)
2969 return 0;
2970
2971 if (obj->base.write_domain & I915_GEM_GPU_DOMAINS)
Chris Wilson05394f32010-11-08 19:18:58 +00002972 i915_gem_flush_ring(obj->base.dev, obj->ring,
Chris Wilson85345512010-11-13 09:49:11 +00002973 0, obj->base.write_domain);
2974
Chris Wilson05394f32010-11-08 19:18:58 +00002975 return i915_gem_object_wait_rendering(obj, interruptible);
Chris Wilson85345512010-11-13 09:49:11 +00002976}
2977
Eric Anholte47c68e2008-11-14 13:35:19 -08002978/**
2979 * Moves a single object to the CPU read, and possibly write domain.
2980 *
2981 * This function returns when the move is complete, including waiting on
2982 * flushes to occur.
2983 */
2984static int
Chris Wilson919926a2010-11-12 13:42:53 +00002985i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholte47c68e2008-11-14 13:35:19 -08002986{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002987 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002988 int ret;
2989
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002990 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Eric Anholte47c68e2008-11-14 13:35:19 -08002991 if (ret != 0)
2992 return ret;
2993
2994 i915_gem_object_flush_gtt_write_domain(obj);
2995
2996 /* If we have a partially-valid cache of the object in the CPU,
2997 * finish invalidating it and free the per-page flags.
2998 */
2999 i915_gem_object_set_to_full_cpu_read_domain(obj);
3000
Chris Wilson72133422010-09-13 23:56:38 +01003001 if (write) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01003002 ret = i915_gem_object_wait_rendering(obj, true);
Chris Wilson72133422010-09-13 23:56:38 +01003003 if (ret)
3004 return ret;
3005 }
3006
Chris Wilson05394f32010-11-08 19:18:58 +00003007 old_write_domain = obj->base.write_domain;
3008 old_read_domains = obj->base.read_domains;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003009
Eric Anholte47c68e2008-11-14 13:35:19 -08003010 /* Flush the CPU cache if it's still invalid. */
Chris Wilson05394f32010-11-08 19:18:58 +00003011 if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
Eric Anholte47c68e2008-11-14 13:35:19 -08003012 i915_gem_clflush_object(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003013
Chris Wilson05394f32010-11-08 19:18:58 +00003014 obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003015 }
3016
3017 /* It should now be out of any other write domains, and we can update
3018 * the domain values for our changes.
3019 */
Chris Wilson05394f32010-11-08 19:18:58 +00003020 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
Eric Anholte47c68e2008-11-14 13:35:19 -08003021
3022 /* If we're writing through the CPU, then the GPU read domains will
3023 * need to be invalidated at next use.
3024 */
3025 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00003026 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3027 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003028 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003029
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003030 trace_i915_gem_object_change_domain(obj,
3031 old_read_domains,
3032 old_write_domain);
3033
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003034 return 0;
3035}
3036
Eric Anholt673a3942008-07-30 12:06:12 -07003037/*
3038 * Set the next domain for the specified object. This
3039 * may not actually perform the necessary flushing/invaliding though,
3040 * as that may want to be batched with other set_domain operations
3041 *
3042 * This is (we hope) the only really tricky part of gem. The goal
3043 * is fairly simple -- track which caches hold bits of the object
3044 * and make sure they remain coherent. A few concrete examples may
3045 * help to explain how it works. For shorthand, we use the notation
3046 * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
3047 * a pair of read and write domain masks.
3048 *
3049 * Case 1: the batch buffer
3050 *
3051 * 1. Allocated
3052 * 2. Written by CPU
3053 * 3. Mapped to GTT
3054 * 4. Read by GPU
3055 * 5. Unmapped from GTT
3056 * 6. Freed
3057 *
3058 * Let's take these a step at a time
3059 *
3060 * 1. Allocated
3061 * Pages allocated from the kernel may still have
3062 * cache contents, so we set them to (CPU, CPU) always.
3063 * 2. Written by CPU (using pwrite)
3064 * The pwrite function calls set_domain (CPU, CPU) and
3065 * this function does nothing (as nothing changes)
3066 * 3. Mapped by GTT
3067 * This function asserts that the object is not
3068 * currently in any GPU-based read or write domains
3069 * 4. Read by GPU
3070 * i915_gem_execbuffer calls set_domain (COMMAND, 0).
3071 * As write_domain is zero, this function adds in the
3072 * current read domains (CPU+COMMAND, 0).
3073 * flush_domains is set to CPU.
3074 * invalidate_domains is set to COMMAND
3075 * clflush is run to get data out of the CPU caches
3076 * then i915_dev_set_domain calls i915_gem_flush to
3077 * emit an MI_FLUSH and drm_agp_chipset_flush
3078 * 5. Unmapped from GTT
3079 * i915_gem_object_unbind calls set_domain (CPU, CPU)
3080 * flush_domains and invalidate_domains end up both zero
3081 * so no flushing/invalidating happens
3082 * 6. Freed
3083 * yay, done
3084 *
3085 * Case 2: The shared render buffer
3086 *
3087 * 1. Allocated
3088 * 2. Mapped to GTT
3089 * 3. Read/written by GPU
3090 * 4. set_domain to (CPU,CPU)
3091 * 5. Read/written by CPU
3092 * 6. Read/written by GPU
3093 *
3094 * 1. Allocated
3095 * Same as last example, (CPU, CPU)
3096 * 2. Mapped to GTT
3097 * Nothing changes (assertions find that it is not in the GPU)
3098 * 3. Read/written by GPU
3099 * execbuffer calls set_domain (RENDER, RENDER)
3100 * flush_domains gets CPU
3101 * invalidate_domains gets GPU
3102 * clflush (obj)
3103 * MI_FLUSH and drm_agp_chipset_flush
3104 * 4. set_domain (CPU, CPU)
3105 * flush_domains gets GPU
3106 * invalidate_domains gets CPU
3107 * wait_rendering (obj) to make sure all drawing is complete.
3108 * This will include an MI_FLUSH to get the data from GPU
3109 * to memory
3110 * clflush (obj) to invalidate the CPU cache
3111 * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
3112 * 5. Read/written by CPU
3113 * cache lines are loaded and dirtied
3114 * 6. Read written by GPU
3115 * Same as last GPU access
3116 *
3117 * Case 3: The constant buffer
3118 *
3119 * 1. Allocated
3120 * 2. Written by CPU
3121 * 3. Read by GPU
3122 * 4. Updated (written) by CPU again
3123 * 5. Read by GPU
3124 *
3125 * 1. Allocated
3126 * (CPU, CPU)
3127 * 2. Written by CPU
3128 * (CPU, CPU)
3129 * 3. Read by GPU
3130 * (CPU+RENDER, 0)
3131 * flush_domains = CPU
3132 * invalidate_domains = RENDER
3133 * clflush (obj)
3134 * MI_FLUSH
3135 * drm_agp_chipset_flush
3136 * 4. Updated (written) by CPU again
3137 * (CPU, CPU)
3138 * flush_domains = 0 (no previous write domain)
3139 * invalidate_domains = 0 (no new read domains)
3140 * 5. Read by GPU
3141 * (CPU+RENDER, 0)
3142 * flush_domains = CPU
3143 * invalidate_domains = RENDER
3144 * clflush (obj)
3145 * MI_FLUSH
3146 * drm_agp_chipset_flush
3147 */
Keith Packardc0d90822008-11-20 23:11:08 -08003148static void
Chris Wilson05394f32010-11-08 19:18:58 +00003149i915_gem_object_set_to_gpu_domain(struct drm_i915_gem_object *obj,
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003150 struct intel_ring_buffer *ring,
3151 struct change_domains *cd)
Eric Anholt673a3942008-07-30 12:06:12 -07003152{
Chris Wilson05394f32010-11-08 19:18:58 +00003153 uint32_t invalidate_domains = 0, flush_domains = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07003154
Eric Anholt673a3942008-07-30 12:06:12 -07003155 /*
3156 * If the object isn't moving to a new write domain,
3157 * let the object stay in multiple read domains
3158 */
Chris Wilson05394f32010-11-08 19:18:58 +00003159 if (obj->base.pending_write_domain == 0)
3160 obj->base.pending_read_domains |= obj->base.read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07003161
3162 /*
3163 * Flush the current write domain if
3164 * the new read domains don't match. Invalidate
3165 * any read domains which differ from the old
3166 * write domain
3167 */
Chris Wilson05394f32010-11-08 19:18:58 +00003168 if (obj->base.write_domain &&
3169 (obj->base.write_domain != obj->base.pending_read_domains ||
3170 obj->ring != ring)) {
3171 flush_domains |= obj->base.write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08003172 invalidate_domains |=
Chris Wilson05394f32010-11-08 19:18:58 +00003173 obj->base.pending_read_domains & ~obj->base.write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07003174 }
3175 /*
3176 * Invalidate any read caches which may have
3177 * stale data. That is, any new read domains.
3178 */
Chris Wilson05394f32010-11-08 19:18:58 +00003179 invalidate_domains |= obj->base.pending_read_domains & ~obj->base.read_domains;
Chris Wilson3d2a8122010-09-29 11:39:53 +01003180 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU)
Eric Anholt673a3942008-07-30 12:06:12 -07003181 i915_gem_clflush_object(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003182
Chris Wilson4a684a42010-10-28 14:44:08 +01003183 /* blow away mappings if mapped through GTT */
3184 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_GTT)
3185 i915_gem_release_mmap(obj);
3186
Eric Anholtefbeed92009-02-19 14:54:51 -08003187 /* The actual obj->write_domain will be updated with
3188 * pending_write_domain after we emit the accumulated flush for all
3189 * of our domain changes in execbuffers (which clears objects'
3190 * write_domains). So if we have a current write domain that we
3191 * aren't changing, set pending_write_domain to that.
3192 */
Chris Wilson05394f32010-11-08 19:18:58 +00003193 if (flush_domains == 0 && obj->base.pending_write_domain == 0)
3194 obj->base.pending_write_domain = obj->base.write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07003195
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003196 cd->invalidate_domains |= invalidate_domains;
3197 cd->flush_domains |= flush_domains;
Chris Wilsonb6651452010-10-23 10:15:06 +01003198 if (flush_domains & I915_GEM_GPU_DOMAINS)
Chris Wilson05394f32010-11-08 19:18:58 +00003199 cd->flush_rings |= obj->ring->id;
Chris Wilsonb6651452010-10-23 10:15:06 +01003200 if (invalidate_domains & I915_GEM_GPU_DOMAINS)
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003201 cd->flush_rings |= ring->id;
Eric Anholt673a3942008-07-30 12:06:12 -07003202}
3203
3204/**
Eric Anholte47c68e2008-11-14 13:35:19 -08003205 * Moves the object from a partially CPU read to a full one.
Eric Anholt673a3942008-07-30 12:06:12 -07003206 *
Eric Anholte47c68e2008-11-14 13:35:19 -08003207 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
3208 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
3209 */
3210static void
Chris Wilson05394f32010-11-08 19:18:58 +00003211i915_gem_object_set_to_full_cpu_read_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08003212{
Chris Wilson05394f32010-11-08 19:18:58 +00003213 if (!obj->page_cpu_valid)
Eric Anholte47c68e2008-11-14 13:35:19 -08003214 return;
3215
3216 /* If we're partially in the CPU read domain, finish moving it in.
3217 */
Chris Wilson05394f32010-11-08 19:18:58 +00003218 if (obj->base.read_domains & I915_GEM_DOMAIN_CPU) {
Eric Anholte47c68e2008-11-14 13:35:19 -08003219 int i;
3220
Chris Wilson05394f32010-11-08 19:18:58 +00003221 for (i = 0; i <= (obj->base.size - 1) / PAGE_SIZE; i++) {
3222 if (obj->page_cpu_valid[i])
Eric Anholte47c68e2008-11-14 13:35:19 -08003223 continue;
Chris Wilson05394f32010-11-08 19:18:58 +00003224 drm_clflush_pages(obj->pages + i, 1);
Eric Anholte47c68e2008-11-14 13:35:19 -08003225 }
Eric Anholte47c68e2008-11-14 13:35:19 -08003226 }
3227
3228 /* Free the page_cpu_valid mappings which are now stale, whether
3229 * or not we've got I915_GEM_DOMAIN_CPU.
3230 */
Chris Wilson05394f32010-11-08 19:18:58 +00003231 kfree(obj->page_cpu_valid);
3232 obj->page_cpu_valid = NULL;
Eric Anholte47c68e2008-11-14 13:35:19 -08003233}
3234
3235/**
3236 * Set the CPU read domain on a range of the object.
3237 *
3238 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
3239 * not entirely valid. The page_cpu_valid member of the object flags which
3240 * pages have been flushed, and will be respected by
3241 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
3242 * of the whole object.
3243 *
3244 * This function returns when the move is complete, including waiting on
3245 * flushes to occur.
Eric Anholt673a3942008-07-30 12:06:12 -07003246 */
3247static int
Chris Wilson05394f32010-11-08 19:18:58 +00003248i915_gem_object_set_cpu_read_domain_range(struct drm_i915_gem_object *obj,
Eric Anholte47c68e2008-11-14 13:35:19 -08003249 uint64_t offset, uint64_t size)
Eric Anholt673a3942008-07-30 12:06:12 -07003250{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003251 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003252 int i, ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003253
Chris Wilson05394f32010-11-08 19:18:58 +00003254 if (offset == 0 && size == obj->base.size)
Eric Anholte47c68e2008-11-14 13:35:19 -08003255 return i915_gem_object_set_to_cpu_domain(obj, 0);
3256
Daniel Vetterba3d8d72010-02-11 22:37:04 +01003257 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Eric Anholte47c68e2008-11-14 13:35:19 -08003258 if (ret != 0)
3259 return ret;
3260 i915_gem_object_flush_gtt_write_domain(obj);
3261
3262 /* If we're already fully in the CPU read domain, we're done. */
Chris Wilson05394f32010-11-08 19:18:58 +00003263 if (obj->page_cpu_valid == NULL &&
3264 (obj->base.read_domains & I915_GEM_DOMAIN_CPU) != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07003265 return 0;
3266
Eric Anholte47c68e2008-11-14 13:35:19 -08003267 /* Otherwise, create/clear the per-page CPU read domain flag if we're
3268 * newly adding I915_GEM_DOMAIN_CPU
3269 */
Chris Wilson05394f32010-11-08 19:18:58 +00003270 if (obj->page_cpu_valid == NULL) {
3271 obj->page_cpu_valid = kzalloc(obj->base.size / PAGE_SIZE,
3272 GFP_KERNEL);
3273 if (obj->page_cpu_valid == NULL)
Eric Anholte47c68e2008-11-14 13:35:19 -08003274 return -ENOMEM;
Chris Wilson05394f32010-11-08 19:18:58 +00003275 } else if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0)
3276 memset(obj->page_cpu_valid, 0, obj->base.size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07003277
3278 /* Flush the cache on any pages that are still invalid from the CPU's
3279 * perspective.
3280 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003281 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
3282 i++) {
Chris Wilson05394f32010-11-08 19:18:58 +00003283 if (obj->page_cpu_valid[i])
Eric Anholt673a3942008-07-30 12:06:12 -07003284 continue;
3285
Chris Wilson05394f32010-11-08 19:18:58 +00003286 drm_clflush_pages(obj->pages + i, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07003287
Chris Wilson05394f32010-11-08 19:18:58 +00003288 obj->page_cpu_valid[i] = 1;
Eric Anholt673a3942008-07-30 12:06:12 -07003289 }
3290
Eric Anholte47c68e2008-11-14 13:35:19 -08003291 /* It should now be out of any other write domains, and we can update
3292 * the domain values for our changes.
3293 */
Chris Wilson05394f32010-11-08 19:18:58 +00003294 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
Eric Anholte47c68e2008-11-14 13:35:19 -08003295
Chris Wilson05394f32010-11-08 19:18:58 +00003296 old_read_domains = obj->base.read_domains;
3297 obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003298
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003299 trace_i915_gem_object_change_domain(obj,
3300 old_read_domains,
Chris Wilson05394f32010-11-08 19:18:58 +00003301 obj->base.write_domain);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003302
Eric Anholt673a3942008-07-30 12:06:12 -07003303 return 0;
3304}
3305
Eric Anholt673a3942008-07-30 12:06:12 -07003306static int
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003307i915_gem_execbuffer_relocate_entry(struct drm_i915_gem_object *obj,
3308 struct drm_file *file_priv,
3309 struct drm_i915_gem_exec_object2 *entry,
3310 struct drm_i915_gem_relocation_entry *reloc)
Eric Anholt673a3942008-07-30 12:06:12 -07003311{
Chris Wilson9af90d12010-10-17 10:01:56 +01003312 struct drm_device *dev = obj->base.dev;
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003313 struct drm_gem_object *target_obj;
3314 uint32_t target_offset;
3315 int ret = -EINVAL;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003316
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003317 target_obj = drm_gem_object_lookup(dev, file_priv,
3318 reloc->target_handle);
3319 if (target_obj == NULL)
3320 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07003321
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003322 target_offset = to_intel_bo(target_obj)->gtt_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003323
Chris Wilson8542a0b2009-09-09 21:15:15 +01003324#if WATCH_RELOC
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003325 DRM_INFO("%s: obj %p offset %08x target %d "
3326 "read %08x write %08x gtt %08x "
3327 "presumed %08x delta %08x\n",
3328 __func__,
3329 obj,
3330 (int) reloc->offset,
3331 (int) reloc->target_handle,
3332 (int) reloc->read_domains,
3333 (int) reloc->write_domain,
3334 (int) target_offset,
3335 (int) reloc->presumed_offset,
3336 reloc->delta);
Chris Wilson8542a0b2009-09-09 21:15:15 +01003337#endif
3338
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003339 /* The target buffer should have appeared before us in the
3340 * exec_object list, so it should have a GTT space bound by now.
3341 */
3342 if (target_offset == 0) {
3343 DRM_ERROR("No GTT space found for object %d\n",
3344 reloc->target_handle);
3345 goto err;
Eric Anholt673a3942008-07-30 12:06:12 -07003346 }
3347
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003348 /* Validate that the target is in a valid r/w GPU domain */
3349 if (reloc->write_domain & (reloc->write_domain - 1)) {
3350 DRM_ERROR("reloc with multiple write domains: "
3351 "obj %p target %d offset %d "
3352 "read %08x write %08x",
3353 obj, reloc->target_handle,
3354 (int) reloc->offset,
3355 reloc->read_domains,
3356 reloc->write_domain);
3357 goto err;
3358 }
3359 if (reloc->write_domain & I915_GEM_DOMAIN_CPU ||
3360 reloc->read_domains & I915_GEM_DOMAIN_CPU) {
3361 DRM_ERROR("reloc with read/write CPU domains: "
3362 "obj %p target %d offset %d "
3363 "read %08x write %08x",
3364 obj, reloc->target_handle,
3365 (int) reloc->offset,
3366 reloc->read_domains,
3367 reloc->write_domain);
3368 goto err;
3369 }
3370 if (reloc->write_domain && target_obj->pending_write_domain &&
3371 reloc->write_domain != target_obj->pending_write_domain) {
3372 DRM_ERROR("Write domain conflict: "
3373 "obj %p target %d offset %d "
3374 "new %08x old %08x\n",
3375 obj, reloc->target_handle,
3376 (int) reloc->offset,
3377 reloc->write_domain,
3378 target_obj->pending_write_domain);
3379 goto err;
3380 }
3381
3382 target_obj->pending_read_domains |= reloc->read_domains;
3383 target_obj->pending_write_domain |= reloc->write_domain;
3384
3385 /* If the relocation already has the right value in it, no
3386 * more work needs to be done.
3387 */
3388 if (target_offset == reloc->presumed_offset)
3389 goto out;
3390
3391 /* Check that the relocation address is valid... */
3392 if (reloc->offset > obj->base.size - 4) {
3393 DRM_ERROR("Relocation beyond object bounds: "
3394 "obj %p target %d offset %d size %d.\n",
3395 obj, reloc->target_handle,
3396 (int) reloc->offset,
3397 (int) obj->base.size);
3398 goto err;
3399 }
3400 if (reloc->offset & 3) {
3401 DRM_ERROR("Relocation not 4-byte aligned: "
3402 "obj %p target %d offset %d.\n",
3403 obj, reloc->target_handle,
3404 (int) reloc->offset);
3405 goto err;
3406 }
3407
3408 /* and points to somewhere within the target object. */
3409 if (reloc->delta >= target_obj->size) {
3410 DRM_ERROR("Relocation beyond target object bounds: "
3411 "obj %p target %d delta %d size %d.\n",
3412 obj, reloc->target_handle,
3413 (int) reloc->delta,
3414 (int) target_obj->size);
3415 goto err;
3416 }
3417
3418 reloc->delta += target_offset;
3419 if (obj->base.write_domain == I915_GEM_DOMAIN_CPU) {
3420 uint32_t page_offset = reloc->offset & ~PAGE_MASK;
3421 char *vaddr;
3422
3423 vaddr = kmap_atomic(obj->pages[reloc->offset >> PAGE_SHIFT]);
3424 *(uint32_t *)(vaddr + page_offset) = reloc->delta;
3425 kunmap_atomic(vaddr);
3426 } else {
3427 struct drm_i915_private *dev_priv = dev->dev_private;
3428 uint32_t __iomem *reloc_entry;
3429 void __iomem *reloc_page;
3430
Chris Wilson05394f32010-11-08 19:18:58 +00003431 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003432 if (ret)
3433 goto err;
3434
3435 /* Map the page containing the relocation we're going to perform. */
3436 reloc->offset += obj->gtt_offset;
3437 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
3438 reloc->offset & PAGE_MASK);
3439 reloc_entry = (uint32_t __iomem *)
3440 (reloc_page + (reloc->offset & ~PAGE_MASK));
3441 iowrite32(reloc->delta, reloc_entry);
3442 io_mapping_unmap_atomic(reloc_page);
3443 }
3444
3445 /* and update the user's relocation entry */
3446 reloc->presumed_offset = target_offset;
3447
3448out:
3449 ret = 0;
3450err:
Chris Wilson9af90d12010-10-17 10:01:56 +01003451 drm_gem_object_unreference(target_obj);
3452 return ret;
3453}
3454
3455static int
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003456i915_gem_execbuffer_relocate_object(struct drm_i915_gem_object *obj,
3457 struct drm_file *file_priv,
3458 struct drm_i915_gem_exec_object2 *entry)
3459{
3460 struct drm_i915_gem_relocation_entry __user *user_relocs;
3461 int i, ret;
3462
3463 user_relocs = (void __user *)(uintptr_t)entry->relocs_ptr;
3464 for (i = 0; i < entry->relocation_count; i++) {
3465 struct drm_i915_gem_relocation_entry reloc;
3466
3467 if (__copy_from_user_inatomic(&reloc,
3468 user_relocs+i,
3469 sizeof(reloc)))
3470 return -EFAULT;
3471
3472 ret = i915_gem_execbuffer_relocate_entry(obj, file_priv, entry, &reloc);
3473 if (ret)
3474 return ret;
3475
3476 if (__copy_to_user_inatomic(&user_relocs[i].presumed_offset,
3477 &reloc.presumed_offset,
3478 sizeof(reloc.presumed_offset)))
3479 return -EFAULT;
3480 }
3481
3482 return 0;
3483}
3484
3485static int
3486i915_gem_execbuffer_relocate_object_slow(struct drm_i915_gem_object *obj,
3487 struct drm_file *file_priv,
3488 struct drm_i915_gem_exec_object2 *entry,
3489 struct drm_i915_gem_relocation_entry *relocs)
3490{
3491 int i, ret;
3492
3493 for (i = 0; i < entry->relocation_count; i++) {
3494 ret = i915_gem_execbuffer_relocate_entry(obj, file_priv, entry, &relocs[i]);
3495 if (ret)
3496 return ret;
3497 }
3498
3499 return 0;
3500}
3501
3502static int
3503i915_gem_execbuffer_relocate(struct drm_device *dev,
3504 struct drm_file *file,
Chris Wilson05394f32010-11-08 19:18:58 +00003505 struct drm_i915_gem_object **object_list,
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003506 struct drm_i915_gem_exec_object2 *exec_list,
3507 int count)
3508{
3509 int i, ret;
3510
3511 for (i = 0; i < count; i++) {
Chris Wilson05394f32010-11-08 19:18:58 +00003512 struct drm_i915_gem_object *obj = object_list[i];
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003513 obj->base.pending_read_domains = 0;
3514 obj->base.pending_write_domain = 0;
3515 ret = i915_gem_execbuffer_relocate_object(obj, file,
3516 &exec_list[i]);
3517 if (ret)
3518 return ret;
3519 }
3520
3521 return 0;
3522}
3523
3524static int
3525i915_gem_execbuffer_reserve(struct drm_device *dev,
3526 struct drm_file *file,
Chris Wilson05394f32010-11-08 19:18:58 +00003527 struct drm_i915_gem_object **object_list,
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003528 struct drm_i915_gem_exec_object2 *exec_list,
3529 int count)
Chris Wilson9af90d12010-10-17 10:01:56 +01003530{
3531 struct drm_i915_private *dev_priv = dev->dev_private;
3532 int ret, i, retry;
3533
Chris Wilsona7a09ae2010-11-12 13:49:09 +00003534 /* Attempt to pin all of the buffers into the GTT.
3535 * This is done in 3 phases:
3536 *
3537 * 1a. Unbind all objects that do not match the GTT constraints for
3538 * the execbuffer (fenceable, mappable, alignment etc).
3539 * 1b. Increment pin count for already bound objects.
3540 * 2. Bind new objects.
3541 * 3. Decrement pin count.
3542 *
3543 * This avoid unnecessary unbinding of later objects in order to makr
3544 * room for the earlier objects *unless* we need to defragment.
3545 */
Chris Wilson5eac3ab2010-10-31 08:49:47 +00003546 retry = 0;
3547 do {
Chris Wilson9af90d12010-10-17 10:01:56 +01003548 ret = 0;
Chris Wilsona7a09ae2010-11-12 13:49:09 +00003549
3550 /* Unbind any ill-fitting objects or pin. */
3551 for (i = 0; i < count; i++) {
3552 struct drm_i915_gem_object *obj = object_list[i];
3553 struct drm_i915_gem_exec_object2 *entry = &exec_list[i];
3554 bool need_fence, need_mappable;
3555
3556 if (!obj->gtt_space)
3557 continue;
3558
3559 need_fence =
3560 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
3561 obj->tiling_mode != I915_TILING_NONE;
3562 need_mappable =
3563 entry->relocation_count ? true : need_fence;
3564
3565 if ((entry->alignment && obj->gtt_offset & (entry->alignment - 1)) ||
3566 (need_mappable && !obj->map_and_fenceable))
3567 ret = i915_gem_object_unbind(obj);
3568 else
3569 ret = i915_gem_object_pin(obj,
3570 entry->alignment,
3571 need_mappable);
3572 if (ret) {
3573 count = i;
3574 goto err;
3575 }
3576 }
3577
3578 /* Bind fresh objects */
Chris Wilson9af90d12010-10-17 10:01:56 +01003579 for (i = 0; i < count; i++) {
3580 struct drm_i915_gem_exec_object2 *entry = &exec_list[i];
Chris Wilson05394f32010-11-08 19:18:58 +00003581 struct drm_i915_gem_object *obj = object_list[i];
Chris Wilsona7a09ae2010-11-12 13:49:09 +00003582 bool need_fence;
3583
3584 need_fence =
Chris Wilson9af90d12010-10-17 10:01:56 +01003585 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
3586 obj->tiling_mode != I915_TILING_NONE;
3587
Chris Wilsona7a09ae2010-11-12 13:49:09 +00003588 if (!obj->gtt_space) {
3589 bool need_mappable =
3590 entry->relocation_count ? true : need_fence;
Daniel Vetter16e809a2010-09-16 19:37:04 +02003591
Chris Wilsona7a09ae2010-11-12 13:49:09 +00003592 ret = i915_gem_object_pin(obj,
3593 entry->alignment,
3594 need_mappable);
Chris Wilson9af90d12010-10-17 10:01:56 +01003595 if (ret)
3596 break;
3597 }
3598
Chris Wilson9af90d12010-10-17 10:01:56 +01003599 if (need_fence) {
Chris Wilson05394f32010-11-08 19:18:58 +00003600 ret = i915_gem_object_get_fence_reg(obj, true);
Chris Wilsona7a09ae2010-11-12 13:49:09 +00003601 if (ret)
Chris Wilson9af90d12010-10-17 10:01:56 +01003602 break;
Chris Wilson9af90d12010-10-17 10:01:56 +01003603
3604 dev_priv->fence_regs[obj->fence_reg].gpu = true;
3605 }
3606
3607 entry->offset = obj->gtt_offset;
3608 }
3609
Chris Wilsona7a09ae2010-11-12 13:49:09 +00003610err: /* Decrement pin count for bound objects */
3611 for (i = 0; i < count; i++) {
3612 struct drm_i915_gem_object *obj = object_list[i];
3613 if (obj->gtt_space)
3614 i915_gem_object_unpin(obj);
3615 }
Chris Wilson9af90d12010-10-17 10:01:56 +01003616
Chris Wilson5eac3ab2010-10-31 08:49:47 +00003617 if (ret != -ENOSPC || retry > 1)
Chris Wilson9af90d12010-10-17 10:01:56 +01003618 return ret;
3619
Chris Wilson5eac3ab2010-10-31 08:49:47 +00003620 /* First attempt, just clear anything that is purgeable.
3621 * Second attempt, clear the entire GTT.
3622 */
3623 ret = i915_gem_evict_everything(dev, retry == 0);
Chris Wilson9af90d12010-10-17 10:01:56 +01003624 if (ret)
3625 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003626
Chris Wilson5eac3ab2010-10-31 08:49:47 +00003627 retry++;
3628 } while (1);
Eric Anholt673a3942008-07-30 12:06:12 -07003629}
3630
Chris Wilson13b29282010-11-01 12:22:48 +00003631static int
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003632i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
3633 struct drm_file *file,
Chris Wilson05394f32010-11-08 19:18:58 +00003634 struct drm_i915_gem_object **object_list,
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003635 struct drm_i915_gem_exec_object2 *exec_list,
3636 int count)
3637{
3638 struct drm_i915_gem_relocation_entry *reloc;
3639 int i, total, ret;
3640
Chris Wilson05394f32010-11-08 19:18:58 +00003641 for (i = 0; i < count; i++)
3642 object_list[i]->in_execbuffer = false;
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003643
3644 mutex_unlock(&dev->struct_mutex);
3645
3646 total = 0;
3647 for (i = 0; i < count; i++)
3648 total += exec_list[i].relocation_count;
3649
3650 reloc = drm_malloc_ab(total, sizeof(*reloc));
3651 if (reloc == NULL) {
3652 mutex_lock(&dev->struct_mutex);
3653 return -ENOMEM;
3654 }
3655
3656 total = 0;
3657 for (i = 0; i < count; i++) {
3658 struct drm_i915_gem_relocation_entry __user *user_relocs;
3659
3660 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3661
3662 if (copy_from_user(reloc+total, user_relocs,
3663 exec_list[i].relocation_count *
3664 sizeof(*reloc))) {
3665 ret = -EFAULT;
3666 mutex_lock(&dev->struct_mutex);
3667 goto err;
3668 }
3669
3670 total += exec_list[i].relocation_count;
3671 }
3672
3673 ret = i915_mutex_lock_interruptible(dev);
3674 if (ret) {
3675 mutex_lock(&dev->struct_mutex);
3676 goto err;
3677 }
3678
3679 ret = i915_gem_execbuffer_reserve(dev, file,
3680 object_list, exec_list,
3681 count);
3682 if (ret)
3683 goto err;
3684
3685 total = 0;
3686 for (i = 0; i < count; i++) {
Chris Wilson05394f32010-11-08 19:18:58 +00003687 struct drm_i915_gem_object *obj = object_list[i];
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003688 obj->base.pending_read_domains = 0;
3689 obj->base.pending_write_domain = 0;
3690 ret = i915_gem_execbuffer_relocate_object_slow(obj, file,
3691 &exec_list[i],
3692 reloc + total);
3693 if (ret)
3694 goto err;
3695
3696 total += exec_list[i].relocation_count;
3697 }
3698
3699 /* Leave the user relocations as are, this is the painfully slow path,
3700 * and we want to avoid the complication of dropping the lock whilst
3701 * having buffers reserved in the aperture and so causing spurious
3702 * ENOSPC for random operations.
3703 */
3704
3705err:
3706 drm_free_large(reloc);
3707 return ret;
3708}
3709
3710static int
Chris Wilson13b29282010-11-01 12:22:48 +00003711i915_gem_execbuffer_move_to_gpu(struct drm_device *dev,
3712 struct drm_file *file,
3713 struct intel_ring_buffer *ring,
Chris Wilson05394f32010-11-08 19:18:58 +00003714 struct drm_i915_gem_object **objects,
Chris Wilson13b29282010-11-01 12:22:48 +00003715 int count)
3716{
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003717 struct change_domains cd;
Chris Wilson13b29282010-11-01 12:22:48 +00003718 int ret, i;
3719
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003720 cd.invalidate_domains = 0;
3721 cd.flush_domains = 0;
3722 cd.flush_rings = 0;
Chris Wilson13b29282010-11-01 12:22:48 +00003723 for (i = 0; i < count; i++)
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003724 i915_gem_object_set_to_gpu_domain(objects[i], ring, &cd);
Chris Wilson13b29282010-11-01 12:22:48 +00003725
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003726 if (cd.invalidate_domains | cd.flush_domains) {
Chris Wilson13b29282010-11-01 12:22:48 +00003727#if WATCH_EXEC
3728 DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
3729 __func__,
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003730 cd.invalidate_domains,
3731 cd.flush_domains);
Chris Wilson13b29282010-11-01 12:22:48 +00003732#endif
Chris Wilson05394f32010-11-08 19:18:58 +00003733 i915_gem_flush(dev,
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003734 cd.invalidate_domains,
3735 cd.flush_domains,
3736 cd.flush_rings);
Chris Wilson13b29282010-11-01 12:22:48 +00003737 }
3738
3739 for (i = 0; i < count; i++) {
Chris Wilson05394f32010-11-08 19:18:58 +00003740 struct drm_i915_gem_object *obj = objects[i];
Chris Wilson13b29282010-11-01 12:22:48 +00003741 /* XXX replace with semaphores */
3742 if (obj->ring && ring != obj->ring) {
Chris Wilson05394f32010-11-08 19:18:58 +00003743 ret = i915_gem_object_wait_rendering(obj, true);
Chris Wilson13b29282010-11-01 12:22:48 +00003744 if (ret)
3745 return ret;
3746 }
3747 }
3748
3749 return 0;
3750}
3751
Eric Anholt673a3942008-07-30 12:06:12 -07003752/* Throttle our rendering by waiting until the ring has completed our requests
3753 * emitted over 20 msec ago.
3754 *
Eric Anholtb9624422009-06-03 07:27:35 +00003755 * Note that if we were to use the current jiffies each time around the loop,
3756 * we wouldn't escape the function with any frames outstanding if the time to
3757 * render a frame was over 20ms.
3758 *
Eric Anholt673a3942008-07-30 12:06:12 -07003759 * This should get us reasonable parallelism between CPU and GPU but also
3760 * relatively low latency when blocking on a particular request to finish.
3761 */
3762static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003763i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003764{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003765 struct drm_i915_private *dev_priv = dev->dev_private;
3766 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00003767 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003768 struct drm_i915_gem_request *request;
3769 struct intel_ring_buffer *ring = NULL;
3770 u32 seqno = 0;
3771 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003772
Chris Wilson1c255952010-09-26 11:03:27 +01003773 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003774 list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
Eric Anholtb9624422009-06-03 07:27:35 +00003775 if (time_after_eq(request->emitted_jiffies, recent_enough))
3776 break;
3777
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003778 ring = request->ring;
3779 seqno = request->seqno;
Eric Anholtb9624422009-06-03 07:27:35 +00003780 }
Chris Wilson1c255952010-09-26 11:03:27 +01003781 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003782
3783 if (seqno == 0)
3784 return 0;
3785
3786 ret = 0;
Chris Wilson78501ea2010-10-27 12:18:21 +01003787 if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) {
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003788 /* And wait for the seqno passing without holding any locks and
3789 * causing extra latency for others. This is safe as the irq
3790 * generation is designed to be run atomically and so is
3791 * lockless.
3792 */
Chris Wilson78501ea2010-10-27 12:18:21 +01003793 ring->user_irq_get(ring);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003794 ret = wait_event_interruptible(ring->irq_queue,
Chris Wilson78501ea2010-10-27 12:18:21 +01003795 i915_seqno_passed(ring->get_seqno(ring), seqno)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003796 || atomic_read(&dev_priv->mm.wedged));
Chris Wilson78501ea2010-10-27 12:18:21 +01003797 ring->user_irq_put(ring);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003798
3799 if (ret == 0 && atomic_read(&dev_priv->mm.wedged))
3800 ret = -EIO;
3801 }
3802
3803 if (ret == 0)
3804 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
Eric Anholtb9624422009-06-03 07:27:35 +00003805
Eric Anholt673a3942008-07-30 12:06:12 -07003806 return ret;
3807}
3808
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003809static int
Chris Wilson2549d6c2010-10-14 12:10:41 +01003810i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec,
3811 uint64_t exec_offset)
Chris Wilson83d60792009-06-06 09:45:57 +01003812{
3813 uint32_t exec_start, exec_len;
3814
3815 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
3816 exec_len = (uint32_t) exec->batch_len;
3817
3818 if ((exec_start | exec_len) & 0x7)
3819 return -EINVAL;
3820
3821 if (!exec_start)
3822 return -EINVAL;
3823
3824 return 0;
3825}
3826
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003827static int
Chris Wilson2549d6c2010-10-14 12:10:41 +01003828validate_exec_list(struct drm_i915_gem_exec_object2 *exec,
3829 int count)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003830{
Chris Wilson2549d6c2010-10-14 12:10:41 +01003831 int i;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003832
Chris Wilson2549d6c2010-10-14 12:10:41 +01003833 for (i = 0; i < count; i++) {
3834 char __user *ptr = (char __user *)(uintptr_t)exec[i].relocs_ptr;
Chris Wilsond1d78832010-11-21 09:23:48 +00003835 int length; /* limited by fault_in_pages_readable() */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003836
Chris Wilsond1d78832010-11-21 09:23:48 +00003837 /* First check for malicious input causing overflow */
3838 if (exec[i].relocation_count >
3839 INT_MAX / sizeof(struct drm_i915_gem_relocation_entry))
3840 return -EINVAL;
3841
3842 length = exec[i].relocation_count *
3843 sizeof(struct drm_i915_gem_relocation_entry);
Chris Wilson2549d6c2010-10-14 12:10:41 +01003844 if (!access_ok(VERIFY_READ, ptr, length))
3845 return -EFAULT;
3846
Chris Wilsonb5dc6082010-10-20 20:59:57 +01003847 /* we may also need to update the presumed offsets */
3848 if (!access_ok(VERIFY_WRITE, ptr, length))
3849 return -EFAULT;
3850
Chris Wilson2549d6c2010-10-14 12:10:41 +01003851 if (fault_in_pages_readable(ptr, length))
3852 return -EFAULT;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003853 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003854
Chris Wilson2549d6c2010-10-14 12:10:41 +01003855 return 0;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003856}
3857
Chris Wilson2549d6c2010-10-14 12:10:41 +01003858static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003859i915_gem_do_execbuffer(struct drm_device *dev, void *data,
Chris Wilson9af90d12010-10-17 10:01:56 +01003860 struct drm_file *file,
Jesse Barnes76446ca2009-12-17 22:05:42 -05003861 struct drm_i915_gem_execbuffer2 *args,
3862 struct drm_i915_gem_exec_object2 *exec_list)
Eric Anholt673a3942008-07-30 12:06:12 -07003863{
3864 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00003865 struct drm_i915_gem_object **object_list = NULL;
3866 struct drm_i915_gem_object *batch_obj;
Eric Anholt201361a2009-03-11 12:30:04 -07003867 struct drm_clip_rect *cliprects = NULL;
Chris Wilson8dc5d142010-08-12 12:36:12 +01003868 struct drm_i915_gem_request *request = NULL;
Chris Wilson9af90d12010-10-17 10:01:56 +01003869 int ret, i, flips;
Eric Anholt673a3942008-07-30 12:06:12 -07003870 uint64_t exec_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003871
Zou Nan hai852835f2010-05-21 09:08:56 +08003872 struct intel_ring_buffer *ring = NULL;
3873
Chris Wilson30dbf0c2010-09-25 10:19:17 +01003874 ret = i915_gem_check_is_wedged(dev);
3875 if (ret)
3876 return ret;
3877
Chris Wilson2549d6c2010-10-14 12:10:41 +01003878 ret = validate_exec_list(exec_list, args->buffer_count);
3879 if (ret)
3880 return ret;
3881
Eric Anholt673a3942008-07-30 12:06:12 -07003882#if WATCH_EXEC
3883 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3884 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3885#endif
Chris Wilson549f7362010-10-19 11:19:32 +01003886 switch (args->flags & I915_EXEC_RING_MASK) {
3887 case I915_EXEC_DEFAULT:
3888 case I915_EXEC_RENDER:
3889 ring = &dev_priv->render_ring;
3890 break;
3891 case I915_EXEC_BSD:
Zou Nan haid1b851f2010-05-21 09:08:57 +08003892 if (!HAS_BSD(dev)) {
Chris Wilson549f7362010-10-19 11:19:32 +01003893 DRM_ERROR("execbuf with invalid ring (BSD)\n");
Zou Nan haid1b851f2010-05-21 09:08:57 +08003894 return -EINVAL;
3895 }
3896 ring = &dev_priv->bsd_ring;
Chris Wilson549f7362010-10-19 11:19:32 +01003897 break;
3898 case I915_EXEC_BLT:
3899 if (!HAS_BLT(dev)) {
3900 DRM_ERROR("execbuf with invalid ring (BLT)\n");
3901 return -EINVAL;
3902 }
3903 ring = &dev_priv->blt_ring;
3904 break;
3905 default:
3906 DRM_ERROR("execbuf with unknown ring: %d\n",
3907 (int)(args->flags & I915_EXEC_RING_MASK));
3908 return -EINVAL;
Zou Nan haid1b851f2010-05-21 09:08:57 +08003909 }
3910
Eric Anholt4f481ed2008-09-10 14:22:49 -07003911 if (args->buffer_count < 1) {
3912 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3913 return -EINVAL;
3914 }
Eric Anholtc8e0f932009-11-22 03:49:37 +01003915 object_list = drm_malloc_ab(sizeof(*object_list), args->buffer_count);
Jesse Barnes76446ca2009-12-17 22:05:42 -05003916 if (object_list == NULL) {
3917 DRM_ERROR("Failed to allocate object list for %d buffers\n",
Eric Anholt673a3942008-07-30 12:06:12 -07003918 args->buffer_count);
3919 ret = -ENOMEM;
3920 goto pre_mutex_err;
3921 }
Eric Anholt673a3942008-07-30 12:06:12 -07003922
Eric Anholt201361a2009-03-11 12:30:04 -07003923 if (args->num_cliprects != 0) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003924 cliprects = kcalloc(args->num_cliprects, sizeof(*cliprects),
3925 GFP_KERNEL);
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003926 if (cliprects == NULL) {
3927 ret = -ENOMEM;
Eric Anholt201361a2009-03-11 12:30:04 -07003928 goto pre_mutex_err;
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003929 }
Eric Anholt201361a2009-03-11 12:30:04 -07003930
3931 ret = copy_from_user(cliprects,
3932 (struct drm_clip_rect __user *)
3933 (uintptr_t) args->cliprects_ptr,
3934 sizeof(*cliprects) * args->num_cliprects);
3935 if (ret != 0) {
3936 DRM_ERROR("copy %d cliprects failed: %d\n",
3937 args->num_cliprects, ret);
Dan Carpenterc877cdce2010-06-23 19:03:01 +02003938 ret = -EFAULT;
Eric Anholt201361a2009-03-11 12:30:04 -07003939 goto pre_mutex_err;
3940 }
3941 }
3942
Chris Wilson8dc5d142010-08-12 12:36:12 +01003943 request = kzalloc(sizeof(*request), GFP_KERNEL);
3944 if (request == NULL) {
3945 ret = -ENOMEM;
Chris Wilsona198bc82009-02-06 16:55:20 +00003946 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003947 }
3948
Chris Wilson76c1dec2010-09-25 11:22:51 +01003949 ret = i915_mutex_lock_interruptible(dev);
3950 if (ret)
3951 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003952
Eric Anholt673a3942008-07-30 12:06:12 -07003953 if (dev_priv->mm.suspended) {
Eric Anholt673a3942008-07-30 12:06:12 -07003954 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003955 ret = -EBUSY;
3956 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003957 }
3958
Keith Packardac94a962008-11-20 23:30:27 -08003959 /* Look up object handles */
Eric Anholt673a3942008-07-30 12:06:12 -07003960 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson05394f32010-11-08 19:18:58 +00003961 struct drm_i915_gem_object *obj;
Chris Wilson7e318e12010-10-27 13:43:39 +01003962
Chris Wilson05394f32010-11-08 19:18:58 +00003963 obj = to_intel_bo (drm_gem_object_lookup(dev, file,
3964 exec_list[i].handle));
3965 if (obj == NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -07003966 DRM_ERROR("Invalid object handle %d at index %d\n",
3967 exec_list[i].handle, i);
Chris Wilson0ce907f2010-01-23 20:26:35 +00003968 /* prevent error path from reading uninitialized data */
Chris Wilson05394f32010-11-08 19:18:58 +00003969 args->buffer_count = i;
Chris Wilsonbf79cb92010-08-04 14:19:46 +01003970 ret = -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07003971 goto err;
3972 }
Chris Wilson05394f32010-11-08 19:18:58 +00003973 object_list[i] = obj;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003974
Chris Wilson05394f32010-11-08 19:18:58 +00003975 if (obj->in_execbuffer) {
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003976 DRM_ERROR("Object %p appears more than once in object list\n",
Chris Wilson05394f32010-11-08 19:18:58 +00003977 obj);
Chris Wilson0ce907f2010-01-23 20:26:35 +00003978 /* prevent error path from reading uninitialized data */
3979 args->buffer_count = i + 1;
Chris Wilsonbf79cb92010-08-04 14:19:46 +01003980 ret = -EINVAL;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003981 goto err;
3982 }
Chris Wilson05394f32010-11-08 19:18:58 +00003983 obj->in_execbuffer = true;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003984 }
3985
Chris Wilson9af90d12010-10-17 10:01:56 +01003986 /* Move the objects en-masse into the GTT, evicting if necessary. */
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003987 ret = i915_gem_execbuffer_reserve(dev, file,
3988 object_list, exec_list,
3989 args->buffer_count);
Chris Wilson9af90d12010-10-17 10:01:56 +01003990 if (ret)
3991 goto err;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003992
Chris Wilson9af90d12010-10-17 10:01:56 +01003993 /* The objects are in their final locations, apply the relocations. */
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003994 ret = i915_gem_execbuffer_relocate(dev, file,
3995 object_list, exec_list,
3996 args->buffer_count);
3997 if (ret) {
3998 if (ret == -EFAULT) {
3999 ret = i915_gem_execbuffer_relocate_slow(dev, file,
4000 object_list,
4001 exec_list,
4002 args->buffer_count);
4003 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
4004 }
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05004005 if (ret)
4006 goto err;
4007 }
4008
Eric Anholt673a3942008-07-30 12:06:12 -07004009 /* Set the pending read domains for the batch buffer to COMMAND */
4010 batch_obj = object_list[args->buffer_count-1];
Chris Wilson05394f32010-11-08 19:18:58 +00004011 if (batch_obj->base.pending_write_domain) {
Chris Wilson5f26a2c2009-06-06 09:45:58 +01004012 DRM_ERROR("Attempting to use self-modifying batch buffer\n");
4013 ret = -EINVAL;
4014 goto err;
4015 }
Chris Wilson05394f32010-11-08 19:18:58 +00004016 batch_obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
Eric Anholt673a3942008-07-30 12:06:12 -07004017
Chris Wilson9af90d12010-10-17 10:01:56 +01004018 /* Sanity check the batch buffer */
Chris Wilson05394f32010-11-08 19:18:58 +00004019 exec_offset = batch_obj->gtt_offset;
Chris Wilson9af90d12010-10-17 10:01:56 +01004020 ret = i915_gem_check_execbuffer(args, exec_offset);
Chris Wilson83d60792009-06-06 09:45:57 +01004021 if (ret != 0) {
4022 DRM_ERROR("execbuf with invalid offset/length\n");
4023 goto err;
4024 }
4025
Chris Wilson13b29282010-11-01 12:22:48 +00004026 ret = i915_gem_execbuffer_move_to_gpu(dev, file, ring,
4027 object_list, args->buffer_count);
4028 if (ret)
4029 goto err;
Eric Anholt673a3942008-07-30 12:06:12 -07004030
Eric Anholt673a3942008-07-30 12:06:12 -07004031#if WATCH_COHERENCY
4032 for (i = 0; i < args->buffer_count; i++) {
4033 i915_gem_object_check_coherency(object_list[i],
4034 exec_list[i].handle);
4035 }
4036#endif
4037
Eric Anholt673a3942008-07-30 12:06:12 -07004038#if WATCH_EXEC
Ben Gamari6911a9b2009-04-02 11:24:54 -07004039 i915_gem_dump_object(batch_obj,
Eric Anholt673a3942008-07-30 12:06:12 -07004040 args->batch_len,
4041 __func__,
4042 ~0);
4043#endif
4044
Chris Wilsone59f2ba2010-10-07 17:28:15 +01004045 /* Check for any pending flips. As we only maintain a flip queue depth
4046 * of 1, we can simply insert a WAIT for the next display flip prior
4047 * to executing the batch and avoid stalling the CPU.
4048 */
4049 flips = 0;
4050 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson05394f32010-11-08 19:18:58 +00004051 if (object_list[i]->base.write_domain)
4052 flips |= atomic_read(&object_list[i]->pending_flip);
Chris Wilsone59f2ba2010-10-07 17:28:15 +01004053 }
4054 if (flips) {
4055 int plane, flip_mask;
4056
4057 for (plane = 0; flips >> plane; plane++) {
4058 if (((flips >> plane) & 1) == 0)
4059 continue;
4060
4061 if (plane)
4062 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
4063 else
4064 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
4065
Chris Wilsone1f99ce2010-10-27 12:45:26 +01004066 ret = intel_ring_begin(ring, 2);
4067 if (ret)
4068 goto err;
4069
Chris Wilson78501ea2010-10-27 12:18:21 +01004070 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
4071 intel_ring_emit(ring, MI_NOOP);
4072 intel_ring_advance(ring);
Chris Wilsone59f2ba2010-10-07 17:28:15 +01004073 }
4074 }
4075
Eric Anholt673a3942008-07-30 12:06:12 -07004076 /* Exec the batchbuffer */
Chris Wilson78501ea2010-10-27 12:18:21 +01004077 ret = ring->dispatch_execbuffer(ring, args, cliprects, exec_offset);
Eric Anholt673a3942008-07-30 12:06:12 -07004078 if (ret) {
4079 DRM_ERROR("dispatch failed %d\n", ret);
4080 goto err;
4081 }
4082
Chris Wilson7e318e12010-10-27 13:43:39 +01004083 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson05394f32010-11-08 19:18:58 +00004084 struct drm_i915_gem_object *obj = object_list[i];
Chris Wilson7e318e12010-10-27 13:43:39 +01004085
Chris Wilson05394f32010-11-08 19:18:58 +00004086 obj->base.read_domains = obj->base.pending_read_domains;
4087 obj->base.write_domain = obj->base.pending_write_domain;
Chris Wilson7e318e12010-10-27 13:43:39 +01004088
4089 i915_gem_object_move_to_active(obj, ring);
Chris Wilson05394f32010-11-08 19:18:58 +00004090 if (obj->base.write_domain) {
4091 obj->dirty = 1;
4092 list_move_tail(&obj->gpu_write_list,
Chris Wilson7e318e12010-10-27 13:43:39 +01004093 &ring->gpu_write_list);
4094 intel_mark_busy(dev, obj);
4095 }
4096
4097 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00004098 obj->base.read_domains,
4099 obj->base.write_domain);
Chris Wilson7e318e12010-10-27 13:43:39 +01004100 }
4101
Eric Anholt673a3942008-07-30 12:06:12 -07004102 /*
4103 * Ensure that the commands in the batch buffer are
4104 * finished before the interrupt fires
4105 */
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01004106 i915_retire_commands(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07004107
Chris Wilson3cce4692010-10-27 16:11:02 +01004108 if (i915_add_request(dev, file, request, ring))
Chris Wilson5d97eb62010-11-10 20:40:02 +00004109 i915_gem_next_request_seqno(dev, ring);
Chris Wilson3cce4692010-10-27 16:11:02 +01004110 else
4111 request = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07004112
Eric Anholt673a3942008-07-30 12:06:12 -07004113err:
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05004114 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson05394f32010-11-08 19:18:58 +00004115 object_list[i]->in_execbuffer = false;
4116 drm_gem_object_unreference(&object_list[i]->base);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05004117 }
Julia Lawallaad87df2008-12-21 16:28:47 +01004118
Eric Anholt673a3942008-07-30 12:06:12 -07004119 mutex_unlock(&dev->struct_mutex);
4120
Chris Wilson93533c22010-01-31 10:40:48 +00004121pre_mutex_err:
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07004122 drm_free_large(object_list);
Eric Anholt9a298b22009-03-24 12:23:04 -07004123 kfree(cliprects);
Chris Wilson8dc5d142010-08-12 12:36:12 +01004124 kfree(request);
Eric Anholt673a3942008-07-30 12:06:12 -07004125
4126 return ret;
4127}
4128
Jesse Barnes76446ca2009-12-17 22:05:42 -05004129/*
4130 * Legacy execbuffer just creates an exec2 list from the original exec object
4131 * list array and passes it to the real function.
4132 */
4133int
4134i915_gem_execbuffer(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00004135 struct drm_file *file)
Jesse Barnes76446ca2009-12-17 22:05:42 -05004136{
4137 struct drm_i915_gem_execbuffer *args = data;
4138 struct drm_i915_gem_execbuffer2 exec2;
4139 struct drm_i915_gem_exec_object *exec_list = NULL;
4140 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
4141 int ret, i;
4142
4143#if WATCH_EXEC
4144 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
4145 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
4146#endif
4147
4148 if (args->buffer_count < 1) {
4149 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
4150 return -EINVAL;
4151 }
4152
4153 /* Copy in the exec list from userland */
4154 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
4155 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
4156 if (exec_list == NULL || exec2_list == NULL) {
4157 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
4158 args->buffer_count);
4159 drm_free_large(exec_list);
4160 drm_free_large(exec2_list);
4161 return -ENOMEM;
4162 }
4163 ret = copy_from_user(exec_list,
4164 (struct drm_i915_relocation_entry __user *)
4165 (uintptr_t) args->buffers_ptr,
4166 sizeof(*exec_list) * args->buffer_count);
4167 if (ret != 0) {
4168 DRM_ERROR("copy %d exec entries failed %d\n",
4169 args->buffer_count, ret);
4170 drm_free_large(exec_list);
4171 drm_free_large(exec2_list);
4172 return -EFAULT;
4173 }
4174
4175 for (i = 0; i < args->buffer_count; i++) {
4176 exec2_list[i].handle = exec_list[i].handle;
4177 exec2_list[i].relocation_count = exec_list[i].relocation_count;
4178 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
4179 exec2_list[i].alignment = exec_list[i].alignment;
4180 exec2_list[i].offset = exec_list[i].offset;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004181 if (INTEL_INFO(dev)->gen < 4)
Jesse Barnes76446ca2009-12-17 22:05:42 -05004182 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
4183 else
4184 exec2_list[i].flags = 0;
4185 }
4186
4187 exec2.buffers_ptr = args->buffers_ptr;
4188 exec2.buffer_count = args->buffer_count;
4189 exec2.batch_start_offset = args->batch_start_offset;
4190 exec2.batch_len = args->batch_len;
4191 exec2.DR1 = args->DR1;
4192 exec2.DR4 = args->DR4;
4193 exec2.num_cliprects = args->num_cliprects;
4194 exec2.cliprects_ptr = args->cliprects_ptr;
Zou Nan hai852835f2010-05-21 09:08:56 +08004195 exec2.flags = I915_EXEC_RENDER;
Jesse Barnes76446ca2009-12-17 22:05:42 -05004196
Chris Wilson05394f32010-11-08 19:18:58 +00004197 ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list);
Jesse Barnes76446ca2009-12-17 22:05:42 -05004198 if (!ret) {
4199 /* Copy the new buffer offsets back to the user's exec list. */
4200 for (i = 0; i < args->buffer_count; i++)
4201 exec_list[i].offset = exec2_list[i].offset;
4202 /* ... and back out to userspace */
4203 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
4204 (uintptr_t) args->buffers_ptr,
4205 exec_list,
4206 sizeof(*exec_list) * args->buffer_count);
4207 if (ret) {
4208 ret = -EFAULT;
4209 DRM_ERROR("failed to copy %d exec entries "
4210 "back to user (%d)\n",
4211 args->buffer_count, ret);
4212 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05004213 }
4214
4215 drm_free_large(exec_list);
4216 drm_free_large(exec2_list);
4217 return ret;
4218}
4219
4220int
4221i915_gem_execbuffer2(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00004222 struct drm_file *file)
Jesse Barnes76446ca2009-12-17 22:05:42 -05004223{
4224 struct drm_i915_gem_execbuffer2 *args = data;
4225 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
4226 int ret;
4227
4228#if WATCH_EXEC
4229 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
4230 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
4231#endif
4232
4233 if (args->buffer_count < 1) {
4234 DRM_ERROR("execbuf2 with %d buffers\n", args->buffer_count);
4235 return -EINVAL;
4236 }
4237
4238 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
4239 if (exec2_list == NULL) {
4240 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
4241 args->buffer_count);
4242 return -ENOMEM;
4243 }
4244 ret = copy_from_user(exec2_list,
4245 (struct drm_i915_relocation_entry __user *)
4246 (uintptr_t) args->buffers_ptr,
4247 sizeof(*exec2_list) * args->buffer_count);
4248 if (ret != 0) {
4249 DRM_ERROR("copy %d exec entries failed %d\n",
4250 args->buffer_count, ret);
4251 drm_free_large(exec2_list);
4252 return -EFAULT;
4253 }
4254
Chris Wilson05394f32010-11-08 19:18:58 +00004255 ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list);
Jesse Barnes76446ca2009-12-17 22:05:42 -05004256 if (!ret) {
4257 /* Copy the new buffer offsets back to the user's exec list. */
4258 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
4259 (uintptr_t) args->buffers_ptr,
4260 exec2_list,
4261 sizeof(*exec2_list) * args->buffer_count);
4262 if (ret) {
4263 ret = -EFAULT;
4264 DRM_ERROR("failed to copy %d exec entries "
4265 "back to user (%d)\n",
4266 args->buffer_count, ret);
4267 }
4268 }
4269
4270 drm_free_large(exec2_list);
4271 return ret;
4272}
4273
Eric Anholt673a3942008-07-30 12:06:12 -07004274int
Chris Wilson05394f32010-11-08 19:18:58 +00004275i915_gem_object_pin(struct drm_i915_gem_object *obj,
4276 uint32_t alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01004277 bool map_and_fenceable)
Eric Anholt673a3942008-07-30 12:06:12 -07004278{
Chris Wilson05394f32010-11-08 19:18:58 +00004279 struct drm_device *dev = obj->base.dev;
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004280 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07004281 int ret;
4282
Chris Wilson05394f32010-11-08 19:18:58 +00004283 BUG_ON(obj->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT);
Chris Wilson23bc5982010-09-29 16:10:57 +01004284 WARN_ON(i915_verify_lists(dev));
Chris Wilsonac0c6b52010-05-27 13:18:18 +01004285
Chris Wilson05394f32010-11-08 19:18:58 +00004286 if (obj->gtt_space != NULL) {
4287 if ((alignment && obj->gtt_offset & (alignment - 1)) ||
4288 (map_and_fenceable && !obj->map_and_fenceable)) {
4289 WARN(obj->pin_count,
Chris Wilsonae7d49d2010-08-04 12:37:41 +01004290 "bo is already pinned with incorrect alignment:"
Daniel Vetter75e9e912010-11-04 17:11:09 +01004291 " offset=%x, req.alignment=%x, req.map_and_fenceable=%d,"
4292 " obj->map_and_fenceable=%d\n",
Chris Wilson05394f32010-11-08 19:18:58 +00004293 obj->gtt_offset, alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01004294 map_and_fenceable,
Chris Wilson05394f32010-11-08 19:18:58 +00004295 obj->map_and_fenceable);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01004296 ret = i915_gem_object_unbind(obj);
4297 if (ret)
4298 return ret;
4299 }
4300 }
4301
Chris Wilson05394f32010-11-08 19:18:58 +00004302 if (obj->gtt_space == NULL) {
Chris Wilsona00b10c2010-09-24 21:15:47 +01004303 ret = i915_gem_object_bind_to_gtt(obj, alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01004304 map_and_fenceable);
Chris Wilson97311292009-09-21 00:22:34 +01004305 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07004306 return ret;
Chris Wilson22c344e2009-02-11 14:26:45 +00004307 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05004308
Chris Wilson05394f32010-11-08 19:18:58 +00004309 if (obj->pin_count++ == 0) {
4310 i915_gem_info_add_pin(dev_priv, obj, map_and_fenceable);
4311 if (!obj->active)
4312 list_move_tail(&obj->mm_list,
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004313 &dev_priv->mm.pinned_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004314 }
Chris Wilson05394f32010-11-08 19:18:58 +00004315 BUG_ON(!obj->pin_mappable && map_and_fenceable);
Eric Anholt673a3942008-07-30 12:06:12 -07004316
Chris Wilson23bc5982010-09-29 16:10:57 +01004317 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07004318 return 0;
4319}
4320
4321void
Chris Wilson05394f32010-11-08 19:18:58 +00004322i915_gem_object_unpin(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07004323{
Chris Wilson05394f32010-11-08 19:18:58 +00004324 struct drm_device *dev = obj->base.dev;
Eric Anholt673a3942008-07-30 12:06:12 -07004325 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07004326
Chris Wilson23bc5982010-09-29 16:10:57 +01004327 WARN_ON(i915_verify_lists(dev));
Chris Wilson05394f32010-11-08 19:18:58 +00004328 BUG_ON(obj->pin_count == 0);
4329 BUG_ON(obj->gtt_space == NULL);
Eric Anholt673a3942008-07-30 12:06:12 -07004330
Chris Wilson05394f32010-11-08 19:18:58 +00004331 if (--obj->pin_count == 0) {
4332 if (!obj->active)
4333 list_move_tail(&obj->mm_list,
Eric Anholt673a3942008-07-30 12:06:12 -07004334 &dev_priv->mm.inactive_list);
Chris Wilson05394f32010-11-08 19:18:58 +00004335 i915_gem_info_remove_pin(dev_priv, obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004336 }
Chris Wilson23bc5982010-09-29 16:10:57 +01004337 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07004338}
4339
4340int
4341i915_gem_pin_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00004342 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07004343{
4344 struct drm_i915_gem_pin *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004345 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07004346 int ret;
4347
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004348 ret = i915_mutex_lock_interruptible(dev);
4349 if (ret)
4350 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004351
Chris Wilson05394f32010-11-08 19:18:58 +00004352 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Eric Anholt673a3942008-07-30 12:06:12 -07004353 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004354 ret = -ENOENT;
4355 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07004356 }
Eric Anholt673a3942008-07-30 12:06:12 -07004357
Chris Wilson05394f32010-11-08 19:18:58 +00004358 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004359 DRM_ERROR("Attempting to pin a purgeable buffer\n");
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004360 ret = -EINVAL;
4361 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004362 }
4363
Chris Wilson05394f32010-11-08 19:18:58 +00004364 if (obj->pin_filp != NULL && obj->pin_filp != file) {
Jesse Barnes79e53942008-11-07 14:24:08 -08004365 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
4366 args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004367 ret = -EINVAL;
4368 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08004369 }
4370
Chris Wilson05394f32010-11-08 19:18:58 +00004371 obj->user_pin_count++;
4372 obj->pin_filp = file;
4373 if (obj->user_pin_count == 1) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01004374 ret = i915_gem_object_pin(obj, args->alignment, true);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004375 if (ret)
4376 goto out;
Eric Anholt673a3942008-07-30 12:06:12 -07004377 }
4378
4379 /* XXX - flush the CPU caches for pinned objects
4380 * as the X server doesn't manage domains yet
4381 */
Eric Anholte47c68e2008-11-14 13:35:19 -08004382 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00004383 args->offset = obj->gtt_offset;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004384out:
Chris Wilson05394f32010-11-08 19:18:58 +00004385 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004386unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07004387 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004388 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004389}
4390
4391int
4392i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00004393 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07004394{
4395 struct drm_i915_gem_pin *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004396 struct drm_i915_gem_object *obj;
Chris Wilson76c1dec2010-09-25 11:22:51 +01004397 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004398
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004399 ret = i915_mutex_lock_interruptible(dev);
4400 if (ret)
4401 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004402
Chris Wilson05394f32010-11-08 19:18:58 +00004403 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Eric Anholt673a3942008-07-30 12:06:12 -07004404 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004405 ret = -ENOENT;
4406 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07004407 }
Chris Wilson76c1dec2010-09-25 11:22:51 +01004408
Chris Wilson05394f32010-11-08 19:18:58 +00004409 if (obj->pin_filp != file) {
Jesse Barnes79e53942008-11-07 14:24:08 -08004410 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
4411 args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004412 ret = -EINVAL;
4413 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08004414 }
Chris Wilson05394f32010-11-08 19:18:58 +00004415 obj->user_pin_count--;
4416 if (obj->user_pin_count == 0) {
4417 obj->pin_filp = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08004418 i915_gem_object_unpin(obj);
4419 }
Eric Anholt673a3942008-07-30 12:06:12 -07004420
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004421out:
Chris Wilson05394f32010-11-08 19:18:58 +00004422 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004423unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07004424 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004425 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004426}
4427
4428int
4429i915_gem_busy_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00004430 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07004431{
4432 struct drm_i915_gem_busy *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004433 struct drm_i915_gem_object *obj;
Chris Wilson30dbf0c2010-09-25 10:19:17 +01004434 int ret;
4435
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004436 ret = i915_mutex_lock_interruptible(dev);
4437 if (ret)
4438 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004439
Chris Wilson05394f32010-11-08 19:18:58 +00004440 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Eric Anholt673a3942008-07-30 12:06:12 -07004441 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004442 ret = -ENOENT;
4443 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07004444 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08004445
Chris Wilson0be555b2010-08-04 15:36:30 +01004446 /* Count all active objects as busy, even if they are currently not used
4447 * by the gpu. Users of this interface expect objects to eventually
4448 * become non-busy without any further actions, therefore emit any
4449 * necessary flushes here.
Eric Anholtc4de0a52008-12-14 19:05:04 -08004450 */
Chris Wilson05394f32010-11-08 19:18:58 +00004451 args->busy = obj->active;
Chris Wilson0be555b2010-08-04 15:36:30 +01004452 if (args->busy) {
4453 /* Unconditionally flush objects, even when the gpu still uses this
4454 * object. Userspace calling this function indicates that it wants to
4455 * use this buffer rather sooner than later, so issuing the required
4456 * flush earlier is beneficial.
4457 */
Chris Wilson05394f32010-11-08 19:18:58 +00004458 if (obj->base.write_domain & I915_GEM_GPU_DOMAINS)
4459 i915_gem_flush_ring(dev, obj->ring,
4460 0, obj->base.write_domain);
Chris Wilson0be555b2010-08-04 15:36:30 +01004461
4462 /* Update the active list for the hardware's current position.
4463 * Otherwise this only updates on a delayed timer or when irqs
4464 * are actually unmasked, and our working set ends up being
4465 * larger than required.
4466 */
Chris Wilson05394f32010-11-08 19:18:58 +00004467 i915_gem_retire_requests_ring(dev, obj->ring);
Chris Wilson0be555b2010-08-04 15:36:30 +01004468
Chris Wilson05394f32010-11-08 19:18:58 +00004469 args->busy = obj->active;
Chris Wilson0be555b2010-08-04 15:36:30 +01004470 }
Eric Anholt673a3942008-07-30 12:06:12 -07004471
Chris Wilson05394f32010-11-08 19:18:58 +00004472 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004473unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07004474 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004475 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004476}
4477
4478int
4479i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4480 struct drm_file *file_priv)
4481{
4482 return i915_gem_ring_throttle(dev, file_priv);
4483}
4484
Chris Wilson3ef94da2009-09-14 16:50:29 +01004485int
4486i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4487 struct drm_file *file_priv)
4488{
4489 struct drm_i915_gem_madvise *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004490 struct drm_i915_gem_object *obj;
Chris Wilson76c1dec2010-09-25 11:22:51 +01004491 int ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004492
4493 switch (args->madv) {
4494 case I915_MADV_DONTNEED:
4495 case I915_MADV_WILLNEED:
4496 break;
4497 default:
4498 return -EINVAL;
4499 }
4500
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004501 ret = i915_mutex_lock_interruptible(dev);
4502 if (ret)
4503 return ret;
4504
Chris Wilson05394f32010-11-08 19:18:58 +00004505 obj = to_intel_bo(drm_gem_object_lookup(dev, file_priv, args->handle));
Chris Wilson3ef94da2009-09-14 16:50:29 +01004506 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004507 ret = -ENOENT;
4508 goto unlock;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004509 }
Chris Wilson3ef94da2009-09-14 16:50:29 +01004510
Chris Wilson05394f32010-11-08 19:18:58 +00004511 if (obj->pin_count) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004512 ret = -EINVAL;
4513 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004514 }
4515
Chris Wilson05394f32010-11-08 19:18:58 +00004516 if (obj->madv != __I915_MADV_PURGED)
4517 obj->madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004518
Chris Wilson2d7ef392009-09-20 23:13:10 +01004519 /* if the object is no longer bound, discard its backing storage */
Chris Wilson05394f32010-11-08 19:18:58 +00004520 if (i915_gem_object_is_purgeable(obj) &&
4521 obj->gtt_space == NULL)
Chris Wilson2d7ef392009-09-20 23:13:10 +01004522 i915_gem_object_truncate(obj);
4523
Chris Wilson05394f32010-11-08 19:18:58 +00004524 args->retained = obj->madv != __I915_MADV_PURGED;
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004525
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004526out:
Chris Wilson05394f32010-11-08 19:18:58 +00004527 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004528unlock:
Chris Wilson3ef94da2009-09-14 16:50:29 +01004529 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004530 return ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004531}
4532
Chris Wilson05394f32010-11-08 19:18:58 +00004533struct drm_i915_gem_object *i915_gem_alloc_object(struct drm_device *dev,
4534 size_t size)
Daniel Vetterac52bc52010-04-09 19:05:06 +00004535{
Chris Wilson73aa8082010-09-30 11:46:12 +01004536 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc397b902010-04-09 19:05:07 +00004537 struct drm_i915_gem_object *obj;
4538
4539 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
4540 if (obj == NULL)
4541 return NULL;
4542
4543 if (drm_gem_object_init(dev, &obj->base, size) != 0) {
4544 kfree(obj);
4545 return NULL;
4546 }
4547
Chris Wilson73aa8082010-09-30 11:46:12 +01004548 i915_gem_info_add_obj(dev_priv, size);
4549
Daniel Vetterc397b902010-04-09 19:05:07 +00004550 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4551 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4552
4553 obj->agp_type = AGP_USER_MEMORY;
Daniel Vetter62b8b212010-04-09 19:05:08 +00004554 obj->base.driver_private = NULL;
Daniel Vetterc397b902010-04-09 19:05:07 +00004555 obj->fence_reg = I915_FENCE_REG_NONE;
Chris Wilson69dc4982010-10-19 10:36:51 +01004556 INIT_LIST_HEAD(&obj->mm_list);
Daniel Vetter93a37f22010-11-05 20:24:53 +01004557 INIT_LIST_HEAD(&obj->gtt_list);
Chris Wilson69dc4982010-10-19 10:36:51 +01004558 INIT_LIST_HEAD(&obj->ring_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00004559 INIT_LIST_HEAD(&obj->gpu_write_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00004560 obj->madv = I915_MADV_WILLNEED;
Daniel Vetter75e9e912010-11-04 17:11:09 +01004561 /* Avoid an unnecessary call to unbind on the first bind. */
4562 obj->map_and_fenceable = true;
Daniel Vetterc397b902010-04-09 19:05:07 +00004563
Chris Wilson05394f32010-11-08 19:18:58 +00004564 return obj;
Daniel Vetterac52bc52010-04-09 19:05:06 +00004565}
4566
Eric Anholt673a3942008-07-30 12:06:12 -07004567int i915_gem_init_object(struct drm_gem_object *obj)
4568{
Daniel Vetterc397b902010-04-09 19:05:07 +00004569 BUG();
Jesse Barnesde151cf2008-11-12 10:03:55 -08004570
Eric Anholt673a3942008-07-30 12:06:12 -07004571 return 0;
4572}
4573
Chris Wilson05394f32010-11-08 19:18:58 +00004574static void i915_gem_free_object_tail(struct drm_i915_gem_object *obj)
Chris Wilsonbe726152010-07-23 23:18:50 +01004575{
Chris Wilson05394f32010-11-08 19:18:58 +00004576 struct drm_device *dev = obj->base.dev;
Chris Wilsonbe726152010-07-23 23:18:50 +01004577 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonbe726152010-07-23 23:18:50 +01004578 int ret;
4579
4580 ret = i915_gem_object_unbind(obj);
4581 if (ret == -ERESTARTSYS) {
Chris Wilson05394f32010-11-08 19:18:58 +00004582 list_move(&obj->mm_list,
Chris Wilsonbe726152010-07-23 23:18:50 +01004583 &dev_priv->mm.deferred_free_list);
4584 return;
4585 }
4586
Chris Wilson05394f32010-11-08 19:18:58 +00004587 if (obj->base.map_list.map)
Chris Wilsonbe726152010-07-23 23:18:50 +01004588 i915_gem_free_mmap_offset(obj);
4589
Chris Wilson05394f32010-11-08 19:18:58 +00004590 drm_gem_object_release(&obj->base);
4591 i915_gem_info_remove_obj(dev_priv, obj->base.size);
Chris Wilsonbe726152010-07-23 23:18:50 +01004592
Chris Wilson05394f32010-11-08 19:18:58 +00004593 kfree(obj->page_cpu_valid);
4594 kfree(obj->bit_17);
4595 kfree(obj);
Chris Wilsonbe726152010-07-23 23:18:50 +01004596}
4597
Chris Wilson05394f32010-11-08 19:18:58 +00004598void i915_gem_free_object(struct drm_gem_object *gem_obj)
Eric Anholt673a3942008-07-30 12:06:12 -07004599{
Chris Wilson05394f32010-11-08 19:18:58 +00004600 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
4601 struct drm_device *dev = obj->base.dev;
Eric Anholt673a3942008-07-30 12:06:12 -07004602
Chris Wilson1c5d22f2009-08-25 11:15:50 +01004603 trace_i915_gem_object_destroy(obj);
4604
Chris Wilson05394f32010-11-08 19:18:58 +00004605 while (obj->pin_count > 0)
Eric Anholt673a3942008-07-30 12:06:12 -07004606 i915_gem_object_unpin(obj);
4607
Chris Wilson05394f32010-11-08 19:18:58 +00004608 if (obj->phys_obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004609 i915_gem_detach_phys_object(dev, obj);
4610
Chris Wilsonbe726152010-07-23 23:18:50 +01004611 i915_gem_free_object_tail(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004612}
4613
Jesse Barnes5669fca2009-02-17 15:13:31 -08004614int
Eric Anholt673a3942008-07-30 12:06:12 -07004615i915_gem_idle(struct drm_device *dev)
4616{
4617 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson29105cc2010-01-07 10:39:13 +00004618 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004619
Keith Packard6dbe2772008-10-14 21:41:13 -07004620 mutex_lock(&dev->struct_mutex);
4621
Chris Wilson87acb0a2010-10-19 10:13:00 +01004622 if (dev_priv->mm.suspended) {
Keith Packard6dbe2772008-10-14 21:41:13 -07004623 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004624 return 0;
Keith Packard6dbe2772008-10-14 21:41:13 -07004625 }
Eric Anholt673a3942008-07-30 12:06:12 -07004626
Chris Wilson29105cc2010-01-07 10:39:13 +00004627 ret = i915_gpu_idle(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004628 if (ret) {
4629 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004630 return ret;
Keith Packard6dbe2772008-10-14 21:41:13 -07004631 }
Eric Anholt673a3942008-07-30 12:06:12 -07004632
Chris Wilson29105cc2010-01-07 10:39:13 +00004633 /* Under UMS, be paranoid and evict. */
4634 if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
Chris Wilson5eac3ab2010-10-31 08:49:47 +00004635 ret = i915_gem_evict_inactive(dev, false);
Chris Wilson29105cc2010-01-07 10:39:13 +00004636 if (ret) {
4637 mutex_unlock(&dev->struct_mutex);
4638 return ret;
4639 }
4640 }
4641
4642 /* Hack! Don't let anybody do execbuf while we don't control the chip.
4643 * We need to replace this with a semaphore, or something.
4644 * And not confound mm.suspended!
4645 */
4646 dev_priv->mm.suspended = 1;
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02004647 del_timer_sync(&dev_priv->hangcheck_timer);
Chris Wilson29105cc2010-01-07 10:39:13 +00004648
4649 i915_kernel_lost_context(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004650 i915_gem_cleanup_ringbuffer(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00004651
Keith Packard6dbe2772008-10-14 21:41:13 -07004652 mutex_unlock(&dev->struct_mutex);
4653
Chris Wilson29105cc2010-01-07 10:39:13 +00004654 /* Cancel the retire work handler, which should be idle now. */
4655 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
4656
Eric Anholt673a3942008-07-30 12:06:12 -07004657 return 0;
4658}
4659
Eric Anholt673a3942008-07-30 12:06:12 -07004660int
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004661i915_gem_init_ringbuffer(struct drm_device *dev)
4662{
4663 drm_i915_private_t *dev_priv = dev->dev_private;
4664 int ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01004665
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08004666 ret = intel_init_render_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004667 if (ret)
Chris Wilsonb6913e42010-11-12 10:46:37 +00004668 return ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01004669
4670 if (HAS_BSD(dev)) {
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08004671 ret = intel_init_bsd_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004672 if (ret)
4673 goto cleanup_render_ring;
Zou Nan haid1b851f2010-05-21 09:08:57 +08004674 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004675
Chris Wilson549f7362010-10-19 11:19:32 +01004676 if (HAS_BLT(dev)) {
4677 ret = intel_init_blt_ring_buffer(dev);
4678 if (ret)
4679 goto cleanup_bsd_ring;
4680 }
4681
Chris Wilson6f392d52010-08-07 11:01:22 +01004682 dev_priv->next_seqno = 1;
4683
Chris Wilson68f95ba2010-05-27 13:18:22 +01004684 return 0;
4685
Chris Wilson549f7362010-10-19 11:19:32 +01004686cleanup_bsd_ring:
Chris Wilson78501ea2010-10-27 12:18:21 +01004687 intel_cleanup_ring_buffer(&dev_priv->bsd_ring);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004688cleanup_render_ring:
Chris Wilson78501ea2010-10-27 12:18:21 +01004689 intel_cleanup_ring_buffer(&dev_priv->render_ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004690 return ret;
4691}
4692
4693void
4694i915_gem_cleanup_ringbuffer(struct drm_device *dev)
4695{
4696 drm_i915_private_t *dev_priv = dev->dev_private;
4697
Chris Wilson78501ea2010-10-27 12:18:21 +01004698 intel_cleanup_ring_buffer(&dev_priv->render_ring);
4699 intel_cleanup_ring_buffer(&dev_priv->bsd_ring);
4700 intel_cleanup_ring_buffer(&dev_priv->blt_ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004701}
4702
4703int
Eric Anholt673a3942008-07-30 12:06:12 -07004704i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
4705 struct drm_file *file_priv)
4706{
4707 drm_i915_private_t *dev_priv = dev->dev_private;
4708 int ret;
4709
Jesse Barnes79e53942008-11-07 14:24:08 -08004710 if (drm_core_check_feature(dev, DRIVER_MODESET))
4711 return 0;
4712
Ben Gamariba1234d2009-09-14 17:48:47 -04004713 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07004714 DRM_ERROR("Reenabling wedged hardware, good luck\n");
Ben Gamariba1234d2009-09-14 17:48:47 -04004715 atomic_set(&dev_priv->mm.wedged, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004716 }
4717
Eric Anholt673a3942008-07-30 12:06:12 -07004718 mutex_lock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004719 dev_priv->mm.suspended = 0;
4720
4721 ret = i915_gem_init_ringbuffer(dev);
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004722 if (ret != 0) {
4723 mutex_unlock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004724 return ret;
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004725 }
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004726
Chris Wilson69dc4982010-10-19 10:36:51 +01004727 BUG_ON(!list_empty(&dev_priv->mm.active_list));
Zou Nan hai852835f2010-05-21 09:08:56 +08004728 BUG_ON(!list_empty(&dev_priv->render_ring.active_list));
Chris Wilson87acb0a2010-10-19 10:13:00 +01004729 BUG_ON(!list_empty(&dev_priv->bsd_ring.active_list));
Chris Wilson549f7362010-10-19 11:19:32 +01004730 BUG_ON(!list_empty(&dev_priv->blt_ring.active_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004731 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
4732 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
Zou Nan hai852835f2010-05-21 09:08:56 +08004733 BUG_ON(!list_empty(&dev_priv->render_ring.request_list));
Chris Wilson87acb0a2010-10-19 10:13:00 +01004734 BUG_ON(!list_empty(&dev_priv->bsd_ring.request_list));
Chris Wilson549f7362010-10-19 11:19:32 +01004735 BUG_ON(!list_empty(&dev_priv->blt_ring.request_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004736 mutex_unlock(&dev->struct_mutex);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004737
Chris Wilson5f353082010-06-07 14:03:03 +01004738 ret = drm_irq_install(dev);
4739 if (ret)
4740 goto cleanup_ringbuffer;
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004741
Eric Anholt673a3942008-07-30 12:06:12 -07004742 return 0;
Chris Wilson5f353082010-06-07 14:03:03 +01004743
4744cleanup_ringbuffer:
4745 mutex_lock(&dev->struct_mutex);
4746 i915_gem_cleanup_ringbuffer(dev);
4747 dev_priv->mm.suspended = 1;
4748 mutex_unlock(&dev->struct_mutex);
4749
4750 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004751}
4752
4753int
4754i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
4755 struct drm_file *file_priv)
4756{
Jesse Barnes79e53942008-11-07 14:24:08 -08004757 if (drm_core_check_feature(dev, DRIVER_MODESET))
4758 return 0;
4759
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004760 drm_irq_uninstall(dev);
Linus Torvaldse6890f62009-09-08 17:09:24 -07004761 return i915_gem_idle(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004762}
4763
4764void
4765i915_gem_lastclose(struct drm_device *dev)
4766{
4767 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004768
Eric Anholte806b492009-01-22 09:56:58 -08004769 if (drm_core_check_feature(dev, DRIVER_MODESET))
4770 return;
4771
Keith Packard6dbe2772008-10-14 21:41:13 -07004772 ret = i915_gem_idle(dev);
4773 if (ret)
4774 DRM_ERROR("failed to idle hardware: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07004775}
4776
Chris Wilson64193402010-10-24 12:38:05 +01004777static void
4778init_ring_lists(struct intel_ring_buffer *ring)
4779{
4780 INIT_LIST_HEAD(&ring->active_list);
4781 INIT_LIST_HEAD(&ring->request_list);
4782 INIT_LIST_HEAD(&ring->gpu_write_list);
4783}
4784
Eric Anholt673a3942008-07-30 12:06:12 -07004785void
4786i915_gem_load(struct drm_device *dev)
4787{
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004788 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07004789 drm_i915_private_t *dev_priv = dev->dev_private;
4790
Chris Wilson69dc4982010-10-19 10:36:51 +01004791 INIT_LIST_HEAD(&dev_priv->mm.active_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004792 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
4793 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004794 INIT_LIST_HEAD(&dev_priv->mm.pinned_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07004795 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Chris Wilsonbe726152010-07-23 23:18:50 +01004796 INIT_LIST_HEAD(&dev_priv->mm.deferred_free_list);
Daniel Vetter93a37f22010-11-05 20:24:53 +01004797 INIT_LIST_HEAD(&dev_priv->mm.gtt_list);
Chris Wilson64193402010-10-24 12:38:05 +01004798 init_ring_lists(&dev_priv->render_ring);
4799 init_ring_lists(&dev_priv->bsd_ring);
4800 init_ring_lists(&dev_priv->blt_ring);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02004801 for (i = 0; i < 16; i++)
4802 INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004803 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
4804 i915_gem_retire_work_handler);
Chris Wilson30dbf0c2010-09-25 10:19:17 +01004805 init_completion(&dev_priv->error_completion);
Chris Wilson31169712009-09-14 16:50:28 +01004806
Dave Airlie94400122010-07-20 13:15:31 +10004807 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
4808 if (IS_GEN3(dev)) {
4809 u32 tmp = I915_READ(MI_ARB_STATE);
4810 if (!(tmp & MI_ARB_C3_LP_WRITE_ENABLE)) {
4811 /* arb state is a masked write, so set bit + bit in mask */
4812 tmp = MI_ARB_C3_LP_WRITE_ENABLE | (MI_ARB_C3_LP_WRITE_ENABLE << MI_ARB_MASK_SHIFT);
4813 I915_WRITE(MI_ARB_STATE, tmp);
4814 }
4815 }
4816
Jesse Barnesde151cf2008-11-12 10:03:55 -08004817 /* Old X drivers will take 0-2 for front, back, depth buffers */
Eric Anholtb397c832010-01-26 09:43:10 -08004818 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4819 dev_priv->fence_reg_start = 3;
Jesse Barnesde151cf2008-11-12 10:03:55 -08004820
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004821 if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08004822 dev_priv->num_fence_regs = 16;
4823 else
4824 dev_priv->num_fence_regs = 8;
4825
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004826 /* Initialize fence registers to zero */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004827 switch (INTEL_INFO(dev)->gen) {
4828 case 6:
4829 for (i = 0; i < 16; i++)
4830 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (i * 8), 0);
4831 break;
4832 case 5:
4833 case 4:
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004834 for (i = 0; i < 16; i++)
4835 I915_WRITE64(FENCE_REG_965_0 + (i * 8), 0);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004836 break;
4837 case 3:
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004838 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4839 for (i = 0; i < 8; i++)
4840 I915_WRITE(FENCE_REG_945_8 + (i * 4), 0);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004841 case 2:
4842 for (i = 0; i < 8; i++)
4843 I915_WRITE(FENCE_REG_830_0 + (i * 4), 0);
4844 break;
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004845 }
Eric Anholt673a3942008-07-30 12:06:12 -07004846 i915_gem_detect_bit_6_swizzle(dev);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05004847 init_waitqueue_head(&dev_priv->pending_flip_queue);
Chris Wilson17250b72010-10-28 12:51:39 +01004848
4849 dev_priv->mm.inactive_shrinker.shrink = i915_gem_inactive_shrink;
4850 dev_priv->mm.inactive_shrinker.seeks = DEFAULT_SEEKS;
4851 register_shrinker(&dev_priv->mm.inactive_shrinker);
Eric Anholt673a3942008-07-30 12:06:12 -07004852}
Dave Airlie71acb5e2008-12-30 20:31:46 +10004853
4854/*
4855 * Create a physically contiguous memory object for this object
4856 * e.g. for cursor + overlay regs
4857 */
Chris Wilson995b6762010-08-20 13:23:26 +01004858static int i915_gem_init_phys_object(struct drm_device *dev,
4859 int id, int size, int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004860{
4861 drm_i915_private_t *dev_priv = dev->dev_private;
4862 struct drm_i915_gem_phys_object *phys_obj;
4863 int ret;
4864
4865 if (dev_priv->mm.phys_objs[id - 1] || !size)
4866 return 0;
4867
Eric Anholt9a298b22009-03-24 12:23:04 -07004868 phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004869 if (!phys_obj)
4870 return -ENOMEM;
4871
4872 phys_obj->id = id;
4873
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004874 phys_obj->handle = drm_pci_alloc(dev, size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004875 if (!phys_obj->handle) {
4876 ret = -ENOMEM;
4877 goto kfree_obj;
4878 }
4879#ifdef CONFIG_X86
4880 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4881#endif
4882
4883 dev_priv->mm.phys_objs[id - 1] = phys_obj;
4884
4885 return 0;
4886kfree_obj:
Eric Anholt9a298b22009-03-24 12:23:04 -07004887 kfree(phys_obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004888 return ret;
4889}
4890
Chris Wilson995b6762010-08-20 13:23:26 +01004891static void i915_gem_free_phys_object(struct drm_device *dev, int id)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004892{
4893 drm_i915_private_t *dev_priv = dev->dev_private;
4894 struct drm_i915_gem_phys_object *phys_obj;
4895
4896 if (!dev_priv->mm.phys_objs[id - 1])
4897 return;
4898
4899 phys_obj = dev_priv->mm.phys_objs[id - 1];
4900 if (phys_obj->cur_obj) {
4901 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
4902 }
4903
4904#ifdef CONFIG_X86
4905 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4906#endif
4907 drm_pci_free(dev, phys_obj->handle);
4908 kfree(phys_obj);
4909 dev_priv->mm.phys_objs[id - 1] = NULL;
4910}
4911
4912void i915_gem_free_all_phys_object(struct drm_device *dev)
4913{
4914 int i;
4915
Dave Airlie260883c2009-01-22 17:58:49 +10004916 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004917 i915_gem_free_phys_object(dev, i);
4918}
4919
4920void i915_gem_detach_phys_object(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00004921 struct drm_i915_gem_object *obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004922{
Chris Wilson05394f32010-11-08 19:18:58 +00004923 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Chris Wilsone5281cc2010-10-28 13:45:36 +01004924 char *vaddr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004925 int i;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004926 int page_count;
4927
Chris Wilson05394f32010-11-08 19:18:58 +00004928 if (!obj->phys_obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004929 return;
Chris Wilson05394f32010-11-08 19:18:58 +00004930 vaddr = obj->phys_obj->handle->vaddr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004931
Chris Wilson05394f32010-11-08 19:18:58 +00004932 page_count = obj->base.size / PAGE_SIZE;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004933 for (i = 0; i < page_count; i++) {
Chris Wilsone5281cc2010-10-28 13:45:36 +01004934 struct page *page = read_cache_page_gfp(mapping, i,
4935 GFP_HIGHUSER | __GFP_RECLAIMABLE);
4936 if (!IS_ERR(page)) {
4937 char *dst = kmap_atomic(page);
4938 memcpy(dst, vaddr + i*PAGE_SIZE, PAGE_SIZE);
4939 kunmap_atomic(dst);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004940
Chris Wilsone5281cc2010-10-28 13:45:36 +01004941 drm_clflush_pages(&page, 1);
4942
4943 set_page_dirty(page);
4944 mark_page_accessed(page);
4945 page_cache_release(page);
4946 }
Dave Airlie71acb5e2008-12-30 20:31:46 +10004947 }
Daniel Vetter40ce6572010-11-05 18:12:18 +01004948 intel_gtt_chipset_flush();
Chris Wilsond78b47b2009-06-17 21:52:49 +01004949
Chris Wilson05394f32010-11-08 19:18:58 +00004950 obj->phys_obj->cur_obj = NULL;
4951 obj->phys_obj = NULL;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004952}
4953
4954int
4955i915_gem_attach_phys_object(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00004956 struct drm_i915_gem_object *obj,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004957 int id,
4958 int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004959{
Chris Wilson05394f32010-11-08 19:18:58 +00004960 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004961 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004962 int ret = 0;
4963 int page_count;
4964 int i;
4965
4966 if (id > I915_MAX_PHYS_OBJECT)
4967 return -EINVAL;
4968
Chris Wilson05394f32010-11-08 19:18:58 +00004969 if (obj->phys_obj) {
4970 if (obj->phys_obj->id == id)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004971 return 0;
4972 i915_gem_detach_phys_object(dev, obj);
4973 }
4974
Dave Airlie71acb5e2008-12-30 20:31:46 +10004975 /* create a new object */
4976 if (!dev_priv->mm.phys_objs[id - 1]) {
4977 ret = i915_gem_init_phys_object(dev, id,
Chris Wilson05394f32010-11-08 19:18:58 +00004978 obj->base.size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004979 if (ret) {
Chris Wilson05394f32010-11-08 19:18:58 +00004980 DRM_ERROR("failed to init phys object %d size: %zu\n",
4981 id, obj->base.size);
Chris Wilsone5281cc2010-10-28 13:45:36 +01004982 return ret;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004983 }
4984 }
4985
4986 /* bind to the object */
Chris Wilson05394f32010-11-08 19:18:58 +00004987 obj->phys_obj = dev_priv->mm.phys_objs[id - 1];
4988 obj->phys_obj->cur_obj = obj;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004989
Chris Wilson05394f32010-11-08 19:18:58 +00004990 page_count = obj->base.size / PAGE_SIZE;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004991
4992 for (i = 0; i < page_count; i++) {
Chris Wilsone5281cc2010-10-28 13:45:36 +01004993 struct page *page;
4994 char *dst, *src;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004995
Chris Wilsone5281cc2010-10-28 13:45:36 +01004996 page = read_cache_page_gfp(mapping, i,
4997 GFP_HIGHUSER | __GFP_RECLAIMABLE);
4998 if (IS_ERR(page))
4999 return PTR_ERR(page);
5000
Chris Wilsonff75b9b2010-10-30 22:52:31 +01005001 src = kmap_atomic(page);
Chris Wilson05394f32010-11-08 19:18:58 +00005002 dst = obj->phys_obj->handle->vaddr + (i * PAGE_SIZE);
Dave Airlie71acb5e2008-12-30 20:31:46 +10005003 memcpy(dst, src, PAGE_SIZE);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -07005004 kunmap_atomic(src);
Chris Wilsone5281cc2010-10-28 13:45:36 +01005005
5006 mark_page_accessed(page);
5007 page_cache_release(page);
Dave Airlie71acb5e2008-12-30 20:31:46 +10005008 }
5009
5010 return 0;
Dave Airlie71acb5e2008-12-30 20:31:46 +10005011}
5012
5013static int
Chris Wilson05394f32010-11-08 19:18:58 +00005014i915_gem_phys_pwrite(struct drm_device *dev,
5015 struct drm_i915_gem_object *obj,
Dave Airlie71acb5e2008-12-30 20:31:46 +10005016 struct drm_i915_gem_pwrite *args,
5017 struct drm_file *file_priv)
5018{
Chris Wilson05394f32010-11-08 19:18:58 +00005019 void *vaddr = obj->phys_obj->handle->vaddr + args->offset;
Chris Wilsonb47b30c2010-11-08 01:12:29 +00005020 char __user *user_data = (char __user *) (uintptr_t) args->data_ptr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10005021
Chris Wilsonb47b30c2010-11-08 01:12:29 +00005022 if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) {
5023 unsigned long unwritten;
5024
5025 /* The physical object once assigned is fixed for the lifetime
5026 * of the obj, so we can safely drop the lock and continue
5027 * to access vaddr.
5028 */
5029 mutex_unlock(&dev->struct_mutex);
5030 unwritten = copy_from_user(vaddr, user_data, args->size);
5031 mutex_lock(&dev->struct_mutex);
5032 if (unwritten)
5033 return -EFAULT;
5034 }
Dave Airlie71acb5e2008-12-30 20:31:46 +10005035
Daniel Vetter40ce6572010-11-05 18:12:18 +01005036 intel_gtt_chipset_flush();
Dave Airlie71acb5e2008-12-30 20:31:46 +10005037 return 0;
5038}
Eric Anholtb9624422009-06-03 07:27:35 +00005039
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005040void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00005041{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005042 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00005043
5044 /* Clean up our request list when the client is going away, so that
5045 * later retire_requests won't dereference our soon-to-be-gone
5046 * file_priv.
5047 */
Chris Wilson1c255952010-09-26 11:03:27 +01005048 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005049 while (!list_empty(&file_priv->mm.request_list)) {
5050 struct drm_i915_gem_request *request;
5051
5052 request = list_first_entry(&file_priv->mm.request_list,
5053 struct drm_i915_gem_request,
5054 client_list);
5055 list_del(&request->client_list);
5056 request->file_priv = NULL;
5057 }
Chris Wilson1c255952010-09-26 11:03:27 +01005058 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00005059}
Chris Wilson31169712009-09-14 16:50:28 +01005060
Chris Wilson31169712009-09-14 16:50:28 +01005061static int
Chris Wilson1637ef42010-04-20 17:10:35 +01005062i915_gpu_is_active(struct drm_device *dev)
5063{
5064 drm_i915_private_t *dev_priv = dev->dev_private;
5065 int lists_empty;
5066
Chris Wilson1637ef42010-04-20 17:10:35 +01005067 lists_empty = list_empty(&dev_priv->mm.flushing_list) &&
Chris Wilson17250b72010-10-28 12:51:39 +01005068 list_empty(&dev_priv->mm.active_list);
Chris Wilson1637ef42010-04-20 17:10:35 +01005069
5070 return !lists_empty;
5071}
5072
5073static int
Chris Wilson17250b72010-10-28 12:51:39 +01005074i915_gem_inactive_shrink(struct shrinker *shrinker,
5075 int nr_to_scan,
5076 gfp_t gfp_mask)
Chris Wilson31169712009-09-14 16:50:28 +01005077{
Chris Wilson17250b72010-10-28 12:51:39 +01005078 struct drm_i915_private *dev_priv =
5079 container_of(shrinker,
5080 struct drm_i915_private,
5081 mm.inactive_shrinker);
5082 struct drm_device *dev = dev_priv->dev;
5083 struct drm_i915_gem_object *obj, *next;
5084 int cnt;
5085
5086 if (!mutex_trylock(&dev->struct_mutex))
Chris Wilsonbbe2e112010-10-28 22:35:07 +01005087 return 0;
Chris Wilson31169712009-09-14 16:50:28 +01005088
5089 /* "fast-path" to count number of available objects */
5090 if (nr_to_scan == 0) {
Chris Wilson17250b72010-10-28 12:51:39 +01005091 cnt = 0;
5092 list_for_each_entry(obj,
5093 &dev_priv->mm.inactive_list,
5094 mm_list)
5095 cnt++;
5096 mutex_unlock(&dev->struct_mutex);
5097 return cnt / 100 * sysctl_vfs_cache_pressure;
Chris Wilson31169712009-09-14 16:50:28 +01005098 }
5099
Chris Wilson1637ef42010-04-20 17:10:35 +01005100rescan:
Chris Wilson31169712009-09-14 16:50:28 +01005101 /* first scan for clean buffers */
Chris Wilson17250b72010-10-28 12:51:39 +01005102 i915_gem_retire_requests(dev);
Chris Wilson31169712009-09-14 16:50:28 +01005103
Chris Wilson17250b72010-10-28 12:51:39 +01005104 list_for_each_entry_safe(obj, next,
5105 &dev_priv->mm.inactive_list,
5106 mm_list) {
5107 if (i915_gem_object_is_purgeable(obj)) {
Chris Wilson05394f32010-11-08 19:18:58 +00005108 i915_gem_object_unbind(obj);
Chris Wilson17250b72010-10-28 12:51:39 +01005109 if (--nr_to_scan == 0)
5110 break;
Chris Wilson31169712009-09-14 16:50:28 +01005111 }
Chris Wilson31169712009-09-14 16:50:28 +01005112 }
5113
5114 /* second pass, evict/count anything still on the inactive list */
Chris Wilson17250b72010-10-28 12:51:39 +01005115 cnt = 0;
5116 list_for_each_entry_safe(obj, next,
5117 &dev_priv->mm.inactive_list,
5118 mm_list) {
5119 if (nr_to_scan) {
Chris Wilson05394f32010-11-08 19:18:58 +00005120 i915_gem_object_unbind(obj);
Chris Wilson17250b72010-10-28 12:51:39 +01005121 nr_to_scan--;
5122 } else
5123 cnt++;
Chris Wilson31169712009-09-14 16:50:28 +01005124 }
5125
Chris Wilson17250b72010-10-28 12:51:39 +01005126 if (nr_to_scan && i915_gpu_is_active(dev)) {
Chris Wilson1637ef42010-04-20 17:10:35 +01005127 /*
5128 * We are desperate for pages, so as a last resort, wait
5129 * for the GPU to finish and discard whatever we can.
5130 * This has a dramatic impact to reduce the number of
5131 * OOM-killer events whilst running the GPU aggressively.
5132 */
Chris Wilson17250b72010-10-28 12:51:39 +01005133 if (i915_gpu_idle(dev) == 0)
Chris Wilson1637ef42010-04-20 17:10:35 +01005134 goto rescan;
5135 }
Chris Wilson17250b72010-10-28 12:51:39 +01005136 mutex_unlock(&dev->struct_mutex);
5137 return cnt / 100 * sysctl_vfs_cache_pressure;
Chris Wilson31169712009-09-14 16:50:28 +01005138}