blob: f9c093c08d58577743c9ae055924da16564b27b0 [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 Wilson3619df02010-11-28 15:37:17 +000038static void i915_gem_object_flush_gpu_write_domain(struct drm_i915_gem_object *obj);
Chris Wilson05394f32010-11-08 19:18:58 +000039static void i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj);
40static void i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj);
41static int i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj,
Chris Wilson919926a2010-11-12 13:42:53 +000042 bool write);
Chris Wilson05394f32010-11-08 19:18:58 +000043static int i915_gem_object_set_cpu_read_domain_range(struct drm_i915_gem_object *obj,
Eric Anholte47c68e2008-11-14 13:35:19 -080044 uint64_t offset,
45 uint64_t size);
Chris Wilson05394f32010-11-08 19:18:58 +000046static void i915_gem_object_set_to_full_cpu_read_domain(struct drm_i915_gem_object *obj);
Chris Wilson05394f32010-11-08 19:18:58 +000047static int i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
Chris Wilsona00b10c2010-09-24 21:15:47 +010048 unsigned alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +010049 bool map_and_fenceable);
Chris Wilsond9e86c02010-11-10 16:40:20 +000050static void i915_gem_clear_fence_reg(struct drm_device *dev,
51 struct drm_i915_fence_reg *reg);
Chris Wilson05394f32010-11-08 19:18:58 +000052static int i915_gem_phys_pwrite(struct drm_device *dev,
53 struct drm_i915_gem_object *obj,
Dave Airlie71acb5e2008-12-30 20:31:46 +100054 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +000055 struct drm_file *file);
56static void i915_gem_free_object_tail(struct drm_i915_gem_object *obj);
Eric Anholt673a3942008-07-30 12:06:12 -070057
Chris Wilson17250b72010-10-28 12:51:39 +010058static int i915_gem_inactive_shrink(struct shrinker *shrinker,
59 int nr_to_scan,
60 gfp_t gfp_mask);
61
Chris Wilson31169712009-09-14 16:50:28 +010062
Chris Wilson73aa8082010-09-30 11:46:12 +010063/* some bookkeeping */
64static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
65 size_t size)
66{
67 dev_priv->mm.object_count++;
68 dev_priv->mm.object_memory += size;
69}
70
71static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
72 size_t size)
73{
74 dev_priv->mm.object_count--;
75 dev_priv->mm.object_memory -= size;
76}
77
Chris Wilson30dbf0c2010-09-25 10:19:17 +010078int
79i915_gem_check_is_wedged(struct drm_device *dev)
80{
81 struct drm_i915_private *dev_priv = dev->dev_private;
82 struct completion *x = &dev_priv->error_completion;
83 unsigned long flags;
84 int ret;
85
86 if (!atomic_read(&dev_priv->mm.wedged))
87 return 0;
88
89 ret = wait_for_completion_interruptible(x);
90 if (ret)
91 return ret;
92
93 /* Success, we reset the GPU! */
94 if (!atomic_read(&dev_priv->mm.wedged))
95 return 0;
96
97 /* GPU is hung, bump the completion count to account for
98 * the token we just consumed so that we never hit zero and
99 * end up waiting upon a subsequent completion event that
100 * will never happen.
101 */
102 spin_lock_irqsave(&x->wait.lock, flags);
103 x->done++;
104 spin_unlock_irqrestore(&x->wait.lock, flags);
105 return -EIO;
106}
107
Chris Wilson54cf91d2010-11-25 18:00:26 +0000108int i915_mutex_lock_interruptible(struct drm_device *dev)
Chris Wilson76c1dec2010-09-25 11:22:51 +0100109{
110 struct drm_i915_private *dev_priv = dev->dev_private;
111 int ret;
112
113 ret = i915_gem_check_is_wedged(dev);
114 if (ret)
115 return ret;
116
117 ret = mutex_lock_interruptible(&dev->struct_mutex);
118 if (ret)
119 return ret;
120
121 if (atomic_read(&dev_priv->mm.wedged)) {
122 mutex_unlock(&dev->struct_mutex);
123 return -EAGAIN;
124 }
125
Chris Wilson23bc5982010-09-29 16:10:57 +0100126 WARN_ON(i915_verify_lists(dev));
Chris Wilson76c1dec2010-09-25 11:22:51 +0100127 return 0;
128}
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100129
Chris Wilson7d1c4802010-08-07 21:45:03 +0100130static inline bool
Chris Wilson05394f32010-11-08 19:18:58 +0000131i915_gem_object_is_inactive(struct drm_i915_gem_object *obj)
Chris Wilson7d1c4802010-08-07 21:45:03 +0100132{
Chris Wilson05394f32010-11-08 19:18:58 +0000133 return obj->gtt_space && !obj->active && obj->pin_count == 0;
Chris Wilson7d1c4802010-08-07 21:45:03 +0100134}
135
Chris Wilson20217462010-11-23 15:26:33 +0000136void i915_gem_do_init(struct drm_device *dev,
137 unsigned long start,
138 unsigned long mappable_end,
139 unsigned long end)
Jesse Barnes79e53942008-11-07 14:24:08 -0800140{
141 drm_i915_private_t *dev_priv = dev->dev_private;
142
Jesse Barnes79e53942008-11-07 14:24:08 -0800143 drm_mm_init(&dev_priv->mm.gtt_space, start,
144 end - start);
145
Chris Wilson73aa8082010-09-30 11:46:12 +0100146 dev_priv->mm.gtt_total = end - start;
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200147 dev_priv->mm.mappable_gtt_total = min(end, mappable_end) - start;
Daniel Vetter53984632010-09-22 23:44:24 +0200148 dev_priv->mm.gtt_mappable_end = mappable_end;
Jesse Barnes79e53942008-11-07 14:24:08 -0800149}
Keith Packard6dbe2772008-10-14 21:41:13 -0700150
Eric Anholt673a3942008-07-30 12:06:12 -0700151int
152i915_gem_init_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000153 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700154{
Eric Anholt673a3942008-07-30 12:06:12 -0700155 struct drm_i915_gem_init *args = data;
Chris Wilson20217462010-11-23 15:26:33 +0000156
157 if (args->gtt_start >= args->gtt_end ||
158 (args->gtt_end | args->gtt_start) & (PAGE_SIZE - 1))
159 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -0700160
161 mutex_lock(&dev->struct_mutex);
Chris Wilson20217462010-11-23 15:26:33 +0000162 i915_gem_do_init(dev, args->gtt_start, args->gtt_end, args->gtt_end);
Eric Anholt673a3942008-07-30 12:06:12 -0700163 mutex_unlock(&dev->struct_mutex);
164
Chris Wilson20217462010-11-23 15:26:33 +0000165 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700166}
167
Eric Anholt5a125c32008-10-22 21:40:13 -0700168int
169i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000170 struct drm_file *file)
Eric Anholt5a125c32008-10-22 21:40:13 -0700171{
Chris Wilson73aa8082010-09-30 11:46:12 +0100172 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt5a125c32008-10-22 21:40:13 -0700173 struct drm_i915_gem_get_aperture *args = data;
Chris Wilson6299f992010-11-24 12:23:44 +0000174 struct drm_i915_gem_object *obj;
175 size_t pinned;
Eric Anholt5a125c32008-10-22 21:40:13 -0700176
177 if (!(dev->driver->driver_features & DRIVER_GEM))
178 return -ENODEV;
179
Chris Wilson6299f992010-11-24 12:23:44 +0000180 pinned = 0;
Chris Wilson73aa8082010-09-30 11:46:12 +0100181 mutex_lock(&dev->struct_mutex);
Chris Wilson6299f992010-11-24 12:23:44 +0000182 list_for_each_entry(obj, &dev_priv->mm.pinned_list, mm_list)
183 pinned += obj->gtt_space->size;
Chris Wilson73aa8082010-09-30 11:46:12 +0100184 mutex_unlock(&dev->struct_mutex);
Eric Anholt5a125c32008-10-22 21:40:13 -0700185
Chris Wilson6299f992010-11-24 12:23:44 +0000186 args->aper_size = dev_priv->mm.gtt_total;
187 args->aper_available_size = args->aper_size -pinned;
188
Eric Anholt5a125c32008-10-22 21:40:13 -0700189 return 0;
190}
191
Eric Anholt673a3942008-07-30 12:06:12 -0700192/**
193 * Creates a new mm object and returns a handle to it.
194 */
195int
196i915_gem_create_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000197 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700198{
199 struct drm_i915_gem_create *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +0000200 struct drm_i915_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300201 int ret;
202 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700203
204 args->size = roundup(args->size, PAGE_SIZE);
205
206 /* Allocate the new object */
Daniel Vetterac52bc52010-04-09 19:05:06 +0000207 obj = i915_gem_alloc_object(dev, args->size);
Eric Anholt673a3942008-07-30 12:06:12 -0700208 if (obj == NULL)
209 return -ENOMEM;
210
Chris Wilson05394f32010-11-08 19:18:58 +0000211 ret = drm_gem_handle_create(file, &obj->base, &handle);
Chris Wilson1dfd9752010-09-06 14:44:14 +0100212 if (ret) {
Chris Wilson05394f32010-11-08 19:18:58 +0000213 drm_gem_object_release(&obj->base);
214 i915_gem_info_remove_obj(dev->dev_private, obj->base.size);
Chris Wilson202f2fe2010-10-14 13:20:40 +0100215 kfree(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700216 return ret;
Chris Wilson1dfd9752010-09-06 14:44:14 +0100217 }
218
Chris Wilson202f2fe2010-10-14 13:20:40 +0100219 /* drop reference from allocate - handle holds it now */
Chris Wilson05394f32010-11-08 19:18:58 +0000220 drm_gem_object_unreference(&obj->base);
Chris Wilson202f2fe2010-10-14 13:20:40 +0100221 trace_i915_gem_object_create(obj);
222
Eric Anholt673a3942008-07-30 12:06:12 -0700223 args->handle = handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700224 return 0;
225}
226
Chris Wilson05394f32010-11-08 19:18:58 +0000227static int i915_gem_object_needs_bit17_swizzle(struct drm_i915_gem_object *obj)
Eric Anholt280b7132009-03-12 16:56:27 -0700228{
Chris Wilson05394f32010-11-08 19:18:58 +0000229 drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
Eric Anholt280b7132009-03-12 16:56:27 -0700230
231 return dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_9_10_17 &&
Chris Wilson05394f32010-11-08 19:18:58 +0000232 obj->tiling_mode != I915_TILING_NONE;
Eric Anholt280b7132009-03-12 16:56:27 -0700233}
234
Chris Wilson99a03df2010-05-27 14:15:34 +0100235static inline void
Eric Anholt40123c12009-03-09 13:42:30 -0700236slow_shmem_copy(struct page *dst_page,
237 int dst_offset,
238 struct page *src_page,
239 int src_offset,
240 int length)
241{
242 char *dst_vaddr, *src_vaddr;
243
Chris Wilson99a03df2010-05-27 14:15:34 +0100244 dst_vaddr = kmap(dst_page);
245 src_vaddr = kmap(src_page);
Eric Anholt40123c12009-03-09 13:42:30 -0700246
247 memcpy(dst_vaddr + dst_offset, src_vaddr + src_offset, length);
248
Chris Wilson99a03df2010-05-27 14:15:34 +0100249 kunmap(src_page);
250 kunmap(dst_page);
Eric Anholt40123c12009-03-09 13:42:30 -0700251}
252
Chris Wilson99a03df2010-05-27 14:15:34 +0100253static inline void
Eric Anholt280b7132009-03-12 16:56:27 -0700254slow_shmem_bit17_copy(struct page *gpu_page,
255 int gpu_offset,
256 struct page *cpu_page,
257 int cpu_offset,
258 int length,
259 int is_read)
260{
261 char *gpu_vaddr, *cpu_vaddr;
262
263 /* Use the unswizzled path if this page isn't affected. */
264 if ((page_to_phys(gpu_page) & (1 << 17)) == 0) {
265 if (is_read)
266 return slow_shmem_copy(cpu_page, cpu_offset,
267 gpu_page, gpu_offset, length);
268 else
269 return slow_shmem_copy(gpu_page, gpu_offset,
270 cpu_page, cpu_offset, length);
271 }
272
Chris Wilson99a03df2010-05-27 14:15:34 +0100273 gpu_vaddr = kmap(gpu_page);
274 cpu_vaddr = kmap(cpu_page);
Eric Anholt280b7132009-03-12 16:56:27 -0700275
276 /* Copy the data, XORing A6 with A17 (1). The user already knows he's
277 * XORing with the other bits (A9 for Y, A9 and A10 for X)
278 */
279 while (length > 0) {
280 int cacheline_end = ALIGN(gpu_offset + 1, 64);
281 int this_length = min(cacheline_end - gpu_offset, length);
282 int swizzled_gpu_offset = gpu_offset ^ 64;
283
284 if (is_read) {
285 memcpy(cpu_vaddr + cpu_offset,
286 gpu_vaddr + swizzled_gpu_offset,
287 this_length);
288 } else {
289 memcpy(gpu_vaddr + swizzled_gpu_offset,
290 cpu_vaddr + cpu_offset,
291 this_length);
292 }
293 cpu_offset += this_length;
294 gpu_offset += this_length;
295 length -= this_length;
296 }
297
Chris Wilson99a03df2010-05-27 14:15:34 +0100298 kunmap(cpu_page);
299 kunmap(gpu_page);
Eric Anholt280b7132009-03-12 16:56:27 -0700300}
301
Eric Anholt673a3942008-07-30 12:06:12 -0700302/**
Eric Anholteb014592009-03-10 11:44:52 -0700303 * This is the fast shmem pread path, which attempts to copy_from_user directly
304 * from the backing pages of the object to the user's address space. On a
305 * fault, it fails so we can fall back to i915_gem_shmem_pwrite_slow().
306 */
307static int
Chris Wilson05394f32010-11-08 19:18:58 +0000308i915_gem_shmem_pread_fast(struct drm_device *dev,
309 struct drm_i915_gem_object *obj,
Eric Anholteb014592009-03-10 11:44:52 -0700310 struct drm_i915_gem_pread *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000311 struct drm_file *file)
Eric Anholteb014592009-03-10 11:44:52 -0700312{
Chris Wilson05394f32010-11-08 19:18:58 +0000313 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Eric Anholteb014592009-03-10 11:44:52 -0700314 ssize_t remain;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100315 loff_t offset;
Eric Anholteb014592009-03-10 11:44:52 -0700316 char __user *user_data;
317 int page_offset, page_length;
Eric Anholteb014592009-03-10 11:44:52 -0700318
319 user_data = (char __user *) (uintptr_t) args->data_ptr;
320 remain = args->size;
321
Eric Anholteb014592009-03-10 11:44:52 -0700322 offset = args->offset;
323
324 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100325 struct page *page;
326 char *vaddr;
327 int ret;
328
Eric Anholteb014592009-03-10 11:44:52 -0700329 /* Operation in this page
330 *
Eric Anholteb014592009-03-10 11:44:52 -0700331 * page_offset = offset within page
332 * page_length = bytes to copy for this page
333 */
Eric Anholteb014592009-03-10 11:44:52 -0700334 page_offset = offset & (PAGE_SIZE-1);
335 page_length = remain;
336 if ((page_offset + remain) > PAGE_SIZE)
337 page_length = PAGE_SIZE - page_offset;
338
Chris Wilsone5281cc2010-10-28 13:45:36 +0100339 page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
340 GFP_HIGHUSER | __GFP_RECLAIMABLE);
341 if (IS_ERR(page))
342 return PTR_ERR(page);
343
344 vaddr = kmap_atomic(page);
345 ret = __copy_to_user_inatomic(user_data,
346 vaddr + page_offset,
347 page_length);
348 kunmap_atomic(vaddr);
349
350 mark_page_accessed(page);
351 page_cache_release(page);
352 if (ret)
Chris Wilson4f27b752010-10-14 15:26:45 +0100353 return -EFAULT;
Eric Anholteb014592009-03-10 11:44:52 -0700354
355 remain -= page_length;
356 user_data += page_length;
357 offset += page_length;
358 }
359
Chris Wilson4f27b752010-10-14 15:26:45 +0100360 return 0;
Eric Anholteb014592009-03-10 11:44:52 -0700361}
362
363/**
364 * This is the fallback shmem pread path, which allocates temporary storage
365 * in kernel space to copy_to_user into outside of the struct_mutex, so we
366 * can copy out of the object's backing pages while holding the struct mutex
367 * and not take page faults.
368 */
369static int
Chris Wilson05394f32010-11-08 19:18:58 +0000370i915_gem_shmem_pread_slow(struct drm_device *dev,
371 struct drm_i915_gem_object *obj,
Eric Anholteb014592009-03-10 11:44:52 -0700372 struct drm_i915_gem_pread *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000373 struct drm_file *file)
Eric Anholteb014592009-03-10 11:44:52 -0700374{
Chris Wilson05394f32010-11-08 19:18:58 +0000375 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Eric Anholteb014592009-03-10 11:44:52 -0700376 struct mm_struct *mm = current->mm;
377 struct page **user_pages;
378 ssize_t remain;
379 loff_t offset, pinned_pages, i;
380 loff_t first_data_page, last_data_page, num_pages;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100381 int shmem_page_offset;
382 int data_page_index, data_page_offset;
Eric Anholteb014592009-03-10 11:44:52 -0700383 int page_length;
384 int ret;
385 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700386 int do_bit17_swizzling;
Eric Anholteb014592009-03-10 11:44:52 -0700387
388 remain = args->size;
389
390 /* Pin the user pages containing the data. We can't fault while
391 * holding the struct mutex, yet we want to hold it while
392 * dereferencing the user data.
393 */
394 first_data_page = data_ptr / PAGE_SIZE;
395 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
396 num_pages = last_data_page - first_data_page + 1;
397
Chris Wilson4f27b752010-10-14 15:26:45 +0100398 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
Eric Anholteb014592009-03-10 11:44:52 -0700399 if (user_pages == NULL)
400 return -ENOMEM;
401
Chris Wilson4f27b752010-10-14 15:26:45 +0100402 mutex_unlock(&dev->struct_mutex);
Eric Anholteb014592009-03-10 11:44:52 -0700403 down_read(&mm->mmap_sem);
404 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
Eric Anholte5e9ecd2009-04-07 16:01:22 -0700405 num_pages, 1, 0, user_pages, NULL);
Eric Anholteb014592009-03-10 11:44:52 -0700406 up_read(&mm->mmap_sem);
Chris Wilson4f27b752010-10-14 15:26:45 +0100407 mutex_lock(&dev->struct_mutex);
Eric Anholteb014592009-03-10 11:44:52 -0700408 if (pinned_pages < num_pages) {
409 ret = -EFAULT;
Chris Wilson4f27b752010-10-14 15:26:45 +0100410 goto out;
Eric Anholteb014592009-03-10 11:44:52 -0700411 }
412
Chris Wilson4f27b752010-10-14 15:26:45 +0100413 ret = i915_gem_object_set_cpu_read_domain_range(obj,
414 args->offset,
Eric Anholteb014592009-03-10 11:44:52 -0700415 args->size);
Chris Wilson4f27b752010-10-14 15:26:45 +0100416 if (ret)
417 goto out;
418
419 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700420
Eric Anholteb014592009-03-10 11:44:52 -0700421 offset = args->offset;
422
423 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100424 struct page *page;
425
Eric Anholteb014592009-03-10 11:44:52 -0700426 /* Operation in this page
427 *
Eric Anholteb014592009-03-10 11:44:52 -0700428 * shmem_page_offset = offset within page in shmem file
429 * data_page_index = page number in get_user_pages return
430 * data_page_offset = offset with data_page_index page.
431 * page_length = bytes to copy for this page
432 */
Eric Anholteb014592009-03-10 11:44:52 -0700433 shmem_page_offset = offset & ~PAGE_MASK;
434 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
435 data_page_offset = data_ptr & ~PAGE_MASK;
436
437 page_length = remain;
438 if ((shmem_page_offset + page_length) > PAGE_SIZE)
439 page_length = PAGE_SIZE - shmem_page_offset;
440 if ((data_page_offset + page_length) > PAGE_SIZE)
441 page_length = PAGE_SIZE - data_page_offset;
442
Chris Wilsone5281cc2010-10-28 13:45:36 +0100443 page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
444 GFP_HIGHUSER | __GFP_RECLAIMABLE);
445 if (IS_ERR(page))
446 return PTR_ERR(page);
447
Eric Anholt280b7132009-03-12 16:56:27 -0700448 if (do_bit17_swizzling) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100449 slow_shmem_bit17_copy(page,
Eric Anholt280b7132009-03-12 16:56:27 -0700450 shmem_page_offset,
Chris Wilson99a03df2010-05-27 14:15:34 +0100451 user_pages[data_page_index],
452 data_page_offset,
453 page_length,
454 1);
455 } else {
456 slow_shmem_copy(user_pages[data_page_index],
457 data_page_offset,
Chris Wilsone5281cc2010-10-28 13:45:36 +0100458 page,
Chris Wilson99a03df2010-05-27 14:15:34 +0100459 shmem_page_offset,
460 page_length);
Eric Anholt280b7132009-03-12 16:56:27 -0700461 }
Eric Anholteb014592009-03-10 11:44:52 -0700462
Chris Wilsone5281cc2010-10-28 13:45:36 +0100463 mark_page_accessed(page);
464 page_cache_release(page);
465
Eric Anholteb014592009-03-10 11:44:52 -0700466 remain -= page_length;
467 data_ptr += page_length;
468 offset += page_length;
469 }
470
Chris Wilson4f27b752010-10-14 15:26:45 +0100471out:
Eric Anholteb014592009-03-10 11:44:52 -0700472 for (i = 0; i < pinned_pages; i++) {
473 SetPageDirty(user_pages[i]);
Chris Wilsone5281cc2010-10-28 13:45:36 +0100474 mark_page_accessed(user_pages[i]);
Eric Anholteb014592009-03-10 11:44:52 -0700475 page_cache_release(user_pages[i]);
476 }
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700477 drm_free_large(user_pages);
Eric Anholteb014592009-03-10 11:44:52 -0700478
479 return ret;
480}
481
Eric Anholt673a3942008-07-30 12:06:12 -0700482/**
483 * Reads data from the object referenced by handle.
484 *
485 * On error, the contents of *data are undefined.
486 */
487int
488i915_gem_pread_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000489 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700490{
491 struct drm_i915_gem_pread *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +0000492 struct drm_i915_gem_object *obj;
Chris Wilson35b62a82010-09-26 20:23:38 +0100493 int ret = 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700494
Chris Wilson51311d02010-11-17 09:10:42 +0000495 if (args->size == 0)
496 return 0;
497
498 if (!access_ok(VERIFY_WRITE,
499 (char __user *)(uintptr_t)args->data_ptr,
500 args->size))
501 return -EFAULT;
502
503 ret = fault_in_pages_writeable((char __user *)(uintptr_t)args->data_ptr,
504 args->size);
505 if (ret)
506 return -EFAULT;
507
Chris Wilson4f27b752010-10-14 15:26:45 +0100508 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100509 if (ret)
Chris Wilson4f27b752010-10-14 15:26:45 +0100510 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700511
Chris Wilson05394f32010-11-08 19:18:58 +0000512 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100513 if (obj == NULL) {
514 ret = -ENOENT;
515 goto unlock;
Chris Wilson4f27b752010-10-14 15:26:45 +0100516 }
Eric Anholt673a3942008-07-30 12:06:12 -0700517
Chris Wilson7dcd2492010-09-26 20:21:44 +0100518 /* Bounds check source. */
Chris Wilson05394f32010-11-08 19:18:58 +0000519 if (args->offset > obj->base.size ||
520 args->size > obj->base.size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +0100521 ret = -EINVAL;
Chris Wilson35b62a82010-09-26 20:23:38 +0100522 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +0100523 }
524
Chris Wilson4f27b752010-10-14 15:26:45 +0100525 ret = i915_gem_object_set_cpu_read_domain_range(obj,
526 args->offset,
527 args->size);
528 if (ret)
Chris Wilsone5281cc2010-10-28 13:45:36 +0100529 goto out;
Chris Wilson4f27b752010-10-14 15:26:45 +0100530
531 ret = -EFAULT;
532 if (!i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilson05394f32010-11-08 19:18:58 +0000533 ret = i915_gem_shmem_pread_fast(dev, obj, args, file);
Chris Wilson4f27b752010-10-14 15:26:45 +0100534 if (ret == -EFAULT)
Chris Wilson05394f32010-11-08 19:18:58 +0000535 ret = i915_gem_shmem_pread_slow(dev, obj, args, file);
Eric Anholt673a3942008-07-30 12:06:12 -0700536
Chris Wilson35b62a82010-09-26 20:23:38 +0100537out:
Chris Wilson05394f32010-11-08 19:18:58 +0000538 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100539unlock:
Chris Wilson4f27b752010-10-14 15:26:45 +0100540 mutex_unlock(&dev->struct_mutex);
Eric Anholteb014592009-03-10 11:44:52 -0700541 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700542}
543
Keith Packard0839ccb2008-10-30 19:38:48 -0700544/* This is the fast write path which cannot handle
545 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700546 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700547
Keith Packard0839ccb2008-10-30 19:38:48 -0700548static inline int
549fast_user_write(struct io_mapping *mapping,
550 loff_t page_base, int page_offset,
551 char __user *user_data,
552 int length)
553{
554 char *vaddr_atomic;
555 unsigned long unwritten;
556
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700557 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
Keith Packard0839ccb2008-10-30 19:38:48 -0700558 unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
559 user_data, length);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700560 io_mapping_unmap_atomic(vaddr_atomic);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100561 return unwritten;
Keith Packard0839ccb2008-10-30 19:38:48 -0700562}
563
564/* Here's the write path which can sleep for
565 * page faults
566 */
567
Chris Wilsonab34c222010-05-27 14:15:35 +0100568static inline void
Eric Anholt3de09aa2009-03-09 09:42:23 -0700569slow_kernel_write(struct io_mapping *mapping,
570 loff_t gtt_base, int gtt_offset,
571 struct page *user_page, int user_offset,
572 int length)
Keith Packard0839ccb2008-10-30 19:38:48 -0700573{
Chris Wilsonab34c222010-05-27 14:15:35 +0100574 char __iomem *dst_vaddr;
575 char *src_vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -0700576
Chris Wilsonab34c222010-05-27 14:15:35 +0100577 dst_vaddr = io_mapping_map_wc(mapping, gtt_base);
578 src_vaddr = kmap(user_page);
579
580 memcpy_toio(dst_vaddr + gtt_offset,
581 src_vaddr + user_offset,
582 length);
583
584 kunmap(user_page);
585 io_mapping_unmap(dst_vaddr);
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700586}
587
Eric Anholt3de09aa2009-03-09 09:42:23 -0700588/**
589 * This is the fast pwrite path, where we copy the data directly from the
590 * user into the GTT, uncached.
591 */
Eric Anholt673a3942008-07-30 12:06:12 -0700592static int
Chris Wilson05394f32010-11-08 19:18:58 +0000593i915_gem_gtt_pwrite_fast(struct drm_device *dev,
594 struct drm_i915_gem_object *obj,
Eric Anholt3de09aa2009-03-09 09:42:23 -0700595 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000596 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700597{
Keith Packard0839ccb2008-10-30 19:38:48 -0700598 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700599 ssize_t remain;
Keith Packard0839ccb2008-10-30 19:38:48 -0700600 loff_t offset, page_base;
Eric Anholt673a3942008-07-30 12:06:12 -0700601 char __user *user_data;
Keith Packard0839ccb2008-10-30 19:38:48 -0700602 int page_offset, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700603
604 user_data = (char __user *) (uintptr_t) args->data_ptr;
605 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700606
Chris Wilson05394f32010-11-08 19:18:58 +0000607 offset = obj->gtt_offset + args->offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700608
609 while (remain > 0) {
610 /* Operation in this page
611 *
Keith Packard0839ccb2008-10-30 19:38:48 -0700612 * page_base = page offset within aperture
613 * page_offset = offset within page
614 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -0700615 */
Keith Packard0839ccb2008-10-30 19:38:48 -0700616 page_base = (offset & ~(PAGE_SIZE-1));
617 page_offset = offset & (PAGE_SIZE-1);
618 page_length = remain;
619 if ((page_offset + remain) > PAGE_SIZE)
620 page_length = PAGE_SIZE - page_offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700621
Keith Packard0839ccb2008-10-30 19:38:48 -0700622 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -0700623 * source page isn't available. Return the error and we'll
624 * retry in the slow path.
Keith Packard0839ccb2008-10-30 19:38:48 -0700625 */
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100626 if (fast_user_write(dev_priv->mm.gtt_mapping, page_base,
627 page_offset, user_data, page_length))
628
629 return -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -0700630
Keith Packard0839ccb2008-10-30 19:38:48 -0700631 remain -= page_length;
632 user_data += page_length;
633 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700634 }
Eric Anholt673a3942008-07-30 12:06:12 -0700635
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100636 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700637}
638
Eric Anholt3de09aa2009-03-09 09:42:23 -0700639/**
640 * This is the fallback GTT pwrite path, which uses get_user_pages to pin
641 * the memory and maps it using kmap_atomic for copying.
642 *
643 * This code resulted in x11perf -rgb10text consuming about 10% more CPU
644 * than using i915_gem_gtt_pwrite_fast on a G45 (32-bit).
645 */
Eric Anholt3043c602008-10-02 12:24:47 -0700646static int
Chris Wilson05394f32010-11-08 19:18:58 +0000647i915_gem_gtt_pwrite_slow(struct drm_device *dev,
648 struct drm_i915_gem_object *obj,
Eric Anholt3de09aa2009-03-09 09:42:23 -0700649 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000650 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700651{
Eric Anholt3de09aa2009-03-09 09:42:23 -0700652 drm_i915_private_t *dev_priv = dev->dev_private;
653 ssize_t remain;
654 loff_t gtt_page_base, offset;
655 loff_t first_data_page, last_data_page, num_pages;
656 loff_t pinned_pages, i;
657 struct page **user_pages;
658 struct mm_struct *mm = current->mm;
659 int gtt_page_offset, data_page_offset, data_page_index, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700660 int ret;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700661 uint64_t data_ptr = args->data_ptr;
662
663 remain = args->size;
664
665 /* Pin the user pages containing the data. We can't fault while
666 * holding the struct mutex, and all of the pwrite implementations
667 * want to hold it while dereferencing the user data.
668 */
669 first_data_page = data_ptr / PAGE_SIZE;
670 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
671 num_pages = last_data_page - first_data_page + 1;
672
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100673 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
Eric Anholt3de09aa2009-03-09 09:42:23 -0700674 if (user_pages == NULL)
675 return -ENOMEM;
676
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100677 mutex_unlock(&dev->struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700678 down_read(&mm->mmap_sem);
679 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
680 num_pages, 0, 0, user_pages, NULL);
681 up_read(&mm->mmap_sem);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100682 mutex_lock(&dev->struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700683 if (pinned_pages < num_pages) {
684 ret = -EFAULT;
685 goto out_unpin_pages;
686 }
687
Chris Wilsond9e86c02010-11-10 16:40:20 +0000688 ret = i915_gem_object_set_to_gtt_domain(obj, true);
689 if (ret)
690 goto out_unpin_pages;
691
692 ret = i915_gem_object_put_fence(obj);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700693 if (ret)
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100694 goto out_unpin_pages;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700695
Chris Wilson05394f32010-11-08 19:18:58 +0000696 offset = obj->gtt_offset + args->offset;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700697
698 while (remain > 0) {
699 /* Operation in this page
700 *
701 * gtt_page_base = page offset within aperture
702 * gtt_page_offset = offset within page in aperture
703 * data_page_index = page number in get_user_pages return
704 * data_page_offset = offset with data_page_index page.
705 * page_length = bytes to copy for this page
706 */
707 gtt_page_base = offset & PAGE_MASK;
708 gtt_page_offset = offset & ~PAGE_MASK;
709 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
710 data_page_offset = data_ptr & ~PAGE_MASK;
711
712 page_length = remain;
713 if ((gtt_page_offset + page_length) > PAGE_SIZE)
714 page_length = PAGE_SIZE - gtt_page_offset;
715 if ((data_page_offset + page_length) > PAGE_SIZE)
716 page_length = PAGE_SIZE - data_page_offset;
717
Chris Wilsonab34c222010-05-27 14:15:35 +0100718 slow_kernel_write(dev_priv->mm.gtt_mapping,
719 gtt_page_base, gtt_page_offset,
720 user_pages[data_page_index],
721 data_page_offset,
722 page_length);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700723
724 remain -= page_length;
725 offset += page_length;
726 data_ptr += page_length;
727 }
728
Eric Anholt3de09aa2009-03-09 09:42:23 -0700729out_unpin_pages:
730 for (i = 0; i < pinned_pages; i++)
731 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700732 drm_free_large(user_pages);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700733
734 return ret;
735}
736
Eric Anholt40123c12009-03-09 13:42:30 -0700737/**
738 * This is the fast shmem pwrite path, which attempts to directly
739 * copy_from_user into the kmapped pages backing the object.
740 */
Eric Anholt673a3942008-07-30 12:06:12 -0700741static int
Chris Wilson05394f32010-11-08 19:18:58 +0000742i915_gem_shmem_pwrite_fast(struct drm_device *dev,
743 struct drm_i915_gem_object *obj,
Eric Anholt40123c12009-03-09 13:42:30 -0700744 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000745 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700746{
Chris Wilson05394f32010-11-08 19:18:58 +0000747 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Eric Anholt40123c12009-03-09 13:42:30 -0700748 ssize_t remain;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100749 loff_t offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700750 char __user *user_data;
751 int page_offset, page_length;
Eric Anholt40123c12009-03-09 13:42:30 -0700752
753 user_data = (char __user *) (uintptr_t) args->data_ptr;
754 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700755
Eric Anholt673a3942008-07-30 12:06:12 -0700756 offset = args->offset;
Chris Wilson05394f32010-11-08 19:18:58 +0000757 obj->dirty = 1;
Eric Anholt673a3942008-07-30 12:06:12 -0700758
Eric Anholt40123c12009-03-09 13:42:30 -0700759 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100760 struct page *page;
761 char *vaddr;
762 int ret;
763
Eric Anholt40123c12009-03-09 13:42:30 -0700764 /* Operation in this page
765 *
Eric Anholt40123c12009-03-09 13:42:30 -0700766 * page_offset = offset within page
767 * page_length = bytes to copy for this page
768 */
Eric Anholt40123c12009-03-09 13:42:30 -0700769 page_offset = offset & (PAGE_SIZE-1);
770 page_length = remain;
771 if ((page_offset + remain) > PAGE_SIZE)
772 page_length = PAGE_SIZE - page_offset;
773
Chris Wilsone5281cc2010-10-28 13:45:36 +0100774 page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
775 GFP_HIGHUSER | __GFP_RECLAIMABLE);
776 if (IS_ERR(page))
777 return PTR_ERR(page);
778
779 vaddr = kmap_atomic(page, KM_USER0);
780 ret = __copy_from_user_inatomic(vaddr + page_offset,
781 user_data,
782 page_length);
783 kunmap_atomic(vaddr, KM_USER0);
784
785 set_page_dirty(page);
786 mark_page_accessed(page);
787 page_cache_release(page);
788
789 /* If we get a fault while copying data, then (presumably) our
790 * source page isn't available. Return the error and we'll
791 * retry in the slow path.
792 */
793 if (ret)
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100794 return -EFAULT;
Eric Anholt40123c12009-03-09 13:42:30 -0700795
796 remain -= page_length;
797 user_data += page_length;
798 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700799 }
800
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100801 return 0;
Eric Anholt40123c12009-03-09 13:42:30 -0700802}
803
804/**
805 * This is the fallback shmem pwrite path, which uses get_user_pages to pin
806 * the memory and maps it using kmap_atomic for copying.
807 *
808 * This avoids taking mmap_sem for faulting on the user's address while the
809 * struct_mutex is held.
810 */
811static int
Chris Wilson05394f32010-11-08 19:18:58 +0000812i915_gem_shmem_pwrite_slow(struct drm_device *dev,
813 struct drm_i915_gem_object *obj,
Eric Anholt40123c12009-03-09 13:42:30 -0700814 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000815 struct drm_file *file)
Eric Anholt40123c12009-03-09 13:42:30 -0700816{
Chris Wilson05394f32010-11-08 19:18:58 +0000817 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Eric Anholt40123c12009-03-09 13:42:30 -0700818 struct mm_struct *mm = current->mm;
819 struct page **user_pages;
820 ssize_t remain;
821 loff_t offset, pinned_pages, i;
822 loff_t first_data_page, last_data_page, num_pages;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100823 int shmem_page_offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700824 int data_page_index, data_page_offset;
825 int page_length;
826 int ret;
827 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700828 int do_bit17_swizzling;
Eric Anholt40123c12009-03-09 13:42:30 -0700829
830 remain = args->size;
831
832 /* Pin the user pages containing the data. We can't fault while
833 * holding the struct mutex, and all of the pwrite implementations
834 * want to hold it while dereferencing the user data.
835 */
836 first_data_page = data_ptr / PAGE_SIZE;
837 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
838 num_pages = last_data_page - first_data_page + 1;
839
Chris Wilson4f27b752010-10-14 15:26:45 +0100840 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
Eric Anholt40123c12009-03-09 13:42:30 -0700841 if (user_pages == NULL)
842 return -ENOMEM;
843
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100844 mutex_unlock(&dev->struct_mutex);
Eric Anholt40123c12009-03-09 13:42:30 -0700845 down_read(&mm->mmap_sem);
846 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
847 num_pages, 0, 0, user_pages, NULL);
848 up_read(&mm->mmap_sem);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100849 mutex_lock(&dev->struct_mutex);
Eric Anholt40123c12009-03-09 13:42:30 -0700850 if (pinned_pages < num_pages) {
851 ret = -EFAULT;
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100852 goto out;
Eric Anholt40123c12009-03-09 13:42:30 -0700853 }
854
Eric Anholt40123c12009-03-09 13:42:30 -0700855 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100856 if (ret)
857 goto out;
858
859 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700860
Eric Anholt40123c12009-03-09 13:42:30 -0700861 offset = args->offset;
Chris Wilson05394f32010-11-08 19:18:58 +0000862 obj->dirty = 1;
Eric Anholt40123c12009-03-09 13:42:30 -0700863
864 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100865 struct page *page;
866
Eric Anholt40123c12009-03-09 13:42:30 -0700867 /* Operation in this page
868 *
Eric Anholt40123c12009-03-09 13:42:30 -0700869 * shmem_page_offset = offset within page in shmem file
870 * data_page_index = page number in get_user_pages return
871 * data_page_offset = offset with data_page_index page.
872 * page_length = bytes to copy for this page
873 */
Eric Anholt40123c12009-03-09 13:42:30 -0700874 shmem_page_offset = offset & ~PAGE_MASK;
875 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
876 data_page_offset = data_ptr & ~PAGE_MASK;
877
878 page_length = remain;
879 if ((shmem_page_offset + page_length) > PAGE_SIZE)
880 page_length = PAGE_SIZE - shmem_page_offset;
881 if ((data_page_offset + page_length) > PAGE_SIZE)
882 page_length = PAGE_SIZE - data_page_offset;
883
Chris Wilsone5281cc2010-10-28 13:45:36 +0100884 page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
885 GFP_HIGHUSER | __GFP_RECLAIMABLE);
886 if (IS_ERR(page)) {
887 ret = PTR_ERR(page);
888 goto out;
889 }
890
Eric Anholt280b7132009-03-12 16:56:27 -0700891 if (do_bit17_swizzling) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100892 slow_shmem_bit17_copy(page,
Eric Anholt280b7132009-03-12 16:56:27 -0700893 shmem_page_offset,
894 user_pages[data_page_index],
895 data_page_offset,
Chris Wilson99a03df2010-05-27 14:15:34 +0100896 page_length,
897 0);
898 } else {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100899 slow_shmem_copy(page,
Chris Wilson99a03df2010-05-27 14:15:34 +0100900 shmem_page_offset,
901 user_pages[data_page_index],
902 data_page_offset,
903 page_length);
Eric Anholt280b7132009-03-12 16:56:27 -0700904 }
Eric Anholt40123c12009-03-09 13:42:30 -0700905
Chris Wilsone5281cc2010-10-28 13:45:36 +0100906 set_page_dirty(page);
907 mark_page_accessed(page);
908 page_cache_release(page);
909
Eric Anholt40123c12009-03-09 13:42:30 -0700910 remain -= page_length;
911 data_ptr += page_length;
912 offset += page_length;
913 }
914
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100915out:
Eric Anholt40123c12009-03-09 13:42:30 -0700916 for (i = 0; i < pinned_pages; i++)
917 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700918 drm_free_large(user_pages);
Eric Anholt40123c12009-03-09 13:42:30 -0700919
920 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700921}
922
923/**
924 * Writes data to the object referenced by handle.
925 *
926 * On error, the contents of the buffer that were to be modified are undefined.
927 */
928int
929i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100930 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700931{
932 struct drm_i915_gem_pwrite *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +0000933 struct drm_i915_gem_object *obj;
Chris Wilson51311d02010-11-17 09:10:42 +0000934 int ret;
935
936 if (args->size == 0)
937 return 0;
938
939 if (!access_ok(VERIFY_READ,
940 (char __user *)(uintptr_t)args->data_ptr,
941 args->size))
942 return -EFAULT;
943
944 ret = fault_in_pages_readable((char __user *)(uintptr_t)args->data_ptr,
945 args->size);
946 if (ret)
947 return -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -0700948
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100949 ret = i915_mutex_lock_interruptible(dev);
950 if (ret)
951 return ret;
952
Chris Wilson05394f32010-11-08 19:18:58 +0000953 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100954 if (obj == NULL) {
955 ret = -ENOENT;
956 goto unlock;
957 }
Eric Anholt673a3942008-07-30 12:06:12 -0700958
Chris Wilson7dcd2492010-09-26 20:21:44 +0100959 /* Bounds check destination. */
Chris Wilson05394f32010-11-08 19:18:58 +0000960 if (args->offset > obj->base.size ||
961 args->size > obj->base.size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +0100962 ret = -EINVAL;
Chris Wilson35b62a82010-09-26 20:23:38 +0100963 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +0100964 }
965
Eric Anholt673a3942008-07-30 12:06:12 -0700966 /* We can only do the GTT pwrite on untiled buffers, as otherwise
967 * it would end up going through the fenced access, and we'll get
968 * different detiling behavior between reading and writing.
969 * pread/pwrite currently are reading and writing from the CPU
970 * perspective, requiring manual detiling by the client.
971 */
Chris Wilson05394f32010-11-08 19:18:58 +0000972 if (obj->phys_obj)
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100973 ret = i915_gem_phys_pwrite(dev, obj, args, file);
Chris Wilsond9e86c02010-11-10 16:40:20 +0000974 else if (obj->gtt_space &&
Chris Wilson05394f32010-11-08 19:18:58 +0000975 obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
Daniel Vetter75e9e912010-11-04 17:11:09 +0100976 ret = i915_gem_object_pin(obj, 0, true);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100977 if (ret)
978 goto out;
979
Chris Wilsond9e86c02010-11-10 16:40:20 +0000980 ret = i915_gem_object_set_to_gtt_domain(obj, true);
981 if (ret)
982 goto out_unpin;
983
984 ret = i915_gem_object_put_fence(obj);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100985 if (ret)
986 goto out_unpin;
987
988 ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file);
989 if (ret == -EFAULT)
990 ret = i915_gem_gtt_pwrite_slow(dev, obj, args, file);
991
992out_unpin:
993 i915_gem_object_unpin(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700994 } else {
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100995 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
996 if (ret)
Chris Wilsone5281cc2010-10-28 13:45:36 +0100997 goto out;
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100998
999 ret = -EFAULT;
1000 if (!i915_gem_object_needs_bit17_swizzle(obj))
1001 ret = i915_gem_shmem_pwrite_fast(dev, obj, args, file);
1002 if (ret == -EFAULT)
1003 ret = i915_gem_shmem_pwrite_slow(dev, obj, args, file);
Eric Anholt40123c12009-03-09 13:42:30 -07001004 }
Eric Anholt673a3942008-07-30 12:06:12 -07001005
Chris Wilson35b62a82010-09-26 20:23:38 +01001006out:
Chris Wilson05394f32010-11-08 19:18:58 +00001007 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001008unlock:
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001009 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07001010 return ret;
1011}
1012
1013/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001014 * Called when user space prepares to use an object with the CPU, either
1015 * through the mmap ioctl's mapping or a GTT mapping.
Eric Anholt673a3942008-07-30 12:06:12 -07001016 */
1017int
1018i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001019 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001020{
1021 struct drm_i915_gem_set_domain *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001022 struct drm_i915_gem_object *obj;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001023 uint32_t read_domains = args->read_domains;
1024 uint32_t write_domain = args->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07001025 int ret;
1026
1027 if (!(dev->driver->driver_features & DRIVER_GEM))
1028 return -ENODEV;
1029
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001030 /* Only handle setting domains to types used by the CPU. */
Chris Wilson21d509e2009-06-06 09:46:02 +01001031 if (write_domain & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001032 return -EINVAL;
1033
Chris Wilson21d509e2009-06-06 09:46:02 +01001034 if (read_domains & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001035 return -EINVAL;
1036
1037 /* Having something in the write domain implies it's in the read
1038 * domain, and only that read domain. Enforce that in the request.
1039 */
1040 if (write_domain != 0 && read_domains != write_domain)
1041 return -EINVAL;
1042
Chris Wilson76c1dec2010-09-25 11:22:51 +01001043 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001044 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001045 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001046
Chris Wilson05394f32010-11-08 19:18:58 +00001047 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001048 if (obj == NULL) {
1049 ret = -ENOENT;
1050 goto unlock;
Chris Wilson76c1dec2010-09-25 11:22:51 +01001051 }
Jesse Barnes652c3932009-08-17 13:31:43 -07001052
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001053 if (read_domains & I915_GEM_DOMAIN_GTT) {
1054 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
Eric Anholt02354392008-11-26 13:58:13 -08001055
1056 /* Silently promote "you're not bound, there was nothing to do"
1057 * to success, since the client was just asking us to
1058 * make sure everything was done.
1059 */
1060 if (ret == -EINVAL)
1061 ret = 0;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001062 } else {
Eric Anholte47c68e2008-11-14 13:35:19 -08001063 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001064 }
1065
Chris Wilson05394f32010-11-08 19:18:58 +00001066 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001067unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07001068 mutex_unlock(&dev->struct_mutex);
1069 return ret;
1070}
1071
1072/**
1073 * Called when user space has done writes to this buffer
1074 */
1075int
1076i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001077 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001078{
1079 struct drm_i915_gem_sw_finish *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001080 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001081 int ret = 0;
1082
1083 if (!(dev->driver->driver_features & DRIVER_GEM))
1084 return -ENODEV;
1085
Chris Wilson76c1dec2010-09-25 11:22:51 +01001086 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001087 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001088 return ret;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001089
Chris Wilson05394f32010-11-08 19:18:58 +00001090 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Eric Anholt673a3942008-07-30 12:06:12 -07001091 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001092 ret = -ENOENT;
1093 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07001094 }
1095
Eric Anholt673a3942008-07-30 12:06:12 -07001096 /* Pinned buffers may be scanout, so flush the cache */
Chris Wilson05394f32010-11-08 19:18:58 +00001097 if (obj->pin_count)
Eric Anholte47c68e2008-11-14 13:35:19 -08001098 i915_gem_object_flush_cpu_write_domain(obj);
1099
Chris Wilson05394f32010-11-08 19:18:58 +00001100 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001101unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07001102 mutex_unlock(&dev->struct_mutex);
1103 return ret;
1104}
1105
1106/**
1107 * Maps the contents of an object, returning the address it is mapped
1108 * into.
1109 *
1110 * While the mapping holds a reference on the contents of the object, it doesn't
1111 * imply a ref on the object itself.
1112 */
1113int
1114i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001115 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001116{
Chris Wilsonda761a62010-10-27 17:37:08 +01001117 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07001118 struct drm_i915_gem_mmap *args = data;
1119 struct drm_gem_object *obj;
1120 loff_t offset;
1121 unsigned long addr;
1122
1123 if (!(dev->driver->driver_features & DRIVER_GEM))
1124 return -ENODEV;
1125
Chris Wilson05394f32010-11-08 19:18:58 +00001126 obj = drm_gem_object_lookup(dev, file, args->handle);
Eric Anholt673a3942008-07-30 12:06:12 -07001127 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001128 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001129
Chris Wilsonda761a62010-10-27 17:37:08 +01001130 if (obj->size > dev_priv->mm.gtt_mappable_end) {
1131 drm_gem_object_unreference_unlocked(obj);
1132 return -E2BIG;
1133 }
1134
Eric Anholt673a3942008-07-30 12:06:12 -07001135 offset = args->offset;
1136
1137 down_write(&current->mm->mmap_sem);
1138 addr = do_mmap(obj->filp, 0, args->size,
1139 PROT_READ | PROT_WRITE, MAP_SHARED,
1140 args->offset);
1141 up_write(&current->mm->mmap_sem);
Luca Barbieribc9025b2010-02-09 05:49:12 +00001142 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001143 if (IS_ERR((void *)addr))
1144 return addr;
1145
1146 args->addr_ptr = (uint64_t) addr;
1147
1148 return 0;
1149}
1150
Jesse Barnesde151cf2008-11-12 10:03:55 -08001151/**
1152 * i915_gem_fault - fault a page into the GTT
1153 * vma: VMA in question
1154 * vmf: fault info
1155 *
1156 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1157 * from userspace. The fault handler takes care of binding the object to
1158 * the GTT (if needed), allocating and programming a fence register (again,
1159 * only if needed based on whether the old reg is still valid or the object
1160 * is tiled) and inserting a new PTE into the faulting process.
1161 *
1162 * Note that the faulting process may involve evicting existing objects
1163 * from the GTT and/or fence registers to make room. So performance may
1164 * suffer if the GTT working set is large or there are few fence registers
1165 * left.
1166 */
1167int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1168{
Chris Wilson05394f32010-11-08 19:18:58 +00001169 struct drm_i915_gem_object *obj = to_intel_bo(vma->vm_private_data);
1170 struct drm_device *dev = obj->base.dev;
Chris Wilson7d1c4802010-08-07 21:45:03 +01001171 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001172 pgoff_t page_offset;
1173 unsigned long pfn;
1174 int ret = 0;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001175 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001176
1177 /* We don't use vmf->pgoff since that has the fake offset */
1178 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1179 PAGE_SHIFT;
1180
1181 /* Now bind it into the GTT if needed */
1182 mutex_lock(&dev->struct_mutex);
Chris Wilsona00b10c2010-09-24 21:15:47 +01001183
Chris Wilson919926a2010-11-12 13:42:53 +00001184 if (!obj->map_and_fenceable) {
1185 ret = i915_gem_object_unbind(obj);
1186 if (ret)
1187 goto unlock;
Chris Wilsona00b10c2010-09-24 21:15:47 +01001188 }
Chris Wilson05394f32010-11-08 19:18:58 +00001189 if (!obj->gtt_space) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01001190 ret = i915_gem_object_bind_to_gtt(obj, 0, true);
Chris Wilsonc7150892009-09-23 00:43:56 +01001191 if (ret)
1192 goto unlock;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001193 }
1194
Chris Wilson4a684a42010-10-28 14:44:08 +01001195 ret = i915_gem_object_set_to_gtt_domain(obj, write);
1196 if (ret)
1197 goto unlock;
1198
Chris Wilsond9e86c02010-11-10 16:40:20 +00001199 if (obj->tiling_mode == I915_TILING_NONE)
1200 ret = i915_gem_object_put_fence(obj);
1201 else
1202 ret = i915_gem_object_get_fence(obj, NULL, true);
1203 if (ret)
1204 goto unlock;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001205
Chris Wilson05394f32010-11-08 19:18:58 +00001206 if (i915_gem_object_is_inactive(obj))
1207 list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
Chris Wilson7d1c4802010-08-07 21:45:03 +01001208
Chris Wilson6299f992010-11-24 12:23:44 +00001209 obj->fault_mappable = true;
1210
Chris Wilson05394f32010-11-08 19:18:58 +00001211 pfn = ((dev->agp->base + obj->gtt_offset) >> PAGE_SHIFT) +
Jesse Barnesde151cf2008-11-12 10:03:55 -08001212 page_offset;
1213
1214 /* Finally, remap it using the new GTT offset */
1215 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
Chris Wilsonc7150892009-09-23 00:43:56 +01001216unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001217 mutex_unlock(&dev->struct_mutex);
1218
1219 switch (ret) {
Chris Wilson045e7692010-11-07 09:18:22 +00001220 case -EAGAIN:
1221 set_need_resched();
Chris Wilsonc7150892009-09-23 00:43:56 +01001222 case 0:
1223 case -ERESTARTSYS:
1224 return VM_FAULT_NOPAGE;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001225 case -ENOMEM:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001226 return VM_FAULT_OOM;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001227 default:
Chris Wilsonc7150892009-09-23 00:43:56 +01001228 return VM_FAULT_SIGBUS;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001229 }
1230}
1231
1232/**
1233 * i915_gem_create_mmap_offset - create a fake mmap offset for an object
1234 * @obj: obj in question
1235 *
1236 * GEM memory mapping works by handing back to userspace a fake mmap offset
1237 * it can use in a subsequent mmap(2) call. The DRM core code then looks
1238 * up the object based on the offset and sets up the various memory mapping
1239 * structures.
1240 *
1241 * This routine allocates and attaches a fake offset for @obj.
1242 */
1243static int
Chris Wilson05394f32010-11-08 19:18:58 +00001244i915_gem_create_mmap_offset(struct drm_i915_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001245{
Chris Wilson05394f32010-11-08 19:18:58 +00001246 struct drm_device *dev = obj->base.dev;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001247 struct drm_gem_mm *mm = dev->mm_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001248 struct drm_map_list *list;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +11001249 struct drm_local_map *map;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001250 int ret = 0;
1251
1252 /* Set the object up for mmap'ing */
Chris Wilson05394f32010-11-08 19:18:58 +00001253 list = &obj->base.map_list;
Eric Anholt9a298b22009-03-24 12:23:04 -07001254 list->map = kzalloc(sizeof(struct drm_map_list), GFP_KERNEL);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001255 if (!list->map)
1256 return -ENOMEM;
1257
1258 map = list->map;
1259 map->type = _DRM_GEM;
Chris Wilson05394f32010-11-08 19:18:58 +00001260 map->size = obj->base.size;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001261 map->handle = obj;
1262
1263 /* Get a DRM GEM mmap offset allocated... */
1264 list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
Chris Wilson05394f32010-11-08 19:18:58 +00001265 obj->base.size / PAGE_SIZE,
1266 0, 0);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001267 if (!list->file_offset_node) {
Chris Wilson05394f32010-11-08 19:18:58 +00001268 DRM_ERROR("failed to allocate offset for bo %d\n",
1269 obj->base.name);
Chris Wilson9e0ae5342010-09-21 15:05:24 +01001270 ret = -ENOSPC;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001271 goto out_free_list;
1272 }
1273
1274 list->file_offset_node = drm_mm_get_block(list->file_offset_node,
Chris Wilson05394f32010-11-08 19:18:58 +00001275 obj->base.size / PAGE_SIZE,
1276 0);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001277 if (!list->file_offset_node) {
1278 ret = -ENOMEM;
1279 goto out_free_list;
1280 }
1281
1282 list->hash.key = list->file_offset_node->start;
Chris Wilson9e0ae5342010-09-21 15:05:24 +01001283 ret = drm_ht_insert_item(&mm->offset_hash, &list->hash);
1284 if (ret) {
Jesse Barnesde151cf2008-11-12 10:03:55 -08001285 DRM_ERROR("failed to add to map hash\n");
1286 goto out_free_mm;
1287 }
1288
Jesse Barnesde151cf2008-11-12 10:03:55 -08001289 return 0;
1290
1291out_free_mm:
1292 drm_mm_put_block(list->file_offset_node);
1293out_free_list:
Eric Anholt9a298b22009-03-24 12:23:04 -07001294 kfree(list->map);
Chris Wilson39a01d12010-10-28 13:03:06 +01001295 list->map = NULL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001296
1297 return ret;
1298}
1299
Chris Wilson901782b2009-07-10 08:18:50 +01001300/**
1301 * i915_gem_release_mmap - remove physical page mappings
1302 * @obj: obj in question
1303 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001304 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01001305 * relinquish ownership of the pages back to the system.
1306 *
1307 * It is vital that we remove the page mapping if we have mapped a tiled
1308 * object through the GTT and then lose the fence register due to
1309 * resource pressure. Similarly if the object has been moved out of the
1310 * aperture, than pages mapped into userspace must be revoked. Removing the
1311 * mapping will then trigger a page fault on the next user access, allowing
1312 * fixup by i915_gem_fault().
1313 */
Eric Anholtd05ca302009-07-10 13:02:26 -07001314void
Chris Wilson05394f32010-11-08 19:18:58 +00001315i915_gem_release_mmap(struct drm_i915_gem_object *obj)
Chris Wilson901782b2009-07-10 08:18:50 +01001316{
Chris Wilson6299f992010-11-24 12:23:44 +00001317 if (!obj->fault_mappable)
1318 return;
Chris Wilson901782b2009-07-10 08:18:50 +01001319
Chris Wilson6299f992010-11-24 12:23:44 +00001320 unmap_mapping_range(obj->base.dev->dev_mapping,
1321 (loff_t)obj->base.map_list.hash.key<<PAGE_SHIFT,
1322 obj->base.size, 1);
Daniel Vetterfb7d5162010-10-01 22:05:20 +02001323
Chris Wilson6299f992010-11-24 12:23:44 +00001324 obj->fault_mappable = false;
Chris Wilson901782b2009-07-10 08:18:50 +01001325}
1326
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001327static void
Chris Wilson05394f32010-11-08 19:18:58 +00001328i915_gem_free_mmap_offset(struct drm_i915_gem_object *obj)
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001329{
Chris Wilson05394f32010-11-08 19:18:58 +00001330 struct drm_device *dev = obj->base.dev;
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001331 struct drm_gem_mm *mm = dev->mm_private;
Chris Wilson05394f32010-11-08 19:18:58 +00001332 struct drm_map_list *list = &obj->base.map_list;
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001333
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001334 drm_ht_remove_item(&mm->offset_hash, &list->hash);
Chris Wilson39a01d12010-10-28 13:03:06 +01001335 drm_mm_put_block(list->file_offset_node);
1336 kfree(list->map);
1337 list->map = NULL;
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001338}
1339
Chris Wilson92b88ae2010-11-09 11:47:32 +00001340static uint32_t
1341i915_gem_get_gtt_size(struct drm_i915_gem_object *obj)
1342{
1343 struct drm_device *dev = obj->base.dev;
1344 uint32_t size;
1345
1346 if (INTEL_INFO(dev)->gen >= 4 ||
1347 obj->tiling_mode == I915_TILING_NONE)
1348 return obj->base.size;
1349
1350 /* Previous chips need a power-of-two fence region when tiling */
1351 if (INTEL_INFO(dev)->gen == 3)
1352 size = 1024*1024;
1353 else
1354 size = 512*1024;
1355
1356 while (size < obj->base.size)
1357 size <<= 1;
1358
1359 return size;
1360}
1361
Jesse Barnesde151cf2008-11-12 10:03:55 -08001362/**
1363 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1364 * @obj: object to check
1365 *
1366 * Return the required GTT alignment for an object, taking into account
Daniel Vetter5e783302010-11-14 22:32:36 +01001367 * potential fence register mapping.
Jesse Barnesde151cf2008-11-12 10:03:55 -08001368 */
1369static uint32_t
Chris Wilson05394f32010-11-08 19:18:58 +00001370i915_gem_get_gtt_alignment(struct drm_i915_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001371{
Chris Wilson05394f32010-11-08 19:18:58 +00001372 struct drm_device *dev = obj->base.dev;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001373
1374 /*
1375 * Minimum alignment is 4k (GTT page size), but might be greater
1376 * if a fence register is needed for the object.
1377 */
Chris Wilsona00b10c2010-09-24 21:15:47 +01001378 if (INTEL_INFO(dev)->gen >= 4 ||
Chris Wilson05394f32010-11-08 19:18:58 +00001379 obj->tiling_mode == I915_TILING_NONE)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001380 return 4096;
1381
1382 /*
1383 * Previous chips need to be aligned to the size of the smallest
1384 * fence register that can contain the object.
1385 */
Chris Wilson05394f32010-11-08 19:18:58 +00001386 return i915_gem_get_gtt_size(obj);
Chris Wilsona00b10c2010-09-24 21:15:47 +01001387}
1388
Daniel Vetter5e783302010-11-14 22:32:36 +01001389/**
1390 * i915_gem_get_unfenced_gtt_alignment - return required GTT alignment for an
1391 * unfenced object
1392 * @obj: object to check
1393 *
1394 * Return the required GTT alignment for an object, only taking into account
1395 * unfenced tiled surface requirements.
1396 */
1397static uint32_t
Chris Wilson05394f32010-11-08 19:18:58 +00001398i915_gem_get_unfenced_gtt_alignment(struct drm_i915_gem_object *obj)
Daniel Vetter5e783302010-11-14 22:32:36 +01001399{
Chris Wilson05394f32010-11-08 19:18:58 +00001400 struct drm_device *dev = obj->base.dev;
Daniel Vetter5e783302010-11-14 22:32:36 +01001401 int tile_height;
1402
1403 /*
1404 * Minimum alignment is 4k (GTT page size) for sane hw.
1405 */
1406 if (INTEL_INFO(dev)->gen >= 4 || IS_G33(dev) ||
Chris Wilson05394f32010-11-08 19:18:58 +00001407 obj->tiling_mode == I915_TILING_NONE)
Daniel Vetter5e783302010-11-14 22:32:36 +01001408 return 4096;
1409
1410 /*
1411 * Older chips need unfenced tiled buffers to be aligned to the left
1412 * edge of an even tile row (where tile rows are counted as if the bo is
1413 * placed in a fenced gtt region).
1414 */
1415 if (IS_GEN2(dev) ||
Chris Wilson05394f32010-11-08 19:18:58 +00001416 (obj->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev)))
Daniel Vetter5e783302010-11-14 22:32:36 +01001417 tile_height = 32;
1418 else
1419 tile_height = 8;
1420
Chris Wilson05394f32010-11-08 19:18:58 +00001421 return tile_height * obj->stride * 2;
Daniel Vetter5e783302010-11-14 22:32:36 +01001422}
1423
Jesse Barnesde151cf2008-11-12 10:03:55 -08001424/**
1425 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1426 * @dev: DRM device
1427 * @data: GTT mapping ioctl data
Chris Wilson05394f32010-11-08 19:18:58 +00001428 * @file: GEM object info
Jesse Barnesde151cf2008-11-12 10:03:55 -08001429 *
1430 * Simply returns the fake offset to userspace so it can mmap it.
1431 * The mmap call will end up in drm_gem_mmap(), which will set things
1432 * up so we can get faults in the handler above.
1433 *
1434 * The fault handler will take care of binding the object into the GTT
1435 * (since it may have been evicted to make room for something), allocating
1436 * a fence register, and mapping the appropriate aperture address into
1437 * userspace.
1438 */
1439int
1440i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001441 struct drm_file *file)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001442{
Chris Wilsonda761a62010-10-27 17:37:08 +01001443 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001444 struct drm_i915_gem_mmap_gtt *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001445 struct drm_i915_gem_object *obj;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001446 int ret;
1447
1448 if (!(dev->driver->driver_features & DRIVER_GEM))
1449 return -ENODEV;
1450
Chris Wilson76c1dec2010-09-25 11:22:51 +01001451 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001452 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001453 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001454
Chris Wilson05394f32010-11-08 19:18:58 +00001455 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001456 if (obj == NULL) {
1457 ret = -ENOENT;
1458 goto unlock;
1459 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001460
Chris Wilson05394f32010-11-08 19:18:58 +00001461 if (obj->base.size > dev_priv->mm.gtt_mappable_end) {
Chris Wilsonda761a62010-10-27 17:37:08 +01001462 ret = -E2BIG;
1463 goto unlock;
1464 }
1465
Chris Wilson05394f32010-11-08 19:18:58 +00001466 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilsonab182822009-09-22 18:46:17 +01001467 DRM_ERROR("Attempting to mmap a purgeable buffer\n");
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001468 ret = -EINVAL;
1469 goto out;
Chris Wilsonab182822009-09-22 18:46:17 +01001470 }
1471
Chris Wilson05394f32010-11-08 19:18:58 +00001472 if (!obj->base.map_list.map) {
Jesse Barnesde151cf2008-11-12 10:03:55 -08001473 ret = i915_gem_create_mmap_offset(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001474 if (ret)
1475 goto out;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001476 }
1477
Chris Wilson05394f32010-11-08 19:18:58 +00001478 args->offset = (u64)obj->base.map_list.hash.key << PAGE_SHIFT;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001479
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001480out:
Chris Wilson05394f32010-11-08 19:18:58 +00001481 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001482unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001483 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001484 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001485}
1486
Chris Wilsone5281cc2010-10-28 13:45:36 +01001487static int
Chris Wilson05394f32010-11-08 19:18:58 +00001488i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj,
Chris Wilsone5281cc2010-10-28 13:45:36 +01001489 gfp_t gfpmask)
1490{
Chris Wilsone5281cc2010-10-28 13:45:36 +01001491 int page_count, i;
1492 struct address_space *mapping;
1493 struct inode *inode;
1494 struct page *page;
1495
1496 /* Get the list of pages out of our struct file. They'll be pinned
1497 * at this point until we release them.
1498 */
Chris Wilson05394f32010-11-08 19:18:58 +00001499 page_count = obj->base.size / PAGE_SIZE;
1500 BUG_ON(obj->pages != NULL);
1501 obj->pages = drm_malloc_ab(page_count, sizeof(struct page *));
1502 if (obj->pages == NULL)
Chris Wilsone5281cc2010-10-28 13:45:36 +01001503 return -ENOMEM;
1504
Chris Wilson05394f32010-11-08 19:18:58 +00001505 inode = obj->base.filp->f_path.dentry->d_inode;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001506 mapping = inode->i_mapping;
1507 for (i = 0; i < page_count; i++) {
1508 page = read_cache_page_gfp(mapping, i,
1509 GFP_HIGHUSER |
1510 __GFP_COLD |
1511 __GFP_RECLAIMABLE |
1512 gfpmask);
1513 if (IS_ERR(page))
1514 goto err_pages;
1515
Chris Wilson05394f32010-11-08 19:18:58 +00001516 obj->pages[i] = page;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001517 }
1518
Chris Wilson05394f32010-11-08 19:18:58 +00001519 if (obj->tiling_mode != I915_TILING_NONE)
Chris Wilsone5281cc2010-10-28 13:45:36 +01001520 i915_gem_object_do_bit_17_swizzle(obj);
1521
1522 return 0;
1523
1524err_pages:
1525 while (i--)
Chris Wilson05394f32010-11-08 19:18:58 +00001526 page_cache_release(obj->pages[i]);
Chris Wilsone5281cc2010-10-28 13:45:36 +01001527
Chris Wilson05394f32010-11-08 19:18:58 +00001528 drm_free_large(obj->pages);
1529 obj->pages = NULL;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001530 return PTR_ERR(page);
1531}
1532
Chris Wilson5cdf5882010-09-27 15:51:07 +01001533static void
Chris Wilson05394f32010-11-08 19:18:58 +00001534i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07001535{
Chris Wilson05394f32010-11-08 19:18:58 +00001536 int page_count = obj->base.size / PAGE_SIZE;
Eric Anholt673a3942008-07-30 12:06:12 -07001537 int i;
1538
Chris Wilson05394f32010-11-08 19:18:58 +00001539 BUG_ON(obj->madv == __I915_MADV_PURGED);
Eric Anholt856fa192009-03-19 14:10:50 -07001540
Chris Wilson05394f32010-11-08 19:18:58 +00001541 if (obj->tiling_mode != I915_TILING_NONE)
Eric Anholt280b7132009-03-12 16:56:27 -07001542 i915_gem_object_save_bit_17_swizzle(obj);
1543
Chris Wilson05394f32010-11-08 19:18:58 +00001544 if (obj->madv == I915_MADV_DONTNEED)
1545 obj->dirty = 0;
Chris Wilson3ef94da2009-09-14 16:50:29 +01001546
1547 for (i = 0; i < page_count; i++) {
Chris Wilson05394f32010-11-08 19:18:58 +00001548 if (obj->dirty)
1549 set_page_dirty(obj->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001550
Chris Wilson05394f32010-11-08 19:18:58 +00001551 if (obj->madv == I915_MADV_WILLNEED)
1552 mark_page_accessed(obj->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001553
Chris Wilson05394f32010-11-08 19:18:58 +00001554 page_cache_release(obj->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001555 }
Chris Wilson05394f32010-11-08 19:18:58 +00001556 obj->dirty = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001557
Chris Wilson05394f32010-11-08 19:18:58 +00001558 drm_free_large(obj->pages);
1559 obj->pages = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001560}
1561
Chris Wilson54cf91d2010-11-25 18:00:26 +00001562void
Chris Wilson05394f32010-11-08 19:18:58 +00001563i915_gem_object_move_to_active(struct drm_i915_gem_object *obj,
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001564 struct intel_ring_buffer *ring,
1565 u32 seqno)
Eric Anholt673a3942008-07-30 12:06:12 -07001566{
Chris Wilson05394f32010-11-08 19:18:58 +00001567 struct drm_device *dev = obj->base.dev;
Chris Wilson69dc4982010-10-19 10:36:51 +01001568 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter617dbe22010-02-11 22:16:02 +01001569
Zou Nan hai852835f2010-05-21 09:08:56 +08001570 BUG_ON(ring == NULL);
Chris Wilson05394f32010-11-08 19:18:58 +00001571 obj->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001572
1573 /* Add a reference if we're newly entering the active list. */
Chris Wilson05394f32010-11-08 19:18:58 +00001574 if (!obj->active) {
1575 drm_gem_object_reference(&obj->base);
1576 obj->active = 1;
Eric Anholt673a3942008-07-30 12:06:12 -07001577 }
Daniel Vettere35a41d2010-02-11 22:13:59 +01001578
Eric Anholt673a3942008-07-30 12:06:12 -07001579 /* Move from whatever list we were on to the tail of execution. */
Chris Wilson05394f32010-11-08 19:18:58 +00001580 list_move_tail(&obj->mm_list, &dev_priv->mm.active_list);
1581 list_move_tail(&obj->ring_list, &ring->active_list);
Chris Wilsoncaea7472010-11-12 13:53:37 +00001582
Chris Wilson05394f32010-11-08 19:18:58 +00001583 obj->last_rendering_seqno = seqno;
Chris Wilsoncaea7472010-11-12 13:53:37 +00001584 if (obj->fenced_gpu_access) {
1585 struct drm_i915_fence_reg *reg;
1586
1587 BUG_ON(obj->fence_reg == I915_FENCE_REG_NONE);
1588
1589 obj->last_fenced_seqno = seqno;
1590 obj->last_fenced_ring = ring;
1591
1592 reg = &dev_priv->fence_regs[obj->fence_reg];
1593 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
1594 }
1595}
1596
1597static void
1598i915_gem_object_move_off_active(struct drm_i915_gem_object *obj)
1599{
1600 list_del_init(&obj->ring_list);
1601 obj->last_rendering_seqno = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001602}
1603
Eric Anholtce44b0e2008-11-06 16:00:31 -08001604static void
Chris Wilson05394f32010-11-08 19:18:58 +00001605i915_gem_object_move_to_flushing(struct drm_i915_gem_object *obj)
Eric Anholtce44b0e2008-11-06 16:00:31 -08001606{
Chris Wilson05394f32010-11-08 19:18:58 +00001607 struct drm_device *dev = obj->base.dev;
Eric Anholtce44b0e2008-11-06 16:00:31 -08001608 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtce44b0e2008-11-06 16:00:31 -08001609
Chris Wilson05394f32010-11-08 19:18:58 +00001610 BUG_ON(!obj->active);
1611 list_move_tail(&obj->mm_list, &dev_priv->mm.flushing_list);
Chris Wilsoncaea7472010-11-12 13:53:37 +00001612
1613 i915_gem_object_move_off_active(obj);
1614}
1615
1616static void
1617i915_gem_object_move_to_inactive(struct drm_i915_gem_object *obj)
1618{
1619 struct drm_device *dev = obj->base.dev;
1620 struct drm_i915_private *dev_priv = dev->dev_private;
1621
1622 if (obj->pin_count != 0)
1623 list_move_tail(&obj->mm_list, &dev_priv->mm.pinned_list);
1624 else
1625 list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
1626
1627 BUG_ON(!list_empty(&obj->gpu_write_list));
1628 BUG_ON(!obj->active);
1629 obj->ring = NULL;
1630
1631 i915_gem_object_move_off_active(obj);
1632 obj->fenced_gpu_access = false;
Chris Wilsoncaea7472010-11-12 13:53:37 +00001633
1634 obj->active = 0;
Chris Wilson87ca9c82010-12-02 09:42:56 +00001635 obj->pending_gpu_write = false;
Chris Wilsoncaea7472010-11-12 13:53:37 +00001636 drm_gem_object_unreference(&obj->base);
1637
1638 WARN_ON(i915_verify_lists(dev));
Eric Anholtce44b0e2008-11-06 16:00:31 -08001639}
Eric Anholt673a3942008-07-30 12:06:12 -07001640
Chris Wilson963b4832009-09-20 23:03:54 +01001641/* Immediately discard the backing storage */
1642static void
Chris Wilson05394f32010-11-08 19:18:58 +00001643i915_gem_object_truncate(struct drm_i915_gem_object *obj)
Chris Wilson963b4832009-09-20 23:03:54 +01001644{
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001645 struct inode *inode;
Chris Wilson963b4832009-09-20 23:03:54 +01001646
Chris Wilsonae9fed62010-08-07 11:01:30 +01001647 /* Our goal here is to return as much of the memory as
1648 * is possible back to the system as we are called from OOM.
1649 * To do this we must instruct the shmfs to drop all of its
1650 * backing pages, *now*. Here we mirror the actions taken
1651 * when by shmem_delete_inode() to release the backing store.
1652 */
Chris Wilson05394f32010-11-08 19:18:58 +00001653 inode = obj->base.filp->f_path.dentry->d_inode;
Chris Wilsonae9fed62010-08-07 11:01:30 +01001654 truncate_inode_pages(inode->i_mapping, 0);
1655 if (inode->i_op->truncate_range)
1656 inode->i_op->truncate_range(inode, 0, (loff_t)-1);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001657
Chris Wilson05394f32010-11-08 19:18:58 +00001658 obj->madv = __I915_MADV_PURGED;
Chris Wilson963b4832009-09-20 23:03:54 +01001659}
1660
1661static inline int
Chris Wilson05394f32010-11-08 19:18:58 +00001662i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj)
Chris Wilson963b4832009-09-20 23:03:54 +01001663{
Chris Wilson05394f32010-11-08 19:18:58 +00001664 return obj->madv == I915_MADV_DONTNEED;
Chris Wilson963b4832009-09-20 23:03:54 +01001665}
1666
Eric Anholt673a3942008-07-30 12:06:12 -07001667static void
Daniel Vetter63560392010-02-19 11:51:59 +01001668i915_gem_process_flushing_list(struct drm_device *dev,
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001669 uint32_t flush_domains,
Zou Nan hai852835f2010-05-21 09:08:56 +08001670 struct intel_ring_buffer *ring)
Daniel Vetter63560392010-02-19 11:51:59 +01001671{
Chris Wilson05394f32010-11-08 19:18:58 +00001672 struct drm_i915_gem_object *obj, *next;
Daniel Vetter63560392010-02-19 11:51:59 +01001673
Chris Wilson05394f32010-11-08 19:18:58 +00001674 list_for_each_entry_safe(obj, next,
Chris Wilson64193402010-10-24 12:38:05 +01001675 &ring->gpu_write_list,
Daniel Vetter63560392010-02-19 11:51:59 +01001676 gpu_write_list) {
Chris Wilson05394f32010-11-08 19:18:58 +00001677 if (obj->base.write_domain & flush_domains) {
1678 uint32_t old_write_domain = obj->base.write_domain;
Daniel Vetter63560392010-02-19 11:51:59 +01001679
Chris Wilson05394f32010-11-08 19:18:58 +00001680 obj->base.write_domain = 0;
1681 list_del_init(&obj->gpu_write_list);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001682 i915_gem_object_move_to_active(obj, ring,
1683 i915_gem_next_request_seqno(dev, ring));
Daniel Vetter63560392010-02-19 11:51:59 +01001684
Daniel Vetter63560392010-02-19 11:51:59 +01001685 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00001686 obj->base.read_domains,
Daniel Vetter63560392010-02-19 11:51:59 +01001687 old_write_domain);
1688 }
1689 }
1690}
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001691
Chris Wilson3cce4692010-10-27 16:11:02 +01001692int
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001693i915_add_request(struct drm_device *dev,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001694 struct drm_file *file,
Chris Wilson8dc5d142010-08-12 12:36:12 +01001695 struct drm_i915_gem_request *request,
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001696 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001697{
1698 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001699 struct drm_i915_file_private *file_priv = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001700 uint32_t seqno;
1701 int was_empty;
Chris Wilson3cce4692010-10-27 16:11:02 +01001702 int ret;
1703
1704 BUG_ON(request == NULL);
Eric Anholt673a3942008-07-30 12:06:12 -07001705
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001706 if (file != NULL)
1707 file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00001708
Chris Wilson3cce4692010-10-27 16:11:02 +01001709 ret = ring->add_request(ring, &seqno);
1710 if (ret)
1711 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001712
Chris Wilsona56ba562010-09-28 10:07:56 +01001713 ring->outstanding_lazy_request = false;
Eric Anholt673a3942008-07-30 12:06:12 -07001714
1715 request->seqno = seqno;
Zou Nan hai852835f2010-05-21 09:08:56 +08001716 request->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001717 request->emitted_jiffies = jiffies;
Zou Nan hai852835f2010-05-21 09:08:56 +08001718 was_empty = list_empty(&ring->request_list);
1719 list_add_tail(&request->list, &ring->request_list);
1720
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001721 if (file_priv) {
Chris Wilson1c255952010-09-26 11:03:27 +01001722 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001723 request->file_priv = file_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00001724 list_add_tail(&request->client_list,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001725 &file_priv->mm.request_list);
Chris Wilson1c255952010-09-26 11:03:27 +01001726 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00001727 }
Eric Anholt673a3942008-07-30 12:06:12 -07001728
Ben Gamarif65d9422009-09-14 17:48:44 -04001729 if (!dev_priv->mm.suspended) {
Chris Wilsonb3b079d2010-09-13 23:44:34 +01001730 mod_timer(&dev_priv->hangcheck_timer,
1731 jiffies + msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
Ben Gamarif65d9422009-09-14 17:48:44 -04001732 if (was_empty)
Chris Wilsonb3b079d2010-09-13 23:44:34 +01001733 queue_delayed_work(dev_priv->wq,
1734 &dev_priv->mm.retire_work, HZ);
Ben Gamarif65d9422009-09-14 17:48:44 -04001735 }
Chris Wilson3cce4692010-10-27 16:11:02 +01001736 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001737}
1738
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001739static inline void
1740i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
Eric Anholt673a3942008-07-30 12:06:12 -07001741{
Chris Wilson1c255952010-09-26 11:03:27 +01001742 struct drm_i915_file_private *file_priv = request->file_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07001743
Chris Wilson1c255952010-09-26 11:03:27 +01001744 if (!file_priv)
1745 return;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001746
Chris Wilson1c255952010-09-26 11:03:27 +01001747 spin_lock(&file_priv->mm.lock);
1748 list_del(&request->client_list);
1749 request->file_priv = NULL;
1750 spin_unlock(&file_priv->mm.lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001751}
1752
Chris Wilsondfaae392010-09-22 10:31:52 +01001753static void i915_gem_reset_ring_lists(struct drm_i915_private *dev_priv,
1754 struct intel_ring_buffer *ring)
Chris Wilson9375e442010-09-19 12:21:28 +01001755{
Chris Wilsondfaae392010-09-22 10:31:52 +01001756 while (!list_empty(&ring->request_list)) {
1757 struct drm_i915_gem_request *request;
Chris Wilson9375e442010-09-19 12:21:28 +01001758
Chris Wilsondfaae392010-09-22 10:31:52 +01001759 request = list_first_entry(&ring->request_list,
1760 struct drm_i915_gem_request,
1761 list);
1762
1763 list_del(&request->list);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001764 i915_gem_request_remove_from_client(request);
Chris Wilsondfaae392010-09-22 10:31:52 +01001765 kfree(request);
1766 }
1767
1768 while (!list_empty(&ring->active_list)) {
Chris Wilson05394f32010-11-08 19:18:58 +00001769 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001770
Chris Wilson05394f32010-11-08 19:18:58 +00001771 obj = list_first_entry(&ring->active_list,
1772 struct drm_i915_gem_object,
1773 ring_list);
Eric Anholt673a3942008-07-30 12:06:12 -07001774
Chris Wilson05394f32010-11-08 19:18:58 +00001775 obj->base.write_domain = 0;
1776 list_del_init(&obj->gpu_write_list);
1777 i915_gem_object_move_to_inactive(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001778 }
Eric Anholt673a3942008-07-30 12:06:12 -07001779}
1780
Chris Wilson312817a2010-11-22 11:50:11 +00001781static void i915_gem_reset_fences(struct drm_device *dev)
1782{
1783 struct drm_i915_private *dev_priv = dev->dev_private;
1784 int i;
1785
1786 for (i = 0; i < 16; i++) {
1787 struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
Chris Wilson7d2cb392010-11-27 17:38:29 +00001788 struct drm_i915_gem_object *obj = reg->obj;
1789
1790 if (!obj)
1791 continue;
1792
1793 if (obj->tiling_mode)
1794 i915_gem_release_mmap(obj);
1795
Chris Wilsond9e86c02010-11-10 16:40:20 +00001796 reg->obj->fence_reg = I915_FENCE_REG_NONE;
1797 reg->obj->fenced_gpu_access = false;
1798 reg->obj->last_fenced_seqno = 0;
1799 reg->obj->last_fenced_ring = NULL;
1800 i915_gem_clear_fence_reg(dev, reg);
Chris Wilson312817a2010-11-22 11:50:11 +00001801 }
1802}
1803
Chris Wilson069efc12010-09-30 16:53:18 +01001804void i915_gem_reset(struct drm_device *dev)
Eric Anholt673a3942008-07-30 12:06:12 -07001805{
Chris Wilsondfaae392010-09-22 10:31:52 +01001806 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00001807 struct drm_i915_gem_object *obj;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001808 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07001809
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001810 for (i = 0; i < I915_NUM_RINGS; i++)
1811 i915_gem_reset_ring_lists(dev_priv, &dev_priv->ring[i]);
Chris Wilsondfaae392010-09-22 10:31:52 +01001812
1813 /* Remove anything from the flushing lists. The GPU cache is likely
1814 * to be lost on reset along with the data, so simply move the
1815 * lost bo to the inactive list.
1816 */
1817 while (!list_empty(&dev_priv->mm.flushing_list)) {
Chris Wilson05394f32010-11-08 19:18:58 +00001818 obj= list_first_entry(&dev_priv->mm.flushing_list,
1819 struct drm_i915_gem_object,
1820 mm_list);
Chris Wilson9375e442010-09-19 12:21:28 +01001821
Chris Wilson05394f32010-11-08 19:18:58 +00001822 obj->base.write_domain = 0;
1823 list_del_init(&obj->gpu_write_list);
1824 i915_gem_object_move_to_inactive(obj);
Chris Wilson9375e442010-09-19 12:21:28 +01001825 }
Chris Wilson9375e442010-09-19 12:21:28 +01001826
Chris Wilsondfaae392010-09-22 10:31:52 +01001827 /* Move everything out of the GPU domains to ensure we do any
1828 * necessary invalidation upon reuse.
1829 */
Chris Wilson05394f32010-11-08 19:18:58 +00001830 list_for_each_entry(obj,
Chris Wilson77f01232010-09-19 12:31:36 +01001831 &dev_priv->mm.inactive_list,
Chris Wilson69dc4982010-10-19 10:36:51 +01001832 mm_list)
Chris Wilson77f01232010-09-19 12:31:36 +01001833 {
Chris Wilson05394f32010-11-08 19:18:58 +00001834 obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
Chris Wilson77f01232010-09-19 12:31:36 +01001835 }
Chris Wilson069efc12010-09-30 16:53:18 +01001836
1837 /* The fence registers are invalidated so clear them out */
Chris Wilson312817a2010-11-22 11:50:11 +00001838 i915_gem_reset_fences(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07001839}
1840
1841/**
1842 * This function clears the request list as sequence numbers are passed.
1843 */
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001844static void
1845i915_gem_retire_requests_ring(struct drm_device *dev,
1846 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001847{
1848 drm_i915_private_t *dev_priv = dev->dev_private;
1849 uint32_t seqno;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001850 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07001851
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001852 if (!ring->status_page.page_addr ||
1853 list_empty(&ring->request_list))
Karsten Wiese6c0594a2009-02-23 15:07:57 +01001854 return;
1855
Chris Wilson23bc5982010-09-29 16:10:57 +01001856 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07001857
Chris Wilson78501ea2010-10-27 12:18:21 +01001858 seqno = ring->get_seqno(ring);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001859
1860 for (i = 0; i < I915_NUM_RINGS; i++)
1861 if (seqno >= ring->sync_seqno[i])
1862 ring->sync_seqno[i] = 0;
1863
Zou Nan hai852835f2010-05-21 09:08:56 +08001864 while (!list_empty(&ring->request_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001865 struct drm_i915_gem_request *request;
Eric Anholt673a3942008-07-30 12:06:12 -07001866
Zou Nan hai852835f2010-05-21 09:08:56 +08001867 request = list_first_entry(&ring->request_list,
Eric Anholt673a3942008-07-30 12:06:12 -07001868 struct drm_i915_gem_request,
1869 list);
Eric Anholt673a3942008-07-30 12:06:12 -07001870
Chris Wilsondfaae392010-09-22 10:31:52 +01001871 if (!i915_seqno_passed(seqno, request->seqno))
Eric Anholt673a3942008-07-30 12:06:12 -07001872 break;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001873
1874 trace_i915_gem_request_retire(dev, request->seqno);
1875
1876 list_del(&request->list);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001877 i915_gem_request_remove_from_client(request);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001878 kfree(request);
1879 }
1880
1881 /* Move any buffers on the active list that are no longer referenced
1882 * by the ringbuffer to the flushing/inactive lists as appropriate.
1883 */
1884 while (!list_empty(&ring->active_list)) {
Chris Wilson05394f32010-11-08 19:18:58 +00001885 struct drm_i915_gem_object *obj;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001886
Chris Wilson05394f32010-11-08 19:18:58 +00001887 obj= list_first_entry(&ring->active_list,
1888 struct drm_i915_gem_object,
1889 ring_list);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001890
Chris Wilson05394f32010-11-08 19:18:58 +00001891 if (!i915_seqno_passed(seqno, obj->last_rendering_seqno))
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001892 break;
1893
Chris Wilson05394f32010-11-08 19:18:58 +00001894 if (obj->base.write_domain != 0)
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001895 i915_gem_object_move_to_flushing(obj);
1896 else
1897 i915_gem_object_move_to_inactive(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001898 }
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001899
1900 if (unlikely (dev_priv->trace_irq_seqno &&
1901 i915_seqno_passed(dev_priv->trace_irq_seqno, seqno))) {
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001902 ring->irq_put(ring);
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001903 dev_priv->trace_irq_seqno = 0;
1904 }
Chris Wilson23bc5982010-09-29 16:10:57 +01001905
1906 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07001907}
1908
1909void
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001910i915_gem_retire_requests(struct drm_device *dev)
1911{
1912 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001913 int i;
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001914
Chris Wilsonbe726152010-07-23 23:18:50 +01001915 if (!list_empty(&dev_priv->mm.deferred_free_list)) {
Chris Wilson05394f32010-11-08 19:18:58 +00001916 struct drm_i915_gem_object *obj, *next;
Chris Wilsonbe726152010-07-23 23:18:50 +01001917
1918 /* We must be careful that during unbind() we do not
1919 * accidentally infinitely recurse into retire requests.
1920 * Currently:
1921 * retire -> free -> unbind -> wait -> retire_ring
1922 */
Chris Wilson05394f32010-11-08 19:18:58 +00001923 list_for_each_entry_safe(obj, next,
Chris Wilsonbe726152010-07-23 23:18:50 +01001924 &dev_priv->mm.deferred_free_list,
Chris Wilson69dc4982010-10-19 10:36:51 +01001925 mm_list)
Chris Wilson05394f32010-11-08 19:18:58 +00001926 i915_gem_free_object_tail(obj);
Chris Wilsonbe726152010-07-23 23:18:50 +01001927 }
1928
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001929 for (i = 0; i < I915_NUM_RINGS; i++)
1930 i915_gem_retire_requests_ring(dev, &dev_priv->ring[i]);
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001931}
1932
Daniel Vetter75ef9da2010-08-21 00:25:16 +02001933static void
Eric Anholt673a3942008-07-30 12:06:12 -07001934i915_gem_retire_work_handler(struct work_struct *work)
1935{
1936 drm_i915_private_t *dev_priv;
1937 struct drm_device *dev;
1938
1939 dev_priv = container_of(work, drm_i915_private_t,
1940 mm.retire_work.work);
1941 dev = dev_priv->dev;
1942
Chris Wilson891b48c2010-09-29 12:26:37 +01001943 /* Come back later if the device is busy... */
1944 if (!mutex_trylock(&dev->struct_mutex)) {
1945 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
1946 return;
1947 }
1948
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001949 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08001950
Keith Packard6dbe2772008-10-14 21:41:13 -07001951 if (!dev_priv->mm.suspended &&
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001952 (!list_empty(&dev_priv->ring[RCS].request_list) ||
1953 !list_empty(&dev_priv->ring[VCS].request_list) ||
1954 !list_empty(&dev_priv->ring[BCS].request_list)))
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001955 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
Eric Anholt673a3942008-07-30 12:06:12 -07001956 mutex_unlock(&dev->struct_mutex);
1957}
1958
Daniel Vetter5a5a0c62009-09-15 22:57:36 +02001959int
Zou Nan hai852835f2010-05-21 09:08:56 +08001960i915_do_wait_request(struct drm_device *dev, uint32_t seqno,
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001961 bool interruptible, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001962{
1963 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001964 u32 ier;
Eric Anholt673a3942008-07-30 12:06:12 -07001965 int ret = 0;
1966
1967 BUG_ON(seqno == 0);
1968
Ben Gamariba1234d2009-09-14 17:48:47 -04001969 if (atomic_read(&dev_priv->mm.wedged))
Chris Wilson30dbf0c2010-09-25 10:19:17 +01001970 return -EAGAIN;
Ben Gamariffed1d02009-09-14 17:48:41 -04001971
Chris Wilson5d97eb62010-11-10 20:40:02 +00001972 if (seqno == ring->outstanding_lazy_request) {
Chris Wilson3cce4692010-10-27 16:11:02 +01001973 struct drm_i915_gem_request *request;
1974
1975 request = kzalloc(sizeof(*request), GFP_KERNEL);
1976 if (request == NULL)
Daniel Vettere35a41d2010-02-11 22:13:59 +01001977 return -ENOMEM;
Chris Wilson3cce4692010-10-27 16:11:02 +01001978
1979 ret = i915_add_request(dev, NULL, request, ring);
1980 if (ret) {
1981 kfree(request);
1982 return ret;
1983 }
1984
1985 seqno = request->seqno;
Daniel Vettere35a41d2010-02-11 22:13:59 +01001986 }
1987
Chris Wilson78501ea2010-10-27 12:18:21 +01001988 if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) {
Eric Anholtbad720f2009-10-22 16:11:14 -07001989 if (HAS_PCH_SPLIT(dev))
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001990 ier = I915_READ(DEIER) | I915_READ(GTIER);
1991 else
1992 ier = I915_READ(IER);
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001993 if (!ier) {
1994 DRM_ERROR("something (likely vbetool) disabled "
1995 "interrupts, re-enabling\n");
1996 i915_driver_irq_preinstall(dev);
1997 i915_driver_irq_postinstall(dev);
1998 }
1999
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002000 trace_i915_gem_request_wait_begin(dev, seqno);
2001
Chris Wilsonb2223492010-10-27 15:27:33 +01002002 ring->waiting_seqno = seqno;
Chris Wilsonb13c2b92010-12-13 16:54:50 +00002003 if (ring->irq_get(ring)) {
2004 if (interruptible)
2005 ret = wait_event_interruptible(ring->irq_queue,
2006 i915_seqno_passed(ring->get_seqno(ring), seqno)
2007 || atomic_read(&dev_priv->mm.wedged));
2008 else
2009 wait_event(ring->irq_queue,
2010 i915_seqno_passed(ring->get_seqno(ring), seqno)
2011 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02002012
Chris Wilsonb13c2b92010-12-13 16:54:50 +00002013 ring->irq_put(ring);
Chris Wilsonb5ba1772010-12-14 12:17:15 +00002014 } else if (wait_for(i915_seqno_passed(ring->get_seqno(ring),
2015 seqno) ||
2016 atomic_read(&dev_priv->mm.wedged), 3000))
2017 ret = -EBUSY;
Chris Wilsonb2223492010-10-27 15:27:33 +01002018 ring->waiting_seqno = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002019
2020 trace_i915_gem_request_wait_end(dev, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07002021 }
Ben Gamariba1234d2009-09-14 17:48:47 -04002022 if (atomic_read(&dev_priv->mm.wedged))
Chris Wilson30dbf0c2010-09-25 10:19:17 +01002023 ret = -EAGAIN;
Eric Anholt673a3942008-07-30 12:06:12 -07002024
2025 if (ret && ret != -ERESTARTSYS)
Daniel Vetter8bff9172010-02-11 22:19:40 +01002026 DRM_ERROR("%s returns %d (awaiting %d at %d, next %d)\n",
Chris Wilson78501ea2010-10-27 12:18:21 +01002027 __func__, ret, seqno, ring->get_seqno(ring),
Daniel Vetter8bff9172010-02-11 22:19:40 +01002028 dev_priv->next_seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07002029
2030 /* Directly dispatch request retiring. While we have the work queue
2031 * to handle this, the waiter on a request often wants an associated
2032 * buffer to have made it to the inactive list, and we would need
2033 * a separate wait queue to handle that.
2034 */
2035 if (ret == 0)
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002036 i915_gem_retire_requests_ring(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07002037
2038 return ret;
2039}
2040
Daniel Vetter48764bf2009-09-15 22:57:32 +02002041/**
2042 * Waits for a sequence number to be signaled, and cleans up the
2043 * request and object lists appropriately for that event.
2044 */
2045static int
Zou Nan hai852835f2010-05-21 09:08:56 +08002046i915_wait_request(struct drm_device *dev, uint32_t seqno,
Chris Wilsona56ba562010-09-28 10:07:56 +01002047 struct intel_ring_buffer *ring)
Daniel Vetter48764bf2009-09-15 22:57:32 +02002048{
Zou Nan hai852835f2010-05-21 09:08:56 +08002049 return i915_do_wait_request(dev, seqno, 1, ring);
Daniel Vetter48764bf2009-09-15 22:57:32 +02002050}
2051
Eric Anholt673a3942008-07-30 12:06:12 -07002052/**
2053 * Ensures that all rendering to the object has completed and the object is
2054 * safe to unbind from the GTT or access from the CPU.
2055 */
Chris Wilson54cf91d2010-11-25 18:00:26 +00002056int
Chris Wilson05394f32010-11-08 19:18:58 +00002057i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj,
Chris Wilson2cf34d72010-09-14 13:03:28 +01002058 bool interruptible)
Eric Anholt673a3942008-07-30 12:06:12 -07002059{
Chris Wilson05394f32010-11-08 19:18:58 +00002060 struct drm_device *dev = obj->base.dev;
Eric Anholt673a3942008-07-30 12:06:12 -07002061 int ret;
2062
Eric Anholte47c68e2008-11-14 13:35:19 -08002063 /* This function only exists to support waiting for existing rendering,
2064 * not for emitting required flushes.
Eric Anholt673a3942008-07-30 12:06:12 -07002065 */
Chris Wilson05394f32010-11-08 19:18:58 +00002066 BUG_ON((obj->base.write_domain & I915_GEM_GPU_DOMAINS) != 0);
Eric Anholt673a3942008-07-30 12:06:12 -07002067
2068 /* If there is rendering queued on the buffer being evicted, wait for
2069 * it.
2070 */
Chris Wilson05394f32010-11-08 19:18:58 +00002071 if (obj->active) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01002072 ret = i915_do_wait_request(dev,
Chris Wilson05394f32010-11-08 19:18:58 +00002073 obj->last_rendering_seqno,
Chris Wilson2cf34d72010-09-14 13:03:28 +01002074 interruptible,
Chris Wilson05394f32010-11-08 19:18:58 +00002075 obj->ring);
Chris Wilson2cf34d72010-09-14 13:03:28 +01002076 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002077 return ret;
2078 }
2079
2080 return 0;
2081}
2082
2083/**
2084 * Unbinds an object from the GTT aperture.
2085 */
Jesse Barnes0f973f22009-01-26 17:10:45 -08002086int
Chris Wilson05394f32010-11-08 19:18:58 +00002087i915_gem_object_unbind(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002088{
Eric Anholt673a3942008-07-30 12:06:12 -07002089 int ret = 0;
2090
Chris Wilson05394f32010-11-08 19:18:58 +00002091 if (obj->gtt_space == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002092 return 0;
2093
Chris Wilson05394f32010-11-08 19:18:58 +00002094 if (obj->pin_count != 0) {
Eric Anholt673a3942008-07-30 12:06:12 -07002095 DRM_ERROR("Attempting to unbind pinned buffer\n");
2096 return -EINVAL;
2097 }
2098
Eric Anholt5323fd02009-09-09 11:50:45 -07002099 /* blow away mappings if mapped through GTT */
2100 i915_gem_release_mmap(obj);
2101
Eric Anholt673a3942008-07-30 12:06:12 -07002102 /* Move the object to the CPU domain to ensure that
2103 * any possible CPU writes while it's not in the GTT
2104 * are flushed when we go to remap it. This will
2105 * also ensure that all pending GPU writes are finished
2106 * before we unbind.
2107 */
Eric Anholte47c68e2008-11-14 13:35:19 -08002108 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Chris Wilson8dc17752010-07-23 23:18:51 +01002109 if (ret == -ERESTARTSYS)
Eric Anholt673a3942008-07-30 12:06:12 -07002110 return ret;
Chris Wilson8dc17752010-07-23 23:18:51 +01002111 /* Continue on if we fail due to EIO, the GPU is hung so we
2112 * should be safe and we need to cleanup or else we might
2113 * cause memory corruption through use-after-free.
2114 */
Chris Wilson812ed4922010-09-30 15:08:57 +01002115 if (ret) {
2116 i915_gem_clflush_object(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00002117 obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
Chris Wilson812ed4922010-09-30 15:08:57 +01002118 }
Eric Anholt673a3942008-07-30 12:06:12 -07002119
Daniel Vetter96b47b62009-12-15 17:50:00 +01002120 /* release the fence reg _after_ flushing */
Chris Wilsond9e86c02010-11-10 16:40:20 +00002121 ret = i915_gem_object_put_fence(obj);
2122 if (ret == -ERESTARTSYS)
2123 return ret;
Daniel Vetter96b47b62009-12-15 17:50:00 +01002124
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002125 i915_gem_gtt_unbind_object(obj);
Chris Wilsone5281cc2010-10-28 13:45:36 +01002126 i915_gem_object_put_pages_gtt(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002127
Chris Wilson6299f992010-11-24 12:23:44 +00002128 list_del_init(&obj->gtt_list);
Chris Wilson05394f32010-11-08 19:18:58 +00002129 list_del_init(&obj->mm_list);
Daniel Vetter75e9e912010-11-04 17:11:09 +01002130 /* Avoid an unnecessary call to unbind on rebind. */
Chris Wilson05394f32010-11-08 19:18:58 +00002131 obj->map_and_fenceable = true;
Eric Anholt673a3942008-07-30 12:06:12 -07002132
Chris Wilson05394f32010-11-08 19:18:58 +00002133 drm_mm_put_block(obj->gtt_space);
2134 obj->gtt_space = NULL;
2135 obj->gtt_offset = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002136
Chris Wilson05394f32010-11-08 19:18:58 +00002137 if (i915_gem_object_is_purgeable(obj))
Chris Wilson963b4832009-09-20 23:03:54 +01002138 i915_gem_object_truncate(obj);
2139
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002140 trace_i915_gem_object_unbind(obj);
2141
Chris Wilson8dc17752010-07-23 23:18:51 +01002142 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002143}
2144
Chris Wilson54cf91d2010-11-25 18:00:26 +00002145void
2146i915_gem_flush_ring(struct drm_device *dev,
2147 struct intel_ring_buffer *ring,
2148 uint32_t invalidate_domains,
2149 uint32_t flush_domains)
2150{
2151 ring->flush(ring, invalidate_domains, flush_domains);
2152 i915_gem_process_flushing_list(dev, flush_domains, ring);
2153}
2154
Chris Wilsona56ba562010-09-28 10:07:56 +01002155static int i915_ring_idle(struct drm_device *dev,
2156 struct intel_ring_buffer *ring)
2157{
Chris Wilson395b70b2010-10-28 21:28:46 +01002158 if (list_empty(&ring->gpu_write_list) && list_empty(&ring->active_list))
Chris Wilson64193402010-10-24 12:38:05 +01002159 return 0;
2160
Chris Wilson0ac74c62010-12-06 14:36:02 +00002161 if (!list_empty(&ring->gpu_write_list))
2162 i915_gem_flush_ring(dev, ring,
2163 I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
Chris Wilsona56ba562010-09-28 10:07:56 +01002164 return i915_wait_request(dev,
2165 i915_gem_next_request_seqno(dev, ring),
2166 ring);
2167}
2168
Chris Wilsonb47eb4a2010-08-07 11:01:23 +01002169int
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002170i915_gpu_idle(struct drm_device *dev)
2171{
2172 drm_i915_private_t *dev_priv = dev->dev_private;
2173 bool lists_empty;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002174 int ret, i;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002175
Zou Nan haid1b851f2010-05-21 09:08:57 +08002176 lists_empty = (list_empty(&dev_priv->mm.flushing_list) &&
Chris Wilson395b70b2010-10-28 21:28:46 +01002177 list_empty(&dev_priv->mm.active_list));
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002178 if (lists_empty)
2179 return 0;
2180
2181 /* Flush everything onto the inactive list. */
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002182 for (i = 0; i < I915_NUM_RINGS; i++) {
2183 ret = i915_ring_idle(dev, &dev_priv->ring[i]);
2184 if (ret)
2185 return ret;
2186 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08002187
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002188 return 0;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002189}
2190
Daniel Vetterc6642782010-11-12 13:46:18 +00002191static int sandybridge_write_fence_reg(struct drm_i915_gem_object *obj,
2192 struct intel_ring_buffer *pipelined)
Eric Anholt4e901fd2009-10-26 16:44:17 -07002193{
Chris Wilson05394f32010-11-08 19:18:58 +00002194 struct drm_device *dev = obj->base.dev;
Eric Anholt4e901fd2009-10-26 16:44:17 -07002195 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00002196 u32 size = obj->gtt_space->size;
2197 int regnum = obj->fence_reg;
Eric Anholt4e901fd2009-10-26 16:44:17 -07002198 uint64_t val;
2199
Chris Wilson05394f32010-11-08 19:18:58 +00002200 val = (uint64_t)((obj->gtt_offset + size - 4096) &
Daniel Vetterc6642782010-11-12 13:46:18 +00002201 0xfffff000) << 32;
Chris Wilson05394f32010-11-08 19:18:58 +00002202 val |= obj->gtt_offset & 0xfffff000;
2203 val |= (uint64_t)((obj->stride / 128) - 1) <<
Eric Anholt4e901fd2009-10-26 16:44:17 -07002204 SANDYBRIDGE_FENCE_PITCH_SHIFT;
2205
Chris Wilson05394f32010-11-08 19:18:58 +00002206 if (obj->tiling_mode == I915_TILING_Y)
Eric Anholt4e901fd2009-10-26 16:44:17 -07002207 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2208 val |= I965_FENCE_REG_VALID;
2209
Daniel Vetterc6642782010-11-12 13:46:18 +00002210 if (pipelined) {
2211 int ret = intel_ring_begin(pipelined, 6);
2212 if (ret)
2213 return ret;
2214
2215 intel_ring_emit(pipelined, MI_NOOP);
2216 intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(2));
2217 intel_ring_emit(pipelined, FENCE_REG_SANDYBRIDGE_0 + regnum*8);
2218 intel_ring_emit(pipelined, (u32)val);
2219 intel_ring_emit(pipelined, FENCE_REG_SANDYBRIDGE_0 + regnum*8 + 4);
2220 intel_ring_emit(pipelined, (u32)(val >> 32));
2221 intel_ring_advance(pipelined);
2222 } else
2223 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + regnum * 8, val);
2224
2225 return 0;
Eric Anholt4e901fd2009-10-26 16:44:17 -07002226}
2227
Daniel Vetterc6642782010-11-12 13:46:18 +00002228static int i965_write_fence_reg(struct drm_i915_gem_object *obj,
2229 struct intel_ring_buffer *pipelined)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002230{
Chris Wilson05394f32010-11-08 19:18:58 +00002231 struct drm_device *dev = obj->base.dev;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002232 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00002233 u32 size = obj->gtt_space->size;
2234 int regnum = obj->fence_reg;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002235 uint64_t val;
2236
Chris Wilson05394f32010-11-08 19:18:58 +00002237 val = (uint64_t)((obj->gtt_offset + size - 4096) &
Jesse Barnesde151cf2008-11-12 10:03:55 -08002238 0xfffff000) << 32;
Chris Wilson05394f32010-11-08 19:18:58 +00002239 val |= obj->gtt_offset & 0xfffff000;
2240 val |= ((obj->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
2241 if (obj->tiling_mode == I915_TILING_Y)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002242 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2243 val |= I965_FENCE_REG_VALID;
2244
Daniel Vetterc6642782010-11-12 13:46:18 +00002245 if (pipelined) {
2246 int ret = intel_ring_begin(pipelined, 6);
2247 if (ret)
2248 return ret;
2249
2250 intel_ring_emit(pipelined, MI_NOOP);
2251 intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(2));
2252 intel_ring_emit(pipelined, FENCE_REG_965_0 + regnum*8);
2253 intel_ring_emit(pipelined, (u32)val);
2254 intel_ring_emit(pipelined, FENCE_REG_965_0 + regnum*8 + 4);
2255 intel_ring_emit(pipelined, (u32)(val >> 32));
2256 intel_ring_advance(pipelined);
2257 } else
2258 I915_WRITE64(FENCE_REG_965_0 + regnum * 8, val);
2259
2260 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002261}
2262
Daniel Vetterc6642782010-11-12 13:46:18 +00002263static int i915_write_fence_reg(struct drm_i915_gem_object *obj,
2264 struct intel_ring_buffer *pipelined)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002265{
Chris Wilson05394f32010-11-08 19:18:58 +00002266 struct drm_device *dev = obj->base.dev;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002267 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00002268 u32 size = obj->gtt_space->size;
Daniel Vetterc6642782010-11-12 13:46:18 +00002269 u32 fence_reg, val, pitch_val;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002270 int tile_width;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002271
Daniel Vetterc6642782010-11-12 13:46:18 +00002272 if (WARN((obj->gtt_offset & ~I915_FENCE_START_MASK) ||
2273 (size & -size) != size ||
2274 (obj->gtt_offset & (size - 1)),
2275 "object 0x%08x [fenceable? %d] not 1M or pot-size (0x%08x) aligned\n",
2276 obj->gtt_offset, obj->map_and_fenceable, size))
2277 return -EINVAL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002278
Daniel Vetterc6642782010-11-12 13:46:18 +00002279 if (obj->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev))
Jesse Barnes0f973f22009-01-26 17:10:45 -08002280 tile_width = 128;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002281 else
Jesse Barnes0f973f22009-01-26 17:10:45 -08002282 tile_width = 512;
2283
2284 /* Note: pitch better be a power of two tile widths */
Chris Wilson05394f32010-11-08 19:18:58 +00002285 pitch_val = obj->stride / tile_width;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002286 pitch_val = ffs(pitch_val) - 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002287
Chris Wilson05394f32010-11-08 19:18:58 +00002288 val = obj->gtt_offset;
2289 if (obj->tiling_mode == I915_TILING_Y)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002290 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002291 val |= I915_FENCE_SIZE_BITS(size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002292 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2293 val |= I830_FENCE_REG_VALID;
2294
Chris Wilson05394f32010-11-08 19:18:58 +00002295 fence_reg = obj->fence_reg;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002296 if (fence_reg < 8)
2297 fence_reg = FENCE_REG_830_0 + fence_reg * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002298 else
Chris Wilsona00b10c2010-09-24 21:15:47 +01002299 fence_reg = FENCE_REG_945_8 + (fence_reg - 8) * 4;
Daniel Vetterc6642782010-11-12 13:46:18 +00002300
2301 if (pipelined) {
2302 int ret = intel_ring_begin(pipelined, 4);
2303 if (ret)
2304 return ret;
2305
2306 intel_ring_emit(pipelined, MI_NOOP);
2307 intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(1));
2308 intel_ring_emit(pipelined, fence_reg);
2309 intel_ring_emit(pipelined, val);
2310 intel_ring_advance(pipelined);
2311 } else
2312 I915_WRITE(fence_reg, val);
2313
2314 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002315}
2316
Daniel Vetterc6642782010-11-12 13:46:18 +00002317static int i830_write_fence_reg(struct drm_i915_gem_object *obj,
2318 struct intel_ring_buffer *pipelined)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002319{
Chris Wilson05394f32010-11-08 19:18:58 +00002320 struct drm_device *dev = obj->base.dev;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002321 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00002322 u32 size = obj->gtt_space->size;
2323 int regnum = obj->fence_reg;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002324 uint32_t val;
2325 uint32_t pitch_val;
2326
Daniel Vetterc6642782010-11-12 13:46:18 +00002327 if (WARN((obj->gtt_offset & ~I830_FENCE_START_MASK) ||
2328 (size & -size) != size ||
2329 (obj->gtt_offset & (size - 1)),
2330 "object 0x%08x not 512K or pot-size 0x%08x aligned\n",
2331 obj->gtt_offset, size))
2332 return -EINVAL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002333
Chris Wilson05394f32010-11-08 19:18:58 +00002334 pitch_val = obj->stride / 128;
Eric Anholte76a16d2009-05-26 17:44:56 -07002335 pitch_val = ffs(pitch_val) - 1;
Eric Anholte76a16d2009-05-26 17:44:56 -07002336
Chris Wilson05394f32010-11-08 19:18:58 +00002337 val = obj->gtt_offset;
2338 if (obj->tiling_mode == I915_TILING_Y)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002339 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Daniel Vetterc6642782010-11-12 13:46:18 +00002340 val |= I830_FENCE_SIZE_BITS(size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002341 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2342 val |= I830_FENCE_REG_VALID;
2343
Daniel Vetterc6642782010-11-12 13:46:18 +00002344 if (pipelined) {
2345 int ret = intel_ring_begin(pipelined, 4);
2346 if (ret)
2347 return ret;
2348
2349 intel_ring_emit(pipelined, MI_NOOP);
2350 intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(1));
2351 intel_ring_emit(pipelined, FENCE_REG_830_0 + regnum*4);
2352 intel_ring_emit(pipelined, val);
2353 intel_ring_advance(pipelined);
2354 } else
2355 I915_WRITE(FENCE_REG_830_0 + regnum * 4, val);
2356
2357 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002358}
2359
Chris Wilsond9e86c02010-11-10 16:40:20 +00002360static bool ring_passed_seqno(struct intel_ring_buffer *ring, u32 seqno)
2361{
2362 return i915_seqno_passed(ring->get_seqno(ring), seqno);
2363}
2364
2365static int
2366i915_gem_object_flush_fence(struct drm_i915_gem_object *obj,
2367 struct intel_ring_buffer *pipelined,
2368 bool interruptible)
2369{
2370 int ret;
2371
2372 if (obj->fenced_gpu_access) {
2373 if (obj->base.write_domain & I915_GEM_GPU_DOMAINS)
2374 i915_gem_flush_ring(obj->base.dev,
2375 obj->last_fenced_ring,
2376 0, obj->base.write_domain);
2377
2378 obj->fenced_gpu_access = false;
2379 }
2380
2381 if (obj->last_fenced_seqno && pipelined != obj->last_fenced_ring) {
2382 if (!ring_passed_seqno(obj->last_fenced_ring,
2383 obj->last_fenced_seqno)) {
2384 ret = i915_do_wait_request(obj->base.dev,
2385 obj->last_fenced_seqno,
2386 interruptible,
2387 obj->last_fenced_ring);
2388 if (ret)
2389 return ret;
2390 }
2391
2392 obj->last_fenced_seqno = 0;
2393 obj->last_fenced_ring = NULL;
2394 }
2395
Chris Wilson63256ec2011-01-04 18:42:07 +00002396 /* Ensure that all CPU reads are completed before installing a fence
2397 * and all writes before removing the fence.
2398 */
2399 if (obj->base.read_domains & I915_GEM_DOMAIN_GTT)
2400 mb();
2401
Chris Wilsond9e86c02010-11-10 16:40:20 +00002402 return 0;
2403}
2404
2405int
2406i915_gem_object_put_fence(struct drm_i915_gem_object *obj)
2407{
2408 int ret;
2409
2410 if (obj->tiling_mode)
2411 i915_gem_release_mmap(obj);
2412
2413 ret = i915_gem_object_flush_fence(obj, NULL, true);
2414 if (ret)
2415 return ret;
2416
2417 if (obj->fence_reg != I915_FENCE_REG_NONE) {
2418 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2419 i915_gem_clear_fence_reg(obj->base.dev,
2420 &dev_priv->fence_regs[obj->fence_reg]);
2421
2422 obj->fence_reg = I915_FENCE_REG_NONE;
2423 }
2424
2425 return 0;
2426}
2427
2428static struct drm_i915_fence_reg *
2429i915_find_fence_reg(struct drm_device *dev,
2430 struct intel_ring_buffer *pipelined)
Daniel Vetterae3db242010-02-19 11:51:58 +01002431{
Daniel Vetterae3db242010-02-19 11:51:58 +01002432 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002433 struct drm_i915_fence_reg *reg, *first, *avail;
2434 int i;
Daniel Vetterae3db242010-02-19 11:51:58 +01002435
2436 /* First try to find a free reg */
Chris Wilsond9e86c02010-11-10 16:40:20 +00002437 avail = NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002438 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2439 reg = &dev_priv->fence_regs[i];
2440 if (!reg->obj)
Chris Wilsond9e86c02010-11-10 16:40:20 +00002441 return reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01002442
Chris Wilson05394f32010-11-08 19:18:58 +00002443 if (!reg->obj->pin_count)
Chris Wilsond9e86c02010-11-10 16:40:20 +00002444 avail = reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01002445 }
2446
Chris Wilsond9e86c02010-11-10 16:40:20 +00002447 if (avail == NULL)
2448 return NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002449
2450 /* None available, try to steal one or wait for a user to finish */
Chris Wilsond9e86c02010-11-10 16:40:20 +00002451 avail = first = NULL;
2452 list_for_each_entry(reg, &dev_priv->mm.fence_list, lru_list) {
2453 if (reg->obj->pin_count)
Daniel Vetterae3db242010-02-19 11:51:58 +01002454 continue;
2455
Chris Wilsond9e86c02010-11-10 16:40:20 +00002456 if (first == NULL)
2457 first = reg;
2458
2459 if (!pipelined ||
2460 !reg->obj->last_fenced_ring ||
2461 reg->obj->last_fenced_ring == pipelined) {
2462 avail = reg;
2463 break;
2464 }
Daniel Vetterae3db242010-02-19 11:51:58 +01002465 }
2466
Chris Wilsond9e86c02010-11-10 16:40:20 +00002467 if (avail == NULL)
2468 avail = first;
Daniel Vetterae3db242010-02-19 11:51:58 +01002469
Chris Wilsona00b10c2010-09-24 21:15:47 +01002470 return avail;
Daniel Vetterae3db242010-02-19 11:51:58 +01002471}
2472
Jesse Barnesde151cf2008-11-12 10:03:55 -08002473/**
Chris Wilsond9e86c02010-11-10 16:40:20 +00002474 * i915_gem_object_get_fence - set up a fence reg for an object
Jesse Barnesde151cf2008-11-12 10:03:55 -08002475 * @obj: object to map through a fence reg
Chris Wilsond9e86c02010-11-10 16:40:20 +00002476 * @pipelined: ring on which to queue the change, or NULL for CPU access
2477 * @interruptible: must we wait uninterruptibly for the register to retire?
Jesse Barnesde151cf2008-11-12 10:03:55 -08002478 *
2479 * When mapping objects through the GTT, userspace wants to be able to write
2480 * to them without having to worry about swizzling if the object is tiled.
2481 *
2482 * This function walks the fence regs looking for a free one for @obj,
2483 * stealing one if it can't find any.
2484 *
2485 * It then sets up the reg based on the object's properties: address, pitch
2486 * and tiling format.
2487 */
Chris Wilson8c4b8c32009-06-17 22:08:52 +01002488int
Chris Wilsond9e86c02010-11-10 16:40:20 +00002489i915_gem_object_get_fence(struct drm_i915_gem_object *obj,
2490 struct intel_ring_buffer *pipelined,
2491 bool interruptible)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002492{
Chris Wilson05394f32010-11-08 19:18:58 +00002493 struct drm_device *dev = obj->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002494 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002495 struct drm_i915_fence_reg *reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01002496 int ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002497
Chris Wilson6bda10d2010-12-05 21:04:18 +00002498 /* XXX disable pipelining. There are bugs. Shocking. */
2499 pipelined = NULL;
2500
Chris Wilsond9e86c02010-11-10 16:40:20 +00002501 /* Just update our place in the LRU if our fence is getting reused. */
Chris Wilson05394f32010-11-08 19:18:58 +00002502 if (obj->fence_reg != I915_FENCE_REG_NONE) {
2503 reg = &dev_priv->fence_regs[obj->fence_reg];
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002504 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002505
2506 if (!obj->fenced_gpu_access && !obj->last_fenced_seqno)
2507 pipelined = NULL;
2508
2509 if (!pipelined) {
2510 if (reg->setup_seqno) {
2511 if (!ring_passed_seqno(obj->last_fenced_ring,
2512 reg->setup_seqno)) {
2513 ret = i915_do_wait_request(obj->base.dev,
2514 reg->setup_seqno,
2515 interruptible,
2516 obj->last_fenced_ring);
2517 if (ret)
2518 return ret;
2519 }
2520
2521 reg->setup_seqno = 0;
2522 }
2523 } else if (obj->last_fenced_ring &&
2524 obj->last_fenced_ring != pipelined) {
2525 ret = i915_gem_object_flush_fence(obj,
2526 pipelined,
2527 interruptible);
2528 if (ret)
2529 return ret;
2530 } else if (obj->tiling_changed) {
2531 if (obj->fenced_gpu_access) {
2532 if (obj->base.write_domain & I915_GEM_GPU_DOMAINS)
2533 i915_gem_flush_ring(obj->base.dev, obj->ring,
2534 0, obj->base.write_domain);
2535
2536 obj->fenced_gpu_access = false;
2537 }
2538 }
2539
2540 if (!obj->fenced_gpu_access && !obj->last_fenced_seqno)
2541 pipelined = NULL;
2542 BUG_ON(!pipelined && reg->setup_seqno);
2543
2544 if (obj->tiling_changed) {
2545 if (pipelined) {
2546 reg->setup_seqno =
2547 i915_gem_next_request_seqno(dev, pipelined);
2548 obj->last_fenced_seqno = reg->setup_seqno;
2549 obj->last_fenced_ring = pipelined;
2550 }
2551 goto update;
2552 }
2553
Eric Anholta09ba7f2009-08-29 12:49:51 -07002554 return 0;
2555 }
2556
Chris Wilsond9e86c02010-11-10 16:40:20 +00002557 reg = i915_find_fence_reg(dev, pipelined);
2558 if (reg == NULL)
2559 return -ENOSPC;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002560
Chris Wilsond9e86c02010-11-10 16:40:20 +00002561 ret = i915_gem_object_flush_fence(obj, pipelined, interruptible);
2562 if (ret)
Daniel Vetterae3db242010-02-19 11:51:58 +01002563 return ret;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002564
Chris Wilsond9e86c02010-11-10 16:40:20 +00002565 if (reg->obj) {
2566 struct drm_i915_gem_object *old = reg->obj;
2567
2568 drm_gem_object_reference(&old->base);
2569
2570 if (old->tiling_mode)
2571 i915_gem_release_mmap(old);
2572
Chris Wilsond9e86c02010-11-10 16:40:20 +00002573 ret = i915_gem_object_flush_fence(old,
Chris Wilson6bda10d2010-12-05 21:04:18 +00002574 pipelined,
Chris Wilsond9e86c02010-11-10 16:40:20 +00002575 interruptible);
2576 if (ret) {
2577 drm_gem_object_unreference(&old->base);
2578 return ret;
2579 }
2580
2581 if (old->last_fenced_seqno == 0 && obj->last_fenced_seqno == 0)
2582 pipelined = NULL;
2583
2584 old->fence_reg = I915_FENCE_REG_NONE;
2585 old->last_fenced_ring = pipelined;
2586 old->last_fenced_seqno =
2587 pipelined ? i915_gem_next_request_seqno(dev, pipelined) : 0;
2588
2589 drm_gem_object_unreference(&old->base);
2590 } else if (obj->last_fenced_seqno == 0)
2591 pipelined = NULL;
Eric Anholta09ba7f2009-08-29 12:49:51 -07002592
Jesse Barnesde151cf2008-11-12 10:03:55 -08002593 reg->obj = obj;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002594 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
2595 obj->fence_reg = reg - dev_priv->fence_regs;
2596 obj->last_fenced_ring = pipelined;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002597
Chris Wilsond9e86c02010-11-10 16:40:20 +00002598 reg->setup_seqno =
2599 pipelined ? i915_gem_next_request_seqno(dev, pipelined) : 0;
2600 obj->last_fenced_seqno = reg->setup_seqno;
2601
2602update:
2603 obj->tiling_changed = false;
Chris Wilsone259bef2010-09-17 00:32:02 +01002604 switch (INTEL_INFO(dev)->gen) {
2605 case 6:
Daniel Vetterc6642782010-11-12 13:46:18 +00002606 ret = sandybridge_write_fence_reg(obj, pipelined);
Chris Wilsone259bef2010-09-17 00:32:02 +01002607 break;
2608 case 5:
2609 case 4:
Daniel Vetterc6642782010-11-12 13:46:18 +00002610 ret = i965_write_fence_reg(obj, pipelined);
Chris Wilsone259bef2010-09-17 00:32:02 +01002611 break;
2612 case 3:
Daniel Vetterc6642782010-11-12 13:46:18 +00002613 ret = i915_write_fence_reg(obj, pipelined);
Chris Wilsone259bef2010-09-17 00:32:02 +01002614 break;
2615 case 2:
Daniel Vetterc6642782010-11-12 13:46:18 +00002616 ret = i830_write_fence_reg(obj, pipelined);
Chris Wilsone259bef2010-09-17 00:32:02 +01002617 break;
2618 }
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002619
Daniel Vetterc6642782010-11-12 13:46:18 +00002620 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002621}
2622
2623/**
2624 * i915_gem_clear_fence_reg - clear out fence register info
2625 * @obj: object to clear
2626 *
2627 * Zeroes out the fence register itself and clears out the associated
Chris Wilson05394f32010-11-08 19:18:58 +00002628 * data structures in dev_priv and obj.
Jesse Barnesde151cf2008-11-12 10:03:55 -08002629 */
2630static void
Chris Wilsond9e86c02010-11-10 16:40:20 +00002631i915_gem_clear_fence_reg(struct drm_device *dev,
2632 struct drm_i915_fence_reg *reg)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002633{
Jesse Barnes79e53942008-11-07 14:24:08 -08002634 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002635 uint32_t fence_reg = reg - dev_priv->fence_regs;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002636
Chris Wilsone259bef2010-09-17 00:32:02 +01002637 switch (INTEL_INFO(dev)->gen) {
2638 case 6:
Chris Wilsond9e86c02010-11-10 16:40:20 +00002639 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + fence_reg*8, 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002640 break;
2641 case 5:
2642 case 4:
Chris Wilsond9e86c02010-11-10 16:40:20 +00002643 I915_WRITE64(FENCE_REG_965_0 + fence_reg*8, 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002644 break;
2645 case 3:
Chris Wilsond9e86c02010-11-10 16:40:20 +00002646 if (fence_reg >= 8)
2647 fence_reg = FENCE_REG_945_8 + (fence_reg - 8) * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002648 else
Chris Wilsone259bef2010-09-17 00:32:02 +01002649 case 2:
Chris Wilsond9e86c02010-11-10 16:40:20 +00002650 fence_reg = FENCE_REG_830_0 + fence_reg * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002651
2652 I915_WRITE(fence_reg, 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002653 break;
Eric Anholtdc529a42009-03-10 22:34:49 -07002654 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002655
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002656 list_del_init(&reg->lru_list);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002657 reg->obj = NULL;
2658 reg->setup_seqno = 0;
Chris Wilson52dc7d32009-06-06 09:46:01 +01002659}
2660
2661/**
Eric Anholt673a3942008-07-30 12:06:12 -07002662 * Finds free space in the GTT aperture and binds the object there.
2663 */
2664static int
Chris Wilson05394f32010-11-08 19:18:58 +00002665i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
Daniel Vetter920afa72010-09-16 17:54:23 +02002666 unsigned alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01002667 bool map_and_fenceable)
Eric Anholt673a3942008-07-30 12:06:12 -07002668{
Chris Wilson05394f32010-11-08 19:18:58 +00002669 struct drm_device *dev = obj->base.dev;
Eric Anholt673a3942008-07-30 12:06:12 -07002670 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07002671 struct drm_mm_node *free_space;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002672 gfp_t gfpmask = __GFP_NORETRY | __GFP_NOWARN;
Daniel Vetter5e783302010-11-14 22:32:36 +01002673 u32 size, fence_size, fence_alignment, unfenced_alignment;
Daniel Vetter75e9e912010-11-04 17:11:09 +01002674 bool mappable, fenceable;
Chris Wilson07f73f62009-09-14 16:50:30 +01002675 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002676
Chris Wilson05394f32010-11-08 19:18:58 +00002677 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01002678 DRM_ERROR("Attempting to bind a purgeable object\n");
2679 return -EINVAL;
2680 }
2681
Chris Wilson05394f32010-11-08 19:18:58 +00002682 fence_size = i915_gem_get_gtt_size(obj);
2683 fence_alignment = i915_gem_get_gtt_alignment(obj);
2684 unfenced_alignment = i915_gem_get_unfenced_gtt_alignment(obj);
Chris Wilsona00b10c2010-09-24 21:15:47 +01002685
Eric Anholt673a3942008-07-30 12:06:12 -07002686 if (alignment == 0)
Daniel Vetter5e783302010-11-14 22:32:36 +01002687 alignment = map_and_fenceable ? fence_alignment :
2688 unfenced_alignment;
Daniel Vetter75e9e912010-11-04 17:11:09 +01002689 if (map_and_fenceable && alignment & (fence_alignment - 1)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002690 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2691 return -EINVAL;
2692 }
2693
Chris Wilson05394f32010-11-08 19:18:58 +00002694 size = map_and_fenceable ? fence_size : obj->base.size;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002695
Chris Wilson654fc602010-05-27 13:18:21 +01002696 /* If the object is bigger than the entire aperture, reject it early
2697 * before evicting everything in a vain attempt to find space.
2698 */
Chris Wilson05394f32010-11-08 19:18:58 +00002699 if (obj->base.size >
Daniel Vetter75e9e912010-11-04 17:11:09 +01002700 (map_and_fenceable ? dev_priv->mm.gtt_mappable_end : dev_priv->mm.gtt_total)) {
Chris Wilson654fc602010-05-27 13:18:21 +01002701 DRM_ERROR("Attempting to bind an object larger than the aperture\n");
2702 return -E2BIG;
2703 }
2704
Eric Anholt673a3942008-07-30 12:06:12 -07002705 search_free:
Daniel Vetter75e9e912010-11-04 17:11:09 +01002706 if (map_and_fenceable)
Daniel Vetter920afa72010-09-16 17:54:23 +02002707 free_space =
2708 drm_mm_search_free_in_range(&dev_priv->mm.gtt_space,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002709 size, alignment, 0,
Daniel Vetter920afa72010-09-16 17:54:23 +02002710 dev_priv->mm.gtt_mappable_end,
2711 0);
2712 else
2713 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002714 size, alignment, 0);
Daniel Vetter920afa72010-09-16 17:54:23 +02002715
2716 if (free_space != NULL) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01002717 if (map_and_fenceable)
Chris Wilson05394f32010-11-08 19:18:58 +00002718 obj->gtt_space =
Daniel Vetter920afa72010-09-16 17:54:23 +02002719 drm_mm_get_block_range_generic(free_space,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002720 size, alignment, 0,
Daniel Vetter920afa72010-09-16 17:54:23 +02002721 dev_priv->mm.gtt_mappable_end,
2722 0);
2723 else
Chris Wilson05394f32010-11-08 19:18:58 +00002724 obj->gtt_space =
Chris Wilsona00b10c2010-09-24 21:15:47 +01002725 drm_mm_get_block(free_space, size, alignment);
Daniel Vetter920afa72010-09-16 17:54:23 +02002726 }
Chris Wilson05394f32010-11-08 19:18:58 +00002727 if (obj->gtt_space == NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -07002728 /* If the gtt is empty and we're still having trouble
2729 * fitting our object in, we're out of memory.
2730 */
Daniel Vetter75e9e912010-11-04 17:11:09 +01002731 ret = i915_gem_evict_something(dev, size, alignment,
2732 map_and_fenceable);
Chris Wilson97311292009-09-21 00:22:34 +01002733 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002734 return ret;
Chris Wilson97311292009-09-21 00:22:34 +01002735
Eric Anholt673a3942008-07-30 12:06:12 -07002736 goto search_free;
2737 }
2738
Chris Wilsone5281cc2010-10-28 13:45:36 +01002739 ret = i915_gem_object_get_pages_gtt(obj, gfpmask);
Eric Anholt673a3942008-07-30 12:06:12 -07002740 if (ret) {
Chris Wilson05394f32010-11-08 19:18:58 +00002741 drm_mm_put_block(obj->gtt_space);
2742 obj->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002743
2744 if (ret == -ENOMEM) {
2745 /* first try to clear up some space from the GTT */
Chris Wilsona00b10c2010-09-24 21:15:47 +01002746 ret = i915_gem_evict_something(dev, size,
Daniel Vetter75e9e912010-11-04 17:11:09 +01002747 alignment,
2748 map_and_fenceable);
Chris Wilson07f73f62009-09-14 16:50:30 +01002749 if (ret) {
Chris Wilson07f73f62009-09-14 16:50:30 +01002750 /* now try to shrink everyone else */
Chris Wilson4bdadb92010-01-27 13:36:32 +00002751 if (gfpmask) {
2752 gfpmask = 0;
2753 goto search_free;
Chris Wilson07f73f62009-09-14 16:50:30 +01002754 }
2755
2756 return ret;
2757 }
2758
2759 goto search_free;
2760 }
2761
Eric Anholt673a3942008-07-30 12:06:12 -07002762 return ret;
2763 }
2764
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002765 ret = i915_gem_gtt_bind_object(obj);
2766 if (ret) {
Chris Wilsone5281cc2010-10-28 13:45:36 +01002767 i915_gem_object_put_pages_gtt(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00002768 drm_mm_put_block(obj->gtt_space);
2769 obj->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002770
Chris Wilsona00b10c2010-09-24 21:15:47 +01002771 ret = i915_gem_evict_something(dev, size,
Daniel Vetter75e9e912010-11-04 17:11:09 +01002772 alignment, map_and_fenceable);
Chris Wilson97311292009-09-21 00:22:34 +01002773 if (ret)
Chris Wilson07f73f62009-09-14 16:50:30 +01002774 return ret;
Chris Wilson07f73f62009-09-14 16:50:30 +01002775
2776 goto search_free;
Eric Anholt673a3942008-07-30 12:06:12 -07002777 }
Eric Anholt673a3942008-07-30 12:06:12 -07002778
Chris Wilson6299f992010-11-24 12:23:44 +00002779 list_add_tail(&obj->gtt_list, &dev_priv->mm.gtt_list);
Chris Wilson05394f32010-11-08 19:18:58 +00002780 list_add_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
Chris Wilsonbf1a1092010-08-07 11:01:20 +01002781
Eric Anholt673a3942008-07-30 12:06:12 -07002782 /* Assert that the object is not currently in any GPU domain. As it
2783 * wasn't in the GTT, there shouldn't be any way it could have been in
2784 * a GPU cache
2785 */
Chris Wilson05394f32010-11-08 19:18:58 +00002786 BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
2787 BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
Eric Anholt673a3942008-07-30 12:06:12 -07002788
Chris Wilson6299f992010-11-24 12:23:44 +00002789 obj->gtt_offset = obj->gtt_space->start;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002790
Daniel Vetter75e9e912010-11-04 17:11:09 +01002791 fenceable =
Chris Wilson05394f32010-11-08 19:18:58 +00002792 obj->gtt_space->size == fence_size &&
2793 (obj->gtt_space->start & (fence_alignment -1)) == 0;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002794
Daniel Vetter75e9e912010-11-04 17:11:09 +01002795 mappable =
Chris Wilson05394f32010-11-08 19:18:58 +00002796 obj->gtt_offset + obj->base.size <= dev_priv->mm.gtt_mappable_end;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002797
Chris Wilson05394f32010-11-08 19:18:58 +00002798 obj->map_and_fenceable = mappable && fenceable;
Daniel Vetter75e9e912010-11-04 17:11:09 +01002799
Chris Wilson6299f992010-11-24 12:23:44 +00002800 trace_i915_gem_object_bind(obj, obj->gtt_offset, map_and_fenceable);
Eric Anholt673a3942008-07-30 12:06:12 -07002801 return 0;
2802}
2803
2804void
Chris Wilson05394f32010-11-08 19:18:58 +00002805i915_gem_clflush_object(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002806{
Eric Anholt673a3942008-07-30 12:06:12 -07002807 /* If we don't have a page list set up, then we're not pinned
2808 * to GPU, and we can ignore the cache flush because it'll happen
2809 * again at bind time.
2810 */
Chris Wilson05394f32010-11-08 19:18:58 +00002811 if (obj->pages == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002812 return;
2813
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002814 trace_i915_gem_object_clflush(obj);
Eric Anholtcfa16a02009-05-26 18:46:16 -07002815
Chris Wilson05394f32010-11-08 19:18:58 +00002816 drm_clflush_pages(obj->pages, obj->base.size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002817}
2818
Eric Anholte47c68e2008-11-14 13:35:19 -08002819/** Flushes any GPU write domain for the object if it's dirty. */
Chris Wilson3619df02010-11-28 15:37:17 +00002820static void
2821i915_gem_object_flush_gpu_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08002822{
Chris Wilson05394f32010-11-08 19:18:58 +00002823 struct drm_device *dev = obj->base.dev;
Eric Anholte47c68e2008-11-14 13:35:19 -08002824
Chris Wilson05394f32010-11-08 19:18:58 +00002825 if ((obj->base.write_domain & I915_GEM_GPU_DOMAINS) == 0)
Chris Wilson3619df02010-11-28 15:37:17 +00002826 return;
Eric Anholte47c68e2008-11-14 13:35:19 -08002827
2828 /* Queue the GPU write cache flushing we need. */
Chris Wilson05394f32010-11-08 19:18:58 +00002829 i915_gem_flush_ring(dev, obj->ring, 0, obj->base.write_domain);
2830 BUG_ON(obj->base.write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002831}
2832
2833/** Flushes the GTT write domain for the object if it's dirty. */
2834static void
Chris Wilson05394f32010-11-08 19:18:58 +00002835i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08002836{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002837 uint32_t old_write_domain;
2838
Chris Wilson05394f32010-11-08 19:18:58 +00002839 if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
Eric Anholte47c68e2008-11-14 13:35:19 -08002840 return;
2841
Chris Wilson63256ec2011-01-04 18:42:07 +00002842 /* No actual flushing is required for the GTT write domain. Writes
Eric Anholte47c68e2008-11-14 13:35:19 -08002843 * to it immediately go to main memory as far as we know, so there's
2844 * no chipset flush. It also doesn't land in render cache.
Chris Wilson63256ec2011-01-04 18:42:07 +00002845 *
2846 * However, we do have to enforce the order so that all writes through
2847 * the GTT land before any writes to the device, such as updates to
2848 * the GATT itself.
Eric Anholte47c68e2008-11-14 13:35:19 -08002849 */
Chris Wilson63256ec2011-01-04 18:42:07 +00002850 wmb();
2851
Chris Wilson4a684a42010-10-28 14:44:08 +01002852 i915_gem_release_mmap(obj);
2853
Chris Wilson05394f32010-11-08 19:18:58 +00002854 old_write_domain = obj->base.write_domain;
2855 obj->base.write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002856
2857 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00002858 obj->base.read_domains,
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002859 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002860}
2861
2862/** Flushes the CPU write domain for the object if it's dirty. */
2863static void
Chris Wilson05394f32010-11-08 19:18:58 +00002864i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08002865{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002866 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002867
Chris Wilson05394f32010-11-08 19:18:58 +00002868 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
Eric Anholte47c68e2008-11-14 13:35:19 -08002869 return;
2870
2871 i915_gem_clflush_object(obj);
Daniel Vetter40ce6572010-11-05 18:12:18 +01002872 intel_gtt_chipset_flush();
Chris Wilson05394f32010-11-08 19:18:58 +00002873 old_write_domain = obj->base.write_domain;
2874 obj->base.write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002875
2876 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00002877 obj->base.read_domains,
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002878 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002879}
2880
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002881/**
2882 * Moves a single object to the GTT read, and possibly write domain.
2883 *
2884 * This function returns when the move is complete, including waiting on
2885 * flushes to occur.
2886 */
Jesse Barnes79e53942008-11-07 14:24:08 -08002887int
Chris Wilson20217462010-11-23 15:26:33 +00002888i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002889{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002890 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002891 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002892
Eric Anholt02354392008-11-26 13:58:13 -08002893 /* Not valid to be called on unbound objects. */
Chris Wilson05394f32010-11-08 19:18:58 +00002894 if (obj->gtt_space == NULL)
Eric Anholt02354392008-11-26 13:58:13 -08002895 return -EINVAL;
2896
Chris Wilson3619df02010-11-28 15:37:17 +00002897 i915_gem_object_flush_gpu_write_domain(obj);
Chris Wilson87ca9c82010-12-02 09:42:56 +00002898 if (obj->pending_gpu_write || write) {
2899 ret = i915_gem_object_wait_rendering(obj, true);
2900 if (ret)
2901 return ret;
2902 }
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002903
Chris Wilson72133422010-09-13 23:56:38 +01002904 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002905
Chris Wilson05394f32010-11-08 19:18:58 +00002906 old_write_domain = obj->base.write_domain;
2907 old_read_domains = obj->base.read_domains;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002908
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002909 /* It should now be out of any other write domains, and we can update
2910 * the domain values for our changes.
2911 */
Chris Wilson05394f32010-11-08 19:18:58 +00002912 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2913 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002914 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00002915 obj->base.read_domains = I915_GEM_DOMAIN_GTT;
2916 obj->base.write_domain = I915_GEM_DOMAIN_GTT;
2917 obj->dirty = 1;
Eric Anholte47c68e2008-11-14 13:35:19 -08002918 }
2919
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002920 trace_i915_gem_object_change_domain(obj,
2921 old_read_domains,
2922 old_write_domain);
2923
Eric Anholte47c68e2008-11-14 13:35:19 -08002924 return 0;
2925}
2926
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002927/*
2928 * Prepare buffer for display plane. Use uninterruptible for possible flush
2929 * wait, as in modesetting process we're not supposed to be interrupted.
2930 */
2931int
Chris Wilson05394f32010-11-08 19:18:58 +00002932i915_gem_object_set_to_display_plane(struct drm_i915_gem_object *obj,
Chris Wilson919926a2010-11-12 13:42:53 +00002933 struct intel_ring_buffer *pipelined)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002934{
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002935 uint32_t old_read_domains;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002936 int ret;
2937
2938 /* Not valid to be called on unbound objects. */
Chris Wilson05394f32010-11-08 19:18:58 +00002939 if (obj->gtt_space == NULL)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002940 return -EINVAL;
2941
Chris Wilson3619df02010-11-28 15:37:17 +00002942 i915_gem_object_flush_gpu_write_domain(obj);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002943
Chris Wilsonced270f2010-09-26 22:47:46 +01002944 /* Currently, we are always called from an non-interruptible context. */
Chris Wilson0be73282010-12-06 14:36:27 +00002945 if (pipelined != obj->ring) {
Chris Wilsonced270f2010-09-26 22:47:46 +01002946 ret = i915_gem_object_wait_rendering(obj, false);
2947 if (ret)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002948 return ret;
2949 }
2950
Chris Wilsonb118c1e2010-05-27 13:18:14 +01002951 i915_gem_object_flush_cpu_write_domain(obj);
2952
Chris Wilson05394f32010-11-08 19:18:58 +00002953 old_read_domains = obj->base.read_domains;
2954 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002955
2956 trace_i915_gem_object_change_domain(obj,
2957 old_read_domains,
Chris Wilson05394f32010-11-08 19:18:58 +00002958 obj->base.write_domain);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002959
2960 return 0;
2961}
2962
Chris Wilson85345512010-11-13 09:49:11 +00002963int
2964i915_gem_object_flush_gpu(struct drm_i915_gem_object *obj,
2965 bool interruptible)
2966{
2967 if (!obj->active)
2968 return 0;
2969
2970 if (obj->base.write_domain & I915_GEM_GPU_DOMAINS)
Chris Wilson05394f32010-11-08 19:18:58 +00002971 i915_gem_flush_ring(obj->base.dev, obj->ring,
Chris Wilson85345512010-11-13 09:49:11 +00002972 0, obj->base.write_domain);
2973
Chris Wilson05394f32010-11-08 19:18:58 +00002974 return i915_gem_object_wait_rendering(obj, interruptible);
Chris Wilson85345512010-11-13 09:49:11 +00002975}
2976
Eric Anholte47c68e2008-11-14 13:35:19 -08002977/**
2978 * Moves a single object to the CPU read, and possibly write domain.
2979 *
2980 * This function returns when the move is complete, including waiting on
2981 * flushes to occur.
2982 */
2983static int
Chris Wilson919926a2010-11-12 13:42:53 +00002984i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholte47c68e2008-11-14 13:35:19 -08002985{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002986 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002987 int ret;
2988
Chris Wilson3619df02010-11-28 15:37:17 +00002989 i915_gem_object_flush_gpu_write_domain(obj);
Daniel Vetterde18a292010-11-27 22:30:41 +01002990 ret = i915_gem_object_wait_rendering(obj, true);
2991 if (ret)
Eric Anholte47c68e2008-11-14 13:35:19 -08002992 return ret;
2993
2994 i915_gem_object_flush_gtt_write_domain(obj);
2995
2996 /* If we have a partially-valid cache of the object in the CPU,
2997 * finish invalidating it and free the per-page flags.
2998 */
2999 i915_gem_object_set_to_full_cpu_read_domain(obj);
3000
Chris Wilson05394f32010-11-08 19:18:58 +00003001 old_write_domain = obj->base.write_domain;
3002 old_read_domains = obj->base.read_domains;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003003
Eric Anholte47c68e2008-11-14 13:35:19 -08003004 /* Flush the CPU cache if it's still invalid. */
Chris Wilson05394f32010-11-08 19:18:58 +00003005 if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
Eric Anholte47c68e2008-11-14 13:35:19 -08003006 i915_gem_clflush_object(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003007
Chris Wilson05394f32010-11-08 19:18:58 +00003008 obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003009 }
3010
3011 /* It should now be out of any other write domains, and we can update
3012 * the domain values for our changes.
3013 */
Chris Wilson05394f32010-11-08 19:18:58 +00003014 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
Eric Anholte47c68e2008-11-14 13:35:19 -08003015
3016 /* If we're writing through the CPU, then the GPU read domains will
3017 * need to be invalidated at next use.
3018 */
3019 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00003020 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3021 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003022 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003023
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003024 trace_i915_gem_object_change_domain(obj,
3025 old_read_domains,
3026 old_write_domain);
3027
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003028 return 0;
3029}
3030
Eric Anholt673a3942008-07-30 12:06:12 -07003031/**
Eric Anholte47c68e2008-11-14 13:35:19 -08003032 * Moves the object from a partially CPU read to a full one.
Eric Anholt673a3942008-07-30 12:06:12 -07003033 *
Eric Anholte47c68e2008-11-14 13:35:19 -08003034 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
3035 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
3036 */
3037static void
Chris Wilson05394f32010-11-08 19:18:58 +00003038i915_gem_object_set_to_full_cpu_read_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08003039{
Chris Wilson05394f32010-11-08 19:18:58 +00003040 if (!obj->page_cpu_valid)
Eric Anholte47c68e2008-11-14 13:35:19 -08003041 return;
3042
3043 /* If we're partially in the CPU read domain, finish moving it in.
3044 */
Chris Wilson05394f32010-11-08 19:18:58 +00003045 if (obj->base.read_domains & I915_GEM_DOMAIN_CPU) {
Eric Anholte47c68e2008-11-14 13:35:19 -08003046 int i;
3047
Chris Wilson05394f32010-11-08 19:18:58 +00003048 for (i = 0; i <= (obj->base.size - 1) / PAGE_SIZE; i++) {
3049 if (obj->page_cpu_valid[i])
Eric Anholte47c68e2008-11-14 13:35:19 -08003050 continue;
Chris Wilson05394f32010-11-08 19:18:58 +00003051 drm_clflush_pages(obj->pages + i, 1);
Eric Anholte47c68e2008-11-14 13:35:19 -08003052 }
Eric Anholte47c68e2008-11-14 13:35:19 -08003053 }
3054
3055 /* Free the page_cpu_valid mappings which are now stale, whether
3056 * or not we've got I915_GEM_DOMAIN_CPU.
3057 */
Chris Wilson05394f32010-11-08 19:18:58 +00003058 kfree(obj->page_cpu_valid);
3059 obj->page_cpu_valid = NULL;
Eric Anholte47c68e2008-11-14 13:35:19 -08003060}
3061
3062/**
3063 * Set the CPU read domain on a range of the object.
3064 *
3065 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
3066 * not entirely valid. The page_cpu_valid member of the object flags which
3067 * pages have been flushed, and will be respected by
3068 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
3069 * of the whole object.
3070 *
3071 * This function returns when the move is complete, including waiting on
3072 * flushes to occur.
Eric Anholt673a3942008-07-30 12:06:12 -07003073 */
3074static int
Chris Wilson05394f32010-11-08 19:18:58 +00003075i915_gem_object_set_cpu_read_domain_range(struct drm_i915_gem_object *obj,
Eric Anholte47c68e2008-11-14 13:35:19 -08003076 uint64_t offset, uint64_t size)
Eric Anholt673a3942008-07-30 12:06:12 -07003077{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003078 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003079 int i, ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003080
Chris Wilson05394f32010-11-08 19:18:58 +00003081 if (offset == 0 && size == obj->base.size)
Eric Anholte47c68e2008-11-14 13:35:19 -08003082 return i915_gem_object_set_to_cpu_domain(obj, 0);
3083
Chris Wilson3619df02010-11-28 15:37:17 +00003084 i915_gem_object_flush_gpu_write_domain(obj);
Daniel Vetterde18a292010-11-27 22:30:41 +01003085 ret = i915_gem_object_wait_rendering(obj, true);
3086 if (ret)
Eric Anholte47c68e2008-11-14 13:35:19 -08003087 return ret;
Daniel Vetterde18a292010-11-27 22:30:41 +01003088
Eric Anholte47c68e2008-11-14 13:35:19 -08003089 i915_gem_object_flush_gtt_write_domain(obj);
3090
3091 /* If we're already fully in the CPU read domain, we're done. */
Chris Wilson05394f32010-11-08 19:18:58 +00003092 if (obj->page_cpu_valid == NULL &&
3093 (obj->base.read_domains & I915_GEM_DOMAIN_CPU) != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07003094 return 0;
3095
Eric Anholte47c68e2008-11-14 13:35:19 -08003096 /* Otherwise, create/clear the per-page CPU read domain flag if we're
3097 * newly adding I915_GEM_DOMAIN_CPU
3098 */
Chris Wilson05394f32010-11-08 19:18:58 +00003099 if (obj->page_cpu_valid == NULL) {
3100 obj->page_cpu_valid = kzalloc(obj->base.size / PAGE_SIZE,
3101 GFP_KERNEL);
3102 if (obj->page_cpu_valid == NULL)
Eric Anholte47c68e2008-11-14 13:35:19 -08003103 return -ENOMEM;
Chris Wilson05394f32010-11-08 19:18:58 +00003104 } else if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0)
3105 memset(obj->page_cpu_valid, 0, obj->base.size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07003106
3107 /* Flush the cache on any pages that are still invalid from the CPU's
3108 * perspective.
3109 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003110 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
3111 i++) {
Chris Wilson05394f32010-11-08 19:18:58 +00003112 if (obj->page_cpu_valid[i])
Eric Anholt673a3942008-07-30 12:06:12 -07003113 continue;
3114
Chris Wilson05394f32010-11-08 19:18:58 +00003115 drm_clflush_pages(obj->pages + i, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07003116
Chris Wilson05394f32010-11-08 19:18:58 +00003117 obj->page_cpu_valid[i] = 1;
Eric Anholt673a3942008-07-30 12:06:12 -07003118 }
3119
Eric Anholte47c68e2008-11-14 13:35:19 -08003120 /* It should now be out of any other write domains, and we can update
3121 * the domain values for our changes.
3122 */
Chris Wilson05394f32010-11-08 19:18:58 +00003123 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
Eric Anholte47c68e2008-11-14 13:35:19 -08003124
Chris Wilson05394f32010-11-08 19:18:58 +00003125 old_read_domains = obj->base.read_domains;
3126 obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003127
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003128 trace_i915_gem_object_change_domain(obj,
3129 old_read_domains,
Chris Wilson05394f32010-11-08 19:18:58 +00003130 obj->base.write_domain);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003131
Eric Anholt673a3942008-07-30 12:06:12 -07003132 return 0;
3133}
3134
Eric Anholt673a3942008-07-30 12:06:12 -07003135/* Throttle our rendering by waiting until the ring has completed our requests
3136 * emitted over 20 msec ago.
3137 *
Eric Anholtb9624422009-06-03 07:27:35 +00003138 * Note that if we were to use the current jiffies each time around the loop,
3139 * we wouldn't escape the function with any frames outstanding if the time to
3140 * render a frame was over 20ms.
3141 *
Eric Anholt673a3942008-07-30 12:06:12 -07003142 * This should get us reasonable parallelism between CPU and GPU but also
3143 * relatively low latency when blocking on a particular request to finish.
3144 */
3145static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003146i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003147{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003148 struct drm_i915_private *dev_priv = dev->dev_private;
3149 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00003150 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003151 struct drm_i915_gem_request *request;
3152 struct intel_ring_buffer *ring = NULL;
3153 u32 seqno = 0;
3154 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003155
Chris Wilson1c255952010-09-26 11:03:27 +01003156 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003157 list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
Eric Anholtb9624422009-06-03 07:27:35 +00003158 if (time_after_eq(request->emitted_jiffies, recent_enough))
3159 break;
3160
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003161 ring = request->ring;
3162 seqno = request->seqno;
Eric Anholtb9624422009-06-03 07:27:35 +00003163 }
Chris Wilson1c255952010-09-26 11:03:27 +01003164 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003165
3166 if (seqno == 0)
3167 return 0;
3168
3169 ret = 0;
Chris Wilson78501ea2010-10-27 12:18:21 +01003170 if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) {
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003171 /* And wait for the seqno passing without holding any locks and
3172 * causing extra latency for others. This is safe as the irq
3173 * generation is designed to be run atomically and so is
3174 * lockless.
3175 */
Chris Wilsonb13c2b92010-12-13 16:54:50 +00003176 if (ring->irq_get(ring)) {
3177 ret = wait_event_interruptible(ring->irq_queue,
3178 i915_seqno_passed(ring->get_seqno(ring), seqno)
3179 || atomic_read(&dev_priv->mm.wedged));
3180 ring->irq_put(ring);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003181
Chris Wilsonb13c2b92010-12-13 16:54:50 +00003182 if (ret == 0 && atomic_read(&dev_priv->mm.wedged))
3183 ret = -EIO;
3184 }
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003185 }
3186
3187 if (ret == 0)
3188 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
Eric Anholtb9624422009-06-03 07:27:35 +00003189
Eric Anholt673a3942008-07-30 12:06:12 -07003190 return ret;
3191}
3192
Eric Anholt673a3942008-07-30 12:06:12 -07003193int
Chris Wilson05394f32010-11-08 19:18:58 +00003194i915_gem_object_pin(struct drm_i915_gem_object *obj,
3195 uint32_t alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01003196 bool map_and_fenceable)
Eric Anholt673a3942008-07-30 12:06:12 -07003197{
Chris Wilson05394f32010-11-08 19:18:58 +00003198 struct drm_device *dev = obj->base.dev;
Chris Wilsonf13d3f72010-09-20 17:36:15 +01003199 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003200 int ret;
3201
Chris Wilson05394f32010-11-08 19:18:58 +00003202 BUG_ON(obj->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT);
Chris Wilson23bc5982010-09-29 16:10:57 +01003203 WARN_ON(i915_verify_lists(dev));
Chris Wilsonac0c6b52010-05-27 13:18:18 +01003204
Chris Wilson05394f32010-11-08 19:18:58 +00003205 if (obj->gtt_space != NULL) {
3206 if ((alignment && obj->gtt_offset & (alignment - 1)) ||
3207 (map_and_fenceable && !obj->map_and_fenceable)) {
3208 WARN(obj->pin_count,
Chris Wilsonae7d49d2010-08-04 12:37:41 +01003209 "bo is already pinned with incorrect alignment:"
Daniel Vetter75e9e912010-11-04 17:11:09 +01003210 " offset=%x, req.alignment=%x, req.map_and_fenceable=%d,"
3211 " obj->map_and_fenceable=%d\n",
Chris Wilson05394f32010-11-08 19:18:58 +00003212 obj->gtt_offset, alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01003213 map_and_fenceable,
Chris Wilson05394f32010-11-08 19:18:58 +00003214 obj->map_and_fenceable);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01003215 ret = i915_gem_object_unbind(obj);
3216 if (ret)
3217 return ret;
3218 }
3219 }
3220
Chris Wilson05394f32010-11-08 19:18:58 +00003221 if (obj->gtt_space == NULL) {
Chris Wilsona00b10c2010-09-24 21:15:47 +01003222 ret = i915_gem_object_bind_to_gtt(obj, alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01003223 map_and_fenceable);
Chris Wilson97311292009-09-21 00:22:34 +01003224 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07003225 return ret;
Chris Wilson22c344e2009-02-11 14:26:45 +00003226 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05003227
Chris Wilson05394f32010-11-08 19:18:58 +00003228 if (obj->pin_count++ == 0) {
Chris Wilson05394f32010-11-08 19:18:58 +00003229 if (!obj->active)
3230 list_move_tail(&obj->mm_list,
Chris Wilsonf13d3f72010-09-20 17:36:15 +01003231 &dev_priv->mm.pinned_list);
Eric Anholt673a3942008-07-30 12:06:12 -07003232 }
Chris Wilson6299f992010-11-24 12:23:44 +00003233 obj->pin_mappable |= map_and_fenceable;
Eric Anholt673a3942008-07-30 12:06:12 -07003234
Chris Wilson23bc5982010-09-29 16:10:57 +01003235 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07003236 return 0;
3237}
3238
3239void
Chris Wilson05394f32010-11-08 19:18:58 +00003240i915_gem_object_unpin(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07003241{
Chris Wilson05394f32010-11-08 19:18:58 +00003242 struct drm_device *dev = obj->base.dev;
Eric Anholt673a3942008-07-30 12:06:12 -07003243 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003244
Chris Wilson23bc5982010-09-29 16:10:57 +01003245 WARN_ON(i915_verify_lists(dev));
Chris Wilson05394f32010-11-08 19:18:58 +00003246 BUG_ON(obj->pin_count == 0);
3247 BUG_ON(obj->gtt_space == NULL);
Eric Anholt673a3942008-07-30 12:06:12 -07003248
Chris Wilson05394f32010-11-08 19:18:58 +00003249 if (--obj->pin_count == 0) {
3250 if (!obj->active)
3251 list_move_tail(&obj->mm_list,
Eric Anholt673a3942008-07-30 12:06:12 -07003252 &dev_priv->mm.inactive_list);
Chris Wilson6299f992010-11-24 12:23:44 +00003253 obj->pin_mappable = false;
Eric Anholt673a3942008-07-30 12:06:12 -07003254 }
Chris Wilson23bc5982010-09-29 16:10:57 +01003255 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07003256}
3257
3258int
3259i915_gem_pin_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00003260 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003261{
3262 struct drm_i915_gem_pin *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003263 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07003264 int ret;
3265
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003266 ret = i915_mutex_lock_interruptible(dev);
3267 if (ret)
3268 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003269
Chris Wilson05394f32010-11-08 19:18:58 +00003270 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Eric Anholt673a3942008-07-30 12:06:12 -07003271 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003272 ret = -ENOENT;
3273 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07003274 }
Eric Anholt673a3942008-07-30 12:06:12 -07003275
Chris Wilson05394f32010-11-08 19:18:58 +00003276 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilsonbb6baf72009-09-22 14:24:13 +01003277 DRM_ERROR("Attempting to pin a purgeable buffer\n");
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003278 ret = -EINVAL;
3279 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003280 }
3281
Chris Wilson05394f32010-11-08 19:18:58 +00003282 if (obj->pin_filp != NULL && obj->pin_filp != file) {
Jesse Barnes79e53942008-11-07 14:24:08 -08003283 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
3284 args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003285 ret = -EINVAL;
3286 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08003287 }
3288
Chris Wilson05394f32010-11-08 19:18:58 +00003289 obj->user_pin_count++;
3290 obj->pin_filp = file;
3291 if (obj->user_pin_count == 1) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01003292 ret = i915_gem_object_pin(obj, args->alignment, true);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003293 if (ret)
3294 goto out;
Eric Anholt673a3942008-07-30 12:06:12 -07003295 }
3296
3297 /* XXX - flush the CPU caches for pinned objects
3298 * as the X server doesn't manage domains yet
3299 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003300 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00003301 args->offset = obj->gtt_offset;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003302out:
Chris Wilson05394f32010-11-08 19:18:58 +00003303 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003304unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07003305 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003306 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003307}
3308
3309int
3310i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00003311 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003312{
3313 struct drm_i915_gem_pin *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003314 struct drm_i915_gem_object *obj;
Chris Wilson76c1dec2010-09-25 11:22:51 +01003315 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003316
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003317 ret = i915_mutex_lock_interruptible(dev);
3318 if (ret)
3319 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003320
Chris Wilson05394f32010-11-08 19:18:58 +00003321 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Eric Anholt673a3942008-07-30 12:06:12 -07003322 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003323 ret = -ENOENT;
3324 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07003325 }
Chris Wilson76c1dec2010-09-25 11:22:51 +01003326
Chris Wilson05394f32010-11-08 19:18:58 +00003327 if (obj->pin_filp != file) {
Jesse Barnes79e53942008-11-07 14:24:08 -08003328 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
3329 args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003330 ret = -EINVAL;
3331 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08003332 }
Chris Wilson05394f32010-11-08 19:18:58 +00003333 obj->user_pin_count--;
3334 if (obj->user_pin_count == 0) {
3335 obj->pin_filp = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08003336 i915_gem_object_unpin(obj);
3337 }
Eric Anholt673a3942008-07-30 12:06:12 -07003338
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003339out:
Chris Wilson05394f32010-11-08 19:18:58 +00003340 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003341unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07003342 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003343 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003344}
3345
3346int
3347i915_gem_busy_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00003348 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003349{
3350 struct drm_i915_gem_busy *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003351 struct drm_i915_gem_object *obj;
Chris Wilson30dbf0c2010-09-25 10:19:17 +01003352 int ret;
3353
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003354 ret = i915_mutex_lock_interruptible(dev);
3355 if (ret)
3356 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003357
Chris Wilson05394f32010-11-08 19:18:58 +00003358 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Eric Anholt673a3942008-07-30 12:06:12 -07003359 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003360 ret = -ENOENT;
3361 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07003362 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08003363
Chris Wilson0be555b2010-08-04 15:36:30 +01003364 /* Count all active objects as busy, even if they are currently not used
3365 * by the gpu. Users of this interface expect objects to eventually
3366 * become non-busy without any further actions, therefore emit any
3367 * necessary flushes here.
Eric Anholtc4de0a52008-12-14 19:05:04 -08003368 */
Chris Wilson05394f32010-11-08 19:18:58 +00003369 args->busy = obj->active;
Chris Wilson0be555b2010-08-04 15:36:30 +01003370 if (args->busy) {
3371 /* Unconditionally flush objects, even when the gpu still uses this
3372 * object. Userspace calling this function indicates that it wants to
3373 * use this buffer rather sooner than later, so issuing the required
3374 * flush earlier is beneficial.
3375 */
Chris Wilson1a1c6972010-12-07 23:00:20 +00003376 if (obj->base.write_domain & I915_GEM_GPU_DOMAINS) {
Chris Wilson05394f32010-11-08 19:18:58 +00003377 i915_gem_flush_ring(dev, obj->ring,
3378 0, obj->base.write_domain);
Chris Wilson1a1c6972010-12-07 23:00:20 +00003379 } else if (obj->ring->outstanding_lazy_request ==
3380 obj->last_rendering_seqno) {
3381 struct drm_i915_gem_request *request;
3382
Chris Wilson7a194872010-12-07 10:38:40 +00003383 /* This ring is not being cleared by active usage,
3384 * so emit a request to do so.
3385 */
Chris Wilson1a1c6972010-12-07 23:00:20 +00003386 request = kzalloc(sizeof(*request), GFP_KERNEL);
3387 if (request)
3388 ret = i915_add_request(dev,
3389 NULL, request,
3390 obj->ring);
3391 else
Chris Wilson7a194872010-12-07 10:38:40 +00003392 ret = -ENOMEM;
3393 }
Chris Wilson0be555b2010-08-04 15:36:30 +01003394
3395 /* Update the active list for the hardware's current position.
3396 * Otherwise this only updates on a delayed timer or when irqs
3397 * are actually unmasked, and our working set ends up being
3398 * larger than required.
3399 */
Chris Wilson05394f32010-11-08 19:18:58 +00003400 i915_gem_retire_requests_ring(dev, obj->ring);
Chris Wilson0be555b2010-08-04 15:36:30 +01003401
Chris Wilson05394f32010-11-08 19:18:58 +00003402 args->busy = obj->active;
Chris Wilson0be555b2010-08-04 15:36:30 +01003403 }
Eric Anholt673a3942008-07-30 12:06:12 -07003404
Chris Wilson05394f32010-11-08 19:18:58 +00003405 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003406unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07003407 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003408 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003409}
3410
3411int
3412i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
3413 struct drm_file *file_priv)
3414{
3415 return i915_gem_ring_throttle(dev, file_priv);
3416}
3417
Chris Wilson3ef94da2009-09-14 16:50:29 +01003418int
3419i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
3420 struct drm_file *file_priv)
3421{
3422 struct drm_i915_gem_madvise *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003423 struct drm_i915_gem_object *obj;
Chris Wilson76c1dec2010-09-25 11:22:51 +01003424 int ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003425
3426 switch (args->madv) {
3427 case I915_MADV_DONTNEED:
3428 case I915_MADV_WILLNEED:
3429 break;
3430 default:
3431 return -EINVAL;
3432 }
3433
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003434 ret = i915_mutex_lock_interruptible(dev);
3435 if (ret)
3436 return ret;
3437
Chris Wilson05394f32010-11-08 19:18:58 +00003438 obj = to_intel_bo(drm_gem_object_lookup(dev, file_priv, args->handle));
Chris Wilson3ef94da2009-09-14 16:50:29 +01003439 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003440 ret = -ENOENT;
3441 goto unlock;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003442 }
Chris Wilson3ef94da2009-09-14 16:50:29 +01003443
Chris Wilson05394f32010-11-08 19:18:58 +00003444 if (obj->pin_count) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003445 ret = -EINVAL;
3446 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003447 }
3448
Chris Wilson05394f32010-11-08 19:18:58 +00003449 if (obj->madv != __I915_MADV_PURGED)
3450 obj->madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003451
Chris Wilson2d7ef392009-09-20 23:13:10 +01003452 /* if the object is no longer bound, discard its backing storage */
Chris Wilson05394f32010-11-08 19:18:58 +00003453 if (i915_gem_object_is_purgeable(obj) &&
3454 obj->gtt_space == NULL)
Chris Wilson2d7ef392009-09-20 23:13:10 +01003455 i915_gem_object_truncate(obj);
3456
Chris Wilson05394f32010-11-08 19:18:58 +00003457 args->retained = obj->madv != __I915_MADV_PURGED;
Chris Wilsonbb6baf72009-09-22 14:24:13 +01003458
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003459out:
Chris Wilson05394f32010-11-08 19:18:58 +00003460 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003461unlock:
Chris Wilson3ef94da2009-09-14 16:50:29 +01003462 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003463 return ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003464}
3465
Chris Wilson05394f32010-11-08 19:18:58 +00003466struct drm_i915_gem_object *i915_gem_alloc_object(struct drm_device *dev,
3467 size_t size)
Daniel Vetterac52bc52010-04-09 19:05:06 +00003468{
Chris Wilson73aa8082010-09-30 11:46:12 +01003469 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc397b902010-04-09 19:05:07 +00003470 struct drm_i915_gem_object *obj;
3471
3472 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
3473 if (obj == NULL)
3474 return NULL;
3475
3476 if (drm_gem_object_init(dev, &obj->base, size) != 0) {
3477 kfree(obj);
3478 return NULL;
3479 }
3480
Chris Wilson73aa8082010-09-30 11:46:12 +01003481 i915_gem_info_add_obj(dev_priv, size);
3482
Daniel Vetterc397b902010-04-09 19:05:07 +00003483 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
3484 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3485
3486 obj->agp_type = AGP_USER_MEMORY;
Daniel Vetter62b8b212010-04-09 19:05:08 +00003487 obj->base.driver_private = NULL;
Daniel Vetterc397b902010-04-09 19:05:07 +00003488 obj->fence_reg = I915_FENCE_REG_NONE;
Chris Wilson69dc4982010-10-19 10:36:51 +01003489 INIT_LIST_HEAD(&obj->mm_list);
Daniel Vetter93a37f22010-11-05 20:24:53 +01003490 INIT_LIST_HEAD(&obj->gtt_list);
Chris Wilson69dc4982010-10-19 10:36:51 +01003491 INIT_LIST_HEAD(&obj->ring_list);
Chris Wilson432e58e2010-11-25 19:32:06 +00003492 INIT_LIST_HEAD(&obj->exec_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00003493 INIT_LIST_HEAD(&obj->gpu_write_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00003494 obj->madv = I915_MADV_WILLNEED;
Daniel Vetter75e9e912010-11-04 17:11:09 +01003495 /* Avoid an unnecessary call to unbind on the first bind. */
3496 obj->map_and_fenceable = true;
Daniel Vetterc397b902010-04-09 19:05:07 +00003497
Chris Wilson05394f32010-11-08 19:18:58 +00003498 return obj;
Daniel Vetterac52bc52010-04-09 19:05:06 +00003499}
3500
Eric Anholt673a3942008-07-30 12:06:12 -07003501int i915_gem_init_object(struct drm_gem_object *obj)
3502{
Daniel Vetterc397b902010-04-09 19:05:07 +00003503 BUG();
Jesse Barnesde151cf2008-11-12 10:03:55 -08003504
Eric Anholt673a3942008-07-30 12:06:12 -07003505 return 0;
3506}
3507
Chris Wilson05394f32010-11-08 19:18:58 +00003508static void i915_gem_free_object_tail(struct drm_i915_gem_object *obj)
Chris Wilsonbe726152010-07-23 23:18:50 +01003509{
Chris Wilson05394f32010-11-08 19:18:58 +00003510 struct drm_device *dev = obj->base.dev;
Chris Wilsonbe726152010-07-23 23:18:50 +01003511 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonbe726152010-07-23 23:18:50 +01003512 int ret;
3513
3514 ret = i915_gem_object_unbind(obj);
3515 if (ret == -ERESTARTSYS) {
Chris Wilson05394f32010-11-08 19:18:58 +00003516 list_move(&obj->mm_list,
Chris Wilsonbe726152010-07-23 23:18:50 +01003517 &dev_priv->mm.deferred_free_list);
3518 return;
3519 }
3520
Chris Wilson05394f32010-11-08 19:18:58 +00003521 if (obj->base.map_list.map)
Chris Wilsonbe726152010-07-23 23:18:50 +01003522 i915_gem_free_mmap_offset(obj);
3523
Chris Wilson05394f32010-11-08 19:18:58 +00003524 drm_gem_object_release(&obj->base);
3525 i915_gem_info_remove_obj(dev_priv, obj->base.size);
Chris Wilsonbe726152010-07-23 23:18:50 +01003526
Chris Wilson05394f32010-11-08 19:18:58 +00003527 kfree(obj->page_cpu_valid);
3528 kfree(obj->bit_17);
3529 kfree(obj);
Chris Wilsonbe726152010-07-23 23:18:50 +01003530}
3531
Chris Wilson05394f32010-11-08 19:18:58 +00003532void i915_gem_free_object(struct drm_gem_object *gem_obj)
Eric Anholt673a3942008-07-30 12:06:12 -07003533{
Chris Wilson05394f32010-11-08 19:18:58 +00003534 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
3535 struct drm_device *dev = obj->base.dev;
Eric Anholt673a3942008-07-30 12:06:12 -07003536
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003537 trace_i915_gem_object_destroy(obj);
3538
Chris Wilson05394f32010-11-08 19:18:58 +00003539 while (obj->pin_count > 0)
Eric Anholt673a3942008-07-30 12:06:12 -07003540 i915_gem_object_unpin(obj);
3541
Chris Wilson05394f32010-11-08 19:18:58 +00003542 if (obj->phys_obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10003543 i915_gem_detach_phys_object(dev, obj);
3544
Chris Wilsonbe726152010-07-23 23:18:50 +01003545 i915_gem_free_object_tail(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003546}
3547
Jesse Barnes5669fca2009-02-17 15:13:31 -08003548int
Eric Anholt673a3942008-07-30 12:06:12 -07003549i915_gem_idle(struct drm_device *dev)
3550{
3551 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson29105cc2010-01-07 10:39:13 +00003552 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003553
Keith Packard6dbe2772008-10-14 21:41:13 -07003554 mutex_lock(&dev->struct_mutex);
3555
Chris Wilson87acb0a2010-10-19 10:13:00 +01003556 if (dev_priv->mm.suspended) {
Keith Packard6dbe2772008-10-14 21:41:13 -07003557 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07003558 return 0;
Keith Packard6dbe2772008-10-14 21:41:13 -07003559 }
Eric Anholt673a3942008-07-30 12:06:12 -07003560
Chris Wilson29105cc2010-01-07 10:39:13 +00003561 ret = i915_gpu_idle(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07003562 if (ret) {
3563 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07003564 return ret;
Keith Packard6dbe2772008-10-14 21:41:13 -07003565 }
Eric Anholt673a3942008-07-30 12:06:12 -07003566
Chris Wilson29105cc2010-01-07 10:39:13 +00003567 /* Under UMS, be paranoid and evict. */
3568 if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
Chris Wilson5eac3ab2010-10-31 08:49:47 +00003569 ret = i915_gem_evict_inactive(dev, false);
Chris Wilson29105cc2010-01-07 10:39:13 +00003570 if (ret) {
3571 mutex_unlock(&dev->struct_mutex);
3572 return ret;
3573 }
3574 }
3575
Chris Wilson312817a2010-11-22 11:50:11 +00003576 i915_gem_reset_fences(dev);
3577
Chris Wilson29105cc2010-01-07 10:39:13 +00003578 /* Hack! Don't let anybody do execbuf while we don't control the chip.
3579 * We need to replace this with a semaphore, or something.
3580 * And not confound mm.suspended!
3581 */
3582 dev_priv->mm.suspended = 1;
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02003583 del_timer_sync(&dev_priv->hangcheck_timer);
Chris Wilson29105cc2010-01-07 10:39:13 +00003584
3585 i915_kernel_lost_context(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07003586 i915_gem_cleanup_ringbuffer(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00003587
Keith Packard6dbe2772008-10-14 21:41:13 -07003588 mutex_unlock(&dev->struct_mutex);
3589
Chris Wilson29105cc2010-01-07 10:39:13 +00003590 /* Cancel the retire work handler, which should be idle now. */
3591 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
3592
Eric Anholt673a3942008-07-30 12:06:12 -07003593 return 0;
3594}
3595
Eric Anholt673a3942008-07-30 12:06:12 -07003596int
Zou Nan hai8187a2b2010-05-21 09:08:55 +08003597i915_gem_init_ringbuffer(struct drm_device *dev)
3598{
3599 drm_i915_private_t *dev_priv = dev->dev_private;
3600 int ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01003601
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08003602 ret = intel_init_render_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01003603 if (ret)
Chris Wilsonb6913e42010-11-12 10:46:37 +00003604 return ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01003605
3606 if (HAS_BSD(dev)) {
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08003607 ret = intel_init_bsd_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01003608 if (ret)
3609 goto cleanup_render_ring;
Zou Nan haid1b851f2010-05-21 09:08:57 +08003610 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01003611
Chris Wilson549f7362010-10-19 11:19:32 +01003612 if (HAS_BLT(dev)) {
3613 ret = intel_init_blt_ring_buffer(dev);
3614 if (ret)
3615 goto cleanup_bsd_ring;
3616 }
3617
Chris Wilson6f392d5482010-08-07 11:01:22 +01003618 dev_priv->next_seqno = 1;
3619
Chris Wilson68f95ba2010-05-27 13:18:22 +01003620 return 0;
3621
Chris Wilson549f7362010-10-19 11:19:32 +01003622cleanup_bsd_ring:
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003623 intel_cleanup_ring_buffer(&dev_priv->ring[VCS]);
Chris Wilson68f95ba2010-05-27 13:18:22 +01003624cleanup_render_ring:
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003625 intel_cleanup_ring_buffer(&dev_priv->ring[RCS]);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08003626 return ret;
3627}
3628
3629void
3630i915_gem_cleanup_ringbuffer(struct drm_device *dev)
3631{
3632 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003633 int i;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08003634
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003635 for (i = 0; i < I915_NUM_RINGS; i++)
3636 intel_cleanup_ring_buffer(&dev_priv->ring[i]);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08003637}
3638
3639int
Eric Anholt673a3942008-07-30 12:06:12 -07003640i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
3641 struct drm_file *file_priv)
3642{
3643 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003644 int ret, i;
Eric Anholt673a3942008-07-30 12:06:12 -07003645
Jesse Barnes79e53942008-11-07 14:24:08 -08003646 if (drm_core_check_feature(dev, DRIVER_MODESET))
3647 return 0;
3648
Ben Gamariba1234d2009-09-14 17:48:47 -04003649 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07003650 DRM_ERROR("Reenabling wedged hardware, good luck\n");
Ben Gamariba1234d2009-09-14 17:48:47 -04003651 atomic_set(&dev_priv->mm.wedged, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07003652 }
3653
Eric Anholt673a3942008-07-30 12:06:12 -07003654 mutex_lock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08003655 dev_priv->mm.suspended = 0;
3656
3657 ret = i915_gem_init_ringbuffer(dev);
Wu Fengguangd816f6a2009-04-18 10:43:32 +08003658 if (ret != 0) {
3659 mutex_unlock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08003660 return ret;
Wu Fengguangd816f6a2009-04-18 10:43:32 +08003661 }
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08003662
Chris Wilson69dc4982010-10-19 10:36:51 +01003663 BUG_ON(!list_empty(&dev_priv->mm.active_list));
Eric Anholt673a3942008-07-30 12:06:12 -07003664 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
3665 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003666 for (i = 0; i < I915_NUM_RINGS; i++) {
3667 BUG_ON(!list_empty(&dev_priv->ring[i].active_list));
3668 BUG_ON(!list_empty(&dev_priv->ring[i].request_list));
3669 }
Eric Anholt673a3942008-07-30 12:06:12 -07003670 mutex_unlock(&dev->struct_mutex);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04003671
Chris Wilson5f353082010-06-07 14:03:03 +01003672 ret = drm_irq_install(dev);
3673 if (ret)
3674 goto cleanup_ringbuffer;
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04003675
Eric Anholt673a3942008-07-30 12:06:12 -07003676 return 0;
Chris Wilson5f353082010-06-07 14:03:03 +01003677
3678cleanup_ringbuffer:
3679 mutex_lock(&dev->struct_mutex);
3680 i915_gem_cleanup_ringbuffer(dev);
3681 dev_priv->mm.suspended = 1;
3682 mutex_unlock(&dev->struct_mutex);
3683
3684 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003685}
3686
3687int
3688i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
3689 struct drm_file *file_priv)
3690{
Jesse Barnes79e53942008-11-07 14:24:08 -08003691 if (drm_core_check_feature(dev, DRIVER_MODESET))
3692 return 0;
3693
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04003694 drm_irq_uninstall(dev);
Linus Torvaldse6890f62009-09-08 17:09:24 -07003695 return i915_gem_idle(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07003696}
3697
3698void
3699i915_gem_lastclose(struct drm_device *dev)
3700{
3701 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003702
Eric Anholte806b492009-01-22 09:56:58 -08003703 if (drm_core_check_feature(dev, DRIVER_MODESET))
3704 return;
3705
Keith Packard6dbe2772008-10-14 21:41:13 -07003706 ret = i915_gem_idle(dev);
3707 if (ret)
3708 DRM_ERROR("failed to idle hardware: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07003709}
3710
Chris Wilson64193402010-10-24 12:38:05 +01003711static void
3712init_ring_lists(struct intel_ring_buffer *ring)
3713{
3714 INIT_LIST_HEAD(&ring->active_list);
3715 INIT_LIST_HEAD(&ring->request_list);
3716 INIT_LIST_HEAD(&ring->gpu_write_list);
3717}
3718
Eric Anholt673a3942008-07-30 12:06:12 -07003719void
3720i915_gem_load(struct drm_device *dev)
3721{
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02003722 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07003723 drm_i915_private_t *dev_priv = dev->dev_private;
3724
Chris Wilson69dc4982010-10-19 10:36:51 +01003725 INIT_LIST_HEAD(&dev_priv->mm.active_list);
Eric Anholt673a3942008-07-30 12:06:12 -07003726 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
3727 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
Chris Wilsonf13d3f72010-09-20 17:36:15 +01003728 INIT_LIST_HEAD(&dev_priv->mm.pinned_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07003729 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Chris Wilsonbe726152010-07-23 23:18:50 +01003730 INIT_LIST_HEAD(&dev_priv->mm.deferred_free_list);
Daniel Vetter93a37f22010-11-05 20:24:53 +01003731 INIT_LIST_HEAD(&dev_priv->mm.gtt_list);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003732 for (i = 0; i < I915_NUM_RINGS; i++)
3733 init_ring_lists(&dev_priv->ring[i]);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02003734 for (i = 0; i < 16; i++)
3735 INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
Eric Anholt673a3942008-07-30 12:06:12 -07003736 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
3737 i915_gem_retire_work_handler);
Chris Wilson30dbf0c2010-09-25 10:19:17 +01003738 init_completion(&dev_priv->error_completion);
Chris Wilson31169712009-09-14 16:50:28 +01003739
Dave Airlie94400122010-07-20 13:15:31 +10003740 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
3741 if (IS_GEN3(dev)) {
3742 u32 tmp = I915_READ(MI_ARB_STATE);
3743 if (!(tmp & MI_ARB_C3_LP_WRITE_ENABLE)) {
3744 /* arb state is a masked write, so set bit + bit in mask */
3745 tmp = MI_ARB_C3_LP_WRITE_ENABLE | (MI_ARB_C3_LP_WRITE_ENABLE << MI_ARB_MASK_SHIFT);
3746 I915_WRITE(MI_ARB_STATE, tmp);
3747 }
3748 }
3749
Chris Wilson72bfa192010-12-19 11:42:05 +00003750 dev_priv->relative_constants_mode = I915_EXEC_CONSTANTS_REL_GENERAL;
3751
Jesse Barnesde151cf2008-11-12 10:03:55 -08003752 /* Old X drivers will take 0-2 for front, back, depth buffers */
Eric Anholtb397c832010-01-26 09:43:10 -08003753 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3754 dev_priv->fence_reg_start = 3;
Jesse Barnesde151cf2008-11-12 10:03:55 -08003755
Chris Wilsona6c45cf2010-09-17 00:32:17 +01003756 if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08003757 dev_priv->num_fence_regs = 16;
3758 else
3759 dev_priv->num_fence_regs = 8;
3760
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02003761 /* Initialize fence registers to zero */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01003762 switch (INTEL_INFO(dev)->gen) {
3763 case 6:
3764 for (i = 0; i < 16; i++)
3765 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (i * 8), 0);
3766 break;
3767 case 5:
3768 case 4:
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02003769 for (i = 0; i < 16; i++)
3770 I915_WRITE64(FENCE_REG_965_0 + (i * 8), 0);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01003771 break;
3772 case 3:
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02003773 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
3774 for (i = 0; i < 8; i++)
3775 I915_WRITE(FENCE_REG_945_8 + (i * 4), 0);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01003776 case 2:
3777 for (i = 0; i < 8; i++)
3778 I915_WRITE(FENCE_REG_830_0 + (i * 4), 0);
3779 break;
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02003780 }
Eric Anholt673a3942008-07-30 12:06:12 -07003781 i915_gem_detect_bit_6_swizzle(dev);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003782 init_waitqueue_head(&dev_priv->pending_flip_queue);
Chris Wilson17250b72010-10-28 12:51:39 +01003783
3784 dev_priv->mm.inactive_shrinker.shrink = i915_gem_inactive_shrink;
3785 dev_priv->mm.inactive_shrinker.seeks = DEFAULT_SEEKS;
3786 register_shrinker(&dev_priv->mm.inactive_shrinker);
Eric Anholt673a3942008-07-30 12:06:12 -07003787}
Dave Airlie71acb5e2008-12-30 20:31:46 +10003788
3789/*
3790 * Create a physically contiguous memory object for this object
3791 * e.g. for cursor + overlay regs
3792 */
Chris Wilson995b6762010-08-20 13:23:26 +01003793static int i915_gem_init_phys_object(struct drm_device *dev,
3794 int id, int size, int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10003795{
3796 drm_i915_private_t *dev_priv = dev->dev_private;
3797 struct drm_i915_gem_phys_object *phys_obj;
3798 int ret;
3799
3800 if (dev_priv->mm.phys_objs[id - 1] || !size)
3801 return 0;
3802
Eric Anholt9a298b22009-03-24 12:23:04 -07003803 phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
Dave Airlie71acb5e2008-12-30 20:31:46 +10003804 if (!phys_obj)
3805 return -ENOMEM;
3806
3807 phys_obj->id = id;
3808
Chris Wilson6eeefaf2010-08-07 11:01:39 +01003809 phys_obj->handle = drm_pci_alloc(dev, size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10003810 if (!phys_obj->handle) {
3811 ret = -ENOMEM;
3812 goto kfree_obj;
3813 }
3814#ifdef CONFIG_X86
3815 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
3816#endif
3817
3818 dev_priv->mm.phys_objs[id - 1] = phys_obj;
3819
3820 return 0;
3821kfree_obj:
Eric Anholt9a298b22009-03-24 12:23:04 -07003822 kfree(phys_obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10003823 return ret;
3824}
3825
Chris Wilson995b6762010-08-20 13:23:26 +01003826static void i915_gem_free_phys_object(struct drm_device *dev, int id)
Dave Airlie71acb5e2008-12-30 20:31:46 +10003827{
3828 drm_i915_private_t *dev_priv = dev->dev_private;
3829 struct drm_i915_gem_phys_object *phys_obj;
3830
3831 if (!dev_priv->mm.phys_objs[id - 1])
3832 return;
3833
3834 phys_obj = dev_priv->mm.phys_objs[id - 1];
3835 if (phys_obj->cur_obj) {
3836 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
3837 }
3838
3839#ifdef CONFIG_X86
3840 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
3841#endif
3842 drm_pci_free(dev, phys_obj->handle);
3843 kfree(phys_obj);
3844 dev_priv->mm.phys_objs[id - 1] = NULL;
3845}
3846
3847void i915_gem_free_all_phys_object(struct drm_device *dev)
3848{
3849 int i;
3850
Dave Airlie260883c2009-01-22 17:58:49 +10003851 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
Dave Airlie71acb5e2008-12-30 20:31:46 +10003852 i915_gem_free_phys_object(dev, i);
3853}
3854
3855void i915_gem_detach_phys_object(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00003856 struct drm_i915_gem_object *obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10003857{
Chris Wilson05394f32010-11-08 19:18:58 +00003858 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Chris Wilsone5281cc2010-10-28 13:45:36 +01003859 char *vaddr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10003860 int i;
Dave Airlie71acb5e2008-12-30 20:31:46 +10003861 int page_count;
3862
Chris Wilson05394f32010-11-08 19:18:58 +00003863 if (!obj->phys_obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10003864 return;
Chris Wilson05394f32010-11-08 19:18:58 +00003865 vaddr = obj->phys_obj->handle->vaddr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10003866
Chris Wilson05394f32010-11-08 19:18:58 +00003867 page_count = obj->base.size / PAGE_SIZE;
Dave Airlie71acb5e2008-12-30 20:31:46 +10003868 for (i = 0; i < page_count; i++) {
Chris Wilsone5281cc2010-10-28 13:45:36 +01003869 struct page *page = read_cache_page_gfp(mapping, i,
3870 GFP_HIGHUSER | __GFP_RECLAIMABLE);
3871 if (!IS_ERR(page)) {
3872 char *dst = kmap_atomic(page);
3873 memcpy(dst, vaddr + i*PAGE_SIZE, PAGE_SIZE);
3874 kunmap_atomic(dst);
Dave Airlie71acb5e2008-12-30 20:31:46 +10003875
Chris Wilsone5281cc2010-10-28 13:45:36 +01003876 drm_clflush_pages(&page, 1);
3877
3878 set_page_dirty(page);
3879 mark_page_accessed(page);
3880 page_cache_release(page);
3881 }
Dave Airlie71acb5e2008-12-30 20:31:46 +10003882 }
Daniel Vetter40ce6572010-11-05 18:12:18 +01003883 intel_gtt_chipset_flush();
Chris Wilsond78b47b2009-06-17 21:52:49 +01003884
Chris Wilson05394f32010-11-08 19:18:58 +00003885 obj->phys_obj->cur_obj = NULL;
3886 obj->phys_obj = NULL;
Dave Airlie71acb5e2008-12-30 20:31:46 +10003887}
3888
3889int
3890i915_gem_attach_phys_object(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00003891 struct drm_i915_gem_object *obj,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01003892 int id,
3893 int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10003894{
Chris Wilson05394f32010-11-08 19:18:58 +00003895 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Dave Airlie71acb5e2008-12-30 20:31:46 +10003896 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie71acb5e2008-12-30 20:31:46 +10003897 int ret = 0;
3898 int page_count;
3899 int i;
3900
3901 if (id > I915_MAX_PHYS_OBJECT)
3902 return -EINVAL;
3903
Chris Wilson05394f32010-11-08 19:18:58 +00003904 if (obj->phys_obj) {
3905 if (obj->phys_obj->id == id)
Dave Airlie71acb5e2008-12-30 20:31:46 +10003906 return 0;
3907 i915_gem_detach_phys_object(dev, obj);
3908 }
3909
Dave Airlie71acb5e2008-12-30 20:31:46 +10003910 /* create a new object */
3911 if (!dev_priv->mm.phys_objs[id - 1]) {
3912 ret = i915_gem_init_phys_object(dev, id,
Chris Wilson05394f32010-11-08 19:18:58 +00003913 obj->base.size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10003914 if (ret) {
Chris Wilson05394f32010-11-08 19:18:58 +00003915 DRM_ERROR("failed to init phys object %d size: %zu\n",
3916 id, obj->base.size);
Chris Wilsone5281cc2010-10-28 13:45:36 +01003917 return ret;
Dave Airlie71acb5e2008-12-30 20:31:46 +10003918 }
3919 }
3920
3921 /* bind to the object */
Chris Wilson05394f32010-11-08 19:18:58 +00003922 obj->phys_obj = dev_priv->mm.phys_objs[id - 1];
3923 obj->phys_obj->cur_obj = obj;
Dave Airlie71acb5e2008-12-30 20:31:46 +10003924
Chris Wilson05394f32010-11-08 19:18:58 +00003925 page_count = obj->base.size / PAGE_SIZE;
Dave Airlie71acb5e2008-12-30 20:31:46 +10003926
3927 for (i = 0; i < page_count; i++) {
Chris Wilsone5281cc2010-10-28 13:45:36 +01003928 struct page *page;
3929 char *dst, *src;
Dave Airlie71acb5e2008-12-30 20:31:46 +10003930
Chris Wilsone5281cc2010-10-28 13:45:36 +01003931 page = read_cache_page_gfp(mapping, i,
3932 GFP_HIGHUSER | __GFP_RECLAIMABLE);
3933 if (IS_ERR(page))
3934 return PTR_ERR(page);
3935
Chris Wilsonff75b9b2010-10-30 22:52:31 +01003936 src = kmap_atomic(page);
Chris Wilson05394f32010-11-08 19:18:58 +00003937 dst = obj->phys_obj->handle->vaddr + (i * PAGE_SIZE);
Dave Airlie71acb5e2008-12-30 20:31:46 +10003938 memcpy(dst, src, PAGE_SIZE);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -07003939 kunmap_atomic(src);
Chris Wilsone5281cc2010-10-28 13:45:36 +01003940
3941 mark_page_accessed(page);
3942 page_cache_release(page);
Dave Airlie71acb5e2008-12-30 20:31:46 +10003943 }
3944
3945 return 0;
Dave Airlie71acb5e2008-12-30 20:31:46 +10003946}
3947
3948static int
Chris Wilson05394f32010-11-08 19:18:58 +00003949i915_gem_phys_pwrite(struct drm_device *dev,
3950 struct drm_i915_gem_object *obj,
Dave Airlie71acb5e2008-12-30 20:31:46 +10003951 struct drm_i915_gem_pwrite *args,
3952 struct drm_file *file_priv)
3953{
Chris Wilson05394f32010-11-08 19:18:58 +00003954 void *vaddr = obj->phys_obj->handle->vaddr + args->offset;
Chris Wilsonb47b30c2010-11-08 01:12:29 +00003955 char __user *user_data = (char __user *) (uintptr_t) args->data_ptr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10003956
Chris Wilsonb47b30c2010-11-08 01:12:29 +00003957 if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) {
3958 unsigned long unwritten;
3959
3960 /* The physical object once assigned is fixed for the lifetime
3961 * of the obj, so we can safely drop the lock and continue
3962 * to access vaddr.
3963 */
3964 mutex_unlock(&dev->struct_mutex);
3965 unwritten = copy_from_user(vaddr, user_data, args->size);
3966 mutex_lock(&dev->struct_mutex);
3967 if (unwritten)
3968 return -EFAULT;
3969 }
Dave Airlie71acb5e2008-12-30 20:31:46 +10003970
Daniel Vetter40ce6572010-11-05 18:12:18 +01003971 intel_gtt_chipset_flush();
Dave Airlie71acb5e2008-12-30 20:31:46 +10003972 return 0;
3973}
Eric Anholtb9624422009-06-03 07:27:35 +00003974
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003975void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00003976{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003977 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00003978
3979 /* Clean up our request list when the client is going away, so that
3980 * later retire_requests won't dereference our soon-to-be-gone
3981 * file_priv.
3982 */
Chris Wilson1c255952010-09-26 11:03:27 +01003983 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003984 while (!list_empty(&file_priv->mm.request_list)) {
3985 struct drm_i915_gem_request *request;
3986
3987 request = list_first_entry(&file_priv->mm.request_list,
3988 struct drm_i915_gem_request,
3989 client_list);
3990 list_del(&request->client_list);
3991 request->file_priv = NULL;
3992 }
Chris Wilson1c255952010-09-26 11:03:27 +01003993 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00003994}
Chris Wilson31169712009-09-14 16:50:28 +01003995
Chris Wilson31169712009-09-14 16:50:28 +01003996static int
Chris Wilson1637ef42010-04-20 17:10:35 +01003997i915_gpu_is_active(struct drm_device *dev)
3998{
3999 drm_i915_private_t *dev_priv = dev->dev_private;
4000 int lists_empty;
4001
Chris Wilson1637ef42010-04-20 17:10:35 +01004002 lists_empty = list_empty(&dev_priv->mm.flushing_list) &&
Chris Wilson17250b72010-10-28 12:51:39 +01004003 list_empty(&dev_priv->mm.active_list);
Chris Wilson1637ef42010-04-20 17:10:35 +01004004
4005 return !lists_empty;
4006}
4007
4008static int
Chris Wilson17250b72010-10-28 12:51:39 +01004009i915_gem_inactive_shrink(struct shrinker *shrinker,
4010 int nr_to_scan,
4011 gfp_t gfp_mask)
Chris Wilson31169712009-09-14 16:50:28 +01004012{
Chris Wilson17250b72010-10-28 12:51:39 +01004013 struct drm_i915_private *dev_priv =
4014 container_of(shrinker,
4015 struct drm_i915_private,
4016 mm.inactive_shrinker);
4017 struct drm_device *dev = dev_priv->dev;
4018 struct drm_i915_gem_object *obj, *next;
4019 int cnt;
4020
4021 if (!mutex_trylock(&dev->struct_mutex))
Chris Wilsonbbe2e112010-10-28 22:35:07 +01004022 return 0;
Chris Wilson31169712009-09-14 16:50:28 +01004023
4024 /* "fast-path" to count number of available objects */
4025 if (nr_to_scan == 0) {
Chris Wilson17250b72010-10-28 12:51:39 +01004026 cnt = 0;
4027 list_for_each_entry(obj,
4028 &dev_priv->mm.inactive_list,
4029 mm_list)
4030 cnt++;
4031 mutex_unlock(&dev->struct_mutex);
4032 return cnt / 100 * sysctl_vfs_cache_pressure;
Chris Wilson31169712009-09-14 16:50:28 +01004033 }
4034
Chris Wilson1637ef42010-04-20 17:10:35 +01004035rescan:
Chris Wilson31169712009-09-14 16:50:28 +01004036 /* first scan for clean buffers */
Chris Wilson17250b72010-10-28 12:51:39 +01004037 i915_gem_retire_requests(dev);
Chris Wilson31169712009-09-14 16:50:28 +01004038
Chris Wilson17250b72010-10-28 12:51:39 +01004039 list_for_each_entry_safe(obj, next,
4040 &dev_priv->mm.inactive_list,
4041 mm_list) {
4042 if (i915_gem_object_is_purgeable(obj)) {
Chris Wilson20217462010-11-23 15:26:33 +00004043 if (i915_gem_object_unbind(obj) == 0 &&
4044 --nr_to_scan == 0)
Chris Wilson17250b72010-10-28 12:51:39 +01004045 break;
Chris Wilson31169712009-09-14 16:50:28 +01004046 }
Chris Wilson31169712009-09-14 16:50:28 +01004047 }
4048
4049 /* second pass, evict/count anything still on the inactive list */
Chris Wilson17250b72010-10-28 12:51:39 +01004050 cnt = 0;
4051 list_for_each_entry_safe(obj, next,
4052 &dev_priv->mm.inactive_list,
4053 mm_list) {
Chris Wilson20217462010-11-23 15:26:33 +00004054 if (nr_to_scan &&
4055 i915_gem_object_unbind(obj) == 0)
Chris Wilson17250b72010-10-28 12:51:39 +01004056 nr_to_scan--;
Chris Wilson20217462010-11-23 15:26:33 +00004057 else
Chris Wilson17250b72010-10-28 12:51:39 +01004058 cnt++;
Chris Wilson31169712009-09-14 16:50:28 +01004059 }
4060
Chris Wilson17250b72010-10-28 12:51:39 +01004061 if (nr_to_scan && i915_gpu_is_active(dev)) {
Chris Wilson1637ef42010-04-20 17:10:35 +01004062 /*
4063 * We are desperate for pages, so as a last resort, wait
4064 * for the GPU to finish and discard whatever we can.
4065 * This has a dramatic impact to reduce the number of
4066 * OOM-killer events whilst running the GPU aggressively.
4067 */
Chris Wilson17250b72010-10-28 12:51:39 +01004068 if (i915_gpu_idle(dev) == 0)
Chris Wilson1637ef42010-04-20 17:10:35 +01004069 goto rescan;
4070 }
Chris Wilson17250b72010-10-28 12:51:39 +01004071 mutex_unlock(&dev->struct_mutex);
4072 return cnt / 100 * sysctl_vfs_cache_pressure;
Chris Wilson31169712009-09-14 16:50:28 +01004073}