blob: b30c6c167048907f18a104419934399a2ac3123f [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 Wilson05394f32010-11-08 19:18:58 +000038static int i915_gem_object_flush_gpu_write_domain(struct drm_i915_gem_object *obj,
Chris Wilson919926a2010-11-12 13:42:53 +000039 struct intel_ring_buffer *pipelined);
Chris Wilson05394f32010-11-08 19:18:58 +000040static void i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj);
41static void i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj);
42static int i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj,
Chris Wilson919926a2010-11-12 13:42:53 +000043 bool write);
Chris Wilson05394f32010-11-08 19:18:58 +000044static int i915_gem_object_set_cpu_read_domain_range(struct drm_i915_gem_object *obj,
Eric Anholte47c68e2008-11-14 13:35:19 -080045 uint64_t offset,
46 uint64_t size);
Chris Wilson05394f32010-11-08 19:18:58 +000047static void i915_gem_object_set_to_full_cpu_read_domain(struct drm_i915_gem_object *obj);
Chris Wilson05394f32010-11-08 19:18:58 +000048static int i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
Chris Wilsona00b10c2010-09-24 21:15:47 +010049 unsigned alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +010050 bool map_and_fenceable);
Chris Wilson05394f32010-11-08 19:18:58 +000051static void i915_gem_clear_fence_reg(struct drm_i915_gem_object *obj);
52static 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
Eric Anholt3de09aa2009-03-09 09:42:23 -0700688 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
689 if (ret)
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100690 goto out_unpin_pages;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700691
Chris Wilson05394f32010-11-08 19:18:58 +0000692 offset = obj->gtt_offset + args->offset;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700693
694 while (remain > 0) {
695 /* Operation in this page
696 *
697 * gtt_page_base = page offset within aperture
698 * gtt_page_offset = offset within page in aperture
699 * data_page_index = page number in get_user_pages return
700 * data_page_offset = offset with data_page_index page.
701 * page_length = bytes to copy for this page
702 */
703 gtt_page_base = offset & PAGE_MASK;
704 gtt_page_offset = offset & ~PAGE_MASK;
705 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
706 data_page_offset = data_ptr & ~PAGE_MASK;
707
708 page_length = remain;
709 if ((gtt_page_offset + page_length) > PAGE_SIZE)
710 page_length = PAGE_SIZE - gtt_page_offset;
711 if ((data_page_offset + page_length) > PAGE_SIZE)
712 page_length = PAGE_SIZE - data_page_offset;
713
Chris Wilsonab34c222010-05-27 14:15:35 +0100714 slow_kernel_write(dev_priv->mm.gtt_mapping,
715 gtt_page_base, gtt_page_offset,
716 user_pages[data_page_index],
717 data_page_offset,
718 page_length);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700719
720 remain -= page_length;
721 offset += page_length;
722 data_ptr += page_length;
723 }
724
Eric Anholt3de09aa2009-03-09 09:42:23 -0700725out_unpin_pages:
726 for (i = 0; i < pinned_pages; i++)
727 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700728 drm_free_large(user_pages);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700729
730 return ret;
731}
732
Eric Anholt40123c12009-03-09 13:42:30 -0700733/**
734 * This is the fast shmem pwrite path, which attempts to directly
735 * copy_from_user into the kmapped pages backing the object.
736 */
Eric Anholt673a3942008-07-30 12:06:12 -0700737static int
Chris Wilson05394f32010-11-08 19:18:58 +0000738i915_gem_shmem_pwrite_fast(struct drm_device *dev,
739 struct drm_i915_gem_object *obj,
Eric Anholt40123c12009-03-09 13:42:30 -0700740 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000741 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700742{
Chris Wilson05394f32010-11-08 19:18:58 +0000743 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Eric Anholt40123c12009-03-09 13:42:30 -0700744 ssize_t remain;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100745 loff_t offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700746 char __user *user_data;
747 int page_offset, page_length;
Eric Anholt40123c12009-03-09 13:42:30 -0700748
749 user_data = (char __user *) (uintptr_t) args->data_ptr;
750 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700751
Eric Anholt673a3942008-07-30 12:06:12 -0700752 offset = args->offset;
Chris Wilson05394f32010-11-08 19:18:58 +0000753 obj->dirty = 1;
Eric Anholt673a3942008-07-30 12:06:12 -0700754
Eric Anholt40123c12009-03-09 13:42:30 -0700755 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100756 struct page *page;
757 char *vaddr;
758 int ret;
759
Eric Anholt40123c12009-03-09 13:42:30 -0700760 /* Operation in this page
761 *
Eric Anholt40123c12009-03-09 13:42:30 -0700762 * page_offset = offset within page
763 * page_length = bytes to copy for this page
764 */
Eric Anholt40123c12009-03-09 13:42:30 -0700765 page_offset = offset & (PAGE_SIZE-1);
766 page_length = remain;
767 if ((page_offset + remain) > PAGE_SIZE)
768 page_length = PAGE_SIZE - page_offset;
769
Chris Wilsone5281cc2010-10-28 13:45:36 +0100770 page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
771 GFP_HIGHUSER | __GFP_RECLAIMABLE);
772 if (IS_ERR(page))
773 return PTR_ERR(page);
774
775 vaddr = kmap_atomic(page, KM_USER0);
776 ret = __copy_from_user_inatomic(vaddr + page_offset,
777 user_data,
778 page_length);
779 kunmap_atomic(vaddr, KM_USER0);
780
781 set_page_dirty(page);
782 mark_page_accessed(page);
783 page_cache_release(page);
784
785 /* If we get a fault while copying data, then (presumably) our
786 * source page isn't available. Return the error and we'll
787 * retry in the slow path.
788 */
789 if (ret)
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100790 return -EFAULT;
Eric Anholt40123c12009-03-09 13:42:30 -0700791
792 remain -= page_length;
793 user_data += page_length;
794 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700795 }
796
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100797 return 0;
Eric Anholt40123c12009-03-09 13:42:30 -0700798}
799
800/**
801 * This is the fallback shmem pwrite path, which uses get_user_pages to pin
802 * the memory and maps it using kmap_atomic for copying.
803 *
804 * This avoids taking mmap_sem for faulting on the user's address while the
805 * struct_mutex is held.
806 */
807static int
Chris Wilson05394f32010-11-08 19:18:58 +0000808i915_gem_shmem_pwrite_slow(struct drm_device *dev,
809 struct drm_i915_gem_object *obj,
Eric Anholt40123c12009-03-09 13:42:30 -0700810 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000811 struct drm_file *file)
Eric Anholt40123c12009-03-09 13:42:30 -0700812{
Chris Wilson05394f32010-11-08 19:18:58 +0000813 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Eric Anholt40123c12009-03-09 13:42:30 -0700814 struct mm_struct *mm = current->mm;
815 struct page **user_pages;
816 ssize_t remain;
817 loff_t offset, pinned_pages, i;
818 loff_t first_data_page, last_data_page, num_pages;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100819 int shmem_page_offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700820 int data_page_index, data_page_offset;
821 int page_length;
822 int ret;
823 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700824 int do_bit17_swizzling;
Eric Anholt40123c12009-03-09 13:42:30 -0700825
826 remain = args->size;
827
828 /* Pin the user pages containing the data. We can't fault while
829 * holding the struct mutex, and all of the pwrite implementations
830 * want to hold it while dereferencing the user data.
831 */
832 first_data_page = data_ptr / PAGE_SIZE;
833 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
834 num_pages = last_data_page - first_data_page + 1;
835
Chris Wilson4f27b752010-10-14 15:26:45 +0100836 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
Eric Anholt40123c12009-03-09 13:42:30 -0700837 if (user_pages == NULL)
838 return -ENOMEM;
839
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100840 mutex_unlock(&dev->struct_mutex);
Eric Anholt40123c12009-03-09 13:42:30 -0700841 down_read(&mm->mmap_sem);
842 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
843 num_pages, 0, 0, user_pages, NULL);
844 up_read(&mm->mmap_sem);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100845 mutex_lock(&dev->struct_mutex);
Eric Anholt40123c12009-03-09 13:42:30 -0700846 if (pinned_pages < num_pages) {
847 ret = -EFAULT;
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100848 goto out;
Eric Anholt40123c12009-03-09 13:42:30 -0700849 }
850
Eric Anholt40123c12009-03-09 13:42:30 -0700851 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100852 if (ret)
853 goto out;
854
855 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700856
Eric Anholt40123c12009-03-09 13:42:30 -0700857 offset = args->offset;
Chris Wilson05394f32010-11-08 19:18:58 +0000858 obj->dirty = 1;
Eric Anholt40123c12009-03-09 13:42:30 -0700859
860 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100861 struct page *page;
862
Eric Anholt40123c12009-03-09 13:42:30 -0700863 /* Operation in this page
864 *
Eric Anholt40123c12009-03-09 13:42:30 -0700865 * shmem_page_offset = offset within page in shmem file
866 * data_page_index = page number in get_user_pages return
867 * data_page_offset = offset with data_page_index page.
868 * page_length = bytes to copy for this page
869 */
Eric Anholt40123c12009-03-09 13:42:30 -0700870 shmem_page_offset = offset & ~PAGE_MASK;
871 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
872 data_page_offset = data_ptr & ~PAGE_MASK;
873
874 page_length = remain;
875 if ((shmem_page_offset + page_length) > PAGE_SIZE)
876 page_length = PAGE_SIZE - shmem_page_offset;
877 if ((data_page_offset + page_length) > PAGE_SIZE)
878 page_length = PAGE_SIZE - data_page_offset;
879
Chris Wilsone5281cc2010-10-28 13:45:36 +0100880 page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
881 GFP_HIGHUSER | __GFP_RECLAIMABLE);
882 if (IS_ERR(page)) {
883 ret = PTR_ERR(page);
884 goto out;
885 }
886
Eric Anholt280b7132009-03-12 16:56:27 -0700887 if (do_bit17_swizzling) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100888 slow_shmem_bit17_copy(page,
Eric Anholt280b7132009-03-12 16:56:27 -0700889 shmem_page_offset,
890 user_pages[data_page_index],
891 data_page_offset,
Chris Wilson99a03df2010-05-27 14:15:34 +0100892 page_length,
893 0);
894 } else {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100895 slow_shmem_copy(page,
Chris Wilson99a03df2010-05-27 14:15:34 +0100896 shmem_page_offset,
897 user_pages[data_page_index],
898 data_page_offset,
899 page_length);
Eric Anholt280b7132009-03-12 16:56:27 -0700900 }
Eric Anholt40123c12009-03-09 13:42:30 -0700901
Chris Wilsone5281cc2010-10-28 13:45:36 +0100902 set_page_dirty(page);
903 mark_page_accessed(page);
904 page_cache_release(page);
905
Eric Anholt40123c12009-03-09 13:42:30 -0700906 remain -= page_length;
907 data_ptr += page_length;
908 offset += page_length;
909 }
910
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100911out:
Eric Anholt40123c12009-03-09 13:42:30 -0700912 for (i = 0; i < pinned_pages; i++)
913 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700914 drm_free_large(user_pages);
Eric Anholt40123c12009-03-09 13:42:30 -0700915
916 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700917}
918
919/**
920 * Writes data to the object referenced by handle.
921 *
922 * On error, the contents of the buffer that were to be modified are undefined.
923 */
924int
925i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100926 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700927{
928 struct drm_i915_gem_pwrite *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +0000929 struct drm_i915_gem_object *obj;
Chris Wilson51311d02010-11-17 09:10:42 +0000930 int ret;
931
932 if (args->size == 0)
933 return 0;
934
935 if (!access_ok(VERIFY_READ,
936 (char __user *)(uintptr_t)args->data_ptr,
937 args->size))
938 return -EFAULT;
939
940 ret = fault_in_pages_readable((char __user *)(uintptr_t)args->data_ptr,
941 args->size);
942 if (ret)
943 return -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -0700944
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100945 ret = i915_mutex_lock_interruptible(dev);
946 if (ret)
947 return ret;
948
Chris Wilson05394f32010-11-08 19:18:58 +0000949 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100950 if (obj == NULL) {
951 ret = -ENOENT;
952 goto unlock;
953 }
Eric Anholt673a3942008-07-30 12:06:12 -0700954
Chris Wilson7dcd2492010-09-26 20:21:44 +0100955 /* Bounds check destination. */
Chris Wilson05394f32010-11-08 19:18:58 +0000956 if (args->offset > obj->base.size ||
957 args->size > obj->base.size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +0100958 ret = -EINVAL;
Chris Wilson35b62a82010-09-26 20:23:38 +0100959 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +0100960 }
961
Eric Anholt673a3942008-07-30 12:06:12 -0700962 /* We can only do the GTT pwrite on untiled buffers, as otherwise
963 * it would end up going through the fenced access, and we'll get
964 * different detiling behavior between reading and writing.
965 * pread/pwrite currently are reading and writing from the CPU
966 * perspective, requiring manual detiling by the client.
967 */
Chris Wilson05394f32010-11-08 19:18:58 +0000968 if (obj->phys_obj)
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100969 ret = i915_gem_phys_pwrite(dev, obj, args, file);
Chris Wilson05394f32010-11-08 19:18:58 +0000970 else if (obj->tiling_mode == I915_TILING_NONE &&
971 obj->gtt_space &&
972 obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
Daniel Vetter75e9e912010-11-04 17:11:09 +0100973 ret = i915_gem_object_pin(obj, 0, true);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100974 if (ret)
975 goto out;
976
977 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
978 if (ret)
979 goto out_unpin;
980
981 ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file);
982 if (ret == -EFAULT)
983 ret = i915_gem_gtt_pwrite_slow(dev, obj, args, file);
984
985out_unpin:
986 i915_gem_object_unpin(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700987 } else {
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100988 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
989 if (ret)
Chris Wilsone5281cc2010-10-28 13:45:36 +0100990 goto out;
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100991
992 ret = -EFAULT;
993 if (!i915_gem_object_needs_bit17_swizzle(obj))
994 ret = i915_gem_shmem_pwrite_fast(dev, obj, args, file);
995 if (ret == -EFAULT)
996 ret = i915_gem_shmem_pwrite_slow(dev, obj, args, file);
Eric Anholt40123c12009-03-09 13:42:30 -0700997 }
Eric Anholt673a3942008-07-30 12:06:12 -0700998
Chris Wilson35b62a82010-09-26 20:23:38 +0100999out:
Chris Wilson05394f32010-11-08 19:18:58 +00001000 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001001unlock:
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001002 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07001003 return ret;
1004}
1005
1006/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001007 * Called when user space prepares to use an object with the CPU, either
1008 * through the mmap ioctl's mapping or a GTT mapping.
Eric Anholt673a3942008-07-30 12:06:12 -07001009 */
1010int
1011i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001012 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001013{
Eric Anholta09ba7f2009-08-29 12:49:51 -07001014 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07001015 struct drm_i915_gem_set_domain *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001016 struct drm_i915_gem_object *obj;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001017 uint32_t read_domains = args->read_domains;
1018 uint32_t write_domain = args->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07001019 int ret;
1020
1021 if (!(dev->driver->driver_features & DRIVER_GEM))
1022 return -ENODEV;
1023
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001024 /* Only handle setting domains to types used by the CPU. */
Chris Wilson21d509e2009-06-06 09:46:02 +01001025 if (write_domain & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001026 return -EINVAL;
1027
Chris Wilson21d509e2009-06-06 09:46:02 +01001028 if (read_domains & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001029 return -EINVAL;
1030
1031 /* Having something in the write domain implies it's in the read
1032 * domain, and only that read domain. Enforce that in the request.
1033 */
1034 if (write_domain != 0 && read_domains != write_domain)
1035 return -EINVAL;
1036
Chris Wilson76c1dec2010-09-25 11:22:51 +01001037 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001038 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001039 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001040
Chris Wilson05394f32010-11-08 19:18:58 +00001041 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001042 if (obj == NULL) {
1043 ret = -ENOENT;
1044 goto unlock;
Chris Wilson76c1dec2010-09-25 11:22:51 +01001045 }
Jesse Barnes652c3932009-08-17 13:31:43 -07001046
1047 intel_mark_busy(dev, obj);
1048
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001049 if (read_domains & I915_GEM_DOMAIN_GTT) {
1050 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
Eric Anholt02354392008-11-26 13:58:13 -08001051
Eric Anholta09ba7f2009-08-29 12:49:51 -07001052 /* Update the LRU on the fence for the CPU access that's
1053 * about to occur.
1054 */
Chris Wilson05394f32010-11-08 19:18:58 +00001055 if (obj->fence_reg != I915_FENCE_REG_NONE) {
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001056 struct drm_i915_fence_reg *reg =
Chris Wilson05394f32010-11-08 19:18:58 +00001057 &dev_priv->fence_regs[obj->fence_reg];
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001058 list_move_tail(&reg->lru_list,
Eric Anholta09ba7f2009-08-29 12:49:51 -07001059 &dev_priv->mm.fence_list);
1060 }
1061
Eric Anholt02354392008-11-26 13:58:13 -08001062 /* Silently promote "you're not bound, there was nothing to do"
1063 * to success, since the client was just asking us to
1064 * make sure everything was done.
1065 */
1066 if (ret == -EINVAL)
1067 ret = 0;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001068 } else {
Eric Anholte47c68e2008-11-14 13:35:19 -08001069 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001070 }
1071
Chris Wilson7d1c4802010-08-07 21:45:03 +01001072 /* Maintain LRU order of "inactive" objects */
Chris Wilson05394f32010-11-08 19:18:58 +00001073 if (ret == 0 && i915_gem_object_is_inactive(obj))
1074 list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
Chris Wilson7d1c4802010-08-07 21:45:03 +01001075
Chris Wilson05394f32010-11-08 19:18:58 +00001076 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001077unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07001078 mutex_unlock(&dev->struct_mutex);
1079 return ret;
1080}
1081
1082/**
1083 * Called when user space has done writes to this buffer
1084 */
1085int
1086i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001087 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001088{
1089 struct drm_i915_gem_sw_finish *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001090 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001091 int ret = 0;
1092
1093 if (!(dev->driver->driver_features & DRIVER_GEM))
1094 return -ENODEV;
1095
Chris Wilson76c1dec2010-09-25 11:22:51 +01001096 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001097 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001098 return ret;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001099
Chris Wilson05394f32010-11-08 19:18:58 +00001100 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Eric Anholt673a3942008-07-30 12:06:12 -07001101 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001102 ret = -ENOENT;
1103 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07001104 }
1105
Eric Anholt673a3942008-07-30 12:06:12 -07001106 /* Pinned buffers may be scanout, so flush the cache */
Chris Wilson05394f32010-11-08 19:18:58 +00001107 if (obj->pin_count)
Eric Anholte47c68e2008-11-14 13:35:19 -08001108 i915_gem_object_flush_cpu_write_domain(obj);
1109
Chris Wilson05394f32010-11-08 19:18:58 +00001110 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001111unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07001112 mutex_unlock(&dev->struct_mutex);
1113 return ret;
1114}
1115
1116/**
1117 * Maps the contents of an object, returning the address it is mapped
1118 * into.
1119 *
1120 * While the mapping holds a reference on the contents of the object, it doesn't
1121 * imply a ref on the object itself.
1122 */
1123int
1124i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001125 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001126{
Chris Wilsonda761a62010-10-27 17:37:08 +01001127 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07001128 struct drm_i915_gem_mmap *args = data;
1129 struct drm_gem_object *obj;
1130 loff_t offset;
1131 unsigned long addr;
1132
1133 if (!(dev->driver->driver_features & DRIVER_GEM))
1134 return -ENODEV;
1135
Chris Wilson05394f32010-11-08 19:18:58 +00001136 obj = drm_gem_object_lookup(dev, file, args->handle);
Eric Anholt673a3942008-07-30 12:06:12 -07001137 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001138 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001139
Chris Wilsonda761a62010-10-27 17:37:08 +01001140 if (obj->size > dev_priv->mm.gtt_mappable_end) {
1141 drm_gem_object_unreference_unlocked(obj);
1142 return -E2BIG;
1143 }
1144
Eric Anholt673a3942008-07-30 12:06:12 -07001145 offset = args->offset;
1146
1147 down_write(&current->mm->mmap_sem);
1148 addr = do_mmap(obj->filp, 0, args->size,
1149 PROT_READ | PROT_WRITE, MAP_SHARED,
1150 args->offset);
1151 up_write(&current->mm->mmap_sem);
Luca Barbieribc9025b2010-02-09 05:49:12 +00001152 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001153 if (IS_ERR((void *)addr))
1154 return addr;
1155
1156 args->addr_ptr = (uint64_t) addr;
1157
1158 return 0;
1159}
1160
Jesse Barnesde151cf2008-11-12 10:03:55 -08001161/**
1162 * i915_gem_fault - fault a page into the GTT
1163 * vma: VMA in question
1164 * vmf: fault info
1165 *
1166 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1167 * from userspace. The fault handler takes care of binding the object to
1168 * the GTT (if needed), allocating and programming a fence register (again,
1169 * only if needed based on whether the old reg is still valid or the object
1170 * is tiled) and inserting a new PTE into the faulting process.
1171 *
1172 * Note that the faulting process may involve evicting existing objects
1173 * from the GTT and/or fence registers to make room. So performance may
1174 * suffer if the GTT working set is large or there are few fence registers
1175 * left.
1176 */
1177int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1178{
Chris Wilson05394f32010-11-08 19:18:58 +00001179 struct drm_i915_gem_object *obj = to_intel_bo(vma->vm_private_data);
1180 struct drm_device *dev = obj->base.dev;
Chris Wilson7d1c4802010-08-07 21:45:03 +01001181 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001182 pgoff_t page_offset;
1183 unsigned long pfn;
1184 int ret = 0;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001185 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001186
1187 /* We don't use vmf->pgoff since that has the fake offset */
1188 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1189 PAGE_SHIFT;
1190
1191 /* Now bind it into the GTT if needed */
1192 mutex_lock(&dev->struct_mutex);
Chris Wilsona00b10c2010-09-24 21:15:47 +01001193
Chris Wilson919926a2010-11-12 13:42:53 +00001194 if (!obj->map_and_fenceable) {
1195 ret = i915_gem_object_unbind(obj);
1196 if (ret)
1197 goto unlock;
Chris Wilsona00b10c2010-09-24 21:15:47 +01001198 }
Chris Wilson05394f32010-11-08 19:18:58 +00001199 if (!obj->gtt_space) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01001200 ret = i915_gem_object_bind_to_gtt(obj, 0, true);
Chris Wilsonc7150892009-09-23 00:43:56 +01001201 if (ret)
1202 goto unlock;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001203 }
1204
Chris Wilson4a684a42010-10-28 14:44:08 +01001205 ret = i915_gem_object_set_to_gtt_domain(obj, write);
1206 if (ret)
1207 goto unlock;
1208
Jesse Barnesde151cf2008-11-12 10:03:55 -08001209 /* Need a new fence register? */
Chris Wilson05394f32010-11-08 19:18:58 +00001210 if (obj->tiling_mode != I915_TILING_NONE) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01001211 ret = i915_gem_object_get_fence_reg(obj, true);
Chris Wilsonc7150892009-09-23 00:43:56 +01001212 if (ret)
1213 goto unlock;
Eric Anholtd9ddcb92009-01-27 10:33:49 -08001214 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001215
Chris Wilson05394f32010-11-08 19:18:58 +00001216 if (i915_gem_object_is_inactive(obj))
1217 list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
Chris Wilson7d1c4802010-08-07 21:45:03 +01001218
Chris Wilson6299f992010-11-24 12:23:44 +00001219 obj->fault_mappable = true;
1220
Chris Wilson05394f32010-11-08 19:18:58 +00001221 pfn = ((dev->agp->base + obj->gtt_offset) >> PAGE_SHIFT) +
Jesse Barnesde151cf2008-11-12 10:03:55 -08001222 page_offset;
1223
1224 /* Finally, remap it using the new GTT offset */
1225 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
Chris Wilsonc7150892009-09-23 00:43:56 +01001226unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001227 mutex_unlock(&dev->struct_mutex);
1228
1229 switch (ret) {
Chris Wilson045e7692010-11-07 09:18:22 +00001230 case -EAGAIN:
1231 set_need_resched();
Chris Wilsonc7150892009-09-23 00:43:56 +01001232 case 0:
1233 case -ERESTARTSYS:
1234 return VM_FAULT_NOPAGE;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001235 case -ENOMEM:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001236 return VM_FAULT_OOM;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001237 default:
Chris Wilsonc7150892009-09-23 00:43:56 +01001238 return VM_FAULT_SIGBUS;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001239 }
1240}
1241
1242/**
1243 * i915_gem_create_mmap_offset - create a fake mmap offset for an object
1244 * @obj: obj in question
1245 *
1246 * GEM memory mapping works by handing back to userspace a fake mmap offset
1247 * it can use in a subsequent mmap(2) call. The DRM core code then looks
1248 * up the object based on the offset and sets up the various memory mapping
1249 * structures.
1250 *
1251 * This routine allocates and attaches a fake offset for @obj.
1252 */
1253static int
Chris Wilson05394f32010-11-08 19:18:58 +00001254i915_gem_create_mmap_offset(struct drm_i915_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001255{
Chris Wilson05394f32010-11-08 19:18:58 +00001256 struct drm_device *dev = obj->base.dev;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001257 struct drm_gem_mm *mm = dev->mm_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001258 struct drm_map_list *list;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +11001259 struct drm_local_map *map;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001260 int ret = 0;
1261
1262 /* Set the object up for mmap'ing */
Chris Wilson05394f32010-11-08 19:18:58 +00001263 list = &obj->base.map_list;
Eric Anholt9a298b22009-03-24 12:23:04 -07001264 list->map = kzalloc(sizeof(struct drm_map_list), GFP_KERNEL);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001265 if (!list->map)
1266 return -ENOMEM;
1267
1268 map = list->map;
1269 map->type = _DRM_GEM;
Chris Wilson05394f32010-11-08 19:18:58 +00001270 map->size = obj->base.size;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001271 map->handle = obj;
1272
1273 /* Get a DRM GEM mmap offset allocated... */
1274 list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
Chris Wilson05394f32010-11-08 19:18:58 +00001275 obj->base.size / PAGE_SIZE,
1276 0, 0);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001277 if (!list->file_offset_node) {
Chris Wilson05394f32010-11-08 19:18:58 +00001278 DRM_ERROR("failed to allocate offset for bo %d\n",
1279 obj->base.name);
Chris Wilson9e0ae5342010-09-21 15:05:24 +01001280 ret = -ENOSPC;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001281 goto out_free_list;
1282 }
1283
1284 list->file_offset_node = drm_mm_get_block(list->file_offset_node,
Chris Wilson05394f32010-11-08 19:18:58 +00001285 obj->base.size / PAGE_SIZE,
1286 0);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001287 if (!list->file_offset_node) {
1288 ret = -ENOMEM;
1289 goto out_free_list;
1290 }
1291
1292 list->hash.key = list->file_offset_node->start;
Chris Wilson9e0ae5342010-09-21 15:05:24 +01001293 ret = drm_ht_insert_item(&mm->offset_hash, &list->hash);
1294 if (ret) {
Jesse Barnesde151cf2008-11-12 10:03:55 -08001295 DRM_ERROR("failed to add to map hash\n");
1296 goto out_free_mm;
1297 }
1298
Jesse Barnesde151cf2008-11-12 10:03:55 -08001299 return 0;
1300
1301out_free_mm:
1302 drm_mm_put_block(list->file_offset_node);
1303out_free_list:
Eric Anholt9a298b22009-03-24 12:23:04 -07001304 kfree(list->map);
Chris Wilson39a01d12010-10-28 13:03:06 +01001305 list->map = NULL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001306
1307 return ret;
1308}
1309
Chris Wilson901782b2009-07-10 08:18:50 +01001310/**
1311 * i915_gem_release_mmap - remove physical page mappings
1312 * @obj: obj in question
1313 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001314 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01001315 * relinquish ownership of the pages back to the system.
1316 *
1317 * It is vital that we remove the page mapping if we have mapped a tiled
1318 * object through the GTT and then lose the fence register due to
1319 * resource pressure. Similarly if the object has been moved out of the
1320 * aperture, than pages mapped into userspace must be revoked. Removing the
1321 * mapping will then trigger a page fault on the next user access, allowing
1322 * fixup by i915_gem_fault().
1323 */
Eric Anholtd05ca302009-07-10 13:02:26 -07001324void
Chris Wilson05394f32010-11-08 19:18:58 +00001325i915_gem_release_mmap(struct drm_i915_gem_object *obj)
Chris Wilson901782b2009-07-10 08:18:50 +01001326{
Chris Wilson6299f992010-11-24 12:23:44 +00001327 if (!obj->fault_mappable)
1328 return;
Chris Wilson901782b2009-07-10 08:18:50 +01001329
Chris Wilson6299f992010-11-24 12:23:44 +00001330 unmap_mapping_range(obj->base.dev->dev_mapping,
1331 (loff_t)obj->base.map_list.hash.key<<PAGE_SHIFT,
1332 obj->base.size, 1);
Daniel Vetterfb7d5162010-10-01 22:05:20 +02001333
Chris Wilson6299f992010-11-24 12:23:44 +00001334 obj->fault_mappable = false;
Chris Wilson901782b2009-07-10 08:18:50 +01001335}
1336
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001337static void
Chris Wilson05394f32010-11-08 19:18:58 +00001338i915_gem_free_mmap_offset(struct drm_i915_gem_object *obj)
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001339{
Chris Wilson05394f32010-11-08 19:18:58 +00001340 struct drm_device *dev = obj->base.dev;
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001341 struct drm_gem_mm *mm = dev->mm_private;
Chris Wilson05394f32010-11-08 19:18:58 +00001342 struct drm_map_list *list = &obj->base.map_list;
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001343
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001344 drm_ht_remove_item(&mm->offset_hash, &list->hash);
Chris Wilson39a01d12010-10-28 13:03:06 +01001345 drm_mm_put_block(list->file_offset_node);
1346 kfree(list->map);
1347 list->map = NULL;
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001348}
1349
Chris Wilson92b88ae2010-11-09 11:47:32 +00001350static uint32_t
1351i915_gem_get_gtt_size(struct drm_i915_gem_object *obj)
1352{
1353 struct drm_device *dev = obj->base.dev;
1354 uint32_t size;
1355
1356 if (INTEL_INFO(dev)->gen >= 4 ||
1357 obj->tiling_mode == I915_TILING_NONE)
1358 return obj->base.size;
1359
1360 /* Previous chips need a power-of-two fence region when tiling */
1361 if (INTEL_INFO(dev)->gen == 3)
1362 size = 1024*1024;
1363 else
1364 size = 512*1024;
1365
1366 while (size < obj->base.size)
1367 size <<= 1;
1368
1369 return size;
1370}
1371
Jesse Barnesde151cf2008-11-12 10:03:55 -08001372/**
1373 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1374 * @obj: object to check
1375 *
1376 * Return the required GTT alignment for an object, taking into account
Daniel Vetter5e783302010-11-14 22:32:36 +01001377 * potential fence register mapping.
Jesse Barnesde151cf2008-11-12 10:03:55 -08001378 */
1379static uint32_t
Chris Wilson05394f32010-11-08 19:18:58 +00001380i915_gem_get_gtt_alignment(struct drm_i915_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001381{
Chris Wilson05394f32010-11-08 19:18:58 +00001382 struct drm_device *dev = obj->base.dev;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001383
1384 /*
1385 * Minimum alignment is 4k (GTT page size), but might be greater
1386 * if a fence register is needed for the object.
1387 */
Chris Wilsona00b10c2010-09-24 21:15:47 +01001388 if (INTEL_INFO(dev)->gen >= 4 ||
Chris Wilson05394f32010-11-08 19:18:58 +00001389 obj->tiling_mode == I915_TILING_NONE)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001390 return 4096;
1391
1392 /*
1393 * Previous chips need to be aligned to the size of the smallest
1394 * fence register that can contain the object.
1395 */
Chris Wilson05394f32010-11-08 19:18:58 +00001396 return i915_gem_get_gtt_size(obj);
Chris Wilsona00b10c2010-09-24 21:15:47 +01001397}
1398
Daniel Vetter5e783302010-11-14 22:32:36 +01001399/**
1400 * i915_gem_get_unfenced_gtt_alignment - return required GTT alignment for an
1401 * unfenced object
1402 * @obj: object to check
1403 *
1404 * Return the required GTT alignment for an object, only taking into account
1405 * unfenced tiled surface requirements.
1406 */
1407static uint32_t
Chris Wilson05394f32010-11-08 19:18:58 +00001408i915_gem_get_unfenced_gtt_alignment(struct drm_i915_gem_object *obj)
Daniel Vetter5e783302010-11-14 22:32:36 +01001409{
Chris Wilson05394f32010-11-08 19:18:58 +00001410 struct drm_device *dev = obj->base.dev;
Daniel Vetter5e783302010-11-14 22:32:36 +01001411 int tile_height;
1412
1413 /*
1414 * Minimum alignment is 4k (GTT page size) for sane hw.
1415 */
1416 if (INTEL_INFO(dev)->gen >= 4 || IS_G33(dev) ||
Chris Wilson05394f32010-11-08 19:18:58 +00001417 obj->tiling_mode == I915_TILING_NONE)
Daniel Vetter5e783302010-11-14 22:32:36 +01001418 return 4096;
1419
1420 /*
1421 * Older chips need unfenced tiled buffers to be aligned to the left
1422 * edge of an even tile row (where tile rows are counted as if the bo is
1423 * placed in a fenced gtt region).
1424 */
1425 if (IS_GEN2(dev) ||
Chris Wilson05394f32010-11-08 19:18:58 +00001426 (obj->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev)))
Daniel Vetter5e783302010-11-14 22:32:36 +01001427 tile_height = 32;
1428 else
1429 tile_height = 8;
1430
Chris Wilson05394f32010-11-08 19:18:58 +00001431 return tile_height * obj->stride * 2;
Daniel Vetter5e783302010-11-14 22:32:36 +01001432}
1433
Jesse Barnesde151cf2008-11-12 10:03:55 -08001434/**
1435 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1436 * @dev: DRM device
1437 * @data: GTT mapping ioctl data
Chris Wilson05394f32010-11-08 19:18:58 +00001438 * @file: GEM object info
Jesse Barnesde151cf2008-11-12 10:03:55 -08001439 *
1440 * Simply returns the fake offset to userspace so it can mmap it.
1441 * The mmap call will end up in drm_gem_mmap(), which will set things
1442 * up so we can get faults in the handler above.
1443 *
1444 * The fault handler will take care of binding the object into the GTT
1445 * (since it may have been evicted to make room for something), allocating
1446 * a fence register, and mapping the appropriate aperture address into
1447 * userspace.
1448 */
1449int
1450i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001451 struct drm_file *file)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001452{
Chris Wilsonda761a62010-10-27 17:37:08 +01001453 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001454 struct drm_i915_gem_mmap_gtt *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001455 struct drm_i915_gem_object *obj;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001456 int ret;
1457
1458 if (!(dev->driver->driver_features & DRIVER_GEM))
1459 return -ENODEV;
1460
Chris Wilson76c1dec2010-09-25 11:22:51 +01001461 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001462 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001463 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001464
Chris Wilson05394f32010-11-08 19:18:58 +00001465 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001466 if (obj == NULL) {
1467 ret = -ENOENT;
1468 goto unlock;
1469 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001470
Chris Wilson05394f32010-11-08 19:18:58 +00001471 if (obj->base.size > dev_priv->mm.gtt_mappable_end) {
Chris Wilsonda761a62010-10-27 17:37:08 +01001472 ret = -E2BIG;
1473 goto unlock;
1474 }
1475
Chris Wilson05394f32010-11-08 19:18:58 +00001476 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilsonab182822009-09-22 18:46:17 +01001477 DRM_ERROR("Attempting to mmap a purgeable buffer\n");
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001478 ret = -EINVAL;
1479 goto out;
Chris Wilsonab182822009-09-22 18:46:17 +01001480 }
1481
Chris Wilson05394f32010-11-08 19:18:58 +00001482 if (!obj->base.map_list.map) {
Jesse Barnesde151cf2008-11-12 10:03:55 -08001483 ret = i915_gem_create_mmap_offset(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001484 if (ret)
1485 goto out;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001486 }
1487
Chris Wilson05394f32010-11-08 19:18:58 +00001488 args->offset = (u64)obj->base.map_list.hash.key << PAGE_SHIFT;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001489
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001490out:
Chris Wilson05394f32010-11-08 19:18:58 +00001491 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001492unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001493 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001494 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001495}
1496
Chris Wilsone5281cc2010-10-28 13:45:36 +01001497static int
Chris Wilson05394f32010-11-08 19:18:58 +00001498i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj,
Chris Wilsone5281cc2010-10-28 13:45:36 +01001499 gfp_t gfpmask)
1500{
Chris Wilsone5281cc2010-10-28 13:45:36 +01001501 int page_count, i;
1502 struct address_space *mapping;
1503 struct inode *inode;
1504 struct page *page;
1505
1506 /* Get the list of pages out of our struct file. They'll be pinned
1507 * at this point until we release them.
1508 */
Chris Wilson05394f32010-11-08 19:18:58 +00001509 page_count = obj->base.size / PAGE_SIZE;
1510 BUG_ON(obj->pages != NULL);
1511 obj->pages = drm_malloc_ab(page_count, sizeof(struct page *));
1512 if (obj->pages == NULL)
Chris Wilsone5281cc2010-10-28 13:45:36 +01001513 return -ENOMEM;
1514
Chris Wilson05394f32010-11-08 19:18:58 +00001515 inode = obj->base.filp->f_path.dentry->d_inode;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001516 mapping = inode->i_mapping;
1517 for (i = 0; i < page_count; i++) {
1518 page = read_cache_page_gfp(mapping, i,
1519 GFP_HIGHUSER |
1520 __GFP_COLD |
1521 __GFP_RECLAIMABLE |
1522 gfpmask);
1523 if (IS_ERR(page))
1524 goto err_pages;
1525
Chris Wilson05394f32010-11-08 19:18:58 +00001526 obj->pages[i] = page;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001527 }
1528
Chris Wilson05394f32010-11-08 19:18:58 +00001529 if (obj->tiling_mode != I915_TILING_NONE)
Chris Wilsone5281cc2010-10-28 13:45:36 +01001530 i915_gem_object_do_bit_17_swizzle(obj);
1531
1532 return 0;
1533
1534err_pages:
1535 while (i--)
Chris Wilson05394f32010-11-08 19:18:58 +00001536 page_cache_release(obj->pages[i]);
Chris Wilsone5281cc2010-10-28 13:45:36 +01001537
Chris Wilson05394f32010-11-08 19:18:58 +00001538 drm_free_large(obj->pages);
1539 obj->pages = NULL;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001540 return PTR_ERR(page);
1541}
1542
Chris Wilson5cdf5882010-09-27 15:51:07 +01001543static void
Chris Wilson05394f32010-11-08 19:18:58 +00001544i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07001545{
Chris Wilson05394f32010-11-08 19:18:58 +00001546 int page_count = obj->base.size / PAGE_SIZE;
Eric Anholt673a3942008-07-30 12:06:12 -07001547 int i;
1548
Chris Wilson05394f32010-11-08 19:18:58 +00001549 BUG_ON(obj->madv == __I915_MADV_PURGED);
Eric Anholt856fa192009-03-19 14:10:50 -07001550
Chris Wilson05394f32010-11-08 19:18:58 +00001551 if (obj->tiling_mode != I915_TILING_NONE)
Eric Anholt280b7132009-03-12 16:56:27 -07001552 i915_gem_object_save_bit_17_swizzle(obj);
1553
Chris Wilson05394f32010-11-08 19:18:58 +00001554 if (obj->madv == I915_MADV_DONTNEED)
1555 obj->dirty = 0;
Chris Wilson3ef94da2009-09-14 16:50:29 +01001556
1557 for (i = 0; i < page_count; i++) {
Chris Wilson05394f32010-11-08 19:18:58 +00001558 if (obj->dirty)
1559 set_page_dirty(obj->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001560
Chris Wilson05394f32010-11-08 19:18:58 +00001561 if (obj->madv == I915_MADV_WILLNEED)
1562 mark_page_accessed(obj->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001563
Chris Wilson05394f32010-11-08 19:18:58 +00001564 page_cache_release(obj->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001565 }
Chris Wilson05394f32010-11-08 19:18:58 +00001566 obj->dirty = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001567
Chris Wilson05394f32010-11-08 19:18:58 +00001568 drm_free_large(obj->pages);
1569 obj->pages = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001570}
1571
Chris Wilson54cf91d2010-11-25 18:00:26 +00001572void
Chris Wilson05394f32010-11-08 19:18:58 +00001573i915_gem_object_move_to_active(struct drm_i915_gem_object *obj,
Zou Nan hai852835f2010-05-21 09:08:56 +08001574 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001575{
Chris Wilson05394f32010-11-08 19:18:58 +00001576 struct drm_device *dev = obj->base.dev;
Chris Wilson69dc4982010-10-19 10:36:51 +01001577 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona56ba562010-09-28 10:07:56 +01001578 uint32_t seqno = i915_gem_next_request_seqno(dev, ring);
Daniel Vetter617dbe22010-02-11 22:16:02 +01001579
Zou Nan hai852835f2010-05-21 09:08:56 +08001580 BUG_ON(ring == NULL);
Chris Wilson05394f32010-11-08 19:18:58 +00001581 obj->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001582
1583 /* Add a reference if we're newly entering the active list. */
Chris Wilson05394f32010-11-08 19:18:58 +00001584 if (!obj->active) {
1585 drm_gem_object_reference(&obj->base);
1586 obj->active = 1;
Eric Anholt673a3942008-07-30 12:06:12 -07001587 }
Daniel Vettere35a41d2010-02-11 22:13:59 +01001588
Eric Anholt673a3942008-07-30 12:06:12 -07001589 /* Move from whatever list we were on to the tail of execution. */
Chris Wilson05394f32010-11-08 19:18:58 +00001590 list_move_tail(&obj->mm_list, &dev_priv->mm.active_list);
1591 list_move_tail(&obj->ring_list, &ring->active_list);
Chris Wilsoncaea7472010-11-12 13:53:37 +00001592
Chris Wilson05394f32010-11-08 19:18:58 +00001593 obj->last_rendering_seqno = seqno;
Chris Wilsoncaea7472010-11-12 13:53:37 +00001594 if (obj->fenced_gpu_access) {
1595 struct drm_i915_fence_reg *reg;
1596
1597 BUG_ON(obj->fence_reg == I915_FENCE_REG_NONE);
1598
1599 obj->last_fenced_seqno = seqno;
1600 obj->last_fenced_ring = ring;
1601
1602 reg = &dev_priv->fence_regs[obj->fence_reg];
1603 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
1604 }
1605}
1606
1607static void
1608i915_gem_object_move_off_active(struct drm_i915_gem_object *obj)
1609{
1610 list_del_init(&obj->ring_list);
1611 obj->last_rendering_seqno = 0;
1612 obj->last_fenced_seqno = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001613}
1614
Eric Anholtce44b0e2008-11-06 16:00:31 -08001615static void
Chris Wilson05394f32010-11-08 19:18:58 +00001616i915_gem_object_move_to_flushing(struct drm_i915_gem_object *obj)
Eric Anholtce44b0e2008-11-06 16:00:31 -08001617{
Chris Wilson05394f32010-11-08 19:18:58 +00001618 struct drm_device *dev = obj->base.dev;
Eric Anholtce44b0e2008-11-06 16:00:31 -08001619 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtce44b0e2008-11-06 16:00:31 -08001620
Chris Wilson05394f32010-11-08 19:18:58 +00001621 BUG_ON(!obj->active);
1622 list_move_tail(&obj->mm_list, &dev_priv->mm.flushing_list);
Chris Wilsoncaea7472010-11-12 13:53:37 +00001623
1624 i915_gem_object_move_off_active(obj);
1625}
1626
1627static void
1628i915_gem_object_move_to_inactive(struct drm_i915_gem_object *obj)
1629{
1630 struct drm_device *dev = obj->base.dev;
1631 struct drm_i915_private *dev_priv = dev->dev_private;
1632
1633 if (obj->pin_count != 0)
1634 list_move_tail(&obj->mm_list, &dev_priv->mm.pinned_list);
1635 else
1636 list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
1637
1638 BUG_ON(!list_empty(&obj->gpu_write_list));
1639 BUG_ON(!obj->active);
1640 obj->ring = NULL;
1641
1642 i915_gem_object_move_off_active(obj);
1643 obj->fenced_gpu_access = false;
1644 obj->last_fenced_ring = NULL;
1645
1646 obj->active = 0;
1647 drm_gem_object_unreference(&obj->base);
1648
1649 WARN_ON(i915_verify_lists(dev));
Eric Anholtce44b0e2008-11-06 16:00:31 -08001650}
Eric Anholt673a3942008-07-30 12:06:12 -07001651
Chris Wilson963b4832009-09-20 23:03:54 +01001652/* Immediately discard the backing storage */
1653static void
Chris Wilson05394f32010-11-08 19:18:58 +00001654i915_gem_object_truncate(struct drm_i915_gem_object *obj)
Chris Wilson963b4832009-09-20 23:03:54 +01001655{
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001656 struct inode *inode;
Chris Wilson963b4832009-09-20 23:03:54 +01001657
Chris Wilsonae9fed62010-08-07 11:01:30 +01001658 /* Our goal here is to return as much of the memory as
1659 * is possible back to the system as we are called from OOM.
1660 * To do this we must instruct the shmfs to drop all of its
1661 * backing pages, *now*. Here we mirror the actions taken
1662 * when by shmem_delete_inode() to release the backing store.
1663 */
Chris Wilson05394f32010-11-08 19:18:58 +00001664 inode = obj->base.filp->f_path.dentry->d_inode;
Chris Wilsonae9fed62010-08-07 11:01:30 +01001665 truncate_inode_pages(inode->i_mapping, 0);
1666 if (inode->i_op->truncate_range)
1667 inode->i_op->truncate_range(inode, 0, (loff_t)-1);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001668
Chris Wilson05394f32010-11-08 19:18:58 +00001669 obj->madv = __I915_MADV_PURGED;
Chris Wilson963b4832009-09-20 23:03:54 +01001670}
1671
1672static inline int
Chris Wilson05394f32010-11-08 19:18:58 +00001673i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj)
Chris Wilson963b4832009-09-20 23:03:54 +01001674{
Chris Wilson05394f32010-11-08 19:18:58 +00001675 return obj->madv == I915_MADV_DONTNEED;
Chris Wilson963b4832009-09-20 23:03:54 +01001676}
1677
Eric Anholt673a3942008-07-30 12:06:12 -07001678static void
Daniel Vetter63560392010-02-19 11:51:59 +01001679i915_gem_process_flushing_list(struct drm_device *dev,
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001680 uint32_t flush_domains,
Zou Nan hai852835f2010-05-21 09:08:56 +08001681 struct intel_ring_buffer *ring)
Daniel Vetter63560392010-02-19 11:51:59 +01001682{
Chris Wilson05394f32010-11-08 19:18:58 +00001683 struct drm_i915_gem_object *obj, *next;
Daniel Vetter63560392010-02-19 11:51:59 +01001684
Chris Wilson05394f32010-11-08 19:18:58 +00001685 list_for_each_entry_safe(obj, next,
Chris Wilson64193402010-10-24 12:38:05 +01001686 &ring->gpu_write_list,
Daniel Vetter63560392010-02-19 11:51:59 +01001687 gpu_write_list) {
Chris Wilson05394f32010-11-08 19:18:58 +00001688 if (obj->base.write_domain & flush_domains) {
1689 uint32_t old_write_domain = obj->base.write_domain;
Daniel Vetter63560392010-02-19 11:51:59 +01001690
Chris Wilson05394f32010-11-08 19:18:58 +00001691 obj->base.write_domain = 0;
1692 list_del_init(&obj->gpu_write_list);
Daniel Vetter617dbe22010-02-11 22:16:02 +01001693 i915_gem_object_move_to_active(obj, ring);
Daniel Vetter63560392010-02-19 11:51:59 +01001694
Daniel Vetter63560392010-02-19 11:51:59 +01001695 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00001696 obj->base.read_domains,
Daniel Vetter63560392010-02-19 11:51:59 +01001697 old_write_domain);
1698 }
1699 }
1700}
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001701
Chris Wilson3cce4692010-10-27 16:11:02 +01001702int
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001703i915_add_request(struct drm_device *dev,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001704 struct drm_file *file,
Chris Wilson8dc5d142010-08-12 12:36:12 +01001705 struct drm_i915_gem_request *request,
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001706 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001707{
1708 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001709 struct drm_i915_file_private *file_priv = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001710 uint32_t seqno;
1711 int was_empty;
Chris Wilson3cce4692010-10-27 16:11:02 +01001712 int ret;
1713
1714 BUG_ON(request == NULL);
Eric Anholt673a3942008-07-30 12:06:12 -07001715
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001716 if (file != NULL)
1717 file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00001718
Chris Wilson3cce4692010-10-27 16:11:02 +01001719 ret = ring->add_request(ring, &seqno);
1720 if (ret)
1721 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001722
Chris Wilsona56ba562010-09-28 10:07:56 +01001723 ring->outstanding_lazy_request = false;
Eric Anholt673a3942008-07-30 12:06:12 -07001724
1725 request->seqno = seqno;
Zou Nan hai852835f2010-05-21 09:08:56 +08001726 request->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001727 request->emitted_jiffies = jiffies;
Zou Nan hai852835f2010-05-21 09:08:56 +08001728 was_empty = list_empty(&ring->request_list);
1729 list_add_tail(&request->list, &ring->request_list);
1730
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001731 if (file_priv) {
Chris Wilson1c255952010-09-26 11:03:27 +01001732 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001733 request->file_priv = file_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00001734 list_add_tail(&request->client_list,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001735 &file_priv->mm.request_list);
Chris Wilson1c255952010-09-26 11:03:27 +01001736 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00001737 }
Eric Anholt673a3942008-07-30 12:06:12 -07001738
Ben Gamarif65d9422009-09-14 17:48:44 -04001739 if (!dev_priv->mm.suspended) {
Chris Wilsonb3b079d2010-09-13 23:44:34 +01001740 mod_timer(&dev_priv->hangcheck_timer,
1741 jiffies + msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
Ben Gamarif65d9422009-09-14 17:48:44 -04001742 if (was_empty)
Chris Wilsonb3b079d2010-09-13 23:44:34 +01001743 queue_delayed_work(dev_priv->wq,
1744 &dev_priv->mm.retire_work, HZ);
Ben Gamarif65d9422009-09-14 17:48:44 -04001745 }
Chris Wilson3cce4692010-10-27 16:11:02 +01001746 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001747}
1748
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001749static inline void
1750i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
Eric Anholt673a3942008-07-30 12:06:12 -07001751{
Chris Wilson1c255952010-09-26 11:03:27 +01001752 struct drm_i915_file_private *file_priv = request->file_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07001753
Chris Wilson1c255952010-09-26 11:03:27 +01001754 if (!file_priv)
1755 return;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001756
Chris Wilson1c255952010-09-26 11:03:27 +01001757 spin_lock(&file_priv->mm.lock);
1758 list_del(&request->client_list);
1759 request->file_priv = NULL;
1760 spin_unlock(&file_priv->mm.lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001761}
1762
Chris Wilsondfaae392010-09-22 10:31:52 +01001763static void i915_gem_reset_ring_lists(struct drm_i915_private *dev_priv,
1764 struct intel_ring_buffer *ring)
Chris Wilson9375e442010-09-19 12:21:28 +01001765{
Chris Wilsondfaae392010-09-22 10:31:52 +01001766 while (!list_empty(&ring->request_list)) {
1767 struct drm_i915_gem_request *request;
Chris Wilson9375e442010-09-19 12:21:28 +01001768
Chris Wilsondfaae392010-09-22 10:31:52 +01001769 request = list_first_entry(&ring->request_list,
1770 struct drm_i915_gem_request,
1771 list);
1772
1773 list_del(&request->list);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001774 i915_gem_request_remove_from_client(request);
Chris Wilsondfaae392010-09-22 10:31:52 +01001775 kfree(request);
1776 }
1777
1778 while (!list_empty(&ring->active_list)) {
Chris Wilson05394f32010-11-08 19:18:58 +00001779 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001780
Chris Wilson05394f32010-11-08 19:18:58 +00001781 obj = list_first_entry(&ring->active_list,
1782 struct drm_i915_gem_object,
1783 ring_list);
Eric Anholt673a3942008-07-30 12:06:12 -07001784
Chris Wilson05394f32010-11-08 19:18:58 +00001785 obj->base.write_domain = 0;
1786 list_del_init(&obj->gpu_write_list);
1787 i915_gem_object_move_to_inactive(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001788 }
Eric Anholt673a3942008-07-30 12:06:12 -07001789}
1790
Chris Wilson312817a2010-11-22 11:50:11 +00001791static void i915_gem_reset_fences(struct drm_device *dev)
1792{
1793 struct drm_i915_private *dev_priv = dev->dev_private;
1794 int i;
1795
1796 for (i = 0; i < 16; i++) {
1797 struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
1798 if (reg->obj)
1799 i915_gem_clear_fence_reg(reg->obj);
1800 }
1801}
1802
Chris Wilson069efc12010-09-30 16:53:18 +01001803void i915_gem_reset(struct drm_device *dev)
Eric Anholt673a3942008-07-30 12:06:12 -07001804{
Chris Wilsondfaae392010-09-22 10:31:52 +01001805 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00001806 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001807
Chris Wilsondfaae392010-09-22 10:31:52 +01001808 i915_gem_reset_ring_lists(dev_priv, &dev_priv->render_ring);
Chris Wilson87acb0a2010-10-19 10:13:00 +01001809 i915_gem_reset_ring_lists(dev_priv, &dev_priv->bsd_ring);
Chris Wilson549f7362010-10-19 11:19:32 +01001810 i915_gem_reset_ring_lists(dev_priv, &dev_priv->blt_ring);
Chris Wilsondfaae392010-09-22 10:31:52 +01001811
1812 /* Remove anything from the flushing lists. The GPU cache is likely
1813 * to be lost on reset along with the data, so simply move the
1814 * lost bo to the inactive list.
1815 */
1816 while (!list_empty(&dev_priv->mm.flushing_list)) {
Chris Wilson05394f32010-11-08 19:18:58 +00001817 obj= list_first_entry(&dev_priv->mm.flushing_list,
1818 struct drm_i915_gem_object,
1819 mm_list);
Chris Wilson9375e442010-09-19 12:21:28 +01001820
Chris Wilson05394f32010-11-08 19:18:58 +00001821 obj->base.write_domain = 0;
1822 list_del_init(&obj->gpu_write_list);
1823 i915_gem_object_move_to_inactive(obj);
Chris Wilson9375e442010-09-19 12:21:28 +01001824 }
Chris Wilson9375e442010-09-19 12:21:28 +01001825
Chris Wilsondfaae392010-09-22 10:31:52 +01001826 /* Move everything out of the GPU domains to ensure we do any
1827 * necessary invalidation upon reuse.
1828 */
Chris Wilson05394f32010-11-08 19:18:58 +00001829 list_for_each_entry(obj,
Chris Wilson77f01232010-09-19 12:31:36 +01001830 &dev_priv->mm.inactive_list,
Chris Wilson69dc4982010-10-19 10:36:51 +01001831 mm_list)
Chris Wilson77f01232010-09-19 12:31:36 +01001832 {
Chris Wilson05394f32010-11-08 19:18:58 +00001833 obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
Chris Wilson77f01232010-09-19 12:31:36 +01001834 }
Chris Wilson069efc12010-09-30 16:53:18 +01001835
1836 /* The fence registers are invalidated so clear them out */
Chris Wilson312817a2010-11-22 11:50:11 +00001837 i915_gem_reset_fences(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07001838}
1839
1840/**
1841 * This function clears the request list as sequence numbers are passed.
1842 */
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001843static void
1844i915_gem_retire_requests_ring(struct drm_device *dev,
1845 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001846{
1847 drm_i915_private_t *dev_priv = dev->dev_private;
1848 uint32_t seqno;
1849
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001850 if (!ring->status_page.page_addr ||
1851 list_empty(&ring->request_list))
Karsten Wiese6c0594a2009-02-23 15:07:57 +01001852 return;
1853
Chris Wilson23bc5982010-09-29 16:10:57 +01001854 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07001855
Chris Wilson78501ea2010-10-27 12:18:21 +01001856 seqno = ring->get_seqno(ring);
Zou Nan hai852835f2010-05-21 09:08:56 +08001857 while (!list_empty(&ring->request_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001858 struct drm_i915_gem_request *request;
Eric Anholt673a3942008-07-30 12:06:12 -07001859
Zou Nan hai852835f2010-05-21 09:08:56 +08001860 request = list_first_entry(&ring->request_list,
Eric Anholt673a3942008-07-30 12:06:12 -07001861 struct drm_i915_gem_request,
1862 list);
Eric Anholt673a3942008-07-30 12:06:12 -07001863
Chris Wilsondfaae392010-09-22 10:31:52 +01001864 if (!i915_seqno_passed(seqno, request->seqno))
Eric Anholt673a3942008-07-30 12:06:12 -07001865 break;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001866
1867 trace_i915_gem_request_retire(dev, request->seqno);
1868
1869 list_del(&request->list);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001870 i915_gem_request_remove_from_client(request);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001871 kfree(request);
1872 }
1873
1874 /* Move any buffers on the active list that are no longer referenced
1875 * by the ringbuffer to the flushing/inactive lists as appropriate.
1876 */
1877 while (!list_empty(&ring->active_list)) {
Chris Wilson05394f32010-11-08 19:18:58 +00001878 struct drm_i915_gem_object *obj;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001879
Chris Wilson05394f32010-11-08 19:18:58 +00001880 obj= list_first_entry(&ring->active_list,
1881 struct drm_i915_gem_object,
1882 ring_list);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001883
Chris Wilson05394f32010-11-08 19:18:58 +00001884 if (!i915_seqno_passed(seqno, obj->last_rendering_seqno))
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001885 break;
1886
Chris Wilson05394f32010-11-08 19:18:58 +00001887 if (obj->base.write_domain != 0)
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001888 i915_gem_object_move_to_flushing(obj);
1889 else
1890 i915_gem_object_move_to_inactive(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001891 }
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001892
1893 if (unlikely (dev_priv->trace_irq_seqno &&
1894 i915_seqno_passed(dev_priv->trace_irq_seqno, seqno))) {
Chris Wilson78501ea2010-10-27 12:18:21 +01001895 ring->user_irq_put(ring);
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001896 dev_priv->trace_irq_seqno = 0;
1897 }
Chris Wilson23bc5982010-09-29 16:10:57 +01001898
1899 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07001900}
1901
1902void
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001903i915_gem_retire_requests(struct drm_device *dev)
1904{
1905 drm_i915_private_t *dev_priv = dev->dev_private;
1906
Chris Wilsonbe726152010-07-23 23:18:50 +01001907 if (!list_empty(&dev_priv->mm.deferred_free_list)) {
Chris Wilson05394f32010-11-08 19:18:58 +00001908 struct drm_i915_gem_object *obj, *next;
Chris Wilsonbe726152010-07-23 23:18:50 +01001909
1910 /* We must be careful that during unbind() we do not
1911 * accidentally infinitely recurse into retire requests.
1912 * Currently:
1913 * retire -> free -> unbind -> wait -> retire_ring
1914 */
Chris Wilson05394f32010-11-08 19:18:58 +00001915 list_for_each_entry_safe(obj, next,
Chris Wilsonbe726152010-07-23 23:18:50 +01001916 &dev_priv->mm.deferred_free_list,
Chris Wilson69dc4982010-10-19 10:36:51 +01001917 mm_list)
Chris Wilson05394f32010-11-08 19:18:58 +00001918 i915_gem_free_object_tail(obj);
Chris Wilsonbe726152010-07-23 23:18:50 +01001919 }
1920
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001921 i915_gem_retire_requests_ring(dev, &dev_priv->render_ring);
Chris Wilson87acb0a2010-10-19 10:13:00 +01001922 i915_gem_retire_requests_ring(dev, &dev_priv->bsd_ring);
Chris Wilson549f7362010-10-19 11:19:32 +01001923 i915_gem_retire_requests_ring(dev, &dev_priv->blt_ring);
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001924}
1925
Daniel Vetter75ef9da2010-08-21 00:25:16 +02001926static void
Eric Anholt673a3942008-07-30 12:06:12 -07001927i915_gem_retire_work_handler(struct work_struct *work)
1928{
1929 drm_i915_private_t *dev_priv;
1930 struct drm_device *dev;
1931
1932 dev_priv = container_of(work, drm_i915_private_t,
1933 mm.retire_work.work);
1934 dev = dev_priv->dev;
1935
Chris Wilson891b48c2010-09-29 12:26:37 +01001936 /* Come back later if the device is busy... */
1937 if (!mutex_trylock(&dev->struct_mutex)) {
1938 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
1939 return;
1940 }
1941
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001942 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08001943
Keith Packard6dbe2772008-10-14 21:41:13 -07001944 if (!dev_priv->mm.suspended &&
Zou Nan haid1b851f2010-05-21 09:08:57 +08001945 (!list_empty(&dev_priv->render_ring.request_list) ||
Chris Wilson549f7362010-10-19 11:19:32 +01001946 !list_empty(&dev_priv->bsd_ring.request_list) ||
1947 !list_empty(&dev_priv->blt_ring.request_list)))
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001948 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
Eric Anholt673a3942008-07-30 12:06:12 -07001949 mutex_unlock(&dev->struct_mutex);
1950}
1951
Daniel Vetter5a5a0c62009-09-15 22:57:36 +02001952int
Zou Nan hai852835f2010-05-21 09:08:56 +08001953i915_do_wait_request(struct drm_device *dev, uint32_t seqno,
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001954 bool interruptible, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001955{
1956 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001957 u32 ier;
Eric Anholt673a3942008-07-30 12:06:12 -07001958 int ret = 0;
1959
1960 BUG_ON(seqno == 0);
1961
Ben Gamariba1234d2009-09-14 17:48:47 -04001962 if (atomic_read(&dev_priv->mm.wedged))
Chris Wilson30dbf0c2010-09-25 10:19:17 +01001963 return -EAGAIN;
Ben Gamariffed1d02009-09-14 17:48:41 -04001964
Chris Wilson5d97eb62010-11-10 20:40:02 +00001965 if (seqno == ring->outstanding_lazy_request) {
Chris Wilson3cce4692010-10-27 16:11:02 +01001966 struct drm_i915_gem_request *request;
1967
1968 request = kzalloc(sizeof(*request), GFP_KERNEL);
1969 if (request == NULL)
Daniel Vettere35a41d2010-02-11 22:13:59 +01001970 return -ENOMEM;
Chris Wilson3cce4692010-10-27 16:11:02 +01001971
1972 ret = i915_add_request(dev, NULL, request, ring);
1973 if (ret) {
1974 kfree(request);
1975 return ret;
1976 }
1977
1978 seqno = request->seqno;
Daniel Vettere35a41d2010-02-11 22:13:59 +01001979 }
1980
Chris Wilson78501ea2010-10-27 12:18:21 +01001981 if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) {
Eric Anholtbad720f2009-10-22 16:11:14 -07001982 if (HAS_PCH_SPLIT(dev))
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001983 ier = I915_READ(DEIER) | I915_READ(GTIER);
1984 else
1985 ier = I915_READ(IER);
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001986 if (!ier) {
1987 DRM_ERROR("something (likely vbetool) disabled "
1988 "interrupts, re-enabling\n");
1989 i915_driver_irq_preinstall(dev);
1990 i915_driver_irq_postinstall(dev);
1991 }
1992
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001993 trace_i915_gem_request_wait_begin(dev, seqno);
1994
Chris Wilsonb2223492010-10-27 15:27:33 +01001995 ring->waiting_seqno = seqno;
Chris Wilson78501ea2010-10-27 12:18:21 +01001996 ring->user_irq_get(ring);
Daniel Vetter48764bf2009-09-15 22:57:32 +02001997 if (interruptible)
Zou Nan hai852835f2010-05-21 09:08:56 +08001998 ret = wait_event_interruptible(ring->irq_queue,
Chris Wilson78501ea2010-10-27 12:18:21 +01001999 i915_seqno_passed(ring->get_seqno(ring), seqno)
Zou Nan hai852835f2010-05-21 09:08:56 +08002000 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02002001 else
Zou Nan hai852835f2010-05-21 09:08:56 +08002002 wait_event(ring->irq_queue,
Chris Wilson78501ea2010-10-27 12:18:21 +01002003 i915_seqno_passed(ring->get_seqno(ring), seqno)
Zou Nan hai852835f2010-05-21 09:08:56 +08002004 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02002005
Chris Wilson78501ea2010-10-27 12:18:21 +01002006 ring->user_irq_put(ring);
Chris Wilsonb2223492010-10-27 15:27:33 +01002007 ring->waiting_seqno = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002008
2009 trace_i915_gem_request_wait_end(dev, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07002010 }
Ben Gamariba1234d2009-09-14 17:48:47 -04002011 if (atomic_read(&dev_priv->mm.wedged))
Chris Wilson30dbf0c2010-09-25 10:19:17 +01002012 ret = -EAGAIN;
Eric Anholt673a3942008-07-30 12:06:12 -07002013
2014 if (ret && ret != -ERESTARTSYS)
Daniel Vetter8bff9172010-02-11 22:19:40 +01002015 DRM_ERROR("%s returns %d (awaiting %d at %d, next %d)\n",
Chris Wilson78501ea2010-10-27 12:18:21 +01002016 __func__, ret, seqno, ring->get_seqno(ring),
Daniel Vetter8bff9172010-02-11 22:19:40 +01002017 dev_priv->next_seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07002018
2019 /* Directly dispatch request retiring. While we have the work queue
2020 * to handle this, the waiter on a request often wants an associated
2021 * buffer to have made it to the inactive list, and we would need
2022 * a separate wait queue to handle that.
2023 */
2024 if (ret == 0)
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002025 i915_gem_retire_requests_ring(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07002026
2027 return ret;
2028}
2029
Daniel Vetter48764bf2009-09-15 22:57:32 +02002030/**
2031 * Waits for a sequence number to be signaled, and cleans up the
2032 * request and object lists appropriately for that event.
2033 */
2034static int
Zou Nan hai852835f2010-05-21 09:08:56 +08002035i915_wait_request(struct drm_device *dev, uint32_t seqno,
Chris Wilsona56ba562010-09-28 10:07:56 +01002036 struct intel_ring_buffer *ring)
Daniel Vetter48764bf2009-09-15 22:57:32 +02002037{
Zou Nan hai852835f2010-05-21 09:08:56 +08002038 return i915_do_wait_request(dev, seqno, 1, ring);
Daniel Vetter48764bf2009-09-15 22:57:32 +02002039}
2040
Eric Anholt673a3942008-07-30 12:06:12 -07002041/**
2042 * Ensures that all rendering to the object has completed and the object is
2043 * safe to unbind from the GTT or access from the CPU.
2044 */
Chris Wilson54cf91d2010-11-25 18:00:26 +00002045int
Chris Wilson05394f32010-11-08 19:18:58 +00002046i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj,
Chris Wilson2cf34d72010-09-14 13:03:28 +01002047 bool interruptible)
Eric Anholt673a3942008-07-30 12:06:12 -07002048{
Chris Wilson05394f32010-11-08 19:18:58 +00002049 struct drm_device *dev = obj->base.dev;
Eric Anholt673a3942008-07-30 12:06:12 -07002050 int ret;
2051
Eric Anholte47c68e2008-11-14 13:35:19 -08002052 /* This function only exists to support waiting for existing rendering,
2053 * not for emitting required flushes.
Eric Anholt673a3942008-07-30 12:06:12 -07002054 */
Chris Wilson05394f32010-11-08 19:18:58 +00002055 BUG_ON((obj->base.write_domain & I915_GEM_GPU_DOMAINS) != 0);
Eric Anholt673a3942008-07-30 12:06:12 -07002056
2057 /* If there is rendering queued on the buffer being evicted, wait for
2058 * it.
2059 */
Chris Wilson05394f32010-11-08 19:18:58 +00002060 if (obj->active) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01002061 ret = i915_do_wait_request(dev,
Chris Wilson05394f32010-11-08 19:18:58 +00002062 obj->last_rendering_seqno,
Chris Wilson2cf34d72010-09-14 13:03:28 +01002063 interruptible,
Chris Wilson05394f32010-11-08 19:18:58 +00002064 obj->ring);
Chris Wilson2cf34d72010-09-14 13:03:28 +01002065 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002066 return ret;
2067 }
2068
2069 return 0;
2070}
2071
2072/**
2073 * Unbinds an object from the GTT aperture.
2074 */
Jesse Barnes0f973f22009-01-26 17:10:45 -08002075int
Chris Wilson05394f32010-11-08 19:18:58 +00002076i915_gem_object_unbind(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002077{
Eric Anholt673a3942008-07-30 12:06:12 -07002078 int ret = 0;
2079
Chris Wilson05394f32010-11-08 19:18:58 +00002080 if (obj->gtt_space == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002081 return 0;
2082
Chris Wilson05394f32010-11-08 19:18:58 +00002083 if (obj->pin_count != 0) {
Eric Anholt673a3942008-07-30 12:06:12 -07002084 DRM_ERROR("Attempting to unbind pinned buffer\n");
2085 return -EINVAL;
2086 }
2087
Eric Anholt5323fd02009-09-09 11:50:45 -07002088 /* blow away mappings if mapped through GTT */
2089 i915_gem_release_mmap(obj);
2090
Eric Anholt673a3942008-07-30 12:06:12 -07002091 /* Move the object to the CPU domain to ensure that
2092 * any possible CPU writes while it's not in the GTT
2093 * are flushed when we go to remap it. This will
2094 * also ensure that all pending GPU writes are finished
2095 * before we unbind.
2096 */
Eric Anholte47c68e2008-11-14 13:35:19 -08002097 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Chris Wilson8dc17752010-07-23 23:18:51 +01002098 if (ret == -ERESTARTSYS)
Eric Anholt673a3942008-07-30 12:06:12 -07002099 return ret;
Chris Wilson8dc17752010-07-23 23:18:51 +01002100 /* Continue on if we fail due to EIO, the GPU is hung so we
2101 * should be safe and we need to cleanup or else we might
2102 * cause memory corruption through use-after-free.
2103 */
Chris Wilson812ed4922010-09-30 15:08:57 +01002104 if (ret) {
2105 i915_gem_clflush_object(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00002106 obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
Chris Wilson812ed4922010-09-30 15:08:57 +01002107 }
Eric Anholt673a3942008-07-30 12:06:12 -07002108
Daniel Vetter96b47b62009-12-15 17:50:00 +01002109 /* release the fence reg _after_ flushing */
Chris Wilson05394f32010-11-08 19:18:58 +00002110 if (obj->fence_reg != I915_FENCE_REG_NONE)
Daniel Vetter96b47b62009-12-15 17:50:00 +01002111 i915_gem_clear_fence_reg(obj);
2112
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002113 i915_gem_gtt_unbind_object(obj);
Chris Wilsone5281cc2010-10-28 13:45:36 +01002114 i915_gem_object_put_pages_gtt(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002115
Chris Wilson6299f992010-11-24 12:23:44 +00002116 list_del_init(&obj->gtt_list);
Chris Wilson05394f32010-11-08 19:18:58 +00002117 list_del_init(&obj->mm_list);
Daniel Vetter75e9e912010-11-04 17:11:09 +01002118 /* Avoid an unnecessary call to unbind on rebind. */
Chris Wilson05394f32010-11-08 19:18:58 +00002119 obj->map_and_fenceable = true;
Eric Anholt673a3942008-07-30 12:06:12 -07002120
Chris Wilson05394f32010-11-08 19:18:58 +00002121 drm_mm_put_block(obj->gtt_space);
2122 obj->gtt_space = NULL;
2123 obj->gtt_offset = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002124
Chris Wilson05394f32010-11-08 19:18:58 +00002125 if (i915_gem_object_is_purgeable(obj))
Chris Wilson963b4832009-09-20 23:03:54 +01002126 i915_gem_object_truncate(obj);
2127
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002128 trace_i915_gem_object_unbind(obj);
2129
Chris Wilson8dc17752010-07-23 23:18:51 +01002130 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002131}
2132
Chris Wilson54cf91d2010-11-25 18:00:26 +00002133void
2134i915_gem_flush_ring(struct drm_device *dev,
2135 struct intel_ring_buffer *ring,
2136 uint32_t invalidate_domains,
2137 uint32_t flush_domains)
2138{
2139 ring->flush(ring, invalidate_domains, flush_domains);
2140 i915_gem_process_flushing_list(dev, flush_domains, ring);
2141}
2142
Chris Wilsona56ba562010-09-28 10:07:56 +01002143static int i915_ring_idle(struct drm_device *dev,
2144 struct intel_ring_buffer *ring)
2145{
Chris Wilson395b70b2010-10-28 21:28:46 +01002146 if (list_empty(&ring->gpu_write_list) && list_empty(&ring->active_list))
Chris Wilson64193402010-10-24 12:38:05 +01002147 return 0;
2148
Chris Wilson05394f32010-11-08 19:18:58 +00002149 i915_gem_flush_ring(dev, ring,
Chris Wilsona56ba562010-09-28 10:07:56 +01002150 I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
2151 return i915_wait_request(dev,
2152 i915_gem_next_request_seqno(dev, ring),
2153 ring);
2154}
2155
Chris Wilsonb47eb4a2010-08-07 11:01:23 +01002156int
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002157i915_gpu_idle(struct drm_device *dev)
2158{
2159 drm_i915_private_t *dev_priv = dev->dev_private;
2160 bool lists_empty;
Zou Nan hai852835f2010-05-21 09:08:56 +08002161 int ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002162
Zou Nan haid1b851f2010-05-21 09:08:57 +08002163 lists_empty = (list_empty(&dev_priv->mm.flushing_list) &&
Chris Wilson395b70b2010-10-28 21:28:46 +01002164 list_empty(&dev_priv->mm.active_list));
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002165 if (lists_empty)
2166 return 0;
2167
2168 /* Flush everything onto the inactive list. */
Chris Wilsona56ba562010-09-28 10:07:56 +01002169 ret = i915_ring_idle(dev, &dev_priv->render_ring);
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002170 if (ret)
2171 return ret;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002172
Chris Wilson87acb0a2010-10-19 10:13:00 +01002173 ret = i915_ring_idle(dev, &dev_priv->bsd_ring);
2174 if (ret)
2175 return ret;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002176
Chris Wilson549f7362010-10-19 11:19:32 +01002177 ret = i915_ring_idle(dev, &dev_priv->blt_ring);
2178 if (ret)
2179 return ret;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002180
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002181 return 0;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002182}
2183
Daniel Vetterc6642782010-11-12 13:46:18 +00002184static int sandybridge_write_fence_reg(struct drm_i915_gem_object *obj,
2185 struct intel_ring_buffer *pipelined)
Eric Anholt4e901fd2009-10-26 16:44:17 -07002186{
Chris Wilson05394f32010-11-08 19:18:58 +00002187 struct drm_device *dev = obj->base.dev;
Eric Anholt4e901fd2009-10-26 16:44:17 -07002188 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00002189 u32 size = obj->gtt_space->size;
2190 int regnum = obj->fence_reg;
Eric Anholt4e901fd2009-10-26 16:44:17 -07002191 uint64_t val;
2192
Chris Wilson05394f32010-11-08 19:18:58 +00002193 val = (uint64_t)((obj->gtt_offset + size - 4096) &
Daniel Vetterc6642782010-11-12 13:46:18 +00002194 0xfffff000) << 32;
Chris Wilson05394f32010-11-08 19:18:58 +00002195 val |= obj->gtt_offset & 0xfffff000;
2196 val |= (uint64_t)((obj->stride / 128) - 1) <<
Eric Anholt4e901fd2009-10-26 16:44:17 -07002197 SANDYBRIDGE_FENCE_PITCH_SHIFT;
2198
Chris Wilson05394f32010-11-08 19:18:58 +00002199 if (obj->tiling_mode == I915_TILING_Y)
Eric Anholt4e901fd2009-10-26 16:44:17 -07002200 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2201 val |= I965_FENCE_REG_VALID;
2202
Daniel Vetterc6642782010-11-12 13:46:18 +00002203 if (pipelined) {
2204 int ret = intel_ring_begin(pipelined, 6);
2205 if (ret)
2206 return ret;
2207
2208 intel_ring_emit(pipelined, MI_NOOP);
2209 intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(2));
2210 intel_ring_emit(pipelined, FENCE_REG_SANDYBRIDGE_0 + regnum*8);
2211 intel_ring_emit(pipelined, (u32)val);
2212 intel_ring_emit(pipelined, FENCE_REG_SANDYBRIDGE_0 + regnum*8 + 4);
2213 intel_ring_emit(pipelined, (u32)(val >> 32));
2214 intel_ring_advance(pipelined);
2215 } else
2216 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + regnum * 8, val);
2217
2218 return 0;
Eric Anholt4e901fd2009-10-26 16:44:17 -07002219}
2220
Daniel Vetterc6642782010-11-12 13:46:18 +00002221static int i965_write_fence_reg(struct drm_i915_gem_object *obj,
2222 struct intel_ring_buffer *pipelined)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002223{
Chris Wilson05394f32010-11-08 19:18:58 +00002224 struct drm_device *dev = obj->base.dev;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002225 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00002226 u32 size = obj->gtt_space->size;
2227 int regnum = obj->fence_reg;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002228 uint64_t val;
2229
Chris Wilson05394f32010-11-08 19:18:58 +00002230 val = (uint64_t)((obj->gtt_offset + size - 4096) &
Jesse Barnesde151cf2008-11-12 10:03:55 -08002231 0xfffff000) << 32;
Chris Wilson05394f32010-11-08 19:18:58 +00002232 val |= obj->gtt_offset & 0xfffff000;
2233 val |= ((obj->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
2234 if (obj->tiling_mode == I915_TILING_Y)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002235 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2236 val |= I965_FENCE_REG_VALID;
2237
Daniel Vetterc6642782010-11-12 13:46:18 +00002238 if (pipelined) {
2239 int ret = intel_ring_begin(pipelined, 6);
2240 if (ret)
2241 return ret;
2242
2243 intel_ring_emit(pipelined, MI_NOOP);
2244 intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(2));
2245 intel_ring_emit(pipelined, FENCE_REG_965_0 + regnum*8);
2246 intel_ring_emit(pipelined, (u32)val);
2247 intel_ring_emit(pipelined, FENCE_REG_965_0 + regnum*8 + 4);
2248 intel_ring_emit(pipelined, (u32)(val >> 32));
2249 intel_ring_advance(pipelined);
2250 } else
2251 I915_WRITE64(FENCE_REG_965_0 + regnum * 8, val);
2252
2253 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002254}
2255
Daniel Vetterc6642782010-11-12 13:46:18 +00002256static int i915_write_fence_reg(struct drm_i915_gem_object *obj,
2257 struct intel_ring_buffer *pipelined)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002258{
Chris Wilson05394f32010-11-08 19:18:58 +00002259 struct drm_device *dev = obj->base.dev;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002260 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00002261 u32 size = obj->gtt_space->size;
Daniel Vetterc6642782010-11-12 13:46:18 +00002262 u32 fence_reg, val, pitch_val;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002263 int tile_width;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002264
Daniel Vetterc6642782010-11-12 13:46:18 +00002265 if (WARN((obj->gtt_offset & ~I915_FENCE_START_MASK) ||
2266 (size & -size) != size ||
2267 (obj->gtt_offset & (size - 1)),
2268 "object 0x%08x [fenceable? %d] not 1M or pot-size (0x%08x) aligned\n",
2269 obj->gtt_offset, obj->map_and_fenceable, size))
2270 return -EINVAL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002271
Daniel Vetterc6642782010-11-12 13:46:18 +00002272 if (obj->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev))
Jesse Barnes0f973f22009-01-26 17:10:45 -08002273 tile_width = 128;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002274 else
Jesse Barnes0f973f22009-01-26 17:10:45 -08002275 tile_width = 512;
2276
2277 /* Note: pitch better be a power of two tile widths */
Chris Wilson05394f32010-11-08 19:18:58 +00002278 pitch_val = obj->stride / tile_width;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002279 pitch_val = ffs(pitch_val) - 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002280
Chris Wilson05394f32010-11-08 19:18:58 +00002281 val = obj->gtt_offset;
2282 if (obj->tiling_mode == I915_TILING_Y)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002283 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002284 val |= I915_FENCE_SIZE_BITS(size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002285 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2286 val |= I830_FENCE_REG_VALID;
2287
Chris Wilson05394f32010-11-08 19:18:58 +00002288 fence_reg = obj->fence_reg;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002289 if (fence_reg < 8)
2290 fence_reg = FENCE_REG_830_0 + fence_reg * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002291 else
Chris Wilsona00b10c2010-09-24 21:15:47 +01002292 fence_reg = FENCE_REG_945_8 + (fence_reg - 8) * 4;
Daniel Vetterc6642782010-11-12 13:46:18 +00002293
2294 if (pipelined) {
2295 int ret = intel_ring_begin(pipelined, 4);
2296 if (ret)
2297 return ret;
2298
2299 intel_ring_emit(pipelined, MI_NOOP);
2300 intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(1));
2301 intel_ring_emit(pipelined, fence_reg);
2302 intel_ring_emit(pipelined, val);
2303 intel_ring_advance(pipelined);
2304 } else
2305 I915_WRITE(fence_reg, val);
2306
2307 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002308}
2309
Daniel Vetterc6642782010-11-12 13:46:18 +00002310static int i830_write_fence_reg(struct drm_i915_gem_object *obj,
2311 struct intel_ring_buffer *pipelined)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002312{
Chris Wilson05394f32010-11-08 19:18:58 +00002313 struct drm_device *dev = obj->base.dev;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002314 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00002315 u32 size = obj->gtt_space->size;
2316 int regnum = obj->fence_reg;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002317 uint32_t val;
2318 uint32_t pitch_val;
2319
Daniel Vetterc6642782010-11-12 13:46:18 +00002320 if (WARN((obj->gtt_offset & ~I830_FENCE_START_MASK) ||
2321 (size & -size) != size ||
2322 (obj->gtt_offset & (size - 1)),
2323 "object 0x%08x not 512K or pot-size 0x%08x aligned\n",
2324 obj->gtt_offset, size))
2325 return -EINVAL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002326
Chris Wilson05394f32010-11-08 19:18:58 +00002327 pitch_val = obj->stride / 128;
Eric Anholte76a16d2009-05-26 17:44:56 -07002328 pitch_val = ffs(pitch_val) - 1;
Eric Anholte76a16d2009-05-26 17:44:56 -07002329
Chris Wilson05394f32010-11-08 19:18:58 +00002330 val = obj->gtt_offset;
2331 if (obj->tiling_mode == I915_TILING_Y)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002332 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Daniel Vetterc6642782010-11-12 13:46:18 +00002333 val |= I830_FENCE_SIZE_BITS(size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002334 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2335 val |= I830_FENCE_REG_VALID;
2336
Daniel Vetterc6642782010-11-12 13:46:18 +00002337 if (pipelined) {
2338 int ret = intel_ring_begin(pipelined, 4);
2339 if (ret)
2340 return ret;
2341
2342 intel_ring_emit(pipelined, MI_NOOP);
2343 intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(1));
2344 intel_ring_emit(pipelined, FENCE_REG_830_0 + regnum*4);
2345 intel_ring_emit(pipelined, val);
2346 intel_ring_advance(pipelined);
2347 } else
2348 I915_WRITE(FENCE_REG_830_0 + regnum * 4, val);
2349
2350 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002351}
2352
Chris Wilson2cf34d72010-09-14 13:03:28 +01002353static int i915_find_fence_reg(struct drm_device *dev,
2354 bool interruptible)
Daniel Vetterae3db242010-02-19 11:51:58 +01002355{
Daniel Vetterae3db242010-02-19 11:51:58 +01002356 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002357 struct drm_i915_fence_reg *reg;
Chris Wilson05394f32010-11-08 19:18:58 +00002358 struct drm_i915_gem_object *obj = NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002359 int i, avail, ret;
2360
2361 /* First try to find a free reg */
2362 avail = 0;
2363 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2364 reg = &dev_priv->fence_regs[i];
2365 if (!reg->obj)
2366 return i;
2367
Chris Wilson05394f32010-11-08 19:18:58 +00002368 if (!reg->obj->pin_count)
2369 avail++;
Daniel Vetterae3db242010-02-19 11:51:58 +01002370 }
2371
2372 if (avail == 0)
2373 return -ENOSPC;
2374
2375 /* None available, try to steal one or wait for a user to finish */
Chris Wilsona00b10c2010-09-24 21:15:47 +01002376 avail = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002377 list_for_each_entry(reg, &dev_priv->mm.fence_list,
2378 lru_list) {
Chris Wilson05394f32010-11-08 19:18:58 +00002379 obj = reg->obj;
2380 if (obj->pin_count)
Daniel Vetterae3db242010-02-19 11:51:58 +01002381 continue;
2382
2383 /* found one! */
Chris Wilson05394f32010-11-08 19:18:58 +00002384 avail = obj->fence_reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01002385 break;
2386 }
2387
Chris Wilsona00b10c2010-09-24 21:15:47 +01002388 BUG_ON(avail == I915_FENCE_REG_NONE);
Daniel Vetterae3db242010-02-19 11:51:58 +01002389
2390 /* We only have a reference on obj from the active list. put_fence_reg
2391 * might drop that one, causing a use-after-free in it. So hold a
2392 * private reference to obj like the other callers of put_fence_reg
2393 * (set_tiling ioctl) do. */
Chris Wilson05394f32010-11-08 19:18:58 +00002394 drm_gem_object_reference(&obj->base);
2395 ret = i915_gem_object_put_fence_reg(obj, interruptible);
2396 drm_gem_object_unreference(&obj->base);
Daniel Vetterae3db242010-02-19 11:51:58 +01002397 if (ret != 0)
2398 return ret;
2399
Chris Wilsona00b10c2010-09-24 21:15:47 +01002400 return avail;
Daniel Vetterae3db242010-02-19 11:51:58 +01002401}
2402
Jesse Barnesde151cf2008-11-12 10:03:55 -08002403/**
2404 * i915_gem_object_get_fence_reg - set up a fence reg for an object
2405 * @obj: object to map through a fence reg
2406 *
2407 * When mapping objects through the GTT, userspace wants to be able to write
2408 * to them without having to worry about swizzling if the object is tiled.
2409 *
2410 * This function walks the fence regs looking for a free one for @obj,
2411 * stealing one if it can't find any.
2412 *
2413 * It then sets up the reg based on the object's properties: address, pitch
2414 * and tiling format.
2415 */
Chris Wilson8c4b8c32009-06-17 22:08:52 +01002416int
Chris Wilson05394f32010-11-08 19:18:58 +00002417i915_gem_object_get_fence_reg(struct drm_i915_gem_object *obj,
Chris Wilson2cf34d72010-09-14 13:03:28 +01002418 bool interruptible)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002419{
Chris Wilson05394f32010-11-08 19:18:58 +00002420 struct drm_device *dev = obj->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002421 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002422 struct drm_i915_fence_reg *reg = NULL;
Daniel Vetterc6642782010-11-12 13:46:18 +00002423 struct intel_ring_buffer *pipelined = NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002424 int ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002425
Eric Anholta09ba7f2009-08-29 12:49:51 -07002426 /* Just update our place in the LRU if our fence is getting used. */
Chris Wilson05394f32010-11-08 19:18:58 +00002427 if (obj->fence_reg != I915_FENCE_REG_NONE) {
2428 reg = &dev_priv->fence_regs[obj->fence_reg];
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002429 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002430 return 0;
2431 }
2432
Chris Wilson05394f32010-11-08 19:18:58 +00002433 switch (obj->tiling_mode) {
Jesse Barnesde151cf2008-11-12 10:03:55 -08002434 case I915_TILING_NONE:
2435 WARN(1, "allocating a fence for non-tiled object?\n");
2436 break;
2437 case I915_TILING_X:
Chris Wilson05394f32010-11-08 19:18:58 +00002438 if (!obj->stride)
Jesse Barnes0f973f22009-01-26 17:10:45 -08002439 return -EINVAL;
Chris Wilson05394f32010-11-08 19:18:58 +00002440 WARN((obj->stride & (512 - 1)),
Jesse Barnes0f973f22009-01-26 17:10:45 -08002441 "object 0x%08x is X tiled but has non-512B pitch\n",
Chris Wilson05394f32010-11-08 19:18:58 +00002442 obj->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002443 break;
2444 case I915_TILING_Y:
Chris Wilson05394f32010-11-08 19:18:58 +00002445 if (!obj->stride)
Jesse Barnes0f973f22009-01-26 17:10:45 -08002446 return -EINVAL;
Chris Wilson05394f32010-11-08 19:18:58 +00002447 WARN((obj->stride & (128 - 1)),
Jesse Barnes0f973f22009-01-26 17:10:45 -08002448 "object 0x%08x is Y tiled but has non-128B pitch\n",
Chris Wilson05394f32010-11-08 19:18:58 +00002449 obj->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002450 break;
2451 }
2452
Chris Wilson2cf34d72010-09-14 13:03:28 +01002453 ret = i915_find_fence_reg(dev, interruptible);
Daniel Vetterae3db242010-02-19 11:51:58 +01002454 if (ret < 0)
2455 return ret;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002456
Chris Wilson05394f32010-11-08 19:18:58 +00002457 obj->fence_reg = ret;
2458 reg = &dev_priv->fence_regs[obj->fence_reg];
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002459 list_add_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002460
Jesse Barnesde151cf2008-11-12 10:03:55 -08002461 reg->obj = obj;
2462
Chris Wilsone259bef2010-09-17 00:32:02 +01002463 switch (INTEL_INFO(dev)->gen) {
2464 case 6:
Daniel Vetterc6642782010-11-12 13:46:18 +00002465 ret = sandybridge_write_fence_reg(obj, pipelined);
Chris Wilsone259bef2010-09-17 00:32:02 +01002466 break;
2467 case 5:
2468 case 4:
Daniel Vetterc6642782010-11-12 13:46:18 +00002469 ret = i965_write_fence_reg(obj, pipelined);
Chris Wilsone259bef2010-09-17 00:32:02 +01002470 break;
2471 case 3:
Daniel Vetterc6642782010-11-12 13:46:18 +00002472 ret = i915_write_fence_reg(obj, pipelined);
Chris Wilsone259bef2010-09-17 00:32:02 +01002473 break;
2474 case 2:
Daniel Vetterc6642782010-11-12 13:46:18 +00002475 ret = i830_write_fence_reg(obj, pipelined);
Chris Wilsone259bef2010-09-17 00:32:02 +01002476 break;
2477 }
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002478
Chris Wilsona00b10c2010-09-24 21:15:47 +01002479 trace_i915_gem_object_get_fence(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00002480 obj->fence_reg,
2481 obj->tiling_mode);
Daniel Vetterc6642782010-11-12 13:46:18 +00002482 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002483}
2484
2485/**
2486 * i915_gem_clear_fence_reg - clear out fence register info
2487 * @obj: object to clear
2488 *
2489 * Zeroes out the fence register itself and clears out the associated
Chris Wilson05394f32010-11-08 19:18:58 +00002490 * data structures in dev_priv and obj.
Jesse Barnesde151cf2008-11-12 10:03:55 -08002491 */
2492static void
Chris Wilson05394f32010-11-08 19:18:58 +00002493i915_gem_clear_fence_reg(struct drm_i915_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002494{
Chris Wilson05394f32010-11-08 19:18:58 +00002495 struct drm_device *dev = obj->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002496 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00002497 struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[obj->fence_reg];
Chris Wilsone259bef2010-09-17 00:32:02 +01002498 uint32_t fence_reg;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002499
Chris Wilsone259bef2010-09-17 00:32:02 +01002500 switch (INTEL_INFO(dev)->gen) {
2501 case 6:
Eric Anholt4e901fd2009-10-26 16:44:17 -07002502 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 +
Chris Wilson05394f32010-11-08 19:18:58 +00002503 (obj->fence_reg * 8), 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002504 break;
2505 case 5:
2506 case 4:
Chris Wilson05394f32010-11-08 19:18:58 +00002507 I915_WRITE64(FENCE_REG_965_0 + (obj->fence_reg * 8), 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002508 break;
2509 case 3:
Chris Wilson05394f32010-11-08 19:18:58 +00002510 if (obj->fence_reg >= 8)
2511 fence_reg = FENCE_REG_945_8 + (obj->fence_reg - 8) * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002512 else
Chris Wilsone259bef2010-09-17 00:32:02 +01002513 case 2:
Chris Wilson05394f32010-11-08 19:18:58 +00002514 fence_reg = FENCE_REG_830_0 + obj->fence_reg * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002515
2516 I915_WRITE(fence_reg, 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002517 break;
Eric Anholtdc529a42009-03-10 22:34:49 -07002518 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002519
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002520 reg->obj = NULL;
Chris Wilson05394f32010-11-08 19:18:58 +00002521 obj->fence_reg = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002522 list_del_init(&reg->lru_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002523}
2524
Eric Anholt673a3942008-07-30 12:06:12 -07002525/**
Chris Wilson52dc7d32009-06-06 09:46:01 +01002526 * i915_gem_object_put_fence_reg - waits on outstanding fenced access
2527 * to the buffer to finish, and then resets the fence register.
2528 * @obj: tiled object holding a fence register.
Chris Wilson2cf34d72010-09-14 13:03:28 +01002529 * @bool: whether the wait upon the fence is interruptible
Chris Wilson52dc7d32009-06-06 09:46:01 +01002530 *
2531 * Zeroes out the fence register itself and clears out the associated
Chris Wilson05394f32010-11-08 19:18:58 +00002532 * data structures in dev_priv and obj.
Chris Wilson52dc7d32009-06-06 09:46:01 +01002533 */
2534int
Chris Wilson05394f32010-11-08 19:18:58 +00002535i915_gem_object_put_fence_reg(struct drm_i915_gem_object *obj,
Chris Wilson2cf34d72010-09-14 13:03:28 +01002536 bool interruptible)
Chris Wilson52dc7d32009-06-06 09:46:01 +01002537{
Chris Wilson05394f32010-11-08 19:18:58 +00002538 struct drm_device *dev = obj->base.dev;
Chris Wilsoncaea7472010-11-12 13:53:37 +00002539 int ret;
Chris Wilson52dc7d32009-06-06 09:46:01 +01002540
Chris Wilson05394f32010-11-08 19:18:58 +00002541 if (obj->fence_reg == I915_FENCE_REG_NONE)
Chris Wilson52dc7d32009-06-06 09:46:01 +01002542 return 0;
2543
Daniel Vetter10ae9bd2010-02-01 13:59:17 +01002544 /* If we've changed tiling, GTT-mappings of the object
2545 * need to re-fault to ensure that the correct fence register
2546 * setup is in place.
2547 */
2548 i915_gem_release_mmap(obj);
2549
Chris Wilson52dc7d32009-06-06 09:46:01 +01002550 /* On the i915, GPU access to tiled buffers is via a fence,
2551 * therefore we must wait for any outstanding access to complete
2552 * before clearing the fence.
2553 */
Chris Wilsoncaea7472010-11-12 13:53:37 +00002554 if (obj->fenced_gpu_access) {
Chris Wilson919926a2010-11-12 13:42:53 +00002555 ret = i915_gem_object_flush_gpu_write_domain(obj, NULL);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002556 if (ret)
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002557 return ret;
2558
Chris Wilsoncaea7472010-11-12 13:53:37 +00002559 obj->fenced_gpu_access = false;
2560 }
2561
2562 if (obj->last_fenced_seqno) {
2563 ret = i915_do_wait_request(dev,
2564 obj->last_fenced_seqno,
2565 interruptible,
2566 obj->last_fenced_ring);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002567 if (ret)
Chris Wilson52dc7d32009-06-06 09:46:01 +01002568 return ret;
Chris Wilson53640e12010-09-20 11:40:50 +01002569
Chris Wilsoncaea7472010-11-12 13:53:37 +00002570 obj->last_fenced_seqno = false;
Chris Wilson52dc7d32009-06-06 09:46:01 +01002571 }
2572
Daniel Vetter4a726612010-02-01 13:59:16 +01002573 i915_gem_object_flush_gtt_write_domain(obj);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002574 i915_gem_clear_fence_reg(obj);
Chris Wilson52dc7d32009-06-06 09:46:01 +01002575
2576 return 0;
2577}
2578
2579/**
Eric Anholt673a3942008-07-30 12:06:12 -07002580 * Finds free space in the GTT aperture and binds the object there.
2581 */
2582static int
Chris Wilson05394f32010-11-08 19:18:58 +00002583i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
Daniel Vetter920afa72010-09-16 17:54:23 +02002584 unsigned alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01002585 bool map_and_fenceable)
Eric Anholt673a3942008-07-30 12:06:12 -07002586{
Chris Wilson05394f32010-11-08 19:18:58 +00002587 struct drm_device *dev = obj->base.dev;
Eric Anholt673a3942008-07-30 12:06:12 -07002588 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07002589 struct drm_mm_node *free_space;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002590 gfp_t gfpmask = __GFP_NORETRY | __GFP_NOWARN;
Daniel Vetter5e783302010-11-14 22:32:36 +01002591 u32 size, fence_size, fence_alignment, unfenced_alignment;
Daniel Vetter75e9e912010-11-04 17:11:09 +01002592 bool mappable, fenceable;
Chris Wilson07f73f62009-09-14 16:50:30 +01002593 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002594
Chris Wilson05394f32010-11-08 19:18:58 +00002595 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01002596 DRM_ERROR("Attempting to bind a purgeable object\n");
2597 return -EINVAL;
2598 }
2599
Chris Wilson05394f32010-11-08 19:18:58 +00002600 fence_size = i915_gem_get_gtt_size(obj);
2601 fence_alignment = i915_gem_get_gtt_alignment(obj);
2602 unfenced_alignment = i915_gem_get_unfenced_gtt_alignment(obj);
Chris Wilsona00b10c2010-09-24 21:15:47 +01002603
Eric Anholt673a3942008-07-30 12:06:12 -07002604 if (alignment == 0)
Daniel Vetter5e783302010-11-14 22:32:36 +01002605 alignment = map_and_fenceable ? fence_alignment :
2606 unfenced_alignment;
Daniel Vetter75e9e912010-11-04 17:11:09 +01002607 if (map_and_fenceable && alignment & (fence_alignment - 1)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002608 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2609 return -EINVAL;
2610 }
2611
Chris Wilson05394f32010-11-08 19:18:58 +00002612 size = map_and_fenceable ? fence_size : obj->base.size;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002613
Chris Wilson654fc602010-05-27 13:18:21 +01002614 /* If the object is bigger than the entire aperture, reject it early
2615 * before evicting everything in a vain attempt to find space.
2616 */
Chris Wilson05394f32010-11-08 19:18:58 +00002617 if (obj->base.size >
Daniel Vetter75e9e912010-11-04 17:11:09 +01002618 (map_and_fenceable ? dev_priv->mm.gtt_mappable_end : dev_priv->mm.gtt_total)) {
Chris Wilson654fc602010-05-27 13:18:21 +01002619 DRM_ERROR("Attempting to bind an object larger than the aperture\n");
2620 return -E2BIG;
2621 }
2622
Eric Anholt673a3942008-07-30 12:06:12 -07002623 search_free:
Daniel Vetter75e9e912010-11-04 17:11:09 +01002624 if (map_and_fenceable)
Daniel Vetter920afa72010-09-16 17:54:23 +02002625 free_space =
2626 drm_mm_search_free_in_range(&dev_priv->mm.gtt_space,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002627 size, alignment, 0,
Daniel Vetter920afa72010-09-16 17:54:23 +02002628 dev_priv->mm.gtt_mappable_end,
2629 0);
2630 else
2631 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002632 size, alignment, 0);
Daniel Vetter920afa72010-09-16 17:54:23 +02002633
2634 if (free_space != NULL) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01002635 if (map_and_fenceable)
Chris Wilson05394f32010-11-08 19:18:58 +00002636 obj->gtt_space =
Daniel Vetter920afa72010-09-16 17:54:23 +02002637 drm_mm_get_block_range_generic(free_space,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002638 size, alignment, 0,
Daniel Vetter920afa72010-09-16 17:54:23 +02002639 dev_priv->mm.gtt_mappable_end,
2640 0);
2641 else
Chris Wilson05394f32010-11-08 19:18:58 +00002642 obj->gtt_space =
Chris Wilsona00b10c2010-09-24 21:15:47 +01002643 drm_mm_get_block(free_space, size, alignment);
Daniel Vetter920afa72010-09-16 17:54:23 +02002644 }
Chris Wilson05394f32010-11-08 19:18:58 +00002645 if (obj->gtt_space == NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -07002646 /* If the gtt is empty and we're still having trouble
2647 * fitting our object in, we're out of memory.
2648 */
Daniel Vetter75e9e912010-11-04 17:11:09 +01002649 ret = i915_gem_evict_something(dev, size, alignment,
2650 map_and_fenceable);
Chris Wilson97311292009-09-21 00:22:34 +01002651 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002652 return ret;
Chris Wilson97311292009-09-21 00:22:34 +01002653
Eric Anholt673a3942008-07-30 12:06:12 -07002654 goto search_free;
2655 }
2656
Chris Wilsone5281cc2010-10-28 13:45:36 +01002657 ret = i915_gem_object_get_pages_gtt(obj, gfpmask);
Eric Anholt673a3942008-07-30 12:06:12 -07002658 if (ret) {
Chris Wilson05394f32010-11-08 19:18:58 +00002659 drm_mm_put_block(obj->gtt_space);
2660 obj->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002661
2662 if (ret == -ENOMEM) {
2663 /* first try to clear up some space from the GTT */
Chris Wilsona00b10c2010-09-24 21:15:47 +01002664 ret = i915_gem_evict_something(dev, size,
Daniel Vetter75e9e912010-11-04 17:11:09 +01002665 alignment,
2666 map_and_fenceable);
Chris Wilson07f73f62009-09-14 16:50:30 +01002667 if (ret) {
Chris Wilson07f73f62009-09-14 16:50:30 +01002668 /* now try to shrink everyone else */
Chris Wilson4bdadb92010-01-27 13:36:32 +00002669 if (gfpmask) {
2670 gfpmask = 0;
2671 goto search_free;
Chris Wilson07f73f62009-09-14 16:50:30 +01002672 }
2673
2674 return ret;
2675 }
2676
2677 goto search_free;
2678 }
2679
Eric Anholt673a3942008-07-30 12:06:12 -07002680 return ret;
2681 }
2682
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002683 ret = i915_gem_gtt_bind_object(obj);
2684 if (ret) {
Chris Wilsone5281cc2010-10-28 13:45:36 +01002685 i915_gem_object_put_pages_gtt(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00002686 drm_mm_put_block(obj->gtt_space);
2687 obj->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002688
Chris Wilsona00b10c2010-09-24 21:15:47 +01002689 ret = i915_gem_evict_something(dev, size,
Daniel Vetter75e9e912010-11-04 17:11:09 +01002690 alignment, map_and_fenceable);
Chris Wilson97311292009-09-21 00:22:34 +01002691 if (ret)
Chris Wilson07f73f62009-09-14 16:50:30 +01002692 return ret;
Chris Wilson07f73f62009-09-14 16:50:30 +01002693
2694 goto search_free;
Eric Anholt673a3942008-07-30 12:06:12 -07002695 }
Eric Anholt673a3942008-07-30 12:06:12 -07002696
Chris Wilson6299f992010-11-24 12:23:44 +00002697 list_add_tail(&obj->gtt_list, &dev_priv->mm.gtt_list);
Chris Wilson05394f32010-11-08 19:18:58 +00002698 list_add_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
Chris Wilsonbf1a1092010-08-07 11:01:20 +01002699
Eric Anholt673a3942008-07-30 12:06:12 -07002700 /* Assert that the object is not currently in any GPU domain. As it
2701 * wasn't in the GTT, there shouldn't be any way it could have been in
2702 * a GPU cache
2703 */
Chris Wilson05394f32010-11-08 19:18:58 +00002704 BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
2705 BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
Eric Anholt673a3942008-07-30 12:06:12 -07002706
Chris Wilson6299f992010-11-24 12:23:44 +00002707 obj->gtt_offset = obj->gtt_space->start;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002708
Daniel Vetter75e9e912010-11-04 17:11:09 +01002709 fenceable =
Chris Wilson05394f32010-11-08 19:18:58 +00002710 obj->gtt_space->size == fence_size &&
2711 (obj->gtt_space->start & (fence_alignment -1)) == 0;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002712
Daniel Vetter75e9e912010-11-04 17:11:09 +01002713 mappable =
Chris Wilson05394f32010-11-08 19:18:58 +00002714 obj->gtt_offset + obj->base.size <= dev_priv->mm.gtt_mappable_end;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002715
Chris Wilson05394f32010-11-08 19:18:58 +00002716 obj->map_and_fenceable = mappable && fenceable;
Daniel Vetter75e9e912010-11-04 17:11:09 +01002717
Chris Wilson6299f992010-11-24 12:23:44 +00002718 trace_i915_gem_object_bind(obj, obj->gtt_offset, map_and_fenceable);
Eric Anholt673a3942008-07-30 12:06:12 -07002719 return 0;
2720}
2721
2722void
Chris Wilson05394f32010-11-08 19:18:58 +00002723i915_gem_clflush_object(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002724{
Eric Anholt673a3942008-07-30 12:06:12 -07002725 /* If we don't have a page list set up, then we're not pinned
2726 * to GPU, and we can ignore the cache flush because it'll happen
2727 * again at bind time.
2728 */
Chris Wilson05394f32010-11-08 19:18:58 +00002729 if (obj->pages == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002730 return;
2731
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002732 trace_i915_gem_object_clflush(obj);
Eric Anholtcfa16a02009-05-26 18:46:16 -07002733
Chris Wilson05394f32010-11-08 19:18:58 +00002734 drm_clflush_pages(obj->pages, obj->base.size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002735}
2736
Eric Anholte47c68e2008-11-14 13:35:19 -08002737/** Flushes any GPU write domain for the object if it's dirty. */
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002738static int
Chris Wilson05394f32010-11-08 19:18:58 +00002739i915_gem_object_flush_gpu_write_domain(struct drm_i915_gem_object *obj,
Chris Wilson919926a2010-11-12 13:42:53 +00002740 struct intel_ring_buffer *pipelined)
Eric Anholte47c68e2008-11-14 13:35:19 -08002741{
Chris Wilson05394f32010-11-08 19:18:58 +00002742 struct drm_device *dev = obj->base.dev;
Eric Anholte47c68e2008-11-14 13:35:19 -08002743
Chris Wilson05394f32010-11-08 19:18:58 +00002744 if ((obj->base.write_domain & I915_GEM_GPU_DOMAINS) == 0)
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002745 return 0;
Eric Anholte47c68e2008-11-14 13:35:19 -08002746
2747 /* Queue the GPU write cache flushing we need. */
Chris Wilson05394f32010-11-08 19:18:58 +00002748 i915_gem_flush_ring(dev, obj->ring, 0, obj->base.write_domain);
2749 BUG_ON(obj->base.write_domain);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002750
Chris Wilson919926a2010-11-12 13:42:53 +00002751 if (pipelined && pipelined == obj->ring)
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002752 return 0;
2753
Chris Wilson2cf34d72010-09-14 13:03:28 +01002754 return i915_gem_object_wait_rendering(obj, true);
Eric Anholte47c68e2008-11-14 13:35:19 -08002755}
2756
2757/** Flushes the GTT write domain for the object if it's dirty. */
2758static void
Chris Wilson05394f32010-11-08 19:18:58 +00002759i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08002760{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002761 uint32_t old_write_domain;
2762
Chris Wilson05394f32010-11-08 19:18:58 +00002763 if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
Eric Anholte47c68e2008-11-14 13:35:19 -08002764 return;
2765
2766 /* No actual flushing is required for the GTT write domain. Writes
2767 * to it immediately go to main memory as far as we know, so there's
2768 * no chipset flush. It also doesn't land in render cache.
2769 */
Chris Wilson4a684a42010-10-28 14:44:08 +01002770 i915_gem_release_mmap(obj);
2771
Chris Wilson05394f32010-11-08 19:18:58 +00002772 old_write_domain = obj->base.write_domain;
2773 obj->base.write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002774
2775 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00002776 obj->base.read_domains,
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002777 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002778}
2779
2780/** Flushes the CPU write domain for the object if it's dirty. */
2781static void
Chris Wilson05394f32010-11-08 19:18:58 +00002782i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08002783{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002784 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002785
Chris Wilson05394f32010-11-08 19:18:58 +00002786 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
Eric Anholte47c68e2008-11-14 13:35:19 -08002787 return;
2788
2789 i915_gem_clflush_object(obj);
Daniel Vetter40ce6572010-11-05 18:12:18 +01002790 intel_gtt_chipset_flush();
Chris Wilson05394f32010-11-08 19:18:58 +00002791 old_write_domain = obj->base.write_domain;
2792 obj->base.write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002793
2794 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00002795 obj->base.read_domains,
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002796 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002797}
2798
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002799/**
2800 * Moves a single object to the GTT read, and possibly write domain.
2801 *
2802 * This function returns when the move is complete, including waiting on
2803 * flushes to occur.
2804 */
Jesse Barnes79e53942008-11-07 14:24:08 -08002805int
Chris Wilson20217462010-11-23 15:26:33 +00002806i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002807{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002808 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002809 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002810
Eric Anholt02354392008-11-26 13:58:13 -08002811 /* Not valid to be called on unbound objects. */
Chris Wilson05394f32010-11-08 19:18:58 +00002812 if (obj->gtt_space == NULL)
Eric Anholt02354392008-11-26 13:58:13 -08002813 return -EINVAL;
2814
Chris Wilson919926a2010-11-12 13:42:53 +00002815 ret = i915_gem_object_flush_gpu_write_domain(obj, NULL);
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002816 if (ret != 0)
2817 return ret;
2818
Chris Wilson72133422010-09-13 23:56:38 +01002819 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002820
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002821 if (write) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01002822 ret = i915_gem_object_wait_rendering(obj, true);
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002823 if (ret)
2824 return ret;
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002825 }
Eric Anholte47c68e2008-11-14 13:35:19 -08002826
Chris Wilson05394f32010-11-08 19:18:58 +00002827 old_write_domain = obj->base.write_domain;
2828 old_read_domains = obj->base.read_domains;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002829
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002830 /* It should now be out of any other write domains, and we can update
2831 * the domain values for our changes.
2832 */
Chris Wilson05394f32010-11-08 19:18:58 +00002833 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2834 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002835 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00002836 obj->base.read_domains = I915_GEM_DOMAIN_GTT;
2837 obj->base.write_domain = I915_GEM_DOMAIN_GTT;
2838 obj->dirty = 1;
Eric Anholte47c68e2008-11-14 13:35:19 -08002839 }
2840
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002841 trace_i915_gem_object_change_domain(obj,
2842 old_read_domains,
2843 old_write_domain);
2844
Eric Anholte47c68e2008-11-14 13:35:19 -08002845 return 0;
2846}
2847
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002848/*
2849 * Prepare buffer for display plane. Use uninterruptible for possible flush
2850 * wait, as in modesetting process we're not supposed to be interrupted.
2851 */
2852int
Chris Wilson05394f32010-11-08 19:18:58 +00002853i915_gem_object_set_to_display_plane(struct drm_i915_gem_object *obj,
Chris Wilson919926a2010-11-12 13:42:53 +00002854 struct intel_ring_buffer *pipelined)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002855{
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002856 uint32_t old_read_domains;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002857 int ret;
2858
2859 /* Not valid to be called on unbound objects. */
Chris Wilson05394f32010-11-08 19:18:58 +00002860 if (obj->gtt_space == NULL)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002861 return -EINVAL;
2862
Chris Wilson919926a2010-11-12 13:42:53 +00002863 ret = i915_gem_object_flush_gpu_write_domain(obj, pipelined);
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002864 if (ret)
2865 return ret;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002866
Chris Wilsonced270f2010-09-26 22:47:46 +01002867 /* Currently, we are always called from an non-interruptible context. */
2868 if (!pipelined) {
2869 ret = i915_gem_object_wait_rendering(obj, false);
2870 if (ret)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002871 return ret;
2872 }
2873
Chris Wilsonb118c1e2010-05-27 13:18:14 +01002874 i915_gem_object_flush_cpu_write_domain(obj);
2875
Chris Wilson05394f32010-11-08 19:18:58 +00002876 old_read_domains = obj->base.read_domains;
2877 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002878
2879 trace_i915_gem_object_change_domain(obj,
2880 old_read_domains,
Chris Wilson05394f32010-11-08 19:18:58 +00002881 obj->base.write_domain);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002882
2883 return 0;
2884}
2885
Chris Wilson85345512010-11-13 09:49:11 +00002886int
2887i915_gem_object_flush_gpu(struct drm_i915_gem_object *obj,
2888 bool interruptible)
2889{
2890 if (!obj->active)
2891 return 0;
2892
2893 if (obj->base.write_domain & I915_GEM_GPU_DOMAINS)
Chris Wilson05394f32010-11-08 19:18:58 +00002894 i915_gem_flush_ring(obj->base.dev, obj->ring,
Chris Wilson85345512010-11-13 09:49:11 +00002895 0, obj->base.write_domain);
2896
Chris Wilson05394f32010-11-08 19:18:58 +00002897 return i915_gem_object_wait_rendering(obj, interruptible);
Chris Wilson85345512010-11-13 09:49:11 +00002898}
2899
Eric Anholte47c68e2008-11-14 13:35:19 -08002900/**
2901 * Moves a single object to the CPU read, and possibly write domain.
2902 *
2903 * This function returns when the move is complete, including waiting on
2904 * flushes to occur.
2905 */
2906static int
Chris Wilson919926a2010-11-12 13:42:53 +00002907i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholte47c68e2008-11-14 13:35:19 -08002908{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002909 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002910 int ret;
2911
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002912 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Eric Anholte47c68e2008-11-14 13:35:19 -08002913 if (ret != 0)
2914 return ret;
2915
2916 i915_gem_object_flush_gtt_write_domain(obj);
2917
2918 /* If we have a partially-valid cache of the object in the CPU,
2919 * finish invalidating it and free the per-page flags.
2920 */
2921 i915_gem_object_set_to_full_cpu_read_domain(obj);
2922
Chris Wilson72133422010-09-13 23:56:38 +01002923 if (write) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01002924 ret = i915_gem_object_wait_rendering(obj, true);
Chris Wilson72133422010-09-13 23:56:38 +01002925 if (ret)
2926 return ret;
2927 }
2928
Chris Wilson05394f32010-11-08 19:18:58 +00002929 old_write_domain = obj->base.write_domain;
2930 old_read_domains = obj->base.read_domains;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002931
Eric Anholte47c68e2008-11-14 13:35:19 -08002932 /* Flush the CPU cache if it's still invalid. */
Chris Wilson05394f32010-11-08 19:18:58 +00002933 if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
Eric Anholte47c68e2008-11-14 13:35:19 -08002934 i915_gem_clflush_object(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08002935
Chris Wilson05394f32010-11-08 19:18:58 +00002936 obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08002937 }
2938
2939 /* It should now be out of any other write domains, and we can update
2940 * the domain values for our changes.
2941 */
Chris Wilson05394f32010-11-08 19:18:58 +00002942 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
Eric Anholte47c68e2008-11-14 13:35:19 -08002943
2944 /* If we're writing through the CPU, then the GPU read domains will
2945 * need to be invalidated at next use.
2946 */
2947 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00002948 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
2949 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08002950 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002951
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002952 trace_i915_gem_object_change_domain(obj,
2953 old_read_domains,
2954 old_write_domain);
2955
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002956 return 0;
2957}
2958
Eric Anholt673a3942008-07-30 12:06:12 -07002959/**
Eric Anholte47c68e2008-11-14 13:35:19 -08002960 * Moves the object from a partially CPU read to a full one.
Eric Anholt673a3942008-07-30 12:06:12 -07002961 *
Eric Anholte47c68e2008-11-14 13:35:19 -08002962 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
2963 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
2964 */
2965static void
Chris Wilson05394f32010-11-08 19:18:58 +00002966i915_gem_object_set_to_full_cpu_read_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08002967{
Chris Wilson05394f32010-11-08 19:18:58 +00002968 if (!obj->page_cpu_valid)
Eric Anholte47c68e2008-11-14 13:35:19 -08002969 return;
2970
2971 /* If we're partially in the CPU read domain, finish moving it in.
2972 */
Chris Wilson05394f32010-11-08 19:18:58 +00002973 if (obj->base.read_domains & I915_GEM_DOMAIN_CPU) {
Eric Anholte47c68e2008-11-14 13:35:19 -08002974 int i;
2975
Chris Wilson05394f32010-11-08 19:18:58 +00002976 for (i = 0; i <= (obj->base.size - 1) / PAGE_SIZE; i++) {
2977 if (obj->page_cpu_valid[i])
Eric Anholte47c68e2008-11-14 13:35:19 -08002978 continue;
Chris Wilson05394f32010-11-08 19:18:58 +00002979 drm_clflush_pages(obj->pages + i, 1);
Eric Anholte47c68e2008-11-14 13:35:19 -08002980 }
Eric Anholte47c68e2008-11-14 13:35:19 -08002981 }
2982
2983 /* Free the page_cpu_valid mappings which are now stale, whether
2984 * or not we've got I915_GEM_DOMAIN_CPU.
2985 */
Chris Wilson05394f32010-11-08 19:18:58 +00002986 kfree(obj->page_cpu_valid);
2987 obj->page_cpu_valid = NULL;
Eric Anholte47c68e2008-11-14 13:35:19 -08002988}
2989
2990/**
2991 * Set the CPU read domain on a range of the object.
2992 *
2993 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
2994 * not entirely valid. The page_cpu_valid member of the object flags which
2995 * pages have been flushed, and will be respected by
2996 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
2997 * of the whole object.
2998 *
2999 * This function returns when the move is complete, including waiting on
3000 * flushes to occur.
Eric Anholt673a3942008-07-30 12:06:12 -07003001 */
3002static int
Chris Wilson05394f32010-11-08 19:18:58 +00003003i915_gem_object_set_cpu_read_domain_range(struct drm_i915_gem_object *obj,
Eric Anholte47c68e2008-11-14 13:35:19 -08003004 uint64_t offset, uint64_t size)
Eric Anholt673a3942008-07-30 12:06:12 -07003005{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003006 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003007 int i, ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003008
Chris Wilson05394f32010-11-08 19:18:58 +00003009 if (offset == 0 && size == obj->base.size)
Eric Anholte47c68e2008-11-14 13:35:19 -08003010 return i915_gem_object_set_to_cpu_domain(obj, 0);
3011
Daniel Vetterba3d8d72010-02-11 22:37:04 +01003012 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Eric Anholte47c68e2008-11-14 13:35:19 -08003013 if (ret != 0)
3014 return ret;
3015 i915_gem_object_flush_gtt_write_domain(obj);
3016
3017 /* If we're already fully in the CPU read domain, we're done. */
Chris Wilson05394f32010-11-08 19:18:58 +00003018 if (obj->page_cpu_valid == NULL &&
3019 (obj->base.read_domains & I915_GEM_DOMAIN_CPU) != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07003020 return 0;
3021
Eric Anholte47c68e2008-11-14 13:35:19 -08003022 /* Otherwise, create/clear the per-page CPU read domain flag if we're
3023 * newly adding I915_GEM_DOMAIN_CPU
3024 */
Chris Wilson05394f32010-11-08 19:18:58 +00003025 if (obj->page_cpu_valid == NULL) {
3026 obj->page_cpu_valid = kzalloc(obj->base.size / PAGE_SIZE,
3027 GFP_KERNEL);
3028 if (obj->page_cpu_valid == NULL)
Eric Anholte47c68e2008-11-14 13:35:19 -08003029 return -ENOMEM;
Chris Wilson05394f32010-11-08 19:18:58 +00003030 } else if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0)
3031 memset(obj->page_cpu_valid, 0, obj->base.size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07003032
3033 /* Flush the cache on any pages that are still invalid from the CPU's
3034 * perspective.
3035 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003036 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
3037 i++) {
Chris Wilson05394f32010-11-08 19:18:58 +00003038 if (obj->page_cpu_valid[i])
Eric Anholt673a3942008-07-30 12:06:12 -07003039 continue;
3040
Chris Wilson05394f32010-11-08 19:18:58 +00003041 drm_clflush_pages(obj->pages + i, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07003042
Chris Wilson05394f32010-11-08 19:18:58 +00003043 obj->page_cpu_valid[i] = 1;
Eric Anholt673a3942008-07-30 12:06:12 -07003044 }
3045
Eric Anholte47c68e2008-11-14 13:35:19 -08003046 /* It should now be out of any other write domains, and we can update
3047 * the domain values for our changes.
3048 */
Chris Wilson05394f32010-11-08 19:18:58 +00003049 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
Eric Anholte47c68e2008-11-14 13:35:19 -08003050
Chris Wilson05394f32010-11-08 19:18:58 +00003051 old_read_domains = obj->base.read_domains;
3052 obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003053
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003054 trace_i915_gem_object_change_domain(obj,
3055 old_read_domains,
Chris Wilson05394f32010-11-08 19:18:58 +00003056 obj->base.write_domain);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003057
Eric Anholt673a3942008-07-30 12:06:12 -07003058 return 0;
3059}
3060
Eric Anholt673a3942008-07-30 12:06:12 -07003061/* Throttle our rendering by waiting until the ring has completed our requests
3062 * emitted over 20 msec ago.
3063 *
Eric Anholtb9624422009-06-03 07:27:35 +00003064 * Note that if we were to use the current jiffies each time around the loop,
3065 * we wouldn't escape the function with any frames outstanding if the time to
3066 * render a frame was over 20ms.
3067 *
Eric Anholt673a3942008-07-30 12:06:12 -07003068 * This should get us reasonable parallelism between CPU and GPU but also
3069 * relatively low latency when blocking on a particular request to finish.
3070 */
3071static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003072i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003073{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003074 struct drm_i915_private *dev_priv = dev->dev_private;
3075 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00003076 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003077 struct drm_i915_gem_request *request;
3078 struct intel_ring_buffer *ring = NULL;
3079 u32 seqno = 0;
3080 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003081
Chris Wilson1c255952010-09-26 11:03:27 +01003082 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003083 list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
Eric Anholtb9624422009-06-03 07:27:35 +00003084 if (time_after_eq(request->emitted_jiffies, recent_enough))
3085 break;
3086
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003087 ring = request->ring;
3088 seqno = request->seqno;
Eric Anholtb9624422009-06-03 07:27:35 +00003089 }
Chris Wilson1c255952010-09-26 11:03:27 +01003090 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003091
3092 if (seqno == 0)
3093 return 0;
3094
3095 ret = 0;
Chris Wilson78501ea2010-10-27 12:18:21 +01003096 if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) {
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003097 /* And wait for the seqno passing without holding any locks and
3098 * causing extra latency for others. This is safe as the irq
3099 * generation is designed to be run atomically and so is
3100 * lockless.
3101 */
Chris Wilson78501ea2010-10-27 12:18:21 +01003102 ring->user_irq_get(ring);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003103 ret = wait_event_interruptible(ring->irq_queue,
Chris Wilson78501ea2010-10-27 12:18:21 +01003104 i915_seqno_passed(ring->get_seqno(ring), seqno)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003105 || atomic_read(&dev_priv->mm.wedged));
Chris Wilson78501ea2010-10-27 12:18:21 +01003106 ring->user_irq_put(ring);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003107
3108 if (ret == 0 && atomic_read(&dev_priv->mm.wedged))
3109 ret = -EIO;
3110 }
3111
3112 if (ret == 0)
3113 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
Eric Anholtb9624422009-06-03 07:27:35 +00003114
Eric Anholt673a3942008-07-30 12:06:12 -07003115 return ret;
3116}
3117
Eric Anholt673a3942008-07-30 12:06:12 -07003118int
Chris Wilson05394f32010-11-08 19:18:58 +00003119i915_gem_object_pin(struct drm_i915_gem_object *obj,
3120 uint32_t alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01003121 bool map_and_fenceable)
Eric Anholt673a3942008-07-30 12:06:12 -07003122{
Chris Wilson05394f32010-11-08 19:18:58 +00003123 struct drm_device *dev = obj->base.dev;
Chris Wilsonf13d3f72010-09-20 17:36:15 +01003124 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003125 int ret;
3126
Chris Wilson05394f32010-11-08 19:18:58 +00003127 BUG_ON(obj->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT);
Chris Wilson23bc5982010-09-29 16:10:57 +01003128 WARN_ON(i915_verify_lists(dev));
Chris Wilsonac0c6b52010-05-27 13:18:18 +01003129
Chris Wilson05394f32010-11-08 19:18:58 +00003130 if (obj->gtt_space != NULL) {
3131 if ((alignment && obj->gtt_offset & (alignment - 1)) ||
3132 (map_and_fenceable && !obj->map_and_fenceable)) {
3133 WARN(obj->pin_count,
Chris Wilsonae7d49d2010-08-04 12:37:41 +01003134 "bo is already pinned with incorrect alignment:"
Daniel Vetter75e9e912010-11-04 17:11:09 +01003135 " offset=%x, req.alignment=%x, req.map_and_fenceable=%d,"
3136 " obj->map_and_fenceable=%d\n",
Chris Wilson05394f32010-11-08 19:18:58 +00003137 obj->gtt_offset, alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01003138 map_and_fenceable,
Chris Wilson05394f32010-11-08 19:18:58 +00003139 obj->map_and_fenceable);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01003140 ret = i915_gem_object_unbind(obj);
3141 if (ret)
3142 return ret;
3143 }
3144 }
3145
Chris Wilson05394f32010-11-08 19:18:58 +00003146 if (obj->gtt_space == NULL) {
Chris Wilsona00b10c2010-09-24 21:15:47 +01003147 ret = i915_gem_object_bind_to_gtt(obj, alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01003148 map_and_fenceable);
Chris Wilson97311292009-09-21 00:22:34 +01003149 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07003150 return ret;
Chris Wilson22c344e2009-02-11 14:26:45 +00003151 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05003152
Chris Wilson05394f32010-11-08 19:18:58 +00003153 if (obj->pin_count++ == 0) {
Chris Wilson05394f32010-11-08 19:18:58 +00003154 if (!obj->active)
3155 list_move_tail(&obj->mm_list,
Chris Wilsonf13d3f72010-09-20 17:36:15 +01003156 &dev_priv->mm.pinned_list);
Eric Anholt673a3942008-07-30 12:06:12 -07003157 }
Chris Wilson6299f992010-11-24 12:23:44 +00003158 obj->pin_mappable |= map_and_fenceable;
Eric Anholt673a3942008-07-30 12:06:12 -07003159
Chris Wilson23bc5982010-09-29 16:10:57 +01003160 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07003161 return 0;
3162}
3163
3164void
Chris Wilson05394f32010-11-08 19:18:58 +00003165i915_gem_object_unpin(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07003166{
Chris Wilson05394f32010-11-08 19:18:58 +00003167 struct drm_device *dev = obj->base.dev;
Eric Anholt673a3942008-07-30 12:06:12 -07003168 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003169
Chris Wilson23bc5982010-09-29 16:10:57 +01003170 WARN_ON(i915_verify_lists(dev));
Chris Wilson05394f32010-11-08 19:18:58 +00003171 BUG_ON(obj->pin_count == 0);
3172 BUG_ON(obj->gtt_space == NULL);
Eric Anholt673a3942008-07-30 12:06:12 -07003173
Chris Wilson05394f32010-11-08 19:18:58 +00003174 if (--obj->pin_count == 0) {
3175 if (!obj->active)
3176 list_move_tail(&obj->mm_list,
Eric Anholt673a3942008-07-30 12:06:12 -07003177 &dev_priv->mm.inactive_list);
Chris Wilson6299f992010-11-24 12:23:44 +00003178 obj->pin_mappable = false;
Eric Anholt673a3942008-07-30 12:06:12 -07003179 }
Chris Wilson23bc5982010-09-29 16:10:57 +01003180 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07003181}
3182
3183int
3184i915_gem_pin_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00003185 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003186{
3187 struct drm_i915_gem_pin *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003188 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07003189 int ret;
3190
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003191 ret = i915_mutex_lock_interruptible(dev);
3192 if (ret)
3193 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003194
Chris Wilson05394f32010-11-08 19:18:58 +00003195 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Eric Anholt673a3942008-07-30 12:06:12 -07003196 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003197 ret = -ENOENT;
3198 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07003199 }
Eric Anholt673a3942008-07-30 12:06:12 -07003200
Chris Wilson05394f32010-11-08 19:18:58 +00003201 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilsonbb6baf72009-09-22 14:24:13 +01003202 DRM_ERROR("Attempting to pin a purgeable buffer\n");
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003203 ret = -EINVAL;
3204 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003205 }
3206
Chris Wilson05394f32010-11-08 19:18:58 +00003207 if (obj->pin_filp != NULL && obj->pin_filp != file) {
Jesse Barnes79e53942008-11-07 14:24:08 -08003208 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
3209 args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003210 ret = -EINVAL;
3211 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08003212 }
3213
Chris Wilson05394f32010-11-08 19:18:58 +00003214 obj->user_pin_count++;
3215 obj->pin_filp = file;
3216 if (obj->user_pin_count == 1) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01003217 ret = i915_gem_object_pin(obj, args->alignment, true);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003218 if (ret)
3219 goto out;
Eric Anholt673a3942008-07-30 12:06:12 -07003220 }
3221
3222 /* XXX - flush the CPU caches for pinned objects
3223 * as the X server doesn't manage domains yet
3224 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003225 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00003226 args->offset = obj->gtt_offset;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003227out:
Chris Wilson05394f32010-11-08 19:18:58 +00003228 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003229unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07003230 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003231 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003232}
3233
3234int
3235i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00003236 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003237{
3238 struct drm_i915_gem_pin *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003239 struct drm_i915_gem_object *obj;
Chris Wilson76c1dec2010-09-25 11:22:51 +01003240 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003241
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003242 ret = i915_mutex_lock_interruptible(dev);
3243 if (ret)
3244 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003245
Chris Wilson05394f32010-11-08 19:18:58 +00003246 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Eric Anholt673a3942008-07-30 12:06:12 -07003247 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003248 ret = -ENOENT;
3249 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07003250 }
Chris Wilson76c1dec2010-09-25 11:22:51 +01003251
Chris Wilson05394f32010-11-08 19:18:58 +00003252 if (obj->pin_filp != file) {
Jesse Barnes79e53942008-11-07 14:24:08 -08003253 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
3254 args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003255 ret = -EINVAL;
3256 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08003257 }
Chris Wilson05394f32010-11-08 19:18:58 +00003258 obj->user_pin_count--;
3259 if (obj->user_pin_count == 0) {
3260 obj->pin_filp = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08003261 i915_gem_object_unpin(obj);
3262 }
Eric Anholt673a3942008-07-30 12:06:12 -07003263
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003264out:
Chris Wilson05394f32010-11-08 19:18:58 +00003265 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003266unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07003267 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003268 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003269}
3270
3271int
3272i915_gem_busy_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00003273 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003274{
3275 struct drm_i915_gem_busy *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003276 struct drm_i915_gem_object *obj;
Chris Wilson30dbf0c2010-09-25 10:19:17 +01003277 int ret;
3278
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003279 ret = i915_mutex_lock_interruptible(dev);
3280 if (ret)
3281 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003282
Chris Wilson05394f32010-11-08 19:18:58 +00003283 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Eric Anholt673a3942008-07-30 12:06:12 -07003284 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003285 ret = -ENOENT;
3286 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07003287 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08003288
Chris Wilson0be555b2010-08-04 15:36:30 +01003289 /* Count all active objects as busy, even if they are currently not used
3290 * by the gpu. Users of this interface expect objects to eventually
3291 * become non-busy without any further actions, therefore emit any
3292 * necessary flushes here.
Eric Anholtc4de0a52008-12-14 19:05:04 -08003293 */
Chris Wilson05394f32010-11-08 19:18:58 +00003294 args->busy = obj->active;
Chris Wilson0be555b2010-08-04 15:36:30 +01003295 if (args->busy) {
3296 /* Unconditionally flush objects, even when the gpu still uses this
3297 * object. Userspace calling this function indicates that it wants to
3298 * use this buffer rather sooner than later, so issuing the required
3299 * flush earlier is beneficial.
3300 */
Chris Wilson05394f32010-11-08 19:18:58 +00003301 if (obj->base.write_domain & I915_GEM_GPU_DOMAINS)
3302 i915_gem_flush_ring(dev, obj->ring,
3303 0, obj->base.write_domain);
Chris Wilson0be555b2010-08-04 15:36:30 +01003304
3305 /* Update the active list for the hardware's current position.
3306 * Otherwise this only updates on a delayed timer or when irqs
3307 * are actually unmasked, and our working set ends up being
3308 * larger than required.
3309 */
Chris Wilson05394f32010-11-08 19:18:58 +00003310 i915_gem_retire_requests_ring(dev, obj->ring);
Chris Wilson0be555b2010-08-04 15:36:30 +01003311
Chris Wilson05394f32010-11-08 19:18:58 +00003312 args->busy = obj->active;
Chris Wilson0be555b2010-08-04 15:36:30 +01003313 }
Eric Anholt673a3942008-07-30 12:06:12 -07003314
Chris Wilson05394f32010-11-08 19:18:58 +00003315 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003316unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07003317 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003318 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003319}
3320
3321int
3322i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
3323 struct drm_file *file_priv)
3324{
3325 return i915_gem_ring_throttle(dev, file_priv);
3326}
3327
Chris Wilson3ef94da2009-09-14 16:50:29 +01003328int
3329i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
3330 struct drm_file *file_priv)
3331{
3332 struct drm_i915_gem_madvise *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003333 struct drm_i915_gem_object *obj;
Chris Wilson76c1dec2010-09-25 11:22:51 +01003334 int ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003335
3336 switch (args->madv) {
3337 case I915_MADV_DONTNEED:
3338 case I915_MADV_WILLNEED:
3339 break;
3340 default:
3341 return -EINVAL;
3342 }
3343
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003344 ret = i915_mutex_lock_interruptible(dev);
3345 if (ret)
3346 return ret;
3347
Chris Wilson05394f32010-11-08 19:18:58 +00003348 obj = to_intel_bo(drm_gem_object_lookup(dev, file_priv, args->handle));
Chris Wilson3ef94da2009-09-14 16:50:29 +01003349 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003350 ret = -ENOENT;
3351 goto unlock;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003352 }
Chris Wilson3ef94da2009-09-14 16:50:29 +01003353
Chris Wilson05394f32010-11-08 19:18:58 +00003354 if (obj->pin_count) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003355 ret = -EINVAL;
3356 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003357 }
3358
Chris Wilson05394f32010-11-08 19:18:58 +00003359 if (obj->madv != __I915_MADV_PURGED)
3360 obj->madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003361
Chris Wilson2d7ef392009-09-20 23:13:10 +01003362 /* if the object is no longer bound, discard its backing storage */
Chris Wilson05394f32010-11-08 19:18:58 +00003363 if (i915_gem_object_is_purgeable(obj) &&
3364 obj->gtt_space == NULL)
Chris Wilson2d7ef392009-09-20 23:13:10 +01003365 i915_gem_object_truncate(obj);
3366
Chris Wilson05394f32010-11-08 19:18:58 +00003367 args->retained = obj->madv != __I915_MADV_PURGED;
Chris Wilsonbb6baf72009-09-22 14:24:13 +01003368
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003369out:
Chris Wilson05394f32010-11-08 19:18:58 +00003370 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003371unlock:
Chris Wilson3ef94da2009-09-14 16:50:29 +01003372 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003373 return ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003374}
3375
Chris Wilson05394f32010-11-08 19:18:58 +00003376struct drm_i915_gem_object *i915_gem_alloc_object(struct drm_device *dev,
3377 size_t size)
Daniel Vetterac52bc52010-04-09 19:05:06 +00003378{
Chris Wilson73aa8082010-09-30 11:46:12 +01003379 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc397b902010-04-09 19:05:07 +00003380 struct drm_i915_gem_object *obj;
3381
3382 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
3383 if (obj == NULL)
3384 return NULL;
3385
3386 if (drm_gem_object_init(dev, &obj->base, size) != 0) {
3387 kfree(obj);
3388 return NULL;
3389 }
3390
Chris Wilson73aa8082010-09-30 11:46:12 +01003391 i915_gem_info_add_obj(dev_priv, size);
3392
Daniel Vetterc397b902010-04-09 19:05:07 +00003393 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
3394 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3395
3396 obj->agp_type = AGP_USER_MEMORY;
Daniel Vetter62b8b212010-04-09 19:05:08 +00003397 obj->base.driver_private = NULL;
Daniel Vetterc397b902010-04-09 19:05:07 +00003398 obj->fence_reg = I915_FENCE_REG_NONE;
Chris Wilson69dc4982010-10-19 10:36:51 +01003399 INIT_LIST_HEAD(&obj->mm_list);
Daniel Vetter93a37f22010-11-05 20:24:53 +01003400 INIT_LIST_HEAD(&obj->gtt_list);
Chris Wilson69dc4982010-10-19 10:36:51 +01003401 INIT_LIST_HEAD(&obj->ring_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00003402 INIT_LIST_HEAD(&obj->gpu_write_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00003403 obj->madv = I915_MADV_WILLNEED;
Daniel Vetter75e9e912010-11-04 17:11:09 +01003404 /* Avoid an unnecessary call to unbind on the first bind. */
3405 obj->map_and_fenceable = true;
Daniel Vetterc397b902010-04-09 19:05:07 +00003406
Chris Wilson05394f32010-11-08 19:18:58 +00003407 return obj;
Daniel Vetterac52bc52010-04-09 19:05:06 +00003408}
3409
Eric Anholt673a3942008-07-30 12:06:12 -07003410int i915_gem_init_object(struct drm_gem_object *obj)
3411{
Daniel Vetterc397b902010-04-09 19:05:07 +00003412 BUG();
Jesse Barnesde151cf2008-11-12 10:03:55 -08003413
Eric Anholt673a3942008-07-30 12:06:12 -07003414 return 0;
3415}
3416
Chris Wilson05394f32010-11-08 19:18:58 +00003417static void i915_gem_free_object_tail(struct drm_i915_gem_object *obj)
Chris Wilsonbe726152010-07-23 23:18:50 +01003418{
Chris Wilson05394f32010-11-08 19:18:58 +00003419 struct drm_device *dev = obj->base.dev;
Chris Wilsonbe726152010-07-23 23:18:50 +01003420 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonbe726152010-07-23 23:18:50 +01003421 int ret;
3422
3423 ret = i915_gem_object_unbind(obj);
3424 if (ret == -ERESTARTSYS) {
Chris Wilson05394f32010-11-08 19:18:58 +00003425 list_move(&obj->mm_list,
Chris Wilsonbe726152010-07-23 23:18:50 +01003426 &dev_priv->mm.deferred_free_list);
3427 return;
3428 }
3429
Chris Wilson05394f32010-11-08 19:18:58 +00003430 if (obj->base.map_list.map)
Chris Wilsonbe726152010-07-23 23:18:50 +01003431 i915_gem_free_mmap_offset(obj);
3432
Chris Wilson05394f32010-11-08 19:18:58 +00003433 drm_gem_object_release(&obj->base);
3434 i915_gem_info_remove_obj(dev_priv, obj->base.size);
Chris Wilsonbe726152010-07-23 23:18:50 +01003435
Chris Wilson05394f32010-11-08 19:18:58 +00003436 kfree(obj->page_cpu_valid);
3437 kfree(obj->bit_17);
3438 kfree(obj);
Chris Wilsonbe726152010-07-23 23:18:50 +01003439}
3440
Chris Wilson05394f32010-11-08 19:18:58 +00003441void i915_gem_free_object(struct drm_gem_object *gem_obj)
Eric Anholt673a3942008-07-30 12:06:12 -07003442{
Chris Wilson05394f32010-11-08 19:18:58 +00003443 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
3444 struct drm_device *dev = obj->base.dev;
Eric Anholt673a3942008-07-30 12:06:12 -07003445
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003446 trace_i915_gem_object_destroy(obj);
3447
Chris Wilson05394f32010-11-08 19:18:58 +00003448 while (obj->pin_count > 0)
Eric Anholt673a3942008-07-30 12:06:12 -07003449 i915_gem_object_unpin(obj);
3450
Chris Wilson05394f32010-11-08 19:18:58 +00003451 if (obj->phys_obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10003452 i915_gem_detach_phys_object(dev, obj);
3453
Chris Wilsonbe726152010-07-23 23:18:50 +01003454 i915_gem_free_object_tail(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003455}
3456
Jesse Barnes5669fca2009-02-17 15:13:31 -08003457int
Eric Anholt673a3942008-07-30 12:06:12 -07003458i915_gem_idle(struct drm_device *dev)
3459{
3460 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson29105cc2010-01-07 10:39:13 +00003461 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003462
Keith Packard6dbe2772008-10-14 21:41:13 -07003463 mutex_lock(&dev->struct_mutex);
3464
Chris Wilson87acb0a2010-10-19 10:13:00 +01003465 if (dev_priv->mm.suspended) {
Keith Packard6dbe2772008-10-14 21:41:13 -07003466 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07003467 return 0;
Keith Packard6dbe2772008-10-14 21:41:13 -07003468 }
Eric Anholt673a3942008-07-30 12:06:12 -07003469
Chris Wilson29105cc2010-01-07 10:39:13 +00003470 ret = i915_gpu_idle(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07003471 if (ret) {
3472 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07003473 return ret;
Keith Packard6dbe2772008-10-14 21:41:13 -07003474 }
Eric Anholt673a3942008-07-30 12:06:12 -07003475
Chris Wilson29105cc2010-01-07 10:39:13 +00003476 /* Under UMS, be paranoid and evict. */
3477 if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
Chris Wilson5eac3ab2010-10-31 08:49:47 +00003478 ret = i915_gem_evict_inactive(dev, false);
Chris Wilson29105cc2010-01-07 10:39:13 +00003479 if (ret) {
3480 mutex_unlock(&dev->struct_mutex);
3481 return ret;
3482 }
3483 }
3484
Chris Wilson312817a2010-11-22 11:50:11 +00003485 i915_gem_reset_fences(dev);
3486
Chris Wilson29105cc2010-01-07 10:39:13 +00003487 /* Hack! Don't let anybody do execbuf while we don't control the chip.
3488 * We need to replace this with a semaphore, or something.
3489 * And not confound mm.suspended!
3490 */
3491 dev_priv->mm.suspended = 1;
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02003492 del_timer_sync(&dev_priv->hangcheck_timer);
Chris Wilson29105cc2010-01-07 10:39:13 +00003493
3494 i915_kernel_lost_context(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07003495 i915_gem_cleanup_ringbuffer(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00003496
Keith Packard6dbe2772008-10-14 21:41:13 -07003497 mutex_unlock(&dev->struct_mutex);
3498
Chris Wilson29105cc2010-01-07 10:39:13 +00003499 /* Cancel the retire work handler, which should be idle now. */
3500 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
3501
Eric Anholt673a3942008-07-30 12:06:12 -07003502 return 0;
3503}
3504
Eric Anholt673a3942008-07-30 12:06:12 -07003505int
Zou Nan hai8187a2b2010-05-21 09:08:55 +08003506i915_gem_init_ringbuffer(struct drm_device *dev)
3507{
3508 drm_i915_private_t *dev_priv = dev->dev_private;
3509 int ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01003510
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08003511 ret = intel_init_render_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01003512 if (ret)
Chris Wilsonb6913e42010-11-12 10:46:37 +00003513 return ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01003514
3515 if (HAS_BSD(dev)) {
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08003516 ret = intel_init_bsd_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01003517 if (ret)
3518 goto cleanup_render_ring;
Zou Nan haid1b851f2010-05-21 09:08:57 +08003519 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01003520
Chris Wilson549f7362010-10-19 11:19:32 +01003521 if (HAS_BLT(dev)) {
3522 ret = intel_init_blt_ring_buffer(dev);
3523 if (ret)
3524 goto cleanup_bsd_ring;
3525 }
3526
Chris Wilson6f392d52010-08-07 11:01:22 +01003527 dev_priv->next_seqno = 1;
3528
Chris Wilson68f95ba2010-05-27 13:18:22 +01003529 return 0;
3530
Chris Wilson549f7362010-10-19 11:19:32 +01003531cleanup_bsd_ring:
Chris Wilson78501ea2010-10-27 12:18:21 +01003532 intel_cleanup_ring_buffer(&dev_priv->bsd_ring);
Chris Wilson68f95ba2010-05-27 13:18:22 +01003533cleanup_render_ring:
Chris Wilson78501ea2010-10-27 12:18:21 +01003534 intel_cleanup_ring_buffer(&dev_priv->render_ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08003535 return ret;
3536}
3537
3538void
3539i915_gem_cleanup_ringbuffer(struct drm_device *dev)
3540{
3541 drm_i915_private_t *dev_priv = dev->dev_private;
3542
Chris Wilson78501ea2010-10-27 12:18:21 +01003543 intel_cleanup_ring_buffer(&dev_priv->render_ring);
3544 intel_cleanup_ring_buffer(&dev_priv->bsd_ring);
3545 intel_cleanup_ring_buffer(&dev_priv->blt_ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08003546}
3547
3548int
Eric Anholt673a3942008-07-30 12:06:12 -07003549i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
3550 struct drm_file *file_priv)
3551{
3552 drm_i915_private_t *dev_priv = dev->dev_private;
3553 int ret;
3554
Jesse Barnes79e53942008-11-07 14:24:08 -08003555 if (drm_core_check_feature(dev, DRIVER_MODESET))
3556 return 0;
3557
Ben Gamariba1234d2009-09-14 17:48:47 -04003558 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07003559 DRM_ERROR("Reenabling wedged hardware, good luck\n");
Ben Gamariba1234d2009-09-14 17:48:47 -04003560 atomic_set(&dev_priv->mm.wedged, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07003561 }
3562
Eric Anholt673a3942008-07-30 12:06:12 -07003563 mutex_lock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08003564 dev_priv->mm.suspended = 0;
3565
3566 ret = i915_gem_init_ringbuffer(dev);
Wu Fengguangd816f6a2009-04-18 10:43:32 +08003567 if (ret != 0) {
3568 mutex_unlock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08003569 return ret;
Wu Fengguangd816f6a2009-04-18 10:43:32 +08003570 }
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08003571
Chris Wilson69dc4982010-10-19 10:36:51 +01003572 BUG_ON(!list_empty(&dev_priv->mm.active_list));
Zou Nan hai852835f2010-05-21 09:08:56 +08003573 BUG_ON(!list_empty(&dev_priv->render_ring.active_list));
Chris Wilson87acb0a2010-10-19 10:13:00 +01003574 BUG_ON(!list_empty(&dev_priv->bsd_ring.active_list));
Chris Wilson549f7362010-10-19 11:19:32 +01003575 BUG_ON(!list_empty(&dev_priv->blt_ring.active_list));
Eric Anholt673a3942008-07-30 12:06:12 -07003576 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
3577 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
Zou Nan hai852835f2010-05-21 09:08:56 +08003578 BUG_ON(!list_empty(&dev_priv->render_ring.request_list));
Chris Wilson87acb0a2010-10-19 10:13:00 +01003579 BUG_ON(!list_empty(&dev_priv->bsd_ring.request_list));
Chris Wilson549f7362010-10-19 11:19:32 +01003580 BUG_ON(!list_empty(&dev_priv->blt_ring.request_list));
Eric Anholt673a3942008-07-30 12:06:12 -07003581 mutex_unlock(&dev->struct_mutex);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04003582
Chris Wilson5f353082010-06-07 14:03:03 +01003583 ret = drm_irq_install(dev);
3584 if (ret)
3585 goto cleanup_ringbuffer;
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04003586
Eric Anholt673a3942008-07-30 12:06:12 -07003587 return 0;
Chris Wilson5f353082010-06-07 14:03:03 +01003588
3589cleanup_ringbuffer:
3590 mutex_lock(&dev->struct_mutex);
3591 i915_gem_cleanup_ringbuffer(dev);
3592 dev_priv->mm.suspended = 1;
3593 mutex_unlock(&dev->struct_mutex);
3594
3595 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003596}
3597
3598int
3599i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
3600 struct drm_file *file_priv)
3601{
Jesse Barnes79e53942008-11-07 14:24:08 -08003602 if (drm_core_check_feature(dev, DRIVER_MODESET))
3603 return 0;
3604
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04003605 drm_irq_uninstall(dev);
Linus Torvaldse6890f62009-09-08 17:09:24 -07003606 return i915_gem_idle(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07003607}
3608
3609void
3610i915_gem_lastclose(struct drm_device *dev)
3611{
3612 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003613
Eric Anholte806b492009-01-22 09:56:58 -08003614 if (drm_core_check_feature(dev, DRIVER_MODESET))
3615 return;
3616
Keith Packard6dbe2772008-10-14 21:41:13 -07003617 ret = i915_gem_idle(dev);
3618 if (ret)
3619 DRM_ERROR("failed to idle hardware: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07003620}
3621
Chris Wilson64193402010-10-24 12:38:05 +01003622static void
3623init_ring_lists(struct intel_ring_buffer *ring)
3624{
3625 INIT_LIST_HEAD(&ring->active_list);
3626 INIT_LIST_HEAD(&ring->request_list);
3627 INIT_LIST_HEAD(&ring->gpu_write_list);
3628}
3629
Eric Anholt673a3942008-07-30 12:06:12 -07003630void
3631i915_gem_load(struct drm_device *dev)
3632{
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02003633 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07003634 drm_i915_private_t *dev_priv = dev->dev_private;
3635
Chris Wilson69dc4982010-10-19 10:36:51 +01003636 INIT_LIST_HEAD(&dev_priv->mm.active_list);
Eric Anholt673a3942008-07-30 12:06:12 -07003637 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
3638 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
Chris Wilsonf13d3f72010-09-20 17:36:15 +01003639 INIT_LIST_HEAD(&dev_priv->mm.pinned_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07003640 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Chris Wilsonbe726152010-07-23 23:18:50 +01003641 INIT_LIST_HEAD(&dev_priv->mm.deferred_free_list);
Daniel Vetter93a37f22010-11-05 20:24:53 +01003642 INIT_LIST_HEAD(&dev_priv->mm.gtt_list);
Chris Wilson64193402010-10-24 12:38:05 +01003643 init_ring_lists(&dev_priv->render_ring);
3644 init_ring_lists(&dev_priv->bsd_ring);
3645 init_ring_lists(&dev_priv->blt_ring);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02003646 for (i = 0; i < 16; i++)
3647 INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
Eric Anholt673a3942008-07-30 12:06:12 -07003648 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
3649 i915_gem_retire_work_handler);
Chris Wilson30dbf0c2010-09-25 10:19:17 +01003650 init_completion(&dev_priv->error_completion);
Chris Wilson31169712009-09-14 16:50:28 +01003651
Dave Airlie94400122010-07-20 13:15:31 +10003652 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
3653 if (IS_GEN3(dev)) {
3654 u32 tmp = I915_READ(MI_ARB_STATE);
3655 if (!(tmp & MI_ARB_C3_LP_WRITE_ENABLE)) {
3656 /* arb state is a masked write, so set bit + bit in mask */
3657 tmp = MI_ARB_C3_LP_WRITE_ENABLE | (MI_ARB_C3_LP_WRITE_ENABLE << MI_ARB_MASK_SHIFT);
3658 I915_WRITE(MI_ARB_STATE, tmp);
3659 }
3660 }
3661
Jesse Barnesde151cf2008-11-12 10:03:55 -08003662 /* Old X drivers will take 0-2 for front, back, depth buffers */
Eric Anholtb397c832010-01-26 09:43:10 -08003663 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3664 dev_priv->fence_reg_start = 3;
Jesse Barnesde151cf2008-11-12 10:03:55 -08003665
Chris Wilsona6c45cf2010-09-17 00:32:17 +01003666 if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08003667 dev_priv->num_fence_regs = 16;
3668 else
3669 dev_priv->num_fence_regs = 8;
3670
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02003671 /* Initialize fence registers to zero */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01003672 switch (INTEL_INFO(dev)->gen) {
3673 case 6:
3674 for (i = 0; i < 16; i++)
3675 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (i * 8), 0);
3676 break;
3677 case 5:
3678 case 4:
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02003679 for (i = 0; i < 16; i++)
3680 I915_WRITE64(FENCE_REG_965_0 + (i * 8), 0);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01003681 break;
3682 case 3:
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02003683 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
3684 for (i = 0; i < 8; i++)
3685 I915_WRITE(FENCE_REG_945_8 + (i * 4), 0);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01003686 case 2:
3687 for (i = 0; i < 8; i++)
3688 I915_WRITE(FENCE_REG_830_0 + (i * 4), 0);
3689 break;
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02003690 }
Eric Anholt673a3942008-07-30 12:06:12 -07003691 i915_gem_detect_bit_6_swizzle(dev);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003692 init_waitqueue_head(&dev_priv->pending_flip_queue);
Chris Wilson17250b72010-10-28 12:51:39 +01003693
3694 dev_priv->mm.inactive_shrinker.shrink = i915_gem_inactive_shrink;
3695 dev_priv->mm.inactive_shrinker.seeks = DEFAULT_SEEKS;
3696 register_shrinker(&dev_priv->mm.inactive_shrinker);
Eric Anholt673a3942008-07-30 12:06:12 -07003697}
Dave Airlie71acb5e2008-12-30 20:31:46 +10003698
3699/*
3700 * Create a physically contiguous memory object for this object
3701 * e.g. for cursor + overlay regs
3702 */
Chris Wilson995b6762010-08-20 13:23:26 +01003703static int i915_gem_init_phys_object(struct drm_device *dev,
3704 int id, int size, int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10003705{
3706 drm_i915_private_t *dev_priv = dev->dev_private;
3707 struct drm_i915_gem_phys_object *phys_obj;
3708 int ret;
3709
3710 if (dev_priv->mm.phys_objs[id - 1] || !size)
3711 return 0;
3712
Eric Anholt9a298b22009-03-24 12:23:04 -07003713 phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
Dave Airlie71acb5e2008-12-30 20:31:46 +10003714 if (!phys_obj)
3715 return -ENOMEM;
3716
3717 phys_obj->id = id;
3718
Chris Wilson6eeefaf2010-08-07 11:01:39 +01003719 phys_obj->handle = drm_pci_alloc(dev, size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10003720 if (!phys_obj->handle) {
3721 ret = -ENOMEM;
3722 goto kfree_obj;
3723 }
3724#ifdef CONFIG_X86
3725 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
3726#endif
3727
3728 dev_priv->mm.phys_objs[id - 1] = phys_obj;
3729
3730 return 0;
3731kfree_obj:
Eric Anholt9a298b22009-03-24 12:23:04 -07003732 kfree(phys_obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10003733 return ret;
3734}
3735
Chris Wilson995b6762010-08-20 13:23:26 +01003736static void i915_gem_free_phys_object(struct drm_device *dev, int id)
Dave Airlie71acb5e2008-12-30 20:31:46 +10003737{
3738 drm_i915_private_t *dev_priv = dev->dev_private;
3739 struct drm_i915_gem_phys_object *phys_obj;
3740
3741 if (!dev_priv->mm.phys_objs[id - 1])
3742 return;
3743
3744 phys_obj = dev_priv->mm.phys_objs[id - 1];
3745 if (phys_obj->cur_obj) {
3746 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
3747 }
3748
3749#ifdef CONFIG_X86
3750 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
3751#endif
3752 drm_pci_free(dev, phys_obj->handle);
3753 kfree(phys_obj);
3754 dev_priv->mm.phys_objs[id - 1] = NULL;
3755}
3756
3757void i915_gem_free_all_phys_object(struct drm_device *dev)
3758{
3759 int i;
3760
Dave Airlie260883c2009-01-22 17:58:49 +10003761 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
Dave Airlie71acb5e2008-12-30 20:31:46 +10003762 i915_gem_free_phys_object(dev, i);
3763}
3764
3765void i915_gem_detach_phys_object(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00003766 struct drm_i915_gem_object *obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10003767{
Chris Wilson05394f32010-11-08 19:18:58 +00003768 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Chris Wilsone5281cc2010-10-28 13:45:36 +01003769 char *vaddr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10003770 int i;
Dave Airlie71acb5e2008-12-30 20:31:46 +10003771 int page_count;
3772
Chris Wilson05394f32010-11-08 19:18:58 +00003773 if (!obj->phys_obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10003774 return;
Chris Wilson05394f32010-11-08 19:18:58 +00003775 vaddr = obj->phys_obj->handle->vaddr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10003776
Chris Wilson05394f32010-11-08 19:18:58 +00003777 page_count = obj->base.size / PAGE_SIZE;
Dave Airlie71acb5e2008-12-30 20:31:46 +10003778 for (i = 0; i < page_count; i++) {
Chris Wilsone5281cc2010-10-28 13:45:36 +01003779 struct page *page = read_cache_page_gfp(mapping, i,
3780 GFP_HIGHUSER | __GFP_RECLAIMABLE);
3781 if (!IS_ERR(page)) {
3782 char *dst = kmap_atomic(page);
3783 memcpy(dst, vaddr + i*PAGE_SIZE, PAGE_SIZE);
3784 kunmap_atomic(dst);
Dave Airlie71acb5e2008-12-30 20:31:46 +10003785
Chris Wilsone5281cc2010-10-28 13:45:36 +01003786 drm_clflush_pages(&page, 1);
3787
3788 set_page_dirty(page);
3789 mark_page_accessed(page);
3790 page_cache_release(page);
3791 }
Dave Airlie71acb5e2008-12-30 20:31:46 +10003792 }
Daniel Vetter40ce6572010-11-05 18:12:18 +01003793 intel_gtt_chipset_flush();
Chris Wilsond78b47b2009-06-17 21:52:49 +01003794
Chris Wilson05394f32010-11-08 19:18:58 +00003795 obj->phys_obj->cur_obj = NULL;
3796 obj->phys_obj = NULL;
Dave Airlie71acb5e2008-12-30 20:31:46 +10003797}
3798
3799int
3800i915_gem_attach_phys_object(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00003801 struct drm_i915_gem_object *obj,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01003802 int id,
3803 int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10003804{
Chris Wilson05394f32010-11-08 19:18:58 +00003805 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Dave Airlie71acb5e2008-12-30 20:31:46 +10003806 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie71acb5e2008-12-30 20:31:46 +10003807 int ret = 0;
3808 int page_count;
3809 int i;
3810
3811 if (id > I915_MAX_PHYS_OBJECT)
3812 return -EINVAL;
3813
Chris Wilson05394f32010-11-08 19:18:58 +00003814 if (obj->phys_obj) {
3815 if (obj->phys_obj->id == id)
Dave Airlie71acb5e2008-12-30 20:31:46 +10003816 return 0;
3817 i915_gem_detach_phys_object(dev, obj);
3818 }
3819
Dave Airlie71acb5e2008-12-30 20:31:46 +10003820 /* create a new object */
3821 if (!dev_priv->mm.phys_objs[id - 1]) {
3822 ret = i915_gem_init_phys_object(dev, id,
Chris Wilson05394f32010-11-08 19:18:58 +00003823 obj->base.size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10003824 if (ret) {
Chris Wilson05394f32010-11-08 19:18:58 +00003825 DRM_ERROR("failed to init phys object %d size: %zu\n",
3826 id, obj->base.size);
Chris Wilsone5281cc2010-10-28 13:45:36 +01003827 return ret;
Dave Airlie71acb5e2008-12-30 20:31:46 +10003828 }
3829 }
3830
3831 /* bind to the object */
Chris Wilson05394f32010-11-08 19:18:58 +00003832 obj->phys_obj = dev_priv->mm.phys_objs[id - 1];
3833 obj->phys_obj->cur_obj = obj;
Dave Airlie71acb5e2008-12-30 20:31:46 +10003834
Chris Wilson05394f32010-11-08 19:18:58 +00003835 page_count = obj->base.size / PAGE_SIZE;
Dave Airlie71acb5e2008-12-30 20:31:46 +10003836
3837 for (i = 0; i < page_count; i++) {
Chris Wilsone5281cc2010-10-28 13:45:36 +01003838 struct page *page;
3839 char *dst, *src;
Dave Airlie71acb5e2008-12-30 20:31:46 +10003840
Chris Wilsone5281cc2010-10-28 13:45:36 +01003841 page = read_cache_page_gfp(mapping, i,
3842 GFP_HIGHUSER | __GFP_RECLAIMABLE);
3843 if (IS_ERR(page))
3844 return PTR_ERR(page);
3845
Chris Wilsonff75b9b2010-10-30 22:52:31 +01003846 src = kmap_atomic(page);
Chris Wilson05394f32010-11-08 19:18:58 +00003847 dst = obj->phys_obj->handle->vaddr + (i * PAGE_SIZE);
Dave Airlie71acb5e2008-12-30 20:31:46 +10003848 memcpy(dst, src, PAGE_SIZE);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -07003849 kunmap_atomic(src);
Chris Wilsone5281cc2010-10-28 13:45:36 +01003850
3851 mark_page_accessed(page);
3852 page_cache_release(page);
Dave Airlie71acb5e2008-12-30 20:31:46 +10003853 }
3854
3855 return 0;
Dave Airlie71acb5e2008-12-30 20:31:46 +10003856}
3857
3858static int
Chris Wilson05394f32010-11-08 19:18:58 +00003859i915_gem_phys_pwrite(struct drm_device *dev,
3860 struct drm_i915_gem_object *obj,
Dave Airlie71acb5e2008-12-30 20:31:46 +10003861 struct drm_i915_gem_pwrite *args,
3862 struct drm_file *file_priv)
3863{
Chris Wilson05394f32010-11-08 19:18:58 +00003864 void *vaddr = obj->phys_obj->handle->vaddr + args->offset;
Chris Wilsonb47b30c2010-11-08 01:12:29 +00003865 char __user *user_data = (char __user *) (uintptr_t) args->data_ptr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10003866
Chris Wilsonb47b30c2010-11-08 01:12:29 +00003867 if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) {
3868 unsigned long unwritten;
3869
3870 /* The physical object once assigned is fixed for the lifetime
3871 * of the obj, so we can safely drop the lock and continue
3872 * to access vaddr.
3873 */
3874 mutex_unlock(&dev->struct_mutex);
3875 unwritten = copy_from_user(vaddr, user_data, args->size);
3876 mutex_lock(&dev->struct_mutex);
3877 if (unwritten)
3878 return -EFAULT;
3879 }
Dave Airlie71acb5e2008-12-30 20:31:46 +10003880
Daniel Vetter40ce6572010-11-05 18:12:18 +01003881 intel_gtt_chipset_flush();
Dave Airlie71acb5e2008-12-30 20:31:46 +10003882 return 0;
3883}
Eric Anholtb9624422009-06-03 07:27:35 +00003884
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003885void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00003886{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003887 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00003888
3889 /* Clean up our request list when the client is going away, so that
3890 * later retire_requests won't dereference our soon-to-be-gone
3891 * file_priv.
3892 */
Chris Wilson1c255952010-09-26 11:03:27 +01003893 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003894 while (!list_empty(&file_priv->mm.request_list)) {
3895 struct drm_i915_gem_request *request;
3896
3897 request = list_first_entry(&file_priv->mm.request_list,
3898 struct drm_i915_gem_request,
3899 client_list);
3900 list_del(&request->client_list);
3901 request->file_priv = NULL;
3902 }
Chris Wilson1c255952010-09-26 11:03:27 +01003903 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00003904}
Chris Wilson31169712009-09-14 16:50:28 +01003905
Chris Wilson31169712009-09-14 16:50:28 +01003906static int
Chris Wilson1637ef42010-04-20 17:10:35 +01003907i915_gpu_is_active(struct drm_device *dev)
3908{
3909 drm_i915_private_t *dev_priv = dev->dev_private;
3910 int lists_empty;
3911
Chris Wilson1637ef42010-04-20 17:10:35 +01003912 lists_empty = list_empty(&dev_priv->mm.flushing_list) &&
Chris Wilson17250b72010-10-28 12:51:39 +01003913 list_empty(&dev_priv->mm.active_list);
Chris Wilson1637ef42010-04-20 17:10:35 +01003914
3915 return !lists_empty;
3916}
3917
3918static int
Chris Wilson17250b72010-10-28 12:51:39 +01003919i915_gem_inactive_shrink(struct shrinker *shrinker,
3920 int nr_to_scan,
3921 gfp_t gfp_mask)
Chris Wilson31169712009-09-14 16:50:28 +01003922{
Chris Wilson17250b72010-10-28 12:51:39 +01003923 struct drm_i915_private *dev_priv =
3924 container_of(shrinker,
3925 struct drm_i915_private,
3926 mm.inactive_shrinker);
3927 struct drm_device *dev = dev_priv->dev;
3928 struct drm_i915_gem_object *obj, *next;
3929 int cnt;
3930
3931 if (!mutex_trylock(&dev->struct_mutex))
Chris Wilsonbbe2e112010-10-28 22:35:07 +01003932 return 0;
Chris Wilson31169712009-09-14 16:50:28 +01003933
3934 /* "fast-path" to count number of available objects */
3935 if (nr_to_scan == 0) {
Chris Wilson17250b72010-10-28 12:51:39 +01003936 cnt = 0;
3937 list_for_each_entry(obj,
3938 &dev_priv->mm.inactive_list,
3939 mm_list)
3940 cnt++;
3941 mutex_unlock(&dev->struct_mutex);
3942 return cnt / 100 * sysctl_vfs_cache_pressure;
Chris Wilson31169712009-09-14 16:50:28 +01003943 }
3944
Chris Wilson1637ef42010-04-20 17:10:35 +01003945rescan:
Chris Wilson31169712009-09-14 16:50:28 +01003946 /* first scan for clean buffers */
Chris Wilson17250b72010-10-28 12:51:39 +01003947 i915_gem_retire_requests(dev);
Chris Wilson31169712009-09-14 16:50:28 +01003948
Chris Wilson17250b72010-10-28 12:51:39 +01003949 list_for_each_entry_safe(obj, next,
3950 &dev_priv->mm.inactive_list,
3951 mm_list) {
3952 if (i915_gem_object_is_purgeable(obj)) {
Chris Wilson20217462010-11-23 15:26:33 +00003953 if (i915_gem_object_unbind(obj) == 0 &&
3954 --nr_to_scan == 0)
Chris Wilson17250b72010-10-28 12:51:39 +01003955 break;
Chris Wilson31169712009-09-14 16:50:28 +01003956 }
Chris Wilson31169712009-09-14 16:50:28 +01003957 }
3958
3959 /* second pass, evict/count anything still on the inactive list */
Chris Wilson17250b72010-10-28 12:51:39 +01003960 cnt = 0;
3961 list_for_each_entry_safe(obj, next,
3962 &dev_priv->mm.inactive_list,
3963 mm_list) {
Chris Wilson20217462010-11-23 15:26:33 +00003964 if (nr_to_scan &&
3965 i915_gem_object_unbind(obj) == 0)
Chris Wilson17250b72010-10-28 12:51:39 +01003966 nr_to_scan--;
Chris Wilson20217462010-11-23 15:26:33 +00003967 else
Chris Wilson17250b72010-10-28 12:51:39 +01003968 cnt++;
Chris Wilson31169712009-09-14 16:50:28 +01003969 }
3970
Chris Wilson17250b72010-10-28 12:51:39 +01003971 if (nr_to_scan && i915_gpu_is_active(dev)) {
Chris Wilson1637ef42010-04-20 17:10:35 +01003972 /*
3973 * We are desperate for pages, so as a last resort, wait
3974 * for the GPU to finish and discard whatever we can.
3975 * This has a dramatic impact to reduce the number of
3976 * OOM-killer events whilst running the GPU aggressively.
3977 */
Chris Wilson17250b72010-10-28 12:51:39 +01003978 if (i915_gpu_idle(dev) == 0)
Chris Wilson1637ef42010-04-20 17:10:35 +01003979 goto rescan;
3980 }
Chris Wilson17250b72010-10-28 12:51:39 +01003981 mutex_unlock(&dev->struct_mutex);
3982 return cnt / 100 * sysctl_vfs_cache_pressure;
Chris Wilson31169712009-09-14 16:50:28 +01003983}