blob: 07ad1e35408446962af247f4a20bd296ad95b6e2 [file] [log] [blame]
Eric Anholt673a3942008-07-30 12:06:12 -07001/*
2 * Copyright © 2008 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 *
26 */
27
28#include "drmP.h"
29#include "drm.h"
30#include "i915_drm.h"
31#include "i915_drv.h"
Chris Wilson1c5d22f2009-08-25 11:15:50 +010032#include "i915_trace.h"
Jesse Barnes652c3932009-08-17 13:31:43 -070033#include "intel_drv.h"
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Eric Anholt673a3942008-07-30 12:06:12 -070035#include <linux/swap.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080036#include <linux/pci.h>
Zhenyu Wangf8f235e2010-08-27 11:08:57 +080037#include <linux/intel-gtt.h>
Eric Anholt673a3942008-07-30 12:06:12 -070038
Chris Wilsona00b10c2010-09-24 21:15:47 +010039static uint32_t i915_gem_get_gtt_alignment(struct drm_i915_gem_object *obj_priv);
40static uint32_t i915_gem_get_gtt_size(struct drm_i915_gem_object *obj_priv);
Daniel Vetterba3d8d72010-02-11 22:37:04 +010041
42static int i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj,
43 bool pipelined);
Eric Anholte47c68e2008-11-14 13:35:19 -080044static void i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj);
45static void i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj);
Eric Anholte47c68e2008-11-14 13:35:19 -080046static int i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj,
47 int write);
48static int i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
49 uint64_t offset,
50 uint64_t size);
51static void i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj);
Chris Wilson2cf34d72010-09-14 13:03:28 +010052static int i915_gem_object_wait_rendering(struct drm_gem_object *obj,
53 bool interruptible);
Jesse Barnesde151cf2008-11-12 10:03:55 -080054static int i915_gem_object_bind_to_gtt(struct drm_gem_object *obj,
Chris Wilsona00b10c2010-09-24 21:15:47 +010055 unsigned alignment,
56 bool mappable,
57 bool need_fence);
Jesse Barnesde151cf2008-11-12 10:03:55 -080058static void i915_gem_clear_fence_reg(struct drm_gem_object *obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +100059static int i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
60 struct drm_i915_gem_pwrite *args,
61 struct drm_file *file_priv);
Chris Wilsonbe726152010-07-23 23:18:50 +010062static void i915_gem_free_object_tail(struct drm_gem_object *obj);
Eric Anholt673a3942008-07-30 12:06:12 -070063
Chris Wilson17250b72010-10-28 12:51:39 +010064static int i915_gem_inactive_shrink(struct shrinker *shrinker,
65 int nr_to_scan,
66 gfp_t gfp_mask);
67
Chris Wilson31169712009-09-14 16:50:28 +010068
Chris Wilson73aa8082010-09-30 11:46:12 +010069/* some bookkeeping */
70static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
71 size_t size)
72{
73 dev_priv->mm.object_count++;
74 dev_priv->mm.object_memory += size;
75}
76
77static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
78 size_t size)
79{
80 dev_priv->mm.object_count--;
81 dev_priv->mm.object_memory -= size;
82}
83
84static void i915_gem_info_add_gtt(struct drm_i915_private *dev_priv,
Chris Wilsona00b10c2010-09-24 21:15:47 +010085 struct drm_i915_gem_object *obj)
Chris Wilson73aa8082010-09-30 11:46:12 +010086{
87 dev_priv->mm.gtt_count++;
Chris Wilsona00b10c2010-09-24 21:15:47 +010088 dev_priv->mm.gtt_memory += obj->gtt_space->size;
89 if (obj->gtt_offset < dev_priv->mm.gtt_mappable_end) {
Daniel Vetterfb7d5162010-10-01 22:05:20 +020090 dev_priv->mm.mappable_gtt_used +=
Chris Wilsona00b10c2010-09-24 21:15:47 +010091 min_t(size_t, obj->gtt_space->size,
92 dev_priv->mm.gtt_mappable_end - obj->gtt_offset);
Daniel Vetterfb7d5162010-10-01 22:05:20 +020093 }
Chris Wilson73aa8082010-09-30 11:46:12 +010094}
95
96static void i915_gem_info_remove_gtt(struct drm_i915_private *dev_priv,
Chris Wilsona00b10c2010-09-24 21:15:47 +010097 struct drm_i915_gem_object *obj)
Chris Wilson73aa8082010-09-30 11:46:12 +010098{
99 dev_priv->mm.gtt_count--;
Chris Wilsona00b10c2010-09-24 21:15:47 +0100100 dev_priv->mm.gtt_memory -= obj->gtt_space->size;
101 if (obj->gtt_offset < dev_priv->mm.gtt_mappable_end) {
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200102 dev_priv->mm.mappable_gtt_used -=
Chris Wilsona00b10c2010-09-24 21:15:47 +0100103 min_t(size_t, obj->gtt_space->size,
104 dev_priv->mm.gtt_mappable_end - obj->gtt_offset);
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200105 }
106}
107
108/**
109 * Update the mappable working set counters. Call _only_ when there is a change
110 * in one of (pin|fault)_mappable and update *_mappable _before_ calling.
111 * @mappable: new state the changed mappable flag (either pin_ or fault_).
112 */
113static void
114i915_gem_info_update_mappable(struct drm_i915_private *dev_priv,
Chris Wilsona00b10c2010-09-24 21:15:47 +0100115 struct drm_i915_gem_object *obj,
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200116 bool mappable)
117{
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200118 if (mappable) {
Chris Wilsona00b10c2010-09-24 21:15:47 +0100119 if (obj->pin_mappable && obj->fault_mappable)
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200120 /* Combined state was already mappable. */
121 return;
122 dev_priv->mm.gtt_mappable_count++;
Chris Wilsona00b10c2010-09-24 21:15:47 +0100123 dev_priv->mm.gtt_mappable_memory += obj->gtt_space->size;
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200124 } else {
Chris Wilsona00b10c2010-09-24 21:15:47 +0100125 if (obj->pin_mappable || obj->fault_mappable)
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200126 /* Combined state still mappable. */
127 return;
128 dev_priv->mm.gtt_mappable_count--;
Chris Wilsona00b10c2010-09-24 21:15:47 +0100129 dev_priv->mm.gtt_mappable_memory -= obj->gtt_space->size;
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200130 }
Chris Wilson73aa8082010-09-30 11:46:12 +0100131}
132
133static void i915_gem_info_add_pin(struct drm_i915_private *dev_priv,
Chris Wilsona00b10c2010-09-24 21:15:47 +0100134 struct drm_i915_gem_object *obj,
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200135 bool mappable)
Chris Wilson73aa8082010-09-30 11:46:12 +0100136{
137 dev_priv->mm.pin_count++;
Chris Wilsona00b10c2010-09-24 21:15:47 +0100138 dev_priv->mm.pin_memory += obj->gtt_space->size;
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200139 if (mappable) {
Chris Wilsona00b10c2010-09-24 21:15:47 +0100140 obj->pin_mappable = true;
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200141 i915_gem_info_update_mappable(dev_priv, obj, true);
142 }
Chris Wilson73aa8082010-09-30 11:46:12 +0100143}
144
145static void i915_gem_info_remove_pin(struct drm_i915_private *dev_priv,
Chris Wilsona00b10c2010-09-24 21:15:47 +0100146 struct drm_i915_gem_object *obj)
Chris Wilson73aa8082010-09-30 11:46:12 +0100147{
148 dev_priv->mm.pin_count--;
Chris Wilsona00b10c2010-09-24 21:15:47 +0100149 dev_priv->mm.pin_memory -= obj->gtt_space->size;
150 if (obj->pin_mappable) {
151 obj->pin_mappable = false;
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200152 i915_gem_info_update_mappable(dev_priv, obj, false);
153 }
Chris Wilson73aa8082010-09-30 11:46:12 +0100154}
155
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100156int
157i915_gem_check_is_wedged(struct drm_device *dev)
158{
159 struct drm_i915_private *dev_priv = dev->dev_private;
160 struct completion *x = &dev_priv->error_completion;
161 unsigned long flags;
162 int ret;
163
164 if (!atomic_read(&dev_priv->mm.wedged))
165 return 0;
166
167 ret = wait_for_completion_interruptible(x);
168 if (ret)
169 return ret;
170
171 /* Success, we reset the GPU! */
172 if (!atomic_read(&dev_priv->mm.wedged))
173 return 0;
174
175 /* GPU is hung, bump the completion count to account for
176 * the token we just consumed so that we never hit zero and
177 * end up waiting upon a subsequent completion event that
178 * will never happen.
179 */
180 spin_lock_irqsave(&x->wait.lock, flags);
181 x->done++;
182 spin_unlock_irqrestore(&x->wait.lock, flags);
183 return -EIO;
184}
185
Chris Wilson76c1dec2010-09-25 11:22:51 +0100186static int i915_mutex_lock_interruptible(struct drm_device *dev)
187{
188 struct drm_i915_private *dev_priv = dev->dev_private;
189 int ret;
190
191 ret = i915_gem_check_is_wedged(dev);
192 if (ret)
193 return ret;
194
195 ret = mutex_lock_interruptible(&dev->struct_mutex);
196 if (ret)
197 return ret;
198
199 if (atomic_read(&dev_priv->mm.wedged)) {
200 mutex_unlock(&dev->struct_mutex);
201 return -EAGAIN;
202 }
203
Chris Wilson23bc5982010-09-29 16:10:57 +0100204 WARN_ON(i915_verify_lists(dev));
Chris Wilson76c1dec2010-09-25 11:22:51 +0100205 return 0;
206}
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100207
Chris Wilson7d1c4802010-08-07 21:45:03 +0100208static inline bool
209i915_gem_object_is_inactive(struct drm_i915_gem_object *obj_priv)
210{
211 return obj_priv->gtt_space &&
212 !obj_priv->active &&
213 obj_priv->pin_count == 0;
214}
215
Chris Wilson73aa8082010-09-30 11:46:12 +0100216int i915_gem_do_init(struct drm_device *dev,
217 unsigned long start,
Daniel Vetter53984632010-09-22 23:44:24 +0200218 unsigned long mappable_end,
Jesse Barnes79e53942008-11-07 14:24:08 -0800219 unsigned long end)
220{
221 drm_i915_private_t *dev_priv = dev->dev_private;
222
223 if (start >= end ||
224 (start & (PAGE_SIZE - 1)) != 0 ||
225 (end & (PAGE_SIZE - 1)) != 0) {
226 return -EINVAL;
227 }
228
229 drm_mm_init(&dev_priv->mm.gtt_space, start,
230 end - start);
231
Chris Wilson73aa8082010-09-30 11:46:12 +0100232 dev_priv->mm.gtt_total = end - start;
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200233 dev_priv->mm.mappable_gtt_total = min(end, mappable_end) - start;
Daniel Vetter53984632010-09-22 23:44:24 +0200234 dev_priv->mm.gtt_mappable_end = mappable_end;
Jesse Barnes79e53942008-11-07 14:24:08 -0800235
236 return 0;
237}
Keith Packard6dbe2772008-10-14 21:41:13 -0700238
Eric Anholt673a3942008-07-30 12:06:12 -0700239int
240i915_gem_init_ioctl(struct drm_device *dev, void *data,
241 struct drm_file *file_priv)
242{
Eric Anholt673a3942008-07-30 12:06:12 -0700243 struct drm_i915_gem_init *args = data;
Jesse Barnes79e53942008-11-07 14:24:08 -0800244 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700245
246 mutex_lock(&dev->struct_mutex);
Daniel Vetter53984632010-09-22 23:44:24 +0200247 ret = i915_gem_do_init(dev, args->gtt_start, args->gtt_end, args->gtt_end);
Eric Anholt673a3942008-07-30 12:06:12 -0700248 mutex_unlock(&dev->struct_mutex);
249
Jesse Barnes79e53942008-11-07 14:24:08 -0800250 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700251}
252
Eric Anholt5a125c32008-10-22 21:40:13 -0700253int
254i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
255 struct drm_file *file_priv)
256{
Chris Wilson73aa8082010-09-30 11:46:12 +0100257 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt5a125c32008-10-22 21:40:13 -0700258 struct drm_i915_gem_get_aperture *args = data;
Eric Anholt5a125c32008-10-22 21:40:13 -0700259
260 if (!(dev->driver->driver_features & DRIVER_GEM))
261 return -ENODEV;
262
Chris Wilson73aa8082010-09-30 11:46:12 +0100263 mutex_lock(&dev->struct_mutex);
264 args->aper_size = dev_priv->mm.gtt_total;
265 args->aper_available_size = args->aper_size - dev_priv->mm.pin_memory;
266 mutex_unlock(&dev->struct_mutex);
Eric Anholt5a125c32008-10-22 21:40:13 -0700267
268 return 0;
269}
270
Eric Anholt673a3942008-07-30 12:06:12 -0700271
272/**
273 * Creates a new mm object and returns a handle to it.
274 */
275int
276i915_gem_create_ioctl(struct drm_device *dev, void *data,
277 struct drm_file *file_priv)
278{
279 struct drm_i915_gem_create *args = data;
280 struct drm_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300281 int ret;
282 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700283
284 args->size = roundup(args->size, PAGE_SIZE);
285
286 /* Allocate the new object */
Daniel Vetterac52bc52010-04-09 19:05:06 +0000287 obj = i915_gem_alloc_object(dev, args->size);
Eric Anholt673a3942008-07-30 12:06:12 -0700288 if (obj == NULL)
289 return -ENOMEM;
290
291 ret = drm_gem_handle_create(file_priv, obj, &handle);
Chris Wilson1dfd9752010-09-06 14:44:14 +0100292 if (ret) {
Chris Wilson202f2fe2010-10-14 13:20:40 +0100293 drm_gem_object_release(obj);
294 i915_gem_info_remove_obj(dev->dev_private, obj->size);
295 kfree(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700296 return ret;
Chris Wilson1dfd9752010-09-06 14:44:14 +0100297 }
298
Chris Wilson202f2fe2010-10-14 13:20:40 +0100299 /* drop reference from allocate - handle holds it now */
300 drm_gem_object_unreference(obj);
301 trace_i915_gem_object_create(obj);
302
Eric Anholt673a3942008-07-30 12:06:12 -0700303 args->handle = handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700304 return 0;
305}
306
Eric Anholt280b7132009-03-12 16:56:27 -0700307static int i915_gem_object_needs_bit17_swizzle(struct drm_gem_object *obj)
308{
309 drm_i915_private_t *dev_priv = obj->dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +0100310 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt280b7132009-03-12 16:56:27 -0700311
312 return dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_9_10_17 &&
313 obj_priv->tiling_mode != I915_TILING_NONE;
314}
315
Chris Wilson99a03df2010-05-27 14:15:34 +0100316static inline void
Eric Anholt40123c12009-03-09 13:42:30 -0700317slow_shmem_copy(struct page *dst_page,
318 int dst_offset,
319 struct page *src_page,
320 int src_offset,
321 int length)
322{
323 char *dst_vaddr, *src_vaddr;
324
Chris Wilson99a03df2010-05-27 14:15:34 +0100325 dst_vaddr = kmap(dst_page);
326 src_vaddr = kmap(src_page);
Eric Anholt40123c12009-03-09 13:42:30 -0700327
328 memcpy(dst_vaddr + dst_offset, src_vaddr + src_offset, length);
329
Chris Wilson99a03df2010-05-27 14:15:34 +0100330 kunmap(src_page);
331 kunmap(dst_page);
Eric Anholt40123c12009-03-09 13:42:30 -0700332}
333
Chris Wilson99a03df2010-05-27 14:15:34 +0100334static inline void
Eric Anholt280b7132009-03-12 16:56:27 -0700335slow_shmem_bit17_copy(struct page *gpu_page,
336 int gpu_offset,
337 struct page *cpu_page,
338 int cpu_offset,
339 int length,
340 int is_read)
341{
342 char *gpu_vaddr, *cpu_vaddr;
343
344 /* Use the unswizzled path if this page isn't affected. */
345 if ((page_to_phys(gpu_page) & (1 << 17)) == 0) {
346 if (is_read)
347 return slow_shmem_copy(cpu_page, cpu_offset,
348 gpu_page, gpu_offset, length);
349 else
350 return slow_shmem_copy(gpu_page, gpu_offset,
351 cpu_page, cpu_offset, length);
352 }
353
Chris Wilson99a03df2010-05-27 14:15:34 +0100354 gpu_vaddr = kmap(gpu_page);
355 cpu_vaddr = kmap(cpu_page);
Eric Anholt280b7132009-03-12 16:56:27 -0700356
357 /* Copy the data, XORing A6 with A17 (1). The user already knows he's
358 * XORing with the other bits (A9 for Y, A9 and A10 for X)
359 */
360 while (length > 0) {
361 int cacheline_end = ALIGN(gpu_offset + 1, 64);
362 int this_length = min(cacheline_end - gpu_offset, length);
363 int swizzled_gpu_offset = gpu_offset ^ 64;
364
365 if (is_read) {
366 memcpy(cpu_vaddr + cpu_offset,
367 gpu_vaddr + swizzled_gpu_offset,
368 this_length);
369 } else {
370 memcpy(gpu_vaddr + swizzled_gpu_offset,
371 cpu_vaddr + cpu_offset,
372 this_length);
373 }
374 cpu_offset += this_length;
375 gpu_offset += this_length;
376 length -= this_length;
377 }
378
Chris Wilson99a03df2010-05-27 14:15:34 +0100379 kunmap(cpu_page);
380 kunmap(gpu_page);
Eric Anholt280b7132009-03-12 16:56:27 -0700381}
382
Eric Anholt673a3942008-07-30 12:06:12 -0700383/**
Eric Anholteb014592009-03-10 11:44:52 -0700384 * This is the fast shmem pread path, which attempts to copy_from_user directly
385 * from the backing pages of the object to the user's address space. On a
386 * fault, it fails so we can fall back to i915_gem_shmem_pwrite_slow().
387 */
388static int
389i915_gem_shmem_pread_fast(struct drm_device *dev, struct drm_gem_object *obj,
390 struct drm_i915_gem_pread *args,
391 struct drm_file *file_priv)
392{
Daniel Vetter23010e42010-03-08 13:35:02 +0100393 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsone5281cc2010-10-28 13:45:36 +0100394 struct address_space *mapping = obj->filp->f_path.dentry->d_inode->i_mapping;
Eric Anholteb014592009-03-10 11:44:52 -0700395 ssize_t remain;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100396 loff_t offset;
Eric Anholteb014592009-03-10 11:44:52 -0700397 char __user *user_data;
398 int page_offset, page_length;
Eric Anholteb014592009-03-10 11:44:52 -0700399
400 user_data = (char __user *) (uintptr_t) args->data_ptr;
401 remain = args->size;
402
Daniel Vetter23010e42010-03-08 13:35:02 +0100403 obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700404 offset = args->offset;
405
406 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100407 struct page *page;
408 char *vaddr;
409 int ret;
410
Eric Anholteb014592009-03-10 11:44:52 -0700411 /* Operation in this page
412 *
Eric Anholteb014592009-03-10 11:44:52 -0700413 * page_offset = offset within page
414 * page_length = bytes to copy for this page
415 */
Eric Anholteb014592009-03-10 11:44:52 -0700416 page_offset = offset & (PAGE_SIZE-1);
417 page_length = remain;
418 if ((page_offset + remain) > PAGE_SIZE)
419 page_length = PAGE_SIZE - page_offset;
420
Chris Wilsone5281cc2010-10-28 13:45:36 +0100421 page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
422 GFP_HIGHUSER | __GFP_RECLAIMABLE);
423 if (IS_ERR(page))
424 return PTR_ERR(page);
425
426 vaddr = kmap_atomic(page);
427 ret = __copy_to_user_inatomic(user_data,
428 vaddr + page_offset,
429 page_length);
430 kunmap_atomic(vaddr);
431
432 mark_page_accessed(page);
433 page_cache_release(page);
434 if (ret)
Chris Wilson4f27b752010-10-14 15:26:45 +0100435 return -EFAULT;
Eric Anholteb014592009-03-10 11:44:52 -0700436
437 remain -= page_length;
438 user_data += page_length;
439 offset += page_length;
440 }
441
Chris Wilson4f27b752010-10-14 15:26:45 +0100442 return 0;
Eric Anholteb014592009-03-10 11:44:52 -0700443}
444
445/**
446 * This is the fallback shmem pread path, which allocates temporary storage
447 * in kernel space to copy_to_user into outside of the struct_mutex, so we
448 * can copy out of the object's backing pages while holding the struct mutex
449 * and not take page faults.
450 */
451static int
452i915_gem_shmem_pread_slow(struct drm_device *dev, struct drm_gem_object *obj,
453 struct drm_i915_gem_pread *args,
454 struct drm_file *file_priv)
455{
Chris Wilsone5281cc2010-10-28 13:45:36 +0100456 struct address_space *mapping = obj->filp->f_path.dentry->d_inode->i_mapping;
Daniel Vetter23010e42010-03-08 13:35:02 +0100457 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700458 struct mm_struct *mm = current->mm;
459 struct page **user_pages;
460 ssize_t remain;
461 loff_t offset, pinned_pages, i;
462 loff_t first_data_page, last_data_page, num_pages;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100463 int shmem_page_offset;
464 int data_page_index, data_page_offset;
Eric Anholteb014592009-03-10 11:44:52 -0700465 int page_length;
466 int ret;
467 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700468 int do_bit17_swizzling;
Eric Anholteb014592009-03-10 11:44:52 -0700469
470 remain = args->size;
471
472 /* Pin the user pages containing the data. We can't fault while
473 * holding the struct mutex, yet we want to hold it while
474 * dereferencing the user data.
475 */
476 first_data_page = data_ptr / PAGE_SIZE;
477 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
478 num_pages = last_data_page - first_data_page + 1;
479
Chris Wilson4f27b752010-10-14 15:26:45 +0100480 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
Eric Anholteb014592009-03-10 11:44:52 -0700481 if (user_pages == NULL)
482 return -ENOMEM;
483
Chris Wilson4f27b752010-10-14 15:26:45 +0100484 mutex_unlock(&dev->struct_mutex);
Eric Anholteb014592009-03-10 11:44:52 -0700485 down_read(&mm->mmap_sem);
486 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
Eric Anholte5e9ecd2009-04-07 16:01:22 -0700487 num_pages, 1, 0, user_pages, NULL);
Eric Anholteb014592009-03-10 11:44:52 -0700488 up_read(&mm->mmap_sem);
Chris Wilson4f27b752010-10-14 15:26:45 +0100489 mutex_lock(&dev->struct_mutex);
Eric Anholteb014592009-03-10 11:44:52 -0700490 if (pinned_pages < num_pages) {
491 ret = -EFAULT;
Chris Wilson4f27b752010-10-14 15:26:45 +0100492 goto out;
Eric Anholteb014592009-03-10 11:44:52 -0700493 }
494
Chris Wilson4f27b752010-10-14 15:26:45 +0100495 ret = i915_gem_object_set_cpu_read_domain_range(obj,
496 args->offset,
Eric Anholteb014592009-03-10 11:44:52 -0700497 args->size);
Chris Wilson4f27b752010-10-14 15:26:45 +0100498 if (ret)
499 goto out;
500
501 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700502
Daniel Vetter23010e42010-03-08 13:35:02 +0100503 obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700504 offset = args->offset;
505
506 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100507 struct page *page;
508
Eric Anholteb014592009-03-10 11:44:52 -0700509 /* Operation in this page
510 *
Eric Anholteb014592009-03-10 11:44:52 -0700511 * shmem_page_offset = offset within page in shmem file
512 * data_page_index = page number in get_user_pages return
513 * data_page_offset = offset with data_page_index page.
514 * page_length = bytes to copy for this page
515 */
Eric Anholteb014592009-03-10 11:44:52 -0700516 shmem_page_offset = offset & ~PAGE_MASK;
517 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
518 data_page_offset = data_ptr & ~PAGE_MASK;
519
520 page_length = remain;
521 if ((shmem_page_offset + page_length) > PAGE_SIZE)
522 page_length = PAGE_SIZE - shmem_page_offset;
523 if ((data_page_offset + page_length) > PAGE_SIZE)
524 page_length = PAGE_SIZE - data_page_offset;
525
Chris Wilsone5281cc2010-10-28 13:45:36 +0100526 page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
527 GFP_HIGHUSER | __GFP_RECLAIMABLE);
528 if (IS_ERR(page))
529 return PTR_ERR(page);
530
Eric Anholt280b7132009-03-12 16:56:27 -0700531 if (do_bit17_swizzling) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100532 slow_shmem_bit17_copy(page,
Eric Anholt280b7132009-03-12 16:56:27 -0700533 shmem_page_offset,
Chris Wilson99a03df2010-05-27 14:15:34 +0100534 user_pages[data_page_index],
535 data_page_offset,
536 page_length,
537 1);
538 } else {
539 slow_shmem_copy(user_pages[data_page_index],
540 data_page_offset,
Chris Wilsone5281cc2010-10-28 13:45:36 +0100541 page,
Chris Wilson99a03df2010-05-27 14:15:34 +0100542 shmem_page_offset,
543 page_length);
Eric Anholt280b7132009-03-12 16:56:27 -0700544 }
Eric Anholteb014592009-03-10 11:44:52 -0700545
Chris Wilsone5281cc2010-10-28 13:45:36 +0100546 mark_page_accessed(page);
547 page_cache_release(page);
548
Eric Anholteb014592009-03-10 11:44:52 -0700549 remain -= page_length;
550 data_ptr += page_length;
551 offset += page_length;
552 }
553
Chris Wilson4f27b752010-10-14 15:26:45 +0100554out:
Eric Anholteb014592009-03-10 11:44:52 -0700555 for (i = 0; i < pinned_pages; i++) {
556 SetPageDirty(user_pages[i]);
Chris Wilsone5281cc2010-10-28 13:45:36 +0100557 mark_page_accessed(user_pages[i]);
Eric Anholteb014592009-03-10 11:44:52 -0700558 page_cache_release(user_pages[i]);
559 }
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700560 drm_free_large(user_pages);
Eric Anholteb014592009-03-10 11:44:52 -0700561
562 return ret;
563}
564
Eric Anholt673a3942008-07-30 12:06:12 -0700565/**
566 * Reads data from the object referenced by handle.
567 *
568 * On error, the contents of *data are undefined.
569 */
570int
571i915_gem_pread_ioctl(struct drm_device *dev, void *data,
572 struct drm_file *file_priv)
573{
574 struct drm_i915_gem_pread *args = data;
575 struct drm_gem_object *obj;
576 struct drm_i915_gem_object *obj_priv;
Chris Wilson35b62a82010-09-26 20:23:38 +0100577 int ret = 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700578
Chris Wilson4f27b752010-10-14 15:26:45 +0100579 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100580 if (ret)
Chris Wilson4f27b752010-10-14 15:26:45 +0100581 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700582
583 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100584 if (obj == NULL) {
585 ret = -ENOENT;
586 goto unlock;
Chris Wilson4f27b752010-10-14 15:26:45 +0100587 }
Daniel Vetter23010e42010-03-08 13:35:02 +0100588 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700589
Chris Wilson7dcd2492010-09-26 20:21:44 +0100590 /* Bounds check source. */
591 if (args->offset > obj->size || args->size > obj->size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +0100592 ret = -EINVAL;
Chris Wilson35b62a82010-09-26 20:23:38 +0100593 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +0100594 }
595
Chris Wilson35b62a82010-09-26 20:23:38 +0100596 if (args->size == 0)
597 goto out;
598
Chris Wilsonce9d4192010-09-26 20:50:05 +0100599 if (!access_ok(VERIFY_WRITE,
600 (char __user *)(uintptr_t)args->data_ptr,
601 args->size)) {
602 ret = -EFAULT;
Chris Wilson35b62a82010-09-26 20:23:38 +0100603 goto out;
Eric Anholt673a3942008-07-30 12:06:12 -0700604 }
605
Chris Wilsonb5e4feb2010-10-14 13:47:43 +0100606 ret = fault_in_pages_writeable((char __user *)(uintptr_t)args->data_ptr,
607 args->size);
608 if (ret) {
609 ret = -EFAULT;
610 goto out;
611 }
612
Chris Wilson4f27b752010-10-14 15:26:45 +0100613 ret = i915_gem_object_set_cpu_read_domain_range(obj,
614 args->offset,
615 args->size);
616 if (ret)
Chris Wilsone5281cc2010-10-28 13:45:36 +0100617 goto out;
Chris Wilson4f27b752010-10-14 15:26:45 +0100618
619 ret = -EFAULT;
620 if (!i915_gem_object_needs_bit17_swizzle(obj))
Eric Anholt280b7132009-03-12 16:56:27 -0700621 ret = i915_gem_shmem_pread_fast(dev, obj, args, file_priv);
Chris Wilson4f27b752010-10-14 15:26:45 +0100622 if (ret == -EFAULT)
623 ret = i915_gem_shmem_pread_slow(dev, obj, args, file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -0700624
Chris Wilson35b62a82010-09-26 20:23:38 +0100625out:
Chris Wilson4f27b752010-10-14 15:26:45 +0100626 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100627unlock:
Chris Wilson4f27b752010-10-14 15:26:45 +0100628 mutex_unlock(&dev->struct_mutex);
Eric Anholteb014592009-03-10 11:44:52 -0700629 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700630}
631
Keith Packard0839ccb2008-10-30 19:38:48 -0700632/* This is the fast write path which cannot handle
633 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700634 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700635
Keith Packard0839ccb2008-10-30 19:38:48 -0700636static inline int
637fast_user_write(struct io_mapping *mapping,
638 loff_t page_base, int page_offset,
639 char __user *user_data,
640 int length)
641{
642 char *vaddr_atomic;
643 unsigned long unwritten;
644
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700645 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
Keith Packard0839ccb2008-10-30 19:38:48 -0700646 unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
647 user_data, length);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700648 io_mapping_unmap_atomic(vaddr_atomic);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100649 return unwritten;
Keith Packard0839ccb2008-10-30 19:38:48 -0700650}
651
652/* Here's the write path which can sleep for
653 * page faults
654 */
655
Chris Wilsonab34c222010-05-27 14:15:35 +0100656static inline void
Eric Anholt3de09aa2009-03-09 09:42:23 -0700657slow_kernel_write(struct io_mapping *mapping,
658 loff_t gtt_base, int gtt_offset,
659 struct page *user_page, int user_offset,
660 int length)
Keith Packard0839ccb2008-10-30 19:38:48 -0700661{
Chris Wilsonab34c222010-05-27 14:15:35 +0100662 char __iomem *dst_vaddr;
663 char *src_vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -0700664
Chris Wilsonab34c222010-05-27 14:15:35 +0100665 dst_vaddr = io_mapping_map_wc(mapping, gtt_base);
666 src_vaddr = kmap(user_page);
667
668 memcpy_toio(dst_vaddr + gtt_offset,
669 src_vaddr + user_offset,
670 length);
671
672 kunmap(user_page);
673 io_mapping_unmap(dst_vaddr);
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700674}
675
Eric Anholt3de09aa2009-03-09 09:42:23 -0700676/**
677 * This is the fast pwrite path, where we copy the data directly from the
678 * user into the GTT, uncached.
679 */
Eric Anholt673a3942008-07-30 12:06:12 -0700680static int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700681i915_gem_gtt_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
682 struct drm_i915_gem_pwrite *args,
683 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700684{
Daniel Vetter23010e42010-03-08 13:35:02 +0100685 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Keith Packard0839ccb2008-10-30 19:38:48 -0700686 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700687 ssize_t remain;
Keith Packard0839ccb2008-10-30 19:38:48 -0700688 loff_t offset, page_base;
Eric Anholt673a3942008-07-30 12:06:12 -0700689 char __user *user_data;
Keith Packard0839ccb2008-10-30 19:38:48 -0700690 int page_offset, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700691
692 user_data = (char __user *) (uintptr_t) args->data_ptr;
693 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700694
Daniel Vetter23010e42010-03-08 13:35:02 +0100695 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700696 offset = obj_priv->gtt_offset + args->offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700697
698 while (remain > 0) {
699 /* Operation in this page
700 *
Keith Packard0839ccb2008-10-30 19:38:48 -0700701 * page_base = page offset within aperture
702 * page_offset = offset within page
703 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -0700704 */
Keith Packard0839ccb2008-10-30 19:38:48 -0700705 page_base = (offset & ~(PAGE_SIZE-1));
706 page_offset = offset & (PAGE_SIZE-1);
707 page_length = remain;
708 if ((page_offset + remain) > PAGE_SIZE)
709 page_length = PAGE_SIZE - page_offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700710
Keith Packard0839ccb2008-10-30 19:38:48 -0700711 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -0700712 * source page isn't available. Return the error and we'll
713 * retry in the slow path.
Keith Packard0839ccb2008-10-30 19:38:48 -0700714 */
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100715 if (fast_user_write(dev_priv->mm.gtt_mapping, page_base,
716 page_offset, user_data, page_length))
717
718 return -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -0700719
Keith Packard0839ccb2008-10-30 19:38:48 -0700720 remain -= page_length;
721 user_data += page_length;
722 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700723 }
Eric Anholt673a3942008-07-30 12:06:12 -0700724
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100725 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700726}
727
Eric Anholt3de09aa2009-03-09 09:42:23 -0700728/**
729 * This is the fallback GTT pwrite path, which uses get_user_pages to pin
730 * the memory and maps it using kmap_atomic for copying.
731 *
732 * This code resulted in x11perf -rgb10text consuming about 10% more CPU
733 * than using i915_gem_gtt_pwrite_fast on a G45 (32-bit).
734 */
Eric Anholt3043c602008-10-02 12:24:47 -0700735static int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700736i915_gem_gtt_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
737 struct drm_i915_gem_pwrite *args,
738 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700739{
Daniel Vetter23010e42010-03-08 13:35:02 +0100740 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700741 drm_i915_private_t *dev_priv = dev->dev_private;
742 ssize_t remain;
743 loff_t gtt_page_base, offset;
744 loff_t first_data_page, last_data_page, num_pages;
745 loff_t pinned_pages, i;
746 struct page **user_pages;
747 struct mm_struct *mm = current->mm;
748 int gtt_page_offset, data_page_offset, data_page_index, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700749 int ret;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700750 uint64_t data_ptr = args->data_ptr;
751
752 remain = args->size;
753
754 /* Pin the user pages containing the data. We can't fault while
755 * holding the struct mutex, and all of the pwrite implementations
756 * want to hold it while dereferencing the user data.
757 */
758 first_data_page = data_ptr / PAGE_SIZE;
759 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
760 num_pages = last_data_page - first_data_page + 1;
761
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100762 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
Eric Anholt3de09aa2009-03-09 09:42:23 -0700763 if (user_pages == NULL)
764 return -ENOMEM;
765
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100766 mutex_unlock(&dev->struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700767 down_read(&mm->mmap_sem);
768 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
769 num_pages, 0, 0, user_pages, NULL);
770 up_read(&mm->mmap_sem);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100771 mutex_lock(&dev->struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700772 if (pinned_pages < num_pages) {
773 ret = -EFAULT;
774 goto out_unpin_pages;
775 }
776
Eric Anholt3de09aa2009-03-09 09:42:23 -0700777 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
778 if (ret)
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100779 goto out_unpin_pages;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700780
Daniel Vetter23010e42010-03-08 13:35:02 +0100781 obj_priv = to_intel_bo(obj);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700782 offset = obj_priv->gtt_offset + args->offset;
783
784 while (remain > 0) {
785 /* Operation in this page
786 *
787 * gtt_page_base = page offset within aperture
788 * gtt_page_offset = offset within page in aperture
789 * data_page_index = page number in get_user_pages return
790 * data_page_offset = offset with data_page_index page.
791 * page_length = bytes to copy for this page
792 */
793 gtt_page_base = offset & PAGE_MASK;
794 gtt_page_offset = offset & ~PAGE_MASK;
795 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
796 data_page_offset = data_ptr & ~PAGE_MASK;
797
798 page_length = remain;
799 if ((gtt_page_offset + page_length) > PAGE_SIZE)
800 page_length = PAGE_SIZE - gtt_page_offset;
801 if ((data_page_offset + page_length) > PAGE_SIZE)
802 page_length = PAGE_SIZE - data_page_offset;
803
Chris Wilsonab34c222010-05-27 14:15:35 +0100804 slow_kernel_write(dev_priv->mm.gtt_mapping,
805 gtt_page_base, gtt_page_offset,
806 user_pages[data_page_index],
807 data_page_offset,
808 page_length);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700809
810 remain -= page_length;
811 offset += page_length;
812 data_ptr += page_length;
813 }
814
Eric Anholt3de09aa2009-03-09 09:42:23 -0700815out_unpin_pages:
816 for (i = 0; i < pinned_pages; i++)
817 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700818 drm_free_large(user_pages);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700819
820 return ret;
821}
822
Eric Anholt40123c12009-03-09 13:42:30 -0700823/**
824 * This is the fast shmem pwrite path, which attempts to directly
825 * copy_from_user into the kmapped pages backing the object.
826 */
Eric Anholt673a3942008-07-30 12:06:12 -0700827static int
Eric Anholt40123c12009-03-09 13:42:30 -0700828i915_gem_shmem_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
829 struct drm_i915_gem_pwrite *args,
830 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700831{
Chris Wilsone5281cc2010-10-28 13:45:36 +0100832 struct address_space *mapping = obj->filp->f_path.dentry->d_inode->i_mapping;
Daniel Vetter23010e42010-03-08 13:35:02 +0100833 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700834 ssize_t remain;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100835 loff_t offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700836 char __user *user_data;
837 int page_offset, page_length;
Eric Anholt40123c12009-03-09 13:42:30 -0700838
839 user_data = (char __user *) (uintptr_t) args->data_ptr;
840 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700841
Daniel Vetter23010e42010-03-08 13:35:02 +0100842 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700843 offset = args->offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700844 obj_priv->dirty = 1;
Eric Anholt673a3942008-07-30 12:06:12 -0700845
Eric Anholt40123c12009-03-09 13:42:30 -0700846 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100847 struct page *page;
848 char *vaddr;
849 int ret;
850
Eric Anholt40123c12009-03-09 13:42:30 -0700851 /* Operation in this page
852 *
Eric Anholt40123c12009-03-09 13:42:30 -0700853 * page_offset = offset within page
854 * page_length = bytes to copy for this page
855 */
Eric Anholt40123c12009-03-09 13:42:30 -0700856 page_offset = offset & (PAGE_SIZE-1);
857 page_length = remain;
858 if ((page_offset + remain) > PAGE_SIZE)
859 page_length = PAGE_SIZE - page_offset;
860
Chris Wilsone5281cc2010-10-28 13:45:36 +0100861 page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
862 GFP_HIGHUSER | __GFP_RECLAIMABLE);
863 if (IS_ERR(page))
864 return PTR_ERR(page);
865
866 vaddr = kmap_atomic(page, KM_USER0);
867 ret = __copy_from_user_inatomic(vaddr + page_offset,
868 user_data,
869 page_length);
870 kunmap_atomic(vaddr, KM_USER0);
871
872 set_page_dirty(page);
873 mark_page_accessed(page);
874 page_cache_release(page);
875
876 /* If we get a fault while copying data, then (presumably) our
877 * source page isn't available. Return the error and we'll
878 * retry in the slow path.
879 */
880 if (ret)
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100881 return -EFAULT;
Eric Anholt40123c12009-03-09 13:42:30 -0700882
883 remain -= page_length;
884 user_data += page_length;
885 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700886 }
887
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100888 return 0;
Eric Anholt40123c12009-03-09 13:42:30 -0700889}
890
891/**
892 * This is the fallback shmem pwrite path, which uses get_user_pages to pin
893 * the memory and maps it using kmap_atomic for copying.
894 *
895 * This avoids taking mmap_sem for faulting on the user's address while the
896 * struct_mutex is held.
897 */
898static int
899i915_gem_shmem_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
900 struct drm_i915_gem_pwrite *args,
901 struct drm_file *file_priv)
902{
Chris Wilsone5281cc2010-10-28 13:45:36 +0100903 struct address_space *mapping = obj->filp->f_path.dentry->d_inode->i_mapping;
Daniel Vetter23010e42010-03-08 13:35:02 +0100904 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700905 struct mm_struct *mm = current->mm;
906 struct page **user_pages;
907 ssize_t remain;
908 loff_t offset, pinned_pages, i;
909 loff_t first_data_page, last_data_page, num_pages;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100910 int shmem_page_offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700911 int data_page_index, data_page_offset;
912 int page_length;
913 int ret;
914 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700915 int do_bit17_swizzling;
Eric Anholt40123c12009-03-09 13:42:30 -0700916
917 remain = args->size;
918
919 /* Pin the user pages containing the data. We can't fault while
920 * holding the struct mutex, and all of the pwrite implementations
921 * want to hold it while dereferencing the user data.
922 */
923 first_data_page = data_ptr / PAGE_SIZE;
924 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
925 num_pages = last_data_page - first_data_page + 1;
926
Chris Wilson4f27b752010-10-14 15:26:45 +0100927 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
Eric Anholt40123c12009-03-09 13:42:30 -0700928 if (user_pages == NULL)
929 return -ENOMEM;
930
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100931 mutex_unlock(&dev->struct_mutex);
Eric Anholt40123c12009-03-09 13:42:30 -0700932 down_read(&mm->mmap_sem);
933 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
934 num_pages, 0, 0, user_pages, NULL);
935 up_read(&mm->mmap_sem);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100936 mutex_lock(&dev->struct_mutex);
Eric Anholt40123c12009-03-09 13:42:30 -0700937 if (pinned_pages < num_pages) {
938 ret = -EFAULT;
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100939 goto out;
Eric Anholt40123c12009-03-09 13:42:30 -0700940 }
941
Eric Anholt40123c12009-03-09 13:42:30 -0700942 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100943 if (ret)
944 goto out;
945
946 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700947
Daniel Vetter23010e42010-03-08 13:35:02 +0100948 obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700949 offset = args->offset;
950 obj_priv->dirty = 1;
951
952 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100953 struct page *page;
954
Eric Anholt40123c12009-03-09 13:42:30 -0700955 /* Operation in this page
956 *
Eric Anholt40123c12009-03-09 13:42:30 -0700957 * shmem_page_offset = offset within page in shmem file
958 * data_page_index = page number in get_user_pages return
959 * data_page_offset = offset with data_page_index page.
960 * page_length = bytes to copy for this page
961 */
Eric Anholt40123c12009-03-09 13:42:30 -0700962 shmem_page_offset = offset & ~PAGE_MASK;
963 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
964 data_page_offset = data_ptr & ~PAGE_MASK;
965
966 page_length = remain;
967 if ((shmem_page_offset + page_length) > PAGE_SIZE)
968 page_length = PAGE_SIZE - shmem_page_offset;
969 if ((data_page_offset + page_length) > PAGE_SIZE)
970 page_length = PAGE_SIZE - data_page_offset;
971
Chris Wilsone5281cc2010-10-28 13:45:36 +0100972 page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
973 GFP_HIGHUSER | __GFP_RECLAIMABLE);
974 if (IS_ERR(page)) {
975 ret = PTR_ERR(page);
976 goto out;
977 }
978
Eric Anholt280b7132009-03-12 16:56:27 -0700979 if (do_bit17_swizzling) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100980 slow_shmem_bit17_copy(page,
Eric Anholt280b7132009-03-12 16:56:27 -0700981 shmem_page_offset,
982 user_pages[data_page_index],
983 data_page_offset,
Chris Wilson99a03df2010-05-27 14:15:34 +0100984 page_length,
985 0);
986 } else {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100987 slow_shmem_copy(page,
Chris Wilson99a03df2010-05-27 14:15:34 +0100988 shmem_page_offset,
989 user_pages[data_page_index],
990 data_page_offset,
991 page_length);
Eric Anholt280b7132009-03-12 16:56:27 -0700992 }
Eric Anholt40123c12009-03-09 13:42:30 -0700993
Chris Wilsone5281cc2010-10-28 13:45:36 +0100994 set_page_dirty(page);
995 mark_page_accessed(page);
996 page_cache_release(page);
997
Eric Anholt40123c12009-03-09 13:42:30 -0700998 remain -= page_length;
999 data_ptr += page_length;
1000 offset += page_length;
1001 }
1002
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001003out:
Eric Anholt40123c12009-03-09 13:42:30 -07001004 for (i = 0; i < pinned_pages; i++)
1005 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07001006 drm_free_large(user_pages);
Eric Anholt40123c12009-03-09 13:42:30 -07001007
1008 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001009}
1010
1011/**
1012 * Writes data to the object referenced by handle.
1013 *
1014 * On error, the contents of the buffer that were to be modified are undefined.
1015 */
1016int
1017i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001018 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001019{
1020 struct drm_i915_gem_pwrite *args = data;
1021 struct drm_gem_object *obj;
1022 struct drm_i915_gem_object *obj_priv;
1023 int ret = 0;
1024
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001025 ret = i915_mutex_lock_interruptible(dev);
1026 if (ret)
1027 return ret;
1028
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001029 obj = drm_gem_object_lookup(dev, file, args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001030 if (obj == NULL) {
1031 ret = -ENOENT;
1032 goto unlock;
1033 }
Daniel Vetter23010e42010-03-08 13:35:02 +01001034 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001035
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001036
Chris Wilson7dcd2492010-09-26 20:21:44 +01001037 /* Bounds check destination. */
1038 if (args->offset > obj->size || args->size > obj->size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001039 ret = -EINVAL;
Chris Wilson35b62a82010-09-26 20:23:38 +01001040 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001041 }
1042
Chris Wilson35b62a82010-09-26 20:23:38 +01001043 if (args->size == 0)
1044 goto out;
1045
Chris Wilsonce9d4192010-09-26 20:50:05 +01001046 if (!access_ok(VERIFY_READ,
1047 (char __user *)(uintptr_t)args->data_ptr,
1048 args->size)) {
1049 ret = -EFAULT;
Chris Wilson35b62a82010-09-26 20:23:38 +01001050 goto out;
Eric Anholt673a3942008-07-30 12:06:12 -07001051 }
1052
Chris Wilsonb5e4feb2010-10-14 13:47:43 +01001053 ret = fault_in_pages_readable((char __user *)(uintptr_t)args->data_ptr,
1054 args->size);
1055 if (ret) {
1056 ret = -EFAULT;
1057 goto out;
Eric Anholt673a3942008-07-30 12:06:12 -07001058 }
1059
1060 /* We can only do the GTT pwrite on untiled buffers, as otherwise
1061 * it would end up going through the fenced access, and we'll get
1062 * different detiling behavior between reading and writing.
1063 * pread/pwrite currently are reading and writing from the CPU
1064 * perspective, requiring manual detiling by the client.
1065 */
Dave Airlie71acb5e2008-12-30 20:31:46 +10001066 if (obj_priv->phys_obj)
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001067 ret = i915_gem_phys_pwrite(dev, obj, args, file);
Dave Airlie71acb5e2008-12-30 20:31:46 +10001068 else if (obj_priv->tiling_mode == I915_TILING_NONE &&
Chris Wilson5cdf5882010-09-27 15:51:07 +01001069 obj_priv->gtt_space &&
Chris Wilson9b8c4a02010-05-27 14:21:01 +01001070 obj->write_domain != I915_GEM_DOMAIN_CPU) {
Chris Wilsona00b10c2010-09-24 21:15:47 +01001071 ret = i915_gem_object_pin(obj, 0, true, false);
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001072 if (ret)
1073 goto out;
1074
1075 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
1076 if (ret)
1077 goto out_unpin;
1078
1079 ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file);
1080 if (ret == -EFAULT)
1081 ret = i915_gem_gtt_pwrite_slow(dev, obj, args, file);
1082
1083out_unpin:
1084 i915_gem_object_unpin(obj);
Eric Anholt40123c12009-03-09 13:42:30 -07001085 } else {
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001086 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
1087 if (ret)
Chris Wilsone5281cc2010-10-28 13:45:36 +01001088 goto out;
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001089
1090 ret = -EFAULT;
1091 if (!i915_gem_object_needs_bit17_swizzle(obj))
1092 ret = i915_gem_shmem_pwrite_fast(dev, obj, args, file);
1093 if (ret == -EFAULT)
1094 ret = i915_gem_shmem_pwrite_slow(dev, obj, args, file);
Eric Anholt40123c12009-03-09 13:42:30 -07001095 }
Eric Anholt673a3942008-07-30 12:06:12 -07001096
Chris Wilson35b62a82010-09-26 20:23:38 +01001097out:
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001098 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001099unlock:
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001100 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07001101 return ret;
1102}
1103
1104/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001105 * Called when user space prepares to use an object with the CPU, either
1106 * through the mmap ioctl's mapping or a GTT mapping.
Eric Anholt673a3942008-07-30 12:06:12 -07001107 */
1108int
1109i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
1110 struct drm_file *file_priv)
1111{
Eric Anholta09ba7f2009-08-29 12:49:51 -07001112 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07001113 struct drm_i915_gem_set_domain *args = data;
1114 struct drm_gem_object *obj;
Jesse Barnes652c3932009-08-17 13:31:43 -07001115 struct drm_i915_gem_object *obj_priv;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001116 uint32_t read_domains = args->read_domains;
1117 uint32_t write_domain = args->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07001118 int ret;
1119
1120 if (!(dev->driver->driver_features & DRIVER_GEM))
1121 return -ENODEV;
1122
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001123 /* Only handle setting domains to types used by the CPU. */
Chris Wilson21d509e2009-06-06 09:46:02 +01001124 if (write_domain & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001125 return -EINVAL;
1126
Chris Wilson21d509e2009-06-06 09:46:02 +01001127 if (read_domains & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001128 return -EINVAL;
1129
1130 /* Having something in the write domain implies it's in the read
1131 * domain, and only that read domain. Enforce that in the request.
1132 */
1133 if (write_domain != 0 && read_domains != write_domain)
1134 return -EINVAL;
1135
Chris Wilson76c1dec2010-09-25 11:22:51 +01001136 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001137 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001138 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001139
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001140 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1141 if (obj == NULL) {
1142 ret = -ENOENT;
1143 goto unlock;
Chris Wilson76c1dec2010-09-25 11:22:51 +01001144 }
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001145 obj_priv = to_intel_bo(obj);
Jesse Barnes652c3932009-08-17 13:31:43 -07001146
1147 intel_mark_busy(dev, obj);
1148
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001149 if (read_domains & I915_GEM_DOMAIN_GTT) {
1150 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
Eric Anholt02354392008-11-26 13:58:13 -08001151
Eric Anholta09ba7f2009-08-29 12:49:51 -07001152 /* Update the LRU on the fence for the CPU access that's
1153 * about to occur.
1154 */
1155 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001156 struct drm_i915_fence_reg *reg =
1157 &dev_priv->fence_regs[obj_priv->fence_reg];
1158 list_move_tail(&reg->lru_list,
Eric Anholta09ba7f2009-08-29 12:49:51 -07001159 &dev_priv->mm.fence_list);
1160 }
1161
Eric Anholt02354392008-11-26 13:58:13 -08001162 /* Silently promote "you're not bound, there was nothing to do"
1163 * to success, since the client was just asking us to
1164 * make sure everything was done.
1165 */
1166 if (ret == -EINVAL)
1167 ret = 0;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001168 } else {
Eric Anholte47c68e2008-11-14 13:35:19 -08001169 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001170 }
1171
Chris Wilson7d1c4802010-08-07 21:45:03 +01001172 /* Maintain LRU order of "inactive" objects */
1173 if (ret == 0 && i915_gem_object_is_inactive(obj_priv))
Chris Wilson69dc4982010-10-19 10:36:51 +01001174 list_move_tail(&obj_priv->mm_list, &dev_priv->mm.inactive_list);
Chris Wilson7d1c4802010-08-07 21:45:03 +01001175
Eric Anholt673a3942008-07-30 12:06:12 -07001176 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001177unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07001178 mutex_unlock(&dev->struct_mutex);
1179 return ret;
1180}
1181
1182/**
1183 * Called when user space has done writes to this buffer
1184 */
1185int
1186i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
1187 struct drm_file *file_priv)
1188{
1189 struct drm_i915_gem_sw_finish *args = data;
1190 struct drm_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001191 int ret = 0;
1192
1193 if (!(dev->driver->driver_features & DRIVER_GEM))
1194 return -ENODEV;
1195
Chris Wilson76c1dec2010-09-25 11:22:51 +01001196 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001197 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001198 return ret;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001199
Eric Anholt673a3942008-07-30 12:06:12 -07001200 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1201 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001202 ret = -ENOENT;
1203 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07001204 }
1205
Eric Anholt673a3942008-07-30 12:06:12 -07001206 /* Pinned buffers may be scanout, so flush the cache */
Chris Wilson3d2a8122010-09-29 11:39:53 +01001207 if (to_intel_bo(obj)->pin_count)
Eric Anholte47c68e2008-11-14 13:35:19 -08001208 i915_gem_object_flush_cpu_write_domain(obj);
1209
Eric Anholt673a3942008-07-30 12:06:12 -07001210 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001211unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07001212 mutex_unlock(&dev->struct_mutex);
1213 return ret;
1214}
1215
1216/**
1217 * Maps the contents of an object, returning the address it is mapped
1218 * into.
1219 *
1220 * While the mapping holds a reference on the contents of the object, it doesn't
1221 * imply a ref on the object itself.
1222 */
1223int
1224i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
1225 struct drm_file *file_priv)
1226{
Chris Wilsonda761a62010-10-27 17:37:08 +01001227 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07001228 struct drm_i915_gem_mmap *args = data;
1229 struct drm_gem_object *obj;
1230 loff_t offset;
1231 unsigned long addr;
1232
1233 if (!(dev->driver->driver_features & DRIVER_GEM))
1234 return -ENODEV;
1235
1236 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1237 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001238 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001239
Chris Wilsonda761a62010-10-27 17:37:08 +01001240 if (obj->size > dev_priv->mm.gtt_mappable_end) {
1241 drm_gem_object_unreference_unlocked(obj);
1242 return -E2BIG;
1243 }
1244
Eric Anholt673a3942008-07-30 12:06:12 -07001245 offset = args->offset;
1246
1247 down_write(&current->mm->mmap_sem);
1248 addr = do_mmap(obj->filp, 0, args->size,
1249 PROT_READ | PROT_WRITE, MAP_SHARED,
1250 args->offset);
1251 up_write(&current->mm->mmap_sem);
Luca Barbieribc9025b2010-02-09 05:49:12 +00001252 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001253 if (IS_ERR((void *)addr))
1254 return addr;
1255
1256 args->addr_ptr = (uint64_t) addr;
1257
1258 return 0;
1259}
1260
Jesse Barnesde151cf2008-11-12 10:03:55 -08001261/**
1262 * i915_gem_fault - fault a page into the GTT
1263 * vma: VMA in question
1264 * vmf: fault info
1265 *
1266 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1267 * from userspace. The fault handler takes care of binding the object to
1268 * the GTT (if needed), allocating and programming a fence register (again,
1269 * only if needed based on whether the old reg is still valid or the object
1270 * is tiled) and inserting a new PTE into the faulting process.
1271 *
1272 * Note that the faulting process may involve evicting existing objects
1273 * from the GTT and/or fence registers to make room. So performance may
1274 * suffer if the GTT working set is large or there are few fence registers
1275 * left.
1276 */
1277int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1278{
1279 struct drm_gem_object *obj = vma->vm_private_data;
1280 struct drm_device *dev = obj->dev;
Chris Wilson7d1c4802010-08-07 21:45:03 +01001281 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001282 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001283 pgoff_t page_offset;
1284 unsigned long pfn;
1285 int ret = 0;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001286 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001287
1288 /* We don't use vmf->pgoff since that has the fake offset */
1289 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1290 PAGE_SHIFT;
1291
1292 /* Now bind it into the GTT if needed */
1293 mutex_lock(&dev->struct_mutex);
Daniel Vetterfb7d5162010-10-01 22:05:20 +02001294 BUG_ON(obj_priv->pin_count && !obj_priv->pin_mappable);
Chris Wilsona00b10c2010-09-24 21:15:47 +01001295
1296 if (obj_priv->gtt_space) {
1297 if (!obj_priv->mappable ||
1298 (obj_priv->tiling_mode && !obj_priv->fenceable)) {
1299 ret = i915_gem_object_unbind(obj);
1300 if (ret)
1301 goto unlock;
1302 }
1303 }
Daniel Vetter16e809a2010-09-16 19:37:04 +02001304
Jesse Barnesde151cf2008-11-12 10:03:55 -08001305 if (!obj_priv->gtt_space) {
Chris Wilsona00b10c2010-09-24 21:15:47 +01001306 ret = i915_gem_object_bind_to_gtt(obj, 0,
1307 true, obj_priv->tiling_mode);
Chris Wilsonc7150892009-09-23 00:43:56 +01001308 if (ret)
1309 goto unlock;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001310 }
1311
Chris Wilson4a684a42010-10-28 14:44:08 +01001312 ret = i915_gem_object_set_to_gtt_domain(obj, write);
1313 if (ret)
1314 goto unlock;
1315
Daniel Vetterfb7d5162010-10-01 22:05:20 +02001316 if (!obj_priv->fault_mappable) {
1317 obj_priv->fault_mappable = true;
Chris Wilsona00b10c2010-09-24 21:15:47 +01001318 i915_gem_info_update_mappable(dev_priv, obj_priv, true);
Daniel Vetterfb7d5162010-10-01 22:05:20 +02001319 }
1320
Jesse Barnesde151cf2008-11-12 10:03:55 -08001321 /* Need a new fence register? */
Eric Anholta09ba7f2009-08-29 12:49:51 -07001322 if (obj_priv->tiling_mode != I915_TILING_NONE) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01001323 ret = i915_gem_object_get_fence_reg(obj, true);
Chris Wilsonc7150892009-09-23 00:43:56 +01001324 if (ret)
1325 goto unlock;
Eric Anholtd9ddcb92009-01-27 10:33:49 -08001326 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001327
Chris Wilson7d1c4802010-08-07 21:45:03 +01001328 if (i915_gem_object_is_inactive(obj_priv))
Chris Wilson69dc4982010-10-19 10:36:51 +01001329 list_move_tail(&obj_priv->mm_list, &dev_priv->mm.inactive_list);
Chris Wilson7d1c4802010-08-07 21:45:03 +01001330
Jesse Barnesde151cf2008-11-12 10:03:55 -08001331 pfn = ((dev->agp->base + obj_priv->gtt_offset) >> PAGE_SHIFT) +
1332 page_offset;
1333
1334 /* Finally, remap it using the new GTT offset */
1335 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
Chris Wilsonc7150892009-09-23 00:43:56 +01001336unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001337 mutex_unlock(&dev->struct_mutex);
1338
1339 switch (ret) {
Chris Wilsonc7150892009-09-23 00:43:56 +01001340 case 0:
1341 case -ERESTARTSYS:
1342 return VM_FAULT_NOPAGE;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001343 case -ENOMEM:
1344 case -EAGAIN:
1345 return VM_FAULT_OOM;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001346 default:
Chris Wilsonc7150892009-09-23 00:43:56 +01001347 return VM_FAULT_SIGBUS;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001348 }
1349}
1350
1351/**
1352 * i915_gem_create_mmap_offset - create a fake mmap offset for an object
1353 * @obj: obj in question
1354 *
1355 * GEM memory mapping works by handing back to userspace a fake mmap offset
1356 * it can use in a subsequent mmap(2) call. The DRM core code then looks
1357 * up the object based on the offset and sets up the various memory mapping
1358 * structures.
1359 *
1360 * This routine allocates and attaches a fake offset for @obj.
1361 */
1362static int
1363i915_gem_create_mmap_offset(struct drm_gem_object *obj)
1364{
1365 struct drm_device *dev = obj->dev;
1366 struct drm_gem_mm *mm = dev->mm_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001367 struct drm_map_list *list;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +11001368 struct drm_local_map *map;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001369 int ret = 0;
1370
1371 /* Set the object up for mmap'ing */
1372 list = &obj->map_list;
Eric Anholt9a298b22009-03-24 12:23:04 -07001373 list->map = kzalloc(sizeof(struct drm_map_list), GFP_KERNEL);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001374 if (!list->map)
1375 return -ENOMEM;
1376
1377 map = list->map;
1378 map->type = _DRM_GEM;
1379 map->size = obj->size;
1380 map->handle = obj;
1381
1382 /* Get a DRM GEM mmap offset allocated... */
1383 list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
1384 obj->size / PAGE_SIZE, 0, 0);
1385 if (!list->file_offset_node) {
1386 DRM_ERROR("failed to allocate offset for bo %d\n", obj->name);
Chris Wilson9e0ae5342010-09-21 15:05:24 +01001387 ret = -ENOSPC;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001388 goto out_free_list;
1389 }
1390
1391 list->file_offset_node = drm_mm_get_block(list->file_offset_node,
1392 obj->size / PAGE_SIZE, 0);
1393 if (!list->file_offset_node) {
1394 ret = -ENOMEM;
1395 goto out_free_list;
1396 }
1397
1398 list->hash.key = list->file_offset_node->start;
Chris Wilson9e0ae5342010-09-21 15:05:24 +01001399 ret = drm_ht_insert_item(&mm->offset_hash, &list->hash);
1400 if (ret) {
Jesse Barnesde151cf2008-11-12 10:03:55 -08001401 DRM_ERROR("failed to add to map hash\n");
1402 goto out_free_mm;
1403 }
1404
Jesse Barnesde151cf2008-11-12 10:03:55 -08001405 return 0;
1406
1407out_free_mm:
1408 drm_mm_put_block(list->file_offset_node);
1409out_free_list:
Eric Anholt9a298b22009-03-24 12:23:04 -07001410 kfree(list->map);
Chris Wilson39a01d12010-10-28 13:03:06 +01001411 list->map = NULL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001412
1413 return ret;
1414}
1415
Chris Wilson901782b2009-07-10 08:18:50 +01001416/**
1417 * i915_gem_release_mmap - remove physical page mappings
1418 * @obj: obj in question
1419 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001420 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01001421 * relinquish ownership of the pages back to the system.
1422 *
1423 * It is vital that we remove the page mapping if we have mapped a tiled
1424 * object through the GTT and then lose the fence register due to
1425 * resource pressure. Similarly if the object has been moved out of the
1426 * aperture, than pages mapped into userspace must be revoked. Removing the
1427 * mapping will then trigger a page fault on the next user access, allowing
1428 * fixup by i915_gem_fault().
1429 */
Eric Anholtd05ca302009-07-10 13:02:26 -07001430void
Chris Wilson901782b2009-07-10 08:18:50 +01001431i915_gem_release_mmap(struct drm_gem_object *obj)
1432{
1433 struct drm_device *dev = obj->dev;
Daniel Vetterfb7d5162010-10-01 22:05:20 +02001434 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001435 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson901782b2009-07-10 08:18:50 +01001436
Chris Wilson39a01d12010-10-28 13:03:06 +01001437 if (unlikely(obj->map_list.map && dev->dev_mapping))
Chris Wilson901782b2009-07-10 08:18:50 +01001438 unmap_mapping_range(dev->dev_mapping,
Chris Wilson39a01d12010-10-28 13:03:06 +01001439 (loff_t)obj->map_list.hash.key<<PAGE_SHIFT,
1440 obj->size, 1);
Daniel Vetterfb7d5162010-10-01 22:05:20 +02001441
1442 if (obj_priv->fault_mappable) {
1443 obj_priv->fault_mappable = false;
Chris Wilsona00b10c2010-09-24 21:15:47 +01001444 i915_gem_info_update_mappable(dev_priv, obj_priv, false);
Daniel Vetterfb7d5162010-10-01 22:05:20 +02001445 }
Chris Wilson901782b2009-07-10 08:18:50 +01001446}
1447
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001448static void
1449i915_gem_free_mmap_offset(struct drm_gem_object *obj)
1450{
1451 struct drm_device *dev = obj->dev;
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001452 struct drm_gem_mm *mm = dev->mm_private;
Chris Wilson39a01d12010-10-28 13:03:06 +01001453 struct drm_map_list *list = &obj->map_list;
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001454
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001455 drm_ht_remove_item(&mm->offset_hash, &list->hash);
Chris Wilson39a01d12010-10-28 13:03:06 +01001456 drm_mm_put_block(list->file_offset_node);
1457 kfree(list->map);
1458 list->map = NULL;
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001459}
1460
Jesse Barnesde151cf2008-11-12 10:03:55 -08001461/**
1462 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1463 * @obj: object to check
1464 *
1465 * Return the required GTT alignment for an object, taking into account
1466 * potential fence register mapping if needed.
1467 */
1468static uint32_t
Chris Wilsona00b10c2010-09-24 21:15:47 +01001469i915_gem_get_gtt_alignment(struct drm_i915_gem_object *obj_priv)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001470{
Chris Wilsona00b10c2010-09-24 21:15:47 +01001471 struct drm_device *dev = obj_priv->base.dev;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001472
1473 /*
1474 * Minimum alignment is 4k (GTT page size), but might be greater
1475 * if a fence register is needed for the object.
1476 */
Chris Wilsona00b10c2010-09-24 21:15:47 +01001477 if (INTEL_INFO(dev)->gen >= 4 ||
1478 obj_priv->tiling_mode == I915_TILING_NONE)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001479 return 4096;
1480
1481 /*
1482 * Previous chips need to be aligned to the size of the smallest
1483 * fence register that can contain the object.
1484 */
Chris Wilsona00b10c2010-09-24 21:15:47 +01001485 return i915_gem_get_gtt_size(obj_priv);
1486}
1487
1488static uint32_t
1489i915_gem_get_gtt_size(struct drm_i915_gem_object *obj_priv)
1490{
1491 struct drm_device *dev = obj_priv->base.dev;
1492 uint32_t size;
1493
1494 /*
1495 * Minimum alignment is 4k (GTT page size), but might be greater
1496 * if a fence register is needed for the object.
1497 */
1498 if (INTEL_INFO(dev)->gen >= 4)
1499 return obj_priv->base.size;
1500
1501 /*
1502 * Previous chips need to be aligned to the size of the smallest
1503 * fence register that can contain the object.
1504 */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001505 if (INTEL_INFO(dev)->gen == 3)
Chris Wilsona00b10c2010-09-24 21:15:47 +01001506 size = 1024*1024;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001507 else
Chris Wilsona00b10c2010-09-24 21:15:47 +01001508 size = 512*1024;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001509
Chris Wilsona00b10c2010-09-24 21:15:47 +01001510 while (size < obj_priv->base.size)
1511 size <<= 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001512
Chris Wilsona00b10c2010-09-24 21:15:47 +01001513 return size;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001514}
1515
1516/**
1517 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1518 * @dev: DRM device
1519 * @data: GTT mapping ioctl data
1520 * @file_priv: GEM object info
1521 *
1522 * Simply returns the fake offset to userspace so it can mmap it.
1523 * The mmap call will end up in drm_gem_mmap(), which will set things
1524 * up so we can get faults in the handler above.
1525 *
1526 * The fault handler will take care of binding the object into the GTT
1527 * (since it may have been evicted to make room for something), allocating
1528 * a fence register, and mapping the appropriate aperture address into
1529 * userspace.
1530 */
1531int
1532i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
1533 struct drm_file *file_priv)
1534{
Chris Wilsonda761a62010-10-27 17:37:08 +01001535 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001536 struct drm_i915_gem_mmap_gtt *args = data;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001537 struct drm_gem_object *obj;
1538 struct drm_i915_gem_object *obj_priv;
1539 int ret;
1540
1541 if (!(dev->driver->driver_features & DRIVER_GEM))
1542 return -ENODEV;
1543
Chris Wilson76c1dec2010-09-25 11:22:51 +01001544 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001545 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001546 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001547
Jesse Barnesde151cf2008-11-12 10:03:55 -08001548 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001549 if (obj == NULL) {
1550 ret = -ENOENT;
1551 goto unlock;
1552 }
Daniel Vetter23010e42010-03-08 13:35:02 +01001553 obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001554
Chris Wilsonda761a62010-10-27 17:37:08 +01001555 if (obj->size > dev_priv->mm.gtt_mappable_end) {
1556 ret = -E2BIG;
1557 goto unlock;
1558 }
1559
Chris Wilsonab182822009-09-22 18:46:17 +01001560 if (obj_priv->madv != I915_MADV_WILLNEED) {
1561 DRM_ERROR("Attempting to mmap a purgeable buffer\n");
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001562 ret = -EINVAL;
1563 goto out;
Chris Wilsonab182822009-09-22 18:46:17 +01001564 }
1565
Chris Wilson39a01d12010-10-28 13:03:06 +01001566 if (!obj->map_list.map) {
Jesse Barnesde151cf2008-11-12 10:03:55 -08001567 ret = i915_gem_create_mmap_offset(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001568 if (ret)
1569 goto out;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001570 }
1571
Chris Wilson39a01d12010-10-28 13:03:06 +01001572 args->offset = (u64)obj->map_list.hash.key << PAGE_SHIFT;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001573
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001574out:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001575 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001576unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001577 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001578 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001579}
1580
Chris Wilsone5281cc2010-10-28 13:45:36 +01001581static int
1582i915_gem_object_get_pages_gtt(struct drm_gem_object *obj,
1583 gfp_t gfpmask)
1584{
1585 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
1586 int page_count, i;
1587 struct address_space *mapping;
1588 struct inode *inode;
1589 struct page *page;
1590
1591 /* Get the list of pages out of our struct file. They'll be pinned
1592 * at this point until we release them.
1593 */
1594 page_count = obj->size / PAGE_SIZE;
1595 BUG_ON(obj_priv->pages != NULL);
1596 obj_priv->pages = drm_malloc_ab(page_count, sizeof(struct page *));
1597 if (obj_priv->pages == NULL)
1598 return -ENOMEM;
1599
1600 inode = obj->filp->f_path.dentry->d_inode;
1601 mapping = inode->i_mapping;
1602 for (i = 0; i < page_count; i++) {
1603 page = read_cache_page_gfp(mapping, i,
1604 GFP_HIGHUSER |
1605 __GFP_COLD |
1606 __GFP_RECLAIMABLE |
1607 gfpmask);
1608 if (IS_ERR(page))
1609 goto err_pages;
1610
1611 obj_priv->pages[i] = page;
1612 }
1613
1614 if (obj_priv->tiling_mode != I915_TILING_NONE)
1615 i915_gem_object_do_bit_17_swizzle(obj);
1616
1617 return 0;
1618
1619err_pages:
1620 while (i--)
1621 page_cache_release(obj_priv->pages[i]);
1622
1623 drm_free_large(obj_priv->pages);
1624 obj_priv->pages = NULL;
1625 return PTR_ERR(page);
1626}
1627
Chris Wilson5cdf5882010-09-27 15:51:07 +01001628static void
Chris Wilsone5281cc2010-10-28 13:45:36 +01001629i915_gem_object_put_pages_gtt(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07001630{
Daniel Vetter23010e42010-03-08 13:35:02 +01001631 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001632 int page_count = obj->size / PAGE_SIZE;
1633 int i;
1634
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001635 BUG_ON(obj_priv->madv == __I915_MADV_PURGED);
Eric Anholt856fa192009-03-19 14:10:50 -07001636
Eric Anholt280b7132009-03-12 16:56:27 -07001637 if (obj_priv->tiling_mode != I915_TILING_NONE)
1638 i915_gem_object_save_bit_17_swizzle(obj);
1639
Chris Wilson3ef94da2009-09-14 16:50:29 +01001640 if (obj_priv->madv == I915_MADV_DONTNEED)
Chris Wilson13a05fd2009-09-20 23:03:19 +01001641 obj_priv->dirty = 0;
Chris Wilson3ef94da2009-09-14 16:50:29 +01001642
1643 for (i = 0; i < page_count; i++) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01001644 if (obj_priv->dirty)
1645 set_page_dirty(obj_priv->pages[i]);
1646
1647 if (obj_priv->madv == I915_MADV_WILLNEED)
Eric Anholt856fa192009-03-19 14:10:50 -07001648 mark_page_accessed(obj_priv->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001649
1650 page_cache_release(obj_priv->pages[i]);
1651 }
Eric Anholt673a3942008-07-30 12:06:12 -07001652 obj_priv->dirty = 0;
1653
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07001654 drm_free_large(obj_priv->pages);
Eric Anholt856fa192009-03-19 14:10:50 -07001655 obj_priv->pages = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001656}
1657
Chris Wilsona56ba562010-09-28 10:07:56 +01001658static uint32_t
1659i915_gem_next_request_seqno(struct drm_device *dev,
1660 struct intel_ring_buffer *ring)
1661{
1662 drm_i915_private_t *dev_priv = dev->dev_private;
1663
1664 ring->outstanding_lazy_request = true;
1665 return dev_priv->next_seqno;
1666}
1667
Eric Anholt673a3942008-07-30 12:06:12 -07001668static void
Daniel Vetter617dbe22010-02-11 22:16:02 +01001669i915_gem_object_move_to_active(struct drm_gem_object *obj,
Zou Nan hai852835f2010-05-21 09:08:56 +08001670 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001671{
1672 struct drm_device *dev = obj->dev;
Chris Wilson69dc4982010-10-19 10:36:51 +01001673 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001674 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsona56ba562010-09-28 10:07:56 +01001675 uint32_t seqno = i915_gem_next_request_seqno(dev, ring);
Daniel Vetter617dbe22010-02-11 22:16:02 +01001676
Zou Nan hai852835f2010-05-21 09:08:56 +08001677 BUG_ON(ring == NULL);
1678 obj_priv->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001679
1680 /* Add a reference if we're newly entering the active list. */
1681 if (!obj_priv->active) {
1682 drm_gem_object_reference(obj);
1683 obj_priv->active = 1;
1684 }
Daniel Vettere35a41d2010-02-11 22:13:59 +01001685
Eric Anholt673a3942008-07-30 12:06:12 -07001686 /* Move from whatever list we were on to the tail of execution. */
Chris Wilson69dc4982010-10-19 10:36:51 +01001687 list_move_tail(&obj_priv->mm_list, &dev_priv->mm.active_list);
1688 list_move_tail(&obj_priv->ring_list, &ring->active_list);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001689 obj_priv->last_rendering_seqno = seqno;
Eric Anholt673a3942008-07-30 12:06:12 -07001690}
1691
Eric Anholtce44b0e2008-11-06 16:00:31 -08001692static void
1693i915_gem_object_move_to_flushing(struct drm_gem_object *obj)
1694{
1695 struct drm_device *dev = obj->dev;
1696 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001697 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001698
1699 BUG_ON(!obj_priv->active);
Chris Wilson69dc4982010-10-19 10:36:51 +01001700 list_move_tail(&obj_priv->mm_list, &dev_priv->mm.flushing_list);
1701 list_del_init(&obj_priv->ring_list);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001702 obj_priv->last_rendering_seqno = 0;
1703}
Eric Anholt673a3942008-07-30 12:06:12 -07001704
Chris Wilson963b4832009-09-20 23:03:54 +01001705/* Immediately discard the backing storage */
1706static void
1707i915_gem_object_truncate(struct drm_gem_object *obj)
1708{
Daniel Vetter23010e42010-03-08 13:35:02 +01001709 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001710 struct inode *inode;
Chris Wilson963b4832009-09-20 23:03:54 +01001711
Chris Wilsonae9fed62010-08-07 11:01:30 +01001712 /* Our goal here is to return as much of the memory as
1713 * is possible back to the system as we are called from OOM.
1714 * To do this we must instruct the shmfs to drop all of its
1715 * backing pages, *now*. Here we mirror the actions taken
1716 * when by shmem_delete_inode() to release the backing store.
1717 */
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001718 inode = obj->filp->f_path.dentry->d_inode;
Chris Wilsonae9fed62010-08-07 11:01:30 +01001719 truncate_inode_pages(inode->i_mapping, 0);
1720 if (inode->i_op->truncate_range)
1721 inode->i_op->truncate_range(inode, 0, (loff_t)-1);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001722
1723 obj_priv->madv = __I915_MADV_PURGED;
Chris Wilson963b4832009-09-20 23:03:54 +01001724}
1725
1726static inline int
1727i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj_priv)
1728{
1729 return obj_priv->madv == I915_MADV_DONTNEED;
1730}
1731
Eric Anholt673a3942008-07-30 12:06:12 -07001732static void
1733i915_gem_object_move_to_inactive(struct drm_gem_object *obj)
1734{
1735 struct drm_device *dev = obj->dev;
1736 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001737 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001738
Eric Anholt673a3942008-07-30 12:06:12 -07001739 if (obj_priv->pin_count != 0)
Chris Wilson69dc4982010-10-19 10:36:51 +01001740 list_move_tail(&obj_priv->mm_list, &dev_priv->mm.pinned_list);
Eric Anholt673a3942008-07-30 12:06:12 -07001741 else
Chris Wilson69dc4982010-10-19 10:36:51 +01001742 list_move_tail(&obj_priv->mm_list, &dev_priv->mm.inactive_list);
1743 list_del_init(&obj_priv->ring_list);
Eric Anholt673a3942008-07-30 12:06:12 -07001744
Daniel Vetter99fcb762010-02-07 16:20:18 +01001745 BUG_ON(!list_empty(&obj_priv->gpu_write_list));
1746
Eric Anholtce44b0e2008-11-06 16:00:31 -08001747 obj_priv->last_rendering_seqno = 0;
Zou Nan hai852835f2010-05-21 09:08:56 +08001748 obj_priv->ring = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001749 if (obj_priv->active) {
1750 obj_priv->active = 0;
1751 drm_gem_object_unreference(obj);
1752 }
Chris Wilson23bc5982010-09-29 16:10:57 +01001753 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07001754}
1755
Daniel Vetter63560392010-02-19 11:51:59 +01001756static void
1757i915_gem_process_flushing_list(struct drm_device *dev,
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001758 uint32_t flush_domains,
Zou Nan hai852835f2010-05-21 09:08:56 +08001759 struct intel_ring_buffer *ring)
Daniel Vetter63560392010-02-19 11:51:59 +01001760{
1761 drm_i915_private_t *dev_priv = dev->dev_private;
1762 struct drm_i915_gem_object *obj_priv, *next;
1763
1764 list_for_each_entry_safe(obj_priv, next,
Chris Wilson64193402010-10-24 12:38:05 +01001765 &ring->gpu_write_list,
Daniel Vetter63560392010-02-19 11:51:59 +01001766 gpu_write_list) {
Daniel Vettera8089e82010-04-09 19:05:09 +00001767 struct drm_gem_object *obj = &obj_priv->base;
Daniel Vetter63560392010-02-19 11:51:59 +01001768
Chris Wilson64193402010-10-24 12:38:05 +01001769 if (obj->write_domain & flush_domains) {
Daniel Vetter63560392010-02-19 11:51:59 +01001770 uint32_t old_write_domain = obj->write_domain;
1771
1772 obj->write_domain = 0;
1773 list_del_init(&obj_priv->gpu_write_list);
Daniel Vetter617dbe22010-02-11 22:16:02 +01001774 i915_gem_object_move_to_active(obj, ring);
Daniel Vetter63560392010-02-19 11:51:59 +01001775
1776 /* update the fence lru list */
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001777 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
1778 struct drm_i915_fence_reg *reg =
1779 &dev_priv->fence_regs[obj_priv->fence_reg];
1780 list_move_tail(&reg->lru_list,
Daniel Vetter63560392010-02-19 11:51:59 +01001781 &dev_priv->mm.fence_list);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001782 }
Daniel Vetter63560392010-02-19 11:51:59 +01001783
1784 trace_i915_gem_object_change_domain(obj,
1785 obj->read_domains,
1786 old_write_domain);
1787 }
1788 }
1789}
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001790
Chris Wilson3cce4692010-10-27 16:11:02 +01001791int
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001792i915_add_request(struct drm_device *dev,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001793 struct drm_file *file,
Chris Wilson8dc5d142010-08-12 12:36:12 +01001794 struct drm_i915_gem_request *request,
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001795 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001796{
1797 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001798 struct drm_i915_file_private *file_priv = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001799 uint32_t seqno;
1800 int was_empty;
Chris Wilson3cce4692010-10-27 16:11:02 +01001801 int ret;
1802
1803 BUG_ON(request == NULL);
Eric Anholt673a3942008-07-30 12:06:12 -07001804
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001805 if (file != NULL)
1806 file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00001807
Chris Wilson3cce4692010-10-27 16:11:02 +01001808 ret = ring->add_request(ring, &seqno);
1809 if (ret)
1810 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001811
Chris Wilsona56ba562010-09-28 10:07:56 +01001812 ring->outstanding_lazy_request = false;
Eric Anholt673a3942008-07-30 12:06:12 -07001813
1814 request->seqno = seqno;
Zou Nan hai852835f2010-05-21 09:08:56 +08001815 request->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001816 request->emitted_jiffies = jiffies;
Zou Nan hai852835f2010-05-21 09:08:56 +08001817 was_empty = list_empty(&ring->request_list);
1818 list_add_tail(&request->list, &ring->request_list);
1819
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001820 if (file_priv) {
Chris Wilson1c255952010-09-26 11:03:27 +01001821 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001822 request->file_priv = file_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00001823 list_add_tail(&request->client_list,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001824 &file_priv->mm.request_list);
Chris Wilson1c255952010-09-26 11:03:27 +01001825 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00001826 }
Eric Anholt673a3942008-07-30 12:06:12 -07001827
Ben Gamarif65d9422009-09-14 17:48:44 -04001828 if (!dev_priv->mm.suspended) {
Chris Wilsonb3b079d2010-09-13 23:44:34 +01001829 mod_timer(&dev_priv->hangcheck_timer,
1830 jiffies + msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
Ben Gamarif65d9422009-09-14 17:48:44 -04001831 if (was_empty)
Chris Wilsonb3b079d2010-09-13 23:44:34 +01001832 queue_delayed_work(dev_priv->wq,
1833 &dev_priv->mm.retire_work, HZ);
Ben Gamarif65d9422009-09-14 17:48:44 -04001834 }
Chris Wilson3cce4692010-10-27 16:11:02 +01001835 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001836}
1837
1838/**
1839 * Command execution barrier
1840 *
1841 * Ensures that all commands in the ring are finished
1842 * before signalling the CPU
1843 */
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001844static void
Zou Nan hai852835f2010-05-21 09:08:56 +08001845i915_retire_commands(struct drm_device *dev, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001846{
Eric Anholt673a3942008-07-30 12:06:12 -07001847 uint32_t flush_domains = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001848
1849 /* The sampler always gets flushed on i965 (sigh) */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001850 if (INTEL_INFO(dev)->gen >= 4)
Eric Anholt673a3942008-07-30 12:06:12 -07001851 flush_domains |= I915_GEM_DOMAIN_SAMPLER;
Zou Nan hai852835f2010-05-21 09:08:56 +08001852
Chris Wilson78501ea2010-10-27 12:18:21 +01001853 ring->flush(ring, I915_GEM_DOMAIN_COMMAND, flush_domains);
Eric Anholt673a3942008-07-30 12:06:12 -07001854}
1855
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001856static inline void
1857i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
Eric Anholt673a3942008-07-30 12:06:12 -07001858{
Chris Wilson1c255952010-09-26 11:03:27 +01001859 struct drm_i915_file_private *file_priv = request->file_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07001860
Chris Wilson1c255952010-09-26 11:03:27 +01001861 if (!file_priv)
1862 return;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001863
Chris Wilson1c255952010-09-26 11:03:27 +01001864 spin_lock(&file_priv->mm.lock);
1865 list_del(&request->client_list);
1866 request->file_priv = NULL;
1867 spin_unlock(&file_priv->mm.lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001868}
1869
Chris Wilsondfaae392010-09-22 10:31:52 +01001870static void i915_gem_reset_ring_lists(struct drm_i915_private *dev_priv,
1871 struct intel_ring_buffer *ring)
Chris Wilson9375e442010-09-19 12:21:28 +01001872{
Chris Wilsondfaae392010-09-22 10:31:52 +01001873 while (!list_empty(&ring->request_list)) {
1874 struct drm_i915_gem_request *request;
Chris Wilson9375e442010-09-19 12:21:28 +01001875
Chris Wilsondfaae392010-09-22 10:31:52 +01001876 request = list_first_entry(&ring->request_list,
1877 struct drm_i915_gem_request,
1878 list);
1879
1880 list_del(&request->list);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001881 i915_gem_request_remove_from_client(request);
Chris Wilsondfaae392010-09-22 10:31:52 +01001882 kfree(request);
1883 }
1884
1885 while (!list_empty(&ring->active_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001886 struct drm_i915_gem_object *obj_priv;
1887
Chris Wilsondfaae392010-09-22 10:31:52 +01001888 obj_priv = list_first_entry(&ring->active_list,
Eric Anholt673a3942008-07-30 12:06:12 -07001889 struct drm_i915_gem_object,
Chris Wilson69dc4982010-10-19 10:36:51 +01001890 ring_list);
Eric Anholt673a3942008-07-30 12:06:12 -07001891
Chris Wilsondfaae392010-09-22 10:31:52 +01001892 obj_priv->base.write_domain = 0;
1893 list_del_init(&obj_priv->gpu_write_list);
1894 i915_gem_object_move_to_inactive(&obj_priv->base);
Eric Anholt673a3942008-07-30 12:06:12 -07001895 }
Eric Anholt673a3942008-07-30 12:06:12 -07001896}
1897
Chris Wilson069efc12010-09-30 16:53:18 +01001898void i915_gem_reset(struct drm_device *dev)
Eric Anholt673a3942008-07-30 12:06:12 -07001899{
Chris Wilsondfaae392010-09-22 10:31:52 +01001900 struct drm_i915_private *dev_priv = dev->dev_private;
1901 struct drm_i915_gem_object *obj_priv;
Chris Wilson069efc12010-09-30 16:53:18 +01001902 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07001903
Chris Wilsondfaae392010-09-22 10:31:52 +01001904 i915_gem_reset_ring_lists(dev_priv, &dev_priv->render_ring);
Chris Wilson87acb0a2010-10-19 10:13:00 +01001905 i915_gem_reset_ring_lists(dev_priv, &dev_priv->bsd_ring);
Chris Wilson549f7362010-10-19 11:19:32 +01001906 i915_gem_reset_ring_lists(dev_priv, &dev_priv->blt_ring);
Chris Wilsondfaae392010-09-22 10:31:52 +01001907
1908 /* Remove anything from the flushing lists. The GPU cache is likely
1909 * to be lost on reset along with the data, so simply move the
1910 * lost bo to the inactive list.
1911 */
1912 while (!list_empty(&dev_priv->mm.flushing_list)) {
Chris Wilson9375e442010-09-19 12:21:28 +01001913 obj_priv = list_first_entry(&dev_priv->mm.flushing_list,
1914 struct drm_i915_gem_object,
Chris Wilson69dc4982010-10-19 10:36:51 +01001915 mm_list);
Chris Wilson9375e442010-09-19 12:21:28 +01001916
1917 obj_priv->base.write_domain = 0;
Chris Wilsondfaae392010-09-22 10:31:52 +01001918 list_del_init(&obj_priv->gpu_write_list);
Chris Wilson9375e442010-09-19 12:21:28 +01001919 i915_gem_object_move_to_inactive(&obj_priv->base);
1920 }
Chris Wilson9375e442010-09-19 12:21:28 +01001921
Chris Wilsondfaae392010-09-22 10:31:52 +01001922 /* Move everything out of the GPU domains to ensure we do any
1923 * necessary invalidation upon reuse.
1924 */
Chris Wilson77f01232010-09-19 12:31:36 +01001925 list_for_each_entry(obj_priv,
1926 &dev_priv->mm.inactive_list,
Chris Wilson69dc4982010-10-19 10:36:51 +01001927 mm_list)
Chris Wilson77f01232010-09-19 12:31:36 +01001928 {
1929 obj_priv->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
1930 }
Chris Wilson069efc12010-09-30 16:53:18 +01001931
1932 /* The fence registers are invalidated so clear them out */
1933 for (i = 0; i < 16; i++) {
1934 struct drm_i915_fence_reg *reg;
1935
1936 reg = &dev_priv->fence_regs[i];
1937 if (!reg->obj)
1938 continue;
1939
1940 i915_gem_clear_fence_reg(reg->obj);
1941 }
Eric Anholt673a3942008-07-30 12:06:12 -07001942}
1943
1944/**
1945 * This function clears the request list as sequence numbers are passed.
1946 */
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001947static void
1948i915_gem_retire_requests_ring(struct drm_device *dev,
1949 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001950{
1951 drm_i915_private_t *dev_priv = dev->dev_private;
1952 uint32_t seqno;
1953
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001954 if (!ring->status_page.page_addr ||
1955 list_empty(&ring->request_list))
Karsten Wiese6c0594a2009-02-23 15:07:57 +01001956 return;
1957
Chris Wilson23bc5982010-09-29 16:10:57 +01001958 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07001959
Chris Wilson78501ea2010-10-27 12:18:21 +01001960 seqno = ring->get_seqno(ring);
Zou Nan hai852835f2010-05-21 09:08:56 +08001961 while (!list_empty(&ring->request_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001962 struct drm_i915_gem_request *request;
Eric Anholt673a3942008-07-30 12:06:12 -07001963
Zou Nan hai852835f2010-05-21 09:08:56 +08001964 request = list_first_entry(&ring->request_list,
Eric Anholt673a3942008-07-30 12:06:12 -07001965 struct drm_i915_gem_request,
1966 list);
Eric Anholt673a3942008-07-30 12:06:12 -07001967
Chris Wilsondfaae392010-09-22 10:31:52 +01001968 if (!i915_seqno_passed(seqno, request->seqno))
Eric Anholt673a3942008-07-30 12:06:12 -07001969 break;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001970
1971 trace_i915_gem_request_retire(dev, request->seqno);
1972
1973 list_del(&request->list);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001974 i915_gem_request_remove_from_client(request);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001975 kfree(request);
1976 }
1977
1978 /* Move any buffers on the active list that are no longer referenced
1979 * by the ringbuffer to the flushing/inactive lists as appropriate.
1980 */
1981 while (!list_empty(&ring->active_list)) {
1982 struct drm_gem_object *obj;
1983 struct drm_i915_gem_object *obj_priv;
1984
1985 obj_priv = list_first_entry(&ring->active_list,
1986 struct drm_i915_gem_object,
Chris Wilson69dc4982010-10-19 10:36:51 +01001987 ring_list);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001988
Chris Wilsondfaae392010-09-22 10:31:52 +01001989 if (!i915_seqno_passed(seqno, obj_priv->last_rendering_seqno))
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001990 break;
1991
1992 obj = &obj_priv->base;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001993 if (obj->write_domain != 0)
1994 i915_gem_object_move_to_flushing(obj);
1995 else
1996 i915_gem_object_move_to_inactive(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001997 }
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001998
1999 if (unlikely (dev_priv->trace_irq_seqno &&
2000 i915_seqno_passed(dev_priv->trace_irq_seqno, seqno))) {
Chris Wilson78501ea2010-10-27 12:18:21 +01002001 ring->user_irq_put(ring);
Chris Wilson9d34e5d2009-09-24 05:26:06 +01002002 dev_priv->trace_irq_seqno = 0;
2003 }
Chris Wilson23bc5982010-09-29 16:10:57 +01002004
2005 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07002006}
2007
2008void
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002009i915_gem_retire_requests(struct drm_device *dev)
2010{
2011 drm_i915_private_t *dev_priv = dev->dev_private;
2012
Chris Wilsonbe726152010-07-23 23:18:50 +01002013 if (!list_empty(&dev_priv->mm.deferred_free_list)) {
2014 struct drm_i915_gem_object *obj_priv, *tmp;
2015
2016 /* We must be careful that during unbind() we do not
2017 * accidentally infinitely recurse into retire requests.
2018 * Currently:
2019 * retire -> free -> unbind -> wait -> retire_ring
2020 */
2021 list_for_each_entry_safe(obj_priv, tmp,
2022 &dev_priv->mm.deferred_free_list,
Chris Wilson69dc4982010-10-19 10:36:51 +01002023 mm_list)
Chris Wilsonbe726152010-07-23 23:18:50 +01002024 i915_gem_free_object_tail(&obj_priv->base);
2025 }
2026
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002027 i915_gem_retire_requests_ring(dev, &dev_priv->render_ring);
Chris Wilson87acb0a2010-10-19 10:13:00 +01002028 i915_gem_retire_requests_ring(dev, &dev_priv->bsd_ring);
Chris Wilson549f7362010-10-19 11:19:32 +01002029 i915_gem_retire_requests_ring(dev, &dev_priv->blt_ring);
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002030}
2031
Daniel Vetter75ef9da2010-08-21 00:25:16 +02002032static void
Eric Anholt673a3942008-07-30 12:06:12 -07002033i915_gem_retire_work_handler(struct work_struct *work)
2034{
2035 drm_i915_private_t *dev_priv;
2036 struct drm_device *dev;
2037
2038 dev_priv = container_of(work, drm_i915_private_t,
2039 mm.retire_work.work);
2040 dev = dev_priv->dev;
2041
Chris Wilson891b48c2010-09-29 12:26:37 +01002042 /* Come back later if the device is busy... */
2043 if (!mutex_trylock(&dev->struct_mutex)) {
2044 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
2045 return;
2046 }
2047
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002048 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08002049
Keith Packard6dbe2772008-10-14 21:41:13 -07002050 if (!dev_priv->mm.suspended &&
Zou Nan haid1b851f2010-05-21 09:08:57 +08002051 (!list_empty(&dev_priv->render_ring.request_list) ||
Chris Wilson549f7362010-10-19 11:19:32 +01002052 !list_empty(&dev_priv->bsd_ring.request_list) ||
2053 !list_empty(&dev_priv->blt_ring.request_list)))
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07002054 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
Eric Anholt673a3942008-07-30 12:06:12 -07002055 mutex_unlock(&dev->struct_mutex);
2056}
2057
Daniel Vetter5a5a0c62009-09-15 22:57:36 +02002058int
Zou Nan hai852835f2010-05-21 09:08:56 +08002059i915_do_wait_request(struct drm_device *dev, uint32_t seqno,
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002060 bool interruptible, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07002061{
2062 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes802c7eb2009-05-05 16:03:48 -07002063 u32 ier;
Eric Anholt673a3942008-07-30 12:06:12 -07002064 int ret = 0;
2065
2066 BUG_ON(seqno == 0);
2067
Ben Gamariba1234d2009-09-14 17:48:47 -04002068 if (atomic_read(&dev_priv->mm.wedged))
Chris Wilson30dbf0c2010-09-25 10:19:17 +01002069 return -EAGAIN;
Ben Gamariffed1d02009-09-14 17:48:41 -04002070
Chris Wilsona56ba562010-09-28 10:07:56 +01002071 if (ring->outstanding_lazy_request) {
Chris Wilson3cce4692010-10-27 16:11:02 +01002072 struct drm_i915_gem_request *request;
2073
2074 request = kzalloc(sizeof(*request), GFP_KERNEL);
2075 if (request == NULL)
Daniel Vettere35a41d2010-02-11 22:13:59 +01002076 return -ENOMEM;
Chris Wilson3cce4692010-10-27 16:11:02 +01002077
2078 ret = i915_add_request(dev, NULL, request, ring);
2079 if (ret) {
2080 kfree(request);
2081 return ret;
2082 }
2083
2084 seqno = request->seqno;
Daniel Vettere35a41d2010-02-11 22:13:59 +01002085 }
Chris Wilsona56ba562010-09-28 10:07:56 +01002086 BUG_ON(seqno == dev_priv->next_seqno);
Daniel Vettere35a41d2010-02-11 22:13:59 +01002087
Chris Wilson78501ea2010-10-27 12:18:21 +01002088 if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) {
Eric Anholtbad720f2009-10-22 16:11:14 -07002089 if (HAS_PCH_SPLIT(dev))
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002090 ier = I915_READ(DEIER) | I915_READ(GTIER);
2091 else
2092 ier = I915_READ(IER);
Jesse Barnes802c7eb2009-05-05 16:03:48 -07002093 if (!ier) {
2094 DRM_ERROR("something (likely vbetool) disabled "
2095 "interrupts, re-enabling\n");
2096 i915_driver_irq_preinstall(dev);
2097 i915_driver_irq_postinstall(dev);
2098 }
2099
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002100 trace_i915_gem_request_wait_begin(dev, seqno);
2101
Chris Wilsonb2223492010-10-27 15:27:33 +01002102 ring->waiting_seqno = seqno;
Chris Wilson78501ea2010-10-27 12:18:21 +01002103 ring->user_irq_get(ring);
Daniel Vetter48764bf2009-09-15 22:57:32 +02002104 if (interruptible)
Zou Nan hai852835f2010-05-21 09:08:56 +08002105 ret = wait_event_interruptible(ring->irq_queue,
Chris Wilson78501ea2010-10-27 12:18:21 +01002106 i915_seqno_passed(ring->get_seqno(ring), seqno)
Zou Nan hai852835f2010-05-21 09:08:56 +08002107 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02002108 else
Zou Nan hai852835f2010-05-21 09:08:56 +08002109 wait_event(ring->irq_queue,
Chris Wilson78501ea2010-10-27 12:18:21 +01002110 i915_seqno_passed(ring->get_seqno(ring), seqno)
Zou Nan hai852835f2010-05-21 09:08:56 +08002111 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02002112
Chris Wilson78501ea2010-10-27 12:18:21 +01002113 ring->user_irq_put(ring);
Chris Wilsonb2223492010-10-27 15:27:33 +01002114 ring->waiting_seqno = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002115
2116 trace_i915_gem_request_wait_end(dev, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07002117 }
Ben Gamariba1234d2009-09-14 17:48:47 -04002118 if (atomic_read(&dev_priv->mm.wedged))
Chris Wilson30dbf0c2010-09-25 10:19:17 +01002119 ret = -EAGAIN;
Eric Anholt673a3942008-07-30 12:06:12 -07002120
2121 if (ret && ret != -ERESTARTSYS)
Daniel Vetter8bff9172010-02-11 22:19:40 +01002122 DRM_ERROR("%s returns %d (awaiting %d at %d, next %d)\n",
Chris Wilson78501ea2010-10-27 12:18:21 +01002123 __func__, ret, seqno, ring->get_seqno(ring),
Daniel Vetter8bff9172010-02-11 22:19:40 +01002124 dev_priv->next_seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07002125
2126 /* Directly dispatch request retiring. While we have the work queue
2127 * to handle this, the waiter on a request often wants an associated
2128 * buffer to have made it to the inactive list, and we would need
2129 * a separate wait queue to handle that.
2130 */
2131 if (ret == 0)
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002132 i915_gem_retire_requests_ring(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07002133
2134 return ret;
2135}
2136
Daniel Vetter48764bf2009-09-15 22:57:32 +02002137/**
2138 * Waits for a sequence number to be signaled, and cleans up the
2139 * request and object lists appropriately for that event.
2140 */
2141static int
Zou Nan hai852835f2010-05-21 09:08:56 +08002142i915_wait_request(struct drm_device *dev, uint32_t seqno,
Chris Wilsona56ba562010-09-28 10:07:56 +01002143 struct intel_ring_buffer *ring)
Daniel Vetter48764bf2009-09-15 22:57:32 +02002144{
Zou Nan hai852835f2010-05-21 09:08:56 +08002145 return i915_do_wait_request(dev, seqno, 1, ring);
Daniel Vetter48764bf2009-09-15 22:57:32 +02002146}
2147
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002148static void
Chris Wilson92204342010-09-18 11:02:01 +01002149i915_gem_flush_ring(struct drm_device *dev,
Chris Wilsonc78ec302010-09-20 12:50:23 +01002150 struct drm_file *file_priv,
Chris Wilson92204342010-09-18 11:02:01 +01002151 struct intel_ring_buffer *ring,
2152 uint32_t invalidate_domains,
2153 uint32_t flush_domains)
2154{
Chris Wilson78501ea2010-10-27 12:18:21 +01002155 ring->flush(ring, invalidate_domains, flush_domains);
Chris Wilson92204342010-09-18 11:02:01 +01002156 i915_gem_process_flushing_list(dev, flush_domains, ring);
2157}
2158
2159static void
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002160i915_gem_flush(struct drm_device *dev,
Chris Wilsonc78ec302010-09-20 12:50:23 +01002161 struct drm_file *file_priv,
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002162 uint32_t invalidate_domains,
Chris Wilson92204342010-09-18 11:02:01 +01002163 uint32_t flush_domains,
2164 uint32_t flush_rings)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002165{
2166 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter8bff9172010-02-11 22:19:40 +01002167
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002168 if (flush_domains & I915_GEM_DOMAIN_CPU)
2169 drm_agp_chipset_flush(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08002170
Chris Wilson92204342010-09-18 11:02:01 +01002171 if ((flush_domains | invalidate_domains) & I915_GEM_GPU_DOMAINS) {
2172 if (flush_rings & RING_RENDER)
Chris Wilsonc78ec302010-09-20 12:50:23 +01002173 i915_gem_flush_ring(dev, file_priv,
Chris Wilson92204342010-09-18 11:02:01 +01002174 &dev_priv->render_ring,
2175 invalidate_domains, flush_domains);
2176 if (flush_rings & RING_BSD)
Chris Wilsonc78ec302010-09-20 12:50:23 +01002177 i915_gem_flush_ring(dev, file_priv,
Chris Wilson92204342010-09-18 11:02:01 +01002178 &dev_priv->bsd_ring,
2179 invalidate_domains, flush_domains);
Chris Wilson549f7362010-10-19 11:19:32 +01002180 if (flush_rings & RING_BLT)
2181 i915_gem_flush_ring(dev, file_priv,
2182 &dev_priv->blt_ring,
2183 invalidate_domains, flush_domains);
Chris Wilson92204342010-09-18 11:02:01 +01002184 }
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002185}
2186
Eric Anholt673a3942008-07-30 12:06:12 -07002187/**
2188 * Ensures that all rendering to the object has completed and the object is
2189 * safe to unbind from the GTT or access from the CPU.
2190 */
2191static int
Chris Wilson2cf34d72010-09-14 13:03:28 +01002192i915_gem_object_wait_rendering(struct drm_gem_object *obj,
2193 bool interruptible)
Eric Anholt673a3942008-07-30 12:06:12 -07002194{
2195 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01002196 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002197 int ret;
2198
Eric Anholte47c68e2008-11-14 13:35:19 -08002199 /* This function only exists to support waiting for existing rendering,
2200 * not for emitting required flushes.
Eric Anholt673a3942008-07-30 12:06:12 -07002201 */
Eric Anholte47c68e2008-11-14 13:35:19 -08002202 BUG_ON((obj->write_domain & I915_GEM_GPU_DOMAINS) != 0);
Eric Anholt673a3942008-07-30 12:06:12 -07002203
2204 /* If there is rendering queued on the buffer being evicted, wait for
2205 * it.
2206 */
2207 if (obj_priv->active) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01002208 ret = i915_do_wait_request(dev,
2209 obj_priv->last_rendering_seqno,
2210 interruptible,
2211 obj_priv->ring);
2212 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002213 return ret;
2214 }
2215
2216 return 0;
2217}
2218
2219/**
2220 * Unbinds an object from the GTT aperture.
2221 */
Jesse Barnes0f973f22009-01-26 17:10:45 -08002222int
Eric Anholt673a3942008-07-30 12:06:12 -07002223i915_gem_object_unbind(struct drm_gem_object *obj)
2224{
2225 struct drm_device *dev = obj->dev;
Chris Wilson73aa8082010-09-30 11:46:12 +01002226 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002227 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002228 int ret = 0;
2229
Eric Anholt673a3942008-07-30 12:06:12 -07002230 if (obj_priv->gtt_space == NULL)
2231 return 0;
2232
2233 if (obj_priv->pin_count != 0) {
2234 DRM_ERROR("Attempting to unbind pinned buffer\n");
2235 return -EINVAL;
2236 }
2237
Eric Anholt5323fd02009-09-09 11:50:45 -07002238 /* blow away mappings if mapped through GTT */
2239 i915_gem_release_mmap(obj);
2240
Eric Anholt673a3942008-07-30 12:06:12 -07002241 /* Move the object to the CPU domain to ensure that
2242 * any possible CPU writes while it's not in the GTT
2243 * are flushed when we go to remap it. This will
2244 * also ensure that all pending GPU writes are finished
2245 * before we unbind.
2246 */
Eric Anholte47c68e2008-11-14 13:35:19 -08002247 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Chris Wilson8dc17752010-07-23 23:18:51 +01002248 if (ret == -ERESTARTSYS)
Eric Anholt673a3942008-07-30 12:06:12 -07002249 return ret;
Chris Wilson8dc17752010-07-23 23:18:51 +01002250 /* Continue on if we fail due to EIO, the GPU is hung so we
2251 * should be safe and we need to cleanup or else we might
2252 * cause memory corruption through use-after-free.
2253 */
Chris Wilson812ed4922010-09-30 15:08:57 +01002254 if (ret) {
2255 i915_gem_clflush_object(obj);
2256 obj->read_domains = obj->write_domain = I915_GEM_DOMAIN_CPU;
2257 }
Eric Anholt673a3942008-07-30 12:06:12 -07002258
Daniel Vetter96b47b62009-12-15 17:50:00 +01002259 /* release the fence reg _after_ flushing */
2260 if (obj_priv->fence_reg != I915_FENCE_REG_NONE)
2261 i915_gem_clear_fence_reg(obj);
2262
Chris Wilson73aa8082010-09-30 11:46:12 +01002263 drm_unbind_agp(obj_priv->agp_mem);
2264 drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002265
Chris Wilsone5281cc2010-10-28 13:45:36 +01002266 i915_gem_object_put_pages_gtt(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002267
Chris Wilsona00b10c2010-09-24 21:15:47 +01002268 i915_gem_info_remove_gtt(dev_priv, obj_priv);
Chris Wilson69dc4982010-10-19 10:36:51 +01002269 list_del_init(&obj_priv->mm_list);
Chris Wilsona00b10c2010-09-24 21:15:47 +01002270 obj_priv->fenceable = true;
2271 obj_priv->mappable = true;
Eric Anholt673a3942008-07-30 12:06:12 -07002272
Chris Wilson73aa8082010-09-30 11:46:12 +01002273 drm_mm_put_block(obj_priv->gtt_space);
2274 obj_priv->gtt_space = NULL;
Chris Wilson9af90d12010-10-17 10:01:56 +01002275 obj_priv->gtt_offset = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002276
Chris Wilson963b4832009-09-20 23:03:54 +01002277 if (i915_gem_object_is_purgeable(obj_priv))
2278 i915_gem_object_truncate(obj);
2279
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002280 trace_i915_gem_object_unbind(obj);
2281
Chris Wilson8dc17752010-07-23 23:18:51 +01002282 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002283}
2284
Chris Wilsona56ba562010-09-28 10:07:56 +01002285static int i915_ring_idle(struct drm_device *dev,
2286 struct intel_ring_buffer *ring)
2287{
Chris Wilson64193402010-10-24 12:38:05 +01002288 if (list_empty(&ring->gpu_write_list))
2289 return 0;
2290
Chris Wilsona56ba562010-09-28 10:07:56 +01002291 i915_gem_flush_ring(dev, NULL, ring,
2292 I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
2293 return i915_wait_request(dev,
2294 i915_gem_next_request_seqno(dev, ring),
2295 ring);
2296}
2297
Chris Wilsonb47eb4a2010-08-07 11:01:23 +01002298int
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002299i915_gpu_idle(struct drm_device *dev)
2300{
2301 drm_i915_private_t *dev_priv = dev->dev_private;
2302 bool lists_empty;
Zou Nan hai852835f2010-05-21 09:08:56 +08002303 int ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002304
Zou Nan haid1b851f2010-05-21 09:08:57 +08002305 lists_empty = (list_empty(&dev_priv->mm.flushing_list) &&
2306 list_empty(&dev_priv->render_ring.active_list) &&
Chris Wilson549f7362010-10-19 11:19:32 +01002307 list_empty(&dev_priv->bsd_ring.active_list) &&
2308 list_empty(&dev_priv->blt_ring.active_list));
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002309 if (lists_empty)
2310 return 0;
2311
2312 /* Flush everything onto the inactive list. */
Chris Wilsona56ba562010-09-28 10:07:56 +01002313 ret = i915_ring_idle(dev, &dev_priv->render_ring);
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002314 if (ret)
2315 return ret;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002316
Chris Wilson87acb0a2010-10-19 10:13:00 +01002317 ret = i915_ring_idle(dev, &dev_priv->bsd_ring);
2318 if (ret)
2319 return ret;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002320
Chris Wilson549f7362010-10-19 11:19:32 +01002321 ret = i915_ring_idle(dev, &dev_priv->blt_ring);
2322 if (ret)
2323 return ret;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002324
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002325 return 0;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002326}
2327
Chris Wilsona00b10c2010-09-24 21:15:47 +01002328static void sandybridge_write_fence_reg(struct drm_gem_object *obj)
Eric Anholt4e901fd2009-10-26 16:44:17 -07002329{
Eric Anholt4e901fd2009-10-26 16:44:17 -07002330 struct drm_device *dev = obj->dev;
2331 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002332 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsona00b10c2010-09-24 21:15:47 +01002333 u32 size = i915_gem_get_gtt_size(obj_priv);
Eric Anholt4e901fd2009-10-26 16:44:17 -07002334 int regnum = obj_priv->fence_reg;
2335 uint64_t val;
2336
Chris Wilsona00b10c2010-09-24 21:15:47 +01002337 val = (uint64_t)((obj_priv->gtt_offset + size - 4096) &
Eric Anholt4e901fd2009-10-26 16:44:17 -07002338 0xfffff000) << 32;
2339 val |= obj_priv->gtt_offset & 0xfffff000;
2340 val |= (uint64_t)((obj_priv->stride / 128) - 1) <<
2341 SANDYBRIDGE_FENCE_PITCH_SHIFT;
2342
2343 if (obj_priv->tiling_mode == I915_TILING_Y)
2344 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2345 val |= I965_FENCE_REG_VALID;
2346
2347 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (regnum * 8), val);
2348}
2349
Chris Wilsona00b10c2010-09-24 21:15:47 +01002350static void i965_write_fence_reg(struct drm_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002351{
Jesse Barnesde151cf2008-11-12 10:03:55 -08002352 struct drm_device *dev = obj->dev;
2353 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002354 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsona00b10c2010-09-24 21:15:47 +01002355 u32 size = i915_gem_get_gtt_size(obj_priv);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002356 int regnum = obj_priv->fence_reg;
2357 uint64_t val;
2358
Chris Wilsona00b10c2010-09-24 21:15:47 +01002359 val = (uint64_t)((obj_priv->gtt_offset + size - 4096) &
Jesse Barnesde151cf2008-11-12 10:03:55 -08002360 0xfffff000) << 32;
2361 val |= obj_priv->gtt_offset & 0xfffff000;
2362 val |= ((obj_priv->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
2363 if (obj_priv->tiling_mode == I915_TILING_Y)
2364 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2365 val |= I965_FENCE_REG_VALID;
2366
2367 I915_WRITE64(FENCE_REG_965_0 + (regnum * 8), val);
2368}
2369
Chris Wilsona00b10c2010-09-24 21:15:47 +01002370static void i915_write_fence_reg(struct drm_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002371{
Jesse Barnesde151cf2008-11-12 10:03:55 -08002372 struct drm_device *dev = obj->dev;
2373 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002374 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsona00b10c2010-09-24 21:15:47 +01002375 u32 size = i915_gem_get_gtt_size(obj_priv);
2376 uint32_t fence_reg, val, pitch_val;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002377 int tile_width;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002378
2379 if ((obj_priv->gtt_offset & ~I915_FENCE_START_MASK) ||
Chris Wilsona00b10c2010-09-24 21:15:47 +01002380 (obj_priv->gtt_offset & (size - 1))) {
2381 WARN(1, "%s: object 0x%08x [fenceable? %d] not 1M or size (0x%08x) aligned [gtt_space offset=%lx, size=%lx]\n",
2382 __func__, obj_priv->gtt_offset, obj_priv->fenceable, size,
2383 obj_priv->gtt_space->start, obj_priv->gtt_space->size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002384 return;
2385 }
2386
Jesse Barnes0f973f22009-01-26 17:10:45 -08002387 if (obj_priv->tiling_mode == I915_TILING_Y &&
2388 HAS_128_BYTE_Y_TILING(dev))
2389 tile_width = 128;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002390 else
Jesse Barnes0f973f22009-01-26 17:10:45 -08002391 tile_width = 512;
2392
2393 /* Note: pitch better be a power of two tile widths */
2394 pitch_val = obj_priv->stride / tile_width;
2395 pitch_val = ffs(pitch_val) - 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002396
Daniel Vetterc36a2a62010-04-17 15:12:03 +02002397 if (obj_priv->tiling_mode == I915_TILING_Y &&
2398 HAS_128_BYTE_Y_TILING(dev))
2399 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2400 else
2401 WARN_ON(pitch_val > I915_FENCE_MAX_PITCH_VAL);
2402
Jesse Barnesde151cf2008-11-12 10:03:55 -08002403 val = obj_priv->gtt_offset;
2404 if (obj_priv->tiling_mode == I915_TILING_Y)
2405 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002406 val |= I915_FENCE_SIZE_BITS(size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002407 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2408 val |= I830_FENCE_REG_VALID;
2409
Chris Wilsona00b10c2010-09-24 21:15:47 +01002410 fence_reg = obj_priv->fence_reg;
2411 if (fence_reg < 8)
2412 fence_reg = FENCE_REG_830_0 + fence_reg * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002413 else
Chris Wilsona00b10c2010-09-24 21:15:47 +01002414 fence_reg = FENCE_REG_945_8 + (fence_reg - 8) * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002415 I915_WRITE(fence_reg, val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002416}
2417
Chris Wilsona00b10c2010-09-24 21:15:47 +01002418static void i830_write_fence_reg(struct drm_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002419{
Jesse Barnesde151cf2008-11-12 10:03:55 -08002420 struct drm_device *dev = obj->dev;
2421 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002422 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsona00b10c2010-09-24 21:15:47 +01002423 u32 size = i915_gem_get_gtt_size(obj_priv);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002424 int regnum = obj_priv->fence_reg;
2425 uint32_t val;
2426 uint32_t pitch_val;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002427 uint32_t fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002428
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002429 if ((obj_priv->gtt_offset & ~I830_FENCE_START_MASK) ||
Jesse Barnesde151cf2008-11-12 10:03:55 -08002430 (obj_priv->gtt_offset & (obj->size - 1))) {
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002431 WARN(1, "%s: object 0x%08x not 512K or size aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002432 __func__, obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002433 return;
2434 }
2435
Eric Anholte76a16d2009-05-26 17:44:56 -07002436 pitch_val = obj_priv->stride / 128;
2437 pitch_val = ffs(pitch_val) - 1;
2438 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2439
Jesse Barnesde151cf2008-11-12 10:03:55 -08002440 val = obj_priv->gtt_offset;
2441 if (obj_priv->tiling_mode == I915_TILING_Y)
2442 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002443 fence_size_bits = I830_FENCE_SIZE_BITS(size);
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002444 WARN_ON(fence_size_bits & ~0x00000f00);
2445 val |= fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002446 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2447 val |= I830_FENCE_REG_VALID;
2448
2449 I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002450}
2451
Chris Wilson2cf34d72010-09-14 13:03:28 +01002452static int i915_find_fence_reg(struct drm_device *dev,
2453 bool interruptible)
Daniel Vetterae3db242010-02-19 11:51:58 +01002454{
Daniel Vetterae3db242010-02-19 11:51:58 +01002455 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002456 struct drm_i915_fence_reg *reg;
2457 struct drm_i915_gem_object *obj_priv = NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002458 int i, avail, ret;
2459
2460 /* First try to find a free reg */
2461 avail = 0;
2462 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2463 reg = &dev_priv->fence_regs[i];
2464 if (!reg->obj)
2465 return i;
2466
Daniel Vetter23010e42010-03-08 13:35:02 +01002467 obj_priv = to_intel_bo(reg->obj);
Daniel Vetterae3db242010-02-19 11:51:58 +01002468 if (!obj_priv->pin_count)
2469 avail++;
2470 }
2471
2472 if (avail == 0)
2473 return -ENOSPC;
2474
2475 /* None available, try to steal one or wait for a user to finish */
Chris Wilsona00b10c2010-09-24 21:15:47 +01002476 avail = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002477 list_for_each_entry(reg, &dev_priv->mm.fence_list,
2478 lru_list) {
Chris Wilsona00b10c2010-09-24 21:15:47 +01002479 obj_priv = to_intel_bo(reg->obj);
Daniel Vetterae3db242010-02-19 11:51:58 +01002480 if (obj_priv->pin_count)
2481 continue;
2482
2483 /* found one! */
Chris Wilsona00b10c2010-09-24 21:15:47 +01002484 avail = obj_priv->fence_reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01002485 break;
2486 }
2487
Chris Wilsona00b10c2010-09-24 21:15:47 +01002488 BUG_ON(avail == I915_FENCE_REG_NONE);
Daniel Vetterae3db242010-02-19 11:51:58 +01002489
2490 /* We only have a reference on obj from the active list. put_fence_reg
2491 * might drop that one, causing a use-after-free in it. So hold a
2492 * private reference to obj like the other callers of put_fence_reg
2493 * (set_tiling ioctl) do. */
Chris Wilsona00b10c2010-09-24 21:15:47 +01002494 drm_gem_object_reference(&obj_priv->base);
2495 ret = i915_gem_object_put_fence_reg(&obj_priv->base, interruptible);
2496 drm_gem_object_unreference(&obj_priv->base);
Daniel Vetterae3db242010-02-19 11:51:58 +01002497 if (ret != 0)
2498 return ret;
2499
Chris Wilsona00b10c2010-09-24 21:15:47 +01002500 return avail;
Daniel Vetterae3db242010-02-19 11:51:58 +01002501}
2502
Jesse Barnesde151cf2008-11-12 10:03:55 -08002503/**
2504 * i915_gem_object_get_fence_reg - set up a fence reg for an object
2505 * @obj: object to map through a fence reg
2506 *
2507 * When mapping objects through the GTT, userspace wants to be able to write
2508 * to them without having to worry about swizzling if the object is tiled.
2509 *
2510 * This function walks the fence regs looking for a free one for @obj,
2511 * stealing one if it can't find any.
2512 *
2513 * It then sets up the reg based on the object's properties: address, pitch
2514 * and tiling format.
2515 */
Chris Wilson8c4b8c32009-06-17 22:08:52 +01002516int
Chris Wilson2cf34d72010-09-14 13:03:28 +01002517i915_gem_object_get_fence_reg(struct drm_gem_object *obj,
2518 bool interruptible)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002519{
2520 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002521 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002522 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002523 struct drm_i915_fence_reg *reg = NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002524 int ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002525
Eric Anholta09ba7f2009-08-29 12:49:51 -07002526 /* Just update our place in the LRU if our fence is getting used. */
2527 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002528 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
2529 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002530 return 0;
2531 }
2532
Jesse Barnesde151cf2008-11-12 10:03:55 -08002533 switch (obj_priv->tiling_mode) {
2534 case I915_TILING_NONE:
2535 WARN(1, "allocating a fence for non-tiled object?\n");
2536 break;
2537 case I915_TILING_X:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002538 if (!obj_priv->stride)
2539 return -EINVAL;
2540 WARN((obj_priv->stride & (512 - 1)),
2541 "object 0x%08x is X tiled but has non-512B pitch\n",
2542 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002543 break;
2544 case I915_TILING_Y:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002545 if (!obj_priv->stride)
2546 return -EINVAL;
2547 WARN((obj_priv->stride & (128 - 1)),
2548 "object 0x%08x is Y tiled but has non-128B pitch\n",
2549 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002550 break;
2551 }
2552
Chris Wilson2cf34d72010-09-14 13:03:28 +01002553 ret = i915_find_fence_reg(dev, interruptible);
Daniel Vetterae3db242010-02-19 11:51:58 +01002554 if (ret < 0)
2555 return ret;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002556
Daniel Vetterae3db242010-02-19 11:51:58 +01002557 obj_priv->fence_reg = ret;
2558 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002559 list_add_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002560
Jesse Barnesde151cf2008-11-12 10:03:55 -08002561 reg->obj = obj;
2562
Chris Wilsone259bef2010-09-17 00:32:02 +01002563 switch (INTEL_INFO(dev)->gen) {
2564 case 6:
Chris Wilsona00b10c2010-09-24 21:15:47 +01002565 sandybridge_write_fence_reg(obj);
Chris Wilsone259bef2010-09-17 00:32:02 +01002566 break;
2567 case 5:
2568 case 4:
Chris Wilsona00b10c2010-09-24 21:15:47 +01002569 i965_write_fence_reg(obj);
Chris Wilsone259bef2010-09-17 00:32:02 +01002570 break;
2571 case 3:
Chris Wilsona00b10c2010-09-24 21:15:47 +01002572 i915_write_fence_reg(obj);
Chris Wilsone259bef2010-09-17 00:32:02 +01002573 break;
2574 case 2:
Chris Wilsona00b10c2010-09-24 21:15:47 +01002575 i830_write_fence_reg(obj);
Chris Wilsone259bef2010-09-17 00:32:02 +01002576 break;
2577 }
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002578
Chris Wilsona00b10c2010-09-24 21:15:47 +01002579 trace_i915_gem_object_get_fence(obj,
2580 obj_priv->fence_reg,
2581 obj_priv->tiling_mode);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002582
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002583 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002584}
2585
2586/**
2587 * i915_gem_clear_fence_reg - clear out fence register info
2588 * @obj: object to clear
2589 *
2590 * Zeroes out the fence register itself and clears out the associated
2591 * data structures in dev_priv and obj_priv.
2592 */
2593static void
2594i915_gem_clear_fence_reg(struct drm_gem_object *obj)
2595{
2596 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002597 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002598 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002599 struct drm_i915_fence_reg *reg =
2600 &dev_priv->fence_regs[obj_priv->fence_reg];
Chris Wilsone259bef2010-09-17 00:32:02 +01002601 uint32_t fence_reg;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002602
Chris Wilsone259bef2010-09-17 00:32:02 +01002603 switch (INTEL_INFO(dev)->gen) {
2604 case 6:
Eric Anholt4e901fd2009-10-26 16:44:17 -07002605 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 +
2606 (obj_priv->fence_reg * 8), 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002607 break;
2608 case 5:
2609 case 4:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002610 I915_WRITE64(FENCE_REG_965_0 + (obj_priv->fence_reg * 8), 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002611 break;
2612 case 3:
Chris Wilson9b74f732010-09-22 19:10:44 +01002613 if (obj_priv->fence_reg >= 8)
Chris Wilsone259bef2010-09-17 00:32:02 +01002614 fence_reg = FENCE_REG_945_8 + (obj_priv->fence_reg - 8) * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002615 else
Chris Wilsone259bef2010-09-17 00:32:02 +01002616 case 2:
2617 fence_reg = FENCE_REG_830_0 + obj_priv->fence_reg * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002618
2619 I915_WRITE(fence_reg, 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002620 break;
Eric Anholtdc529a42009-03-10 22:34:49 -07002621 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002622
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002623 reg->obj = NULL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002624 obj_priv->fence_reg = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002625 list_del_init(&reg->lru_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002626}
2627
Eric Anholt673a3942008-07-30 12:06:12 -07002628/**
Chris Wilson52dc7d32009-06-06 09:46:01 +01002629 * i915_gem_object_put_fence_reg - waits on outstanding fenced access
2630 * to the buffer to finish, and then resets the fence register.
2631 * @obj: tiled object holding a fence register.
Chris Wilson2cf34d72010-09-14 13:03:28 +01002632 * @bool: whether the wait upon the fence is interruptible
Chris Wilson52dc7d32009-06-06 09:46:01 +01002633 *
2634 * Zeroes out the fence register itself and clears out the associated
2635 * data structures in dev_priv and obj_priv.
2636 */
2637int
Chris Wilson2cf34d72010-09-14 13:03:28 +01002638i915_gem_object_put_fence_reg(struct drm_gem_object *obj,
2639 bool interruptible)
Chris Wilson52dc7d32009-06-06 09:46:01 +01002640{
2641 struct drm_device *dev = obj->dev;
Chris Wilson53640e12010-09-20 11:40:50 +01002642 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002643 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson53640e12010-09-20 11:40:50 +01002644 struct drm_i915_fence_reg *reg;
Chris Wilson52dc7d32009-06-06 09:46:01 +01002645
2646 if (obj_priv->fence_reg == I915_FENCE_REG_NONE)
2647 return 0;
2648
Daniel Vetter10ae9bd2010-02-01 13:59:17 +01002649 /* If we've changed tiling, GTT-mappings of the object
2650 * need to re-fault to ensure that the correct fence register
2651 * setup is in place.
2652 */
2653 i915_gem_release_mmap(obj);
2654
Chris Wilson52dc7d32009-06-06 09:46:01 +01002655 /* On the i915, GPU access to tiled buffers is via a fence,
2656 * therefore we must wait for any outstanding access to complete
2657 * before clearing the fence.
2658 */
Chris Wilson53640e12010-09-20 11:40:50 +01002659 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
2660 if (reg->gpu) {
Chris Wilson52dc7d32009-06-06 09:46:01 +01002661 int ret;
2662
Chris Wilson2cf34d72010-09-14 13:03:28 +01002663 ret = i915_gem_object_flush_gpu_write_domain(obj, true);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002664 if (ret)
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002665 return ret;
2666
Chris Wilson2cf34d72010-09-14 13:03:28 +01002667 ret = i915_gem_object_wait_rendering(obj, interruptible);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002668 if (ret)
Chris Wilson52dc7d32009-06-06 09:46:01 +01002669 return ret;
Chris Wilson53640e12010-09-20 11:40:50 +01002670
2671 reg->gpu = false;
Chris Wilson52dc7d32009-06-06 09:46:01 +01002672 }
2673
Daniel Vetter4a726612010-02-01 13:59:16 +01002674 i915_gem_object_flush_gtt_write_domain(obj);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002675 i915_gem_clear_fence_reg(obj);
Chris Wilson52dc7d32009-06-06 09:46:01 +01002676
2677 return 0;
2678}
2679
2680/**
Eric Anholt673a3942008-07-30 12:06:12 -07002681 * Finds free space in the GTT aperture and binds the object there.
2682 */
2683static int
Daniel Vetter920afa72010-09-16 17:54:23 +02002684i915_gem_object_bind_to_gtt(struct drm_gem_object *obj,
2685 unsigned alignment,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002686 bool mappable,
2687 bool need_fence)
Eric Anholt673a3942008-07-30 12:06:12 -07002688{
2689 struct drm_device *dev = obj->dev;
2690 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002691 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002692 struct drm_mm_node *free_space;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002693 gfp_t gfpmask = __GFP_NORETRY | __GFP_NOWARN;
2694 u32 size, fence_size, fence_alignment;
Chris Wilson07f73f62009-09-14 16:50:30 +01002695 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002696
Chris Wilsonbb6baf72009-09-22 14:24:13 +01002697 if (obj_priv->madv != I915_MADV_WILLNEED) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01002698 DRM_ERROR("Attempting to bind a purgeable object\n");
2699 return -EINVAL;
2700 }
2701
Chris Wilsona00b10c2010-09-24 21:15:47 +01002702 fence_size = i915_gem_get_gtt_size(obj_priv);
2703 fence_alignment = i915_gem_get_gtt_alignment(obj_priv);
2704
Eric Anholt673a3942008-07-30 12:06:12 -07002705 if (alignment == 0)
Chris Wilsona00b10c2010-09-24 21:15:47 +01002706 alignment = need_fence ? fence_alignment : 4096;
2707 if (need_fence && alignment & (fence_alignment - 1)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002708 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2709 return -EINVAL;
2710 }
2711
Chris Wilsona00b10c2010-09-24 21:15:47 +01002712 size = need_fence ? fence_size : obj->size;
2713
Chris Wilson654fc602010-05-27 13:18:21 +01002714 /* If the object is bigger than the entire aperture, reject it early
2715 * before evicting everything in a vain attempt to find space.
2716 */
Daniel Vetter920afa72010-09-16 17:54:23 +02002717 if (obj->size >
2718 (mappable ? dev_priv->mm.gtt_mappable_end : dev_priv->mm.gtt_total)) {
Chris Wilson654fc602010-05-27 13:18:21 +01002719 DRM_ERROR("Attempting to bind an object larger than the aperture\n");
2720 return -E2BIG;
2721 }
2722
Eric Anholt673a3942008-07-30 12:06:12 -07002723 search_free:
Daniel Vetter920afa72010-09-16 17:54:23 +02002724 if (mappable)
2725 free_space =
2726 drm_mm_search_free_in_range(&dev_priv->mm.gtt_space,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002727 size, alignment, 0,
Daniel Vetter920afa72010-09-16 17:54:23 +02002728 dev_priv->mm.gtt_mappable_end,
2729 0);
2730 else
2731 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002732 size, alignment, 0);
Daniel Vetter920afa72010-09-16 17:54:23 +02002733
2734 if (free_space != NULL) {
2735 if (mappable)
2736 obj_priv->gtt_space =
2737 drm_mm_get_block_range_generic(free_space,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002738 size, alignment, 0,
Daniel Vetter920afa72010-09-16 17:54:23 +02002739 dev_priv->mm.gtt_mappable_end,
2740 0);
2741 else
2742 obj_priv->gtt_space =
Chris Wilsona00b10c2010-09-24 21:15:47 +01002743 drm_mm_get_block(free_space, size, alignment);
Daniel Vetter920afa72010-09-16 17:54:23 +02002744 }
Eric Anholt673a3942008-07-30 12:06:12 -07002745 if (obj_priv->gtt_space == NULL) {
2746 /* If the gtt is empty and we're still having trouble
2747 * fitting our object in, we're out of memory.
2748 */
Chris Wilsona00b10c2010-09-24 21:15:47 +01002749 ret = i915_gem_evict_something(dev, size, alignment, mappable);
Chris Wilson97311292009-09-21 00:22:34 +01002750 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002751 return ret;
Chris Wilson97311292009-09-21 00:22:34 +01002752
Eric Anholt673a3942008-07-30 12:06:12 -07002753 goto search_free;
2754 }
2755
Chris Wilsone5281cc2010-10-28 13:45:36 +01002756 ret = i915_gem_object_get_pages_gtt(obj, gfpmask);
Eric Anholt673a3942008-07-30 12:06:12 -07002757 if (ret) {
2758 drm_mm_put_block(obj_priv->gtt_space);
2759 obj_priv->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002760
2761 if (ret == -ENOMEM) {
2762 /* first try to clear up some space from the GTT */
Chris Wilsona00b10c2010-09-24 21:15:47 +01002763 ret = i915_gem_evict_something(dev, size,
Daniel Vetter920afa72010-09-16 17:54:23 +02002764 alignment, mappable);
Chris Wilson07f73f62009-09-14 16:50:30 +01002765 if (ret) {
Chris Wilson07f73f62009-09-14 16:50:30 +01002766 /* now try to shrink everyone else */
Chris Wilson4bdadb92010-01-27 13:36:32 +00002767 if (gfpmask) {
2768 gfpmask = 0;
2769 goto search_free;
Chris Wilson07f73f62009-09-14 16:50:30 +01002770 }
2771
2772 return ret;
2773 }
2774
2775 goto search_free;
2776 }
2777
Eric Anholt673a3942008-07-30 12:06:12 -07002778 return ret;
2779 }
2780
Eric Anholt673a3942008-07-30 12:06:12 -07002781 /* Create an AGP memory structure pointing at our pages, and bind it
2782 * into the GTT.
2783 */
2784 obj_priv->agp_mem = drm_agp_bind_pages(dev,
Eric Anholt856fa192009-03-19 14:10:50 -07002785 obj_priv->pages,
Chris Wilson07f73f62009-09-14 16:50:30 +01002786 obj->size >> PAGE_SHIFT,
Chris Wilson9af90d12010-10-17 10:01:56 +01002787 obj_priv->gtt_space->start,
Keith Packardba1eb1d2008-10-14 19:55:10 -07002788 obj_priv->agp_type);
Eric Anholt673a3942008-07-30 12:06:12 -07002789 if (obj_priv->agp_mem == NULL) {
Chris Wilsone5281cc2010-10-28 13:45:36 +01002790 i915_gem_object_put_pages_gtt(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002791 drm_mm_put_block(obj_priv->gtt_space);
2792 obj_priv->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002793
Chris Wilsona00b10c2010-09-24 21:15:47 +01002794 ret = i915_gem_evict_something(dev, size,
2795 alignment, mappable);
Chris Wilson97311292009-09-21 00:22:34 +01002796 if (ret)
Chris Wilson07f73f62009-09-14 16:50:30 +01002797 return ret;
Chris Wilson07f73f62009-09-14 16:50:30 +01002798
2799 goto search_free;
Eric Anholt673a3942008-07-30 12:06:12 -07002800 }
Eric Anholt673a3942008-07-30 12:06:12 -07002801
Daniel Vetterfb7d5162010-10-01 22:05:20 +02002802 obj_priv->gtt_offset = obj_priv->gtt_space->start;
2803
Chris Wilsonbf1a1092010-08-07 11:01:20 +01002804 /* keep track of bounds object by adding it to the inactive list */
Chris Wilson69dc4982010-10-19 10:36:51 +01002805 list_add_tail(&obj_priv->mm_list, &dev_priv->mm.inactive_list);
Chris Wilsona00b10c2010-09-24 21:15:47 +01002806 i915_gem_info_add_gtt(dev_priv, obj_priv);
Chris Wilsonbf1a1092010-08-07 11:01:20 +01002807
Eric Anholt673a3942008-07-30 12:06:12 -07002808 /* Assert that the object is not currently in any GPU domain. As it
2809 * wasn't in the GTT, there shouldn't be any way it could have been in
2810 * a GPU cache
2811 */
Chris Wilson21d509e2009-06-06 09:46:02 +01002812 BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS);
2813 BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS);
Eric Anholt673a3942008-07-30 12:06:12 -07002814
Daniel Vetterec57d262010-09-30 23:42:15 +02002815 trace_i915_gem_object_bind(obj, obj_priv->gtt_offset, mappable);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002816
Chris Wilsona00b10c2010-09-24 21:15:47 +01002817 obj_priv->fenceable =
2818 obj_priv->gtt_space->size == fence_size &&
2819 (obj_priv->gtt_space->start & (fence_alignment -1)) == 0;
2820
2821 obj_priv->mappable =
2822 obj_priv->gtt_offset + obj->size <= dev_priv->mm.gtt_mappable_end;
2823
Eric Anholt673a3942008-07-30 12:06:12 -07002824 return 0;
2825}
2826
2827void
2828i915_gem_clflush_object(struct drm_gem_object *obj)
2829{
Daniel Vetter23010e42010-03-08 13:35:02 +01002830 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002831
2832 /* If we don't have a page list set up, then we're not pinned
2833 * to GPU, and we can ignore the cache flush because it'll happen
2834 * again at bind time.
2835 */
Eric Anholt856fa192009-03-19 14:10:50 -07002836 if (obj_priv->pages == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002837 return;
2838
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002839 trace_i915_gem_object_clflush(obj);
Eric Anholtcfa16a02009-05-26 18:46:16 -07002840
Eric Anholt856fa192009-03-19 14:10:50 -07002841 drm_clflush_pages(obj_priv->pages, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002842}
2843
Eric Anholte47c68e2008-11-14 13:35:19 -08002844/** Flushes any GPU write domain for the object if it's dirty. */
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002845static int
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002846i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj,
2847 bool pipelined)
Eric Anholte47c68e2008-11-14 13:35:19 -08002848{
2849 struct drm_device *dev = obj->dev;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002850 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002851
2852 if ((obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002853 return 0;
Eric Anholte47c68e2008-11-14 13:35:19 -08002854
2855 /* Queue the GPU write cache flushing we need. */
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002856 old_write_domain = obj->write_domain;
Chris Wilsonc78ec302010-09-20 12:50:23 +01002857 i915_gem_flush_ring(dev, NULL,
Chris Wilson92204342010-09-18 11:02:01 +01002858 to_intel_bo(obj)->ring,
2859 0, obj->write_domain);
Chris Wilson48b956c2010-09-14 12:50:34 +01002860 BUG_ON(obj->write_domain);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002861
2862 trace_i915_gem_object_change_domain(obj,
2863 obj->read_domains,
2864 old_write_domain);
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002865
2866 if (pipelined)
2867 return 0;
2868
Chris Wilson2cf34d72010-09-14 13:03:28 +01002869 return i915_gem_object_wait_rendering(obj, true);
Eric Anholte47c68e2008-11-14 13:35:19 -08002870}
2871
2872/** Flushes the GTT write domain for the object if it's dirty. */
2873static void
2874i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj)
2875{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002876 uint32_t old_write_domain;
2877
Eric Anholte47c68e2008-11-14 13:35:19 -08002878 if (obj->write_domain != I915_GEM_DOMAIN_GTT)
2879 return;
2880
2881 /* No actual flushing is required for the GTT write domain. Writes
2882 * to it immediately go to main memory as far as we know, so there's
2883 * no chipset flush. It also doesn't land in render cache.
2884 */
Chris Wilson4a684a42010-10-28 14:44:08 +01002885 i915_gem_release_mmap(obj);
2886
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002887 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002888 obj->write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002889
2890 trace_i915_gem_object_change_domain(obj,
2891 obj->read_domains,
2892 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002893}
2894
2895/** Flushes the CPU write domain for the object if it's dirty. */
2896static void
2897i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj)
2898{
2899 struct drm_device *dev = obj->dev;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002900 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002901
2902 if (obj->write_domain != I915_GEM_DOMAIN_CPU)
2903 return;
2904
2905 i915_gem_clflush_object(obj);
2906 drm_agp_chipset_flush(dev);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002907 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002908 obj->write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002909
2910 trace_i915_gem_object_change_domain(obj,
2911 obj->read_domains,
2912 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002913}
2914
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002915/**
2916 * Moves a single object to the GTT read, and possibly write domain.
2917 *
2918 * This function returns when the move is complete, including waiting on
2919 * flushes to occur.
2920 */
Jesse Barnes79e53942008-11-07 14:24:08 -08002921int
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002922i915_gem_object_set_to_gtt_domain(struct drm_gem_object *obj, int write)
2923{
Daniel Vetter23010e42010-03-08 13:35:02 +01002924 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002925 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002926 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002927
Eric Anholt02354392008-11-26 13:58:13 -08002928 /* Not valid to be called on unbound objects. */
2929 if (obj_priv->gtt_space == NULL)
2930 return -EINVAL;
2931
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002932 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002933 if (ret != 0)
2934 return ret;
2935
Chris Wilson72133422010-09-13 23:56:38 +01002936 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002937
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002938 if (write) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01002939 ret = i915_gem_object_wait_rendering(obj, true);
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002940 if (ret)
2941 return ret;
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002942 }
Eric Anholte47c68e2008-11-14 13:35:19 -08002943
2944 old_write_domain = obj->write_domain;
2945 old_read_domains = obj->read_domains;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002946
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002947 /* It should now be out of any other write domains, and we can update
2948 * the domain values for our changes.
2949 */
2950 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2951 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002952 if (write) {
Chris Wilson72133422010-09-13 23:56:38 +01002953 obj->read_domains = I915_GEM_DOMAIN_GTT;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002954 obj->write_domain = I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002955 obj_priv->dirty = 1;
2956 }
2957
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002958 trace_i915_gem_object_change_domain(obj,
2959 old_read_domains,
2960 old_write_domain);
2961
Eric Anholte47c68e2008-11-14 13:35:19 -08002962 return 0;
2963}
2964
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002965/*
2966 * Prepare buffer for display plane. Use uninterruptible for possible flush
2967 * wait, as in modesetting process we're not supposed to be interrupted.
2968 */
2969int
Chris Wilson48b956c2010-09-14 12:50:34 +01002970i915_gem_object_set_to_display_plane(struct drm_gem_object *obj,
2971 bool pipelined)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002972{
Daniel Vetter23010e42010-03-08 13:35:02 +01002973 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002974 uint32_t old_read_domains;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002975 int ret;
2976
2977 /* Not valid to be called on unbound objects. */
2978 if (obj_priv->gtt_space == NULL)
2979 return -EINVAL;
2980
Chris Wilsonced270f2010-09-26 22:47:46 +01002981 ret = i915_gem_object_flush_gpu_write_domain(obj, true);
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002982 if (ret)
2983 return ret;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002984
Chris Wilsonced270f2010-09-26 22:47:46 +01002985 /* Currently, we are always called from an non-interruptible context. */
2986 if (!pipelined) {
2987 ret = i915_gem_object_wait_rendering(obj, false);
2988 if (ret)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002989 return ret;
2990 }
2991
Chris Wilsonb118c1e2010-05-27 13:18:14 +01002992 i915_gem_object_flush_cpu_write_domain(obj);
2993
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002994 old_read_domains = obj->read_domains;
Chris Wilsonc78ec302010-09-20 12:50:23 +01002995 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002996
2997 trace_i915_gem_object_change_domain(obj,
2998 old_read_domains,
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002999 obj->write_domain);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003000
3001 return 0;
3002}
3003
Eric Anholte47c68e2008-11-14 13:35:19 -08003004/**
3005 * Moves a single object to the CPU read, and possibly write domain.
3006 *
3007 * This function returns when the move is complete, including waiting on
3008 * flushes to occur.
3009 */
3010static int
3011i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj, int write)
3012{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003013 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003014 int ret;
3015
Daniel Vetterba3d8d72010-02-11 22:37:04 +01003016 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Eric Anholte47c68e2008-11-14 13:35:19 -08003017 if (ret != 0)
3018 return ret;
3019
3020 i915_gem_object_flush_gtt_write_domain(obj);
3021
3022 /* If we have a partially-valid cache of the object in the CPU,
3023 * finish invalidating it and free the per-page flags.
3024 */
3025 i915_gem_object_set_to_full_cpu_read_domain(obj);
3026
Chris Wilson72133422010-09-13 23:56:38 +01003027 if (write) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01003028 ret = i915_gem_object_wait_rendering(obj, true);
Chris Wilson72133422010-09-13 23:56:38 +01003029 if (ret)
3030 return ret;
3031 }
3032
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003033 old_write_domain = obj->write_domain;
3034 old_read_domains = obj->read_domains;
3035
Eric Anholte47c68e2008-11-14 13:35:19 -08003036 /* Flush the CPU cache if it's still invalid. */
3037 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
3038 i915_gem_clflush_object(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003039
3040 obj->read_domains |= I915_GEM_DOMAIN_CPU;
3041 }
3042
3043 /* It should now be out of any other write domains, and we can update
3044 * the domain values for our changes.
3045 */
3046 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
3047
3048 /* If we're writing through the CPU, then the GPU read domains will
3049 * need to be invalidated at next use.
3050 */
3051 if (write) {
Chris Wilsonc78ec302010-09-20 12:50:23 +01003052 obj->read_domains = I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003053 obj->write_domain = I915_GEM_DOMAIN_CPU;
3054 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003055
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003056 trace_i915_gem_object_change_domain(obj,
3057 old_read_domains,
3058 old_write_domain);
3059
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003060 return 0;
3061}
3062
Eric Anholt673a3942008-07-30 12:06:12 -07003063/*
3064 * Set the next domain for the specified object. This
3065 * may not actually perform the necessary flushing/invaliding though,
3066 * as that may want to be batched with other set_domain operations
3067 *
3068 * This is (we hope) the only really tricky part of gem. The goal
3069 * is fairly simple -- track which caches hold bits of the object
3070 * and make sure they remain coherent. A few concrete examples may
3071 * help to explain how it works. For shorthand, we use the notation
3072 * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
3073 * a pair of read and write domain masks.
3074 *
3075 * Case 1: the batch buffer
3076 *
3077 * 1. Allocated
3078 * 2. Written by CPU
3079 * 3. Mapped to GTT
3080 * 4. Read by GPU
3081 * 5. Unmapped from GTT
3082 * 6. Freed
3083 *
3084 * Let's take these a step at a time
3085 *
3086 * 1. Allocated
3087 * Pages allocated from the kernel may still have
3088 * cache contents, so we set them to (CPU, CPU) always.
3089 * 2. Written by CPU (using pwrite)
3090 * The pwrite function calls set_domain (CPU, CPU) and
3091 * this function does nothing (as nothing changes)
3092 * 3. Mapped by GTT
3093 * This function asserts that the object is not
3094 * currently in any GPU-based read or write domains
3095 * 4. Read by GPU
3096 * i915_gem_execbuffer calls set_domain (COMMAND, 0).
3097 * As write_domain is zero, this function adds in the
3098 * current read domains (CPU+COMMAND, 0).
3099 * flush_domains is set to CPU.
3100 * invalidate_domains is set to COMMAND
3101 * clflush is run to get data out of the CPU caches
3102 * then i915_dev_set_domain calls i915_gem_flush to
3103 * emit an MI_FLUSH and drm_agp_chipset_flush
3104 * 5. Unmapped from GTT
3105 * i915_gem_object_unbind calls set_domain (CPU, CPU)
3106 * flush_domains and invalidate_domains end up both zero
3107 * so no flushing/invalidating happens
3108 * 6. Freed
3109 * yay, done
3110 *
3111 * Case 2: The shared render buffer
3112 *
3113 * 1. Allocated
3114 * 2. Mapped to GTT
3115 * 3. Read/written by GPU
3116 * 4. set_domain to (CPU,CPU)
3117 * 5. Read/written by CPU
3118 * 6. Read/written by GPU
3119 *
3120 * 1. Allocated
3121 * Same as last example, (CPU, CPU)
3122 * 2. Mapped to GTT
3123 * Nothing changes (assertions find that it is not in the GPU)
3124 * 3. Read/written by GPU
3125 * execbuffer calls set_domain (RENDER, RENDER)
3126 * flush_domains gets CPU
3127 * invalidate_domains gets GPU
3128 * clflush (obj)
3129 * MI_FLUSH and drm_agp_chipset_flush
3130 * 4. set_domain (CPU, CPU)
3131 * flush_domains gets GPU
3132 * invalidate_domains gets CPU
3133 * wait_rendering (obj) to make sure all drawing is complete.
3134 * This will include an MI_FLUSH to get the data from GPU
3135 * to memory
3136 * clflush (obj) to invalidate the CPU cache
3137 * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
3138 * 5. Read/written by CPU
3139 * cache lines are loaded and dirtied
3140 * 6. Read written by GPU
3141 * Same as last GPU access
3142 *
3143 * Case 3: The constant buffer
3144 *
3145 * 1. Allocated
3146 * 2. Written by CPU
3147 * 3. Read by GPU
3148 * 4. Updated (written) by CPU again
3149 * 5. Read by GPU
3150 *
3151 * 1. Allocated
3152 * (CPU, CPU)
3153 * 2. Written by CPU
3154 * (CPU, CPU)
3155 * 3. Read by GPU
3156 * (CPU+RENDER, 0)
3157 * flush_domains = CPU
3158 * invalidate_domains = RENDER
3159 * clflush (obj)
3160 * MI_FLUSH
3161 * drm_agp_chipset_flush
3162 * 4. Updated (written) by CPU again
3163 * (CPU, CPU)
3164 * flush_domains = 0 (no previous write domain)
3165 * invalidate_domains = 0 (no new read domains)
3166 * 5. Read by GPU
3167 * (CPU+RENDER, 0)
3168 * flush_domains = CPU
3169 * invalidate_domains = RENDER
3170 * clflush (obj)
3171 * MI_FLUSH
3172 * drm_agp_chipset_flush
3173 */
Keith Packardc0d90822008-11-20 23:11:08 -08003174static void
Chris Wilsonb6651452010-10-23 10:15:06 +01003175i915_gem_object_set_to_gpu_domain(struct drm_gem_object *obj,
3176 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07003177{
3178 struct drm_device *dev = obj->dev;
Chris Wilson92204342010-09-18 11:02:01 +01003179 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01003180 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003181 uint32_t invalidate_domains = 0;
3182 uint32_t flush_domains = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07003183
Eric Anholt673a3942008-07-30 12:06:12 -07003184 /*
3185 * If the object isn't moving to a new write domain,
3186 * let the object stay in multiple read domains
3187 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003188 if (obj->pending_write_domain == 0)
3189 obj->pending_read_domains |= obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07003190
3191 /*
3192 * Flush the current write domain if
3193 * the new read domains don't match. Invalidate
3194 * any read domains which differ from the old
3195 * write domain
3196 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003197 if (obj->write_domain &&
3198 obj->write_domain != obj->pending_read_domains) {
Eric Anholt673a3942008-07-30 12:06:12 -07003199 flush_domains |= obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08003200 invalidate_domains |=
3201 obj->pending_read_domains & ~obj->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07003202 }
3203 /*
3204 * Invalidate any read caches which may have
3205 * stale data. That is, any new read domains.
3206 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003207 invalidate_domains |= obj->pending_read_domains & ~obj->read_domains;
Chris Wilson3d2a8122010-09-29 11:39:53 +01003208 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU)
Eric Anholt673a3942008-07-30 12:06:12 -07003209 i915_gem_clflush_object(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003210
Chris Wilson4a684a42010-10-28 14:44:08 +01003211 /* blow away mappings if mapped through GTT */
3212 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_GTT)
3213 i915_gem_release_mmap(obj);
3214
Eric Anholtefbeed92009-02-19 14:54:51 -08003215 /* The actual obj->write_domain will be updated with
3216 * pending_write_domain after we emit the accumulated flush for all
3217 * of our domain changes in execbuffers (which clears objects'
3218 * write_domains). So if we have a current write domain that we
3219 * aren't changing, set pending_write_domain to that.
3220 */
3221 if (flush_domains == 0 && obj->pending_write_domain == 0)
3222 obj->pending_write_domain = obj->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07003223
3224 dev->invalidate_domains |= invalidate_domains;
3225 dev->flush_domains |= flush_domains;
Chris Wilsonb6651452010-10-23 10:15:06 +01003226 if (flush_domains & I915_GEM_GPU_DOMAINS)
Chris Wilson92204342010-09-18 11:02:01 +01003227 dev_priv->mm.flush_rings |= obj_priv->ring->id;
Chris Wilsonb6651452010-10-23 10:15:06 +01003228 if (invalidate_domains & I915_GEM_GPU_DOMAINS)
3229 dev_priv->mm.flush_rings |= ring->id;
Eric Anholt673a3942008-07-30 12:06:12 -07003230}
3231
3232/**
Eric Anholte47c68e2008-11-14 13:35:19 -08003233 * Moves the object from a partially CPU read to a full one.
Eric Anholt673a3942008-07-30 12:06:12 -07003234 *
Eric Anholte47c68e2008-11-14 13:35:19 -08003235 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
3236 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
3237 */
3238static void
3239i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj)
3240{
Daniel Vetter23010e42010-03-08 13:35:02 +01003241 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003242
3243 if (!obj_priv->page_cpu_valid)
3244 return;
3245
3246 /* If we're partially in the CPU read domain, finish moving it in.
3247 */
3248 if (obj->read_domains & I915_GEM_DOMAIN_CPU) {
3249 int i;
3250
3251 for (i = 0; i <= (obj->size - 1) / PAGE_SIZE; i++) {
3252 if (obj_priv->page_cpu_valid[i])
3253 continue;
Eric Anholt856fa192009-03-19 14:10:50 -07003254 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholte47c68e2008-11-14 13:35:19 -08003255 }
Eric Anholte47c68e2008-11-14 13:35:19 -08003256 }
3257
3258 /* Free the page_cpu_valid mappings which are now stale, whether
3259 * or not we've got I915_GEM_DOMAIN_CPU.
3260 */
Eric Anholt9a298b22009-03-24 12:23:04 -07003261 kfree(obj_priv->page_cpu_valid);
Eric Anholte47c68e2008-11-14 13:35:19 -08003262 obj_priv->page_cpu_valid = NULL;
3263}
3264
3265/**
3266 * Set the CPU read domain on a range of the object.
3267 *
3268 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
3269 * not entirely valid. The page_cpu_valid member of the object flags which
3270 * pages have been flushed, and will be respected by
3271 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
3272 * of the whole object.
3273 *
3274 * This function returns when the move is complete, including waiting on
3275 * flushes to occur.
Eric Anholt673a3942008-07-30 12:06:12 -07003276 */
3277static int
Eric Anholte47c68e2008-11-14 13:35:19 -08003278i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
3279 uint64_t offset, uint64_t size)
Eric Anholt673a3942008-07-30 12:06:12 -07003280{
Daniel Vetter23010e42010-03-08 13:35:02 +01003281 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003282 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003283 int i, ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003284
Eric Anholte47c68e2008-11-14 13:35:19 -08003285 if (offset == 0 && size == obj->size)
3286 return i915_gem_object_set_to_cpu_domain(obj, 0);
3287
Daniel Vetterba3d8d72010-02-11 22:37:04 +01003288 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Eric Anholte47c68e2008-11-14 13:35:19 -08003289 if (ret != 0)
3290 return ret;
3291 i915_gem_object_flush_gtt_write_domain(obj);
3292
3293 /* If we're already fully in the CPU read domain, we're done. */
3294 if (obj_priv->page_cpu_valid == NULL &&
3295 (obj->read_domains & I915_GEM_DOMAIN_CPU) != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07003296 return 0;
3297
Eric Anholte47c68e2008-11-14 13:35:19 -08003298 /* Otherwise, create/clear the per-page CPU read domain flag if we're
3299 * newly adding I915_GEM_DOMAIN_CPU
3300 */
Eric Anholt673a3942008-07-30 12:06:12 -07003301 if (obj_priv->page_cpu_valid == NULL) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003302 obj_priv->page_cpu_valid = kzalloc(obj->size / PAGE_SIZE,
3303 GFP_KERNEL);
Eric Anholte47c68e2008-11-14 13:35:19 -08003304 if (obj_priv->page_cpu_valid == NULL)
3305 return -ENOMEM;
3306 } else if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0)
3307 memset(obj_priv->page_cpu_valid, 0, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07003308
3309 /* Flush the cache on any pages that are still invalid from the CPU's
3310 * perspective.
3311 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003312 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
3313 i++) {
Eric Anholt673a3942008-07-30 12:06:12 -07003314 if (obj_priv->page_cpu_valid[i])
3315 continue;
3316
Eric Anholt856fa192009-03-19 14:10:50 -07003317 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07003318
3319 obj_priv->page_cpu_valid[i] = 1;
3320 }
3321
Eric Anholte47c68e2008-11-14 13:35:19 -08003322 /* It should now be out of any other write domains, and we can update
3323 * the domain values for our changes.
3324 */
3325 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
3326
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003327 old_read_domains = obj->read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003328 obj->read_domains |= I915_GEM_DOMAIN_CPU;
3329
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003330 trace_i915_gem_object_change_domain(obj,
3331 old_read_domains,
3332 obj->write_domain);
3333
Eric Anholt673a3942008-07-30 12:06:12 -07003334 return 0;
3335}
3336
3337/**
Eric Anholt673a3942008-07-30 12:06:12 -07003338 * Pin an object to the GTT and evaluate the relocations landing in it.
3339 */
3340static int
Chris Wilson9af90d12010-10-17 10:01:56 +01003341i915_gem_execbuffer_relocate(struct drm_i915_gem_object *obj,
3342 struct drm_file *file_priv,
3343 struct drm_i915_gem_exec_object2 *entry)
Eric Anholt673a3942008-07-30 12:06:12 -07003344{
Chris Wilson9af90d12010-10-17 10:01:56 +01003345 struct drm_device *dev = obj->base.dev;
Keith Packard0839ccb2008-10-30 19:38:48 -07003346 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson2549d6c2010-10-14 12:10:41 +01003347 struct drm_i915_gem_relocation_entry __user *user_relocs;
Chris Wilson9af90d12010-10-17 10:01:56 +01003348 struct drm_gem_object *target_obj = NULL;
3349 uint32_t target_handle = 0;
3350 int i, ret = 0;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003351
Chris Wilson2549d6c2010-10-14 12:10:41 +01003352 user_relocs = (void __user *)(uintptr_t)entry->relocs_ptr;
Eric Anholt673a3942008-07-30 12:06:12 -07003353 for (i = 0; i < entry->relocation_count; i++) {
Chris Wilson2549d6c2010-10-14 12:10:41 +01003354 struct drm_i915_gem_relocation_entry reloc;
Chris Wilson9af90d12010-10-17 10:01:56 +01003355 uint32_t target_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003356
Chris Wilson9af90d12010-10-17 10:01:56 +01003357 if (__copy_from_user_inatomic(&reloc,
3358 user_relocs+i,
3359 sizeof(reloc))) {
3360 ret = -EFAULT;
3361 break;
Eric Anholt673a3942008-07-30 12:06:12 -07003362 }
Chris Wilson2549d6c2010-10-14 12:10:41 +01003363
Chris Wilson9af90d12010-10-17 10:01:56 +01003364 if (reloc.target_handle != target_handle) {
3365 drm_gem_object_unreference(target_obj);
3366
3367 target_obj = drm_gem_object_lookup(dev, file_priv,
3368 reloc.target_handle);
3369 if (target_obj == NULL) {
3370 ret = -ENOENT;
3371 break;
3372 }
3373
3374 target_handle = reloc.target_handle;
Eric Anholt673a3942008-07-30 12:06:12 -07003375 }
Chris Wilson9af90d12010-10-17 10:01:56 +01003376 target_offset = to_intel_bo(target_obj)->gtt_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003377
Chris Wilson8542a0b2009-09-09 21:15:15 +01003378#if WATCH_RELOC
3379 DRM_INFO("%s: obj %p offset %08x target %d "
3380 "read %08x write %08x gtt %08x "
3381 "presumed %08x delta %08x\n",
3382 __func__,
3383 obj,
Chris Wilson2549d6c2010-10-14 12:10:41 +01003384 (int) reloc.offset,
3385 (int) reloc.target_handle,
3386 (int) reloc.read_domains,
3387 (int) reloc.write_domain,
Chris Wilson9af90d12010-10-17 10:01:56 +01003388 (int) target_offset,
Chris Wilson2549d6c2010-10-14 12:10:41 +01003389 (int) reloc.presumed_offset,
3390 reloc.delta);
Chris Wilson8542a0b2009-09-09 21:15:15 +01003391#endif
3392
Eric Anholt673a3942008-07-30 12:06:12 -07003393 /* The target buffer should have appeared before us in the
3394 * exec_object list, so it should have a GTT space bound by now.
3395 */
Chris Wilson9af90d12010-10-17 10:01:56 +01003396 if (target_offset == 0) {
Eric Anholt673a3942008-07-30 12:06:12 -07003397 DRM_ERROR("No GTT space found for object %d\n",
Chris Wilson2549d6c2010-10-14 12:10:41 +01003398 reloc.target_handle);
Chris Wilson9af90d12010-10-17 10:01:56 +01003399 ret = -EINVAL;
3400 break;
Eric Anholt673a3942008-07-30 12:06:12 -07003401 }
3402
Chris Wilson8542a0b2009-09-09 21:15:15 +01003403 /* Validate that the target is in a valid r/w GPU domain */
Chris Wilson2549d6c2010-10-14 12:10:41 +01003404 if (reloc.write_domain & (reloc.write_domain - 1)) {
Daniel Vetter16edd552010-02-19 11:52:02 +01003405 DRM_ERROR("reloc with multiple write domains: "
3406 "obj %p target %d offset %d "
3407 "read %08x write %08x",
Chris Wilson2549d6c2010-10-14 12:10:41 +01003408 obj, reloc.target_handle,
3409 (int) reloc.offset,
3410 reloc.read_domains,
3411 reloc.write_domain);
Chris Wilson9af90d12010-10-17 10:01:56 +01003412 ret = -EINVAL;
3413 break;
Daniel Vetter16edd552010-02-19 11:52:02 +01003414 }
Chris Wilson2549d6c2010-10-14 12:10:41 +01003415 if (reloc.write_domain & I915_GEM_DOMAIN_CPU ||
3416 reloc.read_domains & I915_GEM_DOMAIN_CPU) {
Chris Wilson8542a0b2009-09-09 21:15:15 +01003417 DRM_ERROR("reloc with read/write CPU domains: "
3418 "obj %p target %d offset %d "
3419 "read %08x write %08x",
Chris Wilson2549d6c2010-10-14 12:10:41 +01003420 obj, reloc.target_handle,
3421 (int) reloc.offset,
3422 reloc.read_domains,
3423 reloc.write_domain);
Chris Wilson9af90d12010-10-17 10:01:56 +01003424 ret = -EINVAL;
3425 break;
Chris Wilson8542a0b2009-09-09 21:15:15 +01003426 }
Chris Wilson2549d6c2010-10-14 12:10:41 +01003427 if (reloc.write_domain && target_obj->pending_write_domain &&
3428 reloc.write_domain != target_obj->pending_write_domain) {
Chris Wilson8542a0b2009-09-09 21:15:15 +01003429 DRM_ERROR("Write domain conflict: "
3430 "obj %p target %d offset %d "
3431 "new %08x old %08x\n",
Chris Wilson2549d6c2010-10-14 12:10:41 +01003432 obj, reloc.target_handle,
3433 (int) reloc.offset,
3434 reloc.write_domain,
Chris Wilson8542a0b2009-09-09 21:15:15 +01003435 target_obj->pending_write_domain);
Chris Wilson9af90d12010-10-17 10:01:56 +01003436 ret = -EINVAL;
3437 break;
Chris Wilson8542a0b2009-09-09 21:15:15 +01003438 }
3439
Chris Wilson2549d6c2010-10-14 12:10:41 +01003440 target_obj->pending_read_domains |= reloc.read_domains;
Chris Wilson878a3c32010-10-22 10:48:12 +01003441 target_obj->pending_write_domain |= reloc.write_domain;
Chris Wilson8542a0b2009-09-09 21:15:15 +01003442
3443 /* If the relocation already has the right value in it, no
3444 * more work needs to be done.
3445 */
Chris Wilson9af90d12010-10-17 10:01:56 +01003446 if (target_offset == reloc.presumed_offset)
Chris Wilson8542a0b2009-09-09 21:15:15 +01003447 continue;
Chris Wilson8542a0b2009-09-09 21:15:15 +01003448
3449 /* Check that the relocation address is valid... */
Chris Wilson9af90d12010-10-17 10:01:56 +01003450 if (reloc.offset > obj->base.size - 4) {
Eric Anholt673a3942008-07-30 12:06:12 -07003451 DRM_ERROR("Relocation beyond object bounds: "
3452 "obj %p target %d offset %d size %d.\n",
Chris Wilson2549d6c2010-10-14 12:10:41 +01003453 obj, reloc.target_handle,
Chris Wilson9af90d12010-10-17 10:01:56 +01003454 (int) reloc.offset, (int) obj->base.size);
3455 ret = -EINVAL;
3456 break;
Eric Anholt673a3942008-07-30 12:06:12 -07003457 }
Chris Wilson2549d6c2010-10-14 12:10:41 +01003458 if (reloc.offset & 3) {
Eric Anholt673a3942008-07-30 12:06:12 -07003459 DRM_ERROR("Relocation not 4-byte aligned: "
3460 "obj %p target %d offset %d.\n",
Chris Wilson2549d6c2010-10-14 12:10:41 +01003461 obj, reloc.target_handle,
3462 (int) reloc.offset);
Chris Wilson9af90d12010-10-17 10:01:56 +01003463 ret = -EINVAL;
3464 break;
Eric Anholt673a3942008-07-30 12:06:12 -07003465 }
3466
Chris Wilson8542a0b2009-09-09 21:15:15 +01003467 /* and points to somewhere within the target object. */
Chris Wilson2549d6c2010-10-14 12:10:41 +01003468 if (reloc.delta >= target_obj->size) {
Chris Wilsoncd0b9fb2009-09-15 23:23:18 +01003469 DRM_ERROR("Relocation beyond target object bounds: "
3470 "obj %p target %d delta %d size %d.\n",
Chris Wilson2549d6c2010-10-14 12:10:41 +01003471 obj, reloc.target_handle,
3472 (int) reloc.delta, (int) target_obj->size);
Chris Wilson9af90d12010-10-17 10:01:56 +01003473 ret = -EINVAL;
3474 break;
Eric Anholte47c68e2008-11-14 13:35:19 -08003475 }
3476
Chris Wilson9af90d12010-10-17 10:01:56 +01003477 reloc.delta += target_offset;
3478 if (obj->base.write_domain == I915_GEM_DOMAIN_CPU) {
Chris Wilsonf0c43d92010-10-14 12:44:48 +01003479 uint32_t page_offset = reloc.offset & ~PAGE_MASK;
3480 char *vaddr;
3481
Linus Torvaldsc48c43e2010-10-26 18:57:59 -07003482 vaddr = kmap_atomic(obj->pages[reloc.offset >> PAGE_SHIFT]);
Chris Wilsonf0c43d92010-10-14 12:44:48 +01003483 *(uint32_t *)(vaddr + page_offset) = reloc.delta;
Linus Torvaldsc48c43e2010-10-26 18:57:59 -07003484 kunmap_atomic(vaddr);
Chris Wilsonf0c43d92010-10-14 12:44:48 +01003485 } else {
3486 uint32_t __iomem *reloc_entry;
3487 void __iomem *reloc_page;
Chris Wilsonf0c43d92010-10-14 12:44:48 +01003488
Chris Wilson9af90d12010-10-17 10:01:56 +01003489 ret = i915_gem_object_set_to_gtt_domain(&obj->base, 1);
3490 if (ret)
3491 break;
Chris Wilsonf0c43d92010-10-14 12:44:48 +01003492
3493 /* Map the page containing the relocation we're going to perform. */
Chris Wilson9af90d12010-10-17 10:01:56 +01003494 reloc.offset += obj->gtt_offset;
Chris Wilsonf0c43d92010-10-14 12:44:48 +01003495 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
Linus Torvaldsc48c43e2010-10-26 18:57:59 -07003496 reloc.offset & PAGE_MASK);
Chris Wilsonf0c43d92010-10-14 12:44:48 +01003497 reloc_entry = (uint32_t __iomem *)
3498 (reloc_page + (reloc.offset & ~PAGE_MASK));
3499 iowrite32(reloc.delta, reloc_entry);
Linus Torvaldsc48c43e2010-10-26 18:57:59 -07003500 io_mapping_unmap_atomic(reloc_page);
Eric Anholt673a3942008-07-30 12:06:12 -07003501 }
3502
Chris Wilsonb5dc6082010-10-20 20:59:57 +01003503 /* and update the user's relocation entry */
3504 reloc.presumed_offset = target_offset;
3505 if (__copy_to_user_inatomic(&user_relocs[i].presumed_offset,
3506 &reloc.presumed_offset,
3507 sizeof(reloc.presumed_offset))) {
3508 ret = -EFAULT;
3509 break;
3510 }
Eric Anholt673a3942008-07-30 12:06:12 -07003511 }
3512
Chris Wilson9af90d12010-10-17 10:01:56 +01003513 drm_gem_object_unreference(target_obj);
3514 return ret;
3515}
3516
3517static int
3518i915_gem_execbuffer_pin(struct drm_device *dev,
3519 struct drm_file *file,
3520 struct drm_gem_object **object_list,
3521 struct drm_i915_gem_exec_object2 *exec_list,
3522 int count)
3523{
3524 struct drm_i915_private *dev_priv = dev->dev_private;
3525 int ret, i, retry;
3526
3527 /* attempt to pin all of the buffers into the GTT */
3528 for (retry = 0; retry < 2; retry++) {
3529 ret = 0;
3530 for (i = 0; i < count; i++) {
3531 struct drm_i915_gem_exec_object2 *entry = &exec_list[i];
Daniel Vetter16e809a2010-09-16 19:37:04 +02003532 struct drm_i915_gem_object *obj = to_intel_bo(object_list[i]);
Chris Wilson9af90d12010-10-17 10:01:56 +01003533 bool need_fence =
3534 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
3535 obj->tiling_mode != I915_TILING_NONE;
3536
Daniel Vetter16e809a2010-09-16 19:37:04 +02003537 /* g33/pnv can't fence buffers in the unmappable part */
3538 bool need_mappable =
3539 entry->relocation_count ? true : need_fence;
3540
Chris Wilson9af90d12010-10-17 10:01:56 +01003541 /* Check fence reg constraints and rebind if necessary */
Chris Wilsona00b10c2010-09-24 21:15:47 +01003542 if ((need_fence && !obj->fenceable) ||
3543 (need_mappable && !obj->mappable)) {
Chris Wilson9af90d12010-10-17 10:01:56 +01003544 ret = i915_gem_object_unbind(&obj->base);
3545 if (ret)
3546 break;
3547 }
3548
Daniel Vetter920afa72010-09-16 17:54:23 +02003549 ret = i915_gem_object_pin(&obj->base,
Daniel Vetter16e809a2010-09-16 19:37:04 +02003550 entry->alignment,
Chris Wilsona00b10c2010-09-24 21:15:47 +01003551 need_mappable,
3552 need_fence);
Chris Wilson9af90d12010-10-17 10:01:56 +01003553 if (ret)
3554 break;
3555
3556 /*
3557 * Pre-965 chips need a fence register set up in order
3558 * to properly handle blits to/from tiled surfaces.
3559 */
3560 if (need_fence) {
3561 ret = i915_gem_object_get_fence_reg(&obj->base, true);
3562 if (ret) {
3563 i915_gem_object_unpin(&obj->base);
3564 break;
3565 }
3566
3567 dev_priv->fence_regs[obj->fence_reg].gpu = true;
3568 }
3569
3570 entry->offset = obj->gtt_offset;
3571 }
3572
3573 while (i--)
3574 i915_gem_object_unpin(object_list[i]);
3575
3576 if (ret == 0)
3577 break;
3578
3579 if (ret != -ENOSPC || retry)
3580 return ret;
3581
3582 ret = i915_gem_evict_everything(dev);
3583 if (ret)
3584 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003585 }
3586
Eric Anholt673a3942008-07-30 12:06:12 -07003587 return 0;
3588}
3589
Eric Anholt673a3942008-07-30 12:06:12 -07003590/* Throttle our rendering by waiting until the ring has completed our requests
3591 * emitted over 20 msec ago.
3592 *
Eric Anholtb9624422009-06-03 07:27:35 +00003593 * Note that if we were to use the current jiffies each time around the loop,
3594 * we wouldn't escape the function with any frames outstanding if the time to
3595 * render a frame was over 20ms.
3596 *
Eric Anholt673a3942008-07-30 12:06:12 -07003597 * This should get us reasonable parallelism between CPU and GPU but also
3598 * relatively low latency when blocking on a particular request to finish.
3599 */
3600static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003601i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003602{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003603 struct drm_i915_private *dev_priv = dev->dev_private;
3604 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00003605 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003606 struct drm_i915_gem_request *request;
3607 struct intel_ring_buffer *ring = NULL;
3608 u32 seqno = 0;
3609 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003610
Chris Wilson1c255952010-09-26 11:03:27 +01003611 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003612 list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
Eric Anholtb9624422009-06-03 07:27:35 +00003613 if (time_after_eq(request->emitted_jiffies, recent_enough))
3614 break;
3615
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003616 ring = request->ring;
3617 seqno = request->seqno;
Eric Anholtb9624422009-06-03 07:27:35 +00003618 }
Chris Wilson1c255952010-09-26 11:03:27 +01003619 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003620
3621 if (seqno == 0)
3622 return 0;
3623
3624 ret = 0;
Chris Wilson78501ea2010-10-27 12:18:21 +01003625 if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) {
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003626 /* And wait for the seqno passing without holding any locks and
3627 * causing extra latency for others. This is safe as the irq
3628 * generation is designed to be run atomically and so is
3629 * lockless.
3630 */
Chris Wilson78501ea2010-10-27 12:18:21 +01003631 ring->user_irq_get(ring);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003632 ret = wait_event_interruptible(ring->irq_queue,
Chris Wilson78501ea2010-10-27 12:18:21 +01003633 i915_seqno_passed(ring->get_seqno(ring), seqno)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003634 || atomic_read(&dev_priv->mm.wedged));
Chris Wilson78501ea2010-10-27 12:18:21 +01003635 ring->user_irq_put(ring);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003636
3637 if (ret == 0 && atomic_read(&dev_priv->mm.wedged))
3638 ret = -EIO;
3639 }
3640
3641 if (ret == 0)
3642 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
Eric Anholtb9624422009-06-03 07:27:35 +00003643
Eric Anholt673a3942008-07-30 12:06:12 -07003644 return ret;
3645}
3646
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003647static int
Chris Wilson2549d6c2010-10-14 12:10:41 +01003648i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec,
3649 uint64_t exec_offset)
Chris Wilson83d60792009-06-06 09:45:57 +01003650{
3651 uint32_t exec_start, exec_len;
3652
3653 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
3654 exec_len = (uint32_t) exec->batch_len;
3655
3656 if ((exec_start | exec_len) & 0x7)
3657 return -EINVAL;
3658
3659 if (!exec_start)
3660 return -EINVAL;
3661
3662 return 0;
3663}
3664
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003665static int
Chris Wilson2549d6c2010-10-14 12:10:41 +01003666validate_exec_list(struct drm_i915_gem_exec_object2 *exec,
3667 int count)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003668{
Chris Wilson2549d6c2010-10-14 12:10:41 +01003669 int i;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003670
Chris Wilson2549d6c2010-10-14 12:10:41 +01003671 for (i = 0; i < count; i++) {
3672 char __user *ptr = (char __user *)(uintptr_t)exec[i].relocs_ptr;
3673 size_t length = exec[i].relocation_count * sizeof(struct drm_i915_gem_relocation_entry);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003674
Chris Wilson2549d6c2010-10-14 12:10:41 +01003675 if (!access_ok(VERIFY_READ, ptr, length))
3676 return -EFAULT;
3677
Chris Wilsonb5dc6082010-10-20 20:59:57 +01003678 /* we may also need to update the presumed offsets */
3679 if (!access_ok(VERIFY_WRITE, ptr, length))
3680 return -EFAULT;
3681
Chris Wilson2549d6c2010-10-14 12:10:41 +01003682 if (fault_in_pages_readable(ptr, length))
3683 return -EFAULT;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003684 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003685
Chris Wilson2549d6c2010-10-14 12:10:41 +01003686 return 0;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003687}
3688
Chris Wilson2549d6c2010-10-14 12:10:41 +01003689static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003690i915_gem_do_execbuffer(struct drm_device *dev, void *data,
Chris Wilson9af90d12010-10-17 10:01:56 +01003691 struct drm_file *file,
Jesse Barnes76446ca2009-12-17 22:05:42 -05003692 struct drm_i915_gem_execbuffer2 *args,
3693 struct drm_i915_gem_exec_object2 *exec_list)
Eric Anholt673a3942008-07-30 12:06:12 -07003694{
3695 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003696 struct drm_gem_object **object_list = NULL;
3697 struct drm_gem_object *batch_obj;
Eric Anholt201361a2009-03-11 12:30:04 -07003698 struct drm_clip_rect *cliprects = NULL;
Chris Wilson8dc5d142010-08-12 12:36:12 +01003699 struct drm_i915_gem_request *request = NULL;
Chris Wilson9af90d12010-10-17 10:01:56 +01003700 int ret, i, flips;
Eric Anholt673a3942008-07-30 12:06:12 -07003701 uint64_t exec_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003702
Zou Nan hai852835f2010-05-21 09:08:56 +08003703 struct intel_ring_buffer *ring = NULL;
3704
Chris Wilson30dbf0c2010-09-25 10:19:17 +01003705 ret = i915_gem_check_is_wedged(dev);
3706 if (ret)
3707 return ret;
3708
Chris Wilson2549d6c2010-10-14 12:10:41 +01003709 ret = validate_exec_list(exec_list, args->buffer_count);
3710 if (ret)
3711 return ret;
3712
Eric Anholt673a3942008-07-30 12:06:12 -07003713#if WATCH_EXEC
3714 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3715 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3716#endif
Chris Wilson549f7362010-10-19 11:19:32 +01003717 switch (args->flags & I915_EXEC_RING_MASK) {
3718 case I915_EXEC_DEFAULT:
3719 case I915_EXEC_RENDER:
3720 ring = &dev_priv->render_ring;
3721 break;
3722 case I915_EXEC_BSD:
Zou Nan haid1b851f2010-05-21 09:08:57 +08003723 if (!HAS_BSD(dev)) {
Chris Wilson549f7362010-10-19 11:19:32 +01003724 DRM_ERROR("execbuf with invalid ring (BSD)\n");
Zou Nan haid1b851f2010-05-21 09:08:57 +08003725 return -EINVAL;
3726 }
3727 ring = &dev_priv->bsd_ring;
Chris Wilson549f7362010-10-19 11:19:32 +01003728 break;
3729 case I915_EXEC_BLT:
3730 if (!HAS_BLT(dev)) {
3731 DRM_ERROR("execbuf with invalid ring (BLT)\n");
3732 return -EINVAL;
3733 }
3734 ring = &dev_priv->blt_ring;
3735 break;
3736 default:
3737 DRM_ERROR("execbuf with unknown ring: %d\n",
3738 (int)(args->flags & I915_EXEC_RING_MASK));
3739 return -EINVAL;
Zou Nan haid1b851f2010-05-21 09:08:57 +08003740 }
3741
Eric Anholt4f481ed2008-09-10 14:22:49 -07003742 if (args->buffer_count < 1) {
3743 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3744 return -EINVAL;
3745 }
Eric Anholtc8e0f932009-11-22 03:49:37 +01003746 object_list = drm_malloc_ab(sizeof(*object_list), args->buffer_count);
Jesse Barnes76446ca2009-12-17 22:05:42 -05003747 if (object_list == NULL) {
3748 DRM_ERROR("Failed to allocate object list for %d buffers\n",
Eric Anholt673a3942008-07-30 12:06:12 -07003749 args->buffer_count);
3750 ret = -ENOMEM;
3751 goto pre_mutex_err;
3752 }
Eric Anholt673a3942008-07-30 12:06:12 -07003753
Eric Anholt201361a2009-03-11 12:30:04 -07003754 if (args->num_cliprects != 0) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003755 cliprects = kcalloc(args->num_cliprects, sizeof(*cliprects),
3756 GFP_KERNEL);
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003757 if (cliprects == NULL) {
3758 ret = -ENOMEM;
Eric Anholt201361a2009-03-11 12:30:04 -07003759 goto pre_mutex_err;
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003760 }
Eric Anholt201361a2009-03-11 12:30:04 -07003761
3762 ret = copy_from_user(cliprects,
3763 (struct drm_clip_rect __user *)
3764 (uintptr_t) args->cliprects_ptr,
3765 sizeof(*cliprects) * args->num_cliprects);
3766 if (ret != 0) {
3767 DRM_ERROR("copy %d cliprects failed: %d\n",
3768 args->num_cliprects, ret);
Dan Carpenterc877cdc2010-06-23 19:03:01 +02003769 ret = -EFAULT;
Eric Anholt201361a2009-03-11 12:30:04 -07003770 goto pre_mutex_err;
3771 }
3772 }
3773
Chris Wilson8dc5d142010-08-12 12:36:12 +01003774 request = kzalloc(sizeof(*request), GFP_KERNEL);
3775 if (request == NULL) {
3776 ret = -ENOMEM;
Chris Wilsona198bc82009-02-06 16:55:20 +00003777 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003778 }
3779
Chris Wilson76c1dec2010-09-25 11:22:51 +01003780 ret = i915_mutex_lock_interruptible(dev);
3781 if (ret)
3782 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003783
Eric Anholt673a3942008-07-30 12:06:12 -07003784 if (dev_priv->mm.suspended) {
Eric Anholt673a3942008-07-30 12:06:12 -07003785 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003786 ret = -EBUSY;
3787 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003788 }
3789
Keith Packardac94a962008-11-20 23:30:27 -08003790 /* Look up object handles */
Eric Anholt673a3942008-07-30 12:06:12 -07003791 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson7e318e12010-10-27 13:43:39 +01003792 struct drm_i915_gem_object *obj_priv;
3793
Chris Wilson9af90d12010-10-17 10:01:56 +01003794 object_list[i] = drm_gem_object_lookup(dev, file,
Eric Anholt673a3942008-07-30 12:06:12 -07003795 exec_list[i].handle);
3796 if (object_list[i] == NULL) {
3797 DRM_ERROR("Invalid object handle %d at index %d\n",
3798 exec_list[i].handle, i);
Chris Wilson0ce907f2010-01-23 20:26:35 +00003799 /* prevent error path from reading uninitialized data */
3800 args->buffer_count = i + 1;
Chris Wilsonbf79cb92010-08-04 14:19:46 +01003801 ret = -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07003802 goto err;
3803 }
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003804
Daniel Vetter23010e42010-03-08 13:35:02 +01003805 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003806 if (obj_priv->in_execbuffer) {
3807 DRM_ERROR("Object %p appears more than once in object list\n",
3808 object_list[i]);
Chris Wilson0ce907f2010-01-23 20:26:35 +00003809 /* prevent error path from reading uninitialized data */
3810 args->buffer_count = i + 1;
Chris Wilsonbf79cb92010-08-04 14:19:46 +01003811 ret = -EINVAL;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003812 goto err;
3813 }
3814 obj_priv->in_execbuffer = true;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003815 }
3816
Chris Wilson9af90d12010-10-17 10:01:56 +01003817 /* Move the objects en-masse into the GTT, evicting if necessary. */
3818 ret = i915_gem_execbuffer_pin(dev, file,
3819 object_list, exec_list,
3820 args->buffer_count);
3821 if (ret)
3822 goto err;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003823
Chris Wilson9af90d12010-10-17 10:01:56 +01003824 /* The objects are in their final locations, apply the relocations. */
3825 for (i = 0; i < args->buffer_count; i++) {
3826 struct drm_i915_gem_object *obj = to_intel_bo(object_list[i]);
3827 obj->base.pending_read_domains = 0;
3828 obj->base.pending_write_domain = 0;
3829 ret = i915_gem_execbuffer_relocate(obj, file, &exec_list[i]);
Eric Anholt673a3942008-07-30 12:06:12 -07003830 if (ret)
3831 goto err;
3832 }
3833
Eric Anholt673a3942008-07-30 12:06:12 -07003834 /* Set the pending read domains for the batch buffer to COMMAND */
3835 batch_obj = object_list[args->buffer_count-1];
Chris Wilson5f26a2c2009-06-06 09:45:58 +01003836 if (batch_obj->pending_write_domain) {
3837 DRM_ERROR("Attempting to use self-modifying batch buffer\n");
3838 ret = -EINVAL;
3839 goto err;
3840 }
3841 batch_obj->pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
Eric Anholt673a3942008-07-30 12:06:12 -07003842
Chris Wilson9af90d12010-10-17 10:01:56 +01003843 /* Sanity check the batch buffer */
3844 exec_offset = to_intel_bo(batch_obj)->gtt_offset;
3845 ret = i915_gem_check_execbuffer(args, exec_offset);
Chris Wilson83d60792009-06-06 09:45:57 +01003846 if (ret != 0) {
3847 DRM_ERROR("execbuf with invalid offset/length\n");
3848 goto err;
3849 }
3850
Keith Packard646f0f62008-11-20 23:23:03 -08003851 /* Zero the global flush/invalidate flags. These
3852 * will be modified as new domains are computed
3853 * for each object
3854 */
3855 dev->invalidate_domains = 0;
3856 dev->flush_domains = 0;
Chris Wilson92204342010-09-18 11:02:01 +01003857 dev_priv->mm.flush_rings = 0;
Chris Wilson7e318e12010-10-27 13:43:39 +01003858 for (i = 0; i < args->buffer_count; i++)
3859 i915_gem_object_set_to_gpu_domain(object_list[i], ring);
Eric Anholt673a3942008-07-30 12:06:12 -07003860
Keith Packard646f0f62008-11-20 23:23:03 -08003861 if (dev->invalidate_domains | dev->flush_domains) {
3862#if WATCH_EXEC
3863 DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
3864 __func__,
3865 dev->invalidate_domains,
3866 dev->flush_domains);
3867#endif
Chris Wilson9af90d12010-10-17 10:01:56 +01003868 i915_gem_flush(dev, file,
Keith Packard646f0f62008-11-20 23:23:03 -08003869 dev->invalidate_domains,
Chris Wilson92204342010-09-18 11:02:01 +01003870 dev->flush_domains,
3871 dev_priv->mm.flush_rings);
Keith Packard646f0f62008-11-20 23:23:03 -08003872 }
Eric Anholt673a3942008-07-30 12:06:12 -07003873
Eric Anholt673a3942008-07-30 12:06:12 -07003874#if WATCH_COHERENCY
3875 for (i = 0; i < args->buffer_count; i++) {
3876 i915_gem_object_check_coherency(object_list[i],
3877 exec_list[i].handle);
3878 }
3879#endif
3880
Eric Anholt673a3942008-07-30 12:06:12 -07003881#if WATCH_EXEC
Ben Gamari6911a9b2009-04-02 11:24:54 -07003882 i915_gem_dump_object(batch_obj,
Eric Anholt673a3942008-07-30 12:06:12 -07003883 args->batch_len,
3884 __func__,
3885 ~0);
3886#endif
3887
Chris Wilsone59f2ba2010-10-07 17:28:15 +01003888 /* Check for any pending flips. As we only maintain a flip queue depth
3889 * of 1, we can simply insert a WAIT for the next display flip prior
3890 * to executing the batch and avoid stalling the CPU.
3891 */
3892 flips = 0;
3893 for (i = 0; i < args->buffer_count; i++) {
3894 if (object_list[i]->write_domain)
3895 flips |= atomic_read(&to_intel_bo(object_list[i])->pending_flip);
3896 }
3897 if (flips) {
3898 int plane, flip_mask;
3899
3900 for (plane = 0; flips >> plane; plane++) {
3901 if (((flips >> plane) & 1) == 0)
3902 continue;
3903
3904 if (plane)
3905 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
3906 else
3907 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
3908
Chris Wilsone1f99ce2010-10-27 12:45:26 +01003909 ret = intel_ring_begin(ring, 2);
3910 if (ret)
3911 goto err;
3912
Chris Wilson78501ea2010-10-27 12:18:21 +01003913 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
3914 intel_ring_emit(ring, MI_NOOP);
3915 intel_ring_advance(ring);
Chris Wilsone59f2ba2010-10-07 17:28:15 +01003916 }
3917 }
3918
Eric Anholt673a3942008-07-30 12:06:12 -07003919 /* Exec the batchbuffer */
Chris Wilson78501ea2010-10-27 12:18:21 +01003920 ret = ring->dispatch_execbuffer(ring, args, cliprects, exec_offset);
Eric Anholt673a3942008-07-30 12:06:12 -07003921 if (ret) {
3922 DRM_ERROR("dispatch failed %d\n", ret);
3923 goto err;
3924 }
3925
Chris Wilson7e318e12010-10-27 13:43:39 +01003926 for (i = 0; i < args->buffer_count; i++) {
3927 struct drm_gem_object *obj = object_list[i];
3928
3929 obj->read_domains = obj->pending_read_domains;
3930 obj->write_domain = obj->pending_write_domain;
3931
3932 i915_gem_object_move_to_active(obj, ring);
3933 if (obj->write_domain) {
3934 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
3935 obj_priv->dirty = 1;
3936 list_move_tail(&obj_priv->gpu_write_list,
3937 &ring->gpu_write_list);
3938 intel_mark_busy(dev, obj);
3939 }
3940
3941 trace_i915_gem_object_change_domain(obj,
3942 obj->read_domains,
3943 obj->write_domain);
3944 }
3945
Eric Anholt673a3942008-07-30 12:06:12 -07003946 /*
3947 * Ensure that the commands in the batch buffer are
3948 * finished before the interrupt fires
3949 */
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01003950 i915_retire_commands(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07003951
Chris Wilson3cce4692010-10-27 16:11:02 +01003952 if (i915_add_request(dev, file, request, ring))
3953 ring->outstanding_lazy_request = true;
3954 else
3955 request = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07003956
Eric Anholt673a3942008-07-30 12:06:12 -07003957err:
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003958 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson7e318e12010-10-27 13:43:39 +01003959 if (object_list[i] == NULL)
3960 break;
3961
3962 to_intel_bo(object_list[i])->in_execbuffer = false;
Julia Lawallaad87df2008-12-21 16:28:47 +01003963 drm_gem_object_unreference(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003964 }
Julia Lawallaad87df2008-12-21 16:28:47 +01003965
Eric Anholt673a3942008-07-30 12:06:12 -07003966 mutex_unlock(&dev->struct_mutex);
3967
Chris Wilson93533c22010-01-31 10:40:48 +00003968pre_mutex_err:
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003969 drm_free_large(object_list);
Eric Anholt9a298b22009-03-24 12:23:04 -07003970 kfree(cliprects);
Chris Wilson8dc5d142010-08-12 12:36:12 +01003971 kfree(request);
Eric Anholt673a3942008-07-30 12:06:12 -07003972
3973 return ret;
3974}
3975
Jesse Barnes76446ca2009-12-17 22:05:42 -05003976/*
3977 * Legacy execbuffer just creates an exec2 list from the original exec object
3978 * list array and passes it to the real function.
3979 */
3980int
3981i915_gem_execbuffer(struct drm_device *dev, void *data,
3982 struct drm_file *file_priv)
3983{
3984 struct drm_i915_gem_execbuffer *args = data;
3985 struct drm_i915_gem_execbuffer2 exec2;
3986 struct drm_i915_gem_exec_object *exec_list = NULL;
3987 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
3988 int ret, i;
3989
3990#if WATCH_EXEC
3991 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3992 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3993#endif
3994
3995 if (args->buffer_count < 1) {
3996 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3997 return -EINVAL;
3998 }
3999
4000 /* Copy in the exec list from userland */
4001 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
4002 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
4003 if (exec_list == NULL || exec2_list == NULL) {
4004 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
4005 args->buffer_count);
4006 drm_free_large(exec_list);
4007 drm_free_large(exec2_list);
4008 return -ENOMEM;
4009 }
4010 ret = copy_from_user(exec_list,
4011 (struct drm_i915_relocation_entry __user *)
4012 (uintptr_t) args->buffers_ptr,
4013 sizeof(*exec_list) * args->buffer_count);
4014 if (ret != 0) {
4015 DRM_ERROR("copy %d exec entries failed %d\n",
4016 args->buffer_count, ret);
4017 drm_free_large(exec_list);
4018 drm_free_large(exec2_list);
4019 return -EFAULT;
4020 }
4021
4022 for (i = 0; i < args->buffer_count; i++) {
4023 exec2_list[i].handle = exec_list[i].handle;
4024 exec2_list[i].relocation_count = exec_list[i].relocation_count;
4025 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
4026 exec2_list[i].alignment = exec_list[i].alignment;
4027 exec2_list[i].offset = exec_list[i].offset;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004028 if (INTEL_INFO(dev)->gen < 4)
Jesse Barnes76446ca2009-12-17 22:05:42 -05004029 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
4030 else
4031 exec2_list[i].flags = 0;
4032 }
4033
4034 exec2.buffers_ptr = args->buffers_ptr;
4035 exec2.buffer_count = args->buffer_count;
4036 exec2.batch_start_offset = args->batch_start_offset;
4037 exec2.batch_len = args->batch_len;
4038 exec2.DR1 = args->DR1;
4039 exec2.DR4 = args->DR4;
4040 exec2.num_cliprects = args->num_cliprects;
4041 exec2.cliprects_ptr = args->cliprects_ptr;
Zou Nan hai852835f2010-05-21 09:08:56 +08004042 exec2.flags = I915_EXEC_RENDER;
Jesse Barnes76446ca2009-12-17 22:05:42 -05004043
4044 ret = i915_gem_do_execbuffer(dev, data, file_priv, &exec2, exec2_list);
4045 if (!ret) {
4046 /* Copy the new buffer offsets back to the user's exec list. */
4047 for (i = 0; i < args->buffer_count; i++)
4048 exec_list[i].offset = exec2_list[i].offset;
4049 /* ... and back out to userspace */
4050 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
4051 (uintptr_t) args->buffers_ptr,
4052 exec_list,
4053 sizeof(*exec_list) * args->buffer_count);
4054 if (ret) {
4055 ret = -EFAULT;
4056 DRM_ERROR("failed to copy %d exec entries "
4057 "back to user (%d)\n",
4058 args->buffer_count, ret);
4059 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05004060 }
4061
4062 drm_free_large(exec_list);
4063 drm_free_large(exec2_list);
4064 return ret;
4065}
4066
4067int
4068i915_gem_execbuffer2(struct drm_device *dev, void *data,
4069 struct drm_file *file_priv)
4070{
4071 struct drm_i915_gem_execbuffer2 *args = data;
4072 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
4073 int ret;
4074
4075#if WATCH_EXEC
4076 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
4077 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
4078#endif
4079
4080 if (args->buffer_count < 1) {
4081 DRM_ERROR("execbuf2 with %d buffers\n", args->buffer_count);
4082 return -EINVAL;
4083 }
4084
4085 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
4086 if (exec2_list == NULL) {
4087 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
4088 args->buffer_count);
4089 return -ENOMEM;
4090 }
4091 ret = copy_from_user(exec2_list,
4092 (struct drm_i915_relocation_entry __user *)
4093 (uintptr_t) args->buffers_ptr,
4094 sizeof(*exec2_list) * args->buffer_count);
4095 if (ret != 0) {
4096 DRM_ERROR("copy %d exec entries failed %d\n",
4097 args->buffer_count, ret);
4098 drm_free_large(exec2_list);
4099 return -EFAULT;
4100 }
4101
4102 ret = i915_gem_do_execbuffer(dev, data, file_priv, args, exec2_list);
4103 if (!ret) {
4104 /* Copy the new buffer offsets back to the user's exec list. */
4105 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
4106 (uintptr_t) args->buffers_ptr,
4107 exec2_list,
4108 sizeof(*exec2_list) * args->buffer_count);
4109 if (ret) {
4110 ret = -EFAULT;
4111 DRM_ERROR("failed to copy %d exec entries "
4112 "back to user (%d)\n",
4113 args->buffer_count, ret);
4114 }
4115 }
4116
4117 drm_free_large(exec2_list);
4118 return ret;
4119}
4120
Eric Anholt673a3942008-07-30 12:06:12 -07004121int
Daniel Vetter920afa72010-09-16 17:54:23 +02004122i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment,
Chris Wilsona00b10c2010-09-24 21:15:47 +01004123 bool mappable, bool need_fence)
Eric Anholt673a3942008-07-30 12:06:12 -07004124{
4125 struct drm_device *dev = obj->dev;
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004126 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01004127 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004128 int ret;
4129
Daniel Vetter778c3542010-05-13 11:49:44 +02004130 BUG_ON(obj_priv->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT);
Chris Wilson23bc5982010-09-29 16:10:57 +01004131 WARN_ON(i915_verify_lists(dev));
Chris Wilsonac0c6b52010-05-27 13:18:18 +01004132
4133 if (obj_priv->gtt_space != NULL) {
Chris Wilsona00b10c2010-09-24 21:15:47 +01004134 if ((alignment && obj_priv->gtt_offset & (alignment - 1)) ||
4135 (need_fence && !obj_priv->fenceable) ||
4136 (mappable && !obj_priv->mappable)) {
Chris Wilsonae7d49d2010-08-04 12:37:41 +01004137 WARN(obj_priv->pin_count,
4138 "bo is already pinned with incorrect alignment:"
Chris Wilsona00b10c2010-09-24 21:15:47 +01004139 " offset=%x, req.alignment=%x, need_fence=%d, fenceable=%d, mappable=%d, cpu_accessible=%d\n",
4140 obj_priv->gtt_offset, alignment,
4141 need_fence, obj_priv->fenceable,
4142 mappable, obj_priv->mappable);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01004143 ret = i915_gem_object_unbind(obj);
4144 if (ret)
4145 return ret;
4146 }
4147 }
4148
Eric Anholt673a3942008-07-30 12:06:12 -07004149 if (obj_priv->gtt_space == NULL) {
Chris Wilsona00b10c2010-09-24 21:15:47 +01004150 ret = i915_gem_object_bind_to_gtt(obj, alignment,
4151 mappable, need_fence);
Chris Wilson97311292009-09-21 00:22:34 +01004152 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07004153 return ret;
Chris Wilson22c344e2009-02-11 14:26:45 +00004154 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05004155
Chris Wilson74653782010-10-29 10:41:23 +01004156 if (obj_priv->pin_count++ == 0) {
Chris Wilsona00b10c2010-09-24 21:15:47 +01004157 i915_gem_info_add_pin(dev_priv, obj_priv, mappable);
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004158 if (!obj_priv->active)
Chris Wilson69dc4982010-10-19 10:36:51 +01004159 list_move_tail(&obj_priv->mm_list,
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004160 &dev_priv->mm.pinned_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004161 }
Daniel Vetterfb7d5162010-10-01 22:05:20 +02004162 BUG_ON(!obj_priv->pin_mappable && mappable);
Eric Anholt673a3942008-07-30 12:06:12 -07004163
Chris Wilson23bc5982010-09-29 16:10:57 +01004164 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07004165 return 0;
4166}
4167
4168void
4169i915_gem_object_unpin(struct drm_gem_object *obj)
4170{
4171 struct drm_device *dev = obj->dev;
4172 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01004173 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004174
Chris Wilson23bc5982010-09-29 16:10:57 +01004175 WARN_ON(i915_verify_lists(dev));
Chris Wilson74653782010-10-29 10:41:23 +01004176 BUG_ON(obj_priv->pin_count == 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004177 BUG_ON(obj_priv->gtt_space == NULL);
4178
Chris Wilson74653782010-10-29 10:41:23 +01004179 if (--obj_priv->pin_count == 0) {
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004180 if (!obj_priv->active)
Chris Wilson69dc4982010-10-19 10:36:51 +01004181 list_move_tail(&obj_priv->mm_list,
Eric Anholt673a3942008-07-30 12:06:12 -07004182 &dev_priv->mm.inactive_list);
Chris Wilsona00b10c2010-09-24 21:15:47 +01004183 i915_gem_info_remove_pin(dev_priv, obj_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07004184 }
Chris Wilson23bc5982010-09-29 16:10:57 +01004185 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07004186}
4187
4188int
4189i915_gem_pin_ioctl(struct drm_device *dev, void *data,
4190 struct drm_file *file_priv)
4191{
4192 struct drm_i915_gem_pin *args = data;
4193 struct drm_gem_object *obj;
4194 struct drm_i915_gem_object *obj_priv;
4195 int ret;
4196
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004197 ret = i915_mutex_lock_interruptible(dev);
4198 if (ret)
4199 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004200
4201 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4202 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004203 ret = -ENOENT;
4204 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07004205 }
Daniel Vetter23010e42010-03-08 13:35:02 +01004206 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004207
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004208 if (obj_priv->madv != I915_MADV_WILLNEED) {
4209 DRM_ERROR("Attempting to pin a purgeable buffer\n");
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004210 ret = -EINVAL;
4211 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004212 }
4213
Jesse Barnes79e53942008-11-07 14:24:08 -08004214 if (obj_priv->pin_filp != NULL && obj_priv->pin_filp != file_priv) {
4215 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
4216 args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004217 ret = -EINVAL;
4218 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08004219 }
4220
4221 obj_priv->user_pin_count++;
4222 obj_priv->pin_filp = file_priv;
4223 if (obj_priv->user_pin_count == 1) {
Chris Wilsona00b10c2010-09-24 21:15:47 +01004224 ret = i915_gem_object_pin(obj, args->alignment,
4225 true, obj_priv->tiling_mode);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004226 if (ret)
4227 goto out;
Eric Anholt673a3942008-07-30 12:06:12 -07004228 }
4229
4230 /* XXX - flush the CPU caches for pinned objects
4231 * as the X server doesn't manage domains yet
4232 */
Eric Anholte47c68e2008-11-14 13:35:19 -08004233 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004234 args->offset = obj_priv->gtt_offset;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004235out:
Eric Anholt673a3942008-07-30 12:06:12 -07004236 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004237unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07004238 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004239 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004240}
4241
4242int
4243i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
4244 struct drm_file *file_priv)
4245{
4246 struct drm_i915_gem_pin *args = data;
4247 struct drm_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08004248 struct drm_i915_gem_object *obj_priv;
Chris Wilson76c1dec2010-09-25 11:22:51 +01004249 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004250
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004251 ret = i915_mutex_lock_interruptible(dev);
4252 if (ret)
4253 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004254
4255 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4256 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004257 ret = -ENOENT;
4258 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07004259 }
Daniel Vetter23010e42010-03-08 13:35:02 +01004260 obj_priv = to_intel_bo(obj);
Chris Wilson76c1dec2010-09-25 11:22:51 +01004261
Jesse Barnes79e53942008-11-07 14:24:08 -08004262 if (obj_priv->pin_filp != file_priv) {
4263 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
4264 args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004265 ret = -EINVAL;
4266 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08004267 }
4268 obj_priv->user_pin_count--;
4269 if (obj_priv->user_pin_count == 0) {
4270 obj_priv->pin_filp = NULL;
4271 i915_gem_object_unpin(obj);
4272 }
Eric Anholt673a3942008-07-30 12:06:12 -07004273
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004274out:
Eric Anholt673a3942008-07-30 12:06:12 -07004275 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004276unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07004277 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004278 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004279}
4280
4281int
4282i915_gem_busy_ioctl(struct drm_device *dev, void *data,
4283 struct drm_file *file_priv)
4284{
4285 struct drm_i915_gem_busy *args = data;
4286 struct drm_gem_object *obj;
4287 struct drm_i915_gem_object *obj_priv;
Chris Wilson30dbf0c2010-09-25 10:19:17 +01004288 int ret;
4289
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004290 ret = i915_mutex_lock_interruptible(dev);
4291 if (ret)
4292 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004293
Eric Anholt673a3942008-07-30 12:06:12 -07004294 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4295 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004296 ret = -ENOENT;
4297 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07004298 }
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004299 obj_priv = to_intel_bo(obj);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004300
Chris Wilson0be555b2010-08-04 15:36:30 +01004301 /* Count all active objects as busy, even if they are currently not used
4302 * by the gpu. Users of this interface expect objects to eventually
4303 * become non-busy without any further actions, therefore emit any
4304 * necessary flushes here.
Eric Anholtc4de0a52008-12-14 19:05:04 -08004305 */
Chris Wilson0be555b2010-08-04 15:36:30 +01004306 args->busy = obj_priv->active;
4307 if (args->busy) {
4308 /* Unconditionally flush objects, even when the gpu still uses this
4309 * object. Userspace calling this function indicates that it wants to
4310 * use this buffer rather sooner than later, so issuing the required
4311 * flush earlier is beneficial.
4312 */
Chris Wilsonc78ec302010-09-20 12:50:23 +01004313 if (obj->write_domain & I915_GEM_GPU_DOMAINS)
4314 i915_gem_flush_ring(dev, file_priv,
Chris Wilson92204342010-09-18 11:02:01 +01004315 obj_priv->ring,
4316 0, obj->write_domain);
Chris Wilson0be555b2010-08-04 15:36:30 +01004317
4318 /* Update the active list for the hardware's current position.
4319 * Otherwise this only updates on a delayed timer or when irqs
4320 * are actually unmasked, and our working set ends up being
4321 * larger than required.
4322 */
4323 i915_gem_retire_requests_ring(dev, obj_priv->ring);
4324
4325 args->busy = obj_priv->active;
4326 }
Eric Anholt673a3942008-07-30 12:06:12 -07004327
4328 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004329unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07004330 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004331 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004332}
4333
4334int
4335i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4336 struct drm_file *file_priv)
4337{
4338 return i915_gem_ring_throttle(dev, file_priv);
4339}
4340
Chris Wilson3ef94da2009-09-14 16:50:29 +01004341int
4342i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4343 struct drm_file *file_priv)
4344{
4345 struct drm_i915_gem_madvise *args = data;
4346 struct drm_gem_object *obj;
4347 struct drm_i915_gem_object *obj_priv;
Chris Wilson76c1dec2010-09-25 11:22:51 +01004348 int ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004349
4350 switch (args->madv) {
4351 case I915_MADV_DONTNEED:
4352 case I915_MADV_WILLNEED:
4353 break;
4354 default:
4355 return -EINVAL;
4356 }
4357
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004358 ret = i915_mutex_lock_interruptible(dev);
4359 if (ret)
4360 return ret;
4361
Chris Wilson3ef94da2009-09-14 16:50:29 +01004362 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4363 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004364 ret = -ENOENT;
4365 goto unlock;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004366 }
Daniel Vetter23010e42010-03-08 13:35:02 +01004367 obj_priv = to_intel_bo(obj);
Chris Wilson3ef94da2009-09-14 16:50:29 +01004368
4369 if (obj_priv->pin_count) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004370 ret = -EINVAL;
4371 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004372 }
4373
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004374 if (obj_priv->madv != __I915_MADV_PURGED)
4375 obj_priv->madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004376
Chris Wilson2d7ef392009-09-20 23:13:10 +01004377 /* if the object is no longer bound, discard its backing storage */
4378 if (i915_gem_object_is_purgeable(obj_priv) &&
4379 obj_priv->gtt_space == NULL)
4380 i915_gem_object_truncate(obj);
4381
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004382 args->retained = obj_priv->madv != __I915_MADV_PURGED;
4383
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004384out:
Chris Wilson3ef94da2009-09-14 16:50:29 +01004385 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004386unlock:
Chris Wilson3ef94da2009-09-14 16:50:29 +01004387 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004388 return ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004389}
4390
Daniel Vetterac52bc52010-04-09 19:05:06 +00004391struct drm_gem_object * i915_gem_alloc_object(struct drm_device *dev,
4392 size_t size)
4393{
Chris Wilson73aa8082010-09-30 11:46:12 +01004394 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc397b902010-04-09 19:05:07 +00004395 struct drm_i915_gem_object *obj;
4396
4397 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
4398 if (obj == NULL)
4399 return NULL;
4400
4401 if (drm_gem_object_init(dev, &obj->base, size) != 0) {
4402 kfree(obj);
4403 return NULL;
4404 }
4405
Chris Wilson73aa8082010-09-30 11:46:12 +01004406 i915_gem_info_add_obj(dev_priv, size);
4407
Daniel Vetterc397b902010-04-09 19:05:07 +00004408 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4409 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4410
4411 obj->agp_type = AGP_USER_MEMORY;
Daniel Vetter62b8b212010-04-09 19:05:08 +00004412 obj->base.driver_private = NULL;
Daniel Vetterc397b902010-04-09 19:05:07 +00004413 obj->fence_reg = I915_FENCE_REG_NONE;
Chris Wilson69dc4982010-10-19 10:36:51 +01004414 INIT_LIST_HEAD(&obj->mm_list);
4415 INIT_LIST_HEAD(&obj->ring_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00004416 INIT_LIST_HEAD(&obj->gpu_write_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00004417 obj->madv = I915_MADV_WILLNEED;
Chris Wilsona00b10c2010-09-24 21:15:47 +01004418 obj->fenceable = true;
4419 obj->mappable = true;
Daniel Vetterc397b902010-04-09 19:05:07 +00004420
Daniel Vetterc397b902010-04-09 19:05:07 +00004421 return &obj->base;
Daniel Vetterac52bc52010-04-09 19:05:06 +00004422}
4423
Eric Anholt673a3942008-07-30 12:06:12 -07004424int i915_gem_init_object(struct drm_gem_object *obj)
4425{
Daniel Vetterc397b902010-04-09 19:05:07 +00004426 BUG();
Jesse Barnesde151cf2008-11-12 10:03:55 -08004427
Eric Anholt673a3942008-07-30 12:06:12 -07004428 return 0;
4429}
4430
Chris Wilsonbe726152010-07-23 23:18:50 +01004431static void i915_gem_free_object_tail(struct drm_gem_object *obj)
4432{
4433 struct drm_device *dev = obj->dev;
4434 drm_i915_private_t *dev_priv = dev->dev_private;
4435 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
4436 int ret;
4437
4438 ret = i915_gem_object_unbind(obj);
4439 if (ret == -ERESTARTSYS) {
Chris Wilson69dc4982010-10-19 10:36:51 +01004440 list_move(&obj_priv->mm_list,
Chris Wilsonbe726152010-07-23 23:18:50 +01004441 &dev_priv->mm.deferred_free_list);
4442 return;
4443 }
4444
Chris Wilson39a01d12010-10-28 13:03:06 +01004445 if (obj->map_list.map)
Chris Wilsonbe726152010-07-23 23:18:50 +01004446 i915_gem_free_mmap_offset(obj);
4447
4448 drm_gem_object_release(obj);
Chris Wilson73aa8082010-09-30 11:46:12 +01004449 i915_gem_info_remove_obj(dev_priv, obj->size);
Chris Wilsonbe726152010-07-23 23:18:50 +01004450
4451 kfree(obj_priv->page_cpu_valid);
4452 kfree(obj_priv->bit_17);
4453 kfree(obj_priv);
4454}
4455
Eric Anholt673a3942008-07-30 12:06:12 -07004456void i915_gem_free_object(struct drm_gem_object *obj)
4457{
Jesse Barnesde151cf2008-11-12 10:03:55 -08004458 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01004459 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004460
Chris Wilson1c5d22f2009-08-25 11:15:50 +01004461 trace_i915_gem_object_destroy(obj);
4462
Eric Anholt673a3942008-07-30 12:06:12 -07004463 while (obj_priv->pin_count > 0)
4464 i915_gem_object_unpin(obj);
4465
Dave Airlie71acb5e2008-12-30 20:31:46 +10004466 if (obj_priv->phys_obj)
4467 i915_gem_detach_phys_object(dev, obj);
4468
Chris Wilsonbe726152010-07-23 23:18:50 +01004469 i915_gem_free_object_tail(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004470}
4471
Jesse Barnes5669fca2009-02-17 15:13:31 -08004472int
Eric Anholt673a3942008-07-30 12:06:12 -07004473i915_gem_idle(struct drm_device *dev)
4474{
4475 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson29105cc2010-01-07 10:39:13 +00004476 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004477
Keith Packard6dbe2772008-10-14 21:41:13 -07004478 mutex_lock(&dev->struct_mutex);
4479
Chris Wilson87acb0a2010-10-19 10:13:00 +01004480 if (dev_priv->mm.suspended) {
Keith Packard6dbe2772008-10-14 21:41:13 -07004481 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004482 return 0;
Keith Packard6dbe2772008-10-14 21:41:13 -07004483 }
Eric Anholt673a3942008-07-30 12:06:12 -07004484
Chris Wilson29105cc2010-01-07 10:39:13 +00004485 ret = i915_gpu_idle(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004486 if (ret) {
4487 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004488 return ret;
Keith Packard6dbe2772008-10-14 21:41:13 -07004489 }
Eric Anholt673a3942008-07-30 12:06:12 -07004490
Chris Wilson29105cc2010-01-07 10:39:13 +00004491 /* Under UMS, be paranoid and evict. */
4492 if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
Chris Wilsonb47eb4a2010-08-07 11:01:23 +01004493 ret = i915_gem_evict_inactive(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00004494 if (ret) {
4495 mutex_unlock(&dev->struct_mutex);
4496 return ret;
4497 }
4498 }
4499
4500 /* Hack! Don't let anybody do execbuf while we don't control the chip.
4501 * We need to replace this with a semaphore, or something.
4502 * And not confound mm.suspended!
4503 */
4504 dev_priv->mm.suspended = 1;
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02004505 del_timer_sync(&dev_priv->hangcheck_timer);
Chris Wilson29105cc2010-01-07 10:39:13 +00004506
4507 i915_kernel_lost_context(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004508 i915_gem_cleanup_ringbuffer(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00004509
Keith Packard6dbe2772008-10-14 21:41:13 -07004510 mutex_unlock(&dev->struct_mutex);
4511
Chris Wilson29105cc2010-01-07 10:39:13 +00004512 /* Cancel the retire work handler, which should be idle now. */
4513 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
4514
Eric Anholt673a3942008-07-30 12:06:12 -07004515 return 0;
4516}
4517
Jesse Barnese552eb72010-04-21 11:39:23 -07004518/*
4519 * 965+ support PIPE_CONTROL commands, which provide finer grained control
4520 * over cache flushing.
4521 */
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004522static int
Jesse Barnese552eb72010-04-21 11:39:23 -07004523i915_gem_init_pipe_control(struct drm_device *dev)
4524{
4525 drm_i915_private_t *dev_priv = dev->dev_private;
4526 struct drm_gem_object *obj;
4527 struct drm_i915_gem_object *obj_priv;
4528 int ret;
4529
Eric Anholt34dc4d42010-05-07 14:30:03 -07004530 obj = i915_gem_alloc_object(dev, 4096);
Jesse Barnese552eb72010-04-21 11:39:23 -07004531 if (obj == NULL) {
4532 DRM_ERROR("Failed to allocate seqno page\n");
4533 ret = -ENOMEM;
4534 goto err;
4535 }
4536 obj_priv = to_intel_bo(obj);
4537 obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
4538
Chris Wilsona00b10c2010-09-24 21:15:47 +01004539 ret = i915_gem_object_pin(obj, 4096, true, false);
Jesse Barnese552eb72010-04-21 11:39:23 -07004540 if (ret)
4541 goto err_unref;
4542
4543 dev_priv->seqno_gfx_addr = obj_priv->gtt_offset;
4544 dev_priv->seqno_page = kmap(obj_priv->pages[0]);
4545 if (dev_priv->seqno_page == NULL)
4546 goto err_unpin;
4547
4548 dev_priv->seqno_obj = obj;
4549 memset(dev_priv->seqno_page, 0, PAGE_SIZE);
4550
4551 return 0;
4552
4553err_unpin:
4554 i915_gem_object_unpin(obj);
4555err_unref:
4556 drm_gem_object_unreference(obj);
4557err:
4558 return ret;
4559}
4560
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004561
4562static void
Jesse Barnese552eb72010-04-21 11:39:23 -07004563i915_gem_cleanup_pipe_control(struct drm_device *dev)
4564{
4565 drm_i915_private_t *dev_priv = dev->dev_private;
4566 struct drm_gem_object *obj;
4567 struct drm_i915_gem_object *obj_priv;
4568
4569 obj = dev_priv->seqno_obj;
4570 obj_priv = to_intel_bo(obj);
4571 kunmap(obj_priv->pages[0]);
4572 i915_gem_object_unpin(obj);
4573 drm_gem_object_unreference(obj);
4574 dev_priv->seqno_obj = NULL;
4575
4576 dev_priv->seqno_page = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07004577}
4578
Eric Anholt673a3942008-07-30 12:06:12 -07004579int
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004580i915_gem_init_ringbuffer(struct drm_device *dev)
4581{
4582 drm_i915_private_t *dev_priv = dev->dev_private;
4583 int ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01004584
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004585 if (HAS_PIPE_CONTROL(dev)) {
4586 ret = i915_gem_init_pipe_control(dev);
4587 if (ret)
4588 return ret;
4589 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004590
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08004591 ret = intel_init_render_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004592 if (ret)
4593 goto cleanup_pipe_control;
4594
4595 if (HAS_BSD(dev)) {
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08004596 ret = intel_init_bsd_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004597 if (ret)
4598 goto cleanup_render_ring;
Zou Nan haid1b851f2010-05-21 09:08:57 +08004599 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004600
Chris Wilson549f7362010-10-19 11:19:32 +01004601 if (HAS_BLT(dev)) {
4602 ret = intel_init_blt_ring_buffer(dev);
4603 if (ret)
4604 goto cleanup_bsd_ring;
4605 }
4606
Chris Wilson6f392d5482010-08-07 11:01:22 +01004607 dev_priv->next_seqno = 1;
4608
Chris Wilson68f95ba2010-05-27 13:18:22 +01004609 return 0;
4610
Chris Wilson549f7362010-10-19 11:19:32 +01004611cleanup_bsd_ring:
Chris Wilson78501ea2010-10-27 12:18:21 +01004612 intel_cleanup_ring_buffer(&dev_priv->bsd_ring);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004613cleanup_render_ring:
Chris Wilson78501ea2010-10-27 12:18:21 +01004614 intel_cleanup_ring_buffer(&dev_priv->render_ring);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004615cleanup_pipe_control:
4616 if (HAS_PIPE_CONTROL(dev))
4617 i915_gem_cleanup_pipe_control(dev);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004618 return ret;
4619}
4620
4621void
4622i915_gem_cleanup_ringbuffer(struct drm_device *dev)
4623{
4624 drm_i915_private_t *dev_priv = dev->dev_private;
4625
Chris Wilson78501ea2010-10-27 12:18:21 +01004626 intel_cleanup_ring_buffer(&dev_priv->render_ring);
4627 intel_cleanup_ring_buffer(&dev_priv->bsd_ring);
4628 intel_cleanup_ring_buffer(&dev_priv->blt_ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004629 if (HAS_PIPE_CONTROL(dev))
4630 i915_gem_cleanup_pipe_control(dev);
4631}
4632
4633int
Eric Anholt673a3942008-07-30 12:06:12 -07004634i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
4635 struct drm_file *file_priv)
4636{
4637 drm_i915_private_t *dev_priv = dev->dev_private;
4638 int ret;
4639
Jesse Barnes79e53942008-11-07 14:24:08 -08004640 if (drm_core_check_feature(dev, DRIVER_MODESET))
4641 return 0;
4642
Ben Gamariba1234d2009-09-14 17:48:47 -04004643 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07004644 DRM_ERROR("Reenabling wedged hardware, good luck\n");
Ben Gamariba1234d2009-09-14 17:48:47 -04004645 atomic_set(&dev_priv->mm.wedged, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004646 }
4647
Eric Anholt673a3942008-07-30 12:06:12 -07004648 mutex_lock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004649 dev_priv->mm.suspended = 0;
4650
4651 ret = i915_gem_init_ringbuffer(dev);
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004652 if (ret != 0) {
4653 mutex_unlock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004654 return ret;
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004655 }
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004656
Chris Wilson69dc4982010-10-19 10:36:51 +01004657 BUG_ON(!list_empty(&dev_priv->mm.active_list));
Zou Nan hai852835f2010-05-21 09:08:56 +08004658 BUG_ON(!list_empty(&dev_priv->render_ring.active_list));
Chris Wilson87acb0a2010-10-19 10:13:00 +01004659 BUG_ON(!list_empty(&dev_priv->bsd_ring.active_list));
Chris Wilson549f7362010-10-19 11:19:32 +01004660 BUG_ON(!list_empty(&dev_priv->blt_ring.active_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004661 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
4662 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
Zou Nan hai852835f2010-05-21 09:08:56 +08004663 BUG_ON(!list_empty(&dev_priv->render_ring.request_list));
Chris Wilson87acb0a2010-10-19 10:13:00 +01004664 BUG_ON(!list_empty(&dev_priv->bsd_ring.request_list));
Chris Wilson549f7362010-10-19 11:19:32 +01004665 BUG_ON(!list_empty(&dev_priv->blt_ring.request_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004666 mutex_unlock(&dev->struct_mutex);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004667
Chris Wilson5f353082010-06-07 14:03:03 +01004668 ret = drm_irq_install(dev);
4669 if (ret)
4670 goto cleanup_ringbuffer;
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004671
Eric Anholt673a3942008-07-30 12:06:12 -07004672 return 0;
Chris Wilson5f353082010-06-07 14:03:03 +01004673
4674cleanup_ringbuffer:
4675 mutex_lock(&dev->struct_mutex);
4676 i915_gem_cleanup_ringbuffer(dev);
4677 dev_priv->mm.suspended = 1;
4678 mutex_unlock(&dev->struct_mutex);
4679
4680 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004681}
4682
4683int
4684i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
4685 struct drm_file *file_priv)
4686{
Jesse Barnes79e53942008-11-07 14:24:08 -08004687 if (drm_core_check_feature(dev, DRIVER_MODESET))
4688 return 0;
4689
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004690 drm_irq_uninstall(dev);
Linus Torvaldse6890f62009-09-08 17:09:24 -07004691 return i915_gem_idle(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004692}
4693
4694void
4695i915_gem_lastclose(struct drm_device *dev)
4696{
4697 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004698
Eric Anholte806b492009-01-22 09:56:58 -08004699 if (drm_core_check_feature(dev, DRIVER_MODESET))
4700 return;
4701
Keith Packard6dbe2772008-10-14 21:41:13 -07004702 ret = i915_gem_idle(dev);
4703 if (ret)
4704 DRM_ERROR("failed to idle hardware: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07004705}
4706
Chris Wilson64193402010-10-24 12:38:05 +01004707static void
4708init_ring_lists(struct intel_ring_buffer *ring)
4709{
4710 INIT_LIST_HEAD(&ring->active_list);
4711 INIT_LIST_HEAD(&ring->request_list);
4712 INIT_LIST_HEAD(&ring->gpu_write_list);
4713}
4714
Eric Anholt673a3942008-07-30 12:06:12 -07004715void
4716i915_gem_load(struct drm_device *dev)
4717{
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004718 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07004719 drm_i915_private_t *dev_priv = dev->dev_private;
4720
Chris Wilson69dc4982010-10-19 10:36:51 +01004721 INIT_LIST_HEAD(&dev_priv->mm.active_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004722 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
4723 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004724 INIT_LIST_HEAD(&dev_priv->mm.pinned_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07004725 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Chris Wilsonbe726152010-07-23 23:18:50 +01004726 INIT_LIST_HEAD(&dev_priv->mm.deferred_free_list);
Chris Wilson64193402010-10-24 12:38:05 +01004727 init_ring_lists(&dev_priv->render_ring);
4728 init_ring_lists(&dev_priv->bsd_ring);
4729 init_ring_lists(&dev_priv->blt_ring);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02004730 for (i = 0; i < 16; i++)
4731 INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004732 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
4733 i915_gem_retire_work_handler);
Chris Wilson30dbf0c2010-09-25 10:19:17 +01004734 init_completion(&dev_priv->error_completion);
Chris Wilson31169712009-09-14 16:50:28 +01004735
Dave Airlie94400122010-07-20 13:15:31 +10004736 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
4737 if (IS_GEN3(dev)) {
4738 u32 tmp = I915_READ(MI_ARB_STATE);
4739 if (!(tmp & MI_ARB_C3_LP_WRITE_ENABLE)) {
4740 /* arb state is a masked write, so set bit + bit in mask */
4741 tmp = MI_ARB_C3_LP_WRITE_ENABLE | (MI_ARB_C3_LP_WRITE_ENABLE << MI_ARB_MASK_SHIFT);
4742 I915_WRITE(MI_ARB_STATE, tmp);
4743 }
4744 }
4745
Jesse Barnesde151cf2008-11-12 10:03:55 -08004746 /* Old X drivers will take 0-2 for front, back, depth buffers */
Eric Anholtb397c832010-01-26 09:43:10 -08004747 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4748 dev_priv->fence_reg_start = 3;
Jesse Barnesde151cf2008-11-12 10:03:55 -08004749
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004750 if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08004751 dev_priv->num_fence_regs = 16;
4752 else
4753 dev_priv->num_fence_regs = 8;
4754
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004755 /* Initialize fence registers to zero */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004756 switch (INTEL_INFO(dev)->gen) {
4757 case 6:
4758 for (i = 0; i < 16; i++)
4759 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (i * 8), 0);
4760 break;
4761 case 5:
4762 case 4:
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004763 for (i = 0; i < 16; i++)
4764 I915_WRITE64(FENCE_REG_965_0 + (i * 8), 0);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004765 break;
4766 case 3:
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004767 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4768 for (i = 0; i < 8; i++)
4769 I915_WRITE(FENCE_REG_945_8 + (i * 4), 0);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004770 case 2:
4771 for (i = 0; i < 8; i++)
4772 I915_WRITE(FENCE_REG_830_0 + (i * 4), 0);
4773 break;
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004774 }
Eric Anholt673a3942008-07-30 12:06:12 -07004775 i915_gem_detect_bit_6_swizzle(dev);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05004776 init_waitqueue_head(&dev_priv->pending_flip_queue);
Chris Wilson17250b72010-10-28 12:51:39 +01004777
4778 dev_priv->mm.inactive_shrinker.shrink = i915_gem_inactive_shrink;
4779 dev_priv->mm.inactive_shrinker.seeks = DEFAULT_SEEKS;
4780 register_shrinker(&dev_priv->mm.inactive_shrinker);
Eric Anholt673a3942008-07-30 12:06:12 -07004781}
Dave Airlie71acb5e2008-12-30 20:31:46 +10004782
4783/*
4784 * Create a physically contiguous memory object for this object
4785 * e.g. for cursor + overlay regs
4786 */
Chris Wilson995b6762010-08-20 13:23:26 +01004787static int i915_gem_init_phys_object(struct drm_device *dev,
4788 int id, int size, int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004789{
4790 drm_i915_private_t *dev_priv = dev->dev_private;
4791 struct drm_i915_gem_phys_object *phys_obj;
4792 int ret;
4793
4794 if (dev_priv->mm.phys_objs[id - 1] || !size)
4795 return 0;
4796
Eric Anholt9a298b22009-03-24 12:23:04 -07004797 phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004798 if (!phys_obj)
4799 return -ENOMEM;
4800
4801 phys_obj->id = id;
4802
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004803 phys_obj->handle = drm_pci_alloc(dev, size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004804 if (!phys_obj->handle) {
4805 ret = -ENOMEM;
4806 goto kfree_obj;
4807 }
4808#ifdef CONFIG_X86
4809 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4810#endif
4811
4812 dev_priv->mm.phys_objs[id - 1] = phys_obj;
4813
4814 return 0;
4815kfree_obj:
Eric Anholt9a298b22009-03-24 12:23:04 -07004816 kfree(phys_obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004817 return ret;
4818}
4819
Chris Wilson995b6762010-08-20 13:23:26 +01004820static void i915_gem_free_phys_object(struct drm_device *dev, int id)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004821{
4822 drm_i915_private_t *dev_priv = dev->dev_private;
4823 struct drm_i915_gem_phys_object *phys_obj;
4824
4825 if (!dev_priv->mm.phys_objs[id - 1])
4826 return;
4827
4828 phys_obj = dev_priv->mm.phys_objs[id - 1];
4829 if (phys_obj->cur_obj) {
4830 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
4831 }
4832
4833#ifdef CONFIG_X86
4834 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4835#endif
4836 drm_pci_free(dev, phys_obj->handle);
4837 kfree(phys_obj);
4838 dev_priv->mm.phys_objs[id - 1] = NULL;
4839}
4840
4841void i915_gem_free_all_phys_object(struct drm_device *dev)
4842{
4843 int i;
4844
Dave Airlie260883c2009-01-22 17:58:49 +10004845 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004846 i915_gem_free_phys_object(dev, i);
4847}
4848
4849void i915_gem_detach_phys_object(struct drm_device *dev,
4850 struct drm_gem_object *obj)
4851{
Chris Wilsone5281cc2010-10-28 13:45:36 +01004852 struct address_space *mapping = obj->filp->f_path.dentry->d_inode->i_mapping;
4853 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
4854 char *vaddr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004855 int i;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004856 int page_count;
4857
Dave Airlie71acb5e2008-12-30 20:31:46 +10004858 if (!obj_priv->phys_obj)
4859 return;
Chris Wilsone5281cc2010-10-28 13:45:36 +01004860 vaddr = obj_priv->phys_obj->handle->vaddr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004861
4862 page_count = obj->size / PAGE_SIZE;
4863
4864 for (i = 0; i < page_count; i++) {
Chris Wilsone5281cc2010-10-28 13:45:36 +01004865 struct page *page = read_cache_page_gfp(mapping, i,
4866 GFP_HIGHUSER | __GFP_RECLAIMABLE);
4867 if (!IS_ERR(page)) {
4868 char *dst = kmap_atomic(page);
4869 memcpy(dst, vaddr + i*PAGE_SIZE, PAGE_SIZE);
4870 kunmap_atomic(dst);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004871
Chris Wilsone5281cc2010-10-28 13:45:36 +01004872 drm_clflush_pages(&page, 1);
4873
4874 set_page_dirty(page);
4875 mark_page_accessed(page);
4876 page_cache_release(page);
4877 }
Dave Airlie71acb5e2008-12-30 20:31:46 +10004878 }
Dave Airlie71acb5e2008-12-30 20:31:46 +10004879 drm_agp_chipset_flush(dev);
Chris Wilsond78b47b2009-06-17 21:52:49 +01004880
Dave Airlie71acb5e2008-12-30 20:31:46 +10004881 obj_priv->phys_obj->cur_obj = NULL;
4882 obj_priv->phys_obj = NULL;
4883}
4884
4885int
4886i915_gem_attach_phys_object(struct drm_device *dev,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004887 struct drm_gem_object *obj,
4888 int id,
4889 int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004890{
Chris Wilsone5281cc2010-10-28 13:45:36 +01004891 struct address_space *mapping = obj->filp->f_path.dentry->d_inode->i_mapping;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004892 drm_i915_private_t *dev_priv = dev->dev_private;
4893 struct drm_i915_gem_object *obj_priv;
4894 int ret = 0;
4895 int page_count;
4896 int i;
4897
4898 if (id > I915_MAX_PHYS_OBJECT)
4899 return -EINVAL;
4900
Daniel Vetter23010e42010-03-08 13:35:02 +01004901 obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004902
4903 if (obj_priv->phys_obj) {
4904 if (obj_priv->phys_obj->id == id)
4905 return 0;
4906 i915_gem_detach_phys_object(dev, obj);
4907 }
4908
Dave Airlie71acb5e2008-12-30 20:31:46 +10004909 /* create a new object */
4910 if (!dev_priv->mm.phys_objs[id - 1]) {
4911 ret = i915_gem_init_phys_object(dev, id,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004912 obj->size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004913 if (ret) {
Linus Torvaldsaeb565d2009-01-26 10:01:53 -08004914 DRM_ERROR("failed to init phys object %d size: %zu\n", id, obj->size);
Chris Wilsone5281cc2010-10-28 13:45:36 +01004915 return ret;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004916 }
4917 }
4918
4919 /* bind to the object */
4920 obj_priv->phys_obj = dev_priv->mm.phys_objs[id - 1];
4921 obj_priv->phys_obj->cur_obj = obj;
4922
Dave Airlie71acb5e2008-12-30 20:31:46 +10004923 page_count = obj->size / PAGE_SIZE;
4924
4925 for (i = 0; i < page_count; i++) {
Chris Wilsone5281cc2010-10-28 13:45:36 +01004926 struct page *page;
4927 char *dst, *src;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004928
Chris Wilsone5281cc2010-10-28 13:45:36 +01004929 page = read_cache_page_gfp(mapping, i,
4930 GFP_HIGHUSER | __GFP_RECLAIMABLE);
4931 if (IS_ERR(page))
4932 return PTR_ERR(page);
4933
4934 src = kmap_atomic(obj_priv->pages[i]);
4935 dst = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004936 memcpy(dst, src, PAGE_SIZE);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -07004937 kunmap_atomic(src);
Chris Wilsone5281cc2010-10-28 13:45:36 +01004938
4939 mark_page_accessed(page);
4940 page_cache_release(page);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004941 }
4942
4943 return 0;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004944}
4945
4946static int
4947i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
4948 struct drm_i915_gem_pwrite *args,
4949 struct drm_file *file_priv)
4950{
Daniel Vetter23010e42010-03-08 13:35:02 +01004951 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004952 void *obj_addr;
4953 int ret;
4954 char __user *user_data;
4955
4956 user_data = (char __user *) (uintptr_t) args->data_ptr;
4957 obj_addr = obj_priv->phys_obj->handle->vaddr + args->offset;
4958
Zhao Yakui44d98a62009-10-09 11:39:40 +08004959 DRM_DEBUG_DRIVER("obj_addr %p, %lld\n", obj_addr, args->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004960 ret = copy_from_user(obj_addr, user_data, args->size);
4961 if (ret)
4962 return -EFAULT;
4963
4964 drm_agp_chipset_flush(dev);
4965 return 0;
4966}
Eric Anholtb9624422009-06-03 07:27:35 +00004967
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004968void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00004969{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004970 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00004971
4972 /* Clean up our request list when the client is going away, so that
4973 * later retire_requests won't dereference our soon-to-be-gone
4974 * file_priv.
4975 */
Chris Wilson1c255952010-09-26 11:03:27 +01004976 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004977 while (!list_empty(&file_priv->mm.request_list)) {
4978 struct drm_i915_gem_request *request;
4979
4980 request = list_first_entry(&file_priv->mm.request_list,
4981 struct drm_i915_gem_request,
4982 client_list);
4983 list_del(&request->client_list);
4984 request->file_priv = NULL;
4985 }
Chris Wilson1c255952010-09-26 11:03:27 +01004986 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00004987}
Chris Wilson31169712009-09-14 16:50:28 +01004988
Chris Wilson31169712009-09-14 16:50:28 +01004989static int
Chris Wilson1637ef42010-04-20 17:10:35 +01004990i915_gpu_is_active(struct drm_device *dev)
4991{
4992 drm_i915_private_t *dev_priv = dev->dev_private;
4993 int lists_empty;
4994
Chris Wilson1637ef42010-04-20 17:10:35 +01004995 lists_empty = list_empty(&dev_priv->mm.flushing_list) &&
Chris Wilson17250b72010-10-28 12:51:39 +01004996 list_empty(&dev_priv->mm.active_list);
Chris Wilson1637ef42010-04-20 17:10:35 +01004997
4998 return !lists_empty;
4999}
5000
5001static int
Chris Wilson17250b72010-10-28 12:51:39 +01005002i915_gem_inactive_shrink(struct shrinker *shrinker,
5003 int nr_to_scan,
5004 gfp_t gfp_mask)
Chris Wilson31169712009-09-14 16:50:28 +01005005{
Chris Wilson17250b72010-10-28 12:51:39 +01005006 struct drm_i915_private *dev_priv =
5007 container_of(shrinker,
5008 struct drm_i915_private,
5009 mm.inactive_shrinker);
5010 struct drm_device *dev = dev_priv->dev;
5011 struct drm_i915_gem_object *obj, *next;
5012 int cnt;
5013
5014 if (!mutex_trylock(&dev->struct_mutex))
Chris Wilsonbbe2e112010-10-28 22:35:07 +01005015 return 0;
Chris Wilson31169712009-09-14 16:50:28 +01005016
5017 /* "fast-path" to count number of available objects */
5018 if (nr_to_scan == 0) {
Chris Wilson17250b72010-10-28 12:51:39 +01005019 cnt = 0;
5020 list_for_each_entry(obj,
5021 &dev_priv->mm.inactive_list,
5022 mm_list)
5023 cnt++;
5024 mutex_unlock(&dev->struct_mutex);
5025 return cnt / 100 * sysctl_vfs_cache_pressure;
Chris Wilson31169712009-09-14 16:50:28 +01005026 }
5027
Chris Wilson1637ef42010-04-20 17:10:35 +01005028rescan:
Chris Wilson31169712009-09-14 16:50:28 +01005029 /* first scan for clean buffers */
Chris Wilson17250b72010-10-28 12:51:39 +01005030 i915_gem_retire_requests(dev);
Chris Wilson31169712009-09-14 16:50:28 +01005031
Chris Wilson17250b72010-10-28 12:51:39 +01005032 list_for_each_entry_safe(obj, next,
5033 &dev_priv->mm.inactive_list,
5034 mm_list) {
5035 if (i915_gem_object_is_purgeable(obj)) {
5036 i915_gem_object_unbind(&obj->base);
5037 if (--nr_to_scan == 0)
5038 break;
Chris Wilson31169712009-09-14 16:50:28 +01005039 }
Chris Wilson31169712009-09-14 16:50:28 +01005040 }
5041
5042 /* second pass, evict/count anything still on the inactive list */
Chris Wilson17250b72010-10-28 12:51:39 +01005043 cnt = 0;
5044 list_for_each_entry_safe(obj, next,
5045 &dev_priv->mm.inactive_list,
5046 mm_list) {
5047 if (nr_to_scan) {
5048 i915_gem_object_unbind(&obj->base);
5049 nr_to_scan--;
5050 } else
5051 cnt++;
Chris Wilson31169712009-09-14 16:50:28 +01005052 }
5053
Chris Wilson17250b72010-10-28 12:51:39 +01005054 if (nr_to_scan && i915_gpu_is_active(dev)) {
Chris Wilson1637ef42010-04-20 17:10:35 +01005055 /*
5056 * We are desperate for pages, so as a last resort, wait
5057 * for the GPU to finish and discard whatever we can.
5058 * This has a dramatic impact to reduce the number of
5059 * OOM-killer events whilst running the GPU aggressively.
5060 */
Chris Wilson17250b72010-10-28 12:51:39 +01005061 if (i915_gpu_idle(dev) == 0)
Chris Wilson1637ef42010-04-20 17:10:35 +01005062 goto rescan;
5063 }
Chris Wilson17250b72010-10-28 12:51:39 +01005064 mutex_unlock(&dev->struct_mutex);
5065 return cnt / 100 * sysctl_vfs_cache_pressure;
Chris Wilson31169712009-09-14 16:50:28 +01005066}