blob: 07e1b25f4a8a1ce60156e2efa2113f4b335c2c9a [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
Chris Wilson73aa8082010-09-30 11:46:12 +01002291 drm_unbind_agp(obj_priv->agp_mem);
2292 drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002293
Chris Wilsone5281cc2010-10-28 13:45:36 +01002294 i915_gem_object_put_pages_gtt(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002295
Chris Wilsona00b10c2010-09-24 21:15:47 +01002296 i915_gem_info_remove_gtt(dev_priv, obj_priv);
Chris Wilson69dc4982010-10-19 10:36:51 +01002297 list_del_init(&obj_priv->mm_list);
Daniel Vetter75e9e912010-11-04 17:11:09 +01002298 /* Avoid an unnecessary call to unbind on rebind. */
2299 obj_priv->map_and_fenceable = true;
Eric Anholt673a3942008-07-30 12:06:12 -07002300
Chris Wilson73aa8082010-09-30 11:46:12 +01002301 drm_mm_put_block(obj_priv->gtt_space);
2302 obj_priv->gtt_space = NULL;
Chris Wilson9af90d12010-10-17 10:01:56 +01002303 obj_priv->gtt_offset = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002304
Chris Wilson963b4832009-09-20 23:03:54 +01002305 if (i915_gem_object_is_purgeable(obj_priv))
2306 i915_gem_object_truncate(obj);
2307
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002308 trace_i915_gem_object_unbind(obj);
2309
Chris Wilson8dc17752010-07-23 23:18:51 +01002310 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002311}
2312
Chris Wilsona56ba562010-09-28 10:07:56 +01002313static int i915_ring_idle(struct drm_device *dev,
2314 struct intel_ring_buffer *ring)
2315{
Chris Wilson395b70b2010-10-28 21:28:46 +01002316 if (list_empty(&ring->gpu_write_list) && list_empty(&ring->active_list))
Chris Wilson64193402010-10-24 12:38:05 +01002317 return 0;
2318
Chris Wilsona56ba562010-09-28 10:07:56 +01002319 i915_gem_flush_ring(dev, NULL, ring,
2320 I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
2321 return i915_wait_request(dev,
2322 i915_gem_next_request_seqno(dev, ring),
2323 ring);
2324}
2325
Chris Wilsonb47eb4a2010-08-07 11:01:23 +01002326int
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002327i915_gpu_idle(struct drm_device *dev)
2328{
2329 drm_i915_private_t *dev_priv = dev->dev_private;
2330 bool lists_empty;
Zou Nan hai852835f2010-05-21 09:08:56 +08002331 int ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002332
Zou Nan haid1b851f2010-05-21 09:08:57 +08002333 lists_empty = (list_empty(&dev_priv->mm.flushing_list) &&
Chris Wilson395b70b2010-10-28 21:28:46 +01002334 list_empty(&dev_priv->mm.active_list));
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002335 if (lists_empty)
2336 return 0;
2337
2338 /* Flush everything onto the inactive list. */
Chris Wilsona56ba562010-09-28 10:07:56 +01002339 ret = i915_ring_idle(dev, &dev_priv->render_ring);
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002340 if (ret)
2341 return ret;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002342
Chris Wilson87acb0a2010-10-19 10:13:00 +01002343 ret = i915_ring_idle(dev, &dev_priv->bsd_ring);
2344 if (ret)
2345 return ret;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002346
Chris Wilson549f7362010-10-19 11:19:32 +01002347 ret = i915_ring_idle(dev, &dev_priv->blt_ring);
2348 if (ret)
2349 return ret;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002350
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002351 return 0;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002352}
2353
Chris Wilsona00b10c2010-09-24 21:15:47 +01002354static void sandybridge_write_fence_reg(struct drm_gem_object *obj)
Eric Anholt4e901fd2009-10-26 16:44:17 -07002355{
Eric Anholt4e901fd2009-10-26 16:44:17 -07002356 struct drm_device *dev = obj->dev;
2357 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002358 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsona00b10c2010-09-24 21:15:47 +01002359 u32 size = i915_gem_get_gtt_size(obj_priv);
Eric Anholt4e901fd2009-10-26 16:44:17 -07002360 int regnum = obj_priv->fence_reg;
2361 uint64_t val;
2362
Chris Wilsona00b10c2010-09-24 21:15:47 +01002363 val = (uint64_t)((obj_priv->gtt_offset + size - 4096) &
Eric Anholt4e901fd2009-10-26 16:44:17 -07002364 0xfffff000) << 32;
2365 val |= obj_priv->gtt_offset & 0xfffff000;
2366 val |= (uint64_t)((obj_priv->stride / 128) - 1) <<
2367 SANDYBRIDGE_FENCE_PITCH_SHIFT;
2368
2369 if (obj_priv->tiling_mode == I915_TILING_Y)
2370 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2371 val |= I965_FENCE_REG_VALID;
2372
2373 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (regnum * 8), val);
2374}
2375
Chris Wilsona00b10c2010-09-24 21:15:47 +01002376static void i965_write_fence_reg(struct drm_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002377{
Jesse Barnesde151cf2008-11-12 10:03:55 -08002378 struct drm_device *dev = obj->dev;
2379 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002380 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsona00b10c2010-09-24 21:15:47 +01002381 u32 size = i915_gem_get_gtt_size(obj_priv);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002382 int regnum = obj_priv->fence_reg;
2383 uint64_t val;
2384
Chris Wilsona00b10c2010-09-24 21:15:47 +01002385 val = (uint64_t)((obj_priv->gtt_offset + size - 4096) &
Jesse Barnesde151cf2008-11-12 10:03:55 -08002386 0xfffff000) << 32;
2387 val |= obj_priv->gtt_offset & 0xfffff000;
2388 val |= ((obj_priv->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
2389 if (obj_priv->tiling_mode == I915_TILING_Y)
2390 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2391 val |= I965_FENCE_REG_VALID;
2392
2393 I915_WRITE64(FENCE_REG_965_0 + (regnum * 8), val);
2394}
2395
Chris Wilsona00b10c2010-09-24 21:15:47 +01002396static void i915_write_fence_reg(struct drm_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002397{
Jesse Barnesde151cf2008-11-12 10:03:55 -08002398 struct drm_device *dev = obj->dev;
2399 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002400 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsona00b10c2010-09-24 21:15:47 +01002401 u32 size = i915_gem_get_gtt_size(obj_priv);
2402 uint32_t fence_reg, val, pitch_val;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002403 int tile_width;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002404
2405 if ((obj_priv->gtt_offset & ~I915_FENCE_START_MASK) ||
Chris Wilsona00b10c2010-09-24 21:15:47 +01002406 (obj_priv->gtt_offset & (size - 1))) {
2407 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 +01002408 __func__, obj_priv->gtt_offset, obj_priv->map_and_fenceable, size,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002409 obj_priv->gtt_space->start, obj_priv->gtt_space->size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002410 return;
2411 }
2412
Jesse Barnes0f973f22009-01-26 17:10:45 -08002413 if (obj_priv->tiling_mode == I915_TILING_Y &&
2414 HAS_128_BYTE_Y_TILING(dev))
2415 tile_width = 128;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002416 else
Jesse Barnes0f973f22009-01-26 17:10:45 -08002417 tile_width = 512;
2418
2419 /* Note: pitch better be a power of two tile widths */
2420 pitch_val = obj_priv->stride / tile_width;
2421 pitch_val = ffs(pitch_val) - 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002422
Daniel Vetterc36a2a62010-04-17 15:12:03 +02002423 if (obj_priv->tiling_mode == I915_TILING_Y &&
2424 HAS_128_BYTE_Y_TILING(dev))
2425 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2426 else
2427 WARN_ON(pitch_val > I915_FENCE_MAX_PITCH_VAL);
2428
Jesse Barnesde151cf2008-11-12 10:03:55 -08002429 val = obj_priv->gtt_offset;
2430 if (obj_priv->tiling_mode == I915_TILING_Y)
2431 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002432 val |= I915_FENCE_SIZE_BITS(size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002433 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2434 val |= I830_FENCE_REG_VALID;
2435
Chris Wilsona00b10c2010-09-24 21:15:47 +01002436 fence_reg = obj_priv->fence_reg;
2437 if (fence_reg < 8)
2438 fence_reg = FENCE_REG_830_0 + fence_reg * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002439 else
Chris Wilsona00b10c2010-09-24 21:15:47 +01002440 fence_reg = FENCE_REG_945_8 + (fence_reg - 8) * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002441 I915_WRITE(fence_reg, val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002442}
2443
Chris Wilsona00b10c2010-09-24 21:15:47 +01002444static void i830_write_fence_reg(struct drm_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002445{
Jesse Barnesde151cf2008-11-12 10:03:55 -08002446 struct drm_device *dev = obj->dev;
2447 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002448 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsona00b10c2010-09-24 21:15:47 +01002449 u32 size = i915_gem_get_gtt_size(obj_priv);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002450 int regnum = obj_priv->fence_reg;
2451 uint32_t val;
2452 uint32_t pitch_val;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002453 uint32_t fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002454
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002455 if ((obj_priv->gtt_offset & ~I830_FENCE_START_MASK) ||
Jesse Barnesde151cf2008-11-12 10:03:55 -08002456 (obj_priv->gtt_offset & (obj->size - 1))) {
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002457 WARN(1, "%s: object 0x%08x not 512K or size aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002458 __func__, obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002459 return;
2460 }
2461
Eric Anholte76a16d2009-05-26 17:44:56 -07002462 pitch_val = obj_priv->stride / 128;
2463 pitch_val = ffs(pitch_val) - 1;
2464 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2465
Jesse Barnesde151cf2008-11-12 10:03:55 -08002466 val = obj_priv->gtt_offset;
2467 if (obj_priv->tiling_mode == I915_TILING_Y)
2468 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002469 fence_size_bits = I830_FENCE_SIZE_BITS(size);
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002470 WARN_ON(fence_size_bits & ~0x00000f00);
2471 val |= fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002472 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2473 val |= I830_FENCE_REG_VALID;
2474
2475 I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002476}
2477
Chris Wilson2cf34d72010-09-14 13:03:28 +01002478static int i915_find_fence_reg(struct drm_device *dev,
2479 bool interruptible)
Daniel Vetterae3db242010-02-19 11:51:58 +01002480{
Daniel Vetterae3db242010-02-19 11:51:58 +01002481 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002482 struct drm_i915_fence_reg *reg;
2483 struct drm_i915_gem_object *obj_priv = NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002484 int i, avail, ret;
2485
2486 /* First try to find a free reg */
2487 avail = 0;
2488 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2489 reg = &dev_priv->fence_regs[i];
2490 if (!reg->obj)
2491 return i;
2492
Daniel Vetter23010e42010-03-08 13:35:02 +01002493 obj_priv = to_intel_bo(reg->obj);
Daniel Vetterae3db242010-02-19 11:51:58 +01002494 if (!obj_priv->pin_count)
2495 avail++;
2496 }
2497
2498 if (avail == 0)
2499 return -ENOSPC;
2500
2501 /* None available, try to steal one or wait for a user to finish */
Chris Wilsona00b10c2010-09-24 21:15:47 +01002502 avail = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002503 list_for_each_entry(reg, &dev_priv->mm.fence_list,
2504 lru_list) {
Chris Wilsona00b10c2010-09-24 21:15:47 +01002505 obj_priv = to_intel_bo(reg->obj);
Daniel Vetterae3db242010-02-19 11:51:58 +01002506 if (obj_priv->pin_count)
2507 continue;
2508
2509 /* found one! */
Chris Wilsona00b10c2010-09-24 21:15:47 +01002510 avail = obj_priv->fence_reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01002511 break;
2512 }
2513
Chris Wilsona00b10c2010-09-24 21:15:47 +01002514 BUG_ON(avail == I915_FENCE_REG_NONE);
Daniel Vetterae3db242010-02-19 11:51:58 +01002515
2516 /* We only have a reference on obj from the active list. put_fence_reg
2517 * might drop that one, causing a use-after-free in it. So hold a
2518 * private reference to obj like the other callers of put_fence_reg
2519 * (set_tiling ioctl) do. */
Chris Wilsona00b10c2010-09-24 21:15:47 +01002520 drm_gem_object_reference(&obj_priv->base);
2521 ret = i915_gem_object_put_fence_reg(&obj_priv->base, interruptible);
2522 drm_gem_object_unreference(&obj_priv->base);
Daniel Vetterae3db242010-02-19 11:51:58 +01002523 if (ret != 0)
2524 return ret;
2525
Chris Wilsona00b10c2010-09-24 21:15:47 +01002526 return avail;
Daniel Vetterae3db242010-02-19 11:51:58 +01002527}
2528
Jesse Barnesde151cf2008-11-12 10:03:55 -08002529/**
2530 * i915_gem_object_get_fence_reg - set up a fence reg for an object
2531 * @obj: object to map through a fence reg
2532 *
2533 * When mapping objects through the GTT, userspace wants to be able to write
2534 * to them without having to worry about swizzling if the object is tiled.
2535 *
2536 * This function walks the fence regs looking for a free one for @obj,
2537 * stealing one if it can't find any.
2538 *
2539 * It then sets up the reg based on the object's properties: address, pitch
2540 * and tiling format.
2541 */
Chris Wilson8c4b8c32009-06-17 22:08:52 +01002542int
Chris Wilson2cf34d72010-09-14 13:03:28 +01002543i915_gem_object_get_fence_reg(struct drm_gem_object *obj,
2544 bool interruptible)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002545{
2546 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002547 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002548 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002549 struct drm_i915_fence_reg *reg = NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002550 int ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002551
Eric Anholta09ba7f2009-08-29 12:49:51 -07002552 /* Just update our place in the LRU if our fence is getting used. */
2553 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002554 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
2555 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002556 return 0;
2557 }
2558
Jesse Barnesde151cf2008-11-12 10:03:55 -08002559 switch (obj_priv->tiling_mode) {
2560 case I915_TILING_NONE:
2561 WARN(1, "allocating a fence for non-tiled object?\n");
2562 break;
2563 case I915_TILING_X:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002564 if (!obj_priv->stride)
2565 return -EINVAL;
2566 WARN((obj_priv->stride & (512 - 1)),
2567 "object 0x%08x is X tiled but has non-512B pitch\n",
2568 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002569 break;
2570 case I915_TILING_Y:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002571 if (!obj_priv->stride)
2572 return -EINVAL;
2573 WARN((obj_priv->stride & (128 - 1)),
2574 "object 0x%08x is Y tiled but has non-128B pitch\n",
2575 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002576 break;
2577 }
2578
Chris Wilson2cf34d72010-09-14 13:03:28 +01002579 ret = i915_find_fence_reg(dev, interruptible);
Daniel Vetterae3db242010-02-19 11:51:58 +01002580 if (ret < 0)
2581 return ret;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002582
Daniel Vetterae3db242010-02-19 11:51:58 +01002583 obj_priv->fence_reg = ret;
2584 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002585 list_add_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002586
Jesse Barnesde151cf2008-11-12 10:03:55 -08002587 reg->obj = obj;
2588
Chris Wilsone259bef2010-09-17 00:32:02 +01002589 switch (INTEL_INFO(dev)->gen) {
2590 case 6:
Chris Wilsona00b10c2010-09-24 21:15:47 +01002591 sandybridge_write_fence_reg(obj);
Chris Wilsone259bef2010-09-17 00:32:02 +01002592 break;
2593 case 5:
2594 case 4:
Chris Wilsona00b10c2010-09-24 21:15:47 +01002595 i965_write_fence_reg(obj);
Chris Wilsone259bef2010-09-17 00:32:02 +01002596 break;
2597 case 3:
Chris Wilsona00b10c2010-09-24 21:15:47 +01002598 i915_write_fence_reg(obj);
Chris Wilsone259bef2010-09-17 00:32:02 +01002599 break;
2600 case 2:
Chris Wilsona00b10c2010-09-24 21:15:47 +01002601 i830_write_fence_reg(obj);
Chris Wilsone259bef2010-09-17 00:32:02 +01002602 break;
2603 }
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002604
Chris Wilsona00b10c2010-09-24 21:15:47 +01002605 trace_i915_gem_object_get_fence(obj,
2606 obj_priv->fence_reg,
2607 obj_priv->tiling_mode);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002608
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002609 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002610}
2611
2612/**
2613 * i915_gem_clear_fence_reg - clear out fence register info
2614 * @obj: object to clear
2615 *
2616 * Zeroes out the fence register itself and clears out the associated
2617 * data structures in dev_priv and obj_priv.
2618 */
2619static void
2620i915_gem_clear_fence_reg(struct drm_gem_object *obj)
2621{
2622 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002623 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002624 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002625 struct drm_i915_fence_reg *reg =
2626 &dev_priv->fence_regs[obj_priv->fence_reg];
Chris Wilsone259bef2010-09-17 00:32:02 +01002627 uint32_t fence_reg;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002628
Chris Wilsone259bef2010-09-17 00:32:02 +01002629 switch (INTEL_INFO(dev)->gen) {
2630 case 6:
Eric Anholt4e901fd2009-10-26 16:44:17 -07002631 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 +
2632 (obj_priv->fence_reg * 8), 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002633 break;
2634 case 5:
2635 case 4:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002636 I915_WRITE64(FENCE_REG_965_0 + (obj_priv->fence_reg * 8), 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002637 break;
2638 case 3:
Chris Wilson9b74f732010-09-22 19:10:44 +01002639 if (obj_priv->fence_reg >= 8)
Chris Wilsone259bef2010-09-17 00:32:02 +01002640 fence_reg = FENCE_REG_945_8 + (obj_priv->fence_reg - 8) * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002641 else
Chris Wilsone259bef2010-09-17 00:32:02 +01002642 case 2:
2643 fence_reg = FENCE_REG_830_0 + obj_priv->fence_reg * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002644
2645 I915_WRITE(fence_reg, 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002646 break;
Eric Anholtdc529a42009-03-10 22:34:49 -07002647 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002648
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002649 reg->obj = NULL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002650 obj_priv->fence_reg = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002651 list_del_init(&reg->lru_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002652}
2653
Eric Anholt673a3942008-07-30 12:06:12 -07002654/**
Chris Wilson52dc7d32009-06-06 09:46:01 +01002655 * i915_gem_object_put_fence_reg - waits on outstanding fenced access
2656 * to the buffer to finish, and then resets the fence register.
2657 * @obj: tiled object holding a fence register.
Chris Wilson2cf34d72010-09-14 13:03:28 +01002658 * @bool: whether the wait upon the fence is interruptible
Chris Wilson52dc7d32009-06-06 09:46:01 +01002659 *
2660 * Zeroes out the fence register itself and clears out the associated
2661 * data structures in dev_priv and obj_priv.
2662 */
2663int
Chris Wilson2cf34d72010-09-14 13:03:28 +01002664i915_gem_object_put_fence_reg(struct drm_gem_object *obj,
2665 bool interruptible)
Chris Wilson52dc7d32009-06-06 09:46:01 +01002666{
2667 struct drm_device *dev = obj->dev;
Chris Wilson53640e12010-09-20 11:40:50 +01002668 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002669 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson53640e12010-09-20 11:40:50 +01002670 struct drm_i915_fence_reg *reg;
Chris Wilson52dc7d32009-06-06 09:46:01 +01002671
2672 if (obj_priv->fence_reg == I915_FENCE_REG_NONE)
2673 return 0;
2674
Daniel Vetter10ae9bd2010-02-01 13:59:17 +01002675 /* If we've changed tiling, GTT-mappings of the object
2676 * need to re-fault to ensure that the correct fence register
2677 * setup is in place.
2678 */
2679 i915_gem_release_mmap(obj);
2680
Chris Wilson52dc7d32009-06-06 09:46:01 +01002681 /* On the i915, GPU access to tiled buffers is via a fence,
2682 * therefore we must wait for any outstanding access to complete
2683 * before clearing the fence.
2684 */
Chris Wilson53640e12010-09-20 11:40:50 +01002685 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
2686 if (reg->gpu) {
Chris Wilson52dc7d32009-06-06 09:46:01 +01002687 int ret;
2688
Chris Wilson2cf34d72010-09-14 13:03:28 +01002689 ret = i915_gem_object_flush_gpu_write_domain(obj, true);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002690 if (ret)
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002691 return ret;
2692
Chris Wilson2cf34d72010-09-14 13:03:28 +01002693 ret = i915_gem_object_wait_rendering(obj, interruptible);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002694 if (ret)
Chris Wilson52dc7d32009-06-06 09:46:01 +01002695 return ret;
Chris Wilson53640e12010-09-20 11:40:50 +01002696
2697 reg->gpu = false;
Chris Wilson52dc7d32009-06-06 09:46:01 +01002698 }
2699
Daniel Vetter4a726612010-02-01 13:59:16 +01002700 i915_gem_object_flush_gtt_write_domain(obj);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002701 i915_gem_clear_fence_reg(obj);
Chris Wilson52dc7d32009-06-06 09:46:01 +01002702
2703 return 0;
2704}
2705
2706/**
Eric Anholt673a3942008-07-30 12:06:12 -07002707 * Finds free space in the GTT aperture and binds the object there.
2708 */
2709static int
Daniel Vetter920afa72010-09-16 17:54:23 +02002710i915_gem_object_bind_to_gtt(struct drm_gem_object *obj,
2711 unsigned alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01002712 bool map_and_fenceable)
Eric Anholt673a3942008-07-30 12:06:12 -07002713{
2714 struct drm_device *dev = obj->dev;
2715 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002716 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002717 struct drm_mm_node *free_space;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002718 gfp_t gfpmask = __GFP_NORETRY | __GFP_NOWARN;
Daniel Vetter5e783302010-11-14 22:32:36 +01002719 u32 size, fence_size, fence_alignment, unfenced_alignment;
Daniel Vetter75e9e912010-11-04 17:11:09 +01002720 bool mappable, fenceable;
Chris Wilson07f73f62009-09-14 16:50:30 +01002721 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002722
Chris Wilsonbb6baf72009-09-22 14:24:13 +01002723 if (obj_priv->madv != I915_MADV_WILLNEED) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01002724 DRM_ERROR("Attempting to bind a purgeable object\n");
2725 return -EINVAL;
2726 }
2727
Chris Wilsona00b10c2010-09-24 21:15:47 +01002728 fence_size = i915_gem_get_gtt_size(obj_priv);
2729 fence_alignment = i915_gem_get_gtt_alignment(obj_priv);
Daniel Vetter5e783302010-11-14 22:32:36 +01002730 unfenced_alignment = i915_gem_get_unfenced_gtt_alignment(obj_priv);
Chris Wilsona00b10c2010-09-24 21:15:47 +01002731
Eric Anholt673a3942008-07-30 12:06:12 -07002732 if (alignment == 0)
Daniel Vetter5e783302010-11-14 22:32:36 +01002733 alignment = map_and_fenceable ? fence_alignment :
2734 unfenced_alignment;
Daniel Vetter75e9e912010-11-04 17:11:09 +01002735 if (map_and_fenceable && alignment & (fence_alignment - 1)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002736 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2737 return -EINVAL;
2738 }
2739
Daniel Vetter75e9e912010-11-04 17:11:09 +01002740 size = map_and_fenceable ? fence_size : obj->size;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002741
Chris Wilson654fc602010-05-27 13:18:21 +01002742 /* If the object is bigger than the entire aperture, reject it early
2743 * before evicting everything in a vain attempt to find space.
2744 */
Daniel Vetter920afa72010-09-16 17:54:23 +02002745 if (obj->size >
Daniel Vetter75e9e912010-11-04 17:11:09 +01002746 (map_and_fenceable ? dev_priv->mm.gtt_mappable_end : dev_priv->mm.gtt_total)) {
Chris Wilson654fc602010-05-27 13:18:21 +01002747 DRM_ERROR("Attempting to bind an object larger than the aperture\n");
2748 return -E2BIG;
2749 }
2750
Eric Anholt673a3942008-07-30 12:06:12 -07002751 search_free:
Daniel Vetter75e9e912010-11-04 17:11:09 +01002752 if (map_and_fenceable)
Daniel Vetter920afa72010-09-16 17:54:23 +02002753 free_space =
2754 drm_mm_search_free_in_range(&dev_priv->mm.gtt_space,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002755 size, alignment, 0,
Daniel Vetter920afa72010-09-16 17:54:23 +02002756 dev_priv->mm.gtt_mappable_end,
2757 0);
2758 else
2759 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002760 size, alignment, 0);
Daniel Vetter920afa72010-09-16 17:54:23 +02002761
2762 if (free_space != NULL) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01002763 if (map_and_fenceable)
Daniel Vetter920afa72010-09-16 17:54:23 +02002764 obj_priv->gtt_space =
2765 drm_mm_get_block_range_generic(free_space,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002766 size, alignment, 0,
Daniel Vetter920afa72010-09-16 17:54:23 +02002767 dev_priv->mm.gtt_mappable_end,
2768 0);
2769 else
2770 obj_priv->gtt_space =
Chris Wilsona00b10c2010-09-24 21:15:47 +01002771 drm_mm_get_block(free_space, size, alignment);
Daniel Vetter920afa72010-09-16 17:54:23 +02002772 }
Eric Anholt673a3942008-07-30 12:06:12 -07002773 if (obj_priv->gtt_space == NULL) {
2774 /* If the gtt is empty and we're still having trouble
2775 * fitting our object in, we're out of memory.
2776 */
Daniel Vetter75e9e912010-11-04 17:11:09 +01002777 ret = i915_gem_evict_something(dev, size, alignment,
2778 map_and_fenceable);
Chris Wilson97311292009-09-21 00:22:34 +01002779 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002780 return ret;
Chris Wilson97311292009-09-21 00:22:34 +01002781
Eric Anholt673a3942008-07-30 12:06:12 -07002782 goto search_free;
2783 }
2784
Chris Wilsone5281cc2010-10-28 13:45:36 +01002785 ret = i915_gem_object_get_pages_gtt(obj, gfpmask);
Eric Anholt673a3942008-07-30 12:06:12 -07002786 if (ret) {
2787 drm_mm_put_block(obj_priv->gtt_space);
2788 obj_priv->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002789
2790 if (ret == -ENOMEM) {
2791 /* first try to clear up some space from the GTT */
Chris Wilsona00b10c2010-09-24 21:15:47 +01002792 ret = i915_gem_evict_something(dev, size,
Daniel Vetter75e9e912010-11-04 17:11:09 +01002793 alignment,
2794 map_and_fenceable);
Chris Wilson07f73f62009-09-14 16:50:30 +01002795 if (ret) {
Chris Wilson07f73f62009-09-14 16:50:30 +01002796 /* now try to shrink everyone else */
Chris Wilson4bdadb92010-01-27 13:36:32 +00002797 if (gfpmask) {
2798 gfpmask = 0;
2799 goto search_free;
Chris Wilson07f73f62009-09-14 16:50:30 +01002800 }
2801
2802 return ret;
2803 }
2804
2805 goto search_free;
2806 }
2807
Eric Anholt673a3942008-07-30 12:06:12 -07002808 return ret;
2809 }
2810
Eric Anholt673a3942008-07-30 12:06:12 -07002811 /* Create an AGP memory structure pointing at our pages, and bind it
2812 * into the GTT.
2813 */
2814 obj_priv->agp_mem = drm_agp_bind_pages(dev,
Eric Anholt856fa192009-03-19 14:10:50 -07002815 obj_priv->pages,
Chris Wilson07f73f62009-09-14 16:50:30 +01002816 obj->size >> PAGE_SHIFT,
Chris Wilson9af90d12010-10-17 10:01:56 +01002817 obj_priv->gtt_space->start,
Keith Packardba1eb1d2008-10-14 19:55:10 -07002818 obj_priv->agp_type);
Eric Anholt673a3942008-07-30 12:06:12 -07002819 if (obj_priv->agp_mem == NULL) {
Chris Wilsone5281cc2010-10-28 13:45:36 +01002820 i915_gem_object_put_pages_gtt(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002821 drm_mm_put_block(obj_priv->gtt_space);
2822 obj_priv->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002823
Chris Wilsona00b10c2010-09-24 21:15:47 +01002824 ret = i915_gem_evict_something(dev, size,
Daniel Vetter75e9e912010-11-04 17:11:09 +01002825 alignment, map_and_fenceable);
Chris Wilson97311292009-09-21 00:22:34 +01002826 if (ret)
Chris Wilson07f73f62009-09-14 16:50:30 +01002827 return ret;
Chris Wilson07f73f62009-09-14 16:50:30 +01002828
2829 goto search_free;
Eric Anholt673a3942008-07-30 12:06:12 -07002830 }
Eric Anholt673a3942008-07-30 12:06:12 -07002831
Daniel Vetterfb7d5162010-10-01 22:05:20 +02002832 obj_priv->gtt_offset = obj_priv->gtt_space->start;
2833
Chris Wilsonbf1a1092010-08-07 11:01:20 +01002834 /* keep track of bounds object by adding it to the inactive list */
Chris Wilson69dc4982010-10-19 10:36:51 +01002835 list_add_tail(&obj_priv->mm_list, &dev_priv->mm.inactive_list);
Chris Wilsona00b10c2010-09-24 21:15:47 +01002836 i915_gem_info_add_gtt(dev_priv, obj_priv);
Chris Wilsonbf1a1092010-08-07 11:01:20 +01002837
Eric Anholt673a3942008-07-30 12:06:12 -07002838 /* Assert that the object is not currently in any GPU domain. As it
2839 * wasn't in the GTT, there shouldn't be any way it could have been in
2840 * a GPU cache
2841 */
Chris Wilson21d509e2009-06-06 09:46:02 +01002842 BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS);
2843 BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS);
Eric Anholt673a3942008-07-30 12:06:12 -07002844
Daniel Vetter75e9e912010-11-04 17:11:09 +01002845 trace_i915_gem_object_bind(obj, obj_priv->gtt_offset, map_and_fenceable);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002846
Daniel Vetter75e9e912010-11-04 17:11:09 +01002847 fenceable =
Chris Wilsona00b10c2010-09-24 21:15:47 +01002848 obj_priv->gtt_space->size == fence_size &&
2849 (obj_priv->gtt_space->start & (fence_alignment -1)) == 0;
2850
Daniel Vetter75e9e912010-11-04 17:11:09 +01002851 mappable =
Chris Wilsona00b10c2010-09-24 21:15:47 +01002852 obj_priv->gtt_offset + obj->size <= dev_priv->mm.gtt_mappable_end;
2853
Daniel Vetter75e9e912010-11-04 17:11:09 +01002854 obj_priv->map_and_fenceable = mappable && fenceable;
2855
Eric Anholt673a3942008-07-30 12:06:12 -07002856 return 0;
2857}
2858
2859void
2860i915_gem_clflush_object(struct drm_gem_object *obj)
2861{
Daniel Vetter23010e42010-03-08 13:35:02 +01002862 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002863
2864 /* If we don't have a page list set up, then we're not pinned
2865 * to GPU, and we can ignore the cache flush because it'll happen
2866 * again at bind time.
2867 */
Eric Anholt856fa192009-03-19 14:10:50 -07002868 if (obj_priv->pages == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002869 return;
2870
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002871 trace_i915_gem_object_clflush(obj);
Eric Anholtcfa16a02009-05-26 18:46:16 -07002872
Eric Anholt856fa192009-03-19 14:10:50 -07002873 drm_clflush_pages(obj_priv->pages, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002874}
2875
Eric Anholte47c68e2008-11-14 13:35:19 -08002876/** Flushes any GPU write domain for the object if it's dirty. */
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002877static int
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002878i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj,
2879 bool pipelined)
Eric Anholte47c68e2008-11-14 13:35:19 -08002880{
2881 struct drm_device *dev = obj->dev;
Eric Anholte47c68e2008-11-14 13:35:19 -08002882
2883 if ((obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002884 return 0;
Eric Anholte47c68e2008-11-14 13:35:19 -08002885
2886 /* Queue the GPU write cache flushing we need. */
Chris Wilsonc78ec302010-09-20 12:50:23 +01002887 i915_gem_flush_ring(dev, NULL,
Chris Wilson92204342010-09-18 11:02:01 +01002888 to_intel_bo(obj)->ring,
2889 0, obj->write_domain);
Chris Wilson48b956c2010-09-14 12:50:34 +01002890 BUG_ON(obj->write_domain);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002891
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002892 if (pipelined)
2893 return 0;
2894
Chris Wilson2cf34d72010-09-14 13:03:28 +01002895 return i915_gem_object_wait_rendering(obj, true);
Eric Anholte47c68e2008-11-14 13:35:19 -08002896}
2897
2898/** Flushes the GTT write domain for the object if it's dirty. */
2899static void
2900i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj)
2901{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002902 uint32_t old_write_domain;
2903
Eric Anholte47c68e2008-11-14 13:35:19 -08002904 if (obj->write_domain != I915_GEM_DOMAIN_GTT)
2905 return;
2906
2907 /* No actual flushing is required for the GTT write domain. Writes
2908 * to it immediately go to main memory as far as we know, so there's
2909 * no chipset flush. It also doesn't land in render cache.
2910 */
Chris Wilson4a684a42010-10-28 14:44:08 +01002911 i915_gem_release_mmap(obj);
2912
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002913 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002914 obj->write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002915
2916 trace_i915_gem_object_change_domain(obj,
2917 obj->read_domains,
2918 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002919}
2920
2921/** Flushes the CPU write domain for the object if it's dirty. */
2922static void
2923i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj)
2924{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002925 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002926
2927 if (obj->write_domain != I915_GEM_DOMAIN_CPU)
2928 return;
2929
2930 i915_gem_clflush_object(obj);
Daniel Vetter40ce6572010-11-05 18:12:18 +01002931 intel_gtt_chipset_flush();
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002932 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002933 obj->write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002934
2935 trace_i915_gem_object_change_domain(obj,
2936 obj->read_domains,
2937 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002938}
2939
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002940/**
2941 * Moves a single object to the GTT read, and possibly write domain.
2942 *
2943 * This function returns when the move is complete, including waiting on
2944 * flushes to occur.
2945 */
Jesse Barnes79e53942008-11-07 14:24:08 -08002946int
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002947i915_gem_object_set_to_gtt_domain(struct drm_gem_object *obj, int write)
2948{
Daniel Vetter23010e42010-03-08 13:35:02 +01002949 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002950 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002951 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002952
Eric Anholt02354392008-11-26 13:58:13 -08002953 /* Not valid to be called on unbound objects. */
2954 if (obj_priv->gtt_space == NULL)
2955 return -EINVAL;
2956
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002957 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002958 if (ret != 0)
2959 return ret;
2960
Chris Wilson72133422010-09-13 23:56:38 +01002961 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002962
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002963 if (write) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01002964 ret = i915_gem_object_wait_rendering(obj, true);
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002965 if (ret)
2966 return ret;
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002967 }
Eric Anholte47c68e2008-11-14 13:35:19 -08002968
2969 old_write_domain = obj->write_domain;
2970 old_read_domains = obj->read_domains;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002971
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002972 /* It should now be out of any other write domains, and we can update
2973 * the domain values for our changes.
2974 */
2975 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2976 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002977 if (write) {
Chris Wilson72133422010-09-13 23:56:38 +01002978 obj->read_domains = I915_GEM_DOMAIN_GTT;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002979 obj->write_domain = I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002980 obj_priv->dirty = 1;
2981 }
2982
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002983 trace_i915_gem_object_change_domain(obj,
2984 old_read_domains,
2985 old_write_domain);
2986
Eric Anholte47c68e2008-11-14 13:35:19 -08002987 return 0;
2988}
2989
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002990/*
2991 * Prepare buffer for display plane. Use uninterruptible for possible flush
2992 * wait, as in modesetting process we're not supposed to be interrupted.
2993 */
2994int
Chris Wilson48b956c2010-09-14 12:50:34 +01002995i915_gem_object_set_to_display_plane(struct drm_gem_object *obj,
2996 bool pipelined)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002997{
Daniel Vetter23010e42010-03-08 13:35:02 +01002998 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002999 uint32_t old_read_domains;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003000 int ret;
3001
3002 /* Not valid to be called on unbound objects. */
3003 if (obj_priv->gtt_space == NULL)
3004 return -EINVAL;
3005
Chris Wilsonced270f2010-09-26 22:47:46 +01003006 ret = i915_gem_object_flush_gpu_write_domain(obj, true);
Chris Wilson2dafb1e2010-06-07 14:03:05 +01003007 if (ret)
3008 return ret;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003009
Chris Wilsonced270f2010-09-26 22:47:46 +01003010 /* Currently, we are always called from an non-interruptible context. */
3011 if (!pipelined) {
3012 ret = i915_gem_object_wait_rendering(obj, false);
3013 if (ret)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003014 return ret;
3015 }
3016
Chris Wilsonb118c1e2010-05-27 13:18:14 +01003017 i915_gem_object_flush_cpu_write_domain(obj);
3018
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003019 old_read_domains = obj->read_domains;
Chris Wilsonc78ec302010-09-20 12:50:23 +01003020 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003021
3022 trace_i915_gem_object_change_domain(obj,
3023 old_read_domains,
Daniel Vetterba3d8d72010-02-11 22:37:04 +01003024 obj->write_domain);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003025
3026 return 0;
3027}
3028
Chris Wilson85345512010-11-13 09:49:11 +00003029int
3030i915_gem_object_flush_gpu(struct drm_i915_gem_object *obj,
3031 bool interruptible)
3032{
3033 if (!obj->active)
3034 return 0;
3035
3036 if (obj->base.write_domain & I915_GEM_GPU_DOMAINS)
3037 i915_gem_flush_ring(obj->base.dev, NULL, obj->ring,
3038 0, obj->base.write_domain);
3039
3040 return i915_gem_object_wait_rendering(&obj->base, interruptible);
3041}
3042
Eric Anholte47c68e2008-11-14 13:35:19 -08003043/**
3044 * Moves a single object to the CPU read, and possibly write domain.
3045 *
3046 * This function returns when the move is complete, including waiting on
3047 * flushes to occur.
3048 */
3049static int
3050i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj, int write)
3051{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003052 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003053 int ret;
3054
Daniel Vetterba3d8d72010-02-11 22:37:04 +01003055 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Eric Anholte47c68e2008-11-14 13:35:19 -08003056 if (ret != 0)
3057 return ret;
3058
3059 i915_gem_object_flush_gtt_write_domain(obj);
3060
3061 /* If we have a partially-valid cache of the object in the CPU,
3062 * finish invalidating it and free the per-page flags.
3063 */
3064 i915_gem_object_set_to_full_cpu_read_domain(obj);
3065
Chris Wilson72133422010-09-13 23:56:38 +01003066 if (write) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01003067 ret = i915_gem_object_wait_rendering(obj, true);
Chris Wilson72133422010-09-13 23:56:38 +01003068 if (ret)
3069 return ret;
3070 }
3071
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003072 old_write_domain = obj->write_domain;
3073 old_read_domains = obj->read_domains;
3074
Eric Anholte47c68e2008-11-14 13:35:19 -08003075 /* Flush the CPU cache if it's still invalid. */
3076 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
3077 i915_gem_clflush_object(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003078
3079 obj->read_domains |= I915_GEM_DOMAIN_CPU;
3080 }
3081
3082 /* It should now be out of any other write domains, and we can update
3083 * the domain values for our changes.
3084 */
3085 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
3086
3087 /* If we're writing through the CPU, then the GPU read domains will
3088 * need to be invalidated at next use.
3089 */
3090 if (write) {
Chris Wilsonc78ec302010-09-20 12:50:23 +01003091 obj->read_domains = I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003092 obj->write_domain = I915_GEM_DOMAIN_CPU;
3093 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003094
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003095 trace_i915_gem_object_change_domain(obj,
3096 old_read_domains,
3097 old_write_domain);
3098
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003099 return 0;
3100}
3101
Eric Anholt673a3942008-07-30 12:06:12 -07003102/*
3103 * Set the next domain for the specified object. This
3104 * may not actually perform the necessary flushing/invaliding though,
3105 * as that may want to be batched with other set_domain operations
3106 *
3107 * This is (we hope) the only really tricky part of gem. The goal
3108 * is fairly simple -- track which caches hold bits of the object
3109 * and make sure they remain coherent. A few concrete examples may
3110 * help to explain how it works. For shorthand, we use the notation
3111 * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
3112 * a pair of read and write domain masks.
3113 *
3114 * Case 1: the batch buffer
3115 *
3116 * 1. Allocated
3117 * 2. Written by CPU
3118 * 3. Mapped to GTT
3119 * 4. Read by GPU
3120 * 5. Unmapped from GTT
3121 * 6. Freed
3122 *
3123 * Let's take these a step at a time
3124 *
3125 * 1. Allocated
3126 * Pages allocated from the kernel may still have
3127 * cache contents, so we set them to (CPU, CPU) always.
3128 * 2. Written by CPU (using pwrite)
3129 * The pwrite function calls set_domain (CPU, CPU) and
3130 * this function does nothing (as nothing changes)
3131 * 3. Mapped by GTT
3132 * This function asserts that the object is not
3133 * currently in any GPU-based read or write domains
3134 * 4. Read by GPU
3135 * i915_gem_execbuffer calls set_domain (COMMAND, 0).
3136 * As write_domain is zero, this function adds in the
3137 * current read domains (CPU+COMMAND, 0).
3138 * flush_domains is set to CPU.
3139 * invalidate_domains is set to COMMAND
3140 * clflush is run to get data out of the CPU caches
3141 * then i915_dev_set_domain calls i915_gem_flush to
3142 * emit an MI_FLUSH and drm_agp_chipset_flush
3143 * 5. Unmapped from GTT
3144 * i915_gem_object_unbind calls set_domain (CPU, CPU)
3145 * flush_domains and invalidate_domains end up both zero
3146 * so no flushing/invalidating happens
3147 * 6. Freed
3148 * yay, done
3149 *
3150 * Case 2: The shared render buffer
3151 *
3152 * 1. Allocated
3153 * 2. Mapped to GTT
3154 * 3. Read/written by GPU
3155 * 4. set_domain to (CPU,CPU)
3156 * 5. Read/written by CPU
3157 * 6. Read/written by GPU
3158 *
3159 * 1. Allocated
3160 * Same as last example, (CPU, CPU)
3161 * 2. Mapped to GTT
3162 * Nothing changes (assertions find that it is not in the GPU)
3163 * 3. Read/written by GPU
3164 * execbuffer calls set_domain (RENDER, RENDER)
3165 * flush_domains gets CPU
3166 * invalidate_domains gets GPU
3167 * clflush (obj)
3168 * MI_FLUSH and drm_agp_chipset_flush
3169 * 4. set_domain (CPU, CPU)
3170 * flush_domains gets GPU
3171 * invalidate_domains gets CPU
3172 * wait_rendering (obj) to make sure all drawing is complete.
3173 * This will include an MI_FLUSH to get the data from GPU
3174 * to memory
3175 * clflush (obj) to invalidate the CPU cache
3176 * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
3177 * 5. Read/written by CPU
3178 * cache lines are loaded and dirtied
3179 * 6. Read written by GPU
3180 * Same as last GPU access
3181 *
3182 * Case 3: The constant buffer
3183 *
3184 * 1. Allocated
3185 * 2. Written by CPU
3186 * 3. Read by GPU
3187 * 4. Updated (written) by CPU again
3188 * 5. Read by GPU
3189 *
3190 * 1. Allocated
3191 * (CPU, CPU)
3192 * 2. Written by CPU
3193 * (CPU, CPU)
3194 * 3. Read by GPU
3195 * (CPU+RENDER, 0)
3196 * flush_domains = CPU
3197 * invalidate_domains = RENDER
3198 * clflush (obj)
3199 * MI_FLUSH
3200 * drm_agp_chipset_flush
3201 * 4. Updated (written) by CPU again
3202 * (CPU, CPU)
3203 * flush_domains = 0 (no previous write domain)
3204 * invalidate_domains = 0 (no new read domains)
3205 * 5. Read by GPU
3206 * (CPU+RENDER, 0)
3207 * flush_domains = CPU
3208 * invalidate_domains = RENDER
3209 * clflush (obj)
3210 * MI_FLUSH
3211 * drm_agp_chipset_flush
3212 */
Keith Packardc0d90822008-11-20 23:11:08 -08003213static void
Chris Wilsonb6651452010-10-23 10:15:06 +01003214i915_gem_object_set_to_gpu_domain(struct drm_gem_object *obj,
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003215 struct intel_ring_buffer *ring,
3216 struct change_domains *cd)
Eric Anholt673a3942008-07-30 12:06:12 -07003217{
Daniel Vetter23010e42010-03-08 13:35:02 +01003218 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003219 uint32_t invalidate_domains = 0;
3220 uint32_t flush_domains = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07003221
Eric Anholt673a3942008-07-30 12:06:12 -07003222 /*
3223 * If the object isn't moving to a new write domain,
3224 * let the object stay in multiple read domains
3225 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003226 if (obj->pending_write_domain == 0)
3227 obj->pending_read_domains |= obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07003228
3229 /*
3230 * Flush the current write domain if
3231 * the new read domains don't match. Invalidate
3232 * any read domains which differ from the old
3233 * write domain
3234 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003235 if (obj->write_domain &&
Chris Wilson13b29282010-11-01 12:22:48 +00003236 (obj->write_domain != obj->pending_read_domains ||
3237 obj_priv->ring != ring)) {
Eric Anholt673a3942008-07-30 12:06:12 -07003238 flush_domains |= obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08003239 invalidate_domains |=
3240 obj->pending_read_domains & ~obj->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07003241 }
3242 /*
3243 * Invalidate any read caches which may have
3244 * stale data. That is, any new read domains.
3245 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003246 invalidate_domains |= obj->pending_read_domains & ~obj->read_domains;
Chris Wilson3d2a8122010-09-29 11:39:53 +01003247 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU)
Eric Anholt673a3942008-07-30 12:06:12 -07003248 i915_gem_clflush_object(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003249
Chris Wilson4a684a42010-10-28 14:44:08 +01003250 /* blow away mappings if mapped through GTT */
3251 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_GTT)
3252 i915_gem_release_mmap(obj);
3253
Eric Anholtefbeed92009-02-19 14:54:51 -08003254 /* The actual obj->write_domain will be updated with
3255 * pending_write_domain after we emit the accumulated flush for all
3256 * of our domain changes in execbuffers (which clears objects'
3257 * write_domains). So if we have a current write domain that we
3258 * aren't changing, set pending_write_domain to that.
3259 */
3260 if (flush_domains == 0 && obj->pending_write_domain == 0)
3261 obj->pending_write_domain = obj->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07003262
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003263 cd->invalidate_domains |= invalidate_domains;
3264 cd->flush_domains |= flush_domains;
Chris Wilsonb6651452010-10-23 10:15:06 +01003265 if (flush_domains & I915_GEM_GPU_DOMAINS)
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003266 cd->flush_rings |= obj_priv->ring->id;
Chris Wilsonb6651452010-10-23 10:15:06 +01003267 if (invalidate_domains & I915_GEM_GPU_DOMAINS)
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003268 cd->flush_rings |= ring->id;
Eric Anholt673a3942008-07-30 12:06:12 -07003269}
3270
3271/**
Eric Anholte47c68e2008-11-14 13:35:19 -08003272 * Moves the object from a partially CPU read to a full one.
Eric Anholt673a3942008-07-30 12:06:12 -07003273 *
Eric Anholte47c68e2008-11-14 13:35:19 -08003274 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
3275 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
3276 */
3277static void
3278i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj)
3279{
Daniel Vetter23010e42010-03-08 13:35:02 +01003280 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003281
3282 if (!obj_priv->page_cpu_valid)
3283 return;
3284
3285 /* If we're partially in the CPU read domain, finish moving it in.
3286 */
3287 if (obj->read_domains & I915_GEM_DOMAIN_CPU) {
3288 int i;
3289
3290 for (i = 0; i <= (obj->size - 1) / PAGE_SIZE; i++) {
3291 if (obj_priv->page_cpu_valid[i])
3292 continue;
Eric Anholt856fa192009-03-19 14:10:50 -07003293 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholte47c68e2008-11-14 13:35:19 -08003294 }
Eric Anholte47c68e2008-11-14 13:35:19 -08003295 }
3296
3297 /* Free the page_cpu_valid mappings which are now stale, whether
3298 * or not we've got I915_GEM_DOMAIN_CPU.
3299 */
Eric Anholt9a298b22009-03-24 12:23:04 -07003300 kfree(obj_priv->page_cpu_valid);
Eric Anholte47c68e2008-11-14 13:35:19 -08003301 obj_priv->page_cpu_valid = NULL;
3302}
3303
3304/**
3305 * Set the CPU read domain on a range of the object.
3306 *
3307 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
3308 * not entirely valid. The page_cpu_valid member of the object flags which
3309 * pages have been flushed, and will be respected by
3310 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
3311 * of the whole object.
3312 *
3313 * This function returns when the move is complete, including waiting on
3314 * flushes to occur.
Eric Anholt673a3942008-07-30 12:06:12 -07003315 */
3316static int
Eric Anholte47c68e2008-11-14 13:35:19 -08003317i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
3318 uint64_t offset, uint64_t size)
Eric Anholt673a3942008-07-30 12:06:12 -07003319{
Daniel Vetter23010e42010-03-08 13:35:02 +01003320 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003321 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003322 int i, ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003323
Eric Anholte47c68e2008-11-14 13:35:19 -08003324 if (offset == 0 && size == obj->size)
3325 return i915_gem_object_set_to_cpu_domain(obj, 0);
3326
Daniel Vetterba3d8d72010-02-11 22:37:04 +01003327 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Eric Anholte47c68e2008-11-14 13:35:19 -08003328 if (ret != 0)
3329 return ret;
3330 i915_gem_object_flush_gtt_write_domain(obj);
3331
3332 /* If we're already fully in the CPU read domain, we're done. */
3333 if (obj_priv->page_cpu_valid == NULL &&
3334 (obj->read_domains & I915_GEM_DOMAIN_CPU) != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07003335 return 0;
3336
Eric Anholte47c68e2008-11-14 13:35:19 -08003337 /* Otherwise, create/clear the per-page CPU read domain flag if we're
3338 * newly adding I915_GEM_DOMAIN_CPU
3339 */
Eric Anholt673a3942008-07-30 12:06:12 -07003340 if (obj_priv->page_cpu_valid == NULL) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003341 obj_priv->page_cpu_valid = kzalloc(obj->size / PAGE_SIZE,
3342 GFP_KERNEL);
Eric Anholte47c68e2008-11-14 13:35:19 -08003343 if (obj_priv->page_cpu_valid == NULL)
3344 return -ENOMEM;
3345 } else if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0)
3346 memset(obj_priv->page_cpu_valid, 0, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07003347
3348 /* Flush the cache on any pages that are still invalid from the CPU's
3349 * perspective.
3350 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003351 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
3352 i++) {
Eric Anholt673a3942008-07-30 12:06:12 -07003353 if (obj_priv->page_cpu_valid[i])
3354 continue;
3355
Eric Anholt856fa192009-03-19 14:10:50 -07003356 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07003357
3358 obj_priv->page_cpu_valid[i] = 1;
3359 }
3360
Eric Anholte47c68e2008-11-14 13:35:19 -08003361 /* It should now be out of any other write domains, and we can update
3362 * the domain values for our changes.
3363 */
3364 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
3365
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003366 old_read_domains = obj->read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003367 obj->read_domains |= I915_GEM_DOMAIN_CPU;
3368
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003369 trace_i915_gem_object_change_domain(obj,
3370 old_read_domains,
3371 obj->write_domain);
3372
Eric Anholt673a3942008-07-30 12:06:12 -07003373 return 0;
3374}
3375
Eric Anholt673a3942008-07-30 12:06:12 -07003376static int
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003377i915_gem_execbuffer_relocate_entry(struct drm_i915_gem_object *obj,
3378 struct drm_file *file_priv,
3379 struct drm_i915_gem_exec_object2 *entry,
3380 struct drm_i915_gem_relocation_entry *reloc)
Eric Anholt673a3942008-07-30 12:06:12 -07003381{
Chris Wilson9af90d12010-10-17 10:01:56 +01003382 struct drm_device *dev = obj->base.dev;
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003383 struct drm_gem_object *target_obj;
3384 uint32_t target_offset;
3385 int ret = -EINVAL;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003386
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003387 target_obj = drm_gem_object_lookup(dev, file_priv,
3388 reloc->target_handle);
3389 if (target_obj == NULL)
3390 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07003391
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003392 target_offset = to_intel_bo(target_obj)->gtt_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003393
Chris Wilson8542a0b2009-09-09 21:15:15 +01003394#if WATCH_RELOC
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003395 DRM_INFO("%s: obj %p offset %08x target %d "
3396 "read %08x write %08x gtt %08x "
3397 "presumed %08x delta %08x\n",
3398 __func__,
3399 obj,
3400 (int) reloc->offset,
3401 (int) reloc->target_handle,
3402 (int) reloc->read_domains,
3403 (int) reloc->write_domain,
3404 (int) target_offset,
3405 (int) reloc->presumed_offset,
3406 reloc->delta);
Chris Wilson8542a0b2009-09-09 21:15:15 +01003407#endif
3408
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003409 /* The target buffer should have appeared before us in the
3410 * exec_object list, so it should have a GTT space bound by now.
3411 */
3412 if (target_offset == 0) {
3413 DRM_ERROR("No GTT space found for object %d\n",
3414 reloc->target_handle);
3415 goto err;
Eric Anholt673a3942008-07-30 12:06:12 -07003416 }
3417
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003418 /* Validate that the target is in a valid r/w GPU domain */
3419 if (reloc->write_domain & (reloc->write_domain - 1)) {
3420 DRM_ERROR("reloc with multiple write domains: "
3421 "obj %p target %d offset %d "
3422 "read %08x write %08x",
3423 obj, reloc->target_handle,
3424 (int) reloc->offset,
3425 reloc->read_domains,
3426 reloc->write_domain);
3427 goto err;
3428 }
3429 if (reloc->write_domain & I915_GEM_DOMAIN_CPU ||
3430 reloc->read_domains & I915_GEM_DOMAIN_CPU) {
3431 DRM_ERROR("reloc with read/write CPU domains: "
3432 "obj %p target %d offset %d "
3433 "read %08x write %08x",
3434 obj, reloc->target_handle,
3435 (int) reloc->offset,
3436 reloc->read_domains,
3437 reloc->write_domain);
3438 goto err;
3439 }
3440 if (reloc->write_domain && target_obj->pending_write_domain &&
3441 reloc->write_domain != target_obj->pending_write_domain) {
3442 DRM_ERROR("Write domain conflict: "
3443 "obj %p target %d offset %d "
3444 "new %08x old %08x\n",
3445 obj, reloc->target_handle,
3446 (int) reloc->offset,
3447 reloc->write_domain,
3448 target_obj->pending_write_domain);
3449 goto err;
3450 }
3451
3452 target_obj->pending_read_domains |= reloc->read_domains;
3453 target_obj->pending_write_domain |= reloc->write_domain;
3454
3455 /* If the relocation already has the right value in it, no
3456 * more work needs to be done.
3457 */
3458 if (target_offset == reloc->presumed_offset)
3459 goto out;
3460
3461 /* Check that the relocation address is valid... */
3462 if (reloc->offset > obj->base.size - 4) {
3463 DRM_ERROR("Relocation beyond object bounds: "
3464 "obj %p target %d offset %d size %d.\n",
3465 obj, reloc->target_handle,
3466 (int) reloc->offset,
3467 (int) obj->base.size);
3468 goto err;
3469 }
3470 if (reloc->offset & 3) {
3471 DRM_ERROR("Relocation not 4-byte aligned: "
3472 "obj %p target %d offset %d.\n",
3473 obj, reloc->target_handle,
3474 (int) reloc->offset);
3475 goto err;
3476 }
3477
3478 /* and points to somewhere within the target object. */
3479 if (reloc->delta >= target_obj->size) {
3480 DRM_ERROR("Relocation beyond target object bounds: "
3481 "obj %p target %d delta %d size %d.\n",
3482 obj, reloc->target_handle,
3483 (int) reloc->delta,
3484 (int) target_obj->size);
3485 goto err;
3486 }
3487
3488 reloc->delta += target_offset;
3489 if (obj->base.write_domain == I915_GEM_DOMAIN_CPU) {
3490 uint32_t page_offset = reloc->offset & ~PAGE_MASK;
3491 char *vaddr;
3492
3493 vaddr = kmap_atomic(obj->pages[reloc->offset >> PAGE_SHIFT]);
3494 *(uint32_t *)(vaddr + page_offset) = reloc->delta;
3495 kunmap_atomic(vaddr);
3496 } else {
3497 struct drm_i915_private *dev_priv = dev->dev_private;
3498 uint32_t __iomem *reloc_entry;
3499 void __iomem *reloc_page;
3500
3501 ret = i915_gem_object_set_to_gtt_domain(&obj->base, 1);
3502 if (ret)
3503 goto err;
3504
3505 /* Map the page containing the relocation we're going to perform. */
3506 reloc->offset += obj->gtt_offset;
3507 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
3508 reloc->offset & PAGE_MASK);
3509 reloc_entry = (uint32_t __iomem *)
3510 (reloc_page + (reloc->offset & ~PAGE_MASK));
3511 iowrite32(reloc->delta, reloc_entry);
3512 io_mapping_unmap_atomic(reloc_page);
3513 }
3514
3515 /* and update the user's relocation entry */
3516 reloc->presumed_offset = target_offset;
3517
3518out:
3519 ret = 0;
3520err:
Chris Wilson9af90d12010-10-17 10:01:56 +01003521 drm_gem_object_unreference(target_obj);
3522 return ret;
3523}
3524
3525static int
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003526i915_gem_execbuffer_relocate_object(struct drm_i915_gem_object *obj,
3527 struct drm_file *file_priv,
3528 struct drm_i915_gem_exec_object2 *entry)
3529{
3530 struct drm_i915_gem_relocation_entry __user *user_relocs;
3531 int i, ret;
3532
3533 user_relocs = (void __user *)(uintptr_t)entry->relocs_ptr;
3534 for (i = 0; i < entry->relocation_count; i++) {
3535 struct drm_i915_gem_relocation_entry reloc;
3536
3537 if (__copy_from_user_inatomic(&reloc,
3538 user_relocs+i,
3539 sizeof(reloc)))
3540 return -EFAULT;
3541
3542 ret = i915_gem_execbuffer_relocate_entry(obj, file_priv, entry, &reloc);
3543 if (ret)
3544 return ret;
3545
3546 if (__copy_to_user_inatomic(&user_relocs[i].presumed_offset,
3547 &reloc.presumed_offset,
3548 sizeof(reloc.presumed_offset)))
3549 return -EFAULT;
3550 }
3551
3552 return 0;
3553}
3554
3555static int
3556i915_gem_execbuffer_relocate_object_slow(struct drm_i915_gem_object *obj,
3557 struct drm_file *file_priv,
3558 struct drm_i915_gem_exec_object2 *entry,
3559 struct drm_i915_gem_relocation_entry *relocs)
3560{
3561 int i, ret;
3562
3563 for (i = 0; i < entry->relocation_count; i++) {
3564 ret = i915_gem_execbuffer_relocate_entry(obj, file_priv, entry, &relocs[i]);
3565 if (ret)
3566 return ret;
3567 }
3568
3569 return 0;
3570}
3571
3572static int
3573i915_gem_execbuffer_relocate(struct drm_device *dev,
3574 struct drm_file *file,
3575 struct drm_gem_object **object_list,
3576 struct drm_i915_gem_exec_object2 *exec_list,
3577 int count)
3578{
3579 int i, ret;
3580
3581 for (i = 0; i < count; i++) {
3582 struct drm_i915_gem_object *obj = to_intel_bo(object_list[i]);
3583 obj->base.pending_read_domains = 0;
3584 obj->base.pending_write_domain = 0;
3585 ret = i915_gem_execbuffer_relocate_object(obj, file,
3586 &exec_list[i]);
3587 if (ret)
3588 return ret;
3589 }
3590
3591 return 0;
3592}
3593
3594static int
3595i915_gem_execbuffer_reserve(struct drm_device *dev,
3596 struct drm_file *file,
3597 struct drm_gem_object **object_list,
3598 struct drm_i915_gem_exec_object2 *exec_list,
3599 int count)
Chris Wilson9af90d12010-10-17 10:01:56 +01003600{
3601 struct drm_i915_private *dev_priv = dev->dev_private;
3602 int ret, i, retry;
3603
3604 /* attempt to pin all of the buffers into the GTT */
Chris Wilson5eac3ab2010-10-31 08:49:47 +00003605 retry = 0;
3606 do {
Chris Wilson9af90d12010-10-17 10:01:56 +01003607 ret = 0;
3608 for (i = 0; i < count; i++) {
3609 struct drm_i915_gem_exec_object2 *entry = &exec_list[i];
Daniel Vetter16e809a2010-09-16 19:37:04 +02003610 struct drm_i915_gem_object *obj = to_intel_bo(object_list[i]);
Chris Wilson9af90d12010-10-17 10:01:56 +01003611 bool need_fence =
3612 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
3613 obj->tiling_mode != I915_TILING_NONE;
3614
Daniel Vetter16e809a2010-09-16 19:37:04 +02003615 /* g33/pnv can't fence buffers in the unmappable part */
3616 bool need_mappable =
3617 entry->relocation_count ? true : need_fence;
3618
Chris Wilson9af90d12010-10-17 10:01:56 +01003619 /* Check fence reg constraints and rebind if necessary */
Daniel Vetter75e9e912010-11-04 17:11:09 +01003620 if (need_mappable && !obj->map_and_fenceable) {
Chris Wilson9af90d12010-10-17 10:01:56 +01003621 ret = i915_gem_object_unbind(&obj->base);
3622 if (ret)
3623 break;
3624 }
3625
Daniel Vetter920afa72010-09-16 17:54:23 +02003626 ret = i915_gem_object_pin(&obj->base,
Daniel Vetter16e809a2010-09-16 19:37:04 +02003627 entry->alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01003628 need_mappable);
Chris Wilson9af90d12010-10-17 10:01:56 +01003629 if (ret)
3630 break;
3631
3632 /*
3633 * Pre-965 chips need a fence register set up in order
3634 * to properly handle blits to/from tiled surfaces.
3635 */
3636 if (need_fence) {
3637 ret = i915_gem_object_get_fence_reg(&obj->base, true);
3638 if (ret) {
3639 i915_gem_object_unpin(&obj->base);
3640 break;
3641 }
3642
3643 dev_priv->fence_regs[obj->fence_reg].gpu = true;
3644 }
3645
3646 entry->offset = obj->gtt_offset;
3647 }
3648
3649 while (i--)
3650 i915_gem_object_unpin(object_list[i]);
3651
Chris Wilson5eac3ab2010-10-31 08:49:47 +00003652 if (ret != -ENOSPC || retry > 1)
Chris Wilson9af90d12010-10-17 10:01:56 +01003653 return ret;
3654
Chris Wilson5eac3ab2010-10-31 08:49:47 +00003655 /* First attempt, just clear anything that is purgeable.
3656 * Second attempt, clear the entire GTT.
3657 */
3658 ret = i915_gem_evict_everything(dev, retry == 0);
Chris Wilson9af90d12010-10-17 10:01:56 +01003659 if (ret)
3660 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003661
Chris Wilson5eac3ab2010-10-31 08:49:47 +00003662 retry++;
3663 } while (1);
Eric Anholt673a3942008-07-30 12:06:12 -07003664}
3665
Chris Wilson13b29282010-11-01 12:22:48 +00003666static int
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003667i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
3668 struct drm_file *file,
3669 struct drm_gem_object **object_list,
3670 struct drm_i915_gem_exec_object2 *exec_list,
3671 int count)
3672{
3673 struct drm_i915_gem_relocation_entry *reloc;
3674 int i, total, ret;
3675
3676 for (i = 0; i < count; i++) {
3677 struct drm_i915_gem_object *obj = to_intel_bo(object_list[i]);
3678 obj->in_execbuffer = false;
3679 }
3680
3681 mutex_unlock(&dev->struct_mutex);
3682
3683 total = 0;
3684 for (i = 0; i < count; i++)
3685 total += exec_list[i].relocation_count;
3686
3687 reloc = drm_malloc_ab(total, sizeof(*reloc));
3688 if (reloc == NULL) {
3689 mutex_lock(&dev->struct_mutex);
3690 return -ENOMEM;
3691 }
3692
3693 total = 0;
3694 for (i = 0; i < count; i++) {
3695 struct drm_i915_gem_relocation_entry __user *user_relocs;
3696
3697 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3698
3699 if (copy_from_user(reloc+total, user_relocs,
3700 exec_list[i].relocation_count *
3701 sizeof(*reloc))) {
3702 ret = -EFAULT;
3703 mutex_lock(&dev->struct_mutex);
3704 goto err;
3705 }
3706
3707 total += exec_list[i].relocation_count;
3708 }
3709
3710 ret = i915_mutex_lock_interruptible(dev);
3711 if (ret) {
3712 mutex_lock(&dev->struct_mutex);
3713 goto err;
3714 }
3715
3716 ret = i915_gem_execbuffer_reserve(dev, file,
3717 object_list, exec_list,
3718 count);
3719 if (ret)
3720 goto err;
3721
3722 total = 0;
3723 for (i = 0; i < count; i++) {
3724 struct drm_i915_gem_object *obj = to_intel_bo(object_list[i]);
3725 obj->base.pending_read_domains = 0;
3726 obj->base.pending_write_domain = 0;
3727 ret = i915_gem_execbuffer_relocate_object_slow(obj, file,
3728 &exec_list[i],
3729 reloc + total);
3730 if (ret)
3731 goto err;
3732
3733 total += exec_list[i].relocation_count;
3734 }
3735
3736 /* Leave the user relocations as are, this is the painfully slow path,
3737 * and we want to avoid the complication of dropping the lock whilst
3738 * having buffers reserved in the aperture and so causing spurious
3739 * ENOSPC for random operations.
3740 */
3741
3742err:
3743 drm_free_large(reloc);
3744 return ret;
3745}
3746
3747static int
Chris Wilson13b29282010-11-01 12:22:48 +00003748i915_gem_execbuffer_move_to_gpu(struct drm_device *dev,
3749 struct drm_file *file,
3750 struct intel_ring_buffer *ring,
3751 struct drm_gem_object **objects,
3752 int count)
3753{
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003754 struct change_domains cd;
Chris Wilson13b29282010-11-01 12:22:48 +00003755 int ret, i;
3756
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003757 cd.invalidate_domains = 0;
3758 cd.flush_domains = 0;
3759 cd.flush_rings = 0;
Chris Wilson13b29282010-11-01 12:22:48 +00003760 for (i = 0; i < count; i++)
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003761 i915_gem_object_set_to_gpu_domain(objects[i], ring, &cd);
Chris Wilson13b29282010-11-01 12:22:48 +00003762
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003763 if (cd.invalidate_domains | cd.flush_domains) {
Chris Wilson13b29282010-11-01 12:22:48 +00003764#if WATCH_EXEC
3765 DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
3766 __func__,
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003767 cd.invalidate_domains,
3768 cd.flush_domains);
Chris Wilson13b29282010-11-01 12:22:48 +00003769#endif
3770 i915_gem_flush(dev, file,
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003771 cd.invalidate_domains,
3772 cd.flush_domains,
3773 cd.flush_rings);
Chris Wilson13b29282010-11-01 12:22:48 +00003774 }
3775
3776 for (i = 0; i < count; i++) {
3777 struct drm_i915_gem_object *obj = to_intel_bo(objects[i]);
3778 /* XXX replace with semaphores */
3779 if (obj->ring && ring != obj->ring) {
3780 ret = i915_gem_object_wait_rendering(&obj->base, true);
3781 if (ret)
3782 return ret;
3783 }
3784 }
3785
3786 return 0;
3787}
3788
Eric Anholt673a3942008-07-30 12:06:12 -07003789/* Throttle our rendering by waiting until the ring has completed our requests
3790 * emitted over 20 msec ago.
3791 *
Eric Anholtb9624422009-06-03 07:27:35 +00003792 * Note that if we were to use the current jiffies each time around the loop,
3793 * we wouldn't escape the function with any frames outstanding if the time to
3794 * render a frame was over 20ms.
3795 *
Eric Anholt673a3942008-07-30 12:06:12 -07003796 * This should get us reasonable parallelism between CPU and GPU but also
3797 * relatively low latency when blocking on a particular request to finish.
3798 */
3799static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003800i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003801{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003802 struct drm_i915_private *dev_priv = dev->dev_private;
3803 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00003804 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003805 struct drm_i915_gem_request *request;
3806 struct intel_ring_buffer *ring = NULL;
3807 u32 seqno = 0;
3808 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003809
Chris Wilson1c255952010-09-26 11:03:27 +01003810 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003811 list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
Eric Anholtb9624422009-06-03 07:27:35 +00003812 if (time_after_eq(request->emitted_jiffies, recent_enough))
3813 break;
3814
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003815 ring = request->ring;
3816 seqno = request->seqno;
Eric Anholtb9624422009-06-03 07:27:35 +00003817 }
Chris Wilson1c255952010-09-26 11:03:27 +01003818 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003819
3820 if (seqno == 0)
3821 return 0;
3822
3823 ret = 0;
Chris Wilson78501ea2010-10-27 12:18:21 +01003824 if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) {
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003825 /* And wait for the seqno passing without holding any locks and
3826 * causing extra latency for others. This is safe as the irq
3827 * generation is designed to be run atomically and so is
3828 * lockless.
3829 */
Chris Wilson78501ea2010-10-27 12:18:21 +01003830 ring->user_irq_get(ring);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003831 ret = wait_event_interruptible(ring->irq_queue,
Chris Wilson78501ea2010-10-27 12:18:21 +01003832 i915_seqno_passed(ring->get_seqno(ring), seqno)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003833 || atomic_read(&dev_priv->mm.wedged));
Chris Wilson78501ea2010-10-27 12:18:21 +01003834 ring->user_irq_put(ring);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003835
3836 if (ret == 0 && atomic_read(&dev_priv->mm.wedged))
3837 ret = -EIO;
3838 }
3839
3840 if (ret == 0)
3841 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
Eric Anholtb9624422009-06-03 07:27:35 +00003842
Eric Anholt673a3942008-07-30 12:06:12 -07003843 return ret;
3844}
3845
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003846static int
Chris Wilson2549d6c2010-10-14 12:10:41 +01003847i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec,
3848 uint64_t exec_offset)
Chris Wilson83d60792009-06-06 09:45:57 +01003849{
3850 uint32_t exec_start, exec_len;
3851
3852 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
3853 exec_len = (uint32_t) exec->batch_len;
3854
3855 if ((exec_start | exec_len) & 0x7)
3856 return -EINVAL;
3857
3858 if (!exec_start)
3859 return -EINVAL;
3860
3861 return 0;
3862}
3863
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003864static int
Chris Wilson2549d6c2010-10-14 12:10:41 +01003865validate_exec_list(struct drm_i915_gem_exec_object2 *exec,
3866 int count)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003867{
Chris Wilson2549d6c2010-10-14 12:10:41 +01003868 int i;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003869
Chris Wilson2549d6c2010-10-14 12:10:41 +01003870 for (i = 0; i < count; i++) {
3871 char __user *ptr = (char __user *)(uintptr_t)exec[i].relocs_ptr;
Chris Wilsond1d78832010-11-21 09:23:48 +00003872 int length; /* limited by fault_in_pages_readable() */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003873
Chris Wilsond1d78832010-11-21 09:23:48 +00003874 /* First check for malicious input causing overflow */
3875 if (exec[i].relocation_count >
3876 INT_MAX / sizeof(struct drm_i915_gem_relocation_entry))
3877 return -EINVAL;
3878
3879 length = exec[i].relocation_count *
3880 sizeof(struct drm_i915_gem_relocation_entry);
Chris Wilson2549d6c2010-10-14 12:10:41 +01003881 if (!access_ok(VERIFY_READ, ptr, length))
3882 return -EFAULT;
3883
Chris Wilsonb5dc6082010-10-20 20:59:57 +01003884 /* we may also need to update the presumed offsets */
3885 if (!access_ok(VERIFY_WRITE, ptr, length))
3886 return -EFAULT;
3887
Chris Wilson2549d6c2010-10-14 12:10:41 +01003888 if (fault_in_pages_readable(ptr, length))
3889 return -EFAULT;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003890 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003891
Chris Wilson2549d6c2010-10-14 12:10:41 +01003892 return 0;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003893}
3894
Chris Wilson2549d6c2010-10-14 12:10:41 +01003895static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003896i915_gem_do_execbuffer(struct drm_device *dev, void *data,
Chris Wilson9af90d12010-10-17 10:01:56 +01003897 struct drm_file *file,
Jesse Barnes76446ca2009-12-17 22:05:42 -05003898 struct drm_i915_gem_execbuffer2 *args,
3899 struct drm_i915_gem_exec_object2 *exec_list)
Eric Anholt673a3942008-07-30 12:06:12 -07003900{
3901 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003902 struct drm_gem_object **object_list = NULL;
3903 struct drm_gem_object *batch_obj;
Eric Anholt201361a2009-03-11 12:30:04 -07003904 struct drm_clip_rect *cliprects = NULL;
Chris Wilson8dc5d142010-08-12 12:36:12 +01003905 struct drm_i915_gem_request *request = NULL;
Chris Wilson9af90d12010-10-17 10:01:56 +01003906 int ret, i, flips;
Eric Anholt673a3942008-07-30 12:06:12 -07003907 uint64_t exec_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003908
Zou Nan hai852835f2010-05-21 09:08:56 +08003909 struct intel_ring_buffer *ring = NULL;
3910
Chris Wilson30dbf0c2010-09-25 10:19:17 +01003911 ret = i915_gem_check_is_wedged(dev);
3912 if (ret)
3913 return ret;
3914
Chris Wilson2549d6c2010-10-14 12:10:41 +01003915 ret = validate_exec_list(exec_list, args->buffer_count);
3916 if (ret)
3917 return ret;
3918
Eric Anholt673a3942008-07-30 12:06:12 -07003919#if WATCH_EXEC
3920 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3921 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3922#endif
Chris Wilson549f7362010-10-19 11:19:32 +01003923 switch (args->flags & I915_EXEC_RING_MASK) {
3924 case I915_EXEC_DEFAULT:
3925 case I915_EXEC_RENDER:
3926 ring = &dev_priv->render_ring;
3927 break;
3928 case I915_EXEC_BSD:
Zou Nan haid1b851f2010-05-21 09:08:57 +08003929 if (!HAS_BSD(dev)) {
Chris Wilson549f7362010-10-19 11:19:32 +01003930 DRM_ERROR("execbuf with invalid ring (BSD)\n");
Zou Nan haid1b851f2010-05-21 09:08:57 +08003931 return -EINVAL;
3932 }
3933 ring = &dev_priv->bsd_ring;
Chris Wilson549f7362010-10-19 11:19:32 +01003934 break;
3935 case I915_EXEC_BLT:
3936 if (!HAS_BLT(dev)) {
3937 DRM_ERROR("execbuf with invalid ring (BLT)\n");
3938 return -EINVAL;
3939 }
3940 ring = &dev_priv->blt_ring;
3941 break;
3942 default:
3943 DRM_ERROR("execbuf with unknown ring: %d\n",
3944 (int)(args->flags & I915_EXEC_RING_MASK));
3945 return -EINVAL;
Zou Nan haid1b851f2010-05-21 09:08:57 +08003946 }
3947
Eric Anholt4f481ed2008-09-10 14:22:49 -07003948 if (args->buffer_count < 1) {
3949 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3950 return -EINVAL;
3951 }
Eric Anholtc8e0f932009-11-22 03:49:37 +01003952 object_list = drm_malloc_ab(sizeof(*object_list), args->buffer_count);
Jesse Barnes76446ca2009-12-17 22:05:42 -05003953 if (object_list == NULL) {
3954 DRM_ERROR("Failed to allocate object list for %d buffers\n",
Eric Anholt673a3942008-07-30 12:06:12 -07003955 args->buffer_count);
3956 ret = -ENOMEM;
3957 goto pre_mutex_err;
3958 }
Eric Anholt673a3942008-07-30 12:06:12 -07003959
Eric Anholt201361a2009-03-11 12:30:04 -07003960 if (args->num_cliprects != 0) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003961 cliprects = kcalloc(args->num_cliprects, sizeof(*cliprects),
3962 GFP_KERNEL);
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003963 if (cliprects == NULL) {
3964 ret = -ENOMEM;
Eric Anholt201361a2009-03-11 12:30:04 -07003965 goto pre_mutex_err;
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003966 }
Eric Anholt201361a2009-03-11 12:30:04 -07003967
3968 ret = copy_from_user(cliprects,
3969 (struct drm_clip_rect __user *)
3970 (uintptr_t) args->cliprects_ptr,
3971 sizeof(*cliprects) * args->num_cliprects);
3972 if (ret != 0) {
3973 DRM_ERROR("copy %d cliprects failed: %d\n",
3974 args->num_cliprects, ret);
Dan Carpenterc877cdce2010-06-23 19:03:01 +02003975 ret = -EFAULT;
Eric Anholt201361a2009-03-11 12:30:04 -07003976 goto pre_mutex_err;
3977 }
3978 }
3979
Chris Wilson8dc5d142010-08-12 12:36:12 +01003980 request = kzalloc(sizeof(*request), GFP_KERNEL);
3981 if (request == NULL) {
3982 ret = -ENOMEM;
Chris Wilsona198bc82009-02-06 16:55:20 +00003983 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003984 }
3985
Chris Wilson76c1dec2010-09-25 11:22:51 +01003986 ret = i915_mutex_lock_interruptible(dev);
3987 if (ret)
3988 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003989
Eric Anholt673a3942008-07-30 12:06:12 -07003990 if (dev_priv->mm.suspended) {
Eric Anholt673a3942008-07-30 12:06:12 -07003991 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003992 ret = -EBUSY;
3993 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003994 }
3995
Keith Packardac94a962008-11-20 23:30:27 -08003996 /* Look up object handles */
Eric Anholt673a3942008-07-30 12:06:12 -07003997 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson7e318e12010-10-27 13:43:39 +01003998 struct drm_i915_gem_object *obj_priv;
3999
Chris Wilson9af90d12010-10-17 10:01:56 +01004000 object_list[i] = drm_gem_object_lookup(dev, file,
Eric Anholt673a3942008-07-30 12:06:12 -07004001 exec_list[i].handle);
4002 if (object_list[i] == NULL) {
4003 DRM_ERROR("Invalid object handle %d at index %d\n",
4004 exec_list[i].handle, i);
Chris Wilson0ce907f2010-01-23 20:26:35 +00004005 /* prevent error path from reading uninitialized data */
4006 args->buffer_count = i + 1;
Chris Wilsonbf79cb92010-08-04 14:19:46 +01004007 ret = -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07004008 goto err;
4009 }
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05004010
Daniel Vetter23010e42010-03-08 13:35:02 +01004011 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05004012 if (obj_priv->in_execbuffer) {
4013 DRM_ERROR("Object %p appears more than once in object list\n",
4014 object_list[i]);
Chris Wilson0ce907f2010-01-23 20:26:35 +00004015 /* prevent error path from reading uninitialized data */
4016 args->buffer_count = i + 1;
Chris Wilsonbf79cb92010-08-04 14:19:46 +01004017 ret = -EINVAL;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05004018 goto err;
4019 }
4020 obj_priv->in_execbuffer = true;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05004021 }
4022
Chris Wilson9af90d12010-10-17 10:01:56 +01004023 /* Move the objects en-masse into the GTT, evicting if necessary. */
Chris Wilsonbcf50e22010-11-21 22:07:12 +00004024 ret = i915_gem_execbuffer_reserve(dev, file,
4025 object_list, exec_list,
4026 args->buffer_count);
Chris Wilson9af90d12010-10-17 10:01:56 +01004027 if (ret)
4028 goto err;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07004029
Chris Wilson9af90d12010-10-17 10:01:56 +01004030 /* The objects are in their final locations, apply the relocations. */
Chris Wilsonbcf50e22010-11-21 22:07:12 +00004031 ret = i915_gem_execbuffer_relocate(dev, file,
4032 object_list, exec_list,
4033 args->buffer_count);
4034 if (ret) {
4035 if (ret == -EFAULT) {
4036 ret = i915_gem_execbuffer_relocate_slow(dev, file,
4037 object_list,
4038 exec_list,
4039 args->buffer_count);
4040 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
4041 }
Eric Anholt673a3942008-07-30 12:06:12 -07004042 if (ret)
4043 goto err;
4044 }
4045
Eric Anholt673a3942008-07-30 12:06:12 -07004046 /* Set the pending read domains for the batch buffer to COMMAND */
4047 batch_obj = object_list[args->buffer_count-1];
Chris Wilson5f26a2c2009-06-06 09:45:58 +01004048 if (batch_obj->pending_write_domain) {
4049 DRM_ERROR("Attempting to use self-modifying batch buffer\n");
4050 ret = -EINVAL;
4051 goto err;
4052 }
4053 batch_obj->pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
Eric Anholt673a3942008-07-30 12:06:12 -07004054
Chris Wilson9af90d12010-10-17 10:01:56 +01004055 /* Sanity check the batch buffer */
4056 exec_offset = to_intel_bo(batch_obj)->gtt_offset;
4057 ret = i915_gem_check_execbuffer(args, exec_offset);
Chris Wilson83d60792009-06-06 09:45:57 +01004058 if (ret != 0) {
4059 DRM_ERROR("execbuf with invalid offset/length\n");
4060 goto err;
4061 }
4062
Chris Wilson13b29282010-11-01 12:22:48 +00004063 ret = i915_gem_execbuffer_move_to_gpu(dev, file, ring,
4064 object_list, args->buffer_count);
4065 if (ret)
4066 goto err;
Eric Anholt673a3942008-07-30 12:06:12 -07004067
Eric Anholt673a3942008-07-30 12:06:12 -07004068#if WATCH_COHERENCY
4069 for (i = 0; i < args->buffer_count; i++) {
4070 i915_gem_object_check_coherency(object_list[i],
4071 exec_list[i].handle);
4072 }
4073#endif
4074
Eric Anholt673a3942008-07-30 12:06:12 -07004075#if WATCH_EXEC
Ben Gamari6911a9b2009-04-02 11:24:54 -07004076 i915_gem_dump_object(batch_obj,
Eric Anholt673a3942008-07-30 12:06:12 -07004077 args->batch_len,
4078 __func__,
4079 ~0);
4080#endif
4081
Chris Wilsone59f2ba2010-10-07 17:28:15 +01004082 /* Check for any pending flips. As we only maintain a flip queue depth
4083 * of 1, we can simply insert a WAIT for the next display flip prior
4084 * to executing the batch and avoid stalling the CPU.
4085 */
4086 flips = 0;
4087 for (i = 0; i < args->buffer_count; i++) {
4088 if (object_list[i]->write_domain)
4089 flips |= atomic_read(&to_intel_bo(object_list[i])->pending_flip);
4090 }
4091 if (flips) {
4092 int plane, flip_mask;
4093
4094 for (plane = 0; flips >> plane; plane++) {
4095 if (((flips >> plane) & 1) == 0)
4096 continue;
4097
4098 if (plane)
4099 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
4100 else
4101 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
4102
Chris Wilsone1f99ce2010-10-27 12:45:26 +01004103 ret = intel_ring_begin(ring, 2);
4104 if (ret)
4105 goto err;
4106
Chris Wilson78501ea2010-10-27 12:18:21 +01004107 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
4108 intel_ring_emit(ring, MI_NOOP);
4109 intel_ring_advance(ring);
Chris Wilsone59f2ba2010-10-07 17:28:15 +01004110 }
4111 }
4112
Eric Anholt673a3942008-07-30 12:06:12 -07004113 /* Exec the batchbuffer */
Chris Wilson78501ea2010-10-27 12:18:21 +01004114 ret = ring->dispatch_execbuffer(ring, args, cliprects, exec_offset);
Eric Anholt673a3942008-07-30 12:06:12 -07004115 if (ret) {
4116 DRM_ERROR("dispatch failed %d\n", ret);
4117 goto err;
4118 }
4119
Chris Wilson7e318e12010-10-27 13:43:39 +01004120 for (i = 0; i < args->buffer_count; i++) {
4121 struct drm_gem_object *obj = object_list[i];
4122
4123 obj->read_domains = obj->pending_read_domains;
4124 obj->write_domain = obj->pending_write_domain;
4125
4126 i915_gem_object_move_to_active(obj, ring);
4127 if (obj->write_domain) {
4128 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
4129 obj_priv->dirty = 1;
4130 list_move_tail(&obj_priv->gpu_write_list,
4131 &ring->gpu_write_list);
4132 intel_mark_busy(dev, obj);
4133 }
4134
4135 trace_i915_gem_object_change_domain(obj,
4136 obj->read_domains,
4137 obj->write_domain);
4138 }
4139
Eric Anholt673a3942008-07-30 12:06:12 -07004140 /*
4141 * Ensure that the commands in the batch buffer are
4142 * finished before the interrupt fires
4143 */
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01004144 i915_retire_commands(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07004145
Chris Wilson3cce4692010-10-27 16:11:02 +01004146 if (i915_add_request(dev, file, request, ring))
Chris Wilson5d97eb62010-11-10 20:40:02 +00004147 i915_gem_next_request_seqno(dev, ring);
Chris Wilson3cce4692010-10-27 16:11:02 +01004148 else
4149 request = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07004150
Eric Anholt673a3942008-07-30 12:06:12 -07004151err:
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05004152 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson7e318e12010-10-27 13:43:39 +01004153 if (object_list[i] == NULL)
4154 break;
4155
4156 to_intel_bo(object_list[i])->in_execbuffer = false;
Julia Lawallaad87df2008-12-21 16:28:47 +01004157 drm_gem_object_unreference(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05004158 }
Julia Lawallaad87df2008-12-21 16:28:47 +01004159
Eric Anholt673a3942008-07-30 12:06:12 -07004160 mutex_unlock(&dev->struct_mutex);
4161
Chris Wilson93533c22010-01-31 10:40:48 +00004162pre_mutex_err:
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07004163 drm_free_large(object_list);
Eric Anholt9a298b22009-03-24 12:23:04 -07004164 kfree(cliprects);
Chris Wilson8dc5d142010-08-12 12:36:12 +01004165 kfree(request);
Eric Anholt673a3942008-07-30 12:06:12 -07004166
4167 return ret;
4168}
4169
Jesse Barnes76446ca2009-12-17 22:05:42 -05004170/*
4171 * Legacy execbuffer just creates an exec2 list from the original exec object
4172 * list array and passes it to the real function.
4173 */
4174int
4175i915_gem_execbuffer(struct drm_device *dev, void *data,
4176 struct drm_file *file_priv)
4177{
4178 struct drm_i915_gem_execbuffer *args = data;
4179 struct drm_i915_gem_execbuffer2 exec2;
4180 struct drm_i915_gem_exec_object *exec_list = NULL;
4181 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
4182 int ret, i;
4183
4184#if WATCH_EXEC
4185 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
4186 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
4187#endif
4188
4189 if (args->buffer_count < 1) {
4190 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
4191 return -EINVAL;
4192 }
4193
4194 /* Copy in the exec list from userland */
4195 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
4196 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
4197 if (exec_list == NULL || exec2_list == NULL) {
4198 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
4199 args->buffer_count);
4200 drm_free_large(exec_list);
4201 drm_free_large(exec2_list);
4202 return -ENOMEM;
4203 }
4204 ret = copy_from_user(exec_list,
4205 (struct drm_i915_relocation_entry __user *)
4206 (uintptr_t) args->buffers_ptr,
4207 sizeof(*exec_list) * args->buffer_count);
4208 if (ret != 0) {
4209 DRM_ERROR("copy %d exec entries failed %d\n",
4210 args->buffer_count, ret);
4211 drm_free_large(exec_list);
4212 drm_free_large(exec2_list);
4213 return -EFAULT;
4214 }
4215
4216 for (i = 0; i < args->buffer_count; i++) {
4217 exec2_list[i].handle = exec_list[i].handle;
4218 exec2_list[i].relocation_count = exec_list[i].relocation_count;
4219 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
4220 exec2_list[i].alignment = exec_list[i].alignment;
4221 exec2_list[i].offset = exec_list[i].offset;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004222 if (INTEL_INFO(dev)->gen < 4)
Jesse Barnes76446ca2009-12-17 22:05:42 -05004223 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
4224 else
4225 exec2_list[i].flags = 0;
4226 }
4227
4228 exec2.buffers_ptr = args->buffers_ptr;
4229 exec2.buffer_count = args->buffer_count;
4230 exec2.batch_start_offset = args->batch_start_offset;
4231 exec2.batch_len = args->batch_len;
4232 exec2.DR1 = args->DR1;
4233 exec2.DR4 = args->DR4;
4234 exec2.num_cliprects = args->num_cliprects;
4235 exec2.cliprects_ptr = args->cliprects_ptr;
Zou Nan hai852835f2010-05-21 09:08:56 +08004236 exec2.flags = I915_EXEC_RENDER;
Jesse Barnes76446ca2009-12-17 22:05:42 -05004237
4238 ret = i915_gem_do_execbuffer(dev, data, file_priv, &exec2, exec2_list);
4239 if (!ret) {
4240 /* Copy the new buffer offsets back to the user's exec list. */
4241 for (i = 0; i < args->buffer_count; i++)
4242 exec_list[i].offset = exec2_list[i].offset;
4243 /* ... and back out to userspace */
4244 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
4245 (uintptr_t) args->buffers_ptr,
4246 exec_list,
4247 sizeof(*exec_list) * args->buffer_count);
4248 if (ret) {
4249 ret = -EFAULT;
4250 DRM_ERROR("failed to copy %d exec entries "
4251 "back to user (%d)\n",
4252 args->buffer_count, ret);
4253 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05004254 }
4255
4256 drm_free_large(exec_list);
4257 drm_free_large(exec2_list);
4258 return ret;
4259}
4260
4261int
4262i915_gem_execbuffer2(struct drm_device *dev, void *data,
4263 struct drm_file *file_priv)
4264{
4265 struct drm_i915_gem_execbuffer2 *args = data;
4266 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
4267 int ret;
4268
4269#if WATCH_EXEC
4270 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
4271 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
4272#endif
4273
4274 if (args->buffer_count < 1) {
4275 DRM_ERROR("execbuf2 with %d buffers\n", args->buffer_count);
4276 return -EINVAL;
4277 }
4278
4279 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
4280 if (exec2_list == NULL) {
4281 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
4282 args->buffer_count);
4283 return -ENOMEM;
4284 }
4285 ret = copy_from_user(exec2_list,
4286 (struct drm_i915_relocation_entry __user *)
4287 (uintptr_t) args->buffers_ptr,
4288 sizeof(*exec2_list) * args->buffer_count);
4289 if (ret != 0) {
4290 DRM_ERROR("copy %d exec entries failed %d\n",
4291 args->buffer_count, ret);
4292 drm_free_large(exec2_list);
4293 return -EFAULT;
4294 }
4295
4296 ret = i915_gem_do_execbuffer(dev, data, file_priv, args, exec2_list);
4297 if (!ret) {
4298 /* Copy the new buffer offsets back to the user's exec list. */
4299 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
4300 (uintptr_t) args->buffers_ptr,
4301 exec2_list,
4302 sizeof(*exec2_list) * args->buffer_count);
4303 if (ret) {
4304 ret = -EFAULT;
4305 DRM_ERROR("failed to copy %d exec entries "
4306 "back to user (%d)\n",
4307 args->buffer_count, ret);
4308 }
4309 }
4310
4311 drm_free_large(exec2_list);
4312 return ret;
4313}
4314
Eric Anholt673a3942008-07-30 12:06:12 -07004315int
Daniel Vetter920afa72010-09-16 17:54:23 +02004316i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01004317 bool map_and_fenceable)
Eric Anholt673a3942008-07-30 12:06:12 -07004318{
4319 struct drm_device *dev = obj->dev;
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004320 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01004321 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004322 int ret;
4323
Daniel Vetter778c3542010-05-13 11:49:44 +02004324 BUG_ON(obj_priv->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT);
Daniel Vetter75e9e912010-11-04 17:11:09 +01004325 BUG_ON(map_and_fenceable && !map_and_fenceable);
Chris Wilson23bc5982010-09-29 16:10:57 +01004326 WARN_ON(i915_verify_lists(dev));
Chris Wilsonac0c6b52010-05-27 13:18:18 +01004327
4328 if (obj_priv->gtt_space != NULL) {
Chris Wilsona00b10c2010-09-24 21:15:47 +01004329 if ((alignment && obj_priv->gtt_offset & (alignment - 1)) ||
Daniel Vetter75e9e912010-11-04 17:11:09 +01004330 (map_and_fenceable && !obj_priv->map_and_fenceable)) {
Chris Wilsonae7d49d2010-08-04 12:37:41 +01004331 WARN(obj_priv->pin_count,
4332 "bo is already pinned with incorrect alignment:"
Daniel Vetter75e9e912010-11-04 17:11:09 +01004333 " offset=%x, req.alignment=%x, req.map_and_fenceable=%d,"
4334 " obj->map_and_fenceable=%d\n",
Chris Wilsona00b10c2010-09-24 21:15:47 +01004335 obj_priv->gtt_offset, alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01004336 map_and_fenceable,
4337 obj_priv->map_and_fenceable);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01004338 ret = i915_gem_object_unbind(obj);
4339 if (ret)
4340 return ret;
4341 }
4342 }
4343
Eric Anholt673a3942008-07-30 12:06:12 -07004344 if (obj_priv->gtt_space == NULL) {
Chris Wilsona00b10c2010-09-24 21:15:47 +01004345 ret = i915_gem_object_bind_to_gtt(obj, alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01004346 map_and_fenceable);
Chris Wilson97311292009-09-21 00:22:34 +01004347 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07004348 return ret;
Chris Wilson22c344e2009-02-11 14:26:45 +00004349 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05004350
Chris Wilson74653782010-10-29 10:41:23 +01004351 if (obj_priv->pin_count++ == 0) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01004352 i915_gem_info_add_pin(dev_priv, obj_priv, map_and_fenceable);
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004353 if (!obj_priv->active)
Chris Wilson69dc4982010-10-19 10:36:51 +01004354 list_move_tail(&obj_priv->mm_list,
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004355 &dev_priv->mm.pinned_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004356 }
Daniel Vetter75e9e912010-11-04 17:11:09 +01004357 BUG_ON(!obj_priv->pin_mappable && map_and_fenceable);
Eric Anholt673a3942008-07-30 12:06:12 -07004358
Chris Wilson23bc5982010-09-29 16:10:57 +01004359 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07004360 return 0;
4361}
4362
4363void
4364i915_gem_object_unpin(struct drm_gem_object *obj)
4365{
4366 struct drm_device *dev = obj->dev;
4367 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01004368 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004369
Chris Wilson23bc5982010-09-29 16:10:57 +01004370 WARN_ON(i915_verify_lists(dev));
Chris Wilson74653782010-10-29 10:41:23 +01004371 BUG_ON(obj_priv->pin_count == 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004372 BUG_ON(obj_priv->gtt_space == NULL);
4373
Chris Wilson74653782010-10-29 10:41:23 +01004374 if (--obj_priv->pin_count == 0) {
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004375 if (!obj_priv->active)
Chris Wilson69dc4982010-10-19 10:36:51 +01004376 list_move_tail(&obj_priv->mm_list,
Eric Anholt673a3942008-07-30 12:06:12 -07004377 &dev_priv->mm.inactive_list);
Chris Wilsona00b10c2010-09-24 21:15:47 +01004378 i915_gem_info_remove_pin(dev_priv, obj_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07004379 }
Chris Wilson23bc5982010-09-29 16:10:57 +01004380 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07004381}
4382
4383int
4384i915_gem_pin_ioctl(struct drm_device *dev, void *data,
4385 struct drm_file *file_priv)
4386{
4387 struct drm_i915_gem_pin *args = data;
4388 struct drm_gem_object *obj;
4389 struct drm_i915_gem_object *obj_priv;
4390 int ret;
4391
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004392 ret = i915_mutex_lock_interruptible(dev);
4393 if (ret)
4394 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004395
4396 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4397 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004398 ret = -ENOENT;
4399 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07004400 }
Daniel Vetter23010e42010-03-08 13:35:02 +01004401 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004402
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004403 if (obj_priv->madv != I915_MADV_WILLNEED) {
4404 DRM_ERROR("Attempting to pin a purgeable buffer\n");
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004405 ret = -EINVAL;
4406 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004407 }
4408
Jesse Barnes79e53942008-11-07 14:24:08 -08004409 if (obj_priv->pin_filp != NULL && obj_priv->pin_filp != file_priv) {
4410 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
4411 args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004412 ret = -EINVAL;
4413 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08004414 }
4415
4416 obj_priv->user_pin_count++;
4417 obj_priv->pin_filp = file_priv;
4418 if (obj_priv->user_pin_count == 1) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01004419 ret = i915_gem_object_pin(obj, args->alignment, true);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004420 if (ret)
4421 goto out;
Eric Anholt673a3942008-07-30 12:06:12 -07004422 }
4423
4424 /* XXX - flush the CPU caches for pinned objects
4425 * as the X server doesn't manage domains yet
4426 */
Eric Anholte47c68e2008-11-14 13:35:19 -08004427 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004428 args->offset = obj_priv->gtt_offset;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004429out:
Eric Anholt673a3942008-07-30 12:06:12 -07004430 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004431unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07004432 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004433 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004434}
4435
4436int
4437i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
4438 struct drm_file *file_priv)
4439{
4440 struct drm_i915_gem_pin *args = data;
4441 struct drm_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08004442 struct drm_i915_gem_object *obj_priv;
Chris Wilson76c1dec2010-09-25 11:22:51 +01004443 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004444
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004445 ret = i915_mutex_lock_interruptible(dev);
4446 if (ret)
4447 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004448
4449 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4450 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004451 ret = -ENOENT;
4452 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07004453 }
Daniel Vetter23010e42010-03-08 13:35:02 +01004454 obj_priv = to_intel_bo(obj);
Chris Wilson76c1dec2010-09-25 11:22:51 +01004455
Jesse Barnes79e53942008-11-07 14:24:08 -08004456 if (obj_priv->pin_filp != file_priv) {
4457 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
4458 args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004459 ret = -EINVAL;
4460 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08004461 }
4462 obj_priv->user_pin_count--;
4463 if (obj_priv->user_pin_count == 0) {
4464 obj_priv->pin_filp = NULL;
4465 i915_gem_object_unpin(obj);
4466 }
Eric Anholt673a3942008-07-30 12:06:12 -07004467
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004468out:
Eric Anholt673a3942008-07-30 12:06:12 -07004469 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004470unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07004471 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004472 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004473}
4474
4475int
4476i915_gem_busy_ioctl(struct drm_device *dev, void *data,
4477 struct drm_file *file_priv)
4478{
4479 struct drm_i915_gem_busy *args = data;
4480 struct drm_gem_object *obj;
4481 struct drm_i915_gem_object *obj_priv;
Chris Wilson30dbf0c2010-09-25 10:19:17 +01004482 int ret;
4483
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004484 ret = i915_mutex_lock_interruptible(dev);
4485 if (ret)
4486 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004487
Eric Anholt673a3942008-07-30 12:06:12 -07004488 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4489 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004490 ret = -ENOENT;
4491 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07004492 }
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004493 obj_priv = to_intel_bo(obj);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004494
Chris Wilson0be555b2010-08-04 15:36:30 +01004495 /* Count all active objects as busy, even if they are currently not used
4496 * by the gpu. Users of this interface expect objects to eventually
4497 * become non-busy without any further actions, therefore emit any
4498 * necessary flushes here.
Eric Anholtc4de0a52008-12-14 19:05:04 -08004499 */
Chris Wilson0be555b2010-08-04 15:36:30 +01004500 args->busy = obj_priv->active;
4501 if (args->busy) {
4502 /* Unconditionally flush objects, even when the gpu still uses this
4503 * object. Userspace calling this function indicates that it wants to
4504 * use this buffer rather sooner than later, so issuing the required
4505 * flush earlier is beneficial.
4506 */
Chris Wilsonc78ec302010-09-20 12:50:23 +01004507 if (obj->write_domain & I915_GEM_GPU_DOMAINS)
4508 i915_gem_flush_ring(dev, file_priv,
Chris Wilson92204342010-09-18 11:02:01 +01004509 obj_priv->ring,
4510 0, obj->write_domain);
Chris Wilson0be555b2010-08-04 15:36:30 +01004511
4512 /* Update the active list for the hardware's current position.
4513 * Otherwise this only updates on a delayed timer or when irqs
4514 * are actually unmasked, and our working set ends up being
4515 * larger than required.
4516 */
4517 i915_gem_retire_requests_ring(dev, obj_priv->ring);
4518
4519 args->busy = obj_priv->active;
4520 }
Eric Anholt673a3942008-07-30 12:06:12 -07004521
4522 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004523unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07004524 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004525 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004526}
4527
4528int
4529i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4530 struct drm_file *file_priv)
4531{
4532 return i915_gem_ring_throttle(dev, file_priv);
4533}
4534
Chris Wilson3ef94da2009-09-14 16:50:29 +01004535int
4536i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4537 struct drm_file *file_priv)
4538{
4539 struct drm_i915_gem_madvise *args = data;
4540 struct drm_gem_object *obj;
4541 struct drm_i915_gem_object *obj_priv;
Chris Wilson76c1dec2010-09-25 11:22:51 +01004542 int ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004543
4544 switch (args->madv) {
4545 case I915_MADV_DONTNEED:
4546 case I915_MADV_WILLNEED:
4547 break;
4548 default:
4549 return -EINVAL;
4550 }
4551
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004552 ret = i915_mutex_lock_interruptible(dev);
4553 if (ret)
4554 return ret;
4555
Chris Wilson3ef94da2009-09-14 16:50:29 +01004556 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4557 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004558 ret = -ENOENT;
4559 goto unlock;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004560 }
Daniel Vetter23010e42010-03-08 13:35:02 +01004561 obj_priv = to_intel_bo(obj);
Chris Wilson3ef94da2009-09-14 16:50:29 +01004562
4563 if (obj_priv->pin_count) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004564 ret = -EINVAL;
4565 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004566 }
4567
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004568 if (obj_priv->madv != __I915_MADV_PURGED)
4569 obj_priv->madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004570
Chris Wilson2d7ef392009-09-20 23:13:10 +01004571 /* if the object is no longer bound, discard its backing storage */
4572 if (i915_gem_object_is_purgeable(obj_priv) &&
4573 obj_priv->gtt_space == NULL)
4574 i915_gem_object_truncate(obj);
4575
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004576 args->retained = obj_priv->madv != __I915_MADV_PURGED;
4577
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004578out:
Chris Wilson3ef94da2009-09-14 16:50:29 +01004579 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004580unlock:
Chris Wilson3ef94da2009-09-14 16:50:29 +01004581 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004582 return ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004583}
4584
Daniel Vetterac52bc52010-04-09 19:05:06 +00004585struct drm_gem_object * i915_gem_alloc_object(struct drm_device *dev,
4586 size_t size)
4587{
Chris Wilson73aa8082010-09-30 11:46:12 +01004588 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc397b902010-04-09 19:05:07 +00004589 struct drm_i915_gem_object *obj;
4590
4591 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
4592 if (obj == NULL)
4593 return NULL;
4594
4595 if (drm_gem_object_init(dev, &obj->base, size) != 0) {
4596 kfree(obj);
4597 return NULL;
4598 }
4599
Chris Wilson73aa8082010-09-30 11:46:12 +01004600 i915_gem_info_add_obj(dev_priv, size);
4601
Daniel Vetterc397b902010-04-09 19:05:07 +00004602 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4603 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4604
4605 obj->agp_type = AGP_USER_MEMORY;
Daniel Vetter62b8b212010-04-09 19:05:08 +00004606 obj->base.driver_private = NULL;
Daniel Vetterc397b902010-04-09 19:05:07 +00004607 obj->fence_reg = I915_FENCE_REG_NONE;
Chris Wilson69dc4982010-10-19 10:36:51 +01004608 INIT_LIST_HEAD(&obj->mm_list);
Daniel Vetter93a37f22010-11-05 20:24:53 +01004609 INIT_LIST_HEAD(&obj->gtt_list);
Chris Wilson69dc4982010-10-19 10:36:51 +01004610 INIT_LIST_HEAD(&obj->ring_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00004611 INIT_LIST_HEAD(&obj->gpu_write_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00004612 obj->madv = I915_MADV_WILLNEED;
Daniel Vetter75e9e912010-11-04 17:11:09 +01004613 /* Avoid an unnecessary call to unbind on the first bind. */
4614 obj->map_and_fenceable = true;
Daniel Vetterc397b902010-04-09 19:05:07 +00004615
Daniel Vetterc397b902010-04-09 19:05:07 +00004616 return &obj->base;
Daniel Vetterac52bc52010-04-09 19:05:06 +00004617}
4618
Eric Anholt673a3942008-07-30 12:06:12 -07004619int i915_gem_init_object(struct drm_gem_object *obj)
4620{
Daniel Vetterc397b902010-04-09 19:05:07 +00004621 BUG();
Jesse Barnesde151cf2008-11-12 10:03:55 -08004622
Eric Anholt673a3942008-07-30 12:06:12 -07004623 return 0;
4624}
4625
Chris Wilsonbe726152010-07-23 23:18:50 +01004626static void i915_gem_free_object_tail(struct drm_gem_object *obj)
4627{
4628 struct drm_device *dev = obj->dev;
4629 drm_i915_private_t *dev_priv = dev->dev_private;
4630 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
4631 int ret;
4632
4633 ret = i915_gem_object_unbind(obj);
4634 if (ret == -ERESTARTSYS) {
Chris Wilson69dc4982010-10-19 10:36:51 +01004635 list_move(&obj_priv->mm_list,
Chris Wilsonbe726152010-07-23 23:18:50 +01004636 &dev_priv->mm.deferred_free_list);
4637 return;
4638 }
4639
Chris Wilson39a01d12010-10-28 13:03:06 +01004640 if (obj->map_list.map)
Chris Wilsonbe726152010-07-23 23:18:50 +01004641 i915_gem_free_mmap_offset(obj);
4642
4643 drm_gem_object_release(obj);
Chris Wilson73aa8082010-09-30 11:46:12 +01004644 i915_gem_info_remove_obj(dev_priv, obj->size);
Chris Wilsonbe726152010-07-23 23:18:50 +01004645
4646 kfree(obj_priv->page_cpu_valid);
4647 kfree(obj_priv->bit_17);
4648 kfree(obj_priv);
4649}
4650
Eric Anholt673a3942008-07-30 12:06:12 -07004651void i915_gem_free_object(struct drm_gem_object *obj)
4652{
Jesse Barnesde151cf2008-11-12 10:03:55 -08004653 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01004654 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004655
Chris Wilson1c5d22f2009-08-25 11:15:50 +01004656 trace_i915_gem_object_destroy(obj);
4657
Eric Anholt673a3942008-07-30 12:06:12 -07004658 while (obj_priv->pin_count > 0)
4659 i915_gem_object_unpin(obj);
4660
Dave Airlie71acb5e2008-12-30 20:31:46 +10004661 if (obj_priv->phys_obj)
4662 i915_gem_detach_phys_object(dev, obj);
4663
Chris Wilsonbe726152010-07-23 23:18:50 +01004664 i915_gem_free_object_tail(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004665}
4666
Jesse Barnes5669fca2009-02-17 15:13:31 -08004667int
Eric Anholt673a3942008-07-30 12:06:12 -07004668i915_gem_idle(struct drm_device *dev)
4669{
4670 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson29105cc2010-01-07 10:39:13 +00004671 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004672
Keith Packard6dbe2772008-10-14 21:41:13 -07004673 mutex_lock(&dev->struct_mutex);
4674
Chris Wilson87acb0a2010-10-19 10:13:00 +01004675 if (dev_priv->mm.suspended) {
Keith Packard6dbe2772008-10-14 21:41:13 -07004676 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004677 return 0;
Keith Packard6dbe2772008-10-14 21:41:13 -07004678 }
Eric Anholt673a3942008-07-30 12:06:12 -07004679
Chris Wilson29105cc2010-01-07 10:39:13 +00004680 ret = i915_gpu_idle(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004681 if (ret) {
4682 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004683 return ret;
Keith Packard6dbe2772008-10-14 21:41:13 -07004684 }
Eric Anholt673a3942008-07-30 12:06:12 -07004685
Chris Wilson29105cc2010-01-07 10:39:13 +00004686 /* Under UMS, be paranoid and evict. */
4687 if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
Chris Wilson5eac3ab2010-10-31 08:49:47 +00004688 ret = i915_gem_evict_inactive(dev, false);
Chris Wilson29105cc2010-01-07 10:39:13 +00004689 if (ret) {
4690 mutex_unlock(&dev->struct_mutex);
4691 return ret;
4692 }
4693 }
4694
4695 /* Hack! Don't let anybody do execbuf while we don't control the chip.
4696 * We need to replace this with a semaphore, or something.
4697 * And not confound mm.suspended!
4698 */
4699 dev_priv->mm.suspended = 1;
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02004700 del_timer_sync(&dev_priv->hangcheck_timer);
Chris Wilson29105cc2010-01-07 10:39:13 +00004701
4702 i915_kernel_lost_context(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004703 i915_gem_cleanup_ringbuffer(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00004704
Keith Packard6dbe2772008-10-14 21:41:13 -07004705 mutex_unlock(&dev->struct_mutex);
4706
Chris Wilson29105cc2010-01-07 10:39:13 +00004707 /* Cancel the retire work handler, which should be idle now. */
4708 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
4709
Eric Anholt673a3942008-07-30 12:06:12 -07004710 return 0;
4711}
4712
Jesse Barnese552eb72010-04-21 11:39:23 -07004713/*
4714 * 965+ support PIPE_CONTROL commands, which provide finer grained control
4715 * over cache flushing.
4716 */
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004717static int
Jesse Barnese552eb72010-04-21 11:39:23 -07004718i915_gem_init_pipe_control(struct drm_device *dev)
4719{
4720 drm_i915_private_t *dev_priv = dev->dev_private;
4721 struct drm_gem_object *obj;
4722 struct drm_i915_gem_object *obj_priv;
4723 int ret;
4724
Eric Anholt34dc4d42010-05-07 14:30:03 -07004725 obj = i915_gem_alloc_object(dev, 4096);
Jesse Barnese552eb72010-04-21 11:39:23 -07004726 if (obj == NULL) {
4727 DRM_ERROR("Failed to allocate seqno page\n");
4728 ret = -ENOMEM;
4729 goto err;
4730 }
4731 obj_priv = to_intel_bo(obj);
4732 obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
4733
Daniel Vetter75e9e912010-11-04 17:11:09 +01004734 ret = i915_gem_object_pin(obj, 4096, true);
Jesse Barnese552eb72010-04-21 11:39:23 -07004735 if (ret)
4736 goto err_unref;
4737
4738 dev_priv->seqno_gfx_addr = obj_priv->gtt_offset;
4739 dev_priv->seqno_page = kmap(obj_priv->pages[0]);
4740 if (dev_priv->seqno_page == NULL)
4741 goto err_unpin;
4742
4743 dev_priv->seqno_obj = obj;
4744 memset(dev_priv->seqno_page, 0, PAGE_SIZE);
4745
4746 return 0;
4747
4748err_unpin:
4749 i915_gem_object_unpin(obj);
4750err_unref:
4751 drm_gem_object_unreference(obj);
4752err:
4753 return ret;
4754}
4755
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004756
4757static void
Jesse Barnese552eb72010-04-21 11:39:23 -07004758i915_gem_cleanup_pipe_control(struct drm_device *dev)
4759{
4760 drm_i915_private_t *dev_priv = dev->dev_private;
4761 struct drm_gem_object *obj;
4762 struct drm_i915_gem_object *obj_priv;
4763
4764 obj = dev_priv->seqno_obj;
4765 obj_priv = to_intel_bo(obj);
4766 kunmap(obj_priv->pages[0]);
4767 i915_gem_object_unpin(obj);
4768 drm_gem_object_unreference(obj);
4769 dev_priv->seqno_obj = NULL;
4770
4771 dev_priv->seqno_page = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07004772}
4773
Eric Anholt673a3942008-07-30 12:06:12 -07004774int
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004775i915_gem_init_ringbuffer(struct drm_device *dev)
4776{
4777 drm_i915_private_t *dev_priv = dev->dev_private;
4778 int ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01004779
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004780 if (HAS_PIPE_CONTROL(dev)) {
4781 ret = i915_gem_init_pipe_control(dev);
4782 if (ret)
4783 return ret;
4784 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004785
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08004786 ret = intel_init_render_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004787 if (ret)
4788 goto cleanup_pipe_control;
4789
4790 if (HAS_BSD(dev)) {
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08004791 ret = intel_init_bsd_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004792 if (ret)
4793 goto cleanup_render_ring;
Zou Nan haid1b851f2010-05-21 09:08:57 +08004794 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004795
Chris Wilson549f7362010-10-19 11:19:32 +01004796 if (HAS_BLT(dev)) {
4797 ret = intel_init_blt_ring_buffer(dev);
4798 if (ret)
4799 goto cleanup_bsd_ring;
4800 }
4801
Chris Wilson6f392d52010-08-07 11:01:22 +01004802 dev_priv->next_seqno = 1;
4803
Chris Wilson68f95ba2010-05-27 13:18:22 +01004804 return 0;
4805
Chris Wilson549f7362010-10-19 11:19:32 +01004806cleanup_bsd_ring:
Chris Wilson78501ea2010-10-27 12:18:21 +01004807 intel_cleanup_ring_buffer(&dev_priv->bsd_ring);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004808cleanup_render_ring:
Chris Wilson78501ea2010-10-27 12:18:21 +01004809 intel_cleanup_ring_buffer(&dev_priv->render_ring);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004810cleanup_pipe_control:
4811 if (HAS_PIPE_CONTROL(dev))
4812 i915_gem_cleanup_pipe_control(dev);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004813 return ret;
4814}
4815
4816void
4817i915_gem_cleanup_ringbuffer(struct drm_device *dev)
4818{
4819 drm_i915_private_t *dev_priv = dev->dev_private;
4820
Chris Wilson78501ea2010-10-27 12:18:21 +01004821 intel_cleanup_ring_buffer(&dev_priv->render_ring);
4822 intel_cleanup_ring_buffer(&dev_priv->bsd_ring);
4823 intel_cleanup_ring_buffer(&dev_priv->blt_ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004824 if (HAS_PIPE_CONTROL(dev))
4825 i915_gem_cleanup_pipe_control(dev);
4826}
4827
4828int
Eric Anholt673a3942008-07-30 12:06:12 -07004829i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
4830 struct drm_file *file_priv)
4831{
4832 drm_i915_private_t *dev_priv = dev->dev_private;
4833 int ret;
4834
Jesse Barnes79e53942008-11-07 14:24:08 -08004835 if (drm_core_check_feature(dev, DRIVER_MODESET))
4836 return 0;
4837
Ben Gamariba1234d2009-09-14 17:48:47 -04004838 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07004839 DRM_ERROR("Reenabling wedged hardware, good luck\n");
Ben Gamariba1234d2009-09-14 17:48:47 -04004840 atomic_set(&dev_priv->mm.wedged, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004841 }
4842
Eric Anholt673a3942008-07-30 12:06:12 -07004843 mutex_lock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004844 dev_priv->mm.suspended = 0;
4845
4846 ret = i915_gem_init_ringbuffer(dev);
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004847 if (ret != 0) {
4848 mutex_unlock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004849 return ret;
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004850 }
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004851
Chris Wilson69dc4982010-10-19 10:36:51 +01004852 BUG_ON(!list_empty(&dev_priv->mm.active_list));
Zou Nan hai852835f2010-05-21 09:08:56 +08004853 BUG_ON(!list_empty(&dev_priv->render_ring.active_list));
Chris Wilson87acb0a2010-10-19 10:13:00 +01004854 BUG_ON(!list_empty(&dev_priv->bsd_ring.active_list));
Chris Wilson549f7362010-10-19 11:19:32 +01004855 BUG_ON(!list_empty(&dev_priv->blt_ring.active_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004856 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
4857 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
Zou Nan hai852835f2010-05-21 09:08:56 +08004858 BUG_ON(!list_empty(&dev_priv->render_ring.request_list));
Chris Wilson87acb0a2010-10-19 10:13:00 +01004859 BUG_ON(!list_empty(&dev_priv->bsd_ring.request_list));
Chris Wilson549f7362010-10-19 11:19:32 +01004860 BUG_ON(!list_empty(&dev_priv->blt_ring.request_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004861 mutex_unlock(&dev->struct_mutex);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004862
Chris Wilson5f353082010-06-07 14:03:03 +01004863 ret = drm_irq_install(dev);
4864 if (ret)
4865 goto cleanup_ringbuffer;
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004866
Eric Anholt673a3942008-07-30 12:06:12 -07004867 return 0;
Chris Wilson5f353082010-06-07 14:03:03 +01004868
4869cleanup_ringbuffer:
4870 mutex_lock(&dev->struct_mutex);
4871 i915_gem_cleanup_ringbuffer(dev);
4872 dev_priv->mm.suspended = 1;
4873 mutex_unlock(&dev->struct_mutex);
4874
4875 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004876}
4877
4878int
4879i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
4880 struct drm_file *file_priv)
4881{
Jesse Barnes79e53942008-11-07 14:24:08 -08004882 if (drm_core_check_feature(dev, DRIVER_MODESET))
4883 return 0;
4884
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004885 drm_irq_uninstall(dev);
Linus Torvaldse6890f62009-09-08 17:09:24 -07004886 return i915_gem_idle(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004887}
4888
4889void
4890i915_gem_lastclose(struct drm_device *dev)
4891{
4892 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004893
Eric Anholte806b492009-01-22 09:56:58 -08004894 if (drm_core_check_feature(dev, DRIVER_MODESET))
4895 return;
4896
Keith Packard6dbe2772008-10-14 21:41:13 -07004897 ret = i915_gem_idle(dev);
4898 if (ret)
4899 DRM_ERROR("failed to idle hardware: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07004900}
4901
Chris Wilson64193402010-10-24 12:38:05 +01004902static void
4903init_ring_lists(struct intel_ring_buffer *ring)
4904{
4905 INIT_LIST_HEAD(&ring->active_list);
4906 INIT_LIST_HEAD(&ring->request_list);
4907 INIT_LIST_HEAD(&ring->gpu_write_list);
4908}
4909
Eric Anholt673a3942008-07-30 12:06:12 -07004910void
4911i915_gem_load(struct drm_device *dev)
4912{
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004913 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07004914 drm_i915_private_t *dev_priv = dev->dev_private;
4915
Chris Wilson69dc4982010-10-19 10:36:51 +01004916 INIT_LIST_HEAD(&dev_priv->mm.active_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004917 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
4918 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004919 INIT_LIST_HEAD(&dev_priv->mm.pinned_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07004920 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Chris Wilsonbe726152010-07-23 23:18:50 +01004921 INIT_LIST_HEAD(&dev_priv->mm.deferred_free_list);
Daniel Vetter93a37f22010-11-05 20:24:53 +01004922 INIT_LIST_HEAD(&dev_priv->mm.gtt_list);
Chris Wilson64193402010-10-24 12:38:05 +01004923 init_ring_lists(&dev_priv->render_ring);
4924 init_ring_lists(&dev_priv->bsd_ring);
4925 init_ring_lists(&dev_priv->blt_ring);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02004926 for (i = 0; i < 16; i++)
4927 INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004928 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
4929 i915_gem_retire_work_handler);
Chris Wilson30dbf0c2010-09-25 10:19:17 +01004930 init_completion(&dev_priv->error_completion);
Chris Wilson31169712009-09-14 16:50:28 +01004931
Dave Airlie94400122010-07-20 13:15:31 +10004932 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
4933 if (IS_GEN3(dev)) {
4934 u32 tmp = I915_READ(MI_ARB_STATE);
4935 if (!(tmp & MI_ARB_C3_LP_WRITE_ENABLE)) {
4936 /* arb state is a masked write, so set bit + bit in mask */
4937 tmp = MI_ARB_C3_LP_WRITE_ENABLE | (MI_ARB_C3_LP_WRITE_ENABLE << MI_ARB_MASK_SHIFT);
4938 I915_WRITE(MI_ARB_STATE, tmp);
4939 }
4940 }
4941
Jesse Barnesde151cf2008-11-12 10:03:55 -08004942 /* Old X drivers will take 0-2 for front, back, depth buffers */
Eric Anholtb397c832010-01-26 09:43:10 -08004943 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4944 dev_priv->fence_reg_start = 3;
Jesse Barnesde151cf2008-11-12 10:03:55 -08004945
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004946 if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08004947 dev_priv->num_fence_regs = 16;
4948 else
4949 dev_priv->num_fence_regs = 8;
4950
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004951 /* Initialize fence registers to zero */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004952 switch (INTEL_INFO(dev)->gen) {
4953 case 6:
4954 for (i = 0; i < 16; i++)
4955 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (i * 8), 0);
4956 break;
4957 case 5:
4958 case 4:
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004959 for (i = 0; i < 16; i++)
4960 I915_WRITE64(FENCE_REG_965_0 + (i * 8), 0);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004961 break;
4962 case 3:
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004963 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4964 for (i = 0; i < 8; i++)
4965 I915_WRITE(FENCE_REG_945_8 + (i * 4), 0);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004966 case 2:
4967 for (i = 0; i < 8; i++)
4968 I915_WRITE(FENCE_REG_830_0 + (i * 4), 0);
4969 break;
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004970 }
Eric Anholt673a3942008-07-30 12:06:12 -07004971 i915_gem_detect_bit_6_swizzle(dev);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05004972 init_waitqueue_head(&dev_priv->pending_flip_queue);
Chris Wilson17250b72010-10-28 12:51:39 +01004973
4974 dev_priv->mm.inactive_shrinker.shrink = i915_gem_inactive_shrink;
4975 dev_priv->mm.inactive_shrinker.seeks = DEFAULT_SEEKS;
4976 register_shrinker(&dev_priv->mm.inactive_shrinker);
Eric Anholt673a3942008-07-30 12:06:12 -07004977}
Dave Airlie71acb5e2008-12-30 20:31:46 +10004978
4979/*
4980 * Create a physically contiguous memory object for this object
4981 * e.g. for cursor + overlay regs
4982 */
Chris Wilson995b6762010-08-20 13:23:26 +01004983static int i915_gem_init_phys_object(struct drm_device *dev,
4984 int id, int size, int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004985{
4986 drm_i915_private_t *dev_priv = dev->dev_private;
4987 struct drm_i915_gem_phys_object *phys_obj;
4988 int ret;
4989
4990 if (dev_priv->mm.phys_objs[id - 1] || !size)
4991 return 0;
4992
Eric Anholt9a298b22009-03-24 12:23:04 -07004993 phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004994 if (!phys_obj)
4995 return -ENOMEM;
4996
4997 phys_obj->id = id;
4998
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004999 phys_obj->handle = drm_pci_alloc(dev, size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10005000 if (!phys_obj->handle) {
5001 ret = -ENOMEM;
5002 goto kfree_obj;
5003 }
5004#ifdef CONFIG_X86
5005 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
5006#endif
5007
5008 dev_priv->mm.phys_objs[id - 1] = phys_obj;
5009
5010 return 0;
5011kfree_obj:
Eric Anholt9a298b22009-03-24 12:23:04 -07005012 kfree(phys_obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10005013 return ret;
5014}
5015
Chris Wilson995b6762010-08-20 13:23:26 +01005016static void i915_gem_free_phys_object(struct drm_device *dev, int id)
Dave Airlie71acb5e2008-12-30 20:31:46 +10005017{
5018 drm_i915_private_t *dev_priv = dev->dev_private;
5019 struct drm_i915_gem_phys_object *phys_obj;
5020
5021 if (!dev_priv->mm.phys_objs[id - 1])
5022 return;
5023
5024 phys_obj = dev_priv->mm.phys_objs[id - 1];
5025 if (phys_obj->cur_obj) {
5026 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
5027 }
5028
5029#ifdef CONFIG_X86
5030 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
5031#endif
5032 drm_pci_free(dev, phys_obj->handle);
5033 kfree(phys_obj);
5034 dev_priv->mm.phys_objs[id - 1] = NULL;
5035}
5036
5037void i915_gem_free_all_phys_object(struct drm_device *dev)
5038{
5039 int i;
5040
Dave Airlie260883c2009-01-22 17:58:49 +10005041 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
Dave Airlie71acb5e2008-12-30 20:31:46 +10005042 i915_gem_free_phys_object(dev, i);
5043}
5044
5045void i915_gem_detach_phys_object(struct drm_device *dev,
5046 struct drm_gem_object *obj)
5047{
Chris Wilsone5281cc2010-10-28 13:45:36 +01005048 struct address_space *mapping = obj->filp->f_path.dentry->d_inode->i_mapping;
5049 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
5050 char *vaddr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10005051 int i;
Dave Airlie71acb5e2008-12-30 20:31:46 +10005052 int page_count;
5053
Dave Airlie71acb5e2008-12-30 20:31:46 +10005054 if (!obj_priv->phys_obj)
5055 return;
Chris Wilsone5281cc2010-10-28 13:45:36 +01005056 vaddr = obj_priv->phys_obj->handle->vaddr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10005057
5058 page_count = obj->size / PAGE_SIZE;
5059
5060 for (i = 0; i < page_count; i++) {
Chris Wilsone5281cc2010-10-28 13:45:36 +01005061 struct page *page = read_cache_page_gfp(mapping, i,
5062 GFP_HIGHUSER | __GFP_RECLAIMABLE);
5063 if (!IS_ERR(page)) {
5064 char *dst = kmap_atomic(page);
5065 memcpy(dst, vaddr + i*PAGE_SIZE, PAGE_SIZE);
5066 kunmap_atomic(dst);
Dave Airlie71acb5e2008-12-30 20:31:46 +10005067
Chris Wilsone5281cc2010-10-28 13:45:36 +01005068 drm_clflush_pages(&page, 1);
5069
5070 set_page_dirty(page);
5071 mark_page_accessed(page);
5072 page_cache_release(page);
5073 }
Dave Airlie71acb5e2008-12-30 20:31:46 +10005074 }
Daniel Vetter40ce6572010-11-05 18:12:18 +01005075 intel_gtt_chipset_flush();
Chris Wilsond78b47b2009-06-17 21:52:49 +01005076
Dave Airlie71acb5e2008-12-30 20:31:46 +10005077 obj_priv->phys_obj->cur_obj = NULL;
5078 obj_priv->phys_obj = NULL;
5079}
5080
5081int
5082i915_gem_attach_phys_object(struct drm_device *dev,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01005083 struct drm_gem_object *obj,
5084 int id,
5085 int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10005086{
Chris Wilsone5281cc2010-10-28 13:45:36 +01005087 struct address_space *mapping = obj->filp->f_path.dentry->d_inode->i_mapping;
Dave Airlie71acb5e2008-12-30 20:31:46 +10005088 drm_i915_private_t *dev_priv = dev->dev_private;
5089 struct drm_i915_gem_object *obj_priv;
5090 int ret = 0;
5091 int page_count;
5092 int i;
5093
5094 if (id > I915_MAX_PHYS_OBJECT)
5095 return -EINVAL;
5096
Daniel Vetter23010e42010-03-08 13:35:02 +01005097 obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10005098
5099 if (obj_priv->phys_obj) {
5100 if (obj_priv->phys_obj->id == id)
5101 return 0;
5102 i915_gem_detach_phys_object(dev, obj);
5103 }
5104
Dave Airlie71acb5e2008-12-30 20:31:46 +10005105 /* create a new object */
5106 if (!dev_priv->mm.phys_objs[id - 1]) {
5107 ret = i915_gem_init_phys_object(dev, id,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01005108 obj->size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10005109 if (ret) {
Linus Torvaldsaeb565d2009-01-26 10:01:53 -08005110 DRM_ERROR("failed to init phys object %d size: %zu\n", id, obj->size);
Chris Wilsone5281cc2010-10-28 13:45:36 +01005111 return ret;
Dave Airlie71acb5e2008-12-30 20:31:46 +10005112 }
5113 }
5114
5115 /* bind to the object */
5116 obj_priv->phys_obj = dev_priv->mm.phys_objs[id - 1];
5117 obj_priv->phys_obj->cur_obj = obj;
5118
Dave Airlie71acb5e2008-12-30 20:31:46 +10005119 page_count = obj->size / PAGE_SIZE;
5120
5121 for (i = 0; i < page_count; i++) {
Chris Wilsone5281cc2010-10-28 13:45:36 +01005122 struct page *page;
5123 char *dst, *src;
Dave Airlie71acb5e2008-12-30 20:31:46 +10005124
Chris Wilsone5281cc2010-10-28 13:45:36 +01005125 page = read_cache_page_gfp(mapping, i,
5126 GFP_HIGHUSER | __GFP_RECLAIMABLE);
5127 if (IS_ERR(page))
5128 return PTR_ERR(page);
5129
Chris Wilsonff75b9b2010-10-30 22:52:31 +01005130 src = kmap_atomic(page);
Chris Wilsone5281cc2010-10-28 13:45:36 +01005131 dst = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
Dave Airlie71acb5e2008-12-30 20:31:46 +10005132 memcpy(dst, src, PAGE_SIZE);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -07005133 kunmap_atomic(src);
Chris Wilsone5281cc2010-10-28 13:45:36 +01005134
5135 mark_page_accessed(page);
5136 page_cache_release(page);
Dave Airlie71acb5e2008-12-30 20:31:46 +10005137 }
5138
5139 return 0;
Dave Airlie71acb5e2008-12-30 20:31:46 +10005140}
5141
5142static int
5143i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
5144 struct drm_i915_gem_pwrite *args,
5145 struct drm_file *file_priv)
5146{
Daniel Vetter23010e42010-03-08 13:35:02 +01005147 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsonb47b30c2010-11-08 01:12:29 +00005148 void *vaddr = obj_priv->phys_obj->handle->vaddr + args->offset;
5149 char __user *user_data = (char __user *) (uintptr_t) args->data_ptr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10005150
Chris Wilsonb47b30c2010-11-08 01:12:29 +00005151 DRM_DEBUG_DRIVER("vaddr %p, %lld\n", vaddr, args->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10005152
Chris Wilsonb47b30c2010-11-08 01:12:29 +00005153 if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) {
5154 unsigned long unwritten;
5155
5156 /* The physical object once assigned is fixed for the lifetime
5157 * of the obj, so we can safely drop the lock and continue
5158 * to access vaddr.
5159 */
5160 mutex_unlock(&dev->struct_mutex);
5161 unwritten = copy_from_user(vaddr, user_data, args->size);
5162 mutex_lock(&dev->struct_mutex);
5163 if (unwritten)
5164 return -EFAULT;
5165 }
Dave Airlie71acb5e2008-12-30 20:31:46 +10005166
Daniel Vetter40ce6572010-11-05 18:12:18 +01005167 intel_gtt_chipset_flush();
Dave Airlie71acb5e2008-12-30 20:31:46 +10005168 return 0;
5169}
Eric Anholtb9624422009-06-03 07:27:35 +00005170
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005171void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00005172{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005173 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00005174
5175 /* Clean up our request list when the client is going away, so that
5176 * later retire_requests won't dereference our soon-to-be-gone
5177 * file_priv.
5178 */
Chris Wilson1c255952010-09-26 11:03:27 +01005179 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005180 while (!list_empty(&file_priv->mm.request_list)) {
5181 struct drm_i915_gem_request *request;
5182
5183 request = list_first_entry(&file_priv->mm.request_list,
5184 struct drm_i915_gem_request,
5185 client_list);
5186 list_del(&request->client_list);
5187 request->file_priv = NULL;
5188 }
Chris Wilson1c255952010-09-26 11:03:27 +01005189 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00005190}
Chris Wilson31169712009-09-14 16:50:28 +01005191
Chris Wilson31169712009-09-14 16:50:28 +01005192static int
Chris Wilson1637ef42010-04-20 17:10:35 +01005193i915_gpu_is_active(struct drm_device *dev)
5194{
5195 drm_i915_private_t *dev_priv = dev->dev_private;
5196 int lists_empty;
5197
Chris Wilson1637ef42010-04-20 17:10:35 +01005198 lists_empty = list_empty(&dev_priv->mm.flushing_list) &&
Chris Wilson17250b72010-10-28 12:51:39 +01005199 list_empty(&dev_priv->mm.active_list);
Chris Wilson1637ef42010-04-20 17:10:35 +01005200
5201 return !lists_empty;
5202}
5203
5204static int
Chris Wilson17250b72010-10-28 12:51:39 +01005205i915_gem_inactive_shrink(struct shrinker *shrinker,
5206 int nr_to_scan,
5207 gfp_t gfp_mask)
Chris Wilson31169712009-09-14 16:50:28 +01005208{
Chris Wilson17250b72010-10-28 12:51:39 +01005209 struct drm_i915_private *dev_priv =
5210 container_of(shrinker,
5211 struct drm_i915_private,
5212 mm.inactive_shrinker);
5213 struct drm_device *dev = dev_priv->dev;
5214 struct drm_i915_gem_object *obj, *next;
5215 int cnt;
5216
5217 if (!mutex_trylock(&dev->struct_mutex))
Chris Wilsonbbe2e112010-10-28 22:35:07 +01005218 return 0;
Chris Wilson31169712009-09-14 16:50:28 +01005219
5220 /* "fast-path" to count number of available objects */
5221 if (nr_to_scan == 0) {
Chris Wilson17250b72010-10-28 12:51:39 +01005222 cnt = 0;
5223 list_for_each_entry(obj,
5224 &dev_priv->mm.inactive_list,
5225 mm_list)
5226 cnt++;
5227 mutex_unlock(&dev->struct_mutex);
5228 return cnt / 100 * sysctl_vfs_cache_pressure;
Chris Wilson31169712009-09-14 16:50:28 +01005229 }
5230
Chris Wilson1637ef42010-04-20 17:10:35 +01005231rescan:
Chris Wilson31169712009-09-14 16:50:28 +01005232 /* first scan for clean buffers */
Chris Wilson17250b72010-10-28 12:51:39 +01005233 i915_gem_retire_requests(dev);
Chris Wilson31169712009-09-14 16:50:28 +01005234
Chris Wilson17250b72010-10-28 12:51:39 +01005235 list_for_each_entry_safe(obj, next,
5236 &dev_priv->mm.inactive_list,
5237 mm_list) {
5238 if (i915_gem_object_is_purgeable(obj)) {
5239 i915_gem_object_unbind(&obj->base);
5240 if (--nr_to_scan == 0)
5241 break;
Chris Wilson31169712009-09-14 16:50:28 +01005242 }
Chris Wilson31169712009-09-14 16:50:28 +01005243 }
5244
5245 /* second pass, evict/count anything still on the inactive list */
Chris Wilson17250b72010-10-28 12:51:39 +01005246 cnt = 0;
5247 list_for_each_entry_safe(obj, next,
5248 &dev_priv->mm.inactive_list,
5249 mm_list) {
5250 if (nr_to_scan) {
5251 i915_gem_object_unbind(&obj->base);
5252 nr_to_scan--;
5253 } else
5254 cnt++;
Chris Wilson31169712009-09-14 16:50:28 +01005255 }
5256
Chris Wilson17250b72010-10-28 12:51:39 +01005257 if (nr_to_scan && i915_gpu_is_active(dev)) {
Chris Wilson1637ef42010-04-20 17:10:35 +01005258 /*
5259 * We are desperate for pages, so as a last resort, wait
5260 * for the GPU to finish and discard whatever we can.
5261 * This has a dramatic impact to reduce the number of
5262 * OOM-killer events whilst running the GPU aggressively.
5263 */
Chris Wilson17250b72010-10-28 12:51:39 +01005264 if (i915_gpu_idle(dev) == 0)
Chris Wilson1637ef42010-04-20 17:10:35 +01005265 goto rescan;
5266 }
Chris Wilson17250b72010-10-28 12:51:39 +01005267 mutex_unlock(&dev->struct_mutex);
5268 return cnt / 100 * sysctl_vfs_cache_pressure;
Chris Wilson31169712009-09-14 16:50:28 +01005269}