blob: f6167c55a649927ec64124d11cad797a312c7a66 [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 int i915_gem_object_flush_gpu_write_domain(struct drm_i915_gem_object *obj,
Chris Wilson919926a2010-11-12 13:42:53 +000045 struct intel_ring_buffer *pipelined);
Chris Wilson05394f32010-11-08 19:18:58 +000046static void i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj);
47static void i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj);
48static int i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj,
Chris Wilson919926a2010-11-12 13:42:53 +000049 bool write);
Chris Wilson05394f32010-11-08 19:18:58 +000050static int i915_gem_object_set_cpu_read_domain_range(struct drm_i915_gem_object *obj,
Eric Anholte47c68e2008-11-14 13:35:19 -080051 uint64_t offset,
52 uint64_t size);
Chris Wilson05394f32010-11-08 19:18:58 +000053static void i915_gem_object_set_to_full_cpu_read_domain(struct drm_i915_gem_object *obj);
54static int i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj,
Chris Wilson2cf34d72010-09-14 13:03:28 +010055 bool interruptible);
Chris Wilson05394f32010-11-08 19:18:58 +000056static int i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
Chris Wilsona00b10c2010-09-24 21:15:47 +010057 unsigned alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +010058 bool map_and_fenceable);
Chris Wilson05394f32010-11-08 19:18:58 +000059static void i915_gem_clear_fence_reg(struct drm_i915_gem_object *obj);
60static int i915_gem_phys_pwrite(struct drm_device *dev,
61 struct drm_i915_gem_object *obj,
Dave Airlie71acb5e2008-12-30 20:31:46 +100062 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +000063 struct drm_file *file);
64static void i915_gem_free_object_tail(struct drm_i915_gem_object *obj);
Eric Anholt673a3942008-07-30 12:06:12 -070065
Chris Wilson17250b72010-10-28 12:51:39 +010066static int i915_gem_inactive_shrink(struct shrinker *shrinker,
67 int nr_to_scan,
68 gfp_t gfp_mask);
69
Chris Wilson31169712009-09-14 16:50:28 +010070
Chris Wilson73aa8082010-09-30 11:46:12 +010071/* some bookkeeping */
72static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
73 size_t size)
74{
75 dev_priv->mm.object_count++;
76 dev_priv->mm.object_memory += size;
77}
78
79static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
80 size_t size)
81{
82 dev_priv->mm.object_count--;
83 dev_priv->mm.object_memory -= size;
84}
85
86static void i915_gem_info_add_gtt(struct drm_i915_private *dev_priv,
Chris Wilsona00b10c2010-09-24 21:15:47 +010087 struct drm_i915_gem_object *obj)
Chris Wilson73aa8082010-09-30 11:46:12 +010088{
89 dev_priv->mm.gtt_count++;
Chris Wilsona00b10c2010-09-24 21:15:47 +010090 dev_priv->mm.gtt_memory += obj->gtt_space->size;
91 if (obj->gtt_offset < dev_priv->mm.gtt_mappable_end) {
Daniel Vetterfb7d5162010-10-01 22:05:20 +020092 dev_priv->mm.mappable_gtt_used +=
Chris Wilsona00b10c2010-09-24 21:15:47 +010093 min_t(size_t, obj->gtt_space->size,
94 dev_priv->mm.gtt_mappable_end - obj->gtt_offset);
Daniel Vetterfb7d5162010-10-01 22:05:20 +020095 }
Daniel Vetter93a37f22010-11-05 20:24:53 +010096 list_add_tail(&obj->gtt_list, &dev_priv->mm.gtt_list);
Chris Wilson73aa8082010-09-30 11:46:12 +010097}
98
99static void i915_gem_info_remove_gtt(struct drm_i915_private *dev_priv,
Chris Wilsona00b10c2010-09-24 21:15:47 +0100100 struct drm_i915_gem_object *obj)
Chris Wilson73aa8082010-09-30 11:46:12 +0100101{
102 dev_priv->mm.gtt_count--;
Chris Wilsona00b10c2010-09-24 21:15:47 +0100103 dev_priv->mm.gtt_memory -= obj->gtt_space->size;
104 if (obj->gtt_offset < dev_priv->mm.gtt_mappable_end) {
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200105 dev_priv->mm.mappable_gtt_used -=
Chris Wilsona00b10c2010-09-24 21:15:47 +0100106 min_t(size_t, obj->gtt_space->size,
107 dev_priv->mm.gtt_mappable_end - obj->gtt_offset);
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200108 }
Daniel Vetter93a37f22010-11-05 20:24:53 +0100109 list_del_init(&obj->gtt_list);
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200110}
111
112/**
113 * Update the mappable working set counters. Call _only_ when there is a change
114 * in one of (pin|fault)_mappable and update *_mappable _before_ calling.
115 * @mappable: new state the changed mappable flag (either pin_ or fault_).
116 */
117static void
118i915_gem_info_update_mappable(struct drm_i915_private *dev_priv,
Chris Wilsona00b10c2010-09-24 21:15:47 +0100119 struct drm_i915_gem_object *obj,
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200120 bool mappable)
121{
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200122 if (mappable) {
Chris Wilsona00b10c2010-09-24 21:15:47 +0100123 if (obj->pin_mappable && obj->fault_mappable)
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200124 /* Combined state was already mappable. */
125 return;
126 dev_priv->mm.gtt_mappable_count++;
Chris Wilsona00b10c2010-09-24 21:15:47 +0100127 dev_priv->mm.gtt_mappable_memory += obj->gtt_space->size;
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200128 } else {
Chris Wilsona00b10c2010-09-24 21:15:47 +0100129 if (obj->pin_mappable || obj->fault_mappable)
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200130 /* Combined state still mappable. */
131 return;
132 dev_priv->mm.gtt_mappable_count--;
Chris Wilsona00b10c2010-09-24 21:15:47 +0100133 dev_priv->mm.gtt_mappable_memory -= obj->gtt_space->size;
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200134 }
Chris Wilson73aa8082010-09-30 11:46:12 +0100135}
136
137static void i915_gem_info_add_pin(struct drm_i915_private *dev_priv,
Chris Wilsona00b10c2010-09-24 21:15:47 +0100138 struct drm_i915_gem_object *obj,
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200139 bool mappable)
Chris Wilson73aa8082010-09-30 11:46:12 +0100140{
141 dev_priv->mm.pin_count++;
Chris Wilsona00b10c2010-09-24 21:15:47 +0100142 dev_priv->mm.pin_memory += obj->gtt_space->size;
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200143 if (mappable) {
Chris Wilsona00b10c2010-09-24 21:15:47 +0100144 obj->pin_mappable = true;
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200145 i915_gem_info_update_mappable(dev_priv, obj, true);
146 }
Chris Wilson73aa8082010-09-30 11:46:12 +0100147}
148
149static void i915_gem_info_remove_pin(struct drm_i915_private *dev_priv,
Chris Wilsona00b10c2010-09-24 21:15:47 +0100150 struct drm_i915_gem_object *obj)
Chris Wilson73aa8082010-09-30 11:46:12 +0100151{
152 dev_priv->mm.pin_count--;
Chris Wilsona00b10c2010-09-24 21:15:47 +0100153 dev_priv->mm.pin_memory -= obj->gtt_space->size;
154 if (obj->pin_mappable) {
155 obj->pin_mappable = false;
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200156 i915_gem_info_update_mappable(dev_priv, obj, false);
157 }
Chris Wilson73aa8082010-09-30 11:46:12 +0100158}
159
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100160int
161i915_gem_check_is_wedged(struct drm_device *dev)
162{
163 struct drm_i915_private *dev_priv = dev->dev_private;
164 struct completion *x = &dev_priv->error_completion;
165 unsigned long flags;
166 int ret;
167
168 if (!atomic_read(&dev_priv->mm.wedged))
169 return 0;
170
171 ret = wait_for_completion_interruptible(x);
172 if (ret)
173 return ret;
174
175 /* Success, we reset the GPU! */
176 if (!atomic_read(&dev_priv->mm.wedged))
177 return 0;
178
179 /* GPU is hung, bump the completion count to account for
180 * the token we just consumed so that we never hit zero and
181 * end up waiting upon a subsequent completion event that
182 * will never happen.
183 */
184 spin_lock_irqsave(&x->wait.lock, flags);
185 x->done++;
186 spin_unlock_irqrestore(&x->wait.lock, flags);
187 return -EIO;
188}
189
Chris Wilson76c1dec2010-09-25 11:22:51 +0100190static int i915_mutex_lock_interruptible(struct drm_device *dev)
191{
192 struct drm_i915_private *dev_priv = dev->dev_private;
193 int ret;
194
195 ret = i915_gem_check_is_wedged(dev);
196 if (ret)
197 return ret;
198
199 ret = mutex_lock_interruptible(&dev->struct_mutex);
200 if (ret)
201 return ret;
202
203 if (atomic_read(&dev_priv->mm.wedged)) {
204 mutex_unlock(&dev->struct_mutex);
205 return -EAGAIN;
206 }
207
Chris Wilson23bc5982010-09-29 16:10:57 +0100208 WARN_ON(i915_verify_lists(dev));
Chris Wilson76c1dec2010-09-25 11:22:51 +0100209 return 0;
210}
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100211
Chris Wilson7d1c4802010-08-07 21:45:03 +0100212static inline bool
Chris Wilson05394f32010-11-08 19:18:58 +0000213i915_gem_object_is_inactive(struct drm_i915_gem_object *obj)
Chris Wilson7d1c4802010-08-07 21:45:03 +0100214{
Chris Wilson05394f32010-11-08 19:18:58 +0000215 return obj->gtt_space && !obj->active && obj->pin_count == 0;
Chris Wilson7d1c4802010-08-07 21:45:03 +0100216}
217
Chris Wilson20217462010-11-23 15:26:33 +0000218void i915_gem_do_init(struct drm_device *dev,
219 unsigned long start,
220 unsigned long mappable_end,
221 unsigned long end)
Jesse Barnes79e53942008-11-07 14:24:08 -0800222{
223 drm_i915_private_t *dev_priv = dev->dev_private;
224
Jesse Barnes79e53942008-11-07 14:24:08 -0800225 drm_mm_init(&dev_priv->mm.gtt_space, start,
226 end - start);
227
Chris Wilson73aa8082010-09-30 11:46:12 +0100228 dev_priv->mm.gtt_total = end - start;
Daniel Vetterfb7d5162010-10-01 22:05:20 +0200229 dev_priv->mm.mappable_gtt_total = min(end, mappable_end) - start;
Daniel Vetter53984632010-09-22 23:44:24 +0200230 dev_priv->mm.gtt_mappable_end = mappable_end;
Jesse Barnes79e53942008-11-07 14:24:08 -0800231}
Keith Packard6dbe2772008-10-14 21:41:13 -0700232
Eric Anholt673a3942008-07-30 12:06:12 -0700233int
234i915_gem_init_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000235 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700236{
Eric Anholt673a3942008-07-30 12:06:12 -0700237 struct drm_i915_gem_init *args = data;
Chris Wilson20217462010-11-23 15:26:33 +0000238
239 if (args->gtt_start >= args->gtt_end ||
240 (args->gtt_end | args->gtt_start) & (PAGE_SIZE - 1))
241 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -0700242
243 mutex_lock(&dev->struct_mutex);
Chris Wilson20217462010-11-23 15:26:33 +0000244 i915_gem_do_init(dev, args->gtt_start, args->gtt_end, args->gtt_end);
Eric Anholt673a3942008-07-30 12:06:12 -0700245 mutex_unlock(&dev->struct_mutex);
246
Chris Wilson20217462010-11-23 15:26:33 +0000247 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700248}
249
Eric Anholt5a125c32008-10-22 21:40:13 -0700250int
251i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000252 struct drm_file *file)
Eric Anholt5a125c32008-10-22 21:40:13 -0700253{
Chris Wilson73aa8082010-09-30 11:46:12 +0100254 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt5a125c32008-10-22 21:40:13 -0700255 struct drm_i915_gem_get_aperture *args = data;
Eric Anholt5a125c32008-10-22 21:40:13 -0700256
257 if (!(dev->driver->driver_features & DRIVER_GEM))
258 return -ENODEV;
259
Chris Wilson73aa8082010-09-30 11:46:12 +0100260 mutex_lock(&dev->struct_mutex);
261 args->aper_size = dev_priv->mm.gtt_total;
262 args->aper_available_size = args->aper_size - dev_priv->mm.pin_memory;
263 mutex_unlock(&dev->struct_mutex);
Eric Anholt5a125c32008-10-22 21:40:13 -0700264
265 return 0;
266}
267
Eric Anholt673a3942008-07-30 12:06:12 -0700268
269/**
270 * Creates a new mm object and returns a handle to it.
271 */
272int
273i915_gem_create_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000274 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700275{
276 struct drm_i915_gem_create *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +0000277 struct drm_i915_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300278 int ret;
279 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700280
281 args->size = roundup(args->size, PAGE_SIZE);
282
283 /* Allocate the new object */
Daniel Vetterac52bc52010-04-09 19:05:06 +0000284 obj = i915_gem_alloc_object(dev, args->size);
Eric Anholt673a3942008-07-30 12:06:12 -0700285 if (obj == NULL)
286 return -ENOMEM;
287
Chris Wilson05394f32010-11-08 19:18:58 +0000288 ret = drm_gem_handle_create(file, &obj->base, &handle);
Chris Wilson1dfd9752010-09-06 14:44:14 +0100289 if (ret) {
Chris Wilson05394f32010-11-08 19:18:58 +0000290 drm_gem_object_release(&obj->base);
291 i915_gem_info_remove_obj(dev->dev_private, obj->base.size);
Chris Wilson202f2fe2010-10-14 13:20:40 +0100292 kfree(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700293 return ret;
Chris Wilson1dfd9752010-09-06 14:44:14 +0100294 }
295
Chris Wilson202f2fe2010-10-14 13:20:40 +0100296 /* drop reference from allocate - handle holds it now */
Chris Wilson05394f32010-11-08 19:18:58 +0000297 drm_gem_object_unreference(&obj->base);
Chris Wilson202f2fe2010-10-14 13:20:40 +0100298 trace_i915_gem_object_create(obj);
299
Eric Anholt673a3942008-07-30 12:06:12 -0700300 args->handle = handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700301 return 0;
302}
303
Chris Wilson05394f32010-11-08 19:18:58 +0000304static int i915_gem_object_needs_bit17_swizzle(struct drm_i915_gem_object *obj)
Eric Anholt280b7132009-03-12 16:56:27 -0700305{
Chris Wilson05394f32010-11-08 19:18:58 +0000306 drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
Eric Anholt280b7132009-03-12 16:56:27 -0700307
308 return dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_9_10_17 &&
Chris Wilson05394f32010-11-08 19:18:58 +0000309 obj->tiling_mode != I915_TILING_NONE;
Eric Anholt280b7132009-03-12 16:56:27 -0700310}
311
Chris Wilson99a03df2010-05-27 14:15:34 +0100312static inline void
Eric Anholt40123c12009-03-09 13:42:30 -0700313slow_shmem_copy(struct page *dst_page,
314 int dst_offset,
315 struct page *src_page,
316 int src_offset,
317 int length)
318{
319 char *dst_vaddr, *src_vaddr;
320
Chris Wilson99a03df2010-05-27 14:15:34 +0100321 dst_vaddr = kmap(dst_page);
322 src_vaddr = kmap(src_page);
Eric Anholt40123c12009-03-09 13:42:30 -0700323
324 memcpy(dst_vaddr + dst_offset, src_vaddr + src_offset, length);
325
Chris Wilson99a03df2010-05-27 14:15:34 +0100326 kunmap(src_page);
327 kunmap(dst_page);
Eric Anholt40123c12009-03-09 13:42:30 -0700328}
329
Chris Wilson99a03df2010-05-27 14:15:34 +0100330static inline void
Eric Anholt280b7132009-03-12 16:56:27 -0700331slow_shmem_bit17_copy(struct page *gpu_page,
332 int gpu_offset,
333 struct page *cpu_page,
334 int cpu_offset,
335 int length,
336 int is_read)
337{
338 char *gpu_vaddr, *cpu_vaddr;
339
340 /* Use the unswizzled path if this page isn't affected. */
341 if ((page_to_phys(gpu_page) & (1 << 17)) == 0) {
342 if (is_read)
343 return slow_shmem_copy(cpu_page, cpu_offset,
344 gpu_page, gpu_offset, length);
345 else
346 return slow_shmem_copy(gpu_page, gpu_offset,
347 cpu_page, cpu_offset, length);
348 }
349
Chris Wilson99a03df2010-05-27 14:15:34 +0100350 gpu_vaddr = kmap(gpu_page);
351 cpu_vaddr = kmap(cpu_page);
Eric Anholt280b7132009-03-12 16:56:27 -0700352
353 /* Copy the data, XORing A6 with A17 (1). The user already knows he's
354 * XORing with the other bits (A9 for Y, A9 and A10 for X)
355 */
356 while (length > 0) {
357 int cacheline_end = ALIGN(gpu_offset + 1, 64);
358 int this_length = min(cacheline_end - gpu_offset, length);
359 int swizzled_gpu_offset = gpu_offset ^ 64;
360
361 if (is_read) {
362 memcpy(cpu_vaddr + cpu_offset,
363 gpu_vaddr + swizzled_gpu_offset,
364 this_length);
365 } else {
366 memcpy(gpu_vaddr + swizzled_gpu_offset,
367 cpu_vaddr + cpu_offset,
368 this_length);
369 }
370 cpu_offset += this_length;
371 gpu_offset += this_length;
372 length -= this_length;
373 }
374
Chris Wilson99a03df2010-05-27 14:15:34 +0100375 kunmap(cpu_page);
376 kunmap(gpu_page);
Eric Anholt280b7132009-03-12 16:56:27 -0700377}
378
Eric Anholt673a3942008-07-30 12:06:12 -0700379/**
Eric Anholteb014592009-03-10 11:44:52 -0700380 * This is the fast shmem pread path, which attempts to copy_from_user directly
381 * from the backing pages of the object to the user's address space. On a
382 * fault, it fails so we can fall back to i915_gem_shmem_pwrite_slow().
383 */
384static int
Chris Wilson05394f32010-11-08 19:18:58 +0000385i915_gem_shmem_pread_fast(struct drm_device *dev,
386 struct drm_i915_gem_object *obj,
Eric Anholteb014592009-03-10 11:44:52 -0700387 struct drm_i915_gem_pread *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000388 struct drm_file *file)
Eric Anholteb014592009-03-10 11:44:52 -0700389{
Chris Wilson05394f32010-11-08 19:18:58 +0000390 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Eric Anholteb014592009-03-10 11:44:52 -0700391 ssize_t remain;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100392 loff_t offset;
Eric Anholteb014592009-03-10 11:44:52 -0700393 char __user *user_data;
394 int page_offset, page_length;
Eric Anholteb014592009-03-10 11:44:52 -0700395
396 user_data = (char __user *) (uintptr_t) args->data_ptr;
397 remain = args->size;
398
Eric Anholteb014592009-03-10 11:44:52 -0700399 offset = args->offset;
400
401 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100402 struct page *page;
403 char *vaddr;
404 int ret;
405
Eric Anholteb014592009-03-10 11:44:52 -0700406 /* Operation in this page
407 *
Eric Anholteb014592009-03-10 11:44:52 -0700408 * page_offset = offset within page
409 * page_length = bytes to copy for this page
410 */
Eric Anholteb014592009-03-10 11:44:52 -0700411 page_offset = offset & (PAGE_SIZE-1);
412 page_length = remain;
413 if ((page_offset + remain) > PAGE_SIZE)
414 page_length = PAGE_SIZE - page_offset;
415
Chris Wilsone5281cc2010-10-28 13:45:36 +0100416 page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
417 GFP_HIGHUSER | __GFP_RECLAIMABLE);
418 if (IS_ERR(page))
419 return PTR_ERR(page);
420
421 vaddr = kmap_atomic(page);
422 ret = __copy_to_user_inatomic(user_data,
423 vaddr + page_offset,
424 page_length);
425 kunmap_atomic(vaddr);
426
427 mark_page_accessed(page);
428 page_cache_release(page);
429 if (ret)
Chris Wilson4f27b752010-10-14 15:26:45 +0100430 return -EFAULT;
Eric Anholteb014592009-03-10 11:44:52 -0700431
432 remain -= page_length;
433 user_data += page_length;
434 offset += page_length;
435 }
436
Chris Wilson4f27b752010-10-14 15:26:45 +0100437 return 0;
Eric Anholteb014592009-03-10 11:44:52 -0700438}
439
440/**
441 * This is the fallback shmem pread path, which allocates temporary storage
442 * in kernel space to copy_to_user into outside of the struct_mutex, so we
443 * can copy out of the object's backing pages while holding the struct mutex
444 * and not take page faults.
445 */
446static int
Chris Wilson05394f32010-11-08 19:18:58 +0000447i915_gem_shmem_pread_slow(struct drm_device *dev,
448 struct drm_i915_gem_object *obj,
Eric Anholteb014592009-03-10 11:44:52 -0700449 struct drm_i915_gem_pread *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000450 struct drm_file *file)
Eric Anholteb014592009-03-10 11:44:52 -0700451{
Chris Wilson05394f32010-11-08 19:18:58 +0000452 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Eric Anholteb014592009-03-10 11:44:52 -0700453 struct mm_struct *mm = current->mm;
454 struct page **user_pages;
455 ssize_t remain;
456 loff_t offset, pinned_pages, i;
457 loff_t first_data_page, last_data_page, num_pages;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100458 int shmem_page_offset;
459 int data_page_index, data_page_offset;
Eric Anholteb014592009-03-10 11:44:52 -0700460 int page_length;
461 int ret;
462 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700463 int do_bit17_swizzling;
Eric Anholteb014592009-03-10 11:44:52 -0700464
465 remain = args->size;
466
467 /* Pin the user pages containing the data. We can't fault while
468 * holding the struct mutex, yet we want to hold it while
469 * dereferencing the user data.
470 */
471 first_data_page = data_ptr / PAGE_SIZE;
472 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
473 num_pages = last_data_page - first_data_page + 1;
474
Chris Wilson4f27b752010-10-14 15:26:45 +0100475 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
Eric Anholteb014592009-03-10 11:44:52 -0700476 if (user_pages == NULL)
477 return -ENOMEM;
478
Chris Wilson4f27b752010-10-14 15:26:45 +0100479 mutex_unlock(&dev->struct_mutex);
Eric Anholteb014592009-03-10 11:44:52 -0700480 down_read(&mm->mmap_sem);
481 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
Eric Anholte5e9ecd2009-04-07 16:01:22 -0700482 num_pages, 1, 0, user_pages, NULL);
Eric Anholteb014592009-03-10 11:44:52 -0700483 up_read(&mm->mmap_sem);
Chris Wilson4f27b752010-10-14 15:26:45 +0100484 mutex_lock(&dev->struct_mutex);
Eric Anholteb014592009-03-10 11:44:52 -0700485 if (pinned_pages < num_pages) {
486 ret = -EFAULT;
Chris Wilson4f27b752010-10-14 15:26:45 +0100487 goto out;
Eric Anholteb014592009-03-10 11:44:52 -0700488 }
489
Chris Wilson4f27b752010-10-14 15:26:45 +0100490 ret = i915_gem_object_set_cpu_read_domain_range(obj,
491 args->offset,
Eric Anholteb014592009-03-10 11:44:52 -0700492 args->size);
Chris Wilson4f27b752010-10-14 15:26:45 +0100493 if (ret)
494 goto out;
495
496 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700497
Eric Anholteb014592009-03-10 11:44:52 -0700498 offset = args->offset;
499
500 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100501 struct page *page;
502
Eric Anholteb014592009-03-10 11:44:52 -0700503 /* Operation in this page
504 *
Eric Anholteb014592009-03-10 11:44:52 -0700505 * shmem_page_offset = offset within page in shmem file
506 * data_page_index = page number in get_user_pages return
507 * data_page_offset = offset with data_page_index page.
508 * page_length = bytes to copy for this page
509 */
Eric Anholteb014592009-03-10 11:44:52 -0700510 shmem_page_offset = offset & ~PAGE_MASK;
511 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
512 data_page_offset = data_ptr & ~PAGE_MASK;
513
514 page_length = remain;
515 if ((shmem_page_offset + page_length) > PAGE_SIZE)
516 page_length = PAGE_SIZE - shmem_page_offset;
517 if ((data_page_offset + page_length) > PAGE_SIZE)
518 page_length = PAGE_SIZE - data_page_offset;
519
Chris Wilsone5281cc2010-10-28 13:45:36 +0100520 page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
521 GFP_HIGHUSER | __GFP_RECLAIMABLE);
522 if (IS_ERR(page))
523 return PTR_ERR(page);
524
Eric Anholt280b7132009-03-12 16:56:27 -0700525 if (do_bit17_swizzling) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100526 slow_shmem_bit17_copy(page,
Eric Anholt280b7132009-03-12 16:56:27 -0700527 shmem_page_offset,
Chris Wilson99a03df2010-05-27 14:15:34 +0100528 user_pages[data_page_index],
529 data_page_offset,
530 page_length,
531 1);
532 } else {
533 slow_shmem_copy(user_pages[data_page_index],
534 data_page_offset,
Chris Wilsone5281cc2010-10-28 13:45:36 +0100535 page,
Chris Wilson99a03df2010-05-27 14:15:34 +0100536 shmem_page_offset,
537 page_length);
Eric Anholt280b7132009-03-12 16:56:27 -0700538 }
Eric Anholteb014592009-03-10 11:44:52 -0700539
Chris Wilsone5281cc2010-10-28 13:45:36 +0100540 mark_page_accessed(page);
541 page_cache_release(page);
542
Eric Anholteb014592009-03-10 11:44:52 -0700543 remain -= page_length;
544 data_ptr += page_length;
545 offset += page_length;
546 }
547
Chris Wilson4f27b752010-10-14 15:26:45 +0100548out:
Eric Anholteb014592009-03-10 11:44:52 -0700549 for (i = 0; i < pinned_pages; i++) {
550 SetPageDirty(user_pages[i]);
Chris Wilsone5281cc2010-10-28 13:45:36 +0100551 mark_page_accessed(user_pages[i]);
Eric Anholteb014592009-03-10 11:44:52 -0700552 page_cache_release(user_pages[i]);
553 }
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700554 drm_free_large(user_pages);
Eric Anholteb014592009-03-10 11:44:52 -0700555
556 return ret;
557}
558
Eric Anholt673a3942008-07-30 12:06:12 -0700559/**
560 * Reads data from the object referenced by handle.
561 *
562 * On error, the contents of *data are undefined.
563 */
564int
565i915_gem_pread_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000566 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700567{
568 struct drm_i915_gem_pread *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +0000569 struct drm_i915_gem_object *obj;
Chris Wilson35b62a82010-09-26 20:23:38 +0100570 int ret = 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700571
Chris Wilson51311d02010-11-17 09:10:42 +0000572 if (args->size == 0)
573 return 0;
574
575 if (!access_ok(VERIFY_WRITE,
576 (char __user *)(uintptr_t)args->data_ptr,
577 args->size))
578 return -EFAULT;
579
580 ret = fault_in_pages_writeable((char __user *)(uintptr_t)args->data_ptr,
581 args->size);
582 if (ret)
583 return -EFAULT;
584
Chris Wilson4f27b752010-10-14 15:26:45 +0100585 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100586 if (ret)
Chris Wilson4f27b752010-10-14 15:26:45 +0100587 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700588
Chris Wilson05394f32010-11-08 19:18:58 +0000589 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100590 if (obj == NULL) {
591 ret = -ENOENT;
592 goto unlock;
Chris Wilson4f27b752010-10-14 15:26:45 +0100593 }
Eric Anholt673a3942008-07-30 12:06:12 -0700594
Chris Wilson7dcd2492010-09-26 20:21:44 +0100595 /* Bounds check source. */
Chris Wilson05394f32010-11-08 19:18:58 +0000596 if (args->offset > obj->base.size ||
597 args->size > obj->base.size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +0100598 ret = -EINVAL;
Chris Wilson35b62a82010-09-26 20:23:38 +0100599 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +0100600 }
601
Chris Wilson4f27b752010-10-14 15:26:45 +0100602 ret = i915_gem_object_set_cpu_read_domain_range(obj,
603 args->offset,
604 args->size);
605 if (ret)
Chris Wilsone5281cc2010-10-28 13:45:36 +0100606 goto out;
Chris Wilson4f27b752010-10-14 15:26:45 +0100607
608 ret = -EFAULT;
609 if (!i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilson05394f32010-11-08 19:18:58 +0000610 ret = i915_gem_shmem_pread_fast(dev, obj, args, file);
Chris Wilson4f27b752010-10-14 15:26:45 +0100611 if (ret == -EFAULT)
Chris Wilson05394f32010-11-08 19:18:58 +0000612 ret = i915_gem_shmem_pread_slow(dev, obj, args, file);
Eric Anholt673a3942008-07-30 12:06:12 -0700613
Chris Wilson35b62a82010-09-26 20:23:38 +0100614out:
Chris Wilson05394f32010-11-08 19:18:58 +0000615 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100616unlock:
Chris Wilson4f27b752010-10-14 15:26:45 +0100617 mutex_unlock(&dev->struct_mutex);
Eric Anholteb014592009-03-10 11:44:52 -0700618 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700619}
620
Keith Packard0839ccb2008-10-30 19:38:48 -0700621/* This is the fast write path which cannot handle
622 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700623 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700624
Keith Packard0839ccb2008-10-30 19:38:48 -0700625static inline int
626fast_user_write(struct io_mapping *mapping,
627 loff_t page_base, int page_offset,
628 char __user *user_data,
629 int length)
630{
631 char *vaddr_atomic;
632 unsigned long unwritten;
633
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700634 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
Keith Packard0839ccb2008-10-30 19:38:48 -0700635 unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
636 user_data, length);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700637 io_mapping_unmap_atomic(vaddr_atomic);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100638 return unwritten;
Keith Packard0839ccb2008-10-30 19:38:48 -0700639}
640
641/* Here's the write path which can sleep for
642 * page faults
643 */
644
Chris Wilsonab34c222010-05-27 14:15:35 +0100645static inline void
Eric Anholt3de09aa2009-03-09 09:42:23 -0700646slow_kernel_write(struct io_mapping *mapping,
647 loff_t gtt_base, int gtt_offset,
648 struct page *user_page, int user_offset,
649 int length)
Keith Packard0839ccb2008-10-30 19:38:48 -0700650{
Chris Wilsonab34c222010-05-27 14:15:35 +0100651 char __iomem *dst_vaddr;
652 char *src_vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -0700653
Chris Wilsonab34c222010-05-27 14:15:35 +0100654 dst_vaddr = io_mapping_map_wc(mapping, gtt_base);
655 src_vaddr = kmap(user_page);
656
657 memcpy_toio(dst_vaddr + gtt_offset,
658 src_vaddr + user_offset,
659 length);
660
661 kunmap(user_page);
662 io_mapping_unmap(dst_vaddr);
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700663}
664
Eric Anholt3de09aa2009-03-09 09:42:23 -0700665/**
666 * This is the fast pwrite path, where we copy the data directly from the
667 * user into the GTT, uncached.
668 */
Eric Anholt673a3942008-07-30 12:06:12 -0700669static int
Chris Wilson05394f32010-11-08 19:18:58 +0000670i915_gem_gtt_pwrite_fast(struct drm_device *dev,
671 struct drm_i915_gem_object *obj,
Eric Anholt3de09aa2009-03-09 09:42:23 -0700672 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000673 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700674{
Keith Packard0839ccb2008-10-30 19:38:48 -0700675 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700676 ssize_t remain;
Keith Packard0839ccb2008-10-30 19:38:48 -0700677 loff_t offset, page_base;
Eric Anholt673a3942008-07-30 12:06:12 -0700678 char __user *user_data;
Keith Packard0839ccb2008-10-30 19:38:48 -0700679 int page_offset, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700680
681 user_data = (char __user *) (uintptr_t) args->data_ptr;
682 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700683
Chris Wilson05394f32010-11-08 19:18:58 +0000684 offset = obj->gtt_offset + args->offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700685
686 while (remain > 0) {
687 /* Operation in this page
688 *
Keith Packard0839ccb2008-10-30 19:38:48 -0700689 * page_base = page offset within aperture
690 * page_offset = offset within page
691 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -0700692 */
Keith Packard0839ccb2008-10-30 19:38:48 -0700693 page_base = (offset & ~(PAGE_SIZE-1));
694 page_offset = offset & (PAGE_SIZE-1);
695 page_length = remain;
696 if ((page_offset + remain) > PAGE_SIZE)
697 page_length = PAGE_SIZE - page_offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700698
Keith Packard0839ccb2008-10-30 19:38:48 -0700699 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -0700700 * source page isn't available. Return the error and we'll
701 * retry in the slow path.
Keith Packard0839ccb2008-10-30 19:38:48 -0700702 */
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100703 if (fast_user_write(dev_priv->mm.gtt_mapping, page_base,
704 page_offset, user_data, page_length))
705
706 return -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -0700707
Keith Packard0839ccb2008-10-30 19:38:48 -0700708 remain -= page_length;
709 user_data += page_length;
710 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700711 }
Eric Anholt673a3942008-07-30 12:06:12 -0700712
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100713 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700714}
715
Eric Anholt3de09aa2009-03-09 09:42:23 -0700716/**
717 * This is the fallback GTT pwrite path, which uses get_user_pages to pin
718 * the memory and maps it using kmap_atomic for copying.
719 *
720 * This code resulted in x11perf -rgb10text consuming about 10% more CPU
721 * than using i915_gem_gtt_pwrite_fast on a G45 (32-bit).
722 */
Eric Anholt3043c602008-10-02 12:24:47 -0700723static int
Chris Wilson05394f32010-11-08 19:18:58 +0000724i915_gem_gtt_pwrite_slow(struct drm_device *dev,
725 struct drm_i915_gem_object *obj,
Eric Anholt3de09aa2009-03-09 09:42:23 -0700726 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000727 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700728{
Eric Anholt3de09aa2009-03-09 09:42:23 -0700729 drm_i915_private_t *dev_priv = dev->dev_private;
730 ssize_t remain;
731 loff_t gtt_page_base, offset;
732 loff_t first_data_page, last_data_page, num_pages;
733 loff_t pinned_pages, i;
734 struct page **user_pages;
735 struct mm_struct *mm = current->mm;
736 int gtt_page_offset, data_page_offset, data_page_index, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700737 int ret;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700738 uint64_t data_ptr = args->data_ptr;
739
740 remain = args->size;
741
742 /* Pin the user pages containing the data. We can't fault while
743 * holding the struct mutex, and all of the pwrite implementations
744 * want to hold it while dereferencing the user data.
745 */
746 first_data_page = data_ptr / PAGE_SIZE;
747 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
748 num_pages = last_data_page - first_data_page + 1;
749
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100750 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
Eric Anholt3de09aa2009-03-09 09:42:23 -0700751 if (user_pages == NULL)
752 return -ENOMEM;
753
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100754 mutex_unlock(&dev->struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700755 down_read(&mm->mmap_sem);
756 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
757 num_pages, 0, 0, user_pages, NULL);
758 up_read(&mm->mmap_sem);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100759 mutex_lock(&dev->struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700760 if (pinned_pages < num_pages) {
761 ret = -EFAULT;
762 goto out_unpin_pages;
763 }
764
Eric Anholt3de09aa2009-03-09 09:42:23 -0700765 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
766 if (ret)
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100767 goto out_unpin_pages;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700768
Chris Wilson05394f32010-11-08 19:18:58 +0000769 offset = obj->gtt_offset + args->offset;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700770
771 while (remain > 0) {
772 /* Operation in this page
773 *
774 * gtt_page_base = page offset within aperture
775 * gtt_page_offset = offset within page in aperture
776 * data_page_index = page number in get_user_pages return
777 * data_page_offset = offset with data_page_index page.
778 * page_length = bytes to copy for this page
779 */
780 gtt_page_base = offset & PAGE_MASK;
781 gtt_page_offset = offset & ~PAGE_MASK;
782 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
783 data_page_offset = data_ptr & ~PAGE_MASK;
784
785 page_length = remain;
786 if ((gtt_page_offset + page_length) > PAGE_SIZE)
787 page_length = PAGE_SIZE - gtt_page_offset;
788 if ((data_page_offset + page_length) > PAGE_SIZE)
789 page_length = PAGE_SIZE - data_page_offset;
790
Chris Wilsonab34c222010-05-27 14:15:35 +0100791 slow_kernel_write(dev_priv->mm.gtt_mapping,
792 gtt_page_base, gtt_page_offset,
793 user_pages[data_page_index],
794 data_page_offset,
795 page_length);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700796
797 remain -= page_length;
798 offset += page_length;
799 data_ptr += page_length;
800 }
801
Eric Anholt3de09aa2009-03-09 09:42:23 -0700802out_unpin_pages:
803 for (i = 0; i < pinned_pages; i++)
804 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700805 drm_free_large(user_pages);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700806
807 return ret;
808}
809
Eric Anholt40123c12009-03-09 13:42:30 -0700810/**
811 * This is the fast shmem pwrite path, which attempts to directly
812 * copy_from_user into the kmapped pages backing the object.
813 */
Eric Anholt673a3942008-07-30 12:06:12 -0700814static int
Chris Wilson05394f32010-11-08 19:18:58 +0000815i915_gem_shmem_pwrite_fast(struct drm_device *dev,
816 struct drm_i915_gem_object *obj,
Eric Anholt40123c12009-03-09 13:42:30 -0700817 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000818 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700819{
Chris Wilson05394f32010-11-08 19:18:58 +0000820 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Eric Anholt40123c12009-03-09 13:42:30 -0700821 ssize_t remain;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100822 loff_t offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700823 char __user *user_data;
824 int page_offset, page_length;
Eric Anholt40123c12009-03-09 13:42:30 -0700825
826 user_data = (char __user *) (uintptr_t) args->data_ptr;
827 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700828
Eric Anholt673a3942008-07-30 12:06:12 -0700829 offset = args->offset;
Chris Wilson05394f32010-11-08 19:18:58 +0000830 obj->dirty = 1;
Eric Anholt673a3942008-07-30 12:06:12 -0700831
Eric Anholt40123c12009-03-09 13:42:30 -0700832 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100833 struct page *page;
834 char *vaddr;
835 int ret;
836
Eric Anholt40123c12009-03-09 13:42:30 -0700837 /* Operation in this page
838 *
Eric Anholt40123c12009-03-09 13:42:30 -0700839 * page_offset = offset within page
840 * page_length = bytes to copy for this page
841 */
Eric Anholt40123c12009-03-09 13:42:30 -0700842 page_offset = offset & (PAGE_SIZE-1);
843 page_length = remain;
844 if ((page_offset + remain) > PAGE_SIZE)
845 page_length = PAGE_SIZE - page_offset;
846
Chris Wilsone5281cc2010-10-28 13:45:36 +0100847 page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
848 GFP_HIGHUSER | __GFP_RECLAIMABLE);
849 if (IS_ERR(page))
850 return PTR_ERR(page);
851
852 vaddr = kmap_atomic(page, KM_USER0);
853 ret = __copy_from_user_inatomic(vaddr + page_offset,
854 user_data,
855 page_length);
856 kunmap_atomic(vaddr, KM_USER0);
857
858 set_page_dirty(page);
859 mark_page_accessed(page);
860 page_cache_release(page);
861
862 /* If we get a fault while copying data, then (presumably) our
863 * source page isn't available. Return the error and we'll
864 * retry in the slow path.
865 */
866 if (ret)
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100867 return -EFAULT;
Eric Anholt40123c12009-03-09 13:42:30 -0700868
869 remain -= page_length;
870 user_data += page_length;
871 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700872 }
873
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100874 return 0;
Eric Anholt40123c12009-03-09 13:42:30 -0700875}
876
877/**
878 * This is the fallback shmem pwrite path, which uses get_user_pages to pin
879 * the memory and maps it using kmap_atomic for copying.
880 *
881 * This avoids taking mmap_sem for faulting on the user's address while the
882 * struct_mutex is held.
883 */
884static int
Chris Wilson05394f32010-11-08 19:18:58 +0000885i915_gem_shmem_pwrite_slow(struct drm_device *dev,
886 struct drm_i915_gem_object *obj,
Eric Anholt40123c12009-03-09 13:42:30 -0700887 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000888 struct drm_file *file)
Eric Anholt40123c12009-03-09 13:42:30 -0700889{
Chris Wilson05394f32010-11-08 19:18:58 +0000890 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Eric Anholt40123c12009-03-09 13:42:30 -0700891 struct mm_struct *mm = current->mm;
892 struct page **user_pages;
893 ssize_t remain;
894 loff_t offset, pinned_pages, i;
895 loff_t first_data_page, last_data_page, num_pages;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100896 int shmem_page_offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700897 int data_page_index, data_page_offset;
898 int page_length;
899 int ret;
900 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700901 int do_bit17_swizzling;
Eric Anholt40123c12009-03-09 13:42:30 -0700902
903 remain = args->size;
904
905 /* Pin the user pages containing the data. We can't fault while
906 * holding the struct mutex, and all of the pwrite implementations
907 * want to hold it while dereferencing the user data.
908 */
909 first_data_page = data_ptr / PAGE_SIZE;
910 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
911 num_pages = last_data_page - first_data_page + 1;
912
Chris Wilson4f27b752010-10-14 15:26:45 +0100913 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
Eric Anholt40123c12009-03-09 13:42:30 -0700914 if (user_pages == NULL)
915 return -ENOMEM;
916
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100917 mutex_unlock(&dev->struct_mutex);
Eric Anholt40123c12009-03-09 13:42:30 -0700918 down_read(&mm->mmap_sem);
919 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
920 num_pages, 0, 0, user_pages, NULL);
921 up_read(&mm->mmap_sem);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100922 mutex_lock(&dev->struct_mutex);
Eric Anholt40123c12009-03-09 13:42:30 -0700923 if (pinned_pages < num_pages) {
924 ret = -EFAULT;
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100925 goto out;
Eric Anholt40123c12009-03-09 13:42:30 -0700926 }
927
Eric Anholt40123c12009-03-09 13:42:30 -0700928 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100929 if (ret)
930 goto out;
931
932 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700933
Eric Anholt40123c12009-03-09 13:42:30 -0700934 offset = args->offset;
Chris Wilson05394f32010-11-08 19:18:58 +0000935 obj->dirty = 1;
Eric Anholt40123c12009-03-09 13:42:30 -0700936
937 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100938 struct page *page;
939
Eric Anholt40123c12009-03-09 13:42:30 -0700940 /* Operation in this page
941 *
Eric Anholt40123c12009-03-09 13:42:30 -0700942 * shmem_page_offset = offset within page in shmem file
943 * data_page_index = page number in get_user_pages return
944 * data_page_offset = offset with data_page_index page.
945 * page_length = bytes to copy for this page
946 */
Eric Anholt40123c12009-03-09 13:42:30 -0700947 shmem_page_offset = offset & ~PAGE_MASK;
948 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
949 data_page_offset = data_ptr & ~PAGE_MASK;
950
951 page_length = remain;
952 if ((shmem_page_offset + page_length) > PAGE_SIZE)
953 page_length = PAGE_SIZE - shmem_page_offset;
954 if ((data_page_offset + page_length) > PAGE_SIZE)
955 page_length = PAGE_SIZE - data_page_offset;
956
Chris Wilsone5281cc2010-10-28 13:45:36 +0100957 page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
958 GFP_HIGHUSER | __GFP_RECLAIMABLE);
959 if (IS_ERR(page)) {
960 ret = PTR_ERR(page);
961 goto out;
962 }
963
Eric Anholt280b7132009-03-12 16:56:27 -0700964 if (do_bit17_swizzling) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100965 slow_shmem_bit17_copy(page,
Eric Anholt280b7132009-03-12 16:56:27 -0700966 shmem_page_offset,
967 user_pages[data_page_index],
968 data_page_offset,
Chris Wilson99a03df2010-05-27 14:15:34 +0100969 page_length,
970 0);
971 } else {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100972 slow_shmem_copy(page,
Chris Wilson99a03df2010-05-27 14:15:34 +0100973 shmem_page_offset,
974 user_pages[data_page_index],
975 data_page_offset,
976 page_length);
Eric Anholt280b7132009-03-12 16:56:27 -0700977 }
Eric Anholt40123c12009-03-09 13:42:30 -0700978
Chris Wilsone5281cc2010-10-28 13:45:36 +0100979 set_page_dirty(page);
980 mark_page_accessed(page);
981 page_cache_release(page);
982
Eric Anholt40123c12009-03-09 13:42:30 -0700983 remain -= page_length;
984 data_ptr += page_length;
985 offset += page_length;
986 }
987
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100988out:
Eric Anholt40123c12009-03-09 13:42:30 -0700989 for (i = 0; i < pinned_pages; i++)
990 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700991 drm_free_large(user_pages);
Eric Anholt40123c12009-03-09 13:42:30 -0700992
993 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700994}
995
996/**
997 * Writes data to the object referenced by handle.
998 *
999 * On error, the contents of the buffer that were to be modified are undefined.
1000 */
1001int
1002i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001003 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001004{
1005 struct drm_i915_gem_pwrite *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001006 struct drm_i915_gem_object *obj;
Chris Wilson51311d02010-11-17 09:10:42 +00001007 int ret;
1008
1009 if (args->size == 0)
1010 return 0;
1011
1012 if (!access_ok(VERIFY_READ,
1013 (char __user *)(uintptr_t)args->data_ptr,
1014 args->size))
1015 return -EFAULT;
1016
1017 ret = fault_in_pages_readable((char __user *)(uintptr_t)args->data_ptr,
1018 args->size);
1019 if (ret)
1020 return -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -07001021
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001022 ret = i915_mutex_lock_interruptible(dev);
1023 if (ret)
1024 return ret;
1025
Chris Wilson05394f32010-11-08 19:18:58 +00001026 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001027 if (obj == NULL) {
1028 ret = -ENOENT;
1029 goto unlock;
1030 }
Eric Anholt673a3942008-07-30 12:06:12 -07001031
Chris Wilson7dcd2492010-09-26 20:21:44 +01001032 /* Bounds check destination. */
Chris Wilson05394f32010-11-08 19:18:58 +00001033 if (args->offset > obj->base.size ||
1034 args->size > obj->base.size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001035 ret = -EINVAL;
Chris Wilson35b62a82010-09-26 20:23:38 +01001036 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001037 }
1038
Eric Anholt673a3942008-07-30 12:06:12 -07001039 /* We can only do the GTT pwrite on untiled buffers, as otherwise
1040 * it would end up going through the fenced access, and we'll get
1041 * different detiling behavior between reading and writing.
1042 * pread/pwrite currently are reading and writing from the CPU
1043 * perspective, requiring manual detiling by the client.
1044 */
Chris Wilson05394f32010-11-08 19:18:58 +00001045 if (obj->phys_obj)
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001046 ret = i915_gem_phys_pwrite(dev, obj, args, file);
Chris Wilson05394f32010-11-08 19:18:58 +00001047 else if (obj->tiling_mode == I915_TILING_NONE &&
1048 obj->gtt_space &&
1049 obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01001050 ret = i915_gem_object_pin(obj, 0, true);
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001051 if (ret)
1052 goto out;
1053
1054 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
1055 if (ret)
1056 goto out_unpin;
1057
1058 ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file);
1059 if (ret == -EFAULT)
1060 ret = i915_gem_gtt_pwrite_slow(dev, obj, args, file);
1061
1062out_unpin:
1063 i915_gem_object_unpin(obj);
Eric Anholt40123c12009-03-09 13:42:30 -07001064 } else {
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001065 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
1066 if (ret)
Chris Wilsone5281cc2010-10-28 13:45:36 +01001067 goto out;
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001068
1069 ret = -EFAULT;
1070 if (!i915_gem_object_needs_bit17_swizzle(obj))
1071 ret = i915_gem_shmem_pwrite_fast(dev, obj, args, file);
1072 if (ret == -EFAULT)
1073 ret = i915_gem_shmem_pwrite_slow(dev, obj, args, file);
Eric Anholt40123c12009-03-09 13:42:30 -07001074 }
Eric Anholt673a3942008-07-30 12:06:12 -07001075
Chris Wilson35b62a82010-09-26 20:23:38 +01001076out:
Chris Wilson05394f32010-11-08 19:18:58 +00001077 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001078unlock:
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001079 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07001080 return ret;
1081}
1082
1083/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001084 * Called when user space prepares to use an object with the CPU, either
1085 * through the mmap ioctl's mapping or a GTT mapping.
Eric Anholt673a3942008-07-30 12:06:12 -07001086 */
1087int
1088i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001089 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001090{
Eric Anholta09ba7f2009-08-29 12:49:51 -07001091 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07001092 struct drm_i915_gem_set_domain *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001093 struct drm_i915_gem_object *obj;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001094 uint32_t read_domains = args->read_domains;
1095 uint32_t write_domain = args->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07001096 int ret;
1097
1098 if (!(dev->driver->driver_features & DRIVER_GEM))
1099 return -ENODEV;
1100
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001101 /* Only handle setting domains to types used by the CPU. */
Chris Wilson21d509e2009-06-06 09:46:02 +01001102 if (write_domain & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001103 return -EINVAL;
1104
Chris Wilson21d509e2009-06-06 09:46:02 +01001105 if (read_domains & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001106 return -EINVAL;
1107
1108 /* Having something in the write domain implies it's in the read
1109 * domain, and only that read domain. Enforce that in the request.
1110 */
1111 if (write_domain != 0 && read_domains != write_domain)
1112 return -EINVAL;
1113
Chris Wilson76c1dec2010-09-25 11:22:51 +01001114 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001115 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001116 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001117
Chris Wilson05394f32010-11-08 19:18:58 +00001118 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001119 if (obj == NULL) {
1120 ret = -ENOENT;
1121 goto unlock;
Chris Wilson76c1dec2010-09-25 11:22:51 +01001122 }
Jesse Barnes652c3932009-08-17 13:31:43 -07001123
1124 intel_mark_busy(dev, obj);
1125
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001126 if (read_domains & I915_GEM_DOMAIN_GTT) {
1127 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
Eric Anholt02354392008-11-26 13:58:13 -08001128
Eric Anholta09ba7f2009-08-29 12:49:51 -07001129 /* Update the LRU on the fence for the CPU access that's
1130 * about to occur.
1131 */
Chris Wilson05394f32010-11-08 19:18:58 +00001132 if (obj->fence_reg != I915_FENCE_REG_NONE) {
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001133 struct drm_i915_fence_reg *reg =
Chris Wilson05394f32010-11-08 19:18:58 +00001134 &dev_priv->fence_regs[obj->fence_reg];
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001135 list_move_tail(&reg->lru_list,
Eric Anholta09ba7f2009-08-29 12:49:51 -07001136 &dev_priv->mm.fence_list);
1137 }
1138
Eric Anholt02354392008-11-26 13:58:13 -08001139 /* Silently promote "you're not bound, there was nothing to do"
1140 * to success, since the client was just asking us to
1141 * make sure everything was done.
1142 */
1143 if (ret == -EINVAL)
1144 ret = 0;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001145 } else {
Eric Anholte47c68e2008-11-14 13:35:19 -08001146 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001147 }
1148
Chris Wilson7d1c4802010-08-07 21:45:03 +01001149 /* Maintain LRU order of "inactive" objects */
Chris Wilson05394f32010-11-08 19:18:58 +00001150 if (ret == 0 && i915_gem_object_is_inactive(obj))
1151 list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
Chris Wilson7d1c4802010-08-07 21:45:03 +01001152
Chris Wilson05394f32010-11-08 19:18:58 +00001153 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001154unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07001155 mutex_unlock(&dev->struct_mutex);
1156 return ret;
1157}
1158
1159/**
1160 * Called when user space has done writes to this buffer
1161 */
1162int
1163i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001164 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001165{
1166 struct drm_i915_gem_sw_finish *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001167 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001168 int ret = 0;
1169
1170 if (!(dev->driver->driver_features & DRIVER_GEM))
1171 return -ENODEV;
1172
Chris Wilson76c1dec2010-09-25 11:22:51 +01001173 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001174 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001175 return ret;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001176
Chris Wilson05394f32010-11-08 19:18:58 +00001177 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Eric Anholt673a3942008-07-30 12:06:12 -07001178 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001179 ret = -ENOENT;
1180 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07001181 }
1182
Eric Anholt673a3942008-07-30 12:06:12 -07001183 /* Pinned buffers may be scanout, so flush the cache */
Chris Wilson05394f32010-11-08 19:18:58 +00001184 if (obj->pin_count)
Eric Anholte47c68e2008-11-14 13:35:19 -08001185 i915_gem_object_flush_cpu_write_domain(obj);
1186
Chris Wilson05394f32010-11-08 19:18:58 +00001187 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001188unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07001189 mutex_unlock(&dev->struct_mutex);
1190 return ret;
1191}
1192
1193/**
1194 * Maps the contents of an object, returning the address it is mapped
1195 * into.
1196 *
1197 * While the mapping holds a reference on the contents of the object, it doesn't
1198 * imply a ref on the object itself.
1199 */
1200int
1201i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001202 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001203{
Chris Wilsonda761a62010-10-27 17:37:08 +01001204 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07001205 struct drm_i915_gem_mmap *args = data;
1206 struct drm_gem_object *obj;
1207 loff_t offset;
1208 unsigned long addr;
1209
1210 if (!(dev->driver->driver_features & DRIVER_GEM))
1211 return -ENODEV;
1212
Chris Wilson05394f32010-11-08 19:18:58 +00001213 obj = drm_gem_object_lookup(dev, file, args->handle);
Eric Anholt673a3942008-07-30 12:06:12 -07001214 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001215 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001216
Chris Wilsonda761a62010-10-27 17:37:08 +01001217 if (obj->size > dev_priv->mm.gtt_mappable_end) {
1218 drm_gem_object_unreference_unlocked(obj);
1219 return -E2BIG;
1220 }
1221
Eric Anholt673a3942008-07-30 12:06:12 -07001222 offset = args->offset;
1223
1224 down_write(&current->mm->mmap_sem);
1225 addr = do_mmap(obj->filp, 0, args->size,
1226 PROT_READ | PROT_WRITE, MAP_SHARED,
1227 args->offset);
1228 up_write(&current->mm->mmap_sem);
Luca Barbieribc9025b2010-02-09 05:49:12 +00001229 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001230 if (IS_ERR((void *)addr))
1231 return addr;
1232
1233 args->addr_ptr = (uint64_t) addr;
1234
1235 return 0;
1236}
1237
Jesse Barnesde151cf2008-11-12 10:03:55 -08001238/**
1239 * i915_gem_fault - fault a page into the GTT
1240 * vma: VMA in question
1241 * vmf: fault info
1242 *
1243 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1244 * from userspace. The fault handler takes care of binding the object to
1245 * the GTT (if needed), allocating and programming a fence register (again,
1246 * only if needed based on whether the old reg is still valid or the object
1247 * is tiled) and inserting a new PTE into the faulting process.
1248 *
1249 * Note that the faulting process may involve evicting existing objects
1250 * from the GTT and/or fence registers to make room. So performance may
1251 * suffer if the GTT working set is large or there are few fence registers
1252 * left.
1253 */
1254int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1255{
Chris Wilson05394f32010-11-08 19:18:58 +00001256 struct drm_i915_gem_object *obj = to_intel_bo(vma->vm_private_data);
1257 struct drm_device *dev = obj->base.dev;
Chris Wilson7d1c4802010-08-07 21:45:03 +01001258 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001259 pgoff_t page_offset;
1260 unsigned long pfn;
1261 int ret = 0;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001262 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001263
1264 /* We don't use vmf->pgoff since that has the fake offset */
1265 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1266 PAGE_SHIFT;
1267
1268 /* Now bind it into the GTT if needed */
1269 mutex_lock(&dev->struct_mutex);
Chris Wilson05394f32010-11-08 19:18:58 +00001270 BUG_ON(obj->pin_count && !obj->pin_mappable);
Chris Wilsona00b10c2010-09-24 21:15:47 +01001271
Chris Wilson919926a2010-11-12 13:42:53 +00001272 if (!obj->map_and_fenceable) {
1273 ret = i915_gem_object_unbind(obj);
1274 if (ret)
1275 goto unlock;
Chris Wilsona00b10c2010-09-24 21:15:47 +01001276 }
Daniel Vetter16e809a2010-09-16 19:37:04 +02001277
Chris Wilson05394f32010-11-08 19:18:58 +00001278 if (!obj->gtt_space) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01001279 ret = i915_gem_object_bind_to_gtt(obj, 0, true);
Chris Wilsonc7150892009-09-23 00:43:56 +01001280 if (ret)
1281 goto unlock;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001282 }
1283
Chris Wilson4a684a42010-10-28 14:44:08 +01001284 ret = i915_gem_object_set_to_gtt_domain(obj, write);
1285 if (ret)
1286 goto unlock;
1287
Chris Wilson05394f32010-11-08 19:18:58 +00001288 if (!obj->fault_mappable) {
1289 obj->fault_mappable = true;
1290 i915_gem_info_update_mappable(dev_priv, obj, true);
Daniel Vetterfb7d5162010-10-01 22:05:20 +02001291 }
1292
Jesse Barnesde151cf2008-11-12 10:03:55 -08001293 /* Need a new fence register? */
Chris Wilson05394f32010-11-08 19:18:58 +00001294 if (obj->tiling_mode != I915_TILING_NONE) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01001295 ret = i915_gem_object_get_fence_reg(obj, true);
Chris Wilsonc7150892009-09-23 00:43:56 +01001296 if (ret)
1297 goto unlock;
Eric Anholtd9ddcb92009-01-27 10:33:49 -08001298 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001299
Chris Wilson05394f32010-11-08 19:18:58 +00001300 if (i915_gem_object_is_inactive(obj))
1301 list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
Chris Wilson7d1c4802010-08-07 21:45:03 +01001302
Chris Wilson05394f32010-11-08 19:18:58 +00001303 pfn = ((dev->agp->base + obj->gtt_offset) >> PAGE_SHIFT) +
Jesse Barnesde151cf2008-11-12 10:03:55 -08001304 page_offset;
1305
1306 /* Finally, remap it using the new GTT offset */
1307 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
Chris Wilsonc7150892009-09-23 00:43:56 +01001308unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001309 mutex_unlock(&dev->struct_mutex);
1310
1311 switch (ret) {
Chris Wilson045e7692010-11-07 09:18:22 +00001312 case -EAGAIN:
1313 set_need_resched();
Chris Wilsonc7150892009-09-23 00:43:56 +01001314 case 0:
1315 case -ERESTARTSYS:
1316 return VM_FAULT_NOPAGE;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001317 case -ENOMEM:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001318 return VM_FAULT_OOM;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001319 default:
Chris Wilsonc7150892009-09-23 00:43:56 +01001320 return VM_FAULT_SIGBUS;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001321 }
1322}
1323
1324/**
1325 * i915_gem_create_mmap_offset - create a fake mmap offset for an object
1326 * @obj: obj in question
1327 *
1328 * GEM memory mapping works by handing back to userspace a fake mmap offset
1329 * it can use in a subsequent mmap(2) call. The DRM core code then looks
1330 * up the object based on the offset and sets up the various memory mapping
1331 * structures.
1332 *
1333 * This routine allocates and attaches a fake offset for @obj.
1334 */
1335static int
Chris Wilson05394f32010-11-08 19:18:58 +00001336i915_gem_create_mmap_offset(struct drm_i915_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001337{
Chris Wilson05394f32010-11-08 19:18:58 +00001338 struct drm_device *dev = obj->base.dev;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001339 struct drm_gem_mm *mm = dev->mm_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001340 struct drm_map_list *list;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +11001341 struct drm_local_map *map;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001342 int ret = 0;
1343
1344 /* Set the object up for mmap'ing */
Chris Wilson05394f32010-11-08 19:18:58 +00001345 list = &obj->base.map_list;
Eric Anholt9a298b22009-03-24 12:23:04 -07001346 list->map = kzalloc(sizeof(struct drm_map_list), GFP_KERNEL);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001347 if (!list->map)
1348 return -ENOMEM;
1349
1350 map = list->map;
1351 map->type = _DRM_GEM;
Chris Wilson05394f32010-11-08 19:18:58 +00001352 map->size = obj->base.size;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001353 map->handle = obj;
1354
1355 /* Get a DRM GEM mmap offset allocated... */
1356 list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
Chris Wilson05394f32010-11-08 19:18:58 +00001357 obj->base.size / PAGE_SIZE,
1358 0, 0);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001359 if (!list->file_offset_node) {
Chris Wilson05394f32010-11-08 19:18:58 +00001360 DRM_ERROR("failed to allocate offset for bo %d\n",
1361 obj->base.name);
Chris Wilson9e0ae5342010-09-21 15:05:24 +01001362 ret = -ENOSPC;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001363 goto out_free_list;
1364 }
1365
1366 list->file_offset_node = drm_mm_get_block(list->file_offset_node,
Chris Wilson05394f32010-11-08 19:18:58 +00001367 obj->base.size / PAGE_SIZE,
1368 0);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001369 if (!list->file_offset_node) {
1370 ret = -ENOMEM;
1371 goto out_free_list;
1372 }
1373
1374 list->hash.key = list->file_offset_node->start;
Chris Wilson9e0ae5342010-09-21 15:05:24 +01001375 ret = drm_ht_insert_item(&mm->offset_hash, &list->hash);
1376 if (ret) {
Jesse Barnesde151cf2008-11-12 10:03:55 -08001377 DRM_ERROR("failed to add to map hash\n");
1378 goto out_free_mm;
1379 }
1380
Jesse Barnesde151cf2008-11-12 10:03:55 -08001381 return 0;
1382
1383out_free_mm:
1384 drm_mm_put_block(list->file_offset_node);
1385out_free_list:
Eric Anholt9a298b22009-03-24 12:23:04 -07001386 kfree(list->map);
Chris Wilson39a01d12010-10-28 13:03:06 +01001387 list->map = NULL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001388
1389 return ret;
1390}
1391
Chris Wilson901782b2009-07-10 08:18:50 +01001392/**
1393 * i915_gem_release_mmap - remove physical page mappings
1394 * @obj: obj in question
1395 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001396 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01001397 * relinquish ownership of the pages back to the system.
1398 *
1399 * It is vital that we remove the page mapping if we have mapped a tiled
1400 * object through the GTT and then lose the fence register due to
1401 * resource pressure. Similarly if the object has been moved out of the
1402 * aperture, than pages mapped into userspace must be revoked. Removing the
1403 * mapping will then trigger a page fault on the next user access, allowing
1404 * fixup by i915_gem_fault().
1405 */
Eric Anholtd05ca302009-07-10 13:02:26 -07001406void
Chris Wilson05394f32010-11-08 19:18:58 +00001407i915_gem_release_mmap(struct drm_i915_gem_object *obj)
Chris Wilson901782b2009-07-10 08:18:50 +01001408{
Chris Wilson05394f32010-11-08 19:18:58 +00001409 struct drm_device *dev = obj->base.dev;
Daniel Vetterfb7d5162010-10-01 22:05:20 +02001410 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson901782b2009-07-10 08:18:50 +01001411
Chris Wilson05394f32010-11-08 19:18:58 +00001412 if (unlikely(obj->base.map_list.map && dev->dev_mapping))
Chris Wilson901782b2009-07-10 08:18:50 +01001413 unmap_mapping_range(dev->dev_mapping,
Chris Wilson05394f32010-11-08 19:18:58 +00001414 (loff_t)obj->base.map_list.hash.key<<PAGE_SHIFT,
1415 obj->base.size, 1);
Daniel Vetterfb7d5162010-10-01 22:05:20 +02001416
Chris Wilson05394f32010-11-08 19:18:58 +00001417 if (obj->fault_mappable) {
1418 obj->fault_mappable = false;
1419 i915_gem_info_update_mappable(dev_priv, obj, false);
Daniel Vetterfb7d5162010-10-01 22:05:20 +02001420 }
Chris Wilson901782b2009-07-10 08:18:50 +01001421}
1422
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001423static void
Chris Wilson05394f32010-11-08 19:18:58 +00001424i915_gem_free_mmap_offset(struct drm_i915_gem_object *obj)
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001425{
Chris Wilson05394f32010-11-08 19:18:58 +00001426 struct drm_device *dev = obj->base.dev;
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001427 struct drm_gem_mm *mm = dev->mm_private;
Chris Wilson05394f32010-11-08 19:18:58 +00001428 struct drm_map_list *list = &obj->base.map_list;
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001429
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001430 drm_ht_remove_item(&mm->offset_hash, &list->hash);
Chris Wilson39a01d12010-10-28 13:03:06 +01001431 drm_mm_put_block(list->file_offset_node);
1432 kfree(list->map);
1433 list->map = NULL;
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001434}
1435
Chris Wilson92b88ae2010-11-09 11:47:32 +00001436static uint32_t
1437i915_gem_get_gtt_size(struct drm_i915_gem_object *obj)
1438{
1439 struct drm_device *dev = obj->base.dev;
1440 uint32_t size;
1441
1442 if (INTEL_INFO(dev)->gen >= 4 ||
1443 obj->tiling_mode == I915_TILING_NONE)
1444 return obj->base.size;
1445
1446 /* Previous chips need a power-of-two fence region when tiling */
1447 if (INTEL_INFO(dev)->gen == 3)
1448 size = 1024*1024;
1449 else
1450 size = 512*1024;
1451
1452 while (size < obj->base.size)
1453 size <<= 1;
1454
1455 return size;
1456}
1457
Jesse Barnesde151cf2008-11-12 10:03:55 -08001458/**
1459 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1460 * @obj: object to check
1461 *
1462 * Return the required GTT alignment for an object, taking into account
Daniel Vetter5e783302010-11-14 22:32:36 +01001463 * potential fence register mapping.
Jesse Barnesde151cf2008-11-12 10:03:55 -08001464 */
1465static uint32_t
Chris Wilson05394f32010-11-08 19:18:58 +00001466i915_gem_get_gtt_alignment(struct drm_i915_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001467{
Chris Wilson05394f32010-11-08 19:18:58 +00001468 struct drm_device *dev = obj->base.dev;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001469
1470 /*
1471 * Minimum alignment is 4k (GTT page size), but might be greater
1472 * if a fence register is needed for the object.
1473 */
Chris Wilsona00b10c2010-09-24 21:15:47 +01001474 if (INTEL_INFO(dev)->gen >= 4 ||
Chris Wilson05394f32010-11-08 19:18:58 +00001475 obj->tiling_mode == I915_TILING_NONE)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001476 return 4096;
1477
1478 /*
1479 * Previous chips need to be aligned to the size of the smallest
1480 * fence register that can contain the object.
1481 */
Chris Wilson05394f32010-11-08 19:18:58 +00001482 return i915_gem_get_gtt_size(obj);
Chris Wilsona00b10c2010-09-24 21:15:47 +01001483}
1484
Daniel Vetter5e783302010-11-14 22:32:36 +01001485/**
1486 * i915_gem_get_unfenced_gtt_alignment - return required GTT alignment for an
1487 * unfenced object
1488 * @obj: object to check
1489 *
1490 * Return the required GTT alignment for an object, only taking into account
1491 * unfenced tiled surface requirements.
1492 */
1493static uint32_t
Chris Wilson05394f32010-11-08 19:18:58 +00001494i915_gem_get_unfenced_gtt_alignment(struct drm_i915_gem_object *obj)
Daniel Vetter5e783302010-11-14 22:32:36 +01001495{
Chris Wilson05394f32010-11-08 19:18:58 +00001496 struct drm_device *dev = obj->base.dev;
Daniel Vetter5e783302010-11-14 22:32:36 +01001497 int tile_height;
1498
1499 /*
1500 * Minimum alignment is 4k (GTT page size) for sane hw.
1501 */
1502 if (INTEL_INFO(dev)->gen >= 4 || IS_G33(dev) ||
Chris Wilson05394f32010-11-08 19:18:58 +00001503 obj->tiling_mode == I915_TILING_NONE)
Daniel Vetter5e783302010-11-14 22:32:36 +01001504 return 4096;
1505
1506 /*
1507 * Older chips need unfenced tiled buffers to be aligned to the left
1508 * edge of an even tile row (where tile rows are counted as if the bo is
1509 * placed in a fenced gtt region).
1510 */
1511 if (IS_GEN2(dev) ||
Chris Wilson05394f32010-11-08 19:18:58 +00001512 (obj->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev)))
Daniel Vetter5e783302010-11-14 22:32:36 +01001513 tile_height = 32;
1514 else
1515 tile_height = 8;
1516
Chris Wilson05394f32010-11-08 19:18:58 +00001517 return tile_height * obj->stride * 2;
Daniel Vetter5e783302010-11-14 22:32:36 +01001518}
1519
Jesse Barnesde151cf2008-11-12 10:03:55 -08001520/**
1521 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1522 * @dev: DRM device
1523 * @data: GTT mapping ioctl data
Chris Wilson05394f32010-11-08 19:18:58 +00001524 * @file: GEM object info
Jesse Barnesde151cf2008-11-12 10:03:55 -08001525 *
1526 * Simply returns the fake offset to userspace so it can mmap it.
1527 * The mmap call will end up in drm_gem_mmap(), which will set things
1528 * up so we can get faults in the handler above.
1529 *
1530 * The fault handler will take care of binding the object into the GTT
1531 * (since it may have been evicted to make room for something), allocating
1532 * a fence register, and mapping the appropriate aperture address into
1533 * userspace.
1534 */
1535int
1536i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001537 struct drm_file *file)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001538{
Chris Wilsonda761a62010-10-27 17:37:08 +01001539 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001540 struct drm_i915_gem_mmap_gtt *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001541 struct drm_i915_gem_object *obj;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001542 int ret;
1543
1544 if (!(dev->driver->driver_features & DRIVER_GEM))
1545 return -ENODEV;
1546
Chris Wilson76c1dec2010-09-25 11:22:51 +01001547 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001548 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001549 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001550
Chris Wilson05394f32010-11-08 19:18:58 +00001551 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001552 if (obj == NULL) {
1553 ret = -ENOENT;
1554 goto unlock;
1555 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001556
Chris Wilson05394f32010-11-08 19:18:58 +00001557 if (obj->base.size > dev_priv->mm.gtt_mappable_end) {
Chris Wilsonda761a62010-10-27 17:37:08 +01001558 ret = -E2BIG;
1559 goto unlock;
1560 }
1561
Chris Wilson05394f32010-11-08 19:18:58 +00001562 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilsonab182822009-09-22 18:46:17 +01001563 DRM_ERROR("Attempting to mmap a purgeable buffer\n");
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001564 ret = -EINVAL;
1565 goto out;
Chris Wilsonab182822009-09-22 18:46:17 +01001566 }
1567
Chris Wilson05394f32010-11-08 19:18:58 +00001568 if (!obj->base.map_list.map) {
Jesse Barnesde151cf2008-11-12 10:03:55 -08001569 ret = i915_gem_create_mmap_offset(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001570 if (ret)
1571 goto out;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001572 }
1573
Chris Wilson05394f32010-11-08 19:18:58 +00001574 args->offset = (u64)obj->base.map_list.hash.key << PAGE_SHIFT;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001575
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001576out:
Chris Wilson05394f32010-11-08 19:18:58 +00001577 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001578unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001579 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001580 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001581}
1582
Chris Wilsone5281cc2010-10-28 13:45:36 +01001583static int
Chris Wilson05394f32010-11-08 19:18:58 +00001584i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj,
Chris Wilsone5281cc2010-10-28 13:45:36 +01001585 gfp_t gfpmask)
1586{
Chris Wilsone5281cc2010-10-28 13:45:36 +01001587 int page_count, i;
1588 struct address_space *mapping;
1589 struct inode *inode;
1590 struct page *page;
1591
1592 /* Get the list of pages out of our struct file. They'll be pinned
1593 * at this point until we release them.
1594 */
Chris Wilson05394f32010-11-08 19:18:58 +00001595 page_count = obj->base.size / PAGE_SIZE;
1596 BUG_ON(obj->pages != NULL);
1597 obj->pages = drm_malloc_ab(page_count, sizeof(struct page *));
1598 if (obj->pages == NULL)
Chris Wilsone5281cc2010-10-28 13:45:36 +01001599 return -ENOMEM;
1600
Chris Wilson05394f32010-11-08 19:18:58 +00001601 inode = obj->base.filp->f_path.dentry->d_inode;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001602 mapping = inode->i_mapping;
1603 for (i = 0; i < page_count; i++) {
1604 page = read_cache_page_gfp(mapping, i,
1605 GFP_HIGHUSER |
1606 __GFP_COLD |
1607 __GFP_RECLAIMABLE |
1608 gfpmask);
1609 if (IS_ERR(page))
1610 goto err_pages;
1611
Chris Wilson05394f32010-11-08 19:18:58 +00001612 obj->pages[i] = page;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001613 }
1614
Chris Wilson05394f32010-11-08 19:18:58 +00001615 if (obj->tiling_mode != I915_TILING_NONE)
Chris Wilsone5281cc2010-10-28 13:45:36 +01001616 i915_gem_object_do_bit_17_swizzle(obj);
1617
1618 return 0;
1619
1620err_pages:
1621 while (i--)
Chris Wilson05394f32010-11-08 19:18:58 +00001622 page_cache_release(obj->pages[i]);
Chris Wilsone5281cc2010-10-28 13:45:36 +01001623
Chris Wilson05394f32010-11-08 19:18:58 +00001624 drm_free_large(obj->pages);
1625 obj->pages = NULL;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001626 return PTR_ERR(page);
1627}
1628
Chris Wilson5cdf5882010-09-27 15:51:07 +01001629static void
Chris Wilson05394f32010-11-08 19:18:58 +00001630i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07001631{
Chris Wilson05394f32010-11-08 19:18:58 +00001632 int page_count = obj->base.size / PAGE_SIZE;
Eric Anholt673a3942008-07-30 12:06:12 -07001633 int i;
1634
Chris Wilson05394f32010-11-08 19:18:58 +00001635 BUG_ON(obj->madv == __I915_MADV_PURGED);
Eric Anholt856fa192009-03-19 14:10:50 -07001636
Chris Wilson05394f32010-11-08 19:18:58 +00001637 if (obj->tiling_mode != I915_TILING_NONE)
Eric Anholt280b7132009-03-12 16:56:27 -07001638 i915_gem_object_save_bit_17_swizzle(obj);
1639
Chris Wilson05394f32010-11-08 19:18:58 +00001640 if (obj->madv == I915_MADV_DONTNEED)
1641 obj->dirty = 0;
Chris Wilson3ef94da2009-09-14 16:50:29 +01001642
1643 for (i = 0; i < page_count; i++) {
Chris Wilson05394f32010-11-08 19:18:58 +00001644 if (obj->dirty)
1645 set_page_dirty(obj->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001646
Chris Wilson05394f32010-11-08 19:18:58 +00001647 if (obj->madv == I915_MADV_WILLNEED)
1648 mark_page_accessed(obj->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001649
Chris Wilson05394f32010-11-08 19:18:58 +00001650 page_cache_release(obj->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001651 }
Chris Wilson05394f32010-11-08 19:18:58 +00001652 obj->dirty = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001653
Chris Wilson05394f32010-11-08 19:18:58 +00001654 drm_free_large(obj->pages);
1655 obj->pages = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001656}
1657
Chris Wilsona56ba562010-09-28 10:07:56 +01001658static uint32_t
1659i915_gem_next_request_seqno(struct drm_device *dev,
1660 struct intel_ring_buffer *ring)
1661{
1662 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson5d97eb62010-11-10 20:40:02 +00001663 return ring->outstanding_lazy_request = dev_priv->next_seqno;
Chris Wilsona56ba562010-09-28 10:07:56 +01001664}
1665
Eric Anholt673a3942008-07-30 12:06:12 -07001666static void
Chris Wilson05394f32010-11-08 19:18:58 +00001667i915_gem_object_move_to_active(struct drm_i915_gem_object *obj,
Zou Nan hai852835f2010-05-21 09:08:56 +08001668 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001669{
Chris Wilson05394f32010-11-08 19:18:58 +00001670 struct drm_device *dev = obj->base.dev;
Chris Wilson69dc4982010-10-19 10:36:51 +01001671 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona56ba562010-09-28 10:07:56 +01001672 uint32_t seqno = i915_gem_next_request_seqno(dev, ring);
Daniel Vetter617dbe22010-02-11 22:16:02 +01001673
Zou Nan hai852835f2010-05-21 09:08:56 +08001674 BUG_ON(ring == NULL);
Chris Wilson05394f32010-11-08 19:18:58 +00001675 obj->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001676
1677 /* Add a reference if we're newly entering the active list. */
Chris Wilson05394f32010-11-08 19:18:58 +00001678 if (!obj->active) {
1679 drm_gem_object_reference(&obj->base);
1680 obj->active = 1;
Eric Anholt673a3942008-07-30 12:06:12 -07001681 }
Daniel Vettere35a41d2010-02-11 22:13:59 +01001682
Eric Anholt673a3942008-07-30 12:06:12 -07001683 /* Move from whatever list we were on to the tail of execution. */
Chris Wilson05394f32010-11-08 19:18:58 +00001684 list_move_tail(&obj->mm_list, &dev_priv->mm.active_list);
1685 list_move_tail(&obj->ring_list, &ring->active_list);
Chris Wilsoncaea7472010-11-12 13:53:37 +00001686
Chris Wilson05394f32010-11-08 19:18:58 +00001687 obj->last_rendering_seqno = seqno;
Chris Wilsoncaea7472010-11-12 13:53:37 +00001688 if (obj->fenced_gpu_access) {
1689 struct drm_i915_fence_reg *reg;
1690
1691 BUG_ON(obj->fence_reg == I915_FENCE_REG_NONE);
1692
1693 obj->last_fenced_seqno = seqno;
1694 obj->last_fenced_ring = ring;
1695
1696 reg = &dev_priv->fence_regs[obj->fence_reg];
1697 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
1698 }
1699}
1700
1701static void
1702i915_gem_object_move_off_active(struct drm_i915_gem_object *obj)
1703{
1704 list_del_init(&obj->ring_list);
1705 obj->last_rendering_seqno = 0;
1706 obj->last_fenced_seqno = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001707}
1708
Eric Anholtce44b0e2008-11-06 16:00:31 -08001709static void
Chris Wilson05394f32010-11-08 19:18:58 +00001710i915_gem_object_move_to_flushing(struct drm_i915_gem_object *obj)
Eric Anholtce44b0e2008-11-06 16:00:31 -08001711{
Chris Wilson05394f32010-11-08 19:18:58 +00001712 struct drm_device *dev = obj->base.dev;
Eric Anholtce44b0e2008-11-06 16:00:31 -08001713 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtce44b0e2008-11-06 16:00:31 -08001714
Chris Wilson05394f32010-11-08 19:18:58 +00001715 BUG_ON(!obj->active);
1716 list_move_tail(&obj->mm_list, &dev_priv->mm.flushing_list);
Chris Wilsoncaea7472010-11-12 13:53:37 +00001717
1718 i915_gem_object_move_off_active(obj);
1719}
1720
1721static void
1722i915_gem_object_move_to_inactive(struct drm_i915_gem_object *obj)
1723{
1724 struct drm_device *dev = obj->base.dev;
1725 struct drm_i915_private *dev_priv = dev->dev_private;
1726
1727 if (obj->pin_count != 0)
1728 list_move_tail(&obj->mm_list, &dev_priv->mm.pinned_list);
1729 else
1730 list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
1731
1732 BUG_ON(!list_empty(&obj->gpu_write_list));
1733 BUG_ON(!obj->active);
1734 obj->ring = NULL;
1735
1736 i915_gem_object_move_off_active(obj);
1737 obj->fenced_gpu_access = false;
1738 obj->last_fenced_ring = NULL;
1739
1740 obj->active = 0;
1741 drm_gem_object_unreference(&obj->base);
1742
1743 WARN_ON(i915_verify_lists(dev));
Eric Anholtce44b0e2008-11-06 16:00:31 -08001744}
Eric Anholt673a3942008-07-30 12:06:12 -07001745
Chris Wilson963b4832009-09-20 23:03:54 +01001746/* Immediately discard the backing storage */
1747static void
Chris Wilson05394f32010-11-08 19:18:58 +00001748i915_gem_object_truncate(struct drm_i915_gem_object *obj)
Chris Wilson963b4832009-09-20 23:03:54 +01001749{
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001750 struct inode *inode;
Chris Wilson963b4832009-09-20 23:03:54 +01001751
Chris Wilsonae9fed62010-08-07 11:01:30 +01001752 /* Our goal here is to return as much of the memory as
1753 * is possible back to the system as we are called from OOM.
1754 * To do this we must instruct the shmfs to drop all of its
1755 * backing pages, *now*. Here we mirror the actions taken
1756 * when by shmem_delete_inode() to release the backing store.
1757 */
Chris Wilson05394f32010-11-08 19:18:58 +00001758 inode = obj->base.filp->f_path.dentry->d_inode;
Chris Wilsonae9fed62010-08-07 11:01:30 +01001759 truncate_inode_pages(inode->i_mapping, 0);
1760 if (inode->i_op->truncate_range)
1761 inode->i_op->truncate_range(inode, 0, (loff_t)-1);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001762
Chris Wilson05394f32010-11-08 19:18:58 +00001763 obj->madv = __I915_MADV_PURGED;
Chris Wilson963b4832009-09-20 23:03:54 +01001764}
1765
1766static inline int
Chris Wilson05394f32010-11-08 19:18:58 +00001767i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj)
Chris Wilson963b4832009-09-20 23:03:54 +01001768{
Chris Wilson05394f32010-11-08 19:18:58 +00001769 return obj->madv == I915_MADV_DONTNEED;
Chris Wilson963b4832009-09-20 23:03:54 +01001770}
1771
Eric Anholt673a3942008-07-30 12:06:12 -07001772static void
Daniel Vetter63560392010-02-19 11:51:59 +01001773i915_gem_process_flushing_list(struct drm_device *dev,
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001774 uint32_t flush_domains,
Zou Nan hai852835f2010-05-21 09:08:56 +08001775 struct intel_ring_buffer *ring)
Daniel Vetter63560392010-02-19 11:51:59 +01001776{
Chris Wilson05394f32010-11-08 19:18:58 +00001777 struct drm_i915_gem_object *obj, *next;
Daniel Vetter63560392010-02-19 11:51:59 +01001778
Chris Wilson05394f32010-11-08 19:18:58 +00001779 list_for_each_entry_safe(obj, next,
Chris Wilson64193402010-10-24 12:38:05 +01001780 &ring->gpu_write_list,
Daniel Vetter63560392010-02-19 11:51:59 +01001781 gpu_write_list) {
Chris Wilson05394f32010-11-08 19:18:58 +00001782 if (obj->base.write_domain & flush_domains) {
1783 uint32_t old_write_domain = obj->base.write_domain;
Daniel Vetter63560392010-02-19 11:51:59 +01001784
Chris Wilson05394f32010-11-08 19:18:58 +00001785 obj->base.write_domain = 0;
1786 list_del_init(&obj->gpu_write_list);
Daniel Vetter617dbe22010-02-11 22:16:02 +01001787 i915_gem_object_move_to_active(obj, ring);
Daniel Vetter63560392010-02-19 11:51:59 +01001788
Daniel Vetter63560392010-02-19 11:51:59 +01001789 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00001790 obj->base.read_domains,
Daniel Vetter63560392010-02-19 11:51:59 +01001791 old_write_domain);
1792 }
1793 }
1794}
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001795
Chris Wilson3cce4692010-10-27 16:11:02 +01001796int
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001797i915_add_request(struct drm_device *dev,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001798 struct drm_file *file,
Chris Wilson8dc5d142010-08-12 12:36:12 +01001799 struct drm_i915_gem_request *request,
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001800 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001801{
1802 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001803 struct drm_i915_file_private *file_priv = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001804 uint32_t seqno;
1805 int was_empty;
Chris Wilson3cce4692010-10-27 16:11:02 +01001806 int ret;
1807
1808 BUG_ON(request == NULL);
Eric Anholt673a3942008-07-30 12:06:12 -07001809
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001810 if (file != NULL)
1811 file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00001812
Chris Wilson3cce4692010-10-27 16:11:02 +01001813 ret = ring->add_request(ring, &seqno);
1814 if (ret)
1815 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001816
Chris Wilsona56ba562010-09-28 10:07:56 +01001817 ring->outstanding_lazy_request = false;
Eric Anholt673a3942008-07-30 12:06:12 -07001818
1819 request->seqno = seqno;
Zou Nan hai852835f2010-05-21 09:08:56 +08001820 request->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001821 request->emitted_jiffies = jiffies;
Zou Nan hai852835f2010-05-21 09:08:56 +08001822 was_empty = list_empty(&ring->request_list);
1823 list_add_tail(&request->list, &ring->request_list);
1824
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001825 if (file_priv) {
Chris Wilson1c255952010-09-26 11:03:27 +01001826 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001827 request->file_priv = file_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00001828 list_add_tail(&request->client_list,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001829 &file_priv->mm.request_list);
Chris Wilson1c255952010-09-26 11:03:27 +01001830 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00001831 }
Eric Anholt673a3942008-07-30 12:06:12 -07001832
Ben Gamarif65d9422009-09-14 17:48:44 -04001833 if (!dev_priv->mm.suspended) {
Chris Wilsonb3b079d2010-09-13 23:44:34 +01001834 mod_timer(&dev_priv->hangcheck_timer,
1835 jiffies + msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
Ben Gamarif65d9422009-09-14 17:48:44 -04001836 if (was_empty)
Chris Wilsonb3b079d2010-09-13 23:44:34 +01001837 queue_delayed_work(dev_priv->wq,
1838 &dev_priv->mm.retire_work, HZ);
Ben Gamarif65d9422009-09-14 17:48:44 -04001839 }
Chris Wilson3cce4692010-10-27 16:11:02 +01001840 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001841}
1842
1843/**
1844 * Command execution barrier
1845 *
1846 * Ensures that all commands in the ring are finished
1847 * before signalling the CPU
1848 */
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001849static void
Zou Nan hai852835f2010-05-21 09:08:56 +08001850i915_retire_commands(struct drm_device *dev, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001851{
Eric Anholt673a3942008-07-30 12:06:12 -07001852 uint32_t flush_domains = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001853
1854 /* The sampler always gets flushed on i965 (sigh) */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001855 if (INTEL_INFO(dev)->gen >= 4)
Eric Anholt673a3942008-07-30 12:06:12 -07001856 flush_domains |= I915_GEM_DOMAIN_SAMPLER;
Zou Nan hai852835f2010-05-21 09:08:56 +08001857
Chris Wilson78501ea2010-10-27 12:18:21 +01001858 ring->flush(ring, I915_GEM_DOMAIN_COMMAND, flush_domains);
Eric Anholt673a3942008-07-30 12:06:12 -07001859}
1860
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001861static inline void
1862i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
Eric Anholt673a3942008-07-30 12:06:12 -07001863{
Chris Wilson1c255952010-09-26 11:03:27 +01001864 struct drm_i915_file_private *file_priv = request->file_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07001865
Chris Wilson1c255952010-09-26 11:03:27 +01001866 if (!file_priv)
1867 return;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001868
Chris Wilson1c255952010-09-26 11:03:27 +01001869 spin_lock(&file_priv->mm.lock);
1870 list_del(&request->client_list);
1871 request->file_priv = NULL;
1872 spin_unlock(&file_priv->mm.lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001873}
1874
Chris Wilsondfaae392010-09-22 10:31:52 +01001875static void i915_gem_reset_ring_lists(struct drm_i915_private *dev_priv,
1876 struct intel_ring_buffer *ring)
Chris Wilson9375e442010-09-19 12:21:28 +01001877{
Chris Wilsondfaae392010-09-22 10:31:52 +01001878 while (!list_empty(&ring->request_list)) {
1879 struct drm_i915_gem_request *request;
Chris Wilson9375e442010-09-19 12:21:28 +01001880
Chris Wilsondfaae392010-09-22 10:31:52 +01001881 request = list_first_entry(&ring->request_list,
1882 struct drm_i915_gem_request,
1883 list);
1884
1885 list_del(&request->list);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001886 i915_gem_request_remove_from_client(request);
Chris Wilsondfaae392010-09-22 10:31:52 +01001887 kfree(request);
1888 }
1889
1890 while (!list_empty(&ring->active_list)) {
Chris Wilson05394f32010-11-08 19:18:58 +00001891 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001892
Chris Wilson05394f32010-11-08 19:18:58 +00001893 obj = list_first_entry(&ring->active_list,
1894 struct drm_i915_gem_object,
1895 ring_list);
Eric Anholt673a3942008-07-30 12:06:12 -07001896
Chris Wilson05394f32010-11-08 19:18:58 +00001897 obj->base.write_domain = 0;
1898 list_del_init(&obj->gpu_write_list);
1899 i915_gem_object_move_to_inactive(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001900 }
Eric Anholt673a3942008-07-30 12:06:12 -07001901}
1902
Chris Wilson312817a2010-11-22 11:50:11 +00001903static void i915_gem_reset_fences(struct drm_device *dev)
1904{
1905 struct drm_i915_private *dev_priv = dev->dev_private;
1906 int i;
1907
1908 for (i = 0; i < 16; i++) {
1909 struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
1910 if (reg->obj)
1911 i915_gem_clear_fence_reg(reg->obj);
1912 }
1913}
1914
Chris Wilson069efc12010-09-30 16:53:18 +01001915void i915_gem_reset(struct drm_device *dev)
Eric Anholt673a3942008-07-30 12:06:12 -07001916{
Chris Wilsondfaae392010-09-22 10:31:52 +01001917 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00001918 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001919
Chris Wilsondfaae392010-09-22 10:31:52 +01001920 i915_gem_reset_ring_lists(dev_priv, &dev_priv->render_ring);
Chris Wilson87acb0a2010-10-19 10:13:00 +01001921 i915_gem_reset_ring_lists(dev_priv, &dev_priv->bsd_ring);
Chris Wilson549f7362010-10-19 11:19:32 +01001922 i915_gem_reset_ring_lists(dev_priv, &dev_priv->blt_ring);
Chris Wilsondfaae392010-09-22 10:31:52 +01001923
1924 /* Remove anything from the flushing lists. The GPU cache is likely
1925 * to be lost on reset along with the data, so simply move the
1926 * lost bo to the inactive list.
1927 */
1928 while (!list_empty(&dev_priv->mm.flushing_list)) {
Chris Wilson05394f32010-11-08 19:18:58 +00001929 obj= list_first_entry(&dev_priv->mm.flushing_list,
1930 struct drm_i915_gem_object,
1931 mm_list);
Chris Wilson9375e442010-09-19 12:21:28 +01001932
Chris Wilson05394f32010-11-08 19:18:58 +00001933 obj->base.write_domain = 0;
1934 list_del_init(&obj->gpu_write_list);
1935 i915_gem_object_move_to_inactive(obj);
Chris Wilson9375e442010-09-19 12:21:28 +01001936 }
Chris Wilson9375e442010-09-19 12:21:28 +01001937
Chris Wilsondfaae392010-09-22 10:31:52 +01001938 /* Move everything out of the GPU domains to ensure we do any
1939 * necessary invalidation upon reuse.
1940 */
Chris Wilson05394f32010-11-08 19:18:58 +00001941 list_for_each_entry(obj,
Chris Wilson77f01232010-09-19 12:31:36 +01001942 &dev_priv->mm.inactive_list,
Chris Wilson69dc4982010-10-19 10:36:51 +01001943 mm_list)
Chris Wilson77f01232010-09-19 12:31:36 +01001944 {
Chris Wilson05394f32010-11-08 19:18:58 +00001945 obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
Chris Wilson77f01232010-09-19 12:31:36 +01001946 }
Chris Wilson069efc12010-09-30 16:53:18 +01001947
1948 /* The fence registers are invalidated so clear them out */
Chris Wilson312817a2010-11-22 11:50:11 +00001949 i915_gem_reset_fences(dev);
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
Daniel Vetterc6642782010-11-12 13:46:18 +00002323static int sandybridge_write_fence_reg(struct drm_i915_gem_object *obj,
2324 struct intel_ring_buffer *pipelined)
Eric Anholt4e901fd2009-10-26 16:44:17 -07002325{
Chris Wilson05394f32010-11-08 19:18:58 +00002326 struct drm_device *dev = obj->base.dev;
Eric Anholt4e901fd2009-10-26 16:44:17 -07002327 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00002328 u32 size = obj->gtt_space->size;
2329 int regnum = obj->fence_reg;
Eric Anholt4e901fd2009-10-26 16:44:17 -07002330 uint64_t val;
2331
Chris Wilson05394f32010-11-08 19:18:58 +00002332 val = (uint64_t)((obj->gtt_offset + size - 4096) &
Daniel Vetterc6642782010-11-12 13:46:18 +00002333 0xfffff000) << 32;
Chris Wilson05394f32010-11-08 19:18:58 +00002334 val |= obj->gtt_offset & 0xfffff000;
2335 val |= (uint64_t)((obj->stride / 128) - 1) <<
Eric Anholt4e901fd2009-10-26 16:44:17 -07002336 SANDYBRIDGE_FENCE_PITCH_SHIFT;
2337
Chris Wilson05394f32010-11-08 19:18:58 +00002338 if (obj->tiling_mode == I915_TILING_Y)
Eric Anholt4e901fd2009-10-26 16:44:17 -07002339 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2340 val |= I965_FENCE_REG_VALID;
2341
Daniel Vetterc6642782010-11-12 13:46:18 +00002342 if (pipelined) {
2343 int ret = intel_ring_begin(pipelined, 6);
2344 if (ret)
2345 return ret;
2346
2347 intel_ring_emit(pipelined, MI_NOOP);
2348 intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(2));
2349 intel_ring_emit(pipelined, FENCE_REG_SANDYBRIDGE_0 + regnum*8);
2350 intel_ring_emit(pipelined, (u32)val);
2351 intel_ring_emit(pipelined, FENCE_REG_SANDYBRIDGE_0 + regnum*8 + 4);
2352 intel_ring_emit(pipelined, (u32)(val >> 32));
2353 intel_ring_advance(pipelined);
2354 } else
2355 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + regnum * 8, val);
2356
2357 return 0;
Eric Anholt4e901fd2009-10-26 16:44:17 -07002358}
2359
Daniel Vetterc6642782010-11-12 13:46:18 +00002360static int i965_write_fence_reg(struct drm_i915_gem_object *obj,
2361 struct intel_ring_buffer *pipelined)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002362{
Chris Wilson05394f32010-11-08 19:18:58 +00002363 struct drm_device *dev = obj->base.dev;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002364 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00002365 u32 size = obj->gtt_space->size;
2366 int regnum = obj->fence_reg;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002367 uint64_t val;
2368
Chris Wilson05394f32010-11-08 19:18:58 +00002369 val = (uint64_t)((obj->gtt_offset + size - 4096) &
Jesse Barnesde151cf2008-11-12 10:03:55 -08002370 0xfffff000) << 32;
Chris Wilson05394f32010-11-08 19:18:58 +00002371 val |= obj->gtt_offset & 0xfffff000;
2372 val |= ((obj->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
2373 if (obj->tiling_mode == I915_TILING_Y)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002374 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2375 val |= I965_FENCE_REG_VALID;
2376
Daniel Vetterc6642782010-11-12 13:46:18 +00002377 if (pipelined) {
2378 int ret = intel_ring_begin(pipelined, 6);
2379 if (ret)
2380 return ret;
2381
2382 intel_ring_emit(pipelined, MI_NOOP);
2383 intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(2));
2384 intel_ring_emit(pipelined, FENCE_REG_965_0 + regnum*8);
2385 intel_ring_emit(pipelined, (u32)val);
2386 intel_ring_emit(pipelined, FENCE_REG_965_0 + regnum*8 + 4);
2387 intel_ring_emit(pipelined, (u32)(val >> 32));
2388 intel_ring_advance(pipelined);
2389 } else
2390 I915_WRITE64(FENCE_REG_965_0 + regnum * 8, val);
2391
2392 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002393}
2394
Daniel Vetterc6642782010-11-12 13:46:18 +00002395static int i915_write_fence_reg(struct drm_i915_gem_object *obj,
2396 struct intel_ring_buffer *pipelined)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002397{
Chris Wilson05394f32010-11-08 19:18:58 +00002398 struct drm_device *dev = obj->base.dev;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002399 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00002400 u32 size = obj->gtt_space->size;
Daniel Vetterc6642782010-11-12 13:46:18 +00002401 u32 fence_reg, val, pitch_val;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002402 int tile_width;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002403
Daniel Vetterc6642782010-11-12 13:46:18 +00002404 if (WARN((obj->gtt_offset & ~I915_FENCE_START_MASK) ||
2405 (size & -size) != size ||
2406 (obj->gtt_offset & (size - 1)),
2407 "object 0x%08x [fenceable? %d] not 1M or pot-size (0x%08x) aligned\n",
2408 obj->gtt_offset, obj->map_and_fenceable, size))
2409 return -EINVAL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002410
Daniel Vetterc6642782010-11-12 13:46:18 +00002411 if (obj->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev))
Jesse Barnes0f973f22009-01-26 17:10:45 -08002412 tile_width = 128;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002413 else
Jesse Barnes0f973f22009-01-26 17:10:45 -08002414 tile_width = 512;
2415
2416 /* Note: pitch better be a power of two tile widths */
Chris Wilson05394f32010-11-08 19:18:58 +00002417 pitch_val = obj->stride / tile_width;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002418 pitch_val = ffs(pitch_val) - 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002419
Chris Wilson05394f32010-11-08 19:18:58 +00002420 val = obj->gtt_offset;
2421 if (obj->tiling_mode == I915_TILING_Y)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002422 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002423 val |= I915_FENCE_SIZE_BITS(size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002424 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2425 val |= I830_FENCE_REG_VALID;
2426
Chris Wilson05394f32010-11-08 19:18:58 +00002427 fence_reg = obj->fence_reg;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002428 if (fence_reg < 8)
2429 fence_reg = FENCE_REG_830_0 + fence_reg * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002430 else
Chris Wilsona00b10c2010-09-24 21:15:47 +01002431 fence_reg = FENCE_REG_945_8 + (fence_reg - 8) * 4;
Daniel Vetterc6642782010-11-12 13:46:18 +00002432
2433 if (pipelined) {
2434 int ret = intel_ring_begin(pipelined, 4);
2435 if (ret)
2436 return ret;
2437
2438 intel_ring_emit(pipelined, MI_NOOP);
2439 intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(1));
2440 intel_ring_emit(pipelined, fence_reg);
2441 intel_ring_emit(pipelined, val);
2442 intel_ring_advance(pipelined);
2443 } else
2444 I915_WRITE(fence_reg, val);
2445
2446 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002447}
2448
Daniel Vetterc6642782010-11-12 13:46:18 +00002449static int i830_write_fence_reg(struct drm_i915_gem_object *obj,
2450 struct intel_ring_buffer *pipelined)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002451{
Chris Wilson05394f32010-11-08 19:18:58 +00002452 struct drm_device *dev = obj->base.dev;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002453 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00002454 u32 size = obj->gtt_space->size;
2455 int regnum = obj->fence_reg;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002456 uint32_t val;
2457 uint32_t pitch_val;
2458
Daniel Vetterc6642782010-11-12 13:46:18 +00002459 if (WARN((obj->gtt_offset & ~I830_FENCE_START_MASK) ||
2460 (size & -size) != size ||
2461 (obj->gtt_offset & (size - 1)),
2462 "object 0x%08x not 512K or pot-size 0x%08x aligned\n",
2463 obj->gtt_offset, size))
2464 return -EINVAL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002465
Chris Wilson05394f32010-11-08 19:18:58 +00002466 pitch_val = obj->stride / 128;
Eric Anholte76a16d2009-05-26 17:44:56 -07002467 pitch_val = ffs(pitch_val) - 1;
Eric Anholte76a16d2009-05-26 17:44:56 -07002468
Chris Wilson05394f32010-11-08 19:18:58 +00002469 val = obj->gtt_offset;
2470 if (obj->tiling_mode == I915_TILING_Y)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002471 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Daniel Vetterc6642782010-11-12 13:46:18 +00002472 val |= I830_FENCE_SIZE_BITS(size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002473 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2474 val |= I830_FENCE_REG_VALID;
2475
Daniel Vetterc6642782010-11-12 13:46:18 +00002476 if (pipelined) {
2477 int ret = intel_ring_begin(pipelined, 4);
2478 if (ret)
2479 return ret;
2480
2481 intel_ring_emit(pipelined, MI_NOOP);
2482 intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(1));
2483 intel_ring_emit(pipelined, FENCE_REG_830_0 + regnum*4);
2484 intel_ring_emit(pipelined, val);
2485 intel_ring_advance(pipelined);
2486 } else
2487 I915_WRITE(FENCE_REG_830_0 + regnum * 4, val);
2488
2489 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002490}
2491
Chris Wilson2cf34d72010-09-14 13:03:28 +01002492static int i915_find_fence_reg(struct drm_device *dev,
2493 bool interruptible)
Daniel Vetterae3db242010-02-19 11:51:58 +01002494{
Daniel Vetterae3db242010-02-19 11:51:58 +01002495 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002496 struct drm_i915_fence_reg *reg;
Chris Wilson05394f32010-11-08 19:18:58 +00002497 struct drm_i915_gem_object *obj = NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002498 int i, avail, ret;
2499
2500 /* First try to find a free reg */
2501 avail = 0;
2502 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2503 reg = &dev_priv->fence_regs[i];
2504 if (!reg->obj)
2505 return i;
2506
Chris Wilson05394f32010-11-08 19:18:58 +00002507 if (!reg->obj->pin_count)
2508 avail++;
Daniel Vetterae3db242010-02-19 11:51:58 +01002509 }
2510
2511 if (avail == 0)
2512 return -ENOSPC;
2513
2514 /* None available, try to steal one or wait for a user to finish */
Chris Wilsona00b10c2010-09-24 21:15:47 +01002515 avail = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002516 list_for_each_entry(reg, &dev_priv->mm.fence_list,
2517 lru_list) {
Chris Wilson05394f32010-11-08 19:18:58 +00002518 obj = reg->obj;
2519 if (obj->pin_count)
Daniel Vetterae3db242010-02-19 11:51:58 +01002520 continue;
2521
2522 /* found one! */
Chris Wilson05394f32010-11-08 19:18:58 +00002523 avail = obj->fence_reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01002524 break;
2525 }
2526
Chris Wilsona00b10c2010-09-24 21:15:47 +01002527 BUG_ON(avail == I915_FENCE_REG_NONE);
Daniel Vetterae3db242010-02-19 11:51:58 +01002528
2529 /* We only have a reference on obj from the active list. put_fence_reg
2530 * might drop that one, causing a use-after-free in it. So hold a
2531 * private reference to obj like the other callers of put_fence_reg
2532 * (set_tiling ioctl) do. */
Chris Wilson05394f32010-11-08 19:18:58 +00002533 drm_gem_object_reference(&obj->base);
2534 ret = i915_gem_object_put_fence_reg(obj, interruptible);
2535 drm_gem_object_unreference(&obj->base);
Daniel Vetterae3db242010-02-19 11:51:58 +01002536 if (ret != 0)
2537 return ret;
2538
Chris Wilsona00b10c2010-09-24 21:15:47 +01002539 return avail;
Daniel Vetterae3db242010-02-19 11:51:58 +01002540}
2541
Jesse Barnesde151cf2008-11-12 10:03:55 -08002542/**
2543 * i915_gem_object_get_fence_reg - set up a fence reg for an object
2544 * @obj: object to map through a fence reg
2545 *
2546 * When mapping objects through the GTT, userspace wants to be able to write
2547 * to them without having to worry about swizzling if the object is tiled.
2548 *
2549 * This function walks the fence regs looking for a free one for @obj,
2550 * stealing one if it can't find any.
2551 *
2552 * It then sets up the reg based on the object's properties: address, pitch
2553 * and tiling format.
2554 */
Chris Wilson8c4b8c32009-06-17 22:08:52 +01002555int
Chris Wilson05394f32010-11-08 19:18:58 +00002556i915_gem_object_get_fence_reg(struct drm_i915_gem_object *obj,
Chris Wilson2cf34d72010-09-14 13:03:28 +01002557 bool interruptible)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002558{
Chris Wilson05394f32010-11-08 19:18:58 +00002559 struct drm_device *dev = obj->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002560 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002561 struct drm_i915_fence_reg *reg = NULL;
Daniel Vetterc6642782010-11-12 13:46:18 +00002562 struct intel_ring_buffer *pipelined = NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002563 int ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002564
Eric Anholta09ba7f2009-08-29 12:49:51 -07002565 /* Just update our place in the LRU if our fence is getting used. */
Chris Wilson05394f32010-11-08 19:18:58 +00002566 if (obj->fence_reg != I915_FENCE_REG_NONE) {
2567 reg = &dev_priv->fence_regs[obj->fence_reg];
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002568 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002569 return 0;
2570 }
2571
Chris Wilson05394f32010-11-08 19:18:58 +00002572 switch (obj->tiling_mode) {
Jesse Barnesde151cf2008-11-12 10:03:55 -08002573 case I915_TILING_NONE:
2574 WARN(1, "allocating a fence for non-tiled object?\n");
2575 break;
2576 case I915_TILING_X:
Chris Wilson05394f32010-11-08 19:18:58 +00002577 if (!obj->stride)
Jesse Barnes0f973f22009-01-26 17:10:45 -08002578 return -EINVAL;
Chris Wilson05394f32010-11-08 19:18:58 +00002579 WARN((obj->stride & (512 - 1)),
Jesse Barnes0f973f22009-01-26 17:10:45 -08002580 "object 0x%08x is X tiled but has non-512B pitch\n",
Chris Wilson05394f32010-11-08 19:18:58 +00002581 obj->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002582 break;
2583 case I915_TILING_Y:
Chris Wilson05394f32010-11-08 19:18:58 +00002584 if (!obj->stride)
Jesse Barnes0f973f22009-01-26 17:10:45 -08002585 return -EINVAL;
Chris Wilson05394f32010-11-08 19:18:58 +00002586 WARN((obj->stride & (128 - 1)),
Jesse Barnes0f973f22009-01-26 17:10:45 -08002587 "object 0x%08x is Y tiled but has non-128B pitch\n",
Chris Wilson05394f32010-11-08 19:18:58 +00002588 obj->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002589 break;
2590 }
2591
Chris Wilson2cf34d72010-09-14 13:03:28 +01002592 ret = i915_find_fence_reg(dev, interruptible);
Daniel Vetterae3db242010-02-19 11:51:58 +01002593 if (ret < 0)
2594 return ret;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002595
Chris Wilson05394f32010-11-08 19:18:58 +00002596 obj->fence_reg = ret;
2597 reg = &dev_priv->fence_regs[obj->fence_reg];
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002598 list_add_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002599
Jesse Barnesde151cf2008-11-12 10:03:55 -08002600 reg->obj = obj;
2601
Chris Wilsone259bef2010-09-17 00:32:02 +01002602 switch (INTEL_INFO(dev)->gen) {
2603 case 6:
Daniel Vetterc6642782010-11-12 13:46:18 +00002604 ret = sandybridge_write_fence_reg(obj, pipelined);
Chris Wilsone259bef2010-09-17 00:32:02 +01002605 break;
2606 case 5:
2607 case 4:
Daniel Vetterc6642782010-11-12 13:46:18 +00002608 ret = i965_write_fence_reg(obj, pipelined);
Chris Wilsone259bef2010-09-17 00:32:02 +01002609 break;
2610 case 3:
Daniel Vetterc6642782010-11-12 13:46:18 +00002611 ret = i915_write_fence_reg(obj, pipelined);
Chris Wilsone259bef2010-09-17 00:32:02 +01002612 break;
2613 case 2:
Daniel Vetterc6642782010-11-12 13:46:18 +00002614 ret = i830_write_fence_reg(obj, pipelined);
Chris Wilsone259bef2010-09-17 00:32:02 +01002615 break;
2616 }
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002617
Chris Wilsona00b10c2010-09-24 21:15:47 +01002618 trace_i915_gem_object_get_fence(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00002619 obj->fence_reg,
2620 obj->tiling_mode);
Daniel Vetterc6642782010-11-12 13:46:18 +00002621 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002622}
2623
2624/**
2625 * i915_gem_clear_fence_reg - clear out fence register info
2626 * @obj: object to clear
2627 *
2628 * Zeroes out the fence register itself and clears out the associated
Chris Wilson05394f32010-11-08 19:18:58 +00002629 * data structures in dev_priv and obj.
Jesse Barnesde151cf2008-11-12 10:03:55 -08002630 */
2631static void
Chris Wilson05394f32010-11-08 19:18:58 +00002632i915_gem_clear_fence_reg(struct drm_i915_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002633{
Chris Wilson05394f32010-11-08 19:18:58 +00002634 struct drm_device *dev = obj->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002635 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00002636 struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[obj->fence_reg];
Chris Wilsone259bef2010-09-17 00:32:02 +01002637 uint32_t fence_reg;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002638
Chris Wilsone259bef2010-09-17 00:32:02 +01002639 switch (INTEL_INFO(dev)->gen) {
2640 case 6:
Eric Anholt4e901fd2009-10-26 16:44:17 -07002641 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 +
Chris Wilson05394f32010-11-08 19:18:58 +00002642 (obj->fence_reg * 8), 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002643 break;
2644 case 5:
2645 case 4:
Chris Wilson05394f32010-11-08 19:18:58 +00002646 I915_WRITE64(FENCE_REG_965_0 + (obj->fence_reg * 8), 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002647 break;
2648 case 3:
Chris Wilson05394f32010-11-08 19:18:58 +00002649 if (obj->fence_reg >= 8)
2650 fence_reg = FENCE_REG_945_8 + (obj->fence_reg - 8) * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002651 else
Chris Wilsone259bef2010-09-17 00:32:02 +01002652 case 2:
Chris Wilson05394f32010-11-08 19:18:58 +00002653 fence_reg = FENCE_REG_830_0 + obj->fence_reg * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002654
2655 I915_WRITE(fence_reg, 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002656 break;
Eric Anholtdc529a42009-03-10 22:34:49 -07002657 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002658
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002659 reg->obj = NULL;
Chris Wilson05394f32010-11-08 19:18:58 +00002660 obj->fence_reg = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002661 list_del_init(&reg->lru_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002662}
2663
Eric Anholt673a3942008-07-30 12:06:12 -07002664/**
Chris Wilson52dc7d32009-06-06 09:46:01 +01002665 * i915_gem_object_put_fence_reg - waits on outstanding fenced access
2666 * to the buffer to finish, and then resets the fence register.
2667 * @obj: tiled object holding a fence register.
Chris Wilson2cf34d72010-09-14 13:03:28 +01002668 * @bool: whether the wait upon the fence is interruptible
Chris Wilson52dc7d32009-06-06 09:46:01 +01002669 *
2670 * Zeroes out the fence register itself and clears out the associated
Chris Wilson05394f32010-11-08 19:18:58 +00002671 * data structures in dev_priv and obj.
Chris Wilson52dc7d32009-06-06 09:46:01 +01002672 */
2673int
Chris Wilson05394f32010-11-08 19:18:58 +00002674i915_gem_object_put_fence_reg(struct drm_i915_gem_object *obj,
Chris Wilson2cf34d72010-09-14 13:03:28 +01002675 bool interruptible)
Chris Wilson52dc7d32009-06-06 09:46:01 +01002676{
Chris Wilson05394f32010-11-08 19:18:58 +00002677 struct drm_device *dev = obj->base.dev;
Chris Wilsoncaea7472010-11-12 13:53:37 +00002678 int ret;
Chris Wilson52dc7d32009-06-06 09:46:01 +01002679
Chris Wilson05394f32010-11-08 19:18:58 +00002680 if (obj->fence_reg == I915_FENCE_REG_NONE)
Chris Wilson52dc7d32009-06-06 09:46:01 +01002681 return 0;
2682
Daniel Vetter10ae9bd2010-02-01 13:59:17 +01002683 /* If we've changed tiling, GTT-mappings of the object
2684 * need to re-fault to ensure that the correct fence register
2685 * setup is in place.
2686 */
2687 i915_gem_release_mmap(obj);
2688
Chris Wilson52dc7d32009-06-06 09:46:01 +01002689 /* On the i915, GPU access to tiled buffers is via a fence,
2690 * therefore we must wait for any outstanding access to complete
2691 * before clearing the fence.
2692 */
Chris Wilsoncaea7472010-11-12 13:53:37 +00002693 if (obj->fenced_gpu_access) {
Chris Wilson919926a2010-11-12 13:42:53 +00002694 ret = i915_gem_object_flush_gpu_write_domain(obj, NULL);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002695 if (ret)
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002696 return ret;
2697
Chris Wilsoncaea7472010-11-12 13:53:37 +00002698 obj->fenced_gpu_access = false;
2699 }
2700
2701 if (obj->last_fenced_seqno) {
2702 ret = i915_do_wait_request(dev,
2703 obj->last_fenced_seqno,
2704 interruptible,
2705 obj->last_fenced_ring);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002706 if (ret)
Chris Wilson52dc7d32009-06-06 09:46:01 +01002707 return ret;
Chris Wilson53640e12010-09-20 11:40:50 +01002708
Chris Wilsoncaea7472010-11-12 13:53:37 +00002709 obj->last_fenced_seqno = false;
Chris Wilson52dc7d32009-06-06 09:46:01 +01002710 }
2711
Daniel Vetter4a726612010-02-01 13:59:16 +01002712 i915_gem_object_flush_gtt_write_domain(obj);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002713 i915_gem_clear_fence_reg(obj);
Chris Wilson52dc7d32009-06-06 09:46:01 +01002714
2715 return 0;
2716}
2717
2718/**
Eric Anholt673a3942008-07-30 12:06:12 -07002719 * Finds free space in the GTT aperture and binds the object there.
2720 */
2721static int
Chris Wilson05394f32010-11-08 19:18:58 +00002722i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
Daniel Vetter920afa72010-09-16 17:54:23 +02002723 unsigned alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01002724 bool map_and_fenceable)
Eric Anholt673a3942008-07-30 12:06:12 -07002725{
Chris Wilson05394f32010-11-08 19:18:58 +00002726 struct drm_device *dev = obj->base.dev;
Eric Anholt673a3942008-07-30 12:06:12 -07002727 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07002728 struct drm_mm_node *free_space;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002729 gfp_t gfpmask = __GFP_NORETRY | __GFP_NOWARN;
Daniel Vetter5e783302010-11-14 22:32:36 +01002730 u32 size, fence_size, fence_alignment, unfenced_alignment;
Daniel Vetter75e9e912010-11-04 17:11:09 +01002731 bool mappable, fenceable;
Chris Wilson07f73f62009-09-14 16:50:30 +01002732 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002733
Chris Wilson05394f32010-11-08 19:18:58 +00002734 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01002735 DRM_ERROR("Attempting to bind a purgeable object\n");
2736 return -EINVAL;
2737 }
2738
Chris Wilson05394f32010-11-08 19:18:58 +00002739 fence_size = i915_gem_get_gtt_size(obj);
2740 fence_alignment = i915_gem_get_gtt_alignment(obj);
2741 unfenced_alignment = i915_gem_get_unfenced_gtt_alignment(obj);
Chris Wilsona00b10c2010-09-24 21:15:47 +01002742
Eric Anholt673a3942008-07-30 12:06:12 -07002743 if (alignment == 0)
Daniel Vetter5e783302010-11-14 22:32:36 +01002744 alignment = map_and_fenceable ? fence_alignment :
2745 unfenced_alignment;
Daniel Vetter75e9e912010-11-04 17:11:09 +01002746 if (map_and_fenceable && alignment & (fence_alignment - 1)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002747 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2748 return -EINVAL;
2749 }
2750
Chris Wilson05394f32010-11-08 19:18:58 +00002751 size = map_and_fenceable ? fence_size : obj->base.size;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002752
Chris Wilson654fc602010-05-27 13:18:21 +01002753 /* If the object is bigger than the entire aperture, reject it early
2754 * before evicting everything in a vain attempt to find space.
2755 */
Chris Wilson05394f32010-11-08 19:18:58 +00002756 if (obj->base.size >
Daniel Vetter75e9e912010-11-04 17:11:09 +01002757 (map_and_fenceable ? dev_priv->mm.gtt_mappable_end : dev_priv->mm.gtt_total)) {
Chris Wilson654fc602010-05-27 13:18:21 +01002758 DRM_ERROR("Attempting to bind an object larger than the aperture\n");
2759 return -E2BIG;
2760 }
2761
Eric Anholt673a3942008-07-30 12:06:12 -07002762 search_free:
Daniel Vetter75e9e912010-11-04 17:11:09 +01002763 if (map_and_fenceable)
Daniel Vetter920afa72010-09-16 17:54:23 +02002764 free_space =
2765 drm_mm_search_free_in_range(&dev_priv->mm.gtt_space,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002766 size, alignment, 0,
Daniel Vetter920afa72010-09-16 17:54:23 +02002767 dev_priv->mm.gtt_mappable_end,
2768 0);
2769 else
2770 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002771 size, alignment, 0);
Daniel Vetter920afa72010-09-16 17:54:23 +02002772
2773 if (free_space != NULL) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01002774 if (map_and_fenceable)
Chris Wilson05394f32010-11-08 19:18:58 +00002775 obj->gtt_space =
Daniel Vetter920afa72010-09-16 17:54:23 +02002776 drm_mm_get_block_range_generic(free_space,
Chris Wilsona00b10c2010-09-24 21:15:47 +01002777 size, alignment, 0,
Daniel Vetter920afa72010-09-16 17:54:23 +02002778 dev_priv->mm.gtt_mappable_end,
2779 0);
2780 else
Chris Wilson05394f32010-11-08 19:18:58 +00002781 obj->gtt_space =
Chris Wilsona00b10c2010-09-24 21:15:47 +01002782 drm_mm_get_block(free_space, size, alignment);
Daniel Vetter920afa72010-09-16 17:54:23 +02002783 }
Chris Wilson05394f32010-11-08 19:18:58 +00002784 if (obj->gtt_space == NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -07002785 /* If the gtt is empty and we're still having trouble
2786 * fitting our object in, we're out of memory.
2787 */
Daniel Vetter75e9e912010-11-04 17:11:09 +01002788 ret = i915_gem_evict_something(dev, size, alignment,
2789 map_and_fenceable);
Chris Wilson97311292009-09-21 00:22:34 +01002790 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002791 return ret;
Chris Wilson97311292009-09-21 00:22:34 +01002792
Eric Anholt673a3942008-07-30 12:06:12 -07002793 goto search_free;
2794 }
2795
Chris Wilsone5281cc2010-10-28 13:45:36 +01002796 ret = i915_gem_object_get_pages_gtt(obj, gfpmask);
Eric Anholt673a3942008-07-30 12:06:12 -07002797 if (ret) {
Chris Wilson05394f32010-11-08 19:18:58 +00002798 drm_mm_put_block(obj->gtt_space);
2799 obj->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002800
2801 if (ret == -ENOMEM) {
2802 /* first try to clear up some space from the GTT */
Chris Wilsona00b10c2010-09-24 21:15:47 +01002803 ret = i915_gem_evict_something(dev, size,
Daniel Vetter75e9e912010-11-04 17:11:09 +01002804 alignment,
2805 map_and_fenceable);
Chris Wilson07f73f62009-09-14 16:50:30 +01002806 if (ret) {
Chris Wilson07f73f62009-09-14 16:50:30 +01002807 /* now try to shrink everyone else */
Chris Wilson4bdadb92010-01-27 13:36:32 +00002808 if (gfpmask) {
2809 gfpmask = 0;
2810 goto search_free;
Chris Wilson07f73f62009-09-14 16:50:30 +01002811 }
2812
2813 return ret;
2814 }
2815
2816 goto search_free;
2817 }
2818
Eric Anholt673a3942008-07-30 12:06:12 -07002819 return ret;
2820 }
2821
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002822 ret = i915_gem_gtt_bind_object(obj);
2823 if (ret) {
Chris Wilsone5281cc2010-10-28 13:45:36 +01002824 i915_gem_object_put_pages_gtt(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00002825 drm_mm_put_block(obj->gtt_space);
2826 obj->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002827
Chris Wilsona00b10c2010-09-24 21:15:47 +01002828 ret = i915_gem_evict_something(dev, size,
Daniel Vetter75e9e912010-11-04 17:11:09 +01002829 alignment, map_and_fenceable);
Chris Wilson97311292009-09-21 00:22:34 +01002830 if (ret)
Chris Wilson07f73f62009-09-14 16:50:30 +01002831 return ret;
Chris Wilson07f73f62009-09-14 16:50:30 +01002832
2833 goto search_free;
Eric Anholt673a3942008-07-30 12:06:12 -07002834 }
Eric Anholt673a3942008-07-30 12:06:12 -07002835
Chris Wilson05394f32010-11-08 19:18:58 +00002836 obj->gtt_offset = obj->gtt_space->start;
Daniel Vetterfb7d5162010-10-01 22:05:20 +02002837
Chris Wilsonbf1a1092010-08-07 11:01:20 +01002838 /* keep track of bounds object by adding it to the inactive list */
Chris Wilson05394f32010-11-08 19:18:58 +00002839 list_add_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
2840 i915_gem_info_add_gtt(dev_priv, obj);
Chris Wilsonbf1a1092010-08-07 11:01:20 +01002841
Eric Anholt673a3942008-07-30 12:06:12 -07002842 /* Assert that the object is not currently in any GPU domain. As it
2843 * wasn't in the GTT, there shouldn't be any way it could have been in
2844 * a GPU cache
2845 */
Chris Wilson05394f32010-11-08 19:18:58 +00002846 BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
2847 BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
Eric Anholt673a3942008-07-30 12:06:12 -07002848
Chris Wilson05394f32010-11-08 19:18:58 +00002849 trace_i915_gem_object_bind(obj, obj->gtt_offset, map_and_fenceable);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002850
Daniel Vetter75e9e912010-11-04 17:11:09 +01002851 fenceable =
Chris Wilson05394f32010-11-08 19:18:58 +00002852 obj->gtt_space->size == fence_size &&
2853 (obj->gtt_space->start & (fence_alignment -1)) == 0;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002854
Daniel Vetter75e9e912010-11-04 17:11:09 +01002855 mappable =
Chris Wilson05394f32010-11-08 19:18:58 +00002856 obj->gtt_offset + obj->base.size <= dev_priv->mm.gtt_mappable_end;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002857
Chris Wilson05394f32010-11-08 19:18:58 +00002858 obj->map_and_fenceable = mappable && fenceable;
Daniel Vetter75e9e912010-11-04 17:11:09 +01002859
Eric Anholt673a3942008-07-30 12:06:12 -07002860 return 0;
2861}
2862
2863void
Chris Wilson05394f32010-11-08 19:18:58 +00002864i915_gem_clflush_object(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002865{
Eric Anholt673a3942008-07-30 12:06:12 -07002866 /* If we don't have a page list set up, then we're not pinned
2867 * to GPU, and we can ignore the cache flush because it'll happen
2868 * again at bind time.
2869 */
Chris Wilson05394f32010-11-08 19:18:58 +00002870 if (obj->pages == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002871 return;
2872
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002873 trace_i915_gem_object_clflush(obj);
Eric Anholtcfa16a02009-05-26 18:46:16 -07002874
Chris Wilson05394f32010-11-08 19:18:58 +00002875 drm_clflush_pages(obj->pages, obj->base.size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002876}
2877
Eric Anholte47c68e2008-11-14 13:35:19 -08002878/** Flushes any GPU write domain for the object if it's dirty. */
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002879static int
Chris Wilson05394f32010-11-08 19:18:58 +00002880i915_gem_object_flush_gpu_write_domain(struct drm_i915_gem_object *obj,
Chris Wilson919926a2010-11-12 13:42:53 +00002881 struct intel_ring_buffer *pipelined)
Eric Anholte47c68e2008-11-14 13:35:19 -08002882{
Chris Wilson05394f32010-11-08 19:18:58 +00002883 struct drm_device *dev = obj->base.dev;
Eric Anholte47c68e2008-11-14 13:35:19 -08002884
Chris Wilson05394f32010-11-08 19:18:58 +00002885 if ((obj->base.write_domain & I915_GEM_GPU_DOMAINS) == 0)
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002886 return 0;
Eric Anholte47c68e2008-11-14 13:35:19 -08002887
2888 /* Queue the GPU write cache flushing we need. */
Chris Wilson05394f32010-11-08 19:18:58 +00002889 i915_gem_flush_ring(dev, obj->ring, 0, obj->base.write_domain);
2890 BUG_ON(obj->base.write_domain);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002891
Chris Wilson919926a2010-11-12 13:42:53 +00002892 if (pipelined && pipelined == obj->ring)
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002893 return 0;
2894
Chris Wilson2cf34d72010-09-14 13:03:28 +01002895 return i915_gem_object_wait_rendering(obj, true);
Eric Anholte47c68e2008-11-14 13:35:19 -08002896}
2897
2898/** Flushes the GTT write domain for the object if it's dirty. */
2899static void
Chris Wilson05394f32010-11-08 19:18:58 +00002900i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08002901{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002902 uint32_t old_write_domain;
2903
Chris Wilson05394f32010-11-08 19:18:58 +00002904 if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
Eric Anholte47c68e2008-11-14 13:35:19 -08002905 return;
2906
2907 /* No actual flushing is required for the GTT write domain. Writes
2908 * to it immediately go to main memory as far as we know, so there's
2909 * no chipset flush. It also doesn't land in render cache.
2910 */
Chris Wilson4a684a42010-10-28 14:44:08 +01002911 i915_gem_release_mmap(obj);
2912
Chris Wilson05394f32010-11-08 19:18:58 +00002913 old_write_domain = obj->base.write_domain;
2914 obj->base.write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002915
2916 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00002917 obj->base.read_domains,
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002918 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002919}
2920
2921/** Flushes the CPU write domain for the object if it's dirty. */
2922static void
Chris Wilson05394f32010-11-08 19:18:58 +00002923i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08002924{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002925 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002926
Chris Wilson05394f32010-11-08 19:18:58 +00002927 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
Eric Anholte47c68e2008-11-14 13:35:19 -08002928 return;
2929
2930 i915_gem_clflush_object(obj);
Daniel Vetter40ce6572010-11-05 18:12:18 +01002931 intel_gtt_chipset_flush();
Chris Wilson05394f32010-11-08 19:18:58 +00002932 old_write_domain = obj->base.write_domain;
2933 obj->base.write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002934
2935 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00002936 obj->base.read_domains,
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002937 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002938}
2939
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002940/**
2941 * Moves a single object to the GTT read, and possibly write domain.
2942 *
2943 * This function returns when the move is complete, including waiting on
2944 * flushes to occur.
2945 */
Jesse Barnes79e53942008-11-07 14:24:08 -08002946int
Chris Wilson20217462010-11-23 15:26:33 +00002947i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002948{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002949 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002950 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002951
Eric Anholt02354392008-11-26 13:58:13 -08002952 /* Not valid to be called on unbound objects. */
Chris Wilson05394f32010-11-08 19:18:58 +00002953 if (obj->gtt_space == NULL)
Eric Anholt02354392008-11-26 13:58:13 -08002954 return -EINVAL;
2955
Chris Wilson919926a2010-11-12 13:42:53 +00002956 ret = i915_gem_object_flush_gpu_write_domain(obj, NULL);
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002957 if (ret != 0)
2958 return ret;
2959
Chris Wilson72133422010-09-13 23:56:38 +01002960 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002961
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002962 if (write) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01002963 ret = i915_gem_object_wait_rendering(obj, true);
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002964 if (ret)
2965 return ret;
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002966 }
Eric Anholte47c68e2008-11-14 13:35:19 -08002967
Chris Wilson05394f32010-11-08 19:18:58 +00002968 old_write_domain = obj->base.write_domain;
2969 old_read_domains = obj->base.read_domains;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002970
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002971 /* It should now be out of any other write domains, and we can update
2972 * the domain values for our changes.
2973 */
Chris Wilson05394f32010-11-08 19:18:58 +00002974 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2975 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002976 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00002977 obj->base.read_domains = I915_GEM_DOMAIN_GTT;
2978 obj->base.write_domain = I915_GEM_DOMAIN_GTT;
2979 obj->dirty = 1;
Eric Anholte47c68e2008-11-14 13:35:19 -08002980 }
2981
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002982 trace_i915_gem_object_change_domain(obj,
2983 old_read_domains,
2984 old_write_domain);
2985
Eric Anholte47c68e2008-11-14 13:35:19 -08002986 return 0;
2987}
2988
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002989/*
2990 * Prepare buffer for display plane. Use uninterruptible for possible flush
2991 * wait, as in modesetting process we're not supposed to be interrupted.
2992 */
2993int
Chris Wilson05394f32010-11-08 19:18:58 +00002994i915_gem_object_set_to_display_plane(struct drm_i915_gem_object *obj,
Chris Wilson919926a2010-11-12 13:42:53 +00002995 struct intel_ring_buffer *pipelined)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002996{
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002997 uint32_t old_read_domains;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002998 int ret;
2999
3000 /* Not valid to be called on unbound objects. */
Chris Wilson05394f32010-11-08 19:18:58 +00003001 if (obj->gtt_space == NULL)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003002 return -EINVAL;
3003
Chris Wilson919926a2010-11-12 13:42:53 +00003004 ret = i915_gem_object_flush_gpu_write_domain(obj, pipelined);
Chris Wilson2dafb1e2010-06-07 14:03:05 +01003005 if (ret)
3006 return ret;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003007
Chris Wilsonced270f2010-09-26 22:47:46 +01003008 /* Currently, we are always called from an non-interruptible context. */
3009 if (!pipelined) {
3010 ret = i915_gem_object_wait_rendering(obj, false);
3011 if (ret)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003012 return ret;
3013 }
3014
Chris Wilsonb118c1e2010-05-27 13:18:14 +01003015 i915_gem_object_flush_cpu_write_domain(obj);
3016
Chris Wilson05394f32010-11-08 19:18:58 +00003017 old_read_domains = obj->base.read_domains;
3018 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003019
3020 trace_i915_gem_object_change_domain(obj,
3021 old_read_domains,
Chris Wilson05394f32010-11-08 19:18:58 +00003022 obj->base.write_domain);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003023
3024 return 0;
3025}
3026
Chris Wilson85345512010-11-13 09:49:11 +00003027int
3028i915_gem_object_flush_gpu(struct drm_i915_gem_object *obj,
3029 bool interruptible)
3030{
3031 if (!obj->active)
3032 return 0;
3033
3034 if (obj->base.write_domain & I915_GEM_GPU_DOMAINS)
Chris Wilson05394f32010-11-08 19:18:58 +00003035 i915_gem_flush_ring(obj->base.dev, obj->ring,
Chris Wilson85345512010-11-13 09:49:11 +00003036 0, obj->base.write_domain);
3037
Chris Wilson05394f32010-11-08 19:18:58 +00003038 return i915_gem_object_wait_rendering(obj, interruptible);
Chris Wilson85345512010-11-13 09:49:11 +00003039}
3040
Eric Anholte47c68e2008-11-14 13:35:19 -08003041/**
3042 * Moves a single object to the CPU read, and possibly write domain.
3043 *
3044 * This function returns when the move is complete, including waiting on
3045 * flushes to occur.
3046 */
3047static int
Chris Wilson919926a2010-11-12 13:42:53 +00003048i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholte47c68e2008-11-14 13:35:19 -08003049{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003050 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003051 int ret;
3052
Daniel Vetterba3d8d72010-02-11 22:37:04 +01003053 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Eric Anholte47c68e2008-11-14 13:35:19 -08003054 if (ret != 0)
3055 return ret;
3056
3057 i915_gem_object_flush_gtt_write_domain(obj);
3058
3059 /* If we have a partially-valid cache of the object in the CPU,
3060 * finish invalidating it and free the per-page flags.
3061 */
3062 i915_gem_object_set_to_full_cpu_read_domain(obj);
3063
Chris Wilson72133422010-09-13 23:56:38 +01003064 if (write) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01003065 ret = i915_gem_object_wait_rendering(obj, true);
Chris Wilson72133422010-09-13 23:56:38 +01003066 if (ret)
3067 return ret;
3068 }
3069
Chris Wilson05394f32010-11-08 19:18:58 +00003070 old_write_domain = obj->base.write_domain;
3071 old_read_domains = obj->base.read_domains;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003072
Eric Anholte47c68e2008-11-14 13:35:19 -08003073 /* Flush the CPU cache if it's still invalid. */
Chris Wilson05394f32010-11-08 19:18:58 +00003074 if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
Eric Anholte47c68e2008-11-14 13:35:19 -08003075 i915_gem_clflush_object(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003076
Chris Wilson05394f32010-11-08 19:18:58 +00003077 obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003078 }
3079
3080 /* It should now be out of any other write domains, and we can update
3081 * the domain values for our changes.
3082 */
Chris Wilson05394f32010-11-08 19:18:58 +00003083 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
Eric Anholte47c68e2008-11-14 13:35:19 -08003084
3085 /* If we're writing through the CPU, then the GPU read domains will
3086 * need to be invalidated at next use.
3087 */
3088 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00003089 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3090 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003091 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003092
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003093 trace_i915_gem_object_change_domain(obj,
3094 old_read_domains,
3095 old_write_domain);
3096
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003097 return 0;
3098}
3099
Eric Anholt673a3942008-07-30 12:06:12 -07003100/*
3101 * Set the next domain for the specified object. This
3102 * may not actually perform the necessary flushing/invaliding though,
3103 * as that may want to be batched with other set_domain operations
3104 *
3105 * This is (we hope) the only really tricky part of gem. The goal
3106 * is fairly simple -- track which caches hold bits of the object
3107 * and make sure they remain coherent. A few concrete examples may
3108 * help to explain how it works. For shorthand, we use the notation
3109 * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
3110 * a pair of read and write domain masks.
3111 *
3112 * Case 1: the batch buffer
3113 *
3114 * 1. Allocated
3115 * 2. Written by CPU
3116 * 3. Mapped to GTT
3117 * 4. Read by GPU
3118 * 5. Unmapped from GTT
3119 * 6. Freed
3120 *
3121 * Let's take these a step at a time
3122 *
3123 * 1. Allocated
3124 * Pages allocated from the kernel may still have
3125 * cache contents, so we set them to (CPU, CPU) always.
3126 * 2. Written by CPU (using pwrite)
3127 * The pwrite function calls set_domain (CPU, CPU) and
3128 * this function does nothing (as nothing changes)
3129 * 3. Mapped by GTT
3130 * This function asserts that the object is not
3131 * currently in any GPU-based read or write domains
3132 * 4. Read by GPU
3133 * i915_gem_execbuffer calls set_domain (COMMAND, 0).
3134 * As write_domain is zero, this function adds in the
3135 * current read domains (CPU+COMMAND, 0).
3136 * flush_domains is set to CPU.
3137 * invalidate_domains is set to COMMAND
3138 * clflush is run to get data out of the CPU caches
3139 * then i915_dev_set_domain calls i915_gem_flush to
3140 * emit an MI_FLUSH and drm_agp_chipset_flush
3141 * 5. Unmapped from GTT
3142 * i915_gem_object_unbind calls set_domain (CPU, CPU)
3143 * flush_domains and invalidate_domains end up both zero
3144 * so no flushing/invalidating happens
3145 * 6. Freed
3146 * yay, done
3147 *
3148 * Case 2: The shared render buffer
3149 *
3150 * 1. Allocated
3151 * 2. Mapped to GTT
3152 * 3. Read/written by GPU
3153 * 4. set_domain to (CPU,CPU)
3154 * 5. Read/written by CPU
3155 * 6. Read/written by GPU
3156 *
3157 * 1. Allocated
3158 * Same as last example, (CPU, CPU)
3159 * 2. Mapped to GTT
3160 * Nothing changes (assertions find that it is not in the GPU)
3161 * 3. Read/written by GPU
3162 * execbuffer calls set_domain (RENDER, RENDER)
3163 * flush_domains gets CPU
3164 * invalidate_domains gets GPU
3165 * clflush (obj)
3166 * MI_FLUSH and drm_agp_chipset_flush
3167 * 4. set_domain (CPU, CPU)
3168 * flush_domains gets GPU
3169 * invalidate_domains gets CPU
3170 * wait_rendering (obj) to make sure all drawing is complete.
3171 * This will include an MI_FLUSH to get the data from GPU
3172 * to memory
3173 * clflush (obj) to invalidate the CPU cache
3174 * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
3175 * 5. Read/written by CPU
3176 * cache lines are loaded and dirtied
3177 * 6. Read written by GPU
3178 * Same as last GPU access
3179 *
3180 * Case 3: The constant buffer
3181 *
3182 * 1. Allocated
3183 * 2. Written by CPU
3184 * 3. Read by GPU
3185 * 4. Updated (written) by CPU again
3186 * 5. Read by GPU
3187 *
3188 * 1. Allocated
3189 * (CPU, CPU)
3190 * 2. Written by CPU
3191 * (CPU, CPU)
3192 * 3. Read by GPU
3193 * (CPU+RENDER, 0)
3194 * flush_domains = CPU
3195 * invalidate_domains = RENDER
3196 * clflush (obj)
3197 * MI_FLUSH
3198 * drm_agp_chipset_flush
3199 * 4. Updated (written) by CPU again
3200 * (CPU, CPU)
3201 * flush_domains = 0 (no previous write domain)
3202 * invalidate_domains = 0 (no new read domains)
3203 * 5. Read by GPU
3204 * (CPU+RENDER, 0)
3205 * flush_domains = CPU
3206 * invalidate_domains = RENDER
3207 * clflush (obj)
3208 * MI_FLUSH
3209 * drm_agp_chipset_flush
3210 */
Keith Packardc0d90822008-11-20 23:11:08 -08003211static void
Chris Wilson05394f32010-11-08 19:18:58 +00003212i915_gem_object_set_to_gpu_domain(struct drm_i915_gem_object *obj,
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003213 struct intel_ring_buffer *ring,
3214 struct change_domains *cd)
Eric Anholt673a3942008-07-30 12:06:12 -07003215{
Chris Wilson05394f32010-11-08 19:18:58 +00003216 uint32_t invalidate_domains = 0, flush_domains = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07003217
Eric Anholt673a3942008-07-30 12:06:12 -07003218 /*
3219 * If the object isn't moving to a new write domain,
3220 * let the object stay in multiple read domains
3221 */
Chris Wilson05394f32010-11-08 19:18:58 +00003222 if (obj->base.pending_write_domain == 0)
3223 obj->base.pending_read_domains |= obj->base.read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07003224
3225 /*
3226 * Flush the current write domain if
3227 * the new read domains don't match. Invalidate
3228 * any read domains which differ from the old
3229 * write domain
3230 */
Chris Wilson05394f32010-11-08 19:18:58 +00003231 if (obj->base.write_domain &&
Chris Wilsoncaea7472010-11-12 13:53:37 +00003232 (((obj->base.write_domain != obj->base.pending_read_domains ||
3233 obj->ring != ring)) ||
3234 (obj->fenced_gpu_access && !obj->pending_fenced_gpu_access))) {
Chris Wilson05394f32010-11-08 19:18:58 +00003235 flush_domains |= obj->base.write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08003236 invalidate_domains |=
Chris Wilson05394f32010-11-08 19:18:58 +00003237 obj->base.pending_read_domains & ~obj->base.write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07003238 }
3239 /*
3240 * Invalidate any read caches which may have
3241 * stale data. That is, any new read domains.
3242 */
Chris Wilson05394f32010-11-08 19:18:58 +00003243 invalidate_domains |= obj->base.pending_read_domains & ~obj->base.read_domains;
Chris Wilson3d2a8122010-09-29 11:39:53 +01003244 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU)
Eric Anholt673a3942008-07-30 12:06:12 -07003245 i915_gem_clflush_object(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003246
Chris Wilson4a684a42010-10-28 14:44:08 +01003247 /* blow away mappings if mapped through GTT */
3248 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_GTT)
3249 i915_gem_release_mmap(obj);
3250
Eric Anholtefbeed92009-02-19 14:54:51 -08003251 /* The actual obj->write_domain will be updated with
3252 * pending_write_domain after we emit the accumulated flush for all
3253 * of our domain changes in execbuffers (which clears objects'
3254 * write_domains). So if we have a current write domain that we
3255 * aren't changing, set pending_write_domain to that.
3256 */
Chris Wilson05394f32010-11-08 19:18:58 +00003257 if (flush_domains == 0 && obj->base.pending_write_domain == 0)
3258 obj->base.pending_write_domain = obj->base.write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07003259
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003260 cd->invalidate_domains |= invalidate_domains;
3261 cd->flush_domains |= flush_domains;
Chris Wilsonb6651452010-10-23 10:15:06 +01003262 if (flush_domains & I915_GEM_GPU_DOMAINS)
Chris Wilson05394f32010-11-08 19:18:58 +00003263 cd->flush_rings |= obj->ring->id;
Chris Wilsonb6651452010-10-23 10:15:06 +01003264 if (invalidate_domains & I915_GEM_GPU_DOMAINS)
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003265 cd->flush_rings |= ring->id;
Eric Anholt673a3942008-07-30 12:06:12 -07003266}
3267
3268/**
Eric Anholte47c68e2008-11-14 13:35:19 -08003269 * Moves the object from a partially CPU read to a full one.
Eric Anholt673a3942008-07-30 12:06:12 -07003270 *
Eric Anholte47c68e2008-11-14 13:35:19 -08003271 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
3272 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
3273 */
3274static void
Chris Wilson05394f32010-11-08 19:18:58 +00003275i915_gem_object_set_to_full_cpu_read_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08003276{
Chris Wilson05394f32010-11-08 19:18:58 +00003277 if (!obj->page_cpu_valid)
Eric Anholte47c68e2008-11-14 13:35:19 -08003278 return;
3279
3280 /* If we're partially in the CPU read domain, finish moving it in.
3281 */
Chris Wilson05394f32010-11-08 19:18:58 +00003282 if (obj->base.read_domains & I915_GEM_DOMAIN_CPU) {
Eric Anholte47c68e2008-11-14 13:35:19 -08003283 int i;
3284
Chris Wilson05394f32010-11-08 19:18:58 +00003285 for (i = 0; i <= (obj->base.size - 1) / PAGE_SIZE; i++) {
3286 if (obj->page_cpu_valid[i])
Eric Anholte47c68e2008-11-14 13:35:19 -08003287 continue;
Chris Wilson05394f32010-11-08 19:18:58 +00003288 drm_clflush_pages(obj->pages + i, 1);
Eric Anholte47c68e2008-11-14 13:35:19 -08003289 }
Eric Anholte47c68e2008-11-14 13:35:19 -08003290 }
3291
3292 /* Free the page_cpu_valid mappings which are now stale, whether
3293 * or not we've got I915_GEM_DOMAIN_CPU.
3294 */
Chris Wilson05394f32010-11-08 19:18:58 +00003295 kfree(obj->page_cpu_valid);
3296 obj->page_cpu_valid = NULL;
Eric Anholte47c68e2008-11-14 13:35:19 -08003297}
3298
3299/**
3300 * Set the CPU read domain on a range of the object.
3301 *
3302 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
3303 * not entirely valid. The page_cpu_valid member of the object flags which
3304 * pages have been flushed, and will be respected by
3305 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
3306 * of the whole object.
3307 *
3308 * This function returns when the move is complete, including waiting on
3309 * flushes to occur.
Eric Anholt673a3942008-07-30 12:06:12 -07003310 */
3311static int
Chris Wilson05394f32010-11-08 19:18:58 +00003312i915_gem_object_set_cpu_read_domain_range(struct drm_i915_gem_object *obj,
Eric Anholte47c68e2008-11-14 13:35:19 -08003313 uint64_t offset, uint64_t size)
Eric Anholt673a3942008-07-30 12:06:12 -07003314{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003315 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003316 int i, ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003317
Chris Wilson05394f32010-11-08 19:18:58 +00003318 if (offset == 0 && size == obj->base.size)
Eric Anholte47c68e2008-11-14 13:35:19 -08003319 return i915_gem_object_set_to_cpu_domain(obj, 0);
3320
Daniel Vetterba3d8d72010-02-11 22:37:04 +01003321 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Eric Anholte47c68e2008-11-14 13:35:19 -08003322 if (ret != 0)
3323 return ret;
3324 i915_gem_object_flush_gtt_write_domain(obj);
3325
3326 /* If we're already fully in the CPU read domain, we're done. */
Chris Wilson05394f32010-11-08 19:18:58 +00003327 if (obj->page_cpu_valid == NULL &&
3328 (obj->base.read_domains & I915_GEM_DOMAIN_CPU) != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07003329 return 0;
3330
Eric Anholte47c68e2008-11-14 13:35:19 -08003331 /* Otherwise, create/clear the per-page CPU read domain flag if we're
3332 * newly adding I915_GEM_DOMAIN_CPU
3333 */
Chris Wilson05394f32010-11-08 19:18:58 +00003334 if (obj->page_cpu_valid == NULL) {
3335 obj->page_cpu_valid = kzalloc(obj->base.size / PAGE_SIZE,
3336 GFP_KERNEL);
3337 if (obj->page_cpu_valid == NULL)
Eric Anholte47c68e2008-11-14 13:35:19 -08003338 return -ENOMEM;
Chris Wilson05394f32010-11-08 19:18:58 +00003339 } else if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0)
3340 memset(obj->page_cpu_valid, 0, obj->base.size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07003341
3342 /* Flush the cache on any pages that are still invalid from the CPU's
3343 * perspective.
3344 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003345 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
3346 i++) {
Chris Wilson05394f32010-11-08 19:18:58 +00003347 if (obj->page_cpu_valid[i])
Eric Anholt673a3942008-07-30 12:06:12 -07003348 continue;
3349
Chris Wilson05394f32010-11-08 19:18:58 +00003350 drm_clflush_pages(obj->pages + i, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07003351
Chris Wilson05394f32010-11-08 19:18:58 +00003352 obj->page_cpu_valid[i] = 1;
Eric Anholt673a3942008-07-30 12:06:12 -07003353 }
3354
Eric Anholte47c68e2008-11-14 13:35:19 -08003355 /* It should now be out of any other write domains, and we can update
3356 * the domain values for our changes.
3357 */
Chris Wilson05394f32010-11-08 19:18:58 +00003358 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
Eric Anholte47c68e2008-11-14 13:35:19 -08003359
Chris Wilson05394f32010-11-08 19:18:58 +00003360 old_read_domains = obj->base.read_domains;
3361 obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003362
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003363 trace_i915_gem_object_change_domain(obj,
3364 old_read_domains,
Chris Wilson05394f32010-11-08 19:18:58 +00003365 obj->base.write_domain);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003366
Eric Anholt673a3942008-07-30 12:06:12 -07003367 return 0;
3368}
3369
Eric Anholt673a3942008-07-30 12:06:12 -07003370static int
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003371i915_gem_execbuffer_relocate_entry(struct drm_i915_gem_object *obj,
3372 struct drm_file *file_priv,
3373 struct drm_i915_gem_exec_object2 *entry,
3374 struct drm_i915_gem_relocation_entry *reloc)
Eric Anholt673a3942008-07-30 12:06:12 -07003375{
Chris Wilson9af90d12010-10-17 10:01:56 +01003376 struct drm_device *dev = obj->base.dev;
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003377 struct drm_gem_object *target_obj;
3378 uint32_t target_offset;
3379 int ret = -EINVAL;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003380
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003381 target_obj = drm_gem_object_lookup(dev, file_priv,
3382 reloc->target_handle);
3383 if (target_obj == NULL)
3384 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07003385
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003386 target_offset = to_intel_bo(target_obj)->gtt_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003387
Chris Wilson8542a0b2009-09-09 21:15:15 +01003388#if WATCH_RELOC
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003389 DRM_INFO("%s: obj %p offset %08x target %d "
3390 "read %08x write %08x gtt %08x "
3391 "presumed %08x delta %08x\n",
3392 __func__,
3393 obj,
3394 (int) reloc->offset,
3395 (int) reloc->target_handle,
3396 (int) reloc->read_domains,
3397 (int) reloc->write_domain,
3398 (int) target_offset,
3399 (int) reloc->presumed_offset,
3400 reloc->delta);
Chris Wilson8542a0b2009-09-09 21:15:15 +01003401#endif
3402
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003403 /* The target buffer should have appeared before us in the
3404 * exec_object list, so it should have a GTT space bound by now.
3405 */
3406 if (target_offset == 0) {
3407 DRM_ERROR("No GTT space found for object %d\n",
3408 reloc->target_handle);
3409 goto err;
Eric Anholt673a3942008-07-30 12:06:12 -07003410 }
3411
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003412 /* Validate that the target is in a valid r/w GPU domain */
3413 if (reloc->write_domain & (reloc->write_domain - 1)) {
3414 DRM_ERROR("reloc with multiple write domains: "
3415 "obj %p target %d offset %d "
3416 "read %08x write %08x",
3417 obj, reloc->target_handle,
3418 (int) reloc->offset,
3419 reloc->read_domains,
3420 reloc->write_domain);
3421 goto err;
3422 }
3423 if (reloc->write_domain & I915_GEM_DOMAIN_CPU ||
3424 reloc->read_domains & I915_GEM_DOMAIN_CPU) {
3425 DRM_ERROR("reloc with read/write CPU domains: "
3426 "obj %p target %d offset %d "
3427 "read %08x write %08x",
3428 obj, reloc->target_handle,
3429 (int) reloc->offset,
3430 reloc->read_domains,
3431 reloc->write_domain);
3432 goto err;
3433 }
3434 if (reloc->write_domain && target_obj->pending_write_domain &&
3435 reloc->write_domain != target_obj->pending_write_domain) {
3436 DRM_ERROR("Write domain conflict: "
3437 "obj %p target %d offset %d "
3438 "new %08x old %08x\n",
3439 obj, reloc->target_handle,
3440 (int) reloc->offset,
3441 reloc->write_domain,
3442 target_obj->pending_write_domain);
3443 goto err;
3444 }
3445
3446 target_obj->pending_read_domains |= reloc->read_domains;
3447 target_obj->pending_write_domain |= reloc->write_domain;
3448
3449 /* If the relocation already has the right value in it, no
3450 * more work needs to be done.
3451 */
3452 if (target_offset == reloc->presumed_offset)
3453 goto out;
3454
3455 /* Check that the relocation address is valid... */
3456 if (reloc->offset > obj->base.size - 4) {
3457 DRM_ERROR("Relocation beyond object bounds: "
3458 "obj %p target %d offset %d size %d.\n",
3459 obj, reloc->target_handle,
3460 (int) reloc->offset,
3461 (int) obj->base.size);
3462 goto err;
3463 }
3464 if (reloc->offset & 3) {
3465 DRM_ERROR("Relocation not 4-byte aligned: "
3466 "obj %p target %d offset %d.\n",
3467 obj, reloc->target_handle,
3468 (int) reloc->offset);
3469 goto err;
3470 }
3471
3472 /* and points to somewhere within the target object. */
3473 if (reloc->delta >= target_obj->size) {
3474 DRM_ERROR("Relocation beyond target object bounds: "
3475 "obj %p target %d delta %d size %d.\n",
3476 obj, reloc->target_handle,
3477 (int) reloc->delta,
3478 (int) target_obj->size);
3479 goto err;
3480 }
3481
3482 reloc->delta += target_offset;
3483 if (obj->base.write_domain == I915_GEM_DOMAIN_CPU) {
3484 uint32_t page_offset = reloc->offset & ~PAGE_MASK;
3485 char *vaddr;
3486
3487 vaddr = kmap_atomic(obj->pages[reloc->offset >> PAGE_SHIFT]);
3488 *(uint32_t *)(vaddr + page_offset) = reloc->delta;
3489 kunmap_atomic(vaddr);
3490 } else {
3491 struct drm_i915_private *dev_priv = dev->dev_private;
3492 uint32_t __iomem *reloc_entry;
3493 void __iomem *reloc_page;
3494
Chris Wilson05394f32010-11-08 19:18:58 +00003495 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003496 if (ret)
3497 goto err;
3498
3499 /* Map the page containing the relocation we're going to perform. */
3500 reloc->offset += obj->gtt_offset;
3501 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
3502 reloc->offset & PAGE_MASK);
3503 reloc_entry = (uint32_t __iomem *)
3504 (reloc_page + (reloc->offset & ~PAGE_MASK));
3505 iowrite32(reloc->delta, reloc_entry);
3506 io_mapping_unmap_atomic(reloc_page);
3507 }
3508
3509 /* and update the user's relocation entry */
3510 reloc->presumed_offset = target_offset;
3511
3512out:
3513 ret = 0;
3514err:
Chris Wilson9af90d12010-10-17 10:01:56 +01003515 drm_gem_object_unreference(target_obj);
3516 return ret;
3517}
3518
3519static int
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003520i915_gem_execbuffer_relocate_object(struct drm_i915_gem_object *obj,
3521 struct drm_file *file_priv,
3522 struct drm_i915_gem_exec_object2 *entry)
3523{
3524 struct drm_i915_gem_relocation_entry __user *user_relocs;
3525 int i, ret;
3526
3527 user_relocs = (void __user *)(uintptr_t)entry->relocs_ptr;
3528 for (i = 0; i < entry->relocation_count; i++) {
3529 struct drm_i915_gem_relocation_entry reloc;
3530
3531 if (__copy_from_user_inatomic(&reloc,
3532 user_relocs+i,
3533 sizeof(reloc)))
3534 return -EFAULT;
3535
3536 ret = i915_gem_execbuffer_relocate_entry(obj, file_priv, entry, &reloc);
3537 if (ret)
3538 return ret;
3539
3540 if (__copy_to_user_inatomic(&user_relocs[i].presumed_offset,
3541 &reloc.presumed_offset,
3542 sizeof(reloc.presumed_offset)))
3543 return -EFAULT;
3544 }
3545
3546 return 0;
3547}
3548
3549static int
3550i915_gem_execbuffer_relocate_object_slow(struct drm_i915_gem_object *obj,
3551 struct drm_file *file_priv,
3552 struct drm_i915_gem_exec_object2 *entry,
3553 struct drm_i915_gem_relocation_entry *relocs)
3554{
3555 int i, ret;
3556
3557 for (i = 0; i < entry->relocation_count; i++) {
3558 ret = i915_gem_execbuffer_relocate_entry(obj, file_priv, entry, &relocs[i]);
3559 if (ret)
3560 return ret;
3561 }
3562
3563 return 0;
3564}
3565
3566static int
3567i915_gem_execbuffer_relocate(struct drm_device *dev,
3568 struct drm_file *file,
Chris Wilson05394f32010-11-08 19:18:58 +00003569 struct drm_i915_gem_object **object_list,
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003570 struct drm_i915_gem_exec_object2 *exec_list,
3571 int count)
3572{
3573 int i, ret;
3574
3575 for (i = 0; i < count; i++) {
Chris Wilson05394f32010-11-08 19:18:58 +00003576 struct drm_i915_gem_object *obj = object_list[i];
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003577 obj->base.pending_read_domains = 0;
3578 obj->base.pending_write_domain = 0;
3579 ret = i915_gem_execbuffer_relocate_object(obj, file,
3580 &exec_list[i]);
3581 if (ret)
3582 return ret;
3583 }
3584
3585 return 0;
3586}
3587
3588static int
3589i915_gem_execbuffer_reserve(struct drm_device *dev,
3590 struct drm_file *file,
Chris Wilson05394f32010-11-08 19:18:58 +00003591 struct drm_i915_gem_object **object_list,
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003592 struct drm_i915_gem_exec_object2 *exec_list,
3593 int count)
Chris Wilson9af90d12010-10-17 10:01:56 +01003594{
Chris Wilson9af90d12010-10-17 10:01:56 +01003595 int ret, i, retry;
3596
Chris Wilsona7a09ae2010-11-12 13:49:09 +00003597 /* Attempt to pin all of the buffers into the GTT.
3598 * This is done in 3 phases:
3599 *
3600 * 1a. Unbind all objects that do not match the GTT constraints for
3601 * the execbuffer (fenceable, mappable, alignment etc).
3602 * 1b. Increment pin count for already bound objects.
3603 * 2. Bind new objects.
3604 * 3. Decrement pin count.
3605 *
3606 * This avoid unnecessary unbinding of later objects in order to makr
3607 * room for the earlier objects *unless* we need to defragment.
3608 */
Chris Wilson5eac3ab2010-10-31 08:49:47 +00003609 retry = 0;
3610 do {
Chris Wilson9af90d12010-10-17 10:01:56 +01003611 ret = 0;
Chris Wilsona7a09ae2010-11-12 13:49:09 +00003612
3613 /* Unbind any ill-fitting objects or pin. */
3614 for (i = 0; i < count; i++) {
3615 struct drm_i915_gem_object *obj = object_list[i];
3616 struct drm_i915_gem_exec_object2 *entry = &exec_list[i];
3617 bool need_fence, need_mappable;
3618
3619 if (!obj->gtt_space)
3620 continue;
3621
3622 need_fence =
3623 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
3624 obj->tiling_mode != I915_TILING_NONE;
3625 need_mappable =
3626 entry->relocation_count ? true : need_fence;
3627
3628 if ((entry->alignment && obj->gtt_offset & (entry->alignment - 1)) ||
3629 (need_mappable && !obj->map_and_fenceable))
3630 ret = i915_gem_object_unbind(obj);
3631 else
3632 ret = i915_gem_object_pin(obj,
3633 entry->alignment,
3634 need_mappable);
3635 if (ret) {
3636 count = i;
3637 goto err;
3638 }
3639 }
3640
3641 /* Bind fresh objects */
Chris Wilson9af90d12010-10-17 10:01:56 +01003642 for (i = 0; i < count; i++) {
3643 struct drm_i915_gem_exec_object2 *entry = &exec_list[i];
Chris Wilson05394f32010-11-08 19:18:58 +00003644 struct drm_i915_gem_object *obj = object_list[i];
Chris Wilsona7a09ae2010-11-12 13:49:09 +00003645 bool need_fence;
3646
3647 need_fence =
Chris Wilson9af90d12010-10-17 10:01:56 +01003648 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
3649 obj->tiling_mode != I915_TILING_NONE;
3650
Chris Wilsona7a09ae2010-11-12 13:49:09 +00003651 if (!obj->gtt_space) {
3652 bool need_mappable =
3653 entry->relocation_count ? true : need_fence;
Daniel Vetter16e809a2010-09-16 19:37:04 +02003654
Chris Wilsona7a09ae2010-11-12 13:49:09 +00003655 ret = i915_gem_object_pin(obj,
3656 entry->alignment,
3657 need_mappable);
Chris Wilson9af90d12010-10-17 10:01:56 +01003658 if (ret)
3659 break;
3660 }
3661
Chris Wilson9af90d12010-10-17 10:01:56 +01003662 if (need_fence) {
Chris Wilson05394f32010-11-08 19:18:58 +00003663 ret = i915_gem_object_get_fence_reg(obj, true);
Chris Wilsona7a09ae2010-11-12 13:49:09 +00003664 if (ret)
Chris Wilson9af90d12010-10-17 10:01:56 +01003665 break;
Chris Wilson9af90d12010-10-17 10:01:56 +01003666
Chris Wilsoncaea7472010-11-12 13:53:37 +00003667 obj->pending_fenced_gpu_access = true;
Chris Wilson9af90d12010-10-17 10:01:56 +01003668 }
3669
3670 entry->offset = obj->gtt_offset;
3671 }
3672
Chris Wilsona7a09ae2010-11-12 13:49:09 +00003673err: /* Decrement pin count for bound objects */
3674 for (i = 0; i < count; i++) {
3675 struct drm_i915_gem_object *obj = object_list[i];
3676 if (obj->gtt_space)
3677 i915_gem_object_unpin(obj);
3678 }
Chris Wilson9af90d12010-10-17 10:01:56 +01003679
Chris Wilson5eac3ab2010-10-31 08:49:47 +00003680 if (ret != -ENOSPC || retry > 1)
Chris Wilson9af90d12010-10-17 10:01:56 +01003681 return ret;
3682
Chris Wilson5eac3ab2010-10-31 08:49:47 +00003683 /* First attempt, just clear anything that is purgeable.
3684 * Second attempt, clear the entire GTT.
3685 */
3686 ret = i915_gem_evict_everything(dev, retry == 0);
Chris Wilson9af90d12010-10-17 10:01:56 +01003687 if (ret)
3688 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003689
Chris Wilson5eac3ab2010-10-31 08:49:47 +00003690 retry++;
3691 } while (1);
Eric Anholt673a3942008-07-30 12:06:12 -07003692}
3693
Chris Wilson13b29282010-11-01 12:22:48 +00003694static int
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003695i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
3696 struct drm_file *file,
Chris Wilson05394f32010-11-08 19:18:58 +00003697 struct drm_i915_gem_object **object_list,
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003698 struct drm_i915_gem_exec_object2 *exec_list,
3699 int count)
3700{
3701 struct drm_i915_gem_relocation_entry *reloc;
3702 int i, total, ret;
3703
Chris Wilson05394f32010-11-08 19:18:58 +00003704 for (i = 0; i < count; i++)
3705 object_list[i]->in_execbuffer = false;
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003706
3707 mutex_unlock(&dev->struct_mutex);
3708
3709 total = 0;
3710 for (i = 0; i < count; i++)
3711 total += exec_list[i].relocation_count;
3712
3713 reloc = drm_malloc_ab(total, sizeof(*reloc));
3714 if (reloc == NULL) {
3715 mutex_lock(&dev->struct_mutex);
3716 return -ENOMEM;
3717 }
3718
3719 total = 0;
3720 for (i = 0; i < count; i++) {
3721 struct drm_i915_gem_relocation_entry __user *user_relocs;
3722
3723 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3724
3725 if (copy_from_user(reloc+total, user_relocs,
3726 exec_list[i].relocation_count *
3727 sizeof(*reloc))) {
3728 ret = -EFAULT;
3729 mutex_lock(&dev->struct_mutex);
3730 goto err;
3731 }
3732
3733 total += exec_list[i].relocation_count;
3734 }
3735
3736 ret = i915_mutex_lock_interruptible(dev);
3737 if (ret) {
3738 mutex_lock(&dev->struct_mutex);
3739 goto err;
3740 }
3741
3742 ret = i915_gem_execbuffer_reserve(dev, file,
3743 object_list, exec_list,
3744 count);
3745 if (ret)
3746 goto err;
3747
3748 total = 0;
3749 for (i = 0; i < count; i++) {
Chris Wilson05394f32010-11-08 19:18:58 +00003750 struct drm_i915_gem_object *obj = object_list[i];
Chris Wilsonbcf50e22010-11-21 22:07:12 +00003751 obj->base.pending_read_domains = 0;
3752 obj->base.pending_write_domain = 0;
3753 ret = i915_gem_execbuffer_relocate_object_slow(obj, file,
3754 &exec_list[i],
3755 reloc + total);
3756 if (ret)
3757 goto err;
3758
3759 total += exec_list[i].relocation_count;
3760 }
3761
3762 /* Leave the user relocations as are, this is the painfully slow path,
3763 * and we want to avoid the complication of dropping the lock whilst
3764 * having buffers reserved in the aperture and so causing spurious
3765 * ENOSPC for random operations.
3766 */
3767
3768err:
3769 drm_free_large(reloc);
3770 return ret;
3771}
3772
3773static int
Chris Wilson13b29282010-11-01 12:22:48 +00003774i915_gem_execbuffer_move_to_gpu(struct drm_device *dev,
3775 struct drm_file *file,
3776 struct intel_ring_buffer *ring,
Chris Wilson05394f32010-11-08 19:18:58 +00003777 struct drm_i915_gem_object **objects,
Chris Wilson13b29282010-11-01 12:22:48 +00003778 int count)
3779{
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003780 struct change_domains cd;
Chris Wilson13b29282010-11-01 12:22:48 +00003781 int ret, i;
3782
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003783 cd.invalidate_domains = 0;
3784 cd.flush_domains = 0;
3785 cd.flush_rings = 0;
Chris Wilson13b29282010-11-01 12:22:48 +00003786 for (i = 0; i < count; i++)
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003787 i915_gem_object_set_to_gpu_domain(objects[i], ring, &cd);
Chris Wilson13b29282010-11-01 12:22:48 +00003788
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003789 if (cd.invalidate_domains | cd.flush_domains) {
Chris Wilson13b29282010-11-01 12:22:48 +00003790#if WATCH_EXEC
3791 DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
3792 __func__,
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003793 cd.invalidate_domains,
3794 cd.flush_domains);
Chris Wilson13b29282010-11-01 12:22:48 +00003795#endif
Chris Wilson05394f32010-11-08 19:18:58 +00003796 i915_gem_flush(dev,
Chris Wilson0f8c6d72010-11-01 12:38:44 +00003797 cd.invalidate_domains,
3798 cd.flush_domains,
3799 cd.flush_rings);
Chris Wilson13b29282010-11-01 12:22:48 +00003800 }
3801
3802 for (i = 0; i < count; i++) {
Chris Wilson05394f32010-11-08 19:18:58 +00003803 struct drm_i915_gem_object *obj = objects[i];
Chris Wilson13b29282010-11-01 12:22:48 +00003804 /* XXX replace with semaphores */
3805 if (obj->ring && ring != obj->ring) {
Chris Wilson05394f32010-11-08 19:18:58 +00003806 ret = i915_gem_object_wait_rendering(obj, true);
Chris Wilson13b29282010-11-01 12:22:48 +00003807 if (ret)
3808 return ret;
3809 }
3810 }
3811
3812 return 0;
3813}
3814
Eric Anholt673a3942008-07-30 12:06:12 -07003815/* Throttle our rendering by waiting until the ring has completed our requests
3816 * emitted over 20 msec ago.
3817 *
Eric Anholtb9624422009-06-03 07:27:35 +00003818 * Note that if we were to use the current jiffies each time around the loop,
3819 * we wouldn't escape the function with any frames outstanding if the time to
3820 * render a frame was over 20ms.
3821 *
Eric Anholt673a3942008-07-30 12:06:12 -07003822 * This should get us reasonable parallelism between CPU and GPU but also
3823 * relatively low latency when blocking on a particular request to finish.
3824 */
3825static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003826i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003827{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003828 struct drm_i915_private *dev_priv = dev->dev_private;
3829 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00003830 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003831 struct drm_i915_gem_request *request;
3832 struct intel_ring_buffer *ring = NULL;
3833 u32 seqno = 0;
3834 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003835
Chris Wilson1c255952010-09-26 11:03:27 +01003836 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003837 list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
Eric Anholtb9624422009-06-03 07:27:35 +00003838 if (time_after_eq(request->emitted_jiffies, recent_enough))
3839 break;
3840
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003841 ring = request->ring;
3842 seqno = request->seqno;
Eric Anholtb9624422009-06-03 07:27:35 +00003843 }
Chris Wilson1c255952010-09-26 11:03:27 +01003844 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003845
3846 if (seqno == 0)
3847 return 0;
3848
3849 ret = 0;
Chris Wilson78501ea2010-10-27 12:18:21 +01003850 if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) {
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003851 /* And wait for the seqno passing without holding any locks and
3852 * causing extra latency for others. This is safe as the irq
3853 * generation is designed to be run atomically and so is
3854 * lockless.
3855 */
Chris Wilson78501ea2010-10-27 12:18:21 +01003856 ring->user_irq_get(ring);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003857 ret = wait_event_interruptible(ring->irq_queue,
Chris Wilson78501ea2010-10-27 12:18:21 +01003858 i915_seqno_passed(ring->get_seqno(ring), seqno)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003859 || atomic_read(&dev_priv->mm.wedged));
Chris Wilson78501ea2010-10-27 12:18:21 +01003860 ring->user_irq_put(ring);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003861
3862 if (ret == 0 && atomic_read(&dev_priv->mm.wedged))
3863 ret = -EIO;
3864 }
3865
3866 if (ret == 0)
3867 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
Eric Anholtb9624422009-06-03 07:27:35 +00003868
Eric Anholt673a3942008-07-30 12:06:12 -07003869 return ret;
3870}
3871
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003872static int
Chris Wilson2549d6c2010-10-14 12:10:41 +01003873i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec,
3874 uint64_t exec_offset)
Chris Wilson83d60792009-06-06 09:45:57 +01003875{
3876 uint32_t exec_start, exec_len;
3877
3878 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
3879 exec_len = (uint32_t) exec->batch_len;
3880
3881 if ((exec_start | exec_len) & 0x7)
3882 return -EINVAL;
3883
3884 if (!exec_start)
3885 return -EINVAL;
3886
3887 return 0;
3888}
3889
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003890static int
Chris Wilson2549d6c2010-10-14 12:10:41 +01003891validate_exec_list(struct drm_i915_gem_exec_object2 *exec,
3892 int count)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003893{
Chris Wilson2549d6c2010-10-14 12:10:41 +01003894 int i;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003895
Chris Wilson2549d6c2010-10-14 12:10:41 +01003896 for (i = 0; i < count; i++) {
3897 char __user *ptr = (char __user *)(uintptr_t)exec[i].relocs_ptr;
Chris Wilsond1d78832010-11-21 09:23:48 +00003898 int length; /* limited by fault_in_pages_readable() */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003899
Chris Wilsond1d78832010-11-21 09:23:48 +00003900 /* First check for malicious input causing overflow */
3901 if (exec[i].relocation_count >
3902 INT_MAX / sizeof(struct drm_i915_gem_relocation_entry))
3903 return -EINVAL;
3904
3905 length = exec[i].relocation_count *
3906 sizeof(struct drm_i915_gem_relocation_entry);
Chris Wilson2549d6c2010-10-14 12:10:41 +01003907 if (!access_ok(VERIFY_READ, ptr, length))
3908 return -EFAULT;
3909
Chris Wilsonb5dc6082010-10-20 20:59:57 +01003910 /* we may also need to update the presumed offsets */
3911 if (!access_ok(VERIFY_WRITE, ptr, length))
3912 return -EFAULT;
3913
Chris Wilson2549d6c2010-10-14 12:10:41 +01003914 if (fault_in_pages_readable(ptr, length))
3915 return -EFAULT;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003916 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003917
Chris Wilson2549d6c2010-10-14 12:10:41 +01003918 return 0;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003919}
3920
Chris Wilson2549d6c2010-10-14 12:10:41 +01003921static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003922i915_gem_do_execbuffer(struct drm_device *dev, void *data,
Chris Wilson9af90d12010-10-17 10:01:56 +01003923 struct drm_file *file,
Jesse Barnes76446ca2009-12-17 22:05:42 -05003924 struct drm_i915_gem_execbuffer2 *args,
3925 struct drm_i915_gem_exec_object2 *exec_list)
Eric Anholt673a3942008-07-30 12:06:12 -07003926{
3927 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00003928 struct drm_i915_gem_object **object_list = NULL;
3929 struct drm_i915_gem_object *batch_obj;
Eric Anholt201361a2009-03-11 12:30:04 -07003930 struct drm_clip_rect *cliprects = NULL;
Chris Wilson8dc5d142010-08-12 12:36:12 +01003931 struct drm_i915_gem_request *request = NULL;
Chris Wilson9af90d12010-10-17 10:01:56 +01003932 int ret, i, flips;
Eric Anholt673a3942008-07-30 12:06:12 -07003933 uint64_t exec_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003934
Zou Nan hai852835f2010-05-21 09:08:56 +08003935 struct intel_ring_buffer *ring = NULL;
3936
Chris Wilson30dbf0c2010-09-25 10:19:17 +01003937 ret = i915_gem_check_is_wedged(dev);
3938 if (ret)
3939 return ret;
3940
Chris Wilson2549d6c2010-10-14 12:10:41 +01003941 ret = validate_exec_list(exec_list, args->buffer_count);
3942 if (ret)
3943 return ret;
3944
Eric Anholt673a3942008-07-30 12:06:12 -07003945#if WATCH_EXEC
3946 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3947 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3948#endif
Chris Wilson549f7362010-10-19 11:19:32 +01003949 switch (args->flags & I915_EXEC_RING_MASK) {
3950 case I915_EXEC_DEFAULT:
3951 case I915_EXEC_RENDER:
3952 ring = &dev_priv->render_ring;
3953 break;
3954 case I915_EXEC_BSD:
Zou Nan haid1b851f2010-05-21 09:08:57 +08003955 if (!HAS_BSD(dev)) {
Chris Wilson549f7362010-10-19 11:19:32 +01003956 DRM_ERROR("execbuf with invalid ring (BSD)\n");
Zou Nan haid1b851f2010-05-21 09:08:57 +08003957 return -EINVAL;
3958 }
3959 ring = &dev_priv->bsd_ring;
Chris Wilson549f7362010-10-19 11:19:32 +01003960 break;
3961 case I915_EXEC_BLT:
3962 if (!HAS_BLT(dev)) {
3963 DRM_ERROR("execbuf with invalid ring (BLT)\n");
3964 return -EINVAL;
3965 }
3966 ring = &dev_priv->blt_ring;
3967 break;
3968 default:
3969 DRM_ERROR("execbuf with unknown ring: %d\n",
3970 (int)(args->flags & I915_EXEC_RING_MASK));
3971 return -EINVAL;
Zou Nan haid1b851f2010-05-21 09:08:57 +08003972 }
3973
Eric Anholt4f481ed2008-09-10 14:22:49 -07003974 if (args->buffer_count < 1) {
3975 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3976 return -EINVAL;
3977 }
Eric Anholtc8e0f932009-11-22 03:49:37 +01003978 object_list = drm_malloc_ab(sizeof(*object_list), args->buffer_count);
Jesse Barnes76446ca2009-12-17 22:05:42 -05003979 if (object_list == NULL) {
3980 DRM_ERROR("Failed to allocate object list for %d buffers\n",
Eric Anholt673a3942008-07-30 12:06:12 -07003981 args->buffer_count);
3982 ret = -ENOMEM;
3983 goto pre_mutex_err;
3984 }
Eric Anholt673a3942008-07-30 12:06:12 -07003985
Eric Anholt201361a2009-03-11 12:30:04 -07003986 if (args->num_cliprects != 0) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003987 cliprects = kcalloc(args->num_cliprects, sizeof(*cliprects),
3988 GFP_KERNEL);
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003989 if (cliprects == NULL) {
3990 ret = -ENOMEM;
Eric Anholt201361a2009-03-11 12:30:04 -07003991 goto pre_mutex_err;
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003992 }
Eric Anholt201361a2009-03-11 12:30:04 -07003993
3994 ret = copy_from_user(cliprects,
3995 (struct drm_clip_rect __user *)
3996 (uintptr_t) args->cliprects_ptr,
3997 sizeof(*cliprects) * args->num_cliprects);
3998 if (ret != 0) {
3999 DRM_ERROR("copy %d cliprects failed: %d\n",
4000 args->num_cliprects, ret);
Dan Carpenterc877cdc2010-06-23 19:03:01 +02004001 ret = -EFAULT;
Eric Anholt201361a2009-03-11 12:30:04 -07004002 goto pre_mutex_err;
4003 }
4004 }
4005
Chris Wilson8dc5d142010-08-12 12:36:12 +01004006 request = kzalloc(sizeof(*request), GFP_KERNEL);
4007 if (request == NULL) {
4008 ret = -ENOMEM;
Chris Wilsona198bc82009-02-06 16:55:20 +00004009 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07004010 }
4011
Chris Wilson76c1dec2010-09-25 11:22:51 +01004012 ret = i915_mutex_lock_interruptible(dev);
4013 if (ret)
4014 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07004015
Eric Anholt673a3942008-07-30 12:06:12 -07004016 if (dev_priv->mm.suspended) {
Eric Anholt673a3942008-07-30 12:06:12 -07004017 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00004018 ret = -EBUSY;
4019 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07004020 }
4021
Keith Packardac94a962008-11-20 23:30:27 -08004022 /* Look up object handles */
Eric Anholt673a3942008-07-30 12:06:12 -07004023 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson05394f32010-11-08 19:18:58 +00004024 struct drm_i915_gem_object *obj;
Chris Wilson7e318e12010-10-27 13:43:39 +01004025
Chris Wilson05394f32010-11-08 19:18:58 +00004026 obj = to_intel_bo (drm_gem_object_lookup(dev, file,
4027 exec_list[i].handle));
4028 if (obj == NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -07004029 DRM_ERROR("Invalid object handle %d at index %d\n",
4030 exec_list[i].handle, i);
Chris Wilson0ce907f2010-01-23 20:26:35 +00004031 /* prevent error path from reading uninitialized data */
Chris Wilson05394f32010-11-08 19:18:58 +00004032 args->buffer_count = i;
Chris Wilsonbf79cb92010-08-04 14:19:46 +01004033 ret = -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07004034 goto err;
4035 }
Chris Wilson05394f32010-11-08 19:18:58 +00004036 object_list[i] = obj;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05004037
Chris Wilson05394f32010-11-08 19:18:58 +00004038 if (obj->in_execbuffer) {
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05004039 DRM_ERROR("Object %p appears more than once in object list\n",
Chris Wilson05394f32010-11-08 19:18:58 +00004040 obj);
Chris Wilson0ce907f2010-01-23 20:26:35 +00004041 /* prevent error path from reading uninitialized data */
4042 args->buffer_count = i + 1;
Chris Wilsonbf79cb92010-08-04 14:19:46 +01004043 ret = -EINVAL;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05004044 goto err;
4045 }
Chris Wilson05394f32010-11-08 19:18:58 +00004046 obj->in_execbuffer = true;
Chris Wilsoncaea7472010-11-12 13:53:37 +00004047 obj->pending_fenced_gpu_access = false;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05004048 }
4049
Chris Wilson9af90d12010-10-17 10:01:56 +01004050 /* Move the objects en-masse into the GTT, evicting if necessary. */
Chris Wilsonbcf50e22010-11-21 22:07:12 +00004051 ret = i915_gem_execbuffer_reserve(dev, file,
4052 object_list, exec_list,
4053 args->buffer_count);
Chris Wilson9af90d12010-10-17 10:01:56 +01004054 if (ret)
4055 goto err;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07004056
Chris Wilson9af90d12010-10-17 10:01:56 +01004057 /* The objects are in their final locations, apply the relocations. */
Chris Wilsonbcf50e22010-11-21 22:07:12 +00004058 ret = i915_gem_execbuffer_relocate(dev, file,
4059 object_list, exec_list,
4060 args->buffer_count);
4061 if (ret) {
4062 if (ret == -EFAULT) {
4063 ret = i915_gem_execbuffer_relocate_slow(dev, file,
4064 object_list,
4065 exec_list,
4066 args->buffer_count);
4067 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
4068 }
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05004069 if (ret)
4070 goto err;
4071 }
4072
Eric Anholt673a3942008-07-30 12:06:12 -07004073 /* Set the pending read domains for the batch buffer to COMMAND */
4074 batch_obj = object_list[args->buffer_count-1];
Chris Wilson05394f32010-11-08 19:18:58 +00004075 if (batch_obj->base.pending_write_domain) {
Chris Wilson5f26a2c2009-06-06 09:45:58 +01004076 DRM_ERROR("Attempting to use self-modifying batch buffer\n");
4077 ret = -EINVAL;
4078 goto err;
4079 }
Chris Wilson05394f32010-11-08 19:18:58 +00004080 batch_obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
Eric Anholt673a3942008-07-30 12:06:12 -07004081
Chris Wilson9af90d12010-10-17 10:01:56 +01004082 /* Sanity check the batch buffer */
Chris Wilson05394f32010-11-08 19:18:58 +00004083 exec_offset = batch_obj->gtt_offset;
Chris Wilson9af90d12010-10-17 10:01:56 +01004084 ret = i915_gem_check_execbuffer(args, exec_offset);
Chris Wilson83d60792009-06-06 09:45:57 +01004085 if (ret != 0) {
4086 DRM_ERROR("execbuf with invalid offset/length\n");
4087 goto err;
4088 }
4089
Chris Wilson13b29282010-11-01 12:22:48 +00004090 ret = i915_gem_execbuffer_move_to_gpu(dev, file, ring,
4091 object_list, args->buffer_count);
4092 if (ret)
4093 goto err;
Eric Anholt673a3942008-07-30 12:06:12 -07004094
Eric Anholt673a3942008-07-30 12:06:12 -07004095#if WATCH_COHERENCY
4096 for (i = 0; i < args->buffer_count; i++) {
4097 i915_gem_object_check_coherency(object_list[i],
4098 exec_list[i].handle);
4099 }
4100#endif
4101
Eric Anholt673a3942008-07-30 12:06:12 -07004102#if WATCH_EXEC
Ben Gamari6911a9b2009-04-02 11:24:54 -07004103 i915_gem_dump_object(batch_obj,
Eric Anholt673a3942008-07-30 12:06:12 -07004104 args->batch_len,
4105 __func__,
4106 ~0);
4107#endif
4108
Chris Wilsone59f2ba2010-10-07 17:28:15 +01004109 /* Check for any pending flips. As we only maintain a flip queue depth
4110 * of 1, we can simply insert a WAIT for the next display flip prior
4111 * to executing the batch and avoid stalling the CPU.
4112 */
4113 flips = 0;
4114 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson05394f32010-11-08 19:18:58 +00004115 if (object_list[i]->base.write_domain)
4116 flips |= atomic_read(&object_list[i]->pending_flip);
Chris Wilsone59f2ba2010-10-07 17:28:15 +01004117 }
4118 if (flips) {
4119 int plane, flip_mask;
4120
4121 for (plane = 0; flips >> plane; plane++) {
4122 if (((flips >> plane) & 1) == 0)
4123 continue;
4124
4125 if (plane)
4126 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
4127 else
4128 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
4129
Chris Wilsone1f99ce2010-10-27 12:45:26 +01004130 ret = intel_ring_begin(ring, 2);
4131 if (ret)
4132 goto err;
4133
Chris Wilson78501ea2010-10-27 12:18:21 +01004134 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
4135 intel_ring_emit(ring, MI_NOOP);
4136 intel_ring_advance(ring);
Chris Wilsone59f2ba2010-10-07 17:28:15 +01004137 }
4138 }
4139
Eric Anholt673a3942008-07-30 12:06:12 -07004140 /* Exec the batchbuffer */
Chris Wilson78501ea2010-10-27 12:18:21 +01004141 ret = ring->dispatch_execbuffer(ring, args, cliprects, exec_offset);
Eric Anholt673a3942008-07-30 12:06:12 -07004142 if (ret) {
4143 DRM_ERROR("dispatch failed %d\n", ret);
4144 goto err;
4145 }
4146
Chris Wilson7e318e12010-10-27 13:43:39 +01004147 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson05394f32010-11-08 19:18:58 +00004148 struct drm_i915_gem_object *obj = object_list[i];
Chris Wilson7e318e12010-10-27 13:43:39 +01004149
Chris Wilson05394f32010-11-08 19:18:58 +00004150 obj->base.read_domains = obj->base.pending_read_domains;
4151 obj->base.write_domain = obj->base.pending_write_domain;
Chris Wilsoncaea7472010-11-12 13:53:37 +00004152 obj->fenced_gpu_access = obj->pending_fenced_gpu_access;
Chris Wilson7e318e12010-10-27 13:43:39 +01004153
4154 i915_gem_object_move_to_active(obj, ring);
Chris Wilson05394f32010-11-08 19:18:58 +00004155 if (obj->base.write_domain) {
4156 obj->dirty = 1;
4157 list_move_tail(&obj->gpu_write_list,
Chris Wilson7e318e12010-10-27 13:43:39 +01004158 &ring->gpu_write_list);
4159 intel_mark_busy(dev, obj);
4160 }
4161
4162 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00004163 obj->base.read_domains,
4164 obj->base.write_domain);
Chris Wilson7e318e12010-10-27 13:43:39 +01004165 }
4166
Eric Anholt673a3942008-07-30 12:06:12 -07004167 /*
4168 * Ensure that the commands in the batch buffer are
4169 * finished before the interrupt fires
4170 */
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01004171 i915_retire_commands(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07004172
Chris Wilson3cce4692010-10-27 16:11:02 +01004173 if (i915_add_request(dev, file, request, ring))
Chris Wilson5d97eb62010-11-10 20:40:02 +00004174 i915_gem_next_request_seqno(dev, ring);
Chris Wilson3cce4692010-10-27 16:11:02 +01004175 else
4176 request = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07004177
Eric Anholt673a3942008-07-30 12:06:12 -07004178err:
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05004179 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson05394f32010-11-08 19:18:58 +00004180 object_list[i]->in_execbuffer = false;
4181 drm_gem_object_unreference(&object_list[i]->base);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05004182 }
Julia Lawallaad87df2008-12-21 16:28:47 +01004183
Eric Anholt673a3942008-07-30 12:06:12 -07004184 mutex_unlock(&dev->struct_mutex);
4185
Chris Wilson93533c22010-01-31 10:40:48 +00004186pre_mutex_err:
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07004187 drm_free_large(object_list);
Eric Anholt9a298b22009-03-24 12:23:04 -07004188 kfree(cliprects);
Chris Wilson8dc5d142010-08-12 12:36:12 +01004189 kfree(request);
Eric Anholt673a3942008-07-30 12:06:12 -07004190
4191 return ret;
4192}
4193
Jesse Barnes76446ca2009-12-17 22:05:42 -05004194/*
4195 * Legacy execbuffer just creates an exec2 list from the original exec object
4196 * list array and passes it to the real function.
4197 */
4198int
4199i915_gem_execbuffer(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00004200 struct drm_file *file)
Jesse Barnes76446ca2009-12-17 22:05:42 -05004201{
4202 struct drm_i915_gem_execbuffer *args = data;
4203 struct drm_i915_gem_execbuffer2 exec2;
4204 struct drm_i915_gem_exec_object *exec_list = NULL;
4205 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
4206 int ret, i;
4207
4208#if WATCH_EXEC
4209 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
4210 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
4211#endif
4212
4213 if (args->buffer_count < 1) {
4214 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
4215 return -EINVAL;
4216 }
4217
4218 /* Copy in the exec list from userland */
4219 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
4220 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
4221 if (exec_list == NULL || exec2_list == NULL) {
4222 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
4223 args->buffer_count);
4224 drm_free_large(exec_list);
4225 drm_free_large(exec2_list);
4226 return -ENOMEM;
4227 }
4228 ret = copy_from_user(exec_list,
4229 (struct drm_i915_relocation_entry __user *)
4230 (uintptr_t) args->buffers_ptr,
4231 sizeof(*exec_list) * args->buffer_count);
4232 if (ret != 0) {
4233 DRM_ERROR("copy %d exec entries failed %d\n",
4234 args->buffer_count, ret);
4235 drm_free_large(exec_list);
4236 drm_free_large(exec2_list);
4237 return -EFAULT;
4238 }
4239
4240 for (i = 0; i < args->buffer_count; i++) {
4241 exec2_list[i].handle = exec_list[i].handle;
4242 exec2_list[i].relocation_count = exec_list[i].relocation_count;
4243 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
4244 exec2_list[i].alignment = exec_list[i].alignment;
4245 exec2_list[i].offset = exec_list[i].offset;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004246 if (INTEL_INFO(dev)->gen < 4)
Jesse Barnes76446ca2009-12-17 22:05:42 -05004247 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
4248 else
4249 exec2_list[i].flags = 0;
4250 }
4251
4252 exec2.buffers_ptr = args->buffers_ptr;
4253 exec2.buffer_count = args->buffer_count;
4254 exec2.batch_start_offset = args->batch_start_offset;
4255 exec2.batch_len = args->batch_len;
4256 exec2.DR1 = args->DR1;
4257 exec2.DR4 = args->DR4;
4258 exec2.num_cliprects = args->num_cliprects;
4259 exec2.cliprects_ptr = args->cliprects_ptr;
Zou Nan hai852835f2010-05-21 09:08:56 +08004260 exec2.flags = I915_EXEC_RENDER;
Jesse Barnes76446ca2009-12-17 22:05:42 -05004261
Chris Wilson05394f32010-11-08 19:18:58 +00004262 ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list);
Jesse Barnes76446ca2009-12-17 22:05:42 -05004263 if (!ret) {
4264 /* Copy the new buffer offsets back to the user's exec list. */
4265 for (i = 0; i < args->buffer_count; i++)
4266 exec_list[i].offset = exec2_list[i].offset;
4267 /* ... and back out to userspace */
4268 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
4269 (uintptr_t) args->buffers_ptr,
4270 exec_list,
4271 sizeof(*exec_list) * args->buffer_count);
4272 if (ret) {
4273 ret = -EFAULT;
4274 DRM_ERROR("failed to copy %d exec entries "
4275 "back to user (%d)\n",
4276 args->buffer_count, ret);
4277 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05004278 }
4279
4280 drm_free_large(exec_list);
4281 drm_free_large(exec2_list);
4282 return ret;
4283}
4284
4285int
4286i915_gem_execbuffer2(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00004287 struct drm_file *file)
Jesse Barnes76446ca2009-12-17 22:05:42 -05004288{
4289 struct drm_i915_gem_execbuffer2 *args = data;
4290 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
4291 int ret;
4292
4293#if WATCH_EXEC
4294 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
4295 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
4296#endif
4297
4298 if (args->buffer_count < 1) {
4299 DRM_ERROR("execbuf2 with %d buffers\n", args->buffer_count);
4300 return -EINVAL;
4301 }
4302
4303 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
4304 if (exec2_list == NULL) {
4305 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
4306 args->buffer_count);
4307 return -ENOMEM;
4308 }
4309 ret = copy_from_user(exec2_list,
4310 (struct drm_i915_relocation_entry __user *)
4311 (uintptr_t) args->buffers_ptr,
4312 sizeof(*exec2_list) * args->buffer_count);
4313 if (ret != 0) {
4314 DRM_ERROR("copy %d exec entries failed %d\n",
4315 args->buffer_count, ret);
4316 drm_free_large(exec2_list);
4317 return -EFAULT;
4318 }
4319
Chris Wilson05394f32010-11-08 19:18:58 +00004320 ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list);
Jesse Barnes76446ca2009-12-17 22:05:42 -05004321 if (!ret) {
4322 /* Copy the new buffer offsets back to the user's exec list. */
4323 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
4324 (uintptr_t) args->buffers_ptr,
4325 exec2_list,
4326 sizeof(*exec2_list) * args->buffer_count);
4327 if (ret) {
4328 ret = -EFAULT;
4329 DRM_ERROR("failed to copy %d exec entries "
4330 "back to user (%d)\n",
4331 args->buffer_count, ret);
4332 }
4333 }
4334
4335 drm_free_large(exec2_list);
4336 return ret;
4337}
4338
Eric Anholt673a3942008-07-30 12:06:12 -07004339int
Chris Wilson05394f32010-11-08 19:18:58 +00004340i915_gem_object_pin(struct drm_i915_gem_object *obj,
4341 uint32_t alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01004342 bool map_and_fenceable)
Eric Anholt673a3942008-07-30 12:06:12 -07004343{
Chris Wilson05394f32010-11-08 19:18:58 +00004344 struct drm_device *dev = obj->base.dev;
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004345 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07004346 int ret;
4347
Chris Wilson05394f32010-11-08 19:18:58 +00004348 BUG_ON(obj->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT);
Chris Wilson23bc5982010-09-29 16:10:57 +01004349 WARN_ON(i915_verify_lists(dev));
Chris Wilsonac0c6b52010-05-27 13:18:18 +01004350
Chris Wilson05394f32010-11-08 19:18:58 +00004351 if (obj->gtt_space != NULL) {
4352 if ((alignment && obj->gtt_offset & (alignment - 1)) ||
4353 (map_and_fenceable && !obj->map_and_fenceable)) {
4354 WARN(obj->pin_count,
Chris Wilsonae7d49d2010-08-04 12:37:41 +01004355 "bo is already pinned with incorrect alignment:"
Daniel Vetter75e9e912010-11-04 17:11:09 +01004356 " offset=%x, req.alignment=%x, req.map_and_fenceable=%d,"
4357 " obj->map_and_fenceable=%d\n",
Chris Wilson05394f32010-11-08 19:18:58 +00004358 obj->gtt_offset, alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01004359 map_and_fenceable,
Chris Wilson05394f32010-11-08 19:18:58 +00004360 obj->map_and_fenceable);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01004361 ret = i915_gem_object_unbind(obj);
4362 if (ret)
4363 return ret;
4364 }
4365 }
4366
Chris Wilson05394f32010-11-08 19:18:58 +00004367 if (obj->gtt_space == NULL) {
Chris Wilsona00b10c2010-09-24 21:15:47 +01004368 ret = i915_gem_object_bind_to_gtt(obj, alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01004369 map_and_fenceable);
Chris Wilson97311292009-09-21 00:22:34 +01004370 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07004371 return ret;
Chris Wilson22c344e2009-02-11 14:26:45 +00004372 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05004373
Chris Wilson05394f32010-11-08 19:18:58 +00004374 if (obj->pin_count++ == 0) {
4375 i915_gem_info_add_pin(dev_priv, obj, map_and_fenceable);
4376 if (!obj->active)
4377 list_move_tail(&obj->mm_list,
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004378 &dev_priv->mm.pinned_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004379 }
Chris Wilson05394f32010-11-08 19:18:58 +00004380 BUG_ON(!obj->pin_mappable && map_and_fenceable);
Eric Anholt673a3942008-07-30 12:06:12 -07004381
Chris Wilson23bc5982010-09-29 16:10:57 +01004382 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07004383 return 0;
4384}
4385
4386void
Chris Wilson05394f32010-11-08 19:18:58 +00004387i915_gem_object_unpin(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07004388{
Chris Wilson05394f32010-11-08 19:18:58 +00004389 struct drm_device *dev = obj->base.dev;
Eric Anholt673a3942008-07-30 12:06:12 -07004390 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07004391
Chris Wilson23bc5982010-09-29 16:10:57 +01004392 WARN_ON(i915_verify_lists(dev));
Chris Wilson05394f32010-11-08 19:18:58 +00004393 BUG_ON(obj->pin_count == 0);
4394 BUG_ON(obj->gtt_space == NULL);
Eric Anholt673a3942008-07-30 12:06:12 -07004395
Chris Wilson05394f32010-11-08 19:18:58 +00004396 if (--obj->pin_count == 0) {
4397 if (!obj->active)
4398 list_move_tail(&obj->mm_list,
Eric Anholt673a3942008-07-30 12:06:12 -07004399 &dev_priv->mm.inactive_list);
Chris Wilson05394f32010-11-08 19:18:58 +00004400 i915_gem_info_remove_pin(dev_priv, obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004401 }
Chris Wilson23bc5982010-09-29 16:10:57 +01004402 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07004403}
4404
4405int
4406i915_gem_pin_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_pin *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004410 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07004411 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 }
Eric Anholt673a3942008-07-30 12:06:12 -07004422
Chris Wilson05394f32010-11-08 19:18:58 +00004423 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004424 DRM_ERROR("Attempting to pin a purgeable buffer\n");
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004425 ret = -EINVAL;
4426 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004427 }
4428
Chris Wilson05394f32010-11-08 19:18:58 +00004429 if (obj->pin_filp != NULL && obj->pin_filp != file) {
Jesse Barnes79e53942008-11-07 14:24:08 -08004430 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
4431 args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004432 ret = -EINVAL;
4433 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08004434 }
4435
Chris Wilson05394f32010-11-08 19:18:58 +00004436 obj->user_pin_count++;
4437 obj->pin_filp = file;
4438 if (obj->user_pin_count == 1) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01004439 ret = i915_gem_object_pin(obj, args->alignment, true);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004440 if (ret)
4441 goto out;
Eric Anholt673a3942008-07-30 12:06:12 -07004442 }
4443
4444 /* XXX - flush the CPU caches for pinned objects
4445 * as the X server doesn't manage domains yet
4446 */
Eric Anholte47c68e2008-11-14 13:35:19 -08004447 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00004448 args->offset = obj->gtt_offset;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004449out:
Chris Wilson05394f32010-11-08 19:18:58 +00004450 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004451unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07004452 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004453 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004454}
4455
4456int
4457i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00004458 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07004459{
4460 struct drm_i915_gem_pin *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004461 struct drm_i915_gem_object *obj;
Chris Wilson76c1dec2010-09-25 11:22:51 +01004462 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004463
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004464 ret = i915_mutex_lock_interruptible(dev);
4465 if (ret)
4466 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004467
Chris Wilson05394f32010-11-08 19:18:58 +00004468 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Eric Anholt673a3942008-07-30 12:06:12 -07004469 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004470 ret = -ENOENT;
4471 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07004472 }
Chris Wilson76c1dec2010-09-25 11:22:51 +01004473
Chris Wilson05394f32010-11-08 19:18:58 +00004474 if (obj->pin_filp != file) {
Jesse Barnes79e53942008-11-07 14:24:08 -08004475 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
4476 args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004477 ret = -EINVAL;
4478 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08004479 }
Chris Wilson05394f32010-11-08 19:18:58 +00004480 obj->user_pin_count--;
4481 if (obj->user_pin_count == 0) {
4482 obj->pin_filp = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08004483 i915_gem_object_unpin(obj);
4484 }
Eric Anholt673a3942008-07-30 12:06:12 -07004485
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004486out:
Chris Wilson05394f32010-11-08 19:18:58 +00004487 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004488unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07004489 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004490 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004491}
4492
4493int
4494i915_gem_busy_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00004495 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07004496{
4497 struct drm_i915_gem_busy *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004498 struct drm_i915_gem_object *obj;
Chris Wilson30dbf0c2010-09-25 10:19:17 +01004499 int ret;
4500
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004501 ret = i915_mutex_lock_interruptible(dev);
4502 if (ret)
4503 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004504
Chris Wilson05394f32010-11-08 19:18:58 +00004505 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Eric Anholt673a3942008-07-30 12:06:12 -07004506 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004507 ret = -ENOENT;
4508 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07004509 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08004510
Chris Wilson0be555b2010-08-04 15:36:30 +01004511 /* Count all active objects as busy, even if they are currently not used
4512 * by the gpu. Users of this interface expect objects to eventually
4513 * become non-busy without any further actions, therefore emit any
4514 * necessary flushes here.
Eric Anholtc4de0a52008-12-14 19:05:04 -08004515 */
Chris Wilson05394f32010-11-08 19:18:58 +00004516 args->busy = obj->active;
Chris Wilson0be555b2010-08-04 15:36:30 +01004517 if (args->busy) {
4518 /* Unconditionally flush objects, even when the gpu still uses this
4519 * object. Userspace calling this function indicates that it wants to
4520 * use this buffer rather sooner than later, so issuing the required
4521 * flush earlier is beneficial.
4522 */
Chris Wilson05394f32010-11-08 19:18:58 +00004523 if (obj->base.write_domain & I915_GEM_GPU_DOMAINS)
4524 i915_gem_flush_ring(dev, obj->ring,
4525 0, obj->base.write_domain);
Chris Wilson0be555b2010-08-04 15:36:30 +01004526
4527 /* Update the active list for the hardware's current position.
4528 * Otherwise this only updates on a delayed timer or when irqs
4529 * are actually unmasked, and our working set ends up being
4530 * larger than required.
4531 */
Chris Wilson05394f32010-11-08 19:18:58 +00004532 i915_gem_retire_requests_ring(dev, obj->ring);
Chris Wilson0be555b2010-08-04 15:36:30 +01004533
Chris Wilson05394f32010-11-08 19:18:58 +00004534 args->busy = obj->active;
Chris Wilson0be555b2010-08-04 15:36:30 +01004535 }
Eric Anholt673a3942008-07-30 12:06:12 -07004536
Chris Wilson05394f32010-11-08 19:18:58 +00004537 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004538unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07004539 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004540 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004541}
4542
4543int
4544i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4545 struct drm_file *file_priv)
4546{
4547 return i915_gem_ring_throttle(dev, file_priv);
4548}
4549
Chris Wilson3ef94da2009-09-14 16:50:29 +01004550int
4551i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4552 struct drm_file *file_priv)
4553{
4554 struct drm_i915_gem_madvise *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004555 struct drm_i915_gem_object *obj;
Chris Wilson76c1dec2010-09-25 11:22:51 +01004556 int ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004557
4558 switch (args->madv) {
4559 case I915_MADV_DONTNEED:
4560 case I915_MADV_WILLNEED:
4561 break;
4562 default:
4563 return -EINVAL;
4564 }
4565
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004566 ret = i915_mutex_lock_interruptible(dev);
4567 if (ret)
4568 return ret;
4569
Chris Wilson05394f32010-11-08 19:18:58 +00004570 obj = to_intel_bo(drm_gem_object_lookup(dev, file_priv, args->handle));
Chris Wilson3ef94da2009-09-14 16:50:29 +01004571 if (obj == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004572 ret = -ENOENT;
4573 goto unlock;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004574 }
Chris Wilson3ef94da2009-09-14 16:50:29 +01004575
Chris Wilson05394f32010-11-08 19:18:58 +00004576 if (obj->pin_count) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004577 ret = -EINVAL;
4578 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004579 }
4580
Chris Wilson05394f32010-11-08 19:18:58 +00004581 if (obj->madv != __I915_MADV_PURGED)
4582 obj->madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004583
Chris Wilson2d7ef392009-09-20 23:13:10 +01004584 /* if the object is no longer bound, discard its backing storage */
Chris Wilson05394f32010-11-08 19:18:58 +00004585 if (i915_gem_object_is_purgeable(obj) &&
4586 obj->gtt_space == NULL)
Chris Wilson2d7ef392009-09-20 23:13:10 +01004587 i915_gem_object_truncate(obj);
4588
Chris Wilson05394f32010-11-08 19:18:58 +00004589 args->retained = obj->madv != __I915_MADV_PURGED;
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004590
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004591out:
Chris Wilson05394f32010-11-08 19:18:58 +00004592 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004593unlock:
Chris Wilson3ef94da2009-09-14 16:50:29 +01004594 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004595 return ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004596}
4597
Chris Wilson05394f32010-11-08 19:18:58 +00004598struct drm_i915_gem_object *i915_gem_alloc_object(struct drm_device *dev,
4599 size_t size)
Daniel Vetterac52bc52010-04-09 19:05:06 +00004600{
Chris Wilson73aa8082010-09-30 11:46:12 +01004601 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc397b902010-04-09 19:05:07 +00004602 struct drm_i915_gem_object *obj;
4603
4604 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
4605 if (obj == NULL)
4606 return NULL;
4607
4608 if (drm_gem_object_init(dev, &obj->base, size) != 0) {
4609 kfree(obj);
4610 return NULL;
4611 }
4612
Chris Wilson73aa8082010-09-30 11:46:12 +01004613 i915_gem_info_add_obj(dev_priv, size);
4614
Daniel Vetterc397b902010-04-09 19:05:07 +00004615 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4616 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4617
4618 obj->agp_type = AGP_USER_MEMORY;
Daniel Vetter62b8b212010-04-09 19:05:08 +00004619 obj->base.driver_private = NULL;
Daniel Vetterc397b902010-04-09 19:05:07 +00004620 obj->fence_reg = I915_FENCE_REG_NONE;
Chris Wilson69dc4982010-10-19 10:36:51 +01004621 INIT_LIST_HEAD(&obj->mm_list);
Daniel Vetter93a37f22010-11-05 20:24:53 +01004622 INIT_LIST_HEAD(&obj->gtt_list);
Chris Wilson69dc4982010-10-19 10:36:51 +01004623 INIT_LIST_HEAD(&obj->ring_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00004624 INIT_LIST_HEAD(&obj->gpu_write_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00004625 obj->madv = I915_MADV_WILLNEED;
Daniel Vetter75e9e912010-11-04 17:11:09 +01004626 /* Avoid an unnecessary call to unbind on the first bind. */
4627 obj->map_and_fenceable = true;
Daniel Vetterc397b902010-04-09 19:05:07 +00004628
Chris Wilson05394f32010-11-08 19:18:58 +00004629 return obj;
Daniel Vetterac52bc52010-04-09 19:05:06 +00004630}
4631
Eric Anholt673a3942008-07-30 12:06:12 -07004632int i915_gem_init_object(struct drm_gem_object *obj)
4633{
Daniel Vetterc397b902010-04-09 19:05:07 +00004634 BUG();
Jesse Barnesde151cf2008-11-12 10:03:55 -08004635
Eric Anholt673a3942008-07-30 12:06:12 -07004636 return 0;
4637}
4638
Chris Wilson05394f32010-11-08 19:18:58 +00004639static void i915_gem_free_object_tail(struct drm_i915_gem_object *obj)
Chris Wilsonbe726152010-07-23 23:18:50 +01004640{
Chris Wilson05394f32010-11-08 19:18:58 +00004641 struct drm_device *dev = obj->base.dev;
Chris Wilsonbe726152010-07-23 23:18:50 +01004642 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonbe726152010-07-23 23:18:50 +01004643 int ret;
4644
4645 ret = i915_gem_object_unbind(obj);
4646 if (ret == -ERESTARTSYS) {
Chris Wilson05394f32010-11-08 19:18:58 +00004647 list_move(&obj->mm_list,
Chris Wilsonbe726152010-07-23 23:18:50 +01004648 &dev_priv->mm.deferred_free_list);
4649 return;
4650 }
4651
Chris Wilson05394f32010-11-08 19:18:58 +00004652 if (obj->base.map_list.map)
Chris Wilsonbe726152010-07-23 23:18:50 +01004653 i915_gem_free_mmap_offset(obj);
4654
Chris Wilson05394f32010-11-08 19:18:58 +00004655 drm_gem_object_release(&obj->base);
4656 i915_gem_info_remove_obj(dev_priv, obj->base.size);
Chris Wilsonbe726152010-07-23 23:18:50 +01004657
Chris Wilson05394f32010-11-08 19:18:58 +00004658 kfree(obj->page_cpu_valid);
4659 kfree(obj->bit_17);
4660 kfree(obj);
Chris Wilsonbe726152010-07-23 23:18:50 +01004661}
4662
Chris Wilson05394f32010-11-08 19:18:58 +00004663void i915_gem_free_object(struct drm_gem_object *gem_obj)
Eric Anholt673a3942008-07-30 12:06:12 -07004664{
Chris Wilson05394f32010-11-08 19:18:58 +00004665 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
4666 struct drm_device *dev = obj->base.dev;
Eric Anholt673a3942008-07-30 12:06:12 -07004667
Chris Wilson1c5d22f2009-08-25 11:15:50 +01004668 trace_i915_gem_object_destroy(obj);
4669
Chris Wilson05394f32010-11-08 19:18:58 +00004670 while (obj->pin_count > 0)
Eric Anholt673a3942008-07-30 12:06:12 -07004671 i915_gem_object_unpin(obj);
4672
Chris Wilson05394f32010-11-08 19:18:58 +00004673 if (obj->phys_obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004674 i915_gem_detach_phys_object(dev, obj);
4675
Chris Wilsonbe726152010-07-23 23:18:50 +01004676 i915_gem_free_object_tail(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004677}
4678
Jesse Barnes5669fca2009-02-17 15:13:31 -08004679int
Eric Anholt673a3942008-07-30 12:06:12 -07004680i915_gem_idle(struct drm_device *dev)
4681{
4682 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson29105cc2010-01-07 10:39:13 +00004683 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004684
Keith Packard6dbe2772008-10-14 21:41:13 -07004685 mutex_lock(&dev->struct_mutex);
4686
Chris Wilson87acb0a2010-10-19 10:13:00 +01004687 if (dev_priv->mm.suspended) {
Keith Packard6dbe2772008-10-14 21:41:13 -07004688 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004689 return 0;
Keith Packard6dbe2772008-10-14 21:41:13 -07004690 }
Eric Anholt673a3942008-07-30 12:06:12 -07004691
Chris Wilson29105cc2010-01-07 10:39:13 +00004692 ret = i915_gpu_idle(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004693 if (ret) {
4694 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004695 return ret;
Keith Packard6dbe2772008-10-14 21:41:13 -07004696 }
Eric Anholt673a3942008-07-30 12:06:12 -07004697
Chris Wilson29105cc2010-01-07 10:39:13 +00004698 /* Under UMS, be paranoid and evict. */
4699 if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
Chris Wilson5eac3ab2010-10-31 08:49:47 +00004700 ret = i915_gem_evict_inactive(dev, false);
Chris Wilson29105cc2010-01-07 10:39:13 +00004701 if (ret) {
4702 mutex_unlock(&dev->struct_mutex);
4703 return ret;
4704 }
4705 }
4706
Chris Wilson312817a2010-11-22 11:50:11 +00004707 i915_gem_reset_fences(dev);
4708
Chris Wilson29105cc2010-01-07 10:39:13 +00004709 /* Hack! Don't let anybody do execbuf while we don't control the chip.
4710 * We need to replace this with a semaphore, or something.
4711 * And not confound mm.suspended!
4712 */
4713 dev_priv->mm.suspended = 1;
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02004714 del_timer_sync(&dev_priv->hangcheck_timer);
Chris Wilson29105cc2010-01-07 10:39:13 +00004715
4716 i915_kernel_lost_context(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004717 i915_gem_cleanup_ringbuffer(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00004718
Keith Packard6dbe2772008-10-14 21:41:13 -07004719 mutex_unlock(&dev->struct_mutex);
4720
Chris Wilson29105cc2010-01-07 10:39:13 +00004721 /* Cancel the retire work handler, which should be idle now. */
4722 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
4723
Eric Anholt673a3942008-07-30 12:06:12 -07004724 return 0;
4725}
4726
Eric Anholt673a3942008-07-30 12:06:12 -07004727int
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004728i915_gem_init_ringbuffer(struct drm_device *dev)
4729{
4730 drm_i915_private_t *dev_priv = dev->dev_private;
4731 int ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01004732
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08004733 ret = intel_init_render_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004734 if (ret)
Chris Wilsonb6913e42010-11-12 10:46:37 +00004735 return ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01004736
4737 if (HAS_BSD(dev)) {
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08004738 ret = intel_init_bsd_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004739 if (ret)
4740 goto cleanup_render_ring;
Zou Nan haid1b851f2010-05-21 09:08:57 +08004741 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004742
Chris Wilson549f7362010-10-19 11:19:32 +01004743 if (HAS_BLT(dev)) {
4744 ret = intel_init_blt_ring_buffer(dev);
4745 if (ret)
4746 goto cleanup_bsd_ring;
4747 }
4748
Chris Wilson6f392d5482010-08-07 11:01:22 +01004749 dev_priv->next_seqno = 1;
4750
Chris Wilson68f95ba2010-05-27 13:18:22 +01004751 return 0;
4752
Chris Wilson549f7362010-10-19 11:19:32 +01004753cleanup_bsd_ring:
Chris Wilson78501ea2010-10-27 12:18:21 +01004754 intel_cleanup_ring_buffer(&dev_priv->bsd_ring);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004755cleanup_render_ring:
Chris Wilson78501ea2010-10-27 12:18:21 +01004756 intel_cleanup_ring_buffer(&dev_priv->render_ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004757 return ret;
4758}
4759
4760void
4761i915_gem_cleanup_ringbuffer(struct drm_device *dev)
4762{
4763 drm_i915_private_t *dev_priv = dev->dev_private;
4764
Chris Wilson78501ea2010-10-27 12:18:21 +01004765 intel_cleanup_ring_buffer(&dev_priv->render_ring);
4766 intel_cleanup_ring_buffer(&dev_priv->bsd_ring);
4767 intel_cleanup_ring_buffer(&dev_priv->blt_ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004768}
4769
4770int
Eric Anholt673a3942008-07-30 12:06:12 -07004771i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
4772 struct drm_file *file_priv)
4773{
4774 drm_i915_private_t *dev_priv = dev->dev_private;
4775 int ret;
4776
Jesse Barnes79e53942008-11-07 14:24:08 -08004777 if (drm_core_check_feature(dev, DRIVER_MODESET))
4778 return 0;
4779
Ben Gamariba1234d2009-09-14 17:48:47 -04004780 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07004781 DRM_ERROR("Reenabling wedged hardware, good luck\n");
Ben Gamariba1234d2009-09-14 17:48:47 -04004782 atomic_set(&dev_priv->mm.wedged, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004783 }
4784
Eric Anholt673a3942008-07-30 12:06:12 -07004785 mutex_lock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004786 dev_priv->mm.suspended = 0;
4787
4788 ret = i915_gem_init_ringbuffer(dev);
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004789 if (ret != 0) {
4790 mutex_unlock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004791 return ret;
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004792 }
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004793
Chris Wilson69dc4982010-10-19 10:36:51 +01004794 BUG_ON(!list_empty(&dev_priv->mm.active_list));
Zou Nan hai852835f2010-05-21 09:08:56 +08004795 BUG_ON(!list_empty(&dev_priv->render_ring.active_list));
Chris Wilson87acb0a2010-10-19 10:13:00 +01004796 BUG_ON(!list_empty(&dev_priv->bsd_ring.active_list));
Chris Wilson549f7362010-10-19 11:19:32 +01004797 BUG_ON(!list_empty(&dev_priv->blt_ring.active_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004798 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
4799 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
Zou Nan hai852835f2010-05-21 09:08:56 +08004800 BUG_ON(!list_empty(&dev_priv->render_ring.request_list));
Chris Wilson87acb0a2010-10-19 10:13:00 +01004801 BUG_ON(!list_empty(&dev_priv->bsd_ring.request_list));
Chris Wilson549f7362010-10-19 11:19:32 +01004802 BUG_ON(!list_empty(&dev_priv->blt_ring.request_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004803 mutex_unlock(&dev->struct_mutex);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004804
Chris Wilson5f353082010-06-07 14:03:03 +01004805 ret = drm_irq_install(dev);
4806 if (ret)
4807 goto cleanup_ringbuffer;
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004808
Eric Anholt673a3942008-07-30 12:06:12 -07004809 return 0;
Chris Wilson5f353082010-06-07 14:03:03 +01004810
4811cleanup_ringbuffer:
4812 mutex_lock(&dev->struct_mutex);
4813 i915_gem_cleanup_ringbuffer(dev);
4814 dev_priv->mm.suspended = 1;
4815 mutex_unlock(&dev->struct_mutex);
4816
4817 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004818}
4819
4820int
4821i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
4822 struct drm_file *file_priv)
4823{
Jesse Barnes79e53942008-11-07 14:24:08 -08004824 if (drm_core_check_feature(dev, DRIVER_MODESET))
4825 return 0;
4826
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004827 drm_irq_uninstall(dev);
Linus Torvaldse6890f62009-09-08 17:09:24 -07004828 return i915_gem_idle(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004829}
4830
4831void
4832i915_gem_lastclose(struct drm_device *dev)
4833{
4834 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004835
Eric Anholte806b492009-01-22 09:56:58 -08004836 if (drm_core_check_feature(dev, DRIVER_MODESET))
4837 return;
4838
Keith Packard6dbe2772008-10-14 21:41:13 -07004839 ret = i915_gem_idle(dev);
4840 if (ret)
4841 DRM_ERROR("failed to idle hardware: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07004842}
4843
Chris Wilson64193402010-10-24 12:38:05 +01004844static void
4845init_ring_lists(struct intel_ring_buffer *ring)
4846{
4847 INIT_LIST_HEAD(&ring->active_list);
4848 INIT_LIST_HEAD(&ring->request_list);
4849 INIT_LIST_HEAD(&ring->gpu_write_list);
4850}
4851
Eric Anholt673a3942008-07-30 12:06:12 -07004852void
4853i915_gem_load(struct drm_device *dev)
4854{
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004855 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07004856 drm_i915_private_t *dev_priv = dev->dev_private;
4857
Chris Wilson69dc4982010-10-19 10:36:51 +01004858 INIT_LIST_HEAD(&dev_priv->mm.active_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004859 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
4860 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004861 INIT_LIST_HEAD(&dev_priv->mm.pinned_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07004862 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Chris Wilsonbe726152010-07-23 23:18:50 +01004863 INIT_LIST_HEAD(&dev_priv->mm.deferred_free_list);
Daniel Vetter93a37f22010-11-05 20:24:53 +01004864 INIT_LIST_HEAD(&dev_priv->mm.gtt_list);
Chris Wilson64193402010-10-24 12:38:05 +01004865 init_ring_lists(&dev_priv->render_ring);
4866 init_ring_lists(&dev_priv->bsd_ring);
4867 init_ring_lists(&dev_priv->blt_ring);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02004868 for (i = 0; i < 16; i++)
4869 INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004870 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
4871 i915_gem_retire_work_handler);
Chris Wilson30dbf0c2010-09-25 10:19:17 +01004872 init_completion(&dev_priv->error_completion);
Chris Wilson31169712009-09-14 16:50:28 +01004873
Dave Airlie94400122010-07-20 13:15:31 +10004874 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
4875 if (IS_GEN3(dev)) {
4876 u32 tmp = I915_READ(MI_ARB_STATE);
4877 if (!(tmp & MI_ARB_C3_LP_WRITE_ENABLE)) {
4878 /* arb state is a masked write, so set bit + bit in mask */
4879 tmp = MI_ARB_C3_LP_WRITE_ENABLE | (MI_ARB_C3_LP_WRITE_ENABLE << MI_ARB_MASK_SHIFT);
4880 I915_WRITE(MI_ARB_STATE, tmp);
4881 }
4882 }
4883
Jesse Barnesde151cf2008-11-12 10:03:55 -08004884 /* Old X drivers will take 0-2 for front, back, depth buffers */
Eric Anholtb397c832010-01-26 09:43:10 -08004885 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4886 dev_priv->fence_reg_start = 3;
Jesse Barnesde151cf2008-11-12 10:03:55 -08004887
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004888 if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08004889 dev_priv->num_fence_regs = 16;
4890 else
4891 dev_priv->num_fence_regs = 8;
4892
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004893 /* Initialize fence registers to zero */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004894 switch (INTEL_INFO(dev)->gen) {
4895 case 6:
4896 for (i = 0; i < 16; i++)
4897 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (i * 8), 0);
4898 break;
4899 case 5:
4900 case 4:
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004901 for (i = 0; i < 16; i++)
4902 I915_WRITE64(FENCE_REG_965_0 + (i * 8), 0);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004903 break;
4904 case 3:
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004905 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4906 for (i = 0; i < 8; i++)
4907 I915_WRITE(FENCE_REG_945_8 + (i * 4), 0);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004908 case 2:
4909 for (i = 0; i < 8; i++)
4910 I915_WRITE(FENCE_REG_830_0 + (i * 4), 0);
4911 break;
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004912 }
Eric Anholt673a3942008-07-30 12:06:12 -07004913 i915_gem_detect_bit_6_swizzle(dev);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05004914 init_waitqueue_head(&dev_priv->pending_flip_queue);
Chris Wilson17250b72010-10-28 12:51:39 +01004915
4916 dev_priv->mm.inactive_shrinker.shrink = i915_gem_inactive_shrink;
4917 dev_priv->mm.inactive_shrinker.seeks = DEFAULT_SEEKS;
4918 register_shrinker(&dev_priv->mm.inactive_shrinker);
Eric Anholt673a3942008-07-30 12:06:12 -07004919}
Dave Airlie71acb5e2008-12-30 20:31:46 +10004920
4921/*
4922 * Create a physically contiguous memory object for this object
4923 * e.g. for cursor + overlay regs
4924 */
Chris Wilson995b6762010-08-20 13:23:26 +01004925static int i915_gem_init_phys_object(struct drm_device *dev,
4926 int id, int size, int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004927{
4928 drm_i915_private_t *dev_priv = dev->dev_private;
4929 struct drm_i915_gem_phys_object *phys_obj;
4930 int ret;
4931
4932 if (dev_priv->mm.phys_objs[id - 1] || !size)
4933 return 0;
4934
Eric Anholt9a298b22009-03-24 12:23:04 -07004935 phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004936 if (!phys_obj)
4937 return -ENOMEM;
4938
4939 phys_obj->id = id;
4940
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004941 phys_obj->handle = drm_pci_alloc(dev, size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004942 if (!phys_obj->handle) {
4943 ret = -ENOMEM;
4944 goto kfree_obj;
4945 }
4946#ifdef CONFIG_X86
4947 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4948#endif
4949
4950 dev_priv->mm.phys_objs[id - 1] = phys_obj;
4951
4952 return 0;
4953kfree_obj:
Eric Anholt9a298b22009-03-24 12:23:04 -07004954 kfree(phys_obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004955 return ret;
4956}
4957
Chris Wilson995b6762010-08-20 13:23:26 +01004958static void i915_gem_free_phys_object(struct drm_device *dev, int id)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004959{
4960 drm_i915_private_t *dev_priv = dev->dev_private;
4961 struct drm_i915_gem_phys_object *phys_obj;
4962
4963 if (!dev_priv->mm.phys_objs[id - 1])
4964 return;
4965
4966 phys_obj = dev_priv->mm.phys_objs[id - 1];
4967 if (phys_obj->cur_obj) {
4968 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
4969 }
4970
4971#ifdef CONFIG_X86
4972 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4973#endif
4974 drm_pci_free(dev, phys_obj->handle);
4975 kfree(phys_obj);
4976 dev_priv->mm.phys_objs[id - 1] = NULL;
4977}
4978
4979void i915_gem_free_all_phys_object(struct drm_device *dev)
4980{
4981 int i;
4982
Dave Airlie260883c2009-01-22 17:58:49 +10004983 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004984 i915_gem_free_phys_object(dev, i);
4985}
4986
4987void i915_gem_detach_phys_object(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00004988 struct drm_i915_gem_object *obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004989{
Chris Wilson05394f32010-11-08 19:18:58 +00004990 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Chris Wilsone5281cc2010-10-28 13:45:36 +01004991 char *vaddr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004992 int i;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004993 int page_count;
4994
Chris Wilson05394f32010-11-08 19:18:58 +00004995 if (!obj->phys_obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004996 return;
Chris Wilson05394f32010-11-08 19:18:58 +00004997 vaddr = obj->phys_obj->handle->vaddr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004998
Chris Wilson05394f32010-11-08 19:18:58 +00004999 page_count = obj->base.size / PAGE_SIZE;
Dave Airlie71acb5e2008-12-30 20:31:46 +10005000 for (i = 0; i < page_count; i++) {
Chris Wilsone5281cc2010-10-28 13:45:36 +01005001 struct page *page = read_cache_page_gfp(mapping, i,
5002 GFP_HIGHUSER | __GFP_RECLAIMABLE);
5003 if (!IS_ERR(page)) {
5004 char *dst = kmap_atomic(page);
5005 memcpy(dst, vaddr + i*PAGE_SIZE, PAGE_SIZE);
5006 kunmap_atomic(dst);
Dave Airlie71acb5e2008-12-30 20:31:46 +10005007
Chris Wilsone5281cc2010-10-28 13:45:36 +01005008 drm_clflush_pages(&page, 1);
5009
5010 set_page_dirty(page);
5011 mark_page_accessed(page);
5012 page_cache_release(page);
5013 }
Dave Airlie71acb5e2008-12-30 20:31:46 +10005014 }
Daniel Vetter40ce6572010-11-05 18:12:18 +01005015 intel_gtt_chipset_flush();
Chris Wilsond78b47b2009-06-17 21:52:49 +01005016
Chris Wilson05394f32010-11-08 19:18:58 +00005017 obj->phys_obj->cur_obj = NULL;
5018 obj->phys_obj = NULL;
Dave Airlie71acb5e2008-12-30 20:31:46 +10005019}
5020
5021int
5022i915_gem_attach_phys_object(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00005023 struct drm_i915_gem_object *obj,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01005024 int id,
5025 int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10005026{
Chris Wilson05394f32010-11-08 19:18:58 +00005027 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Dave Airlie71acb5e2008-12-30 20:31:46 +10005028 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie71acb5e2008-12-30 20:31:46 +10005029 int ret = 0;
5030 int page_count;
5031 int i;
5032
5033 if (id > I915_MAX_PHYS_OBJECT)
5034 return -EINVAL;
5035
Chris Wilson05394f32010-11-08 19:18:58 +00005036 if (obj->phys_obj) {
5037 if (obj->phys_obj->id == id)
Dave Airlie71acb5e2008-12-30 20:31:46 +10005038 return 0;
5039 i915_gem_detach_phys_object(dev, obj);
5040 }
5041
Dave Airlie71acb5e2008-12-30 20:31:46 +10005042 /* create a new object */
5043 if (!dev_priv->mm.phys_objs[id - 1]) {
5044 ret = i915_gem_init_phys_object(dev, id,
Chris Wilson05394f32010-11-08 19:18:58 +00005045 obj->base.size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10005046 if (ret) {
Chris Wilson05394f32010-11-08 19:18:58 +00005047 DRM_ERROR("failed to init phys object %d size: %zu\n",
5048 id, obj->base.size);
Chris Wilsone5281cc2010-10-28 13:45:36 +01005049 return ret;
Dave Airlie71acb5e2008-12-30 20:31:46 +10005050 }
5051 }
5052
5053 /* bind to the object */
Chris Wilson05394f32010-11-08 19:18:58 +00005054 obj->phys_obj = dev_priv->mm.phys_objs[id - 1];
5055 obj->phys_obj->cur_obj = obj;
Dave Airlie71acb5e2008-12-30 20:31:46 +10005056
Chris Wilson05394f32010-11-08 19:18:58 +00005057 page_count = obj->base.size / PAGE_SIZE;
Dave Airlie71acb5e2008-12-30 20:31:46 +10005058
5059 for (i = 0; i < page_count; i++) {
Chris Wilsone5281cc2010-10-28 13:45:36 +01005060 struct page *page;
5061 char *dst, *src;
Dave Airlie71acb5e2008-12-30 20:31:46 +10005062
Chris Wilsone5281cc2010-10-28 13:45:36 +01005063 page = read_cache_page_gfp(mapping, i,
5064 GFP_HIGHUSER | __GFP_RECLAIMABLE);
5065 if (IS_ERR(page))
5066 return PTR_ERR(page);
5067
Chris Wilsonff75b9b2010-10-30 22:52:31 +01005068 src = kmap_atomic(page);
Chris Wilson05394f32010-11-08 19:18:58 +00005069 dst = obj->phys_obj->handle->vaddr + (i * PAGE_SIZE);
Dave Airlie71acb5e2008-12-30 20:31:46 +10005070 memcpy(dst, src, PAGE_SIZE);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -07005071 kunmap_atomic(src);
Chris Wilsone5281cc2010-10-28 13:45:36 +01005072
5073 mark_page_accessed(page);
5074 page_cache_release(page);
Dave Airlie71acb5e2008-12-30 20:31:46 +10005075 }
5076
5077 return 0;
Dave Airlie71acb5e2008-12-30 20:31:46 +10005078}
5079
5080static int
Chris Wilson05394f32010-11-08 19:18:58 +00005081i915_gem_phys_pwrite(struct drm_device *dev,
5082 struct drm_i915_gem_object *obj,
Dave Airlie71acb5e2008-12-30 20:31:46 +10005083 struct drm_i915_gem_pwrite *args,
5084 struct drm_file *file_priv)
5085{
Chris Wilson05394f32010-11-08 19:18:58 +00005086 void *vaddr = obj->phys_obj->handle->vaddr + args->offset;
Chris Wilsonb47b30c2010-11-08 01:12:29 +00005087 char __user *user_data = (char __user *) (uintptr_t) args->data_ptr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10005088
Chris Wilsonb47b30c2010-11-08 01:12:29 +00005089 if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) {
5090 unsigned long unwritten;
5091
5092 /* The physical object once assigned is fixed for the lifetime
5093 * of the obj, so we can safely drop the lock and continue
5094 * to access vaddr.
5095 */
5096 mutex_unlock(&dev->struct_mutex);
5097 unwritten = copy_from_user(vaddr, user_data, args->size);
5098 mutex_lock(&dev->struct_mutex);
5099 if (unwritten)
5100 return -EFAULT;
5101 }
Dave Airlie71acb5e2008-12-30 20:31:46 +10005102
Daniel Vetter40ce6572010-11-05 18:12:18 +01005103 intel_gtt_chipset_flush();
Dave Airlie71acb5e2008-12-30 20:31:46 +10005104 return 0;
5105}
Eric Anholtb9624422009-06-03 07:27:35 +00005106
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005107void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00005108{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005109 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00005110
5111 /* Clean up our request list when the client is going away, so that
5112 * later retire_requests won't dereference our soon-to-be-gone
5113 * file_priv.
5114 */
Chris Wilson1c255952010-09-26 11:03:27 +01005115 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005116 while (!list_empty(&file_priv->mm.request_list)) {
5117 struct drm_i915_gem_request *request;
5118
5119 request = list_first_entry(&file_priv->mm.request_list,
5120 struct drm_i915_gem_request,
5121 client_list);
5122 list_del(&request->client_list);
5123 request->file_priv = NULL;
5124 }
Chris Wilson1c255952010-09-26 11:03:27 +01005125 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00005126}
Chris Wilson31169712009-09-14 16:50:28 +01005127
Chris Wilson31169712009-09-14 16:50:28 +01005128static int
Chris Wilson1637ef42010-04-20 17:10:35 +01005129i915_gpu_is_active(struct drm_device *dev)
5130{
5131 drm_i915_private_t *dev_priv = dev->dev_private;
5132 int lists_empty;
5133
Chris Wilson1637ef42010-04-20 17:10:35 +01005134 lists_empty = list_empty(&dev_priv->mm.flushing_list) &&
Chris Wilson17250b72010-10-28 12:51:39 +01005135 list_empty(&dev_priv->mm.active_list);
Chris Wilson1637ef42010-04-20 17:10:35 +01005136
5137 return !lists_empty;
5138}
5139
5140static int
Chris Wilson17250b72010-10-28 12:51:39 +01005141i915_gem_inactive_shrink(struct shrinker *shrinker,
5142 int nr_to_scan,
5143 gfp_t gfp_mask)
Chris Wilson31169712009-09-14 16:50:28 +01005144{
Chris Wilson17250b72010-10-28 12:51:39 +01005145 struct drm_i915_private *dev_priv =
5146 container_of(shrinker,
5147 struct drm_i915_private,
5148 mm.inactive_shrinker);
5149 struct drm_device *dev = dev_priv->dev;
5150 struct drm_i915_gem_object *obj, *next;
5151 int cnt;
5152
5153 if (!mutex_trylock(&dev->struct_mutex))
Chris Wilsonbbe2e112010-10-28 22:35:07 +01005154 return 0;
Chris Wilson31169712009-09-14 16:50:28 +01005155
5156 /* "fast-path" to count number of available objects */
5157 if (nr_to_scan == 0) {
Chris Wilson17250b72010-10-28 12:51:39 +01005158 cnt = 0;
5159 list_for_each_entry(obj,
5160 &dev_priv->mm.inactive_list,
5161 mm_list)
5162 cnt++;
5163 mutex_unlock(&dev->struct_mutex);
5164 return cnt / 100 * sysctl_vfs_cache_pressure;
Chris Wilson31169712009-09-14 16:50:28 +01005165 }
5166
Chris Wilson1637ef42010-04-20 17:10:35 +01005167rescan:
Chris Wilson31169712009-09-14 16:50:28 +01005168 /* first scan for clean buffers */
Chris Wilson17250b72010-10-28 12:51:39 +01005169 i915_gem_retire_requests(dev);
Chris Wilson31169712009-09-14 16:50:28 +01005170
Chris Wilson17250b72010-10-28 12:51:39 +01005171 list_for_each_entry_safe(obj, next,
5172 &dev_priv->mm.inactive_list,
5173 mm_list) {
5174 if (i915_gem_object_is_purgeable(obj)) {
Chris Wilson20217462010-11-23 15:26:33 +00005175 if (i915_gem_object_unbind(obj) == 0 &&
5176 --nr_to_scan == 0)
Chris Wilson17250b72010-10-28 12:51:39 +01005177 break;
Chris Wilson31169712009-09-14 16:50:28 +01005178 }
Chris Wilson31169712009-09-14 16:50:28 +01005179 }
5180
5181 /* second pass, evict/count anything still on the inactive list */
Chris Wilson17250b72010-10-28 12:51:39 +01005182 cnt = 0;
5183 list_for_each_entry_safe(obj, next,
5184 &dev_priv->mm.inactive_list,
5185 mm_list) {
Chris Wilson20217462010-11-23 15:26:33 +00005186 if (nr_to_scan &&
5187 i915_gem_object_unbind(obj) == 0)
Chris Wilson17250b72010-10-28 12:51:39 +01005188 nr_to_scan--;
Chris Wilson20217462010-11-23 15:26:33 +00005189 else
Chris Wilson17250b72010-10-28 12:51:39 +01005190 cnt++;
Chris Wilson31169712009-09-14 16:50:28 +01005191 }
5192
Chris Wilson17250b72010-10-28 12:51:39 +01005193 if (nr_to_scan && i915_gpu_is_active(dev)) {
Chris Wilson1637ef42010-04-20 17:10:35 +01005194 /*
5195 * We are desperate for pages, so as a last resort, wait
5196 * for the GPU to finish and discard whatever we can.
5197 * This has a dramatic impact to reduce the number of
5198 * OOM-killer events whilst running the GPU aggressively.
5199 */
Chris Wilson17250b72010-10-28 12:51:39 +01005200 if (i915_gpu_idle(dev) == 0)
Chris Wilson1637ef42010-04-20 17:10:35 +01005201 goto rescan;
5202 }
Chris Wilson17250b72010-10-28 12:51:39 +01005203 mutex_unlock(&dev->struct_mutex);
5204 return cnt / 100 * sysctl_vfs_cache_pressure;
Chris Wilson31169712009-09-14 16:50:28 +01005205}