blob: e1ec587c42c8f614ae81ccad60d4bb8e5780a866 [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
Chris Wilsond8cb5082012-08-11 15:41:03 +01001279static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj)
1280{
1281 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
1282 int ret;
1283
1284 if (obj->base.map_list.map)
1285 return 0;
1286
1287 ret = drm_gem_create_mmap_offset(&obj->base);
1288 if (ret != -ENOSPC)
1289 return ret;
1290
1291 /* Badly fragmented mmap space? The only way we can recover
1292 * space is by destroying unwanted objects. We can't randomly release
1293 * mmap_offsets as userspace expects them to be persistent for the
1294 * lifetime of the objects. The closest we can is to release the
1295 * offsets on purgeable objects by truncating it and marking it purged,
1296 * which prevents userspace from ever using that object again.
1297 */
1298 i915_gem_purge(dev_priv, obj->base.size >> PAGE_SHIFT);
1299 ret = drm_gem_create_mmap_offset(&obj->base);
1300 if (ret != -ENOSPC)
1301 return ret;
1302
1303 i915_gem_shrink_all(dev_priv);
1304 return drm_gem_create_mmap_offset(&obj->base);
1305}
1306
1307static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj)
1308{
1309 if (!obj->base.map_list.map)
1310 return;
1311
1312 drm_gem_free_mmap_offset(&obj->base);
1313}
1314
Jesse Barnesde151cf2008-11-12 10:03:55 -08001315int
Dave Airlieff72145b2011-02-07 12:16:14 +10001316i915_gem_mmap_gtt(struct drm_file *file,
1317 struct drm_device *dev,
1318 uint32_t handle,
1319 uint64_t *offset)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001320{
Chris Wilsonda761a62010-10-27 17:37:08 +01001321 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00001322 struct drm_i915_gem_object *obj;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001323 int ret;
1324
Chris Wilson76c1dec2010-09-25 11:22:51 +01001325 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001326 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001327 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001328
Dave Airlieff72145b2011-02-07 12:16:14 +10001329 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00001330 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001331 ret = -ENOENT;
1332 goto unlock;
1333 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001334
Chris Wilson05394f32010-11-08 19:18:58 +00001335 if (obj->base.size > dev_priv->mm.gtt_mappable_end) {
Chris Wilsonda761a62010-10-27 17:37:08 +01001336 ret = -E2BIG;
Eric Anholtff56b0b2011-10-31 23:16:21 -07001337 goto out;
Chris Wilsonda761a62010-10-27 17:37:08 +01001338 }
1339
Chris Wilson05394f32010-11-08 19:18:58 +00001340 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilsonab182822009-09-22 18:46:17 +01001341 DRM_ERROR("Attempting to mmap a purgeable buffer\n");
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001342 ret = -EINVAL;
1343 goto out;
Chris Wilsonab182822009-09-22 18:46:17 +01001344 }
1345
Chris Wilsond8cb5082012-08-11 15:41:03 +01001346 ret = i915_gem_object_create_mmap_offset(obj);
1347 if (ret)
1348 goto out;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001349
Dave Airlieff72145b2011-02-07 12:16:14 +10001350 *offset = (u64)obj->base.map_list.hash.key << PAGE_SHIFT;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001351
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001352out:
Chris Wilson05394f32010-11-08 19:18:58 +00001353 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001354unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001355 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001356 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001357}
1358
Dave Airlieff72145b2011-02-07 12:16:14 +10001359/**
1360 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1361 * @dev: DRM device
1362 * @data: GTT mapping ioctl data
1363 * @file: GEM object info
1364 *
1365 * Simply returns the fake offset to userspace so it can mmap it.
1366 * The mmap call will end up in drm_gem_mmap(), which will set things
1367 * up so we can get faults in the handler above.
1368 *
1369 * The fault handler will take care of binding the object into the GTT
1370 * (since it may have been evicted to make room for something), allocating
1371 * a fence register, and mapping the appropriate aperture address into
1372 * userspace.
1373 */
1374int
1375i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
1376 struct drm_file *file)
1377{
1378 struct drm_i915_gem_mmap_gtt *args = data;
1379
Dave Airlieff72145b2011-02-07 12:16:14 +10001380 return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
1381}
1382
Daniel Vetter225067e2012-08-20 10:23:20 +02001383/* Immediately discard the backing storage */
1384static void
1385i915_gem_object_truncate(struct drm_i915_gem_object *obj)
1386{
1387 struct inode *inode;
1388
1389 /* Our goal here is to return as much of the memory as
1390 * is possible back to the system as we are called from OOM.
1391 * To do this we must instruct the shmfs to drop all of its
1392 * backing pages, *now*.
1393 */
1394 inode = obj->base.filp->f_path.dentry->d_inode;
1395 shmem_truncate_range(inode, 0, (loff_t)-1);
1396
Chris Wilsond8cb5082012-08-11 15:41:03 +01001397 i915_gem_object_free_mmap_offset(obj);
Daniel Vetter225067e2012-08-20 10:23:20 +02001398
1399 obj->madv = __I915_MADV_PURGED;
1400}
1401
1402static inline int
1403i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj)
1404{
1405 return obj->madv == I915_MADV_DONTNEED;
1406}
1407
Chris Wilson6c085a72012-08-20 11:40:46 +02001408static int
Daniel Vetter225067e2012-08-20 10:23:20 +02001409i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj)
1410{
1411 int page_count = obj->base.size / PAGE_SIZE;
Chris Wilson6c085a72012-08-20 11:40:46 +02001412 int ret, i;
Daniel Vetter225067e2012-08-20 10:23:20 +02001413
Chris Wilsonc4670ad2012-08-20 10:23:27 +01001414 BUG_ON(obj->gtt_space);
1415
Chris Wilson6c085a72012-08-20 11:40:46 +02001416 if (obj->pages == NULL)
1417 return 0;
Daniel Vetter225067e2012-08-20 10:23:20 +02001418
Chris Wilson6c085a72012-08-20 11:40:46 +02001419 BUG_ON(obj->gtt_space);
Daniel Vetter225067e2012-08-20 10:23:20 +02001420 BUG_ON(obj->madv == __I915_MADV_PURGED);
1421
Chris Wilson6c085a72012-08-20 11:40:46 +02001422 ret = i915_gem_object_set_to_cpu_domain(obj, true);
1423 if (ret) {
1424 /* In the event of a disaster, abandon all caches and
1425 * hope for the best.
1426 */
1427 WARN_ON(ret != -EIO);
1428 i915_gem_clflush_object(obj);
1429 obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
1430 }
1431
Daniel Vetter225067e2012-08-20 10:23:20 +02001432 if (i915_gem_object_needs_bit17_swizzle(obj))
1433 i915_gem_object_save_bit_17_swizzle(obj);
1434
1435 if (obj->madv == I915_MADV_DONTNEED)
1436 obj->dirty = 0;
1437
1438 for (i = 0; i < page_count; i++) {
1439 if (obj->dirty)
1440 set_page_dirty(obj->pages[i]);
1441
1442 if (obj->madv == I915_MADV_WILLNEED)
1443 mark_page_accessed(obj->pages[i]);
1444
1445 page_cache_release(obj->pages[i]);
1446 }
1447 obj->dirty = 0;
1448
1449 drm_free_large(obj->pages);
1450 obj->pages = NULL;
Chris Wilson6c085a72012-08-20 11:40:46 +02001451
1452 list_del(&obj->gtt_list);
1453
1454 if (i915_gem_object_is_purgeable(obj))
1455 i915_gem_object_truncate(obj);
1456
1457 return 0;
1458}
1459
1460static long
1461i915_gem_purge(struct drm_i915_private *dev_priv, long target)
1462{
1463 struct drm_i915_gem_object *obj, *next;
1464 long count = 0;
1465
1466 list_for_each_entry_safe(obj, next,
1467 &dev_priv->mm.unbound_list,
1468 gtt_list) {
1469 if (i915_gem_object_is_purgeable(obj) &&
1470 i915_gem_object_put_pages_gtt(obj) == 0) {
1471 count += obj->base.size >> PAGE_SHIFT;
1472 if (count >= target)
1473 return count;
1474 }
1475 }
1476
1477 list_for_each_entry_safe(obj, next,
1478 &dev_priv->mm.inactive_list,
1479 mm_list) {
1480 if (i915_gem_object_is_purgeable(obj) &&
1481 i915_gem_object_unbind(obj) == 0 &&
1482 i915_gem_object_put_pages_gtt(obj) == 0) {
1483 count += obj->base.size >> PAGE_SHIFT;
1484 if (count >= target)
1485 return count;
1486 }
1487 }
1488
1489 return count;
1490}
1491
1492static void
1493i915_gem_shrink_all(struct drm_i915_private *dev_priv)
1494{
1495 struct drm_i915_gem_object *obj, *next;
1496
1497 i915_gem_evict_everything(dev_priv->dev);
1498
1499 list_for_each_entry_safe(obj, next, &dev_priv->mm.unbound_list, gtt_list)
1500 i915_gem_object_put_pages_gtt(obj);
Daniel Vetter225067e2012-08-20 10:23:20 +02001501}
1502
Daniel Vetter1286ff72012-05-10 15:25:09 +02001503int
Chris Wilson6c085a72012-08-20 11:40:46 +02001504i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
Chris Wilsone5281cc2010-10-28 13:45:36 +01001505{
Chris Wilson6c085a72012-08-20 11:40:46 +02001506 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001507 int page_count, i;
1508 struct address_space *mapping;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001509 struct page *page;
Chris Wilson6c085a72012-08-20 11:40:46 +02001510 gfp_t gfp;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001511
Daniel Vetter1286ff72012-05-10 15:25:09 +02001512 if (obj->pages || obj->sg_table)
1513 return 0;
1514
Chris Wilson6c085a72012-08-20 11:40:46 +02001515 /* Assert that the object is not currently in any GPU domain. As it
1516 * wasn't in the GTT, there shouldn't be any way it could have been in
1517 * a GPU cache
1518 */
1519 BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
1520 BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
1521
Chris Wilsone5281cc2010-10-28 13:45:36 +01001522 /* Get the list of pages out of our struct file. They'll be pinned
1523 * at this point until we release them.
1524 */
Chris Wilson05394f32010-11-08 19:18:58 +00001525 page_count = obj->base.size / PAGE_SIZE;
Chris Wilson05394f32010-11-08 19:18:58 +00001526 obj->pages = drm_malloc_ab(page_count, sizeof(struct page *));
1527 if (obj->pages == NULL)
Chris Wilsone5281cc2010-10-28 13:45:36 +01001528 return -ENOMEM;
1529
Chris Wilson6c085a72012-08-20 11:40:46 +02001530 /* Fail silently without starting the shrinker */
1531 mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
1532 gfp = mapping_gfp_mask(mapping);
1533 gfp |= __GFP_NORETRY | __GFP_NOWARN | __GFP_NO_KSWAPD;
1534 gfp &= ~(__GFP_IO | __GFP_WAIT);
Chris Wilsone5281cc2010-10-28 13:45:36 +01001535 for (i = 0; i < page_count; i++) {
Chris Wilson6c085a72012-08-20 11:40:46 +02001536 page = shmem_read_mapping_page_gfp(mapping, i, gfp);
1537 if (IS_ERR(page)) {
1538 i915_gem_purge(dev_priv, page_count);
1539 page = shmem_read_mapping_page_gfp(mapping, i, gfp);
1540 }
1541 if (IS_ERR(page)) {
1542 /* We've tried hard to allocate the memory by reaping
1543 * our own buffer, now let the real VM do its job and
1544 * go down in flames if truly OOM.
1545 */
1546 gfp &= ~(__GFP_NORETRY | __GFP_NOWARN | __GFP_NO_KSWAPD);
1547 gfp |= __GFP_IO | __GFP_WAIT;
1548
1549 i915_gem_shrink_all(dev_priv);
1550 page = shmem_read_mapping_page_gfp(mapping, i, gfp);
1551 if (IS_ERR(page))
1552 goto err_pages;
1553
1554 gfp |= __GFP_NORETRY | __GFP_NOWARN | __GFP_NO_KSWAPD;
1555 gfp &= ~(__GFP_IO | __GFP_WAIT);
1556 }
Chris Wilsone5281cc2010-10-28 13:45:36 +01001557
Chris Wilson05394f32010-11-08 19:18:58 +00001558 obj->pages[i] = page;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001559 }
1560
Daniel Vetter6dacfd22011-09-12 21:30:02 +02001561 if (i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilsone5281cc2010-10-28 13:45:36 +01001562 i915_gem_object_do_bit_17_swizzle(obj);
1563
Chris Wilson6c085a72012-08-20 11:40:46 +02001564 list_add_tail(&obj->gtt_list, &dev_priv->mm.unbound_list);
Chris Wilsone5281cc2010-10-28 13:45:36 +01001565 return 0;
1566
1567err_pages:
1568 while (i--)
Chris Wilson05394f32010-11-08 19:18:58 +00001569 page_cache_release(obj->pages[i]);
Chris Wilsone5281cc2010-10-28 13:45:36 +01001570
Chris Wilson05394f32010-11-08 19:18:58 +00001571 drm_free_large(obj->pages);
1572 obj->pages = NULL;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001573 return PTR_ERR(page);
1574}
1575
Chris Wilson54cf91d2010-11-25 18:00:26 +00001576void
Chris Wilson05394f32010-11-08 19:18:58 +00001577i915_gem_object_move_to_active(struct drm_i915_gem_object *obj,
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001578 struct intel_ring_buffer *ring,
1579 u32 seqno)
Eric Anholt673a3942008-07-30 12:06:12 -07001580{
Chris Wilson05394f32010-11-08 19:18:58 +00001581 struct drm_device *dev = obj->base.dev;
Chris Wilson69dc4982010-10-19 10:36:51 +01001582 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter617dbe22010-02-11 22:16:02 +01001583
Zou Nan hai852835f2010-05-21 09:08:56 +08001584 BUG_ON(ring == NULL);
Chris Wilson05394f32010-11-08 19:18:58 +00001585 obj->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001586
1587 /* Add a reference if we're newly entering the active list. */
Chris Wilson05394f32010-11-08 19:18:58 +00001588 if (!obj->active) {
1589 drm_gem_object_reference(&obj->base);
1590 obj->active = 1;
Eric Anholt673a3942008-07-30 12:06:12 -07001591 }
Daniel Vettere35a41d2010-02-11 22:13:59 +01001592
Eric Anholt673a3942008-07-30 12:06:12 -07001593 /* Move from whatever list we were on to the tail of execution. */
Chris Wilson05394f32010-11-08 19:18:58 +00001594 list_move_tail(&obj->mm_list, &dev_priv->mm.active_list);
1595 list_move_tail(&obj->ring_list, &ring->active_list);
Chris Wilsoncaea7472010-11-12 13:53:37 +00001596
Chris Wilson0201f1e2012-07-20 12:41:01 +01001597 obj->last_read_seqno = seqno;
Chris Wilson7dd49062012-03-21 10:48:18 +00001598
Chris Wilsoncaea7472010-11-12 13:53:37 +00001599 if (obj->fenced_gpu_access) {
Chris Wilsoncaea7472010-11-12 13:53:37 +00001600 obj->last_fenced_seqno = seqno;
Chris Wilsoncaea7472010-11-12 13:53:37 +00001601
Chris Wilson7dd49062012-03-21 10:48:18 +00001602 /* Bump MRU to take account of the delayed flush */
1603 if (obj->fence_reg != I915_FENCE_REG_NONE) {
1604 struct drm_i915_fence_reg *reg;
1605
1606 reg = &dev_priv->fence_regs[obj->fence_reg];
1607 list_move_tail(&reg->lru_list,
1608 &dev_priv->mm.fence_list);
1609 }
Chris Wilsoncaea7472010-11-12 13:53:37 +00001610 }
1611}
1612
1613static void
Chris Wilsoncaea7472010-11-12 13:53:37 +00001614i915_gem_object_move_to_inactive(struct drm_i915_gem_object *obj)
1615{
1616 struct drm_device *dev = obj->base.dev;
1617 struct drm_i915_private *dev_priv = dev->dev_private;
1618
Chris Wilson65ce3022012-07-20 12:41:02 +01001619 BUG_ON(obj->base.write_domain & ~I915_GEM_GPU_DOMAINS);
Chris Wilsoncaea7472010-11-12 13:53:37 +00001620 BUG_ON(!obj->active);
Chris Wilson65ce3022012-07-20 12:41:02 +01001621
Chris Wilsonf047e392012-07-21 12:31:41 +01001622 if (obj->pin_count) /* are we a framebuffer? */
1623 intel_mark_fb_idle(obj);
1624
1625 list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
1626
Chris Wilson65ce3022012-07-20 12:41:02 +01001627 list_del_init(&obj->ring_list);
Chris Wilsoncaea7472010-11-12 13:53:37 +00001628 obj->ring = NULL;
1629
Chris Wilson65ce3022012-07-20 12:41:02 +01001630 obj->last_read_seqno = 0;
1631 obj->last_write_seqno = 0;
1632 obj->base.write_domain = 0;
1633
1634 obj->last_fenced_seqno = 0;
Chris Wilsoncaea7472010-11-12 13:53:37 +00001635 obj->fenced_gpu_access = false;
Chris Wilsoncaea7472010-11-12 13:53:37 +00001636
1637 obj->active = 0;
1638 drm_gem_object_unreference(&obj->base);
1639
1640 WARN_ON(i915_verify_lists(dev));
Eric Anholtce44b0e2008-11-06 16:00:31 -08001641}
Eric Anholt673a3942008-07-30 12:06:12 -07001642
Daniel Vetter53d227f2012-01-25 16:32:49 +01001643static u32
1644i915_gem_get_seqno(struct drm_device *dev)
1645{
1646 drm_i915_private_t *dev_priv = dev->dev_private;
1647 u32 seqno = dev_priv->next_seqno;
1648
1649 /* reserve 0 for non-seqno */
1650 if (++dev_priv->next_seqno == 0)
1651 dev_priv->next_seqno = 1;
1652
1653 return seqno;
1654}
1655
1656u32
1657i915_gem_next_request_seqno(struct intel_ring_buffer *ring)
1658{
1659 if (ring->outstanding_lazy_request == 0)
1660 ring->outstanding_lazy_request = i915_gem_get_seqno(ring->dev);
1661
1662 return ring->outstanding_lazy_request;
1663}
1664
Chris Wilson3cce4692010-10-27 16:11:02 +01001665int
Chris Wilsondb53a302011-02-03 11:57:46 +00001666i915_add_request(struct intel_ring_buffer *ring,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001667 struct drm_file *file,
Chris Wilsondb53a302011-02-03 11:57:46 +00001668 struct drm_i915_gem_request *request)
Eric Anholt673a3942008-07-30 12:06:12 -07001669{
Chris Wilsondb53a302011-02-03 11:57:46 +00001670 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07001671 uint32_t seqno;
Chris Wilsona71d8d92012-02-15 11:25:36 +00001672 u32 request_ring_position;
Eric Anholt673a3942008-07-30 12:06:12 -07001673 int was_empty;
Chris Wilson3cce4692010-10-27 16:11:02 +01001674 int ret;
1675
Daniel Vettercc889e02012-06-13 20:45:19 +02001676 /*
1677 * Emit any outstanding flushes - execbuf can fail to emit the flush
1678 * after having emitted the batchbuffer command. Hence we need to fix
1679 * things up similar to emitting the lazy request. The difference here
1680 * is that the flush _must_ happen before the next request, no matter
1681 * what.
1682 */
Chris Wilsona7b97612012-07-20 12:41:08 +01001683 ret = intel_ring_flush_all_caches(ring);
1684 if (ret)
1685 return ret;
Daniel Vettercc889e02012-06-13 20:45:19 +02001686
Chris Wilson3bb73ab2012-07-20 12:40:59 +01001687 if (request == NULL) {
1688 request = kmalloc(sizeof(*request), GFP_KERNEL);
1689 if (request == NULL)
1690 return -ENOMEM;
1691 }
1692
Daniel Vetter53d227f2012-01-25 16:32:49 +01001693 seqno = i915_gem_next_request_seqno(ring);
Eric Anholt673a3942008-07-30 12:06:12 -07001694
Chris Wilsona71d8d92012-02-15 11:25:36 +00001695 /* Record the position of the start of the request so that
1696 * should we detect the updated seqno part-way through the
1697 * GPU processing the request, we never over-estimate the
1698 * position of the head.
1699 */
1700 request_ring_position = intel_ring_get_tail(ring);
1701
Chris Wilson3cce4692010-10-27 16:11:02 +01001702 ret = ring->add_request(ring, &seqno);
Chris Wilson3bb73ab2012-07-20 12:40:59 +01001703 if (ret) {
1704 kfree(request);
1705 return ret;
1706 }
Eric Anholt673a3942008-07-30 12:06:12 -07001707
Chris Wilsondb53a302011-02-03 11:57:46 +00001708 trace_i915_gem_request_add(ring, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07001709
1710 request->seqno = seqno;
Zou Nan hai852835f2010-05-21 09:08:56 +08001711 request->ring = ring;
Chris Wilsona71d8d92012-02-15 11:25:36 +00001712 request->tail = request_ring_position;
Eric Anholt673a3942008-07-30 12:06:12 -07001713 request->emitted_jiffies = jiffies;
Zou Nan hai852835f2010-05-21 09:08:56 +08001714 was_empty = list_empty(&ring->request_list);
1715 list_add_tail(&request->list, &ring->request_list);
Chris Wilson3bb73ab2012-07-20 12:40:59 +01001716 request->file_priv = NULL;
Zou Nan hai852835f2010-05-21 09:08:56 +08001717
Chris Wilsondb53a302011-02-03 11:57:46 +00001718 if (file) {
1719 struct drm_i915_file_private *file_priv = file->driver_priv;
1720
Chris Wilson1c255952010-09-26 11:03:27 +01001721 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001722 request->file_priv = file_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00001723 list_add_tail(&request->client_list,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001724 &file_priv->mm.request_list);
Chris Wilson1c255952010-09-26 11:03:27 +01001725 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00001726 }
Eric Anholt673a3942008-07-30 12:06:12 -07001727
Daniel Vetter5391d0c2012-01-25 14:03:57 +01001728 ring->outstanding_lazy_request = 0;
Chris Wilsondb53a302011-02-03 11:57:46 +00001729
Ben Gamarif65d9422009-09-14 17:48:44 -04001730 if (!dev_priv->mm.suspended) {
Ben Widawsky3e0dc6b2011-06-29 10:26:42 -07001731 if (i915_enable_hangcheck) {
1732 mod_timer(&dev_priv->hangcheck_timer,
1733 jiffies +
1734 msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
1735 }
Chris Wilsonf047e392012-07-21 12:31:41 +01001736 if (was_empty) {
Chris Wilsonb3b079d2010-09-13 23:44:34 +01001737 queue_delayed_work(dev_priv->wq,
1738 &dev_priv->mm.retire_work, HZ);
Chris Wilsonf047e392012-07-21 12:31:41 +01001739 intel_mark_busy(dev_priv->dev);
1740 }
Ben Gamarif65d9422009-09-14 17:48:44 -04001741 }
Daniel Vettercc889e02012-06-13 20:45:19 +02001742
Chris Wilson3cce4692010-10-27 16:11:02 +01001743 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001744}
1745
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001746static inline void
1747i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
Eric Anholt673a3942008-07-30 12:06:12 -07001748{
Chris Wilson1c255952010-09-26 11:03:27 +01001749 struct drm_i915_file_private *file_priv = request->file_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07001750
Chris Wilson1c255952010-09-26 11:03:27 +01001751 if (!file_priv)
1752 return;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001753
Chris Wilson1c255952010-09-26 11:03:27 +01001754 spin_lock(&file_priv->mm.lock);
Herton Ronaldo Krzesinski09bfa512011-03-17 13:45:12 +00001755 if (request->file_priv) {
1756 list_del(&request->client_list);
1757 request->file_priv = NULL;
1758 }
Chris Wilson1c255952010-09-26 11:03:27 +01001759 spin_unlock(&file_priv->mm.lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001760}
1761
Chris Wilsondfaae392010-09-22 10:31:52 +01001762static void i915_gem_reset_ring_lists(struct drm_i915_private *dev_priv,
1763 struct intel_ring_buffer *ring)
Chris Wilson9375e442010-09-19 12:21:28 +01001764{
Chris Wilsondfaae392010-09-22 10:31:52 +01001765 while (!list_empty(&ring->request_list)) {
1766 struct drm_i915_gem_request *request;
Chris Wilson9375e442010-09-19 12:21:28 +01001767
Chris Wilsondfaae392010-09-22 10:31:52 +01001768 request = list_first_entry(&ring->request_list,
1769 struct drm_i915_gem_request,
1770 list);
1771
1772 list_del(&request->list);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001773 i915_gem_request_remove_from_client(request);
Chris Wilsondfaae392010-09-22 10:31:52 +01001774 kfree(request);
1775 }
1776
1777 while (!list_empty(&ring->active_list)) {
Chris Wilson05394f32010-11-08 19:18:58 +00001778 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001779
Chris Wilson05394f32010-11-08 19:18:58 +00001780 obj = list_first_entry(&ring->active_list,
1781 struct drm_i915_gem_object,
1782 ring_list);
Eric Anholt673a3942008-07-30 12:06:12 -07001783
Chris Wilson05394f32010-11-08 19:18:58 +00001784 i915_gem_object_move_to_inactive(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001785 }
Eric Anholt673a3942008-07-30 12:06:12 -07001786}
1787
Chris Wilson312817a2010-11-22 11:50:11 +00001788static void i915_gem_reset_fences(struct drm_device *dev)
1789{
1790 struct drm_i915_private *dev_priv = dev->dev_private;
1791 int i;
1792
Daniel Vetter4b9de732011-10-09 21:52:02 +02001793 for (i = 0; i < dev_priv->num_fence_regs; i++) {
Chris Wilson312817a2010-11-22 11:50:11 +00001794 struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
Chris Wilson7d2cb392010-11-27 17:38:29 +00001795
Chris Wilsonada726c2012-04-17 15:31:32 +01001796 i915_gem_write_fence(dev, i, NULL);
Chris Wilson7d2cb392010-11-27 17:38:29 +00001797
Chris Wilsonada726c2012-04-17 15:31:32 +01001798 if (reg->obj)
1799 i915_gem_object_fence_lost(reg->obj);
Chris Wilson7d2cb392010-11-27 17:38:29 +00001800
Chris Wilsonada726c2012-04-17 15:31:32 +01001801 reg->pin_count = 0;
1802 reg->obj = NULL;
1803 INIT_LIST_HEAD(&reg->lru_list);
Chris Wilson312817a2010-11-22 11:50:11 +00001804 }
Chris Wilsonada726c2012-04-17 15:31:32 +01001805
1806 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Chris Wilson312817a2010-11-22 11:50:11 +00001807}
1808
Chris Wilson069efc12010-09-30 16:53:18 +01001809void i915_gem_reset(struct drm_device *dev)
Eric Anholt673a3942008-07-30 12:06:12 -07001810{
Chris Wilsondfaae392010-09-22 10:31:52 +01001811 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00001812 struct drm_i915_gem_object *obj;
Chris Wilsonb4519512012-05-11 14:29:30 +01001813 struct intel_ring_buffer *ring;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001814 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07001815
Chris Wilsonb4519512012-05-11 14:29:30 +01001816 for_each_ring(ring, dev_priv, i)
1817 i915_gem_reset_ring_lists(dev_priv, ring);
Chris Wilsondfaae392010-09-22 10:31:52 +01001818
Chris Wilsondfaae392010-09-22 10:31:52 +01001819 /* Move everything out of the GPU domains to ensure we do any
1820 * necessary invalidation upon reuse.
1821 */
Chris Wilson05394f32010-11-08 19:18:58 +00001822 list_for_each_entry(obj,
Chris Wilson77f01232010-09-19 12:31:36 +01001823 &dev_priv->mm.inactive_list,
Chris Wilson69dc4982010-10-19 10:36:51 +01001824 mm_list)
Chris Wilson77f01232010-09-19 12:31:36 +01001825 {
Chris Wilson05394f32010-11-08 19:18:58 +00001826 obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
Chris Wilson77f01232010-09-19 12:31:36 +01001827 }
Chris Wilson069efc12010-09-30 16:53:18 +01001828
Chris Wilson6c085a72012-08-20 11:40:46 +02001829
Chris Wilson069efc12010-09-30 16:53:18 +01001830 /* The fence registers are invalidated so clear them out */
Chris Wilson312817a2010-11-22 11:50:11 +00001831 i915_gem_reset_fences(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07001832}
1833
1834/**
1835 * This function clears the request list as sequence numbers are passed.
1836 */
Chris Wilsona71d8d92012-02-15 11:25:36 +00001837void
Chris Wilsondb53a302011-02-03 11:57:46 +00001838i915_gem_retire_requests_ring(struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001839{
Eric Anholt673a3942008-07-30 12:06:12 -07001840 uint32_t seqno;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001841 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07001842
Chris Wilsondb53a302011-02-03 11:57:46 +00001843 if (list_empty(&ring->request_list))
Karsten Wiese6c0594a2009-02-23 15:07:57 +01001844 return;
1845
Chris Wilsondb53a302011-02-03 11:57:46 +00001846 WARN_ON(i915_verify_lists(ring->dev));
Eric Anholt673a3942008-07-30 12:06:12 -07001847
Chris Wilsonb2eadbc2012-08-09 10:58:30 +01001848 seqno = ring->get_seqno(ring, true);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001849
Chris Wilson076e2c02011-01-21 10:07:18 +00001850 for (i = 0; i < ARRAY_SIZE(ring->sync_seqno); i++)
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001851 if (seqno >= ring->sync_seqno[i])
1852 ring->sync_seqno[i] = 0;
1853
Zou Nan hai852835f2010-05-21 09:08:56 +08001854 while (!list_empty(&ring->request_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001855 struct drm_i915_gem_request *request;
Eric Anholt673a3942008-07-30 12:06:12 -07001856
Zou Nan hai852835f2010-05-21 09:08:56 +08001857 request = list_first_entry(&ring->request_list,
Eric Anholt673a3942008-07-30 12:06:12 -07001858 struct drm_i915_gem_request,
1859 list);
Eric Anholt673a3942008-07-30 12:06:12 -07001860
Chris Wilsondfaae392010-09-22 10:31:52 +01001861 if (!i915_seqno_passed(seqno, request->seqno))
Eric Anholt673a3942008-07-30 12:06:12 -07001862 break;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001863
Chris Wilsondb53a302011-02-03 11:57:46 +00001864 trace_i915_gem_request_retire(ring, request->seqno);
Chris Wilsona71d8d92012-02-15 11:25:36 +00001865 /* We know the GPU must have read the request to have
1866 * sent us the seqno + interrupt, so use the position
1867 * of tail of the request to update the last known position
1868 * of the GPU head.
1869 */
1870 ring->last_retired_head = request->tail;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001871
1872 list_del(&request->list);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001873 i915_gem_request_remove_from_client(request);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001874 kfree(request);
1875 }
1876
1877 /* Move any buffers on the active list that are no longer referenced
1878 * by the ringbuffer to the flushing/inactive lists as appropriate.
1879 */
1880 while (!list_empty(&ring->active_list)) {
Chris Wilson05394f32010-11-08 19:18:58 +00001881 struct drm_i915_gem_object *obj;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001882
Akshay Joshi0206e352011-08-16 15:34:10 -04001883 obj = list_first_entry(&ring->active_list,
Chris Wilson05394f32010-11-08 19:18:58 +00001884 struct drm_i915_gem_object,
1885 ring_list);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001886
Chris Wilson0201f1e2012-07-20 12:41:01 +01001887 if (!i915_seqno_passed(seqno, obj->last_read_seqno))
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001888 break;
1889
Chris Wilson65ce3022012-07-20 12:41:02 +01001890 i915_gem_object_move_to_inactive(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001891 }
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001892
Chris Wilsondb53a302011-02-03 11:57:46 +00001893 if (unlikely(ring->trace_irq_seqno &&
1894 i915_seqno_passed(seqno, ring->trace_irq_seqno))) {
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001895 ring->irq_put(ring);
Chris Wilsondb53a302011-02-03 11:57:46 +00001896 ring->trace_irq_seqno = 0;
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001897 }
Chris Wilson23bc5982010-09-29 16:10:57 +01001898
Chris Wilsondb53a302011-02-03 11:57:46 +00001899 WARN_ON(i915_verify_lists(ring->dev));
Eric Anholt673a3942008-07-30 12:06:12 -07001900}
1901
1902void
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001903i915_gem_retire_requests(struct drm_device *dev)
1904{
1905 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonb4519512012-05-11 14:29:30 +01001906 struct intel_ring_buffer *ring;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001907 int i;
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001908
Chris Wilsonb4519512012-05-11 14:29:30 +01001909 for_each_ring(ring, dev_priv, i)
1910 i915_gem_retire_requests_ring(ring);
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001911}
1912
Daniel Vetter75ef9da2010-08-21 00:25:16 +02001913static void
Eric Anholt673a3942008-07-30 12:06:12 -07001914i915_gem_retire_work_handler(struct work_struct *work)
1915{
1916 drm_i915_private_t *dev_priv;
1917 struct drm_device *dev;
Chris Wilsonb4519512012-05-11 14:29:30 +01001918 struct intel_ring_buffer *ring;
Chris Wilson0a587052011-01-09 21:05:44 +00001919 bool idle;
1920 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07001921
1922 dev_priv = container_of(work, drm_i915_private_t,
1923 mm.retire_work.work);
1924 dev = dev_priv->dev;
1925
Chris Wilson891b48c2010-09-29 12:26:37 +01001926 /* Come back later if the device is busy... */
1927 if (!mutex_trylock(&dev->struct_mutex)) {
1928 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
1929 return;
1930 }
1931
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001932 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08001933
Chris Wilson0a587052011-01-09 21:05:44 +00001934 /* Send a periodic flush down the ring so we don't hold onto GEM
1935 * objects indefinitely.
1936 */
1937 idle = true;
Chris Wilsonb4519512012-05-11 14:29:30 +01001938 for_each_ring(ring, dev_priv, i) {
Chris Wilson3bb73ab2012-07-20 12:40:59 +01001939 if (ring->gpu_caches_dirty)
1940 i915_add_request(ring, NULL, NULL);
Chris Wilson0a587052011-01-09 21:05:44 +00001941
1942 idle &= list_empty(&ring->request_list);
1943 }
1944
1945 if (!dev_priv->mm.suspended && !idle)
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001946 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
Chris Wilsonf047e392012-07-21 12:31:41 +01001947 if (idle)
1948 intel_mark_idle(dev);
Chris Wilson0a587052011-01-09 21:05:44 +00001949
Eric Anholt673a3942008-07-30 12:06:12 -07001950 mutex_unlock(&dev->struct_mutex);
1951}
1952
Daniel Vetterd6b2c792012-07-04 22:54:13 +02001953int
1954i915_gem_check_wedge(struct drm_i915_private *dev_priv,
1955 bool interruptible)
Ben Widawskyb4aca012012-04-25 20:50:12 -07001956{
Ben Widawskyb4aca012012-04-25 20:50:12 -07001957 if (atomic_read(&dev_priv->mm.wedged)) {
1958 struct completion *x = &dev_priv->error_completion;
1959 bool recovery_complete;
1960 unsigned long flags;
1961
1962 /* Give the error handler a chance to run. */
1963 spin_lock_irqsave(&x->wait.lock, flags);
1964 recovery_complete = x->done > 0;
1965 spin_unlock_irqrestore(&x->wait.lock, flags);
1966
Daniel Vetterd6b2c792012-07-04 22:54:13 +02001967 /* Non-interruptible callers can't handle -EAGAIN, hence return
1968 * -EIO unconditionally for these. */
1969 if (!interruptible)
1970 return -EIO;
1971
1972 /* Recovery complete, but still wedged means reset failure. */
1973 if (recovery_complete)
1974 return -EIO;
1975
1976 return -EAGAIN;
Ben Widawskyb4aca012012-04-25 20:50:12 -07001977 }
1978
1979 return 0;
1980}
1981
1982/*
1983 * Compare seqno against outstanding lazy request. Emit a request if they are
1984 * equal.
1985 */
1986static int
1987i915_gem_check_olr(struct intel_ring_buffer *ring, u32 seqno)
1988{
Chris Wilson3bb73ab2012-07-20 12:40:59 +01001989 int ret;
Ben Widawskyb4aca012012-04-25 20:50:12 -07001990
1991 BUG_ON(!mutex_is_locked(&ring->dev->struct_mutex));
1992
Chris Wilson3bb73ab2012-07-20 12:40:59 +01001993 ret = 0;
1994 if (seqno == ring->outstanding_lazy_request)
1995 ret = i915_add_request(ring, NULL, NULL);
Ben Widawskyb4aca012012-04-25 20:50:12 -07001996
1997 return ret;
1998}
1999
Ben Widawsky5c81fe852012-05-24 15:03:08 -07002000/**
2001 * __wait_seqno - wait until execution of seqno has finished
2002 * @ring: the ring expected to report seqno
2003 * @seqno: duh!
2004 * @interruptible: do an interruptible wait (normally yes)
2005 * @timeout: in - how long to wait (NULL forever); out - how much time remaining
2006 *
2007 * Returns 0 if the seqno was found within the alloted time. Else returns the
2008 * errno with remaining time filled in timeout argument.
2009 */
Ben Widawsky604dd3e2012-04-26 16:03:03 -07002010static int __wait_seqno(struct intel_ring_buffer *ring, u32 seqno,
Ben Widawsky5c81fe852012-05-24 15:03:08 -07002011 bool interruptible, struct timespec *timeout)
Ben Widawsky604dd3e2012-04-26 16:03:03 -07002012{
2013 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Ben Widawsky5c81fe852012-05-24 15:03:08 -07002014 struct timespec before, now, wait_time={1,0};
2015 unsigned long timeout_jiffies;
2016 long end;
2017 bool wait_forever = true;
Daniel Vetterd6b2c792012-07-04 22:54:13 +02002018 int ret;
Ben Widawsky604dd3e2012-04-26 16:03:03 -07002019
Chris Wilsonb2eadbc2012-08-09 10:58:30 +01002020 if (i915_seqno_passed(ring->get_seqno(ring, true), seqno))
Ben Widawsky604dd3e2012-04-26 16:03:03 -07002021 return 0;
2022
2023 trace_i915_gem_request_wait_begin(ring, seqno);
Ben Widawsky5c81fe852012-05-24 15:03:08 -07002024
2025 if (timeout != NULL) {
2026 wait_time = *timeout;
2027 wait_forever = false;
2028 }
2029
2030 timeout_jiffies = timespec_to_jiffies(&wait_time);
2031
Ben Widawsky604dd3e2012-04-26 16:03:03 -07002032 if (WARN_ON(!ring->irq_get(ring)))
2033 return -ENODEV;
2034
Ben Widawsky5c81fe852012-05-24 15:03:08 -07002035 /* Record current time in case interrupted by signal, or wedged * */
2036 getrawmonotonic(&before);
2037
Ben Widawsky604dd3e2012-04-26 16:03:03 -07002038#define EXIT_COND \
Chris Wilsonb2eadbc2012-08-09 10:58:30 +01002039 (i915_seqno_passed(ring->get_seqno(ring, false), seqno) || \
Ben Widawsky604dd3e2012-04-26 16:03:03 -07002040 atomic_read(&dev_priv->mm.wedged))
Ben Widawsky5c81fe852012-05-24 15:03:08 -07002041 do {
2042 if (interruptible)
2043 end = wait_event_interruptible_timeout(ring->irq_queue,
2044 EXIT_COND,
2045 timeout_jiffies);
2046 else
2047 end = wait_event_timeout(ring->irq_queue, EXIT_COND,
2048 timeout_jiffies);
Ben Widawsky604dd3e2012-04-26 16:03:03 -07002049
Daniel Vetterd6b2c792012-07-04 22:54:13 +02002050 ret = i915_gem_check_wedge(dev_priv, interruptible);
2051 if (ret)
2052 end = ret;
Ben Widawsky5c81fe852012-05-24 15:03:08 -07002053 } while (end == 0 && wait_forever);
2054
2055 getrawmonotonic(&now);
Ben Widawsky604dd3e2012-04-26 16:03:03 -07002056
2057 ring->irq_put(ring);
2058 trace_i915_gem_request_wait_end(ring, seqno);
2059#undef EXIT_COND
2060
Ben Widawsky5c81fe852012-05-24 15:03:08 -07002061 if (timeout) {
2062 struct timespec sleep_time = timespec_sub(now, before);
2063 *timeout = timespec_sub(*timeout, sleep_time);
2064 }
2065
2066 switch (end) {
Chris Wilsoneeef9b32012-07-16 13:05:34 +01002067 case -EIO:
Ben Widawsky5c81fe852012-05-24 15:03:08 -07002068 case -EAGAIN: /* Wedged */
2069 case -ERESTARTSYS: /* Signal */
2070 return (int)end;
2071 case 0: /* Timeout */
2072 if (timeout)
2073 set_normalized_timespec(timeout, 0, 0);
2074 return -ETIME;
2075 default: /* Completed */
2076 WARN_ON(end < 0); /* We're not aware of other errors */
2077 return 0;
2078 }
Ben Widawsky604dd3e2012-04-26 16:03:03 -07002079}
2080
Chris Wilsondb53a302011-02-03 11:57:46 +00002081/**
2082 * Waits for a sequence number to be signaled, and cleans up the
2083 * request and object lists appropriately for that event.
2084 */
Daniel Vetter5a5a0c62009-09-15 22:57:36 +02002085int
Ben Widawsky199b2bc2012-05-24 15:03:11 -07002086i915_wait_seqno(struct intel_ring_buffer *ring, uint32_t seqno)
Eric Anholt673a3942008-07-30 12:06:12 -07002087{
Chris Wilsondb53a302011-02-03 11:57:46 +00002088 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07002089 int ret = 0;
2090
2091 BUG_ON(seqno == 0);
2092
Daniel Vetterd6b2c792012-07-04 22:54:13 +02002093 ret = i915_gem_check_wedge(dev_priv, dev_priv->mm.interruptible);
Ben Widawskyb4aca012012-04-25 20:50:12 -07002094 if (ret)
2095 return ret;
Chris Wilsond9bc7e92011-02-07 13:09:31 +00002096
Ben Widawskyb4aca012012-04-25 20:50:12 -07002097 ret = i915_gem_check_olr(ring, seqno);
2098 if (ret)
2099 return ret;
Daniel Vettere35a41d2010-02-11 22:13:59 +01002100
Ben Widawsky5c81fe852012-05-24 15:03:08 -07002101 ret = __wait_seqno(ring, seqno, dev_priv->mm.interruptible, NULL);
Eric Anholt673a3942008-07-30 12:06:12 -07002102
Eric Anholt673a3942008-07-30 12:06:12 -07002103 return ret;
2104}
2105
Daniel Vetter48764bf2009-09-15 22:57:32 +02002106/**
Eric Anholt673a3942008-07-30 12:06:12 -07002107 * Ensures that all rendering to the object has completed and the object is
2108 * safe to unbind from the GTT or access from the CPU.
2109 */
Chris Wilson0201f1e2012-07-20 12:41:01 +01002110static __must_check int
2111i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj,
2112 bool readonly)
Eric Anholt673a3942008-07-30 12:06:12 -07002113{
Chris Wilson0201f1e2012-07-20 12:41:01 +01002114 u32 seqno;
Eric Anholt673a3942008-07-30 12:06:12 -07002115 int ret;
2116
Eric Anholt673a3942008-07-30 12:06:12 -07002117 /* If there is rendering queued on the buffer being evicted, wait for
2118 * it.
2119 */
Chris Wilson0201f1e2012-07-20 12:41:01 +01002120 if (readonly)
2121 seqno = obj->last_write_seqno;
2122 else
2123 seqno = obj->last_read_seqno;
2124 if (seqno == 0)
2125 return 0;
2126
2127 ret = i915_wait_seqno(obj->ring, seqno);
2128 if (ret)
2129 return ret;
2130
2131 /* Manually manage the write flush as we may have not yet retired
2132 * the buffer.
2133 */
2134 if (obj->last_write_seqno &&
2135 i915_seqno_passed(seqno, obj->last_write_seqno)) {
2136 obj->last_write_seqno = 0;
2137 obj->base.write_domain &= ~I915_GEM_GPU_DOMAINS;
Eric Anholt673a3942008-07-30 12:06:12 -07002138 }
2139
Chris Wilson0201f1e2012-07-20 12:41:01 +01002140 i915_gem_retire_requests_ring(obj->ring);
Eric Anholt673a3942008-07-30 12:06:12 -07002141 return 0;
2142}
2143
Ben Widawsky5816d642012-04-11 11:18:19 -07002144/**
Daniel Vetter30dfebf2012-06-01 15:21:23 +02002145 * Ensures that an object will eventually get non-busy by flushing any required
2146 * write domains, emitting any outstanding lazy request and retiring and
2147 * completed requests.
2148 */
2149static int
2150i915_gem_object_flush_active(struct drm_i915_gem_object *obj)
2151{
2152 int ret;
2153
2154 if (obj->active) {
Chris Wilson0201f1e2012-07-20 12:41:01 +01002155 ret = i915_gem_check_olr(obj->ring, obj->last_read_seqno);
Daniel Vetter30dfebf2012-06-01 15:21:23 +02002156 if (ret)
2157 return ret;
Chris Wilson0201f1e2012-07-20 12:41:01 +01002158
Daniel Vetter30dfebf2012-06-01 15:21:23 +02002159 i915_gem_retire_requests_ring(obj->ring);
2160 }
2161
2162 return 0;
2163}
2164
2165/**
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002166 * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
2167 * @DRM_IOCTL_ARGS: standard ioctl arguments
2168 *
2169 * Returns 0 if successful, else an error is returned with the remaining time in
2170 * the timeout parameter.
2171 * -ETIME: object is still busy after timeout
2172 * -ERESTARTSYS: signal interrupted the wait
2173 * -ENONENT: object doesn't exist
2174 * Also possible, but rare:
2175 * -EAGAIN: GPU wedged
2176 * -ENOMEM: damn
2177 * -ENODEV: Internal IRQ fail
2178 * -E?: The add request failed
2179 *
2180 * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
2181 * non-zero timeout parameter the wait ioctl will wait for the given number of
2182 * nanoseconds on an object becoming unbusy. Since the wait itself does so
2183 * without holding struct_mutex the object may become re-busied before this
2184 * function completes. A similar but shorter * race condition exists in the busy
2185 * ioctl
2186 */
2187int
2188i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
2189{
2190 struct drm_i915_gem_wait *args = data;
2191 struct drm_i915_gem_object *obj;
2192 struct intel_ring_buffer *ring = NULL;
Ben Widawskyeac1f142012-06-05 15:24:24 -07002193 struct timespec timeout_stack, *timeout = NULL;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002194 u32 seqno = 0;
2195 int ret = 0;
2196
Ben Widawskyeac1f142012-06-05 15:24:24 -07002197 if (args->timeout_ns >= 0) {
2198 timeout_stack = ns_to_timespec(args->timeout_ns);
2199 timeout = &timeout_stack;
2200 }
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002201
2202 ret = i915_mutex_lock_interruptible(dev);
2203 if (ret)
2204 return ret;
2205
2206 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->bo_handle));
2207 if (&obj->base == NULL) {
2208 mutex_unlock(&dev->struct_mutex);
2209 return -ENOENT;
2210 }
2211
Daniel Vetter30dfebf2012-06-01 15:21:23 +02002212 /* Need to make sure the object gets inactive eventually. */
2213 ret = i915_gem_object_flush_active(obj);
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002214 if (ret)
2215 goto out;
2216
2217 if (obj->active) {
Chris Wilson0201f1e2012-07-20 12:41:01 +01002218 seqno = obj->last_read_seqno;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002219 ring = obj->ring;
2220 }
2221
2222 if (seqno == 0)
2223 goto out;
2224
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002225 /* Do this after OLR check to make sure we make forward progress polling
2226 * on this IOCTL with a 0 timeout (like busy ioctl)
2227 */
2228 if (!args->timeout_ns) {
2229 ret = -ETIME;
2230 goto out;
2231 }
2232
2233 drm_gem_object_unreference(&obj->base);
2234 mutex_unlock(&dev->struct_mutex);
2235
Ben Widawskyeac1f142012-06-05 15:24:24 -07002236 ret = __wait_seqno(ring, seqno, true, timeout);
2237 if (timeout) {
2238 WARN_ON(!timespec_valid(timeout));
2239 args->timeout_ns = timespec_to_ns(timeout);
2240 }
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002241 return ret;
2242
2243out:
2244 drm_gem_object_unreference(&obj->base);
2245 mutex_unlock(&dev->struct_mutex);
2246 return ret;
2247}
2248
2249/**
Ben Widawsky5816d642012-04-11 11:18:19 -07002250 * i915_gem_object_sync - sync an object to a ring.
2251 *
2252 * @obj: object which may be in use on another ring.
2253 * @to: ring we wish to use the object on. May be NULL.
2254 *
2255 * This code is meant to abstract object synchronization with the GPU.
2256 * Calling with NULL implies synchronizing the object with the CPU
2257 * rather than a particular GPU ring.
2258 *
2259 * Returns 0 if successful, else propagates up the lower layer error.
2260 */
Ben Widawsky2911a352012-04-05 14:47:36 -07002261int
2262i915_gem_object_sync(struct drm_i915_gem_object *obj,
2263 struct intel_ring_buffer *to)
2264{
2265 struct intel_ring_buffer *from = obj->ring;
2266 u32 seqno;
2267 int ret, idx;
2268
2269 if (from == NULL || to == from)
2270 return 0;
2271
Ben Widawsky5816d642012-04-11 11:18:19 -07002272 if (to == NULL || !i915_semaphore_is_enabled(obj->base.dev))
Chris Wilson0201f1e2012-07-20 12:41:01 +01002273 return i915_gem_object_wait_rendering(obj, false);
Ben Widawsky2911a352012-04-05 14:47:36 -07002274
2275 idx = intel_ring_sync_index(from, to);
2276
Chris Wilson0201f1e2012-07-20 12:41:01 +01002277 seqno = obj->last_read_seqno;
Ben Widawsky2911a352012-04-05 14:47:36 -07002278 if (seqno <= from->sync_seqno[idx])
2279 return 0;
2280
Ben Widawskyb4aca012012-04-25 20:50:12 -07002281 ret = i915_gem_check_olr(obj->ring, seqno);
2282 if (ret)
2283 return ret;
Ben Widawsky2911a352012-04-05 14:47:36 -07002284
Ben Widawsky1500f7e2012-04-11 11:18:21 -07002285 ret = to->sync_to(to, from, seqno);
Ben Widawskye3a5a222012-04-11 11:18:20 -07002286 if (!ret)
2287 from->sync_seqno[idx] = seqno;
Ben Widawsky2911a352012-04-05 14:47:36 -07002288
Ben Widawskye3a5a222012-04-11 11:18:20 -07002289 return ret;
Ben Widawsky2911a352012-04-05 14:47:36 -07002290}
2291
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01002292static void i915_gem_object_finish_gtt(struct drm_i915_gem_object *obj)
2293{
2294 u32 old_write_domain, old_read_domains;
2295
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01002296 /* Act a barrier for all accesses through the GTT */
2297 mb();
2298
2299 /* Force a pagefault for domain tracking on next user access */
2300 i915_gem_release_mmap(obj);
2301
Keith Packardb97c3d92011-06-24 21:02:59 -07002302 if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
2303 return;
2304
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01002305 old_read_domains = obj->base.read_domains;
2306 old_write_domain = obj->base.write_domain;
2307
2308 obj->base.read_domains &= ~I915_GEM_DOMAIN_GTT;
2309 obj->base.write_domain &= ~I915_GEM_DOMAIN_GTT;
2310
2311 trace_i915_gem_object_change_domain(obj,
2312 old_read_domains,
2313 old_write_domain);
2314}
2315
Eric Anholt673a3942008-07-30 12:06:12 -07002316/**
2317 * Unbinds an object from the GTT aperture.
2318 */
Jesse Barnes0f973f22009-01-26 17:10:45 -08002319int
Chris Wilson05394f32010-11-08 19:18:58 +00002320i915_gem_object_unbind(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002321{
Daniel Vetter7bddb012012-02-09 17:15:47 +01002322 drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07002323 int ret = 0;
2324
Chris Wilson05394f32010-11-08 19:18:58 +00002325 if (obj->gtt_space == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002326 return 0;
2327
Chris Wilson31d8d652012-05-24 19:11:20 +01002328 if (obj->pin_count)
2329 return -EBUSY;
Eric Anholt673a3942008-07-30 12:06:12 -07002330
Chris Wilsonc4670ad2012-08-20 10:23:27 +01002331 BUG_ON(obj->pages == NULL);
2332
Chris Wilsona8198ee2011-04-13 22:04:09 +01002333 ret = i915_gem_object_finish_gpu(obj);
Chris Wilson1488fc02012-04-24 15:47:31 +01002334 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002335 return ret;
Chris Wilson8dc17752010-07-23 23:18:51 +01002336 /* Continue on if we fail due to EIO, the GPU is hung so we
2337 * should be safe and we need to cleanup or else we might
2338 * cause memory corruption through use-after-free.
2339 */
Chris Wilsona8198ee2011-04-13 22:04:09 +01002340
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01002341 i915_gem_object_finish_gtt(obj);
Chris Wilsona8198ee2011-04-13 22:04:09 +01002342
Daniel Vetter96b47b62009-12-15 17:50:00 +01002343 /* release the fence reg _after_ flushing */
Chris Wilsond9e86c02010-11-10 16:40:20 +00002344 ret = i915_gem_object_put_fence(obj);
Chris Wilson1488fc02012-04-24 15:47:31 +01002345 if (ret)
Chris Wilsond9e86c02010-11-10 16:40:20 +00002346 return ret;
Daniel Vetter96b47b62009-12-15 17:50:00 +01002347
Chris Wilsondb53a302011-02-03 11:57:46 +00002348 trace_i915_gem_object_unbind(obj);
2349
Daniel Vetter74898d72012-02-15 23:50:22 +01002350 if (obj->has_global_gtt_mapping)
2351 i915_gem_gtt_unbind_object(obj);
Daniel Vetter7bddb012012-02-09 17:15:47 +01002352 if (obj->has_aliasing_ppgtt_mapping) {
2353 i915_ppgtt_unbind_object(dev_priv->mm.aliasing_ppgtt, obj);
2354 obj->has_aliasing_ppgtt_mapping = 0;
2355 }
Daniel Vetter74163902012-02-15 23:50:21 +01002356 i915_gem_gtt_finish_object(obj);
Daniel Vetter7bddb012012-02-09 17:15:47 +01002357
Chris Wilson6c085a72012-08-20 11:40:46 +02002358 list_del(&obj->mm_list);
2359 list_move_tail(&obj->gtt_list, &dev_priv->mm.unbound_list);
Daniel Vetter75e9e912010-11-04 17:11:09 +01002360 /* Avoid an unnecessary call to unbind on rebind. */
Chris Wilson05394f32010-11-08 19:18:58 +00002361 obj->map_and_fenceable = true;
Eric Anholt673a3942008-07-30 12:06:12 -07002362
Chris Wilson05394f32010-11-08 19:18:58 +00002363 drm_mm_put_block(obj->gtt_space);
2364 obj->gtt_space = NULL;
2365 obj->gtt_offset = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002366
Chris Wilson6c085a72012-08-20 11:40:46 +02002367 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002368}
2369
Ben Widawskyb2da9fe2012-04-26 16:02:58 -07002370static int i915_ring_idle(struct intel_ring_buffer *ring)
Chris Wilsona56ba562010-09-28 10:07:56 +01002371{
Chris Wilson69c2fc82012-07-20 12:41:03 +01002372 if (list_empty(&ring->active_list))
Chris Wilson64193402010-10-24 12:38:05 +01002373 return 0;
2374
Ben Widawsky199b2bc2012-05-24 15:03:11 -07002375 return i915_wait_seqno(ring, i915_gem_next_request_seqno(ring));
Chris Wilsona56ba562010-09-28 10:07:56 +01002376}
2377
Ben Widawskyb2da9fe2012-04-26 16:02:58 -07002378int i915_gpu_idle(struct drm_device *dev)
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002379{
2380 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonb4519512012-05-11 14:29:30 +01002381 struct intel_ring_buffer *ring;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002382 int ret, i;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002383
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002384 /* Flush everything onto the inactive list. */
Chris Wilsonb4519512012-05-11 14:29:30 +01002385 for_each_ring(ring, dev_priv, i) {
2386 ret = i915_ring_idle(ring);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002387 if (ret)
2388 return ret;
Chris Wilsonb4519512012-05-11 14:29:30 +01002389
Ben Widawskyf2ef6eb2012-06-04 14:42:53 -07002390 ret = i915_switch_context(ring, NULL, DEFAULT_CONTEXT_ID);
2391 if (ret)
2392 return ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002393 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08002394
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002395 return 0;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002396}
2397
Chris Wilson9ce079e2012-04-17 15:31:30 +01002398static void sandybridge_write_fence_reg(struct drm_device *dev, int reg,
2399 struct drm_i915_gem_object *obj)
Eric Anholt4e901fd2009-10-26 16:44:17 -07002400{
Eric Anholt4e901fd2009-10-26 16:44:17 -07002401 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt4e901fd2009-10-26 16:44:17 -07002402 uint64_t val;
2403
Chris Wilson9ce079e2012-04-17 15:31:30 +01002404 if (obj) {
2405 u32 size = obj->gtt_space->size;
Eric Anholt4e901fd2009-10-26 16:44:17 -07002406
Chris Wilson9ce079e2012-04-17 15:31:30 +01002407 val = (uint64_t)((obj->gtt_offset + size - 4096) &
2408 0xfffff000) << 32;
2409 val |= obj->gtt_offset & 0xfffff000;
2410 val |= (uint64_t)((obj->stride / 128) - 1) <<
2411 SANDYBRIDGE_FENCE_PITCH_SHIFT;
Eric Anholt4e901fd2009-10-26 16:44:17 -07002412
Chris Wilson9ce079e2012-04-17 15:31:30 +01002413 if (obj->tiling_mode == I915_TILING_Y)
2414 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2415 val |= I965_FENCE_REG_VALID;
2416 } else
2417 val = 0;
Daniel Vetterc6642782010-11-12 13:46:18 +00002418
Chris Wilson9ce079e2012-04-17 15:31:30 +01002419 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + reg * 8, val);
2420 POSTING_READ(FENCE_REG_SANDYBRIDGE_0 + reg * 8);
Eric Anholt4e901fd2009-10-26 16:44:17 -07002421}
2422
Chris Wilson9ce079e2012-04-17 15:31:30 +01002423static void i965_write_fence_reg(struct drm_device *dev, int reg,
2424 struct drm_i915_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002425{
Jesse Barnesde151cf2008-11-12 10:03:55 -08002426 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002427 uint64_t val;
2428
Chris Wilson9ce079e2012-04-17 15:31:30 +01002429 if (obj) {
2430 u32 size = obj->gtt_space->size;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002431
Chris Wilson9ce079e2012-04-17 15:31:30 +01002432 val = (uint64_t)((obj->gtt_offset + size - 4096) &
2433 0xfffff000) << 32;
2434 val |= obj->gtt_offset & 0xfffff000;
2435 val |= ((obj->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
2436 if (obj->tiling_mode == I915_TILING_Y)
2437 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2438 val |= I965_FENCE_REG_VALID;
2439 } else
2440 val = 0;
Daniel Vetterc6642782010-11-12 13:46:18 +00002441
Chris Wilson9ce079e2012-04-17 15:31:30 +01002442 I915_WRITE64(FENCE_REG_965_0 + reg * 8, val);
2443 POSTING_READ(FENCE_REG_965_0 + reg * 8);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002444}
2445
Chris Wilson9ce079e2012-04-17 15:31:30 +01002446static void i915_write_fence_reg(struct drm_device *dev, int reg,
2447 struct drm_i915_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002448{
Jesse Barnesde151cf2008-11-12 10:03:55 -08002449 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson9ce079e2012-04-17 15:31:30 +01002450 u32 val;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002451
Chris Wilson9ce079e2012-04-17 15:31:30 +01002452 if (obj) {
2453 u32 size = obj->gtt_space->size;
2454 int pitch_val;
2455 int tile_width;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002456
Chris Wilson9ce079e2012-04-17 15:31:30 +01002457 WARN((obj->gtt_offset & ~I915_FENCE_START_MASK) ||
2458 (size & -size) != size ||
2459 (obj->gtt_offset & (size - 1)),
2460 "object 0x%08x [fenceable? %d] not 1M or pot-size (0x%08x) aligned\n",
2461 obj->gtt_offset, obj->map_and_fenceable, size);
2462
2463 if (obj->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev))
2464 tile_width = 128;
2465 else
2466 tile_width = 512;
2467
2468 /* Note: pitch better be a power of two tile widths */
2469 pitch_val = obj->stride / tile_width;
2470 pitch_val = ffs(pitch_val) - 1;
2471
2472 val = obj->gtt_offset;
2473 if (obj->tiling_mode == I915_TILING_Y)
2474 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2475 val |= I915_FENCE_SIZE_BITS(size);
2476 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2477 val |= I830_FENCE_REG_VALID;
2478 } else
2479 val = 0;
2480
2481 if (reg < 8)
2482 reg = FENCE_REG_830_0 + reg * 4;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002483 else
Chris Wilson9ce079e2012-04-17 15:31:30 +01002484 reg = FENCE_REG_945_8 + (reg - 8) * 4;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002485
Chris Wilson9ce079e2012-04-17 15:31:30 +01002486 I915_WRITE(reg, val);
2487 POSTING_READ(reg);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002488}
2489
Chris Wilson9ce079e2012-04-17 15:31:30 +01002490static void i830_write_fence_reg(struct drm_device *dev, int reg,
2491 struct drm_i915_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002492{
Jesse Barnesde151cf2008-11-12 10:03:55 -08002493 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002494 uint32_t val;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002495
Chris Wilson9ce079e2012-04-17 15:31:30 +01002496 if (obj) {
2497 u32 size = obj->gtt_space->size;
2498 uint32_t pitch_val;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002499
Chris Wilson9ce079e2012-04-17 15:31:30 +01002500 WARN((obj->gtt_offset & ~I830_FENCE_START_MASK) ||
2501 (size & -size) != size ||
2502 (obj->gtt_offset & (size - 1)),
2503 "object 0x%08x not 512K or pot-size 0x%08x aligned\n",
2504 obj->gtt_offset, size);
Eric Anholte76a16d2009-05-26 17:44:56 -07002505
Chris Wilson9ce079e2012-04-17 15:31:30 +01002506 pitch_val = obj->stride / 128;
2507 pitch_val = ffs(pitch_val) - 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002508
Chris Wilson9ce079e2012-04-17 15:31:30 +01002509 val = obj->gtt_offset;
2510 if (obj->tiling_mode == I915_TILING_Y)
2511 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2512 val |= I830_FENCE_SIZE_BITS(size);
2513 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2514 val |= I830_FENCE_REG_VALID;
2515 } else
2516 val = 0;
Daniel Vetterc6642782010-11-12 13:46:18 +00002517
Chris Wilson9ce079e2012-04-17 15:31:30 +01002518 I915_WRITE(FENCE_REG_830_0 + reg * 4, val);
2519 POSTING_READ(FENCE_REG_830_0 + reg * 4);
2520}
2521
2522static void i915_gem_write_fence(struct drm_device *dev, int reg,
2523 struct drm_i915_gem_object *obj)
2524{
2525 switch (INTEL_INFO(dev)->gen) {
2526 case 7:
2527 case 6: sandybridge_write_fence_reg(dev, reg, obj); break;
2528 case 5:
2529 case 4: i965_write_fence_reg(dev, reg, obj); break;
2530 case 3: i915_write_fence_reg(dev, reg, obj); break;
2531 case 2: i830_write_fence_reg(dev, reg, obj); break;
2532 default: break;
2533 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002534}
2535
Chris Wilson61050802012-04-17 15:31:31 +01002536static inline int fence_number(struct drm_i915_private *dev_priv,
2537 struct drm_i915_fence_reg *fence)
2538{
2539 return fence - dev_priv->fence_regs;
2540}
2541
2542static void i915_gem_object_update_fence(struct drm_i915_gem_object *obj,
2543 struct drm_i915_fence_reg *fence,
2544 bool enable)
2545{
2546 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2547 int reg = fence_number(dev_priv, fence);
2548
2549 i915_gem_write_fence(obj->base.dev, reg, enable ? obj : NULL);
2550
2551 if (enable) {
2552 obj->fence_reg = reg;
2553 fence->obj = obj;
2554 list_move_tail(&fence->lru_list, &dev_priv->mm.fence_list);
2555 } else {
2556 obj->fence_reg = I915_FENCE_REG_NONE;
2557 fence->obj = NULL;
2558 list_del_init(&fence->lru_list);
2559 }
2560}
2561
Chris Wilsond9e86c02010-11-10 16:40:20 +00002562static int
Chris Wilsona360bb12012-04-17 15:31:25 +01002563i915_gem_object_flush_fence(struct drm_i915_gem_object *obj)
Chris Wilsond9e86c02010-11-10 16:40:20 +00002564{
Chris Wilson1c293ea2012-04-17 15:31:27 +01002565 if (obj->last_fenced_seqno) {
Chris Wilson86d5bc32012-07-20 12:41:04 +01002566 int ret = i915_wait_seqno(obj->ring, obj->last_fenced_seqno);
Chris Wilson18991842012-04-17 15:31:29 +01002567 if (ret)
2568 return ret;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002569
2570 obj->last_fenced_seqno = 0;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002571 }
2572
Chris Wilson63256ec2011-01-04 18:42:07 +00002573 /* Ensure that all CPU reads are completed before installing a fence
2574 * and all writes before removing the fence.
2575 */
2576 if (obj->base.read_domains & I915_GEM_DOMAIN_GTT)
2577 mb();
2578
Chris Wilson86d5bc32012-07-20 12:41:04 +01002579 obj->fenced_gpu_access = false;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002580 return 0;
2581}
2582
2583int
2584i915_gem_object_put_fence(struct drm_i915_gem_object *obj)
2585{
Chris Wilson61050802012-04-17 15:31:31 +01002586 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002587 int ret;
2588
Chris Wilsona360bb12012-04-17 15:31:25 +01002589 ret = i915_gem_object_flush_fence(obj);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002590 if (ret)
2591 return ret;
2592
Chris Wilson61050802012-04-17 15:31:31 +01002593 if (obj->fence_reg == I915_FENCE_REG_NONE)
2594 return 0;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002595
Chris Wilson61050802012-04-17 15:31:31 +01002596 i915_gem_object_update_fence(obj,
2597 &dev_priv->fence_regs[obj->fence_reg],
2598 false);
2599 i915_gem_object_fence_lost(obj);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002600
2601 return 0;
2602}
2603
2604static struct drm_i915_fence_reg *
Chris Wilsona360bb12012-04-17 15:31:25 +01002605i915_find_fence_reg(struct drm_device *dev)
Daniel Vetterae3db242010-02-19 11:51:58 +01002606{
Daniel Vetterae3db242010-02-19 11:51:58 +01002607 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson8fe301a2012-04-17 15:31:28 +01002608 struct drm_i915_fence_reg *reg, *avail;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002609 int i;
Daniel Vetterae3db242010-02-19 11:51:58 +01002610
2611 /* First try to find a free reg */
Chris Wilsond9e86c02010-11-10 16:40:20 +00002612 avail = NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002613 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2614 reg = &dev_priv->fence_regs[i];
2615 if (!reg->obj)
Chris Wilsond9e86c02010-11-10 16:40:20 +00002616 return reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01002617
Chris Wilson1690e1e2011-12-14 13:57:08 +01002618 if (!reg->pin_count)
Chris Wilsond9e86c02010-11-10 16:40:20 +00002619 avail = reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01002620 }
2621
Chris Wilsond9e86c02010-11-10 16:40:20 +00002622 if (avail == NULL)
2623 return NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002624
2625 /* None available, try to steal one or wait for a user to finish */
Chris Wilsond9e86c02010-11-10 16:40:20 +00002626 list_for_each_entry(reg, &dev_priv->mm.fence_list, lru_list) {
Chris Wilson1690e1e2011-12-14 13:57:08 +01002627 if (reg->pin_count)
Daniel Vetterae3db242010-02-19 11:51:58 +01002628 continue;
2629
Chris Wilson8fe301a2012-04-17 15:31:28 +01002630 return reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01002631 }
2632
Chris Wilson8fe301a2012-04-17 15:31:28 +01002633 return NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002634}
2635
Jesse Barnesde151cf2008-11-12 10:03:55 -08002636/**
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002637 * i915_gem_object_get_fence - set up fencing for an object
Jesse Barnesde151cf2008-11-12 10:03:55 -08002638 * @obj: object to map through a fence reg
2639 *
2640 * When mapping objects through the GTT, userspace wants to be able to write
2641 * to them without having to worry about swizzling if the object is tiled.
Jesse Barnesde151cf2008-11-12 10:03:55 -08002642 * This function walks the fence regs looking for a free one for @obj,
2643 * stealing one if it can't find any.
2644 *
2645 * It then sets up the reg based on the object's properties: address, pitch
2646 * and tiling format.
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002647 *
2648 * For an untiled surface, this removes any existing fence.
Jesse Barnesde151cf2008-11-12 10:03:55 -08002649 */
Chris Wilson8c4b8c32009-06-17 22:08:52 +01002650int
Chris Wilson06d98132012-04-17 15:31:24 +01002651i915_gem_object_get_fence(struct drm_i915_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002652{
Chris Wilson05394f32010-11-08 19:18:58 +00002653 struct drm_device *dev = obj->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002654 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson14415742012-04-17 15:31:33 +01002655 bool enable = obj->tiling_mode != I915_TILING_NONE;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002656 struct drm_i915_fence_reg *reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01002657 int ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002658
Chris Wilson14415742012-04-17 15:31:33 +01002659 /* Have we updated the tiling parameters upon the object and so
2660 * will need to serialise the write to the associated fence register?
2661 */
Chris Wilson5d82e3e2012-04-21 16:23:23 +01002662 if (obj->fence_dirty) {
Chris Wilson14415742012-04-17 15:31:33 +01002663 ret = i915_gem_object_flush_fence(obj);
2664 if (ret)
2665 return ret;
2666 }
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002667
Chris Wilsond9e86c02010-11-10 16:40:20 +00002668 /* Just update our place in the LRU if our fence is getting reused. */
Chris Wilson05394f32010-11-08 19:18:58 +00002669 if (obj->fence_reg != I915_FENCE_REG_NONE) {
2670 reg = &dev_priv->fence_regs[obj->fence_reg];
Chris Wilson5d82e3e2012-04-21 16:23:23 +01002671 if (!obj->fence_dirty) {
Chris Wilson14415742012-04-17 15:31:33 +01002672 list_move_tail(&reg->lru_list,
2673 &dev_priv->mm.fence_list);
2674 return 0;
2675 }
2676 } else if (enable) {
2677 reg = i915_find_fence_reg(dev);
2678 if (reg == NULL)
2679 return -EDEADLK;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002680
Chris Wilson14415742012-04-17 15:31:33 +01002681 if (reg->obj) {
2682 struct drm_i915_gem_object *old = reg->obj;
2683
2684 ret = i915_gem_object_flush_fence(old);
Chris Wilson29c5a582011-03-17 15:23:22 +00002685 if (ret)
2686 return ret;
2687
Chris Wilson14415742012-04-17 15:31:33 +01002688 i915_gem_object_fence_lost(old);
Chris Wilson29c5a582011-03-17 15:23:22 +00002689 }
Chris Wilson14415742012-04-17 15:31:33 +01002690 } else
Eric Anholta09ba7f2009-08-29 12:49:51 -07002691 return 0;
Eric Anholta09ba7f2009-08-29 12:49:51 -07002692
Chris Wilson14415742012-04-17 15:31:33 +01002693 i915_gem_object_update_fence(obj, reg, enable);
Chris Wilson5d82e3e2012-04-21 16:23:23 +01002694 obj->fence_dirty = false;
Chris Wilson14415742012-04-17 15:31:33 +01002695
Chris Wilson9ce079e2012-04-17 15:31:30 +01002696 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002697}
2698
Chris Wilson42d6ab42012-07-26 11:49:32 +01002699static bool i915_gem_valid_gtt_space(struct drm_device *dev,
2700 struct drm_mm_node *gtt_space,
2701 unsigned long cache_level)
2702{
2703 struct drm_mm_node *other;
2704
2705 /* On non-LLC machines we have to be careful when putting differing
2706 * types of snoopable memory together to avoid the prefetcher
2707 * crossing memory domains and dieing.
2708 */
2709 if (HAS_LLC(dev))
2710 return true;
2711
2712 if (gtt_space == NULL)
2713 return true;
2714
2715 if (list_empty(&gtt_space->node_list))
2716 return true;
2717
2718 other = list_entry(gtt_space->node_list.prev, struct drm_mm_node, node_list);
2719 if (other->allocated && !other->hole_follows && other->color != cache_level)
2720 return false;
2721
2722 other = list_entry(gtt_space->node_list.next, struct drm_mm_node, node_list);
2723 if (other->allocated && !gtt_space->hole_follows && other->color != cache_level)
2724 return false;
2725
2726 return true;
2727}
2728
2729static void i915_gem_verify_gtt(struct drm_device *dev)
2730{
2731#if WATCH_GTT
2732 struct drm_i915_private *dev_priv = dev->dev_private;
2733 struct drm_i915_gem_object *obj;
2734 int err = 0;
2735
2736 list_for_each_entry(obj, &dev_priv->mm.gtt_list, gtt_list) {
2737 if (obj->gtt_space == NULL) {
2738 printk(KERN_ERR "object found on GTT list with no space reserved\n");
2739 err++;
2740 continue;
2741 }
2742
2743 if (obj->cache_level != obj->gtt_space->color) {
2744 printk(KERN_ERR "object reserved space [%08lx, %08lx] with wrong color, cache_level=%x, color=%lx\n",
2745 obj->gtt_space->start,
2746 obj->gtt_space->start + obj->gtt_space->size,
2747 obj->cache_level,
2748 obj->gtt_space->color);
2749 err++;
2750 continue;
2751 }
2752
2753 if (!i915_gem_valid_gtt_space(dev,
2754 obj->gtt_space,
2755 obj->cache_level)) {
2756 printk(KERN_ERR "invalid GTT space found at [%08lx, %08lx] - color=%x\n",
2757 obj->gtt_space->start,
2758 obj->gtt_space->start + obj->gtt_space->size,
2759 obj->cache_level);
2760 err++;
2761 continue;
2762 }
2763 }
2764
2765 WARN_ON(err);
2766#endif
2767}
2768
Jesse Barnesde151cf2008-11-12 10:03:55 -08002769/**
Eric Anholt673a3942008-07-30 12:06:12 -07002770 * Finds free space in the GTT aperture and binds the object there.
2771 */
2772static int
Chris Wilson05394f32010-11-08 19:18:58 +00002773i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
Daniel Vetter920afa72010-09-16 17:54:23 +02002774 unsigned alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01002775 bool map_and_fenceable)
Eric Anholt673a3942008-07-30 12:06:12 -07002776{
Chris Wilson05394f32010-11-08 19:18:58 +00002777 struct drm_device *dev = obj->base.dev;
Eric Anholt673a3942008-07-30 12:06:12 -07002778 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07002779 struct drm_mm_node *free_space;
Daniel Vetter5e783302010-11-14 22:32:36 +01002780 u32 size, fence_size, fence_alignment, unfenced_alignment;
Daniel Vetter75e9e912010-11-04 17:11:09 +01002781 bool mappable, fenceable;
Chris Wilson07f73f62009-09-14 16:50:30 +01002782 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002783
Chris Wilson05394f32010-11-08 19:18:58 +00002784 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01002785 DRM_ERROR("Attempting to bind a purgeable object\n");
2786 return -EINVAL;
2787 }
2788
Chris Wilsone28f8712011-07-18 13:11:49 -07002789 fence_size = i915_gem_get_gtt_size(dev,
2790 obj->base.size,
2791 obj->tiling_mode);
2792 fence_alignment = i915_gem_get_gtt_alignment(dev,
2793 obj->base.size,
2794 obj->tiling_mode);
2795 unfenced_alignment =
2796 i915_gem_get_unfenced_gtt_alignment(dev,
2797 obj->base.size,
2798 obj->tiling_mode);
Chris Wilsona00b10c2010-09-24 21:15:47 +01002799
Eric Anholt673a3942008-07-30 12:06:12 -07002800 if (alignment == 0)
Daniel Vetter5e783302010-11-14 22:32:36 +01002801 alignment = map_and_fenceable ? fence_alignment :
2802 unfenced_alignment;
Daniel Vetter75e9e912010-11-04 17:11:09 +01002803 if (map_and_fenceable && alignment & (fence_alignment - 1)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002804 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2805 return -EINVAL;
2806 }
2807
Chris Wilson05394f32010-11-08 19:18:58 +00002808 size = map_and_fenceable ? fence_size : obj->base.size;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002809
Chris Wilson654fc602010-05-27 13:18:21 +01002810 /* If the object is bigger than the entire aperture, reject it early
2811 * before evicting everything in a vain attempt to find space.
2812 */
Chris Wilson05394f32010-11-08 19:18:58 +00002813 if (obj->base.size >
Daniel Vetter75e9e912010-11-04 17:11:09 +01002814 (map_and_fenceable ? dev_priv->mm.gtt_mappable_end : dev_priv->mm.gtt_total)) {
Chris Wilson654fc602010-05-27 13:18:21 +01002815 DRM_ERROR("Attempting to bind an object larger than the aperture\n");
2816 return -E2BIG;
2817 }
2818
Chris Wilson6c085a72012-08-20 11:40:46 +02002819 ret = i915_gem_object_get_pages_gtt(obj);
2820 if (ret)
2821 return ret;
2822
Eric Anholt673a3942008-07-30 12:06:12 -07002823 search_free:
Daniel Vetter75e9e912010-11-04 17:11:09 +01002824 if (map_and_fenceable)
Daniel Vetter920afa72010-09-16 17:54:23 +02002825 free_space =
Chris Wilson42d6ab42012-07-26 11:49:32 +01002826 drm_mm_search_free_in_range_color(&dev_priv->mm.gtt_space,
2827 size, alignment, obj->cache_level,
2828 0, dev_priv->mm.gtt_mappable_end,
2829 false);
Daniel Vetter920afa72010-09-16 17:54:23 +02002830 else
Chris Wilson42d6ab42012-07-26 11:49:32 +01002831 free_space = drm_mm_search_free_color(&dev_priv->mm.gtt_space,
2832 size, alignment, obj->cache_level,
2833 false);
Daniel Vetter920afa72010-09-16 17:54:23 +02002834
2835 if (free_space != NULL) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01002836 if (map_and_fenceable)
Chris Wilson05394f32010-11-08 19:18:58 +00002837 obj->gtt_space =
Daniel Vetter920afa72010-09-16 17:54:23 +02002838 drm_mm_get_block_range_generic(free_space,
Chris Wilson42d6ab42012-07-26 11:49:32 +01002839 size, alignment, obj->cache_level,
Chris Wilson6b9d89b2012-07-10 11:15:23 +01002840 0, dev_priv->mm.gtt_mappable_end,
Chris Wilson42d6ab42012-07-26 11:49:32 +01002841 false);
Daniel Vetter920afa72010-09-16 17:54:23 +02002842 else
Chris Wilson05394f32010-11-08 19:18:58 +00002843 obj->gtt_space =
Chris Wilson42d6ab42012-07-26 11:49:32 +01002844 drm_mm_get_block_generic(free_space,
2845 size, alignment, obj->cache_level,
2846 false);
Daniel Vetter920afa72010-09-16 17:54:23 +02002847 }
Chris Wilson05394f32010-11-08 19:18:58 +00002848 if (obj->gtt_space == NULL) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01002849 ret = i915_gem_evict_something(dev, size, alignment,
Chris Wilson42d6ab42012-07-26 11:49:32 +01002850 obj->cache_level,
Daniel Vetter75e9e912010-11-04 17:11:09 +01002851 map_and_fenceable);
Chris Wilson97311292009-09-21 00:22:34 +01002852 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002853 return ret;
Chris Wilson97311292009-09-21 00:22:34 +01002854
Eric Anholt673a3942008-07-30 12:06:12 -07002855 goto search_free;
2856 }
Chris Wilson42d6ab42012-07-26 11:49:32 +01002857 if (WARN_ON(!i915_gem_valid_gtt_space(dev,
2858 obj->gtt_space,
2859 obj->cache_level))) {
2860 drm_mm_put_block(obj->gtt_space);
2861 obj->gtt_space = NULL;
2862 return -EINVAL;
2863 }
Eric Anholt673a3942008-07-30 12:06:12 -07002864
Eric Anholt673a3942008-07-30 12:06:12 -07002865
Daniel Vetter74163902012-02-15 23:50:21 +01002866 ret = i915_gem_gtt_prepare_object(obj);
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002867 if (ret) {
Chris Wilson05394f32010-11-08 19:18:58 +00002868 drm_mm_put_block(obj->gtt_space);
2869 obj->gtt_space = NULL;
Chris Wilson6c085a72012-08-20 11:40:46 +02002870 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002871 }
Eric Anholt673a3942008-07-30 12:06:12 -07002872
Daniel Vetter0ebb9822012-02-15 23:50:24 +01002873 if (!dev_priv->mm.aliasing_ppgtt)
2874 i915_gem_gtt_bind_object(obj, obj->cache_level);
Eric Anholt673a3942008-07-30 12:06:12 -07002875
Chris Wilson6c085a72012-08-20 11:40:46 +02002876 list_move_tail(&obj->gtt_list, &dev_priv->mm.bound_list);
Chris Wilson05394f32010-11-08 19:18:58 +00002877 list_add_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
Chris Wilsonbf1a1092010-08-07 11:01:20 +01002878
Chris Wilson6299f992010-11-24 12:23:44 +00002879 obj->gtt_offset = obj->gtt_space->start;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002880
Daniel Vetter75e9e912010-11-04 17:11:09 +01002881 fenceable =
Chris Wilson05394f32010-11-08 19:18:58 +00002882 obj->gtt_space->size == fence_size &&
Akshay Joshi0206e352011-08-16 15:34:10 -04002883 (obj->gtt_space->start & (fence_alignment - 1)) == 0;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002884
Daniel Vetter75e9e912010-11-04 17:11:09 +01002885 mappable =
Chris Wilson05394f32010-11-08 19:18:58 +00002886 obj->gtt_offset + obj->base.size <= dev_priv->mm.gtt_mappable_end;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002887
Chris Wilson05394f32010-11-08 19:18:58 +00002888 obj->map_and_fenceable = mappable && fenceable;
Daniel Vetter75e9e912010-11-04 17:11:09 +01002889
Chris Wilsondb53a302011-02-03 11:57:46 +00002890 trace_i915_gem_object_bind(obj, map_and_fenceable);
Chris Wilson42d6ab42012-07-26 11:49:32 +01002891 i915_gem_verify_gtt(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07002892 return 0;
2893}
2894
2895void
Chris Wilson05394f32010-11-08 19:18:58 +00002896i915_gem_clflush_object(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002897{
Eric Anholt673a3942008-07-30 12:06:12 -07002898 /* If we don't have a page list set up, then we're not pinned
2899 * to GPU, and we can ignore the cache flush because it'll happen
2900 * again at bind time.
2901 */
Chris Wilson05394f32010-11-08 19:18:58 +00002902 if (obj->pages == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002903 return;
2904
Chris Wilson9c23f7f2011-03-29 16:59:52 -07002905 /* If the GPU is snooping the contents of the CPU cache,
2906 * we do not need to manually clear the CPU cache lines. However,
2907 * the caches are only snooped when the render cache is
2908 * flushed/invalidated. As we always have to emit invalidations
2909 * and flushes when moving into and out of the RENDER domain, correct
2910 * snooping behaviour occurs naturally as the result of our domain
2911 * tracking.
2912 */
2913 if (obj->cache_level != I915_CACHE_NONE)
2914 return;
2915
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002916 trace_i915_gem_object_clflush(obj);
Eric Anholtcfa16a02009-05-26 18:46:16 -07002917
Chris Wilson05394f32010-11-08 19:18:58 +00002918 drm_clflush_pages(obj->pages, obj->base.size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002919}
2920
Eric Anholte47c68e2008-11-14 13:35:19 -08002921/** Flushes the GTT write domain for the object if it's dirty. */
2922static void
Chris Wilson05394f32010-11-08 19:18:58 +00002923i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08002924{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002925 uint32_t old_write_domain;
2926
Chris Wilson05394f32010-11-08 19:18:58 +00002927 if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
Eric Anholte47c68e2008-11-14 13:35:19 -08002928 return;
2929
Chris Wilson63256ec2011-01-04 18:42:07 +00002930 /* No actual flushing is required for the GTT write domain. Writes
Eric Anholte47c68e2008-11-14 13:35:19 -08002931 * to it immediately go to main memory as far as we know, so there's
2932 * no chipset flush. It also doesn't land in render cache.
Chris Wilson63256ec2011-01-04 18:42:07 +00002933 *
2934 * However, we do have to enforce the order so that all writes through
2935 * the GTT land before any writes to the device, such as updates to
2936 * the GATT itself.
Eric Anholte47c68e2008-11-14 13:35:19 -08002937 */
Chris Wilson63256ec2011-01-04 18:42:07 +00002938 wmb();
2939
Chris Wilson05394f32010-11-08 19:18:58 +00002940 old_write_domain = obj->base.write_domain;
2941 obj->base.write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002942
2943 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00002944 obj->base.read_domains,
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002945 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002946}
2947
2948/** Flushes the CPU write domain for the object if it's dirty. */
2949static void
Chris Wilson05394f32010-11-08 19:18:58 +00002950i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08002951{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002952 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002953
Chris Wilson05394f32010-11-08 19:18:58 +00002954 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
Eric Anholte47c68e2008-11-14 13:35:19 -08002955 return;
2956
2957 i915_gem_clflush_object(obj);
Daniel Vetter40ce6572010-11-05 18:12:18 +01002958 intel_gtt_chipset_flush();
Chris Wilson05394f32010-11-08 19:18:58 +00002959 old_write_domain = obj->base.write_domain;
2960 obj->base.write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002961
2962 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00002963 obj->base.read_domains,
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002964 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002965}
2966
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002967/**
2968 * Moves a single object to the GTT read, and possibly write domain.
2969 *
2970 * This function returns when the move is complete, including waiting on
2971 * flushes to occur.
2972 */
Jesse Barnes79e53942008-11-07 14:24:08 -08002973int
Chris Wilson20217462010-11-23 15:26:33 +00002974i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002975{
Chris Wilson8325a092012-04-24 15:52:35 +01002976 drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002977 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002978 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002979
Eric Anholt02354392008-11-26 13:58:13 -08002980 /* Not valid to be called on unbound objects. */
Chris Wilson05394f32010-11-08 19:18:58 +00002981 if (obj->gtt_space == NULL)
Eric Anholt02354392008-11-26 13:58:13 -08002982 return -EINVAL;
2983
Chris Wilson8d7e3de2011-02-07 15:23:02 +00002984 if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
2985 return 0;
2986
Chris Wilson0201f1e2012-07-20 12:41:01 +01002987 ret = i915_gem_object_wait_rendering(obj, !write);
2988 if (ret)
2989 return ret;
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002990
Chris Wilson72133422010-09-13 23:56:38 +01002991 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002992
Chris Wilson05394f32010-11-08 19:18:58 +00002993 old_write_domain = obj->base.write_domain;
2994 old_read_domains = obj->base.read_domains;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002995
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002996 /* It should now be out of any other write domains, and we can update
2997 * the domain values for our changes.
2998 */
Chris Wilson05394f32010-11-08 19:18:58 +00002999 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
3000 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08003001 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00003002 obj->base.read_domains = I915_GEM_DOMAIN_GTT;
3003 obj->base.write_domain = I915_GEM_DOMAIN_GTT;
3004 obj->dirty = 1;
Eric Anholte47c68e2008-11-14 13:35:19 -08003005 }
3006
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003007 trace_i915_gem_object_change_domain(obj,
3008 old_read_domains,
3009 old_write_domain);
3010
Chris Wilson8325a092012-04-24 15:52:35 +01003011 /* And bump the LRU for this access */
3012 if (i915_gem_object_is_inactive(obj))
3013 list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
3014
Eric Anholte47c68e2008-11-14 13:35:19 -08003015 return 0;
3016}
3017
Chris Wilsone4ffd172011-04-04 09:44:39 +01003018int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
3019 enum i915_cache_level cache_level)
3020{
Daniel Vetter7bddb012012-02-09 17:15:47 +01003021 struct drm_device *dev = obj->base.dev;
3022 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003023 int ret;
3024
3025 if (obj->cache_level == cache_level)
3026 return 0;
3027
3028 if (obj->pin_count) {
3029 DRM_DEBUG("can not change the cache level of pinned objects\n");
3030 return -EBUSY;
3031 }
3032
Chris Wilson42d6ab42012-07-26 11:49:32 +01003033 if (!i915_gem_valid_gtt_space(dev, obj->gtt_space, cache_level)) {
3034 ret = i915_gem_object_unbind(obj);
3035 if (ret)
3036 return ret;
3037 }
3038
Chris Wilsone4ffd172011-04-04 09:44:39 +01003039 if (obj->gtt_space) {
3040 ret = i915_gem_object_finish_gpu(obj);
3041 if (ret)
3042 return ret;
3043
3044 i915_gem_object_finish_gtt(obj);
3045
3046 /* Before SandyBridge, you could not use tiling or fence
3047 * registers with snooped memory, so relinquish any fences
3048 * currently pointing to our region in the aperture.
3049 */
Chris Wilson42d6ab42012-07-26 11:49:32 +01003050 if (INTEL_INFO(dev)->gen < 6) {
Chris Wilsone4ffd172011-04-04 09:44:39 +01003051 ret = i915_gem_object_put_fence(obj);
3052 if (ret)
3053 return ret;
3054 }
3055
Daniel Vetter74898d72012-02-15 23:50:22 +01003056 if (obj->has_global_gtt_mapping)
3057 i915_gem_gtt_bind_object(obj, cache_level);
Daniel Vetter7bddb012012-02-09 17:15:47 +01003058 if (obj->has_aliasing_ppgtt_mapping)
3059 i915_ppgtt_bind_object(dev_priv->mm.aliasing_ppgtt,
3060 obj, cache_level);
Chris Wilson42d6ab42012-07-26 11:49:32 +01003061
3062 obj->gtt_space->color = cache_level;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003063 }
3064
3065 if (cache_level == I915_CACHE_NONE) {
3066 u32 old_read_domains, old_write_domain;
3067
3068 /* If we're coming from LLC cached, then we haven't
3069 * actually been tracking whether the data is in the
3070 * CPU cache or not, since we only allow one bit set
3071 * in obj->write_domain and have been skipping the clflushes.
3072 * Just set it to the CPU cache for now.
3073 */
3074 WARN_ON(obj->base.write_domain & ~I915_GEM_DOMAIN_CPU);
3075 WARN_ON(obj->base.read_domains & ~I915_GEM_DOMAIN_CPU);
3076
3077 old_read_domains = obj->base.read_domains;
3078 old_write_domain = obj->base.write_domain;
3079
3080 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3081 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
3082
3083 trace_i915_gem_object_change_domain(obj,
3084 old_read_domains,
3085 old_write_domain);
3086 }
3087
3088 obj->cache_level = cache_level;
Chris Wilson42d6ab42012-07-26 11:49:32 +01003089 i915_gem_verify_gtt(dev);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003090 return 0;
3091}
3092
Chris Wilsone6994ae2012-07-10 10:27:08 +01003093int i915_gem_get_cacheing_ioctl(struct drm_device *dev, void *data,
3094 struct drm_file *file)
3095{
3096 struct drm_i915_gem_cacheing *args = data;
3097 struct drm_i915_gem_object *obj;
3098 int ret;
3099
3100 ret = i915_mutex_lock_interruptible(dev);
3101 if (ret)
3102 return ret;
3103
3104 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
3105 if (&obj->base == NULL) {
3106 ret = -ENOENT;
3107 goto unlock;
3108 }
3109
3110 args->cacheing = obj->cache_level != I915_CACHE_NONE;
3111
3112 drm_gem_object_unreference(&obj->base);
3113unlock:
3114 mutex_unlock(&dev->struct_mutex);
3115 return ret;
3116}
3117
3118int i915_gem_set_cacheing_ioctl(struct drm_device *dev, void *data,
3119 struct drm_file *file)
3120{
3121 struct drm_i915_gem_cacheing *args = data;
3122 struct drm_i915_gem_object *obj;
3123 enum i915_cache_level level;
3124 int ret;
3125
3126 ret = i915_mutex_lock_interruptible(dev);
3127 if (ret)
3128 return ret;
3129
3130 switch (args->cacheing) {
3131 case I915_CACHEING_NONE:
3132 level = I915_CACHE_NONE;
3133 break;
3134 case I915_CACHEING_CACHED:
3135 level = I915_CACHE_LLC;
3136 break;
3137 default:
3138 return -EINVAL;
3139 }
3140
3141 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
3142 if (&obj->base == NULL) {
3143 ret = -ENOENT;
3144 goto unlock;
3145 }
3146
3147 ret = i915_gem_object_set_cache_level(obj, level);
3148
3149 drm_gem_object_unreference(&obj->base);
3150unlock:
3151 mutex_unlock(&dev->struct_mutex);
3152 return ret;
3153}
3154
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003155/*
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003156 * Prepare buffer for display plane (scanout, cursors, etc).
3157 * Can be called from an uninterruptible phase (modesetting) and allows
3158 * any flushes to be pipelined (for pageflips).
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003159 */
3160int
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003161i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
3162 u32 alignment,
Chris Wilson919926a2010-11-12 13:42:53 +00003163 struct intel_ring_buffer *pipelined)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003164{
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003165 u32 old_read_domains, old_write_domain;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003166 int ret;
3167
Chris Wilson0be73282010-12-06 14:36:27 +00003168 if (pipelined != obj->ring) {
Ben Widawsky2911a352012-04-05 14:47:36 -07003169 ret = i915_gem_object_sync(obj, pipelined);
3170 if (ret)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003171 return ret;
3172 }
3173
Eric Anholta7ef0642011-03-29 16:59:54 -07003174 /* The display engine is not coherent with the LLC cache on gen6. As
3175 * a result, we make sure that the pinning that is about to occur is
3176 * done with uncached PTEs. This is lowest common denominator for all
3177 * chipsets.
3178 *
3179 * However for gen6+, we could do better by using the GFDT bit instead
3180 * of uncaching, which would allow us to flush all the LLC-cached data
3181 * with that bit in the PTE to main memory with just one PIPE_CONTROL.
3182 */
3183 ret = i915_gem_object_set_cache_level(obj, I915_CACHE_NONE);
3184 if (ret)
3185 return ret;
3186
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003187 /* As the user may map the buffer once pinned in the display plane
3188 * (e.g. libkms for the bootup splash), we have to ensure that we
3189 * always use map_and_fenceable for all scanout buffers.
3190 */
3191 ret = i915_gem_object_pin(obj, alignment, true);
3192 if (ret)
3193 return ret;
3194
Chris Wilsonb118c1e2010-05-27 13:18:14 +01003195 i915_gem_object_flush_cpu_write_domain(obj);
3196
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003197 old_write_domain = obj->base.write_domain;
Chris Wilson05394f32010-11-08 19:18:58 +00003198 old_read_domains = obj->base.read_domains;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003199
3200 /* It should now be out of any other write domains, and we can update
3201 * the domain values for our changes.
3202 */
Chris Wilsone5f1d962012-07-20 12:41:00 +01003203 obj->base.write_domain = 0;
Chris Wilson05394f32010-11-08 19:18:58 +00003204 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003205
3206 trace_i915_gem_object_change_domain(obj,
3207 old_read_domains,
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003208 old_write_domain);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003209
3210 return 0;
3211}
3212
Chris Wilson85345512010-11-13 09:49:11 +00003213int
Chris Wilsona8198ee2011-04-13 22:04:09 +01003214i915_gem_object_finish_gpu(struct drm_i915_gem_object *obj)
Chris Wilson85345512010-11-13 09:49:11 +00003215{
Chris Wilson88241782011-01-07 17:09:48 +00003216 int ret;
3217
Chris Wilsona8198ee2011-04-13 22:04:09 +01003218 if ((obj->base.read_domains & I915_GEM_GPU_DOMAINS) == 0)
Chris Wilson85345512010-11-13 09:49:11 +00003219 return 0;
3220
Chris Wilson0201f1e2012-07-20 12:41:01 +01003221 ret = i915_gem_object_wait_rendering(obj, false);
Chris Wilsonc501ae72011-12-14 13:57:23 +01003222 if (ret)
3223 return ret;
3224
Chris Wilsona8198ee2011-04-13 22:04:09 +01003225 /* Ensure that we invalidate the GPU's caches and TLBs. */
3226 obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
Chris Wilsonc501ae72011-12-14 13:57:23 +01003227 return 0;
Chris Wilson85345512010-11-13 09:49:11 +00003228}
3229
Eric Anholte47c68e2008-11-14 13:35:19 -08003230/**
3231 * Moves a single object to the CPU read, and possibly write domain.
3232 *
3233 * This function returns when the move is complete, including waiting on
3234 * flushes to occur.
3235 */
Chris Wilsondabdfe02012-03-26 10:10:27 +02003236int
Chris Wilson919926a2010-11-12 13:42:53 +00003237i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholte47c68e2008-11-14 13:35:19 -08003238{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003239 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003240 int ret;
3241
Chris Wilson8d7e3de2011-02-07 15:23:02 +00003242 if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
3243 return 0;
3244
Chris Wilson0201f1e2012-07-20 12:41:01 +01003245 ret = i915_gem_object_wait_rendering(obj, !write);
3246 if (ret)
3247 return ret;
Eric Anholte47c68e2008-11-14 13:35:19 -08003248
3249 i915_gem_object_flush_gtt_write_domain(obj);
3250
Chris Wilson05394f32010-11-08 19:18:58 +00003251 old_write_domain = obj->base.write_domain;
3252 old_read_domains = obj->base.read_domains;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003253
Eric Anholte47c68e2008-11-14 13:35:19 -08003254 /* Flush the CPU cache if it's still invalid. */
Chris Wilson05394f32010-11-08 19:18:58 +00003255 if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
Eric Anholte47c68e2008-11-14 13:35:19 -08003256 i915_gem_clflush_object(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003257
Chris Wilson05394f32010-11-08 19:18:58 +00003258 obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003259 }
3260
3261 /* It should now be out of any other write domains, and we can update
3262 * the domain values for our changes.
3263 */
Chris Wilson05394f32010-11-08 19:18:58 +00003264 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
Eric Anholte47c68e2008-11-14 13:35:19 -08003265
3266 /* If we're writing through the CPU, then the GPU read domains will
3267 * need to be invalidated at next use.
3268 */
3269 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00003270 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3271 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003272 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003273
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003274 trace_i915_gem_object_change_domain(obj,
3275 old_read_domains,
3276 old_write_domain);
3277
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003278 return 0;
3279}
3280
Eric Anholt673a3942008-07-30 12:06:12 -07003281/* Throttle our rendering by waiting until the ring has completed our requests
3282 * emitted over 20 msec ago.
3283 *
Eric Anholtb9624422009-06-03 07:27:35 +00003284 * Note that if we were to use the current jiffies each time around the loop,
3285 * we wouldn't escape the function with any frames outstanding if the time to
3286 * render a frame was over 20ms.
3287 *
Eric Anholt673a3942008-07-30 12:06:12 -07003288 * This should get us reasonable parallelism between CPU and GPU but also
3289 * relatively low latency when blocking on a particular request to finish.
3290 */
3291static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003292i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003293{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003294 struct drm_i915_private *dev_priv = dev->dev_private;
3295 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00003296 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003297 struct drm_i915_gem_request *request;
3298 struct intel_ring_buffer *ring = NULL;
3299 u32 seqno = 0;
3300 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003301
Chris Wilsone110e8d2011-01-26 15:39:14 +00003302 if (atomic_read(&dev_priv->mm.wedged))
3303 return -EIO;
3304
Chris Wilson1c255952010-09-26 11:03:27 +01003305 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003306 list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
Eric Anholtb9624422009-06-03 07:27:35 +00003307 if (time_after_eq(request->emitted_jiffies, recent_enough))
3308 break;
3309
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003310 ring = request->ring;
3311 seqno = request->seqno;
Eric Anholtb9624422009-06-03 07:27:35 +00003312 }
Chris Wilson1c255952010-09-26 11:03:27 +01003313 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003314
3315 if (seqno == 0)
3316 return 0;
3317
Ben Widawsky5c81fe852012-05-24 15:03:08 -07003318 ret = __wait_seqno(ring, seqno, true, NULL);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003319 if (ret == 0)
3320 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
Eric Anholtb9624422009-06-03 07:27:35 +00003321
Eric Anholt673a3942008-07-30 12:06:12 -07003322 return ret;
3323}
3324
Eric Anholt673a3942008-07-30 12:06:12 -07003325int
Chris Wilson05394f32010-11-08 19:18:58 +00003326i915_gem_object_pin(struct drm_i915_gem_object *obj,
3327 uint32_t alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01003328 bool map_and_fenceable)
Eric Anholt673a3942008-07-30 12:06:12 -07003329{
Eric Anholt673a3942008-07-30 12:06:12 -07003330 int ret;
3331
Chris Wilson05394f32010-11-08 19:18:58 +00003332 BUG_ON(obj->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01003333
Chris Wilson05394f32010-11-08 19:18:58 +00003334 if (obj->gtt_space != NULL) {
3335 if ((alignment && obj->gtt_offset & (alignment - 1)) ||
3336 (map_and_fenceable && !obj->map_and_fenceable)) {
3337 WARN(obj->pin_count,
Chris Wilsonae7d49d2010-08-04 12:37:41 +01003338 "bo is already pinned with incorrect alignment:"
Daniel Vetter75e9e912010-11-04 17:11:09 +01003339 " offset=%x, req.alignment=%x, req.map_and_fenceable=%d,"
3340 " obj->map_and_fenceable=%d\n",
Chris Wilson05394f32010-11-08 19:18:58 +00003341 obj->gtt_offset, alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01003342 map_and_fenceable,
Chris Wilson05394f32010-11-08 19:18:58 +00003343 obj->map_and_fenceable);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01003344 ret = i915_gem_object_unbind(obj);
3345 if (ret)
3346 return ret;
3347 }
3348 }
3349
Chris Wilson05394f32010-11-08 19:18:58 +00003350 if (obj->gtt_space == NULL) {
Chris Wilsona00b10c2010-09-24 21:15:47 +01003351 ret = i915_gem_object_bind_to_gtt(obj, alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01003352 map_and_fenceable);
Chris Wilson97311292009-09-21 00:22:34 +01003353 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07003354 return ret;
Chris Wilson22c344e2009-02-11 14:26:45 +00003355 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05003356
Daniel Vetter74898d72012-02-15 23:50:22 +01003357 if (!obj->has_global_gtt_mapping && map_and_fenceable)
3358 i915_gem_gtt_bind_object(obj, obj->cache_level);
3359
Chris Wilson1b502472012-04-24 15:47:30 +01003360 obj->pin_count++;
Chris Wilson6299f992010-11-24 12:23:44 +00003361 obj->pin_mappable |= map_and_fenceable;
Eric Anholt673a3942008-07-30 12:06:12 -07003362
3363 return 0;
3364}
3365
3366void
Chris Wilson05394f32010-11-08 19:18:58 +00003367i915_gem_object_unpin(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07003368{
Chris Wilson05394f32010-11-08 19:18:58 +00003369 BUG_ON(obj->pin_count == 0);
3370 BUG_ON(obj->gtt_space == NULL);
Eric Anholt673a3942008-07-30 12:06:12 -07003371
Chris Wilson1b502472012-04-24 15:47:30 +01003372 if (--obj->pin_count == 0)
Chris Wilson6299f992010-11-24 12:23:44 +00003373 obj->pin_mappable = false;
Eric Anholt673a3942008-07-30 12:06:12 -07003374}
3375
3376int
3377i915_gem_pin_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00003378 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003379{
3380 struct drm_i915_gem_pin *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003381 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07003382 int ret;
3383
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003384 ret = i915_mutex_lock_interruptible(dev);
3385 if (ret)
3386 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003387
Chris Wilson05394f32010-11-08 19:18:58 +00003388 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00003389 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003390 ret = -ENOENT;
3391 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07003392 }
Eric Anholt673a3942008-07-30 12:06:12 -07003393
Chris Wilson05394f32010-11-08 19:18:58 +00003394 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilsonbb6baf72009-09-22 14:24:13 +01003395 DRM_ERROR("Attempting to pin a purgeable buffer\n");
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003396 ret = -EINVAL;
3397 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003398 }
3399
Chris Wilson05394f32010-11-08 19:18:58 +00003400 if (obj->pin_filp != NULL && obj->pin_filp != file) {
Jesse Barnes79e53942008-11-07 14:24:08 -08003401 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
3402 args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003403 ret = -EINVAL;
3404 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08003405 }
3406
Chris Wilson05394f32010-11-08 19:18:58 +00003407 obj->user_pin_count++;
3408 obj->pin_filp = file;
3409 if (obj->user_pin_count == 1) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01003410 ret = i915_gem_object_pin(obj, args->alignment, true);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003411 if (ret)
3412 goto out;
Eric Anholt673a3942008-07-30 12:06:12 -07003413 }
3414
3415 /* XXX - flush the CPU caches for pinned objects
3416 * as the X server doesn't manage domains yet
3417 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003418 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00003419 args->offset = obj->gtt_offset;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003420out:
Chris Wilson05394f32010-11-08 19:18:58 +00003421 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003422unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07003423 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003424 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003425}
3426
3427int
3428i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00003429 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003430{
3431 struct drm_i915_gem_pin *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003432 struct drm_i915_gem_object *obj;
Chris Wilson76c1dec2010-09-25 11:22:51 +01003433 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003434
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003435 ret = i915_mutex_lock_interruptible(dev);
3436 if (ret)
3437 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003438
Chris Wilson05394f32010-11-08 19:18:58 +00003439 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00003440 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003441 ret = -ENOENT;
3442 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07003443 }
Chris Wilson76c1dec2010-09-25 11:22:51 +01003444
Chris Wilson05394f32010-11-08 19:18:58 +00003445 if (obj->pin_filp != file) {
Jesse Barnes79e53942008-11-07 14:24:08 -08003446 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
3447 args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003448 ret = -EINVAL;
3449 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08003450 }
Chris Wilson05394f32010-11-08 19:18:58 +00003451 obj->user_pin_count--;
3452 if (obj->user_pin_count == 0) {
3453 obj->pin_filp = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08003454 i915_gem_object_unpin(obj);
3455 }
Eric Anholt673a3942008-07-30 12:06:12 -07003456
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003457out:
Chris Wilson05394f32010-11-08 19:18:58 +00003458 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003459unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07003460 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003461 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003462}
3463
3464int
3465i915_gem_busy_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00003466 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003467{
3468 struct drm_i915_gem_busy *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003469 struct drm_i915_gem_object *obj;
Chris Wilson30dbf0c2010-09-25 10:19:17 +01003470 int ret;
3471
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003472 ret = i915_mutex_lock_interruptible(dev);
3473 if (ret)
3474 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003475
Chris Wilson05394f32010-11-08 19:18:58 +00003476 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00003477 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003478 ret = -ENOENT;
3479 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07003480 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08003481
Chris Wilson0be555b2010-08-04 15:36:30 +01003482 /* Count all active objects as busy, even if they are currently not used
3483 * by the gpu. Users of this interface expect objects to eventually
3484 * become non-busy without any further actions, therefore emit any
3485 * necessary flushes here.
Eric Anholtc4de0a52008-12-14 19:05:04 -08003486 */
Daniel Vetter30dfebf2012-06-01 15:21:23 +02003487 ret = i915_gem_object_flush_active(obj);
3488
Chris Wilson05394f32010-11-08 19:18:58 +00003489 args->busy = obj->active;
Chris Wilsone9808ed2012-07-04 12:25:08 +01003490 if (obj->ring) {
3491 BUILD_BUG_ON(I915_NUM_RINGS > 16);
3492 args->busy |= intel_ring_flag(obj->ring) << 16;
3493 }
Eric Anholt673a3942008-07-30 12:06:12 -07003494
Chris Wilson05394f32010-11-08 19:18:58 +00003495 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003496unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07003497 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003498 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003499}
3500
3501int
3502i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
3503 struct drm_file *file_priv)
3504{
Akshay Joshi0206e352011-08-16 15:34:10 -04003505 return i915_gem_ring_throttle(dev, file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07003506}
3507
Chris Wilson3ef94da2009-09-14 16:50:29 +01003508int
3509i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
3510 struct drm_file *file_priv)
3511{
3512 struct drm_i915_gem_madvise *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003513 struct drm_i915_gem_object *obj;
Chris Wilson76c1dec2010-09-25 11:22:51 +01003514 int ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003515
3516 switch (args->madv) {
3517 case I915_MADV_DONTNEED:
3518 case I915_MADV_WILLNEED:
3519 break;
3520 default:
3521 return -EINVAL;
3522 }
3523
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003524 ret = i915_mutex_lock_interruptible(dev);
3525 if (ret)
3526 return ret;
3527
Chris Wilson05394f32010-11-08 19:18:58 +00003528 obj = to_intel_bo(drm_gem_object_lookup(dev, file_priv, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00003529 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003530 ret = -ENOENT;
3531 goto unlock;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003532 }
Chris Wilson3ef94da2009-09-14 16:50:29 +01003533
Chris Wilson05394f32010-11-08 19:18:58 +00003534 if (obj->pin_count) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003535 ret = -EINVAL;
3536 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003537 }
3538
Chris Wilson05394f32010-11-08 19:18:58 +00003539 if (obj->madv != __I915_MADV_PURGED)
3540 obj->madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003541
Chris Wilson6c085a72012-08-20 11:40:46 +02003542 /* if the object is no longer attached, discard its backing storage */
3543 if (i915_gem_object_is_purgeable(obj) && obj->pages == NULL)
Chris Wilson2d7ef392009-09-20 23:13:10 +01003544 i915_gem_object_truncate(obj);
3545
Chris Wilson05394f32010-11-08 19:18:58 +00003546 args->retained = obj->madv != __I915_MADV_PURGED;
Chris Wilsonbb6baf72009-09-22 14:24:13 +01003547
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003548out:
Chris Wilson05394f32010-11-08 19:18:58 +00003549 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003550unlock:
Chris Wilson3ef94da2009-09-14 16:50:29 +01003551 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003552 return ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003553}
3554
Chris Wilson05394f32010-11-08 19:18:58 +00003555struct drm_i915_gem_object *i915_gem_alloc_object(struct drm_device *dev,
3556 size_t size)
Daniel Vetterac52bc52010-04-09 19:05:06 +00003557{
Chris Wilson73aa8082010-09-30 11:46:12 +01003558 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc397b902010-04-09 19:05:07 +00003559 struct drm_i915_gem_object *obj;
Hugh Dickins5949eac2011-06-27 16:18:18 -07003560 struct address_space *mapping;
Chris Wilsonbed1ea92012-05-24 20:48:12 +01003561 u32 mask;
Daniel Vetterc397b902010-04-09 19:05:07 +00003562
3563 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
3564 if (obj == NULL)
3565 return NULL;
3566
3567 if (drm_gem_object_init(dev, &obj->base, size) != 0) {
3568 kfree(obj);
3569 return NULL;
3570 }
3571
Chris Wilsonbed1ea92012-05-24 20:48:12 +01003572 mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
3573 if (IS_CRESTLINE(dev) || IS_BROADWATER(dev)) {
3574 /* 965gm cannot relocate objects above 4GiB. */
3575 mask &= ~__GFP_HIGHMEM;
3576 mask |= __GFP_DMA32;
3577 }
3578
Hugh Dickins5949eac2011-06-27 16:18:18 -07003579 mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Chris Wilsonbed1ea92012-05-24 20:48:12 +01003580 mapping_set_gfp_mask(mapping, mask);
Hugh Dickins5949eac2011-06-27 16:18:18 -07003581
Chris Wilson73aa8082010-09-30 11:46:12 +01003582 i915_gem_info_add_obj(dev_priv, size);
3583
Daniel Vetterc397b902010-04-09 19:05:07 +00003584 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
3585 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3586
Eugeni Dodonov3d29b842012-01-17 14:43:53 -02003587 if (HAS_LLC(dev)) {
3588 /* On some devices, we can have the GPU use the LLC (the CPU
Eric Anholta1871112011-03-29 16:59:55 -07003589 * cache) for about a 10% performance improvement
3590 * compared to uncached. Graphics requests other than
3591 * display scanout are coherent with the CPU in
3592 * accessing this cache. This means in this mode we
3593 * don't need to clflush on the CPU side, and on the
3594 * GPU side we only need to flush internal caches to
3595 * get data visible to the CPU.
3596 *
3597 * However, we maintain the display planes as UC, and so
3598 * need to rebind when first used as such.
3599 */
3600 obj->cache_level = I915_CACHE_LLC;
3601 } else
3602 obj->cache_level = I915_CACHE_NONE;
3603
Daniel Vetter62b8b212010-04-09 19:05:08 +00003604 obj->base.driver_private = NULL;
Daniel Vetterc397b902010-04-09 19:05:07 +00003605 obj->fence_reg = I915_FENCE_REG_NONE;
Chris Wilson69dc4982010-10-19 10:36:51 +01003606 INIT_LIST_HEAD(&obj->mm_list);
Daniel Vetter93a37f22010-11-05 20:24:53 +01003607 INIT_LIST_HEAD(&obj->gtt_list);
Chris Wilson69dc4982010-10-19 10:36:51 +01003608 INIT_LIST_HEAD(&obj->ring_list);
Chris Wilson432e58e2010-11-25 19:32:06 +00003609 INIT_LIST_HEAD(&obj->exec_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00003610 obj->madv = I915_MADV_WILLNEED;
Daniel Vetter75e9e912010-11-04 17:11:09 +01003611 /* Avoid an unnecessary call to unbind on the first bind. */
3612 obj->map_and_fenceable = true;
Daniel Vetterc397b902010-04-09 19:05:07 +00003613
Chris Wilson05394f32010-11-08 19:18:58 +00003614 return obj;
Daniel Vetterac52bc52010-04-09 19:05:06 +00003615}
3616
Eric Anholt673a3942008-07-30 12:06:12 -07003617int i915_gem_init_object(struct drm_gem_object *obj)
3618{
Daniel Vetterc397b902010-04-09 19:05:07 +00003619 BUG();
Jesse Barnesde151cf2008-11-12 10:03:55 -08003620
Eric Anholt673a3942008-07-30 12:06:12 -07003621 return 0;
3622}
3623
Chris Wilson1488fc02012-04-24 15:47:31 +01003624void i915_gem_free_object(struct drm_gem_object *gem_obj)
Chris Wilsonbe726152010-07-23 23:18:50 +01003625{
Chris Wilson1488fc02012-04-24 15:47:31 +01003626 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
Chris Wilson05394f32010-11-08 19:18:58 +00003627 struct drm_device *dev = obj->base.dev;
Chris Wilsonbe726152010-07-23 23:18:50 +01003628 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonbe726152010-07-23 23:18:50 +01003629
Chris Wilson26e12f892011-03-20 11:20:19 +00003630 trace_i915_gem_object_destroy(obj);
3631
Daniel Vetter1286ff72012-05-10 15:25:09 +02003632 if (gem_obj->import_attach)
3633 drm_prime_gem_destroy(gem_obj, obj->sg_table);
3634
Chris Wilson1488fc02012-04-24 15:47:31 +01003635 if (obj->phys_obj)
3636 i915_gem_detach_phys_object(dev, obj);
3637
3638 obj->pin_count = 0;
3639 if (WARN_ON(i915_gem_object_unbind(obj) == -ERESTARTSYS)) {
3640 bool was_interruptible;
3641
3642 was_interruptible = dev_priv->mm.interruptible;
3643 dev_priv->mm.interruptible = false;
3644
3645 WARN_ON(i915_gem_object_unbind(obj));
3646
3647 dev_priv->mm.interruptible = was_interruptible;
3648 }
3649
Chris Wilson6c085a72012-08-20 11:40:46 +02003650 i915_gem_object_put_pages_gtt(obj);
Chris Wilsond8cb5082012-08-11 15:41:03 +01003651 i915_gem_object_free_mmap_offset(obj);
Chris Wilsonbe726152010-07-23 23:18:50 +01003652
Chris Wilson05394f32010-11-08 19:18:58 +00003653 drm_gem_object_release(&obj->base);
3654 i915_gem_info_remove_obj(dev_priv, obj->base.size);
Chris Wilsonbe726152010-07-23 23:18:50 +01003655
Chris Wilson05394f32010-11-08 19:18:58 +00003656 kfree(obj->bit_17);
3657 kfree(obj);
Chris Wilsonbe726152010-07-23 23:18:50 +01003658}
3659
Jesse Barnes5669fca2009-02-17 15:13:31 -08003660int
Eric Anholt673a3942008-07-30 12:06:12 -07003661i915_gem_idle(struct drm_device *dev)
3662{
3663 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson29105cc2010-01-07 10:39:13 +00003664 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003665
Keith Packard6dbe2772008-10-14 21:41:13 -07003666 mutex_lock(&dev->struct_mutex);
3667
Chris Wilson87acb0a2010-10-19 10:13:00 +01003668 if (dev_priv->mm.suspended) {
Keith Packard6dbe2772008-10-14 21:41:13 -07003669 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07003670 return 0;
Keith Packard6dbe2772008-10-14 21:41:13 -07003671 }
Eric Anholt673a3942008-07-30 12:06:12 -07003672
Ben Widawskyb2da9fe2012-04-26 16:02:58 -07003673 ret = i915_gpu_idle(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07003674 if (ret) {
3675 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07003676 return ret;
Keith Packard6dbe2772008-10-14 21:41:13 -07003677 }
Ben Widawskyb2da9fe2012-04-26 16:02:58 -07003678 i915_gem_retire_requests(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07003679
Chris Wilson29105cc2010-01-07 10:39:13 +00003680 /* Under UMS, be paranoid and evict. */
Chris Wilsona39d7ef2012-04-24 18:22:52 +01003681 if (!drm_core_check_feature(dev, DRIVER_MODESET))
Chris Wilson6c085a72012-08-20 11:40:46 +02003682 i915_gem_evict_everything(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00003683
Chris Wilson312817a2010-11-22 11:50:11 +00003684 i915_gem_reset_fences(dev);
3685
Chris Wilson29105cc2010-01-07 10:39:13 +00003686 /* Hack! Don't let anybody do execbuf while we don't control the chip.
3687 * We need to replace this with a semaphore, or something.
3688 * And not confound mm.suspended!
3689 */
3690 dev_priv->mm.suspended = 1;
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02003691 del_timer_sync(&dev_priv->hangcheck_timer);
Chris Wilson29105cc2010-01-07 10:39:13 +00003692
3693 i915_kernel_lost_context(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07003694 i915_gem_cleanup_ringbuffer(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00003695
Keith Packard6dbe2772008-10-14 21:41:13 -07003696 mutex_unlock(&dev->struct_mutex);
3697
Chris Wilson29105cc2010-01-07 10:39:13 +00003698 /* Cancel the retire work handler, which should be idle now. */
3699 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
3700
Eric Anholt673a3942008-07-30 12:06:12 -07003701 return 0;
3702}
3703
Ben Widawskyb9524a12012-05-25 16:56:24 -07003704void i915_gem_l3_remap(struct drm_device *dev)
3705{
3706 drm_i915_private_t *dev_priv = dev->dev_private;
3707 u32 misccpctl;
3708 int i;
3709
3710 if (!IS_IVYBRIDGE(dev))
3711 return;
3712
3713 if (!dev_priv->mm.l3_remap_info)
3714 return;
3715
3716 misccpctl = I915_READ(GEN7_MISCCPCTL);
3717 I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
3718 POSTING_READ(GEN7_MISCCPCTL);
3719
3720 for (i = 0; i < GEN7_L3LOG_SIZE; i += 4) {
3721 u32 remap = I915_READ(GEN7_L3LOG_BASE + i);
3722 if (remap && remap != dev_priv->mm.l3_remap_info[i/4])
3723 DRM_DEBUG("0x%x was already programmed to %x\n",
3724 GEN7_L3LOG_BASE + i, remap);
3725 if (remap && !dev_priv->mm.l3_remap_info[i/4])
3726 DRM_DEBUG_DRIVER("Clearing remapped register\n");
3727 I915_WRITE(GEN7_L3LOG_BASE + i, dev_priv->mm.l3_remap_info[i/4]);
3728 }
3729
3730 /* Make sure all the writes land before disabling dop clock gating */
3731 POSTING_READ(GEN7_L3LOG_BASE);
3732
3733 I915_WRITE(GEN7_MISCCPCTL, misccpctl);
3734}
3735
Daniel Vetterf691e2f2012-02-02 09:58:12 +01003736void i915_gem_init_swizzling(struct drm_device *dev)
3737{
3738 drm_i915_private_t *dev_priv = dev->dev_private;
3739
Daniel Vetter11782b02012-01-31 16:47:55 +01003740 if (INTEL_INFO(dev)->gen < 5 ||
Daniel Vetterf691e2f2012-02-02 09:58:12 +01003741 dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
3742 return;
3743
3744 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
3745 DISP_TILE_SURFACE_SWIZZLING);
3746
Daniel Vetter11782b02012-01-31 16:47:55 +01003747 if (IS_GEN5(dev))
3748 return;
3749
Daniel Vetterf691e2f2012-02-02 09:58:12 +01003750 I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
3751 if (IS_GEN6(dev))
Daniel Vetter6b26c862012-04-24 14:04:12 +02003752 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
Daniel Vetterf691e2f2012-02-02 09:58:12 +01003753 else
Daniel Vetter6b26c862012-04-24 14:04:12 +02003754 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
Daniel Vetterf691e2f2012-02-02 09:58:12 +01003755}
Daniel Vettere21af882012-02-09 20:53:27 +01003756
3757void i915_gem_init_ppgtt(struct drm_device *dev)
3758{
3759 drm_i915_private_t *dev_priv = dev->dev_private;
3760 uint32_t pd_offset;
3761 struct intel_ring_buffer *ring;
Daniel Vetter55a254a2012-03-22 00:14:43 +01003762 struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
3763 uint32_t __iomem *pd_addr;
3764 uint32_t pd_entry;
Daniel Vettere21af882012-02-09 20:53:27 +01003765 int i;
3766
3767 if (!dev_priv->mm.aliasing_ppgtt)
3768 return;
3769
Daniel Vetter55a254a2012-03-22 00:14:43 +01003770
3771 pd_addr = dev_priv->mm.gtt->gtt + ppgtt->pd_offset/sizeof(uint32_t);
3772 for (i = 0; i < ppgtt->num_pd_entries; i++) {
3773 dma_addr_t pt_addr;
3774
3775 if (dev_priv->mm.gtt->needs_dmar)
3776 pt_addr = ppgtt->pt_dma_addr[i];
3777 else
3778 pt_addr = page_to_phys(ppgtt->pt_pages[i]);
3779
3780 pd_entry = GEN6_PDE_ADDR_ENCODE(pt_addr);
3781 pd_entry |= GEN6_PDE_VALID;
3782
3783 writel(pd_entry, pd_addr + i);
3784 }
3785 readl(pd_addr);
3786
3787 pd_offset = ppgtt->pd_offset;
Daniel Vettere21af882012-02-09 20:53:27 +01003788 pd_offset /= 64; /* in cachelines, */
3789 pd_offset <<= 16;
3790
3791 if (INTEL_INFO(dev)->gen == 6) {
Daniel Vetter48ecfa12012-04-11 20:42:40 +02003792 uint32_t ecochk, gab_ctl, ecobits;
3793
3794 ecobits = I915_READ(GAC_ECO_BITS);
3795 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B);
Daniel Vetterbe901a52012-04-11 20:42:39 +02003796
3797 gab_ctl = I915_READ(GAB_CTL);
3798 I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT);
3799
3800 ecochk = I915_READ(GAM_ECOCHK);
Daniel Vettere21af882012-02-09 20:53:27 +01003801 I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT |
3802 ECOCHK_PPGTT_CACHE64B);
Daniel Vetter6b26c862012-04-24 14:04:12 +02003803 I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Daniel Vettere21af882012-02-09 20:53:27 +01003804 } else if (INTEL_INFO(dev)->gen >= 7) {
3805 I915_WRITE(GAM_ECOCHK, ECOCHK_PPGTT_CACHE64B);
3806 /* GFX_MODE is per-ring on gen7+ */
3807 }
3808
Chris Wilsonb4519512012-05-11 14:29:30 +01003809 for_each_ring(ring, dev_priv, i) {
Daniel Vettere21af882012-02-09 20:53:27 +01003810 if (INTEL_INFO(dev)->gen >= 7)
3811 I915_WRITE(RING_MODE_GEN7(ring),
Daniel Vetter6b26c862012-04-24 14:04:12 +02003812 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Daniel Vettere21af882012-02-09 20:53:27 +01003813
3814 I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
3815 I915_WRITE(RING_PP_DIR_BASE(ring), pd_offset);
3816 }
3817}
3818
Chris Wilson67b1b572012-07-05 23:49:40 +01003819static bool
3820intel_enable_blt(struct drm_device *dev)
3821{
3822 if (!HAS_BLT(dev))
3823 return false;
3824
3825 /* The blitter was dysfunctional on early prototypes */
3826 if (IS_GEN6(dev) && dev->pdev->revision < 8) {
3827 DRM_INFO("BLT not supported on this pre-production hardware;"
3828 " graphics performance will be degraded.\n");
3829 return false;
3830 }
3831
3832 return true;
3833}
3834
Eric Anholt673a3942008-07-30 12:06:12 -07003835int
Daniel Vetterf691e2f2012-02-02 09:58:12 +01003836i915_gem_init_hw(struct drm_device *dev)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08003837{
3838 drm_i915_private_t *dev_priv = dev->dev_private;
3839 int ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01003840
Daniel Vetter8ecd1a62012-06-07 15:56:03 +02003841 if (!intel_enable_gtt())
3842 return -EIO;
3843
Ben Widawskyb9524a12012-05-25 16:56:24 -07003844 i915_gem_l3_remap(dev);
3845
Daniel Vetterf691e2f2012-02-02 09:58:12 +01003846 i915_gem_init_swizzling(dev);
3847
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08003848 ret = intel_init_render_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01003849 if (ret)
Chris Wilsonb6913e42010-11-12 10:46:37 +00003850 return ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01003851
3852 if (HAS_BSD(dev)) {
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08003853 ret = intel_init_bsd_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01003854 if (ret)
3855 goto cleanup_render_ring;
Zou Nan haid1b851f2010-05-21 09:08:57 +08003856 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01003857
Chris Wilson67b1b572012-07-05 23:49:40 +01003858 if (intel_enable_blt(dev)) {
Chris Wilson549f7362010-10-19 11:19:32 +01003859 ret = intel_init_blt_ring_buffer(dev);
3860 if (ret)
3861 goto cleanup_bsd_ring;
3862 }
3863
Chris Wilson6f392d5482010-08-07 11:01:22 +01003864 dev_priv->next_seqno = 1;
3865
Ben Widawsky254f9652012-06-04 14:42:42 -07003866 /*
3867 * XXX: There was some w/a described somewhere suggesting loading
3868 * contexts before PPGTT.
3869 */
3870 i915_gem_context_init(dev);
Daniel Vettere21af882012-02-09 20:53:27 +01003871 i915_gem_init_ppgtt(dev);
3872
Chris Wilson68f95ba2010-05-27 13:18:22 +01003873 return 0;
3874
Chris Wilson549f7362010-10-19 11:19:32 +01003875cleanup_bsd_ring:
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003876 intel_cleanup_ring_buffer(&dev_priv->ring[VCS]);
Chris Wilson68f95ba2010-05-27 13:18:22 +01003877cleanup_render_ring:
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003878 intel_cleanup_ring_buffer(&dev_priv->ring[RCS]);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08003879 return ret;
3880}
3881
Chris Wilson1070a422012-04-24 15:47:41 +01003882static bool
3883intel_enable_ppgtt(struct drm_device *dev)
3884{
3885 if (i915_enable_ppgtt >= 0)
3886 return i915_enable_ppgtt;
3887
3888#ifdef CONFIG_INTEL_IOMMU
3889 /* Disable ppgtt on SNB if VT-d is on. */
3890 if (INTEL_INFO(dev)->gen == 6 && intel_iommu_gfx_mapped)
3891 return false;
3892#endif
3893
3894 return true;
3895}
3896
3897int i915_gem_init(struct drm_device *dev)
3898{
3899 struct drm_i915_private *dev_priv = dev->dev_private;
3900 unsigned long gtt_size, mappable_size;
3901 int ret;
3902
3903 gtt_size = dev_priv->mm.gtt->gtt_total_entries << PAGE_SHIFT;
3904 mappable_size = dev_priv->mm.gtt->gtt_mappable_entries << PAGE_SHIFT;
3905
3906 mutex_lock(&dev->struct_mutex);
3907 if (intel_enable_ppgtt(dev) && HAS_ALIASING_PPGTT(dev)) {
3908 /* PPGTT pdes are stolen from global gtt ptes, so shrink the
3909 * aperture accordingly when using aliasing ppgtt. */
3910 gtt_size -= I915_PPGTT_PD_ENTRIES*PAGE_SIZE;
3911
3912 i915_gem_init_global_gtt(dev, 0, mappable_size, gtt_size);
3913
3914 ret = i915_gem_init_aliasing_ppgtt(dev);
3915 if (ret) {
3916 mutex_unlock(&dev->struct_mutex);
3917 return ret;
3918 }
3919 } else {
3920 /* Let GEM Manage all of the aperture.
3921 *
3922 * However, leave one page at the end still bound to the scratch
3923 * page. There are a number of places where the hardware
3924 * apparently prefetches past the end of the object, and we've
3925 * seen multiple hangs with the GPU head pointer stuck in a
3926 * batchbuffer bound at the last page of the aperture. One page
3927 * should be enough to keep any prefetching inside of the
3928 * aperture.
3929 */
3930 i915_gem_init_global_gtt(dev, 0, mappable_size,
3931 gtt_size);
3932 }
3933
3934 ret = i915_gem_init_hw(dev);
3935 mutex_unlock(&dev->struct_mutex);
3936 if (ret) {
3937 i915_gem_cleanup_aliasing_ppgtt(dev);
3938 return ret;
3939 }
3940
Daniel Vetter53ca26c2012-04-26 23:28:03 +02003941 /* Allow hardware batchbuffers unless told otherwise, but not for KMS. */
3942 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3943 dev_priv->dri1.allow_batchbuffer = 1;
Chris Wilson1070a422012-04-24 15:47:41 +01003944 return 0;
3945}
3946
Zou Nan hai8187a2b2010-05-21 09:08:55 +08003947void
3948i915_gem_cleanup_ringbuffer(struct drm_device *dev)
3949{
3950 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonb4519512012-05-11 14:29:30 +01003951 struct intel_ring_buffer *ring;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003952 int i;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08003953
Chris Wilsonb4519512012-05-11 14:29:30 +01003954 for_each_ring(ring, dev_priv, i)
3955 intel_cleanup_ring_buffer(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08003956}
3957
3958int
Eric Anholt673a3942008-07-30 12:06:12 -07003959i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
3960 struct drm_file *file_priv)
3961{
3962 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonb4519512012-05-11 14:29:30 +01003963 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003964
Jesse Barnes79e53942008-11-07 14:24:08 -08003965 if (drm_core_check_feature(dev, DRIVER_MODESET))
3966 return 0;
3967
Ben Gamariba1234d2009-09-14 17:48:47 -04003968 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07003969 DRM_ERROR("Reenabling wedged hardware, good luck\n");
Ben Gamariba1234d2009-09-14 17:48:47 -04003970 atomic_set(&dev_priv->mm.wedged, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07003971 }
3972
Eric Anholt673a3942008-07-30 12:06:12 -07003973 mutex_lock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08003974 dev_priv->mm.suspended = 0;
3975
Daniel Vetterf691e2f2012-02-02 09:58:12 +01003976 ret = i915_gem_init_hw(dev);
Wu Fengguangd816f6a2009-04-18 10:43:32 +08003977 if (ret != 0) {
3978 mutex_unlock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08003979 return ret;
Wu Fengguangd816f6a2009-04-18 10:43:32 +08003980 }
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08003981
Chris Wilson69dc4982010-10-19 10:36:51 +01003982 BUG_ON(!list_empty(&dev_priv->mm.active_list));
Eric Anholt673a3942008-07-30 12:06:12 -07003983 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
Eric Anholt673a3942008-07-30 12:06:12 -07003984 mutex_unlock(&dev->struct_mutex);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04003985
Chris Wilson5f353082010-06-07 14:03:03 +01003986 ret = drm_irq_install(dev);
3987 if (ret)
3988 goto cleanup_ringbuffer;
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04003989
Eric Anholt673a3942008-07-30 12:06:12 -07003990 return 0;
Chris Wilson5f353082010-06-07 14:03:03 +01003991
3992cleanup_ringbuffer:
3993 mutex_lock(&dev->struct_mutex);
3994 i915_gem_cleanup_ringbuffer(dev);
3995 dev_priv->mm.suspended = 1;
3996 mutex_unlock(&dev->struct_mutex);
3997
3998 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003999}
4000
4001int
4002i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
4003 struct drm_file *file_priv)
4004{
Jesse Barnes79e53942008-11-07 14:24:08 -08004005 if (drm_core_check_feature(dev, DRIVER_MODESET))
4006 return 0;
4007
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004008 drm_irq_uninstall(dev);
Linus Torvaldse6890f62009-09-08 17:09:24 -07004009 return i915_gem_idle(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004010}
4011
4012void
4013i915_gem_lastclose(struct drm_device *dev)
4014{
4015 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004016
Eric Anholte806b492009-01-22 09:56:58 -08004017 if (drm_core_check_feature(dev, DRIVER_MODESET))
4018 return;
4019
Keith Packard6dbe2772008-10-14 21:41:13 -07004020 ret = i915_gem_idle(dev);
4021 if (ret)
4022 DRM_ERROR("failed to idle hardware: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07004023}
4024
Chris Wilson64193402010-10-24 12:38:05 +01004025static void
4026init_ring_lists(struct intel_ring_buffer *ring)
4027{
4028 INIT_LIST_HEAD(&ring->active_list);
4029 INIT_LIST_HEAD(&ring->request_list);
Chris Wilson64193402010-10-24 12:38:05 +01004030}
4031
Eric Anholt673a3942008-07-30 12:06:12 -07004032void
4033i915_gem_load(struct drm_device *dev)
4034{
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004035 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07004036 drm_i915_private_t *dev_priv = dev->dev_private;
4037
Chris Wilson69dc4982010-10-19 10:36:51 +01004038 INIT_LIST_HEAD(&dev_priv->mm.active_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004039 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
Chris Wilson6c085a72012-08-20 11:40:46 +02004040 INIT_LIST_HEAD(&dev_priv->mm.unbound_list);
4041 INIT_LIST_HEAD(&dev_priv->mm.bound_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07004042 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00004043 for (i = 0; i < I915_NUM_RINGS; i++)
4044 init_ring_lists(&dev_priv->ring[i]);
Daniel Vetter4b9de732011-10-09 21:52:02 +02004045 for (i = 0; i < I915_MAX_NUM_FENCES; i++)
Daniel Vetter007cc8a2010-04-28 11:02:31 +02004046 INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004047 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
4048 i915_gem_retire_work_handler);
Chris Wilson30dbf0c2010-09-25 10:19:17 +01004049 init_completion(&dev_priv->error_completion);
Chris Wilson31169712009-09-14 16:50:28 +01004050
Dave Airlie94400122010-07-20 13:15:31 +10004051 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
4052 if (IS_GEN3(dev)) {
Daniel Vetter50743292012-04-26 22:02:54 +02004053 I915_WRITE(MI_ARB_STATE,
4054 _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
Dave Airlie94400122010-07-20 13:15:31 +10004055 }
4056
Chris Wilson72bfa192010-12-19 11:42:05 +00004057 dev_priv->relative_constants_mode = I915_EXEC_CONSTANTS_REL_GENERAL;
4058
Jesse Barnesde151cf2008-11-12 10:03:55 -08004059 /* Old X drivers will take 0-2 for front, back, depth buffers */
Eric Anholtb397c832010-01-26 09:43:10 -08004060 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4061 dev_priv->fence_reg_start = 3;
Jesse Barnesde151cf2008-11-12 10:03:55 -08004062
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004063 if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08004064 dev_priv->num_fence_regs = 16;
4065 else
4066 dev_priv->num_fence_regs = 8;
4067
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004068 /* Initialize fence registers to zero */
Chris Wilsonada726c2012-04-17 15:31:32 +01004069 i915_gem_reset_fences(dev);
Eric Anholt10ed13e2011-05-06 13:53:49 -07004070
Eric Anholt673a3942008-07-30 12:06:12 -07004071 i915_gem_detect_bit_6_swizzle(dev);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05004072 init_waitqueue_head(&dev_priv->pending_flip_queue);
Chris Wilson17250b72010-10-28 12:51:39 +01004073
Chris Wilsonce453d82011-02-21 14:43:56 +00004074 dev_priv->mm.interruptible = true;
4075
Chris Wilson17250b72010-10-28 12:51:39 +01004076 dev_priv->mm.inactive_shrinker.shrink = i915_gem_inactive_shrink;
4077 dev_priv->mm.inactive_shrinker.seeks = DEFAULT_SEEKS;
4078 register_shrinker(&dev_priv->mm.inactive_shrinker);
Eric Anholt673a3942008-07-30 12:06:12 -07004079}
Dave Airlie71acb5e2008-12-30 20:31:46 +10004080
4081/*
4082 * Create a physically contiguous memory object for this object
4083 * e.g. for cursor + overlay regs
4084 */
Chris Wilson995b6762010-08-20 13:23:26 +01004085static int i915_gem_init_phys_object(struct drm_device *dev,
4086 int id, int size, int align)
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 int ret;
4091
4092 if (dev_priv->mm.phys_objs[id - 1] || !size)
4093 return 0;
4094
Eric Anholt9a298b22009-03-24 12:23:04 -07004095 phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004096 if (!phys_obj)
4097 return -ENOMEM;
4098
4099 phys_obj->id = id;
4100
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004101 phys_obj->handle = drm_pci_alloc(dev, size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004102 if (!phys_obj->handle) {
4103 ret = -ENOMEM;
4104 goto kfree_obj;
4105 }
4106#ifdef CONFIG_X86
4107 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4108#endif
4109
4110 dev_priv->mm.phys_objs[id - 1] = phys_obj;
4111
4112 return 0;
4113kfree_obj:
Eric Anholt9a298b22009-03-24 12:23:04 -07004114 kfree(phys_obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004115 return ret;
4116}
4117
Chris Wilson995b6762010-08-20 13:23:26 +01004118static void i915_gem_free_phys_object(struct drm_device *dev, int id)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004119{
4120 drm_i915_private_t *dev_priv = dev->dev_private;
4121 struct drm_i915_gem_phys_object *phys_obj;
4122
4123 if (!dev_priv->mm.phys_objs[id - 1])
4124 return;
4125
4126 phys_obj = dev_priv->mm.phys_objs[id - 1];
4127 if (phys_obj->cur_obj) {
4128 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
4129 }
4130
4131#ifdef CONFIG_X86
4132 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4133#endif
4134 drm_pci_free(dev, phys_obj->handle);
4135 kfree(phys_obj);
4136 dev_priv->mm.phys_objs[id - 1] = NULL;
4137}
4138
4139void i915_gem_free_all_phys_object(struct drm_device *dev)
4140{
4141 int i;
4142
Dave Airlie260883c2009-01-22 17:58:49 +10004143 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004144 i915_gem_free_phys_object(dev, i);
4145}
4146
4147void i915_gem_detach_phys_object(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00004148 struct drm_i915_gem_object *obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004149{
Chris Wilson05394f32010-11-08 19:18:58 +00004150 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Chris Wilsone5281cc2010-10-28 13:45:36 +01004151 char *vaddr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004152 int i;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004153 int page_count;
4154
Chris Wilson05394f32010-11-08 19:18:58 +00004155 if (!obj->phys_obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004156 return;
Chris Wilson05394f32010-11-08 19:18:58 +00004157 vaddr = obj->phys_obj->handle->vaddr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004158
Chris Wilson05394f32010-11-08 19:18:58 +00004159 page_count = obj->base.size / PAGE_SIZE;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004160 for (i = 0; i < page_count; i++) {
Hugh Dickins5949eac2011-06-27 16:18:18 -07004161 struct page *page = shmem_read_mapping_page(mapping, i);
Chris Wilsone5281cc2010-10-28 13:45:36 +01004162 if (!IS_ERR(page)) {
4163 char *dst = kmap_atomic(page);
4164 memcpy(dst, vaddr + i*PAGE_SIZE, PAGE_SIZE);
4165 kunmap_atomic(dst);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004166
Chris Wilsone5281cc2010-10-28 13:45:36 +01004167 drm_clflush_pages(&page, 1);
4168
4169 set_page_dirty(page);
4170 mark_page_accessed(page);
4171 page_cache_release(page);
4172 }
Dave Airlie71acb5e2008-12-30 20:31:46 +10004173 }
Daniel Vetter40ce6572010-11-05 18:12:18 +01004174 intel_gtt_chipset_flush();
Chris Wilsond78b47b2009-06-17 21:52:49 +01004175
Chris Wilson05394f32010-11-08 19:18:58 +00004176 obj->phys_obj->cur_obj = NULL;
4177 obj->phys_obj = NULL;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004178}
4179
4180int
4181i915_gem_attach_phys_object(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00004182 struct drm_i915_gem_object *obj,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004183 int id,
4184 int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004185{
Chris Wilson05394f32010-11-08 19:18:58 +00004186 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004187 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004188 int ret = 0;
4189 int page_count;
4190 int i;
4191
4192 if (id > I915_MAX_PHYS_OBJECT)
4193 return -EINVAL;
4194
Chris Wilson05394f32010-11-08 19:18:58 +00004195 if (obj->phys_obj) {
4196 if (obj->phys_obj->id == id)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004197 return 0;
4198 i915_gem_detach_phys_object(dev, obj);
4199 }
4200
Dave Airlie71acb5e2008-12-30 20:31:46 +10004201 /* create a new object */
4202 if (!dev_priv->mm.phys_objs[id - 1]) {
4203 ret = i915_gem_init_phys_object(dev, id,
Chris Wilson05394f32010-11-08 19:18:58 +00004204 obj->base.size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004205 if (ret) {
Chris Wilson05394f32010-11-08 19:18:58 +00004206 DRM_ERROR("failed to init phys object %d size: %zu\n",
4207 id, obj->base.size);
Chris Wilsone5281cc2010-10-28 13:45:36 +01004208 return ret;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004209 }
4210 }
4211
4212 /* bind to the object */
Chris Wilson05394f32010-11-08 19:18:58 +00004213 obj->phys_obj = dev_priv->mm.phys_objs[id - 1];
4214 obj->phys_obj->cur_obj = obj;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004215
Chris Wilson05394f32010-11-08 19:18:58 +00004216 page_count = obj->base.size / PAGE_SIZE;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004217
4218 for (i = 0; i < page_count; i++) {
Chris Wilsone5281cc2010-10-28 13:45:36 +01004219 struct page *page;
4220 char *dst, *src;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004221
Hugh Dickins5949eac2011-06-27 16:18:18 -07004222 page = shmem_read_mapping_page(mapping, i);
Chris Wilsone5281cc2010-10-28 13:45:36 +01004223 if (IS_ERR(page))
4224 return PTR_ERR(page);
4225
Chris Wilsonff75b9b2010-10-30 22:52:31 +01004226 src = kmap_atomic(page);
Chris Wilson05394f32010-11-08 19:18:58 +00004227 dst = obj->phys_obj->handle->vaddr + (i * PAGE_SIZE);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004228 memcpy(dst, src, PAGE_SIZE);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -07004229 kunmap_atomic(src);
Chris Wilsone5281cc2010-10-28 13:45:36 +01004230
4231 mark_page_accessed(page);
4232 page_cache_release(page);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004233 }
4234
4235 return 0;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004236}
4237
4238static int
Chris Wilson05394f32010-11-08 19:18:58 +00004239i915_gem_phys_pwrite(struct drm_device *dev,
4240 struct drm_i915_gem_object *obj,
Dave Airlie71acb5e2008-12-30 20:31:46 +10004241 struct drm_i915_gem_pwrite *args,
4242 struct drm_file *file_priv)
4243{
Chris Wilson05394f32010-11-08 19:18:58 +00004244 void *vaddr = obj->phys_obj->handle->vaddr + args->offset;
Chris Wilsonb47b30c2010-11-08 01:12:29 +00004245 char __user *user_data = (char __user *) (uintptr_t) args->data_ptr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004246
Chris Wilsonb47b30c2010-11-08 01:12:29 +00004247 if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) {
4248 unsigned long unwritten;
4249
4250 /* The physical object once assigned is fixed for the lifetime
4251 * of the obj, so we can safely drop the lock and continue
4252 * to access vaddr.
4253 */
4254 mutex_unlock(&dev->struct_mutex);
4255 unwritten = copy_from_user(vaddr, user_data, args->size);
4256 mutex_lock(&dev->struct_mutex);
4257 if (unwritten)
4258 return -EFAULT;
4259 }
Dave Airlie71acb5e2008-12-30 20:31:46 +10004260
Daniel Vetter40ce6572010-11-05 18:12:18 +01004261 intel_gtt_chipset_flush();
Dave Airlie71acb5e2008-12-30 20:31:46 +10004262 return 0;
4263}
Eric Anholtb9624422009-06-03 07:27:35 +00004264
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004265void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00004266{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004267 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00004268
4269 /* Clean up our request list when the client is going away, so that
4270 * later retire_requests won't dereference our soon-to-be-gone
4271 * file_priv.
4272 */
Chris Wilson1c255952010-09-26 11:03:27 +01004273 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004274 while (!list_empty(&file_priv->mm.request_list)) {
4275 struct drm_i915_gem_request *request;
4276
4277 request = list_first_entry(&file_priv->mm.request_list,
4278 struct drm_i915_gem_request,
4279 client_list);
4280 list_del(&request->client_list);
4281 request->file_priv = NULL;
4282 }
Chris Wilson1c255952010-09-26 11:03:27 +01004283 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00004284}
Chris Wilson31169712009-09-14 16:50:28 +01004285
Chris Wilson31169712009-09-14 16:50:28 +01004286static int
Ying Han1495f232011-05-24 17:12:27 -07004287i915_gem_inactive_shrink(struct shrinker *shrinker, struct shrink_control *sc)
Chris Wilson31169712009-09-14 16:50:28 +01004288{
Chris Wilson17250b72010-10-28 12:51:39 +01004289 struct drm_i915_private *dev_priv =
4290 container_of(shrinker,
4291 struct drm_i915_private,
4292 mm.inactive_shrinker);
4293 struct drm_device *dev = dev_priv->dev;
Chris Wilson6c085a72012-08-20 11:40:46 +02004294 struct drm_i915_gem_object *obj;
Ying Han1495f232011-05-24 17:12:27 -07004295 int nr_to_scan = sc->nr_to_scan;
Chris Wilson17250b72010-10-28 12:51:39 +01004296 int cnt;
4297
4298 if (!mutex_trylock(&dev->struct_mutex))
Chris Wilsonbbe2e112010-10-28 22:35:07 +01004299 return 0;
Chris Wilson31169712009-09-14 16:50:28 +01004300
Chris Wilson6c085a72012-08-20 11:40:46 +02004301 if (nr_to_scan) {
4302 nr_to_scan -= i915_gem_purge(dev_priv, nr_to_scan);
4303 if (nr_to_scan > 0)
4304 i915_gem_shrink_all(dev_priv);
Chris Wilson31169712009-09-14 16:50:28 +01004305 }
4306
Chris Wilson17250b72010-10-28 12:51:39 +01004307 cnt = 0;
Chris Wilson6c085a72012-08-20 11:40:46 +02004308 list_for_each_entry(obj, &dev_priv->mm.unbound_list, gtt_list)
4309 cnt += obj->base.size >> PAGE_SHIFT;
4310 list_for_each_entry(obj, &dev_priv->mm.bound_list, gtt_list)
4311 if (obj->pin_count == 0)
4312 cnt += obj->base.size >> PAGE_SHIFT;
Chris Wilson31169712009-09-14 16:50:28 +01004313
Chris Wilson17250b72010-10-28 12:51:39 +01004314 mutex_unlock(&dev->struct_mutex);
Chris Wilson6c085a72012-08-20 11:40:46 +02004315 return cnt;
Chris Wilson31169712009-09-14 16:50:28 +01004316}