blob: 47c665eeaf174da1dbbc59fe6fa7dc3f1e6643e1 [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>
Zhenyu Wangf8f235e2010-08-27 11:08:57 +080037#include <linux/intel-gtt.h>
Eric Anholt673a3942008-07-30 12:06:12 -070038
Chris Wilson0f8c6d72010-11-01 12:38:44 +000039struct change_domains {
40 uint32_t invalidate_domains;
41 uint32_t flush_domains;
42 uint32_t flush_rings;
43};
44
Chris Wilsona00b10c2010-09-24 21:15:47 +010045static uint32_t i915_gem_get_gtt_alignment(struct drm_i915_gem_object *obj_priv);
46static uint32_t i915_gem_get_gtt_size(struct drm_i915_gem_object *obj_priv);
Daniel Vetterba3d8d72010-02-11 22:37:04 +010047
48static int i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj,
49 bool pipelined);
Eric Anholte47c68e2008-11-14 13:35:19 -080050static void i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj);
51static void i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj);
Eric Anholte47c68e2008-11-14 13:35:19 -080052static int i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj,
53 int write);
54static int i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
55 uint64_t offset,
56 uint64_t size);
57static void i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj);
Chris Wilson2cf34d72010-09-14 13:03:28 +010058static int i915_gem_object_wait_rendering(struct drm_gem_object *obj,
59 bool interruptible);
Jesse Barnesde151cf2008-11-12 10:03:55 -080060static int i915_gem_object_bind_to_gtt(struct drm_gem_object *obj,
Chris Wilsona00b10c2010-09-24 21:15:47 +010061 unsigned alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +010062 bool map_and_fenceable);
Jesse Barnesde151cf2008-11-12 10:03:55 -080063static void i915_gem_clear_fence_reg(struct drm_gem_object *obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +100064static int i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
65 struct drm_i915_gem_pwrite *args,
66 struct drm_file *file_priv);
Chris Wilsonbe726152010-07-23 23:18:50 +010067static void i915_gem_free_object_tail(struct drm_gem_object *obj);
Eric Anholt673a3942008-07-30 12:06:12 -070068
Chris Wilson17250b72010-10-28 12:51:39 +010069static int i915_gem_inactive_shrink(struct shrinker *shrinker,
70 int nr_to_scan,
71 gfp_t gfp_mask);
72
Chris Wilson31169712009-09-14 16:50:28 +010073
Chris Wilson73aa8082010-09-30 11:46:12 +010074/* some bookkeeping */
75static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
76 size_t size)
77{
78 dev_priv->mm.object_count++;
79 dev_priv->mm.object_memory += size;
80}
81
82static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
83 size_t size)
84{
85 dev_priv->mm.object_count--;
86 dev_priv->mm.object_memory -= size;
87}
88
89static void i915_gem_info_add_gtt(struct drm_i915_private *dev_priv,
Chris Wilsona00b10c2010-09-24 21:15:47 +010090 struct drm_i915_gem_object *obj)
Chris Wilson73aa8082010-09-30 11:46:12 +010091{
92 dev_priv->mm.gtt_count++;
Chris Wilsona00b10c2010-09-24 21:15:47 +010093 dev_priv->mm.gtt_memory += obj->gtt_space->size;
94 if (obj->gtt_offset < dev_priv->mm.gtt_mappable_end) {
Daniel Vetterfb7d5162010-10-01 22:05:20 +020095 dev_priv->mm.mappable_gtt_used +=
Chris Wilsona00b10c2010-09-24 21:15:47 +010096 min_t(size_t, obj->gtt_space->size,
97 dev_priv->mm.gtt_mappable_end - obj->gtt_offset);
Daniel Vetterfb7d5162010-10-01 22:05:20 +020098 }
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 }
111}
112
113/**
114 * Update the mappable working set counters. Call _only_ when there is a change
115 * in one of (pin|fault)_mappable and update *_mappable _before_ calling.
116 * @mappable: new state the changed mappable flag (either pin_ or fault_).
117 */
118static void
119i915_gem_info_update_mappable(struct drm_i915_private *dev_priv,
Chris Wilsona00b10c2010-09-24 21:15:47 +0100120 struct drm_i915_gem_object *obj,
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200121 bool mappable)
122{
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200123 if (mappable) {
Chris Wilsona00b10c2010-09-24 21:15:47 +0100124 if (obj->pin_mappable && obj->fault_mappable)
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200125 /* Combined state was already mappable. */
126 return;
127 dev_priv->mm.gtt_mappable_count++;
Chris Wilsona00b10c2010-09-24 21:15:47 +0100128 dev_priv->mm.gtt_mappable_memory += obj->gtt_space->size;
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200129 } else {
Chris Wilsona00b10c2010-09-24 21:15:47 +0100130 if (obj->pin_mappable || obj->fault_mappable)
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200131 /* Combined state still mappable. */
132 return;
133 dev_priv->mm.gtt_mappable_count--;
Chris Wilsona00b10c2010-09-24 21:15:47 +0100134 dev_priv->mm.gtt_mappable_memory -= obj->gtt_space->size;
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200135 }
Chris Wilson73aa8082010-09-30 11:46:12 +0100136}
137
138static void i915_gem_info_add_pin(struct drm_i915_private *dev_priv,
Chris Wilsona00b10c2010-09-24 21:15:47 +0100139 struct drm_i915_gem_object *obj,
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200140 bool mappable)
Chris Wilson73aa8082010-09-30 11:46:12 +0100141{
142 dev_priv->mm.pin_count++;
Chris Wilsona00b10c2010-09-24 21:15:47 +0100143 dev_priv->mm.pin_memory += obj->gtt_space->size;
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200144 if (mappable) {
Chris Wilsona00b10c2010-09-24 21:15:47 +0100145 obj->pin_mappable = true;
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200146 i915_gem_info_update_mappable(dev_priv, obj, true);
147 }
Chris Wilson73aa8082010-09-30 11:46:12 +0100148}
149
150static void i915_gem_info_remove_pin(struct drm_i915_private *dev_priv,
Chris Wilsona00b10c2010-09-24 21:15:47 +0100151 struct drm_i915_gem_object *obj)
Chris Wilson73aa8082010-09-30 11:46:12 +0100152{
153 dev_priv->mm.pin_count--;
Chris Wilsona00b10c2010-09-24 21:15:47 +0100154 dev_priv->mm.pin_memory -= obj->gtt_space->size;
155 if (obj->pin_mappable) {
156 obj->pin_mappable = false;
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200157 i915_gem_info_update_mappable(dev_priv, obj, false);
158 }
Chris Wilson73aa8082010-09-30 11:46:12 +0100159}
160
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100161int
162i915_gem_check_is_wedged(struct drm_device *dev)
163{
164 struct drm_i915_private *dev_priv = dev->dev_private;
165 struct completion *x = &dev_priv->error_completion;
166 unsigned long flags;
167 int ret;
168
169 if (!atomic_read(&dev_priv->mm.wedged))
170 return 0;
171
172 ret = wait_for_completion_interruptible(x);
173 if (ret)
174 return ret;
175
176 /* Success, we reset the GPU! */
177 if (!atomic_read(&dev_priv->mm.wedged))
178 return 0;
179
180 /* GPU is hung, bump the completion count to account for
181 * the token we just consumed so that we never hit zero and
182 * end up waiting upon a subsequent completion event that
183 * will never happen.
184 */
185 spin_lock_irqsave(&x->wait.lock, flags);
186 x->done++;
187 spin_unlock_irqrestore(&x->wait.lock, flags);
188 return -EIO;
189}
190
Chris Wilson76c1dec2010-09-25 11:22:51 +0100191static int i915_mutex_lock_interruptible(struct drm_device *dev)
192{
193 struct drm_i915_private *dev_priv = dev->dev_private;
194 int ret;
195
196 ret = i915_gem_check_is_wedged(dev);
197 if (ret)
198 return ret;
199
200 ret = mutex_lock_interruptible(&dev->struct_mutex);
201 if (ret)
202 return ret;
203
204 if (atomic_read(&dev_priv->mm.wedged)) {
205 mutex_unlock(&dev->struct_mutex);
206 return -EAGAIN;
207 }
208
Chris Wilson23bc5982010-09-29 16:10:57 +0100209 WARN_ON(i915_verify_lists(dev));
Chris Wilson76c1dec2010-09-25 11:22:51 +0100210 return 0;
211}
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100212
Chris Wilson7d1c4802010-08-07 21:45:03 +0100213static inline bool
214i915_gem_object_is_inactive(struct drm_i915_gem_object *obj_priv)
215{
216 return obj_priv->gtt_space &&
217 !obj_priv->active &&
218 obj_priv->pin_count == 0;
219}
220
Chris Wilson73aa8082010-09-30 11:46:12 +0100221int i915_gem_do_init(struct drm_device *dev,
222 unsigned long start,
Daniel Vetter53984632010-09-22 23:44:24 +0200223 unsigned long mappable_end,
Jesse Barnes79e53942008-11-07 14:24:08 -0800224 unsigned long end)
225{
226 drm_i915_private_t *dev_priv = dev->dev_private;
227
228 if (start >= end ||
229 (start & (PAGE_SIZE - 1)) != 0 ||
230 (end & (PAGE_SIZE - 1)) != 0) {
231 return -EINVAL;
232 }
233
234 drm_mm_init(&dev_priv->mm.gtt_space, start,
235 end - start);
236
Chris Wilson73aa8082010-09-30 11:46:12 +0100237 dev_priv->mm.gtt_total = end - start;
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200238 dev_priv->mm.mappable_gtt_total = min(end, mappable_end) - start;
Daniel Vetter53984632010-09-22 23:44:24 +0200239 dev_priv->mm.gtt_mappable_end = mappable_end;
Jesse Barnes79e53942008-11-07 14:24:08 -0800240
241 return 0;
242}
Keith Packard6dbe2772008-10-14 21:41:13 -0700243
Eric Anholt673a3942008-07-30 12:06:12 -0700244int
245i915_gem_init_ioctl(struct drm_device *dev, void *data,
246 struct drm_file *file_priv)
247{
Eric Anholt673a3942008-07-30 12:06:12 -0700248 struct drm_i915_gem_init *args = data;
Jesse Barnes79e53942008-11-07 14:24:08 -0800249 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700250
251 mutex_lock(&dev->struct_mutex);
Daniel Vetter53984632010-09-22 23:44:24 +0200252 ret = i915_gem_do_init(dev, args->gtt_start, args->gtt_end, args->gtt_end);
Eric Anholt673a3942008-07-30 12:06:12 -0700253 mutex_unlock(&dev->struct_mutex);
254
Jesse Barnes79e53942008-11-07 14:24:08 -0800255 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700256}
257
Eric Anholt5a125c32008-10-22 21:40:13 -0700258int
259i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
260 struct drm_file *file_priv)
261{
Chris Wilson73aa8082010-09-30 11:46:12 +0100262 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt5a125c32008-10-22 21:40:13 -0700263 struct drm_i915_gem_get_aperture *args = data;
Eric Anholt5a125c32008-10-22 21:40:13 -0700264
265 if (!(dev->driver->driver_features & DRIVER_GEM))
266 return -ENODEV;
267
Chris Wilson73aa8082010-09-30 11:46:12 +0100268 mutex_lock(&dev->struct_mutex);
269 args->aper_size = dev_priv->mm.gtt_total;
270 args->aper_available_size = args->aper_size - dev_priv->mm.pin_memory;
271 mutex_unlock(&dev->struct_mutex);
Eric Anholt5a125c32008-10-22 21:40:13 -0700272
273 return 0;
274}
275
Eric Anholt673a3942008-07-30 12:06:12 -0700276
277/**
278 * Creates a new mm object and returns a handle to it.
279 */
280int
281i915_gem_create_ioctl(struct drm_device *dev, void *data,
282 struct drm_file *file_priv)
283{
284 struct drm_i915_gem_create *args = data;
285 struct drm_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300286 int ret;
287 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700288
289 args->size = roundup(args->size, PAGE_SIZE);
290
291 /* Allocate the new object */
Daniel Vetterac52bc52010-04-09 19:05:06 +0000292 obj = i915_gem_alloc_object(dev, args->size);
Eric Anholt673a3942008-07-30 12:06:12 -0700293 if (obj == NULL)
294 return -ENOMEM;
295
296 ret = drm_gem_handle_create(file_priv, obj, &handle);
Chris Wilson1dfd9752010-09-06 14:44:14 +0100297 if (ret) {
Chris Wilson202f2fe2010-10-14 13:20:40 +0100298 drm_gem_object_release(obj);
299 i915_gem_info_remove_obj(dev->dev_private, obj->size);
300 kfree(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700301 return ret;
Chris Wilson1dfd9752010-09-06 14:44:14 +0100302 }
303
Chris Wilson202f2fe2010-10-14 13:20:40 +0100304 /* drop reference from allocate - handle holds it now */
305 drm_gem_object_unreference(obj);
306 trace_i915_gem_object_create(obj);
307
Eric Anholt673a3942008-07-30 12:06:12 -0700308 args->handle = handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700309 return 0;
310}
311
Eric Anholt280b7132009-03-12 16:56:27 -0700312static int i915_gem_object_needs_bit17_swizzle(struct drm_gem_object *obj)
313{
314 drm_i915_private_t *dev_priv = obj->dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +0100315 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt280b7132009-03-12 16:56:27 -0700316
317 return dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_9_10_17 &&
318 obj_priv->tiling_mode != I915_TILING_NONE;
319}
320
Chris Wilson99a03df2010-05-27 14:15:34 +0100321static inline void
Eric Anholt40123c12009-03-09 13:42:30 -0700322slow_shmem_copy(struct page *dst_page,
323 int dst_offset,
324 struct page *src_page,
325 int src_offset,
326 int length)
327{
328 char *dst_vaddr, *src_vaddr;
329
Chris Wilson99a03df2010-05-27 14:15:34 +0100330 dst_vaddr = kmap(dst_page);
331 src_vaddr = kmap(src_page);
Eric Anholt40123c12009-03-09 13:42:30 -0700332
333 memcpy(dst_vaddr + dst_offset, src_vaddr + src_offset, length);
334
Chris Wilson99a03df2010-05-27 14:15:34 +0100335 kunmap(src_page);
336 kunmap(dst_page);
Eric Anholt40123c12009-03-09 13:42:30 -0700337}
338
Chris Wilson99a03df2010-05-27 14:15:34 +0100339static inline void
Eric Anholt280b7132009-03-12 16:56:27 -0700340slow_shmem_bit17_copy(struct page *gpu_page,
341 int gpu_offset,
342 struct page *cpu_page,
343 int cpu_offset,
344 int length,
345 int is_read)
346{
347 char *gpu_vaddr, *cpu_vaddr;
348
349 /* Use the unswizzled path if this page isn't affected. */
350 if ((page_to_phys(gpu_page) & (1 << 17)) == 0) {
351 if (is_read)
352 return slow_shmem_copy(cpu_page, cpu_offset,
353 gpu_page, gpu_offset, length);
354 else
355 return slow_shmem_copy(gpu_page, gpu_offset,
356 cpu_page, cpu_offset, length);
357 }
358
Chris Wilson99a03df2010-05-27 14:15:34 +0100359 gpu_vaddr = kmap(gpu_page);
360 cpu_vaddr = kmap(cpu_page);
Eric Anholt280b7132009-03-12 16:56:27 -0700361
362 /* Copy the data, XORing A6 with A17 (1). The user already knows he's
363 * XORing with the other bits (A9 for Y, A9 and A10 for X)
364 */
365 while (length > 0) {
366 int cacheline_end = ALIGN(gpu_offset + 1, 64);
367 int this_length = min(cacheline_end - gpu_offset, length);
368 int swizzled_gpu_offset = gpu_offset ^ 64;
369
370 if (is_read) {
371 memcpy(cpu_vaddr + cpu_offset,
372 gpu_vaddr + swizzled_gpu_offset,
373 this_length);
374 } else {
375 memcpy(gpu_vaddr + swizzled_gpu_offset,
376 cpu_vaddr + cpu_offset,
377 this_length);
378 }
379 cpu_offset += this_length;
380 gpu_offset += this_length;
381 length -= this_length;
382 }
383
Chris Wilson99a03df2010-05-27 14:15:34 +0100384 kunmap(cpu_page);
385 kunmap(gpu_page);
Eric Anholt280b7132009-03-12 16:56:27 -0700386}
387
Eric Anholt673a3942008-07-30 12:06:12 -0700388/**
Eric Anholteb014592009-03-10 11:44:52 -0700389 * This is the fast shmem pread path, which attempts to copy_from_user directly
390 * from the backing pages of the object to the user's address space. On a
391 * fault, it fails so we can fall back to i915_gem_shmem_pwrite_slow().
392 */
393static int
394i915_gem_shmem_pread_fast(struct drm_device *dev, struct drm_gem_object *obj,
395 struct drm_i915_gem_pread *args,
396 struct drm_file *file_priv)
397{
Daniel Vetter23010e42010-03-08 13:35:02 +0100398 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsone5281cc2010-10-28 13:45:36 +0100399 struct address_space *mapping = obj->filp->f_path.dentry->d_inode->i_mapping;
Eric Anholteb014592009-03-10 11:44:52 -0700400 ssize_t remain;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100401 loff_t offset;
Eric Anholteb014592009-03-10 11:44:52 -0700402 char __user *user_data;
403 int page_offset, page_length;
Eric Anholteb014592009-03-10 11:44:52 -0700404
405 user_data = (char __user *) (uintptr_t) args->data_ptr;
406 remain = args->size;
407
Daniel Vetter23010e42010-03-08 13:35:02 +0100408 obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700409 offset = args->offset;
410
411 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100412 struct page *page;
413 char *vaddr;
414 int ret;
415
Eric Anholteb014592009-03-10 11:44:52 -0700416 /* Operation in this page
417 *
Eric Anholteb014592009-03-10 11:44:52 -0700418 * page_offset = offset within page
419 * page_length = bytes to copy for this page
420 */
Eric Anholteb014592009-03-10 11:44:52 -0700421 page_offset = offset & (PAGE_SIZE-1);
422 page_length = remain;
423 if ((page_offset + remain) > PAGE_SIZE)
424 page_length = PAGE_SIZE - page_offset;
425
Chris Wilsone5281cc2010-10-28 13:45:36 +0100426 page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
427 GFP_HIGHUSER | __GFP_RECLAIMABLE);
428 if (IS_ERR(page))
429 return PTR_ERR(page);
430
431 vaddr = kmap_atomic(page);
432 ret = __copy_to_user_inatomic(user_data,
433 vaddr + page_offset,
434 page_length);
435 kunmap_atomic(vaddr);
436
437 mark_page_accessed(page);
438 page_cache_release(page);
439 if (ret)
Chris Wilson4f27b752010-10-14 15:26:45 +0100440 return -EFAULT;
Eric Anholteb014592009-03-10 11:44:52 -0700441
442 remain -= page_length;
443 user_data += page_length;
444 offset += page_length;
445 }
446
Chris Wilson4f27b752010-10-14 15:26:45 +0100447 return 0;
Eric Anholteb014592009-03-10 11:44:52 -0700448}
449
450/**
451 * This is the fallback shmem pread path, which allocates temporary storage
452 * in kernel space to copy_to_user into outside of the struct_mutex, so we
453 * can copy out of the object's backing pages while holding the struct mutex
454 * and not take page faults.
455 */
456static int
457i915_gem_shmem_pread_slow(struct drm_device *dev, struct drm_gem_object *obj,
458 struct drm_i915_gem_pread *args,
459 struct drm_file *file_priv)
460{
Chris Wilsone5281cc2010-10-28 13:45:36 +0100461 struct address_space *mapping = obj->filp->f_path.dentry->d_inode->i_mapping;
Daniel Vetter23010e42010-03-08 13:35:02 +0100462 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700463 struct mm_struct *mm = current->mm;
464 struct page **user_pages;
465 ssize_t remain;
466 loff_t offset, pinned_pages, i;
467 loff_t first_data_page, last_data_page, num_pages;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100468 int shmem_page_offset;
469 int data_page_index, data_page_offset;
Eric Anholteb014592009-03-10 11:44:52 -0700470 int page_length;
471 int ret;
472 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700473 int do_bit17_swizzling;
Eric Anholteb014592009-03-10 11:44:52 -0700474
475 remain = args->size;
476
477 /* Pin the user pages containing the data. We can't fault while
478 * holding the struct mutex, yet we want to hold it while
479 * dereferencing the user data.
480 */
481 first_data_page = data_ptr / PAGE_SIZE;
482 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
483 num_pages = last_data_page - first_data_page + 1;
484
Chris Wilson4f27b752010-10-14 15:26:45 +0100485 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
Eric Anholteb014592009-03-10 11:44:52 -0700486 if (user_pages == NULL)
487 return -ENOMEM;
488
Chris Wilson4f27b752010-10-14 15:26:45 +0100489 mutex_unlock(&dev->struct_mutex);
Eric Anholteb014592009-03-10 11:44:52 -0700490 down_read(&mm->mmap_sem);
491 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
Eric Anholte5e9ecd2009-04-07 16:01:22 -0700492 num_pages, 1, 0, user_pages, NULL);
Eric Anholteb014592009-03-10 11:44:52 -0700493 up_read(&mm->mmap_sem);
Chris Wilson4f27b752010-10-14 15:26:45 +0100494 mutex_lock(&dev->struct_mutex);
Eric Anholteb014592009-03-10 11:44:52 -0700495 if (pinned_pages < num_pages) {
496 ret = -EFAULT;
Chris Wilson4f27b752010-10-14 15:26:45 +0100497 goto out;
Eric Anholteb014592009-03-10 11:44:52 -0700498 }
499
Chris Wilson4f27b752010-10-14 15:26:45 +0100500 ret = i915_gem_object_set_cpu_read_domain_range(obj,
501 args->offset,
Eric Anholteb014592009-03-10 11:44:52 -0700502 args->size);
Chris Wilson4f27b752010-10-14 15:26:45 +0100503 if (ret)
504 goto out;
505
506 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700507
Daniel Vetter23010e42010-03-08 13:35:02 +0100508 obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700509 offset = args->offset;
510
511 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100512 struct page *page;
513
Eric Anholteb014592009-03-10 11:44:52 -0700514 /* Operation in this page
515 *
Eric Anholteb014592009-03-10 11:44:52 -0700516 * shmem_page_offset = offset within page in shmem file
517 * data_page_index = page number in get_user_pages return
518 * data_page_offset = offset with data_page_index page.
519 * page_length = bytes to copy for this page
520 */
Eric Anholteb014592009-03-10 11:44:52 -0700521 shmem_page_offset = offset & ~PAGE_MASK;
522 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
523 data_page_offset = data_ptr & ~PAGE_MASK;
524
525 page_length = remain;
526 if ((shmem_page_offset + page_length) > PAGE_SIZE)
527 page_length = PAGE_SIZE - shmem_page_offset;
528 if ((data_page_offset + page_length) > PAGE_SIZE)
529 page_length = PAGE_SIZE - data_page_offset;
530
Chris Wilsone5281cc2010-10-28 13:45:36 +0100531 page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
532 GFP_HIGHUSER | __GFP_RECLAIMABLE);
533 if (IS_ERR(page))
534 return PTR_ERR(page);
535
Eric Anholt280b7132009-03-12 16:56:27 -0700536 if (do_bit17_swizzling) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100537 slow_shmem_bit17_copy(page,
Eric Anholt280b7132009-03-12 16:56:27 -0700538 shmem_page_offset,
Chris Wilson99a03df2010-05-27 14:15:34 +0100539 user_pages[data_page_index],
540 data_page_offset,
541 page_length,
542 1);
543 } else {
544 slow_shmem_copy(user_pages[data_page_index],
545 data_page_offset,
Chris Wilsone5281cc2010-10-28 13:45:36 +0100546 page,
Chris Wilson99a03df2010-05-27 14:15:34 +0100547 shmem_page_offset,
548 page_length);
Eric Anholt280b7132009-03-12 16:56:27 -0700549 }
Eric Anholteb014592009-03-10 11:44:52 -0700550
Chris Wilsone5281cc2010-10-28 13:45:36 +0100551 mark_page_accessed(page);
552 page_cache_release(page);
553
Eric Anholteb014592009-03-10 11:44:52 -0700554 remain -= page_length;
555 data_ptr += page_length;
556 offset += page_length;
557 }
558
Chris Wilson4f27b752010-10-14 15:26:45 +0100559out:
Eric Anholteb014592009-03-10 11:44:52 -0700560 for (i = 0; i < pinned_pages; i++) {
561 SetPageDirty(user_pages[i]);
Chris Wilsone5281cc2010-10-28 13:45:36 +0100562 mark_page_accessed(user_pages[i]);
Eric Anholteb014592009-03-10 11:44:52 -0700563 page_cache_release(user_pages[i]);
564 }
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700565 drm_free_large(user_pages);
Eric Anholteb014592009-03-10 11:44:52 -0700566
567 return ret;
568}
569
Eric Anholt673a3942008-07-30 12:06:12 -0700570/**
571 * Reads data from the object referenced by handle.
572 *
573 * On error, the contents of *data are undefined.
574 */
575int
576i915_gem_pread_ioctl(struct drm_device *dev, void *data,
577 struct drm_file *file_priv)
578{
579 struct drm_i915_gem_pread *args = data;
580 struct drm_gem_object *obj;
581 struct drm_i915_gem_object *obj_priv;
Chris Wilson35b62a82010-09-26 20:23:38 +0100582 int ret = 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700583
Chris Wilson4f27b752010-10-14 15:26:45 +0100584 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100585 if (ret)
Chris Wilson4f27b752010-10-14 15:26:45 +0100586 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700587
588 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100589 if (obj == NULL) {
590 ret = -ENOENT;
591 goto unlock;
Chris Wilson4f27b752010-10-14 15:26:45 +0100592 }
Daniel Vetter23010e42010-03-08 13:35:02 +0100593 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700594
Chris Wilson7dcd2492010-09-26 20:21:44 +0100595 /* Bounds check source. */
596 if (args->offset > obj->size || args->size > obj->size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +0100597 ret = -EINVAL;
Chris Wilson35b62a82010-09-26 20:23:38 +0100598 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +0100599 }
600
Chris Wilson35b62a82010-09-26 20:23:38 +0100601 if (args->size == 0)
602 goto out;
603
Chris Wilsonce9d4192010-09-26 20:50:05 +0100604 if (!access_ok(VERIFY_WRITE,
605 (char __user *)(uintptr_t)args->data_ptr,
606 args->size)) {
607 ret = -EFAULT;
Chris Wilson35b62a82010-09-26 20:23:38 +0100608 goto out;
Eric Anholt673a3942008-07-30 12:06:12 -0700609 }
610
Chris Wilsonb5e4feb2010-10-14 13:47:43 +0100611 ret = fault_in_pages_writeable((char __user *)(uintptr_t)args->data_ptr,
612 args->size);
613 if (ret) {
614 ret = -EFAULT;
615 goto out;
616 }
617
Chris Wilson4f27b752010-10-14 15:26:45 +0100618 ret = i915_gem_object_set_cpu_read_domain_range(obj,
619 args->offset,
620 args->size);
621 if (ret)
Chris Wilsone5281cc2010-10-28 13:45:36 +0100622 goto out;
Chris Wilson4f27b752010-10-14 15:26:45 +0100623
624 ret = -EFAULT;
625 if (!i915_gem_object_needs_bit17_swizzle(obj))
Eric Anholt280b7132009-03-12 16:56:27 -0700626 ret = i915_gem_shmem_pread_fast(dev, obj, args, file_priv);
Chris Wilson4f27b752010-10-14 15:26:45 +0100627 if (ret == -EFAULT)
628 ret = i915_gem_shmem_pread_slow(dev, obj, args, file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -0700629
Chris Wilson35b62a82010-09-26 20:23:38 +0100630out:
Chris Wilson4f27b752010-10-14 15:26:45 +0100631 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100632unlock:
Chris Wilson4f27b752010-10-14 15:26:45 +0100633 mutex_unlock(&dev->struct_mutex);
Eric Anholteb014592009-03-10 11:44:52 -0700634 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700635}
636
Keith Packard0839ccb2008-10-30 19:38:48 -0700637/* This is the fast write path which cannot handle
638 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700639 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700640
Keith Packard0839ccb2008-10-30 19:38:48 -0700641static inline int
642fast_user_write(struct io_mapping *mapping,
643 loff_t page_base, int page_offset,
644 char __user *user_data,
645 int length)
646{
647 char *vaddr_atomic;
648 unsigned long unwritten;
649
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700650 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
Keith Packard0839ccb2008-10-30 19:38:48 -0700651 unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
652 user_data, length);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700653 io_mapping_unmap_atomic(vaddr_atomic);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100654 return unwritten;
Keith Packard0839ccb2008-10-30 19:38:48 -0700655}
656
657/* Here's the write path which can sleep for
658 * page faults
659 */
660
Chris Wilsonab34c222010-05-27 14:15:35 +0100661static inline void
Eric Anholt3de09aa2009-03-09 09:42:23 -0700662slow_kernel_write(struct io_mapping *mapping,
663 loff_t gtt_base, int gtt_offset,
664 struct page *user_page, int user_offset,
665 int length)
Keith Packard0839ccb2008-10-30 19:38:48 -0700666{
Chris Wilsonab34c222010-05-27 14:15:35 +0100667 char __iomem *dst_vaddr;
668 char *src_vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -0700669
Chris Wilsonab34c222010-05-27 14:15:35 +0100670 dst_vaddr = io_mapping_map_wc(mapping, gtt_base);
671 src_vaddr = kmap(user_page);
672
673 memcpy_toio(dst_vaddr + gtt_offset,
674 src_vaddr + user_offset,
675 length);
676
677 kunmap(user_page);
678 io_mapping_unmap(dst_vaddr);
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700679}
680
Eric Anholt3de09aa2009-03-09 09:42:23 -0700681/**
682 * This is the fast pwrite path, where we copy the data directly from the
683 * user into the GTT, uncached.
684 */
Eric Anholt673a3942008-07-30 12:06:12 -0700685static int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700686i915_gem_gtt_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
687 struct drm_i915_gem_pwrite *args,
688 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700689{
Daniel Vetter23010e42010-03-08 13:35:02 +0100690 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Keith Packard0839ccb2008-10-30 19:38:48 -0700691 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700692 ssize_t remain;
Keith Packard0839ccb2008-10-30 19:38:48 -0700693 loff_t offset, page_base;
Eric Anholt673a3942008-07-30 12:06:12 -0700694 char __user *user_data;
Keith Packard0839ccb2008-10-30 19:38:48 -0700695 int page_offset, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700696
697 user_data = (char __user *) (uintptr_t) args->data_ptr;
698 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700699
Daniel Vetter23010e42010-03-08 13:35:02 +0100700 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700701 offset = obj_priv->gtt_offset + args->offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700702
703 while (remain > 0) {
704 /* Operation in this page
705 *
Keith Packard0839ccb2008-10-30 19:38:48 -0700706 * page_base = page offset within aperture
707 * page_offset = offset within page
708 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -0700709 */
Keith Packard0839ccb2008-10-30 19:38:48 -0700710 page_base = (offset & ~(PAGE_SIZE-1));
711 page_offset = offset & (PAGE_SIZE-1);
712 page_length = remain;
713 if ((page_offset + remain) > PAGE_SIZE)
714 page_length = PAGE_SIZE - page_offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700715
Keith Packard0839ccb2008-10-30 19:38:48 -0700716 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -0700717 * source page isn't available. Return the error and we'll
718 * retry in the slow path.
Keith Packard0839ccb2008-10-30 19:38:48 -0700719 */
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100720 if (fast_user_write(dev_priv->mm.gtt_mapping, page_base,
721 page_offset, user_data, page_length))
722
723 return -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -0700724
Keith Packard0839ccb2008-10-30 19:38:48 -0700725 remain -= page_length;
726 user_data += page_length;
727 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700728 }
Eric Anholt673a3942008-07-30 12:06:12 -0700729
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100730 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700731}
732
Eric Anholt3de09aa2009-03-09 09:42:23 -0700733/**
734 * This is the fallback GTT pwrite path, which uses get_user_pages to pin
735 * the memory and maps it using kmap_atomic for copying.
736 *
737 * This code resulted in x11perf -rgb10text consuming about 10% more CPU
738 * than using i915_gem_gtt_pwrite_fast on a G45 (32-bit).
739 */
Eric Anholt3043c602008-10-02 12:24:47 -0700740static int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700741i915_gem_gtt_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
742 struct drm_i915_gem_pwrite *args,
743 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700744{
Daniel Vetter23010e42010-03-08 13:35:02 +0100745 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700746 drm_i915_private_t *dev_priv = dev->dev_private;
747 ssize_t remain;
748 loff_t gtt_page_base, offset;
749 loff_t first_data_page, last_data_page, num_pages;
750 loff_t pinned_pages, i;
751 struct page **user_pages;
752 struct mm_struct *mm = current->mm;
753 int gtt_page_offset, data_page_offset, data_page_index, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700754 int ret;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700755 uint64_t data_ptr = args->data_ptr;
756
757 remain = args->size;
758
759 /* Pin the user pages containing the data. We can't fault while
760 * holding the struct mutex, and all of the pwrite implementations
761 * want to hold it while dereferencing the user data.
762 */
763 first_data_page = data_ptr / PAGE_SIZE;
764 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
765 num_pages = last_data_page - first_data_page + 1;
766
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100767 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
Eric Anholt3de09aa2009-03-09 09:42:23 -0700768 if (user_pages == NULL)
769 return -ENOMEM;
770
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100771 mutex_unlock(&dev->struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700772 down_read(&mm->mmap_sem);
773 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
774 num_pages, 0, 0, user_pages, NULL);
775 up_read(&mm->mmap_sem);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100776 mutex_lock(&dev->struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700777 if (pinned_pages < num_pages) {
778 ret = -EFAULT;
779 goto out_unpin_pages;
780 }
781
Eric Anholt3de09aa2009-03-09 09:42:23 -0700782 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
783 if (ret)
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100784 goto out_unpin_pages;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700785
Daniel Vetter23010e42010-03-08 13:35:02 +0100786 obj_priv = to_intel_bo(obj);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700787 offset = obj_priv->gtt_offset + args->offset;
788
789 while (remain > 0) {
790 /* Operation in this page
791 *
792 * gtt_page_base = page offset within aperture
793 * gtt_page_offset = offset within page in aperture
794 * data_page_index = page number in get_user_pages return
795 * data_page_offset = offset with data_page_index page.
796 * page_length = bytes to copy for this page
797 */
798 gtt_page_base = offset & PAGE_MASK;
799 gtt_page_offset = offset & ~PAGE_MASK;
800 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
801 data_page_offset = data_ptr & ~PAGE_MASK;
802
803 page_length = remain;
804 if ((gtt_page_offset + page_length) > PAGE_SIZE)
805 page_length = PAGE_SIZE - gtt_page_offset;
806 if ((data_page_offset + page_length) > PAGE_SIZE)
807 page_length = PAGE_SIZE - data_page_offset;
808
Chris Wilsonab34c222010-05-27 14:15:35 +0100809 slow_kernel_write(dev_priv->mm.gtt_mapping,
810 gtt_page_base, gtt_page_offset,
811 user_pages[data_page_index],
812 data_page_offset,
813 page_length);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700814
815 remain -= page_length;
816 offset += page_length;
817 data_ptr += page_length;
818 }
819
Eric Anholt3de09aa2009-03-09 09:42:23 -0700820out_unpin_pages:
821 for (i = 0; i < pinned_pages; i++)
822 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700823 drm_free_large(user_pages);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700824
825 return ret;
826}
827
Eric Anholt40123c12009-03-09 13:42:30 -0700828/**
829 * This is the fast shmem pwrite path, which attempts to directly
830 * copy_from_user into the kmapped pages backing the object.
831 */
Eric Anholt673a3942008-07-30 12:06:12 -0700832static int
Eric Anholt40123c12009-03-09 13:42:30 -0700833i915_gem_shmem_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
834 struct drm_i915_gem_pwrite *args,
835 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700836{
Chris Wilsone5281cc2010-10-28 13:45:36 +0100837 struct address_space *mapping = obj->filp->f_path.dentry->d_inode->i_mapping;
Daniel Vetter23010e42010-03-08 13:35:02 +0100838 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700839 ssize_t remain;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100840 loff_t offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700841 char __user *user_data;
842 int page_offset, page_length;
Eric Anholt40123c12009-03-09 13:42:30 -0700843
844 user_data = (char __user *) (uintptr_t) args->data_ptr;
845 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700846
Daniel Vetter23010e42010-03-08 13:35:02 +0100847 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700848 offset = args->offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700849 obj_priv->dirty = 1;
Eric Anholt673a3942008-07-30 12:06:12 -0700850
Eric Anholt40123c12009-03-09 13:42:30 -0700851 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100852 struct page *page;
853 char *vaddr;
854 int ret;
855
Eric Anholt40123c12009-03-09 13:42:30 -0700856 /* Operation in this page
857 *
Eric Anholt40123c12009-03-09 13:42:30 -0700858 * page_offset = offset within page
859 * page_length = bytes to copy for this page
860 */
Eric Anholt40123c12009-03-09 13:42:30 -0700861 page_offset = offset & (PAGE_SIZE-1);
862 page_length = remain;
863 if ((page_offset + remain) > PAGE_SIZE)
864 page_length = PAGE_SIZE - page_offset;
865
Chris Wilsone5281cc2010-10-28 13:45:36 +0100866 page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
867 GFP_HIGHUSER | __GFP_RECLAIMABLE);
868 if (IS_ERR(page))
869 return PTR_ERR(page);
870
871 vaddr = kmap_atomic(page, KM_USER0);
872 ret = __copy_from_user_inatomic(vaddr + page_offset,
873 user_data,
874 page_length);
875 kunmap_atomic(vaddr, KM_USER0);
876
877 set_page_dirty(page);
878 mark_page_accessed(page);
879 page_cache_release(page);
880
881 /* If we get a fault while copying data, then (presumably) our
882 * source page isn't available. Return the error and we'll
883 * retry in the slow path.
884 */
885 if (ret)
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100886 return -EFAULT;
Eric Anholt40123c12009-03-09 13:42:30 -0700887
888 remain -= page_length;
889 user_data += page_length;
890 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700891 }
892
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100893 return 0;
Eric Anholt40123c12009-03-09 13:42:30 -0700894}
895
896/**
897 * This is the fallback shmem pwrite path, which uses get_user_pages to pin
898 * the memory and maps it using kmap_atomic for copying.
899 *
900 * This avoids taking mmap_sem for faulting on the user's address while the
901 * struct_mutex is held.
902 */
903static int
904i915_gem_shmem_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
905 struct drm_i915_gem_pwrite *args,
906 struct drm_file *file_priv)
907{
Chris Wilsone5281cc2010-10-28 13:45:36 +0100908 struct address_space *mapping = obj->filp->f_path.dentry->d_inode->i_mapping;
Daniel Vetter23010e42010-03-08 13:35:02 +0100909 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700910 struct mm_struct *mm = current->mm;
911 struct page **user_pages;
912 ssize_t remain;
913 loff_t offset, pinned_pages, i;
914 loff_t first_data_page, last_data_page, num_pages;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100915 int shmem_page_offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700916 int data_page_index, data_page_offset;
917 int page_length;
918 int ret;
919 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700920 int do_bit17_swizzling;
Eric Anholt40123c12009-03-09 13:42:30 -0700921
922 remain = args->size;
923
924 /* Pin the user pages containing the data. We can't fault while
925 * holding the struct mutex, and all of the pwrite implementations
926 * want to hold it while dereferencing the user data.
927 */
928 first_data_page = data_ptr / PAGE_SIZE;
929 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
930 num_pages = last_data_page - first_data_page + 1;
931
Chris Wilson4f27b752010-10-14 15:26:45 +0100932 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
Eric Anholt40123c12009-03-09 13:42:30 -0700933 if (user_pages == NULL)
934 return -ENOMEM;
935
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100936 mutex_unlock(&dev->struct_mutex);
Eric Anholt40123c12009-03-09 13:42:30 -0700937 down_read(&mm->mmap_sem);
938 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
939 num_pages, 0, 0, user_pages, NULL);
940 up_read(&mm->mmap_sem);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100941 mutex_lock(&dev->struct_mutex);
Eric Anholt40123c12009-03-09 13:42:30 -0700942 if (pinned_pages < num_pages) {
943 ret = -EFAULT;
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100944 goto out;
Eric Anholt40123c12009-03-09 13:42:30 -0700945 }
946
Eric Anholt40123c12009-03-09 13:42:30 -0700947 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100948 if (ret)
949 goto out;
950
951 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700952
Daniel Vetter23010e42010-03-08 13:35:02 +0100953 obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700954 offset = args->offset;
955 obj_priv->dirty = 1;
956
957 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100958 struct page *page;
959
Eric Anholt40123c12009-03-09 13:42:30 -0700960 /* Operation in this page
961 *
Eric Anholt40123c12009-03-09 13:42:30 -0700962 * shmem_page_offset = offset within page in shmem file
963 * data_page_index = page number in get_user_pages return
964 * data_page_offset = offset with data_page_index page.
965 * page_length = bytes to copy for this page
966 */
Eric Anholt40123c12009-03-09 13:42:30 -0700967 shmem_page_offset = offset & ~PAGE_MASK;
968 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
969 data_page_offset = data_ptr & ~PAGE_MASK;
970
971 page_length = remain;
972 if ((shmem_page_offset + page_length) > PAGE_SIZE)
973 page_length = PAGE_SIZE - shmem_page_offset;
974 if ((data_page_offset + page_length) > PAGE_SIZE)
975 page_length = PAGE_SIZE - data_page_offset;
976
Chris Wilsone5281cc2010-10-28 13:45:36 +0100977 page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
978 GFP_HIGHUSER | __GFP_RECLAIMABLE);
979 if (IS_ERR(page)) {
980 ret = PTR_ERR(page);
981 goto out;
982 }
983
Eric Anholt280b7132009-03-12 16:56:27 -0700984 if (do_bit17_swizzling) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100985 slow_shmem_bit17_copy(page,
Eric Anholt280b7132009-03-12 16:56:27 -0700986 shmem_page_offset,
987 user_pages[data_page_index],
988 data_page_offset,
Chris Wilson99a03df2010-05-27 14:15:34 +0100989 page_length,
990 0);
991 } else {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100992 slow_shmem_copy(page,
Chris Wilson99a03df2010-05-27 14:15:34 +0100993 shmem_page_offset,
994 user_pages[data_page_index],
995 data_page_offset,
996 page_length);
Eric Anholt280b7132009-03-12 16:56:27 -0700997 }
Eric Anholt40123c12009-03-09 13:42:30 -0700998
Chris Wilsone5281cc2010-10-28 13:45:36 +0100999 set_page_dirty(page);
1000 mark_page_accessed(page);
1001 page_cache_release(page);
1002
Eric Anholt40123c12009-03-09 13:42:30 -07001003 remain -= page_length;
1004 data_ptr += page_length;
1005 offset += page_length;
1006 }
1007
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001008out:
Eric Anholt40123c12009-03-09 13:42:30 -07001009 for (i = 0; i < pinned_pages; i++)
1010 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07001011 drm_free_large(user_pages);
Eric Anholt40123c12009-03-09 13:42:30 -07001012
1013 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001014}
1015
1016/**
1017 * Writes data to the object referenced by handle.
1018 *
1019 * On error, the contents of the buffer that were to be modified are undefined.
1020 */
1021int
1022i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001023 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001024{
1025 struct drm_i915_gem_pwrite *args = data;
1026 struct drm_gem_object *obj;
1027 struct drm_i915_gem_object *obj_priv;
1028 int ret = 0;
1029
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001030 ret = i915_mutex_lock_interruptible(dev);
1031 if (ret)
1032 return ret;
1033
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001034 obj = drm_gem_object_lookup(dev, file, args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001035 if (obj == NULL) {
1036 ret = -ENOENT;
1037 goto unlock;
1038 }
Daniel Vetter23010e42010-03-08 13:35:02 +01001039 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001040
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001041
Chris Wilson7dcd2492010-09-26 20:21:44 +01001042 /* Bounds check destination. */
1043 if (args->offset > obj->size || args->size > obj->size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001044 ret = -EINVAL;
Chris Wilson35b62a82010-09-26 20:23:38 +01001045 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001046 }
1047
Chris Wilson35b62a82010-09-26 20:23:38 +01001048 if (args->size == 0)
1049 goto out;
1050
Chris Wilsonce9d4192010-09-26 20:50:05 +01001051 if (!access_ok(VERIFY_READ,
1052 (char __user *)(uintptr_t)args->data_ptr,
1053 args->size)) {
1054 ret = -EFAULT;
Chris Wilson35b62a82010-09-26 20:23:38 +01001055 goto out;
Eric Anholt673a3942008-07-30 12:06:12 -07001056 }
1057
Chris Wilsonb5e4feb2010-10-14 13:47:43 +01001058 ret = fault_in_pages_readable((char __user *)(uintptr_t)args->data_ptr,
1059 args->size);
1060 if (ret) {
1061 ret = -EFAULT;
1062 goto out;
Eric Anholt673a3942008-07-30 12:06:12 -07001063 }
1064
1065 /* We can only do the GTT pwrite on untiled buffers, as otherwise
1066 * it would end up going through the fenced access, and we'll get
1067 * different detiling behavior between reading and writing.
1068 * pread/pwrite currently are reading and writing from the CPU
1069 * perspective, requiring manual detiling by the client.
1070 */
Dave Airlie71acb5e2008-12-30 20:31:46 +10001071 if (obj_priv->phys_obj)
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001072 ret = i915_gem_phys_pwrite(dev, obj, args, file);
Dave Airlie71acb5e2008-12-30 20:31:46 +10001073 else if (obj_priv->tiling_mode == I915_TILING_NONE &&
Chris Wilson5cdf5882010-09-27 15:51:07 +01001074 obj_priv->gtt_space &&
Chris Wilson9b8c4a02010-05-27 14:21:01 +01001075 obj->write_domain != I915_GEM_DOMAIN_CPU) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01001076 ret = i915_gem_object_pin(obj, 0, true);
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001077 if (ret)
1078 goto out;
1079
1080 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
1081 if (ret)
1082 goto out_unpin;
1083
1084 ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file);
1085 if (ret == -EFAULT)
1086 ret = i915_gem_gtt_pwrite_slow(dev, obj, args, file);
1087
1088out_unpin:
1089 i915_gem_object_unpin(obj);
Eric Anholt40123c12009-03-09 13:42:30 -07001090 } else {
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001091 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
1092 if (ret)
Chris Wilsone5281cc2010-10-28 13:45:36 +01001093 goto out;
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001094
1095 ret = -EFAULT;
1096 if (!i915_gem_object_needs_bit17_swizzle(obj))
1097 ret = i915_gem_shmem_pwrite_fast(dev, obj, args, file);
1098 if (ret == -EFAULT)
1099 ret = i915_gem_shmem_pwrite_slow(dev, obj, args, file);
Eric Anholt40123c12009-03-09 13:42:30 -07001100 }
Eric Anholt673a3942008-07-30 12:06:12 -07001101
Chris Wilson35b62a82010-09-26 20:23:38 +01001102out:
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001103 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001104unlock:
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001105 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07001106 return ret;
1107}
1108
1109/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001110 * Called when user space prepares to use an object with the CPU, either
1111 * through the mmap ioctl's mapping or a GTT mapping.
Eric Anholt673a3942008-07-30 12:06:12 -07001112 */
1113int
1114i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
1115 struct drm_file *file_priv)
1116{
Eric Anholta09ba7f2009-08-29 12:49:51 -07001117 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07001118 struct drm_i915_gem_set_domain *args = data;
1119 struct drm_gem_object *obj;
Jesse Barnes652c3932009-08-17 13:31:43 -07001120 struct drm_i915_gem_object *obj_priv;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001121 uint32_t read_domains = args->read_domains;
1122 uint32_t write_domain = args->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07001123 int ret;
1124
1125 if (!(dev->driver->driver_features & DRIVER_GEM))
1126 return -ENODEV;
1127
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001128 /* Only handle setting domains to types used by the CPU. */
Chris Wilson21d509e2009-06-06 09:46:02 +01001129 if (write_domain & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001130 return -EINVAL;
1131
Chris Wilson21d509e2009-06-06 09:46:02 +01001132 if (read_domains & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001133 return -EINVAL;
1134
1135 /* Having something in the write domain implies it's in the read
1136 * domain, and only that read domain. Enforce that in the request.
1137 */
1138 if (write_domain != 0 && read_domains != write_domain)
1139 return -EINVAL;
1140
Chris Wilson76c1dec2010-09-25 11:22:51 +01001141 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001142 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001143 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001144
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001145 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1146 if (obj == NULL) {
1147 ret = -ENOENT;
1148 goto unlock;
Chris Wilson76c1dec2010-09-25 11:22:51 +01001149 }
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001150 obj_priv = to_intel_bo(obj);
Jesse Barnes652c3932009-08-17 13:31:43 -07001151
1152 intel_mark_busy(dev, obj);
1153
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001154 if (read_domains & I915_GEM_DOMAIN_GTT) {
1155 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
Eric Anholt02354392008-11-26 13:58:13 -08001156
Eric Anholta09ba7f2009-08-29 12:49:51 -07001157 /* Update the LRU on the fence for the CPU access that's
1158 * about to occur.
1159 */
1160 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001161 struct drm_i915_fence_reg *reg =
1162 &dev_priv->fence_regs[obj_priv->fence_reg];
1163 list_move_tail(&reg->lru_list,
Eric Anholta09ba7f2009-08-29 12:49:51 -07001164 &dev_priv->mm.fence_list);
1165 }
1166
Eric Anholt02354392008-11-26 13:58:13 -08001167 /* Silently promote "you're not bound, there was nothing to do"
1168 * to success, since the client was just asking us to
1169 * make sure everything was done.
1170 */
1171 if (ret == -EINVAL)
1172 ret = 0;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001173 } else {
Eric Anholte47c68e2008-11-14 13:35:19 -08001174 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001175 }
1176
Chris Wilson7d1c4802010-08-07 21:45:03 +01001177 /* Maintain LRU order of "inactive" objects */
1178 if (ret == 0 && i915_gem_object_is_inactive(obj_priv))
Chris Wilson69dc4982010-10-19 10:36:51 +01001179 list_move_tail(&obj_priv->mm_list, &dev_priv->mm.inactive_list);
Chris Wilson7d1c4802010-08-07 21:45:03 +01001180
Eric Anholt673a3942008-07-30 12:06:12 -07001181 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001182unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07001183 mutex_unlock(&dev->struct_mutex);
1184 return ret;
1185}
1186
1187/**
1188 * Called when user space has done writes to this buffer
1189 */
1190int
1191i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
1192 struct drm_file *file_priv)
1193{
1194 struct drm_i915_gem_sw_finish *args = data;
1195 struct drm_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001196 int ret = 0;
1197
1198 if (!(dev->driver->driver_features & DRIVER_GEM))
1199 return -ENODEV;
1200
Chris Wilson76c1dec2010-09-25 11:22:51 +01001201 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001202 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001203 return ret;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001204
Eric Anholt673a3942008-07-30 12:06:12 -07001205 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1206 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001207 ret = -ENOENT;
1208 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07001209 }
1210
Eric Anholt673a3942008-07-30 12:06:12 -07001211 /* Pinned buffers may be scanout, so flush the cache */
Chris Wilson3d2a8122010-09-29 11:39:53 +01001212 if (to_intel_bo(obj)->pin_count)
Eric Anholte47c68e2008-11-14 13:35:19 -08001213 i915_gem_object_flush_cpu_write_domain(obj);
1214
Eric Anholt673a3942008-07-30 12:06:12 -07001215 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001216unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07001217 mutex_unlock(&dev->struct_mutex);
1218 return ret;
1219}
1220
1221/**
1222 * Maps the contents of an object, returning the address it is mapped
1223 * into.
1224 *
1225 * While the mapping holds a reference on the contents of the object, it doesn't
1226 * imply a ref on the object itself.
1227 */
1228int
1229i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
1230 struct drm_file *file_priv)
1231{
Chris Wilsonda761a62010-10-27 17:37:08 +01001232 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07001233 struct drm_i915_gem_mmap *args = data;
1234 struct drm_gem_object *obj;
1235 loff_t offset;
1236 unsigned long addr;
1237
1238 if (!(dev->driver->driver_features & DRIVER_GEM))
1239 return -ENODEV;
1240
1241 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1242 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001243 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001244
Chris Wilsonda761a62010-10-27 17:37:08 +01001245 if (obj->size > dev_priv->mm.gtt_mappable_end) {
1246 drm_gem_object_unreference_unlocked(obj);
1247 return -E2BIG;
1248 }
1249
Eric Anholt673a3942008-07-30 12:06:12 -07001250 offset = args->offset;
1251
1252 down_write(&current->mm->mmap_sem);
1253 addr = do_mmap(obj->filp, 0, args->size,
1254 PROT_READ | PROT_WRITE, MAP_SHARED,
1255 args->offset);
1256 up_write(&current->mm->mmap_sem);
Luca Barbieribc9025b2010-02-09 05:49:12 +00001257 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001258 if (IS_ERR((void *)addr))
1259 return addr;
1260
1261 args->addr_ptr = (uint64_t) addr;
1262
1263 return 0;
1264}
1265
Jesse Barnesde151cf2008-11-12 10:03:55 -08001266/**
1267 * i915_gem_fault - fault a page into the GTT
1268 * vma: VMA in question
1269 * vmf: fault info
1270 *
1271 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1272 * from userspace. The fault handler takes care of binding the object to
1273 * the GTT (if needed), allocating and programming a fence register (again,
1274 * only if needed based on whether the old reg is still valid or the object
1275 * is tiled) and inserting a new PTE into the faulting process.
1276 *
1277 * Note that the faulting process may involve evicting existing objects
1278 * from the GTT and/or fence registers to make room. So performance may
1279 * suffer if the GTT working set is large or there are few fence registers
1280 * left.
1281 */
1282int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1283{
1284 struct drm_gem_object *obj = vma->vm_private_data;
1285 struct drm_device *dev = obj->dev;
Chris Wilson7d1c4802010-08-07 21:45:03 +01001286 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001287 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001288 pgoff_t page_offset;
1289 unsigned long pfn;
1290 int ret = 0;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001291 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001292
1293 /* We don't use vmf->pgoff since that has the fake offset */
1294 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1295 PAGE_SHIFT;
1296
1297 /* Now bind it into the GTT if needed */
1298 mutex_lock(&dev->struct_mutex);
Daniel Vetterfb7d5162010-10-01 22:05:20 +02001299 BUG_ON(obj_priv->pin_count && !obj_priv->pin_mappable);
Chris Wilsona00b10c2010-09-24 21:15:47 +01001300
1301 if (obj_priv->gtt_space) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01001302 if (!obj_priv->map_and_fenceable) {
Chris Wilsona00b10c2010-09-24 21:15:47 +01001303 ret = i915_gem_object_unbind(obj);
1304 if (ret)
1305 goto unlock;
1306 }
1307 }
Daniel Vetter16e809a2010-09-16 19:37:04 +02001308
Jesse Barnesde151cf2008-11-12 10:03:55 -08001309 if (!obj_priv->gtt_space) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01001310 ret = i915_gem_object_bind_to_gtt(obj, 0, true);
Chris Wilsonc7150892009-09-23 00:43:56 +01001311 if (ret)
1312 goto unlock;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001313 }
1314
Chris Wilson4a684a42010-10-28 14:44:08 +01001315 ret = i915_gem_object_set_to_gtt_domain(obj, write);
1316 if (ret)
1317 goto unlock;
1318
Daniel Vetterfb7d5162010-10-01 22:05:20 +02001319 if (!obj_priv->fault_mappable) {
1320 obj_priv->fault_mappable = true;
Chris Wilsona00b10c2010-09-24 21:15:47 +01001321 i915_gem_info_update_mappable(dev_priv, obj_priv, true);
Daniel Vetterfb7d5162010-10-01 22:05:20 +02001322 }
1323
Jesse Barnesde151cf2008-11-12 10:03:55 -08001324 /* Need a new fence register? */
Eric Anholta09ba7f2009-08-29 12:49:51 -07001325 if (obj_priv->tiling_mode != I915_TILING_NONE) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01001326 ret = i915_gem_object_get_fence_reg(obj, true);
Chris Wilsonc7150892009-09-23 00:43:56 +01001327 if (ret)
1328 goto unlock;
Eric Anholtd9ddcb92009-01-27 10:33:49 -08001329 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001330
Chris Wilson7d1c4802010-08-07 21:45:03 +01001331 if (i915_gem_object_is_inactive(obj_priv))
Chris Wilson69dc4982010-10-19 10:36:51 +01001332 list_move_tail(&obj_priv->mm_list, &dev_priv->mm.inactive_list);
Chris Wilson7d1c4802010-08-07 21:45:03 +01001333
Jesse Barnesde151cf2008-11-12 10:03:55 -08001334 pfn = ((dev->agp->base + obj_priv->gtt_offset) >> PAGE_SHIFT) +
1335 page_offset;
1336
1337 /* Finally, remap it using the new GTT offset */
1338 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
Chris Wilsonc7150892009-09-23 00:43:56 +01001339unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001340 mutex_unlock(&dev->struct_mutex);
1341
1342 switch (ret) {
Chris Wilsonc7150892009-09-23 00:43:56 +01001343 case 0:
1344 case -ERESTARTSYS:
1345 return VM_FAULT_NOPAGE;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001346 case -ENOMEM:
1347 case -EAGAIN:
1348 return VM_FAULT_OOM;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001349 default:
Chris Wilsonc7150892009-09-23 00:43:56 +01001350 return VM_FAULT_SIGBUS;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001351 }
1352}
1353
1354/**
1355 * i915_gem_create_mmap_offset - create a fake mmap offset for an object
1356 * @obj: obj in question
1357 *
1358 * GEM memory mapping works by handing back to userspace a fake mmap offset
1359 * it can use in a subsequent mmap(2) call. The DRM core code then looks
1360 * up the object based on the offset and sets up the various memory mapping
1361 * structures.
1362 *
1363 * This routine allocates and attaches a fake offset for @obj.
1364 */
1365static int
1366i915_gem_create_mmap_offset(struct drm_gem_object *obj)
1367{
1368 struct drm_device *dev = obj->dev;
1369 struct drm_gem_mm *mm = dev->mm_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001370 struct drm_map_list *list;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +11001371 struct drm_local_map *map;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001372 int ret = 0;
1373
1374 /* Set the object up for mmap'ing */
1375 list = &obj->map_list;
Eric Anholt9a298b22009-03-24 12:23:04 -07001376 list->map = kzalloc(sizeof(struct drm_map_list), GFP_KERNEL);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001377 if (!list->map)
1378 return -ENOMEM;
1379
1380 map = list->map;
1381 map->type = _DRM_GEM;
1382 map->size = obj->size;
1383 map->handle = obj;
1384
1385 /* Get a DRM GEM mmap offset allocated... */
1386 list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
1387 obj->size / PAGE_SIZE, 0, 0);
1388 if (!list->file_offset_node) {
1389 DRM_ERROR("failed to allocate offset for bo %d\n", obj->name);
Chris Wilson9e0ae5342010-09-21 15:05:24 +01001390 ret = -ENOSPC;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001391 goto out_free_list;
1392 }
1393
1394 list->file_offset_node = drm_mm_get_block(list->file_offset_node,
1395 obj->size / PAGE_SIZE, 0);
1396 if (!list->file_offset_node) {
1397 ret = -ENOMEM;
1398 goto out_free_list;
1399 }
1400
1401 list->hash.key = list->file_offset_node->start;
Chris Wilson9e0ae5342010-09-21 15:05:24 +01001402 ret = drm_ht_insert_item(&mm->offset_hash, &list->hash);
1403 if (ret) {
Jesse Barnesde151cf2008-11-12 10:03:55 -08001404 DRM_ERROR("failed to add to map hash\n");
1405 goto out_free_mm;
1406 }
1407
Jesse Barnesde151cf2008-11-12 10:03:55 -08001408 return 0;
1409
1410out_free_mm:
1411 drm_mm_put_block(list->file_offset_node);
1412out_free_list:
Eric Anholt9a298b22009-03-24 12:23:04 -07001413 kfree(list->map);
Chris Wilson39a01d12010-10-28 13:03:06 +01001414 list->map = NULL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001415
1416 return ret;
1417}
1418
Chris Wilson901782b2009-07-10 08:18:50 +01001419/**
1420 * i915_gem_release_mmap - remove physical page mappings
1421 * @obj: obj in question
1422 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001423 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01001424 * relinquish ownership of the pages back to the system.
1425 *
1426 * It is vital that we remove the page mapping if we have mapped a tiled
1427 * object through the GTT and then lose the fence register due to
1428 * resource pressure. Similarly if the object has been moved out of the
1429 * aperture, than pages mapped into userspace must be revoked. Removing the
1430 * mapping will then trigger a page fault on the next user access, allowing
1431 * fixup by i915_gem_fault().
1432 */
Eric Anholtd05ca302009-07-10 13:02:26 -07001433void
Chris Wilson901782b2009-07-10 08:18:50 +01001434i915_gem_release_mmap(struct drm_gem_object *obj)
1435{
1436 struct drm_device *dev = obj->dev;
Daniel Vetterfb7d5162010-10-01 22:05:20 +02001437 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001438 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson901782b2009-07-10 08:18:50 +01001439
Chris Wilson39a01d12010-10-28 13:03:06 +01001440 if (unlikely(obj->map_list.map && dev->dev_mapping))
Chris Wilson901782b2009-07-10 08:18:50 +01001441 unmap_mapping_range(dev->dev_mapping,
Chris Wilson39a01d12010-10-28 13:03:06 +01001442 (loff_t)obj->map_list.hash.key<<PAGE_SHIFT,
1443 obj->size, 1);
Daniel Vetterfb7d5162010-10-01 22:05:20 +02001444
1445 if (obj_priv->fault_mappable) {
1446 obj_priv->fault_mappable = false;
Chris Wilsona00b10c2010-09-24 21:15:47 +01001447 i915_gem_info_update_mappable(dev_priv, obj_priv, false);
Daniel Vetterfb7d5162010-10-01 22:05:20 +02001448 }
Chris Wilson901782b2009-07-10 08:18:50 +01001449}
1450
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001451static void
1452i915_gem_free_mmap_offset(struct drm_gem_object *obj)
1453{
1454 struct drm_device *dev = obj->dev;
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001455 struct drm_gem_mm *mm = dev->mm_private;
Chris Wilson39a01d12010-10-28 13:03:06 +01001456 struct drm_map_list *list = &obj->map_list;
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001457
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001458 drm_ht_remove_item(&mm->offset_hash, &list->hash);
Chris Wilson39a01d12010-10-28 13:03:06 +01001459 drm_mm_put_block(list->file_offset_node);
1460 kfree(list->map);
1461 list->map = NULL;
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001462}
1463
Jesse Barnesde151cf2008-11-12 10:03:55 -08001464/**
1465 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1466 * @obj: object to check
1467 *
1468 * Return the required GTT alignment for an object, taking into account
1469 * potential fence register mapping if needed.
1470 */
1471static uint32_t
Chris Wilsona00b10c2010-09-24 21:15:47 +01001472i915_gem_get_gtt_alignment(struct drm_i915_gem_object *obj_priv)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001473{
Chris Wilsona00b10c2010-09-24 21:15:47 +01001474 struct drm_device *dev = obj_priv->base.dev;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001475
1476 /*
1477 * Minimum alignment is 4k (GTT page size), but might be greater
1478 * if a fence register is needed for the object.
1479 */
Chris Wilsona00b10c2010-09-24 21:15:47 +01001480 if (INTEL_INFO(dev)->gen >= 4 ||
1481 obj_priv->tiling_mode == I915_TILING_NONE)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001482 return 4096;
1483
1484 /*
1485 * Previous chips need to be aligned to the size of the smallest
1486 * fence register that can contain the object.
1487 */
Chris Wilsona00b10c2010-09-24 21:15:47 +01001488 return i915_gem_get_gtt_size(obj_priv);
1489}
1490
1491static uint32_t
1492i915_gem_get_gtt_size(struct drm_i915_gem_object *obj_priv)
1493{
1494 struct drm_device *dev = obj_priv->base.dev;
1495 uint32_t size;
1496
1497 /*
1498 * Minimum alignment is 4k (GTT page size), but might be greater
1499 * if a fence register is needed for the object.
1500 */
1501 if (INTEL_INFO(dev)->gen >= 4)
1502 return obj_priv->base.size;
1503
1504 /*
1505 * Previous chips need to be aligned to the size of the smallest
1506 * fence register that can contain the object.
1507 */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001508 if (INTEL_INFO(dev)->gen == 3)
Chris Wilsona00b10c2010-09-24 21:15:47 +01001509 size = 1024*1024;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001510 else
Chris Wilsona00b10c2010-09-24 21:15:47 +01001511 size = 512*1024;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001512
Chris Wilsona00b10c2010-09-24 21:15:47 +01001513 while (size < obj_priv->base.size)
1514 size <<= 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001515
Chris Wilsona00b10c2010-09-24 21:15:47 +01001516 return size;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001517}
1518
1519/**
1520 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1521 * @dev: DRM device
1522 * @data: GTT mapping ioctl data
1523 * @file_priv: GEM object info
1524 *
1525 * Simply returns the fake offset to userspace so it can mmap it.
1526 * The mmap call will end up in drm_gem_mmap(), which will set things
1527 * up so we can get faults in the handler above.
1528 *
1529 * The fault handler will take care of binding the object into the GTT
1530 * (since it may have been evicted to make room for something), allocating
1531 * a fence register, and mapping the appropriate aperture address into
1532 * userspace.
1533 */
1534int
1535i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
1536 struct drm_file *file_priv)
1537{
Chris Wilsonda761a62010-10-27 17:37:08 +01001538 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001539 struct drm_i915_gem_mmap_gtt *args = data;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001540 struct drm_gem_object *obj;
1541 struct drm_i915_gem_object *obj_priv;
1542 int ret;
1543
1544 if (!(dev->driver->driver_features & DRIVER_GEM))
1545 return -ENODEV;
1546
Chris Wilson76c1dec2010-09-25 11:22:51 +01001547 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001548 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001549 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001550
Jesse Barnesde151cf2008-11-12 10:03:55 -08001551 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001552 if (obj == NULL) {
1553 ret = -ENOENT;
1554 goto unlock;
1555 }
Daniel Vetter23010e42010-03-08 13:35:02 +01001556 obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001557
Chris Wilsonda761a62010-10-27 17:37:08 +01001558 if (obj->size > dev_priv->mm.gtt_mappable_end) {
1559 ret = -E2BIG;
1560 goto unlock;
1561 }
1562
Chris Wilsonab182822009-09-22 18:46:17 +01001563 if (obj_priv->madv != I915_MADV_WILLNEED) {
1564 DRM_ERROR("Attempting to mmap a purgeable buffer\n");
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001565 ret = -EINVAL;
1566 goto out;
Chris Wilsonab182822009-09-22 18:46:17 +01001567 }
1568
Chris Wilson39a01d12010-10-28 13:03:06 +01001569 if (!obj->map_list.map) {
Jesse Barnesde151cf2008-11-12 10:03:55 -08001570 ret = i915_gem_create_mmap_offset(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001571 if (ret)
1572 goto out;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001573 }
1574
Chris Wilson39a01d12010-10-28 13:03:06 +01001575 args->offset = (u64)obj->map_list.hash.key << PAGE_SHIFT;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001576
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001577out:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001578 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001579unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001580 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001581 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001582}
1583
Chris Wilsone5281cc2010-10-28 13:45:36 +01001584static int
1585i915_gem_object_get_pages_gtt(struct drm_gem_object *obj,
1586 gfp_t gfpmask)
1587{
1588 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
1589 int page_count, i;
1590 struct address_space *mapping;
1591 struct inode *inode;
1592 struct page *page;
1593
1594 /* Get the list of pages out of our struct file. They'll be pinned
1595 * at this point until we release them.
1596 */
1597 page_count = obj->size / PAGE_SIZE;
1598 BUG_ON(obj_priv->pages != NULL);
1599 obj_priv->pages = drm_malloc_ab(page_count, sizeof(struct page *));
1600 if (obj_priv->pages == NULL)
1601 return -ENOMEM;
1602
1603 inode = obj->filp->f_path.dentry->d_inode;
1604 mapping = inode->i_mapping;
1605 for (i = 0; i < page_count; i++) {
1606 page = read_cache_page_gfp(mapping, i,
1607 GFP_HIGHUSER |
1608 __GFP_COLD |
1609 __GFP_RECLAIMABLE |
1610 gfpmask);
1611 if (IS_ERR(page))
1612 goto err_pages;
1613
1614 obj_priv->pages[i] = page;
1615 }
1616
1617 if (obj_priv->tiling_mode != I915_TILING_NONE)
1618 i915_gem_object_do_bit_17_swizzle(obj);
1619
1620 return 0;
1621
1622err_pages:
1623 while (i--)
1624 page_cache_release(obj_priv->pages[i]);
1625
1626 drm_free_large(obj_priv->pages);
1627 obj_priv->pages = NULL;
1628 return PTR_ERR(page);
1629}
1630
Chris Wilson5cdf5882010-09-27 15:51:07 +01001631static void
Chris Wilsone5281cc2010-10-28 13:45:36 +01001632i915_gem_object_put_pages_gtt(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07001633{
Daniel Vetter23010e42010-03-08 13:35:02 +01001634 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001635 int page_count = obj->size / PAGE_SIZE;
1636 int i;
1637
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001638 BUG_ON(obj_priv->madv == __I915_MADV_PURGED);
Eric Anholt856fa192009-03-19 14:10:50 -07001639
Eric Anholt280b7132009-03-12 16:56:27 -07001640 if (obj_priv->tiling_mode != I915_TILING_NONE)
1641 i915_gem_object_save_bit_17_swizzle(obj);
1642
Chris Wilson3ef94da2009-09-14 16:50:29 +01001643 if (obj_priv->madv == I915_MADV_DONTNEED)
Chris Wilson13a05fd2009-09-20 23:03:19 +01001644 obj_priv->dirty = 0;
Chris Wilson3ef94da2009-09-14 16:50:29 +01001645
1646 for (i = 0; i < page_count; i++) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01001647 if (obj_priv->dirty)
1648 set_page_dirty(obj_priv->pages[i]);
1649
1650 if (obj_priv->madv == I915_MADV_WILLNEED)
Eric Anholt856fa192009-03-19 14:10:50 -07001651 mark_page_accessed(obj_priv->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001652
1653 page_cache_release(obj_priv->pages[i]);
1654 }
Eric Anholt673a3942008-07-30 12:06:12 -07001655 obj_priv->dirty = 0;
1656
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07001657 drm_free_large(obj_priv->pages);
Eric Anholt856fa192009-03-19 14:10:50 -07001658 obj_priv->pages = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001659}
1660
Chris Wilsona56ba562010-09-28 10:07:56 +01001661static uint32_t
1662i915_gem_next_request_seqno(struct drm_device *dev,
1663 struct intel_ring_buffer *ring)
1664{
1665 drm_i915_private_t *dev_priv = dev->dev_private;
1666
1667 ring->outstanding_lazy_request = true;
1668 return dev_priv->next_seqno;
1669}
1670
Eric Anholt673a3942008-07-30 12:06:12 -07001671static void
Daniel Vetter617dbe22010-02-11 22:16:02 +01001672i915_gem_object_move_to_active(struct drm_gem_object *obj,
Zou Nan hai852835f2010-05-21 09:08:56 +08001673 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001674{
1675 struct drm_device *dev = obj->dev;
Chris Wilson69dc4982010-10-19 10:36:51 +01001676 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001677 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsona56ba562010-09-28 10:07:56 +01001678 uint32_t seqno = i915_gem_next_request_seqno(dev, ring);
Daniel Vetter617dbe22010-02-11 22:16:02 +01001679
Zou Nan hai852835f2010-05-21 09:08:56 +08001680 BUG_ON(ring == NULL);
1681 obj_priv->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001682
1683 /* Add a reference if we're newly entering the active list. */
1684 if (!obj_priv->active) {
1685 drm_gem_object_reference(obj);
1686 obj_priv->active = 1;
1687 }
Daniel Vettere35a41d2010-02-11 22:13:59 +01001688
Eric Anholt673a3942008-07-30 12:06:12 -07001689 /* Move from whatever list we were on to the tail of execution. */
Chris Wilson69dc4982010-10-19 10:36:51 +01001690 list_move_tail(&obj_priv->mm_list, &dev_priv->mm.active_list);
1691 list_move_tail(&obj_priv->ring_list, &ring->active_list);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001692 obj_priv->last_rendering_seqno = seqno;
Eric Anholt673a3942008-07-30 12:06:12 -07001693}
1694
Eric Anholtce44b0e2008-11-06 16:00:31 -08001695static void
1696i915_gem_object_move_to_flushing(struct drm_gem_object *obj)
1697{
1698 struct drm_device *dev = obj->dev;
1699 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001700 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001701
1702 BUG_ON(!obj_priv->active);
Chris Wilson69dc4982010-10-19 10:36:51 +01001703 list_move_tail(&obj_priv->mm_list, &dev_priv->mm.flushing_list);
1704 list_del_init(&obj_priv->ring_list);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001705 obj_priv->last_rendering_seqno = 0;
1706}
Eric Anholt673a3942008-07-30 12:06:12 -07001707
Chris Wilson963b4832009-09-20 23:03:54 +01001708/* Immediately discard the backing storage */
1709static void
1710i915_gem_object_truncate(struct drm_gem_object *obj)
1711{
Daniel Vetter23010e42010-03-08 13:35:02 +01001712 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001713 struct inode *inode;
Chris Wilson963b4832009-09-20 23:03:54 +01001714
Chris Wilsonae9fed62010-08-07 11:01:30 +01001715 /* Our goal here is to return as much of the memory as
1716 * is possible back to the system as we are called from OOM.
1717 * To do this we must instruct the shmfs to drop all of its
1718 * backing pages, *now*. Here we mirror the actions taken
1719 * when by shmem_delete_inode() to release the backing store.
1720 */
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001721 inode = obj->filp->f_path.dentry->d_inode;
Chris Wilsonae9fed62010-08-07 11:01:30 +01001722 truncate_inode_pages(inode->i_mapping, 0);
1723 if (inode->i_op->truncate_range)
1724 inode->i_op->truncate_range(inode, 0, (loff_t)-1);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001725
1726 obj_priv->madv = __I915_MADV_PURGED;
Chris Wilson963b4832009-09-20 23:03:54 +01001727}
1728
1729static inline int
1730i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj_priv)
1731{
1732 return obj_priv->madv == I915_MADV_DONTNEED;
1733}
1734
Eric Anholt673a3942008-07-30 12:06:12 -07001735static void
1736i915_gem_object_move_to_inactive(struct drm_gem_object *obj)
1737{
1738 struct drm_device *dev = obj->dev;
1739 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001740 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001741
Eric Anholt673a3942008-07-30 12:06:12 -07001742 if (obj_priv->pin_count != 0)
Chris Wilson69dc4982010-10-19 10:36:51 +01001743 list_move_tail(&obj_priv->mm_list, &dev_priv->mm.pinned_list);
Eric Anholt673a3942008-07-30 12:06:12 -07001744 else
Chris Wilson69dc4982010-10-19 10:36:51 +01001745 list_move_tail(&obj_priv->mm_list, &dev_priv->mm.inactive_list);
1746 list_del_init(&obj_priv->ring_list);
Eric Anholt673a3942008-07-30 12:06:12 -07001747
Daniel Vetter99fcb762010-02-07 16:20:18 +01001748 BUG_ON(!list_empty(&obj_priv->gpu_write_list));
1749
Eric Anholtce44b0e2008-11-06 16:00:31 -08001750 obj_priv->last_rendering_seqno = 0;
Zou Nan hai852835f2010-05-21 09:08:56 +08001751 obj_priv->ring = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001752 if (obj_priv->active) {
1753 obj_priv->active = 0;
1754 drm_gem_object_unreference(obj);
1755 }
Chris Wilson23bc5982010-09-29 16:10:57 +01001756 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07001757}
1758
Daniel Vetter63560392010-02-19 11:51:59 +01001759static void
1760i915_gem_process_flushing_list(struct drm_device *dev,
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001761 uint32_t flush_domains,
Zou Nan hai852835f2010-05-21 09:08:56 +08001762 struct intel_ring_buffer *ring)
Daniel Vetter63560392010-02-19 11:51:59 +01001763{
1764 drm_i915_private_t *dev_priv = dev->dev_private;
1765 struct drm_i915_gem_object *obj_priv, *next;
1766
1767 list_for_each_entry_safe(obj_priv, next,
Chris Wilson64193402010-10-24 12:38:05 +01001768 &ring->gpu_write_list,
Daniel Vetter63560392010-02-19 11:51:59 +01001769 gpu_write_list) {
Daniel Vettera8089e82010-04-09 19:05:09 +00001770 struct drm_gem_object *obj = &obj_priv->base;
Daniel Vetter63560392010-02-19 11:51:59 +01001771
Chris Wilson64193402010-10-24 12:38:05 +01001772 if (obj->write_domain & flush_domains) {
Daniel Vetter63560392010-02-19 11:51:59 +01001773 uint32_t old_write_domain = obj->write_domain;
1774
1775 obj->write_domain = 0;
1776 list_del_init(&obj_priv->gpu_write_list);
Daniel Vetter617dbe22010-02-11 22:16:02 +01001777 i915_gem_object_move_to_active(obj, ring);
Daniel Vetter63560392010-02-19 11:51:59 +01001778
1779 /* update the fence lru list */
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001780 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
1781 struct drm_i915_fence_reg *reg =
1782 &dev_priv->fence_regs[obj_priv->fence_reg];
1783 list_move_tail(&reg->lru_list,
Daniel Vetter63560392010-02-19 11:51:59 +01001784 &dev_priv->mm.fence_list);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001785 }
Daniel Vetter63560392010-02-19 11:51:59 +01001786
1787 trace_i915_gem_object_change_domain(obj,
1788 obj->read_domains,
1789 old_write_domain);
1790 }
1791 }
1792}
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001793
Chris Wilson3cce4692010-10-27 16:11:02 +01001794int
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001795i915_add_request(struct drm_device *dev,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001796 struct drm_file *file,
Chris Wilson8dc5d142010-08-12 12:36:12 +01001797 struct drm_i915_gem_request *request,
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001798 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001799{
1800 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001801 struct drm_i915_file_private *file_priv = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001802 uint32_t seqno;
1803 int was_empty;
Chris Wilson3cce4692010-10-27 16:11:02 +01001804 int ret;
1805
1806 BUG_ON(request == NULL);
Eric Anholt673a3942008-07-30 12:06:12 -07001807
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001808 if (file != NULL)
1809 file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00001810
Chris Wilson3cce4692010-10-27 16:11:02 +01001811 ret = ring->add_request(ring, &seqno);
1812 if (ret)
1813 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001814
Chris Wilsona56ba562010-09-28 10:07:56 +01001815 ring->outstanding_lazy_request = false;
Eric Anholt673a3942008-07-30 12:06:12 -07001816
1817 request->seqno = seqno;
Zou Nan hai852835f2010-05-21 09:08:56 +08001818 request->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001819 request->emitted_jiffies = jiffies;
Zou Nan hai852835f2010-05-21 09:08:56 +08001820 was_empty = list_empty(&ring->request_list);
1821 list_add_tail(&request->list, &ring->request_list);
1822
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001823 if (file_priv) {
Chris Wilson1c255952010-09-26 11:03:27 +01001824 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001825 request->file_priv = file_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00001826 list_add_tail(&request->client_list,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001827 &file_priv->mm.request_list);
Chris Wilson1c255952010-09-26 11:03:27 +01001828 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00001829 }
Eric Anholt673a3942008-07-30 12:06:12 -07001830
Ben Gamarif65d9422009-09-14 17:48:44 -04001831 if (!dev_priv->mm.suspended) {
Chris Wilsonb3b079d2010-09-13 23:44:34 +01001832 mod_timer(&dev_priv->hangcheck_timer,
1833 jiffies + msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
Ben Gamarif65d9422009-09-14 17:48:44 -04001834 if (was_empty)
Chris Wilsonb3b079d2010-09-13 23:44:34 +01001835 queue_delayed_work(dev_priv->wq,
1836 &dev_priv->mm.retire_work, HZ);
Ben Gamarif65d9422009-09-14 17:48:44 -04001837 }
Chris Wilson3cce4692010-10-27 16:11:02 +01001838 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001839}
1840
1841/**
1842 * Command execution barrier
1843 *
1844 * Ensures that all commands in the ring are finished
1845 * before signalling the CPU
1846 */
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001847static void
Zou Nan hai852835f2010-05-21 09:08:56 +08001848i915_retire_commands(struct drm_device *dev, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001849{
Eric Anholt673a3942008-07-30 12:06:12 -07001850 uint32_t flush_domains = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001851
1852 /* The sampler always gets flushed on i965 (sigh) */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001853 if (INTEL_INFO(dev)->gen >= 4)
Eric Anholt673a3942008-07-30 12:06:12 -07001854 flush_domains |= I915_GEM_DOMAIN_SAMPLER;
Zou Nan hai852835f2010-05-21 09:08:56 +08001855
Chris Wilson78501ea2010-10-27 12:18:21 +01001856 ring->flush(ring, I915_GEM_DOMAIN_COMMAND, flush_domains);
Eric Anholt673a3942008-07-30 12:06:12 -07001857}
1858
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001859static inline void
1860i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
Eric Anholt673a3942008-07-30 12:06:12 -07001861{
Chris Wilson1c255952010-09-26 11:03:27 +01001862 struct drm_i915_file_private *file_priv = request->file_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07001863
Chris Wilson1c255952010-09-26 11:03:27 +01001864 if (!file_priv)
1865 return;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001866
Chris Wilson1c255952010-09-26 11:03:27 +01001867 spin_lock(&file_priv->mm.lock);
1868 list_del(&request->client_list);
1869 request->file_priv = NULL;
1870 spin_unlock(&file_priv->mm.lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001871}
1872
Chris Wilsondfaae392010-09-22 10:31:52 +01001873static void i915_gem_reset_ring_lists(struct drm_i915_private *dev_priv,
1874 struct intel_ring_buffer *ring)
Chris Wilson9375e442010-09-19 12:21:28 +01001875{
Chris Wilsondfaae392010-09-22 10:31:52 +01001876 while (!list_empty(&ring->request_list)) {
1877 struct drm_i915_gem_request *request;
Chris Wilson9375e442010-09-19 12:21:28 +01001878
Chris Wilsondfaae392010-09-22 10:31:52 +01001879 request = list_first_entry(&ring->request_list,
1880 struct drm_i915_gem_request,
1881 list);
1882
1883 list_del(&request->list);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001884 i915_gem_request_remove_from_client(request);
Chris Wilsondfaae392010-09-22 10:31:52 +01001885 kfree(request);
1886 }
1887
1888 while (!list_empty(&ring->active_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001889 struct drm_i915_gem_object *obj_priv;
1890
Chris Wilsondfaae392010-09-22 10:31:52 +01001891 obj_priv = list_first_entry(&ring->active_list,
Eric Anholt673a3942008-07-30 12:06:12 -07001892 struct drm_i915_gem_object,
Chris Wilson69dc4982010-10-19 10:36:51 +01001893 ring_list);
Eric Anholt673a3942008-07-30 12:06:12 -07001894
Chris Wilsondfaae392010-09-22 10:31:52 +01001895 obj_priv->base.write_domain = 0;
1896 list_del_init(&obj_priv->gpu_write_list);
1897 i915_gem_object_move_to_inactive(&obj_priv->base);
Eric Anholt673a3942008-07-30 12:06:12 -07001898 }
Eric Anholt673a3942008-07-30 12:06:12 -07001899}
1900
Chris Wilson069efc12010-09-30 16:53:18 +01001901void i915_gem_reset(struct drm_device *dev)
Eric Anholt673a3942008-07-30 12:06:12 -07001902{
Chris Wilsondfaae392010-09-22 10:31:52 +01001903 struct drm_i915_private *dev_priv = dev->dev_private;
1904 struct drm_i915_gem_object *obj_priv;
Chris Wilson069efc12010-09-30 16:53:18 +01001905 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07001906
Chris Wilsondfaae392010-09-22 10:31:52 +01001907 i915_gem_reset_ring_lists(dev_priv, &dev_priv->render_ring);
Chris Wilson87acb0a2010-10-19 10:13:00 +01001908 i915_gem_reset_ring_lists(dev_priv, &dev_priv->bsd_ring);
Chris Wilson549f7362010-10-19 11:19:32 +01001909 i915_gem_reset_ring_lists(dev_priv, &dev_priv->blt_ring);
Chris Wilsondfaae392010-09-22 10:31:52 +01001910
1911 /* Remove anything from the flushing lists. The GPU cache is likely
1912 * to be lost on reset along with the data, so simply move the
1913 * lost bo to the inactive list.
1914 */
1915 while (!list_empty(&dev_priv->mm.flushing_list)) {
Chris Wilson9375e442010-09-19 12:21:28 +01001916 obj_priv = list_first_entry(&dev_priv->mm.flushing_list,
1917 struct drm_i915_gem_object,
Chris Wilson69dc4982010-10-19 10:36:51 +01001918 mm_list);
Chris Wilson9375e442010-09-19 12:21:28 +01001919
1920 obj_priv->base.write_domain = 0;
Chris Wilsondfaae392010-09-22 10:31:52 +01001921 list_del_init(&obj_priv->gpu_write_list);
Chris Wilson9375e442010-09-19 12:21:28 +01001922 i915_gem_object_move_to_inactive(&obj_priv->base);
1923 }
Chris Wilson9375e442010-09-19 12:21:28 +01001924
Chris Wilsondfaae392010-09-22 10:31:52 +01001925 /* Move everything out of the GPU domains to ensure we do any
1926 * necessary invalidation upon reuse.
1927 */
Chris Wilson77f01232010-09-19 12:31:36 +01001928 list_for_each_entry(obj_priv,
1929 &dev_priv->mm.inactive_list,
Chris Wilson69dc4982010-10-19 10:36:51 +01001930 mm_list)
Chris Wilson77f01232010-09-19 12:31:36 +01001931 {
1932 obj_priv->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
1933 }
Chris Wilson069efc12010-09-30 16:53:18 +01001934
1935 /* The fence registers are invalidated so clear them out */
1936 for (i = 0; i < 16; i++) {
1937 struct drm_i915_fence_reg *reg;
1938
1939 reg = &dev_priv->fence_regs[i];
1940 if (!reg->obj)
1941 continue;
1942
1943 i915_gem_clear_fence_reg(reg->obj);
1944 }
Eric Anholt673a3942008-07-30 12:06:12 -07001945}
1946
1947/**
1948 * This function clears the request list as sequence numbers are passed.
1949 */
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001950static void
1951i915_gem_retire_requests_ring(struct drm_device *dev,
1952 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001953{
1954 drm_i915_private_t *dev_priv = dev->dev_private;
1955 uint32_t seqno;
1956
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001957 if (!ring->status_page.page_addr ||
1958 list_empty(&ring->request_list))
Karsten Wiese6c0594a2009-02-23 15:07:57 +01001959 return;
1960
Chris Wilson23bc5982010-09-29 16:10:57 +01001961 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07001962
Chris Wilson78501ea2010-10-27 12:18:21 +01001963 seqno = ring->get_seqno(ring);
Zou Nan hai852835f2010-05-21 09:08:56 +08001964 while (!list_empty(&ring->request_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001965 struct drm_i915_gem_request *request;
Eric Anholt673a3942008-07-30 12:06:12 -07001966
Zou Nan hai852835f2010-05-21 09:08:56 +08001967 request = list_first_entry(&ring->request_list,
Eric Anholt673a3942008-07-30 12:06:12 -07001968 struct drm_i915_gem_request,
1969 list);
Eric Anholt673a3942008-07-30 12:06:12 -07001970
Chris Wilsondfaae392010-09-22 10:31:52 +01001971 if (!i915_seqno_passed(seqno, request->seqno))
Eric Anholt673a3942008-07-30 12:06:12 -07001972 break;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001973
1974 trace_i915_gem_request_retire(dev, request->seqno);
1975
1976 list_del(&request->list);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001977 i915_gem_request_remove_from_client(request);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001978 kfree(request);
1979 }
1980
1981 /* Move any buffers on the active list that are no longer referenced
1982 * by the ringbuffer to the flushing/inactive lists as appropriate.
1983 */
1984 while (!list_empty(&ring->active_list)) {
1985 struct drm_gem_object *obj;
1986 struct drm_i915_gem_object *obj_priv;
1987
1988 obj_priv = list_first_entry(&ring->active_list,
1989 struct drm_i915_gem_object,
Chris Wilson69dc4982010-10-19 10:36:51 +01001990 ring_list);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001991
Chris Wilsondfaae392010-09-22 10:31:52 +01001992 if (!i915_seqno_passed(seqno, obj_priv->last_rendering_seqno))
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001993 break;
1994
1995 obj = &obj_priv->base;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001996 if (obj->write_domain != 0)
1997 i915_gem_object_move_to_flushing(obj);
1998 else
1999 i915_gem_object_move_to_inactive(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002000 }
Chris Wilson9d34e5d2009-09-24 05:26:06 +01002001
2002 if (unlikely (dev_priv->trace_irq_seqno &&
2003 i915_seqno_passed(dev_priv->trace_irq_seqno, seqno))) {
Chris Wilson78501ea2010-10-27 12:18:21 +01002004 ring->user_irq_put(ring);
Chris Wilson9d34e5d2009-09-24 05:26:06 +01002005 dev_priv->trace_irq_seqno = 0;
2006 }
Chris Wilson23bc5982010-09-29 16:10:57 +01002007
2008 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07002009}
2010
2011void
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002012i915_gem_retire_requests(struct drm_device *dev)
2013{
2014 drm_i915_private_t *dev_priv = dev->dev_private;
2015
Chris Wilsonbe726152010-07-23 23:18:50 +01002016 if (!list_empty(&dev_priv->mm.deferred_free_list)) {
2017 struct drm_i915_gem_object *obj_priv, *tmp;
2018
2019 /* We must be careful that during unbind() we do not
2020 * accidentally infinitely recurse into retire requests.
2021 * Currently:
2022 * retire -> free -> unbind -> wait -> retire_ring
2023 */
2024 list_for_each_entry_safe(obj_priv, tmp,
2025 &dev_priv->mm.deferred_free_list,
Chris Wilson69dc4982010-10-19 10:36:51 +01002026 mm_list)
Chris Wilsonbe726152010-07-23 23:18:50 +01002027 i915_gem_free_object_tail(&obj_priv->base);
2028 }
2029
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002030 i915_gem_retire_requests_ring(dev, &dev_priv->render_ring);
Chris Wilson87acb0a2010-10-19 10:13:00 +01002031 i915_gem_retire_requests_ring(dev, &dev_priv->bsd_ring);
Chris Wilson549f7362010-10-19 11:19:32 +01002032 i915_gem_retire_requests_ring(dev, &dev_priv->blt_ring);
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002033}
2034
Daniel Vetter75ef9da2010-08-21 00:25:16 +02002035static void
Eric Anholt673a3942008-07-30 12:06:12 -07002036i915_gem_retire_work_handler(struct work_struct *work)
2037{
2038 drm_i915_private_t *dev_priv;
2039 struct drm_device *dev;
2040
2041 dev_priv = container_of(work, drm_i915_private_t,
2042 mm.retire_work.work);
2043 dev = dev_priv->dev;
2044
Chris Wilson891b48c2010-09-29 12:26:37 +01002045 /* Come back later if the device is busy... */
2046 if (!mutex_trylock(&dev->struct_mutex)) {
2047 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
2048 return;
2049 }
2050
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002051 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08002052
Keith Packard6dbe2772008-10-14 21:41:13 -07002053 if (!dev_priv->mm.suspended &&
Zou Nan haid1b851f2010-05-21 09:08:57 +08002054 (!list_empty(&dev_priv->render_ring.request_list) ||
Chris Wilson549f7362010-10-19 11:19:32 +01002055 !list_empty(&dev_priv->bsd_ring.request_list) ||
2056 !list_empty(&dev_priv->blt_ring.request_list)))
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07002057 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
Eric Anholt673a3942008-07-30 12:06:12 -07002058 mutex_unlock(&dev->struct_mutex);
2059}
2060
Daniel Vetter5a5a0c62009-09-15 22:57:36 +02002061int
Zou Nan hai852835f2010-05-21 09:08:56 +08002062i915_do_wait_request(struct drm_device *dev, uint32_t seqno,
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002063 bool interruptible, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07002064{
2065 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes802c7eb2009-05-05 16:03:48 -07002066 u32 ier;
Eric Anholt673a3942008-07-30 12:06:12 -07002067 int ret = 0;
2068
2069 BUG_ON(seqno == 0);
2070
Ben Gamariba1234d2009-09-14 17:48:47 -04002071 if (atomic_read(&dev_priv->mm.wedged))
Chris Wilson30dbf0c2010-09-25 10:19:17 +01002072 return -EAGAIN;
Ben Gamariffed1d02009-09-14 17:48:41 -04002073
Chris Wilsona56ba562010-09-28 10:07:56 +01002074 if (ring->outstanding_lazy_request) {
Chris Wilson3cce4692010-10-27 16:11:02 +01002075 struct drm_i915_gem_request *request;
2076
2077 request = kzalloc(sizeof(*request), GFP_KERNEL);
2078 if (request == NULL)
Daniel Vettere35a41d2010-02-11 22:13:59 +01002079 return -ENOMEM;
Chris Wilson3cce4692010-10-27 16:11:02 +01002080
2081 ret = i915_add_request(dev, NULL, request, ring);
2082 if (ret) {
2083 kfree(request);
2084 return ret;
2085 }
2086
2087 seqno = request->seqno;
Daniel Vettere35a41d2010-02-11 22:13:59 +01002088 }
Chris Wilsona56ba562010-09-28 10:07:56 +01002089 BUG_ON(seqno == dev_priv->next_seqno);
Daniel Vettere35a41d2010-02-11 22:13:59 +01002090
Chris Wilson78501ea2010-10-27 12:18:21 +01002091 if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) {
Eric Anholtbad720f2009-10-22 16:11:14 -07002092 if (HAS_PCH_SPLIT(dev))
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002093 ier = I915_READ(DEIER) | I915_READ(GTIER);
2094 else
2095 ier = I915_READ(IER);
Jesse Barnes802c7eb2009-05-05 16:03:48 -07002096 if (!ier) {
2097 DRM_ERROR("something (likely vbetool) disabled "
2098 "interrupts, re-enabling\n");
2099 i915_driver_irq_preinstall(dev);
2100 i915_driver_irq_postinstall(dev);
2101 }
2102
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002103 trace_i915_gem_request_wait_begin(dev, seqno);
2104
Chris Wilsonb2223492010-10-27 15:27:33 +01002105 ring->waiting_seqno = seqno;
Chris Wilson78501ea2010-10-27 12:18:21 +01002106 ring->user_irq_get(ring);
Daniel Vetter48764bf2009-09-15 22:57:32 +02002107 if (interruptible)
Zou Nan hai852835f2010-05-21 09:08:56 +08002108 ret = wait_event_interruptible(ring->irq_queue,
Chris Wilson78501ea2010-10-27 12:18:21 +01002109 i915_seqno_passed(ring->get_seqno(ring), seqno)
Zou Nan hai852835f2010-05-21 09:08:56 +08002110 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02002111 else
Zou Nan hai852835f2010-05-21 09:08:56 +08002112 wait_event(ring->irq_queue,
Chris Wilson78501ea2010-10-27 12:18:21 +01002113 i915_seqno_passed(ring->get_seqno(ring), seqno)
Zou Nan hai852835f2010-05-21 09:08:56 +08002114 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02002115
Chris Wilson78501ea2010-10-27 12:18:21 +01002116 ring->user_irq_put(ring);
Chris Wilsonb2223492010-10-27 15:27:33 +01002117 ring->waiting_seqno = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002118
2119 trace_i915_gem_request_wait_end(dev, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07002120 }
Ben Gamariba1234d2009-09-14 17:48:47 -04002121 if (atomic_read(&dev_priv->mm.wedged))
Chris Wilson30dbf0c2010-09-25 10:19:17 +01002122 ret = -EAGAIN;
Eric Anholt673a3942008-07-30 12:06:12 -07002123
2124 if (ret && ret != -ERESTARTSYS)
Daniel Vetter8bff9172010-02-11 22:19:40 +01002125 DRM_ERROR("%s returns %d (awaiting %d at %d, next %d)\n",
Chris Wilson78501ea2010-10-27 12:18:21 +01002126 __func__, ret, seqno, ring->get_seqno(ring),
Daniel Vetter8bff9172010-02-11 22:19:40 +01002127 dev_priv->next_seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07002128
2129 /* Directly dispatch request retiring. While we have the work queue
2130 * to handle this, the waiter on a request often wants an associated
2131 * buffer to have made it to the inactive list, and we would need
2132 * a separate wait queue to handle that.
2133 */
2134 if (ret == 0)
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002135 i915_gem_retire_requests_ring(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07002136
2137 return ret;
2138}
2139
Daniel Vetter48764bf2009-09-15 22:57:32 +02002140/**
2141 * Waits for a sequence number to be signaled, and cleans up the
2142 * request and object lists appropriately for that event.
2143 */
2144static int
Zou Nan hai852835f2010-05-21 09:08:56 +08002145i915_wait_request(struct drm_device *dev, uint32_t seqno,
Chris Wilsona56ba562010-09-28 10:07:56 +01002146 struct intel_ring_buffer *ring)
Daniel Vetter48764bf2009-09-15 22:57:32 +02002147{
Zou Nan hai852835f2010-05-21 09:08:56 +08002148 return i915_do_wait_request(dev, seqno, 1, ring);
Daniel Vetter48764bf2009-09-15 22:57:32 +02002149}
2150
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002151static void
Chris Wilson92204342010-09-18 11:02:01 +01002152i915_gem_flush_ring(struct drm_device *dev,
Chris Wilsonc78ec302010-09-20 12:50:23 +01002153 struct drm_file *file_priv,
Chris Wilson92204342010-09-18 11:02:01 +01002154 struct intel_ring_buffer *ring,
2155 uint32_t invalidate_domains,
2156 uint32_t flush_domains)
2157{
Chris Wilson78501ea2010-10-27 12:18:21 +01002158 ring->flush(ring, invalidate_domains, flush_domains);
Chris Wilson92204342010-09-18 11:02:01 +01002159 i915_gem_process_flushing_list(dev, flush_domains, ring);
2160}
2161
2162static void
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002163i915_gem_flush(struct drm_device *dev,
Chris Wilsonc78ec302010-09-20 12:50:23 +01002164 struct drm_file *file_priv,
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002165 uint32_t invalidate_domains,
Chris Wilson92204342010-09-18 11:02:01 +01002166 uint32_t flush_domains,
2167 uint32_t flush_rings)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002168{
2169 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter8bff9172010-02-11 22:19:40 +01002170
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002171 if (flush_domains & I915_GEM_DOMAIN_CPU)
2172 drm_agp_chipset_flush(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08002173
Chris Wilson92204342010-09-18 11:02:01 +01002174 if ((flush_domains | invalidate_domains) & I915_GEM_GPU_DOMAINS) {
2175 if (flush_rings & RING_RENDER)
Chris Wilsonc78ec302010-09-20 12:50:23 +01002176 i915_gem_flush_ring(dev, file_priv,
Chris Wilson92204342010-09-18 11:02:01 +01002177 &dev_priv->render_ring,
2178 invalidate_domains, flush_domains);
2179 if (flush_rings & RING_BSD)
Chris Wilsonc78ec302010-09-20 12:50:23 +01002180 i915_gem_flush_ring(dev, file_priv,
Chris Wilson92204342010-09-18 11:02:01 +01002181 &dev_priv->bsd_ring,
2182 invalidate_domains, flush_domains);
Chris Wilson549f7362010-10-19 11:19:32 +01002183 if (flush_rings & RING_BLT)
2184 i915_gem_flush_ring(dev, file_priv,
2185 &dev_priv->blt_ring,
2186 invalidate_domains, flush_domains);
Chris Wilson92204342010-09-18 11:02:01 +01002187 }
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002188}
2189
Eric Anholt673a3942008-07-30 12:06:12 -07002190/**
2191 * Ensures that all rendering to the object has completed and the object is
2192 * safe to unbind from the GTT or access from the CPU.
2193 */
2194static int
Chris Wilson2cf34d72010-09-14 13:03:28 +01002195i915_gem_object_wait_rendering(struct drm_gem_object *obj,
2196 bool interruptible)
Eric Anholt673a3942008-07-30 12:06:12 -07002197{
2198 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01002199 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002200 int ret;
2201
Eric Anholte47c68e2008-11-14 13:35:19 -08002202 /* This function only exists to support waiting for existing rendering,
2203 * not for emitting required flushes.
Eric Anholt673a3942008-07-30 12:06:12 -07002204 */
Eric Anholte47c68e2008-11-14 13:35:19 -08002205 BUG_ON((obj->write_domain & I915_GEM_GPU_DOMAINS) != 0);
Eric Anholt673a3942008-07-30 12:06:12 -07002206
2207 /* If there is rendering queued on the buffer being evicted, wait for
2208 * it.
2209 */
2210 if (obj_priv->active) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01002211 ret = i915_do_wait_request(dev,
2212 obj_priv->last_rendering_seqno,
2213 interruptible,
2214 obj_priv->ring);
2215 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002216 return ret;
2217 }
2218
2219 return 0;
2220}
2221
2222/**
2223 * Unbinds an object from the GTT aperture.
2224 */
Jesse Barnes0f973f22009-01-26 17:10:45 -08002225int
Eric Anholt673a3942008-07-30 12:06:12 -07002226i915_gem_object_unbind(struct drm_gem_object *obj)
2227{
2228 struct drm_device *dev = obj->dev;
Chris Wilson73aa8082010-09-30 11:46:12 +01002229 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002230 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002231 int ret = 0;
2232
Eric Anholt673a3942008-07-30 12:06:12 -07002233 if (obj_priv->gtt_space == NULL)
2234 return 0;
2235
2236 if (obj_priv->pin_count != 0) {
2237 DRM_ERROR("Attempting to unbind pinned buffer\n");
2238 return -EINVAL;
2239 }
2240
Eric Anholt5323fd02009-09-09 11:50:45 -07002241 /* blow away mappings if mapped through GTT */
2242 i915_gem_release_mmap(obj);
2243
Eric Anholt673a3942008-07-30 12:06:12 -07002244 /* Move the object to the CPU domain to ensure that
2245 * any possible CPU writes while it's not in the GTT
2246 * are flushed when we go to remap it. This will
2247 * also ensure that all pending GPU writes are finished
2248 * before we unbind.
2249 */
Eric Anholte47c68e2008-11-14 13:35:19 -08002250 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Chris Wilson8dc17752010-07-23 23:18:51 +01002251 if (ret == -ERESTARTSYS)
Eric Anholt673a3942008-07-30 12:06:12 -07002252 return ret;
Chris Wilson8dc17752010-07-23 23:18:51 +01002253 /* Continue on if we fail due to EIO, the GPU is hung so we
2254 * should be safe and we need to cleanup or else we might
2255 * cause memory corruption through use-after-free.
2256 */
Chris Wilson812ed4922010-09-30 15:08:57 +01002257 if (ret) {
2258 i915_gem_clflush_object(obj);
2259 obj->read_domains = obj->write_domain = I915_GEM_DOMAIN_CPU;
2260 }
Eric Anholt673a3942008-07-30 12:06:12 -07002261
Daniel Vetter96b47b62009-12-15 17:50:00 +01002262 /* release the fence reg _after_ flushing */
2263 if (obj_priv->fence_reg != I915_FENCE_REG_NONE)
2264 i915_gem_clear_fence_reg(obj);
2265
Chris Wilson73aa8082010-09-30 11:46:12 +01002266 drm_unbind_agp(obj_priv->agp_mem);
2267 drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002268
Chris Wilsone5281cc2010-10-28 13:45:36 +01002269 i915_gem_object_put_pages_gtt(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002270
Chris Wilsona00b10c2010-09-24 21:15:47 +01002271 i915_gem_info_remove_gtt(dev_priv, obj_priv);
Chris Wilson69dc4982010-10-19 10:36:51 +01002272 list_del_init(&obj_priv->mm_list);
Daniel Vetter75e9e912010-11-04 17:11:09 +01002273 /* Avoid an unnecessary call to unbind on rebind. */
2274 obj_priv->map_and_fenceable = true;
Eric Anholt673a3942008-07-30 12:06:12 -07002275
Chris Wilson73aa8082010-09-30 11:46:12 +01002276 drm_mm_put_block(obj_priv->gtt_space);
2277 obj_priv->gtt_space = NULL;
Chris Wilson9af90d12010-10-17 10:01:56 +01002278 obj_priv->gtt_offset = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002279
Chris Wilson963b4832009-09-20 23:03:54 +01002280 if (i915_gem_object_is_purgeable(obj_priv))
2281 i915_gem_object_truncate(obj);
2282
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002283 trace_i915_gem_object_unbind(obj);
2284
Chris Wilson8dc17752010-07-23 23:18:51 +01002285 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002286}
2287
Chris Wilsona56ba562010-09-28 10:07:56 +01002288static int i915_ring_idle(struct drm_device *dev,
2289 struct intel_ring_buffer *ring)
2290{
Chris Wilson395b70b2010-10-28 21:28:46 +01002291 if (list_empty(&ring->gpu_write_list) && list_empty(&ring->active_list))
Chris Wilson64193402010-10-24 12:38:05 +01002292 return 0;
2293
Chris Wilsona56ba562010-09-28 10:07:56 +01002294 i915_gem_flush_ring(dev, NULL, ring,
2295 I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
2296 return i915_wait_request(dev,
2297 i915_gem_next_request_seqno(dev, ring),
2298 ring);
2299}
2300
Chris Wilsonb47eb4a2010-08-07 11:01:23 +01002301int
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002302i915_gpu_idle(struct drm_device *dev)
2303{
2304 drm_i915_private_t *dev_priv = dev->dev_private;
2305 bool lists_empty;
Zou Nan hai852835f2010-05-21 09:08:56 +08002306 int ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002307
Zou Nan haid1b851f2010-05-21 09:08:57 +08002308 lists_empty = (list_empty(&dev_priv->mm.flushing_list) &&
Chris Wilson395b70b2010-10-28 21:28:46 +01002309 list_empty(&dev_priv->mm.active_list));
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002310 if (lists_empty)
2311 return 0;
2312
2313 /* Flush everything onto the inactive list. */
Chris Wilsona56ba562010-09-28 10:07:56 +01002314 ret = i915_ring_idle(dev, &dev_priv->render_ring);
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002315 if (ret)
2316 return ret;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002317
Chris Wilson87acb0a2010-10-19 10:13:00 +01002318 ret = i915_ring_idle(dev, &dev_priv->bsd_ring);
2319 if (ret)
2320 return ret;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002321
Chris Wilson549f7362010-10-19 11:19:32 +01002322 ret = i915_ring_idle(dev, &dev_priv->blt_ring);
2323 if (ret)
2324 return ret;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002325
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002326 return 0;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002327}
2328
Chris Wilsona00b10c2010-09-24 21:15:47 +01002329static void sandybridge_write_fence_reg(struct drm_gem_object *obj)
Eric Anholt4e901fd2009-10-26 16:44:17 -07002330{
Eric Anholt4e901fd2009-10-26 16:44:17 -07002331 struct drm_device *dev = obj->dev;
2332 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002333 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsona00b10c2010-09-24 21:15:47 +01002334 u32 size = i915_gem_get_gtt_size(obj_priv);
Eric Anholt4e901fd2009-10-26 16:44:17 -07002335 int regnum = obj_priv->fence_reg;
2336 uint64_t val;
2337
Chris Wilsona00b10c2010-09-24 21:15:47 +01002338 val = (uint64_t)((obj_priv->gtt_offset + size - 4096) &
Eric Anholt4e901fd2009-10-26 16:44:17 -07002339 0xfffff000) << 32;
2340 val |= obj_priv->gtt_offset & 0xfffff000;
2341 val |= (uint64_t)((obj_priv->stride / 128) - 1) <<
2342 SANDYBRIDGE_FENCE_PITCH_SHIFT;
2343
2344 if (obj_priv->tiling_mode == I915_TILING_Y)
2345 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2346 val |= I965_FENCE_REG_VALID;
2347
2348 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (regnum * 8), val);
2349}
2350
Chris Wilsona00b10c2010-09-24 21:15:47 +01002351static void i965_write_fence_reg(struct drm_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002352{
Jesse Barnesde151cf2008-11-12 10:03:55 -08002353 struct drm_device *dev = obj->dev;
2354 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002355 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsona00b10c2010-09-24 21:15:47 +01002356 u32 size = i915_gem_get_gtt_size(obj_priv);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002357 int regnum = obj_priv->fence_reg;
2358 uint64_t val;
2359
Chris Wilsona00b10c2010-09-24 21:15:47 +01002360 val = (uint64_t)((obj_priv->gtt_offset + size - 4096) &
Jesse Barnesde151cf2008-11-12 10:03:55 -08002361 0xfffff000) << 32;
2362 val |= obj_priv->gtt_offset & 0xfffff000;
2363 val |= ((obj_priv->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
2364 if (obj_priv->tiling_mode == I915_TILING_Y)
2365 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2366 val |= I965_FENCE_REG_VALID;
2367
2368 I915_WRITE64(FENCE_REG_965_0 + (regnum * 8), val);
2369}
2370
Chris Wilsona00b10c2010-09-24 21:15:47 +01002371static void i915_write_fence_reg(struct drm_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002372{
Jesse Barnesde151cf2008-11-12 10:03:55 -08002373 struct drm_device *dev = obj->dev;
2374 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002375 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsona00b10c2010-09-24 21:15:47 +01002376 u32 size = i915_gem_get_gtt_size(obj_priv);
2377 uint32_t fence_reg, val, pitch_val;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002378 int tile_width;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002379
2380 if ((obj_priv->gtt_offset & ~I915_FENCE_START_MASK) ||
Chris Wilsona00b10c2010-09-24 21:15:47 +01002381 (obj_priv->gtt_offset & (size - 1))) {
2382 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 +01002383 __func__, obj_priv->gtt_offset, obj_priv->map_and_fenceable, size,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002384 obj_priv->gtt_space->start, obj_priv->gtt_space->size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002385 return;
2386 }
2387
Jesse Barnes0f973f22009-01-26 17:10:45 -08002388 if (obj_priv->tiling_mode == I915_TILING_Y &&
2389 HAS_128_BYTE_Y_TILING(dev))
2390 tile_width = 128;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002391 else
Jesse Barnes0f973f22009-01-26 17:10:45 -08002392 tile_width = 512;
2393
2394 /* Note: pitch better be a power of two tile widths */
2395 pitch_val = obj_priv->stride / tile_width;
2396 pitch_val = ffs(pitch_val) - 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002397
Daniel Vetterc36a2a62010-04-17 15:12:03 +02002398 if (obj_priv->tiling_mode == I915_TILING_Y &&
2399 HAS_128_BYTE_Y_TILING(dev))
2400 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2401 else
2402 WARN_ON(pitch_val > I915_FENCE_MAX_PITCH_VAL);
2403
Jesse Barnesde151cf2008-11-12 10:03:55 -08002404 val = obj_priv->gtt_offset;
2405 if (obj_priv->tiling_mode == I915_TILING_Y)
2406 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002407 val |= I915_FENCE_SIZE_BITS(size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002408 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2409 val |= I830_FENCE_REG_VALID;
2410
Chris Wilsona00b10c2010-09-24 21:15:47 +01002411 fence_reg = obj_priv->fence_reg;
2412 if (fence_reg < 8)
2413 fence_reg = FENCE_REG_830_0 + fence_reg * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002414 else
Chris Wilsona00b10c2010-09-24 21:15:47 +01002415 fence_reg = FENCE_REG_945_8 + (fence_reg - 8) * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002416 I915_WRITE(fence_reg, val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002417}
2418
Chris Wilsona00b10c2010-09-24 21:15:47 +01002419static void i830_write_fence_reg(struct drm_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002420{
Jesse Barnesde151cf2008-11-12 10:03:55 -08002421 struct drm_device *dev = obj->dev;
2422 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002423 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsona00b10c2010-09-24 21:15:47 +01002424 u32 size = i915_gem_get_gtt_size(obj_priv);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002425 int regnum = obj_priv->fence_reg;
2426 uint32_t val;
2427 uint32_t pitch_val;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002428 uint32_t fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002429
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002430 if ((obj_priv->gtt_offset & ~I830_FENCE_START_MASK) ||
Jesse Barnesde151cf2008-11-12 10:03:55 -08002431 (obj_priv->gtt_offset & (obj->size - 1))) {
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002432 WARN(1, "%s: object 0x%08x not 512K or size aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002433 __func__, obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002434 return;
2435 }
2436
Eric Anholte76a16d2009-05-26 17:44:56 -07002437 pitch_val = obj_priv->stride / 128;
2438 pitch_val = ffs(pitch_val) - 1;
2439 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2440
Jesse Barnesde151cf2008-11-12 10:03:55 -08002441 val = obj_priv->gtt_offset;
2442 if (obj_priv->tiling_mode == I915_TILING_Y)
2443 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002444 fence_size_bits = I830_FENCE_SIZE_BITS(size);
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002445 WARN_ON(fence_size_bits & ~0x00000f00);
2446 val |= fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002447 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2448 val |= I830_FENCE_REG_VALID;
2449
2450 I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002451}
2452
Chris Wilson2cf34d72010-09-14 13:03:28 +01002453static int i915_find_fence_reg(struct drm_device *dev,
2454 bool interruptible)
Daniel Vetterae3db242010-02-19 11:51:58 +01002455{
Daniel Vetterae3db242010-02-19 11:51:58 +01002456 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002457 struct drm_i915_fence_reg *reg;
2458 struct drm_i915_gem_object *obj_priv = NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002459 int i, avail, ret;
2460
2461 /* First try to find a free reg */
2462 avail = 0;
2463 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2464 reg = &dev_priv->fence_regs[i];
2465 if (!reg->obj)
2466 return i;
2467
Daniel Vetter23010e42010-03-08 13:35:02 +01002468 obj_priv = to_intel_bo(reg->obj);
Daniel Vetterae3db242010-02-19 11:51:58 +01002469 if (!obj_priv->pin_count)
2470 avail++;
2471 }
2472
2473 if (avail == 0)
2474 return -ENOSPC;
2475
2476 /* None available, try to steal one or wait for a user to finish */
Chris Wilsona00b10c2010-09-24 21:15:47 +01002477 avail = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002478 list_for_each_entry(reg, &dev_priv->mm.fence_list,
2479 lru_list) {
Chris Wilsona00b10c2010-09-24 21:15:47 +01002480 obj_priv = to_intel_bo(reg->obj);
Daniel Vetterae3db242010-02-19 11:51:58 +01002481 if (obj_priv->pin_count)
2482 continue;
2483
2484 /* found one! */
Chris Wilsona00b10c2010-09-24 21:15:47 +01002485 avail = obj_priv->fence_reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01002486 break;
2487 }
2488
Chris Wilsona00b10c2010-09-24 21:15:47 +01002489 BUG_ON(avail == I915_FENCE_REG_NONE);
Daniel Vetterae3db242010-02-19 11:51:58 +01002490
2491 /* We only have a reference on obj from the active list. put_fence_reg
2492 * might drop that one, causing a use-after-free in it. So hold a
2493 * private reference to obj like the other callers of put_fence_reg
2494 * (set_tiling ioctl) do. */
Chris Wilsona00b10c2010-09-24 21:15:47 +01002495 drm_gem_object_reference(&obj_priv->base);
2496 ret = i915_gem_object_put_fence_reg(&obj_priv->base, interruptible);
2497 drm_gem_object_unreference(&obj_priv->base);
Daniel Vetterae3db242010-02-19 11:51:58 +01002498 if (ret != 0)
2499 return ret;
2500
Chris Wilsona00b10c2010-09-24 21:15:47 +01002501 return avail;
Daniel Vetterae3db242010-02-19 11:51:58 +01002502}
2503
Jesse Barnesde151cf2008-11-12 10:03:55 -08002504/**
2505 * i915_gem_object_get_fence_reg - set up a fence reg for an object
2506 * @obj: object to map through a fence reg
2507 *
2508 * When mapping objects through the GTT, userspace wants to be able to write
2509 * to them without having to worry about swizzling if the object is tiled.
2510 *
2511 * This function walks the fence regs looking for a free one for @obj,
2512 * stealing one if it can't find any.
2513 *
2514 * It then sets up the reg based on the object's properties: address, pitch
2515 * and tiling format.
2516 */
Chris Wilson8c4b8c32009-06-17 22:08:52 +01002517int
Chris Wilson2cf34d72010-09-14 13:03:28 +01002518i915_gem_object_get_fence_reg(struct drm_gem_object *obj,
2519 bool interruptible)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002520{
2521 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002522 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002523 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002524 struct drm_i915_fence_reg *reg = NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002525 int ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002526
Eric Anholta09ba7f2009-08-29 12:49:51 -07002527 /* Just update our place in the LRU if our fence is getting used. */
2528 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002529 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
2530 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002531 return 0;
2532 }
2533
Jesse Barnesde151cf2008-11-12 10:03:55 -08002534 switch (obj_priv->tiling_mode) {
2535 case I915_TILING_NONE:
2536 WARN(1, "allocating a fence for non-tiled object?\n");
2537 break;
2538 case I915_TILING_X:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002539 if (!obj_priv->stride)
2540 return -EINVAL;
2541 WARN((obj_priv->stride & (512 - 1)),
2542 "object 0x%08x is X tiled but has non-512B pitch\n",
2543 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002544 break;
2545 case I915_TILING_Y:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002546 if (!obj_priv->stride)
2547 return -EINVAL;
2548 WARN((obj_priv->stride & (128 - 1)),
2549 "object 0x%08x is Y tiled but has non-128B pitch\n",
2550 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002551 break;
2552 }
2553
Chris Wilson2cf34d72010-09-14 13:03:28 +01002554 ret = i915_find_fence_reg(dev, interruptible);
Daniel Vetterae3db242010-02-19 11:51:58 +01002555 if (ret < 0)
2556 return ret;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002557
Daniel Vetterae3db242010-02-19 11:51:58 +01002558 obj_priv->fence_reg = ret;
2559 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002560 list_add_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002561
Jesse Barnesde151cf2008-11-12 10:03:55 -08002562 reg->obj = obj;
2563
Chris Wilsone259bef2010-09-17 00:32:02 +01002564 switch (INTEL_INFO(dev)->gen) {
2565 case 6:
Chris Wilsona00b10c2010-09-24 21:15:47 +01002566 sandybridge_write_fence_reg(obj);
Chris Wilsone259bef2010-09-17 00:32:02 +01002567 break;
2568 case 5:
2569 case 4:
Chris Wilsona00b10c2010-09-24 21:15:47 +01002570 i965_write_fence_reg(obj);
Chris Wilsone259bef2010-09-17 00:32:02 +01002571 break;
2572 case 3:
Chris Wilsona00b10c2010-09-24 21:15:47 +01002573 i915_write_fence_reg(obj);
Chris Wilsone259bef2010-09-17 00:32:02 +01002574 break;
2575 case 2:
Chris Wilsona00b10c2010-09-24 21:15:47 +01002576 i830_write_fence_reg(obj);
Chris Wilsone259bef2010-09-17 00:32:02 +01002577 break;
2578 }
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002579
Chris Wilsona00b10c2010-09-24 21:15:47 +01002580 trace_i915_gem_object_get_fence(obj,
2581 obj_priv->fence_reg,
2582 obj_priv->tiling_mode);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002583
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002584 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002585}
2586
2587/**
2588 * i915_gem_clear_fence_reg - clear out fence register info
2589 * @obj: object to clear
2590 *
2591 * Zeroes out the fence register itself and clears out the associated
2592 * data structures in dev_priv and obj_priv.
2593 */
2594static void
2595i915_gem_clear_fence_reg(struct drm_gem_object *obj)
2596{
2597 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002598 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002599 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002600 struct drm_i915_fence_reg *reg =
2601 &dev_priv->fence_regs[obj_priv->fence_reg];
Chris Wilsone259bef2010-09-17 00:32:02 +01002602 uint32_t fence_reg;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002603
Chris Wilsone259bef2010-09-17 00:32:02 +01002604 switch (INTEL_INFO(dev)->gen) {
2605 case 6:
Eric Anholt4e901fd2009-10-26 16:44:17 -07002606 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 +
2607 (obj_priv->fence_reg * 8), 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002608 break;
2609 case 5:
2610 case 4:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002611 I915_WRITE64(FENCE_REG_965_0 + (obj_priv->fence_reg * 8), 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002612 break;
2613 case 3:
Chris Wilson9b74f732010-09-22 19:10:44 +01002614 if (obj_priv->fence_reg >= 8)
Chris Wilsone259bef2010-09-17 00:32:02 +01002615 fence_reg = FENCE_REG_945_8 + (obj_priv->fence_reg - 8) * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002616 else
Chris Wilsone259bef2010-09-17 00:32:02 +01002617 case 2:
2618 fence_reg = FENCE_REG_830_0 + obj_priv->fence_reg * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002619
2620 I915_WRITE(fence_reg, 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002621 break;
Eric Anholtdc529a42009-03-10 22:34:49 -07002622 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002623
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002624 reg->obj = NULL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002625 obj_priv->fence_reg = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002626 list_del_init(&reg->lru_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002627}
2628
Eric Anholt673a3942008-07-30 12:06:12 -07002629/**
Chris Wilson52dc7d32009-06-06 09:46:01 +01002630 * i915_gem_object_put_fence_reg - waits on outstanding fenced access
2631 * to the buffer to finish, and then resets the fence register.
2632 * @obj: tiled object holding a fence register.
Chris Wilson2cf34d72010-09-14 13:03:28 +01002633 * @bool: whether the wait upon the fence is interruptible
Chris Wilson52dc7d32009-06-06 09:46:01 +01002634 *
2635 * Zeroes out the fence register itself and clears out the associated
2636 * data structures in dev_priv and obj_priv.
2637 */
2638int
Chris Wilson2cf34d72010-09-14 13:03:28 +01002639i915_gem_object_put_fence_reg(struct drm_gem_object *obj,
2640 bool interruptible)
Chris Wilson52dc7d32009-06-06 09:46:01 +01002641{
2642 struct drm_device *dev = obj->dev;
Chris Wilson53640e12010-09-20 11:40:50 +01002643 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002644 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson53640e12010-09-20 11:40:50 +01002645 struct drm_i915_fence_reg *reg;
Chris Wilson52dc7d32009-06-06 09:46:01 +01002646
2647 if (obj_priv->fence_reg == I915_FENCE_REG_NONE)
2648 return 0;
2649
Daniel Vetter10ae9bd2010-02-01 13:59:17 +01002650 /* If we've changed tiling, GTT-mappings of the object
2651 * need to re-fault to ensure that the correct fence register
2652 * setup is in place.
2653 */
2654 i915_gem_release_mmap(obj);
2655
Chris Wilson52dc7d32009-06-06 09:46:01 +01002656 /* On the i915, GPU access to tiled buffers is via a fence,
2657 * therefore we must wait for any outstanding access to complete
2658 * before clearing the fence.
2659 */
Chris Wilson53640e12010-09-20 11:40:50 +01002660 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
2661 if (reg->gpu) {
Chris Wilson52dc7d32009-06-06 09:46:01 +01002662 int ret;
2663
Chris Wilson2cf34d72010-09-14 13:03:28 +01002664 ret = i915_gem_object_flush_gpu_write_domain(obj, true);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002665 if (ret)
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002666 return ret;
2667
Chris Wilson2cf34d72010-09-14 13:03:28 +01002668 ret = i915_gem_object_wait_rendering(obj, interruptible);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002669 if (ret)
Chris Wilson52dc7d32009-06-06 09:46:01 +01002670 return ret;
Chris Wilson53640e12010-09-20 11:40:50 +01002671
2672 reg->gpu = false;
Chris Wilson52dc7d32009-06-06 09:46:01 +01002673 }
2674
Daniel Vetter4a726612010-02-01 13:59:16 +01002675 i915_gem_object_flush_gtt_write_domain(obj);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002676 i915_gem_clear_fence_reg(obj);
Chris Wilson52dc7d32009-06-06 09:46:01 +01002677
2678 return 0;
2679}
2680
2681/**
Eric Anholt673a3942008-07-30 12:06:12 -07002682 * Finds free space in the GTT aperture and binds the object there.
2683 */
2684static int
Daniel Vetter920afa72010-09-16 17:54:23 +02002685i915_gem_object_bind_to_gtt(struct drm_gem_object *obj,
2686 unsigned alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01002687 bool map_and_fenceable)
Eric Anholt673a3942008-07-30 12:06:12 -07002688{
2689 struct drm_device *dev = obj->dev;
2690 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002691 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002692 struct drm_mm_node *free_space;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002693 gfp_t gfpmask = __GFP_NORETRY | __GFP_NOWARN;
2694 u32 size, fence_size, fence_alignment;
Daniel Vetter75e9e912010-11-04 17:11:09 +01002695 bool mappable, fenceable;
Chris Wilson07f73f62009-09-14 16:50:30 +01002696 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002697
Chris Wilsonbb6baf72009-09-22 14:24:13 +01002698 if (obj_priv->madv != I915_MADV_WILLNEED) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01002699 DRM_ERROR("Attempting to bind a purgeable object\n");
2700 return -EINVAL;
2701 }
2702
Chris Wilsona00b10c2010-09-24 21:15:47 +01002703 fence_size = i915_gem_get_gtt_size(obj_priv);
2704 fence_alignment = i915_gem_get_gtt_alignment(obj_priv);
2705
Eric Anholt673a3942008-07-30 12:06:12 -07002706 if (alignment == 0)
Daniel Vetter75e9e912010-11-04 17:11:09 +01002707 alignment = map_and_fenceable ? fence_alignment : 4096;
2708 if (map_and_fenceable && alignment & (fence_alignment - 1)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002709 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2710 return -EINVAL;
2711 }
2712
Daniel Vetter75e9e912010-11-04 17:11:09 +01002713 size = map_and_fenceable ? fence_size : obj->size;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002714
Chris Wilson654fc602010-05-27 13:18:21 +01002715 /* If the object is bigger than the entire aperture, reject it early
2716 * before evicting everything in a vain attempt to find space.
2717 */
Daniel Vetter920afa72010-09-16 17:54:23 +02002718 if (obj->size >
Daniel Vetter75e9e912010-11-04 17:11:09 +01002719 (map_and_fenceable ? dev_priv->mm.gtt_mappable_end : dev_priv->mm.gtt_total)) {
Chris Wilson654fc602010-05-27 13:18:21 +01002720 DRM_ERROR("Attempting to bind an object larger than the aperture\n");
2721 return -E2BIG;
2722 }
2723
Eric Anholt673a3942008-07-30 12:06:12 -07002724 search_free:
Daniel Vetter75e9e912010-11-04 17:11:09 +01002725 if (map_and_fenceable)
Daniel Vetter920afa72010-09-16 17:54:23 +02002726 free_space =
2727 drm_mm_search_free_in_range(&dev_priv->mm.gtt_space,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002728 size, alignment, 0,
Daniel Vetter920afa72010-09-16 17:54:23 +02002729 dev_priv->mm.gtt_mappable_end,
2730 0);
2731 else
2732 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002733 size, alignment, 0);
Daniel Vetter920afa72010-09-16 17:54:23 +02002734
2735 if (free_space != NULL) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01002736 if (map_and_fenceable)
Daniel Vetter920afa72010-09-16 17:54:23 +02002737 obj_priv->gtt_space =
2738 drm_mm_get_block_range_generic(free_space,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002739 size, alignment, 0,
Daniel Vetter920afa72010-09-16 17:54:23 +02002740 dev_priv->mm.gtt_mappable_end,
2741 0);
2742 else
2743 obj_priv->gtt_space =
Chris Wilsona00b10c2010-09-24 21:15:47 +01002744 drm_mm_get_block(free_space, size, alignment);
Daniel Vetter920afa72010-09-16 17:54:23 +02002745 }
Eric Anholt673a3942008-07-30 12:06:12 -07002746 if (obj_priv->gtt_space == NULL) {
2747 /* If the gtt is empty and we're still having trouble
2748 * fitting our object in, we're out of memory.
2749 */
Daniel Vetter75e9e912010-11-04 17:11:09 +01002750 ret = i915_gem_evict_something(dev, size, alignment,
2751 map_and_fenceable);
Chris Wilson97311292009-09-21 00:22:34 +01002752 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002753 return ret;
Chris Wilson97311292009-09-21 00:22:34 +01002754
Eric Anholt673a3942008-07-30 12:06:12 -07002755 goto search_free;
2756 }
2757
Chris Wilsone5281cc2010-10-28 13:45:36 +01002758 ret = i915_gem_object_get_pages_gtt(obj, gfpmask);
Eric Anholt673a3942008-07-30 12:06:12 -07002759 if (ret) {
2760 drm_mm_put_block(obj_priv->gtt_space);
2761 obj_priv->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002762
2763 if (ret == -ENOMEM) {
2764 /* first try to clear up some space from the GTT */
Chris Wilsona00b10c2010-09-24 21:15:47 +01002765 ret = i915_gem_evict_something(dev, size,
Daniel Vetter75e9e912010-11-04 17:11:09 +01002766 alignment,
2767 map_and_fenceable);
Chris Wilson07f73f62009-09-14 16:50:30 +01002768 if (ret) {
Chris Wilson07f73f62009-09-14 16:50:30 +01002769 /* now try to shrink everyone else */
Chris Wilson4bdadb92010-01-27 13:36:32 +00002770 if (gfpmask) {
2771 gfpmask = 0;
2772 goto search_free;
Chris Wilson07f73f62009-09-14 16:50:30 +01002773 }
2774
2775 return ret;
2776 }
2777
2778 goto search_free;
2779 }
2780
Eric Anholt673a3942008-07-30 12:06:12 -07002781 return ret;
2782 }
2783
Eric Anholt673a3942008-07-30 12:06:12 -07002784 /* Create an AGP memory structure pointing at our pages, and bind it
2785 * into the GTT.
2786 */
2787 obj_priv->agp_mem = drm_agp_bind_pages(dev,
Eric Anholt856fa192009-03-19 14:10:50 -07002788 obj_priv->pages,
Chris Wilson07f73f62009-09-14 16:50:30 +01002789 obj->size >> PAGE_SHIFT,
Chris Wilson9af90d12010-10-17 10:01:56 +01002790 obj_priv->gtt_space->start,
Keith Packardba1eb1d2008-10-14 19:55:10 -07002791 obj_priv->agp_type);
Eric Anholt673a3942008-07-30 12:06:12 -07002792 if (obj_priv->agp_mem == NULL) {
Chris Wilsone5281cc2010-10-28 13:45:36 +01002793 i915_gem_object_put_pages_gtt(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002794 drm_mm_put_block(obj_priv->gtt_space);
2795 obj_priv->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002796
Chris Wilsona00b10c2010-09-24 21:15:47 +01002797 ret = i915_gem_evict_something(dev, size,
Daniel Vetter75e9e912010-11-04 17:11:09 +01002798 alignment, map_and_fenceable);
Chris Wilson97311292009-09-21 00:22:34 +01002799 if (ret)
Chris Wilson07f73f62009-09-14 16:50:30 +01002800 return ret;
Chris Wilson07f73f62009-09-14 16:50:30 +01002801
2802 goto search_free;
Eric Anholt673a3942008-07-30 12:06:12 -07002803 }
Eric Anholt673a3942008-07-30 12:06:12 -07002804
Daniel Vetterfb7d5162010-10-01 22:05:20 +02002805 obj_priv->gtt_offset = obj_priv->gtt_space->start;
2806
Chris Wilsonbf1a1092010-08-07 11:01:20 +01002807 /* keep track of bounds object by adding it to the inactive list */
Chris Wilson69dc4982010-10-19 10:36:51 +01002808 list_add_tail(&obj_priv->mm_list, &dev_priv->mm.inactive_list);
Chris Wilsona00b10c2010-09-24 21:15:47 +01002809 i915_gem_info_add_gtt(dev_priv, obj_priv);
Chris Wilsonbf1a1092010-08-07 11:01:20 +01002810
Eric Anholt673a3942008-07-30 12:06:12 -07002811 /* Assert that the object is not currently in any GPU domain. As it
2812 * wasn't in the GTT, there shouldn't be any way it could have been in
2813 * a GPU cache
2814 */
Chris Wilson21d509e2009-06-06 09:46:02 +01002815 BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS);
2816 BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS);
Eric Anholt673a3942008-07-30 12:06:12 -07002817
Daniel Vetter75e9e912010-11-04 17:11:09 +01002818 trace_i915_gem_object_bind(obj, obj_priv->gtt_offset, map_and_fenceable);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002819
Daniel Vetter75e9e912010-11-04 17:11:09 +01002820 fenceable =
Chris Wilsona00b10c2010-09-24 21:15:47 +01002821 obj_priv->gtt_space->size == fence_size &&
2822 (obj_priv->gtt_space->start & (fence_alignment -1)) == 0;
2823
Daniel Vetter75e9e912010-11-04 17:11:09 +01002824 mappable =
Chris Wilsona00b10c2010-09-24 21:15:47 +01002825 obj_priv->gtt_offset + obj->size <= dev_priv->mm.gtt_mappable_end;
2826
Daniel Vetter75e9e912010-11-04 17:11:09 +01002827 obj_priv->map_and_fenceable = mappable && fenceable;
2828
Eric Anholt673a3942008-07-30 12:06:12 -07002829 return 0;
2830}
2831
2832void
2833i915_gem_clflush_object(struct drm_gem_object *obj)
2834{
Daniel Vetter23010e42010-03-08 13:35:02 +01002835 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002836
2837 /* If we don't have a page list set up, then we're not pinned
2838 * to GPU, and we can ignore the cache flush because it'll happen
2839 * again at bind time.
2840 */
Eric Anholt856fa192009-03-19 14:10:50 -07002841 if (obj_priv->pages == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002842 return;
2843
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002844 trace_i915_gem_object_clflush(obj);
Eric Anholtcfa16a02009-05-26 18:46:16 -07002845
Eric Anholt856fa192009-03-19 14:10:50 -07002846 drm_clflush_pages(obj_priv->pages, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002847}
2848
Eric Anholte47c68e2008-11-14 13:35:19 -08002849/** Flushes any GPU write domain for the object if it's dirty. */
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002850static int
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002851i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj,
2852 bool pipelined)
Eric Anholte47c68e2008-11-14 13:35:19 -08002853{
2854 struct drm_device *dev = obj->dev;
Eric Anholte47c68e2008-11-14 13:35:19 -08002855
2856 if ((obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002857 return 0;
Eric Anholte47c68e2008-11-14 13:35:19 -08002858
2859 /* Queue the GPU write cache flushing we need. */
Chris Wilsonc78ec302010-09-20 12:50:23 +01002860 i915_gem_flush_ring(dev, NULL,
Chris Wilson92204342010-09-18 11:02:01 +01002861 to_intel_bo(obj)->ring,
2862 0, obj->write_domain);
Chris Wilson48b956c2010-09-14 12:50:34 +01002863 BUG_ON(obj->write_domain);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002864
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002865 if (pipelined)
2866 return 0;
2867
Chris Wilson2cf34d72010-09-14 13:03:28 +01002868 return i915_gem_object_wait_rendering(obj, true);
Eric Anholte47c68e2008-11-14 13:35:19 -08002869}
2870
2871/** Flushes the GTT write domain for the object if it's dirty. */
2872static void
2873i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj)
2874{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002875 uint32_t old_write_domain;
2876
Eric Anholte47c68e2008-11-14 13:35:19 -08002877 if (obj->write_domain != I915_GEM_DOMAIN_GTT)
2878 return;
2879
2880 /* No actual flushing is required for the GTT write domain. Writes
2881 * to it immediately go to main memory as far as we know, so there's
2882 * no chipset flush. It also doesn't land in render cache.
2883 */
Chris Wilson4a684a42010-10-28 14:44:08 +01002884 i915_gem_release_mmap(obj);
2885
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002886 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002887 obj->write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002888
2889 trace_i915_gem_object_change_domain(obj,
2890 obj->read_domains,
2891 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002892}
2893
2894/** Flushes the CPU write domain for the object if it's dirty. */
2895static void
2896i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj)
2897{
2898 struct drm_device *dev = obj->dev;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002899 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002900
2901 if (obj->write_domain != I915_GEM_DOMAIN_CPU)
2902 return;
2903
2904 i915_gem_clflush_object(obj);
2905 drm_agp_chipset_flush(dev);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002906 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002907 obj->write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002908
2909 trace_i915_gem_object_change_domain(obj,
2910 obj->read_domains,
2911 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002912}
2913
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002914/**
2915 * Moves a single object to the GTT read, and possibly write domain.
2916 *
2917 * This function returns when the move is complete, including waiting on
2918 * flushes to occur.
2919 */
Jesse Barnes79e53942008-11-07 14:24:08 -08002920int
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002921i915_gem_object_set_to_gtt_domain(struct drm_gem_object *obj, int write)
2922{
Daniel Vetter23010e42010-03-08 13:35:02 +01002923 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002924 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002925 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002926
Eric Anholt02354392008-11-26 13:58:13 -08002927 /* Not valid to be called on unbound objects. */
2928 if (obj_priv->gtt_space == NULL)
2929 return -EINVAL;
2930
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002931 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002932 if (ret != 0)
2933 return ret;
2934
Chris Wilson72133422010-09-13 23:56:38 +01002935 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002936
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002937 if (write) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01002938 ret = i915_gem_object_wait_rendering(obj, true);
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002939 if (ret)
2940 return ret;
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002941 }
Eric Anholte47c68e2008-11-14 13:35:19 -08002942
2943 old_write_domain = obj->write_domain;
2944 old_read_domains = obj->read_domains;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002945
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002946 /* It should now be out of any other write domains, and we can update
2947 * the domain values for our changes.
2948 */
2949 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2950 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002951 if (write) {
Chris Wilson72133422010-09-13 23:56:38 +01002952 obj->read_domains = I915_GEM_DOMAIN_GTT;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002953 obj->write_domain = I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002954 obj_priv->dirty = 1;
2955 }
2956
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002957 trace_i915_gem_object_change_domain(obj,
2958 old_read_domains,
2959 old_write_domain);
2960
Eric Anholte47c68e2008-11-14 13:35:19 -08002961 return 0;
2962}
2963
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002964/*
2965 * Prepare buffer for display plane. Use uninterruptible for possible flush
2966 * wait, as in modesetting process we're not supposed to be interrupted.
2967 */
2968int
Chris Wilson48b956c2010-09-14 12:50:34 +01002969i915_gem_object_set_to_display_plane(struct drm_gem_object *obj,
2970 bool pipelined)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002971{
Daniel Vetter23010e42010-03-08 13:35:02 +01002972 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002973 uint32_t old_read_domains;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002974 int ret;
2975
2976 /* Not valid to be called on unbound objects. */
2977 if (obj_priv->gtt_space == NULL)
2978 return -EINVAL;
2979
Chris Wilsonced270f2010-09-26 22:47:46 +01002980 ret = i915_gem_object_flush_gpu_write_domain(obj, true);
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002981 if (ret)
2982 return ret;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002983
Chris Wilsonced270f2010-09-26 22:47:46 +01002984 /* Currently, we are always called from an non-interruptible context. */
2985 if (!pipelined) {
2986 ret = i915_gem_object_wait_rendering(obj, false);
2987 if (ret)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002988 return ret;
2989 }
2990
Chris Wilsonb118c1e2010-05-27 13:18:14 +01002991 i915_gem_object_flush_cpu_write_domain(obj);
2992
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002993 old_read_domains = obj->read_domains;
Chris Wilsonc78ec302010-09-20 12:50:23 +01002994 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002995
2996 trace_i915_gem_object_change_domain(obj,
2997 old_read_domains,
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002998 obj->write_domain);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002999
3000 return 0;
3001}
3002
Eric Anholte47c68e2008-11-14 13:35:19 -08003003/**
3004 * Moves a single object to the CPU read, and possibly write domain.
3005 *
3006 * This function returns when the move is complete, including waiting on
3007 * flushes to occur.
3008 */
3009static int
3010i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj, int write)
3011{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003012 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003013 int ret;
3014
Daniel Vetterba3d8d72010-02-11 22:37:04 +01003015 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Eric Anholte47c68e2008-11-14 13:35:19 -08003016 if (ret != 0)
3017 return ret;
3018
3019 i915_gem_object_flush_gtt_write_domain(obj);
3020
3021 /* If we have a partially-valid cache of the object in the CPU,
3022 * finish invalidating it and free the per-page flags.
3023 */
3024 i915_gem_object_set_to_full_cpu_read_domain(obj);
3025
Chris Wilson72133422010-09-13 23:56:38 +01003026 if (write) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01003027 ret = i915_gem_object_wait_rendering(obj, true);
Chris Wilson72133422010-09-13 23:56:38 +01003028 if (ret)
3029 return ret;
3030 }
3031
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003032 old_write_domain = obj->write_domain;
3033 old_read_domains = obj->read_domains;
3034
Eric Anholte47c68e2008-11-14 13:35:19 -08003035 /* Flush the CPU cache if it's still invalid. */
3036 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
3037 i915_gem_clflush_object(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003038
3039 obj->read_domains |= I915_GEM_DOMAIN_CPU;
3040 }
3041
3042 /* It should now be out of any other write domains, and we can update
3043 * the domain values for our changes.
3044 */
3045 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
3046
3047 /* If we're writing through the CPU, then the GPU read domains will
3048 * need to be invalidated at next use.
3049 */
3050 if (write) {
Chris Wilsonc78ec302010-09-20 12:50:23 +01003051 obj->read_domains = I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003052 obj->write_domain = I915_GEM_DOMAIN_CPU;
3053 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003054
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003055 trace_i915_gem_object_change_domain(obj,
3056 old_read_domains,
3057 old_write_domain);
3058
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003059 return 0;
3060}
3061
Eric Anholt673a3942008-07-30 12:06:12 -07003062/*
3063 * Set the next domain for the specified object. This
3064 * may not actually perform the necessary flushing/invaliding though,
3065 * as that may want to be batched with other set_domain operations
3066 *
3067 * This is (we hope) the only really tricky part of gem. The goal
3068 * is fairly simple -- track which caches hold bits of the object
3069 * and make sure they remain coherent. A few concrete examples may
3070 * help to explain how it works. For shorthand, we use the notation
3071 * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
3072 * a pair of read and write domain masks.
3073 *
3074 * Case 1: the batch buffer
3075 *
3076 * 1. Allocated
3077 * 2. Written by CPU
3078 * 3. Mapped to GTT
3079 * 4. Read by GPU
3080 * 5. Unmapped from GTT
3081 * 6. Freed
3082 *
3083 * Let's take these a step at a time
3084 *
3085 * 1. Allocated
3086 * Pages allocated from the kernel may still have
3087 * cache contents, so we set them to (CPU, CPU) always.
3088 * 2. Written by CPU (using pwrite)
3089 * The pwrite function calls set_domain (CPU, CPU) and
3090 * this function does nothing (as nothing changes)
3091 * 3. Mapped by GTT
3092 * This function asserts that the object is not
3093 * currently in any GPU-based read or write domains
3094 * 4. Read by GPU
3095 * i915_gem_execbuffer calls set_domain (COMMAND, 0).
3096 * As write_domain is zero, this function adds in the
3097 * current read domains (CPU+COMMAND, 0).
3098 * flush_domains is set to CPU.
3099 * invalidate_domains is set to COMMAND
3100 * clflush is run to get data out of the CPU caches
3101 * then i915_dev_set_domain calls i915_gem_flush to
3102 * emit an MI_FLUSH and drm_agp_chipset_flush
3103 * 5. Unmapped from GTT
3104 * i915_gem_object_unbind calls set_domain (CPU, CPU)
3105 * flush_domains and invalidate_domains end up both zero
3106 * so no flushing/invalidating happens
3107 * 6. Freed
3108 * yay, done
3109 *
3110 * Case 2: The shared render buffer
3111 *
3112 * 1. Allocated
3113 * 2. Mapped to GTT
3114 * 3. Read/written by GPU
3115 * 4. set_domain to (CPU,CPU)
3116 * 5. Read/written by CPU
3117 * 6. Read/written by GPU
3118 *
3119 * 1. Allocated
3120 * Same as last example, (CPU, CPU)
3121 * 2. Mapped to GTT
3122 * Nothing changes (assertions find that it is not in the GPU)
3123 * 3. Read/written by GPU
3124 * execbuffer calls set_domain (RENDER, RENDER)
3125 * flush_domains gets CPU
3126 * invalidate_domains gets GPU
3127 * clflush (obj)
3128 * MI_FLUSH and drm_agp_chipset_flush
3129 * 4. set_domain (CPU, CPU)
3130 * flush_domains gets GPU
3131 * invalidate_domains gets CPU
3132 * wait_rendering (obj) to make sure all drawing is complete.
3133 * This will include an MI_FLUSH to get the data from GPU
3134 * to memory
3135 * clflush (obj) to invalidate the CPU cache
3136 * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
3137 * 5. Read/written by CPU
3138 * cache lines are loaded and dirtied
3139 * 6. Read written by GPU
3140 * Same as last GPU access
3141 *
3142 * Case 3: The constant buffer
3143 *
3144 * 1. Allocated
3145 * 2. Written by CPU
3146 * 3. Read by GPU
3147 * 4. Updated (written) by CPU again
3148 * 5. Read by GPU
3149 *
3150 * 1. Allocated
3151 * (CPU, CPU)
3152 * 2. Written by CPU
3153 * (CPU, CPU)
3154 * 3. Read by GPU
3155 * (CPU+RENDER, 0)
3156 * flush_domains = CPU
3157 * invalidate_domains = RENDER
3158 * clflush (obj)
3159 * MI_FLUSH
3160 * drm_agp_chipset_flush
3161 * 4. Updated (written) by CPU again
3162 * (CPU, CPU)
3163 * flush_domains = 0 (no previous write domain)
3164 * invalidate_domains = 0 (no new read domains)
3165 * 5. Read by GPU
3166 * (CPU+RENDER, 0)
3167 * flush_domains = CPU
3168 * invalidate_domains = RENDER
3169 * clflush (obj)
3170 * MI_FLUSH
3171 * drm_agp_chipset_flush
3172 */
Keith Packardc0d90822008-11-20 23:11:08 -08003173static void
Chris Wilsonb6651452010-10-23 10:15:06 +01003174i915_gem_object_set_to_gpu_domain(struct drm_gem_object *obj,
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003175 struct intel_ring_buffer *ring,
3176 struct change_domains *cd)
Eric Anholt673a3942008-07-30 12:06:12 -07003177{
Daniel Vetter23010e42010-03-08 13:35:02 +01003178 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003179 uint32_t invalidate_domains = 0;
3180 uint32_t flush_domains = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07003181
Eric Anholt673a3942008-07-30 12:06:12 -07003182 /*
3183 * If the object isn't moving to a new write domain,
3184 * let the object stay in multiple read domains
3185 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003186 if (obj->pending_write_domain == 0)
3187 obj->pending_read_domains |= obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07003188
3189 /*
3190 * Flush the current write domain if
3191 * the new read domains don't match. Invalidate
3192 * any read domains which differ from the old
3193 * write domain
3194 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003195 if (obj->write_domain &&
Chris Wilson13b29282010-11-01 12:22:48 +00003196 (obj->write_domain != obj->pending_read_domains ||
3197 obj_priv->ring != ring)) {
Eric Anholt673a3942008-07-30 12:06:12 -07003198 flush_domains |= obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08003199 invalidate_domains |=
3200 obj->pending_read_domains & ~obj->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07003201 }
3202 /*
3203 * Invalidate any read caches which may have
3204 * stale data. That is, any new read domains.
3205 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003206 invalidate_domains |= obj->pending_read_domains & ~obj->read_domains;
Chris Wilson3d2a8122010-09-29 11:39:53 +01003207 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU)
Eric Anholt673a3942008-07-30 12:06:12 -07003208 i915_gem_clflush_object(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003209
Chris Wilson4a684a42010-10-28 14:44:08 +01003210 /* blow away mappings if mapped through GTT */
3211 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_GTT)
3212 i915_gem_release_mmap(obj);
3213
Eric Anholtefbeed92009-02-19 14:54:51 -08003214 /* The actual obj->write_domain will be updated with
3215 * pending_write_domain after we emit the accumulated flush for all
3216 * of our domain changes in execbuffers (which clears objects'
3217 * write_domains). So if we have a current write domain that we
3218 * aren't changing, set pending_write_domain to that.
3219 */
3220 if (flush_domains == 0 && obj->pending_write_domain == 0)
3221 obj->pending_write_domain = obj->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07003222
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003223 cd->invalidate_domains |= invalidate_domains;
3224 cd->flush_domains |= flush_domains;
Chris Wilsonb6651452010-10-23 10:15:06 +01003225 if (flush_domains & I915_GEM_GPU_DOMAINS)
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003226 cd->flush_rings |= obj_priv->ring->id;
Chris Wilsonb6651452010-10-23 10:15:06 +01003227 if (invalidate_domains & I915_GEM_GPU_DOMAINS)
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003228 cd->flush_rings |= ring->id;
Eric Anholt673a3942008-07-30 12:06:12 -07003229}
3230
3231/**
Eric Anholte47c68e2008-11-14 13:35:19 -08003232 * Moves the object from a partially CPU read to a full one.
Eric Anholt673a3942008-07-30 12:06:12 -07003233 *
Eric Anholte47c68e2008-11-14 13:35:19 -08003234 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
3235 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
3236 */
3237static void
3238i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj)
3239{
Daniel Vetter23010e42010-03-08 13:35:02 +01003240 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003241
3242 if (!obj_priv->page_cpu_valid)
3243 return;
3244
3245 /* If we're partially in the CPU read domain, finish moving it in.
3246 */
3247 if (obj->read_domains & I915_GEM_DOMAIN_CPU) {
3248 int i;
3249
3250 for (i = 0; i <= (obj->size - 1) / PAGE_SIZE; i++) {
3251 if (obj_priv->page_cpu_valid[i])
3252 continue;
Eric Anholt856fa192009-03-19 14:10:50 -07003253 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholte47c68e2008-11-14 13:35:19 -08003254 }
Eric Anholte47c68e2008-11-14 13:35:19 -08003255 }
3256
3257 /* Free the page_cpu_valid mappings which are now stale, whether
3258 * or not we've got I915_GEM_DOMAIN_CPU.
3259 */
Eric Anholt9a298b22009-03-24 12:23:04 -07003260 kfree(obj_priv->page_cpu_valid);
Eric Anholte47c68e2008-11-14 13:35:19 -08003261 obj_priv->page_cpu_valid = NULL;
3262}
3263
3264/**
3265 * Set the CPU read domain on a range of the object.
3266 *
3267 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
3268 * not entirely valid. The page_cpu_valid member of the object flags which
3269 * pages have been flushed, and will be respected by
3270 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
3271 * of the whole object.
3272 *
3273 * This function returns when the move is complete, including waiting on
3274 * flushes to occur.
Eric Anholt673a3942008-07-30 12:06:12 -07003275 */
3276static int
Eric Anholte47c68e2008-11-14 13:35:19 -08003277i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
3278 uint64_t offset, uint64_t size)
Eric Anholt673a3942008-07-30 12:06:12 -07003279{
Daniel Vetter23010e42010-03-08 13:35:02 +01003280 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003281 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003282 int i, ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003283
Eric Anholte47c68e2008-11-14 13:35:19 -08003284 if (offset == 0 && size == obj->size)
3285 return i915_gem_object_set_to_cpu_domain(obj, 0);
3286
Daniel Vetterba3d8d72010-02-11 22:37:04 +01003287 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Eric Anholte47c68e2008-11-14 13:35:19 -08003288 if (ret != 0)
3289 return ret;
3290 i915_gem_object_flush_gtt_write_domain(obj);
3291
3292 /* If we're already fully in the CPU read domain, we're done. */
3293 if (obj_priv->page_cpu_valid == NULL &&
3294 (obj->read_domains & I915_GEM_DOMAIN_CPU) != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07003295 return 0;
3296
Eric Anholte47c68e2008-11-14 13:35:19 -08003297 /* Otherwise, create/clear the per-page CPU read domain flag if we're
3298 * newly adding I915_GEM_DOMAIN_CPU
3299 */
Eric Anholt673a3942008-07-30 12:06:12 -07003300 if (obj_priv->page_cpu_valid == NULL) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003301 obj_priv->page_cpu_valid = kzalloc(obj->size / PAGE_SIZE,
3302 GFP_KERNEL);
Eric Anholte47c68e2008-11-14 13:35:19 -08003303 if (obj_priv->page_cpu_valid == NULL)
3304 return -ENOMEM;
3305 } else if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0)
3306 memset(obj_priv->page_cpu_valid, 0, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07003307
3308 /* Flush the cache on any pages that are still invalid from the CPU's
3309 * perspective.
3310 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003311 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
3312 i++) {
Eric Anholt673a3942008-07-30 12:06:12 -07003313 if (obj_priv->page_cpu_valid[i])
3314 continue;
3315
Eric Anholt856fa192009-03-19 14:10:50 -07003316 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07003317
3318 obj_priv->page_cpu_valid[i] = 1;
3319 }
3320
Eric Anholte47c68e2008-11-14 13:35:19 -08003321 /* It should now be out of any other write domains, and we can update
3322 * the domain values for our changes.
3323 */
3324 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
3325
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003326 old_read_domains = obj->read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003327 obj->read_domains |= I915_GEM_DOMAIN_CPU;
3328
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003329 trace_i915_gem_object_change_domain(obj,
3330 old_read_domains,
3331 obj->write_domain);
3332
Eric Anholt673a3942008-07-30 12:06:12 -07003333 return 0;
3334}
3335
3336/**
Eric Anholt673a3942008-07-30 12:06:12 -07003337 * Pin an object to the GTT and evaluate the relocations landing in it.
3338 */
3339static int
Chris Wilson9af90d12010-10-17 10:01:56 +01003340i915_gem_execbuffer_relocate(struct drm_i915_gem_object *obj,
3341 struct drm_file *file_priv,
3342 struct drm_i915_gem_exec_object2 *entry)
Eric Anholt673a3942008-07-30 12:06:12 -07003343{
Chris Wilson9af90d12010-10-17 10:01:56 +01003344 struct drm_device *dev = obj->base.dev;
Keith Packard0839ccb2008-10-30 19:38:48 -07003345 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson2549d6c2010-10-14 12:10:41 +01003346 struct drm_i915_gem_relocation_entry __user *user_relocs;
Chris Wilson9af90d12010-10-17 10:01:56 +01003347 struct drm_gem_object *target_obj = NULL;
3348 uint32_t target_handle = 0;
3349 int i, ret = 0;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003350
Chris Wilson2549d6c2010-10-14 12:10:41 +01003351 user_relocs = (void __user *)(uintptr_t)entry->relocs_ptr;
Eric Anholt673a3942008-07-30 12:06:12 -07003352 for (i = 0; i < entry->relocation_count; i++) {
Chris Wilson2549d6c2010-10-14 12:10:41 +01003353 struct drm_i915_gem_relocation_entry reloc;
Chris Wilson9af90d12010-10-17 10:01:56 +01003354 uint32_t target_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003355
Chris Wilson9af90d12010-10-17 10:01:56 +01003356 if (__copy_from_user_inatomic(&reloc,
3357 user_relocs+i,
3358 sizeof(reloc))) {
3359 ret = -EFAULT;
3360 break;
Eric Anholt673a3942008-07-30 12:06:12 -07003361 }
Chris Wilson2549d6c2010-10-14 12:10:41 +01003362
Chris Wilson9af90d12010-10-17 10:01:56 +01003363 if (reloc.target_handle != target_handle) {
3364 drm_gem_object_unreference(target_obj);
3365
3366 target_obj = drm_gem_object_lookup(dev, file_priv,
3367 reloc.target_handle);
3368 if (target_obj == NULL) {
3369 ret = -ENOENT;
3370 break;
3371 }
3372
3373 target_handle = reloc.target_handle;
Eric Anholt673a3942008-07-30 12:06:12 -07003374 }
Chris Wilson9af90d12010-10-17 10:01:56 +01003375 target_offset = to_intel_bo(target_obj)->gtt_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003376
Chris Wilson8542a0b2009-09-09 21:15:15 +01003377#if WATCH_RELOC
3378 DRM_INFO("%s: obj %p offset %08x target %d "
3379 "read %08x write %08x gtt %08x "
3380 "presumed %08x delta %08x\n",
3381 __func__,
3382 obj,
Chris Wilson2549d6c2010-10-14 12:10:41 +01003383 (int) reloc.offset,
3384 (int) reloc.target_handle,
3385 (int) reloc.read_domains,
3386 (int) reloc.write_domain,
Chris Wilson9af90d12010-10-17 10:01:56 +01003387 (int) target_offset,
Chris Wilson2549d6c2010-10-14 12:10:41 +01003388 (int) reloc.presumed_offset,
3389 reloc.delta);
Chris Wilson8542a0b2009-09-09 21:15:15 +01003390#endif
3391
Eric Anholt673a3942008-07-30 12:06:12 -07003392 /* The target buffer should have appeared before us in the
3393 * exec_object list, so it should have a GTT space bound by now.
3394 */
Chris Wilson9af90d12010-10-17 10:01:56 +01003395 if (target_offset == 0) {
Eric Anholt673a3942008-07-30 12:06:12 -07003396 DRM_ERROR("No GTT space found for object %d\n",
Chris Wilson2549d6c2010-10-14 12:10:41 +01003397 reloc.target_handle);
Chris Wilson9af90d12010-10-17 10:01:56 +01003398 ret = -EINVAL;
3399 break;
Eric Anholt673a3942008-07-30 12:06:12 -07003400 }
3401
Chris Wilson8542a0b2009-09-09 21:15:15 +01003402 /* Validate that the target is in a valid r/w GPU domain */
Chris Wilson2549d6c2010-10-14 12:10:41 +01003403 if (reloc.write_domain & (reloc.write_domain - 1)) {
Daniel Vetter16edd552010-02-19 11:52:02 +01003404 DRM_ERROR("reloc with multiple write domains: "
3405 "obj %p target %d offset %d "
3406 "read %08x write %08x",
Chris Wilson2549d6c2010-10-14 12:10:41 +01003407 obj, reloc.target_handle,
3408 (int) reloc.offset,
3409 reloc.read_domains,
3410 reloc.write_domain);
Chris Wilson9af90d12010-10-17 10:01:56 +01003411 ret = -EINVAL;
3412 break;
Daniel Vetter16edd552010-02-19 11:52:02 +01003413 }
Chris Wilson2549d6c2010-10-14 12:10:41 +01003414 if (reloc.write_domain & I915_GEM_DOMAIN_CPU ||
3415 reloc.read_domains & I915_GEM_DOMAIN_CPU) {
Chris Wilson8542a0b2009-09-09 21:15:15 +01003416 DRM_ERROR("reloc with read/write CPU domains: "
3417 "obj %p target %d offset %d "
3418 "read %08x write %08x",
Chris Wilson2549d6c2010-10-14 12:10:41 +01003419 obj, reloc.target_handle,
3420 (int) reloc.offset,
3421 reloc.read_domains,
3422 reloc.write_domain);
Chris Wilson9af90d12010-10-17 10:01:56 +01003423 ret = -EINVAL;
3424 break;
Chris Wilson8542a0b2009-09-09 21:15:15 +01003425 }
Chris Wilson2549d6c2010-10-14 12:10:41 +01003426 if (reloc.write_domain && target_obj->pending_write_domain &&
3427 reloc.write_domain != target_obj->pending_write_domain) {
Chris Wilson8542a0b2009-09-09 21:15:15 +01003428 DRM_ERROR("Write domain conflict: "
3429 "obj %p target %d offset %d "
3430 "new %08x old %08x\n",
Chris Wilson2549d6c2010-10-14 12:10:41 +01003431 obj, reloc.target_handle,
3432 (int) reloc.offset,
3433 reloc.write_domain,
Chris Wilson8542a0b2009-09-09 21:15:15 +01003434 target_obj->pending_write_domain);
Chris Wilson9af90d12010-10-17 10:01:56 +01003435 ret = -EINVAL;
3436 break;
Chris Wilson8542a0b2009-09-09 21:15:15 +01003437 }
3438
Chris Wilson2549d6c2010-10-14 12:10:41 +01003439 target_obj->pending_read_domains |= reloc.read_domains;
Chris Wilson878a3c32010-10-22 10:48:12 +01003440 target_obj->pending_write_domain |= reloc.write_domain;
Chris Wilson8542a0b2009-09-09 21:15:15 +01003441
3442 /* If the relocation already has the right value in it, no
3443 * more work needs to be done.
3444 */
Chris Wilson9af90d12010-10-17 10:01:56 +01003445 if (target_offset == reloc.presumed_offset)
Chris Wilson8542a0b2009-09-09 21:15:15 +01003446 continue;
Chris Wilson8542a0b2009-09-09 21:15:15 +01003447
3448 /* Check that the relocation address is valid... */
Chris Wilson9af90d12010-10-17 10:01:56 +01003449 if (reloc.offset > obj->base.size - 4) {
Eric Anholt673a3942008-07-30 12:06:12 -07003450 DRM_ERROR("Relocation beyond object bounds: "
3451 "obj %p target %d offset %d size %d.\n",
Chris Wilson2549d6c2010-10-14 12:10:41 +01003452 obj, reloc.target_handle,
Chris Wilson9af90d12010-10-17 10:01:56 +01003453 (int) reloc.offset, (int) obj->base.size);
3454 ret = -EINVAL;
3455 break;
Eric Anholt673a3942008-07-30 12:06:12 -07003456 }
Chris Wilson2549d6c2010-10-14 12:10:41 +01003457 if (reloc.offset & 3) {
Eric Anholt673a3942008-07-30 12:06:12 -07003458 DRM_ERROR("Relocation not 4-byte aligned: "
3459 "obj %p target %d offset %d.\n",
Chris Wilson2549d6c2010-10-14 12:10:41 +01003460 obj, reloc.target_handle,
3461 (int) reloc.offset);
Chris Wilson9af90d12010-10-17 10:01:56 +01003462 ret = -EINVAL;
3463 break;
Eric Anholt673a3942008-07-30 12:06:12 -07003464 }
3465
Chris Wilson8542a0b2009-09-09 21:15:15 +01003466 /* and points to somewhere within the target object. */
Chris Wilson2549d6c2010-10-14 12:10:41 +01003467 if (reloc.delta >= target_obj->size) {
Chris Wilsoncd0b9fb2009-09-15 23:23:18 +01003468 DRM_ERROR("Relocation beyond target object bounds: "
3469 "obj %p target %d delta %d size %d.\n",
Chris Wilson2549d6c2010-10-14 12:10:41 +01003470 obj, reloc.target_handle,
3471 (int) reloc.delta, (int) target_obj->size);
Chris Wilson9af90d12010-10-17 10:01:56 +01003472 ret = -EINVAL;
3473 break;
Eric Anholte47c68e2008-11-14 13:35:19 -08003474 }
3475
Chris Wilson9af90d12010-10-17 10:01:56 +01003476 reloc.delta += target_offset;
3477 if (obj->base.write_domain == I915_GEM_DOMAIN_CPU) {
Chris Wilsonf0c43d92010-10-14 12:44:48 +01003478 uint32_t page_offset = reloc.offset & ~PAGE_MASK;
3479 char *vaddr;
3480
Linus Torvaldsc48c43e2010-10-26 18:57:59 -07003481 vaddr = kmap_atomic(obj->pages[reloc.offset >> PAGE_SHIFT]);
Chris Wilsonf0c43d92010-10-14 12:44:48 +01003482 *(uint32_t *)(vaddr + page_offset) = reloc.delta;
Linus Torvaldsc48c43e2010-10-26 18:57:59 -07003483 kunmap_atomic(vaddr);
Chris Wilsonf0c43d92010-10-14 12:44:48 +01003484 } else {
3485 uint32_t __iomem *reloc_entry;
3486 void __iomem *reloc_page;
Chris Wilsonf0c43d92010-10-14 12:44:48 +01003487
Chris Wilson9af90d12010-10-17 10:01:56 +01003488 ret = i915_gem_object_set_to_gtt_domain(&obj->base, 1);
3489 if (ret)
3490 break;
Chris Wilsonf0c43d92010-10-14 12:44:48 +01003491
3492 /* Map the page containing the relocation we're going to perform. */
Chris Wilson9af90d12010-10-17 10:01:56 +01003493 reloc.offset += obj->gtt_offset;
Chris Wilsonf0c43d92010-10-14 12:44:48 +01003494 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
Linus Torvaldsc48c43e2010-10-26 18:57:59 -07003495 reloc.offset & PAGE_MASK);
Chris Wilsonf0c43d92010-10-14 12:44:48 +01003496 reloc_entry = (uint32_t __iomem *)
3497 (reloc_page + (reloc.offset & ~PAGE_MASK));
3498 iowrite32(reloc.delta, reloc_entry);
Linus Torvaldsc48c43e2010-10-26 18:57:59 -07003499 io_mapping_unmap_atomic(reloc_page);
Eric Anholt673a3942008-07-30 12:06:12 -07003500 }
3501
Chris Wilsonb5dc6082010-10-20 20:59:57 +01003502 /* and update the user's relocation entry */
3503 reloc.presumed_offset = target_offset;
3504 if (__copy_to_user_inatomic(&user_relocs[i].presumed_offset,
3505 &reloc.presumed_offset,
3506 sizeof(reloc.presumed_offset))) {
3507 ret = -EFAULT;
3508 break;
3509 }
Eric Anholt673a3942008-07-30 12:06:12 -07003510 }
3511
Chris Wilson9af90d12010-10-17 10:01:56 +01003512 drm_gem_object_unreference(target_obj);
3513 return ret;
3514}
3515
3516static int
3517i915_gem_execbuffer_pin(struct drm_device *dev,
3518 struct drm_file *file,
3519 struct drm_gem_object **object_list,
3520 struct drm_i915_gem_exec_object2 *exec_list,
3521 int count)
3522{
3523 struct drm_i915_private *dev_priv = dev->dev_private;
3524 int ret, i, retry;
3525
3526 /* attempt to pin all of the buffers into the GTT */
Chris Wilson5eac3ab2010-10-31 08:49:47 +00003527 retry = 0;
3528 do {
Chris Wilson9af90d12010-10-17 10:01:56 +01003529 ret = 0;
3530 for (i = 0; i < count; i++) {
3531 struct drm_i915_gem_exec_object2 *entry = &exec_list[i];
Daniel Vetter16e809a2010-09-16 19:37:04 +02003532 struct drm_i915_gem_object *obj = to_intel_bo(object_list[i]);
Chris Wilson9af90d12010-10-17 10:01:56 +01003533 bool need_fence =
3534 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
3535 obj->tiling_mode != I915_TILING_NONE;
3536
Daniel Vetter16e809a2010-09-16 19:37:04 +02003537 /* g33/pnv can't fence buffers in the unmappable part */
3538 bool need_mappable =
3539 entry->relocation_count ? true : need_fence;
3540
Chris Wilson9af90d12010-10-17 10:01:56 +01003541 /* Check fence reg constraints and rebind if necessary */
Daniel Vetter75e9e912010-11-04 17:11:09 +01003542 if (need_mappable && !obj->map_and_fenceable) {
Chris Wilson9af90d12010-10-17 10:01:56 +01003543 ret = i915_gem_object_unbind(&obj->base);
3544 if (ret)
3545 break;
3546 }
3547
Daniel Vetter920afa72010-09-16 17:54:23 +02003548 ret = i915_gem_object_pin(&obj->base,
Daniel Vetter16e809a2010-09-16 19:37:04 +02003549 entry->alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01003550 need_mappable);
Chris Wilson9af90d12010-10-17 10:01:56 +01003551 if (ret)
3552 break;
3553
3554 /*
3555 * Pre-965 chips need a fence register set up in order
3556 * to properly handle blits to/from tiled surfaces.
3557 */
3558 if (need_fence) {
3559 ret = i915_gem_object_get_fence_reg(&obj->base, true);
3560 if (ret) {
3561 i915_gem_object_unpin(&obj->base);
3562 break;
3563 }
3564
3565 dev_priv->fence_regs[obj->fence_reg].gpu = true;
3566 }
3567
3568 entry->offset = obj->gtt_offset;
3569 }
3570
3571 while (i--)
3572 i915_gem_object_unpin(object_list[i]);
3573
Chris Wilson5eac3ab2010-10-31 08:49:47 +00003574 if (ret != -ENOSPC || retry > 1)
Chris Wilson9af90d12010-10-17 10:01:56 +01003575 return ret;
3576
Chris Wilson5eac3ab2010-10-31 08:49:47 +00003577 /* First attempt, just clear anything that is purgeable.
3578 * Second attempt, clear the entire GTT.
3579 */
3580 ret = i915_gem_evict_everything(dev, retry == 0);
Chris Wilson9af90d12010-10-17 10:01:56 +01003581 if (ret)
3582 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003583
Chris Wilson5eac3ab2010-10-31 08:49:47 +00003584 retry++;
3585 } while (1);
Eric Anholt673a3942008-07-30 12:06:12 -07003586}
3587
Chris Wilson13b29282010-11-01 12:22:48 +00003588static int
3589i915_gem_execbuffer_move_to_gpu(struct drm_device *dev,
3590 struct drm_file *file,
3591 struct intel_ring_buffer *ring,
3592 struct drm_gem_object **objects,
3593 int count)
3594{
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003595 struct change_domains cd;
Chris Wilson13b29282010-11-01 12:22:48 +00003596 int ret, i;
3597
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003598 cd.invalidate_domains = 0;
3599 cd.flush_domains = 0;
3600 cd.flush_rings = 0;
Chris Wilson13b29282010-11-01 12:22:48 +00003601 for (i = 0; i < count; i++)
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003602 i915_gem_object_set_to_gpu_domain(objects[i], ring, &cd);
Chris Wilson13b29282010-11-01 12:22:48 +00003603
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003604 if (cd.invalidate_domains | cd.flush_domains) {
Chris Wilson13b29282010-11-01 12:22:48 +00003605#if WATCH_EXEC
3606 DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
3607 __func__,
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003608 cd.invalidate_domains,
3609 cd.flush_domains);
Chris Wilson13b29282010-11-01 12:22:48 +00003610#endif
3611 i915_gem_flush(dev, file,
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003612 cd.invalidate_domains,
3613 cd.flush_domains,
3614 cd.flush_rings);
Chris Wilson13b29282010-11-01 12:22:48 +00003615 }
3616
3617 for (i = 0; i < count; i++) {
3618 struct drm_i915_gem_object *obj = to_intel_bo(objects[i]);
3619 /* XXX replace with semaphores */
3620 if (obj->ring && ring != obj->ring) {
3621 ret = i915_gem_object_wait_rendering(&obj->base, true);
3622 if (ret)
3623 return ret;
3624 }
3625 }
3626
3627 return 0;
3628}
3629
Eric Anholt673a3942008-07-30 12:06:12 -07003630/* Throttle our rendering by waiting until the ring has completed our requests
3631 * emitted over 20 msec ago.
3632 *
Eric Anholtb9624422009-06-03 07:27:35 +00003633 * Note that if we were to use the current jiffies each time around the loop,
3634 * we wouldn't escape the function with any frames outstanding if the time to
3635 * render a frame was over 20ms.
3636 *
Eric Anholt673a3942008-07-30 12:06:12 -07003637 * This should get us reasonable parallelism between CPU and GPU but also
3638 * relatively low latency when blocking on a particular request to finish.
3639 */
3640static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003641i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003642{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003643 struct drm_i915_private *dev_priv = dev->dev_private;
3644 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00003645 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003646 struct drm_i915_gem_request *request;
3647 struct intel_ring_buffer *ring = NULL;
3648 u32 seqno = 0;
3649 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003650
Chris Wilson1c255952010-09-26 11:03:27 +01003651 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003652 list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
Eric Anholtb9624422009-06-03 07:27:35 +00003653 if (time_after_eq(request->emitted_jiffies, recent_enough))
3654 break;
3655
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003656 ring = request->ring;
3657 seqno = request->seqno;
Eric Anholtb9624422009-06-03 07:27:35 +00003658 }
Chris Wilson1c255952010-09-26 11:03:27 +01003659 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003660
3661 if (seqno == 0)
3662 return 0;
3663
3664 ret = 0;
Chris Wilson78501ea2010-10-27 12:18:21 +01003665 if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) {
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003666 /* And wait for the seqno passing without holding any locks and
3667 * causing extra latency for others. This is safe as the irq
3668 * generation is designed to be run atomically and so is
3669 * lockless.
3670 */
Chris Wilson78501ea2010-10-27 12:18:21 +01003671 ring->user_irq_get(ring);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003672 ret = wait_event_interruptible(ring->irq_queue,
Chris Wilson78501ea2010-10-27 12:18:21 +01003673 i915_seqno_passed(ring->get_seqno(ring), seqno)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003674 || atomic_read(&dev_priv->mm.wedged));
Chris Wilson78501ea2010-10-27 12:18:21 +01003675 ring->user_irq_put(ring);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003676
3677 if (ret == 0 && atomic_read(&dev_priv->mm.wedged))
3678 ret = -EIO;
3679 }
3680
3681 if (ret == 0)
3682 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
Eric Anholtb9624422009-06-03 07:27:35 +00003683
Eric Anholt673a3942008-07-30 12:06:12 -07003684 return ret;
3685}
3686
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003687static int
Chris Wilson2549d6c2010-10-14 12:10:41 +01003688i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec,
3689 uint64_t exec_offset)
Chris Wilson83d60792009-06-06 09:45:57 +01003690{
3691 uint32_t exec_start, exec_len;
3692
3693 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
3694 exec_len = (uint32_t) exec->batch_len;
3695
3696 if ((exec_start | exec_len) & 0x7)
3697 return -EINVAL;
3698
3699 if (!exec_start)
3700 return -EINVAL;
3701
3702 return 0;
3703}
3704
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003705static int
Chris Wilson2549d6c2010-10-14 12:10:41 +01003706validate_exec_list(struct drm_i915_gem_exec_object2 *exec,
3707 int count)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003708{
Chris Wilson2549d6c2010-10-14 12:10:41 +01003709 int i;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003710
Chris Wilson2549d6c2010-10-14 12:10:41 +01003711 for (i = 0; i < count; i++) {
3712 char __user *ptr = (char __user *)(uintptr_t)exec[i].relocs_ptr;
3713 size_t length = exec[i].relocation_count * sizeof(struct drm_i915_gem_relocation_entry);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003714
Chris Wilson2549d6c2010-10-14 12:10:41 +01003715 if (!access_ok(VERIFY_READ, ptr, length))
3716 return -EFAULT;
3717
Chris Wilsonb5dc6082010-10-20 20:59:57 +01003718 /* we may also need to update the presumed offsets */
3719 if (!access_ok(VERIFY_WRITE, ptr, length))
3720 return -EFAULT;
3721
Chris Wilson2549d6c2010-10-14 12:10:41 +01003722 if (fault_in_pages_readable(ptr, length))
3723 return -EFAULT;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003724 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003725
Chris Wilson2549d6c2010-10-14 12:10:41 +01003726 return 0;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003727}
3728
Chris Wilson2549d6c2010-10-14 12:10:41 +01003729static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003730i915_gem_do_execbuffer(struct drm_device *dev, void *data,
Chris Wilson9af90d12010-10-17 10:01:56 +01003731 struct drm_file *file,
Jesse Barnes76446ca2009-12-17 22:05:42 -05003732 struct drm_i915_gem_execbuffer2 *args,
3733 struct drm_i915_gem_exec_object2 *exec_list)
Eric Anholt673a3942008-07-30 12:06:12 -07003734{
3735 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003736 struct drm_gem_object **object_list = NULL;
3737 struct drm_gem_object *batch_obj;
Eric Anholt201361a2009-03-11 12:30:04 -07003738 struct drm_clip_rect *cliprects = NULL;
Chris Wilson8dc5d142010-08-12 12:36:12 +01003739 struct drm_i915_gem_request *request = NULL;
Chris Wilson9af90d12010-10-17 10:01:56 +01003740 int ret, i, flips;
Eric Anholt673a3942008-07-30 12:06:12 -07003741 uint64_t exec_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003742
Zou Nan hai852835f2010-05-21 09:08:56 +08003743 struct intel_ring_buffer *ring = NULL;
3744
Chris Wilson30dbf0c2010-09-25 10:19:17 +01003745 ret = i915_gem_check_is_wedged(dev);
3746 if (ret)
3747 return ret;
3748
Chris Wilson2549d6c2010-10-14 12:10:41 +01003749 ret = validate_exec_list(exec_list, args->buffer_count);
3750 if (ret)
3751 return ret;
3752
Eric Anholt673a3942008-07-30 12:06:12 -07003753#if WATCH_EXEC
3754 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3755 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3756#endif
Chris Wilson549f7362010-10-19 11:19:32 +01003757 switch (args->flags & I915_EXEC_RING_MASK) {
3758 case I915_EXEC_DEFAULT:
3759 case I915_EXEC_RENDER:
3760 ring = &dev_priv->render_ring;
3761 break;
3762 case I915_EXEC_BSD:
Zou Nan haid1b851f2010-05-21 09:08:57 +08003763 if (!HAS_BSD(dev)) {
Chris Wilson549f7362010-10-19 11:19:32 +01003764 DRM_ERROR("execbuf with invalid ring (BSD)\n");
Zou Nan haid1b851f2010-05-21 09:08:57 +08003765 return -EINVAL;
3766 }
3767 ring = &dev_priv->bsd_ring;
Chris Wilson549f7362010-10-19 11:19:32 +01003768 break;
3769 case I915_EXEC_BLT:
3770 if (!HAS_BLT(dev)) {
3771 DRM_ERROR("execbuf with invalid ring (BLT)\n");
3772 return -EINVAL;
3773 }
3774 ring = &dev_priv->blt_ring;
3775 break;
3776 default:
3777 DRM_ERROR("execbuf with unknown ring: %d\n",
3778 (int)(args->flags & I915_EXEC_RING_MASK));
3779 return -EINVAL;
Zou Nan haid1b851f2010-05-21 09:08:57 +08003780 }
3781
Eric Anholt4f481ed2008-09-10 14:22:49 -07003782 if (args->buffer_count < 1) {
3783 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3784 return -EINVAL;
3785 }
Eric Anholtc8e0f932009-11-22 03:49:37 +01003786 object_list = drm_malloc_ab(sizeof(*object_list), args->buffer_count);
Jesse Barnes76446ca2009-12-17 22:05:42 -05003787 if (object_list == NULL) {
3788 DRM_ERROR("Failed to allocate object list for %d buffers\n",
Eric Anholt673a3942008-07-30 12:06:12 -07003789 args->buffer_count);
3790 ret = -ENOMEM;
3791 goto pre_mutex_err;
3792 }
Eric Anholt673a3942008-07-30 12:06:12 -07003793
Eric Anholt201361a2009-03-11 12:30:04 -07003794 if (args->num_cliprects != 0) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003795 cliprects = kcalloc(args->num_cliprects, sizeof(*cliprects),
3796 GFP_KERNEL);
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003797 if (cliprects == NULL) {
3798 ret = -ENOMEM;
Eric Anholt201361a2009-03-11 12:30:04 -07003799 goto pre_mutex_err;
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003800 }
Eric Anholt201361a2009-03-11 12:30:04 -07003801
3802 ret = copy_from_user(cliprects,
3803 (struct drm_clip_rect __user *)
3804 (uintptr_t) args->cliprects_ptr,
3805 sizeof(*cliprects) * args->num_cliprects);
3806 if (ret != 0) {
3807 DRM_ERROR("copy %d cliprects failed: %d\n",
3808 args->num_cliprects, ret);
Dan Carpenterc877cdc2010-06-23 19:03:01 +02003809 ret = -EFAULT;
Eric Anholt201361a2009-03-11 12:30:04 -07003810 goto pre_mutex_err;
3811 }
3812 }
3813
Chris Wilson8dc5d142010-08-12 12:36:12 +01003814 request = kzalloc(sizeof(*request), GFP_KERNEL);
3815 if (request == NULL) {
3816 ret = -ENOMEM;
Chris Wilsona198bc82009-02-06 16:55:20 +00003817 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003818 }
3819
Chris Wilson76c1dec2010-09-25 11:22:51 +01003820 ret = i915_mutex_lock_interruptible(dev);
3821 if (ret)
3822 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003823
Eric Anholt673a3942008-07-30 12:06:12 -07003824 if (dev_priv->mm.suspended) {
Eric Anholt673a3942008-07-30 12:06:12 -07003825 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003826 ret = -EBUSY;
3827 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003828 }
3829
Keith Packardac94a962008-11-20 23:30:27 -08003830 /* Look up object handles */
Eric Anholt673a3942008-07-30 12:06:12 -07003831 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson7e318e12010-10-27 13:43:39 +01003832 struct drm_i915_gem_object *obj_priv;
3833
Chris Wilson9af90d12010-10-17 10:01:56 +01003834 object_list[i] = drm_gem_object_lookup(dev, file,
Eric Anholt673a3942008-07-30 12:06:12 -07003835 exec_list[i].handle);
3836 if (object_list[i] == NULL) {
3837 DRM_ERROR("Invalid object handle %d at index %d\n",
3838 exec_list[i].handle, i);
Chris Wilson0ce907f2010-01-23 20:26:35 +00003839 /* prevent error path from reading uninitialized data */
3840 args->buffer_count = i + 1;
Chris Wilsonbf79cb92010-08-04 14:19:46 +01003841 ret = -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07003842 goto err;
3843 }
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003844
Daniel Vetter23010e42010-03-08 13:35:02 +01003845 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003846 if (obj_priv->in_execbuffer) {
3847 DRM_ERROR("Object %p appears more than once in object list\n",
3848 object_list[i]);
Chris Wilson0ce907f2010-01-23 20:26:35 +00003849 /* prevent error path from reading uninitialized data */
3850 args->buffer_count = i + 1;
Chris Wilsonbf79cb92010-08-04 14:19:46 +01003851 ret = -EINVAL;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003852 goto err;
3853 }
3854 obj_priv->in_execbuffer = true;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003855 }
3856
Chris Wilson9af90d12010-10-17 10:01:56 +01003857 /* Move the objects en-masse into the GTT, evicting if necessary. */
3858 ret = i915_gem_execbuffer_pin(dev, file,
3859 object_list, exec_list,
3860 args->buffer_count);
3861 if (ret)
3862 goto err;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003863
Chris Wilson9af90d12010-10-17 10:01:56 +01003864 /* The objects are in their final locations, apply the relocations. */
3865 for (i = 0; i < args->buffer_count; i++) {
3866 struct drm_i915_gem_object *obj = to_intel_bo(object_list[i]);
3867 obj->base.pending_read_domains = 0;
3868 obj->base.pending_write_domain = 0;
3869 ret = i915_gem_execbuffer_relocate(obj, file, &exec_list[i]);
Eric Anholt673a3942008-07-30 12:06:12 -07003870 if (ret)
3871 goto err;
3872 }
3873
Eric Anholt673a3942008-07-30 12:06:12 -07003874 /* Set the pending read domains for the batch buffer to COMMAND */
3875 batch_obj = object_list[args->buffer_count-1];
Chris Wilson5f26a2c2009-06-06 09:45:58 +01003876 if (batch_obj->pending_write_domain) {
3877 DRM_ERROR("Attempting to use self-modifying batch buffer\n");
3878 ret = -EINVAL;
3879 goto err;
3880 }
3881 batch_obj->pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
Eric Anholt673a3942008-07-30 12:06:12 -07003882
Chris Wilson9af90d12010-10-17 10:01:56 +01003883 /* Sanity check the batch buffer */
3884 exec_offset = to_intel_bo(batch_obj)->gtt_offset;
3885 ret = i915_gem_check_execbuffer(args, exec_offset);
Chris Wilson83d60792009-06-06 09:45:57 +01003886 if (ret != 0) {
3887 DRM_ERROR("execbuf with invalid offset/length\n");
3888 goto err;
3889 }
3890
Chris Wilson13b29282010-11-01 12:22:48 +00003891 ret = i915_gem_execbuffer_move_to_gpu(dev, file, ring,
3892 object_list, args->buffer_count);
3893 if (ret)
3894 goto err;
Eric Anholt673a3942008-07-30 12:06:12 -07003895
Eric Anholt673a3942008-07-30 12:06:12 -07003896#if WATCH_COHERENCY
3897 for (i = 0; i < args->buffer_count; i++) {
3898 i915_gem_object_check_coherency(object_list[i],
3899 exec_list[i].handle);
3900 }
3901#endif
3902
Eric Anholt673a3942008-07-30 12:06:12 -07003903#if WATCH_EXEC
Ben Gamari6911a9b2009-04-02 11:24:54 -07003904 i915_gem_dump_object(batch_obj,
Eric Anholt673a3942008-07-30 12:06:12 -07003905 args->batch_len,
3906 __func__,
3907 ~0);
3908#endif
3909
Chris Wilsone59f2ba2010-10-07 17:28:15 +01003910 /* Check for any pending flips. As we only maintain a flip queue depth
3911 * of 1, we can simply insert a WAIT for the next display flip prior
3912 * to executing the batch and avoid stalling the CPU.
3913 */
3914 flips = 0;
3915 for (i = 0; i < args->buffer_count; i++) {
3916 if (object_list[i]->write_domain)
3917 flips |= atomic_read(&to_intel_bo(object_list[i])->pending_flip);
3918 }
3919 if (flips) {
3920 int plane, flip_mask;
3921
3922 for (plane = 0; flips >> plane; plane++) {
3923 if (((flips >> plane) & 1) == 0)
3924 continue;
3925
3926 if (plane)
3927 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
3928 else
3929 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
3930
Chris Wilsone1f99ce2010-10-27 12:45:26 +01003931 ret = intel_ring_begin(ring, 2);
3932 if (ret)
3933 goto err;
3934
Chris Wilson78501ea2010-10-27 12:18:21 +01003935 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
3936 intel_ring_emit(ring, MI_NOOP);
3937 intel_ring_advance(ring);
Chris Wilsone59f2ba2010-10-07 17:28:15 +01003938 }
3939 }
3940
Eric Anholt673a3942008-07-30 12:06:12 -07003941 /* Exec the batchbuffer */
Chris Wilson78501ea2010-10-27 12:18:21 +01003942 ret = ring->dispatch_execbuffer(ring, args, cliprects, exec_offset);
Eric Anholt673a3942008-07-30 12:06:12 -07003943 if (ret) {
3944 DRM_ERROR("dispatch failed %d\n", ret);
3945 goto err;
3946 }
3947
Chris Wilson7e318e12010-10-27 13:43:39 +01003948 for (i = 0; i < args->buffer_count; i++) {
3949 struct drm_gem_object *obj = object_list[i];
3950
3951 obj->read_domains = obj->pending_read_domains;
3952 obj->write_domain = obj->pending_write_domain;
3953
3954 i915_gem_object_move_to_active(obj, ring);
3955 if (obj->write_domain) {
3956 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
3957 obj_priv->dirty = 1;
3958 list_move_tail(&obj_priv->gpu_write_list,
3959 &ring->gpu_write_list);
3960 intel_mark_busy(dev, obj);
3961 }
3962
3963 trace_i915_gem_object_change_domain(obj,
3964 obj->read_domains,
3965 obj->write_domain);
3966 }
3967
Eric Anholt673a3942008-07-30 12:06:12 -07003968 /*
3969 * Ensure that the commands in the batch buffer are
3970 * finished before the interrupt fires
3971 */
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01003972 i915_retire_commands(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07003973
Chris Wilson3cce4692010-10-27 16:11:02 +01003974 if (i915_add_request(dev, file, request, ring))
3975 ring->outstanding_lazy_request = true;
3976 else
3977 request = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07003978
Eric Anholt673a3942008-07-30 12:06:12 -07003979err:
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003980 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson7e318e12010-10-27 13:43:39 +01003981 if (object_list[i] == NULL)
3982 break;
3983
3984 to_intel_bo(object_list[i])->in_execbuffer = false;
Julia Lawallaad87df2008-12-21 16:28:47 +01003985 drm_gem_object_unreference(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003986 }
Julia Lawallaad87df2008-12-21 16:28:47 +01003987
Eric Anholt673a3942008-07-30 12:06:12 -07003988 mutex_unlock(&dev->struct_mutex);
3989
Chris Wilson93533c22010-01-31 10:40:48 +00003990pre_mutex_err:
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003991 drm_free_large(object_list);
Eric Anholt9a298b22009-03-24 12:23:04 -07003992 kfree(cliprects);
Chris Wilson8dc5d142010-08-12 12:36:12 +01003993 kfree(request);
Eric Anholt673a3942008-07-30 12:06:12 -07003994
3995 return ret;
3996}
3997
Jesse Barnes76446ca2009-12-17 22:05:42 -05003998/*
3999 * Legacy execbuffer just creates an exec2 list from the original exec object
4000 * list array and passes it to the real function.
4001 */
4002int
4003i915_gem_execbuffer(struct drm_device *dev, void *data,
4004 struct drm_file *file_priv)
4005{
4006 struct drm_i915_gem_execbuffer *args = data;
4007 struct drm_i915_gem_execbuffer2 exec2;
4008 struct drm_i915_gem_exec_object *exec_list = NULL;
4009 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
4010 int ret, i;
4011
4012#if WATCH_EXEC
4013 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
4014 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
4015#endif
4016
4017 if (args->buffer_count < 1) {
4018 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
4019 return -EINVAL;
4020 }
4021
4022 /* Copy in the exec list from userland */
4023 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
4024 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
4025 if (exec_list == NULL || exec2_list == NULL) {
4026 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
4027 args->buffer_count);
4028 drm_free_large(exec_list);
4029 drm_free_large(exec2_list);
4030 return -ENOMEM;
4031 }
4032 ret = copy_from_user(exec_list,
4033 (struct drm_i915_relocation_entry __user *)
4034 (uintptr_t) args->buffers_ptr,
4035 sizeof(*exec_list) * args->buffer_count);
4036 if (ret != 0) {
4037 DRM_ERROR("copy %d exec entries failed %d\n",
4038 args->buffer_count, ret);
4039 drm_free_large(exec_list);
4040 drm_free_large(exec2_list);
4041 return -EFAULT;
4042 }
4043
4044 for (i = 0; i < args->buffer_count; i++) {
4045 exec2_list[i].handle = exec_list[i].handle;
4046 exec2_list[i].relocation_count = exec_list[i].relocation_count;
4047 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
4048 exec2_list[i].alignment = exec_list[i].alignment;
4049 exec2_list[i].offset = exec_list[i].offset;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004050 if (INTEL_INFO(dev)->gen < 4)
Jesse Barnes76446ca2009-12-17 22:05:42 -05004051 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
4052 else
4053 exec2_list[i].flags = 0;
4054 }
4055
4056 exec2.buffers_ptr = args->buffers_ptr;
4057 exec2.buffer_count = args->buffer_count;
4058 exec2.batch_start_offset = args->batch_start_offset;
4059 exec2.batch_len = args->batch_len;
4060 exec2.DR1 = args->DR1;
4061 exec2.DR4 = args->DR4;
4062 exec2.num_cliprects = args->num_cliprects;
4063 exec2.cliprects_ptr = args->cliprects_ptr;
Zou Nan hai852835f2010-05-21 09:08:56 +08004064 exec2.flags = I915_EXEC_RENDER;
Jesse Barnes76446ca2009-12-17 22:05:42 -05004065
4066 ret = i915_gem_do_execbuffer(dev, data, file_priv, &exec2, exec2_list);
4067 if (!ret) {
4068 /* Copy the new buffer offsets back to the user's exec list. */
4069 for (i = 0; i < args->buffer_count; i++)
4070 exec_list[i].offset = exec2_list[i].offset;
4071 /* ... and back out to userspace */
4072 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
4073 (uintptr_t) args->buffers_ptr,
4074 exec_list,
4075 sizeof(*exec_list) * args->buffer_count);
4076 if (ret) {
4077 ret = -EFAULT;
4078 DRM_ERROR("failed to copy %d exec entries "
4079 "back to user (%d)\n",
4080 args->buffer_count, ret);
4081 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05004082 }
4083
4084 drm_free_large(exec_list);
4085 drm_free_large(exec2_list);
4086 return ret;
4087}
4088
4089int
4090i915_gem_execbuffer2(struct drm_device *dev, void *data,
4091 struct drm_file *file_priv)
4092{
4093 struct drm_i915_gem_execbuffer2 *args = data;
4094 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
4095 int ret;
4096
4097#if WATCH_EXEC
4098 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
4099 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
4100#endif
4101
4102 if (args->buffer_count < 1) {
4103 DRM_ERROR("execbuf2 with %d buffers\n", args->buffer_count);
4104 return -EINVAL;
4105 }
4106
4107 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
4108 if (exec2_list == NULL) {
4109 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
4110 args->buffer_count);
4111 return -ENOMEM;
4112 }
4113 ret = copy_from_user(exec2_list,
4114 (struct drm_i915_relocation_entry __user *)
4115 (uintptr_t) args->buffers_ptr,
4116 sizeof(*exec2_list) * args->buffer_count);
4117 if (ret != 0) {
4118 DRM_ERROR("copy %d exec entries failed %d\n",
4119 args->buffer_count, ret);
4120 drm_free_large(exec2_list);
4121 return -EFAULT;
4122 }
4123
4124 ret = i915_gem_do_execbuffer(dev, data, file_priv, args, exec2_list);
4125 if (!ret) {
4126 /* Copy the new buffer offsets back to the user's exec list. */
4127 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
4128 (uintptr_t) args->buffers_ptr,
4129 exec2_list,
4130 sizeof(*exec2_list) * args->buffer_count);
4131 if (ret) {
4132 ret = -EFAULT;
4133 DRM_ERROR("failed to copy %d exec entries "
4134 "back to user (%d)\n",
4135 args->buffer_count, ret);
4136 }
4137 }
4138
4139 drm_free_large(exec2_list);
4140 return ret;
4141}
4142
Eric Anholt673a3942008-07-30 12:06:12 -07004143int
Daniel Vetter920afa72010-09-16 17:54:23 +02004144i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01004145 bool map_and_fenceable)
Eric Anholt673a3942008-07-30 12:06:12 -07004146{
4147 struct drm_device *dev = obj->dev;
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004148 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01004149 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004150 int ret;
4151
Daniel Vetter778c3542010-05-13 11:49:44 +02004152 BUG_ON(obj_priv->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT);
Daniel Vetter75e9e912010-11-04 17:11:09 +01004153 BUG_ON(map_and_fenceable && !map_and_fenceable);
Chris Wilson23bc5982010-09-29 16:10:57 +01004154 WARN_ON(i915_verify_lists(dev));
Chris Wilsonac0c6b52010-05-27 13:18:18 +01004155
4156 if (obj_priv->gtt_space != NULL) {
Chris Wilsona00b10c2010-09-24 21:15:47 +01004157 if ((alignment && obj_priv->gtt_offset & (alignment - 1)) ||
Daniel Vetter75e9e912010-11-04 17:11:09 +01004158 (map_and_fenceable && !obj_priv->map_and_fenceable)) {
Chris Wilsonae7d49d2010-08-04 12:37:41 +01004159 WARN(obj_priv->pin_count,
4160 "bo is already pinned with incorrect alignment:"
Daniel Vetter75e9e912010-11-04 17:11:09 +01004161 " offset=%x, req.alignment=%x, req.map_and_fenceable=%d,"
4162 " obj->map_and_fenceable=%d\n",
Chris Wilsona00b10c2010-09-24 21:15:47 +01004163 obj_priv->gtt_offset, alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01004164 map_and_fenceable,
4165 obj_priv->map_and_fenceable);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01004166 ret = i915_gem_object_unbind(obj);
4167 if (ret)
4168 return ret;
4169 }
4170 }
4171
Eric Anholt673a3942008-07-30 12:06:12 -07004172 if (obj_priv->gtt_space == NULL) {
Chris Wilsona00b10c2010-09-24 21:15:47 +01004173 ret = i915_gem_object_bind_to_gtt(obj, alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01004174 map_and_fenceable);
Chris Wilson97311292009-09-21 00:22:34 +01004175 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07004176 return ret;
Chris Wilson22c344e2009-02-11 14:26:45 +00004177 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05004178
Chris Wilson74653782010-10-29 10:41:23 +01004179 if (obj_priv->pin_count++ == 0) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01004180 i915_gem_info_add_pin(dev_priv, obj_priv, map_and_fenceable);
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004181 if (!obj_priv->active)
Chris Wilson69dc4982010-10-19 10:36:51 +01004182 list_move_tail(&obj_priv->mm_list,
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004183 &dev_priv->mm.pinned_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004184 }
Daniel Vetter75e9e912010-11-04 17:11:09 +01004185 BUG_ON(!obj_priv->pin_mappable && map_and_fenceable);
Eric Anholt673a3942008-07-30 12:06:12 -07004186
Chris Wilson23bc5982010-09-29 16:10:57 +01004187 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07004188 return 0;
4189}
4190
4191void
4192i915_gem_object_unpin(struct drm_gem_object *obj)
4193{
4194 struct drm_device *dev = obj->dev;
4195 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01004196 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004197
Chris Wilson23bc5982010-09-29 16:10:57 +01004198 WARN_ON(i915_verify_lists(dev));
Chris Wilson74653782010-10-29 10:41:23 +01004199 BUG_ON(obj_priv->pin_count == 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004200 BUG_ON(obj_priv->gtt_space == NULL);
4201
Chris Wilson74653782010-10-29 10:41:23 +01004202 if (--obj_priv->pin_count == 0) {
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004203 if (!obj_priv->active)
Chris Wilson69dc4982010-10-19 10:36:51 +01004204 list_move_tail(&obj_priv->mm_list,
Eric Anholt673a3942008-07-30 12:06:12 -07004205 &dev_priv->mm.inactive_list);
Chris Wilsona00b10c2010-09-24 21:15:47 +01004206 i915_gem_info_remove_pin(dev_priv, obj_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07004207 }
Chris Wilson23bc5982010-09-29 16:10:57 +01004208 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07004209}
4210
4211int
4212i915_gem_pin_ioctl(struct drm_device *dev, void *data,
4213 struct drm_file *file_priv)
4214{
4215 struct drm_i915_gem_pin *args = data;
4216 struct drm_gem_object *obj;
4217 struct drm_i915_gem_object *obj_priv;
4218 int ret;
4219
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004220 ret = i915_mutex_lock_interruptible(dev);
4221 if (ret)
4222 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004223
4224 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4225 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004226 ret = -ENOENT;
4227 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07004228 }
Daniel Vetter23010e42010-03-08 13:35:02 +01004229 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004230
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004231 if (obj_priv->madv != I915_MADV_WILLNEED) {
4232 DRM_ERROR("Attempting to pin a purgeable buffer\n");
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004233 ret = -EINVAL;
4234 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004235 }
4236
Jesse Barnes79e53942008-11-07 14:24:08 -08004237 if (obj_priv->pin_filp != NULL && obj_priv->pin_filp != file_priv) {
4238 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
4239 args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004240 ret = -EINVAL;
4241 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08004242 }
4243
4244 obj_priv->user_pin_count++;
4245 obj_priv->pin_filp = file_priv;
4246 if (obj_priv->user_pin_count == 1) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01004247 ret = i915_gem_object_pin(obj, args->alignment, true);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004248 if (ret)
4249 goto out;
Eric Anholt673a3942008-07-30 12:06:12 -07004250 }
4251
4252 /* XXX - flush the CPU caches for pinned objects
4253 * as the X server doesn't manage domains yet
4254 */
Eric Anholte47c68e2008-11-14 13:35:19 -08004255 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004256 args->offset = obj_priv->gtt_offset;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004257out:
Eric Anholt673a3942008-07-30 12:06:12 -07004258 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004259unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07004260 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004261 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004262}
4263
4264int
4265i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
4266 struct drm_file *file_priv)
4267{
4268 struct drm_i915_gem_pin *args = data;
4269 struct drm_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08004270 struct drm_i915_gem_object *obj_priv;
Chris Wilson76c1dec2010-09-25 11:22:51 +01004271 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004272
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004273 ret = i915_mutex_lock_interruptible(dev);
4274 if (ret)
4275 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004276
4277 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4278 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004279 ret = -ENOENT;
4280 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07004281 }
Daniel Vetter23010e42010-03-08 13:35:02 +01004282 obj_priv = to_intel_bo(obj);
Chris Wilson76c1dec2010-09-25 11:22:51 +01004283
Jesse Barnes79e53942008-11-07 14:24:08 -08004284 if (obj_priv->pin_filp != file_priv) {
4285 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
4286 args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004287 ret = -EINVAL;
4288 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08004289 }
4290 obj_priv->user_pin_count--;
4291 if (obj_priv->user_pin_count == 0) {
4292 obj_priv->pin_filp = NULL;
4293 i915_gem_object_unpin(obj);
4294 }
Eric Anholt673a3942008-07-30 12:06:12 -07004295
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004296out:
Eric Anholt673a3942008-07-30 12:06:12 -07004297 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004298unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07004299 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004300 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004301}
4302
4303int
4304i915_gem_busy_ioctl(struct drm_device *dev, void *data,
4305 struct drm_file *file_priv)
4306{
4307 struct drm_i915_gem_busy *args = data;
4308 struct drm_gem_object *obj;
4309 struct drm_i915_gem_object *obj_priv;
Chris Wilson30dbf0c2010-09-25 10:19:17 +01004310 int ret;
4311
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004312 ret = i915_mutex_lock_interruptible(dev);
4313 if (ret)
4314 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004315
Eric Anholt673a3942008-07-30 12:06:12 -07004316 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4317 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004318 ret = -ENOENT;
4319 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07004320 }
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004321 obj_priv = to_intel_bo(obj);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004322
Chris Wilson0be555b2010-08-04 15:36:30 +01004323 /* Count all active objects as busy, even if they are currently not used
4324 * by the gpu. Users of this interface expect objects to eventually
4325 * become non-busy without any further actions, therefore emit any
4326 * necessary flushes here.
Eric Anholtc4de0a52008-12-14 19:05:04 -08004327 */
Chris Wilson0be555b2010-08-04 15:36:30 +01004328 args->busy = obj_priv->active;
4329 if (args->busy) {
4330 /* Unconditionally flush objects, even when the gpu still uses this
4331 * object. Userspace calling this function indicates that it wants to
4332 * use this buffer rather sooner than later, so issuing the required
4333 * flush earlier is beneficial.
4334 */
Chris Wilsonc78ec302010-09-20 12:50:23 +01004335 if (obj->write_domain & I915_GEM_GPU_DOMAINS)
4336 i915_gem_flush_ring(dev, file_priv,
Chris Wilson92204342010-09-18 11:02:01 +01004337 obj_priv->ring,
4338 0, obj->write_domain);
Chris Wilson0be555b2010-08-04 15:36:30 +01004339
4340 /* Update the active list for the hardware's current position.
4341 * Otherwise this only updates on a delayed timer or when irqs
4342 * are actually unmasked, and our working set ends up being
4343 * larger than required.
4344 */
4345 i915_gem_retire_requests_ring(dev, obj_priv->ring);
4346
4347 args->busy = obj_priv->active;
4348 }
Eric Anholt673a3942008-07-30 12:06:12 -07004349
4350 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004351unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07004352 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004353 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004354}
4355
4356int
4357i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4358 struct drm_file *file_priv)
4359{
4360 return i915_gem_ring_throttle(dev, file_priv);
4361}
4362
Chris Wilson3ef94da2009-09-14 16:50:29 +01004363int
4364i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4365 struct drm_file *file_priv)
4366{
4367 struct drm_i915_gem_madvise *args = data;
4368 struct drm_gem_object *obj;
4369 struct drm_i915_gem_object *obj_priv;
Chris Wilson76c1dec2010-09-25 11:22:51 +01004370 int ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004371
4372 switch (args->madv) {
4373 case I915_MADV_DONTNEED:
4374 case I915_MADV_WILLNEED:
4375 break;
4376 default:
4377 return -EINVAL;
4378 }
4379
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004380 ret = i915_mutex_lock_interruptible(dev);
4381 if (ret)
4382 return ret;
4383
Chris Wilson3ef94da2009-09-14 16:50:29 +01004384 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4385 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004386 ret = -ENOENT;
4387 goto unlock;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004388 }
Daniel Vetter23010e42010-03-08 13:35:02 +01004389 obj_priv = to_intel_bo(obj);
Chris Wilson3ef94da2009-09-14 16:50:29 +01004390
4391 if (obj_priv->pin_count) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004392 ret = -EINVAL;
4393 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004394 }
4395
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004396 if (obj_priv->madv != __I915_MADV_PURGED)
4397 obj_priv->madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004398
Chris Wilson2d7ef392009-09-20 23:13:10 +01004399 /* if the object is no longer bound, discard its backing storage */
4400 if (i915_gem_object_is_purgeable(obj_priv) &&
4401 obj_priv->gtt_space == NULL)
4402 i915_gem_object_truncate(obj);
4403
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004404 args->retained = obj_priv->madv != __I915_MADV_PURGED;
4405
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004406out:
Chris Wilson3ef94da2009-09-14 16:50:29 +01004407 drm_gem_object_unreference(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004408unlock:
Chris Wilson3ef94da2009-09-14 16:50:29 +01004409 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004410 return ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004411}
4412
Daniel Vetterac52bc52010-04-09 19:05:06 +00004413struct drm_gem_object * i915_gem_alloc_object(struct drm_device *dev,
4414 size_t size)
4415{
Chris Wilson73aa8082010-09-30 11:46:12 +01004416 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc397b902010-04-09 19:05:07 +00004417 struct drm_i915_gem_object *obj;
4418
4419 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
4420 if (obj == NULL)
4421 return NULL;
4422
4423 if (drm_gem_object_init(dev, &obj->base, size) != 0) {
4424 kfree(obj);
4425 return NULL;
4426 }
4427
Chris Wilson73aa8082010-09-30 11:46:12 +01004428 i915_gem_info_add_obj(dev_priv, size);
4429
Daniel Vetterc397b902010-04-09 19:05:07 +00004430 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4431 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4432
4433 obj->agp_type = AGP_USER_MEMORY;
Daniel Vetter62b8b212010-04-09 19:05:08 +00004434 obj->base.driver_private = NULL;
Daniel Vetterc397b902010-04-09 19:05:07 +00004435 obj->fence_reg = I915_FENCE_REG_NONE;
Chris Wilson69dc4982010-10-19 10:36:51 +01004436 INIT_LIST_HEAD(&obj->mm_list);
4437 INIT_LIST_HEAD(&obj->ring_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00004438 INIT_LIST_HEAD(&obj->gpu_write_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00004439 obj->madv = I915_MADV_WILLNEED;
Daniel Vetter75e9e912010-11-04 17:11:09 +01004440 /* Avoid an unnecessary call to unbind on the first bind. */
4441 obj->map_and_fenceable = true;
Daniel Vetterc397b902010-04-09 19:05:07 +00004442
Daniel Vetterc397b902010-04-09 19:05:07 +00004443 return &obj->base;
Daniel Vetterac52bc52010-04-09 19:05:06 +00004444}
4445
Eric Anholt673a3942008-07-30 12:06:12 -07004446int i915_gem_init_object(struct drm_gem_object *obj)
4447{
Daniel Vetterc397b902010-04-09 19:05:07 +00004448 BUG();
Jesse Barnesde151cf2008-11-12 10:03:55 -08004449
Eric Anholt673a3942008-07-30 12:06:12 -07004450 return 0;
4451}
4452
Chris Wilsonbe726152010-07-23 23:18:50 +01004453static void i915_gem_free_object_tail(struct drm_gem_object *obj)
4454{
4455 struct drm_device *dev = obj->dev;
4456 drm_i915_private_t *dev_priv = dev->dev_private;
4457 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
4458 int ret;
4459
4460 ret = i915_gem_object_unbind(obj);
4461 if (ret == -ERESTARTSYS) {
Chris Wilson69dc4982010-10-19 10:36:51 +01004462 list_move(&obj_priv->mm_list,
Chris Wilsonbe726152010-07-23 23:18:50 +01004463 &dev_priv->mm.deferred_free_list);
4464 return;
4465 }
4466
Chris Wilson39a01d12010-10-28 13:03:06 +01004467 if (obj->map_list.map)
Chris Wilsonbe726152010-07-23 23:18:50 +01004468 i915_gem_free_mmap_offset(obj);
4469
4470 drm_gem_object_release(obj);
Chris Wilson73aa8082010-09-30 11:46:12 +01004471 i915_gem_info_remove_obj(dev_priv, obj->size);
Chris Wilsonbe726152010-07-23 23:18:50 +01004472
4473 kfree(obj_priv->page_cpu_valid);
4474 kfree(obj_priv->bit_17);
4475 kfree(obj_priv);
4476}
4477
Eric Anholt673a3942008-07-30 12:06:12 -07004478void i915_gem_free_object(struct drm_gem_object *obj)
4479{
Jesse Barnesde151cf2008-11-12 10:03:55 -08004480 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01004481 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004482
Chris Wilson1c5d22f2009-08-25 11:15:50 +01004483 trace_i915_gem_object_destroy(obj);
4484
Eric Anholt673a3942008-07-30 12:06:12 -07004485 while (obj_priv->pin_count > 0)
4486 i915_gem_object_unpin(obj);
4487
Dave Airlie71acb5e2008-12-30 20:31:46 +10004488 if (obj_priv->phys_obj)
4489 i915_gem_detach_phys_object(dev, obj);
4490
Chris Wilsonbe726152010-07-23 23:18:50 +01004491 i915_gem_free_object_tail(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004492}
4493
Jesse Barnes5669fca2009-02-17 15:13:31 -08004494int
Eric Anholt673a3942008-07-30 12:06:12 -07004495i915_gem_idle(struct drm_device *dev)
4496{
4497 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson29105cc2010-01-07 10:39:13 +00004498 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004499
Keith Packard6dbe2772008-10-14 21:41:13 -07004500 mutex_lock(&dev->struct_mutex);
4501
Chris Wilson87acb0a2010-10-19 10:13:00 +01004502 if (dev_priv->mm.suspended) {
Keith Packard6dbe2772008-10-14 21:41:13 -07004503 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004504 return 0;
Keith Packard6dbe2772008-10-14 21:41:13 -07004505 }
Eric Anholt673a3942008-07-30 12:06:12 -07004506
Chris Wilson29105cc2010-01-07 10:39:13 +00004507 ret = i915_gpu_idle(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004508 if (ret) {
4509 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004510 return ret;
Keith Packard6dbe2772008-10-14 21:41:13 -07004511 }
Eric Anholt673a3942008-07-30 12:06:12 -07004512
Chris Wilson29105cc2010-01-07 10:39:13 +00004513 /* Under UMS, be paranoid and evict. */
4514 if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
Chris Wilson5eac3ab2010-10-31 08:49:47 +00004515 ret = i915_gem_evict_inactive(dev, false);
Chris Wilson29105cc2010-01-07 10:39:13 +00004516 if (ret) {
4517 mutex_unlock(&dev->struct_mutex);
4518 return ret;
4519 }
4520 }
4521
4522 /* Hack! Don't let anybody do execbuf while we don't control the chip.
4523 * We need to replace this with a semaphore, or something.
4524 * And not confound mm.suspended!
4525 */
4526 dev_priv->mm.suspended = 1;
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02004527 del_timer_sync(&dev_priv->hangcheck_timer);
Chris Wilson29105cc2010-01-07 10:39:13 +00004528
4529 i915_kernel_lost_context(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004530 i915_gem_cleanup_ringbuffer(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00004531
Keith Packard6dbe2772008-10-14 21:41:13 -07004532 mutex_unlock(&dev->struct_mutex);
4533
Chris Wilson29105cc2010-01-07 10:39:13 +00004534 /* Cancel the retire work handler, which should be idle now. */
4535 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
4536
Eric Anholt673a3942008-07-30 12:06:12 -07004537 return 0;
4538}
4539
Jesse Barnese552eb72010-04-21 11:39:23 -07004540/*
4541 * 965+ support PIPE_CONTROL commands, which provide finer grained control
4542 * over cache flushing.
4543 */
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004544static int
Jesse Barnese552eb72010-04-21 11:39:23 -07004545i915_gem_init_pipe_control(struct drm_device *dev)
4546{
4547 drm_i915_private_t *dev_priv = dev->dev_private;
4548 struct drm_gem_object *obj;
4549 struct drm_i915_gem_object *obj_priv;
4550 int ret;
4551
Eric Anholt34dc4d42010-05-07 14:30:03 -07004552 obj = i915_gem_alloc_object(dev, 4096);
Jesse Barnese552eb72010-04-21 11:39:23 -07004553 if (obj == NULL) {
4554 DRM_ERROR("Failed to allocate seqno page\n");
4555 ret = -ENOMEM;
4556 goto err;
4557 }
4558 obj_priv = to_intel_bo(obj);
4559 obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
4560
Daniel Vetter75e9e912010-11-04 17:11:09 +01004561 ret = i915_gem_object_pin(obj, 4096, true);
Jesse Barnese552eb72010-04-21 11:39:23 -07004562 if (ret)
4563 goto err_unref;
4564
4565 dev_priv->seqno_gfx_addr = obj_priv->gtt_offset;
4566 dev_priv->seqno_page = kmap(obj_priv->pages[0]);
4567 if (dev_priv->seqno_page == NULL)
4568 goto err_unpin;
4569
4570 dev_priv->seqno_obj = obj;
4571 memset(dev_priv->seqno_page, 0, PAGE_SIZE);
4572
4573 return 0;
4574
4575err_unpin:
4576 i915_gem_object_unpin(obj);
4577err_unref:
4578 drm_gem_object_unreference(obj);
4579err:
4580 return ret;
4581}
4582
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004583
4584static void
Jesse Barnese552eb72010-04-21 11:39:23 -07004585i915_gem_cleanup_pipe_control(struct drm_device *dev)
4586{
4587 drm_i915_private_t *dev_priv = dev->dev_private;
4588 struct drm_gem_object *obj;
4589 struct drm_i915_gem_object *obj_priv;
4590
4591 obj = dev_priv->seqno_obj;
4592 obj_priv = to_intel_bo(obj);
4593 kunmap(obj_priv->pages[0]);
4594 i915_gem_object_unpin(obj);
4595 drm_gem_object_unreference(obj);
4596 dev_priv->seqno_obj = NULL;
4597
4598 dev_priv->seqno_page = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07004599}
4600
Eric Anholt673a3942008-07-30 12:06:12 -07004601int
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004602i915_gem_init_ringbuffer(struct drm_device *dev)
4603{
4604 drm_i915_private_t *dev_priv = dev->dev_private;
4605 int ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01004606
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004607 if (HAS_PIPE_CONTROL(dev)) {
4608 ret = i915_gem_init_pipe_control(dev);
4609 if (ret)
4610 return ret;
4611 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004612
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08004613 ret = intel_init_render_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004614 if (ret)
4615 goto cleanup_pipe_control;
4616
4617 if (HAS_BSD(dev)) {
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08004618 ret = intel_init_bsd_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004619 if (ret)
4620 goto cleanup_render_ring;
Zou Nan haid1b851f2010-05-21 09:08:57 +08004621 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004622
Chris Wilson549f7362010-10-19 11:19:32 +01004623 if (HAS_BLT(dev)) {
4624 ret = intel_init_blt_ring_buffer(dev);
4625 if (ret)
4626 goto cleanup_bsd_ring;
4627 }
4628
Chris Wilson6f392d52010-08-07 11:01:22 +01004629 dev_priv->next_seqno = 1;
4630
Chris Wilson68f95ba2010-05-27 13:18:22 +01004631 return 0;
4632
Chris Wilson549f7362010-10-19 11:19:32 +01004633cleanup_bsd_ring:
Chris Wilson78501ea2010-10-27 12:18:21 +01004634 intel_cleanup_ring_buffer(&dev_priv->bsd_ring);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004635cleanup_render_ring:
Chris Wilson78501ea2010-10-27 12:18:21 +01004636 intel_cleanup_ring_buffer(&dev_priv->render_ring);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004637cleanup_pipe_control:
4638 if (HAS_PIPE_CONTROL(dev))
4639 i915_gem_cleanup_pipe_control(dev);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004640 return ret;
4641}
4642
4643void
4644i915_gem_cleanup_ringbuffer(struct drm_device *dev)
4645{
4646 drm_i915_private_t *dev_priv = dev->dev_private;
4647
Chris Wilson78501ea2010-10-27 12:18:21 +01004648 intel_cleanup_ring_buffer(&dev_priv->render_ring);
4649 intel_cleanup_ring_buffer(&dev_priv->bsd_ring);
4650 intel_cleanup_ring_buffer(&dev_priv->blt_ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004651 if (HAS_PIPE_CONTROL(dev))
4652 i915_gem_cleanup_pipe_control(dev);
4653}
4654
4655int
Eric Anholt673a3942008-07-30 12:06:12 -07004656i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
4657 struct drm_file *file_priv)
4658{
4659 drm_i915_private_t *dev_priv = dev->dev_private;
4660 int ret;
4661
Jesse Barnes79e53942008-11-07 14:24:08 -08004662 if (drm_core_check_feature(dev, DRIVER_MODESET))
4663 return 0;
4664
Ben Gamariba1234d2009-09-14 17:48:47 -04004665 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07004666 DRM_ERROR("Reenabling wedged hardware, good luck\n");
Ben Gamariba1234d2009-09-14 17:48:47 -04004667 atomic_set(&dev_priv->mm.wedged, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004668 }
4669
Eric Anholt673a3942008-07-30 12:06:12 -07004670 mutex_lock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004671 dev_priv->mm.suspended = 0;
4672
4673 ret = i915_gem_init_ringbuffer(dev);
Wu Fengguangd816f6ac2009-04-18 10:43:32 +08004674 if (ret != 0) {
4675 mutex_unlock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004676 return ret;
Wu Fengguangd816f6ac2009-04-18 10:43:32 +08004677 }
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004678
Chris Wilson69dc4982010-10-19 10:36:51 +01004679 BUG_ON(!list_empty(&dev_priv->mm.active_list));
Zou Nan hai852835f2010-05-21 09:08:56 +08004680 BUG_ON(!list_empty(&dev_priv->render_ring.active_list));
Chris Wilson87acb0a2010-10-19 10:13:00 +01004681 BUG_ON(!list_empty(&dev_priv->bsd_ring.active_list));
Chris Wilson549f7362010-10-19 11:19:32 +01004682 BUG_ON(!list_empty(&dev_priv->blt_ring.active_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004683 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
4684 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
Zou Nan hai852835f2010-05-21 09:08:56 +08004685 BUG_ON(!list_empty(&dev_priv->render_ring.request_list));
Chris Wilson87acb0a2010-10-19 10:13:00 +01004686 BUG_ON(!list_empty(&dev_priv->bsd_ring.request_list));
Chris Wilson549f7362010-10-19 11:19:32 +01004687 BUG_ON(!list_empty(&dev_priv->blt_ring.request_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004688 mutex_unlock(&dev->struct_mutex);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004689
Chris Wilson5f353082010-06-07 14:03:03 +01004690 ret = drm_irq_install(dev);
4691 if (ret)
4692 goto cleanup_ringbuffer;
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004693
Eric Anholt673a3942008-07-30 12:06:12 -07004694 return 0;
Chris Wilson5f353082010-06-07 14:03:03 +01004695
4696cleanup_ringbuffer:
4697 mutex_lock(&dev->struct_mutex);
4698 i915_gem_cleanup_ringbuffer(dev);
4699 dev_priv->mm.suspended = 1;
4700 mutex_unlock(&dev->struct_mutex);
4701
4702 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004703}
4704
4705int
4706i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
4707 struct drm_file *file_priv)
4708{
Jesse Barnes79e53942008-11-07 14:24:08 -08004709 if (drm_core_check_feature(dev, DRIVER_MODESET))
4710 return 0;
4711
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004712 drm_irq_uninstall(dev);
Linus Torvaldse6890f62009-09-08 17:09:24 -07004713 return i915_gem_idle(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004714}
4715
4716void
4717i915_gem_lastclose(struct drm_device *dev)
4718{
4719 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004720
Eric Anholte806b492009-01-22 09:56:58 -08004721 if (drm_core_check_feature(dev, DRIVER_MODESET))
4722 return;
4723
Keith Packard6dbe2772008-10-14 21:41:13 -07004724 ret = i915_gem_idle(dev);
4725 if (ret)
4726 DRM_ERROR("failed to idle hardware: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07004727}
4728
Chris Wilson64193402010-10-24 12:38:05 +01004729static void
4730init_ring_lists(struct intel_ring_buffer *ring)
4731{
4732 INIT_LIST_HEAD(&ring->active_list);
4733 INIT_LIST_HEAD(&ring->request_list);
4734 INIT_LIST_HEAD(&ring->gpu_write_list);
4735}
4736
Eric Anholt673a3942008-07-30 12:06:12 -07004737void
4738i915_gem_load(struct drm_device *dev)
4739{
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004740 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07004741 drm_i915_private_t *dev_priv = dev->dev_private;
4742
Chris Wilson69dc4982010-10-19 10:36:51 +01004743 INIT_LIST_HEAD(&dev_priv->mm.active_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004744 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
4745 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004746 INIT_LIST_HEAD(&dev_priv->mm.pinned_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07004747 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Chris Wilsonbe726152010-07-23 23:18:50 +01004748 INIT_LIST_HEAD(&dev_priv->mm.deferred_free_list);
Chris Wilson64193402010-10-24 12:38:05 +01004749 init_ring_lists(&dev_priv->render_ring);
4750 init_ring_lists(&dev_priv->bsd_ring);
4751 init_ring_lists(&dev_priv->blt_ring);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02004752 for (i = 0; i < 16; i++)
4753 INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004754 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
4755 i915_gem_retire_work_handler);
Chris Wilson30dbf0c2010-09-25 10:19:17 +01004756 init_completion(&dev_priv->error_completion);
Chris Wilson31169712009-09-14 16:50:28 +01004757
Dave Airlie94400122010-07-20 13:15:31 +10004758 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
4759 if (IS_GEN3(dev)) {
4760 u32 tmp = I915_READ(MI_ARB_STATE);
4761 if (!(tmp & MI_ARB_C3_LP_WRITE_ENABLE)) {
4762 /* arb state is a masked write, so set bit + bit in mask */
4763 tmp = MI_ARB_C3_LP_WRITE_ENABLE | (MI_ARB_C3_LP_WRITE_ENABLE << MI_ARB_MASK_SHIFT);
4764 I915_WRITE(MI_ARB_STATE, tmp);
4765 }
4766 }
4767
Jesse Barnesde151cf2008-11-12 10:03:55 -08004768 /* Old X drivers will take 0-2 for front, back, depth buffers */
Eric Anholtb397c832010-01-26 09:43:10 -08004769 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4770 dev_priv->fence_reg_start = 3;
Jesse Barnesde151cf2008-11-12 10:03:55 -08004771
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004772 if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08004773 dev_priv->num_fence_regs = 16;
4774 else
4775 dev_priv->num_fence_regs = 8;
4776
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004777 /* Initialize fence registers to zero */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004778 switch (INTEL_INFO(dev)->gen) {
4779 case 6:
4780 for (i = 0; i < 16; i++)
4781 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (i * 8), 0);
4782 break;
4783 case 5:
4784 case 4:
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004785 for (i = 0; i < 16; i++)
4786 I915_WRITE64(FENCE_REG_965_0 + (i * 8), 0);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004787 break;
4788 case 3:
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004789 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4790 for (i = 0; i < 8; i++)
4791 I915_WRITE(FENCE_REG_945_8 + (i * 4), 0);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004792 case 2:
4793 for (i = 0; i < 8; i++)
4794 I915_WRITE(FENCE_REG_830_0 + (i * 4), 0);
4795 break;
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004796 }
Eric Anholt673a3942008-07-30 12:06:12 -07004797 i915_gem_detect_bit_6_swizzle(dev);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05004798 init_waitqueue_head(&dev_priv->pending_flip_queue);
Chris Wilson17250b72010-10-28 12:51:39 +01004799
4800 dev_priv->mm.inactive_shrinker.shrink = i915_gem_inactive_shrink;
4801 dev_priv->mm.inactive_shrinker.seeks = DEFAULT_SEEKS;
4802 register_shrinker(&dev_priv->mm.inactive_shrinker);
Eric Anholt673a3942008-07-30 12:06:12 -07004803}
Dave Airlie71acb5e2008-12-30 20:31:46 +10004804
4805/*
4806 * Create a physically contiguous memory object for this object
4807 * e.g. for cursor + overlay regs
4808 */
Chris Wilson995b67622010-08-20 13:23:26 +01004809static int i915_gem_init_phys_object(struct drm_device *dev,
4810 int id, int size, int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004811{
4812 drm_i915_private_t *dev_priv = dev->dev_private;
4813 struct drm_i915_gem_phys_object *phys_obj;
4814 int ret;
4815
4816 if (dev_priv->mm.phys_objs[id - 1] || !size)
4817 return 0;
4818
Eric Anholt9a298b22009-03-24 12:23:04 -07004819 phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004820 if (!phys_obj)
4821 return -ENOMEM;
4822
4823 phys_obj->id = id;
4824
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004825 phys_obj->handle = drm_pci_alloc(dev, size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004826 if (!phys_obj->handle) {
4827 ret = -ENOMEM;
4828 goto kfree_obj;
4829 }
4830#ifdef CONFIG_X86
4831 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4832#endif
4833
4834 dev_priv->mm.phys_objs[id - 1] = phys_obj;
4835
4836 return 0;
4837kfree_obj:
Eric Anholt9a298b22009-03-24 12:23:04 -07004838 kfree(phys_obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004839 return ret;
4840}
4841
Chris Wilson995b67622010-08-20 13:23:26 +01004842static void i915_gem_free_phys_object(struct drm_device *dev, int id)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004843{
4844 drm_i915_private_t *dev_priv = dev->dev_private;
4845 struct drm_i915_gem_phys_object *phys_obj;
4846
4847 if (!dev_priv->mm.phys_objs[id - 1])
4848 return;
4849
4850 phys_obj = dev_priv->mm.phys_objs[id - 1];
4851 if (phys_obj->cur_obj) {
4852 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
4853 }
4854
4855#ifdef CONFIG_X86
4856 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4857#endif
4858 drm_pci_free(dev, phys_obj->handle);
4859 kfree(phys_obj);
4860 dev_priv->mm.phys_objs[id - 1] = NULL;
4861}
4862
4863void i915_gem_free_all_phys_object(struct drm_device *dev)
4864{
4865 int i;
4866
Dave Airlie260883c2009-01-22 17:58:49 +10004867 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004868 i915_gem_free_phys_object(dev, i);
4869}
4870
4871void i915_gem_detach_phys_object(struct drm_device *dev,
4872 struct drm_gem_object *obj)
4873{
Chris Wilsone5281cc2010-10-28 13:45:36 +01004874 struct address_space *mapping = obj->filp->f_path.dentry->d_inode->i_mapping;
4875 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
4876 char *vaddr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004877 int i;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004878 int page_count;
4879
Dave Airlie71acb5e2008-12-30 20:31:46 +10004880 if (!obj_priv->phys_obj)
4881 return;
Chris Wilsone5281cc2010-10-28 13:45:36 +01004882 vaddr = obj_priv->phys_obj->handle->vaddr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004883
4884 page_count = obj->size / PAGE_SIZE;
4885
4886 for (i = 0; i < page_count; i++) {
Chris Wilsone5281cc2010-10-28 13:45:36 +01004887 struct page *page = read_cache_page_gfp(mapping, i,
4888 GFP_HIGHUSER | __GFP_RECLAIMABLE);
4889 if (!IS_ERR(page)) {
4890 char *dst = kmap_atomic(page);
4891 memcpy(dst, vaddr + i*PAGE_SIZE, PAGE_SIZE);
4892 kunmap_atomic(dst);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004893
Chris Wilsone5281cc2010-10-28 13:45:36 +01004894 drm_clflush_pages(&page, 1);
4895
4896 set_page_dirty(page);
4897 mark_page_accessed(page);
4898 page_cache_release(page);
4899 }
Dave Airlie71acb5e2008-12-30 20:31:46 +10004900 }
Dave Airlie71acb5e2008-12-30 20:31:46 +10004901 drm_agp_chipset_flush(dev);
Chris Wilsond78b47b2009-06-17 21:52:49 +01004902
Dave Airlie71acb5e2008-12-30 20:31:46 +10004903 obj_priv->phys_obj->cur_obj = NULL;
4904 obj_priv->phys_obj = NULL;
4905}
4906
4907int
4908i915_gem_attach_phys_object(struct drm_device *dev,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004909 struct drm_gem_object *obj,
4910 int id,
4911 int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004912{
Chris Wilsone5281cc2010-10-28 13:45:36 +01004913 struct address_space *mapping = obj->filp->f_path.dentry->d_inode->i_mapping;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004914 drm_i915_private_t *dev_priv = dev->dev_private;
4915 struct drm_i915_gem_object *obj_priv;
4916 int ret = 0;
4917 int page_count;
4918 int i;
4919
4920 if (id > I915_MAX_PHYS_OBJECT)
4921 return -EINVAL;
4922
Daniel Vetter23010e42010-03-08 13:35:02 +01004923 obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004924
4925 if (obj_priv->phys_obj) {
4926 if (obj_priv->phys_obj->id == id)
4927 return 0;
4928 i915_gem_detach_phys_object(dev, obj);
4929 }
4930
Dave Airlie71acb5e2008-12-30 20:31:46 +10004931 /* create a new object */
4932 if (!dev_priv->mm.phys_objs[id - 1]) {
4933 ret = i915_gem_init_phys_object(dev, id,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004934 obj->size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004935 if (ret) {
Linus Torvaldsaeb565d2009-01-26 10:01:53 -08004936 DRM_ERROR("failed to init phys object %d size: %zu\n", id, obj->size);
Chris Wilsone5281cc2010-10-28 13:45:36 +01004937 return ret;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004938 }
4939 }
4940
4941 /* bind to the object */
4942 obj_priv->phys_obj = dev_priv->mm.phys_objs[id - 1];
4943 obj_priv->phys_obj->cur_obj = obj;
4944
Dave Airlie71acb5e2008-12-30 20:31:46 +10004945 page_count = obj->size / PAGE_SIZE;
4946
4947 for (i = 0; i < page_count; i++) {
Chris Wilsone5281cc2010-10-28 13:45:36 +01004948 struct page *page;
4949 char *dst, *src;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004950
Chris Wilsone5281cc2010-10-28 13:45:36 +01004951 page = read_cache_page_gfp(mapping, i,
4952 GFP_HIGHUSER | __GFP_RECLAIMABLE);
4953 if (IS_ERR(page))
4954 return PTR_ERR(page);
4955
Chris Wilsonff75b9b2010-10-30 22:52:31 +01004956 src = kmap_atomic(page);
Chris Wilsone5281cc2010-10-28 13:45:36 +01004957 dst = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004958 memcpy(dst, src, PAGE_SIZE);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -07004959 kunmap_atomic(src);
Chris Wilsone5281cc2010-10-28 13:45:36 +01004960
4961 mark_page_accessed(page);
4962 page_cache_release(page);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004963 }
4964
4965 return 0;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004966}
4967
4968static int
4969i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
4970 struct drm_i915_gem_pwrite *args,
4971 struct drm_file *file_priv)
4972{
Daniel Vetter23010e42010-03-08 13:35:02 +01004973 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004974 void *obj_addr;
4975 int ret;
4976 char __user *user_data;
4977
4978 user_data = (char __user *) (uintptr_t) args->data_ptr;
4979 obj_addr = obj_priv->phys_obj->handle->vaddr + args->offset;
4980
Zhao Yakui44d98a62009-10-09 11:39:40 +08004981 DRM_DEBUG_DRIVER("obj_addr %p, %lld\n", obj_addr, args->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004982 ret = copy_from_user(obj_addr, user_data, args->size);
4983 if (ret)
4984 return -EFAULT;
4985
4986 drm_agp_chipset_flush(dev);
4987 return 0;
4988}
Eric Anholtb9624422009-06-03 07:27:35 +00004989
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004990void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00004991{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004992 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00004993
4994 /* Clean up our request list when the client is going away, so that
4995 * later retire_requests won't dereference our soon-to-be-gone
4996 * file_priv.
4997 */
Chris Wilson1c255952010-09-26 11:03:27 +01004998 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004999 while (!list_empty(&file_priv->mm.request_list)) {
5000 struct drm_i915_gem_request *request;
5001
5002 request = list_first_entry(&file_priv->mm.request_list,
5003 struct drm_i915_gem_request,
5004 client_list);
5005 list_del(&request->client_list);
5006 request->file_priv = NULL;
5007 }
Chris Wilson1c255952010-09-26 11:03:27 +01005008 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00005009}
Chris Wilson31169712009-09-14 16:50:28 +01005010
Chris Wilson31169712009-09-14 16:50:28 +01005011static int
Chris Wilson1637ef42010-04-20 17:10:35 +01005012i915_gpu_is_active(struct drm_device *dev)
5013{
5014 drm_i915_private_t *dev_priv = dev->dev_private;
5015 int lists_empty;
5016
Chris Wilson1637ef42010-04-20 17:10:35 +01005017 lists_empty = list_empty(&dev_priv->mm.flushing_list) &&
Chris Wilson17250b72010-10-28 12:51:39 +01005018 list_empty(&dev_priv->mm.active_list);
Chris Wilson1637ef42010-04-20 17:10:35 +01005019
5020 return !lists_empty;
5021}
5022
5023static int
Chris Wilson17250b72010-10-28 12:51:39 +01005024i915_gem_inactive_shrink(struct shrinker *shrinker,
5025 int nr_to_scan,
5026 gfp_t gfp_mask)
Chris Wilson31169712009-09-14 16:50:28 +01005027{
Chris Wilson17250b72010-10-28 12:51:39 +01005028 struct drm_i915_private *dev_priv =
5029 container_of(shrinker,
5030 struct drm_i915_private,
5031 mm.inactive_shrinker);
5032 struct drm_device *dev = dev_priv->dev;
5033 struct drm_i915_gem_object *obj, *next;
5034 int cnt;
5035
5036 if (!mutex_trylock(&dev->struct_mutex))
Chris Wilsonbbe2e112010-10-28 22:35:07 +01005037 return 0;
Chris Wilson31169712009-09-14 16:50:28 +01005038
5039 /* "fast-path" to count number of available objects */
5040 if (nr_to_scan == 0) {
Chris Wilson17250b72010-10-28 12:51:39 +01005041 cnt = 0;
5042 list_for_each_entry(obj,
5043 &dev_priv->mm.inactive_list,
5044 mm_list)
5045 cnt++;
5046 mutex_unlock(&dev->struct_mutex);
5047 return cnt / 100 * sysctl_vfs_cache_pressure;
Chris Wilson31169712009-09-14 16:50:28 +01005048 }
5049
Chris Wilson1637ef42010-04-20 17:10:35 +01005050rescan:
Chris Wilson31169712009-09-14 16:50:28 +01005051 /* first scan for clean buffers */
Chris Wilson17250b72010-10-28 12:51:39 +01005052 i915_gem_retire_requests(dev);
Chris Wilson31169712009-09-14 16:50:28 +01005053
Chris Wilson17250b72010-10-28 12:51:39 +01005054 list_for_each_entry_safe(obj, next,
5055 &dev_priv->mm.inactive_list,
5056 mm_list) {
5057 if (i915_gem_object_is_purgeable(obj)) {
5058 i915_gem_object_unbind(&obj->base);
5059 if (--nr_to_scan == 0)
5060 break;
Chris Wilson31169712009-09-14 16:50:28 +01005061 }
Chris Wilson31169712009-09-14 16:50:28 +01005062 }
5063
5064 /* second pass, evict/count anything still on the inactive list */
Chris Wilson17250b72010-10-28 12:51:39 +01005065 cnt = 0;
5066 list_for_each_entry_safe(obj, next,
5067 &dev_priv->mm.inactive_list,
5068 mm_list) {
5069 if (nr_to_scan) {
5070 i915_gem_object_unbind(&obj->base);
5071 nr_to_scan--;
5072 } else
5073 cnt++;
Chris Wilson31169712009-09-14 16:50:28 +01005074 }
5075
Chris Wilson17250b72010-10-28 12:51:39 +01005076 if (nr_to_scan && i915_gpu_is_active(dev)) {
Chris Wilson1637ef42010-04-20 17:10:35 +01005077 /*
5078 * We are desperate for pages, so as a last resort, wait
5079 * for the GPU to finish and discard whatever we can.
5080 * This has a dramatic impact to reduce the number of
5081 * OOM-killer events whilst running the GPU aggressively.
5082 */
Chris Wilson17250b72010-10-28 12:51:39 +01005083 if (i915_gpu_idle(dev) == 0)
Chris Wilson1637ef42010-04-20 17:10:35 +01005084 goto rescan;
5085 }
Chris Wilson17250b72010-10-28 12:51:39 +01005086 mutex_unlock(&dev->struct_mutex);
5087 return cnt / 100 * sysctl_vfs_cache_pressure;
Chris Wilson31169712009-09-14 16:50:28 +01005088}