blob: 7507a86c3feb4d8057fac1a0b602501e404aa522 [file] [log] [blame]
Eric Anholt673a3942008-07-30 12:06:12 -07001/*
2 * Copyright © 2008 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 *
26 */
27
28#include "drmP.h"
29#include "drm.h"
30#include "i915_drm.h"
31#include "i915_drv.h"
Chris Wilson1c5d22f2009-08-25 11:15:50 +010032#include "i915_trace.h"
Jesse Barnes652c3932009-08-17 13:31:43 -070033#include "intel_drv.h"
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Eric Anholt673a3942008-07-30 12:06:12 -070035#include <linux/swap.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080036#include <linux/pci.h>
Eric Anholt673a3942008-07-30 12:06:12 -070037
Chris Wilson0f8c6d72010-11-01 12:38:44 +000038struct change_domains {
39 uint32_t invalidate_domains;
40 uint32_t flush_domains;
41 uint32_t flush_rings;
42};
43
Chris Wilson05394f32010-11-08 19:18:58 +000044static int i915_gem_object_flush_gpu_write_domain(struct drm_i915_gem_object *obj,
Chris Wilson919926a2010-11-12 13:42:53 +000045 struct intel_ring_buffer *pipelined);
Chris Wilson05394f32010-11-08 19:18:58 +000046static void i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj);
47static void i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj);
48static int i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj,
Chris Wilson919926a2010-11-12 13:42:53 +000049 bool write);
Chris Wilson05394f32010-11-08 19:18:58 +000050static int i915_gem_object_set_cpu_read_domain_range(struct drm_i915_gem_object *obj,
Eric Anholte47c68e2008-11-14 13:35:19 -080051 uint64_t offset,
52 uint64_t size);
Chris Wilson05394f32010-11-08 19:18:58 +000053static void i915_gem_object_set_to_full_cpu_read_domain(struct drm_i915_gem_object *obj);
54static int i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj,
Chris Wilson2cf34d72010-09-14 13:03:28 +010055 bool interruptible);
Chris Wilson05394f32010-11-08 19:18:58 +000056static int i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
Chris Wilsona00b10c2010-09-24 21:15:47 +010057 unsigned alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +010058 bool map_and_fenceable);
Chris Wilson05394f32010-11-08 19:18:58 +000059static void i915_gem_clear_fence_reg(struct drm_i915_gem_object *obj);
60static int i915_gem_phys_pwrite(struct drm_device *dev,
61 struct drm_i915_gem_object *obj,
Dave Airlie71acb5e2008-12-30 20:31:46 +100062 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +000063 struct drm_file *file);
64static void i915_gem_free_object_tail(struct drm_i915_gem_object *obj);
Eric Anholt673a3942008-07-30 12:06:12 -070065
Chris Wilson17250b72010-10-28 12:51:39 +010066static int i915_gem_inactive_shrink(struct shrinker *shrinker,
67 int nr_to_scan,
68 gfp_t gfp_mask);
69
Chris Wilson31169712009-09-14 16:50:28 +010070
Chris Wilson73aa8082010-09-30 11:46:12 +010071/* some bookkeeping */
72static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
73 size_t size)
74{
75 dev_priv->mm.object_count++;
76 dev_priv->mm.object_memory += size;
77}
78
79static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
80 size_t size)
81{
82 dev_priv->mm.object_count--;
83 dev_priv->mm.object_memory -= size;
84}
85
Chris Wilson30dbf0c2010-09-25 10:19:17 +010086int
87i915_gem_check_is_wedged(struct drm_device *dev)
88{
89 struct drm_i915_private *dev_priv = dev->dev_private;
90 struct completion *x = &dev_priv->error_completion;
91 unsigned long flags;
92 int ret;
93
94 if (!atomic_read(&dev_priv->mm.wedged))
95 return 0;
96
97 ret = wait_for_completion_interruptible(x);
98 if (ret)
99 return ret;
100
101 /* Success, we reset the GPU! */
102 if (!atomic_read(&dev_priv->mm.wedged))
103 return 0;
104
105 /* GPU is hung, bump the completion count to account for
106 * the token we just consumed so that we never hit zero and
107 * end up waiting upon a subsequent completion event that
108 * will never happen.
109 */
110 spin_lock_irqsave(&x->wait.lock, flags);
111 x->done++;
112 spin_unlock_irqrestore(&x->wait.lock, flags);
113 return -EIO;
114}
115
Chris Wilson76c1dec2010-09-25 11:22:51 +0100116static int i915_mutex_lock_interruptible(struct drm_device *dev)
117{
118 struct drm_i915_private *dev_priv = dev->dev_private;
119 int ret;
120
121 ret = i915_gem_check_is_wedged(dev);
122 if (ret)
123 return ret;
124
125 ret = mutex_lock_interruptible(&dev->struct_mutex);
126 if (ret)
127 return ret;
128
129 if (atomic_read(&dev_priv->mm.wedged)) {
130 mutex_unlock(&dev->struct_mutex);
131 return -EAGAIN;
132 }
133
Chris Wilson23bc5982010-09-29 16:10:57 +0100134 WARN_ON(i915_verify_lists(dev));
Chris Wilson76c1dec2010-09-25 11:22:51 +0100135 return 0;
136}
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100137
Chris Wilson7d1c4802010-08-07 21:45:03 +0100138static inline bool
Chris Wilson05394f32010-11-08 19:18:58 +0000139i915_gem_object_is_inactive(struct drm_i915_gem_object *obj)
Chris Wilson7d1c4802010-08-07 21:45:03 +0100140{
Chris Wilson05394f32010-11-08 19:18:58 +0000141 return obj->gtt_space && !obj->active && obj->pin_count == 0;
Chris Wilson7d1c4802010-08-07 21:45:03 +0100142}
143
Chris Wilson20217462010-11-23 15:26:33 +0000144void i915_gem_do_init(struct drm_device *dev,
145 unsigned long start,
146 unsigned long mappable_end,
147 unsigned long end)
Jesse Barnes79e53942008-11-07 14:24:08 -0800148{
149 drm_i915_private_t *dev_priv = dev->dev_private;
150
Jesse Barnes79e53942008-11-07 14:24:08 -0800151 drm_mm_init(&dev_priv->mm.gtt_space, start,
152 end - start);
153
Chris Wilson73aa8082010-09-30 11:46:12 +0100154 dev_priv->mm.gtt_total = end - start;
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200155 dev_priv->mm.mappable_gtt_total = min(end, mappable_end) - start;
Daniel Vetter53984632010-09-22 23:44:24 +0200156 dev_priv->mm.gtt_mappable_end = mappable_end;
Jesse Barnes79e53942008-11-07 14:24:08 -0800157}
Keith Packard6dbe2772008-10-14 21:41:13 -0700158
Eric Anholt673a3942008-07-30 12:06:12 -0700159int
160i915_gem_init_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000161 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700162{
Eric Anholt673a3942008-07-30 12:06:12 -0700163 struct drm_i915_gem_init *args = data;
Chris Wilson20217462010-11-23 15:26:33 +0000164
165 if (args->gtt_start >= args->gtt_end ||
166 (args->gtt_end | args->gtt_start) & (PAGE_SIZE - 1))
167 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -0700168
169 mutex_lock(&dev->struct_mutex);
Chris Wilson20217462010-11-23 15:26:33 +0000170 i915_gem_do_init(dev, args->gtt_start, args->gtt_end, args->gtt_end);
Eric Anholt673a3942008-07-30 12:06:12 -0700171 mutex_unlock(&dev->struct_mutex);
172
Chris Wilson20217462010-11-23 15:26:33 +0000173 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700174}
175
Eric Anholt5a125c32008-10-22 21:40:13 -0700176int
177i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000178 struct drm_file *file)
Eric Anholt5a125c32008-10-22 21:40:13 -0700179{
Chris Wilson73aa8082010-09-30 11:46:12 +0100180 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt5a125c32008-10-22 21:40:13 -0700181 struct drm_i915_gem_get_aperture *args = data;
Chris Wilson6299f992010-11-24 12:23:44 +0000182 struct drm_i915_gem_object *obj;
183 size_t pinned;
Eric Anholt5a125c32008-10-22 21:40:13 -0700184
185 if (!(dev->driver->driver_features & DRIVER_GEM))
186 return -ENODEV;
187
Chris Wilson6299f992010-11-24 12:23:44 +0000188 pinned = 0;
Chris Wilson73aa8082010-09-30 11:46:12 +0100189 mutex_lock(&dev->struct_mutex);
Chris Wilson6299f992010-11-24 12:23:44 +0000190 list_for_each_entry(obj, &dev_priv->mm.pinned_list, mm_list)
191 pinned += obj->gtt_space->size;
Chris Wilson73aa8082010-09-30 11:46:12 +0100192 mutex_unlock(&dev->struct_mutex);
Eric Anholt5a125c32008-10-22 21:40:13 -0700193
Chris Wilson6299f992010-11-24 12:23:44 +0000194 args->aper_size = dev_priv->mm.gtt_total;
195 args->aper_available_size = args->aper_size -pinned;
196
Eric Anholt5a125c32008-10-22 21:40:13 -0700197 return 0;
198}
199
Eric Anholt673a3942008-07-30 12:06:12 -0700200/**
201 * Creates a new mm object and returns a handle to it.
202 */
203int
204i915_gem_create_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000205 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700206{
207 struct drm_i915_gem_create *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +0000208 struct drm_i915_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300209 int ret;
210 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700211
212 args->size = roundup(args->size, PAGE_SIZE);
213
214 /* Allocate the new object */
Daniel Vetterac52bc52010-04-09 19:05:06 +0000215 obj = i915_gem_alloc_object(dev, args->size);
Eric Anholt673a3942008-07-30 12:06:12 -0700216 if (obj == NULL)
217 return -ENOMEM;
218
Chris Wilson05394f32010-11-08 19:18:58 +0000219 ret = drm_gem_handle_create(file, &obj->base, &handle);
Chris Wilson1dfd9752010-09-06 14:44:14 +0100220 if (ret) {
Chris Wilson05394f32010-11-08 19:18:58 +0000221 drm_gem_object_release(&obj->base);
222 i915_gem_info_remove_obj(dev->dev_private, obj->base.size);
Chris Wilson202f2fe2010-10-14 13:20:40 +0100223 kfree(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700224 return ret;
Chris Wilson1dfd9752010-09-06 14:44:14 +0100225 }
226
Chris Wilson202f2fe2010-10-14 13:20:40 +0100227 /* drop reference from allocate - handle holds it now */
Chris Wilson05394f32010-11-08 19:18:58 +0000228 drm_gem_object_unreference(&obj->base);
Chris Wilson202f2fe2010-10-14 13:20:40 +0100229 trace_i915_gem_object_create(obj);
230
Eric Anholt673a3942008-07-30 12:06:12 -0700231 args->handle = handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700232 return 0;
233}
234
Chris Wilson05394f32010-11-08 19:18:58 +0000235static int i915_gem_object_needs_bit17_swizzle(struct drm_i915_gem_object *obj)
Eric Anholt280b7132009-03-12 16:56:27 -0700236{
Chris Wilson05394f32010-11-08 19:18:58 +0000237 drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
Eric Anholt280b7132009-03-12 16:56:27 -0700238
239 return dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_9_10_17 &&
Chris Wilson05394f32010-11-08 19:18:58 +0000240 obj->tiling_mode != I915_TILING_NONE;
Eric Anholt280b7132009-03-12 16:56:27 -0700241}
242
Chris Wilson99a03df2010-05-27 14:15:34 +0100243static inline void
Eric Anholt40123c12009-03-09 13:42:30 -0700244slow_shmem_copy(struct page *dst_page,
245 int dst_offset,
246 struct page *src_page,
247 int src_offset,
248 int length)
249{
250 char *dst_vaddr, *src_vaddr;
251
Chris Wilson99a03df2010-05-27 14:15:34 +0100252 dst_vaddr = kmap(dst_page);
253 src_vaddr = kmap(src_page);
Eric Anholt40123c12009-03-09 13:42:30 -0700254
255 memcpy(dst_vaddr + dst_offset, src_vaddr + src_offset, length);
256
Chris Wilson99a03df2010-05-27 14:15:34 +0100257 kunmap(src_page);
258 kunmap(dst_page);
Eric Anholt40123c12009-03-09 13:42:30 -0700259}
260
Chris Wilson99a03df2010-05-27 14:15:34 +0100261static inline void
Eric Anholt280b7132009-03-12 16:56:27 -0700262slow_shmem_bit17_copy(struct page *gpu_page,
263 int gpu_offset,
264 struct page *cpu_page,
265 int cpu_offset,
266 int length,
267 int is_read)
268{
269 char *gpu_vaddr, *cpu_vaddr;
270
271 /* Use the unswizzled path if this page isn't affected. */
272 if ((page_to_phys(gpu_page) & (1 << 17)) == 0) {
273 if (is_read)
274 return slow_shmem_copy(cpu_page, cpu_offset,
275 gpu_page, gpu_offset, length);
276 else
277 return slow_shmem_copy(gpu_page, gpu_offset,
278 cpu_page, cpu_offset, length);
279 }
280
Chris Wilson99a03df2010-05-27 14:15:34 +0100281 gpu_vaddr = kmap(gpu_page);
282 cpu_vaddr = kmap(cpu_page);
Eric Anholt280b7132009-03-12 16:56:27 -0700283
284 /* Copy the data, XORing A6 with A17 (1). The user already knows he's
285 * XORing with the other bits (A9 for Y, A9 and A10 for X)
286 */
287 while (length > 0) {
288 int cacheline_end = ALIGN(gpu_offset + 1, 64);
289 int this_length = min(cacheline_end - gpu_offset, length);
290 int swizzled_gpu_offset = gpu_offset ^ 64;
291
292 if (is_read) {
293 memcpy(cpu_vaddr + cpu_offset,
294 gpu_vaddr + swizzled_gpu_offset,
295 this_length);
296 } else {
297 memcpy(gpu_vaddr + swizzled_gpu_offset,
298 cpu_vaddr + cpu_offset,
299 this_length);
300 }
301 cpu_offset += this_length;
302 gpu_offset += this_length;
303 length -= this_length;
304 }
305
Chris Wilson99a03df2010-05-27 14:15:34 +0100306 kunmap(cpu_page);
307 kunmap(gpu_page);
Eric Anholt280b7132009-03-12 16:56:27 -0700308}
309
Eric Anholt673a3942008-07-30 12:06:12 -0700310/**
Eric Anholteb014592009-03-10 11:44:52 -0700311 * This is the fast shmem pread path, which attempts to copy_from_user directly
312 * from the backing pages of the object to the user's address space. On a
313 * fault, it fails so we can fall back to i915_gem_shmem_pwrite_slow().
314 */
315static int
Chris Wilson05394f32010-11-08 19:18:58 +0000316i915_gem_shmem_pread_fast(struct drm_device *dev,
317 struct drm_i915_gem_object *obj,
Eric Anholteb014592009-03-10 11:44:52 -0700318 struct drm_i915_gem_pread *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000319 struct drm_file *file)
Eric Anholteb014592009-03-10 11:44:52 -0700320{
Chris Wilson05394f32010-11-08 19:18:58 +0000321 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Eric Anholteb014592009-03-10 11:44:52 -0700322 ssize_t remain;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100323 loff_t offset;
Eric Anholteb014592009-03-10 11:44:52 -0700324 char __user *user_data;
325 int page_offset, page_length;
Eric Anholteb014592009-03-10 11:44:52 -0700326
327 user_data = (char __user *) (uintptr_t) args->data_ptr;
328 remain = args->size;
329
Eric Anholteb014592009-03-10 11:44:52 -0700330 offset = args->offset;
331
332 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100333 struct page *page;
334 char *vaddr;
335 int ret;
336
Eric Anholteb014592009-03-10 11:44:52 -0700337 /* Operation in this page
338 *
Eric Anholteb014592009-03-10 11:44:52 -0700339 * page_offset = offset within page
340 * page_length = bytes to copy for this page
341 */
Eric Anholteb014592009-03-10 11:44:52 -0700342 page_offset = offset & (PAGE_SIZE-1);
343 page_length = remain;
344 if ((page_offset + remain) > PAGE_SIZE)
345 page_length = PAGE_SIZE - page_offset;
346
Chris Wilsone5281cc2010-10-28 13:45:36 +0100347 page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
348 GFP_HIGHUSER | __GFP_RECLAIMABLE);
349 if (IS_ERR(page))
350 return PTR_ERR(page);
351
352 vaddr = kmap_atomic(page);
353 ret = __copy_to_user_inatomic(user_data,
354 vaddr + page_offset,
355 page_length);
356 kunmap_atomic(vaddr);
357
358 mark_page_accessed(page);
359 page_cache_release(page);
360 if (ret)
Chris Wilson4f27b752010-10-14 15:26:45 +0100361 return -EFAULT;
Eric Anholteb014592009-03-10 11:44:52 -0700362
363 remain -= page_length;
364 user_data += page_length;
365 offset += page_length;
366 }
367
Chris Wilson4f27b752010-10-14 15:26:45 +0100368 return 0;
Eric Anholteb014592009-03-10 11:44:52 -0700369}
370
371/**
372 * This is the fallback shmem pread path, which allocates temporary storage
373 * in kernel space to copy_to_user into outside of the struct_mutex, so we
374 * can copy out of the object's backing pages while holding the struct mutex
375 * and not take page faults.
376 */
377static int
Chris Wilson05394f32010-11-08 19:18:58 +0000378i915_gem_shmem_pread_slow(struct drm_device *dev,
379 struct drm_i915_gem_object *obj,
Eric Anholteb014592009-03-10 11:44:52 -0700380 struct drm_i915_gem_pread *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000381 struct drm_file *file)
Eric Anholteb014592009-03-10 11:44:52 -0700382{
Chris Wilson05394f32010-11-08 19:18:58 +0000383 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Eric Anholteb014592009-03-10 11:44:52 -0700384 struct mm_struct *mm = current->mm;
385 struct page **user_pages;
386 ssize_t remain;
387 loff_t offset, pinned_pages, i;
388 loff_t first_data_page, last_data_page, num_pages;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100389 int shmem_page_offset;
390 int data_page_index, data_page_offset;
Eric Anholteb014592009-03-10 11:44:52 -0700391 int page_length;
392 int ret;
393 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700394 int do_bit17_swizzling;
Eric Anholteb014592009-03-10 11:44:52 -0700395
396 remain = args->size;
397
398 /* Pin the user pages containing the data. We can't fault while
399 * holding the struct mutex, yet we want to hold it while
400 * dereferencing the user data.
401 */
402 first_data_page = data_ptr / PAGE_SIZE;
403 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
404 num_pages = last_data_page - first_data_page + 1;
405
Chris Wilson4f27b752010-10-14 15:26:45 +0100406 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
Eric Anholteb014592009-03-10 11:44:52 -0700407 if (user_pages == NULL)
408 return -ENOMEM;
409
Chris Wilson4f27b752010-10-14 15:26:45 +0100410 mutex_unlock(&dev->struct_mutex);
Eric Anholteb014592009-03-10 11:44:52 -0700411 down_read(&mm->mmap_sem);
412 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
Eric Anholte5e9ecd2009-04-07 16:01:22 -0700413 num_pages, 1, 0, user_pages, NULL);
Eric Anholteb014592009-03-10 11:44:52 -0700414 up_read(&mm->mmap_sem);
Chris Wilson4f27b752010-10-14 15:26:45 +0100415 mutex_lock(&dev->struct_mutex);
Eric Anholteb014592009-03-10 11:44:52 -0700416 if (pinned_pages < num_pages) {
417 ret = -EFAULT;
Chris Wilson4f27b752010-10-14 15:26:45 +0100418 goto out;
Eric Anholteb014592009-03-10 11:44:52 -0700419 }
420
Chris Wilson4f27b752010-10-14 15:26:45 +0100421 ret = i915_gem_object_set_cpu_read_domain_range(obj,
422 args->offset,
Eric Anholteb014592009-03-10 11:44:52 -0700423 args->size);
Chris Wilson4f27b752010-10-14 15:26:45 +0100424 if (ret)
425 goto out;
426
427 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700428
Eric Anholteb014592009-03-10 11:44:52 -0700429 offset = args->offset;
430
431 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100432 struct page *page;
433
Eric Anholteb014592009-03-10 11:44:52 -0700434 /* Operation in this page
435 *
Eric Anholteb014592009-03-10 11:44:52 -0700436 * shmem_page_offset = offset within page in shmem file
437 * data_page_index = page number in get_user_pages return
438 * data_page_offset = offset with data_page_index page.
439 * page_length = bytes to copy for this page
440 */
Eric Anholteb014592009-03-10 11:44:52 -0700441 shmem_page_offset = offset & ~PAGE_MASK;
442 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
443 data_page_offset = data_ptr & ~PAGE_MASK;
444
445 page_length = remain;
446 if ((shmem_page_offset + page_length) > PAGE_SIZE)
447 page_length = PAGE_SIZE - shmem_page_offset;
448 if ((data_page_offset + page_length) > PAGE_SIZE)
449 page_length = PAGE_SIZE - data_page_offset;
450
Chris Wilsone5281cc2010-10-28 13:45:36 +0100451 page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
452 GFP_HIGHUSER | __GFP_RECLAIMABLE);
453 if (IS_ERR(page))
454 return PTR_ERR(page);
455
Eric Anholt280b7132009-03-12 16:56:27 -0700456 if (do_bit17_swizzling) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100457 slow_shmem_bit17_copy(page,
Eric Anholt280b7132009-03-12 16:56:27 -0700458 shmem_page_offset,
Chris Wilson99a03df2010-05-27 14:15:34 +0100459 user_pages[data_page_index],
460 data_page_offset,
461 page_length,
462 1);
463 } else {
464 slow_shmem_copy(user_pages[data_page_index],
465 data_page_offset,
Chris Wilsone5281cc2010-10-28 13:45:36 +0100466 page,
Chris Wilson99a03df2010-05-27 14:15:34 +0100467 shmem_page_offset,
468 page_length);
Eric Anholt280b7132009-03-12 16:56:27 -0700469 }
Eric Anholteb014592009-03-10 11:44:52 -0700470
Chris Wilsone5281cc2010-10-28 13:45:36 +0100471 mark_page_accessed(page);
472 page_cache_release(page);
473
Eric Anholteb014592009-03-10 11:44:52 -0700474 remain -= page_length;
475 data_ptr += page_length;
476 offset += page_length;
477 }
478
Chris Wilson4f27b752010-10-14 15:26:45 +0100479out:
Eric Anholteb014592009-03-10 11:44:52 -0700480 for (i = 0; i < pinned_pages; i++) {
481 SetPageDirty(user_pages[i]);
Chris Wilsone5281cc2010-10-28 13:45:36 +0100482 mark_page_accessed(user_pages[i]);
Eric Anholteb014592009-03-10 11:44:52 -0700483 page_cache_release(user_pages[i]);
484 }
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700485 drm_free_large(user_pages);
Eric Anholteb014592009-03-10 11:44:52 -0700486
487 return ret;
488}
489
Eric Anholt673a3942008-07-30 12:06:12 -0700490/**
491 * Reads data from the object referenced by handle.
492 *
493 * On error, the contents of *data are undefined.
494 */
495int
496i915_gem_pread_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000497 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700498{
499 struct drm_i915_gem_pread *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +0000500 struct drm_i915_gem_object *obj;
Chris Wilson35b62a82010-09-26 20:23:38 +0100501 int ret = 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700502
Chris Wilson51311d02010-11-17 09:10:42 +0000503 if (args->size == 0)
504 return 0;
505
506 if (!access_ok(VERIFY_WRITE,
507 (char __user *)(uintptr_t)args->data_ptr,
508 args->size))
509 return -EFAULT;
510
511 ret = fault_in_pages_writeable((char __user *)(uintptr_t)args->data_ptr,
512 args->size);
513 if (ret)
514 return -EFAULT;
515
Chris Wilson4f27b752010-10-14 15:26:45 +0100516 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100517 if (ret)
Chris Wilson4f27b752010-10-14 15:26:45 +0100518 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700519
Chris Wilson05394f32010-11-08 19:18:58 +0000520 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100521 if (obj == NULL) {
522 ret = -ENOENT;
523 goto unlock;
Chris Wilson4f27b752010-10-14 15:26:45 +0100524 }
Eric Anholt673a3942008-07-30 12:06:12 -0700525
Chris Wilson7dcd2492010-09-26 20:21:44 +0100526 /* Bounds check source. */
Chris Wilson05394f32010-11-08 19:18:58 +0000527 if (args->offset > obj->base.size ||
528 args->size > obj->base.size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +0100529 ret = -EINVAL;
Chris Wilson35b62a82010-09-26 20:23:38 +0100530 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +0100531 }
532
Chris Wilson4f27b752010-10-14 15:26:45 +0100533 ret = i915_gem_object_set_cpu_read_domain_range(obj,
534 args->offset,
535 args->size);
536 if (ret)
Chris Wilsone5281cc2010-10-28 13:45:36 +0100537 goto out;
Chris Wilson4f27b752010-10-14 15:26:45 +0100538
539 ret = -EFAULT;
540 if (!i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilson05394f32010-11-08 19:18:58 +0000541 ret = i915_gem_shmem_pread_fast(dev, obj, args, file);
Chris Wilson4f27b752010-10-14 15:26:45 +0100542 if (ret == -EFAULT)
Chris Wilson05394f32010-11-08 19:18:58 +0000543 ret = i915_gem_shmem_pread_slow(dev, obj, args, file);
Eric Anholt673a3942008-07-30 12:06:12 -0700544
Chris Wilson35b62a82010-09-26 20:23:38 +0100545out:
Chris Wilson05394f32010-11-08 19:18:58 +0000546 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100547unlock:
Chris Wilson4f27b752010-10-14 15:26:45 +0100548 mutex_unlock(&dev->struct_mutex);
Eric Anholteb014592009-03-10 11:44:52 -0700549 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700550}
551
Keith Packard0839ccb2008-10-30 19:38:48 -0700552/* This is the fast write path which cannot handle
553 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700554 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700555
Keith Packard0839ccb2008-10-30 19:38:48 -0700556static inline int
557fast_user_write(struct io_mapping *mapping,
558 loff_t page_base, int page_offset,
559 char __user *user_data,
560 int length)
561{
562 char *vaddr_atomic;
563 unsigned long unwritten;
564
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700565 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
Keith Packard0839ccb2008-10-30 19:38:48 -0700566 unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
567 user_data, length);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700568 io_mapping_unmap_atomic(vaddr_atomic);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100569 return unwritten;
Keith Packard0839ccb2008-10-30 19:38:48 -0700570}
571
572/* Here's the write path which can sleep for
573 * page faults
574 */
575
Chris Wilsonab34c222010-05-27 14:15:35 +0100576static inline void
Eric Anholt3de09aa2009-03-09 09:42:23 -0700577slow_kernel_write(struct io_mapping *mapping,
578 loff_t gtt_base, int gtt_offset,
579 struct page *user_page, int user_offset,
580 int length)
Keith Packard0839ccb2008-10-30 19:38:48 -0700581{
Chris Wilsonab34c222010-05-27 14:15:35 +0100582 char __iomem *dst_vaddr;
583 char *src_vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -0700584
Chris Wilsonab34c222010-05-27 14:15:35 +0100585 dst_vaddr = io_mapping_map_wc(mapping, gtt_base);
586 src_vaddr = kmap(user_page);
587
588 memcpy_toio(dst_vaddr + gtt_offset,
589 src_vaddr + user_offset,
590 length);
591
592 kunmap(user_page);
593 io_mapping_unmap(dst_vaddr);
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700594}
595
Eric Anholt3de09aa2009-03-09 09:42:23 -0700596/**
597 * This is the fast pwrite path, where we copy the data directly from the
598 * user into the GTT, uncached.
599 */
Eric Anholt673a3942008-07-30 12:06:12 -0700600static int
Chris Wilson05394f32010-11-08 19:18:58 +0000601i915_gem_gtt_pwrite_fast(struct drm_device *dev,
602 struct drm_i915_gem_object *obj,
Eric Anholt3de09aa2009-03-09 09:42:23 -0700603 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000604 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700605{
Keith Packard0839ccb2008-10-30 19:38:48 -0700606 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700607 ssize_t remain;
Keith Packard0839ccb2008-10-30 19:38:48 -0700608 loff_t offset, page_base;
Eric Anholt673a3942008-07-30 12:06:12 -0700609 char __user *user_data;
Keith Packard0839ccb2008-10-30 19:38:48 -0700610 int page_offset, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700611
612 user_data = (char __user *) (uintptr_t) args->data_ptr;
613 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700614
Chris Wilson05394f32010-11-08 19:18:58 +0000615 offset = obj->gtt_offset + args->offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700616
617 while (remain > 0) {
618 /* Operation in this page
619 *
Keith Packard0839ccb2008-10-30 19:38:48 -0700620 * page_base = page offset within aperture
621 * page_offset = offset within page
622 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -0700623 */
Keith Packard0839ccb2008-10-30 19:38:48 -0700624 page_base = (offset & ~(PAGE_SIZE-1));
625 page_offset = offset & (PAGE_SIZE-1);
626 page_length = remain;
627 if ((page_offset + remain) > PAGE_SIZE)
628 page_length = PAGE_SIZE - page_offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700629
Keith Packard0839ccb2008-10-30 19:38:48 -0700630 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -0700631 * source page isn't available. Return the error and we'll
632 * retry in the slow path.
Keith Packard0839ccb2008-10-30 19:38:48 -0700633 */
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100634 if (fast_user_write(dev_priv->mm.gtt_mapping, page_base,
635 page_offset, user_data, page_length))
636
637 return -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -0700638
Keith Packard0839ccb2008-10-30 19:38:48 -0700639 remain -= page_length;
640 user_data += page_length;
641 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700642 }
Eric Anholt673a3942008-07-30 12:06:12 -0700643
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100644 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700645}
646
Eric Anholt3de09aa2009-03-09 09:42:23 -0700647/**
648 * This is the fallback GTT pwrite path, which uses get_user_pages to pin
649 * the memory and maps it using kmap_atomic for copying.
650 *
651 * This code resulted in x11perf -rgb10text consuming about 10% more CPU
652 * than using i915_gem_gtt_pwrite_fast on a G45 (32-bit).
653 */
Eric Anholt3043c602008-10-02 12:24:47 -0700654static int
Chris Wilson05394f32010-11-08 19:18:58 +0000655i915_gem_gtt_pwrite_slow(struct drm_device *dev,
656 struct drm_i915_gem_object *obj,
Eric Anholt3de09aa2009-03-09 09:42:23 -0700657 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000658 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700659{
Eric Anholt3de09aa2009-03-09 09:42:23 -0700660 drm_i915_private_t *dev_priv = dev->dev_private;
661 ssize_t remain;
662 loff_t gtt_page_base, offset;
663 loff_t first_data_page, last_data_page, num_pages;
664 loff_t pinned_pages, i;
665 struct page **user_pages;
666 struct mm_struct *mm = current->mm;
667 int gtt_page_offset, data_page_offset, data_page_index, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700668 int ret;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700669 uint64_t data_ptr = args->data_ptr;
670
671 remain = args->size;
672
673 /* Pin the user pages containing the data. We can't fault while
674 * holding the struct mutex, and all of the pwrite implementations
675 * want to hold it while dereferencing the user data.
676 */
677 first_data_page = data_ptr / PAGE_SIZE;
678 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
679 num_pages = last_data_page - first_data_page + 1;
680
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100681 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
Eric Anholt3de09aa2009-03-09 09:42:23 -0700682 if (user_pages == NULL)
683 return -ENOMEM;
684
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100685 mutex_unlock(&dev->struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700686 down_read(&mm->mmap_sem);
687 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
688 num_pages, 0, 0, user_pages, NULL);
689 up_read(&mm->mmap_sem);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100690 mutex_lock(&dev->struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700691 if (pinned_pages < num_pages) {
692 ret = -EFAULT;
693 goto out_unpin_pages;
694 }
695
Eric Anholt3de09aa2009-03-09 09:42:23 -0700696 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
697 if (ret)
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100698 goto out_unpin_pages;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700699
Chris Wilson05394f32010-11-08 19:18:58 +0000700 offset = obj->gtt_offset + args->offset;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700701
702 while (remain > 0) {
703 /* Operation in this page
704 *
705 * gtt_page_base = page offset within aperture
706 * gtt_page_offset = offset within page in aperture
707 * data_page_index = page number in get_user_pages return
708 * data_page_offset = offset with data_page_index page.
709 * page_length = bytes to copy for this page
710 */
711 gtt_page_base = offset & PAGE_MASK;
712 gtt_page_offset = offset & ~PAGE_MASK;
713 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
714 data_page_offset = data_ptr & ~PAGE_MASK;
715
716 page_length = remain;
717 if ((gtt_page_offset + page_length) > PAGE_SIZE)
718 page_length = PAGE_SIZE - gtt_page_offset;
719 if ((data_page_offset + page_length) > PAGE_SIZE)
720 page_length = PAGE_SIZE - data_page_offset;
721
Chris Wilsonab34c222010-05-27 14:15:35 +0100722 slow_kernel_write(dev_priv->mm.gtt_mapping,
723 gtt_page_base, gtt_page_offset,
724 user_pages[data_page_index],
725 data_page_offset,
726 page_length);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700727
728 remain -= page_length;
729 offset += page_length;
730 data_ptr += page_length;
731 }
732
Eric Anholt3de09aa2009-03-09 09:42:23 -0700733out_unpin_pages:
734 for (i = 0; i < pinned_pages; i++)
735 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700736 drm_free_large(user_pages);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700737
738 return ret;
739}
740
Eric Anholt40123c12009-03-09 13:42:30 -0700741/**
742 * This is the fast shmem pwrite path, which attempts to directly
743 * copy_from_user into the kmapped pages backing the object.
744 */
Eric Anholt673a3942008-07-30 12:06:12 -0700745static int
Chris Wilson05394f32010-11-08 19:18:58 +0000746i915_gem_shmem_pwrite_fast(struct drm_device *dev,
747 struct drm_i915_gem_object *obj,
Eric Anholt40123c12009-03-09 13:42:30 -0700748 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000749 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700750{
Chris Wilson05394f32010-11-08 19:18:58 +0000751 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Eric Anholt40123c12009-03-09 13:42:30 -0700752 ssize_t remain;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100753 loff_t offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700754 char __user *user_data;
755 int page_offset, page_length;
Eric Anholt40123c12009-03-09 13:42:30 -0700756
757 user_data = (char __user *) (uintptr_t) args->data_ptr;
758 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700759
Eric Anholt673a3942008-07-30 12:06:12 -0700760 offset = args->offset;
Chris Wilson05394f32010-11-08 19:18:58 +0000761 obj->dirty = 1;
Eric Anholt673a3942008-07-30 12:06:12 -0700762
Eric Anholt40123c12009-03-09 13:42:30 -0700763 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100764 struct page *page;
765 char *vaddr;
766 int ret;
767
Eric Anholt40123c12009-03-09 13:42:30 -0700768 /* Operation in this page
769 *
Eric Anholt40123c12009-03-09 13:42:30 -0700770 * page_offset = offset within page
771 * page_length = bytes to copy for this page
772 */
Eric Anholt40123c12009-03-09 13:42:30 -0700773 page_offset = offset & (PAGE_SIZE-1);
774 page_length = remain;
775 if ((page_offset + remain) > PAGE_SIZE)
776 page_length = PAGE_SIZE - page_offset;
777
Chris Wilsone5281cc2010-10-28 13:45:36 +0100778 page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
779 GFP_HIGHUSER | __GFP_RECLAIMABLE);
780 if (IS_ERR(page))
781 return PTR_ERR(page);
782
783 vaddr = kmap_atomic(page, KM_USER0);
784 ret = __copy_from_user_inatomic(vaddr + page_offset,
785 user_data,
786 page_length);
787 kunmap_atomic(vaddr, KM_USER0);
788
789 set_page_dirty(page);
790 mark_page_accessed(page);
791 page_cache_release(page);
792
793 /* If we get a fault while copying data, then (presumably) our
794 * source page isn't available. Return the error and we'll
795 * retry in the slow path.
796 */
797 if (ret)
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100798 return -EFAULT;
Eric Anholt40123c12009-03-09 13:42:30 -0700799
800 remain -= page_length;
801 user_data += page_length;
802 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700803 }
804
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100805 return 0;
Eric Anholt40123c12009-03-09 13:42:30 -0700806}
807
808/**
809 * This is the fallback shmem pwrite path, which uses get_user_pages to pin
810 * the memory and maps it using kmap_atomic for copying.
811 *
812 * This avoids taking mmap_sem for faulting on the user's address while the
813 * struct_mutex is held.
814 */
815static int
Chris Wilson05394f32010-11-08 19:18:58 +0000816i915_gem_shmem_pwrite_slow(struct drm_device *dev,
817 struct drm_i915_gem_object *obj,
Eric Anholt40123c12009-03-09 13:42:30 -0700818 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000819 struct drm_file *file)
Eric Anholt40123c12009-03-09 13:42:30 -0700820{
Chris Wilson05394f32010-11-08 19:18:58 +0000821 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Eric Anholt40123c12009-03-09 13:42:30 -0700822 struct mm_struct *mm = current->mm;
823 struct page **user_pages;
824 ssize_t remain;
825 loff_t offset, pinned_pages, i;
826 loff_t first_data_page, last_data_page, num_pages;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100827 int shmem_page_offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700828 int data_page_index, data_page_offset;
829 int page_length;
830 int ret;
831 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700832 int do_bit17_swizzling;
Eric Anholt40123c12009-03-09 13:42:30 -0700833
834 remain = args->size;
835
836 /* Pin the user pages containing the data. We can't fault while
837 * holding the struct mutex, and all of the pwrite implementations
838 * want to hold it while dereferencing the user data.
839 */
840 first_data_page = data_ptr / PAGE_SIZE;
841 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
842 num_pages = last_data_page - first_data_page + 1;
843
Chris Wilson4f27b752010-10-14 15:26:45 +0100844 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
Eric Anholt40123c12009-03-09 13:42:30 -0700845 if (user_pages == NULL)
846 return -ENOMEM;
847
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100848 mutex_unlock(&dev->struct_mutex);
Eric Anholt40123c12009-03-09 13:42:30 -0700849 down_read(&mm->mmap_sem);
850 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
851 num_pages, 0, 0, user_pages, NULL);
852 up_read(&mm->mmap_sem);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100853 mutex_lock(&dev->struct_mutex);
Eric Anholt40123c12009-03-09 13:42:30 -0700854 if (pinned_pages < num_pages) {
855 ret = -EFAULT;
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100856 goto out;
Eric Anholt40123c12009-03-09 13:42:30 -0700857 }
858
Eric Anholt40123c12009-03-09 13:42:30 -0700859 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100860 if (ret)
861 goto out;
862
863 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700864
Eric Anholt40123c12009-03-09 13:42:30 -0700865 offset = args->offset;
Chris Wilson05394f32010-11-08 19:18:58 +0000866 obj->dirty = 1;
Eric Anholt40123c12009-03-09 13:42:30 -0700867
868 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100869 struct page *page;
870
Eric Anholt40123c12009-03-09 13:42:30 -0700871 /* Operation in this page
872 *
Eric Anholt40123c12009-03-09 13:42:30 -0700873 * shmem_page_offset = offset within page in shmem file
874 * data_page_index = page number in get_user_pages return
875 * data_page_offset = offset with data_page_index page.
876 * page_length = bytes to copy for this page
877 */
Eric Anholt40123c12009-03-09 13:42:30 -0700878 shmem_page_offset = offset & ~PAGE_MASK;
879 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
880 data_page_offset = data_ptr & ~PAGE_MASK;
881
882 page_length = remain;
883 if ((shmem_page_offset + page_length) > PAGE_SIZE)
884 page_length = PAGE_SIZE - shmem_page_offset;
885 if ((data_page_offset + page_length) > PAGE_SIZE)
886 page_length = PAGE_SIZE - data_page_offset;
887
Chris Wilsone5281cc2010-10-28 13:45:36 +0100888 page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
889 GFP_HIGHUSER | __GFP_RECLAIMABLE);
890 if (IS_ERR(page)) {
891 ret = PTR_ERR(page);
892 goto out;
893 }
894
Eric Anholt280b7132009-03-12 16:56:27 -0700895 if (do_bit17_swizzling) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100896 slow_shmem_bit17_copy(page,
Eric Anholt280b7132009-03-12 16:56:27 -0700897 shmem_page_offset,
898 user_pages[data_page_index],
899 data_page_offset,
Chris Wilson99a03df2010-05-27 14:15:34 +0100900 page_length,
901 0);
902 } else {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100903 slow_shmem_copy(page,
Chris Wilson99a03df2010-05-27 14:15:34 +0100904 shmem_page_offset,
905 user_pages[data_page_index],
906 data_page_offset,
907 page_length);
Eric Anholt280b7132009-03-12 16:56:27 -0700908 }
Eric Anholt40123c12009-03-09 13:42:30 -0700909
Chris Wilsone5281cc2010-10-28 13:45:36 +0100910 set_page_dirty(page);
911 mark_page_accessed(page);
912 page_cache_release(page);
913
Eric Anholt40123c12009-03-09 13:42:30 -0700914 remain -= page_length;
915 data_ptr += page_length;
916 offset += page_length;
917 }
918
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100919out:
Eric Anholt40123c12009-03-09 13:42:30 -0700920 for (i = 0; i < pinned_pages; i++)
921 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700922 drm_free_large(user_pages);
Eric Anholt40123c12009-03-09 13:42:30 -0700923
924 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700925}
926
927/**
928 * Writes data to the object referenced by handle.
929 *
930 * On error, the contents of the buffer that were to be modified are undefined.
931 */
932int
933i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100934 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700935{
936 struct drm_i915_gem_pwrite *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +0000937 struct drm_i915_gem_object *obj;
Chris Wilson51311d02010-11-17 09:10:42 +0000938 int ret;
939
940 if (args->size == 0)
941 return 0;
942
943 if (!access_ok(VERIFY_READ,
944 (char __user *)(uintptr_t)args->data_ptr,
945 args->size))
946 return -EFAULT;
947
948 ret = fault_in_pages_readable((char __user *)(uintptr_t)args->data_ptr,
949 args->size);
950 if (ret)
951 return -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -0700952
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100953 ret = i915_mutex_lock_interruptible(dev);
954 if (ret)
955 return ret;
956
Chris Wilson05394f32010-11-08 19:18:58 +0000957 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100958 if (obj == NULL) {
959 ret = -ENOENT;
960 goto unlock;
961 }
Eric Anholt673a3942008-07-30 12:06:12 -0700962
Chris Wilson7dcd2492010-09-26 20:21:44 +0100963 /* Bounds check destination. */
Chris Wilson05394f32010-11-08 19:18:58 +0000964 if (args->offset > obj->base.size ||
965 args->size > obj->base.size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +0100966 ret = -EINVAL;
Chris Wilson35b62a82010-09-26 20:23:38 +0100967 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +0100968 }
969
Eric Anholt673a3942008-07-30 12:06:12 -0700970 /* We can only do the GTT pwrite on untiled buffers, as otherwise
971 * it would end up going through the fenced access, and we'll get
972 * different detiling behavior between reading and writing.
973 * pread/pwrite currently are reading and writing from the CPU
974 * perspective, requiring manual detiling by the client.
975 */
Chris Wilson05394f32010-11-08 19:18:58 +0000976 if (obj->phys_obj)
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100977 ret = i915_gem_phys_pwrite(dev, obj, args, file);
Chris Wilson05394f32010-11-08 19:18:58 +0000978 else if (obj->tiling_mode == I915_TILING_NONE &&
979 obj->gtt_space &&
980 obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
Daniel Vetter75e9e912010-11-04 17:11:09 +0100981 ret = i915_gem_object_pin(obj, 0, true);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100982 if (ret)
983 goto out;
984
985 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
986 if (ret)
987 goto out_unpin;
988
989 ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file);
990 if (ret == -EFAULT)
991 ret = i915_gem_gtt_pwrite_slow(dev, obj, args, file);
992
993out_unpin:
994 i915_gem_object_unpin(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700995 } else {
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100996 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
997 if (ret)
Chris Wilsone5281cc2010-10-28 13:45:36 +0100998 goto out;
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100999
1000 ret = -EFAULT;
1001 if (!i915_gem_object_needs_bit17_swizzle(obj))
1002 ret = i915_gem_shmem_pwrite_fast(dev, obj, args, file);
1003 if (ret == -EFAULT)
1004 ret = i915_gem_shmem_pwrite_slow(dev, obj, args, file);
Eric Anholt40123c12009-03-09 13:42:30 -07001005 }
Eric Anholt673a3942008-07-30 12:06:12 -07001006
Chris Wilson35b62a82010-09-26 20:23:38 +01001007out:
Chris Wilson05394f32010-11-08 19:18:58 +00001008 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001009unlock:
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001010 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07001011 return ret;
1012}
1013
1014/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001015 * Called when user space prepares to use an object with the CPU, either
1016 * through the mmap ioctl's mapping or a GTT mapping.
Eric Anholt673a3942008-07-30 12:06:12 -07001017 */
1018int
1019i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001020 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001021{
Eric Anholta09ba7f2009-08-29 12:49:51 -07001022 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07001023 struct drm_i915_gem_set_domain *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001024 struct drm_i915_gem_object *obj;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001025 uint32_t read_domains = args->read_domains;
1026 uint32_t write_domain = args->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07001027 int ret;
1028
1029 if (!(dev->driver->driver_features & DRIVER_GEM))
1030 return -ENODEV;
1031
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001032 /* Only handle setting domains to types used by the CPU. */
Chris Wilson21d509e2009-06-06 09:46:02 +01001033 if (write_domain & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001034 return -EINVAL;
1035
Chris Wilson21d509e2009-06-06 09:46:02 +01001036 if (read_domains & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001037 return -EINVAL;
1038
1039 /* Having something in the write domain implies it's in the read
1040 * domain, and only that read domain. Enforce that in the request.
1041 */
1042 if (write_domain != 0 && read_domains != write_domain)
1043 return -EINVAL;
1044
Chris Wilson76c1dec2010-09-25 11:22:51 +01001045 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001046 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001047 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001048
Chris Wilson05394f32010-11-08 19:18:58 +00001049 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001050 if (obj == NULL) {
1051 ret = -ENOENT;
1052 goto unlock;
Chris Wilson76c1dec2010-09-25 11:22:51 +01001053 }
Jesse Barnes652c3932009-08-17 13:31:43 -07001054
1055 intel_mark_busy(dev, obj);
1056
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001057 if (read_domains & I915_GEM_DOMAIN_GTT) {
1058 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
Eric Anholt02354392008-11-26 13:58:13 -08001059
Eric Anholta09ba7f2009-08-29 12:49:51 -07001060 /* Update the LRU on the fence for the CPU access that's
1061 * about to occur.
1062 */
Chris Wilson05394f32010-11-08 19:18:58 +00001063 if (obj->fence_reg != I915_FENCE_REG_NONE) {
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001064 struct drm_i915_fence_reg *reg =
Chris Wilson05394f32010-11-08 19:18:58 +00001065 &dev_priv->fence_regs[obj->fence_reg];
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001066 list_move_tail(&reg->lru_list,
Eric Anholta09ba7f2009-08-29 12:49:51 -07001067 &dev_priv->mm.fence_list);
1068 }
1069
Eric Anholt02354392008-11-26 13:58:13 -08001070 /* Silently promote "you're not bound, there was nothing to do"
1071 * to success, since the client was just asking us to
1072 * make sure everything was done.
1073 */
1074 if (ret == -EINVAL)
1075 ret = 0;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001076 } else {
Eric Anholte47c68e2008-11-14 13:35:19 -08001077 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001078 }
1079
Chris Wilson7d1c4802010-08-07 21:45:03 +01001080 /* Maintain LRU order of "inactive" objects */
Chris Wilson05394f32010-11-08 19:18:58 +00001081 if (ret == 0 && i915_gem_object_is_inactive(obj))
1082 list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
Chris Wilson7d1c4802010-08-07 21:45:03 +01001083
Chris Wilson05394f32010-11-08 19:18:58 +00001084 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001085unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07001086 mutex_unlock(&dev->struct_mutex);
1087 return ret;
1088}
1089
1090/**
1091 * Called when user space has done writes to this buffer
1092 */
1093int
1094i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001095 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001096{
1097 struct drm_i915_gem_sw_finish *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001098 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001099 int ret = 0;
1100
1101 if (!(dev->driver->driver_features & DRIVER_GEM))
1102 return -ENODEV;
1103
Chris Wilson76c1dec2010-09-25 11:22:51 +01001104 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001105 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001106 return ret;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001107
Chris Wilson05394f32010-11-08 19:18:58 +00001108 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Eric Anholt673a3942008-07-30 12:06:12 -07001109 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001110 ret = -ENOENT;
1111 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07001112 }
1113
Eric Anholt673a3942008-07-30 12:06:12 -07001114 /* Pinned buffers may be scanout, so flush the cache */
Chris Wilson05394f32010-11-08 19:18:58 +00001115 if (obj->pin_count)
Eric Anholte47c68e2008-11-14 13:35:19 -08001116 i915_gem_object_flush_cpu_write_domain(obj);
1117
Chris Wilson05394f32010-11-08 19:18:58 +00001118 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001119unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07001120 mutex_unlock(&dev->struct_mutex);
1121 return ret;
1122}
1123
1124/**
1125 * Maps the contents of an object, returning the address it is mapped
1126 * into.
1127 *
1128 * While the mapping holds a reference on the contents of the object, it doesn't
1129 * imply a ref on the object itself.
1130 */
1131int
1132i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001133 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001134{
Chris Wilsonda761a62010-10-27 17:37:08 +01001135 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07001136 struct drm_i915_gem_mmap *args = data;
1137 struct drm_gem_object *obj;
1138 loff_t offset;
1139 unsigned long addr;
1140
1141 if (!(dev->driver->driver_features & DRIVER_GEM))
1142 return -ENODEV;
1143
Chris Wilson05394f32010-11-08 19:18:58 +00001144 obj = drm_gem_object_lookup(dev, file, args->handle);
Eric Anholt673a3942008-07-30 12:06:12 -07001145 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001146 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001147
Chris Wilsonda761a62010-10-27 17:37:08 +01001148 if (obj->size > dev_priv->mm.gtt_mappable_end) {
1149 drm_gem_object_unreference_unlocked(obj);
1150 return -E2BIG;
1151 }
1152
Eric Anholt673a3942008-07-30 12:06:12 -07001153 offset = args->offset;
1154
1155 down_write(&current->mm->mmap_sem);
1156 addr = do_mmap(obj->filp, 0, args->size,
1157 PROT_READ | PROT_WRITE, MAP_SHARED,
1158 args->offset);
1159 up_write(&current->mm->mmap_sem);
Luca Barbieribc9025b2010-02-09 05:49:12 +00001160 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001161 if (IS_ERR((void *)addr))
1162 return addr;
1163
1164 args->addr_ptr = (uint64_t) addr;
1165
1166 return 0;
1167}
1168
Jesse Barnesde151cf2008-11-12 10:03:55 -08001169/**
1170 * i915_gem_fault - fault a page into the GTT
1171 * vma: VMA in question
1172 * vmf: fault info
1173 *
1174 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1175 * from userspace. The fault handler takes care of binding the object to
1176 * the GTT (if needed), allocating and programming a fence register (again,
1177 * only if needed based on whether the old reg is still valid or the object
1178 * is tiled) and inserting a new PTE into the faulting process.
1179 *
1180 * Note that the faulting process may involve evicting existing objects
1181 * from the GTT and/or fence registers to make room. So performance may
1182 * suffer if the GTT working set is large or there are few fence registers
1183 * left.
1184 */
1185int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1186{
Chris Wilson05394f32010-11-08 19:18:58 +00001187 struct drm_i915_gem_object *obj = to_intel_bo(vma->vm_private_data);
1188 struct drm_device *dev = obj->base.dev;
Chris Wilson7d1c4802010-08-07 21:45:03 +01001189 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001190 pgoff_t page_offset;
1191 unsigned long pfn;
1192 int ret = 0;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001193 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001194
1195 /* We don't use vmf->pgoff since that has the fake offset */
1196 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1197 PAGE_SHIFT;
1198
1199 /* Now bind it into the GTT if needed */
1200 mutex_lock(&dev->struct_mutex);
Chris Wilsona00b10c2010-09-24 21:15:47 +01001201
Chris Wilson919926a2010-11-12 13:42:53 +00001202 if (!obj->map_and_fenceable) {
1203 ret = i915_gem_object_unbind(obj);
1204 if (ret)
1205 goto unlock;
Chris Wilsona00b10c2010-09-24 21:15:47 +01001206 }
Chris Wilson05394f32010-11-08 19:18:58 +00001207 if (!obj->gtt_space) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01001208 ret = i915_gem_object_bind_to_gtt(obj, 0, true);
Chris Wilsonc7150892009-09-23 00:43:56 +01001209 if (ret)
1210 goto unlock;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001211 }
1212
Chris Wilson4a684a42010-10-28 14:44:08 +01001213 ret = i915_gem_object_set_to_gtt_domain(obj, write);
1214 if (ret)
1215 goto unlock;
1216
Jesse Barnesde151cf2008-11-12 10:03:55 -08001217 /* Need a new fence register? */
Chris Wilson05394f32010-11-08 19:18:58 +00001218 if (obj->tiling_mode != I915_TILING_NONE) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01001219 ret = i915_gem_object_get_fence_reg(obj, true);
Chris Wilsonc7150892009-09-23 00:43:56 +01001220 if (ret)
1221 goto unlock;
Eric Anholtd9ddcb92009-01-27 10:33:49 -08001222 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001223
Chris Wilson05394f32010-11-08 19:18:58 +00001224 if (i915_gem_object_is_inactive(obj))
1225 list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
Chris Wilson7d1c4802010-08-07 21:45:03 +01001226
Chris Wilson6299f992010-11-24 12:23:44 +00001227 obj->fault_mappable = true;
1228
Chris Wilson05394f32010-11-08 19:18:58 +00001229 pfn = ((dev->agp->base + obj->gtt_offset) >> PAGE_SHIFT) +
Jesse Barnesde151cf2008-11-12 10:03:55 -08001230 page_offset;
1231
1232 /* Finally, remap it using the new GTT offset */
1233 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
Chris Wilsonc7150892009-09-23 00:43:56 +01001234unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001235 mutex_unlock(&dev->struct_mutex);
1236
1237 switch (ret) {
Chris Wilson045e7692010-11-07 09:18:22 +00001238 case -EAGAIN:
1239 set_need_resched();
Chris Wilsonc7150892009-09-23 00:43:56 +01001240 case 0:
1241 case -ERESTARTSYS:
1242 return VM_FAULT_NOPAGE;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001243 case -ENOMEM:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001244 return VM_FAULT_OOM;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001245 default:
Chris Wilsonc7150892009-09-23 00:43:56 +01001246 return VM_FAULT_SIGBUS;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001247 }
1248}
1249
1250/**
1251 * i915_gem_create_mmap_offset - create a fake mmap offset for an object
1252 * @obj: obj in question
1253 *
1254 * GEM memory mapping works by handing back to userspace a fake mmap offset
1255 * it can use in a subsequent mmap(2) call. The DRM core code then looks
1256 * up the object based on the offset and sets up the various memory mapping
1257 * structures.
1258 *
1259 * This routine allocates and attaches a fake offset for @obj.
1260 */
1261static int
Chris Wilson05394f32010-11-08 19:18:58 +00001262i915_gem_create_mmap_offset(struct drm_i915_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001263{
Chris Wilson05394f32010-11-08 19:18:58 +00001264 struct drm_device *dev = obj->base.dev;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001265 struct drm_gem_mm *mm = dev->mm_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001266 struct drm_map_list *list;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +11001267 struct drm_local_map *map;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001268 int ret = 0;
1269
1270 /* Set the object up for mmap'ing */
Chris Wilson05394f32010-11-08 19:18:58 +00001271 list = &obj->base.map_list;
Eric Anholt9a298b22009-03-24 12:23:04 -07001272 list->map = kzalloc(sizeof(struct drm_map_list), GFP_KERNEL);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001273 if (!list->map)
1274 return -ENOMEM;
1275
1276 map = list->map;
1277 map->type = _DRM_GEM;
Chris Wilson05394f32010-11-08 19:18:58 +00001278 map->size = obj->base.size;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001279 map->handle = obj;
1280
1281 /* Get a DRM GEM mmap offset allocated... */
1282 list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
Chris Wilson05394f32010-11-08 19:18:58 +00001283 obj->base.size / PAGE_SIZE,
1284 0, 0);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001285 if (!list->file_offset_node) {
Chris Wilson05394f32010-11-08 19:18:58 +00001286 DRM_ERROR("failed to allocate offset for bo %d\n",
1287 obj->base.name);
Chris Wilson9e0ae5342010-09-21 15:05:24 +01001288 ret = -ENOSPC;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001289 goto out_free_list;
1290 }
1291
1292 list->file_offset_node = drm_mm_get_block(list->file_offset_node,
Chris Wilson05394f32010-11-08 19:18:58 +00001293 obj->base.size / PAGE_SIZE,
1294 0);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001295 if (!list->file_offset_node) {
1296 ret = -ENOMEM;
1297 goto out_free_list;
1298 }
1299
1300 list->hash.key = list->file_offset_node->start;
Chris Wilson9e0ae5342010-09-21 15:05:24 +01001301 ret = drm_ht_insert_item(&mm->offset_hash, &list->hash);
1302 if (ret) {
Jesse Barnesde151cf2008-11-12 10:03:55 -08001303 DRM_ERROR("failed to add to map hash\n");
1304 goto out_free_mm;
1305 }
1306
Jesse Barnesde151cf2008-11-12 10:03:55 -08001307 return 0;
1308
1309out_free_mm:
1310 drm_mm_put_block(list->file_offset_node);
1311out_free_list:
Eric Anholt9a298b22009-03-24 12:23:04 -07001312 kfree(list->map);
Chris Wilson39a01d12010-10-28 13:03:06 +01001313 list->map = NULL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001314
1315 return ret;
1316}
1317
Chris Wilson901782b2009-07-10 08:18:50 +01001318/**
1319 * i915_gem_release_mmap - remove physical page mappings
1320 * @obj: obj in question
1321 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001322 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01001323 * relinquish ownership of the pages back to the system.
1324 *
1325 * It is vital that we remove the page mapping if we have mapped a tiled
1326 * object through the GTT and then lose the fence register due to
1327 * resource pressure. Similarly if the object has been moved out of the
1328 * aperture, than pages mapped into userspace must be revoked. Removing the
1329 * mapping will then trigger a page fault on the next user access, allowing
1330 * fixup by i915_gem_fault().
1331 */
Eric Anholtd05ca302009-07-10 13:02:26 -07001332void
Chris Wilson05394f32010-11-08 19:18:58 +00001333i915_gem_release_mmap(struct drm_i915_gem_object *obj)
Chris Wilson901782b2009-07-10 08:18:50 +01001334{
Chris Wilson6299f992010-11-24 12:23:44 +00001335 if (!obj->fault_mappable)
1336 return;
Chris Wilson901782b2009-07-10 08:18:50 +01001337
Chris Wilson6299f992010-11-24 12:23:44 +00001338 unmap_mapping_range(obj->base.dev->dev_mapping,
1339 (loff_t)obj->base.map_list.hash.key<<PAGE_SHIFT,
1340 obj->base.size, 1);
Daniel Vetterfb7d5162010-10-01 22:05:20 +02001341
Chris Wilson6299f992010-11-24 12:23:44 +00001342 obj->fault_mappable = false;
Chris Wilson901782b2009-07-10 08:18:50 +01001343}
1344
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001345static void
Chris Wilson05394f32010-11-08 19:18:58 +00001346i915_gem_free_mmap_offset(struct drm_i915_gem_object *obj)
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001347{
Chris Wilson05394f32010-11-08 19:18:58 +00001348 struct drm_device *dev = obj->base.dev;
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001349 struct drm_gem_mm *mm = dev->mm_private;
Chris Wilson05394f32010-11-08 19:18:58 +00001350 struct drm_map_list *list = &obj->base.map_list;
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001351
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001352 drm_ht_remove_item(&mm->offset_hash, &list->hash);
Chris Wilson39a01d12010-10-28 13:03:06 +01001353 drm_mm_put_block(list->file_offset_node);
1354 kfree(list->map);
1355 list->map = NULL;
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001356}
1357
Chris Wilson92b88ae2010-11-09 11:47:32 +00001358static uint32_t
1359i915_gem_get_gtt_size(struct drm_i915_gem_object *obj)
1360{
1361 struct drm_device *dev = obj->base.dev;
1362 uint32_t size;
1363
1364 if (INTEL_INFO(dev)->gen >= 4 ||
1365 obj->tiling_mode == I915_TILING_NONE)
1366 return obj->base.size;
1367
1368 /* Previous chips need a power-of-two fence region when tiling */
1369 if (INTEL_INFO(dev)->gen == 3)
1370 size = 1024*1024;
1371 else
1372 size = 512*1024;
1373
1374 while (size < obj->base.size)
1375 size <<= 1;
1376
1377 return size;
1378}
1379
Jesse Barnesde151cf2008-11-12 10:03:55 -08001380/**
1381 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1382 * @obj: object to check
1383 *
1384 * Return the required GTT alignment for an object, taking into account
Daniel Vetter5e783302010-11-14 22:32:36 +01001385 * potential fence register mapping.
Jesse Barnesde151cf2008-11-12 10:03:55 -08001386 */
1387static uint32_t
Chris Wilson05394f32010-11-08 19:18:58 +00001388i915_gem_get_gtt_alignment(struct drm_i915_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001389{
Chris Wilson05394f32010-11-08 19:18:58 +00001390 struct drm_device *dev = obj->base.dev;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001391
1392 /*
1393 * Minimum alignment is 4k (GTT page size), but might be greater
1394 * if a fence register is needed for the object.
1395 */
Chris Wilsona00b10c2010-09-24 21:15:47 +01001396 if (INTEL_INFO(dev)->gen >= 4 ||
Chris Wilson05394f32010-11-08 19:18:58 +00001397 obj->tiling_mode == I915_TILING_NONE)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001398 return 4096;
1399
1400 /*
1401 * Previous chips need to be aligned to the size of the smallest
1402 * fence register that can contain the object.
1403 */
Chris Wilson05394f32010-11-08 19:18:58 +00001404 return i915_gem_get_gtt_size(obj);
Chris Wilsona00b10c2010-09-24 21:15:47 +01001405}
1406
Daniel Vetter5e783302010-11-14 22:32:36 +01001407/**
1408 * i915_gem_get_unfenced_gtt_alignment - return required GTT alignment for an
1409 * unfenced object
1410 * @obj: object to check
1411 *
1412 * Return the required GTT alignment for an object, only taking into account
1413 * unfenced tiled surface requirements.
1414 */
1415static uint32_t
Chris Wilson05394f32010-11-08 19:18:58 +00001416i915_gem_get_unfenced_gtt_alignment(struct drm_i915_gem_object *obj)
Daniel Vetter5e783302010-11-14 22:32:36 +01001417{
Chris Wilson05394f32010-11-08 19:18:58 +00001418 struct drm_device *dev = obj->base.dev;
Daniel Vetter5e783302010-11-14 22:32:36 +01001419 int tile_height;
1420
1421 /*
1422 * Minimum alignment is 4k (GTT page size) for sane hw.
1423 */
1424 if (INTEL_INFO(dev)->gen >= 4 || IS_G33(dev) ||
Chris Wilson05394f32010-11-08 19:18:58 +00001425 obj->tiling_mode == I915_TILING_NONE)
Daniel Vetter5e783302010-11-14 22:32:36 +01001426 return 4096;
1427
1428 /*
1429 * Older chips need unfenced tiled buffers to be aligned to the left
1430 * edge of an even tile row (where tile rows are counted as if the bo is
1431 * placed in a fenced gtt region).
1432 */
1433 if (IS_GEN2(dev) ||
Chris Wilson05394f32010-11-08 19:18:58 +00001434 (obj->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev)))
Daniel Vetter5e783302010-11-14 22:32:36 +01001435 tile_height = 32;
1436 else
1437 tile_height = 8;
1438
Chris Wilson05394f32010-11-08 19:18:58 +00001439 return tile_height * obj->stride * 2;
Daniel Vetter5e783302010-11-14 22:32:36 +01001440}
1441
Jesse Barnesde151cf2008-11-12 10:03:55 -08001442/**
1443 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1444 * @dev: DRM device
1445 * @data: GTT mapping ioctl data
Chris Wilson05394f32010-11-08 19:18:58 +00001446 * @file: GEM object info
Jesse Barnesde151cf2008-11-12 10:03:55 -08001447 *
1448 * Simply returns the fake offset to userspace so it can mmap it.
1449 * The mmap call will end up in drm_gem_mmap(), which will set things
1450 * up so we can get faults in the handler above.
1451 *
1452 * The fault handler will take care of binding the object into the GTT
1453 * (since it may have been evicted to make room for something), allocating
1454 * a fence register, and mapping the appropriate aperture address into
1455 * userspace.
1456 */
1457int
1458i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001459 struct drm_file *file)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001460{
Chris Wilsonda761a62010-10-27 17:37:08 +01001461 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001462 struct drm_i915_gem_mmap_gtt *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001463 struct drm_i915_gem_object *obj;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001464 int ret;
1465
1466 if (!(dev->driver->driver_features & DRIVER_GEM))
1467 return -ENODEV;
1468
Chris Wilson76c1dec2010-09-25 11:22:51 +01001469 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001470 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001471 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001472
Chris Wilson05394f32010-11-08 19:18:58 +00001473 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001474 if (obj == NULL) {
1475 ret = -ENOENT;
1476 goto unlock;
1477 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001478
Chris Wilson05394f32010-11-08 19:18:58 +00001479 if (obj->base.size > dev_priv->mm.gtt_mappable_end) {
Chris Wilsonda761a62010-10-27 17:37:08 +01001480 ret = -E2BIG;
1481 goto unlock;
1482 }
1483
Chris Wilson05394f32010-11-08 19:18:58 +00001484 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilsonab182822009-09-22 18:46:17 +01001485 DRM_ERROR("Attempting to mmap a purgeable buffer\n");
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001486 ret = -EINVAL;
1487 goto out;
Chris Wilsonab182822009-09-22 18:46:17 +01001488 }
1489
Chris Wilson05394f32010-11-08 19:18:58 +00001490 if (!obj->base.map_list.map) {
Jesse Barnesde151cf2008-11-12 10:03:55 -08001491 ret = i915_gem_create_mmap_offset(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001492 if (ret)
1493 goto out;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001494 }
1495
Chris Wilson05394f32010-11-08 19:18:58 +00001496 args->offset = (u64)obj->base.map_list.hash.key << PAGE_SHIFT;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001497
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001498out:
Chris Wilson05394f32010-11-08 19:18:58 +00001499 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001500unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001501 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001502 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001503}
1504
Chris Wilsone5281cc2010-10-28 13:45:36 +01001505static int
Chris Wilson05394f32010-11-08 19:18:58 +00001506i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj,
Chris Wilsone5281cc2010-10-28 13:45:36 +01001507 gfp_t gfpmask)
1508{
Chris Wilsone5281cc2010-10-28 13:45:36 +01001509 int page_count, i;
1510 struct address_space *mapping;
1511 struct inode *inode;
1512 struct page *page;
1513
1514 /* Get the list of pages out of our struct file. They'll be pinned
1515 * at this point until we release them.
1516 */
Chris Wilson05394f32010-11-08 19:18:58 +00001517 page_count = obj->base.size / PAGE_SIZE;
1518 BUG_ON(obj->pages != NULL);
1519 obj->pages = drm_malloc_ab(page_count, sizeof(struct page *));
1520 if (obj->pages == NULL)
Chris Wilsone5281cc2010-10-28 13:45:36 +01001521 return -ENOMEM;
1522
Chris Wilson05394f32010-11-08 19:18:58 +00001523 inode = obj->base.filp->f_path.dentry->d_inode;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001524 mapping = inode->i_mapping;
1525 for (i = 0; i < page_count; i++) {
1526 page = read_cache_page_gfp(mapping, i,
1527 GFP_HIGHUSER |
1528 __GFP_COLD |
1529 __GFP_RECLAIMABLE |
1530 gfpmask);
1531 if (IS_ERR(page))
1532 goto err_pages;
1533
Chris Wilson05394f32010-11-08 19:18:58 +00001534 obj->pages[i] = page;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001535 }
1536
Chris Wilson05394f32010-11-08 19:18:58 +00001537 if (obj->tiling_mode != I915_TILING_NONE)
Chris Wilsone5281cc2010-10-28 13:45:36 +01001538 i915_gem_object_do_bit_17_swizzle(obj);
1539
1540 return 0;
1541
1542err_pages:
1543 while (i--)
Chris Wilson05394f32010-11-08 19:18:58 +00001544 page_cache_release(obj->pages[i]);
Chris Wilsone5281cc2010-10-28 13:45:36 +01001545
Chris Wilson05394f32010-11-08 19:18:58 +00001546 drm_free_large(obj->pages);
1547 obj->pages = NULL;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001548 return PTR_ERR(page);
1549}
1550
Chris Wilson5cdf5882010-09-27 15:51:07 +01001551static void
Chris Wilson05394f32010-11-08 19:18:58 +00001552i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07001553{
Chris Wilson05394f32010-11-08 19:18:58 +00001554 int page_count = obj->base.size / PAGE_SIZE;
Eric Anholt673a3942008-07-30 12:06:12 -07001555 int i;
1556
Chris Wilson05394f32010-11-08 19:18:58 +00001557 BUG_ON(obj->madv == __I915_MADV_PURGED);
Eric Anholt856fa192009-03-19 14:10:50 -07001558
Chris Wilson05394f32010-11-08 19:18:58 +00001559 if (obj->tiling_mode != I915_TILING_NONE)
Eric Anholt280b7132009-03-12 16:56:27 -07001560 i915_gem_object_save_bit_17_swizzle(obj);
1561
Chris Wilson05394f32010-11-08 19:18:58 +00001562 if (obj->madv == I915_MADV_DONTNEED)
1563 obj->dirty = 0;
Chris Wilson3ef94da2009-09-14 16:50:29 +01001564
1565 for (i = 0; i < page_count; i++) {
Chris Wilson05394f32010-11-08 19:18:58 +00001566 if (obj->dirty)
1567 set_page_dirty(obj->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001568
Chris Wilson05394f32010-11-08 19:18:58 +00001569 if (obj->madv == I915_MADV_WILLNEED)
1570 mark_page_accessed(obj->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001571
Chris Wilson05394f32010-11-08 19:18:58 +00001572 page_cache_release(obj->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001573 }
Chris Wilson05394f32010-11-08 19:18:58 +00001574 obj->dirty = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001575
Chris Wilson05394f32010-11-08 19:18:58 +00001576 drm_free_large(obj->pages);
1577 obj->pages = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001578}
1579
Chris Wilsona56ba562010-09-28 10:07:56 +01001580static uint32_t
1581i915_gem_next_request_seqno(struct drm_device *dev,
1582 struct intel_ring_buffer *ring)
1583{
1584 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson5d97eb62010-11-10 20:40:02 +00001585 return ring->outstanding_lazy_request = dev_priv->next_seqno;
Chris Wilsona56ba562010-09-28 10:07:56 +01001586}
1587
Eric Anholt673a3942008-07-30 12:06:12 -07001588static void
Chris Wilson05394f32010-11-08 19:18:58 +00001589i915_gem_object_move_to_active(struct drm_i915_gem_object *obj,
Zou Nan hai852835f2010-05-21 09:08:56 +08001590 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001591{
Chris Wilson05394f32010-11-08 19:18:58 +00001592 struct drm_device *dev = obj->base.dev;
Chris Wilson69dc4982010-10-19 10:36:51 +01001593 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona56ba562010-09-28 10:07:56 +01001594 uint32_t seqno = i915_gem_next_request_seqno(dev, ring);
Daniel Vetter617dbe22010-02-11 22:16:02 +01001595
Zou Nan hai852835f2010-05-21 09:08:56 +08001596 BUG_ON(ring == NULL);
Chris Wilson05394f32010-11-08 19:18:58 +00001597 obj->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001598
1599 /* Add a reference if we're newly entering the active list. */
Chris Wilson05394f32010-11-08 19:18:58 +00001600 if (!obj->active) {
1601 drm_gem_object_reference(&obj->base);
1602 obj->active = 1;
Eric Anholt673a3942008-07-30 12:06:12 -07001603 }
Daniel Vettere35a41d2010-02-11 22:13:59 +01001604
Eric Anholt673a3942008-07-30 12:06:12 -07001605 /* Move from whatever list we were on to the tail of execution. */
Chris Wilson05394f32010-11-08 19:18:58 +00001606 list_move_tail(&obj->mm_list, &dev_priv->mm.active_list);
1607 list_move_tail(&obj->ring_list, &ring->active_list);
Chris Wilsoncaea7472010-11-12 13:53:37 +00001608
Chris Wilson05394f32010-11-08 19:18:58 +00001609 obj->last_rendering_seqno = seqno;
Chris Wilsoncaea7472010-11-12 13:53:37 +00001610 if (obj->fenced_gpu_access) {
1611 struct drm_i915_fence_reg *reg;
1612
1613 BUG_ON(obj->fence_reg == I915_FENCE_REG_NONE);
1614
1615 obj->last_fenced_seqno = seqno;
1616 obj->last_fenced_ring = ring;
1617
1618 reg = &dev_priv->fence_regs[obj->fence_reg];
1619 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
1620 }
1621}
1622
1623static void
1624i915_gem_object_move_off_active(struct drm_i915_gem_object *obj)
1625{
1626 list_del_init(&obj->ring_list);
1627 obj->last_rendering_seqno = 0;
1628 obj->last_fenced_seqno = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001629}
1630
Eric Anholtce44b0e2008-11-06 16:00:31 -08001631static void
Chris Wilson05394f32010-11-08 19:18:58 +00001632i915_gem_object_move_to_flushing(struct drm_i915_gem_object *obj)
Eric Anholtce44b0e2008-11-06 16:00:31 -08001633{
Chris Wilson05394f32010-11-08 19:18:58 +00001634 struct drm_device *dev = obj->base.dev;
Eric Anholtce44b0e2008-11-06 16:00:31 -08001635 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtce44b0e2008-11-06 16:00:31 -08001636
Chris Wilson05394f32010-11-08 19:18:58 +00001637 BUG_ON(!obj->active);
1638 list_move_tail(&obj->mm_list, &dev_priv->mm.flushing_list);
Chris Wilsoncaea7472010-11-12 13:53:37 +00001639
1640 i915_gem_object_move_off_active(obj);
1641}
1642
1643static void
1644i915_gem_object_move_to_inactive(struct drm_i915_gem_object *obj)
1645{
1646 struct drm_device *dev = obj->base.dev;
1647 struct drm_i915_private *dev_priv = dev->dev_private;
1648
1649 if (obj->pin_count != 0)
1650 list_move_tail(&obj->mm_list, &dev_priv->mm.pinned_list);
1651 else
1652 list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
1653
1654 BUG_ON(!list_empty(&obj->gpu_write_list));
1655 BUG_ON(!obj->active);
1656 obj->ring = NULL;
1657
1658 i915_gem_object_move_off_active(obj);
1659 obj->fenced_gpu_access = false;
1660 obj->last_fenced_ring = NULL;
1661
1662 obj->active = 0;
1663 drm_gem_object_unreference(&obj->base);
1664
1665 WARN_ON(i915_verify_lists(dev));
Eric Anholtce44b0e2008-11-06 16:00:31 -08001666}
Eric Anholt673a3942008-07-30 12:06:12 -07001667
Chris Wilson963b4832009-09-20 23:03:54 +01001668/* Immediately discard the backing storage */
1669static void
Chris Wilson05394f32010-11-08 19:18:58 +00001670i915_gem_object_truncate(struct drm_i915_gem_object *obj)
Chris Wilson963b4832009-09-20 23:03:54 +01001671{
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001672 struct inode *inode;
Chris Wilson963b4832009-09-20 23:03:54 +01001673
Chris Wilsonae9fed62010-08-07 11:01:30 +01001674 /* Our goal here is to return as much of the memory as
1675 * is possible back to the system as we are called from OOM.
1676 * To do this we must instruct the shmfs to drop all of its
1677 * backing pages, *now*. Here we mirror the actions taken
1678 * when by shmem_delete_inode() to release the backing store.
1679 */
Chris Wilson05394f32010-11-08 19:18:58 +00001680 inode = obj->base.filp->f_path.dentry->d_inode;
Chris Wilsonae9fed62010-08-07 11:01:30 +01001681 truncate_inode_pages(inode->i_mapping, 0);
1682 if (inode->i_op->truncate_range)
1683 inode->i_op->truncate_range(inode, 0, (loff_t)-1);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001684
Chris Wilson05394f32010-11-08 19:18:58 +00001685 obj->madv = __I915_MADV_PURGED;
Chris Wilson963b4832009-09-20 23:03:54 +01001686}
1687
1688static inline int
Chris Wilson05394f32010-11-08 19:18:58 +00001689i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj)
Chris Wilson963b4832009-09-20 23:03:54 +01001690{
Chris Wilson05394f32010-11-08 19:18:58 +00001691 return obj->madv == I915_MADV_DONTNEED;
Chris Wilson963b4832009-09-20 23:03:54 +01001692}
1693
Eric Anholt673a3942008-07-30 12:06:12 -07001694static void
Daniel Vetter63560392010-02-19 11:51:59 +01001695i915_gem_process_flushing_list(struct drm_device *dev,
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001696 uint32_t flush_domains,
Zou Nan hai852835f2010-05-21 09:08:56 +08001697 struct intel_ring_buffer *ring)
Daniel Vetter63560392010-02-19 11:51:59 +01001698{
Chris Wilson05394f32010-11-08 19:18:58 +00001699 struct drm_i915_gem_object *obj, *next;
Daniel Vetter63560392010-02-19 11:51:59 +01001700
Chris Wilson05394f32010-11-08 19:18:58 +00001701 list_for_each_entry_safe(obj, next,
Chris Wilson64193402010-10-24 12:38:05 +01001702 &ring->gpu_write_list,
Daniel Vetter63560392010-02-19 11:51:59 +01001703 gpu_write_list) {
Chris Wilson05394f32010-11-08 19:18:58 +00001704 if (obj->base.write_domain & flush_domains) {
1705 uint32_t old_write_domain = obj->base.write_domain;
Daniel Vetter63560392010-02-19 11:51:59 +01001706
Chris Wilson05394f32010-11-08 19:18:58 +00001707 obj->base.write_domain = 0;
1708 list_del_init(&obj->gpu_write_list);
Daniel Vetter617dbe22010-02-11 22:16:02 +01001709 i915_gem_object_move_to_active(obj, ring);
Daniel Vetter63560392010-02-19 11:51:59 +01001710
Daniel Vetter63560392010-02-19 11:51:59 +01001711 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00001712 obj->base.read_domains,
Daniel Vetter63560392010-02-19 11:51:59 +01001713 old_write_domain);
1714 }
1715 }
1716}
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001717
Chris Wilson3cce4692010-10-27 16:11:02 +01001718int
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001719i915_add_request(struct drm_device *dev,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001720 struct drm_file *file,
Chris Wilson8dc5d142010-08-12 12:36:12 +01001721 struct drm_i915_gem_request *request,
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001722 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001723{
1724 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001725 struct drm_i915_file_private *file_priv = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001726 uint32_t seqno;
1727 int was_empty;
Chris Wilson3cce4692010-10-27 16:11:02 +01001728 int ret;
1729
1730 BUG_ON(request == NULL);
Eric Anholt673a3942008-07-30 12:06:12 -07001731
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001732 if (file != NULL)
1733 file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00001734
Chris Wilson3cce4692010-10-27 16:11:02 +01001735 ret = ring->add_request(ring, &seqno);
1736 if (ret)
1737 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001738
Chris Wilsona56ba562010-09-28 10:07:56 +01001739 ring->outstanding_lazy_request = false;
Eric Anholt673a3942008-07-30 12:06:12 -07001740
1741 request->seqno = seqno;
Zou Nan hai852835f2010-05-21 09:08:56 +08001742 request->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001743 request->emitted_jiffies = jiffies;
Zou Nan hai852835f2010-05-21 09:08:56 +08001744 was_empty = list_empty(&ring->request_list);
1745 list_add_tail(&request->list, &ring->request_list);
1746
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001747 if (file_priv) {
Chris Wilson1c255952010-09-26 11:03:27 +01001748 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001749 request->file_priv = file_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00001750 list_add_tail(&request->client_list,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001751 &file_priv->mm.request_list);
Chris Wilson1c255952010-09-26 11:03:27 +01001752 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00001753 }
Eric Anholt673a3942008-07-30 12:06:12 -07001754
Ben Gamarif65d9422009-09-14 17:48:44 -04001755 if (!dev_priv->mm.suspended) {
Chris Wilsonb3b079d2010-09-13 23:44:34 +01001756 mod_timer(&dev_priv->hangcheck_timer,
1757 jiffies + msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
Ben Gamarif65d9422009-09-14 17:48:44 -04001758 if (was_empty)
Chris Wilsonb3b079d2010-09-13 23:44:34 +01001759 queue_delayed_work(dev_priv->wq,
1760 &dev_priv->mm.retire_work, HZ);
Ben Gamarif65d9422009-09-14 17:48:44 -04001761 }
Chris Wilson3cce4692010-10-27 16:11:02 +01001762 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001763}
1764
1765/**
1766 * Command execution barrier
1767 *
1768 * Ensures that all commands in the ring are finished
1769 * before signalling the CPU
1770 */
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001771static void
Zou Nan hai852835f2010-05-21 09:08:56 +08001772i915_retire_commands(struct drm_device *dev, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001773{
Eric Anholt673a3942008-07-30 12:06:12 -07001774 uint32_t flush_domains = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001775
1776 /* The sampler always gets flushed on i965 (sigh) */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001777 if (INTEL_INFO(dev)->gen >= 4)
Eric Anholt673a3942008-07-30 12:06:12 -07001778 flush_domains |= I915_GEM_DOMAIN_SAMPLER;
Zou Nan hai852835f2010-05-21 09:08:56 +08001779
Chris Wilson78501ea2010-10-27 12:18:21 +01001780 ring->flush(ring, I915_GEM_DOMAIN_COMMAND, flush_domains);
Eric Anholt673a3942008-07-30 12:06:12 -07001781}
1782
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001783static inline void
1784i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
Eric Anholt673a3942008-07-30 12:06:12 -07001785{
Chris Wilson1c255952010-09-26 11:03:27 +01001786 struct drm_i915_file_private *file_priv = request->file_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07001787
Chris Wilson1c255952010-09-26 11:03:27 +01001788 if (!file_priv)
1789 return;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001790
Chris Wilson1c255952010-09-26 11:03:27 +01001791 spin_lock(&file_priv->mm.lock);
1792 list_del(&request->client_list);
1793 request->file_priv = NULL;
1794 spin_unlock(&file_priv->mm.lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001795}
1796
Chris Wilsondfaae392010-09-22 10:31:52 +01001797static void i915_gem_reset_ring_lists(struct drm_i915_private *dev_priv,
1798 struct intel_ring_buffer *ring)
Chris Wilson9375e442010-09-19 12:21:28 +01001799{
Chris Wilsondfaae392010-09-22 10:31:52 +01001800 while (!list_empty(&ring->request_list)) {
1801 struct drm_i915_gem_request *request;
Chris Wilson9375e442010-09-19 12:21:28 +01001802
Chris Wilsondfaae392010-09-22 10:31:52 +01001803 request = list_first_entry(&ring->request_list,
1804 struct drm_i915_gem_request,
1805 list);
1806
1807 list_del(&request->list);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001808 i915_gem_request_remove_from_client(request);
Chris Wilsondfaae392010-09-22 10:31:52 +01001809 kfree(request);
1810 }
1811
1812 while (!list_empty(&ring->active_list)) {
Chris Wilson05394f32010-11-08 19:18:58 +00001813 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001814
Chris Wilson05394f32010-11-08 19:18:58 +00001815 obj = list_first_entry(&ring->active_list,
1816 struct drm_i915_gem_object,
1817 ring_list);
Eric Anholt673a3942008-07-30 12:06:12 -07001818
Chris Wilson05394f32010-11-08 19:18:58 +00001819 obj->base.write_domain = 0;
1820 list_del_init(&obj->gpu_write_list);
1821 i915_gem_object_move_to_inactive(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001822 }
Eric Anholt673a3942008-07-30 12:06:12 -07001823}
1824
Chris Wilson312817a2010-11-22 11:50:11 +00001825static void i915_gem_reset_fences(struct drm_device *dev)
1826{
1827 struct drm_i915_private *dev_priv = dev->dev_private;
1828 int i;
1829
1830 for (i = 0; i < 16; i++) {
1831 struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
1832 if (reg->obj)
1833 i915_gem_clear_fence_reg(reg->obj);
1834 }
1835}
1836
Chris Wilson069efc12010-09-30 16:53:18 +01001837void i915_gem_reset(struct drm_device *dev)
Eric Anholt673a3942008-07-30 12:06:12 -07001838{
Chris Wilsondfaae392010-09-22 10:31:52 +01001839 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00001840 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001841
Chris Wilsondfaae392010-09-22 10:31:52 +01001842 i915_gem_reset_ring_lists(dev_priv, &dev_priv->render_ring);
Chris Wilson87acb0a2010-10-19 10:13:00 +01001843 i915_gem_reset_ring_lists(dev_priv, &dev_priv->bsd_ring);
Chris Wilson549f7362010-10-19 11:19:32 +01001844 i915_gem_reset_ring_lists(dev_priv, &dev_priv->blt_ring);
Chris Wilsondfaae392010-09-22 10:31:52 +01001845
1846 /* Remove anything from the flushing lists. The GPU cache is likely
1847 * to be lost on reset along with the data, so simply move the
1848 * lost bo to the inactive list.
1849 */
1850 while (!list_empty(&dev_priv->mm.flushing_list)) {
Chris Wilson05394f32010-11-08 19:18:58 +00001851 obj= list_first_entry(&dev_priv->mm.flushing_list,
1852 struct drm_i915_gem_object,
1853 mm_list);
Chris Wilson9375e442010-09-19 12:21:28 +01001854
Chris Wilson05394f32010-11-08 19:18:58 +00001855 obj->base.write_domain = 0;
1856 list_del_init(&obj->gpu_write_list);
1857 i915_gem_object_move_to_inactive(obj);
Chris Wilson9375e442010-09-19 12:21:28 +01001858 }
Chris Wilson9375e442010-09-19 12:21:28 +01001859
Chris Wilsondfaae392010-09-22 10:31:52 +01001860 /* Move everything out of the GPU domains to ensure we do any
1861 * necessary invalidation upon reuse.
1862 */
Chris Wilson05394f32010-11-08 19:18:58 +00001863 list_for_each_entry(obj,
Chris Wilson77f01232010-09-19 12:31:36 +01001864 &dev_priv->mm.inactive_list,
Chris Wilson69dc4982010-10-19 10:36:51 +01001865 mm_list)
Chris Wilson77f01232010-09-19 12:31:36 +01001866 {
Chris Wilson05394f32010-11-08 19:18:58 +00001867 obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
Chris Wilson77f01232010-09-19 12:31:36 +01001868 }
Chris Wilson069efc12010-09-30 16:53:18 +01001869
1870 /* The fence registers are invalidated so clear them out */
Chris Wilson312817a2010-11-22 11:50:11 +00001871 i915_gem_reset_fences(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07001872}
1873
1874/**
1875 * This function clears the request list as sequence numbers are passed.
1876 */
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001877static void
1878i915_gem_retire_requests_ring(struct drm_device *dev,
1879 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001880{
1881 drm_i915_private_t *dev_priv = dev->dev_private;
1882 uint32_t seqno;
1883
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001884 if (!ring->status_page.page_addr ||
1885 list_empty(&ring->request_list))
Karsten Wiese6c0594a2009-02-23 15:07:57 +01001886 return;
1887
Chris Wilson23bc5982010-09-29 16:10:57 +01001888 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07001889
Chris Wilson78501ea2010-10-27 12:18:21 +01001890 seqno = ring->get_seqno(ring);
Zou Nan hai852835f2010-05-21 09:08:56 +08001891 while (!list_empty(&ring->request_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001892 struct drm_i915_gem_request *request;
Eric Anholt673a3942008-07-30 12:06:12 -07001893
Zou Nan hai852835f2010-05-21 09:08:56 +08001894 request = list_first_entry(&ring->request_list,
Eric Anholt673a3942008-07-30 12:06:12 -07001895 struct drm_i915_gem_request,
1896 list);
Eric Anholt673a3942008-07-30 12:06:12 -07001897
Chris Wilsondfaae392010-09-22 10:31:52 +01001898 if (!i915_seqno_passed(seqno, request->seqno))
Eric Anholt673a3942008-07-30 12:06:12 -07001899 break;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001900
1901 trace_i915_gem_request_retire(dev, request->seqno);
1902
1903 list_del(&request->list);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001904 i915_gem_request_remove_from_client(request);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001905 kfree(request);
1906 }
1907
1908 /* Move any buffers on the active list that are no longer referenced
1909 * by the ringbuffer to the flushing/inactive lists as appropriate.
1910 */
1911 while (!list_empty(&ring->active_list)) {
Chris Wilson05394f32010-11-08 19:18:58 +00001912 struct drm_i915_gem_object *obj;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001913
Chris Wilson05394f32010-11-08 19:18:58 +00001914 obj= list_first_entry(&ring->active_list,
1915 struct drm_i915_gem_object,
1916 ring_list);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001917
Chris Wilson05394f32010-11-08 19:18:58 +00001918 if (!i915_seqno_passed(seqno, obj->last_rendering_seqno))
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001919 break;
1920
Chris Wilson05394f32010-11-08 19:18:58 +00001921 if (obj->base.write_domain != 0)
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001922 i915_gem_object_move_to_flushing(obj);
1923 else
1924 i915_gem_object_move_to_inactive(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001925 }
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001926
1927 if (unlikely (dev_priv->trace_irq_seqno &&
1928 i915_seqno_passed(dev_priv->trace_irq_seqno, seqno))) {
Chris Wilson78501ea2010-10-27 12:18:21 +01001929 ring->user_irq_put(ring);
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001930 dev_priv->trace_irq_seqno = 0;
1931 }
Chris Wilson23bc5982010-09-29 16:10:57 +01001932
1933 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07001934}
1935
1936void
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001937i915_gem_retire_requests(struct drm_device *dev)
1938{
1939 drm_i915_private_t *dev_priv = dev->dev_private;
1940
Chris Wilsonbe726152010-07-23 23:18:50 +01001941 if (!list_empty(&dev_priv->mm.deferred_free_list)) {
Chris Wilson05394f32010-11-08 19:18:58 +00001942 struct drm_i915_gem_object *obj, *next;
Chris Wilsonbe726152010-07-23 23:18:50 +01001943
1944 /* We must be careful that during unbind() we do not
1945 * accidentally infinitely recurse into retire requests.
1946 * Currently:
1947 * retire -> free -> unbind -> wait -> retire_ring
1948 */
Chris Wilson05394f32010-11-08 19:18:58 +00001949 list_for_each_entry_safe(obj, next,
Chris Wilsonbe726152010-07-23 23:18:50 +01001950 &dev_priv->mm.deferred_free_list,
Chris Wilson69dc4982010-10-19 10:36:51 +01001951 mm_list)
Chris Wilson05394f32010-11-08 19:18:58 +00001952 i915_gem_free_object_tail(obj);
Chris Wilsonbe726152010-07-23 23:18:50 +01001953 }
1954
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001955 i915_gem_retire_requests_ring(dev, &dev_priv->render_ring);
Chris Wilson87acb0a2010-10-19 10:13:00 +01001956 i915_gem_retire_requests_ring(dev, &dev_priv->bsd_ring);
Chris Wilson549f7362010-10-19 11:19:32 +01001957 i915_gem_retire_requests_ring(dev, &dev_priv->blt_ring);
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001958}
1959
Daniel Vetter75ef9da2010-08-21 00:25:16 +02001960static void
Eric Anholt673a3942008-07-30 12:06:12 -07001961i915_gem_retire_work_handler(struct work_struct *work)
1962{
1963 drm_i915_private_t *dev_priv;
1964 struct drm_device *dev;
1965
1966 dev_priv = container_of(work, drm_i915_private_t,
1967 mm.retire_work.work);
1968 dev = dev_priv->dev;
1969
Chris Wilson891b48c2010-09-29 12:26:37 +01001970 /* Come back later if the device is busy... */
1971 if (!mutex_trylock(&dev->struct_mutex)) {
1972 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
1973 return;
1974 }
1975
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001976 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08001977
Keith Packard6dbe2772008-10-14 21:41:13 -07001978 if (!dev_priv->mm.suspended &&
Zou Nan haid1b851f2010-05-21 09:08:57 +08001979 (!list_empty(&dev_priv->render_ring.request_list) ||
Chris Wilson549f7362010-10-19 11:19:32 +01001980 !list_empty(&dev_priv->bsd_ring.request_list) ||
1981 !list_empty(&dev_priv->blt_ring.request_list)))
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001982 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
Eric Anholt673a3942008-07-30 12:06:12 -07001983 mutex_unlock(&dev->struct_mutex);
1984}
1985
Daniel Vetter5a5a0c62009-09-15 22:57:36 +02001986int
Zou Nan hai852835f2010-05-21 09:08:56 +08001987i915_do_wait_request(struct drm_device *dev, uint32_t seqno,
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001988 bool interruptible, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001989{
1990 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001991 u32 ier;
Eric Anholt673a3942008-07-30 12:06:12 -07001992 int ret = 0;
1993
1994 BUG_ON(seqno == 0);
1995
Ben Gamariba1234d2009-09-14 17:48:47 -04001996 if (atomic_read(&dev_priv->mm.wedged))
Chris Wilson30dbf0c2010-09-25 10:19:17 +01001997 return -EAGAIN;
Ben Gamariffed1d02009-09-14 17:48:41 -04001998
Chris Wilson5d97eb62010-11-10 20:40:02 +00001999 if (seqno == ring->outstanding_lazy_request) {
Chris Wilson3cce4692010-10-27 16:11:02 +01002000 struct drm_i915_gem_request *request;
2001
2002 request = kzalloc(sizeof(*request), GFP_KERNEL);
2003 if (request == NULL)
Daniel Vettere35a41d2010-02-11 22:13:59 +01002004 return -ENOMEM;
Chris Wilson3cce4692010-10-27 16:11:02 +01002005
2006 ret = i915_add_request(dev, NULL, request, ring);
2007 if (ret) {
2008 kfree(request);
2009 return ret;
2010 }
2011
2012 seqno = request->seqno;
Daniel Vettere35a41d2010-02-11 22:13:59 +01002013 }
2014
Chris Wilson78501ea2010-10-27 12:18:21 +01002015 if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) {
Eric Anholtbad720f2009-10-22 16:11:14 -07002016 if (HAS_PCH_SPLIT(dev))
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002017 ier = I915_READ(DEIER) | I915_READ(GTIER);
2018 else
2019 ier = I915_READ(IER);
Jesse Barnes802c7eb2009-05-05 16:03:48 -07002020 if (!ier) {
2021 DRM_ERROR("something (likely vbetool) disabled "
2022 "interrupts, re-enabling\n");
2023 i915_driver_irq_preinstall(dev);
2024 i915_driver_irq_postinstall(dev);
2025 }
2026
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002027 trace_i915_gem_request_wait_begin(dev, seqno);
2028
Chris Wilsonb2223492010-10-27 15:27:33 +01002029 ring->waiting_seqno = seqno;
Chris Wilson78501ea2010-10-27 12:18:21 +01002030 ring->user_irq_get(ring);
Daniel Vetter48764bf2009-09-15 22:57:32 +02002031 if (interruptible)
Zou Nan hai852835f2010-05-21 09:08:56 +08002032 ret = wait_event_interruptible(ring->irq_queue,
Chris Wilson78501ea2010-10-27 12:18:21 +01002033 i915_seqno_passed(ring->get_seqno(ring), seqno)
Zou Nan hai852835f2010-05-21 09:08:56 +08002034 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02002035 else
Zou Nan hai852835f2010-05-21 09:08:56 +08002036 wait_event(ring->irq_queue,
Chris Wilson78501ea2010-10-27 12:18:21 +01002037 i915_seqno_passed(ring->get_seqno(ring), seqno)
Zou Nan hai852835f2010-05-21 09:08:56 +08002038 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02002039
Chris Wilson78501ea2010-10-27 12:18:21 +01002040 ring->user_irq_put(ring);
Chris Wilsonb2223492010-10-27 15:27:33 +01002041 ring->waiting_seqno = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002042
2043 trace_i915_gem_request_wait_end(dev, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07002044 }
Ben Gamariba1234d2009-09-14 17:48:47 -04002045 if (atomic_read(&dev_priv->mm.wedged))
Chris Wilson30dbf0c2010-09-25 10:19:17 +01002046 ret = -EAGAIN;
Eric Anholt673a3942008-07-30 12:06:12 -07002047
2048 if (ret && ret != -ERESTARTSYS)
Daniel Vetter8bff9172010-02-11 22:19:40 +01002049 DRM_ERROR("%s returns %d (awaiting %d at %d, next %d)\n",
Chris Wilson78501ea2010-10-27 12:18:21 +01002050 __func__, ret, seqno, ring->get_seqno(ring),
Daniel Vetter8bff9172010-02-11 22:19:40 +01002051 dev_priv->next_seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07002052
2053 /* Directly dispatch request retiring. While we have the work queue
2054 * to handle this, the waiter on a request often wants an associated
2055 * buffer to have made it to the inactive list, and we would need
2056 * a separate wait queue to handle that.
2057 */
2058 if (ret == 0)
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002059 i915_gem_retire_requests_ring(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07002060
2061 return ret;
2062}
2063
Daniel Vetter48764bf2009-09-15 22:57:32 +02002064/**
2065 * Waits for a sequence number to be signaled, and cleans up the
2066 * request and object lists appropriately for that event.
2067 */
2068static int
Zou Nan hai852835f2010-05-21 09:08:56 +08002069i915_wait_request(struct drm_device *dev, uint32_t seqno,
Chris Wilsona56ba562010-09-28 10:07:56 +01002070 struct intel_ring_buffer *ring)
Daniel Vetter48764bf2009-09-15 22:57:32 +02002071{
Zou Nan hai852835f2010-05-21 09:08:56 +08002072 return i915_do_wait_request(dev, seqno, 1, ring);
Daniel Vetter48764bf2009-09-15 22:57:32 +02002073}
2074
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002075static void
Chris Wilson92204342010-09-18 11:02:01 +01002076i915_gem_flush_ring(struct drm_device *dev,
2077 struct intel_ring_buffer *ring,
2078 uint32_t invalidate_domains,
2079 uint32_t flush_domains)
2080{
Chris Wilson78501ea2010-10-27 12:18:21 +01002081 ring->flush(ring, invalidate_domains, flush_domains);
Chris Wilson92204342010-09-18 11:02:01 +01002082 i915_gem_process_flushing_list(dev, flush_domains, ring);
2083}
2084
2085static void
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002086i915_gem_flush(struct drm_device *dev,
2087 uint32_t invalidate_domains,
Chris Wilson92204342010-09-18 11:02:01 +01002088 uint32_t flush_domains,
2089 uint32_t flush_rings)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002090{
2091 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter8bff9172010-02-11 22:19:40 +01002092
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002093 if (flush_domains & I915_GEM_DOMAIN_CPU)
Daniel Vetter40ce6572010-11-05 18:12:18 +01002094 intel_gtt_chipset_flush();
Zou Nan haid1b851f2010-05-21 09:08:57 +08002095
Chris Wilson92204342010-09-18 11:02:01 +01002096 if ((flush_domains | invalidate_domains) & I915_GEM_GPU_DOMAINS) {
2097 if (flush_rings & RING_RENDER)
Chris Wilson05394f32010-11-08 19:18:58 +00002098 i915_gem_flush_ring(dev, &dev_priv->render_ring,
Chris Wilson92204342010-09-18 11:02:01 +01002099 invalidate_domains, flush_domains);
2100 if (flush_rings & RING_BSD)
Chris Wilson05394f32010-11-08 19:18:58 +00002101 i915_gem_flush_ring(dev, &dev_priv->bsd_ring,
Chris Wilson92204342010-09-18 11:02:01 +01002102 invalidate_domains, flush_domains);
Chris Wilson549f7362010-10-19 11:19:32 +01002103 if (flush_rings & RING_BLT)
Chris Wilson05394f32010-11-08 19:18:58 +00002104 i915_gem_flush_ring(dev, &dev_priv->blt_ring,
Chris Wilson549f7362010-10-19 11:19:32 +01002105 invalidate_domains, flush_domains);
Chris Wilson92204342010-09-18 11:02:01 +01002106 }
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002107}
2108
Eric Anholt673a3942008-07-30 12:06:12 -07002109/**
2110 * Ensures that all rendering to the object has completed and the object is
2111 * safe to unbind from the GTT or access from the CPU.
2112 */
2113static int
Chris Wilson05394f32010-11-08 19:18:58 +00002114i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj,
Chris Wilson2cf34d72010-09-14 13:03:28 +01002115 bool interruptible)
Eric Anholt673a3942008-07-30 12:06:12 -07002116{
Chris Wilson05394f32010-11-08 19:18:58 +00002117 struct drm_device *dev = obj->base.dev;
Eric Anholt673a3942008-07-30 12:06:12 -07002118 int ret;
2119
Eric Anholte47c68e2008-11-14 13:35:19 -08002120 /* This function only exists to support waiting for existing rendering,
2121 * not for emitting required flushes.
Eric Anholt673a3942008-07-30 12:06:12 -07002122 */
Chris Wilson05394f32010-11-08 19:18:58 +00002123 BUG_ON((obj->base.write_domain & I915_GEM_GPU_DOMAINS) != 0);
Eric Anholt673a3942008-07-30 12:06:12 -07002124
2125 /* If there is rendering queued on the buffer being evicted, wait for
2126 * it.
2127 */
Chris Wilson05394f32010-11-08 19:18:58 +00002128 if (obj->active) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01002129 ret = i915_do_wait_request(dev,
Chris Wilson05394f32010-11-08 19:18:58 +00002130 obj->last_rendering_seqno,
Chris Wilson2cf34d72010-09-14 13:03:28 +01002131 interruptible,
Chris Wilson05394f32010-11-08 19:18:58 +00002132 obj->ring);
Chris Wilson2cf34d72010-09-14 13:03:28 +01002133 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002134 return ret;
2135 }
2136
2137 return 0;
2138}
2139
2140/**
2141 * Unbinds an object from the GTT aperture.
2142 */
Jesse Barnes0f973f22009-01-26 17:10:45 -08002143int
Chris Wilson05394f32010-11-08 19:18:58 +00002144i915_gem_object_unbind(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002145{
Eric Anholt673a3942008-07-30 12:06:12 -07002146 int ret = 0;
2147
Chris Wilson05394f32010-11-08 19:18:58 +00002148 if (obj->gtt_space == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002149 return 0;
2150
Chris Wilson05394f32010-11-08 19:18:58 +00002151 if (obj->pin_count != 0) {
Eric Anholt673a3942008-07-30 12:06:12 -07002152 DRM_ERROR("Attempting to unbind pinned buffer\n");
2153 return -EINVAL;
2154 }
2155
Eric Anholt5323fd02009-09-09 11:50:45 -07002156 /* blow away mappings if mapped through GTT */
2157 i915_gem_release_mmap(obj);
2158
Eric Anholt673a3942008-07-30 12:06:12 -07002159 /* Move the object to the CPU domain to ensure that
2160 * any possible CPU writes while it's not in the GTT
2161 * are flushed when we go to remap it. This will
2162 * also ensure that all pending GPU writes are finished
2163 * before we unbind.
2164 */
Eric Anholte47c68e2008-11-14 13:35:19 -08002165 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Chris Wilson8dc17752010-07-23 23:18:51 +01002166 if (ret == -ERESTARTSYS)
Eric Anholt673a3942008-07-30 12:06:12 -07002167 return ret;
Chris Wilson8dc17752010-07-23 23:18:51 +01002168 /* Continue on if we fail due to EIO, the GPU is hung so we
2169 * should be safe and we need to cleanup or else we might
2170 * cause memory corruption through use-after-free.
2171 */
Chris Wilson812ed4922010-09-30 15:08:57 +01002172 if (ret) {
2173 i915_gem_clflush_object(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00002174 obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
Chris Wilson812ed4922010-09-30 15:08:57 +01002175 }
Eric Anholt673a3942008-07-30 12:06:12 -07002176
Daniel Vetter96b47b62009-12-15 17:50:00 +01002177 /* release the fence reg _after_ flushing */
Chris Wilson05394f32010-11-08 19:18:58 +00002178 if (obj->fence_reg != I915_FENCE_REG_NONE)
Daniel Vetter96b47b62009-12-15 17:50:00 +01002179 i915_gem_clear_fence_reg(obj);
2180
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002181 i915_gem_gtt_unbind_object(obj);
Chris Wilsone5281cc2010-10-28 13:45:36 +01002182 i915_gem_object_put_pages_gtt(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002183
Chris Wilson6299f992010-11-24 12:23:44 +00002184 list_del_init(&obj->gtt_list);
Chris Wilson05394f32010-11-08 19:18:58 +00002185 list_del_init(&obj->mm_list);
Daniel Vetter75e9e912010-11-04 17:11:09 +01002186 /* Avoid an unnecessary call to unbind on rebind. */
Chris Wilson05394f32010-11-08 19:18:58 +00002187 obj->map_and_fenceable = true;
Eric Anholt673a3942008-07-30 12:06:12 -07002188
Chris Wilson05394f32010-11-08 19:18:58 +00002189 drm_mm_put_block(obj->gtt_space);
2190 obj->gtt_space = NULL;
2191 obj->gtt_offset = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002192
Chris Wilson05394f32010-11-08 19:18:58 +00002193 if (i915_gem_object_is_purgeable(obj))
Chris Wilson963b4832009-09-20 23:03:54 +01002194 i915_gem_object_truncate(obj);
2195
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002196 trace_i915_gem_object_unbind(obj);
2197
Chris Wilson8dc17752010-07-23 23:18:51 +01002198 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002199}
2200
Chris Wilsona56ba562010-09-28 10:07:56 +01002201static int i915_ring_idle(struct drm_device *dev,
2202 struct intel_ring_buffer *ring)
2203{
Chris Wilson395b70b2010-10-28 21:28:46 +01002204 if (list_empty(&ring->gpu_write_list) && list_empty(&ring->active_list))
Chris Wilson64193402010-10-24 12:38:05 +01002205 return 0;
2206
Chris Wilson05394f32010-11-08 19:18:58 +00002207 i915_gem_flush_ring(dev, ring,
Chris Wilsona56ba562010-09-28 10:07:56 +01002208 I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
2209 return i915_wait_request(dev,
2210 i915_gem_next_request_seqno(dev, ring),
2211 ring);
2212}
2213
Chris Wilsonb47eb4a2010-08-07 11:01:23 +01002214int
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002215i915_gpu_idle(struct drm_device *dev)
2216{
2217 drm_i915_private_t *dev_priv = dev->dev_private;
2218 bool lists_empty;
Zou Nan hai852835f2010-05-21 09:08:56 +08002219 int ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002220
Zou Nan haid1b851f2010-05-21 09:08:57 +08002221 lists_empty = (list_empty(&dev_priv->mm.flushing_list) &&
Chris Wilson395b70b2010-10-28 21:28:46 +01002222 list_empty(&dev_priv->mm.active_list));
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002223 if (lists_empty)
2224 return 0;
2225
2226 /* Flush everything onto the inactive list. */
Chris Wilsona56ba562010-09-28 10:07:56 +01002227 ret = i915_ring_idle(dev, &dev_priv->render_ring);
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002228 if (ret)
2229 return ret;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002230
Chris Wilson87acb0a2010-10-19 10:13:00 +01002231 ret = i915_ring_idle(dev, &dev_priv->bsd_ring);
2232 if (ret)
2233 return ret;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002234
Chris Wilson549f7362010-10-19 11:19:32 +01002235 ret = i915_ring_idle(dev, &dev_priv->blt_ring);
2236 if (ret)
2237 return ret;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002238
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002239 return 0;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002240}
2241
Daniel Vetterc6642782010-11-12 13:46:18 +00002242static int sandybridge_write_fence_reg(struct drm_i915_gem_object *obj,
2243 struct intel_ring_buffer *pipelined)
Eric Anholt4e901fd2009-10-26 16:44:17 -07002244{
Chris Wilson05394f32010-11-08 19:18:58 +00002245 struct drm_device *dev = obj->base.dev;
Eric Anholt4e901fd2009-10-26 16:44:17 -07002246 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00002247 u32 size = obj->gtt_space->size;
2248 int regnum = obj->fence_reg;
Eric Anholt4e901fd2009-10-26 16:44:17 -07002249 uint64_t val;
2250
Chris Wilson05394f32010-11-08 19:18:58 +00002251 val = (uint64_t)((obj->gtt_offset + size - 4096) &
Daniel Vetterc6642782010-11-12 13:46:18 +00002252 0xfffff000) << 32;
Chris Wilson05394f32010-11-08 19:18:58 +00002253 val |= obj->gtt_offset & 0xfffff000;
2254 val |= (uint64_t)((obj->stride / 128) - 1) <<
Eric Anholt4e901fd2009-10-26 16:44:17 -07002255 SANDYBRIDGE_FENCE_PITCH_SHIFT;
2256
Chris Wilson05394f32010-11-08 19:18:58 +00002257 if (obj->tiling_mode == I915_TILING_Y)
Eric Anholt4e901fd2009-10-26 16:44:17 -07002258 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2259 val |= I965_FENCE_REG_VALID;
2260
Daniel Vetterc6642782010-11-12 13:46:18 +00002261 if (pipelined) {
2262 int ret = intel_ring_begin(pipelined, 6);
2263 if (ret)
2264 return ret;
2265
2266 intel_ring_emit(pipelined, MI_NOOP);
2267 intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(2));
2268 intel_ring_emit(pipelined, FENCE_REG_SANDYBRIDGE_0 + regnum*8);
2269 intel_ring_emit(pipelined, (u32)val);
2270 intel_ring_emit(pipelined, FENCE_REG_SANDYBRIDGE_0 + regnum*8 + 4);
2271 intel_ring_emit(pipelined, (u32)(val >> 32));
2272 intel_ring_advance(pipelined);
2273 } else
2274 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + regnum * 8, val);
2275
2276 return 0;
Eric Anholt4e901fd2009-10-26 16:44:17 -07002277}
2278
Daniel Vetterc6642782010-11-12 13:46:18 +00002279static int i965_write_fence_reg(struct drm_i915_gem_object *obj,
2280 struct intel_ring_buffer *pipelined)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002281{
Chris Wilson05394f32010-11-08 19:18:58 +00002282 struct drm_device *dev = obj->base.dev;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002283 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00002284 u32 size = obj->gtt_space->size;
2285 int regnum = obj->fence_reg;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002286 uint64_t val;
2287
Chris Wilson05394f32010-11-08 19:18:58 +00002288 val = (uint64_t)((obj->gtt_offset + size - 4096) &
Jesse Barnesde151cf2008-11-12 10:03:55 -08002289 0xfffff000) << 32;
Chris Wilson05394f32010-11-08 19:18:58 +00002290 val |= obj->gtt_offset & 0xfffff000;
2291 val |= ((obj->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
2292 if (obj->tiling_mode == I915_TILING_Y)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002293 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2294 val |= I965_FENCE_REG_VALID;
2295
Daniel Vetterc6642782010-11-12 13:46:18 +00002296 if (pipelined) {
2297 int ret = intel_ring_begin(pipelined, 6);
2298 if (ret)
2299 return ret;
2300
2301 intel_ring_emit(pipelined, MI_NOOP);
2302 intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(2));
2303 intel_ring_emit(pipelined, FENCE_REG_965_0 + regnum*8);
2304 intel_ring_emit(pipelined, (u32)val);
2305 intel_ring_emit(pipelined, FENCE_REG_965_0 + regnum*8 + 4);
2306 intel_ring_emit(pipelined, (u32)(val >> 32));
2307 intel_ring_advance(pipelined);
2308 } else
2309 I915_WRITE64(FENCE_REG_965_0 + regnum * 8, val);
2310
2311 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002312}
2313
Daniel Vetterc6642782010-11-12 13:46:18 +00002314static int i915_write_fence_reg(struct drm_i915_gem_object *obj,
2315 struct intel_ring_buffer *pipelined)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002316{
Chris Wilson05394f32010-11-08 19:18:58 +00002317 struct drm_device *dev = obj->base.dev;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002318 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00002319 u32 size = obj->gtt_space->size;
Daniel Vetterc6642782010-11-12 13:46:18 +00002320 u32 fence_reg, val, pitch_val;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002321 int tile_width;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002322
Daniel Vetterc6642782010-11-12 13:46:18 +00002323 if (WARN((obj->gtt_offset & ~I915_FENCE_START_MASK) ||
2324 (size & -size) != size ||
2325 (obj->gtt_offset & (size - 1)),
2326 "object 0x%08x [fenceable? %d] not 1M or pot-size (0x%08x) aligned\n",
2327 obj->gtt_offset, obj->map_and_fenceable, size))
2328 return -EINVAL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002329
Daniel Vetterc6642782010-11-12 13:46:18 +00002330 if (obj->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev))
Jesse Barnes0f973f22009-01-26 17:10:45 -08002331 tile_width = 128;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002332 else
Jesse Barnes0f973f22009-01-26 17:10:45 -08002333 tile_width = 512;
2334
2335 /* Note: pitch better be a power of two tile widths */
Chris Wilson05394f32010-11-08 19:18:58 +00002336 pitch_val = obj->stride / tile_width;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002337 pitch_val = ffs(pitch_val) - 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002338
Chris Wilson05394f32010-11-08 19:18:58 +00002339 val = obj->gtt_offset;
2340 if (obj->tiling_mode == I915_TILING_Y)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002341 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002342 val |= I915_FENCE_SIZE_BITS(size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002343 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2344 val |= I830_FENCE_REG_VALID;
2345
Chris Wilson05394f32010-11-08 19:18:58 +00002346 fence_reg = obj->fence_reg;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002347 if (fence_reg < 8)
2348 fence_reg = FENCE_REG_830_0 + fence_reg * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002349 else
Chris Wilsona00b10c2010-09-24 21:15:47 +01002350 fence_reg = FENCE_REG_945_8 + (fence_reg - 8) * 4;
Daniel Vetterc6642782010-11-12 13:46:18 +00002351
2352 if (pipelined) {
2353 int ret = intel_ring_begin(pipelined, 4);
2354 if (ret)
2355 return ret;
2356
2357 intel_ring_emit(pipelined, MI_NOOP);
2358 intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(1));
2359 intel_ring_emit(pipelined, fence_reg);
2360 intel_ring_emit(pipelined, val);
2361 intel_ring_advance(pipelined);
2362 } else
2363 I915_WRITE(fence_reg, val);
2364
2365 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002366}
2367
Daniel Vetterc6642782010-11-12 13:46:18 +00002368static int i830_write_fence_reg(struct drm_i915_gem_object *obj,
2369 struct intel_ring_buffer *pipelined)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002370{
Chris Wilson05394f32010-11-08 19:18:58 +00002371 struct drm_device *dev = obj->base.dev;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002372 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00002373 u32 size = obj->gtt_space->size;
2374 int regnum = obj->fence_reg;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002375 uint32_t val;
2376 uint32_t pitch_val;
2377
Daniel Vetterc6642782010-11-12 13:46:18 +00002378 if (WARN((obj->gtt_offset & ~I830_FENCE_START_MASK) ||
2379 (size & -size) != size ||
2380 (obj->gtt_offset & (size - 1)),
2381 "object 0x%08x not 512K or pot-size 0x%08x aligned\n",
2382 obj->gtt_offset, size))
2383 return -EINVAL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002384
Chris Wilson05394f32010-11-08 19:18:58 +00002385 pitch_val = obj->stride / 128;
Eric Anholte76a16d2009-05-26 17:44:56 -07002386 pitch_val = ffs(pitch_val) - 1;
Eric Anholte76a16d2009-05-26 17:44:56 -07002387
Chris Wilson05394f32010-11-08 19:18:58 +00002388 val = obj->gtt_offset;
2389 if (obj->tiling_mode == I915_TILING_Y)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002390 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Daniel Vetterc6642782010-11-12 13:46:18 +00002391 val |= I830_FENCE_SIZE_BITS(size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002392 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2393 val |= I830_FENCE_REG_VALID;
2394
Daniel Vetterc6642782010-11-12 13:46:18 +00002395 if (pipelined) {
2396 int ret = intel_ring_begin(pipelined, 4);
2397 if (ret)
2398 return ret;
2399
2400 intel_ring_emit(pipelined, MI_NOOP);
2401 intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(1));
2402 intel_ring_emit(pipelined, FENCE_REG_830_0 + regnum*4);
2403 intel_ring_emit(pipelined, val);
2404 intel_ring_advance(pipelined);
2405 } else
2406 I915_WRITE(FENCE_REG_830_0 + regnum * 4, val);
2407
2408 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002409}
2410
Chris Wilson2cf34d72010-09-14 13:03:28 +01002411static int i915_find_fence_reg(struct drm_device *dev,
2412 bool interruptible)
Daniel Vetterae3db242010-02-19 11:51:58 +01002413{
Daniel Vetterae3db242010-02-19 11:51:58 +01002414 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002415 struct drm_i915_fence_reg *reg;
Chris Wilson05394f32010-11-08 19:18:58 +00002416 struct drm_i915_gem_object *obj = NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002417 int i, avail, ret;
2418
2419 /* First try to find a free reg */
2420 avail = 0;
2421 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2422 reg = &dev_priv->fence_regs[i];
2423 if (!reg->obj)
2424 return i;
2425
Chris Wilson05394f32010-11-08 19:18:58 +00002426 if (!reg->obj->pin_count)
2427 avail++;
Daniel Vetterae3db242010-02-19 11:51:58 +01002428 }
2429
2430 if (avail == 0)
2431 return -ENOSPC;
2432
2433 /* None available, try to steal one or wait for a user to finish */
Chris Wilsona00b10c2010-09-24 21:15:47 +01002434 avail = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002435 list_for_each_entry(reg, &dev_priv->mm.fence_list,
2436 lru_list) {
Chris Wilson05394f32010-11-08 19:18:58 +00002437 obj = reg->obj;
2438 if (obj->pin_count)
Daniel Vetterae3db242010-02-19 11:51:58 +01002439 continue;
2440
2441 /* found one! */
Chris Wilson05394f32010-11-08 19:18:58 +00002442 avail = obj->fence_reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01002443 break;
2444 }
2445
Chris Wilsona00b10c2010-09-24 21:15:47 +01002446 BUG_ON(avail == I915_FENCE_REG_NONE);
Daniel Vetterae3db242010-02-19 11:51:58 +01002447
2448 /* We only have a reference on obj from the active list. put_fence_reg
2449 * might drop that one, causing a use-after-free in it. So hold a
2450 * private reference to obj like the other callers of put_fence_reg
2451 * (set_tiling ioctl) do. */
Chris Wilson05394f32010-11-08 19:18:58 +00002452 drm_gem_object_reference(&obj->base);
2453 ret = i915_gem_object_put_fence_reg(obj, interruptible);
2454 drm_gem_object_unreference(&obj->base);
Daniel Vetterae3db242010-02-19 11:51:58 +01002455 if (ret != 0)
2456 return ret;
2457
Chris Wilsona00b10c2010-09-24 21:15:47 +01002458 return avail;
Daniel Vetterae3db242010-02-19 11:51:58 +01002459}
2460
Jesse Barnesde151cf2008-11-12 10:03:55 -08002461/**
2462 * i915_gem_object_get_fence_reg - set up a fence reg for an object
2463 * @obj: object to map through a fence reg
2464 *
2465 * When mapping objects through the GTT, userspace wants to be able to write
2466 * to them without having to worry about swizzling if the object is tiled.
2467 *
2468 * This function walks the fence regs looking for a free one for @obj,
2469 * stealing one if it can't find any.
2470 *
2471 * It then sets up the reg based on the object's properties: address, pitch
2472 * and tiling format.
2473 */
Chris Wilson8c4b8c32009-06-17 22:08:52 +01002474int
Chris Wilson05394f32010-11-08 19:18:58 +00002475i915_gem_object_get_fence_reg(struct drm_i915_gem_object *obj,
Chris Wilson2cf34d72010-09-14 13:03:28 +01002476 bool interruptible)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002477{
Chris Wilson05394f32010-11-08 19:18:58 +00002478 struct drm_device *dev = obj->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002479 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002480 struct drm_i915_fence_reg *reg = NULL;
Daniel Vetterc6642782010-11-12 13:46:18 +00002481 struct intel_ring_buffer *pipelined = NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002482 int ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002483
Eric Anholta09ba7f2009-08-29 12:49:51 -07002484 /* Just update our place in the LRU if our fence is getting used. */
Chris Wilson05394f32010-11-08 19:18:58 +00002485 if (obj->fence_reg != I915_FENCE_REG_NONE) {
2486 reg = &dev_priv->fence_regs[obj->fence_reg];
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002487 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002488 return 0;
2489 }
2490
Chris Wilson05394f32010-11-08 19:18:58 +00002491 switch (obj->tiling_mode) {
Jesse Barnesde151cf2008-11-12 10:03:55 -08002492 case I915_TILING_NONE:
2493 WARN(1, "allocating a fence for non-tiled object?\n");
2494 break;
2495 case I915_TILING_X:
Chris Wilson05394f32010-11-08 19:18:58 +00002496 if (!obj->stride)
Jesse Barnes0f973f22009-01-26 17:10:45 -08002497 return -EINVAL;
Chris Wilson05394f32010-11-08 19:18:58 +00002498 WARN((obj->stride & (512 - 1)),
Jesse Barnes0f973f22009-01-26 17:10:45 -08002499 "object 0x%08x is X tiled but has non-512B pitch\n",
Chris Wilson05394f32010-11-08 19:18:58 +00002500 obj->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002501 break;
2502 case I915_TILING_Y:
Chris Wilson05394f32010-11-08 19:18:58 +00002503 if (!obj->stride)
Jesse Barnes0f973f22009-01-26 17:10:45 -08002504 return -EINVAL;
Chris Wilson05394f32010-11-08 19:18:58 +00002505 WARN((obj->stride & (128 - 1)),
Jesse Barnes0f973f22009-01-26 17:10:45 -08002506 "object 0x%08x is Y tiled but has non-128B pitch\n",
Chris Wilson05394f32010-11-08 19:18:58 +00002507 obj->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002508 break;
2509 }
2510
Chris Wilson2cf34d72010-09-14 13:03:28 +01002511 ret = i915_find_fence_reg(dev, interruptible);
Daniel Vetterae3db242010-02-19 11:51:58 +01002512 if (ret < 0)
2513 return ret;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002514
Chris Wilson05394f32010-11-08 19:18:58 +00002515 obj->fence_reg = ret;
2516 reg = &dev_priv->fence_regs[obj->fence_reg];
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002517 list_add_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002518
Jesse Barnesde151cf2008-11-12 10:03:55 -08002519 reg->obj = obj;
2520
Chris Wilsone259bef2010-09-17 00:32:02 +01002521 switch (INTEL_INFO(dev)->gen) {
2522 case 6:
Daniel Vetterc6642782010-11-12 13:46:18 +00002523 ret = sandybridge_write_fence_reg(obj, pipelined);
Chris Wilsone259bef2010-09-17 00:32:02 +01002524 break;
2525 case 5:
2526 case 4:
Daniel Vetterc6642782010-11-12 13:46:18 +00002527 ret = i965_write_fence_reg(obj, pipelined);
Chris Wilsone259bef2010-09-17 00:32:02 +01002528 break;
2529 case 3:
Daniel Vetterc6642782010-11-12 13:46:18 +00002530 ret = i915_write_fence_reg(obj, pipelined);
Chris Wilsone259bef2010-09-17 00:32:02 +01002531 break;
2532 case 2:
Daniel Vetterc6642782010-11-12 13:46:18 +00002533 ret = i830_write_fence_reg(obj, pipelined);
Chris Wilsone259bef2010-09-17 00:32:02 +01002534 break;
2535 }
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002536
Chris Wilsona00b10c2010-09-24 21:15:47 +01002537 trace_i915_gem_object_get_fence(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00002538 obj->fence_reg,
2539 obj->tiling_mode);
Daniel Vetterc6642782010-11-12 13:46:18 +00002540 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002541}
2542
2543/**
2544 * i915_gem_clear_fence_reg - clear out fence register info
2545 * @obj: object to clear
2546 *
2547 * Zeroes out the fence register itself and clears out the associated
Chris Wilson05394f32010-11-08 19:18:58 +00002548 * data structures in dev_priv and obj.
Jesse Barnesde151cf2008-11-12 10:03:55 -08002549 */
2550static void
Chris Wilson05394f32010-11-08 19:18:58 +00002551i915_gem_clear_fence_reg(struct drm_i915_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002552{
Chris Wilson05394f32010-11-08 19:18:58 +00002553 struct drm_device *dev = obj->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002554 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00002555 struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[obj->fence_reg];
Chris Wilsone259bef2010-09-17 00:32:02 +01002556 uint32_t fence_reg;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002557
Chris Wilsone259bef2010-09-17 00:32:02 +01002558 switch (INTEL_INFO(dev)->gen) {
2559 case 6:
Eric Anholt4e901fd2009-10-26 16:44:17 -07002560 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 +
Chris Wilson05394f32010-11-08 19:18:58 +00002561 (obj->fence_reg * 8), 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002562 break;
2563 case 5:
2564 case 4:
Chris Wilson05394f32010-11-08 19:18:58 +00002565 I915_WRITE64(FENCE_REG_965_0 + (obj->fence_reg * 8), 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002566 break;
2567 case 3:
Chris Wilson05394f32010-11-08 19:18:58 +00002568 if (obj->fence_reg >= 8)
2569 fence_reg = FENCE_REG_945_8 + (obj->fence_reg - 8) * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002570 else
Chris Wilsone259bef2010-09-17 00:32:02 +01002571 case 2:
Chris Wilson05394f32010-11-08 19:18:58 +00002572 fence_reg = FENCE_REG_830_0 + obj->fence_reg * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002573
2574 I915_WRITE(fence_reg, 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002575 break;
Eric Anholtdc529a42009-03-10 22:34:49 -07002576 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002577
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002578 reg->obj = NULL;
Chris Wilson05394f32010-11-08 19:18:58 +00002579 obj->fence_reg = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002580 list_del_init(&reg->lru_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002581}
2582
Eric Anholt673a3942008-07-30 12:06:12 -07002583/**
Chris Wilson52dc7d32009-06-06 09:46:01 +01002584 * i915_gem_object_put_fence_reg - waits on outstanding fenced access
2585 * to the buffer to finish, and then resets the fence register.
2586 * @obj: tiled object holding a fence register.
Chris Wilson2cf34d72010-09-14 13:03:28 +01002587 * @bool: whether the wait upon the fence is interruptible
Chris Wilson52dc7d32009-06-06 09:46:01 +01002588 *
2589 * Zeroes out the fence register itself and clears out the associated
Chris Wilson05394f32010-11-08 19:18:58 +00002590 * data structures in dev_priv and obj.
Chris Wilson52dc7d32009-06-06 09:46:01 +01002591 */
2592int
Chris Wilson05394f32010-11-08 19:18:58 +00002593i915_gem_object_put_fence_reg(struct drm_i915_gem_object *obj,
Chris Wilson2cf34d72010-09-14 13:03:28 +01002594 bool interruptible)
Chris Wilson52dc7d32009-06-06 09:46:01 +01002595{
Chris Wilson05394f32010-11-08 19:18:58 +00002596 struct drm_device *dev = obj->base.dev;
Chris Wilsoncaea7472010-11-12 13:53:37 +00002597 int ret;
Chris Wilson52dc7d32009-06-06 09:46:01 +01002598
Chris Wilson05394f32010-11-08 19:18:58 +00002599 if (obj->fence_reg == I915_FENCE_REG_NONE)
Chris Wilson52dc7d32009-06-06 09:46:01 +01002600 return 0;
2601
Daniel Vetter10ae9bd2010-02-01 13:59:17 +01002602 /* If we've changed tiling, GTT-mappings of the object
2603 * need to re-fault to ensure that the correct fence register
2604 * setup is in place.
2605 */
2606 i915_gem_release_mmap(obj);
2607
Chris Wilson52dc7d32009-06-06 09:46:01 +01002608 /* On the i915, GPU access to tiled buffers is via a fence,
2609 * therefore we must wait for any outstanding access to complete
2610 * before clearing the fence.
2611 */
Chris Wilsoncaea7472010-11-12 13:53:37 +00002612 if (obj->fenced_gpu_access) {
Chris Wilson919926a2010-11-12 13:42:53 +00002613 ret = i915_gem_object_flush_gpu_write_domain(obj, NULL);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002614 if (ret)
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002615 return ret;
2616
Chris Wilsoncaea7472010-11-12 13:53:37 +00002617 obj->fenced_gpu_access = false;
2618 }
2619
2620 if (obj->last_fenced_seqno) {
2621 ret = i915_do_wait_request(dev,
2622 obj->last_fenced_seqno,
2623 interruptible,
2624 obj->last_fenced_ring);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002625 if (ret)
Chris Wilson52dc7d32009-06-06 09:46:01 +01002626 return ret;
Chris Wilson53640e12010-09-20 11:40:50 +01002627
Chris Wilsoncaea7472010-11-12 13:53:37 +00002628 obj->last_fenced_seqno = false;
Chris Wilson52dc7d32009-06-06 09:46:01 +01002629 }
2630
Daniel Vetter4a726612010-02-01 13:59:16 +01002631 i915_gem_object_flush_gtt_write_domain(obj);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002632 i915_gem_clear_fence_reg(obj);
Chris Wilson52dc7d32009-06-06 09:46:01 +01002633
2634 return 0;
2635}
2636
2637/**
Eric Anholt673a3942008-07-30 12:06:12 -07002638 * Finds free space in the GTT aperture and binds the object there.
2639 */
2640static int
Chris Wilson05394f32010-11-08 19:18:58 +00002641i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
Daniel Vetter920afa72010-09-16 17:54:23 +02002642 unsigned alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01002643 bool map_and_fenceable)
Eric Anholt673a3942008-07-30 12:06:12 -07002644{
Chris Wilson05394f32010-11-08 19:18:58 +00002645 struct drm_device *dev = obj->base.dev;
Eric Anholt673a3942008-07-30 12:06:12 -07002646 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07002647 struct drm_mm_node *free_space;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002648 gfp_t gfpmask = __GFP_NORETRY | __GFP_NOWARN;
Daniel Vetter5e783302010-11-14 22:32:36 +01002649 u32 size, fence_size, fence_alignment, unfenced_alignment;
Daniel Vetter75e9e912010-11-04 17:11:09 +01002650 bool mappable, fenceable;
Chris Wilson07f73f62009-09-14 16:50:30 +01002651 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002652
Chris Wilson05394f32010-11-08 19:18:58 +00002653 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01002654 DRM_ERROR("Attempting to bind a purgeable object\n");
2655 return -EINVAL;
2656 }
2657
Chris Wilson05394f32010-11-08 19:18:58 +00002658 fence_size = i915_gem_get_gtt_size(obj);
2659 fence_alignment = i915_gem_get_gtt_alignment(obj);
2660 unfenced_alignment = i915_gem_get_unfenced_gtt_alignment(obj);
Chris Wilsona00b10c2010-09-24 21:15:47 +01002661
Eric Anholt673a3942008-07-30 12:06:12 -07002662 if (alignment == 0)
Daniel Vetter5e783302010-11-14 22:32:36 +01002663 alignment = map_and_fenceable ? fence_alignment :
2664 unfenced_alignment;
Daniel Vetter75e9e912010-11-04 17:11:09 +01002665 if (map_and_fenceable && alignment & (fence_alignment - 1)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002666 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2667 return -EINVAL;
2668 }
2669
Chris Wilson05394f32010-11-08 19:18:58 +00002670 size = map_and_fenceable ? fence_size : obj->base.size;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002671
Chris Wilson654fc602010-05-27 13:18:21 +01002672 /* If the object is bigger than the entire aperture, reject it early
2673 * before evicting everything in a vain attempt to find space.
2674 */
Chris Wilson05394f32010-11-08 19:18:58 +00002675 if (obj->base.size >
Daniel Vetter75e9e912010-11-04 17:11:09 +01002676 (map_and_fenceable ? dev_priv->mm.gtt_mappable_end : dev_priv->mm.gtt_total)) {
Chris Wilson654fc602010-05-27 13:18:21 +01002677 DRM_ERROR("Attempting to bind an object larger than the aperture\n");
2678 return -E2BIG;
2679 }
2680
Eric Anholt673a3942008-07-30 12:06:12 -07002681 search_free:
Daniel Vetter75e9e912010-11-04 17:11:09 +01002682 if (map_and_fenceable)
Daniel Vetter920afa72010-09-16 17:54:23 +02002683 free_space =
2684 drm_mm_search_free_in_range(&dev_priv->mm.gtt_space,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002685 size, alignment, 0,
Daniel Vetter920afa72010-09-16 17:54:23 +02002686 dev_priv->mm.gtt_mappable_end,
2687 0);
2688 else
2689 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002690 size, alignment, 0);
Daniel Vetter920afa72010-09-16 17:54:23 +02002691
2692 if (free_space != NULL) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01002693 if (map_and_fenceable)
Chris Wilson05394f32010-11-08 19:18:58 +00002694 obj->gtt_space =
Daniel Vetter920afa72010-09-16 17:54:23 +02002695 drm_mm_get_block_range_generic(free_space,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002696 size, alignment, 0,
Daniel Vetter920afa72010-09-16 17:54:23 +02002697 dev_priv->mm.gtt_mappable_end,
2698 0);
2699 else
Chris Wilson05394f32010-11-08 19:18:58 +00002700 obj->gtt_space =
Chris Wilsona00b10c2010-09-24 21:15:47 +01002701 drm_mm_get_block(free_space, size, alignment);
Daniel Vetter920afa72010-09-16 17:54:23 +02002702 }
Chris Wilson05394f32010-11-08 19:18:58 +00002703 if (obj->gtt_space == NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -07002704 /* If the gtt is empty and we're still having trouble
2705 * fitting our object in, we're out of memory.
2706 */
Daniel Vetter75e9e912010-11-04 17:11:09 +01002707 ret = i915_gem_evict_something(dev, size, alignment,
2708 map_and_fenceable);
Chris Wilson97311292009-09-21 00:22:34 +01002709 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002710 return ret;
Chris Wilson97311292009-09-21 00:22:34 +01002711
Eric Anholt673a3942008-07-30 12:06:12 -07002712 goto search_free;
2713 }
2714
Chris Wilsone5281cc2010-10-28 13:45:36 +01002715 ret = i915_gem_object_get_pages_gtt(obj, gfpmask);
Eric Anholt673a3942008-07-30 12:06:12 -07002716 if (ret) {
Chris Wilson05394f32010-11-08 19:18:58 +00002717 drm_mm_put_block(obj->gtt_space);
2718 obj->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002719
2720 if (ret == -ENOMEM) {
2721 /* first try to clear up some space from the GTT */
Chris Wilsona00b10c2010-09-24 21:15:47 +01002722 ret = i915_gem_evict_something(dev, size,
Daniel Vetter75e9e912010-11-04 17:11:09 +01002723 alignment,
2724 map_and_fenceable);
Chris Wilson07f73f62009-09-14 16:50:30 +01002725 if (ret) {
Chris Wilson07f73f62009-09-14 16:50:30 +01002726 /* now try to shrink everyone else */
Chris Wilson4bdadb92010-01-27 13:36:32 +00002727 if (gfpmask) {
2728 gfpmask = 0;
2729 goto search_free;
Chris Wilson07f73f62009-09-14 16:50:30 +01002730 }
2731
2732 return ret;
2733 }
2734
2735 goto search_free;
2736 }
2737
Eric Anholt673a3942008-07-30 12:06:12 -07002738 return ret;
2739 }
2740
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002741 ret = i915_gem_gtt_bind_object(obj);
2742 if (ret) {
Chris Wilsone5281cc2010-10-28 13:45:36 +01002743 i915_gem_object_put_pages_gtt(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00002744 drm_mm_put_block(obj->gtt_space);
2745 obj->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002746
Chris Wilsona00b10c2010-09-24 21:15:47 +01002747 ret = i915_gem_evict_something(dev, size,
Daniel Vetter75e9e912010-11-04 17:11:09 +01002748 alignment, map_and_fenceable);
Chris Wilson97311292009-09-21 00:22:34 +01002749 if (ret)
Chris Wilson07f73f62009-09-14 16:50:30 +01002750 return ret;
Chris Wilson07f73f62009-09-14 16:50:30 +01002751
2752 goto search_free;
Eric Anholt673a3942008-07-30 12:06:12 -07002753 }
Eric Anholt673a3942008-07-30 12:06:12 -07002754
Chris Wilson6299f992010-11-24 12:23:44 +00002755 list_add_tail(&obj->gtt_list, &dev_priv->mm.gtt_list);
Chris Wilson05394f32010-11-08 19:18:58 +00002756 list_add_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
Chris Wilsonbf1a1092010-08-07 11:01:20 +01002757
Eric Anholt673a3942008-07-30 12:06:12 -07002758 /* Assert that the object is not currently in any GPU domain. As it
2759 * wasn't in the GTT, there shouldn't be any way it could have been in
2760 * a GPU cache
2761 */
Chris Wilson05394f32010-11-08 19:18:58 +00002762 BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
2763 BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
Eric Anholt673a3942008-07-30 12:06:12 -07002764
Chris Wilson6299f992010-11-24 12:23:44 +00002765 obj->gtt_offset = obj->gtt_space->start;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002766
Daniel Vetter75e9e912010-11-04 17:11:09 +01002767 fenceable =
Chris Wilson05394f32010-11-08 19:18:58 +00002768 obj->gtt_space->size == fence_size &&
2769 (obj->gtt_space->start & (fence_alignment -1)) == 0;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002770
Daniel Vetter75e9e912010-11-04 17:11:09 +01002771 mappable =
Chris Wilson05394f32010-11-08 19:18:58 +00002772 obj->gtt_offset + obj->base.size <= dev_priv->mm.gtt_mappable_end;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002773
Chris Wilson05394f32010-11-08 19:18:58 +00002774 obj->map_and_fenceable = mappable && fenceable;
Daniel Vetter75e9e912010-11-04 17:11:09 +01002775
Chris Wilson6299f992010-11-24 12:23:44 +00002776 trace_i915_gem_object_bind(obj, obj->gtt_offset, map_and_fenceable);
Eric Anholt673a3942008-07-30 12:06:12 -07002777 return 0;
2778}
2779
2780void
Chris Wilson05394f32010-11-08 19:18:58 +00002781i915_gem_clflush_object(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002782{
Eric Anholt673a3942008-07-30 12:06:12 -07002783 /* If we don't have a page list set up, then we're not pinned
2784 * to GPU, and we can ignore the cache flush because it'll happen
2785 * again at bind time.
2786 */
Chris Wilson05394f32010-11-08 19:18:58 +00002787 if (obj->pages == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002788 return;
2789
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002790 trace_i915_gem_object_clflush(obj);
Eric Anholtcfa16a02009-05-26 18:46:16 -07002791
Chris Wilson05394f32010-11-08 19:18:58 +00002792 drm_clflush_pages(obj->pages, obj->base.size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002793}
2794
Eric Anholte47c68e2008-11-14 13:35:19 -08002795/** Flushes any GPU write domain for the object if it's dirty. */
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002796static int
Chris Wilson05394f32010-11-08 19:18:58 +00002797i915_gem_object_flush_gpu_write_domain(struct drm_i915_gem_object *obj,
Chris Wilson919926a2010-11-12 13:42:53 +00002798 struct intel_ring_buffer *pipelined)
Eric Anholte47c68e2008-11-14 13:35:19 -08002799{
Chris Wilson05394f32010-11-08 19:18:58 +00002800 struct drm_device *dev = obj->base.dev;
Eric Anholte47c68e2008-11-14 13:35:19 -08002801
Chris Wilson05394f32010-11-08 19:18:58 +00002802 if ((obj->base.write_domain & I915_GEM_GPU_DOMAINS) == 0)
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002803 return 0;
Eric Anholte47c68e2008-11-14 13:35:19 -08002804
2805 /* Queue the GPU write cache flushing we need. */
Chris Wilson05394f32010-11-08 19:18:58 +00002806 i915_gem_flush_ring(dev, obj->ring, 0, obj->base.write_domain);
2807 BUG_ON(obj->base.write_domain);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002808
Chris Wilson919926a2010-11-12 13:42:53 +00002809 if (pipelined && pipelined == obj->ring)
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002810 return 0;
2811
Chris Wilson2cf34d72010-09-14 13:03:28 +01002812 return i915_gem_object_wait_rendering(obj, true);
Eric Anholte47c68e2008-11-14 13:35:19 -08002813}
2814
2815/** Flushes the GTT write domain for the object if it's dirty. */
2816static void
Chris Wilson05394f32010-11-08 19:18:58 +00002817i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08002818{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002819 uint32_t old_write_domain;
2820
Chris Wilson05394f32010-11-08 19:18:58 +00002821 if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
Eric Anholte47c68e2008-11-14 13:35:19 -08002822 return;
2823
2824 /* No actual flushing is required for the GTT write domain. Writes
2825 * to it immediately go to main memory as far as we know, so there's
2826 * no chipset flush. It also doesn't land in render cache.
2827 */
Chris Wilson4a684a42010-10-28 14:44:08 +01002828 i915_gem_release_mmap(obj);
2829
Chris Wilson05394f32010-11-08 19:18:58 +00002830 old_write_domain = obj->base.write_domain;
2831 obj->base.write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002832
2833 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00002834 obj->base.read_domains,
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002835 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002836}
2837
2838/** Flushes the CPU write domain for the object if it's dirty. */
2839static void
Chris Wilson05394f32010-11-08 19:18:58 +00002840i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08002841{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002842 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002843
Chris Wilson05394f32010-11-08 19:18:58 +00002844 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
Eric Anholte47c68e2008-11-14 13:35:19 -08002845 return;
2846
2847 i915_gem_clflush_object(obj);
Daniel Vetter40ce6572010-11-05 18:12:18 +01002848 intel_gtt_chipset_flush();
Chris Wilson05394f32010-11-08 19:18:58 +00002849 old_write_domain = obj->base.write_domain;
2850 obj->base.write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002851
2852 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00002853 obj->base.read_domains,
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002854 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002855}
2856
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002857/**
2858 * Moves a single object to the GTT read, and possibly write domain.
2859 *
2860 * This function returns when the move is complete, including waiting on
2861 * flushes to occur.
2862 */
Jesse Barnes79e53942008-11-07 14:24:08 -08002863int
Chris Wilson20217462010-11-23 15:26:33 +00002864i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002865{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002866 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002867 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002868
Eric Anholt02354392008-11-26 13:58:13 -08002869 /* Not valid to be called on unbound objects. */
Chris Wilson05394f32010-11-08 19:18:58 +00002870 if (obj->gtt_space == NULL)
Eric Anholt02354392008-11-26 13:58:13 -08002871 return -EINVAL;
2872
Chris Wilson919926a2010-11-12 13:42:53 +00002873 ret = i915_gem_object_flush_gpu_write_domain(obj, NULL);
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002874 if (ret != 0)
2875 return ret;
2876
Chris Wilson72133422010-09-13 23:56:38 +01002877 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002878
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002879 if (write) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01002880 ret = i915_gem_object_wait_rendering(obj, true);
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002881 if (ret)
2882 return ret;
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002883 }
Eric Anholte47c68e2008-11-14 13:35:19 -08002884
Chris Wilson05394f32010-11-08 19:18:58 +00002885 old_write_domain = obj->base.write_domain;
2886 old_read_domains = obj->base.read_domains;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002887
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002888 /* It should now be out of any other write domains, and we can update
2889 * the domain values for our changes.
2890 */
Chris Wilson05394f32010-11-08 19:18:58 +00002891 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2892 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002893 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00002894 obj->base.read_domains = I915_GEM_DOMAIN_GTT;
2895 obj->base.write_domain = I915_GEM_DOMAIN_GTT;
2896 obj->dirty = 1;
Eric Anholte47c68e2008-11-14 13:35:19 -08002897 }
2898
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002899 trace_i915_gem_object_change_domain(obj,
2900 old_read_domains,
2901 old_write_domain);
2902
Eric Anholte47c68e2008-11-14 13:35:19 -08002903 return 0;
2904}
2905
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002906/*
2907 * Prepare buffer for display plane. Use uninterruptible for possible flush
2908 * wait, as in modesetting process we're not supposed to be interrupted.
2909 */
2910int
Chris Wilson05394f32010-11-08 19:18:58 +00002911i915_gem_object_set_to_display_plane(struct drm_i915_gem_object *obj,
Chris Wilson919926a2010-11-12 13:42:53 +00002912 struct intel_ring_buffer *pipelined)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002913{
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002914 uint32_t old_read_domains;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002915 int ret;
2916
2917 /* Not valid to be called on unbound objects. */
Chris Wilson05394f32010-11-08 19:18:58 +00002918 if (obj->gtt_space == NULL)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002919 return -EINVAL;
2920
Chris Wilson919926a2010-11-12 13:42:53 +00002921 ret = i915_gem_object_flush_gpu_write_domain(obj, pipelined);
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002922 if (ret)
2923 return ret;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002924
Chris Wilsonced270f2010-09-26 22:47:46 +01002925 /* Currently, we are always called from an non-interruptible context. */
2926 if (!pipelined) {
2927 ret = i915_gem_object_wait_rendering(obj, false);
2928 if (ret)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002929 return ret;
2930 }
2931
Chris Wilsonb118c1e2010-05-27 13:18:14 +01002932 i915_gem_object_flush_cpu_write_domain(obj);
2933
Chris Wilson05394f32010-11-08 19:18:58 +00002934 old_read_domains = obj->base.read_domains;
2935 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002936
2937 trace_i915_gem_object_change_domain(obj,
2938 old_read_domains,
Chris Wilson05394f32010-11-08 19:18:58 +00002939 obj->base.write_domain);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002940
2941 return 0;
2942}
2943
Chris Wilson85345512010-11-13 09:49:11 +00002944int
2945i915_gem_object_flush_gpu(struct drm_i915_gem_object *obj,
2946 bool interruptible)
2947{
2948 if (!obj->active)
2949 return 0;
2950
2951 if (obj->base.write_domain & I915_GEM_GPU_DOMAINS)
Chris Wilson05394f32010-11-08 19:18:58 +00002952 i915_gem_flush_ring(obj->base.dev, obj->ring,
Chris Wilson85345512010-11-13 09:49:11 +00002953 0, obj->base.write_domain);
2954
Chris Wilson05394f32010-11-08 19:18:58 +00002955 return i915_gem_object_wait_rendering(obj, interruptible);
Chris Wilson85345512010-11-13 09:49:11 +00002956}
2957
Eric Anholte47c68e2008-11-14 13:35:19 -08002958/**
2959 * Moves a single object to the CPU read, and possibly write domain.
2960 *
2961 * This function returns when the move is complete, including waiting on
2962 * flushes to occur.
2963 */
2964static int
Chris Wilson919926a2010-11-12 13:42:53 +00002965i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholte47c68e2008-11-14 13:35:19 -08002966{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002967 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002968 int ret;
2969
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002970 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Eric Anholte47c68e2008-11-14 13:35:19 -08002971 if (ret != 0)
2972 return ret;
2973
2974 i915_gem_object_flush_gtt_write_domain(obj);
2975
2976 /* If we have a partially-valid cache of the object in the CPU,
2977 * finish invalidating it and free the per-page flags.
2978 */
2979 i915_gem_object_set_to_full_cpu_read_domain(obj);
2980
Chris Wilson72133422010-09-13 23:56:38 +01002981 if (write) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01002982 ret = i915_gem_object_wait_rendering(obj, true);
Chris Wilson72133422010-09-13 23:56:38 +01002983 if (ret)
2984 return ret;
2985 }
2986
Chris Wilson05394f32010-11-08 19:18:58 +00002987 old_write_domain = obj->base.write_domain;
2988 old_read_domains = obj->base.read_domains;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002989
Eric Anholte47c68e2008-11-14 13:35:19 -08002990 /* Flush the CPU cache if it's still invalid. */
Chris Wilson05394f32010-11-08 19:18:58 +00002991 if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
Eric Anholte47c68e2008-11-14 13:35:19 -08002992 i915_gem_clflush_object(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08002993
Chris Wilson05394f32010-11-08 19:18:58 +00002994 obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08002995 }
2996
2997 /* It should now be out of any other write domains, and we can update
2998 * the domain values for our changes.
2999 */
Chris Wilson05394f32010-11-08 19:18:58 +00003000 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
Eric Anholte47c68e2008-11-14 13:35:19 -08003001
3002 /* If we're writing through the CPU, then the GPU read domains will
3003 * need to be invalidated at next use.
3004 */
3005 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00003006 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3007 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003008 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003009
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003010 trace_i915_gem_object_change_domain(obj,
3011 old_read_domains,
3012 old_write_domain);
3013
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003014 return 0;
3015}
3016
Eric Anholt673a3942008-07-30 12:06:12 -07003017/*
3018 * Set the next domain for the specified object. This
3019 * may not actually perform the necessary flushing/invaliding though,
3020 * as that may want to be batched with other set_domain operations
3021 *
3022 * This is (we hope) the only really tricky part of gem. The goal
3023 * is fairly simple -- track which caches hold bits of the object
3024 * and make sure they remain coherent. A few concrete examples may
3025 * help to explain how it works. For shorthand, we use the notation
3026 * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
3027 * a pair of read and write domain masks.
3028 *
3029 * Case 1: the batch buffer
3030 *
3031 * 1. Allocated
3032 * 2. Written by CPU
3033 * 3. Mapped to GTT
3034 * 4. Read by GPU
3035 * 5. Unmapped from GTT
3036 * 6. Freed
3037 *
3038 * Let's take these a step at a time
3039 *
3040 * 1. Allocated
3041 * Pages allocated from the kernel may still have
3042 * cache contents, so we set them to (CPU, CPU) always.
3043 * 2. Written by CPU (using pwrite)
3044 * The pwrite function calls set_domain (CPU, CPU) and
3045 * this function does nothing (as nothing changes)
3046 * 3. Mapped by GTT
3047 * This function asserts that the object is not
3048 * currently in any GPU-based read or write domains
3049 * 4. Read by GPU
3050 * i915_gem_execbuffer calls set_domain (COMMAND, 0).
3051 * As write_domain is zero, this function adds in the
3052 * current read domains (CPU+COMMAND, 0).
3053 * flush_domains is set to CPU.
3054 * invalidate_domains is set to COMMAND
3055 * clflush is run to get data out of the CPU caches
3056 * then i915_dev_set_domain calls i915_gem_flush to
3057 * emit an MI_FLUSH and drm_agp_chipset_flush
3058 * 5. Unmapped from GTT
3059 * i915_gem_object_unbind calls set_domain (CPU, CPU)
3060 * flush_domains and invalidate_domains end up both zero
3061 * so no flushing/invalidating happens
3062 * 6. Freed
3063 * yay, done
3064 *
3065 * Case 2: The shared render buffer
3066 *
3067 * 1. Allocated
3068 * 2. Mapped to GTT
3069 * 3. Read/written by GPU
3070 * 4. set_domain to (CPU,CPU)
3071 * 5. Read/written by CPU
3072 * 6. Read/written by GPU
3073 *
3074 * 1. Allocated
3075 * Same as last example, (CPU, CPU)
3076 * 2. Mapped to GTT
3077 * Nothing changes (assertions find that it is not in the GPU)
3078 * 3. Read/written by GPU
3079 * execbuffer calls set_domain (RENDER, RENDER)
3080 * flush_domains gets CPU
3081 * invalidate_domains gets GPU
3082 * clflush (obj)
3083 * MI_FLUSH and drm_agp_chipset_flush
3084 * 4. set_domain (CPU, CPU)
3085 * flush_domains gets GPU
3086 * invalidate_domains gets CPU
3087 * wait_rendering (obj) to make sure all drawing is complete.
3088 * This will include an MI_FLUSH to get the data from GPU
3089 * to memory
3090 * clflush (obj) to invalidate the CPU cache
3091 * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
3092 * 5. Read/written by CPU
3093 * cache lines are loaded and dirtied
3094 * 6. Read written by GPU
3095 * Same as last GPU access
3096 *
3097 * Case 3: The constant buffer
3098 *
3099 * 1. Allocated
3100 * 2. Written by CPU
3101 * 3. Read by GPU
3102 * 4. Updated (written) by CPU again
3103 * 5. Read by GPU
3104 *
3105 * 1. Allocated
3106 * (CPU, CPU)
3107 * 2. Written by CPU
3108 * (CPU, CPU)
3109 * 3. Read by GPU
3110 * (CPU+RENDER, 0)
3111 * flush_domains = CPU
3112 * invalidate_domains = RENDER
3113 * clflush (obj)
3114 * MI_FLUSH
3115 * drm_agp_chipset_flush
3116 * 4. Updated (written) by CPU again
3117 * (CPU, CPU)
3118 * flush_domains = 0 (no previous write domain)
3119 * invalidate_domains = 0 (no new read domains)
3120 * 5. Read by GPU
3121 * (CPU+RENDER, 0)
3122 * flush_domains = CPU
3123 * invalidate_domains = RENDER
3124 * clflush (obj)
3125 * MI_FLUSH
3126 * drm_agp_chipset_flush
3127 */
Keith Packardc0d90822008-11-20 23:11:08 -08003128static void
Chris Wilson05394f32010-11-08 19:18:58 +00003129i915_gem_object_set_to_gpu_domain(struct drm_i915_gem_object *obj,
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003130 struct intel_ring_buffer *ring,
3131 struct change_domains *cd)
Eric Anholt673a3942008-07-30 12:06:12 -07003132{
Chris Wilson05394f32010-11-08 19:18:58 +00003133 uint32_t invalidate_domains = 0, flush_domains = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07003134
Eric Anholt673a3942008-07-30 12:06:12 -07003135 /*
3136 * If the object isn't moving to a new write domain,
3137 * let the object stay in multiple read domains
3138 */
Chris Wilson05394f32010-11-08 19:18:58 +00003139 if (obj->base.pending_write_domain == 0)
3140 obj->base.pending_read_domains |= obj->base.read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07003141
3142 /*
3143 * Flush the current write domain if
3144 * the new read domains don't match. Invalidate
3145 * any read domains which differ from the old
3146 * write domain
3147 */
Chris Wilson05394f32010-11-08 19:18:58 +00003148 if (obj->base.write_domain &&
Chris Wilsoncaea7472010-11-12 13:53:37 +00003149 (((obj->base.write_domain != obj->base.pending_read_domains ||
3150 obj->ring != ring)) ||
3151 (obj->fenced_gpu_access && !obj->pending_fenced_gpu_access))) {
Chris Wilson05394f32010-11-08 19:18:58 +00003152 flush_domains |= obj->base.write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08003153 invalidate_domains |=
Chris Wilson05394f32010-11-08 19:18:58 +00003154 obj->base.pending_read_domains & ~obj->base.write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07003155 }
3156 /*
3157 * Invalidate any read caches which may have
3158 * stale data. That is, any new read domains.
3159 */
Chris Wilson05394f32010-11-08 19:18:58 +00003160 invalidate_domains |= obj->base.pending_read_domains & ~obj->base.read_domains;
Chris Wilson3d2a8122010-09-29 11:39:53 +01003161 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU)
Eric Anholt673a3942008-07-30 12:06:12 -07003162 i915_gem_clflush_object(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003163
Chris Wilson4a684a42010-10-28 14:44:08 +01003164 /* blow away mappings if mapped through GTT */
3165 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_GTT)
3166 i915_gem_release_mmap(obj);
3167
Eric Anholtefbeed92009-02-19 14:54:51 -08003168 /* The actual obj->write_domain will be updated with
3169 * pending_write_domain after we emit the accumulated flush for all
3170 * of our domain changes in execbuffers (which clears objects'
3171 * write_domains). So if we have a current write domain that we
3172 * aren't changing, set pending_write_domain to that.
3173 */
Chris Wilson05394f32010-11-08 19:18:58 +00003174 if (flush_domains == 0 && obj->base.pending_write_domain == 0)
3175 obj->base.pending_write_domain = obj->base.write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07003176
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003177 cd->invalidate_domains |= invalidate_domains;
3178 cd->flush_domains |= flush_domains;
Chris Wilsonb6651452010-10-23 10:15:06 +01003179 if (flush_domains & I915_GEM_GPU_DOMAINS)
Chris Wilson05394f32010-11-08 19:18:58 +00003180 cd->flush_rings |= obj->ring->id;
Chris Wilsonb6651452010-10-23 10:15:06 +01003181 if (invalidate_domains & I915_GEM_GPU_DOMAINS)
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003182 cd->flush_rings |= ring->id;
Eric Anholt673a3942008-07-30 12:06:12 -07003183}
3184
3185/**
Eric Anholte47c68e2008-11-14 13:35:19 -08003186 * Moves the object from a partially CPU read to a full one.
Eric Anholt673a3942008-07-30 12:06:12 -07003187 *
Eric Anholte47c68e2008-11-14 13:35:19 -08003188 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
3189 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
3190 */
3191static void
Chris Wilson05394f32010-11-08 19:18:58 +00003192i915_gem_object_set_to_full_cpu_read_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08003193{
Chris Wilson05394f32010-11-08 19:18:58 +00003194 if (!obj->page_cpu_valid)
Eric Anholte47c68e2008-11-14 13:35:19 -08003195 return;
3196
3197 /* If we're partially in the CPU read domain, finish moving it in.
3198 */
Chris Wilson05394f32010-11-08 19:18:58 +00003199 if (obj->base.read_domains & I915_GEM_DOMAIN_CPU) {
Eric Anholte47c68e2008-11-14 13:35:19 -08003200 int i;
3201
Chris Wilson05394f32010-11-08 19:18:58 +00003202 for (i = 0; i <= (obj->base.size - 1) / PAGE_SIZE; i++) {
3203 if (obj->page_cpu_valid[i])
Eric Anholte47c68e2008-11-14 13:35:19 -08003204 continue;
Chris Wilson05394f32010-11-08 19:18:58 +00003205 drm_clflush_pages(obj->pages + i, 1);
Eric Anholte47c68e2008-11-14 13:35:19 -08003206 }
Eric Anholte47c68e2008-11-14 13:35:19 -08003207 }
3208
3209 /* Free the page_cpu_valid mappings which are now stale, whether
3210 * or not we've got I915_GEM_DOMAIN_CPU.
3211 */
Chris Wilson05394f32010-11-08 19:18:58 +00003212 kfree(obj->page_cpu_valid);
3213 obj->page_cpu_valid = NULL;
Eric Anholte47c68e2008-11-14 13:35:19 -08003214}
3215
3216/**
3217 * Set the CPU read domain on a range of the object.
3218 *
3219 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
3220 * not entirely valid. The page_cpu_valid member of the object flags which
3221 * pages have been flushed, and will be respected by
3222 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
3223 * of the whole object.
3224 *
3225 * This function returns when the move is complete, including waiting on
3226 * flushes to occur.
Eric Anholt673a3942008-07-30 12:06:12 -07003227 */
3228static int
Chris Wilson05394f32010-11-08 19:18:58 +00003229i915_gem_object_set_cpu_read_domain_range(struct drm_i915_gem_object *obj,
Eric Anholte47c68e2008-11-14 13:35:19 -08003230 uint64_t offset, uint64_t size)
Eric Anholt673a3942008-07-30 12:06:12 -07003231{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003232 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003233 int i, ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003234
Chris Wilson05394f32010-11-08 19:18:58 +00003235 if (offset == 0 && size == obj->base.size)
Eric Anholte47c68e2008-11-14 13:35:19 -08003236 return i915_gem_object_set_to_cpu_domain(obj, 0);
3237
Daniel Vetterba3d8d72010-02-11 22:37:04 +01003238 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Eric Anholte47c68e2008-11-14 13:35:19 -08003239 if (ret != 0)
3240 return ret;
3241 i915_gem_object_flush_gtt_write_domain(obj);
3242
3243 /* If we're already fully in the CPU read domain, we're done. */
Chris Wilson05394f32010-11-08 19:18:58 +00003244 if (obj->page_cpu_valid == NULL &&
3245 (obj->base.read_domains & I915_GEM_DOMAIN_CPU) != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07003246 return 0;
3247
Eric Anholte47c68e2008-11-14 13:35:19 -08003248 /* Otherwise, create/clear the per-page CPU read domain flag if we're
3249 * newly adding I915_GEM_DOMAIN_CPU
3250 */
Chris Wilson05394f32010-11-08 19:18:58 +00003251 if (obj->page_cpu_valid == NULL) {
3252 obj->page_cpu_valid = kzalloc(obj->base.size / PAGE_SIZE,
3253 GFP_KERNEL);
3254 if (obj->page_cpu_valid == NULL)
Eric Anholte47c68e2008-11-14 13:35:19 -08003255 return -ENOMEM;
Chris Wilson05394f32010-11-08 19:18:58 +00003256 } else if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0)
3257 memset(obj->page_cpu_valid, 0, obj->base.size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07003258
3259 /* Flush the cache on any pages that are still invalid from the CPU's
3260 * perspective.
3261 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003262 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
3263 i++) {
Chris Wilson05394f32010-11-08 19:18:58 +00003264 if (obj->page_cpu_valid[i])
Eric Anholt673a3942008-07-30 12:06:12 -07003265 continue;
3266
Chris Wilson05394f32010-11-08 19:18:58 +00003267 drm_clflush_pages(obj->pages + i, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07003268
Chris Wilson05394f32010-11-08 19:18:58 +00003269 obj->page_cpu_valid[i] = 1;
Eric Anholt673a3942008-07-30 12:06:12 -07003270 }
3271
Eric Anholte47c68e2008-11-14 13:35:19 -08003272 /* It should now be out of any other write domains, and we can update
3273 * the domain values for our changes.
3274 */
Chris Wilson05394f32010-11-08 19:18:58 +00003275 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
Eric Anholte47c68e2008-11-14 13:35:19 -08003276
Chris Wilson05394f32010-11-08 19:18:58 +00003277 old_read_domains = obj->base.read_domains;
3278 obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003279
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003280 trace_i915_gem_object_change_domain(obj,
3281 old_read_domains,
Chris Wilson05394f32010-11-08 19:18:58 +00003282 obj->base.write_domain);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003283
Eric Anholt673a3942008-07-30 12:06:12 -07003284 return 0;
3285}
3286
Eric Anholt673a3942008-07-30 12:06:12 -07003287static int
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003288i915_gem_execbuffer_relocate_entry(struct drm_i915_gem_object *obj,
3289 struct drm_file *file_priv,
3290 struct drm_i915_gem_exec_object2 *entry,
3291 struct drm_i915_gem_relocation_entry *reloc)
Eric Anholt673a3942008-07-30 12:06:12 -07003292{
Chris Wilson9af90d12010-10-17 10:01:56 +01003293 struct drm_device *dev = obj->base.dev;
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003294 struct drm_gem_object *target_obj;
3295 uint32_t target_offset;
3296 int ret = -EINVAL;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003297
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003298 target_obj = drm_gem_object_lookup(dev, file_priv,
3299 reloc->target_handle);
3300 if (target_obj == NULL)
3301 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07003302
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003303 target_offset = to_intel_bo(target_obj)->gtt_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003304
Chris Wilson8542a0b2009-09-09 21:15:15 +01003305#if WATCH_RELOC
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003306 DRM_INFO("%s: obj %p offset %08x target %d "
3307 "read %08x write %08x gtt %08x "
3308 "presumed %08x delta %08x\n",
3309 __func__,
3310 obj,
3311 (int) reloc->offset,
3312 (int) reloc->target_handle,
3313 (int) reloc->read_domains,
3314 (int) reloc->write_domain,
3315 (int) target_offset,
3316 (int) reloc->presumed_offset,
3317 reloc->delta);
Chris Wilson8542a0b2009-09-09 21:15:15 +01003318#endif
3319
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003320 /* The target buffer should have appeared before us in the
3321 * exec_object list, so it should have a GTT space bound by now.
3322 */
3323 if (target_offset == 0) {
3324 DRM_ERROR("No GTT space found for object %d\n",
3325 reloc->target_handle);
3326 goto err;
Eric Anholt673a3942008-07-30 12:06:12 -07003327 }
3328
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003329 /* Validate that the target is in a valid r/w GPU domain */
3330 if (reloc->write_domain & (reloc->write_domain - 1)) {
3331 DRM_ERROR("reloc with multiple write domains: "
3332 "obj %p target %d offset %d "
3333 "read %08x write %08x",
3334 obj, reloc->target_handle,
3335 (int) reloc->offset,
3336 reloc->read_domains,
3337 reloc->write_domain);
3338 goto err;
3339 }
3340 if (reloc->write_domain & I915_GEM_DOMAIN_CPU ||
3341 reloc->read_domains & I915_GEM_DOMAIN_CPU) {
3342 DRM_ERROR("reloc with read/write CPU domains: "
3343 "obj %p target %d offset %d "
3344 "read %08x write %08x",
3345 obj, reloc->target_handle,
3346 (int) reloc->offset,
3347 reloc->read_domains,
3348 reloc->write_domain);
3349 goto err;
3350 }
3351 if (reloc->write_domain && target_obj->pending_write_domain &&
3352 reloc->write_domain != target_obj->pending_write_domain) {
3353 DRM_ERROR("Write domain conflict: "
3354 "obj %p target %d offset %d "
3355 "new %08x old %08x\n",
3356 obj, reloc->target_handle,
3357 (int) reloc->offset,
3358 reloc->write_domain,
3359 target_obj->pending_write_domain);
3360 goto err;
3361 }
3362
3363 target_obj->pending_read_domains |= reloc->read_domains;
3364 target_obj->pending_write_domain |= reloc->write_domain;
3365
3366 /* If the relocation already has the right value in it, no
3367 * more work needs to be done.
3368 */
3369 if (target_offset == reloc->presumed_offset)
3370 goto out;
3371
3372 /* Check that the relocation address is valid... */
3373 if (reloc->offset > obj->base.size - 4) {
3374 DRM_ERROR("Relocation beyond object bounds: "
3375 "obj %p target %d offset %d size %d.\n",
3376 obj, reloc->target_handle,
3377 (int) reloc->offset,
3378 (int) obj->base.size);
3379 goto err;
3380 }
3381 if (reloc->offset & 3) {
3382 DRM_ERROR("Relocation not 4-byte aligned: "
3383 "obj %p target %d offset %d.\n",
3384 obj, reloc->target_handle,
3385 (int) reloc->offset);
3386 goto err;
3387 }
3388
3389 /* and points to somewhere within the target object. */
3390 if (reloc->delta >= target_obj->size) {
3391 DRM_ERROR("Relocation beyond target object bounds: "
3392 "obj %p target %d delta %d size %d.\n",
3393 obj, reloc->target_handle,
3394 (int) reloc->delta,
3395 (int) target_obj->size);
3396 goto err;
3397 }
3398
3399 reloc->delta += target_offset;
3400 if (obj->base.write_domain == I915_GEM_DOMAIN_CPU) {
3401 uint32_t page_offset = reloc->offset & ~PAGE_MASK;
3402 char *vaddr;
3403
3404 vaddr = kmap_atomic(obj->pages[reloc->offset >> PAGE_SHIFT]);
3405 *(uint32_t *)(vaddr + page_offset) = reloc->delta;
3406 kunmap_atomic(vaddr);
3407 } else {
3408 struct drm_i915_private *dev_priv = dev->dev_private;
3409 uint32_t __iomem *reloc_entry;
3410 void __iomem *reloc_page;
3411
Chris Wilson05394f32010-11-08 19:18:58 +00003412 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003413 if (ret)
3414 goto err;
3415
3416 /* Map the page containing the relocation we're going to perform. */
3417 reloc->offset += obj->gtt_offset;
3418 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
3419 reloc->offset & PAGE_MASK);
3420 reloc_entry = (uint32_t __iomem *)
3421 (reloc_page + (reloc->offset & ~PAGE_MASK));
3422 iowrite32(reloc->delta, reloc_entry);
3423 io_mapping_unmap_atomic(reloc_page);
3424 }
3425
3426 /* and update the user's relocation entry */
3427 reloc->presumed_offset = target_offset;
3428
3429out:
3430 ret = 0;
3431err:
Chris Wilson9af90d12010-10-17 10:01:56 +01003432 drm_gem_object_unreference(target_obj);
3433 return ret;
3434}
3435
3436static int
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003437i915_gem_execbuffer_relocate_object(struct drm_i915_gem_object *obj,
3438 struct drm_file *file_priv,
3439 struct drm_i915_gem_exec_object2 *entry)
3440{
3441 struct drm_i915_gem_relocation_entry __user *user_relocs;
3442 int i, ret;
3443
3444 user_relocs = (void __user *)(uintptr_t)entry->relocs_ptr;
3445 for (i = 0; i < entry->relocation_count; i++) {
3446 struct drm_i915_gem_relocation_entry reloc;
3447
3448 if (__copy_from_user_inatomic(&reloc,
3449 user_relocs+i,
3450 sizeof(reloc)))
3451 return -EFAULT;
3452
3453 ret = i915_gem_execbuffer_relocate_entry(obj, file_priv, entry, &reloc);
3454 if (ret)
3455 return ret;
3456
3457 if (__copy_to_user_inatomic(&user_relocs[i].presumed_offset,
3458 &reloc.presumed_offset,
3459 sizeof(reloc.presumed_offset)))
3460 return -EFAULT;
3461 }
3462
3463 return 0;
3464}
3465
3466static int
3467i915_gem_execbuffer_relocate_object_slow(struct drm_i915_gem_object *obj,
3468 struct drm_file *file_priv,
3469 struct drm_i915_gem_exec_object2 *entry,
3470 struct drm_i915_gem_relocation_entry *relocs)
3471{
3472 int i, ret;
3473
3474 for (i = 0; i < entry->relocation_count; i++) {
3475 ret = i915_gem_execbuffer_relocate_entry(obj, file_priv, entry, &relocs[i]);
3476 if (ret)
3477 return ret;
3478 }
3479
3480 return 0;
3481}
3482
3483static int
3484i915_gem_execbuffer_relocate(struct drm_device *dev,
3485 struct drm_file *file,
Chris Wilson05394f32010-11-08 19:18:58 +00003486 struct drm_i915_gem_object **object_list,
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003487 struct drm_i915_gem_exec_object2 *exec_list,
3488 int count)
3489{
3490 int i, ret;
3491
3492 for (i = 0; i < count; i++) {
Chris Wilson05394f32010-11-08 19:18:58 +00003493 struct drm_i915_gem_object *obj = object_list[i];
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003494 obj->base.pending_read_domains = 0;
3495 obj->base.pending_write_domain = 0;
3496 ret = i915_gem_execbuffer_relocate_object(obj, file,
3497 &exec_list[i]);
3498 if (ret)
3499 return ret;
3500 }
3501
3502 return 0;
3503}
3504
3505static int
3506i915_gem_execbuffer_reserve(struct drm_device *dev,
3507 struct drm_file *file,
Chris Wilson05394f32010-11-08 19:18:58 +00003508 struct drm_i915_gem_object **object_list,
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003509 struct drm_i915_gem_exec_object2 *exec_list,
3510 int count)
Chris Wilson9af90d12010-10-17 10:01:56 +01003511{
Chris Wilson9af90d12010-10-17 10:01:56 +01003512 int ret, i, retry;
3513
Chris Wilsona7a09ae2010-11-12 13:49:09 +00003514 /* Attempt to pin all of the buffers into the GTT.
3515 * This is done in 3 phases:
3516 *
3517 * 1a. Unbind all objects that do not match the GTT constraints for
3518 * the execbuffer (fenceable, mappable, alignment etc).
3519 * 1b. Increment pin count for already bound objects.
3520 * 2. Bind new objects.
3521 * 3. Decrement pin count.
3522 *
3523 * This avoid unnecessary unbinding of later objects in order to makr
3524 * room for the earlier objects *unless* we need to defragment.
3525 */
Chris Wilson5eac3ab2010-10-31 08:49:47 +00003526 retry = 0;
3527 do {
Chris Wilson9af90d12010-10-17 10:01:56 +01003528 ret = 0;
Chris Wilsona7a09ae2010-11-12 13:49:09 +00003529
3530 /* Unbind any ill-fitting objects or pin. */
3531 for (i = 0; i < count; i++) {
3532 struct drm_i915_gem_object *obj = object_list[i];
3533 struct drm_i915_gem_exec_object2 *entry = &exec_list[i];
3534 bool need_fence, need_mappable;
3535
3536 if (!obj->gtt_space)
3537 continue;
3538
3539 need_fence =
3540 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
3541 obj->tiling_mode != I915_TILING_NONE;
3542 need_mappable =
3543 entry->relocation_count ? true : need_fence;
3544
3545 if ((entry->alignment && obj->gtt_offset & (entry->alignment - 1)) ||
3546 (need_mappable && !obj->map_and_fenceable))
3547 ret = i915_gem_object_unbind(obj);
3548 else
3549 ret = i915_gem_object_pin(obj,
3550 entry->alignment,
3551 need_mappable);
3552 if (ret) {
3553 count = i;
3554 goto err;
3555 }
3556 }
3557
3558 /* Bind fresh objects */
Chris Wilson9af90d12010-10-17 10:01:56 +01003559 for (i = 0; i < count; i++) {
3560 struct drm_i915_gem_exec_object2 *entry = &exec_list[i];
Chris Wilson05394f32010-11-08 19:18:58 +00003561 struct drm_i915_gem_object *obj = object_list[i];
Chris Wilsona7a09ae2010-11-12 13:49:09 +00003562 bool need_fence;
3563
3564 need_fence =
Chris Wilson9af90d12010-10-17 10:01:56 +01003565 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
3566 obj->tiling_mode != I915_TILING_NONE;
3567
Chris Wilsona7a09ae2010-11-12 13:49:09 +00003568 if (!obj->gtt_space) {
3569 bool need_mappable =
3570 entry->relocation_count ? true : need_fence;
Daniel Vetter16e809a2010-09-16 19:37:04 +02003571
Chris Wilsona7a09ae2010-11-12 13:49:09 +00003572 ret = i915_gem_object_pin(obj,
3573 entry->alignment,
3574 need_mappable);
Chris Wilson9af90d12010-10-17 10:01:56 +01003575 if (ret)
3576 break;
3577 }
3578
Chris Wilson9af90d12010-10-17 10:01:56 +01003579 if (need_fence) {
Chris Wilson05394f32010-11-08 19:18:58 +00003580 ret = i915_gem_object_get_fence_reg(obj, true);
Chris Wilsona7a09ae2010-11-12 13:49:09 +00003581 if (ret)
Chris Wilson9af90d12010-10-17 10:01:56 +01003582 break;
Chris Wilson9af90d12010-10-17 10:01:56 +01003583
Chris Wilsoncaea7472010-11-12 13:53:37 +00003584 obj->pending_fenced_gpu_access = true;
Chris Wilson9af90d12010-10-17 10:01:56 +01003585 }
3586
3587 entry->offset = obj->gtt_offset;
3588 }
3589
Chris Wilsona7a09ae2010-11-12 13:49:09 +00003590err: /* Decrement pin count for bound objects */
3591 for (i = 0; i < count; i++) {
3592 struct drm_i915_gem_object *obj = object_list[i];
3593 if (obj->gtt_space)
3594 i915_gem_object_unpin(obj);
3595 }
Chris Wilson9af90d12010-10-17 10:01:56 +01003596
Chris Wilson5eac3ab2010-10-31 08:49:47 +00003597 if (ret != -ENOSPC || retry > 1)
Chris Wilson9af90d12010-10-17 10:01:56 +01003598 return ret;
3599
Chris Wilson5eac3ab2010-10-31 08:49:47 +00003600 /* First attempt, just clear anything that is purgeable.
3601 * Second attempt, clear the entire GTT.
3602 */
3603 ret = i915_gem_evict_everything(dev, retry == 0);
Chris Wilson9af90d12010-10-17 10:01:56 +01003604 if (ret)
3605 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003606
Chris Wilson5eac3ab2010-10-31 08:49:47 +00003607 retry++;
3608 } while (1);
Eric Anholt673a3942008-07-30 12:06:12 -07003609}
3610
Chris Wilson13b29282010-11-01 12:22:48 +00003611static int
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003612i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
3613 struct drm_file *file,
Chris Wilson05394f32010-11-08 19:18:58 +00003614 struct drm_i915_gem_object **object_list,
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003615 struct drm_i915_gem_exec_object2 *exec_list,
3616 int count)
3617{
3618 struct drm_i915_gem_relocation_entry *reloc;
3619 int i, total, ret;
3620
Chris Wilson05394f32010-11-08 19:18:58 +00003621 for (i = 0; i < count; i++)
3622 object_list[i]->in_execbuffer = false;
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003623
3624 mutex_unlock(&dev->struct_mutex);
3625
3626 total = 0;
3627 for (i = 0; i < count; i++)
3628 total += exec_list[i].relocation_count;
3629
3630 reloc = drm_malloc_ab(total, sizeof(*reloc));
3631 if (reloc == NULL) {
3632 mutex_lock(&dev->struct_mutex);
3633 return -ENOMEM;
3634 }
3635
3636 total = 0;
3637 for (i = 0; i < count; i++) {
3638 struct drm_i915_gem_relocation_entry __user *user_relocs;
3639
3640 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3641
3642 if (copy_from_user(reloc+total, user_relocs,
3643 exec_list[i].relocation_count *
3644 sizeof(*reloc))) {
3645 ret = -EFAULT;
3646 mutex_lock(&dev->struct_mutex);
3647 goto err;
3648 }
3649
3650 total += exec_list[i].relocation_count;
3651 }
3652
3653 ret = i915_mutex_lock_interruptible(dev);
3654 if (ret) {
3655 mutex_lock(&dev->struct_mutex);
3656 goto err;
3657 }
3658
3659 ret = i915_gem_execbuffer_reserve(dev, file,
3660 object_list, exec_list,
3661 count);
3662 if (ret)
3663 goto err;
3664
3665 total = 0;
3666 for (i = 0; i < count; i++) {
Chris Wilson05394f32010-11-08 19:18:58 +00003667 struct drm_i915_gem_object *obj = object_list[i];
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003668 obj->base.pending_read_domains = 0;
3669 obj->base.pending_write_domain = 0;
3670 ret = i915_gem_execbuffer_relocate_object_slow(obj, file,
3671 &exec_list[i],
3672 reloc + total);
3673 if (ret)
3674 goto err;
3675
3676 total += exec_list[i].relocation_count;
3677 }
3678
3679 /* Leave the user relocations as are, this is the painfully slow path,
3680 * and we want to avoid the complication of dropping the lock whilst
3681 * having buffers reserved in the aperture and so causing spurious
3682 * ENOSPC for random operations.
3683 */
3684
3685err:
3686 drm_free_large(reloc);
3687 return ret;
3688}
3689
3690static int
Chris Wilson13b29282010-11-01 12:22:48 +00003691i915_gem_execbuffer_move_to_gpu(struct drm_device *dev,
3692 struct drm_file *file,
3693 struct intel_ring_buffer *ring,
Chris Wilson05394f32010-11-08 19:18:58 +00003694 struct drm_i915_gem_object **objects,
Chris Wilson13b29282010-11-01 12:22:48 +00003695 int count)
3696{
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003697 struct change_domains cd;
Chris Wilson13b29282010-11-01 12:22:48 +00003698 int ret, i;
3699
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003700 cd.invalidate_domains = 0;
3701 cd.flush_domains = 0;
3702 cd.flush_rings = 0;
Chris Wilson13b29282010-11-01 12:22:48 +00003703 for (i = 0; i < count; i++)
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003704 i915_gem_object_set_to_gpu_domain(objects[i], ring, &cd);
Chris Wilson13b29282010-11-01 12:22:48 +00003705
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003706 if (cd.invalidate_domains | cd.flush_domains) {
Chris Wilson13b29282010-11-01 12:22:48 +00003707#if WATCH_EXEC
3708 DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
3709 __func__,
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003710 cd.invalidate_domains,
3711 cd.flush_domains);
Chris Wilson13b29282010-11-01 12:22:48 +00003712#endif
Chris Wilson05394f32010-11-08 19:18:58 +00003713 i915_gem_flush(dev,
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003714 cd.invalidate_domains,
3715 cd.flush_domains,
3716 cd.flush_rings);
Chris Wilson13b29282010-11-01 12:22:48 +00003717 }
3718
3719 for (i = 0; i < count; i++) {
Chris Wilson05394f32010-11-08 19:18:58 +00003720 struct drm_i915_gem_object *obj = objects[i];
Chris Wilson13b29282010-11-01 12:22:48 +00003721 /* XXX replace with semaphores */
3722 if (obj->ring && ring != obj->ring) {
Chris Wilson05394f32010-11-08 19:18:58 +00003723 ret = i915_gem_object_wait_rendering(obj, true);
Chris Wilson13b29282010-11-01 12:22:48 +00003724 if (ret)
3725 return ret;
3726 }
3727 }
3728
3729 return 0;
3730}
3731
Eric Anholt673a3942008-07-30 12:06:12 -07003732/* Throttle our rendering by waiting until the ring has completed our requests
3733 * emitted over 20 msec ago.
3734 *
Eric Anholtb9624422009-06-03 07:27:35 +00003735 * Note that if we were to use the current jiffies each time around the loop,
3736 * we wouldn't escape the function with any frames outstanding if the time to
3737 * render a frame was over 20ms.
3738 *
Eric Anholt673a3942008-07-30 12:06:12 -07003739 * This should get us reasonable parallelism between CPU and GPU but also
3740 * relatively low latency when blocking on a particular request to finish.
3741 */
3742static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003743i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003744{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003745 struct drm_i915_private *dev_priv = dev->dev_private;
3746 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00003747 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003748 struct drm_i915_gem_request *request;
3749 struct intel_ring_buffer *ring = NULL;
3750 u32 seqno = 0;
3751 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003752
Chris Wilson1c255952010-09-26 11:03:27 +01003753 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003754 list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
Eric Anholtb9624422009-06-03 07:27:35 +00003755 if (time_after_eq(request->emitted_jiffies, recent_enough))
3756 break;
3757
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003758 ring = request->ring;
3759 seqno = request->seqno;
Eric Anholtb9624422009-06-03 07:27:35 +00003760 }
Chris Wilson1c255952010-09-26 11:03:27 +01003761 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003762
3763 if (seqno == 0)
3764 return 0;
3765
3766 ret = 0;
Chris Wilson78501ea2010-10-27 12:18:21 +01003767 if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) {
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003768 /* And wait for the seqno passing without holding any locks and
3769 * causing extra latency for others. This is safe as the irq
3770 * generation is designed to be run atomically and so is
3771 * lockless.
3772 */
Chris Wilson78501ea2010-10-27 12:18:21 +01003773 ring->user_irq_get(ring);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003774 ret = wait_event_interruptible(ring->irq_queue,
Chris Wilson78501ea2010-10-27 12:18:21 +01003775 i915_seqno_passed(ring->get_seqno(ring), seqno)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003776 || atomic_read(&dev_priv->mm.wedged));
Chris Wilson78501ea2010-10-27 12:18:21 +01003777 ring->user_irq_put(ring);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003778
3779 if (ret == 0 && atomic_read(&dev_priv->mm.wedged))
3780 ret = -EIO;
3781 }
3782
3783 if (ret == 0)
3784 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
Eric Anholtb9624422009-06-03 07:27:35 +00003785
Eric Anholt673a3942008-07-30 12:06:12 -07003786 return ret;
3787}
3788
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003789static int
Chris Wilson2549d6c2010-10-14 12:10:41 +01003790i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec,
3791 uint64_t exec_offset)
Chris Wilson83d60792009-06-06 09:45:57 +01003792{
3793 uint32_t exec_start, exec_len;
3794
3795 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
3796 exec_len = (uint32_t) exec->batch_len;
3797
3798 if ((exec_start | exec_len) & 0x7)
3799 return -EINVAL;
3800
3801 if (!exec_start)
3802 return -EINVAL;
3803
3804 return 0;
3805}
3806
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003807static int
Chris Wilson2549d6c2010-10-14 12:10:41 +01003808validate_exec_list(struct drm_i915_gem_exec_object2 *exec,
3809 int count)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003810{
Chris Wilson2549d6c2010-10-14 12:10:41 +01003811 int i;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003812
Chris Wilson2549d6c2010-10-14 12:10:41 +01003813 for (i = 0; i < count; i++) {
3814 char __user *ptr = (char __user *)(uintptr_t)exec[i].relocs_ptr;
Chris Wilsond1d78832010-11-21 09:23:48 +00003815 int length; /* limited by fault_in_pages_readable() */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003816
Chris Wilsond1d78832010-11-21 09:23:48 +00003817 /* First check for malicious input causing overflow */
3818 if (exec[i].relocation_count >
3819 INT_MAX / sizeof(struct drm_i915_gem_relocation_entry))
3820 return -EINVAL;
3821
3822 length = exec[i].relocation_count *
3823 sizeof(struct drm_i915_gem_relocation_entry);
Chris Wilson2549d6c2010-10-14 12:10:41 +01003824 if (!access_ok(VERIFY_READ, ptr, length))
3825 return -EFAULT;
3826
Chris Wilsonb5dc6082010-10-20 20:59:57 +01003827 /* we may also need to update the presumed offsets */
3828 if (!access_ok(VERIFY_WRITE, ptr, length))
3829 return -EFAULT;
3830
Chris Wilson2549d6c2010-10-14 12:10:41 +01003831 if (fault_in_pages_readable(ptr, length))
3832 return -EFAULT;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003833 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003834
Chris Wilson2549d6c2010-10-14 12:10:41 +01003835 return 0;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003836}
3837
Chris Wilson2549d6c2010-10-14 12:10:41 +01003838static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003839i915_gem_do_execbuffer(struct drm_device *dev, void *data,
Chris Wilson9af90d12010-10-17 10:01:56 +01003840 struct drm_file *file,
Jesse Barnes76446ca2009-12-17 22:05:42 -05003841 struct drm_i915_gem_execbuffer2 *args,
3842 struct drm_i915_gem_exec_object2 *exec_list)
Eric Anholt673a3942008-07-30 12:06:12 -07003843{
3844 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00003845 struct drm_i915_gem_object **object_list = NULL;
3846 struct drm_i915_gem_object *batch_obj;
Eric Anholt201361a2009-03-11 12:30:04 -07003847 struct drm_clip_rect *cliprects = NULL;
Chris Wilson8dc5d142010-08-12 12:36:12 +01003848 struct drm_i915_gem_request *request = NULL;
Chris Wilson9af90d12010-10-17 10:01:56 +01003849 int ret, i, flips;
Eric Anholt673a3942008-07-30 12:06:12 -07003850 uint64_t exec_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003851
Zou Nan hai852835f2010-05-21 09:08:56 +08003852 struct intel_ring_buffer *ring = NULL;
3853
Chris Wilson30dbf0c2010-09-25 10:19:17 +01003854 ret = i915_gem_check_is_wedged(dev);
3855 if (ret)
3856 return ret;
3857
Chris Wilson2549d6c2010-10-14 12:10:41 +01003858 ret = validate_exec_list(exec_list, args->buffer_count);
3859 if (ret)
3860 return ret;
3861
Eric Anholt673a3942008-07-30 12:06:12 -07003862#if WATCH_EXEC
3863 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3864 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3865#endif
Chris Wilson549f7362010-10-19 11:19:32 +01003866 switch (args->flags & I915_EXEC_RING_MASK) {
3867 case I915_EXEC_DEFAULT:
3868 case I915_EXEC_RENDER:
3869 ring = &dev_priv->render_ring;
3870 break;
3871 case I915_EXEC_BSD:
Zou Nan haid1b851f2010-05-21 09:08:57 +08003872 if (!HAS_BSD(dev)) {
Chris Wilson549f7362010-10-19 11:19:32 +01003873 DRM_ERROR("execbuf with invalid ring (BSD)\n");
Zou Nan haid1b851f2010-05-21 09:08:57 +08003874 return -EINVAL;
3875 }
3876 ring = &dev_priv->bsd_ring;
Chris Wilson549f7362010-10-19 11:19:32 +01003877 break;
3878 case I915_EXEC_BLT:
3879 if (!HAS_BLT(dev)) {
3880 DRM_ERROR("execbuf with invalid ring (BLT)\n");
3881 return -EINVAL;
3882 }
3883 ring = &dev_priv->blt_ring;
3884 break;
3885 default:
3886 DRM_ERROR("execbuf with unknown ring: %d\n",
3887 (int)(args->flags & I915_EXEC_RING_MASK));
3888 return -EINVAL;
Zou Nan haid1b851f2010-05-21 09:08:57 +08003889 }
3890
Eric Anholt4f481ed2008-09-10 14:22:49 -07003891 if (args->buffer_count < 1) {
3892 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3893 return -EINVAL;
3894 }
Eric Anholtc8e0f932009-11-22 03:49:37 +01003895 object_list = drm_malloc_ab(sizeof(*object_list), args->buffer_count);
Jesse Barnes76446ca2009-12-17 22:05:42 -05003896 if (object_list == NULL) {
3897 DRM_ERROR("Failed to allocate object list for %d buffers\n",
Eric Anholt673a3942008-07-30 12:06:12 -07003898 args->buffer_count);
3899 ret = -ENOMEM;
3900 goto pre_mutex_err;
3901 }
Eric Anholt673a3942008-07-30 12:06:12 -07003902
Eric Anholt201361a2009-03-11 12:30:04 -07003903 if (args->num_cliprects != 0) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003904 cliprects = kcalloc(args->num_cliprects, sizeof(*cliprects),
3905 GFP_KERNEL);
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003906 if (cliprects == NULL) {
3907 ret = -ENOMEM;
Eric Anholt201361a2009-03-11 12:30:04 -07003908 goto pre_mutex_err;
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003909 }
Eric Anholt201361a2009-03-11 12:30:04 -07003910
3911 ret = copy_from_user(cliprects,
3912 (struct drm_clip_rect __user *)
3913 (uintptr_t) args->cliprects_ptr,
3914 sizeof(*cliprects) * args->num_cliprects);
3915 if (ret != 0) {
3916 DRM_ERROR("copy %d cliprects failed: %d\n",
3917 args->num_cliprects, ret);
Dan Carpenterc877cdc2010-06-23 19:03:01 +02003918 ret = -EFAULT;
Eric Anholt201361a2009-03-11 12:30:04 -07003919 goto pre_mutex_err;
3920 }
3921 }
3922
Chris Wilson8dc5d142010-08-12 12:36:12 +01003923 request = kzalloc(sizeof(*request), GFP_KERNEL);
3924 if (request == NULL) {
3925 ret = -ENOMEM;
Chris Wilsona198bc82009-02-06 16:55:20 +00003926 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003927 }
3928
Chris Wilson76c1dec2010-09-25 11:22:51 +01003929 ret = i915_mutex_lock_interruptible(dev);
3930 if (ret)
3931 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003932
Eric Anholt673a3942008-07-30 12:06:12 -07003933 if (dev_priv->mm.suspended) {
Eric Anholt673a3942008-07-30 12:06:12 -07003934 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003935 ret = -EBUSY;
3936 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003937 }
3938
Keith Packardac94a962008-11-20 23:30:27 -08003939 /* Look up object handles */
Eric Anholt673a3942008-07-30 12:06:12 -07003940 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson05394f32010-11-08 19:18:58 +00003941 struct drm_i915_gem_object *obj;
Chris Wilson7e318e12010-10-27 13:43:39 +01003942
Chris Wilson05394f32010-11-08 19:18:58 +00003943 obj = to_intel_bo (drm_gem_object_lookup(dev, file,
3944 exec_list[i].handle));
3945 if (obj == NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -07003946 DRM_ERROR("Invalid object handle %d at index %d\n",
3947 exec_list[i].handle, i);
Chris Wilson0ce907f2010-01-23 20:26:35 +00003948 /* prevent error path from reading uninitialized data */
Chris Wilson05394f32010-11-08 19:18:58 +00003949 args->buffer_count = i;
Chris Wilsonbf79cb92010-08-04 14:19:46 +01003950 ret = -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07003951 goto err;
3952 }
Chris Wilson05394f32010-11-08 19:18:58 +00003953 object_list[i] = obj;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003954
Chris Wilson05394f32010-11-08 19:18:58 +00003955 if (obj->in_execbuffer) {
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003956 DRM_ERROR("Object %p appears more than once in object list\n",
Chris Wilson05394f32010-11-08 19:18:58 +00003957 obj);
Chris Wilson0ce907f2010-01-23 20:26:35 +00003958 /* prevent error path from reading uninitialized data */
3959 args->buffer_count = i + 1;
Chris Wilsonbf79cb92010-08-04 14:19:46 +01003960 ret = -EINVAL;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003961 goto err;
3962 }
Chris Wilson05394f32010-11-08 19:18:58 +00003963 obj->in_execbuffer = true;
Chris Wilsoncaea7472010-11-12 13:53:37 +00003964 obj->pending_fenced_gpu_access = false;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003965 }
3966
Chris Wilson9af90d12010-10-17 10:01:56 +01003967 /* Move the objects en-masse into the GTT, evicting if necessary. */
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003968 ret = i915_gem_execbuffer_reserve(dev, file,
3969 object_list, exec_list,
3970 args->buffer_count);
Chris Wilson9af90d12010-10-17 10:01:56 +01003971 if (ret)
3972 goto err;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003973
Chris Wilson9af90d12010-10-17 10:01:56 +01003974 /* The objects are in their final locations, apply the relocations. */
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003975 ret = i915_gem_execbuffer_relocate(dev, file,
3976 object_list, exec_list,
3977 args->buffer_count);
3978 if (ret) {
3979 if (ret == -EFAULT) {
3980 ret = i915_gem_execbuffer_relocate_slow(dev, file,
3981 object_list,
3982 exec_list,
3983 args->buffer_count);
3984 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
3985 }
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003986 if (ret)
3987 goto err;
3988 }
3989
Eric Anholt673a3942008-07-30 12:06:12 -07003990 /* Set the pending read domains for the batch buffer to COMMAND */
3991 batch_obj = object_list[args->buffer_count-1];
Chris Wilson05394f32010-11-08 19:18:58 +00003992 if (batch_obj->base.pending_write_domain) {
Chris Wilson5f26a2c2009-06-06 09:45:58 +01003993 DRM_ERROR("Attempting to use self-modifying batch buffer\n");
3994 ret = -EINVAL;
3995 goto err;
3996 }
Chris Wilson05394f32010-11-08 19:18:58 +00003997 batch_obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
Eric Anholt673a3942008-07-30 12:06:12 -07003998
Chris Wilson9af90d12010-10-17 10:01:56 +01003999 /* Sanity check the batch buffer */
Chris Wilson05394f32010-11-08 19:18:58 +00004000 exec_offset = batch_obj->gtt_offset;
Chris Wilson9af90d12010-10-17 10:01:56 +01004001 ret = i915_gem_check_execbuffer(args, exec_offset);
Chris Wilson83d60792009-06-06 09:45:57 +01004002 if (ret != 0) {
4003 DRM_ERROR("execbuf with invalid offset/length\n");
4004 goto err;
4005 }
4006
Chris Wilson13b29282010-11-01 12:22:48 +00004007 ret = i915_gem_execbuffer_move_to_gpu(dev, file, ring,
4008 object_list, args->buffer_count);
4009 if (ret)
4010 goto err;
Eric Anholt673a3942008-07-30 12:06:12 -07004011
Eric Anholt673a3942008-07-30 12:06:12 -07004012#if WATCH_COHERENCY
4013 for (i = 0; i < args->buffer_count; i++) {
4014 i915_gem_object_check_coherency(object_list[i],
4015 exec_list[i].handle);
4016 }
4017#endif
4018
Eric Anholt673a3942008-07-30 12:06:12 -07004019#if WATCH_EXEC
Ben Gamari6911a9b2009-04-02 11:24:54 -07004020 i915_gem_dump_object(batch_obj,
Eric Anholt673a3942008-07-30 12:06:12 -07004021 args->batch_len,
4022 __func__,
4023 ~0);
4024#endif
4025
Chris Wilsone59f2ba2010-10-07 17:28:15 +01004026 /* Check for any pending flips. As we only maintain a flip queue depth
4027 * of 1, we can simply insert a WAIT for the next display flip prior
4028 * to executing the batch and avoid stalling the CPU.
4029 */
4030 flips = 0;
4031 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson05394f32010-11-08 19:18:58 +00004032 if (object_list[i]->base.write_domain)
4033 flips |= atomic_read(&object_list[i]->pending_flip);
Chris Wilsone59f2ba2010-10-07 17:28:15 +01004034 }
4035 if (flips) {
4036 int plane, flip_mask;
4037
4038 for (plane = 0; flips >> plane; plane++) {
4039 if (((flips >> plane) & 1) == 0)
4040 continue;
4041
4042 if (plane)
4043 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
4044 else
4045 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
4046
Chris Wilsone1f99ce2010-10-27 12:45:26 +01004047 ret = intel_ring_begin(ring, 2);
4048 if (ret)
4049 goto err;
4050
Chris Wilson78501ea2010-10-27 12:18:21 +01004051 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
4052 intel_ring_emit(ring, MI_NOOP);
4053 intel_ring_advance(ring);
Chris Wilsone59f2ba2010-10-07 17:28:15 +01004054 }
4055 }
4056
Eric Anholt673a3942008-07-30 12:06:12 -07004057 /* Exec the batchbuffer */
Chris Wilson78501ea2010-10-27 12:18:21 +01004058 ret = ring->dispatch_execbuffer(ring, args, cliprects, exec_offset);
Eric Anholt673a3942008-07-30 12:06:12 -07004059 if (ret) {
4060 DRM_ERROR("dispatch failed %d\n", ret);
4061 goto err;
4062 }
4063
Chris Wilson7e318e12010-10-27 13:43:39 +01004064 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson05394f32010-11-08 19:18:58 +00004065 struct drm_i915_gem_object *obj = object_list[i];
Chris Wilson7e318e12010-10-27 13:43:39 +01004066
Chris Wilson05394f32010-11-08 19:18:58 +00004067 obj->base.read_domains = obj->base.pending_read_domains;
4068 obj->base.write_domain = obj->base.pending_write_domain;
Chris Wilsoncaea7472010-11-12 13:53:37 +00004069 obj->fenced_gpu_access = obj->pending_fenced_gpu_access;
Chris Wilson7e318e12010-10-27 13:43:39 +01004070
4071 i915_gem_object_move_to_active(obj, ring);
Chris Wilson05394f32010-11-08 19:18:58 +00004072 if (obj->base.write_domain) {
4073 obj->dirty = 1;
4074 list_move_tail(&obj->gpu_write_list,
Chris Wilson7e318e12010-10-27 13:43:39 +01004075 &ring->gpu_write_list);
4076 intel_mark_busy(dev, obj);
4077 }
4078
4079 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00004080 obj->base.read_domains,
4081 obj->base.write_domain);
Chris Wilson7e318e12010-10-27 13:43:39 +01004082 }
4083
Eric Anholt673a3942008-07-30 12:06:12 -07004084 /*
4085 * Ensure that the commands in the batch buffer are
4086 * finished before the interrupt fires
4087 */
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01004088 i915_retire_commands(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07004089
Chris Wilson3cce4692010-10-27 16:11:02 +01004090 if (i915_add_request(dev, file, request, ring))
Chris Wilson5d97eb62010-11-10 20:40:02 +00004091 i915_gem_next_request_seqno(dev, ring);
Chris Wilson3cce4692010-10-27 16:11:02 +01004092 else
4093 request = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07004094
Eric Anholt673a3942008-07-30 12:06:12 -07004095err:
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05004096 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson05394f32010-11-08 19:18:58 +00004097 object_list[i]->in_execbuffer = false;
4098 drm_gem_object_unreference(&object_list[i]->base);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05004099 }
Julia Lawallaad87df2008-12-21 16:28:47 +01004100
Eric Anholt673a3942008-07-30 12:06:12 -07004101 mutex_unlock(&dev->struct_mutex);
4102
Chris Wilson93533c22010-01-31 10:40:48 +00004103pre_mutex_err:
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07004104 drm_free_large(object_list);
Eric Anholt9a298b22009-03-24 12:23:04 -07004105 kfree(cliprects);
Chris Wilson8dc5d142010-08-12 12:36:12 +01004106 kfree(request);
Eric Anholt673a3942008-07-30 12:06:12 -07004107
4108 return ret;
4109}
4110
Jesse Barnes76446ca2009-12-17 22:05:42 -05004111/*
4112 * Legacy execbuffer just creates an exec2 list from the original exec object
4113 * list array and passes it to the real function.
4114 */
4115int
4116i915_gem_execbuffer(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00004117 struct drm_file *file)
Jesse Barnes76446ca2009-12-17 22:05:42 -05004118{
4119 struct drm_i915_gem_execbuffer *args = data;
4120 struct drm_i915_gem_execbuffer2 exec2;
4121 struct drm_i915_gem_exec_object *exec_list = NULL;
4122 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
4123 int ret, i;
4124
4125#if WATCH_EXEC
4126 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
4127 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
4128#endif
4129
4130 if (args->buffer_count < 1) {
4131 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
4132 return -EINVAL;
4133 }
4134
4135 /* Copy in the exec list from userland */
4136 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
4137 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
4138 if (exec_list == NULL || exec2_list == NULL) {
4139 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
4140 args->buffer_count);
4141 drm_free_large(exec_list);
4142 drm_free_large(exec2_list);
4143 return -ENOMEM;
4144 }
4145 ret = copy_from_user(exec_list,
4146 (struct drm_i915_relocation_entry __user *)
4147 (uintptr_t) args->buffers_ptr,
4148 sizeof(*exec_list) * args->buffer_count);
4149 if (ret != 0) {
4150 DRM_ERROR("copy %d exec entries failed %d\n",
4151 args->buffer_count, ret);
4152 drm_free_large(exec_list);
4153 drm_free_large(exec2_list);
4154 return -EFAULT;
4155 }
4156
4157 for (i = 0; i < args->buffer_count; i++) {
4158 exec2_list[i].handle = exec_list[i].handle;
4159 exec2_list[i].relocation_count = exec_list[i].relocation_count;
4160 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
4161 exec2_list[i].alignment = exec_list[i].alignment;
4162 exec2_list[i].offset = exec_list[i].offset;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004163 if (INTEL_INFO(dev)->gen < 4)
Jesse Barnes76446ca2009-12-17 22:05:42 -05004164 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
4165 else
4166 exec2_list[i].flags = 0;
4167 }
4168
4169 exec2.buffers_ptr = args->buffers_ptr;
4170 exec2.buffer_count = args->buffer_count;
4171 exec2.batch_start_offset = args->batch_start_offset;
4172 exec2.batch_len = args->batch_len;
4173 exec2.DR1 = args->DR1;
4174 exec2.DR4 = args->DR4;
4175 exec2.num_cliprects = args->num_cliprects;
4176 exec2.cliprects_ptr = args->cliprects_ptr;
Zou Nan hai852835f2010-05-21 09:08:56 +08004177 exec2.flags = I915_EXEC_RENDER;
Jesse Barnes76446ca2009-12-17 22:05:42 -05004178
Chris Wilson05394f32010-11-08 19:18:58 +00004179 ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list);
Jesse Barnes76446ca2009-12-17 22:05:42 -05004180 if (!ret) {
4181 /* Copy the new buffer offsets back to the user's exec list. */
4182 for (i = 0; i < args->buffer_count; i++)
4183 exec_list[i].offset = exec2_list[i].offset;
4184 /* ... and back out to userspace */
4185 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
4186 (uintptr_t) args->buffers_ptr,
4187 exec_list,
4188 sizeof(*exec_list) * args->buffer_count);
4189 if (ret) {
4190 ret = -EFAULT;
4191 DRM_ERROR("failed to copy %d exec entries "
4192 "back to user (%d)\n",
4193 args->buffer_count, ret);
4194 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05004195 }
4196
4197 drm_free_large(exec_list);
4198 drm_free_large(exec2_list);
4199 return ret;
4200}
4201
4202int
4203i915_gem_execbuffer2(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00004204 struct drm_file *file)
Jesse Barnes76446ca2009-12-17 22:05:42 -05004205{
4206 struct drm_i915_gem_execbuffer2 *args = data;
4207 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
4208 int ret;
4209
4210#if WATCH_EXEC
4211 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
4212 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
4213#endif
4214
4215 if (args->buffer_count < 1) {
4216 DRM_ERROR("execbuf2 with %d buffers\n", args->buffer_count);
4217 return -EINVAL;
4218 }
4219
4220 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
4221 if (exec2_list == NULL) {
4222 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
4223 args->buffer_count);
4224 return -ENOMEM;
4225 }
4226 ret = copy_from_user(exec2_list,
4227 (struct drm_i915_relocation_entry __user *)
4228 (uintptr_t) args->buffers_ptr,
4229 sizeof(*exec2_list) * args->buffer_count);
4230 if (ret != 0) {
4231 DRM_ERROR("copy %d exec entries failed %d\n",
4232 args->buffer_count, ret);
4233 drm_free_large(exec2_list);
4234 return -EFAULT;
4235 }
4236
Chris Wilson05394f32010-11-08 19:18:58 +00004237 ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list);
Jesse Barnes76446ca2009-12-17 22:05:42 -05004238 if (!ret) {
4239 /* Copy the new buffer offsets back to the user's exec list. */
4240 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
4241 (uintptr_t) args->buffers_ptr,
4242 exec2_list,
4243 sizeof(*exec2_list) * args->buffer_count);
4244 if (ret) {
4245 ret = -EFAULT;
4246 DRM_ERROR("failed to copy %d exec entries "
4247 "back to user (%d)\n",
4248 args->buffer_count, ret);
4249 }
4250 }
4251
4252 drm_free_large(exec2_list);
4253 return ret;
4254}
4255
Eric Anholt673a3942008-07-30 12:06:12 -07004256int
Chris Wilson05394f32010-11-08 19:18:58 +00004257i915_gem_object_pin(struct drm_i915_gem_object *obj,
4258 uint32_t alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01004259 bool map_and_fenceable)
Eric Anholt673a3942008-07-30 12:06:12 -07004260{
Chris Wilson05394f32010-11-08 19:18:58 +00004261 struct drm_device *dev = obj->base.dev;
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004262 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07004263 int ret;
4264
Chris Wilson05394f32010-11-08 19:18:58 +00004265 BUG_ON(obj->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT);
Chris Wilson23bc5982010-09-29 16:10:57 +01004266 WARN_ON(i915_verify_lists(dev));
Chris Wilsonac0c6b52010-05-27 13:18:18 +01004267
Chris Wilson05394f32010-11-08 19:18:58 +00004268 if (obj->gtt_space != NULL) {
4269 if ((alignment && obj->gtt_offset & (alignment - 1)) ||
4270 (map_and_fenceable && !obj->map_and_fenceable)) {
4271 WARN(obj->pin_count,
Chris Wilsonae7d49d2010-08-04 12:37:41 +01004272 "bo is already pinned with incorrect alignment:"
Daniel Vetter75e9e912010-11-04 17:11:09 +01004273 " offset=%x, req.alignment=%x, req.map_and_fenceable=%d,"
4274 " obj->map_and_fenceable=%d\n",
Chris Wilson05394f32010-11-08 19:18:58 +00004275 obj->gtt_offset, alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01004276 map_and_fenceable,
Chris Wilson05394f32010-11-08 19:18:58 +00004277 obj->map_and_fenceable);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01004278 ret = i915_gem_object_unbind(obj);
4279 if (ret)
4280 return ret;
4281 }
4282 }
4283
Chris Wilson05394f32010-11-08 19:18:58 +00004284 if (obj->gtt_space == NULL) {
Chris Wilsona00b10c2010-09-24 21:15:47 +01004285 ret = i915_gem_object_bind_to_gtt(obj, alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01004286 map_and_fenceable);
Chris Wilson97311292009-09-21 00:22:34 +01004287 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07004288 return ret;
Chris Wilson22c344e2009-02-11 14:26:45 +00004289 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05004290
Chris Wilson05394f32010-11-08 19:18:58 +00004291 if (obj->pin_count++ == 0) {
Chris Wilson05394f32010-11-08 19:18:58 +00004292 if (!obj->active)
4293 list_move_tail(&obj->mm_list,
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004294 &dev_priv->mm.pinned_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004295 }
Chris Wilson6299f992010-11-24 12:23:44 +00004296 obj->pin_mappable |= map_and_fenceable;
Eric Anholt673a3942008-07-30 12:06:12 -07004297
Chris Wilson23bc5982010-09-29 16:10:57 +01004298 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07004299 return 0;
4300}
4301
4302void
Chris Wilson05394f32010-11-08 19:18:58 +00004303i915_gem_object_unpin(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07004304{
Chris Wilson05394f32010-11-08 19:18:58 +00004305 struct drm_device *dev = obj->base.dev;
Eric Anholt673a3942008-07-30 12:06:12 -07004306 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07004307
Chris Wilson23bc5982010-09-29 16:10:57 +01004308 WARN_ON(i915_verify_lists(dev));
Chris Wilson05394f32010-11-08 19:18:58 +00004309 BUG_ON(obj->pin_count == 0);
4310 BUG_ON(obj->gtt_space == NULL);
Eric Anholt673a3942008-07-30 12:06:12 -07004311
Chris Wilson05394f32010-11-08 19:18:58 +00004312 if (--obj->pin_count == 0) {
4313 if (!obj->active)
4314 list_move_tail(&obj->mm_list,
Eric Anholt673a3942008-07-30 12:06:12 -07004315 &dev_priv->mm.inactive_list);
Chris Wilson6299f992010-11-24 12:23:44 +00004316 obj->pin_mappable = false;
Eric Anholt673a3942008-07-30 12:06:12 -07004317 }
Chris Wilson23bc5982010-09-29 16:10:57 +01004318 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07004319}
4320
4321int
4322i915_gem_pin_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00004323 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07004324{
4325 struct drm_i915_gem_pin *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004326 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07004327 int ret;
4328
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004329 ret = i915_mutex_lock_interruptible(dev);
4330 if (ret)
4331 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004332
Chris Wilson05394f32010-11-08 19:18:58 +00004333 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Eric Anholt673a3942008-07-30 12:06:12 -07004334 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004335 ret = -ENOENT;
4336 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07004337 }
Eric Anholt673a3942008-07-30 12:06:12 -07004338
Chris Wilson05394f32010-11-08 19:18:58 +00004339 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004340 DRM_ERROR("Attempting to pin a purgeable buffer\n");
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004341 ret = -EINVAL;
4342 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004343 }
4344
Chris Wilson05394f32010-11-08 19:18:58 +00004345 if (obj->pin_filp != NULL && obj->pin_filp != file) {
Jesse Barnes79e53942008-11-07 14:24:08 -08004346 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
4347 args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004348 ret = -EINVAL;
4349 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08004350 }
4351
Chris Wilson05394f32010-11-08 19:18:58 +00004352 obj->user_pin_count++;
4353 obj->pin_filp = file;
4354 if (obj->user_pin_count == 1) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01004355 ret = i915_gem_object_pin(obj, args->alignment, true);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004356 if (ret)
4357 goto out;
Eric Anholt673a3942008-07-30 12:06:12 -07004358 }
4359
4360 /* XXX - flush the CPU caches for pinned objects
4361 * as the X server doesn't manage domains yet
4362 */
Eric Anholte47c68e2008-11-14 13:35:19 -08004363 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00004364 args->offset = obj->gtt_offset;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004365out:
Chris Wilson05394f32010-11-08 19:18:58 +00004366 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004367unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07004368 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004369 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004370}
4371
4372int
4373i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00004374 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07004375{
4376 struct drm_i915_gem_pin *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004377 struct drm_i915_gem_object *obj;
Chris Wilson76c1dec2010-09-25 11:22:51 +01004378 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004379
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004380 ret = i915_mutex_lock_interruptible(dev);
4381 if (ret)
4382 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004383
Chris Wilson05394f32010-11-08 19:18:58 +00004384 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Eric Anholt673a3942008-07-30 12:06:12 -07004385 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004386 ret = -ENOENT;
4387 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07004388 }
Chris Wilson76c1dec2010-09-25 11:22:51 +01004389
Chris Wilson05394f32010-11-08 19:18:58 +00004390 if (obj->pin_filp != file) {
Jesse Barnes79e53942008-11-07 14:24:08 -08004391 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
4392 args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004393 ret = -EINVAL;
4394 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08004395 }
Chris Wilson05394f32010-11-08 19:18:58 +00004396 obj->user_pin_count--;
4397 if (obj->user_pin_count == 0) {
4398 obj->pin_filp = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08004399 i915_gem_object_unpin(obj);
4400 }
Eric Anholt673a3942008-07-30 12:06:12 -07004401
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004402out:
Chris Wilson05394f32010-11-08 19:18:58 +00004403 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004404unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07004405 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004406 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004407}
4408
4409int
4410i915_gem_busy_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00004411 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07004412{
4413 struct drm_i915_gem_busy *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004414 struct drm_i915_gem_object *obj;
Chris Wilson30dbf0c2010-09-25 10:19:17 +01004415 int ret;
4416
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004417 ret = i915_mutex_lock_interruptible(dev);
4418 if (ret)
4419 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004420
Chris Wilson05394f32010-11-08 19:18:58 +00004421 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Eric Anholt673a3942008-07-30 12:06:12 -07004422 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004423 ret = -ENOENT;
4424 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07004425 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08004426
Chris Wilson0be555b2010-08-04 15:36:30 +01004427 /* Count all active objects as busy, even if they are currently not used
4428 * by the gpu. Users of this interface expect objects to eventually
4429 * become non-busy without any further actions, therefore emit any
4430 * necessary flushes here.
Eric Anholtc4de0a52008-12-14 19:05:04 -08004431 */
Chris Wilson05394f32010-11-08 19:18:58 +00004432 args->busy = obj->active;
Chris Wilson0be555b2010-08-04 15:36:30 +01004433 if (args->busy) {
4434 /* Unconditionally flush objects, even when the gpu still uses this
4435 * object. Userspace calling this function indicates that it wants to
4436 * use this buffer rather sooner than later, so issuing the required
4437 * flush earlier is beneficial.
4438 */
Chris Wilson05394f32010-11-08 19:18:58 +00004439 if (obj->base.write_domain & I915_GEM_GPU_DOMAINS)
4440 i915_gem_flush_ring(dev, obj->ring,
4441 0, obj->base.write_domain);
Chris Wilson0be555b2010-08-04 15:36:30 +01004442
4443 /* Update the active list for the hardware's current position.
4444 * Otherwise this only updates on a delayed timer or when irqs
4445 * are actually unmasked, and our working set ends up being
4446 * larger than required.
4447 */
Chris Wilson05394f32010-11-08 19:18:58 +00004448 i915_gem_retire_requests_ring(dev, obj->ring);
Chris Wilson0be555b2010-08-04 15:36:30 +01004449
Chris Wilson05394f32010-11-08 19:18:58 +00004450 args->busy = obj->active;
Chris Wilson0be555b2010-08-04 15:36:30 +01004451 }
Eric Anholt673a3942008-07-30 12:06:12 -07004452
Chris Wilson05394f32010-11-08 19:18:58 +00004453 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004454unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07004455 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004456 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004457}
4458
4459int
4460i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4461 struct drm_file *file_priv)
4462{
4463 return i915_gem_ring_throttle(dev, file_priv);
4464}
4465
Chris Wilson3ef94da2009-09-14 16:50:29 +01004466int
4467i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4468 struct drm_file *file_priv)
4469{
4470 struct drm_i915_gem_madvise *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004471 struct drm_i915_gem_object *obj;
Chris Wilson76c1dec2010-09-25 11:22:51 +01004472 int ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004473
4474 switch (args->madv) {
4475 case I915_MADV_DONTNEED:
4476 case I915_MADV_WILLNEED:
4477 break;
4478 default:
4479 return -EINVAL;
4480 }
4481
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004482 ret = i915_mutex_lock_interruptible(dev);
4483 if (ret)
4484 return ret;
4485
Chris Wilson05394f32010-11-08 19:18:58 +00004486 obj = to_intel_bo(drm_gem_object_lookup(dev, file_priv, args->handle));
Chris Wilson3ef94da2009-09-14 16:50:29 +01004487 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004488 ret = -ENOENT;
4489 goto unlock;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004490 }
Chris Wilson3ef94da2009-09-14 16:50:29 +01004491
Chris Wilson05394f32010-11-08 19:18:58 +00004492 if (obj->pin_count) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004493 ret = -EINVAL;
4494 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004495 }
4496
Chris Wilson05394f32010-11-08 19:18:58 +00004497 if (obj->madv != __I915_MADV_PURGED)
4498 obj->madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004499
Chris Wilson2d7ef392009-09-20 23:13:10 +01004500 /* if the object is no longer bound, discard its backing storage */
Chris Wilson05394f32010-11-08 19:18:58 +00004501 if (i915_gem_object_is_purgeable(obj) &&
4502 obj->gtt_space == NULL)
Chris Wilson2d7ef392009-09-20 23:13:10 +01004503 i915_gem_object_truncate(obj);
4504
Chris Wilson05394f32010-11-08 19:18:58 +00004505 args->retained = obj->madv != __I915_MADV_PURGED;
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004506
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004507out:
Chris Wilson05394f32010-11-08 19:18:58 +00004508 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004509unlock:
Chris Wilson3ef94da2009-09-14 16:50:29 +01004510 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004511 return ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004512}
4513
Chris Wilson05394f32010-11-08 19:18:58 +00004514struct drm_i915_gem_object *i915_gem_alloc_object(struct drm_device *dev,
4515 size_t size)
Daniel Vetterac52bc52010-04-09 19:05:06 +00004516{
Chris Wilson73aa8082010-09-30 11:46:12 +01004517 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc397b902010-04-09 19:05:07 +00004518 struct drm_i915_gem_object *obj;
4519
4520 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
4521 if (obj == NULL)
4522 return NULL;
4523
4524 if (drm_gem_object_init(dev, &obj->base, size) != 0) {
4525 kfree(obj);
4526 return NULL;
4527 }
4528
Chris Wilson73aa8082010-09-30 11:46:12 +01004529 i915_gem_info_add_obj(dev_priv, size);
4530
Daniel Vetterc397b902010-04-09 19:05:07 +00004531 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4532 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4533
4534 obj->agp_type = AGP_USER_MEMORY;
Daniel Vetter62b8b212010-04-09 19:05:08 +00004535 obj->base.driver_private = NULL;
Daniel Vetterc397b902010-04-09 19:05:07 +00004536 obj->fence_reg = I915_FENCE_REG_NONE;
Chris Wilson69dc4982010-10-19 10:36:51 +01004537 INIT_LIST_HEAD(&obj->mm_list);
Daniel Vetter93a37f22010-11-05 20:24:53 +01004538 INIT_LIST_HEAD(&obj->gtt_list);
Chris Wilson69dc4982010-10-19 10:36:51 +01004539 INIT_LIST_HEAD(&obj->ring_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00004540 INIT_LIST_HEAD(&obj->gpu_write_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00004541 obj->madv = I915_MADV_WILLNEED;
Daniel Vetter75e9e912010-11-04 17:11:09 +01004542 /* Avoid an unnecessary call to unbind on the first bind. */
4543 obj->map_and_fenceable = true;
Daniel Vetterc397b902010-04-09 19:05:07 +00004544
Chris Wilson05394f32010-11-08 19:18:58 +00004545 return obj;
Daniel Vetterac52bc52010-04-09 19:05:06 +00004546}
4547
Eric Anholt673a3942008-07-30 12:06:12 -07004548int i915_gem_init_object(struct drm_gem_object *obj)
4549{
Daniel Vetterc397b902010-04-09 19:05:07 +00004550 BUG();
Jesse Barnesde151cf2008-11-12 10:03:55 -08004551
Eric Anholt673a3942008-07-30 12:06:12 -07004552 return 0;
4553}
4554
Chris Wilson05394f32010-11-08 19:18:58 +00004555static void i915_gem_free_object_tail(struct drm_i915_gem_object *obj)
Chris Wilsonbe726152010-07-23 23:18:50 +01004556{
Chris Wilson05394f32010-11-08 19:18:58 +00004557 struct drm_device *dev = obj->base.dev;
Chris Wilsonbe726152010-07-23 23:18:50 +01004558 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonbe726152010-07-23 23:18:50 +01004559 int ret;
4560
4561 ret = i915_gem_object_unbind(obj);
4562 if (ret == -ERESTARTSYS) {
Chris Wilson05394f32010-11-08 19:18:58 +00004563 list_move(&obj->mm_list,
Chris Wilsonbe726152010-07-23 23:18:50 +01004564 &dev_priv->mm.deferred_free_list);
4565 return;
4566 }
4567
Chris Wilson05394f32010-11-08 19:18:58 +00004568 if (obj->base.map_list.map)
Chris Wilsonbe726152010-07-23 23:18:50 +01004569 i915_gem_free_mmap_offset(obj);
4570
Chris Wilson05394f32010-11-08 19:18:58 +00004571 drm_gem_object_release(&obj->base);
4572 i915_gem_info_remove_obj(dev_priv, obj->base.size);
Chris Wilsonbe726152010-07-23 23:18:50 +01004573
Chris Wilson05394f32010-11-08 19:18:58 +00004574 kfree(obj->page_cpu_valid);
4575 kfree(obj->bit_17);
4576 kfree(obj);
Chris Wilsonbe726152010-07-23 23:18:50 +01004577}
4578
Chris Wilson05394f32010-11-08 19:18:58 +00004579void i915_gem_free_object(struct drm_gem_object *gem_obj)
Eric Anholt673a3942008-07-30 12:06:12 -07004580{
Chris Wilson05394f32010-11-08 19:18:58 +00004581 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
4582 struct drm_device *dev = obj->base.dev;
Eric Anholt673a3942008-07-30 12:06:12 -07004583
Chris Wilson1c5d22f2009-08-25 11:15:50 +01004584 trace_i915_gem_object_destroy(obj);
4585
Chris Wilson05394f32010-11-08 19:18:58 +00004586 while (obj->pin_count > 0)
Eric Anholt673a3942008-07-30 12:06:12 -07004587 i915_gem_object_unpin(obj);
4588
Chris Wilson05394f32010-11-08 19:18:58 +00004589 if (obj->phys_obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004590 i915_gem_detach_phys_object(dev, obj);
4591
Chris Wilsonbe726152010-07-23 23:18:50 +01004592 i915_gem_free_object_tail(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004593}
4594
Jesse Barnes5669fca2009-02-17 15:13:31 -08004595int
Eric Anholt673a3942008-07-30 12:06:12 -07004596i915_gem_idle(struct drm_device *dev)
4597{
4598 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson29105cc2010-01-07 10:39:13 +00004599 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004600
Keith Packard6dbe2772008-10-14 21:41:13 -07004601 mutex_lock(&dev->struct_mutex);
4602
Chris Wilson87acb0a2010-10-19 10:13:00 +01004603 if (dev_priv->mm.suspended) {
Keith Packard6dbe2772008-10-14 21:41:13 -07004604 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004605 return 0;
Keith Packard6dbe2772008-10-14 21:41:13 -07004606 }
Eric Anholt673a3942008-07-30 12:06:12 -07004607
Chris Wilson29105cc2010-01-07 10:39:13 +00004608 ret = i915_gpu_idle(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004609 if (ret) {
4610 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004611 return ret;
Keith Packard6dbe2772008-10-14 21:41:13 -07004612 }
Eric Anholt673a3942008-07-30 12:06:12 -07004613
Chris Wilson29105cc2010-01-07 10:39:13 +00004614 /* Under UMS, be paranoid and evict. */
4615 if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
Chris Wilson5eac3ab2010-10-31 08:49:47 +00004616 ret = i915_gem_evict_inactive(dev, false);
Chris Wilson29105cc2010-01-07 10:39:13 +00004617 if (ret) {
4618 mutex_unlock(&dev->struct_mutex);
4619 return ret;
4620 }
4621 }
4622
Chris Wilson312817a2010-11-22 11:50:11 +00004623 i915_gem_reset_fences(dev);
4624
Chris Wilson29105cc2010-01-07 10:39:13 +00004625 /* Hack! Don't let anybody do execbuf while we don't control the chip.
4626 * We need to replace this with a semaphore, or something.
4627 * And not confound mm.suspended!
4628 */
4629 dev_priv->mm.suspended = 1;
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02004630 del_timer_sync(&dev_priv->hangcheck_timer);
Chris Wilson29105cc2010-01-07 10:39:13 +00004631
4632 i915_kernel_lost_context(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004633 i915_gem_cleanup_ringbuffer(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00004634
Keith Packard6dbe2772008-10-14 21:41:13 -07004635 mutex_unlock(&dev->struct_mutex);
4636
Chris Wilson29105cc2010-01-07 10:39:13 +00004637 /* Cancel the retire work handler, which should be idle now. */
4638 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
4639
Eric Anholt673a3942008-07-30 12:06:12 -07004640 return 0;
4641}
4642
Eric Anholt673a3942008-07-30 12:06:12 -07004643int
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004644i915_gem_init_ringbuffer(struct drm_device *dev)
4645{
4646 drm_i915_private_t *dev_priv = dev->dev_private;
4647 int ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01004648
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08004649 ret = intel_init_render_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004650 if (ret)
Chris Wilsonb6913e42010-11-12 10:46:37 +00004651 return ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01004652
4653 if (HAS_BSD(dev)) {
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08004654 ret = intel_init_bsd_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004655 if (ret)
4656 goto cleanup_render_ring;
Zou Nan haid1b851f2010-05-21 09:08:57 +08004657 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004658
Chris Wilson549f7362010-10-19 11:19:32 +01004659 if (HAS_BLT(dev)) {
4660 ret = intel_init_blt_ring_buffer(dev);
4661 if (ret)
4662 goto cleanup_bsd_ring;
4663 }
4664
Chris Wilson6f392d5482010-08-07 11:01:22 +01004665 dev_priv->next_seqno = 1;
4666
Chris Wilson68f95ba2010-05-27 13:18:22 +01004667 return 0;
4668
Chris Wilson549f7362010-10-19 11:19:32 +01004669cleanup_bsd_ring:
Chris Wilson78501ea2010-10-27 12:18:21 +01004670 intel_cleanup_ring_buffer(&dev_priv->bsd_ring);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004671cleanup_render_ring:
Chris Wilson78501ea2010-10-27 12:18:21 +01004672 intel_cleanup_ring_buffer(&dev_priv->render_ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004673 return ret;
4674}
4675
4676void
4677i915_gem_cleanup_ringbuffer(struct drm_device *dev)
4678{
4679 drm_i915_private_t *dev_priv = dev->dev_private;
4680
Chris Wilson78501ea2010-10-27 12:18:21 +01004681 intel_cleanup_ring_buffer(&dev_priv->render_ring);
4682 intel_cleanup_ring_buffer(&dev_priv->bsd_ring);
4683 intel_cleanup_ring_buffer(&dev_priv->blt_ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004684}
4685
4686int
Eric Anholt673a3942008-07-30 12:06:12 -07004687i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
4688 struct drm_file *file_priv)
4689{
4690 drm_i915_private_t *dev_priv = dev->dev_private;
4691 int ret;
4692
Jesse Barnes79e53942008-11-07 14:24:08 -08004693 if (drm_core_check_feature(dev, DRIVER_MODESET))
4694 return 0;
4695
Ben Gamariba1234d2009-09-14 17:48:47 -04004696 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07004697 DRM_ERROR("Reenabling wedged hardware, good luck\n");
Ben Gamariba1234d2009-09-14 17:48:47 -04004698 atomic_set(&dev_priv->mm.wedged, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004699 }
4700
Eric Anholt673a3942008-07-30 12:06:12 -07004701 mutex_lock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004702 dev_priv->mm.suspended = 0;
4703
4704 ret = i915_gem_init_ringbuffer(dev);
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004705 if (ret != 0) {
4706 mutex_unlock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004707 return ret;
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004708 }
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004709
Chris Wilson69dc4982010-10-19 10:36:51 +01004710 BUG_ON(!list_empty(&dev_priv->mm.active_list));
Zou Nan hai852835f2010-05-21 09:08:56 +08004711 BUG_ON(!list_empty(&dev_priv->render_ring.active_list));
Chris Wilson87acb0a2010-10-19 10:13:00 +01004712 BUG_ON(!list_empty(&dev_priv->bsd_ring.active_list));
Chris Wilson549f7362010-10-19 11:19:32 +01004713 BUG_ON(!list_empty(&dev_priv->blt_ring.active_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004714 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
4715 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
Zou Nan hai852835f2010-05-21 09:08:56 +08004716 BUG_ON(!list_empty(&dev_priv->render_ring.request_list));
Chris Wilson87acb0a2010-10-19 10:13:00 +01004717 BUG_ON(!list_empty(&dev_priv->bsd_ring.request_list));
Chris Wilson549f7362010-10-19 11:19:32 +01004718 BUG_ON(!list_empty(&dev_priv->blt_ring.request_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004719 mutex_unlock(&dev->struct_mutex);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004720
Chris Wilson5f353082010-06-07 14:03:03 +01004721 ret = drm_irq_install(dev);
4722 if (ret)
4723 goto cleanup_ringbuffer;
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004724
Eric Anholt673a3942008-07-30 12:06:12 -07004725 return 0;
Chris Wilson5f353082010-06-07 14:03:03 +01004726
4727cleanup_ringbuffer:
4728 mutex_lock(&dev->struct_mutex);
4729 i915_gem_cleanup_ringbuffer(dev);
4730 dev_priv->mm.suspended = 1;
4731 mutex_unlock(&dev->struct_mutex);
4732
4733 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004734}
4735
4736int
4737i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
4738 struct drm_file *file_priv)
4739{
Jesse Barnes79e53942008-11-07 14:24:08 -08004740 if (drm_core_check_feature(dev, DRIVER_MODESET))
4741 return 0;
4742
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004743 drm_irq_uninstall(dev);
Linus Torvaldse6890f62009-09-08 17:09:24 -07004744 return i915_gem_idle(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004745}
4746
4747void
4748i915_gem_lastclose(struct drm_device *dev)
4749{
4750 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004751
Eric Anholte806b492009-01-22 09:56:58 -08004752 if (drm_core_check_feature(dev, DRIVER_MODESET))
4753 return;
4754
Keith Packard6dbe2772008-10-14 21:41:13 -07004755 ret = i915_gem_idle(dev);
4756 if (ret)
4757 DRM_ERROR("failed to idle hardware: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07004758}
4759
Chris Wilson64193402010-10-24 12:38:05 +01004760static void
4761init_ring_lists(struct intel_ring_buffer *ring)
4762{
4763 INIT_LIST_HEAD(&ring->active_list);
4764 INIT_LIST_HEAD(&ring->request_list);
4765 INIT_LIST_HEAD(&ring->gpu_write_list);
4766}
4767
Eric Anholt673a3942008-07-30 12:06:12 -07004768void
4769i915_gem_load(struct drm_device *dev)
4770{
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004771 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07004772 drm_i915_private_t *dev_priv = dev->dev_private;
4773
Chris Wilson69dc4982010-10-19 10:36:51 +01004774 INIT_LIST_HEAD(&dev_priv->mm.active_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004775 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
4776 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004777 INIT_LIST_HEAD(&dev_priv->mm.pinned_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07004778 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Chris Wilsonbe726152010-07-23 23:18:50 +01004779 INIT_LIST_HEAD(&dev_priv->mm.deferred_free_list);
Daniel Vetter93a37f22010-11-05 20:24:53 +01004780 INIT_LIST_HEAD(&dev_priv->mm.gtt_list);
Chris Wilson64193402010-10-24 12:38:05 +01004781 init_ring_lists(&dev_priv->render_ring);
4782 init_ring_lists(&dev_priv->bsd_ring);
4783 init_ring_lists(&dev_priv->blt_ring);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02004784 for (i = 0; i < 16; i++)
4785 INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004786 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
4787 i915_gem_retire_work_handler);
Chris Wilson30dbf0c2010-09-25 10:19:17 +01004788 init_completion(&dev_priv->error_completion);
Chris Wilson31169712009-09-14 16:50:28 +01004789
Dave Airlie94400122010-07-20 13:15:31 +10004790 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
4791 if (IS_GEN3(dev)) {
4792 u32 tmp = I915_READ(MI_ARB_STATE);
4793 if (!(tmp & MI_ARB_C3_LP_WRITE_ENABLE)) {
4794 /* arb state is a masked write, so set bit + bit in mask */
4795 tmp = MI_ARB_C3_LP_WRITE_ENABLE | (MI_ARB_C3_LP_WRITE_ENABLE << MI_ARB_MASK_SHIFT);
4796 I915_WRITE(MI_ARB_STATE, tmp);
4797 }
4798 }
4799
Jesse Barnesde151cf2008-11-12 10:03:55 -08004800 /* Old X drivers will take 0-2 for front, back, depth buffers */
Eric Anholtb397c832010-01-26 09:43:10 -08004801 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4802 dev_priv->fence_reg_start = 3;
Jesse Barnesde151cf2008-11-12 10:03:55 -08004803
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004804 if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08004805 dev_priv->num_fence_regs = 16;
4806 else
4807 dev_priv->num_fence_regs = 8;
4808
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004809 /* Initialize fence registers to zero */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004810 switch (INTEL_INFO(dev)->gen) {
4811 case 6:
4812 for (i = 0; i < 16; i++)
4813 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (i * 8), 0);
4814 break;
4815 case 5:
4816 case 4:
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004817 for (i = 0; i < 16; i++)
4818 I915_WRITE64(FENCE_REG_965_0 + (i * 8), 0);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004819 break;
4820 case 3:
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004821 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4822 for (i = 0; i < 8; i++)
4823 I915_WRITE(FENCE_REG_945_8 + (i * 4), 0);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004824 case 2:
4825 for (i = 0; i < 8; i++)
4826 I915_WRITE(FENCE_REG_830_0 + (i * 4), 0);
4827 break;
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004828 }
Eric Anholt673a3942008-07-30 12:06:12 -07004829 i915_gem_detect_bit_6_swizzle(dev);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05004830 init_waitqueue_head(&dev_priv->pending_flip_queue);
Chris Wilson17250b72010-10-28 12:51:39 +01004831
4832 dev_priv->mm.inactive_shrinker.shrink = i915_gem_inactive_shrink;
4833 dev_priv->mm.inactive_shrinker.seeks = DEFAULT_SEEKS;
4834 register_shrinker(&dev_priv->mm.inactive_shrinker);
Eric Anholt673a3942008-07-30 12:06:12 -07004835}
Dave Airlie71acb5e2008-12-30 20:31:46 +10004836
4837/*
4838 * Create a physically contiguous memory object for this object
4839 * e.g. for cursor + overlay regs
4840 */
Chris Wilson995b6762010-08-20 13:23:26 +01004841static int i915_gem_init_phys_object(struct drm_device *dev,
4842 int id, int size, int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004843{
4844 drm_i915_private_t *dev_priv = dev->dev_private;
4845 struct drm_i915_gem_phys_object *phys_obj;
4846 int ret;
4847
4848 if (dev_priv->mm.phys_objs[id - 1] || !size)
4849 return 0;
4850
Eric Anholt9a298b22009-03-24 12:23:04 -07004851 phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004852 if (!phys_obj)
4853 return -ENOMEM;
4854
4855 phys_obj->id = id;
4856
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004857 phys_obj->handle = drm_pci_alloc(dev, size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004858 if (!phys_obj->handle) {
4859 ret = -ENOMEM;
4860 goto kfree_obj;
4861 }
4862#ifdef CONFIG_X86
4863 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4864#endif
4865
4866 dev_priv->mm.phys_objs[id - 1] = phys_obj;
4867
4868 return 0;
4869kfree_obj:
Eric Anholt9a298b22009-03-24 12:23:04 -07004870 kfree(phys_obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004871 return ret;
4872}
4873
Chris Wilson995b6762010-08-20 13:23:26 +01004874static void i915_gem_free_phys_object(struct drm_device *dev, int id)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004875{
4876 drm_i915_private_t *dev_priv = dev->dev_private;
4877 struct drm_i915_gem_phys_object *phys_obj;
4878
4879 if (!dev_priv->mm.phys_objs[id - 1])
4880 return;
4881
4882 phys_obj = dev_priv->mm.phys_objs[id - 1];
4883 if (phys_obj->cur_obj) {
4884 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
4885 }
4886
4887#ifdef CONFIG_X86
4888 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4889#endif
4890 drm_pci_free(dev, phys_obj->handle);
4891 kfree(phys_obj);
4892 dev_priv->mm.phys_objs[id - 1] = NULL;
4893}
4894
4895void i915_gem_free_all_phys_object(struct drm_device *dev)
4896{
4897 int i;
4898
Dave Airlie260883c2009-01-22 17:58:49 +10004899 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004900 i915_gem_free_phys_object(dev, i);
4901}
4902
4903void i915_gem_detach_phys_object(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00004904 struct drm_i915_gem_object *obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004905{
Chris Wilson05394f32010-11-08 19:18:58 +00004906 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Chris Wilsone5281cc2010-10-28 13:45:36 +01004907 char *vaddr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004908 int i;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004909 int page_count;
4910
Chris Wilson05394f32010-11-08 19:18:58 +00004911 if (!obj->phys_obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004912 return;
Chris Wilson05394f32010-11-08 19:18:58 +00004913 vaddr = obj->phys_obj->handle->vaddr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004914
Chris Wilson05394f32010-11-08 19:18:58 +00004915 page_count = obj->base.size / PAGE_SIZE;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004916 for (i = 0; i < page_count; i++) {
Chris Wilsone5281cc2010-10-28 13:45:36 +01004917 struct page *page = read_cache_page_gfp(mapping, i,
4918 GFP_HIGHUSER | __GFP_RECLAIMABLE);
4919 if (!IS_ERR(page)) {
4920 char *dst = kmap_atomic(page);
4921 memcpy(dst, vaddr + i*PAGE_SIZE, PAGE_SIZE);
4922 kunmap_atomic(dst);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004923
Chris Wilsone5281cc2010-10-28 13:45:36 +01004924 drm_clflush_pages(&page, 1);
4925
4926 set_page_dirty(page);
4927 mark_page_accessed(page);
4928 page_cache_release(page);
4929 }
Dave Airlie71acb5e2008-12-30 20:31:46 +10004930 }
Daniel Vetter40ce6572010-11-05 18:12:18 +01004931 intel_gtt_chipset_flush();
Chris Wilsond78b47b2009-06-17 21:52:49 +01004932
Chris Wilson05394f32010-11-08 19:18:58 +00004933 obj->phys_obj->cur_obj = NULL;
4934 obj->phys_obj = NULL;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004935}
4936
4937int
4938i915_gem_attach_phys_object(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00004939 struct drm_i915_gem_object *obj,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004940 int id,
4941 int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004942{
Chris Wilson05394f32010-11-08 19:18:58 +00004943 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004944 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004945 int ret = 0;
4946 int page_count;
4947 int i;
4948
4949 if (id > I915_MAX_PHYS_OBJECT)
4950 return -EINVAL;
4951
Chris Wilson05394f32010-11-08 19:18:58 +00004952 if (obj->phys_obj) {
4953 if (obj->phys_obj->id == id)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004954 return 0;
4955 i915_gem_detach_phys_object(dev, obj);
4956 }
4957
Dave Airlie71acb5e2008-12-30 20:31:46 +10004958 /* create a new object */
4959 if (!dev_priv->mm.phys_objs[id - 1]) {
4960 ret = i915_gem_init_phys_object(dev, id,
Chris Wilson05394f32010-11-08 19:18:58 +00004961 obj->base.size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004962 if (ret) {
Chris Wilson05394f32010-11-08 19:18:58 +00004963 DRM_ERROR("failed to init phys object %d size: %zu\n",
4964 id, obj->base.size);
Chris Wilsone5281cc2010-10-28 13:45:36 +01004965 return ret;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004966 }
4967 }
4968
4969 /* bind to the object */
Chris Wilson05394f32010-11-08 19:18:58 +00004970 obj->phys_obj = dev_priv->mm.phys_objs[id - 1];
4971 obj->phys_obj->cur_obj = obj;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004972
Chris Wilson05394f32010-11-08 19:18:58 +00004973 page_count = obj->base.size / PAGE_SIZE;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004974
4975 for (i = 0; i < page_count; i++) {
Chris Wilsone5281cc2010-10-28 13:45:36 +01004976 struct page *page;
4977 char *dst, *src;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004978
Chris Wilsone5281cc2010-10-28 13:45:36 +01004979 page = read_cache_page_gfp(mapping, i,
4980 GFP_HIGHUSER | __GFP_RECLAIMABLE);
4981 if (IS_ERR(page))
4982 return PTR_ERR(page);
4983
Chris Wilsonff75b9b2010-10-30 22:52:31 +01004984 src = kmap_atomic(page);
Chris Wilson05394f32010-11-08 19:18:58 +00004985 dst = obj->phys_obj->handle->vaddr + (i * PAGE_SIZE);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004986 memcpy(dst, src, PAGE_SIZE);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -07004987 kunmap_atomic(src);
Chris Wilsone5281cc2010-10-28 13:45:36 +01004988
4989 mark_page_accessed(page);
4990 page_cache_release(page);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004991 }
4992
4993 return 0;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004994}
4995
4996static int
Chris Wilson05394f32010-11-08 19:18:58 +00004997i915_gem_phys_pwrite(struct drm_device *dev,
4998 struct drm_i915_gem_object *obj,
Dave Airlie71acb5e2008-12-30 20:31:46 +10004999 struct drm_i915_gem_pwrite *args,
5000 struct drm_file *file_priv)
5001{
Chris Wilson05394f32010-11-08 19:18:58 +00005002 void *vaddr = obj->phys_obj->handle->vaddr + args->offset;
Chris Wilsonb47b30c2010-11-08 01:12:29 +00005003 char __user *user_data = (char __user *) (uintptr_t) args->data_ptr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10005004
Chris Wilsonb47b30c2010-11-08 01:12:29 +00005005 if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) {
5006 unsigned long unwritten;
5007
5008 /* The physical object once assigned is fixed for the lifetime
5009 * of the obj, so we can safely drop the lock and continue
5010 * to access vaddr.
5011 */
5012 mutex_unlock(&dev->struct_mutex);
5013 unwritten = copy_from_user(vaddr, user_data, args->size);
5014 mutex_lock(&dev->struct_mutex);
5015 if (unwritten)
5016 return -EFAULT;
5017 }
Dave Airlie71acb5e2008-12-30 20:31:46 +10005018
Daniel Vetter40ce6572010-11-05 18:12:18 +01005019 intel_gtt_chipset_flush();
Dave Airlie71acb5e2008-12-30 20:31:46 +10005020 return 0;
5021}
Eric Anholtb9624422009-06-03 07:27:35 +00005022
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005023void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00005024{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005025 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00005026
5027 /* Clean up our request list when the client is going away, so that
5028 * later retire_requests won't dereference our soon-to-be-gone
5029 * file_priv.
5030 */
Chris Wilson1c255952010-09-26 11:03:27 +01005031 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005032 while (!list_empty(&file_priv->mm.request_list)) {
5033 struct drm_i915_gem_request *request;
5034
5035 request = list_first_entry(&file_priv->mm.request_list,
5036 struct drm_i915_gem_request,
5037 client_list);
5038 list_del(&request->client_list);
5039 request->file_priv = NULL;
5040 }
Chris Wilson1c255952010-09-26 11:03:27 +01005041 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00005042}
Chris Wilson31169712009-09-14 16:50:28 +01005043
Chris Wilson31169712009-09-14 16:50:28 +01005044static int
Chris Wilson1637ef42010-04-20 17:10:35 +01005045i915_gpu_is_active(struct drm_device *dev)
5046{
5047 drm_i915_private_t *dev_priv = dev->dev_private;
5048 int lists_empty;
5049
Chris Wilson1637ef42010-04-20 17:10:35 +01005050 lists_empty = list_empty(&dev_priv->mm.flushing_list) &&
Chris Wilson17250b72010-10-28 12:51:39 +01005051 list_empty(&dev_priv->mm.active_list);
Chris Wilson1637ef42010-04-20 17:10:35 +01005052
5053 return !lists_empty;
5054}
5055
5056static int
Chris Wilson17250b72010-10-28 12:51:39 +01005057i915_gem_inactive_shrink(struct shrinker *shrinker,
5058 int nr_to_scan,
5059 gfp_t gfp_mask)
Chris Wilson31169712009-09-14 16:50:28 +01005060{
Chris Wilson17250b72010-10-28 12:51:39 +01005061 struct drm_i915_private *dev_priv =
5062 container_of(shrinker,
5063 struct drm_i915_private,
5064 mm.inactive_shrinker);
5065 struct drm_device *dev = dev_priv->dev;
5066 struct drm_i915_gem_object *obj, *next;
5067 int cnt;
5068
5069 if (!mutex_trylock(&dev->struct_mutex))
Chris Wilsonbbe2e112010-10-28 22:35:07 +01005070 return 0;
Chris Wilson31169712009-09-14 16:50:28 +01005071
5072 /* "fast-path" to count number of available objects */
5073 if (nr_to_scan == 0) {
Chris Wilson17250b72010-10-28 12:51:39 +01005074 cnt = 0;
5075 list_for_each_entry(obj,
5076 &dev_priv->mm.inactive_list,
5077 mm_list)
5078 cnt++;
5079 mutex_unlock(&dev->struct_mutex);
5080 return cnt / 100 * sysctl_vfs_cache_pressure;
Chris Wilson31169712009-09-14 16:50:28 +01005081 }
5082
Chris Wilson1637ef42010-04-20 17:10:35 +01005083rescan:
Chris Wilson31169712009-09-14 16:50:28 +01005084 /* first scan for clean buffers */
Chris Wilson17250b72010-10-28 12:51:39 +01005085 i915_gem_retire_requests(dev);
Chris Wilson31169712009-09-14 16:50:28 +01005086
Chris Wilson17250b72010-10-28 12:51:39 +01005087 list_for_each_entry_safe(obj, next,
5088 &dev_priv->mm.inactive_list,
5089 mm_list) {
5090 if (i915_gem_object_is_purgeable(obj)) {
Chris Wilson20217462010-11-23 15:26:33 +00005091 if (i915_gem_object_unbind(obj) == 0 &&
5092 --nr_to_scan == 0)
Chris Wilson17250b72010-10-28 12:51:39 +01005093 break;
Chris Wilson31169712009-09-14 16:50:28 +01005094 }
Chris Wilson31169712009-09-14 16:50:28 +01005095 }
5096
5097 /* second pass, evict/count anything still on the inactive list */
Chris Wilson17250b72010-10-28 12:51:39 +01005098 cnt = 0;
5099 list_for_each_entry_safe(obj, next,
5100 &dev_priv->mm.inactive_list,
5101 mm_list) {
Chris Wilson20217462010-11-23 15:26:33 +00005102 if (nr_to_scan &&
5103 i915_gem_object_unbind(obj) == 0)
Chris Wilson17250b72010-10-28 12:51:39 +01005104 nr_to_scan--;
Chris Wilson20217462010-11-23 15:26:33 +00005105 else
Chris Wilson17250b72010-10-28 12:51:39 +01005106 cnt++;
Chris Wilson31169712009-09-14 16:50:28 +01005107 }
5108
Chris Wilson17250b72010-10-28 12:51:39 +01005109 if (nr_to_scan && i915_gpu_is_active(dev)) {
Chris Wilson1637ef42010-04-20 17:10:35 +01005110 /*
5111 * We are desperate for pages, so as a last resort, wait
5112 * for the GPU to finish and discard whatever we can.
5113 * This has a dramatic impact to reduce the number of
5114 * OOM-killer events whilst running the GPU aggressively.
5115 */
Chris Wilson17250b72010-10-28 12:51:39 +01005116 if (i915_gpu_idle(dev) == 0)
Chris Wilson1637ef42010-04-20 17:10:35 +01005117 goto rescan;
5118 }
Chris Wilson17250b72010-10-28 12:51:39 +01005119 mutex_unlock(&dev->struct_mutex);
5120 return cnt / 100 * sysctl_vfs_cache_pressure;
Chris Wilson31169712009-09-14 16:50:28 +01005121}