blob: c9bdaf208a91f9e2023d621107716ac77b68fb1c [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 Wilson88241782011-01-07 17:09:48 +000038static __must_check int i915_gem_object_flush_gpu_write_domain(struct drm_i915_gem_object *obj);
Chris Wilson05394f32010-11-08 19:18:58 +000039static void i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj);
40static void i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj);
Chris Wilson88241782011-01-07 17:09:48 +000041static __must_check int i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj,
42 bool write);
43static __must_check int i915_gem_object_set_cpu_read_domain_range(struct drm_i915_gem_object *obj,
44 uint64_t offset,
45 uint64_t size);
Chris Wilson05394f32010-11-08 19:18:58 +000046static void i915_gem_object_set_to_full_cpu_read_domain(struct drm_i915_gem_object *obj);
Chris Wilson88241782011-01-07 17:09:48 +000047static __must_check int i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
48 unsigned alignment,
49 bool map_and_fenceable);
Chris Wilsond9e86c02010-11-10 16:40:20 +000050static void i915_gem_clear_fence_reg(struct drm_device *dev,
51 struct drm_i915_fence_reg *reg);
Chris Wilson05394f32010-11-08 19:18:58 +000052static int i915_gem_phys_pwrite(struct drm_device *dev,
53 struct drm_i915_gem_object *obj,
Dave Airlie71acb5e2008-12-30 20:31:46 +100054 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +000055 struct drm_file *file);
56static void i915_gem_free_object_tail(struct drm_i915_gem_object *obj);
Eric Anholt673a3942008-07-30 12:06:12 -070057
Chris Wilson17250b72010-10-28 12:51:39 +010058static int i915_gem_inactive_shrink(struct shrinker *shrinker,
59 int nr_to_scan,
60 gfp_t gfp_mask);
61
Chris Wilson31169712009-09-14 16:50:28 +010062
Chris Wilson73aa8082010-09-30 11:46:12 +010063/* some bookkeeping */
64static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
65 size_t size)
66{
67 dev_priv->mm.object_count++;
68 dev_priv->mm.object_memory += size;
69}
70
71static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
72 size_t size)
73{
74 dev_priv->mm.object_count--;
75 dev_priv->mm.object_memory -= size;
76}
77
Chris Wilson21dd3732011-01-26 15:55:56 +000078static int
79i915_gem_wait_for_error(struct drm_device *dev)
Chris Wilson30dbf0c2010-09-25 10:19:17 +010080{
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
Chris Wilson21dd3732011-01-26 15:55:56 +000093 if (atomic_read(&dev_priv->mm.wedged)) {
94 /* GPU is hung, bump the completion count to account for
95 * the token we just consumed so that we never hit zero and
96 * end up waiting upon a subsequent completion event that
97 * will never happen.
98 */
99 spin_lock_irqsave(&x->wait.lock, flags);
100 x->done++;
101 spin_unlock_irqrestore(&x->wait.lock, flags);
102 }
103 return 0;
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100104}
105
Chris Wilson54cf91d2010-11-25 18:00:26 +0000106int i915_mutex_lock_interruptible(struct drm_device *dev)
Chris Wilson76c1dec2010-09-25 11:22:51 +0100107{
Chris Wilson76c1dec2010-09-25 11:22:51 +0100108 int ret;
109
Chris Wilson21dd3732011-01-26 15:55:56 +0000110 ret = i915_gem_wait_for_error(dev);
Chris Wilson76c1dec2010-09-25 11:22:51 +0100111 if (ret)
112 return ret;
113
114 ret = mutex_lock_interruptible(&dev->struct_mutex);
115 if (ret)
116 return ret;
117
Chris Wilson23bc5982010-09-29 16:10:57 +0100118 WARN_ON(i915_verify_lists(dev));
Chris Wilson76c1dec2010-09-25 11:22:51 +0100119 return 0;
120}
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100121
Chris Wilson7d1c4802010-08-07 21:45:03 +0100122static inline bool
Chris Wilson05394f32010-11-08 19:18:58 +0000123i915_gem_object_is_inactive(struct drm_i915_gem_object *obj)
Chris Wilson7d1c4802010-08-07 21:45:03 +0100124{
Chris Wilson05394f32010-11-08 19:18:58 +0000125 return obj->gtt_space && !obj->active && obj->pin_count == 0;
Chris Wilson7d1c4802010-08-07 21:45:03 +0100126}
127
Chris Wilson20217462010-11-23 15:26:33 +0000128void i915_gem_do_init(struct drm_device *dev,
129 unsigned long start,
130 unsigned long mappable_end,
131 unsigned long end)
Jesse Barnes79e53942008-11-07 14:24:08 -0800132{
133 drm_i915_private_t *dev_priv = dev->dev_private;
134
Chris Wilsonbee4a182011-01-21 10:54:32 +0000135 drm_mm_init(&dev_priv->mm.gtt_space, start, end - start);
Jesse Barnes79e53942008-11-07 14:24:08 -0800136
Chris Wilsonbee4a182011-01-21 10:54:32 +0000137 dev_priv->mm.gtt_start = start;
138 dev_priv->mm.gtt_mappable_end = mappable_end;
139 dev_priv->mm.gtt_end = end;
Chris Wilson73aa8082010-09-30 11:46:12 +0100140 dev_priv->mm.gtt_total = end - start;
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200141 dev_priv->mm.mappable_gtt_total = min(end, mappable_end) - start;
Chris Wilsonbee4a182011-01-21 10:54:32 +0000142
143 /* Take over this portion of the GTT */
144 intel_gtt_clear_range(start / PAGE_SIZE, (end-start) / PAGE_SIZE);
Jesse Barnes79e53942008-11-07 14:24:08 -0800145}
Keith Packard6dbe2772008-10-14 21:41:13 -0700146
Eric Anholt673a3942008-07-30 12:06:12 -0700147int
148i915_gem_init_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000149 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700150{
Eric Anholt673a3942008-07-30 12:06:12 -0700151 struct drm_i915_gem_init *args = data;
Chris Wilson20217462010-11-23 15:26:33 +0000152
153 if (args->gtt_start >= args->gtt_end ||
154 (args->gtt_end | args->gtt_start) & (PAGE_SIZE - 1))
155 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -0700156
157 mutex_lock(&dev->struct_mutex);
Chris Wilson20217462010-11-23 15:26:33 +0000158 i915_gem_do_init(dev, args->gtt_start, args->gtt_end, args->gtt_end);
Eric Anholt673a3942008-07-30 12:06:12 -0700159 mutex_unlock(&dev->struct_mutex);
160
Chris Wilson20217462010-11-23 15:26:33 +0000161 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700162}
163
Eric Anholt5a125c32008-10-22 21:40:13 -0700164int
165i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000166 struct drm_file *file)
Eric Anholt5a125c32008-10-22 21:40:13 -0700167{
Chris Wilson73aa8082010-09-30 11:46:12 +0100168 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt5a125c32008-10-22 21:40:13 -0700169 struct drm_i915_gem_get_aperture *args = data;
Chris Wilson6299f992010-11-24 12:23:44 +0000170 struct drm_i915_gem_object *obj;
171 size_t pinned;
Eric Anholt5a125c32008-10-22 21:40:13 -0700172
173 if (!(dev->driver->driver_features & DRIVER_GEM))
174 return -ENODEV;
175
Chris Wilson6299f992010-11-24 12:23:44 +0000176 pinned = 0;
Chris Wilson73aa8082010-09-30 11:46:12 +0100177 mutex_lock(&dev->struct_mutex);
Chris Wilson6299f992010-11-24 12:23:44 +0000178 list_for_each_entry(obj, &dev_priv->mm.pinned_list, mm_list)
179 pinned += obj->gtt_space->size;
Chris Wilson73aa8082010-09-30 11:46:12 +0100180 mutex_unlock(&dev->struct_mutex);
Eric Anholt5a125c32008-10-22 21:40:13 -0700181
Chris Wilson6299f992010-11-24 12:23:44 +0000182 args->aper_size = dev_priv->mm.gtt_total;
183 args->aper_available_size = args->aper_size -pinned;
184
Eric Anholt5a125c32008-10-22 21:40:13 -0700185 return 0;
186}
187
Dave Airlieff72145b2011-02-07 12:16:14 +1000188static int
189i915_gem_create(struct drm_file *file,
190 struct drm_device *dev,
191 uint64_t size,
192 uint32_t *handle_p)
Eric Anholt673a3942008-07-30 12:06:12 -0700193{
Chris Wilson05394f32010-11-08 19:18:58 +0000194 struct drm_i915_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300195 int ret;
196 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700197
Dave Airlieff72145b2011-02-07 12:16:14 +1000198 size = roundup(size, PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -0700199
200 /* Allocate the new object */
Dave Airlieff72145b2011-02-07 12:16:14 +1000201 obj = i915_gem_alloc_object(dev, size);
Eric Anholt673a3942008-07-30 12:06:12 -0700202 if (obj == NULL)
203 return -ENOMEM;
204
Chris Wilson05394f32010-11-08 19:18:58 +0000205 ret = drm_gem_handle_create(file, &obj->base, &handle);
Chris Wilson1dfd9752010-09-06 14:44:14 +0100206 if (ret) {
Chris Wilson05394f32010-11-08 19:18:58 +0000207 drm_gem_object_release(&obj->base);
208 i915_gem_info_remove_obj(dev->dev_private, obj->base.size);
Chris Wilson202f2fe2010-10-14 13:20:40 +0100209 kfree(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700210 return ret;
Chris Wilson1dfd9752010-09-06 14:44:14 +0100211 }
212
Chris Wilson202f2fe2010-10-14 13:20:40 +0100213 /* drop reference from allocate - handle holds it now */
Chris Wilson05394f32010-11-08 19:18:58 +0000214 drm_gem_object_unreference(&obj->base);
Chris Wilson202f2fe2010-10-14 13:20:40 +0100215 trace_i915_gem_object_create(obj);
216
Dave Airlieff72145b2011-02-07 12:16:14 +1000217 *handle_p = handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700218 return 0;
219}
220
Dave Airlieff72145b2011-02-07 12:16:14 +1000221int
222i915_gem_dumb_create(struct drm_file *file,
223 struct drm_device *dev,
224 struct drm_mode_create_dumb *args)
225{
226 /* have to work out size/pitch and return them */
Chris Wilsoned0291f2011-03-19 08:21:45 +0000227 args->pitch = ALIGN(args->width * ((args->bpp + 7) / 8), 64);
Dave Airlieff72145b2011-02-07 12:16:14 +1000228 args->size = args->pitch * args->height;
229 return i915_gem_create(file, dev,
230 args->size, &args->handle);
231}
232
233int i915_gem_dumb_destroy(struct drm_file *file,
234 struct drm_device *dev,
235 uint32_t handle)
236{
237 return drm_gem_handle_delete(file, handle);
238}
239
240/**
241 * Creates a new mm object and returns a handle to it.
242 */
243int
244i915_gem_create_ioctl(struct drm_device *dev, void *data,
245 struct drm_file *file)
246{
247 struct drm_i915_gem_create *args = data;
248 return i915_gem_create(file, dev,
249 args->size, &args->handle);
250}
251
Chris Wilson05394f32010-11-08 19:18:58 +0000252static int i915_gem_object_needs_bit17_swizzle(struct drm_i915_gem_object *obj)
Eric Anholt280b7132009-03-12 16:56:27 -0700253{
Chris Wilson05394f32010-11-08 19:18:58 +0000254 drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
Eric Anholt280b7132009-03-12 16:56:27 -0700255
256 return dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_9_10_17 &&
Chris Wilson05394f32010-11-08 19:18:58 +0000257 obj->tiling_mode != I915_TILING_NONE;
Eric Anholt280b7132009-03-12 16:56:27 -0700258}
259
Chris Wilson99a03df2010-05-27 14:15:34 +0100260static inline void
Eric Anholt40123c12009-03-09 13:42:30 -0700261slow_shmem_copy(struct page *dst_page,
262 int dst_offset,
263 struct page *src_page,
264 int src_offset,
265 int length)
266{
267 char *dst_vaddr, *src_vaddr;
268
Chris Wilson99a03df2010-05-27 14:15:34 +0100269 dst_vaddr = kmap(dst_page);
270 src_vaddr = kmap(src_page);
Eric Anholt40123c12009-03-09 13:42:30 -0700271
272 memcpy(dst_vaddr + dst_offset, src_vaddr + src_offset, length);
273
Chris Wilson99a03df2010-05-27 14:15:34 +0100274 kunmap(src_page);
275 kunmap(dst_page);
Eric Anholt40123c12009-03-09 13:42:30 -0700276}
277
Chris Wilson99a03df2010-05-27 14:15:34 +0100278static inline void
Eric Anholt280b7132009-03-12 16:56:27 -0700279slow_shmem_bit17_copy(struct page *gpu_page,
280 int gpu_offset,
281 struct page *cpu_page,
282 int cpu_offset,
283 int length,
284 int is_read)
285{
286 char *gpu_vaddr, *cpu_vaddr;
287
288 /* Use the unswizzled path if this page isn't affected. */
289 if ((page_to_phys(gpu_page) & (1 << 17)) == 0) {
290 if (is_read)
291 return slow_shmem_copy(cpu_page, cpu_offset,
292 gpu_page, gpu_offset, length);
293 else
294 return slow_shmem_copy(gpu_page, gpu_offset,
295 cpu_page, cpu_offset, length);
296 }
297
Chris Wilson99a03df2010-05-27 14:15:34 +0100298 gpu_vaddr = kmap(gpu_page);
299 cpu_vaddr = kmap(cpu_page);
Eric Anholt280b7132009-03-12 16:56:27 -0700300
301 /* Copy the data, XORing A6 with A17 (1). The user already knows he's
302 * XORing with the other bits (A9 for Y, A9 and A10 for X)
303 */
304 while (length > 0) {
305 int cacheline_end = ALIGN(gpu_offset + 1, 64);
306 int this_length = min(cacheline_end - gpu_offset, length);
307 int swizzled_gpu_offset = gpu_offset ^ 64;
308
309 if (is_read) {
310 memcpy(cpu_vaddr + cpu_offset,
311 gpu_vaddr + swizzled_gpu_offset,
312 this_length);
313 } else {
314 memcpy(gpu_vaddr + swizzled_gpu_offset,
315 cpu_vaddr + cpu_offset,
316 this_length);
317 }
318 cpu_offset += this_length;
319 gpu_offset += this_length;
320 length -= this_length;
321 }
322
Chris Wilson99a03df2010-05-27 14:15:34 +0100323 kunmap(cpu_page);
324 kunmap(gpu_page);
Eric Anholt280b7132009-03-12 16:56:27 -0700325}
326
Eric Anholt673a3942008-07-30 12:06:12 -0700327/**
Eric Anholteb014592009-03-10 11:44:52 -0700328 * This is the fast shmem pread path, which attempts to copy_from_user directly
329 * from the backing pages of the object to the user's address space. On a
330 * fault, it fails so we can fall back to i915_gem_shmem_pwrite_slow().
331 */
332static int
Chris Wilson05394f32010-11-08 19:18:58 +0000333i915_gem_shmem_pread_fast(struct drm_device *dev,
334 struct drm_i915_gem_object *obj,
Eric Anholteb014592009-03-10 11:44:52 -0700335 struct drm_i915_gem_pread *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000336 struct drm_file *file)
Eric Anholteb014592009-03-10 11:44:52 -0700337{
Chris Wilson05394f32010-11-08 19:18:58 +0000338 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Eric Anholteb014592009-03-10 11:44:52 -0700339 ssize_t remain;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100340 loff_t offset;
Eric Anholteb014592009-03-10 11:44:52 -0700341 char __user *user_data;
342 int page_offset, page_length;
Eric Anholteb014592009-03-10 11:44:52 -0700343
344 user_data = (char __user *) (uintptr_t) args->data_ptr;
345 remain = args->size;
346
Eric Anholteb014592009-03-10 11:44:52 -0700347 offset = args->offset;
348
349 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100350 struct page *page;
351 char *vaddr;
352 int ret;
353
Eric Anholteb014592009-03-10 11:44:52 -0700354 /* Operation in this page
355 *
Eric Anholteb014592009-03-10 11:44:52 -0700356 * page_offset = offset within page
357 * page_length = bytes to copy for this page
358 */
Eric Anholteb014592009-03-10 11:44:52 -0700359 page_offset = offset & (PAGE_SIZE-1);
360 page_length = remain;
361 if ((page_offset + remain) > PAGE_SIZE)
362 page_length = PAGE_SIZE - page_offset;
363
Chris Wilsone5281cc2010-10-28 13:45:36 +0100364 page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
365 GFP_HIGHUSER | __GFP_RECLAIMABLE);
366 if (IS_ERR(page))
367 return PTR_ERR(page);
368
369 vaddr = kmap_atomic(page);
370 ret = __copy_to_user_inatomic(user_data,
371 vaddr + page_offset,
372 page_length);
373 kunmap_atomic(vaddr);
374
375 mark_page_accessed(page);
376 page_cache_release(page);
377 if (ret)
Chris Wilson4f27b752010-10-14 15:26:45 +0100378 return -EFAULT;
Eric Anholteb014592009-03-10 11:44:52 -0700379
380 remain -= page_length;
381 user_data += page_length;
382 offset += page_length;
383 }
384
Chris Wilson4f27b752010-10-14 15:26:45 +0100385 return 0;
Eric Anholteb014592009-03-10 11:44:52 -0700386}
387
388/**
389 * This is the fallback shmem pread path, which allocates temporary storage
390 * in kernel space to copy_to_user into outside of the struct_mutex, so we
391 * can copy out of the object's backing pages while holding the struct mutex
392 * and not take page faults.
393 */
394static int
Chris Wilson05394f32010-11-08 19:18:58 +0000395i915_gem_shmem_pread_slow(struct drm_device *dev,
396 struct drm_i915_gem_object *obj,
Eric Anholteb014592009-03-10 11:44:52 -0700397 struct drm_i915_gem_pread *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000398 struct drm_file *file)
Eric Anholteb014592009-03-10 11:44:52 -0700399{
Chris Wilson05394f32010-11-08 19:18:58 +0000400 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Eric Anholteb014592009-03-10 11:44:52 -0700401 struct mm_struct *mm = current->mm;
402 struct page **user_pages;
403 ssize_t remain;
404 loff_t offset, pinned_pages, i;
405 loff_t first_data_page, last_data_page, num_pages;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100406 int shmem_page_offset;
407 int data_page_index, data_page_offset;
Eric Anholteb014592009-03-10 11:44:52 -0700408 int page_length;
409 int ret;
410 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700411 int do_bit17_swizzling;
Eric Anholteb014592009-03-10 11:44:52 -0700412
413 remain = args->size;
414
415 /* Pin the user pages containing the data. We can't fault while
416 * holding the struct mutex, yet we want to hold it while
417 * dereferencing the user data.
418 */
419 first_data_page = data_ptr / PAGE_SIZE;
420 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
421 num_pages = last_data_page - first_data_page + 1;
422
Chris Wilson4f27b752010-10-14 15:26:45 +0100423 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
Eric Anholteb014592009-03-10 11:44:52 -0700424 if (user_pages == NULL)
425 return -ENOMEM;
426
Chris Wilson4f27b752010-10-14 15:26:45 +0100427 mutex_unlock(&dev->struct_mutex);
Eric Anholteb014592009-03-10 11:44:52 -0700428 down_read(&mm->mmap_sem);
429 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
Eric Anholte5e9ecd2009-04-07 16:01:22 -0700430 num_pages, 1, 0, user_pages, NULL);
Eric Anholteb014592009-03-10 11:44:52 -0700431 up_read(&mm->mmap_sem);
Chris Wilson4f27b752010-10-14 15:26:45 +0100432 mutex_lock(&dev->struct_mutex);
Eric Anholteb014592009-03-10 11:44:52 -0700433 if (pinned_pages < num_pages) {
434 ret = -EFAULT;
Chris Wilson4f27b752010-10-14 15:26:45 +0100435 goto out;
Eric Anholteb014592009-03-10 11:44:52 -0700436 }
437
Chris Wilson4f27b752010-10-14 15:26:45 +0100438 ret = i915_gem_object_set_cpu_read_domain_range(obj,
439 args->offset,
Eric Anholteb014592009-03-10 11:44:52 -0700440 args->size);
Chris Wilson4f27b752010-10-14 15:26:45 +0100441 if (ret)
442 goto out;
443
444 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700445
Eric Anholteb014592009-03-10 11:44:52 -0700446 offset = args->offset;
447
448 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100449 struct page *page;
450
Eric Anholteb014592009-03-10 11:44:52 -0700451 /* Operation in this page
452 *
Eric Anholteb014592009-03-10 11:44:52 -0700453 * shmem_page_offset = offset within page in shmem file
454 * data_page_index = page number in get_user_pages return
455 * data_page_offset = offset with data_page_index page.
456 * page_length = bytes to copy for this page
457 */
Eric Anholteb014592009-03-10 11:44:52 -0700458 shmem_page_offset = offset & ~PAGE_MASK;
459 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
460 data_page_offset = data_ptr & ~PAGE_MASK;
461
462 page_length = remain;
463 if ((shmem_page_offset + page_length) > PAGE_SIZE)
464 page_length = PAGE_SIZE - shmem_page_offset;
465 if ((data_page_offset + page_length) > PAGE_SIZE)
466 page_length = PAGE_SIZE - data_page_offset;
467
Chris Wilsone5281cc2010-10-28 13:45:36 +0100468 page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
469 GFP_HIGHUSER | __GFP_RECLAIMABLE);
470 if (IS_ERR(page))
471 return PTR_ERR(page);
472
Eric Anholt280b7132009-03-12 16:56:27 -0700473 if (do_bit17_swizzling) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100474 slow_shmem_bit17_copy(page,
Eric Anholt280b7132009-03-12 16:56:27 -0700475 shmem_page_offset,
Chris Wilson99a03df2010-05-27 14:15:34 +0100476 user_pages[data_page_index],
477 data_page_offset,
478 page_length,
479 1);
480 } else {
481 slow_shmem_copy(user_pages[data_page_index],
482 data_page_offset,
Chris Wilsone5281cc2010-10-28 13:45:36 +0100483 page,
Chris Wilson99a03df2010-05-27 14:15:34 +0100484 shmem_page_offset,
485 page_length);
Eric Anholt280b7132009-03-12 16:56:27 -0700486 }
Eric Anholteb014592009-03-10 11:44:52 -0700487
Chris Wilsone5281cc2010-10-28 13:45:36 +0100488 mark_page_accessed(page);
489 page_cache_release(page);
490
Eric Anholteb014592009-03-10 11:44:52 -0700491 remain -= page_length;
492 data_ptr += page_length;
493 offset += page_length;
494 }
495
Chris Wilson4f27b752010-10-14 15:26:45 +0100496out:
Eric Anholteb014592009-03-10 11:44:52 -0700497 for (i = 0; i < pinned_pages; i++) {
498 SetPageDirty(user_pages[i]);
Chris Wilsone5281cc2010-10-28 13:45:36 +0100499 mark_page_accessed(user_pages[i]);
Eric Anholteb014592009-03-10 11:44:52 -0700500 page_cache_release(user_pages[i]);
501 }
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700502 drm_free_large(user_pages);
Eric Anholteb014592009-03-10 11:44:52 -0700503
504 return ret;
505}
506
Eric Anholt673a3942008-07-30 12:06:12 -0700507/**
508 * Reads data from the object referenced by handle.
509 *
510 * On error, the contents of *data are undefined.
511 */
512int
513i915_gem_pread_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000514 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700515{
516 struct drm_i915_gem_pread *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +0000517 struct drm_i915_gem_object *obj;
Chris Wilson35b62a82010-09-26 20:23:38 +0100518 int ret = 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700519
Chris Wilson51311d02010-11-17 09:10:42 +0000520 if (args->size == 0)
521 return 0;
522
523 if (!access_ok(VERIFY_WRITE,
524 (char __user *)(uintptr_t)args->data_ptr,
525 args->size))
526 return -EFAULT;
527
528 ret = fault_in_pages_writeable((char __user *)(uintptr_t)args->data_ptr,
529 args->size);
530 if (ret)
531 return -EFAULT;
532
Chris Wilson4f27b752010-10-14 15:26:45 +0100533 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100534 if (ret)
Chris Wilson4f27b752010-10-14 15:26:45 +0100535 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700536
Chris Wilson05394f32010-11-08 19:18:58 +0000537 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +0000538 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100539 ret = -ENOENT;
540 goto unlock;
Chris Wilson4f27b752010-10-14 15:26:45 +0100541 }
Eric Anholt673a3942008-07-30 12:06:12 -0700542
Chris Wilson7dcd2492010-09-26 20:21:44 +0100543 /* Bounds check source. */
Chris Wilson05394f32010-11-08 19:18:58 +0000544 if (args->offset > obj->base.size ||
545 args->size > obj->base.size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +0100546 ret = -EINVAL;
Chris Wilson35b62a82010-09-26 20:23:38 +0100547 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +0100548 }
549
Chris Wilsondb53a302011-02-03 11:57:46 +0000550 trace_i915_gem_object_pread(obj, args->offset, args->size);
551
Chris Wilson4f27b752010-10-14 15:26:45 +0100552 ret = i915_gem_object_set_cpu_read_domain_range(obj,
553 args->offset,
554 args->size);
555 if (ret)
Chris Wilsone5281cc2010-10-28 13:45:36 +0100556 goto out;
Chris Wilson4f27b752010-10-14 15:26:45 +0100557
558 ret = -EFAULT;
559 if (!i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilson05394f32010-11-08 19:18:58 +0000560 ret = i915_gem_shmem_pread_fast(dev, obj, args, file);
Chris Wilson4f27b752010-10-14 15:26:45 +0100561 if (ret == -EFAULT)
Chris Wilson05394f32010-11-08 19:18:58 +0000562 ret = i915_gem_shmem_pread_slow(dev, obj, args, file);
Eric Anholt673a3942008-07-30 12:06:12 -0700563
Chris Wilson35b62a82010-09-26 20:23:38 +0100564out:
Chris Wilson05394f32010-11-08 19:18:58 +0000565 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100566unlock:
Chris Wilson4f27b752010-10-14 15:26:45 +0100567 mutex_unlock(&dev->struct_mutex);
Eric Anholteb014592009-03-10 11:44:52 -0700568 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700569}
570
Keith Packard0839ccb2008-10-30 19:38:48 -0700571/* This is the fast write path which cannot handle
572 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700573 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700574
Keith Packard0839ccb2008-10-30 19:38:48 -0700575static inline int
576fast_user_write(struct io_mapping *mapping,
577 loff_t page_base, int page_offset,
578 char __user *user_data,
579 int length)
580{
581 char *vaddr_atomic;
582 unsigned long unwritten;
583
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700584 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
Keith Packard0839ccb2008-10-30 19:38:48 -0700585 unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
586 user_data, length);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700587 io_mapping_unmap_atomic(vaddr_atomic);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100588 return unwritten;
Keith Packard0839ccb2008-10-30 19:38:48 -0700589}
590
591/* Here's the write path which can sleep for
592 * page faults
593 */
594
Chris Wilsonab34c222010-05-27 14:15:35 +0100595static inline void
Eric Anholt3de09aa2009-03-09 09:42:23 -0700596slow_kernel_write(struct io_mapping *mapping,
597 loff_t gtt_base, int gtt_offset,
598 struct page *user_page, int user_offset,
599 int length)
Keith Packard0839ccb2008-10-30 19:38:48 -0700600{
Chris Wilsonab34c222010-05-27 14:15:35 +0100601 char __iomem *dst_vaddr;
602 char *src_vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -0700603
Chris Wilsonab34c222010-05-27 14:15:35 +0100604 dst_vaddr = io_mapping_map_wc(mapping, gtt_base);
605 src_vaddr = kmap(user_page);
606
607 memcpy_toio(dst_vaddr + gtt_offset,
608 src_vaddr + user_offset,
609 length);
610
611 kunmap(user_page);
612 io_mapping_unmap(dst_vaddr);
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700613}
614
Eric Anholt3de09aa2009-03-09 09:42:23 -0700615/**
616 * This is the fast pwrite path, where we copy the data directly from the
617 * user into the GTT, uncached.
618 */
Eric Anholt673a3942008-07-30 12:06:12 -0700619static int
Chris Wilson05394f32010-11-08 19:18:58 +0000620i915_gem_gtt_pwrite_fast(struct drm_device *dev,
621 struct drm_i915_gem_object *obj,
Eric Anholt3de09aa2009-03-09 09:42:23 -0700622 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000623 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700624{
Keith Packard0839ccb2008-10-30 19:38:48 -0700625 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700626 ssize_t remain;
Keith Packard0839ccb2008-10-30 19:38:48 -0700627 loff_t offset, page_base;
Eric Anholt673a3942008-07-30 12:06:12 -0700628 char __user *user_data;
Keith Packard0839ccb2008-10-30 19:38:48 -0700629 int page_offset, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700630
631 user_data = (char __user *) (uintptr_t) args->data_ptr;
632 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700633
Chris Wilson05394f32010-11-08 19:18:58 +0000634 offset = obj->gtt_offset + args->offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700635
636 while (remain > 0) {
637 /* Operation in this page
638 *
Keith Packard0839ccb2008-10-30 19:38:48 -0700639 * page_base = page offset within aperture
640 * page_offset = offset within page
641 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -0700642 */
Keith Packard0839ccb2008-10-30 19:38:48 -0700643 page_base = (offset & ~(PAGE_SIZE-1));
644 page_offset = offset & (PAGE_SIZE-1);
645 page_length = remain;
646 if ((page_offset + remain) > PAGE_SIZE)
647 page_length = PAGE_SIZE - page_offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700648
Keith Packard0839ccb2008-10-30 19:38:48 -0700649 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -0700650 * source page isn't available. Return the error and we'll
651 * retry in the slow path.
Keith Packard0839ccb2008-10-30 19:38:48 -0700652 */
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100653 if (fast_user_write(dev_priv->mm.gtt_mapping, page_base,
654 page_offset, user_data, page_length))
655
656 return -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -0700657
Keith Packard0839ccb2008-10-30 19:38:48 -0700658 remain -= page_length;
659 user_data += page_length;
660 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700661 }
Eric Anholt673a3942008-07-30 12:06:12 -0700662
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100663 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700664}
665
Eric Anholt3de09aa2009-03-09 09:42:23 -0700666/**
667 * This is the fallback GTT pwrite path, which uses get_user_pages to pin
668 * the memory and maps it using kmap_atomic for copying.
669 *
670 * This code resulted in x11perf -rgb10text consuming about 10% more CPU
671 * than using i915_gem_gtt_pwrite_fast on a G45 (32-bit).
672 */
Eric Anholt3043c602008-10-02 12:24:47 -0700673static int
Chris Wilson05394f32010-11-08 19:18:58 +0000674i915_gem_gtt_pwrite_slow(struct drm_device *dev,
675 struct drm_i915_gem_object *obj,
Eric Anholt3de09aa2009-03-09 09:42:23 -0700676 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000677 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700678{
Eric Anholt3de09aa2009-03-09 09:42:23 -0700679 drm_i915_private_t *dev_priv = dev->dev_private;
680 ssize_t remain;
681 loff_t gtt_page_base, offset;
682 loff_t first_data_page, last_data_page, num_pages;
683 loff_t pinned_pages, i;
684 struct page **user_pages;
685 struct mm_struct *mm = current->mm;
686 int gtt_page_offset, data_page_offset, data_page_index, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700687 int ret;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700688 uint64_t data_ptr = args->data_ptr;
689
690 remain = args->size;
691
692 /* Pin the user pages containing the data. We can't fault while
693 * holding the struct mutex, and all of the pwrite implementations
694 * want to hold it while dereferencing the user data.
695 */
696 first_data_page = data_ptr / PAGE_SIZE;
697 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
698 num_pages = last_data_page - first_data_page + 1;
699
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100700 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
Eric Anholt3de09aa2009-03-09 09:42:23 -0700701 if (user_pages == NULL)
702 return -ENOMEM;
703
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100704 mutex_unlock(&dev->struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700705 down_read(&mm->mmap_sem);
706 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
707 num_pages, 0, 0, user_pages, NULL);
708 up_read(&mm->mmap_sem);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100709 mutex_lock(&dev->struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700710 if (pinned_pages < num_pages) {
711 ret = -EFAULT;
712 goto out_unpin_pages;
713 }
714
Chris Wilsond9e86c02010-11-10 16:40:20 +0000715 ret = i915_gem_object_set_to_gtt_domain(obj, true);
716 if (ret)
717 goto out_unpin_pages;
718
719 ret = i915_gem_object_put_fence(obj);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700720 if (ret)
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100721 goto out_unpin_pages;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700722
Chris Wilson05394f32010-11-08 19:18:58 +0000723 offset = obj->gtt_offset + args->offset;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700724
725 while (remain > 0) {
726 /* Operation in this page
727 *
728 * gtt_page_base = page offset within aperture
729 * gtt_page_offset = offset within page in aperture
730 * data_page_index = page number in get_user_pages return
731 * data_page_offset = offset with data_page_index page.
732 * page_length = bytes to copy for this page
733 */
734 gtt_page_base = offset & PAGE_MASK;
735 gtt_page_offset = offset & ~PAGE_MASK;
736 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
737 data_page_offset = data_ptr & ~PAGE_MASK;
738
739 page_length = remain;
740 if ((gtt_page_offset + page_length) > PAGE_SIZE)
741 page_length = PAGE_SIZE - gtt_page_offset;
742 if ((data_page_offset + page_length) > PAGE_SIZE)
743 page_length = PAGE_SIZE - data_page_offset;
744
Chris Wilsonab34c222010-05-27 14:15:35 +0100745 slow_kernel_write(dev_priv->mm.gtt_mapping,
746 gtt_page_base, gtt_page_offset,
747 user_pages[data_page_index],
748 data_page_offset,
749 page_length);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700750
751 remain -= page_length;
752 offset += page_length;
753 data_ptr += page_length;
754 }
755
Eric Anholt3de09aa2009-03-09 09:42:23 -0700756out_unpin_pages:
757 for (i = 0; i < pinned_pages; i++)
758 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700759 drm_free_large(user_pages);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700760
761 return ret;
762}
763
Eric Anholt40123c12009-03-09 13:42:30 -0700764/**
765 * This is the fast shmem pwrite path, which attempts to directly
766 * copy_from_user into the kmapped pages backing the object.
767 */
Eric Anholt673a3942008-07-30 12:06:12 -0700768static int
Chris Wilson05394f32010-11-08 19:18:58 +0000769i915_gem_shmem_pwrite_fast(struct drm_device *dev,
770 struct drm_i915_gem_object *obj,
Eric Anholt40123c12009-03-09 13:42:30 -0700771 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000772 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700773{
Chris Wilson05394f32010-11-08 19:18:58 +0000774 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Eric Anholt40123c12009-03-09 13:42:30 -0700775 ssize_t remain;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100776 loff_t offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700777 char __user *user_data;
778 int page_offset, page_length;
Eric Anholt40123c12009-03-09 13:42:30 -0700779
780 user_data = (char __user *) (uintptr_t) args->data_ptr;
781 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700782
Eric Anholt673a3942008-07-30 12:06:12 -0700783 offset = args->offset;
Chris Wilson05394f32010-11-08 19:18:58 +0000784 obj->dirty = 1;
Eric Anholt673a3942008-07-30 12:06:12 -0700785
Eric Anholt40123c12009-03-09 13:42:30 -0700786 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100787 struct page *page;
788 char *vaddr;
789 int ret;
790
Eric Anholt40123c12009-03-09 13:42:30 -0700791 /* Operation in this page
792 *
Eric Anholt40123c12009-03-09 13:42:30 -0700793 * page_offset = offset within page
794 * page_length = bytes to copy for this page
795 */
Eric Anholt40123c12009-03-09 13:42:30 -0700796 page_offset = offset & (PAGE_SIZE-1);
797 page_length = remain;
798 if ((page_offset + remain) > PAGE_SIZE)
799 page_length = PAGE_SIZE - page_offset;
800
Chris Wilsone5281cc2010-10-28 13:45:36 +0100801 page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
802 GFP_HIGHUSER | __GFP_RECLAIMABLE);
803 if (IS_ERR(page))
804 return PTR_ERR(page);
805
806 vaddr = kmap_atomic(page, KM_USER0);
807 ret = __copy_from_user_inatomic(vaddr + page_offset,
808 user_data,
809 page_length);
810 kunmap_atomic(vaddr, KM_USER0);
811
812 set_page_dirty(page);
813 mark_page_accessed(page);
814 page_cache_release(page);
815
816 /* If we get a fault while copying data, then (presumably) our
817 * source page isn't available. Return the error and we'll
818 * retry in the slow path.
819 */
820 if (ret)
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100821 return -EFAULT;
Eric Anholt40123c12009-03-09 13:42:30 -0700822
823 remain -= page_length;
824 user_data += page_length;
825 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700826 }
827
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100828 return 0;
Eric Anholt40123c12009-03-09 13:42:30 -0700829}
830
831/**
832 * This is the fallback shmem pwrite path, which uses get_user_pages to pin
833 * the memory and maps it using kmap_atomic for copying.
834 *
835 * This avoids taking mmap_sem for faulting on the user's address while the
836 * struct_mutex is held.
837 */
838static int
Chris Wilson05394f32010-11-08 19:18:58 +0000839i915_gem_shmem_pwrite_slow(struct drm_device *dev,
840 struct drm_i915_gem_object *obj,
Eric Anholt40123c12009-03-09 13:42:30 -0700841 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000842 struct drm_file *file)
Eric Anholt40123c12009-03-09 13:42:30 -0700843{
Chris Wilson05394f32010-11-08 19:18:58 +0000844 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Eric Anholt40123c12009-03-09 13:42:30 -0700845 struct mm_struct *mm = current->mm;
846 struct page **user_pages;
847 ssize_t remain;
848 loff_t offset, pinned_pages, i;
849 loff_t first_data_page, last_data_page, num_pages;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100850 int shmem_page_offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700851 int data_page_index, data_page_offset;
852 int page_length;
853 int ret;
854 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700855 int do_bit17_swizzling;
Eric Anholt40123c12009-03-09 13:42:30 -0700856
857 remain = args->size;
858
859 /* Pin the user pages containing the data. We can't fault while
860 * holding the struct mutex, and all of the pwrite implementations
861 * want to hold it while dereferencing the user data.
862 */
863 first_data_page = data_ptr / PAGE_SIZE;
864 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
865 num_pages = last_data_page - first_data_page + 1;
866
Chris Wilson4f27b752010-10-14 15:26:45 +0100867 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
Eric Anholt40123c12009-03-09 13:42:30 -0700868 if (user_pages == NULL)
869 return -ENOMEM;
870
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100871 mutex_unlock(&dev->struct_mutex);
Eric Anholt40123c12009-03-09 13:42:30 -0700872 down_read(&mm->mmap_sem);
873 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
874 num_pages, 0, 0, user_pages, NULL);
875 up_read(&mm->mmap_sem);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100876 mutex_lock(&dev->struct_mutex);
Eric Anholt40123c12009-03-09 13:42:30 -0700877 if (pinned_pages < num_pages) {
878 ret = -EFAULT;
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100879 goto out;
Eric Anholt40123c12009-03-09 13:42:30 -0700880 }
881
Eric Anholt40123c12009-03-09 13:42:30 -0700882 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100883 if (ret)
884 goto out;
885
886 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700887
Eric Anholt40123c12009-03-09 13:42:30 -0700888 offset = args->offset;
Chris Wilson05394f32010-11-08 19:18:58 +0000889 obj->dirty = 1;
Eric Anholt40123c12009-03-09 13:42:30 -0700890
891 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100892 struct page *page;
893
Eric Anholt40123c12009-03-09 13:42:30 -0700894 /* Operation in this page
895 *
Eric Anholt40123c12009-03-09 13:42:30 -0700896 * shmem_page_offset = offset within page in shmem file
897 * data_page_index = page number in get_user_pages return
898 * data_page_offset = offset with data_page_index page.
899 * page_length = bytes to copy for this page
900 */
Eric Anholt40123c12009-03-09 13:42:30 -0700901 shmem_page_offset = offset & ~PAGE_MASK;
902 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
903 data_page_offset = data_ptr & ~PAGE_MASK;
904
905 page_length = remain;
906 if ((shmem_page_offset + page_length) > PAGE_SIZE)
907 page_length = PAGE_SIZE - shmem_page_offset;
908 if ((data_page_offset + page_length) > PAGE_SIZE)
909 page_length = PAGE_SIZE - data_page_offset;
910
Chris Wilsone5281cc2010-10-28 13:45:36 +0100911 page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
912 GFP_HIGHUSER | __GFP_RECLAIMABLE);
913 if (IS_ERR(page)) {
914 ret = PTR_ERR(page);
915 goto out;
916 }
917
Eric Anholt280b7132009-03-12 16:56:27 -0700918 if (do_bit17_swizzling) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100919 slow_shmem_bit17_copy(page,
Eric Anholt280b7132009-03-12 16:56:27 -0700920 shmem_page_offset,
921 user_pages[data_page_index],
922 data_page_offset,
Chris Wilson99a03df2010-05-27 14:15:34 +0100923 page_length,
924 0);
925 } else {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100926 slow_shmem_copy(page,
Chris Wilson99a03df2010-05-27 14:15:34 +0100927 shmem_page_offset,
928 user_pages[data_page_index],
929 data_page_offset,
930 page_length);
Eric Anholt280b7132009-03-12 16:56:27 -0700931 }
Eric Anholt40123c12009-03-09 13:42:30 -0700932
Chris Wilsone5281cc2010-10-28 13:45:36 +0100933 set_page_dirty(page);
934 mark_page_accessed(page);
935 page_cache_release(page);
936
Eric Anholt40123c12009-03-09 13:42:30 -0700937 remain -= page_length;
938 data_ptr += page_length;
939 offset += page_length;
940 }
941
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100942out:
Eric Anholt40123c12009-03-09 13:42:30 -0700943 for (i = 0; i < pinned_pages; i++)
944 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700945 drm_free_large(user_pages);
Eric Anholt40123c12009-03-09 13:42:30 -0700946
947 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700948}
949
950/**
951 * Writes data to the object referenced by handle.
952 *
953 * On error, the contents of the buffer that were to be modified are undefined.
954 */
955int
956i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100957 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700958{
959 struct drm_i915_gem_pwrite *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +0000960 struct drm_i915_gem_object *obj;
Chris Wilson51311d02010-11-17 09:10:42 +0000961 int ret;
962
963 if (args->size == 0)
964 return 0;
965
966 if (!access_ok(VERIFY_READ,
967 (char __user *)(uintptr_t)args->data_ptr,
968 args->size))
969 return -EFAULT;
970
971 ret = fault_in_pages_readable((char __user *)(uintptr_t)args->data_ptr,
972 args->size);
973 if (ret)
974 return -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -0700975
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100976 ret = i915_mutex_lock_interruptible(dev);
977 if (ret)
978 return ret;
979
Chris Wilson05394f32010-11-08 19:18:58 +0000980 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +0000981 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100982 ret = -ENOENT;
983 goto unlock;
984 }
Eric Anholt673a3942008-07-30 12:06:12 -0700985
Chris Wilson7dcd2492010-09-26 20:21:44 +0100986 /* Bounds check destination. */
Chris Wilson05394f32010-11-08 19:18:58 +0000987 if (args->offset > obj->base.size ||
988 args->size > obj->base.size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +0100989 ret = -EINVAL;
Chris Wilson35b62a82010-09-26 20:23:38 +0100990 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +0100991 }
992
Chris Wilsondb53a302011-02-03 11:57:46 +0000993 trace_i915_gem_object_pwrite(obj, args->offset, args->size);
994
Eric Anholt673a3942008-07-30 12:06:12 -0700995 /* We can only do the GTT pwrite on untiled buffers, as otherwise
996 * it would end up going through the fenced access, and we'll get
997 * different detiling behavior between reading and writing.
998 * pread/pwrite currently are reading and writing from the CPU
999 * perspective, requiring manual detiling by the client.
1000 */
Chris Wilson05394f32010-11-08 19:18:58 +00001001 if (obj->phys_obj)
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001002 ret = i915_gem_phys_pwrite(dev, obj, args, file);
Chris Wilsond9e86c02010-11-10 16:40:20 +00001003 else if (obj->gtt_space &&
Chris Wilson05394f32010-11-08 19:18:58 +00001004 obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01001005 ret = i915_gem_object_pin(obj, 0, true);
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001006 if (ret)
1007 goto out;
1008
Chris Wilsond9e86c02010-11-10 16:40:20 +00001009 ret = i915_gem_object_set_to_gtt_domain(obj, true);
1010 if (ret)
1011 goto out_unpin;
1012
1013 ret = i915_gem_object_put_fence(obj);
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001014 if (ret)
1015 goto out_unpin;
1016
1017 ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file);
1018 if (ret == -EFAULT)
1019 ret = i915_gem_gtt_pwrite_slow(dev, obj, args, file);
1020
1021out_unpin:
1022 i915_gem_object_unpin(obj);
Eric Anholt40123c12009-03-09 13:42:30 -07001023 } else {
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001024 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
1025 if (ret)
Chris Wilsone5281cc2010-10-28 13:45:36 +01001026 goto out;
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001027
1028 ret = -EFAULT;
1029 if (!i915_gem_object_needs_bit17_swizzle(obj))
1030 ret = i915_gem_shmem_pwrite_fast(dev, obj, args, file);
1031 if (ret == -EFAULT)
1032 ret = i915_gem_shmem_pwrite_slow(dev, obj, args, file);
Eric Anholt40123c12009-03-09 13:42:30 -07001033 }
Eric Anholt673a3942008-07-30 12:06:12 -07001034
Chris Wilson35b62a82010-09-26 20:23:38 +01001035out:
Chris Wilson05394f32010-11-08 19:18:58 +00001036 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001037unlock:
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001038 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07001039 return ret;
1040}
1041
1042/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001043 * Called when user space prepares to use an object with the CPU, either
1044 * through the mmap ioctl's mapping or a GTT mapping.
Eric Anholt673a3942008-07-30 12:06:12 -07001045 */
1046int
1047i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001048 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001049{
1050 struct drm_i915_gem_set_domain *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001051 struct drm_i915_gem_object *obj;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001052 uint32_t read_domains = args->read_domains;
1053 uint32_t write_domain = args->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07001054 int ret;
1055
1056 if (!(dev->driver->driver_features & DRIVER_GEM))
1057 return -ENODEV;
1058
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001059 /* Only handle setting domains to types used by the CPU. */
Chris Wilson21d509e2009-06-06 09:46:02 +01001060 if (write_domain & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001061 return -EINVAL;
1062
Chris Wilson21d509e2009-06-06 09:46:02 +01001063 if (read_domains & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001064 return -EINVAL;
1065
1066 /* Having something in the write domain implies it's in the read
1067 * domain, and only that read domain. Enforce that in the request.
1068 */
1069 if (write_domain != 0 && read_domains != write_domain)
1070 return -EINVAL;
1071
Chris Wilson76c1dec2010-09-25 11:22:51 +01001072 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001073 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001074 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001075
Chris Wilson05394f32010-11-08 19:18:58 +00001076 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00001077 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001078 ret = -ENOENT;
1079 goto unlock;
Chris Wilson76c1dec2010-09-25 11:22:51 +01001080 }
Jesse Barnes652c3932009-08-17 13:31:43 -07001081
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001082 if (read_domains & I915_GEM_DOMAIN_GTT) {
1083 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
Eric Anholt02354392008-11-26 13:58:13 -08001084
1085 /* Silently promote "you're not bound, there was nothing to do"
1086 * to success, since the client was just asking us to
1087 * make sure everything was done.
1088 */
1089 if (ret == -EINVAL)
1090 ret = 0;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001091 } else {
Eric Anholte47c68e2008-11-14 13:35:19 -08001092 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001093 }
1094
Chris Wilson05394f32010-11-08 19:18:58 +00001095 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001096unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07001097 mutex_unlock(&dev->struct_mutex);
1098 return ret;
1099}
1100
1101/**
1102 * Called when user space has done writes to this buffer
1103 */
1104int
1105i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001106 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001107{
1108 struct drm_i915_gem_sw_finish *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001109 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001110 int ret = 0;
1111
1112 if (!(dev->driver->driver_features & DRIVER_GEM))
1113 return -ENODEV;
1114
Chris Wilson76c1dec2010-09-25 11:22:51 +01001115 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001116 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001117 return ret;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001118
Chris Wilson05394f32010-11-08 19:18:58 +00001119 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00001120 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001121 ret = -ENOENT;
1122 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07001123 }
1124
Eric Anholt673a3942008-07-30 12:06:12 -07001125 /* Pinned buffers may be scanout, so flush the cache */
Chris Wilson05394f32010-11-08 19:18:58 +00001126 if (obj->pin_count)
Eric Anholte47c68e2008-11-14 13:35:19 -08001127 i915_gem_object_flush_cpu_write_domain(obj);
1128
Chris Wilson05394f32010-11-08 19:18:58 +00001129 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001130unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07001131 mutex_unlock(&dev->struct_mutex);
1132 return ret;
1133}
1134
1135/**
1136 * Maps the contents of an object, returning the address it is mapped
1137 * into.
1138 *
1139 * While the mapping holds a reference on the contents of the object, it doesn't
1140 * imply a ref on the object itself.
1141 */
1142int
1143i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001144 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001145{
Chris Wilsonda761a62010-10-27 17:37:08 +01001146 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07001147 struct drm_i915_gem_mmap *args = data;
1148 struct drm_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001149 unsigned long addr;
1150
1151 if (!(dev->driver->driver_features & DRIVER_GEM))
1152 return -ENODEV;
1153
Chris Wilson05394f32010-11-08 19:18:58 +00001154 obj = drm_gem_object_lookup(dev, file, args->handle);
Eric Anholt673a3942008-07-30 12:06:12 -07001155 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001156 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001157
Chris Wilsonda761a62010-10-27 17:37:08 +01001158 if (obj->size > dev_priv->mm.gtt_mappable_end) {
1159 drm_gem_object_unreference_unlocked(obj);
1160 return -E2BIG;
1161 }
1162
Eric Anholt673a3942008-07-30 12:06:12 -07001163 down_write(&current->mm->mmap_sem);
1164 addr = do_mmap(obj->filp, 0, args->size,
1165 PROT_READ | PROT_WRITE, MAP_SHARED,
1166 args->offset);
1167 up_write(&current->mm->mmap_sem);
Luca Barbieribc9025b2010-02-09 05:49:12 +00001168 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001169 if (IS_ERR((void *)addr))
1170 return addr;
1171
1172 args->addr_ptr = (uint64_t) addr;
1173
1174 return 0;
1175}
1176
Jesse Barnesde151cf2008-11-12 10:03:55 -08001177/**
1178 * i915_gem_fault - fault a page into the GTT
1179 * vma: VMA in question
1180 * vmf: fault info
1181 *
1182 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1183 * from userspace. The fault handler takes care of binding the object to
1184 * the GTT (if needed), allocating and programming a fence register (again,
1185 * only if needed based on whether the old reg is still valid or the object
1186 * is tiled) and inserting a new PTE into the faulting process.
1187 *
1188 * Note that the faulting process may involve evicting existing objects
1189 * from the GTT and/or fence registers to make room. So performance may
1190 * suffer if the GTT working set is large or there are few fence registers
1191 * left.
1192 */
1193int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1194{
Chris Wilson05394f32010-11-08 19:18:58 +00001195 struct drm_i915_gem_object *obj = to_intel_bo(vma->vm_private_data);
1196 struct drm_device *dev = obj->base.dev;
Chris Wilson7d1c4802010-08-07 21:45:03 +01001197 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001198 pgoff_t page_offset;
1199 unsigned long pfn;
1200 int ret = 0;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001201 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001202
1203 /* We don't use vmf->pgoff since that has the fake offset */
1204 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1205 PAGE_SHIFT;
1206
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001207 ret = i915_mutex_lock_interruptible(dev);
1208 if (ret)
1209 goto out;
Chris Wilsona00b10c2010-09-24 21:15:47 +01001210
Chris Wilsondb53a302011-02-03 11:57:46 +00001211 trace_i915_gem_object_fault(obj, page_offset, true, write);
1212
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001213 /* Now bind it into the GTT if needed */
Chris Wilson919926a2010-11-12 13:42:53 +00001214 if (!obj->map_and_fenceable) {
1215 ret = i915_gem_object_unbind(obj);
1216 if (ret)
1217 goto unlock;
Chris Wilsona00b10c2010-09-24 21:15:47 +01001218 }
Chris Wilson05394f32010-11-08 19:18:58 +00001219 if (!obj->gtt_space) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01001220 ret = i915_gem_object_bind_to_gtt(obj, 0, true);
Chris Wilsonc7150892009-09-23 00:43:56 +01001221 if (ret)
1222 goto unlock;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001223 }
1224
Chris Wilson4a684a42010-10-28 14:44:08 +01001225 ret = i915_gem_object_set_to_gtt_domain(obj, write);
1226 if (ret)
1227 goto unlock;
1228
Chris Wilsond9e86c02010-11-10 16:40:20 +00001229 if (obj->tiling_mode == I915_TILING_NONE)
1230 ret = i915_gem_object_put_fence(obj);
1231 else
Chris Wilsonce453d82011-02-21 14:43:56 +00001232 ret = i915_gem_object_get_fence(obj, NULL);
Chris Wilsond9e86c02010-11-10 16:40:20 +00001233 if (ret)
1234 goto unlock;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001235
Chris Wilson05394f32010-11-08 19:18:58 +00001236 if (i915_gem_object_is_inactive(obj))
1237 list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
Chris Wilson7d1c4802010-08-07 21:45:03 +01001238
Chris Wilson6299f992010-11-24 12:23:44 +00001239 obj->fault_mappable = true;
1240
Chris Wilson05394f32010-11-08 19:18:58 +00001241 pfn = ((dev->agp->base + obj->gtt_offset) >> PAGE_SHIFT) +
Jesse Barnesde151cf2008-11-12 10:03:55 -08001242 page_offset;
1243
1244 /* Finally, remap it using the new GTT offset */
1245 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
Chris Wilsonc7150892009-09-23 00:43:56 +01001246unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001247 mutex_unlock(&dev->struct_mutex);
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001248out:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001249 switch (ret) {
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001250 case -EIO:
Chris Wilson045e7692010-11-07 09:18:22 +00001251 case -EAGAIN:
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001252 /* Give the error handler a chance to run and move the
1253 * objects off the GPU active list. Next time we service the
1254 * fault, we should be able to transition the page into the
1255 * GTT without touching the GPU (and so avoid further
1256 * EIO/EGAIN). If the GPU is wedged, then there is no issue
1257 * with coherency, just lost writes.
1258 */
Chris Wilson045e7692010-11-07 09:18:22 +00001259 set_need_resched();
Chris Wilsonc7150892009-09-23 00:43:56 +01001260 case 0:
1261 case -ERESTARTSYS:
Chris Wilsonbed636a2011-02-11 20:31:19 +00001262 case -EINTR:
Chris Wilsonc7150892009-09-23 00:43:56 +01001263 return VM_FAULT_NOPAGE;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001264 case -ENOMEM:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001265 return VM_FAULT_OOM;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001266 default:
Chris Wilsonc7150892009-09-23 00:43:56 +01001267 return VM_FAULT_SIGBUS;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001268 }
1269}
1270
1271/**
1272 * i915_gem_create_mmap_offset - create a fake mmap offset for an object
1273 * @obj: obj in question
1274 *
1275 * GEM memory mapping works by handing back to userspace a fake mmap offset
1276 * it can use in a subsequent mmap(2) call. The DRM core code then looks
1277 * up the object based on the offset and sets up the various memory mapping
1278 * structures.
1279 *
1280 * This routine allocates and attaches a fake offset for @obj.
1281 */
1282static int
Chris Wilson05394f32010-11-08 19:18:58 +00001283i915_gem_create_mmap_offset(struct drm_i915_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001284{
Chris Wilson05394f32010-11-08 19:18:58 +00001285 struct drm_device *dev = obj->base.dev;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001286 struct drm_gem_mm *mm = dev->mm_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001287 struct drm_map_list *list;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +11001288 struct drm_local_map *map;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001289 int ret = 0;
1290
1291 /* Set the object up for mmap'ing */
Chris Wilson05394f32010-11-08 19:18:58 +00001292 list = &obj->base.map_list;
Eric Anholt9a298b22009-03-24 12:23:04 -07001293 list->map = kzalloc(sizeof(struct drm_map_list), GFP_KERNEL);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001294 if (!list->map)
1295 return -ENOMEM;
1296
1297 map = list->map;
1298 map->type = _DRM_GEM;
Chris Wilson05394f32010-11-08 19:18:58 +00001299 map->size = obj->base.size;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001300 map->handle = obj;
1301
1302 /* Get a DRM GEM mmap offset allocated... */
1303 list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
Chris Wilson05394f32010-11-08 19:18:58 +00001304 obj->base.size / PAGE_SIZE,
1305 0, 0);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001306 if (!list->file_offset_node) {
Chris Wilson05394f32010-11-08 19:18:58 +00001307 DRM_ERROR("failed to allocate offset for bo %d\n",
1308 obj->base.name);
Chris Wilson9e0ae5342010-09-21 15:05:24 +01001309 ret = -ENOSPC;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001310 goto out_free_list;
1311 }
1312
1313 list->file_offset_node = drm_mm_get_block(list->file_offset_node,
Chris Wilson05394f32010-11-08 19:18:58 +00001314 obj->base.size / PAGE_SIZE,
1315 0);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001316 if (!list->file_offset_node) {
1317 ret = -ENOMEM;
1318 goto out_free_list;
1319 }
1320
1321 list->hash.key = list->file_offset_node->start;
Chris Wilson9e0ae5342010-09-21 15:05:24 +01001322 ret = drm_ht_insert_item(&mm->offset_hash, &list->hash);
1323 if (ret) {
Jesse Barnesde151cf2008-11-12 10:03:55 -08001324 DRM_ERROR("failed to add to map hash\n");
1325 goto out_free_mm;
1326 }
1327
Jesse Barnesde151cf2008-11-12 10:03:55 -08001328 return 0;
1329
1330out_free_mm:
1331 drm_mm_put_block(list->file_offset_node);
1332out_free_list:
Eric Anholt9a298b22009-03-24 12:23:04 -07001333 kfree(list->map);
Chris Wilson39a01d12010-10-28 13:03:06 +01001334 list->map = NULL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001335
1336 return ret;
1337}
1338
Chris Wilson901782b2009-07-10 08:18:50 +01001339/**
1340 * i915_gem_release_mmap - remove physical page mappings
1341 * @obj: obj in question
1342 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001343 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01001344 * relinquish ownership of the pages back to the system.
1345 *
1346 * It is vital that we remove the page mapping if we have mapped a tiled
1347 * object through the GTT and then lose the fence register due to
1348 * resource pressure. Similarly if the object has been moved out of the
1349 * aperture, than pages mapped into userspace must be revoked. Removing the
1350 * mapping will then trigger a page fault on the next user access, allowing
1351 * fixup by i915_gem_fault().
1352 */
Eric Anholtd05ca302009-07-10 13:02:26 -07001353void
Chris Wilson05394f32010-11-08 19:18:58 +00001354i915_gem_release_mmap(struct drm_i915_gem_object *obj)
Chris Wilson901782b2009-07-10 08:18:50 +01001355{
Chris Wilson6299f992010-11-24 12:23:44 +00001356 if (!obj->fault_mappable)
1357 return;
Chris Wilson901782b2009-07-10 08:18:50 +01001358
Chris Wilson6299f992010-11-24 12:23:44 +00001359 unmap_mapping_range(obj->base.dev->dev_mapping,
1360 (loff_t)obj->base.map_list.hash.key<<PAGE_SHIFT,
1361 obj->base.size, 1);
Daniel Vetterfb7d5162010-10-01 22:05:20 +02001362
Chris Wilson6299f992010-11-24 12:23:44 +00001363 obj->fault_mappable = false;
Chris Wilson901782b2009-07-10 08:18:50 +01001364}
1365
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001366static void
Chris Wilson05394f32010-11-08 19:18:58 +00001367i915_gem_free_mmap_offset(struct drm_i915_gem_object *obj)
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001368{
Chris Wilson05394f32010-11-08 19:18:58 +00001369 struct drm_device *dev = obj->base.dev;
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001370 struct drm_gem_mm *mm = dev->mm_private;
Chris Wilson05394f32010-11-08 19:18:58 +00001371 struct drm_map_list *list = &obj->base.map_list;
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001372
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001373 drm_ht_remove_item(&mm->offset_hash, &list->hash);
Chris Wilson39a01d12010-10-28 13:03:06 +01001374 drm_mm_put_block(list->file_offset_node);
1375 kfree(list->map);
1376 list->map = NULL;
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001377}
1378
Chris Wilson92b88ae2010-11-09 11:47:32 +00001379static uint32_t
1380i915_gem_get_gtt_size(struct drm_i915_gem_object *obj)
1381{
1382 struct drm_device *dev = obj->base.dev;
1383 uint32_t size;
1384
1385 if (INTEL_INFO(dev)->gen >= 4 ||
1386 obj->tiling_mode == I915_TILING_NONE)
1387 return obj->base.size;
1388
1389 /* Previous chips need a power-of-two fence region when tiling */
1390 if (INTEL_INFO(dev)->gen == 3)
1391 size = 1024*1024;
1392 else
1393 size = 512*1024;
1394
1395 while (size < obj->base.size)
1396 size <<= 1;
1397
1398 return size;
1399}
1400
Jesse Barnesde151cf2008-11-12 10:03:55 -08001401/**
1402 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1403 * @obj: object to check
1404 *
1405 * Return the required GTT alignment for an object, taking into account
Daniel Vetter5e783302010-11-14 22:32:36 +01001406 * potential fence register mapping.
Jesse Barnesde151cf2008-11-12 10:03:55 -08001407 */
1408static uint32_t
Chris Wilson05394f32010-11-08 19:18:58 +00001409i915_gem_get_gtt_alignment(struct drm_i915_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001410{
Chris Wilson05394f32010-11-08 19:18:58 +00001411 struct drm_device *dev = obj->base.dev;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001412
1413 /*
1414 * Minimum alignment is 4k (GTT page size), but might be greater
1415 * if a fence register is needed for the object.
1416 */
Chris Wilsona00b10c2010-09-24 21:15:47 +01001417 if (INTEL_INFO(dev)->gen >= 4 ||
Chris Wilson05394f32010-11-08 19:18:58 +00001418 obj->tiling_mode == I915_TILING_NONE)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001419 return 4096;
1420
1421 /*
1422 * Previous chips need to be aligned to the size of the smallest
1423 * fence register that can contain the object.
1424 */
Chris Wilson05394f32010-11-08 19:18:58 +00001425 return i915_gem_get_gtt_size(obj);
Chris Wilsona00b10c2010-09-24 21:15:47 +01001426}
1427
Daniel Vetter5e783302010-11-14 22:32:36 +01001428/**
1429 * i915_gem_get_unfenced_gtt_alignment - return required GTT alignment for an
1430 * unfenced object
1431 * @obj: object to check
1432 *
1433 * Return the required GTT alignment for an object, only taking into account
1434 * unfenced tiled surface requirements.
1435 */
Chris Wilson467cffb2011-03-07 10:42:03 +00001436uint32_t
Chris Wilson05394f32010-11-08 19:18:58 +00001437i915_gem_get_unfenced_gtt_alignment(struct drm_i915_gem_object *obj)
Daniel Vetter5e783302010-11-14 22:32:36 +01001438{
Chris Wilson05394f32010-11-08 19:18:58 +00001439 struct drm_device *dev = obj->base.dev;
Daniel Vetter5e783302010-11-14 22:32:36 +01001440 int tile_height;
1441
1442 /*
1443 * Minimum alignment is 4k (GTT page size) for sane hw.
1444 */
1445 if (INTEL_INFO(dev)->gen >= 4 || IS_G33(dev) ||
Chris Wilson05394f32010-11-08 19:18:58 +00001446 obj->tiling_mode == I915_TILING_NONE)
Daniel Vetter5e783302010-11-14 22:32:36 +01001447 return 4096;
1448
1449 /*
1450 * Older chips need unfenced tiled buffers to be aligned to the left
1451 * edge of an even tile row (where tile rows are counted as if the bo is
1452 * placed in a fenced gtt region).
1453 */
1454 if (IS_GEN2(dev) ||
Chris Wilson05394f32010-11-08 19:18:58 +00001455 (obj->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev)))
Daniel Vetter5e783302010-11-14 22:32:36 +01001456 tile_height = 32;
1457 else
1458 tile_height = 8;
1459
Chris Wilson05394f32010-11-08 19:18:58 +00001460 return tile_height * obj->stride * 2;
Daniel Vetter5e783302010-11-14 22:32:36 +01001461}
1462
Jesse Barnesde151cf2008-11-12 10:03:55 -08001463int
Dave Airlieff72145b2011-02-07 12:16:14 +10001464i915_gem_mmap_gtt(struct drm_file *file,
1465 struct drm_device *dev,
1466 uint32_t handle,
1467 uint64_t *offset)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001468{
Chris Wilsonda761a62010-10-27 17:37:08 +01001469 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00001470 struct drm_i915_gem_object *obj;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001471 int ret;
1472
1473 if (!(dev->driver->driver_features & DRIVER_GEM))
1474 return -ENODEV;
1475
Chris Wilson76c1dec2010-09-25 11:22:51 +01001476 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001477 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001478 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001479
Dave Airlieff72145b2011-02-07 12:16:14 +10001480 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00001481 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001482 ret = -ENOENT;
1483 goto unlock;
1484 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001485
Chris Wilson05394f32010-11-08 19:18:58 +00001486 if (obj->base.size > dev_priv->mm.gtt_mappable_end) {
Chris Wilsonda761a62010-10-27 17:37:08 +01001487 ret = -E2BIG;
1488 goto unlock;
1489 }
1490
Chris Wilson05394f32010-11-08 19:18:58 +00001491 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilsonab182822009-09-22 18:46:17 +01001492 DRM_ERROR("Attempting to mmap a purgeable buffer\n");
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001493 ret = -EINVAL;
1494 goto out;
Chris Wilsonab182822009-09-22 18:46:17 +01001495 }
1496
Chris Wilson05394f32010-11-08 19:18:58 +00001497 if (!obj->base.map_list.map) {
Jesse Barnesde151cf2008-11-12 10:03:55 -08001498 ret = i915_gem_create_mmap_offset(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001499 if (ret)
1500 goto out;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001501 }
1502
Dave Airlieff72145b2011-02-07 12:16:14 +10001503 *offset = (u64)obj->base.map_list.hash.key << PAGE_SHIFT;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001504
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001505out:
Chris Wilson05394f32010-11-08 19:18:58 +00001506 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001507unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001508 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001509 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001510}
1511
Dave Airlieff72145b2011-02-07 12:16:14 +10001512/**
1513 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1514 * @dev: DRM device
1515 * @data: GTT mapping ioctl data
1516 * @file: GEM object info
1517 *
1518 * Simply returns the fake offset to userspace so it can mmap it.
1519 * The mmap call will end up in drm_gem_mmap(), which will set things
1520 * up so we can get faults in the handler above.
1521 *
1522 * The fault handler will take care of binding the object into the GTT
1523 * (since it may have been evicted to make room for something), allocating
1524 * a fence register, and mapping the appropriate aperture address into
1525 * userspace.
1526 */
1527int
1528i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
1529 struct drm_file *file)
1530{
1531 struct drm_i915_gem_mmap_gtt *args = data;
1532
1533 if (!(dev->driver->driver_features & DRIVER_GEM))
1534 return -ENODEV;
1535
1536 return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
1537}
1538
1539
Chris Wilsone5281cc2010-10-28 13:45:36 +01001540static int
Chris Wilson05394f32010-11-08 19:18:58 +00001541i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj,
Chris Wilsone5281cc2010-10-28 13:45:36 +01001542 gfp_t gfpmask)
1543{
Chris Wilsone5281cc2010-10-28 13:45:36 +01001544 int page_count, i;
1545 struct address_space *mapping;
1546 struct inode *inode;
1547 struct page *page;
1548
1549 /* Get the list of pages out of our struct file. They'll be pinned
1550 * at this point until we release them.
1551 */
Chris Wilson05394f32010-11-08 19:18:58 +00001552 page_count = obj->base.size / PAGE_SIZE;
1553 BUG_ON(obj->pages != NULL);
1554 obj->pages = drm_malloc_ab(page_count, sizeof(struct page *));
1555 if (obj->pages == NULL)
Chris Wilsone5281cc2010-10-28 13:45:36 +01001556 return -ENOMEM;
1557
Chris Wilson05394f32010-11-08 19:18:58 +00001558 inode = obj->base.filp->f_path.dentry->d_inode;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001559 mapping = inode->i_mapping;
1560 for (i = 0; i < page_count; i++) {
1561 page = read_cache_page_gfp(mapping, i,
1562 GFP_HIGHUSER |
1563 __GFP_COLD |
1564 __GFP_RECLAIMABLE |
1565 gfpmask);
1566 if (IS_ERR(page))
1567 goto err_pages;
1568
Chris Wilson05394f32010-11-08 19:18:58 +00001569 obj->pages[i] = page;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001570 }
1571
Chris Wilson05394f32010-11-08 19:18:58 +00001572 if (obj->tiling_mode != I915_TILING_NONE)
Chris Wilsone5281cc2010-10-28 13:45:36 +01001573 i915_gem_object_do_bit_17_swizzle(obj);
1574
1575 return 0;
1576
1577err_pages:
1578 while (i--)
Chris Wilson05394f32010-11-08 19:18:58 +00001579 page_cache_release(obj->pages[i]);
Chris Wilsone5281cc2010-10-28 13:45:36 +01001580
Chris Wilson05394f32010-11-08 19:18:58 +00001581 drm_free_large(obj->pages);
1582 obj->pages = NULL;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001583 return PTR_ERR(page);
1584}
1585
Chris Wilson5cdf5882010-09-27 15:51:07 +01001586static void
Chris Wilson05394f32010-11-08 19:18:58 +00001587i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07001588{
Chris Wilson05394f32010-11-08 19:18:58 +00001589 int page_count = obj->base.size / PAGE_SIZE;
Eric Anholt673a3942008-07-30 12:06:12 -07001590 int i;
1591
Chris Wilson05394f32010-11-08 19:18:58 +00001592 BUG_ON(obj->madv == __I915_MADV_PURGED);
Eric Anholt856fa192009-03-19 14:10:50 -07001593
Chris Wilson05394f32010-11-08 19:18:58 +00001594 if (obj->tiling_mode != I915_TILING_NONE)
Eric Anholt280b7132009-03-12 16:56:27 -07001595 i915_gem_object_save_bit_17_swizzle(obj);
1596
Chris Wilson05394f32010-11-08 19:18:58 +00001597 if (obj->madv == I915_MADV_DONTNEED)
1598 obj->dirty = 0;
Chris Wilson3ef94da2009-09-14 16:50:29 +01001599
1600 for (i = 0; i < page_count; i++) {
Chris Wilson05394f32010-11-08 19:18:58 +00001601 if (obj->dirty)
1602 set_page_dirty(obj->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001603
Chris Wilson05394f32010-11-08 19:18:58 +00001604 if (obj->madv == I915_MADV_WILLNEED)
1605 mark_page_accessed(obj->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001606
Chris Wilson05394f32010-11-08 19:18:58 +00001607 page_cache_release(obj->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001608 }
Chris Wilson05394f32010-11-08 19:18:58 +00001609 obj->dirty = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001610
Chris Wilson05394f32010-11-08 19:18:58 +00001611 drm_free_large(obj->pages);
1612 obj->pages = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001613}
1614
Chris Wilson54cf91d2010-11-25 18:00:26 +00001615void
Chris Wilson05394f32010-11-08 19:18:58 +00001616i915_gem_object_move_to_active(struct drm_i915_gem_object *obj,
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001617 struct intel_ring_buffer *ring,
1618 u32 seqno)
Eric Anholt673a3942008-07-30 12:06:12 -07001619{
Chris Wilson05394f32010-11-08 19:18:58 +00001620 struct drm_device *dev = obj->base.dev;
Chris Wilson69dc4982010-10-19 10:36:51 +01001621 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter617dbe22010-02-11 22:16:02 +01001622
Zou Nan hai852835f2010-05-21 09:08:56 +08001623 BUG_ON(ring == NULL);
Chris Wilson05394f32010-11-08 19:18:58 +00001624 obj->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001625
1626 /* Add a reference if we're newly entering the active list. */
Chris Wilson05394f32010-11-08 19:18:58 +00001627 if (!obj->active) {
1628 drm_gem_object_reference(&obj->base);
1629 obj->active = 1;
Eric Anholt673a3942008-07-30 12:06:12 -07001630 }
Daniel Vettere35a41d2010-02-11 22:13:59 +01001631
Eric Anholt673a3942008-07-30 12:06:12 -07001632 /* Move from whatever list we were on to the tail of execution. */
Chris Wilson05394f32010-11-08 19:18:58 +00001633 list_move_tail(&obj->mm_list, &dev_priv->mm.active_list);
1634 list_move_tail(&obj->ring_list, &ring->active_list);
Chris Wilsoncaea7472010-11-12 13:53:37 +00001635
Chris Wilson05394f32010-11-08 19:18:58 +00001636 obj->last_rendering_seqno = seqno;
Chris Wilsoncaea7472010-11-12 13:53:37 +00001637 if (obj->fenced_gpu_access) {
1638 struct drm_i915_fence_reg *reg;
1639
1640 BUG_ON(obj->fence_reg == I915_FENCE_REG_NONE);
1641
1642 obj->last_fenced_seqno = seqno;
1643 obj->last_fenced_ring = ring;
1644
1645 reg = &dev_priv->fence_regs[obj->fence_reg];
1646 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
1647 }
1648}
1649
1650static void
1651i915_gem_object_move_off_active(struct drm_i915_gem_object *obj)
1652{
1653 list_del_init(&obj->ring_list);
1654 obj->last_rendering_seqno = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001655}
1656
Eric Anholtce44b0e2008-11-06 16:00:31 -08001657static void
Chris Wilson05394f32010-11-08 19:18:58 +00001658i915_gem_object_move_to_flushing(struct drm_i915_gem_object *obj)
Eric Anholtce44b0e2008-11-06 16:00:31 -08001659{
Chris Wilson05394f32010-11-08 19:18:58 +00001660 struct drm_device *dev = obj->base.dev;
Eric Anholtce44b0e2008-11-06 16:00:31 -08001661 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtce44b0e2008-11-06 16:00:31 -08001662
Chris Wilson05394f32010-11-08 19:18:58 +00001663 BUG_ON(!obj->active);
1664 list_move_tail(&obj->mm_list, &dev_priv->mm.flushing_list);
Chris Wilsoncaea7472010-11-12 13:53:37 +00001665
1666 i915_gem_object_move_off_active(obj);
1667}
1668
1669static void
1670i915_gem_object_move_to_inactive(struct drm_i915_gem_object *obj)
1671{
1672 struct drm_device *dev = obj->base.dev;
1673 struct drm_i915_private *dev_priv = dev->dev_private;
1674
1675 if (obj->pin_count != 0)
1676 list_move_tail(&obj->mm_list, &dev_priv->mm.pinned_list);
1677 else
1678 list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
1679
1680 BUG_ON(!list_empty(&obj->gpu_write_list));
1681 BUG_ON(!obj->active);
1682 obj->ring = NULL;
1683
1684 i915_gem_object_move_off_active(obj);
1685 obj->fenced_gpu_access = false;
Chris Wilsoncaea7472010-11-12 13:53:37 +00001686
1687 obj->active = 0;
Chris Wilson87ca9c82010-12-02 09:42:56 +00001688 obj->pending_gpu_write = false;
Chris Wilsoncaea7472010-11-12 13:53:37 +00001689 drm_gem_object_unreference(&obj->base);
1690
1691 WARN_ON(i915_verify_lists(dev));
Eric Anholtce44b0e2008-11-06 16:00:31 -08001692}
Eric Anholt673a3942008-07-30 12:06:12 -07001693
Chris Wilson963b4832009-09-20 23:03:54 +01001694/* Immediately discard the backing storage */
1695static void
Chris Wilson05394f32010-11-08 19:18:58 +00001696i915_gem_object_truncate(struct drm_i915_gem_object *obj)
Chris Wilson963b4832009-09-20 23:03:54 +01001697{
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001698 struct inode *inode;
Chris Wilson963b4832009-09-20 23:03:54 +01001699
Chris Wilsonae9fed62010-08-07 11:01:30 +01001700 /* Our goal here is to return as much of the memory as
1701 * is possible back to the system as we are called from OOM.
1702 * To do this we must instruct the shmfs to drop all of its
1703 * backing pages, *now*. Here we mirror the actions taken
1704 * when by shmem_delete_inode() to release the backing store.
1705 */
Chris Wilson05394f32010-11-08 19:18:58 +00001706 inode = obj->base.filp->f_path.dentry->d_inode;
Chris Wilsonae9fed62010-08-07 11:01:30 +01001707 truncate_inode_pages(inode->i_mapping, 0);
1708 if (inode->i_op->truncate_range)
1709 inode->i_op->truncate_range(inode, 0, (loff_t)-1);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001710
Chris Wilson05394f32010-11-08 19:18:58 +00001711 obj->madv = __I915_MADV_PURGED;
Chris Wilson963b4832009-09-20 23:03:54 +01001712}
1713
1714static inline int
Chris Wilson05394f32010-11-08 19:18:58 +00001715i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj)
Chris Wilson963b4832009-09-20 23:03:54 +01001716{
Chris Wilson05394f32010-11-08 19:18:58 +00001717 return obj->madv == I915_MADV_DONTNEED;
Chris Wilson963b4832009-09-20 23:03:54 +01001718}
1719
Eric Anholt673a3942008-07-30 12:06:12 -07001720static void
Chris Wilsondb53a302011-02-03 11:57:46 +00001721i915_gem_process_flushing_list(struct intel_ring_buffer *ring,
1722 uint32_t flush_domains)
Daniel Vetter63560392010-02-19 11:51:59 +01001723{
Chris Wilson05394f32010-11-08 19:18:58 +00001724 struct drm_i915_gem_object *obj, *next;
Daniel Vetter63560392010-02-19 11:51:59 +01001725
Chris Wilson05394f32010-11-08 19:18:58 +00001726 list_for_each_entry_safe(obj, next,
Chris Wilson64193402010-10-24 12:38:05 +01001727 &ring->gpu_write_list,
Daniel Vetter63560392010-02-19 11:51:59 +01001728 gpu_write_list) {
Chris Wilson05394f32010-11-08 19:18:58 +00001729 if (obj->base.write_domain & flush_domains) {
1730 uint32_t old_write_domain = obj->base.write_domain;
Daniel Vetter63560392010-02-19 11:51:59 +01001731
Chris Wilson05394f32010-11-08 19:18:58 +00001732 obj->base.write_domain = 0;
1733 list_del_init(&obj->gpu_write_list);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001734 i915_gem_object_move_to_active(obj, ring,
Chris Wilsondb53a302011-02-03 11:57:46 +00001735 i915_gem_next_request_seqno(ring));
Daniel Vetter63560392010-02-19 11:51:59 +01001736
Daniel Vetter63560392010-02-19 11:51:59 +01001737 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00001738 obj->base.read_domains,
Daniel Vetter63560392010-02-19 11:51:59 +01001739 old_write_domain);
1740 }
1741 }
1742}
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001743
Chris Wilson3cce4692010-10-27 16:11:02 +01001744int
Chris Wilsondb53a302011-02-03 11:57:46 +00001745i915_add_request(struct intel_ring_buffer *ring,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001746 struct drm_file *file,
Chris Wilsondb53a302011-02-03 11:57:46 +00001747 struct drm_i915_gem_request *request)
Eric Anholt673a3942008-07-30 12:06:12 -07001748{
Chris Wilsondb53a302011-02-03 11:57:46 +00001749 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07001750 uint32_t seqno;
1751 int was_empty;
Chris Wilson3cce4692010-10-27 16:11:02 +01001752 int ret;
1753
1754 BUG_ON(request == NULL);
Eric Anholt673a3942008-07-30 12:06:12 -07001755
Chris Wilson3cce4692010-10-27 16:11:02 +01001756 ret = ring->add_request(ring, &seqno);
1757 if (ret)
1758 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001759
Chris Wilsondb53a302011-02-03 11:57:46 +00001760 trace_i915_gem_request_add(ring, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07001761
1762 request->seqno = seqno;
Zou Nan hai852835f2010-05-21 09:08:56 +08001763 request->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001764 request->emitted_jiffies = jiffies;
Zou Nan hai852835f2010-05-21 09:08:56 +08001765 was_empty = list_empty(&ring->request_list);
1766 list_add_tail(&request->list, &ring->request_list);
1767
Chris Wilsondb53a302011-02-03 11:57:46 +00001768 if (file) {
1769 struct drm_i915_file_private *file_priv = file->driver_priv;
1770
Chris Wilson1c255952010-09-26 11:03:27 +01001771 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001772 request->file_priv = file_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00001773 list_add_tail(&request->client_list,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001774 &file_priv->mm.request_list);
Chris Wilson1c255952010-09-26 11:03:27 +01001775 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00001776 }
Eric Anholt673a3942008-07-30 12:06:12 -07001777
Chris Wilsondb53a302011-02-03 11:57:46 +00001778 ring->outstanding_lazy_request = false;
1779
Ben Gamarif65d9422009-09-14 17:48:44 -04001780 if (!dev_priv->mm.suspended) {
Chris Wilsonb3b079d2010-09-13 23:44:34 +01001781 mod_timer(&dev_priv->hangcheck_timer,
1782 jiffies + msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
Ben Gamarif65d9422009-09-14 17:48:44 -04001783 if (was_empty)
Chris Wilsonb3b079d2010-09-13 23:44:34 +01001784 queue_delayed_work(dev_priv->wq,
1785 &dev_priv->mm.retire_work, HZ);
Ben Gamarif65d9422009-09-14 17:48:44 -04001786 }
Chris Wilson3cce4692010-10-27 16:11:02 +01001787 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001788}
1789
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001790static inline void
1791i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
Eric Anholt673a3942008-07-30 12:06:12 -07001792{
Chris Wilson1c255952010-09-26 11:03:27 +01001793 struct drm_i915_file_private *file_priv = request->file_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07001794
Chris Wilson1c255952010-09-26 11:03:27 +01001795 if (!file_priv)
1796 return;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001797
Chris Wilson1c255952010-09-26 11:03:27 +01001798 spin_lock(&file_priv->mm.lock);
Herton Ronaldo Krzesinski09bfa512011-03-17 13:45:12 +00001799 if (request->file_priv) {
1800 list_del(&request->client_list);
1801 request->file_priv = NULL;
1802 }
Chris Wilson1c255952010-09-26 11:03:27 +01001803 spin_unlock(&file_priv->mm.lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001804}
1805
Chris Wilsondfaae392010-09-22 10:31:52 +01001806static void i915_gem_reset_ring_lists(struct drm_i915_private *dev_priv,
1807 struct intel_ring_buffer *ring)
Chris Wilson9375e442010-09-19 12:21:28 +01001808{
Chris Wilsondfaae392010-09-22 10:31:52 +01001809 while (!list_empty(&ring->request_list)) {
1810 struct drm_i915_gem_request *request;
Chris Wilson9375e442010-09-19 12:21:28 +01001811
Chris Wilsondfaae392010-09-22 10:31:52 +01001812 request = list_first_entry(&ring->request_list,
1813 struct drm_i915_gem_request,
1814 list);
1815
1816 list_del(&request->list);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001817 i915_gem_request_remove_from_client(request);
Chris Wilsondfaae392010-09-22 10:31:52 +01001818 kfree(request);
1819 }
1820
1821 while (!list_empty(&ring->active_list)) {
Chris Wilson05394f32010-11-08 19:18:58 +00001822 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001823
Chris Wilson05394f32010-11-08 19:18:58 +00001824 obj = list_first_entry(&ring->active_list,
1825 struct drm_i915_gem_object,
1826 ring_list);
Eric Anholt673a3942008-07-30 12:06:12 -07001827
Chris Wilson05394f32010-11-08 19:18:58 +00001828 obj->base.write_domain = 0;
1829 list_del_init(&obj->gpu_write_list);
1830 i915_gem_object_move_to_inactive(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001831 }
Eric Anholt673a3942008-07-30 12:06:12 -07001832}
1833
Chris Wilson312817a2010-11-22 11:50:11 +00001834static void i915_gem_reset_fences(struct drm_device *dev)
1835{
1836 struct drm_i915_private *dev_priv = dev->dev_private;
1837 int i;
1838
1839 for (i = 0; i < 16; i++) {
1840 struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
Chris Wilson7d2cb392010-11-27 17:38:29 +00001841 struct drm_i915_gem_object *obj = reg->obj;
1842
1843 if (!obj)
1844 continue;
1845
1846 if (obj->tiling_mode)
1847 i915_gem_release_mmap(obj);
1848
Chris Wilsond9e86c02010-11-10 16:40:20 +00001849 reg->obj->fence_reg = I915_FENCE_REG_NONE;
1850 reg->obj->fenced_gpu_access = false;
1851 reg->obj->last_fenced_seqno = 0;
1852 reg->obj->last_fenced_ring = NULL;
1853 i915_gem_clear_fence_reg(dev, reg);
Chris Wilson312817a2010-11-22 11:50:11 +00001854 }
1855}
1856
Chris Wilson069efc12010-09-30 16:53:18 +01001857void i915_gem_reset(struct drm_device *dev)
Eric Anholt673a3942008-07-30 12:06:12 -07001858{
Chris Wilsondfaae392010-09-22 10:31:52 +01001859 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00001860 struct drm_i915_gem_object *obj;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001861 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07001862
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001863 for (i = 0; i < I915_NUM_RINGS; i++)
1864 i915_gem_reset_ring_lists(dev_priv, &dev_priv->ring[i]);
Chris Wilsondfaae392010-09-22 10:31:52 +01001865
1866 /* Remove anything from the flushing lists. The GPU cache is likely
1867 * to be lost on reset along with the data, so simply move the
1868 * lost bo to the inactive list.
1869 */
1870 while (!list_empty(&dev_priv->mm.flushing_list)) {
Chris Wilson05394f32010-11-08 19:18:58 +00001871 obj= list_first_entry(&dev_priv->mm.flushing_list,
1872 struct drm_i915_gem_object,
1873 mm_list);
Chris Wilson9375e442010-09-19 12:21:28 +01001874
Chris Wilson05394f32010-11-08 19:18:58 +00001875 obj->base.write_domain = 0;
1876 list_del_init(&obj->gpu_write_list);
1877 i915_gem_object_move_to_inactive(obj);
Chris Wilson9375e442010-09-19 12:21:28 +01001878 }
Chris Wilson9375e442010-09-19 12:21:28 +01001879
Chris Wilsondfaae392010-09-22 10:31:52 +01001880 /* Move everything out of the GPU domains to ensure we do any
1881 * necessary invalidation upon reuse.
1882 */
Chris Wilson05394f32010-11-08 19:18:58 +00001883 list_for_each_entry(obj,
Chris Wilson77f01232010-09-19 12:31:36 +01001884 &dev_priv->mm.inactive_list,
Chris Wilson69dc4982010-10-19 10:36:51 +01001885 mm_list)
Chris Wilson77f01232010-09-19 12:31:36 +01001886 {
Chris Wilson05394f32010-11-08 19:18:58 +00001887 obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
Chris Wilson77f01232010-09-19 12:31:36 +01001888 }
Chris Wilson069efc12010-09-30 16:53:18 +01001889
1890 /* The fence registers are invalidated so clear them out */
Chris Wilson312817a2010-11-22 11:50:11 +00001891 i915_gem_reset_fences(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07001892}
1893
1894/**
1895 * This function clears the request list as sequence numbers are passed.
1896 */
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001897static void
Chris Wilsondb53a302011-02-03 11:57:46 +00001898i915_gem_retire_requests_ring(struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001899{
Eric Anholt673a3942008-07-30 12:06:12 -07001900 uint32_t seqno;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001901 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07001902
Chris Wilsondb53a302011-02-03 11:57:46 +00001903 if (list_empty(&ring->request_list))
Karsten Wiese6c0594a2009-02-23 15:07:57 +01001904 return;
1905
Chris Wilsondb53a302011-02-03 11:57:46 +00001906 WARN_ON(i915_verify_lists(ring->dev));
Eric Anholt673a3942008-07-30 12:06:12 -07001907
Chris Wilson78501ea2010-10-27 12:18:21 +01001908 seqno = ring->get_seqno(ring);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001909
Chris Wilson076e2c02011-01-21 10:07:18 +00001910 for (i = 0; i < ARRAY_SIZE(ring->sync_seqno); i++)
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001911 if (seqno >= ring->sync_seqno[i])
1912 ring->sync_seqno[i] = 0;
1913
Zou Nan hai852835f2010-05-21 09:08:56 +08001914 while (!list_empty(&ring->request_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001915 struct drm_i915_gem_request *request;
Eric Anholt673a3942008-07-30 12:06:12 -07001916
Zou Nan hai852835f2010-05-21 09:08:56 +08001917 request = list_first_entry(&ring->request_list,
Eric Anholt673a3942008-07-30 12:06:12 -07001918 struct drm_i915_gem_request,
1919 list);
Eric Anholt673a3942008-07-30 12:06:12 -07001920
Chris Wilsondfaae392010-09-22 10:31:52 +01001921 if (!i915_seqno_passed(seqno, request->seqno))
Eric Anholt673a3942008-07-30 12:06:12 -07001922 break;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001923
Chris Wilsondb53a302011-02-03 11:57:46 +00001924 trace_i915_gem_request_retire(ring, request->seqno);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001925
1926 list_del(&request->list);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001927 i915_gem_request_remove_from_client(request);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001928 kfree(request);
1929 }
1930
1931 /* Move any buffers on the active list that are no longer referenced
1932 * by the ringbuffer to the flushing/inactive lists as appropriate.
1933 */
1934 while (!list_empty(&ring->active_list)) {
Chris Wilson05394f32010-11-08 19:18:58 +00001935 struct drm_i915_gem_object *obj;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001936
Chris Wilson05394f32010-11-08 19:18:58 +00001937 obj= list_first_entry(&ring->active_list,
1938 struct drm_i915_gem_object,
1939 ring_list);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001940
Chris Wilson05394f32010-11-08 19:18:58 +00001941 if (!i915_seqno_passed(seqno, obj->last_rendering_seqno))
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001942 break;
1943
Chris Wilson05394f32010-11-08 19:18:58 +00001944 if (obj->base.write_domain != 0)
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001945 i915_gem_object_move_to_flushing(obj);
1946 else
1947 i915_gem_object_move_to_inactive(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001948 }
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001949
Chris Wilsondb53a302011-02-03 11:57:46 +00001950 if (unlikely(ring->trace_irq_seqno &&
1951 i915_seqno_passed(seqno, ring->trace_irq_seqno))) {
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001952 ring->irq_put(ring);
Chris Wilsondb53a302011-02-03 11:57:46 +00001953 ring->trace_irq_seqno = 0;
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001954 }
Chris Wilson23bc5982010-09-29 16:10:57 +01001955
Chris Wilsondb53a302011-02-03 11:57:46 +00001956 WARN_ON(i915_verify_lists(ring->dev));
Eric Anholt673a3942008-07-30 12:06:12 -07001957}
1958
1959void
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001960i915_gem_retire_requests(struct drm_device *dev)
1961{
1962 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001963 int i;
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001964
Chris Wilsonbe726152010-07-23 23:18:50 +01001965 if (!list_empty(&dev_priv->mm.deferred_free_list)) {
Chris Wilson05394f32010-11-08 19:18:58 +00001966 struct drm_i915_gem_object *obj, *next;
Chris Wilsonbe726152010-07-23 23:18:50 +01001967
1968 /* We must be careful that during unbind() we do not
1969 * accidentally infinitely recurse into retire requests.
1970 * Currently:
1971 * retire -> free -> unbind -> wait -> retire_ring
1972 */
Chris Wilson05394f32010-11-08 19:18:58 +00001973 list_for_each_entry_safe(obj, next,
Chris Wilsonbe726152010-07-23 23:18:50 +01001974 &dev_priv->mm.deferred_free_list,
Chris Wilson69dc4982010-10-19 10:36:51 +01001975 mm_list)
Chris Wilson05394f32010-11-08 19:18:58 +00001976 i915_gem_free_object_tail(obj);
Chris Wilsonbe726152010-07-23 23:18:50 +01001977 }
1978
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001979 for (i = 0; i < I915_NUM_RINGS; i++)
Chris Wilsondb53a302011-02-03 11:57:46 +00001980 i915_gem_retire_requests_ring(&dev_priv->ring[i]);
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001981}
1982
Daniel Vetter75ef9da2010-08-21 00:25:16 +02001983static void
Eric Anholt673a3942008-07-30 12:06:12 -07001984i915_gem_retire_work_handler(struct work_struct *work)
1985{
1986 drm_i915_private_t *dev_priv;
1987 struct drm_device *dev;
Chris Wilson0a587052011-01-09 21:05:44 +00001988 bool idle;
1989 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07001990
1991 dev_priv = container_of(work, drm_i915_private_t,
1992 mm.retire_work.work);
1993 dev = dev_priv->dev;
1994
Chris Wilson891b48c2010-09-29 12:26:37 +01001995 /* Come back later if the device is busy... */
1996 if (!mutex_trylock(&dev->struct_mutex)) {
1997 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
1998 return;
1999 }
2000
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002001 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08002002
Chris Wilson0a587052011-01-09 21:05:44 +00002003 /* Send a periodic flush down the ring so we don't hold onto GEM
2004 * objects indefinitely.
2005 */
2006 idle = true;
2007 for (i = 0; i < I915_NUM_RINGS; i++) {
2008 struct intel_ring_buffer *ring = &dev_priv->ring[i];
2009
2010 if (!list_empty(&ring->gpu_write_list)) {
2011 struct drm_i915_gem_request *request;
2012 int ret;
2013
Chris Wilsondb53a302011-02-03 11:57:46 +00002014 ret = i915_gem_flush_ring(ring,
2015 0, I915_GEM_GPU_DOMAINS);
Chris Wilson0a587052011-01-09 21:05:44 +00002016 request = kzalloc(sizeof(*request), GFP_KERNEL);
2017 if (ret || request == NULL ||
Chris Wilsondb53a302011-02-03 11:57:46 +00002018 i915_add_request(ring, NULL, request))
Chris Wilson0a587052011-01-09 21:05:44 +00002019 kfree(request);
2020 }
2021
2022 idle &= list_empty(&ring->request_list);
2023 }
2024
2025 if (!dev_priv->mm.suspended && !idle)
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07002026 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
Chris Wilson0a587052011-01-09 21:05:44 +00002027
Eric Anholt673a3942008-07-30 12:06:12 -07002028 mutex_unlock(&dev->struct_mutex);
2029}
2030
Chris Wilsondb53a302011-02-03 11:57:46 +00002031/**
2032 * Waits for a sequence number to be signaled, and cleans up the
2033 * request and object lists appropriately for that event.
2034 */
Daniel Vetter5a5a0c62009-09-15 22:57:36 +02002035int
Chris Wilsondb53a302011-02-03 11:57:46 +00002036i915_wait_request(struct intel_ring_buffer *ring,
Chris Wilsonce453d82011-02-21 14:43:56 +00002037 uint32_t seqno)
Eric Anholt673a3942008-07-30 12:06:12 -07002038{
Chris Wilsondb53a302011-02-03 11:57:46 +00002039 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Jesse Barnes802c7eb2009-05-05 16:03:48 -07002040 u32 ier;
Eric Anholt673a3942008-07-30 12:06:12 -07002041 int ret = 0;
2042
2043 BUG_ON(seqno == 0);
2044
Chris Wilsond9bc7e92011-02-07 13:09:31 +00002045 if (atomic_read(&dev_priv->mm.wedged)) {
2046 struct completion *x = &dev_priv->error_completion;
2047 bool recovery_complete;
2048 unsigned long flags;
2049
2050 /* Give the error handler a chance to run. */
2051 spin_lock_irqsave(&x->wait.lock, flags);
2052 recovery_complete = x->done > 0;
2053 spin_unlock_irqrestore(&x->wait.lock, flags);
2054
2055 return recovery_complete ? -EIO : -EAGAIN;
2056 }
Ben Gamariffed1d02009-09-14 17:48:41 -04002057
Chris Wilson5d97eb62010-11-10 20:40:02 +00002058 if (seqno == ring->outstanding_lazy_request) {
Chris Wilson3cce4692010-10-27 16:11:02 +01002059 struct drm_i915_gem_request *request;
2060
2061 request = kzalloc(sizeof(*request), GFP_KERNEL);
2062 if (request == NULL)
Daniel Vettere35a41d2010-02-11 22:13:59 +01002063 return -ENOMEM;
Chris Wilson3cce4692010-10-27 16:11:02 +01002064
Chris Wilsondb53a302011-02-03 11:57:46 +00002065 ret = i915_add_request(ring, NULL, request);
Chris Wilson3cce4692010-10-27 16:11:02 +01002066 if (ret) {
2067 kfree(request);
2068 return ret;
2069 }
2070
2071 seqno = request->seqno;
Daniel Vettere35a41d2010-02-11 22:13:59 +01002072 }
2073
Chris Wilson78501ea2010-10-27 12:18:21 +01002074 if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) {
Chris Wilsondb53a302011-02-03 11:57:46 +00002075 if (HAS_PCH_SPLIT(ring->dev))
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002076 ier = I915_READ(DEIER) | I915_READ(GTIER);
2077 else
2078 ier = I915_READ(IER);
Jesse Barnes802c7eb2009-05-05 16:03:48 -07002079 if (!ier) {
2080 DRM_ERROR("something (likely vbetool) disabled "
2081 "interrupts, re-enabling\n");
Chris Wilsondb53a302011-02-03 11:57:46 +00002082 i915_driver_irq_preinstall(ring->dev);
2083 i915_driver_irq_postinstall(ring->dev);
Jesse Barnes802c7eb2009-05-05 16:03:48 -07002084 }
2085
Chris Wilsondb53a302011-02-03 11:57:46 +00002086 trace_i915_gem_request_wait_begin(ring, seqno);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002087
Chris Wilsonb2223492010-10-27 15:27:33 +01002088 ring->waiting_seqno = seqno;
Chris Wilsonb13c2b92010-12-13 16:54:50 +00002089 if (ring->irq_get(ring)) {
Chris Wilsonce453d82011-02-21 14:43:56 +00002090 if (dev_priv->mm.interruptible)
Chris Wilsonb13c2b92010-12-13 16:54:50 +00002091 ret = wait_event_interruptible(ring->irq_queue,
2092 i915_seqno_passed(ring->get_seqno(ring), seqno)
2093 || atomic_read(&dev_priv->mm.wedged));
2094 else
2095 wait_event(ring->irq_queue,
2096 i915_seqno_passed(ring->get_seqno(ring), seqno)
2097 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02002098
Chris Wilsonb13c2b92010-12-13 16:54:50 +00002099 ring->irq_put(ring);
Chris Wilsonb5ba1772010-12-14 12:17:15 +00002100 } else if (wait_for(i915_seqno_passed(ring->get_seqno(ring),
2101 seqno) ||
2102 atomic_read(&dev_priv->mm.wedged), 3000))
2103 ret = -EBUSY;
Chris Wilsonb2223492010-10-27 15:27:33 +01002104 ring->waiting_seqno = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002105
Chris Wilsondb53a302011-02-03 11:57:46 +00002106 trace_i915_gem_request_wait_end(ring, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07002107 }
Ben Gamariba1234d2009-09-14 17:48:47 -04002108 if (atomic_read(&dev_priv->mm.wedged))
Chris Wilson30dbf0c2010-09-25 10:19:17 +01002109 ret = -EAGAIN;
Eric Anholt673a3942008-07-30 12:06:12 -07002110
2111 if (ret && ret != -ERESTARTSYS)
Daniel Vetter8bff9172010-02-11 22:19:40 +01002112 DRM_ERROR("%s returns %d (awaiting %d at %d, next %d)\n",
Chris Wilson78501ea2010-10-27 12:18:21 +01002113 __func__, ret, seqno, ring->get_seqno(ring),
Daniel Vetter8bff9172010-02-11 22:19:40 +01002114 dev_priv->next_seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07002115
2116 /* Directly dispatch request retiring. While we have the work queue
2117 * to handle this, the waiter on a request often wants an associated
2118 * buffer to have made it to the inactive list, and we would need
2119 * a separate wait queue to handle that.
2120 */
2121 if (ret == 0)
Chris Wilsondb53a302011-02-03 11:57:46 +00002122 i915_gem_retire_requests_ring(ring);
Eric Anholt673a3942008-07-30 12:06:12 -07002123
2124 return ret;
2125}
2126
Daniel Vetter48764bf2009-09-15 22:57:32 +02002127/**
Eric Anholt673a3942008-07-30 12:06:12 -07002128 * Ensures that all rendering to the object has completed and the object is
2129 * safe to unbind from the GTT or access from the CPU.
2130 */
Chris Wilson54cf91d2010-11-25 18:00:26 +00002131int
Chris Wilsonce453d82011-02-21 14:43:56 +00002132i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002133{
Eric Anholt673a3942008-07-30 12:06:12 -07002134 int ret;
2135
Eric Anholte47c68e2008-11-14 13:35:19 -08002136 /* This function only exists to support waiting for existing rendering,
2137 * not for emitting required flushes.
Eric Anholt673a3942008-07-30 12:06:12 -07002138 */
Chris Wilson05394f32010-11-08 19:18:58 +00002139 BUG_ON((obj->base.write_domain & I915_GEM_GPU_DOMAINS) != 0);
Eric Anholt673a3942008-07-30 12:06:12 -07002140
2141 /* If there is rendering queued on the buffer being evicted, wait for
2142 * it.
2143 */
Chris Wilson05394f32010-11-08 19:18:58 +00002144 if (obj->active) {
Chris Wilsonce453d82011-02-21 14:43:56 +00002145 ret = i915_wait_request(obj->ring, obj->last_rendering_seqno);
Chris Wilson2cf34d72010-09-14 13:03:28 +01002146 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002147 return ret;
2148 }
2149
2150 return 0;
2151}
2152
2153/**
2154 * Unbinds an object from the GTT aperture.
2155 */
Jesse Barnes0f973f22009-01-26 17:10:45 -08002156int
Chris Wilson05394f32010-11-08 19:18:58 +00002157i915_gem_object_unbind(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002158{
Eric Anholt673a3942008-07-30 12:06:12 -07002159 int ret = 0;
2160
Chris Wilson05394f32010-11-08 19:18:58 +00002161 if (obj->gtt_space == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002162 return 0;
2163
Chris Wilson05394f32010-11-08 19:18:58 +00002164 if (obj->pin_count != 0) {
Eric Anholt673a3942008-07-30 12:06:12 -07002165 DRM_ERROR("Attempting to unbind pinned buffer\n");
2166 return -EINVAL;
2167 }
2168
Eric Anholt5323fd02009-09-09 11:50:45 -07002169 /* blow away mappings if mapped through GTT */
2170 i915_gem_release_mmap(obj);
2171
Eric Anholt673a3942008-07-30 12:06:12 -07002172 /* Move the object to the CPU domain to ensure that
2173 * any possible CPU writes while it's not in the GTT
2174 * are flushed when we go to remap it. This will
2175 * also ensure that all pending GPU writes are finished
2176 * before we unbind.
2177 */
Eric Anholte47c68e2008-11-14 13:35:19 -08002178 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Chris Wilson8dc17752010-07-23 23:18:51 +01002179 if (ret == -ERESTARTSYS)
Eric Anholt673a3942008-07-30 12:06:12 -07002180 return ret;
Chris Wilson8dc17752010-07-23 23:18:51 +01002181 /* Continue on if we fail due to EIO, the GPU is hung so we
2182 * should be safe and we need to cleanup or else we might
2183 * cause memory corruption through use-after-free.
2184 */
Chris Wilson812ed4922010-09-30 15:08:57 +01002185 if (ret) {
2186 i915_gem_clflush_object(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00002187 obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
Chris Wilson812ed4922010-09-30 15:08:57 +01002188 }
Eric Anholt673a3942008-07-30 12:06:12 -07002189
Daniel Vetter96b47b62009-12-15 17:50:00 +01002190 /* release the fence reg _after_ flushing */
Chris Wilsond9e86c02010-11-10 16:40:20 +00002191 ret = i915_gem_object_put_fence(obj);
2192 if (ret == -ERESTARTSYS)
2193 return ret;
Daniel Vetter96b47b62009-12-15 17:50:00 +01002194
Chris Wilsondb53a302011-02-03 11:57:46 +00002195 trace_i915_gem_object_unbind(obj);
2196
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002197 i915_gem_gtt_unbind_object(obj);
Chris Wilsone5281cc2010-10-28 13:45:36 +01002198 i915_gem_object_put_pages_gtt(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002199
Chris Wilson6299f992010-11-24 12:23:44 +00002200 list_del_init(&obj->gtt_list);
Chris Wilson05394f32010-11-08 19:18:58 +00002201 list_del_init(&obj->mm_list);
Daniel Vetter75e9e912010-11-04 17:11:09 +01002202 /* Avoid an unnecessary call to unbind on rebind. */
Chris Wilson05394f32010-11-08 19:18:58 +00002203 obj->map_and_fenceable = true;
Eric Anholt673a3942008-07-30 12:06:12 -07002204
Chris Wilson05394f32010-11-08 19:18:58 +00002205 drm_mm_put_block(obj->gtt_space);
2206 obj->gtt_space = NULL;
2207 obj->gtt_offset = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002208
Chris Wilson05394f32010-11-08 19:18:58 +00002209 if (i915_gem_object_is_purgeable(obj))
Chris Wilson963b4832009-09-20 23:03:54 +01002210 i915_gem_object_truncate(obj);
2211
Chris Wilson8dc17752010-07-23 23:18:51 +01002212 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002213}
2214
Chris Wilson88241782011-01-07 17:09:48 +00002215int
Chris Wilsondb53a302011-02-03 11:57:46 +00002216i915_gem_flush_ring(struct intel_ring_buffer *ring,
Chris Wilson54cf91d2010-11-25 18:00:26 +00002217 uint32_t invalidate_domains,
2218 uint32_t flush_domains)
2219{
Chris Wilson88241782011-01-07 17:09:48 +00002220 int ret;
2221
Chris Wilson36d527d2011-03-19 22:26:49 +00002222 if (((invalidate_domains | flush_domains) & I915_GEM_GPU_DOMAINS) == 0)
2223 return 0;
2224
Chris Wilsondb53a302011-02-03 11:57:46 +00002225 trace_i915_gem_ring_flush(ring, invalidate_domains, flush_domains);
2226
Chris Wilson88241782011-01-07 17:09:48 +00002227 ret = ring->flush(ring, invalidate_domains, flush_domains);
2228 if (ret)
2229 return ret;
2230
Chris Wilson36d527d2011-03-19 22:26:49 +00002231 if (flush_domains & I915_GEM_GPU_DOMAINS)
2232 i915_gem_process_flushing_list(ring, flush_domains);
2233
Chris Wilson88241782011-01-07 17:09:48 +00002234 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002235}
2236
Chris Wilsondb53a302011-02-03 11:57:46 +00002237static int i915_ring_idle(struct intel_ring_buffer *ring)
Chris Wilsona56ba562010-09-28 10:07:56 +01002238{
Chris Wilson88241782011-01-07 17:09:48 +00002239 int ret;
2240
Chris Wilson395b70b2010-10-28 21:28:46 +01002241 if (list_empty(&ring->gpu_write_list) && list_empty(&ring->active_list))
Chris Wilson64193402010-10-24 12:38:05 +01002242 return 0;
2243
Chris Wilson88241782011-01-07 17:09:48 +00002244 if (!list_empty(&ring->gpu_write_list)) {
Chris Wilsondb53a302011-02-03 11:57:46 +00002245 ret = i915_gem_flush_ring(ring,
Chris Wilson0ac74c62010-12-06 14:36:02 +00002246 I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
Chris Wilson88241782011-01-07 17:09:48 +00002247 if (ret)
2248 return ret;
2249 }
2250
Chris Wilsonce453d82011-02-21 14:43:56 +00002251 return i915_wait_request(ring, i915_gem_next_request_seqno(ring));
Chris Wilsona56ba562010-09-28 10:07:56 +01002252}
2253
Chris Wilsonb47eb4a2010-08-07 11:01:23 +01002254int
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002255i915_gpu_idle(struct drm_device *dev)
2256{
2257 drm_i915_private_t *dev_priv = dev->dev_private;
2258 bool lists_empty;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002259 int ret, i;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002260
Zou Nan haid1b851f2010-05-21 09:08:57 +08002261 lists_empty = (list_empty(&dev_priv->mm.flushing_list) &&
Chris Wilson395b70b2010-10-28 21:28:46 +01002262 list_empty(&dev_priv->mm.active_list));
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002263 if (lists_empty)
2264 return 0;
2265
2266 /* Flush everything onto the inactive list. */
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002267 for (i = 0; i < I915_NUM_RINGS; i++) {
Chris Wilsondb53a302011-02-03 11:57:46 +00002268 ret = i915_ring_idle(&dev_priv->ring[i]);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002269 if (ret)
2270 return ret;
2271 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08002272
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002273 return 0;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002274}
2275
Daniel Vetterc6642782010-11-12 13:46:18 +00002276static int sandybridge_write_fence_reg(struct drm_i915_gem_object *obj,
2277 struct intel_ring_buffer *pipelined)
Eric Anholt4e901fd2009-10-26 16:44:17 -07002278{
Chris Wilson05394f32010-11-08 19:18:58 +00002279 struct drm_device *dev = obj->base.dev;
Eric Anholt4e901fd2009-10-26 16:44:17 -07002280 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00002281 u32 size = obj->gtt_space->size;
2282 int regnum = obj->fence_reg;
Eric Anholt4e901fd2009-10-26 16:44:17 -07002283 uint64_t val;
2284
Chris Wilson05394f32010-11-08 19:18:58 +00002285 val = (uint64_t)((obj->gtt_offset + size - 4096) &
Daniel Vetterc6642782010-11-12 13:46:18 +00002286 0xfffff000) << 32;
Chris Wilson05394f32010-11-08 19:18:58 +00002287 val |= obj->gtt_offset & 0xfffff000;
2288 val |= (uint64_t)((obj->stride / 128) - 1) <<
Eric Anholt4e901fd2009-10-26 16:44:17 -07002289 SANDYBRIDGE_FENCE_PITCH_SHIFT;
2290
Chris Wilson05394f32010-11-08 19:18:58 +00002291 if (obj->tiling_mode == I915_TILING_Y)
Eric Anholt4e901fd2009-10-26 16:44:17 -07002292 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2293 val |= I965_FENCE_REG_VALID;
2294
Daniel Vetterc6642782010-11-12 13:46:18 +00002295 if (pipelined) {
2296 int ret = intel_ring_begin(pipelined, 6);
2297 if (ret)
2298 return ret;
2299
2300 intel_ring_emit(pipelined, MI_NOOP);
2301 intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(2));
2302 intel_ring_emit(pipelined, FENCE_REG_SANDYBRIDGE_0 + regnum*8);
2303 intel_ring_emit(pipelined, (u32)val);
2304 intel_ring_emit(pipelined, FENCE_REG_SANDYBRIDGE_0 + regnum*8 + 4);
2305 intel_ring_emit(pipelined, (u32)(val >> 32));
2306 intel_ring_advance(pipelined);
2307 } else
2308 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + regnum * 8, val);
2309
2310 return 0;
Eric Anholt4e901fd2009-10-26 16:44:17 -07002311}
2312
Daniel Vetterc6642782010-11-12 13:46:18 +00002313static int i965_write_fence_reg(struct drm_i915_gem_object *obj,
2314 struct intel_ring_buffer *pipelined)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002315{
Chris Wilson05394f32010-11-08 19:18:58 +00002316 struct drm_device *dev = obj->base.dev;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002317 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00002318 u32 size = obj->gtt_space->size;
2319 int regnum = obj->fence_reg;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002320 uint64_t val;
2321
Chris Wilson05394f32010-11-08 19:18:58 +00002322 val = (uint64_t)((obj->gtt_offset + size - 4096) &
Jesse Barnesde151cf2008-11-12 10:03:55 -08002323 0xfffff000) << 32;
Chris Wilson05394f32010-11-08 19:18:58 +00002324 val |= obj->gtt_offset & 0xfffff000;
2325 val |= ((obj->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
2326 if (obj->tiling_mode == I915_TILING_Y)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002327 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2328 val |= I965_FENCE_REG_VALID;
2329
Daniel Vetterc6642782010-11-12 13:46:18 +00002330 if (pipelined) {
2331 int ret = intel_ring_begin(pipelined, 6);
2332 if (ret)
2333 return ret;
2334
2335 intel_ring_emit(pipelined, MI_NOOP);
2336 intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(2));
2337 intel_ring_emit(pipelined, FENCE_REG_965_0 + regnum*8);
2338 intel_ring_emit(pipelined, (u32)val);
2339 intel_ring_emit(pipelined, FENCE_REG_965_0 + regnum*8 + 4);
2340 intel_ring_emit(pipelined, (u32)(val >> 32));
2341 intel_ring_advance(pipelined);
2342 } else
2343 I915_WRITE64(FENCE_REG_965_0 + regnum * 8, val);
2344
2345 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002346}
2347
Daniel Vetterc6642782010-11-12 13:46:18 +00002348static int i915_write_fence_reg(struct drm_i915_gem_object *obj,
2349 struct intel_ring_buffer *pipelined)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002350{
Chris Wilson05394f32010-11-08 19:18:58 +00002351 struct drm_device *dev = obj->base.dev;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002352 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00002353 u32 size = obj->gtt_space->size;
Daniel Vetterc6642782010-11-12 13:46:18 +00002354 u32 fence_reg, val, pitch_val;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002355 int tile_width;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002356
Daniel Vetterc6642782010-11-12 13:46:18 +00002357 if (WARN((obj->gtt_offset & ~I915_FENCE_START_MASK) ||
2358 (size & -size) != size ||
2359 (obj->gtt_offset & (size - 1)),
2360 "object 0x%08x [fenceable? %d] not 1M or pot-size (0x%08x) aligned\n",
2361 obj->gtt_offset, obj->map_and_fenceable, size))
2362 return -EINVAL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002363
Daniel Vetterc6642782010-11-12 13:46:18 +00002364 if (obj->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev))
Jesse Barnes0f973f22009-01-26 17:10:45 -08002365 tile_width = 128;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002366 else
Jesse Barnes0f973f22009-01-26 17:10:45 -08002367 tile_width = 512;
2368
2369 /* Note: pitch better be a power of two tile widths */
Chris Wilson05394f32010-11-08 19:18:58 +00002370 pitch_val = obj->stride / tile_width;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002371 pitch_val = ffs(pitch_val) - 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002372
Chris Wilson05394f32010-11-08 19:18:58 +00002373 val = obj->gtt_offset;
2374 if (obj->tiling_mode == I915_TILING_Y)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002375 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002376 val |= I915_FENCE_SIZE_BITS(size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002377 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2378 val |= I830_FENCE_REG_VALID;
2379
Chris Wilson05394f32010-11-08 19:18:58 +00002380 fence_reg = obj->fence_reg;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002381 if (fence_reg < 8)
2382 fence_reg = FENCE_REG_830_0 + fence_reg * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002383 else
Chris Wilsona00b10c2010-09-24 21:15:47 +01002384 fence_reg = FENCE_REG_945_8 + (fence_reg - 8) * 4;
Daniel Vetterc6642782010-11-12 13:46:18 +00002385
2386 if (pipelined) {
2387 int ret = intel_ring_begin(pipelined, 4);
2388 if (ret)
2389 return ret;
2390
2391 intel_ring_emit(pipelined, MI_NOOP);
2392 intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(1));
2393 intel_ring_emit(pipelined, fence_reg);
2394 intel_ring_emit(pipelined, val);
2395 intel_ring_advance(pipelined);
2396 } else
2397 I915_WRITE(fence_reg, val);
2398
2399 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002400}
2401
Daniel Vetterc6642782010-11-12 13:46:18 +00002402static int i830_write_fence_reg(struct drm_i915_gem_object *obj,
2403 struct intel_ring_buffer *pipelined)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002404{
Chris Wilson05394f32010-11-08 19:18:58 +00002405 struct drm_device *dev = obj->base.dev;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002406 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00002407 u32 size = obj->gtt_space->size;
2408 int regnum = obj->fence_reg;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002409 uint32_t val;
2410 uint32_t pitch_val;
2411
Daniel Vetterc6642782010-11-12 13:46:18 +00002412 if (WARN((obj->gtt_offset & ~I830_FENCE_START_MASK) ||
2413 (size & -size) != size ||
2414 (obj->gtt_offset & (size - 1)),
2415 "object 0x%08x not 512K or pot-size 0x%08x aligned\n",
2416 obj->gtt_offset, size))
2417 return -EINVAL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002418
Chris Wilson05394f32010-11-08 19:18:58 +00002419 pitch_val = obj->stride / 128;
Eric Anholte76a16d2009-05-26 17:44:56 -07002420 pitch_val = ffs(pitch_val) - 1;
Eric Anholte76a16d2009-05-26 17:44:56 -07002421
Chris Wilson05394f32010-11-08 19:18:58 +00002422 val = obj->gtt_offset;
2423 if (obj->tiling_mode == I915_TILING_Y)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002424 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Daniel Vetterc6642782010-11-12 13:46:18 +00002425 val |= I830_FENCE_SIZE_BITS(size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002426 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2427 val |= I830_FENCE_REG_VALID;
2428
Daniel Vetterc6642782010-11-12 13:46:18 +00002429 if (pipelined) {
2430 int ret = intel_ring_begin(pipelined, 4);
2431 if (ret)
2432 return ret;
2433
2434 intel_ring_emit(pipelined, MI_NOOP);
2435 intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(1));
2436 intel_ring_emit(pipelined, FENCE_REG_830_0 + regnum*4);
2437 intel_ring_emit(pipelined, val);
2438 intel_ring_advance(pipelined);
2439 } else
2440 I915_WRITE(FENCE_REG_830_0 + regnum * 4, val);
2441
2442 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002443}
2444
Chris Wilsond9e86c02010-11-10 16:40:20 +00002445static bool ring_passed_seqno(struct intel_ring_buffer *ring, u32 seqno)
2446{
2447 return i915_seqno_passed(ring->get_seqno(ring), seqno);
2448}
2449
2450static int
2451i915_gem_object_flush_fence(struct drm_i915_gem_object *obj,
Chris Wilsonce453d82011-02-21 14:43:56 +00002452 struct intel_ring_buffer *pipelined)
Chris Wilsond9e86c02010-11-10 16:40:20 +00002453{
2454 int ret;
2455
2456 if (obj->fenced_gpu_access) {
Chris Wilson88241782011-01-07 17:09:48 +00002457 if (obj->base.write_domain & I915_GEM_GPU_DOMAINS) {
Chris Wilsondb53a302011-02-03 11:57:46 +00002458 ret = i915_gem_flush_ring(obj->last_fenced_ring,
Chris Wilson88241782011-01-07 17:09:48 +00002459 0, obj->base.write_domain);
2460 if (ret)
2461 return ret;
2462 }
Chris Wilsond9e86c02010-11-10 16:40:20 +00002463
2464 obj->fenced_gpu_access = false;
2465 }
2466
2467 if (obj->last_fenced_seqno && pipelined != obj->last_fenced_ring) {
2468 if (!ring_passed_seqno(obj->last_fenced_ring,
2469 obj->last_fenced_seqno)) {
Chris Wilsondb53a302011-02-03 11:57:46 +00002470 ret = i915_wait_request(obj->last_fenced_ring,
Chris Wilsonce453d82011-02-21 14:43:56 +00002471 obj->last_fenced_seqno);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002472 if (ret)
2473 return ret;
2474 }
2475
2476 obj->last_fenced_seqno = 0;
2477 obj->last_fenced_ring = NULL;
2478 }
2479
Chris Wilson63256ec2011-01-04 18:42:07 +00002480 /* Ensure that all CPU reads are completed before installing a fence
2481 * and all writes before removing the fence.
2482 */
2483 if (obj->base.read_domains & I915_GEM_DOMAIN_GTT)
2484 mb();
2485
Chris Wilsond9e86c02010-11-10 16:40:20 +00002486 return 0;
2487}
2488
2489int
2490i915_gem_object_put_fence(struct drm_i915_gem_object *obj)
2491{
2492 int ret;
2493
2494 if (obj->tiling_mode)
2495 i915_gem_release_mmap(obj);
2496
Chris Wilsonce453d82011-02-21 14:43:56 +00002497 ret = i915_gem_object_flush_fence(obj, NULL);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002498 if (ret)
2499 return ret;
2500
2501 if (obj->fence_reg != I915_FENCE_REG_NONE) {
2502 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2503 i915_gem_clear_fence_reg(obj->base.dev,
2504 &dev_priv->fence_regs[obj->fence_reg]);
2505
2506 obj->fence_reg = I915_FENCE_REG_NONE;
2507 }
2508
2509 return 0;
2510}
2511
2512static struct drm_i915_fence_reg *
2513i915_find_fence_reg(struct drm_device *dev,
2514 struct intel_ring_buffer *pipelined)
Daniel Vetterae3db242010-02-19 11:51:58 +01002515{
Daniel Vetterae3db242010-02-19 11:51:58 +01002516 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002517 struct drm_i915_fence_reg *reg, *first, *avail;
2518 int i;
Daniel Vetterae3db242010-02-19 11:51:58 +01002519
2520 /* First try to find a free reg */
Chris Wilsond9e86c02010-11-10 16:40:20 +00002521 avail = NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002522 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2523 reg = &dev_priv->fence_regs[i];
2524 if (!reg->obj)
Chris Wilsond9e86c02010-11-10 16:40:20 +00002525 return reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01002526
Chris Wilson05394f32010-11-08 19:18:58 +00002527 if (!reg->obj->pin_count)
Chris Wilsond9e86c02010-11-10 16:40:20 +00002528 avail = reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01002529 }
2530
Chris Wilsond9e86c02010-11-10 16:40:20 +00002531 if (avail == NULL)
2532 return NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002533
2534 /* None available, try to steal one or wait for a user to finish */
Chris Wilsond9e86c02010-11-10 16:40:20 +00002535 avail = first = NULL;
2536 list_for_each_entry(reg, &dev_priv->mm.fence_list, lru_list) {
2537 if (reg->obj->pin_count)
Daniel Vetterae3db242010-02-19 11:51:58 +01002538 continue;
2539
Chris Wilsond9e86c02010-11-10 16:40:20 +00002540 if (first == NULL)
2541 first = reg;
2542
2543 if (!pipelined ||
2544 !reg->obj->last_fenced_ring ||
2545 reg->obj->last_fenced_ring == pipelined) {
2546 avail = reg;
2547 break;
2548 }
Daniel Vetterae3db242010-02-19 11:51:58 +01002549 }
2550
Chris Wilsond9e86c02010-11-10 16:40:20 +00002551 if (avail == NULL)
2552 avail = first;
Daniel Vetterae3db242010-02-19 11:51:58 +01002553
Chris Wilsona00b10c2010-09-24 21:15:47 +01002554 return avail;
Daniel Vetterae3db242010-02-19 11:51:58 +01002555}
2556
Jesse Barnesde151cf2008-11-12 10:03:55 -08002557/**
Chris Wilsond9e86c02010-11-10 16:40:20 +00002558 * i915_gem_object_get_fence - set up a fence reg for an object
Jesse Barnesde151cf2008-11-12 10:03:55 -08002559 * @obj: object to map through a fence reg
Chris Wilsond9e86c02010-11-10 16:40:20 +00002560 * @pipelined: ring on which to queue the change, or NULL for CPU access
2561 * @interruptible: must we wait uninterruptibly for the register to retire?
Jesse Barnesde151cf2008-11-12 10:03:55 -08002562 *
2563 * When mapping objects through the GTT, userspace wants to be able to write
2564 * to them without having to worry about swizzling if the object is tiled.
2565 *
2566 * This function walks the fence regs looking for a free one for @obj,
2567 * stealing one if it can't find any.
2568 *
2569 * It then sets up the reg based on the object's properties: address, pitch
2570 * and tiling format.
2571 */
Chris Wilson8c4b8c32009-06-17 22:08:52 +01002572int
Chris Wilsond9e86c02010-11-10 16:40:20 +00002573i915_gem_object_get_fence(struct drm_i915_gem_object *obj,
Chris Wilsonce453d82011-02-21 14:43:56 +00002574 struct intel_ring_buffer *pipelined)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002575{
Chris Wilson05394f32010-11-08 19:18:58 +00002576 struct drm_device *dev = obj->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002577 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002578 struct drm_i915_fence_reg *reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01002579 int ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002580
Chris Wilson6bda10d2010-12-05 21:04:18 +00002581 /* XXX disable pipelining. There are bugs. Shocking. */
2582 pipelined = NULL;
2583
Chris Wilsond9e86c02010-11-10 16:40:20 +00002584 /* Just update our place in the LRU if our fence is getting reused. */
Chris Wilson05394f32010-11-08 19:18:58 +00002585 if (obj->fence_reg != I915_FENCE_REG_NONE) {
2586 reg = &dev_priv->fence_regs[obj->fence_reg];
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002587 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002588
Chris Wilson29c5a582011-03-17 15:23:22 +00002589 if (obj->tiling_changed) {
2590 ret = i915_gem_object_flush_fence(obj, pipelined);
2591 if (ret)
2592 return ret;
2593
2594 if (!obj->fenced_gpu_access && !obj->last_fenced_seqno)
2595 pipelined = NULL;
2596
2597 if (pipelined) {
2598 reg->setup_seqno =
2599 i915_gem_next_request_seqno(pipelined);
2600 obj->last_fenced_seqno = reg->setup_seqno;
2601 obj->last_fenced_ring = pipelined;
2602 }
2603
2604 goto update;
2605 }
Chris Wilsond9e86c02010-11-10 16:40:20 +00002606
2607 if (!pipelined) {
2608 if (reg->setup_seqno) {
2609 if (!ring_passed_seqno(obj->last_fenced_ring,
2610 reg->setup_seqno)) {
Chris Wilsondb53a302011-02-03 11:57:46 +00002611 ret = i915_wait_request(obj->last_fenced_ring,
Chris Wilsonce453d82011-02-21 14:43:56 +00002612 reg->setup_seqno);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002613 if (ret)
2614 return ret;
2615 }
2616
2617 reg->setup_seqno = 0;
2618 }
2619 } else if (obj->last_fenced_ring &&
2620 obj->last_fenced_ring != pipelined) {
Chris Wilsonce453d82011-02-21 14:43:56 +00002621 ret = i915_gem_object_flush_fence(obj, pipelined);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002622 if (ret)
2623 return ret;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002624 }
2625
Eric Anholta09ba7f2009-08-29 12:49:51 -07002626 return 0;
2627 }
2628
Chris Wilsond9e86c02010-11-10 16:40:20 +00002629 reg = i915_find_fence_reg(dev, pipelined);
2630 if (reg == NULL)
2631 return -ENOSPC;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002632
Chris Wilsonce453d82011-02-21 14:43:56 +00002633 ret = i915_gem_object_flush_fence(obj, pipelined);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002634 if (ret)
Daniel Vetterae3db242010-02-19 11:51:58 +01002635 return ret;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002636
Chris Wilsond9e86c02010-11-10 16:40:20 +00002637 if (reg->obj) {
2638 struct drm_i915_gem_object *old = reg->obj;
2639
2640 drm_gem_object_reference(&old->base);
2641
2642 if (old->tiling_mode)
2643 i915_gem_release_mmap(old);
2644
Chris Wilsonce453d82011-02-21 14:43:56 +00002645 ret = i915_gem_object_flush_fence(old, pipelined);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002646 if (ret) {
2647 drm_gem_object_unreference(&old->base);
2648 return ret;
2649 }
2650
2651 if (old->last_fenced_seqno == 0 && obj->last_fenced_seqno == 0)
2652 pipelined = NULL;
2653
2654 old->fence_reg = I915_FENCE_REG_NONE;
2655 old->last_fenced_ring = pipelined;
2656 old->last_fenced_seqno =
Chris Wilsondb53a302011-02-03 11:57:46 +00002657 pipelined ? i915_gem_next_request_seqno(pipelined) : 0;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002658
2659 drm_gem_object_unreference(&old->base);
2660 } else if (obj->last_fenced_seqno == 0)
2661 pipelined = NULL;
Eric Anholta09ba7f2009-08-29 12:49:51 -07002662
Jesse Barnesde151cf2008-11-12 10:03:55 -08002663 reg->obj = obj;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002664 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
2665 obj->fence_reg = reg - dev_priv->fence_regs;
2666 obj->last_fenced_ring = pipelined;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002667
Chris Wilsond9e86c02010-11-10 16:40:20 +00002668 reg->setup_seqno =
Chris Wilsondb53a302011-02-03 11:57:46 +00002669 pipelined ? i915_gem_next_request_seqno(pipelined) : 0;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002670 obj->last_fenced_seqno = reg->setup_seqno;
2671
2672update:
2673 obj->tiling_changed = false;
Chris Wilsone259bef2010-09-17 00:32:02 +01002674 switch (INTEL_INFO(dev)->gen) {
2675 case 6:
Daniel Vetterc6642782010-11-12 13:46:18 +00002676 ret = sandybridge_write_fence_reg(obj, pipelined);
Chris Wilsone259bef2010-09-17 00:32:02 +01002677 break;
2678 case 5:
2679 case 4:
Daniel Vetterc6642782010-11-12 13:46:18 +00002680 ret = i965_write_fence_reg(obj, pipelined);
Chris Wilsone259bef2010-09-17 00:32:02 +01002681 break;
2682 case 3:
Daniel Vetterc6642782010-11-12 13:46:18 +00002683 ret = i915_write_fence_reg(obj, pipelined);
Chris Wilsone259bef2010-09-17 00:32:02 +01002684 break;
2685 case 2:
Daniel Vetterc6642782010-11-12 13:46:18 +00002686 ret = i830_write_fence_reg(obj, pipelined);
Chris Wilsone259bef2010-09-17 00:32:02 +01002687 break;
2688 }
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002689
Daniel Vetterc6642782010-11-12 13:46:18 +00002690 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002691}
2692
2693/**
2694 * i915_gem_clear_fence_reg - clear out fence register info
2695 * @obj: object to clear
2696 *
2697 * Zeroes out the fence register itself and clears out the associated
Chris Wilson05394f32010-11-08 19:18:58 +00002698 * data structures in dev_priv and obj.
Jesse Barnesde151cf2008-11-12 10:03:55 -08002699 */
2700static void
Chris Wilsond9e86c02010-11-10 16:40:20 +00002701i915_gem_clear_fence_reg(struct drm_device *dev,
2702 struct drm_i915_fence_reg *reg)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002703{
Jesse Barnes79e53942008-11-07 14:24:08 -08002704 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002705 uint32_t fence_reg = reg - dev_priv->fence_regs;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002706
Chris Wilsone259bef2010-09-17 00:32:02 +01002707 switch (INTEL_INFO(dev)->gen) {
2708 case 6:
Chris Wilsond9e86c02010-11-10 16:40:20 +00002709 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + fence_reg*8, 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002710 break;
2711 case 5:
2712 case 4:
Chris Wilsond9e86c02010-11-10 16:40:20 +00002713 I915_WRITE64(FENCE_REG_965_0 + fence_reg*8, 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002714 break;
2715 case 3:
Chris Wilsond9e86c02010-11-10 16:40:20 +00002716 if (fence_reg >= 8)
2717 fence_reg = FENCE_REG_945_8 + (fence_reg - 8) * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002718 else
Chris Wilsone259bef2010-09-17 00:32:02 +01002719 case 2:
Chris Wilsond9e86c02010-11-10 16:40:20 +00002720 fence_reg = FENCE_REG_830_0 + fence_reg * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002721
2722 I915_WRITE(fence_reg, 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002723 break;
Eric Anholtdc529a42009-03-10 22:34:49 -07002724 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002725
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002726 list_del_init(&reg->lru_list);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002727 reg->obj = NULL;
2728 reg->setup_seqno = 0;
Chris Wilson52dc7d32009-06-06 09:46:01 +01002729}
2730
2731/**
Eric Anholt673a3942008-07-30 12:06:12 -07002732 * Finds free space in the GTT aperture and binds the object there.
2733 */
2734static int
Chris Wilson05394f32010-11-08 19:18:58 +00002735i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
Daniel Vetter920afa72010-09-16 17:54:23 +02002736 unsigned alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01002737 bool map_and_fenceable)
Eric Anholt673a3942008-07-30 12:06:12 -07002738{
Chris Wilson05394f32010-11-08 19:18:58 +00002739 struct drm_device *dev = obj->base.dev;
Eric Anholt673a3942008-07-30 12:06:12 -07002740 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07002741 struct drm_mm_node *free_space;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002742 gfp_t gfpmask = __GFP_NORETRY | __GFP_NOWARN;
Daniel Vetter5e783302010-11-14 22:32:36 +01002743 u32 size, fence_size, fence_alignment, unfenced_alignment;
Daniel Vetter75e9e912010-11-04 17:11:09 +01002744 bool mappable, fenceable;
Chris Wilson07f73f62009-09-14 16:50:30 +01002745 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002746
Chris Wilson05394f32010-11-08 19:18:58 +00002747 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01002748 DRM_ERROR("Attempting to bind a purgeable object\n");
2749 return -EINVAL;
2750 }
2751
Chris Wilson05394f32010-11-08 19:18:58 +00002752 fence_size = i915_gem_get_gtt_size(obj);
2753 fence_alignment = i915_gem_get_gtt_alignment(obj);
2754 unfenced_alignment = i915_gem_get_unfenced_gtt_alignment(obj);
Chris Wilsona00b10c2010-09-24 21:15:47 +01002755
Eric Anholt673a3942008-07-30 12:06:12 -07002756 if (alignment == 0)
Daniel Vetter5e783302010-11-14 22:32:36 +01002757 alignment = map_and_fenceable ? fence_alignment :
2758 unfenced_alignment;
Daniel Vetter75e9e912010-11-04 17:11:09 +01002759 if (map_and_fenceable && alignment & (fence_alignment - 1)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002760 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2761 return -EINVAL;
2762 }
2763
Chris Wilson05394f32010-11-08 19:18:58 +00002764 size = map_and_fenceable ? fence_size : obj->base.size;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002765
Chris Wilson654fc602010-05-27 13:18:21 +01002766 /* If the object is bigger than the entire aperture, reject it early
2767 * before evicting everything in a vain attempt to find space.
2768 */
Chris Wilson05394f32010-11-08 19:18:58 +00002769 if (obj->base.size >
Daniel Vetter75e9e912010-11-04 17:11:09 +01002770 (map_and_fenceable ? dev_priv->mm.gtt_mappable_end : dev_priv->mm.gtt_total)) {
Chris Wilson654fc602010-05-27 13:18:21 +01002771 DRM_ERROR("Attempting to bind an object larger than the aperture\n");
2772 return -E2BIG;
2773 }
2774
Eric Anholt673a3942008-07-30 12:06:12 -07002775 search_free:
Daniel Vetter75e9e912010-11-04 17:11:09 +01002776 if (map_and_fenceable)
Daniel Vetter920afa72010-09-16 17:54:23 +02002777 free_space =
2778 drm_mm_search_free_in_range(&dev_priv->mm.gtt_space,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002779 size, alignment, 0,
Daniel Vetter920afa72010-09-16 17:54:23 +02002780 dev_priv->mm.gtt_mappable_end,
2781 0);
2782 else
2783 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002784 size, alignment, 0);
Daniel Vetter920afa72010-09-16 17:54:23 +02002785
2786 if (free_space != NULL) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01002787 if (map_and_fenceable)
Chris Wilson05394f32010-11-08 19:18:58 +00002788 obj->gtt_space =
Daniel Vetter920afa72010-09-16 17:54:23 +02002789 drm_mm_get_block_range_generic(free_space,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002790 size, alignment, 0,
Daniel Vetter920afa72010-09-16 17:54:23 +02002791 dev_priv->mm.gtt_mappable_end,
2792 0);
2793 else
Chris Wilson05394f32010-11-08 19:18:58 +00002794 obj->gtt_space =
Chris Wilsona00b10c2010-09-24 21:15:47 +01002795 drm_mm_get_block(free_space, size, alignment);
Daniel Vetter920afa72010-09-16 17:54:23 +02002796 }
Chris Wilson05394f32010-11-08 19:18:58 +00002797 if (obj->gtt_space == NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -07002798 /* If the gtt is empty and we're still having trouble
2799 * fitting our object in, we're out of memory.
2800 */
Daniel Vetter75e9e912010-11-04 17:11:09 +01002801 ret = i915_gem_evict_something(dev, size, alignment,
2802 map_and_fenceable);
Chris Wilson97311292009-09-21 00:22:34 +01002803 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002804 return ret;
Chris Wilson97311292009-09-21 00:22:34 +01002805
Eric Anholt673a3942008-07-30 12:06:12 -07002806 goto search_free;
2807 }
2808
Chris Wilsone5281cc2010-10-28 13:45:36 +01002809 ret = i915_gem_object_get_pages_gtt(obj, gfpmask);
Eric Anholt673a3942008-07-30 12:06:12 -07002810 if (ret) {
Chris Wilson05394f32010-11-08 19:18:58 +00002811 drm_mm_put_block(obj->gtt_space);
2812 obj->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002813
2814 if (ret == -ENOMEM) {
Chris Wilson809b6332011-01-10 17:33:15 +00002815 /* first try to reclaim some memory by clearing the GTT */
2816 ret = i915_gem_evict_everything(dev, false);
Chris Wilson07f73f62009-09-14 16:50:30 +01002817 if (ret) {
Chris Wilson07f73f62009-09-14 16:50:30 +01002818 /* now try to shrink everyone else */
Chris Wilson4bdadb92010-01-27 13:36:32 +00002819 if (gfpmask) {
2820 gfpmask = 0;
2821 goto search_free;
Chris Wilson07f73f62009-09-14 16:50:30 +01002822 }
2823
Chris Wilson809b6332011-01-10 17:33:15 +00002824 return -ENOMEM;
Chris Wilson07f73f62009-09-14 16:50:30 +01002825 }
2826
2827 goto search_free;
2828 }
2829
Eric Anholt673a3942008-07-30 12:06:12 -07002830 return ret;
2831 }
2832
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002833 ret = i915_gem_gtt_bind_object(obj);
2834 if (ret) {
Chris Wilsone5281cc2010-10-28 13:45:36 +01002835 i915_gem_object_put_pages_gtt(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00002836 drm_mm_put_block(obj->gtt_space);
2837 obj->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002838
Chris Wilson809b6332011-01-10 17:33:15 +00002839 if (i915_gem_evict_everything(dev, false))
Chris Wilson07f73f62009-09-14 16:50:30 +01002840 return ret;
Chris Wilson07f73f62009-09-14 16:50:30 +01002841
2842 goto search_free;
Eric Anholt673a3942008-07-30 12:06:12 -07002843 }
Eric Anholt673a3942008-07-30 12:06:12 -07002844
Chris Wilson6299f992010-11-24 12:23:44 +00002845 list_add_tail(&obj->gtt_list, &dev_priv->mm.gtt_list);
Chris Wilson05394f32010-11-08 19:18:58 +00002846 list_add_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
Chris Wilsonbf1a1092010-08-07 11:01:20 +01002847
Eric Anholt673a3942008-07-30 12:06:12 -07002848 /* Assert that the object is not currently in any GPU domain. As it
2849 * wasn't in the GTT, there shouldn't be any way it could have been in
2850 * a GPU cache
2851 */
Chris Wilson05394f32010-11-08 19:18:58 +00002852 BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
2853 BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
Eric Anholt673a3942008-07-30 12:06:12 -07002854
Chris Wilson6299f992010-11-24 12:23:44 +00002855 obj->gtt_offset = obj->gtt_space->start;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002856
Daniel Vetter75e9e912010-11-04 17:11:09 +01002857 fenceable =
Chris Wilson05394f32010-11-08 19:18:58 +00002858 obj->gtt_space->size == fence_size &&
2859 (obj->gtt_space->start & (fence_alignment -1)) == 0;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002860
Daniel Vetter75e9e912010-11-04 17:11:09 +01002861 mappable =
Chris Wilson05394f32010-11-08 19:18:58 +00002862 obj->gtt_offset + obj->base.size <= dev_priv->mm.gtt_mappable_end;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002863
Chris Wilson05394f32010-11-08 19:18:58 +00002864 obj->map_and_fenceable = mappable && fenceable;
Daniel Vetter75e9e912010-11-04 17:11:09 +01002865
Chris Wilsondb53a302011-02-03 11:57:46 +00002866 trace_i915_gem_object_bind(obj, map_and_fenceable);
Eric Anholt673a3942008-07-30 12:06:12 -07002867 return 0;
2868}
2869
2870void
Chris Wilson05394f32010-11-08 19:18:58 +00002871i915_gem_clflush_object(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002872{
Eric Anholt673a3942008-07-30 12:06:12 -07002873 /* If we don't have a page list set up, then we're not pinned
2874 * to GPU, and we can ignore the cache flush because it'll happen
2875 * again at bind time.
2876 */
Chris Wilson05394f32010-11-08 19:18:58 +00002877 if (obj->pages == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002878 return;
2879
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002880 trace_i915_gem_object_clflush(obj);
Eric Anholtcfa16a02009-05-26 18:46:16 -07002881
Chris Wilson05394f32010-11-08 19:18:58 +00002882 drm_clflush_pages(obj->pages, obj->base.size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002883}
2884
Eric Anholte47c68e2008-11-14 13:35:19 -08002885/** Flushes any GPU write domain for the object if it's dirty. */
Chris Wilson88241782011-01-07 17:09:48 +00002886static int
Chris Wilson3619df02010-11-28 15:37:17 +00002887i915_gem_object_flush_gpu_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08002888{
Chris Wilson05394f32010-11-08 19:18:58 +00002889 if ((obj->base.write_domain & I915_GEM_GPU_DOMAINS) == 0)
Chris Wilson88241782011-01-07 17:09:48 +00002890 return 0;
Eric Anholte47c68e2008-11-14 13:35:19 -08002891
2892 /* Queue the GPU write cache flushing we need. */
Chris Wilsondb53a302011-02-03 11:57:46 +00002893 return i915_gem_flush_ring(obj->ring, 0, obj->base.write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002894}
2895
2896/** Flushes the GTT write domain for the object if it's dirty. */
2897static void
Chris Wilson05394f32010-11-08 19:18:58 +00002898i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08002899{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002900 uint32_t old_write_domain;
2901
Chris Wilson05394f32010-11-08 19:18:58 +00002902 if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
Eric Anholte47c68e2008-11-14 13:35:19 -08002903 return;
2904
Chris Wilson63256ec2011-01-04 18:42:07 +00002905 /* No actual flushing is required for the GTT write domain. Writes
Eric Anholte47c68e2008-11-14 13:35:19 -08002906 * to it immediately go to main memory as far as we know, so there's
2907 * no chipset flush. It also doesn't land in render cache.
Chris Wilson63256ec2011-01-04 18:42:07 +00002908 *
2909 * However, we do have to enforce the order so that all writes through
2910 * the GTT land before any writes to the device, such as updates to
2911 * the GATT itself.
Eric Anholte47c68e2008-11-14 13:35:19 -08002912 */
Chris Wilson63256ec2011-01-04 18:42:07 +00002913 wmb();
2914
Chris Wilson4a684a42010-10-28 14:44:08 +01002915 i915_gem_release_mmap(obj);
2916
Chris Wilson05394f32010-11-08 19:18:58 +00002917 old_write_domain = obj->base.write_domain;
2918 obj->base.write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002919
2920 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00002921 obj->base.read_domains,
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002922 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002923}
2924
2925/** Flushes the CPU write domain for the object if it's dirty. */
2926static void
Chris Wilson05394f32010-11-08 19:18:58 +00002927i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08002928{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002929 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002930
Chris Wilson05394f32010-11-08 19:18:58 +00002931 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
Eric Anholte47c68e2008-11-14 13:35:19 -08002932 return;
2933
2934 i915_gem_clflush_object(obj);
Daniel Vetter40ce6572010-11-05 18:12:18 +01002935 intel_gtt_chipset_flush();
Chris Wilson05394f32010-11-08 19:18:58 +00002936 old_write_domain = obj->base.write_domain;
2937 obj->base.write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002938
2939 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00002940 obj->base.read_domains,
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002941 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002942}
2943
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002944/**
2945 * Moves a single object to the GTT read, and possibly write domain.
2946 *
2947 * This function returns when the move is complete, including waiting on
2948 * flushes to occur.
2949 */
Jesse Barnes79e53942008-11-07 14:24:08 -08002950int
Chris Wilson20217462010-11-23 15:26:33 +00002951i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002952{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002953 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002954 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002955
Eric Anholt02354392008-11-26 13:58:13 -08002956 /* Not valid to be called on unbound objects. */
Chris Wilson05394f32010-11-08 19:18:58 +00002957 if (obj->gtt_space == NULL)
Eric Anholt02354392008-11-26 13:58:13 -08002958 return -EINVAL;
2959
Chris Wilson8d7e3de2011-02-07 15:23:02 +00002960 if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
2961 return 0;
2962
Chris Wilson88241782011-01-07 17:09:48 +00002963 ret = i915_gem_object_flush_gpu_write_domain(obj);
2964 if (ret)
2965 return ret;
2966
Chris Wilson87ca9c82010-12-02 09:42:56 +00002967 if (obj->pending_gpu_write || write) {
Chris Wilsonce453d82011-02-21 14:43:56 +00002968 ret = i915_gem_object_wait_rendering(obj);
Chris Wilson87ca9c82010-12-02 09:42:56 +00002969 if (ret)
2970 return ret;
2971 }
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002972
Chris Wilson72133422010-09-13 23:56:38 +01002973 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002974
Chris Wilson05394f32010-11-08 19:18:58 +00002975 old_write_domain = obj->base.write_domain;
2976 old_read_domains = obj->base.read_domains;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002977
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002978 /* It should now be out of any other write domains, and we can update
2979 * the domain values for our changes.
2980 */
Chris Wilson05394f32010-11-08 19:18:58 +00002981 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2982 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002983 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00002984 obj->base.read_domains = I915_GEM_DOMAIN_GTT;
2985 obj->base.write_domain = I915_GEM_DOMAIN_GTT;
2986 obj->dirty = 1;
Eric Anholte47c68e2008-11-14 13:35:19 -08002987 }
2988
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002989 trace_i915_gem_object_change_domain(obj,
2990 old_read_domains,
2991 old_write_domain);
2992
Eric Anholte47c68e2008-11-14 13:35:19 -08002993 return 0;
2994}
2995
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002996/*
2997 * Prepare buffer for display plane. Use uninterruptible for possible flush
2998 * wait, as in modesetting process we're not supposed to be interrupted.
2999 */
3000int
Chris Wilson05394f32010-11-08 19:18:58 +00003001i915_gem_object_set_to_display_plane(struct drm_i915_gem_object *obj,
Chris Wilson919926a2010-11-12 13:42:53 +00003002 struct intel_ring_buffer *pipelined)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003003{
Daniel Vetterba3d8d72010-02-11 22:37:04 +01003004 uint32_t old_read_domains;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003005 int ret;
3006
3007 /* Not valid to be called on unbound objects. */
Chris Wilson05394f32010-11-08 19:18:58 +00003008 if (obj->gtt_space == NULL)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003009 return -EINVAL;
3010
Chris Wilson88241782011-01-07 17:09:48 +00003011 ret = i915_gem_object_flush_gpu_write_domain(obj);
3012 if (ret)
3013 return ret;
3014
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003015
Chris Wilsonced270f2010-09-26 22:47:46 +01003016 /* Currently, we are always called from an non-interruptible context. */
Chris Wilson0be73282010-12-06 14:36:27 +00003017 if (pipelined != obj->ring) {
Chris Wilsonce453d82011-02-21 14:43:56 +00003018 ret = i915_gem_object_wait_rendering(obj);
Chris Wilsonced270f2010-09-26 22:47:46 +01003019 if (ret)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003020 return ret;
3021 }
3022
Chris Wilsonb118c1e2010-05-27 13:18:14 +01003023 i915_gem_object_flush_cpu_write_domain(obj);
3024
Chris Wilson05394f32010-11-08 19:18:58 +00003025 old_read_domains = obj->base.read_domains;
3026 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003027
3028 trace_i915_gem_object_change_domain(obj,
3029 old_read_domains,
Chris Wilson05394f32010-11-08 19:18:58 +00003030 obj->base.write_domain);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003031
3032 return 0;
3033}
3034
Chris Wilson85345512010-11-13 09:49:11 +00003035int
Chris Wilsonce453d82011-02-21 14:43:56 +00003036i915_gem_object_flush_gpu(struct drm_i915_gem_object *obj)
Chris Wilson85345512010-11-13 09:49:11 +00003037{
Chris Wilson88241782011-01-07 17:09:48 +00003038 int ret;
3039
Chris Wilson85345512010-11-13 09:49:11 +00003040 if (!obj->active)
3041 return 0;
3042
Chris Wilson88241782011-01-07 17:09:48 +00003043 if (obj->base.write_domain & I915_GEM_GPU_DOMAINS) {
Chris Wilsondb53a302011-02-03 11:57:46 +00003044 ret = i915_gem_flush_ring(obj->ring, 0, obj->base.write_domain);
Chris Wilson88241782011-01-07 17:09:48 +00003045 if (ret)
3046 return ret;
3047 }
Chris Wilson85345512010-11-13 09:49:11 +00003048
Chris Wilsonce453d82011-02-21 14:43:56 +00003049 return i915_gem_object_wait_rendering(obj);
Chris Wilson85345512010-11-13 09:49:11 +00003050}
3051
Eric Anholte47c68e2008-11-14 13:35:19 -08003052/**
3053 * Moves a single object to the CPU read, and possibly write domain.
3054 *
3055 * This function returns when the move is complete, including waiting on
3056 * flushes to occur.
3057 */
3058static int
Chris Wilson919926a2010-11-12 13:42:53 +00003059i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholte47c68e2008-11-14 13:35:19 -08003060{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003061 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003062 int ret;
3063
Chris Wilson8d7e3de2011-02-07 15:23:02 +00003064 if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
3065 return 0;
3066
Chris Wilson88241782011-01-07 17:09:48 +00003067 ret = i915_gem_object_flush_gpu_write_domain(obj);
3068 if (ret)
3069 return ret;
3070
Chris Wilsonce453d82011-02-21 14:43:56 +00003071 ret = i915_gem_object_wait_rendering(obj);
Daniel Vetterde18a292010-11-27 22:30:41 +01003072 if (ret)
Eric Anholte47c68e2008-11-14 13:35:19 -08003073 return ret;
3074
3075 i915_gem_object_flush_gtt_write_domain(obj);
3076
3077 /* If we have a partially-valid cache of the object in the CPU,
3078 * finish invalidating it and free the per-page flags.
3079 */
3080 i915_gem_object_set_to_full_cpu_read_domain(obj);
3081
Chris Wilson05394f32010-11-08 19:18:58 +00003082 old_write_domain = obj->base.write_domain;
3083 old_read_domains = obj->base.read_domains;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003084
Eric Anholte47c68e2008-11-14 13:35:19 -08003085 /* Flush the CPU cache if it's still invalid. */
Chris Wilson05394f32010-11-08 19:18:58 +00003086 if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
Eric Anholte47c68e2008-11-14 13:35:19 -08003087 i915_gem_clflush_object(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003088
Chris Wilson05394f32010-11-08 19:18:58 +00003089 obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003090 }
3091
3092 /* It should now be out of any other write domains, and we can update
3093 * the domain values for our changes.
3094 */
Chris Wilson05394f32010-11-08 19:18:58 +00003095 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
Eric Anholte47c68e2008-11-14 13:35:19 -08003096
3097 /* If we're writing through the CPU, then the GPU read domains will
3098 * need to be invalidated at next use.
3099 */
3100 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00003101 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3102 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003103 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003104
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003105 trace_i915_gem_object_change_domain(obj,
3106 old_read_domains,
3107 old_write_domain);
3108
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003109 return 0;
3110}
3111
Eric Anholt673a3942008-07-30 12:06:12 -07003112/**
Eric Anholte47c68e2008-11-14 13:35:19 -08003113 * Moves the object from a partially CPU read to a full one.
Eric Anholt673a3942008-07-30 12:06:12 -07003114 *
Eric Anholte47c68e2008-11-14 13:35:19 -08003115 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
3116 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
3117 */
3118static void
Chris Wilson05394f32010-11-08 19:18:58 +00003119i915_gem_object_set_to_full_cpu_read_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08003120{
Chris Wilson05394f32010-11-08 19:18:58 +00003121 if (!obj->page_cpu_valid)
Eric Anholte47c68e2008-11-14 13:35:19 -08003122 return;
3123
3124 /* If we're partially in the CPU read domain, finish moving it in.
3125 */
Chris Wilson05394f32010-11-08 19:18:58 +00003126 if (obj->base.read_domains & I915_GEM_DOMAIN_CPU) {
Eric Anholte47c68e2008-11-14 13:35:19 -08003127 int i;
3128
Chris Wilson05394f32010-11-08 19:18:58 +00003129 for (i = 0; i <= (obj->base.size - 1) / PAGE_SIZE; i++) {
3130 if (obj->page_cpu_valid[i])
Eric Anholte47c68e2008-11-14 13:35:19 -08003131 continue;
Chris Wilson05394f32010-11-08 19:18:58 +00003132 drm_clflush_pages(obj->pages + i, 1);
Eric Anholte47c68e2008-11-14 13:35:19 -08003133 }
Eric Anholte47c68e2008-11-14 13:35:19 -08003134 }
3135
3136 /* Free the page_cpu_valid mappings which are now stale, whether
3137 * or not we've got I915_GEM_DOMAIN_CPU.
3138 */
Chris Wilson05394f32010-11-08 19:18:58 +00003139 kfree(obj->page_cpu_valid);
3140 obj->page_cpu_valid = NULL;
Eric Anholte47c68e2008-11-14 13:35:19 -08003141}
3142
3143/**
3144 * Set the CPU read domain on a range of the object.
3145 *
3146 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
3147 * not entirely valid. The page_cpu_valid member of the object flags which
3148 * pages have been flushed, and will be respected by
3149 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
3150 * of the whole object.
3151 *
3152 * This function returns when the move is complete, including waiting on
3153 * flushes to occur.
Eric Anholt673a3942008-07-30 12:06:12 -07003154 */
3155static int
Chris Wilson05394f32010-11-08 19:18:58 +00003156i915_gem_object_set_cpu_read_domain_range(struct drm_i915_gem_object *obj,
Eric Anholte47c68e2008-11-14 13:35:19 -08003157 uint64_t offset, uint64_t size)
Eric Anholt673a3942008-07-30 12:06:12 -07003158{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003159 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003160 int i, ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003161
Chris Wilson05394f32010-11-08 19:18:58 +00003162 if (offset == 0 && size == obj->base.size)
Eric Anholte47c68e2008-11-14 13:35:19 -08003163 return i915_gem_object_set_to_cpu_domain(obj, 0);
3164
Chris Wilson88241782011-01-07 17:09:48 +00003165 ret = i915_gem_object_flush_gpu_write_domain(obj);
3166 if (ret)
3167 return ret;
3168
Chris Wilsonce453d82011-02-21 14:43:56 +00003169 ret = i915_gem_object_wait_rendering(obj);
Daniel Vetterde18a292010-11-27 22:30:41 +01003170 if (ret)
Eric Anholte47c68e2008-11-14 13:35:19 -08003171 return ret;
Daniel Vetterde18a292010-11-27 22:30:41 +01003172
Eric Anholte47c68e2008-11-14 13:35:19 -08003173 i915_gem_object_flush_gtt_write_domain(obj);
3174
3175 /* If we're already fully in the CPU read domain, we're done. */
Chris Wilson05394f32010-11-08 19:18:58 +00003176 if (obj->page_cpu_valid == NULL &&
3177 (obj->base.read_domains & I915_GEM_DOMAIN_CPU) != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07003178 return 0;
3179
Eric Anholte47c68e2008-11-14 13:35:19 -08003180 /* Otherwise, create/clear the per-page CPU read domain flag if we're
3181 * newly adding I915_GEM_DOMAIN_CPU
3182 */
Chris Wilson05394f32010-11-08 19:18:58 +00003183 if (obj->page_cpu_valid == NULL) {
3184 obj->page_cpu_valid = kzalloc(obj->base.size / PAGE_SIZE,
3185 GFP_KERNEL);
3186 if (obj->page_cpu_valid == NULL)
Eric Anholte47c68e2008-11-14 13:35:19 -08003187 return -ENOMEM;
Chris Wilson05394f32010-11-08 19:18:58 +00003188 } else if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0)
3189 memset(obj->page_cpu_valid, 0, obj->base.size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07003190
3191 /* Flush the cache on any pages that are still invalid from the CPU's
3192 * perspective.
3193 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003194 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
3195 i++) {
Chris Wilson05394f32010-11-08 19:18:58 +00003196 if (obj->page_cpu_valid[i])
Eric Anholt673a3942008-07-30 12:06:12 -07003197 continue;
3198
Chris Wilson05394f32010-11-08 19:18:58 +00003199 drm_clflush_pages(obj->pages + i, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07003200
Chris Wilson05394f32010-11-08 19:18:58 +00003201 obj->page_cpu_valid[i] = 1;
Eric Anholt673a3942008-07-30 12:06:12 -07003202 }
3203
Eric Anholte47c68e2008-11-14 13:35:19 -08003204 /* It should now be out of any other write domains, and we can update
3205 * the domain values for our changes.
3206 */
Chris Wilson05394f32010-11-08 19:18:58 +00003207 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
Eric Anholte47c68e2008-11-14 13:35:19 -08003208
Chris Wilson05394f32010-11-08 19:18:58 +00003209 old_read_domains = obj->base.read_domains;
3210 obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003211
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003212 trace_i915_gem_object_change_domain(obj,
3213 old_read_domains,
Chris Wilson05394f32010-11-08 19:18:58 +00003214 obj->base.write_domain);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003215
Eric Anholt673a3942008-07-30 12:06:12 -07003216 return 0;
3217}
3218
Eric Anholt673a3942008-07-30 12:06:12 -07003219/* Throttle our rendering by waiting until the ring has completed our requests
3220 * emitted over 20 msec ago.
3221 *
Eric Anholtb9624422009-06-03 07:27:35 +00003222 * Note that if we were to use the current jiffies each time around the loop,
3223 * we wouldn't escape the function with any frames outstanding if the time to
3224 * render a frame was over 20ms.
3225 *
Eric Anholt673a3942008-07-30 12:06:12 -07003226 * This should get us reasonable parallelism between CPU and GPU but also
3227 * relatively low latency when blocking on a particular request to finish.
3228 */
3229static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003230i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003231{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003232 struct drm_i915_private *dev_priv = dev->dev_private;
3233 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00003234 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003235 struct drm_i915_gem_request *request;
3236 struct intel_ring_buffer *ring = NULL;
3237 u32 seqno = 0;
3238 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003239
Chris Wilsone110e8d2011-01-26 15:39:14 +00003240 if (atomic_read(&dev_priv->mm.wedged))
3241 return -EIO;
3242
Chris Wilson1c255952010-09-26 11:03:27 +01003243 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003244 list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
Eric Anholtb9624422009-06-03 07:27:35 +00003245 if (time_after_eq(request->emitted_jiffies, recent_enough))
3246 break;
3247
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003248 ring = request->ring;
3249 seqno = request->seqno;
Eric Anholtb9624422009-06-03 07:27:35 +00003250 }
Chris Wilson1c255952010-09-26 11:03:27 +01003251 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003252
3253 if (seqno == 0)
3254 return 0;
3255
3256 ret = 0;
Chris Wilson78501ea2010-10-27 12:18:21 +01003257 if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) {
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003258 /* And wait for the seqno passing without holding any locks and
3259 * causing extra latency for others. This is safe as the irq
3260 * generation is designed to be run atomically and so is
3261 * lockless.
3262 */
Chris Wilsonb13c2b92010-12-13 16:54:50 +00003263 if (ring->irq_get(ring)) {
3264 ret = wait_event_interruptible(ring->irq_queue,
3265 i915_seqno_passed(ring->get_seqno(ring), seqno)
3266 || atomic_read(&dev_priv->mm.wedged));
3267 ring->irq_put(ring);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003268
Chris Wilsonb13c2b92010-12-13 16:54:50 +00003269 if (ret == 0 && atomic_read(&dev_priv->mm.wedged))
3270 ret = -EIO;
3271 }
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003272 }
3273
3274 if (ret == 0)
3275 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
Eric Anholtb9624422009-06-03 07:27:35 +00003276
Eric Anholt673a3942008-07-30 12:06:12 -07003277 return ret;
3278}
3279
Eric Anholt673a3942008-07-30 12:06:12 -07003280int
Chris Wilson05394f32010-11-08 19:18:58 +00003281i915_gem_object_pin(struct drm_i915_gem_object *obj,
3282 uint32_t alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01003283 bool map_and_fenceable)
Eric Anholt673a3942008-07-30 12:06:12 -07003284{
Chris Wilson05394f32010-11-08 19:18:58 +00003285 struct drm_device *dev = obj->base.dev;
Chris Wilsonf13d3f72010-09-20 17:36:15 +01003286 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003287 int ret;
3288
Chris Wilson05394f32010-11-08 19:18:58 +00003289 BUG_ON(obj->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT);
Chris Wilson23bc5982010-09-29 16:10:57 +01003290 WARN_ON(i915_verify_lists(dev));
Chris Wilsonac0c6b52010-05-27 13:18:18 +01003291
Chris Wilson05394f32010-11-08 19:18:58 +00003292 if (obj->gtt_space != NULL) {
3293 if ((alignment && obj->gtt_offset & (alignment - 1)) ||
3294 (map_and_fenceable && !obj->map_and_fenceable)) {
3295 WARN(obj->pin_count,
Chris Wilsonae7d49d2010-08-04 12:37:41 +01003296 "bo is already pinned with incorrect alignment:"
Daniel Vetter75e9e912010-11-04 17:11:09 +01003297 " offset=%x, req.alignment=%x, req.map_and_fenceable=%d,"
3298 " obj->map_and_fenceable=%d\n",
Chris Wilson05394f32010-11-08 19:18:58 +00003299 obj->gtt_offset, alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01003300 map_and_fenceable,
Chris Wilson05394f32010-11-08 19:18:58 +00003301 obj->map_and_fenceable);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01003302 ret = i915_gem_object_unbind(obj);
3303 if (ret)
3304 return ret;
3305 }
3306 }
3307
Chris Wilson05394f32010-11-08 19:18:58 +00003308 if (obj->gtt_space == NULL) {
Chris Wilsona00b10c2010-09-24 21:15:47 +01003309 ret = i915_gem_object_bind_to_gtt(obj, alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01003310 map_and_fenceable);
Chris Wilson97311292009-09-21 00:22:34 +01003311 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07003312 return ret;
Chris Wilson22c344e2009-02-11 14:26:45 +00003313 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05003314
Chris Wilson05394f32010-11-08 19:18:58 +00003315 if (obj->pin_count++ == 0) {
Chris Wilson05394f32010-11-08 19:18:58 +00003316 if (!obj->active)
3317 list_move_tail(&obj->mm_list,
Chris Wilsonf13d3f72010-09-20 17:36:15 +01003318 &dev_priv->mm.pinned_list);
Eric Anholt673a3942008-07-30 12:06:12 -07003319 }
Chris Wilson6299f992010-11-24 12:23:44 +00003320 obj->pin_mappable |= map_and_fenceable;
Eric Anholt673a3942008-07-30 12:06:12 -07003321
Chris Wilson23bc5982010-09-29 16:10:57 +01003322 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07003323 return 0;
3324}
3325
3326void
Chris Wilson05394f32010-11-08 19:18:58 +00003327i915_gem_object_unpin(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07003328{
Chris Wilson05394f32010-11-08 19:18:58 +00003329 struct drm_device *dev = obj->base.dev;
Eric Anholt673a3942008-07-30 12:06:12 -07003330 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003331
Chris Wilson23bc5982010-09-29 16:10:57 +01003332 WARN_ON(i915_verify_lists(dev));
Chris Wilson05394f32010-11-08 19:18:58 +00003333 BUG_ON(obj->pin_count == 0);
3334 BUG_ON(obj->gtt_space == NULL);
Eric Anholt673a3942008-07-30 12:06:12 -07003335
Chris Wilson05394f32010-11-08 19:18:58 +00003336 if (--obj->pin_count == 0) {
3337 if (!obj->active)
3338 list_move_tail(&obj->mm_list,
Eric Anholt673a3942008-07-30 12:06:12 -07003339 &dev_priv->mm.inactive_list);
Chris Wilson6299f992010-11-24 12:23:44 +00003340 obj->pin_mappable = false;
Eric Anholt673a3942008-07-30 12:06:12 -07003341 }
Chris Wilson23bc5982010-09-29 16:10:57 +01003342 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07003343}
3344
3345int
3346i915_gem_pin_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00003347 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003348{
3349 struct drm_i915_gem_pin *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003350 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07003351 int ret;
3352
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003353 ret = i915_mutex_lock_interruptible(dev);
3354 if (ret)
3355 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003356
Chris Wilson05394f32010-11-08 19:18:58 +00003357 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00003358 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003359 ret = -ENOENT;
3360 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07003361 }
Eric Anholt673a3942008-07-30 12:06:12 -07003362
Chris Wilson05394f32010-11-08 19:18:58 +00003363 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilsonbb6baf72009-09-22 14:24:13 +01003364 DRM_ERROR("Attempting to pin a purgeable buffer\n");
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003365 ret = -EINVAL;
3366 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003367 }
3368
Chris Wilson05394f32010-11-08 19:18:58 +00003369 if (obj->pin_filp != NULL && obj->pin_filp != file) {
Jesse Barnes79e53942008-11-07 14:24:08 -08003370 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
3371 args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003372 ret = -EINVAL;
3373 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08003374 }
3375
Chris Wilson05394f32010-11-08 19:18:58 +00003376 obj->user_pin_count++;
3377 obj->pin_filp = file;
3378 if (obj->user_pin_count == 1) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01003379 ret = i915_gem_object_pin(obj, args->alignment, true);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003380 if (ret)
3381 goto out;
Eric Anholt673a3942008-07-30 12:06:12 -07003382 }
3383
3384 /* XXX - flush the CPU caches for pinned objects
3385 * as the X server doesn't manage domains yet
3386 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003387 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00003388 args->offset = obj->gtt_offset;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003389out:
Chris Wilson05394f32010-11-08 19:18:58 +00003390 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003391unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07003392 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003393 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003394}
3395
3396int
3397i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00003398 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003399{
3400 struct drm_i915_gem_pin *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003401 struct drm_i915_gem_object *obj;
Chris Wilson76c1dec2010-09-25 11:22:51 +01003402 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003403
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003404 ret = i915_mutex_lock_interruptible(dev);
3405 if (ret)
3406 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003407
Chris Wilson05394f32010-11-08 19:18:58 +00003408 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00003409 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003410 ret = -ENOENT;
3411 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07003412 }
Chris Wilson76c1dec2010-09-25 11:22:51 +01003413
Chris Wilson05394f32010-11-08 19:18:58 +00003414 if (obj->pin_filp != file) {
Jesse Barnes79e53942008-11-07 14:24:08 -08003415 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
3416 args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003417 ret = -EINVAL;
3418 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08003419 }
Chris Wilson05394f32010-11-08 19:18:58 +00003420 obj->user_pin_count--;
3421 if (obj->user_pin_count == 0) {
3422 obj->pin_filp = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08003423 i915_gem_object_unpin(obj);
3424 }
Eric Anholt673a3942008-07-30 12:06:12 -07003425
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003426out:
Chris Wilson05394f32010-11-08 19:18:58 +00003427 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003428unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07003429 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003430 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003431}
3432
3433int
3434i915_gem_busy_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00003435 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003436{
3437 struct drm_i915_gem_busy *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003438 struct drm_i915_gem_object *obj;
Chris Wilson30dbf0c2010-09-25 10:19:17 +01003439 int ret;
3440
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003441 ret = i915_mutex_lock_interruptible(dev);
3442 if (ret)
3443 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003444
Chris Wilson05394f32010-11-08 19:18:58 +00003445 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00003446 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003447 ret = -ENOENT;
3448 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07003449 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08003450
Chris Wilson0be555b2010-08-04 15:36:30 +01003451 /* Count all active objects as busy, even if they are currently not used
3452 * by the gpu. Users of this interface expect objects to eventually
3453 * become non-busy without any further actions, therefore emit any
3454 * necessary flushes here.
Eric Anholtc4de0a52008-12-14 19:05:04 -08003455 */
Chris Wilson05394f32010-11-08 19:18:58 +00003456 args->busy = obj->active;
Chris Wilson0be555b2010-08-04 15:36:30 +01003457 if (args->busy) {
3458 /* Unconditionally flush objects, even when the gpu still uses this
3459 * object. Userspace calling this function indicates that it wants to
3460 * use this buffer rather sooner than later, so issuing the required
3461 * flush earlier is beneficial.
3462 */
Chris Wilson1a1c6972010-12-07 23:00:20 +00003463 if (obj->base.write_domain & I915_GEM_GPU_DOMAINS) {
Chris Wilsondb53a302011-02-03 11:57:46 +00003464 ret = i915_gem_flush_ring(obj->ring,
Chris Wilson88241782011-01-07 17:09:48 +00003465 0, obj->base.write_domain);
Chris Wilson1a1c6972010-12-07 23:00:20 +00003466 } else if (obj->ring->outstanding_lazy_request ==
3467 obj->last_rendering_seqno) {
3468 struct drm_i915_gem_request *request;
3469
Chris Wilson7a194872010-12-07 10:38:40 +00003470 /* This ring is not being cleared by active usage,
3471 * so emit a request to do so.
3472 */
Chris Wilson1a1c6972010-12-07 23:00:20 +00003473 request = kzalloc(sizeof(*request), GFP_KERNEL);
3474 if (request)
Chris Wilsondb53a302011-02-03 11:57:46 +00003475 ret = i915_add_request(obj->ring, NULL,request);
Chris Wilson1a1c6972010-12-07 23:00:20 +00003476 else
Chris Wilson7a194872010-12-07 10:38:40 +00003477 ret = -ENOMEM;
3478 }
Chris Wilson0be555b2010-08-04 15:36:30 +01003479
3480 /* Update the active list for the hardware's current position.
3481 * Otherwise this only updates on a delayed timer or when irqs
3482 * are actually unmasked, and our working set ends up being
3483 * larger than required.
3484 */
Chris Wilsondb53a302011-02-03 11:57:46 +00003485 i915_gem_retire_requests_ring(obj->ring);
Chris Wilson0be555b2010-08-04 15:36:30 +01003486
Chris Wilson05394f32010-11-08 19:18:58 +00003487 args->busy = obj->active;
Chris Wilson0be555b2010-08-04 15:36:30 +01003488 }
Eric Anholt673a3942008-07-30 12:06:12 -07003489
Chris Wilson05394f32010-11-08 19:18:58 +00003490 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003491unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07003492 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003493 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003494}
3495
3496int
3497i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
3498 struct drm_file *file_priv)
3499{
3500 return i915_gem_ring_throttle(dev, file_priv);
3501}
3502
Chris Wilson3ef94da2009-09-14 16:50:29 +01003503int
3504i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
3505 struct drm_file *file_priv)
3506{
3507 struct drm_i915_gem_madvise *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003508 struct drm_i915_gem_object *obj;
Chris Wilson76c1dec2010-09-25 11:22:51 +01003509 int ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003510
3511 switch (args->madv) {
3512 case I915_MADV_DONTNEED:
3513 case I915_MADV_WILLNEED:
3514 break;
3515 default:
3516 return -EINVAL;
3517 }
3518
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003519 ret = i915_mutex_lock_interruptible(dev);
3520 if (ret)
3521 return ret;
3522
Chris Wilson05394f32010-11-08 19:18:58 +00003523 obj = to_intel_bo(drm_gem_object_lookup(dev, file_priv, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00003524 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003525 ret = -ENOENT;
3526 goto unlock;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003527 }
Chris Wilson3ef94da2009-09-14 16:50:29 +01003528
Chris Wilson05394f32010-11-08 19:18:58 +00003529 if (obj->pin_count) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003530 ret = -EINVAL;
3531 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003532 }
3533
Chris Wilson05394f32010-11-08 19:18:58 +00003534 if (obj->madv != __I915_MADV_PURGED)
3535 obj->madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003536
Chris Wilson2d7ef392009-09-20 23:13:10 +01003537 /* if the object is no longer bound, discard its backing storage */
Chris Wilson05394f32010-11-08 19:18:58 +00003538 if (i915_gem_object_is_purgeable(obj) &&
3539 obj->gtt_space == NULL)
Chris Wilson2d7ef392009-09-20 23:13:10 +01003540 i915_gem_object_truncate(obj);
3541
Chris Wilson05394f32010-11-08 19:18:58 +00003542 args->retained = obj->madv != __I915_MADV_PURGED;
Chris Wilsonbb6baf72009-09-22 14:24:13 +01003543
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003544out:
Chris Wilson05394f32010-11-08 19:18:58 +00003545 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003546unlock:
Chris Wilson3ef94da2009-09-14 16:50:29 +01003547 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003548 return ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003549}
3550
Chris Wilson05394f32010-11-08 19:18:58 +00003551struct drm_i915_gem_object *i915_gem_alloc_object(struct drm_device *dev,
3552 size_t size)
Daniel Vetterac52bc52010-04-09 19:05:06 +00003553{
Chris Wilson73aa8082010-09-30 11:46:12 +01003554 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc397b902010-04-09 19:05:07 +00003555 struct drm_i915_gem_object *obj;
3556
3557 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
3558 if (obj == NULL)
3559 return NULL;
3560
3561 if (drm_gem_object_init(dev, &obj->base, size) != 0) {
3562 kfree(obj);
3563 return NULL;
3564 }
3565
Chris Wilson73aa8082010-09-30 11:46:12 +01003566 i915_gem_info_add_obj(dev_priv, size);
3567
Daniel Vetterc397b902010-04-09 19:05:07 +00003568 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
3569 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3570
3571 obj->agp_type = AGP_USER_MEMORY;
Daniel Vetter62b8b212010-04-09 19:05:08 +00003572 obj->base.driver_private = NULL;
Daniel Vetterc397b902010-04-09 19:05:07 +00003573 obj->fence_reg = I915_FENCE_REG_NONE;
Chris Wilson69dc4982010-10-19 10:36:51 +01003574 INIT_LIST_HEAD(&obj->mm_list);
Daniel Vetter93a37f22010-11-05 20:24:53 +01003575 INIT_LIST_HEAD(&obj->gtt_list);
Chris Wilson69dc4982010-10-19 10:36:51 +01003576 INIT_LIST_HEAD(&obj->ring_list);
Chris Wilson432e58e2010-11-25 19:32:06 +00003577 INIT_LIST_HEAD(&obj->exec_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00003578 INIT_LIST_HEAD(&obj->gpu_write_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00003579 obj->madv = I915_MADV_WILLNEED;
Daniel Vetter75e9e912010-11-04 17:11:09 +01003580 /* Avoid an unnecessary call to unbind on the first bind. */
3581 obj->map_and_fenceable = true;
Daniel Vetterc397b902010-04-09 19:05:07 +00003582
Chris Wilson05394f32010-11-08 19:18:58 +00003583 return obj;
Daniel Vetterac52bc52010-04-09 19:05:06 +00003584}
3585
Eric Anholt673a3942008-07-30 12:06:12 -07003586int i915_gem_init_object(struct drm_gem_object *obj)
3587{
Daniel Vetterc397b902010-04-09 19:05:07 +00003588 BUG();
Jesse Barnesde151cf2008-11-12 10:03:55 -08003589
Eric Anholt673a3942008-07-30 12:06:12 -07003590 return 0;
3591}
3592
Chris Wilson05394f32010-11-08 19:18:58 +00003593static void i915_gem_free_object_tail(struct drm_i915_gem_object *obj)
Chris Wilsonbe726152010-07-23 23:18:50 +01003594{
Chris Wilson05394f32010-11-08 19:18:58 +00003595 struct drm_device *dev = obj->base.dev;
Chris Wilsonbe726152010-07-23 23:18:50 +01003596 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonbe726152010-07-23 23:18:50 +01003597 int ret;
3598
3599 ret = i915_gem_object_unbind(obj);
3600 if (ret == -ERESTARTSYS) {
Chris Wilson05394f32010-11-08 19:18:58 +00003601 list_move(&obj->mm_list,
Chris Wilsonbe726152010-07-23 23:18:50 +01003602 &dev_priv->mm.deferred_free_list);
3603 return;
3604 }
3605
Chris Wilson26e12f82011-03-20 11:20:19 +00003606 trace_i915_gem_object_destroy(obj);
3607
Chris Wilson05394f32010-11-08 19:18:58 +00003608 if (obj->base.map_list.map)
Chris Wilsonbe726152010-07-23 23:18:50 +01003609 i915_gem_free_mmap_offset(obj);
3610
Chris Wilson05394f32010-11-08 19:18:58 +00003611 drm_gem_object_release(&obj->base);
3612 i915_gem_info_remove_obj(dev_priv, obj->base.size);
Chris Wilsonbe726152010-07-23 23:18:50 +01003613
Chris Wilson05394f32010-11-08 19:18:58 +00003614 kfree(obj->page_cpu_valid);
3615 kfree(obj->bit_17);
3616 kfree(obj);
Chris Wilsonbe726152010-07-23 23:18:50 +01003617}
3618
Chris Wilson05394f32010-11-08 19:18:58 +00003619void i915_gem_free_object(struct drm_gem_object *gem_obj)
Eric Anholt673a3942008-07-30 12:06:12 -07003620{
Chris Wilson05394f32010-11-08 19:18:58 +00003621 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
3622 struct drm_device *dev = obj->base.dev;
Eric Anholt673a3942008-07-30 12:06:12 -07003623
Chris Wilson05394f32010-11-08 19:18:58 +00003624 while (obj->pin_count > 0)
Eric Anholt673a3942008-07-30 12:06:12 -07003625 i915_gem_object_unpin(obj);
3626
Chris Wilson05394f32010-11-08 19:18:58 +00003627 if (obj->phys_obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10003628 i915_gem_detach_phys_object(dev, obj);
3629
Chris Wilsonbe726152010-07-23 23:18:50 +01003630 i915_gem_free_object_tail(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003631}
3632
Jesse Barnes5669fca2009-02-17 15:13:31 -08003633int
Eric Anholt673a3942008-07-30 12:06:12 -07003634i915_gem_idle(struct drm_device *dev)
3635{
3636 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson29105cc2010-01-07 10:39:13 +00003637 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003638
Keith Packard6dbe2772008-10-14 21:41:13 -07003639 mutex_lock(&dev->struct_mutex);
3640
Chris Wilson87acb0a2010-10-19 10:13:00 +01003641 if (dev_priv->mm.suspended) {
Keith Packard6dbe2772008-10-14 21:41:13 -07003642 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07003643 return 0;
Keith Packard6dbe2772008-10-14 21:41:13 -07003644 }
Eric Anholt673a3942008-07-30 12:06:12 -07003645
Chris Wilson29105cc2010-01-07 10:39:13 +00003646 ret = i915_gpu_idle(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07003647 if (ret) {
3648 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07003649 return ret;
Keith Packard6dbe2772008-10-14 21:41:13 -07003650 }
Eric Anholt673a3942008-07-30 12:06:12 -07003651
Chris Wilson29105cc2010-01-07 10:39:13 +00003652 /* Under UMS, be paranoid and evict. */
3653 if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
Chris Wilson5eac3ab2010-10-31 08:49:47 +00003654 ret = i915_gem_evict_inactive(dev, false);
Chris Wilson29105cc2010-01-07 10:39:13 +00003655 if (ret) {
3656 mutex_unlock(&dev->struct_mutex);
3657 return ret;
3658 }
3659 }
3660
Chris Wilson312817a2010-11-22 11:50:11 +00003661 i915_gem_reset_fences(dev);
3662
Chris Wilson29105cc2010-01-07 10:39:13 +00003663 /* Hack! Don't let anybody do execbuf while we don't control the chip.
3664 * We need to replace this with a semaphore, or something.
3665 * And not confound mm.suspended!
3666 */
3667 dev_priv->mm.suspended = 1;
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02003668 del_timer_sync(&dev_priv->hangcheck_timer);
Chris Wilson29105cc2010-01-07 10:39:13 +00003669
3670 i915_kernel_lost_context(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07003671 i915_gem_cleanup_ringbuffer(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00003672
Keith Packard6dbe2772008-10-14 21:41:13 -07003673 mutex_unlock(&dev->struct_mutex);
3674
Chris Wilson29105cc2010-01-07 10:39:13 +00003675 /* Cancel the retire work handler, which should be idle now. */
3676 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
3677
Eric Anholt673a3942008-07-30 12:06:12 -07003678 return 0;
3679}
3680
Eric Anholt673a3942008-07-30 12:06:12 -07003681int
Zou Nan hai8187a2b2010-05-21 09:08:55 +08003682i915_gem_init_ringbuffer(struct drm_device *dev)
3683{
3684 drm_i915_private_t *dev_priv = dev->dev_private;
3685 int ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01003686
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08003687 ret = intel_init_render_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01003688 if (ret)
Chris Wilsonb6913e42010-11-12 10:46:37 +00003689 return ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01003690
3691 if (HAS_BSD(dev)) {
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08003692 ret = intel_init_bsd_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01003693 if (ret)
3694 goto cleanup_render_ring;
Zou Nan haid1b851f2010-05-21 09:08:57 +08003695 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01003696
Chris Wilson549f7362010-10-19 11:19:32 +01003697 if (HAS_BLT(dev)) {
3698 ret = intel_init_blt_ring_buffer(dev);
3699 if (ret)
3700 goto cleanup_bsd_ring;
3701 }
3702
Chris Wilson6f392d52010-08-07 11:01:22 +01003703 dev_priv->next_seqno = 1;
3704
Chris Wilson68f95ba2010-05-27 13:18:22 +01003705 return 0;
3706
Chris Wilson549f7362010-10-19 11:19:32 +01003707cleanup_bsd_ring:
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003708 intel_cleanup_ring_buffer(&dev_priv->ring[VCS]);
Chris Wilson68f95ba2010-05-27 13:18:22 +01003709cleanup_render_ring:
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003710 intel_cleanup_ring_buffer(&dev_priv->ring[RCS]);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08003711 return ret;
3712}
3713
3714void
3715i915_gem_cleanup_ringbuffer(struct drm_device *dev)
3716{
3717 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003718 int i;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08003719
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003720 for (i = 0; i < I915_NUM_RINGS; i++)
3721 intel_cleanup_ring_buffer(&dev_priv->ring[i]);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08003722}
3723
3724int
Eric Anholt673a3942008-07-30 12:06:12 -07003725i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
3726 struct drm_file *file_priv)
3727{
3728 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003729 int ret, i;
Eric Anholt673a3942008-07-30 12:06:12 -07003730
Jesse Barnes79e53942008-11-07 14:24:08 -08003731 if (drm_core_check_feature(dev, DRIVER_MODESET))
3732 return 0;
3733
Ben Gamariba1234d2009-09-14 17:48:47 -04003734 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07003735 DRM_ERROR("Reenabling wedged hardware, good luck\n");
Ben Gamariba1234d2009-09-14 17:48:47 -04003736 atomic_set(&dev_priv->mm.wedged, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07003737 }
3738
Eric Anholt673a3942008-07-30 12:06:12 -07003739 mutex_lock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08003740 dev_priv->mm.suspended = 0;
3741
3742 ret = i915_gem_init_ringbuffer(dev);
Wu Fengguangd816f6ac2009-04-18 10:43:32 +08003743 if (ret != 0) {
3744 mutex_unlock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08003745 return ret;
Wu Fengguangd816f6ac2009-04-18 10:43:32 +08003746 }
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08003747
Chris Wilson69dc4982010-10-19 10:36:51 +01003748 BUG_ON(!list_empty(&dev_priv->mm.active_list));
Eric Anholt673a3942008-07-30 12:06:12 -07003749 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
3750 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003751 for (i = 0; i < I915_NUM_RINGS; i++) {
3752 BUG_ON(!list_empty(&dev_priv->ring[i].active_list));
3753 BUG_ON(!list_empty(&dev_priv->ring[i].request_list));
3754 }
Eric Anholt673a3942008-07-30 12:06:12 -07003755 mutex_unlock(&dev->struct_mutex);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04003756
Chris Wilson5f353082010-06-07 14:03:03 +01003757 ret = drm_irq_install(dev);
3758 if (ret)
3759 goto cleanup_ringbuffer;
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04003760
Eric Anholt673a3942008-07-30 12:06:12 -07003761 return 0;
Chris Wilson5f353082010-06-07 14:03:03 +01003762
3763cleanup_ringbuffer:
3764 mutex_lock(&dev->struct_mutex);
3765 i915_gem_cleanup_ringbuffer(dev);
3766 dev_priv->mm.suspended = 1;
3767 mutex_unlock(&dev->struct_mutex);
3768
3769 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003770}
3771
3772int
3773i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
3774 struct drm_file *file_priv)
3775{
Jesse Barnes79e53942008-11-07 14:24:08 -08003776 if (drm_core_check_feature(dev, DRIVER_MODESET))
3777 return 0;
3778
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04003779 drm_irq_uninstall(dev);
Linus Torvaldse6890f62009-09-08 17:09:24 -07003780 return i915_gem_idle(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07003781}
3782
3783void
3784i915_gem_lastclose(struct drm_device *dev)
3785{
3786 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003787
Eric Anholte806b492009-01-22 09:56:58 -08003788 if (drm_core_check_feature(dev, DRIVER_MODESET))
3789 return;
3790
Keith Packard6dbe2772008-10-14 21:41:13 -07003791 ret = i915_gem_idle(dev);
3792 if (ret)
3793 DRM_ERROR("failed to idle hardware: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07003794}
3795
Chris Wilson64193402010-10-24 12:38:05 +01003796static void
3797init_ring_lists(struct intel_ring_buffer *ring)
3798{
3799 INIT_LIST_HEAD(&ring->active_list);
3800 INIT_LIST_HEAD(&ring->request_list);
3801 INIT_LIST_HEAD(&ring->gpu_write_list);
3802}
3803
Eric Anholt673a3942008-07-30 12:06:12 -07003804void
3805i915_gem_load(struct drm_device *dev)
3806{
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02003807 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07003808 drm_i915_private_t *dev_priv = dev->dev_private;
3809
Chris Wilson69dc4982010-10-19 10:36:51 +01003810 INIT_LIST_HEAD(&dev_priv->mm.active_list);
Eric Anholt673a3942008-07-30 12:06:12 -07003811 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
3812 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
Chris Wilsonf13d3f72010-09-20 17:36:15 +01003813 INIT_LIST_HEAD(&dev_priv->mm.pinned_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07003814 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Chris Wilsonbe726152010-07-23 23:18:50 +01003815 INIT_LIST_HEAD(&dev_priv->mm.deferred_free_list);
Daniel Vetter93a37f22010-11-05 20:24:53 +01003816 INIT_LIST_HEAD(&dev_priv->mm.gtt_list);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003817 for (i = 0; i < I915_NUM_RINGS; i++)
3818 init_ring_lists(&dev_priv->ring[i]);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02003819 for (i = 0; i < 16; i++)
3820 INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
Eric Anholt673a3942008-07-30 12:06:12 -07003821 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
3822 i915_gem_retire_work_handler);
Chris Wilson30dbf0c2010-09-25 10:19:17 +01003823 init_completion(&dev_priv->error_completion);
Chris Wilson31169712009-09-14 16:50:28 +01003824
Dave Airlie94400122010-07-20 13:15:31 +10003825 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
3826 if (IS_GEN3(dev)) {
3827 u32 tmp = I915_READ(MI_ARB_STATE);
3828 if (!(tmp & MI_ARB_C3_LP_WRITE_ENABLE)) {
3829 /* arb state is a masked write, so set bit + bit in mask */
3830 tmp = MI_ARB_C3_LP_WRITE_ENABLE | (MI_ARB_C3_LP_WRITE_ENABLE << MI_ARB_MASK_SHIFT);
3831 I915_WRITE(MI_ARB_STATE, tmp);
3832 }
3833 }
3834
Chris Wilson72bfa192010-12-19 11:42:05 +00003835 dev_priv->relative_constants_mode = I915_EXEC_CONSTANTS_REL_GENERAL;
3836
Jesse Barnesde151cf2008-11-12 10:03:55 -08003837 /* Old X drivers will take 0-2 for front, back, depth buffers */
Eric Anholtb397c832010-01-26 09:43:10 -08003838 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3839 dev_priv->fence_reg_start = 3;
Jesse Barnesde151cf2008-11-12 10:03:55 -08003840
Chris Wilsona6c45cf2010-09-17 00:32:17 +01003841 if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08003842 dev_priv->num_fence_regs = 16;
3843 else
3844 dev_priv->num_fence_regs = 8;
3845
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02003846 /* Initialize fence registers to zero */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01003847 switch (INTEL_INFO(dev)->gen) {
3848 case 6:
3849 for (i = 0; i < 16; i++)
3850 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (i * 8), 0);
3851 break;
3852 case 5:
3853 case 4:
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02003854 for (i = 0; i < 16; i++)
3855 I915_WRITE64(FENCE_REG_965_0 + (i * 8), 0);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01003856 break;
3857 case 3:
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02003858 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
3859 for (i = 0; i < 8; i++)
3860 I915_WRITE(FENCE_REG_945_8 + (i * 4), 0);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01003861 case 2:
3862 for (i = 0; i < 8; i++)
3863 I915_WRITE(FENCE_REG_830_0 + (i * 4), 0);
3864 break;
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02003865 }
Eric Anholt673a3942008-07-30 12:06:12 -07003866 i915_gem_detect_bit_6_swizzle(dev);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003867 init_waitqueue_head(&dev_priv->pending_flip_queue);
Chris Wilson17250b72010-10-28 12:51:39 +01003868
Chris Wilsonce453d82011-02-21 14:43:56 +00003869 dev_priv->mm.interruptible = true;
3870
Chris Wilson17250b72010-10-28 12:51:39 +01003871 dev_priv->mm.inactive_shrinker.shrink = i915_gem_inactive_shrink;
3872 dev_priv->mm.inactive_shrinker.seeks = DEFAULT_SEEKS;
3873 register_shrinker(&dev_priv->mm.inactive_shrinker);
Eric Anholt673a3942008-07-30 12:06:12 -07003874}
Dave Airlie71acb5e2008-12-30 20:31:46 +10003875
3876/*
3877 * Create a physically contiguous memory object for this object
3878 * e.g. for cursor + overlay regs
3879 */
Chris Wilson995b67622010-08-20 13:23:26 +01003880static int i915_gem_init_phys_object(struct drm_device *dev,
3881 int id, int size, int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10003882{
3883 drm_i915_private_t *dev_priv = dev->dev_private;
3884 struct drm_i915_gem_phys_object *phys_obj;
3885 int ret;
3886
3887 if (dev_priv->mm.phys_objs[id - 1] || !size)
3888 return 0;
3889
Eric Anholt9a298b22009-03-24 12:23:04 -07003890 phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
Dave Airlie71acb5e2008-12-30 20:31:46 +10003891 if (!phys_obj)
3892 return -ENOMEM;
3893
3894 phys_obj->id = id;
3895
Chris Wilson6eeefaf2010-08-07 11:01:39 +01003896 phys_obj->handle = drm_pci_alloc(dev, size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10003897 if (!phys_obj->handle) {
3898 ret = -ENOMEM;
3899 goto kfree_obj;
3900 }
3901#ifdef CONFIG_X86
3902 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
3903#endif
3904
3905 dev_priv->mm.phys_objs[id - 1] = phys_obj;
3906
3907 return 0;
3908kfree_obj:
Eric Anholt9a298b22009-03-24 12:23:04 -07003909 kfree(phys_obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10003910 return ret;
3911}
3912
Chris Wilson995b67622010-08-20 13:23:26 +01003913static void i915_gem_free_phys_object(struct drm_device *dev, int id)
Dave Airlie71acb5e2008-12-30 20:31:46 +10003914{
3915 drm_i915_private_t *dev_priv = dev->dev_private;
3916 struct drm_i915_gem_phys_object *phys_obj;
3917
3918 if (!dev_priv->mm.phys_objs[id - 1])
3919 return;
3920
3921 phys_obj = dev_priv->mm.phys_objs[id - 1];
3922 if (phys_obj->cur_obj) {
3923 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
3924 }
3925
3926#ifdef CONFIG_X86
3927 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
3928#endif
3929 drm_pci_free(dev, phys_obj->handle);
3930 kfree(phys_obj);
3931 dev_priv->mm.phys_objs[id - 1] = NULL;
3932}
3933
3934void i915_gem_free_all_phys_object(struct drm_device *dev)
3935{
3936 int i;
3937
Dave Airlie260883c2009-01-22 17:58:49 +10003938 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
Dave Airlie71acb5e2008-12-30 20:31:46 +10003939 i915_gem_free_phys_object(dev, i);
3940}
3941
3942void i915_gem_detach_phys_object(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00003943 struct drm_i915_gem_object *obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10003944{
Chris Wilson05394f32010-11-08 19:18:58 +00003945 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Chris Wilsone5281cc2010-10-28 13:45:36 +01003946 char *vaddr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10003947 int i;
Dave Airlie71acb5e2008-12-30 20:31:46 +10003948 int page_count;
3949
Chris Wilson05394f32010-11-08 19:18:58 +00003950 if (!obj->phys_obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10003951 return;
Chris Wilson05394f32010-11-08 19:18:58 +00003952 vaddr = obj->phys_obj->handle->vaddr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10003953
Chris Wilson05394f32010-11-08 19:18:58 +00003954 page_count = obj->base.size / PAGE_SIZE;
Dave Airlie71acb5e2008-12-30 20:31:46 +10003955 for (i = 0; i < page_count; i++) {
Chris Wilsone5281cc2010-10-28 13:45:36 +01003956 struct page *page = read_cache_page_gfp(mapping, i,
3957 GFP_HIGHUSER | __GFP_RECLAIMABLE);
3958 if (!IS_ERR(page)) {
3959 char *dst = kmap_atomic(page);
3960 memcpy(dst, vaddr + i*PAGE_SIZE, PAGE_SIZE);
3961 kunmap_atomic(dst);
Dave Airlie71acb5e2008-12-30 20:31:46 +10003962
Chris Wilsone5281cc2010-10-28 13:45:36 +01003963 drm_clflush_pages(&page, 1);
3964
3965 set_page_dirty(page);
3966 mark_page_accessed(page);
3967 page_cache_release(page);
3968 }
Dave Airlie71acb5e2008-12-30 20:31:46 +10003969 }
Daniel Vetter40ce6572010-11-05 18:12:18 +01003970 intel_gtt_chipset_flush();
Chris Wilsond78b47b2009-06-17 21:52:49 +01003971
Chris Wilson05394f32010-11-08 19:18:58 +00003972 obj->phys_obj->cur_obj = NULL;
3973 obj->phys_obj = NULL;
Dave Airlie71acb5e2008-12-30 20:31:46 +10003974}
3975
3976int
3977i915_gem_attach_phys_object(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00003978 struct drm_i915_gem_object *obj,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01003979 int id,
3980 int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10003981{
Chris Wilson05394f32010-11-08 19:18:58 +00003982 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Dave Airlie71acb5e2008-12-30 20:31:46 +10003983 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie71acb5e2008-12-30 20:31:46 +10003984 int ret = 0;
3985 int page_count;
3986 int i;
3987
3988 if (id > I915_MAX_PHYS_OBJECT)
3989 return -EINVAL;
3990
Chris Wilson05394f32010-11-08 19:18:58 +00003991 if (obj->phys_obj) {
3992 if (obj->phys_obj->id == id)
Dave Airlie71acb5e2008-12-30 20:31:46 +10003993 return 0;
3994 i915_gem_detach_phys_object(dev, obj);
3995 }
3996
Dave Airlie71acb5e2008-12-30 20:31:46 +10003997 /* create a new object */
3998 if (!dev_priv->mm.phys_objs[id - 1]) {
3999 ret = i915_gem_init_phys_object(dev, id,
Chris Wilson05394f32010-11-08 19:18:58 +00004000 obj->base.size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004001 if (ret) {
Chris Wilson05394f32010-11-08 19:18:58 +00004002 DRM_ERROR("failed to init phys object %d size: %zu\n",
4003 id, obj->base.size);
Chris Wilsone5281cc2010-10-28 13:45:36 +01004004 return ret;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004005 }
4006 }
4007
4008 /* bind to the object */
Chris Wilson05394f32010-11-08 19:18:58 +00004009 obj->phys_obj = dev_priv->mm.phys_objs[id - 1];
4010 obj->phys_obj->cur_obj = obj;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004011
Chris Wilson05394f32010-11-08 19:18:58 +00004012 page_count = obj->base.size / PAGE_SIZE;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004013
4014 for (i = 0; i < page_count; i++) {
Chris Wilsone5281cc2010-10-28 13:45:36 +01004015 struct page *page;
4016 char *dst, *src;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004017
Chris Wilsone5281cc2010-10-28 13:45:36 +01004018 page = read_cache_page_gfp(mapping, i,
4019 GFP_HIGHUSER | __GFP_RECLAIMABLE);
4020 if (IS_ERR(page))
4021 return PTR_ERR(page);
4022
Chris Wilsonff75b9b2010-10-30 22:52:31 +01004023 src = kmap_atomic(page);
Chris Wilson05394f32010-11-08 19:18:58 +00004024 dst = obj->phys_obj->handle->vaddr + (i * PAGE_SIZE);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004025 memcpy(dst, src, PAGE_SIZE);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -07004026 kunmap_atomic(src);
Chris Wilsone5281cc2010-10-28 13:45:36 +01004027
4028 mark_page_accessed(page);
4029 page_cache_release(page);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004030 }
4031
4032 return 0;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004033}
4034
4035static int
Chris Wilson05394f32010-11-08 19:18:58 +00004036i915_gem_phys_pwrite(struct drm_device *dev,
4037 struct drm_i915_gem_object *obj,
Dave Airlie71acb5e2008-12-30 20:31:46 +10004038 struct drm_i915_gem_pwrite *args,
4039 struct drm_file *file_priv)
4040{
Chris Wilson05394f32010-11-08 19:18:58 +00004041 void *vaddr = obj->phys_obj->handle->vaddr + args->offset;
Chris Wilsonb47b30c2010-11-08 01:12:29 +00004042 char __user *user_data = (char __user *) (uintptr_t) args->data_ptr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004043
Chris Wilsonb47b30c2010-11-08 01:12:29 +00004044 if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) {
4045 unsigned long unwritten;
4046
4047 /* The physical object once assigned is fixed for the lifetime
4048 * of the obj, so we can safely drop the lock and continue
4049 * to access vaddr.
4050 */
4051 mutex_unlock(&dev->struct_mutex);
4052 unwritten = copy_from_user(vaddr, user_data, args->size);
4053 mutex_lock(&dev->struct_mutex);
4054 if (unwritten)
4055 return -EFAULT;
4056 }
Dave Airlie71acb5e2008-12-30 20:31:46 +10004057
Daniel Vetter40ce6572010-11-05 18:12:18 +01004058 intel_gtt_chipset_flush();
Dave Airlie71acb5e2008-12-30 20:31:46 +10004059 return 0;
4060}
Eric Anholtb9624422009-06-03 07:27:35 +00004061
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004062void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00004063{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004064 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00004065
4066 /* Clean up our request list when the client is going away, so that
4067 * later retire_requests won't dereference our soon-to-be-gone
4068 * file_priv.
4069 */
Chris Wilson1c255952010-09-26 11:03:27 +01004070 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004071 while (!list_empty(&file_priv->mm.request_list)) {
4072 struct drm_i915_gem_request *request;
4073
4074 request = list_first_entry(&file_priv->mm.request_list,
4075 struct drm_i915_gem_request,
4076 client_list);
4077 list_del(&request->client_list);
4078 request->file_priv = NULL;
4079 }
Chris Wilson1c255952010-09-26 11:03:27 +01004080 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00004081}
Chris Wilson31169712009-09-14 16:50:28 +01004082
Chris Wilson31169712009-09-14 16:50:28 +01004083static int
Chris Wilson1637ef42010-04-20 17:10:35 +01004084i915_gpu_is_active(struct drm_device *dev)
4085{
4086 drm_i915_private_t *dev_priv = dev->dev_private;
4087 int lists_empty;
4088
Chris Wilson1637ef42010-04-20 17:10:35 +01004089 lists_empty = list_empty(&dev_priv->mm.flushing_list) &&
Chris Wilson17250b72010-10-28 12:51:39 +01004090 list_empty(&dev_priv->mm.active_list);
Chris Wilson1637ef42010-04-20 17:10:35 +01004091
4092 return !lists_empty;
4093}
4094
4095static int
Chris Wilson17250b72010-10-28 12:51:39 +01004096i915_gem_inactive_shrink(struct shrinker *shrinker,
4097 int nr_to_scan,
4098 gfp_t gfp_mask)
Chris Wilson31169712009-09-14 16:50:28 +01004099{
Chris Wilson17250b72010-10-28 12:51:39 +01004100 struct drm_i915_private *dev_priv =
4101 container_of(shrinker,
4102 struct drm_i915_private,
4103 mm.inactive_shrinker);
4104 struct drm_device *dev = dev_priv->dev;
4105 struct drm_i915_gem_object *obj, *next;
4106 int cnt;
4107
4108 if (!mutex_trylock(&dev->struct_mutex))
Chris Wilsonbbe2e112010-10-28 22:35:07 +01004109 return 0;
Chris Wilson31169712009-09-14 16:50:28 +01004110
4111 /* "fast-path" to count number of available objects */
4112 if (nr_to_scan == 0) {
Chris Wilson17250b72010-10-28 12:51:39 +01004113 cnt = 0;
4114 list_for_each_entry(obj,
4115 &dev_priv->mm.inactive_list,
4116 mm_list)
4117 cnt++;
4118 mutex_unlock(&dev->struct_mutex);
4119 return cnt / 100 * sysctl_vfs_cache_pressure;
Chris Wilson31169712009-09-14 16:50:28 +01004120 }
4121
Chris Wilson1637ef42010-04-20 17:10:35 +01004122rescan:
Chris Wilson31169712009-09-14 16:50:28 +01004123 /* first scan for clean buffers */
Chris Wilson17250b72010-10-28 12:51:39 +01004124 i915_gem_retire_requests(dev);
Chris Wilson31169712009-09-14 16:50:28 +01004125
Chris Wilson17250b72010-10-28 12:51:39 +01004126 list_for_each_entry_safe(obj, next,
4127 &dev_priv->mm.inactive_list,
4128 mm_list) {
4129 if (i915_gem_object_is_purgeable(obj)) {
Chris Wilson20217462010-11-23 15:26:33 +00004130 if (i915_gem_object_unbind(obj) == 0 &&
4131 --nr_to_scan == 0)
Chris Wilson17250b72010-10-28 12:51:39 +01004132 break;
Chris Wilson31169712009-09-14 16:50:28 +01004133 }
Chris Wilson31169712009-09-14 16:50:28 +01004134 }
4135
4136 /* second pass, evict/count anything still on the inactive list */
Chris Wilson17250b72010-10-28 12:51:39 +01004137 cnt = 0;
4138 list_for_each_entry_safe(obj, next,
4139 &dev_priv->mm.inactive_list,
4140 mm_list) {
Chris Wilson20217462010-11-23 15:26:33 +00004141 if (nr_to_scan &&
4142 i915_gem_object_unbind(obj) == 0)
Chris Wilson17250b72010-10-28 12:51:39 +01004143 nr_to_scan--;
Chris Wilson20217462010-11-23 15:26:33 +00004144 else
Chris Wilson17250b72010-10-28 12:51:39 +01004145 cnt++;
Chris Wilson31169712009-09-14 16:50:28 +01004146 }
4147
Chris Wilson17250b72010-10-28 12:51:39 +01004148 if (nr_to_scan && i915_gpu_is_active(dev)) {
Chris Wilson1637ef42010-04-20 17:10:35 +01004149 /*
4150 * We are desperate for pages, so as a last resort, wait
4151 * for the GPU to finish and discard whatever we can.
4152 * This has a dramatic impact to reduce the number of
4153 * OOM-killer events whilst running the GPU aggressively.
4154 */
Chris Wilson17250b72010-10-28 12:51:39 +01004155 if (i915_gpu_idle(dev) == 0)
Chris Wilson1637ef42010-04-20 17:10:35 +01004156 goto rescan;
4157 }
Chris Wilson17250b72010-10-28 12:51:39 +01004158 mutex_unlock(&dev->struct_mutex);
4159 return cnt / 100 * sysctl_vfs_cache_pressure;
Chris Wilson31169712009-09-14 16:50:28 +01004160}