blob: d196895527a65de566aeb1978b27ae98c54fdbad [file] [log] [blame]
Eric Anholt673a3942008-07-30 12:06:12 -07001/*
2 * Copyright © 2008 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 *
26 */
27
28#include "drmP.h"
29#include "drm.h"
30#include "i915_drm.h"
31#include "i915_drv.h"
Chris Wilson1c5d22f2009-08-25 11:15:50 +010032#include "i915_trace.h"
Jesse Barnes652c3932009-08-17 13:31:43 -070033#include "intel_drv.h"
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Eric Anholt673a3942008-07-30 12:06:12 -070035#include <linux/swap.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080036#include <linux/pci.h>
Eric Anholt673a3942008-07-30 12:06:12 -070037
Chris Wilson0f8c6d72010-11-01 12:38:44 +000038struct change_domains {
39 uint32_t invalidate_domains;
40 uint32_t flush_domains;
41 uint32_t flush_rings;
42};
43
Chris Wilson05394f32010-11-08 19:18:58 +000044static uint32_t i915_gem_get_gtt_alignment(struct drm_i915_gem_object *obj);
45static uint32_t i915_gem_get_gtt_size(struct drm_i915_gem_object *obj);
Daniel Vetterba3d8d72010-02-11 22:37:04 +010046
Chris Wilson05394f32010-11-08 19:18:58 +000047static int i915_gem_object_flush_gpu_write_domain(struct drm_i915_gem_object *obj,
Daniel Vetterba3d8d72010-02-11 22:37:04 +010048 bool pipelined);
Chris Wilson05394f32010-11-08 19:18:58 +000049static void i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj);
50static void i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj);
51static int i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj,
Eric Anholte47c68e2008-11-14 13:35:19 -080052 int write);
Chris Wilson05394f32010-11-08 19:18:58 +000053static int i915_gem_object_set_cpu_read_domain_range(struct drm_i915_gem_object *obj,
Eric Anholte47c68e2008-11-14 13:35:19 -080054 uint64_t offset,
55 uint64_t size);
Chris Wilson05394f32010-11-08 19:18:58 +000056static void i915_gem_object_set_to_full_cpu_read_domain(struct drm_i915_gem_object *obj);
57static int i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj,
Chris Wilson2cf34d72010-09-14 13:03:28 +010058 bool interruptible);
Chris Wilson05394f32010-11-08 19:18:58 +000059static int i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
Chris Wilsona00b10c2010-09-24 21:15:47 +010060 unsigned alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +010061 bool map_and_fenceable);
Chris Wilson05394f32010-11-08 19:18:58 +000062static void i915_gem_clear_fence_reg(struct drm_i915_gem_object *obj);
63static int i915_gem_phys_pwrite(struct drm_device *dev,
64 struct drm_i915_gem_object *obj,
Dave Airlie71acb5e2008-12-30 20:31:46 +100065 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +000066 struct drm_file *file);
67static void i915_gem_free_object_tail(struct drm_i915_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 }
Daniel Vetter93a37f22010-11-05 20:24:53 +010099 list_add_tail(&obj->gtt_list, &dev_priv->mm.gtt_list);
Chris Wilson73aa8082010-09-30 11:46:12 +0100100}
101
102static void i915_gem_info_remove_gtt(struct drm_i915_private *dev_priv,
Chris Wilsona00b10c2010-09-24 21:15:47 +0100103 struct drm_i915_gem_object *obj)
Chris Wilson73aa8082010-09-30 11:46:12 +0100104{
105 dev_priv->mm.gtt_count--;
Chris Wilsona00b10c2010-09-24 21:15:47 +0100106 dev_priv->mm.gtt_memory -= obj->gtt_space->size;
107 if (obj->gtt_offset < dev_priv->mm.gtt_mappable_end) {
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200108 dev_priv->mm.mappable_gtt_used -=
Chris Wilsona00b10c2010-09-24 21:15:47 +0100109 min_t(size_t, obj->gtt_space->size,
110 dev_priv->mm.gtt_mappable_end - obj->gtt_offset);
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200111 }
Daniel Vetter93a37f22010-11-05 20:24:53 +0100112 list_del_init(&obj->gtt_list);
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200113}
114
115/**
116 * Update the mappable working set counters. Call _only_ when there is a change
117 * in one of (pin|fault)_mappable and update *_mappable _before_ calling.
118 * @mappable: new state the changed mappable flag (either pin_ or fault_).
119 */
120static void
121i915_gem_info_update_mappable(struct drm_i915_private *dev_priv,
Chris Wilsona00b10c2010-09-24 21:15:47 +0100122 struct drm_i915_gem_object *obj,
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200123 bool mappable)
124{
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200125 if (mappable) {
Chris Wilsona00b10c2010-09-24 21:15:47 +0100126 if (obj->pin_mappable && obj->fault_mappable)
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200127 /* Combined state was already mappable. */
128 return;
129 dev_priv->mm.gtt_mappable_count++;
Chris Wilsona00b10c2010-09-24 21:15:47 +0100130 dev_priv->mm.gtt_mappable_memory += obj->gtt_space->size;
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200131 } else {
Chris Wilsona00b10c2010-09-24 21:15:47 +0100132 if (obj->pin_mappable || obj->fault_mappable)
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200133 /* Combined state still mappable. */
134 return;
135 dev_priv->mm.gtt_mappable_count--;
Chris Wilsona00b10c2010-09-24 21:15:47 +0100136 dev_priv->mm.gtt_mappable_memory -= obj->gtt_space->size;
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200137 }
Chris Wilson73aa8082010-09-30 11:46:12 +0100138}
139
140static void i915_gem_info_add_pin(struct drm_i915_private *dev_priv,
Chris Wilsona00b10c2010-09-24 21:15:47 +0100141 struct drm_i915_gem_object *obj,
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200142 bool mappable)
Chris Wilson73aa8082010-09-30 11:46:12 +0100143{
144 dev_priv->mm.pin_count++;
Chris Wilsona00b10c2010-09-24 21:15:47 +0100145 dev_priv->mm.pin_memory += obj->gtt_space->size;
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200146 if (mappable) {
Chris Wilsona00b10c2010-09-24 21:15:47 +0100147 obj->pin_mappable = true;
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200148 i915_gem_info_update_mappable(dev_priv, obj, true);
149 }
Chris Wilson73aa8082010-09-30 11:46:12 +0100150}
151
152static void i915_gem_info_remove_pin(struct drm_i915_private *dev_priv,
Chris Wilsona00b10c2010-09-24 21:15:47 +0100153 struct drm_i915_gem_object *obj)
Chris Wilson73aa8082010-09-30 11:46:12 +0100154{
155 dev_priv->mm.pin_count--;
Chris Wilsona00b10c2010-09-24 21:15:47 +0100156 dev_priv->mm.pin_memory -= obj->gtt_space->size;
157 if (obj->pin_mappable) {
158 obj->pin_mappable = false;
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200159 i915_gem_info_update_mappable(dev_priv, obj, false);
160 }
Chris Wilson73aa8082010-09-30 11:46:12 +0100161}
162
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100163int
164i915_gem_check_is_wedged(struct drm_device *dev)
165{
166 struct drm_i915_private *dev_priv = dev->dev_private;
167 struct completion *x = &dev_priv->error_completion;
168 unsigned long flags;
169 int ret;
170
171 if (!atomic_read(&dev_priv->mm.wedged))
172 return 0;
173
174 ret = wait_for_completion_interruptible(x);
175 if (ret)
176 return ret;
177
178 /* Success, we reset the GPU! */
179 if (!atomic_read(&dev_priv->mm.wedged))
180 return 0;
181
182 /* GPU is hung, bump the completion count to account for
183 * the token we just consumed so that we never hit zero and
184 * end up waiting upon a subsequent completion event that
185 * will never happen.
186 */
187 spin_lock_irqsave(&x->wait.lock, flags);
188 x->done++;
189 spin_unlock_irqrestore(&x->wait.lock, flags);
190 return -EIO;
191}
192
Chris Wilson76c1dec2010-09-25 11:22:51 +0100193static int i915_mutex_lock_interruptible(struct drm_device *dev)
194{
195 struct drm_i915_private *dev_priv = dev->dev_private;
196 int ret;
197
198 ret = i915_gem_check_is_wedged(dev);
199 if (ret)
200 return ret;
201
202 ret = mutex_lock_interruptible(&dev->struct_mutex);
203 if (ret)
204 return ret;
205
206 if (atomic_read(&dev_priv->mm.wedged)) {
207 mutex_unlock(&dev->struct_mutex);
208 return -EAGAIN;
209 }
210
Chris Wilson23bc5982010-09-29 16:10:57 +0100211 WARN_ON(i915_verify_lists(dev));
Chris Wilson76c1dec2010-09-25 11:22:51 +0100212 return 0;
213}
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100214
Chris Wilson7d1c4802010-08-07 21:45:03 +0100215static inline bool
Chris Wilson05394f32010-11-08 19:18:58 +0000216i915_gem_object_is_inactive(struct drm_i915_gem_object *obj)
Chris Wilson7d1c4802010-08-07 21:45:03 +0100217{
Chris Wilson05394f32010-11-08 19:18:58 +0000218 return obj->gtt_space && !obj->active && obj->pin_count == 0;
Chris Wilson7d1c4802010-08-07 21:45:03 +0100219}
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,
Chris Wilson05394f32010-11-08 19:18:58 +0000246 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700247{
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,
Chris Wilson05394f32010-11-08 19:18:58 +0000260 struct drm_file *file)
Eric Anholt5a125c32008-10-22 21:40:13 -0700261{
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,
Chris Wilson05394f32010-11-08 19:18:58 +0000282 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700283{
284 struct drm_i915_gem_create *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +0000285 struct drm_i915_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
Chris Wilson05394f32010-11-08 19:18:58 +0000296 ret = drm_gem_handle_create(file, &obj->base, &handle);
Chris Wilson1dfd9752010-09-06 14:44:14 +0100297 if (ret) {
Chris Wilson05394f32010-11-08 19:18:58 +0000298 drm_gem_object_release(&obj->base);
299 i915_gem_info_remove_obj(dev->dev_private, obj->base.size);
Chris Wilson202f2fe2010-10-14 13:20:40 +0100300 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 */
Chris Wilson05394f32010-11-08 19:18:58 +0000305 drm_gem_object_unreference(&obj->base);
Chris Wilson202f2fe2010-10-14 13:20:40 +0100306 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
Chris Wilson05394f32010-11-08 19:18:58 +0000312static int i915_gem_object_needs_bit17_swizzle(struct drm_i915_gem_object *obj)
Eric Anholt280b7132009-03-12 16:56:27 -0700313{
Chris Wilson05394f32010-11-08 19:18:58 +0000314 drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
Eric Anholt280b7132009-03-12 16:56:27 -0700315
316 return dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_9_10_17 &&
Chris Wilson05394f32010-11-08 19:18:58 +0000317 obj->tiling_mode != I915_TILING_NONE;
Eric Anholt280b7132009-03-12 16:56:27 -0700318}
319
Chris Wilson99a03df2010-05-27 14:15:34 +0100320static inline void
Eric Anholt40123c12009-03-09 13:42:30 -0700321slow_shmem_copy(struct page *dst_page,
322 int dst_offset,
323 struct page *src_page,
324 int src_offset,
325 int length)
326{
327 char *dst_vaddr, *src_vaddr;
328
Chris Wilson99a03df2010-05-27 14:15:34 +0100329 dst_vaddr = kmap(dst_page);
330 src_vaddr = kmap(src_page);
Eric Anholt40123c12009-03-09 13:42:30 -0700331
332 memcpy(dst_vaddr + dst_offset, src_vaddr + src_offset, length);
333
Chris Wilson99a03df2010-05-27 14:15:34 +0100334 kunmap(src_page);
335 kunmap(dst_page);
Eric Anholt40123c12009-03-09 13:42:30 -0700336}
337
Chris Wilson99a03df2010-05-27 14:15:34 +0100338static inline void
Eric Anholt280b7132009-03-12 16:56:27 -0700339slow_shmem_bit17_copy(struct page *gpu_page,
340 int gpu_offset,
341 struct page *cpu_page,
342 int cpu_offset,
343 int length,
344 int is_read)
345{
346 char *gpu_vaddr, *cpu_vaddr;
347
348 /* Use the unswizzled path if this page isn't affected. */
349 if ((page_to_phys(gpu_page) & (1 << 17)) == 0) {
350 if (is_read)
351 return slow_shmem_copy(cpu_page, cpu_offset,
352 gpu_page, gpu_offset, length);
353 else
354 return slow_shmem_copy(gpu_page, gpu_offset,
355 cpu_page, cpu_offset, length);
356 }
357
Chris Wilson99a03df2010-05-27 14:15:34 +0100358 gpu_vaddr = kmap(gpu_page);
359 cpu_vaddr = kmap(cpu_page);
Eric Anholt280b7132009-03-12 16:56:27 -0700360
361 /* Copy the data, XORing A6 with A17 (1). The user already knows he's
362 * XORing with the other bits (A9 for Y, A9 and A10 for X)
363 */
364 while (length > 0) {
365 int cacheline_end = ALIGN(gpu_offset + 1, 64);
366 int this_length = min(cacheline_end - gpu_offset, length);
367 int swizzled_gpu_offset = gpu_offset ^ 64;
368
369 if (is_read) {
370 memcpy(cpu_vaddr + cpu_offset,
371 gpu_vaddr + swizzled_gpu_offset,
372 this_length);
373 } else {
374 memcpy(gpu_vaddr + swizzled_gpu_offset,
375 cpu_vaddr + cpu_offset,
376 this_length);
377 }
378 cpu_offset += this_length;
379 gpu_offset += this_length;
380 length -= this_length;
381 }
382
Chris Wilson99a03df2010-05-27 14:15:34 +0100383 kunmap(cpu_page);
384 kunmap(gpu_page);
Eric Anholt280b7132009-03-12 16:56:27 -0700385}
386
Eric Anholt673a3942008-07-30 12:06:12 -0700387/**
Eric Anholteb014592009-03-10 11:44:52 -0700388 * This is the fast shmem pread path, which attempts to copy_from_user directly
389 * from the backing pages of the object to the user's address space. On a
390 * fault, it fails so we can fall back to i915_gem_shmem_pwrite_slow().
391 */
392static int
Chris Wilson05394f32010-11-08 19:18:58 +0000393i915_gem_shmem_pread_fast(struct drm_device *dev,
394 struct drm_i915_gem_object *obj,
Eric Anholteb014592009-03-10 11:44:52 -0700395 struct drm_i915_gem_pread *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000396 struct drm_file *file)
Eric Anholteb014592009-03-10 11:44:52 -0700397{
Chris Wilson05394f32010-11-08 19:18:58 +0000398 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Eric Anholteb014592009-03-10 11:44:52 -0700399 ssize_t remain;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100400 loff_t offset;
Eric Anholteb014592009-03-10 11:44:52 -0700401 char __user *user_data;
402 int page_offset, page_length;
Eric Anholteb014592009-03-10 11:44:52 -0700403
404 user_data = (char __user *) (uintptr_t) args->data_ptr;
405 remain = args->size;
406
Eric Anholteb014592009-03-10 11:44:52 -0700407 offset = args->offset;
408
409 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100410 struct page *page;
411 char *vaddr;
412 int ret;
413
Eric Anholteb014592009-03-10 11:44:52 -0700414 /* Operation in this page
415 *
Eric Anholteb014592009-03-10 11:44:52 -0700416 * page_offset = offset within page
417 * page_length = bytes to copy for this page
418 */
Eric Anholteb014592009-03-10 11:44:52 -0700419 page_offset = offset & (PAGE_SIZE-1);
420 page_length = remain;
421 if ((page_offset + remain) > PAGE_SIZE)
422 page_length = PAGE_SIZE - page_offset;
423
Chris Wilsone5281cc2010-10-28 13:45:36 +0100424 page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
425 GFP_HIGHUSER | __GFP_RECLAIMABLE);
426 if (IS_ERR(page))
427 return PTR_ERR(page);
428
429 vaddr = kmap_atomic(page);
430 ret = __copy_to_user_inatomic(user_data,
431 vaddr + page_offset,
432 page_length);
433 kunmap_atomic(vaddr);
434
435 mark_page_accessed(page);
436 page_cache_release(page);
437 if (ret)
Chris Wilson4f27b752010-10-14 15:26:45 +0100438 return -EFAULT;
Eric Anholteb014592009-03-10 11:44:52 -0700439
440 remain -= page_length;
441 user_data += page_length;
442 offset += page_length;
443 }
444
Chris Wilson4f27b752010-10-14 15:26:45 +0100445 return 0;
Eric Anholteb014592009-03-10 11:44:52 -0700446}
447
448/**
449 * This is the fallback shmem pread path, which allocates temporary storage
450 * in kernel space to copy_to_user into outside of the struct_mutex, so we
451 * can copy out of the object's backing pages while holding the struct mutex
452 * and not take page faults.
453 */
454static int
Chris Wilson05394f32010-11-08 19:18:58 +0000455i915_gem_shmem_pread_slow(struct drm_device *dev,
456 struct drm_i915_gem_object *obj,
Eric Anholteb014592009-03-10 11:44:52 -0700457 struct drm_i915_gem_pread *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000458 struct drm_file *file)
Eric Anholteb014592009-03-10 11:44:52 -0700459{
Chris Wilson05394f32010-11-08 19:18:58 +0000460 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Eric Anholteb014592009-03-10 11:44:52 -0700461 struct mm_struct *mm = current->mm;
462 struct page **user_pages;
463 ssize_t remain;
464 loff_t offset, pinned_pages, i;
465 loff_t first_data_page, last_data_page, num_pages;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100466 int shmem_page_offset;
467 int data_page_index, data_page_offset;
Eric Anholteb014592009-03-10 11:44:52 -0700468 int page_length;
469 int ret;
470 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700471 int do_bit17_swizzling;
Eric Anholteb014592009-03-10 11:44:52 -0700472
473 remain = args->size;
474
475 /* Pin the user pages containing the data. We can't fault while
476 * holding the struct mutex, yet we want to hold it while
477 * dereferencing the user data.
478 */
479 first_data_page = data_ptr / PAGE_SIZE;
480 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
481 num_pages = last_data_page - first_data_page + 1;
482
Chris Wilson4f27b752010-10-14 15:26:45 +0100483 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
Eric Anholteb014592009-03-10 11:44:52 -0700484 if (user_pages == NULL)
485 return -ENOMEM;
486
Chris Wilson4f27b752010-10-14 15:26:45 +0100487 mutex_unlock(&dev->struct_mutex);
Eric Anholteb014592009-03-10 11:44:52 -0700488 down_read(&mm->mmap_sem);
489 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
Eric Anholte5e9ecd2009-04-07 16:01:22 -0700490 num_pages, 1, 0, user_pages, NULL);
Eric Anholteb014592009-03-10 11:44:52 -0700491 up_read(&mm->mmap_sem);
Chris Wilson4f27b752010-10-14 15:26:45 +0100492 mutex_lock(&dev->struct_mutex);
Eric Anholteb014592009-03-10 11:44:52 -0700493 if (pinned_pages < num_pages) {
494 ret = -EFAULT;
Chris Wilson4f27b752010-10-14 15:26:45 +0100495 goto out;
Eric Anholteb014592009-03-10 11:44:52 -0700496 }
497
Chris Wilson4f27b752010-10-14 15:26:45 +0100498 ret = i915_gem_object_set_cpu_read_domain_range(obj,
499 args->offset,
Eric Anholteb014592009-03-10 11:44:52 -0700500 args->size);
Chris Wilson4f27b752010-10-14 15:26:45 +0100501 if (ret)
502 goto out;
503
504 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700505
Eric Anholteb014592009-03-10 11:44:52 -0700506 offset = args->offset;
507
508 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100509 struct page *page;
510
Eric Anholteb014592009-03-10 11:44:52 -0700511 /* Operation in this page
512 *
Eric Anholteb014592009-03-10 11:44:52 -0700513 * shmem_page_offset = offset within page in shmem file
514 * data_page_index = page number in get_user_pages return
515 * data_page_offset = offset with data_page_index page.
516 * page_length = bytes to copy for this page
517 */
Eric Anholteb014592009-03-10 11:44:52 -0700518 shmem_page_offset = offset & ~PAGE_MASK;
519 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
520 data_page_offset = data_ptr & ~PAGE_MASK;
521
522 page_length = remain;
523 if ((shmem_page_offset + page_length) > PAGE_SIZE)
524 page_length = PAGE_SIZE - shmem_page_offset;
525 if ((data_page_offset + page_length) > PAGE_SIZE)
526 page_length = PAGE_SIZE - data_page_offset;
527
Chris Wilsone5281cc2010-10-28 13:45:36 +0100528 page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
529 GFP_HIGHUSER | __GFP_RECLAIMABLE);
530 if (IS_ERR(page))
531 return PTR_ERR(page);
532
Eric Anholt280b7132009-03-12 16:56:27 -0700533 if (do_bit17_swizzling) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100534 slow_shmem_bit17_copy(page,
Eric Anholt280b7132009-03-12 16:56:27 -0700535 shmem_page_offset,
Chris Wilson99a03df2010-05-27 14:15:34 +0100536 user_pages[data_page_index],
537 data_page_offset,
538 page_length,
539 1);
540 } else {
541 slow_shmem_copy(user_pages[data_page_index],
542 data_page_offset,
Chris Wilsone5281cc2010-10-28 13:45:36 +0100543 page,
Chris Wilson99a03df2010-05-27 14:15:34 +0100544 shmem_page_offset,
545 page_length);
Eric Anholt280b7132009-03-12 16:56:27 -0700546 }
Eric Anholteb014592009-03-10 11:44:52 -0700547
Chris Wilsone5281cc2010-10-28 13:45:36 +0100548 mark_page_accessed(page);
549 page_cache_release(page);
550
Eric Anholteb014592009-03-10 11:44:52 -0700551 remain -= page_length;
552 data_ptr += page_length;
553 offset += page_length;
554 }
555
Chris Wilson4f27b752010-10-14 15:26:45 +0100556out:
Eric Anholteb014592009-03-10 11:44:52 -0700557 for (i = 0; i < pinned_pages; i++) {
558 SetPageDirty(user_pages[i]);
Chris Wilsone5281cc2010-10-28 13:45:36 +0100559 mark_page_accessed(user_pages[i]);
Eric Anholteb014592009-03-10 11:44:52 -0700560 page_cache_release(user_pages[i]);
561 }
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700562 drm_free_large(user_pages);
Eric Anholteb014592009-03-10 11:44:52 -0700563
564 return ret;
565}
566
Eric Anholt673a3942008-07-30 12:06:12 -0700567/**
568 * Reads data from the object referenced by handle.
569 *
570 * On error, the contents of *data are undefined.
571 */
572int
573i915_gem_pread_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000574 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700575{
576 struct drm_i915_gem_pread *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +0000577 struct drm_i915_gem_object *obj;
Chris Wilson35b62a82010-09-26 20:23:38 +0100578 int ret = 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700579
Chris Wilson51311d02010-11-17 09:10:42 +0000580 if (args->size == 0)
581 return 0;
582
583 if (!access_ok(VERIFY_WRITE,
584 (char __user *)(uintptr_t)args->data_ptr,
585 args->size))
586 return -EFAULT;
587
588 ret = fault_in_pages_writeable((char __user *)(uintptr_t)args->data_ptr,
589 args->size);
590 if (ret)
591 return -EFAULT;
592
Chris Wilson4f27b752010-10-14 15:26:45 +0100593 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100594 if (ret)
Chris Wilson4f27b752010-10-14 15:26:45 +0100595 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700596
Chris Wilson05394f32010-11-08 19:18:58 +0000597 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100598 if (obj == NULL) {
599 ret = -ENOENT;
600 goto unlock;
Chris Wilson4f27b752010-10-14 15:26:45 +0100601 }
Eric Anholt673a3942008-07-30 12:06:12 -0700602
Chris Wilson7dcd2492010-09-26 20:21:44 +0100603 /* Bounds check source. */
Chris Wilson05394f32010-11-08 19:18:58 +0000604 if (args->offset > obj->base.size ||
605 args->size > obj->base.size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +0100606 ret = -EINVAL;
Chris Wilson35b62a82010-09-26 20:23:38 +0100607 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +0100608 }
609
Chris Wilson4f27b752010-10-14 15:26:45 +0100610 ret = i915_gem_object_set_cpu_read_domain_range(obj,
611 args->offset,
612 args->size);
613 if (ret)
Chris Wilsone5281cc2010-10-28 13:45:36 +0100614 goto out;
Chris Wilson4f27b752010-10-14 15:26:45 +0100615
616 ret = -EFAULT;
617 if (!i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilson05394f32010-11-08 19:18:58 +0000618 ret = i915_gem_shmem_pread_fast(dev, obj, args, file);
Chris Wilson4f27b752010-10-14 15:26:45 +0100619 if (ret == -EFAULT)
Chris Wilson05394f32010-11-08 19:18:58 +0000620 ret = i915_gem_shmem_pread_slow(dev, obj, args, file);
Eric Anholt673a3942008-07-30 12:06:12 -0700621
Chris Wilson35b62a82010-09-26 20:23:38 +0100622out:
Chris Wilson05394f32010-11-08 19:18:58 +0000623 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100624unlock:
Chris Wilson4f27b752010-10-14 15:26:45 +0100625 mutex_unlock(&dev->struct_mutex);
Eric Anholteb014592009-03-10 11:44:52 -0700626 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700627}
628
Keith Packard0839ccb2008-10-30 19:38:48 -0700629/* This is the fast write path which cannot handle
630 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700631 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700632
Keith Packard0839ccb2008-10-30 19:38:48 -0700633static inline int
634fast_user_write(struct io_mapping *mapping,
635 loff_t page_base, int page_offset,
636 char __user *user_data,
637 int length)
638{
639 char *vaddr_atomic;
640 unsigned long unwritten;
641
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700642 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
Keith Packard0839ccb2008-10-30 19:38:48 -0700643 unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
644 user_data, length);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700645 io_mapping_unmap_atomic(vaddr_atomic);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100646 return unwritten;
Keith Packard0839ccb2008-10-30 19:38:48 -0700647}
648
649/* Here's the write path which can sleep for
650 * page faults
651 */
652
Chris Wilsonab34c222010-05-27 14:15:35 +0100653static inline void
Eric Anholt3de09aa2009-03-09 09:42:23 -0700654slow_kernel_write(struct io_mapping *mapping,
655 loff_t gtt_base, int gtt_offset,
656 struct page *user_page, int user_offset,
657 int length)
Keith Packard0839ccb2008-10-30 19:38:48 -0700658{
Chris Wilsonab34c222010-05-27 14:15:35 +0100659 char __iomem *dst_vaddr;
660 char *src_vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -0700661
Chris Wilsonab34c222010-05-27 14:15:35 +0100662 dst_vaddr = io_mapping_map_wc(mapping, gtt_base);
663 src_vaddr = kmap(user_page);
664
665 memcpy_toio(dst_vaddr + gtt_offset,
666 src_vaddr + user_offset,
667 length);
668
669 kunmap(user_page);
670 io_mapping_unmap(dst_vaddr);
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700671}
672
Eric Anholt3de09aa2009-03-09 09:42:23 -0700673/**
674 * This is the fast pwrite path, where we copy the data directly from the
675 * user into the GTT, uncached.
676 */
Eric Anholt673a3942008-07-30 12:06:12 -0700677static int
Chris Wilson05394f32010-11-08 19:18:58 +0000678i915_gem_gtt_pwrite_fast(struct drm_device *dev,
679 struct drm_i915_gem_object *obj,
Eric Anholt3de09aa2009-03-09 09:42:23 -0700680 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000681 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700682{
Keith Packard0839ccb2008-10-30 19:38:48 -0700683 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700684 ssize_t remain;
Keith Packard0839ccb2008-10-30 19:38:48 -0700685 loff_t offset, page_base;
Eric Anholt673a3942008-07-30 12:06:12 -0700686 char __user *user_data;
Keith Packard0839ccb2008-10-30 19:38:48 -0700687 int page_offset, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700688
689 user_data = (char __user *) (uintptr_t) args->data_ptr;
690 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700691
Chris Wilson05394f32010-11-08 19:18:58 +0000692 offset = obj->gtt_offset + args->offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700693
694 while (remain > 0) {
695 /* Operation in this page
696 *
Keith Packard0839ccb2008-10-30 19:38:48 -0700697 * page_base = page offset within aperture
698 * page_offset = offset within page
699 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -0700700 */
Keith Packard0839ccb2008-10-30 19:38:48 -0700701 page_base = (offset & ~(PAGE_SIZE-1));
702 page_offset = offset & (PAGE_SIZE-1);
703 page_length = remain;
704 if ((page_offset + remain) > PAGE_SIZE)
705 page_length = PAGE_SIZE - page_offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700706
Keith Packard0839ccb2008-10-30 19:38:48 -0700707 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -0700708 * source page isn't available. Return the error and we'll
709 * retry in the slow path.
Keith Packard0839ccb2008-10-30 19:38:48 -0700710 */
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100711 if (fast_user_write(dev_priv->mm.gtt_mapping, page_base,
712 page_offset, user_data, page_length))
713
714 return -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -0700715
Keith Packard0839ccb2008-10-30 19:38:48 -0700716 remain -= page_length;
717 user_data += page_length;
718 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700719 }
Eric Anholt673a3942008-07-30 12:06:12 -0700720
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100721 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700722}
723
Eric Anholt3de09aa2009-03-09 09:42:23 -0700724/**
725 * This is the fallback GTT pwrite path, which uses get_user_pages to pin
726 * the memory and maps it using kmap_atomic for copying.
727 *
728 * This code resulted in x11perf -rgb10text consuming about 10% more CPU
729 * than using i915_gem_gtt_pwrite_fast on a G45 (32-bit).
730 */
Eric Anholt3043c602008-10-02 12:24:47 -0700731static int
Chris Wilson05394f32010-11-08 19:18:58 +0000732i915_gem_gtt_pwrite_slow(struct drm_device *dev,
733 struct drm_i915_gem_object *obj,
Eric Anholt3de09aa2009-03-09 09:42:23 -0700734 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000735 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700736{
Eric Anholt3de09aa2009-03-09 09:42:23 -0700737 drm_i915_private_t *dev_priv = dev->dev_private;
738 ssize_t remain;
739 loff_t gtt_page_base, offset;
740 loff_t first_data_page, last_data_page, num_pages;
741 loff_t pinned_pages, i;
742 struct page **user_pages;
743 struct mm_struct *mm = current->mm;
744 int gtt_page_offset, data_page_offset, data_page_index, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700745 int ret;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700746 uint64_t data_ptr = args->data_ptr;
747
748 remain = args->size;
749
750 /* Pin the user pages containing the data. We can't fault while
751 * holding the struct mutex, and all of the pwrite implementations
752 * want to hold it while dereferencing the user data.
753 */
754 first_data_page = data_ptr / PAGE_SIZE;
755 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
756 num_pages = last_data_page - first_data_page + 1;
757
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100758 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
Eric Anholt3de09aa2009-03-09 09:42:23 -0700759 if (user_pages == NULL)
760 return -ENOMEM;
761
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100762 mutex_unlock(&dev->struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700763 down_read(&mm->mmap_sem);
764 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
765 num_pages, 0, 0, user_pages, NULL);
766 up_read(&mm->mmap_sem);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100767 mutex_lock(&dev->struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700768 if (pinned_pages < num_pages) {
769 ret = -EFAULT;
770 goto out_unpin_pages;
771 }
772
Eric Anholt3de09aa2009-03-09 09:42:23 -0700773 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
774 if (ret)
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100775 goto out_unpin_pages;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700776
Chris Wilson05394f32010-11-08 19:18:58 +0000777 offset = obj->gtt_offset + args->offset;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700778
779 while (remain > 0) {
780 /* Operation in this page
781 *
782 * gtt_page_base = page offset within aperture
783 * gtt_page_offset = offset within page in aperture
784 * data_page_index = page number in get_user_pages return
785 * data_page_offset = offset with data_page_index page.
786 * page_length = bytes to copy for this page
787 */
788 gtt_page_base = offset & PAGE_MASK;
789 gtt_page_offset = offset & ~PAGE_MASK;
790 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
791 data_page_offset = data_ptr & ~PAGE_MASK;
792
793 page_length = remain;
794 if ((gtt_page_offset + page_length) > PAGE_SIZE)
795 page_length = PAGE_SIZE - gtt_page_offset;
796 if ((data_page_offset + page_length) > PAGE_SIZE)
797 page_length = PAGE_SIZE - data_page_offset;
798
Chris Wilsonab34c222010-05-27 14:15:35 +0100799 slow_kernel_write(dev_priv->mm.gtt_mapping,
800 gtt_page_base, gtt_page_offset,
801 user_pages[data_page_index],
802 data_page_offset,
803 page_length);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700804
805 remain -= page_length;
806 offset += page_length;
807 data_ptr += page_length;
808 }
809
Eric Anholt3de09aa2009-03-09 09:42:23 -0700810out_unpin_pages:
811 for (i = 0; i < pinned_pages; i++)
812 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700813 drm_free_large(user_pages);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700814
815 return ret;
816}
817
Eric Anholt40123c12009-03-09 13:42:30 -0700818/**
819 * This is the fast shmem pwrite path, which attempts to directly
820 * copy_from_user into the kmapped pages backing the object.
821 */
Eric Anholt673a3942008-07-30 12:06:12 -0700822static int
Chris Wilson05394f32010-11-08 19:18:58 +0000823i915_gem_shmem_pwrite_fast(struct drm_device *dev,
824 struct drm_i915_gem_object *obj,
Eric Anholt40123c12009-03-09 13:42:30 -0700825 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000826 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700827{
Chris Wilson05394f32010-11-08 19:18:58 +0000828 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Eric Anholt40123c12009-03-09 13:42:30 -0700829 ssize_t remain;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100830 loff_t offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700831 char __user *user_data;
832 int page_offset, page_length;
Eric Anholt40123c12009-03-09 13:42:30 -0700833
834 user_data = (char __user *) (uintptr_t) args->data_ptr;
835 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700836
Eric Anholt673a3942008-07-30 12:06:12 -0700837 offset = args->offset;
Chris Wilson05394f32010-11-08 19:18:58 +0000838 obj->dirty = 1;
Eric Anholt673a3942008-07-30 12:06:12 -0700839
Eric Anholt40123c12009-03-09 13:42:30 -0700840 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100841 struct page *page;
842 char *vaddr;
843 int ret;
844
Eric Anholt40123c12009-03-09 13:42:30 -0700845 /* Operation in this page
846 *
Eric Anholt40123c12009-03-09 13:42:30 -0700847 * page_offset = offset within page
848 * page_length = bytes to copy for this page
849 */
Eric Anholt40123c12009-03-09 13:42:30 -0700850 page_offset = offset & (PAGE_SIZE-1);
851 page_length = remain;
852 if ((page_offset + remain) > PAGE_SIZE)
853 page_length = PAGE_SIZE - page_offset;
854
Chris Wilsone5281cc2010-10-28 13:45:36 +0100855 page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
856 GFP_HIGHUSER | __GFP_RECLAIMABLE);
857 if (IS_ERR(page))
858 return PTR_ERR(page);
859
860 vaddr = kmap_atomic(page, KM_USER0);
861 ret = __copy_from_user_inatomic(vaddr + page_offset,
862 user_data,
863 page_length);
864 kunmap_atomic(vaddr, KM_USER0);
865
866 set_page_dirty(page);
867 mark_page_accessed(page);
868 page_cache_release(page);
869
870 /* If we get a fault while copying data, then (presumably) our
871 * source page isn't available. Return the error and we'll
872 * retry in the slow path.
873 */
874 if (ret)
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100875 return -EFAULT;
Eric Anholt40123c12009-03-09 13:42:30 -0700876
877 remain -= page_length;
878 user_data += page_length;
879 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700880 }
881
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100882 return 0;
Eric Anholt40123c12009-03-09 13:42:30 -0700883}
884
885/**
886 * This is the fallback shmem pwrite path, which uses get_user_pages to pin
887 * the memory and maps it using kmap_atomic for copying.
888 *
889 * This avoids taking mmap_sem for faulting on the user's address while the
890 * struct_mutex is held.
891 */
892static int
Chris Wilson05394f32010-11-08 19:18:58 +0000893i915_gem_shmem_pwrite_slow(struct drm_device *dev,
894 struct drm_i915_gem_object *obj,
Eric Anholt40123c12009-03-09 13:42:30 -0700895 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000896 struct drm_file *file)
Eric Anholt40123c12009-03-09 13:42:30 -0700897{
Chris Wilson05394f32010-11-08 19:18:58 +0000898 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Eric Anholt40123c12009-03-09 13:42:30 -0700899 struct mm_struct *mm = current->mm;
900 struct page **user_pages;
901 ssize_t remain;
902 loff_t offset, pinned_pages, i;
903 loff_t first_data_page, last_data_page, num_pages;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100904 int shmem_page_offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700905 int data_page_index, data_page_offset;
906 int page_length;
907 int ret;
908 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700909 int do_bit17_swizzling;
Eric Anholt40123c12009-03-09 13:42:30 -0700910
911 remain = args->size;
912
913 /* Pin the user pages containing the data. We can't fault while
914 * holding the struct mutex, and all of the pwrite implementations
915 * want to hold it while dereferencing the user data.
916 */
917 first_data_page = data_ptr / PAGE_SIZE;
918 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
919 num_pages = last_data_page - first_data_page + 1;
920
Chris Wilson4f27b752010-10-14 15:26:45 +0100921 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
Eric Anholt40123c12009-03-09 13:42:30 -0700922 if (user_pages == NULL)
923 return -ENOMEM;
924
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100925 mutex_unlock(&dev->struct_mutex);
Eric Anholt40123c12009-03-09 13:42:30 -0700926 down_read(&mm->mmap_sem);
927 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
928 num_pages, 0, 0, user_pages, NULL);
929 up_read(&mm->mmap_sem);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100930 mutex_lock(&dev->struct_mutex);
Eric Anholt40123c12009-03-09 13:42:30 -0700931 if (pinned_pages < num_pages) {
932 ret = -EFAULT;
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100933 goto out;
Eric Anholt40123c12009-03-09 13:42:30 -0700934 }
935
Eric Anholt40123c12009-03-09 13:42:30 -0700936 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100937 if (ret)
938 goto out;
939
940 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700941
Eric Anholt40123c12009-03-09 13:42:30 -0700942 offset = args->offset;
Chris Wilson05394f32010-11-08 19:18:58 +0000943 obj->dirty = 1;
Eric Anholt40123c12009-03-09 13:42:30 -0700944
945 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100946 struct page *page;
947
Eric Anholt40123c12009-03-09 13:42:30 -0700948 /* Operation in this page
949 *
Eric Anholt40123c12009-03-09 13:42:30 -0700950 * shmem_page_offset = offset within page in shmem file
951 * data_page_index = page number in get_user_pages return
952 * data_page_offset = offset with data_page_index page.
953 * page_length = bytes to copy for this page
954 */
Eric Anholt40123c12009-03-09 13:42:30 -0700955 shmem_page_offset = offset & ~PAGE_MASK;
956 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
957 data_page_offset = data_ptr & ~PAGE_MASK;
958
959 page_length = remain;
960 if ((shmem_page_offset + page_length) > PAGE_SIZE)
961 page_length = PAGE_SIZE - shmem_page_offset;
962 if ((data_page_offset + page_length) > PAGE_SIZE)
963 page_length = PAGE_SIZE - data_page_offset;
964
Chris Wilsone5281cc2010-10-28 13:45:36 +0100965 page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
966 GFP_HIGHUSER | __GFP_RECLAIMABLE);
967 if (IS_ERR(page)) {
968 ret = PTR_ERR(page);
969 goto out;
970 }
971
Eric Anholt280b7132009-03-12 16:56:27 -0700972 if (do_bit17_swizzling) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100973 slow_shmem_bit17_copy(page,
Eric Anholt280b7132009-03-12 16:56:27 -0700974 shmem_page_offset,
975 user_pages[data_page_index],
976 data_page_offset,
Chris Wilson99a03df2010-05-27 14:15:34 +0100977 page_length,
978 0);
979 } else {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100980 slow_shmem_copy(page,
Chris Wilson99a03df2010-05-27 14:15:34 +0100981 shmem_page_offset,
982 user_pages[data_page_index],
983 data_page_offset,
984 page_length);
Eric Anholt280b7132009-03-12 16:56:27 -0700985 }
Eric Anholt40123c12009-03-09 13:42:30 -0700986
Chris Wilsone5281cc2010-10-28 13:45:36 +0100987 set_page_dirty(page);
988 mark_page_accessed(page);
989 page_cache_release(page);
990
Eric Anholt40123c12009-03-09 13:42:30 -0700991 remain -= page_length;
992 data_ptr += page_length;
993 offset += page_length;
994 }
995
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100996out:
Eric Anholt40123c12009-03-09 13:42:30 -0700997 for (i = 0; i < pinned_pages; i++)
998 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700999 drm_free_large(user_pages);
Eric Anholt40123c12009-03-09 13:42:30 -07001000
1001 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001002}
1003
1004/**
1005 * Writes data to the object referenced by handle.
1006 *
1007 * On error, the contents of the buffer that were to be modified are undefined.
1008 */
1009int
1010i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001011 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001012{
1013 struct drm_i915_gem_pwrite *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001014 struct drm_i915_gem_object *obj;
Chris Wilson51311d02010-11-17 09:10:42 +00001015 int ret;
1016
1017 if (args->size == 0)
1018 return 0;
1019
1020 if (!access_ok(VERIFY_READ,
1021 (char __user *)(uintptr_t)args->data_ptr,
1022 args->size))
1023 return -EFAULT;
1024
1025 ret = fault_in_pages_readable((char __user *)(uintptr_t)args->data_ptr,
1026 args->size);
1027 if (ret)
1028 return -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -07001029
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001030 ret = i915_mutex_lock_interruptible(dev);
1031 if (ret)
1032 return ret;
1033
Chris Wilson05394f32010-11-08 19:18:58 +00001034 obj = to_intel_bo(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 }
Eric Anholt673a3942008-07-30 12:06:12 -07001039
Chris Wilson7dcd2492010-09-26 20:21:44 +01001040 /* Bounds check destination. */
Chris Wilson05394f32010-11-08 19:18:58 +00001041 if (args->offset > obj->base.size ||
1042 args->size > obj->base.size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001043 ret = -EINVAL;
Chris Wilson35b62a82010-09-26 20:23:38 +01001044 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001045 }
1046
Eric Anholt673a3942008-07-30 12:06:12 -07001047 /* We can only do the GTT pwrite on untiled buffers, as otherwise
1048 * it would end up going through the fenced access, and we'll get
1049 * different detiling behavior between reading and writing.
1050 * pread/pwrite currently are reading and writing from the CPU
1051 * perspective, requiring manual detiling by the client.
1052 */
Chris Wilson05394f32010-11-08 19:18:58 +00001053 if (obj->phys_obj)
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001054 ret = i915_gem_phys_pwrite(dev, obj, args, file);
Chris Wilson05394f32010-11-08 19:18:58 +00001055 else if (obj->tiling_mode == I915_TILING_NONE &&
1056 obj->gtt_space &&
1057 obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01001058 ret = i915_gem_object_pin(obj, 0, true);
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001059 if (ret)
1060 goto out;
1061
1062 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
1063 if (ret)
1064 goto out_unpin;
1065
1066 ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file);
1067 if (ret == -EFAULT)
1068 ret = i915_gem_gtt_pwrite_slow(dev, obj, args, file);
1069
1070out_unpin:
1071 i915_gem_object_unpin(obj);
Eric Anholt40123c12009-03-09 13:42:30 -07001072 } else {
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001073 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
1074 if (ret)
Chris Wilsone5281cc2010-10-28 13:45:36 +01001075 goto out;
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001076
1077 ret = -EFAULT;
1078 if (!i915_gem_object_needs_bit17_swizzle(obj))
1079 ret = i915_gem_shmem_pwrite_fast(dev, obj, args, file);
1080 if (ret == -EFAULT)
1081 ret = i915_gem_shmem_pwrite_slow(dev, obj, args, file);
Eric Anholt40123c12009-03-09 13:42:30 -07001082 }
Eric Anholt673a3942008-07-30 12:06:12 -07001083
Chris Wilson35b62a82010-09-26 20:23:38 +01001084out:
Chris Wilson05394f32010-11-08 19:18:58 +00001085 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001086unlock:
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001087 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07001088 return ret;
1089}
1090
1091/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001092 * Called when user space prepares to use an object with the CPU, either
1093 * through the mmap ioctl's mapping or a GTT mapping.
Eric Anholt673a3942008-07-30 12:06:12 -07001094 */
1095int
1096i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001097 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001098{
Eric Anholta09ba7f2009-08-29 12:49:51 -07001099 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07001100 struct drm_i915_gem_set_domain *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001101 struct drm_i915_gem_object *obj;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001102 uint32_t read_domains = args->read_domains;
1103 uint32_t write_domain = args->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07001104 int ret;
1105
1106 if (!(dev->driver->driver_features & DRIVER_GEM))
1107 return -ENODEV;
1108
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001109 /* Only handle setting domains to types used by the CPU. */
Chris Wilson21d509e2009-06-06 09:46:02 +01001110 if (write_domain & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001111 return -EINVAL;
1112
Chris Wilson21d509e2009-06-06 09:46:02 +01001113 if (read_domains & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001114 return -EINVAL;
1115
1116 /* Having something in the write domain implies it's in the read
1117 * domain, and only that read domain. Enforce that in the request.
1118 */
1119 if (write_domain != 0 && read_domains != write_domain)
1120 return -EINVAL;
1121
Chris Wilson76c1dec2010-09-25 11:22:51 +01001122 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001123 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001124 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001125
Chris Wilson05394f32010-11-08 19:18:58 +00001126 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001127 if (obj == NULL) {
1128 ret = -ENOENT;
1129 goto unlock;
Chris Wilson76c1dec2010-09-25 11:22:51 +01001130 }
Jesse Barnes652c3932009-08-17 13:31:43 -07001131
1132 intel_mark_busy(dev, obj);
1133
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001134 if (read_domains & I915_GEM_DOMAIN_GTT) {
1135 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
Eric Anholt02354392008-11-26 13:58:13 -08001136
Eric Anholta09ba7f2009-08-29 12:49:51 -07001137 /* Update the LRU on the fence for the CPU access that's
1138 * about to occur.
1139 */
Chris Wilson05394f32010-11-08 19:18:58 +00001140 if (obj->fence_reg != I915_FENCE_REG_NONE) {
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001141 struct drm_i915_fence_reg *reg =
Chris Wilson05394f32010-11-08 19:18:58 +00001142 &dev_priv->fence_regs[obj->fence_reg];
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001143 list_move_tail(&reg->lru_list,
Eric Anholta09ba7f2009-08-29 12:49:51 -07001144 &dev_priv->mm.fence_list);
1145 }
1146
Eric Anholt02354392008-11-26 13:58:13 -08001147 /* Silently promote "you're not bound, there was nothing to do"
1148 * to success, since the client was just asking us to
1149 * make sure everything was done.
1150 */
1151 if (ret == -EINVAL)
1152 ret = 0;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001153 } else {
Eric Anholte47c68e2008-11-14 13:35:19 -08001154 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001155 }
1156
Chris Wilson7d1c4802010-08-07 21:45:03 +01001157 /* Maintain LRU order of "inactive" objects */
Chris Wilson05394f32010-11-08 19:18:58 +00001158 if (ret == 0 && i915_gem_object_is_inactive(obj))
1159 list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
Chris Wilson7d1c4802010-08-07 21:45:03 +01001160
Chris Wilson05394f32010-11-08 19:18:58 +00001161 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001162unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07001163 mutex_unlock(&dev->struct_mutex);
1164 return ret;
1165}
1166
1167/**
1168 * Called when user space has done writes to this buffer
1169 */
1170int
1171i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001172 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001173{
1174 struct drm_i915_gem_sw_finish *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001175 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001176 int ret = 0;
1177
1178 if (!(dev->driver->driver_features & DRIVER_GEM))
1179 return -ENODEV;
1180
Chris Wilson76c1dec2010-09-25 11:22:51 +01001181 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001182 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001183 return ret;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001184
Chris Wilson05394f32010-11-08 19:18:58 +00001185 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Eric Anholt673a3942008-07-30 12:06:12 -07001186 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001187 ret = -ENOENT;
1188 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07001189 }
1190
Eric Anholt673a3942008-07-30 12:06:12 -07001191 /* Pinned buffers may be scanout, so flush the cache */
Chris Wilson05394f32010-11-08 19:18:58 +00001192 if (obj->pin_count)
Eric Anholte47c68e2008-11-14 13:35:19 -08001193 i915_gem_object_flush_cpu_write_domain(obj);
1194
Chris Wilson05394f32010-11-08 19:18:58 +00001195 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001196unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07001197 mutex_unlock(&dev->struct_mutex);
1198 return ret;
1199}
1200
1201/**
1202 * Maps the contents of an object, returning the address it is mapped
1203 * into.
1204 *
1205 * While the mapping holds a reference on the contents of the object, it doesn't
1206 * imply a ref on the object itself.
1207 */
1208int
1209i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001210 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001211{
Chris Wilsonda761a62010-10-27 17:37:08 +01001212 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07001213 struct drm_i915_gem_mmap *args = data;
1214 struct drm_gem_object *obj;
1215 loff_t offset;
1216 unsigned long addr;
1217
1218 if (!(dev->driver->driver_features & DRIVER_GEM))
1219 return -ENODEV;
1220
Chris Wilson05394f32010-11-08 19:18:58 +00001221 obj = drm_gem_object_lookup(dev, file, args->handle);
Eric Anholt673a3942008-07-30 12:06:12 -07001222 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001223 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001224
Chris Wilsonda761a62010-10-27 17:37:08 +01001225 if (obj->size > dev_priv->mm.gtt_mappable_end) {
1226 drm_gem_object_unreference_unlocked(obj);
1227 return -E2BIG;
1228 }
1229
Eric Anholt673a3942008-07-30 12:06:12 -07001230 offset = args->offset;
1231
1232 down_write(&current->mm->mmap_sem);
1233 addr = do_mmap(obj->filp, 0, args->size,
1234 PROT_READ | PROT_WRITE, MAP_SHARED,
1235 args->offset);
1236 up_write(&current->mm->mmap_sem);
Luca Barbieribc9025b2010-02-09 05:49:12 +00001237 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001238 if (IS_ERR((void *)addr))
1239 return addr;
1240
1241 args->addr_ptr = (uint64_t) addr;
1242
1243 return 0;
1244}
1245
Jesse Barnesde151cf2008-11-12 10:03:55 -08001246/**
1247 * i915_gem_fault - fault a page into the GTT
1248 * vma: VMA in question
1249 * vmf: fault info
1250 *
1251 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1252 * from userspace. The fault handler takes care of binding the object to
1253 * the GTT (if needed), allocating and programming a fence register (again,
1254 * only if needed based on whether the old reg is still valid or the object
1255 * is tiled) and inserting a new PTE into the faulting process.
1256 *
1257 * Note that the faulting process may involve evicting existing objects
1258 * from the GTT and/or fence registers to make room. So performance may
1259 * suffer if the GTT working set is large or there are few fence registers
1260 * left.
1261 */
1262int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1263{
Chris Wilson05394f32010-11-08 19:18:58 +00001264 struct drm_i915_gem_object *obj = to_intel_bo(vma->vm_private_data);
1265 struct drm_device *dev = obj->base.dev;
Chris Wilson7d1c4802010-08-07 21:45:03 +01001266 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001267 pgoff_t page_offset;
1268 unsigned long pfn;
1269 int ret = 0;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001270 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001271
1272 /* We don't use vmf->pgoff since that has the fake offset */
1273 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1274 PAGE_SHIFT;
1275
1276 /* Now bind it into the GTT if needed */
1277 mutex_lock(&dev->struct_mutex);
Chris Wilson05394f32010-11-08 19:18:58 +00001278 BUG_ON(obj->pin_count && !obj->pin_mappable);
Chris Wilsona00b10c2010-09-24 21:15:47 +01001279
Chris Wilson05394f32010-11-08 19:18:58 +00001280 if (obj->gtt_space) {
1281 if (!obj->map_and_fenceable) {
Chris Wilsona00b10c2010-09-24 21:15:47 +01001282 ret = i915_gem_object_unbind(obj);
1283 if (ret)
1284 goto unlock;
1285 }
1286 }
Daniel Vetter16e809a2010-09-16 19:37:04 +02001287
Chris Wilson05394f32010-11-08 19:18:58 +00001288 if (!obj->gtt_space) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01001289 ret = i915_gem_object_bind_to_gtt(obj, 0, true);
Chris Wilsonc7150892009-09-23 00:43:56 +01001290 if (ret)
1291 goto unlock;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001292 }
1293
Chris Wilson4a684a42010-10-28 14:44:08 +01001294 ret = i915_gem_object_set_to_gtt_domain(obj, write);
1295 if (ret)
1296 goto unlock;
1297
Chris Wilson05394f32010-11-08 19:18:58 +00001298 if (!obj->fault_mappable) {
1299 obj->fault_mappable = true;
1300 i915_gem_info_update_mappable(dev_priv, obj, true);
Daniel Vetterfb7d5162010-10-01 22:05:20 +02001301 }
1302
Jesse Barnesde151cf2008-11-12 10:03:55 -08001303 /* Need a new fence register? */
Chris Wilson05394f32010-11-08 19:18:58 +00001304 if (obj->tiling_mode != I915_TILING_NONE) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01001305 ret = i915_gem_object_get_fence_reg(obj, true);
Chris Wilsonc7150892009-09-23 00:43:56 +01001306 if (ret)
1307 goto unlock;
Eric Anholtd9ddcb92009-01-27 10:33:49 -08001308 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001309
Chris Wilson05394f32010-11-08 19:18:58 +00001310 if (i915_gem_object_is_inactive(obj))
1311 list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
Chris Wilson7d1c4802010-08-07 21:45:03 +01001312
Chris Wilson05394f32010-11-08 19:18:58 +00001313 pfn = ((dev->agp->base + obj->gtt_offset) >> PAGE_SHIFT) +
Jesse Barnesde151cf2008-11-12 10:03:55 -08001314 page_offset;
1315
1316 /* Finally, remap it using the new GTT offset */
1317 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
Chris Wilsonc7150892009-09-23 00:43:56 +01001318unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001319 mutex_unlock(&dev->struct_mutex);
1320
1321 switch (ret) {
Chris Wilson045e7692010-11-07 09:18:22 +00001322 case -EAGAIN:
1323 set_need_resched();
Chris Wilsonc7150892009-09-23 00:43:56 +01001324 case 0:
1325 case -ERESTARTSYS:
1326 return VM_FAULT_NOPAGE;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001327 case -ENOMEM:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001328 return VM_FAULT_OOM;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001329 default:
Chris Wilsonc7150892009-09-23 00:43:56 +01001330 return VM_FAULT_SIGBUS;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001331 }
1332}
1333
1334/**
1335 * i915_gem_create_mmap_offset - create a fake mmap offset for an object
1336 * @obj: obj in question
1337 *
1338 * GEM memory mapping works by handing back to userspace a fake mmap offset
1339 * it can use in a subsequent mmap(2) call. The DRM core code then looks
1340 * up the object based on the offset and sets up the various memory mapping
1341 * structures.
1342 *
1343 * This routine allocates and attaches a fake offset for @obj.
1344 */
1345static int
Chris Wilson05394f32010-11-08 19:18:58 +00001346i915_gem_create_mmap_offset(struct drm_i915_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001347{
Chris Wilson05394f32010-11-08 19:18:58 +00001348 struct drm_device *dev = obj->base.dev;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001349 struct drm_gem_mm *mm = dev->mm_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001350 struct drm_map_list *list;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +11001351 struct drm_local_map *map;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001352 int ret = 0;
1353
1354 /* Set the object up for mmap'ing */
Chris Wilson05394f32010-11-08 19:18:58 +00001355 list = &obj->base.map_list;
Eric Anholt9a298b22009-03-24 12:23:04 -07001356 list->map = kzalloc(sizeof(struct drm_map_list), GFP_KERNEL);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001357 if (!list->map)
1358 return -ENOMEM;
1359
1360 map = list->map;
1361 map->type = _DRM_GEM;
Chris Wilson05394f32010-11-08 19:18:58 +00001362 map->size = obj->base.size;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001363 map->handle = obj;
1364
1365 /* Get a DRM GEM mmap offset allocated... */
1366 list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
Chris Wilson05394f32010-11-08 19:18:58 +00001367 obj->base.size / PAGE_SIZE,
1368 0, 0);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001369 if (!list->file_offset_node) {
Chris Wilson05394f32010-11-08 19:18:58 +00001370 DRM_ERROR("failed to allocate offset for bo %d\n",
1371 obj->base.name);
Chris Wilson9e0ae5342010-09-21 15:05:24 +01001372 ret = -ENOSPC;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001373 goto out_free_list;
1374 }
1375
1376 list->file_offset_node = drm_mm_get_block(list->file_offset_node,
Chris Wilson05394f32010-11-08 19:18:58 +00001377 obj->base.size / PAGE_SIZE,
1378 0);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001379 if (!list->file_offset_node) {
1380 ret = -ENOMEM;
1381 goto out_free_list;
1382 }
1383
1384 list->hash.key = list->file_offset_node->start;
Chris Wilson9e0ae5342010-09-21 15:05:24 +01001385 ret = drm_ht_insert_item(&mm->offset_hash, &list->hash);
1386 if (ret) {
Jesse Barnesde151cf2008-11-12 10:03:55 -08001387 DRM_ERROR("failed to add to map hash\n");
1388 goto out_free_mm;
1389 }
1390
Jesse Barnesde151cf2008-11-12 10:03:55 -08001391 return 0;
1392
1393out_free_mm:
1394 drm_mm_put_block(list->file_offset_node);
1395out_free_list:
Eric Anholt9a298b22009-03-24 12:23:04 -07001396 kfree(list->map);
Chris Wilson39a01d12010-10-28 13:03:06 +01001397 list->map = NULL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001398
1399 return ret;
1400}
1401
Chris Wilson901782b2009-07-10 08:18:50 +01001402/**
1403 * i915_gem_release_mmap - remove physical page mappings
1404 * @obj: obj in question
1405 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001406 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01001407 * relinquish ownership of the pages back to the system.
1408 *
1409 * It is vital that we remove the page mapping if we have mapped a tiled
1410 * object through the GTT and then lose the fence register due to
1411 * resource pressure. Similarly if the object has been moved out of the
1412 * aperture, than pages mapped into userspace must be revoked. Removing the
1413 * mapping will then trigger a page fault on the next user access, allowing
1414 * fixup by i915_gem_fault().
1415 */
Eric Anholtd05ca302009-07-10 13:02:26 -07001416void
Chris Wilson05394f32010-11-08 19:18:58 +00001417i915_gem_release_mmap(struct drm_i915_gem_object *obj)
Chris Wilson901782b2009-07-10 08:18:50 +01001418{
Chris Wilson05394f32010-11-08 19:18:58 +00001419 struct drm_device *dev = obj->base.dev;
Daniel Vetterfb7d5162010-10-01 22:05:20 +02001420 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson901782b2009-07-10 08:18:50 +01001421
Chris Wilson05394f32010-11-08 19:18:58 +00001422 if (unlikely(obj->base.map_list.map && dev->dev_mapping))
Chris Wilson901782b2009-07-10 08:18:50 +01001423 unmap_mapping_range(dev->dev_mapping,
Chris Wilson05394f32010-11-08 19:18:58 +00001424 (loff_t)obj->base.map_list.hash.key<<PAGE_SHIFT,
1425 obj->base.size, 1);
Daniel Vetterfb7d5162010-10-01 22:05:20 +02001426
Chris Wilson05394f32010-11-08 19:18:58 +00001427 if (obj->fault_mappable) {
1428 obj->fault_mappable = false;
1429 i915_gem_info_update_mappable(dev_priv, obj, false);
Daniel Vetterfb7d5162010-10-01 22:05:20 +02001430 }
Chris Wilson901782b2009-07-10 08:18:50 +01001431}
1432
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001433static void
Chris Wilson05394f32010-11-08 19:18:58 +00001434i915_gem_free_mmap_offset(struct drm_i915_gem_object *obj)
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001435{
Chris Wilson05394f32010-11-08 19:18:58 +00001436 struct drm_device *dev = obj->base.dev;
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001437 struct drm_gem_mm *mm = dev->mm_private;
Chris Wilson05394f32010-11-08 19:18:58 +00001438 struct drm_map_list *list = &obj->base.map_list;
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001439
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001440 drm_ht_remove_item(&mm->offset_hash, &list->hash);
Chris Wilson39a01d12010-10-28 13:03:06 +01001441 drm_mm_put_block(list->file_offset_node);
1442 kfree(list->map);
1443 list->map = NULL;
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001444}
1445
Jesse Barnesde151cf2008-11-12 10:03:55 -08001446/**
1447 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1448 * @obj: object to check
1449 *
1450 * Return the required GTT alignment for an object, taking into account
Daniel Vetter5e783302010-11-14 22:32:36 +01001451 * potential fence register mapping.
Jesse Barnesde151cf2008-11-12 10:03:55 -08001452 */
1453static uint32_t
Chris Wilson05394f32010-11-08 19:18:58 +00001454i915_gem_get_gtt_alignment(struct drm_i915_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001455{
Chris Wilson05394f32010-11-08 19:18:58 +00001456 struct drm_device *dev = obj->base.dev;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001457
1458 /*
1459 * Minimum alignment is 4k (GTT page size), but might be greater
1460 * if a fence register is needed for the object.
1461 */
Chris Wilsona00b10c2010-09-24 21:15:47 +01001462 if (INTEL_INFO(dev)->gen >= 4 ||
Chris Wilson05394f32010-11-08 19:18:58 +00001463 obj->tiling_mode == I915_TILING_NONE)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001464 return 4096;
1465
1466 /*
1467 * Previous chips need to be aligned to the size of the smallest
1468 * fence register that can contain the object.
1469 */
Chris Wilson05394f32010-11-08 19:18:58 +00001470 return i915_gem_get_gtt_size(obj);
Chris Wilsona00b10c2010-09-24 21:15:47 +01001471}
1472
Daniel Vetter5e783302010-11-14 22:32:36 +01001473/**
1474 * i915_gem_get_unfenced_gtt_alignment - return required GTT alignment for an
1475 * unfenced object
1476 * @obj: object to check
1477 *
1478 * Return the required GTT alignment for an object, only taking into account
1479 * unfenced tiled surface requirements.
1480 */
1481static uint32_t
Chris Wilson05394f32010-11-08 19:18:58 +00001482i915_gem_get_unfenced_gtt_alignment(struct drm_i915_gem_object *obj)
Daniel Vetter5e783302010-11-14 22:32:36 +01001483{
Chris Wilson05394f32010-11-08 19:18:58 +00001484 struct drm_device *dev = obj->base.dev;
Daniel Vetter5e783302010-11-14 22:32:36 +01001485 int tile_height;
1486
1487 /*
1488 * Minimum alignment is 4k (GTT page size) for sane hw.
1489 */
1490 if (INTEL_INFO(dev)->gen >= 4 || IS_G33(dev) ||
Chris Wilson05394f32010-11-08 19:18:58 +00001491 obj->tiling_mode == I915_TILING_NONE)
Daniel Vetter5e783302010-11-14 22:32:36 +01001492 return 4096;
1493
1494 /*
1495 * Older chips need unfenced tiled buffers to be aligned to the left
1496 * edge of an even tile row (where tile rows are counted as if the bo is
1497 * placed in a fenced gtt region).
1498 */
1499 if (IS_GEN2(dev) ||
Chris Wilson05394f32010-11-08 19:18:58 +00001500 (obj->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev)))
Daniel Vetter5e783302010-11-14 22:32:36 +01001501 tile_height = 32;
1502 else
1503 tile_height = 8;
1504
Chris Wilson05394f32010-11-08 19:18:58 +00001505 return tile_height * obj->stride * 2;
Daniel Vetter5e783302010-11-14 22:32:36 +01001506}
1507
Chris Wilsona00b10c2010-09-24 21:15:47 +01001508static uint32_t
Chris Wilson05394f32010-11-08 19:18:58 +00001509i915_gem_get_gtt_size(struct drm_i915_gem_object *obj)
Chris Wilsona00b10c2010-09-24 21:15:47 +01001510{
Chris Wilson05394f32010-11-08 19:18:58 +00001511 struct drm_device *dev = obj->base.dev;
Chris Wilsona00b10c2010-09-24 21:15:47 +01001512 uint32_t size;
1513
1514 /*
1515 * Minimum alignment is 4k (GTT page size), but might be greater
1516 * if a fence register is needed for the object.
1517 */
1518 if (INTEL_INFO(dev)->gen >= 4)
Chris Wilson05394f32010-11-08 19:18:58 +00001519 return obj->base.size;
Chris Wilsona00b10c2010-09-24 21:15:47 +01001520
1521 /*
1522 * Previous chips need to be aligned to the size of the smallest
1523 * fence register that can contain the object.
1524 */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001525 if (INTEL_INFO(dev)->gen == 3)
Chris Wilsona00b10c2010-09-24 21:15:47 +01001526 size = 1024*1024;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001527 else
Chris Wilsona00b10c2010-09-24 21:15:47 +01001528 size = 512*1024;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001529
Chris Wilson05394f32010-11-08 19:18:58 +00001530 while (size < obj->base.size)
Chris Wilsona00b10c2010-09-24 21:15:47 +01001531 size <<= 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001532
Chris Wilsona00b10c2010-09-24 21:15:47 +01001533 return size;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001534}
1535
1536/**
1537 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1538 * @dev: DRM device
1539 * @data: GTT mapping ioctl data
Chris Wilson05394f32010-11-08 19:18:58 +00001540 * @file: GEM object info
Jesse Barnesde151cf2008-11-12 10:03:55 -08001541 *
1542 * Simply returns the fake offset to userspace so it can mmap it.
1543 * The mmap call will end up in drm_gem_mmap(), which will set things
1544 * up so we can get faults in the handler above.
1545 *
1546 * The fault handler will take care of binding the object into the GTT
1547 * (since it may have been evicted to make room for something), allocating
1548 * a fence register, and mapping the appropriate aperture address into
1549 * userspace.
1550 */
1551int
1552i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001553 struct drm_file *file)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001554{
Chris Wilsonda761a62010-10-27 17:37:08 +01001555 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001556 struct drm_i915_gem_mmap_gtt *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001557 struct drm_i915_gem_object *obj;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001558 int ret;
1559
1560 if (!(dev->driver->driver_features & DRIVER_GEM))
1561 return -ENODEV;
1562
Chris Wilson76c1dec2010-09-25 11:22:51 +01001563 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001564 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001565 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001566
Chris Wilson05394f32010-11-08 19:18:58 +00001567 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001568 if (obj == NULL) {
1569 ret = -ENOENT;
1570 goto unlock;
1571 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001572
Chris Wilson05394f32010-11-08 19:18:58 +00001573 if (obj->base.size > dev_priv->mm.gtt_mappable_end) {
Chris Wilsonda761a62010-10-27 17:37:08 +01001574 ret = -E2BIG;
1575 goto unlock;
1576 }
1577
Chris Wilson05394f32010-11-08 19:18:58 +00001578 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilsonab182822009-09-22 18:46:17 +01001579 DRM_ERROR("Attempting to mmap a purgeable buffer\n");
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001580 ret = -EINVAL;
1581 goto out;
Chris Wilsonab182822009-09-22 18:46:17 +01001582 }
1583
Chris Wilson05394f32010-11-08 19:18:58 +00001584 if (!obj->base.map_list.map) {
Jesse Barnesde151cf2008-11-12 10:03:55 -08001585 ret = i915_gem_create_mmap_offset(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001586 if (ret)
1587 goto out;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001588 }
1589
Chris Wilson05394f32010-11-08 19:18:58 +00001590 args->offset = (u64)obj->base.map_list.hash.key << PAGE_SHIFT;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001591
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001592out:
Chris Wilson05394f32010-11-08 19:18:58 +00001593 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001594unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001595 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001596 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001597}
1598
Chris Wilsone5281cc2010-10-28 13:45:36 +01001599static int
Chris Wilson05394f32010-11-08 19:18:58 +00001600i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj,
Chris Wilsone5281cc2010-10-28 13:45:36 +01001601 gfp_t gfpmask)
1602{
Chris Wilsone5281cc2010-10-28 13:45:36 +01001603 int page_count, i;
1604 struct address_space *mapping;
1605 struct inode *inode;
1606 struct page *page;
1607
1608 /* Get the list of pages out of our struct file. They'll be pinned
1609 * at this point until we release them.
1610 */
Chris Wilson05394f32010-11-08 19:18:58 +00001611 page_count = obj->base.size / PAGE_SIZE;
1612 BUG_ON(obj->pages != NULL);
1613 obj->pages = drm_malloc_ab(page_count, sizeof(struct page *));
1614 if (obj->pages == NULL)
Chris Wilsone5281cc2010-10-28 13:45:36 +01001615 return -ENOMEM;
1616
Chris Wilson05394f32010-11-08 19:18:58 +00001617 inode = obj->base.filp->f_path.dentry->d_inode;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001618 mapping = inode->i_mapping;
1619 for (i = 0; i < page_count; i++) {
1620 page = read_cache_page_gfp(mapping, i,
1621 GFP_HIGHUSER |
1622 __GFP_COLD |
1623 __GFP_RECLAIMABLE |
1624 gfpmask);
1625 if (IS_ERR(page))
1626 goto err_pages;
1627
Chris Wilson05394f32010-11-08 19:18:58 +00001628 obj->pages[i] = page;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001629 }
1630
Chris Wilson05394f32010-11-08 19:18:58 +00001631 if (obj->tiling_mode != I915_TILING_NONE)
Chris Wilsone5281cc2010-10-28 13:45:36 +01001632 i915_gem_object_do_bit_17_swizzle(obj);
1633
1634 return 0;
1635
1636err_pages:
1637 while (i--)
Chris Wilson05394f32010-11-08 19:18:58 +00001638 page_cache_release(obj->pages[i]);
Chris Wilsone5281cc2010-10-28 13:45:36 +01001639
Chris Wilson05394f32010-11-08 19:18:58 +00001640 drm_free_large(obj->pages);
1641 obj->pages = NULL;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001642 return PTR_ERR(page);
1643}
1644
Chris Wilson5cdf5882010-09-27 15:51:07 +01001645static void
Chris Wilson05394f32010-11-08 19:18:58 +00001646i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07001647{
Chris Wilson05394f32010-11-08 19:18:58 +00001648 int page_count = obj->base.size / PAGE_SIZE;
Eric Anholt673a3942008-07-30 12:06:12 -07001649 int i;
1650
Chris Wilson05394f32010-11-08 19:18:58 +00001651 BUG_ON(obj->madv == __I915_MADV_PURGED);
Eric Anholt856fa192009-03-19 14:10:50 -07001652
Chris Wilson05394f32010-11-08 19:18:58 +00001653 if (obj->tiling_mode != I915_TILING_NONE)
Eric Anholt280b7132009-03-12 16:56:27 -07001654 i915_gem_object_save_bit_17_swizzle(obj);
1655
Chris Wilson05394f32010-11-08 19:18:58 +00001656 if (obj->madv == I915_MADV_DONTNEED)
1657 obj->dirty = 0;
Chris Wilson3ef94da2009-09-14 16:50:29 +01001658
1659 for (i = 0; i < page_count; i++) {
Chris Wilson05394f32010-11-08 19:18:58 +00001660 if (obj->dirty)
1661 set_page_dirty(obj->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001662
Chris Wilson05394f32010-11-08 19:18:58 +00001663 if (obj->madv == I915_MADV_WILLNEED)
1664 mark_page_accessed(obj->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001665
Chris Wilson05394f32010-11-08 19:18:58 +00001666 page_cache_release(obj->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001667 }
Chris Wilson05394f32010-11-08 19:18:58 +00001668 obj->dirty = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001669
Chris Wilson05394f32010-11-08 19:18:58 +00001670 drm_free_large(obj->pages);
1671 obj->pages = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001672}
1673
Chris Wilsona56ba562010-09-28 10:07:56 +01001674static uint32_t
1675i915_gem_next_request_seqno(struct drm_device *dev,
1676 struct intel_ring_buffer *ring)
1677{
1678 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson5d97eb62010-11-10 20:40:02 +00001679 return ring->outstanding_lazy_request = dev_priv->next_seqno;
Chris Wilsona56ba562010-09-28 10:07:56 +01001680}
1681
Eric Anholt673a3942008-07-30 12:06:12 -07001682static void
Chris Wilson05394f32010-11-08 19:18:58 +00001683i915_gem_object_move_to_active(struct drm_i915_gem_object *obj,
Zou Nan hai852835f2010-05-21 09:08:56 +08001684 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001685{
Chris Wilson05394f32010-11-08 19:18:58 +00001686 struct drm_device *dev = obj->base.dev;
Chris Wilson69dc4982010-10-19 10:36:51 +01001687 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona56ba562010-09-28 10:07:56 +01001688 uint32_t seqno = i915_gem_next_request_seqno(dev, ring);
Daniel Vetter617dbe22010-02-11 22:16:02 +01001689
Zou Nan hai852835f2010-05-21 09:08:56 +08001690 BUG_ON(ring == NULL);
Chris Wilson05394f32010-11-08 19:18:58 +00001691 obj->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001692
1693 /* Add a reference if we're newly entering the active list. */
Chris Wilson05394f32010-11-08 19:18:58 +00001694 if (!obj->active) {
1695 drm_gem_object_reference(&obj->base);
1696 obj->active = 1;
Eric Anholt673a3942008-07-30 12:06:12 -07001697 }
Daniel Vettere35a41d2010-02-11 22:13:59 +01001698
Eric Anholt673a3942008-07-30 12:06:12 -07001699 /* Move from whatever list we were on to the tail of execution. */
Chris Wilson05394f32010-11-08 19:18:58 +00001700 list_move_tail(&obj->mm_list, &dev_priv->mm.active_list);
1701 list_move_tail(&obj->ring_list, &ring->active_list);
1702 obj->last_rendering_seqno = seqno;
Eric Anholt673a3942008-07-30 12:06:12 -07001703}
1704
Eric Anholtce44b0e2008-11-06 16:00:31 -08001705static void
Chris Wilson05394f32010-11-08 19:18:58 +00001706i915_gem_object_move_to_flushing(struct drm_i915_gem_object *obj)
Eric Anholtce44b0e2008-11-06 16:00:31 -08001707{
Chris Wilson05394f32010-11-08 19:18:58 +00001708 struct drm_device *dev = obj->base.dev;
Eric Anholtce44b0e2008-11-06 16:00:31 -08001709 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtce44b0e2008-11-06 16:00:31 -08001710
Chris Wilson05394f32010-11-08 19:18:58 +00001711 BUG_ON(!obj->active);
1712 list_move_tail(&obj->mm_list, &dev_priv->mm.flushing_list);
1713 list_del_init(&obj->ring_list);
1714 obj->last_rendering_seqno = 0;
Eric Anholtce44b0e2008-11-06 16:00:31 -08001715}
Eric Anholt673a3942008-07-30 12:06:12 -07001716
Chris Wilson963b4832009-09-20 23:03:54 +01001717/* Immediately discard the backing storage */
1718static void
Chris Wilson05394f32010-11-08 19:18:58 +00001719i915_gem_object_truncate(struct drm_i915_gem_object *obj)
Chris Wilson963b4832009-09-20 23:03:54 +01001720{
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001721 struct inode *inode;
Chris Wilson963b4832009-09-20 23:03:54 +01001722
Chris Wilsonae9fed62010-08-07 11:01:30 +01001723 /* Our goal here is to return as much of the memory as
1724 * is possible back to the system as we are called from OOM.
1725 * To do this we must instruct the shmfs to drop all of its
1726 * backing pages, *now*. Here we mirror the actions taken
1727 * when by shmem_delete_inode() to release the backing store.
1728 */
Chris Wilson05394f32010-11-08 19:18:58 +00001729 inode = obj->base.filp->f_path.dentry->d_inode;
Chris Wilsonae9fed62010-08-07 11:01:30 +01001730 truncate_inode_pages(inode->i_mapping, 0);
1731 if (inode->i_op->truncate_range)
1732 inode->i_op->truncate_range(inode, 0, (loff_t)-1);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001733
Chris Wilson05394f32010-11-08 19:18:58 +00001734 obj->madv = __I915_MADV_PURGED;
Chris Wilson963b4832009-09-20 23:03:54 +01001735}
1736
1737static inline int
Chris Wilson05394f32010-11-08 19:18:58 +00001738i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj)
Chris Wilson963b4832009-09-20 23:03:54 +01001739{
Chris Wilson05394f32010-11-08 19:18:58 +00001740 return obj->madv == I915_MADV_DONTNEED;
Chris Wilson963b4832009-09-20 23:03:54 +01001741}
1742
Eric Anholt673a3942008-07-30 12:06:12 -07001743static void
Chris Wilson05394f32010-11-08 19:18:58 +00001744i915_gem_object_move_to_inactive(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07001745{
Chris Wilson05394f32010-11-08 19:18:58 +00001746 struct drm_device *dev = obj->base.dev;
Eric Anholt673a3942008-07-30 12:06:12 -07001747 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07001748
Chris Wilson05394f32010-11-08 19:18:58 +00001749 if (obj->pin_count != 0)
1750 list_move_tail(&obj->mm_list, &dev_priv->mm.pinned_list);
Eric Anholt673a3942008-07-30 12:06:12 -07001751 else
Chris Wilson05394f32010-11-08 19:18:58 +00001752 list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
1753 list_del_init(&obj->ring_list);
Eric Anholt673a3942008-07-30 12:06:12 -07001754
Chris Wilson05394f32010-11-08 19:18:58 +00001755 BUG_ON(!list_empty(&obj->gpu_write_list));
Daniel Vetter99fcb762010-02-07 16:20:18 +01001756
Chris Wilson05394f32010-11-08 19:18:58 +00001757 obj->last_rendering_seqno = 0;
1758 obj->ring = NULL;
1759 if (obj->active) {
1760 obj->active = 0;
1761 drm_gem_object_unreference(&obj->base);
Eric Anholt673a3942008-07-30 12:06:12 -07001762 }
Chris Wilson23bc5982010-09-29 16:10:57 +01001763 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07001764}
1765
Daniel Vetter63560392010-02-19 11:51:59 +01001766static void
1767i915_gem_process_flushing_list(struct drm_device *dev,
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001768 uint32_t flush_domains,
Zou Nan hai852835f2010-05-21 09:08:56 +08001769 struct intel_ring_buffer *ring)
Daniel Vetter63560392010-02-19 11:51:59 +01001770{
1771 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00001772 struct drm_i915_gem_object *obj, *next;
Daniel Vetter63560392010-02-19 11:51:59 +01001773
Chris Wilson05394f32010-11-08 19:18:58 +00001774 list_for_each_entry_safe(obj, next,
Chris Wilson64193402010-10-24 12:38:05 +01001775 &ring->gpu_write_list,
Daniel Vetter63560392010-02-19 11:51:59 +01001776 gpu_write_list) {
Chris Wilson05394f32010-11-08 19:18:58 +00001777 if (obj->base.write_domain & flush_domains) {
1778 uint32_t old_write_domain = obj->base.write_domain;
Daniel Vetter63560392010-02-19 11:51:59 +01001779
Chris Wilson05394f32010-11-08 19:18:58 +00001780 obj->base.write_domain = 0;
1781 list_del_init(&obj->gpu_write_list);
Daniel Vetter617dbe22010-02-11 22:16:02 +01001782 i915_gem_object_move_to_active(obj, ring);
Daniel Vetter63560392010-02-19 11:51:59 +01001783
1784 /* update the fence lru list */
Chris Wilson05394f32010-11-08 19:18:58 +00001785 if (obj->fence_reg != I915_FENCE_REG_NONE) {
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001786 struct drm_i915_fence_reg *reg =
Chris Wilson05394f32010-11-08 19:18:58 +00001787 &dev_priv->fence_regs[obj->fence_reg];
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001788 list_move_tail(&reg->lru_list,
Daniel Vetter63560392010-02-19 11:51:59 +01001789 &dev_priv->mm.fence_list);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001790 }
Daniel Vetter63560392010-02-19 11:51:59 +01001791
1792 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00001793 obj->base.read_domains,
Daniel Vetter63560392010-02-19 11:51:59 +01001794 old_write_domain);
1795 }
1796 }
1797}
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001798
Chris Wilson3cce4692010-10-27 16:11:02 +01001799int
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001800i915_add_request(struct drm_device *dev,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001801 struct drm_file *file,
Chris Wilson8dc5d142010-08-12 12:36:12 +01001802 struct drm_i915_gem_request *request,
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001803 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001804{
1805 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001806 struct drm_i915_file_private *file_priv = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001807 uint32_t seqno;
1808 int was_empty;
Chris Wilson3cce4692010-10-27 16:11:02 +01001809 int ret;
1810
1811 BUG_ON(request == NULL);
Eric Anholt673a3942008-07-30 12:06:12 -07001812
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001813 if (file != NULL)
1814 file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00001815
Chris Wilson3cce4692010-10-27 16:11:02 +01001816 ret = ring->add_request(ring, &seqno);
1817 if (ret)
1818 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001819
Chris Wilsona56ba562010-09-28 10:07:56 +01001820 ring->outstanding_lazy_request = false;
Eric Anholt673a3942008-07-30 12:06:12 -07001821
1822 request->seqno = seqno;
Zou Nan hai852835f2010-05-21 09:08:56 +08001823 request->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001824 request->emitted_jiffies = jiffies;
Zou Nan hai852835f2010-05-21 09:08:56 +08001825 was_empty = list_empty(&ring->request_list);
1826 list_add_tail(&request->list, &ring->request_list);
1827
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001828 if (file_priv) {
Chris Wilson1c255952010-09-26 11:03:27 +01001829 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001830 request->file_priv = file_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00001831 list_add_tail(&request->client_list,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001832 &file_priv->mm.request_list);
Chris Wilson1c255952010-09-26 11:03:27 +01001833 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00001834 }
Eric Anholt673a3942008-07-30 12:06:12 -07001835
Ben Gamarif65d9422009-09-14 17:48:44 -04001836 if (!dev_priv->mm.suspended) {
Chris Wilsonb3b079d2010-09-13 23:44:34 +01001837 mod_timer(&dev_priv->hangcheck_timer,
1838 jiffies + msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
Ben Gamarif65d9422009-09-14 17:48:44 -04001839 if (was_empty)
Chris Wilsonb3b079d2010-09-13 23:44:34 +01001840 queue_delayed_work(dev_priv->wq,
1841 &dev_priv->mm.retire_work, HZ);
Ben Gamarif65d9422009-09-14 17:48:44 -04001842 }
Chris Wilson3cce4692010-10-27 16:11:02 +01001843 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001844}
1845
1846/**
1847 * Command execution barrier
1848 *
1849 * Ensures that all commands in the ring are finished
1850 * before signalling the CPU
1851 */
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001852static void
Zou Nan hai852835f2010-05-21 09:08:56 +08001853i915_retire_commands(struct drm_device *dev, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001854{
Eric Anholt673a3942008-07-30 12:06:12 -07001855 uint32_t flush_domains = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001856
1857 /* The sampler always gets flushed on i965 (sigh) */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001858 if (INTEL_INFO(dev)->gen >= 4)
Eric Anholt673a3942008-07-30 12:06:12 -07001859 flush_domains |= I915_GEM_DOMAIN_SAMPLER;
Zou Nan hai852835f2010-05-21 09:08:56 +08001860
Chris Wilson78501ea2010-10-27 12:18:21 +01001861 ring->flush(ring, I915_GEM_DOMAIN_COMMAND, flush_domains);
Eric Anholt673a3942008-07-30 12:06:12 -07001862}
1863
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001864static inline void
1865i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
Eric Anholt673a3942008-07-30 12:06:12 -07001866{
Chris Wilson1c255952010-09-26 11:03:27 +01001867 struct drm_i915_file_private *file_priv = request->file_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07001868
Chris Wilson1c255952010-09-26 11:03:27 +01001869 if (!file_priv)
1870 return;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001871
Chris Wilson1c255952010-09-26 11:03:27 +01001872 spin_lock(&file_priv->mm.lock);
1873 list_del(&request->client_list);
1874 request->file_priv = NULL;
1875 spin_unlock(&file_priv->mm.lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001876}
1877
Chris Wilsondfaae392010-09-22 10:31:52 +01001878static void i915_gem_reset_ring_lists(struct drm_i915_private *dev_priv,
1879 struct intel_ring_buffer *ring)
Chris Wilson9375e442010-09-19 12:21:28 +01001880{
Chris Wilsondfaae392010-09-22 10:31:52 +01001881 while (!list_empty(&ring->request_list)) {
1882 struct drm_i915_gem_request *request;
Chris Wilson9375e442010-09-19 12:21:28 +01001883
Chris Wilsondfaae392010-09-22 10:31:52 +01001884 request = list_first_entry(&ring->request_list,
1885 struct drm_i915_gem_request,
1886 list);
1887
1888 list_del(&request->list);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001889 i915_gem_request_remove_from_client(request);
Chris Wilsondfaae392010-09-22 10:31:52 +01001890 kfree(request);
1891 }
1892
1893 while (!list_empty(&ring->active_list)) {
Chris Wilson05394f32010-11-08 19:18:58 +00001894 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001895
Chris Wilson05394f32010-11-08 19:18:58 +00001896 obj = list_first_entry(&ring->active_list,
1897 struct drm_i915_gem_object,
1898 ring_list);
Eric Anholt673a3942008-07-30 12:06:12 -07001899
Chris Wilson05394f32010-11-08 19:18:58 +00001900 obj->base.write_domain = 0;
1901 list_del_init(&obj->gpu_write_list);
1902 i915_gem_object_move_to_inactive(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001903 }
Eric Anholt673a3942008-07-30 12:06:12 -07001904}
1905
Chris Wilson069efc12010-09-30 16:53:18 +01001906void i915_gem_reset(struct drm_device *dev)
Eric Anholt673a3942008-07-30 12:06:12 -07001907{
Chris Wilsondfaae392010-09-22 10:31:52 +01001908 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00001909 struct drm_i915_gem_object *obj;
Chris Wilson069efc12010-09-30 16:53:18 +01001910 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07001911
Chris Wilsondfaae392010-09-22 10:31:52 +01001912 i915_gem_reset_ring_lists(dev_priv, &dev_priv->render_ring);
Chris Wilson87acb0a2010-10-19 10:13:00 +01001913 i915_gem_reset_ring_lists(dev_priv, &dev_priv->bsd_ring);
Chris Wilson549f7362010-10-19 11:19:32 +01001914 i915_gem_reset_ring_lists(dev_priv, &dev_priv->blt_ring);
Chris Wilsondfaae392010-09-22 10:31:52 +01001915
1916 /* Remove anything from the flushing lists. The GPU cache is likely
1917 * to be lost on reset along with the data, so simply move the
1918 * lost bo to the inactive list.
1919 */
1920 while (!list_empty(&dev_priv->mm.flushing_list)) {
Chris Wilson05394f32010-11-08 19:18:58 +00001921 obj= list_first_entry(&dev_priv->mm.flushing_list,
1922 struct drm_i915_gem_object,
1923 mm_list);
Chris Wilson9375e442010-09-19 12:21:28 +01001924
Chris Wilson05394f32010-11-08 19:18:58 +00001925 obj->base.write_domain = 0;
1926 list_del_init(&obj->gpu_write_list);
1927 i915_gem_object_move_to_inactive(obj);
Chris Wilson9375e442010-09-19 12:21:28 +01001928 }
Chris Wilson9375e442010-09-19 12:21:28 +01001929
Chris Wilsondfaae392010-09-22 10:31:52 +01001930 /* Move everything out of the GPU domains to ensure we do any
1931 * necessary invalidation upon reuse.
1932 */
Chris Wilson05394f32010-11-08 19:18:58 +00001933 list_for_each_entry(obj,
Chris Wilson77f01232010-09-19 12:31:36 +01001934 &dev_priv->mm.inactive_list,
Chris Wilson69dc4982010-10-19 10:36:51 +01001935 mm_list)
Chris Wilson77f01232010-09-19 12:31:36 +01001936 {
Chris Wilson05394f32010-11-08 19:18:58 +00001937 obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
Chris Wilson77f01232010-09-19 12:31:36 +01001938 }
Chris Wilson069efc12010-09-30 16:53:18 +01001939
1940 /* The fence registers are invalidated so clear them out */
1941 for (i = 0; i < 16; i++) {
1942 struct drm_i915_fence_reg *reg;
1943
1944 reg = &dev_priv->fence_regs[i];
1945 if (!reg->obj)
1946 continue;
1947
1948 i915_gem_clear_fence_reg(reg->obj);
1949 }
Eric Anholt673a3942008-07-30 12:06:12 -07001950}
1951
1952/**
1953 * This function clears the request list as sequence numbers are passed.
1954 */
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001955static void
1956i915_gem_retire_requests_ring(struct drm_device *dev,
1957 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001958{
1959 drm_i915_private_t *dev_priv = dev->dev_private;
1960 uint32_t seqno;
1961
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001962 if (!ring->status_page.page_addr ||
1963 list_empty(&ring->request_list))
Karsten Wiese6c0594a2009-02-23 15:07:57 +01001964 return;
1965
Chris Wilson23bc5982010-09-29 16:10:57 +01001966 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07001967
Chris Wilson78501ea2010-10-27 12:18:21 +01001968 seqno = ring->get_seqno(ring);
Zou Nan hai852835f2010-05-21 09:08:56 +08001969 while (!list_empty(&ring->request_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001970 struct drm_i915_gem_request *request;
Eric Anholt673a3942008-07-30 12:06:12 -07001971
Zou Nan hai852835f2010-05-21 09:08:56 +08001972 request = list_first_entry(&ring->request_list,
Eric Anholt673a3942008-07-30 12:06:12 -07001973 struct drm_i915_gem_request,
1974 list);
Eric Anholt673a3942008-07-30 12:06:12 -07001975
Chris Wilsondfaae392010-09-22 10:31:52 +01001976 if (!i915_seqno_passed(seqno, request->seqno))
Eric Anholt673a3942008-07-30 12:06:12 -07001977 break;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001978
1979 trace_i915_gem_request_retire(dev, request->seqno);
1980
1981 list_del(&request->list);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001982 i915_gem_request_remove_from_client(request);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001983 kfree(request);
1984 }
1985
1986 /* Move any buffers on the active list that are no longer referenced
1987 * by the ringbuffer to the flushing/inactive lists as appropriate.
1988 */
1989 while (!list_empty(&ring->active_list)) {
Chris Wilson05394f32010-11-08 19:18:58 +00001990 struct drm_i915_gem_object *obj;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001991
Chris Wilson05394f32010-11-08 19:18:58 +00001992 obj= list_first_entry(&ring->active_list,
1993 struct drm_i915_gem_object,
1994 ring_list);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001995
Chris Wilson05394f32010-11-08 19:18:58 +00001996 if (!i915_seqno_passed(seqno, obj->last_rendering_seqno))
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001997 break;
1998
Chris Wilson05394f32010-11-08 19:18:58 +00001999 if (obj->base.write_domain != 0)
Chris Wilsonb84d5f02010-09-18 01:38:04 +01002000 i915_gem_object_move_to_flushing(obj);
2001 else
2002 i915_gem_object_move_to_inactive(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002003 }
Chris Wilson9d34e5d2009-09-24 05:26:06 +01002004
2005 if (unlikely (dev_priv->trace_irq_seqno &&
2006 i915_seqno_passed(dev_priv->trace_irq_seqno, seqno))) {
Chris Wilson78501ea2010-10-27 12:18:21 +01002007 ring->user_irq_put(ring);
Chris Wilson9d34e5d2009-09-24 05:26:06 +01002008 dev_priv->trace_irq_seqno = 0;
2009 }
Chris Wilson23bc5982010-09-29 16:10:57 +01002010
2011 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07002012}
2013
2014void
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002015i915_gem_retire_requests(struct drm_device *dev)
2016{
2017 drm_i915_private_t *dev_priv = dev->dev_private;
2018
Chris Wilsonbe726152010-07-23 23:18:50 +01002019 if (!list_empty(&dev_priv->mm.deferred_free_list)) {
Chris Wilson05394f32010-11-08 19:18:58 +00002020 struct drm_i915_gem_object *obj, *next;
Chris Wilsonbe726152010-07-23 23:18:50 +01002021
2022 /* We must be careful that during unbind() we do not
2023 * accidentally infinitely recurse into retire requests.
2024 * Currently:
2025 * retire -> free -> unbind -> wait -> retire_ring
2026 */
Chris Wilson05394f32010-11-08 19:18:58 +00002027 list_for_each_entry_safe(obj, next,
Chris Wilsonbe726152010-07-23 23:18:50 +01002028 &dev_priv->mm.deferred_free_list,
Chris Wilson69dc4982010-10-19 10:36:51 +01002029 mm_list)
Chris Wilson05394f32010-11-08 19:18:58 +00002030 i915_gem_free_object_tail(obj);
Chris Wilsonbe726152010-07-23 23:18:50 +01002031 }
2032
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002033 i915_gem_retire_requests_ring(dev, &dev_priv->render_ring);
Chris Wilson87acb0a2010-10-19 10:13:00 +01002034 i915_gem_retire_requests_ring(dev, &dev_priv->bsd_ring);
Chris Wilson549f7362010-10-19 11:19:32 +01002035 i915_gem_retire_requests_ring(dev, &dev_priv->blt_ring);
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002036}
2037
Daniel Vetter75ef9da2010-08-21 00:25:16 +02002038static void
Eric Anholt673a3942008-07-30 12:06:12 -07002039i915_gem_retire_work_handler(struct work_struct *work)
2040{
2041 drm_i915_private_t *dev_priv;
2042 struct drm_device *dev;
2043
2044 dev_priv = container_of(work, drm_i915_private_t,
2045 mm.retire_work.work);
2046 dev = dev_priv->dev;
2047
Chris Wilson891b48c2010-09-29 12:26:37 +01002048 /* Come back later if the device is busy... */
2049 if (!mutex_trylock(&dev->struct_mutex)) {
2050 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
2051 return;
2052 }
2053
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002054 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08002055
Keith Packard6dbe2772008-10-14 21:41:13 -07002056 if (!dev_priv->mm.suspended &&
Zou Nan haid1b851f2010-05-21 09:08:57 +08002057 (!list_empty(&dev_priv->render_ring.request_list) ||
Chris Wilson549f7362010-10-19 11:19:32 +01002058 !list_empty(&dev_priv->bsd_ring.request_list) ||
2059 !list_empty(&dev_priv->blt_ring.request_list)))
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07002060 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
Eric Anholt673a3942008-07-30 12:06:12 -07002061 mutex_unlock(&dev->struct_mutex);
2062}
2063
Daniel Vetter5a5a0c62009-09-15 22:57:36 +02002064int
Zou Nan hai852835f2010-05-21 09:08:56 +08002065i915_do_wait_request(struct drm_device *dev, uint32_t seqno,
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002066 bool interruptible, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07002067{
2068 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes802c7eb2009-05-05 16:03:48 -07002069 u32 ier;
Eric Anholt673a3942008-07-30 12:06:12 -07002070 int ret = 0;
2071
2072 BUG_ON(seqno == 0);
2073
Ben Gamariba1234d2009-09-14 17:48:47 -04002074 if (atomic_read(&dev_priv->mm.wedged))
Chris Wilson30dbf0c2010-09-25 10:19:17 +01002075 return -EAGAIN;
Ben Gamariffed1d02009-09-14 17:48:41 -04002076
Chris Wilson5d97eb62010-11-10 20:40:02 +00002077 if (seqno == ring->outstanding_lazy_request) {
Chris Wilson3cce4692010-10-27 16:11:02 +01002078 struct drm_i915_gem_request *request;
2079
2080 request = kzalloc(sizeof(*request), GFP_KERNEL);
2081 if (request == NULL)
Daniel Vettere35a41d2010-02-11 22:13:59 +01002082 return -ENOMEM;
Chris Wilson3cce4692010-10-27 16:11:02 +01002083
2084 ret = i915_add_request(dev, NULL, request, ring);
2085 if (ret) {
2086 kfree(request);
2087 return ret;
2088 }
2089
2090 seqno = request->seqno;
Daniel Vettere35a41d2010-02-11 22:13:59 +01002091 }
2092
Chris Wilson78501ea2010-10-27 12:18:21 +01002093 if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) {
Eric Anholtbad720f2009-10-22 16:11:14 -07002094 if (HAS_PCH_SPLIT(dev))
Zhenyu Wang036a4a72009-06-08 14:40:19 +08002095 ier = I915_READ(DEIER) | I915_READ(GTIER);
2096 else
2097 ier = I915_READ(IER);
Jesse Barnes802c7eb2009-05-05 16:03:48 -07002098 if (!ier) {
2099 DRM_ERROR("something (likely vbetool) disabled "
2100 "interrupts, re-enabling\n");
2101 i915_driver_irq_preinstall(dev);
2102 i915_driver_irq_postinstall(dev);
2103 }
2104
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002105 trace_i915_gem_request_wait_begin(dev, seqno);
2106
Chris Wilsonb2223492010-10-27 15:27:33 +01002107 ring->waiting_seqno = seqno;
Chris Wilson78501ea2010-10-27 12:18:21 +01002108 ring->user_irq_get(ring);
Daniel Vetter48764bf2009-09-15 22:57:32 +02002109 if (interruptible)
Zou Nan hai852835f2010-05-21 09:08:56 +08002110 ret = wait_event_interruptible(ring->irq_queue,
Chris Wilson78501ea2010-10-27 12:18:21 +01002111 i915_seqno_passed(ring->get_seqno(ring), seqno)
Zou Nan hai852835f2010-05-21 09:08:56 +08002112 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02002113 else
Zou Nan hai852835f2010-05-21 09:08:56 +08002114 wait_event(ring->irq_queue,
Chris Wilson78501ea2010-10-27 12:18:21 +01002115 i915_seqno_passed(ring->get_seqno(ring), seqno)
Zou Nan hai852835f2010-05-21 09:08:56 +08002116 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02002117
Chris Wilson78501ea2010-10-27 12:18:21 +01002118 ring->user_irq_put(ring);
Chris Wilsonb2223492010-10-27 15:27:33 +01002119 ring->waiting_seqno = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002120
2121 trace_i915_gem_request_wait_end(dev, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07002122 }
Ben Gamariba1234d2009-09-14 17:48:47 -04002123 if (atomic_read(&dev_priv->mm.wedged))
Chris Wilson30dbf0c2010-09-25 10:19:17 +01002124 ret = -EAGAIN;
Eric Anholt673a3942008-07-30 12:06:12 -07002125
2126 if (ret && ret != -ERESTARTSYS)
Daniel Vetter8bff9172010-02-11 22:19:40 +01002127 DRM_ERROR("%s returns %d (awaiting %d at %d, next %d)\n",
Chris Wilson78501ea2010-10-27 12:18:21 +01002128 __func__, ret, seqno, ring->get_seqno(ring),
Daniel Vetter8bff9172010-02-11 22:19:40 +01002129 dev_priv->next_seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07002130
2131 /* Directly dispatch request retiring. While we have the work queue
2132 * to handle this, the waiter on a request often wants an associated
2133 * buffer to have made it to the inactive list, and we would need
2134 * a separate wait queue to handle that.
2135 */
2136 if (ret == 0)
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002137 i915_gem_retire_requests_ring(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07002138
2139 return ret;
2140}
2141
Daniel Vetter48764bf2009-09-15 22:57:32 +02002142/**
2143 * Waits for a sequence number to be signaled, and cleans up the
2144 * request and object lists appropriately for that event.
2145 */
2146static int
Zou Nan hai852835f2010-05-21 09:08:56 +08002147i915_wait_request(struct drm_device *dev, uint32_t seqno,
Chris Wilsona56ba562010-09-28 10:07:56 +01002148 struct intel_ring_buffer *ring)
Daniel Vetter48764bf2009-09-15 22:57:32 +02002149{
Zou Nan hai852835f2010-05-21 09:08:56 +08002150 return i915_do_wait_request(dev, seqno, 1, ring);
Daniel Vetter48764bf2009-09-15 22:57:32 +02002151}
2152
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002153static void
Chris Wilson92204342010-09-18 11:02:01 +01002154i915_gem_flush_ring(struct drm_device *dev,
2155 struct intel_ring_buffer *ring,
2156 uint32_t invalidate_domains,
2157 uint32_t flush_domains)
2158{
Chris Wilson78501ea2010-10-27 12:18:21 +01002159 ring->flush(ring, invalidate_domains, flush_domains);
Chris Wilson92204342010-09-18 11:02:01 +01002160 i915_gem_process_flushing_list(dev, flush_domains, ring);
2161}
2162
2163static void
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002164i915_gem_flush(struct drm_device *dev,
2165 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)
Daniel Vetter40ce6572010-11-05 18:12:18 +01002172 intel_gtt_chipset_flush();
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 Wilson05394f32010-11-08 19:18:58 +00002176 i915_gem_flush_ring(dev, &dev_priv->render_ring,
Chris Wilson92204342010-09-18 11:02:01 +01002177 invalidate_domains, flush_domains);
2178 if (flush_rings & RING_BSD)
Chris Wilson05394f32010-11-08 19:18:58 +00002179 i915_gem_flush_ring(dev, &dev_priv->bsd_ring,
Chris Wilson92204342010-09-18 11:02:01 +01002180 invalidate_domains, flush_domains);
Chris Wilson549f7362010-10-19 11:19:32 +01002181 if (flush_rings & RING_BLT)
Chris Wilson05394f32010-11-08 19:18:58 +00002182 i915_gem_flush_ring(dev, &dev_priv->blt_ring,
Chris Wilson549f7362010-10-19 11:19:32 +01002183 invalidate_domains, flush_domains);
Chris Wilson92204342010-09-18 11:02:01 +01002184 }
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002185}
2186
Eric Anholt673a3942008-07-30 12:06:12 -07002187/**
2188 * Ensures that all rendering to the object has completed and the object is
2189 * safe to unbind from the GTT or access from the CPU.
2190 */
2191static int
Chris Wilson05394f32010-11-08 19:18:58 +00002192i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj,
Chris Wilson2cf34d72010-09-14 13:03:28 +01002193 bool interruptible)
Eric Anholt673a3942008-07-30 12:06:12 -07002194{
Chris Wilson05394f32010-11-08 19:18:58 +00002195 struct drm_device *dev = obj->base.dev;
Eric Anholt673a3942008-07-30 12:06:12 -07002196 int ret;
2197
Eric Anholte47c68e2008-11-14 13:35:19 -08002198 /* This function only exists to support waiting for existing rendering,
2199 * not for emitting required flushes.
Eric Anholt673a3942008-07-30 12:06:12 -07002200 */
Chris Wilson05394f32010-11-08 19:18:58 +00002201 BUG_ON((obj->base.write_domain & I915_GEM_GPU_DOMAINS) != 0);
Eric Anholt673a3942008-07-30 12:06:12 -07002202
2203 /* If there is rendering queued on the buffer being evicted, wait for
2204 * it.
2205 */
Chris Wilson05394f32010-11-08 19:18:58 +00002206 if (obj->active) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01002207 ret = i915_do_wait_request(dev,
Chris Wilson05394f32010-11-08 19:18:58 +00002208 obj->last_rendering_seqno,
Chris Wilson2cf34d72010-09-14 13:03:28 +01002209 interruptible,
Chris Wilson05394f32010-11-08 19:18:58 +00002210 obj->ring);
Chris Wilson2cf34d72010-09-14 13:03:28 +01002211 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002212 return ret;
2213 }
2214
2215 return 0;
2216}
2217
2218/**
2219 * Unbinds an object from the GTT aperture.
2220 */
Jesse Barnes0f973f22009-01-26 17:10:45 -08002221int
Chris Wilson05394f32010-11-08 19:18:58 +00002222i915_gem_object_unbind(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002223{
Chris Wilson05394f32010-11-08 19:18:58 +00002224 struct drm_device *dev = obj->base.dev;
Chris Wilson73aa8082010-09-30 11:46:12 +01002225 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07002226 int ret = 0;
2227
Chris Wilson05394f32010-11-08 19:18:58 +00002228 if (obj->gtt_space == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002229 return 0;
2230
Chris Wilson05394f32010-11-08 19:18:58 +00002231 if (obj->pin_count != 0) {
Eric Anholt673a3942008-07-30 12:06:12 -07002232 DRM_ERROR("Attempting to unbind pinned buffer\n");
2233 return -EINVAL;
2234 }
2235
Eric Anholt5323fd02009-09-09 11:50:45 -07002236 /* blow away mappings if mapped through GTT */
2237 i915_gem_release_mmap(obj);
2238
Eric Anholt673a3942008-07-30 12:06:12 -07002239 /* Move the object to the CPU domain to ensure that
2240 * any possible CPU writes while it's not in the GTT
2241 * are flushed when we go to remap it. This will
2242 * also ensure that all pending GPU writes are finished
2243 * before we unbind.
2244 */
Eric Anholte47c68e2008-11-14 13:35:19 -08002245 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Chris Wilson8dc17752010-07-23 23:18:51 +01002246 if (ret == -ERESTARTSYS)
Eric Anholt673a3942008-07-30 12:06:12 -07002247 return ret;
Chris Wilson8dc17752010-07-23 23:18:51 +01002248 /* Continue on if we fail due to EIO, the GPU is hung so we
2249 * should be safe and we need to cleanup or else we might
2250 * cause memory corruption through use-after-free.
2251 */
Chris Wilson812ed4922010-09-30 15:08:57 +01002252 if (ret) {
2253 i915_gem_clflush_object(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00002254 obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
Chris Wilson812ed4922010-09-30 15:08:57 +01002255 }
Eric Anholt673a3942008-07-30 12:06:12 -07002256
Daniel Vetter96b47b62009-12-15 17:50:00 +01002257 /* release the fence reg _after_ flushing */
Chris Wilson05394f32010-11-08 19:18:58 +00002258 if (obj->fence_reg != I915_FENCE_REG_NONE)
Daniel Vetter96b47b62009-12-15 17:50:00 +01002259 i915_gem_clear_fence_reg(obj);
2260
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002261 i915_gem_gtt_unbind_object(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002262
Chris Wilsone5281cc2010-10-28 13:45:36 +01002263 i915_gem_object_put_pages_gtt(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002264
Chris Wilson05394f32010-11-08 19:18:58 +00002265 i915_gem_info_remove_gtt(dev_priv, obj);
2266 list_del_init(&obj->mm_list);
Daniel Vetter75e9e912010-11-04 17:11:09 +01002267 /* Avoid an unnecessary call to unbind on rebind. */
Chris Wilson05394f32010-11-08 19:18:58 +00002268 obj->map_and_fenceable = true;
Eric Anholt673a3942008-07-30 12:06:12 -07002269
Chris Wilson05394f32010-11-08 19:18:58 +00002270 drm_mm_put_block(obj->gtt_space);
2271 obj->gtt_space = NULL;
2272 obj->gtt_offset = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002273
Chris Wilson05394f32010-11-08 19:18:58 +00002274 if (i915_gem_object_is_purgeable(obj))
Chris Wilson963b4832009-09-20 23:03:54 +01002275 i915_gem_object_truncate(obj);
2276
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002277 trace_i915_gem_object_unbind(obj);
2278
Chris Wilson8dc17752010-07-23 23:18:51 +01002279 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002280}
2281
Chris Wilsona56ba562010-09-28 10:07:56 +01002282static int i915_ring_idle(struct drm_device *dev,
2283 struct intel_ring_buffer *ring)
2284{
Chris Wilson395b70b2010-10-28 21:28:46 +01002285 if (list_empty(&ring->gpu_write_list) && list_empty(&ring->active_list))
Chris Wilson64193402010-10-24 12:38:05 +01002286 return 0;
2287
Chris Wilson05394f32010-11-08 19:18:58 +00002288 i915_gem_flush_ring(dev, ring,
Chris Wilsona56ba562010-09-28 10:07:56 +01002289 I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
2290 return i915_wait_request(dev,
2291 i915_gem_next_request_seqno(dev, ring),
2292 ring);
2293}
2294
Chris Wilsonb47eb4a2010-08-07 11:01:23 +01002295int
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002296i915_gpu_idle(struct drm_device *dev)
2297{
2298 drm_i915_private_t *dev_priv = dev->dev_private;
2299 bool lists_empty;
Zou Nan hai852835f2010-05-21 09:08:56 +08002300 int ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002301
Zou Nan haid1b851f2010-05-21 09:08:57 +08002302 lists_empty = (list_empty(&dev_priv->mm.flushing_list) &&
Chris Wilson395b70b2010-10-28 21:28:46 +01002303 list_empty(&dev_priv->mm.active_list));
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002304 if (lists_empty)
2305 return 0;
2306
2307 /* Flush everything onto the inactive list. */
Chris Wilsona56ba562010-09-28 10:07:56 +01002308 ret = i915_ring_idle(dev, &dev_priv->render_ring);
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002309 if (ret)
2310 return ret;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002311
Chris Wilson87acb0a2010-10-19 10:13:00 +01002312 ret = i915_ring_idle(dev, &dev_priv->bsd_ring);
2313 if (ret)
2314 return ret;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002315
Chris Wilson549f7362010-10-19 11:19:32 +01002316 ret = i915_ring_idle(dev, &dev_priv->blt_ring);
2317 if (ret)
2318 return ret;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002319
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002320 return 0;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002321}
2322
Chris Wilson05394f32010-11-08 19:18:58 +00002323static void sandybridge_write_fence_reg(struct drm_i915_gem_object *obj)
Eric Anholt4e901fd2009-10-26 16:44:17 -07002324{
Chris Wilson05394f32010-11-08 19:18:58 +00002325 struct drm_device *dev = obj->base.dev;
Eric Anholt4e901fd2009-10-26 16:44:17 -07002326 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00002327 u32 size = obj->gtt_space->size;
2328 int regnum = obj->fence_reg;
Eric Anholt4e901fd2009-10-26 16:44:17 -07002329 uint64_t val;
2330
Chris Wilson05394f32010-11-08 19:18:58 +00002331 val = (uint64_t)((obj->gtt_offset + size - 4096) &
Eric Anholt4e901fd2009-10-26 16:44:17 -07002332 0xfffff000) << 32;
Chris Wilson05394f32010-11-08 19:18:58 +00002333 val |= obj->gtt_offset & 0xfffff000;
2334 val |= (uint64_t)((obj->stride / 128) - 1) <<
Eric Anholt4e901fd2009-10-26 16:44:17 -07002335 SANDYBRIDGE_FENCE_PITCH_SHIFT;
2336
Chris Wilson05394f32010-11-08 19:18:58 +00002337 if (obj->tiling_mode == I915_TILING_Y)
Eric Anholt4e901fd2009-10-26 16:44:17 -07002338 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2339 val |= I965_FENCE_REG_VALID;
2340
2341 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (regnum * 8), val);
2342}
2343
Chris Wilson05394f32010-11-08 19:18:58 +00002344static void i965_write_fence_reg(struct drm_i915_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002345{
Chris Wilson05394f32010-11-08 19:18:58 +00002346 struct drm_device *dev = obj->base.dev;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002347 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00002348 u32 size = obj->gtt_space->size;
2349 int regnum = obj->fence_reg;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002350 uint64_t val;
2351
Chris Wilson05394f32010-11-08 19:18:58 +00002352 val = (uint64_t)((obj->gtt_offset + size - 4096) &
Jesse Barnesde151cf2008-11-12 10:03:55 -08002353 0xfffff000) << 32;
Chris Wilson05394f32010-11-08 19:18:58 +00002354 val |= obj->gtt_offset & 0xfffff000;
2355 val |= ((obj->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
2356 if (obj->tiling_mode == I915_TILING_Y)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002357 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2358 val |= I965_FENCE_REG_VALID;
2359
2360 I915_WRITE64(FENCE_REG_965_0 + (regnum * 8), val);
2361}
2362
Chris Wilson05394f32010-11-08 19:18:58 +00002363static void i915_write_fence_reg(struct drm_i915_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002364{
Chris Wilson05394f32010-11-08 19:18:58 +00002365 struct drm_device *dev = obj->base.dev;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002366 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00002367 u32 size = obj->gtt_space->size;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002368 uint32_t fence_reg, val, pitch_val;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002369 int tile_width;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002370
Chris Wilson05394f32010-11-08 19:18:58 +00002371 if ((obj->gtt_offset & ~I915_FENCE_START_MASK) ||
2372 (obj->gtt_offset & (size - 1))) {
Chris Wilsona00b10c2010-09-24 21:15:47 +01002373 WARN(1, "%s: object 0x%08x [fenceable? %d] not 1M or size (0x%08x) aligned [gtt_space offset=%lx, size=%lx]\n",
Chris Wilson05394f32010-11-08 19:18:58 +00002374 __func__, obj->gtt_offset, obj->map_and_fenceable, size,
2375 obj->gtt_space->start, obj->gtt_space->size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002376 return;
2377 }
2378
Chris Wilson05394f32010-11-08 19:18:58 +00002379 if (obj->tiling_mode == I915_TILING_Y &&
Jesse Barnes0f973f22009-01-26 17:10:45 -08002380 HAS_128_BYTE_Y_TILING(dev))
2381 tile_width = 128;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002382 else
Jesse Barnes0f973f22009-01-26 17:10:45 -08002383 tile_width = 512;
2384
2385 /* Note: pitch better be a power of two tile widths */
Chris Wilson05394f32010-11-08 19:18:58 +00002386 pitch_val = obj->stride / tile_width;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002387 pitch_val = ffs(pitch_val) - 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002388
Chris Wilson05394f32010-11-08 19:18:58 +00002389 if (obj->tiling_mode == I915_TILING_Y &&
Daniel Vetterc36a2a62010-04-17 15:12:03 +02002390 HAS_128_BYTE_Y_TILING(dev))
2391 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2392 else
2393 WARN_ON(pitch_val > I915_FENCE_MAX_PITCH_VAL);
2394
Chris Wilson05394f32010-11-08 19:18:58 +00002395 val = obj->gtt_offset;
2396 if (obj->tiling_mode == I915_TILING_Y)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002397 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002398 val |= I915_FENCE_SIZE_BITS(size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002399 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2400 val |= I830_FENCE_REG_VALID;
2401
Chris Wilson05394f32010-11-08 19:18:58 +00002402 fence_reg = obj->fence_reg;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002403 if (fence_reg < 8)
2404 fence_reg = FENCE_REG_830_0 + fence_reg * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002405 else
Chris Wilsona00b10c2010-09-24 21:15:47 +01002406 fence_reg = FENCE_REG_945_8 + (fence_reg - 8) * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002407 I915_WRITE(fence_reg, val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002408}
2409
Chris Wilson05394f32010-11-08 19:18:58 +00002410static void i830_write_fence_reg(struct drm_i915_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002411{
Chris Wilson05394f32010-11-08 19:18:58 +00002412 struct drm_device *dev = obj->base.dev;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002413 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00002414 u32 size = obj->gtt_space->size;
2415 int regnum = obj->fence_reg;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002416 uint32_t val;
2417 uint32_t pitch_val;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002418 uint32_t fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002419
Chris Wilson05394f32010-11-08 19:18:58 +00002420 if ((obj->gtt_offset & ~I830_FENCE_START_MASK) ||
2421 (obj->gtt_offset & (obj->base.size - 1))) {
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002422 WARN(1, "%s: object 0x%08x not 512K or size aligned\n",
Chris Wilson05394f32010-11-08 19:18:58 +00002423 __func__, obj->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002424 return;
2425 }
2426
Chris Wilson05394f32010-11-08 19:18:58 +00002427 pitch_val = obj->stride / 128;
Eric Anholte76a16d2009-05-26 17:44:56 -07002428 pitch_val = ffs(pitch_val) - 1;
2429 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2430
Chris Wilson05394f32010-11-08 19:18:58 +00002431 val = obj->gtt_offset;
2432 if (obj->tiling_mode == I915_TILING_Y)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002433 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002434 fence_size_bits = I830_FENCE_SIZE_BITS(size);
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002435 WARN_ON(fence_size_bits & ~0x00000f00);
2436 val |= fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002437 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2438 val |= I830_FENCE_REG_VALID;
2439
2440 I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002441}
2442
Chris Wilson2cf34d72010-09-14 13:03:28 +01002443static int i915_find_fence_reg(struct drm_device *dev,
2444 bool interruptible)
Daniel Vetterae3db242010-02-19 11:51:58 +01002445{
Daniel Vetterae3db242010-02-19 11:51:58 +01002446 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002447 struct drm_i915_fence_reg *reg;
Chris Wilson05394f32010-11-08 19:18:58 +00002448 struct drm_i915_gem_object *obj = NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002449 int i, avail, ret;
2450
2451 /* First try to find a free reg */
2452 avail = 0;
2453 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2454 reg = &dev_priv->fence_regs[i];
2455 if (!reg->obj)
2456 return i;
2457
Chris Wilson05394f32010-11-08 19:18:58 +00002458 if (!reg->obj->pin_count)
2459 avail++;
Daniel Vetterae3db242010-02-19 11:51:58 +01002460 }
2461
2462 if (avail == 0)
2463 return -ENOSPC;
2464
2465 /* None available, try to steal one or wait for a user to finish */
Chris Wilsona00b10c2010-09-24 21:15:47 +01002466 avail = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002467 list_for_each_entry(reg, &dev_priv->mm.fence_list,
2468 lru_list) {
Chris Wilson05394f32010-11-08 19:18:58 +00002469 obj = reg->obj;
2470 if (obj->pin_count)
Daniel Vetterae3db242010-02-19 11:51:58 +01002471 continue;
2472
2473 /* found one! */
Chris Wilson05394f32010-11-08 19:18:58 +00002474 avail = obj->fence_reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01002475 break;
2476 }
2477
Chris Wilsona00b10c2010-09-24 21:15:47 +01002478 BUG_ON(avail == I915_FENCE_REG_NONE);
Daniel Vetterae3db242010-02-19 11:51:58 +01002479
2480 /* We only have a reference on obj from the active list. put_fence_reg
2481 * might drop that one, causing a use-after-free in it. So hold a
2482 * private reference to obj like the other callers of put_fence_reg
2483 * (set_tiling ioctl) do. */
Chris Wilson05394f32010-11-08 19:18:58 +00002484 drm_gem_object_reference(&obj->base);
2485 ret = i915_gem_object_put_fence_reg(obj, interruptible);
2486 drm_gem_object_unreference(&obj->base);
Daniel Vetterae3db242010-02-19 11:51:58 +01002487 if (ret != 0)
2488 return ret;
2489
Chris Wilsona00b10c2010-09-24 21:15:47 +01002490 return avail;
Daniel Vetterae3db242010-02-19 11:51:58 +01002491}
2492
Jesse Barnesde151cf2008-11-12 10:03:55 -08002493/**
2494 * i915_gem_object_get_fence_reg - set up a fence reg for an object
2495 * @obj: object to map through a fence reg
2496 *
2497 * When mapping objects through the GTT, userspace wants to be able to write
2498 * to them without having to worry about swizzling if the object is tiled.
2499 *
2500 * This function walks the fence regs looking for a free one for @obj,
2501 * stealing one if it can't find any.
2502 *
2503 * It then sets up the reg based on the object's properties: address, pitch
2504 * and tiling format.
2505 */
Chris Wilson8c4b8c32009-06-17 22:08:52 +01002506int
Chris Wilson05394f32010-11-08 19:18:58 +00002507i915_gem_object_get_fence_reg(struct drm_i915_gem_object *obj,
Chris Wilson2cf34d72010-09-14 13:03:28 +01002508 bool interruptible)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002509{
Chris Wilson05394f32010-11-08 19:18:58 +00002510 struct drm_device *dev = obj->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002511 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002512 struct drm_i915_fence_reg *reg = NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002513 int ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002514
Eric Anholta09ba7f2009-08-29 12:49:51 -07002515 /* Just update our place in the LRU if our fence is getting used. */
Chris Wilson05394f32010-11-08 19:18:58 +00002516 if (obj->fence_reg != I915_FENCE_REG_NONE) {
2517 reg = &dev_priv->fence_regs[obj->fence_reg];
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002518 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002519 return 0;
2520 }
2521
Chris Wilson05394f32010-11-08 19:18:58 +00002522 switch (obj->tiling_mode) {
Jesse Barnesde151cf2008-11-12 10:03:55 -08002523 case I915_TILING_NONE:
2524 WARN(1, "allocating a fence for non-tiled object?\n");
2525 break;
2526 case I915_TILING_X:
Chris Wilson05394f32010-11-08 19:18:58 +00002527 if (!obj->stride)
Jesse Barnes0f973f22009-01-26 17:10:45 -08002528 return -EINVAL;
Chris Wilson05394f32010-11-08 19:18:58 +00002529 WARN((obj->stride & (512 - 1)),
Jesse Barnes0f973f22009-01-26 17:10:45 -08002530 "object 0x%08x is X tiled but has non-512B pitch\n",
Chris Wilson05394f32010-11-08 19:18:58 +00002531 obj->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002532 break;
2533 case I915_TILING_Y:
Chris Wilson05394f32010-11-08 19:18:58 +00002534 if (!obj->stride)
Jesse Barnes0f973f22009-01-26 17:10:45 -08002535 return -EINVAL;
Chris Wilson05394f32010-11-08 19:18:58 +00002536 WARN((obj->stride & (128 - 1)),
Jesse Barnes0f973f22009-01-26 17:10:45 -08002537 "object 0x%08x is Y tiled but has non-128B pitch\n",
Chris Wilson05394f32010-11-08 19:18:58 +00002538 obj->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002539 break;
2540 }
2541
Chris Wilson2cf34d72010-09-14 13:03:28 +01002542 ret = i915_find_fence_reg(dev, interruptible);
Daniel Vetterae3db242010-02-19 11:51:58 +01002543 if (ret < 0)
2544 return ret;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002545
Chris Wilson05394f32010-11-08 19:18:58 +00002546 obj->fence_reg = ret;
2547 reg = &dev_priv->fence_regs[obj->fence_reg];
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002548 list_add_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002549
Jesse Barnesde151cf2008-11-12 10:03:55 -08002550 reg->obj = obj;
2551
Chris Wilsone259bef2010-09-17 00:32:02 +01002552 switch (INTEL_INFO(dev)->gen) {
2553 case 6:
Chris Wilsona00b10c2010-09-24 21:15:47 +01002554 sandybridge_write_fence_reg(obj);
Chris Wilsone259bef2010-09-17 00:32:02 +01002555 break;
2556 case 5:
2557 case 4:
Chris Wilsona00b10c2010-09-24 21:15:47 +01002558 i965_write_fence_reg(obj);
Chris Wilsone259bef2010-09-17 00:32:02 +01002559 break;
2560 case 3:
Chris Wilsona00b10c2010-09-24 21:15:47 +01002561 i915_write_fence_reg(obj);
Chris Wilsone259bef2010-09-17 00:32:02 +01002562 break;
2563 case 2:
Chris Wilsona00b10c2010-09-24 21:15:47 +01002564 i830_write_fence_reg(obj);
Chris Wilsone259bef2010-09-17 00:32:02 +01002565 break;
2566 }
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002567
Chris Wilsona00b10c2010-09-24 21:15:47 +01002568 trace_i915_gem_object_get_fence(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00002569 obj->fence_reg,
2570 obj->tiling_mode);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002571
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002572 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002573}
2574
2575/**
2576 * i915_gem_clear_fence_reg - clear out fence register info
2577 * @obj: object to clear
2578 *
2579 * Zeroes out the fence register itself and clears out the associated
Chris Wilson05394f32010-11-08 19:18:58 +00002580 * data structures in dev_priv and obj.
Jesse Barnesde151cf2008-11-12 10:03:55 -08002581 */
2582static void
Chris Wilson05394f32010-11-08 19:18:58 +00002583i915_gem_clear_fence_reg(struct drm_i915_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002584{
Chris Wilson05394f32010-11-08 19:18:58 +00002585 struct drm_device *dev = obj->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002586 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00002587 struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[obj->fence_reg];
Chris Wilsone259bef2010-09-17 00:32:02 +01002588 uint32_t fence_reg;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002589
Chris Wilsone259bef2010-09-17 00:32:02 +01002590 switch (INTEL_INFO(dev)->gen) {
2591 case 6:
Eric Anholt4e901fd2009-10-26 16:44:17 -07002592 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 +
Chris Wilson05394f32010-11-08 19:18:58 +00002593 (obj->fence_reg * 8), 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002594 break;
2595 case 5:
2596 case 4:
Chris Wilson05394f32010-11-08 19:18:58 +00002597 I915_WRITE64(FENCE_REG_965_0 + (obj->fence_reg * 8), 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002598 break;
2599 case 3:
Chris Wilson05394f32010-11-08 19:18:58 +00002600 if (obj->fence_reg >= 8)
2601 fence_reg = FENCE_REG_945_8 + (obj->fence_reg - 8) * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002602 else
Chris Wilsone259bef2010-09-17 00:32:02 +01002603 case 2:
Chris Wilson05394f32010-11-08 19:18:58 +00002604 fence_reg = FENCE_REG_830_0 + obj->fence_reg * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002605
2606 I915_WRITE(fence_reg, 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002607 break;
Eric Anholtdc529a42009-03-10 22:34:49 -07002608 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002609
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002610 reg->obj = NULL;
Chris Wilson05394f32010-11-08 19:18:58 +00002611 obj->fence_reg = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002612 list_del_init(&reg->lru_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002613}
2614
Eric Anholt673a3942008-07-30 12:06:12 -07002615/**
Chris Wilson52dc7d32009-06-06 09:46:01 +01002616 * i915_gem_object_put_fence_reg - waits on outstanding fenced access
2617 * to the buffer to finish, and then resets the fence register.
2618 * @obj: tiled object holding a fence register.
Chris Wilson2cf34d72010-09-14 13:03:28 +01002619 * @bool: whether the wait upon the fence is interruptible
Chris Wilson52dc7d32009-06-06 09:46:01 +01002620 *
2621 * Zeroes out the fence register itself and clears out the associated
Chris Wilson05394f32010-11-08 19:18:58 +00002622 * data structures in dev_priv and obj.
Chris Wilson52dc7d32009-06-06 09:46:01 +01002623 */
2624int
Chris Wilson05394f32010-11-08 19:18:58 +00002625i915_gem_object_put_fence_reg(struct drm_i915_gem_object *obj,
Chris Wilson2cf34d72010-09-14 13:03:28 +01002626 bool interruptible)
Chris Wilson52dc7d32009-06-06 09:46:01 +01002627{
Chris Wilson05394f32010-11-08 19:18:58 +00002628 struct drm_device *dev = obj->base.dev;
Chris Wilson53640e12010-09-20 11:40:50 +01002629 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson53640e12010-09-20 11:40:50 +01002630 struct drm_i915_fence_reg *reg;
Chris Wilson52dc7d32009-06-06 09:46:01 +01002631
Chris Wilson05394f32010-11-08 19:18:58 +00002632 if (obj->fence_reg == I915_FENCE_REG_NONE)
Chris Wilson52dc7d32009-06-06 09:46:01 +01002633 return 0;
2634
Daniel Vetter10ae9bd2010-02-01 13:59:17 +01002635 /* If we've changed tiling, GTT-mappings of the object
2636 * need to re-fault to ensure that the correct fence register
2637 * setup is in place.
2638 */
2639 i915_gem_release_mmap(obj);
2640
Chris Wilson52dc7d32009-06-06 09:46:01 +01002641 /* On the i915, GPU access to tiled buffers is via a fence,
2642 * therefore we must wait for any outstanding access to complete
2643 * before clearing the fence.
2644 */
Chris Wilson05394f32010-11-08 19:18:58 +00002645 reg = &dev_priv->fence_regs[obj->fence_reg];
Chris Wilson53640e12010-09-20 11:40:50 +01002646 if (reg->gpu) {
Chris Wilson52dc7d32009-06-06 09:46:01 +01002647 int ret;
2648
Chris Wilson2cf34d72010-09-14 13:03:28 +01002649 ret = i915_gem_object_flush_gpu_write_domain(obj, true);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002650 if (ret)
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002651 return ret;
2652
Chris Wilson2cf34d72010-09-14 13:03:28 +01002653 ret = i915_gem_object_wait_rendering(obj, interruptible);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002654 if (ret)
Chris Wilson52dc7d32009-06-06 09:46:01 +01002655 return ret;
Chris Wilson53640e12010-09-20 11:40:50 +01002656
2657 reg->gpu = false;
Chris Wilson52dc7d32009-06-06 09:46:01 +01002658 }
2659
Daniel Vetter4a726612010-02-01 13:59:16 +01002660 i915_gem_object_flush_gtt_write_domain(obj);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002661 i915_gem_clear_fence_reg(obj);
Chris Wilson52dc7d32009-06-06 09:46:01 +01002662
2663 return 0;
2664}
2665
2666/**
Eric Anholt673a3942008-07-30 12:06:12 -07002667 * Finds free space in the GTT aperture and binds the object there.
2668 */
2669static int
Chris Wilson05394f32010-11-08 19:18:58 +00002670i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
Daniel Vetter920afa72010-09-16 17:54:23 +02002671 unsigned alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01002672 bool map_and_fenceable)
Eric Anholt673a3942008-07-30 12:06:12 -07002673{
Chris Wilson05394f32010-11-08 19:18:58 +00002674 struct drm_device *dev = obj->base.dev;
Eric Anholt673a3942008-07-30 12:06:12 -07002675 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07002676 struct drm_mm_node *free_space;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002677 gfp_t gfpmask = __GFP_NORETRY | __GFP_NOWARN;
Daniel Vetter5e783302010-11-14 22:32:36 +01002678 u32 size, fence_size, fence_alignment, unfenced_alignment;
Daniel Vetter75e9e912010-11-04 17:11:09 +01002679 bool mappable, fenceable;
Chris Wilson07f73f62009-09-14 16:50:30 +01002680 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002681
Chris Wilson05394f32010-11-08 19:18:58 +00002682 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01002683 DRM_ERROR("Attempting to bind a purgeable object\n");
2684 return -EINVAL;
2685 }
2686
Chris Wilson05394f32010-11-08 19:18:58 +00002687 fence_size = i915_gem_get_gtt_size(obj);
2688 fence_alignment = i915_gem_get_gtt_alignment(obj);
2689 unfenced_alignment = i915_gem_get_unfenced_gtt_alignment(obj);
Chris Wilsona00b10c2010-09-24 21:15:47 +01002690
Eric Anholt673a3942008-07-30 12:06:12 -07002691 if (alignment == 0)
Daniel Vetter5e783302010-11-14 22:32:36 +01002692 alignment = map_and_fenceable ? fence_alignment :
2693 unfenced_alignment;
Daniel Vetter75e9e912010-11-04 17:11:09 +01002694 if (map_and_fenceable && alignment & (fence_alignment - 1)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002695 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2696 return -EINVAL;
2697 }
2698
Chris Wilson05394f32010-11-08 19:18:58 +00002699 size = map_and_fenceable ? fence_size : obj->base.size;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002700
Chris Wilson654fc602010-05-27 13:18:21 +01002701 /* If the object is bigger than the entire aperture, reject it early
2702 * before evicting everything in a vain attempt to find space.
2703 */
Chris Wilson05394f32010-11-08 19:18:58 +00002704 if (obj->base.size >
Daniel Vetter75e9e912010-11-04 17:11:09 +01002705 (map_and_fenceable ? dev_priv->mm.gtt_mappable_end : dev_priv->mm.gtt_total)) {
Chris Wilson654fc602010-05-27 13:18:21 +01002706 DRM_ERROR("Attempting to bind an object larger than the aperture\n");
2707 return -E2BIG;
2708 }
2709
Eric Anholt673a3942008-07-30 12:06:12 -07002710 search_free:
Daniel Vetter75e9e912010-11-04 17:11:09 +01002711 if (map_and_fenceable)
Daniel Vetter920afa72010-09-16 17:54:23 +02002712 free_space =
2713 drm_mm_search_free_in_range(&dev_priv->mm.gtt_space,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002714 size, alignment, 0,
Daniel Vetter920afa72010-09-16 17:54:23 +02002715 dev_priv->mm.gtt_mappable_end,
2716 0);
2717 else
2718 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002719 size, alignment, 0);
Daniel Vetter920afa72010-09-16 17:54:23 +02002720
2721 if (free_space != NULL) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01002722 if (map_and_fenceable)
Chris Wilson05394f32010-11-08 19:18:58 +00002723 obj->gtt_space =
Daniel Vetter920afa72010-09-16 17:54:23 +02002724 drm_mm_get_block_range_generic(free_space,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002725 size, alignment, 0,
Daniel Vetter920afa72010-09-16 17:54:23 +02002726 dev_priv->mm.gtt_mappable_end,
2727 0);
2728 else
Chris Wilson05394f32010-11-08 19:18:58 +00002729 obj->gtt_space =
Chris Wilsona00b10c2010-09-24 21:15:47 +01002730 drm_mm_get_block(free_space, size, alignment);
Daniel Vetter920afa72010-09-16 17:54:23 +02002731 }
Chris Wilson05394f32010-11-08 19:18:58 +00002732 if (obj->gtt_space == NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -07002733 /* If the gtt is empty and we're still having trouble
2734 * fitting our object in, we're out of memory.
2735 */
Daniel Vetter75e9e912010-11-04 17:11:09 +01002736 ret = i915_gem_evict_something(dev, size, alignment,
2737 map_and_fenceable);
Chris Wilson97311292009-09-21 00:22:34 +01002738 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002739 return ret;
Chris Wilson97311292009-09-21 00:22:34 +01002740
Eric Anholt673a3942008-07-30 12:06:12 -07002741 goto search_free;
2742 }
2743
Chris Wilsone5281cc2010-10-28 13:45:36 +01002744 ret = i915_gem_object_get_pages_gtt(obj, gfpmask);
Eric Anholt673a3942008-07-30 12:06:12 -07002745 if (ret) {
Chris Wilson05394f32010-11-08 19:18:58 +00002746 drm_mm_put_block(obj->gtt_space);
2747 obj->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002748
2749 if (ret == -ENOMEM) {
2750 /* first try to clear up some space from the GTT */
Chris Wilsona00b10c2010-09-24 21:15:47 +01002751 ret = i915_gem_evict_something(dev, size,
Daniel Vetter75e9e912010-11-04 17:11:09 +01002752 alignment,
2753 map_and_fenceable);
Chris Wilson07f73f62009-09-14 16:50:30 +01002754 if (ret) {
Chris Wilson07f73f62009-09-14 16:50:30 +01002755 /* now try to shrink everyone else */
Chris Wilson4bdadb92010-01-27 13:36:32 +00002756 if (gfpmask) {
2757 gfpmask = 0;
2758 goto search_free;
Chris Wilson07f73f62009-09-14 16:50:30 +01002759 }
2760
2761 return ret;
2762 }
2763
2764 goto search_free;
2765 }
2766
Eric Anholt673a3942008-07-30 12:06:12 -07002767 return ret;
2768 }
2769
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002770 ret = i915_gem_gtt_bind_object(obj);
2771 if (ret) {
Chris Wilsone5281cc2010-10-28 13:45:36 +01002772 i915_gem_object_put_pages_gtt(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00002773 drm_mm_put_block(obj->gtt_space);
2774 obj->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002775
Chris Wilsona00b10c2010-09-24 21:15:47 +01002776 ret = i915_gem_evict_something(dev, size,
Daniel Vetter75e9e912010-11-04 17:11:09 +01002777 alignment, map_and_fenceable);
Chris Wilson97311292009-09-21 00:22:34 +01002778 if (ret)
Chris Wilson07f73f62009-09-14 16:50:30 +01002779 return ret;
Chris Wilson07f73f62009-09-14 16:50:30 +01002780
2781 goto search_free;
Eric Anholt673a3942008-07-30 12:06:12 -07002782 }
Eric Anholt673a3942008-07-30 12:06:12 -07002783
Chris Wilson05394f32010-11-08 19:18:58 +00002784 obj->gtt_offset = obj->gtt_space->start;
Daniel Vetterfb7d5162010-10-01 22:05:20 +02002785
Chris Wilsonbf1a1092010-08-07 11:01:20 +01002786 /* keep track of bounds object by adding it to the inactive list */
Chris Wilson05394f32010-11-08 19:18:58 +00002787 list_add_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
2788 i915_gem_info_add_gtt(dev_priv, obj);
Chris Wilsonbf1a1092010-08-07 11:01:20 +01002789
Eric Anholt673a3942008-07-30 12:06:12 -07002790 /* Assert that the object is not currently in any GPU domain. As it
2791 * wasn't in the GTT, there shouldn't be any way it could have been in
2792 * a GPU cache
2793 */
Chris Wilson05394f32010-11-08 19:18:58 +00002794 BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
2795 BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
Eric Anholt673a3942008-07-30 12:06:12 -07002796
Chris Wilson05394f32010-11-08 19:18:58 +00002797 trace_i915_gem_object_bind(obj, obj->gtt_offset, map_and_fenceable);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002798
Daniel Vetter75e9e912010-11-04 17:11:09 +01002799 fenceable =
Chris Wilson05394f32010-11-08 19:18:58 +00002800 obj->gtt_space->size == fence_size &&
2801 (obj->gtt_space->start & (fence_alignment -1)) == 0;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002802
Daniel Vetter75e9e912010-11-04 17:11:09 +01002803 mappable =
Chris Wilson05394f32010-11-08 19:18:58 +00002804 obj->gtt_offset + obj->base.size <= dev_priv->mm.gtt_mappable_end;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002805
Chris Wilson05394f32010-11-08 19:18:58 +00002806 obj->map_and_fenceable = mappable && fenceable;
Daniel Vetter75e9e912010-11-04 17:11:09 +01002807
Eric Anholt673a3942008-07-30 12:06:12 -07002808 return 0;
2809}
2810
2811void
Chris Wilson05394f32010-11-08 19:18:58 +00002812i915_gem_clflush_object(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002813{
Eric Anholt673a3942008-07-30 12:06:12 -07002814 /* If we don't have a page list set up, then we're not pinned
2815 * to GPU, and we can ignore the cache flush because it'll happen
2816 * again at bind time.
2817 */
Chris Wilson05394f32010-11-08 19:18:58 +00002818 if (obj->pages == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002819 return;
2820
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002821 trace_i915_gem_object_clflush(obj);
Eric Anholtcfa16a02009-05-26 18:46:16 -07002822
Chris Wilson05394f32010-11-08 19:18:58 +00002823 drm_clflush_pages(obj->pages, obj->base.size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002824}
2825
Eric Anholte47c68e2008-11-14 13:35:19 -08002826/** Flushes any GPU write domain for the object if it's dirty. */
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002827static int
Chris Wilson05394f32010-11-08 19:18:58 +00002828i915_gem_object_flush_gpu_write_domain(struct drm_i915_gem_object *obj,
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002829 bool pipelined)
Eric Anholte47c68e2008-11-14 13:35:19 -08002830{
Chris Wilson05394f32010-11-08 19:18:58 +00002831 struct drm_device *dev = obj->base.dev;
Eric Anholte47c68e2008-11-14 13:35:19 -08002832
Chris Wilson05394f32010-11-08 19:18:58 +00002833 if ((obj->base.write_domain & I915_GEM_GPU_DOMAINS) == 0)
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002834 return 0;
Eric Anholte47c68e2008-11-14 13:35:19 -08002835
2836 /* Queue the GPU write cache flushing we need. */
Chris Wilson05394f32010-11-08 19:18:58 +00002837 i915_gem_flush_ring(dev, obj->ring, 0, obj->base.write_domain);
2838 BUG_ON(obj->base.write_domain);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002839
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002840 if (pipelined)
2841 return 0;
2842
Chris Wilson2cf34d72010-09-14 13:03:28 +01002843 return i915_gem_object_wait_rendering(obj, true);
Eric Anholte47c68e2008-11-14 13:35:19 -08002844}
2845
2846/** Flushes the GTT write domain for the object if it's dirty. */
2847static void
Chris Wilson05394f32010-11-08 19:18:58 +00002848i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08002849{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002850 uint32_t old_write_domain;
2851
Chris Wilson05394f32010-11-08 19:18:58 +00002852 if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
Eric Anholte47c68e2008-11-14 13:35:19 -08002853 return;
2854
2855 /* No actual flushing is required for the GTT write domain. Writes
2856 * to it immediately go to main memory as far as we know, so there's
2857 * no chipset flush. It also doesn't land in render cache.
2858 */
Chris Wilson4a684a42010-10-28 14:44:08 +01002859 i915_gem_release_mmap(obj);
2860
Chris Wilson05394f32010-11-08 19:18:58 +00002861 old_write_domain = obj->base.write_domain;
2862 obj->base.write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002863
2864 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00002865 obj->base.read_domains,
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002866 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002867}
2868
2869/** Flushes the CPU write domain for the object if it's dirty. */
2870static void
Chris Wilson05394f32010-11-08 19:18:58 +00002871i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08002872{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002873 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002874
Chris Wilson05394f32010-11-08 19:18:58 +00002875 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
Eric Anholte47c68e2008-11-14 13:35:19 -08002876 return;
2877
2878 i915_gem_clflush_object(obj);
Daniel Vetter40ce6572010-11-05 18:12:18 +01002879 intel_gtt_chipset_flush();
Chris Wilson05394f32010-11-08 19:18:58 +00002880 old_write_domain = obj->base.write_domain;
2881 obj->base.write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002882
2883 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00002884 obj->base.read_domains,
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002885 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002886}
2887
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002888/**
2889 * Moves a single object to the GTT read, and possibly write domain.
2890 *
2891 * This function returns when the move is complete, including waiting on
2892 * flushes to occur.
2893 */
Jesse Barnes79e53942008-11-07 14:24:08 -08002894int
Chris Wilson05394f32010-11-08 19:18:58 +00002895i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, int write)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002896{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002897 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002898 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002899
Eric Anholt02354392008-11-26 13:58:13 -08002900 /* Not valid to be called on unbound objects. */
Chris Wilson05394f32010-11-08 19:18:58 +00002901 if (obj->gtt_space == NULL)
Eric Anholt02354392008-11-26 13:58:13 -08002902 return -EINVAL;
2903
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002904 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002905 if (ret != 0)
2906 return ret;
2907
Chris Wilson72133422010-09-13 23:56:38 +01002908 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002909
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002910 if (write) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01002911 ret = i915_gem_object_wait_rendering(obj, true);
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002912 if (ret)
2913 return ret;
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002914 }
Eric Anholte47c68e2008-11-14 13:35:19 -08002915
Chris Wilson05394f32010-11-08 19:18:58 +00002916 old_write_domain = obj->base.write_domain;
2917 old_read_domains = obj->base.read_domains;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002918
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002919 /* It should now be out of any other write domains, and we can update
2920 * the domain values for our changes.
2921 */
Chris Wilson05394f32010-11-08 19:18:58 +00002922 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2923 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002924 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00002925 obj->base.read_domains = I915_GEM_DOMAIN_GTT;
2926 obj->base.write_domain = I915_GEM_DOMAIN_GTT;
2927 obj->dirty = 1;
Eric Anholte47c68e2008-11-14 13:35:19 -08002928 }
2929
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002930 trace_i915_gem_object_change_domain(obj,
2931 old_read_domains,
2932 old_write_domain);
2933
Eric Anholte47c68e2008-11-14 13:35:19 -08002934 return 0;
2935}
2936
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002937/*
2938 * Prepare buffer for display plane. Use uninterruptible for possible flush
2939 * wait, as in modesetting process we're not supposed to be interrupted.
2940 */
2941int
Chris Wilson05394f32010-11-08 19:18:58 +00002942i915_gem_object_set_to_display_plane(struct drm_i915_gem_object *obj,
Chris Wilson48b956c2010-09-14 12:50:34 +01002943 bool pipelined)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002944{
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002945 uint32_t old_read_domains;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002946 int ret;
2947
2948 /* Not valid to be called on unbound objects. */
Chris Wilson05394f32010-11-08 19:18:58 +00002949 if (obj->gtt_space == NULL)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002950 return -EINVAL;
2951
Chris Wilsonced270f2010-09-26 22:47:46 +01002952 ret = i915_gem_object_flush_gpu_write_domain(obj, true);
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002953 if (ret)
2954 return ret;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002955
Chris Wilsonced270f2010-09-26 22:47:46 +01002956 /* Currently, we are always called from an non-interruptible context. */
2957 if (!pipelined) {
2958 ret = i915_gem_object_wait_rendering(obj, false);
2959 if (ret)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002960 return ret;
2961 }
2962
Chris Wilsonb118c1e2010-05-27 13:18:14 +01002963 i915_gem_object_flush_cpu_write_domain(obj);
2964
Chris Wilson05394f32010-11-08 19:18:58 +00002965 old_read_domains = obj->base.read_domains;
2966 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002967
2968 trace_i915_gem_object_change_domain(obj,
2969 old_read_domains,
Chris Wilson05394f32010-11-08 19:18:58 +00002970 obj->base.write_domain);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002971
2972 return 0;
2973}
2974
Chris Wilson85345512010-11-13 09:49:11 +00002975int
2976i915_gem_object_flush_gpu(struct drm_i915_gem_object *obj,
2977 bool interruptible)
2978{
2979 if (!obj->active)
2980 return 0;
2981
2982 if (obj->base.write_domain & I915_GEM_GPU_DOMAINS)
Chris Wilson05394f32010-11-08 19:18:58 +00002983 i915_gem_flush_ring(obj->base.dev, obj->ring,
Chris Wilson85345512010-11-13 09:49:11 +00002984 0, obj->base.write_domain);
2985
Chris Wilson05394f32010-11-08 19:18:58 +00002986 return i915_gem_object_wait_rendering(obj, interruptible);
Chris Wilson85345512010-11-13 09:49:11 +00002987}
2988
Eric Anholte47c68e2008-11-14 13:35:19 -08002989/**
2990 * Moves a single object to the CPU read, and possibly write domain.
2991 *
2992 * This function returns when the move is complete, including waiting on
2993 * flushes to occur.
2994 */
2995static int
Chris Wilson05394f32010-11-08 19:18:58 +00002996i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, int write)
Eric Anholte47c68e2008-11-14 13:35:19 -08002997{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002998 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002999 int ret;
3000
Daniel Vetterba3d8d72010-02-11 22:37:04 +01003001 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Eric Anholte47c68e2008-11-14 13:35:19 -08003002 if (ret != 0)
3003 return ret;
3004
3005 i915_gem_object_flush_gtt_write_domain(obj);
3006
3007 /* If we have a partially-valid cache of the object in the CPU,
3008 * finish invalidating it and free the per-page flags.
3009 */
3010 i915_gem_object_set_to_full_cpu_read_domain(obj);
3011
Chris Wilson72133422010-09-13 23:56:38 +01003012 if (write) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01003013 ret = i915_gem_object_wait_rendering(obj, true);
Chris Wilson72133422010-09-13 23:56:38 +01003014 if (ret)
3015 return ret;
3016 }
3017
Chris Wilson05394f32010-11-08 19:18:58 +00003018 old_write_domain = obj->base.write_domain;
3019 old_read_domains = obj->base.read_domains;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003020
Eric Anholte47c68e2008-11-14 13:35:19 -08003021 /* Flush the CPU cache if it's still invalid. */
Chris Wilson05394f32010-11-08 19:18:58 +00003022 if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
Eric Anholte47c68e2008-11-14 13:35:19 -08003023 i915_gem_clflush_object(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003024
Chris Wilson05394f32010-11-08 19:18:58 +00003025 obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003026 }
3027
3028 /* It should now be out of any other write domains, and we can update
3029 * the domain values for our changes.
3030 */
Chris Wilson05394f32010-11-08 19:18:58 +00003031 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
Eric Anholte47c68e2008-11-14 13:35:19 -08003032
3033 /* If we're writing through the CPU, then the GPU read domains will
3034 * need to be invalidated at next use.
3035 */
3036 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00003037 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3038 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003039 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003040
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003041 trace_i915_gem_object_change_domain(obj,
3042 old_read_domains,
3043 old_write_domain);
3044
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003045 return 0;
3046}
3047
Eric Anholt673a3942008-07-30 12:06:12 -07003048/*
3049 * Set the next domain for the specified object. This
3050 * may not actually perform the necessary flushing/invaliding though,
3051 * as that may want to be batched with other set_domain operations
3052 *
3053 * This is (we hope) the only really tricky part of gem. The goal
3054 * is fairly simple -- track which caches hold bits of the object
3055 * and make sure they remain coherent. A few concrete examples may
3056 * help to explain how it works. For shorthand, we use the notation
3057 * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
3058 * a pair of read and write domain masks.
3059 *
3060 * Case 1: the batch buffer
3061 *
3062 * 1. Allocated
3063 * 2. Written by CPU
3064 * 3. Mapped to GTT
3065 * 4. Read by GPU
3066 * 5. Unmapped from GTT
3067 * 6. Freed
3068 *
3069 * Let's take these a step at a time
3070 *
3071 * 1. Allocated
3072 * Pages allocated from the kernel may still have
3073 * cache contents, so we set them to (CPU, CPU) always.
3074 * 2. Written by CPU (using pwrite)
3075 * The pwrite function calls set_domain (CPU, CPU) and
3076 * this function does nothing (as nothing changes)
3077 * 3. Mapped by GTT
3078 * This function asserts that the object is not
3079 * currently in any GPU-based read or write domains
3080 * 4. Read by GPU
3081 * i915_gem_execbuffer calls set_domain (COMMAND, 0).
3082 * As write_domain is zero, this function adds in the
3083 * current read domains (CPU+COMMAND, 0).
3084 * flush_domains is set to CPU.
3085 * invalidate_domains is set to COMMAND
3086 * clflush is run to get data out of the CPU caches
3087 * then i915_dev_set_domain calls i915_gem_flush to
3088 * emit an MI_FLUSH and drm_agp_chipset_flush
3089 * 5. Unmapped from GTT
3090 * i915_gem_object_unbind calls set_domain (CPU, CPU)
3091 * flush_domains and invalidate_domains end up both zero
3092 * so no flushing/invalidating happens
3093 * 6. Freed
3094 * yay, done
3095 *
3096 * Case 2: The shared render buffer
3097 *
3098 * 1. Allocated
3099 * 2. Mapped to GTT
3100 * 3. Read/written by GPU
3101 * 4. set_domain to (CPU,CPU)
3102 * 5. Read/written by CPU
3103 * 6. Read/written by GPU
3104 *
3105 * 1. Allocated
3106 * Same as last example, (CPU, CPU)
3107 * 2. Mapped to GTT
3108 * Nothing changes (assertions find that it is not in the GPU)
3109 * 3. Read/written by GPU
3110 * execbuffer calls set_domain (RENDER, RENDER)
3111 * flush_domains gets CPU
3112 * invalidate_domains gets GPU
3113 * clflush (obj)
3114 * MI_FLUSH and drm_agp_chipset_flush
3115 * 4. set_domain (CPU, CPU)
3116 * flush_domains gets GPU
3117 * invalidate_domains gets CPU
3118 * wait_rendering (obj) to make sure all drawing is complete.
3119 * This will include an MI_FLUSH to get the data from GPU
3120 * to memory
3121 * clflush (obj) to invalidate the CPU cache
3122 * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
3123 * 5. Read/written by CPU
3124 * cache lines are loaded and dirtied
3125 * 6. Read written by GPU
3126 * Same as last GPU access
3127 *
3128 * Case 3: The constant buffer
3129 *
3130 * 1. Allocated
3131 * 2. Written by CPU
3132 * 3. Read by GPU
3133 * 4. Updated (written) by CPU again
3134 * 5. Read by GPU
3135 *
3136 * 1. Allocated
3137 * (CPU, CPU)
3138 * 2. Written by CPU
3139 * (CPU, CPU)
3140 * 3. Read by GPU
3141 * (CPU+RENDER, 0)
3142 * flush_domains = CPU
3143 * invalidate_domains = RENDER
3144 * clflush (obj)
3145 * MI_FLUSH
3146 * drm_agp_chipset_flush
3147 * 4. Updated (written) by CPU again
3148 * (CPU, CPU)
3149 * flush_domains = 0 (no previous write domain)
3150 * invalidate_domains = 0 (no new read domains)
3151 * 5. Read by GPU
3152 * (CPU+RENDER, 0)
3153 * flush_domains = CPU
3154 * invalidate_domains = RENDER
3155 * clflush (obj)
3156 * MI_FLUSH
3157 * drm_agp_chipset_flush
3158 */
Keith Packardc0d90822008-11-20 23:11:08 -08003159static void
Chris Wilson05394f32010-11-08 19:18:58 +00003160i915_gem_object_set_to_gpu_domain(struct drm_i915_gem_object *obj,
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003161 struct intel_ring_buffer *ring,
3162 struct change_domains *cd)
Eric Anholt673a3942008-07-30 12:06:12 -07003163{
Chris Wilson05394f32010-11-08 19:18:58 +00003164 uint32_t invalidate_domains = 0, flush_domains = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07003165
Eric Anholt673a3942008-07-30 12:06:12 -07003166 /*
3167 * If the object isn't moving to a new write domain,
3168 * let the object stay in multiple read domains
3169 */
Chris Wilson05394f32010-11-08 19:18:58 +00003170 if (obj->base.pending_write_domain == 0)
3171 obj->base.pending_read_domains |= obj->base.read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07003172
3173 /*
3174 * Flush the current write domain if
3175 * the new read domains don't match. Invalidate
3176 * any read domains which differ from the old
3177 * write domain
3178 */
Chris Wilson05394f32010-11-08 19:18:58 +00003179 if (obj->base.write_domain &&
3180 (obj->base.write_domain != obj->base.pending_read_domains ||
3181 obj->ring != ring)) {
3182 flush_domains |= obj->base.write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08003183 invalidate_domains |=
Chris Wilson05394f32010-11-08 19:18:58 +00003184 obj->base.pending_read_domains & ~obj->base.write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07003185 }
3186 /*
3187 * Invalidate any read caches which may have
3188 * stale data. That is, any new read domains.
3189 */
Chris Wilson05394f32010-11-08 19:18:58 +00003190 invalidate_domains |= obj->base.pending_read_domains & ~obj->base.read_domains;
Chris Wilson3d2a8122010-09-29 11:39:53 +01003191 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU)
Eric Anholt673a3942008-07-30 12:06:12 -07003192 i915_gem_clflush_object(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003193
Chris Wilson4a684a42010-10-28 14:44:08 +01003194 /* blow away mappings if mapped through GTT */
3195 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_GTT)
3196 i915_gem_release_mmap(obj);
3197
Eric Anholtefbeed92009-02-19 14:54:51 -08003198 /* The actual obj->write_domain will be updated with
3199 * pending_write_domain after we emit the accumulated flush for all
3200 * of our domain changes in execbuffers (which clears objects'
3201 * write_domains). So if we have a current write domain that we
3202 * aren't changing, set pending_write_domain to that.
3203 */
Chris Wilson05394f32010-11-08 19:18:58 +00003204 if (flush_domains == 0 && obj->base.pending_write_domain == 0)
3205 obj->base.pending_write_domain = obj->base.write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07003206
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003207 cd->invalidate_domains |= invalidate_domains;
3208 cd->flush_domains |= flush_domains;
Chris Wilsonb6651452010-10-23 10:15:06 +01003209 if (flush_domains & I915_GEM_GPU_DOMAINS)
Chris Wilson05394f32010-11-08 19:18:58 +00003210 cd->flush_rings |= obj->ring->id;
Chris Wilsonb6651452010-10-23 10:15:06 +01003211 if (invalidate_domains & I915_GEM_GPU_DOMAINS)
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003212 cd->flush_rings |= ring->id;
Eric Anholt673a3942008-07-30 12:06:12 -07003213}
3214
3215/**
Eric Anholte47c68e2008-11-14 13:35:19 -08003216 * Moves the object from a partially CPU read to a full one.
Eric Anholt673a3942008-07-30 12:06:12 -07003217 *
Eric Anholte47c68e2008-11-14 13:35:19 -08003218 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
3219 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
3220 */
3221static void
Chris Wilson05394f32010-11-08 19:18:58 +00003222i915_gem_object_set_to_full_cpu_read_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08003223{
Chris Wilson05394f32010-11-08 19:18:58 +00003224 if (!obj->page_cpu_valid)
Eric Anholte47c68e2008-11-14 13:35:19 -08003225 return;
3226
3227 /* If we're partially in the CPU read domain, finish moving it in.
3228 */
Chris Wilson05394f32010-11-08 19:18:58 +00003229 if (obj->base.read_domains & I915_GEM_DOMAIN_CPU) {
Eric Anholte47c68e2008-11-14 13:35:19 -08003230 int i;
3231
Chris Wilson05394f32010-11-08 19:18:58 +00003232 for (i = 0; i <= (obj->base.size - 1) / PAGE_SIZE; i++) {
3233 if (obj->page_cpu_valid[i])
Eric Anholte47c68e2008-11-14 13:35:19 -08003234 continue;
Chris Wilson05394f32010-11-08 19:18:58 +00003235 drm_clflush_pages(obj->pages + i, 1);
Eric Anholte47c68e2008-11-14 13:35:19 -08003236 }
Eric Anholte47c68e2008-11-14 13:35:19 -08003237 }
3238
3239 /* Free the page_cpu_valid mappings which are now stale, whether
3240 * or not we've got I915_GEM_DOMAIN_CPU.
3241 */
Chris Wilson05394f32010-11-08 19:18:58 +00003242 kfree(obj->page_cpu_valid);
3243 obj->page_cpu_valid = NULL;
Eric Anholte47c68e2008-11-14 13:35:19 -08003244}
3245
3246/**
3247 * Set the CPU read domain on a range of the object.
3248 *
3249 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
3250 * not entirely valid. The page_cpu_valid member of the object flags which
3251 * pages have been flushed, and will be respected by
3252 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
3253 * of the whole object.
3254 *
3255 * This function returns when the move is complete, including waiting on
3256 * flushes to occur.
Eric Anholt673a3942008-07-30 12:06:12 -07003257 */
3258static int
Chris Wilson05394f32010-11-08 19:18:58 +00003259i915_gem_object_set_cpu_read_domain_range(struct drm_i915_gem_object *obj,
Eric Anholte47c68e2008-11-14 13:35:19 -08003260 uint64_t offset, uint64_t size)
Eric Anholt673a3942008-07-30 12:06:12 -07003261{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003262 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003263 int i, ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003264
Chris Wilson05394f32010-11-08 19:18:58 +00003265 if (offset == 0 && size == obj->base.size)
Eric Anholte47c68e2008-11-14 13:35:19 -08003266 return i915_gem_object_set_to_cpu_domain(obj, 0);
3267
Daniel Vetterba3d8d72010-02-11 22:37:04 +01003268 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Eric Anholte47c68e2008-11-14 13:35:19 -08003269 if (ret != 0)
3270 return ret;
3271 i915_gem_object_flush_gtt_write_domain(obj);
3272
3273 /* If we're already fully in the CPU read domain, we're done. */
Chris Wilson05394f32010-11-08 19:18:58 +00003274 if (obj->page_cpu_valid == NULL &&
3275 (obj->base.read_domains & I915_GEM_DOMAIN_CPU) != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07003276 return 0;
3277
Eric Anholte47c68e2008-11-14 13:35:19 -08003278 /* Otherwise, create/clear the per-page CPU read domain flag if we're
3279 * newly adding I915_GEM_DOMAIN_CPU
3280 */
Chris Wilson05394f32010-11-08 19:18:58 +00003281 if (obj->page_cpu_valid == NULL) {
3282 obj->page_cpu_valid = kzalloc(obj->base.size / PAGE_SIZE,
3283 GFP_KERNEL);
3284 if (obj->page_cpu_valid == NULL)
Eric Anholte47c68e2008-11-14 13:35:19 -08003285 return -ENOMEM;
Chris Wilson05394f32010-11-08 19:18:58 +00003286 } else if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0)
3287 memset(obj->page_cpu_valid, 0, obj->base.size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07003288
3289 /* Flush the cache on any pages that are still invalid from the CPU's
3290 * perspective.
3291 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003292 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
3293 i++) {
Chris Wilson05394f32010-11-08 19:18:58 +00003294 if (obj->page_cpu_valid[i])
Eric Anholt673a3942008-07-30 12:06:12 -07003295 continue;
3296
Chris Wilson05394f32010-11-08 19:18:58 +00003297 drm_clflush_pages(obj->pages + i, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07003298
Chris Wilson05394f32010-11-08 19:18:58 +00003299 obj->page_cpu_valid[i] = 1;
Eric Anholt673a3942008-07-30 12:06:12 -07003300 }
3301
Eric Anholte47c68e2008-11-14 13:35:19 -08003302 /* It should now be out of any other write domains, and we can update
3303 * the domain values for our changes.
3304 */
Chris Wilson05394f32010-11-08 19:18:58 +00003305 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
Eric Anholte47c68e2008-11-14 13:35:19 -08003306
Chris Wilson05394f32010-11-08 19:18:58 +00003307 old_read_domains = obj->base.read_domains;
3308 obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003309
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003310 trace_i915_gem_object_change_domain(obj,
3311 old_read_domains,
Chris Wilson05394f32010-11-08 19:18:58 +00003312 obj->base.write_domain);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003313
Eric Anholt673a3942008-07-30 12:06:12 -07003314 return 0;
3315}
3316
Eric Anholt673a3942008-07-30 12:06:12 -07003317static int
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003318i915_gem_execbuffer_relocate_entry(struct drm_i915_gem_object *obj,
3319 struct drm_file *file_priv,
3320 struct drm_i915_gem_exec_object2 *entry,
3321 struct drm_i915_gem_relocation_entry *reloc)
Eric Anholt673a3942008-07-30 12:06:12 -07003322{
Chris Wilson9af90d12010-10-17 10:01:56 +01003323 struct drm_device *dev = obj->base.dev;
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003324 struct drm_gem_object *target_obj;
3325 uint32_t target_offset;
3326 int ret = -EINVAL;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003327
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003328 target_obj = drm_gem_object_lookup(dev, file_priv,
3329 reloc->target_handle);
3330 if (target_obj == NULL)
3331 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07003332
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003333 target_offset = to_intel_bo(target_obj)->gtt_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003334
Chris Wilson8542a0b2009-09-09 21:15:15 +01003335#if WATCH_RELOC
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003336 DRM_INFO("%s: obj %p offset %08x target %d "
3337 "read %08x write %08x gtt %08x "
3338 "presumed %08x delta %08x\n",
3339 __func__,
3340 obj,
3341 (int) reloc->offset,
3342 (int) reloc->target_handle,
3343 (int) reloc->read_domains,
3344 (int) reloc->write_domain,
3345 (int) target_offset,
3346 (int) reloc->presumed_offset,
3347 reloc->delta);
Chris Wilson8542a0b2009-09-09 21:15:15 +01003348#endif
3349
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003350 /* The target buffer should have appeared before us in the
3351 * exec_object list, so it should have a GTT space bound by now.
3352 */
3353 if (target_offset == 0) {
3354 DRM_ERROR("No GTT space found for object %d\n",
3355 reloc->target_handle);
3356 goto err;
Eric Anholt673a3942008-07-30 12:06:12 -07003357 }
3358
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003359 /* Validate that the target is in a valid r/w GPU domain */
3360 if (reloc->write_domain & (reloc->write_domain - 1)) {
3361 DRM_ERROR("reloc with multiple write domains: "
3362 "obj %p target %d offset %d "
3363 "read %08x write %08x",
3364 obj, reloc->target_handle,
3365 (int) reloc->offset,
3366 reloc->read_domains,
3367 reloc->write_domain);
3368 goto err;
3369 }
3370 if (reloc->write_domain & I915_GEM_DOMAIN_CPU ||
3371 reloc->read_domains & I915_GEM_DOMAIN_CPU) {
3372 DRM_ERROR("reloc with read/write CPU domains: "
3373 "obj %p target %d offset %d "
3374 "read %08x write %08x",
3375 obj, reloc->target_handle,
3376 (int) reloc->offset,
3377 reloc->read_domains,
3378 reloc->write_domain);
3379 goto err;
3380 }
3381 if (reloc->write_domain && target_obj->pending_write_domain &&
3382 reloc->write_domain != target_obj->pending_write_domain) {
3383 DRM_ERROR("Write domain conflict: "
3384 "obj %p target %d offset %d "
3385 "new %08x old %08x\n",
3386 obj, reloc->target_handle,
3387 (int) reloc->offset,
3388 reloc->write_domain,
3389 target_obj->pending_write_domain);
3390 goto err;
3391 }
3392
3393 target_obj->pending_read_domains |= reloc->read_domains;
3394 target_obj->pending_write_domain |= reloc->write_domain;
3395
3396 /* If the relocation already has the right value in it, no
3397 * more work needs to be done.
3398 */
3399 if (target_offset == reloc->presumed_offset)
3400 goto out;
3401
3402 /* Check that the relocation address is valid... */
3403 if (reloc->offset > obj->base.size - 4) {
3404 DRM_ERROR("Relocation beyond object bounds: "
3405 "obj %p target %d offset %d size %d.\n",
3406 obj, reloc->target_handle,
3407 (int) reloc->offset,
3408 (int) obj->base.size);
3409 goto err;
3410 }
3411 if (reloc->offset & 3) {
3412 DRM_ERROR("Relocation not 4-byte aligned: "
3413 "obj %p target %d offset %d.\n",
3414 obj, reloc->target_handle,
3415 (int) reloc->offset);
3416 goto err;
3417 }
3418
3419 /* and points to somewhere within the target object. */
3420 if (reloc->delta >= target_obj->size) {
3421 DRM_ERROR("Relocation beyond target object bounds: "
3422 "obj %p target %d delta %d size %d.\n",
3423 obj, reloc->target_handle,
3424 (int) reloc->delta,
3425 (int) target_obj->size);
3426 goto err;
3427 }
3428
3429 reloc->delta += target_offset;
3430 if (obj->base.write_domain == I915_GEM_DOMAIN_CPU) {
3431 uint32_t page_offset = reloc->offset & ~PAGE_MASK;
3432 char *vaddr;
3433
3434 vaddr = kmap_atomic(obj->pages[reloc->offset >> PAGE_SHIFT]);
3435 *(uint32_t *)(vaddr + page_offset) = reloc->delta;
3436 kunmap_atomic(vaddr);
3437 } else {
3438 struct drm_i915_private *dev_priv = dev->dev_private;
3439 uint32_t __iomem *reloc_entry;
3440 void __iomem *reloc_page;
3441
Chris Wilson05394f32010-11-08 19:18:58 +00003442 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003443 if (ret)
3444 goto err;
3445
3446 /* Map the page containing the relocation we're going to perform. */
3447 reloc->offset += obj->gtt_offset;
3448 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
3449 reloc->offset & PAGE_MASK);
3450 reloc_entry = (uint32_t __iomem *)
3451 (reloc_page + (reloc->offset & ~PAGE_MASK));
3452 iowrite32(reloc->delta, reloc_entry);
3453 io_mapping_unmap_atomic(reloc_page);
3454 }
3455
3456 /* and update the user's relocation entry */
3457 reloc->presumed_offset = target_offset;
3458
3459out:
3460 ret = 0;
3461err:
Chris Wilson9af90d12010-10-17 10:01:56 +01003462 drm_gem_object_unreference(target_obj);
3463 return ret;
3464}
3465
3466static int
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003467i915_gem_execbuffer_relocate_object(struct drm_i915_gem_object *obj,
3468 struct drm_file *file_priv,
3469 struct drm_i915_gem_exec_object2 *entry)
3470{
3471 struct drm_i915_gem_relocation_entry __user *user_relocs;
3472 int i, ret;
3473
3474 user_relocs = (void __user *)(uintptr_t)entry->relocs_ptr;
3475 for (i = 0; i < entry->relocation_count; i++) {
3476 struct drm_i915_gem_relocation_entry reloc;
3477
3478 if (__copy_from_user_inatomic(&reloc,
3479 user_relocs+i,
3480 sizeof(reloc)))
3481 return -EFAULT;
3482
3483 ret = i915_gem_execbuffer_relocate_entry(obj, file_priv, entry, &reloc);
3484 if (ret)
3485 return ret;
3486
3487 if (__copy_to_user_inatomic(&user_relocs[i].presumed_offset,
3488 &reloc.presumed_offset,
3489 sizeof(reloc.presumed_offset)))
3490 return -EFAULT;
3491 }
3492
3493 return 0;
3494}
3495
3496static int
3497i915_gem_execbuffer_relocate_object_slow(struct drm_i915_gem_object *obj,
3498 struct drm_file *file_priv,
3499 struct drm_i915_gem_exec_object2 *entry,
3500 struct drm_i915_gem_relocation_entry *relocs)
3501{
3502 int i, ret;
3503
3504 for (i = 0; i < entry->relocation_count; i++) {
3505 ret = i915_gem_execbuffer_relocate_entry(obj, file_priv, entry, &relocs[i]);
3506 if (ret)
3507 return ret;
3508 }
3509
3510 return 0;
3511}
3512
3513static int
3514i915_gem_execbuffer_relocate(struct drm_device *dev,
3515 struct drm_file *file,
Chris Wilson05394f32010-11-08 19:18:58 +00003516 struct drm_i915_gem_object **object_list,
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003517 struct drm_i915_gem_exec_object2 *exec_list,
3518 int count)
3519{
3520 int i, ret;
3521
3522 for (i = 0; i < count; i++) {
Chris Wilson05394f32010-11-08 19:18:58 +00003523 struct drm_i915_gem_object *obj = object_list[i];
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003524 obj->base.pending_read_domains = 0;
3525 obj->base.pending_write_domain = 0;
3526 ret = i915_gem_execbuffer_relocate_object(obj, file,
3527 &exec_list[i]);
3528 if (ret)
3529 return ret;
3530 }
3531
3532 return 0;
3533}
3534
3535static int
3536i915_gem_execbuffer_reserve(struct drm_device *dev,
3537 struct drm_file *file,
Chris Wilson05394f32010-11-08 19:18:58 +00003538 struct drm_i915_gem_object **object_list,
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003539 struct drm_i915_gem_exec_object2 *exec_list,
3540 int count)
Chris Wilson9af90d12010-10-17 10:01:56 +01003541{
3542 struct drm_i915_private *dev_priv = dev->dev_private;
3543 int ret, i, retry;
3544
3545 /* attempt to pin all of the buffers into the GTT */
Chris Wilson5eac3ab2010-10-31 08:49:47 +00003546 retry = 0;
3547 do {
Chris Wilson9af90d12010-10-17 10:01:56 +01003548 ret = 0;
3549 for (i = 0; i < count; i++) {
3550 struct drm_i915_gem_exec_object2 *entry = &exec_list[i];
Chris Wilson05394f32010-11-08 19:18:58 +00003551 struct drm_i915_gem_object *obj = object_list[i];
Chris Wilson9af90d12010-10-17 10:01:56 +01003552 bool need_fence =
3553 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
3554 obj->tiling_mode != I915_TILING_NONE;
3555
Daniel Vetter16e809a2010-09-16 19:37:04 +02003556 /* g33/pnv can't fence buffers in the unmappable part */
3557 bool need_mappable =
3558 entry->relocation_count ? true : need_fence;
3559
Chris Wilson9af90d12010-10-17 10:01:56 +01003560 /* Check fence reg constraints and rebind if necessary */
Daniel Vetter75e9e912010-11-04 17:11:09 +01003561 if (need_mappable && !obj->map_and_fenceable) {
Chris Wilson05394f32010-11-08 19:18:58 +00003562 ret = i915_gem_object_unbind(obj);
Chris Wilson9af90d12010-10-17 10:01:56 +01003563 if (ret)
3564 break;
3565 }
3566
Chris Wilson05394f32010-11-08 19:18:58 +00003567 ret = i915_gem_object_pin(obj,
Daniel Vetter16e809a2010-09-16 19:37:04 +02003568 entry->alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01003569 need_mappable);
Chris Wilson9af90d12010-10-17 10:01:56 +01003570 if (ret)
3571 break;
3572
3573 /*
3574 * Pre-965 chips need a fence register set up in order
3575 * to properly handle blits to/from tiled surfaces.
3576 */
3577 if (need_fence) {
Chris Wilson05394f32010-11-08 19:18:58 +00003578 ret = i915_gem_object_get_fence_reg(obj, true);
Chris Wilson9af90d12010-10-17 10:01:56 +01003579 if (ret) {
Chris Wilson05394f32010-11-08 19:18:58 +00003580 i915_gem_object_unpin(obj);
Chris Wilson9af90d12010-10-17 10:01:56 +01003581 break;
3582 }
3583
3584 dev_priv->fence_regs[obj->fence_reg].gpu = true;
3585 }
3586
3587 entry->offset = obj->gtt_offset;
3588 }
3589
3590 while (i--)
3591 i915_gem_object_unpin(object_list[i]);
3592
Chris Wilson5eac3ab2010-10-31 08:49:47 +00003593 if (ret != -ENOSPC || retry > 1)
Chris Wilson9af90d12010-10-17 10:01:56 +01003594 return ret;
3595
Chris Wilson5eac3ab2010-10-31 08:49:47 +00003596 /* First attempt, just clear anything that is purgeable.
3597 * Second attempt, clear the entire GTT.
3598 */
3599 ret = i915_gem_evict_everything(dev, retry == 0);
Chris Wilson9af90d12010-10-17 10:01:56 +01003600 if (ret)
3601 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003602
Chris Wilson5eac3ab2010-10-31 08:49:47 +00003603 retry++;
3604 } while (1);
Eric Anholt673a3942008-07-30 12:06:12 -07003605}
3606
Chris Wilson13b29282010-11-01 12:22:48 +00003607static int
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003608i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
3609 struct drm_file *file,
Chris Wilson05394f32010-11-08 19:18:58 +00003610 struct drm_i915_gem_object **object_list,
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003611 struct drm_i915_gem_exec_object2 *exec_list,
3612 int count)
3613{
3614 struct drm_i915_gem_relocation_entry *reloc;
3615 int i, total, ret;
3616
Chris Wilson05394f32010-11-08 19:18:58 +00003617 for (i = 0; i < count; i++)
3618 object_list[i]->in_execbuffer = false;
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003619
3620 mutex_unlock(&dev->struct_mutex);
3621
3622 total = 0;
3623 for (i = 0; i < count; i++)
3624 total += exec_list[i].relocation_count;
3625
3626 reloc = drm_malloc_ab(total, sizeof(*reloc));
3627 if (reloc == NULL) {
3628 mutex_lock(&dev->struct_mutex);
3629 return -ENOMEM;
3630 }
3631
3632 total = 0;
3633 for (i = 0; i < count; i++) {
3634 struct drm_i915_gem_relocation_entry __user *user_relocs;
3635
3636 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3637
3638 if (copy_from_user(reloc+total, user_relocs,
3639 exec_list[i].relocation_count *
3640 sizeof(*reloc))) {
3641 ret = -EFAULT;
3642 mutex_lock(&dev->struct_mutex);
3643 goto err;
3644 }
3645
3646 total += exec_list[i].relocation_count;
3647 }
3648
3649 ret = i915_mutex_lock_interruptible(dev);
3650 if (ret) {
3651 mutex_lock(&dev->struct_mutex);
3652 goto err;
3653 }
3654
3655 ret = i915_gem_execbuffer_reserve(dev, file,
3656 object_list, exec_list,
3657 count);
3658 if (ret)
3659 goto err;
3660
3661 total = 0;
3662 for (i = 0; i < count; i++) {
Chris Wilson05394f32010-11-08 19:18:58 +00003663 struct drm_i915_gem_object *obj = object_list[i];
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003664 obj->base.pending_read_domains = 0;
3665 obj->base.pending_write_domain = 0;
3666 ret = i915_gem_execbuffer_relocate_object_slow(obj, file,
3667 &exec_list[i],
3668 reloc + total);
3669 if (ret)
3670 goto err;
3671
3672 total += exec_list[i].relocation_count;
3673 }
3674
3675 /* Leave the user relocations as are, this is the painfully slow path,
3676 * and we want to avoid the complication of dropping the lock whilst
3677 * having buffers reserved in the aperture and so causing spurious
3678 * ENOSPC for random operations.
3679 */
3680
3681err:
3682 drm_free_large(reloc);
3683 return ret;
3684}
3685
3686static int
Chris Wilson13b29282010-11-01 12:22:48 +00003687i915_gem_execbuffer_move_to_gpu(struct drm_device *dev,
3688 struct drm_file *file,
3689 struct intel_ring_buffer *ring,
Chris Wilson05394f32010-11-08 19:18:58 +00003690 struct drm_i915_gem_object **objects,
Chris Wilson13b29282010-11-01 12:22:48 +00003691 int count)
3692{
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003693 struct change_domains cd;
Chris Wilson13b29282010-11-01 12:22:48 +00003694 int ret, i;
3695
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003696 cd.invalidate_domains = 0;
3697 cd.flush_domains = 0;
3698 cd.flush_rings = 0;
Chris Wilson13b29282010-11-01 12:22:48 +00003699 for (i = 0; i < count; i++)
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003700 i915_gem_object_set_to_gpu_domain(objects[i], ring, &cd);
Chris Wilson13b29282010-11-01 12:22:48 +00003701
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003702 if (cd.invalidate_domains | cd.flush_domains) {
Chris Wilson13b29282010-11-01 12:22:48 +00003703#if WATCH_EXEC
3704 DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
3705 __func__,
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003706 cd.invalidate_domains,
3707 cd.flush_domains);
Chris Wilson13b29282010-11-01 12:22:48 +00003708#endif
Chris Wilson05394f32010-11-08 19:18:58 +00003709 i915_gem_flush(dev,
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003710 cd.invalidate_domains,
3711 cd.flush_domains,
3712 cd.flush_rings);
Chris Wilson13b29282010-11-01 12:22:48 +00003713 }
3714
3715 for (i = 0; i < count; i++) {
Chris Wilson05394f32010-11-08 19:18:58 +00003716 struct drm_i915_gem_object *obj = objects[i];
Chris Wilson13b29282010-11-01 12:22:48 +00003717 /* XXX replace with semaphores */
3718 if (obj->ring && ring != obj->ring) {
Chris Wilson05394f32010-11-08 19:18:58 +00003719 ret = i915_gem_object_wait_rendering(obj, true);
Chris Wilson13b29282010-11-01 12:22:48 +00003720 if (ret)
3721 return ret;
3722 }
3723 }
3724
3725 return 0;
3726}
3727
Eric Anholt673a3942008-07-30 12:06:12 -07003728/* Throttle our rendering by waiting until the ring has completed our requests
3729 * emitted over 20 msec ago.
3730 *
Eric Anholtb9624422009-06-03 07:27:35 +00003731 * Note that if we were to use the current jiffies each time around the loop,
3732 * we wouldn't escape the function with any frames outstanding if the time to
3733 * render a frame was over 20ms.
3734 *
Eric Anholt673a3942008-07-30 12:06:12 -07003735 * This should get us reasonable parallelism between CPU and GPU but also
3736 * relatively low latency when blocking on a particular request to finish.
3737 */
3738static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003739i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003740{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003741 struct drm_i915_private *dev_priv = dev->dev_private;
3742 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00003743 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003744 struct drm_i915_gem_request *request;
3745 struct intel_ring_buffer *ring = NULL;
3746 u32 seqno = 0;
3747 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003748
Chris Wilson1c255952010-09-26 11:03:27 +01003749 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003750 list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
Eric Anholtb9624422009-06-03 07:27:35 +00003751 if (time_after_eq(request->emitted_jiffies, recent_enough))
3752 break;
3753
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003754 ring = request->ring;
3755 seqno = request->seqno;
Eric Anholtb9624422009-06-03 07:27:35 +00003756 }
Chris Wilson1c255952010-09-26 11:03:27 +01003757 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003758
3759 if (seqno == 0)
3760 return 0;
3761
3762 ret = 0;
Chris Wilson78501ea2010-10-27 12:18:21 +01003763 if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) {
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003764 /* And wait for the seqno passing without holding any locks and
3765 * causing extra latency for others. This is safe as the irq
3766 * generation is designed to be run atomically and so is
3767 * lockless.
3768 */
Chris Wilson78501ea2010-10-27 12:18:21 +01003769 ring->user_irq_get(ring);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003770 ret = wait_event_interruptible(ring->irq_queue,
Chris Wilson78501ea2010-10-27 12:18:21 +01003771 i915_seqno_passed(ring->get_seqno(ring), seqno)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003772 || atomic_read(&dev_priv->mm.wedged));
Chris Wilson78501ea2010-10-27 12:18:21 +01003773 ring->user_irq_put(ring);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003774
3775 if (ret == 0 && atomic_read(&dev_priv->mm.wedged))
3776 ret = -EIO;
3777 }
3778
3779 if (ret == 0)
3780 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
Eric Anholtb9624422009-06-03 07:27:35 +00003781
Eric Anholt673a3942008-07-30 12:06:12 -07003782 return ret;
3783}
3784
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003785static int
Chris Wilson2549d6c2010-10-14 12:10:41 +01003786i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec,
3787 uint64_t exec_offset)
Chris Wilson83d60792009-06-06 09:45:57 +01003788{
3789 uint32_t exec_start, exec_len;
3790
3791 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
3792 exec_len = (uint32_t) exec->batch_len;
3793
3794 if ((exec_start | exec_len) & 0x7)
3795 return -EINVAL;
3796
3797 if (!exec_start)
3798 return -EINVAL;
3799
3800 return 0;
3801}
3802
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003803static int
Chris Wilson2549d6c2010-10-14 12:10:41 +01003804validate_exec_list(struct drm_i915_gem_exec_object2 *exec,
3805 int count)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003806{
Chris Wilson2549d6c2010-10-14 12:10:41 +01003807 int i;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003808
Chris Wilson2549d6c2010-10-14 12:10:41 +01003809 for (i = 0; i < count; i++) {
3810 char __user *ptr = (char __user *)(uintptr_t)exec[i].relocs_ptr;
Chris Wilsond1d78832010-11-21 09:23:48 +00003811 int length; /* limited by fault_in_pages_readable() */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003812
Chris Wilsond1d78832010-11-21 09:23:48 +00003813 /* First check for malicious input causing overflow */
3814 if (exec[i].relocation_count >
3815 INT_MAX / sizeof(struct drm_i915_gem_relocation_entry))
3816 return -EINVAL;
3817
3818 length = exec[i].relocation_count *
3819 sizeof(struct drm_i915_gem_relocation_entry);
Chris Wilson2549d6c2010-10-14 12:10:41 +01003820 if (!access_ok(VERIFY_READ, ptr, length))
3821 return -EFAULT;
3822
Chris Wilsonb5dc6082010-10-20 20:59:57 +01003823 /* we may also need to update the presumed offsets */
3824 if (!access_ok(VERIFY_WRITE, ptr, length))
3825 return -EFAULT;
3826
Chris Wilson2549d6c2010-10-14 12:10:41 +01003827 if (fault_in_pages_readable(ptr, length))
3828 return -EFAULT;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003829 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003830
Chris Wilson2549d6c2010-10-14 12:10:41 +01003831 return 0;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003832}
3833
Chris Wilson2549d6c2010-10-14 12:10:41 +01003834static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003835i915_gem_do_execbuffer(struct drm_device *dev, void *data,
Chris Wilson9af90d12010-10-17 10:01:56 +01003836 struct drm_file *file,
Jesse Barnes76446ca2009-12-17 22:05:42 -05003837 struct drm_i915_gem_execbuffer2 *args,
3838 struct drm_i915_gem_exec_object2 *exec_list)
Eric Anholt673a3942008-07-30 12:06:12 -07003839{
3840 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00003841 struct drm_i915_gem_object **object_list = NULL;
3842 struct drm_i915_gem_object *batch_obj;
Eric Anholt201361a2009-03-11 12:30:04 -07003843 struct drm_clip_rect *cliprects = NULL;
Chris Wilson8dc5d142010-08-12 12:36:12 +01003844 struct drm_i915_gem_request *request = NULL;
Chris Wilson9af90d12010-10-17 10:01:56 +01003845 int ret, i, flips;
Eric Anholt673a3942008-07-30 12:06:12 -07003846 uint64_t exec_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003847
Zou Nan hai852835f2010-05-21 09:08:56 +08003848 struct intel_ring_buffer *ring = NULL;
3849
Chris Wilson30dbf0c2010-09-25 10:19:17 +01003850 ret = i915_gem_check_is_wedged(dev);
3851 if (ret)
3852 return ret;
3853
Chris Wilson2549d6c2010-10-14 12:10:41 +01003854 ret = validate_exec_list(exec_list, args->buffer_count);
3855 if (ret)
3856 return ret;
3857
Eric Anholt673a3942008-07-30 12:06:12 -07003858#if WATCH_EXEC
3859 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3860 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3861#endif
Chris Wilson549f7362010-10-19 11:19:32 +01003862 switch (args->flags & I915_EXEC_RING_MASK) {
3863 case I915_EXEC_DEFAULT:
3864 case I915_EXEC_RENDER:
3865 ring = &dev_priv->render_ring;
3866 break;
3867 case I915_EXEC_BSD:
Zou Nan haid1b851f2010-05-21 09:08:57 +08003868 if (!HAS_BSD(dev)) {
Chris Wilson549f7362010-10-19 11:19:32 +01003869 DRM_ERROR("execbuf with invalid ring (BSD)\n");
Zou Nan haid1b851f2010-05-21 09:08:57 +08003870 return -EINVAL;
3871 }
3872 ring = &dev_priv->bsd_ring;
Chris Wilson549f7362010-10-19 11:19:32 +01003873 break;
3874 case I915_EXEC_BLT:
3875 if (!HAS_BLT(dev)) {
3876 DRM_ERROR("execbuf with invalid ring (BLT)\n");
3877 return -EINVAL;
3878 }
3879 ring = &dev_priv->blt_ring;
3880 break;
3881 default:
3882 DRM_ERROR("execbuf with unknown ring: %d\n",
3883 (int)(args->flags & I915_EXEC_RING_MASK));
3884 return -EINVAL;
Zou Nan haid1b851f2010-05-21 09:08:57 +08003885 }
3886
Eric Anholt4f481ed2008-09-10 14:22:49 -07003887 if (args->buffer_count < 1) {
3888 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3889 return -EINVAL;
3890 }
Eric Anholtc8e0f932009-11-22 03:49:37 +01003891 object_list = drm_malloc_ab(sizeof(*object_list), args->buffer_count);
Jesse Barnes76446ca2009-12-17 22:05:42 -05003892 if (object_list == NULL) {
3893 DRM_ERROR("Failed to allocate object list for %d buffers\n",
Eric Anholt673a3942008-07-30 12:06:12 -07003894 args->buffer_count);
3895 ret = -ENOMEM;
3896 goto pre_mutex_err;
3897 }
Eric Anholt673a3942008-07-30 12:06:12 -07003898
Eric Anholt201361a2009-03-11 12:30:04 -07003899 if (args->num_cliprects != 0) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003900 cliprects = kcalloc(args->num_cliprects, sizeof(*cliprects),
3901 GFP_KERNEL);
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003902 if (cliprects == NULL) {
3903 ret = -ENOMEM;
Eric Anholt201361a2009-03-11 12:30:04 -07003904 goto pre_mutex_err;
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003905 }
Eric Anholt201361a2009-03-11 12:30:04 -07003906
3907 ret = copy_from_user(cliprects,
3908 (struct drm_clip_rect __user *)
3909 (uintptr_t) args->cliprects_ptr,
3910 sizeof(*cliprects) * args->num_cliprects);
3911 if (ret != 0) {
3912 DRM_ERROR("copy %d cliprects failed: %d\n",
3913 args->num_cliprects, ret);
Dan Carpenterc877cdc2010-06-23 19:03:01 +02003914 ret = -EFAULT;
Eric Anholt201361a2009-03-11 12:30:04 -07003915 goto pre_mutex_err;
3916 }
3917 }
3918
Chris Wilson8dc5d142010-08-12 12:36:12 +01003919 request = kzalloc(sizeof(*request), GFP_KERNEL);
3920 if (request == NULL) {
3921 ret = -ENOMEM;
Chris Wilsona198bc82009-02-06 16:55:20 +00003922 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003923 }
3924
Chris Wilson76c1dec2010-09-25 11:22:51 +01003925 ret = i915_mutex_lock_interruptible(dev);
3926 if (ret)
3927 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003928
Eric Anholt673a3942008-07-30 12:06:12 -07003929 if (dev_priv->mm.suspended) {
Eric Anholt673a3942008-07-30 12:06:12 -07003930 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003931 ret = -EBUSY;
3932 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003933 }
3934
Keith Packardac94a962008-11-20 23:30:27 -08003935 /* Look up object handles */
Eric Anholt673a3942008-07-30 12:06:12 -07003936 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson05394f32010-11-08 19:18:58 +00003937 struct drm_i915_gem_object *obj;
Chris Wilson7e318e12010-10-27 13:43:39 +01003938
Chris Wilson05394f32010-11-08 19:18:58 +00003939 obj = to_intel_bo (drm_gem_object_lookup(dev, file,
3940 exec_list[i].handle));
3941 if (obj == NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -07003942 DRM_ERROR("Invalid object handle %d at index %d\n",
3943 exec_list[i].handle, i);
Chris Wilson0ce907f2010-01-23 20:26:35 +00003944 /* prevent error path from reading uninitialized data */
Chris Wilson05394f32010-11-08 19:18:58 +00003945 args->buffer_count = i;
Chris Wilsonbf79cb92010-08-04 14:19:46 +01003946 ret = -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07003947 goto err;
3948 }
Chris Wilson05394f32010-11-08 19:18:58 +00003949 object_list[i] = obj;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003950
Chris Wilson05394f32010-11-08 19:18:58 +00003951 if (obj->in_execbuffer) {
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003952 DRM_ERROR("Object %p appears more than once in object list\n",
Chris Wilson05394f32010-11-08 19:18:58 +00003953 obj);
Chris Wilson0ce907f2010-01-23 20:26:35 +00003954 /* prevent error path from reading uninitialized data */
3955 args->buffer_count = i + 1;
Chris Wilsonbf79cb92010-08-04 14:19:46 +01003956 ret = -EINVAL;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003957 goto err;
3958 }
Chris Wilson05394f32010-11-08 19:18:58 +00003959 obj->in_execbuffer = true;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003960 }
3961
Chris Wilson9af90d12010-10-17 10:01:56 +01003962 /* Move the objects en-masse into the GTT, evicting if necessary. */
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003963 ret = i915_gem_execbuffer_reserve(dev, file,
3964 object_list, exec_list,
3965 args->buffer_count);
Chris Wilson9af90d12010-10-17 10:01:56 +01003966 if (ret)
3967 goto err;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003968
Chris Wilson9af90d12010-10-17 10:01:56 +01003969 /* The objects are in their final locations, apply the relocations. */
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003970 ret = i915_gem_execbuffer_relocate(dev, file,
3971 object_list, exec_list,
3972 args->buffer_count);
3973 if (ret) {
3974 if (ret == -EFAULT) {
3975 ret = i915_gem_execbuffer_relocate_slow(dev, file,
3976 object_list,
3977 exec_list,
3978 args->buffer_count);
3979 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
3980 }
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003981 if (ret)
3982 goto err;
3983 }
3984
Eric Anholt673a3942008-07-30 12:06:12 -07003985 /* Set the pending read domains for the batch buffer to COMMAND */
3986 batch_obj = object_list[args->buffer_count-1];
Chris Wilson05394f32010-11-08 19:18:58 +00003987 if (batch_obj->base.pending_write_domain) {
Chris Wilson5f26a2c2009-06-06 09:45:58 +01003988 DRM_ERROR("Attempting to use self-modifying batch buffer\n");
3989 ret = -EINVAL;
3990 goto err;
3991 }
Chris Wilson05394f32010-11-08 19:18:58 +00003992 batch_obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
Eric Anholt673a3942008-07-30 12:06:12 -07003993
Chris Wilson9af90d12010-10-17 10:01:56 +01003994 /* Sanity check the batch buffer */
Chris Wilson05394f32010-11-08 19:18:58 +00003995 exec_offset = batch_obj->gtt_offset;
Chris Wilson9af90d12010-10-17 10:01:56 +01003996 ret = i915_gem_check_execbuffer(args, exec_offset);
Chris Wilson83d60792009-06-06 09:45:57 +01003997 if (ret != 0) {
3998 DRM_ERROR("execbuf with invalid offset/length\n");
3999 goto err;
4000 }
4001
Chris Wilson13b29282010-11-01 12:22:48 +00004002 ret = i915_gem_execbuffer_move_to_gpu(dev, file, ring,
4003 object_list, args->buffer_count);
4004 if (ret)
4005 goto err;
Eric Anholt673a3942008-07-30 12:06:12 -07004006
Eric Anholt673a3942008-07-30 12:06:12 -07004007#if WATCH_COHERENCY
4008 for (i = 0; i < args->buffer_count; i++) {
4009 i915_gem_object_check_coherency(object_list[i],
4010 exec_list[i].handle);
4011 }
4012#endif
4013
Eric Anholt673a3942008-07-30 12:06:12 -07004014#if WATCH_EXEC
Ben Gamari6911a9b2009-04-02 11:24:54 -07004015 i915_gem_dump_object(batch_obj,
Eric Anholt673a3942008-07-30 12:06:12 -07004016 args->batch_len,
4017 __func__,
4018 ~0);
4019#endif
4020
Chris Wilsone59f2ba2010-10-07 17:28:15 +01004021 /* Check for any pending flips. As we only maintain a flip queue depth
4022 * of 1, we can simply insert a WAIT for the next display flip prior
4023 * to executing the batch and avoid stalling the CPU.
4024 */
4025 flips = 0;
4026 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson05394f32010-11-08 19:18:58 +00004027 if (object_list[i]->base.write_domain)
4028 flips |= atomic_read(&object_list[i]->pending_flip);
Chris Wilsone59f2ba2010-10-07 17:28:15 +01004029 }
4030 if (flips) {
4031 int plane, flip_mask;
4032
4033 for (plane = 0; flips >> plane; plane++) {
4034 if (((flips >> plane) & 1) == 0)
4035 continue;
4036
4037 if (plane)
4038 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
4039 else
4040 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
4041
Chris Wilsone1f99ce2010-10-27 12:45:26 +01004042 ret = intel_ring_begin(ring, 2);
4043 if (ret)
4044 goto err;
4045
Chris Wilson78501ea2010-10-27 12:18:21 +01004046 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
4047 intel_ring_emit(ring, MI_NOOP);
4048 intel_ring_advance(ring);
Chris Wilsone59f2ba2010-10-07 17:28:15 +01004049 }
4050 }
4051
Eric Anholt673a3942008-07-30 12:06:12 -07004052 /* Exec the batchbuffer */
Chris Wilson78501ea2010-10-27 12:18:21 +01004053 ret = ring->dispatch_execbuffer(ring, args, cliprects, exec_offset);
Eric Anholt673a3942008-07-30 12:06:12 -07004054 if (ret) {
4055 DRM_ERROR("dispatch failed %d\n", ret);
4056 goto err;
4057 }
4058
Chris Wilson7e318e12010-10-27 13:43:39 +01004059 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson05394f32010-11-08 19:18:58 +00004060 struct drm_i915_gem_object *obj = object_list[i];
Chris Wilson7e318e12010-10-27 13:43:39 +01004061
Chris Wilson05394f32010-11-08 19:18:58 +00004062 obj->base.read_domains = obj->base.pending_read_domains;
4063 obj->base.write_domain = obj->base.pending_write_domain;
Chris Wilson7e318e12010-10-27 13:43:39 +01004064
4065 i915_gem_object_move_to_active(obj, ring);
Chris Wilson05394f32010-11-08 19:18:58 +00004066 if (obj->base.write_domain) {
4067 obj->dirty = 1;
4068 list_move_tail(&obj->gpu_write_list,
Chris Wilson7e318e12010-10-27 13:43:39 +01004069 &ring->gpu_write_list);
4070 intel_mark_busy(dev, obj);
4071 }
4072
4073 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00004074 obj->base.read_domains,
4075 obj->base.write_domain);
Chris Wilson7e318e12010-10-27 13:43:39 +01004076 }
4077
Eric Anholt673a3942008-07-30 12:06:12 -07004078 /*
4079 * Ensure that the commands in the batch buffer are
4080 * finished before the interrupt fires
4081 */
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01004082 i915_retire_commands(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07004083
Chris Wilson3cce4692010-10-27 16:11:02 +01004084 if (i915_add_request(dev, file, request, ring))
Chris Wilson5d97eb62010-11-10 20:40:02 +00004085 i915_gem_next_request_seqno(dev, ring);
Chris Wilson3cce4692010-10-27 16:11:02 +01004086 else
4087 request = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07004088
Eric Anholt673a3942008-07-30 12:06:12 -07004089err:
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05004090 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson05394f32010-11-08 19:18:58 +00004091 object_list[i]->in_execbuffer = false;
4092 drm_gem_object_unreference(&object_list[i]->base);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05004093 }
Julia Lawallaad87df2008-12-21 16:28:47 +01004094
Eric Anholt673a3942008-07-30 12:06:12 -07004095 mutex_unlock(&dev->struct_mutex);
4096
Chris Wilson93533c22010-01-31 10:40:48 +00004097pre_mutex_err:
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07004098 drm_free_large(object_list);
Eric Anholt9a298b22009-03-24 12:23:04 -07004099 kfree(cliprects);
Chris Wilson8dc5d142010-08-12 12:36:12 +01004100 kfree(request);
Eric Anholt673a3942008-07-30 12:06:12 -07004101
4102 return ret;
4103}
4104
Jesse Barnes76446ca2009-12-17 22:05:42 -05004105/*
4106 * Legacy execbuffer just creates an exec2 list from the original exec object
4107 * list array and passes it to the real function.
4108 */
4109int
4110i915_gem_execbuffer(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00004111 struct drm_file *file)
Jesse Barnes76446ca2009-12-17 22:05:42 -05004112{
4113 struct drm_i915_gem_execbuffer *args = data;
4114 struct drm_i915_gem_execbuffer2 exec2;
4115 struct drm_i915_gem_exec_object *exec_list = NULL;
4116 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
4117 int ret, i;
4118
4119#if WATCH_EXEC
4120 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
4121 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
4122#endif
4123
4124 if (args->buffer_count < 1) {
4125 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
4126 return -EINVAL;
4127 }
4128
4129 /* Copy in the exec list from userland */
4130 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
4131 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
4132 if (exec_list == NULL || exec2_list == NULL) {
4133 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
4134 args->buffer_count);
4135 drm_free_large(exec_list);
4136 drm_free_large(exec2_list);
4137 return -ENOMEM;
4138 }
4139 ret = copy_from_user(exec_list,
4140 (struct drm_i915_relocation_entry __user *)
4141 (uintptr_t) args->buffers_ptr,
4142 sizeof(*exec_list) * args->buffer_count);
4143 if (ret != 0) {
4144 DRM_ERROR("copy %d exec entries failed %d\n",
4145 args->buffer_count, ret);
4146 drm_free_large(exec_list);
4147 drm_free_large(exec2_list);
4148 return -EFAULT;
4149 }
4150
4151 for (i = 0; i < args->buffer_count; i++) {
4152 exec2_list[i].handle = exec_list[i].handle;
4153 exec2_list[i].relocation_count = exec_list[i].relocation_count;
4154 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
4155 exec2_list[i].alignment = exec_list[i].alignment;
4156 exec2_list[i].offset = exec_list[i].offset;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004157 if (INTEL_INFO(dev)->gen < 4)
Jesse Barnes76446ca2009-12-17 22:05:42 -05004158 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
4159 else
4160 exec2_list[i].flags = 0;
4161 }
4162
4163 exec2.buffers_ptr = args->buffers_ptr;
4164 exec2.buffer_count = args->buffer_count;
4165 exec2.batch_start_offset = args->batch_start_offset;
4166 exec2.batch_len = args->batch_len;
4167 exec2.DR1 = args->DR1;
4168 exec2.DR4 = args->DR4;
4169 exec2.num_cliprects = args->num_cliprects;
4170 exec2.cliprects_ptr = args->cliprects_ptr;
Zou Nan hai852835f2010-05-21 09:08:56 +08004171 exec2.flags = I915_EXEC_RENDER;
Jesse Barnes76446ca2009-12-17 22:05:42 -05004172
Chris Wilson05394f32010-11-08 19:18:58 +00004173 ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list);
Jesse Barnes76446ca2009-12-17 22:05:42 -05004174 if (!ret) {
4175 /* Copy the new buffer offsets back to the user's exec list. */
4176 for (i = 0; i < args->buffer_count; i++)
4177 exec_list[i].offset = exec2_list[i].offset;
4178 /* ... and back out to userspace */
4179 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
4180 (uintptr_t) args->buffers_ptr,
4181 exec_list,
4182 sizeof(*exec_list) * args->buffer_count);
4183 if (ret) {
4184 ret = -EFAULT;
4185 DRM_ERROR("failed to copy %d exec entries "
4186 "back to user (%d)\n",
4187 args->buffer_count, ret);
4188 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05004189 }
4190
4191 drm_free_large(exec_list);
4192 drm_free_large(exec2_list);
4193 return ret;
4194}
4195
4196int
4197i915_gem_execbuffer2(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00004198 struct drm_file *file)
Jesse Barnes76446ca2009-12-17 22:05:42 -05004199{
4200 struct drm_i915_gem_execbuffer2 *args = data;
4201 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
4202 int ret;
4203
4204#if WATCH_EXEC
4205 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
4206 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
4207#endif
4208
4209 if (args->buffer_count < 1) {
4210 DRM_ERROR("execbuf2 with %d buffers\n", args->buffer_count);
4211 return -EINVAL;
4212 }
4213
4214 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
4215 if (exec2_list == NULL) {
4216 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
4217 args->buffer_count);
4218 return -ENOMEM;
4219 }
4220 ret = copy_from_user(exec2_list,
4221 (struct drm_i915_relocation_entry __user *)
4222 (uintptr_t) args->buffers_ptr,
4223 sizeof(*exec2_list) * args->buffer_count);
4224 if (ret != 0) {
4225 DRM_ERROR("copy %d exec entries failed %d\n",
4226 args->buffer_count, ret);
4227 drm_free_large(exec2_list);
4228 return -EFAULT;
4229 }
4230
Chris Wilson05394f32010-11-08 19:18:58 +00004231 ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list);
Jesse Barnes76446ca2009-12-17 22:05:42 -05004232 if (!ret) {
4233 /* Copy the new buffer offsets back to the user's exec list. */
4234 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
4235 (uintptr_t) args->buffers_ptr,
4236 exec2_list,
4237 sizeof(*exec2_list) * args->buffer_count);
4238 if (ret) {
4239 ret = -EFAULT;
4240 DRM_ERROR("failed to copy %d exec entries "
4241 "back to user (%d)\n",
4242 args->buffer_count, ret);
4243 }
4244 }
4245
4246 drm_free_large(exec2_list);
4247 return ret;
4248}
4249
Eric Anholt673a3942008-07-30 12:06:12 -07004250int
Chris Wilson05394f32010-11-08 19:18:58 +00004251i915_gem_object_pin(struct drm_i915_gem_object *obj,
4252 uint32_t alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01004253 bool map_and_fenceable)
Eric Anholt673a3942008-07-30 12:06:12 -07004254{
Chris Wilson05394f32010-11-08 19:18:58 +00004255 struct drm_device *dev = obj->base.dev;
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004256 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07004257 int ret;
4258
Chris Wilson05394f32010-11-08 19:18:58 +00004259 BUG_ON(obj->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT);
Daniel Vetter75e9e912010-11-04 17:11:09 +01004260 BUG_ON(map_and_fenceable && !map_and_fenceable);
Chris Wilson23bc5982010-09-29 16:10:57 +01004261 WARN_ON(i915_verify_lists(dev));
Chris Wilsonac0c6b52010-05-27 13:18:18 +01004262
Chris Wilson05394f32010-11-08 19:18:58 +00004263 if (obj->gtt_space != NULL) {
4264 if ((alignment && obj->gtt_offset & (alignment - 1)) ||
4265 (map_and_fenceable && !obj->map_and_fenceable)) {
4266 WARN(obj->pin_count,
Chris Wilsonae7d49d2010-08-04 12:37:41 +01004267 "bo is already pinned with incorrect alignment:"
Daniel Vetter75e9e912010-11-04 17:11:09 +01004268 " offset=%x, req.alignment=%x, req.map_and_fenceable=%d,"
4269 " obj->map_and_fenceable=%d\n",
Chris Wilson05394f32010-11-08 19:18:58 +00004270 obj->gtt_offset, alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01004271 map_and_fenceable,
Chris Wilson05394f32010-11-08 19:18:58 +00004272 obj->map_and_fenceable);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01004273 ret = i915_gem_object_unbind(obj);
4274 if (ret)
4275 return ret;
4276 }
4277 }
4278
Chris Wilson05394f32010-11-08 19:18:58 +00004279 if (obj->gtt_space == NULL) {
Chris Wilsona00b10c2010-09-24 21:15:47 +01004280 ret = i915_gem_object_bind_to_gtt(obj, alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01004281 map_and_fenceable);
Chris Wilson97311292009-09-21 00:22:34 +01004282 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07004283 return ret;
Chris Wilson22c344e2009-02-11 14:26:45 +00004284 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05004285
Chris Wilson05394f32010-11-08 19:18:58 +00004286 if (obj->pin_count++ == 0) {
4287 i915_gem_info_add_pin(dev_priv, obj, map_and_fenceable);
4288 if (!obj->active)
4289 list_move_tail(&obj->mm_list,
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004290 &dev_priv->mm.pinned_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004291 }
Chris Wilson05394f32010-11-08 19:18:58 +00004292 BUG_ON(!obj->pin_mappable && map_and_fenceable);
Eric Anholt673a3942008-07-30 12:06:12 -07004293
Chris Wilson23bc5982010-09-29 16:10:57 +01004294 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07004295 return 0;
4296}
4297
4298void
Chris Wilson05394f32010-11-08 19:18:58 +00004299i915_gem_object_unpin(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07004300{
Chris Wilson05394f32010-11-08 19:18:58 +00004301 struct drm_device *dev = obj->base.dev;
Eric Anholt673a3942008-07-30 12:06:12 -07004302 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07004303
Chris Wilson23bc5982010-09-29 16:10:57 +01004304 WARN_ON(i915_verify_lists(dev));
Chris Wilson05394f32010-11-08 19:18:58 +00004305 BUG_ON(obj->pin_count == 0);
4306 BUG_ON(obj->gtt_space == NULL);
Eric Anholt673a3942008-07-30 12:06:12 -07004307
Chris Wilson05394f32010-11-08 19:18:58 +00004308 if (--obj->pin_count == 0) {
4309 if (!obj->active)
4310 list_move_tail(&obj->mm_list,
Eric Anholt673a3942008-07-30 12:06:12 -07004311 &dev_priv->mm.inactive_list);
Chris Wilson05394f32010-11-08 19:18:58 +00004312 i915_gem_info_remove_pin(dev_priv, obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004313 }
Chris Wilson23bc5982010-09-29 16:10:57 +01004314 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07004315}
4316
4317int
4318i915_gem_pin_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00004319 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07004320{
4321 struct drm_i915_gem_pin *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004322 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07004323 int ret;
4324
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004325 ret = i915_mutex_lock_interruptible(dev);
4326 if (ret)
4327 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004328
Chris Wilson05394f32010-11-08 19:18:58 +00004329 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Eric Anholt673a3942008-07-30 12:06:12 -07004330 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004331 ret = -ENOENT;
4332 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07004333 }
Eric Anholt673a3942008-07-30 12:06:12 -07004334
Chris Wilson05394f32010-11-08 19:18:58 +00004335 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004336 DRM_ERROR("Attempting to pin a purgeable buffer\n");
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004337 ret = -EINVAL;
4338 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004339 }
4340
Chris Wilson05394f32010-11-08 19:18:58 +00004341 if (obj->pin_filp != NULL && obj->pin_filp != file) {
Jesse Barnes79e53942008-11-07 14:24:08 -08004342 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
4343 args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004344 ret = -EINVAL;
4345 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08004346 }
4347
Chris Wilson05394f32010-11-08 19:18:58 +00004348 obj->user_pin_count++;
4349 obj->pin_filp = file;
4350 if (obj->user_pin_count == 1) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01004351 ret = i915_gem_object_pin(obj, args->alignment, true);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004352 if (ret)
4353 goto out;
Eric Anholt673a3942008-07-30 12:06:12 -07004354 }
4355
4356 /* XXX - flush the CPU caches for pinned objects
4357 * as the X server doesn't manage domains yet
4358 */
Eric Anholte47c68e2008-11-14 13:35:19 -08004359 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00004360 args->offset = obj->gtt_offset;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004361out:
Chris Wilson05394f32010-11-08 19:18:58 +00004362 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004363unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07004364 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004365 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004366}
4367
4368int
4369i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00004370 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07004371{
4372 struct drm_i915_gem_pin *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004373 struct drm_i915_gem_object *obj;
Chris Wilson76c1dec2010-09-25 11:22:51 +01004374 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004375
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004376 ret = i915_mutex_lock_interruptible(dev);
4377 if (ret)
4378 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004379
Chris Wilson05394f32010-11-08 19:18:58 +00004380 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Eric Anholt673a3942008-07-30 12:06:12 -07004381 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004382 ret = -ENOENT;
4383 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07004384 }
Chris Wilson76c1dec2010-09-25 11:22:51 +01004385
Chris Wilson05394f32010-11-08 19:18:58 +00004386 if (obj->pin_filp != file) {
Jesse Barnes79e53942008-11-07 14:24:08 -08004387 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
4388 args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004389 ret = -EINVAL;
4390 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08004391 }
Chris Wilson05394f32010-11-08 19:18:58 +00004392 obj->user_pin_count--;
4393 if (obj->user_pin_count == 0) {
4394 obj->pin_filp = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08004395 i915_gem_object_unpin(obj);
4396 }
Eric Anholt673a3942008-07-30 12:06:12 -07004397
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004398out:
Chris Wilson05394f32010-11-08 19:18:58 +00004399 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004400unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07004401 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004402 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004403}
4404
4405int
4406i915_gem_busy_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00004407 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07004408{
4409 struct drm_i915_gem_busy *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004410 struct drm_i915_gem_object *obj;
Chris Wilson30dbf0c2010-09-25 10:19:17 +01004411 int ret;
4412
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004413 ret = i915_mutex_lock_interruptible(dev);
4414 if (ret)
4415 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004416
Chris Wilson05394f32010-11-08 19:18:58 +00004417 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Eric Anholt673a3942008-07-30 12:06:12 -07004418 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004419 ret = -ENOENT;
4420 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07004421 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08004422
Chris Wilson0be555b2010-08-04 15:36:30 +01004423 /* Count all active objects as busy, even if they are currently not used
4424 * by the gpu. Users of this interface expect objects to eventually
4425 * become non-busy without any further actions, therefore emit any
4426 * necessary flushes here.
Eric Anholtc4de0a52008-12-14 19:05:04 -08004427 */
Chris Wilson05394f32010-11-08 19:18:58 +00004428 args->busy = obj->active;
Chris Wilson0be555b2010-08-04 15:36:30 +01004429 if (args->busy) {
4430 /* Unconditionally flush objects, even when the gpu still uses this
4431 * object. Userspace calling this function indicates that it wants to
4432 * use this buffer rather sooner than later, so issuing the required
4433 * flush earlier is beneficial.
4434 */
Chris Wilson05394f32010-11-08 19:18:58 +00004435 if (obj->base.write_domain & I915_GEM_GPU_DOMAINS)
4436 i915_gem_flush_ring(dev, obj->ring,
4437 0, obj->base.write_domain);
Chris Wilson0be555b2010-08-04 15:36:30 +01004438
4439 /* Update the active list for the hardware's current position.
4440 * Otherwise this only updates on a delayed timer or when irqs
4441 * are actually unmasked, and our working set ends up being
4442 * larger than required.
4443 */
Chris Wilson05394f32010-11-08 19:18:58 +00004444 i915_gem_retire_requests_ring(dev, obj->ring);
Chris Wilson0be555b2010-08-04 15:36:30 +01004445
Chris Wilson05394f32010-11-08 19:18:58 +00004446 args->busy = obj->active;
Chris Wilson0be555b2010-08-04 15:36:30 +01004447 }
Eric Anholt673a3942008-07-30 12:06:12 -07004448
Chris Wilson05394f32010-11-08 19:18:58 +00004449 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004450unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07004451 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004452 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004453}
4454
4455int
4456i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4457 struct drm_file *file_priv)
4458{
4459 return i915_gem_ring_throttle(dev, file_priv);
4460}
4461
Chris Wilson3ef94da2009-09-14 16:50:29 +01004462int
4463i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4464 struct drm_file *file_priv)
4465{
4466 struct drm_i915_gem_madvise *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004467 struct drm_i915_gem_object *obj;
Chris Wilson76c1dec2010-09-25 11:22:51 +01004468 int ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004469
4470 switch (args->madv) {
4471 case I915_MADV_DONTNEED:
4472 case I915_MADV_WILLNEED:
4473 break;
4474 default:
4475 return -EINVAL;
4476 }
4477
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004478 ret = i915_mutex_lock_interruptible(dev);
4479 if (ret)
4480 return ret;
4481
Chris Wilson05394f32010-11-08 19:18:58 +00004482 obj = to_intel_bo(drm_gem_object_lookup(dev, file_priv, args->handle));
Chris Wilson3ef94da2009-09-14 16:50:29 +01004483 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004484 ret = -ENOENT;
4485 goto unlock;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004486 }
Chris Wilson3ef94da2009-09-14 16:50:29 +01004487
Chris Wilson05394f32010-11-08 19:18:58 +00004488 if (obj->pin_count) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004489 ret = -EINVAL;
4490 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004491 }
4492
Chris Wilson05394f32010-11-08 19:18:58 +00004493 if (obj->madv != __I915_MADV_PURGED)
4494 obj->madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004495
Chris Wilson2d7ef392009-09-20 23:13:10 +01004496 /* if the object is no longer bound, discard its backing storage */
Chris Wilson05394f32010-11-08 19:18:58 +00004497 if (i915_gem_object_is_purgeable(obj) &&
4498 obj->gtt_space == NULL)
Chris Wilson2d7ef392009-09-20 23:13:10 +01004499 i915_gem_object_truncate(obj);
4500
Chris Wilson05394f32010-11-08 19:18:58 +00004501 args->retained = obj->madv != __I915_MADV_PURGED;
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004502
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004503out:
Chris Wilson05394f32010-11-08 19:18:58 +00004504 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004505unlock:
Chris Wilson3ef94da2009-09-14 16:50:29 +01004506 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004507 return ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004508}
4509
Chris Wilson05394f32010-11-08 19:18:58 +00004510struct drm_i915_gem_object *i915_gem_alloc_object(struct drm_device *dev,
4511 size_t size)
Daniel Vetterac52bc52010-04-09 19:05:06 +00004512{
Chris Wilson73aa8082010-09-30 11:46:12 +01004513 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc397b902010-04-09 19:05:07 +00004514 struct drm_i915_gem_object *obj;
4515
4516 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
4517 if (obj == NULL)
4518 return NULL;
4519
4520 if (drm_gem_object_init(dev, &obj->base, size) != 0) {
4521 kfree(obj);
4522 return NULL;
4523 }
4524
Chris Wilson73aa8082010-09-30 11:46:12 +01004525 i915_gem_info_add_obj(dev_priv, size);
4526
Daniel Vetterc397b902010-04-09 19:05:07 +00004527 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4528 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4529
4530 obj->agp_type = AGP_USER_MEMORY;
Daniel Vetter62b8b212010-04-09 19:05:08 +00004531 obj->base.driver_private = NULL;
Daniel Vetterc397b902010-04-09 19:05:07 +00004532 obj->fence_reg = I915_FENCE_REG_NONE;
Chris Wilson69dc4982010-10-19 10:36:51 +01004533 INIT_LIST_HEAD(&obj->mm_list);
Daniel Vetter93a37f22010-11-05 20:24:53 +01004534 INIT_LIST_HEAD(&obj->gtt_list);
Chris Wilson69dc4982010-10-19 10:36:51 +01004535 INIT_LIST_HEAD(&obj->ring_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00004536 INIT_LIST_HEAD(&obj->gpu_write_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00004537 obj->madv = I915_MADV_WILLNEED;
Daniel Vetter75e9e912010-11-04 17:11:09 +01004538 /* Avoid an unnecessary call to unbind on the first bind. */
4539 obj->map_and_fenceable = true;
Daniel Vetterc397b902010-04-09 19:05:07 +00004540
Chris Wilson05394f32010-11-08 19:18:58 +00004541 return obj;
Daniel Vetterac52bc52010-04-09 19:05:06 +00004542}
4543
Eric Anholt673a3942008-07-30 12:06:12 -07004544int i915_gem_init_object(struct drm_gem_object *obj)
4545{
Daniel Vetterc397b902010-04-09 19:05:07 +00004546 BUG();
Jesse Barnesde151cf2008-11-12 10:03:55 -08004547
Eric Anholt673a3942008-07-30 12:06:12 -07004548 return 0;
4549}
4550
Chris Wilson05394f32010-11-08 19:18:58 +00004551static void i915_gem_free_object_tail(struct drm_i915_gem_object *obj)
Chris Wilsonbe726152010-07-23 23:18:50 +01004552{
Chris Wilson05394f32010-11-08 19:18:58 +00004553 struct drm_device *dev = obj->base.dev;
Chris Wilsonbe726152010-07-23 23:18:50 +01004554 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonbe726152010-07-23 23:18:50 +01004555 int ret;
4556
4557 ret = i915_gem_object_unbind(obj);
4558 if (ret == -ERESTARTSYS) {
Chris Wilson05394f32010-11-08 19:18:58 +00004559 list_move(&obj->mm_list,
Chris Wilsonbe726152010-07-23 23:18:50 +01004560 &dev_priv->mm.deferred_free_list);
4561 return;
4562 }
4563
Chris Wilson05394f32010-11-08 19:18:58 +00004564 if (obj->base.map_list.map)
Chris Wilsonbe726152010-07-23 23:18:50 +01004565 i915_gem_free_mmap_offset(obj);
4566
Chris Wilson05394f32010-11-08 19:18:58 +00004567 drm_gem_object_release(&obj->base);
4568 i915_gem_info_remove_obj(dev_priv, obj->base.size);
Chris Wilsonbe726152010-07-23 23:18:50 +01004569
Chris Wilson05394f32010-11-08 19:18:58 +00004570 kfree(obj->page_cpu_valid);
4571 kfree(obj->bit_17);
4572 kfree(obj);
Chris Wilsonbe726152010-07-23 23:18:50 +01004573}
4574
Chris Wilson05394f32010-11-08 19:18:58 +00004575void i915_gem_free_object(struct drm_gem_object *gem_obj)
Eric Anholt673a3942008-07-30 12:06:12 -07004576{
Chris Wilson05394f32010-11-08 19:18:58 +00004577 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
4578 struct drm_device *dev = obj->base.dev;
Eric Anholt673a3942008-07-30 12:06:12 -07004579
Chris Wilson1c5d22f2009-08-25 11:15:50 +01004580 trace_i915_gem_object_destroy(obj);
4581
Chris Wilson05394f32010-11-08 19:18:58 +00004582 while (obj->pin_count > 0)
Eric Anholt673a3942008-07-30 12:06:12 -07004583 i915_gem_object_unpin(obj);
4584
Chris Wilson05394f32010-11-08 19:18:58 +00004585 if (obj->phys_obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004586 i915_gem_detach_phys_object(dev, obj);
4587
Chris Wilsonbe726152010-07-23 23:18:50 +01004588 i915_gem_free_object_tail(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004589}
4590
Jesse Barnes5669fca2009-02-17 15:13:31 -08004591int
Eric Anholt673a3942008-07-30 12:06:12 -07004592i915_gem_idle(struct drm_device *dev)
4593{
4594 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson29105cc2010-01-07 10:39:13 +00004595 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004596
Keith Packard6dbe2772008-10-14 21:41:13 -07004597 mutex_lock(&dev->struct_mutex);
4598
Chris Wilson87acb0a2010-10-19 10:13:00 +01004599 if (dev_priv->mm.suspended) {
Keith Packard6dbe2772008-10-14 21:41:13 -07004600 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004601 return 0;
Keith Packard6dbe2772008-10-14 21:41:13 -07004602 }
Eric Anholt673a3942008-07-30 12:06:12 -07004603
Chris Wilson29105cc2010-01-07 10:39:13 +00004604 ret = i915_gpu_idle(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004605 if (ret) {
4606 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004607 return ret;
Keith Packard6dbe2772008-10-14 21:41:13 -07004608 }
Eric Anholt673a3942008-07-30 12:06:12 -07004609
Chris Wilson29105cc2010-01-07 10:39:13 +00004610 /* Under UMS, be paranoid and evict. */
4611 if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
Chris Wilson5eac3ab2010-10-31 08:49:47 +00004612 ret = i915_gem_evict_inactive(dev, false);
Chris Wilson29105cc2010-01-07 10:39:13 +00004613 if (ret) {
4614 mutex_unlock(&dev->struct_mutex);
4615 return ret;
4616 }
4617 }
4618
4619 /* Hack! Don't let anybody do execbuf while we don't control the chip.
4620 * We need to replace this with a semaphore, or something.
4621 * And not confound mm.suspended!
4622 */
4623 dev_priv->mm.suspended = 1;
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02004624 del_timer_sync(&dev_priv->hangcheck_timer);
Chris Wilson29105cc2010-01-07 10:39:13 +00004625
4626 i915_kernel_lost_context(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004627 i915_gem_cleanup_ringbuffer(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00004628
Keith Packard6dbe2772008-10-14 21:41:13 -07004629 mutex_unlock(&dev->struct_mutex);
4630
Chris Wilson29105cc2010-01-07 10:39:13 +00004631 /* Cancel the retire work handler, which should be idle now. */
4632 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
4633
Eric Anholt673a3942008-07-30 12:06:12 -07004634 return 0;
4635}
4636
Jesse Barnese552eb72010-04-21 11:39:23 -07004637/*
4638 * 965+ support PIPE_CONTROL commands, which provide finer grained control
4639 * over cache flushing.
4640 */
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004641static int
Jesse Barnese552eb72010-04-21 11:39:23 -07004642i915_gem_init_pipe_control(struct drm_device *dev)
4643{
4644 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00004645 struct drm_i915_gem_object *obj;
Jesse Barnese552eb72010-04-21 11:39:23 -07004646 int ret;
4647
Eric Anholt34dc4d42010-05-07 14:30:03 -07004648 obj = i915_gem_alloc_object(dev, 4096);
Jesse Barnese552eb72010-04-21 11:39:23 -07004649 if (obj == NULL) {
4650 DRM_ERROR("Failed to allocate seqno page\n");
4651 ret = -ENOMEM;
4652 goto err;
4653 }
Chris Wilson05394f32010-11-08 19:18:58 +00004654 obj->agp_type = AGP_USER_CACHED_MEMORY;
Jesse Barnese552eb72010-04-21 11:39:23 -07004655
Daniel Vetter75e9e912010-11-04 17:11:09 +01004656 ret = i915_gem_object_pin(obj, 4096, true);
Jesse Barnese552eb72010-04-21 11:39:23 -07004657 if (ret)
4658 goto err_unref;
4659
Chris Wilson05394f32010-11-08 19:18:58 +00004660 dev_priv->seqno_gfx_addr = obj->gtt_offset;
4661 dev_priv->seqno_page = kmap(obj->pages[0]);
Jesse Barnese552eb72010-04-21 11:39:23 -07004662 if (dev_priv->seqno_page == NULL)
4663 goto err_unpin;
4664
4665 dev_priv->seqno_obj = obj;
4666 memset(dev_priv->seqno_page, 0, PAGE_SIZE);
4667
4668 return 0;
4669
4670err_unpin:
4671 i915_gem_object_unpin(obj);
4672err_unref:
Chris Wilson05394f32010-11-08 19:18:58 +00004673 drm_gem_object_unreference(&obj->base);
Jesse Barnese552eb72010-04-21 11:39:23 -07004674err:
4675 return ret;
4676}
4677
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004678
4679static void
Jesse Barnese552eb72010-04-21 11:39:23 -07004680i915_gem_cleanup_pipe_control(struct drm_device *dev)
4681{
4682 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00004683 struct drm_i915_gem_object *obj;
Jesse Barnese552eb72010-04-21 11:39:23 -07004684
4685 obj = dev_priv->seqno_obj;
Chris Wilson05394f32010-11-08 19:18:58 +00004686 kunmap(obj->pages[0]);
Jesse Barnese552eb72010-04-21 11:39:23 -07004687 i915_gem_object_unpin(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00004688 drm_gem_object_unreference(&obj->base);
Jesse Barnese552eb72010-04-21 11:39:23 -07004689 dev_priv->seqno_obj = NULL;
4690
4691 dev_priv->seqno_page = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07004692}
4693
Eric Anholt673a3942008-07-30 12:06:12 -07004694int
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004695i915_gem_init_ringbuffer(struct drm_device *dev)
4696{
4697 drm_i915_private_t *dev_priv = dev->dev_private;
4698 int ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01004699
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004700 if (HAS_PIPE_CONTROL(dev)) {
4701 ret = i915_gem_init_pipe_control(dev);
4702 if (ret)
4703 return ret;
4704 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004705
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08004706 ret = intel_init_render_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004707 if (ret)
4708 goto cleanup_pipe_control;
4709
4710 if (HAS_BSD(dev)) {
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08004711 ret = intel_init_bsd_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004712 if (ret)
4713 goto cleanup_render_ring;
Zou Nan haid1b851f2010-05-21 09:08:57 +08004714 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004715
Chris Wilson549f7362010-10-19 11:19:32 +01004716 if (HAS_BLT(dev)) {
4717 ret = intel_init_blt_ring_buffer(dev);
4718 if (ret)
4719 goto cleanup_bsd_ring;
4720 }
4721
Chris Wilson6f392d5482010-08-07 11:01:22 +01004722 dev_priv->next_seqno = 1;
4723
Chris Wilson68f95ba2010-05-27 13:18:22 +01004724 return 0;
4725
Chris Wilson549f7362010-10-19 11:19:32 +01004726cleanup_bsd_ring:
Chris Wilson78501ea2010-10-27 12:18:21 +01004727 intel_cleanup_ring_buffer(&dev_priv->bsd_ring);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004728cleanup_render_ring:
Chris Wilson78501ea2010-10-27 12:18:21 +01004729 intel_cleanup_ring_buffer(&dev_priv->render_ring);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004730cleanup_pipe_control:
4731 if (HAS_PIPE_CONTROL(dev))
4732 i915_gem_cleanup_pipe_control(dev);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004733 return ret;
4734}
4735
4736void
4737i915_gem_cleanup_ringbuffer(struct drm_device *dev)
4738{
4739 drm_i915_private_t *dev_priv = dev->dev_private;
4740
Chris Wilson78501ea2010-10-27 12:18:21 +01004741 intel_cleanup_ring_buffer(&dev_priv->render_ring);
4742 intel_cleanup_ring_buffer(&dev_priv->bsd_ring);
4743 intel_cleanup_ring_buffer(&dev_priv->blt_ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004744 if (HAS_PIPE_CONTROL(dev))
4745 i915_gem_cleanup_pipe_control(dev);
4746}
4747
4748int
Eric Anholt673a3942008-07-30 12:06:12 -07004749i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
4750 struct drm_file *file_priv)
4751{
4752 drm_i915_private_t *dev_priv = dev->dev_private;
4753 int ret;
4754
Jesse Barnes79e53942008-11-07 14:24:08 -08004755 if (drm_core_check_feature(dev, DRIVER_MODESET))
4756 return 0;
4757
Ben Gamariba1234d2009-09-14 17:48:47 -04004758 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07004759 DRM_ERROR("Reenabling wedged hardware, good luck\n");
Ben Gamariba1234d2009-09-14 17:48:47 -04004760 atomic_set(&dev_priv->mm.wedged, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004761 }
4762
Eric Anholt673a3942008-07-30 12:06:12 -07004763 mutex_lock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004764 dev_priv->mm.suspended = 0;
4765
4766 ret = i915_gem_init_ringbuffer(dev);
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004767 if (ret != 0) {
4768 mutex_unlock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004769 return ret;
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004770 }
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004771
Chris Wilson69dc4982010-10-19 10:36:51 +01004772 BUG_ON(!list_empty(&dev_priv->mm.active_list));
Zou Nan hai852835f2010-05-21 09:08:56 +08004773 BUG_ON(!list_empty(&dev_priv->render_ring.active_list));
Chris Wilson87acb0a2010-10-19 10:13:00 +01004774 BUG_ON(!list_empty(&dev_priv->bsd_ring.active_list));
Chris Wilson549f7362010-10-19 11:19:32 +01004775 BUG_ON(!list_empty(&dev_priv->blt_ring.active_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004776 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
4777 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
Zou Nan hai852835f2010-05-21 09:08:56 +08004778 BUG_ON(!list_empty(&dev_priv->render_ring.request_list));
Chris Wilson87acb0a2010-10-19 10:13:00 +01004779 BUG_ON(!list_empty(&dev_priv->bsd_ring.request_list));
Chris Wilson549f7362010-10-19 11:19:32 +01004780 BUG_ON(!list_empty(&dev_priv->blt_ring.request_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004781 mutex_unlock(&dev->struct_mutex);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004782
Chris Wilson5f353082010-06-07 14:03:03 +01004783 ret = drm_irq_install(dev);
4784 if (ret)
4785 goto cleanup_ringbuffer;
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004786
Eric Anholt673a3942008-07-30 12:06:12 -07004787 return 0;
Chris Wilson5f353082010-06-07 14:03:03 +01004788
4789cleanup_ringbuffer:
4790 mutex_lock(&dev->struct_mutex);
4791 i915_gem_cleanup_ringbuffer(dev);
4792 dev_priv->mm.suspended = 1;
4793 mutex_unlock(&dev->struct_mutex);
4794
4795 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004796}
4797
4798int
4799i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
4800 struct drm_file *file_priv)
4801{
Jesse Barnes79e53942008-11-07 14:24:08 -08004802 if (drm_core_check_feature(dev, DRIVER_MODESET))
4803 return 0;
4804
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004805 drm_irq_uninstall(dev);
Linus Torvaldse6890f62009-09-08 17:09:24 -07004806 return i915_gem_idle(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004807}
4808
4809void
4810i915_gem_lastclose(struct drm_device *dev)
4811{
4812 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004813
Eric Anholte806b492009-01-22 09:56:58 -08004814 if (drm_core_check_feature(dev, DRIVER_MODESET))
4815 return;
4816
Keith Packard6dbe2772008-10-14 21:41:13 -07004817 ret = i915_gem_idle(dev);
4818 if (ret)
4819 DRM_ERROR("failed to idle hardware: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07004820}
4821
Chris Wilson64193402010-10-24 12:38:05 +01004822static void
4823init_ring_lists(struct intel_ring_buffer *ring)
4824{
4825 INIT_LIST_HEAD(&ring->active_list);
4826 INIT_LIST_HEAD(&ring->request_list);
4827 INIT_LIST_HEAD(&ring->gpu_write_list);
4828}
4829
Eric Anholt673a3942008-07-30 12:06:12 -07004830void
4831i915_gem_load(struct drm_device *dev)
4832{
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004833 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07004834 drm_i915_private_t *dev_priv = dev->dev_private;
4835
Chris Wilson69dc4982010-10-19 10:36:51 +01004836 INIT_LIST_HEAD(&dev_priv->mm.active_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004837 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
4838 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004839 INIT_LIST_HEAD(&dev_priv->mm.pinned_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07004840 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Chris Wilsonbe726152010-07-23 23:18:50 +01004841 INIT_LIST_HEAD(&dev_priv->mm.deferred_free_list);
Daniel Vetter93a37f22010-11-05 20:24:53 +01004842 INIT_LIST_HEAD(&dev_priv->mm.gtt_list);
Chris Wilson64193402010-10-24 12:38:05 +01004843 init_ring_lists(&dev_priv->render_ring);
4844 init_ring_lists(&dev_priv->bsd_ring);
4845 init_ring_lists(&dev_priv->blt_ring);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02004846 for (i = 0; i < 16; i++)
4847 INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004848 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
4849 i915_gem_retire_work_handler);
Chris Wilson30dbf0c2010-09-25 10:19:17 +01004850 init_completion(&dev_priv->error_completion);
Chris Wilson31169712009-09-14 16:50:28 +01004851
Dave Airlie94400122010-07-20 13:15:31 +10004852 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
4853 if (IS_GEN3(dev)) {
4854 u32 tmp = I915_READ(MI_ARB_STATE);
4855 if (!(tmp & MI_ARB_C3_LP_WRITE_ENABLE)) {
4856 /* arb state is a masked write, so set bit + bit in mask */
4857 tmp = MI_ARB_C3_LP_WRITE_ENABLE | (MI_ARB_C3_LP_WRITE_ENABLE << MI_ARB_MASK_SHIFT);
4858 I915_WRITE(MI_ARB_STATE, tmp);
4859 }
4860 }
4861
Jesse Barnesde151cf2008-11-12 10:03:55 -08004862 /* Old X drivers will take 0-2 for front, back, depth buffers */
Eric Anholtb397c832010-01-26 09:43:10 -08004863 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4864 dev_priv->fence_reg_start = 3;
Jesse Barnesde151cf2008-11-12 10:03:55 -08004865
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004866 if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08004867 dev_priv->num_fence_regs = 16;
4868 else
4869 dev_priv->num_fence_regs = 8;
4870
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004871 /* Initialize fence registers to zero */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004872 switch (INTEL_INFO(dev)->gen) {
4873 case 6:
4874 for (i = 0; i < 16; i++)
4875 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (i * 8), 0);
4876 break;
4877 case 5:
4878 case 4:
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004879 for (i = 0; i < 16; i++)
4880 I915_WRITE64(FENCE_REG_965_0 + (i * 8), 0);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004881 break;
4882 case 3:
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004883 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4884 for (i = 0; i < 8; i++)
4885 I915_WRITE(FENCE_REG_945_8 + (i * 4), 0);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004886 case 2:
4887 for (i = 0; i < 8; i++)
4888 I915_WRITE(FENCE_REG_830_0 + (i * 4), 0);
4889 break;
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004890 }
Eric Anholt673a3942008-07-30 12:06:12 -07004891 i915_gem_detect_bit_6_swizzle(dev);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05004892 init_waitqueue_head(&dev_priv->pending_flip_queue);
Chris Wilson17250b72010-10-28 12:51:39 +01004893
4894 dev_priv->mm.inactive_shrinker.shrink = i915_gem_inactive_shrink;
4895 dev_priv->mm.inactive_shrinker.seeks = DEFAULT_SEEKS;
4896 register_shrinker(&dev_priv->mm.inactive_shrinker);
Eric Anholt673a3942008-07-30 12:06:12 -07004897}
Dave Airlie71acb5e2008-12-30 20:31:46 +10004898
4899/*
4900 * Create a physically contiguous memory object for this object
4901 * e.g. for cursor + overlay regs
4902 */
Chris Wilson995b6762010-08-20 13:23:26 +01004903static int i915_gem_init_phys_object(struct drm_device *dev,
4904 int id, int size, int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004905{
4906 drm_i915_private_t *dev_priv = dev->dev_private;
4907 struct drm_i915_gem_phys_object *phys_obj;
4908 int ret;
4909
4910 if (dev_priv->mm.phys_objs[id - 1] || !size)
4911 return 0;
4912
Eric Anholt9a298b22009-03-24 12:23:04 -07004913 phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004914 if (!phys_obj)
4915 return -ENOMEM;
4916
4917 phys_obj->id = id;
4918
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004919 phys_obj->handle = drm_pci_alloc(dev, size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004920 if (!phys_obj->handle) {
4921 ret = -ENOMEM;
4922 goto kfree_obj;
4923 }
4924#ifdef CONFIG_X86
4925 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4926#endif
4927
4928 dev_priv->mm.phys_objs[id - 1] = phys_obj;
4929
4930 return 0;
4931kfree_obj:
Eric Anholt9a298b22009-03-24 12:23:04 -07004932 kfree(phys_obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004933 return ret;
4934}
4935
Chris Wilson995b6762010-08-20 13:23:26 +01004936static void i915_gem_free_phys_object(struct drm_device *dev, int id)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004937{
4938 drm_i915_private_t *dev_priv = dev->dev_private;
4939 struct drm_i915_gem_phys_object *phys_obj;
4940
4941 if (!dev_priv->mm.phys_objs[id - 1])
4942 return;
4943
4944 phys_obj = dev_priv->mm.phys_objs[id - 1];
4945 if (phys_obj->cur_obj) {
4946 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
4947 }
4948
4949#ifdef CONFIG_X86
4950 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4951#endif
4952 drm_pci_free(dev, phys_obj->handle);
4953 kfree(phys_obj);
4954 dev_priv->mm.phys_objs[id - 1] = NULL;
4955}
4956
4957void i915_gem_free_all_phys_object(struct drm_device *dev)
4958{
4959 int i;
4960
Dave Airlie260883c2009-01-22 17:58:49 +10004961 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004962 i915_gem_free_phys_object(dev, i);
4963}
4964
4965void i915_gem_detach_phys_object(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00004966 struct drm_i915_gem_object *obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004967{
Chris Wilson05394f32010-11-08 19:18:58 +00004968 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Chris Wilsone5281cc2010-10-28 13:45:36 +01004969 char *vaddr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004970 int i;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004971 int page_count;
4972
Chris Wilson05394f32010-11-08 19:18:58 +00004973 if (!obj->phys_obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004974 return;
Chris Wilson05394f32010-11-08 19:18:58 +00004975 vaddr = obj->phys_obj->handle->vaddr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004976
Chris Wilson05394f32010-11-08 19:18:58 +00004977 page_count = obj->base.size / PAGE_SIZE;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004978 for (i = 0; i < page_count; i++) {
Chris Wilsone5281cc2010-10-28 13:45:36 +01004979 struct page *page = read_cache_page_gfp(mapping, i,
4980 GFP_HIGHUSER | __GFP_RECLAIMABLE);
4981 if (!IS_ERR(page)) {
4982 char *dst = kmap_atomic(page);
4983 memcpy(dst, vaddr + i*PAGE_SIZE, PAGE_SIZE);
4984 kunmap_atomic(dst);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004985
Chris Wilsone5281cc2010-10-28 13:45:36 +01004986 drm_clflush_pages(&page, 1);
4987
4988 set_page_dirty(page);
4989 mark_page_accessed(page);
4990 page_cache_release(page);
4991 }
Dave Airlie71acb5e2008-12-30 20:31:46 +10004992 }
Daniel Vetter40ce6572010-11-05 18:12:18 +01004993 intel_gtt_chipset_flush();
Chris Wilsond78b47b2009-06-17 21:52:49 +01004994
Chris Wilson05394f32010-11-08 19:18:58 +00004995 obj->phys_obj->cur_obj = NULL;
4996 obj->phys_obj = NULL;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004997}
4998
4999int
5000i915_gem_attach_phys_object(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00005001 struct drm_i915_gem_object *obj,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01005002 int id,
5003 int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10005004{
Chris Wilson05394f32010-11-08 19:18:58 +00005005 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Dave Airlie71acb5e2008-12-30 20:31:46 +10005006 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie71acb5e2008-12-30 20:31:46 +10005007 int ret = 0;
5008 int page_count;
5009 int i;
5010
5011 if (id > I915_MAX_PHYS_OBJECT)
5012 return -EINVAL;
5013
Chris Wilson05394f32010-11-08 19:18:58 +00005014 if (obj->phys_obj) {
5015 if (obj->phys_obj->id == id)
Dave Airlie71acb5e2008-12-30 20:31:46 +10005016 return 0;
5017 i915_gem_detach_phys_object(dev, obj);
5018 }
5019
Dave Airlie71acb5e2008-12-30 20:31:46 +10005020 /* create a new object */
5021 if (!dev_priv->mm.phys_objs[id - 1]) {
5022 ret = i915_gem_init_phys_object(dev, id,
Chris Wilson05394f32010-11-08 19:18:58 +00005023 obj->base.size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10005024 if (ret) {
Chris Wilson05394f32010-11-08 19:18:58 +00005025 DRM_ERROR("failed to init phys object %d size: %zu\n",
5026 id, obj->base.size);
Chris Wilsone5281cc2010-10-28 13:45:36 +01005027 return ret;
Dave Airlie71acb5e2008-12-30 20:31:46 +10005028 }
5029 }
5030
5031 /* bind to the object */
Chris Wilson05394f32010-11-08 19:18:58 +00005032 obj->phys_obj = dev_priv->mm.phys_objs[id - 1];
5033 obj->phys_obj->cur_obj = obj;
Dave Airlie71acb5e2008-12-30 20:31:46 +10005034
Chris Wilson05394f32010-11-08 19:18:58 +00005035 page_count = obj->base.size / PAGE_SIZE;
Dave Airlie71acb5e2008-12-30 20:31:46 +10005036
5037 for (i = 0; i < page_count; i++) {
Chris Wilsone5281cc2010-10-28 13:45:36 +01005038 struct page *page;
5039 char *dst, *src;
Dave Airlie71acb5e2008-12-30 20:31:46 +10005040
Chris Wilsone5281cc2010-10-28 13:45:36 +01005041 page = read_cache_page_gfp(mapping, i,
5042 GFP_HIGHUSER | __GFP_RECLAIMABLE);
5043 if (IS_ERR(page))
5044 return PTR_ERR(page);
5045
Chris Wilsonff75b9b2010-10-30 22:52:31 +01005046 src = kmap_atomic(page);
Chris Wilson05394f32010-11-08 19:18:58 +00005047 dst = obj->phys_obj->handle->vaddr + (i * PAGE_SIZE);
Dave Airlie71acb5e2008-12-30 20:31:46 +10005048 memcpy(dst, src, PAGE_SIZE);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -07005049 kunmap_atomic(src);
Chris Wilsone5281cc2010-10-28 13:45:36 +01005050
5051 mark_page_accessed(page);
5052 page_cache_release(page);
Dave Airlie71acb5e2008-12-30 20:31:46 +10005053 }
5054
5055 return 0;
Dave Airlie71acb5e2008-12-30 20:31:46 +10005056}
5057
5058static int
Chris Wilson05394f32010-11-08 19:18:58 +00005059i915_gem_phys_pwrite(struct drm_device *dev,
5060 struct drm_i915_gem_object *obj,
Dave Airlie71acb5e2008-12-30 20:31:46 +10005061 struct drm_i915_gem_pwrite *args,
5062 struct drm_file *file_priv)
5063{
Chris Wilson05394f32010-11-08 19:18:58 +00005064 void *vaddr = obj->phys_obj->handle->vaddr + args->offset;
Chris Wilsonb47b30c2010-11-08 01:12:29 +00005065 char __user *user_data = (char __user *) (uintptr_t) args->data_ptr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10005066
Chris Wilsonb47b30c2010-11-08 01:12:29 +00005067 if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) {
5068 unsigned long unwritten;
5069
5070 /* The physical object once assigned is fixed for the lifetime
5071 * of the obj, so we can safely drop the lock and continue
5072 * to access vaddr.
5073 */
5074 mutex_unlock(&dev->struct_mutex);
5075 unwritten = copy_from_user(vaddr, user_data, args->size);
5076 mutex_lock(&dev->struct_mutex);
5077 if (unwritten)
5078 return -EFAULT;
5079 }
Dave Airlie71acb5e2008-12-30 20:31:46 +10005080
Daniel Vetter40ce6572010-11-05 18:12:18 +01005081 intel_gtt_chipset_flush();
Dave Airlie71acb5e2008-12-30 20:31:46 +10005082 return 0;
5083}
Eric Anholtb9624422009-06-03 07:27:35 +00005084
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005085void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00005086{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005087 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00005088
5089 /* Clean up our request list when the client is going away, so that
5090 * later retire_requests won't dereference our soon-to-be-gone
5091 * file_priv.
5092 */
Chris Wilson1c255952010-09-26 11:03:27 +01005093 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005094 while (!list_empty(&file_priv->mm.request_list)) {
5095 struct drm_i915_gem_request *request;
5096
5097 request = list_first_entry(&file_priv->mm.request_list,
5098 struct drm_i915_gem_request,
5099 client_list);
5100 list_del(&request->client_list);
5101 request->file_priv = NULL;
5102 }
Chris Wilson1c255952010-09-26 11:03:27 +01005103 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00005104}
Chris Wilson31169712009-09-14 16:50:28 +01005105
Chris Wilson31169712009-09-14 16:50:28 +01005106static int
Chris Wilson1637ef42010-04-20 17:10:35 +01005107i915_gpu_is_active(struct drm_device *dev)
5108{
5109 drm_i915_private_t *dev_priv = dev->dev_private;
5110 int lists_empty;
5111
Chris Wilson1637ef42010-04-20 17:10:35 +01005112 lists_empty = list_empty(&dev_priv->mm.flushing_list) &&
Chris Wilson17250b72010-10-28 12:51:39 +01005113 list_empty(&dev_priv->mm.active_list);
Chris Wilson1637ef42010-04-20 17:10:35 +01005114
5115 return !lists_empty;
5116}
5117
5118static int
Chris Wilson17250b72010-10-28 12:51:39 +01005119i915_gem_inactive_shrink(struct shrinker *shrinker,
5120 int nr_to_scan,
5121 gfp_t gfp_mask)
Chris Wilson31169712009-09-14 16:50:28 +01005122{
Chris Wilson17250b72010-10-28 12:51:39 +01005123 struct drm_i915_private *dev_priv =
5124 container_of(shrinker,
5125 struct drm_i915_private,
5126 mm.inactive_shrinker);
5127 struct drm_device *dev = dev_priv->dev;
5128 struct drm_i915_gem_object *obj, *next;
5129 int cnt;
5130
5131 if (!mutex_trylock(&dev->struct_mutex))
Chris Wilsonbbe2e112010-10-28 22:35:07 +01005132 return 0;
Chris Wilson31169712009-09-14 16:50:28 +01005133
5134 /* "fast-path" to count number of available objects */
5135 if (nr_to_scan == 0) {
Chris Wilson17250b72010-10-28 12:51:39 +01005136 cnt = 0;
5137 list_for_each_entry(obj,
5138 &dev_priv->mm.inactive_list,
5139 mm_list)
5140 cnt++;
5141 mutex_unlock(&dev->struct_mutex);
5142 return cnt / 100 * sysctl_vfs_cache_pressure;
Chris Wilson31169712009-09-14 16:50:28 +01005143 }
5144
Chris Wilson1637ef42010-04-20 17:10:35 +01005145rescan:
Chris Wilson31169712009-09-14 16:50:28 +01005146 /* first scan for clean buffers */
Chris Wilson17250b72010-10-28 12:51:39 +01005147 i915_gem_retire_requests(dev);
Chris Wilson31169712009-09-14 16:50:28 +01005148
Chris Wilson17250b72010-10-28 12:51:39 +01005149 list_for_each_entry_safe(obj, next,
5150 &dev_priv->mm.inactive_list,
5151 mm_list) {
5152 if (i915_gem_object_is_purgeable(obj)) {
Chris Wilson05394f32010-11-08 19:18:58 +00005153 i915_gem_object_unbind(obj);
Chris Wilson17250b72010-10-28 12:51:39 +01005154 if (--nr_to_scan == 0)
5155 break;
Chris Wilson31169712009-09-14 16:50:28 +01005156 }
Chris Wilson31169712009-09-14 16:50:28 +01005157 }
5158
5159 /* second pass, evict/count anything still on the inactive list */
Chris Wilson17250b72010-10-28 12:51:39 +01005160 cnt = 0;
5161 list_for_each_entry_safe(obj, next,
5162 &dev_priv->mm.inactive_list,
5163 mm_list) {
5164 if (nr_to_scan) {
Chris Wilson05394f32010-11-08 19:18:58 +00005165 i915_gem_object_unbind(obj);
Chris Wilson17250b72010-10-28 12:51:39 +01005166 nr_to_scan--;
5167 } else
5168 cnt++;
Chris Wilson31169712009-09-14 16:50:28 +01005169 }
5170
Chris Wilson17250b72010-10-28 12:51:39 +01005171 if (nr_to_scan && i915_gpu_is_active(dev)) {
Chris Wilson1637ef42010-04-20 17:10:35 +01005172 /*
5173 * We are desperate for pages, so as a last resort, wait
5174 * for the GPU to finish and discard whatever we can.
5175 * This has a dramatic impact to reduce the number of
5176 * OOM-killer events whilst running the GPU aggressively.
5177 */
Chris Wilson17250b72010-10-28 12:51:39 +01005178 if (i915_gpu_idle(dev) == 0)
Chris Wilson1637ef42010-04-20 17:10:35 +01005179 goto rescan;
5180 }
Chris Wilson17250b72010-10-28 12:51:39 +01005181 mutex_unlock(&dev->struct_mutex);
5182 return cnt / 100 * sysctl_vfs_cache_pressure;
Chris Wilson31169712009-09-14 16:50:28 +01005183}