blob: 3cac366b3053ab794ff13b40360ebb31c24029a1 [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 }
Daniel Vetter93a37f22010-11-05 20:24:53 +010098 list_add_tail(&obj->gtt_list, &dev_priv->mm.gtt_list);
Chris Wilson73aa8082010-09-30 11:46:12 +010099}
100
101static void i915_gem_info_remove_gtt(struct drm_i915_private *dev_priv,
Chris Wilsona00b10c2010-09-24 21:15:47 +0100102 struct drm_i915_gem_object *obj)
Chris Wilson73aa8082010-09-30 11:46:12 +0100103{
104 dev_priv->mm.gtt_count--;
Chris Wilsona00b10c2010-09-24 21:15:47 +0100105 dev_priv->mm.gtt_memory -= obj->gtt_space->size;
106 if (obj->gtt_offset < dev_priv->mm.gtt_mappable_end) {
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200107 dev_priv->mm.mappable_gtt_used -=
Chris Wilsona00b10c2010-09-24 21:15:47 +0100108 min_t(size_t, obj->gtt_space->size,
109 dev_priv->mm.gtt_mappable_end - obj->gtt_offset);
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200110 }
Daniel Vetter93a37f22010-11-05 20:24:53 +0100111 list_del_init(&obj->gtt_list);
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200112}
113
114/**
115 * Update the mappable working set counters. Call _only_ when there is a change
116 * in one of (pin|fault)_mappable and update *_mappable _before_ calling.
117 * @mappable: new state the changed mappable flag (either pin_ or fault_).
118 */
119static void
120i915_gem_info_update_mappable(struct drm_i915_private *dev_priv,
Chris Wilsona00b10c2010-09-24 21:15:47 +0100121 struct drm_i915_gem_object *obj,
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200122 bool mappable)
123{
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200124 if (mappable) {
Chris Wilsona00b10c2010-09-24 21:15:47 +0100125 if (obj->pin_mappable && obj->fault_mappable)
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200126 /* Combined state was already mappable. */
127 return;
128 dev_priv->mm.gtt_mappable_count++;
Chris Wilsona00b10c2010-09-24 21:15:47 +0100129 dev_priv->mm.gtt_mappable_memory += obj->gtt_space->size;
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200130 } else {
Chris Wilsona00b10c2010-09-24 21:15:47 +0100131 if (obj->pin_mappable || obj->fault_mappable)
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200132 /* Combined state still mappable. */
133 return;
134 dev_priv->mm.gtt_mappable_count--;
Chris Wilsona00b10c2010-09-24 21:15:47 +0100135 dev_priv->mm.gtt_mappable_memory -= obj->gtt_space->size;
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200136 }
Chris Wilson73aa8082010-09-30 11:46:12 +0100137}
138
139static void i915_gem_info_add_pin(struct drm_i915_private *dev_priv,
Chris Wilsona00b10c2010-09-24 21:15:47 +0100140 struct drm_i915_gem_object *obj,
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200141 bool mappable)
Chris Wilson73aa8082010-09-30 11:46:12 +0100142{
143 dev_priv->mm.pin_count++;
Chris Wilsona00b10c2010-09-24 21:15:47 +0100144 dev_priv->mm.pin_memory += obj->gtt_space->size;
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200145 if (mappable) {
Chris Wilsona00b10c2010-09-24 21:15:47 +0100146 obj->pin_mappable = true;
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200147 i915_gem_info_update_mappable(dev_priv, obj, true);
148 }
Chris Wilson73aa8082010-09-30 11:46:12 +0100149}
150
151static void i915_gem_info_remove_pin(struct drm_i915_private *dev_priv,
Chris Wilsona00b10c2010-09-24 21:15:47 +0100152 struct drm_i915_gem_object *obj)
Chris Wilson73aa8082010-09-30 11:46:12 +0100153{
154 dev_priv->mm.pin_count--;
Chris Wilsona00b10c2010-09-24 21:15:47 +0100155 dev_priv->mm.pin_memory -= obj->gtt_space->size;
156 if (obj->pin_mappable) {
157 obj->pin_mappable = false;
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200158 i915_gem_info_update_mappable(dev_priv, obj, false);
159 }
Chris Wilson73aa8082010-09-30 11:46:12 +0100160}
161
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100162int
163i915_gem_check_is_wedged(struct drm_device *dev)
164{
165 struct drm_i915_private *dev_priv = dev->dev_private;
166 struct completion *x = &dev_priv->error_completion;
167 unsigned long flags;
168 int ret;
169
170 if (!atomic_read(&dev_priv->mm.wedged))
171 return 0;
172
173 ret = wait_for_completion_interruptible(x);
174 if (ret)
175 return ret;
176
177 /* Success, we reset the GPU! */
178 if (!atomic_read(&dev_priv->mm.wedged))
179 return 0;
180
181 /* GPU is hung, bump the completion count to account for
182 * the token we just consumed so that we never hit zero and
183 * end up waiting upon a subsequent completion event that
184 * will never happen.
185 */
186 spin_lock_irqsave(&x->wait.lock, flags);
187 x->done++;
188 spin_unlock_irqrestore(&x->wait.lock, flags);
189 return -EIO;
190}
191
Chris Wilson76c1dec2010-09-25 11:22:51 +0100192static int i915_mutex_lock_interruptible(struct drm_device *dev)
193{
194 struct drm_i915_private *dev_priv = dev->dev_private;
195 int ret;
196
197 ret = i915_gem_check_is_wedged(dev);
198 if (ret)
199 return ret;
200
201 ret = mutex_lock_interruptible(&dev->struct_mutex);
202 if (ret)
203 return ret;
204
205 if (atomic_read(&dev_priv->mm.wedged)) {
206 mutex_unlock(&dev->struct_mutex);
207 return -EAGAIN;
208 }
209
Chris Wilson23bc5982010-09-29 16:10:57 +0100210 WARN_ON(i915_verify_lists(dev));
Chris Wilson76c1dec2010-09-25 11:22:51 +0100211 return 0;
212}
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100213
Chris Wilson7d1c4802010-08-07 21:45:03 +0100214static inline bool
215i915_gem_object_is_inactive(struct drm_i915_gem_object *obj_priv)
216{
217 return obj_priv->gtt_space &&
218 !obj_priv->active &&
219 obj_priv->pin_count == 0;
220}
221
Chris Wilson73aa8082010-09-30 11:46:12 +0100222int i915_gem_do_init(struct drm_device *dev,
223 unsigned long start,
Daniel Vetter53984632010-09-22 23:44:24 +0200224 unsigned long mappable_end,
Jesse Barnes79e53942008-11-07 14:24:08 -0800225 unsigned long end)
226{
227 drm_i915_private_t *dev_priv = dev->dev_private;
228
229 if (start >= end ||
230 (start & (PAGE_SIZE - 1)) != 0 ||
231 (end & (PAGE_SIZE - 1)) != 0) {
232 return -EINVAL;
233 }
234
235 drm_mm_init(&dev_priv->mm.gtt_space, start,
236 end - start);
237
Chris Wilson73aa8082010-09-30 11:46:12 +0100238 dev_priv->mm.gtt_total = end - start;
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200239 dev_priv->mm.mappable_gtt_total = min(end, mappable_end) - start;
Daniel Vetter53984632010-09-22 23:44:24 +0200240 dev_priv->mm.gtt_mappable_end = mappable_end;
Jesse Barnes79e53942008-11-07 14:24:08 -0800241
242 return 0;
243}
Keith Packard6dbe2772008-10-14 21:41:13 -0700244
Eric Anholt673a3942008-07-30 12:06:12 -0700245int
246i915_gem_init_ioctl(struct drm_device *dev, void *data,
247 struct drm_file *file_priv)
248{
Eric Anholt673a3942008-07-30 12:06:12 -0700249 struct drm_i915_gem_init *args = data;
Jesse Barnes79e53942008-11-07 14:24:08 -0800250 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700251
252 mutex_lock(&dev->struct_mutex);
Daniel Vetter53984632010-09-22 23:44:24 +0200253 ret = i915_gem_do_init(dev, args->gtt_start, args->gtt_end, args->gtt_end);
Eric Anholt673a3942008-07-30 12:06:12 -0700254 mutex_unlock(&dev->struct_mutex);
255
Jesse Barnes79e53942008-11-07 14:24:08 -0800256 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700257}
258
Eric Anholt5a125c32008-10-22 21:40:13 -0700259int
260i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
261 struct drm_file *file_priv)
262{
Chris Wilson73aa8082010-09-30 11:46:12 +0100263 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt5a125c32008-10-22 21:40:13 -0700264 struct drm_i915_gem_get_aperture *args = data;
Eric Anholt5a125c32008-10-22 21:40:13 -0700265
266 if (!(dev->driver->driver_features & DRIVER_GEM))
267 return -ENODEV;
268
Chris Wilson73aa8082010-09-30 11:46:12 +0100269 mutex_lock(&dev->struct_mutex);
270 args->aper_size = dev_priv->mm.gtt_total;
271 args->aper_available_size = args->aper_size - dev_priv->mm.pin_memory;
272 mutex_unlock(&dev->struct_mutex);
Eric Anholt5a125c32008-10-22 21:40:13 -0700273
274 return 0;
275}
276
Eric Anholt673a3942008-07-30 12:06:12 -0700277
278/**
279 * Creates a new mm object and returns a handle to it.
280 */
281int
282i915_gem_create_ioctl(struct drm_device *dev, void *data,
283 struct drm_file *file_priv)
284{
285 struct drm_i915_gem_create *args = data;
286 struct drm_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300287 int ret;
288 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700289
290 args->size = roundup(args->size, PAGE_SIZE);
291
292 /* Allocate the new object */
Daniel Vetterac52bc52010-04-09 19:05:06 +0000293 obj = i915_gem_alloc_object(dev, args->size);
Eric Anholt673a3942008-07-30 12:06:12 -0700294 if (obj == NULL)
295 return -ENOMEM;
296
297 ret = drm_gem_handle_create(file_priv, obj, &handle);
Chris Wilson1dfd9752010-09-06 14:44:14 +0100298 if (ret) {
Chris Wilson202f2fe2010-10-14 13:20:40 +0100299 drm_gem_object_release(obj);
300 i915_gem_info_remove_obj(dev->dev_private, obj->size);
301 kfree(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700302 return ret;
Chris Wilson1dfd9752010-09-06 14:44:14 +0100303 }
304
Chris Wilson202f2fe2010-10-14 13:20:40 +0100305 /* drop reference from allocate - handle holds it now */
306 drm_gem_object_unreference(obj);
307 trace_i915_gem_object_create(obj);
308
Eric Anholt673a3942008-07-30 12:06:12 -0700309 args->handle = handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700310 return 0;
311}
312
Eric Anholt280b7132009-03-12 16:56:27 -0700313static int i915_gem_object_needs_bit17_swizzle(struct drm_gem_object *obj)
314{
315 drm_i915_private_t *dev_priv = obj->dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +0100316 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt280b7132009-03-12 16:56:27 -0700317
318 return dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_9_10_17 &&
319 obj_priv->tiling_mode != I915_TILING_NONE;
320}
321
Chris Wilson99a03df2010-05-27 14:15:34 +0100322static inline void
Eric Anholt40123c12009-03-09 13:42:30 -0700323slow_shmem_copy(struct page *dst_page,
324 int dst_offset,
325 struct page *src_page,
326 int src_offset,
327 int length)
328{
329 char *dst_vaddr, *src_vaddr;
330
Chris Wilson99a03df2010-05-27 14:15:34 +0100331 dst_vaddr = kmap(dst_page);
332 src_vaddr = kmap(src_page);
Eric Anholt40123c12009-03-09 13:42:30 -0700333
334 memcpy(dst_vaddr + dst_offset, src_vaddr + src_offset, length);
335
Chris Wilson99a03df2010-05-27 14:15:34 +0100336 kunmap(src_page);
337 kunmap(dst_page);
Eric Anholt40123c12009-03-09 13:42:30 -0700338}
339
Chris Wilson99a03df2010-05-27 14:15:34 +0100340static inline void
Eric Anholt280b7132009-03-12 16:56:27 -0700341slow_shmem_bit17_copy(struct page *gpu_page,
342 int gpu_offset,
343 struct page *cpu_page,
344 int cpu_offset,
345 int length,
346 int is_read)
347{
348 char *gpu_vaddr, *cpu_vaddr;
349
350 /* Use the unswizzled path if this page isn't affected. */
351 if ((page_to_phys(gpu_page) & (1 << 17)) == 0) {
352 if (is_read)
353 return slow_shmem_copy(cpu_page, cpu_offset,
354 gpu_page, gpu_offset, length);
355 else
356 return slow_shmem_copy(gpu_page, gpu_offset,
357 cpu_page, cpu_offset, length);
358 }
359
Chris Wilson99a03df2010-05-27 14:15:34 +0100360 gpu_vaddr = kmap(gpu_page);
361 cpu_vaddr = kmap(cpu_page);
Eric Anholt280b7132009-03-12 16:56:27 -0700362
363 /* Copy the data, XORing A6 with A17 (1). The user already knows he's
364 * XORing with the other bits (A9 for Y, A9 and A10 for X)
365 */
366 while (length > 0) {
367 int cacheline_end = ALIGN(gpu_offset + 1, 64);
368 int this_length = min(cacheline_end - gpu_offset, length);
369 int swizzled_gpu_offset = gpu_offset ^ 64;
370
371 if (is_read) {
372 memcpy(cpu_vaddr + cpu_offset,
373 gpu_vaddr + swizzled_gpu_offset,
374 this_length);
375 } else {
376 memcpy(gpu_vaddr + swizzled_gpu_offset,
377 cpu_vaddr + cpu_offset,
378 this_length);
379 }
380 cpu_offset += this_length;
381 gpu_offset += this_length;
382 length -= this_length;
383 }
384
Chris Wilson99a03df2010-05-27 14:15:34 +0100385 kunmap(cpu_page);
386 kunmap(gpu_page);
Eric Anholt280b7132009-03-12 16:56:27 -0700387}
388
Eric Anholt673a3942008-07-30 12:06:12 -0700389/**
Eric Anholteb014592009-03-10 11:44:52 -0700390 * This is the fast shmem pread path, which attempts to copy_from_user directly
391 * from the backing pages of the object to the user's address space. On a
392 * fault, it fails so we can fall back to i915_gem_shmem_pwrite_slow().
393 */
394static int
395i915_gem_shmem_pread_fast(struct drm_device *dev, struct drm_gem_object *obj,
396 struct drm_i915_gem_pread *args,
397 struct drm_file *file_priv)
398{
Daniel Vetter23010e42010-03-08 13:35:02 +0100399 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsone5281cc2010-10-28 13:45:36 +0100400 struct address_space *mapping = obj->filp->f_path.dentry->d_inode->i_mapping;
Eric Anholteb014592009-03-10 11:44:52 -0700401 ssize_t remain;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100402 loff_t offset;
Eric Anholteb014592009-03-10 11:44:52 -0700403 char __user *user_data;
404 int page_offset, page_length;
Eric Anholteb014592009-03-10 11:44:52 -0700405
406 user_data = (char __user *) (uintptr_t) args->data_ptr;
407 remain = args->size;
408
Daniel Vetter23010e42010-03-08 13:35:02 +0100409 obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700410 offset = args->offset;
411
412 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100413 struct page *page;
414 char *vaddr;
415 int ret;
416
Eric Anholteb014592009-03-10 11:44:52 -0700417 /* Operation in this page
418 *
Eric Anholteb014592009-03-10 11:44:52 -0700419 * page_offset = offset within page
420 * page_length = bytes to copy for this page
421 */
Eric Anholteb014592009-03-10 11:44:52 -0700422 page_offset = offset & (PAGE_SIZE-1);
423 page_length = remain;
424 if ((page_offset + remain) > PAGE_SIZE)
425 page_length = PAGE_SIZE - page_offset;
426
Chris Wilsone5281cc2010-10-28 13:45:36 +0100427 page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
428 GFP_HIGHUSER | __GFP_RECLAIMABLE);
429 if (IS_ERR(page))
430 return PTR_ERR(page);
431
432 vaddr = kmap_atomic(page);
433 ret = __copy_to_user_inatomic(user_data,
434 vaddr + page_offset,
435 page_length);
436 kunmap_atomic(vaddr);
437
438 mark_page_accessed(page);
439 page_cache_release(page);
440 if (ret)
Chris Wilson4f27b752010-10-14 15:26:45 +0100441 return -EFAULT;
Eric Anholteb014592009-03-10 11:44:52 -0700442
443 remain -= page_length;
444 user_data += page_length;
445 offset += page_length;
446 }
447
Chris Wilson4f27b752010-10-14 15:26:45 +0100448 return 0;
Eric Anholteb014592009-03-10 11:44:52 -0700449}
450
451/**
452 * This is the fallback shmem pread path, which allocates temporary storage
453 * in kernel space to copy_to_user into outside of the struct_mutex, so we
454 * can copy out of the object's backing pages while holding the struct mutex
455 * and not take page faults.
456 */
457static int
458i915_gem_shmem_pread_slow(struct drm_device *dev, struct drm_gem_object *obj,
459 struct drm_i915_gem_pread *args,
460 struct drm_file *file_priv)
461{
Chris Wilsone5281cc2010-10-28 13:45:36 +0100462 struct address_space *mapping = obj->filp->f_path.dentry->d_inode->i_mapping;
Daniel Vetter23010e42010-03-08 13:35:02 +0100463 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700464 struct mm_struct *mm = current->mm;
465 struct page **user_pages;
466 ssize_t remain;
467 loff_t offset, pinned_pages, i;
468 loff_t first_data_page, last_data_page, num_pages;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100469 int shmem_page_offset;
470 int data_page_index, data_page_offset;
Eric Anholteb014592009-03-10 11:44:52 -0700471 int page_length;
472 int ret;
473 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700474 int do_bit17_swizzling;
Eric Anholteb014592009-03-10 11:44:52 -0700475
476 remain = args->size;
477
478 /* Pin the user pages containing the data. We can't fault while
479 * holding the struct mutex, yet we want to hold it while
480 * dereferencing the user data.
481 */
482 first_data_page = data_ptr / PAGE_SIZE;
483 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
484 num_pages = last_data_page - first_data_page + 1;
485
Chris Wilson4f27b752010-10-14 15:26:45 +0100486 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
Eric Anholteb014592009-03-10 11:44:52 -0700487 if (user_pages == NULL)
488 return -ENOMEM;
489
Chris Wilson4f27b752010-10-14 15:26:45 +0100490 mutex_unlock(&dev->struct_mutex);
Eric Anholteb014592009-03-10 11:44:52 -0700491 down_read(&mm->mmap_sem);
492 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
Eric Anholte5e9ecd2009-04-07 16:01:22 -0700493 num_pages, 1, 0, user_pages, NULL);
Eric Anholteb014592009-03-10 11:44:52 -0700494 up_read(&mm->mmap_sem);
Chris Wilson4f27b752010-10-14 15:26:45 +0100495 mutex_lock(&dev->struct_mutex);
Eric Anholteb014592009-03-10 11:44:52 -0700496 if (pinned_pages < num_pages) {
497 ret = -EFAULT;
Chris Wilson4f27b752010-10-14 15:26:45 +0100498 goto out;
Eric Anholteb014592009-03-10 11:44:52 -0700499 }
500
Chris Wilson4f27b752010-10-14 15:26:45 +0100501 ret = i915_gem_object_set_cpu_read_domain_range(obj,
502 args->offset,
Eric Anholteb014592009-03-10 11:44:52 -0700503 args->size);
Chris Wilson4f27b752010-10-14 15:26:45 +0100504 if (ret)
505 goto out;
506
507 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700508
Daniel Vetter23010e42010-03-08 13:35:02 +0100509 obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700510 offset = args->offset;
511
512 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100513 struct page *page;
514
Eric Anholteb014592009-03-10 11:44:52 -0700515 /* Operation in this page
516 *
Eric Anholteb014592009-03-10 11:44:52 -0700517 * shmem_page_offset = offset within page in shmem file
518 * data_page_index = page number in get_user_pages return
519 * data_page_offset = offset with data_page_index page.
520 * page_length = bytes to copy for this page
521 */
Eric Anholteb014592009-03-10 11:44:52 -0700522 shmem_page_offset = offset & ~PAGE_MASK;
523 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
524 data_page_offset = data_ptr & ~PAGE_MASK;
525
526 page_length = remain;
527 if ((shmem_page_offset + page_length) > PAGE_SIZE)
528 page_length = PAGE_SIZE - shmem_page_offset;
529 if ((data_page_offset + page_length) > PAGE_SIZE)
530 page_length = PAGE_SIZE - data_page_offset;
531
Chris Wilsone5281cc2010-10-28 13:45:36 +0100532 page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
533 GFP_HIGHUSER | __GFP_RECLAIMABLE);
534 if (IS_ERR(page))
535 return PTR_ERR(page);
536
Eric Anholt280b7132009-03-12 16:56:27 -0700537 if (do_bit17_swizzling) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100538 slow_shmem_bit17_copy(page,
Eric Anholt280b7132009-03-12 16:56:27 -0700539 shmem_page_offset,
Chris Wilson99a03df2010-05-27 14:15:34 +0100540 user_pages[data_page_index],
541 data_page_offset,
542 page_length,
543 1);
544 } else {
545 slow_shmem_copy(user_pages[data_page_index],
546 data_page_offset,
Chris Wilsone5281cc2010-10-28 13:45:36 +0100547 page,
Chris Wilson99a03df2010-05-27 14:15:34 +0100548 shmem_page_offset,
549 page_length);
Eric Anholt280b7132009-03-12 16:56:27 -0700550 }
Eric Anholteb014592009-03-10 11:44:52 -0700551
Chris Wilsone5281cc2010-10-28 13:45:36 +0100552 mark_page_accessed(page);
553 page_cache_release(page);
554
Eric Anholteb014592009-03-10 11:44:52 -0700555 remain -= page_length;
556 data_ptr += page_length;
557 offset += page_length;
558 }
559
Chris Wilson4f27b752010-10-14 15:26:45 +0100560out:
Eric Anholteb014592009-03-10 11:44:52 -0700561 for (i = 0; i < pinned_pages; i++) {
562 SetPageDirty(user_pages[i]);
Chris Wilsone5281cc2010-10-28 13:45:36 +0100563 mark_page_accessed(user_pages[i]);
Eric Anholteb014592009-03-10 11:44:52 -0700564 page_cache_release(user_pages[i]);
565 }
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700566 drm_free_large(user_pages);
Eric Anholteb014592009-03-10 11:44:52 -0700567
568 return ret;
569}
570
Eric Anholt673a3942008-07-30 12:06:12 -0700571/**
572 * Reads data from the object referenced by handle.
573 *
574 * On error, the contents of *data are undefined.
575 */
576int
577i915_gem_pread_ioctl(struct drm_device *dev, void *data,
578 struct drm_file *file_priv)
579{
580 struct drm_i915_gem_pread *args = data;
581 struct drm_gem_object *obj;
582 struct drm_i915_gem_object *obj_priv;
Chris Wilson35b62a82010-09-26 20:23:38 +0100583 int ret = 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700584
Chris Wilson51311d02010-11-17 09:10:42 +0000585 if (args->size == 0)
586 return 0;
587
588 if (!access_ok(VERIFY_WRITE,
589 (char __user *)(uintptr_t)args->data_ptr,
590 args->size))
591 return -EFAULT;
592
593 ret = fault_in_pages_writeable((char __user *)(uintptr_t)args->data_ptr,
594 args->size);
595 if (ret)
596 return -EFAULT;
597
Chris Wilson4f27b752010-10-14 15:26:45 +0100598 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100599 if (ret)
Chris Wilson4f27b752010-10-14 15:26:45 +0100600 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700601
602 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100603 if (obj == NULL) {
604 ret = -ENOENT;
605 goto unlock;
Chris Wilson4f27b752010-10-14 15:26:45 +0100606 }
Daniel Vetter23010e42010-03-08 13:35:02 +0100607 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700608
Chris Wilson7dcd2492010-09-26 20:21:44 +0100609 /* Bounds check source. */
610 if (args->offset > obj->size || args->size > obj->size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +0100611 ret = -EINVAL;
Chris Wilson35b62a82010-09-26 20:23:38 +0100612 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +0100613 }
614
Chris Wilson4f27b752010-10-14 15:26:45 +0100615 ret = i915_gem_object_set_cpu_read_domain_range(obj,
616 args->offset,
617 args->size);
618 if (ret)
Chris Wilsone5281cc2010-10-28 13:45:36 +0100619 goto out;
Chris Wilson4f27b752010-10-14 15:26:45 +0100620
621 ret = -EFAULT;
622 if (!i915_gem_object_needs_bit17_swizzle(obj))
Eric Anholt280b7132009-03-12 16:56:27 -0700623 ret = i915_gem_shmem_pread_fast(dev, obj, args, file_priv);
Chris Wilson4f27b752010-10-14 15:26:45 +0100624 if (ret == -EFAULT)
625 ret = i915_gem_shmem_pread_slow(dev, obj, args, file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -0700626
Chris Wilson35b62a82010-09-26 20:23:38 +0100627out:
Chris Wilson4f27b752010-10-14 15:26:45 +0100628 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100629unlock:
Chris Wilson4f27b752010-10-14 15:26:45 +0100630 mutex_unlock(&dev->struct_mutex);
Eric Anholteb014592009-03-10 11:44:52 -0700631 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700632}
633
Keith Packard0839ccb2008-10-30 19:38:48 -0700634/* This is the fast write path which cannot handle
635 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700636 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700637
Keith Packard0839ccb2008-10-30 19:38:48 -0700638static inline int
639fast_user_write(struct io_mapping *mapping,
640 loff_t page_base, int page_offset,
641 char __user *user_data,
642 int length)
643{
644 char *vaddr_atomic;
645 unsigned long unwritten;
646
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700647 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
Keith Packard0839ccb2008-10-30 19:38:48 -0700648 unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
649 user_data, length);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700650 io_mapping_unmap_atomic(vaddr_atomic);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100651 return unwritten;
Keith Packard0839ccb2008-10-30 19:38:48 -0700652}
653
654/* Here's the write path which can sleep for
655 * page faults
656 */
657
Chris Wilsonab34c222010-05-27 14:15:35 +0100658static inline void
Eric Anholt3de09aa2009-03-09 09:42:23 -0700659slow_kernel_write(struct io_mapping *mapping,
660 loff_t gtt_base, int gtt_offset,
661 struct page *user_page, int user_offset,
662 int length)
Keith Packard0839ccb2008-10-30 19:38:48 -0700663{
Chris Wilsonab34c222010-05-27 14:15:35 +0100664 char __iomem *dst_vaddr;
665 char *src_vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -0700666
Chris Wilsonab34c222010-05-27 14:15:35 +0100667 dst_vaddr = io_mapping_map_wc(mapping, gtt_base);
668 src_vaddr = kmap(user_page);
669
670 memcpy_toio(dst_vaddr + gtt_offset,
671 src_vaddr + user_offset,
672 length);
673
674 kunmap(user_page);
675 io_mapping_unmap(dst_vaddr);
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700676}
677
Eric Anholt3de09aa2009-03-09 09:42:23 -0700678/**
679 * This is the fast pwrite path, where we copy the data directly from the
680 * user into the GTT, uncached.
681 */
Eric Anholt673a3942008-07-30 12:06:12 -0700682static int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700683i915_gem_gtt_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
684 struct drm_i915_gem_pwrite *args,
685 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700686{
Daniel Vetter23010e42010-03-08 13:35:02 +0100687 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Keith Packard0839ccb2008-10-30 19:38:48 -0700688 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700689 ssize_t remain;
Keith Packard0839ccb2008-10-30 19:38:48 -0700690 loff_t offset, page_base;
Eric Anholt673a3942008-07-30 12:06:12 -0700691 char __user *user_data;
Keith Packard0839ccb2008-10-30 19:38:48 -0700692 int page_offset, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700693
694 user_data = (char __user *) (uintptr_t) args->data_ptr;
695 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700696
Daniel Vetter23010e42010-03-08 13:35:02 +0100697 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700698 offset = obj_priv->gtt_offset + args->offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700699
700 while (remain > 0) {
701 /* Operation in this page
702 *
Keith Packard0839ccb2008-10-30 19:38:48 -0700703 * page_base = page offset within aperture
704 * page_offset = offset within page
705 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -0700706 */
Keith Packard0839ccb2008-10-30 19:38:48 -0700707 page_base = (offset & ~(PAGE_SIZE-1));
708 page_offset = offset & (PAGE_SIZE-1);
709 page_length = remain;
710 if ((page_offset + remain) > PAGE_SIZE)
711 page_length = PAGE_SIZE - page_offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700712
Keith Packard0839ccb2008-10-30 19:38:48 -0700713 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -0700714 * source page isn't available. Return the error and we'll
715 * retry in the slow path.
Keith Packard0839ccb2008-10-30 19:38:48 -0700716 */
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100717 if (fast_user_write(dev_priv->mm.gtt_mapping, page_base,
718 page_offset, user_data, page_length))
719
720 return -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -0700721
Keith Packard0839ccb2008-10-30 19:38:48 -0700722 remain -= page_length;
723 user_data += page_length;
724 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700725 }
Eric Anholt673a3942008-07-30 12:06:12 -0700726
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100727 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700728}
729
Eric Anholt3de09aa2009-03-09 09:42:23 -0700730/**
731 * This is the fallback GTT pwrite path, which uses get_user_pages to pin
732 * the memory and maps it using kmap_atomic for copying.
733 *
734 * This code resulted in x11perf -rgb10text consuming about 10% more CPU
735 * than using i915_gem_gtt_pwrite_fast on a G45 (32-bit).
736 */
Eric Anholt3043c602008-10-02 12:24:47 -0700737static int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700738i915_gem_gtt_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
739 struct drm_i915_gem_pwrite *args,
740 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700741{
Daniel Vetter23010e42010-03-08 13:35:02 +0100742 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700743 drm_i915_private_t *dev_priv = dev->dev_private;
744 ssize_t remain;
745 loff_t gtt_page_base, offset;
746 loff_t first_data_page, last_data_page, num_pages;
747 loff_t pinned_pages, i;
748 struct page **user_pages;
749 struct mm_struct *mm = current->mm;
750 int gtt_page_offset, data_page_offset, data_page_index, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700751 int ret;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700752 uint64_t data_ptr = args->data_ptr;
753
754 remain = args->size;
755
756 /* Pin the user pages containing the data. We can't fault while
757 * holding the struct mutex, and all of the pwrite implementations
758 * want to hold it while dereferencing the user data.
759 */
760 first_data_page = data_ptr / PAGE_SIZE;
761 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
762 num_pages = last_data_page - first_data_page + 1;
763
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100764 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
Eric Anholt3de09aa2009-03-09 09:42:23 -0700765 if (user_pages == NULL)
766 return -ENOMEM;
767
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100768 mutex_unlock(&dev->struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700769 down_read(&mm->mmap_sem);
770 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
771 num_pages, 0, 0, user_pages, NULL);
772 up_read(&mm->mmap_sem);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100773 mutex_lock(&dev->struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700774 if (pinned_pages < num_pages) {
775 ret = -EFAULT;
776 goto out_unpin_pages;
777 }
778
Eric Anholt3de09aa2009-03-09 09:42:23 -0700779 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
780 if (ret)
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100781 goto out_unpin_pages;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700782
Daniel Vetter23010e42010-03-08 13:35:02 +0100783 obj_priv = to_intel_bo(obj);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700784 offset = obj_priv->gtt_offset + args->offset;
785
786 while (remain > 0) {
787 /* Operation in this page
788 *
789 * gtt_page_base = page offset within aperture
790 * gtt_page_offset = offset within page in aperture
791 * data_page_index = page number in get_user_pages return
792 * data_page_offset = offset with data_page_index page.
793 * page_length = bytes to copy for this page
794 */
795 gtt_page_base = offset & PAGE_MASK;
796 gtt_page_offset = offset & ~PAGE_MASK;
797 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
798 data_page_offset = data_ptr & ~PAGE_MASK;
799
800 page_length = remain;
801 if ((gtt_page_offset + page_length) > PAGE_SIZE)
802 page_length = PAGE_SIZE - gtt_page_offset;
803 if ((data_page_offset + page_length) > PAGE_SIZE)
804 page_length = PAGE_SIZE - data_page_offset;
805
Chris Wilsonab34c222010-05-27 14:15:35 +0100806 slow_kernel_write(dev_priv->mm.gtt_mapping,
807 gtt_page_base, gtt_page_offset,
808 user_pages[data_page_index],
809 data_page_offset,
810 page_length);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700811
812 remain -= page_length;
813 offset += page_length;
814 data_ptr += page_length;
815 }
816
Eric Anholt3de09aa2009-03-09 09:42:23 -0700817out_unpin_pages:
818 for (i = 0; i < pinned_pages; i++)
819 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700820 drm_free_large(user_pages);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700821
822 return ret;
823}
824
Eric Anholt40123c12009-03-09 13:42:30 -0700825/**
826 * This is the fast shmem pwrite path, which attempts to directly
827 * copy_from_user into the kmapped pages backing the object.
828 */
Eric Anholt673a3942008-07-30 12:06:12 -0700829static int
Eric Anholt40123c12009-03-09 13:42:30 -0700830i915_gem_shmem_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
831 struct drm_i915_gem_pwrite *args,
832 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700833{
Chris Wilsone5281cc2010-10-28 13:45:36 +0100834 struct address_space *mapping = obj->filp->f_path.dentry->d_inode->i_mapping;
Daniel Vetter23010e42010-03-08 13:35:02 +0100835 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700836 ssize_t remain;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100837 loff_t offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700838 char __user *user_data;
839 int page_offset, page_length;
Eric Anholt40123c12009-03-09 13:42:30 -0700840
841 user_data = (char __user *) (uintptr_t) args->data_ptr;
842 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700843
Daniel Vetter23010e42010-03-08 13:35:02 +0100844 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700845 offset = args->offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700846 obj_priv->dirty = 1;
Eric Anholt673a3942008-07-30 12:06:12 -0700847
Eric Anholt40123c12009-03-09 13:42:30 -0700848 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100849 struct page *page;
850 char *vaddr;
851 int ret;
852
Eric Anholt40123c12009-03-09 13:42:30 -0700853 /* Operation in this page
854 *
Eric Anholt40123c12009-03-09 13:42:30 -0700855 * page_offset = offset within page
856 * page_length = bytes to copy for this page
857 */
Eric Anholt40123c12009-03-09 13:42:30 -0700858 page_offset = offset & (PAGE_SIZE-1);
859 page_length = remain;
860 if ((page_offset + remain) > PAGE_SIZE)
861 page_length = PAGE_SIZE - page_offset;
862
Chris Wilsone5281cc2010-10-28 13:45:36 +0100863 page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
864 GFP_HIGHUSER | __GFP_RECLAIMABLE);
865 if (IS_ERR(page))
866 return PTR_ERR(page);
867
868 vaddr = kmap_atomic(page, KM_USER0);
869 ret = __copy_from_user_inatomic(vaddr + page_offset,
870 user_data,
871 page_length);
872 kunmap_atomic(vaddr, KM_USER0);
873
874 set_page_dirty(page);
875 mark_page_accessed(page);
876 page_cache_release(page);
877
878 /* If we get a fault while copying data, then (presumably) our
879 * source page isn't available. Return the error and we'll
880 * retry in the slow path.
881 */
882 if (ret)
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100883 return -EFAULT;
Eric Anholt40123c12009-03-09 13:42:30 -0700884
885 remain -= page_length;
886 user_data += page_length;
887 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700888 }
889
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100890 return 0;
Eric Anholt40123c12009-03-09 13:42:30 -0700891}
892
893/**
894 * This is the fallback shmem pwrite path, which uses get_user_pages to pin
895 * the memory and maps it using kmap_atomic for copying.
896 *
897 * This avoids taking mmap_sem for faulting on the user's address while the
898 * struct_mutex is held.
899 */
900static int
901i915_gem_shmem_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
902 struct drm_i915_gem_pwrite *args,
903 struct drm_file *file_priv)
904{
Chris Wilsone5281cc2010-10-28 13:45:36 +0100905 struct address_space *mapping = obj->filp->f_path.dentry->d_inode->i_mapping;
Daniel Vetter23010e42010-03-08 13:35:02 +0100906 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700907 struct mm_struct *mm = current->mm;
908 struct page **user_pages;
909 ssize_t remain;
910 loff_t offset, pinned_pages, i;
911 loff_t first_data_page, last_data_page, num_pages;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100912 int shmem_page_offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700913 int data_page_index, data_page_offset;
914 int page_length;
915 int ret;
916 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700917 int do_bit17_swizzling;
Eric Anholt40123c12009-03-09 13:42:30 -0700918
919 remain = args->size;
920
921 /* Pin the user pages containing the data. We can't fault while
922 * holding the struct mutex, and all of the pwrite implementations
923 * want to hold it while dereferencing the user data.
924 */
925 first_data_page = data_ptr / PAGE_SIZE;
926 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
927 num_pages = last_data_page - first_data_page + 1;
928
Chris Wilson4f27b752010-10-14 15:26:45 +0100929 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
Eric Anholt40123c12009-03-09 13:42:30 -0700930 if (user_pages == NULL)
931 return -ENOMEM;
932
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100933 mutex_unlock(&dev->struct_mutex);
Eric Anholt40123c12009-03-09 13:42:30 -0700934 down_read(&mm->mmap_sem);
935 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
936 num_pages, 0, 0, user_pages, NULL);
937 up_read(&mm->mmap_sem);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100938 mutex_lock(&dev->struct_mutex);
Eric Anholt40123c12009-03-09 13:42:30 -0700939 if (pinned_pages < num_pages) {
940 ret = -EFAULT;
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100941 goto out;
Eric Anholt40123c12009-03-09 13:42:30 -0700942 }
943
Eric Anholt40123c12009-03-09 13:42:30 -0700944 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100945 if (ret)
946 goto out;
947
948 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700949
Daniel Vetter23010e42010-03-08 13:35:02 +0100950 obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700951 offset = args->offset;
952 obj_priv->dirty = 1;
953
954 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100955 struct page *page;
956
Eric Anholt40123c12009-03-09 13:42:30 -0700957 /* Operation in this page
958 *
Eric Anholt40123c12009-03-09 13:42:30 -0700959 * shmem_page_offset = offset within page in shmem file
960 * data_page_index = page number in get_user_pages return
961 * data_page_offset = offset with data_page_index page.
962 * page_length = bytes to copy for this page
963 */
Eric Anholt40123c12009-03-09 13:42:30 -0700964 shmem_page_offset = offset & ~PAGE_MASK;
965 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
966 data_page_offset = data_ptr & ~PAGE_MASK;
967
968 page_length = remain;
969 if ((shmem_page_offset + page_length) > PAGE_SIZE)
970 page_length = PAGE_SIZE - shmem_page_offset;
971 if ((data_page_offset + page_length) > PAGE_SIZE)
972 page_length = PAGE_SIZE - data_page_offset;
973
Chris Wilsone5281cc2010-10-28 13:45:36 +0100974 page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
975 GFP_HIGHUSER | __GFP_RECLAIMABLE);
976 if (IS_ERR(page)) {
977 ret = PTR_ERR(page);
978 goto out;
979 }
980
Eric Anholt280b7132009-03-12 16:56:27 -0700981 if (do_bit17_swizzling) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100982 slow_shmem_bit17_copy(page,
Eric Anholt280b7132009-03-12 16:56:27 -0700983 shmem_page_offset,
984 user_pages[data_page_index],
985 data_page_offset,
Chris Wilson99a03df2010-05-27 14:15:34 +0100986 page_length,
987 0);
988 } else {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100989 slow_shmem_copy(page,
Chris Wilson99a03df2010-05-27 14:15:34 +0100990 shmem_page_offset,
991 user_pages[data_page_index],
992 data_page_offset,
993 page_length);
Eric Anholt280b7132009-03-12 16:56:27 -0700994 }
Eric Anholt40123c12009-03-09 13:42:30 -0700995
Chris Wilsone5281cc2010-10-28 13:45:36 +0100996 set_page_dirty(page);
997 mark_page_accessed(page);
998 page_cache_release(page);
999
Eric Anholt40123c12009-03-09 13:42:30 -07001000 remain -= page_length;
1001 data_ptr += page_length;
1002 offset += page_length;
1003 }
1004
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001005out:
Eric Anholt40123c12009-03-09 13:42:30 -07001006 for (i = 0; i < pinned_pages; i++)
1007 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07001008 drm_free_large(user_pages);
Eric Anholt40123c12009-03-09 13:42:30 -07001009
1010 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001011}
1012
1013/**
1014 * Writes data to the object referenced by handle.
1015 *
1016 * On error, the contents of the buffer that were to be modified are undefined.
1017 */
1018int
1019i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001020 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001021{
1022 struct drm_i915_gem_pwrite *args = data;
1023 struct drm_gem_object *obj;
1024 struct drm_i915_gem_object *obj_priv;
Chris Wilson51311d02010-11-17 09:10:42 +00001025 int ret;
1026
1027 if (args->size == 0)
1028 return 0;
1029
1030 if (!access_ok(VERIFY_READ,
1031 (char __user *)(uintptr_t)args->data_ptr,
1032 args->size))
1033 return -EFAULT;
1034
1035 ret = fault_in_pages_readable((char __user *)(uintptr_t)args->data_ptr,
1036 args->size);
1037 if (ret)
1038 return -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -07001039
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001040 ret = i915_mutex_lock_interruptible(dev);
1041 if (ret)
1042 return ret;
1043
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001044 obj = drm_gem_object_lookup(dev, file, args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001045 if (obj == NULL) {
1046 ret = -ENOENT;
1047 goto unlock;
1048 }
Daniel Vetter23010e42010-03-08 13:35:02 +01001049 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001050
Chris Wilson7dcd2492010-09-26 20:21:44 +01001051 /* Bounds check destination. */
1052 if (args->offset > obj->size || args->size > obj->size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001053 ret = -EINVAL;
Chris Wilson35b62a82010-09-26 20:23:38 +01001054 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001055 }
1056
Eric Anholt673a3942008-07-30 12:06:12 -07001057 /* We can only do the GTT pwrite on untiled buffers, as otherwise
1058 * it would end up going through the fenced access, and we'll get
1059 * different detiling behavior between reading and writing.
1060 * pread/pwrite currently are reading and writing from the CPU
1061 * perspective, requiring manual detiling by the client.
1062 */
Dave Airlie71acb5e2008-12-30 20:31:46 +10001063 if (obj_priv->phys_obj)
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001064 ret = i915_gem_phys_pwrite(dev, obj, args, file);
Dave Airlie71acb5e2008-12-30 20:31:46 +10001065 else if (obj_priv->tiling_mode == I915_TILING_NONE &&
Chris Wilson5cdf5882010-09-27 15:51:07 +01001066 obj_priv->gtt_space &&
Chris Wilson9b8c4a02010-05-27 14:21:01 +01001067 obj->write_domain != I915_GEM_DOMAIN_CPU) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01001068 ret = i915_gem_object_pin(obj, 0, true);
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001069 if (ret)
1070 goto out;
1071
1072 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
1073 if (ret)
1074 goto out_unpin;
1075
1076 ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file);
1077 if (ret == -EFAULT)
1078 ret = i915_gem_gtt_pwrite_slow(dev, obj, args, file);
1079
1080out_unpin:
1081 i915_gem_object_unpin(obj);
Eric Anholt40123c12009-03-09 13:42:30 -07001082 } else {
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001083 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
1084 if (ret)
Chris Wilsone5281cc2010-10-28 13:45:36 +01001085 goto out;
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001086
1087 ret = -EFAULT;
1088 if (!i915_gem_object_needs_bit17_swizzle(obj))
1089 ret = i915_gem_shmem_pwrite_fast(dev, obj, args, file);
1090 if (ret == -EFAULT)
1091 ret = i915_gem_shmem_pwrite_slow(dev, obj, args, file);
Eric Anholt40123c12009-03-09 13:42:30 -07001092 }
Eric Anholt673a3942008-07-30 12:06:12 -07001093
Chris Wilson35b62a82010-09-26 20:23:38 +01001094out:
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001095 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001096unlock:
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001097 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07001098 return ret;
1099}
1100
1101/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001102 * Called when user space prepares to use an object with the CPU, either
1103 * through the mmap ioctl's mapping or a GTT mapping.
Eric Anholt673a3942008-07-30 12:06:12 -07001104 */
1105int
1106i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
1107 struct drm_file *file_priv)
1108{
Eric Anholta09ba7f2009-08-29 12:49:51 -07001109 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07001110 struct drm_i915_gem_set_domain *args = data;
1111 struct drm_gem_object *obj;
Jesse Barnes652c3932009-08-17 13:31:43 -07001112 struct drm_i915_gem_object *obj_priv;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001113 uint32_t read_domains = args->read_domains;
1114 uint32_t write_domain = args->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07001115 int ret;
1116
1117 if (!(dev->driver->driver_features & DRIVER_GEM))
1118 return -ENODEV;
1119
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001120 /* Only handle setting domains to types used by the CPU. */
Chris Wilson21d509e2009-06-06 09:46:02 +01001121 if (write_domain & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001122 return -EINVAL;
1123
Chris Wilson21d509e2009-06-06 09:46:02 +01001124 if (read_domains & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001125 return -EINVAL;
1126
1127 /* Having something in the write domain implies it's in the read
1128 * domain, and only that read domain. Enforce that in the request.
1129 */
1130 if (write_domain != 0 && read_domains != write_domain)
1131 return -EINVAL;
1132
Chris Wilson76c1dec2010-09-25 11:22:51 +01001133 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001134 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001135 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001136
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001137 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1138 if (obj == NULL) {
1139 ret = -ENOENT;
1140 goto unlock;
Chris Wilson76c1dec2010-09-25 11:22:51 +01001141 }
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001142 obj_priv = to_intel_bo(obj);
Jesse Barnes652c3932009-08-17 13:31:43 -07001143
1144 intel_mark_busy(dev, obj);
1145
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001146 if (read_domains & I915_GEM_DOMAIN_GTT) {
1147 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
Eric Anholt02354392008-11-26 13:58:13 -08001148
Eric Anholta09ba7f2009-08-29 12:49:51 -07001149 /* Update the LRU on the fence for the CPU access that's
1150 * about to occur.
1151 */
1152 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001153 struct drm_i915_fence_reg *reg =
1154 &dev_priv->fence_regs[obj_priv->fence_reg];
1155 list_move_tail(&reg->lru_list,
Eric Anholta09ba7f2009-08-29 12:49:51 -07001156 &dev_priv->mm.fence_list);
1157 }
1158
Eric Anholt02354392008-11-26 13:58:13 -08001159 /* Silently promote "you're not bound, there was nothing to do"
1160 * to success, since the client was just asking us to
1161 * make sure everything was done.
1162 */
1163 if (ret == -EINVAL)
1164 ret = 0;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001165 } else {
Eric Anholte47c68e2008-11-14 13:35:19 -08001166 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001167 }
1168
Chris Wilson7d1c4802010-08-07 21:45:03 +01001169 /* Maintain LRU order of "inactive" objects */
1170 if (ret == 0 && i915_gem_object_is_inactive(obj_priv))
Chris Wilson69dc4982010-10-19 10:36:51 +01001171 list_move_tail(&obj_priv->mm_list, &dev_priv->mm.inactive_list);
Chris Wilson7d1c4802010-08-07 21:45:03 +01001172
Eric Anholt673a3942008-07-30 12:06:12 -07001173 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001174unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07001175 mutex_unlock(&dev->struct_mutex);
1176 return ret;
1177}
1178
1179/**
1180 * Called when user space has done writes to this buffer
1181 */
1182int
1183i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
1184 struct drm_file *file_priv)
1185{
1186 struct drm_i915_gem_sw_finish *args = data;
1187 struct drm_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001188 int ret = 0;
1189
1190 if (!(dev->driver->driver_features & DRIVER_GEM))
1191 return -ENODEV;
1192
Chris Wilson76c1dec2010-09-25 11:22:51 +01001193 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001194 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001195 return ret;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001196
Eric Anholt673a3942008-07-30 12:06:12 -07001197 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1198 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001199 ret = -ENOENT;
1200 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07001201 }
1202
Eric Anholt673a3942008-07-30 12:06:12 -07001203 /* Pinned buffers may be scanout, so flush the cache */
Chris Wilson3d2a8122010-09-29 11:39:53 +01001204 if (to_intel_bo(obj)->pin_count)
Eric Anholte47c68e2008-11-14 13:35:19 -08001205 i915_gem_object_flush_cpu_write_domain(obj);
1206
Eric Anholt673a3942008-07-30 12:06:12 -07001207 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001208unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07001209 mutex_unlock(&dev->struct_mutex);
1210 return ret;
1211}
1212
1213/**
1214 * Maps the contents of an object, returning the address it is mapped
1215 * into.
1216 *
1217 * While the mapping holds a reference on the contents of the object, it doesn't
1218 * imply a ref on the object itself.
1219 */
1220int
1221i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
1222 struct drm_file *file_priv)
1223{
Chris Wilsonda761a62010-10-27 17:37:08 +01001224 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07001225 struct drm_i915_gem_mmap *args = data;
1226 struct drm_gem_object *obj;
1227 loff_t offset;
1228 unsigned long addr;
1229
1230 if (!(dev->driver->driver_features & DRIVER_GEM))
1231 return -ENODEV;
1232
1233 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1234 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001235 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001236
Chris Wilsonda761a62010-10-27 17:37:08 +01001237 if (obj->size > dev_priv->mm.gtt_mappable_end) {
1238 drm_gem_object_unreference_unlocked(obj);
1239 return -E2BIG;
1240 }
1241
Eric Anholt673a3942008-07-30 12:06:12 -07001242 offset = args->offset;
1243
1244 down_write(&current->mm->mmap_sem);
1245 addr = do_mmap(obj->filp, 0, args->size,
1246 PROT_READ | PROT_WRITE, MAP_SHARED,
1247 args->offset);
1248 up_write(&current->mm->mmap_sem);
Luca Barbieribc9025b2010-02-09 05:49:12 +00001249 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001250 if (IS_ERR((void *)addr))
1251 return addr;
1252
1253 args->addr_ptr = (uint64_t) addr;
1254
1255 return 0;
1256}
1257
Jesse Barnesde151cf2008-11-12 10:03:55 -08001258/**
1259 * i915_gem_fault - fault a page into the GTT
1260 * vma: VMA in question
1261 * vmf: fault info
1262 *
1263 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1264 * from userspace. The fault handler takes care of binding the object to
1265 * the GTT (if needed), allocating and programming a fence register (again,
1266 * only if needed based on whether the old reg is still valid or the object
1267 * is tiled) and inserting a new PTE into the faulting process.
1268 *
1269 * Note that the faulting process may involve evicting existing objects
1270 * from the GTT and/or fence registers to make room. So performance may
1271 * suffer if the GTT working set is large or there are few fence registers
1272 * left.
1273 */
1274int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1275{
1276 struct drm_gem_object *obj = vma->vm_private_data;
1277 struct drm_device *dev = obj->dev;
Chris Wilson7d1c4802010-08-07 21:45:03 +01001278 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001279 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001280 pgoff_t page_offset;
1281 unsigned long pfn;
1282 int ret = 0;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001283 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001284
1285 /* We don't use vmf->pgoff since that has the fake offset */
1286 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1287 PAGE_SHIFT;
1288
1289 /* Now bind it into the GTT if needed */
1290 mutex_lock(&dev->struct_mutex);
Daniel Vetterfb7d5162010-10-01 22:05:20 +02001291 BUG_ON(obj_priv->pin_count && !obj_priv->pin_mappable);
Chris Wilsona00b10c2010-09-24 21:15:47 +01001292
1293 if (obj_priv->gtt_space) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01001294 if (!obj_priv->map_and_fenceable) {
Chris Wilsona00b10c2010-09-24 21:15:47 +01001295 ret = i915_gem_object_unbind(obj);
1296 if (ret)
1297 goto unlock;
1298 }
1299 }
Daniel Vetter16e809a2010-09-16 19:37:04 +02001300
Jesse Barnesde151cf2008-11-12 10:03:55 -08001301 if (!obj_priv->gtt_space) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01001302 ret = i915_gem_object_bind_to_gtt(obj, 0, true);
Chris Wilsonc7150892009-09-23 00:43:56 +01001303 if (ret)
1304 goto unlock;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001305 }
1306
Chris Wilson4a684a42010-10-28 14:44:08 +01001307 ret = i915_gem_object_set_to_gtt_domain(obj, write);
1308 if (ret)
1309 goto unlock;
1310
Daniel Vetterfb7d5162010-10-01 22:05:20 +02001311 if (!obj_priv->fault_mappable) {
1312 obj_priv->fault_mappable = true;
Chris Wilsona00b10c2010-09-24 21:15:47 +01001313 i915_gem_info_update_mappable(dev_priv, obj_priv, true);
Daniel Vetterfb7d5162010-10-01 22:05:20 +02001314 }
1315
Jesse Barnesde151cf2008-11-12 10:03:55 -08001316 /* Need a new fence register? */
Eric Anholta09ba7f2009-08-29 12:49:51 -07001317 if (obj_priv->tiling_mode != I915_TILING_NONE) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01001318 ret = i915_gem_object_get_fence_reg(obj, true);
Chris Wilsonc7150892009-09-23 00:43:56 +01001319 if (ret)
1320 goto unlock;
Eric Anholtd9ddcb92009-01-27 10:33:49 -08001321 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001322
Chris Wilson7d1c4802010-08-07 21:45:03 +01001323 if (i915_gem_object_is_inactive(obj_priv))
Chris Wilson69dc4982010-10-19 10:36:51 +01001324 list_move_tail(&obj_priv->mm_list, &dev_priv->mm.inactive_list);
Chris Wilson7d1c4802010-08-07 21:45:03 +01001325
Jesse Barnesde151cf2008-11-12 10:03:55 -08001326 pfn = ((dev->agp->base + obj_priv->gtt_offset) >> PAGE_SHIFT) +
1327 page_offset;
1328
1329 /* Finally, remap it using the new GTT offset */
1330 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
Chris Wilsonc7150892009-09-23 00:43:56 +01001331unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001332 mutex_unlock(&dev->struct_mutex);
1333
1334 switch (ret) {
Chris Wilson045e7692010-11-07 09:18:22 +00001335 case -EAGAIN:
1336 set_need_resched();
Chris Wilsonc7150892009-09-23 00:43:56 +01001337 case 0:
1338 case -ERESTARTSYS:
1339 return VM_FAULT_NOPAGE;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001340 case -ENOMEM:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001341 return VM_FAULT_OOM;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001342 default:
Chris Wilsonc7150892009-09-23 00:43:56 +01001343 return VM_FAULT_SIGBUS;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001344 }
1345}
1346
1347/**
1348 * i915_gem_create_mmap_offset - create a fake mmap offset for an object
1349 * @obj: obj in question
1350 *
1351 * GEM memory mapping works by handing back to userspace a fake mmap offset
1352 * it can use in a subsequent mmap(2) call. The DRM core code then looks
1353 * up the object based on the offset and sets up the various memory mapping
1354 * structures.
1355 *
1356 * This routine allocates and attaches a fake offset for @obj.
1357 */
1358static int
1359i915_gem_create_mmap_offset(struct drm_gem_object *obj)
1360{
1361 struct drm_device *dev = obj->dev;
1362 struct drm_gem_mm *mm = dev->mm_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001363 struct drm_map_list *list;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +11001364 struct drm_local_map *map;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001365 int ret = 0;
1366
1367 /* Set the object up for mmap'ing */
1368 list = &obj->map_list;
Eric Anholt9a298b22009-03-24 12:23:04 -07001369 list->map = kzalloc(sizeof(struct drm_map_list), GFP_KERNEL);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001370 if (!list->map)
1371 return -ENOMEM;
1372
1373 map = list->map;
1374 map->type = _DRM_GEM;
1375 map->size = obj->size;
1376 map->handle = obj;
1377
1378 /* Get a DRM GEM mmap offset allocated... */
1379 list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
1380 obj->size / PAGE_SIZE, 0, 0);
1381 if (!list->file_offset_node) {
1382 DRM_ERROR("failed to allocate offset for bo %d\n", obj->name);
Chris Wilson9e0ae5342010-09-21 15:05:24 +01001383 ret = -ENOSPC;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001384 goto out_free_list;
1385 }
1386
1387 list->file_offset_node = drm_mm_get_block(list->file_offset_node,
1388 obj->size / PAGE_SIZE, 0);
1389 if (!list->file_offset_node) {
1390 ret = -ENOMEM;
1391 goto out_free_list;
1392 }
1393
1394 list->hash.key = list->file_offset_node->start;
Chris Wilson9e0ae5342010-09-21 15:05:24 +01001395 ret = drm_ht_insert_item(&mm->offset_hash, &list->hash);
1396 if (ret) {
Jesse Barnesde151cf2008-11-12 10:03:55 -08001397 DRM_ERROR("failed to add to map hash\n");
1398 goto out_free_mm;
1399 }
1400
Jesse Barnesde151cf2008-11-12 10:03:55 -08001401 return 0;
1402
1403out_free_mm:
1404 drm_mm_put_block(list->file_offset_node);
1405out_free_list:
Eric Anholt9a298b22009-03-24 12:23:04 -07001406 kfree(list->map);
Chris Wilson39a01d12010-10-28 13:03:06 +01001407 list->map = NULL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001408
1409 return ret;
1410}
1411
Chris Wilson901782b2009-07-10 08:18:50 +01001412/**
1413 * i915_gem_release_mmap - remove physical page mappings
1414 * @obj: obj in question
1415 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001416 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01001417 * relinquish ownership of the pages back to the system.
1418 *
1419 * It is vital that we remove the page mapping if we have mapped a tiled
1420 * object through the GTT and then lose the fence register due to
1421 * resource pressure. Similarly if the object has been moved out of the
1422 * aperture, than pages mapped into userspace must be revoked. Removing the
1423 * mapping will then trigger a page fault on the next user access, allowing
1424 * fixup by i915_gem_fault().
1425 */
Eric Anholtd05ca302009-07-10 13:02:26 -07001426void
Chris Wilson901782b2009-07-10 08:18:50 +01001427i915_gem_release_mmap(struct drm_gem_object *obj)
1428{
1429 struct drm_device *dev = obj->dev;
Daniel Vetterfb7d5162010-10-01 22:05:20 +02001430 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001431 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson901782b2009-07-10 08:18:50 +01001432
Chris Wilson39a01d12010-10-28 13:03:06 +01001433 if (unlikely(obj->map_list.map && dev->dev_mapping))
Chris Wilson901782b2009-07-10 08:18:50 +01001434 unmap_mapping_range(dev->dev_mapping,
Chris Wilson39a01d12010-10-28 13:03:06 +01001435 (loff_t)obj->map_list.hash.key<<PAGE_SHIFT,
1436 obj->size, 1);
Daniel Vetterfb7d5162010-10-01 22:05:20 +02001437
1438 if (obj_priv->fault_mappable) {
1439 obj_priv->fault_mappable = false;
Chris Wilsona00b10c2010-09-24 21:15:47 +01001440 i915_gem_info_update_mappable(dev_priv, obj_priv, false);
Daniel Vetterfb7d5162010-10-01 22:05:20 +02001441 }
Chris Wilson901782b2009-07-10 08:18:50 +01001442}
1443
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001444static void
1445i915_gem_free_mmap_offset(struct drm_gem_object *obj)
1446{
1447 struct drm_device *dev = obj->dev;
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001448 struct drm_gem_mm *mm = dev->mm_private;
Chris Wilson39a01d12010-10-28 13:03:06 +01001449 struct drm_map_list *list = &obj->map_list;
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001450
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001451 drm_ht_remove_item(&mm->offset_hash, &list->hash);
Chris Wilson39a01d12010-10-28 13:03:06 +01001452 drm_mm_put_block(list->file_offset_node);
1453 kfree(list->map);
1454 list->map = NULL;
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001455}
1456
Jesse Barnesde151cf2008-11-12 10:03:55 -08001457/**
1458 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1459 * @obj: object to check
1460 *
1461 * Return the required GTT alignment for an object, taking into account
Daniel Vetter5e783302010-11-14 22:32:36 +01001462 * potential fence register mapping.
Jesse Barnesde151cf2008-11-12 10:03:55 -08001463 */
1464static uint32_t
Chris Wilsona00b10c2010-09-24 21:15:47 +01001465i915_gem_get_gtt_alignment(struct drm_i915_gem_object *obj_priv)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001466{
Chris Wilsona00b10c2010-09-24 21:15:47 +01001467 struct drm_device *dev = obj_priv->base.dev;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001468
1469 /*
1470 * Minimum alignment is 4k (GTT page size), but might be greater
1471 * if a fence register is needed for the object.
1472 */
Chris Wilsona00b10c2010-09-24 21:15:47 +01001473 if (INTEL_INFO(dev)->gen >= 4 ||
1474 obj_priv->tiling_mode == I915_TILING_NONE)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001475 return 4096;
1476
1477 /*
1478 * Previous chips need to be aligned to the size of the smallest
1479 * fence register that can contain the object.
1480 */
Chris Wilsona00b10c2010-09-24 21:15:47 +01001481 return i915_gem_get_gtt_size(obj_priv);
1482}
1483
Daniel Vetter5e783302010-11-14 22:32:36 +01001484/**
1485 * i915_gem_get_unfenced_gtt_alignment - return required GTT alignment for an
1486 * unfenced object
1487 * @obj: object to check
1488 *
1489 * Return the required GTT alignment for an object, only taking into account
1490 * unfenced tiled surface requirements.
1491 */
1492static uint32_t
1493i915_gem_get_unfenced_gtt_alignment(struct drm_i915_gem_object *obj_priv)
1494{
1495 struct drm_device *dev = obj_priv->base.dev;
1496 int tile_height;
1497
1498 /*
1499 * Minimum alignment is 4k (GTT page size) for sane hw.
1500 */
1501 if (INTEL_INFO(dev)->gen >= 4 || IS_G33(dev) ||
1502 obj_priv->tiling_mode == I915_TILING_NONE)
1503 return 4096;
1504
1505 /*
1506 * Older chips need unfenced tiled buffers to be aligned to the left
1507 * edge of an even tile row (where tile rows are counted as if the bo is
1508 * placed in a fenced gtt region).
1509 */
1510 if (IS_GEN2(dev) ||
1511 (obj_priv->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev)))
1512 tile_height = 32;
1513 else
1514 tile_height = 8;
1515
1516 return tile_height * obj_priv->stride * 2;
1517}
1518
Chris Wilsona00b10c2010-09-24 21:15:47 +01001519static uint32_t
1520i915_gem_get_gtt_size(struct drm_i915_gem_object *obj_priv)
1521{
1522 struct drm_device *dev = obj_priv->base.dev;
1523 uint32_t size;
1524
1525 /*
1526 * Minimum alignment is 4k (GTT page size), but might be greater
1527 * if a fence register is needed for the object.
1528 */
1529 if (INTEL_INFO(dev)->gen >= 4)
1530 return obj_priv->base.size;
1531
1532 /*
1533 * Previous chips need to be aligned to the size of the smallest
1534 * fence register that can contain the object.
1535 */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001536 if (INTEL_INFO(dev)->gen == 3)
Chris Wilsona00b10c2010-09-24 21:15:47 +01001537 size = 1024*1024;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001538 else
Chris Wilsona00b10c2010-09-24 21:15:47 +01001539 size = 512*1024;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001540
Chris Wilsona00b10c2010-09-24 21:15:47 +01001541 while (size < obj_priv->base.size)
1542 size <<= 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001543
Chris Wilsona00b10c2010-09-24 21:15:47 +01001544 return size;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001545}
1546
1547/**
1548 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1549 * @dev: DRM device
1550 * @data: GTT mapping ioctl data
1551 * @file_priv: GEM object info
1552 *
1553 * Simply returns the fake offset to userspace so it can mmap it.
1554 * The mmap call will end up in drm_gem_mmap(), which will set things
1555 * up so we can get faults in the handler above.
1556 *
1557 * The fault handler will take care of binding the object into the GTT
1558 * (since it may have been evicted to make room for something), allocating
1559 * a fence register, and mapping the appropriate aperture address into
1560 * userspace.
1561 */
1562int
1563i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
1564 struct drm_file *file_priv)
1565{
Chris Wilsonda761a62010-10-27 17:37:08 +01001566 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001567 struct drm_i915_gem_mmap_gtt *args = data;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001568 struct drm_gem_object *obj;
1569 struct drm_i915_gem_object *obj_priv;
1570 int ret;
1571
1572 if (!(dev->driver->driver_features & DRIVER_GEM))
1573 return -ENODEV;
1574
Chris Wilson76c1dec2010-09-25 11:22:51 +01001575 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001576 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001577 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001578
Jesse Barnesde151cf2008-11-12 10:03:55 -08001579 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001580 if (obj == NULL) {
1581 ret = -ENOENT;
1582 goto unlock;
1583 }
Daniel Vetter23010e42010-03-08 13:35:02 +01001584 obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001585
Chris Wilsonda761a62010-10-27 17:37:08 +01001586 if (obj->size > dev_priv->mm.gtt_mappable_end) {
1587 ret = -E2BIG;
1588 goto unlock;
1589 }
1590
Chris Wilsonab182822009-09-22 18:46:17 +01001591 if (obj_priv->madv != I915_MADV_WILLNEED) {
1592 DRM_ERROR("Attempting to mmap a purgeable buffer\n");
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001593 ret = -EINVAL;
1594 goto out;
Chris Wilsonab182822009-09-22 18:46:17 +01001595 }
1596
Chris Wilson39a01d12010-10-28 13:03:06 +01001597 if (!obj->map_list.map) {
Jesse Barnesde151cf2008-11-12 10:03:55 -08001598 ret = i915_gem_create_mmap_offset(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001599 if (ret)
1600 goto out;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001601 }
1602
Chris Wilson39a01d12010-10-28 13:03:06 +01001603 args->offset = (u64)obj->map_list.hash.key << PAGE_SHIFT;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001604
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001605out:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001606 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001607unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001608 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001609 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001610}
1611
Chris Wilsone5281cc2010-10-28 13:45:36 +01001612static int
1613i915_gem_object_get_pages_gtt(struct drm_gem_object *obj,
1614 gfp_t gfpmask)
1615{
1616 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
1617 int page_count, i;
1618 struct address_space *mapping;
1619 struct inode *inode;
1620 struct page *page;
1621
1622 /* Get the list of pages out of our struct file. They'll be pinned
1623 * at this point until we release them.
1624 */
1625 page_count = obj->size / PAGE_SIZE;
1626 BUG_ON(obj_priv->pages != NULL);
1627 obj_priv->pages = drm_malloc_ab(page_count, sizeof(struct page *));
1628 if (obj_priv->pages == NULL)
1629 return -ENOMEM;
1630
1631 inode = obj->filp->f_path.dentry->d_inode;
1632 mapping = inode->i_mapping;
1633 for (i = 0; i < page_count; i++) {
1634 page = read_cache_page_gfp(mapping, i,
1635 GFP_HIGHUSER |
1636 __GFP_COLD |
1637 __GFP_RECLAIMABLE |
1638 gfpmask);
1639 if (IS_ERR(page))
1640 goto err_pages;
1641
1642 obj_priv->pages[i] = page;
1643 }
1644
1645 if (obj_priv->tiling_mode != I915_TILING_NONE)
1646 i915_gem_object_do_bit_17_swizzle(obj);
1647
1648 return 0;
1649
1650err_pages:
1651 while (i--)
1652 page_cache_release(obj_priv->pages[i]);
1653
1654 drm_free_large(obj_priv->pages);
1655 obj_priv->pages = NULL;
1656 return PTR_ERR(page);
1657}
1658
Chris Wilson5cdf5882010-09-27 15:51:07 +01001659static void
Chris Wilsone5281cc2010-10-28 13:45:36 +01001660i915_gem_object_put_pages_gtt(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07001661{
Daniel Vetter23010e42010-03-08 13:35:02 +01001662 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001663 int page_count = obj->size / PAGE_SIZE;
1664 int i;
1665
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001666 BUG_ON(obj_priv->madv == __I915_MADV_PURGED);
Eric Anholt856fa192009-03-19 14:10:50 -07001667
Eric Anholt280b7132009-03-12 16:56:27 -07001668 if (obj_priv->tiling_mode != I915_TILING_NONE)
1669 i915_gem_object_save_bit_17_swizzle(obj);
1670
Chris Wilson3ef94da2009-09-14 16:50:29 +01001671 if (obj_priv->madv == I915_MADV_DONTNEED)
Chris Wilson13a05fd2009-09-20 23:03:19 +01001672 obj_priv->dirty = 0;
Chris Wilson3ef94da2009-09-14 16:50:29 +01001673
1674 for (i = 0; i < page_count; i++) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01001675 if (obj_priv->dirty)
1676 set_page_dirty(obj_priv->pages[i]);
1677
1678 if (obj_priv->madv == I915_MADV_WILLNEED)
Eric Anholt856fa192009-03-19 14:10:50 -07001679 mark_page_accessed(obj_priv->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001680
1681 page_cache_release(obj_priv->pages[i]);
1682 }
Eric Anholt673a3942008-07-30 12:06:12 -07001683 obj_priv->dirty = 0;
1684
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07001685 drm_free_large(obj_priv->pages);
Eric Anholt856fa192009-03-19 14:10:50 -07001686 obj_priv->pages = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001687}
1688
Chris Wilsona56ba562010-09-28 10:07:56 +01001689static uint32_t
1690i915_gem_next_request_seqno(struct drm_device *dev,
1691 struct intel_ring_buffer *ring)
1692{
1693 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson5d97eb62010-11-10 20:40:02 +00001694 return ring->outstanding_lazy_request = dev_priv->next_seqno;
Chris Wilsona56ba562010-09-28 10:07:56 +01001695}
1696
Eric Anholt673a3942008-07-30 12:06:12 -07001697static void
Daniel Vetter617dbe22010-02-11 22:16:02 +01001698i915_gem_object_move_to_active(struct drm_gem_object *obj,
Zou Nan hai852835f2010-05-21 09:08:56 +08001699 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001700{
1701 struct drm_device *dev = obj->dev;
Chris Wilson69dc4982010-10-19 10:36:51 +01001702 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001703 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsona56ba562010-09-28 10:07:56 +01001704 uint32_t seqno = i915_gem_next_request_seqno(dev, ring);
Daniel Vetter617dbe22010-02-11 22:16:02 +01001705
Zou Nan hai852835f2010-05-21 09:08:56 +08001706 BUG_ON(ring == NULL);
1707 obj_priv->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001708
1709 /* Add a reference if we're newly entering the active list. */
1710 if (!obj_priv->active) {
1711 drm_gem_object_reference(obj);
1712 obj_priv->active = 1;
1713 }
Daniel Vettere35a41d2010-02-11 22:13:59 +01001714
Eric Anholt673a3942008-07-30 12:06:12 -07001715 /* Move from whatever list we were on to the tail of execution. */
Chris Wilson69dc4982010-10-19 10:36:51 +01001716 list_move_tail(&obj_priv->mm_list, &dev_priv->mm.active_list);
1717 list_move_tail(&obj_priv->ring_list, &ring->active_list);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001718 obj_priv->last_rendering_seqno = seqno;
Eric Anholt673a3942008-07-30 12:06:12 -07001719}
1720
Eric Anholtce44b0e2008-11-06 16:00:31 -08001721static void
1722i915_gem_object_move_to_flushing(struct drm_gem_object *obj)
1723{
1724 struct drm_device *dev = obj->dev;
1725 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001726 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001727
1728 BUG_ON(!obj_priv->active);
Chris Wilson69dc4982010-10-19 10:36:51 +01001729 list_move_tail(&obj_priv->mm_list, &dev_priv->mm.flushing_list);
1730 list_del_init(&obj_priv->ring_list);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001731 obj_priv->last_rendering_seqno = 0;
1732}
Eric Anholt673a3942008-07-30 12:06:12 -07001733
Chris Wilson963b4832009-09-20 23:03:54 +01001734/* Immediately discard the backing storage */
1735static void
1736i915_gem_object_truncate(struct drm_gem_object *obj)
1737{
Daniel Vetter23010e42010-03-08 13:35:02 +01001738 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001739 struct inode *inode;
Chris Wilson963b4832009-09-20 23:03:54 +01001740
Chris Wilsonae9fed62010-08-07 11:01:30 +01001741 /* Our goal here is to return as much of the memory as
1742 * is possible back to the system as we are called from OOM.
1743 * To do this we must instruct the shmfs to drop all of its
1744 * backing pages, *now*. Here we mirror the actions taken
1745 * when by shmem_delete_inode() to release the backing store.
1746 */
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001747 inode = obj->filp->f_path.dentry->d_inode;
Chris Wilsonae9fed62010-08-07 11:01:30 +01001748 truncate_inode_pages(inode->i_mapping, 0);
1749 if (inode->i_op->truncate_range)
1750 inode->i_op->truncate_range(inode, 0, (loff_t)-1);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001751
1752 obj_priv->madv = __I915_MADV_PURGED;
Chris Wilson963b4832009-09-20 23:03:54 +01001753}
1754
1755static inline int
1756i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj_priv)
1757{
1758 return obj_priv->madv == I915_MADV_DONTNEED;
1759}
1760
Eric Anholt673a3942008-07-30 12:06:12 -07001761static void
1762i915_gem_object_move_to_inactive(struct drm_gem_object *obj)
1763{
1764 struct drm_device *dev = obj->dev;
1765 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001766 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001767
Eric Anholt673a3942008-07-30 12:06:12 -07001768 if (obj_priv->pin_count != 0)
Chris Wilson69dc4982010-10-19 10:36:51 +01001769 list_move_tail(&obj_priv->mm_list, &dev_priv->mm.pinned_list);
Eric Anholt673a3942008-07-30 12:06:12 -07001770 else
Chris Wilson69dc4982010-10-19 10:36:51 +01001771 list_move_tail(&obj_priv->mm_list, &dev_priv->mm.inactive_list);
1772 list_del_init(&obj_priv->ring_list);
Eric Anholt673a3942008-07-30 12:06:12 -07001773
Daniel Vetter99fcb762010-02-07 16:20:18 +01001774 BUG_ON(!list_empty(&obj_priv->gpu_write_list));
1775
Eric Anholtce44b0e2008-11-06 16:00:31 -08001776 obj_priv->last_rendering_seqno = 0;
Zou Nan hai852835f2010-05-21 09:08:56 +08001777 obj_priv->ring = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001778 if (obj_priv->active) {
1779 obj_priv->active = 0;
1780 drm_gem_object_unreference(obj);
1781 }
Chris Wilson23bc5982010-09-29 16:10:57 +01001782 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07001783}
1784
Daniel Vetter63560392010-02-19 11:51:59 +01001785static void
1786i915_gem_process_flushing_list(struct drm_device *dev,
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001787 uint32_t flush_domains,
Zou Nan hai852835f2010-05-21 09:08:56 +08001788 struct intel_ring_buffer *ring)
Daniel Vetter63560392010-02-19 11:51:59 +01001789{
1790 drm_i915_private_t *dev_priv = dev->dev_private;
1791 struct drm_i915_gem_object *obj_priv, *next;
1792
1793 list_for_each_entry_safe(obj_priv, next,
Chris Wilson64193402010-10-24 12:38:05 +01001794 &ring->gpu_write_list,
Daniel Vetter63560392010-02-19 11:51:59 +01001795 gpu_write_list) {
Daniel Vettera8089e82010-04-09 19:05:09 +00001796 struct drm_gem_object *obj = &obj_priv->base;
Daniel Vetter63560392010-02-19 11:51:59 +01001797
Chris Wilson64193402010-10-24 12:38:05 +01001798 if (obj->write_domain & flush_domains) {
Daniel Vetter63560392010-02-19 11:51:59 +01001799 uint32_t old_write_domain = obj->write_domain;
1800
1801 obj->write_domain = 0;
1802 list_del_init(&obj_priv->gpu_write_list);
Daniel Vetter617dbe22010-02-11 22:16:02 +01001803 i915_gem_object_move_to_active(obj, ring);
Daniel Vetter63560392010-02-19 11:51:59 +01001804
1805 /* update the fence lru list */
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001806 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
1807 struct drm_i915_fence_reg *reg =
1808 &dev_priv->fence_regs[obj_priv->fence_reg];
1809 list_move_tail(&reg->lru_list,
Daniel Vetter63560392010-02-19 11:51:59 +01001810 &dev_priv->mm.fence_list);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001811 }
Daniel Vetter63560392010-02-19 11:51:59 +01001812
1813 trace_i915_gem_object_change_domain(obj,
1814 obj->read_domains,
1815 old_write_domain);
1816 }
1817 }
1818}
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001819
Chris Wilson3cce4692010-10-27 16:11:02 +01001820int
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001821i915_add_request(struct drm_device *dev,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001822 struct drm_file *file,
Chris Wilson8dc5d142010-08-12 12:36:12 +01001823 struct drm_i915_gem_request *request,
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001824 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001825{
1826 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001827 struct drm_i915_file_private *file_priv = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001828 uint32_t seqno;
1829 int was_empty;
Chris Wilson3cce4692010-10-27 16:11:02 +01001830 int ret;
1831
1832 BUG_ON(request == NULL);
Eric Anholt673a3942008-07-30 12:06:12 -07001833
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001834 if (file != NULL)
1835 file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00001836
Chris Wilson3cce4692010-10-27 16:11:02 +01001837 ret = ring->add_request(ring, &seqno);
1838 if (ret)
1839 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001840
Chris Wilsona56ba562010-09-28 10:07:56 +01001841 ring->outstanding_lazy_request = false;
Eric Anholt673a3942008-07-30 12:06:12 -07001842
1843 request->seqno = seqno;
Zou Nan hai852835f2010-05-21 09:08:56 +08001844 request->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001845 request->emitted_jiffies = jiffies;
Zou Nan hai852835f2010-05-21 09:08:56 +08001846 was_empty = list_empty(&ring->request_list);
1847 list_add_tail(&request->list, &ring->request_list);
1848
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001849 if (file_priv) {
Chris Wilson1c255952010-09-26 11:03:27 +01001850 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001851 request->file_priv = file_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00001852 list_add_tail(&request->client_list,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001853 &file_priv->mm.request_list);
Chris Wilson1c255952010-09-26 11:03:27 +01001854 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00001855 }
Eric Anholt673a3942008-07-30 12:06:12 -07001856
Ben Gamarif65d9422009-09-14 17:48:44 -04001857 if (!dev_priv->mm.suspended) {
Chris Wilsonb3b079d2010-09-13 23:44:34 +01001858 mod_timer(&dev_priv->hangcheck_timer,
1859 jiffies + msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
Ben Gamarif65d9422009-09-14 17:48:44 -04001860 if (was_empty)
Chris Wilsonb3b079d2010-09-13 23:44:34 +01001861 queue_delayed_work(dev_priv->wq,
1862 &dev_priv->mm.retire_work, HZ);
Ben Gamarif65d9422009-09-14 17:48:44 -04001863 }
Chris Wilson3cce4692010-10-27 16:11:02 +01001864 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001865}
1866
1867/**
1868 * Command execution barrier
1869 *
1870 * Ensures that all commands in the ring are finished
1871 * before signalling the CPU
1872 */
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001873static void
Zou Nan hai852835f2010-05-21 09:08:56 +08001874i915_retire_commands(struct drm_device *dev, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001875{
Eric Anholt673a3942008-07-30 12:06:12 -07001876 uint32_t flush_domains = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001877
1878 /* The sampler always gets flushed on i965 (sigh) */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001879 if (INTEL_INFO(dev)->gen >= 4)
Eric Anholt673a3942008-07-30 12:06:12 -07001880 flush_domains |= I915_GEM_DOMAIN_SAMPLER;
Zou Nan hai852835f2010-05-21 09:08:56 +08001881
Chris Wilson78501ea2010-10-27 12:18:21 +01001882 ring->flush(ring, I915_GEM_DOMAIN_COMMAND, flush_domains);
Eric Anholt673a3942008-07-30 12:06:12 -07001883}
1884
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001885static inline void
1886i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
Eric Anholt673a3942008-07-30 12:06:12 -07001887{
Chris Wilson1c255952010-09-26 11:03:27 +01001888 struct drm_i915_file_private *file_priv = request->file_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07001889
Chris Wilson1c255952010-09-26 11:03:27 +01001890 if (!file_priv)
1891 return;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001892
Chris Wilson1c255952010-09-26 11:03:27 +01001893 spin_lock(&file_priv->mm.lock);
1894 list_del(&request->client_list);
1895 request->file_priv = NULL;
1896 spin_unlock(&file_priv->mm.lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001897}
1898
Chris Wilsondfaae392010-09-22 10:31:52 +01001899static void i915_gem_reset_ring_lists(struct drm_i915_private *dev_priv,
1900 struct intel_ring_buffer *ring)
Chris Wilson9375e442010-09-19 12:21:28 +01001901{
Chris Wilsondfaae392010-09-22 10:31:52 +01001902 while (!list_empty(&ring->request_list)) {
1903 struct drm_i915_gem_request *request;
Chris Wilson9375e442010-09-19 12:21:28 +01001904
Chris Wilsondfaae392010-09-22 10:31:52 +01001905 request = list_first_entry(&ring->request_list,
1906 struct drm_i915_gem_request,
1907 list);
1908
1909 list_del(&request->list);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001910 i915_gem_request_remove_from_client(request);
Chris Wilsondfaae392010-09-22 10:31:52 +01001911 kfree(request);
1912 }
1913
1914 while (!list_empty(&ring->active_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001915 struct drm_i915_gem_object *obj_priv;
1916
Chris Wilsondfaae392010-09-22 10:31:52 +01001917 obj_priv = list_first_entry(&ring->active_list,
Eric Anholt673a3942008-07-30 12:06:12 -07001918 struct drm_i915_gem_object,
Chris Wilson69dc4982010-10-19 10:36:51 +01001919 ring_list);
Eric Anholt673a3942008-07-30 12:06:12 -07001920
Chris Wilsondfaae392010-09-22 10:31:52 +01001921 obj_priv->base.write_domain = 0;
1922 list_del_init(&obj_priv->gpu_write_list);
1923 i915_gem_object_move_to_inactive(&obj_priv->base);
Eric Anholt673a3942008-07-30 12:06:12 -07001924 }
Eric Anholt673a3942008-07-30 12:06:12 -07001925}
1926
Chris Wilson069efc12010-09-30 16:53:18 +01001927void i915_gem_reset(struct drm_device *dev)
Eric Anholt673a3942008-07-30 12:06:12 -07001928{
Chris Wilsondfaae392010-09-22 10:31:52 +01001929 struct drm_i915_private *dev_priv = dev->dev_private;
1930 struct drm_i915_gem_object *obj_priv;
Chris Wilson069efc12010-09-30 16:53:18 +01001931 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07001932
Chris Wilsondfaae392010-09-22 10:31:52 +01001933 i915_gem_reset_ring_lists(dev_priv, &dev_priv->render_ring);
Chris Wilson87acb0a2010-10-19 10:13:00 +01001934 i915_gem_reset_ring_lists(dev_priv, &dev_priv->bsd_ring);
Chris Wilson549f7362010-10-19 11:19:32 +01001935 i915_gem_reset_ring_lists(dev_priv, &dev_priv->blt_ring);
Chris Wilsondfaae392010-09-22 10:31:52 +01001936
1937 /* Remove anything from the flushing lists. The GPU cache is likely
1938 * to be lost on reset along with the data, so simply move the
1939 * lost bo to the inactive list.
1940 */
1941 while (!list_empty(&dev_priv->mm.flushing_list)) {
Chris Wilson9375e442010-09-19 12:21:28 +01001942 obj_priv = list_first_entry(&dev_priv->mm.flushing_list,
1943 struct drm_i915_gem_object,
Chris Wilson69dc4982010-10-19 10:36:51 +01001944 mm_list);
Chris Wilson9375e442010-09-19 12:21:28 +01001945
1946 obj_priv->base.write_domain = 0;
Chris Wilsondfaae392010-09-22 10:31:52 +01001947 list_del_init(&obj_priv->gpu_write_list);
Chris Wilson9375e442010-09-19 12:21:28 +01001948 i915_gem_object_move_to_inactive(&obj_priv->base);
1949 }
Chris Wilson9375e442010-09-19 12:21:28 +01001950
Chris Wilsondfaae392010-09-22 10:31:52 +01001951 /* Move everything out of the GPU domains to ensure we do any
1952 * necessary invalidation upon reuse.
1953 */
Chris Wilson77f01232010-09-19 12:31:36 +01001954 list_for_each_entry(obj_priv,
1955 &dev_priv->mm.inactive_list,
Chris Wilson69dc4982010-10-19 10:36:51 +01001956 mm_list)
Chris Wilson77f01232010-09-19 12:31:36 +01001957 {
1958 obj_priv->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
1959 }
Chris Wilson069efc12010-09-30 16:53:18 +01001960
1961 /* The fence registers are invalidated so clear them out */
1962 for (i = 0; i < 16; i++) {
1963 struct drm_i915_fence_reg *reg;
1964
1965 reg = &dev_priv->fence_regs[i];
1966 if (!reg->obj)
1967 continue;
1968
1969 i915_gem_clear_fence_reg(reg->obj);
1970 }
Eric Anholt673a3942008-07-30 12:06:12 -07001971}
1972
1973/**
1974 * This function clears the request list as sequence numbers are passed.
1975 */
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001976static void
1977i915_gem_retire_requests_ring(struct drm_device *dev,
1978 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001979{
1980 drm_i915_private_t *dev_priv = dev->dev_private;
1981 uint32_t seqno;
1982
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001983 if (!ring->status_page.page_addr ||
1984 list_empty(&ring->request_list))
Karsten Wiese6c0594a2009-02-23 15:07:57 +01001985 return;
1986
Chris Wilson23bc5982010-09-29 16:10:57 +01001987 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07001988
Chris Wilson78501ea2010-10-27 12:18:21 +01001989 seqno = ring->get_seqno(ring);
Zou Nan hai852835f2010-05-21 09:08:56 +08001990 while (!list_empty(&ring->request_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001991 struct drm_i915_gem_request *request;
Eric Anholt673a3942008-07-30 12:06:12 -07001992
Zou Nan hai852835f2010-05-21 09:08:56 +08001993 request = list_first_entry(&ring->request_list,
Eric Anholt673a3942008-07-30 12:06:12 -07001994 struct drm_i915_gem_request,
1995 list);
Eric Anholt673a3942008-07-30 12:06:12 -07001996
Chris Wilsondfaae392010-09-22 10:31:52 +01001997 if (!i915_seqno_passed(seqno, request->seqno))
Eric Anholt673a3942008-07-30 12:06:12 -07001998 break;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001999
2000 trace_i915_gem_request_retire(dev, request->seqno);
2001
2002 list_del(&request->list);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01002003 i915_gem_request_remove_from_client(request);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01002004 kfree(request);
2005 }
2006
2007 /* Move any buffers on the active list that are no longer referenced
2008 * by the ringbuffer to the flushing/inactive lists as appropriate.
2009 */
2010 while (!list_empty(&ring->active_list)) {
2011 struct drm_gem_object *obj;
2012 struct drm_i915_gem_object *obj_priv;
2013
2014 obj_priv = list_first_entry(&ring->active_list,
2015 struct drm_i915_gem_object,
Chris Wilson69dc4982010-10-19 10:36:51 +01002016 ring_list);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01002017
Chris Wilsondfaae392010-09-22 10:31:52 +01002018 if (!i915_seqno_passed(seqno, obj_priv->last_rendering_seqno))
Chris Wilsonb84d5f02010-09-18 01:38:04 +01002019 break;
2020
2021 obj = &obj_priv->base;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01002022 if (obj->write_domain != 0)
2023 i915_gem_object_move_to_flushing(obj);
2024 else
2025 i915_gem_object_move_to_inactive(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002026 }
Chris Wilson9d34e5d2009-09-24 05:26:06 +01002027
2028 if (unlikely (dev_priv->trace_irq_seqno &&
2029 i915_seqno_passed(dev_priv->trace_irq_seqno, seqno))) {
Chris Wilson78501ea2010-10-27 12:18:21 +01002030 ring->user_irq_put(ring);
Chris Wilson9d34e5d2009-09-24 05:26:06 +01002031 dev_priv->trace_irq_seqno = 0;
2032 }
Chris Wilson23bc5982010-09-29 16:10:57 +01002033
2034 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07002035}
2036
2037void
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002038i915_gem_retire_requests(struct drm_device *dev)
2039{
2040 drm_i915_private_t *dev_priv = dev->dev_private;
2041
Chris Wilsonbe726152010-07-23 23:18:50 +01002042 if (!list_empty(&dev_priv->mm.deferred_free_list)) {
2043 struct drm_i915_gem_object *obj_priv, *tmp;
2044
2045 /* We must be careful that during unbind() we do not
2046 * accidentally infinitely recurse into retire requests.
2047 * Currently:
2048 * retire -> free -> unbind -> wait -> retire_ring
2049 */
2050 list_for_each_entry_safe(obj_priv, tmp,
2051 &dev_priv->mm.deferred_free_list,
Chris Wilson69dc4982010-10-19 10:36:51 +01002052 mm_list)
Chris Wilsonbe726152010-07-23 23:18:50 +01002053 i915_gem_free_object_tail(&obj_priv->base);
2054 }
2055
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002056 i915_gem_retire_requests_ring(dev, &dev_priv->render_ring);
Chris Wilson87acb0a2010-10-19 10:13:00 +01002057 i915_gem_retire_requests_ring(dev, &dev_priv->bsd_ring);
Chris Wilson549f7362010-10-19 11:19:32 +01002058 i915_gem_retire_requests_ring(dev, &dev_priv->blt_ring);
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002059}
2060
Daniel Vetter75ef9da2010-08-21 00:25:16 +02002061static void
Eric Anholt673a3942008-07-30 12:06:12 -07002062i915_gem_retire_work_handler(struct work_struct *work)
2063{
2064 drm_i915_private_t *dev_priv;
2065 struct drm_device *dev;
2066
2067 dev_priv = container_of(work, drm_i915_private_t,
2068 mm.retire_work.work);
2069 dev = dev_priv->dev;
2070
Chris Wilson891b48c2010-09-29 12:26:37 +01002071 /* Come back later if the device is busy... */
2072 if (!mutex_trylock(&dev->struct_mutex)) {
2073 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
2074 return;
2075 }
2076
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002077 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08002078
Keith Packard6dbe2772008-10-14 21:41:13 -07002079 if (!dev_priv->mm.suspended &&
Zou Nan haid1b851f2010-05-21 09:08:57 +08002080 (!list_empty(&dev_priv->render_ring.request_list) ||
Chris Wilson549f7362010-10-19 11:19:32 +01002081 !list_empty(&dev_priv->bsd_ring.request_list) ||
2082 !list_empty(&dev_priv->blt_ring.request_list)))
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07002083 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
Eric Anholt673a3942008-07-30 12:06:12 -07002084 mutex_unlock(&dev->struct_mutex);
2085}
2086
Daniel Vetter5a5a0c62009-09-15 22:57:36 +02002087int
Zou Nan hai852835f2010-05-21 09:08:56 +08002088i915_do_wait_request(struct drm_device *dev, uint32_t seqno,
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002089 bool interruptible, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07002090{
2091 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes802c7eb2009-05-05 16:03:48 -07002092 u32 ier;
Eric Anholt673a3942008-07-30 12:06:12 -07002093 int ret = 0;
2094
2095 BUG_ON(seqno == 0);
2096
Ben Gamariba1234d2009-09-14 17:48:47 -04002097 if (atomic_read(&dev_priv->mm.wedged))
Chris Wilson30dbf0c2010-09-25 10:19:17 +01002098 return -EAGAIN;
Ben Gamariffed1d02009-09-14 17:48:41 -04002099
Chris Wilson5d97eb62010-11-10 20:40:02 +00002100 if (seqno == ring->outstanding_lazy_request) {
Chris Wilson3cce4692010-10-27 16:11:02 +01002101 struct drm_i915_gem_request *request;
2102
2103 request = kzalloc(sizeof(*request), GFP_KERNEL);
2104 if (request == NULL)
Daniel Vettere35a41d2010-02-11 22:13:59 +01002105 return -ENOMEM;
Chris Wilson3cce4692010-10-27 16:11:02 +01002106
2107 ret = i915_add_request(dev, NULL, request, ring);
2108 if (ret) {
2109 kfree(request);
2110 return ret;
2111 }
2112
2113 seqno = request->seqno;
Daniel Vettere35a41d2010-02-11 22:13:59 +01002114 }
2115
Chris Wilson78501ea2010-10-27 12:18:21 +01002116 if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) {
Eric Anholtbad720f2009-10-22 16:11:14 -07002117 if (HAS_PCH_SPLIT(dev))
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002118 ier = I915_READ(DEIER) | I915_READ(GTIER);
2119 else
2120 ier = I915_READ(IER);
Jesse Barnes802c7eb2009-05-05 16:03:48 -07002121 if (!ier) {
2122 DRM_ERROR("something (likely vbetool) disabled "
2123 "interrupts, re-enabling\n");
2124 i915_driver_irq_preinstall(dev);
2125 i915_driver_irq_postinstall(dev);
2126 }
2127
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002128 trace_i915_gem_request_wait_begin(dev, seqno);
2129
Chris Wilsonb2223492010-10-27 15:27:33 +01002130 ring->waiting_seqno = seqno;
Chris Wilson78501ea2010-10-27 12:18:21 +01002131 ring->user_irq_get(ring);
Daniel Vetter48764bf2009-09-15 22:57:32 +02002132 if (interruptible)
Zou Nan hai852835f2010-05-21 09:08:56 +08002133 ret = wait_event_interruptible(ring->irq_queue,
Chris Wilson78501ea2010-10-27 12:18:21 +01002134 i915_seqno_passed(ring->get_seqno(ring), seqno)
Zou Nan hai852835f2010-05-21 09:08:56 +08002135 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02002136 else
Zou Nan hai852835f2010-05-21 09:08:56 +08002137 wait_event(ring->irq_queue,
Chris Wilson78501ea2010-10-27 12:18:21 +01002138 i915_seqno_passed(ring->get_seqno(ring), seqno)
Zou Nan hai852835f2010-05-21 09:08:56 +08002139 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02002140
Chris Wilson78501ea2010-10-27 12:18:21 +01002141 ring->user_irq_put(ring);
Chris Wilsonb2223492010-10-27 15:27:33 +01002142 ring->waiting_seqno = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002143
2144 trace_i915_gem_request_wait_end(dev, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07002145 }
Ben Gamariba1234d2009-09-14 17:48:47 -04002146 if (atomic_read(&dev_priv->mm.wedged))
Chris Wilson30dbf0c2010-09-25 10:19:17 +01002147 ret = -EAGAIN;
Eric Anholt673a3942008-07-30 12:06:12 -07002148
2149 if (ret && ret != -ERESTARTSYS)
Daniel Vetter8bff9172010-02-11 22:19:40 +01002150 DRM_ERROR("%s returns %d (awaiting %d at %d, next %d)\n",
Chris Wilson78501ea2010-10-27 12:18:21 +01002151 __func__, ret, seqno, ring->get_seqno(ring),
Daniel Vetter8bff9172010-02-11 22:19:40 +01002152 dev_priv->next_seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07002153
2154 /* Directly dispatch request retiring. While we have the work queue
2155 * to handle this, the waiter on a request often wants an associated
2156 * buffer to have made it to the inactive list, and we would need
2157 * a separate wait queue to handle that.
2158 */
2159 if (ret == 0)
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002160 i915_gem_retire_requests_ring(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07002161
2162 return ret;
2163}
2164
Daniel Vetter48764bf2009-09-15 22:57:32 +02002165/**
2166 * Waits for a sequence number to be signaled, and cleans up the
2167 * request and object lists appropriately for that event.
2168 */
2169static int
Zou Nan hai852835f2010-05-21 09:08:56 +08002170i915_wait_request(struct drm_device *dev, uint32_t seqno,
Chris Wilsona56ba562010-09-28 10:07:56 +01002171 struct intel_ring_buffer *ring)
Daniel Vetter48764bf2009-09-15 22:57:32 +02002172{
Zou Nan hai852835f2010-05-21 09:08:56 +08002173 return i915_do_wait_request(dev, seqno, 1, ring);
Daniel Vetter48764bf2009-09-15 22:57:32 +02002174}
2175
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002176static void
Chris Wilson92204342010-09-18 11:02:01 +01002177i915_gem_flush_ring(struct drm_device *dev,
Chris Wilsonc78ec302010-09-20 12:50:23 +01002178 struct drm_file *file_priv,
Chris Wilson92204342010-09-18 11:02:01 +01002179 struct intel_ring_buffer *ring,
2180 uint32_t invalidate_domains,
2181 uint32_t flush_domains)
2182{
Chris Wilson78501ea2010-10-27 12:18:21 +01002183 ring->flush(ring, invalidate_domains, flush_domains);
Chris Wilson92204342010-09-18 11:02:01 +01002184 i915_gem_process_flushing_list(dev, flush_domains, ring);
2185}
2186
2187static void
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002188i915_gem_flush(struct drm_device *dev,
Chris Wilsonc78ec302010-09-20 12:50:23 +01002189 struct drm_file *file_priv,
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002190 uint32_t invalidate_domains,
Chris Wilson92204342010-09-18 11:02:01 +01002191 uint32_t flush_domains,
2192 uint32_t flush_rings)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002193{
2194 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter8bff9172010-02-11 22:19:40 +01002195
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002196 if (flush_domains & I915_GEM_DOMAIN_CPU)
Daniel Vetter40ce6572010-11-05 18:12:18 +01002197 intel_gtt_chipset_flush();
Zou Nan haid1b851f2010-05-21 09:08:57 +08002198
Chris Wilson92204342010-09-18 11:02:01 +01002199 if ((flush_domains | invalidate_domains) & I915_GEM_GPU_DOMAINS) {
2200 if (flush_rings & RING_RENDER)
Chris Wilsonc78ec302010-09-20 12:50:23 +01002201 i915_gem_flush_ring(dev, file_priv,
Chris Wilson92204342010-09-18 11:02:01 +01002202 &dev_priv->render_ring,
2203 invalidate_domains, flush_domains);
2204 if (flush_rings & RING_BSD)
Chris Wilsonc78ec302010-09-20 12:50:23 +01002205 i915_gem_flush_ring(dev, file_priv,
Chris Wilson92204342010-09-18 11:02:01 +01002206 &dev_priv->bsd_ring,
2207 invalidate_domains, flush_domains);
Chris Wilson549f7362010-10-19 11:19:32 +01002208 if (flush_rings & RING_BLT)
2209 i915_gem_flush_ring(dev, file_priv,
2210 &dev_priv->blt_ring,
2211 invalidate_domains, flush_domains);
Chris Wilson92204342010-09-18 11:02:01 +01002212 }
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002213}
2214
Eric Anholt673a3942008-07-30 12:06:12 -07002215/**
2216 * Ensures that all rendering to the object has completed and the object is
2217 * safe to unbind from the GTT or access from the CPU.
2218 */
2219static int
Chris Wilson2cf34d72010-09-14 13:03:28 +01002220i915_gem_object_wait_rendering(struct drm_gem_object *obj,
2221 bool interruptible)
Eric Anholt673a3942008-07-30 12:06:12 -07002222{
2223 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01002224 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002225 int ret;
2226
Eric Anholte47c68e2008-11-14 13:35:19 -08002227 /* This function only exists to support waiting for existing rendering,
2228 * not for emitting required flushes.
Eric Anholt673a3942008-07-30 12:06:12 -07002229 */
Eric Anholte47c68e2008-11-14 13:35:19 -08002230 BUG_ON((obj->write_domain & I915_GEM_GPU_DOMAINS) != 0);
Eric Anholt673a3942008-07-30 12:06:12 -07002231
2232 /* If there is rendering queued on the buffer being evicted, wait for
2233 * it.
2234 */
2235 if (obj_priv->active) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01002236 ret = i915_do_wait_request(dev,
2237 obj_priv->last_rendering_seqno,
2238 interruptible,
2239 obj_priv->ring);
2240 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002241 return ret;
2242 }
2243
2244 return 0;
2245}
2246
2247/**
2248 * Unbinds an object from the GTT aperture.
2249 */
Jesse Barnes0f973f22009-01-26 17:10:45 -08002250int
Eric Anholt673a3942008-07-30 12:06:12 -07002251i915_gem_object_unbind(struct drm_gem_object *obj)
2252{
2253 struct drm_device *dev = obj->dev;
Chris Wilson73aa8082010-09-30 11:46:12 +01002254 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002255 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002256 int ret = 0;
2257
Eric Anholt673a3942008-07-30 12:06:12 -07002258 if (obj_priv->gtt_space == NULL)
2259 return 0;
2260
2261 if (obj_priv->pin_count != 0) {
2262 DRM_ERROR("Attempting to unbind pinned buffer\n");
2263 return -EINVAL;
2264 }
2265
Eric Anholt5323fd02009-09-09 11:50:45 -07002266 /* blow away mappings if mapped through GTT */
2267 i915_gem_release_mmap(obj);
2268
Eric Anholt673a3942008-07-30 12:06:12 -07002269 /* Move the object to the CPU domain to ensure that
2270 * any possible CPU writes while it's not in the GTT
2271 * are flushed when we go to remap it. This will
2272 * also ensure that all pending GPU writes are finished
2273 * before we unbind.
2274 */
Eric Anholte47c68e2008-11-14 13:35:19 -08002275 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Chris Wilson8dc17752010-07-23 23:18:51 +01002276 if (ret == -ERESTARTSYS)
Eric Anholt673a3942008-07-30 12:06:12 -07002277 return ret;
Chris Wilson8dc17752010-07-23 23:18:51 +01002278 /* Continue on if we fail due to EIO, the GPU is hung so we
2279 * should be safe and we need to cleanup or else we might
2280 * cause memory corruption through use-after-free.
2281 */
Chris Wilson812ed4922010-09-30 15:08:57 +01002282 if (ret) {
2283 i915_gem_clflush_object(obj);
2284 obj->read_domains = obj->write_domain = I915_GEM_DOMAIN_CPU;
2285 }
Eric Anholt673a3942008-07-30 12:06:12 -07002286
Daniel Vetter96b47b62009-12-15 17:50:00 +01002287 /* release the fence reg _after_ flushing */
2288 if (obj_priv->fence_reg != I915_FENCE_REG_NONE)
2289 i915_gem_clear_fence_reg(obj);
2290
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002291 i915_gem_gtt_unbind_object(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002292
Chris Wilsone5281cc2010-10-28 13:45:36 +01002293 i915_gem_object_put_pages_gtt(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002294
Chris Wilsona00b10c2010-09-24 21:15:47 +01002295 i915_gem_info_remove_gtt(dev_priv, obj_priv);
Chris Wilson69dc4982010-10-19 10:36:51 +01002296 list_del_init(&obj_priv->mm_list);
Daniel Vetter75e9e912010-11-04 17:11:09 +01002297 /* Avoid an unnecessary call to unbind on rebind. */
2298 obj_priv->map_and_fenceable = true;
Eric Anholt673a3942008-07-30 12:06:12 -07002299
Chris Wilson73aa8082010-09-30 11:46:12 +01002300 drm_mm_put_block(obj_priv->gtt_space);
2301 obj_priv->gtt_space = NULL;
Chris Wilson9af90d12010-10-17 10:01:56 +01002302 obj_priv->gtt_offset = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002303
Chris Wilson963b4832009-09-20 23:03:54 +01002304 if (i915_gem_object_is_purgeable(obj_priv))
2305 i915_gem_object_truncate(obj);
2306
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002307 trace_i915_gem_object_unbind(obj);
2308
Chris Wilson8dc17752010-07-23 23:18:51 +01002309 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002310}
2311
Chris Wilsona56ba562010-09-28 10:07:56 +01002312static int i915_ring_idle(struct drm_device *dev,
2313 struct intel_ring_buffer *ring)
2314{
Chris Wilson395b70b2010-10-28 21:28:46 +01002315 if (list_empty(&ring->gpu_write_list) && list_empty(&ring->active_list))
Chris Wilson64193402010-10-24 12:38:05 +01002316 return 0;
2317
Chris Wilsona56ba562010-09-28 10:07:56 +01002318 i915_gem_flush_ring(dev, NULL, ring,
2319 I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
2320 return i915_wait_request(dev,
2321 i915_gem_next_request_seqno(dev, ring),
2322 ring);
2323}
2324
Chris Wilsonb47eb4a2010-08-07 11:01:23 +01002325int
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002326i915_gpu_idle(struct drm_device *dev)
2327{
2328 drm_i915_private_t *dev_priv = dev->dev_private;
2329 bool lists_empty;
Zou Nan hai852835f2010-05-21 09:08:56 +08002330 int ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002331
Zou Nan haid1b851f2010-05-21 09:08:57 +08002332 lists_empty = (list_empty(&dev_priv->mm.flushing_list) &&
Chris Wilson395b70b2010-10-28 21:28:46 +01002333 list_empty(&dev_priv->mm.active_list));
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002334 if (lists_empty)
2335 return 0;
2336
2337 /* Flush everything onto the inactive list. */
Chris Wilsona56ba562010-09-28 10:07:56 +01002338 ret = i915_ring_idle(dev, &dev_priv->render_ring);
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002339 if (ret)
2340 return ret;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002341
Chris Wilson87acb0a2010-10-19 10:13:00 +01002342 ret = i915_ring_idle(dev, &dev_priv->bsd_ring);
2343 if (ret)
2344 return ret;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002345
Chris Wilson549f7362010-10-19 11:19:32 +01002346 ret = i915_ring_idle(dev, &dev_priv->blt_ring);
2347 if (ret)
2348 return ret;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002349
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002350 return 0;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002351}
2352
Chris Wilsona00b10c2010-09-24 21:15:47 +01002353static void sandybridge_write_fence_reg(struct drm_gem_object *obj)
Eric Anholt4e901fd2009-10-26 16:44:17 -07002354{
Eric Anholt4e901fd2009-10-26 16:44:17 -07002355 struct drm_device *dev = obj->dev;
2356 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002357 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsona00b10c2010-09-24 21:15:47 +01002358 u32 size = i915_gem_get_gtt_size(obj_priv);
Eric Anholt4e901fd2009-10-26 16:44:17 -07002359 int regnum = obj_priv->fence_reg;
2360 uint64_t val;
2361
Chris Wilsona00b10c2010-09-24 21:15:47 +01002362 val = (uint64_t)((obj_priv->gtt_offset + size - 4096) &
Eric Anholt4e901fd2009-10-26 16:44:17 -07002363 0xfffff000) << 32;
2364 val |= obj_priv->gtt_offset & 0xfffff000;
2365 val |= (uint64_t)((obj_priv->stride / 128) - 1) <<
2366 SANDYBRIDGE_FENCE_PITCH_SHIFT;
2367
2368 if (obj_priv->tiling_mode == I915_TILING_Y)
2369 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2370 val |= I965_FENCE_REG_VALID;
2371
2372 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (regnum * 8), val);
2373}
2374
Chris Wilsona00b10c2010-09-24 21:15:47 +01002375static void i965_write_fence_reg(struct drm_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002376{
Jesse Barnesde151cf2008-11-12 10:03:55 -08002377 struct drm_device *dev = obj->dev;
2378 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002379 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsona00b10c2010-09-24 21:15:47 +01002380 u32 size = i915_gem_get_gtt_size(obj_priv);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002381 int regnum = obj_priv->fence_reg;
2382 uint64_t val;
2383
Chris Wilsona00b10c2010-09-24 21:15:47 +01002384 val = (uint64_t)((obj_priv->gtt_offset + size - 4096) &
Jesse Barnesde151cf2008-11-12 10:03:55 -08002385 0xfffff000) << 32;
2386 val |= obj_priv->gtt_offset & 0xfffff000;
2387 val |= ((obj_priv->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
2388 if (obj_priv->tiling_mode == I915_TILING_Y)
2389 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2390 val |= I965_FENCE_REG_VALID;
2391
2392 I915_WRITE64(FENCE_REG_965_0 + (regnum * 8), val);
2393}
2394
Chris Wilsona00b10c2010-09-24 21:15:47 +01002395static void i915_write_fence_reg(struct drm_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002396{
Jesse Barnesde151cf2008-11-12 10:03:55 -08002397 struct drm_device *dev = obj->dev;
2398 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002399 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsona00b10c2010-09-24 21:15:47 +01002400 u32 size = i915_gem_get_gtt_size(obj_priv);
2401 uint32_t fence_reg, val, pitch_val;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002402 int tile_width;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002403
2404 if ((obj_priv->gtt_offset & ~I915_FENCE_START_MASK) ||
Chris Wilsona00b10c2010-09-24 21:15:47 +01002405 (obj_priv->gtt_offset & (size - 1))) {
2406 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 +01002407 __func__, obj_priv->gtt_offset, obj_priv->map_and_fenceable, size,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002408 obj_priv->gtt_space->start, obj_priv->gtt_space->size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002409 return;
2410 }
2411
Jesse Barnes0f973f22009-01-26 17:10:45 -08002412 if (obj_priv->tiling_mode == I915_TILING_Y &&
2413 HAS_128_BYTE_Y_TILING(dev))
2414 tile_width = 128;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002415 else
Jesse Barnes0f973f22009-01-26 17:10:45 -08002416 tile_width = 512;
2417
2418 /* Note: pitch better be a power of two tile widths */
2419 pitch_val = obj_priv->stride / tile_width;
2420 pitch_val = ffs(pitch_val) - 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002421
Daniel Vetterc36a2a62010-04-17 15:12:03 +02002422 if (obj_priv->tiling_mode == I915_TILING_Y &&
2423 HAS_128_BYTE_Y_TILING(dev))
2424 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2425 else
2426 WARN_ON(pitch_val > I915_FENCE_MAX_PITCH_VAL);
2427
Jesse Barnesde151cf2008-11-12 10:03:55 -08002428 val = obj_priv->gtt_offset;
2429 if (obj_priv->tiling_mode == I915_TILING_Y)
2430 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002431 val |= I915_FENCE_SIZE_BITS(size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002432 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2433 val |= I830_FENCE_REG_VALID;
2434
Chris Wilsona00b10c2010-09-24 21:15:47 +01002435 fence_reg = obj_priv->fence_reg;
2436 if (fence_reg < 8)
2437 fence_reg = FENCE_REG_830_0 + fence_reg * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002438 else
Chris Wilsona00b10c2010-09-24 21:15:47 +01002439 fence_reg = FENCE_REG_945_8 + (fence_reg - 8) * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002440 I915_WRITE(fence_reg, val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002441}
2442
Chris Wilsona00b10c2010-09-24 21:15:47 +01002443static void i830_write_fence_reg(struct drm_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002444{
Jesse Barnesde151cf2008-11-12 10:03:55 -08002445 struct drm_device *dev = obj->dev;
2446 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002447 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsona00b10c2010-09-24 21:15:47 +01002448 u32 size = i915_gem_get_gtt_size(obj_priv);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002449 int regnum = obj_priv->fence_reg;
2450 uint32_t val;
2451 uint32_t pitch_val;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002452 uint32_t fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002453
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002454 if ((obj_priv->gtt_offset & ~I830_FENCE_START_MASK) ||
Jesse Barnesde151cf2008-11-12 10:03:55 -08002455 (obj_priv->gtt_offset & (obj->size - 1))) {
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002456 WARN(1, "%s: object 0x%08x not 512K or size aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002457 __func__, obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002458 return;
2459 }
2460
Eric Anholte76a16d2009-05-26 17:44:56 -07002461 pitch_val = obj_priv->stride / 128;
2462 pitch_val = ffs(pitch_val) - 1;
2463 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2464
Jesse Barnesde151cf2008-11-12 10:03:55 -08002465 val = obj_priv->gtt_offset;
2466 if (obj_priv->tiling_mode == I915_TILING_Y)
2467 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002468 fence_size_bits = I830_FENCE_SIZE_BITS(size);
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002469 WARN_ON(fence_size_bits & ~0x00000f00);
2470 val |= fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002471 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2472 val |= I830_FENCE_REG_VALID;
2473
2474 I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002475}
2476
Chris Wilson2cf34d72010-09-14 13:03:28 +01002477static int i915_find_fence_reg(struct drm_device *dev,
2478 bool interruptible)
Daniel Vetterae3db242010-02-19 11:51:58 +01002479{
Daniel Vetterae3db242010-02-19 11:51:58 +01002480 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002481 struct drm_i915_fence_reg *reg;
2482 struct drm_i915_gem_object *obj_priv = NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002483 int i, avail, ret;
2484
2485 /* First try to find a free reg */
2486 avail = 0;
2487 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2488 reg = &dev_priv->fence_regs[i];
2489 if (!reg->obj)
2490 return i;
2491
Daniel Vetter23010e42010-03-08 13:35:02 +01002492 obj_priv = to_intel_bo(reg->obj);
Daniel Vetterae3db242010-02-19 11:51:58 +01002493 if (!obj_priv->pin_count)
2494 avail++;
2495 }
2496
2497 if (avail == 0)
2498 return -ENOSPC;
2499
2500 /* None available, try to steal one or wait for a user to finish */
Chris Wilsona00b10c2010-09-24 21:15:47 +01002501 avail = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002502 list_for_each_entry(reg, &dev_priv->mm.fence_list,
2503 lru_list) {
Chris Wilsona00b10c2010-09-24 21:15:47 +01002504 obj_priv = to_intel_bo(reg->obj);
Daniel Vetterae3db242010-02-19 11:51:58 +01002505 if (obj_priv->pin_count)
2506 continue;
2507
2508 /* found one! */
Chris Wilsona00b10c2010-09-24 21:15:47 +01002509 avail = obj_priv->fence_reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01002510 break;
2511 }
2512
Chris Wilsona00b10c2010-09-24 21:15:47 +01002513 BUG_ON(avail == I915_FENCE_REG_NONE);
Daniel Vetterae3db242010-02-19 11:51:58 +01002514
2515 /* We only have a reference on obj from the active list. put_fence_reg
2516 * might drop that one, causing a use-after-free in it. So hold a
2517 * private reference to obj like the other callers of put_fence_reg
2518 * (set_tiling ioctl) do. */
Chris Wilsona00b10c2010-09-24 21:15:47 +01002519 drm_gem_object_reference(&obj_priv->base);
2520 ret = i915_gem_object_put_fence_reg(&obj_priv->base, interruptible);
2521 drm_gem_object_unreference(&obj_priv->base);
Daniel Vetterae3db242010-02-19 11:51:58 +01002522 if (ret != 0)
2523 return ret;
2524
Chris Wilsona00b10c2010-09-24 21:15:47 +01002525 return avail;
Daniel Vetterae3db242010-02-19 11:51:58 +01002526}
2527
Jesse Barnesde151cf2008-11-12 10:03:55 -08002528/**
2529 * i915_gem_object_get_fence_reg - set up a fence reg for an object
2530 * @obj: object to map through a fence reg
2531 *
2532 * When mapping objects through the GTT, userspace wants to be able to write
2533 * to them without having to worry about swizzling if the object is tiled.
2534 *
2535 * This function walks the fence regs looking for a free one for @obj,
2536 * stealing one if it can't find any.
2537 *
2538 * It then sets up the reg based on the object's properties: address, pitch
2539 * and tiling format.
2540 */
Chris Wilson8c4b8c32009-06-17 22:08:52 +01002541int
Chris Wilson2cf34d72010-09-14 13:03:28 +01002542i915_gem_object_get_fence_reg(struct drm_gem_object *obj,
2543 bool interruptible)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002544{
2545 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002546 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002547 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002548 struct drm_i915_fence_reg *reg = NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002549 int ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002550
Eric Anholta09ba7f2009-08-29 12:49:51 -07002551 /* Just update our place in the LRU if our fence is getting used. */
2552 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002553 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
2554 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002555 return 0;
2556 }
2557
Jesse Barnesde151cf2008-11-12 10:03:55 -08002558 switch (obj_priv->tiling_mode) {
2559 case I915_TILING_NONE:
2560 WARN(1, "allocating a fence for non-tiled object?\n");
2561 break;
2562 case I915_TILING_X:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002563 if (!obj_priv->stride)
2564 return -EINVAL;
2565 WARN((obj_priv->stride & (512 - 1)),
2566 "object 0x%08x is X tiled but has non-512B pitch\n",
2567 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002568 break;
2569 case I915_TILING_Y:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002570 if (!obj_priv->stride)
2571 return -EINVAL;
2572 WARN((obj_priv->stride & (128 - 1)),
2573 "object 0x%08x is Y tiled but has non-128B pitch\n",
2574 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002575 break;
2576 }
2577
Chris Wilson2cf34d72010-09-14 13:03:28 +01002578 ret = i915_find_fence_reg(dev, interruptible);
Daniel Vetterae3db242010-02-19 11:51:58 +01002579 if (ret < 0)
2580 return ret;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002581
Daniel Vetterae3db242010-02-19 11:51:58 +01002582 obj_priv->fence_reg = ret;
2583 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002584 list_add_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002585
Jesse Barnesde151cf2008-11-12 10:03:55 -08002586 reg->obj = obj;
2587
Chris Wilsone259bef2010-09-17 00:32:02 +01002588 switch (INTEL_INFO(dev)->gen) {
2589 case 6:
Chris Wilsona00b10c2010-09-24 21:15:47 +01002590 sandybridge_write_fence_reg(obj);
Chris Wilsone259bef2010-09-17 00:32:02 +01002591 break;
2592 case 5:
2593 case 4:
Chris Wilsona00b10c2010-09-24 21:15:47 +01002594 i965_write_fence_reg(obj);
Chris Wilsone259bef2010-09-17 00:32:02 +01002595 break;
2596 case 3:
Chris Wilsona00b10c2010-09-24 21:15:47 +01002597 i915_write_fence_reg(obj);
Chris Wilsone259bef2010-09-17 00:32:02 +01002598 break;
2599 case 2:
Chris Wilsona00b10c2010-09-24 21:15:47 +01002600 i830_write_fence_reg(obj);
Chris Wilsone259bef2010-09-17 00:32:02 +01002601 break;
2602 }
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002603
Chris Wilsona00b10c2010-09-24 21:15:47 +01002604 trace_i915_gem_object_get_fence(obj,
2605 obj_priv->fence_reg,
2606 obj_priv->tiling_mode);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002607
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002608 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002609}
2610
2611/**
2612 * i915_gem_clear_fence_reg - clear out fence register info
2613 * @obj: object to clear
2614 *
2615 * Zeroes out the fence register itself and clears out the associated
2616 * data structures in dev_priv and obj_priv.
2617 */
2618static void
2619i915_gem_clear_fence_reg(struct drm_gem_object *obj)
2620{
2621 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002622 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002623 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002624 struct drm_i915_fence_reg *reg =
2625 &dev_priv->fence_regs[obj_priv->fence_reg];
Chris Wilsone259bef2010-09-17 00:32:02 +01002626 uint32_t fence_reg;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002627
Chris Wilsone259bef2010-09-17 00:32:02 +01002628 switch (INTEL_INFO(dev)->gen) {
2629 case 6:
Eric Anholt4e901fd2009-10-26 16:44:17 -07002630 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 +
2631 (obj_priv->fence_reg * 8), 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002632 break;
2633 case 5:
2634 case 4:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002635 I915_WRITE64(FENCE_REG_965_0 + (obj_priv->fence_reg * 8), 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002636 break;
2637 case 3:
Chris Wilson9b74f732010-09-22 19:10:44 +01002638 if (obj_priv->fence_reg >= 8)
Chris Wilsone259bef2010-09-17 00:32:02 +01002639 fence_reg = FENCE_REG_945_8 + (obj_priv->fence_reg - 8) * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002640 else
Chris Wilsone259bef2010-09-17 00:32:02 +01002641 case 2:
2642 fence_reg = FENCE_REG_830_0 + obj_priv->fence_reg * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002643
2644 I915_WRITE(fence_reg, 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002645 break;
Eric Anholtdc529a42009-03-10 22:34:49 -07002646 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002647
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002648 reg->obj = NULL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002649 obj_priv->fence_reg = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002650 list_del_init(&reg->lru_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002651}
2652
Eric Anholt673a3942008-07-30 12:06:12 -07002653/**
Chris Wilson52dc7d32009-06-06 09:46:01 +01002654 * i915_gem_object_put_fence_reg - waits on outstanding fenced access
2655 * to the buffer to finish, and then resets the fence register.
2656 * @obj: tiled object holding a fence register.
Chris Wilson2cf34d72010-09-14 13:03:28 +01002657 * @bool: whether the wait upon the fence is interruptible
Chris Wilson52dc7d32009-06-06 09:46:01 +01002658 *
2659 * Zeroes out the fence register itself and clears out the associated
2660 * data structures in dev_priv and obj_priv.
2661 */
2662int
Chris Wilson2cf34d72010-09-14 13:03:28 +01002663i915_gem_object_put_fence_reg(struct drm_gem_object *obj,
2664 bool interruptible)
Chris Wilson52dc7d32009-06-06 09:46:01 +01002665{
2666 struct drm_device *dev = obj->dev;
Chris Wilson53640e12010-09-20 11:40:50 +01002667 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002668 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson53640e12010-09-20 11:40:50 +01002669 struct drm_i915_fence_reg *reg;
Chris Wilson52dc7d32009-06-06 09:46:01 +01002670
2671 if (obj_priv->fence_reg == I915_FENCE_REG_NONE)
2672 return 0;
2673
Daniel Vetter10ae9bd2010-02-01 13:59:17 +01002674 /* If we've changed tiling, GTT-mappings of the object
2675 * need to re-fault to ensure that the correct fence register
2676 * setup is in place.
2677 */
2678 i915_gem_release_mmap(obj);
2679
Chris Wilson52dc7d32009-06-06 09:46:01 +01002680 /* On the i915, GPU access to tiled buffers is via a fence,
2681 * therefore we must wait for any outstanding access to complete
2682 * before clearing the fence.
2683 */
Chris Wilson53640e12010-09-20 11:40:50 +01002684 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
2685 if (reg->gpu) {
Chris Wilson52dc7d32009-06-06 09:46:01 +01002686 int ret;
2687
Chris Wilson2cf34d72010-09-14 13:03:28 +01002688 ret = i915_gem_object_flush_gpu_write_domain(obj, true);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002689 if (ret)
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002690 return ret;
2691
Chris Wilson2cf34d72010-09-14 13:03:28 +01002692 ret = i915_gem_object_wait_rendering(obj, interruptible);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002693 if (ret)
Chris Wilson52dc7d32009-06-06 09:46:01 +01002694 return ret;
Chris Wilson53640e12010-09-20 11:40:50 +01002695
2696 reg->gpu = false;
Chris Wilson52dc7d32009-06-06 09:46:01 +01002697 }
2698
Daniel Vetter4a726612010-02-01 13:59:16 +01002699 i915_gem_object_flush_gtt_write_domain(obj);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002700 i915_gem_clear_fence_reg(obj);
Chris Wilson52dc7d32009-06-06 09:46:01 +01002701
2702 return 0;
2703}
2704
2705/**
Eric Anholt673a3942008-07-30 12:06:12 -07002706 * Finds free space in the GTT aperture and binds the object there.
2707 */
2708static int
Daniel Vetter920afa72010-09-16 17:54:23 +02002709i915_gem_object_bind_to_gtt(struct drm_gem_object *obj,
2710 unsigned alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01002711 bool map_and_fenceable)
Eric Anholt673a3942008-07-30 12:06:12 -07002712{
2713 struct drm_device *dev = obj->dev;
2714 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002715 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002716 struct drm_mm_node *free_space;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002717 gfp_t gfpmask = __GFP_NORETRY | __GFP_NOWARN;
Daniel Vetter5e783302010-11-14 22:32:36 +01002718 u32 size, fence_size, fence_alignment, unfenced_alignment;
Daniel Vetter75e9e912010-11-04 17:11:09 +01002719 bool mappable, fenceable;
Chris Wilson07f73f62009-09-14 16:50:30 +01002720 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002721
Chris Wilsonbb6baf72009-09-22 14:24:13 +01002722 if (obj_priv->madv != I915_MADV_WILLNEED) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01002723 DRM_ERROR("Attempting to bind a purgeable object\n");
2724 return -EINVAL;
2725 }
2726
Chris Wilsona00b10c2010-09-24 21:15:47 +01002727 fence_size = i915_gem_get_gtt_size(obj_priv);
2728 fence_alignment = i915_gem_get_gtt_alignment(obj_priv);
Daniel Vetter5e783302010-11-14 22:32:36 +01002729 unfenced_alignment = i915_gem_get_unfenced_gtt_alignment(obj_priv);
Chris Wilsona00b10c2010-09-24 21:15:47 +01002730
Eric Anholt673a3942008-07-30 12:06:12 -07002731 if (alignment == 0)
Daniel Vetter5e783302010-11-14 22:32:36 +01002732 alignment = map_and_fenceable ? fence_alignment :
2733 unfenced_alignment;
Daniel Vetter75e9e912010-11-04 17:11:09 +01002734 if (map_and_fenceable && alignment & (fence_alignment - 1)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002735 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2736 return -EINVAL;
2737 }
2738
Daniel Vetter75e9e912010-11-04 17:11:09 +01002739 size = map_and_fenceable ? fence_size : obj->size;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002740
Chris Wilson654fc602010-05-27 13:18:21 +01002741 /* If the object is bigger than the entire aperture, reject it early
2742 * before evicting everything in a vain attempt to find space.
2743 */
Daniel Vetter920afa72010-09-16 17:54:23 +02002744 if (obj->size >
Daniel Vetter75e9e912010-11-04 17:11:09 +01002745 (map_and_fenceable ? dev_priv->mm.gtt_mappable_end : dev_priv->mm.gtt_total)) {
Chris Wilson654fc602010-05-27 13:18:21 +01002746 DRM_ERROR("Attempting to bind an object larger than the aperture\n");
2747 return -E2BIG;
2748 }
2749
Eric Anholt673a3942008-07-30 12:06:12 -07002750 search_free:
Daniel Vetter75e9e912010-11-04 17:11:09 +01002751 if (map_and_fenceable)
Daniel Vetter920afa72010-09-16 17:54:23 +02002752 free_space =
2753 drm_mm_search_free_in_range(&dev_priv->mm.gtt_space,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002754 size, alignment, 0,
Daniel Vetter920afa72010-09-16 17:54:23 +02002755 dev_priv->mm.gtt_mappable_end,
2756 0);
2757 else
2758 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002759 size, alignment, 0);
Daniel Vetter920afa72010-09-16 17:54:23 +02002760
2761 if (free_space != NULL) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01002762 if (map_and_fenceable)
Daniel Vetter920afa72010-09-16 17:54:23 +02002763 obj_priv->gtt_space =
2764 drm_mm_get_block_range_generic(free_space,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002765 size, alignment, 0,
Daniel Vetter920afa72010-09-16 17:54:23 +02002766 dev_priv->mm.gtt_mappable_end,
2767 0);
2768 else
2769 obj_priv->gtt_space =
Chris Wilsona00b10c2010-09-24 21:15:47 +01002770 drm_mm_get_block(free_space, size, alignment);
Daniel Vetter920afa72010-09-16 17:54:23 +02002771 }
Eric Anholt673a3942008-07-30 12:06:12 -07002772 if (obj_priv->gtt_space == NULL) {
2773 /* If the gtt is empty and we're still having trouble
2774 * fitting our object in, we're out of memory.
2775 */
Daniel Vetter75e9e912010-11-04 17:11:09 +01002776 ret = i915_gem_evict_something(dev, size, alignment,
2777 map_and_fenceable);
Chris Wilson97311292009-09-21 00:22:34 +01002778 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002779 return ret;
Chris Wilson97311292009-09-21 00:22:34 +01002780
Eric Anholt673a3942008-07-30 12:06:12 -07002781 goto search_free;
2782 }
2783
Chris Wilsone5281cc2010-10-28 13:45:36 +01002784 ret = i915_gem_object_get_pages_gtt(obj, gfpmask);
Eric Anholt673a3942008-07-30 12:06:12 -07002785 if (ret) {
2786 drm_mm_put_block(obj_priv->gtt_space);
2787 obj_priv->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002788
2789 if (ret == -ENOMEM) {
2790 /* first try to clear up some space from the GTT */
Chris Wilsona00b10c2010-09-24 21:15:47 +01002791 ret = i915_gem_evict_something(dev, size,
Daniel Vetter75e9e912010-11-04 17:11:09 +01002792 alignment,
2793 map_and_fenceable);
Chris Wilson07f73f62009-09-14 16:50:30 +01002794 if (ret) {
Chris Wilson07f73f62009-09-14 16:50:30 +01002795 /* now try to shrink everyone else */
Chris Wilson4bdadb92010-01-27 13:36:32 +00002796 if (gfpmask) {
2797 gfpmask = 0;
2798 goto search_free;
Chris Wilson07f73f62009-09-14 16:50:30 +01002799 }
2800
2801 return ret;
2802 }
2803
2804 goto search_free;
2805 }
2806
Eric Anholt673a3942008-07-30 12:06:12 -07002807 return ret;
2808 }
2809
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002810 ret = i915_gem_gtt_bind_object(obj);
2811 if (ret) {
Chris Wilsone5281cc2010-10-28 13:45:36 +01002812 i915_gem_object_put_pages_gtt(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002813 drm_mm_put_block(obj_priv->gtt_space);
2814 obj_priv->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002815
Chris Wilsona00b10c2010-09-24 21:15:47 +01002816 ret = i915_gem_evict_something(dev, size,
Daniel Vetter75e9e912010-11-04 17:11:09 +01002817 alignment, map_and_fenceable);
Chris Wilson97311292009-09-21 00:22:34 +01002818 if (ret)
Chris Wilson07f73f62009-09-14 16:50:30 +01002819 return ret;
Chris Wilson07f73f62009-09-14 16:50:30 +01002820
2821 goto search_free;
Eric Anholt673a3942008-07-30 12:06:12 -07002822 }
Eric Anholt673a3942008-07-30 12:06:12 -07002823
Daniel Vetterfb7d5162010-10-01 22:05:20 +02002824 obj_priv->gtt_offset = obj_priv->gtt_space->start;
2825
Chris Wilsonbf1a1092010-08-07 11:01:20 +01002826 /* keep track of bounds object by adding it to the inactive list */
Chris Wilson69dc4982010-10-19 10:36:51 +01002827 list_add_tail(&obj_priv->mm_list, &dev_priv->mm.inactive_list);
Chris Wilsona00b10c2010-09-24 21:15:47 +01002828 i915_gem_info_add_gtt(dev_priv, obj_priv);
Chris Wilsonbf1a1092010-08-07 11:01:20 +01002829
Eric Anholt673a3942008-07-30 12:06:12 -07002830 /* Assert that the object is not currently in any GPU domain. As it
2831 * wasn't in the GTT, there shouldn't be any way it could have been in
2832 * a GPU cache
2833 */
Chris Wilson21d509e2009-06-06 09:46:02 +01002834 BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS);
2835 BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS);
Eric Anholt673a3942008-07-30 12:06:12 -07002836
Daniel Vetter75e9e912010-11-04 17:11:09 +01002837 trace_i915_gem_object_bind(obj, obj_priv->gtt_offset, map_and_fenceable);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002838
Daniel Vetter75e9e912010-11-04 17:11:09 +01002839 fenceable =
Chris Wilsona00b10c2010-09-24 21:15:47 +01002840 obj_priv->gtt_space->size == fence_size &&
2841 (obj_priv->gtt_space->start & (fence_alignment -1)) == 0;
2842
Daniel Vetter75e9e912010-11-04 17:11:09 +01002843 mappable =
Chris Wilsona00b10c2010-09-24 21:15:47 +01002844 obj_priv->gtt_offset + obj->size <= dev_priv->mm.gtt_mappable_end;
2845
Daniel Vetter75e9e912010-11-04 17:11:09 +01002846 obj_priv->map_and_fenceable = mappable && fenceable;
2847
Eric Anholt673a3942008-07-30 12:06:12 -07002848 return 0;
2849}
2850
2851void
2852i915_gem_clflush_object(struct drm_gem_object *obj)
2853{
Daniel Vetter23010e42010-03-08 13:35:02 +01002854 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002855
2856 /* If we don't have a page list set up, then we're not pinned
2857 * to GPU, and we can ignore the cache flush because it'll happen
2858 * again at bind time.
2859 */
Eric Anholt856fa192009-03-19 14:10:50 -07002860 if (obj_priv->pages == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002861 return;
2862
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002863 trace_i915_gem_object_clflush(obj);
Eric Anholtcfa16a02009-05-26 18:46:16 -07002864
Eric Anholt856fa192009-03-19 14:10:50 -07002865 drm_clflush_pages(obj_priv->pages, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002866}
2867
Eric Anholte47c68e2008-11-14 13:35:19 -08002868/** Flushes any GPU write domain for the object if it's dirty. */
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002869static int
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002870i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj,
2871 bool pipelined)
Eric Anholte47c68e2008-11-14 13:35:19 -08002872{
2873 struct drm_device *dev = obj->dev;
Eric Anholte47c68e2008-11-14 13:35:19 -08002874
2875 if ((obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002876 return 0;
Eric Anholte47c68e2008-11-14 13:35:19 -08002877
2878 /* Queue the GPU write cache flushing we need. */
Chris Wilsonc78ec302010-09-20 12:50:23 +01002879 i915_gem_flush_ring(dev, NULL,
Chris Wilson92204342010-09-18 11:02:01 +01002880 to_intel_bo(obj)->ring,
2881 0, obj->write_domain);
Chris Wilson48b956c2010-09-14 12:50:34 +01002882 BUG_ON(obj->write_domain);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002883
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002884 if (pipelined)
2885 return 0;
2886
Chris Wilson2cf34d72010-09-14 13:03:28 +01002887 return i915_gem_object_wait_rendering(obj, true);
Eric Anholte47c68e2008-11-14 13:35:19 -08002888}
2889
2890/** Flushes the GTT write domain for the object if it's dirty. */
2891static void
2892i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj)
2893{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002894 uint32_t old_write_domain;
2895
Eric Anholte47c68e2008-11-14 13:35:19 -08002896 if (obj->write_domain != I915_GEM_DOMAIN_GTT)
2897 return;
2898
2899 /* No actual flushing is required for the GTT write domain. Writes
2900 * to it immediately go to main memory as far as we know, so there's
2901 * no chipset flush. It also doesn't land in render cache.
2902 */
Chris Wilson4a684a42010-10-28 14:44:08 +01002903 i915_gem_release_mmap(obj);
2904
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002905 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002906 obj->write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002907
2908 trace_i915_gem_object_change_domain(obj,
2909 obj->read_domains,
2910 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002911}
2912
2913/** Flushes the CPU write domain for the object if it's dirty. */
2914static void
2915i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj)
2916{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002917 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002918
2919 if (obj->write_domain != I915_GEM_DOMAIN_CPU)
2920 return;
2921
2922 i915_gem_clflush_object(obj);
Daniel Vetter40ce6572010-11-05 18:12:18 +01002923 intel_gtt_chipset_flush();
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002924 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002925 obj->write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002926
2927 trace_i915_gem_object_change_domain(obj,
2928 obj->read_domains,
2929 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002930}
2931
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002932/**
2933 * Moves a single object to the GTT read, and possibly write domain.
2934 *
2935 * This function returns when the move is complete, including waiting on
2936 * flushes to occur.
2937 */
Jesse Barnes79e53942008-11-07 14:24:08 -08002938int
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002939i915_gem_object_set_to_gtt_domain(struct drm_gem_object *obj, int write)
2940{
Daniel Vetter23010e42010-03-08 13:35:02 +01002941 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002942 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002943 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002944
Eric Anholt02354392008-11-26 13:58:13 -08002945 /* Not valid to be called on unbound objects. */
2946 if (obj_priv->gtt_space == NULL)
2947 return -EINVAL;
2948
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002949 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002950 if (ret != 0)
2951 return ret;
2952
Chris Wilson72133422010-09-13 23:56:38 +01002953 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002954
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002955 if (write) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01002956 ret = i915_gem_object_wait_rendering(obj, true);
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002957 if (ret)
2958 return ret;
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002959 }
Eric Anholte47c68e2008-11-14 13:35:19 -08002960
2961 old_write_domain = obj->write_domain;
2962 old_read_domains = obj->read_domains;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002963
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002964 /* It should now be out of any other write domains, and we can update
2965 * the domain values for our changes.
2966 */
2967 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2968 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002969 if (write) {
Chris Wilson72133422010-09-13 23:56:38 +01002970 obj->read_domains = I915_GEM_DOMAIN_GTT;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002971 obj->write_domain = I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002972 obj_priv->dirty = 1;
2973 }
2974
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002975 trace_i915_gem_object_change_domain(obj,
2976 old_read_domains,
2977 old_write_domain);
2978
Eric Anholte47c68e2008-11-14 13:35:19 -08002979 return 0;
2980}
2981
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002982/*
2983 * Prepare buffer for display plane. Use uninterruptible for possible flush
2984 * wait, as in modesetting process we're not supposed to be interrupted.
2985 */
2986int
Chris Wilson48b956c2010-09-14 12:50:34 +01002987i915_gem_object_set_to_display_plane(struct drm_gem_object *obj,
2988 bool pipelined)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002989{
Daniel Vetter23010e42010-03-08 13:35:02 +01002990 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002991 uint32_t old_read_domains;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002992 int ret;
2993
2994 /* Not valid to be called on unbound objects. */
2995 if (obj_priv->gtt_space == NULL)
2996 return -EINVAL;
2997
Chris Wilsonced270f2010-09-26 22:47:46 +01002998 ret = i915_gem_object_flush_gpu_write_domain(obj, true);
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002999 if (ret)
3000 return ret;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003001
Chris Wilsonced270f2010-09-26 22:47:46 +01003002 /* Currently, we are always called from an non-interruptible context. */
3003 if (!pipelined) {
3004 ret = i915_gem_object_wait_rendering(obj, false);
3005 if (ret)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003006 return ret;
3007 }
3008
Chris Wilsonb118c1e2010-05-27 13:18:14 +01003009 i915_gem_object_flush_cpu_write_domain(obj);
3010
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003011 old_read_domains = obj->read_domains;
Chris Wilsonc78ec302010-09-20 12:50:23 +01003012 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003013
3014 trace_i915_gem_object_change_domain(obj,
3015 old_read_domains,
Daniel Vetterba3d8d72010-02-11 22:37:04 +01003016 obj->write_domain);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003017
3018 return 0;
3019}
3020
Chris Wilson85345512010-11-13 09:49:11 +00003021int
3022i915_gem_object_flush_gpu(struct drm_i915_gem_object *obj,
3023 bool interruptible)
3024{
3025 if (!obj->active)
3026 return 0;
3027
3028 if (obj->base.write_domain & I915_GEM_GPU_DOMAINS)
3029 i915_gem_flush_ring(obj->base.dev, NULL, obj->ring,
3030 0, obj->base.write_domain);
3031
3032 return i915_gem_object_wait_rendering(&obj->base, interruptible);
3033}
3034
Eric Anholte47c68e2008-11-14 13:35:19 -08003035/**
3036 * Moves a single object to the CPU read, and possibly write domain.
3037 *
3038 * This function returns when the move is complete, including waiting on
3039 * flushes to occur.
3040 */
3041static int
3042i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj, int write)
3043{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003044 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003045 int ret;
3046
Daniel Vetterba3d8d72010-02-11 22:37:04 +01003047 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Eric Anholte47c68e2008-11-14 13:35:19 -08003048 if (ret != 0)
3049 return ret;
3050
3051 i915_gem_object_flush_gtt_write_domain(obj);
3052
3053 /* If we have a partially-valid cache of the object in the CPU,
3054 * finish invalidating it and free the per-page flags.
3055 */
3056 i915_gem_object_set_to_full_cpu_read_domain(obj);
3057
Chris Wilson72133422010-09-13 23:56:38 +01003058 if (write) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01003059 ret = i915_gem_object_wait_rendering(obj, true);
Chris Wilson72133422010-09-13 23:56:38 +01003060 if (ret)
3061 return ret;
3062 }
3063
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003064 old_write_domain = obj->write_domain;
3065 old_read_domains = obj->read_domains;
3066
Eric Anholte47c68e2008-11-14 13:35:19 -08003067 /* Flush the CPU cache if it's still invalid. */
3068 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
3069 i915_gem_clflush_object(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003070
3071 obj->read_domains |= I915_GEM_DOMAIN_CPU;
3072 }
3073
3074 /* It should now be out of any other write domains, and we can update
3075 * the domain values for our changes.
3076 */
3077 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
3078
3079 /* If we're writing through the CPU, then the GPU read domains will
3080 * need to be invalidated at next use.
3081 */
3082 if (write) {
Chris Wilsonc78ec302010-09-20 12:50:23 +01003083 obj->read_domains = I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003084 obj->write_domain = I915_GEM_DOMAIN_CPU;
3085 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003086
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003087 trace_i915_gem_object_change_domain(obj,
3088 old_read_domains,
3089 old_write_domain);
3090
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003091 return 0;
3092}
3093
Eric Anholt673a3942008-07-30 12:06:12 -07003094/*
3095 * Set the next domain for the specified object. This
3096 * may not actually perform the necessary flushing/invaliding though,
3097 * as that may want to be batched with other set_domain operations
3098 *
3099 * This is (we hope) the only really tricky part of gem. The goal
3100 * is fairly simple -- track which caches hold bits of the object
3101 * and make sure they remain coherent. A few concrete examples may
3102 * help to explain how it works. For shorthand, we use the notation
3103 * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
3104 * a pair of read and write domain masks.
3105 *
3106 * Case 1: the batch buffer
3107 *
3108 * 1. Allocated
3109 * 2. Written by CPU
3110 * 3. Mapped to GTT
3111 * 4. Read by GPU
3112 * 5. Unmapped from GTT
3113 * 6. Freed
3114 *
3115 * Let's take these a step at a time
3116 *
3117 * 1. Allocated
3118 * Pages allocated from the kernel may still have
3119 * cache contents, so we set them to (CPU, CPU) always.
3120 * 2. Written by CPU (using pwrite)
3121 * The pwrite function calls set_domain (CPU, CPU) and
3122 * this function does nothing (as nothing changes)
3123 * 3. Mapped by GTT
3124 * This function asserts that the object is not
3125 * currently in any GPU-based read or write domains
3126 * 4. Read by GPU
3127 * i915_gem_execbuffer calls set_domain (COMMAND, 0).
3128 * As write_domain is zero, this function adds in the
3129 * current read domains (CPU+COMMAND, 0).
3130 * flush_domains is set to CPU.
3131 * invalidate_domains is set to COMMAND
3132 * clflush is run to get data out of the CPU caches
3133 * then i915_dev_set_domain calls i915_gem_flush to
3134 * emit an MI_FLUSH and drm_agp_chipset_flush
3135 * 5. Unmapped from GTT
3136 * i915_gem_object_unbind calls set_domain (CPU, CPU)
3137 * flush_domains and invalidate_domains end up both zero
3138 * so no flushing/invalidating happens
3139 * 6. Freed
3140 * yay, done
3141 *
3142 * Case 2: The shared render buffer
3143 *
3144 * 1. Allocated
3145 * 2. Mapped to GTT
3146 * 3. Read/written by GPU
3147 * 4. set_domain to (CPU,CPU)
3148 * 5. Read/written by CPU
3149 * 6. Read/written by GPU
3150 *
3151 * 1. Allocated
3152 * Same as last example, (CPU, CPU)
3153 * 2. Mapped to GTT
3154 * Nothing changes (assertions find that it is not in the GPU)
3155 * 3. Read/written by GPU
3156 * execbuffer calls set_domain (RENDER, RENDER)
3157 * flush_domains gets CPU
3158 * invalidate_domains gets GPU
3159 * clflush (obj)
3160 * MI_FLUSH and drm_agp_chipset_flush
3161 * 4. set_domain (CPU, CPU)
3162 * flush_domains gets GPU
3163 * invalidate_domains gets CPU
3164 * wait_rendering (obj) to make sure all drawing is complete.
3165 * This will include an MI_FLUSH to get the data from GPU
3166 * to memory
3167 * clflush (obj) to invalidate the CPU cache
3168 * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
3169 * 5. Read/written by CPU
3170 * cache lines are loaded and dirtied
3171 * 6. Read written by GPU
3172 * Same as last GPU access
3173 *
3174 * Case 3: The constant buffer
3175 *
3176 * 1. Allocated
3177 * 2. Written by CPU
3178 * 3. Read by GPU
3179 * 4. Updated (written) by CPU again
3180 * 5. Read by GPU
3181 *
3182 * 1. Allocated
3183 * (CPU, CPU)
3184 * 2. Written by CPU
3185 * (CPU, CPU)
3186 * 3. Read by GPU
3187 * (CPU+RENDER, 0)
3188 * flush_domains = CPU
3189 * invalidate_domains = RENDER
3190 * clflush (obj)
3191 * MI_FLUSH
3192 * drm_agp_chipset_flush
3193 * 4. Updated (written) by CPU again
3194 * (CPU, CPU)
3195 * flush_domains = 0 (no previous write domain)
3196 * invalidate_domains = 0 (no new read domains)
3197 * 5. Read by GPU
3198 * (CPU+RENDER, 0)
3199 * flush_domains = CPU
3200 * invalidate_domains = RENDER
3201 * clflush (obj)
3202 * MI_FLUSH
3203 * drm_agp_chipset_flush
3204 */
Keith Packardc0d90822008-11-20 23:11:08 -08003205static void
Chris Wilsonb6651452010-10-23 10:15:06 +01003206i915_gem_object_set_to_gpu_domain(struct drm_gem_object *obj,
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003207 struct intel_ring_buffer *ring,
3208 struct change_domains *cd)
Eric Anholt673a3942008-07-30 12:06:12 -07003209{
Daniel Vetter23010e42010-03-08 13:35:02 +01003210 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003211 uint32_t invalidate_domains = 0;
3212 uint32_t flush_domains = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07003213
Eric Anholt673a3942008-07-30 12:06:12 -07003214 /*
3215 * If the object isn't moving to a new write domain,
3216 * let the object stay in multiple read domains
3217 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003218 if (obj->pending_write_domain == 0)
3219 obj->pending_read_domains |= obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07003220
3221 /*
3222 * Flush the current write domain if
3223 * the new read domains don't match. Invalidate
3224 * any read domains which differ from the old
3225 * write domain
3226 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003227 if (obj->write_domain &&
Chris Wilson13b29282010-11-01 12:22:48 +00003228 (obj->write_domain != obj->pending_read_domains ||
3229 obj_priv->ring != ring)) {
Eric Anholt673a3942008-07-30 12:06:12 -07003230 flush_domains |= obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08003231 invalidate_domains |=
3232 obj->pending_read_domains & ~obj->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07003233 }
3234 /*
3235 * Invalidate any read caches which may have
3236 * stale data. That is, any new read domains.
3237 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003238 invalidate_domains |= obj->pending_read_domains & ~obj->read_domains;
Chris Wilson3d2a8122010-09-29 11:39:53 +01003239 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU)
Eric Anholt673a3942008-07-30 12:06:12 -07003240 i915_gem_clflush_object(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003241
Chris Wilson4a684a42010-10-28 14:44:08 +01003242 /* blow away mappings if mapped through GTT */
3243 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_GTT)
3244 i915_gem_release_mmap(obj);
3245
Eric Anholtefbeed92009-02-19 14:54:51 -08003246 /* The actual obj->write_domain will be updated with
3247 * pending_write_domain after we emit the accumulated flush for all
3248 * of our domain changes in execbuffers (which clears objects'
3249 * write_domains). So if we have a current write domain that we
3250 * aren't changing, set pending_write_domain to that.
3251 */
3252 if (flush_domains == 0 && obj->pending_write_domain == 0)
3253 obj->pending_write_domain = obj->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07003254
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003255 cd->invalidate_domains |= invalidate_domains;
3256 cd->flush_domains |= flush_domains;
Chris Wilsonb6651452010-10-23 10:15:06 +01003257 if (flush_domains & I915_GEM_GPU_DOMAINS)
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003258 cd->flush_rings |= obj_priv->ring->id;
Chris Wilsonb6651452010-10-23 10:15:06 +01003259 if (invalidate_domains & I915_GEM_GPU_DOMAINS)
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003260 cd->flush_rings |= ring->id;
Eric Anholt673a3942008-07-30 12:06:12 -07003261}
3262
3263/**
Eric Anholte47c68e2008-11-14 13:35:19 -08003264 * Moves the object from a partially CPU read to a full one.
Eric Anholt673a3942008-07-30 12:06:12 -07003265 *
Eric Anholte47c68e2008-11-14 13:35:19 -08003266 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
3267 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
3268 */
3269static void
3270i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj)
3271{
Daniel Vetter23010e42010-03-08 13:35:02 +01003272 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003273
3274 if (!obj_priv->page_cpu_valid)
3275 return;
3276
3277 /* If we're partially in the CPU read domain, finish moving it in.
3278 */
3279 if (obj->read_domains & I915_GEM_DOMAIN_CPU) {
3280 int i;
3281
3282 for (i = 0; i <= (obj->size - 1) / PAGE_SIZE; i++) {
3283 if (obj_priv->page_cpu_valid[i])
3284 continue;
Eric Anholt856fa192009-03-19 14:10:50 -07003285 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholte47c68e2008-11-14 13:35:19 -08003286 }
Eric Anholte47c68e2008-11-14 13:35:19 -08003287 }
3288
3289 /* Free the page_cpu_valid mappings which are now stale, whether
3290 * or not we've got I915_GEM_DOMAIN_CPU.
3291 */
Eric Anholt9a298b22009-03-24 12:23:04 -07003292 kfree(obj_priv->page_cpu_valid);
Eric Anholte47c68e2008-11-14 13:35:19 -08003293 obj_priv->page_cpu_valid = NULL;
3294}
3295
3296/**
3297 * Set the CPU read domain on a range of the object.
3298 *
3299 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
3300 * not entirely valid. The page_cpu_valid member of the object flags which
3301 * pages have been flushed, and will be respected by
3302 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
3303 * of the whole object.
3304 *
3305 * This function returns when the move is complete, including waiting on
3306 * flushes to occur.
Eric Anholt673a3942008-07-30 12:06:12 -07003307 */
3308static int
Eric Anholte47c68e2008-11-14 13:35:19 -08003309i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
3310 uint64_t offset, uint64_t size)
Eric Anholt673a3942008-07-30 12:06:12 -07003311{
Daniel Vetter23010e42010-03-08 13:35:02 +01003312 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003313 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003314 int i, ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003315
Eric Anholte47c68e2008-11-14 13:35:19 -08003316 if (offset == 0 && size == obj->size)
3317 return i915_gem_object_set_to_cpu_domain(obj, 0);
3318
Daniel Vetterba3d8d72010-02-11 22:37:04 +01003319 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Eric Anholte47c68e2008-11-14 13:35:19 -08003320 if (ret != 0)
3321 return ret;
3322 i915_gem_object_flush_gtt_write_domain(obj);
3323
3324 /* If we're already fully in the CPU read domain, we're done. */
3325 if (obj_priv->page_cpu_valid == NULL &&
3326 (obj->read_domains & I915_GEM_DOMAIN_CPU) != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07003327 return 0;
3328
Eric Anholte47c68e2008-11-14 13:35:19 -08003329 /* Otherwise, create/clear the per-page CPU read domain flag if we're
3330 * newly adding I915_GEM_DOMAIN_CPU
3331 */
Eric Anholt673a3942008-07-30 12:06:12 -07003332 if (obj_priv->page_cpu_valid == NULL) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003333 obj_priv->page_cpu_valid = kzalloc(obj->size / PAGE_SIZE,
3334 GFP_KERNEL);
Eric Anholte47c68e2008-11-14 13:35:19 -08003335 if (obj_priv->page_cpu_valid == NULL)
3336 return -ENOMEM;
3337 } else if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0)
3338 memset(obj_priv->page_cpu_valid, 0, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07003339
3340 /* Flush the cache on any pages that are still invalid from the CPU's
3341 * perspective.
3342 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003343 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
3344 i++) {
Eric Anholt673a3942008-07-30 12:06:12 -07003345 if (obj_priv->page_cpu_valid[i])
3346 continue;
3347
Eric Anholt856fa192009-03-19 14:10:50 -07003348 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07003349
3350 obj_priv->page_cpu_valid[i] = 1;
3351 }
3352
Eric Anholte47c68e2008-11-14 13:35:19 -08003353 /* It should now be out of any other write domains, and we can update
3354 * the domain values for our changes.
3355 */
3356 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
3357
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003358 old_read_domains = obj->read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003359 obj->read_domains |= I915_GEM_DOMAIN_CPU;
3360
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003361 trace_i915_gem_object_change_domain(obj,
3362 old_read_domains,
3363 obj->write_domain);
3364
Eric Anholt673a3942008-07-30 12:06:12 -07003365 return 0;
3366}
3367
Eric Anholt673a3942008-07-30 12:06:12 -07003368static int
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003369i915_gem_execbuffer_relocate_entry(struct drm_i915_gem_object *obj,
3370 struct drm_file *file_priv,
3371 struct drm_i915_gem_exec_object2 *entry,
3372 struct drm_i915_gem_relocation_entry *reloc)
Eric Anholt673a3942008-07-30 12:06:12 -07003373{
Chris Wilson9af90d12010-10-17 10:01:56 +01003374 struct drm_device *dev = obj->base.dev;
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003375 struct drm_gem_object *target_obj;
3376 uint32_t target_offset;
3377 int ret = -EINVAL;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003378
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003379 target_obj = drm_gem_object_lookup(dev, file_priv,
3380 reloc->target_handle);
3381 if (target_obj == NULL)
3382 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07003383
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003384 target_offset = to_intel_bo(target_obj)->gtt_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003385
Chris Wilson8542a0b2009-09-09 21:15:15 +01003386#if WATCH_RELOC
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003387 DRM_INFO("%s: obj %p offset %08x target %d "
3388 "read %08x write %08x gtt %08x "
3389 "presumed %08x delta %08x\n",
3390 __func__,
3391 obj,
3392 (int) reloc->offset,
3393 (int) reloc->target_handle,
3394 (int) reloc->read_domains,
3395 (int) reloc->write_domain,
3396 (int) target_offset,
3397 (int) reloc->presumed_offset,
3398 reloc->delta);
Chris Wilson8542a0b2009-09-09 21:15:15 +01003399#endif
3400
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003401 /* The target buffer should have appeared before us in the
3402 * exec_object list, so it should have a GTT space bound by now.
3403 */
3404 if (target_offset == 0) {
3405 DRM_ERROR("No GTT space found for object %d\n",
3406 reloc->target_handle);
3407 goto err;
Eric Anholt673a3942008-07-30 12:06:12 -07003408 }
3409
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003410 /* Validate that the target is in a valid r/w GPU domain */
3411 if (reloc->write_domain & (reloc->write_domain - 1)) {
3412 DRM_ERROR("reloc with multiple write domains: "
3413 "obj %p target %d offset %d "
3414 "read %08x write %08x",
3415 obj, reloc->target_handle,
3416 (int) reloc->offset,
3417 reloc->read_domains,
3418 reloc->write_domain);
3419 goto err;
3420 }
3421 if (reloc->write_domain & I915_GEM_DOMAIN_CPU ||
3422 reloc->read_domains & I915_GEM_DOMAIN_CPU) {
3423 DRM_ERROR("reloc with read/write CPU domains: "
3424 "obj %p target %d offset %d "
3425 "read %08x write %08x",
3426 obj, reloc->target_handle,
3427 (int) reloc->offset,
3428 reloc->read_domains,
3429 reloc->write_domain);
3430 goto err;
3431 }
3432 if (reloc->write_domain && target_obj->pending_write_domain &&
3433 reloc->write_domain != target_obj->pending_write_domain) {
3434 DRM_ERROR("Write domain conflict: "
3435 "obj %p target %d offset %d "
3436 "new %08x old %08x\n",
3437 obj, reloc->target_handle,
3438 (int) reloc->offset,
3439 reloc->write_domain,
3440 target_obj->pending_write_domain);
3441 goto err;
3442 }
3443
3444 target_obj->pending_read_domains |= reloc->read_domains;
3445 target_obj->pending_write_domain |= reloc->write_domain;
3446
3447 /* If the relocation already has the right value in it, no
3448 * more work needs to be done.
3449 */
3450 if (target_offset == reloc->presumed_offset)
3451 goto out;
3452
3453 /* Check that the relocation address is valid... */
3454 if (reloc->offset > obj->base.size - 4) {
3455 DRM_ERROR("Relocation beyond object bounds: "
3456 "obj %p target %d offset %d size %d.\n",
3457 obj, reloc->target_handle,
3458 (int) reloc->offset,
3459 (int) obj->base.size);
3460 goto err;
3461 }
3462 if (reloc->offset & 3) {
3463 DRM_ERROR("Relocation not 4-byte aligned: "
3464 "obj %p target %d offset %d.\n",
3465 obj, reloc->target_handle,
3466 (int) reloc->offset);
3467 goto err;
3468 }
3469
3470 /* and points to somewhere within the target object. */
3471 if (reloc->delta >= target_obj->size) {
3472 DRM_ERROR("Relocation beyond target object bounds: "
3473 "obj %p target %d delta %d size %d.\n",
3474 obj, reloc->target_handle,
3475 (int) reloc->delta,
3476 (int) target_obj->size);
3477 goto err;
3478 }
3479
3480 reloc->delta += target_offset;
3481 if (obj->base.write_domain == I915_GEM_DOMAIN_CPU) {
3482 uint32_t page_offset = reloc->offset & ~PAGE_MASK;
3483 char *vaddr;
3484
3485 vaddr = kmap_atomic(obj->pages[reloc->offset >> PAGE_SHIFT]);
3486 *(uint32_t *)(vaddr + page_offset) = reloc->delta;
3487 kunmap_atomic(vaddr);
3488 } else {
3489 struct drm_i915_private *dev_priv = dev->dev_private;
3490 uint32_t __iomem *reloc_entry;
3491 void __iomem *reloc_page;
3492
3493 ret = i915_gem_object_set_to_gtt_domain(&obj->base, 1);
3494 if (ret)
3495 goto err;
3496
3497 /* Map the page containing the relocation we're going to perform. */
3498 reloc->offset += obj->gtt_offset;
3499 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
3500 reloc->offset & PAGE_MASK);
3501 reloc_entry = (uint32_t __iomem *)
3502 (reloc_page + (reloc->offset & ~PAGE_MASK));
3503 iowrite32(reloc->delta, reloc_entry);
3504 io_mapping_unmap_atomic(reloc_page);
3505 }
3506
3507 /* and update the user's relocation entry */
3508 reloc->presumed_offset = target_offset;
3509
3510out:
3511 ret = 0;
3512err:
Chris Wilson9af90d12010-10-17 10:01:56 +01003513 drm_gem_object_unreference(target_obj);
3514 return ret;
3515}
3516
3517static int
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003518i915_gem_execbuffer_relocate_object(struct drm_i915_gem_object *obj,
3519 struct drm_file *file_priv,
3520 struct drm_i915_gem_exec_object2 *entry)
3521{
3522 struct drm_i915_gem_relocation_entry __user *user_relocs;
3523 int i, ret;
3524
3525 user_relocs = (void __user *)(uintptr_t)entry->relocs_ptr;
3526 for (i = 0; i < entry->relocation_count; i++) {
3527 struct drm_i915_gem_relocation_entry reloc;
3528
3529 if (__copy_from_user_inatomic(&reloc,
3530 user_relocs+i,
3531 sizeof(reloc)))
3532 return -EFAULT;
3533
3534 ret = i915_gem_execbuffer_relocate_entry(obj, file_priv, entry, &reloc);
3535 if (ret)
3536 return ret;
3537
3538 if (__copy_to_user_inatomic(&user_relocs[i].presumed_offset,
3539 &reloc.presumed_offset,
3540 sizeof(reloc.presumed_offset)))
3541 return -EFAULT;
3542 }
3543
3544 return 0;
3545}
3546
3547static int
3548i915_gem_execbuffer_relocate_object_slow(struct drm_i915_gem_object *obj,
3549 struct drm_file *file_priv,
3550 struct drm_i915_gem_exec_object2 *entry,
3551 struct drm_i915_gem_relocation_entry *relocs)
3552{
3553 int i, ret;
3554
3555 for (i = 0; i < entry->relocation_count; i++) {
3556 ret = i915_gem_execbuffer_relocate_entry(obj, file_priv, entry, &relocs[i]);
3557 if (ret)
3558 return ret;
3559 }
3560
3561 return 0;
3562}
3563
3564static int
3565i915_gem_execbuffer_relocate(struct drm_device *dev,
3566 struct drm_file *file,
3567 struct drm_gem_object **object_list,
3568 struct drm_i915_gem_exec_object2 *exec_list,
3569 int count)
3570{
3571 int i, ret;
3572
3573 for (i = 0; i < count; i++) {
3574 struct drm_i915_gem_object *obj = to_intel_bo(object_list[i]);
3575 obj->base.pending_read_domains = 0;
3576 obj->base.pending_write_domain = 0;
3577 ret = i915_gem_execbuffer_relocate_object(obj, file,
3578 &exec_list[i]);
3579 if (ret)
3580 return ret;
3581 }
3582
3583 return 0;
3584}
3585
3586static int
3587i915_gem_execbuffer_reserve(struct drm_device *dev,
3588 struct drm_file *file,
3589 struct drm_gem_object **object_list,
3590 struct drm_i915_gem_exec_object2 *exec_list,
3591 int count)
Chris Wilson9af90d12010-10-17 10:01:56 +01003592{
3593 struct drm_i915_private *dev_priv = dev->dev_private;
3594 int ret, i, retry;
3595
3596 /* attempt to pin all of the buffers into the GTT */
Chris Wilson5eac3ab2010-10-31 08:49:47 +00003597 retry = 0;
3598 do {
Chris Wilson9af90d12010-10-17 10:01:56 +01003599 ret = 0;
3600 for (i = 0; i < count; i++) {
3601 struct drm_i915_gem_exec_object2 *entry = &exec_list[i];
Daniel Vetter16e809a2010-09-16 19:37:04 +02003602 struct drm_i915_gem_object *obj = to_intel_bo(object_list[i]);
Chris Wilson9af90d12010-10-17 10:01:56 +01003603 bool need_fence =
3604 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
3605 obj->tiling_mode != I915_TILING_NONE;
3606
Daniel Vetter16e809a2010-09-16 19:37:04 +02003607 /* g33/pnv can't fence buffers in the unmappable part */
3608 bool need_mappable =
3609 entry->relocation_count ? true : need_fence;
3610
Chris Wilson9af90d12010-10-17 10:01:56 +01003611 /* Check fence reg constraints and rebind if necessary */
Daniel Vetter75e9e912010-11-04 17:11:09 +01003612 if (need_mappable && !obj->map_and_fenceable) {
Chris Wilson9af90d12010-10-17 10:01:56 +01003613 ret = i915_gem_object_unbind(&obj->base);
3614 if (ret)
3615 break;
3616 }
3617
Daniel Vetter920afa72010-09-16 17:54:23 +02003618 ret = i915_gem_object_pin(&obj->base,
Daniel Vetter16e809a2010-09-16 19:37:04 +02003619 entry->alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01003620 need_mappable);
Chris Wilson9af90d12010-10-17 10:01:56 +01003621 if (ret)
3622 break;
3623
3624 /*
3625 * Pre-965 chips need a fence register set up in order
3626 * to properly handle blits to/from tiled surfaces.
3627 */
3628 if (need_fence) {
3629 ret = i915_gem_object_get_fence_reg(&obj->base, true);
3630 if (ret) {
3631 i915_gem_object_unpin(&obj->base);
3632 break;
3633 }
3634
3635 dev_priv->fence_regs[obj->fence_reg].gpu = true;
3636 }
3637
3638 entry->offset = obj->gtt_offset;
3639 }
3640
3641 while (i--)
3642 i915_gem_object_unpin(object_list[i]);
3643
Chris Wilson5eac3ab2010-10-31 08:49:47 +00003644 if (ret != -ENOSPC || retry > 1)
Chris Wilson9af90d12010-10-17 10:01:56 +01003645 return ret;
3646
Chris Wilson5eac3ab2010-10-31 08:49:47 +00003647 /* First attempt, just clear anything that is purgeable.
3648 * Second attempt, clear the entire GTT.
3649 */
3650 ret = i915_gem_evict_everything(dev, retry == 0);
Chris Wilson9af90d12010-10-17 10:01:56 +01003651 if (ret)
3652 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003653
Chris Wilson5eac3ab2010-10-31 08:49:47 +00003654 retry++;
3655 } while (1);
Eric Anholt673a3942008-07-30 12:06:12 -07003656}
3657
Chris Wilson13b29282010-11-01 12:22:48 +00003658static int
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003659i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
3660 struct drm_file *file,
3661 struct drm_gem_object **object_list,
3662 struct drm_i915_gem_exec_object2 *exec_list,
3663 int count)
3664{
3665 struct drm_i915_gem_relocation_entry *reloc;
3666 int i, total, ret;
3667
3668 for (i = 0; i < count; i++) {
3669 struct drm_i915_gem_object *obj = to_intel_bo(object_list[i]);
3670 obj->in_execbuffer = false;
3671 }
3672
3673 mutex_unlock(&dev->struct_mutex);
3674
3675 total = 0;
3676 for (i = 0; i < count; i++)
3677 total += exec_list[i].relocation_count;
3678
3679 reloc = drm_malloc_ab(total, sizeof(*reloc));
3680 if (reloc == NULL) {
3681 mutex_lock(&dev->struct_mutex);
3682 return -ENOMEM;
3683 }
3684
3685 total = 0;
3686 for (i = 0; i < count; i++) {
3687 struct drm_i915_gem_relocation_entry __user *user_relocs;
3688
3689 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3690
3691 if (copy_from_user(reloc+total, user_relocs,
3692 exec_list[i].relocation_count *
3693 sizeof(*reloc))) {
3694 ret = -EFAULT;
3695 mutex_lock(&dev->struct_mutex);
3696 goto err;
3697 }
3698
3699 total += exec_list[i].relocation_count;
3700 }
3701
3702 ret = i915_mutex_lock_interruptible(dev);
3703 if (ret) {
3704 mutex_lock(&dev->struct_mutex);
3705 goto err;
3706 }
3707
3708 ret = i915_gem_execbuffer_reserve(dev, file,
3709 object_list, exec_list,
3710 count);
3711 if (ret)
3712 goto err;
3713
3714 total = 0;
3715 for (i = 0; i < count; i++) {
3716 struct drm_i915_gem_object *obj = to_intel_bo(object_list[i]);
3717 obj->base.pending_read_domains = 0;
3718 obj->base.pending_write_domain = 0;
3719 ret = i915_gem_execbuffer_relocate_object_slow(obj, file,
3720 &exec_list[i],
3721 reloc + total);
3722 if (ret)
3723 goto err;
3724
3725 total += exec_list[i].relocation_count;
3726 }
3727
3728 /* Leave the user relocations as are, this is the painfully slow path,
3729 * and we want to avoid the complication of dropping the lock whilst
3730 * having buffers reserved in the aperture and so causing spurious
3731 * ENOSPC for random operations.
3732 */
3733
3734err:
3735 drm_free_large(reloc);
3736 return ret;
3737}
3738
3739static int
Chris Wilson13b29282010-11-01 12:22:48 +00003740i915_gem_execbuffer_move_to_gpu(struct drm_device *dev,
3741 struct drm_file *file,
3742 struct intel_ring_buffer *ring,
3743 struct drm_gem_object **objects,
3744 int count)
3745{
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003746 struct change_domains cd;
Chris Wilson13b29282010-11-01 12:22:48 +00003747 int ret, i;
3748
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003749 cd.invalidate_domains = 0;
3750 cd.flush_domains = 0;
3751 cd.flush_rings = 0;
Chris Wilson13b29282010-11-01 12:22:48 +00003752 for (i = 0; i < count; i++)
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003753 i915_gem_object_set_to_gpu_domain(objects[i], ring, &cd);
Chris Wilson13b29282010-11-01 12:22:48 +00003754
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003755 if (cd.invalidate_domains | cd.flush_domains) {
Chris Wilson13b29282010-11-01 12:22:48 +00003756#if WATCH_EXEC
3757 DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
3758 __func__,
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003759 cd.invalidate_domains,
3760 cd.flush_domains);
Chris Wilson13b29282010-11-01 12:22:48 +00003761#endif
3762 i915_gem_flush(dev, file,
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003763 cd.invalidate_domains,
3764 cd.flush_domains,
3765 cd.flush_rings);
Chris Wilson13b29282010-11-01 12:22:48 +00003766 }
3767
3768 for (i = 0; i < count; i++) {
3769 struct drm_i915_gem_object *obj = to_intel_bo(objects[i]);
3770 /* XXX replace with semaphores */
3771 if (obj->ring && ring != obj->ring) {
3772 ret = i915_gem_object_wait_rendering(&obj->base, true);
3773 if (ret)
3774 return ret;
3775 }
3776 }
3777
3778 return 0;
3779}
3780
Eric Anholt673a3942008-07-30 12:06:12 -07003781/* Throttle our rendering by waiting until the ring has completed our requests
3782 * emitted over 20 msec ago.
3783 *
Eric Anholtb9624422009-06-03 07:27:35 +00003784 * Note that if we were to use the current jiffies each time around the loop,
3785 * we wouldn't escape the function with any frames outstanding if the time to
3786 * render a frame was over 20ms.
3787 *
Eric Anholt673a3942008-07-30 12:06:12 -07003788 * This should get us reasonable parallelism between CPU and GPU but also
3789 * relatively low latency when blocking on a particular request to finish.
3790 */
3791static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003792i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003793{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003794 struct drm_i915_private *dev_priv = dev->dev_private;
3795 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00003796 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003797 struct drm_i915_gem_request *request;
3798 struct intel_ring_buffer *ring = NULL;
3799 u32 seqno = 0;
3800 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003801
Chris Wilson1c255952010-09-26 11:03:27 +01003802 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003803 list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
Eric Anholtb9624422009-06-03 07:27:35 +00003804 if (time_after_eq(request->emitted_jiffies, recent_enough))
3805 break;
3806
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003807 ring = request->ring;
3808 seqno = request->seqno;
Eric Anholtb9624422009-06-03 07:27:35 +00003809 }
Chris Wilson1c255952010-09-26 11:03:27 +01003810 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003811
3812 if (seqno == 0)
3813 return 0;
3814
3815 ret = 0;
Chris Wilson78501ea2010-10-27 12:18:21 +01003816 if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) {
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003817 /* And wait for the seqno passing without holding any locks and
3818 * causing extra latency for others. This is safe as the irq
3819 * generation is designed to be run atomically and so is
3820 * lockless.
3821 */
Chris Wilson78501ea2010-10-27 12:18:21 +01003822 ring->user_irq_get(ring);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003823 ret = wait_event_interruptible(ring->irq_queue,
Chris Wilson78501ea2010-10-27 12:18:21 +01003824 i915_seqno_passed(ring->get_seqno(ring), seqno)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003825 || atomic_read(&dev_priv->mm.wedged));
Chris Wilson78501ea2010-10-27 12:18:21 +01003826 ring->user_irq_put(ring);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003827
3828 if (ret == 0 && atomic_read(&dev_priv->mm.wedged))
3829 ret = -EIO;
3830 }
3831
3832 if (ret == 0)
3833 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
Eric Anholtb9624422009-06-03 07:27:35 +00003834
Eric Anholt673a3942008-07-30 12:06:12 -07003835 return ret;
3836}
3837
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003838static int
Chris Wilson2549d6c2010-10-14 12:10:41 +01003839i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec,
3840 uint64_t exec_offset)
Chris Wilson83d60792009-06-06 09:45:57 +01003841{
3842 uint32_t exec_start, exec_len;
3843
3844 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
3845 exec_len = (uint32_t) exec->batch_len;
3846
3847 if ((exec_start | exec_len) & 0x7)
3848 return -EINVAL;
3849
3850 if (!exec_start)
3851 return -EINVAL;
3852
3853 return 0;
3854}
3855
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003856static int
Chris Wilson2549d6c2010-10-14 12:10:41 +01003857validate_exec_list(struct drm_i915_gem_exec_object2 *exec,
3858 int count)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003859{
Chris Wilson2549d6c2010-10-14 12:10:41 +01003860 int i;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003861
Chris Wilson2549d6c2010-10-14 12:10:41 +01003862 for (i = 0; i < count; i++) {
3863 char __user *ptr = (char __user *)(uintptr_t)exec[i].relocs_ptr;
Chris Wilsond1d78832010-11-21 09:23:48 +00003864 int length; /* limited by fault_in_pages_readable() */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003865
Chris Wilsond1d78832010-11-21 09:23:48 +00003866 /* First check for malicious input causing overflow */
3867 if (exec[i].relocation_count >
3868 INT_MAX / sizeof(struct drm_i915_gem_relocation_entry))
3869 return -EINVAL;
3870
3871 length = exec[i].relocation_count *
3872 sizeof(struct drm_i915_gem_relocation_entry);
Chris Wilson2549d6c2010-10-14 12:10:41 +01003873 if (!access_ok(VERIFY_READ, ptr, length))
3874 return -EFAULT;
3875
Chris Wilsonb5dc6082010-10-20 20:59:57 +01003876 /* we may also need to update the presumed offsets */
3877 if (!access_ok(VERIFY_WRITE, ptr, length))
3878 return -EFAULT;
3879
Chris Wilson2549d6c2010-10-14 12:10:41 +01003880 if (fault_in_pages_readable(ptr, length))
3881 return -EFAULT;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003882 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003883
Chris Wilson2549d6c2010-10-14 12:10:41 +01003884 return 0;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003885}
3886
Chris Wilson2549d6c2010-10-14 12:10:41 +01003887static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003888i915_gem_do_execbuffer(struct drm_device *dev, void *data,
Chris Wilson9af90d12010-10-17 10:01:56 +01003889 struct drm_file *file,
Jesse Barnes76446ca2009-12-17 22:05:42 -05003890 struct drm_i915_gem_execbuffer2 *args,
3891 struct drm_i915_gem_exec_object2 *exec_list)
Eric Anholt673a3942008-07-30 12:06:12 -07003892{
3893 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003894 struct drm_gem_object **object_list = NULL;
3895 struct drm_gem_object *batch_obj;
Eric Anholt201361a2009-03-11 12:30:04 -07003896 struct drm_clip_rect *cliprects = NULL;
Chris Wilson8dc5d142010-08-12 12:36:12 +01003897 struct drm_i915_gem_request *request = NULL;
Chris Wilson9af90d12010-10-17 10:01:56 +01003898 int ret, i, flips;
Eric Anholt673a3942008-07-30 12:06:12 -07003899 uint64_t exec_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003900
Zou Nan hai852835f2010-05-21 09:08:56 +08003901 struct intel_ring_buffer *ring = NULL;
3902
Chris Wilson30dbf0c2010-09-25 10:19:17 +01003903 ret = i915_gem_check_is_wedged(dev);
3904 if (ret)
3905 return ret;
3906
Chris Wilson2549d6c2010-10-14 12:10:41 +01003907 ret = validate_exec_list(exec_list, args->buffer_count);
3908 if (ret)
3909 return ret;
3910
Eric Anholt673a3942008-07-30 12:06:12 -07003911#if WATCH_EXEC
3912 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3913 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3914#endif
Chris Wilson549f7362010-10-19 11:19:32 +01003915 switch (args->flags & I915_EXEC_RING_MASK) {
3916 case I915_EXEC_DEFAULT:
3917 case I915_EXEC_RENDER:
3918 ring = &dev_priv->render_ring;
3919 break;
3920 case I915_EXEC_BSD:
Zou Nan haid1b851f2010-05-21 09:08:57 +08003921 if (!HAS_BSD(dev)) {
Chris Wilson549f7362010-10-19 11:19:32 +01003922 DRM_ERROR("execbuf with invalid ring (BSD)\n");
Zou Nan haid1b851f2010-05-21 09:08:57 +08003923 return -EINVAL;
3924 }
3925 ring = &dev_priv->bsd_ring;
Chris Wilson549f7362010-10-19 11:19:32 +01003926 break;
3927 case I915_EXEC_BLT:
3928 if (!HAS_BLT(dev)) {
3929 DRM_ERROR("execbuf with invalid ring (BLT)\n");
3930 return -EINVAL;
3931 }
3932 ring = &dev_priv->blt_ring;
3933 break;
3934 default:
3935 DRM_ERROR("execbuf with unknown ring: %d\n",
3936 (int)(args->flags & I915_EXEC_RING_MASK));
3937 return -EINVAL;
Zou Nan haid1b851f2010-05-21 09:08:57 +08003938 }
3939
Eric Anholt4f481ed2008-09-10 14:22:49 -07003940 if (args->buffer_count < 1) {
3941 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3942 return -EINVAL;
3943 }
Eric Anholtc8e0f932009-11-22 03:49:37 +01003944 object_list = drm_malloc_ab(sizeof(*object_list), args->buffer_count);
Jesse Barnes76446ca2009-12-17 22:05:42 -05003945 if (object_list == NULL) {
3946 DRM_ERROR("Failed to allocate object list for %d buffers\n",
Eric Anholt673a3942008-07-30 12:06:12 -07003947 args->buffer_count);
3948 ret = -ENOMEM;
3949 goto pre_mutex_err;
3950 }
Eric Anholt673a3942008-07-30 12:06:12 -07003951
Eric Anholt201361a2009-03-11 12:30:04 -07003952 if (args->num_cliprects != 0) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003953 cliprects = kcalloc(args->num_cliprects, sizeof(*cliprects),
3954 GFP_KERNEL);
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003955 if (cliprects == NULL) {
3956 ret = -ENOMEM;
Eric Anholt201361a2009-03-11 12:30:04 -07003957 goto pre_mutex_err;
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003958 }
Eric Anholt201361a2009-03-11 12:30:04 -07003959
3960 ret = copy_from_user(cliprects,
3961 (struct drm_clip_rect __user *)
3962 (uintptr_t) args->cliprects_ptr,
3963 sizeof(*cliprects) * args->num_cliprects);
3964 if (ret != 0) {
3965 DRM_ERROR("copy %d cliprects failed: %d\n",
3966 args->num_cliprects, ret);
Dan Carpenterc877cdc2010-06-23 19:03:01 +02003967 ret = -EFAULT;
Eric Anholt201361a2009-03-11 12:30:04 -07003968 goto pre_mutex_err;
3969 }
3970 }
3971
Chris Wilson8dc5d142010-08-12 12:36:12 +01003972 request = kzalloc(sizeof(*request), GFP_KERNEL);
3973 if (request == NULL) {
3974 ret = -ENOMEM;
Chris Wilsona198bc82009-02-06 16:55:20 +00003975 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003976 }
3977
Chris Wilson76c1dec2010-09-25 11:22:51 +01003978 ret = i915_mutex_lock_interruptible(dev);
3979 if (ret)
3980 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003981
Eric Anholt673a3942008-07-30 12:06:12 -07003982 if (dev_priv->mm.suspended) {
Eric Anholt673a3942008-07-30 12:06:12 -07003983 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003984 ret = -EBUSY;
3985 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003986 }
3987
Keith Packardac94a962008-11-20 23:30:27 -08003988 /* Look up object handles */
Eric Anholt673a3942008-07-30 12:06:12 -07003989 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson7e318e12010-10-27 13:43:39 +01003990 struct drm_i915_gem_object *obj_priv;
3991
Chris Wilson9af90d12010-10-17 10:01:56 +01003992 object_list[i] = drm_gem_object_lookup(dev, file,
Eric Anholt673a3942008-07-30 12:06:12 -07003993 exec_list[i].handle);
3994 if (object_list[i] == NULL) {
3995 DRM_ERROR("Invalid object handle %d at index %d\n",
3996 exec_list[i].handle, i);
Chris Wilson0ce907f2010-01-23 20:26:35 +00003997 /* prevent error path from reading uninitialized data */
3998 args->buffer_count = i + 1;
Chris Wilsonbf79cb92010-08-04 14:19:46 +01003999 ret = -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07004000 goto err;
4001 }
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05004002
Daniel Vetter23010e42010-03-08 13:35:02 +01004003 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05004004 if (obj_priv->in_execbuffer) {
4005 DRM_ERROR("Object %p appears more than once in object list\n",
4006 object_list[i]);
Chris Wilson0ce907f2010-01-23 20:26:35 +00004007 /* prevent error path from reading uninitialized data */
4008 args->buffer_count = i + 1;
Chris Wilsonbf79cb92010-08-04 14:19:46 +01004009 ret = -EINVAL;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05004010 goto err;
4011 }
4012 obj_priv->in_execbuffer = true;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05004013 }
4014
Chris Wilson9af90d12010-10-17 10:01:56 +01004015 /* Move the objects en-masse into the GTT, evicting if necessary. */
Chris Wilsonbcf50e22010-11-21 22:07:12 +00004016 ret = i915_gem_execbuffer_reserve(dev, file,
4017 object_list, exec_list,
4018 args->buffer_count);
Chris Wilson9af90d12010-10-17 10:01:56 +01004019 if (ret)
4020 goto err;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07004021
Chris Wilson9af90d12010-10-17 10:01:56 +01004022 /* The objects are in their final locations, apply the relocations. */
Chris Wilsonbcf50e22010-11-21 22:07:12 +00004023 ret = i915_gem_execbuffer_relocate(dev, file,
4024 object_list, exec_list,
4025 args->buffer_count);
4026 if (ret) {
4027 if (ret == -EFAULT) {
4028 ret = i915_gem_execbuffer_relocate_slow(dev, file,
4029 object_list,
4030 exec_list,
4031 args->buffer_count);
4032 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
4033 }
Eric Anholt673a3942008-07-30 12:06:12 -07004034 if (ret)
4035 goto err;
4036 }
4037
Eric Anholt673a3942008-07-30 12:06:12 -07004038 /* Set the pending read domains for the batch buffer to COMMAND */
4039 batch_obj = object_list[args->buffer_count-1];
Chris Wilson5f26a2c2009-06-06 09:45:58 +01004040 if (batch_obj->pending_write_domain) {
4041 DRM_ERROR("Attempting to use self-modifying batch buffer\n");
4042 ret = -EINVAL;
4043 goto err;
4044 }
4045 batch_obj->pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
Eric Anholt673a3942008-07-30 12:06:12 -07004046
Chris Wilson9af90d12010-10-17 10:01:56 +01004047 /* Sanity check the batch buffer */
4048 exec_offset = to_intel_bo(batch_obj)->gtt_offset;
4049 ret = i915_gem_check_execbuffer(args, exec_offset);
Chris Wilson83d60792009-06-06 09:45:57 +01004050 if (ret != 0) {
4051 DRM_ERROR("execbuf with invalid offset/length\n");
4052 goto err;
4053 }
4054
Chris Wilson13b29282010-11-01 12:22:48 +00004055 ret = i915_gem_execbuffer_move_to_gpu(dev, file, ring,
4056 object_list, args->buffer_count);
4057 if (ret)
4058 goto err;
Eric Anholt673a3942008-07-30 12:06:12 -07004059
Eric Anholt673a3942008-07-30 12:06:12 -07004060#if WATCH_COHERENCY
4061 for (i = 0; i < args->buffer_count; i++) {
4062 i915_gem_object_check_coherency(object_list[i],
4063 exec_list[i].handle);
4064 }
4065#endif
4066
Eric Anholt673a3942008-07-30 12:06:12 -07004067#if WATCH_EXEC
Ben Gamari6911a9b2009-04-02 11:24:54 -07004068 i915_gem_dump_object(batch_obj,
Eric Anholt673a3942008-07-30 12:06:12 -07004069 args->batch_len,
4070 __func__,
4071 ~0);
4072#endif
4073
Chris Wilsone59f2ba2010-10-07 17:28:15 +01004074 /* Check for any pending flips. As we only maintain a flip queue depth
4075 * of 1, we can simply insert a WAIT for the next display flip prior
4076 * to executing the batch and avoid stalling the CPU.
4077 */
4078 flips = 0;
4079 for (i = 0; i < args->buffer_count; i++) {
4080 if (object_list[i]->write_domain)
4081 flips |= atomic_read(&to_intel_bo(object_list[i])->pending_flip);
4082 }
4083 if (flips) {
4084 int plane, flip_mask;
4085
4086 for (plane = 0; flips >> plane; plane++) {
4087 if (((flips >> plane) & 1) == 0)
4088 continue;
4089
4090 if (plane)
4091 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
4092 else
4093 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
4094
Chris Wilsone1f99ce2010-10-27 12:45:26 +01004095 ret = intel_ring_begin(ring, 2);
4096 if (ret)
4097 goto err;
4098
Chris Wilson78501ea2010-10-27 12:18:21 +01004099 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
4100 intel_ring_emit(ring, MI_NOOP);
4101 intel_ring_advance(ring);
Chris Wilsone59f2ba2010-10-07 17:28:15 +01004102 }
4103 }
4104
Eric Anholt673a3942008-07-30 12:06:12 -07004105 /* Exec the batchbuffer */
Chris Wilson78501ea2010-10-27 12:18:21 +01004106 ret = ring->dispatch_execbuffer(ring, args, cliprects, exec_offset);
Eric Anholt673a3942008-07-30 12:06:12 -07004107 if (ret) {
4108 DRM_ERROR("dispatch failed %d\n", ret);
4109 goto err;
4110 }
4111
Chris Wilson7e318e12010-10-27 13:43:39 +01004112 for (i = 0; i < args->buffer_count; i++) {
4113 struct drm_gem_object *obj = object_list[i];
4114
4115 obj->read_domains = obj->pending_read_domains;
4116 obj->write_domain = obj->pending_write_domain;
4117
4118 i915_gem_object_move_to_active(obj, ring);
4119 if (obj->write_domain) {
4120 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
4121 obj_priv->dirty = 1;
4122 list_move_tail(&obj_priv->gpu_write_list,
4123 &ring->gpu_write_list);
4124 intel_mark_busy(dev, obj);
4125 }
4126
4127 trace_i915_gem_object_change_domain(obj,
4128 obj->read_domains,
4129 obj->write_domain);
4130 }
4131
Eric Anholt673a3942008-07-30 12:06:12 -07004132 /*
4133 * Ensure that the commands in the batch buffer are
4134 * finished before the interrupt fires
4135 */
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01004136 i915_retire_commands(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07004137
Chris Wilson3cce4692010-10-27 16:11:02 +01004138 if (i915_add_request(dev, file, request, ring))
Chris Wilson5d97eb62010-11-10 20:40:02 +00004139 i915_gem_next_request_seqno(dev, ring);
Chris Wilson3cce4692010-10-27 16:11:02 +01004140 else
4141 request = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07004142
Eric Anholt673a3942008-07-30 12:06:12 -07004143err:
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05004144 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson7e318e12010-10-27 13:43:39 +01004145 if (object_list[i] == NULL)
4146 break;
4147
4148 to_intel_bo(object_list[i])->in_execbuffer = false;
Julia Lawallaad87df2008-12-21 16:28:47 +01004149 drm_gem_object_unreference(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05004150 }
Julia Lawallaad87df2008-12-21 16:28:47 +01004151
Eric Anholt673a3942008-07-30 12:06:12 -07004152 mutex_unlock(&dev->struct_mutex);
4153
Chris Wilson93533c22010-01-31 10:40:48 +00004154pre_mutex_err:
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07004155 drm_free_large(object_list);
Eric Anholt9a298b22009-03-24 12:23:04 -07004156 kfree(cliprects);
Chris Wilson8dc5d142010-08-12 12:36:12 +01004157 kfree(request);
Eric Anholt673a3942008-07-30 12:06:12 -07004158
4159 return ret;
4160}
4161
Jesse Barnes76446ca2009-12-17 22:05:42 -05004162/*
4163 * Legacy execbuffer just creates an exec2 list from the original exec object
4164 * list array and passes it to the real function.
4165 */
4166int
4167i915_gem_execbuffer(struct drm_device *dev, void *data,
4168 struct drm_file *file_priv)
4169{
4170 struct drm_i915_gem_execbuffer *args = data;
4171 struct drm_i915_gem_execbuffer2 exec2;
4172 struct drm_i915_gem_exec_object *exec_list = NULL;
4173 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
4174 int ret, i;
4175
4176#if WATCH_EXEC
4177 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
4178 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
4179#endif
4180
4181 if (args->buffer_count < 1) {
4182 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
4183 return -EINVAL;
4184 }
4185
4186 /* Copy in the exec list from userland */
4187 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
4188 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
4189 if (exec_list == NULL || exec2_list == NULL) {
4190 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
4191 args->buffer_count);
4192 drm_free_large(exec_list);
4193 drm_free_large(exec2_list);
4194 return -ENOMEM;
4195 }
4196 ret = copy_from_user(exec_list,
4197 (struct drm_i915_relocation_entry __user *)
4198 (uintptr_t) args->buffers_ptr,
4199 sizeof(*exec_list) * args->buffer_count);
4200 if (ret != 0) {
4201 DRM_ERROR("copy %d exec entries failed %d\n",
4202 args->buffer_count, ret);
4203 drm_free_large(exec_list);
4204 drm_free_large(exec2_list);
4205 return -EFAULT;
4206 }
4207
4208 for (i = 0; i < args->buffer_count; i++) {
4209 exec2_list[i].handle = exec_list[i].handle;
4210 exec2_list[i].relocation_count = exec_list[i].relocation_count;
4211 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
4212 exec2_list[i].alignment = exec_list[i].alignment;
4213 exec2_list[i].offset = exec_list[i].offset;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004214 if (INTEL_INFO(dev)->gen < 4)
Jesse Barnes76446ca2009-12-17 22:05:42 -05004215 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
4216 else
4217 exec2_list[i].flags = 0;
4218 }
4219
4220 exec2.buffers_ptr = args->buffers_ptr;
4221 exec2.buffer_count = args->buffer_count;
4222 exec2.batch_start_offset = args->batch_start_offset;
4223 exec2.batch_len = args->batch_len;
4224 exec2.DR1 = args->DR1;
4225 exec2.DR4 = args->DR4;
4226 exec2.num_cliprects = args->num_cliprects;
4227 exec2.cliprects_ptr = args->cliprects_ptr;
Zou Nan hai852835f2010-05-21 09:08:56 +08004228 exec2.flags = I915_EXEC_RENDER;
Jesse Barnes76446ca2009-12-17 22:05:42 -05004229
4230 ret = i915_gem_do_execbuffer(dev, data, file_priv, &exec2, exec2_list);
4231 if (!ret) {
4232 /* Copy the new buffer offsets back to the user's exec list. */
4233 for (i = 0; i < args->buffer_count; i++)
4234 exec_list[i].offset = exec2_list[i].offset;
4235 /* ... and back out to userspace */
4236 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
4237 (uintptr_t) args->buffers_ptr,
4238 exec_list,
4239 sizeof(*exec_list) * args->buffer_count);
4240 if (ret) {
4241 ret = -EFAULT;
4242 DRM_ERROR("failed to copy %d exec entries "
4243 "back to user (%d)\n",
4244 args->buffer_count, ret);
4245 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05004246 }
4247
4248 drm_free_large(exec_list);
4249 drm_free_large(exec2_list);
4250 return ret;
4251}
4252
4253int
4254i915_gem_execbuffer2(struct drm_device *dev, void *data,
4255 struct drm_file *file_priv)
4256{
4257 struct drm_i915_gem_execbuffer2 *args = data;
4258 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
4259 int ret;
4260
4261#if WATCH_EXEC
4262 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
4263 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
4264#endif
4265
4266 if (args->buffer_count < 1) {
4267 DRM_ERROR("execbuf2 with %d buffers\n", args->buffer_count);
4268 return -EINVAL;
4269 }
4270
4271 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
4272 if (exec2_list == NULL) {
4273 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
4274 args->buffer_count);
4275 return -ENOMEM;
4276 }
4277 ret = copy_from_user(exec2_list,
4278 (struct drm_i915_relocation_entry __user *)
4279 (uintptr_t) args->buffers_ptr,
4280 sizeof(*exec2_list) * args->buffer_count);
4281 if (ret != 0) {
4282 DRM_ERROR("copy %d exec entries failed %d\n",
4283 args->buffer_count, ret);
4284 drm_free_large(exec2_list);
4285 return -EFAULT;
4286 }
4287
4288 ret = i915_gem_do_execbuffer(dev, data, file_priv, args, exec2_list);
4289 if (!ret) {
4290 /* Copy the new buffer offsets back to the user's exec list. */
4291 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
4292 (uintptr_t) args->buffers_ptr,
4293 exec2_list,
4294 sizeof(*exec2_list) * args->buffer_count);
4295 if (ret) {
4296 ret = -EFAULT;
4297 DRM_ERROR("failed to copy %d exec entries "
4298 "back to user (%d)\n",
4299 args->buffer_count, ret);
4300 }
4301 }
4302
4303 drm_free_large(exec2_list);
4304 return ret;
4305}
4306
Eric Anholt673a3942008-07-30 12:06:12 -07004307int
Daniel Vetter920afa72010-09-16 17:54:23 +02004308i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01004309 bool map_and_fenceable)
Eric Anholt673a3942008-07-30 12:06:12 -07004310{
4311 struct drm_device *dev = obj->dev;
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004312 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01004313 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004314 int ret;
4315
Daniel Vetter778c3542010-05-13 11:49:44 +02004316 BUG_ON(obj_priv->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT);
Daniel Vetter75e9e912010-11-04 17:11:09 +01004317 BUG_ON(map_and_fenceable && !map_and_fenceable);
Chris Wilson23bc5982010-09-29 16:10:57 +01004318 WARN_ON(i915_verify_lists(dev));
Chris Wilsonac0c6b52010-05-27 13:18:18 +01004319
4320 if (obj_priv->gtt_space != NULL) {
Chris Wilsona00b10c2010-09-24 21:15:47 +01004321 if ((alignment && obj_priv->gtt_offset & (alignment - 1)) ||
Daniel Vetter75e9e912010-11-04 17:11:09 +01004322 (map_and_fenceable && !obj_priv->map_and_fenceable)) {
Chris Wilsonae7d49d2010-08-04 12:37:41 +01004323 WARN(obj_priv->pin_count,
4324 "bo is already pinned with incorrect alignment:"
Daniel Vetter75e9e912010-11-04 17:11:09 +01004325 " offset=%x, req.alignment=%x, req.map_and_fenceable=%d,"
4326 " obj->map_and_fenceable=%d\n",
Chris Wilsona00b10c2010-09-24 21:15:47 +01004327 obj_priv->gtt_offset, alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01004328 map_and_fenceable,
4329 obj_priv->map_and_fenceable);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01004330 ret = i915_gem_object_unbind(obj);
4331 if (ret)
4332 return ret;
4333 }
4334 }
4335
Eric Anholt673a3942008-07-30 12:06:12 -07004336 if (obj_priv->gtt_space == NULL) {
Chris Wilsona00b10c2010-09-24 21:15:47 +01004337 ret = i915_gem_object_bind_to_gtt(obj, alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01004338 map_and_fenceable);
Chris Wilson97311292009-09-21 00:22:34 +01004339 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07004340 return ret;
Chris Wilson22c344e2009-02-11 14:26:45 +00004341 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05004342
Chris Wilson74653782010-10-29 10:41:23 +01004343 if (obj_priv->pin_count++ == 0) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01004344 i915_gem_info_add_pin(dev_priv, obj_priv, map_and_fenceable);
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004345 if (!obj_priv->active)
Chris Wilson69dc4982010-10-19 10:36:51 +01004346 list_move_tail(&obj_priv->mm_list,
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004347 &dev_priv->mm.pinned_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004348 }
Daniel Vetter75e9e912010-11-04 17:11:09 +01004349 BUG_ON(!obj_priv->pin_mappable && map_and_fenceable);
Eric Anholt673a3942008-07-30 12:06:12 -07004350
Chris Wilson23bc5982010-09-29 16:10:57 +01004351 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07004352 return 0;
4353}
4354
4355void
4356i915_gem_object_unpin(struct drm_gem_object *obj)
4357{
4358 struct drm_device *dev = obj->dev;
4359 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01004360 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004361
Chris Wilson23bc5982010-09-29 16:10:57 +01004362 WARN_ON(i915_verify_lists(dev));
Chris Wilson74653782010-10-29 10:41:23 +01004363 BUG_ON(obj_priv->pin_count == 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004364 BUG_ON(obj_priv->gtt_space == NULL);
4365
Chris Wilson74653782010-10-29 10:41:23 +01004366 if (--obj_priv->pin_count == 0) {
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004367 if (!obj_priv->active)
Chris Wilson69dc4982010-10-19 10:36:51 +01004368 list_move_tail(&obj_priv->mm_list,
Eric Anholt673a3942008-07-30 12:06:12 -07004369 &dev_priv->mm.inactive_list);
Chris Wilsona00b10c2010-09-24 21:15:47 +01004370 i915_gem_info_remove_pin(dev_priv, obj_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07004371 }
Chris Wilson23bc5982010-09-29 16:10:57 +01004372 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07004373}
4374
4375int
4376i915_gem_pin_ioctl(struct drm_device *dev, void *data,
4377 struct drm_file *file_priv)
4378{
4379 struct drm_i915_gem_pin *args = data;
4380 struct drm_gem_object *obj;
4381 struct drm_i915_gem_object *obj_priv;
4382 int ret;
4383
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004384 ret = i915_mutex_lock_interruptible(dev);
4385 if (ret)
4386 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004387
4388 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4389 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004390 ret = -ENOENT;
4391 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07004392 }
Daniel Vetter23010e42010-03-08 13:35:02 +01004393 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004394
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004395 if (obj_priv->madv != I915_MADV_WILLNEED) {
4396 DRM_ERROR("Attempting to pin a purgeable buffer\n");
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004397 ret = -EINVAL;
4398 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004399 }
4400
Jesse Barnes79e53942008-11-07 14:24:08 -08004401 if (obj_priv->pin_filp != NULL && obj_priv->pin_filp != file_priv) {
4402 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
4403 args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004404 ret = -EINVAL;
4405 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08004406 }
4407
4408 obj_priv->user_pin_count++;
4409 obj_priv->pin_filp = file_priv;
4410 if (obj_priv->user_pin_count == 1) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01004411 ret = i915_gem_object_pin(obj, args->alignment, true);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004412 if (ret)
4413 goto out;
Eric Anholt673a3942008-07-30 12:06:12 -07004414 }
4415
4416 /* XXX - flush the CPU caches for pinned objects
4417 * as the X server doesn't manage domains yet
4418 */
Eric Anholte47c68e2008-11-14 13:35:19 -08004419 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004420 args->offset = obj_priv->gtt_offset;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004421out:
Eric Anholt673a3942008-07-30 12:06:12 -07004422 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004423unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07004424 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004425 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004426}
4427
4428int
4429i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
4430 struct drm_file *file_priv)
4431{
4432 struct drm_i915_gem_pin *args = data;
4433 struct drm_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08004434 struct drm_i915_gem_object *obj_priv;
Chris Wilson76c1dec2010-09-25 11:22:51 +01004435 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004436
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004437 ret = i915_mutex_lock_interruptible(dev);
4438 if (ret)
4439 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004440
4441 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4442 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004443 ret = -ENOENT;
4444 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07004445 }
Daniel Vetter23010e42010-03-08 13:35:02 +01004446 obj_priv = to_intel_bo(obj);
Chris Wilson76c1dec2010-09-25 11:22:51 +01004447
Jesse Barnes79e53942008-11-07 14:24:08 -08004448 if (obj_priv->pin_filp != file_priv) {
4449 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
4450 args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004451 ret = -EINVAL;
4452 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08004453 }
4454 obj_priv->user_pin_count--;
4455 if (obj_priv->user_pin_count == 0) {
4456 obj_priv->pin_filp = NULL;
4457 i915_gem_object_unpin(obj);
4458 }
Eric Anholt673a3942008-07-30 12:06:12 -07004459
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004460out:
Eric Anholt673a3942008-07-30 12:06:12 -07004461 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004462unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07004463 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004464 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004465}
4466
4467int
4468i915_gem_busy_ioctl(struct drm_device *dev, void *data,
4469 struct drm_file *file_priv)
4470{
4471 struct drm_i915_gem_busy *args = data;
4472 struct drm_gem_object *obj;
4473 struct drm_i915_gem_object *obj_priv;
Chris Wilson30dbf0c2010-09-25 10:19:17 +01004474 int ret;
4475
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004476 ret = i915_mutex_lock_interruptible(dev);
4477 if (ret)
4478 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004479
Eric Anholt673a3942008-07-30 12:06:12 -07004480 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4481 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004482 ret = -ENOENT;
4483 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07004484 }
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004485 obj_priv = to_intel_bo(obj);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004486
Chris Wilson0be555b2010-08-04 15:36:30 +01004487 /* Count all active objects as busy, even if they are currently not used
4488 * by the gpu. Users of this interface expect objects to eventually
4489 * become non-busy without any further actions, therefore emit any
4490 * necessary flushes here.
Eric Anholtc4de0a52008-12-14 19:05:04 -08004491 */
Chris Wilson0be555b2010-08-04 15:36:30 +01004492 args->busy = obj_priv->active;
4493 if (args->busy) {
4494 /* Unconditionally flush objects, even when the gpu still uses this
4495 * object. Userspace calling this function indicates that it wants to
4496 * use this buffer rather sooner than later, so issuing the required
4497 * flush earlier is beneficial.
4498 */
Chris Wilsonc78ec302010-09-20 12:50:23 +01004499 if (obj->write_domain & I915_GEM_GPU_DOMAINS)
4500 i915_gem_flush_ring(dev, file_priv,
Chris Wilson92204342010-09-18 11:02:01 +01004501 obj_priv->ring,
4502 0, obj->write_domain);
Chris Wilson0be555b2010-08-04 15:36:30 +01004503
4504 /* Update the active list for the hardware's current position.
4505 * Otherwise this only updates on a delayed timer or when irqs
4506 * are actually unmasked, and our working set ends up being
4507 * larger than required.
4508 */
4509 i915_gem_retire_requests_ring(dev, obj_priv->ring);
4510
4511 args->busy = obj_priv->active;
4512 }
Eric Anholt673a3942008-07-30 12:06:12 -07004513
4514 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004515unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07004516 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004517 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004518}
4519
4520int
4521i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4522 struct drm_file *file_priv)
4523{
4524 return i915_gem_ring_throttle(dev, file_priv);
4525}
4526
Chris Wilson3ef94da2009-09-14 16:50:29 +01004527int
4528i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4529 struct drm_file *file_priv)
4530{
4531 struct drm_i915_gem_madvise *args = data;
4532 struct drm_gem_object *obj;
4533 struct drm_i915_gem_object *obj_priv;
Chris Wilson76c1dec2010-09-25 11:22:51 +01004534 int ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004535
4536 switch (args->madv) {
4537 case I915_MADV_DONTNEED:
4538 case I915_MADV_WILLNEED:
4539 break;
4540 default:
4541 return -EINVAL;
4542 }
4543
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004544 ret = i915_mutex_lock_interruptible(dev);
4545 if (ret)
4546 return ret;
4547
Chris Wilson3ef94da2009-09-14 16:50:29 +01004548 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4549 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004550 ret = -ENOENT;
4551 goto unlock;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004552 }
Daniel Vetter23010e42010-03-08 13:35:02 +01004553 obj_priv = to_intel_bo(obj);
Chris Wilson3ef94da2009-09-14 16:50:29 +01004554
4555 if (obj_priv->pin_count) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004556 ret = -EINVAL;
4557 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004558 }
4559
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004560 if (obj_priv->madv != __I915_MADV_PURGED)
4561 obj_priv->madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004562
Chris Wilson2d7ef392009-09-20 23:13:10 +01004563 /* if the object is no longer bound, discard its backing storage */
4564 if (i915_gem_object_is_purgeable(obj_priv) &&
4565 obj_priv->gtt_space == NULL)
4566 i915_gem_object_truncate(obj);
4567
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004568 args->retained = obj_priv->madv != __I915_MADV_PURGED;
4569
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004570out:
Chris Wilson3ef94da2009-09-14 16:50:29 +01004571 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004572unlock:
Chris Wilson3ef94da2009-09-14 16:50:29 +01004573 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004574 return ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004575}
4576
Daniel Vetterac52bc52010-04-09 19:05:06 +00004577struct drm_gem_object * i915_gem_alloc_object(struct drm_device *dev,
4578 size_t size)
4579{
Chris Wilson73aa8082010-09-30 11:46:12 +01004580 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc397b902010-04-09 19:05:07 +00004581 struct drm_i915_gem_object *obj;
4582
4583 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
4584 if (obj == NULL)
4585 return NULL;
4586
4587 if (drm_gem_object_init(dev, &obj->base, size) != 0) {
4588 kfree(obj);
4589 return NULL;
4590 }
4591
Chris Wilson73aa8082010-09-30 11:46:12 +01004592 i915_gem_info_add_obj(dev_priv, size);
4593
Daniel Vetterc397b902010-04-09 19:05:07 +00004594 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4595 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4596
4597 obj->agp_type = AGP_USER_MEMORY;
Daniel Vetter62b8b212010-04-09 19:05:08 +00004598 obj->base.driver_private = NULL;
Daniel Vetterc397b902010-04-09 19:05:07 +00004599 obj->fence_reg = I915_FENCE_REG_NONE;
Chris Wilson69dc4982010-10-19 10:36:51 +01004600 INIT_LIST_HEAD(&obj->mm_list);
Daniel Vetter93a37f22010-11-05 20:24:53 +01004601 INIT_LIST_HEAD(&obj->gtt_list);
Chris Wilson69dc4982010-10-19 10:36:51 +01004602 INIT_LIST_HEAD(&obj->ring_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00004603 INIT_LIST_HEAD(&obj->gpu_write_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00004604 obj->madv = I915_MADV_WILLNEED;
Daniel Vetter75e9e912010-11-04 17:11:09 +01004605 /* Avoid an unnecessary call to unbind on the first bind. */
4606 obj->map_and_fenceable = true;
Daniel Vetterc397b902010-04-09 19:05:07 +00004607
Daniel Vetterc397b902010-04-09 19:05:07 +00004608 return &obj->base;
Daniel Vetterac52bc52010-04-09 19:05:06 +00004609}
4610
Eric Anholt673a3942008-07-30 12:06:12 -07004611int i915_gem_init_object(struct drm_gem_object *obj)
4612{
Daniel Vetterc397b902010-04-09 19:05:07 +00004613 BUG();
Jesse Barnesde151cf2008-11-12 10:03:55 -08004614
Eric Anholt673a3942008-07-30 12:06:12 -07004615 return 0;
4616}
4617
Chris Wilsonbe726152010-07-23 23:18:50 +01004618static void i915_gem_free_object_tail(struct drm_gem_object *obj)
4619{
4620 struct drm_device *dev = obj->dev;
4621 drm_i915_private_t *dev_priv = dev->dev_private;
4622 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
4623 int ret;
4624
4625 ret = i915_gem_object_unbind(obj);
4626 if (ret == -ERESTARTSYS) {
Chris Wilson69dc4982010-10-19 10:36:51 +01004627 list_move(&obj_priv->mm_list,
Chris Wilsonbe726152010-07-23 23:18:50 +01004628 &dev_priv->mm.deferred_free_list);
4629 return;
4630 }
4631
Chris Wilson39a01d12010-10-28 13:03:06 +01004632 if (obj->map_list.map)
Chris Wilsonbe726152010-07-23 23:18:50 +01004633 i915_gem_free_mmap_offset(obj);
4634
4635 drm_gem_object_release(obj);
Chris Wilson73aa8082010-09-30 11:46:12 +01004636 i915_gem_info_remove_obj(dev_priv, obj->size);
Chris Wilsonbe726152010-07-23 23:18:50 +01004637
4638 kfree(obj_priv->page_cpu_valid);
4639 kfree(obj_priv->bit_17);
4640 kfree(obj_priv);
4641}
4642
Eric Anholt673a3942008-07-30 12:06:12 -07004643void i915_gem_free_object(struct drm_gem_object *obj)
4644{
Jesse Barnesde151cf2008-11-12 10:03:55 -08004645 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01004646 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004647
Chris Wilson1c5d22f2009-08-25 11:15:50 +01004648 trace_i915_gem_object_destroy(obj);
4649
Eric Anholt673a3942008-07-30 12:06:12 -07004650 while (obj_priv->pin_count > 0)
4651 i915_gem_object_unpin(obj);
4652
Dave Airlie71acb5e2008-12-30 20:31:46 +10004653 if (obj_priv->phys_obj)
4654 i915_gem_detach_phys_object(dev, obj);
4655
Chris Wilsonbe726152010-07-23 23:18:50 +01004656 i915_gem_free_object_tail(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004657}
4658
Jesse Barnes5669fca2009-02-17 15:13:31 -08004659int
Eric Anholt673a3942008-07-30 12:06:12 -07004660i915_gem_idle(struct drm_device *dev)
4661{
4662 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson29105cc2010-01-07 10:39:13 +00004663 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004664
Keith Packard6dbe2772008-10-14 21:41:13 -07004665 mutex_lock(&dev->struct_mutex);
4666
Chris Wilson87acb0a2010-10-19 10:13:00 +01004667 if (dev_priv->mm.suspended) {
Keith Packard6dbe2772008-10-14 21:41:13 -07004668 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004669 return 0;
Keith Packard6dbe2772008-10-14 21:41:13 -07004670 }
Eric Anholt673a3942008-07-30 12:06:12 -07004671
Chris Wilson29105cc2010-01-07 10:39:13 +00004672 ret = i915_gpu_idle(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004673 if (ret) {
4674 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004675 return ret;
Keith Packard6dbe2772008-10-14 21:41:13 -07004676 }
Eric Anholt673a3942008-07-30 12:06:12 -07004677
Chris Wilson29105cc2010-01-07 10:39:13 +00004678 /* Under UMS, be paranoid and evict. */
4679 if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
Chris Wilson5eac3ab2010-10-31 08:49:47 +00004680 ret = i915_gem_evict_inactive(dev, false);
Chris Wilson29105cc2010-01-07 10:39:13 +00004681 if (ret) {
4682 mutex_unlock(&dev->struct_mutex);
4683 return ret;
4684 }
4685 }
4686
4687 /* Hack! Don't let anybody do execbuf while we don't control the chip.
4688 * We need to replace this with a semaphore, or something.
4689 * And not confound mm.suspended!
4690 */
4691 dev_priv->mm.suspended = 1;
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02004692 del_timer_sync(&dev_priv->hangcheck_timer);
Chris Wilson29105cc2010-01-07 10:39:13 +00004693
4694 i915_kernel_lost_context(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004695 i915_gem_cleanup_ringbuffer(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00004696
Keith Packard6dbe2772008-10-14 21:41:13 -07004697 mutex_unlock(&dev->struct_mutex);
4698
Chris Wilson29105cc2010-01-07 10:39:13 +00004699 /* Cancel the retire work handler, which should be idle now. */
4700 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
4701
Eric Anholt673a3942008-07-30 12:06:12 -07004702 return 0;
4703}
4704
Jesse Barnese552eb72010-04-21 11:39:23 -07004705/*
4706 * 965+ support PIPE_CONTROL commands, which provide finer grained control
4707 * over cache flushing.
4708 */
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004709static int
Jesse Barnese552eb72010-04-21 11:39:23 -07004710i915_gem_init_pipe_control(struct drm_device *dev)
4711{
4712 drm_i915_private_t *dev_priv = dev->dev_private;
4713 struct drm_gem_object *obj;
4714 struct drm_i915_gem_object *obj_priv;
4715 int ret;
4716
Eric Anholt34dc4d42010-05-07 14:30:03 -07004717 obj = i915_gem_alloc_object(dev, 4096);
Jesse Barnese552eb72010-04-21 11:39:23 -07004718 if (obj == NULL) {
4719 DRM_ERROR("Failed to allocate seqno page\n");
4720 ret = -ENOMEM;
4721 goto err;
4722 }
4723 obj_priv = to_intel_bo(obj);
4724 obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
4725
Daniel Vetter75e9e912010-11-04 17:11:09 +01004726 ret = i915_gem_object_pin(obj, 4096, true);
Jesse Barnese552eb72010-04-21 11:39:23 -07004727 if (ret)
4728 goto err_unref;
4729
4730 dev_priv->seqno_gfx_addr = obj_priv->gtt_offset;
4731 dev_priv->seqno_page = kmap(obj_priv->pages[0]);
4732 if (dev_priv->seqno_page == NULL)
4733 goto err_unpin;
4734
4735 dev_priv->seqno_obj = obj;
4736 memset(dev_priv->seqno_page, 0, PAGE_SIZE);
4737
4738 return 0;
4739
4740err_unpin:
4741 i915_gem_object_unpin(obj);
4742err_unref:
4743 drm_gem_object_unreference(obj);
4744err:
4745 return ret;
4746}
4747
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004748
4749static void
Jesse Barnese552eb72010-04-21 11:39:23 -07004750i915_gem_cleanup_pipe_control(struct drm_device *dev)
4751{
4752 drm_i915_private_t *dev_priv = dev->dev_private;
4753 struct drm_gem_object *obj;
4754 struct drm_i915_gem_object *obj_priv;
4755
4756 obj = dev_priv->seqno_obj;
4757 obj_priv = to_intel_bo(obj);
4758 kunmap(obj_priv->pages[0]);
4759 i915_gem_object_unpin(obj);
4760 drm_gem_object_unreference(obj);
4761 dev_priv->seqno_obj = NULL;
4762
4763 dev_priv->seqno_page = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07004764}
4765
Eric Anholt673a3942008-07-30 12:06:12 -07004766int
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004767i915_gem_init_ringbuffer(struct drm_device *dev)
4768{
4769 drm_i915_private_t *dev_priv = dev->dev_private;
4770 int ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01004771
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004772 if (HAS_PIPE_CONTROL(dev)) {
4773 ret = i915_gem_init_pipe_control(dev);
4774 if (ret)
4775 return ret;
4776 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004777
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08004778 ret = intel_init_render_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004779 if (ret)
4780 goto cleanup_pipe_control;
4781
4782 if (HAS_BSD(dev)) {
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08004783 ret = intel_init_bsd_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004784 if (ret)
4785 goto cleanup_render_ring;
Zou Nan haid1b851f2010-05-21 09:08:57 +08004786 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004787
Chris Wilson549f7362010-10-19 11:19:32 +01004788 if (HAS_BLT(dev)) {
4789 ret = intel_init_blt_ring_buffer(dev);
4790 if (ret)
4791 goto cleanup_bsd_ring;
4792 }
4793
Chris Wilson6f392d5482010-08-07 11:01:22 +01004794 dev_priv->next_seqno = 1;
4795
Chris Wilson68f95ba2010-05-27 13:18:22 +01004796 return 0;
4797
Chris Wilson549f7362010-10-19 11:19:32 +01004798cleanup_bsd_ring:
Chris Wilson78501ea2010-10-27 12:18:21 +01004799 intel_cleanup_ring_buffer(&dev_priv->bsd_ring);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004800cleanup_render_ring:
Chris Wilson78501ea2010-10-27 12:18:21 +01004801 intel_cleanup_ring_buffer(&dev_priv->render_ring);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004802cleanup_pipe_control:
4803 if (HAS_PIPE_CONTROL(dev))
4804 i915_gem_cleanup_pipe_control(dev);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004805 return ret;
4806}
4807
4808void
4809i915_gem_cleanup_ringbuffer(struct drm_device *dev)
4810{
4811 drm_i915_private_t *dev_priv = dev->dev_private;
4812
Chris Wilson78501ea2010-10-27 12:18:21 +01004813 intel_cleanup_ring_buffer(&dev_priv->render_ring);
4814 intel_cleanup_ring_buffer(&dev_priv->bsd_ring);
4815 intel_cleanup_ring_buffer(&dev_priv->blt_ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004816 if (HAS_PIPE_CONTROL(dev))
4817 i915_gem_cleanup_pipe_control(dev);
4818}
4819
4820int
Eric Anholt673a3942008-07-30 12:06:12 -07004821i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
4822 struct drm_file *file_priv)
4823{
4824 drm_i915_private_t *dev_priv = dev->dev_private;
4825 int ret;
4826
Jesse Barnes79e53942008-11-07 14:24:08 -08004827 if (drm_core_check_feature(dev, DRIVER_MODESET))
4828 return 0;
4829
Ben Gamariba1234d2009-09-14 17:48:47 -04004830 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07004831 DRM_ERROR("Reenabling wedged hardware, good luck\n");
Ben Gamariba1234d2009-09-14 17:48:47 -04004832 atomic_set(&dev_priv->mm.wedged, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004833 }
4834
Eric Anholt673a3942008-07-30 12:06:12 -07004835 mutex_lock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004836 dev_priv->mm.suspended = 0;
4837
4838 ret = i915_gem_init_ringbuffer(dev);
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004839 if (ret != 0) {
4840 mutex_unlock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004841 return ret;
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004842 }
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004843
Chris Wilson69dc4982010-10-19 10:36:51 +01004844 BUG_ON(!list_empty(&dev_priv->mm.active_list));
Zou Nan hai852835f2010-05-21 09:08:56 +08004845 BUG_ON(!list_empty(&dev_priv->render_ring.active_list));
Chris Wilson87acb0a2010-10-19 10:13:00 +01004846 BUG_ON(!list_empty(&dev_priv->bsd_ring.active_list));
Chris Wilson549f7362010-10-19 11:19:32 +01004847 BUG_ON(!list_empty(&dev_priv->blt_ring.active_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004848 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
4849 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
Zou Nan hai852835f2010-05-21 09:08:56 +08004850 BUG_ON(!list_empty(&dev_priv->render_ring.request_list));
Chris Wilson87acb0a2010-10-19 10:13:00 +01004851 BUG_ON(!list_empty(&dev_priv->bsd_ring.request_list));
Chris Wilson549f7362010-10-19 11:19:32 +01004852 BUG_ON(!list_empty(&dev_priv->blt_ring.request_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004853 mutex_unlock(&dev->struct_mutex);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004854
Chris Wilson5f353082010-06-07 14:03:03 +01004855 ret = drm_irq_install(dev);
4856 if (ret)
4857 goto cleanup_ringbuffer;
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004858
Eric Anholt673a3942008-07-30 12:06:12 -07004859 return 0;
Chris Wilson5f353082010-06-07 14:03:03 +01004860
4861cleanup_ringbuffer:
4862 mutex_lock(&dev->struct_mutex);
4863 i915_gem_cleanup_ringbuffer(dev);
4864 dev_priv->mm.suspended = 1;
4865 mutex_unlock(&dev->struct_mutex);
4866
4867 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004868}
4869
4870int
4871i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
4872 struct drm_file *file_priv)
4873{
Jesse Barnes79e53942008-11-07 14:24:08 -08004874 if (drm_core_check_feature(dev, DRIVER_MODESET))
4875 return 0;
4876
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004877 drm_irq_uninstall(dev);
Linus Torvaldse6890f62009-09-08 17:09:24 -07004878 return i915_gem_idle(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004879}
4880
4881void
4882i915_gem_lastclose(struct drm_device *dev)
4883{
4884 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004885
Eric Anholte806b492009-01-22 09:56:58 -08004886 if (drm_core_check_feature(dev, DRIVER_MODESET))
4887 return;
4888
Keith Packard6dbe2772008-10-14 21:41:13 -07004889 ret = i915_gem_idle(dev);
4890 if (ret)
4891 DRM_ERROR("failed to idle hardware: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07004892}
4893
Chris Wilson64193402010-10-24 12:38:05 +01004894static void
4895init_ring_lists(struct intel_ring_buffer *ring)
4896{
4897 INIT_LIST_HEAD(&ring->active_list);
4898 INIT_LIST_HEAD(&ring->request_list);
4899 INIT_LIST_HEAD(&ring->gpu_write_list);
4900}
4901
Eric Anholt673a3942008-07-30 12:06:12 -07004902void
4903i915_gem_load(struct drm_device *dev)
4904{
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004905 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07004906 drm_i915_private_t *dev_priv = dev->dev_private;
4907
Chris Wilson69dc4982010-10-19 10:36:51 +01004908 INIT_LIST_HEAD(&dev_priv->mm.active_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004909 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
4910 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004911 INIT_LIST_HEAD(&dev_priv->mm.pinned_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07004912 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Chris Wilsonbe726152010-07-23 23:18:50 +01004913 INIT_LIST_HEAD(&dev_priv->mm.deferred_free_list);
Daniel Vetter93a37f22010-11-05 20:24:53 +01004914 INIT_LIST_HEAD(&dev_priv->mm.gtt_list);
Chris Wilson64193402010-10-24 12:38:05 +01004915 init_ring_lists(&dev_priv->render_ring);
4916 init_ring_lists(&dev_priv->bsd_ring);
4917 init_ring_lists(&dev_priv->blt_ring);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02004918 for (i = 0; i < 16; i++)
4919 INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004920 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
4921 i915_gem_retire_work_handler);
Chris Wilson30dbf0c2010-09-25 10:19:17 +01004922 init_completion(&dev_priv->error_completion);
Chris Wilson31169712009-09-14 16:50:28 +01004923
Dave Airlie94400122010-07-20 13:15:31 +10004924 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
4925 if (IS_GEN3(dev)) {
4926 u32 tmp = I915_READ(MI_ARB_STATE);
4927 if (!(tmp & MI_ARB_C3_LP_WRITE_ENABLE)) {
4928 /* arb state is a masked write, so set bit + bit in mask */
4929 tmp = MI_ARB_C3_LP_WRITE_ENABLE | (MI_ARB_C3_LP_WRITE_ENABLE << MI_ARB_MASK_SHIFT);
4930 I915_WRITE(MI_ARB_STATE, tmp);
4931 }
4932 }
4933
Jesse Barnesde151cf2008-11-12 10:03:55 -08004934 /* Old X drivers will take 0-2 for front, back, depth buffers */
Eric Anholtb397c832010-01-26 09:43:10 -08004935 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4936 dev_priv->fence_reg_start = 3;
Jesse Barnesde151cf2008-11-12 10:03:55 -08004937
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004938 if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08004939 dev_priv->num_fence_regs = 16;
4940 else
4941 dev_priv->num_fence_regs = 8;
4942
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004943 /* Initialize fence registers to zero */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004944 switch (INTEL_INFO(dev)->gen) {
4945 case 6:
4946 for (i = 0; i < 16; i++)
4947 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (i * 8), 0);
4948 break;
4949 case 5:
4950 case 4:
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004951 for (i = 0; i < 16; i++)
4952 I915_WRITE64(FENCE_REG_965_0 + (i * 8), 0);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004953 break;
4954 case 3:
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004955 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4956 for (i = 0; i < 8; i++)
4957 I915_WRITE(FENCE_REG_945_8 + (i * 4), 0);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004958 case 2:
4959 for (i = 0; i < 8; i++)
4960 I915_WRITE(FENCE_REG_830_0 + (i * 4), 0);
4961 break;
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004962 }
Eric Anholt673a3942008-07-30 12:06:12 -07004963 i915_gem_detect_bit_6_swizzle(dev);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05004964 init_waitqueue_head(&dev_priv->pending_flip_queue);
Chris Wilson17250b72010-10-28 12:51:39 +01004965
4966 dev_priv->mm.inactive_shrinker.shrink = i915_gem_inactive_shrink;
4967 dev_priv->mm.inactive_shrinker.seeks = DEFAULT_SEEKS;
4968 register_shrinker(&dev_priv->mm.inactive_shrinker);
Eric Anholt673a3942008-07-30 12:06:12 -07004969}
Dave Airlie71acb5e2008-12-30 20:31:46 +10004970
4971/*
4972 * Create a physically contiguous memory object for this object
4973 * e.g. for cursor + overlay regs
4974 */
Chris Wilson995b6762010-08-20 13:23:26 +01004975static int i915_gem_init_phys_object(struct drm_device *dev,
4976 int id, int size, int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004977{
4978 drm_i915_private_t *dev_priv = dev->dev_private;
4979 struct drm_i915_gem_phys_object *phys_obj;
4980 int ret;
4981
4982 if (dev_priv->mm.phys_objs[id - 1] || !size)
4983 return 0;
4984
Eric Anholt9a298b22009-03-24 12:23:04 -07004985 phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004986 if (!phys_obj)
4987 return -ENOMEM;
4988
4989 phys_obj->id = id;
4990
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004991 phys_obj->handle = drm_pci_alloc(dev, size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004992 if (!phys_obj->handle) {
4993 ret = -ENOMEM;
4994 goto kfree_obj;
4995 }
4996#ifdef CONFIG_X86
4997 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4998#endif
4999
5000 dev_priv->mm.phys_objs[id - 1] = phys_obj;
5001
5002 return 0;
5003kfree_obj:
Eric Anholt9a298b22009-03-24 12:23:04 -07005004 kfree(phys_obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10005005 return ret;
5006}
5007
Chris Wilson995b6762010-08-20 13:23:26 +01005008static void i915_gem_free_phys_object(struct drm_device *dev, int id)
Dave Airlie71acb5e2008-12-30 20:31:46 +10005009{
5010 drm_i915_private_t *dev_priv = dev->dev_private;
5011 struct drm_i915_gem_phys_object *phys_obj;
5012
5013 if (!dev_priv->mm.phys_objs[id - 1])
5014 return;
5015
5016 phys_obj = dev_priv->mm.phys_objs[id - 1];
5017 if (phys_obj->cur_obj) {
5018 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
5019 }
5020
5021#ifdef CONFIG_X86
5022 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
5023#endif
5024 drm_pci_free(dev, phys_obj->handle);
5025 kfree(phys_obj);
5026 dev_priv->mm.phys_objs[id - 1] = NULL;
5027}
5028
5029void i915_gem_free_all_phys_object(struct drm_device *dev)
5030{
5031 int i;
5032
Dave Airlie260883c2009-01-22 17:58:49 +10005033 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
Dave Airlie71acb5e2008-12-30 20:31:46 +10005034 i915_gem_free_phys_object(dev, i);
5035}
5036
5037void i915_gem_detach_phys_object(struct drm_device *dev,
5038 struct drm_gem_object *obj)
5039{
Chris Wilsone5281cc2010-10-28 13:45:36 +01005040 struct address_space *mapping = obj->filp->f_path.dentry->d_inode->i_mapping;
5041 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
5042 char *vaddr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10005043 int i;
Dave Airlie71acb5e2008-12-30 20:31:46 +10005044 int page_count;
5045
Dave Airlie71acb5e2008-12-30 20:31:46 +10005046 if (!obj_priv->phys_obj)
5047 return;
Chris Wilsone5281cc2010-10-28 13:45:36 +01005048 vaddr = obj_priv->phys_obj->handle->vaddr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10005049
5050 page_count = obj->size / PAGE_SIZE;
5051
5052 for (i = 0; i < page_count; i++) {
Chris Wilsone5281cc2010-10-28 13:45:36 +01005053 struct page *page = read_cache_page_gfp(mapping, i,
5054 GFP_HIGHUSER | __GFP_RECLAIMABLE);
5055 if (!IS_ERR(page)) {
5056 char *dst = kmap_atomic(page);
5057 memcpy(dst, vaddr + i*PAGE_SIZE, PAGE_SIZE);
5058 kunmap_atomic(dst);
Dave Airlie71acb5e2008-12-30 20:31:46 +10005059
Chris Wilsone5281cc2010-10-28 13:45:36 +01005060 drm_clflush_pages(&page, 1);
5061
5062 set_page_dirty(page);
5063 mark_page_accessed(page);
5064 page_cache_release(page);
5065 }
Dave Airlie71acb5e2008-12-30 20:31:46 +10005066 }
Daniel Vetter40ce6572010-11-05 18:12:18 +01005067 intel_gtt_chipset_flush();
Chris Wilsond78b47b2009-06-17 21:52:49 +01005068
Dave Airlie71acb5e2008-12-30 20:31:46 +10005069 obj_priv->phys_obj->cur_obj = NULL;
5070 obj_priv->phys_obj = NULL;
5071}
5072
5073int
5074i915_gem_attach_phys_object(struct drm_device *dev,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01005075 struct drm_gem_object *obj,
5076 int id,
5077 int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10005078{
Chris Wilsone5281cc2010-10-28 13:45:36 +01005079 struct address_space *mapping = obj->filp->f_path.dentry->d_inode->i_mapping;
Dave Airlie71acb5e2008-12-30 20:31:46 +10005080 drm_i915_private_t *dev_priv = dev->dev_private;
5081 struct drm_i915_gem_object *obj_priv;
5082 int ret = 0;
5083 int page_count;
5084 int i;
5085
5086 if (id > I915_MAX_PHYS_OBJECT)
5087 return -EINVAL;
5088
Daniel Vetter23010e42010-03-08 13:35:02 +01005089 obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10005090
5091 if (obj_priv->phys_obj) {
5092 if (obj_priv->phys_obj->id == id)
5093 return 0;
5094 i915_gem_detach_phys_object(dev, obj);
5095 }
5096
Dave Airlie71acb5e2008-12-30 20:31:46 +10005097 /* create a new object */
5098 if (!dev_priv->mm.phys_objs[id - 1]) {
5099 ret = i915_gem_init_phys_object(dev, id,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01005100 obj->size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10005101 if (ret) {
Linus Torvaldsaeb565d2009-01-26 10:01:53 -08005102 DRM_ERROR("failed to init phys object %d size: %zu\n", id, obj->size);
Chris Wilsone5281cc2010-10-28 13:45:36 +01005103 return ret;
Dave Airlie71acb5e2008-12-30 20:31:46 +10005104 }
5105 }
5106
5107 /* bind to the object */
5108 obj_priv->phys_obj = dev_priv->mm.phys_objs[id - 1];
5109 obj_priv->phys_obj->cur_obj = obj;
5110
Dave Airlie71acb5e2008-12-30 20:31:46 +10005111 page_count = obj->size / PAGE_SIZE;
5112
5113 for (i = 0; i < page_count; i++) {
Chris Wilsone5281cc2010-10-28 13:45:36 +01005114 struct page *page;
5115 char *dst, *src;
Dave Airlie71acb5e2008-12-30 20:31:46 +10005116
Chris Wilsone5281cc2010-10-28 13:45:36 +01005117 page = read_cache_page_gfp(mapping, i,
5118 GFP_HIGHUSER | __GFP_RECLAIMABLE);
5119 if (IS_ERR(page))
5120 return PTR_ERR(page);
5121
Chris Wilsonff75b9b2010-10-30 22:52:31 +01005122 src = kmap_atomic(page);
Chris Wilsone5281cc2010-10-28 13:45:36 +01005123 dst = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
Dave Airlie71acb5e2008-12-30 20:31:46 +10005124 memcpy(dst, src, PAGE_SIZE);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -07005125 kunmap_atomic(src);
Chris Wilsone5281cc2010-10-28 13:45:36 +01005126
5127 mark_page_accessed(page);
5128 page_cache_release(page);
Dave Airlie71acb5e2008-12-30 20:31:46 +10005129 }
5130
5131 return 0;
Dave Airlie71acb5e2008-12-30 20:31:46 +10005132}
5133
5134static int
5135i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
5136 struct drm_i915_gem_pwrite *args,
5137 struct drm_file *file_priv)
5138{
Daniel Vetter23010e42010-03-08 13:35:02 +01005139 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsonb47b30c2010-11-08 01:12:29 +00005140 void *vaddr = obj_priv->phys_obj->handle->vaddr + args->offset;
5141 char __user *user_data = (char __user *) (uintptr_t) args->data_ptr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10005142
Chris Wilsonb47b30c2010-11-08 01:12:29 +00005143 DRM_DEBUG_DRIVER("vaddr %p, %lld\n", vaddr, args->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10005144
Chris Wilsonb47b30c2010-11-08 01:12:29 +00005145 if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) {
5146 unsigned long unwritten;
5147
5148 /* The physical object once assigned is fixed for the lifetime
5149 * of the obj, so we can safely drop the lock and continue
5150 * to access vaddr.
5151 */
5152 mutex_unlock(&dev->struct_mutex);
5153 unwritten = copy_from_user(vaddr, user_data, args->size);
5154 mutex_lock(&dev->struct_mutex);
5155 if (unwritten)
5156 return -EFAULT;
5157 }
Dave Airlie71acb5e2008-12-30 20:31:46 +10005158
Daniel Vetter40ce6572010-11-05 18:12:18 +01005159 intel_gtt_chipset_flush();
Dave Airlie71acb5e2008-12-30 20:31:46 +10005160 return 0;
5161}
Eric Anholtb9624422009-06-03 07:27:35 +00005162
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005163void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00005164{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005165 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00005166
5167 /* Clean up our request list when the client is going away, so that
5168 * later retire_requests won't dereference our soon-to-be-gone
5169 * file_priv.
5170 */
Chris Wilson1c255952010-09-26 11:03:27 +01005171 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005172 while (!list_empty(&file_priv->mm.request_list)) {
5173 struct drm_i915_gem_request *request;
5174
5175 request = list_first_entry(&file_priv->mm.request_list,
5176 struct drm_i915_gem_request,
5177 client_list);
5178 list_del(&request->client_list);
5179 request->file_priv = NULL;
5180 }
Chris Wilson1c255952010-09-26 11:03:27 +01005181 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00005182}
Chris Wilson31169712009-09-14 16:50:28 +01005183
Chris Wilson31169712009-09-14 16:50:28 +01005184static int
Chris Wilson1637ef42010-04-20 17:10:35 +01005185i915_gpu_is_active(struct drm_device *dev)
5186{
5187 drm_i915_private_t *dev_priv = dev->dev_private;
5188 int lists_empty;
5189
Chris Wilson1637ef42010-04-20 17:10:35 +01005190 lists_empty = list_empty(&dev_priv->mm.flushing_list) &&
Chris Wilson17250b72010-10-28 12:51:39 +01005191 list_empty(&dev_priv->mm.active_list);
Chris Wilson1637ef42010-04-20 17:10:35 +01005192
5193 return !lists_empty;
5194}
5195
5196static int
Chris Wilson17250b72010-10-28 12:51:39 +01005197i915_gem_inactive_shrink(struct shrinker *shrinker,
5198 int nr_to_scan,
5199 gfp_t gfp_mask)
Chris Wilson31169712009-09-14 16:50:28 +01005200{
Chris Wilson17250b72010-10-28 12:51:39 +01005201 struct drm_i915_private *dev_priv =
5202 container_of(shrinker,
5203 struct drm_i915_private,
5204 mm.inactive_shrinker);
5205 struct drm_device *dev = dev_priv->dev;
5206 struct drm_i915_gem_object *obj, *next;
5207 int cnt;
5208
5209 if (!mutex_trylock(&dev->struct_mutex))
Chris Wilsonbbe2e112010-10-28 22:35:07 +01005210 return 0;
Chris Wilson31169712009-09-14 16:50:28 +01005211
5212 /* "fast-path" to count number of available objects */
5213 if (nr_to_scan == 0) {
Chris Wilson17250b72010-10-28 12:51:39 +01005214 cnt = 0;
5215 list_for_each_entry(obj,
5216 &dev_priv->mm.inactive_list,
5217 mm_list)
5218 cnt++;
5219 mutex_unlock(&dev->struct_mutex);
5220 return cnt / 100 * sysctl_vfs_cache_pressure;
Chris Wilson31169712009-09-14 16:50:28 +01005221 }
5222
Chris Wilson1637ef42010-04-20 17:10:35 +01005223rescan:
Chris Wilson31169712009-09-14 16:50:28 +01005224 /* first scan for clean buffers */
Chris Wilson17250b72010-10-28 12:51:39 +01005225 i915_gem_retire_requests(dev);
Chris Wilson31169712009-09-14 16:50:28 +01005226
Chris Wilson17250b72010-10-28 12:51:39 +01005227 list_for_each_entry_safe(obj, next,
5228 &dev_priv->mm.inactive_list,
5229 mm_list) {
5230 if (i915_gem_object_is_purgeable(obj)) {
5231 i915_gem_object_unbind(&obj->base);
5232 if (--nr_to_scan == 0)
5233 break;
Chris Wilson31169712009-09-14 16:50:28 +01005234 }
Chris Wilson31169712009-09-14 16:50:28 +01005235 }
5236
5237 /* second pass, evict/count anything still on the inactive list */
Chris Wilson17250b72010-10-28 12:51:39 +01005238 cnt = 0;
5239 list_for_each_entry_safe(obj, next,
5240 &dev_priv->mm.inactive_list,
5241 mm_list) {
5242 if (nr_to_scan) {
5243 i915_gem_object_unbind(&obj->base);
5244 nr_to_scan--;
5245 } else
5246 cnt++;
Chris Wilson31169712009-09-14 16:50:28 +01005247 }
5248
Chris Wilson17250b72010-10-28 12:51:39 +01005249 if (nr_to_scan && i915_gpu_is_active(dev)) {
Chris Wilson1637ef42010-04-20 17:10:35 +01005250 /*
5251 * We are desperate for pages, so as a last resort, wait
5252 * for the GPU to finish and discard whatever we can.
5253 * This has a dramatic impact to reduce the number of
5254 * OOM-killer events whilst running the GPU aggressively.
5255 */
Chris Wilson17250b72010-10-28 12:51:39 +01005256 if (i915_gpu_idle(dev) == 0)
Chris Wilson1637ef42010-04-20 17:10:35 +01005257 goto rescan;
5258 }
Chris Wilson17250b72010-10-28 12:51:39 +01005259 mutex_unlock(&dev->struct_mutex);
5260 return cnt / 100 * sysctl_vfs_cache_pressure;
Chris Wilson31169712009-09-14 16:50:28 +01005261}