blob: a549be5f535bbae674ecfbec996cf6f0fe68100b [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"
Hugh Dickins5949eac2011-06-27 16:18:18 -070034#include <linux/shmem_fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
Eric Anholt673a3942008-07-30 12:06:12 -070036#include <linux/swap.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080037#include <linux/pci.h>
Daniel Vetter1286ff72012-05-10 15:25:09 +020038#include <linux/dma-buf.h>
Eric Anholt673a3942008-07-30 12:06:12 -070039
Chris Wilson05394f32010-11-08 19:18:58 +000040static void i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj);
41static void i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj);
Chris Wilson88241782011-01-07 17:09:48 +000042static __must_check int i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
43 unsigned alignment,
44 bool map_and_fenceable);
Chris Wilson05394f32010-11-08 19:18:58 +000045static int i915_gem_phys_pwrite(struct drm_device *dev,
46 struct drm_i915_gem_object *obj,
Dave Airlie71acb5e2008-12-30 20:31:46 +100047 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +000048 struct drm_file *file);
Eric Anholt673a3942008-07-30 12:06:12 -070049
Chris Wilson61050802012-04-17 15:31:31 +010050static void i915_gem_write_fence(struct drm_device *dev, int reg,
51 struct drm_i915_gem_object *obj);
52static void i915_gem_object_update_fence(struct drm_i915_gem_object *obj,
53 struct drm_i915_fence_reg *fence,
54 bool enable);
55
Chris Wilson17250b72010-10-28 12:51:39 +010056static int i915_gem_inactive_shrink(struct shrinker *shrinker,
Ying Han1495f232011-05-24 17:12:27 -070057 struct shrink_control *sc);
Chris Wilson6c085a72012-08-20 11:40:46 +020058static long i915_gem_purge(struct drm_i915_private *dev_priv, long target);
59static void i915_gem_shrink_all(struct drm_i915_private *dev_priv);
Daniel Vetter8c599672011-12-14 13:57:31 +010060static void i915_gem_object_truncate(struct drm_i915_gem_object *obj);
Chris Wilson31169712009-09-14 16:50:28 +010061
Chris Wilson61050802012-04-17 15:31:31 +010062static inline void i915_gem_object_fence_lost(struct drm_i915_gem_object *obj)
63{
64 if (obj->tiling_mode)
65 i915_gem_release_mmap(obj);
66
67 /* As we do not have an associated fence register, we will force
68 * a tiling change if we ever need to acquire one.
69 */
Chris Wilson5d82e3e2012-04-21 16:23:23 +010070 obj->fence_dirty = false;
Chris Wilson61050802012-04-17 15:31:31 +010071 obj->fence_reg = I915_FENCE_REG_NONE;
72}
73
Chris Wilson73aa8082010-09-30 11:46:12 +010074/* some bookkeeping */
75static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
76 size_t size)
77{
78 dev_priv->mm.object_count++;
79 dev_priv->mm.object_memory += size;
80}
81
82static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
83 size_t size)
84{
85 dev_priv->mm.object_count--;
86 dev_priv->mm.object_memory -= size;
87}
88
Chris Wilson21dd3732011-01-26 15:55:56 +000089static int
90i915_gem_wait_for_error(struct drm_device *dev)
Chris Wilson30dbf0c2010-09-25 10:19:17 +010091{
92 struct drm_i915_private *dev_priv = dev->dev_private;
93 struct completion *x = &dev_priv->error_completion;
94 unsigned long flags;
95 int ret;
96
97 if (!atomic_read(&dev_priv->mm.wedged))
98 return 0;
99
Daniel Vetter0a6759c2012-07-04 22:18:41 +0200100 /*
101 * Only wait 10 seconds for the gpu reset to complete to avoid hanging
102 * userspace. If it takes that long something really bad is going on and
103 * we should simply try to bail out and fail as gracefully as possible.
104 */
105 ret = wait_for_completion_interruptible_timeout(x, 10*HZ);
106 if (ret == 0) {
107 DRM_ERROR("Timed out waiting for the gpu reset to complete\n");
108 return -EIO;
109 } else if (ret < 0) {
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100110 return ret;
Daniel Vetter0a6759c2012-07-04 22:18:41 +0200111 }
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100112
Chris Wilson21dd3732011-01-26 15:55:56 +0000113 if (atomic_read(&dev_priv->mm.wedged)) {
114 /* GPU is hung, bump the completion count to account for
115 * the token we just consumed so that we never hit zero and
116 * end up waiting upon a subsequent completion event that
117 * will never happen.
118 */
119 spin_lock_irqsave(&x->wait.lock, flags);
120 x->done++;
121 spin_unlock_irqrestore(&x->wait.lock, flags);
122 }
123 return 0;
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100124}
125
Chris Wilson54cf91d2010-11-25 18:00:26 +0000126int i915_mutex_lock_interruptible(struct drm_device *dev)
Chris Wilson76c1dec2010-09-25 11:22:51 +0100127{
Chris Wilson76c1dec2010-09-25 11:22:51 +0100128 int ret;
129
Chris Wilson21dd3732011-01-26 15:55:56 +0000130 ret = i915_gem_wait_for_error(dev);
Chris Wilson76c1dec2010-09-25 11:22:51 +0100131 if (ret)
132 return ret;
133
134 ret = mutex_lock_interruptible(&dev->struct_mutex);
135 if (ret)
136 return ret;
137
Chris Wilson23bc5982010-09-29 16:10:57 +0100138 WARN_ON(i915_verify_lists(dev));
Chris Wilson76c1dec2010-09-25 11:22:51 +0100139 return 0;
140}
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100141
Chris Wilson7d1c4802010-08-07 21:45:03 +0100142static inline bool
Chris Wilson05394f32010-11-08 19:18:58 +0000143i915_gem_object_is_inactive(struct drm_i915_gem_object *obj)
Chris Wilson7d1c4802010-08-07 21:45:03 +0100144{
Chris Wilson6c085a72012-08-20 11:40:46 +0200145 return obj->gtt_space && !obj->active;
Chris Wilson7d1c4802010-08-07 21:45:03 +0100146}
147
Eric Anholt673a3942008-07-30 12:06:12 -0700148int
149i915_gem_init_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000150 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700151{
Eric Anholt673a3942008-07-30 12:06:12 -0700152 struct drm_i915_gem_init *args = data;
Chris Wilson20217462010-11-23 15:26:33 +0000153
Daniel Vetter7bb6fb82012-04-24 08:22:52 +0200154 if (drm_core_check_feature(dev, DRIVER_MODESET))
155 return -ENODEV;
156
Chris Wilson20217462010-11-23 15:26:33 +0000157 if (args->gtt_start >= args->gtt_end ||
158 (args->gtt_end | args->gtt_start) & (PAGE_SIZE - 1))
159 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -0700160
Daniel Vetterf534bc02012-03-26 22:37:04 +0200161 /* GEM with user mode setting was never supported on ilk and later. */
162 if (INTEL_INFO(dev)->gen >= 5)
163 return -ENODEV;
164
Eric Anholt673a3942008-07-30 12:06:12 -0700165 mutex_lock(&dev->struct_mutex);
Daniel Vetter644ec022012-03-26 09:45:40 +0200166 i915_gem_init_global_gtt(dev, args->gtt_start,
167 args->gtt_end, args->gtt_end);
Eric Anholt673a3942008-07-30 12:06:12 -0700168 mutex_unlock(&dev->struct_mutex);
169
Chris Wilson20217462010-11-23 15:26:33 +0000170 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700171}
172
Eric Anholt5a125c32008-10-22 21:40:13 -0700173int
174i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000175 struct drm_file *file)
Eric Anholt5a125c32008-10-22 21:40:13 -0700176{
Chris Wilson73aa8082010-09-30 11:46:12 +0100177 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt5a125c32008-10-22 21:40:13 -0700178 struct drm_i915_gem_get_aperture *args = data;
Chris Wilson6299f992010-11-24 12:23:44 +0000179 struct drm_i915_gem_object *obj;
180 size_t pinned;
Eric Anholt5a125c32008-10-22 21:40:13 -0700181
Chris Wilson6299f992010-11-24 12:23:44 +0000182 pinned = 0;
Chris Wilson73aa8082010-09-30 11:46:12 +0100183 mutex_lock(&dev->struct_mutex);
Chris Wilson6c085a72012-08-20 11:40:46 +0200184 list_for_each_entry(obj, &dev_priv->mm.bound_list, gtt_list)
Chris Wilson1b502472012-04-24 15:47:30 +0100185 if (obj->pin_count)
186 pinned += obj->gtt_space->size;
Chris Wilson73aa8082010-09-30 11:46:12 +0100187 mutex_unlock(&dev->struct_mutex);
Eric Anholt5a125c32008-10-22 21:40:13 -0700188
Chris Wilson6299f992010-11-24 12:23:44 +0000189 args->aper_size = dev_priv->mm.gtt_total;
Akshay Joshi0206e352011-08-16 15:34:10 -0400190 args->aper_available_size = args->aper_size - pinned;
Chris Wilson6299f992010-11-24 12:23:44 +0000191
Eric Anholt5a125c32008-10-22 21:40:13 -0700192 return 0;
193}
194
Dave Airlieff72145b2011-02-07 12:16:14 +1000195static int
196i915_gem_create(struct drm_file *file,
197 struct drm_device *dev,
198 uint64_t size,
199 uint32_t *handle_p)
Eric Anholt673a3942008-07-30 12:06:12 -0700200{
Chris Wilson05394f32010-11-08 19:18:58 +0000201 struct drm_i915_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300202 int ret;
203 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700204
Dave Airlieff72145b2011-02-07 12:16:14 +1000205 size = roundup(size, PAGE_SIZE);
Chris Wilson8ffc0242011-09-14 14:14:28 +0200206 if (size == 0)
207 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -0700208
209 /* Allocate the new object */
Dave Airlieff72145b2011-02-07 12:16:14 +1000210 obj = i915_gem_alloc_object(dev, size);
Eric Anholt673a3942008-07-30 12:06:12 -0700211 if (obj == NULL)
212 return -ENOMEM;
213
Chris Wilson05394f32010-11-08 19:18:58 +0000214 ret = drm_gem_handle_create(file, &obj->base, &handle);
Chris Wilson1dfd9752010-09-06 14:44:14 +0100215 if (ret) {
Chris Wilson05394f32010-11-08 19:18:58 +0000216 drm_gem_object_release(&obj->base);
217 i915_gem_info_remove_obj(dev->dev_private, obj->base.size);
Chris Wilson202f2fe2010-10-14 13:20:40 +0100218 kfree(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700219 return ret;
Chris Wilson1dfd9752010-09-06 14:44:14 +0100220 }
221
Chris Wilson202f2fe2010-10-14 13:20:40 +0100222 /* drop reference from allocate - handle holds it now */
Chris Wilson05394f32010-11-08 19:18:58 +0000223 drm_gem_object_unreference(&obj->base);
Chris Wilson202f2fe2010-10-14 13:20:40 +0100224 trace_i915_gem_object_create(obj);
225
Dave Airlieff72145b2011-02-07 12:16:14 +1000226 *handle_p = handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700227 return 0;
228}
229
Dave Airlieff72145b2011-02-07 12:16:14 +1000230int
231i915_gem_dumb_create(struct drm_file *file,
232 struct drm_device *dev,
233 struct drm_mode_create_dumb *args)
234{
235 /* have to work out size/pitch and return them */
Chris Wilsoned0291f2011-03-19 08:21:45 +0000236 args->pitch = ALIGN(args->width * ((args->bpp + 7) / 8), 64);
Dave Airlieff72145b2011-02-07 12:16:14 +1000237 args->size = args->pitch * args->height;
238 return i915_gem_create(file, dev,
239 args->size, &args->handle);
240}
241
242int i915_gem_dumb_destroy(struct drm_file *file,
243 struct drm_device *dev,
244 uint32_t handle)
245{
246 return drm_gem_handle_delete(file, handle);
247}
248
249/**
250 * Creates a new mm object and returns a handle to it.
251 */
252int
253i915_gem_create_ioctl(struct drm_device *dev, void *data,
254 struct drm_file *file)
255{
256 struct drm_i915_gem_create *args = data;
Daniel Vetter63ed2cb2012-04-23 16:50:50 +0200257
Dave Airlieff72145b2011-02-07 12:16:14 +1000258 return i915_gem_create(file, dev,
259 args->size, &args->handle);
260}
261
Chris Wilson05394f32010-11-08 19:18:58 +0000262static int i915_gem_object_needs_bit17_swizzle(struct drm_i915_gem_object *obj)
Eric Anholt280b7132009-03-12 16:56:27 -0700263{
Chris Wilson05394f32010-11-08 19:18:58 +0000264 drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
Eric Anholt280b7132009-03-12 16:56:27 -0700265
266 return dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_9_10_17 &&
Chris Wilson05394f32010-11-08 19:18:58 +0000267 obj->tiling_mode != I915_TILING_NONE;
Eric Anholt280b7132009-03-12 16:56:27 -0700268}
269
Daniel Vetter8c599672011-12-14 13:57:31 +0100270static inline int
Daniel Vetter8461d222011-12-14 13:57:32 +0100271__copy_to_user_swizzled(char __user *cpu_vaddr,
272 const char *gpu_vaddr, int gpu_offset,
273 int length)
274{
275 int ret, cpu_offset = 0;
276
277 while (length > 0) {
278 int cacheline_end = ALIGN(gpu_offset + 1, 64);
279 int this_length = min(cacheline_end - gpu_offset, length);
280 int swizzled_gpu_offset = gpu_offset ^ 64;
281
282 ret = __copy_to_user(cpu_vaddr + cpu_offset,
283 gpu_vaddr + swizzled_gpu_offset,
284 this_length);
285 if (ret)
286 return ret + length;
287
288 cpu_offset += this_length;
289 gpu_offset += this_length;
290 length -= this_length;
291 }
292
293 return 0;
294}
295
296static inline int
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -0700297__copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset,
298 const char __user *cpu_vaddr,
Daniel Vetter8c599672011-12-14 13:57:31 +0100299 int length)
300{
301 int ret, cpu_offset = 0;
302
303 while (length > 0) {
304 int cacheline_end = ALIGN(gpu_offset + 1, 64);
305 int this_length = min(cacheline_end - gpu_offset, length);
306 int swizzled_gpu_offset = gpu_offset ^ 64;
307
308 ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset,
309 cpu_vaddr + cpu_offset,
310 this_length);
311 if (ret)
312 return ret + length;
313
314 cpu_offset += this_length;
315 gpu_offset += this_length;
316 length -= this_length;
317 }
318
319 return 0;
320}
321
Daniel Vetterd174bd62012-03-25 19:47:40 +0200322/* Per-page copy function for the shmem pread fastpath.
323 * Flushes invalid cachelines before reading the target if
324 * needs_clflush is set. */
Eric Anholteb014592009-03-10 11:44:52 -0700325static int
Daniel Vetterd174bd62012-03-25 19:47:40 +0200326shmem_pread_fast(struct page *page, int shmem_page_offset, int page_length,
327 char __user *user_data,
328 bool page_do_bit17_swizzling, bool needs_clflush)
329{
330 char *vaddr;
331 int ret;
332
Daniel Vettere7e58eb2012-03-25 19:47:43 +0200333 if (unlikely(page_do_bit17_swizzling))
Daniel Vetterd174bd62012-03-25 19:47:40 +0200334 return -EINVAL;
335
336 vaddr = kmap_atomic(page);
337 if (needs_clflush)
338 drm_clflush_virt_range(vaddr + shmem_page_offset,
339 page_length);
340 ret = __copy_to_user_inatomic(user_data,
341 vaddr + shmem_page_offset,
342 page_length);
343 kunmap_atomic(vaddr);
344
345 return ret;
346}
347
Daniel Vetter23c18c72012-03-25 19:47:42 +0200348static void
349shmem_clflush_swizzled_range(char *addr, unsigned long length,
350 bool swizzled)
351{
Daniel Vettere7e58eb2012-03-25 19:47:43 +0200352 if (unlikely(swizzled)) {
Daniel Vetter23c18c72012-03-25 19:47:42 +0200353 unsigned long start = (unsigned long) addr;
354 unsigned long end = (unsigned long) addr + length;
355
356 /* For swizzling simply ensure that we always flush both
357 * channels. Lame, but simple and it works. Swizzled
358 * pwrite/pread is far from a hotpath - current userspace
359 * doesn't use it at all. */
360 start = round_down(start, 128);
361 end = round_up(end, 128);
362
363 drm_clflush_virt_range((void *)start, end - start);
364 } else {
365 drm_clflush_virt_range(addr, length);
366 }
367
368}
369
Daniel Vetterd174bd62012-03-25 19:47:40 +0200370/* Only difference to the fast-path function is that this can handle bit17
371 * and uses non-atomic copy and kmap functions. */
372static int
373shmem_pread_slow(struct page *page, int shmem_page_offset, int page_length,
374 char __user *user_data,
375 bool page_do_bit17_swizzling, bool needs_clflush)
376{
377 char *vaddr;
378 int ret;
379
380 vaddr = kmap(page);
381 if (needs_clflush)
Daniel Vetter23c18c72012-03-25 19:47:42 +0200382 shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
383 page_length,
384 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200385
386 if (page_do_bit17_swizzling)
387 ret = __copy_to_user_swizzled(user_data,
388 vaddr, shmem_page_offset,
389 page_length);
390 else
391 ret = __copy_to_user(user_data,
392 vaddr + shmem_page_offset,
393 page_length);
394 kunmap(page);
395
396 return ret;
397}
398
Eric Anholteb014592009-03-10 11:44:52 -0700399static int
Daniel Vetterdbf7bff2012-03-25 19:47:29 +0200400i915_gem_shmem_pread(struct drm_device *dev,
401 struct drm_i915_gem_object *obj,
402 struct drm_i915_gem_pread *args,
403 struct drm_file *file)
Eric Anholteb014592009-03-10 11:44:52 -0700404{
Chris Wilson05394f32010-11-08 19:18:58 +0000405 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Daniel Vetter8461d222011-12-14 13:57:32 +0100406 char __user *user_data;
Eric Anholteb014592009-03-10 11:44:52 -0700407 ssize_t remain;
Daniel Vetter8461d222011-12-14 13:57:32 +0100408 loff_t offset;
Ben Widawskyeb2c0c82012-02-15 14:42:43 +0100409 int shmem_page_offset, page_length, ret = 0;
Daniel Vetter8461d222011-12-14 13:57:32 +0100410 int obj_do_bit17_swizzling, page_do_bit17_swizzling;
Daniel Vetterdbf7bff2012-03-25 19:47:29 +0200411 int hit_slowpath = 0;
Daniel Vetter96d79b52012-03-25 19:47:36 +0200412 int prefaulted = 0;
Daniel Vetter84897312012-03-25 19:47:31 +0200413 int needs_clflush = 0;
Daniel Vetter692a5762012-03-25 19:47:34 +0200414 int release_page;
Eric Anholteb014592009-03-10 11:44:52 -0700415
Daniel Vetter8461d222011-12-14 13:57:32 +0100416 user_data = (char __user *) (uintptr_t) args->data_ptr;
Eric Anholteb014592009-03-10 11:44:52 -0700417 remain = args->size;
418
Daniel Vetter8461d222011-12-14 13:57:32 +0100419 obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700420
Daniel Vetter84897312012-03-25 19:47:31 +0200421 if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU)) {
422 /* If we're not in the cpu read domain, set ourself into the gtt
423 * read domain and manually flush cachelines (if required). This
424 * optimizes for the case when the gpu will dirty the data
425 * anyway again before the next pread happens. */
426 if (obj->cache_level == I915_CACHE_NONE)
427 needs_clflush = 1;
Chris Wilson6c085a72012-08-20 11:40:46 +0200428 if (obj->gtt_space) {
429 ret = i915_gem_object_set_to_gtt_domain(obj, false);
430 if (ret)
431 return ret;
432 }
Daniel Vetter84897312012-03-25 19:47:31 +0200433 }
Eric Anholteb014592009-03-10 11:44:52 -0700434
Eric Anholteb014592009-03-10 11:44:52 -0700435 offset = args->offset;
Daniel Vetter8461d222011-12-14 13:57:32 +0100436
Eric Anholteb014592009-03-10 11:44:52 -0700437 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100438 struct page *page;
439
Eric Anholteb014592009-03-10 11:44:52 -0700440 /* Operation in this page
441 *
Eric Anholteb014592009-03-10 11:44:52 -0700442 * shmem_page_offset = offset within page in shmem file
Eric Anholteb014592009-03-10 11:44:52 -0700443 * page_length = bytes to copy for this page
444 */
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100445 shmem_page_offset = offset_in_page(offset);
Eric Anholteb014592009-03-10 11:44:52 -0700446 page_length = remain;
447 if ((shmem_page_offset + page_length) > PAGE_SIZE)
448 page_length = PAGE_SIZE - shmem_page_offset;
Eric Anholteb014592009-03-10 11:44:52 -0700449
Daniel Vetter692a5762012-03-25 19:47:34 +0200450 if (obj->pages) {
451 page = obj->pages[offset >> PAGE_SHIFT];
452 release_page = 0;
453 } else {
454 page = shmem_read_mapping_page(mapping, offset >> PAGE_SHIFT);
455 if (IS_ERR(page)) {
456 ret = PTR_ERR(page);
457 goto out;
458 }
459 release_page = 1;
Jesper Juhlb65552f2011-06-12 20:53:44 +0000460 }
Chris Wilsone5281cc2010-10-28 13:45:36 +0100461
Daniel Vetter8461d222011-12-14 13:57:32 +0100462 page_do_bit17_swizzling = obj_do_bit17_swizzling &&
463 (page_to_phys(page) & (1 << 17)) != 0;
464
Daniel Vetterd174bd62012-03-25 19:47:40 +0200465 ret = shmem_pread_fast(page, shmem_page_offset, page_length,
466 user_data, page_do_bit17_swizzling,
467 needs_clflush);
468 if (ret == 0)
469 goto next_page;
Eric Anholteb014592009-03-10 11:44:52 -0700470
Daniel Vetterdbf7bff2012-03-25 19:47:29 +0200471 hit_slowpath = 1;
Daniel Vetter692a5762012-03-25 19:47:34 +0200472 page_cache_get(page);
Daniel Vetterdbf7bff2012-03-25 19:47:29 +0200473 mutex_unlock(&dev->struct_mutex);
474
Daniel Vetter96d79b52012-03-25 19:47:36 +0200475 if (!prefaulted) {
Daniel Vetterf56f8212012-03-25 19:47:41 +0200476 ret = fault_in_multipages_writeable(user_data, remain);
Daniel Vetter96d79b52012-03-25 19:47:36 +0200477 /* Userspace is tricking us, but we've already clobbered
478 * its pages with the prefault and promised to write the
479 * data up to the first fault. Hence ignore any errors
480 * and just continue. */
481 (void)ret;
482 prefaulted = 1;
483 }
484
Daniel Vetterd174bd62012-03-25 19:47:40 +0200485 ret = shmem_pread_slow(page, shmem_page_offset, page_length,
486 user_data, page_do_bit17_swizzling,
487 needs_clflush);
Eric Anholteb014592009-03-10 11:44:52 -0700488
Daniel Vetterdbf7bff2012-03-25 19:47:29 +0200489 mutex_lock(&dev->struct_mutex);
Chris Wilsone5281cc2010-10-28 13:45:36 +0100490 page_cache_release(page);
Daniel Vetterdbf7bff2012-03-25 19:47:29 +0200491next_page:
Chris Wilsone5281cc2010-10-28 13:45:36 +0100492 mark_page_accessed(page);
Daniel Vetter692a5762012-03-25 19:47:34 +0200493 if (release_page)
494 page_cache_release(page);
Chris Wilsone5281cc2010-10-28 13:45:36 +0100495
Daniel Vetter8461d222011-12-14 13:57:32 +0100496 if (ret) {
497 ret = -EFAULT;
498 goto out;
499 }
500
Eric Anholteb014592009-03-10 11:44:52 -0700501 remain -= page_length;
Daniel Vetter8461d222011-12-14 13:57:32 +0100502 user_data += page_length;
Eric Anholteb014592009-03-10 11:44:52 -0700503 offset += page_length;
504 }
505
Chris Wilson4f27b752010-10-14 15:26:45 +0100506out:
Daniel Vetterdbf7bff2012-03-25 19:47:29 +0200507 if (hit_slowpath) {
508 /* Fixup: Kill any reinstated backing storage pages */
509 if (obj->madv == __I915_MADV_PURGED)
510 i915_gem_object_truncate(obj);
511 }
Eric Anholteb014592009-03-10 11:44:52 -0700512
513 return ret;
514}
515
Eric Anholt673a3942008-07-30 12:06:12 -0700516/**
517 * Reads data from the object referenced by handle.
518 *
519 * On error, the contents of *data are undefined.
520 */
521int
522i915_gem_pread_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000523 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700524{
525 struct drm_i915_gem_pread *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +0000526 struct drm_i915_gem_object *obj;
Chris Wilson35b62a82010-09-26 20:23:38 +0100527 int ret = 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700528
Chris Wilson51311d02010-11-17 09:10:42 +0000529 if (args->size == 0)
530 return 0;
531
532 if (!access_ok(VERIFY_WRITE,
533 (char __user *)(uintptr_t)args->data_ptr,
534 args->size))
535 return -EFAULT;
536
Chris Wilson4f27b752010-10-14 15:26:45 +0100537 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100538 if (ret)
Chris Wilson4f27b752010-10-14 15:26:45 +0100539 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700540
Chris Wilson05394f32010-11-08 19:18:58 +0000541 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +0000542 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100543 ret = -ENOENT;
544 goto unlock;
Chris Wilson4f27b752010-10-14 15:26:45 +0100545 }
Eric Anholt673a3942008-07-30 12:06:12 -0700546
Chris Wilson7dcd2492010-09-26 20:21:44 +0100547 /* Bounds check source. */
Chris Wilson05394f32010-11-08 19:18:58 +0000548 if (args->offset > obj->base.size ||
549 args->size > obj->base.size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +0100550 ret = -EINVAL;
Chris Wilson35b62a82010-09-26 20:23:38 +0100551 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +0100552 }
553
Daniel Vetter1286ff72012-05-10 15:25:09 +0200554 /* prime objects have no backing filp to GEM pread/pwrite
555 * pages from.
556 */
557 if (!obj->base.filp) {
558 ret = -EINVAL;
559 goto out;
560 }
561
Chris Wilsondb53a302011-02-03 11:57:46 +0000562 trace_i915_gem_object_pread(obj, args->offset, args->size);
563
Daniel Vetterdbf7bff2012-03-25 19:47:29 +0200564 ret = i915_gem_shmem_pread(dev, obj, args, file);
Eric Anholt673a3942008-07-30 12:06:12 -0700565
Chris Wilson35b62a82010-09-26 20:23:38 +0100566out:
Chris Wilson05394f32010-11-08 19:18:58 +0000567 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100568unlock:
Chris Wilson4f27b752010-10-14 15:26:45 +0100569 mutex_unlock(&dev->struct_mutex);
Eric Anholteb014592009-03-10 11:44:52 -0700570 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700571}
572
Keith Packard0839ccb2008-10-30 19:38:48 -0700573/* This is the fast write path which cannot handle
574 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700575 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700576
Keith Packard0839ccb2008-10-30 19:38:48 -0700577static inline int
578fast_user_write(struct io_mapping *mapping,
579 loff_t page_base, int page_offset,
580 char __user *user_data,
581 int length)
582{
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -0700583 void __iomem *vaddr_atomic;
584 void *vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -0700585 unsigned long unwritten;
586
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700587 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -0700588 /* We can use the cpu mem copy function because this is X86. */
589 vaddr = (void __force*)vaddr_atomic + page_offset;
590 unwritten = __copy_from_user_inatomic_nocache(vaddr,
Keith Packard0839ccb2008-10-30 19:38:48 -0700591 user_data, length);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700592 io_mapping_unmap_atomic(vaddr_atomic);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100593 return unwritten;
Keith Packard0839ccb2008-10-30 19:38:48 -0700594}
595
Eric Anholt3de09aa2009-03-09 09:42:23 -0700596/**
597 * This is the fast pwrite path, where we copy the data directly from the
598 * user into the GTT, uncached.
599 */
Eric Anholt673a3942008-07-30 12:06:12 -0700600static int
Chris Wilson05394f32010-11-08 19:18:58 +0000601i915_gem_gtt_pwrite_fast(struct drm_device *dev,
602 struct drm_i915_gem_object *obj,
Eric Anholt3de09aa2009-03-09 09:42:23 -0700603 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000604 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700605{
Keith Packard0839ccb2008-10-30 19:38:48 -0700606 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700607 ssize_t remain;
Keith Packard0839ccb2008-10-30 19:38:48 -0700608 loff_t offset, page_base;
Eric Anholt673a3942008-07-30 12:06:12 -0700609 char __user *user_data;
Daniel Vetter935aaa62012-03-25 19:47:35 +0200610 int page_offset, page_length, ret;
611
612 ret = i915_gem_object_pin(obj, 0, true);
613 if (ret)
614 goto out;
615
616 ret = i915_gem_object_set_to_gtt_domain(obj, true);
617 if (ret)
618 goto out_unpin;
619
620 ret = i915_gem_object_put_fence(obj);
621 if (ret)
622 goto out_unpin;
Eric Anholt673a3942008-07-30 12:06:12 -0700623
624 user_data = (char __user *) (uintptr_t) args->data_ptr;
625 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700626
Chris Wilson05394f32010-11-08 19:18:58 +0000627 offset = obj->gtt_offset + args->offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700628
629 while (remain > 0) {
630 /* Operation in this page
631 *
Keith Packard0839ccb2008-10-30 19:38:48 -0700632 * page_base = page offset within aperture
633 * page_offset = offset within page
634 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -0700635 */
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100636 page_base = offset & PAGE_MASK;
637 page_offset = offset_in_page(offset);
Keith Packard0839ccb2008-10-30 19:38:48 -0700638 page_length = remain;
639 if ((page_offset + remain) > PAGE_SIZE)
640 page_length = PAGE_SIZE - page_offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700641
Keith Packard0839ccb2008-10-30 19:38:48 -0700642 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -0700643 * source page isn't available. Return the error and we'll
644 * retry in the slow path.
Keith Packard0839ccb2008-10-30 19:38:48 -0700645 */
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100646 if (fast_user_write(dev_priv->mm.gtt_mapping, page_base,
Daniel Vetter935aaa62012-03-25 19:47:35 +0200647 page_offset, user_data, page_length)) {
648 ret = -EFAULT;
649 goto out_unpin;
650 }
Eric Anholt673a3942008-07-30 12:06:12 -0700651
Keith Packard0839ccb2008-10-30 19:38:48 -0700652 remain -= page_length;
653 user_data += page_length;
654 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700655 }
Eric Anholt673a3942008-07-30 12:06:12 -0700656
Daniel Vetter935aaa62012-03-25 19:47:35 +0200657out_unpin:
658 i915_gem_object_unpin(obj);
659out:
Eric Anholt3de09aa2009-03-09 09:42:23 -0700660 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700661}
662
Daniel Vetterd174bd62012-03-25 19:47:40 +0200663/* Per-page copy function for the shmem pwrite fastpath.
664 * Flushes invalid cachelines before writing to the target if
665 * needs_clflush_before is set and flushes out any written cachelines after
666 * writing if needs_clflush is set. */
Eric Anholt673a3942008-07-30 12:06:12 -0700667static int
Daniel Vetterd174bd62012-03-25 19:47:40 +0200668shmem_pwrite_fast(struct page *page, int shmem_page_offset, int page_length,
669 char __user *user_data,
670 bool page_do_bit17_swizzling,
671 bool needs_clflush_before,
672 bool needs_clflush_after)
Eric Anholt673a3942008-07-30 12:06:12 -0700673{
Daniel Vetterd174bd62012-03-25 19:47:40 +0200674 char *vaddr;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700675 int ret;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700676
Daniel Vettere7e58eb2012-03-25 19:47:43 +0200677 if (unlikely(page_do_bit17_swizzling))
Daniel Vetterd174bd62012-03-25 19:47:40 +0200678 return -EINVAL;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700679
Daniel Vetterd174bd62012-03-25 19:47:40 +0200680 vaddr = kmap_atomic(page);
681 if (needs_clflush_before)
682 drm_clflush_virt_range(vaddr + shmem_page_offset,
683 page_length);
684 ret = __copy_from_user_inatomic_nocache(vaddr + shmem_page_offset,
685 user_data,
686 page_length);
687 if (needs_clflush_after)
688 drm_clflush_virt_range(vaddr + shmem_page_offset,
689 page_length);
690 kunmap_atomic(vaddr);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700691
692 return ret;
693}
694
Daniel Vetterd174bd62012-03-25 19:47:40 +0200695/* Only difference to the fast-path function is that this can handle bit17
696 * and uses non-atomic copy and kmap functions. */
Eric Anholt3043c602008-10-02 12:24:47 -0700697static int
Daniel Vetterd174bd62012-03-25 19:47:40 +0200698shmem_pwrite_slow(struct page *page, int shmem_page_offset, int page_length,
699 char __user *user_data,
700 bool page_do_bit17_swizzling,
701 bool needs_clflush_before,
702 bool needs_clflush_after)
Eric Anholt673a3942008-07-30 12:06:12 -0700703{
Daniel Vetterd174bd62012-03-25 19:47:40 +0200704 char *vaddr;
705 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -0700706
Daniel Vetterd174bd62012-03-25 19:47:40 +0200707 vaddr = kmap(page);
Daniel Vettere7e58eb2012-03-25 19:47:43 +0200708 if (unlikely(needs_clflush_before || page_do_bit17_swizzling))
Daniel Vetter23c18c72012-03-25 19:47:42 +0200709 shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
710 page_length,
711 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200712 if (page_do_bit17_swizzling)
713 ret = __copy_from_user_swizzled(vaddr, shmem_page_offset,
Chris Wilsone5281cc2010-10-28 13:45:36 +0100714 user_data,
715 page_length);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200716 else
717 ret = __copy_from_user(vaddr + shmem_page_offset,
718 user_data,
719 page_length);
720 if (needs_clflush_after)
Daniel Vetter23c18c72012-03-25 19:47:42 +0200721 shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
722 page_length,
723 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200724 kunmap(page);
Chris Wilsone5281cc2010-10-28 13:45:36 +0100725
Daniel Vetterd174bd62012-03-25 19:47:40 +0200726 return ret;
Eric Anholt40123c12009-03-09 13:42:30 -0700727}
728
Eric Anholt40123c12009-03-09 13:42:30 -0700729static int
Daniel Vettere244a442012-03-25 19:47:28 +0200730i915_gem_shmem_pwrite(struct drm_device *dev,
731 struct drm_i915_gem_object *obj,
732 struct drm_i915_gem_pwrite *args,
733 struct drm_file *file)
Eric Anholt40123c12009-03-09 13:42:30 -0700734{
Chris Wilson05394f32010-11-08 19:18:58 +0000735 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Eric Anholt40123c12009-03-09 13:42:30 -0700736 ssize_t remain;
Daniel Vetter8c599672011-12-14 13:57:31 +0100737 loff_t offset;
738 char __user *user_data;
Ben Widawskyeb2c0c82012-02-15 14:42:43 +0100739 int shmem_page_offset, page_length, ret = 0;
Daniel Vetter8c599672011-12-14 13:57:31 +0100740 int obj_do_bit17_swizzling, page_do_bit17_swizzling;
Daniel Vettere244a442012-03-25 19:47:28 +0200741 int hit_slowpath = 0;
Daniel Vetter58642882012-03-25 19:47:37 +0200742 int needs_clflush_after = 0;
743 int needs_clflush_before = 0;
Daniel Vetter692a5762012-03-25 19:47:34 +0200744 int release_page;
Eric Anholt40123c12009-03-09 13:42:30 -0700745
Daniel Vetter8c599672011-12-14 13:57:31 +0100746 user_data = (char __user *) (uintptr_t) args->data_ptr;
Eric Anholt40123c12009-03-09 13:42:30 -0700747 remain = args->size;
748
Daniel Vetter8c599672011-12-14 13:57:31 +0100749 obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700750
Daniel Vetter58642882012-03-25 19:47:37 +0200751 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
752 /* If we're not in the cpu write domain, set ourself into the gtt
753 * write domain and manually flush cachelines (if required). This
754 * optimizes for the case when the gpu will use the data
755 * right away and we therefore have to clflush anyway. */
756 if (obj->cache_level == I915_CACHE_NONE)
757 needs_clflush_after = 1;
Chris Wilson6c085a72012-08-20 11:40:46 +0200758 if (obj->gtt_space) {
759 ret = i915_gem_object_set_to_gtt_domain(obj, true);
760 if (ret)
761 return ret;
762 }
Daniel Vetter58642882012-03-25 19:47:37 +0200763 }
764 /* Same trick applies for invalidate partially written cachelines before
765 * writing. */
766 if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU)
767 && obj->cache_level == I915_CACHE_NONE)
768 needs_clflush_before = 1;
769
Eric Anholt40123c12009-03-09 13:42:30 -0700770 offset = args->offset;
Chris Wilson05394f32010-11-08 19:18:58 +0000771 obj->dirty = 1;
Eric Anholt40123c12009-03-09 13:42:30 -0700772
773 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100774 struct page *page;
Daniel Vetter58642882012-03-25 19:47:37 +0200775 int partial_cacheline_write;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100776
Eric Anholt40123c12009-03-09 13:42:30 -0700777 /* Operation in this page
778 *
Eric Anholt40123c12009-03-09 13:42:30 -0700779 * shmem_page_offset = offset within page in shmem file
Eric Anholt40123c12009-03-09 13:42:30 -0700780 * page_length = bytes to copy for this page
781 */
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100782 shmem_page_offset = offset_in_page(offset);
Eric Anholt40123c12009-03-09 13:42:30 -0700783
784 page_length = remain;
785 if ((shmem_page_offset + page_length) > PAGE_SIZE)
786 page_length = PAGE_SIZE - shmem_page_offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700787
Daniel Vetter58642882012-03-25 19:47:37 +0200788 /* If we don't overwrite a cacheline completely we need to be
789 * careful to have up-to-date data by first clflushing. Don't
790 * overcomplicate things and flush the entire patch. */
791 partial_cacheline_write = needs_clflush_before &&
792 ((shmem_page_offset | page_length)
793 & (boot_cpu_data.x86_clflush_size - 1));
794
Daniel Vetter692a5762012-03-25 19:47:34 +0200795 if (obj->pages) {
796 page = obj->pages[offset >> PAGE_SHIFT];
797 release_page = 0;
798 } else {
799 page = shmem_read_mapping_page(mapping, offset >> PAGE_SHIFT);
800 if (IS_ERR(page)) {
801 ret = PTR_ERR(page);
802 goto out;
803 }
804 release_page = 1;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100805 }
806
Daniel Vetter8c599672011-12-14 13:57:31 +0100807 page_do_bit17_swizzling = obj_do_bit17_swizzling &&
808 (page_to_phys(page) & (1 << 17)) != 0;
809
Daniel Vetterd174bd62012-03-25 19:47:40 +0200810 ret = shmem_pwrite_fast(page, shmem_page_offset, page_length,
811 user_data, page_do_bit17_swizzling,
812 partial_cacheline_write,
813 needs_clflush_after);
814 if (ret == 0)
815 goto next_page;
Eric Anholt40123c12009-03-09 13:42:30 -0700816
Daniel Vettere244a442012-03-25 19:47:28 +0200817 hit_slowpath = 1;
Daniel Vetter692a5762012-03-25 19:47:34 +0200818 page_cache_get(page);
Daniel Vettere244a442012-03-25 19:47:28 +0200819 mutex_unlock(&dev->struct_mutex);
820
Daniel Vetterd174bd62012-03-25 19:47:40 +0200821 ret = shmem_pwrite_slow(page, shmem_page_offset, page_length,
822 user_data, page_do_bit17_swizzling,
823 partial_cacheline_write,
824 needs_clflush_after);
Eric Anholt40123c12009-03-09 13:42:30 -0700825
Daniel Vettere244a442012-03-25 19:47:28 +0200826 mutex_lock(&dev->struct_mutex);
Daniel Vetter692a5762012-03-25 19:47:34 +0200827 page_cache_release(page);
Daniel Vettere244a442012-03-25 19:47:28 +0200828next_page:
Chris Wilsone5281cc2010-10-28 13:45:36 +0100829 set_page_dirty(page);
830 mark_page_accessed(page);
Daniel Vetter692a5762012-03-25 19:47:34 +0200831 if (release_page)
832 page_cache_release(page);
Chris Wilsone5281cc2010-10-28 13:45:36 +0100833
Daniel Vetter8c599672011-12-14 13:57:31 +0100834 if (ret) {
835 ret = -EFAULT;
836 goto out;
837 }
838
Eric Anholt40123c12009-03-09 13:42:30 -0700839 remain -= page_length;
Daniel Vetter8c599672011-12-14 13:57:31 +0100840 user_data += page_length;
Eric Anholt40123c12009-03-09 13:42:30 -0700841 offset += page_length;
842 }
843
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100844out:
Daniel Vettere244a442012-03-25 19:47:28 +0200845 if (hit_slowpath) {
846 /* Fixup: Kill any reinstated backing storage pages */
847 if (obj->madv == __I915_MADV_PURGED)
848 i915_gem_object_truncate(obj);
849 /* and flush dirty cachelines in case the object isn't in the cpu write
850 * domain anymore. */
851 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
852 i915_gem_clflush_object(obj);
853 intel_gtt_chipset_flush();
854 }
Daniel Vetter8c599672011-12-14 13:57:31 +0100855 }
Eric Anholt40123c12009-03-09 13:42:30 -0700856
Daniel Vetter58642882012-03-25 19:47:37 +0200857 if (needs_clflush_after)
858 intel_gtt_chipset_flush();
859
Eric Anholt40123c12009-03-09 13:42:30 -0700860 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700861}
862
863/**
864 * Writes data to the object referenced by handle.
865 *
866 * On error, the contents of the buffer that were to be modified are undefined.
867 */
868int
869i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100870 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700871{
872 struct drm_i915_gem_pwrite *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +0000873 struct drm_i915_gem_object *obj;
Chris Wilson51311d02010-11-17 09:10:42 +0000874 int ret;
875
876 if (args->size == 0)
877 return 0;
878
879 if (!access_ok(VERIFY_READ,
880 (char __user *)(uintptr_t)args->data_ptr,
881 args->size))
882 return -EFAULT;
883
Daniel Vetterf56f8212012-03-25 19:47:41 +0200884 ret = fault_in_multipages_readable((char __user *)(uintptr_t)args->data_ptr,
885 args->size);
Chris Wilson51311d02010-11-17 09:10:42 +0000886 if (ret)
887 return -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -0700888
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100889 ret = i915_mutex_lock_interruptible(dev);
890 if (ret)
891 return ret;
892
Chris Wilson05394f32010-11-08 19:18:58 +0000893 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +0000894 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100895 ret = -ENOENT;
896 goto unlock;
897 }
Eric Anholt673a3942008-07-30 12:06:12 -0700898
Chris Wilson7dcd2492010-09-26 20:21:44 +0100899 /* Bounds check destination. */
Chris Wilson05394f32010-11-08 19:18:58 +0000900 if (args->offset > obj->base.size ||
901 args->size > obj->base.size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +0100902 ret = -EINVAL;
Chris Wilson35b62a82010-09-26 20:23:38 +0100903 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +0100904 }
905
Daniel Vetter1286ff72012-05-10 15:25:09 +0200906 /* prime objects have no backing filp to GEM pread/pwrite
907 * pages from.
908 */
909 if (!obj->base.filp) {
910 ret = -EINVAL;
911 goto out;
912 }
913
Chris Wilsondb53a302011-02-03 11:57:46 +0000914 trace_i915_gem_object_pwrite(obj, args->offset, args->size);
915
Daniel Vetter935aaa62012-03-25 19:47:35 +0200916 ret = -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -0700917 /* We can only do the GTT pwrite on untiled buffers, as otherwise
918 * it would end up going through the fenced access, and we'll get
919 * different detiling behavior between reading and writing.
920 * pread/pwrite currently are reading and writing from the CPU
921 * perspective, requiring manual detiling by the client.
922 */
Daniel Vetter5c0480f2011-12-14 13:57:30 +0100923 if (obj->phys_obj) {
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100924 ret = i915_gem_phys_pwrite(dev, obj, args, file);
Daniel Vetter5c0480f2011-12-14 13:57:30 +0100925 goto out;
926 }
927
928 if (obj->gtt_space &&
Daniel Vetter3ae53782012-03-25 19:47:33 +0200929 obj->cache_level == I915_CACHE_NONE &&
Daniel Vetterc07496f2012-04-13 15:51:51 +0200930 obj->tiling_mode == I915_TILING_NONE &&
Daniel Vetterffc62972012-03-25 19:47:38 +0200931 obj->map_and_fenceable &&
Daniel Vetter5c0480f2011-12-14 13:57:30 +0100932 obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100933 ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file);
Daniel Vetter935aaa62012-03-25 19:47:35 +0200934 /* Note that the gtt paths might fail with non-page-backed user
935 * pointers (e.g. gtt mappings when moving data between
936 * textures). Fallback to the shmem path in that case. */
Eric Anholt40123c12009-03-09 13:42:30 -0700937 }
Eric Anholt673a3942008-07-30 12:06:12 -0700938
Daniel Vetter5c0480f2011-12-14 13:57:30 +0100939 if (ret == -EFAULT)
Daniel Vetter935aaa62012-03-25 19:47:35 +0200940 ret = i915_gem_shmem_pwrite(dev, obj, args, file);
Daniel Vetter5c0480f2011-12-14 13:57:30 +0100941
Chris Wilson35b62a82010-09-26 20:23:38 +0100942out:
Chris Wilson05394f32010-11-08 19:18:58 +0000943 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100944unlock:
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100945 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -0700946 return ret;
947}
948
949/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800950 * Called when user space prepares to use an object with the CPU, either
951 * through the mmap ioctl's mapping or a GTT mapping.
Eric Anholt673a3942008-07-30 12:06:12 -0700952 */
953int
954i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000955 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700956{
957 struct drm_i915_gem_set_domain *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +0000958 struct drm_i915_gem_object *obj;
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800959 uint32_t read_domains = args->read_domains;
960 uint32_t write_domain = args->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -0700961 int ret;
962
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800963 /* Only handle setting domains to types used by the CPU. */
Chris Wilson21d509e2009-06-06 09:46:02 +0100964 if (write_domain & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800965 return -EINVAL;
966
Chris Wilson21d509e2009-06-06 09:46:02 +0100967 if (read_domains & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800968 return -EINVAL;
969
970 /* Having something in the write domain implies it's in the read
971 * domain, and only that read domain. Enforce that in the request.
972 */
973 if (write_domain != 0 && read_domains != write_domain)
974 return -EINVAL;
975
Chris Wilson76c1dec2010-09-25 11:22:51 +0100976 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100977 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +0100978 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700979
Chris Wilson05394f32010-11-08 19:18:58 +0000980 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +0000981 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100982 ret = -ENOENT;
983 goto unlock;
Chris Wilson76c1dec2010-09-25 11:22:51 +0100984 }
Jesse Barnes652c3932009-08-17 13:31:43 -0700985
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800986 if (read_domains & I915_GEM_DOMAIN_GTT) {
987 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
Eric Anholt02354392008-11-26 13:58:13 -0800988
989 /* Silently promote "you're not bound, there was nothing to do"
990 * to success, since the client was just asking us to
991 * make sure everything was done.
992 */
993 if (ret == -EINVAL)
994 ret = 0;
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800995 } else {
Eric Anholte47c68e2008-11-14 13:35:19 -0800996 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800997 }
998
Chris Wilson05394f32010-11-08 19:18:58 +0000999 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001000unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07001001 mutex_unlock(&dev->struct_mutex);
1002 return ret;
1003}
1004
1005/**
1006 * Called when user space has done writes to this buffer
1007 */
1008int
1009i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001010 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001011{
1012 struct drm_i915_gem_sw_finish *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001013 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001014 int ret = 0;
1015
Chris Wilson76c1dec2010-09-25 11:22:51 +01001016 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001017 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001018 return ret;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001019
Chris Wilson05394f32010-11-08 19:18:58 +00001020 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00001021 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001022 ret = -ENOENT;
1023 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07001024 }
1025
Eric Anholt673a3942008-07-30 12:06:12 -07001026 /* Pinned buffers may be scanout, so flush the cache */
Chris Wilson05394f32010-11-08 19:18:58 +00001027 if (obj->pin_count)
Eric Anholte47c68e2008-11-14 13:35:19 -08001028 i915_gem_object_flush_cpu_write_domain(obj);
1029
Chris Wilson05394f32010-11-08 19:18:58 +00001030 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001031unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07001032 mutex_unlock(&dev->struct_mutex);
1033 return ret;
1034}
1035
1036/**
1037 * Maps the contents of an object, returning the address it is mapped
1038 * into.
1039 *
1040 * While the mapping holds a reference on the contents of the object, it doesn't
1041 * imply a ref on the object itself.
1042 */
1043int
1044i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001045 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001046{
1047 struct drm_i915_gem_mmap *args = data;
1048 struct drm_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001049 unsigned long addr;
1050
Chris Wilson05394f32010-11-08 19:18:58 +00001051 obj = drm_gem_object_lookup(dev, file, args->handle);
Eric Anholt673a3942008-07-30 12:06:12 -07001052 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001053 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001054
Daniel Vetter1286ff72012-05-10 15:25:09 +02001055 /* prime objects have no backing filp to GEM mmap
1056 * pages from.
1057 */
1058 if (!obj->filp) {
1059 drm_gem_object_unreference_unlocked(obj);
1060 return -EINVAL;
1061 }
1062
Linus Torvalds6be5ceb2012-04-20 17:13:58 -07001063 addr = vm_mmap(obj->filp, 0, args->size,
Eric Anholt673a3942008-07-30 12:06:12 -07001064 PROT_READ | PROT_WRITE, MAP_SHARED,
1065 args->offset);
Luca Barbieribc9025b2010-02-09 05:49:12 +00001066 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001067 if (IS_ERR((void *)addr))
1068 return addr;
1069
1070 args->addr_ptr = (uint64_t) addr;
1071
1072 return 0;
1073}
1074
Jesse Barnesde151cf2008-11-12 10:03:55 -08001075/**
1076 * i915_gem_fault - fault a page into the GTT
1077 * vma: VMA in question
1078 * vmf: fault info
1079 *
1080 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1081 * from userspace. The fault handler takes care of binding the object to
1082 * the GTT (if needed), allocating and programming a fence register (again,
1083 * only if needed based on whether the old reg is still valid or the object
1084 * is tiled) and inserting a new PTE into the faulting process.
1085 *
1086 * Note that the faulting process may involve evicting existing objects
1087 * from the GTT and/or fence registers to make room. So performance may
1088 * suffer if the GTT working set is large or there are few fence registers
1089 * left.
1090 */
1091int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1092{
Chris Wilson05394f32010-11-08 19:18:58 +00001093 struct drm_i915_gem_object *obj = to_intel_bo(vma->vm_private_data);
1094 struct drm_device *dev = obj->base.dev;
Chris Wilson7d1c4802010-08-07 21:45:03 +01001095 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001096 pgoff_t page_offset;
1097 unsigned long pfn;
1098 int ret = 0;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001099 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001100
1101 /* We don't use vmf->pgoff since that has the fake offset */
1102 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1103 PAGE_SHIFT;
1104
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001105 ret = i915_mutex_lock_interruptible(dev);
1106 if (ret)
1107 goto out;
Chris Wilsona00b10c2010-09-24 21:15:47 +01001108
Chris Wilsondb53a302011-02-03 11:57:46 +00001109 trace_i915_gem_object_fault(obj, page_offset, true, write);
1110
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001111 /* Now bind it into the GTT if needed */
Chris Wilson919926a2010-11-12 13:42:53 +00001112 if (!obj->map_and_fenceable) {
1113 ret = i915_gem_object_unbind(obj);
1114 if (ret)
1115 goto unlock;
Chris Wilsona00b10c2010-09-24 21:15:47 +01001116 }
Chris Wilson05394f32010-11-08 19:18:58 +00001117 if (!obj->gtt_space) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01001118 ret = i915_gem_object_bind_to_gtt(obj, 0, true);
Chris Wilsonc7150892009-09-23 00:43:56 +01001119 if (ret)
1120 goto unlock;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001121
Eric Anholte92d03b2011-06-14 16:43:09 -07001122 ret = i915_gem_object_set_to_gtt_domain(obj, write);
1123 if (ret)
1124 goto unlock;
1125 }
Chris Wilson4a684a42010-10-28 14:44:08 +01001126
Daniel Vetter74898d72012-02-15 23:50:22 +01001127 if (!obj->has_global_gtt_mapping)
1128 i915_gem_gtt_bind_object(obj, obj->cache_level);
1129
Chris Wilson06d98132012-04-17 15:31:24 +01001130 ret = i915_gem_object_get_fence(obj);
Chris Wilsond9e86c02010-11-10 16:40:20 +00001131 if (ret)
1132 goto unlock;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001133
Chris Wilson05394f32010-11-08 19:18:58 +00001134 if (i915_gem_object_is_inactive(obj))
1135 list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
Chris Wilson7d1c4802010-08-07 21:45:03 +01001136
Chris Wilson6299f992010-11-24 12:23:44 +00001137 obj->fault_mappable = true;
1138
Daniel Vetterdd2757f2012-06-07 15:55:57 +02001139 pfn = ((dev_priv->mm.gtt_base_addr + obj->gtt_offset) >> PAGE_SHIFT) +
Jesse Barnesde151cf2008-11-12 10:03:55 -08001140 page_offset;
1141
1142 /* Finally, remap it using the new GTT offset */
1143 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
Chris Wilsonc7150892009-09-23 00:43:56 +01001144unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001145 mutex_unlock(&dev->struct_mutex);
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001146out:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001147 switch (ret) {
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001148 case -EIO:
Daniel Vettera9340cc2012-07-04 22:18:42 +02001149 /* If this -EIO is due to a gpu hang, give the reset code a
1150 * chance to clean up the mess. Otherwise return the proper
1151 * SIGBUS. */
1152 if (!atomic_read(&dev_priv->mm.wedged))
1153 return VM_FAULT_SIGBUS;
Chris Wilson045e7692010-11-07 09:18:22 +00001154 case -EAGAIN:
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001155 /* Give the error handler a chance to run and move the
1156 * objects off the GPU active list. Next time we service the
1157 * fault, we should be able to transition the page into the
1158 * GTT without touching the GPU (and so avoid further
1159 * EIO/EGAIN). If the GPU is wedged, then there is no issue
1160 * with coherency, just lost writes.
1161 */
Chris Wilson045e7692010-11-07 09:18:22 +00001162 set_need_resched();
Chris Wilsonc7150892009-09-23 00:43:56 +01001163 case 0:
1164 case -ERESTARTSYS:
Chris Wilsonbed636a2011-02-11 20:31:19 +00001165 case -EINTR:
Chris Wilsonc7150892009-09-23 00:43:56 +01001166 return VM_FAULT_NOPAGE;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001167 case -ENOMEM:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001168 return VM_FAULT_OOM;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001169 default:
Chris Wilsonc7150892009-09-23 00:43:56 +01001170 return VM_FAULT_SIGBUS;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001171 }
1172}
1173
1174/**
Chris Wilson901782b2009-07-10 08:18:50 +01001175 * i915_gem_release_mmap - remove physical page mappings
1176 * @obj: obj in question
1177 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001178 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01001179 * relinquish ownership of the pages back to the system.
1180 *
1181 * It is vital that we remove the page mapping if we have mapped a tiled
1182 * object through the GTT and then lose the fence register due to
1183 * resource pressure. Similarly if the object has been moved out of the
1184 * aperture, than pages mapped into userspace must be revoked. Removing the
1185 * mapping will then trigger a page fault on the next user access, allowing
1186 * fixup by i915_gem_fault().
1187 */
Eric Anholtd05ca302009-07-10 13:02:26 -07001188void
Chris Wilson05394f32010-11-08 19:18:58 +00001189i915_gem_release_mmap(struct drm_i915_gem_object *obj)
Chris Wilson901782b2009-07-10 08:18:50 +01001190{
Chris Wilson6299f992010-11-24 12:23:44 +00001191 if (!obj->fault_mappable)
1192 return;
Chris Wilson901782b2009-07-10 08:18:50 +01001193
Chris Wilsonf6e47882011-03-20 21:09:12 +00001194 if (obj->base.dev->dev_mapping)
1195 unmap_mapping_range(obj->base.dev->dev_mapping,
1196 (loff_t)obj->base.map_list.hash.key<<PAGE_SHIFT,
1197 obj->base.size, 1);
Daniel Vetterfb7d5162010-10-01 22:05:20 +02001198
Chris Wilson6299f992010-11-24 12:23:44 +00001199 obj->fault_mappable = false;
Chris Wilson901782b2009-07-10 08:18:50 +01001200}
1201
Chris Wilson92b88ae2010-11-09 11:47:32 +00001202static uint32_t
Chris Wilsone28f8712011-07-18 13:11:49 -07001203i915_gem_get_gtt_size(struct drm_device *dev, uint32_t size, int tiling_mode)
Chris Wilson92b88ae2010-11-09 11:47:32 +00001204{
Chris Wilsone28f8712011-07-18 13:11:49 -07001205 uint32_t gtt_size;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001206
1207 if (INTEL_INFO(dev)->gen >= 4 ||
Chris Wilsone28f8712011-07-18 13:11:49 -07001208 tiling_mode == I915_TILING_NONE)
1209 return size;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001210
1211 /* Previous chips need a power-of-two fence region when tiling */
1212 if (INTEL_INFO(dev)->gen == 3)
Chris Wilsone28f8712011-07-18 13:11:49 -07001213 gtt_size = 1024*1024;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001214 else
Chris Wilsone28f8712011-07-18 13:11:49 -07001215 gtt_size = 512*1024;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001216
Chris Wilsone28f8712011-07-18 13:11:49 -07001217 while (gtt_size < size)
1218 gtt_size <<= 1;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001219
Chris Wilsone28f8712011-07-18 13:11:49 -07001220 return gtt_size;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001221}
1222
Jesse Barnesde151cf2008-11-12 10:03:55 -08001223/**
1224 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1225 * @obj: object to check
1226 *
1227 * Return the required GTT alignment for an object, taking into account
Daniel Vetter5e783302010-11-14 22:32:36 +01001228 * potential fence register mapping.
Jesse Barnesde151cf2008-11-12 10:03:55 -08001229 */
1230static uint32_t
Chris Wilsone28f8712011-07-18 13:11:49 -07001231i915_gem_get_gtt_alignment(struct drm_device *dev,
1232 uint32_t size,
1233 int tiling_mode)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001234{
Jesse Barnesde151cf2008-11-12 10:03:55 -08001235 /*
1236 * Minimum alignment is 4k (GTT page size), but might be greater
1237 * if a fence register is needed for the object.
1238 */
Chris Wilsona00b10c2010-09-24 21:15:47 +01001239 if (INTEL_INFO(dev)->gen >= 4 ||
Chris Wilsone28f8712011-07-18 13:11:49 -07001240 tiling_mode == I915_TILING_NONE)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001241 return 4096;
1242
1243 /*
1244 * Previous chips need to be aligned to the size of the smallest
1245 * fence register that can contain the object.
1246 */
Chris Wilsone28f8712011-07-18 13:11:49 -07001247 return i915_gem_get_gtt_size(dev, size, tiling_mode);
Chris Wilsona00b10c2010-09-24 21:15:47 +01001248}
1249
Daniel Vetter5e783302010-11-14 22:32:36 +01001250/**
1251 * i915_gem_get_unfenced_gtt_alignment - return required GTT alignment for an
1252 * unfenced object
Chris Wilsone28f8712011-07-18 13:11:49 -07001253 * @dev: the device
1254 * @size: size of the object
1255 * @tiling_mode: tiling mode of the object
Daniel Vetter5e783302010-11-14 22:32:36 +01001256 *
1257 * Return the required GTT alignment for an object, only taking into account
1258 * unfenced tiled surface requirements.
1259 */
Chris Wilson467cffb2011-03-07 10:42:03 +00001260uint32_t
Chris Wilsone28f8712011-07-18 13:11:49 -07001261i915_gem_get_unfenced_gtt_alignment(struct drm_device *dev,
1262 uint32_t size,
1263 int tiling_mode)
Daniel Vetter5e783302010-11-14 22:32:36 +01001264{
Daniel Vetter5e783302010-11-14 22:32:36 +01001265 /*
1266 * Minimum alignment is 4k (GTT page size) for sane hw.
1267 */
1268 if (INTEL_INFO(dev)->gen >= 4 || IS_G33(dev) ||
Chris Wilsone28f8712011-07-18 13:11:49 -07001269 tiling_mode == I915_TILING_NONE)
Daniel Vetter5e783302010-11-14 22:32:36 +01001270 return 4096;
1271
Chris Wilsone28f8712011-07-18 13:11:49 -07001272 /* Previous hardware however needs to be aligned to a power-of-two
1273 * tile height. The simplest method for determining this is to reuse
1274 * the power-of-tile object size.
Daniel Vetter5e783302010-11-14 22:32:36 +01001275 */
Chris Wilsone28f8712011-07-18 13:11:49 -07001276 return i915_gem_get_gtt_size(dev, size, tiling_mode);
Daniel Vetter5e783302010-11-14 22:32:36 +01001277}
1278
Jesse Barnesde151cf2008-11-12 10:03:55 -08001279int
Dave Airlieff72145b2011-02-07 12:16:14 +10001280i915_gem_mmap_gtt(struct drm_file *file,
1281 struct drm_device *dev,
1282 uint32_t handle,
1283 uint64_t *offset)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001284{
Chris Wilsonda761a62010-10-27 17:37:08 +01001285 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00001286 struct drm_i915_gem_object *obj;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001287 int ret;
1288
Chris Wilson76c1dec2010-09-25 11:22:51 +01001289 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001290 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001291 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001292
Dave Airlieff72145b2011-02-07 12:16:14 +10001293 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00001294 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001295 ret = -ENOENT;
1296 goto unlock;
1297 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001298
Chris Wilson05394f32010-11-08 19:18:58 +00001299 if (obj->base.size > dev_priv->mm.gtt_mappable_end) {
Chris Wilsonda761a62010-10-27 17:37:08 +01001300 ret = -E2BIG;
Eric Anholtff56b0b2011-10-31 23:16:21 -07001301 goto out;
Chris Wilsonda761a62010-10-27 17:37:08 +01001302 }
1303
Chris Wilson05394f32010-11-08 19:18:58 +00001304 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilsonab182822009-09-22 18:46:17 +01001305 DRM_ERROR("Attempting to mmap a purgeable buffer\n");
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001306 ret = -EINVAL;
1307 goto out;
Chris Wilsonab182822009-09-22 18:46:17 +01001308 }
1309
Chris Wilson05394f32010-11-08 19:18:58 +00001310 if (!obj->base.map_list.map) {
Rob Clarkb464e9a2011-08-10 08:09:08 -05001311 ret = drm_gem_create_mmap_offset(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001312 if (ret)
1313 goto out;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001314 }
1315
Dave Airlieff72145b2011-02-07 12:16:14 +10001316 *offset = (u64)obj->base.map_list.hash.key << PAGE_SHIFT;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001317
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001318out:
Chris Wilson05394f32010-11-08 19:18:58 +00001319 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001320unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001321 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001322 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001323}
1324
Dave Airlieff72145b2011-02-07 12:16:14 +10001325/**
1326 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1327 * @dev: DRM device
1328 * @data: GTT mapping ioctl data
1329 * @file: GEM object info
1330 *
1331 * Simply returns the fake offset to userspace so it can mmap it.
1332 * The mmap call will end up in drm_gem_mmap(), which will set things
1333 * up so we can get faults in the handler above.
1334 *
1335 * The fault handler will take care of binding the object into the GTT
1336 * (since it may have been evicted to make room for something), allocating
1337 * a fence register, and mapping the appropriate aperture address into
1338 * userspace.
1339 */
1340int
1341i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
1342 struct drm_file *file)
1343{
1344 struct drm_i915_gem_mmap_gtt *args = data;
1345
Dave Airlieff72145b2011-02-07 12:16:14 +10001346 return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
1347}
1348
Daniel Vetter225067e2012-08-20 10:23:20 +02001349/* Immediately discard the backing storage */
1350static void
1351i915_gem_object_truncate(struct drm_i915_gem_object *obj)
1352{
1353 struct inode *inode;
1354
1355 /* Our goal here is to return as much of the memory as
1356 * is possible back to the system as we are called from OOM.
1357 * To do this we must instruct the shmfs to drop all of its
1358 * backing pages, *now*.
1359 */
1360 inode = obj->base.filp->f_path.dentry->d_inode;
1361 shmem_truncate_range(inode, 0, (loff_t)-1);
1362
1363 if (obj->base.map_list.map)
1364 drm_gem_free_mmap_offset(&obj->base);
1365
1366 obj->madv = __I915_MADV_PURGED;
1367}
1368
1369static inline int
1370i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj)
1371{
1372 return obj->madv == I915_MADV_DONTNEED;
1373}
1374
Chris Wilson6c085a72012-08-20 11:40:46 +02001375static int
Daniel Vetter225067e2012-08-20 10:23:20 +02001376i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj)
1377{
1378 int page_count = obj->base.size / PAGE_SIZE;
Chris Wilson6c085a72012-08-20 11:40:46 +02001379 int ret, i;
Daniel Vetter225067e2012-08-20 10:23:20 +02001380
Chris Wilsonc4670ad2012-08-20 10:23:27 +01001381 BUG_ON(obj->gtt_space);
1382
Chris Wilson6c085a72012-08-20 11:40:46 +02001383 if (obj->pages == NULL)
1384 return 0;
Daniel Vetter225067e2012-08-20 10:23:20 +02001385
Chris Wilson6c085a72012-08-20 11:40:46 +02001386 BUG_ON(obj->gtt_space);
Daniel Vetter225067e2012-08-20 10:23:20 +02001387 BUG_ON(obj->madv == __I915_MADV_PURGED);
1388
Chris Wilson6c085a72012-08-20 11:40:46 +02001389 ret = i915_gem_object_set_to_cpu_domain(obj, true);
1390 if (ret) {
1391 /* In the event of a disaster, abandon all caches and
1392 * hope for the best.
1393 */
1394 WARN_ON(ret != -EIO);
1395 i915_gem_clflush_object(obj);
1396 obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
1397 }
1398
Daniel Vetter225067e2012-08-20 10:23:20 +02001399 if (i915_gem_object_needs_bit17_swizzle(obj))
1400 i915_gem_object_save_bit_17_swizzle(obj);
1401
1402 if (obj->madv == I915_MADV_DONTNEED)
1403 obj->dirty = 0;
1404
1405 for (i = 0; i < page_count; i++) {
1406 if (obj->dirty)
1407 set_page_dirty(obj->pages[i]);
1408
1409 if (obj->madv == I915_MADV_WILLNEED)
1410 mark_page_accessed(obj->pages[i]);
1411
1412 page_cache_release(obj->pages[i]);
1413 }
1414 obj->dirty = 0;
1415
1416 drm_free_large(obj->pages);
1417 obj->pages = NULL;
Chris Wilson6c085a72012-08-20 11:40:46 +02001418
1419 list_del(&obj->gtt_list);
1420
1421 if (i915_gem_object_is_purgeable(obj))
1422 i915_gem_object_truncate(obj);
1423
1424 return 0;
1425}
1426
1427static long
1428i915_gem_purge(struct drm_i915_private *dev_priv, long target)
1429{
1430 struct drm_i915_gem_object *obj, *next;
1431 long count = 0;
1432
1433 list_for_each_entry_safe(obj, next,
1434 &dev_priv->mm.unbound_list,
1435 gtt_list) {
1436 if (i915_gem_object_is_purgeable(obj) &&
1437 i915_gem_object_put_pages_gtt(obj) == 0) {
1438 count += obj->base.size >> PAGE_SHIFT;
1439 if (count >= target)
1440 return count;
1441 }
1442 }
1443
1444 list_for_each_entry_safe(obj, next,
1445 &dev_priv->mm.inactive_list,
1446 mm_list) {
1447 if (i915_gem_object_is_purgeable(obj) &&
1448 i915_gem_object_unbind(obj) == 0 &&
1449 i915_gem_object_put_pages_gtt(obj) == 0) {
1450 count += obj->base.size >> PAGE_SHIFT;
1451 if (count >= target)
1452 return count;
1453 }
1454 }
1455
1456 return count;
1457}
1458
1459static void
1460i915_gem_shrink_all(struct drm_i915_private *dev_priv)
1461{
1462 struct drm_i915_gem_object *obj, *next;
1463
1464 i915_gem_evict_everything(dev_priv->dev);
1465
1466 list_for_each_entry_safe(obj, next, &dev_priv->mm.unbound_list, gtt_list)
1467 i915_gem_object_put_pages_gtt(obj);
Daniel Vetter225067e2012-08-20 10:23:20 +02001468}
1469
Daniel Vetter1286ff72012-05-10 15:25:09 +02001470int
Chris Wilson6c085a72012-08-20 11:40:46 +02001471i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
Chris Wilsone5281cc2010-10-28 13:45:36 +01001472{
Chris Wilson6c085a72012-08-20 11:40:46 +02001473 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001474 int page_count, i;
1475 struct address_space *mapping;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001476 struct page *page;
Chris Wilson6c085a72012-08-20 11:40:46 +02001477 gfp_t gfp;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001478
Daniel Vetter1286ff72012-05-10 15:25:09 +02001479 if (obj->pages || obj->sg_table)
1480 return 0;
1481
Chris Wilson6c085a72012-08-20 11:40:46 +02001482 /* Assert that the object is not currently in any GPU domain. As it
1483 * wasn't in the GTT, there shouldn't be any way it could have been in
1484 * a GPU cache
1485 */
1486 BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
1487 BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
1488
Chris Wilsone5281cc2010-10-28 13:45:36 +01001489 /* Get the list of pages out of our struct file. They'll be pinned
1490 * at this point until we release them.
1491 */
Chris Wilson05394f32010-11-08 19:18:58 +00001492 page_count = obj->base.size / PAGE_SIZE;
Chris Wilson05394f32010-11-08 19:18:58 +00001493 obj->pages = drm_malloc_ab(page_count, sizeof(struct page *));
1494 if (obj->pages == NULL)
Chris Wilsone5281cc2010-10-28 13:45:36 +01001495 return -ENOMEM;
1496
Chris Wilson6c085a72012-08-20 11:40:46 +02001497 /* Fail silently without starting the shrinker */
1498 mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
1499 gfp = mapping_gfp_mask(mapping);
1500 gfp |= __GFP_NORETRY | __GFP_NOWARN | __GFP_NO_KSWAPD;
1501 gfp &= ~(__GFP_IO | __GFP_WAIT);
Chris Wilsone5281cc2010-10-28 13:45:36 +01001502 for (i = 0; i < page_count; i++) {
Chris Wilson6c085a72012-08-20 11:40:46 +02001503 page = shmem_read_mapping_page_gfp(mapping, i, gfp);
1504 if (IS_ERR(page)) {
1505 i915_gem_purge(dev_priv, page_count);
1506 page = shmem_read_mapping_page_gfp(mapping, i, gfp);
1507 }
1508 if (IS_ERR(page)) {
1509 /* We've tried hard to allocate the memory by reaping
1510 * our own buffer, now let the real VM do its job and
1511 * go down in flames if truly OOM.
1512 */
1513 gfp &= ~(__GFP_NORETRY | __GFP_NOWARN | __GFP_NO_KSWAPD);
1514 gfp |= __GFP_IO | __GFP_WAIT;
1515
1516 i915_gem_shrink_all(dev_priv);
1517 page = shmem_read_mapping_page_gfp(mapping, i, gfp);
1518 if (IS_ERR(page))
1519 goto err_pages;
1520
1521 gfp |= __GFP_NORETRY | __GFP_NOWARN | __GFP_NO_KSWAPD;
1522 gfp &= ~(__GFP_IO | __GFP_WAIT);
1523 }
Chris Wilsone5281cc2010-10-28 13:45:36 +01001524
Chris Wilson05394f32010-11-08 19:18:58 +00001525 obj->pages[i] = page;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001526 }
1527
Daniel Vetter6dacfd22011-09-12 21:30:02 +02001528 if (i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilsone5281cc2010-10-28 13:45:36 +01001529 i915_gem_object_do_bit_17_swizzle(obj);
1530
Chris Wilson6c085a72012-08-20 11:40:46 +02001531 list_add_tail(&obj->gtt_list, &dev_priv->mm.unbound_list);
Chris Wilsone5281cc2010-10-28 13:45:36 +01001532 return 0;
1533
1534err_pages:
1535 while (i--)
Chris Wilson05394f32010-11-08 19:18:58 +00001536 page_cache_release(obj->pages[i]);
Chris Wilsone5281cc2010-10-28 13:45:36 +01001537
Chris Wilson05394f32010-11-08 19:18:58 +00001538 drm_free_large(obj->pages);
1539 obj->pages = NULL;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001540 return PTR_ERR(page);
1541}
1542
Chris Wilson54cf91d2010-11-25 18:00:26 +00001543void
Chris Wilson05394f32010-11-08 19:18:58 +00001544i915_gem_object_move_to_active(struct drm_i915_gem_object *obj,
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001545 struct intel_ring_buffer *ring,
1546 u32 seqno)
Eric Anholt673a3942008-07-30 12:06:12 -07001547{
Chris Wilson05394f32010-11-08 19:18:58 +00001548 struct drm_device *dev = obj->base.dev;
Chris Wilson69dc4982010-10-19 10:36:51 +01001549 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter617dbe22010-02-11 22:16:02 +01001550
Zou Nan hai852835f2010-05-21 09:08:56 +08001551 BUG_ON(ring == NULL);
Chris Wilson05394f32010-11-08 19:18:58 +00001552 obj->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001553
1554 /* Add a reference if we're newly entering the active list. */
Chris Wilson05394f32010-11-08 19:18:58 +00001555 if (!obj->active) {
1556 drm_gem_object_reference(&obj->base);
1557 obj->active = 1;
Eric Anholt673a3942008-07-30 12:06:12 -07001558 }
Daniel Vettere35a41d2010-02-11 22:13:59 +01001559
Eric Anholt673a3942008-07-30 12:06:12 -07001560 /* Move from whatever list we were on to the tail of execution. */
Chris Wilson05394f32010-11-08 19:18:58 +00001561 list_move_tail(&obj->mm_list, &dev_priv->mm.active_list);
1562 list_move_tail(&obj->ring_list, &ring->active_list);
Chris Wilsoncaea7472010-11-12 13:53:37 +00001563
Chris Wilson0201f1e2012-07-20 12:41:01 +01001564 obj->last_read_seqno = seqno;
Chris Wilson7dd49062012-03-21 10:48:18 +00001565
Chris Wilsoncaea7472010-11-12 13:53:37 +00001566 if (obj->fenced_gpu_access) {
Chris Wilsoncaea7472010-11-12 13:53:37 +00001567 obj->last_fenced_seqno = seqno;
Chris Wilsoncaea7472010-11-12 13:53:37 +00001568
Chris Wilson7dd49062012-03-21 10:48:18 +00001569 /* Bump MRU to take account of the delayed flush */
1570 if (obj->fence_reg != I915_FENCE_REG_NONE) {
1571 struct drm_i915_fence_reg *reg;
1572
1573 reg = &dev_priv->fence_regs[obj->fence_reg];
1574 list_move_tail(&reg->lru_list,
1575 &dev_priv->mm.fence_list);
1576 }
Chris Wilsoncaea7472010-11-12 13:53:37 +00001577 }
1578}
1579
1580static void
Chris Wilsoncaea7472010-11-12 13:53:37 +00001581i915_gem_object_move_to_inactive(struct drm_i915_gem_object *obj)
1582{
1583 struct drm_device *dev = obj->base.dev;
1584 struct drm_i915_private *dev_priv = dev->dev_private;
1585
Chris Wilson65ce3022012-07-20 12:41:02 +01001586 BUG_ON(obj->base.write_domain & ~I915_GEM_GPU_DOMAINS);
Chris Wilsoncaea7472010-11-12 13:53:37 +00001587 BUG_ON(!obj->active);
Chris Wilson65ce3022012-07-20 12:41:02 +01001588
Chris Wilsonf047e392012-07-21 12:31:41 +01001589 if (obj->pin_count) /* are we a framebuffer? */
1590 intel_mark_fb_idle(obj);
1591
1592 list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
1593
Chris Wilson65ce3022012-07-20 12:41:02 +01001594 list_del_init(&obj->ring_list);
Chris Wilsoncaea7472010-11-12 13:53:37 +00001595 obj->ring = NULL;
1596
Chris Wilson65ce3022012-07-20 12:41:02 +01001597 obj->last_read_seqno = 0;
1598 obj->last_write_seqno = 0;
1599 obj->base.write_domain = 0;
1600
1601 obj->last_fenced_seqno = 0;
Chris Wilsoncaea7472010-11-12 13:53:37 +00001602 obj->fenced_gpu_access = false;
Chris Wilsoncaea7472010-11-12 13:53:37 +00001603
1604 obj->active = 0;
1605 drm_gem_object_unreference(&obj->base);
1606
1607 WARN_ON(i915_verify_lists(dev));
Eric Anholtce44b0e2008-11-06 16:00:31 -08001608}
Eric Anholt673a3942008-07-30 12:06:12 -07001609
Daniel Vetter53d227f2012-01-25 16:32:49 +01001610static u32
1611i915_gem_get_seqno(struct drm_device *dev)
1612{
1613 drm_i915_private_t *dev_priv = dev->dev_private;
1614 u32 seqno = dev_priv->next_seqno;
1615
1616 /* reserve 0 for non-seqno */
1617 if (++dev_priv->next_seqno == 0)
1618 dev_priv->next_seqno = 1;
1619
1620 return seqno;
1621}
1622
1623u32
1624i915_gem_next_request_seqno(struct intel_ring_buffer *ring)
1625{
1626 if (ring->outstanding_lazy_request == 0)
1627 ring->outstanding_lazy_request = i915_gem_get_seqno(ring->dev);
1628
1629 return ring->outstanding_lazy_request;
1630}
1631
Chris Wilson3cce4692010-10-27 16:11:02 +01001632int
Chris Wilsondb53a302011-02-03 11:57:46 +00001633i915_add_request(struct intel_ring_buffer *ring,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001634 struct drm_file *file,
Chris Wilsondb53a302011-02-03 11:57:46 +00001635 struct drm_i915_gem_request *request)
Eric Anholt673a3942008-07-30 12:06:12 -07001636{
Chris Wilsondb53a302011-02-03 11:57:46 +00001637 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07001638 uint32_t seqno;
Chris Wilsona71d8d92012-02-15 11:25:36 +00001639 u32 request_ring_position;
Eric Anholt673a3942008-07-30 12:06:12 -07001640 int was_empty;
Chris Wilson3cce4692010-10-27 16:11:02 +01001641 int ret;
1642
Daniel Vettercc889e02012-06-13 20:45:19 +02001643 /*
1644 * Emit any outstanding flushes - execbuf can fail to emit the flush
1645 * after having emitted the batchbuffer command. Hence we need to fix
1646 * things up similar to emitting the lazy request. The difference here
1647 * is that the flush _must_ happen before the next request, no matter
1648 * what.
1649 */
Chris Wilsona7b97612012-07-20 12:41:08 +01001650 ret = intel_ring_flush_all_caches(ring);
1651 if (ret)
1652 return ret;
Daniel Vettercc889e02012-06-13 20:45:19 +02001653
Chris Wilson3bb73ab2012-07-20 12:40:59 +01001654 if (request == NULL) {
1655 request = kmalloc(sizeof(*request), GFP_KERNEL);
1656 if (request == NULL)
1657 return -ENOMEM;
1658 }
1659
Daniel Vetter53d227f2012-01-25 16:32:49 +01001660 seqno = i915_gem_next_request_seqno(ring);
Eric Anholt673a3942008-07-30 12:06:12 -07001661
Chris Wilsona71d8d92012-02-15 11:25:36 +00001662 /* Record the position of the start of the request so that
1663 * should we detect the updated seqno part-way through the
1664 * GPU processing the request, we never over-estimate the
1665 * position of the head.
1666 */
1667 request_ring_position = intel_ring_get_tail(ring);
1668
Chris Wilson3cce4692010-10-27 16:11:02 +01001669 ret = ring->add_request(ring, &seqno);
Chris Wilson3bb73ab2012-07-20 12:40:59 +01001670 if (ret) {
1671 kfree(request);
1672 return ret;
1673 }
Eric Anholt673a3942008-07-30 12:06:12 -07001674
Chris Wilsondb53a302011-02-03 11:57:46 +00001675 trace_i915_gem_request_add(ring, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07001676
1677 request->seqno = seqno;
Zou Nan hai852835f2010-05-21 09:08:56 +08001678 request->ring = ring;
Chris Wilsona71d8d92012-02-15 11:25:36 +00001679 request->tail = request_ring_position;
Eric Anholt673a3942008-07-30 12:06:12 -07001680 request->emitted_jiffies = jiffies;
Zou Nan hai852835f2010-05-21 09:08:56 +08001681 was_empty = list_empty(&ring->request_list);
1682 list_add_tail(&request->list, &ring->request_list);
Chris Wilson3bb73ab2012-07-20 12:40:59 +01001683 request->file_priv = NULL;
Zou Nan hai852835f2010-05-21 09:08:56 +08001684
Chris Wilsondb53a302011-02-03 11:57:46 +00001685 if (file) {
1686 struct drm_i915_file_private *file_priv = file->driver_priv;
1687
Chris Wilson1c255952010-09-26 11:03:27 +01001688 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001689 request->file_priv = file_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00001690 list_add_tail(&request->client_list,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001691 &file_priv->mm.request_list);
Chris Wilson1c255952010-09-26 11:03:27 +01001692 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00001693 }
Eric Anholt673a3942008-07-30 12:06:12 -07001694
Daniel Vetter5391d0c2012-01-25 14:03:57 +01001695 ring->outstanding_lazy_request = 0;
Chris Wilsondb53a302011-02-03 11:57:46 +00001696
Ben Gamarif65d9422009-09-14 17:48:44 -04001697 if (!dev_priv->mm.suspended) {
Ben Widawsky3e0dc6b2011-06-29 10:26:42 -07001698 if (i915_enable_hangcheck) {
1699 mod_timer(&dev_priv->hangcheck_timer,
1700 jiffies +
1701 msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
1702 }
Chris Wilsonf047e392012-07-21 12:31:41 +01001703 if (was_empty) {
Chris Wilsonb3b079d2010-09-13 23:44:34 +01001704 queue_delayed_work(dev_priv->wq,
1705 &dev_priv->mm.retire_work, HZ);
Chris Wilsonf047e392012-07-21 12:31:41 +01001706 intel_mark_busy(dev_priv->dev);
1707 }
Ben Gamarif65d9422009-09-14 17:48:44 -04001708 }
Daniel Vettercc889e02012-06-13 20:45:19 +02001709
Chris Wilson3cce4692010-10-27 16:11:02 +01001710 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001711}
1712
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001713static inline void
1714i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
Eric Anholt673a3942008-07-30 12:06:12 -07001715{
Chris Wilson1c255952010-09-26 11:03:27 +01001716 struct drm_i915_file_private *file_priv = request->file_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07001717
Chris Wilson1c255952010-09-26 11:03:27 +01001718 if (!file_priv)
1719 return;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001720
Chris Wilson1c255952010-09-26 11:03:27 +01001721 spin_lock(&file_priv->mm.lock);
Herton Ronaldo Krzesinski09bfa512011-03-17 13:45:12 +00001722 if (request->file_priv) {
1723 list_del(&request->client_list);
1724 request->file_priv = NULL;
1725 }
Chris Wilson1c255952010-09-26 11:03:27 +01001726 spin_unlock(&file_priv->mm.lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001727}
1728
Chris Wilsondfaae392010-09-22 10:31:52 +01001729static void i915_gem_reset_ring_lists(struct drm_i915_private *dev_priv,
1730 struct intel_ring_buffer *ring)
Chris Wilson9375e442010-09-19 12:21:28 +01001731{
Chris Wilsondfaae392010-09-22 10:31:52 +01001732 while (!list_empty(&ring->request_list)) {
1733 struct drm_i915_gem_request *request;
Chris Wilson9375e442010-09-19 12:21:28 +01001734
Chris Wilsondfaae392010-09-22 10:31:52 +01001735 request = list_first_entry(&ring->request_list,
1736 struct drm_i915_gem_request,
1737 list);
1738
1739 list_del(&request->list);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001740 i915_gem_request_remove_from_client(request);
Chris Wilsondfaae392010-09-22 10:31:52 +01001741 kfree(request);
1742 }
1743
1744 while (!list_empty(&ring->active_list)) {
Chris Wilson05394f32010-11-08 19:18:58 +00001745 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001746
Chris Wilson05394f32010-11-08 19:18:58 +00001747 obj = list_first_entry(&ring->active_list,
1748 struct drm_i915_gem_object,
1749 ring_list);
Eric Anholt673a3942008-07-30 12:06:12 -07001750
Chris Wilson05394f32010-11-08 19:18:58 +00001751 i915_gem_object_move_to_inactive(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001752 }
Eric Anholt673a3942008-07-30 12:06:12 -07001753}
1754
Chris Wilson312817a2010-11-22 11:50:11 +00001755static void i915_gem_reset_fences(struct drm_device *dev)
1756{
1757 struct drm_i915_private *dev_priv = dev->dev_private;
1758 int i;
1759
Daniel Vetter4b9de732011-10-09 21:52:02 +02001760 for (i = 0; i < dev_priv->num_fence_regs; i++) {
Chris Wilson312817a2010-11-22 11:50:11 +00001761 struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
Chris Wilson7d2cb392010-11-27 17:38:29 +00001762
Chris Wilsonada726c2012-04-17 15:31:32 +01001763 i915_gem_write_fence(dev, i, NULL);
Chris Wilson7d2cb392010-11-27 17:38:29 +00001764
Chris Wilsonada726c2012-04-17 15:31:32 +01001765 if (reg->obj)
1766 i915_gem_object_fence_lost(reg->obj);
Chris Wilson7d2cb392010-11-27 17:38:29 +00001767
Chris Wilsonada726c2012-04-17 15:31:32 +01001768 reg->pin_count = 0;
1769 reg->obj = NULL;
1770 INIT_LIST_HEAD(&reg->lru_list);
Chris Wilson312817a2010-11-22 11:50:11 +00001771 }
Chris Wilsonada726c2012-04-17 15:31:32 +01001772
1773 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Chris Wilson312817a2010-11-22 11:50:11 +00001774}
1775
Chris Wilson069efc12010-09-30 16:53:18 +01001776void i915_gem_reset(struct drm_device *dev)
Eric Anholt673a3942008-07-30 12:06:12 -07001777{
Chris Wilsondfaae392010-09-22 10:31:52 +01001778 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00001779 struct drm_i915_gem_object *obj;
Chris Wilsonb4519512012-05-11 14:29:30 +01001780 struct intel_ring_buffer *ring;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001781 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07001782
Chris Wilsonb4519512012-05-11 14:29:30 +01001783 for_each_ring(ring, dev_priv, i)
1784 i915_gem_reset_ring_lists(dev_priv, ring);
Chris Wilsondfaae392010-09-22 10:31:52 +01001785
Chris Wilsondfaae392010-09-22 10:31:52 +01001786 /* Move everything out of the GPU domains to ensure we do any
1787 * necessary invalidation upon reuse.
1788 */
Chris Wilson05394f32010-11-08 19:18:58 +00001789 list_for_each_entry(obj,
Chris Wilson77f01232010-09-19 12:31:36 +01001790 &dev_priv->mm.inactive_list,
Chris Wilson69dc4982010-10-19 10:36:51 +01001791 mm_list)
Chris Wilson77f01232010-09-19 12:31:36 +01001792 {
Chris Wilson05394f32010-11-08 19:18:58 +00001793 obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
Chris Wilson77f01232010-09-19 12:31:36 +01001794 }
Chris Wilson069efc12010-09-30 16:53:18 +01001795
Chris Wilson6c085a72012-08-20 11:40:46 +02001796
Chris Wilson069efc12010-09-30 16:53:18 +01001797 /* The fence registers are invalidated so clear them out */
Chris Wilson312817a2010-11-22 11:50:11 +00001798 i915_gem_reset_fences(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07001799}
1800
1801/**
1802 * This function clears the request list as sequence numbers are passed.
1803 */
Chris Wilsona71d8d92012-02-15 11:25:36 +00001804void
Chris Wilsondb53a302011-02-03 11:57:46 +00001805i915_gem_retire_requests_ring(struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001806{
Eric Anholt673a3942008-07-30 12:06:12 -07001807 uint32_t seqno;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001808 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07001809
Chris Wilsondb53a302011-02-03 11:57:46 +00001810 if (list_empty(&ring->request_list))
Karsten Wiese6c0594a2009-02-23 15:07:57 +01001811 return;
1812
Chris Wilsondb53a302011-02-03 11:57:46 +00001813 WARN_ON(i915_verify_lists(ring->dev));
Eric Anholt673a3942008-07-30 12:06:12 -07001814
Chris Wilsonb2eadbc2012-08-09 10:58:30 +01001815 seqno = ring->get_seqno(ring, true);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001816
Chris Wilson076e2c02011-01-21 10:07:18 +00001817 for (i = 0; i < ARRAY_SIZE(ring->sync_seqno); i++)
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001818 if (seqno >= ring->sync_seqno[i])
1819 ring->sync_seqno[i] = 0;
1820
Zou Nan hai852835f2010-05-21 09:08:56 +08001821 while (!list_empty(&ring->request_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001822 struct drm_i915_gem_request *request;
Eric Anholt673a3942008-07-30 12:06:12 -07001823
Zou Nan hai852835f2010-05-21 09:08:56 +08001824 request = list_first_entry(&ring->request_list,
Eric Anholt673a3942008-07-30 12:06:12 -07001825 struct drm_i915_gem_request,
1826 list);
Eric Anholt673a3942008-07-30 12:06:12 -07001827
Chris Wilsondfaae392010-09-22 10:31:52 +01001828 if (!i915_seqno_passed(seqno, request->seqno))
Eric Anholt673a3942008-07-30 12:06:12 -07001829 break;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001830
Chris Wilsondb53a302011-02-03 11:57:46 +00001831 trace_i915_gem_request_retire(ring, request->seqno);
Chris Wilsona71d8d92012-02-15 11:25:36 +00001832 /* We know the GPU must have read the request to have
1833 * sent us the seqno + interrupt, so use the position
1834 * of tail of the request to update the last known position
1835 * of the GPU head.
1836 */
1837 ring->last_retired_head = request->tail;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001838
1839 list_del(&request->list);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001840 i915_gem_request_remove_from_client(request);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001841 kfree(request);
1842 }
1843
1844 /* Move any buffers on the active list that are no longer referenced
1845 * by the ringbuffer to the flushing/inactive lists as appropriate.
1846 */
1847 while (!list_empty(&ring->active_list)) {
Chris Wilson05394f32010-11-08 19:18:58 +00001848 struct drm_i915_gem_object *obj;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001849
Akshay Joshi0206e352011-08-16 15:34:10 -04001850 obj = list_first_entry(&ring->active_list,
Chris Wilson05394f32010-11-08 19:18:58 +00001851 struct drm_i915_gem_object,
1852 ring_list);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001853
Chris Wilson0201f1e2012-07-20 12:41:01 +01001854 if (!i915_seqno_passed(seqno, obj->last_read_seqno))
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001855 break;
1856
Chris Wilson65ce3022012-07-20 12:41:02 +01001857 i915_gem_object_move_to_inactive(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001858 }
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001859
Chris Wilsondb53a302011-02-03 11:57:46 +00001860 if (unlikely(ring->trace_irq_seqno &&
1861 i915_seqno_passed(seqno, ring->trace_irq_seqno))) {
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001862 ring->irq_put(ring);
Chris Wilsondb53a302011-02-03 11:57:46 +00001863 ring->trace_irq_seqno = 0;
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001864 }
Chris Wilson23bc5982010-09-29 16:10:57 +01001865
Chris Wilsondb53a302011-02-03 11:57:46 +00001866 WARN_ON(i915_verify_lists(ring->dev));
Eric Anholt673a3942008-07-30 12:06:12 -07001867}
1868
1869void
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001870i915_gem_retire_requests(struct drm_device *dev)
1871{
1872 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonb4519512012-05-11 14:29:30 +01001873 struct intel_ring_buffer *ring;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001874 int i;
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001875
Chris Wilsonb4519512012-05-11 14:29:30 +01001876 for_each_ring(ring, dev_priv, i)
1877 i915_gem_retire_requests_ring(ring);
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001878}
1879
Daniel Vetter75ef9da2010-08-21 00:25:16 +02001880static void
Eric Anholt673a3942008-07-30 12:06:12 -07001881i915_gem_retire_work_handler(struct work_struct *work)
1882{
1883 drm_i915_private_t *dev_priv;
1884 struct drm_device *dev;
Chris Wilsonb4519512012-05-11 14:29:30 +01001885 struct intel_ring_buffer *ring;
Chris Wilson0a587052011-01-09 21:05:44 +00001886 bool idle;
1887 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07001888
1889 dev_priv = container_of(work, drm_i915_private_t,
1890 mm.retire_work.work);
1891 dev = dev_priv->dev;
1892
Chris Wilson891b48c2010-09-29 12:26:37 +01001893 /* Come back later if the device is busy... */
1894 if (!mutex_trylock(&dev->struct_mutex)) {
1895 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
1896 return;
1897 }
1898
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001899 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08001900
Chris Wilson0a587052011-01-09 21:05:44 +00001901 /* Send a periodic flush down the ring so we don't hold onto GEM
1902 * objects indefinitely.
1903 */
1904 idle = true;
Chris Wilsonb4519512012-05-11 14:29:30 +01001905 for_each_ring(ring, dev_priv, i) {
Chris Wilson3bb73ab2012-07-20 12:40:59 +01001906 if (ring->gpu_caches_dirty)
1907 i915_add_request(ring, NULL, NULL);
Chris Wilson0a587052011-01-09 21:05:44 +00001908
1909 idle &= list_empty(&ring->request_list);
1910 }
1911
1912 if (!dev_priv->mm.suspended && !idle)
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001913 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
Chris Wilsonf047e392012-07-21 12:31:41 +01001914 if (idle)
1915 intel_mark_idle(dev);
Chris Wilson0a587052011-01-09 21:05:44 +00001916
Eric Anholt673a3942008-07-30 12:06:12 -07001917 mutex_unlock(&dev->struct_mutex);
1918}
1919
Daniel Vetterd6b2c792012-07-04 22:54:13 +02001920int
1921i915_gem_check_wedge(struct drm_i915_private *dev_priv,
1922 bool interruptible)
Ben Widawskyb4aca012012-04-25 20:50:12 -07001923{
Ben Widawskyb4aca012012-04-25 20:50:12 -07001924 if (atomic_read(&dev_priv->mm.wedged)) {
1925 struct completion *x = &dev_priv->error_completion;
1926 bool recovery_complete;
1927 unsigned long flags;
1928
1929 /* Give the error handler a chance to run. */
1930 spin_lock_irqsave(&x->wait.lock, flags);
1931 recovery_complete = x->done > 0;
1932 spin_unlock_irqrestore(&x->wait.lock, flags);
1933
Daniel Vetterd6b2c792012-07-04 22:54:13 +02001934 /* Non-interruptible callers can't handle -EAGAIN, hence return
1935 * -EIO unconditionally for these. */
1936 if (!interruptible)
1937 return -EIO;
1938
1939 /* Recovery complete, but still wedged means reset failure. */
1940 if (recovery_complete)
1941 return -EIO;
1942
1943 return -EAGAIN;
Ben Widawskyb4aca012012-04-25 20:50:12 -07001944 }
1945
1946 return 0;
1947}
1948
1949/*
1950 * Compare seqno against outstanding lazy request. Emit a request if they are
1951 * equal.
1952 */
1953static int
1954i915_gem_check_olr(struct intel_ring_buffer *ring, u32 seqno)
1955{
Chris Wilson3bb73ab2012-07-20 12:40:59 +01001956 int ret;
Ben Widawskyb4aca012012-04-25 20:50:12 -07001957
1958 BUG_ON(!mutex_is_locked(&ring->dev->struct_mutex));
1959
Chris Wilson3bb73ab2012-07-20 12:40:59 +01001960 ret = 0;
1961 if (seqno == ring->outstanding_lazy_request)
1962 ret = i915_add_request(ring, NULL, NULL);
Ben Widawskyb4aca012012-04-25 20:50:12 -07001963
1964 return ret;
1965}
1966
Ben Widawsky5c81fe852012-05-24 15:03:08 -07001967/**
1968 * __wait_seqno - wait until execution of seqno has finished
1969 * @ring: the ring expected to report seqno
1970 * @seqno: duh!
1971 * @interruptible: do an interruptible wait (normally yes)
1972 * @timeout: in - how long to wait (NULL forever); out - how much time remaining
1973 *
1974 * Returns 0 if the seqno was found within the alloted time. Else returns the
1975 * errno with remaining time filled in timeout argument.
1976 */
Ben Widawsky604dd3e2012-04-26 16:03:03 -07001977static int __wait_seqno(struct intel_ring_buffer *ring, u32 seqno,
Ben Widawsky5c81fe852012-05-24 15:03:08 -07001978 bool interruptible, struct timespec *timeout)
Ben Widawsky604dd3e2012-04-26 16:03:03 -07001979{
1980 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Ben Widawsky5c81fe852012-05-24 15:03:08 -07001981 struct timespec before, now, wait_time={1,0};
1982 unsigned long timeout_jiffies;
1983 long end;
1984 bool wait_forever = true;
Daniel Vetterd6b2c792012-07-04 22:54:13 +02001985 int ret;
Ben Widawsky604dd3e2012-04-26 16:03:03 -07001986
Chris Wilsonb2eadbc2012-08-09 10:58:30 +01001987 if (i915_seqno_passed(ring->get_seqno(ring, true), seqno))
Ben Widawsky604dd3e2012-04-26 16:03:03 -07001988 return 0;
1989
1990 trace_i915_gem_request_wait_begin(ring, seqno);
Ben Widawsky5c81fe852012-05-24 15:03:08 -07001991
1992 if (timeout != NULL) {
1993 wait_time = *timeout;
1994 wait_forever = false;
1995 }
1996
1997 timeout_jiffies = timespec_to_jiffies(&wait_time);
1998
Ben Widawsky604dd3e2012-04-26 16:03:03 -07001999 if (WARN_ON(!ring->irq_get(ring)))
2000 return -ENODEV;
2001
Ben Widawsky5c81fe852012-05-24 15:03:08 -07002002 /* Record current time in case interrupted by signal, or wedged * */
2003 getrawmonotonic(&before);
2004
Ben Widawsky604dd3e2012-04-26 16:03:03 -07002005#define EXIT_COND \
Chris Wilsonb2eadbc2012-08-09 10:58:30 +01002006 (i915_seqno_passed(ring->get_seqno(ring, false), seqno) || \
Ben Widawsky604dd3e2012-04-26 16:03:03 -07002007 atomic_read(&dev_priv->mm.wedged))
Ben Widawsky5c81fe852012-05-24 15:03:08 -07002008 do {
2009 if (interruptible)
2010 end = wait_event_interruptible_timeout(ring->irq_queue,
2011 EXIT_COND,
2012 timeout_jiffies);
2013 else
2014 end = wait_event_timeout(ring->irq_queue, EXIT_COND,
2015 timeout_jiffies);
Ben Widawsky604dd3e2012-04-26 16:03:03 -07002016
Daniel Vetterd6b2c792012-07-04 22:54:13 +02002017 ret = i915_gem_check_wedge(dev_priv, interruptible);
2018 if (ret)
2019 end = ret;
Ben Widawsky5c81fe852012-05-24 15:03:08 -07002020 } while (end == 0 && wait_forever);
2021
2022 getrawmonotonic(&now);
Ben Widawsky604dd3e2012-04-26 16:03:03 -07002023
2024 ring->irq_put(ring);
2025 trace_i915_gem_request_wait_end(ring, seqno);
2026#undef EXIT_COND
2027
Ben Widawsky5c81fe852012-05-24 15:03:08 -07002028 if (timeout) {
2029 struct timespec sleep_time = timespec_sub(now, before);
2030 *timeout = timespec_sub(*timeout, sleep_time);
2031 }
2032
2033 switch (end) {
Chris Wilsoneeef9b32012-07-16 13:05:34 +01002034 case -EIO:
Ben Widawsky5c81fe852012-05-24 15:03:08 -07002035 case -EAGAIN: /* Wedged */
2036 case -ERESTARTSYS: /* Signal */
2037 return (int)end;
2038 case 0: /* Timeout */
2039 if (timeout)
2040 set_normalized_timespec(timeout, 0, 0);
2041 return -ETIME;
2042 default: /* Completed */
2043 WARN_ON(end < 0); /* We're not aware of other errors */
2044 return 0;
2045 }
Ben Widawsky604dd3e2012-04-26 16:03:03 -07002046}
2047
Chris Wilsondb53a302011-02-03 11:57:46 +00002048/**
2049 * Waits for a sequence number to be signaled, and cleans up the
2050 * request and object lists appropriately for that event.
2051 */
Daniel Vetter5a5a0c62009-09-15 22:57:36 +02002052int
Ben Widawsky199b2bc2012-05-24 15:03:11 -07002053i915_wait_seqno(struct intel_ring_buffer *ring, uint32_t seqno)
Eric Anholt673a3942008-07-30 12:06:12 -07002054{
Chris Wilsondb53a302011-02-03 11:57:46 +00002055 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07002056 int ret = 0;
2057
2058 BUG_ON(seqno == 0);
2059
Daniel Vetterd6b2c792012-07-04 22:54:13 +02002060 ret = i915_gem_check_wedge(dev_priv, dev_priv->mm.interruptible);
Ben Widawskyb4aca012012-04-25 20:50:12 -07002061 if (ret)
2062 return ret;
Chris Wilsond9bc7e92011-02-07 13:09:31 +00002063
Ben Widawskyb4aca012012-04-25 20:50:12 -07002064 ret = i915_gem_check_olr(ring, seqno);
2065 if (ret)
2066 return ret;
Daniel Vettere35a41d2010-02-11 22:13:59 +01002067
Ben Widawsky5c81fe852012-05-24 15:03:08 -07002068 ret = __wait_seqno(ring, seqno, dev_priv->mm.interruptible, NULL);
Eric Anholt673a3942008-07-30 12:06:12 -07002069
Eric Anholt673a3942008-07-30 12:06:12 -07002070 return ret;
2071}
2072
Daniel Vetter48764bf2009-09-15 22:57:32 +02002073/**
Eric Anholt673a3942008-07-30 12:06:12 -07002074 * Ensures that all rendering to the object has completed and the object is
2075 * safe to unbind from the GTT or access from the CPU.
2076 */
Chris Wilson0201f1e2012-07-20 12:41:01 +01002077static __must_check int
2078i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj,
2079 bool readonly)
Eric Anholt673a3942008-07-30 12:06:12 -07002080{
Chris Wilson0201f1e2012-07-20 12:41:01 +01002081 u32 seqno;
Eric Anholt673a3942008-07-30 12:06:12 -07002082 int ret;
2083
Eric Anholt673a3942008-07-30 12:06:12 -07002084 /* If there is rendering queued on the buffer being evicted, wait for
2085 * it.
2086 */
Chris Wilson0201f1e2012-07-20 12:41:01 +01002087 if (readonly)
2088 seqno = obj->last_write_seqno;
2089 else
2090 seqno = obj->last_read_seqno;
2091 if (seqno == 0)
2092 return 0;
2093
2094 ret = i915_wait_seqno(obj->ring, seqno);
2095 if (ret)
2096 return ret;
2097
2098 /* Manually manage the write flush as we may have not yet retired
2099 * the buffer.
2100 */
2101 if (obj->last_write_seqno &&
2102 i915_seqno_passed(seqno, obj->last_write_seqno)) {
2103 obj->last_write_seqno = 0;
2104 obj->base.write_domain &= ~I915_GEM_GPU_DOMAINS;
Eric Anholt673a3942008-07-30 12:06:12 -07002105 }
2106
Chris Wilson0201f1e2012-07-20 12:41:01 +01002107 i915_gem_retire_requests_ring(obj->ring);
Eric Anholt673a3942008-07-30 12:06:12 -07002108 return 0;
2109}
2110
Ben Widawsky5816d642012-04-11 11:18:19 -07002111/**
Daniel Vetter30dfebf2012-06-01 15:21:23 +02002112 * Ensures that an object will eventually get non-busy by flushing any required
2113 * write domains, emitting any outstanding lazy request and retiring and
2114 * completed requests.
2115 */
2116static int
2117i915_gem_object_flush_active(struct drm_i915_gem_object *obj)
2118{
2119 int ret;
2120
2121 if (obj->active) {
Chris Wilson0201f1e2012-07-20 12:41:01 +01002122 ret = i915_gem_check_olr(obj->ring, obj->last_read_seqno);
Daniel Vetter30dfebf2012-06-01 15:21:23 +02002123 if (ret)
2124 return ret;
Chris Wilson0201f1e2012-07-20 12:41:01 +01002125
Daniel Vetter30dfebf2012-06-01 15:21:23 +02002126 i915_gem_retire_requests_ring(obj->ring);
2127 }
2128
2129 return 0;
2130}
2131
2132/**
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002133 * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
2134 * @DRM_IOCTL_ARGS: standard ioctl arguments
2135 *
2136 * Returns 0 if successful, else an error is returned with the remaining time in
2137 * the timeout parameter.
2138 * -ETIME: object is still busy after timeout
2139 * -ERESTARTSYS: signal interrupted the wait
2140 * -ENONENT: object doesn't exist
2141 * Also possible, but rare:
2142 * -EAGAIN: GPU wedged
2143 * -ENOMEM: damn
2144 * -ENODEV: Internal IRQ fail
2145 * -E?: The add request failed
2146 *
2147 * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
2148 * non-zero timeout parameter the wait ioctl will wait for the given number of
2149 * nanoseconds on an object becoming unbusy. Since the wait itself does so
2150 * without holding struct_mutex the object may become re-busied before this
2151 * function completes. A similar but shorter * race condition exists in the busy
2152 * ioctl
2153 */
2154int
2155i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
2156{
2157 struct drm_i915_gem_wait *args = data;
2158 struct drm_i915_gem_object *obj;
2159 struct intel_ring_buffer *ring = NULL;
Ben Widawskyeac1f142012-06-05 15:24:24 -07002160 struct timespec timeout_stack, *timeout = NULL;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002161 u32 seqno = 0;
2162 int ret = 0;
2163
Ben Widawskyeac1f142012-06-05 15:24:24 -07002164 if (args->timeout_ns >= 0) {
2165 timeout_stack = ns_to_timespec(args->timeout_ns);
2166 timeout = &timeout_stack;
2167 }
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002168
2169 ret = i915_mutex_lock_interruptible(dev);
2170 if (ret)
2171 return ret;
2172
2173 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->bo_handle));
2174 if (&obj->base == NULL) {
2175 mutex_unlock(&dev->struct_mutex);
2176 return -ENOENT;
2177 }
2178
Daniel Vetter30dfebf2012-06-01 15:21:23 +02002179 /* Need to make sure the object gets inactive eventually. */
2180 ret = i915_gem_object_flush_active(obj);
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002181 if (ret)
2182 goto out;
2183
2184 if (obj->active) {
Chris Wilson0201f1e2012-07-20 12:41:01 +01002185 seqno = obj->last_read_seqno;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002186 ring = obj->ring;
2187 }
2188
2189 if (seqno == 0)
2190 goto out;
2191
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002192 /* Do this after OLR check to make sure we make forward progress polling
2193 * on this IOCTL with a 0 timeout (like busy ioctl)
2194 */
2195 if (!args->timeout_ns) {
2196 ret = -ETIME;
2197 goto out;
2198 }
2199
2200 drm_gem_object_unreference(&obj->base);
2201 mutex_unlock(&dev->struct_mutex);
2202
Ben Widawskyeac1f142012-06-05 15:24:24 -07002203 ret = __wait_seqno(ring, seqno, true, timeout);
2204 if (timeout) {
2205 WARN_ON(!timespec_valid(timeout));
2206 args->timeout_ns = timespec_to_ns(timeout);
2207 }
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002208 return ret;
2209
2210out:
2211 drm_gem_object_unreference(&obj->base);
2212 mutex_unlock(&dev->struct_mutex);
2213 return ret;
2214}
2215
2216/**
Ben Widawsky5816d642012-04-11 11:18:19 -07002217 * i915_gem_object_sync - sync an object to a ring.
2218 *
2219 * @obj: object which may be in use on another ring.
2220 * @to: ring we wish to use the object on. May be NULL.
2221 *
2222 * This code is meant to abstract object synchronization with the GPU.
2223 * Calling with NULL implies synchronizing the object with the CPU
2224 * rather than a particular GPU ring.
2225 *
2226 * Returns 0 if successful, else propagates up the lower layer error.
2227 */
Ben Widawsky2911a352012-04-05 14:47:36 -07002228int
2229i915_gem_object_sync(struct drm_i915_gem_object *obj,
2230 struct intel_ring_buffer *to)
2231{
2232 struct intel_ring_buffer *from = obj->ring;
2233 u32 seqno;
2234 int ret, idx;
2235
2236 if (from == NULL || to == from)
2237 return 0;
2238
Ben Widawsky5816d642012-04-11 11:18:19 -07002239 if (to == NULL || !i915_semaphore_is_enabled(obj->base.dev))
Chris Wilson0201f1e2012-07-20 12:41:01 +01002240 return i915_gem_object_wait_rendering(obj, false);
Ben Widawsky2911a352012-04-05 14:47:36 -07002241
2242 idx = intel_ring_sync_index(from, to);
2243
Chris Wilson0201f1e2012-07-20 12:41:01 +01002244 seqno = obj->last_read_seqno;
Ben Widawsky2911a352012-04-05 14:47:36 -07002245 if (seqno <= from->sync_seqno[idx])
2246 return 0;
2247
Ben Widawskyb4aca012012-04-25 20:50:12 -07002248 ret = i915_gem_check_olr(obj->ring, seqno);
2249 if (ret)
2250 return ret;
Ben Widawsky2911a352012-04-05 14:47:36 -07002251
Ben Widawsky1500f7e2012-04-11 11:18:21 -07002252 ret = to->sync_to(to, from, seqno);
Ben Widawskye3a5a222012-04-11 11:18:20 -07002253 if (!ret)
2254 from->sync_seqno[idx] = seqno;
Ben Widawsky2911a352012-04-05 14:47:36 -07002255
Ben Widawskye3a5a222012-04-11 11:18:20 -07002256 return ret;
Ben Widawsky2911a352012-04-05 14:47:36 -07002257}
2258
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01002259static void i915_gem_object_finish_gtt(struct drm_i915_gem_object *obj)
2260{
2261 u32 old_write_domain, old_read_domains;
2262
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01002263 /* Act a barrier for all accesses through the GTT */
2264 mb();
2265
2266 /* Force a pagefault for domain tracking on next user access */
2267 i915_gem_release_mmap(obj);
2268
Keith Packardb97c3d92011-06-24 21:02:59 -07002269 if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
2270 return;
2271
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01002272 old_read_domains = obj->base.read_domains;
2273 old_write_domain = obj->base.write_domain;
2274
2275 obj->base.read_domains &= ~I915_GEM_DOMAIN_GTT;
2276 obj->base.write_domain &= ~I915_GEM_DOMAIN_GTT;
2277
2278 trace_i915_gem_object_change_domain(obj,
2279 old_read_domains,
2280 old_write_domain);
2281}
2282
Eric Anholt673a3942008-07-30 12:06:12 -07002283/**
2284 * Unbinds an object from the GTT aperture.
2285 */
Jesse Barnes0f973f22009-01-26 17:10:45 -08002286int
Chris Wilson05394f32010-11-08 19:18:58 +00002287i915_gem_object_unbind(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002288{
Daniel Vetter7bddb012012-02-09 17:15:47 +01002289 drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07002290 int ret = 0;
2291
Chris Wilson05394f32010-11-08 19:18:58 +00002292 if (obj->gtt_space == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002293 return 0;
2294
Chris Wilson31d8d652012-05-24 19:11:20 +01002295 if (obj->pin_count)
2296 return -EBUSY;
Eric Anholt673a3942008-07-30 12:06:12 -07002297
Chris Wilsonc4670ad2012-08-20 10:23:27 +01002298 BUG_ON(obj->pages == NULL);
2299
Chris Wilsona8198ee2011-04-13 22:04:09 +01002300 ret = i915_gem_object_finish_gpu(obj);
Chris Wilson1488fc02012-04-24 15:47:31 +01002301 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002302 return ret;
Chris Wilson8dc17752010-07-23 23:18:51 +01002303 /* Continue on if we fail due to EIO, the GPU is hung so we
2304 * should be safe and we need to cleanup or else we might
2305 * cause memory corruption through use-after-free.
2306 */
Chris Wilsona8198ee2011-04-13 22:04:09 +01002307
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01002308 i915_gem_object_finish_gtt(obj);
Chris Wilsona8198ee2011-04-13 22:04:09 +01002309
Daniel Vetter96b47b62009-12-15 17:50:00 +01002310 /* release the fence reg _after_ flushing */
Chris Wilsond9e86c02010-11-10 16:40:20 +00002311 ret = i915_gem_object_put_fence(obj);
Chris Wilson1488fc02012-04-24 15:47:31 +01002312 if (ret)
Chris Wilsond9e86c02010-11-10 16:40:20 +00002313 return ret;
Daniel Vetter96b47b62009-12-15 17:50:00 +01002314
Chris Wilsondb53a302011-02-03 11:57:46 +00002315 trace_i915_gem_object_unbind(obj);
2316
Daniel Vetter74898d72012-02-15 23:50:22 +01002317 if (obj->has_global_gtt_mapping)
2318 i915_gem_gtt_unbind_object(obj);
Daniel Vetter7bddb012012-02-09 17:15:47 +01002319 if (obj->has_aliasing_ppgtt_mapping) {
2320 i915_ppgtt_unbind_object(dev_priv->mm.aliasing_ppgtt, obj);
2321 obj->has_aliasing_ppgtt_mapping = 0;
2322 }
Daniel Vetter74163902012-02-15 23:50:21 +01002323 i915_gem_gtt_finish_object(obj);
Daniel Vetter7bddb012012-02-09 17:15:47 +01002324
Chris Wilson6c085a72012-08-20 11:40:46 +02002325 list_del(&obj->mm_list);
2326 list_move_tail(&obj->gtt_list, &dev_priv->mm.unbound_list);
Daniel Vetter75e9e912010-11-04 17:11:09 +01002327 /* Avoid an unnecessary call to unbind on rebind. */
Chris Wilson05394f32010-11-08 19:18:58 +00002328 obj->map_and_fenceable = true;
Eric Anholt673a3942008-07-30 12:06:12 -07002329
Chris Wilson05394f32010-11-08 19:18:58 +00002330 drm_mm_put_block(obj->gtt_space);
2331 obj->gtt_space = NULL;
2332 obj->gtt_offset = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002333
Chris Wilson6c085a72012-08-20 11:40:46 +02002334 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002335}
2336
Ben Widawskyb2da9fe2012-04-26 16:02:58 -07002337static int i915_ring_idle(struct intel_ring_buffer *ring)
Chris Wilsona56ba562010-09-28 10:07:56 +01002338{
Chris Wilson69c2fc82012-07-20 12:41:03 +01002339 if (list_empty(&ring->active_list))
Chris Wilson64193402010-10-24 12:38:05 +01002340 return 0;
2341
Ben Widawsky199b2bc2012-05-24 15:03:11 -07002342 return i915_wait_seqno(ring, i915_gem_next_request_seqno(ring));
Chris Wilsona56ba562010-09-28 10:07:56 +01002343}
2344
Ben Widawskyb2da9fe2012-04-26 16:02:58 -07002345int i915_gpu_idle(struct drm_device *dev)
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002346{
2347 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonb4519512012-05-11 14:29:30 +01002348 struct intel_ring_buffer *ring;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002349 int ret, i;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002350
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002351 /* Flush everything onto the inactive list. */
Chris Wilsonb4519512012-05-11 14:29:30 +01002352 for_each_ring(ring, dev_priv, i) {
2353 ret = i915_ring_idle(ring);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002354 if (ret)
2355 return ret;
Chris Wilsonb4519512012-05-11 14:29:30 +01002356
Ben Widawskyf2ef6eb2012-06-04 14:42:53 -07002357 ret = i915_switch_context(ring, NULL, DEFAULT_CONTEXT_ID);
2358 if (ret)
2359 return ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002360 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08002361
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002362 return 0;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002363}
2364
Chris Wilson9ce079e2012-04-17 15:31:30 +01002365static void sandybridge_write_fence_reg(struct drm_device *dev, int reg,
2366 struct drm_i915_gem_object *obj)
Eric Anholt4e901fd2009-10-26 16:44:17 -07002367{
Eric Anholt4e901fd2009-10-26 16:44:17 -07002368 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt4e901fd2009-10-26 16:44:17 -07002369 uint64_t val;
2370
Chris Wilson9ce079e2012-04-17 15:31:30 +01002371 if (obj) {
2372 u32 size = obj->gtt_space->size;
Eric Anholt4e901fd2009-10-26 16:44:17 -07002373
Chris Wilson9ce079e2012-04-17 15:31:30 +01002374 val = (uint64_t)((obj->gtt_offset + size - 4096) &
2375 0xfffff000) << 32;
2376 val |= obj->gtt_offset & 0xfffff000;
2377 val |= (uint64_t)((obj->stride / 128) - 1) <<
2378 SANDYBRIDGE_FENCE_PITCH_SHIFT;
Eric Anholt4e901fd2009-10-26 16:44:17 -07002379
Chris Wilson9ce079e2012-04-17 15:31:30 +01002380 if (obj->tiling_mode == I915_TILING_Y)
2381 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2382 val |= I965_FENCE_REG_VALID;
2383 } else
2384 val = 0;
Daniel Vetterc6642782010-11-12 13:46:18 +00002385
Chris Wilson9ce079e2012-04-17 15:31:30 +01002386 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + reg * 8, val);
2387 POSTING_READ(FENCE_REG_SANDYBRIDGE_0 + reg * 8);
Eric Anholt4e901fd2009-10-26 16:44:17 -07002388}
2389
Chris Wilson9ce079e2012-04-17 15:31:30 +01002390static void i965_write_fence_reg(struct drm_device *dev, int reg,
2391 struct drm_i915_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002392{
Jesse Barnesde151cf2008-11-12 10:03:55 -08002393 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002394 uint64_t val;
2395
Chris Wilson9ce079e2012-04-17 15:31:30 +01002396 if (obj) {
2397 u32 size = obj->gtt_space->size;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002398
Chris Wilson9ce079e2012-04-17 15:31:30 +01002399 val = (uint64_t)((obj->gtt_offset + size - 4096) &
2400 0xfffff000) << 32;
2401 val |= obj->gtt_offset & 0xfffff000;
2402 val |= ((obj->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
2403 if (obj->tiling_mode == I915_TILING_Y)
2404 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2405 val |= I965_FENCE_REG_VALID;
2406 } else
2407 val = 0;
Daniel Vetterc6642782010-11-12 13:46:18 +00002408
Chris Wilson9ce079e2012-04-17 15:31:30 +01002409 I915_WRITE64(FENCE_REG_965_0 + reg * 8, val);
2410 POSTING_READ(FENCE_REG_965_0 + reg * 8);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002411}
2412
Chris Wilson9ce079e2012-04-17 15:31:30 +01002413static void i915_write_fence_reg(struct drm_device *dev, int reg,
2414 struct drm_i915_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002415{
Jesse Barnesde151cf2008-11-12 10:03:55 -08002416 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson9ce079e2012-04-17 15:31:30 +01002417 u32 val;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002418
Chris Wilson9ce079e2012-04-17 15:31:30 +01002419 if (obj) {
2420 u32 size = obj->gtt_space->size;
2421 int pitch_val;
2422 int tile_width;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002423
Chris Wilson9ce079e2012-04-17 15:31:30 +01002424 WARN((obj->gtt_offset & ~I915_FENCE_START_MASK) ||
2425 (size & -size) != size ||
2426 (obj->gtt_offset & (size - 1)),
2427 "object 0x%08x [fenceable? %d] not 1M or pot-size (0x%08x) aligned\n",
2428 obj->gtt_offset, obj->map_and_fenceable, size);
2429
2430 if (obj->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev))
2431 tile_width = 128;
2432 else
2433 tile_width = 512;
2434
2435 /* Note: pitch better be a power of two tile widths */
2436 pitch_val = obj->stride / tile_width;
2437 pitch_val = ffs(pitch_val) - 1;
2438
2439 val = obj->gtt_offset;
2440 if (obj->tiling_mode == I915_TILING_Y)
2441 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2442 val |= I915_FENCE_SIZE_BITS(size);
2443 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2444 val |= I830_FENCE_REG_VALID;
2445 } else
2446 val = 0;
2447
2448 if (reg < 8)
2449 reg = FENCE_REG_830_0 + reg * 4;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002450 else
Chris Wilson9ce079e2012-04-17 15:31:30 +01002451 reg = FENCE_REG_945_8 + (reg - 8) * 4;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002452
Chris Wilson9ce079e2012-04-17 15:31:30 +01002453 I915_WRITE(reg, val);
2454 POSTING_READ(reg);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002455}
2456
Chris Wilson9ce079e2012-04-17 15:31:30 +01002457static void i830_write_fence_reg(struct drm_device *dev, int reg,
2458 struct drm_i915_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002459{
Jesse Barnesde151cf2008-11-12 10:03:55 -08002460 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002461 uint32_t val;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002462
Chris Wilson9ce079e2012-04-17 15:31:30 +01002463 if (obj) {
2464 u32 size = obj->gtt_space->size;
2465 uint32_t pitch_val;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002466
Chris Wilson9ce079e2012-04-17 15:31:30 +01002467 WARN((obj->gtt_offset & ~I830_FENCE_START_MASK) ||
2468 (size & -size) != size ||
2469 (obj->gtt_offset & (size - 1)),
2470 "object 0x%08x not 512K or pot-size 0x%08x aligned\n",
2471 obj->gtt_offset, size);
Eric Anholte76a16d2009-05-26 17:44:56 -07002472
Chris Wilson9ce079e2012-04-17 15:31:30 +01002473 pitch_val = obj->stride / 128;
2474 pitch_val = ffs(pitch_val) - 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002475
Chris Wilson9ce079e2012-04-17 15:31:30 +01002476 val = obj->gtt_offset;
2477 if (obj->tiling_mode == I915_TILING_Y)
2478 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2479 val |= I830_FENCE_SIZE_BITS(size);
2480 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2481 val |= I830_FENCE_REG_VALID;
2482 } else
2483 val = 0;
Daniel Vetterc6642782010-11-12 13:46:18 +00002484
Chris Wilson9ce079e2012-04-17 15:31:30 +01002485 I915_WRITE(FENCE_REG_830_0 + reg * 4, val);
2486 POSTING_READ(FENCE_REG_830_0 + reg * 4);
2487}
2488
2489static void i915_gem_write_fence(struct drm_device *dev, int reg,
2490 struct drm_i915_gem_object *obj)
2491{
2492 switch (INTEL_INFO(dev)->gen) {
2493 case 7:
2494 case 6: sandybridge_write_fence_reg(dev, reg, obj); break;
2495 case 5:
2496 case 4: i965_write_fence_reg(dev, reg, obj); break;
2497 case 3: i915_write_fence_reg(dev, reg, obj); break;
2498 case 2: i830_write_fence_reg(dev, reg, obj); break;
2499 default: break;
2500 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002501}
2502
Chris Wilson61050802012-04-17 15:31:31 +01002503static inline int fence_number(struct drm_i915_private *dev_priv,
2504 struct drm_i915_fence_reg *fence)
2505{
2506 return fence - dev_priv->fence_regs;
2507}
2508
2509static void i915_gem_object_update_fence(struct drm_i915_gem_object *obj,
2510 struct drm_i915_fence_reg *fence,
2511 bool enable)
2512{
2513 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2514 int reg = fence_number(dev_priv, fence);
2515
2516 i915_gem_write_fence(obj->base.dev, reg, enable ? obj : NULL);
2517
2518 if (enable) {
2519 obj->fence_reg = reg;
2520 fence->obj = obj;
2521 list_move_tail(&fence->lru_list, &dev_priv->mm.fence_list);
2522 } else {
2523 obj->fence_reg = I915_FENCE_REG_NONE;
2524 fence->obj = NULL;
2525 list_del_init(&fence->lru_list);
2526 }
2527}
2528
Chris Wilsond9e86c02010-11-10 16:40:20 +00002529static int
Chris Wilsona360bb12012-04-17 15:31:25 +01002530i915_gem_object_flush_fence(struct drm_i915_gem_object *obj)
Chris Wilsond9e86c02010-11-10 16:40:20 +00002531{
Chris Wilson1c293ea2012-04-17 15:31:27 +01002532 if (obj->last_fenced_seqno) {
Chris Wilson86d5bc32012-07-20 12:41:04 +01002533 int ret = i915_wait_seqno(obj->ring, obj->last_fenced_seqno);
Chris Wilson18991842012-04-17 15:31:29 +01002534 if (ret)
2535 return ret;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002536
2537 obj->last_fenced_seqno = 0;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002538 }
2539
Chris Wilson63256ec2011-01-04 18:42:07 +00002540 /* Ensure that all CPU reads are completed before installing a fence
2541 * and all writes before removing the fence.
2542 */
2543 if (obj->base.read_domains & I915_GEM_DOMAIN_GTT)
2544 mb();
2545
Chris Wilson86d5bc32012-07-20 12:41:04 +01002546 obj->fenced_gpu_access = false;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002547 return 0;
2548}
2549
2550int
2551i915_gem_object_put_fence(struct drm_i915_gem_object *obj)
2552{
Chris Wilson61050802012-04-17 15:31:31 +01002553 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002554 int ret;
2555
Chris Wilsona360bb12012-04-17 15:31:25 +01002556 ret = i915_gem_object_flush_fence(obj);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002557 if (ret)
2558 return ret;
2559
Chris Wilson61050802012-04-17 15:31:31 +01002560 if (obj->fence_reg == I915_FENCE_REG_NONE)
2561 return 0;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002562
Chris Wilson61050802012-04-17 15:31:31 +01002563 i915_gem_object_update_fence(obj,
2564 &dev_priv->fence_regs[obj->fence_reg],
2565 false);
2566 i915_gem_object_fence_lost(obj);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002567
2568 return 0;
2569}
2570
2571static struct drm_i915_fence_reg *
Chris Wilsona360bb12012-04-17 15:31:25 +01002572i915_find_fence_reg(struct drm_device *dev)
Daniel Vetterae3db242010-02-19 11:51:58 +01002573{
Daniel Vetterae3db242010-02-19 11:51:58 +01002574 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson8fe301a2012-04-17 15:31:28 +01002575 struct drm_i915_fence_reg *reg, *avail;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002576 int i;
Daniel Vetterae3db242010-02-19 11:51:58 +01002577
2578 /* First try to find a free reg */
Chris Wilsond9e86c02010-11-10 16:40:20 +00002579 avail = NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002580 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2581 reg = &dev_priv->fence_regs[i];
2582 if (!reg->obj)
Chris Wilsond9e86c02010-11-10 16:40:20 +00002583 return reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01002584
Chris Wilson1690e1e2011-12-14 13:57:08 +01002585 if (!reg->pin_count)
Chris Wilsond9e86c02010-11-10 16:40:20 +00002586 avail = reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01002587 }
2588
Chris Wilsond9e86c02010-11-10 16:40:20 +00002589 if (avail == NULL)
2590 return NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002591
2592 /* None available, try to steal one or wait for a user to finish */
Chris Wilsond9e86c02010-11-10 16:40:20 +00002593 list_for_each_entry(reg, &dev_priv->mm.fence_list, lru_list) {
Chris Wilson1690e1e2011-12-14 13:57:08 +01002594 if (reg->pin_count)
Daniel Vetterae3db242010-02-19 11:51:58 +01002595 continue;
2596
Chris Wilson8fe301a2012-04-17 15:31:28 +01002597 return reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01002598 }
2599
Chris Wilson8fe301a2012-04-17 15:31:28 +01002600 return NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002601}
2602
Jesse Barnesde151cf2008-11-12 10:03:55 -08002603/**
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002604 * i915_gem_object_get_fence - set up fencing for an object
Jesse Barnesde151cf2008-11-12 10:03:55 -08002605 * @obj: object to map through a fence reg
2606 *
2607 * When mapping objects through the GTT, userspace wants to be able to write
2608 * to them without having to worry about swizzling if the object is tiled.
Jesse Barnesde151cf2008-11-12 10:03:55 -08002609 * This function walks the fence regs looking for a free one for @obj,
2610 * stealing one if it can't find any.
2611 *
2612 * It then sets up the reg based on the object's properties: address, pitch
2613 * and tiling format.
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002614 *
2615 * For an untiled surface, this removes any existing fence.
Jesse Barnesde151cf2008-11-12 10:03:55 -08002616 */
Chris Wilson8c4b8c32009-06-17 22:08:52 +01002617int
Chris Wilson06d98132012-04-17 15:31:24 +01002618i915_gem_object_get_fence(struct drm_i915_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002619{
Chris Wilson05394f32010-11-08 19:18:58 +00002620 struct drm_device *dev = obj->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002621 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson14415742012-04-17 15:31:33 +01002622 bool enable = obj->tiling_mode != I915_TILING_NONE;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002623 struct drm_i915_fence_reg *reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01002624 int ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002625
Chris Wilson14415742012-04-17 15:31:33 +01002626 /* Have we updated the tiling parameters upon the object and so
2627 * will need to serialise the write to the associated fence register?
2628 */
Chris Wilson5d82e3e2012-04-21 16:23:23 +01002629 if (obj->fence_dirty) {
Chris Wilson14415742012-04-17 15:31:33 +01002630 ret = i915_gem_object_flush_fence(obj);
2631 if (ret)
2632 return ret;
2633 }
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002634
Chris Wilsond9e86c02010-11-10 16:40:20 +00002635 /* Just update our place in the LRU if our fence is getting reused. */
Chris Wilson05394f32010-11-08 19:18:58 +00002636 if (obj->fence_reg != I915_FENCE_REG_NONE) {
2637 reg = &dev_priv->fence_regs[obj->fence_reg];
Chris Wilson5d82e3e2012-04-21 16:23:23 +01002638 if (!obj->fence_dirty) {
Chris Wilson14415742012-04-17 15:31:33 +01002639 list_move_tail(&reg->lru_list,
2640 &dev_priv->mm.fence_list);
2641 return 0;
2642 }
2643 } else if (enable) {
2644 reg = i915_find_fence_reg(dev);
2645 if (reg == NULL)
2646 return -EDEADLK;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002647
Chris Wilson14415742012-04-17 15:31:33 +01002648 if (reg->obj) {
2649 struct drm_i915_gem_object *old = reg->obj;
2650
2651 ret = i915_gem_object_flush_fence(old);
Chris Wilson29c5a582011-03-17 15:23:22 +00002652 if (ret)
2653 return ret;
2654
Chris Wilson14415742012-04-17 15:31:33 +01002655 i915_gem_object_fence_lost(old);
Chris Wilson29c5a582011-03-17 15:23:22 +00002656 }
Chris Wilson14415742012-04-17 15:31:33 +01002657 } else
Eric Anholta09ba7f2009-08-29 12:49:51 -07002658 return 0;
Eric Anholta09ba7f2009-08-29 12:49:51 -07002659
Chris Wilson14415742012-04-17 15:31:33 +01002660 i915_gem_object_update_fence(obj, reg, enable);
Chris Wilson5d82e3e2012-04-21 16:23:23 +01002661 obj->fence_dirty = false;
Chris Wilson14415742012-04-17 15:31:33 +01002662
Chris Wilson9ce079e2012-04-17 15:31:30 +01002663 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002664}
2665
Chris Wilson42d6ab42012-07-26 11:49:32 +01002666static bool i915_gem_valid_gtt_space(struct drm_device *dev,
2667 struct drm_mm_node *gtt_space,
2668 unsigned long cache_level)
2669{
2670 struct drm_mm_node *other;
2671
2672 /* On non-LLC machines we have to be careful when putting differing
2673 * types of snoopable memory together to avoid the prefetcher
2674 * crossing memory domains and dieing.
2675 */
2676 if (HAS_LLC(dev))
2677 return true;
2678
2679 if (gtt_space == NULL)
2680 return true;
2681
2682 if (list_empty(&gtt_space->node_list))
2683 return true;
2684
2685 other = list_entry(gtt_space->node_list.prev, struct drm_mm_node, node_list);
2686 if (other->allocated && !other->hole_follows && other->color != cache_level)
2687 return false;
2688
2689 other = list_entry(gtt_space->node_list.next, struct drm_mm_node, node_list);
2690 if (other->allocated && !gtt_space->hole_follows && other->color != cache_level)
2691 return false;
2692
2693 return true;
2694}
2695
2696static void i915_gem_verify_gtt(struct drm_device *dev)
2697{
2698#if WATCH_GTT
2699 struct drm_i915_private *dev_priv = dev->dev_private;
2700 struct drm_i915_gem_object *obj;
2701 int err = 0;
2702
2703 list_for_each_entry(obj, &dev_priv->mm.gtt_list, gtt_list) {
2704 if (obj->gtt_space == NULL) {
2705 printk(KERN_ERR "object found on GTT list with no space reserved\n");
2706 err++;
2707 continue;
2708 }
2709
2710 if (obj->cache_level != obj->gtt_space->color) {
2711 printk(KERN_ERR "object reserved space [%08lx, %08lx] with wrong color, cache_level=%x, color=%lx\n",
2712 obj->gtt_space->start,
2713 obj->gtt_space->start + obj->gtt_space->size,
2714 obj->cache_level,
2715 obj->gtt_space->color);
2716 err++;
2717 continue;
2718 }
2719
2720 if (!i915_gem_valid_gtt_space(dev,
2721 obj->gtt_space,
2722 obj->cache_level)) {
2723 printk(KERN_ERR "invalid GTT space found at [%08lx, %08lx] - color=%x\n",
2724 obj->gtt_space->start,
2725 obj->gtt_space->start + obj->gtt_space->size,
2726 obj->cache_level);
2727 err++;
2728 continue;
2729 }
2730 }
2731
2732 WARN_ON(err);
2733#endif
2734}
2735
Jesse Barnesde151cf2008-11-12 10:03:55 -08002736/**
Eric Anholt673a3942008-07-30 12:06:12 -07002737 * Finds free space in the GTT aperture and binds the object there.
2738 */
2739static int
Chris Wilson05394f32010-11-08 19:18:58 +00002740i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
Daniel Vetter920afa72010-09-16 17:54:23 +02002741 unsigned alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01002742 bool map_and_fenceable)
Eric Anholt673a3942008-07-30 12:06:12 -07002743{
Chris Wilson05394f32010-11-08 19:18:58 +00002744 struct drm_device *dev = obj->base.dev;
Eric Anholt673a3942008-07-30 12:06:12 -07002745 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07002746 struct drm_mm_node *free_space;
Daniel Vetter5e783302010-11-14 22:32:36 +01002747 u32 size, fence_size, fence_alignment, unfenced_alignment;
Daniel Vetter75e9e912010-11-04 17:11:09 +01002748 bool mappable, fenceable;
Chris Wilson07f73f62009-09-14 16:50:30 +01002749 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002750
Chris Wilson05394f32010-11-08 19:18:58 +00002751 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01002752 DRM_ERROR("Attempting to bind a purgeable object\n");
2753 return -EINVAL;
2754 }
2755
Chris Wilsone28f8712011-07-18 13:11:49 -07002756 fence_size = i915_gem_get_gtt_size(dev,
2757 obj->base.size,
2758 obj->tiling_mode);
2759 fence_alignment = i915_gem_get_gtt_alignment(dev,
2760 obj->base.size,
2761 obj->tiling_mode);
2762 unfenced_alignment =
2763 i915_gem_get_unfenced_gtt_alignment(dev,
2764 obj->base.size,
2765 obj->tiling_mode);
Chris Wilsona00b10c2010-09-24 21:15:47 +01002766
Eric Anholt673a3942008-07-30 12:06:12 -07002767 if (alignment == 0)
Daniel Vetter5e783302010-11-14 22:32:36 +01002768 alignment = map_and_fenceable ? fence_alignment :
2769 unfenced_alignment;
Daniel Vetter75e9e912010-11-04 17:11:09 +01002770 if (map_and_fenceable && alignment & (fence_alignment - 1)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002771 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2772 return -EINVAL;
2773 }
2774
Chris Wilson05394f32010-11-08 19:18:58 +00002775 size = map_and_fenceable ? fence_size : obj->base.size;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002776
Chris Wilson654fc602010-05-27 13:18:21 +01002777 /* If the object is bigger than the entire aperture, reject it early
2778 * before evicting everything in a vain attempt to find space.
2779 */
Chris Wilson05394f32010-11-08 19:18:58 +00002780 if (obj->base.size >
Daniel Vetter75e9e912010-11-04 17:11:09 +01002781 (map_and_fenceable ? dev_priv->mm.gtt_mappable_end : dev_priv->mm.gtt_total)) {
Chris Wilson654fc602010-05-27 13:18:21 +01002782 DRM_ERROR("Attempting to bind an object larger than the aperture\n");
2783 return -E2BIG;
2784 }
2785
Chris Wilson6c085a72012-08-20 11:40:46 +02002786 ret = i915_gem_object_get_pages_gtt(obj);
2787 if (ret)
2788 return ret;
2789
Eric Anholt673a3942008-07-30 12:06:12 -07002790 search_free:
Daniel Vetter75e9e912010-11-04 17:11:09 +01002791 if (map_and_fenceable)
Daniel Vetter920afa72010-09-16 17:54:23 +02002792 free_space =
Chris Wilson42d6ab42012-07-26 11:49:32 +01002793 drm_mm_search_free_in_range_color(&dev_priv->mm.gtt_space,
2794 size, alignment, obj->cache_level,
2795 0, dev_priv->mm.gtt_mappable_end,
2796 false);
Daniel Vetter920afa72010-09-16 17:54:23 +02002797 else
Chris Wilson42d6ab42012-07-26 11:49:32 +01002798 free_space = drm_mm_search_free_color(&dev_priv->mm.gtt_space,
2799 size, alignment, obj->cache_level,
2800 false);
Daniel Vetter920afa72010-09-16 17:54:23 +02002801
2802 if (free_space != NULL) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01002803 if (map_and_fenceable)
Chris Wilson05394f32010-11-08 19:18:58 +00002804 obj->gtt_space =
Daniel Vetter920afa72010-09-16 17:54:23 +02002805 drm_mm_get_block_range_generic(free_space,
Chris Wilson42d6ab42012-07-26 11:49:32 +01002806 size, alignment, obj->cache_level,
Chris Wilson6b9d89b2012-07-10 11:15:23 +01002807 0, dev_priv->mm.gtt_mappable_end,
Chris Wilson42d6ab42012-07-26 11:49:32 +01002808 false);
Daniel Vetter920afa72010-09-16 17:54:23 +02002809 else
Chris Wilson05394f32010-11-08 19:18:58 +00002810 obj->gtt_space =
Chris Wilson42d6ab42012-07-26 11:49:32 +01002811 drm_mm_get_block_generic(free_space,
2812 size, alignment, obj->cache_level,
2813 false);
Daniel Vetter920afa72010-09-16 17:54:23 +02002814 }
Chris Wilson05394f32010-11-08 19:18:58 +00002815 if (obj->gtt_space == NULL) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01002816 ret = i915_gem_evict_something(dev, size, alignment,
Chris Wilson42d6ab42012-07-26 11:49:32 +01002817 obj->cache_level,
Daniel Vetter75e9e912010-11-04 17:11:09 +01002818 map_and_fenceable);
Chris Wilson97311292009-09-21 00:22:34 +01002819 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002820 return ret;
Chris Wilson97311292009-09-21 00:22:34 +01002821
Eric Anholt673a3942008-07-30 12:06:12 -07002822 goto search_free;
2823 }
Chris Wilson42d6ab42012-07-26 11:49:32 +01002824 if (WARN_ON(!i915_gem_valid_gtt_space(dev,
2825 obj->gtt_space,
2826 obj->cache_level))) {
2827 drm_mm_put_block(obj->gtt_space);
2828 obj->gtt_space = NULL;
2829 return -EINVAL;
2830 }
Eric Anholt673a3942008-07-30 12:06:12 -07002831
Eric Anholt673a3942008-07-30 12:06:12 -07002832
Daniel Vetter74163902012-02-15 23:50:21 +01002833 ret = i915_gem_gtt_prepare_object(obj);
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002834 if (ret) {
Chris Wilson05394f32010-11-08 19:18:58 +00002835 drm_mm_put_block(obj->gtt_space);
2836 obj->gtt_space = NULL;
Chris Wilson6c085a72012-08-20 11:40:46 +02002837 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002838 }
Eric Anholt673a3942008-07-30 12:06:12 -07002839
Daniel Vetter0ebb9822012-02-15 23:50:24 +01002840 if (!dev_priv->mm.aliasing_ppgtt)
2841 i915_gem_gtt_bind_object(obj, obj->cache_level);
Eric Anholt673a3942008-07-30 12:06:12 -07002842
Chris Wilson6c085a72012-08-20 11:40:46 +02002843 list_move_tail(&obj->gtt_list, &dev_priv->mm.bound_list);
Chris Wilson05394f32010-11-08 19:18:58 +00002844 list_add_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
Chris Wilsonbf1a1092010-08-07 11:01:20 +01002845
Chris Wilson6299f992010-11-24 12:23:44 +00002846 obj->gtt_offset = obj->gtt_space->start;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002847
Daniel Vetter75e9e912010-11-04 17:11:09 +01002848 fenceable =
Chris Wilson05394f32010-11-08 19:18:58 +00002849 obj->gtt_space->size == fence_size &&
Akshay Joshi0206e352011-08-16 15:34:10 -04002850 (obj->gtt_space->start & (fence_alignment - 1)) == 0;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002851
Daniel Vetter75e9e912010-11-04 17:11:09 +01002852 mappable =
Chris Wilson05394f32010-11-08 19:18:58 +00002853 obj->gtt_offset + obj->base.size <= dev_priv->mm.gtt_mappable_end;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002854
Chris Wilson05394f32010-11-08 19:18:58 +00002855 obj->map_and_fenceable = mappable && fenceable;
Daniel Vetter75e9e912010-11-04 17:11:09 +01002856
Chris Wilsondb53a302011-02-03 11:57:46 +00002857 trace_i915_gem_object_bind(obj, map_and_fenceable);
Chris Wilson42d6ab42012-07-26 11:49:32 +01002858 i915_gem_verify_gtt(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07002859 return 0;
2860}
2861
2862void
Chris Wilson05394f32010-11-08 19:18:58 +00002863i915_gem_clflush_object(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002864{
Eric Anholt673a3942008-07-30 12:06:12 -07002865 /* If we don't have a page list set up, then we're not pinned
2866 * to GPU, and we can ignore the cache flush because it'll happen
2867 * again at bind time.
2868 */
Chris Wilson05394f32010-11-08 19:18:58 +00002869 if (obj->pages == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002870 return;
2871
Chris Wilson9c23f7f2011-03-29 16:59:52 -07002872 /* If the GPU is snooping the contents of the CPU cache,
2873 * we do not need to manually clear the CPU cache lines. However,
2874 * the caches are only snooped when the render cache is
2875 * flushed/invalidated. As we always have to emit invalidations
2876 * and flushes when moving into and out of the RENDER domain, correct
2877 * snooping behaviour occurs naturally as the result of our domain
2878 * tracking.
2879 */
2880 if (obj->cache_level != I915_CACHE_NONE)
2881 return;
2882
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002883 trace_i915_gem_object_clflush(obj);
Eric Anholtcfa16a02009-05-26 18:46:16 -07002884
Chris Wilson05394f32010-11-08 19:18:58 +00002885 drm_clflush_pages(obj->pages, obj->base.size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002886}
2887
Eric Anholte47c68e2008-11-14 13:35:19 -08002888/** Flushes the GTT write domain for the object if it's dirty. */
2889static void
Chris Wilson05394f32010-11-08 19:18:58 +00002890i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08002891{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002892 uint32_t old_write_domain;
2893
Chris Wilson05394f32010-11-08 19:18:58 +00002894 if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
Eric Anholte47c68e2008-11-14 13:35:19 -08002895 return;
2896
Chris Wilson63256ec2011-01-04 18:42:07 +00002897 /* No actual flushing is required for the GTT write domain. Writes
Eric Anholte47c68e2008-11-14 13:35:19 -08002898 * to it immediately go to main memory as far as we know, so there's
2899 * no chipset flush. It also doesn't land in render cache.
Chris Wilson63256ec2011-01-04 18:42:07 +00002900 *
2901 * However, we do have to enforce the order so that all writes through
2902 * the GTT land before any writes to the device, such as updates to
2903 * the GATT itself.
Eric Anholte47c68e2008-11-14 13:35:19 -08002904 */
Chris Wilson63256ec2011-01-04 18:42:07 +00002905 wmb();
2906
Chris Wilson05394f32010-11-08 19:18:58 +00002907 old_write_domain = obj->base.write_domain;
2908 obj->base.write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002909
2910 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00002911 obj->base.read_domains,
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002912 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002913}
2914
2915/** Flushes the CPU write domain for the object if it's dirty. */
2916static void
Chris Wilson05394f32010-11-08 19:18:58 +00002917i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08002918{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002919 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002920
Chris Wilson05394f32010-11-08 19:18:58 +00002921 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
Eric Anholte47c68e2008-11-14 13:35:19 -08002922 return;
2923
2924 i915_gem_clflush_object(obj);
Daniel Vetter40ce6572010-11-05 18:12:18 +01002925 intel_gtt_chipset_flush();
Chris Wilson05394f32010-11-08 19:18:58 +00002926 old_write_domain = obj->base.write_domain;
2927 obj->base.write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002928
2929 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00002930 obj->base.read_domains,
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002931 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002932}
2933
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002934/**
2935 * Moves a single object to the GTT read, and possibly write domain.
2936 *
2937 * This function returns when the move is complete, including waiting on
2938 * flushes to occur.
2939 */
Jesse Barnes79e53942008-11-07 14:24:08 -08002940int
Chris Wilson20217462010-11-23 15:26:33 +00002941i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002942{
Chris Wilson8325a092012-04-24 15:52:35 +01002943 drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002944 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002945 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002946
Eric Anholt02354392008-11-26 13:58:13 -08002947 /* Not valid to be called on unbound objects. */
Chris Wilson05394f32010-11-08 19:18:58 +00002948 if (obj->gtt_space == NULL)
Eric Anholt02354392008-11-26 13:58:13 -08002949 return -EINVAL;
2950
Chris Wilson8d7e3de2011-02-07 15:23:02 +00002951 if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
2952 return 0;
2953
Chris Wilson0201f1e2012-07-20 12:41:01 +01002954 ret = i915_gem_object_wait_rendering(obj, !write);
2955 if (ret)
2956 return ret;
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002957
Chris Wilson72133422010-09-13 23:56:38 +01002958 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002959
Chris Wilson05394f32010-11-08 19:18:58 +00002960 old_write_domain = obj->base.write_domain;
2961 old_read_domains = obj->base.read_domains;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002962
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002963 /* It should now be out of any other write domains, and we can update
2964 * the domain values for our changes.
2965 */
Chris Wilson05394f32010-11-08 19:18:58 +00002966 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2967 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002968 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00002969 obj->base.read_domains = I915_GEM_DOMAIN_GTT;
2970 obj->base.write_domain = I915_GEM_DOMAIN_GTT;
2971 obj->dirty = 1;
Eric Anholte47c68e2008-11-14 13:35:19 -08002972 }
2973
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002974 trace_i915_gem_object_change_domain(obj,
2975 old_read_domains,
2976 old_write_domain);
2977
Chris Wilson8325a092012-04-24 15:52:35 +01002978 /* And bump the LRU for this access */
2979 if (i915_gem_object_is_inactive(obj))
2980 list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
2981
Eric Anholte47c68e2008-11-14 13:35:19 -08002982 return 0;
2983}
2984
Chris Wilsone4ffd172011-04-04 09:44:39 +01002985int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
2986 enum i915_cache_level cache_level)
2987{
Daniel Vetter7bddb012012-02-09 17:15:47 +01002988 struct drm_device *dev = obj->base.dev;
2989 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsone4ffd172011-04-04 09:44:39 +01002990 int ret;
2991
2992 if (obj->cache_level == cache_level)
2993 return 0;
2994
2995 if (obj->pin_count) {
2996 DRM_DEBUG("can not change the cache level of pinned objects\n");
2997 return -EBUSY;
2998 }
2999
Chris Wilson42d6ab42012-07-26 11:49:32 +01003000 if (!i915_gem_valid_gtt_space(dev, obj->gtt_space, cache_level)) {
3001 ret = i915_gem_object_unbind(obj);
3002 if (ret)
3003 return ret;
3004 }
3005
Chris Wilsone4ffd172011-04-04 09:44:39 +01003006 if (obj->gtt_space) {
3007 ret = i915_gem_object_finish_gpu(obj);
3008 if (ret)
3009 return ret;
3010
3011 i915_gem_object_finish_gtt(obj);
3012
3013 /* Before SandyBridge, you could not use tiling or fence
3014 * registers with snooped memory, so relinquish any fences
3015 * currently pointing to our region in the aperture.
3016 */
Chris Wilson42d6ab42012-07-26 11:49:32 +01003017 if (INTEL_INFO(dev)->gen < 6) {
Chris Wilsone4ffd172011-04-04 09:44:39 +01003018 ret = i915_gem_object_put_fence(obj);
3019 if (ret)
3020 return ret;
3021 }
3022
Daniel Vetter74898d72012-02-15 23:50:22 +01003023 if (obj->has_global_gtt_mapping)
3024 i915_gem_gtt_bind_object(obj, cache_level);
Daniel Vetter7bddb012012-02-09 17:15:47 +01003025 if (obj->has_aliasing_ppgtt_mapping)
3026 i915_ppgtt_bind_object(dev_priv->mm.aliasing_ppgtt,
3027 obj, cache_level);
Chris Wilson42d6ab42012-07-26 11:49:32 +01003028
3029 obj->gtt_space->color = cache_level;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003030 }
3031
3032 if (cache_level == I915_CACHE_NONE) {
3033 u32 old_read_domains, old_write_domain;
3034
3035 /* If we're coming from LLC cached, then we haven't
3036 * actually been tracking whether the data is in the
3037 * CPU cache or not, since we only allow one bit set
3038 * in obj->write_domain and have been skipping the clflushes.
3039 * Just set it to the CPU cache for now.
3040 */
3041 WARN_ON(obj->base.write_domain & ~I915_GEM_DOMAIN_CPU);
3042 WARN_ON(obj->base.read_domains & ~I915_GEM_DOMAIN_CPU);
3043
3044 old_read_domains = obj->base.read_domains;
3045 old_write_domain = obj->base.write_domain;
3046
3047 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3048 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
3049
3050 trace_i915_gem_object_change_domain(obj,
3051 old_read_domains,
3052 old_write_domain);
3053 }
3054
3055 obj->cache_level = cache_level;
Chris Wilson42d6ab42012-07-26 11:49:32 +01003056 i915_gem_verify_gtt(dev);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003057 return 0;
3058}
3059
Chris Wilsone6994ae2012-07-10 10:27:08 +01003060int i915_gem_get_cacheing_ioctl(struct drm_device *dev, void *data,
3061 struct drm_file *file)
3062{
3063 struct drm_i915_gem_cacheing *args = data;
3064 struct drm_i915_gem_object *obj;
3065 int ret;
3066
3067 ret = i915_mutex_lock_interruptible(dev);
3068 if (ret)
3069 return ret;
3070
3071 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
3072 if (&obj->base == NULL) {
3073 ret = -ENOENT;
3074 goto unlock;
3075 }
3076
3077 args->cacheing = obj->cache_level != I915_CACHE_NONE;
3078
3079 drm_gem_object_unreference(&obj->base);
3080unlock:
3081 mutex_unlock(&dev->struct_mutex);
3082 return ret;
3083}
3084
3085int i915_gem_set_cacheing_ioctl(struct drm_device *dev, void *data,
3086 struct drm_file *file)
3087{
3088 struct drm_i915_gem_cacheing *args = data;
3089 struct drm_i915_gem_object *obj;
3090 enum i915_cache_level level;
3091 int ret;
3092
3093 ret = i915_mutex_lock_interruptible(dev);
3094 if (ret)
3095 return ret;
3096
3097 switch (args->cacheing) {
3098 case I915_CACHEING_NONE:
3099 level = I915_CACHE_NONE;
3100 break;
3101 case I915_CACHEING_CACHED:
3102 level = I915_CACHE_LLC;
3103 break;
3104 default:
3105 return -EINVAL;
3106 }
3107
3108 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
3109 if (&obj->base == NULL) {
3110 ret = -ENOENT;
3111 goto unlock;
3112 }
3113
3114 ret = i915_gem_object_set_cache_level(obj, level);
3115
3116 drm_gem_object_unreference(&obj->base);
3117unlock:
3118 mutex_unlock(&dev->struct_mutex);
3119 return ret;
3120}
3121
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003122/*
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003123 * Prepare buffer for display plane (scanout, cursors, etc).
3124 * Can be called from an uninterruptible phase (modesetting) and allows
3125 * any flushes to be pipelined (for pageflips).
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003126 */
3127int
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003128i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
3129 u32 alignment,
Chris Wilson919926a2010-11-12 13:42:53 +00003130 struct intel_ring_buffer *pipelined)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003131{
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003132 u32 old_read_domains, old_write_domain;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003133 int ret;
3134
Chris Wilson0be73282010-12-06 14:36:27 +00003135 if (pipelined != obj->ring) {
Ben Widawsky2911a352012-04-05 14:47:36 -07003136 ret = i915_gem_object_sync(obj, pipelined);
3137 if (ret)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003138 return ret;
3139 }
3140
Eric Anholta7ef0642011-03-29 16:59:54 -07003141 /* The display engine is not coherent with the LLC cache on gen6. As
3142 * a result, we make sure that the pinning that is about to occur is
3143 * done with uncached PTEs. This is lowest common denominator for all
3144 * chipsets.
3145 *
3146 * However for gen6+, we could do better by using the GFDT bit instead
3147 * of uncaching, which would allow us to flush all the LLC-cached data
3148 * with that bit in the PTE to main memory with just one PIPE_CONTROL.
3149 */
3150 ret = i915_gem_object_set_cache_level(obj, I915_CACHE_NONE);
3151 if (ret)
3152 return ret;
3153
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003154 /* As the user may map the buffer once pinned in the display plane
3155 * (e.g. libkms for the bootup splash), we have to ensure that we
3156 * always use map_and_fenceable for all scanout buffers.
3157 */
3158 ret = i915_gem_object_pin(obj, alignment, true);
3159 if (ret)
3160 return ret;
3161
Chris Wilsonb118c1e2010-05-27 13:18:14 +01003162 i915_gem_object_flush_cpu_write_domain(obj);
3163
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003164 old_write_domain = obj->base.write_domain;
Chris Wilson05394f32010-11-08 19:18:58 +00003165 old_read_domains = obj->base.read_domains;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003166
3167 /* It should now be out of any other write domains, and we can update
3168 * the domain values for our changes.
3169 */
Chris Wilsone5f1d962012-07-20 12:41:00 +01003170 obj->base.write_domain = 0;
Chris Wilson05394f32010-11-08 19:18:58 +00003171 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003172
3173 trace_i915_gem_object_change_domain(obj,
3174 old_read_domains,
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003175 old_write_domain);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003176
3177 return 0;
3178}
3179
Chris Wilson85345512010-11-13 09:49:11 +00003180int
Chris Wilsona8198ee2011-04-13 22:04:09 +01003181i915_gem_object_finish_gpu(struct drm_i915_gem_object *obj)
Chris Wilson85345512010-11-13 09:49:11 +00003182{
Chris Wilson88241782011-01-07 17:09:48 +00003183 int ret;
3184
Chris Wilsona8198ee2011-04-13 22:04:09 +01003185 if ((obj->base.read_domains & I915_GEM_GPU_DOMAINS) == 0)
Chris Wilson85345512010-11-13 09:49:11 +00003186 return 0;
3187
Chris Wilson0201f1e2012-07-20 12:41:01 +01003188 ret = i915_gem_object_wait_rendering(obj, false);
Chris Wilsonc501ae72011-12-14 13:57:23 +01003189 if (ret)
3190 return ret;
3191
Chris Wilsona8198ee2011-04-13 22:04:09 +01003192 /* Ensure that we invalidate the GPU's caches and TLBs. */
3193 obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
Chris Wilsonc501ae72011-12-14 13:57:23 +01003194 return 0;
Chris Wilson85345512010-11-13 09:49:11 +00003195}
3196
Eric Anholte47c68e2008-11-14 13:35:19 -08003197/**
3198 * Moves a single object to the CPU read, and possibly write domain.
3199 *
3200 * This function returns when the move is complete, including waiting on
3201 * flushes to occur.
3202 */
Chris Wilsondabdfe02012-03-26 10:10:27 +02003203int
Chris Wilson919926a2010-11-12 13:42:53 +00003204i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholte47c68e2008-11-14 13:35:19 -08003205{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003206 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003207 int ret;
3208
Chris Wilson8d7e3de2011-02-07 15:23:02 +00003209 if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
3210 return 0;
3211
Chris Wilson0201f1e2012-07-20 12:41:01 +01003212 ret = i915_gem_object_wait_rendering(obj, !write);
3213 if (ret)
3214 return ret;
Eric Anholte47c68e2008-11-14 13:35:19 -08003215
3216 i915_gem_object_flush_gtt_write_domain(obj);
3217
Chris Wilson05394f32010-11-08 19:18:58 +00003218 old_write_domain = obj->base.write_domain;
3219 old_read_domains = obj->base.read_domains;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003220
Eric Anholte47c68e2008-11-14 13:35:19 -08003221 /* Flush the CPU cache if it's still invalid. */
Chris Wilson05394f32010-11-08 19:18:58 +00003222 if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
Eric Anholte47c68e2008-11-14 13:35:19 -08003223 i915_gem_clflush_object(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003224
Chris Wilson05394f32010-11-08 19:18:58 +00003225 obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003226 }
3227
3228 /* It should now be out of any other write domains, and we can update
3229 * the domain values for our changes.
3230 */
Chris Wilson05394f32010-11-08 19:18:58 +00003231 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
Eric Anholte47c68e2008-11-14 13:35:19 -08003232
3233 /* If we're writing through the CPU, then the GPU read domains will
3234 * need to be invalidated at next use.
3235 */
3236 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00003237 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3238 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003239 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003240
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003241 trace_i915_gem_object_change_domain(obj,
3242 old_read_domains,
3243 old_write_domain);
3244
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003245 return 0;
3246}
3247
Eric Anholt673a3942008-07-30 12:06:12 -07003248/* Throttle our rendering by waiting until the ring has completed our requests
3249 * emitted over 20 msec ago.
3250 *
Eric Anholtb9624422009-06-03 07:27:35 +00003251 * Note that if we were to use the current jiffies each time around the loop,
3252 * we wouldn't escape the function with any frames outstanding if the time to
3253 * render a frame was over 20ms.
3254 *
Eric Anholt673a3942008-07-30 12:06:12 -07003255 * This should get us reasonable parallelism between CPU and GPU but also
3256 * relatively low latency when blocking on a particular request to finish.
3257 */
3258static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003259i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003260{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003261 struct drm_i915_private *dev_priv = dev->dev_private;
3262 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00003263 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003264 struct drm_i915_gem_request *request;
3265 struct intel_ring_buffer *ring = NULL;
3266 u32 seqno = 0;
3267 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003268
Chris Wilsone110e8d2011-01-26 15:39:14 +00003269 if (atomic_read(&dev_priv->mm.wedged))
3270 return -EIO;
3271
Chris Wilson1c255952010-09-26 11:03:27 +01003272 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003273 list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
Eric Anholtb9624422009-06-03 07:27:35 +00003274 if (time_after_eq(request->emitted_jiffies, recent_enough))
3275 break;
3276
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003277 ring = request->ring;
3278 seqno = request->seqno;
Eric Anholtb9624422009-06-03 07:27:35 +00003279 }
Chris Wilson1c255952010-09-26 11:03:27 +01003280 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003281
3282 if (seqno == 0)
3283 return 0;
3284
Ben Widawsky5c81fe852012-05-24 15:03:08 -07003285 ret = __wait_seqno(ring, seqno, true, NULL);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003286 if (ret == 0)
3287 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
Eric Anholtb9624422009-06-03 07:27:35 +00003288
Eric Anholt673a3942008-07-30 12:06:12 -07003289 return ret;
3290}
3291
Eric Anholt673a3942008-07-30 12:06:12 -07003292int
Chris Wilson05394f32010-11-08 19:18:58 +00003293i915_gem_object_pin(struct drm_i915_gem_object *obj,
3294 uint32_t alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01003295 bool map_and_fenceable)
Eric Anholt673a3942008-07-30 12:06:12 -07003296{
Eric Anholt673a3942008-07-30 12:06:12 -07003297 int ret;
3298
Chris Wilson05394f32010-11-08 19:18:58 +00003299 BUG_ON(obj->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01003300
Chris Wilson05394f32010-11-08 19:18:58 +00003301 if (obj->gtt_space != NULL) {
3302 if ((alignment && obj->gtt_offset & (alignment - 1)) ||
3303 (map_and_fenceable && !obj->map_and_fenceable)) {
3304 WARN(obj->pin_count,
Chris Wilsonae7d49d2010-08-04 12:37:41 +01003305 "bo is already pinned with incorrect alignment:"
Daniel Vetter75e9e912010-11-04 17:11:09 +01003306 " offset=%x, req.alignment=%x, req.map_and_fenceable=%d,"
3307 " obj->map_and_fenceable=%d\n",
Chris Wilson05394f32010-11-08 19:18:58 +00003308 obj->gtt_offset, alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01003309 map_and_fenceable,
Chris Wilson05394f32010-11-08 19:18:58 +00003310 obj->map_and_fenceable);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01003311 ret = i915_gem_object_unbind(obj);
3312 if (ret)
3313 return ret;
3314 }
3315 }
3316
Chris Wilson05394f32010-11-08 19:18:58 +00003317 if (obj->gtt_space == NULL) {
Chris Wilsona00b10c2010-09-24 21:15:47 +01003318 ret = i915_gem_object_bind_to_gtt(obj, alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01003319 map_and_fenceable);
Chris Wilson97311292009-09-21 00:22:34 +01003320 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07003321 return ret;
Chris Wilson22c344e2009-02-11 14:26:45 +00003322 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05003323
Daniel Vetter74898d72012-02-15 23:50:22 +01003324 if (!obj->has_global_gtt_mapping && map_and_fenceable)
3325 i915_gem_gtt_bind_object(obj, obj->cache_level);
3326
Chris Wilson1b502472012-04-24 15:47:30 +01003327 obj->pin_count++;
Chris Wilson6299f992010-11-24 12:23:44 +00003328 obj->pin_mappable |= map_and_fenceable;
Eric Anholt673a3942008-07-30 12:06:12 -07003329
3330 return 0;
3331}
3332
3333void
Chris Wilson05394f32010-11-08 19:18:58 +00003334i915_gem_object_unpin(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07003335{
Chris Wilson05394f32010-11-08 19:18:58 +00003336 BUG_ON(obj->pin_count == 0);
3337 BUG_ON(obj->gtt_space == NULL);
Eric Anholt673a3942008-07-30 12:06:12 -07003338
Chris Wilson1b502472012-04-24 15:47:30 +01003339 if (--obj->pin_count == 0)
Chris Wilson6299f992010-11-24 12:23:44 +00003340 obj->pin_mappable = false;
Eric Anholt673a3942008-07-30 12:06:12 -07003341}
3342
3343int
3344i915_gem_pin_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00003345 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003346{
3347 struct drm_i915_gem_pin *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003348 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07003349 int ret;
3350
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003351 ret = i915_mutex_lock_interruptible(dev);
3352 if (ret)
3353 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003354
Chris Wilson05394f32010-11-08 19:18:58 +00003355 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00003356 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003357 ret = -ENOENT;
3358 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07003359 }
Eric Anholt673a3942008-07-30 12:06:12 -07003360
Chris Wilson05394f32010-11-08 19:18:58 +00003361 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilsonbb6baf72009-09-22 14:24:13 +01003362 DRM_ERROR("Attempting to pin a purgeable buffer\n");
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003363 ret = -EINVAL;
3364 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003365 }
3366
Chris Wilson05394f32010-11-08 19:18:58 +00003367 if (obj->pin_filp != NULL && obj->pin_filp != file) {
Jesse Barnes79e53942008-11-07 14:24:08 -08003368 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
3369 args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003370 ret = -EINVAL;
3371 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08003372 }
3373
Chris Wilson05394f32010-11-08 19:18:58 +00003374 obj->user_pin_count++;
3375 obj->pin_filp = file;
3376 if (obj->user_pin_count == 1) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01003377 ret = i915_gem_object_pin(obj, args->alignment, true);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003378 if (ret)
3379 goto out;
Eric Anholt673a3942008-07-30 12:06:12 -07003380 }
3381
3382 /* XXX - flush the CPU caches for pinned objects
3383 * as the X server doesn't manage domains yet
3384 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003385 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00003386 args->offset = obj->gtt_offset;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003387out:
Chris Wilson05394f32010-11-08 19:18:58 +00003388 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003389unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07003390 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003391 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003392}
3393
3394int
3395i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00003396 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003397{
3398 struct drm_i915_gem_pin *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003399 struct drm_i915_gem_object *obj;
Chris Wilson76c1dec2010-09-25 11:22:51 +01003400 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003401
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003402 ret = i915_mutex_lock_interruptible(dev);
3403 if (ret)
3404 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003405
Chris Wilson05394f32010-11-08 19:18:58 +00003406 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00003407 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003408 ret = -ENOENT;
3409 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07003410 }
Chris Wilson76c1dec2010-09-25 11:22:51 +01003411
Chris Wilson05394f32010-11-08 19:18:58 +00003412 if (obj->pin_filp != file) {
Jesse Barnes79e53942008-11-07 14:24:08 -08003413 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
3414 args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003415 ret = -EINVAL;
3416 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08003417 }
Chris Wilson05394f32010-11-08 19:18:58 +00003418 obj->user_pin_count--;
3419 if (obj->user_pin_count == 0) {
3420 obj->pin_filp = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08003421 i915_gem_object_unpin(obj);
3422 }
Eric Anholt673a3942008-07-30 12:06:12 -07003423
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003424out:
Chris Wilson05394f32010-11-08 19:18:58 +00003425 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003426unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07003427 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003428 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003429}
3430
3431int
3432i915_gem_busy_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00003433 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003434{
3435 struct drm_i915_gem_busy *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003436 struct drm_i915_gem_object *obj;
Chris Wilson30dbf0c2010-09-25 10:19:17 +01003437 int ret;
3438
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003439 ret = i915_mutex_lock_interruptible(dev);
3440 if (ret)
3441 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003442
Chris Wilson05394f32010-11-08 19:18:58 +00003443 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00003444 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003445 ret = -ENOENT;
3446 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07003447 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08003448
Chris Wilson0be555b2010-08-04 15:36:30 +01003449 /* Count all active objects as busy, even if they are currently not used
3450 * by the gpu. Users of this interface expect objects to eventually
3451 * become non-busy without any further actions, therefore emit any
3452 * necessary flushes here.
Eric Anholtc4de0a52008-12-14 19:05:04 -08003453 */
Daniel Vetter30dfebf2012-06-01 15:21:23 +02003454 ret = i915_gem_object_flush_active(obj);
3455
Chris Wilson05394f32010-11-08 19:18:58 +00003456 args->busy = obj->active;
Chris Wilsone9808ed2012-07-04 12:25:08 +01003457 if (obj->ring) {
3458 BUILD_BUG_ON(I915_NUM_RINGS > 16);
3459 args->busy |= intel_ring_flag(obj->ring) << 16;
3460 }
Eric Anholt673a3942008-07-30 12:06:12 -07003461
Chris Wilson05394f32010-11-08 19:18:58 +00003462 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003463unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07003464 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003465 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003466}
3467
3468int
3469i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
3470 struct drm_file *file_priv)
3471{
Akshay Joshi0206e352011-08-16 15:34:10 -04003472 return i915_gem_ring_throttle(dev, file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07003473}
3474
Chris Wilson3ef94da2009-09-14 16:50:29 +01003475int
3476i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
3477 struct drm_file *file_priv)
3478{
3479 struct drm_i915_gem_madvise *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003480 struct drm_i915_gem_object *obj;
Chris Wilson76c1dec2010-09-25 11:22:51 +01003481 int ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003482
3483 switch (args->madv) {
3484 case I915_MADV_DONTNEED:
3485 case I915_MADV_WILLNEED:
3486 break;
3487 default:
3488 return -EINVAL;
3489 }
3490
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003491 ret = i915_mutex_lock_interruptible(dev);
3492 if (ret)
3493 return ret;
3494
Chris Wilson05394f32010-11-08 19:18:58 +00003495 obj = to_intel_bo(drm_gem_object_lookup(dev, file_priv, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00003496 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003497 ret = -ENOENT;
3498 goto unlock;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003499 }
Chris Wilson3ef94da2009-09-14 16:50:29 +01003500
Chris Wilson05394f32010-11-08 19:18:58 +00003501 if (obj->pin_count) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003502 ret = -EINVAL;
3503 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003504 }
3505
Chris Wilson05394f32010-11-08 19:18:58 +00003506 if (obj->madv != __I915_MADV_PURGED)
3507 obj->madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003508
Chris Wilson6c085a72012-08-20 11:40:46 +02003509 /* if the object is no longer attached, discard its backing storage */
3510 if (i915_gem_object_is_purgeable(obj) && obj->pages == NULL)
Chris Wilson2d7ef392009-09-20 23:13:10 +01003511 i915_gem_object_truncate(obj);
3512
Chris Wilson05394f32010-11-08 19:18:58 +00003513 args->retained = obj->madv != __I915_MADV_PURGED;
Chris Wilsonbb6baf72009-09-22 14:24:13 +01003514
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003515out:
Chris Wilson05394f32010-11-08 19:18:58 +00003516 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003517unlock:
Chris Wilson3ef94da2009-09-14 16:50:29 +01003518 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003519 return ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003520}
3521
Chris Wilson05394f32010-11-08 19:18:58 +00003522struct drm_i915_gem_object *i915_gem_alloc_object(struct drm_device *dev,
3523 size_t size)
Daniel Vetterac52bc52010-04-09 19:05:06 +00003524{
Chris Wilson73aa8082010-09-30 11:46:12 +01003525 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc397b902010-04-09 19:05:07 +00003526 struct drm_i915_gem_object *obj;
Hugh Dickins5949eac2011-06-27 16:18:18 -07003527 struct address_space *mapping;
Chris Wilsonbed1ea92012-05-24 20:48:12 +01003528 u32 mask;
Daniel Vetterc397b902010-04-09 19:05:07 +00003529
3530 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
3531 if (obj == NULL)
3532 return NULL;
3533
3534 if (drm_gem_object_init(dev, &obj->base, size) != 0) {
3535 kfree(obj);
3536 return NULL;
3537 }
3538
Chris Wilsonbed1ea92012-05-24 20:48:12 +01003539 mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
3540 if (IS_CRESTLINE(dev) || IS_BROADWATER(dev)) {
3541 /* 965gm cannot relocate objects above 4GiB. */
3542 mask &= ~__GFP_HIGHMEM;
3543 mask |= __GFP_DMA32;
3544 }
3545
Hugh Dickins5949eac2011-06-27 16:18:18 -07003546 mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Chris Wilsonbed1ea92012-05-24 20:48:12 +01003547 mapping_set_gfp_mask(mapping, mask);
Hugh Dickins5949eac2011-06-27 16:18:18 -07003548
Chris Wilson73aa8082010-09-30 11:46:12 +01003549 i915_gem_info_add_obj(dev_priv, size);
3550
Daniel Vetterc397b902010-04-09 19:05:07 +00003551 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
3552 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3553
Eugeni Dodonov3d29b842012-01-17 14:43:53 -02003554 if (HAS_LLC(dev)) {
3555 /* On some devices, we can have the GPU use the LLC (the CPU
Eric Anholta1871112011-03-29 16:59:55 -07003556 * cache) for about a 10% performance improvement
3557 * compared to uncached. Graphics requests other than
3558 * display scanout are coherent with the CPU in
3559 * accessing this cache. This means in this mode we
3560 * don't need to clflush on the CPU side, and on the
3561 * GPU side we only need to flush internal caches to
3562 * get data visible to the CPU.
3563 *
3564 * However, we maintain the display planes as UC, and so
3565 * need to rebind when first used as such.
3566 */
3567 obj->cache_level = I915_CACHE_LLC;
3568 } else
3569 obj->cache_level = I915_CACHE_NONE;
3570
Daniel Vetter62b8b212010-04-09 19:05:08 +00003571 obj->base.driver_private = NULL;
Daniel Vetterc397b902010-04-09 19:05:07 +00003572 obj->fence_reg = I915_FENCE_REG_NONE;
Chris Wilson69dc4982010-10-19 10:36:51 +01003573 INIT_LIST_HEAD(&obj->mm_list);
Daniel Vetter93a37f22010-11-05 20:24:53 +01003574 INIT_LIST_HEAD(&obj->gtt_list);
Chris Wilson69dc4982010-10-19 10:36:51 +01003575 INIT_LIST_HEAD(&obj->ring_list);
Chris Wilson432e58e2010-11-25 19:32:06 +00003576 INIT_LIST_HEAD(&obj->exec_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00003577 obj->madv = I915_MADV_WILLNEED;
Daniel Vetter75e9e912010-11-04 17:11:09 +01003578 /* Avoid an unnecessary call to unbind on the first bind. */
3579 obj->map_and_fenceable = true;
Daniel Vetterc397b902010-04-09 19:05:07 +00003580
Chris Wilson05394f32010-11-08 19:18:58 +00003581 return obj;
Daniel Vetterac52bc52010-04-09 19:05:06 +00003582}
3583
Eric Anholt673a3942008-07-30 12:06:12 -07003584int i915_gem_init_object(struct drm_gem_object *obj)
3585{
Daniel Vetterc397b902010-04-09 19:05:07 +00003586 BUG();
Jesse Barnesde151cf2008-11-12 10:03:55 -08003587
Eric Anholt673a3942008-07-30 12:06:12 -07003588 return 0;
3589}
3590
Chris Wilson1488fc02012-04-24 15:47:31 +01003591void i915_gem_free_object(struct drm_gem_object *gem_obj)
Chris Wilsonbe726152010-07-23 23:18:50 +01003592{
Chris Wilson1488fc02012-04-24 15:47:31 +01003593 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
Chris Wilson05394f32010-11-08 19:18:58 +00003594 struct drm_device *dev = obj->base.dev;
Chris Wilsonbe726152010-07-23 23:18:50 +01003595 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonbe726152010-07-23 23:18:50 +01003596
Chris Wilson26e12f892011-03-20 11:20:19 +00003597 trace_i915_gem_object_destroy(obj);
3598
Daniel Vetter1286ff72012-05-10 15:25:09 +02003599 if (gem_obj->import_attach)
3600 drm_prime_gem_destroy(gem_obj, obj->sg_table);
3601
Chris Wilson1488fc02012-04-24 15:47:31 +01003602 if (obj->phys_obj)
3603 i915_gem_detach_phys_object(dev, obj);
3604
3605 obj->pin_count = 0;
3606 if (WARN_ON(i915_gem_object_unbind(obj) == -ERESTARTSYS)) {
3607 bool was_interruptible;
3608
3609 was_interruptible = dev_priv->mm.interruptible;
3610 dev_priv->mm.interruptible = false;
3611
3612 WARN_ON(i915_gem_object_unbind(obj));
3613
3614 dev_priv->mm.interruptible = was_interruptible;
3615 }
3616
Chris Wilson6c085a72012-08-20 11:40:46 +02003617 i915_gem_object_put_pages_gtt(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00003618 if (obj->base.map_list.map)
Rob Clarkb464e9a2011-08-10 08:09:08 -05003619 drm_gem_free_mmap_offset(&obj->base);
Chris Wilsonbe726152010-07-23 23:18:50 +01003620
Chris Wilson05394f32010-11-08 19:18:58 +00003621 drm_gem_object_release(&obj->base);
3622 i915_gem_info_remove_obj(dev_priv, obj->base.size);
Chris Wilsonbe726152010-07-23 23:18:50 +01003623
Chris Wilson05394f32010-11-08 19:18:58 +00003624 kfree(obj->bit_17);
3625 kfree(obj);
Chris Wilsonbe726152010-07-23 23:18:50 +01003626}
3627
Jesse Barnes5669fca2009-02-17 15:13:31 -08003628int
Eric Anholt673a3942008-07-30 12:06:12 -07003629i915_gem_idle(struct drm_device *dev)
3630{
3631 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson29105cc2010-01-07 10:39:13 +00003632 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003633
Keith Packard6dbe2772008-10-14 21:41:13 -07003634 mutex_lock(&dev->struct_mutex);
3635
Chris Wilson87acb0a2010-10-19 10:13:00 +01003636 if (dev_priv->mm.suspended) {
Keith Packard6dbe2772008-10-14 21:41:13 -07003637 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07003638 return 0;
Keith Packard6dbe2772008-10-14 21:41:13 -07003639 }
Eric Anholt673a3942008-07-30 12:06:12 -07003640
Ben Widawskyb2da9fe2012-04-26 16:02:58 -07003641 ret = i915_gpu_idle(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07003642 if (ret) {
3643 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07003644 return ret;
Keith Packard6dbe2772008-10-14 21:41:13 -07003645 }
Ben Widawskyb2da9fe2012-04-26 16:02:58 -07003646 i915_gem_retire_requests(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07003647
Chris Wilson29105cc2010-01-07 10:39:13 +00003648 /* Under UMS, be paranoid and evict. */
Chris Wilsona39d7ef2012-04-24 18:22:52 +01003649 if (!drm_core_check_feature(dev, DRIVER_MODESET))
Chris Wilson6c085a72012-08-20 11:40:46 +02003650 i915_gem_evict_everything(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00003651
Chris Wilson312817a2010-11-22 11:50:11 +00003652 i915_gem_reset_fences(dev);
3653
Chris Wilson29105cc2010-01-07 10:39:13 +00003654 /* Hack! Don't let anybody do execbuf while we don't control the chip.
3655 * We need to replace this with a semaphore, or something.
3656 * And not confound mm.suspended!
3657 */
3658 dev_priv->mm.suspended = 1;
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02003659 del_timer_sync(&dev_priv->hangcheck_timer);
Chris Wilson29105cc2010-01-07 10:39:13 +00003660
3661 i915_kernel_lost_context(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07003662 i915_gem_cleanup_ringbuffer(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00003663
Keith Packard6dbe2772008-10-14 21:41:13 -07003664 mutex_unlock(&dev->struct_mutex);
3665
Chris Wilson29105cc2010-01-07 10:39:13 +00003666 /* Cancel the retire work handler, which should be idle now. */
3667 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
3668
Eric Anholt673a3942008-07-30 12:06:12 -07003669 return 0;
3670}
3671
Ben Widawskyb9524a12012-05-25 16:56:24 -07003672void i915_gem_l3_remap(struct drm_device *dev)
3673{
3674 drm_i915_private_t *dev_priv = dev->dev_private;
3675 u32 misccpctl;
3676 int i;
3677
3678 if (!IS_IVYBRIDGE(dev))
3679 return;
3680
3681 if (!dev_priv->mm.l3_remap_info)
3682 return;
3683
3684 misccpctl = I915_READ(GEN7_MISCCPCTL);
3685 I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
3686 POSTING_READ(GEN7_MISCCPCTL);
3687
3688 for (i = 0; i < GEN7_L3LOG_SIZE; i += 4) {
3689 u32 remap = I915_READ(GEN7_L3LOG_BASE + i);
3690 if (remap && remap != dev_priv->mm.l3_remap_info[i/4])
3691 DRM_DEBUG("0x%x was already programmed to %x\n",
3692 GEN7_L3LOG_BASE + i, remap);
3693 if (remap && !dev_priv->mm.l3_remap_info[i/4])
3694 DRM_DEBUG_DRIVER("Clearing remapped register\n");
3695 I915_WRITE(GEN7_L3LOG_BASE + i, dev_priv->mm.l3_remap_info[i/4]);
3696 }
3697
3698 /* Make sure all the writes land before disabling dop clock gating */
3699 POSTING_READ(GEN7_L3LOG_BASE);
3700
3701 I915_WRITE(GEN7_MISCCPCTL, misccpctl);
3702}
3703
Daniel Vetterf691e2f2012-02-02 09:58:12 +01003704void i915_gem_init_swizzling(struct drm_device *dev)
3705{
3706 drm_i915_private_t *dev_priv = dev->dev_private;
3707
Daniel Vetter11782b02012-01-31 16:47:55 +01003708 if (INTEL_INFO(dev)->gen < 5 ||
Daniel Vetterf691e2f2012-02-02 09:58:12 +01003709 dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
3710 return;
3711
3712 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
3713 DISP_TILE_SURFACE_SWIZZLING);
3714
Daniel Vetter11782b02012-01-31 16:47:55 +01003715 if (IS_GEN5(dev))
3716 return;
3717
Daniel Vetterf691e2f2012-02-02 09:58:12 +01003718 I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
3719 if (IS_GEN6(dev))
Daniel Vetter6b26c862012-04-24 14:04:12 +02003720 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
Daniel Vetterf691e2f2012-02-02 09:58:12 +01003721 else
Daniel Vetter6b26c862012-04-24 14:04:12 +02003722 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
Daniel Vetterf691e2f2012-02-02 09:58:12 +01003723}
Daniel Vettere21af882012-02-09 20:53:27 +01003724
3725void i915_gem_init_ppgtt(struct drm_device *dev)
3726{
3727 drm_i915_private_t *dev_priv = dev->dev_private;
3728 uint32_t pd_offset;
3729 struct intel_ring_buffer *ring;
Daniel Vetter55a254a2012-03-22 00:14:43 +01003730 struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
3731 uint32_t __iomem *pd_addr;
3732 uint32_t pd_entry;
Daniel Vettere21af882012-02-09 20:53:27 +01003733 int i;
3734
3735 if (!dev_priv->mm.aliasing_ppgtt)
3736 return;
3737
Daniel Vetter55a254a2012-03-22 00:14:43 +01003738
3739 pd_addr = dev_priv->mm.gtt->gtt + ppgtt->pd_offset/sizeof(uint32_t);
3740 for (i = 0; i < ppgtt->num_pd_entries; i++) {
3741 dma_addr_t pt_addr;
3742
3743 if (dev_priv->mm.gtt->needs_dmar)
3744 pt_addr = ppgtt->pt_dma_addr[i];
3745 else
3746 pt_addr = page_to_phys(ppgtt->pt_pages[i]);
3747
3748 pd_entry = GEN6_PDE_ADDR_ENCODE(pt_addr);
3749 pd_entry |= GEN6_PDE_VALID;
3750
3751 writel(pd_entry, pd_addr + i);
3752 }
3753 readl(pd_addr);
3754
3755 pd_offset = ppgtt->pd_offset;
Daniel Vettere21af882012-02-09 20:53:27 +01003756 pd_offset /= 64; /* in cachelines, */
3757 pd_offset <<= 16;
3758
3759 if (INTEL_INFO(dev)->gen == 6) {
Daniel Vetter48ecfa12012-04-11 20:42:40 +02003760 uint32_t ecochk, gab_ctl, ecobits;
3761
3762 ecobits = I915_READ(GAC_ECO_BITS);
3763 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B);
Daniel Vetterbe901a52012-04-11 20:42:39 +02003764
3765 gab_ctl = I915_READ(GAB_CTL);
3766 I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT);
3767
3768 ecochk = I915_READ(GAM_ECOCHK);
Daniel Vettere21af882012-02-09 20:53:27 +01003769 I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT |
3770 ECOCHK_PPGTT_CACHE64B);
Daniel Vetter6b26c862012-04-24 14:04:12 +02003771 I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Daniel Vettere21af882012-02-09 20:53:27 +01003772 } else if (INTEL_INFO(dev)->gen >= 7) {
3773 I915_WRITE(GAM_ECOCHK, ECOCHK_PPGTT_CACHE64B);
3774 /* GFX_MODE is per-ring on gen7+ */
3775 }
3776
Chris Wilsonb4519512012-05-11 14:29:30 +01003777 for_each_ring(ring, dev_priv, i) {
Daniel Vettere21af882012-02-09 20:53:27 +01003778 if (INTEL_INFO(dev)->gen >= 7)
3779 I915_WRITE(RING_MODE_GEN7(ring),
Daniel Vetter6b26c862012-04-24 14:04:12 +02003780 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Daniel Vettere21af882012-02-09 20:53:27 +01003781
3782 I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
3783 I915_WRITE(RING_PP_DIR_BASE(ring), pd_offset);
3784 }
3785}
3786
Chris Wilson67b1b572012-07-05 23:49:40 +01003787static bool
3788intel_enable_blt(struct drm_device *dev)
3789{
3790 if (!HAS_BLT(dev))
3791 return false;
3792
3793 /* The blitter was dysfunctional on early prototypes */
3794 if (IS_GEN6(dev) && dev->pdev->revision < 8) {
3795 DRM_INFO("BLT not supported on this pre-production hardware;"
3796 " graphics performance will be degraded.\n");
3797 return false;
3798 }
3799
3800 return true;
3801}
3802
Eric Anholt673a3942008-07-30 12:06:12 -07003803int
Daniel Vetterf691e2f2012-02-02 09:58:12 +01003804i915_gem_init_hw(struct drm_device *dev)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08003805{
3806 drm_i915_private_t *dev_priv = dev->dev_private;
3807 int ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01003808
Daniel Vetter8ecd1a62012-06-07 15:56:03 +02003809 if (!intel_enable_gtt())
3810 return -EIO;
3811
Ben Widawskyb9524a12012-05-25 16:56:24 -07003812 i915_gem_l3_remap(dev);
3813
Daniel Vetterf691e2f2012-02-02 09:58:12 +01003814 i915_gem_init_swizzling(dev);
3815
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08003816 ret = intel_init_render_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01003817 if (ret)
Chris Wilsonb6913e42010-11-12 10:46:37 +00003818 return ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01003819
3820 if (HAS_BSD(dev)) {
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08003821 ret = intel_init_bsd_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01003822 if (ret)
3823 goto cleanup_render_ring;
Zou Nan haid1b851f2010-05-21 09:08:57 +08003824 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01003825
Chris Wilson67b1b572012-07-05 23:49:40 +01003826 if (intel_enable_blt(dev)) {
Chris Wilson549f7362010-10-19 11:19:32 +01003827 ret = intel_init_blt_ring_buffer(dev);
3828 if (ret)
3829 goto cleanup_bsd_ring;
3830 }
3831
Chris Wilson6f392d5482010-08-07 11:01:22 +01003832 dev_priv->next_seqno = 1;
3833
Ben Widawsky254f9652012-06-04 14:42:42 -07003834 /*
3835 * XXX: There was some w/a described somewhere suggesting loading
3836 * contexts before PPGTT.
3837 */
3838 i915_gem_context_init(dev);
Daniel Vettere21af882012-02-09 20:53:27 +01003839 i915_gem_init_ppgtt(dev);
3840
Chris Wilson68f95ba2010-05-27 13:18:22 +01003841 return 0;
3842
Chris Wilson549f7362010-10-19 11:19:32 +01003843cleanup_bsd_ring:
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003844 intel_cleanup_ring_buffer(&dev_priv->ring[VCS]);
Chris Wilson68f95ba2010-05-27 13:18:22 +01003845cleanup_render_ring:
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003846 intel_cleanup_ring_buffer(&dev_priv->ring[RCS]);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08003847 return ret;
3848}
3849
Chris Wilson1070a422012-04-24 15:47:41 +01003850static bool
3851intel_enable_ppgtt(struct drm_device *dev)
3852{
3853 if (i915_enable_ppgtt >= 0)
3854 return i915_enable_ppgtt;
3855
3856#ifdef CONFIG_INTEL_IOMMU
3857 /* Disable ppgtt on SNB if VT-d is on. */
3858 if (INTEL_INFO(dev)->gen == 6 && intel_iommu_gfx_mapped)
3859 return false;
3860#endif
3861
3862 return true;
3863}
3864
3865int i915_gem_init(struct drm_device *dev)
3866{
3867 struct drm_i915_private *dev_priv = dev->dev_private;
3868 unsigned long gtt_size, mappable_size;
3869 int ret;
3870
3871 gtt_size = dev_priv->mm.gtt->gtt_total_entries << PAGE_SHIFT;
3872 mappable_size = dev_priv->mm.gtt->gtt_mappable_entries << PAGE_SHIFT;
3873
3874 mutex_lock(&dev->struct_mutex);
3875 if (intel_enable_ppgtt(dev) && HAS_ALIASING_PPGTT(dev)) {
3876 /* PPGTT pdes are stolen from global gtt ptes, so shrink the
3877 * aperture accordingly when using aliasing ppgtt. */
3878 gtt_size -= I915_PPGTT_PD_ENTRIES*PAGE_SIZE;
3879
3880 i915_gem_init_global_gtt(dev, 0, mappable_size, gtt_size);
3881
3882 ret = i915_gem_init_aliasing_ppgtt(dev);
3883 if (ret) {
3884 mutex_unlock(&dev->struct_mutex);
3885 return ret;
3886 }
3887 } else {
3888 /* Let GEM Manage all of the aperture.
3889 *
3890 * However, leave one page at the end still bound to the scratch
3891 * page. There are a number of places where the hardware
3892 * apparently prefetches past the end of the object, and we've
3893 * seen multiple hangs with the GPU head pointer stuck in a
3894 * batchbuffer bound at the last page of the aperture. One page
3895 * should be enough to keep any prefetching inside of the
3896 * aperture.
3897 */
3898 i915_gem_init_global_gtt(dev, 0, mappable_size,
3899 gtt_size);
3900 }
3901
3902 ret = i915_gem_init_hw(dev);
3903 mutex_unlock(&dev->struct_mutex);
3904 if (ret) {
3905 i915_gem_cleanup_aliasing_ppgtt(dev);
3906 return ret;
3907 }
3908
Daniel Vetter53ca26c2012-04-26 23:28:03 +02003909 /* Allow hardware batchbuffers unless told otherwise, but not for KMS. */
3910 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3911 dev_priv->dri1.allow_batchbuffer = 1;
Chris Wilson1070a422012-04-24 15:47:41 +01003912 return 0;
3913}
3914
Zou Nan hai8187a2b2010-05-21 09:08:55 +08003915void
3916i915_gem_cleanup_ringbuffer(struct drm_device *dev)
3917{
3918 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonb4519512012-05-11 14:29:30 +01003919 struct intel_ring_buffer *ring;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003920 int i;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08003921
Chris Wilsonb4519512012-05-11 14:29:30 +01003922 for_each_ring(ring, dev_priv, i)
3923 intel_cleanup_ring_buffer(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08003924}
3925
3926int
Eric Anholt673a3942008-07-30 12:06:12 -07003927i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
3928 struct drm_file *file_priv)
3929{
3930 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonb4519512012-05-11 14:29:30 +01003931 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003932
Jesse Barnes79e53942008-11-07 14:24:08 -08003933 if (drm_core_check_feature(dev, DRIVER_MODESET))
3934 return 0;
3935
Ben Gamariba1234d2009-09-14 17:48:47 -04003936 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07003937 DRM_ERROR("Reenabling wedged hardware, good luck\n");
Ben Gamariba1234d2009-09-14 17:48:47 -04003938 atomic_set(&dev_priv->mm.wedged, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07003939 }
3940
Eric Anholt673a3942008-07-30 12:06:12 -07003941 mutex_lock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08003942 dev_priv->mm.suspended = 0;
3943
Daniel Vetterf691e2f2012-02-02 09:58:12 +01003944 ret = i915_gem_init_hw(dev);
Wu Fengguangd816f6a2009-04-18 10:43:32 +08003945 if (ret != 0) {
3946 mutex_unlock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08003947 return ret;
Wu Fengguangd816f6a2009-04-18 10:43:32 +08003948 }
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08003949
Chris Wilson69dc4982010-10-19 10:36:51 +01003950 BUG_ON(!list_empty(&dev_priv->mm.active_list));
Eric Anholt673a3942008-07-30 12:06:12 -07003951 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
Eric Anholt673a3942008-07-30 12:06:12 -07003952 mutex_unlock(&dev->struct_mutex);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04003953
Chris Wilson5f353082010-06-07 14:03:03 +01003954 ret = drm_irq_install(dev);
3955 if (ret)
3956 goto cleanup_ringbuffer;
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04003957
Eric Anholt673a3942008-07-30 12:06:12 -07003958 return 0;
Chris Wilson5f353082010-06-07 14:03:03 +01003959
3960cleanup_ringbuffer:
3961 mutex_lock(&dev->struct_mutex);
3962 i915_gem_cleanup_ringbuffer(dev);
3963 dev_priv->mm.suspended = 1;
3964 mutex_unlock(&dev->struct_mutex);
3965
3966 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003967}
3968
3969int
3970i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
3971 struct drm_file *file_priv)
3972{
Jesse Barnes79e53942008-11-07 14:24:08 -08003973 if (drm_core_check_feature(dev, DRIVER_MODESET))
3974 return 0;
3975
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04003976 drm_irq_uninstall(dev);
Linus Torvaldse6890f62009-09-08 17:09:24 -07003977 return i915_gem_idle(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07003978}
3979
3980void
3981i915_gem_lastclose(struct drm_device *dev)
3982{
3983 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003984
Eric Anholte806b492009-01-22 09:56:58 -08003985 if (drm_core_check_feature(dev, DRIVER_MODESET))
3986 return;
3987
Keith Packard6dbe2772008-10-14 21:41:13 -07003988 ret = i915_gem_idle(dev);
3989 if (ret)
3990 DRM_ERROR("failed to idle hardware: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07003991}
3992
Chris Wilson64193402010-10-24 12:38:05 +01003993static void
3994init_ring_lists(struct intel_ring_buffer *ring)
3995{
3996 INIT_LIST_HEAD(&ring->active_list);
3997 INIT_LIST_HEAD(&ring->request_list);
Chris Wilson64193402010-10-24 12:38:05 +01003998}
3999
Eric Anholt673a3942008-07-30 12:06:12 -07004000void
4001i915_gem_load(struct drm_device *dev)
4002{
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004003 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07004004 drm_i915_private_t *dev_priv = dev->dev_private;
4005
Chris Wilson69dc4982010-10-19 10:36:51 +01004006 INIT_LIST_HEAD(&dev_priv->mm.active_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004007 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
Chris Wilson6c085a72012-08-20 11:40:46 +02004008 INIT_LIST_HEAD(&dev_priv->mm.unbound_list);
4009 INIT_LIST_HEAD(&dev_priv->mm.bound_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07004010 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00004011 for (i = 0; i < I915_NUM_RINGS; i++)
4012 init_ring_lists(&dev_priv->ring[i]);
Daniel Vetter4b9de732011-10-09 21:52:02 +02004013 for (i = 0; i < I915_MAX_NUM_FENCES; i++)
Daniel Vetter007cc8a2010-04-28 11:02:31 +02004014 INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004015 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
4016 i915_gem_retire_work_handler);
Chris Wilson30dbf0c2010-09-25 10:19:17 +01004017 init_completion(&dev_priv->error_completion);
Chris Wilson31169712009-09-14 16:50:28 +01004018
Dave Airlie94400122010-07-20 13:15:31 +10004019 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
4020 if (IS_GEN3(dev)) {
Daniel Vetter50743292012-04-26 22:02:54 +02004021 I915_WRITE(MI_ARB_STATE,
4022 _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
Dave Airlie94400122010-07-20 13:15:31 +10004023 }
4024
Chris Wilson72bfa192010-12-19 11:42:05 +00004025 dev_priv->relative_constants_mode = I915_EXEC_CONSTANTS_REL_GENERAL;
4026
Jesse Barnesde151cf2008-11-12 10:03:55 -08004027 /* Old X drivers will take 0-2 for front, back, depth buffers */
Eric Anholtb397c832010-01-26 09:43:10 -08004028 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4029 dev_priv->fence_reg_start = 3;
Jesse Barnesde151cf2008-11-12 10:03:55 -08004030
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004031 if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08004032 dev_priv->num_fence_regs = 16;
4033 else
4034 dev_priv->num_fence_regs = 8;
4035
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004036 /* Initialize fence registers to zero */
Chris Wilsonada726c2012-04-17 15:31:32 +01004037 i915_gem_reset_fences(dev);
Eric Anholt10ed13e2011-05-06 13:53:49 -07004038
Eric Anholt673a3942008-07-30 12:06:12 -07004039 i915_gem_detect_bit_6_swizzle(dev);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05004040 init_waitqueue_head(&dev_priv->pending_flip_queue);
Chris Wilson17250b72010-10-28 12:51:39 +01004041
Chris Wilsonce453d82011-02-21 14:43:56 +00004042 dev_priv->mm.interruptible = true;
4043
Chris Wilson17250b72010-10-28 12:51:39 +01004044 dev_priv->mm.inactive_shrinker.shrink = i915_gem_inactive_shrink;
4045 dev_priv->mm.inactive_shrinker.seeks = DEFAULT_SEEKS;
4046 register_shrinker(&dev_priv->mm.inactive_shrinker);
Eric Anholt673a3942008-07-30 12:06:12 -07004047}
Dave Airlie71acb5e2008-12-30 20:31:46 +10004048
4049/*
4050 * Create a physically contiguous memory object for this object
4051 * e.g. for cursor + overlay regs
4052 */
Chris Wilson995b6762010-08-20 13:23:26 +01004053static int i915_gem_init_phys_object(struct drm_device *dev,
4054 int id, int size, int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004055{
4056 drm_i915_private_t *dev_priv = dev->dev_private;
4057 struct drm_i915_gem_phys_object *phys_obj;
4058 int ret;
4059
4060 if (dev_priv->mm.phys_objs[id - 1] || !size)
4061 return 0;
4062
Eric Anholt9a298b22009-03-24 12:23:04 -07004063 phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004064 if (!phys_obj)
4065 return -ENOMEM;
4066
4067 phys_obj->id = id;
4068
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004069 phys_obj->handle = drm_pci_alloc(dev, size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004070 if (!phys_obj->handle) {
4071 ret = -ENOMEM;
4072 goto kfree_obj;
4073 }
4074#ifdef CONFIG_X86
4075 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4076#endif
4077
4078 dev_priv->mm.phys_objs[id - 1] = phys_obj;
4079
4080 return 0;
4081kfree_obj:
Eric Anholt9a298b22009-03-24 12:23:04 -07004082 kfree(phys_obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004083 return ret;
4084}
4085
Chris Wilson995b6762010-08-20 13:23:26 +01004086static void i915_gem_free_phys_object(struct drm_device *dev, int id)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004087{
4088 drm_i915_private_t *dev_priv = dev->dev_private;
4089 struct drm_i915_gem_phys_object *phys_obj;
4090
4091 if (!dev_priv->mm.phys_objs[id - 1])
4092 return;
4093
4094 phys_obj = dev_priv->mm.phys_objs[id - 1];
4095 if (phys_obj->cur_obj) {
4096 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
4097 }
4098
4099#ifdef CONFIG_X86
4100 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4101#endif
4102 drm_pci_free(dev, phys_obj->handle);
4103 kfree(phys_obj);
4104 dev_priv->mm.phys_objs[id - 1] = NULL;
4105}
4106
4107void i915_gem_free_all_phys_object(struct drm_device *dev)
4108{
4109 int i;
4110
Dave Airlie260883c2009-01-22 17:58:49 +10004111 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004112 i915_gem_free_phys_object(dev, i);
4113}
4114
4115void i915_gem_detach_phys_object(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00004116 struct drm_i915_gem_object *obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004117{
Chris Wilson05394f32010-11-08 19:18:58 +00004118 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Chris Wilsone5281cc2010-10-28 13:45:36 +01004119 char *vaddr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004120 int i;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004121 int page_count;
4122
Chris Wilson05394f32010-11-08 19:18:58 +00004123 if (!obj->phys_obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004124 return;
Chris Wilson05394f32010-11-08 19:18:58 +00004125 vaddr = obj->phys_obj->handle->vaddr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004126
Chris Wilson05394f32010-11-08 19:18:58 +00004127 page_count = obj->base.size / PAGE_SIZE;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004128 for (i = 0; i < page_count; i++) {
Hugh Dickins5949eac2011-06-27 16:18:18 -07004129 struct page *page = shmem_read_mapping_page(mapping, i);
Chris Wilsone5281cc2010-10-28 13:45:36 +01004130 if (!IS_ERR(page)) {
4131 char *dst = kmap_atomic(page);
4132 memcpy(dst, vaddr + i*PAGE_SIZE, PAGE_SIZE);
4133 kunmap_atomic(dst);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004134
Chris Wilsone5281cc2010-10-28 13:45:36 +01004135 drm_clflush_pages(&page, 1);
4136
4137 set_page_dirty(page);
4138 mark_page_accessed(page);
4139 page_cache_release(page);
4140 }
Dave Airlie71acb5e2008-12-30 20:31:46 +10004141 }
Daniel Vetter40ce6572010-11-05 18:12:18 +01004142 intel_gtt_chipset_flush();
Chris Wilsond78b47b2009-06-17 21:52:49 +01004143
Chris Wilson05394f32010-11-08 19:18:58 +00004144 obj->phys_obj->cur_obj = NULL;
4145 obj->phys_obj = NULL;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004146}
4147
4148int
4149i915_gem_attach_phys_object(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00004150 struct drm_i915_gem_object *obj,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004151 int id,
4152 int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004153{
Chris Wilson05394f32010-11-08 19:18:58 +00004154 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004155 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004156 int ret = 0;
4157 int page_count;
4158 int i;
4159
4160 if (id > I915_MAX_PHYS_OBJECT)
4161 return -EINVAL;
4162
Chris Wilson05394f32010-11-08 19:18:58 +00004163 if (obj->phys_obj) {
4164 if (obj->phys_obj->id == id)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004165 return 0;
4166 i915_gem_detach_phys_object(dev, obj);
4167 }
4168
Dave Airlie71acb5e2008-12-30 20:31:46 +10004169 /* create a new object */
4170 if (!dev_priv->mm.phys_objs[id - 1]) {
4171 ret = i915_gem_init_phys_object(dev, id,
Chris Wilson05394f32010-11-08 19:18:58 +00004172 obj->base.size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004173 if (ret) {
Chris Wilson05394f32010-11-08 19:18:58 +00004174 DRM_ERROR("failed to init phys object %d size: %zu\n",
4175 id, obj->base.size);
Chris Wilsone5281cc2010-10-28 13:45:36 +01004176 return ret;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004177 }
4178 }
4179
4180 /* bind to the object */
Chris Wilson05394f32010-11-08 19:18:58 +00004181 obj->phys_obj = dev_priv->mm.phys_objs[id - 1];
4182 obj->phys_obj->cur_obj = obj;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004183
Chris Wilson05394f32010-11-08 19:18:58 +00004184 page_count = obj->base.size / PAGE_SIZE;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004185
4186 for (i = 0; i < page_count; i++) {
Chris Wilsone5281cc2010-10-28 13:45:36 +01004187 struct page *page;
4188 char *dst, *src;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004189
Hugh Dickins5949eac2011-06-27 16:18:18 -07004190 page = shmem_read_mapping_page(mapping, i);
Chris Wilsone5281cc2010-10-28 13:45:36 +01004191 if (IS_ERR(page))
4192 return PTR_ERR(page);
4193
Chris Wilsonff75b9b2010-10-30 22:52:31 +01004194 src = kmap_atomic(page);
Chris Wilson05394f32010-11-08 19:18:58 +00004195 dst = obj->phys_obj->handle->vaddr + (i * PAGE_SIZE);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004196 memcpy(dst, src, PAGE_SIZE);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -07004197 kunmap_atomic(src);
Chris Wilsone5281cc2010-10-28 13:45:36 +01004198
4199 mark_page_accessed(page);
4200 page_cache_release(page);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004201 }
4202
4203 return 0;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004204}
4205
4206static int
Chris Wilson05394f32010-11-08 19:18:58 +00004207i915_gem_phys_pwrite(struct drm_device *dev,
4208 struct drm_i915_gem_object *obj,
Dave Airlie71acb5e2008-12-30 20:31:46 +10004209 struct drm_i915_gem_pwrite *args,
4210 struct drm_file *file_priv)
4211{
Chris Wilson05394f32010-11-08 19:18:58 +00004212 void *vaddr = obj->phys_obj->handle->vaddr + args->offset;
Chris Wilsonb47b30c2010-11-08 01:12:29 +00004213 char __user *user_data = (char __user *) (uintptr_t) args->data_ptr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004214
Chris Wilsonb47b30c2010-11-08 01:12:29 +00004215 if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) {
4216 unsigned long unwritten;
4217
4218 /* The physical object once assigned is fixed for the lifetime
4219 * of the obj, so we can safely drop the lock and continue
4220 * to access vaddr.
4221 */
4222 mutex_unlock(&dev->struct_mutex);
4223 unwritten = copy_from_user(vaddr, user_data, args->size);
4224 mutex_lock(&dev->struct_mutex);
4225 if (unwritten)
4226 return -EFAULT;
4227 }
Dave Airlie71acb5e2008-12-30 20:31:46 +10004228
Daniel Vetter40ce6572010-11-05 18:12:18 +01004229 intel_gtt_chipset_flush();
Dave Airlie71acb5e2008-12-30 20:31:46 +10004230 return 0;
4231}
Eric Anholtb9624422009-06-03 07:27:35 +00004232
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004233void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00004234{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004235 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00004236
4237 /* Clean up our request list when the client is going away, so that
4238 * later retire_requests won't dereference our soon-to-be-gone
4239 * file_priv.
4240 */
Chris Wilson1c255952010-09-26 11:03:27 +01004241 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004242 while (!list_empty(&file_priv->mm.request_list)) {
4243 struct drm_i915_gem_request *request;
4244
4245 request = list_first_entry(&file_priv->mm.request_list,
4246 struct drm_i915_gem_request,
4247 client_list);
4248 list_del(&request->client_list);
4249 request->file_priv = NULL;
4250 }
Chris Wilson1c255952010-09-26 11:03:27 +01004251 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00004252}
Chris Wilson31169712009-09-14 16:50:28 +01004253
Chris Wilson31169712009-09-14 16:50:28 +01004254static int
Ying Han1495f232011-05-24 17:12:27 -07004255i915_gem_inactive_shrink(struct shrinker *shrinker, struct shrink_control *sc)
Chris Wilson31169712009-09-14 16:50:28 +01004256{
Chris Wilson17250b72010-10-28 12:51:39 +01004257 struct drm_i915_private *dev_priv =
4258 container_of(shrinker,
4259 struct drm_i915_private,
4260 mm.inactive_shrinker);
4261 struct drm_device *dev = dev_priv->dev;
Chris Wilson6c085a72012-08-20 11:40:46 +02004262 struct drm_i915_gem_object *obj;
Ying Han1495f232011-05-24 17:12:27 -07004263 int nr_to_scan = sc->nr_to_scan;
Chris Wilson17250b72010-10-28 12:51:39 +01004264 int cnt;
4265
4266 if (!mutex_trylock(&dev->struct_mutex))
Chris Wilsonbbe2e112010-10-28 22:35:07 +01004267 return 0;
Chris Wilson31169712009-09-14 16:50:28 +01004268
Chris Wilson6c085a72012-08-20 11:40:46 +02004269 if (nr_to_scan) {
4270 nr_to_scan -= i915_gem_purge(dev_priv, nr_to_scan);
4271 if (nr_to_scan > 0)
4272 i915_gem_shrink_all(dev_priv);
Chris Wilson31169712009-09-14 16:50:28 +01004273 }
4274
Chris Wilson17250b72010-10-28 12:51:39 +01004275 cnt = 0;
Chris Wilson6c085a72012-08-20 11:40:46 +02004276 list_for_each_entry(obj, &dev_priv->mm.unbound_list, gtt_list)
4277 cnt += obj->base.size >> PAGE_SHIFT;
4278 list_for_each_entry(obj, &dev_priv->mm.bound_list, gtt_list)
4279 if (obj->pin_count == 0)
4280 cnt += obj->base.size >> PAGE_SHIFT;
Chris Wilson31169712009-09-14 16:50:28 +01004281
Chris Wilson17250b72010-10-28 12:51:39 +01004282 mutex_unlock(&dev->struct_mutex);
Chris Wilson6c085a72012-08-20 11:40:46 +02004283 return cnt;
Chris Wilson31169712009-09-14 16:50:28 +01004284}