blob: b663d2da1db38f1380661a51422fe8973c1246f0 [file] [log] [blame]
Eric Anholt673a3942008-07-30 12:06:12 -07001/*
2 * Copyright © 2008 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 *
26 */
27
28#include "drmP.h"
29#include "drm.h"
30#include "i915_drm.h"
31#include "i915_drv.h"
Chris Wilson1c5d22f2009-08-25 11:15:50 +010032#include "i915_trace.h"
Jesse Barnes652c3932009-08-17 13:31:43 -070033#include "intel_drv.h"
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Eric Anholt673a3942008-07-30 12:06:12 -070035#include <linux/swap.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080036#include <linux/pci.h>
Eric Anholt673a3942008-07-30 12:06:12 -070037
Chris Wilson0f8c6d72010-11-01 12:38:44 +000038struct change_domains {
39 uint32_t invalidate_domains;
40 uint32_t flush_domains;
41 uint32_t flush_rings;
42};
43
Chris Wilsona00b10c2010-09-24 21:15:47 +010044static uint32_t i915_gem_get_gtt_alignment(struct drm_i915_gem_object *obj_priv);
45static uint32_t i915_gem_get_gtt_size(struct drm_i915_gem_object *obj_priv);
Daniel Vetterba3d8d72010-02-11 22:37:04 +010046
47static int i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj,
48 bool pipelined);
Eric Anholte47c68e2008-11-14 13:35:19 -080049static void i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj);
50static void i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj);
Eric Anholte47c68e2008-11-14 13:35:19 -080051static int i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj,
52 int write);
53static int i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
54 uint64_t offset,
55 uint64_t size);
56static void i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj);
Chris Wilson2cf34d72010-09-14 13:03:28 +010057static int i915_gem_object_wait_rendering(struct drm_gem_object *obj,
58 bool interruptible);
Jesse Barnesde151cf2008-11-12 10:03:55 -080059static int i915_gem_object_bind_to_gtt(struct drm_gem_object *obj,
Chris Wilsona00b10c2010-09-24 21:15:47 +010060 unsigned alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +010061 bool map_and_fenceable);
Jesse Barnesde151cf2008-11-12 10:03:55 -080062static void i915_gem_clear_fence_reg(struct drm_gem_object *obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +100063static int i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
64 struct drm_i915_gem_pwrite *args,
65 struct drm_file *file_priv);
Chris Wilsonbe726152010-07-23 23:18:50 +010066static void i915_gem_free_object_tail(struct drm_gem_object *obj);
Eric Anholt673a3942008-07-30 12:06:12 -070067
Chris Wilson17250b72010-10-28 12:51:39 +010068static int i915_gem_inactive_shrink(struct shrinker *shrinker,
69 int nr_to_scan,
70 gfp_t gfp_mask);
71
Chris Wilson31169712009-09-14 16:50:28 +010072
Chris Wilson73aa8082010-09-30 11:46:12 +010073/* some bookkeeping */
74static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
75 size_t size)
76{
77 dev_priv->mm.object_count++;
78 dev_priv->mm.object_memory += size;
79}
80
81static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
82 size_t size)
83{
84 dev_priv->mm.object_count--;
85 dev_priv->mm.object_memory -= size;
86}
87
88static void i915_gem_info_add_gtt(struct drm_i915_private *dev_priv,
Chris Wilsona00b10c2010-09-24 21:15:47 +010089 struct drm_i915_gem_object *obj)
Chris Wilson73aa8082010-09-30 11:46:12 +010090{
91 dev_priv->mm.gtt_count++;
Chris Wilsona00b10c2010-09-24 21:15:47 +010092 dev_priv->mm.gtt_memory += obj->gtt_space->size;
93 if (obj->gtt_offset < dev_priv->mm.gtt_mappable_end) {
Daniel Vetterfb7d5162010-10-01 22:05:20 +020094 dev_priv->mm.mappable_gtt_used +=
Chris Wilsona00b10c2010-09-24 21:15:47 +010095 min_t(size_t, obj->gtt_space->size,
96 dev_priv->mm.gtt_mappable_end - obj->gtt_offset);
Daniel Vetterfb7d5162010-10-01 22:05:20 +020097 }
Chris Wilson73aa8082010-09-30 11:46:12 +010098}
99
100static void i915_gem_info_remove_gtt(struct drm_i915_private *dev_priv,
Chris Wilsona00b10c2010-09-24 21:15:47 +0100101 struct drm_i915_gem_object *obj)
Chris Wilson73aa8082010-09-30 11:46:12 +0100102{
103 dev_priv->mm.gtt_count--;
Chris Wilsona00b10c2010-09-24 21:15:47 +0100104 dev_priv->mm.gtt_memory -= obj->gtt_space->size;
105 if (obj->gtt_offset < dev_priv->mm.gtt_mappable_end) {
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200106 dev_priv->mm.mappable_gtt_used -=
Chris Wilsona00b10c2010-09-24 21:15:47 +0100107 min_t(size_t, obj->gtt_space->size,
108 dev_priv->mm.gtt_mappable_end - obj->gtt_offset);
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200109 }
110}
111
112/**
113 * Update the mappable working set counters. Call _only_ when there is a change
114 * in one of (pin|fault)_mappable and update *_mappable _before_ calling.
115 * @mappable: new state the changed mappable flag (either pin_ or fault_).
116 */
117static void
118i915_gem_info_update_mappable(struct drm_i915_private *dev_priv,
Chris Wilsona00b10c2010-09-24 21:15:47 +0100119 struct drm_i915_gem_object *obj,
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200120 bool mappable)
121{
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200122 if (mappable) {
Chris Wilsona00b10c2010-09-24 21:15:47 +0100123 if (obj->pin_mappable && obj->fault_mappable)
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200124 /* Combined state was already mappable. */
125 return;
126 dev_priv->mm.gtt_mappable_count++;
Chris Wilsona00b10c2010-09-24 21:15:47 +0100127 dev_priv->mm.gtt_mappable_memory += obj->gtt_space->size;
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200128 } else {
Chris Wilsona00b10c2010-09-24 21:15:47 +0100129 if (obj->pin_mappable || obj->fault_mappable)
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200130 /* Combined state still mappable. */
131 return;
132 dev_priv->mm.gtt_mappable_count--;
Chris Wilsona00b10c2010-09-24 21:15:47 +0100133 dev_priv->mm.gtt_mappable_memory -= obj->gtt_space->size;
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200134 }
Chris Wilson73aa8082010-09-30 11:46:12 +0100135}
136
137static void i915_gem_info_add_pin(struct drm_i915_private *dev_priv,
Chris Wilsona00b10c2010-09-24 21:15:47 +0100138 struct drm_i915_gem_object *obj,
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200139 bool mappable)
Chris Wilson73aa8082010-09-30 11:46:12 +0100140{
141 dev_priv->mm.pin_count++;
Chris Wilsona00b10c2010-09-24 21:15:47 +0100142 dev_priv->mm.pin_memory += obj->gtt_space->size;
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200143 if (mappable) {
Chris Wilsona00b10c2010-09-24 21:15:47 +0100144 obj->pin_mappable = true;
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200145 i915_gem_info_update_mappable(dev_priv, obj, true);
146 }
Chris Wilson73aa8082010-09-30 11:46:12 +0100147}
148
149static void i915_gem_info_remove_pin(struct drm_i915_private *dev_priv,
Chris Wilsona00b10c2010-09-24 21:15:47 +0100150 struct drm_i915_gem_object *obj)
Chris Wilson73aa8082010-09-30 11:46:12 +0100151{
152 dev_priv->mm.pin_count--;
Chris Wilsona00b10c2010-09-24 21:15:47 +0100153 dev_priv->mm.pin_memory -= obj->gtt_space->size;
154 if (obj->pin_mappable) {
155 obj->pin_mappable = false;
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200156 i915_gem_info_update_mappable(dev_priv, obj, false);
157 }
Chris Wilson73aa8082010-09-30 11:46:12 +0100158}
159
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100160int
161i915_gem_check_is_wedged(struct drm_device *dev)
162{
163 struct drm_i915_private *dev_priv = dev->dev_private;
164 struct completion *x = &dev_priv->error_completion;
165 unsigned long flags;
166 int ret;
167
168 if (!atomic_read(&dev_priv->mm.wedged))
169 return 0;
170
171 ret = wait_for_completion_interruptible(x);
172 if (ret)
173 return ret;
174
175 /* Success, we reset the GPU! */
176 if (!atomic_read(&dev_priv->mm.wedged))
177 return 0;
178
179 /* GPU is hung, bump the completion count to account for
180 * the token we just consumed so that we never hit zero and
181 * end up waiting upon a subsequent completion event that
182 * will never happen.
183 */
184 spin_lock_irqsave(&x->wait.lock, flags);
185 x->done++;
186 spin_unlock_irqrestore(&x->wait.lock, flags);
187 return -EIO;
188}
189
Chris Wilson76c1dec2010-09-25 11:22:51 +0100190static int i915_mutex_lock_interruptible(struct drm_device *dev)
191{
192 struct drm_i915_private *dev_priv = dev->dev_private;
193 int ret;
194
195 ret = i915_gem_check_is_wedged(dev);
196 if (ret)
197 return ret;
198
199 ret = mutex_lock_interruptible(&dev->struct_mutex);
200 if (ret)
201 return ret;
202
203 if (atomic_read(&dev_priv->mm.wedged)) {
204 mutex_unlock(&dev->struct_mutex);
205 return -EAGAIN;
206 }
207
Chris Wilson23bc5982010-09-29 16:10:57 +0100208 WARN_ON(i915_verify_lists(dev));
Chris Wilson76c1dec2010-09-25 11:22:51 +0100209 return 0;
210}
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100211
Chris Wilson7d1c4802010-08-07 21:45:03 +0100212static inline bool
213i915_gem_object_is_inactive(struct drm_i915_gem_object *obj_priv)
214{
215 return obj_priv->gtt_space &&
216 !obj_priv->active &&
217 obj_priv->pin_count == 0;
218}
219
Chris Wilson73aa8082010-09-30 11:46:12 +0100220int i915_gem_do_init(struct drm_device *dev,
221 unsigned long start,
Daniel Vetter53984632010-09-22 23:44:24 +0200222 unsigned long mappable_end,
Jesse Barnes79e53942008-11-07 14:24:08 -0800223 unsigned long end)
224{
225 drm_i915_private_t *dev_priv = dev->dev_private;
226
227 if (start >= end ||
228 (start & (PAGE_SIZE - 1)) != 0 ||
229 (end & (PAGE_SIZE - 1)) != 0) {
230 return -EINVAL;
231 }
232
233 drm_mm_init(&dev_priv->mm.gtt_space, start,
234 end - start);
235
Chris Wilson73aa8082010-09-30 11:46:12 +0100236 dev_priv->mm.gtt_total = end - start;
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200237 dev_priv->mm.mappable_gtt_total = min(end, mappable_end) - start;
Daniel Vetter53984632010-09-22 23:44:24 +0200238 dev_priv->mm.gtt_mappable_end = mappable_end;
Jesse Barnes79e53942008-11-07 14:24:08 -0800239
240 return 0;
241}
Keith Packard6dbe2772008-10-14 21:41:13 -0700242
Eric Anholt673a3942008-07-30 12:06:12 -0700243int
244i915_gem_init_ioctl(struct drm_device *dev, void *data,
245 struct drm_file *file_priv)
246{
Eric Anholt673a3942008-07-30 12:06:12 -0700247 struct drm_i915_gem_init *args = data;
Jesse Barnes79e53942008-11-07 14:24:08 -0800248 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700249
250 mutex_lock(&dev->struct_mutex);
Daniel Vetter53984632010-09-22 23:44:24 +0200251 ret = i915_gem_do_init(dev, args->gtt_start, args->gtt_end, args->gtt_end);
Eric Anholt673a3942008-07-30 12:06:12 -0700252 mutex_unlock(&dev->struct_mutex);
253
Jesse Barnes79e53942008-11-07 14:24:08 -0800254 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700255}
256
Eric Anholt5a125c32008-10-22 21:40:13 -0700257int
258i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
259 struct drm_file *file_priv)
260{
Chris Wilson73aa8082010-09-30 11:46:12 +0100261 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt5a125c32008-10-22 21:40:13 -0700262 struct drm_i915_gem_get_aperture *args = data;
Eric Anholt5a125c32008-10-22 21:40:13 -0700263
264 if (!(dev->driver->driver_features & DRIVER_GEM))
265 return -ENODEV;
266
Chris Wilson73aa8082010-09-30 11:46:12 +0100267 mutex_lock(&dev->struct_mutex);
268 args->aper_size = dev_priv->mm.gtt_total;
269 args->aper_available_size = args->aper_size - dev_priv->mm.pin_memory;
270 mutex_unlock(&dev->struct_mutex);
Eric Anholt5a125c32008-10-22 21:40:13 -0700271
272 return 0;
273}
274
Eric Anholt673a3942008-07-30 12:06:12 -0700275
276/**
277 * Creates a new mm object and returns a handle to it.
278 */
279int
280i915_gem_create_ioctl(struct drm_device *dev, void *data,
281 struct drm_file *file_priv)
282{
283 struct drm_i915_gem_create *args = data;
284 struct drm_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300285 int ret;
286 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700287
288 args->size = roundup(args->size, PAGE_SIZE);
289
290 /* Allocate the new object */
Daniel Vetterac52bc52010-04-09 19:05:06 +0000291 obj = i915_gem_alloc_object(dev, args->size);
Eric Anholt673a3942008-07-30 12:06:12 -0700292 if (obj == NULL)
293 return -ENOMEM;
294
295 ret = drm_gem_handle_create(file_priv, obj, &handle);
Chris Wilson1dfd9752010-09-06 14:44:14 +0100296 if (ret) {
Chris Wilson202f2fe2010-10-14 13:20:40 +0100297 drm_gem_object_release(obj);
298 i915_gem_info_remove_obj(dev->dev_private, obj->size);
299 kfree(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700300 return ret;
Chris Wilson1dfd9752010-09-06 14:44:14 +0100301 }
302
Chris Wilson202f2fe2010-10-14 13:20:40 +0100303 /* drop reference from allocate - handle holds it now */
304 drm_gem_object_unreference(obj);
305 trace_i915_gem_object_create(obj);
306
Eric Anholt673a3942008-07-30 12:06:12 -0700307 args->handle = handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700308 return 0;
309}
310
Eric Anholt280b7132009-03-12 16:56:27 -0700311static int i915_gem_object_needs_bit17_swizzle(struct drm_gem_object *obj)
312{
313 drm_i915_private_t *dev_priv = obj->dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +0100314 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt280b7132009-03-12 16:56:27 -0700315
316 return dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_9_10_17 &&
317 obj_priv->tiling_mode != I915_TILING_NONE;
318}
319
Chris Wilson99a03df2010-05-27 14:15:34 +0100320static inline void
Eric Anholt40123c12009-03-09 13:42:30 -0700321slow_shmem_copy(struct page *dst_page,
322 int dst_offset,
323 struct page *src_page,
324 int src_offset,
325 int length)
326{
327 char *dst_vaddr, *src_vaddr;
328
Chris Wilson99a03df2010-05-27 14:15:34 +0100329 dst_vaddr = kmap(dst_page);
330 src_vaddr = kmap(src_page);
Eric Anholt40123c12009-03-09 13:42:30 -0700331
332 memcpy(dst_vaddr + dst_offset, src_vaddr + src_offset, length);
333
Chris Wilson99a03df2010-05-27 14:15:34 +0100334 kunmap(src_page);
335 kunmap(dst_page);
Eric Anholt40123c12009-03-09 13:42:30 -0700336}
337
Chris Wilson99a03df2010-05-27 14:15:34 +0100338static inline void
Eric Anholt280b7132009-03-12 16:56:27 -0700339slow_shmem_bit17_copy(struct page *gpu_page,
340 int gpu_offset,
341 struct page *cpu_page,
342 int cpu_offset,
343 int length,
344 int is_read)
345{
346 char *gpu_vaddr, *cpu_vaddr;
347
348 /* Use the unswizzled path if this page isn't affected. */
349 if ((page_to_phys(gpu_page) & (1 << 17)) == 0) {
350 if (is_read)
351 return slow_shmem_copy(cpu_page, cpu_offset,
352 gpu_page, gpu_offset, length);
353 else
354 return slow_shmem_copy(gpu_page, gpu_offset,
355 cpu_page, cpu_offset, length);
356 }
357
Chris Wilson99a03df2010-05-27 14:15:34 +0100358 gpu_vaddr = kmap(gpu_page);
359 cpu_vaddr = kmap(cpu_page);
Eric Anholt280b7132009-03-12 16:56:27 -0700360
361 /* Copy the data, XORing A6 with A17 (1). The user already knows he's
362 * XORing with the other bits (A9 for Y, A9 and A10 for X)
363 */
364 while (length > 0) {
365 int cacheline_end = ALIGN(gpu_offset + 1, 64);
366 int this_length = min(cacheline_end - gpu_offset, length);
367 int swizzled_gpu_offset = gpu_offset ^ 64;
368
369 if (is_read) {
370 memcpy(cpu_vaddr + cpu_offset,
371 gpu_vaddr + swizzled_gpu_offset,
372 this_length);
373 } else {
374 memcpy(gpu_vaddr + swizzled_gpu_offset,
375 cpu_vaddr + cpu_offset,
376 this_length);
377 }
378 cpu_offset += this_length;
379 gpu_offset += this_length;
380 length -= this_length;
381 }
382
Chris Wilson99a03df2010-05-27 14:15:34 +0100383 kunmap(cpu_page);
384 kunmap(gpu_page);
Eric Anholt280b7132009-03-12 16:56:27 -0700385}
386
Eric Anholt673a3942008-07-30 12:06:12 -0700387/**
Eric Anholteb014592009-03-10 11:44:52 -0700388 * This is the fast shmem pread path, which attempts to copy_from_user directly
389 * from the backing pages of the object to the user's address space. On a
390 * fault, it fails so we can fall back to i915_gem_shmem_pwrite_slow().
391 */
392static int
393i915_gem_shmem_pread_fast(struct drm_device *dev, struct drm_gem_object *obj,
394 struct drm_i915_gem_pread *args,
395 struct drm_file *file_priv)
396{
Daniel Vetter23010e42010-03-08 13:35:02 +0100397 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsone5281cc2010-10-28 13:45:36 +0100398 struct address_space *mapping = obj->filp->f_path.dentry->d_inode->i_mapping;
Eric Anholteb014592009-03-10 11:44:52 -0700399 ssize_t remain;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100400 loff_t offset;
Eric Anholteb014592009-03-10 11:44:52 -0700401 char __user *user_data;
402 int page_offset, page_length;
Eric Anholteb014592009-03-10 11:44:52 -0700403
404 user_data = (char __user *) (uintptr_t) args->data_ptr;
405 remain = args->size;
406
Daniel Vetter23010e42010-03-08 13:35:02 +0100407 obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700408 offset = args->offset;
409
410 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100411 struct page *page;
412 char *vaddr;
413 int ret;
414
Eric Anholteb014592009-03-10 11:44:52 -0700415 /* Operation in this page
416 *
Eric Anholteb014592009-03-10 11:44:52 -0700417 * page_offset = offset within page
418 * page_length = bytes to copy for this page
419 */
Eric Anholteb014592009-03-10 11:44:52 -0700420 page_offset = offset & (PAGE_SIZE-1);
421 page_length = remain;
422 if ((page_offset + remain) > PAGE_SIZE)
423 page_length = PAGE_SIZE - page_offset;
424
Chris Wilsone5281cc2010-10-28 13:45:36 +0100425 page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
426 GFP_HIGHUSER | __GFP_RECLAIMABLE);
427 if (IS_ERR(page))
428 return PTR_ERR(page);
429
430 vaddr = kmap_atomic(page);
431 ret = __copy_to_user_inatomic(user_data,
432 vaddr + page_offset,
433 page_length);
434 kunmap_atomic(vaddr);
435
436 mark_page_accessed(page);
437 page_cache_release(page);
438 if (ret)
Chris Wilson4f27b752010-10-14 15:26:45 +0100439 return -EFAULT;
Eric Anholteb014592009-03-10 11:44:52 -0700440
441 remain -= page_length;
442 user_data += page_length;
443 offset += page_length;
444 }
445
Chris Wilson4f27b752010-10-14 15:26:45 +0100446 return 0;
Eric Anholteb014592009-03-10 11:44:52 -0700447}
448
449/**
450 * This is the fallback shmem pread path, which allocates temporary storage
451 * in kernel space to copy_to_user into outside of the struct_mutex, so we
452 * can copy out of the object's backing pages while holding the struct mutex
453 * and not take page faults.
454 */
455static int
456i915_gem_shmem_pread_slow(struct drm_device *dev, struct drm_gem_object *obj,
457 struct drm_i915_gem_pread *args,
458 struct drm_file *file_priv)
459{
Chris Wilsone5281cc2010-10-28 13:45:36 +0100460 struct address_space *mapping = obj->filp->f_path.dentry->d_inode->i_mapping;
Daniel Vetter23010e42010-03-08 13:35:02 +0100461 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700462 struct mm_struct *mm = current->mm;
463 struct page **user_pages;
464 ssize_t remain;
465 loff_t offset, pinned_pages, i;
466 loff_t first_data_page, last_data_page, num_pages;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100467 int shmem_page_offset;
468 int data_page_index, data_page_offset;
Eric Anholteb014592009-03-10 11:44:52 -0700469 int page_length;
470 int ret;
471 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700472 int do_bit17_swizzling;
Eric Anholteb014592009-03-10 11:44:52 -0700473
474 remain = args->size;
475
476 /* Pin the user pages containing the data. We can't fault while
477 * holding the struct mutex, yet we want to hold it while
478 * dereferencing the user data.
479 */
480 first_data_page = data_ptr / PAGE_SIZE;
481 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
482 num_pages = last_data_page - first_data_page + 1;
483
Chris Wilson4f27b752010-10-14 15:26:45 +0100484 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
Eric Anholteb014592009-03-10 11:44:52 -0700485 if (user_pages == NULL)
486 return -ENOMEM;
487
Chris Wilson4f27b752010-10-14 15:26:45 +0100488 mutex_unlock(&dev->struct_mutex);
Eric Anholteb014592009-03-10 11:44:52 -0700489 down_read(&mm->mmap_sem);
490 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
Eric Anholte5e9ecd2009-04-07 16:01:22 -0700491 num_pages, 1, 0, user_pages, NULL);
Eric Anholteb014592009-03-10 11:44:52 -0700492 up_read(&mm->mmap_sem);
Chris Wilson4f27b752010-10-14 15:26:45 +0100493 mutex_lock(&dev->struct_mutex);
Eric Anholteb014592009-03-10 11:44:52 -0700494 if (pinned_pages < num_pages) {
495 ret = -EFAULT;
Chris Wilson4f27b752010-10-14 15:26:45 +0100496 goto out;
Eric Anholteb014592009-03-10 11:44:52 -0700497 }
498
Chris Wilson4f27b752010-10-14 15:26:45 +0100499 ret = i915_gem_object_set_cpu_read_domain_range(obj,
500 args->offset,
Eric Anholteb014592009-03-10 11:44:52 -0700501 args->size);
Chris Wilson4f27b752010-10-14 15:26:45 +0100502 if (ret)
503 goto out;
504
505 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700506
Daniel Vetter23010e42010-03-08 13:35:02 +0100507 obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700508 offset = args->offset;
509
510 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100511 struct page *page;
512
Eric Anholteb014592009-03-10 11:44:52 -0700513 /* Operation in this page
514 *
Eric Anholteb014592009-03-10 11:44:52 -0700515 * shmem_page_offset = offset within page in shmem file
516 * data_page_index = page number in get_user_pages return
517 * data_page_offset = offset with data_page_index page.
518 * page_length = bytes to copy for this page
519 */
Eric Anholteb014592009-03-10 11:44:52 -0700520 shmem_page_offset = offset & ~PAGE_MASK;
521 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
522 data_page_offset = data_ptr & ~PAGE_MASK;
523
524 page_length = remain;
525 if ((shmem_page_offset + page_length) > PAGE_SIZE)
526 page_length = PAGE_SIZE - shmem_page_offset;
527 if ((data_page_offset + page_length) > PAGE_SIZE)
528 page_length = PAGE_SIZE - data_page_offset;
529
Chris Wilsone5281cc2010-10-28 13:45:36 +0100530 page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
531 GFP_HIGHUSER | __GFP_RECLAIMABLE);
532 if (IS_ERR(page))
533 return PTR_ERR(page);
534
Eric Anholt280b7132009-03-12 16:56:27 -0700535 if (do_bit17_swizzling) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100536 slow_shmem_bit17_copy(page,
Eric Anholt280b7132009-03-12 16:56:27 -0700537 shmem_page_offset,
Chris Wilson99a03df2010-05-27 14:15:34 +0100538 user_pages[data_page_index],
539 data_page_offset,
540 page_length,
541 1);
542 } else {
543 slow_shmem_copy(user_pages[data_page_index],
544 data_page_offset,
Chris Wilsone5281cc2010-10-28 13:45:36 +0100545 page,
Chris Wilson99a03df2010-05-27 14:15:34 +0100546 shmem_page_offset,
547 page_length);
Eric Anholt280b7132009-03-12 16:56:27 -0700548 }
Eric Anholteb014592009-03-10 11:44:52 -0700549
Chris Wilsone5281cc2010-10-28 13:45:36 +0100550 mark_page_accessed(page);
551 page_cache_release(page);
552
Eric Anholteb014592009-03-10 11:44:52 -0700553 remain -= page_length;
554 data_ptr += page_length;
555 offset += page_length;
556 }
557
Chris Wilson4f27b752010-10-14 15:26:45 +0100558out:
Eric Anholteb014592009-03-10 11:44:52 -0700559 for (i = 0; i < pinned_pages; i++) {
560 SetPageDirty(user_pages[i]);
Chris Wilsone5281cc2010-10-28 13:45:36 +0100561 mark_page_accessed(user_pages[i]);
Eric Anholteb014592009-03-10 11:44:52 -0700562 page_cache_release(user_pages[i]);
563 }
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700564 drm_free_large(user_pages);
Eric Anholteb014592009-03-10 11:44:52 -0700565
566 return ret;
567}
568
Eric Anholt673a3942008-07-30 12:06:12 -0700569/**
570 * Reads data from the object referenced by handle.
571 *
572 * On error, the contents of *data are undefined.
573 */
574int
575i915_gem_pread_ioctl(struct drm_device *dev, void *data,
576 struct drm_file *file_priv)
577{
578 struct drm_i915_gem_pread *args = data;
579 struct drm_gem_object *obj;
580 struct drm_i915_gem_object *obj_priv;
Chris Wilson35b62a82010-09-26 20:23:38 +0100581 int ret = 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700582
Chris Wilson51311d02010-11-17 09:10:42 +0000583 if (args->size == 0)
584 return 0;
585
586 if (!access_ok(VERIFY_WRITE,
587 (char __user *)(uintptr_t)args->data_ptr,
588 args->size))
589 return -EFAULT;
590
591 ret = fault_in_pages_writeable((char __user *)(uintptr_t)args->data_ptr,
592 args->size);
593 if (ret)
594 return -EFAULT;
595
Chris Wilson4f27b752010-10-14 15:26:45 +0100596 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100597 if (ret)
Chris Wilson4f27b752010-10-14 15:26:45 +0100598 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700599
600 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100601 if (obj == NULL) {
602 ret = -ENOENT;
603 goto unlock;
Chris Wilson4f27b752010-10-14 15:26:45 +0100604 }
Daniel Vetter23010e42010-03-08 13:35:02 +0100605 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700606
Chris Wilson7dcd2492010-09-26 20:21:44 +0100607 /* Bounds check source. */
608 if (args->offset > obj->size || args->size > obj->size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +0100609 ret = -EINVAL;
Chris Wilson35b62a82010-09-26 20:23:38 +0100610 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +0100611 }
612
Chris Wilson4f27b752010-10-14 15:26:45 +0100613 ret = i915_gem_object_set_cpu_read_domain_range(obj,
614 args->offset,
615 args->size);
616 if (ret)
Chris Wilsone5281cc2010-10-28 13:45:36 +0100617 goto out;
Chris Wilson4f27b752010-10-14 15:26:45 +0100618
619 ret = -EFAULT;
620 if (!i915_gem_object_needs_bit17_swizzle(obj))
Eric Anholt280b7132009-03-12 16:56:27 -0700621 ret = i915_gem_shmem_pread_fast(dev, obj, args, file_priv);
Chris Wilson4f27b752010-10-14 15:26:45 +0100622 if (ret == -EFAULT)
623 ret = i915_gem_shmem_pread_slow(dev, obj, args, file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -0700624
Chris Wilson35b62a82010-09-26 20:23:38 +0100625out:
Chris Wilson4f27b752010-10-14 15:26:45 +0100626 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100627unlock:
Chris Wilson4f27b752010-10-14 15:26:45 +0100628 mutex_unlock(&dev->struct_mutex);
Eric Anholteb014592009-03-10 11:44:52 -0700629 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700630}
631
Keith Packard0839ccb2008-10-30 19:38:48 -0700632/* This is the fast write path which cannot handle
633 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700634 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700635
Keith Packard0839ccb2008-10-30 19:38:48 -0700636static inline int
637fast_user_write(struct io_mapping *mapping,
638 loff_t page_base, int page_offset,
639 char __user *user_data,
640 int length)
641{
642 char *vaddr_atomic;
643 unsigned long unwritten;
644
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700645 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
Keith Packard0839ccb2008-10-30 19:38:48 -0700646 unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
647 user_data, length);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700648 io_mapping_unmap_atomic(vaddr_atomic);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100649 return unwritten;
Keith Packard0839ccb2008-10-30 19:38:48 -0700650}
651
652/* Here's the write path which can sleep for
653 * page faults
654 */
655
Chris Wilsonab34c222010-05-27 14:15:35 +0100656static inline void
Eric Anholt3de09aa2009-03-09 09:42:23 -0700657slow_kernel_write(struct io_mapping *mapping,
658 loff_t gtt_base, int gtt_offset,
659 struct page *user_page, int user_offset,
660 int length)
Keith Packard0839ccb2008-10-30 19:38:48 -0700661{
Chris Wilsonab34c222010-05-27 14:15:35 +0100662 char __iomem *dst_vaddr;
663 char *src_vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -0700664
Chris Wilsonab34c222010-05-27 14:15:35 +0100665 dst_vaddr = io_mapping_map_wc(mapping, gtt_base);
666 src_vaddr = kmap(user_page);
667
668 memcpy_toio(dst_vaddr + gtt_offset,
669 src_vaddr + user_offset,
670 length);
671
672 kunmap(user_page);
673 io_mapping_unmap(dst_vaddr);
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700674}
675
Eric Anholt3de09aa2009-03-09 09:42:23 -0700676/**
677 * This is the fast pwrite path, where we copy the data directly from the
678 * user into the GTT, uncached.
679 */
Eric Anholt673a3942008-07-30 12:06:12 -0700680static int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700681i915_gem_gtt_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
682 struct drm_i915_gem_pwrite *args,
683 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700684{
Daniel Vetter23010e42010-03-08 13:35:02 +0100685 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Keith Packard0839ccb2008-10-30 19:38:48 -0700686 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700687 ssize_t remain;
Keith Packard0839ccb2008-10-30 19:38:48 -0700688 loff_t offset, page_base;
Eric Anholt673a3942008-07-30 12:06:12 -0700689 char __user *user_data;
Keith Packard0839ccb2008-10-30 19:38:48 -0700690 int page_offset, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700691
692 user_data = (char __user *) (uintptr_t) args->data_ptr;
693 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700694
Daniel Vetter23010e42010-03-08 13:35:02 +0100695 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700696 offset = obj_priv->gtt_offset + args->offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700697
698 while (remain > 0) {
699 /* Operation in this page
700 *
Keith Packard0839ccb2008-10-30 19:38:48 -0700701 * page_base = page offset within aperture
702 * page_offset = offset within page
703 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -0700704 */
Keith Packard0839ccb2008-10-30 19:38:48 -0700705 page_base = (offset & ~(PAGE_SIZE-1));
706 page_offset = offset & (PAGE_SIZE-1);
707 page_length = remain;
708 if ((page_offset + remain) > PAGE_SIZE)
709 page_length = PAGE_SIZE - page_offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700710
Keith Packard0839ccb2008-10-30 19:38:48 -0700711 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -0700712 * source page isn't available. Return the error and we'll
713 * retry in the slow path.
Keith Packard0839ccb2008-10-30 19:38:48 -0700714 */
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100715 if (fast_user_write(dev_priv->mm.gtt_mapping, page_base,
716 page_offset, user_data, page_length))
717
718 return -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -0700719
Keith Packard0839ccb2008-10-30 19:38:48 -0700720 remain -= page_length;
721 user_data += page_length;
722 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700723 }
Eric Anholt673a3942008-07-30 12:06:12 -0700724
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100725 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700726}
727
Eric Anholt3de09aa2009-03-09 09:42:23 -0700728/**
729 * This is the fallback GTT pwrite path, which uses get_user_pages to pin
730 * the memory and maps it using kmap_atomic for copying.
731 *
732 * This code resulted in x11perf -rgb10text consuming about 10% more CPU
733 * than using i915_gem_gtt_pwrite_fast on a G45 (32-bit).
734 */
Eric Anholt3043c602008-10-02 12:24:47 -0700735static int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700736i915_gem_gtt_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
737 struct drm_i915_gem_pwrite *args,
738 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700739{
Daniel Vetter23010e42010-03-08 13:35:02 +0100740 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700741 drm_i915_private_t *dev_priv = dev->dev_private;
742 ssize_t remain;
743 loff_t gtt_page_base, offset;
744 loff_t first_data_page, last_data_page, num_pages;
745 loff_t pinned_pages, i;
746 struct page **user_pages;
747 struct mm_struct *mm = current->mm;
748 int gtt_page_offset, data_page_offset, data_page_index, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700749 int ret;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700750 uint64_t data_ptr = args->data_ptr;
751
752 remain = args->size;
753
754 /* Pin the user pages containing the data. We can't fault while
755 * holding the struct mutex, and all of the pwrite implementations
756 * want to hold it while dereferencing the user data.
757 */
758 first_data_page = data_ptr / PAGE_SIZE;
759 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
760 num_pages = last_data_page - first_data_page + 1;
761
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100762 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
Eric Anholt3de09aa2009-03-09 09:42:23 -0700763 if (user_pages == NULL)
764 return -ENOMEM;
765
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100766 mutex_unlock(&dev->struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700767 down_read(&mm->mmap_sem);
768 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
769 num_pages, 0, 0, user_pages, NULL);
770 up_read(&mm->mmap_sem);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100771 mutex_lock(&dev->struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700772 if (pinned_pages < num_pages) {
773 ret = -EFAULT;
774 goto out_unpin_pages;
775 }
776
Eric Anholt3de09aa2009-03-09 09:42:23 -0700777 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
778 if (ret)
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100779 goto out_unpin_pages;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700780
Daniel Vetter23010e42010-03-08 13:35:02 +0100781 obj_priv = to_intel_bo(obj);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700782 offset = obj_priv->gtt_offset + args->offset;
783
784 while (remain > 0) {
785 /* Operation in this page
786 *
787 * gtt_page_base = page offset within aperture
788 * gtt_page_offset = offset within page in aperture
789 * data_page_index = page number in get_user_pages return
790 * data_page_offset = offset with data_page_index page.
791 * page_length = bytes to copy for this page
792 */
793 gtt_page_base = offset & PAGE_MASK;
794 gtt_page_offset = offset & ~PAGE_MASK;
795 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
796 data_page_offset = data_ptr & ~PAGE_MASK;
797
798 page_length = remain;
799 if ((gtt_page_offset + page_length) > PAGE_SIZE)
800 page_length = PAGE_SIZE - gtt_page_offset;
801 if ((data_page_offset + page_length) > PAGE_SIZE)
802 page_length = PAGE_SIZE - data_page_offset;
803
Chris Wilsonab34c222010-05-27 14:15:35 +0100804 slow_kernel_write(dev_priv->mm.gtt_mapping,
805 gtt_page_base, gtt_page_offset,
806 user_pages[data_page_index],
807 data_page_offset,
808 page_length);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700809
810 remain -= page_length;
811 offset += page_length;
812 data_ptr += page_length;
813 }
814
Eric Anholt3de09aa2009-03-09 09:42:23 -0700815out_unpin_pages:
816 for (i = 0; i < pinned_pages; i++)
817 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700818 drm_free_large(user_pages);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700819
820 return ret;
821}
822
Eric Anholt40123c12009-03-09 13:42:30 -0700823/**
824 * This is the fast shmem pwrite path, which attempts to directly
825 * copy_from_user into the kmapped pages backing the object.
826 */
Eric Anholt673a3942008-07-30 12:06:12 -0700827static int
Eric Anholt40123c12009-03-09 13:42:30 -0700828i915_gem_shmem_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
829 struct drm_i915_gem_pwrite *args,
830 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700831{
Chris Wilsone5281cc2010-10-28 13:45:36 +0100832 struct address_space *mapping = obj->filp->f_path.dentry->d_inode->i_mapping;
Daniel Vetter23010e42010-03-08 13:35:02 +0100833 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700834 ssize_t remain;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100835 loff_t offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700836 char __user *user_data;
837 int page_offset, page_length;
Eric Anholt40123c12009-03-09 13:42:30 -0700838
839 user_data = (char __user *) (uintptr_t) args->data_ptr;
840 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700841
Daniel Vetter23010e42010-03-08 13:35:02 +0100842 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700843 offset = args->offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700844 obj_priv->dirty = 1;
Eric Anholt673a3942008-07-30 12:06:12 -0700845
Eric Anholt40123c12009-03-09 13:42:30 -0700846 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100847 struct page *page;
848 char *vaddr;
849 int ret;
850
Eric Anholt40123c12009-03-09 13:42:30 -0700851 /* Operation in this page
852 *
Eric Anholt40123c12009-03-09 13:42:30 -0700853 * page_offset = offset within page
854 * page_length = bytes to copy for this page
855 */
Eric Anholt40123c12009-03-09 13:42:30 -0700856 page_offset = offset & (PAGE_SIZE-1);
857 page_length = remain;
858 if ((page_offset + remain) > PAGE_SIZE)
859 page_length = PAGE_SIZE - page_offset;
860
Chris Wilsone5281cc2010-10-28 13:45:36 +0100861 page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
862 GFP_HIGHUSER | __GFP_RECLAIMABLE);
863 if (IS_ERR(page))
864 return PTR_ERR(page);
865
866 vaddr = kmap_atomic(page, KM_USER0);
867 ret = __copy_from_user_inatomic(vaddr + page_offset,
868 user_data,
869 page_length);
870 kunmap_atomic(vaddr, KM_USER0);
871
872 set_page_dirty(page);
873 mark_page_accessed(page);
874 page_cache_release(page);
875
876 /* If we get a fault while copying data, then (presumably) our
877 * source page isn't available. Return the error and we'll
878 * retry in the slow path.
879 */
880 if (ret)
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100881 return -EFAULT;
Eric Anholt40123c12009-03-09 13:42:30 -0700882
883 remain -= page_length;
884 user_data += page_length;
885 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700886 }
887
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100888 return 0;
Eric Anholt40123c12009-03-09 13:42:30 -0700889}
890
891/**
892 * This is the fallback shmem pwrite path, which uses get_user_pages to pin
893 * the memory and maps it using kmap_atomic for copying.
894 *
895 * This avoids taking mmap_sem for faulting on the user's address while the
896 * struct_mutex is held.
897 */
898static int
899i915_gem_shmem_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
900 struct drm_i915_gem_pwrite *args,
901 struct drm_file *file_priv)
902{
Chris Wilsone5281cc2010-10-28 13:45:36 +0100903 struct address_space *mapping = obj->filp->f_path.dentry->d_inode->i_mapping;
Daniel Vetter23010e42010-03-08 13:35:02 +0100904 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700905 struct mm_struct *mm = current->mm;
906 struct page **user_pages;
907 ssize_t remain;
908 loff_t offset, pinned_pages, i;
909 loff_t first_data_page, last_data_page, num_pages;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100910 int shmem_page_offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700911 int data_page_index, data_page_offset;
912 int page_length;
913 int ret;
914 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700915 int do_bit17_swizzling;
Eric Anholt40123c12009-03-09 13:42:30 -0700916
917 remain = args->size;
918
919 /* Pin the user pages containing the data. We can't fault while
920 * holding the struct mutex, and all of the pwrite implementations
921 * want to hold it while dereferencing the user data.
922 */
923 first_data_page = data_ptr / PAGE_SIZE;
924 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
925 num_pages = last_data_page - first_data_page + 1;
926
Chris Wilson4f27b752010-10-14 15:26:45 +0100927 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
Eric Anholt40123c12009-03-09 13:42:30 -0700928 if (user_pages == NULL)
929 return -ENOMEM;
930
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100931 mutex_unlock(&dev->struct_mutex);
Eric Anholt40123c12009-03-09 13:42:30 -0700932 down_read(&mm->mmap_sem);
933 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
934 num_pages, 0, 0, user_pages, NULL);
935 up_read(&mm->mmap_sem);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100936 mutex_lock(&dev->struct_mutex);
Eric Anholt40123c12009-03-09 13:42:30 -0700937 if (pinned_pages < num_pages) {
938 ret = -EFAULT;
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100939 goto out;
Eric Anholt40123c12009-03-09 13:42:30 -0700940 }
941
Eric Anholt40123c12009-03-09 13:42:30 -0700942 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100943 if (ret)
944 goto out;
945
946 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700947
Daniel Vetter23010e42010-03-08 13:35:02 +0100948 obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700949 offset = args->offset;
950 obj_priv->dirty = 1;
951
952 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100953 struct page *page;
954
Eric Anholt40123c12009-03-09 13:42:30 -0700955 /* Operation in this page
956 *
Eric Anholt40123c12009-03-09 13:42:30 -0700957 * shmem_page_offset = offset within page in shmem file
958 * data_page_index = page number in get_user_pages return
959 * data_page_offset = offset with data_page_index page.
960 * page_length = bytes to copy for this page
961 */
Eric Anholt40123c12009-03-09 13:42:30 -0700962 shmem_page_offset = offset & ~PAGE_MASK;
963 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
964 data_page_offset = data_ptr & ~PAGE_MASK;
965
966 page_length = remain;
967 if ((shmem_page_offset + page_length) > PAGE_SIZE)
968 page_length = PAGE_SIZE - shmem_page_offset;
969 if ((data_page_offset + page_length) > PAGE_SIZE)
970 page_length = PAGE_SIZE - data_page_offset;
971
Chris Wilsone5281cc2010-10-28 13:45:36 +0100972 page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
973 GFP_HIGHUSER | __GFP_RECLAIMABLE);
974 if (IS_ERR(page)) {
975 ret = PTR_ERR(page);
976 goto out;
977 }
978
Eric Anholt280b7132009-03-12 16:56:27 -0700979 if (do_bit17_swizzling) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100980 slow_shmem_bit17_copy(page,
Eric Anholt280b7132009-03-12 16:56:27 -0700981 shmem_page_offset,
982 user_pages[data_page_index],
983 data_page_offset,
Chris Wilson99a03df2010-05-27 14:15:34 +0100984 page_length,
985 0);
986 } else {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100987 slow_shmem_copy(page,
Chris Wilson99a03df2010-05-27 14:15:34 +0100988 shmem_page_offset,
989 user_pages[data_page_index],
990 data_page_offset,
991 page_length);
Eric Anholt280b7132009-03-12 16:56:27 -0700992 }
Eric Anholt40123c12009-03-09 13:42:30 -0700993
Chris Wilsone5281cc2010-10-28 13:45:36 +0100994 set_page_dirty(page);
995 mark_page_accessed(page);
996 page_cache_release(page);
997
Eric Anholt40123c12009-03-09 13:42:30 -0700998 remain -= page_length;
999 data_ptr += page_length;
1000 offset += page_length;
1001 }
1002
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001003out:
Eric Anholt40123c12009-03-09 13:42:30 -07001004 for (i = 0; i < pinned_pages; i++)
1005 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07001006 drm_free_large(user_pages);
Eric Anholt40123c12009-03-09 13:42:30 -07001007
1008 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001009}
1010
1011/**
1012 * Writes data to the object referenced by handle.
1013 *
1014 * On error, the contents of the buffer that were to be modified are undefined.
1015 */
1016int
1017i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001018 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001019{
1020 struct drm_i915_gem_pwrite *args = data;
1021 struct drm_gem_object *obj;
1022 struct drm_i915_gem_object *obj_priv;
Chris Wilson51311d02010-11-17 09:10:42 +00001023 int ret;
1024
1025 if (args->size == 0)
1026 return 0;
1027
1028 if (!access_ok(VERIFY_READ,
1029 (char __user *)(uintptr_t)args->data_ptr,
1030 args->size))
1031 return -EFAULT;
1032
1033 ret = fault_in_pages_readable((char __user *)(uintptr_t)args->data_ptr,
1034 args->size);
1035 if (ret)
1036 return -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -07001037
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001038 ret = i915_mutex_lock_interruptible(dev);
1039 if (ret)
1040 return ret;
1041
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001042 obj = drm_gem_object_lookup(dev, file, args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001043 if (obj == NULL) {
1044 ret = -ENOENT;
1045 goto unlock;
1046 }
Daniel Vetter23010e42010-03-08 13:35:02 +01001047 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001048
Chris Wilson7dcd2492010-09-26 20:21:44 +01001049 /* Bounds check destination. */
1050 if (args->offset > obj->size || args->size > obj->size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001051 ret = -EINVAL;
Chris Wilson35b62a82010-09-26 20:23:38 +01001052 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001053 }
1054
Eric Anholt673a3942008-07-30 12:06:12 -07001055 /* We can only do the GTT pwrite on untiled buffers, as otherwise
1056 * it would end up going through the fenced access, and we'll get
1057 * different detiling behavior between reading and writing.
1058 * pread/pwrite currently are reading and writing from the CPU
1059 * perspective, requiring manual detiling by the client.
1060 */
Dave Airlie71acb5e2008-12-30 20:31:46 +10001061 if (obj_priv->phys_obj)
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001062 ret = i915_gem_phys_pwrite(dev, obj, args, file);
Dave Airlie71acb5e2008-12-30 20:31:46 +10001063 else if (obj_priv->tiling_mode == I915_TILING_NONE &&
Chris Wilson5cdf5882010-09-27 15:51:07 +01001064 obj_priv->gtt_space &&
Chris Wilson9b8c4a02010-05-27 14:21:01 +01001065 obj->write_domain != I915_GEM_DOMAIN_CPU) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01001066 ret = i915_gem_object_pin(obj, 0, true);
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001067 if (ret)
1068 goto out;
1069
1070 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
1071 if (ret)
1072 goto out_unpin;
1073
1074 ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file);
1075 if (ret == -EFAULT)
1076 ret = i915_gem_gtt_pwrite_slow(dev, obj, args, file);
1077
1078out_unpin:
1079 i915_gem_object_unpin(obj);
Eric Anholt40123c12009-03-09 13:42:30 -07001080 } else {
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001081 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
1082 if (ret)
Chris Wilsone5281cc2010-10-28 13:45:36 +01001083 goto out;
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001084
1085 ret = -EFAULT;
1086 if (!i915_gem_object_needs_bit17_swizzle(obj))
1087 ret = i915_gem_shmem_pwrite_fast(dev, obj, args, file);
1088 if (ret == -EFAULT)
1089 ret = i915_gem_shmem_pwrite_slow(dev, obj, args, file);
Eric Anholt40123c12009-03-09 13:42:30 -07001090 }
Eric Anholt673a3942008-07-30 12:06:12 -07001091
Chris Wilson35b62a82010-09-26 20:23:38 +01001092out:
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001093 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001094unlock:
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001095 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07001096 return ret;
1097}
1098
1099/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001100 * Called when user space prepares to use an object with the CPU, either
1101 * through the mmap ioctl's mapping or a GTT mapping.
Eric Anholt673a3942008-07-30 12:06:12 -07001102 */
1103int
1104i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
1105 struct drm_file *file_priv)
1106{
Eric Anholta09ba7f2009-08-29 12:49:51 -07001107 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07001108 struct drm_i915_gem_set_domain *args = data;
1109 struct drm_gem_object *obj;
Jesse Barnes652c3932009-08-17 13:31:43 -07001110 struct drm_i915_gem_object *obj_priv;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001111 uint32_t read_domains = args->read_domains;
1112 uint32_t write_domain = args->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07001113 int ret;
1114
1115 if (!(dev->driver->driver_features & DRIVER_GEM))
1116 return -ENODEV;
1117
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001118 /* Only handle setting domains to types used by the CPU. */
Chris Wilson21d509e2009-06-06 09:46:02 +01001119 if (write_domain & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001120 return -EINVAL;
1121
Chris Wilson21d509e2009-06-06 09:46:02 +01001122 if (read_domains & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001123 return -EINVAL;
1124
1125 /* Having something in the write domain implies it's in the read
1126 * domain, and only that read domain. Enforce that in the request.
1127 */
1128 if (write_domain != 0 && read_domains != write_domain)
1129 return -EINVAL;
1130
Chris Wilson76c1dec2010-09-25 11:22:51 +01001131 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001132 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001133 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001134
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001135 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1136 if (obj == NULL) {
1137 ret = -ENOENT;
1138 goto unlock;
Chris Wilson76c1dec2010-09-25 11:22:51 +01001139 }
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001140 obj_priv = to_intel_bo(obj);
Jesse Barnes652c3932009-08-17 13:31:43 -07001141
1142 intel_mark_busy(dev, obj);
1143
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001144 if (read_domains & I915_GEM_DOMAIN_GTT) {
1145 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
Eric Anholt02354392008-11-26 13:58:13 -08001146
Eric Anholta09ba7f2009-08-29 12:49:51 -07001147 /* Update the LRU on the fence for the CPU access that's
1148 * about to occur.
1149 */
1150 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001151 struct drm_i915_fence_reg *reg =
1152 &dev_priv->fence_regs[obj_priv->fence_reg];
1153 list_move_tail(&reg->lru_list,
Eric Anholta09ba7f2009-08-29 12:49:51 -07001154 &dev_priv->mm.fence_list);
1155 }
1156
Eric Anholt02354392008-11-26 13:58:13 -08001157 /* Silently promote "you're not bound, there was nothing to do"
1158 * to success, since the client was just asking us to
1159 * make sure everything was done.
1160 */
1161 if (ret == -EINVAL)
1162 ret = 0;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001163 } else {
Eric Anholte47c68e2008-11-14 13:35:19 -08001164 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001165 }
1166
Chris Wilson7d1c4802010-08-07 21:45:03 +01001167 /* Maintain LRU order of "inactive" objects */
1168 if (ret == 0 && i915_gem_object_is_inactive(obj_priv))
Chris Wilson69dc4982010-10-19 10:36:51 +01001169 list_move_tail(&obj_priv->mm_list, &dev_priv->mm.inactive_list);
Chris Wilson7d1c4802010-08-07 21:45:03 +01001170
Eric Anholt673a3942008-07-30 12:06:12 -07001171 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001172unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07001173 mutex_unlock(&dev->struct_mutex);
1174 return ret;
1175}
1176
1177/**
1178 * Called when user space has done writes to this buffer
1179 */
1180int
1181i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
1182 struct drm_file *file_priv)
1183{
1184 struct drm_i915_gem_sw_finish *args = data;
1185 struct drm_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001186 int ret = 0;
1187
1188 if (!(dev->driver->driver_features & DRIVER_GEM))
1189 return -ENODEV;
1190
Chris Wilson76c1dec2010-09-25 11:22:51 +01001191 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001192 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001193 return ret;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001194
Eric Anholt673a3942008-07-30 12:06:12 -07001195 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1196 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001197 ret = -ENOENT;
1198 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07001199 }
1200
Eric Anholt673a3942008-07-30 12:06:12 -07001201 /* Pinned buffers may be scanout, so flush the cache */
Chris Wilson3d2a8122010-09-29 11:39:53 +01001202 if (to_intel_bo(obj)->pin_count)
Eric Anholte47c68e2008-11-14 13:35:19 -08001203 i915_gem_object_flush_cpu_write_domain(obj);
1204
Eric Anholt673a3942008-07-30 12:06:12 -07001205 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001206unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07001207 mutex_unlock(&dev->struct_mutex);
1208 return ret;
1209}
1210
1211/**
1212 * Maps the contents of an object, returning the address it is mapped
1213 * into.
1214 *
1215 * While the mapping holds a reference on the contents of the object, it doesn't
1216 * imply a ref on the object itself.
1217 */
1218int
1219i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
1220 struct drm_file *file_priv)
1221{
Chris Wilsonda761a62010-10-27 17:37:08 +01001222 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07001223 struct drm_i915_gem_mmap *args = data;
1224 struct drm_gem_object *obj;
1225 loff_t offset;
1226 unsigned long addr;
1227
1228 if (!(dev->driver->driver_features & DRIVER_GEM))
1229 return -ENODEV;
1230
1231 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1232 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001233 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001234
Chris Wilsonda761a62010-10-27 17:37:08 +01001235 if (obj->size > dev_priv->mm.gtt_mappable_end) {
1236 drm_gem_object_unreference_unlocked(obj);
1237 return -E2BIG;
1238 }
1239
Eric Anholt673a3942008-07-30 12:06:12 -07001240 offset = args->offset;
1241
1242 down_write(&current->mm->mmap_sem);
1243 addr = do_mmap(obj->filp, 0, args->size,
1244 PROT_READ | PROT_WRITE, MAP_SHARED,
1245 args->offset);
1246 up_write(&current->mm->mmap_sem);
Luca Barbieribc9025b2010-02-09 05:49:12 +00001247 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001248 if (IS_ERR((void *)addr))
1249 return addr;
1250
1251 args->addr_ptr = (uint64_t) addr;
1252
1253 return 0;
1254}
1255
Jesse Barnesde151cf2008-11-12 10:03:55 -08001256/**
1257 * i915_gem_fault - fault a page into the GTT
1258 * vma: VMA in question
1259 * vmf: fault info
1260 *
1261 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1262 * from userspace. The fault handler takes care of binding the object to
1263 * the GTT (if needed), allocating and programming a fence register (again,
1264 * only if needed based on whether the old reg is still valid or the object
1265 * is tiled) and inserting a new PTE into the faulting process.
1266 *
1267 * Note that the faulting process may involve evicting existing objects
1268 * from the GTT and/or fence registers to make room. So performance may
1269 * suffer if the GTT working set is large or there are few fence registers
1270 * left.
1271 */
1272int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1273{
1274 struct drm_gem_object *obj = vma->vm_private_data;
1275 struct drm_device *dev = obj->dev;
Chris Wilson7d1c4802010-08-07 21:45:03 +01001276 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001277 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001278 pgoff_t page_offset;
1279 unsigned long pfn;
1280 int ret = 0;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001281 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001282
1283 /* We don't use vmf->pgoff since that has the fake offset */
1284 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1285 PAGE_SHIFT;
1286
1287 /* Now bind it into the GTT if needed */
1288 mutex_lock(&dev->struct_mutex);
Daniel Vetterfb7d5162010-10-01 22:05:20 +02001289 BUG_ON(obj_priv->pin_count && !obj_priv->pin_mappable);
Chris Wilsona00b10c2010-09-24 21:15:47 +01001290
1291 if (obj_priv->gtt_space) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01001292 if (!obj_priv->map_and_fenceable) {
Chris Wilsona00b10c2010-09-24 21:15:47 +01001293 ret = i915_gem_object_unbind(obj);
1294 if (ret)
1295 goto unlock;
1296 }
1297 }
Daniel Vetter16e809a2010-09-16 19:37:04 +02001298
Jesse Barnesde151cf2008-11-12 10:03:55 -08001299 if (!obj_priv->gtt_space) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01001300 ret = i915_gem_object_bind_to_gtt(obj, 0, true);
Chris Wilsonc7150892009-09-23 00:43:56 +01001301 if (ret)
1302 goto unlock;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001303 }
1304
Chris Wilson4a684a42010-10-28 14:44:08 +01001305 ret = i915_gem_object_set_to_gtt_domain(obj, write);
1306 if (ret)
1307 goto unlock;
1308
Daniel Vetterfb7d5162010-10-01 22:05:20 +02001309 if (!obj_priv->fault_mappable) {
1310 obj_priv->fault_mappable = true;
Chris Wilsona00b10c2010-09-24 21:15:47 +01001311 i915_gem_info_update_mappable(dev_priv, obj_priv, true);
Daniel Vetterfb7d5162010-10-01 22:05:20 +02001312 }
1313
Jesse Barnesde151cf2008-11-12 10:03:55 -08001314 /* Need a new fence register? */
Eric Anholta09ba7f2009-08-29 12:49:51 -07001315 if (obj_priv->tiling_mode != I915_TILING_NONE) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01001316 ret = i915_gem_object_get_fence_reg(obj, true);
Chris Wilsonc7150892009-09-23 00:43:56 +01001317 if (ret)
1318 goto unlock;
Eric Anholtd9ddcb92009-01-27 10:33:49 -08001319 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001320
Chris Wilson7d1c4802010-08-07 21:45:03 +01001321 if (i915_gem_object_is_inactive(obj_priv))
Chris Wilson69dc4982010-10-19 10:36:51 +01001322 list_move_tail(&obj_priv->mm_list, &dev_priv->mm.inactive_list);
Chris Wilson7d1c4802010-08-07 21:45:03 +01001323
Jesse Barnesde151cf2008-11-12 10:03:55 -08001324 pfn = ((dev->agp->base + obj_priv->gtt_offset) >> PAGE_SHIFT) +
1325 page_offset;
1326
1327 /* Finally, remap it using the new GTT offset */
1328 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
Chris Wilsonc7150892009-09-23 00:43:56 +01001329unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001330 mutex_unlock(&dev->struct_mutex);
1331
1332 switch (ret) {
Chris Wilson045e7692010-11-07 09:18:22 +00001333 case -EAGAIN:
1334 set_need_resched();
Chris Wilsonc7150892009-09-23 00:43:56 +01001335 case 0:
1336 case -ERESTARTSYS:
1337 return VM_FAULT_NOPAGE;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001338 case -ENOMEM:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001339 return VM_FAULT_OOM;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001340 default:
Chris Wilsonc7150892009-09-23 00:43:56 +01001341 return VM_FAULT_SIGBUS;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001342 }
1343}
1344
1345/**
1346 * i915_gem_create_mmap_offset - create a fake mmap offset for an object
1347 * @obj: obj in question
1348 *
1349 * GEM memory mapping works by handing back to userspace a fake mmap offset
1350 * it can use in a subsequent mmap(2) call. The DRM core code then looks
1351 * up the object based on the offset and sets up the various memory mapping
1352 * structures.
1353 *
1354 * This routine allocates and attaches a fake offset for @obj.
1355 */
1356static int
1357i915_gem_create_mmap_offset(struct drm_gem_object *obj)
1358{
1359 struct drm_device *dev = obj->dev;
1360 struct drm_gem_mm *mm = dev->mm_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001361 struct drm_map_list *list;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +11001362 struct drm_local_map *map;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001363 int ret = 0;
1364
1365 /* Set the object up for mmap'ing */
1366 list = &obj->map_list;
Eric Anholt9a298b22009-03-24 12:23:04 -07001367 list->map = kzalloc(sizeof(struct drm_map_list), GFP_KERNEL);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001368 if (!list->map)
1369 return -ENOMEM;
1370
1371 map = list->map;
1372 map->type = _DRM_GEM;
1373 map->size = obj->size;
1374 map->handle = obj;
1375
1376 /* Get a DRM GEM mmap offset allocated... */
1377 list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
1378 obj->size / PAGE_SIZE, 0, 0);
1379 if (!list->file_offset_node) {
1380 DRM_ERROR("failed to allocate offset for bo %d\n", obj->name);
Chris Wilson9e0ae5342010-09-21 15:05:24 +01001381 ret = -ENOSPC;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001382 goto out_free_list;
1383 }
1384
1385 list->file_offset_node = drm_mm_get_block(list->file_offset_node,
1386 obj->size / PAGE_SIZE, 0);
1387 if (!list->file_offset_node) {
1388 ret = -ENOMEM;
1389 goto out_free_list;
1390 }
1391
1392 list->hash.key = list->file_offset_node->start;
Chris Wilson9e0ae5342010-09-21 15:05:24 +01001393 ret = drm_ht_insert_item(&mm->offset_hash, &list->hash);
1394 if (ret) {
Jesse Barnesde151cf2008-11-12 10:03:55 -08001395 DRM_ERROR("failed to add to map hash\n");
1396 goto out_free_mm;
1397 }
1398
Jesse Barnesde151cf2008-11-12 10:03:55 -08001399 return 0;
1400
1401out_free_mm:
1402 drm_mm_put_block(list->file_offset_node);
1403out_free_list:
Eric Anholt9a298b22009-03-24 12:23:04 -07001404 kfree(list->map);
Chris Wilson39a01d12010-10-28 13:03:06 +01001405 list->map = NULL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001406
1407 return ret;
1408}
1409
Chris Wilson901782b2009-07-10 08:18:50 +01001410/**
1411 * i915_gem_release_mmap - remove physical page mappings
1412 * @obj: obj in question
1413 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001414 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01001415 * relinquish ownership of the pages back to the system.
1416 *
1417 * It is vital that we remove the page mapping if we have mapped a tiled
1418 * object through the GTT and then lose the fence register due to
1419 * resource pressure. Similarly if the object has been moved out of the
1420 * aperture, than pages mapped into userspace must be revoked. Removing the
1421 * mapping will then trigger a page fault on the next user access, allowing
1422 * fixup by i915_gem_fault().
1423 */
Eric Anholtd05ca302009-07-10 13:02:26 -07001424void
Chris Wilson901782b2009-07-10 08:18:50 +01001425i915_gem_release_mmap(struct drm_gem_object *obj)
1426{
1427 struct drm_device *dev = obj->dev;
Daniel Vetterfb7d5162010-10-01 22:05:20 +02001428 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001429 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson901782b2009-07-10 08:18:50 +01001430
Chris Wilson39a01d12010-10-28 13:03:06 +01001431 if (unlikely(obj->map_list.map && dev->dev_mapping))
Chris Wilson901782b2009-07-10 08:18:50 +01001432 unmap_mapping_range(dev->dev_mapping,
Chris Wilson39a01d12010-10-28 13:03:06 +01001433 (loff_t)obj->map_list.hash.key<<PAGE_SHIFT,
1434 obj->size, 1);
Daniel Vetterfb7d5162010-10-01 22:05:20 +02001435
1436 if (obj_priv->fault_mappable) {
1437 obj_priv->fault_mappable = false;
Chris Wilsona00b10c2010-09-24 21:15:47 +01001438 i915_gem_info_update_mappable(dev_priv, obj_priv, false);
Daniel Vetterfb7d5162010-10-01 22:05:20 +02001439 }
Chris Wilson901782b2009-07-10 08:18:50 +01001440}
1441
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001442static void
1443i915_gem_free_mmap_offset(struct drm_gem_object *obj)
1444{
1445 struct drm_device *dev = obj->dev;
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001446 struct drm_gem_mm *mm = dev->mm_private;
Chris Wilson39a01d12010-10-28 13:03:06 +01001447 struct drm_map_list *list = &obj->map_list;
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001448
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001449 drm_ht_remove_item(&mm->offset_hash, &list->hash);
Chris Wilson39a01d12010-10-28 13:03:06 +01001450 drm_mm_put_block(list->file_offset_node);
1451 kfree(list->map);
1452 list->map = NULL;
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001453}
1454
Jesse Barnesde151cf2008-11-12 10:03:55 -08001455/**
1456 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1457 * @obj: object to check
1458 *
1459 * Return the required GTT alignment for an object, taking into account
Daniel Vetter5e783302010-11-14 22:32:36 +01001460 * potential fence register mapping.
Jesse Barnesde151cf2008-11-12 10:03:55 -08001461 */
1462static uint32_t
Chris Wilsona00b10c2010-09-24 21:15:47 +01001463i915_gem_get_gtt_alignment(struct drm_i915_gem_object *obj_priv)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001464{
Chris Wilsona00b10c2010-09-24 21:15:47 +01001465 struct drm_device *dev = obj_priv->base.dev;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001466
1467 /*
1468 * Minimum alignment is 4k (GTT page size), but might be greater
1469 * if a fence register is needed for the object.
1470 */
Chris Wilsona00b10c2010-09-24 21:15:47 +01001471 if (INTEL_INFO(dev)->gen >= 4 ||
1472 obj_priv->tiling_mode == I915_TILING_NONE)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001473 return 4096;
1474
1475 /*
1476 * Previous chips need to be aligned to the size of the smallest
1477 * fence register that can contain the object.
1478 */
Chris Wilsona00b10c2010-09-24 21:15:47 +01001479 return i915_gem_get_gtt_size(obj_priv);
1480}
1481
Daniel Vetter5e783302010-11-14 22:32:36 +01001482/**
1483 * i915_gem_get_unfenced_gtt_alignment - return required GTT alignment for an
1484 * unfenced object
1485 * @obj: object to check
1486 *
1487 * Return the required GTT alignment for an object, only taking into account
1488 * unfenced tiled surface requirements.
1489 */
1490static uint32_t
1491i915_gem_get_unfenced_gtt_alignment(struct drm_i915_gem_object *obj_priv)
1492{
1493 struct drm_device *dev = obj_priv->base.dev;
1494 int tile_height;
1495
1496 /*
1497 * Minimum alignment is 4k (GTT page size) for sane hw.
1498 */
1499 if (INTEL_INFO(dev)->gen >= 4 || IS_G33(dev) ||
1500 obj_priv->tiling_mode == I915_TILING_NONE)
1501 return 4096;
1502
1503 /*
1504 * Older chips need unfenced tiled buffers to be aligned to the left
1505 * edge of an even tile row (where tile rows are counted as if the bo is
1506 * placed in a fenced gtt region).
1507 */
1508 if (IS_GEN2(dev) ||
1509 (obj_priv->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev)))
1510 tile_height = 32;
1511 else
1512 tile_height = 8;
1513
1514 return tile_height * obj_priv->stride * 2;
1515}
1516
Chris Wilsona00b10c2010-09-24 21:15:47 +01001517static uint32_t
1518i915_gem_get_gtt_size(struct drm_i915_gem_object *obj_priv)
1519{
1520 struct drm_device *dev = obj_priv->base.dev;
1521 uint32_t size;
1522
1523 /*
1524 * Minimum alignment is 4k (GTT page size), but might be greater
1525 * if a fence register is needed for the object.
1526 */
1527 if (INTEL_INFO(dev)->gen >= 4)
1528 return obj_priv->base.size;
1529
1530 /*
1531 * Previous chips need to be aligned to the size of the smallest
1532 * fence register that can contain the object.
1533 */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001534 if (INTEL_INFO(dev)->gen == 3)
Chris Wilsona00b10c2010-09-24 21:15:47 +01001535 size = 1024*1024;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001536 else
Chris Wilsona00b10c2010-09-24 21:15:47 +01001537 size = 512*1024;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001538
Chris Wilsona00b10c2010-09-24 21:15:47 +01001539 while (size < obj_priv->base.size)
1540 size <<= 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001541
Chris Wilsona00b10c2010-09-24 21:15:47 +01001542 return size;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001543}
1544
1545/**
1546 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1547 * @dev: DRM device
1548 * @data: GTT mapping ioctl data
1549 * @file_priv: GEM object info
1550 *
1551 * Simply returns the fake offset to userspace so it can mmap it.
1552 * The mmap call will end up in drm_gem_mmap(), which will set things
1553 * up so we can get faults in the handler above.
1554 *
1555 * The fault handler will take care of binding the object into the GTT
1556 * (since it may have been evicted to make room for something), allocating
1557 * a fence register, and mapping the appropriate aperture address into
1558 * userspace.
1559 */
1560int
1561i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
1562 struct drm_file *file_priv)
1563{
Chris Wilsonda761a62010-10-27 17:37:08 +01001564 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001565 struct drm_i915_gem_mmap_gtt *args = data;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001566 struct drm_gem_object *obj;
1567 struct drm_i915_gem_object *obj_priv;
1568 int ret;
1569
1570 if (!(dev->driver->driver_features & DRIVER_GEM))
1571 return -ENODEV;
1572
Chris Wilson76c1dec2010-09-25 11:22:51 +01001573 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001574 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001575 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001576
Jesse Barnesde151cf2008-11-12 10:03:55 -08001577 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001578 if (obj == NULL) {
1579 ret = -ENOENT;
1580 goto unlock;
1581 }
Daniel Vetter23010e42010-03-08 13:35:02 +01001582 obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001583
Chris Wilsonda761a62010-10-27 17:37:08 +01001584 if (obj->size > dev_priv->mm.gtt_mappable_end) {
1585 ret = -E2BIG;
1586 goto unlock;
1587 }
1588
Chris Wilsonab182822009-09-22 18:46:17 +01001589 if (obj_priv->madv != I915_MADV_WILLNEED) {
1590 DRM_ERROR("Attempting to mmap a purgeable buffer\n");
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001591 ret = -EINVAL;
1592 goto out;
Chris Wilsonab182822009-09-22 18:46:17 +01001593 }
1594
Chris Wilson39a01d12010-10-28 13:03:06 +01001595 if (!obj->map_list.map) {
Jesse Barnesde151cf2008-11-12 10:03:55 -08001596 ret = i915_gem_create_mmap_offset(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001597 if (ret)
1598 goto out;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001599 }
1600
Chris Wilson39a01d12010-10-28 13:03:06 +01001601 args->offset = (u64)obj->map_list.hash.key << PAGE_SHIFT;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001602
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001603out:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001604 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001605unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001606 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001607 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001608}
1609
Chris Wilsone5281cc2010-10-28 13:45:36 +01001610static int
1611i915_gem_object_get_pages_gtt(struct drm_gem_object *obj,
1612 gfp_t gfpmask)
1613{
1614 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
1615 int page_count, i;
1616 struct address_space *mapping;
1617 struct inode *inode;
1618 struct page *page;
1619
1620 /* Get the list of pages out of our struct file. They'll be pinned
1621 * at this point until we release them.
1622 */
1623 page_count = obj->size / PAGE_SIZE;
1624 BUG_ON(obj_priv->pages != NULL);
1625 obj_priv->pages = drm_malloc_ab(page_count, sizeof(struct page *));
1626 if (obj_priv->pages == NULL)
1627 return -ENOMEM;
1628
1629 inode = obj->filp->f_path.dentry->d_inode;
1630 mapping = inode->i_mapping;
1631 for (i = 0; i < page_count; i++) {
1632 page = read_cache_page_gfp(mapping, i,
1633 GFP_HIGHUSER |
1634 __GFP_COLD |
1635 __GFP_RECLAIMABLE |
1636 gfpmask);
1637 if (IS_ERR(page))
1638 goto err_pages;
1639
1640 obj_priv->pages[i] = page;
1641 }
1642
1643 if (obj_priv->tiling_mode != I915_TILING_NONE)
1644 i915_gem_object_do_bit_17_swizzle(obj);
1645
1646 return 0;
1647
1648err_pages:
1649 while (i--)
1650 page_cache_release(obj_priv->pages[i]);
1651
1652 drm_free_large(obj_priv->pages);
1653 obj_priv->pages = NULL;
1654 return PTR_ERR(page);
1655}
1656
Chris Wilson5cdf5882010-09-27 15:51:07 +01001657static void
Chris Wilsone5281cc2010-10-28 13:45:36 +01001658i915_gem_object_put_pages_gtt(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07001659{
Daniel Vetter23010e42010-03-08 13:35:02 +01001660 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001661 int page_count = obj->size / PAGE_SIZE;
1662 int i;
1663
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001664 BUG_ON(obj_priv->madv == __I915_MADV_PURGED);
Eric Anholt856fa192009-03-19 14:10:50 -07001665
Eric Anholt280b7132009-03-12 16:56:27 -07001666 if (obj_priv->tiling_mode != I915_TILING_NONE)
1667 i915_gem_object_save_bit_17_swizzle(obj);
1668
Chris Wilson3ef94da2009-09-14 16:50:29 +01001669 if (obj_priv->madv == I915_MADV_DONTNEED)
Chris Wilson13a05fd2009-09-20 23:03:19 +01001670 obj_priv->dirty = 0;
Chris Wilson3ef94da2009-09-14 16:50:29 +01001671
1672 for (i = 0; i < page_count; i++) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01001673 if (obj_priv->dirty)
1674 set_page_dirty(obj_priv->pages[i]);
1675
1676 if (obj_priv->madv == I915_MADV_WILLNEED)
Eric Anholt856fa192009-03-19 14:10:50 -07001677 mark_page_accessed(obj_priv->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001678
1679 page_cache_release(obj_priv->pages[i]);
1680 }
Eric Anholt673a3942008-07-30 12:06:12 -07001681 obj_priv->dirty = 0;
1682
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07001683 drm_free_large(obj_priv->pages);
Eric Anholt856fa192009-03-19 14:10:50 -07001684 obj_priv->pages = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001685}
1686
Chris Wilsona56ba562010-09-28 10:07:56 +01001687static uint32_t
1688i915_gem_next_request_seqno(struct drm_device *dev,
1689 struct intel_ring_buffer *ring)
1690{
1691 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson5d97eb62010-11-10 20:40:02 +00001692 return ring->outstanding_lazy_request = dev_priv->next_seqno;
Chris Wilsona56ba562010-09-28 10:07:56 +01001693}
1694
Eric Anholt673a3942008-07-30 12:06:12 -07001695static void
Daniel Vetter617dbe22010-02-11 22:16:02 +01001696i915_gem_object_move_to_active(struct drm_gem_object *obj,
Zou Nan hai852835f2010-05-21 09:08:56 +08001697 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001698{
1699 struct drm_device *dev = obj->dev;
Chris Wilson69dc4982010-10-19 10:36:51 +01001700 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001701 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsona56ba562010-09-28 10:07:56 +01001702 uint32_t seqno = i915_gem_next_request_seqno(dev, ring);
Daniel Vetter617dbe22010-02-11 22:16:02 +01001703
Zou Nan hai852835f2010-05-21 09:08:56 +08001704 BUG_ON(ring == NULL);
1705 obj_priv->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001706
1707 /* Add a reference if we're newly entering the active list. */
1708 if (!obj_priv->active) {
1709 drm_gem_object_reference(obj);
1710 obj_priv->active = 1;
1711 }
Daniel Vettere35a41d2010-02-11 22:13:59 +01001712
Eric Anholt673a3942008-07-30 12:06:12 -07001713 /* Move from whatever list we were on to the tail of execution. */
Chris Wilson69dc4982010-10-19 10:36:51 +01001714 list_move_tail(&obj_priv->mm_list, &dev_priv->mm.active_list);
1715 list_move_tail(&obj_priv->ring_list, &ring->active_list);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001716 obj_priv->last_rendering_seqno = seqno;
Eric Anholt673a3942008-07-30 12:06:12 -07001717}
1718
Eric Anholtce44b0e2008-11-06 16:00:31 -08001719static void
1720i915_gem_object_move_to_flushing(struct drm_gem_object *obj)
1721{
1722 struct drm_device *dev = obj->dev;
1723 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001724 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001725
1726 BUG_ON(!obj_priv->active);
Chris Wilson69dc4982010-10-19 10:36:51 +01001727 list_move_tail(&obj_priv->mm_list, &dev_priv->mm.flushing_list);
1728 list_del_init(&obj_priv->ring_list);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001729 obj_priv->last_rendering_seqno = 0;
1730}
Eric Anholt673a3942008-07-30 12:06:12 -07001731
Chris Wilson963b4832009-09-20 23:03:54 +01001732/* Immediately discard the backing storage */
1733static void
1734i915_gem_object_truncate(struct drm_gem_object *obj)
1735{
Daniel Vetter23010e42010-03-08 13:35:02 +01001736 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001737 struct inode *inode;
Chris Wilson963b4832009-09-20 23:03:54 +01001738
Chris Wilsonae9fed62010-08-07 11:01:30 +01001739 /* Our goal here is to return as much of the memory as
1740 * is possible back to the system as we are called from OOM.
1741 * To do this we must instruct the shmfs to drop all of its
1742 * backing pages, *now*. Here we mirror the actions taken
1743 * when by shmem_delete_inode() to release the backing store.
1744 */
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001745 inode = obj->filp->f_path.dentry->d_inode;
Chris Wilsonae9fed62010-08-07 11:01:30 +01001746 truncate_inode_pages(inode->i_mapping, 0);
1747 if (inode->i_op->truncate_range)
1748 inode->i_op->truncate_range(inode, 0, (loff_t)-1);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001749
1750 obj_priv->madv = __I915_MADV_PURGED;
Chris Wilson963b4832009-09-20 23:03:54 +01001751}
1752
1753static inline int
1754i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj_priv)
1755{
1756 return obj_priv->madv == I915_MADV_DONTNEED;
1757}
1758
Eric Anholt673a3942008-07-30 12:06:12 -07001759static void
1760i915_gem_object_move_to_inactive(struct drm_gem_object *obj)
1761{
1762 struct drm_device *dev = obj->dev;
1763 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001764 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001765
Eric Anholt673a3942008-07-30 12:06:12 -07001766 if (obj_priv->pin_count != 0)
Chris Wilson69dc4982010-10-19 10:36:51 +01001767 list_move_tail(&obj_priv->mm_list, &dev_priv->mm.pinned_list);
Eric Anholt673a3942008-07-30 12:06:12 -07001768 else
Chris Wilson69dc4982010-10-19 10:36:51 +01001769 list_move_tail(&obj_priv->mm_list, &dev_priv->mm.inactive_list);
1770 list_del_init(&obj_priv->ring_list);
Eric Anholt673a3942008-07-30 12:06:12 -07001771
Daniel Vetter99fcb762010-02-07 16:20:18 +01001772 BUG_ON(!list_empty(&obj_priv->gpu_write_list));
1773
Eric Anholtce44b0e2008-11-06 16:00:31 -08001774 obj_priv->last_rendering_seqno = 0;
Zou Nan hai852835f2010-05-21 09:08:56 +08001775 obj_priv->ring = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001776 if (obj_priv->active) {
1777 obj_priv->active = 0;
1778 drm_gem_object_unreference(obj);
1779 }
Chris Wilson23bc5982010-09-29 16:10:57 +01001780 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07001781}
1782
Daniel Vetter63560392010-02-19 11:51:59 +01001783static void
1784i915_gem_process_flushing_list(struct drm_device *dev,
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001785 uint32_t flush_domains,
Zou Nan hai852835f2010-05-21 09:08:56 +08001786 struct intel_ring_buffer *ring)
Daniel Vetter63560392010-02-19 11:51:59 +01001787{
1788 drm_i915_private_t *dev_priv = dev->dev_private;
1789 struct drm_i915_gem_object *obj_priv, *next;
1790
1791 list_for_each_entry_safe(obj_priv, next,
Chris Wilson64193402010-10-24 12:38:05 +01001792 &ring->gpu_write_list,
Daniel Vetter63560392010-02-19 11:51:59 +01001793 gpu_write_list) {
Daniel Vettera8089e82010-04-09 19:05:09 +00001794 struct drm_gem_object *obj = &obj_priv->base;
Daniel Vetter63560392010-02-19 11:51:59 +01001795
Chris Wilson64193402010-10-24 12:38:05 +01001796 if (obj->write_domain & flush_domains) {
Daniel Vetter63560392010-02-19 11:51:59 +01001797 uint32_t old_write_domain = obj->write_domain;
1798
1799 obj->write_domain = 0;
1800 list_del_init(&obj_priv->gpu_write_list);
Daniel Vetter617dbe22010-02-11 22:16:02 +01001801 i915_gem_object_move_to_active(obj, ring);
Daniel Vetter63560392010-02-19 11:51:59 +01001802
1803 /* update the fence lru list */
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001804 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
1805 struct drm_i915_fence_reg *reg =
1806 &dev_priv->fence_regs[obj_priv->fence_reg];
1807 list_move_tail(&reg->lru_list,
Daniel Vetter63560392010-02-19 11:51:59 +01001808 &dev_priv->mm.fence_list);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001809 }
Daniel Vetter63560392010-02-19 11:51:59 +01001810
1811 trace_i915_gem_object_change_domain(obj,
1812 obj->read_domains,
1813 old_write_domain);
1814 }
1815 }
1816}
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001817
Chris Wilson3cce4692010-10-27 16:11:02 +01001818int
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001819i915_add_request(struct drm_device *dev,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001820 struct drm_file *file,
Chris Wilson8dc5d142010-08-12 12:36:12 +01001821 struct drm_i915_gem_request *request,
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001822 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001823{
1824 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001825 struct drm_i915_file_private *file_priv = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001826 uint32_t seqno;
1827 int was_empty;
Chris Wilson3cce4692010-10-27 16:11:02 +01001828 int ret;
1829
1830 BUG_ON(request == NULL);
Eric Anholt673a3942008-07-30 12:06:12 -07001831
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001832 if (file != NULL)
1833 file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00001834
Chris Wilson3cce4692010-10-27 16:11:02 +01001835 ret = ring->add_request(ring, &seqno);
1836 if (ret)
1837 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001838
Chris Wilsona56ba562010-09-28 10:07:56 +01001839 ring->outstanding_lazy_request = false;
Eric Anholt673a3942008-07-30 12:06:12 -07001840
1841 request->seqno = seqno;
Zou Nan hai852835f2010-05-21 09:08:56 +08001842 request->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001843 request->emitted_jiffies = jiffies;
Zou Nan hai852835f2010-05-21 09:08:56 +08001844 was_empty = list_empty(&ring->request_list);
1845 list_add_tail(&request->list, &ring->request_list);
1846
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001847 if (file_priv) {
Chris Wilson1c255952010-09-26 11:03:27 +01001848 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001849 request->file_priv = file_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00001850 list_add_tail(&request->client_list,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001851 &file_priv->mm.request_list);
Chris Wilson1c255952010-09-26 11:03:27 +01001852 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00001853 }
Eric Anholt673a3942008-07-30 12:06:12 -07001854
Ben Gamarif65d9422009-09-14 17:48:44 -04001855 if (!dev_priv->mm.suspended) {
Chris Wilsonb3b079d2010-09-13 23:44:34 +01001856 mod_timer(&dev_priv->hangcheck_timer,
1857 jiffies + msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
Ben Gamarif65d9422009-09-14 17:48:44 -04001858 if (was_empty)
Chris Wilsonb3b079d2010-09-13 23:44:34 +01001859 queue_delayed_work(dev_priv->wq,
1860 &dev_priv->mm.retire_work, HZ);
Ben Gamarif65d9422009-09-14 17:48:44 -04001861 }
Chris Wilson3cce4692010-10-27 16:11:02 +01001862 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001863}
1864
1865/**
1866 * Command execution barrier
1867 *
1868 * Ensures that all commands in the ring are finished
1869 * before signalling the CPU
1870 */
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001871static void
Zou Nan hai852835f2010-05-21 09:08:56 +08001872i915_retire_commands(struct drm_device *dev, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001873{
Eric Anholt673a3942008-07-30 12:06:12 -07001874 uint32_t flush_domains = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001875
1876 /* The sampler always gets flushed on i965 (sigh) */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001877 if (INTEL_INFO(dev)->gen >= 4)
Eric Anholt673a3942008-07-30 12:06:12 -07001878 flush_domains |= I915_GEM_DOMAIN_SAMPLER;
Zou Nan hai852835f2010-05-21 09:08:56 +08001879
Chris Wilson78501ea2010-10-27 12:18:21 +01001880 ring->flush(ring, I915_GEM_DOMAIN_COMMAND, flush_domains);
Eric Anholt673a3942008-07-30 12:06:12 -07001881}
1882
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001883static inline void
1884i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
Eric Anholt673a3942008-07-30 12:06:12 -07001885{
Chris Wilson1c255952010-09-26 11:03:27 +01001886 struct drm_i915_file_private *file_priv = request->file_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07001887
Chris Wilson1c255952010-09-26 11:03:27 +01001888 if (!file_priv)
1889 return;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001890
Chris Wilson1c255952010-09-26 11:03:27 +01001891 spin_lock(&file_priv->mm.lock);
1892 list_del(&request->client_list);
1893 request->file_priv = NULL;
1894 spin_unlock(&file_priv->mm.lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001895}
1896
Chris Wilsondfaae392010-09-22 10:31:52 +01001897static void i915_gem_reset_ring_lists(struct drm_i915_private *dev_priv,
1898 struct intel_ring_buffer *ring)
Chris Wilson9375e442010-09-19 12:21:28 +01001899{
Chris Wilsondfaae392010-09-22 10:31:52 +01001900 while (!list_empty(&ring->request_list)) {
1901 struct drm_i915_gem_request *request;
Chris Wilson9375e442010-09-19 12:21:28 +01001902
Chris Wilsondfaae392010-09-22 10:31:52 +01001903 request = list_first_entry(&ring->request_list,
1904 struct drm_i915_gem_request,
1905 list);
1906
1907 list_del(&request->list);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001908 i915_gem_request_remove_from_client(request);
Chris Wilsondfaae392010-09-22 10:31:52 +01001909 kfree(request);
1910 }
1911
1912 while (!list_empty(&ring->active_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001913 struct drm_i915_gem_object *obj_priv;
1914
Chris Wilsondfaae392010-09-22 10:31:52 +01001915 obj_priv = list_first_entry(&ring->active_list,
Eric Anholt673a3942008-07-30 12:06:12 -07001916 struct drm_i915_gem_object,
Chris Wilson69dc4982010-10-19 10:36:51 +01001917 ring_list);
Eric Anholt673a3942008-07-30 12:06:12 -07001918
Chris Wilsondfaae392010-09-22 10:31:52 +01001919 obj_priv->base.write_domain = 0;
1920 list_del_init(&obj_priv->gpu_write_list);
1921 i915_gem_object_move_to_inactive(&obj_priv->base);
Eric Anholt673a3942008-07-30 12:06:12 -07001922 }
Eric Anholt673a3942008-07-30 12:06:12 -07001923}
1924
Chris Wilson069efc12010-09-30 16:53:18 +01001925void i915_gem_reset(struct drm_device *dev)
Eric Anholt673a3942008-07-30 12:06:12 -07001926{
Chris Wilsondfaae392010-09-22 10:31:52 +01001927 struct drm_i915_private *dev_priv = dev->dev_private;
1928 struct drm_i915_gem_object *obj_priv;
Chris Wilson069efc12010-09-30 16:53:18 +01001929 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07001930
Chris Wilsondfaae392010-09-22 10:31:52 +01001931 i915_gem_reset_ring_lists(dev_priv, &dev_priv->render_ring);
Chris Wilson87acb0a2010-10-19 10:13:00 +01001932 i915_gem_reset_ring_lists(dev_priv, &dev_priv->bsd_ring);
Chris Wilson549f7362010-10-19 11:19:32 +01001933 i915_gem_reset_ring_lists(dev_priv, &dev_priv->blt_ring);
Chris Wilsondfaae392010-09-22 10:31:52 +01001934
1935 /* Remove anything from the flushing lists. The GPU cache is likely
1936 * to be lost on reset along with the data, so simply move the
1937 * lost bo to the inactive list.
1938 */
1939 while (!list_empty(&dev_priv->mm.flushing_list)) {
Chris Wilson9375e442010-09-19 12:21:28 +01001940 obj_priv = list_first_entry(&dev_priv->mm.flushing_list,
1941 struct drm_i915_gem_object,
Chris Wilson69dc4982010-10-19 10:36:51 +01001942 mm_list);
Chris Wilson9375e442010-09-19 12:21:28 +01001943
1944 obj_priv->base.write_domain = 0;
Chris Wilsondfaae392010-09-22 10:31:52 +01001945 list_del_init(&obj_priv->gpu_write_list);
Chris Wilson9375e442010-09-19 12:21:28 +01001946 i915_gem_object_move_to_inactive(&obj_priv->base);
1947 }
Chris Wilson9375e442010-09-19 12:21:28 +01001948
Chris Wilsondfaae392010-09-22 10:31:52 +01001949 /* Move everything out of the GPU domains to ensure we do any
1950 * necessary invalidation upon reuse.
1951 */
Chris Wilson77f01232010-09-19 12:31:36 +01001952 list_for_each_entry(obj_priv,
1953 &dev_priv->mm.inactive_list,
Chris Wilson69dc4982010-10-19 10:36:51 +01001954 mm_list)
Chris Wilson77f01232010-09-19 12:31:36 +01001955 {
1956 obj_priv->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
1957 }
Chris Wilson069efc12010-09-30 16:53:18 +01001958
1959 /* The fence registers are invalidated so clear them out */
1960 for (i = 0; i < 16; i++) {
1961 struct drm_i915_fence_reg *reg;
1962
1963 reg = &dev_priv->fence_regs[i];
1964 if (!reg->obj)
1965 continue;
1966
1967 i915_gem_clear_fence_reg(reg->obj);
1968 }
Eric Anholt673a3942008-07-30 12:06:12 -07001969}
1970
1971/**
1972 * This function clears the request list as sequence numbers are passed.
1973 */
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001974static void
1975i915_gem_retire_requests_ring(struct drm_device *dev,
1976 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001977{
1978 drm_i915_private_t *dev_priv = dev->dev_private;
1979 uint32_t seqno;
1980
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001981 if (!ring->status_page.page_addr ||
1982 list_empty(&ring->request_list))
Karsten Wiese6c0594a2009-02-23 15:07:57 +01001983 return;
1984
Chris Wilson23bc5982010-09-29 16:10:57 +01001985 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07001986
Chris Wilson78501ea2010-10-27 12:18:21 +01001987 seqno = ring->get_seqno(ring);
Zou Nan hai852835f2010-05-21 09:08:56 +08001988 while (!list_empty(&ring->request_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001989 struct drm_i915_gem_request *request;
Eric Anholt673a3942008-07-30 12:06:12 -07001990
Zou Nan hai852835f2010-05-21 09:08:56 +08001991 request = list_first_entry(&ring->request_list,
Eric Anholt673a3942008-07-30 12:06:12 -07001992 struct drm_i915_gem_request,
1993 list);
Eric Anholt673a3942008-07-30 12:06:12 -07001994
Chris Wilsondfaae392010-09-22 10:31:52 +01001995 if (!i915_seqno_passed(seqno, request->seqno))
Eric Anholt673a3942008-07-30 12:06:12 -07001996 break;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001997
1998 trace_i915_gem_request_retire(dev, request->seqno);
1999
2000 list_del(&request->list);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01002001 i915_gem_request_remove_from_client(request);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01002002 kfree(request);
2003 }
2004
2005 /* Move any buffers on the active list that are no longer referenced
2006 * by the ringbuffer to the flushing/inactive lists as appropriate.
2007 */
2008 while (!list_empty(&ring->active_list)) {
2009 struct drm_gem_object *obj;
2010 struct drm_i915_gem_object *obj_priv;
2011
2012 obj_priv = list_first_entry(&ring->active_list,
2013 struct drm_i915_gem_object,
Chris Wilson69dc4982010-10-19 10:36:51 +01002014 ring_list);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01002015
Chris Wilsondfaae392010-09-22 10:31:52 +01002016 if (!i915_seqno_passed(seqno, obj_priv->last_rendering_seqno))
Chris Wilsonb84d5f02010-09-18 01:38:04 +01002017 break;
2018
2019 obj = &obj_priv->base;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01002020 if (obj->write_domain != 0)
2021 i915_gem_object_move_to_flushing(obj);
2022 else
2023 i915_gem_object_move_to_inactive(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002024 }
Chris Wilson9d34e5d2009-09-24 05:26:06 +01002025
2026 if (unlikely (dev_priv->trace_irq_seqno &&
2027 i915_seqno_passed(dev_priv->trace_irq_seqno, seqno))) {
Chris Wilson78501ea2010-10-27 12:18:21 +01002028 ring->user_irq_put(ring);
Chris Wilson9d34e5d2009-09-24 05:26:06 +01002029 dev_priv->trace_irq_seqno = 0;
2030 }
Chris Wilson23bc5982010-09-29 16:10:57 +01002031
2032 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07002033}
2034
2035void
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002036i915_gem_retire_requests(struct drm_device *dev)
2037{
2038 drm_i915_private_t *dev_priv = dev->dev_private;
2039
Chris Wilsonbe726152010-07-23 23:18:50 +01002040 if (!list_empty(&dev_priv->mm.deferred_free_list)) {
2041 struct drm_i915_gem_object *obj_priv, *tmp;
2042
2043 /* We must be careful that during unbind() we do not
2044 * accidentally infinitely recurse into retire requests.
2045 * Currently:
2046 * retire -> free -> unbind -> wait -> retire_ring
2047 */
2048 list_for_each_entry_safe(obj_priv, tmp,
2049 &dev_priv->mm.deferred_free_list,
Chris Wilson69dc4982010-10-19 10:36:51 +01002050 mm_list)
Chris Wilsonbe726152010-07-23 23:18:50 +01002051 i915_gem_free_object_tail(&obj_priv->base);
2052 }
2053
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002054 i915_gem_retire_requests_ring(dev, &dev_priv->render_ring);
Chris Wilson87acb0a2010-10-19 10:13:00 +01002055 i915_gem_retire_requests_ring(dev, &dev_priv->bsd_ring);
Chris Wilson549f7362010-10-19 11:19:32 +01002056 i915_gem_retire_requests_ring(dev, &dev_priv->blt_ring);
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002057}
2058
Daniel Vetter75ef9da2010-08-21 00:25:16 +02002059static void
Eric Anholt673a3942008-07-30 12:06:12 -07002060i915_gem_retire_work_handler(struct work_struct *work)
2061{
2062 drm_i915_private_t *dev_priv;
2063 struct drm_device *dev;
2064
2065 dev_priv = container_of(work, drm_i915_private_t,
2066 mm.retire_work.work);
2067 dev = dev_priv->dev;
2068
Chris Wilson891b48c2010-09-29 12:26:37 +01002069 /* Come back later if the device is busy... */
2070 if (!mutex_trylock(&dev->struct_mutex)) {
2071 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
2072 return;
2073 }
2074
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002075 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08002076
Keith Packard6dbe2772008-10-14 21:41:13 -07002077 if (!dev_priv->mm.suspended &&
Zou Nan haid1b851f2010-05-21 09:08:57 +08002078 (!list_empty(&dev_priv->render_ring.request_list) ||
Chris Wilson549f7362010-10-19 11:19:32 +01002079 !list_empty(&dev_priv->bsd_ring.request_list) ||
2080 !list_empty(&dev_priv->blt_ring.request_list)))
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07002081 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
Eric Anholt673a3942008-07-30 12:06:12 -07002082 mutex_unlock(&dev->struct_mutex);
2083}
2084
Daniel Vetter5a5a0c62009-09-15 22:57:36 +02002085int
Zou Nan hai852835f2010-05-21 09:08:56 +08002086i915_do_wait_request(struct drm_device *dev, uint32_t seqno,
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002087 bool interruptible, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07002088{
2089 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes802c7eb2009-05-05 16:03:48 -07002090 u32 ier;
Eric Anholt673a3942008-07-30 12:06:12 -07002091 int ret = 0;
2092
2093 BUG_ON(seqno == 0);
2094
Ben Gamariba1234d2009-09-14 17:48:47 -04002095 if (atomic_read(&dev_priv->mm.wedged))
Chris Wilson30dbf0c2010-09-25 10:19:17 +01002096 return -EAGAIN;
Ben Gamariffed1d02009-09-14 17:48:41 -04002097
Chris Wilson5d97eb62010-11-10 20:40:02 +00002098 if (seqno == ring->outstanding_lazy_request) {
Chris Wilson3cce4692010-10-27 16:11:02 +01002099 struct drm_i915_gem_request *request;
2100
2101 request = kzalloc(sizeof(*request), GFP_KERNEL);
2102 if (request == NULL)
Daniel Vettere35a41d2010-02-11 22:13:59 +01002103 return -ENOMEM;
Chris Wilson3cce4692010-10-27 16:11:02 +01002104
2105 ret = i915_add_request(dev, NULL, request, ring);
2106 if (ret) {
2107 kfree(request);
2108 return ret;
2109 }
2110
2111 seqno = request->seqno;
Daniel Vettere35a41d2010-02-11 22:13:59 +01002112 }
2113
Chris Wilson78501ea2010-10-27 12:18:21 +01002114 if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) {
Eric Anholtbad720f2009-10-22 16:11:14 -07002115 if (HAS_PCH_SPLIT(dev))
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002116 ier = I915_READ(DEIER) | I915_READ(GTIER);
2117 else
2118 ier = I915_READ(IER);
Jesse Barnes802c7eb2009-05-05 16:03:48 -07002119 if (!ier) {
2120 DRM_ERROR("something (likely vbetool) disabled "
2121 "interrupts, re-enabling\n");
2122 i915_driver_irq_preinstall(dev);
2123 i915_driver_irq_postinstall(dev);
2124 }
2125
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002126 trace_i915_gem_request_wait_begin(dev, seqno);
2127
Chris Wilsonb2223492010-10-27 15:27:33 +01002128 ring->waiting_seqno = seqno;
Chris Wilson78501ea2010-10-27 12:18:21 +01002129 ring->user_irq_get(ring);
Daniel Vetter48764bf2009-09-15 22:57:32 +02002130 if (interruptible)
Zou Nan hai852835f2010-05-21 09:08:56 +08002131 ret = wait_event_interruptible(ring->irq_queue,
Chris Wilson78501ea2010-10-27 12:18:21 +01002132 i915_seqno_passed(ring->get_seqno(ring), seqno)
Zou Nan hai852835f2010-05-21 09:08:56 +08002133 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02002134 else
Zou Nan hai852835f2010-05-21 09:08:56 +08002135 wait_event(ring->irq_queue,
Chris Wilson78501ea2010-10-27 12:18:21 +01002136 i915_seqno_passed(ring->get_seqno(ring), seqno)
Zou Nan hai852835f2010-05-21 09:08:56 +08002137 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02002138
Chris Wilson78501ea2010-10-27 12:18:21 +01002139 ring->user_irq_put(ring);
Chris Wilsonb2223492010-10-27 15:27:33 +01002140 ring->waiting_seqno = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002141
2142 trace_i915_gem_request_wait_end(dev, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07002143 }
Ben Gamariba1234d2009-09-14 17:48:47 -04002144 if (atomic_read(&dev_priv->mm.wedged))
Chris Wilson30dbf0c2010-09-25 10:19:17 +01002145 ret = -EAGAIN;
Eric Anholt673a3942008-07-30 12:06:12 -07002146
2147 if (ret && ret != -ERESTARTSYS)
Daniel Vetter8bff9172010-02-11 22:19:40 +01002148 DRM_ERROR("%s returns %d (awaiting %d at %d, next %d)\n",
Chris Wilson78501ea2010-10-27 12:18:21 +01002149 __func__, ret, seqno, ring->get_seqno(ring),
Daniel Vetter8bff9172010-02-11 22:19:40 +01002150 dev_priv->next_seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07002151
2152 /* Directly dispatch request retiring. While we have the work queue
2153 * to handle this, the waiter on a request often wants an associated
2154 * buffer to have made it to the inactive list, and we would need
2155 * a separate wait queue to handle that.
2156 */
2157 if (ret == 0)
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002158 i915_gem_retire_requests_ring(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07002159
2160 return ret;
2161}
2162
Daniel Vetter48764bf2009-09-15 22:57:32 +02002163/**
2164 * Waits for a sequence number to be signaled, and cleans up the
2165 * request and object lists appropriately for that event.
2166 */
2167static int
Zou Nan hai852835f2010-05-21 09:08:56 +08002168i915_wait_request(struct drm_device *dev, uint32_t seqno,
Chris Wilsona56ba562010-09-28 10:07:56 +01002169 struct intel_ring_buffer *ring)
Daniel Vetter48764bf2009-09-15 22:57:32 +02002170{
Zou Nan hai852835f2010-05-21 09:08:56 +08002171 return i915_do_wait_request(dev, seqno, 1, ring);
Daniel Vetter48764bf2009-09-15 22:57:32 +02002172}
2173
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002174static void
Chris Wilson92204342010-09-18 11:02:01 +01002175i915_gem_flush_ring(struct drm_device *dev,
Chris Wilsonc78ec302010-09-20 12:50:23 +01002176 struct drm_file *file_priv,
Chris Wilson92204342010-09-18 11:02:01 +01002177 struct intel_ring_buffer *ring,
2178 uint32_t invalidate_domains,
2179 uint32_t flush_domains)
2180{
Chris Wilson78501ea2010-10-27 12:18:21 +01002181 ring->flush(ring, invalidate_domains, flush_domains);
Chris Wilson92204342010-09-18 11:02:01 +01002182 i915_gem_process_flushing_list(dev, flush_domains, ring);
2183}
2184
2185static void
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002186i915_gem_flush(struct drm_device *dev,
Chris Wilsonc78ec302010-09-20 12:50:23 +01002187 struct drm_file *file_priv,
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002188 uint32_t invalidate_domains,
Chris Wilson92204342010-09-18 11:02:01 +01002189 uint32_t flush_domains,
2190 uint32_t flush_rings)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002191{
2192 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter8bff9172010-02-11 22:19:40 +01002193
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002194 if (flush_domains & I915_GEM_DOMAIN_CPU)
Daniel Vetter40ce6572010-11-05 18:12:18 +01002195 intel_gtt_chipset_flush();
Zou Nan haid1b851f2010-05-21 09:08:57 +08002196
Chris Wilson92204342010-09-18 11:02:01 +01002197 if ((flush_domains | invalidate_domains) & I915_GEM_GPU_DOMAINS) {
2198 if (flush_rings & RING_RENDER)
Chris Wilsonc78ec302010-09-20 12:50:23 +01002199 i915_gem_flush_ring(dev, file_priv,
Chris Wilson92204342010-09-18 11:02:01 +01002200 &dev_priv->render_ring,
2201 invalidate_domains, flush_domains);
2202 if (flush_rings & RING_BSD)
Chris Wilsonc78ec302010-09-20 12:50:23 +01002203 i915_gem_flush_ring(dev, file_priv,
Chris Wilson92204342010-09-18 11:02:01 +01002204 &dev_priv->bsd_ring,
2205 invalidate_domains, flush_domains);
Chris Wilson549f7362010-10-19 11:19:32 +01002206 if (flush_rings & RING_BLT)
2207 i915_gem_flush_ring(dev, file_priv,
2208 &dev_priv->blt_ring,
2209 invalidate_domains, flush_domains);
Chris Wilson92204342010-09-18 11:02:01 +01002210 }
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002211}
2212
Eric Anholt673a3942008-07-30 12:06:12 -07002213/**
2214 * Ensures that all rendering to the object has completed and the object is
2215 * safe to unbind from the GTT or access from the CPU.
2216 */
2217static int
Chris Wilson2cf34d72010-09-14 13:03:28 +01002218i915_gem_object_wait_rendering(struct drm_gem_object *obj,
2219 bool interruptible)
Eric Anholt673a3942008-07-30 12:06:12 -07002220{
2221 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01002222 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002223 int ret;
2224
Eric Anholte47c68e2008-11-14 13:35:19 -08002225 /* This function only exists to support waiting for existing rendering,
2226 * not for emitting required flushes.
Eric Anholt673a3942008-07-30 12:06:12 -07002227 */
Eric Anholte47c68e2008-11-14 13:35:19 -08002228 BUG_ON((obj->write_domain & I915_GEM_GPU_DOMAINS) != 0);
Eric Anholt673a3942008-07-30 12:06:12 -07002229
2230 /* If there is rendering queued on the buffer being evicted, wait for
2231 * it.
2232 */
2233 if (obj_priv->active) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01002234 ret = i915_do_wait_request(dev,
2235 obj_priv->last_rendering_seqno,
2236 interruptible,
2237 obj_priv->ring);
2238 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002239 return ret;
2240 }
2241
2242 return 0;
2243}
2244
2245/**
2246 * Unbinds an object from the GTT aperture.
2247 */
Jesse Barnes0f973f22009-01-26 17:10:45 -08002248int
Eric Anholt673a3942008-07-30 12:06:12 -07002249i915_gem_object_unbind(struct drm_gem_object *obj)
2250{
2251 struct drm_device *dev = obj->dev;
Chris Wilson73aa8082010-09-30 11:46:12 +01002252 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002253 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002254 int ret = 0;
2255
Eric Anholt673a3942008-07-30 12:06:12 -07002256 if (obj_priv->gtt_space == NULL)
2257 return 0;
2258
2259 if (obj_priv->pin_count != 0) {
2260 DRM_ERROR("Attempting to unbind pinned buffer\n");
2261 return -EINVAL;
2262 }
2263
Eric Anholt5323fd02009-09-09 11:50:45 -07002264 /* blow away mappings if mapped through GTT */
2265 i915_gem_release_mmap(obj);
2266
Eric Anholt673a3942008-07-30 12:06:12 -07002267 /* Move the object to the CPU domain to ensure that
2268 * any possible CPU writes while it's not in the GTT
2269 * are flushed when we go to remap it. This will
2270 * also ensure that all pending GPU writes are finished
2271 * before we unbind.
2272 */
Eric Anholte47c68e2008-11-14 13:35:19 -08002273 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Chris Wilson8dc17752010-07-23 23:18:51 +01002274 if (ret == -ERESTARTSYS)
Eric Anholt673a3942008-07-30 12:06:12 -07002275 return ret;
Chris Wilson8dc17752010-07-23 23:18:51 +01002276 /* Continue on if we fail due to EIO, the GPU is hung so we
2277 * should be safe and we need to cleanup or else we might
2278 * cause memory corruption through use-after-free.
2279 */
Chris Wilson812ed4922010-09-30 15:08:57 +01002280 if (ret) {
2281 i915_gem_clflush_object(obj);
2282 obj->read_domains = obj->write_domain = I915_GEM_DOMAIN_CPU;
2283 }
Eric Anholt673a3942008-07-30 12:06:12 -07002284
Daniel Vetter96b47b62009-12-15 17:50:00 +01002285 /* release the fence reg _after_ flushing */
2286 if (obj_priv->fence_reg != I915_FENCE_REG_NONE)
2287 i915_gem_clear_fence_reg(obj);
2288
Chris Wilson73aa8082010-09-30 11:46:12 +01002289 drm_unbind_agp(obj_priv->agp_mem);
2290 drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002291
Chris Wilsone5281cc2010-10-28 13:45:36 +01002292 i915_gem_object_put_pages_gtt(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002293
Chris Wilsona00b10c2010-09-24 21:15:47 +01002294 i915_gem_info_remove_gtt(dev_priv, obj_priv);
Chris Wilson69dc4982010-10-19 10:36:51 +01002295 list_del_init(&obj_priv->mm_list);
Daniel Vetter75e9e912010-11-04 17:11:09 +01002296 /* Avoid an unnecessary call to unbind on rebind. */
2297 obj_priv->map_and_fenceable = true;
Eric Anholt673a3942008-07-30 12:06:12 -07002298
Chris Wilson73aa8082010-09-30 11:46:12 +01002299 drm_mm_put_block(obj_priv->gtt_space);
2300 obj_priv->gtt_space = NULL;
Chris Wilson9af90d12010-10-17 10:01:56 +01002301 obj_priv->gtt_offset = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002302
Chris Wilson963b4832009-09-20 23:03:54 +01002303 if (i915_gem_object_is_purgeable(obj_priv))
2304 i915_gem_object_truncate(obj);
2305
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002306 trace_i915_gem_object_unbind(obj);
2307
Chris Wilson8dc17752010-07-23 23:18:51 +01002308 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002309}
2310
Chris Wilsona56ba562010-09-28 10:07:56 +01002311static int i915_ring_idle(struct drm_device *dev,
2312 struct intel_ring_buffer *ring)
2313{
Chris Wilson395b70b2010-10-28 21:28:46 +01002314 if (list_empty(&ring->gpu_write_list) && list_empty(&ring->active_list))
Chris Wilson64193402010-10-24 12:38:05 +01002315 return 0;
2316
Chris Wilsona56ba562010-09-28 10:07:56 +01002317 i915_gem_flush_ring(dev, NULL, ring,
2318 I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
2319 return i915_wait_request(dev,
2320 i915_gem_next_request_seqno(dev, ring),
2321 ring);
2322}
2323
Chris Wilsonb47eb4a2010-08-07 11:01:23 +01002324int
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002325i915_gpu_idle(struct drm_device *dev)
2326{
2327 drm_i915_private_t *dev_priv = dev->dev_private;
2328 bool lists_empty;
Zou Nan hai852835f2010-05-21 09:08:56 +08002329 int ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002330
Zou Nan haid1b851f2010-05-21 09:08:57 +08002331 lists_empty = (list_empty(&dev_priv->mm.flushing_list) &&
Chris Wilson395b70b2010-10-28 21:28:46 +01002332 list_empty(&dev_priv->mm.active_list));
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002333 if (lists_empty)
2334 return 0;
2335
2336 /* Flush everything onto the inactive list. */
Chris Wilsona56ba562010-09-28 10:07:56 +01002337 ret = i915_ring_idle(dev, &dev_priv->render_ring);
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002338 if (ret)
2339 return ret;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002340
Chris Wilson87acb0a2010-10-19 10:13:00 +01002341 ret = i915_ring_idle(dev, &dev_priv->bsd_ring);
2342 if (ret)
2343 return ret;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002344
Chris Wilson549f7362010-10-19 11:19:32 +01002345 ret = i915_ring_idle(dev, &dev_priv->blt_ring);
2346 if (ret)
2347 return ret;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002348
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002349 return 0;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002350}
2351
Chris Wilsona00b10c2010-09-24 21:15:47 +01002352static void sandybridge_write_fence_reg(struct drm_gem_object *obj)
Eric Anholt4e901fd2009-10-26 16:44:17 -07002353{
Eric Anholt4e901fd2009-10-26 16:44:17 -07002354 struct drm_device *dev = obj->dev;
2355 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002356 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsona00b10c2010-09-24 21:15:47 +01002357 u32 size = i915_gem_get_gtt_size(obj_priv);
Eric Anholt4e901fd2009-10-26 16:44:17 -07002358 int regnum = obj_priv->fence_reg;
2359 uint64_t val;
2360
Chris Wilsona00b10c2010-09-24 21:15:47 +01002361 val = (uint64_t)((obj_priv->gtt_offset + size - 4096) &
Eric Anholt4e901fd2009-10-26 16:44:17 -07002362 0xfffff000) << 32;
2363 val |= obj_priv->gtt_offset & 0xfffff000;
2364 val |= (uint64_t)((obj_priv->stride / 128) - 1) <<
2365 SANDYBRIDGE_FENCE_PITCH_SHIFT;
2366
2367 if (obj_priv->tiling_mode == I915_TILING_Y)
2368 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2369 val |= I965_FENCE_REG_VALID;
2370
2371 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (regnum * 8), val);
2372}
2373
Chris Wilsona00b10c2010-09-24 21:15:47 +01002374static void i965_write_fence_reg(struct drm_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002375{
Jesse Barnesde151cf2008-11-12 10:03:55 -08002376 struct drm_device *dev = obj->dev;
2377 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002378 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsona00b10c2010-09-24 21:15:47 +01002379 u32 size = i915_gem_get_gtt_size(obj_priv);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002380 int regnum = obj_priv->fence_reg;
2381 uint64_t val;
2382
Chris Wilsona00b10c2010-09-24 21:15:47 +01002383 val = (uint64_t)((obj_priv->gtt_offset + size - 4096) &
Jesse Barnesde151cf2008-11-12 10:03:55 -08002384 0xfffff000) << 32;
2385 val |= obj_priv->gtt_offset & 0xfffff000;
2386 val |= ((obj_priv->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
2387 if (obj_priv->tiling_mode == I915_TILING_Y)
2388 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2389 val |= I965_FENCE_REG_VALID;
2390
2391 I915_WRITE64(FENCE_REG_965_0 + (regnum * 8), val);
2392}
2393
Chris Wilsona00b10c2010-09-24 21:15:47 +01002394static void i915_write_fence_reg(struct drm_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002395{
Jesse Barnesde151cf2008-11-12 10:03:55 -08002396 struct drm_device *dev = obj->dev;
2397 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002398 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsona00b10c2010-09-24 21:15:47 +01002399 u32 size = i915_gem_get_gtt_size(obj_priv);
2400 uint32_t fence_reg, val, pitch_val;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002401 int tile_width;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002402
2403 if ((obj_priv->gtt_offset & ~I915_FENCE_START_MASK) ||
Chris Wilsona00b10c2010-09-24 21:15:47 +01002404 (obj_priv->gtt_offset & (size - 1))) {
2405 WARN(1, "%s: object 0x%08x [fenceable? %d] not 1M or size (0x%08x) aligned [gtt_space offset=%lx, size=%lx]\n",
Daniel Vetter75e9e912010-11-04 17:11:09 +01002406 __func__, obj_priv->gtt_offset, obj_priv->map_and_fenceable, size,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002407 obj_priv->gtt_space->start, obj_priv->gtt_space->size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002408 return;
2409 }
2410
Jesse Barnes0f973f22009-01-26 17:10:45 -08002411 if (obj_priv->tiling_mode == I915_TILING_Y &&
2412 HAS_128_BYTE_Y_TILING(dev))
2413 tile_width = 128;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002414 else
Jesse Barnes0f973f22009-01-26 17:10:45 -08002415 tile_width = 512;
2416
2417 /* Note: pitch better be a power of two tile widths */
2418 pitch_val = obj_priv->stride / tile_width;
2419 pitch_val = ffs(pitch_val) - 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002420
Daniel Vetterc36a2a62010-04-17 15:12:03 +02002421 if (obj_priv->tiling_mode == I915_TILING_Y &&
2422 HAS_128_BYTE_Y_TILING(dev))
2423 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2424 else
2425 WARN_ON(pitch_val > I915_FENCE_MAX_PITCH_VAL);
2426
Jesse Barnesde151cf2008-11-12 10:03:55 -08002427 val = obj_priv->gtt_offset;
2428 if (obj_priv->tiling_mode == I915_TILING_Y)
2429 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002430 val |= I915_FENCE_SIZE_BITS(size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002431 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2432 val |= I830_FENCE_REG_VALID;
2433
Chris Wilsona00b10c2010-09-24 21:15:47 +01002434 fence_reg = obj_priv->fence_reg;
2435 if (fence_reg < 8)
2436 fence_reg = FENCE_REG_830_0 + fence_reg * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002437 else
Chris Wilsona00b10c2010-09-24 21:15:47 +01002438 fence_reg = FENCE_REG_945_8 + (fence_reg - 8) * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002439 I915_WRITE(fence_reg, val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002440}
2441
Chris Wilsona00b10c2010-09-24 21:15:47 +01002442static void i830_write_fence_reg(struct drm_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002443{
Jesse Barnesde151cf2008-11-12 10:03:55 -08002444 struct drm_device *dev = obj->dev;
2445 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002446 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsona00b10c2010-09-24 21:15:47 +01002447 u32 size = i915_gem_get_gtt_size(obj_priv);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002448 int regnum = obj_priv->fence_reg;
2449 uint32_t val;
2450 uint32_t pitch_val;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002451 uint32_t fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002452
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002453 if ((obj_priv->gtt_offset & ~I830_FENCE_START_MASK) ||
Jesse Barnesde151cf2008-11-12 10:03:55 -08002454 (obj_priv->gtt_offset & (obj->size - 1))) {
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002455 WARN(1, "%s: object 0x%08x not 512K or size aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002456 __func__, obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002457 return;
2458 }
2459
Eric Anholte76a16d2009-05-26 17:44:56 -07002460 pitch_val = obj_priv->stride / 128;
2461 pitch_val = ffs(pitch_val) - 1;
2462 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2463
Jesse Barnesde151cf2008-11-12 10:03:55 -08002464 val = obj_priv->gtt_offset;
2465 if (obj_priv->tiling_mode == I915_TILING_Y)
2466 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002467 fence_size_bits = I830_FENCE_SIZE_BITS(size);
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002468 WARN_ON(fence_size_bits & ~0x00000f00);
2469 val |= fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002470 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2471 val |= I830_FENCE_REG_VALID;
2472
2473 I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002474}
2475
Chris Wilson2cf34d72010-09-14 13:03:28 +01002476static int i915_find_fence_reg(struct drm_device *dev,
2477 bool interruptible)
Daniel Vetterae3db242010-02-19 11:51:58 +01002478{
Daniel Vetterae3db242010-02-19 11:51:58 +01002479 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002480 struct drm_i915_fence_reg *reg;
2481 struct drm_i915_gem_object *obj_priv = NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002482 int i, avail, ret;
2483
2484 /* First try to find a free reg */
2485 avail = 0;
2486 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2487 reg = &dev_priv->fence_regs[i];
2488 if (!reg->obj)
2489 return i;
2490
Daniel Vetter23010e42010-03-08 13:35:02 +01002491 obj_priv = to_intel_bo(reg->obj);
Daniel Vetterae3db242010-02-19 11:51:58 +01002492 if (!obj_priv->pin_count)
2493 avail++;
2494 }
2495
2496 if (avail == 0)
2497 return -ENOSPC;
2498
2499 /* None available, try to steal one or wait for a user to finish */
Chris Wilsona00b10c2010-09-24 21:15:47 +01002500 avail = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002501 list_for_each_entry(reg, &dev_priv->mm.fence_list,
2502 lru_list) {
Chris Wilsona00b10c2010-09-24 21:15:47 +01002503 obj_priv = to_intel_bo(reg->obj);
Daniel Vetterae3db242010-02-19 11:51:58 +01002504 if (obj_priv->pin_count)
2505 continue;
2506
2507 /* found one! */
Chris Wilsona00b10c2010-09-24 21:15:47 +01002508 avail = obj_priv->fence_reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01002509 break;
2510 }
2511
Chris Wilsona00b10c2010-09-24 21:15:47 +01002512 BUG_ON(avail == I915_FENCE_REG_NONE);
Daniel Vetterae3db242010-02-19 11:51:58 +01002513
2514 /* We only have a reference on obj from the active list. put_fence_reg
2515 * might drop that one, causing a use-after-free in it. So hold a
2516 * private reference to obj like the other callers of put_fence_reg
2517 * (set_tiling ioctl) do. */
Chris Wilsona00b10c2010-09-24 21:15:47 +01002518 drm_gem_object_reference(&obj_priv->base);
2519 ret = i915_gem_object_put_fence_reg(&obj_priv->base, interruptible);
2520 drm_gem_object_unreference(&obj_priv->base);
Daniel Vetterae3db242010-02-19 11:51:58 +01002521 if (ret != 0)
2522 return ret;
2523
Chris Wilsona00b10c2010-09-24 21:15:47 +01002524 return avail;
Daniel Vetterae3db242010-02-19 11:51:58 +01002525}
2526
Jesse Barnesde151cf2008-11-12 10:03:55 -08002527/**
2528 * i915_gem_object_get_fence_reg - set up a fence reg for an object
2529 * @obj: object to map through a fence reg
2530 *
2531 * When mapping objects through the GTT, userspace wants to be able to write
2532 * to them without having to worry about swizzling if the object is tiled.
2533 *
2534 * This function walks the fence regs looking for a free one for @obj,
2535 * stealing one if it can't find any.
2536 *
2537 * It then sets up the reg based on the object's properties: address, pitch
2538 * and tiling format.
2539 */
Chris Wilson8c4b8c32009-06-17 22:08:52 +01002540int
Chris Wilson2cf34d72010-09-14 13:03:28 +01002541i915_gem_object_get_fence_reg(struct drm_gem_object *obj,
2542 bool interruptible)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002543{
2544 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002545 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002546 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002547 struct drm_i915_fence_reg *reg = NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002548 int ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002549
Eric Anholta09ba7f2009-08-29 12:49:51 -07002550 /* Just update our place in the LRU if our fence is getting used. */
2551 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002552 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
2553 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002554 return 0;
2555 }
2556
Jesse Barnesde151cf2008-11-12 10:03:55 -08002557 switch (obj_priv->tiling_mode) {
2558 case I915_TILING_NONE:
2559 WARN(1, "allocating a fence for non-tiled object?\n");
2560 break;
2561 case I915_TILING_X:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002562 if (!obj_priv->stride)
2563 return -EINVAL;
2564 WARN((obj_priv->stride & (512 - 1)),
2565 "object 0x%08x is X tiled but has non-512B pitch\n",
2566 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002567 break;
2568 case I915_TILING_Y:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002569 if (!obj_priv->stride)
2570 return -EINVAL;
2571 WARN((obj_priv->stride & (128 - 1)),
2572 "object 0x%08x is Y tiled but has non-128B pitch\n",
2573 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002574 break;
2575 }
2576
Chris Wilson2cf34d72010-09-14 13:03:28 +01002577 ret = i915_find_fence_reg(dev, interruptible);
Daniel Vetterae3db242010-02-19 11:51:58 +01002578 if (ret < 0)
2579 return ret;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002580
Daniel Vetterae3db242010-02-19 11:51:58 +01002581 obj_priv->fence_reg = ret;
2582 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002583 list_add_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002584
Jesse Barnesde151cf2008-11-12 10:03:55 -08002585 reg->obj = obj;
2586
Chris Wilsone259bef2010-09-17 00:32:02 +01002587 switch (INTEL_INFO(dev)->gen) {
2588 case 6:
Chris Wilsona00b10c2010-09-24 21:15:47 +01002589 sandybridge_write_fence_reg(obj);
Chris Wilsone259bef2010-09-17 00:32:02 +01002590 break;
2591 case 5:
2592 case 4:
Chris Wilsona00b10c2010-09-24 21:15:47 +01002593 i965_write_fence_reg(obj);
Chris Wilsone259bef2010-09-17 00:32:02 +01002594 break;
2595 case 3:
Chris Wilsona00b10c2010-09-24 21:15:47 +01002596 i915_write_fence_reg(obj);
Chris Wilsone259bef2010-09-17 00:32:02 +01002597 break;
2598 case 2:
Chris Wilsona00b10c2010-09-24 21:15:47 +01002599 i830_write_fence_reg(obj);
Chris Wilsone259bef2010-09-17 00:32:02 +01002600 break;
2601 }
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002602
Chris Wilsona00b10c2010-09-24 21:15:47 +01002603 trace_i915_gem_object_get_fence(obj,
2604 obj_priv->fence_reg,
2605 obj_priv->tiling_mode);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002606
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002607 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002608}
2609
2610/**
2611 * i915_gem_clear_fence_reg - clear out fence register info
2612 * @obj: object to clear
2613 *
2614 * Zeroes out the fence register itself and clears out the associated
2615 * data structures in dev_priv and obj_priv.
2616 */
2617static void
2618i915_gem_clear_fence_reg(struct drm_gem_object *obj)
2619{
2620 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002621 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002622 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002623 struct drm_i915_fence_reg *reg =
2624 &dev_priv->fence_regs[obj_priv->fence_reg];
Chris Wilsone259bef2010-09-17 00:32:02 +01002625 uint32_t fence_reg;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002626
Chris Wilsone259bef2010-09-17 00:32:02 +01002627 switch (INTEL_INFO(dev)->gen) {
2628 case 6:
Eric Anholt4e901fd2009-10-26 16:44:17 -07002629 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 +
2630 (obj_priv->fence_reg * 8), 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002631 break;
2632 case 5:
2633 case 4:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002634 I915_WRITE64(FENCE_REG_965_0 + (obj_priv->fence_reg * 8), 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002635 break;
2636 case 3:
Chris Wilson9b74f732010-09-22 19:10:44 +01002637 if (obj_priv->fence_reg >= 8)
Chris Wilsone259bef2010-09-17 00:32:02 +01002638 fence_reg = FENCE_REG_945_8 + (obj_priv->fence_reg - 8) * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002639 else
Chris Wilsone259bef2010-09-17 00:32:02 +01002640 case 2:
2641 fence_reg = FENCE_REG_830_0 + obj_priv->fence_reg * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002642
2643 I915_WRITE(fence_reg, 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002644 break;
Eric Anholtdc529a42009-03-10 22:34:49 -07002645 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002646
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002647 reg->obj = NULL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002648 obj_priv->fence_reg = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002649 list_del_init(&reg->lru_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002650}
2651
Eric Anholt673a3942008-07-30 12:06:12 -07002652/**
Chris Wilson52dc7d32009-06-06 09:46:01 +01002653 * i915_gem_object_put_fence_reg - waits on outstanding fenced access
2654 * to the buffer to finish, and then resets the fence register.
2655 * @obj: tiled object holding a fence register.
Chris Wilson2cf34d72010-09-14 13:03:28 +01002656 * @bool: whether the wait upon the fence is interruptible
Chris Wilson52dc7d32009-06-06 09:46:01 +01002657 *
2658 * Zeroes out the fence register itself and clears out the associated
2659 * data structures in dev_priv and obj_priv.
2660 */
2661int
Chris Wilson2cf34d72010-09-14 13:03:28 +01002662i915_gem_object_put_fence_reg(struct drm_gem_object *obj,
2663 bool interruptible)
Chris Wilson52dc7d32009-06-06 09:46:01 +01002664{
2665 struct drm_device *dev = obj->dev;
Chris Wilson53640e12010-09-20 11:40:50 +01002666 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002667 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson53640e12010-09-20 11:40:50 +01002668 struct drm_i915_fence_reg *reg;
Chris Wilson52dc7d32009-06-06 09:46:01 +01002669
2670 if (obj_priv->fence_reg == I915_FENCE_REG_NONE)
2671 return 0;
2672
Daniel Vetter10ae9bd2010-02-01 13:59:17 +01002673 /* If we've changed tiling, GTT-mappings of the object
2674 * need to re-fault to ensure that the correct fence register
2675 * setup is in place.
2676 */
2677 i915_gem_release_mmap(obj);
2678
Chris Wilson52dc7d32009-06-06 09:46:01 +01002679 /* On the i915, GPU access to tiled buffers is via a fence,
2680 * therefore we must wait for any outstanding access to complete
2681 * before clearing the fence.
2682 */
Chris Wilson53640e12010-09-20 11:40:50 +01002683 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
2684 if (reg->gpu) {
Chris Wilson52dc7d32009-06-06 09:46:01 +01002685 int ret;
2686
Chris Wilson2cf34d72010-09-14 13:03:28 +01002687 ret = i915_gem_object_flush_gpu_write_domain(obj, true);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002688 if (ret)
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002689 return ret;
2690
Chris Wilson2cf34d72010-09-14 13:03:28 +01002691 ret = i915_gem_object_wait_rendering(obj, interruptible);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002692 if (ret)
Chris Wilson52dc7d32009-06-06 09:46:01 +01002693 return ret;
Chris Wilson53640e12010-09-20 11:40:50 +01002694
2695 reg->gpu = false;
Chris Wilson52dc7d32009-06-06 09:46:01 +01002696 }
2697
Daniel Vetter4a726612010-02-01 13:59:16 +01002698 i915_gem_object_flush_gtt_write_domain(obj);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002699 i915_gem_clear_fence_reg(obj);
Chris Wilson52dc7d32009-06-06 09:46:01 +01002700
2701 return 0;
2702}
2703
2704/**
Eric Anholt673a3942008-07-30 12:06:12 -07002705 * Finds free space in the GTT aperture and binds the object there.
2706 */
2707static int
Daniel Vetter920afa72010-09-16 17:54:23 +02002708i915_gem_object_bind_to_gtt(struct drm_gem_object *obj,
2709 unsigned alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01002710 bool map_and_fenceable)
Eric Anholt673a3942008-07-30 12:06:12 -07002711{
2712 struct drm_device *dev = obj->dev;
2713 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002714 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002715 struct drm_mm_node *free_space;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002716 gfp_t gfpmask = __GFP_NORETRY | __GFP_NOWARN;
Daniel Vetter5e783302010-11-14 22:32:36 +01002717 u32 size, fence_size, fence_alignment, unfenced_alignment;
Daniel Vetter75e9e912010-11-04 17:11:09 +01002718 bool mappable, fenceable;
Chris Wilson07f73f62009-09-14 16:50:30 +01002719 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002720
Chris Wilsonbb6baf72009-09-22 14:24:13 +01002721 if (obj_priv->madv != I915_MADV_WILLNEED) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01002722 DRM_ERROR("Attempting to bind a purgeable object\n");
2723 return -EINVAL;
2724 }
2725
Chris Wilsona00b10c2010-09-24 21:15:47 +01002726 fence_size = i915_gem_get_gtt_size(obj_priv);
2727 fence_alignment = i915_gem_get_gtt_alignment(obj_priv);
Daniel Vetter5e783302010-11-14 22:32:36 +01002728 unfenced_alignment = i915_gem_get_unfenced_gtt_alignment(obj_priv);
Chris Wilsona00b10c2010-09-24 21:15:47 +01002729
Eric Anholt673a3942008-07-30 12:06:12 -07002730 if (alignment == 0)
Daniel Vetter5e783302010-11-14 22:32:36 +01002731 alignment = map_and_fenceable ? fence_alignment :
2732 unfenced_alignment;
Daniel Vetter75e9e912010-11-04 17:11:09 +01002733 if (map_and_fenceable && alignment & (fence_alignment - 1)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002734 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2735 return -EINVAL;
2736 }
2737
Daniel Vetter75e9e912010-11-04 17:11:09 +01002738 size = map_and_fenceable ? fence_size : obj->size;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002739
Chris Wilson654fc602010-05-27 13:18:21 +01002740 /* If the object is bigger than the entire aperture, reject it early
2741 * before evicting everything in a vain attempt to find space.
2742 */
Daniel Vetter920afa72010-09-16 17:54:23 +02002743 if (obj->size >
Daniel Vetter75e9e912010-11-04 17:11:09 +01002744 (map_and_fenceable ? dev_priv->mm.gtt_mappable_end : dev_priv->mm.gtt_total)) {
Chris Wilson654fc602010-05-27 13:18:21 +01002745 DRM_ERROR("Attempting to bind an object larger than the aperture\n");
2746 return -E2BIG;
2747 }
2748
Eric Anholt673a3942008-07-30 12:06:12 -07002749 search_free:
Daniel Vetter75e9e912010-11-04 17:11:09 +01002750 if (map_and_fenceable)
Daniel Vetter920afa72010-09-16 17:54:23 +02002751 free_space =
2752 drm_mm_search_free_in_range(&dev_priv->mm.gtt_space,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002753 size, alignment, 0,
Daniel Vetter920afa72010-09-16 17:54:23 +02002754 dev_priv->mm.gtt_mappable_end,
2755 0);
2756 else
2757 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002758 size, alignment, 0);
Daniel Vetter920afa72010-09-16 17:54:23 +02002759
2760 if (free_space != NULL) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01002761 if (map_and_fenceable)
Daniel Vetter920afa72010-09-16 17:54:23 +02002762 obj_priv->gtt_space =
2763 drm_mm_get_block_range_generic(free_space,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002764 size, alignment, 0,
Daniel Vetter920afa72010-09-16 17:54:23 +02002765 dev_priv->mm.gtt_mappable_end,
2766 0);
2767 else
2768 obj_priv->gtt_space =
Chris Wilsona00b10c2010-09-24 21:15:47 +01002769 drm_mm_get_block(free_space, size, alignment);
Daniel Vetter920afa72010-09-16 17:54:23 +02002770 }
Eric Anholt673a3942008-07-30 12:06:12 -07002771 if (obj_priv->gtt_space == NULL) {
2772 /* If the gtt is empty and we're still having trouble
2773 * fitting our object in, we're out of memory.
2774 */
Daniel Vetter75e9e912010-11-04 17:11:09 +01002775 ret = i915_gem_evict_something(dev, size, alignment,
2776 map_and_fenceable);
Chris Wilson97311292009-09-21 00:22:34 +01002777 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002778 return ret;
Chris Wilson97311292009-09-21 00:22:34 +01002779
Eric Anholt673a3942008-07-30 12:06:12 -07002780 goto search_free;
2781 }
2782
Chris Wilsone5281cc2010-10-28 13:45:36 +01002783 ret = i915_gem_object_get_pages_gtt(obj, gfpmask);
Eric Anholt673a3942008-07-30 12:06:12 -07002784 if (ret) {
2785 drm_mm_put_block(obj_priv->gtt_space);
2786 obj_priv->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002787
2788 if (ret == -ENOMEM) {
2789 /* first try to clear up some space from the GTT */
Chris Wilsona00b10c2010-09-24 21:15:47 +01002790 ret = i915_gem_evict_something(dev, size,
Daniel Vetter75e9e912010-11-04 17:11:09 +01002791 alignment,
2792 map_and_fenceable);
Chris Wilson07f73f62009-09-14 16:50:30 +01002793 if (ret) {
Chris Wilson07f73f62009-09-14 16:50:30 +01002794 /* now try to shrink everyone else */
Chris Wilson4bdadb92010-01-27 13:36:32 +00002795 if (gfpmask) {
2796 gfpmask = 0;
2797 goto search_free;
Chris Wilson07f73f62009-09-14 16:50:30 +01002798 }
2799
2800 return ret;
2801 }
2802
2803 goto search_free;
2804 }
2805
Eric Anholt673a3942008-07-30 12:06:12 -07002806 return ret;
2807 }
2808
Eric Anholt673a3942008-07-30 12:06:12 -07002809 /* Create an AGP memory structure pointing at our pages, and bind it
2810 * into the GTT.
2811 */
2812 obj_priv->agp_mem = drm_agp_bind_pages(dev,
Eric Anholt856fa192009-03-19 14:10:50 -07002813 obj_priv->pages,
Chris Wilson07f73f62009-09-14 16:50:30 +01002814 obj->size >> PAGE_SHIFT,
Chris Wilson9af90d12010-10-17 10:01:56 +01002815 obj_priv->gtt_space->start,
Keith Packardba1eb1d2008-10-14 19:55:10 -07002816 obj_priv->agp_type);
Eric Anholt673a3942008-07-30 12:06:12 -07002817 if (obj_priv->agp_mem == NULL) {
Chris Wilsone5281cc2010-10-28 13:45:36 +01002818 i915_gem_object_put_pages_gtt(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002819 drm_mm_put_block(obj_priv->gtt_space);
2820 obj_priv->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002821
Chris Wilsona00b10c2010-09-24 21:15:47 +01002822 ret = i915_gem_evict_something(dev, size,
Daniel Vetter75e9e912010-11-04 17:11:09 +01002823 alignment, map_and_fenceable);
Chris Wilson97311292009-09-21 00:22:34 +01002824 if (ret)
Chris Wilson07f73f62009-09-14 16:50:30 +01002825 return ret;
Chris Wilson07f73f62009-09-14 16:50:30 +01002826
2827 goto search_free;
Eric Anholt673a3942008-07-30 12:06:12 -07002828 }
Eric Anholt673a3942008-07-30 12:06:12 -07002829
Daniel Vetterfb7d5162010-10-01 22:05:20 +02002830 obj_priv->gtt_offset = obj_priv->gtt_space->start;
2831
Chris Wilsonbf1a1092010-08-07 11:01:20 +01002832 /* keep track of bounds object by adding it to the inactive list */
Chris Wilson69dc4982010-10-19 10:36:51 +01002833 list_add_tail(&obj_priv->mm_list, &dev_priv->mm.inactive_list);
Chris Wilsona00b10c2010-09-24 21:15:47 +01002834 i915_gem_info_add_gtt(dev_priv, obj_priv);
Chris Wilsonbf1a1092010-08-07 11:01:20 +01002835
Eric Anholt673a3942008-07-30 12:06:12 -07002836 /* Assert that the object is not currently in any GPU domain. As it
2837 * wasn't in the GTT, there shouldn't be any way it could have been in
2838 * a GPU cache
2839 */
Chris Wilson21d509e2009-06-06 09:46:02 +01002840 BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS);
2841 BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS);
Eric Anholt673a3942008-07-30 12:06:12 -07002842
Daniel Vetter75e9e912010-11-04 17:11:09 +01002843 trace_i915_gem_object_bind(obj, obj_priv->gtt_offset, map_and_fenceable);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002844
Daniel Vetter75e9e912010-11-04 17:11:09 +01002845 fenceable =
Chris Wilsona00b10c2010-09-24 21:15:47 +01002846 obj_priv->gtt_space->size == fence_size &&
2847 (obj_priv->gtt_space->start & (fence_alignment -1)) == 0;
2848
Daniel Vetter75e9e912010-11-04 17:11:09 +01002849 mappable =
Chris Wilsona00b10c2010-09-24 21:15:47 +01002850 obj_priv->gtt_offset + obj->size <= dev_priv->mm.gtt_mappable_end;
2851
Daniel Vetter75e9e912010-11-04 17:11:09 +01002852 obj_priv->map_and_fenceable = mappable && fenceable;
2853
Eric Anholt673a3942008-07-30 12:06:12 -07002854 return 0;
2855}
2856
2857void
2858i915_gem_clflush_object(struct drm_gem_object *obj)
2859{
Daniel Vetter23010e42010-03-08 13:35:02 +01002860 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002861
2862 /* If we don't have a page list set up, then we're not pinned
2863 * to GPU, and we can ignore the cache flush because it'll happen
2864 * again at bind time.
2865 */
Eric Anholt856fa192009-03-19 14:10:50 -07002866 if (obj_priv->pages == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002867 return;
2868
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002869 trace_i915_gem_object_clflush(obj);
Eric Anholtcfa16a02009-05-26 18:46:16 -07002870
Eric Anholt856fa192009-03-19 14:10:50 -07002871 drm_clflush_pages(obj_priv->pages, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002872}
2873
Eric Anholte47c68e2008-11-14 13:35:19 -08002874/** Flushes any GPU write domain for the object if it's dirty. */
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002875static int
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002876i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj,
2877 bool pipelined)
Eric Anholte47c68e2008-11-14 13:35:19 -08002878{
2879 struct drm_device *dev = obj->dev;
Eric Anholte47c68e2008-11-14 13:35:19 -08002880
2881 if ((obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002882 return 0;
Eric Anholte47c68e2008-11-14 13:35:19 -08002883
2884 /* Queue the GPU write cache flushing we need. */
Chris Wilsonc78ec302010-09-20 12:50:23 +01002885 i915_gem_flush_ring(dev, NULL,
Chris Wilson92204342010-09-18 11:02:01 +01002886 to_intel_bo(obj)->ring,
2887 0, obj->write_domain);
Chris Wilson48b956c2010-09-14 12:50:34 +01002888 BUG_ON(obj->write_domain);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002889
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002890 if (pipelined)
2891 return 0;
2892
Chris Wilson2cf34d72010-09-14 13:03:28 +01002893 return i915_gem_object_wait_rendering(obj, true);
Eric Anholte47c68e2008-11-14 13:35:19 -08002894}
2895
2896/** Flushes the GTT write domain for the object if it's dirty. */
2897static void
2898i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj)
2899{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002900 uint32_t old_write_domain;
2901
Eric Anholte47c68e2008-11-14 13:35:19 -08002902 if (obj->write_domain != I915_GEM_DOMAIN_GTT)
2903 return;
2904
2905 /* No actual flushing is required for the GTT write domain. Writes
2906 * 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.
2908 */
Chris Wilson4a684a42010-10-28 14:44:08 +01002909 i915_gem_release_mmap(obj);
2910
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002911 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002912 obj->write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002913
2914 trace_i915_gem_object_change_domain(obj,
2915 obj->read_domains,
2916 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002917}
2918
2919/** Flushes the CPU write domain for the object if it's dirty. */
2920static void
2921i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj)
2922{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002923 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002924
2925 if (obj->write_domain != I915_GEM_DOMAIN_CPU)
2926 return;
2927
2928 i915_gem_clflush_object(obj);
Daniel Vetter40ce6572010-11-05 18:12:18 +01002929 intel_gtt_chipset_flush();
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002930 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002931 obj->write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002932
2933 trace_i915_gem_object_change_domain(obj,
2934 obj->read_domains,
2935 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002936}
2937
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002938/**
2939 * Moves a single object to the GTT read, and possibly write domain.
2940 *
2941 * This function returns when the move is complete, including waiting on
2942 * flushes to occur.
2943 */
Jesse Barnes79e53942008-11-07 14:24:08 -08002944int
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002945i915_gem_object_set_to_gtt_domain(struct drm_gem_object *obj, int write)
2946{
Daniel Vetter23010e42010-03-08 13:35:02 +01002947 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002948 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002949 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002950
Eric Anholt02354392008-11-26 13:58:13 -08002951 /* Not valid to be called on unbound objects. */
2952 if (obj_priv->gtt_space == NULL)
2953 return -EINVAL;
2954
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002955 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002956 if (ret != 0)
2957 return ret;
2958
Chris Wilson72133422010-09-13 23:56:38 +01002959 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002960
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002961 if (write) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01002962 ret = i915_gem_object_wait_rendering(obj, true);
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002963 if (ret)
2964 return ret;
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002965 }
Eric Anholte47c68e2008-11-14 13:35:19 -08002966
2967 old_write_domain = obj->write_domain;
2968 old_read_domains = obj->read_domains;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002969
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002970 /* It should now be out of any other write domains, and we can update
2971 * the domain values for our changes.
2972 */
2973 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2974 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002975 if (write) {
Chris Wilson72133422010-09-13 23:56:38 +01002976 obj->read_domains = I915_GEM_DOMAIN_GTT;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002977 obj->write_domain = I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002978 obj_priv->dirty = 1;
2979 }
2980
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002981 trace_i915_gem_object_change_domain(obj,
2982 old_read_domains,
2983 old_write_domain);
2984
Eric Anholte47c68e2008-11-14 13:35:19 -08002985 return 0;
2986}
2987
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002988/*
2989 * Prepare buffer for display plane. Use uninterruptible for possible flush
2990 * wait, as in modesetting process we're not supposed to be interrupted.
2991 */
2992int
Chris Wilson48b956c2010-09-14 12:50:34 +01002993i915_gem_object_set_to_display_plane(struct drm_gem_object *obj,
2994 bool pipelined)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002995{
Daniel Vetter23010e42010-03-08 13:35:02 +01002996 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002997 uint32_t old_read_domains;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002998 int ret;
2999
3000 /* Not valid to be called on unbound objects. */
3001 if (obj_priv->gtt_space == NULL)
3002 return -EINVAL;
3003
Chris Wilsonced270f2010-09-26 22:47:46 +01003004 ret = i915_gem_object_flush_gpu_write_domain(obj, true);
Chris Wilson2dafb1e2010-06-07 14:03:05 +01003005 if (ret)
3006 return ret;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003007
Chris Wilsonced270f2010-09-26 22:47:46 +01003008 /* Currently, we are always called from an non-interruptible context. */
3009 if (!pipelined) {
3010 ret = i915_gem_object_wait_rendering(obj, false);
3011 if (ret)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003012 return ret;
3013 }
3014
Chris Wilsonb118c1e2010-05-27 13:18:14 +01003015 i915_gem_object_flush_cpu_write_domain(obj);
3016
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003017 old_read_domains = obj->read_domains;
Chris Wilsonc78ec302010-09-20 12:50:23 +01003018 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003019
3020 trace_i915_gem_object_change_domain(obj,
3021 old_read_domains,
Daniel Vetterba3d8d72010-02-11 22:37:04 +01003022 obj->write_domain);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003023
3024 return 0;
3025}
3026
Chris Wilson85345512010-11-13 09:49:11 +00003027int
3028i915_gem_object_flush_gpu(struct drm_i915_gem_object *obj,
3029 bool interruptible)
3030{
3031 if (!obj->active)
3032 return 0;
3033
3034 if (obj->base.write_domain & I915_GEM_GPU_DOMAINS)
3035 i915_gem_flush_ring(obj->base.dev, NULL, obj->ring,
3036 0, obj->base.write_domain);
3037
3038 return i915_gem_object_wait_rendering(&obj->base, interruptible);
3039}
3040
Eric Anholte47c68e2008-11-14 13:35:19 -08003041/**
3042 * Moves a single object to the CPU read, and possibly write domain.
3043 *
3044 * This function returns when the move is complete, including waiting on
3045 * flushes to occur.
3046 */
3047static int
3048i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj, int write)
3049{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003050 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003051 int ret;
3052
Daniel Vetterba3d8d72010-02-11 22:37:04 +01003053 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Eric Anholte47c68e2008-11-14 13:35:19 -08003054 if (ret != 0)
3055 return ret;
3056
3057 i915_gem_object_flush_gtt_write_domain(obj);
3058
3059 /* If we have a partially-valid cache of the object in the CPU,
3060 * finish invalidating it and free the per-page flags.
3061 */
3062 i915_gem_object_set_to_full_cpu_read_domain(obj);
3063
Chris Wilson72133422010-09-13 23:56:38 +01003064 if (write) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01003065 ret = i915_gem_object_wait_rendering(obj, true);
Chris Wilson72133422010-09-13 23:56:38 +01003066 if (ret)
3067 return ret;
3068 }
3069
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003070 old_write_domain = obj->write_domain;
3071 old_read_domains = obj->read_domains;
3072
Eric Anholte47c68e2008-11-14 13:35:19 -08003073 /* Flush the CPU cache if it's still invalid. */
3074 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
3075 i915_gem_clflush_object(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003076
3077 obj->read_domains |= I915_GEM_DOMAIN_CPU;
3078 }
3079
3080 /* It should now be out of any other write domains, and we can update
3081 * the domain values for our changes.
3082 */
3083 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
3084
3085 /* If we're writing through the CPU, then the GPU read domains will
3086 * need to be invalidated at next use.
3087 */
3088 if (write) {
Chris Wilsonc78ec302010-09-20 12:50:23 +01003089 obj->read_domains = I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003090 obj->write_domain = I915_GEM_DOMAIN_CPU;
3091 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003092
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003093 trace_i915_gem_object_change_domain(obj,
3094 old_read_domains,
3095 old_write_domain);
3096
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003097 return 0;
3098}
3099
Eric Anholt673a3942008-07-30 12:06:12 -07003100/*
3101 * Set the next domain for the specified object. This
3102 * may not actually perform the necessary flushing/invaliding though,
3103 * as that may want to be batched with other set_domain operations
3104 *
3105 * This is (we hope) the only really tricky part of gem. The goal
3106 * is fairly simple -- track which caches hold bits of the object
3107 * and make sure they remain coherent. A few concrete examples may
3108 * help to explain how it works. For shorthand, we use the notation
3109 * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
3110 * a pair of read and write domain masks.
3111 *
3112 * Case 1: the batch buffer
3113 *
3114 * 1. Allocated
3115 * 2. Written by CPU
3116 * 3. Mapped to GTT
3117 * 4. Read by GPU
3118 * 5. Unmapped from GTT
3119 * 6. Freed
3120 *
3121 * Let's take these a step at a time
3122 *
3123 * 1. Allocated
3124 * Pages allocated from the kernel may still have
3125 * cache contents, so we set them to (CPU, CPU) always.
3126 * 2. Written by CPU (using pwrite)
3127 * The pwrite function calls set_domain (CPU, CPU) and
3128 * this function does nothing (as nothing changes)
3129 * 3. Mapped by GTT
3130 * This function asserts that the object is not
3131 * currently in any GPU-based read or write domains
3132 * 4. Read by GPU
3133 * i915_gem_execbuffer calls set_domain (COMMAND, 0).
3134 * As write_domain is zero, this function adds in the
3135 * current read domains (CPU+COMMAND, 0).
3136 * flush_domains is set to CPU.
3137 * invalidate_domains is set to COMMAND
3138 * clflush is run to get data out of the CPU caches
3139 * then i915_dev_set_domain calls i915_gem_flush to
3140 * emit an MI_FLUSH and drm_agp_chipset_flush
3141 * 5. Unmapped from GTT
3142 * i915_gem_object_unbind calls set_domain (CPU, CPU)
3143 * flush_domains and invalidate_domains end up both zero
3144 * so no flushing/invalidating happens
3145 * 6. Freed
3146 * yay, done
3147 *
3148 * Case 2: The shared render buffer
3149 *
3150 * 1. Allocated
3151 * 2. Mapped to GTT
3152 * 3. Read/written by GPU
3153 * 4. set_domain to (CPU,CPU)
3154 * 5. Read/written by CPU
3155 * 6. Read/written by GPU
3156 *
3157 * 1. Allocated
3158 * Same as last example, (CPU, CPU)
3159 * 2. Mapped to GTT
3160 * Nothing changes (assertions find that it is not in the GPU)
3161 * 3. Read/written by GPU
3162 * execbuffer calls set_domain (RENDER, RENDER)
3163 * flush_domains gets CPU
3164 * invalidate_domains gets GPU
3165 * clflush (obj)
3166 * MI_FLUSH and drm_agp_chipset_flush
3167 * 4. set_domain (CPU, CPU)
3168 * flush_domains gets GPU
3169 * invalidate_domains gets CPU
3170 * wait_rendering (obj) to make sure all drawing is complete.
3171 * This will include an MI_FLUSH to get the data from GPU
3172 * to memory
3173 * clflush (obj) to invalidate the CPU cache
3174 * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
3175 * 5. Read/written by CPU
3176 * cache lines are loaded and dirtied
3177 * 6. Read written by GPU
3178 * Same as last GPU access
3179 *
3180 * Case 3: The constant buffer
3181 *
3182 * 1. Allocated
3183 * 2. Written by CPU
3184 * 3. Read by GPU
3185 * 4. Updated (written) by CPU again
3186 * 5. Read by GPU
3187 *
3188 * 1. Allocated
3189 * (CPU, CPU)
3190 * 2. Written by CPU
3191 * (CPU, CPU)
3192 * 3. Read by GPU
3193 * (CPU+RENDER, 0)
3194 * flush_domains = CPU
3195 * invalidate_domains = RENDER
3196 * clflush (obj)
3197 * MI_FLUSH
3198 * drm_agp_chipset_flush
3199 * 4. Updated (written) by CPU again
3200 * (CPU, CPU)
3201 * flush_domains = 0 (no previous write domain)
3202 * invalidate_domains = 0 (no new read domains)
3203 * 5. Read by GPU
3204 * (CPU+RENDER, 0)
3205 * flush_domains = CPU
3206 * invalidate_domains = RENDER
3207 * clflush (obj)
3208 * MI_FLUSH
3209 * drm_agp_chipset_flush
3210 */
Keith Packardc0d90822008-11-20 23:11:08 -08003211static void
Chris Wilsonb6651452010-10-23 10:15:06 +01003212i915_gem_object_set_to_gpu_domain(struct drm_gem_object *obj,
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003213 struct intel_ring_buffer *ring,
3214 struct change_domains *cd)
Eric Anholt673a3942008-07-30 12:06:12 -07003215{
Daniel Vetter23010e42010-03-08 13:35:02 +01003216 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003217 uint32_t invalidate_domains = 0;
3218 uint32_t flush_domains = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07003219
Eric Anholt673a3942008-07-30 12:06:12 -07003220 /*
3221 * If the object isn't moving to a new write domain,
3222 * let the object stay in multiple read domains
3223 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003224 if (obj->pending_write_domain == 0)
3225 obj->pending_read_domains |= obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07003226
3227 /*
3228 * Flush the current write domain if
3229 * the new read domains don't match. Invalidate
3230 * any read domains which differ from the old
3231 * write domain
3232 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003233 if (obj->write_domain &&
Chris Wilson13b29282010-11-01 12:22:48 +00003234 (obj->write_domain != obj->pending_read_domains ||
3235 obj_priv->ring != ring)) {
Eric Anholt673a3942008-07-30 12:06:12 -07003236 flush_domains |= obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08003237 invalidate_domains |=
3238 obj->pending_read_domains & ~obj->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07003239 }
3240 /*
3241 * Invalidate any read caches which may have
3242 * stale data. That is, any new read domains.
3243 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003244 invalidate_domains |= obj->pending_read_domains & ~obj->read_domains;
Chris Wilson3d2a8122010-09-29 11:39:53 +01003245 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU)
Eric Anholt673a3942008-07-30 12:06:12 -07003246 i915_gem_clflush_object(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003247
Chris Wilson4a684a42010-10-28 14:44:08 +01003248 /* blow away mappings if mapped through GTT */
3249 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_GTT)
3250 i915_gem_release_mmap(obj);
3251
Eric Anholtefbeed92009-02-19 14:54:51 -08003252 /* The actual obj->write_domain will be updated with
3253 * pending_write_domain after we emit the accumulated flush for all
3254 * of our domain changes in execbuffers (which clears objects'
3255 * write_domains). So if we have a current write domain that we
3256 * aren't changing, set pending_write_domain to that.
3257 */
3258 if (flush_domains == 0 && obj->pending_write_domain == 0)
3259 obj->pending_write_domain = obj->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07003260
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003261 cd->invalidate_domains |= invalidate_domains;
3262 cd->flush_domains |= flush_domains;
Chris Wilsonb6651452010-10-23 10:15:06 +01003263 if (flush_domains & I915_GEM_GPU_DOMAINS)
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003264 cd->flush_rings |= obj_priv->ring->id;
Chris Wilsonb6651452010-10-23 10:15:06 +01003265 if (invalidate_domains & I915_GEM_GPU_DOMAINS)
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003266 cd->flush_rings |= ring->id;
Eric Anholt673a3942008-07-30 12:06:12 -07003267}
3268
3269/**
Eric Anholte47c68e2008-11-14 13:35:19 -08003270 * Moves the object from a partially CPU read to a full one.
Eric Anholt673a3942008-07-30 12:06:12 -07003271 *
Eric Anholte47c68e2008-11-14 13:35:19 -08003272 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
3273 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
3274 */
3275static void
3276i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj)
3277{
Daniel Vetter23010e42010-03-08 13:35:02 +01003278 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003279
3280 if (!obj_priv->page_cpu_valid)
3281 return;
3282
3283 /* If we're partially in the CPU read domain, finish moving it in.
3284 */
3285 if (obj->read_domains & I915_GEM_DOMAIN_CPU) {
3286 int i;
3287
3288 for (i = 0; i <= (obj->size - 1) / PAGE_SIZE; i++) {
3289 if (obj_priv->page_cpu_valid[i])
3290 continue;
Eric Anholt856fa192009-03-19 14:10:50 -07003291 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholte47c68e2008-11-14 13:35:19 -08003292 }
Eric Anholte47c68e2008-11-14 13:35:19 -08003293 }
3294
3295 /* Free the page_cpu_valid mappings which are now stale, whether
3296 * or not we've got I915_GEM_DOMAIN_CPU.
3297 */
Eric Anholt9a298b22009-03-24 12:23:04 -07003298 kfree(obj_priv->page_cpu_valid);
Eric Anholte47c68e2008-11-14 13:35:19 -08003299 obj_priv->page_cpu_valid = NULL;
3300}
3301
3302/**
3303 * Set the CPU read domain on a range of the object.
3304 *
3305 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
3306 * not entirely valid. The page_cpu_valid member of the object flags which
3307 * pages have been flushed, and will be respected by
3308 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
3309 * of the whole object.
3310 *
3311 * This function returns when the move is complete, including waiting on
3312 * flushes to occur.
Eric Anholt673a3942008-07-30 12:06:12 -07003313 */
3314static int
Eric Anholte47c68e2008-11-14 13:35:19 -08003315i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
3316 uint64_t offset, uint64_t size)
Eric Anholt673a3942008-07-30 12:06:12 -07003317{
Daniel Vetter23010e42010-03-08 13:35:02 +01003318 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003319 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003320 int i, ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003321
Eric Anholte47c68e2008-11-14 13:35:19 -08003322 if (offset == 0 && size == obj->size)
3323 return i915_gem_object_set_to_cpu_domain(obj, 0);
3324
Daniel Vetterba3d8d72010-02-11 22:37:04 +01003325 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Eric Anholte47c68e2008-11-14 13:35:19 -08003326 if (ret != 0)
3327 return ret;
3328 i915_gem_object_flush_gtt_write_domain(obj);
3329
3330 /* If we're already fully in the CPU read domain, we're done. */
3331 if (obj_priv->page_cpu_valid == NULL &&
3332 (obj->read_domains & I915_GEM_DOMAIN_CPU) != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07003333 return 0;
3334
Eric Anholte47c68e2008-11-14 13:35:19 -08003335 /* Otherwise, create/clear the per-page CPU read domain flag if we're
3336 * newly adding I915_GEM_DOMAIN_CPU
3337 */
Eric Anholt673a3942008-07-30 12:06:12 -07003338 if (obj_priv->page_cpu_valid == NULL) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003339 obj_priv->page_cpu_valid = kzalloc(obj->size / PAGE_SIZE,
3340 GFP_KERNEL);
Eric Anholte47c68e2008-11-14 13:35:19 -08003341 if (obj_priv->page_cpu_valid == NULL)
3342 return -ENOMEM;
3343 } else if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0)
3344 memset(obj_priv->page_cpu_valid, 0, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07003345
3346 /* Flush the cache on any pages that are still invalid from the CPU's
3347 * perspective.
3348 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003349 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
3350 i++) {
Eric Anholt673a3942008-07-30 12:06:12 -07003351 if (obj_priv->page_cpu_valid[i])
3352 continue;
3353
Eric Anholt856fa192009-03-19 14:10:50 -07003354 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07003355
3356 obj_priv->page_cpu_valid[i] = 1;
3357 }
3358
Eric Anholte47c68e2008-11-14 13:35:19 -08003359 /* It should now be out of any other write domains, and we can update
3360 * the domain values for our changes.
3361 */
3362 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
3363
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003364 old_read_domains = obj->read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003365 obj->read_domains |= I915_GEM_DOMAIN_CPU;
3366
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003367 trace_i915_gem_object_change_domain(obj,
3368 old_read_domains,
3369 obj->write_domain);
3370
Eric Anholt673a3942008-07-30 12:06:12 -07003371 return 0;
3372}
3373
Eric Anholt673a3942008-07-30 12:06:12 -07003374static int
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003375i915_gem_execbuffer_relocate_entry(struct drm_i915_gem_object *obj,
3376 struct drm_file *file_priv,
3377 struct drm_i915_gem_exec_object2 *entry,
3378 struct drm_i915_gem_relocation_entry *reloc)
Eric Anholt673a3942008-07-30 12:06:12 -07003379{
Chris Wilson9af90d12010-10-17 10:01:56 +01003380 struct drm_device *dev = obj->base.dev;
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003381 struct drm_gem_object *target_obj;
3382 uint32_t target_offset;
3383 int ret = -EINVAL;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003384
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003385 target_obj = drm_gem_object_lookup(dev, file_priv,
3386 reloc->target_handle);
3387 if (target_obj == NULL)
3388 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07003389
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003390 target_offset = to_intel_bo(target_obj)->gtt_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003391
Chris Wilson8542a0b2009-09-09 21:15:15 +01003392#if WATCH_RELOC
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003393 DRM_INFO("%s: obj %p offset %08x target %d "
3394 "read %08x write %08x gtt %08x "
3395 "presumed %08x delta %08x\n",
3396 __func__,
3397 obj,
3398 (int) reloc->offset,
3399 (int) reloc->target_handle,
3400 (int) reloc->read_domains,
3401 (int) reloc->write_domain,
3402 (int) target_offset,
3403 (int) reloc->presumed_offset,
3404 reloc->delta);
Chris Wilson8542a0b2009-09-09 21:15:15 +01003405#endif
3406
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003407 /* The target buffer should have appeared before us in the
3408 * exec_object list, so it should have a GTT space bound by now.
3409 */
3410 if (target_offset == 0) {
3411 DRM_ERROR("No GTT space found for object %d\n",
3412 reloc->target_handle);
3413 goto err;
Eric Anholt673a3942008-07-30 12:06:12 -07003414 }
3415
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003416 /* Validate that the target is in a valid r/w GPU domain */
3417 if (reloc->write_domain & (reloc->write_domain - 1)) {
3418 DRM_ERROR("reloc with multiple write domains: "
3419 "obj %p target %d offset %d "
3420 "read %08x write %08x",
3421 obj, reloc->target_handle,
3422 (int) reloc->offset,
3423 reloc->read_domains,
3424 reloc->write_domain);
3425 goto err;
3426 }
3427 if (reloc->write_domain & I915_GEM_DOMAIN_CPU ||
3428 reloc->read_domains & I915_GEM_DOMAIN_CPU) {
3429 DRM_ERROR("reloc with read/write CPU domains: "
3430 "obj %p target %d offset %d "
3431 "read %08x write %08x",
3432 obj, reloc->target_handle,
3433 (int) reloc->offset,
3434 reloc->read_domains,
3435 reloc->write_domain);
3436 goto err;
3437 }
3438 if (reloc->write_domain && target_obj->pending_write_domain &&
3439 reloc->write_domain != target_obj->pending_write_domain) {
3440 DRM_ERROR("Write domain conflict: "
3441 "obj %p target %d offset %d "
3442 "new %08x old %08x\n",
3443 obj, reloc->target_handle,
3444 (int) reloc->offset,
3445 reloc->write_domain,
3446 target_obj->pending_write_domain);
3447 goto err;
3448 }
3449
3450 target_obj->pending_read_domains |= reloc->read_domains;
3451 target_obj->pending_write_domain |= reloc->write_domain;
3452
3453 /* If the relocation already has the right value in it, no
3454 * more work needs to be done.
3455 */
3456 if (target_offset == reloc->presumed_offset)
3457 goto out;
3458
3459 /* Check that the relocation address is valid... */
3460 if (reloc->offset > obj->base.size - 4) {
3461 DRM_ERROR("Relocation beyond object bounds: "
3462 "obj %p target %d offset %d size %d.\n",
3463 obj, reloc->target_handle,
3464 (int) reloc->offset,
3465 (int) obj->base.size);
3466 goto err;
3467 }
3468 if (reloc->offset & 3) {
3469 DRM_ERROR("Relocation not 4-byte aligned: "
3470 "obj %p target %d offset %d.\n",
3471 obj, reloc->target_handle,
3472 (int) reloc->offset);
3473 goto err;
3474 }
3475
3476 /* and points to somewhere within the target object. */
3477 if (reloc->delta >= target_obj->size) {
3478 DRM_ERROR("Relocation beyond target object bounds: "
3479 "obj %p target %d delta %d size %d.\n",
3480 obj, reloc->target_handle,
3481 (int) reloc->delta,
3482 (int) target_obj->size);
3483 goto err;
3484 }
3485
3486 reloc->delta += target_offset;
3487 if (obj->base.write_domain == I915_GEM_DOMAIN_CPU) {
3488 uint32_t page_offset = reloc->offset & ~PAGE_MASK;
3489 char *vaddr;
3490
3491 vaddr = kmap_atomic(obj->pages[reloc->offset >> PAGE_SHIFT]);
3492 *(uint32_t *)(vaddr + page_offset) = reloc->delta;
3493 kunmap_atomic(vaddr);
3494 } else {
3495 struct drm_i915_private *dev_priv = dev->dev_private;
3496 uint32_t __iomem *reloc_entry;
3497 void __iomem *reloc_page;
3498
3499 ret = i915_gem_object_set_to_gtt_domain(&obj->base, 1);
3500 if (ret)
3501 goto err;
3502
3503 /* Map the page containing the relocation we're going to perform. */
3504 reloc->offset += obj->gtt_offset;
3505 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
3506 reloc->offset & PAGE_MASK);
3507 reloc_entry = (uint32_t __iomem *)
3508 (reloc_page + (reloc->offset & ~PAGE_MASK));
3509 iowrite32(reloc->delta, reloc_entry);
3510 io_mapping_unmap_atomic(reloc_page);
3511 }
3512
3513 /* and update the user's relocation entry */
3514 reloc->presumed_offset = target_offset;
3515
3516out:
3517 ret = 0;
3518err:
Chris Wilson9af90d12010-10-17 10:01:56 +01003519 drm_gem_object_unreference(target_obj);
3520 return ret;
3521}
3522
3523static int
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003524i915_gem_execbuffer_relocate_object(struct drm_i915_gem_object *obj,
3525 struct drm_file *file_priv,
3526 struct drm_i915_gem_exec_object2 *entry)
3527{
3528 struct drm_i915_gem_relocation_entry __user *user_relocs;
3529 int i, ret;
3530
3531 user_relocs = (void __user *)(uintptr_t)entry->relocs_ptr;
3532 for (i = 0; i < entry->relocation_count; i++) {
3533 struct drm_i915_gem_relocation_entry reloc;
3534
3535 if (__copy_from_user_inatomic(&reloc,
3536 user_relocs+i,
3537 sizeof(reloc)))
3538 return -EFAULT;
3539
3540 ret = i915_gem_execbuffer_relocate_entry(obj, file_priv, entry, &reloc);
3541 if (ret)
3542 return ret;
3543
3544 if (__copy_to_user_inatomic(&user_relocs[i].presumed_offset,
3545 &reloc.presumed_offset,
3546 sizeof(reloc.presumed_offset)))
3547 return -EFAULT;
3548 }
3549
3550 return 0;
3551}
3552
3553static int
3554i915_gem_execbuffer_relocate_object_slow(struct drm_i915_gem_object *obj,
3555 struct drm_file *file_priv,
3556 struct drm_i915_gem_exec_object2 *entry,
3557 struct drm_i915_gem_relocation_entry *relocs)
3558{
3559 int i, ret;
3560
3561 for (i = 0; i < entry->relocation_count; i++) {
3562 ret = i915_gem_execbuffer_relocate_entry(obj, file_priv, entry, &relocs[i]);
3563 if (ret)
3564 return ret;
3565 }
3566
3567 return 0;
3568}
3569
3570static int
3571i915_gem_execbuffer_relocate(struct drm_device *dev,
3572 struct drm_file *file,
3573 struct drm_gem_object **object_list,
3574 struct drm_i915_gem_exec_object2 *exec_list,
3575 int count)
3576{
3577 int i, ret;
3578
3579 for (i = 0; i < count; i++) {
3580 struct drm_i915_gem_object *obj = to_intel_bo(object_list[i]);
3581 obj->base.pending_read_domains = 0;
3582 obj->base.pending_write_domain = 0;
3583 ret = i915_gem_execbuffer_relocate_object(obj, file,
3584 &exec_list[i]);
3585 if (ret)
3586 return ret;
3587 }
3588
3589 return 0;
3590}
3591
3592static int
3593i915_gem_execbuffer_reserve(struct drm_device *dev,
3594 struct drm_file *file,
3595 struct drm_gem_object **object_list,
3596 struct drm_i915_gem_exec_object2 *exec_list,
3597 int count)
Chris Wilson9af90d12010-10-17 10:01:56 +01003598{
3599 struct drm_i915_private *dev_priv = dev->dev_private;
3600 int ret, i, retry;
3601
3602 /* attempt to pin all of the buffers into the GTT */
Chris Wilson5eac3ab2010-10-31 08:49:47 +00003603 retry = 0;
3604 do {
Chris Wilson9af90d12010-10-17 10:01:56 +01003605 ret = 0;
3606 for (i = 0; i < count; i++) {
3607 struct drm_i915_gem_exec_object2 *entry = &exec_list[i];
Daniel Vetter16e809a2010-09-16 19:37:04 +02003608 struct drm_i915_gem_object *obj = to_intel_bo(object_list[i]);
Chris Wilson9af90d12010-10-17 10:01:56 +01003609 bool need_fence =
3610 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
3611 obj->tiling_mode != I915_TILING_NONE;
3612
Daniel Vetter16e809a2010-09-16 19:37:04 +02003613 /* g33/pnv can't fence buffers in the unmappable part */
3614 bool need_mappable =
3615 entry->relocation_count ? true : need_fence;
3616
Chris Wilson9af90d12010-10-17 10:01:56 +01003617 /* Check fence reg constraints and rebind if necessary */
Daniel Vetter75e9e912010-11-04 17:11:09 +01003618 if (need_mappable && !obj->map_and_fenceable) {
Chris Wilson9af90d12010-10-17 10:01:56 +01003619 ret = i915_gem_object_unbind(&obj->base);
3620 if (ret)
3621 break;
3622 }
3623
Daniel Vetter920afa72010-09-16 17:54:23 +02003624 ret = i915_gem_object_pin(&obj->base,
Daniel Vetter16e809a2010-09-16 19:37:04 +02003625 entry->alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01003626 need_mappable);
Chris Wilson9af90d12010-10-17 10:01:56 +01003627 if (ret)
3628 break;
3629
3630 /*
3631 * Pre-965 chips need a fence register set up in order
3632 * to properly handle blits to/from tiled surfaces.
3633 */
3634 if (need_fence) {
3635 ret = i915_gem_object_get_fence_reg(&obj->base, true);
3636 if (ret) {
3637 i915_gem_object_unpin(&obj->base);
3638 break;
3639 }
3640
3641 dev_priv->fence_regs[obj->fence_reg].gpu = true;
3642 }
3643
3644 entry->offset = obj->gtt_offset;
3645 }
3646
3647 while (i--)
3648 i915_gem_object_unpin(object_list[i]);
3649
Chris Wilson5eac3ab2010-10-31 08:49:47 +00003650 if (ret != -ENOSPC || retry > 1)
Chris Wilson9af90d12010-10-17 10:01:56 +01003651 return ret;
3652
Chris Wilson5eac3ab2010-10-31 08:49:47 +00003653 /* First attempt, just clear anything that is purgeable.
3654 * Second attempt, clear the entire GTT.
3655 */
3656 ret = i915_gem_evict_everything(dev, retry == 0);
Chris Wilson9af90d12010-10-17 10:01:56 +01003657 if (ret)
3658 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003659
Chris Wilson5eac3ab2010-10-31 08:49:47 +00003660 retry++;
3661 } while (1);
Eric Anholt673a3942008-07-30 12:06:12 -07003662}
3663
Chris Wilson13b29282010-11-01 12:22:48 +00003664static int
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003665i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
3666 struct drm_file *file,
3667 struct drm_gem_object **object_list,
3668 struct drm_i915_gem_exec_object2 *exec_list,
3669 int count)
3670{
3671 struct drm_i915_gem_relocation_entry *reloc;
3672 int i, total, ret;
3673
3674 for (i = 0; i < count; i++) {
3675 struct drm_i915_gem_object *obj = to_intel_bo(object_list[i]);
3676 obj->in_execbuffer = false;
3677 }
3678
3679 mutex_unlock(&dev->struct_mutex);
3680
3681 total = 0;
3682 for (i = 0; i < count; i++)
3683 total += exec_list[i].relocation_count;
3684
3685 reloc = drm_malloc_ab(total, sizeof(*reloc));
3686 if (reloc == NULL) {
3687 mutex_lock(&dev->struct_mutex);
3688 return -ENOMEM;
3689 }
3690
3691 total = 0;
3692 for (i = 0; i < count; i++) {
3693 struct drm_i915_gem_relocation_entry __user *user_relocs;
3694
3695 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3696
3697 if (copy_from_user(reloc+total, user_relocs,
3698 exec_list[i].relocation_count *
3699 sizeof(*reloc))) {
3700 ret = -EFAULT;
3701 mutex_lock(&dev->struct_mutex);
3702 goto err;
3703 }
3704
3705 total += exec_list[i].relocation_count;
3706 }
3707
3708 ret = i915_mutex_lock_interruptible(dev);
3709 if (ret) {
3710 mutex_lock(&dev->struct_mutex);
3711 goto err;
3712 }
3713
3714 ret = i915_gem_execbuffer_reserve(dev, file,
3715 object_list, exec_list,
3716 count);
3717 if (ret)
3718 goto err;
3719
3720 total = 0;
3721 for (i = 0; i < count; i++) {
3722 struct drm_i915_gem_object *obj = to_intel_bo(object_list[i]);
3723 obj->base.pending_read_domains = 0;
3724 obj->base.pending_write_domain = 0;
3725 ret = i915_gem_execbuffer_relocate_object_slow(obj, file,
3726 &exec_list[i],
3727 reloc + total);
3728 if (ret)
3729 goto err;
3730
3731 total += exec_list[i].relocation_count;
3732 }
3733
3734 /* Leave the user relocations as are, this is the painfully slow path,
3735 * and we want to avoid the complication of dropping the lock whilst
3736 * having buffers reserved in the aperture and so causing spurious
3737 * ENOSPC for random operations.
3738 */
3739
3740err:
3741 drm_free_large(reloc);
3742 return ret;
3743}
3744
3745static int
Chris Wilson13b29282010-11-01 12:22:48 +00003746i915_gem_execbuffer_move_to_gpu(struct drm_device *dev,
3747 struct drm_file *file,
3748 struct intel_ring_buffer *ring,
3749 struct drm_gem_object **objects,
3750 int count)
3751{
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003752 struct change_domains cd;
Chris Wilson13b29282010-11-01 12:22:48 +00003753 int ret, i;
3754
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003755 cd.invalidate_domains = 0;
3756 cd.flush_domains = 0;
3757 cd.flush_rings = 0;
Chris Wilson13b29282010-11-01 12:22:48 +00003758 for (i = 0; i < count; i++)
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003759 i915_gem_object_set_to_gpu_domain(objects[i], ring, &cd);
Chris Wilson13b29282010-11-01 12:22:48 +00003760
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003761 if (cd.invalidate_domains | cd.flush_domains) {
Chris Wilson13b29282010-11-01 12:22:48 +00003762#if WATCH_EXEC
3763 DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
3764 __func__,
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003765 cd.invalidate_domains,
3766 cd.flush_domains);
Chris Wilson13b29282010-11-01 12:22:48 +00003767#endif
3768 i915_gem_flush(dev, file,
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003769 cd.invalidate_domains,
3770 cd.flush_domains,
3771 cd.flush_rings);
Chris Wilson13b29282010-11-01 12:22:48 +00003772 }
3773
3774 for (i = 0; i < count; i++) {
3775 struct drm_i915_gem_object *obj = to_intel_bo(objects[i]);
3776 /* XXX replace with semaphores */
3777 if (obj->ring && ring != obj->ring) {
3778 ret = i915_gem_object_wait_rendering(&obj->base, true);
3779 if (ret)
3780 return ret;
3781 }
3782 }
3783
3784 return 0;
3785}
3786
Eric Anholt673a3942008-07-30 12:06:12 -07003787/* Throttle our rendering by waiting until the ring has completed our requests
3788 * emitted over 20 msec ago.
3789 *
Eric Anholtb9624422009-06-03 07:27:35 +00003790 * Note that if we were to use the current jiffies each time around the loop,
3791 * we wouldn't escape the function with any frames outstanding if the time to
3792 * render a frame was over 20ms.
3793 *
Eric Anholt673a3942008-07-30 12:06:12 -07003794 * This should get us reasonable parallelism between CPU and GPU but also
3795 * relatively low latency when blocking on a particular request to finish.
3796 */
3797static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003798i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003799{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003800 struct drm_i915_private *dev_priv = dev->dev_private;
3801 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00003802 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003803 struct drm_i915_gem_request *request;
3804 struct intel_ring_buffer *ring = NULL;
3805 u32 seqno = 0;
3806 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003807
Chris Wilson1c255952010-09-26 11:03:27 +01003808 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003809 list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
Eric Anholtb9624422009-06-03 07:27:35 +00003810 if (time_after_eq(request->emitted_jiffies, recent_enough))
3811 break;
3812
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003813 ring = request->ring;
3814 seqno = request->seqno;
Eric Anholtb9624422009-06-03 07:27:35 +00003815 }
Chris Wilson1c255952010-09-26 11:03:27 +01003816 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003817
3818 if (seqno == 0)
3819 return 0;
3820
3821 ret = 0;
Chris Wilson78501ea2010-10-27 12:18:21 +01003822 if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) {
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003823 /* And wait for the seqno passing without holding any locks and
3824 * causing extra latency for others. This is safe as the irq
3825 * generation is designed to be run atomically and so is
3826 * lockless.
3827 */
Chris Wilson78501ea2010-10-27 12:18:21 +01003828 ring->user_irq_get(ring);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003829 ret = wait_event_interruptible(ring->irq_queue,
Chris Wilson78501ea2010-10-27 12:18:21 +01003830 i915_seqno_passed(ring->get_seqno(ring), seqno)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003831 || atomic_read(&dev_priv->mm.wedged));
Chris Wilson78501ea2010-10-27 12:18:21 +01003832 ring->user_irq_put(ring);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003833
3834 if (ret == 0 && atomic_read(&dev_priv->mm.wedged))
3835 ret = -EIO;
3836 }
3837
3838 if (ret == 0)
3839 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
Eric Anholtb9624422009-06-03 07:27:35 +00003840
Eric Anholt673a3942008-07-30 12:06:12 -07003841 return ret;
3842}
3843
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003844static int
Chris Wilson2549d6c2010-10-14 12:10:41 +01003845i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec,
3846 uint64_t exec_offset)
Chris Wilson83d60792009-06-06 09:45:57 +01003847{
3848 uint32_t exec_start, exec_len;
3849
3850 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
3851 exec_len = (uint32_t) exec->batch_len;
3852
3853 if ((exec_start | exec_len) & 0x7)
3854 return -EINVAL;
3855
3856 if (!exec_start)
3857 return -EINVAL;
3858
3859 return 0;
3860}
3861
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003862static int
Chris Wilson2549d6c2010-10-14 12:10:41 +01003863validate_exec_list(struct drm_i915_gem_exec_object2 *exec,
3864 int count)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003865{
Chris Wilson2549d6c2010-10-14 12:10:41 +01003866 int i;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003867
Chris Wilson2549d6c2010-10-14 12:10:41 +01003868 for (i = 0; i < count; i++) {
3869 char __user *ptr = (char __user *)(uintptr_t)exec[i].relocs_ptr;
Chris Wilsond1d78832010-11-21 09:23:48 +00003870 int length; /* limited by fault_in_pages_readable() */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003871
Chris Wilsond1d78832010-11-21 09:23:48 +00003872 /* First check for malicious input causing overflow */
3873 if (exec[i].relocation_count >
3874 INT_MAX / sizeof(struct drm_i915_gem_relocation_entry))
3875 return -EINVAL;
3876
3877 length = exec[i].relocation_count *
3878 sizeof(struct drm_i915_gem_relocation_entry);
Chris Wilson2549d6c2010-10-14 12:10:41 +01003879 if (!access_ok(VERIFY_READ, ptr, length))
3880 return -EFAULT;
3881
Chris Wilsonb5dc6082010-10-20 20:59:57 +01003882 /* we may also need to update the presumed offsets */
3883 if (!access_ok(VERIFY_WRITE, ptr, length))
3884 return -EFAULT;
3885
Chris Wilson2549d6c2010-10-14 12:10:41 +01003886 if (fault_in_pages_readable(ptr, length))
3887 return -EFAULT;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003888 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003889
Chris Wilson2549d6c2010-10-14 12:10:41 +01003890 return 0;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003891}
3892
Chris Wilson2549d6c2010-10-14 12:10:41 +01003893static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003894i915_gem_do_execbuffer(struct drm_device *dev, void *data,
Chris Wilson9af90d12010-10-17 10:01:56 +01003895 struct drm_file *file,
Jesse Barnes76446ca2009-12-17 22:05:42 -05003896 struct drm_i915_gem_execbuffer2 *args,
3897 struct drm_i915_gem_exec_object2 *exec_list)
Eric Anholt673a3942008-07-30 12:06:12 -07003898{
3899 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003900 struct drm_gem_object **object_list = NULL;
3901 struct drm_gem_object *batch_obj;
Eric Anholt201361a2009-03-11 12:30:04 -07003902 struct drm_clip_rect *cliprects = NULL;
Chris Wilson8dc5d142010-08-12 12:36:12 +01003903 struct drm_i915_gem_request *request = NULL;
Chris Wilson9af90d12010-10-17 10:01:56 +01003904 int ret, i, flips;
Eric Anholt673a3942008-07-30 12:06:12 -07003905 uint64_t exec_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003906
Zou Nan hai852835f2010-05-21 09:08:56 +08003907 struct intel_ring_buffer *ring = NULL;
3908
Chris Wilson30dbf0c2010-09-25 10:19:17 +01003909 ret = i915_gem_check_is_wedged(dev);
3910 if (ret)
3911 return ret;
3912
Chris Wilson2549d6c2010-10-14 12:10:41 +01003913 ret = validate_exec_list(exec_list, args->buffer_count);
3914 if (ret)
3915 return ret;
3916
Eric Anholt673a3942008-07-30 12:06:12 -07003917#if WATCH_EXEC
3918 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3919 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3920#endif
Chris Wilson549f7362010-10-19 11:19:32 +01003921 switch (args->flags & I915_EXEC_RING_MASK) {
3922 case I915_EXEC_DEFAULT:
3923 case I915_EXEC_RENDER:
3924 ring = &dev_priv->render_ring;
3925 break;
3926 case I915_EXEC_BSD:
Zou Nan haid1b851f2010-05-21 09:08:57 +08003927 if (!HAS_BSD(dev)) {
Chris Wilson549f7362010-10-19 11:19:32 +01003928 DRM_ERROR("execbuf with invalid ring (BSD)\n");
Zou Nan haid1b851f2010-05-21 09:08:57 +08003929 return -EINVAL;
3930 }
3931 ring = &dev_priv->bsd_ring;
Chris Wilson549f7362010-10-19 11:19:32 +01003932 break;
3933 case I915_EXEC_BLT:
3934 if (!HAS_BLT(dev)) {
3935 DRM_ERROR("execbuf with invalid ring (BLT)\n");
3936 return -EINVAL;
3937 }
3938 ring = &dev_priv->blt_ring;
3939 break;
3940 default:
3941 DRM_ERROR("execbuf with unknown ring: %d\n",
3942 (int)(args->flags & I915_EXEC_RING_MASK));
3943 return -EINVAL;
Zou Nan haid1b851f2010-05-21 09:08:57 +08003944 }
3945
Eric Anholt4f481ed2008-09-10 14:22:49 -07003946 if (args->buffer_count < 1) {
3947 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3948 return -EINVAL;
3949 }
Eric Anholtc8e0f932009-11-22 03:49:37 +01003950 object_list = drm_malloc_ab(sizeof(*object_list), args->buffer_count);
Jesse Barnes76446ca2009-12-17 22:05:42 -05003951 if (object_list == NULL) {
3952 DRM_ERROR("Failed to allocate object list for %d buffers\n",
Eric Anholt673a3942008-07-30 12:06:12 -07003953 args->buffer_count);
3954 ret = -ENOMEM;
3955 goto pre_mutex_err;
3956 }
Eric Anholt673a3942008-07-30 12:06:12 -07003957
Eric Anholt201361a2009-03-11 12:30:04 -07003958 if (args->num_cliprects != 0) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003959 cliprects = kcalloc(args->num_cliprects, sizeof(*cliprects),
3960 GFP_KERNEL);
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003961 if (cliprects == NULL) {
3962 ret = -ENOMEM;
Eric Anholt201361a2009-03-11 12:30:04 -07003963 goto pre_mutex_err;
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003964 }
Eric Anholt201361a2009-03-11 12:30:04 -07003965
3966 ret = copy_from_user(cliprects,
3967 (struct drm_clip_rect __user *)
3968 (uintptr_t) args->cliprects_ptr,
3969 sizeof(*cliprects) * args->num_cliprects);
3970 if (ret != 0) {
3971 DRM_ERROR("copy %d cliprects failed: %d\n",
3972 args->num_cliprects, ret);
Dan Carpenterc877cdc2010-06-23 19:03:01 +02003973 ret = -EFAULT;
Eric Anholt201361a2009-03-11 12:30:04 -07003974 goto pre_mutex_err;
3975 }
3976 }
3977
Chris Wilson8dc5d142010-08-12 12:36:12 +01003978 request = kzalloc(sizeof(*request), GFP_KERNEL);
3979 if (request == NULL) {
3980 ret = -ENOMEM;
Chris Wilsona198bc82009-02-06 16:55:20 +00003981 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003982 }
3983
Chris Wilson76c1dec2010-09-25 11:22:51 +01003984 ret = i915_mutex_lock_interruptible(dev);
3985 if (ret)
3986 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003987
Eric Anholt673a3942008-07-30 12:06:12 -07003988 if (dev_priv->mm.suspended) {
Eric Anholt673a3942008-07-30 12:06:12 -07003989 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003990 ret = -EBUSY;
3991 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003992 }
3993
Keith Packardac94a962008-11-20 23:30:27 -08003994 /* Look up object handles */
Eric Anholt673a3942008-07-30 12:06:12 -07003995 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson7e318e12010-10-27 13:43:39 +01003996 struct drm_i915_gem_object *obj_priv;
3997
Chris Wilson9af90d12010-10-17 10:01:56 +01003998 object_list[i] = drm_gem_object_lookup(dev, file,
Eric Anholt673a3942008-07-30 12:06:12 -07003999 exec_list[i].handle);
4000 if (object_list[i] == NULL) {
4001 DRM_ERROR("Invalid object handle %d at index %d\n",
4002 exec_list[i].handle, i);
Chris Wilson0ce907f2010-01-23 20:26:35 +00004003 /* prevent error path from reading uninitialized data */
4004 args->buffer_count = i + 1;
Chris Wilsonbf79cb92010-08-04 14:19:46 +01004005 ret = -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07004006 goto err;
4007 }
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05004008
Daniel Vetter23010e42010-03-08 13:35:02 +01004009 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05004010 if (obj_priv->in_execbuffer) {
4011 DRM_ERROR("Object %p appears more than once in object list\n",
4012 object_list[i]);
Chris Wilson0ce907f2010-01-23 20:26:35 +00004013 /* prevent error path from reading uninitialized data */
4014 args->buffer_count = i + 1;
Chris Wilsonbf79cb92010-08-04 14:19:46 +01004015 ret = -EINVAL;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05004016 goto err;
4017 }
4018 obj_priv->in_execbuffer = true;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05004019 }
4020
Chris Wilson9af90d12010-10-17 10:01:56 +01004021 /* Move the objects en-masse into the GTT, evicting if necessary. */
Chris Wilsonbcf50e22010-11-21 22:07:12 +00004022 ret = i915_gem_execbuffer_reserve(dev, file,
4023 object_list, exec_list,
4024 args->buffer_count);
Chris Wilson9af90d12010-10-17 10:01:56 +01004025 if (ret)
4026 goto err;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07004027
Chris Wilson9af90d12010-10-17 10:01:56 +01004028 /* The objects are in their final locations, apply the relocations. */
Chris Wilsonbcf50e22010-11-21 22:07:12 +00004029 ret = i915_gem_execbuffer_relocate(dev, file,
4030 object_list, exec_list,
4031 args->buffer_count);
4032 if (ret) {
4033 if (ret == -EFAULT) {
4034 ret = i915_gem_execbuffer_relocate_slow(dev, file,
4035 object_list,
4036 exec_list,
4037 args->buffer_count);
4038 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
4039 }
Eric Anholt673a3942008-07-30 12:06:12 -07004040 if (ret)
4041 goto err;
4042 }
4043
Eric Anholt673a3942008-07-30 12:06:12 -07004044 /* Set the pending read domains for the batch buffer to COMMAND */
4045 batch_obj = object_list[args->buffer_count-1];
Chris Wilson5f26a2c2009-06-06 09:45:58 +01004046 if (batch_obj->pending_write_domain) {
4047 DRM_ERROR("Attempting to use self-modifying batch buffer\n");
4048 ret = -EINVAL;
4049 goto err;
4050 }
4051 batch_obj->pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
Eric Anholt673a3942008-07-30 12:06:12 -07004052
Chris Wilson9af90d12010-10-17 10:01:56 +01004053 /* Sanity check the batch buffer */
4054 exec_offset = to_intel_bo(batch_obj)->gtt_offset;
4055 ret = i915_gem_check_execbuffer(args, exec_offset);
Chris Wilson83d60792009-06-06 09:45:57 +01004056 if (ret != 0) {
4057 DRM_ERROR("execbuf with invalid offset/length\n");
4058 goto err;
4059 }
4060
Chris Wilson13b29282010-11-01 12:22:48 +00004061 ret = i915_gem_execbuffer_move_to_gpu(dev, file, ring,
4062 object_list, args->buffer_count);
4063 if (ret)
4064 goto err;
Eric Anholt673a3942008-07-30 12:06:12 -07004065
Eric Anholt673a3942008-07-30 12:06:12 -07004066#if WATCH_COHERENCY
4067 for (i = 0; i < args->buffer_count; i++) {
4068 i915_gem_object_check_coherency(object_list[i],
4069 exec_list[i].handle);
4070 }
4071#endif
4072
Eric Anholt673a3942008-07-30 12:06:12 -07004073#if WATCH_EXEC
Ben Gamari6911a9b2009-04-02 11:24:54 -07004074 i915_gem_dump_object(batch_obj,
Eric Anholt673a3942008-07-30 12:06:12 -07004075 args->batch_len,
4076 __func__,
4077 ~0);
4078#endif
4079
Chris Wilsone59f2ba2010-10-07 17:28:15 +01004080 /* Check for any pending flips. As we only maintain a flip queue depth
4081 * of 1, we can simply insert a WAIT for the next display flip prior
4082 * to executing the batch and avoid stalling the CPU.
4083 */
4084 flips = 0;
4085 for (i = 0; i < args->buffer_count; i++) {
4086 if (object_list[i]->write_domain)
4087 flips |= atomic_read(&to_intel_bo(object_list[i])->pending_flip);
4088 }
4089 if (flips) {
4090 int plane, flip_mask;
4091
4092 for (plane = 0; flips >> plane; plane++) {
4093 if (((flips >> plane) & 1) == 0)
4094 continue;
4095
4096 if (plane)
4097 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
4098 else
4099 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
4100
Chris Wilsone1f99ce2010-10-27 12:45:26 +01004101 ret = intel_ring_begin(ring, 2);
4102 if (ret)
4103 goto err;
4104
Chris Wilson78501ea2010-10-27 12:18:21 +01004105 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
4106 intel_ring_emit(ring, MI_NOOP);
4107 intel_ring_advance(ring);
Chris Wilsone59f2ba2010-10-07 17:28:15 +01004108 }
4109 }
4110
Eric Anholt673a3942008-07-30 12:06:12 -07004111 /* Exec the batchbuffer */
Chris Wilson78501ea2010-10-27 12:18:21 +01004112 ret = ring->dispatch_execbuffer(ring, args, cliprects, exec_offset);
Eric Anholt673a3942008-07-30 12:06:12 -07004113 if (ret) {
4114 DRM_ERROR("dispatch failed %d\n", ret);
4115 goto err;
4116 }
4117
Chris Wilson7e318e12010-10-27 13:43:39 +01004118 for (i = 0; i < args->buffer_count; i++) {
4119 struct drm_gem_object *obj = object_list[i];
4120
4121 obj->read_domains = obj->pending_read_domains;
4122 obj->write_domain = obj->pending_write_domain;
4123
4124 i915_gem_object_move_to_active(obj, ring);
4125 if (obj->write_domain) {
4126 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
4127 obj_priv->dirty = 1;
4128 list_move_tail(&obj_priv->gpu_write_list,
4129 &ring->gpu_write_list);
4130 intel_mark_busy(dev, obj);
4131 }
4132
4133 trace_i915_gem_object_change_domain(obj,
4134 obj->read_domains,
4135 obj->write_domain);
4136 }
4137
Eric Anholt673a3942008-07-30 12:06:12 -07004138 /*
4139 * Ensure that the commands in the batch buffer are
4140 * finished before the interrupt fires
4141 */
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01004142 i915_retire_commands(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07004143
Chris Wilson3cce4692010-10-27 16:11:02 +01004144 if (i915_add_request(dev, file, request, ring))
Chris Wilson5d97eb62010-11-10 20:40:02 +00004145 i915_gem_next_request_seqno(dev, ring);
Chris Wilson3cce4692010-10-27 16:11:02 +01004146 else
4147 request = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07004148
Eric Anholt673a3942008-07-30 12:06:12 -07004149err:
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05004150 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson7e318e12010-10-27 13:43:39 +01004151 if (object_list[i] == NULL)
4152 break;
4153
4154 to_intel_bo(object_list[i])->in_execbuffer = false;
Julia Lawallaad87df2008-12-21 16:28:47 +01004155 drm_gem_object_unreference(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05004156 }
Julia Lawallaad87df2008-12-21 16:28:47 +01004157
Eric Anholt673a3942008-07-30 12:06:12 -07004158 mutex_unlock(&dev->struct_mutex);
4159
Chris Wilson93533c22010-01-31 10:40:48 +00004160pre_mutex_err:
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07004161 drm_free_large(object_list);
Eric Anholt9a298b22009-03-24 12:23:04 -07004162 kfree(cliprects);
Chris Wilson8dc5d142010-08-12 12:36:12 +01004163 kfree(request);
Eric Anholt673a3942008-07-30 12:06:12 -07004164
4165 return ret;
4166}
4167
Jesse Barnes76446ca2009-12-17 22:05:42 -05004168/*
4169 * Legacy execbuffer just creates an exec2 list from the original exec object
4170 * list array and passes it to the real function.
4171 */
4172int
4173i915_gem_execbuffer(struct drm_device *dev, void *data,
4174 struct drm_file *file_priv)
4175{
4176 struct drm_i915_gem_execbuffer *args = data;
4177 struct drm_i915_gem_execbuffer2 exec2;
4178 struct drm_i915_gem_exec_object *exec_list = NULL;
4179 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
4180 int ret, i;
4181
4182#if WATCH_EXEC
4183 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
4184 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
4185#endif
4186
4187 if (args->buffer_count < 1) {
4188 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
4189 return -EINVAL;
4190 }
4191
4192 /* Copy in the exec list from userland */
4193 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
4194 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
4195 if (exec_list == NULL || exec2_list == NULL) {
4196 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
4197 args->buffer_count);
4198 drm_free_large(exec_list);
4199 drm_free_large(exec2_list);
4200 return -ENOMEM;
4201 }
4202 ret = copy_from_user(exec_list,
4203 (struct drm_i915_relocation_entry __user *)
4204 (uintptr_t) args->buffers_ptr,
4205 sizeof(*exec_list) * args->buffer_count);
4206 if (ret != 0) {
4207 DRM_ERROR("copy %d exec entries failed %d\n",
4208 args->buffer_count, ret);
4209 drm_free_large(exec_list);
4210 drm_free_large(exec2_list);
4211 return -EFAULT;
4212 }
4213
4214 for (i = 0; i < args->buffer_count; i++) {
4215 exec2_list[i].handle = exec_list[i].handle;
4216 exec2_list[i].relocation_count = exec_list[i].relocation_count;
4217 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
4218 exec2_list[i].alignment = exec_list[i].alignment;
4219 exec2_list[i].offset = exec_list[i].offset;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004220 if (INTEL_INFO(dev)->gen < 4)
Jesse Barnes76446ca2009-12-17 22:05:42 -05004221 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
4222 else
4223 exec2_list[i].flags = 0;
4224 }
4225
4226 exec2.buffers_ptr = args->buffers_ptr;
4227 exec2.buffer_count = args->buffer_count;
4228 exec2.batch_start_offset = args->batch_start_offset;
4229 exec2.batch_len = args->batch_len;
4230 exec2.DR1 = args->DR1;
4231 exec2.DR4 = args->DR4;
4232 exec2.num_cliprects = args->num_cliprects;
4233 exec2.cliprects_ptr = args->cliprects_ptr;
Zou Nan hai852835f2010-05-21 09:08:56 +08004234 exec2.flags = I915_EXEC_RENDER;
Jesse Barnes76446ca2009-12-17 22:05:42 -05004235
4236 ret = i915_gem_do_execbuffer(dev, data, file_priv, &exec2, exec2_list);
4237 if (!ret) {
4238 /* Copy the new buffer offsets back to the user's exec list. */
4239 for (i = 0; i < args->buffer_count; i++)
4240 exec_list[i].offset = exec2_list[i].offset;
4241 /* ... and back out to userspace */
4242 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
4243 (uintptr_t) args->buffers_ptr,
4244 exec_list,
4245 sizeof(*exec_list) * args->buffer_count);
4246 if (ret) {
4247 ret = -EFAULT;
4248 DRM_ERROR("failed to copy %d exec entries "
4249 "back to user (%d)\n",
4250 args->buffer_count, ret);
4251 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05004252 }
4253
4254 drm_free_large(exec_list);
4255 drm_free_large(exec2_list);
4256 return ret;
4257}
4258
4259int
4260i915_gem_execbuffer2(struct drm_device *dev, void *data,
4261 struct drm_file *file_priv)
4262{
4263 struct drm_i915_gem_execbuffer2 *args = data;
4264 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
4265 int ret;
4266
4267#if WATCH_EXEC
4268 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
4269 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
4270#endif
4271
4272 if (args->buffer_count < 1) {
4273 DRM_ERROR("execbuf2 with %d buffers\n", args->buffer_count);
4274 return -EINVAL;
4275 }
4276
4277 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
4278 if (exec2_list == NULL) {
4279 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
4280 args->buffer_count);
4281 return -ENOMEM;
4282 }
4283 ret = copy_from_user(exec2_list,
4284 (struct drm_i915_relocation_entry __user *)
4285 (uintptr_t) args->buffers_ptr,
4286 sizeof(*exec2_list) * args->buffer_count);
4287 if (ret != 0) {
4288 DRM_ERROR("copy %d exec entries failed %d\n",
4289 args->buffer_count, ret);
4290 drm_free_large(exec2_list);
4291 return -EFAULT;
4292 }
4293
4294 ret = i915_gem_do_execbuffer(dev, data, file_priv, args, exec2_list);
4295 if (!ret) {
4296 /* Copy the new buffer offsets back to the user's exec list. */
4297 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
4298 (uintptr_t) args->buffers_ptr,
4299 exec2_list,
4300 sizeof(*exec2_list) * args->buffer_count);
4301 if (ret) {
4302 ret = -EFAULT;
4303 DRM_ERROR("failed to copy %d exec entries "
4304 "back to user (%d)\n",
4305 args->buffer_count, ret);
4306 }
4307 }
4308
4309 drm_free_large(exec2_list);
4310 return ret;
4311}
4312
Eric Anholt673a3942008-07-30 12:06:12 -07004313int
Daniel Vetter920afa72010-09-16 17:54:23 +02004314i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01004315 bool map_and_fenceable)
Eric Anholt673a3942008-07-30 12:06:12 -07004316{
4317 struct drm_device *dev = obj->dev;
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004318 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01004319 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004320 int ret;
4321
Daniel Vetter778c3542010-05-13 11:49:44 +02004322 BUG_ON(obj_priv->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT);
Daniel Vetter75e9e912010-11-04 17:11:09 +01004323 BUG_ON(map_and_fenceable && !map_and_fenceable);
Chris Wilson23bc5982010-09-29 16:10:57 +01004324 WARN_ON(i915_verify_lists(dev));
Chris Wilsonac0c6b52010-05-27 13:18:18 +01004325
4326 if (obj_priv->gtt_space != NULL) {
Chris Wilsona00b10c2010-09-24 21:15:47 +01004327 if ((alignment && obj_priv->gtt_offset & (alignment - 1)) ||
Daniel Vetter75e9e912010-11-04 17:11:09 +01004328 (map_and_fenceable && !obj_priv->map_and_fenceable)) {
Chris Wilsonae7d49d2010-08-04 12:37:41 +01004329 WARN(obj_priv->pin_count,
4330 "bo is already pinned with incorrect alignment:"
Daniel Vetter75e9e912010-11-04 17:11:09 +01004331 " offset=%x, req.alignment=%x, req.map_and_fenceable=%d,"
4332 " obj->map_and_fenceable=%d\n",
Chris Wilsona00b10c2010-09-24 21:15:47 +01004333 obj_priv->gtt_offset, alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01004334 map_and_fenceable,
4335 obj_priv->map_and_fenceable);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01004336 ret = i915_gem_object_unbind(obj);
4337 if (ret)
4338 return ret;
4339 }
4340 }
4341
Eric Anholt673a3942008-07-30 12:06:12 -07004342 if (obj_priv->gtt_space == NULL) {
Chris Wilsona00b10c2010-09-24 21:15:47 +01004343 ret = i915_gem_object_bind_to_gtt(obj, alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01004344 map_and_fenceable);
Chris Wilson97311292009-09-21 00:22:34 +01004345 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07004346 return ret;
Chris Wilson22c344e2009-02-11 14:26:45 +00004347 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05004348
Chris Wilson74653782010-10-29 10:41:23 +01004349 if (obj_priv->pin_count++ == 0) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01004350 i915_gem_info_add_pin(dev_priv, obj_priv, map_and_fenceable);
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004351 if (!obj_priv->active)
Chris Wilson69dc4982010-10-19 10:36:51 +01004352 list_move_tail(&obj_priv->mm_list,
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004353 &dev_priv->mm.pinned_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004354 }
Daniel Vetter75e9e912010-11-04 17:11:09 +01004355 BUG_ON(!obj_priv->pin_mappable && map_and_fenceable);
Eric Anholt673a3942008-07-30 12:06:12 -07004356
Chris Wilson23bc5982010-09-29 16:10:57 +01004357 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07004358 return 0;
4359}
4360
4361void
4362i915_gem_object_unpin(struct drm_gem_object *obj)
4363{
4364 struct drm_device *dev = obj->dev;
4365 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01004366 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004367
Chris Wilson23bc5982010-09-29 16:10:57 +01004368 WARN_ON(i915_verify_lists(dev));
Chris Wilson74653782010-10-29 10:41:23 +01004369 BUG_ON(obj_priv->pin_count == 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004370 BUG_ON(obj_priv->gtt_space == NULL);
4371
Chris Wilson74653782010-10-29 10:41:23 +01004372 if (--obj_priv->pin_count == 0) {
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004373 if (!obj_priv->active)
Chris Wilson69dc4982010-10-19 10:36:51 +01004374 list_move_tail(&obj_priv->mm_list,
Eric Anholt673a3942008-07-30 12:06:12 -07004375 &dev_priv->mm.inactive_list);
Chris Wilsona00b10c2010-09-24 21:15:47 +01004376 i915_gem_info_remove_pin(dev_priv, obj_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07004377 }
Chris Wilson23bc5982010-09-29 16:10:57 +01004378 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07004379}
4380
4381int
4382i915_gem_pin_ioctl(struct drm_device *dev, void *data,
4383 struct drm_file *file_priv)
4384{
4385 struct drm_i915_gem_pin *args = data;
4386 struct drm_gem_object *obj;
4387 struct drm_i915_gem_object *obj_priv;
4388 int ret;
4389
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004390 ret = i915_mutex_lock_interruptible(dev);
4391 if (ret)
4392 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004393
4394 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4395 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004396 ret = -ENOENT;
4397 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07004398 }
Daniel Vetter23010e42010-03-08 13:35:02 +01004399 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004400
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004401 if (obj_priv->madv != I915_MADV_WILLNEED) {
4402 DRM_ERROR("Attempting to pin a purgeable buffer\n");
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004403 ret = -EINVAL;
4404 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004405 }
4406
Jesse Barnes79e53942008-11-07 14:24:08 -08004407 if (obj_priv->pin_filp != NULL && obj_priv->pin_filp != file_priv) {
4408 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
4409 args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004410 ret = -EINVAL;
4411 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08004412 }
4413
4414 obj_priv->user_pin_count++;
4415 obj_priv->pin_filp = file_priv;
4416 if (obj_priv->user_pin_count == 1) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01004417 ret = i915_gem_object_pin(obj, args->alignment, true);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004418 if (ret)
4419 goto out;
Eric Anholt673a3942008-07-30 12:06:12 -07004420 }
4421
4422 /* XXX - flush the CPU caches for pinned objects
4423 * as the X server doesn't manage domains yet
4424 */
Eric Anholte47c68e2008-11-14 13:35:19 -08004425 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004426 args->offset = obj_priv->gtt_offset;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004427out:
Eric Anholt673a3942008-07-30 12:06:12 -07004428 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004429unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07004430 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004431 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004432}
4433
4434int
4435i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
4436 struct drm_file *file_priv)
4437{
4438 struct drm_i915_gem_pin *args = data;
4439 struct drm_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08004440 struct drm_i915_gem_object *obj_priv;
Chris Wilson76c1dec2010-09-25 11:22:51 +01004441 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004442
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004443 ret = i915_mutex_lock_interruptible(dev);
4444 if (ret)
4445 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004446
4447 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4448 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004449 ret = -ENOENT;
4450 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07004451 }
Daniel Vetter23010e42010-03-08 13:35:02 +01004452 obj_priv = to_intel_bo(obj);
Chris Wilson76c1dec2010-09-25 11:22:51 +01004453
Jesse Barnes79e53942008-11-07 14:24:08 -08004454 if (obj_priv->pin_filp != file_priv) {
4455 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
4456 args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004457 ret = -EINVAL;
4458 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08004459 }
4460 obj_priv->user_pin_count--;
4461 if (obj_priv->user_pin_count == 0) {
4462 obj_priv->pin_filp = NULL;
4463 i915_gem_object_unpin(obj);
4464 }
Eric Anholt673a3942008-07-30 12:06:12 -07004465
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004466out:
Eric Anholt673a3942008-07-30 12:06:12 -07004467 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004468unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07004469 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004470 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004471}
4472
4473int
4474i915_gem_busy_ioctl(struct drm_device *dev, void *data,
4475 struct drm_file *file_priv)
4476{
4477 struct drm_i915_gem_busy *args = data;
4478 struct drm_gem_object *obj;
4479 struct drm_i915_gem_object *obj_priv;
Chris Wilson30dbf0c2010-09-25 10:19:17 +01004480 int ret;
4481
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004482 ret = i915_mutex_lock_interruptible(dev);
4483 if (ret)
4484 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004485
Eric Anholt673a3942008-07-30 12:06:12 -07004486 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4487 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004488 ret = -ENOENT;
4489 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07004490 }
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004491 obj_priv = to_intel_bo(obj);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004492
Chris Wilson0be555b2010-08-04 15:36:30 +01004493 /* Count all active objects as busy, even if they are currently not used
4494 * by the gpu. Users of this interface expect objects to eventually
4495 * become non-busy without any further actions, therefore emit any
4496 * necessary flushes here.
Eric Anholtc4de0a52008-12-14 19:05:04 -08004497 */
Chris Wilson0be555b2010-08-04 15:36:30 +01004498 args->busy = obj_priv->active;
4499 if (args->busy) {
4500 /* Unconditionally flush objects, even when the gpu still uses this
4501 * object. Userspace calling this function indicates that it wants to
4502 * use this buffer rather sooner than later, so issuing the required
4503 * flush earlier is beneficial.
4504 */
Chris Wilsonc78ec302010-09-20 12:50:23 +01004505 if (obj->write_domain & I915_GEM_GPU_DOMAINS)
4506 i915_gem_flush_ring(dev, file_priv,
Chris Wilson92204342010-09-18 11:02:01 +01004507 obj_priv->ring,
4508 0, obj->write_domain);
Chris Wilson0be555b2010-08-04 15:36:30 +01004509
4510 /* Update the active list for the hardware's current position.
4511 * Otherwise this only updates on a delayed timer or when irqs
4512 * are actually unmasked, and our working set ends up being
4513 * larger than required.
4514 */
4515 i915_gem_retire_requests_ring(dev, obj_priv->ring);
4516
4517 args->busy = obj_priv->active;
4518 }
Eric Anholt673a3942008-07-30 12:06:12 -07004519
4520 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004521unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07004522 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004523 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004524}
4525
4526int
4527i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4528 struct drm_file *file_priv)
4529{
4530 return i915_gem_ring_throttle(dev, file_priv);
4531}
4532
Chris Wilson3ef94da2009-09-14 16:50:29 +01004533int
4534i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4535 struct drm_file *file_priv)
4536{
4537 struct drm_i915_gem_madvise *args = data;
4538 struct drm_gem_object *obj;
4539 struct drm_i915_gem_object *obj_priv;
Chris Wilson76c1dec2010-09-25 11:22:51 +01004540 int ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004541
4542 switch (args->madv) {
4543 case I915_MADV_DONTNEED:
4544 case I915_MADV_WILLNEED:
4545 break;
4546 default:
4547 return -EINVAL;
4548 }
4549
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004550 ret = i915_mutex_lock_interruptible(dev);
4551 if (ret)
4552 return ret;
4553
Chris Wilson3ef94da2009-09-14 16:50:29 +01004554 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4555 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004556 ret = -ENOENT;
4557 goto unlock;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004558 }
Daniel Vetter23010e42010-03-08 13:35:02 +01004559 obj_priv = to_intel_bo(obj);
Chris Wilson3ef94da2009-09-14 16:50:29 +01004560
4561 if (obj_priv->pin_count) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004562 ret = -EINVAL;
4563 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004564 }
4565
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004566 if (obj_priv->madv != __I915_MADV_PURGED)
4567 obj_priv->madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004568
Chris Wilson2d7ef392009-09-20 23:13:10 +01004569 /* if the object is no longer bound, discard its backing storage */
4570 if (i915_gem_object_is_purgeable(obj_priv) &&
4571 obj_priv->gtt_space == NULL)
4572 i915_gem_object_truncate(obj);
4573
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004574 args->retained = obj_priv->madv != __I915_MADV_PURGED;
4575
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004576out:
Chris Wilson3ef94da2009-09-14 16:50:29 +01004577 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004578unlock:
Chris Wilson3ef94da2009-09-14 16:50:29 +01004579 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004580 return ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004581}
4582
Daniel Vetterac52bc52010-04-09 19:05:06 +00004583struct drm_gem_object * i915_gem_alloc_object(struct drm_device *dev,
4584 size_t size)
4585{
Chris Wilson73aa8082010-09-30 11:46:12 +01004586 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc397b902010-04-09 19:05:07 +00004587 struct drm_i915_gem_object *obj;
4588
4589 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
4590 if (obj == NULL)
4591 return NULL;
4592
4593 if (drm_gem_object_init(dev, &obj->base, size) != 0) {
4594 kfree(obj);
4595 return NULL;
4596 }
4597
Chris Wilson73aa8082010-09-30 11:46:12 +01004598 i915_gem_info_add_obj(dev_priv, size);
4599
Daniel Vetterc397b902010-04-09 19:05:07 +00004600 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4601 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4602
4603 obj->agp_type = AGP_USER_MEMORY;
Daniel Vetter62b8b212010-04-09 19:05:08 +00004604 obj->base.driver_private = NULL;
Daniel Vetterc397b902010-04-09 19:05:07 +00004605 obj->fence_reg = I915_FENCE_REG_NONE;
Chris Wilson69dc4982010-10-19 10:36:51 +01004606 INIT_LIST_HEAD(&obj->mm_list);
4607 INIT_LIST_HEAD(&obj->ring_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00004608 INIT_LIST_HEAD(&obj->gpu_write_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00004609 obj->madv = I915_MADV_WILLNEED;
Daniel Vetter75e9e912010-11-04 17:11:09 +01004610 /* Avoid an unnecessary call to unbind on the first bind. */
4611 obj->map_and_fenceable = true;
Daniel Vetterc397b902010-04-09 19:05:07 +00004612
Daniel Vetterc397b902010-04-09 19:05:07 +00004613 return &obj->base;
Daniel Vetterac52bc52010-04-09 19:05:06 +00004614}
4615
Eric Anholt673a3942008-07-30 12:06:12 -07004616int i915_gem_init_object(struct drm_gem_object *obj)
4617{
Daniel Vetterc397b902010-04-09 19:05:07 +00004618 BUG();
Jesse Barnesde151cf2008-11-12 10:03:55 -08004619
Eric Anholt673a3942008-07-30 12:06:12 -07004620 return 0;
4621}
4622
Chris Wilsonbe726152010-07-23 23:18:50 +01004623static void i915_gem_free_object_tail(struct drm_gem_object *obj)
4624{
4625 struct drm_device *dev = obj->dev;
4626 drm_i915_private_t *dev_priv = dev->dev_private;
4627 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
4628 int ret;
4629
4630 ret = i915_gem_object_unbind(obj);
4631 if (ret == -ERESTARTSYS) {
Chris Wilson69dc4982010-10-19 10:36:51 +01004632 list_move(&obj_priv->mm_list,
Chris Wilsonbe726152010-07-23 23:18:50 +01004633 &dev_priv->mm.deferred_free_list);
4634 return;
4635 }
4636
Chris Wilson39a01d12010-10-28 13:03:06 +01004637 if (obj->map_list.map)
Chris Wilsonbe726152010-07-23 23:18:50 +01004638 i915_gem_free_mmap_offset(obj);
4639
4640 drm_gem_object_release(obj);
Chris Wilson73aa8082010-09-30 11:46:12 +01004641 i915_gem_info_remove_obj(dev_priv, obj->size);
Chris Wilsonbe726152010-07-23 23:18:50 +01004642
4643 kfree(obj_priv->page_cpu_valid);
4644 kfree(obj_priv->bit_17);
4645 kfree(obj_priv);
4646}
4647
Eric Anholt673a3942008-07-30 12:06:12 -07004648void i915_gem_free_object(struct drm_gem_object *obj)
4649{
Jesse Barnesde151cf2008-11-12 10:03:55 -08004650 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01004651 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004652
Chris Wilson1c5d22f2009-08-25 11:15:50 +01004653 trace_i915_gem_object_destroy(obj);
4654
Eric Anholt673a3942008-07-30 12:06:12 -07004655 while (obj_priv->pin_count > 0)
4656 i915_gem_object_unpin(obj);
4657
Dave Airlie71acb5e2008-12-30 20:31:46 +10004658 if (obj_priv->phys_obj)
4659 i915_gem_detach_phys_object(dev, obj);
4660
Chris Wilsonbe726152010-07-23 23:18:50 +01004661 i915_gem_free_object_tail(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004662}
4663
Jesse Barnes5669fca2009-02-17 15:13:31 -08004664int
Eric Anholt673a3942008-07-30 12:06:12 -07004665i915_gem_idle(struct drm_device *dev)
4666{
4667 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson29105cc2010-01-07 10:39:13 +00004668 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004669
Keith Packard6dbe2772008-10-14 21:41:13 -07004670 mutex_lock(&dev->struct_mutex);
4671
Chris Wilson87acb0a2010-10-19 10:13:00 +01004672 if (dev_priv->mm.suspended) {
Keith Packard6dbe2772008-10-14 21:41:13 -07004673 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004674 return 0;
Keith Packard6dbe2772008-10-14 21:41:13 -07004675 }
Eric Anholt673a3942008-07-30 12:06:12 -07004676
Chris Wilson29105cc2010-01-07 10:39:13 +00004677 ret = i915_gpu_idle(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004678 if (ret) {
4679 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004680 return ret;
Keith Packard6dbe2772008-10-14 21:41:13 -07004681 }
Eric Anholt673a3942008-07-30 12:06:12 -07004682
Chris Wilson29105cc2010-01-07 10:39:13 +00004683 /* Under UMS, be paranoid and evict. */
4684 if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
Chris Wilson5eac3ab2010-10-31 08:49:47 +00004685 ret = i915_gem_evict_inactive(dev, false);
Chris Wilson29105cc2010-01-07 10:39:13 +00004686 if (ret) {
4687 mutex_unlock(&dev->struct_mutex);
4688 return ret;
4689 }
4690 }
4691
4692 /* Hack! Don't let anybody do execbuf while we don't control the chip.
4693 * We need to replace this with a semaphore, or something.
4694 * And not confound mm.suspended!
4695 */
4696 dev_priv->mm.suspended = 1;
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02004697 del_timer_sync(&dev_priv->hangcheck_timer);
Chris Wilson29105cc2010-01-07 10:39:13 +00004698
4699 i915_kernel_lost_context(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004700 i915_gem_cleanup_ringbuffer(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00004701
Keith Packard6dbe2772008-10-14 21:41:13 -07004702 mutex_unlock(&dev->struct_mutex);
4703
Chris Wilson29105cc2010-01-07 10:39:13 +00004704 /* Cancel the retire work handler, which should be idle now. */
4705 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
4706
Eric Anholt673a3942008-07-30 12:06:12 -07004707 return 0;
4708}
4709
Jesse Barnese552eb72010-04-21 11:39:23 -07004710/*
4711 * 965+ support PIPE_CONTROL commands, which provide finer grained control
4712 * over cache flushing.
4713 */
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004714static int
Jesse Barnese552eb72010-04-21 11:39:23 -07004715i915_gem_init_pipe_control(struct drm_device *dev)
4716{
4717 drm_i915_private_t *dev_priv = dev->dev_private;
4718 struct drm_gem_object *obj;
4719 struct drm_i915_gem_object *obj_priv;
4720 int ret;
4721
Eric Anholt34dc4d42010-05-07 14:30:03 -07004722 obj = i915_gem_alloc_object(dev, 4096);
Jesse Barnese552eb72010-04-21 11:39:23 -07004723 if (obj == NULL) {
4724 DRM_ERROR("Failed to allocate seqno page\n");
4725 ret = -ENOMEM;
4726 goto err;
4727 }
4728 obj_priv = to_intel_bo(obj);
4729 obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
4730
Daniel Vetter75e9e912010-11-04 17:11:09 +01004731 ret = i915_gem_object_pin(obj, 4096, true);
Jesse Barnese552eb72010-04-21 11:39:23 -07004732 if (ret)
4733 goto err_unref;
4734
4735 dev_priv->seqno_gfx_addr = obj_priv->gtt_offset;
4736 dev_priv->seqno_page = kmap(obj_priv->pages[0]);
4737 if (dev_priv->seqno_page == NULL)
4738 goto err_unpin;
4739
4740 dev_priv->seqno_obj = obj;
4741 memset(dev_priv->seqno_page, 0, PAGE_SIZE);
4742
4743 return 0;
4744
4745err_unpin:
4746 i915_gem_object_unpin(obj);
4747err_unref:
4748 drm_gem_object_unreference(obj);
4749err:
4750 return ret;
4751}
4752
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004753
4754static void
Jesse Barnese552eb72010-04-21 11:39:23 -07004755i915_gem_cleanup_pipe_control(struct drm_device *dev)
4756{
4757 drm_i915_private_t *dev_priv = dev->dev_private;
4758 struct drm_gem_object *obj;
4759 struct drm_i915_gem_object *obj_priv;
4760
4761 obj = dev_priv->seqno_obj;
4762 obj_priv = to_intel_bo(obj);
4763 kunmap(obj_priv->pages[0]);
4764 i915_gem_object_unpin(obj);
4765 drm_gem_object_unreference(obj);
4766 dev_priv->seqno_obj = NULL;
4767
4768 dev_priv->seqno_page = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07004769}
4770
Eric Anholt673a3942008-07-30 12:06:12 -07004771int
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004772i915_gem_init_ringbuffer(struct drm_device *dev)
4773{
4774 drm_i915_private_t *dev_priv = dev->dev_private;
4775 int ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01004776
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004777 if (HAS_PIPE_CONTROL(dev)) {
4778 ret = i915_gem_init_pipe_control(dev);
4779 if (ret)
4780 return ret;
4781 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004782
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08004783 ret = intel_init_render_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004784 if (ret)
4785 goto cleanup_pipe_control;
4786
4787 if (HAS_BSD(dev)) {
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08004788 ret = intel_init_bsd_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004789 if (ret)
4790 goto cleanup_render_ring;
Zou Nan haid1b851f2010-05-21 09:08:57 +08004791 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004792
Chris Wilson549f7362010-10-19 11:19:32 +01004793 if (HAS_BLT(dev)) {
4794 ret = intel_init_blt_ring_buffer(dev);
4795 if (ret)
4796 goto cleanup_bsd_ring;
4797 }
4798
Chris Wilson6f392d52010-08-07 11:01:22 +01004799 dev_priv->next_seqno = 1;
4800
Chris Wilson68f95ba2010-05-27 13:18:22 +01004801 return 0;
4802
Chris Wilson549f7362010-10-19 11:19:32 +01004803cleanup_bsd_ring:
Chris Wilson78501ea2010-10-27 12:18:21 +01004804 intel_cleanup_ring_buffer(&dev_priv->bsd_ring);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004805cleanup_render_ring:
Chris Wilson78501ea2010-10-27 12:18:21 +01004806 intel_cleanup_ring_buffer(&dev_priv->render_ring);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004807cleanup_pipe_control:
4808 if (HAS_PIPE_CONTROL(dev))
4809 i915_gem_cleanup_pipe_control(dev);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004810 return ret;
4811}
4812
4813void
4814i915_gem_cleanup_ringbuffer(struct drm_device *dev)
4815{
4816 drm_i915_private_t *dev_priv = dev->dev_private;
4817
Chris Wilson78501ea2010-10-27 12:18:21 +01004818 intel_cleanup_ring_buffer(&dev_priv->render_ring);
4819 intel_cleanup_ring_buffer(&dev_priv->bsd_ring);
4820 intel_cleanup_ring_buffer(&dev_priv->blt_ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004821 if (HAS_PIPE_CONTROL(dev))
4822 i915_gem_cleanup_pipe_control(dev);
4823}
4824
4825int
Eric Anholt673a3942008-07-30 12:06:12 -07004826i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
4827 struct drm_file *file_priv)
4828{
4829 drm_i915_private_t *dev_priv = dev->dev_private;
4830 int ret;
4831
Jesse Barnes79e53942008-11-07 14:24:08 -08004832 if (drm_core_check_feature(dev, DRIVER_MODESET))
4833 return 0;
4834
Ben Gamariba1234d2009-09-14 17:48:47 -04004835 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07004836 DRM_ERROR("Reenabling wedged hardware, good luck\n");
Ben Gamariba1234d2009-09-14 17:48:47 -04004837 atomic_set(&dev_priv->mm.wedged, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004838 }
4839
Eric Anholt673a3942008-07-30 12:06:12 -07004840 mutex_lock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004841 dev_priv->mm.suspended = 0;
4842
4843 ret = i915_gem_init_ringbuffer(dev);
Wu Fengguangd816f6ac2009-04-18 10:43:32 +08004844 if (ret != 0) {
4845 mutex_unlock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004846 return ret;
Wu Fengguangd816f6ac2009-04-18 10:43:32 +08004847 }
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004848
Chris Wilson69dc4982010-10-19 10:36:51 +01004849 BUG_ON(!list_empty(&dev_priv->mm.active_list));
Zou Nan hai852835f2010-05-21 09:08:56 +08004850 BUG_ON(!list_empty(&dev_priv->render_ring.active_list));
Chris Wilson87acb0a2010-10-19 10:13:00 +01004851 BUG_ON(!list_empty(&dev_priv->bsd_ring.active_list));
Chris Wilson549f7362010-10-19 11:19:32 +01004852 BUG_ON(!list_empty(&dev_priv->blt_ring.active_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004853 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
4854 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
Zou Nan hai852835f2010-05-21 09:08:56 +08004855 BUG_ON(!list_empty(&dev_priv->render_ring.request_list));
Chris Wilson87acb0a2010-10-19 10:13:00 +01004856 BUG_ON(!list_empty(&dev_priv->bsd_ring.request_list));
Chris Wilson549f7362010-10-19 11:19:32 +01004857 BUG_ON(!list_empty(&dev_priv->blt_ring.request_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004858 mutex_unlock(&dev->struct_mutex);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004859
Chris Wilson5f353082010-06-07 14:03:03 +01004860 ret = drm_irq_install(dev);
4861 if (ret)
4862 goto cleanup_ringbuffer;
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004863
Eric Anholt673a3942008-07-30 12:06:12 -07004864 return 0;
Chris Wilson5f353082010-06-07 14:03:03 +01004865
4866cleanup_ringbuffer:
4867 mutex_lock(&dev->struct_mutex);
4868 i915_gem_cleanup_ringbuffer(dev);
4869 dev_priv->mm.suspended = 1;
4870 mutex_unlock(&dev->struct_mutex);
4871
4872 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004873}
4874
4875int
4876i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
4877 struct drm_file *file_priv)
4878{
Jesse Barnes79e53942008-11-07 14:24:08 -08004879 if (drm_core_check_feature(dev, DRIVER_MODESET))
4880 return 0;
4881
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004882 drm_irq_uninstall(dev);
Linus Torvaldse6890f62009-09-08 17:09:24 -07004883 return i915_gem_idle(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004884}
4885
4886void
4887i915_gem_lastclose(struct drm_device *dev)
4888{
4889 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004890
Eric Anholte806b492009-01-22 09:56:58 -08004891 if (drm_core_check_feature(dev, DRIVER_MODESET))
4892 return;
4893
Keith Packard6dbe2772008-10-14 21:41:13 -07004894 ret = i915_gem_idle(dev);
4895 if (ret)
4896 DRM_ERROR("failed to idle hardware: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07004897}
4898
Chris Wilson64193402010-10-24 12:38:05 +01004899static void
4900init_ring_lists(struct intel_ring_buffer *ring)
4901{
4902 INIT_LIST_HEAD(&ring->active_list);
4903 INIT_LIST_HEAD(&ring->request_list);
4904 INIT_LIST_HEAD(&ring->gpu_write_list);
4905}
4906
Eric Anholt673a3942008-07-30 12:06:12 -07004907void
4908i915_gem_load(struct drm_device *dev)
4909{
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004910 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07004911 drm_i915_private_t *dev_priv = dev->dev_private;
4912
Chris Wilson69dc4982010-10-19 10:36:51 +01004913 INIT_LIST_HEAD(&dev_priv->mm.active_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004914 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
4915 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004916 INIT_LIST_HEAD(&dev_priv->mm.pinned_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07004917 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Chris Wilsonbe726152010-07-23 23:18:50 +01004918 INIT_LIST_HEAD(&dev_priv->mm.deferred_free_list);
Chris Wilson64193402010-10-24 12:38:05 +01004919 init_ring_lists(&dev_priv->render_ring);
4920 init_ring_lists(&dev_priv->bsd_ring);
4921 init_ring_lists(&dev_priv->blt_ring);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02004922 for (i = 0; i < 16; i++)
4923 INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004924 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
4925 i915_gem_retire_work_handler);
Chris Wilson30dbf0c2010-09-25 10:19:17 +01004926 init_completion(&dev_priv->error_completion);
Chris Wilson31169712009-09-14 16:50:28 +01004927
Dave Airlie94400122010-07-20 13:15:31 +10004928 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
4929 if (IS_GEN3(dev)) {
4930 u32 tmp = I915_READ(MI_ARB_STATE);
4931 if (!(tmp & MI_ARB_C3_LP_WRITE_ENABLE)) {
4932 /* arb state is a masked write, so set bit + bit in mask */
4933 tmp = MI_ARB_C3_LP_WRITE_ENABLE | (MI_ARB_C3_LP_WRITE_ENABLE << MI_ARB_MASK_SHIFT);
4934 I915_WRITE(MI_ARB_STATE, tmp);
4935 }
4936 }
4937
Jesse Barnesde151cf2008-11-12 10:03:55 -08004938 /* Old X drivers will take 0-2 for front, back, depth buffers */
Eric Anholtb397c832010-01-26 09:43:10 -08004939 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4940 dev_priv->fence_reg_start = 3;
Jesse Barnesde151cf2008-11-12 10:03:55 -08004941
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004942 if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08004943 dev_priv->num_fence_regs = 16;
4944 else
4945 dev_priv->num_fence_regs = 8;
4946
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004947 /* Initialize fence registers to zero */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004948 switch (INTEL_INFO(dev)->gen) {
4949 case 6:
4950 for (i = 0; i < 16; i++)
4951 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (i * 8), 0);
4952 break;
4953 case 5:
4954 case 4:
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004955 for (i = 0; i < 16; i++)
4956 I915_WRITE64(FENCE_REG_965_0 + (i * 8), 0);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004957 break;
4958 case 3:
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004959 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4960 for (i = 0; i < 8; i++)
4961 I915_WRITE(FENCE_REG_945_8 + (i * 4), 0);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004962 case 2:
4963 for (i = 0; i < 8; i++)
4964 I915_WRITE(FENCE_REG_830_0 + (i * 4), 0);
4965 break;
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004966 }
Eric Anholt673a3942008-07-30 12:06:12 -07004967 i915_gem_detect_bit_6_swizzle(dev);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05004968 init_waitqueue_head(&dev_priv->pending_flip_queue);
Chris Wilson17250b72010-10-28 12:51:39 +01004969
4970 dev_priv->mm.inactive_shrinker.shrink = i915_gem_inactive_shrink;
4971 dev_priv->mm.inactive_shrinker.seeks = DEFAULT_SEEKS;
4972 register_shrinker(&dev_priv->mm.inactive_shrinker);
Eric Anholt673a3942008-07-30 12:06:12 -07004973}
Dave Airlie71acb5e2008-12-30 20:31:46 +10004974
4975/*
4976 * Create a physically contiguous memory object for this object
4977 * e.g. for cursor + overlay regs
4978 */
Chris Wilson995b67622010-08-20 13:23:26 +01004979static int i915_gem_init_phys_object(struct drm_device *dev,
4980 int id, int size, int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004981{
4982 drm_i915_private_t *dev_priv = dev->dev_private;
4983 struct drm_i915_gem_phys_object *phys_obj;
4984 int ret;
4985
4986 if (dev_priv->mm.phys_objs[id - 1] || !size)
4987 return 0;
4988
Eric Anholt9a298b22009-03-24 12:23:04 -07004989 phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004990 if (!phys_obj)
4991 return -ENOMEM;
4992
4993 phys_obj->id = id;
4994
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004995 phys_obj->handle = drm_pci_alloc(dev, size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004996 if (!phys_obj->handle) {
4997 ret = -ENOMEM;
4998 goto kfree_obj;
4999 }
5000#ifdef CONFIG_X86
5001 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
5002#endif
5003
5004 dev_priv->mm.phys_objs[id - 1] = phys_obj;
5005
5006 return 0;
5007kfree_obj:
Eric Anholt9a298b22009-03-24 12:23:04 -07005008 kfree(phys_obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10005009 return ret;
5010}
5011
Chris Wilson995b67622010-08-20 13:23:26 +01005012static void i915_gem_free_phys_object(struct drm_device *dev, int id)
Dave Airlie71acb5e2008-12-30 20:31:46 +10005013{
5014 drm_i915_private_t *dev_priv = dev->dev_private;
5015 struct drm_i915_gem_phys_object *phys_obj;
5016
5017 if (!dev_priv->mm.phys_objs[id - 1])
5018 return;
5019
5020 phys_obj = dev_priv->mm.phys_objs[id - 1];
5021 if (phys_obj->cur_obj) {
5022 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
5023 }
5024
5025#ifdef CONFIG_X86
5026 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
5027#endif
5028 drm_pci_free(dev, phys_obj->handle);
5029 kfree(phys_obj);
5030 dev_priv->mm.phys_objs[id - 1] = NULL;
5031}
5032
5033void i915_gem_free_all_phys_object(struct drm_device *dev)
5034{
5035 int i;
5036
Dave Airlie260883c2009-01-22 17:58:49 +10005037 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
Dave Airlie71acb5e2008-12-30 20:31:46 +10005038 i915_gem_free_phys_object(dev, i);
5039}
5040
5041void i915_gem_detach_phys_object(struct drm_device *dev,
5042 struct drm_gem_object *obj)
5043{
Chris Wilsone5281cc2010-10-28 13:45:36 +01005044 struct address_space *mapping = obj->filp->f_path.dentry->d_inode->i_mapping;
5045 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
5046 char *vaddr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10005047 int i;
Dave Airlie71acb5e2008-12-30 20:31:46 +10005048 int page_count;
5049
Dave Airlie71acb5e2008-12-30 20:31:46 +10005050 if (!obj_priv->phys_obj)
5051 return;
Chris Wilsone5281cc2010-10-28 13:45:36 +01005052 vaddr = obj_priv->phys_obj->handle->vaddr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10005053
5054 page_count = obj->size / PAGE_SIZE;
5055
5056 for (i = 0; i < page_count; i++) {
Chris Wilsone5281cc2010-10-28 13:45:36 +01005057 struct page *page = read_cache_page_gfp(mapping, i,
5058 GFP_HIGHUSER | __GFP_RECLAIMABLE);
5059 if (!IS_ERR(page)) {
5060 char *dst = kmap_atomic(page);
5061 memcpy(dst, vaddr + i*PAGE_SIZE, PAGE_SIZE);
5062 kunmap_atomic(dst);
Dave Airlie71acb5e2008-12-30 20:31:46 +10005063
Chris Wilsone5281cc2010-10-28 13:45:36 +01005064 drm_clflush_pages(&page, 1);
5065
5066 set_page_dirty(page);
5067 mark_page_accessed(page);
5068 page_cache_release(page);
5069 }
Dave Airlie71acb5e2008-12-30 20:31:46 +10005070 }
Daniel Vetter40ce6572010-11-05 18:12:18 +01005071 intel_gtt_chipset_flush();
Chris Wilsond78b47b2009-06-17 21:52:49 +01005072
Dave Airlie71acb5e2008-12-30 20:31:46 +10005073 obj_priv->phys_obj->cur_obj = NULL;
5074 obj_priv->phys_obj = NULL;
5075}
5076
5077int
5078i915_gem_attach_phys_object(struct drm_device *dev,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01005079 struct drm_gem_object *obj,
5080 int id,
5081 int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10005082{
Chris Wilsone5281cc2010-10-28 13:45:36 +01005083 struct address_space *mapping = obj->filp->f_path.dentry->d_inode->i_mapping;
Dave Airlie71acb5e2008-12-30 20:31:46 +10005084 drm_i915_private_t *dev_priv = dev->dev_private;
5085 struct drm_i915_gem_object *obj_priv;
5086 int ret = 0;
5087 int page_count;
5088 int i;
5089
5090 if (id > I915_MAX_PHYS_OBJECT)
5091 return -EINVAL;
5092
Daniel Vetter23010e42010-03-08 13:35:02 +01005093 obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10005094
5095 if (obj_priv->phys_obj) {
5096 if (obj_priv->phys_obj->id == id)
5097 return 0;
5098 i915_gem_detach_phys_object(dev, obj);
5099 }
5100
Dave Airlie71acb5e2008-12-30 20:31:46 +10005101 /* create a new object */
5102 if (!dev_priv->mm.phys_objs[id - 1]) {
5103 ret = i915_gem_init_phys_object(dev, id,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01005104 obj->size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10005105 if (ret) {
Linus Torvaldsaeb565d2009-01-26 10:01:53 -08005106 DRM_ERROR("failed to init phys object %d size: %zu\n", id, obj->size);
Chris Wilsone5281cc2010-10-28 13:45:36 +01005107 return ret;
Dave Airlie71acb5e2008-12-30 20:31:46 +10005108 }
5109 }
5110
5111 /* bind to the object */
5112 obj_priv->phys_obj = dev_priv->mm.phys_objs[id - 1];
5113 obj_priv->phys_obj->cur_obj = obj;
5114
Dave Airlie71acb5e2008-12-30 20:31:46 +10005115 page_count = obj->size / PAGE_SIZE;
5116
5117 for (i = 0; i < page_count; i++) {
Chris Wilsone5281cc2010-10-28 13:45:36 +01005118 struct page *page;
5119 char *dst, *src;
Dave Airlie71acb5e2008-12-30 20:31:46 +10005120
Chris Wilsone5281cc2010-10-28 13:45:36 +01005121 page = read_cache_page_gfp(mapping, i,
5122 GFP_HIGHUSER | __GFP_RECLAIMABLE);
5123 if (IS_ERR(page))
5124 return PTR_ERR(page);
5125
Chris Wilsonff75b9b2010-10-30 22:52:31 +01005126 src = kmap_atomic(page);
Chris Wilsone5281cc2010-10-28 13:45:36 +01005127 dst = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
Dave Airlie71acb5e2008-12-30 20:31:46 +10005128 memcpy(dst, src, PAGE_SIZE);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -07005129 kunmap_atomic(src);
Chris Wilsone5281cc2010-10-28 13:45:36 +01005130
5131 mark_page_accessed(page);
5132 page_cache_release(page);
Dave Airlie71acb5e2008-12-30 20:31:46 +10005133 }
5134
5135 return 0;
Dave Airlie71acb5e2008-12-30 20:31:46 +10005136}
5137
5138static int
5139i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
5140 struct drm_i915_gem_pwrite *args,
5141 struct drm_file *file_priv)
5142{
Daniel Vetter23010e42010-03-08 13:35:02 +01005143 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsonb47b30c2010-11-08 01:12:29 +00005144 void *vaddr = obj_priv->phys_obj->handle->vaddr + args->offset;
5145 char __user *user_data = (char __user *) (uintptr_t) args->data_ptr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10005146
Chris Wilsonb47b30c2010-11-08 01:12:29 +00005147 DRM_DEBUG_DRIVER("vaddr %p, %lld\n", vaddr, args->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10005148
Chris Wilsonb47b30c2010-11-08 01:12:29 +00005149 if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) {
5150 unsigned long unwritten;
5151
5152 /* The physical object once assigned is fixed for the lifetime
5153 * of the obj, so we can safely drop the lock and continue
5154 * to access vaddr.
5155 */
5156 mutex_unlock(&dev->struct_mutex);
5157 unwritten = copy_from_user(vaddr, user_data, args->size);
5158 mutex_lock(&dev->struct_mutex);
5159 if (unwritten)
5160 return -EFAULT;
5161 }
Dave Airlie71acb5e2008-12-30 20:31:46 +10005162
Daniel Vetter40ce6572010-11-05 18:12:18 +01005163 intel_gtt_chipset_flush();
Dave Airlie71acb5e2008-12-30 20:31:46 +10005164 return 0;
5165}
Eric Anholtb9624422009-06-03 07:27:35 +00005166
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005167void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00005168{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005169 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00005170
5171 /* Clean up our request list when the client is going away, so that
5172 * later retire_requests won't dereference our soon-to-be-gone
5173 * file_priv.
5174 */
Chris Wilson1c255952010-09-26 11:03:27 +01005175 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005176 while (!list_empty(&file_priv->mm.request_list)) {
5177 struct drm_i915_gem_request *request;
5178
5179 request = list_first_entry(&file_priv->mm.request_list,
5180 struct drm_i915_gem_request,
5181 client_list);
5182 list_del(&request->client_list);
5183 request->file_priv = NULL;
5184 }
Chris Wilson1c255952010-09-26 11:03:27 +01005185 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00005186}
Chris Wilson31169712009-09-14 16:50:28 +01005187
Chris Wilson31169712009-09-14 16:50:28 +01005188static int
Chris Wilson1637ef42010-04-20 17:10:35 +01005189i915_gpu_is_active(struct drm_device *dev)
5190{
5191 drm_i915_private_t *dev_priv = dev->dev_private;
5192 int lists_empty;
5193
Chris Wilson1637ef42010-04-20 17:10:35 +01005194 lists_empty = list_empty(&dev_priv->mm.flushing_list) &&
Chris Wilson17250b72010-10-28 12:51:39 +01005195 list_empty(&dev_priv->mm.active_list);
Chris Wilson1637ef42010-04-20 17:10:35 +01005196
5197 return !lists_empty;
5198}
5199
5200static int
Chris Wilson17250b72010-10-28 12:51:39 +01005201i915_gem_inactive_shrink(struct shrinker *shrinker,
5202 int nr_to_scan,
5203 gfp_t gfp_mask)
Chris Wilson31169712009-09-14 16:50:28 +01005204{
Chris Wilson17250b72010-10-28 12:51:39 +01005205 struct drm_i915_private *dev_priv =
5206 container_of(shrinker,
5207 struct drm_i915_private,
5208 mm.inactive_shrinker);
5209 struct drm_device *dev = dev_priv->dev;
5210 struct drm_i915_gem_object *obj, *next;
5211 int cnt;
5212
5213 if (!mutex_trylock(&dev->struct_mutex))
Chris Wilsonbbe2e112010-10-28 22:35:07 +01005214 return 0;
Chris Wilson31169712009-09-14 16:50:28 +01005215
5216 /* "fast-path" to count number of available objects */
5217 if (nr_to_scan == 0) {
Chris Wilson17250b72010-10-28 12:51:39 +01005218 cnt = 0;
5219 list_for_each_entry(obj,
5220 &dev_priv->mm.inactive_list,
5221 mm_list)
5222 cnt++;
5223 mutex_unlock(&dev->struct_mutex);
5224 return cnt / 100 * sysctl_vfs_cache_pressure;
Chris Wilson31169712009-09-14 16:50:28 +01005225 }
5226
Chris Wilson1637ef42010-04-20 17:10:35 +01005227rescan:
Chris Wilson31169712009-09-14 16:50:28 +01005228 /* first scan for clean buffers */
Chris Wilson17250b72010-10-28 12:51:39 +01005229 i915_gem_retire_requests(dev);
Chris Wilson31169712009-09-14 16:50:28 +01005230
Chris Wilson17250b72010-10-28 12:51:39 +01005231 list_for_each_entry_safe(obj, next,
5232 &dev_priv->mm.inactive_list,
5233 mm_list) {
5234 if (i915_gem_object_is_purgeable(obj)) {
5235 i915_gem_object_unbind(&obj->base);
5236 if (--nr_to_scan == 0)
5237 break;
Chris Wilson31169712009-09-14 16:50:28 +01005238 }
Chris Wilson31169712009-09-14 16:50:28 +01005239 }
5240
5241 /* second pass, evict/count anything still on the inactive list */
Chris Wilson17250b72010-10-28 12:51:39 +01005242 cnt = 0;
5243 list_for_each_entry_safe(obj, next,
5244 &dev_priv->mm.inactive_list,
5245 mm_list) {
5246 if (nr_to_scan) {
5247 i915_gem_object_unbind(&obj->base);
5248 nr_to_scan--;
5249 } else
5250 cnt++;
Chris Wilson31169712009-09-14 16:50:28 +01005251 }
5252
Chris Wilson17250b72010-10-28 12:51:39 +01005253 if (nr_to_scan && i915_gpu_is_active(dev)) {
Chris Wilson1637ef42010-04-20 17:10:35 +01005254 /*
5255 * We are desperate for pages, so as a last resort, wait
5256 * for the GPU to finish and discard whatever we can.
5257 * This has a dramatic impact to reduce the number of
5258 * OOM-killer events whilst running the GPU aggressively.
5259 */
Chris Wilson17250b72010-10-28 12:51:39 +01005260 if (i915_gpu_idle(dev) == 0)
Chris Wilson1637ef42010-04-20 17:10:35 +01005261 goto rescan;
5262 }
Chris Wilson17250b72010-10-28 12:51:39 +01005263 mutex_unlock(&dev->struct_mutex);
5264 return cnt / 100 * sysctl_vfs_cache_pressure;
Chris Wilson31169712009-09-14 16:50:28 +01005265}