blob: 462a8f2ad3a200be2822662e7bece791f50102d3 [file] [log] [blame]
Eric Anholt673a3942008-07-30 12:06:12 -07001/*
2 * Copyright © 2008 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 *
26 */
27
28#include "drmP.h"
29#include "drm.h"
30#include "i915_drm.h"
31#include "i915_drv.h"
Chris Wilson1c5d22f2009-08-25 11:15:50 +010032#include "i915_trace.h"
Jesse Barnes652c3932009-08-17 13:31:43 -070033#include "intel_drv.h"
Hugh Dickins5949eac2011-06-27 16:18:18 -070034#include <linux/shmem_fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
Eric Anholt673a3942008-07-30 12:06:12 -070036#include <linux/swap.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080037#include <linux/pci.h>
Daniel Vetter1286ff72012-05-10 15:25:09 +020038#include <linux/dma-buf.h>
Eric Anholt673a3942008-07-30 12:06:12 -070039
Chris Wilson05394f32010-11-08 19:18:58 +000040static void i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj);
41static void i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj);
Chris Wilson88241782011-01-07 17:09:48 +000042static __must_check int i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
43 unsigned alignment,
44 bool map_and_fenceable);
Chris Wilson05394f32010-11-08 19:18:58 +000045static int i915_gem_phys_pwrite(struct drm_device *dev,
46 struct drm_i915_gem_object *obj,
Dave Airlie71acb5e2008-12-30 20:31:46 +100047 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +000048 struct drm_file *file);
Eric Anholt673a3942008-07-30 12:06:12 -070049
Chris Wilson61050802012-04-17 15:31:31 +010050static void i915_gem_write_fence(struct drm_device *dev, int reg,
51 struct drm_i915_gem_object *obj);
52static void i915_gem_object_update_fence(struct drm_i915_gem_object *obj,
53 struct drm_i915_fence_reg *fence,
54 bool enable);
55
Chris Wilson17250b72010-10-28 12:51:39 +010056static int i915_gem_inactive_shrink(struct shrinker *shrinker,
Ying Han1495f232011-05-24 17:12:27 -070057 struct shrink_control *sc);
Chris Wilson6c085a72012-08-20 11:40:46 +020058static long i915_gem_purge(struct drm_i915_private *dev_priv, long target);
59static void i915_gem_shrink_all(struct drm_i915_private *dev_priv);
Daniel Vetter8c599672011-12-14 13:57:31 +010060static void i915_gem_object_truncate(struct drm_i915_gem_object *obj);
Chris Wilson31169712009-09-14 16:50:28 +010061
Chris Wilson61050802012-04-17 15:31:31 +010062static inline void i915_gem_object_fence_lost(struct drm_i915_gem_object *obj)
63{
64 if (obj->tiling_mode)
65 i915_gem_release_mmap(obj);
66
67 /* As we do not have an associated fence register, we will force
68 * a tiling change if we ever need to acquire one.
69 */
Chris Wilson5d82e3e2012-04-21 16:23:23 +010070 obj->fence_dirty = false;
Chris Wilson61050802012-04-17 15:31:31 +010071 obj->fence_reg = I915_FENCE_REG_NONE;
72}
73
Chris Wilson73aa8082010-09-30 11:46:12 +010074/* some bookkeeping */
75static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
76 size_t size)
77{
78 dev_priv->mm.object_count++;
79 dev_priv->mm.object_memory += size;
80}
81
82static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
83 size_t size)
84{
85 dev_priv->mm.object_count--;
86 dev_priv->mm.object_memory -= size;
87}
88
Chris Wilson21dd3732011-01-26 15:55:56 +000089static int
90i915_gem_wait_for_error(struct drm_device *dev)
Chris Wilson30dbf0c2010-09-25 10:19:17 +010091{
92 struct drm_i915_private *dev_priv = dev->dev_private;
93 struct completion *x = &dev_priv->error_completion;
94 unsigned long flags;
95 int ret;
96
97 if (!atomic_read(&dev_priv->mm.wedged))
98 return 0;
99
Daniel Vetter0a6759c2012-07-04 22:18:41 +0200100 /*
101 * Only wait 10 seconds for the gpu reset to complete to avoid hanging
102 * userspace. If it takes that long something really bad is going on and
103 * we should simply try to bail out and fail as gracefully as possible.
104 */
105 ret = wait_for_completion_interruptible_timeout(x, 10*HZ);
106 if (ret == 0) {
107 DRM_ERROR("Timed out waiting for the gpu reset to complete\n");
108 return -EIO;
109 } else if (ret < 0) {
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100110 return ret;
Daniel Vetter0a6759c2012-07-04 22:18:41 +0200111 }
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100112
Chris Wilson21dd3732011-01-26 15:55:56 +0000113 if (atomic_read(&dev_priv->mm.wedged)) {
114 /* GPU is hung, bump the completion count to account for
115 * the token we just consumed so that we never hit zero and
116 * end up waiting upon a subsequent completion event that
117 * will never happen.
118 */
119 spin_lock_irqsave(&x->wait.lock, flags);
120 x->done++;
121 spin_unlock_irqrestore(&x->wait.lock, flags);
122 }
123 return 0;
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100124}
125
Chris Wilson54cf91d2010-11-25 18:00:26 +0000126int i915_mutex_lock_interruptible(struct drm_device *dev)
Chris Wilson76c1dec2010-09-25 11:22:51 +0100127{
Chris Wilson76c1dec2010-09-25 11:22:51 +0100128 int ret;
129
Chris Wilson21dd3732011-01-26 15:55:56 +0000130 ret = i915_gem_wait_for_error(dev);
Chris Wilson76c1dec2010-09-25 11:22:51 +0100131 if (ret)
132 return ret;
133
134 ret = mutex_lock_interruptible(&dev->struct_mutex);
135 if (ret)
136 return ret;
137
Chris Wilson23bc5982010-09-29 16:10:57 +0100138 WARN_ON(i915_verify_lists(dev));
Chris Wilson76c1dec2010-09-25 11:22:51 +0100139 return 0;
140}
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100141
Chris Wilson7d1c4802010-08-07 21:45:03 +0100142static inline bool
Chris Wilson05394f32010-11-08 19:18:58 +0000143i915_gem_object_is_inactive(struct drm_i915_gem_object *obj)
Chris Wilson7d1c4802010-08-07 21:45:03 +0100144{
Chris Wilson6c085a72012-08-20 11:40:46 +0200145 return obj->gtt_space && !obj->active;
Chris Wilson7d1c4802010-08-07 21:45:03 +0100146}
147
Eric Anholt673a3942008-07-30 12:06:12 -0700148int
149i915_gem_init_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000150 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700151{
Eric Anholt673a3942008-07-30 12:06:12 -0700152 struct drm_i915_gem_init *args = data;
Chris Wilson20217462010-11-23 15:26:33 +0000153
Daniel Vetter7bb6fb82012-04-24 08:22:52 +0200154 if (drm_core_check_feature(dev, DRIVER_MODESET))
155 return -ENODEV;
156
Chris Wilson20217462010-11-23 15:26:33 +0000157 if (args->gtt_start >= args->gtt_end ||
158 (args->gtt_end | args->gtt_start) & (PAGE_SIZE - 1))
159 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -0700160
Daniel Vetterf534bc02012-03-26 22:37:04 +0200161 /* GEM with user mode setting was never supported on ilk and later. */
162 if (INTEL_INFO(dev)->gen >= 5)
163 return -ENODEV;
164
Eric Anholt673a3942008-07-30 12:06:12 -0700165 mutex_lock(&dev->struct_mutex);
Daniel Vetter644ec022012-03-26 09:45:40 +0200166 i915_gem_init_global_gtt(dev, args->gtt_start,
167 args->gtt_end, args->gtt_end);
Eric Anholt673a3942008-07-30 12:06:12 -0700168 mutex_unlock(&dev->struct_mutex);
169
Chris Wilson20217462010-11-23 15:26:33 +0000170 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700171}
172
Eric Anholt5a125c32008-10-22 21:40:13 -0700173int
174i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000175 struct drm_file *file)
Eric Anholt5a125c32008-10-22 21:40:13 -0700176{
Chris Wilson73aa8082010-09-30 11:46:12 +0100177 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt5a125c32008-10-22 21:40:13 -0700178 struct drm_i915_gem_get_aperture *args = data;
Chris Wilson6299f992010-11-24 12:23:44 +0000179 struct drm_i915_gem_object *obj;
180 size_t pinned;
Eric Anholt5a125c32008-10-22 21:40:13 -0700181
Chris Wilson6299f992010-11-24 12:23:44 +0000182 pinned = 0;
Chris Wilson73aa8082010-09-30 11:46:12 +0100183 mutex_lock(&dev->struct_mutex);
Chris Wilson6c085a72012-08-20 11:40:46 +0200184 list_for_each_entry(obj, &dev_priv->mm.bound_list, gtt_list)
Chris Wilson1b502472012-04-24 15:47:30 +0100185 if (obj->pin_count)
186 pinned += obj->gtt_space->size;
Chris Wilson73aa8082010-09-30 11:46:12 +0100187 mutex_unlock(&dev->struct_mutex);
Eric Anholt5a125c32008-10-22 21:40:13 -0700188
Chris Wilson6299f992010-11-24 12:23:44 +0000189 args->aper_size = dev_priv->mm.gtt_total;
Akshay Joshi0206e352011-08-16 15:34:10 -0400190 args->aper_available_size = args->aper_size - pinned;
Chris Wilson6299f992010-11-24 12:23:44 +0000191
Eric Anholt5a125c32008-10-22 21:40:13 -0700192 return 0;
193}
194
Dave Airlieff72145b2011-02-07 12:16:14 +1000195static int
196i915_gem_create(struct drm_file *file,
197 struct drm_device *dev,
198 uint64_t size,
199 uint32_t *handle_p)
Eric Anholt673a3942008-07-30 12:06:12 -0700200{
Chris Wilson05394f32010-11-08 19:18:58 +0000201 struct drm_i915_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300202 int ret;
203 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700204
Dave Airlieff72145b2011-02-07 12:16:14 +1000205 size = roundup(size, PAGE_SIZE);
Chris Wilson8ffc0242011-09-14 14:14:28 +0200206 if (size == 0)
207 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -0700208
209 /* Allocate the new object */
Dave Airlieff72145b2011-02-07 12:16:14 +1000210 obj = i915_gem_alloc_object(dev, size);
Eric Anholt673a3942008-07-30 12:06:12 -0700211 if (obj == NULL)
212 return -ENOMEM;
213
Chris Wilson05394f32010-11-08 19:18:58 +0000214 ret = drm_gem_handle_create(file, &obj->base, &handle);
Chris Wilson1dfd9752010-09-06 14:44:14 +0100215 if (ret) {
Chris Wilson05394f32010-11-08 19:18:58 +0000216 drm_gem_object_release(&obj->base);
217 i915_gem_info_remove_obj(dev->dev_private, obj->base.size);
Chris Wilson202f2fe2010-10-14 13:20:40 +0100218 kfree(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700219 return ret;
Chris Wilson1dfd9752010-09-06 14:44:14 +0100220 }
221
Chris Wilson202f2fe2010-10-14 13:20:40 +0100222 /* drop reference from allocate - handle holds it now */
Chris Wilson05394f32010-11-08 19:18:58 +0000223 drm_gem_object_unreference(&obj->base);
Chris Wilson202f2fe2010-10-14 13:20:40 +0100224 trace_i915_gem_object_create(obj);
225
Dave Airlieff72145b2011-02-07 12:16:14 +1000226 *handle_p = handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700227 return 0;
228}
229
Dave Airlieff72145b2011-02-07 12:16:14 +1000230int
231i915_gem_dumb_create(struct drm_file *file,
232 struct drm_device *dev,
233 struct drm_mode_create_dumb *args)
234{
235 /* have to work out size/pitch and return them */
Chris Wilsoned0291f2011-03-19 08:21:45 +0000236 args->pitch = ALIGN(args->width * ((args->bpp + 7) / 8), 64);
Dave Airlieff72145b2011-02-07 12:16:14 +1000237 args->size = args->pitch * args->height;
238 return i915_gem_create(file, dev,
239 args->size, &args->handle);
240}
241
242int i915_gem_dumb_destroy(struct drm_file *file,
243 struct drm_device *dev,
244 uint32_t handle)
245{
246 return drm_gem_handle_delete(file, handle);
247}
248
249/**
250 * Creates a new mm object and returns a handle to it.
251 */
252int
253i915_gem_create_ioctl(struct drm_device *dev, void *data,
254 struct drm_file *file)
255{
256 struct drm_i915_gem_create *args = data;
Daniel Vetter63ed2cb2012-04-23 16:50:50 +0200257
Dave Airlieff72145b2011-02-07 12:16:14 +1000258 return i915_gem_create(file, dev,
259 args->size, &args->handle);
260}
261
Chris Wilson05394f32010-11-08 19:18:58 +0000262static int i915_gem_object_needs_bit17_swizzle(struct drm_i915_gem_object *obj)
Eric Anholt280b7132009-03-12 16:56:27 -0700263{
Chris Wilson05394f32010-11-08 19:18:58 +0000264 drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
Eric Anholt280b7132009-03-12 16:56:27 -0700265
266 return dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_9_10_17 &&
Chris Wilson05394f32010-11-08 19:18:58 +0000267 obj->tiling_mode != I915_TILING_NONE;
Eric Anholt280b7132009-03-12 16:56:27 -0700268}
269
Daniel Vetter8c599672011-12-14 13:57:31 +0100270static inline int
Daniel Vetter8461d222011-12-14 13:57:32 +0100271__copy_to_user_swizzled(char __user *cpu_vaddr,
272 const char *gpu_vaddr, int gpu_offset,
273 int length)
274{
275 int ret, cpu_offset = 0;
276
277 while (length > 0) {
278 int cacheline_end = ALIGN(gpu_offset + 1, 64);
279 int this_length = min(cacheline_end - gpu_offset, length);
280 int swizzled_gpu_offset = gpu_offset ^ 64;
281
282 ret = __copy_to_user(cpu_vaddr + cpu_offset,
283 gpu_vaddr + swizzled_gpu_offset,
284 this_length);
285 if (ret)
286 return ret + length;
287
288 cpu_offset += this_length;
289 gpu_offset += this_length;
290 length -= this_length;
291 }
292
293 return 0;
294}
295
296static inline int
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -0700297__copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset,
298 const char __user *cpu_vaddr,
Daniel Vetter8c599672011-12-14 13:57:31 +0100299 int length)
300{
301 int ret, cpu_offset = 0;
302
303 while (length > 0) {
304 int cacheline_end = ALIGN(gpu_offset + 1, 64);
305 int this_length = min(cacheline_end - gpu_offset, length);
306 int swizzled_gpu_offset = gpu_offset ^ 64;
307
308 ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset,
309 cpu_vaddr + cpu_offset,
310 this_length);
311 if (ret)
312 return ret + length;
313
314 cpu_offset += this_length;
315 gpu_offset += this_length;
316 length -= this_length;
317 }
318
319 return 0;
320}
321
Daniel Vetterd174bd62012-03-25 19:47:40 +0200322/* Per-page copy function for the shmem pread fastpath.
323 * Flushes invalid cachelines before reading the target if
324 * needs_clflush is set. */
Eric Anholteb014592009-03-10 11:44:52 -0700325static int
Daniel Vetterd174bd62012-03-25 19:47:40 +0200326shmem_pread_fast(struct page *page, int shmem_page_offset, int page_length,
327 char __user *user_data,
328 bool page_do_bit17_swizzling, bool needs_clflush)
329{
330 char *vaddr;
331 int ret;
332
Daniel Vettere7e58eb2012-03-25 19:47:43 +0200333 if (unlikely(page_do_bit17_swizzling))
Daniel Vetterd174bd62012-03-25 19:47:40 +0200334 return -EINVAL;
335
336 vaddr = kmap_atomic(page);
337 if (needs_clflush)
338 drm_clflush_virt_range(vaddr + shmem_page_offset,
339 page_length);
340 ret = __copy_to_user_inatomic(user_data,
341 vaddr + shmem_page_offset,
342 page_length);
343 kunmap_atomic(vaddr);
344
345 return ret;
346}
347
Daniel Vetter23c18c72012-03-25 19:47:42 +0200348static void
349shmem_clflush_swizzled_range(char *addr, unsigned long length,
350 bool swizzled)
351{
Daniel Vettere7e58eb2012-03-25 19:47:43 +0200352 if (unlikely(swizzled)) {
Daniel Vetter23c18c72012-03-25 19:47:42 +0200353 unsigned long start = (unsigned long) addr;
354 unsigned long end = (unsigned long) addr + length;
355
356 /* For swizzling simply ensure that we always flush both
357 * channels. Lame, but simple and it works. Swizzled
358 * pwrite/pread is far from a hotpath - current userspace
359 * doesn't use it at all. */
360 start = round_down(start, 128);
361 end = round_up(end, 128);
362
363 drm_clflush_virt_range((void *)start, end - start);
364 } else {
365 drm_clflush_virt_range(addr, length);
366 }
367
368}
369
Daniel Vetterd174bd62012-03-25 19:47:40 +0200370/* Only difference to the fast-path function is that this can handle bit17
371 * and uses non-atomic copy and kmap functions. */
372static int
373shmem_pread_slow(struct page *page, int shmem_page_offset, int page_length,
374 char __user *user_data,
375 bool page_do_bit17_swizzling, bool needs_clflush)
376{
377 char *vaddr;
378 int ret;
379
380 vaddr = kmap(page);
381 if (needs_clflush)
Daniel Vetter23c18c72012-03-25 19:47:42 +0200382 shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
383 page_length,
384 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200385
386 if (page_do_bit17_swizzling)
387 ret = __copy_to_user_swizzled(user_data,
388 vaddr, shmem_page_offset,
389 page_length);
390 else
391 ret = __copy_to_user(user_data,
392 vaddr + shmem_page_offset,
393 page_length);
394 kunmap(page);
395
396 return ret;
397}
398
Eric Anholteb014592009-03-10 11:44:52 -0700399static int
Daniel Vetterdbf7bff2012-03-25 19:47:29 +0200400i915_gem_shmem_pread(struct drm_device *dev,
401 struct drm_i915_gem_object *obj,
402 struct drm_i915_gem_pread *args,
403 struct drm_file *file)
Eric Anholteb014592009-03-10 11:44:52 -0700404{
Chris Wilson05394f32010-11-08 19:18:58 +0000405 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Daniel Vetter8461d222011-12-14 13:57:32 +0100406 char __user *user_data;
Eric Anholteb014592009-03-10 11:44:52 -0700407 ssize_t remain;
Daniel Vetter8461d222011-12-14 13:57:32 +0100408 loff_t offset;
Ben Widawskyeb2c0c82012-02-15 14:42:43 +0100409 int shmem_page_offset, page_length, ret = 0;
Daniel Vetter8461d222011-12-14 13:57:32 +0100410 int obj_do_bit17_swizzling, page_do_bit17_swizzling;
Daniel Vetterdbf7bff2012-03-25 19:47:29 +0200411 int hit_slowpath = 0;
Daniel Vetter96d79b52012-03-25 19:47:36 +0200412 int prefaulted = 0;
Daniel Vetter84897312012-03-25 19:47:31 +0200413 int needs_clflush = 0;
Daniel Vetter692a5762012-03-25 19:47:34 +0200414 int release_page;
Eric Anholteb014592009-03-10 11:44:52 -0700415
Daniel Vetter8461d222011-12-14 13:57:32 +0100416 user_data = (char __user *) (uintptr_t) args->data_ptr;
Eric Anholteb014592009-03-10 11:44:52 -0700417 remain = args->size;
418
Daniel Vetter8461d222011-12-14 13:57:32 +0100419 obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700420
Daniel Vetter84897312012-03-25 19:47:31 +0200421 if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU)) {
422 /* If we're not in the cpu read domain, set ourself into the gtt
423 * read domain and manually flush cachelines (if required). This
424 * optimizes for the case when the gpu will dirty the data
425 * anyway again before the next pread happens. */
426 if (obj->cache_level == I915_CACHE_NONE)
427 needs_clflush = 1;
Chris Wilson6c085a72012-08-20 11:40:46 +0200428 if (obj->gtt_space) {
429 ret = i915_gem_object_set_to_gtt_domain(obj, false);
430 if (ret)
431 return ret;
432 }
Daniel Vetter84897312012-03-25 19:47:31 +0200433 }
Eric Anholteb014592009-03-10 11:44:52 -0700434
Eric Anholteb014592009-03-10 11:44:52 -0700435 offset = args->offset;
Daniel Vetter8461d222011-12-14 13:57:32 +0100436
Eric Anholteb014592009-03-10 11:44:52 -0700437 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100438 struct page *page;
439
Eric Anholteb014592009-03-10 11:44:52 -0700440 /* Operation in this page
441 *
Eric Anholteb014592009-03-10 11:44:52 -0700442 * shmem_page_offset = offset within page in shmem file
Eric Anholteb014592009-03-10 11:44:52 -0700443 * page_length = bytes to copy for this page
444 */
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100445 shmem_page_offset = offset_in_page(offset);
Eric Anholteb014592009-03-10 11:44:52 -0700446 page_length = remain;
447 if ((shmem_page_offset + page_length) > PAGE_SIZE)
448 page_length = PAGE_SIZE - shmem_page_offset;
Eric Anholteb014592009-03-10 11:44:52 -0700449
Daniel Vetter692a5762012-03-25 19:47:34 +0200450 if (obj->pages) {
451 page = obj->pages[offset >> PAGE_SHIFT];
452 release_page = 0;
453 } else {
454 page = shmem_read_mapping_page(mapping, offset >> PAGE_SHIFT);
455 if (IS_ERR(page)) {
456 ret = PTR_ERR(page);
457 goto out;
458 }
459 release_page = 1;
Jesper Juhlb65552f2011-06-12 20:53:44 +0000460 }
Chris Wilsone5281cc2010-10-28 13:45:36 +0100461
Daniel Vetter8461d222011-12-14 13:57:32 +0100462 page_do_bit17_swizzling = obj_do_bit17_swizzling &&
463 (page_to_phys(page) & (1 << 17)) != 0;
464
Daniel Vetterd174bd62012-03-25 19:47:40 +0200465 ret = shmem_pread_fast(page, shmem_page_offset, page_length,
466 user_data, page_do_bit17_swizzling,
467 needs_clflush);
468 if (ret == 0)
469 goto next_page;
Eric Anholteb014592009-03-10 11:44:52 -0700470
Daniel Vetterdbf7bff2012-03-25 19:47:29 +0200471 hit_slowpath = 1;
Daniel Vetter692a5762012-03-25 19:47:34 +0200472 page_cache_get(page);
Daniel Vetterdbf7bff2012-03-25 19:47:29 +0200473 mutex_unlock(&dev->struct_mutex);
474
Daniel Vetter96d79b52012-03-25 19:47:36 +0200475 if (!prefaulted) {
Daniel Vetterf56f8212012-03-25 19:47:41 +0200476 ret = fault_in_multipages_writeable(user_data, remain);
Daniel Vetter96d79b52012-03-25 19:47:36 +0200477 /* Userspace is tricking us, but we've already clobbered
478 * its pages with the prefault and promised to write the
479 * data up to the first fault. Hence ignore any errors
480 * and just continue. */
481 (void)ret;
482 prefaulted = 1;
483 }
484
Daniel Vetterd174bd62012-03-25 19:47:40 +0200485 ret = shmem_pread_slow(page, shmem_page_offset, page_length,
486 user_data, page_do_bit17_swizzling,
487 needs_clflush);
Eric Anholteb014592009-03-10 11:44:52 -0700488
Daniel Vetterdbf7bff2012-03-25 19:47:29 +0200489 mutex_lock(&dev->struct_mutex);
Chris Wilsone5281cc2010-10-28 13:45:36 +0100490 page_cache_release(page);
Daniel Vetterdbf7bff2012-03-25 19:47:29 +0200491next_page:
Chris Wilsone5281cc2010-10-28 13:45:36 +0100492 mark_page_accessed(page);
Daniel Vetter692a5762012-03-25 19:47:34 +0200493 if (release_page)
494 page_cache_release(page);
Chris Wilsone5281cc2010-10-28 13:45:36 +0100495
Daniel Vetter8461d222011-12-14 13:57:32 +0100496 if (ret) {
497 ret = -EFAULT;
498 goto out;
499 }
500
Eric Anholteb014592009-03-10 11:44:52 -0700501 remain -= page_length;
Daniel Vetter8461d222011-12-14 13:57:32 +0100502 user_data += page_length;
Eric Anholteb014592009-03-10 11:44:52 -0700503 offset += page_length;
504 }
505
Chris Wilson4f27b752010-10-14 15:26:45 +0100506out:
Daniel Vetterdbf7bff2012-03-25 19:47:29 +0200507 if (hit_slowpath) {
508 /* Fixup: Kill any reinstated backing storage pages */
509 if (obj->madv == __I915_MADV_PURGED)
510 i915_gem_object_truncate(obj);
511 }
Eric Anholteb014592009-03-10 11:44:52 -0700512
513 return ret;
514}
515
Eric Anholt673a3942008-07-30 12:06:12 -0700516/**
517 * Reads data from the object referenced by handle.
518 *
519 * On error, the contents of *data are undefined.
520 */
521int
522i915_gem_pread_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000523 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700524{
525 struct drm_i915_gem_pread *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +0000526 struct drm_i915_gem_object *obj;
Chris Wilson35b62a82010-09-26 20:23:38 +0100527 int ret = 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700528
Chris Wilson51311d02010-11-17 09:10:42 +0000529 if (args->size == 0)
530 return 0;
531
532 if (!access_ok(VERIFY_WRITE,
533 (char __user *)(uintptr_t)args->data_ptr,
534 args->size))
535 return -EFAULT;
536
Chris Wilson4f27b752010-10-14 15:26:45 +0100537 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100538 if (ret)
Chris Wilson4f27b752010-10-14 15:26:45 +0100539 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700540
Chris Wilson05394f32010-11-08 19:18:58 +0000541 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +0000542 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100543 ret = -ENOENT;
544 goto unlock;
Chris Wilson4f27b752010-10-14 15:26:45 +0100545 }
Eric Anholt673a3942008-07-30 12:06:12 -0700546
Chris Wilson7dcd2492010-09-26 20:21:44 +0100547 /* Bounds check source. */
Chris Wilson05394f32010-11-08 19:18:58 +0000548 if (args->offset > obj->base.size ||
549 args->size > obj->base.size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +0100550 ret = -EINVAL;
Chris Wilson35b62a82010-09-26 20:23:38 +0100551 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +0100552 }
553
Daniel Vetter1286ff72012-05-10 15:25:09 +0200554 /* prime objects have no backing filp to GEM pread/pwrite
555 * pages from.
556 */
557 if (!obj->base.filp) {
558 ret = -EINVAL;
559 goto out;
560 }
561
Chris Wilsondb53a302011-02-03 11:57:46 +0000562 trace_i915_gem_object_pread(obj, args->offset, args->size);
563
Daniel Vetterdbf7bff2012-03-25 19:47:29 +0200564 ret = i915_gem_shmem_pread(dev, obj, args, file);
Eric Anholt673a3942008-07-30 12:06:12 -0700565
Chris Wilson35b62a82010-09-26 20:23:38 +0100566out:
Chris Wilson05394f32010-11-08 19:18:58 +0000567 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100568unlock:
Chris Wilson4f27b752010-10-14 15:26:45 +0100569 mutex_unlock(&dev->struct_mutex);
Eric Anholteb014592009-03-10 11:44:52 -0700570 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700571}
572
Keith Packard0839ccb2008-10-30 19:38:48 -0700573/* This is the fast write path which cannot handle
574 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700575 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700576
Keith Packard0839ccb2008-10-30 19:38:48 -0700577static inline int
578fast_user_write(struct io_mapping *mapping,
579 loff_t page_base, int page_offset,
580 char __user *user_data,
581 int length)
582{
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -0700583 void __iomem *vaddr_atomic;
584 void *vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -0700585 unsigned long unwritten;
586
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700587 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -0700588 /* We can use the cpu mem copy function because this is X86. */
589 vaddr = (void __force*)vaddr_atomic + page_offset;
590 unwritten = __copy_from_user_inatomic_nocache(vaddr,
Keith Packard0839ccb2008-10-30 19:38:48 -0700591 user_data, length);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700592 io_mapping_unmap_atomic(vaddr_atomic);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100593 return unwritten;
Keith Packard0839ccb2008-10-30 19:38:48 -0700594}
595
Eric Anholt3de09aa2009-03-09 09:42:23 -0700596/**
597 * This is the fast pwrite path, where we copy the data directly from the
598 * user into the GTT, uncached.
599 */
Eric Anholt673a3942008-07-30 12:06:12 -0700600static int
Chris Wilson05394f32010-11-08 19:18:58 +0000601i915_gem_gtt_pwrite_fast(struct drm_device *dev,
602 struct drm_i915_gem_object *obj,
Eric Anholt3de09aa2009-03-09 09:42:23 -0700603 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000604 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700605{
Keith Packard0839ccb2008-10-30 19:38:48 -0700606 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700607 ssize_t remain;
Keith Packard0839ccb2008-10-30 19:38:48 -0700608 loff_t offset, page_base;
Eric Anholt673a3942008-07-30 12:06:12 -0700609 char __user *user_data;
Daniel Vetter935aaa62012-03-25 19:47:35 +0200610 int page_offset, page_length, ret;
611
612 ret = i915_gem_object_pin(obj, 0, true);
613 if (ret)
614 goto out;
615
616 ret = i915_gem_object_set_to_gtt_domain(obj, true);
617 if (ret)
618 goto out_unpin;
619
620 ret = i915_gem_object_put_fence(obj);
621 if (ret)
622 goto out_unpin;
Eric Anholt673a3942008-07-30 12:06:12 -0700623
624 user_data = (char __user *) (uintptr_t) args->data_ptr;
625 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700626
Chris Wilson05394f32010-11-08 19:18:58 +0000627 offset = obj->gtt_offset + args->offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700628
629 while (remain > 0) {
630 /* Operation in this page
631 *
Keith Packard0839ccb2008-10-30 19:38:48 -0700632 * page_base = page offset within aperture
633 * page_offset = offset within page
634 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -0700635 */
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100636 page_base = offset & PAGE_MASK;
637 page_offset = offset_in_page(offset);
Keith Packard0839ccb2008-10-30 19:38:48 -0700638 page_length = remain;
639 if ((page_offset + remain) > PAGE_SIZE)
640 page_length = PAGE_SIZE - page_offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700641
Keith Packard0839ccb2008-10-30 19:38:48 -0700642 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -0700643 * source page isn't available. Return the error and we'll
644 * retry in the slow path.
Keith Packard0839ccb2008-10-30 19:38:48 -0700645 */
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100646 if (fast_user_write(dev_priv->mm.gtt_mapping, page_base,
Daniel Vetter935aaa62012-03-25 19:47:35 +0200647 page_offset, user_data, page_length)) {
648 ret = -EFAULT;
649 goto out_unpin;
650 }
Eric Anholt673a3942008-07-30 12:06:12 -0700651
Keith Packard0839ccb2008-10-30 19:38:48 -0700652 remain -= page_length;
653 user_data += page_length;
654 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700655 }
Eric Anholt673a3942008-07-30 12:06:12 -0700656
Daniel Vetter935aaa62012-03-25 19:47:35 +0200657out_unpin:
658 i915_gem_object_unpin(obj);
659out:
Eric Anholt3de09aa2009-03-09 09:42:23 -0700660 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700661}
662
Daniel Vetterd174bd62012-03-25 19:47:40 +0200663/* Per-page copy function for the shmem pwrite fastpath.
664 * Flushes invalid cachelines before writing to the target if
665 * needs_clflush_before is set and flushes out any written cachelines after
666 * writing if needs_clflush is set. */
Eric Anholt673a3942008-07-30 12:06:12 -0700667static int
Daniel Vetterd174bd62012-03-25 19:47:40 +0200668shmem_pwrite_fast(struct page *page, int shmem_page_offset, int page_length,
669 char __user *user_data,
670 bool page_do_bit17_swizzling,
671 bool needs_clflush_before,
672 bool needs_clflush_after)
Eric Anholt673a3942008-07-30 12:06:12 -0700673{
Daniel Vetterd174bd62012-03-25 19:47:40 +0200674 char *vaddr;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700675 int ret;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700676
Daniel Vettere7e58eb2012-03-25 19:47:43 +0200677 if (unlikely(page_do_bit17_swizzling))
Daniel Vetterd174bd62012-03-25 19:47:40 +0200678 return -EINVAL;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700679
Daniel Vetterd174bd62012-03-25 19:47:40 +0200680 vaddr = kmap_atomic(page);
681 if (needs_clflush_before)
682 drm_clflush_virt_range(vaddr + shmem_page_offset,
683 page_length);
684 ret = __copy_from_user_inatomic_nocache(vaddr + shmem_page_offset,
685 user_data,
686 page_length);
687 if (needs_clflush_after)
688 drm_clflush_virt_range(vaddr + shmem_page_offset,
689 page_length);
690 kunmap_atomic(vaddr);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700691
692 return ret;
693}
694
Daniel Vetterd174bd62012-03-25 19:47:40 +0200695/* Only difference to the fast-path function is that this can handle bit17
696 * and uses non-atomic copy and kmap functions. */
Eric Anholt3043c602008-10-02 12:24:47 -0700697static int
Daniel Vetterd174bd62012-03-25 19:47:40 +0200698shmem_pwrite_slow(struct page *page, int shmem_page_offset, int page_length,
699 char __user *user_data,
700 bool page_do_bit17_swizzling,
701 bool needs_clflush_before,
702 bool needs_clflush_after)
Eric Anholt673a3942008-07-30 12:06:12 -0700703{
Daniel Vetterd174bd62012-03-25 19:47:40 +0200704 char *vaddr;
705 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -0700706
Daniel Vetterd174bd62012-03-25 19:47:40 +0200707 vaddr = kmap(page);
Daniel Vettere7e58eb2012-03-25 19:47:43 +0200708 if (unlikely(needs_clflush_before || page_do_bit17_swizzling))
Daniel Vetter23c18c72012-03-25 19:47:42 +0200709 shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
710 page_length,
711 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200712 if (page_do_bit17_swizzling)
713 ret = __copy_from_user_swizzled(vaddr, shmem_page_offset,
Chris Wilsone5281cc2010-10-28 13:45:36 +0100714 user_data,
715 page_length);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200716 else
717 ret = __copy_from_user(vaddr + shmem_page_offset,
718 user_data,
719 page_length);
720 if (needs_clflush_after)
Daniel Vetter23c18c72012-03-25 19:47:42 +0200721 shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
722 page_length,
723 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200724 kunmap(page);
Chris Wilsone5281cc2010-10-28 13:45:36 +0100725
Daniel Vetterd174bd62012-03-25 19:47:40 +0200726 return ret;
Eric Anholt40123c12009-03-09 13:42:30 -0700727}
728
Eric Anholt40123c12009-03-09 13:42:30 -0700729static int
Daniel Vettere244a442012-03-25 19:47:28 +0200730i915_gem_shmem_pwrite(struct drm_device *dev,
731 struct drm_i915_gem_object *obj,
732 struct drm_i915_gem_pwrite *args,
733 struct drm_file *file)
Eric Anholt40123c12009-03-09 13:42:30 -0700734{
Chris Wilson05394f32010-11-08 19:18:58 +0000735 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Eric Anholt40123c12009-03-09 13:42:30 -0700736 ssize_t remain;
Daniel Vetter8c599672011-12-14 13:57:31 +0100737 loff_t offset;
738 char __user *user_data;
Ben Widawskyeb2c0c82012-02-15 14:42:43 +0100739 int shmem_page_offset, page_length, ret = 0;
Daniel Vetter8c599672011-12-14 13:57:31 +0100740 int obj_do_bit17_swizzling, page_do_bit17_swizzling;
Daniel Vettere244a442012-03-25 19:47:28 +0200741 int hit_slowpath = 0;
Daniel Vetter58642882012-03-25 19:47:37 +0200742 int needs_clflush_after = 0;
743 int needs_clflush_before = 0;
Daniel Vetter692a5762012-03-25 19:47:34 +0200744 int release_page;
Eric Anholt40123c12009-03-09 13:42:30 -0700745
Daniel Vetter8c599672011-12-14 13:57:31 +0100746 user_data = (char __user *) (uintptr_t) args->data_ptr;
Eric Anholt40123c12009-03-09 13:42:30 -0700747 remain = args->size;
748
Daniel Vetter8c599672011-12-14 13:57:31 +0100749 obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700750
Daniel Vetter58642882012-03-25 19:47:37 +0200751 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
752 /* If we're not in the cpu write domain, set ourself into the gtt
753 * write domain and manually flush cachelines (if required). This
754 * optimizes for the case when the gpu will use the data
755 * right away and we therefore have to clflush anyway. */
756 if (obj->cache_level == I915_CACHE_NONE)
757 needs_clflush_after = 1;
Chris Wilson6c085a72012-08-20 11:40:46 +0200758 if (obj->gtt_space) {
759 ret = i915_gem_object_set_to_gtt_domain(obj, true);
760 if (ret)
761 return ret;
762 }
Daniel Vetter58642882012-03-25 19:47:37 +0200763 }
764 /* Same trick applies for invalidate partially written cachelines before
765 * writing. */
766 if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU)
767 && obj->cache_level == I915_CACHE_NONE)
768 needs_clflush_before = 1;
769
Eric Anholt40123c12009-03-09 13:42:30 -0700770 offset = args->offset;
Chris Wilson05394f32010-11-08 19:18:58 +0000771 obj->dirty = 1;
Eric Anholt40123c12009-03-09 13:42:30 -0700772
773 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100774 struct page *page;
Daniel Vetter58642882012-03-25 19:47:37 +0200775 int partial_cacheline_write;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100776
Eric Anholt40123c12009-03-09 13:42:30 -0700777 /* Operation in this page
778 *
Eric Anholt40123c12009-03-09 13:42:30 -0700779 * shmem_page_offset = offset within page in shmem file
Eric Anholt40123c12009-03-09 13:42:30 -0700780 * page_length = bytes to copy for this page
781 */
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100782 shmem_page_offset = offset_in_page(offset);
Eric Anholt40123c12009-03-09 13:42:30 -0700783
784 page_length = remain;
785 if ((shmem_page_offset + page_length) > PAGE_SIZE)
786 page_length = PAGE_SIZE - shmem_page_offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700787
Daniel Vetter58642882012-03-25 19:47:37 +0200788 /* If we don't overwrite a cacheline completely we need to be
789 * careful to have up-to-date data by first clflushing. Don't
790 * overcomplicate things and flush the entire patch. */
791 partial_cacheline_write = needs_clflush_before &&
792 ((shmem_page_offset | page_length)
793 & (boot_cpu_data.x86_clflush_size - 1));
794
Daniel Vetter692a5762012-03-25 19:47:34 +0200795 if (obj->pages) {
796 page = obj->pages[offset >> PAGE_SHIFT];
797 release_page = 0;
798 } else {
799 page = shmem_read_mapping_page(mapping, offset >> PAGE_SHIFT);
800 if (IS_ERR(page)) {
801 ret = PTR_ERR(page);
802 goto out;
803 }
804 release_page = 1;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100805 }
806
Daniel Vetter8c599672011-12-14 13:57:31 +0100807 page_do_bit17_swizzling = obj_do_bit17_swizzling &&
808 (page_to_phys(page) & (1 << 17)) != 0;
809
Daniel Vetterd174bd62012-03-25 19:47:40 +0200810 ret = shmem_pwrite_fast(page, shmem_page_offset, page_length,
811 user_data, page_do_bit17_swizzling,
812 partial_cacheline_write,
813 needs_clflush_after);
814 if (ret == 0)
815 goto next_page;
Eric Anholt40123c12009-03-09 13:42:30 -0700816
Daniel Vettere244a442012-03-25 19:47:28 +0200817 hit_slowpath = 1;
Daniel Vetter692a5762012-03-25 19:47:34 +0200818 page_cache_get(page);
Daniel Vettere244a442012-03-25 19:47:28 +0200819 mutex_unlock(&dev->struct_mutex);
820
Daniel Vetterd174bd62012-03-25 19:47:40 +0200821 ret = shmem_pwrite_slow(page, shmem_page_offset, page_length,
822 user_data, page_do_bit17_swizzling,
823 partial_cacheline_write,
824 needs_clflush_after);
Eric Anholt40123c12009-03-09 13:42:30 -0700825
Daniel Vettere244a442012-03-25 19:47:28 +0200826 mutex_lock(&dev->struct_mutex);
Daniel Vetter692a5762012-03-25 19:47:34 +0200827 page_cache_release(page);
Daniel Vettere244a442012-03-25 19:47:28 +0200828next_page:
Chris Wilsone5281cc2010-10-28 13:45:36 +0100829 set_page_dirty(page);
830 mark_page_accessed(page);
Daniel Vetter692a5762012-03-25 19:47:34 +0200831 if (release_page)
832 page_cache_release(page);
Chris Wilsone5281cc2010-10-28 13:45:36 +0100833
Daniel Vetter8c599672011-12-14 13:57:31 +0100834 if (ret) {
835 ret = -EFAULT;
836 goto out;
837 }
838
Eric Anholt40123c12009-03-09 13:42:30 -0700839 remain -= page_length;
Daniel Vetter8c599672011-12-14 13:57:31 +0100840 user_data += page_length;
Eric Anholt40123c12009-03-09 13:42:30 -0700841 offset += page_length;
842 }
843
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100844out:
Daniel Vettere244a442012-03-25 19:47:28 +0200845 if (hit_slowpath) {
846 /* Fixup: Kill any reinstated backing storage pages */
847 if (obj->madv == __I915_MADV_PURGED)
848 i915_gem_object_truncate(obj);
849 /* and flush dirty cachelines in case the object isn't in the cpu write
850 * domain anymore. */
851 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
852 i915_gem_clflush_object(obj);
853 intel_gtt_chipset_flush();
854 }
Daniel Vetter8c599672011-12-14 13:57:31 +0100855 }
Eric Anholt40123c12009-03-09 13:42:30 -0700856
Daniel Vetter58642882012-03-25 19:47:37 +0200857 if (needs_clflush_after)
858 intel_gtt_chipset_flush();
859
Eric Anholt40123c12009-03-09 13:42:30 -0700860 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700861}
862
863/**
864 * Writes data to the object referenced by handle.
865 *
866 * On error, the contents of the buffer that were to be modified are undefined.
867 */
868int
869i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100870 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700871{
872 struct drm_i915_gem_pwrite *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +0000873 struct drm_i915_gem_object *obj;
Chris Wilson51311d02010-11-17 09:10:42 +0000874 int ret;
875
876 if (args->size == 0)
877 return 0;
878
879 if (!access_ok(VERIFY_READ,
880 (char __user *)(uintptr_t)args->data_ptr,
881 args->size))
882 return -EFAULT;
883
Daniel Vetterf56f8212012-03-25 19:47:41 +0200884 ret = fault_in_multipages_readable((char __user *)(uintptr_t)args->data_ptr,
885 args->size);
Chris Wilson51311d02010-11-17 09:10:42 +0000886 if (ret)
887 return -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -0700888
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100889 ret = i915_mutex_lock_interruptible(dev);
890 if (ret)
891 return ret;
892
Chris Wilson05394f32010-11-08 19:18:58 +0000893 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +0000894 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100895 ret = -ENOENT;
896 goto unlock;
897 }
Eric Anholt673a3942008-07-30 12:06:12 -0700898
Chris Wilson7dcd2492010-09-26 20:21:44 +0100899 /* Bounds check destination. */
Chris Wilson05394f32010-11-08 19:18:58 +0000900 if (args->offset > obj->base.size ||
901 args->size > obj->base.size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +0100902 ret = -EINVAL;
Chris Wilson35b62a82010-09-26 20:23:38 +0100903 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +0100904 }
905
Daniel Vetter1286ff72012-05-10 15:25:09 +0200906 /* prime objects have no backing filp to GEM pread/pwrite
907 * pages from.
908 */
909 if (!obj->base.filp) {
910 ret = -EINVAL;
911 goto out;
912 }
913
Chris Wilsondb53a302011-02-03 11:57:46 +0000914 trace_i915_gem_object_pwrite(obj, args->offset, args->size);
915
Daniel Vetter935aaa62012-03-25 19:47:35 +0200916 ret = -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -0700917 /* We can only do the GTT pwrite on untiled buffers, as otherwise
918 * it would end up going through the fenced access, and we'll get
919 * different detiling behavior between reading and writing.
920 * pread/pwrite currently are reading and writing from the CPU
921 * perspective, requiring manual detiling by the client.
922 */
Daniel Vetter5c0480f2011-12-14 13:57:30 +0100923 if (obj->phys_obj) {
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100924 ret = i915_gem_phys_pwrite(dev, obj, args, file);
Daniel Vetter5c0480f2011-12-14 13:57:30 +0100925 goto out;
926 }
927
928 if (obj->gtt_space &&
Daniel Vetter3ae53782012-03-25 19:47:33 +0200929 obj->cache_level == I915_CACHE_NONE &&
Daniel Vetterc07496f2012-04-13 15:51:51 +0200930 obj->tiling_mode == I915_TILING_NONE &&
Daniel Vetterffc62972012-03-25 19:47:38 +0200931 obj->map_and_fenceable &&
Daniel Vetter5c0480f2011-12-14 13:57:30 +0100932 obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100933 ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file);
Daniel Vetter935aaa62012-03-25 19:47:35 +0200934 /* Note that the gtt paths might fail with non-page-backed user
935 * pointers (e.g. gtt mappings when moving data between
936 * textures). Fallback to the shmem path in that case. */
Eric Anholt40123c12009-03-09 13:42:30 -0700937 }
Eric Anholt673a3942008-07-30 12:06:12 -0700938
Daniel Vetter5c0480f2011-12-14 13:57:30 +0100939 if (ret == -EFAULT)
Daniel Vetter935aaa62012-03-25 19:47:35 +0200940 ret = i915_gem_shmem_pwrite(dev, obj, args, file);
Daniel Vetter5c0480f2011-12-14 13:57:30 +0100941
Chris Wilson35b62a82010-09-26 20:23:38 +0100942out:
Chris Wilson05394f32010-11-08 19:18:58 +0000943 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100944unlock:
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100945 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -0700946 return ret;
947}
948
949/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800950 * Called when user space prepares to use an object with the CPU, either
951 * through the mmap ioctl's mapping or a GTT mapping.
Eric Anholt673a3942008-07-30 12:06:12 -0700952 */
953int
954i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000955 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700956{
957 struct drm_i915_gem_set_domain *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +0000958 struct drm_i915_gem_object *obj;
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800959 uint32_t read_domains = args->read_domains;
960 uint32_t write_domain = args->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -0700961 int ret;
962
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800963 /* Only handle setting domains to types used by the CPU. */
Chris Wilson21d509e2009-06-06 09:46:02 +0100964 if (write_domain & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800965 return -EINVAL;
966
Chris Wilson21d509e2009-06-06 09:46:02 +0100967 if (read_domains & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800968 return -EINVAL;
969
970 /* Having something in the write domain implies it's in the read
971 * domain, and only that read domain. Enforce that in the request.
972 */
973 if (write_domain != 0 && read_domains != write_domain)
974 return -EINVAL;
975
Chris Wilson76c1dec2010-09-25 11:22:51 +0100976 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100977 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +0100978 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700979
Chris Wilson05394f32010-11-08 19:18:58 +0000980 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +0000981 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100982 ret = -ENOENT;
983 goto unlock;
Chris Wilson76c1dec2010-09-25 11:22:51 +0100984 }
Jesse Barnes652c3932009-08-17 13:31:43 -0700985
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800986 if (read_domains & I915_GEM_DOMAIN_GTT) {
987 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
Eric Anholt02354392008-11-26 13:58:13 -0800988
989 /* Silently promote "you're not bound, there was nothing to do"
990 * to success, since the client was just asking us to
991 * make sure everything was done.
992 */
993 if (ret == -EINVAL)
994 ret = 0;
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800995 } else {
Eric Anholte47c68e2008-11-14 13:35:19 -0800996 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800997 }
998
Chris Wilson05394f32010-11-08 19:18:58 +0000999 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001000unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07001001 mutex_unlock(&dev->struct_mutex);
1002 return ret;
1003}
1004
1005/**
1006 * Called when user space has done writes to this buffer
1007 */
1008int
1009i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001010 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001011{
1012 struct drm_i915_gem_sw_finish *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001013 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001014 int ret = 0;
1015
Chris Wilson76c1dec2010-09-25 11:22:51 +01001016 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001017 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001018 return ret;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001019
Chris Wilson05394f32010-11-08 19:18:58 +00001020 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00001021 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001022 ret = -ENOENT;
1023 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07001024 }
1025
Eric Anholt673a3942008-07-30 12:06:12 -07001026 /* Pinned buffers may be scanout, so flush the cache */
Chris Wilson05394f32010-11-08 19:18:58 +00001027 if (obj->pin_count)
Eric Anholte47c68e2008-11-14 13:35:19 -08001028 i915_gem_object_flush_cpu_write_domain(obj);
1029
Chris Wilson05394f32010-11-08 19:18:58 +00001030 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001031unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07001032 mutex_unlock(&dev->struct_mutex);
1033 return ret;
1034}
1035
1036/**
1037 * Maps the contents of an object, returning the address it is mapped
1038 * into.
1039 *
1040 * While the mapping holds a reference on the contents of the object, it doesn't
1041 * imply a ref on the object itself.
1042 */
1043int
1044i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001045 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001046{
1047 struct drm_i915_gem_mmap *args = data;
1048 struct drm_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001049 unsigned long addr;
1050
Chris Wilson05394f32010-11-08 19:18:58 +00001051 obj = drm_gem_object_lookup(dev, file, args->handle);
Eric Anholt673a3942008-07-30 12:06:12 -07001052 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001053 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001054
Daniel Vetter1286ff72012-05-10 15:25:09 +02001055 /* prime objects have no backing filp to GEM mmap
1056 * pages from.
1057 */
1058 if (!obj->filp) {
1059 drm_gem_object_unreference_unlocked(obj);
1060 return -EINVAL;
1061 }
1062
Linus Torvalds6be5ceb2012-04-20 17:13:58 -07001063 addr = vm_mmap(obj->filp, 0, args->size,
Eric Anholt673a3942008-07-30 12:06:12 -07001064 PROT_READ | PROT_WRITE, MAP_SHARED,
1065 args->offset);
Luca Barbieribc9025b2010-02-09 05:49:12 +00001066 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001067 if (IS_ERR((void *)addr))
1068 return addr;
1069
1070 args->addr_ptr = (uint64_t) addr;
1071
1072 return 0;
1073}
1074
Jesse Barnesde151cf2008-11-12 10:03:55 -08001075/**
1076 * i915_gem_fault - fault a page into the GTT
1077 * vma: VMA in question
1078 * vmf: fault info
1079 *
1080 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1081 * from userspace. The fault handler takes care of binding the object to
1082 * the GTT (if needed), allocating and programming a fence register (again,
1083 * only if needed based on whether the old reg is still valid or the object
1084 * is tiled) and inserting a new PTE into the faulting process.
1085 *
1086 * Note that the faulting process may involve evicting existing objects
1087 * from the GTT and/or fence registers to make room. So performance may
1088 * suffer if the GTT working set is large or there are few fence registers
1089 * left.
1090 */
1091int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1092{
Chris Wilson05394f32010-11-08 19:18:58 +00001093 struct drm_i915_gem_object *obj = to_intel_bo(vma->vm_private_data);
1094 struct drm_device *dev = obj->base.dev;
Chris Wilson7d1c4802010-08-07 21:45:03 +01001095 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001096 pgoff_t page_offset;
1097 unsigned long pfn;
1098 int ret = 0;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001099 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001100
1101 /* We don't use vmf->pgoff since that has the fake offset */
1102 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1103 PAGE_SHIFT;
1104
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001105 ret = i915_mutex_lock_interruptible(dev);
1106 if (ret)
1107 goto out;
Chris Wilsona00b10c2010-09-24 21:15:47 +01001108
Chris Wilsondb53a302011-02-03 11:57:46 +00001109 trace_i915_gem_object_fault(obj, page_offset, true, write);
1110
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001111 /* Now bind it into the GTT if needed */
Chris Wilson919926a2010-11-12 13:42:53 +00001112 if (!obj->map_and_fenceable) {
1113 ret = i915_gem_object_unbind(obj);
1114 if (ret)
1115 goto unlock;
Chris Wilsona00b10c2010-09-24 21:15:47 +01001116 }
Chris Wilson05394f32010-11-08 19:18:58 +00001117 if (!obj->gtt_space) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01001118 ret = i915_gem_object_bind_to_gtt(obj, 0, true);
Chris Wilsonc7150892009-09-23 00:43:56 +01001119 if (ret)
1120 goto unlock;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001121
Eric Anholte92d03b2011-06-14 16:43:09 -07001122 ret = i915_gem_object_set_to_gtt_domain(obj, write);
1123 if (ret)
1124 goto unlock;
1125 }
Chris Wilson4a684a42010-10-28 14:44:08 +01001126
Daniel Vetter74898d72012-02-15 23:50:22 +01001127 if (!obj->has_global_gtt_mapping)
1128 i915_gem_gtt_bind_object(obj, obj->cache_level);
1129
Chris Wilson06d98132012-04-17 15:31:24 +01001130 ret = i915_gem_object_get_fence(obj);
Chris Wilsond9e86c02010-11-10 16:40:20 +00001131 if (ret)
1132 goto unlock;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001133
Chris Wilson05394f32010-11-08 19:18:58 +00001134 if (i915_gem_object_is_inactive(obj))
1135 list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
Chris Wilson7d1c4802010-08-07 21:45:03 +01001136
Chris Wilson6299f992010-11-24 12:23:44 +00001137 obj->fault_mappable = true;
1138
Daniel Vetterdd2757f2012-06-07 15:55:57 +02001139 pfn = ((dev_priv->mm.gtt_base_addr + obj->gtt_offset) >> PAGE_SHIFT) +
Jesse Barnesde151cf2008-11-12 10:03:55 -08001140 page_offset;
1141
1142 /* Finally, remap it using the new GTT offset */
1143 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
Chris Wilsonc7150892009-09-23 00:43:56 +01001144unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001145 mutex_unlock(&dev->struct_mutex);
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001146out:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001147 switch (ret) {
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001148 case -EIO:
Daniel Vettera9340cc2012-07-04 22:18:42 +02001149 /* If this -EIO is due to a gpu hang, give the reset code a
1150 * chance to clean up the mess. Otherwise return the proper
1151 * SIGBUS. */
1152 if (!atomic_read(&dev_priv->mm.wedged))
1153 return VM_FAULT_SIGBUS;
Chris Wilson045e7692010-11-07 09:18:22 +00001154 case -EAGAIN:
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001155 /* Give the error handler a chance to run and move the
1156 * objects off the GPU active list. Next time we service the
1157 * fault, we should be able to transition the page into the
1158 * GTT without touching the GPU (and so avoid further
1159 * EIO/EGAIN). If the GPU is wedged, then there is no issue
1160 * with coherency, just lost writes.
1161 */
Chris Wilson045e7692010-11-07 09:18:22 +00001162 set_need_resched();
Chris Wilsonc7150892009-09-23 00:43:56 +01001163 case 0:
1164 case -ERESTARTSYS:
Chris Wilsonbed636a2011-02-11 20:31:19 +00001165 case -EINTR:
Chris Wilsonc7150892009-09-23 00:43:56 +01001166 return VM_FAULT_NOPAGE;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001167 case -ENOMEM:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001168 return VM_FAULT_OOM;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001169 default:
Chris Wilsonc7150892009-09-23 00:43:56 +01001170 return VM_FAULT_SIGBUS;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001171 }
1172}
1173
1174/**
Chris Wilson901782b2009-07-10 08:18:50 +01001175 * i915_gem_release_mmap - remove physical page mappings
1176 * @obj: obj in question
1177 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001178 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01001179 * relinquish ownership of the pages back to the system.
1180 *
1181 * It is vital that we remove the page mapping if we have mapped a tiled
1182 * object through the GTT and then lose the fence register due to
1183 * resource pressure. Similarly if the object has been moved out of the
1184 * aperture, than pages mapped into userspace must be revoked. Removing the
1185 * mapping will then trigger a page fault on the next user access, allowing
1186 * fixup by i915_gem_fault().
1187 */
Eric Anholtd05ca302009-07-10 13:02:26 -07001188void
Chris Wilson05394f32010-11-08 19:18:58 +00001189i915_gem_release_mmap(struct drm_i915_gem_object *obj)
Chris Wilson901782b2009-07-10 08:18:50 +01001190{
Chris Wilson6299f992010-11-24 12:23:44 +00001191 if (!obj->fault_mappable)
1192 return;
Chris Wilson901782b2009-07-10 08:18:50 +01001193
Chris Wilsonf6e47882011-03-20 21:09:12 +00001194 if (obj->base.dev->dev_mapping)
1195 unmap_mapping_range(obj->base.dev->dev_mapping,
1196 (loff_t)obj->base.map_list.hash.key<<PAGE_SHIFT,
1197 obj->base.size, 1);
Daniel Vetterfb7d5162010-10-01 22:05:20 +02001198
Chris Wilson6299f992010-11-24 12:23:44 +00001199 obj->fault_mappable = false;
Chris Wilson901782b2009-07-10 08:18:50 +01001200}
1201
Chris Wilson92b88ae2010-11-09 11:47:32 +00001202static uint32_t
Chris Wilsone28f8712011-07-18 13:11:49 -07001203i915_gem_get_gtt_size(struct drm_device *dev, uint32_t size, int tiling_mode)
Chris Wilson92b88ae2010-11-09 11:47:32 +00001204{
Chris Wilsone28f8712011-07-18 13:11:49 -07001205 uint32_t gtt_size;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001206
1207 if (INTEL_INFO(dev)->gen >= 4 ||
Chris Wilsone28f8712011-07-18 13:11:49 -07001208 tiling_mode == I915_TILING_NONE)
1209 return size;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001210
1211 /* Previous chips need a power-of-two fence region when tiling */
1212 if (INTEL_INFO(dev)->gen == 3)
Chris Wilsone28f8712011-07-18 13:11:49 -07001213 gtt_size = 1024*1024;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001214 else
Chris Wilsone28f8712011-07-18 13:11:49 -07001215 gtt_size = 512*1024;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001216
Chris Wilsone28f8712011-07-18 13:11:49 -07001217 while (gtt_size < size)
1218 gtt_size <<= 1;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001219
Chris Wilsone28f8712011-07-18 13:11:49 -07001220 return gtt_size;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001221}
1222
Jesse Barnesde151cf2008-11-12 10:03:55 -08001223/**
1224 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1225 * @obj: object to check
1226 *
1227 * Return the required GTT alignment for an object, taking into account
Daniel Vetter5e783302010-11-14 22:32:36 +01001228 * potential fence register mapping.
Jesse Barnesde151cf2008-11-12 10:03:55 -08001229 */
1230static uint32_t
Chris Wilsone28f8712011-07-18 13:11:49 -07001231i915_gem_get_gtt_alignment(struct drm_device *dev,
1232 uint32_t size,
1233 int tiling_mode)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001234{
Jesse Barnesde151cf2008-11-12 10:03:55 -08001235 /*
1236 * Minimum alignment is 4k (GTT page size), but might be greater
1237 * if a fence register is needed for the object.
1238 */
Chris Wilsona00b10c2010-09-24 21:15:47 +01001239 if (INTEL_INFO(dev)->gen >= 4 ||
Chris Wilsone28f8712011-07-18 13:11:49 -07001240 tiling_mode == I915_TILING_NONE)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001241 return 4096;
1242
1243 /*
1244 * Previous chips need to be aligned to the size of the smallest
1245 * fence register that can contain the object.
1246 */
Chris Wilsone28f8712011-07-18 13:11:49 -07001247 return i915_gem_get_gtt_size(dev, size, tiling_mode);
Chris Wilsona00b10c2010-09-24 21:15:47 +01001248}
1249
Daniel Vetter5e783302010-11-14 22:32:36 +01001250/**
1251 * i915_gem_get_unfenced_gtt_alignment - return required GTT alignment for an
1252 * unfenced object
Chris Wilsone28f8712011-07-18 13:11:49 -07001253 * @dev: the device
1254 * @size: size of the object
1255 * @tiling_mode: tiling mode of the object
Daniel Vetter5e783302010-11-14 22:32:36 +01001256 *
1257 * Return the required GTT alignment for an object, only taking into account
1258 * unfenced tiled surface requirements.
1259 */
Chris Wilson467cffb2011-03-07 10:42:03 +00001260uint32_t
Chris Wilsone28f8712011-07-18 13:11:49 -07001261i915_gem_get_unfenced_gtt_alignment(struct drm_device *dev,
1262 uint32_t size,
1263 int tiling_mode)
Daniel Vetter5e783302010-11-14 22:32:36 +01001264{
Daniel Vetter5e783302010-11-14 22:32:36 +01001265 /*
1266 * Minimum alignment is 4k (GTT page size) for sane hw.
1267 */
1268 if (INTEL_INFO(dev)->gen >= 4 || IS_G33(dev) ||
Chris Wilsone28f8712011-07-18 13:11:49 -07001269 tiling_mode == I915_TILING_NONE)
Daniel Vetter5e783302010-11-14 22:32:36 +01001270 return 4096;
1271
Chris Wilsone28f8712011-07-18 13:11:49 -07001272 /* Previous hardware however needs to be aligned to a power-of-two
1273 * tile height. The simplest method for determining this is to reuse
1274 * the power-of-tile object size.
Daniel Vetter5e783302010-11-14 22:32:36 +01001275 */
Chris Wilsone28f8712011-07-18 13:11:49 -07001276 return i915_gem_get_gtt_size(dev, size, tiling_mode);
Daniel Vetter5e783302010-11-14 22:32:36 +01001277}
1278
Jesse Barnesde151cf2008-11-12 10:03:55 -08001279int
Dave Airlieff72145b2011-02-07 12:16:14 +10001280i915_gem_mmap_gtt(struct drm_file *file,
1281 struct drm_device *dev,
1282 uint32_t handle,
1283 uint64_t *offset)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001284{
Chris Wilsonda761a62010-10-27 17:37:08 +01001285 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00001286 struct drm_i915_gem_object *obj;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001287 int ret;
1288
Chris Wilson76c1dec2010-09-25 11:22:51 +01001289 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001290 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001291 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001292
Dave Airlieff72145b2011-02-07 12:16:14 +10001293 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00001294 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001295 ret = -ENOENT;
1296 goto unlock;
1297 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001298
Chris Wilson05394f32010-11-08 19:18:58 +00001299 if (obj->base.size > dev_priv->mm.gtt_mappable_end) {
Chris Wilsonda761a62010-10-27 17:37:08 +01001300 ret = -E2BIG;
Eric Anholtff56b0b2011-10-31 23:16:21 -07001301 goto out;
Chris Wilsonda761a62010-10-27 17:37:08 +01001302 }
1303
Chris Wilson05394f32010-11-08 19:18:58 +00001304 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilsonab182822009-09-22 18:46:17 +01001305 DRM_ERROR("Attempting to mmap a purgeable buffer\n");
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001306 ret = -EINVAL;
1307 goto out;
Chris Wilsonab182822009-09-22 18:46:17 +01001308 }
1309
Chris Wilson05394f32010-11-08 19:18:58 +00001310 if (!obj->base.map_list.map) {
Rob Clarkb464e9a2011-08-10 08:09:08 -05001311 ret = drm_gem_create_mmap_offset(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001312 if (ret)
1313 goto out;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001314 }
1315
Dave Airlieff72145b2011-02-07 12:16:14 +10001316 *offset = (u64)obj->base.map_list.hash.key << PAGE_SHIFT;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001317
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001318out:
Chris Wilson05394f32010-11-08 19:18:58 +00001319 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001320unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001321 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001322 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001323}
1324
Dave Airlieff72145b2011-02-07 12:16:14 +10001325/**
1326 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1327 * @dev: DRM device
1328 * @data: GTT mapping ioctl data
1329 * @file: GEM object info
1330 *
1331 * Simply returns the fake offset to userspace so it can mmap it.
1332 * The mmap call will end up in drm_gem_mmap(), which will set things
1333 * up so we can get faults in the handler above.
1334 *
1335 * The fault handler will take care of binding the object into the GTT
1336 * (since it may have been evicted to make room for something), allocating
1337 * a fence register, and mapping the appropriate aperture address into
1338 * userspace.
1339 */
1340int
1341i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
1342 struct drm_file *file)
1343{
1344 struct drm_i915_gem_mmap_gtt *args = data;
1345
Dave Airlieff72145b2011-02-07 12:16:14 +10001346 return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
1347}
1348
Daniel Vetter225067e2012-08-20 10:23:20 +02001349/* Immediately discard the backing storage */
1350static void
1351i915_gem_object_truncate(struct drm_i915_gem_object *obj)
1352{
1353 struct inode *inode;
1354
1355 /* Our goal here is to return as much of the memory as
1356 * is possible back to the system as we are called from OOM.
1357 * To do this we must instruct the shmfs to drop all of its
1358 * backing pages, *now*.
1359 */
1360 inode = obj->base.filp->f_path.dentry->d_inode;
1361 shmem_truncate_range(inode, 0, (loff_t)-1);
1362
1363 if (obj->base.map_list.map)
1364 drm_gem_free_mmap_offset(&obj->base);
1365
1366 obj->madv = __I915_MADV_PURGED;
1367}
1368
1369static inline int
1370i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj)
1371{
1372 return obj->madv == I915_MADV_DONTNEED;
1373}
1374
Chris Wilson6c085a72012-08-20 11:40:46 +02001375static int
Daniel Vetter225067e2012-08-20 10:23:20 +02001376i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj)
1377{
1378 int page_count = obj->base.size / PAGE_SIZE;
Chris Wilson6c085a72012-08-20 11:40:46 +02001379 int ret, i;
Daniel Vetter225067e2012-08-20 10:23:20 +02001380
Chris Wilson6c085a72012-08-20 11:40:46 +02001381 if (obj->pages == NULL)
1382 return 0;
Daniel Vetter225067e2012-08-20 10:23:20 +02001383
Chris Wilson6c085a72012-08-20 11:40:46 +02001384 BUG_ON(obj->gtt_space);
Daniel Vetter225067e2012-08-20 10:23:20 +02001385 BUG_ON(obj->madv == __I915_MADV_PURGED);
1386
Chris Wilson6c085a72012-08-20 11:40:46 +02001387 ret = i915_gem_object_set_to_cpu_domain(obj, true);
1388 if (ret) {
1389 /* In the event of a disaster, abandon all caches and
1390 * hope for the best.
1391 */
1392 WARN_ON(ret != -EIO);
1393 i915_gem_clflush_object(obj);
1394 obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
1395 }
1396
Daniel Vetter225067e2012-08-20 10:23:20 +02001397 if (i915_gem_object_needs_bit17_swizzle(obj))
1398 i915_gem_object_save_bit_17_swizzle(obj);
1399
1400 if (obj->madv == I915_MADV_DONTNEED)
1401 obj->dirty = 0;
1402
1403 for (i = 0; i < page_count; i++) {
1404 if (obj->dirty)
1405 set_page_dirty(obj->pages[i]);
1406
1407 if (obj->madv == I915_MADV_WILLNEED)
1408 mark_page_accessed(obj->pages[i]);
1409
1410 page_cache_release(obj->pages[i]);
1411 }
1412 obj->dirty = 0;
1413
1414 drm_free_large(obj->pages);
1415 obj->pages = NULL;
Chris Wilson6c085a72012-08-20 11:40:46 +02001416
1417 list_del(&obj->gtt_list);
1418
1419 if (i915_gem_object_is_purgeable(obj))
1420 i915_gem_object_truncate(obj);
1421
1422 return 0;
1423}
1424
1425static long
1426i915_gem_purge(struct drm_i915_private *dev_priv, long target)
1427{
1428 struct drm_i915_gem_object *obj, *next;
1429 long count = 0;
1430
1431 list_for_each_entry_safe(obj, next,
1432 &dev_priv->mm.unbound_list,
1433 gtt_list) {
1434 if (i915_gem_object_is_purgeable(obj) &&
1435 i915_gem_object_put_pages_gtt(obj) == 0) {
1436 count += obj->base.size >> PAGE_SHIFT;
1437 if (count >= target)
1438 return count;
1439 }
1440 }
1441
1442 list_for_each_entry_safe(obj, next,
1443 &dev_priv->mm.inactive_list,
1444 mm_list) {
1445 if (i915_gem_object_is_purgeable(obj) &&
1446 i915_gem_object_unbind(obj) == 0 &&
1447 i915_gem_object_put_pages_gtt(obj) == 0) {
1448 count += obj->base.size >> PAGE_SHIFT;
1449 if (count >= target)
1450 return count;
1451 }
1452 }
1453
1454 return count;
1455}
1456
1457static void
1458i915_gem_shrink_all(struct drm_i915_private *dev_priv)
1459{
1460 struct drm_i915_gem_object *obj, *next;
1461
1462 i915_gem_evict_everything(dev_priv->dev);
1463
1464 list_for_each_entry_safe(obj, next, &dev_priv->mm.unbound_list, gtt_list)
1465 i915_gem_object_put_pages_gtt(obj);
Daniel Vetter225067e2012-08-20 10:23:20 +02001466}
1467
Daniel Vetter1286ff72012-05-10 15:25:09 +02001468int
Chris Wilson6c085a72012-08-20 11:40:46 +02001469i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
Chris Wilsone5281cc2010-10-28 13:45:36 +01001470{
Chris Wilson6c085a72012-08-20 11:40:46 +02001471 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001472 int page_count, i;
1473 struct address_space *mapping;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001474 struct page *page;
Chris Wilson6c085a72012-08-20 11:40:46 +02001475 gfp_t gfp;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001476
Daniel Vetter1286ff72012-05-10 15:25:09 +02001477 if (obj->pages || obj->sg_table)
1478 return 0;
1479
Chris Wilson6c085a72012-08-20 11:40:46 +02001480 /* Assert that the object is not currently in any GPU domain. As it
1481 * wasn't in the GTT, there shouldn't be any way it could have been in
1482 * a GPU cache
1483 */
1484 BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
1485 BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
1486
Chris Wilsone5281cc2010-10-28 13:45:36 +01001487 /* Get the list of pages out of our struct file. They'll be pinned
1488 * at this point until we release them.
1489 */
Chris Wilson05394f32010-11-08 19:18:58 +00001490 page_count = obj->base.size / PAGE_SIZE;
Chris Wilson05394f32010-11-08 19:18:58 +00001491 obj->pages = drm_malloc_ab(page_count, sizeof(struct page *));
1492 if (obj->pages == NULL)
Chris Wilsone5281cc2010-10-28 13:45:36 +01001493 return -ENOMEM;
1494
Chris Wilson6c085a72012-08-20 11:40:46 +02001495 /* Fail silently without starting the shrinker */
1496 mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
1497 gfp = mapping_gfp_mask(mapping);
1498 gfp |= __GFP_NORETRY | __GFP_NOWARN | __GFP_NO_KSWAPD;
1499 gfp &= ~(__GFP_IO | __GFP_WAIT);
Chris Wilsone5281cc2010-10-28 13:45:36 +01001500 for (i = 0; i < page_count; i++) {
Chris Wilson6c085a72012-08-20 11:40:46 +02001501 page = shmem_read_mapping_page_gfp(mapping, i, gfp);
1502 if (IS_ERR(page)) {
1503 i915_gem_purge(dev_priv, page_count);
1504 page = shmem_read_mapping_page_gfp(mapping, i, gfp);
1505 }
1506 if (IS_ERR(page)) {
1507 /* We've tried hard to allocate the memory by reaping
1508 * our own buffer, now let the real VM do its job and
1509 * go down in flames if truly OOM.
1510 */
1511 gfp &= ~(__GFP_NORETRY | __GFP_NOWARN | __GFP_NO_KSWAPD);
1512 gfp |= __GFP_IO | __GFP_WAIT;
1513
1514 i915_gem_shrink_all(dev_priv);
1515 page = shmem_read_mapping_page_gfp(mapping, i, gfp);
1516 if (IS_ERR(page))
1517 goto err_pages;
1518
1519 gfp |= __GFP_NORETRY | __GFP_NOWARN | __GFP_NO_KSWAPD;
1520 gfp &= ~(__GFP_IO | __GFP_WAIT);
1521 }
Chris Wilsone5281cc2010-10-28 13:45:36 +01001522
Chris Wilson05394f32010-11-08 19:18:58 +00001523 obj->pages[i] = page;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001524 }
1525
Daniel Vetter6dacfd22011-09-12 21:30:02 +02001526 if (i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilsone5281cc2010-10-28 13:45:36 +01001527 i915_gem_object_do_bit_17_swizzle(obj);
1528
Chris Wilson6c085a72012-08-20 11:40:46 +02001529 list_add_tail(&obj->gtt_list, &dev_priv->mm.unbound_list);
Chris Wilsone5281cc2010-10-28 13:45:36 +01001530 return 0;
1531
1532err_pages:
1533 while (i--)
Chris Wilson05394f32010-11-08 19:18:58 +00001534 page_cache_release(obj->pages[i]);
Chris Wilsone5281cc2010-10-28 13:45:36 +01001535
Chris Wilson05394f32010-11-08 19:18:58 +00001536 drm_free_large(obj->pages);
1537 obj->pages = NULL;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001538 return PTR_ERR(page);
1539}
1540
Chris Wilson54cf91d2010-11-25 18:00:26 +00001541void
Chris Wilson05394f32010-11-08 19:18:58 +00001542i915_gem_object_move_to_active(struct drm_i915_gem_object *obj,
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001543 struct intel_ring_buffer *ring,
1544 u32 seqno)
Eric Anholt673a3942008-07-30 12:06:12 -07001545{
Chris Wilson05394f32010-11-08 19:18:58 +00001546 struct drm_device *dev = obj->base.dev;
Chris Wilson69dc4982010-10-19 10:36:51 +01001547 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter617dbe22010-02-11 22:16:02 +01001548
Zou Nan hai852835f2010-05-21 09:08:56 +08001549 BUG_ON(ring == NULL);
Chris Wilson05394f32010-11-08 19:18:58 +00001550 obj->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001551
1552 /* Add a reference if we're newly entering the active list. */
Chris Wilson05394f32010-11-08 19:18:58 +00001553 if (!obj->active) {
1554 drm_gem_object_reference(&obj->base);
1555 obj->active = 1;
Eric Anholt673a3942008-07-30 12:06:12 -07001556 }
Daniel Vettere35a41d2010-02-11 22:13:59 +01001557
Eric Anholt673a3942008-07-30 12:06:12 -07001558 /* Move from whatever list we were on to the tail of execution. */
Chris Wilson05394f32010-11-08 19:18:58 +00001559 list_move_tail(&obj->mm_list, &dev_priv->mm.active_list);
1560 list_move_tail(&obj->ring_list, &ring->active_list);
Chris Wilsoncaea7472010-11-12 13:53:37 +00001561
Chris Wilson0201f1e2012-07-20 12:41:01 +01001562 obj->last_read_seqno = seqno;
Chris Wilson7dd49062012-03-21 10:48:18 +00001563
Chris Wilsoncaea7472010-11-12 13:53:37 +00001564 if (obj->fenced_gpu_access) {
Chris Wilsoncaea7472010-11-12 13:53:37 +00001565 obj->last_fenced_seqno = seqno;
Chris Wilsoncaea7472010-11-12 13:53:37 +00001566
Chris Wilson7dd49062012-03-21 10:48:18 +00001567 /* Bump MRU to take account of the delayed flush */
1568 if (obj->fence_reg != I915_FENCE_REG_NONE) {
1569 struct drm_i915_fence_reg *reg;
1570
1571 reg = &dev_priv->fence_regs[obj->fence_reg];
1572 list_move_tail(&reg->lru_list,
1573 &dev_priv->mm.fence_list);
1574 }
Chris Wilsoncaea7472010-11-12 13:53:37 +00001575 }
1576}
1577
1578static void
Chris Wilsoncaea7472010-11-12 13:53:37 +00001579i915_gem_object_move_to_inactive(struct drm_i915_gem_object *obj)
1580{
1581 struct drm_device *dev = obj->base.dev;
1582 struct drm_i915_private *dev_priv = dev->dev_private;
1583
Chris Wilson65ce3022012-07-20 12:41:02 +01001584 BUG_ON(obj->base.write_domain & ~I915_GEM_GPU_DOMAINS);
Chris Wilsoncaea7472010-11-12 13:53:37 +00001585 BUG_ON(!obj->active);
Chris Wilson65ce3022012-07-20 12:41:02 +01001586
Chris Wilsonf047e392012-07-21 12:31:41 +01001587 if (obj->pin_count) /* are we a framebuffer? */
1588 intel_mark_fb_idle(obj);
1589
1590 list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
1591
Chris Wilson65ce3022012-07-20 12:41:02 +01001592 list_del_init(&obj->ring_list);
Chris Wilsoncaea7472010-11-12 13:53:37 +00001593 obj->ring = NULL;
1594
Chris Wilson65ce3022012-07-20 12:41:02 +01001595 obj->last_read_seqno = 0;
1596 obj->last_write_seqno = 0;
1597 obj->base.write_domain = 0;
1598
1599 obj->last_fenced_seqno = 0;
Chris Wilsoncaea7472010-11-12 13:53:37 +00001600 obj->fenced_gpu_access = false;
Chris Wilsoncaea7472010-11-12 13:53:37 +00001601
1602 obj->active = 0;
1603 drm_gem_object_unreference(&obj->base);
1604
1605 WARN_ON(i915_verify_lists(dev));
Eric Anholtce44b0e2008-11-06 16:00:31 -08001606}
Eric Anholt673a3942008-07-30 12:06:12 -07001607
Daniel Vetter53d227f2012-01-25 16:32:49 +01001608static u32
1609i915_gem_get_seqno(struct drm_device *dev)
1610{
1611 drm_i915_private_t *dev_priv = dev->dev_private;
1612 u32 seqno = dev_priv->next_seqno;
1613
1614 /* reserve 0 for non-seqno */
1615 if (++dev_priv->next_seqno == 0)
1616 dev_priv->next_seqno = 1;
1617
1618 return seqno;
1619}
1620
1621u32
1622i915_gem_next_request_seqno(struct intel_ring_buffer *ring)
1623{
1624 if (ring->outstanding_lazy_request == 0)
1625 ring->outstanding_lazy_request = i915_gem_get_seqno(ring->dev);
1626
1627 return ring->outstanding_lazy_request;
1628}
1629
Chris Wilson3cce4692010-10-27 16:11:02 +01001630int
Chris Wilsondb53a302011-02-03 11:57:46 +00001631i915_add_request(struct intel_ring_buffer *ring,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001632 struct drm_file *file,
Chris Wilsondb53a302011-02-03 11:57:46 +00001633 struct drm_i915_gem_request *request)
Eric Anholt673a3942008-07-30 12:06:12 -07001634{
Chris Wilsondb53a302011-02-03 11:57:46 +00001635 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07001636 uint32_t seqno;
Chris Wilsona71d8d92012-02-15 11:25:36 +00001637 u32 request_ring_position;
Eric Anholt673a3942008-07-30 12:06:12 -07001638 int was_empty;
Chris Wilson3cce4692010-10-27 16:11:02 +01001639 int ret;
1640
Daniel Vettercc889e02012-06-13 20:45:19 +02001641 /*
1642 * Emit any outstanding flushes - execbuf can fail to emit the flush
1643 * after having emitted the batchbuffer command. Hence we need to fix
1644 * things up similar to emitting the lazy request. The difference here
1645 * is that the flush _must_ happen before the next request, no matter
1646 * what.
1647 */
Chris Wilsona7b97612012-07-20 12:41:08 +01001648 ret = intel_ring_flush_all_caches(ring);
1649 if (ret)
1650 return ret;
Daniel Vettercc889e02012-06-13 20:45:19 +02001651
Chris Wilson3bb73ab2012-07-20 12:40:59 +01001652 if (request == NULL) {
1653 request = kmalloc(sizeof(*request), GFP_KERNEL);
1654 if (request == NULL)
1655 return -ENOMEM;
1656 }
1657
Daniel Vetter53d227f2012-01-25 16:32:49 +01001658 seqno = i915_gem_next_request_seqno(ring);
Eric Anholt673a3942008-07-30 12:06:12 -07001659
Chris Wilsona71d8d92012-02-15 11:25:36 +00001660 /* Record the position of the start of the request so that
1661 * should we detect the updated seqno part-way through the
1662 * GPU processing the request, we never over-estimate the
1663 * position of the head.
1664 */
1665 request_ring_position = intel_ring_get_tail(ring);
1666
Chris Wilson3cce4692010-10-27 16:11:02 +01001667 ret = ring->add_request(ring, &seqno);
Chris Wilson3bb73ab2012-07-20 12:40:59 +01001668 if (ret) {
1669 kfree(request);
1670 return ret;
1671 }
Eric Anholt673a3942008-07-30 12:06:12 -07001672
Chris Wilsondb53a302011-02-03 11:57:46 +00001673 trace_i915_gem_request_add(ring, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07001674
1675 request->seqno = seqno;
Zou Nan hai852835f2010-05-21 09:08:56 +08001676 request->ring = ring;
Chris Wilsona71d8d92012-02-15 11:25:36 +00001677 request->tail = request_ring_position;
Eric Anholt673a3942008-07-30 12:06:12 -07001678 request->emitted_jiffies = jiffies;
Zou Nan hai852835f2010-05-21 09:08:56 +08001679 was_empty = list_empty(&ring->request_list);
1680 list_add_tail(&request->list, &ring->request_list);
Chris Wilson3bb73ab2012-07-20 12:40:59 +01001681 request->file_priv = NULL;
Zou Nan hai852835f2010-05-21 09:08:56 +08001682
Chris Wilsondb53a302011-02-03 11:57:46 +00001683 if (file) {
1684 struct drm_i915_file_private *file_priv = file->driver_priv;
1685
Chris Wilson1c255952010-09-26 11:03:27 +01001686 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001687 request->file_priv = file_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00001688 list_add_tail(&request->client_list,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001689 &file_priv->mm.request_list);
Chris Wilson1c255952010-09-26 11:03:27 +01001690 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00001691 }
Eric Anholt673a3942008-07-30 12:06:12 -07001692
Daniel Vetter5391d0c2012-01-25 14:03:57 +01001693 ring->outstanding_lazy_request = 0;
Chris Wilsondb53a302011-02-03 11:57:46 +00001694
Ben Gamarif65d9422009-09-14 17:48:44 -04001695 if (!dev_priv->mm.suspended) {
Ben Widawsky3e0dc6b2011-06-29 10:26:42 -07001696 if (i915_enable_hangcheck) {
1697 mod_timer(&dev_priv->hangcheck_timer,
1698 jiffies +
1699 msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
1700 }
Chris Wilsonf047e392012-07-21 12:31:41 +01001701 if (was_empty) {
Chris Wilsonb3b079d2010-09-13 23:44:34 +01001702 queue_delayed_work(dev_priv->wq,
1703 &dev_priv->mm.retire_work, HZ);
Chris Wilsonf047e392012-07-21 12:31:41 +01001704 intel_mark_busy(dev_priv->dev);
1705 }
Ben Gamarif65d9422009-09-14 17:48:44 -04001706 }
Daniel Vettercc889e02012-06-13 20:45:19 +02001707
Chris Wilson3cce4692010-10-27 16:11:02 +01001708 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001709}
1710
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001711static inline void
1712i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
Eric Anholt673a3942008-07-30 12:06:12 -07001713{
Chris Wilson1c255952010-09-26 11:03:27 +01001714 struct drm_i915_file_private *file_priv = request->file_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07001715
Chris Wilson1c255952010-09-26 11:03:27 +01001716 if (!file_priv)
1717 return;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001718
Chris Wilson1c255952010-09-26 11:03:27 +01001719 spin_lock(&file_priv->mm.lock);
Herton Ronaldo Krzesinski09bfa512011-03-17 13:45:12 +00001720 if (request->file_priv) {
1721 list_del(&request->client_list);
1722 request->file_priv = NULL;
1723 }
Chris Wilson1c255952010-09-26 11:03:27 +01001724 spin_unlock(&file_priv->mm.lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001725}
1726
Chris Wilsondfaae392010-09-22 10:31:52 +01001727static void i915_gem_reset_ring_lists(struct drm_i915_private *dev_priv,
1728 struct intel_ring_buffer *ring)
Chris Wilson9375e442010-09-19 12:21:28 +01001729{
Chris Wilsondfaae392010-09-22 10:31:52 +01001730 while (!list_empty(&ring->request_list)) {
1731 struct drm_i915_gem_request *request;
Chris Wilson9375e442010-09-19 12:21:28 +01001732
Chris Wilsondfaae392010-09-22 10:31:52 +01001733 request = list_first_entry(&ring->request_list,
1734 struct drm_i915_gem_request,
1735 list);
1736
1737 list_del(&request->list);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001738 i915_gem_request_remove_from_client(request);
Chris Wilsondfaae392010-09-22 10:31:52 +01001739 kfree(request);
1740 }
1741
1742 while (!list_empty(&ring->active_list)) {
Chris Wilson05394f32010-11-08 19:18:58 +00001743 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001744
Chris Wilson05394f32010-11-08 19:18:58 +00001745 obj = list_first_entry(&ring->active_list,
1746 struct drm_i915_gem_object,
1747 ring_list);
Eric Anholt673a3942008-07-30 12:06:12 -07001748
Chris Wilson05394f32010-11-08 19:18:58 +00001749 i915_gem_object_move_to_inactive(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001750 }
Eric Anholt673a3942008-07-30 12:06:12 -07001751}
1752
Chris Wilson312817a2010-11-22 11:50:11 +00001753static void i915_gem_reset_fences(struct drm_device *dev)
1754{
1755 struct drm_i915_private *dev_priv = dev->dev_private;
1756 int i;
1757
Daniel Vetter4b9de732011-10-09 21:52:02 +02001758 for (i = 0; i < dev_priv->num_fence_regs; i++) {
Chris Wilson312817a2010-11-22 11:50:11 +00001759 struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
Chris Wilson7d2cb392010-11-27 17:38:29 +00001760
Chris Wilsonada726c2012-04-17 15:31:32 +01001761 i915_gem_write_fence(dev, i, NULL);
Chris Wilson7d2cb392010-11-27 17:38:29 +00001762
Chris Wilsonada726c2012-04-17 15:31:32 +01001763 if (reg->obj)
1764 i915_gem_object_fence_lost(reg->obj);
Chris Wilson7d2cb392010-11-27 17:38:29 +00001765
Chris Wilsonada726c2012-04-17 15:31:32 +01001766 reg->pin_count = 0;
1767 reg->obj = NULL;
1768 INIT_LIST_HEAD(&reg->lru_list);
Chris Wilson312817a2010-11-22 11:50:11 +00001769 }
Chris Wilsonada726c2012-04-17 15:31:32 +01001770
1771 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Chris Wilson312817a2010-11-22 11:50:11 +00001772}
1773
Chris Wilson069efc12010-09-30 16:53:18 +01001774void i915_gem_reset(struct drm_device *dev)
Eric Anholt673a3942008-07-30 12:06:12 -07001775{
Chris Wilsondfaae392010-09-22 10:31:52 +01001776 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00001777 struct drm_i915_gem_object *obj;
Chris Wilsonb4519512012-05-11 14:29:30 +01001778 struct intel_ring_buffer *ring;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001779 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07001780
Chris Wilsonb4519512012-05-11 14:29:30 +01001781 for_each_ring(ring, dev_priv, i)
1782 i915_gem_reset_ring_lists(dev_priv, ring);
Chris Wilsondfaae392010-09-22 10:31:52 +01001783
Chris Wilsondfaae392010-09-22 10:31:52 +01001784 /* Move everything out of the GPU domains to ensure we do any
1785 * necessary invalidation upon reuse.
1786 */
Chris Wilson05394f32010-11-08 19:18:58 +00001787 list_for_each_entry(obj,
Chris Wilson77f01232010-09-19 12:31:36 +01001788 &dev_priv->mm.inactive_list,
Chris Wilson69dc4982010-10-19 10:36:51 +01001789 mm_list)
Chris Wilson77f01232010-09-19 12:31:36 +01001790 {
Chris Wilson05394f32010-11-08 19:18:58 +00001791 obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
Chris Wilson77f01232010-09-19 12:31:36 +01001792 }
Chris Wilson069efc12010-09-30 16:53:18 +01001793
Chris Wilson6c085a72012-08-20 11:40:46 +02001794
Chris Wilson069efc12010-09-30 16:53:18 +01001795 /* The fence registers are invalidated so clear them out */
Chris Wilson312817a2010-11-22 11:50:11 +00001796 i915_gem_reset_fences(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07001797}
1798
1799/**
1800 * This function clears the request list as sequence numbers are passed.
1801 */
Chris Wilsona71d8d92012-02-15 11:25:36 +00001802void
Chris Wilsondb53a302011-02-03 11:57:46 +00001803i915_gem_retire_requests_ring(struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001804{
Eric Anholt673a3942008-07-30 12:06:12 -07001805 uint32_t seqno;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001806 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07001807
Chris Wilsondb53a302011-02-03 11:57:46 +00001808 if (list_empty(&ring->request_list))
Karsten Wiese6c0594a2009-02-23 15:07:57 +01001809 return;
1810
Chris Wilsondb53a302011-02-03 11:57:46 +00001811 WARN_ON(i915_verify_lists(ring->dev));
Eric Anholt673a3942008-07-30 12:06:12 -07001812
Chris Wilsonb2eadbc2012-08-09 10:58:30 +01001813 seqno = ring->get_seqno(ring, true);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001814
Chris Wilson076e2c02011-01-21 10:07:18 +00001815 for (i = 0; i < ARRAY_SIZE(ring->sync_seqno); i++)
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001816 if (seqno >= ring->sync_seqno[i])
1817 ring->sync_seqno[i] = 0;
1818
Zou Nan hai852835f2010-05-21 09:08:56 +08001819 while (!list_empty(&ring->request_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001820 struct drm_i915_gem_request *request;
Eric Anholt673a3942008-07-30 12:06:12 -07001821
Zou Nan hai852835f2010-05-21 09:08:56 +08001822 request = list_first_entry(&ring->request_list,
Eric Anholt673a3942008-07-30 12:06:12 -07001823 struct drm_i915_gem_request,
1824 list);
Eric Anholt673a3942008-07-30 12:06:12 -07001825
Chris Wilsondfaae392010-09-22 10:31:52 +01001826 if (!i915_seqno_passed(seqno, request->seqno))
Eric Anholt673a3942008-07-30 12:06:12 -07001827 break;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001828
Chris Wilsondb53a302011-02-03 11:57:46 +00001829 trace_i915_gem_request_retire(ring, request->seqno);
Chris Wilsona71d8d92012-02-15 11:25:36 +00001830 /* We know the GPU must have read the request to have
1831 * sent us the seqno + interrupt, so use the position
1832 * of tail of the request to update the last known position
1833 * of the GPU head.
1834 */
1835 ring->last_retired_head = request->tail;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001836
1837 list_del(&request->list);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001838 i915_gem_request_remove_from_client(request);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001839 kfree(request);
1840 }
1841
1842 /* Move any buffers on the active list that are no longer referenced
1843 * by the ringbuffer to the flushing/inactive lists as appropriate.
1844 */
1845 while (!list_empty(&ring->active_list)) {
Chris Wilson05394f32010-11-08 19:18:58 +00001846 struct drm_i915_gem_object *obj;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001847
Akshay Joshi0206e352011-08-16 15:34:10 -04001848 obj = list_first_entry(&ring->active_list,
Chris Wilson05394f32010-11-08 19:18:58 +00001849 struct drm_i915_gem_object,
1850 ring_list);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001851
Chris Wilson0201f1e2012-07-20 12:41:01 +01001852 if (!i915_seqno_passed(seqno, obj->last_read_seqno))
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001853 break;
1854
Chris Wilson65ce3022012-07-20 12:41:02 +01001855 i915_gem_object_move_to_inactive(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001856 }
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001857
Chris Wilsondb53a302011-02-03 11:57:46 +00001858 if (unlikely(ring->trace_irq_seqno &&
1859 i915_seqno_passed(seqno, ring->trace_irq_seqno))) {
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001860 ring->irq_put(ring);
Chris Wilsondb53a302011-02-03 11:57:46 +00001861 ring->trace_irq_seqno = 0;
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001862 }
Chris Wilson23bc5982010-09-29 16:10:57 +01001863
Chris Wilsondb53a302011-02-03 11:57:46 +00001864 WARN_ON(i915_verify_lists(ring->dev));
Eric Anholt673a3942008-07-30 12:06:12 -07001865}
1866
1867void
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001868i915_gem_retire_requests(struct drm_device *dev)
1869{
1870 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonb4519512012-05-11 14:29:30 +01001871 struct intel_ring_buffer *ring;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001872 int i;
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001873
Chris Wilsonb4519512012-05-11 14:29:30 +01001874 for_each_ring(ring, dev_priv, i)
1875 i915_gem_retire_requests_ring(ring);
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001876}
1877
Daniel Vetter75ef9da2010-08-21 00:25:16 +02001878static void
Eric Anholt673a3942008-07-30 12:06:12 -07001879i915_gem_retire_work_handler(struct work_struct *work)
1880{
1881 drm_i915_private_t *dev_priv;
1882 struct drm_device *dev;
Chris Wilsonb4519512012-05-11 14:29:30 +01001883 struct intel_ring_buffer *ring;
Chris Wilson0a587052011-01-09 21:05:44 +00001884 bool idle;
1885 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07001886
1887 dev_priv = container_of(work, drm_i915_private_t,
1888 mm.retire_work.work);
1889 dev = dev_priv->dev;
1890
Chris Wilson891b48c2010-09-29 12:26:37 +01001891 /* Come back later if the device is busy... */
1892 if (!mutex_trylock(&dev->struct_mutex)) {
1893 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
1894 return;
1895 }
1896
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001897 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08001898
Chris Wilson0a587052011-01-09 21:05:44 +00001899 /* Send a periodic flush down the ring so we don't hold onto GEM
1900 * objects indefinitely.
1901 */
1902 idle = true;
Chris Wilsonb4519512012-05-11 14:29:30 +01001903 for_each_ring(ring, dev_priv, i) {
Chris Wilson3bb73ab2012-07-20 12:40:59 +01001904 if (ring->gpu_caches_dirty)
1905 i915_add_request(ring, NULL, NULL);
Chris Wilson0a587052011-01-09 21:05:44 +00001906
1907 idle &= list_empty(&ring->request_list);
1908 }
1909
1910 if (!dev_priv->mm.suspended && !idle)
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001911 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
Chris Wilsonf047e392012-07-21 12:31:41 +01001912 if (idle)
1913 intel_mark_idle(dev);
Chris Wilson0a587052011-01-09 21:05:44 +00001914
Eric Anholt673a3942008-07-30 12:06:12 -07001915 mutex_unlock(&dev->struct_mutex);
1916}
1917
Daniel Vetterd6b2c792012-07-04 22:54:13 +02001918int
1919i915_gem_check_wedge(struct drm_i915_private *dev_priv,
1920 bool interruptible)
Ben Widawskyb4aca012012-04-25 20:50:12 -07001921{
Ben Widawskyb4aca012012-04-25 20:50:12 -07001922 if (atomic_read(&dev_priv->mm.wedged)) {
1923 struct completion *x = &dev_priv->error_completion;
1924 bool recovery_complete;
1925 unsigned long flags;
1926
1927 /* Give the error handler a chance to run. */
1928 spin_lock_irqsave(&x->wait.lock, flags);
1929 recovery_complete = x->done > 0;
1930 spin_unlock_irqrestore(&x->wait.lock, flags);
1931
Daniel Vetterd6b2c792012-07-04 22:54:13 +02001932 /* Non-interruptible callers can't handle -EAGAIN, hence return
1933 * -EIO unconditionally for these. */
1934 if (!interruptible)
1935 return -EIO;
1936
1937 /* Recovery complete, but still wedged means reset failure. */
1938 if (recovery_complete)
1939 return -EIO;
1940
1941 return -EAGAIN;
Ben Widawskyb4aca012012-04-25 20:50:12 -07001942 }
1943
1944 return 0;
1945}
1946
1947/*
1948 * Compare seqno against outstanding lazy request. Emit a request if they are
1949 * equal.
1950 */
1951static int
1952i915_gem_check_olr(struct intel_ring_buffer *ring, u32 seqno)
1953{
Chris Wilson3bb73ab2012-07-20 12:40:59 +01001954 int ret;
Ben Widawskyb4aca012012-04-25 20:50:12 -07001955
1956 BUG_ON(!mutex_is_locked(&ring->dev->struct_mutex));
1957
Chris Wilson3bb73ab2012-07-20 12:40:59 +01001958 ret = 0;
1959 if (seqno == ring->outstanding_lazy_request)
1960 ret = i915_add_request(ring, NULL, NULL);
Ben Widawskyb4aca012012-04-25 20:50:12 -07001961
1962 return ret;
1963}
1964
Ben Widawsky5c81fe852012-05-24 15:03:08 -07001965/**
1966 * __wait_seqno - wait until execution of seqno has finished
1967 * @ring: the ring expected to report seqno
1968 * @seqno: duh!
1969 * @interruptible: do an interruptible wait (normally yes)
1970 * @timeout: in - how long to wait (NULL forever); out - how much time remaining
1971 *
1972 * Returns 0 if the seqno was found within the alloted time. Else returns the
1973 * errno with remaining time filled in timeout argument.
1974 */
Ben Widawsky604dd3e2012-04-26 16:03:03 -07001975static int __wait_seqno(struct intel_ring_buffer *ring, u32 seqno,
Ben Widawsky5c81fe852012-05-24 15:03:08 -07001976 bool interruptible, struct timespec *timeout)
Ben Widawsky604dd3e2012-04-26 16:03:03 -07001977{
1978 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Ben Widawsky5c81fe852012-05-24 15:03:08 -07001979 struct timespec before, now, wait_time={1,0};
1980 unsigned long timeout_jiffies;
1981 long end;
1982 bool wait_forever = true;
Daniel Vetterd6b2c792012-07-04 22:54:13 +02001983 int ret;
Ben Widawsky604dd3e2012-04-26 16:03:03 -07001984
Chris Wilsonb2eadbc2012-08-09 10:58:30 +01001985 if (i915_seqno_passed(ring->get_seqno(ring, true), seqno))
Ben Widawsky604dd3e2012-04-26 16:03:03 -07001986 return 0;
1987
1988 trace_i915_gem_request_wait_begin(ring, seqno);
Ben Widawsky5c81fe852012-05-24 15:03:08 -07001989
1990 if (timeout != NULL) {
1991 wait_time = *timeout;
1992 wait_forever = false;
1993 }
1994
1995 timeout_jiffies = timespec_to_jiffies(&wait_time);
1996
Ben Widawsky604dd3e2012-04-26 16:03:03 -07001997 if (WARN_ON(!ring->irq_get(ring)))
1998 return -ENODEV;
1999
Ben Widawsky5c81fe852012-05-24 15:03:08 -07002000 /* Record current time in case interrupted by signal, or wedged * */
2001 getrawmonotonic(&before);
2002
Ben Widawsky604dd3e2012-04-26 16:03:03 -07002003#define EXIT_COND \
Chris Wilsonb2eadbc2012-08-09 10:58:30 +01002004 (i915_seqno_passed(ring->get_seqno(ring, false), seqno) || \
Ben Widawsky604dd3e2012-04-26 16:03:03 -07002005 atomic_read(&dev_priv->mm.wedged))
Ben Widawsky5c81fe852012-05-24 15:03:08 -07002006 do {
2007 if (interruptible)
2008 end = wait_event_interruptible_timeout(ring->irq_queue,
2009 EXIT_COND,
2010 timeout_jiffies);
2011 else
2012 end = wait_event_timeout(ring->irq_queue, EXIT_COND,
2013 timeout_jiffies);
Ben Widawsky604dd3e2012-04-26 16:03:03 -07002014
Daniel Vetterd6b2c792012-07-04 22:54:13 +02002015 ret = i915_gem_check_wedge(dev_priv, interruptible);
2016 if (ret)
2017 end = ret;
Ben Widawsky5c81fe852012-05-24 15:03:08 -07002018 } while (end == 0 && wait_forever);
2019
2020 getrawmonotonic(&now);
Ben Widawsky604dd3e2012-04-26 16:03:03 -07002021
2022 ring->irq_put(ring);
2023 trace_i915_gem_request_wait_end(ring, seqno);
2024#undef EXIT_COND
2025
Ben Widawsky5c81fe852012-05-24 15:03:08 -07002026 if (timeout) {
2027 struct timespec sleep_time = timespec_sub(now, before);
2028 *timeout = timespec_sub(*timeout, sleep_time);
2029 }
2030
2031 switch (end) {
Chris Wilsoneeef9b32012-07-16 13:05:34 +01002032 case -EIO:
Ben Widawsky5c81fe852012-05-24 15:03:08 -07002033 case -EAGAIN: /* Wedged */
2034 case -ERESTARTSYS: /* Signal */
2035 return (int)end;
2036 case 0: /* Timeout */
2037 if (timeout)
2038 set_normalized_timespec(timeout, 0, 0);
2039 return -ETIME;
2040 default: /* Completed */
2041 WARN_ON(end < 0); /* We're not aware of other errors */
2042 return 0;
2043 }
Ben Widawsky604dd3e2012-04-26 16:03:03 -07002044}
2045
Chris Wilsondb53a302011-02-03 11:57:46 +00002046/**
2047 * Waits for a sequence number to be signaled, and cleans up the
2048 * request and object lists appropriately for that event.
2049 */
Daniel Vetter5a5a0c62009-09-15 22:57:36 +02002050int
Ben Widawsky199b2bc2012-05-24 15:03:11 -07002051i915_wait_seqno(struct intel_ring_buffer *ring, uint32_t seqno)
Eric Anholt673a3942008-07-30 12:06:12 -07002052{
Chris Wilsondb53a302011-02-03 11:57:46 +00002053 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07002054 int ret = 0;
2055
2056 BUG_ON(seqno == 0);
2057
Daniel Vetterd6b2c792012-07-04 22:54:13 +02002058 ret = i915_gem_check_wedge(dev_priv, dev_priv->mm.interruptible);
Ben Widawskyb4aca012012-04-25 20:50:12 -07002059 if (ret)
2060 return ret;
Chris Wilsond9bc7e92011-02-07 13:09:31 +00002061
Ben Widawskyb4aca012012-04-25 20:50:12 -07002062 ret = i915_gem_check_olr(ring, seqno);
2063 if (ret)
2064 return ret;
Daniel Vettere35a41d2010-02-11 22:13:59 +01002065
Ben Widawsky5c81fe852012-05-24 15:03:08 -07002066 ret = __wait_seqno(ring, seqno, dev_priv->mm.interruptible, NULL);
Eric Anholt673a3942008-07-30 12:06:12 -07002067
Eric Anholt673a3942008-07-30 12:06:12 -07002068 return ret;
2069}
2070
Daniel Vetter48764bf2009-09-15 22:57:32 +02002071/**
Eric Anholt673a3942008-07-30 12:06:12 -07002072 * Ensures that all rendering to the object has completed and the object is
2073 * safe to unbind from the GTT or access from the CPU.
2074 */
Chris Wilson0201f1e2012-07-20 12:41:01 +01002075static __must_check int
2076i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj,
2077 bool readonly)
Eric Anholt673a3942008-07-30 12:06:12 -07002078{
Chris Wilson0201f1e2012-07-20 12:41:01 +01002079 u32 seqno;
Eric Anholt673a3942008-07-30 12:06:12 -07002080 int ret;
2081
Eric Anholt673a3942008-07-30 12:06:12 -07002082 /* If there is rendering queued on the buffer being evicted, wait for
2083 * it.
2084 */
Chris Wilson0201f1e2012-07-20 12:41:01 +01002085 if (readonly)
2086 seqno = obj->last_write_seqno;
2087 else
2088 seqno = obj->last_read_seqno;
2089 if (seqno == 0)
2090 return 0;
2091
2092 ret = i915_wait_seqno(obj->ring, seqno);
2093 if (ret)
2094 return ret;
2095
2096 /* Manually manage the write flush as we may have not yet retired
2097 * the buffer.
2098 */
2099 if (obj->last_write_seqno &&
2100 i915_seqno_passed(seqno, obj->last_write_seqno)) {
2101 obj->last_write_seqno = 0;
2102 obj->base.write_domain &= ~I915_GEM_GPU_DOMAINS;
Eric Anholt673a3942008-07-30 12:06:12 -07002103 }
2104
Chris Wilson0201f1e2012-07-20 12:41:01 +01002105 i915_gem_retire_requests_ring(obj->ring);
Eric Anholt673a3942008-07-30 12:06:12 -07002106 return 0;
2107}
2108
Ben Widawsky5816d642012-04-11 11:18:19 -07002109/**
Daniel Vetter30dfebf2012-06-01 15:21:23 +02002110 * Ensures that an object will eventually get non-busy by flushing any required
2111 * write domains, emitting any outstanding lazy request and retiring and
2112 * completed requests.
2113 */
2114static int
2115i915_gem_object_flush_active(struct drm_i915_gem_object *obj)
2116{
2117 int ret;
2118
2119 if (obj->active) {
Chris Wilson0201f1e2012-07-20 12:41:01 +01002120 ret = i915_gem_check_olr(obj->ring, obj->last_read_seqno);
Daniel Vetter30dfebf2012-06-01 15:21:23 +02002121 if (ret)
2122 return ret;
Chris Wilson0201f1e2012-07-20 12:41:01 +01002123
Daniel Vetter30dfebf2012-06-01 15:21:23 +02002124 i915_gem_retire_requests_ring(obj->ring);
2125 }
2126
2127 return 0;
2128}
2129
2130/**
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002131 * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
2132 * @DRM_IOCTL_ARGS: standard ioctl arguments
2133 *
2134 * Returns 0 if successful, else an error is returned with the remaining time in
2135 * the timeout parameter.
2136 * -ETIME: object is still busy after timeout
2137 * -ERESTARTSYS: signal interrupted the wait
2138 * -ENONENT: object doesn't exist
2139 * Also possible, but rare:
2140 * -EAGAIN: GPU wedged
2141 * -ENOMEM: damn
2142 * -ENODEV: Internal IRQ fail
2143 * -E?: The add request failed
2144 *
2145 * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
2146 * non-zero timeout parameter the wait ioctl will wait for the given number of
2147 * nanoseconds on an object becoming unbusy. Since the wait itself does so
2148 * without holding struct_mutex the object may become re-busied before this
2149 * function completes. A similar but shorter * race condition exists in the busy
2150 * ioctl
2151 */
2152int
2153i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
2154{
2155 struct drm_i915_gem_wait *args = data;
2156 struct drm_i915_gem_object *obj;
2157 struct intel_ring_buffer *ring = NULL;
Ben Widawskyeac1f142012-06-05 15:24:24 -07002158 struct timespec timeout_stack, *timeout = NULL;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002159 u32 seqno = 0;
2160 int ret = 0;
2161
Ben Widawskyeac1f142012-06-05 15:24:24 -07002162 if (args->timeout_ns >= 0) {
2163 timeout_stack = ns_to_timespec(args->timeout_ns);
2164 timeout = &timeout_stack;
2165 }
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002166
2167 ret = i915_mutex_lock_interruptible(dev);
2168 if (ret)
2169 return ret;
2170
2171 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->bo_handle));
2172 if (&obj->base == NULL) {
2173 mutex_unlock(&dev->struct_mutex);
2174 return -ENOENT;
2175 }
2176
Daniel Vetter30dfebf2012-06-01 15:21:23 +02002177 /* Need to make sure the object gets inactive eventually. */
2178 ret = i915_gem_object_flush_active(obj);
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002179 if (ret)
2180 goto out;
2181
2182 if (obj->active) {
Chris Wilson0201f1e2012-07-20 12:41:01 +01002183 seqno = obj->last_read_seqno;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002184 ring = obj->ring;
2185 }
2186
2187 if (seqno == 0)
2188 goto out;
2189
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002190 /* Do this after OLR check to make sure we make forward progress polling
2191 * on this IOCTL with a 0 timeout (like busy ioctl)
2192 */
2193 if (!args->timeout_ns) {
2194 ret = -ETIME;
2195 goto out;
2196 }
2197
2198 drm_gem_object_unreference(&obj->base);
2199 mutex_unlock(&dev->struct_mutex);
2200
Ben Widawskyeac1f142012-06-05 15:24:24 -07002201 ret = __wait_seqno(ring, seqno, true, timeout);
2202 if (timeout) {
2203 WARN_ON(!timespec_valid(timeout));
2204 args->timeout_ns = timespec_to_ns(timeout);
2205 }
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002206 return ret;
2207
2208out:
2209 drm_gem_object_unreference(&obj->base);
2210 mutex_unlock(&dev->struct_mutex);
2211 return ret;
2212}
2213
2214/**
Ben Widawsky5816d642012-04-11 11:18:19 -07002215 * i915_gem_object_sync - sync an object to a ring.
2216 *
2217 * @obj: object which may be in use on another ring.
2218 * @to: ring we wish to use the object on. May be NULL.
2219 *
2220 * This code is meant to abstract object synchronization with the GPU.
2221 * Calling with NULL implies synchronizing the object with the CPU
2222 * rather than a particular GPU ring.
2223 *
2224 * Returns 0 if successful, else propagates up the lower layer error.
2225 */
Ben Widawsky2911a352012-04-05 14:47:36 -07002226int
2227i915_gem_object_sync(struct drm_i915_gem_object *obj,
2228 struct intel_ring_buffer *to)
2229{
2230 struct intel_ring_buffer *from = obj->ring;
2231 u32 seqno;
2232 int ret, idx;
2233
2234 if (from == NULL || to == from)
2235 return 0;
2236
Ben Widawsky5816d642012-04-11 11:18:19 -07002237 if (to == NULL || !i915_semaphore_is_enabled(obj->base.dev))
Chris Wilson0201f1e2012-07-20 12:41:01 +01002238 return i915_gem_object_wait_rendering(obj, false);
Ben Widawsky2911a352012-04-05 14:47:36 -07002239
2240 idx = intel_ring_sync_index(from, to);
2241
Chris Wilson0201f1e2012-07-20 12:41:01 +01002242 seqno = obj->last_read_seqno;
Ben Widawsky2911a352012-04-05 14:47:36 -07002243 if (seqno <= from->sync_seqno[idx])
2244 return 0;
2245
Ben Widawskyb4aca012012-04-25 20:50:12 -07002246 ret = i915_gem_check_olr(obj->ring, seqno);
2247 if (ret)
2248 return ret;
Ben Widawsky2911a352012-04-05 14:47:36 -07002249
Ben Widawsky1500f7e2012-04-11 11:18:21 -07002250 ret = to->sync_to(to, from, seqno);
Ben Widawskye3a5a222012-04-11 11:18:20 -07002251 if (!ret)
2252 from->sync_seqno[idx] = seqno;
Ben Widawsky2911a352012-04-05 14:47:36 -07002253
Ben Widawskye3a5a222012-04-11 11:18:20 -07002254 return ret;
Ben Widawsky2911a352012-04-05 14:47:36 -07002255}
2256
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01002257static void i915_gem_object_finish_gtt(struct drm_i915_gem_object *obj)
2258{
2259 u32 old_write_domain, old_read_domains;
2260
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01002261 /* Act a barrier for all accesses through the GTT */
2262 mb();
2263
2264 /* Force a pagefault for domain tracking on next user access */
2265 i915_gem_release_mmap(obj);
2266
Keith Packardb97c3d92011-06-24 21:02:59 -07002267 if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
2268 return;
2269
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01002270 old_read_domains = obj->base.read_domains;
2271 old_write_domain = obj->base.write_domain;
2272
2273 obj->base.read_domains &= ~I915_GEM_DOMAIN_GTT;
2274 obj->base.write_domain &= ~I915_GEM_DOMAIN_GTT;
2275
2276 trace_i915_gem_object_change_domain(obj,
2277 old_read_domains,
2278 old_write_domain);
2279}
2280
Eric Anholt673a3942008-07-30 12:06:12 -07002281/**
2282 * Unbinds an object from the GTT aperture.
2283 */
Jesse Barnes0f973f22009-01-26 17:10:45 -08002284int
Chris Wilson05394f32010-11-08 19:18:58 +00002285i915_gem_object_unbind(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002286{
Daniel Vetter7bddb012012-02-09 17:15:47 +01002287 drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07002288 int ret = 0;
2289
Chris Wilson05394f32010-11-08 19:18:58 +00002290 if (obj->gtt_space == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002291 return 0;
2292
Chris Wilson31d8d652012-05-24 19:11:20 +01002293 if (obj->pin_count)
2294 return -EBUSY;
Eric Anholt673a3942008-07-30 12:06:12 -07002295
Chris Wilsona8198ee2011-04-13 22:04:09 +01002296 ret = i915_gem_object_finish_gpu(obj);
Chris Wilson1488fc02012-04-24 15:47:31 +01002297 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002298 return ret;
Chris Wilson8dc17752010-07-23 23:18:51 +01002299 /* Continue on if we fail due to EIO, the GPU is hung so we
2300 * should be safe and we need to cleanup or else we might
2301 * cause memory corruption through use-after-free.
2302 */
Chris Wilsona8198ee2011-04-13 22:04:09 +01002303
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01002304 i915_gem_object_finish_gtt(obj);
Chris Wilsona8198ee2011-04-13 22:04:09 +01002305
Daniel Vetter96b47b62009-12-15 17:50:00 +01002306 /* release the fence reg _after_ flushing */
Chris Wilsond9e86c02010-11-10 16:40:20 +00002307 ret = i915_gem_object_put_fence(obj);
Chris Wilson1488fc02012-04-24 15:47:31 +01002308 if (ret)
Chris Wilsond9e86c02010-11-10 16:40:20 +00002309 return ret;
Daniel Vetter96b47b62009-12-15 17:50:00 +01002310
Chris Wilsondb53a302011-02-03 11:57:46 +00002311 trace_i915_gem_object_unbind(obj);
2312
Daniel Vetter74898d72012-02-15 23:50:22 +01002313 if (obj->has_global_gtt_mapping)
2314 i915_gem_gtt_unbind_object(obj);
Daniel Vetter7bddb012012-02-09 17:15:47 +01002315 if (obj->has_aliasing_ppgtt_mapping) {
2316 i915_ppgtt_unbind_object(dev_priv->mm.aliasing_ppgtt, obj);
2317 obj->has_aliasing_ppgtt_mapping = 0;
2318 }
Daniel Vetter74163902012-02-15 23:50:21 +01002319 i915_gem_gtt_finish_object(obj);
Daniel Vetter7bddb012012-02-09 17:15:47 +01002320
Chris Wilson6c085a72012-08-20 11:40:46 +02002321 list_del(&obj->mm_list);
2322 list_move_tail(&obj->gtt_list, &dev_priv->mm.unbound_list);
Daniel Vetter75e9e912010-11-04 17:11:09 +01002323 /* Avoid an unnecessary call to unbind on rebind. */
Chris Wilson05394f32010-11-08 19:18:58 +00002324 obj->map_and_fenceable = true;
Eric Anholt673a3942008-07-30 12:06:12 -07002325
Chris Wilson05394f32010-11-08 19:18:58 +00002326 drm_mm_put_block(obj->gtt_space);
2327 obj->gtt_space = NULL;
2328 obj->gtt_offset = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002329
Chris Wilson6c085a72012-08-20 11:40:46 +02002330 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002331}
2332
Ben Widawskyb2da9fe2012-04-26 16:02:58 -07002333static int i915_ring_idle(struct intel_ring_buffer *ring)
Chris Wilsona56ba562010-09-28 10:07:56 +01002334{
Chris Wilson69c2fc82012-07-20 12:41:03 +01002335 if (list_empty(&ring->active_list))
Chris Wilson64193402010-10-24 12:38:05 +01002336 return 0;
2337
Ben Widawsky199b2bc2012-05-24 15:03:11 -07002338 return i915_wait_seqno(ring, i915_gem_next_request_seqno(ring));
Chris Wilsona56ba562010-09-28 10:07:56 +01002339}
2340
Ben Widawskyb2da9fe2012-04-26 16:02:58 -07002341int i915_gpu_idle(struct drm_device *dev)
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002342{
2343 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonb4519512012-05-11 14:29:30 +01002344 struct intel_ring_buffer *ring;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002345 int ret, i;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002346
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002347 /* Flush everything onto the inactive list. */
Chris Wilsonb4519512012-05-11 14:29:30 +01002348 for_each_ring(ring, dev_priv, i) {
2349 ret = i915_ring_idle(ring);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002350 if (ret)
2351 return ret;
Chris Wilsonb4519512012-05-11 14:29:30 +01002352
Ben Widawskyf2ef6eb2012-06-04 14:42:53 -07002353 ret = i915_switch_context(ring, NULL, DEFAULT_CONTEXT_ID);
2354 if (ret)
2355 return ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002356 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08002357
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002358 return 0;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002359}
2360
Chris Wilson9ce079e2012-04-17 15:31:30 +01002361static void sandybridge_write_fence_reg(struct drm_device *dev, int reg,
2362 struct drm_i915_gem_object *obj)
Eric Anholt4e901fd2009-10-26 16:44:17 -07002363{
Eric Anholt4e901fd2009-10-26 16:44:17 -07002364 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt4e901fd2009-10-26 16:44:17 -07002365 uint64_t val;
2366
Chris Wilson9ce079e2012-04-17 15:31:30 +01002367 if (obj) {
2368 u32 size = obj->gtt_space->size;
Eric Anholt4e901fd2009-10-26 16:44:17 -07002369
Chris Wilson9ce079e2012-04-17 15:31:30 +01002370 val = (uint64_t)((obj->gtt_offset + size - 4096) &
2371 0xfffff000) << 32;
2372 val |= obj->gtt_offset & 0xfffff000;
2373 val |= (uint64_t)((obj->stride / 128) - 1) <<
2374 SANDYBRIDGE_FENCE_PITCH_SHIFT;
Eric Anholt4e901fd2009-10-26 16:44:17 -07002375
Chris Wilson9ce079e2012-04-17 15:31:30 +01002376 if (obj->tiling_mode == I915_TILING_Y)
2377 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2378 val |= I965_FENCE_REG_VALID;
2379 } else
2380 val = 0;
Daniel Vetterc6642782010-11-12 13:46:18 +00002381
Chris Wilson9ce079e2012-04-17 15:31:30 +01002382 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + reg * 8, val);
2383 POSTING_READ(FENCE_REG_SANDYBRIDGE_0 + reg * 8);
Eric Anholt4e901fd2009-10-26 16:44:17 -07002384}
2385
Chris Wilson9ce079e2012-04-17 15:31:30 +01002386static void i965_write_fence_reg(struct drm_device *dev, int reg,
2387 struct drm_i915_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002388{
Jesse Barnesde151cf2008-11-12 10:03:55 -08002389 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002390 uint64_t val;
2391
Chris Wilson9ce079e2012-04-17 15:31:30 +01002392 if (obj) {
2393 u32 size = obj->gtt_space->size;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002394
Chris Wilson9ce079e2012-04-17 15:31:30 +01002395 val = (uint64_t)((obj->gtt_offset + size - 4096) &
2396 0xfffff000) << 32;
2397 val |= obj->gtt_offset & 0xfffff000;
2398 val |= ((obj->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
2399 if (obj->tiling_mode == I915_TILING_Y)
2400 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2401 val |= I965_FENCE_REG_VALID;
2402 } else
2403 val = 0;
Daniel Vetterc6642782010-11-12 13:46:18 +00002404
Chris Wilson9ce079e2012-04-17 15:31:30 +01002405 I915_WRITE64(FENCE_REG_965_0 + reg * 8, val);
2406 POSTING_READ(FENCE_REG_965_0 + reg * 8);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002407}
2408
Chris Wilson9ce079e2012-04-17 15:31:30 +01002409static void i915_write_fence_reg(struct drm_device *dev, int reg,
2410 struct drm_i915_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002411{
Jesse Barnesde151cf2008-11-12 10:03:55 -08002412 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson9ce079e2012-04-17 15:31:30 +01002413 u32 val;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002414
Chris Wilson9ce079e2012-04-17 15:31:30 +01002415 if (obj) {
2416 u32 size = obj->gtt_space->size;
2417 int pitch_val;
2418 int tile_width;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002419
Chris Wilson9ce079e2012-04-17 15:31:30 +01002420 WARN((obj->gtt_offset & ~I915_FENCE_START_MASK) ||
2421 (size & -size) != size ||
2422 (obj->gtt_offset & (size - 1)),
2423 "object 0x%08x [fenceable? %d] not 1M or pot-size (0x%08x) aligned\n",
2424 obj->gtt_offset, obj->map_and_fenceable, size);
2425
2426 if (obj->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev))
2427 tile_width = 128;
2428 else
2429 tile_width = 512;
2430
2431 /* Note: pitch better be a power of two tile widths */
2432 pitch_val = obj->stride / tile_width;
2433 pitch_val = ffs(pitch_val) - 1;
2434
2435 val = obj->gtt_offset;
2436 if (obj->tiling_mode == I915_TILING_Y)
2437 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2438 val |= I915_FENCE_SIZE_BITS(size);
2439 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2440 val |= I830_FENCE_REG_VALID;
2441 } else
2442 val = 0;
2443
2444 if (reg < 8)
2445 reg = FENCE_REG_830_0 + reg * 4;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002446 else
Chris Wilson9ce079e2012-04-17 15:31:30 +01002447 reg = FENCE_REG_945_8 + (reg - 8) * 4;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002448
Chris Wilson9ce079e2012-04-17 15:31:30 +01002449 I915_WRITE(reg, val);
2450 POSTING_READ(reg);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002451}
2452
Chris Wilson9ce079e2012-04-17 15:31:30 +01002453static void i830_write_fence_reg(struct drm_device *dev, int reg,
2454 struct drm_i915_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002455{
Jesse Barnesde151cf2008-11-12 10:03:55 -08002456 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002457 uint32_t val;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002458
Chris Wilson9ce079e2012-04-17 15:31:30 +01002459 if (obj) {
2460 u32 size = obj->gtt_space->size;
2461 uint32_t pitch_val;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002462
Chris Wilson9ce079e2012-04-17 15:31:30 +01002463 WARN((obj->gtt_offset & ~I830_FENCE_START_MASK) ||
2464 (size & -size) != size ||
2465 (obj->gtt_offset & (size - 1)),
2466 "object 0x%08x not 512K or pot-size 0x%08x aligned\n",
2467 obj->gtt_offset, size);
Eric Anholte76a16d2009-05-26 17:44:56 -07002468
Chris Wilson9ce079e2012-04-17 15:31:30 +01002469 pitch_val = obj->stride / 128;
2470 pitch_val = ffs(pitch_val) - 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002471
Chris Wilson9ce079e2012-04-17 15:31:30 +01002472 val = obj->gtt_offset;
2473 if (obj->tiling_mode == I915_TILING_Y)
2474 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2475 val |= I830_FENCE_SIZE_BITS(size);
2476 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2477 val |= I830_FENCE_REG_VALID;
2478 } else
2479 val = 0;
Daniel Vetterc6642782010-11-12 13:46:18 +00002480
Chris Wilson9ce079e2012-04-17 15:31:30 +01002481 I915_WRITE(FENCE_REG_830_0 + reg * 4, val);
2482 POSTING_READ(FENCE_REG_830_0 + reg * 4);
2483}
2484
2485static void i915_gem_write_fence(struct drm_device *dev, int reg,
2486 struct drm_i915_gem_object *obj)
2487{
2488 switch (INTEL_INFO(dev)->gen) {
2489 case 7:
2490 case 6: sandybridge_write_fence_reg(dev, reg, obj); break;
2491 case 5:
2492 case 4: i965_write_fence_reg(dev, reg, obj); break;
2493 case 3: i915_write_fence_reg(dev, reg, obj); break;
2494 case 2: i830_write_fence_reg(dev, reg, obj); break;
2495 default: break;
2496 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002497}
2498
Chris Wilson61050802012-04-17 15:31:31 +01002499static inline int fence_number(struct drm_i915_private *dev_priv,
2500 struct drm_i915_fence_reg *fence)
2501{
2502 return fence - dev_priv->fence_regs;
2503}
2504
2505static void i915_gem_object_update_fence(struct drm_i915_gem_object *obj,
2506 struct drm_i915_fence_reg *fence,
2507 bool enable)
2508{
2509 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2510 int reg = fence_number(dev_priv, fence);
2511
2512 i915_gem_write_fence(obj->base.dev, reg, enable ? obj : NULL);
2513
2514 if (enable) {
2515 obj->fence_reg = reg;
2516 fence->obj = obj;
2517 list_move_tail(&fence->lru_list, &dev_priv->mm.fence_list);
2518 } else {
2519 obj->fence_reg = I915_FENCE_REG_NONE;
2520 fence->obj = NULL;
2521 list_del_init(&fence->lru_list);
2522 }
2523}
2524
Chris Wilsond9e86c02010-11-10 16:40:20 +00002525static int
Chris Wilsona360bb12012-04-17 15:31:25 +01002526i915_gem_object_flush_fence(struct drm_i915_gem_object *obj)
Chris Wilsond9e86c02010-11-10 16:40:20 +00002527{
Chris Wilson1c293ea2012-04-17 15:31:27 +01002528 if (obj->last_fenced_seqno) {
Chris Wilson86d5bc32012-07-20 12:41:04 +01002529 int ret = i915_wait_seqno(obj->ring, obj->last_fenced_seqno);
Chris Wilson18991842012-04-17 15:31:29 +01002530 if (ret)
2531 return ret;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002532
2533 obj->last_fenced_seqno = 0;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002534 }
2535
Chris Wilson63256ec2011-01-04 18:42:07 +00002536 /* Ensure that all CPU reads are completed before installing a fence
2537 * and all writes before removing the fence.
2538 */
2539 if (obj->base.read_domains & I915_GEM_DOMAIN_GTT)
2540 mb();
2541
Chris Wilson86d5bc32012-07-20 12:41:04 +01002542 obj->fenced_gpu_access = false;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002543 return 0;
2544}
2545
2546int
2547i915_gem_object_put_fence(struct drm_i915_gem_object *obj)
2548{
Chris Wilson61050802012-04-17 15:31:31 +01002549 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002550 int ret;
2551
Chris Wilsona360bb12012-04-17 15:31:25 +01002552 ret = i915_gem_object_flush_fence(obj);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002553 if (ret)
2554 return ret;
2555
Chris Wilson61050802012-04-17 15:31:31 +01002556 if (obj->fence_reg == I915_FENCE_REG_NONE)
2557 return 0;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002558
Chris Wilson61050802012-04-17 15:31:31 +01002559 i915_gem_object_update_fence(obj,
2560 &dev_priv->fence_regs[obj->fence_reg],
2561 false);
2562 i915_gem_object_fence_lost(obj);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002563
2564 return 0;
2565}
2566
2567static struct drm_i915_fence_reg *
Chris Wilsona360bb12012-04-17 15:31:25 +01002568i915_find_fence_reg(struct drm_device *dev)
Daniel Vetterae3db242010-02-19 11:51:58 +01002569{
Daniel Vetterae3db242010-02-19 11:51:58 +01002570 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson8fe301a2012-04-17 15:31:28 +01002571 struct drm_i915_fence_reg *reg, *avail;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002572 int i;
Daniel Vetterae3db242010-02-19 11:51:58 +01002573
2574 /* First try to find a free reg */
Chris Wilsond9e86c02010-11-10 16:40:20 +00002575 avail = NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002576 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2577 reg = &dev_priv->fence_regs[i];
2578 if (!reg->obj)
Chris Wilsond9e86c02010-11-10 16:40:20 +00002579 return reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01002580
Chris Wilson1690e1e2011-12-14 13:57:08 +01002581 if (!reg->pin_count)
Chris Wilsond9e86c02010-11-10 16:40:20 +00002582 avail = reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01002583 }
2584
Chris Wilsond9e86c02010-11-10 16:40:20 +00002585 if (avail == NULL)
2586 return NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002587
2588 /* None available, try to steal one or wait for a user to finish */
Chris Wilsond9e86c02010-11-10 16:40:20 +00002589 list_for_each_entry(reg, &dev_priv->mm.fence_list, lru_list) {
Chris Wilson1690e1e2011-12-14 13:57:08 +01002590 if (reg->pin_count)
Daniel Vetterae3db242010-02-19 11:51:58 +01002591 continue;
2592
Chris Wilson8fe301a2012-04-17 15:31:28 +01002593 return reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01002594 }
2595
Chris Wilson8fe301a2012-04-17 15:31:28 +01002596 return NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002597}
2598
Jesse Barnesde151cf2008-11-12 10:03:55 -08002599/**
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002600 * i915_gem_object_get_fence - set up fencing for an object
Jesse Barnesde151cf2008-11-12 10:03:55 -08002601 * @obj: object to map through a fence reg
2602 *
2603 * When mapping objects through the GTT, userspace wants to be able to write
2604 * to them without having to worry about swizzling if the object is tiled.
Jesse Barnesde151cf2008-11-12 10:03:55 -08002605 * This function walks the fence regs looking for a free one for @obj,
2606 * stealing one if it can't find any.
2607 *
2608 * It then sets up the reg based on the object's properties: address, pitch
2609 * and tiling format.
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002610 *
2611 * For an untiled surface, this removes any existing fence.
Jesse Barnesde151cf2008-11-12 10:03:55 -08002612 */
Chris Wilson8c4b8c32009-06-17 22:08:52 +01002613int
Chris Wilson06d98132012-04-17 15:31:24 +01002614i915_gem_object_get_fence(struct drm_i915_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002615{
Chris Wilson05394f32010-11-08 19:18:58 +00002616 struct drm_device *dev = obj->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002617 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson14415742012-04-17 15:31:33 +01002618 bool enable = obj->tiling_mode != I915_TILING_NONE;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002619 struct drm_i915_fence_reg *reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01002620 int ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002621
Chris Wilson14415742012-04-17 15:31:33 +01002622 /* Have we updated the tiling parameters upon the object and so
2623 * will need to serialise the write to the associated fence register?
2624 */
Chris Wilson5d82e3e2012-04-21 16:23:23 +01002625 if (obj->fence_dirty) {
Chris Wilson14415742012-04-17 15:31:33 +01002626 ret = i915_gem_object_flush_fence(obj);
2627 if (ret)
2628 return ret;
2629 }
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002630
Chris Wilsond9e86c02010-11-10 16:40:20 +00002631 /* Just update our place in the LRU if our fence is getting reused. */
Chris Wilson05394f32010-11-08 19:18:58 +00002632 if (obj->fence_reg != I915_FENCE_REG_NONE) {
2633 reg = &dev_priv->fence_regs[obj->fence_reg];
Chris Wilson5d82e3e2012-04-21 16:23:23 +01002634 if (!obj->fence_dirty) {
Chris Wilson14415742012-04-17 15:31:33 +01002635 list_move_tail(&reg->lru_list,
2636 &dev_priv->mm.fence_list);
2637 return 0;
2638 }
2639 } else if (enable) {
2640 reg = i915_find_fence_reg(dev);
2641 if (reg == NULL)
2642 return -EDEADLK;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002643
Chris Wilson14415742012-04-17 15:31:33 +01002644 if (reg->obj) {
2645 struct drm_i915_gem_object *old = reg->obj;
2646
2647 ret = i915_gem_object_flush_fence(old);
Chris Wilson29c5a582011-03-17 15:23:22 +00002648 if (ret)
2649 return ret;
2650
Chris Wilson14415742012-04-17 15:31:33 +01002651 i915_gem_object_fence_lost(old);
Chris Wilson29c5a582011-03-17 15:23:22 +00002652 }
Chris Wilson14415742012-04-17 15:31:33 +01002653 } else
Eric Anholta09ba7f2009-08-29 12:49:51 -07002654 return 0;
Eric Anholta09ba7f2009-08-29 12:49:51 -07002655
Chris Wilson14415742012-04-17 15:31:33 +01002656 i915_gem_object_update_fence(obj, reg, enable);
Chris Wilson5d82e3e2012-04-21 16:23:23 +01002657 obj->fence_dirty = false;
Chris Wilson14415742012-04-17 15:31:33 +01002658
Chris Wilson9ce079e2012-04-17 15:31:30 +01002659 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002660}
2661
Chris Wilson42d6ab42012-07-26 11:49:32 +01002662static bool i915_gem_valid_gtt_space(struct drm_device *dev,
2663 struct drm_mm_node *gtt_space,
2664 unsigned long cache_level)
2665{
2666 struct drm_mm_node *other;
2667
2668 /* On non-LLC machines we have to be careful when putting differing
2669 * types of snoopable memory together to avoid the prefetcher
2670 * crossing memory domains and dieing.
2671 */
2672 if (HAS_LLC(dev))
2673 return true;
2674
2675 if (gtt_space == NULL)
2676 return true;
2677
2678 if (list_empty(&gtt_space->node_list))
2679 return true;
2680
2681 other = list_entry(gtt_space->node_list.prev, struct drm_mm_node, node_list);
2682 if (other->allocated && !other->hole_follows && other->color != cache_level)
2683 return false;
2684
2685 other = list_entry(gtt_space->node_list.next, struct drm_mm_node, node_list);
2686 if (other->allocated && !gtt_space->hole_follows && other->color != cache_level)
2687 return false;
2688
2689 return true;
2690}
2691
2692static void i915_gem_verify_gtt(struct drm_device *dev)
2693{
2694#if WATCH_GTT
2695 struct drm_i915_private *dev_priv = dev->dev_private;
2696 struct drm_i915_gem_object *obj;
2697 int err = 0;
2698
2699 list_for_each_entry(obj, &dev_priv->mm.gtt_list, gtt_list) {
2700 if (obj->gtt_space == NULL) {
2701 printk(KERN_ERR "object found on GTT list with no space reserved\n");
2702 err++;
2703 continue;
2704 }
2705
2706 if (obj->cache_level != obj->gtt_space->color) {
2707 printk(KERN_ERR "object reserved space [%08lx, %08lx] with wrong color, cache_level=%x, color=%lx\n",
2708 obj->gtt_space->start,
2709 obj->gtt_space->start + obj->gtt_space->size,
2710 obj->cache_level,
2711 obj->gtt_space->color);
2712 err++;
2713 continue;
2714 }
2715
2716 if (!i915_gem_valid_gtt_space(dev,
2717 obj->gtt_space,
2718 obj->cache_level)) {
2719 printk(KERN_ERR "invalid GTT space found at [%08lx, %08lx] - color=%x\n",
2720 obj->gtt_space->start,
2721 obj->gtt_space->start + obj->gtt_space->size,
2722 obj->cache_level);
2723 err++;
2724 continue;
2725 }
2726 }
2727
2728 WARN_ON(err);
2729#endif
2730}
2731
Jesse Barnesde151cf2008-11-12 10:03:55 -08002732/**
Eric Anholt673a3942008-07-30 12:06:12 -07002733 * Finds free space in the GTT aperture and binds the object there.
2734 */
2735static int
Chris Wilson05394f32010-11-08 19:18:58 +00002736i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
Daniel Vetter920afa72010-09-16 17:54:23 +02002737 unsigned alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01002738 bool map_and_fenceable)
Eric Anholt673a3942008-07-30 12:06:12 -07002739{
Chris Wilson05394f32010-11-08 19:18:58 +00002740 struct drm_device *dev = obj->base.dev;
Eric Anholt673a3942008-07-30 12:06:12 -07002741 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07002742 struct drm_mm_node *free_space;
Daniel Vetter5e783302010-11-14 22:32:36 +01002743 u32 size, fence_size, fence_alignment, unfenced_alignment;
Daniel Vetter75e9e912010-11-04 17:11:09 +01002744 bool mappable, fenceable;
Chris Wilson07f73f62009-09-14 16:50:30 +01002745 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002746
Chris Wilson05394f32010-11-08 19:18:58 +00002747 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01002748 DRM_ERROR("Attempting to bind a purgeable object\n");
2749 return -EINVAL;
2750 }
2751
Chris Wilsone28f8712011-07-18 13:11:49 -07002752 fence_size = i915_gem_get_gtt_size(dev,
2753 obj->base.size,
2754 obj->tiling_mode);
2755 fence_alignment = i915_gem_get_gtt_alignment(dev,
2756 obj->base.size,
2757 obj->tiling_mode);
2758 unfenced_alignment =
2759 i915_gem_get_unfenced_gtt_alignment(dev,
2760 obj->base.size,
2761 obj->tiling_mode);
Chris Wilsona00b10c2010-09-24 21:15:47 +01002762
Eric Anholt673a3942008-07-30 12:06:12 -07002763 if (alignment == 0)
Daniel Vetter5e783302010-11-14 22:32:36 +01002764 alignment = map_and_fenceable ? fence_alignment :
2765 unfenced_alignment;
Daniel Vetter75e9e912010-11-04 17:11:09 +01002766 if (map_and_fenceable && alignment & (fence_alignment - 1)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002767 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2768 return -EINVAL;
2769 }
2770
Chris Wilson05394f32010-11-08 19:18:58 +00002771 size = map_and_fenceable ? fence_size : obj->base.size;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002772
Chris Wilson654fc602010-05-27 13:18:21 +01002773 /* If the object is bigger than the entire aperture, reject it early
2774 * before evicting everything in a vain attempt to find space.
2775 */
Chris Wilson05394f32010-11-08 19:18:58 +00002776 if (obj->base.size >
Daniel Vetter75e9e912010-11-04 17:11:09 +01002777 (map_and_fenceable ? dev_priv->mm.gtt_mappable_end : dev_priv->mm.gtt_total)) {
Chris Wilson654fc602010-05-27 13:18:21 +01002778 DRM_ERROR("Attempting to bind an object larger than the aperture\n");
2779 return -E2BIG;
2780 }
2781
Chris Wilson6c085a72012-08-20 11:40:46 +02002782 ret = i915_gem_object_get_pages_gtt(obj);
2783 if (ret)
2784 return ret;
2785
Eric Anholt673a3942008-07-30 12:06:12 -07002786 search_free:
Daniel Vetter75e9e912010-11-04 17:11:09 +01002787 if (map_and_fenceable)
Daniel Vetter920afa72010-09-16 17:54:23 +02002788 free_space =
Chris Wilson42d6ab42012-07-26 11:49:32 +01002789 drm_mm_search_free_in_range_color(&dev_priv->mm.gtt_space,
2790 size, alignment, obj->cache_level,
2791 0, dev_priv->mm.gtt_mappable_end,
2792 false);
Daniel Vetter920afa72010-09-16 17:54:23 +02002793 else
Chris Wilson42d6ab42012-07-26 11:49:32 +01002794 free_space = drm_mm_search_free_color(&dev_priv->mm.gtt_space,
2795 size, alignment, obj->cache_level,
2796 false);
Daniel Vetter920afa72010-09-16 17:54:23 +02002797
2798 if (free_space != NULL) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01002799 if (map_and_fenceable)
Chris Wilson05394f32010-11-08 19:18:58 +00002800 obj->gtt_space =
Daniel Vetter920afa72010-09-16 17:54:23 +02002801 drm_mm_get_block_range_generic(free_space,
Chris Wilson42d6ab42012-07-26 11:49:32 +01002802 size, alignment, obj->cache_level,
Chris Wilson6b9d89b2012-07-10 11:15:23 +01002803 0, dev_priv->mm.gtt_mappable_end,
Chris Wilson42d6ab42012-07-26 11:49:32 +01002804 false);
Daniel Vetter920afa72010-09-16 17:54:23 +02002805 else
Chris Wilson05394f32010-11-08 19:18:58 +00002806 obj->gtt_space =
Chris Wilson42d6ab42012-07-26 11:49:32 +01002807 drm_mm_get_block_generic(free_space,
2808 size, alignment, obj->cache_level,
2809 false);
Daniel Vetter920afa72010-09-16 17:54:23 +02002810 }
Chris Wilson05394f32010-11-08 19:18:58 +00002811 if (obj->gtt_space == NULL) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01002812 ret = i915_gem_evict_something(dev, size, alignment,
Chris Wilson42d6ab42012-07-26 11:49:32 +01002813 obj->cache_level,
Daniel Vetter75e9e912010-11-04 17:11:09 +01002814 map_and_fenceable);
Chris Wilson97311292009-09-21 00:22:34 +01002815 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002816 return ret;
Chris Wilson97311292009-09-21 00:22:34 +01002817
Eric Anholt673a3942008-07-30 12:06:12 -07002818 goto search_free;
2819 }
Chris Wilson42d6ab42012-07-26 11:49:32 +01002820 if (WARN_ON(!i915_gem_valid_gtt_space(dev,
2821 obj->gtt_space,
2822 obj->cache_level))) {
2823 drm_mm_put_block(obj->gtt_space);
2824 obj->gtt_space = NULL;
2825 return -EINVAL;
2826 }
Eric Anholt673a3942008-07-30 12:06:12 -07002827
Eric Anholt673a3942008-07-30 12:06:12 -07002828
Daniel Vetter74163902012-02-15 23:50:21 +01002829 ret = i915_gem_gtt_prepare_object(obj);
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002830 if (ret) {
Chris Wilson05394f32010-11-08 19:18:58 +00002831 drm_mm_put_block(obj->gtt_space);
2832 obj->gtt_space = NULL;
Chris Wilson6c085a72012-08-20 11:40:46 +02002833 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002834 }
Eric Anholt673a3942008-07-30 12:06:12 -07002835
Daniel Vetter0ebb9822012-02-15 23:50:24 +01002836 if (!dev_priv->mm.aliasing_ppgtt)
2837 i915_gem_gtt_bind_object(obj, obj->cache_level);
Eric Anholt673a3942008-07-30 12:06:12 -07002838
Chris Wilson6c085a72012-08-20 11:40:46 +02002839 list_move_tail(&obj->gtt_list, &dev_priv->mm.bound_list);
Chris Wilson05394f32010-11-08 19:18:58 +00002840 list_add_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
Chris Wilsonbf1a1092010-08-07 11:01:20 +01002841
Chris Wilson6299f992010-11-24 12:23:44 +00002842 obj->gtt_offset = obj->gtt_space->start;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002843
Daniel Vetter75e9e912010-11-04 17:11:09 +01002844 fenceable =
Chris Wilson05394f32010-11-08 19:18:58 +00002845 obj->gtt_space->size == fence_size &&
Akshay Joshi0206e352011-08-16 15:34:10 -04002846 (obj->gtt_space->start & (fence_alignment - 1)) == 0;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002847
Daniel Vetter75e9e912010-11-04 17:11:09 +01002848 mappable =
Chris Wilson05394f32010-11-08 19:18:58 +00002849 obj->gtt_offset + obj->base.size <= dev_priv->mm.gtt_mappable_end;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002850
Chris Wilson05394f32010-11-08 19:18:58 +00002851 obj->map_and_fenceable = mappable && fenceable;
Daniel Vetter75e9e912010-11-04 17:11:09 +01002852
Chris Wilsondb53a302011-02-03 11:57:46 +00002853 trace_i915_gem_object_bind(obj, map_and_fenceable);
Chris Wilson42d6ab42012-07-26 11:49:32 +01002854 i915_gem_verify_gtt(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07002855 return 0;
2856}
2857
2858void
Chris Wilson05394f32010-11-08 19:18:58 +00002859i915_gem_clflush_object(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002860{
Eric Anholt673a3942008-07-30 12:06:12 -07002861 /* If we don't have a page list set up, then we're not pinned
2862 * to GPU, and we can ignore the cache flush because it'll happen
2863 * again at bind time.
2864 */
Chris Wilson05394f32010-11-08 19:18:58 +00002865 if (obj->pages == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002866 return;
2867
Chris Wilson9c23f7f2011-03-29 16:59:52 -07002868 /* If the GPU is snooping the contents of the CPU cache,
2869 * we do not need to manually clear the CPU cache lines. However,
2870 * the caches are only snooped when the render cache is
2871 * flushed/invalidated. As we always have to emit invalidations
2872 * and flushes when moving into and out of the RENDER domain, correct
2873 * snooping behaviour occurs naturally as the result of our domain
2874 * tracking.
2875 */
2876 if (obj->cache_level != I915_CACHE_NONE)
2877 return;
2878
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002879 trace_i915_gem_object_clflush(obj);
Eric Anholtcfa16a02009-05-26 18:46:16 -07002880
Chris Wilson05394f32010-11-08 19:18:58 +00002881 drm_clflush_pages(obj->pages, obj->base.size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002882}
2883
Eric Anholte47c68e2008-11-14 13:35:19 -08002884/** Flushes the GTT write domain for the object if it's dirty. */
2885static void
Chris Wilson05394f32010-11-08 19:18:58 +00002886i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08002887{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002888 uint32_t old_write_domain;
2889
Chris Wilson05394f32010-11-08 19:18:58 +00002890 if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
Eric Anholte47c68e2008-11-14 13:35:19 -08002891 return;
2892
Chris Wilson63256ec2011-01-04 18:42:07 +00002893 /* No actual flushing is required for the GTT write domain. Writes
Eric Anholte47c68e2008-11-14 13:35:19 -08002894 * to it immediately go to main memory as far as we know, so there's
2895 * no chipset flush. It also doesn't land in render cache.
Chris Wilson63256ec2011-01-04 18:42:07 +00002896 *
2897 * However, we do have to enforce the order so that all writes through
2898 * the GTT land before any writes to the device, such as updates to
2899 * the GATT itself.
Eric Anholte47c68e2008-11-14 13:35:19 -08002900 */
Chris Wilson63256ec2011-01-04 18:42:07 +00002901 wmb();
2902
Chris Wilson05394f32010-11-08 19:18:58 +00002903 old_write_domain = obj->base.write_domain;
2904 obj->base.write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002905
2906 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00002907 obj->base.read_domains,
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002908 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002909}
2910
2911/** Flushes the CPU write domain for the object if it's dirty. */
2912static void
Chris Wilson05394f32010-11-08 19:18:58 +00002913i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08002914{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002915 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002916
Chris Wilson05394f32010-11-08 19:18:58 +00002917 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
Eric Anholte47c68e2008-11-14 13:35:19 -08002918 return;
2919
2920 i915_gem_clflush_object(obj);
Daniel Vetter40ce6572010-11-05 18:12:18 +01002921 intel_gtt_chipset_flush();
Chris Wilson05394f32010-11-08 19:18:58 +00002922 old_write_domain = obj->base.write_domain;
2923 obj->base.write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002924
2925 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00002926 obj->base.read_domains,
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002927 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002928}
2929
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002930/**
2931 * Moves a single object to the GTT read, and possibly write domain.
2932 *
2933 * This function returns when the move is complete, including waiting on
2934 * flushes to occur.
2935 */
Jesse Barnes79e53942008-11-07 14:24:08 -08002936int
Chris Wilson20217462010-11-23 15:26:33 +00002937i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002938{
Chris Wilson8325a092012-04-24 15:52:35 +01002939 drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002940 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002941 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002942
Eric Anholt02354392008-11-26 13:58:13 -08002943 /* Not valid to be called on unbound objects. */
Chris Wilson05394f32010-11-08 19:18:58 +00002944 if (obj->gtt_space == NULL)
Eric Anholt02354392008-11-26 13:58:13 -08002945 return -EINVAL;
2946
Chris Wilson8d7e3de2011-02-07 15:23:02 +00002947 if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
2948 return 0;
2949
Chris Wilson0201f1e2012-07-20 12:41:01 +01002950 ret = i915_gem_object_wait_rendering(obj, !write);
2951 if (ret)
2952 return ret;
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002953
Chris Wilson72133422010-09-13 23:56:38 +01002954 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002955
Chris Wilson05394f32010-11-08 19:18:58 +00002956 old_write_domain = obj->base.write_domain;
2957 old_read_domains = obj->base.read_domains;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002958
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002959 /* It should now be out of any other write domains, and we can update
2960 * the domain values for our changes.
2961 */
Chris Wilson05394f32010-11-08 19:18:58 +00002962 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2963 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002964 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00002965 obj->base.read_domains = I915_GEM_DOMAIN_GTT;
2966 obj->base.write_domain = I915_GEM_DOMAIN_GTT;
2967 obj->dirty = 1;
Eric Anholte47c68e2008-11-14 13:35:19 -08002968 }
2969
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002970 trace_i915_gem_object_change_domain(obj,
2971 old_read_domains,
2972 old_write_domain);
2973
Chris Wilson8325a092012-04-24 15:52:35 +01002974 /* And bump the LRU for this access */
2975 if (i915_gem_object_is_inactive(obj))
2976 list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
2977
Eric Anholte47c68e2008-11-14 13:35:19 -08002978 return 0;
2979}
2980
Chris Wilsone4ffd172011-04-04 09:44:39 +01002981int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
2982 enum i915_cache_level cache_level)
2983{
Daniel Vetter7bddb012012-02-09 17:15:47 +01002984 struct drm_device *dev = obj->base.dev;
2985 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsone4ffd172011-04-04 09:44:39 +01002986 int ret;
2987
2988 if (obj->cache_level == cache_level)
2989 return 0;
2990
2991 if (obj->pin_count) {
2992 DRM_DEBUG("can not change the cache level of pinned objects\n");
2993 return -EBUSY;
2994 }
2995
Chris Wilson42d6ab42012-07-26 11:49:32 +01002996 if (!i915_gem_valid_gtt_space(dev, obj->gtt_space, cache_level)) {
2997 ret = i915_gem_object_unbind(obj);
2998 if (ret)
2999 return ret;
3000 }
3001
Chris Wilsone4ffd172011-04-04 09:44:39 +01003002 if (obj->gtt_space) {
3003 ret = i915_gem_object_finish_gpu(obj);
3004 if (ret)
3005 return ret;
3006
3007 i915_gem_object_finish_gtt(obj);
3008
3009 /* Before SandyBridge, you could not use tiling or fence
3010 * registers with snooped memory, so relinquish any fences
3011 * currently pointing to our region in the aperture.
3012 */
Chris Wilson42d6ab42012-07-26 11:49:32 +01003013 if (INTEL_INFO(dev)->gen < 6) {
Chris Wilsone4ffd172011-04-04 09:44:39 +01003014 ret = i915_gem_object_put_fence(obj);
3015 if (ret)
3016 return ret;
3017 }
3018
Daniel Vetter74898d72012-02-15 23:50:22 +01003019 if (obj->has_global_gtt_mapping)
3020 i915_gem_gtt_bind_object(obj, cache_level);
Daniel Vetter7bddb012012-02-09 17:15:47 +01003021 if (obj->has_aliasing_ppgtt_mapping)
3022 i915_ppgtt_bind_object(dev_priv->mm.aliasing_ppgtt,
3023 obj, cache_level);
Chris Wilson42d6ab42012-07-26 11:49:32 +01003024
3025 obj->gtt_space->color = cache_level;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003026 }
3027
3028 if (cache_level == I915_CACHE_NONE) {
3029 u32 old_read_domains, old_write_domain;
3030
3031 /* If we're coming from LLC cached, then we haven't
3032 * actually been tracking whether the data is in the
3033 * CPU cache or not, since we only allow one bit set
3034 * in obj->write_domain and have been skipping the clflushes.
3035 * Just set it to the CPU cache for now.
3036 */
3037 WARN_ON(obj->base.write_domain & ~I915_GEM_DOMAIN_CPU);
3038 WARN_ON(obj->base.read_domains & ~I915_GEM_DOMAIN_CPU);
3039
3040 old_read_domains = obj->base.read_domains;
3041 old_write_domain = obj->base.write_domain;
3042
3043 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3044 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
3045
3046 trace_i915_gem_object_change_domain(obj,
3047 old_read_domains,
3048 old_write_domain);
3049 }
3050
3051 obj->cache_level = cache_level;
Chris Wilson42d6ab42012-07-26 11:49:32 +01003052 i915_gem_verify_gtt(dev);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003053 return 0;
3054}
3055
Chris Wilsone6994ae2012-07-10 10:27:08 +01003056int i915_gem_get_cacheing_ioctl(struct drm_device *dev, void *data,
3057 struct drm_file *file)
3058{
3059 struct drm_i915_gem_cacheing *args = data;
3060 struct drm_i915_gem_object *obj;
3061 int ret;
3062
3063 ret = i915_mutex_lock_interruptible(dev);
3064 if (ret)
3065 return ret;
3066
3067 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
3068 if (&obj->base == NULL) {
3069 ret = -ENOENT;
3070 goto unlock;
3071 }
3072
3073 args->cacheing = obj->cache_level != I915_CACHE_NONE;
3074
3075 drm_gem_object_unreference(&obj->base);
3076unlock:
3077 mutex_unlock(&dev->struct_mutex);
3078 return ret;
3079}
3080
3081int i915_gem_set_cacheing_ioctl(struct drm_device *dev, void *data,
3082 struct drm_file *file)
3083{
3084 struct drm_i915_gem_cacheing *args = data;
3085 struct drm_i915_gem_object *obj;
3086 enum i915_cache_level level;
3087 int ret;
3088
3089 ret = i915_mutex_lock_interruptible(dev);
3090 if (ret)
3091 return ret;
3092
3093 switch (args->cacheing) {
3094 case I915_CACHEING_NONE:
3095 level = I915_CACHE_NONE;
3096 break;
3097 case I915_CACHEING_CACHED:
3098 level = I915_CACHE_LLC;
3099 break;
3100 default:
3101 return -EINVAL;
3102 }
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 ret = i915_gem_object_set_cache_level(obj, level);
3111
3112 drm_gem_object_unreference(&obj->base);
3113unlock:
3114 mutex_unlock(&dev->struct_mutex);
3115 return ret;
3116}
3117
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003118/*
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003119 * Prepare buffer for display plane (scanout, cursors, etc).
3120 * Can be called from an uninterruptible phase (modesetting) and allows
3121 * any flushes to be pipelined (for pageflips).
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003122 */
3123int
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003124i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
3125 u32 alignment,
Chris Wilson919926a2010-11-12 13:42:53 +00003126 struct intel_ring_buffer *pipelined)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003127{
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003128 u32 old_read_domains, old_write_domain;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003129 int ret;
3130
Chris Wilson0be73282010-12-06 14:36:27 +00003131 if (pipelined != obj->ring) {
Ben Widawsky2911a352012-04-05 14:47:36 -07003132 ret = i915_gem_object_sync(obj, pipelined);
3133 if (ret)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003134 return ret;
3135 }
3136
Eric Anholta7ef0642011-03-29 16:59:54 -07003137 /* The display engine is not coherent with the LLC cache on gen6. As
3138 * a result, we make sure that the pinning that is about to occur is
3139 * done with uncached PTEs. This is lowest common denominator for all
3140 * chipsets.
3141 *
3142 * However for gen6+, we could do better by using the GFDT bit instead
3143 * of uncaching, which would allow us to flush all the LLC-cached data
3144 * with that bit in the PTE to main memory with just one PIPE_CONTROL.
3145 */
3146 ret = i915_gem_object_set_cache_level(obj, I915_CACHE_NONE);
3147 if (ret)
3148 return ret;
3149
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003150 /* As the user may map the buffer once pinned in the display plane
3151 * (e.g. libkms for the bootup splash), we have to ensure that we
3152 * always use map_and_fenceable for all scanout buffers.
3153 */
3154 ret = i915_gem_object_pin(obj, alignment, true);
3155 if (ret)
3156 return ret;
3157
Chris Wilsonb118c1e2010-05-27 13:18:14 +01003158 i915_gem_object_flush_cpu_write_domain(obj);
3159
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003160 old_write_domain = obj->base.write_domain;
Chris Wilson05394f32010-11-08 19:18:58 +00003161 old_read_domains = obj->base.read_domains;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003162
3163 /* It should now be out of any other write domains, and we can update
3164 * the domain values for our changes.
3165 */
Chris Wilsone5f1d962012-07-20 12:41:00 +01003166 obj->base.write_domain = 0;
Chris Wilson05394f32010-11-08 19:18:58 +00003167 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003168
3169 trace_i915_gem_object_change_domain(obj,
3170 old_read_domains,
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003171 old_write_domain);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003172
3173 return 0;
3174}
3175
Chris Wilson85345512010-11-13 09:49:11 +00003176int
Chris Wilsona8198ee2011-04-13 22:04:09 +01003177i915_gem_object_finish_gpu(struct drm_i915_gem_object *obj)
Chris Wilson85345512010-11-13 09:49:11 +00003178{
Chris Wilson88241782011-01-07 17:09:48 +00003179 int ret;
3180
Chris Wilsona8198ee2011-04-13 22:04:09 +01003181 if ((obj->base.read_domains & I915_GEM_GPU_DOMAINS) == 0)
Chris Wilson85345512010-11-13 09:49:11 +00003182 return 0;
3183
Chris Wilson0201f1e2012-07-20 12:41:01 +01003184 ret = i915_gem_object_wait_rendering(obj, false);
Chris Wilsonc501ae72011-12-14 13:57:23 +01003185 if (ret)
3186 return ret;
3187
Chris Wilsona8198ee2011-04-13 22:04:09 +01003188 /* Ensure that we invalidate the GPU's caches and TLBs. */
3189 obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
Chris Wilsonc501ae72011-12-14 13:57:23 +01003190 return 0;
Chris Wilson85345512010-11-13 09:49:11 +00003191}
3192
Eric Anholte47c68e2008-11-14 13:35:19 -08003193/**
3194 * Moves a single object to the CPU read, and possibly write domain.
3195 *
3196 * This function returns when the move is complete, including waiting on
3197 * flushes to occur.
3198 */
Chris Wilsondabdfe02012-03-26 10:10:27 +02003199int
Chris Wilson919926a2010-11-12 13:42:53 +00003200i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholte47c68e2008-11-14 13:35:19 -08003201{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003202 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003203 int ret;
3204
Chris Wilson8d7e3de2011-02-07 15:23:02 +00003205 if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
3206 return 0;
3207
Chris Wilson0201f1e2012-07-20 12:41:01 +01003208 ret = i915_gem_object_wait_rendering(obj, !write);
3209 if (ret)
3210 return ret;
Eric Anholte47c68e2008-11-14 13:35:19 -08003211
3212 i915_gem_object_flush_gtt_write_domain(obj);
3213
Chris Wilson05394f32010-11-08 19:18:58 +00003214 old_write_domain = obj->base.write_domain;
3215 old_read_domains = obj->base.read_domains;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003216
Eric Anholte47c68e2008-11-14 13:35:19 -08003217 /* Flush the CPU cache if it's still invalid. */
Chris Wilson05394f32010-11-08 19:18:58 +00003218 if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
Eric Anholte47c68e2008-11-14 13:35:19 -08003219 i915_gem_clflush_object(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003220
Chris Wilson05394f32010-11-08 19:18:58 +00003221 obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003222 }
3223
3224 /* It should now be out of any other write domains, and we can update
3225 * the domain values for our changes.
3226 */
Chris Wilson05394f32010-11-08 19:18:58 +00003227 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
Eric Anholte47c68e2008-11-14 13:35:19 -08003228
3229 /* If we're writing through the CPU, then the GPU read domains will
3230 * need to be invalidated at next use.
3231 */
3232 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00003233 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3234 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003235 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003236
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003237 trace_i915_gem_object_change_domain(obj,
3238 old_read_domains,
3239 old_write_domain);
3240
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003241 return 0;
3242}
3243
Eric Anholt673a3942008-07-30 12:06:12 -07003244/* Throttle our rendering by waiting until the ring has completed our requests
3245 * emitted over 20 msec ago.
3246 *
Eric Anholtb9624422009-06-03 07:27:35 +00003247 * Note that if we were to use the current jiffies each time around the loop,
3248 * we wouldn't escape the function with any frames outstanding if the time to
3249 * render a frame was over 20ms.
3250 *
Eric Anholt673a3942008-07-30 12:06:12 -07003251 * This should get us reasonable parallelism between CPU and GPU but also
3252 * relatively low latency when blocking on a particular request to finish.
3253 */
3254static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003255i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003256{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003257 struct drm_i915_private *dev_priv = dev->dev_private;
3258 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00003259 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003260 struct drm_i915_gem_request *request;
3261 struct intel_ring_buffer *ring = NULL;
3262 u32 seqno = 0;
3263 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003264
Chris Wilsone110e8d2011-01-26 15:39:14 +00003265 if (atomic_read(&dev_priv->mm.wedged))
3266 return -EIO;
3267
Chris Wilson1c255952010-09-26 11:03:27 +01003268 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003269 list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
Eric Anholtb9624422009-06-03 07:27:35 +00003270 if (time_after_eq(request->emitted_jiffies, recent_enough))
3271 break;
3272
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003273 ring = request->ring;
3274 seqno = request->seqno;
Eric Anholtb9624422009-06-03 07:27:35 +00003275 }
Chris Wilson1c255952010-09-26 11:03:27 +01003276 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003277
3278 if (seqno == 0)
3279 return 0;
3280
Ben Widawsky5c81fe852012-05-24 15:03:08 -07003281 ret = __wait_seqno(ring, seqno, true, NULL);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003282 if (ret == 0)
3283 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
Eric Anholtb9624422009-06-03 07:27:35 +00003284
Eric Anholt673a3942008-07-30 12:06:12 -07003285 return ret;
3286}
3287
Eric Anholt673a3942008-07-30 12:06:12 -07003288int
Chris Wilson05394f32010-11-08 19:18:58 +00003289i915_gem_object_pin(struct drm_i915_gem_object *obj,
3290 uint32_t alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01003291 bool map_and_fenceable)
Eric Anholt673a3942008-07-30 12:06:12 -07003292{
Eric Anholt673a3942008-07-30 12:06:12 -07003293 int ret;
3294
Chris Wilson05394f32010-11-08 19:18:58 +00003295 BUG_ON(obj->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01003296
Chris Wilson05394f32010-11-08 19:18:58 +00003297 if (obj->gtt_space != NULL) {
3298 if ((alignment && obj->gtt_offset & (alignment - 1)) ||
3299 (map_and_fenceable && !obj->map_and_fenceable)) {
3300 WARN(obj->pin_count,
Chris Wilsonae7d49d2010-08-04 12:37:41 +01003301 "bo is already pinned with incorrect alignment:"
Daniel Vetter75e9e912010-11-04 17:11:09 +01003302 " offset=%x, req.alignment=%x, req.map_and_fenceable=%d,"
3303 " obj->map_and_fenceable=%d\n",
Chris Wilson05394f32010-11-08 19:18:58 +00003304 obj->gtt_offset, alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01003305 map_and_fenceable,
Chris Wilson05394f32010-11-08 19:18:58 +00003306 obj->map_and_fenceable);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01003307 ret = i915_gem_object_unbind(obj);
3308 if (ret)
3309 return ret;
3310 }
3311 }
3312
Chris Wilson05394f32010-11-08 19:18:58 +00003313 if (obj->gtt_space == NULL) {
Chris Wilsona00b10c2010-09-24 21:15:47 +01003314 ret = i915_gem_object_bind_to_gtt(obj, alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01003315 map_and_fenceable);
Chris Wilson97311292009-09-21 00:22:34 +01003316 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07003317 return ret;
Chris Wilson22c344e2009-02-11 14:26:45 +00003318 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05003319
Daniel Vetter74898d72012-02-15 23:50:22 +01003320 if (!obj->has_global_gtt_mapping && map_and_fenceable)
3321 i915_gem_gtt_bind_object(obj, obj->cache_level);
3322
Chris Wilson1b502472012-04-24 15:47:30 +01003323 obj->pin_count++;
Chris Wilson6299f992010-11-24 12:23:44 +00003324 obj->pin_mappable |= map_and_fenceable;
Eric Anholt673a3942008-07-30 12:06:12 -07003325
3326 return 0;
3327}
3328
3329void
Chris Wilson05394f32010-11-08 19:18:58 +00003330i915_gem_object_unpin(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07003331{
Chris Wilson05394f32010-11-08 19:18:58 +00003332 BUG_ON(obj->pin_count == 0);
3333 BUG_ON(obj->gtt_space == NULL);
Eric Anholt673a3942008-07-30 12:06:12 -07003334
Chris Wilson1b502472012-04-24 15:47:30 +01003335 if (--obj->pin_count == 0)
Chris Wilson6299f992010-11-24 12:23:44 +00003336 obj->pin_mappable = false;
Eric Anholt673a3942008-07-30 12:06:12 -07003337}
3338
3339int
3340i915_gem_pin_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00003341 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003342{
3343 struct drm_i915_gem_pin *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003344 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07003345 int ret;
3346
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003347 ret = i915_mutex_lock_interruptible(dev);
3348 if (ret)
3349 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003350
Chris Wilson05394f32010-11-08 19:18:58 +00003351 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00003352 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003353 ret = -ENOENT;
3354 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07003355 }
Eric Anholt673a3942008-07-30 12:06:12 -07003356
Chris Wilson05394f32010-11-08 19:18:58 +00003357 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilsonbb6baf72009-09-22 14:24:13 +01003358 DRM_ERROR("Attempting to pin a purgeable buffer\n");
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003359 ret = -EINVAL;
3360 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003361 }
3362
Chris Wilson05394f32010-11-08 19:18:58 +00003363 if (obj->pin_filp != NULL && obj->pin_filp != file) {
Jesse Barnes79e53942008-11-07 14:24:08 -08003364 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
3365 args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003366 ret = -EINVAL;
3367 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08003368 }
3369
Chris Wilson05394f32010-11-08 19:18:58 +00003370 obj->user_pin_count++;
3371 obj->pin_filp = file;
3372 if (obj->user_pin_count == 1) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01003373 ret = i915_gem_object_pin(obj, args->alignment, true);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003374 if (ret)
3375 goto out;
Eric Anholt673a3942008-07-30 12:06:12 -07003376 }
3377
3378 /* XXX - flush the CPU caches for pinned objects
3379 * as the X server doesn't manage domains yet
3380 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003381 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00003382 args->offset = obj->gtt_offset;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003383out:
Chris Wilson05394f32010-11-08 19:18:58 +00003384 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003385unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07003386 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003387 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003388}
3389
3390int
3391i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00003392 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003393{
3394 struct drm_i915_gem_pin *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003395 struct drm_i915_gem_object *obj;
Chris Wilson76c1dec2010-09-25 11:22:51 +01003396 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003397
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003398 ret = i915_mutex_lock_interruptible(dev);
3399 if (ret)
3400 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003401
Chris Wilson05394f32010-11-08 19:18:58 +00003402 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00003403 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003404 ret = -ENOENT;
3405 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07003406 }
Chris Wilson76c1dec2010-09-25 11:22:51 +01003407
Chris Wilson05394f32010-11-08 19:18:58 +00003408 if (obj->pin_filp != file) {
Jesse Barnes79e53942008-11-07 14:24:08 -08003409 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
3410 args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003411 ret = -EINVAL;
3412 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08003413 }
Chris Wilson05394f32010-11-08 19:18:58 +00003414 obj->user_pin_count--;
3415 if (obj->user_pin_count == 0) {
3416 obj->pin_filp = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08003417 i915_gem_object_unpin(obj);
3418 }
Eric Anholt673a3942008-07-30 12:06:12 -07003419
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_busy_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_busy *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003432 struct drm_i915_gem_object *obj;
Chris Wilson30dbf0c2010-09-25 10:19:17 +01003433 int ret;
3434
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 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08003444
Chris Wilson0be555b2010-08-04 15:36:30 +01003445 /* Count all active objects as busy, even if they are currently not used
3446 * by the gpu. Users of this interface expect objects to eventually
3447 * become non-busy without any further actions, therefore emit any
3448 * necessary flushes here.
Eric Anholtc4de0a52008-12-14 19:05:04 -08003449 */
Daniel Vetter30dfebf2012-06-01 15:21:23 +02003450 ret = i915_gem_object_flush_active(obj);
3451
Chris Wilson05394f32010-11-08 19:18:58 +00003452 args->busy = obj->active;
Chris Wilsone9808ed2012-07-04 12:25:08 +01003453 if (obj->ring) {
3454 BUILD_BUG_ON(I915_NUM_RINGS > 16);
3455 args->busy |= intel_ring_flag(obj->ring) << 16;
3456 }
Eric Anholt673a3942008-07-30 12:06:12 -07003457
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_throttle_ioctl(struct drm_device *dev, void *data,
3466 struct drm_file *file_priv)
3467{
Akshay Joshi0206e352011-08-16 15:34:10 -04003468 return i915_gem_ring_throttle(dev, file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07003469}
3470
Chris Wilson3ef94da2009-09-14 16:50:29 +01003471int
3472i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
3473 struct drm_file *file_priv)
3474{
3475 struct drm_i915_gem_madvise *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003476 struct drm_i915_gem_object *obj;
Chris Wilson76c1dec2010-09-25 11:22:51 +01003477 int ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003478
3479 switch (args->madv) {
3480 case I915_MADV_DONTNEED:
3481 case I915_MADV_WILLNEED:
3482 break;
3483 default:
3484 return -EINVAL;
3485 }
3486
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003487 ret = i915_mutex_lock_interruptible(dev);
3488 if (ret)
3489 return ret;
3490
Chris Wilson05394f32010-11-08 19:18:58 +00003491 obj = to_intel_bo(drm_gem_object_lookup(dev, file_priv, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00003492 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003493 ret = -ENOENT;
3494 goto unlock;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003495 }
Chris Wilson3ef94da2009-09-14 16:50:29 +01003496
Chris Wilson05394f32010-11-08 19:18:58 +00003497 if (obj->pin_count) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003498 ret = -EINVAL;
3499 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003500 }
3501
Chris Wilson05394f32010-11-08 19:18:58 +00003502 if (obj->madv != __I915_MADV_PURGED)
3503 obj->madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003504
Chris Wilson6c085a72012-08-20 11:40:46 +02003505 /* if the object is no longer attached, discard its backing storage */
3506 if (i915_gem_object_is_purgeable(obj) && obj->pages == NULL)
Chris Wilson2d7ef392009-09-20 23:13:10 +01003507 i915_gem_object_truncate(obj);
3508
Chris Wilson05394f32010-11-08 19:18:58 +00003509 args->retained = obj->madv != __I915_MADV_PURGED;
Chris Wilsonbb6baf72009-09-22 14:24:13 +01003510
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003511out:
Chris Wilson05394f32010-11-08 19:18:58 +00003512 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003513unlock:
Chris Wilson3ef94da2009-09-14 16:50:29 +01003514 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003515 return ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003516}
3517
Chris Wilson05394f32010-11-08 19:18:58 +00003518struct drm_i915_gem_object *i915_gem_alloc_object(struct drm_device *dev,
3519 size_t size)
Daniel Vetterac52bc52010-04-09 19:05:06 +00003520{
Chris Wilson73aa8082010-09-30 11:46:12 +01003521 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc397b902010-04-09 19:05:07 +00003522 struct drm_i915_gem_object *obj;
Hugh Dickins5949eac2011-06-27 16:18:18 -07003523 struct address_space *mapping;
Chris Wilsonbed1ea92012-05-24 20:48:12 +01003524 u32 mask;
Daniel Vetterc397b902010-04-09 19:05:07 +00003525
3526 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
3527 if (obj == NULL)
3528 return NULL;
3529
3530 if (drm_gem_object_init(dev, &obj->base, size) != 0) {
3531 kfree(obj);
3532 return NULL;
3533 }
3534
Chris Wilsonbed1ea92012-05-24 20:48:12 +01003535 mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
3536 if (IS_CRESTLINE(dev) || IS_BROADWATER(dev)) {
3537 /* 965gm cannot relocate objects above 4GiB. */
3538 mask &= ~__GFP_HIGHMEM;
3539 mask |= __GFP_DMA32;
3540 }
3541
Hugh Dickins5949eac2011-06-27 16:18:18 -07003542 mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Chris Wilsonbed1ea92012-05-24 20:48:12 +01003543 mapping_set_gfp_mask(mapping, mask);
Hugh Dickins5949eac2011-06-27 16:18:18 -07003544
Chris Wilson73aa8082010-09-30 11:46:12 +01003545 i915_gem_info_add_obj(dev_priv, size);
3546
Daniel Vetterc397b902010-04-09 19:05:07 +00003547 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
3548 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3549
Eugeni Dodonov3d29b842012-01-17 14:43:53 -02003550 if (HAS_LLC(dev)) {
3551 /* On some devices, we can have the GPU use the LLC (the CPU
Eric Anholta1871112011-03-29 16:59:55 -07003552 * cache) for about a 10% performance improvement
3553 * compared to uncached. Graphics requests other than
3554 * display scanout are coherent with the CPU in
3555 * accessing this cache. This means in this mode we
3556 * don't need to clflush on the CPU side, and on the
3557 * GPU side we only need to flush internal caches to
3558 * get data visible to the CPU.
3559 *
3560 * However, we maintain the display planes as UC, and so
3561 * need to rebind when first used as such.
3562 */
3563 obj->cache_level = I915_CACHE_LLC;
3564 } else
3565 obj->cache_level = I915_CACHE_NONE;
3566
Daniel Vetter62b8b212010-04-09 19:05:08 +00003567 obj->base.driver_private = NULL;
Daniel Vetterc397b902010-04-09 19:05:07 +00003568 obj->fence_reg = I915_FENCE_REG_NONE;
Chris Wilson69dc4982010-10-19 10:36:51 +01003569 INIT_LIST_HEAD(&obj->mm_list);
Daniel Vetter93a37f22010-11-05 20:24:53 +01003570 INIT_LIST_HEAD(&obj->gtt_list);
Chris Wilson69dc4982010-10-19 10:36:51 +01003571 INIT_LIST_HEAD(&obj->ring_list);
Chris Wilson432e58e2010-11-25 19:32:06 +00003572 INIT_LIST_HEAD(&obj->exec_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00003573 obj->madv = I915_MADV_WILLNEED;
Daniel Vetter75e9e912010-11-04 17:11:09 +01003574 /* Avoid an unnecessary call to unbind on the first bind. */
3575 obj->map_and_fenceable = true;
Daniel Vetterc397b902010-04-09 19:05:07 +00003576
Chris Wilson05394f32010-11-08 19:18:58 +00003577 return obj;
Daniel Vetterac52bc52010-04-09 19:05:06 +00003578}
3579
Eric Anholt673a3942008-07-30 12:06:12 -07003580int i915_gem_init_object(struct drm_gem_object *obj)
3581{
Daniel Vetterc397b902010-04-09 19:05:07 +00003582 BUG();
Jesse Barnesde151cf2008-11-12 10:03:55 -08003583
Eric Anholt673a3942008-07-30 12:06:12 -07003584 return 0;
3585}
3586
Chris Wilson1488fc02012-04-24 15:47:31 +01003587void i915_gem_free_object(struct drm_gem_object *gem_obj)
Chris Wilsonbe726152010-07-23 23:18:50 +01003588{
Chris Wilson1488fc02012-04-24 15:47:31 +01003589 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
Chris Wilson05394f32010-11-08 19:18:58 +00003590 struct drm_device *dev = obj->base.dev;
Chris Wilsonbe726152010-07-23 23:18:50 +01003591 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonbe726152010-07-23 23:18:50 +01003592
Chris Wilson26e12f892011-03-20 11:20:19 +00003593 trace_i915_gem_object_destroy(obj);
3594
Daniel Vetter1286ff72012-05-10 15:25:09 +02003595 if (gem_obj->import_attach)
3596 drm_prime_gem_destroy(gem_obj, obj->sg_table);
3597
Chris Wilson1488fc02012-04-24 15:47:31 +01003598 if (obj->phys_obj)
3599 i915_gem_detach_phys_object(dev, obj);
3600
3601 obj->pin_count = 0;
3602 if (WARN_ON(i915_gem_object_unbind(obj) == -ERESTARTSYS)) {
3603 bool was_interruptible;
3604
3605 was_interruptible = dev_priv->mm.interruptible;
3606 dev_priv->mm.interruptible = false;
3607
3608 WARN_ON(i915_gem_object_unbind(obj));
3609
3610 dev_priv->mm.interruptible = was_interruptible;
3611 }
3612
Chris Wilson6c085a72012-08-20 11:40:46 +02003613 i915_gem_object_put_pages_gtt(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00003614 if (obj->base.map_list.map)
Rob Clarkb464e9a2011-08-10 08:09:08 -05003615 drm_gem_free_mmap_offset(&obj->base);
Chris Wilsonbe726152010-07-23 23:18:50 +01003616
Chris Wilson05394f32010-11-08 19:18:58 +00003617 drm_gem_object_release(&obj->base);
3618 i915_gem_info_remove_obj(dev_priv, obj->base.size);
Chris Wilsonbe726152010-07-23 23:18:50 +01003619
Chris Wilson05394f32010-11-08 19:18:58 +00003620 kfree(obj->bit_17);
3621 kfree(obj);
Chris Wilsonbe726152010-07-23 23:18:50 +01003622}
3623
Jesse Barnes5669fca2009-02-17 15:13:31 -08003624int
Eric Anholt673a3942008-07-30 12:06:12 -07003625i915_gem_idle(struct drm_device *dev)
3626{
3627 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson29105cc2010-01-07 10:39:13 +00003628 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003629
Keith Packard6dbe2772008-10-14 21:41:13 -07003630 mutex_lock(&dev->struct_mutex);
3631
Chris Wilson87acb0a2010-10-19 10:13:00 +01003632 if (dev_priv->mm.suspended) {
Keith Packard6dbe2772008-10-14 21:41:13 -07003633 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07003634 return 0;
Keith Packard6dbe2772008-10-14 21:41:13 -07003635 }
Eric Anholt673a3942008-07-30 12:06:12 -07003636
Ben Widawskyb2da9fe2012-04-26 16:02:58 -07003637 ret = i915_gpu_idle(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07003638 if (ret) {
3639 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07003640 return ret;
Keith Packard6dbe2772008-10-14 21:41:13 -07003641 }
Ben Widawskyb2da9fe2012-04-26 16:02:58 -07003642 i915_gem_retire_requests(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07003643
Chris Wilson29105cc2010-01-07 10:39:13 +00003644 /* Under UMS, be paranoid and evict. */
Chris Wilsona39d7ef2012-04-24 18:22:52 +01003645 if (!drm_core_check_feature(dev, DRIVER_MODESET))
Chris Wilson6c085a72012-08-20 11:40:46 +02003646 i915_gem_evict_everything(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00003647
Chris Wilson312817a2010-11-22 11:50:11 +00003648 i915_gem_reset_fences(dev);
3649
Chris Wilson29105cc2010-01-07 10:39:13 +00003650 /* Hack! Don't let anybody do execbuf while we don't control the chip.
3651 * We need to replace this with a semaphore, or something.
3652 * And not confound mm.suspended!
3653 */
3654 dev_priv->mm.suspended = 1;
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02003655 del_timer_sync(&dev_priv->hangcheck_timer);
Chris Wilson29105cc2010-01-07 10:39:13 +00003656
3657 i915_kernel_lost_context(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07003658 i915_gem_cleanup_ringbuffer(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00003659
Keith Packard6dbe2772008-10-14 21:41:13 -07003660 mutex_unlock(&dev->struct_mutex);
3661
Chris Wilson29105cc2010-01-07 10:39:13 +00003662 /* Cancel the retire work handler, which should be idle now. */
3663 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
3664
Eric Anholt673a3942008-07-30 12:06:12 -07003665 return 0;
3666}
3667
Ben Widawskyb9524a12012-05-25 16:56:24 -07003668void i915_gem_l3_remap(struct drm_device *dev)
3669{
3670 drm_i915_private_t *dev_priv = dev->dev_private;
3671 u32 misccpctl;
3672 int i;
3673
3674 if (!IS_IVYBRIDGE(dev))
3675 return;
3676
3677 if (!dev_priv->mm.l3_remap_info)
3678 return;
3679
3680 misccpctl = I915_READ(GEN7_MISCCPCTL);
3681 I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
3682 POSTING_READ(GEN7_MISCCPCTL);
3683
3684 for (i = 0; i < GEN7_L3LOG_SIZE; i += 4) {
3685 u32 remap = I915_READ(GEN7_L3LOG_BASE + i);
3686 if (remap && remap != dev_priv->mm.l3_remap_info[i/4])
3687 DRM_DEBUG("0x%x was already programmed to %x\n",
3688 GEN7_L3LOG_BASE + i, remap);
3689 if (remap && !dev_priv->mm.l3_remap_info[i/4])
3690 DRM_DEBUG_DRIVER("Clearing remapped register\n");
3691 I915_WRITE(GEN7_L3LOG_BASE + i, dev_priv->mm.l3_remap_info[i/4]);
3692 }
3693
3694 /* Make sure all the writes land before disabling dop clock gating */
3695 POSTING_READ(GEN7_L3LOG_BASE);
3696
3697 I915_WRITE(GEN7_MISCCPCTL, misccpctl);
3698}
3699
Daniel Vetterf691e2f2012-02-02 09:58:12 +01003700void i915_gem_init_swizzling(struct drm_device *dev)
3701{
3702 drm_i915_private_t *dev_priv = dev->dev_private;
3703
Daniel Vetter11782b02012-01-31 16:47:55 +01003704 if (INTEL_INFO(dev)->gen < 5 ||
Daniel Vetterf691e2f2012-02-02 09:58:12 +01003705 dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
3706 return;
3707
3708 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
3709 DISP_TILE_SURFACE_SWIZZLING);
3710
Daniel Vetter11782b02012-01-31 16:47:55 +01003711 if (IS_GEN5(dev))
3712 return;
3713
Daniel Vetterf691e2f2012-02-02 09:58:12 +01003714 I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
3715 if (IS_GEN6(dev))
Daniel Vetter6b26c862012-04-24 14:04:12 +02003716 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
Daniel Vetterf691e2f2012-02-02 09:58:12 +01003717 else
Daniel Vetter6b26c862012-04-24 14:04:12 +02003718 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
Daniel Vetterf691e2f2012-02-02 09:58:12 +01003719}
Daniel Vettere21af882012-02-09 20:53:27 +01003720
3721void i915_gem_init_ppgtt(struct drm_device *dev)
3722{
3723 drm_i915_private_t *dev_priv = dev->dev_private;
3724 uint32_t pd_offset;
3725 struct intel_ring_buffer *ring;
Daniel Vetter55a254a2012-03-22 00:14:43 +01003726 struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
3727 uint32_t __iomem *pd_addr;
3728 uint32_t pd_entry;
Daniel Vettere21af882012-02-09 20:53:27 +01003729 int i;
3730
3731 if (!dev_priv->mm.aliasing_ppgtt)
3732 return;
3733
Daniel Vetter55a254a2012-03-22 00:14:43 +01003734
3735 pd_addr = dev_priv->mm.gtt->gtt + ppgtt->pd_offset/sizeof(uint32_t);
3736 for (i = 0; i < ppgtt->num_pd_entries; i++) {
3737 dma_addr_t pt_addr;
3738
3739 if (dev_priv->mm.gtt->needs_dmar)
3740 pt_addr = ppgtt->pt_dma_addr[i];
3741 else
3742 pt_addr = page_to_phys(ppgtt->pt_pages[i]);
3743
3744 pd_entry = GEN6_PDE_ADDR_ENCODE(pt_addr);
3745 pd_entry |= GEN6_PDE_VALID;
3746
3747 writel(pd_entry, pd_addr + i);
3748 }
3749 readl(pd_addr);
3750
3751 pd_offset = ppgtt->pd_offset;
Daniel Vettere21af882012-02-09 20:53:27 +01003752 pd_offset /= 64; /* in cachelines, */
3753 pd_offset <<= 16;
3754
3755 if (INTEL_INFO(dev)->gen == 6) {
Daniel Vetter48ecfa12012-04-11 20:42:40 +02003756 uint32_t ecochk, gab_ctl, ecobits;
3757
3758 ecobits = I915_READ(GAC_ECO_BITS);
3759 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B);
Daniel Vetterbe901a52012-04-11 20:42:39 +02003760
3761 gab_ctl = I915_READ(GAB_CTL);
3762 I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT);
3763
3764 ecochk = I915_READ(GAM_ECOCHK);
Daniel Vettere21af882012-02-09 20:53:27 +01003765 I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT |
3766 ECOCHK_PPGTT_CACHE64B);
Daniel Vetter6b26c862012-04-24 14:04:12 +02003767 I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Daniel Vettere21af882012-02-09 20:53:27 +01003768 } else if (INTEL_INFO(dev)->gen >= 7) {
3769 I915_WRITE(GAM_ECOCHK, ECOCHK_PPGTT_CACHE64B);
3770 /* GFX_MODE is per-ring on gen7+ */
3771 }
3772
Chris Wilsonb4519512012-05-11 14:29:30 +01003773 for_each_ring(ring, dev_priv, i) {
Daniel Vettere21af882012-02-09 20:53:27 +01003774 if (INTEL_INFO(dev)->gen >= 7)
3775 I915_WRITE(RING_MODE_GEN7(ring),
Daniel Vetter6b26c862012-04-24 14:04:12 +02003776 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Daniel Vettere21af882012-02-09 20:53:27 +01003777
3778 I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
3779 I915_WRITE(RING_PP_DIR_BASE(ring), pd_offset);
3780 }
3781}
3782
Chris Wilson67b1b572012-07-05 23:49:40 +01003783static bool
3784intel_enable_blt(struct drm_device *dev)
3785{
3786 if (!HAS_BLT(dev))
3787 return false;
3788
3789 /* The blitter was dysfunctional on early prototypes */
3790 if (IS_GEN6(dev) && dev->pdev->revision < 8) {
3791 DRM_INFO("BLT not supported on this pre-production hardware;"
3792 " graphics performance will be degraded.\n");
3793 return false;
3794 }
3795
3796 return true;
3797}
3798
Eric Anholt673a3942008-07-30 12:06:12 -07003799int
Daniel Vetterf691e2f2012-02-02 09:58:12 +01003800i915_gem_init_hw(struct drm_device *dev)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08003801{
3802 drm_i915_private_t *dev_priv = dev->dev_private;
3803 int ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01003804
Daniel Vetter8ecd1a62012-06-07 15:56:03 +02003805 if (!intel_enable_gtt())
3806 return -EIO;
3807
Ben Widawskyb9524a12012-05-25 16:56:24 -07003808 i915_gem_l3_remap(dev);
3809
Daniel Vetterf691e2f2012-02-02 09:58:12 +01003810 i915_gem_init_swizzling(dev);
3811
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08003812 ret = intel_init_render_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01003813 if (ret)
Chris Wilsonb6913e42010-11-12 10:46:37 +00003814 return ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01003815
3816 if (HAS_BSD(dev)) {
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08003817 ret = intel_init_bsd_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01003818 if (ret)
3819 goto cleanup_render_ring;
Zou Nan haid1b851f2010-05-21 09:08:57 +08003820 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01003821
Chris Wilson67b1b572012-07-05 23:49:40 +01003822 if (intel_enable_blt(dev)) {
Chris Wilson549f7362010-10-19 11:19:32 +01003823 ret = intel_init_blt_ring_buffer(dev);
3824 if (ret)
3825 goto cleanup_bsd_ring;
3826 }
3827
Chris Wilson6f392d5482010-08-07 11:01:22 +01003828 dev_priv->next_seqno = 1;
3829
Ben Widawsky254f9652012-06-04 14:42:42 -07003830 /*
3831 * XXX: There was some w/a described somewhere suggesting loading
3832 * contexts before PPGTT.
3833 */
3834 i915_gem_context_init(dev);
Daniel Vettere21af882012-02-09 20:53:27 +01003835 i915_gem_init_ppgtt(dev);
3836
Chris Wilson68f95ba2010-05-27 13:18:22 +01003837 return 0;
3838
Chris Wilson549f7362010-10-19 11:19:32 +01003839cleanup_bsd_ring:
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003840 intel_cleanup_ring_buffer(&dev_priv->ring[VCS]);
Chris Wilson68f95ba2010-05-27 13:18:22 +01003841cleanup_render_ring:
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003842 intel_cleanup_ring_buffer(&dev_priv->ring[RCS]);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08003843 return ret;
3844}
3845
Chris Wilson1070a422012-04-24 15:47:41 +01003846static bool
3847intel_enable_ppgtt(struct drm_device *dev)
3848{
3849 if (i915_enable_ppgtt >= 0)
3850 return i915_enable_ppgtt;
3851
3852#ifdef CONFIG_INTEL_IOMMU
3853 /* Disable ppgtt on SNB if VT-d is on. */
3854 if (INTEL_INFO(dev)->gen == 6 && intel_iommu_gfx_mapped)
3855 return false;
3856#endif
3857
3858 return true;
3859}
3860
3861int i915_gem_init(struct drm_device *dev)
3862{
3863 struct drm_i915_private *dev_priv = dev->dev_private;
3864 unsigned long gtt_size, mappable_size;
3865 int ret;
3866
3867 gtt_size = dev_priv->mm.gtt->gtt_total_entries << PAGE_SHIFT;
3868 mappable_size = dev_priv->mm.gtt->gtt_mappable_entries << PAGE_SHIFT;
3869
3870 mutex_lock(&dev->struct_mutex);
3871 if (intel_enable_ppgtt(dev) && HAS_ALIASING_PPGTT(dev)) {
3872 /* PPGTT pdes are stolen from global gtt ptes, so shrink the
3873 * aperture accordingly when using aliasing ppgtt. */
3874 gtt_size -= I915_PPGTT_PD_ENTRIES*PAGE_SIZE;
3875
3876 i915_gem_init_global_gtt(dev, 0, mappable_size, gtt_size);
3877
3878 ret = i915_gem_init_aliasing_ppgtt(dev);
3879 if (ret) {
3880 mutex_unlock(&dev->struct_mutex);
3881 return ret;
3882 }
3883 } else {
3884 /* Let GEM Manage all of the aperture.
3885 *
3886 * However, leave one page at the end still bound to the scratch
3887 * page. There are a number of places where the hardware
3888 * apparently prefetches past the end of the object, and we've
3889 * seen multiple hangs with the GPU head pointer stuck in a
3890 * batchbuffer bound at the last page of the aperture. One page
3891 * should be enough to keep any prefetching inside of the
3892 * aperture.
3893 */
3894 i915_gem_init_global_gtt(dev, 0, mappable_size,
3895 gtt_size);
3896 }
3897
3898 ret = i915_gem_init_hw(dev);
3899 mutex_unlock(&dev->struct_mutex);
3900 if (ret) {
3901 i915_gem_cleanup_aliasing_ppgtt(dev);
3902 return ret;
3903 }
3904
Daniel Vetter53ca26c2012-04-26 23:28:03 +02003905 /* Allow hardware batchbuffers unless told otherwise, but not for KMS. */
3906 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3907 dev_priv->dri1.allow_batchbuffer = 1;
Chris Wilson1070a422012-04-24 15:47:41 +01003908 return 0;
3909}
3910
Zou Nan hai8187a2b2010-05-21 09:08:55 +08003911void
3912i915_gem_cleanup_ringbuffer(struct drm_device *dev)
3913{
3914 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonb4519512012-05-11 14:29:30 +01003915 struct intel_ring_buffer *ring;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003916 int i;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08003917
Chris Wilsonb4519512012-05-11 14:29:30 +01003918 for_each_ring(ring, dev_priv, i)
3919 intel_cleanup_ring_buffer(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08003920}
3921
3922int
Eric Anholt673a3942008-07-30 12:06:12 -07003923i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
3924 struct drm_file *file_priv)
3925{
3926 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonb4519512012-05-11 14:29:30 +01003927 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003928
Jesse Barnes79e53942008-11-07 14:24:08 -08003929 if (drm_core_check_feature(dev, DRIVER_MODESET))
3930 return 0;
3931
Ben Gamariba1234d2009-09-14 17:48:47 -04003932 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07003933 DRM_ERROR("Reenabling wedged hardware, good luck\n");
Ben Gamariba1234d2009-09-14 17:48:47 -04003934 atomic_set(&dev_priv->mm.wedged, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07003935 }
3936
Eric Anholt673a3942008-07-30 12:06:12 -07003937 mutex_lock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08003938 dev_priv->mm.suspended = 0;
3939
Daniel Vetterf691e2f2012-02-02 09:58:12 +01003940 ret = i915_gem_init_hw(dev);
Wu Fengguangd816f6a2009-04-18 10:43:32 +08003941 if (ret != 0) {
3942 mutex_unlock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08003943 return ret;
Wu Fengguangd816f6a2009-04-18 10:43:32 +08003944 }
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08003945
Chris Wilson69dc4982010-10-19 10:36:51 +01003946 BUG_ON(!list_empty(&dev_priv->mm.active_list));
Eric Anholt673a3942008-07-30 12:06:12 -07003947 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
Eric Anholt673a3942008-07-30 12:06:12 -07003948 mutex_unlock(&dev->struct_mutex);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04003949
Chris Wilson5f353082010-06-07 14:03:03 +01003950 ret = drm_irq_install(dev);
3951 if (ret)
3952 goto cleanup_ringbuffer;
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04003953
Eric Anholt673a3942008-07-30 12:06:12 -07003954 return 0;
Chris Wilson5f353082010-06-07 14:03:03 +01003955
3956cleanup_ringbuffer:
3957 mutex_lock(&dev->struct_mutex);
3958 i915_gem_cleanup_ringbuffer(dev);
3959 dev_priv->mm.suspended = 1;
3960 mutex_unlock(&dev->struct_mutex);
3961
3962 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003963}
3964
3965int
3966i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
3967 struct drm_file *file_priv)
3968{
Jesse Barnes79e53942008-11-07 14:24:08 -08003969 if (drm_core_check_feature(dev, DRIVER_MODESET))
3970 return 0;
3971
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04003972 drm_irq_uninstall(dev);
Linus Torvaldse6890f62009-09-08 17:09:24 -07003973 return i915_gem_idle(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07003974}
3975
3976void
3977i915_gem_lastclose(struct drm_device *dev)
3978{
3979 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003980
Eric Anholte806b492009-01-22 09:56:58 -08003981 if (drm_core_check_feature(dev, DRIVER_MODESET))
3982 return;
3983
Keith Packard6dbe2772008-10-14 21:41:13 -07003984 ret = i915_gem_idle(dev);
3985 if (ret)
3986 DRM_ERROR("failed to idle hardware: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07003987}
3988
Chris Wilson64193402010-10-24 12:38:05 +01003989static void
3990init_ring_lists(struct intel_ring_buffer *ring)
3991{
3992 INIT_LIST_HEAD(&ring->active_list);
3993 INIT_LIST_HEAD(&ring->request_list);
Chris Wilson64193402010-10-24 12:38:05 +01003994}
3995
Eric Anholt673a3942008-07-30 12:06:12 -07003996void
3997i915_gem_load(struct drm_device *dev)
3998{
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02003999 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07004000 drm_i915_private_t *dev_priv = dev->dev_private;
4001
Chris Wilson69dc4982010-10-19 10:36:51 +01004002 INIT_LIST_HEAD(&dev_priv->mm.active_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004003 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
Chris Wilson6c085a72012-08-20 11:40:46 +02004004 INIT_LIST_HEAD(&dev_priv->mm.unbound_list);
4005 INIT_LIST_HEAD(&dev_priv->mm.bound_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07004006 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00004007 for (i = 0; i < I915_NUM_RINGS; i++)
4008 init_ring_lists(&dev_priv->ring[i]);
Daniel Vetter4b9de732011-10-09 21:52:02 +02004009 for (i = 0; i < I915_MAX_NUM_FENCES; i++)
Daniel Vetter007cc8a2010-04-28 11:02:31 +02004010 INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004011 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
4012 i915_gem_retire_work_handler);
Chris Wilson30dbf0c2010-09-25 10:19:17 +01004013 init_completion(&dev_priv->error_completion);
Chris Wilson31169712009-09-14 16:50:28 +01004014
Dave Airlie94400122010-07-20 13:15:31 +10004015 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
4016 if (IS_GEN3(dev)) {
Daniel Vetter50743292012-04-26 22:02:54 +02004017 I915_WRITE(MI_ARB_STATE,
4018 _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
Dave Airlie94400122010-07-20 13:15:31 +10004019 }
4020
Chris Wilson72bfa192010-12-19 11:42:05 +00004021 dev_priv->relative_constants_mode = I915_EXEC_CONSTANTS_REL_GENERAL;
4022
Jesse Barnesde151cf2008-11-12 10:03:55 -08004023 /* Old X drivers will take 0-2 for front, back, depth buffers */
Eric Anholtb397c832010-01-26 09:43:10 -08004024 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4025 dev_priv->fence_reg_start = 3;
Jesse Barnesde151cf2008-11-12 10:03:55 -08004026
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004027 if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08004028 dev_priv->num_fence_regs = 16;
4029 else
4030 dev_priv->num_fence_regs = 8;
4031
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004032 /* Initialize fence registers to zero */
Chris Wilsonada726c2012-04-17 15:31:32 +01004033 i915_gem_reset_fences(dev);
Eric Anholt10ed13e2011-05-06 13:53:49 -07004034
Eric Anholt673a3942008-07-30 12:06:12 -07004035 i915_gem_detect_bit_6_swizzle(dev);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05004036 init_waitqueue_head(&dev_priv->pending_flip_queue);
Chris Wilson17250b72010-10-28 12:51:39 +01004037
Chris Wilsonce453d82011-02-21 14:43:56 +00004038 dev_priv->mm.interruptible = true;
4039
Chris Wilson17250b72010-10-28 12:51:39 +01004040 dev_priv->mm.inactive_shrinker.shrink = i915_gem_inactive_shrink;
4041 dev_priv->mm.inactive_shrinker.seeks = DEFAULT_SEEKS;
4042 register_shrinker(&dev_priv->mm.inactive_shrinker);
Eric Anholt673a3942008-07-30 12:06:12 -07004043}
Dave Airlie71acb5e2008-12-30 20:31:46 +10004044
4045/*
4046 * Create a physically contiguous memory object for this object
4047 * e.g. for cursor + overlay regs
4048 */
Chris Wilson995b6762010-08-20 13:23:26 +01004049static int i915_gem_init_phys_object(struct drm_device *dev,
4050 int id, int size, int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004051{
4052 drm_i915_private_t *dev_priv = dev->dev_private;
4053 struct drm_i915_gem_phys_object *phys_obj;
4054 int ret;
4055
4056 if (dev_priv->mm.phys_objs[id - 1] || !size)
4057 return 0;
4058
Eric Anholt9a298b22009-03-24 12:23:04 -07004059 phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004060 if (!phys_obj)
4061 return -ENOMEM;
4062
4063 phys_obj->id = id;
4064
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004065 phys_obj->handle = drm_pci_alloc(dev, size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004066 if (!phys_obj->handle) {
4067 ret = -ENOMEM;
4068 goto kfree_obj;
4069 }
4070#ifdef CONFIG_X86
4071 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4072#endif
4073
4074 dev_priv->mm.phys_objs[id - 1] = phys_obj;
4075
4076 return 0;
4077kfree_obj:
Eric Anholt9a298b22009-03-24 12:23:04 -07004078 kfree(phys_obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004079 return ret;
4080}
4081
Chris Wilson995b6762010-08-20 13:23:26 +01004082static void i915_gem_free_phys_object(struct drm_device *dev, int id)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004083{
4084 drm_i915_private_t *dev_priv = dev->dev_private;
4085 struct drm_i915_gem_phys_object *phys_obj;
4086
4087 if (!dev_priv->mm.phys_objs[id - 1])
4088 return;
4089
4090 phys_obj = dev_priv->mm.phys_objs[id - 1];
4091 if (phys_obj->cur_obj) {
4092 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
4093 }
4094
4095#ifdef CONFIG_X86
4096 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4097#endif
4098 drm_pci_free(dev, phys_obj->handle);
4099 kfree(phys_obj);
4100 dev_priv->mm.phys_objs[id - 1] = NULL;
4101}
4102
4103void i915_gem_free_all_phys_object(struct drm_device *dev)
4104{
4105 int i;
4106
Dave Airlie260883c2009-01-22 17:58:49 +10004107 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004108 i915_gem_free_phys_object(dev, i);
4109}
4110
4111void i915_gem_detach_phys_object(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00004112 struct drm_i915_gem_object *obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004113{
Chris Wilson05394f32010-11-08 19:18:58 +00004114 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Chris Wilsone5281cc2010-10-28 13:45:36 +01004115 char *vaddr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004116 int i;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004117 int page_count;
4118
Chris Wilson05394f32010-11-08 19:18:58 +00004119 if (!obj->phys_obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004120 return;
Chris Wilson05394f32010-11-08 19:18:58 +00004121 vaddr = obj->phys_obj->handle->vaddr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004122
Chris Wilson05394f32010-11-08 19:18:58 +00004123 page_count = obj->base.size / PAGE_SIZE;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004124 for (i = 0; i < page_count; i++) {
Hugh Dickins5949eac2011-06-27 16:18:18 -07004125 struct page *page = shmem_read_mapping_page(mapping, i);
Chris Wilsone5281cc2010-10-28 13:45:36 +01004126 if (!IS_ERR(page)) {
4127 char *dst = kmap_atomic(page);
4128 memcpy(dst, vaddr + i*PAGE_SIZE, PAGE_SIZE);
4129 kunmap_atomic(dst);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004130
Chris Wilsone5281cc2010-10-28 13:45:36 +01004131 drm_clflush_pages(&page, 1);
4132
4133 set_page_dirty(page);
4134 mark_page_accessed(page);
4135 page_cache_release(page);
4136 }
Dave Airlie71acb5e2008-12-30 20:31:46 +10004137 }
Daniel Vetter40ce6572010-11-05 18:12:18 +01004138 intel_gtt_chipset_flush();
Chris Wilsond78b47b2009-06-17 21:52:49 +01004139
Chris Wilson05394f32010-11-08 19:18:58 +00004140 obj->phys_obj->cur_obj = NULL;
4141 obj->phys_obj = NULL;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004142}
4143
4144int
4145i915_gem_attach_phys_object(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00004146 struct drm_i915_gem_object *obj,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004147 int id,
4148 int align)
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;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004151 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004152 int ret = 0;
4153 int page_count;
4154 int i;
4155
4156 if (id > I915_MAX_PHYS_OBJECT)
4157 return -EINVAL;
4158
Chris Wilson05394f32010-11-08 19:18:58 +00004159 if (obj->phys_obj) {
4160 if (obj->phys_obj->id == id)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004161 return 0;
4162 i915_gem_detach_phys_object(dev, obj);
4163 }
4164
Dave Airlie71acb5e2008-12-30 20:31:46 +10004165 /* create a new object */
4166 if (!dev_priv->mm.phys_objs[id - 1]) {
4167 ret = i915_gem_init_phys_object(dev, id,
Chris Wilson05394f32010-11-08 19:18:58 +00004168 obj->base.size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004169 if (ret) {
Chris Wilson05394f32010-11-08 19:18:58 +00004170 DRM_ERROR("failed to init phys object %d size: %zu\n",
4171 id, obj->base.size);
Chris Wilsone5281cc2010-10-28 13:45:36 +01004172 return ret;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004173 }
4174 }
4175
4176 /* bind to the object */
Chris Wilson05394f32010-11-08 19:18:58 +00004177 obj->phys_obj = dev_priv->mm.phys_objs[id - 1];
4178 obj->phys_obj->cur_obj = obj;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004179
Chris Wilson05394f32010-11-08 19:18:58 +00004180 page_count = obj->base.size / PAGE_SIZE;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004181
4182 for (i = 0; i < page_count; i++) {
Chris Wilsone5281cc2010-10-28 13:45:36 +01004183 struct page *page;
4184 char *dst, *src;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004185
Hugh Dickins5949eac2011-06-27 16:18:18 -07004186 page = shmem_read_mapping_page(mapping, i);
Chris Wilsone5281cc2010-10-28 13:45:36 +01004187 if (IS_ERR(page))
4188 return PTR_ERR(page);
4189
Chris Wilsonff75b9b2010-10-30 22:52:31 +01004190 src = kmap_atomic(page);
Chris Wilson05394f32010-11-08 19:18:58 +00004191 dst = obj->phys_obj->handle->vaddr + (i * PAGE_SIZE);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004192 memcpy(dst, src, PAGE_SIZE);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -07004193 kunmap_atomic(src);
Chris Wilsone5281cc2010-10-28 13:45:36 +01004194
4195 mark_page_accessed(page);
4196 page_cache_release(page);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004197 }
4198
4199 return 0;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004200}
4201
4202static int
Chris Wilson05394f32010-11-08 19:18:58 +00004203i915_gem_phys_pwrite(struct drm_device *dev,
4204 struct drm_i915_gem_object *obj,
Dave Airlie71acb5e2008-12-30 20:31:46 +10004205 struct drm_i915_gem_pwrite *args,
4206 struct drm_file *file_priv)
4207{
Chris Wilson05394f32010-11-08 19:18:58 +00004208 void *vaddr = obj->phys_obj->handle->vaddr + args->offset;
Chris Wilsonb47b30c2010-11-08 01:12:29 +00004209 char __user *user_data = (char __user *) (uintptr_t) args->data_ptr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004210
Chris Wilsonb47b30c2010-11-08 01:12:29 +00004211 if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) {
4212 unsigned long unwritten;
4213
4214 /* The physical object once assigned is fixed for the lifetime
4215 * of the obj, so we can safely drop the lock and continue
4216 * to access vaddr.
4217 */
4218 mutex_unlock(&dev->struct_mutex);
4219 unwritten = copy_from_user(vaddr, user_data, args->size);
4220 mutex_lock(&dev->struct_mutex);
4221 if (unwritten)
4222 return -EFAULT;
4223 }
Dave Airlie71acb5e2008-12-30 20:31:46 +10004224
Daniel Vetter40ce6572010-11-05 18:12:18 +01004225 intel_gtt_chipset_flush();
Dave Airlie71acb5e2008-12-30 20:31:46 +10004226 return 0;
4227}
Eric Anholtb9624422009-06-03 07:27:35 +00004228
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004229void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00004230{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004231 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00004232
4233 /* Clean up our request list when the client is going away, so that
4234 * later retire_requests won't dereference our soon-to-be-gone
4235 * file_priv.
4236 */
Chris Wilson1c255952010-09-26 11:03:27 +01004237 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004238 while (!list_empty(&file_priv->mm.request_list)) {
4239 struct drm_i915_gem_request *request;
4240
4241 request = list_first_entry(&file_priv->mm.request_list,
4242 struct drm_i915_gem_request,
4243 client_list);
4244 list_del(&request->client_list);
4245 request->file_priv = NULL;
4246 }
Chris Wilson1c255952010-09-26 11:03:27 +01004247 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00004248}
Chris Wilson31169712009-09-14 16:50:28 +01004249
Chris Wilson31169712009-09-14 16:50:28 +01004250static int
Ying Han1495f232011-05-24 17:12:27 -07004251i915_gem_inactive_shrink(struct shrinker *shrinker, struct shrink_control *sc)
Chris Wilson31169712009-09-14 16:50:28 +01004252{
Chris Wilson17250b72010-10-28 12:51:39 +01004253 struct drm_i915_private *dev_priv =
4254 container_of(shrinker,
4255 struct drm_i915_private,
4256 mm.inactive_shrinker);
4257 struct drm_device *dev = dev_priv->dev;
Chris Wilson6c085a72012-08-20 11:40:46 +02004258 struct drm_i915_gem_object *obj;
Ying Han1495f232011-05-24 17:12:27 -07004259 int nr_to_scan = sc->nr_to_scan;
Chris Wilson17250b72010-10-28 12:51:39 +01004260 int cnt;
4261
4262 if (!mutex_trylock(&dev->struct_mutex))
Chris Wilsonbbe2e112010-10-28 22:35:07 +01004263 return 0;
Chris Wilson31169712009-09-14 16:50:28 +01004264
Chris Wilson6c085a72012-08-20 11:40:46 +02004265 if (nr_to_scan) {
4266 nr_to_scan -= i915_gem_purge(dev_priv, nr_to_scan);
4267 if (nr_to_scan > 0)
4268 i915_gem_shrink_all(dev_priv);
Chris Wilson31169712009-09-14 16:50:28 +01004269 }
4270
Chris Wilson17250b72010-10-28 12:51:39 +01004271 cnt = 0;
Chris Wilson6c085a72012-08-20 11:40:46 +02004272 list_for_each_entry(obj, &dev_priv->mm.unbound_list, gtt_list)
4273 cnt += obj->base.size >> PAGE_SHIFT;
4274 list_for_each_entry(obj, &dev_priv->mm.bound_list, gtt_list)
4275 if (obj->pin_count == 0)
4276 cnt += obj->base.size >> PAGE_SHIFT;
Chris Wilson31169712009-09-14 16:50:28 +01004277
Chris Wilson17250b72010-10-28 12:51:39 +01004278 mutex_unlock(&dev->struct_mutex);
Chris Wilson6c085a72012-08-20 11:40:46 +02004279 return cnt;
Chris Wilson31169712009-09-14 16:50:28 +01004280}