blob: 26c8bf9c5fa6a3304a69489d8a073a9b8a80621e [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,
Chris Wilson86a1ee22012-08-11 15:41:04 +010044 bool map_and_fenceable,
45 bool nonblocking);
Chris Wilson05394f32010-11-08 19:18:58 +000046static int i915_gem_phys_pwrite(struct drm_device *dev,
47 struct drm_i915_gem_object *obj,
Dave Airlie71acb5e2008-12-30 20:31:46 +100048 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +000049 struct drm_file *file);
Eric Anholt673a3942008-07-30 12:06:12 -070050
Chris Wilson61050802012-04-17 15:31:31 +010051static void i915_gem_write_fence(struct drm_device *dev, int reg,
52 struct drm_i915_gem_object *obj);
53static void i915_gem_object_update_fence(struct drm_i915_gem_object *obj,
54 struct drm_i915_fence_reg *fence,
55 bool enable);
56
Chris Wilson17250b72010-10-28 12:51:39 +010057static int i915_gem_inactive_shrink(struct shrinker *shrinker,
Ying Han1495f232011-05-24 17:12:27 -070058 struct shrink_control *sc);
Chris Wilson6c085a72012-08-20 11:40:46 +020059static long i915_gem_purge(struct drm_i915_private *dev_priv, long target);
60static void i915_gem_shrink_all(struct drm_i915_private *dev_priv);
Daniel Vetter8c599672011-12-14 13:57:31 +010061static void i915_gem_object_truncate(struct drm_i915_gem_object *obj);
Chris Wilson31169712009-09-14 16:50:28 +010062
Chris Wilson61050802012-04-17 15:31:31 +010063static inline void i915_gem_object_fence_lost(struct drm_i915_gem_object *obj)
64{
65 if (obj->tiling_mode)
66 i915_gem_release_mmap(obj);
67
68 /* As we do not have an associated fence register, we will force
69 * a tiling change if we ever need to acquire one.
70 */
Chris Wilson5d82e3e2012-04-21 16:23:23 +010071 obj->fence_dirty = false;
Chris Wilson61050802012-04-17 15:31:31 +010072 obj->fence_reg = I915_FENCE_REG_NONE;
73}
74
Chris Wilson73aa8082010-09-30 11:46:12 +010075/* some bookkeeping */
76static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
77 size_t size)
78{
79 dev_priv->mm.object_count++;
80 dev_priv->mm.object_memory += size;
81}
82
83static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
84 size_t size)
85{
86 dev_priv->mm.object_count--;
87 dev_priv->mm.object_memory -= size;
88}
89
Chris Wilson21dd3732011-01-26 15:55:56 +000090static int
91i915_gem_wait_for_error(struct drm_device *dev)
Chris Wilson30dbf0c2010-09-25 10:19:17 +010092{
93 struct drm_i915_private *dev_priv = dev->dev_private;
94 struct completion *x = &dev_priv->error_completion;
95 unsigned long flags;
96 int ret;
97
98 if (!atomic_read(&dev_priv->mm.wedged))
99 return 0;
100
Daniel Vetter0a6759c2012-07-04 22:18:41 +0200101 /*
102 * Only wait 10 seconds for the gpu reset to complete to avoid hanging
103 * userspace. If it takes that long something really bad is going on and
104 * we should simply try to bail out and fail as gracefully as possible.
105 */
106 ret = wait_for_completion_interruptible_timeout(x, 10*HZ);
107 if (ret == 0) {
108 DRM_ERROR("Timed out waiting for the gpu reset to complete\n");
109 return -EIO;
110 } else if (ret < 0) {
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100111 return ret;
Daniel Vetter0a6759c2012-07-04 22:18:41 +0200112 }
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100113
Chris Wilson21dd3732011-01-26 15:55:56 +0000114 if (atomic_read(&dev_priv->mm.wedged)) {
115 /* GPU is hung, bump the completion count to account for
116 * the token we just consumed so that we never hit zero and
117 * end up waiting upon a subsequent completion event that
118 * will never happen.
119 */
120 spin_lock_irqsave(&x->wait.lock, flags);
121 x->done++;
122 spin_unlock_irqrestore(&x->wait.lock, flags);
123 }
124 return 0;
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100125}
126
Chris Wilson54cf91d2010-11-25 18:00:26 +0000127int i915_mutex_lock_interruptible(struct drm_device *dev)
Chris Wilson76c1dec2010-09-25 11:22:51 +0100128{
Chris Wilson76c1dec2010-09-25 11:22:51 +0100129 int ret;
130
Chris Wilson21dd3732011-01-26 15:55:56 +0000131 ret = i915_gem_wait_for_error(dev);
Chris Wilson76c1dec2010-09-25 11:22:51 +0100132 if (ret)
133 return ret;
134
135 ret = mutex_lock_interruptible(&dev->struct_mutex);
136 if (ret)
137 return ret;
138
Chris Wilson23bc5982010-09-29 16:10:57 +0100139 WARN_ON(i915_verify_lists(dev));
Chris Wilson76c1dec2010-09-25 11:22:51 +0100140 return 0;
141}
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100142
Chris Wilson7d1c4802010-08-07 21:45:03 +0100143static inline bool
Chris Wilson05394f32010-11-08 19:18:58 +0000144i915_gem_object_is_inactive(struct drm_i915_gem_object *obj)
Chris Wilson7d1c4802010-08-07 21:45:03 +0100145{
Chris Wilson6c085a72012-08-20 11:40:46 +0200146 return obj->gtt_space && !obj->active;
Chris Wilson7d1c4802010-08-07 21:45:03 +0100147}
148
Eric Anholt673a3942008-07-30 12:06:12 -0700149int
150i915_gem_init_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000151 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700152{
Eric Anholt673a3942008-07-30 12:06:12 -0700153 struct drm_i915_gem_init *args = data;
Chris Wilson20217462010-11-23 15:26:33 +0000154
Daniel Vetter7bb6fb82012-04-24 08:22:52 +0200155 if (drm_core_check_feature(dev, DRIVER_MODESET))
156 return -ENODEV;
157
Chris Wilson20217462010-11-23 15:26:33 +0000158 if (args->gtt_start >= args->gtt_end ||
159 (args->gtt_end | args->gtt_start) & (PAGE_SIZE - 1))
160 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -0700161
Daniel Vetterf534bc02012-03-26 22:37:04 +0200162 /* GEM with user mode setting was never supported on ilk and later. */
163 if (INTEL_INFO(dev)->gen >= 5)
164 return -ENODEV;
165
Eric Anholt673a3942008-07-30 12:06:12 -0700166 mutex_lock(&dev->struct_mutex);
Daniel Vetter644ec022012-03-26 09:45:40 +0200167 i915_gem_init_global_gtt(dev, args->gtt_start,
168 args->gtt_end, args->gtt_end);
Eric Anholt673a3942008-07-30 12:06:12 -0700169 mutex_unlock(&dev->struct_mutex);
170
Chris Wilson20217462010-11-23 15:26:33 +0000171 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700172}
173
Eric Anholt5a125c32008-10-22 21:40:13 -0700174int
175i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000176 struct drm_file *file)
Eric Anholt5a125c32008-10-22 21:40:13 -0700177{
Chris Wilson73aa8082010-09-30 11:46:12 +0100178 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt5a125c32008-10-22 21:40:13 -0700179 struct drm_i915_gem_get_aperture *args = data;
Chris Wilson6299f992010-11-24 12:23:44 +0000180 struct drm_i915_gem_object *obj;
181 size_t pinned;
Eric Anholt5a125c32008-10-22 21:40:13 -0700182
Chris Wilson6299f992010-11-24 12:23:44 +0000183 pinned = 0;
Chris Wilson73aa8082010-09-30 11:46:12 +0100184 mutex_lock(&dev->struct_mutex);
Chris Wilson6c085a72012-08-20 11:40:46 +0200185 list_for_each_entry(obj, &dev_priv->mm.bound_list, gtt_list)
Chris Wilson1b502472012-04-24 15:47:30 +0100186 if (obj->pin_count)
187 pinned += obj->gtt_space->size;
Chris Wilson73aa8082010-09-30 11:46:12 +0100188 mutex_unlock(&dev->struct_mutex);
Eric Anholt5a125c32008-10-22 21:40:13 -0700189
Chris Wilson6299f992010-11-24 12:23:44 +0000190 args->aper_size = dev_priv->mm.gtt_total;
Akshay Joshi0206e352011-08-16 15:34:10 -0400191 args->aper_available_size = args->aper_size - pinned;
Chris Wilson6299f992010-11-24 12:23:44 +0000192
Eric Anholt5a125c32008-10-22 21:40:13 -0700193 return 0;
194}
195
Dave Airlieff72145b2011-02-07 12:16:14 +1000196static int
197i915_gem_create(struct drm_file *file,
198 struct drm_device *dev,
199 uint64_t size,
200 uint32_t *handle_p)
Eric Anholt673a3942008-07-30 12:06:12 -0700201{
Chris Wilson05394f32010-11-08 19:18:58 +0000202 struct drm_i915_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300203 int ret;
204 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700205
Dave Airlieff72145b2011-02-07 12:16:14 +1000206 size = roundup(size, PAGE_SIZE);
Chris Wilson8ffc0242011-09-14 14:14:28 +0200207 if (size == 0)
208 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -0700209
210 /* Allocate the new object */
Dave Airlieff72145b2011-02-07 12:16:14 +1000211 obj = i915_gem_alloc_object(dev, size);
Eric Anholt673a3942008-07-30 12:06:12 -0700212 if (obj == NULL)
213 return -ENOMEM;
214
Chris Wilson05394f32010-11-08 19:18:58 +0000215 ret = drm_gem_handle_create(file, &obj->base, &handle);
Chris Wilson1dfd9752010-09-06 14:44:14 +0100216 if (ret) {
Chris Wilson05394f32010-11-08 19:18:58 +0000217 drm_gem_object_release(&obj->base);
218 i915_gem_info_remove_obj(dev->dev_private, obj->base.size);
Chris Wilson202f2fe2010-10-14 13:20:40 +0100219 kfree(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700220 return ret;
Chris Wilson1dfd9752010-09-06 14:44:14 +0100221 }
222
Chris Wilson202f2fe2010-10-14 13:20:40 +0100223 /* drop reference from allocate - handle holds it now */
Chris Wilson05394f32010-11-08 19:18:58 +0000224 drm_gem_object_unreference(&obj->base);
Chris Wilson202f2fe2010-10-14 13:20:40 +0100225 trace_i915_gem_object_create(obj);
226
Dave Airlieff72145b2011-02-07 12:16:14 +1000227 *handle_p = handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700228 return 0;
229}
230
Dave Airlieff72145b2011-02-07 12:16:14 +1000231int
232i915_gem_dumb_create(struct drm_file *file,
233 struct drm_device *dev,
234 struct drm_mode_create_dumb *args)
235{
236 /* have to work out size/pitch and return them */
Chris Wilsoned0291f2011-03-19 08:21:45 +0000237 args->pitch = ALIGN(args->width * ((args->bpp + 7) / 8), 64);
Dave Airlieff72145b2011-02-07 12:16:14 +1000238 args->size = args->pitch * args->height;
239 return i915_gem_create(file, dev,
240 args->size, &args->handle);
241}
242
243int i915_gem_dumb_destroy(struct drm_file *file,
244 struct drm_device *dev,
245 uint32_t handle)
246{
247 return drm_gem_handle_delete(file, handle);
248}
249
250/**
251 * Creates a new mm object and returns a handle to it.
252 */
253int
254i915_gem_create_ioctl(struct drm_device *dev, void *data,
255 struct drm_file *file)
256{
257 struct drm_i915_gem_create *args = data;
Daniel Vetter63ed2cb2012-04-23 16:50:50 +0200258
Dave Airlieff72145b2011-02-07 12:16:14 +1000259 return i915_gem_create(file, dev,
260 args->size, &args->handle);
261}
262
Chris Wilson05394f32010-11-08 19:18:58 +0000263static int i915_gem_object_needs_bit17_swizzle(struct drm_i915_gem_object *obj)
Eric Anholt280b7132009-03-12 16:56:27 -0700264{
Chris Wilson05394f32010-11-08 19:18:58 +0000265 drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
Eric Anholt280b7132009-03-12 16:56:27 -0700266
267 return dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_9_10_17 &&
Chris Wilson05394f32010-11-08 19:18:58 +0000268 obj->tiling_mode != I915_TILING_NONE;
Eric Anholt280b7132009-03-12 16:56:27 -0700269}
270
Daniel Vetter8c599672011-12-14 13:57:31 +0100271static inline int
Daniel Vetter8461d222011-12-14 13:57:32 +0100272__copy_to_user_swizzled(char __user *cpu_vaddr,
273 const char *gpu_vaddr, int gpu_offset,
274 int length)
275{
276 int ret, cpu_offset = 0;
277
278 while (length > 0) {
279 int cacheline_end = ALIGN(gpu_offset + 1, 64);
280 int this_length = min(cacheline_end - gpu_offset, length);
281 int swizzled_gpu_offset = gpu_offset ^ 64;
282
283 ret = __copy_to_user(cpu_vaddr + cpu_offset,
284 gpu_vaddr + swizzled_gpu_offset,
285 this_length);
286 if (ret)
287 return ret + length;
288
289 cpu_offset += this_length;
290 gpu_offset += this_length;
291 length -= this_length;
292 }
293
294 return 0;
295}
296
297static inline int
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -0700298__copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset,
299 const char __user *cpu_vaddr,
Daniel Vetter8c599672011-12-14 13:57:31 +0100300 int length)
301{
302 int ret, cpu_offset = 0;
303
304 while (length > 0) {
305 int cacheline_end = ALIGN(gpu_offset + 1, 64);
306 int this_length = min(cacheline_end - gpu_offset, length);
307 int swizzled_gpu_offset = gpu_offset ^ 64;
308
309 ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset,
310 cpu_vaddr + cpu_offset,
311 this_length);
312 if (ret)
313 return ret + length;
314
315 cpu_offset += this_length;
316 gpu_offset += this_length;
317 length -= this_length;
318 }
319
320 return 0;
321}
322
Daniel Vetterd174bd62012-03-25 19:47:40 +0200323/* Per-page copy function for the shmem pread fastpath.
324 * Flushes invalid cachelines before reading the target if
325 * needs_clflush is set. */
Eric Anholteb014592009-03-10 11:44:52 -0700326static int
Daniel Vetterd174bd62012-03-25 19:47:40 +0200327shmem_pread_fast(struct page *page, int shmem_page_offset, int page_length,
328 char __user *user_data,
329 bool page_do_bit17_swizzling, bool needs_clflush)
330{
331 char *vaddr;
332 int ret;
333
Daniel Vettere7e58eb2012-03-25 19:47:43 +0200334 if (unlikely(page_do_bit17_swizzling))
Daniel Vetterd174bd62012-03-25 19:47:40 +0200335 return -EINVAL;
336
337 vaddr = kmap_atomic(page);
338 if (needs_clflush)
339 drm_clflush_virt_range(vaddr + shmem_page_offset,
340 page_length);
341 ret = __copy_to_user_inatomic(user_data,
342 vaddr + shmem_page_offset,
343 page_length);
344 kunmap_atomic(vaddr);
345
Chris Wilsonf60d7f02012-09-04 21:02:56 +0100346 return ret ? -EFAULT : 0;
Daniel Vetterd174bd62012-03-25 19:47:40 +0200347}
348
Daniel Vetter23c18c72012-03-25 19:47:42 +0200349static void
350shmem_clflush_swizzled_range(char *addr, unsigned long length,
351 bool swizzled)
352{
Daniel Vettere7e58eb2012-03-25 19:47:43 +0200353 if (unlikely(swizzled)) {
Daniel Vetter23c18c72012-03-25 19:47:42 +0200354 unsigned long start = (unsigned long) addr;
355 unsigned long end = (unsigned long) addr + length;
356
357 /* For swizzling simply ensure that we always flush both
358 * channels. Lame, but simple and it works. Swizzled
359 * pwrite/pread is far from a hotpath - current userspace
360 * doesn't use it at all. */
361 start = round_down(start, 128);
362 end = round_up(end, 128);
363
364 drm_clflush_virt_range((void *)start, end - start);
365 } else {
366 drm_clflush_virt_range(addr, length);
367 }
368
369}
370
Daniel Vetterd174bd62012-03-25 19:47:40 +0200371/* Only difference to the fast-path function is that this can handle bit17
372 * and uses non-atomic copy and kmap functions. */
373static int
374shmem_pread_slow(struct page *page, int shmem_page_offset, int page_length,
375 char __user *user_data,
376 bool page_do_bit17_swizzling, bool needs_clflush)
377{
378 char *vaddr;
379 int ret;
380
381 vaddr = kmap(page);
382 if (needs_clflush)
Daniel Vetter23c18c72012-03-25 19:47:42 +0200383 shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
384 page_length,
385 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200386
387 if (page_do_bit17_swizzling)
388 ret = __copy_to_user_swizzled(user_data,
389 vaddr, shmem_page_offset,
390 page_length);
391 else
392 ret = __copy_to_user(user_data,
393 vaddr + shmem_page_offset,
394 page_length);
395 kunmap(page);
396
Chris Wilsonf60d7f02012-09-04 21:02:56 +0100397 return ret ? - EFAULT : 0;
Daniel Vetterd174bd62012-03-25 19:47:40 +0200398}
399
Eric Anholteb014592009-03-10 11:44:52 -0700400static int
Daniel Vetterdbf7bff2012-03-25 19:47:29 +0200401i915_gem_shmem_pread(struct drm_device *dev,
402 struct drm_i915_gem_object *obj,
403 struct drm_i915_gem_pread *args,
404 struct drm_file *file)
Eric Anholteb014592009-03-10 11:44:52 -0700405{
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;
Eric Anholteb014592009-03-10 11:44:52 -0700414
Daniel Vetter8461d222011-12-14 13:57:32 +0100415 user_data = (char __user *) (uintptr_t) args->data_ptr;
Eric Anholteb014592009-03-10 11:44:52 -0700416 remain = args->size;
417
Daniel Vetter8461d222011-12-14 13:57:32 +0100418 obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700419
Daniel Vetter84897312012-03-25 19:47:31 +0200420 if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU)) {
421 /* If we're not in the cpu read domain, set ourself into the gtt
422 * read domain and manually flush cachelines (if required). This
423 * optimizes for the case when the gpu will dirty the data
424 * anyway again before the next pread happens. */
425 if (obj->cache_level == I915_CACHE_NONE)
426 needs_clflush = 1;
Chris Wilson6c085a72012-08-20 11:40:46 +0200427 if (obj->gtt_space) {
428 ret = i915_gem_object_set_to_gtt_domain(obj, false);
429 if (ret)
430 return ret;
431 }
Daniel Vetter84897312012-03-25 19:47:31 +0200432 }
Eric Anholteb014592009-03-10 11:44:52 -0700433
Chris Wilsonf60d7f02012-09-04 21:02:56 +0100434 ret = i915_gem_object_get_pages(obj);
435 if (ret)
436 return ret;
437
438 i915_gem_object_pin_pages(obj);
439
Eric Anholteb014592009-03-10 11:44:52 -0700440 offset = args->offset;
Daniel Vetter8461d222011-12-14 13:57:32 +0100441
Eric Anholteb014592009-03-10 11:44:52 -0700442 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100443 struct page *page;
444
Eric Anholteb014592009-03-10 11:44:52 -0700445 /* Operation in this page
446 *
Eric Anholteb014592009-03-10 11:44:52 -0700447 * shmem_page_offset = offset within page in shmem file
Eric Anholteb014592009-03-10 11:44:52 -0700448 * page_length = bytes to copy for this page
449 */
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100450 shmem_page_offset = offset_in_page(offset);
Eric Anholteb014592009-03-10 11:44:52 -0700451 page_length = remain;
452 if ((shmem_page_offset + page_length) > PAGE_SIZE)
453 page_length = PAGE_SIZE - shmem_page_offset;
Eric Anholteb014592009-03-10 11:44:52 -0700454
Chris Wilsonf60d7f02012-09-04 21:02:56 +0100455 page = obj->pages[offset >> PAGE_SHIFT];
Daniel Vetter8461d222011-12-14 13:57:32 +0100456 page_do_bit17_swizzling = obj_do_bit17_swizzling &&
457 (page_to_phys(page) & (1 << 17)) != 0;
458
Daniel Vetterd174bd62012-03-25 19:47:40 +0200459 ret = shmem_pread_fast(page, shmem_page_offset, page_length,
460 user_data, page_do_bit17_swizzling,
461 needs_clflush);
462 if (ret == 0)
463 goto next_page;
Eric Anholteb014592009-03-10 11:44:52 -0700464
Daniel Vetterdbf7bff2012-03-25 19:47:29 +0200465 hit_slowpath = 1;
Daniel Vetterdbf7bff2012-03-25 19:47:29 +0200466 mutex_unlock(&dev->struct_mutex);
467
Daniel Vetter96d79b52012-03-25 19:47:36 +0200468 if (!prefaulted) {
Daniel Vetterf56f8212012-03-25 19:47:41 +0200469 ret = fault_in_multipages_writeable(user_data, remain);
Daniel Vetter96d79b52012-03-25 19:47:36 +0200470 /* Userspace is tricking us, but we've already clobbered
471 * its pages with the prefault and promised to write the
472 * data up to the first fault. Hence ignore any errors
473 * and just continue. */
474 (void)ret;
475 prefaulted = 1;
476 }
477
Daniel Vetterd174bd62012-03-25 19:47:40 +0200478 ret = shmem_pread_slow(page, shmem_page_offset, page_length,
479 user_data, page_do_bit17_swizzling,
480 needs_clflush);
Eric Anholteb014592009-03-10 11:44:52 -0700481
Daniel Vetterdbf7bff2012-03-25 19:47:29 +0200482 mutex_lock(&dev->struct_mutex);
Chris Wilsonf60d7f02012-09-04 21:02:56 +0100483
Daniel Vetterdbf7bff2012-03-25 19:47:29 +0200484next_page:
Chris Wilsone5281cc2010-10-28 13:45:36 +0100485 mark_page_accessed(page);
Chris Wilsone5281cc2010-10-28 13:45:36 +0100486
Chris Wilsonf60d7f02012-09-04 21:02:56 +0100487 if (ret)
Daniel Vetter8461d222011-12-14 13:57:32 +0100488 goto out;
Daniel Vetter8461d222011-12-14 13:57:32 +0100489
Eric Anholteb014592009-03-10 11:44:52 -0700490 remain -= page_length;
Daniel Vetter8461d222011-12-14 13:57:32 +0100491 user_data += page_length;
Eric Anholteb014592009-03-10 11:44:52 -0700492 offset += page_length;
493 }
494
Chris Wilson4f27b752010-10-14 15:26:45 +0100495out:
Chris Wilsonf60d7f02012-09-04 21:02:56 +0100496 i915_gem_object_unpin_pages(obj);
497
Daniel Vetterdbf7bff2012-03-25 19:47:29 +0200498 if (hit_slowpath) {
499 /* Fixup: Kill any reinstated backing storage pages */
500 if (obj->madv == __I915_MADV_PURGED)
501 i915_gem_object_truncate(obj);
502 }
Eric Anholteb014592009-03-10 11:44:52 -0700503
504 return ret;
505}
506
Eric Anholt673a3942008-07-30 12:06:12 -0700507/**
508 * Reads data from the object referenced by handle.
509 *
510 * On error, the contents of *data are undefined.
511 */
512int
513i915_gem_pread_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000514 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700515{
516 struct drm_i915_gem_pread *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +0000517 struct drm_i915_gem_object *obj;
Chris Wilson35b62a82010-09-26 20:23:38 +0100518 int ret = 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700519
Chris Wilson51311d02010-11-17 09:10:42 +0000520 if (args->size == 0)
521 return 0;
522
523 if (!access_ok(VERIFY_WRITE,
524 (char __user *)(uintptr_t)args->data_ptr,
525 args->size))
526 return -EFAULT;
527
Chris Wilson4f27b752010-10-14 15:26:45 +0100528 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100529 if (ret)
Chris Wilson4f27b752010-10-14 15:26:45 +0100530 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700531
Chris Wilson05394f32010-11-08 19:18:58 +0000532 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +0000533 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100534 ret = -ENOENT;
535 goto unlock;
Chris Wilson4f27b752010-10-14 15:26:45 +0100536 }
Eric Anholt673a3942008-07-30 12:06:12 -0700537
Chris Wilson7dcd2492010-09-26 20:21:44 +0100538 /* Bounds check source. */
Chris Wilson05394f32010-11-08 19:18:58 +0000539 if (args->offset > obj->base.size ||
540 args->size > obj->base.size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +0100541 ret = -EINVAL;
Chris Wilson35b62a82010-09-26 20:23:38 +0100542 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +0100543 }
544
Daniel Vetter1286ff72012-05-10 15:25:09 +0200545 /* prime objects have no backing filp to GEM pread/pwrite
546 * pages from.
547 */
548 if (!obj->base.filp) {
549 ret = -EINVAL;
550 goto out;
551 }
552
Chris Wilsondb53a302011-02-03 11:57:46 +0000553 trace_i915_gem_object_pread(obj, args->offset, args->size);
554
Daniel Vetterdbf7bff2012-03-25 19:47:29 +0200555 ret = i915_gem_shmem_pread(dev, obj, args, file);
Eric Anholt673a3942008-07-30 12:06:12 -0700556
Chris Wilson35b62a82010-09-26 20:23:38 +0100557out:
Chris Wilson05394f32010-11-08 19:18:58 +0000558 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100559unlock:
Chris Wilson4f27b752010-10-14 15:26:45 +0100560 mutex_unlock(&dev->struct_mutex);
Eric Anholteb014592009-03-10 11:44:52 -0700561 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700562}
563
Keith Packard0839ccb2008-10-30 19:38:48 -0700564/* This is the fast write path which cannot handle
565 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700566 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700567
Keith Packard0839ccb2008-10-30 19:38:48 -0700568static inline int
569fast_user_write(struct io_mapping *mapping,
570 loff_t page_base, int page_offset,
571 char __user *user_data,
572 int length)
573{
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -0700574 void __iomem *vaddr_atomic;
575 void *vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -0700576 unsigned long unwritten;
577
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700578 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -0700579 /* We can use the cpu mem copy function because this is X86. */
580 vaddr = (void __force*)vaddr_atomic + page_offset;
581 unwritten = __copy_from_user_inatomic_nocache(vaddr,
Keith Packard0839ccb2008-10-30 19:38:48 -0700582 user_data, length);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700583 io_mapping_unmap_atomic(vaddr_atomic);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100584 return unwritten;
Keith Packard0839ccb2008-10-30 19:38:48 -0700585}
586
Eric Anholt3de09aa2009-03-09 09:42:23 -0700587/**
588 * This is the fast pwrite path, where we copy the data directly from the
589 * user into the GTT, uncached.
590 */
Eric Anholt673a3942008-07-30 12:06:12 -0700591static int
Chris Wilson05394f32010-11-08 19:18:58 +0000592i915_gem_gtt_pwrite_fast(struct drm_device *dev,
593 struct drm_i915_gem_object *obj,
Eric Anholt3de09aa2009-03-09 09:42:23 -0700594 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000595 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700596{
Keith Packard0839ccb2008-10-30 19:38:48 -0700597 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700598 ssize_t remain;
Keith Packard0839ccb2008-10-30 19:38:48 -0700599 loff_t offset, page_base;
Eric Anholt673a3942008-07-30 12:06:12 -0700600 char __user *user_data;
Daniel Vetter935aaa62012-03-25 19:47:35 +0200601 int page_offset, page_length, ret;
602
Chris Wilson86a1ee22012-08-11 15:41:04 +0100603 ret = i915_gem_object_pin(obj, 0, true, true);
Daniel Vetter935aaa62012-03-25 19:47:35 +0200604 if (ret)
605 goto out;
606
607 ret = i915_gem_object_set_to_gtt_domain(obj, true);
608 if (ret)
609 goto out_unpin;
610
611 ret = i915_gem_object_put_fence(obj);
612 if (ret)
613 goto out_unpin;
Eric Anholt673a3942008-07-30 12:06:12 -0700614
615 user_data = (char __user *) (uintptr_t) args->data_ptr;
616 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700617
Chris Wilson05394f32010-11-08 19:18:58 +0000618 offset = obj->gtt_offset + args->offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700619
620 while (remain > 0) {
621 /* Operation in this page
622 *
Keith Packard0839ccb2008-10-30 19:38:48 -0700623 * page_base = page offset within aperture
624 * page_offset = offset within page
625 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -0700626 */
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100627 page_base = offset & PAGE_MASK;
628 page_offset = offset_in_page(offset);
Keith Packard0839ccb2008-10-30 19:38:48 -0700629 page_length = remain;
630 if ((page_offset + remain) > PAGE_SIZE)
631 page_length = PAGE_SIZE - page_offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700632
Keith Packard0839ccb2008-10-30 19:38:48 -0700633 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -0700634 * source page isn't available. Return the error and we'll
635 * retry in the slow path.
Keith Packard0839ccb2008-10-30 19:38:48 -0700636 */
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100637 if (fast_user_write(dev_priv->mm.gtt_mapping, page_base,
Daniel Vetter935aaa62012-03-25 19:47:35 +0200638 page_offset, user_data, page_length)) {
639 ret = -EFAULT;
640 goto out_unpin;
641 }
Eric Anholt673a3942008-07-30 12:06:12 -0700642
Keith Packard0839ccb2008-10-30 19:38:48 -0700643 remain -= page_length;
644 user_data += page_length;
645 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700646 }
Eric Anholt673a3942008-07-30 12:06:12 -0700647
Daniel Vetter935aaa62012-03-25 19:47:35 +0200648out_unpin:
649 i915_gem_object_unpin(obj);
650out:
Eric Anholt3de09aa2009-03-09 09:42:23 -0700651 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700652}
653
Daniel Vetterd174bd62012-03-25 19:47:40 +0200654/* Per-page copy function for the shmem pwrite fastpath.
655 * Flushes invalid cachelines before writing to the target if
656 * needs_clflush_before is set and flushes out any written cachelines after
657 * writing if needs_clflush is set. */
Eric Anholt673a3942008-07-30 12:06:12 -0700658static int
Daniel Vetterd174bd62012-03-25 19:47:40 +0200659shmem_pwrite_fast(struct page *page, int shmem_page_offset, int page_length,
660 char __user *user_data,
661 bool page_do_bit17_swizzling,
662 bool needs_clflush_before,
663 bool needs_clflush_after)
Eric Anholt673a3942008-07-30 12:06:12 -0700664{
Daniel Vetterd174bd62012-03-25 19:47:40 +0200665 char *vaddr;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700666 int ret;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700667
Daniel Vettere7e58eb2012-03-25 19:47:43 +0200668 if (unlikely(page_do_bit17_swizzling))
Daniel Vetterd174bd62012-03-25 19:47:40 +0200669 return -EINVAL;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700670
Daniel Vetterd174bd62012-03-25 19:47:40 +0200671 vaddr = kmap_atomic(page);
672 if (needs_clflush_before)
673 drm_clflush_virt_range(vaddr + shmem_page_offset,
674 page_length);
675 ret = __copy_from_user_inatomic_nocache(vaddr + shmem_page_offset,
676 user_data,
677 page_length);
678 if (needs_clflush_after)
679 drm_clflush_virt_range(vaddr + shmem_page_offset,
680 page_length);
681 kunmap_atomic(vaddr);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700682
Chris Wilson755d2212012-09-04 21:02:55 +0100683 return ret ? -EFAULT : 0;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700684}
685
Daniel Vetterd174bd62012-03-25 19:47:40 +0200686/* Only difference to the fast-path function is that this can handle bit17
687 * and uses non-atomic copy and kmap functions. */
Eric Anholt3043c602008-10-02 12:24:47 -0700688static int
Daniel Vetterd174bd62012-03-25 19:47:40 +0200689shmem_pwrite_slow(struct page *page, int shmem_page_offset, int page_length,
690 char __user *user_data,
691 bool page_do_bit17_swizzling,
692 bool needs_clflush_before,
693 bool needs_clflush_after)
Eric Anholt673a3942008-07-30 12:06:12 -0700694{
Daniel Vetterd174bd62012-03-25 19:47:40 +0200695 char *vaddr;
696 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -0700697
Daniel Vetterd174bd62012-03-25 19:47:40 +0200698 vaddr = kmap(page);
Daniel Vettere7e58eb2012-03-25 19:47:43 +0200699 if (unlikely(needs_clflush_before || page_do_bit17_swizzling))
Daniel Vetter23c18c72012-03-25 19:47:42 +0200700 shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
701 page_length,
702 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200703 if (page_do_bit17_swizzling)
704 ret = __copy_from_user_swizzled(vaddr, shmem_page_offset,
Chris Wilsone5281cc2010-10-28 13:45:36 +0100705 user_data,
706 page_length);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200707 else
708 ret = __copy_from_user(vaddr + shmem_page_offset,
709 user_data,
710 page_length);
711 if (needs_clflush_after)
Daniel Vetter23c18c72012-03-25 19:47:42 +0200712 shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
713 page_length,
714 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200715 kunmap(page);
Chris Wilsone5281cc2010-10-28 13:45:36 +0100716
Chris Wilson755d2212012-09-04 21:02:55 +0100717 return ret ? -EFAULT : 0;
Eric Anholt40123c12009-03-09 13:42:30 -0700718}
719
Eric Anholt40123c12009-03-09 13:42:30 -0700720static int
Daniel Vettere244a442012-03-25 19:47:28 +0200721i915_gem_shmem_pwrite(struct drm_device *dev,
722 struct drm_i915_gem_object *obj,
723 struct drm_i915_gem_pwrite *args,
724 struct drm_file *file)
Eric Anholt40123c12009-03-09 13:42:30 -0700725{
Eric Anholt40123c12009-03-09 13:42:30 -0700726 ssize_t remain;
Daniel Vetter8c599672011-12-14 13:57:31 +0100727 loff_t offset;
728 char __user *user_data;
Ben Widawskyeb2c0c82012-02-15 14:42:43 +0100729 int shmem_page_offset, page_length, ret = 0;
Daniel Vetter8c599672011-12-14 13:57:31 +0100730 int obj_do_bit17_swizzling, page_do_bit17_swizzling;
Daniel Vettere244a442012-03-25 19:47:28 +0200731 int hit_slowpath = 0;
Daniel Vetter58642882012-03-25 19:47:37 +0200732 int needs_clflush_after = 0;
733 int needs_clflush_before = 0;
Eric Anholt40123c12009-03-09 13:42:30 -0700734
Daniel Vetter8c599672011-12-14 13:57:31 +0100735 user_data = (char __user *) (uintptr_t) args->data_ptr;
Eric Anholt40123c12009-03-09 13:42:30 -0700736 remain = args->size;
737
Daniel Vetter8c599672011-12-14 13:57:31 +0100738 obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700739
Daniel Vetter58642882012-03-25 19:47:37 +0200740 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
741 /* If we're not in the cpu write domain, set ourself into the gtt
742 * write domain and manually flush cachelines (if required). This
743 * optimizes for the case when the gpu will use the data
744 * right away and we therefore have to clflush anyway. */
745 if (obj->cache_level == I915_CACHE_NONE)
746 needs_clflush_after = 1;
Chris Wilson6c085a72012-08-20 11:40:46 +0200747 if (obj->gtt_space) {
748 ret = i915_gem_object_set_to_gtt_domain(obj, true);
749 if (ret)
750 return ret;
751 }
Daniel Vetter58642882012-03-25 19:47:37 +0200752 }
753 /* Same trick applies for invalidate partially written cachelines before
754 * writing. */
755 if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU)
756 && obj->cache_level == I915_CACHE_NONE)
757 needs_clflush_before = 1;
758
Chris Wilson755d2212012-09-04 21:02:55 +0100759 ret = i915_gem_object_get_pages(obj);
760 if (ret)
761 return ret;
762
763 i915_gem_object_pin_pages(obj);
764
Eric Anholt40123c12009-03-09 13:42:30 -0700765 offset = args->offset;
Chris Wilson05394f32010-11-08 19:18:58 +0000766 obj->dirty = 1;
Eric Anholt40123c12009-03-09 13:42:30 -0700767
768 while (remain > 0) {
Chris Wilsone5281cc2010-10-28 13:45:36 +0100769 struct page *page;
Daniel Vetter58642882012-03-25 19:47:37 +0200770 int partial_cacheline_write;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100771
Eric Anholt40123c12009-03-09 13:42:30 -0700772 /* Operation in this page
773 *
Eric Anholt40123c12009-03-09 13:42:30 -0700774 * shmem_page_offset = offset within page in shmem file
Eric Anholt40123c12009-03-09 13:42:30 -0700775 * page_length = bytes to copy for this page
776 */
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100777 shmem_page_offset = offset_in_page(offset);
Eric Anholt40123c12009-03-09 13:42:30 -0700778
779 page_length = remain;
780 if ((shmem_page_offset + page_length) > PAGE_SIZE)
781 page_length = PAGE_SIZE - shmem_page_offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700782
Daniel Vetter58642882012-03-25 19:47:37 +0200783 /* If we don't overwrite a cacheline completely we need to be
784 * careful to have up-to-date data by first clflushing. Don't
785 * overcomplicate things and flush the entire patch. */
786 partial_cacheline_write = needs_clflush_before &&
787 ((shmem_page_offset | page_length)
788 & (boot_cpu_data.x86_clflush_size - 1));
789
Chris Wilson755d2212012-09-04 21:02:55 +0100790 page = obj->pages[offset >> PAGE_SHIFT];
Daniel Vetter8c599672011-12-14 13:57:31 +0100791 page_do_bit17_swizzling = obj_do_bit17_swizzling &&
792 (page_to_phys(page) & (1 << 17)) != 0;
793
Daniel Vetterd174bd62012-03-25 19:47:40 +0200794 ret = shmem_pwrite_fast(page, shmem_page_offset, page_length,
795 user_data, page_do_bit17_swizzling,
796 partial_cacheline_write,
797 needs_clflush_after);
798 if (ret == 0)
799 goto next_page;
Eric Anholt40123c12009-03-09 13:42:30 -0700800
Daniel Vettere244a442012-03-25 19:47:28 +0200801 hit_slowpath = 1;
Daniel Vettere244a442012-03-25 19:47:28 +0200802 mutex_unlock(&dev->struct_mutex);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200803 ret = shmem_pwrite_slow(page, shmem_page_offset, page_length,
804 user_data, page_do_bit17_swizzling,
805 partial_cacheline_write,
806 needs_clflush_after);
Eric Anholt40123c12009-03-09 13:42:30 -0700807
Daniel Vettere244a442012-03-25 19:47:28 +0200808 mutex_lock(&dev->struct_mutex);
Chris Wilson755d2212012-09-04 21:02:55 +0100809
Daniel Vettere244a442012-03-25 19:47:28 +0200810next_page:
Chris Wilsone5281cc2010-10-28 13:45:36 +0100811 set_page_dirty(page);
812 mark_page_accessed(page);
Chris Wilsone5281cc2010-10-28 13:45:36 +0100813
Chris Wilson755d2212012-09-04 21:02:55 +0100814 if (ret)
Daniel Vetter8c599672011-12-14 13:57:31 +0100815 goto out;
Daniel Vetter8c599672011-12-14 13:57:31 +0100816
Eric Anholt40123c12009-03-09 13:42:30 -0700817 remain -= page_length;
Daniel Vetter8c599672011-12-14 13:57:31 +0100818 user_data += page_length;
Eric Anholt40123c12009-03-09 13:42:30 -0700819 offset += page_length;
820 }
821
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100822out:
Chris Wilson755d2212012-09-04 21:02:55 +0100823 i915_gem_object_unpin_pages(obj);
824
Daniel Vettere244a442012-03-25 19:47:28 +0200825 if (hit_slowpath) {
826 /* Fixup: Kill any reinstated backing storage pages */
827 if (obj->madv == __I915_MADV_PURGED)
828 i915_gem_object_truncate(obj);
829 /* and flush dirty cachelines in case the object isn't in the cpu write
830 * domain anymore. */
831 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
832 i915_gem_clflush_object(obj);
833 intel_gtt_chipset_flush();
834 }
Daniel Vetter8c599672011-12-14 13:57:31 +0100835 }
Eric Anholt40123c12009-03-09 13:42:30 -0700836
Daniel Vetter58642882012-03-25 19:47:37 +0200837 if (needs_clflush_after)
838 intel_gtt_chipset_flush();
839
Eric Anholt40123c12009-03-09 13:42:30 -0700840 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700841}
842
843/**
844 * Writes data to the object referenced by handle.
845 *
846 * On error, the contents of the buffer that were to be modified are undefined.
847 */
848int
849i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100850 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700851{
852 struct drm_i915_gem_pwrite *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +0000853 struct drm_i915_gem_object *obj;
Chris Wilson51311d02010-11-17 09:10:42 +0000854 int ret;
855
856 if (args->size == 0)
857 return 0;
858
859 if (!access_ok(VERIFY_READ,
860 (char __user *)(uintptr_t)args->data_ptr,
861 args->size))
862 return -EFAULT;
863
Daniel Vetterf56f8212012-03-25 19:47:41 +0200864 ret = fault_in_multipages_readable((char __user *)(uintptr_t)args->data_ptr,
865 args->size);
Chris Wilson51311d02010-11-17 09:10:42 +0000866 if (ret)
867 return -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -0700868
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100869 ret = i915_mutex_lock_interruptible(dev);
870 if (ret)
871 return ret;
872
Chris Wilson05394f32010-11-08 19:18:58 +0000873 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +0000874 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100875 ret = -ENOENT;
876 goto unlock;
877 }
Eric Anholt673a3942008-07-30 12:06:12 -0700878
Chris Wilson7dcd2492010-09-26 20:21:44 +0100879 /* Bounds check destination. */
Chris Wilson05394f32010-11-08 19:18:58 +0000880 if (args->offset > obj->base.size ||
881 args->size > obj->base.size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +0100882 ret = -EINVAL;
Chris Wilson35b62a82010-09-26 20:23:38 +0100883 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +0100884 }
885
Daniel Vetter1286ff72012-05-10 15:25:09 +0200886 /* prime objects have no backing filp to GEM pread/pwrite
887 * pages from.
888 */
889 if (!obj->base.filp) {
890 ret = -EINVAL;
891 goto out;
892 }
893
Chris Wilsondb53a302011-02-03 11:57:46 +0000894 trace_i915_gem_object_pwrite(obj, args->offset, args->size);
895
Daniel Vetter935aaa62012-03-25 19:47:35 +0200896 ret = -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -0700897 /* We can only do the GTT pwrite on untiled buffers, as otherwise
898 * it would end up going through the fenced access, and we'll get
899 * different detiling behavior between reading and writing.
900 * pread/pwrite currently are reading and writing from the CPU
901 * perspective, requiring manual detiling by the client.
902 */
Daniel Vetter5c0480f2011-12-14 13:57:30 +0100903 if (obj->phys_obj) {
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100904 ret = i915_gem_phys_pwrite(dev, obj, args, file);
Daniel Vetter5c0480f2011-12-14 13:57:30 +0100905 goto out;
906 }
907
Chris Wilson86a1ee22012-08-11 15:41:04 +0100908 if (obj->cache_level == I915_CACHE_NONE &&
Daniel Vetterc07496f2012-04-13 15:51:51 +0200909 obj->tiling_mode == I915_TILING_NONE &&
Daniel Vetter5c0480f2011-12-14 13:57:30 +0100910 obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100911 ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file);
Daniel Vetter935aaa62012-03-25 19:47:35 +0200912 /* Note that the gtt paths might fail with non-page-backed user
913 * pointers (e.g. gtt mappings when moving data between
914 * textures). Fallback to the shmem path in that case. */
Eric Anholt40123c12009-03-09 13:42:30 -0700915 }
Eric Anholt673a3942008-07-30 12:06:12 -0700916
Chris Wilson86a1ee22012-08-11 15:41:04 +0100917 if (ret == -EFAULT || ret == -ENOSPC)
Daniel Vetter935aaa62012-03-25 19:47:35 +0200918 ret = i915_gem_shmem_pwrite(dev, obj, args, file);
Daniel Vetter5c0480f2011-12-14 13:57:30 +0100919
Chris Wilson35b62a82010-09-26 20:23:38 +0100920out:
Chris Wilson05394f32010-11-08 19:18:58 +0000921 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100922unlock:
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100923 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -0700924 return ret;
925}
926
Chris Wilsonb3612372012-08-24 09:35:08 +0100927int
928i915_gem_check_wedge(struct drm_i915_private *dev_priv,
929 bool interruptible)
930{
931 if (atomic_read(&dev_priv->mm.wedged)) {
932 struct completion *x = &dev_priv->error_completion;
933 bool recovery_complete;
934 unsigned long flags;
935
936 /* Give the error handler a chance to run. */
937 spin_lock_irqsave(&x->wait.lock, flags);
938 recovery_complete = x->done > 0;
939 spin_unlock_irqrestore(&x->wait.lock, flags);
940
941 /* Non-interruptible callers can't handle -EAGAIN, hence return
942 * -EIO unconditionally for these. */
943 if (!interruptible)
944 return -EIO;
945
946 /* Recovery complete, but still wedged means reset failure. */
947 if (recovery_complete)
948 return -EIO;
949
950 return -EAGAIN;
951 }
952
953 return 0;
954}
955
956/*
957 * Compare seqno against outstanding lazy request. Emit a request if they are
958 * equal.
959 */
960static int
961i915_gem_check_olr(struct intel_ring_buffer *ring, u32 seqno)
962{
963 int ret;
964
965 BUG_ON(!mutex_is_locked(&ring->dev->struct_mutex));
966
967 ret = 0;
968 if (seqno == ring->outstanding_lazy_request)
969 ret = i915_add_request(ring, NULL, NULL);
970
971 return ret;
972}
973
974/**
975 * __wait_seqno - wait until execution of seqno has finished
976 * @ring: the ring expected to report seqno
977 * @seqno: duh!
978 * @interruptible: do an interruptible wait (normally yes)
979 * @timeout: in - how long to wait (NULL forever); out - how much time remaining
980 *
981 * Returns 0 if the seqno was found within the alloted time. Else returns the
982 * errno with remaining time filled in timeout argument.
983 */
984static int __wait_seqno(struct intel_ring_buffer *ring, u32 seqno,
985 bool interruptible, struct timespec *timeout)
986{
987 drm_i915_private_t *dev_priv = ring->dev->dev_private;
988 struct timespec before, now, wait_time={1,0};
989 unsigned long timeout_jiffies;
990 long end;
991 bool wait_forever = true;
992 int ret;
993
994 if (i915_seqno_passed(ring->get_seqno(ring, true), seqno))
995 return 0;
996
997 trace_i915_gem_request_wait_begin(ring, seqno);
998
999 if (timeout != NULL) {
1000 wait_time = *timeout;
1001 wait_forever = false;
1002 }
1003
1004 timeout_jiffies = timespec_to_jiffies(&wait_time);
1005
1006 if (WARN_ON(!ring->irq_get(ring)))
1007 return -ENODEV;
1008
1009 /* Record current time in case interrupted by signal, or wedged * */
1010 getrawmonotonic(&before);
1011
1012#define EXIT_COND \
1013 (i915_seqno_passed(ring->get_seqno(ring, false), seqno) || \
1014 atomic_read(&dev_priv->mm.wedged))
1015 do {
1016 if (interruptible)
1017 end = wait_event_interruptible_timeout(ring->irq_queue,
1018 EXIT_COND,
1019 timeout_jiffies);
1020 else
1021 end = wait_event_timeout(ring->irq_queue, EXIT_COND,
1022 timeout_jiffies);
1023
1024 ret = i915_gem_check_wedge(dev_priv, interruptible);
1025 if (ret)
1026 end = ret;
1027 } while (end == 0 && wait_forever);
1028
1029 getrawmonotonic(&now);
1030
1031 ring->irq_put(ring);
1032 trace_i915_gem_request_wait_end(ring, seqno);
1033#undef EXIT_COND
1034
1035 if (timeout) {
1036 struct timespec sleep_time = timespec_sub(now, before);
1037 *timeout = timespec_sub(*timeout, sleep_time);
1038 }
1039
1040 switch (end) {
1041 case -EIO:
1042 case -EAGAIN: /* Wedged */
1043 case -ERESTARTSYS: /* Signal */
1044 return (int)end;
1045 case 0: /* Timeout */
1046 if (timeout)
1047 set_normalized_timespec(timeout, 0, 0);
1048 return -ETIME;
1049 default: /* Completed */
1050 WARN_ON(end < 0); /* We're not aware of other errors */
1051 return 0;
1052 }
1053}
1054
1055/**
1056 * Waits for a sequence number to be signaled, and cleans up the
1057 * request and object lists appropriately for that event.
1058 */
1059int
1060i915_wait_seqno(struct intel_ring_buffer *ring, uint32_t seqno)
1061{
1062 struct drm_device *dev = ring->dev;
1063 struct drm_i915_private *dev_priv = dev->dev_private;
1064 bool interruptible = dev_priv->mm.interruptible;
1065 int ret;
1066
1067 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
1068 BUG_ON(seqno == 0);
1069
1070 ret = i915_gem_check_wedge(dev_priv, interruptible);
1071 if (ret)
1072 return ret;
1073
1074 ret = i915_gem_check_olr(ring, seqno);
1075 if (ret)
1076 return ret;
1077
1078 return __wait_seqno(ring, seqno, interruptible, NULL);
1079}
1080
1081/**
1082 * Ensures that all rendering to the object has completed and the object is
1083 * safe to unbind from the GTT or access from the CPU.
1084 */
1085static __must_check int
1086i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj,
1087 bool readonly)
1088{
1089 struct intel_ring_buffer *ring = obj->ring;
1090 u32 seqno;
1091 int ret;
1092
1093 seqno = readonly ? obj->last_write_seqno : obj->last_read_seqno;
1094 if (seqno == 0)
1095 return 0;
1096
1097 ret = i915_wait_seqno(ring, seqno);
1098 if (ret)
1099 return ret;
1100
1101 i915_gem_retire_requests_ring(ring);
1102
1103 /* Manually manage the write flush as we may have not yet
1104 * retired the buffer.
1105 */
1106 if (obj->last_write_seqno &&
1107 i915_seqno_passed(seqno, obj->last_write_seqno)) {
1108 obj->last_write_seqno = 0;
1109 obj->base.write_domain &= ~I915_GEM_GPU_DOMAINS;
1110 }
1111
1112 return 0;
1113}
1114
Chris Wilson3236f572012-08-24 09:35:09 +01001115/* A nonblocking variant of the above wait. This is a highly dangerous routine
1116 * as the object state may change during this call.
1117 */
1118static __must_check int
1119i915_gem_object_wait_rendering__nonblocking(struct drm_i915_gem_object *obj,
1120 bool readonly)
1121{
1122 struct drm_device *dev = obj->base.dev;
1123 struct drm_i915_private *dev_priv = dev->dev_private;
1124 struct intel_ring_buffer *ring = obj->ring;
1125 u32 seqno;
1126 int ret;
1127
1128 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
1129 BUG_ON(!dev_priv->mm.interruptible);
1130
1131 seqno = readonly ? obj->last_write_seqno : obj->last_read_seqno;
1132 if (seqno == 0)
1133 return 0;
1134
1135 ret = i915_gem_check_wedge(dev_priv, true);
1136 if (ret)
1137 return ret;
1138
1139 ret = i915_gem_check_olr(ring, seqno);
1140 if (ret)
1141 return ret;
1142
1143 mutex_unlock(&dev->struct_mutex);
1144 ret = __wait_seqno(ring, seqno, true, NULL);
1145 mutex_lock(&dev->struct_mutex);
1146
1147 i915_gem_retire_requests_ring(ring);
1148
1149 /* Manually manage the write flush as we may have not yet
1150 * retired the buffer.
1151 */
1152 if (obj->last_write_seqno &&
1153 i915_seqno_passed(seqno, obj->last_write_seqno)) {
1154 obj->last_write_seqno = 0;
1155 obj->base.write_domain &= ~I915_GEM_GPU_DOMAINS;
1156 }
1157
1158 return ret;
1159}
1160
Eric Anholt673a3942008-07-30 12:06:12 -07001161/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001162 * Called when user space prepares to use an object with the CPU, either
1163 * through the mmap ioctl's mapping or a GTT mapping.
Eric Anholt673a3942008-07-30 12:06:12 -07001164 */
1165int
1166i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001167 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001168{
1169 struct drm_i915_gem_set_domain *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001170 struct drm_i915_gem_object *obj;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001171 uint32_t read_domains = args->read_domains;
1172 uint32_t write_domain = args->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07001173 int ret;
1174
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001175 /* Only handle setting domains to types used by the CPU. */
Chris Wilson21d509e2009-06-06 09:46:02 +01001176 if (write_domain & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001177 return -EINVAL;
1178
Chris Wilson21d509e2009-06-06 09:46:02 +01001179 if (read_domains & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001180 return -EINVAL;
1181
1182 /* Having something in the write domain implies it's in the read
1183 * domain, and only that read domain. Enforce that in the request.
1184 */
1185 if (write_domain != 0 && read_domains != write_domain)
1186 return -EINVAL;
1187
Chris Wilson76c1dec2010-09-25 11:22:51 +01001188 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001189 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001190 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001191
Chris Wilson05394f32010-11-08 19:18:58 +00001192 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00001193 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001194 ret = -ENOENT;
1195 goto unlock;
Chris Wilson76c1dec2010-09-25 11:22:51 +01001196 }
Jesse Barnes652c3932009-08-17 13:31:43 -07001197
Chris Wilson3236f572012-08-24 09:35:09 +01001198 /* Try to flush the object off the GPU without holding the lock.
1199 * We will repeat the flush holding the lock in the normal manner
1200 * to catch cases where we are gazumped.
1201 */
1202 ret = i915_gem_object_wait_rendering__nonblocking(obj, !write_domain);
1203 if (ret)
1204 goto unref;
1205
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001206 if (read_domains & I915_GEM_DOMAIN_GTT) {
1207 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
Eric Anholt02354392008-11-26 13:58:13 -08001208
1209 /* Silently promote "you're not bound, there was nothing to do"
1210 * to success, since the client was just asking us to
1211 * make sure everything was done.
1212 */
1213 if (ret == -EINVAL)
1214 ret = 0;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001215 } else {
Eric Anholte47c68e2008-11-14 13:35:19 -08001216 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001217 }
1218
Chris Wilson3236f572012-08-24 09:35:09 +01001219unref:
Chris Wilson05394f32010-11-08 19:18:58 +00001220 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001221unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07001222 mutex_unlock(&dev->struct_mutex);
1223 return ret;
1224}
1225
1226/**
1227 * Called when user space has done writes to this buffer
1228 */
1229int
1230i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001231 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001232{
1233 struct drm_i915_gem_sw_finish *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001234 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001235 int ret = 0;
1236
Chris Wilson76c1dec2010-09-25 11:22:51 +01001237 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001238 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001239 return ret;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001240
Chris Wilson05394f32010-11-08 19:18:58 +00001241 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00001242 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001243 ret = -ENOENT;
1244 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07001245 }
1246
Eric Anholt673a3942008-07-30 12:06:12 -07001247 /* Pinned buffers may be scanout, so flush the cache */
Chris Wilson05394f32010-11-08 19:18:58 +00001248 if (obj->pin_count)
Eric Anholte47c68e2008-11-14 13:35:19 -08001249 i915_gem_object_flush_cpu_write_domain(obj);
1250
Chris Wilson05394f32010-11-08 19:18:58 +00001251 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001252unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07001253 mutex_unlock(&dev->struct_mutex);
1254 return ret;
1255}
1256
1257/**
1258 * Maps the contents of an object, returning the address it is mapped
1259 * into.
1260 *
1261 * While the mapping holds a reference on the contents of the object, it doesn't
1262 * imply a ref on the object itself.
1263 */
1264int
1265i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001266 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001267{
1268 struct drm_i915_gem_mmap *args = data;
1269 struct drm_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001270 unsigned long addr;
1271
Chris Wilson05394f32010-11-08 19:18:58 +00001272 obj = drm_gem_object_lookup(dev, file, args->handle);
Eric Anholt673a3942008-07-30 12:06:12 -07001273 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001274 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001275
Daniel Vetter1286ff72012-05-10 15:25:09 +02001276 /* prime objects have no backing filp to GEM mmap
1277 * pages from.
1278 */
1279 if (!obj->filp) {
1280 drm_gem_object_unreference_unlocked(obj);
1281 return -EINVAL;
1282 }
1283
Linus Torvalds6be5ceb2012-04-20 17:13:58 -07001284 addr = vm_mmap(obj->filp, 0, args->size,
Eric Anholt673a3942008-07-30 12:06:12 -07001285 PROT_READ | PROT_WRITE, MAP_SHARED,
1286 args->offset);
Luca Barbieribc9025b2010-02-09 05:49:12 +00001287 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001288 if (IS_ERR((void *)addr))
1289 return addr;
1290
1291 args->addr_ptr = (uint64_t) addr;
1292
1293 return 0;
1294}
1295
Jesse Barnesde151cf2008-11-12 10:03:55 -08001296/**
1297 * i915_gem_fault - fault a page into the GTT
1298 * vma: VMA in question
1299 * vmf: fault info
1300 *
1301 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1302 * from userspace. The fault handler takes care of binding the object to
1303 * the GTT (if needed), allocating and programming a fence register (again,
1304 * only if needed based on whether the old reg is still valid or the object
1305 * is tiled) and inserting a new PTE into the faulting process.
1306 *
1307 * Note that the faulting process may involve evicting existing objects
1308 * from the GTT and/or fence registers to make room. So performance may
1309 * suffer if the GTT working set is large or there are few fence registers
1310 * left.
1311 */
1312int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1313{
Chris Wilson05394f32010-11-08 19:18:58 +00001314 struct drm_i915_gem_object *obj = to_intel_bo(vma->vm_private_data);
1315 struct drm_device *dev = obj->base.dev;
Chris Wilson7d1c4802010-08-07 21:45:03 +01001316 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001317 pgoff_t page_offset;
1318 unsigned long pfn;
1319 int ret = 0;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001320 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001321
1322 /* We don't use vmf->pgoff since that has the fake offset */
1323 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1324 PAGE_SHIFT;
1325
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001326 ret = i915_mutex_lock_interruptible(dev);
1327 if (ret)
1328 goto out;
Chris Wilsona00b10c2010-09-24 21:15:47 +01001329
Chris Wilsondb53a302011-02-03 11:57:46 +00001330 trace_i915_gem_object_fault(obj, page_offset, true, write);
1331
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001332 /* Now bind it into the GTT if needed */
Chris Wilson919926a2010-11-12 13:42:53 +00001333 if (!obj->map_and_fenceable) {
1334 ret = i915_gem_object_unbind(obj);
1335 if (ret)
1336 goto unlock;
Chris Wilsona00b10c2010-09-24 21:15:47 +01001337 }
Chris Wilson05394f32010-11-08 19:18:58 +00001338 if (!obj->gtt_space) {
Chris Wilson86a1ee22012-08-11 15:41:04 +01001339 ret = i915_gem_object_bind_to_gtt(obj, 0, true, false);
Chris Wilsonc7150892009-09-23 00:43:56 +01001340 if (ret)
1341 goto unlock;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001342
Eric Anholte92d03b2011-06-14 16:43:09 -07001343 ret = i915_gem_object_set_to_gtt_domain(obj, write);
1344 if (ret)
1345 goto unlock;
1346 }
Chris Wilson4a684a42010-10-28 14:44:08 +01001347
Daniel Vetter74898d72012-02-15 23:50:22 +01001348 if (!obj->has_global_gtt_mapping)
1349 i915_gem_gtt_bind_object(obj, obj->cache_level);
1350
Chris Wilson06d98132012-04-17 15:31:24 +01001351 ret = i915_gem_object_get_fence(obj);
Chris Wilsond9e86c02010-11-10 16:40:20 +00001352 if (ret)
1353 goto unlock;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001354
Chris Wilson05394f32010-11-08 19:18:58 +00001355 if (i915_gem_object_is_inactive(obj))
1356 list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
Chris Wilson7d1c4802010-08-07 21:45:03 +01001357
Chris Wilson6299f992010-11-24 12:23:44 +00001358 obj->fault_mappable = true;
1359
Daniel Vetterdd2757f2012-06-07 15:55:57 +02001360 pfn = ((dev_priv->mm.gtt_base_addr + obj->gtt_offset) >> PAGE_SHIFT) +
Jesse Barnesde151cf2008-11-12 10:03:55 -08001361 page_offset;
1362
1363 /* Finally, remap it using the new GTT offset */
1364 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
Chris Wilsonc7150892009-09-23 00:43:56 +01001365unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001366 mutex_unlock(&dev->struct_mutex);
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001367out:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001368 switch (ret) {
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001369 case -EIO:
Daniel Vettera9340cc2012-07-04 22:18:42 +02001370 /* If this -EIO is due to a gpu hang, give the reset code a
1371 * chance to clean up the mess. Otherwise return the proper
1372 * SIGBUS. */
1373 if (!atomic_read(&dev_priv->mm.wedged))
1374 return VM_FAULT_SIGBUS;
Chris Wilson045e7692010-11-07 09:18:22 +00001375 case -EAGAIN:
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001376 /* Give the error handler a chance to run and move the
1377 * objects off the GPU active list. Next time we service the
1378 * fault, we should be able to transition the page into the
1379 * GTT without touching the GPU (and so avoid further
1380 * EIO/EGAIN). If the GPU is wedged, then there is no issue
1381 * with coherency, just lost writes.
1382 */
Chris Wilson045e7692010-11-07 09:18:22 +00001383 set_need_resched();
Chris Wilsonc7150892009-09-23 00:43:56 +01001384 case 0:
1385 case -ERESTARTSYS:
Chris Wilsonbed636a2011-02-11 20:31:19 +00001386 case -EINTR:
Chris Wilsonc7150892009-09-23 00:43:56 +01001387 return VM_FAULT_NOPAGE;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001388 case -ENOMEM:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001389 return VM_FAULT_OOM;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001390 default:
Chris Wilsonc7150892009-09-23 00:43:56 +01001391 return VM_FAULT_SIGBUS;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001392 }
1393}
1394
1395/**
Chris Wilson901782b2009-07-10 08:18:50 +01001396 * i915_gem_release_mmap - remove physical page mappings
1397 * @obj: obj in question
1398 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001399 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01001400 * relinquish ownership of the pages back to the system.
1401 *
1402 * It is vital that we remove the page mapping if we have mapped a tiled
1403 * object through the GTT and then lose the fence register due to
1404 * resource pressure. Similarly if the object has been moved out of the
1405 * aperture, than pages mapped into userspace must be revoked. Removing the
1406 * mapping will then trigger a page fault on the next user access, allowing
1407 * fixup by i915_gem_fault().
1408 */
Eric Anholtd05ca302009-07-10 13:02:26 -07001409void
Chris Wilson05394f32010-11-08 19:18:58 +00001410i915_gem_release_mmap(struct drm_i915_gem_object *obj)
Chris Wilson901782b2009-07-10 08:18:50 +01001411{
Chris Wilson6299f992010-11-24 12:23:44 +00001412 if (!obj->fault_mappable)
1413 return;
Chris Wilson901782b2009-07-10 08:18:50 +01001414
Chris Wilsonf6e47882011-03-20 21:09:12 +00001415 if (obj->base.dev->dev_mapping)
1416 unmap_mapping_range(obj->base.dev->dev_mapping,
1417 (loff_t)obj->base.map_list.hash.key<<PAGE_SHIFT,
1418 obj->base.size, 1);
Daniel Vetterfb7d5162010-10-01 22:05:20 +02001419
Chris Wilson6299f992010-11-24 12:23:44 +00001420 obj->fault_mappable = false;
Chris Wilson901782b2009-07-10 08:18:50 +01001421}
1422
Chris Wilson92b88ae2010-11-09 11:47:32 +00001423static uint32_t
Chris Wilsone28f8712011-07-18 13:11:49 -07001424i915_gem_get_gtt_size(struct drm_device *dev, uint32_t size, int tiling_mode)
Chris Wilson92b88ae2010-11-09 11:47:32 +00001425{
Chris Wilsone28f8712011-07-18 13:11:49 -07001426 uint32_t gtt_size;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001427
1428 if (INTEL_INFO(dev)->gen >= 4 ||
Chris Wilsone28f8712011-07-18 13:11:49 -07001429 tiling_mode == I915_TILING_NONE)
1430 return size;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001431
1432 /* Previous chips need a power-of-two fence region when tiling */
1433 if (INTEL_INFO(dev)->gen == 3)
Chris Wilsone28f8712011-07-18 13:11:49 -07001434 gtt_size = 1024*1024;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001435 else
Chris Wilsone28f8712011-07-18 13:11:49 -07001436 gtt_size = 512*1024;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001437
Chris Wilsone28f8712011-07-18 13:11:49 -07001438 while (gtt_size < size)
1439 gtt_size <<= 1;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001440
Chris Wilsone28f8712011-07-18 13:11:49 -07001441 return gtt_size;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001442}
1443
Jesse Barnesde151cf2008-11-12 10:03:55 -08001444/**
1445 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1446 * @obj: object to check
1447 *
1448 * Return the required GTT alignment for an object, taking into account
Daniel Vetter5e783302010-11-14 22:32:36 +01001449 * potential fence register mapping.
Jesse Barnesde151cf2008-11-12 10:03:55 -08001450 */
1451static uint32_t
Chris Wilsone28f8712011-07-18 13:11:49 -07001452i915_gem_get_gtt_alignment(struct drm_device *dev,
1453 uint32_t size,
1454 int tiling_mode)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001455{
Jesse Barnesde151cf2008-11-12 10:03:55 -08001456 /*
1457 * Minimum alignment is 4k (GTT page size), but might be greater
1458 * if a fence register is needed for the object.
1459 */
Chris Wilsona00b10c2010-09-24 21:15:47 +01001460 if (INTEL_INFO(dev)->gen >= 4 ||
Chris Wilsone28f8712011-07-18 13:11:49 -07001461 tiling_mode == I915_TILING_NONE)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001462 return 4096;
1463
1464 /*
1465 * Previous chips need to be aligned to the size of the smallest
1466 * fence register that can contain the object.
1467 */
Chris Wilsone28f8712011-07-18 13:11:49 -07001468 return i915_gem_get_gtt_size(dev, size, tiling_mode);
Chris Wilsona00b10c2010-09-24 21:15:47 +01001469}
1470
Daniel Vetter5e783302010-11-14 22:32:36 +01001471/**
1472 * i915_gem_get_unfenced_gtt_alignment - return required GTT alignment for an
1473 * unfenced object
Chris Wilsone28f8712011-07-18 13:11:49 -07001474 * @dev: the device
1475 * @size: size of the object
1476 * @tiling_mode: tiling mode of the object
Daniel Vetter5e783302010-11-14 22:32:36 +01001477 *
1478 * Return the required GTT alignment for an object, only taking into account
1479 * unfenced tiled surface requirements.
1480 */
Chris Wilson467cffb2011-03-07 10:42:03 +00001481uint32_t
Chris Wilsone28f8712011-07-18 13:11:49 -07001482i915_gem_get_unfenced_gtt_alignment(struct drm_device *dev,
1483 uint32_t size,
1484 int tiling_mode)
Daniel Vetter5e783302010-11-14 22:32:36 +01001485{
Daniel Vetter5e783302010-11-14 22:32:36 +01001486 /*
1487 * Minimum alignment is 4k (GTT page size) for sane hw.
1488 */
1489 if (INTEL_INFO(dev)->gen >= 4 || IS_G33(dev) ||
Chris Wilsone28f8712011-07-18 13:11:49 -07001490 tiling_mode == I915_TILING_NONE)
Daniel Vetter5e783302010-11-14 22:32:36 +01001491 return 4096;
1492
Chris Wilsone28f8712011-07-18 13:11:49 -07001493 /* Previous hardware however needs to be aligned to a power-of-two
1494 * tile height. The simplest method for determining this is to reuse
1495 * the power-of-tile object size.
Daniel Vetter5e783302010-11-14 22:32:36 +01001496 */
Chris Wilsone28f8712011-07-18 13:11:49 -07001497 return i915_gem_get_gtt_size(dev, size, tiling_mode);
Daniel Vetter5e783302010-11-14 22:32:36 +01001498}
1499
Chris Wilsond8cb5082012-08-11 15:41:03 +01001500static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj)
1501{
1502 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
1503 int ret;
1504
1505 if (obj->base.map_list.map)
1506 return 0;
1507
1508 ret = drm_gem_create_mmap_offset(&obj->base);
1509 if (ret != -ENOSPC)
1510 return ret;
1511
1512 /* Badly fragmented mmap space? The only way we can recover
1513 * space is by destroying unwanted objects. We can't randomly release
1514 * mmap_offsets as userspace expects them to be persistent for the
1515 * lifetime of the objects. The closest we can is to release the
1516 * offsets on purgeable objects by truncating it and marking it purged,
1517 * which prevents userspace from ever using that object again.
1518 */
1519 i915_gem_purge(dev_priv, obj->base.size >> PAGE_SHIFT);
1520 ret = drm_gem_create_mmap_offset(&obj->base);
1521 if (ret != -ENOSPC)
1522 return ret;
1523
1524 i915_gem_shrink_all(dev_priv);
1525 return drm_gem_create_mmap_offset(&obj->base);
1526}
1527
1528static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj)
1529{
1530 if (!obj->base.map_list.map)
1531 return;
1532
1533 drm_gem_free_mmap_offset(&obj->base);
1534}
1535
Jesse Barnesde151cf2008-11-12 10:03:55 -08001536int
Dave Airlieff72145b2011-02-07 12:16:14 +10001537i915_gem_mmap_gtt(struct drm_file *file,
1538 struct drm_device *dev,
1539 uint32_t handle,
1540 uint64_t *offset)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001541{
Chris Wilsonda761a62010-10-27 17:37:08 +01001542 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00001543 struct drm_i915_gem_object *obj;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001544 int ret;
1545
Chris Wilson76c1dec2010-09-25 11:22:51 +01001546 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001547 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001548 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001549
Dave Airlieff72145b2011-02-07 12:16:14 +10001550 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00001551 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001552 ret = -ENOENT;
1553 goto unlock;
1554 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001555
Chris Wilson05394f32010-11-08 19:18:58 +00001556 if (obj->base.size > dev_priv->mm.gtt_mappable_end) {
Chris Wilsonda761a62010-10-27 17:37:08 +01001557 ret = -E2BIG;
Eric Anholtff56b0b2011-10-31 23:16:21 -07001558 goto out;
Chris Wilsonda761a62010-10-27 17:37:08 +01001559 }
1560
Chris Wilson05394f32010-11-08 19:18:58 +00001561 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilsonab182822009-09-22 18:46:17 +01001562 DRM_ERROR("Attempting to mmap a purgeable buffer\n");
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001563 ret = -EINVAL;
1564 goto out;
Chris Wilsonab182822009-09-22 18:46:17 +01001565 }
1566
Chris Wilsond8cb5082012-08-11 15:41:03 +01001567 ret = i915_gem_object_create_mmap_offset(obj);
1568 if (ret)
1569 goto out;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001570
Dave Airlieff72145b2011-02-07 12:16:14 +10001571 *offset = (u64)obj->base.map_list.hash.key << PAGE_SHIFT;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001572
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001573out:
Chris Wilson05394f32010-11-08 19:18:58 +00001574 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001575unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001576 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001577 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001578}
1579
Dave Airlieff72145b2011-02-07 12:16:14 +10001580/**
1581 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1582 * @dev: DRM device
1583 * @data: GTT mapping ioctl data
1584 * @file: GEM object info
1585 *
1586 * Simply returns the fake offset to userspace so it can mmap it.
1587 * The mmap call will end up in drm_gem_mmap(), which will set things
1588 * up so we can get faults in the handler above.
1589 *
1590 * The fault handler will take care of binding the object into the GTT
1591 * (since it may have been evicted to make room for something), allocating
1592 * a fence register, and mapping the appropriate aperture address into
1593 * userspace.
1594 */
1595int
1596i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
1597 struct drm_file *file)
1598{
1599 struct drm_i915_gem_mmap_gtt *args = data;
1600
Dave Airlieff72145b2011-02-07 12:16:14 +10001601 return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
1602}
1603
Daniel Vetter225067e2012-08-20 10:23:20 +02001604/* Immediately discard the backing storage */
1605static void
1606i915_gem_object_truncate(struct drm_i915_gem_object *obj)
Chris Wilsone5281cc2010-10-28 13:45:36 +01001607{
Chris Wilsone5281cc2010-10-28 13:45:36 +01001608 struct inode *inode;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001609
Chris Wilson4d6294bf2012-08-11 15:41:05 +01001610 i915_gem_object_free_mmap_offset(obj);
Daniel Vetter1286ff72012-05-10 15:25:09 +02001611
Chris Wilson4d6294bf2012-08-11 15:41:05 +01001612 if (obj->base.filp == NULL)
1613 return;
1614
Daniel Vetter225067e2012-08-20 10:23:20 +02001615 /* Our goal here is to return as much of the memory as
1616 * is possible back to the system as we are called from OOM.
1617 * To do this we must instruct the shmfs to drop all of its
1618 * backing pages, *now*.
Chris Wilsone5281cc2010-10-28 13:45:36 +01001619 */
Chris Wilson05394f32010-11-08 19:18:58 +00001620 inode = obj->base.filp->f_path.dentry->d_inode;
Daniel Vetter225067e2012-08-20 10:23:20 +02001621 shmem_truncate_range(inode, 0, (loff_t)-1);
Hugh Dickins5949eac2011-06-27 16:18:18 -07001622
Daniel Vetter225067e2012-08-20 10:23:20 +02001623 obj->madv = __I915_MADV_PURGED;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001624}
1625
Daniel Vetter225067e2012-08-20 10:23:20 +02001626static inline int
1627i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj)
1628{
1629 return obj->madv == I915_MADV_DONTNEED;
1630}
1631
Chris Wilson37e680a2012-06-07 15:38:42 +01001632static void
Chris Wilson05394f32010-11-08 19:18:58 +00001633i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07001634{
Chris Wilson05394f32010-11-08 19:18:58 +00001635 int page_count = obj->base.size / PAGE_SIZE;
Chris Wilson6c085a72012-08-20 11:40:46 +02001636 int ret, i;
Eric Anholt673a3942008-07-30 12:06:12 -07001637
Chris Wilson05394f32010-11-08 19:18:58 +00001638 BUG_ON(obj->madv == __I915_MADV_PURGED);
Eric Anholt856fa192009-03-19 14:10:50 -07001639
Chris Wilson6c085a72012-08-20 11:40:46 +02001640 ret = i915_gem_object_set_to_cpu_domain(obj, true);
1641 if (ret) {
1642 /* In the event of a disaster, abandon all caches and
1643 * hope for the best.
1644 */
1645 WARN_ON(ret != -EIO);
1646 i915_gem_clflush_object(obj);
1647 obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
1648 }
1649
Daniel Vetter6dacfd22011-09-12 21:30:02 +02001650 if (i915_gem_object_needs_bit17_swizzle(obj))
Eric Anholt280b7132009-03-12 16:56:27 -07001651 i915_gem_object_save_bit_17_swizzle(obj);
1652
Chris Wilson05394f32010-11-08 19:18:58 +00001653 if (obj->madv == I915_MADV_DONTNEED)
1654 obj->dirty = 0;
Chris Wilson3ef94da2009-09-14 16:50:29 +01001655
1656 for (i = 0; i < page_count; i++) {
Chris Wilson05394f32010-11-08 19:18:58 +00001657 if (obj->dirty)
1658 set_page_dirty(obj->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001659
Chris Wilson05394f32010-11-08 19:18:58 +00001660 if (obj->madv == I915_MADV_WILLNEED)
1661 mark_page_accessed(obj->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001662
Chris Wilson05394f32010-11-08 19:18:58 +00001663 page_cache_release(obj->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001664 }
Chris Wilson05394f32010-11-08 19:18:58 +00001665 obj->dirty = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001666
Chris Wilson05394f32010-11-08 19:18:58 +00001667 drm_free_large(obj->pages);
1668 obj->pages = NULL;
Chris Wilson37e680a2012-06-07 15:38:42 +01001669}
1670
1671static int
1672i915_gem_object_put_pages(struct drm_i915_gem_object *obj)
1673{
1674 const struct drm_i915_gem_object_ops *ops = obj->ops;
1675
1676 if (obj->sg_table || obj->pages == NULL)
1677 return 0;
1678
1679 BUG_ON(obj->gtt_space);
1680
Chris Wilsona5570172012-09-04 21:02:54 +01001681 if (obj->pages_pin_count)
1682 return -EBUSY;
1683
Chris Wilson37e680a2012-06-07 15:38:42 +01001684 ops->put_pages(obj);
Chris Wilson6c085a72012-08-20 11:40:46 +02001685
1686 list_del(&obj->gtt_list);
Chris Wilson6c085a72012-08-20 11:40:46 +02001687 if (i915_gem_object_is_purgeable(obj))
1688 i915_gem_object_truncate(obj);
1689
1690 return 0;
1691}
1692
1693static long
1694i915_gem_purge(struct drm_i915_private *dev_priv, long target)
1695{
1696 struct drm_i915_gem_object *obj, *next;
1697 long count = 0;
1698
1699 list_for_each_entry_safe(obj, next,
1700 &dev_priv->mm.unbound_list,
1701 gtt_list) {
1702 if (i915_gem_object_is_purgeable(obj) &&
Chris Wilson37e680a2012-06-07 15:38:42 +01001703 i915_gem_object_put_pages(obj) == 0) {
Chris Wilson6c085a72012-08-20 11:40:46 +02001704 count += obj->base.size >> PAGE_SHIFT;
1705 if (count >= target)
1706 return count;
1707 }
1708 }
1709
1710 list_for_each_entry_safe(obj, next,
1711 &dev_priv->mm.inactive_list,
1712 mm_list) {
1713 if (i915_gem_object_is_purgeable(obj) &&
1714 i915_gem_object_unbind(obj) == 0 &&
Chris Wilson37e680a2012-06-07 15:38:42 +01001715 i915_gem_object_put_pages(obj) == 0) {
Chris Wilson6c085a72012-08-20 11:40:46 +02001716 count += obj->base.size >> PAGE_SHIFT;
1717 if (count >= target)
1718 return count;
1719 }
1720 }
1721
1722 return count;
1723}
1724
1725static void
1726i915_gem_shrink_all(struct drm_i915_private *dev_priv)
1727{
1728 struct drm_i915_gem_object *obj, *next;
1729
1730 i915_gem_evict_everything(dev_priv->dev);
1731
1732 list_for_each_entry_safe(obj, next, &dev_priv->mm.unbound_list, gtt_list)
Chris Wilson37e680a2012-06-07 15:38:42 +01001733 i915_gem_object_put_pages(obj);
Daniel Vetter225067e2012-08-20 10:23:20 +02001734}
1735
Chris Wilson37e680a2012-06-07 15:38:42 +01001736static int
Chris Wilson6c085a72012-08-20 11:40:46 +02001737i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07001738{
Chris Wilson6c085a72012-08-20 11:40:46 +02001739 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07001740 int page_count, i;
1741 struct address_space *mapping;
Eric Anholt673a3942008-07-30 12:06:12 -07001742 struct page *page;
Chris Wilson6c085a72012-08-20 11:40:46 +02001743 gfp_t gfp;
Eric Anholt673a3942008-07-30 12:06:12 -07001744
Chris Wilson6c085a72012-08-20 11:40:46 +02001745 /* Assert that the object is not currently in any GPU domain. As it
1746 * wasn't in the GTT, there shouldn't be any way it could have been in
1747 * a GPU cache
1748 */
1749 BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
1750 BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
1751
Eric Anholt673a3942008-07-30 12:06:12 -07001752 /* Get the list of pages out of our struct file. They'll be pinned
1753 * at this point until we release them.
1754 */
1755 page_count = obj->base.size / PAGE_SIZE;
Eric Anholt673a3942008-07-30 12:06:12 -07001756 obj->pages = drm_malloc_ab(page_count, sizeof(struct page *));
1757 if (obj->pages == NULL)
1758 return -ENOMEM;
1759
Chris Wilson6c085a72012-08-20 11:40:46 +02001760 /* Fail silently without starting the shrinker */
1761 mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
1762 gfp = mapping_gfp_mask(mapping);
Sedat Dilekd7c3b932012-08-27 14:02:37 +02001763 gfp |= __GFP_NORETRY | __GFP_NOWARN;
Chris Wilson6c085a72012-08-20 11:40:46 +02001764 gfp &= ~(__GFP_IO | __GFP_WAIT);
Eric Anholt673a3942008-07-30 12:06:12 -07001765 for (i = 0; i < page_count; i++) {
Chris Wilson6c085a72012-08-20 11:40:46 +02001766 page = shmem_read_mapping_page_gfp(mapping, i, gfp);
1767 if (IS_ERR(page)) {
1768 i915_gem_purge(dev_priv, page_count);
1769 page = shmem_read_mapping_page_gfp(mapping, i, gfp);
1770 }
1771 if (IS_ERR(page)) {
1772 /* We've tried hard to allocate the memory by reaping
1773 * our own buffer, now let the real VM do its job and
1774 * go down in flames if truly OOM.
1775 */
Sedat Dilekd7c3b932012-08-27 14:02:37 +02001776 gfp &= ~(__GFP_NORETRY | __GFP_NOWARN);
Chris Wilson6c085a72012-08-20 11:40:46 +02001777 gfp |= __GFP_IO | __GFP_WAIT;
1778
1779 i915_gem_shrink_all(dev_priv);
1780 page = shmem_read_mapping_page_gfp(mapping, i, gfp);
1781 if (IS_ERR(page))
1782 goto err_pages;
1783
Sedat Dilekd7c3b932012-08-27 14:02:37 +02001784 gfp |= __GFP_NORETRY | __GFP_NOWARN;
Chris Wilson6c085a72012-08-20 11:40:46 +02001785 gfp &= ~(__GFP_IO | __GFP_WAIT);
1786 }
Eric Anholt673a3942008-07-30 12:06:12 -07001787
1788 obj->pages[i] = page;
1789 }
1790
1791 if (i915_gem_object_needs_bit17_swizzle(obj))
1792 i915_gem_object_do_bit_17_swizzle(obj);
1793
1794 return 0;
1795
1796err_pages:
1797 while (i--)
1798 page_cache_release(obj->pages[i]);
1799
1800 drm_free_large(obj->pages);
1801 obj->pages = NULL;
1802 return PTR_ERR(page);
Eric Anholt673a3942008-07-30 12:06:12 -07001803}
1804
Chris Wilson37e680a2012-06-07 15:38:42 +01001805/* Ensure that the associated pages are gathered from the backing storage
1806 * and pinned into our object. i915_gem_object_get_pages() may be called
1807 * multiple times before they are released by a single call to
1808 * i915_gem_object_put_pages() - once the pages are no longer referenced
1809 * either as a result of memory pressure (reaping pages under the shrinker)
1810 * or as the object is itself released.
1811 */
1812int
1813i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
1814{
1815 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
1816 const struct drm_i915_gem_object_ops *ops = obj->ops;
1817 int ret;
1818
1819 if (obj->sg_table || obj->pages)
1820 return 0;
1821
Chris Wilsona5570172012-09-04 21:02:54 +01001822 BUG_ON(obj->pages_pin_count);
1823
Chris Wilson37e680a2012-06-07 15:38:42 +01001824 ret = ops->get_pages(obj);
1825 if (ret)
1826 return ret;
1827
1828 list_add_tail(&obj->gtt_list, &dev_priv->mm.unbound_list);
1829 return 0;
1830}
1831
Chris Wilson54cf91d2010-11-25 18:00:26 +00001832void
Chris Wilson05394f32010-11-08 19:18:58 +00001833i915_gem_object_move_to_active(struct drm_i915_gem_object *obj,
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001834 struct intel_ring_buffer *ring,
1835 u32 seqno)
Eric Anholt673a3942008-07-30 12:06:12 -07001836{
Chris Wilson05394f32010-11-08 19:18:58 +00001837 struct drm_device *dev = obj->base.dev;
Chris Wilson69dc4982010-10-19 10:36:51 +01001838 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter617dbe22010-02-11 22:16:02 +01001839
Zou Nan hai852835f2010-05-21 09:08:56 +08001840 BUG_ON(ring == NULL);
Chris Wilson05394f32010-11-08 19:18:58 +00001841 obj->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001842
1843 /* Add a reference if we're newly entering the active list. */
Chris Wilson05394f32010-11-08 19:18:58 +00001844 if (!obj->active) {
1845 drm_gem_object_reference(&obj->base);
1846 obj->active = 1;
Eric Anholt673a3942008-07-30 12:06:12 -07001847 }
Daniel Vettere35a41d2010-02-11 22:13:59 +01001848
Eric Anholt673a3942008-07-30 12:06:12 -07001849 /* Move from whatever list we were on to the tail of execution. */
Chris Wilson05394f32010-11-08 19:18:58 +00001850 list_move_tail(&obj->mm_list, &dev_priv->mm.active_list);
1851 list_move_tail(&obj->ring_list, &ring->active_list);
Chris Wilsoncaea7472010-11-12 13:53:37 +00001852
Chris Wilson0201f1e2012-07-20 12:41:01 +01001853 obj->last_read_seqno = seqno;
Chris Wilson7dd49062012-03-21 10:48:18 +00001854
Chris Wilsoncaea7472010-11-12 13:53:37 +00001855 if (obj->fenced_gpu_access) {
Chris Wilsoncaea7472010-11-12 13:53:37 +00001856 obj->last_fenced_seqno = seqno;
Chris Wilsoncaea7472010-11-12 13:53:37 +00001857
Chris Wilson7dd49062012-03-21 10:48:18 +00001858 /* Bump MRU to take account of the delayed flush */
1859 if (obj->fence_reg != I915_FENCE_REG_NONE) {
1860 struct drm_i915_fence_reg *reg;
1861
1862 reg = &dev_priv->fence_regs[obj->fence_reg];
1863 list_move_tail(&reg->lru_list,
1864 &dev_priv->mm.fence_list);
1865 }
Chris Wilsoncaea7472010-11-12 13:53:37 +00001866 }
1867}
1868
1869static void
Chris Wilsoncaea7472010-11-12 13:53:37 +00001870i915_gem_object_move_to_inactive(struct drm_i915_gem_object *obj)
1871{
1872 struct drm_device *dev = obj->base.dev;
1873 struct drm_i915_private *dev_priv = dev->dev_private;
1874
Chris Wilson65ce3022012-07-20 12:41:02 +01001875 BUG_ON(obj->base.write_domain & ~I915_GEM_GPU_DOMAINS);
Chris Wilsoncaea7472010-11-12 13:53:37 +00001876 BUG_ON(!obj->active);
Chris Wilson65ce3022012-07-20 12:41:02 +01001877
Chris Wilsonf047e392012-07-21 12:31:41 +01001878 if (obj->pin_count) /* are we a framebuffer? */
1879 intel_mark_fb_idle(obj);
1880
1881 list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
1882
Chris Wilson65ce3022012-07-20 12:41:02 +01001883 list_del_init(&obj->ring_list);
Chris Wilsoncaea7472010-11-12 13:53:37 +00001884 obj->ring = NULL;
1885
Chris Wilson65ce3022012-07-20 12:41:02 +01001886 obj->last_read_seqno = 0;
1887 obj->last_write_seqno = 0;
1888 obj->base.write_domain = 0;
1889
1890 obj->last_fenced_seqno = 0;
Chris Wilsoncaea7472010-11-12 13:53:37 +00001891 obj->fenced_gpu_access = false;
Chris Wilsoncaea7472010-11-12 13:53:37 +00001892
1893 obj->active = 0;
1894 drm_gem_object_unreference(&obj->base);
1895
1896 WARN_ON(i915_verify_lists(dev));
Eric Anholtce44b0e2008-11-06 16:00:31 -08001897}
Eric Anholt673a3942008-07-30 12:06:12 -07001898
Daniel Vetter53d227f2012-01-25 16:32:49 +01001899static u32
1900i915_gem_get_seqno(struct drm_device *dev)
1901{
1902 drm_i915_private_t *dev_priv = dev->dev_private;
1903 u32 seqno = dev_priv->next_seqno;
1904
1905 /* reserve 0 for non-seqno */
1906 if (++dev_priv->next_seqno == 0)
1907 dev_priv->next_seqno = 1;
1908
1909 return seqno;
1910}
1911
1912u32
1913i915_gem_next_request_seqno(struct intel_ring_buffer *ring)
1914{
1915 if (ring->outstanding_lazy_request == 0)
1916 ring->outstanding_lazy_request = i915_gem_get_seqno(ring->dev);
1917
1918 return ring->outstanding_lazy_request;
1919}
1920
Chris Wilson3cce4692010-10-27 16:11:02 +01001921int
Chris Wilsondb53a302011-02-03 11:57:46 +00001922i915_add_request(struct intel_ring_buffer *ring,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001923 struct drm_file *file,
Chris Wilsondb53a302011-02-03 11:57:46 +00001924 struct drm_i915_gem_request *request)
Eric Anholt673a3942008-07-30 12:06:12 -07001925{
Chris Wilsondb53a302011-02-03 11:57:46 +00001926 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07001927 uint32_t seqno;
Chris Wilsona71d8d92012-02-15 11:25:36 +00001928 u32 request_ring_position;
Eric Anholt673a3942008-07-30 12:06:12 -07001929 int was_empty;
Chris Wilson3cce4692010-10-27 16:11:02 +01001930 int ret;
1931
Daniel Vettercc889e02012-06-13 20:45:19 +02001932 /*
1933 * Emit any outstanding flushes - execbuf can fail to emit the flush
1934 * after having emitted the batchbuffer command. Hence we need to fix
1935 * things up similar to emitting the lazy request. The difference here
1936 * is that the flush _must_ happen before the next request, no matter
1937 * what.
1938 */
Chris Wilsona7b97612012-07-20 12:41:08 +01001939 ret = intel_ring_flush_all_caches(ring);
1940 if (ret)
1941 return ret;
Daniel Vettercc889e02012-06-13 20:45:19 +02001942
Chris Wilson3bb73ab2012-07-20 12:40:59 +01001943 if (request == NULL) {
1944 request = kmalloc(sizeof(*request), GFP_KERNEL);
1945 if (request == NULL)
1946 return -ENOMEM;
1947 }
1948
Daniel Vetter53d227f2012-01-25 16:32:49 +01001949 seqno = i915_gem_next_request_seqno(ring);
Eric Anholt673a3942008-07-30 12:06:12 -07001950
Chris Wilsona71d8d92012-02-15 11:25:36 +00001951 /* Record the position of the start of the request so that
1952 * should we detect the updated seqno part-way through the
1953 * GPU processing the request, we never over-estimate the
1954 * position of the head.
1955 */
1956 request_ring_position = intel_ring_get_tail(ring);
1957
Chris Wilson3cce4692010-10-27 16:11:02 +01001958 ret = ring->add_request(ring, &seqno);
Chris Wilson3bb73ab2012-07-20 12:40:59 +01001959 if (ret) {
1960 kfree(request);
1961 return ret;
1962 }
Eric Anholt673a3942008-07-30 12:06:12 -07001963
Chris Wilsondb53a302011-02-03 11:57:46 +00001964 trace_i915_gem_request_add(ring, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07001965
1966 request->seqno = seqno;
Zou Nan hai852835f2010-05-21 09:08:56 +08001967 request->ring = ring;
Chris Wilsona71d8d92012-02-15 11:25:36 +00001968 request->tail = request_ring_position;
Eric Anholt673a3942008-07-30 12:06:12 -07001969 request->emitted_jiffies = jiffies;
Zou Nan hai852835f2010-05-21 09:08:56 +08001970 was_empty = list_empty(&ring->request_list);
1971 list_add_tail(&request->list, &ring->request_list);
Chris Wilson3bb73ab2012-07-20 12:40:59 +01001972 request->file_priv = NULL;
Zou Nan hai852835f2010-05-21 09:08:56 +08001973
Chris Wilsondb53a302011-02-03 11:57:46 +00001974 if (file) {
1975 struct drm_i915_file_private *file_priv = file->driver_priv;
1976
Chris Wilson1c255952010-09-26 11:03:27 +01001977 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001978 request->file_priv = file_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00001979 list_add_tail(&request->client_list,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001980 &file_priv->mm.request_list);
Chris Wilson1c255952010-09-26 11:03:27 +01001981 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00001982 }
Eric Anholt673a3942008-07-30 12:06:12 -07001983
Daniel Vetter5391d0c2012-01-25 14:03:57 +01001984 ring->outstanding_lazy_request = 0;
Chris Wilsondb53a302011-02-03 11:57:46 +00001985
Ben Gamarif65d9422009-09-14 17:48:44 -04001986 if (!dev_priv->mm.suspended) {
Ben Widawsky3e0dc6b2011-06-29 10:26:42 -07001987 if (i915_enable_hangcheck) {
1988 mod_timer(&dev_priv->hangcheck_timer,
1989 jiffies +
1990 msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
1991 }
Chris Wilsonf047e392012-07-21 12:31:41 +01001992 if (was_empty) {
Chris Wilsonb3b079d2010-09-13 23:44:34 +01001993 queue_delayed_work(dev_priv->wq,
1994 &dev_priv->mm.retire_work, HZ);
Chris Wilsonf047e392012-07-21 12:31:41 +01001995 intel_mark_busy(dev_priv->dev);
1996 }
Ben Gamarif65d9422009-09-14 17:48:44 -04001997 }
Daniel Vettercc889e02012-06-13 20:45:19 +02001998
Chris Wilson3cce4692010-10-27 16:11:02 +01001999 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002000}
2001
Chris Wilsonf787a5f2010-09-24 16:02:42 +01002002static inline void
2003i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
Eric Anholt673a3942008-07-30 12:06:12 -07002004{
Chris Wilson1c255952010-09-26 11:03:27 +01002005 struct drm_i915_file_private *file_priv = request->file_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07002006
Chris Wilson1c255952010-09-26 11:03:27 +01002007 if (!file_priv)
2008 return;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002009
Chris Wilson1c255952010-09-26 11:03:27 +01002010 spin_lock(&file_priv->mm.lock);
Herton Ronaldo Krzesinski09bfa512011-03-17 13:45:12 +00002011 if (request->file_priv) {
2012 list_del(&request->client_list);
2013 request->file_priv = NULL;
2014 }
Chris Wilson1c255952010-09-26 11:03:27 +01002015 spin_unlock(&file_priv->mm.lock);
Eric Anholt673a3942008-07-30 12:06:12 -07002016}
2017
Chris Wilsondfaae392010-09-22 10:31:52 +01002018static void i915_gem_reset_ring_lists(struct drm_i915_private *dev_priv,
2019 struct intel_ring_buffer *ring)
Chris Wilson9375e442010-09-19 12:21:28 +01002020{
Chris Wilsondfaae392010-09-22 10:31:52 +01002021 while (!list_empty(&ring->request_list)) {
2022 struct drm_i915_gem_request *request;
Chris Wilson9375e442010-09-19 12:21:28 +01002023
Chris Wilsondfaae392010-09-22 10:31:52 +01002024 request = list_first_entry(&ring->request_list,
2025 struct drm_i915_gem_request,
2026 list);
2027
2028 list_del(&request->list);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01002029 i915_gem_request_remove_from_client(request);
Chris Wilsondfaae392010-09-22 10:31:52 +01002030 kfree(request);
2031 }
2032
2033 while (!list_empty(&ring->active_list)) {
Chris Wilson05394f32010-11-08 19:18:58 +00002034 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07002035
Chris Wilson05394f32010-11-08 19:18:58 +00002036 obj = list_first_entry(&ring->active_list,
2037 struct drm_i915_gem_object,
2038 ring_list);
Eric Anholt673a3942008-07-30 12:06:12 -07002039
Chris Wilson05394f32010-11-08 19:18:58 +00002040 i915_gem_object_move_to_inactive(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002041 }
Eric Anholt673a3942008-07-30 12:06:12 -07002042}
2043
Chris Wilson312817a2010-11-22 11:50:11 +00002044static void i915_gem_reset_fences(struct drm_device *dev)
2045{
2046 struct drm_i915_private *dev_priv = dev->dev_private;
2047 int i;
2048
Daniel Vetter4b9de732011-10-09 21:52:02 +02002049 for (i = 0; i < dev_priv->num_fence_regs; i++) {
Chris Wilson312817a2010-11-22 11:50:11 +00002050 struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
Chris Wilson7d2cb392010-11-27 17:38:29 +00002051
Chris Wilsonada726c2012-04-17 15:31:32 +01002052 i915_gem_write_fence(dev, i, NULL);
Chris Wilson7d2cb392010-11-27 17:38:29 +00002053
Chris Wilsonada726c2012-04-17 15:31:32 +01002054 if (reg->obj)
2055 i915_gem_object_fence_lost(reg->obj);
Chris Wilson7d2cb392010-11-27 17:38:29 +00002056
Chris Wilsonada726c2012-04-17 15:31:32 +01002057 reg->pin_count = 0;
2058 reg->obj = NULL;
2059 INIT_LIST_HEAD(&reg->lru_list);
Chris Wilson312817a2010-11-22 11:50:11 +00002060 }
Chris Wilsonada726c2012-04-17 15:31:32 +01002061
2062 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Chris Wilson312817a2010-11-22 11:50:11 +00002063}
2064
Chris Wilson069efc12010-09-30 16:53:18 +01002065void i915_gem_reset(struct drm_device *dev)
Eric Anholt673a3942008-07-30 12:06:12 -07002066{
Chris Wilsondfaae392010-09-22 10:31:52 +01002067 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00002068 struct drm_i915_gem_object *obj;
Chris Wilsonb4519512012-05-11 14:29:30 +01002069 struct intel_ring_buffer *ring;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002070 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07002071
Chris Wilsonb4519512012-05-11 14:29:30 +01002072 for_each_ring(ring, dev_priv, i)
2073 i915_gem_reset_ring_lists(dev_priv, ring);
Chris Wilsondfaae392010-09-22 10:31:52 +01002074
Chris Wilsondfaae392010-09-22 10:31:52 +01002075 /* Move everything out of the GPU domains to ensure we do any
2076 * necessary invalidation upon reuse.
2077 */
Chris Wilson05394f32010-11-08 19:18:58 +00002078 list_for_each_entry(obj,
Chris Wilson77f01232010-09-19 12:31:36 +01002079 &dev_priv->mm.inactive_list,
Chris Wilson69dc4982010-10-19 10:36:51 +01002080 mm_list)
Chris Wilson77f01232010-09-19 12:31:36 +01002081 {
Chris Wilson05394f32010-11-08 19:18:58 +00002082 obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
Chris Wilson77f01232010-09-19 12:31:36 +01002083 }
Chris Wilson069efc12010-09-30 16:53:18 +01002084
2085 /* The fence registers are invalidated so clear them out */
Chris Wilson312817a2010-11-22 11:50:11 +00002086 i915_gem_reset_fences(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07002087}
2088
2089/**
2090 * This function clears the request list as sequence numbers are passed.
2091 */
Chris Wilsona71d8d92012-02-15 11:25:36 +00002092void
Chris Wilsondb53a302011-02-03 11:57:46 +00002093i915_gem_retire_requests_ring(struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07002094{
Eric Anholt673a3942008-07-30 12:06:12 -07002095 uint32_t seqno;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002096 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07002097
Chris Wilsondb53a302011-02-03 11:57:46 +00002098 if (list_empty(&ring->request_list))
Karsten Wiese6c0594a2009-02-23 15:07:57 +01002099 return;
2100
Chris Wilsondb53a302011-02-03 11:57:46 +00002101 WARN_ON(i915_verify_lists(ring->dev));
Eric Anholt673a3942008-07-30 12:06:12 -07002102
Chris Wilsonb2eadbc2012-08-09 10:58:30 +01002103 seqno = ring->get_seqno(ring, true);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002104
Chris Wilson076e2c02011-01-21 10:07:18 +00002105 for (i = 0; i < ARRAY_SIZE(ring->sync_seqno); i++)
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002106 if (seqno >= ring->sync_seqno[i])
2107 ring->sync_seqno[i] = 0;
2108
Zou Nan hai852835f2010-05-21 09:08:56 +08002109 while (!list_empty(&ring->request_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002110 struct drm_i915_gem_request *request;
Eric Anholt673a3942008-07-30 12:06:12 -07002111
Zou Nan hai852835f2010-05-21 09:08:56 +08002112 request = list_first_entry(&ring->request_list,
Eric Anholt673a3942008-07-30 12:06:12 -07002113 struct drm_i915_gem_request,
2114 list);
Eric Anholt673a3942008-07-30 12:06:12 -07002115
Chris Wilsondfaae392010-09-22 10:31:52 +01002116 if (!i915_seqno_passed(seqno, request->seqno))
Eric Anholt673a3942008-07-30 12:06:12 -07002117 break;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01002118
Chris Wilsondb53a302011-02-03 11:57:46 +00002119 trace_i915_gem_request_retire(ring, request->seqno);
Chris Wilsona71d8d92012-02-15 11:25:36 +00002120 /* We know the GPU must have read the request to have
2121 * sent us the seqno + interrupt, so use the position
2122 * of tail of the request to update the last known position
2123 * of the GPU head.
2124 */
2125 ring->last_retired_head = request->tail;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01002126
2127 list_del(&request->list);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01002128 i915_gem_request_remove_from_client(request);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01002129 kfree(request);
2130 }
2131
2132 /* Move any buffers on the active list that are no longer referenced
2133 * by the ringbuffer to the flushing/inactive lists as appropriate.
2134 */
2135 while (!list_empty(&ring->active_list)) {
Chris Wilson05394f32010-11-08 19:18:58 +00002136 struct drm_i915_gem_object *obj;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01002137
Akshay Joshi0206e352011-08-16 15:34:10 -04002138 obj = list_first_entry(&ring->active_list,
Chris Wilson05394f32010-11-08 19:18:58 +00002139 struct drm_i915_gem_object,
2140 ring_list);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01002141
Chris Wilson0201f1e2012-07-20 12:41:01 +01002142 if (!i915_seqno_passed(seqno, obj->last_read_seqno))
Chris Wilsonb84d5f02010-09-18 01:38:04 +01002143 break;
2144
Chris Wilson65ce3022012-07-20 12:41:02 +01002145 i915_gem_object_move_to_inactive(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002146 }
Chris Wilson9d34e5d2009-09-24 05:26:06 +01002147
Chris Wilsondb53a302011-02-03 11:57:46 +00002148 if (unlikely(ring->trace_irq_seqno &&
2149 i915_seqno_passed(seqno, ring->trace_irq_seqno))) {
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002150 ring->irq_put(ring);
Chris Wilsondb53a302011-02-03 11:57:46 +00002151 ring->trace_irq_seqno = 0;
Chris Wilson9d34e5d2009-09-24 05:26:06 +01002152 }
Chris Wilson23bc5982010-09-29 16:10:57 +01002153
Chris Wilsondb53a302011-02-03 11:57:46 +00002154 WARN_ON(i915_verify_lists(ring->dev));
Eric Anholt673a3942008-07-30 12:06:12 -07002155}
2156
2157void
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002158i915_gem_retire_requests(struct drm_device *dev)
2159{
2160 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonb4519512012-05-11 14:29:30 +01002161 struct intel_ring_buffer *ring;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002162 int i;
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002163
Chris Wilsonb4519512012-05-11 14:29:30 +01002164 for_each_ring(ring, dev_priv, i)
2165 i915_gem_retire_requests_ring(ring);
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002166}
2167
Daniel Vetter75ef9da2010-08-21 00:25:16 +02002168static void
Eric Anholt673a3942008-07-30 12:06:12 -07002169i915_gem_retire_work_handler(struct work_struct *work)
2170{
2171 drm_i915_private_t *dev_priv;
2172 struct drm_device *dev;
Chris Wilsonb4519512012-05-11 14:29:30 +01002173 struct intel_ring_buffer *ring;
Chris Wilson0a587052011-01-09 21:05:44 +00002174 bool idle;
2175 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07002176
2177 dev_priv = container_of(work, drm_i915_private_t,
2178 mm.retire_work.work);
2179 dev = dev_priv->dev;
2180
Chris Wilson891b48c2010-09-29 12:26:37 +01002181 /* Come back later if the device is busy... */
2182 if (!mutex_trylock(&dev->struct_mutex)) {
2183 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
2184 return;
2185 }
2186
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002187 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08002188
Chris Wilson0a587052011-01-09 21:05:44 +00002189 /* Send a periodic flush down the ring so we don't hold onto GEM
2190 * objects indefinitely.
2191 */
2192 idle = true;
Chris Wilsonb4519512012-05-11 14:29:30 +01002193 for_each_ring(ring, dev_priv, i) {
Chris Wilson3bb73ab2012-07-20 12:40:59 +01002194 if (ring->gpu_caches_dirty)
2195 i915_add_request(ring, NULL, NULL);
Chris Wilson0a587052011-01-09 21:05:44 +00002196
2197 idle &= list_empty(&ring->request_list);
2198 }
2199
2200 if (!dev_priv->mm.suspended && !idle)
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07002201 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
Chris Wilsonf047e392012-07-21 12:31:41 +01002202 if (idle)
2203 intel_mark_idle(dev);
Chris Wilson0a587052011-01-09 21:05:44 +00002204
Eric Anholt673a3942008-07-30 12:06:12 -07002205 mutex_unlock(&dev->struct_mutex);
2206}
2207
Ben Widawsky5816d642012-04-11 11:18:19 -07002208/**
Daniel Vetter30dfebf2012-06-01 15:21:23 +02002209 * Ensures that an object will eventually get non-busy by flushing any required
2210 * write domains, emitting any outstanding lazy request and retiring and
2211 * completed requests.
2212 */
2213static int
2214i915_gem_object_flush_active(struct drm_i915_gem_object *obj)
2215{
2216 int ret;
2217
2218 if (obj->active) {
Chris Wilson0201f1e2012-07-20 12:41:01 +01002219 ret = i915_gem_check_olr(obj->ring, obj->last_read_seqno);
Daniel Vetter30dfebf2012-06-01 15:21:23 +02002220 if (ret)
2221 return ret;
Chris Wilson0201f1e2012-07-20 12:41:01 +01002222
Daniel Vetter30dfebf2012-06-01 15:21:23 +02002223 i915_gem_retire_requests_ring(obj->ring);
2224 }
2225
2226 return 0;
2227}
2228
2229/**
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002230 * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
2231 * @DRM_IOCTL_ARGS: standard ioctl arguments
2232 *
2233 * Returns 0 if successful, else an error is returned with the remaining time in
2234 * the timeout parameter.
2235 * -ETIME: object is still busy after timeout
2236 * -ERESTARTSYS: signal interrupted the wait
2237 * -ENONENT: object doesn't exist
2238 * Also possible, but rare:
2239 * -EAGAIN: GPU wedged
2240 * -ENOMEM: damn
2241 * -ENODEV: Internal IRQ fail
2242 * -E?: The add request failed
2243 *
2244 * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
2245 * non-zero timeout parameter the wait ioctl will wait for the given number of
2246 * nanoseconds on an object becoming unbusy. Since the wait itself does so
2247 * without holding struct_mutex the object may become re-busied before this
2248 * function completes. A similar but shorter * race condition exists in the busy
2249 * ioctl
2250 */
2251int
2252i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
2253{
2254 struct drm_i915_gem_wait *args = data;
2255 struct drm_i915_gem_object *obj;
2256 struct intel_ring_buffer *ring = NULL;
Ben Widawskyeac1f142012-06-05 15:24:24 -07002257 struct timespec timeout_stack, *timeout = NULL;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002258 u32 seqno = 0;
2259 int ret = 0;
2260
Ben Widawskyeac1f142012-06-05 15:24:24 -07002261 if (args->timeout_ns >= 0) {
2262 timeout_stack = ns_to_timespec(args->timeout_ns);
2263 timeout = &timeout_stack;
2264 }
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002265
2266 ret = i915_mutex_lock_interruptible(dev);
2267 if (ret)
2268 return ret;
2269
2270 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->bo_handle));
2271 if (&obj->base == NULL) {
2272 mutex_unlock(&dev->struct_mutex);
2273 return -ENOENT;
2274 }
2275
Daniel Vetter30dfebf2012-06-01 15:21:23 +02002276 /* Need to make sure the object gets inactive eventually. */
2277 ret = i915_gem_object_flush_active(obj);
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002278 if (ret)
2279 goto out;
2280
2281 if (obj->active) {
Chris Wilson0201f1e2012-07-20 12:41:01 +01002282 seqno = obj->last_read_seqno;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002283 ring = obj->ring;
2284 }
2285
2286 if (seqno == 0)
2287 goto out;
2288
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002289 /* Do this after OLR check to make sure we make forward progress polling
2290 * on this IOCTL with a 0 timeout (like busy ioctl)
2291 */
2292 if (!args->timeout_ns) {
2293 ret = -ETIME;
2294 goto out;
2295 }
2296
2297 drm_gem_object_unreference(&obj->base);
2298 mutex_unlock(&dev->struct_mutex);
2299
Ben Widawskyeac1f142012-06-05 15:24:24 -07002300 ret = __wait_seqno(ring, seqno, true, timeout);
2301 if (timeout) {
2302 WARN_ON(!timespec_valid(timeout));
2303 args->timeout_ns = timespec_to_ns(timeout);
2304 }
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002305 return ret;
2306
2307out:
2308 drm_gem_object_unreference(&obj->base);
2309 mutex_unlock(&dev->struct_mutex);
2310 return ret;
2311}
2312
2313/**
Ben Widawsky5816d642012-04-11 11:18:19 -07002314 * i915_gem_object_sync - sync an object to a ring.
2315 *
2316 * @obj: object which may be in use on another ring.
2317 * @to: ring we wish to use the object on. May be NULL.
2318 *
2319 * This code is meant to abstract object synchronization with the GPU.
2320 * Calling with NULL implies synchronizing the object with the CPU
2321 * rather than a particular GPU ring.
2322 *
2323 * Returns 0 if successful, else propagates up the lower layer error.
2324 */
Ben Widawsky2911a352012-04-05 14:47:36 -07002325int
2326i915_gem_object_sync(struct drm_i915_gem_object *obj,
2327 struct intel_ring_buffer *to)
2328{
2329 struct intel_ring_buffer *from = obj->ring;
2330 u32 seqno;
2331 int ret, idx;
2332
2333 if (from == NULL || to == from)
2334 return 0;
2335
Ben Widawsky5816d642012-04-11 11:18:19 -07002336 if (to == NULL || !i915_semaphore_is_enabled(obj->base.dev))
Chris Wilson0201f1e2012-07-20 12:41:01 +01002337 return i915_gem_object_wait_rendering(obj, false);
Ben Widawsky2911a352012-04-05 14:47:36 -07002338
2339 idx = intel_ring_sync_index(from, to);
2340
Chris Wilson0201f1e2012-07-20 12:41:01 +01002341 seqno = obj->last_read_seqno;
Ben Widawsky2911a352012-04-05 14:47:36 -07002342 if (seqno <= from->sync_seqno[idx])
2343 return 0;
2344
Ben Widawskyb4aca012012-04-25 20:50:12 -07002345 ret = i915_gem_check_olr(obj->ring, seqno);
2346 if (ret)
2347 return ret;
Ben Widawsky2911a352012-04-05 14:47:36 -07002348
Ben Widawsky1500f7e2012-04-11 11:18:21 -07002349 ret = to->sync_to(to, from, seqno);
Ben Widawskye3a5a222012-04-11 11:18:20 -07002350 if (!ret)
2351 from->sync_seqno[idx] = seqno;
Ben Widawsky2911a352012-04-05 14:47:36 -07002352
Ben Widawskye3a5a222012-04-11 11:18:20 -07002353 return ret;
Ben Widawsky2911a352012-04-05 14:47:36 -07002354}
2355
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01002356static void i915_gem_object_finish_gtt(struct drm_i915_gem_object *obj)
2357{
2358 u32 old_write_domain, old_read_domains;
2359
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01002360 /* Act a barrier for all accesses through the GTT */
2361 mb();
2362
2363 /* Force a pagefault for domain tracking on next user access */
2364 i915_gem_release_mmap(obj);
2365
Keith Packardb97c3d92011-06-24 21:02:59 -07002366 if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
2367 return;
2368
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01002369 old_read_domains = obj->base.read_domains;
2370 old_write_domain = obj->base.write_domain;
2371
2372 obj->base.read_domains &= ~I915_GEM_DOMAIN_GTT;
2373 obj->base.write_domain &= ~I915_GEM_DOMAIN_GTT;
2374
2375 trace_i915_gem_object_change_domain(obj,
2376 old_read_domains,
2377 old_write_domain);
2378}
2379
Eric Anholt673a3942008-07-30 12:06:12 -07002380/**
2381 * Unbinds an object from the GTT aperture.
2382 */
Jesse Barnes0f973f22009-01-26 17:10:45 -08002383int
Chris Wilson05394f32010-11-08 19:18:58 +00002384i915_gem_object_unbind(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002385{
Daniel Vetter7bddb012012-02-09 17:15:47 +01002386 drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07002387 int ret = 0;
2388
Chris Wilson05394f32010-11-08 19:18:58 +00002389 if (obj->gtt_space == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002390 return 0;
2391
Chris Wilson31d8d652012-05-24 19:11:20 +01002392 if (obj->pin_count)
2393 return -EBUSY;
Eric Anholt673a3942008-07-30 12:06:12 -07002394
Chris Wilsonc4670ad2012-08-20 10:23:27 +01002395 BUG_ON(obj->pages == NULL);
2396
Chris Wilsona8198ee2011-04-13 22:04:09 +01002397 ret = i915_gem_object_finish_gpu(obj);
Chris Wilson1488fc02012-04-24 15:47:31 +01002398 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002399 return ret;
Chris Wilson8dc17752010-07-23 23:18:51 +01002400 /* Continue on if we fail due to EIO, the GPU is hung so we
2401 * should be safe and we need to cleanup or else we might
2402 * cause memory corruption through use-after-free.
2403 */
Chris Wilsona8198ee2011-04-13 22:04:09 +01002404
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01002405 i915_gem_object_finish_gtt(obj);
Chris Wilsona8198ee2011-04-13 22:04:09 +01002406
Daniel Vetter96b47b62009-12-15 17:50:00 +01002407 /* release the fence reg _after_ flushing */
Chris Wilsond9e86c02010-11-10 16:40:20 +00002408 ret = i915_gem_object_put_fence(obj);
Chris Wilson1488fc02012-04-24 15:47:31 +01002409 if (ret)
Chris Wilsond9e86c02010-11-10 16:40:20 +00002410 return ret;
Daniel Vetter96b47b62009-12-15 17:50:00 +01002411
Chris Wilsondb53a302011-02-03 11:57:46 +00002412 trace_i915_gem_object_unbind(obj);
2413
Daniel Vetter74898d72012-02-15 23:50:22 +01002414 if (obj->has_global_gtt_mapping)
2415 i915_gem_gtt_unbind_object(obj);
Daniel Vetter7bddb012012-02-09 17:15:47 +01002416 if (obj->has_aliasing_ppgtt_mapping) {
2417 i915_ppgtt_unbind_object(dev_priv->mm.aliasing_ppgtt, obj);
2418 obj->has_aliasing_ppgtt_mapping = 0;
2419 }
Daniel Vetter74163902012-02-15 23:50:21 +01002420 i915_gem_gtt_finish_object(obj);
Daniel Vetter7bddb012012-02-09 17:15:47 +01002421
Chris Wilson6c085a72012-08-20 11:40:46 +02002422 list_del(&obj->mm_list);
2423 list_move_tail(&obj->gtt_list, &dev_priv->mm.unbound_list);
Daniel Vetter75e9e912010-11-04 17:11:09 +01002424 /* Avoid an unnecessary call to unbind on rebind. */
Chris Wilson05394f32010-11-08 19:18:58 +00002425 obj->map_and_fenceable = true;
Eric Anholt673a3942008-07-30 12:06:12 -07002426
Chris Wilson05394f32010-11-08 19:18:58 +00002427 drm_mm_put_block(obj->gtt_space);
2428 obj->gtt_space = NULL;
2429 obj->gtt_offset = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002430
Chris Wilson6c085a72012-08-20 11:40:46 +02002431 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002432}
2433
Ben Widawskyb2da9fe2012-04-26 16:02:58 -07002434static int i915_ring_idle(struct intel_ring_buffer *ring)
Chris Wilsona56ba562010-09-28 10:07:56 +01002435{
Chris Wilson69c2fc82012-07-20 12:41:03 +01002436 if (list_empty(&ring->active_list))
Chris Wilson64193402010-10-24 12:38:05 +01002437 return 0;
2438
Ben Widawsky199b2bc2012-05-24 15:03:11 -07002439 return i915_wait_seqno(ring, i915_gem_next_request_seqno(ring));
Chris Wilsona56ba562010-09-28 10:07:56 +01002440}
2441
Ben Widawskyb2da9fe2012-04-26 16:02:58 -07002442int i915_gpu_idle(struct drm_device *dev)
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002443{
2444 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonb4519512012-05-11 14:29:30 +01002445 struct intel_ring_buffer *ring;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002446 int ret, i;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002447
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002448 /* Flush everything onto the inactive list. */
Chris Wilsonb4519512012-05-11 14:29:30 +01002449 for_each_ring(ring, dev_priv, i) {
Ben Widawskyb6c74882012-08-14 14:35:14 -07002450 ret = i915_switch_context(ring, NULL, DEFAULT_CONTEXT_ID);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002451 if (ret)
2452 return ret;
Chris Wilsonb4519512012-05-11 14:29:30 +01002453
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002454 ret = i915_ring_idle(ring);
Ben Widawskyf2ef6eb2012-06-04 14:42:53 -07002455 if (ret)
2456 return ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002457 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08002458
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002459 return 0;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002460}
2461
Chris Wilson9ce079e2012-04-17 15:31:30 +01002462static void sandybridge_write_fence_reg(struct drm_device *dev, int reg,
2463 struct drm_i915_gem_object *obj)
Eric Anholt4e901fd2009-10-26 16:44:17 -07002464{
Eric Anholt4e901fd2009-10-26 16:44:17 -07002465 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt4e901fd2009-10-26 16:44:17 -07002466 uint64_t val;
2467
Chris Wilson9ce079e2012-04-17 15:31:30 +01002468 if (obj) {
2469 u32 size = obj->gtt_space->size;
Eric Anholt4e901fd2009-10-26 16:44:17 -07002470
Chris Wilson9ce079e2012-04-17 15:31:30 +01002471 val = (uint64_t)((obj->gtt_offset + size - 4096) &
2472 0xfffff000) << 32;
2473 val |= obj->gtt_offset & 0xfffff000;
2474 val |= (uint64_t)((obj->stride / 128) - 1) <<
2475 SANDYBRIDGE_FENCE_PITCH_SHIFT;
Eric Anholt4e901fd2009-10-26 16:44:17 -07002476
Chris Wilson9ce079e2012-04-17 15:31:30 +01002477 if (obj->tiling_mode == I915_TILING_Y)
2478 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2479 val |= I965_FENCE_REG_VALID;
2480 } else
2481 val = 0;
Daniel Vetterc6642782010-11-12 13:46:18 +00002482
Chris Wilson9ce079e2012-04-17 15:31:30 +01002483 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + reg * 8, val);
2484 POSTING_READ(FENCE_REG_SANDYBRIDGE_0 + reg * 8);
Eric Anholt4e901fd2009-10-26 16:44:17 -07002485}
2486
Chris Wilson9ce079e2012-04-17 15:31:30 +01002487static void i965_write_fence_reg(struct drm_device *dev, int reg,
2488 struct drm_i915_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002489{
Jesse Barnesde151cf2008-11-12 10:03:55 -08002490 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002491 uint64_t val;
2492
Chris Wilson9ce079e2012-04-17 15:31:30 +01002493 if (obj) {
2494 u32 size = obj->gtt_space->size;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002495
Chris Wilson9ce079e2012-04-17 15:31:30 +01002496 val = (uint64_t)((obj->gtt_offset + size - 4096) &
2497 0xfffff000) << 32;
2498 val |= obj->gtt_offset & 0xfffff000;
2499 val |= ((obj->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
2500 if (obj->tiling_mode == I915_TILING_Y)
2501 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2502 val |= I965_FENCE_REG_VALID;
2503 } else
2504 val = 0;
Daniel Vetterc6642782010-11-12 13:46:18 +00002505
Chris Wilson9ce079e2012-04-17 15:31:30 +01002506 I915_WRITE64(FENCE_REG_965_0 + reg * 8, val);
2507 POSTING_READ(FENCE_REG_965_0 + reg * 8);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002508}
2509
Chris Wilson9ce079e2012-04-17 15:31:30 +01002510static void i915_write_fence_reg(struct drm_device *dev, int reg,
2511 struct drm_i915_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002512{
Jesse Barnesde151cf2008-11-12 10:03:55 -08002513 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson9ce079e2012-04-17 15:31:30 +01002514 u32 val;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002515
Chris Wilson9ce079e2012-04-17 15:31:30 +01002516 if (obj) {
2517 u32 size = obj->gtt_space->size;
2518 int pitch_val;
2519 int tile_width;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002520
Chris Wilson9ce079e2012-04-17 15:31:30 +01002521 WARN((obj->gtt_offset & ~I915_FENCE_START_MASK) ||
2522 (size & -size) != size ||
2523 (obj->gtt_offset & (size - 1)),
2524 "object 0x%08x [fenceable? %d] not 1M or pot-size (0x%08x) aligned\n",
2525 obj->gtt_offset, obj->map_and_fenceable, size);
2526
2527 if (obj->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev))
2528 tile_width = 128;
2529 else
2530 tile_width = 512;
2531
2532 /* Note: pitch better be a power of two tile widths */
2533 pitch_val = obj->stride / tile_width;
2534 pitch_val = ffs(pitch_val) - 1;
2535
2536 val = obj->gtt_offset;
2537 if (obj->tiling_mode == I915_TILING_Y)
2538 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2539 val |= I915_FENCE_SIZE_BITS(size);
2540 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2541 val |= I830_FENCE_REG_VALID;
2542 } else
2543 val = 0;
2544
2545 if (reg < 8)
2546 reg = FENCE_REG_830_0 + reg * 4;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002547 else
Chris Wilson9ce079e2012-04-17 15:31:30 +01002548 reg = FENCE_REG_945_8 + (reg - 8) * 4;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002549
Chris Wilson9ce079e2012-04-17 15:31:30 +01002550 I915_WRITE(reg, val);
2551 POSTING_READ(reg);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002552}
2553
Chris Wilson9ce079e2012-04-17 15:31:30 +01002554static void i830_write_fence_reg(struct drm_device *dev, int reg,
2555 struct drm_i915_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002556{
Jesse Barnesde151cf2008-11-12 10:03:55 -08002557 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002558 uint32_t val;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002559
Chris Wilson9ce079e2012-04-17 15:31:30 +01002560 if (obj) {
2561 u32 size = obj->gtt_space->size;
2562 uint32_t pitch_val;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002563
Chris Wilson9ce079e2012-04-17 15:31:30 +01002564 WARN((obj->gtt_offset & ~I830_FENCE_START_MASK) ||
2565 (size & -size) != size ||
2566 (obj->gtt_offset & (size - 1)),
2567 "object 0x%08x not 512K or pot-size 0x%08x aligned\n",
2568 obj->gtt_offset, size);
Eric Anholte76a16d2009-05-26 17:44:56 -07002569
Chris Wilson9ce079e2012-04-17 15:31:30 +01002570 pitch_val = obj->stride / 128;
2571 pitch_val = ffs(pitch_val) - 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002572
Chris Wilson9ce079e2012-04-17 15:31:30 +01002573 val = obj->gtt_offset;
2574 if (obj->tiling_mode == I915_TILING_Y)
2575 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2576 val |= I830_FENCE_SIZE_BITS(size);
2577 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2578 val |= I830_FENCE_REG_VALID;
2579 } else
2580 val = 0;
Daniel Vetterc6642782010-11-12 13:46:18 +00002581
Chris Wilson9ce079e2012-04-17 15:31:30 +01002582 I915_WRITE(FENCE_REG_830_0 + reg * 4, val);
2583 POSTING_READ(FENCE_REG_830_0 + reg * 4);
2584}
2585
2586static void i915_gem_write_fence(struct drm_device *dev, int reg,
2587 struct drm_i915_gem_object *obj)
2588{
2589 switch (INTEL_INFO(dev)->gen) {
2590 case 7:
2591 case 6: sandybridge_write_fence_reg(dev, reg, obj); break;
2592 case 5:
2593 case 4: i965_write_fence_reg(dev, reg, obj); break;
2594 case 3: i915_write_fence_reg(dev, reg, obj); break;
2595 case 2: i830_write_fence_reg(dev, reg, obj); break;
2596 default: break;
2597 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002598}
2599
Chris Wilson61050802012-04-17 15:31:31 +01002600static inline int fence_number(struct drm_i915_private *dev_priv,
2601 struct drm_i915_fence_reg *fence)
2602{
2603 return fence - dev_priv->fence_regs;
2604}
2605
2606static void i915_gem_object_update_fence(struct drm_i915_gem_object *obj,
2607 struct drm_i915_fence_reg *fence,
2608 bool enable)
2609{
2610 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2611 int reg = fence_number(dev_priv, fence);
2612
2613 i915_gem_write_fence(obj->base.dev, reg, enable ? obj : NULL);
2614
2615 if (enable) {
2616 obj->fence_reg = reg;
2617 fence->obj = obj;
2618 list_move_tail(&fence->lru_list, &dev_priv->mm.fence_list);
2619 } else {
2620 obj->fence_reg = I915_FENCE_REG_NONE;
2621 fence->obj = NULL;
2622 list_del_init(&fence->lru_list);
2623 }
2624}
2625
Chris Wilsond9e86c02010-11-10 16:40:20 +00002626static int
Chris Wilsona360bb12012-04-17 15:31:25 +01002627i915_gem_object_flush_fence(struct drm_i915_gem_object *obj)
Chris Wilsond9e86c02010-11-10 16:40:20 +00002628{
Chris Wilson1c293ea2012-04-17 15:31:27 +01002629 if (obj->last_fenced_seqno) {
Chris Wilson86d5bc32012-07-20 12:41:04 +01002630 int ret = i915_wait_seqno(obj->ring, obj->last_fenced_seqno);
Chris Wilson18991842012-04-17 15:31:29 +01002631 if (ret)
2632 return ret;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002633
2634 obj->last_fenced_seqno = 0;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002635 }
2636
Chris Wilson63256ec2011-01-04 18:42:07 +00002637 /* Ensure that all CPU reads are completed before installing a fence
2638 * and all writes before removing the fence.
2639 */
2640 if (obj->base.read_domains & I915_GEM_DOMAIN_GTT)
2641 mb();
2642
Chris Wilson86d5bc32012-07-20 12:41:04 +01002643 obj->fenced_gpu_access = false;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002644 return 0;
2645}
2646
2647int
2648i915_gem_object_put_fence(struct drm_i915_gem_object *obj)
2649{
Chris Wilson61050802012-04-17 15:31:31 +01002650 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002651 int ret;
2652
Chris Wilsona360bb12012-04-17 15:31:25 +01002653 ret = i915_gem_object_flush_fence(obj);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002654 if (ret)
2655 return ret;
2656
Chris Wilson61050802012-04-17 15:31:31 +01002657 if (obj->fence_reg == I915_FENCE_REG_NONE)
2658 return 0;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002659
Chris Wilson61050802012-04-17 15:31:31 +01002660 i915_gem_object_update_fence(obj,
2661 &dev_priv->fence_regs[obj->fence_reg],
2662 false);
2663 i915_gem_object_fence_lost(obj);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002664
2665 return 0;
2666}
2667
2668static struct drm_i915_fence_reg *
Chris Wilsona360bb12012-04-17 15:31:25 +01002669i915_find_fence_reg(struct drm_device *dev)
Daniel Vetterae3db242010-02-19 11:51:58 +01002670{
Daniel Vetterae3db242010-02-19 11:51:58 +01002671 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson8fe301a2012-04-17 15:31:28 +01002672 struct drm_i915_fence_reg *reg, *avail;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002673 int i;
Daniel Vetterae3db242010-02-19 11:51:58 +01002674
2675 /* First try to find a free reg */
Chris Wilsond9e86c02010-11-10 16:40:20 +00002676 avail = NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002677 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2678 reg = &dev_priv->fence_regs[i];
2679 if (!reg->obj)
Chris Wilsond9e86c02010-11-10 16:40:20 +00002680 return reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01002681
Chris Wilson1690e1e2011-12-14 13:57:08 +01002682 if (!reg->pin_count)
Chris Wilsond9e86c02010-11-10 16:40:20 +00002683 avail = reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01002684 }
2685
Chris Wilsond9e86c02010-11-10 16:40:20 +00002686 if (avail == NULL)
2687 return NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002688
2689 /* None available, try to steal one or wait for a user to finish */
Chris Wilsond9e86c02010-11-10 16:40:20 +00002690 list_for_each_entry(reg, &dev_priv->mm.fence_list, lru_list) {
Chris Wilson1690e1e2011-12-14 13:57:08 +01002691 if (reg->pin_count)
Daniel Vetterae3db242010-02-19 11:51:58 +01002692 continue;
2693
Chris Wilson8fe301a2012-04-17 15:31:28 +01002694 return reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01002695 }
2696
Chris Wilson8fe301a2012-04-17 15:31:28 +01002697 return NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002698}
2699
Jesse Barnesde151cf2008-11-12 10:03:55 -08002700/**
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002701 * i915_gem_object_get_fence - set up fencing for an object
Jesse Barnesde151cf2008-11-12 10:03:55 -08002702 * @obj: object to map through a fence reg
2703 *
2704 * When mapping objects through the GTT, userspace wants to be able to write
2705 * to them without having to worry about swizzling if the object is tiled.
Jesse Barnesde151cf2008-11-12 10:03:55 -08002706 * This function walks the fence regs looking for a free one for @obj,
2707 * stealing one if it can't find any.
2708 *
2709 * It then sets up the reg based on the object's properties: address, pitch
2710 * and tiling format.
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002711 *
2712 * For an untiled surface, this removes any existing fence.
Jesse Barnesde151cf2008-11-12 10:03:55 -08002713 */
Chris Wilson8c4b8c32009-06-17 22:08:52 +01002714int
Chris Wilson06d98132012-04-17 15:31:24 +01002715i915_gem_object_get_fence(struct drm_i915_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002716{
Chris Wilson05394f32010-11-08 19:18:58 +00002717 struct drm_device *dev = obj->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002718 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson14415742012-04-17 15:31:33 +01002719 bool enable = obj->tiling_mode != I915_TILING_NONE;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002720 struct drm_i915_fence_reg *reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01002721 int ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002722
Chris Wilson14415742012-04-17 15:31:33 +01002723 /* Have we updated the tiling parameters upon the object and so
2724 * will need to serialise the write to the associated fence register?
2725 */
Chris Wilson5d82e3e2012-04-21 16:23:23 +01002726 if (obj->fence_dirty) {
Chris Wilson14415742012-04-17 15:31:33 +01002727 ret = i915_gem_object_flush_fence(obj);
2728 if (ret)
2729 return ret;
2730 }
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002731
Chris Wilsond9e86c02010-11-10 16:40:20 +00002732 /* Just update our place in the LRU if our fence is getting reused. */
Chris Wilson05394f32010-11-08 19:18:58 +00002733 if (obj->fence_reg != I915_FENCE_REG_NONE) {
2734 reg = &dev_priv->fence_regs[obj->fence_reg];
Chris Wilson5d82e3e2012-04-21 16:23:23 +01002735 if (!obj->fence_dirty) {
Chris Wilson14415742012-04-17 15:31:33 +01002736 list_move_tail(&reg->lru_list,
2737 &dev_priv->mm.fence_list);
2738 return 0;
2739 }
2740 } else if (enable) {
2741 reg = i915_find_fence_reg(dev);
2742 if (reg == NULL)
2743 return -EDEADLK;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002744
Chris Wilson14415742012-04-17 15:31:33 +01002745 if (reg->obj) {
2746 struct drm_i915_gem_object *old = reg->obj;
2747
2748 ret = i915_gem_object_flush_fence(old);
Chris Wilson29c5a582011-03-17 15:23:22 +00002749 if (ret)
2750 return ret;
2751
Chris Wilson14415742012-04-17 15:31:33 +01002752 i915_gem_object_fence_lost(old);
Chris Wilson29c5a582011-03-17 15:23:22 +00002753 }
Chris Wilson14415742012-04-17 15:31:33 +01002754 } else
Eric Anholta09ba7f2009-08-29 12:49:51 -07002755 return 0;
Eric Anholta09ba7f2009-08-29 12:49:51 -07002756
Chris Wilson14415742012-04-17 15:31:33 +01002757 i915_gem_object_update_fence(obj, reg, enable);
Chris Wilson5d82e3e2012-04-21 16:23:23 +01002758 obj->fence_dirty = false;
Chris Wilson14415742012-04-17 15:31:33 +01002759
Chris Wilson9ce079e2012-04-17 15:31:30 +01002760 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002761}
2762
Chris Wilson42d6ab42012-07-26 11:49:32 +01002763static bool i915_gem_valid_gtt_space(struct drm_device *dev,
2764 struct drm_mm_node *gtt_space,
2765 unsigned long cache_level)
2766{
2767 struct drm_mm_node *other;
2768
2769 /* On non-LLC machines we have to be careful when putting differing
2770 * types of snoopable memory together to avoid the prefetcher
2771 * crossing memory domains and dieing.
2772 */
2773 if (HAS_LLC(dev))
2774 return true;
2775
2776 if (gtt_space == NULL)
2777 return true;
2778
2779 if (list_empty(&gtt_space->node_list))
2780 return true;
2781
2782 other = list_entry(gtt_space->node_list.prev, struct drm_mm_node, node_list);
2783 if (other->allocated && !other->hole_follows && other->color != cache_level)
2784 return false;
2785
2786 other = list_entry(gtt_space->node_list.next, struct drm_mm_node, node_list);
2787 if (other->allocated && !gtt_space->hole_follows && other->color != cache_level)
2788 return false;
2789
2790 return true;
2791}
2792
2793static void i915_gem_verify_gtt(struct drm_device *dev)
2794{
2795#if WATCH_GTT
2796 struct drm_i915_private *dev_priv = dev->dev_private;
2797 struct drm_i915_gem_object *obj;
2798 int err = 0;
2799
2800 list_for_each_entry(obj, &dev_priv->mm.gtt_list, gtt_list) {
2801 if (obj->gtt_space == NULL) {
2802 printk(KERN_ERR "object found on GTT list with no space reserved\n");
2803 err++;
2804 continue;
2805 }
2806
2807 if (obj->cache_level != obj->gtt_space->color) {
2808 printk(KERN_ERR "object reserved space [%08lx, %08lx] with wrong color, cache_level=%x, color=%lx\n",
2809 obj->gtt_space->start,
2810 obj->gtt_space->start + obj->gtt_space->size,
2811 obj->cache_level,
2812 obj->gtt_space->color);
2813 err++;
2814 continue;
2815 }
2816
2817 if (!i915_gem_valid_gtt_space(dev,
2818 obj->gtt_space,
2819 obj->cache_level)) {
2820 printk(KERN_ERR "invalid GTT space found at [%08lx, %08lx] - color=%x\n",
2821 obj->gtt_space->start,
2822 obj->gtt_space->start + obj->gtt_space->size,
2823 obj->cache_level);
2824 err++;
2825 continue;
2826 }
2827 }
2828
2829 WARN_ON(err);
2830#endif
2831}
2832
Jesse Barnesde151cf2008-11-12 10:03:55 -08002833/**
Eric Anholt673a3942008-07-30 12:06:12 -07002834 * Finds free space in the GTT aperture and binds the object there.
2835 */
2836static int
Chris Wilson05394f32010-11-08 19:18:58 +00002837i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
Daniel Vetter920afa72010-09-16 17:54:23 +02002838 unsigned alignment,
Chris Wilson86a1ee22012-08-11 15:41:04 +01002839 bool map_and_fenceable,
2840 bool nonblocking)
Eric Anholt673a3942008-07-30 12:06:12 -07002841{
Chris Wilson05394f32010-11-08 19:18:58 +00002842 struct drm_device *dev = obj->base.dev;
Eric Anholt673a3942008-07-30 12:06:12 -07002843 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07002844 struct drm_mm_node *free_space;
Daniel Vetter5e783302010-11-14 22:32:36 +01002845 u32 size, fence_size, fence_alignment, unfenced_alignment;
Daniel Vetter75e9e912010-11-04 17:11:09 +01002846 bool mappable, fenceable;
Chris Wilson07f73f62009-09-14 16:50:30 +01002847 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002848
Chris Wilson05394f32010-11-08 19:18:58 +00002849 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01002850 DRM_ERROR("Attempting to bind a purgeable object\n");
2851 return -EINVAL;
2852 }
2853
Chris Wilsone28f8712011-07-18 13:11:49 -07002854 fence_size = i915_gem_get_gtt_size(dev,
2855 obj->base.size,
2856 obj->tiling_mode);
2857 fence_alignment = i915_gem_get_gtt_alignment(dev,
2858 obj->base.size,
2859 obj->tiling_mode);
2860 unfenced_alignment =
2861 i915_gem_get_unfenced_gtt_alignment(dev,
2862 obj->base.size,
2863 obj->tiling_mode);
Chris Wilsona00b10c2010-09-24 21:15:47 +01002864
Eric Anholt673a3942008-07-30 12:06:12 -07002865 if (alignment == 0)
Daniel Vetter5e783302010-11-14 22:32:36 +01002866 alignment = map_and_fenceable ? fence_alignment :
2867 unfenced_alignment;
Daniel Vetter75e9e912010-11-04 17:11:09 +01002868 if (map_and_fenceable && alignment & (fence_alignment - 1)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002869 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2870 return -EINVAL;
2871 }
2872
Chris Wilson05394f32010-11-08 19:18:58 +00002873 size = map_and_fenceable ? fence_size : obj->base.size;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002874
Chris Wilson654fc602010-05-27 13:18:21 +01002875 /* If the object is bigger than the entire aperture, reject it early
2876 * before evicting everything in a vain attempt to find space.
2877 */
Chris Wilson05394f32010-11-08 19:18:58 +00002878 if (obj->base.size >
Daniel Vetter75e9e912010-11-04 17:11:09 +01002879 (map_and_fenceable ? dev_priv->mm.gtt_mappable_end : dev_priv->mm.gtt_total)) {
Chris Wilson654fc602010-05-27 13:18:21 +01002880 DRM_ERROR("Attempting to bind an object larger than the aperture\n");
2881 return -E2BIG;
2882 }
2883
Chris Wilson37e680a2012-06-07 15:38:42 +01002884 ret = i915_gem_object_get_pages(obj);
Chris Wilson6c085a72012-08-20 11:40:46 +02002885 if (ret)
2886 return ret;
2887
Eric Anholt673a3942008-07-30 12:06:12 -07002888 search_free:
Daniel Vetter75e9e912010-11-04 17:11:09 +01002889 if (map_and_fenceable)
Daniel Vetter920afa72010-09-16 17:54:23 +02002890 free_space =
Chris Wilson42d6ab42012-07-26 11:49:32 +01002891 drm_mm_search_free_in_range_color(&dev_priv->mm.gtt_space,
2892 size, alignment, obj->cache_level,
2893 0, dev_priv->mm.gtt_mappable_end,
2894 false);
Daniel Vetter920afa72010-09-16 17:54:23 +02002895 else
Chris Wilson42d6ab42012-07-26 11:49:32 +01002896 free_space = drm_mm_search_free_color(&dev_priv->mm.gtt_space,
2897 size, alignment, obj->cache_level,
2898 false);
Daniel Vetter920afa72010-09-16 17:54:23 +02002899
2900 if (free_space != NULL) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01002901 if (map_and_fenceable)
Chris Wilson05394f32010-11-08 19:18:58 +00002902 obj->gtt_space =
Daniel Vetter920afa72010-09-16 17:54:23 +02002903 drm_mm_get_block_range_generic(free_space,
Chris Wilson42d6ab42012-07-26 11:49:32 +01002904 size, alignment, obj->cache_level,
Chris Wilson6b9d89b2012-07-10 11:15:23 +01002905 0, dev_priv->mm.gtt_mappable_end,
Chris Wilson42d6ab42012-07-26 11:49:32 +01002906 false);
Daniel Vetter920afa72010-09-16 17:54:23 +02002907 else
Chris Wilson05394f32010-11-08 19:18:58 +00002908 obj->gtt_space =
Chris Wilson42d6ab42012-07-26 11:49:32 +01002909 drm_mm_get_block_generic(free_space,
2910 size, alignment, obj->cache_level,
2911 false);
Daniel Vetter920afa72010-09-16 17:54:23 +02002912 }
Chris Wilson05394f32010-11-08 19:18:58 +00002913 if (obj->gtt_space == NULL) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01002914 ret = i915_gem_evict_something(dev, size, alignment,
Chris Wilson42d6ab42012-07-26 11:49:32 +01002915 obj->cache_level,
Chris Wilson86a1ee22012-08-11 15:41:04 +01002916 map_and_fenceable,
2917 nonblocking);
Chris Wilson97311292009-09-21 00:22:34 +01002918 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002919 return ret;
Chris Wilson97311292009-09-21 00:22:34 +01002920
Eric Anholt673a3942008-07-30 12:06:12 -07002921 goto search_free;
2922 }
Chris Wilson42d6ab42012-07-26 11:49:32 +01002923 if (WARN_ON(!i915_gem_valid_gtt_space(dev,
2924 obj->gtt_space,
2925 obj->cache_level))) {
2926 drm_mm_put_block(obj->gtt_space);
2927 obj->gtt_space = NULL;
2928 return -EINVAL;
2929 }
Eric Anholt673a3942008-07-30 12:06:12 -07002930
Eric Anholt673a3942008-07-30 12:06:12 -07002931
Daniel Vetter74163902012-02-15 23:50:21 +01002932 ret = i915_gem_gtt_prepare_object(obj);
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002933 if (ret) {
Chris Wilson05394f32010-11-08 19:18:58 +00002934 drm_mm_put_block(obj->gtt_space);
2935 obj->gtt_space = NULL;
Chris Wilson6c085a72012-08-20 11:40:46 +02002936 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002937 }
Eric Anholt673a3942008-07-30 12:06:12 -07002938
Daniel Vetter0ebb9822012-02-15 23:50:24 +01002939 if (!dev_priv->mm.aliasing_ppgtt)
2940 i915_gem_gtt_bind_object(obj, obj->cache_level);
Eric Anholt673a3942008-07-30 12:06:12 -07002941
Chris Wilson6c085a72012-08-20 11:40:46 +02002942 list_move_tail(&obj->gtt_list, &dev_priv->mm.bound_list);
Chris Wilson05394f32010-11-08 19:18:58 +00002943 list_add_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
Chris Wilsonbf1a1092010-08-07 11:01:20 +01002944
Chris Wilson6299f992010-11-24 12:23:44 +00002945 obj->gtt_offset = obj->gtt_space->start;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002946
Daniel Vetter75e9e912010-11-04 17:11:09 +01002947 fenceable =
Chris Wilson05394f32010-11-08 19:18:58 +00002948 obj->gtt_space->size == fence_size &&
Akshay Joshi0206e352011-08-16 15:34:10 -04002949 (obj->gtt_space->start & (fence_alignment - 1)) == 0;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002950
Daniel Vetter75e9e912010-11-04 17:11:09 +01002951 mappable =
Chris Wilson05394f32010-11-08 19:18:58 +00002952 obj->gtt_offset + obj->base.size <= dev_priv->mm.gtt_mappable_end;
Chris Wilsona00b10c2010-09-24 21:15:47 +01002953
Chris Wilson05394f32010-11-08 19:18:58 +00002954 obj->map_and_fenceable = mappable && fenceable;
Daniel Vetter75e9e912010-11-04 17:11:09 +01002955
Chris Wilsondb53a302011-02-03 11:57:46 +00002956 trace_i915_gem_object_bind(obj, map_and_fenceable);
Chris Wilson42d6ab42012-07-26 11:49:32 +01002957 i915_gem_verify_gtt(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07002958 return 0;
2959}
2960
2961void
Chris Wilson05394f32010-11-08 19:18:58 +00002962i915_gem_clflush_object(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002963{
Eric Anholt673a3942008-07-30 12:06:12 -07002964 /* If we don't have a page list set up, then we're not pinned
2965 * to GPU, and we can ignore the cache flush because it'll happen
2966 * again at bind time.
2967 */
Chris Wilson05394f32010-11-08 19:18:58 +00002968 if (obj->pages == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002969 return;
2970
Chris Wilson9c23f7f2011-03-29 16:59:52 -07002971 /* If the GPU is snooping the contents of the CPU cache,
2972 * we do not need to manually clear the CPU cache lines. However,
2973 * the caches are only snooped when the render cache is
2974 * flushed/invalidated. As we always have to emit invalidations
2975 * and flushes when moving into and out of the RENDER domain, correct
2976 * snooping behaviour occurs naturally as the result of our domain
2977 * tracking.
2978 */
2979 if (obj->cache_level != I915_CACHE_NONE)
2980 return;
2981
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002982 trace_i915_gem_object_clflush(obj);
Eric Anholtcfa16a02009-05-26 18:46:16 -07002983
Chris Wilson05394f32010-11-08 19:18:58 +00002984 drm_clflush_pages(obj->pages, obj->base.size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002985}
2986
Eric Anholte47c68e2008-11-14 13:35:19 -08002987/** Flushes the GTT write domain for the object if it's dirty. */
2988static void
Chris Wilson05394f32010-11-08 19:18:58 +00002989i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08002990{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002991 uint32_t old_write_domain;
2992
Chris Wilson05394f32010-11-08 19:18:58 +00002993 if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
Eric Anholte47c68e2008-11-14 13:35:19 -08002994 return;
2995
Chris Wilson63256ec2011-01-04 18:42:07 +00002996 /* No actual flushing is required for the GTT write domain. Writes
Eric Anholte47c68e2008-11-14 13:35:19 -08002997 * to it immediately go to main memory as far as we know, so there's
2998 * no chipset flush. It also doesn't land in render cache.
Chris Wilson63256ec2011-01-04 18:42:07 +00002999 *
3000 * However, we do have to enforce the order so that all writes through
3001 * the GTT land before any writes to the device, such as updates to
3002 * the GATT itself.
Eric Anholte47c68e2008-11-14 13:35:19 -08003003 */
Chris Wilson63256ec2011-01-04 18:42:07 +00003004 wmb();
3005
Chris Wilson05394f32010-11-08 19:18:58 +00003006 old_write_domain = obj->base.write_domain;
3007 obj->base.write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003008
3009 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00003010 obj->base.read_domains,
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003011 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08003012}
3013
3014/** Flushes the CPU write domain for the object if it's dirty. */
3015static void
Chris Wilson05394f32010-11-08 19:18:58 +00003016i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08003017{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003018 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08003019
Chris Wilson05394f32010-11-08 19:18:58 +00003020 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
Eric Anholte47c68e2008-11-14 13:35:19 -08003021 return;
3022
3023 i915_gem_clflush_object(obj);
Daniel Vetter40ce6572010-11-05 18:12:18 +01003024 intel_gtt_chipset_flush();
Chris Wilson05394f32010-11-08 19:18:58 +00003025 old_write_domain = obj->base.write_domain;
3026 obj->base.write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003027
3028 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00003029 obj->base.read_domains,
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003030 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08003031}
3032
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003033/**
3034 * Moves a single object to the GTT read, and possibly write domain.
3035 *
3036 * This function returns when the move is complete, including waiting on
3037 * flushes to occur.
3038 */
Jesse Barnes79e53942008-11-07 14:24:08 -08003039int
Chris Wilson20217462010-11-23 15:26:33 +00003040i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003041{
Chris Wilson8325a092012-04-24 15:52:35 +01003042 drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003043 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003044 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003045
Eric Anholt02354392008-11-26 13:58:13 -08003046 /* Not valid to be called on unbound objects. */
Chris Wilson05394f32010-11-08 19:18:58 +00003047 if (obj->gtt_space == NULL)
Eric Anholt02354392008-11-26 13:58:13 -08003048 return -EINVAL;
3049
Chris Wilson8d7e3de2011-02-07 15:23:02 +00003050 if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
3051 return 0;
3052
Chris Wilson0201f1e2012-07-20 12:41:01 +01003053 ret = i915_gem_object_wait_rendering(obj, !write);
3054 if (ret)
3055 return ret;
Chris Wilson2dafb1e2010-06-07 14:03:05 +01003056
Chris Wilson72133422010-09-13 23:56:38 +01003057 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003058
Chris Wilson05394f32010-11-08 19:18:58 +00003059 old_write_domain = obj->base.write_domain;
3060 old_read_domains = obj->base.read_domains;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003061
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003062 /* It should now be out of any other write domains, and we can update
3063 * the domain values for our changes.
3064 */
Chris Wilson05394f32010-11-08 19:18:58 +00003065 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
3066 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08003067 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00003068 obj->base.read_domains = I915_GEM_DOMAIN_GTT;
3069 obj->base.write_domain = I915_GEM_DOMAIN_GTT;
3070 obj->dirty = 1;
Eric Anholte47c68e2008-11-14 13:35:19 -08003071 }
3072
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003073 trace_i915_gem_object_change_domain(obj,
3074 old_read_domains,
3075 old_write_domain);
3076
Chris Wilson8325a092012-04-24 15:52:35 +01003077 /* And bump the LRU for this access */
3078 if (i915_gem_object_is_inactive(obj))
3079 list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
3080
Eric Anholte47c68e2008-11-14 13:35:19 -08003081 return 0;
3082}
3083
Chris Wilsone4ffd172011-04-04 09:44:39 +01003084int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
3085 enum i915_cache_level cache_level)
3086{
Daniel Vetter7bddb012012-02-09 17:15:47 +01003087 struct drm_device *dev = obj->base.dev;
3088 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003089 int ret;
3090
3091 if (obj->cache_level == cache_level)
3092 return 0;
3093
3094 if (obj->pin_count) {
3095 DRM_DEBUG("can not change the cache level of pinned objects\n");
3096 return -EBUSY;
3097 }
3098
Chris Wilson42d6ab42012-07-26 11:49:32 +01003099 if (!i915_gem_valid_gtt_space(dev, obj->gtt_space, cache_level)) {
3100 ret = i915_gem_object_unbind(obj);
3101 if (ret)
3102 return ret;
3103 }
3104
Chris Wilsone4ffd172011-04-04 09:44:39 +01003105 if (obj->gtt_space) {
3106 ret = i915_gem_object_finish_gpu(obj);
3107 if (ret)
3108 return ret;
3109
3110 i915_gem_object_finish_gtt(obj);
3111
3112 /* Before SandyBridge, you could not use tiling or fence
3113 * registers with snooped memory, so relinquish any fences
3114 * currently pointing to our region in the aperture.
3115 */
Chris Wilson42d6ab42012-07-26 11:49:32 +01003116 if (INTEL_INFO(dev)->gen < 6) {
Chris Wilsone4ffd172011-04-04 09:44:39 +01003117 ret = i915_gem_object_put_fence(obj);
3118 if (ret)
3119 return ret;
3120 }
3121
Daniel Vetter74898d72012-02-15 23:50:22 +01003122 if (obj->has_global_gtt_mapping)
3123 i915_gem_gtt_bind_object(obj, cache_level);
Daniel Vetter7bddb012012-02-09 17:15:47 +01003124 if (obj->has_aliasing_ppgtt_mapping)
3125 i915_ppgtt_bind_object(dev_priv->mm.aliasing_ppgtt,
3126 obj, cache_level);
Chris Wilson42d6ab42012-07-26 11:49:32 +01003127
3128 obj->gtt_space->color = cache_level;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003129 }
3130
3131 if (cache_level == I915_CACHE_NONE) {
3132 u32 old_read_domains, old_write_domain;
3133
3134 /* If we're coming from LLC cached, then we haven't
3135 * actually been tracking whether the data is in the
3136 * CPU cache or not, since we only allow one bit set
3137 * in obj->write_domain and have been skipping the clflushes.
3138 * Just set it to the CPU cache for now.
3139 */
3140 WARN_ON(obj->base.write_domain & ~I915_GEM_DOMAIN_CPU);
3141 WARN_ON(obj->base.read_domains & ~I915_GEM_DOMAIN_CPU);
3142
3143 old_read_domains = obj->base.read_domains;
3144 old_write_domain = obj->base.write_domain;
3145
3146 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3147 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
3148
3149 trace_i915_gem_object_change_domain(obj,
3150 old_read_domains,
3151 old_write_domain);
3152 }
3153
3154 obj->cache_level = cache_level;
Chris Wilson42d6ab42012-07-26 11:49:32 +01003155 i915_gem_verify_gtt(dev);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003156 return 0;
3157}
3158
Chris Wilsone6994ae2012-07-10 10:27:08 +01003159int i915_gem_get_cacheing_ioctl(struct drm_device *dev, void *data,
3160 struct drm_file *file)
3161{
3162 struct drm_i915_gem_cacheing *args = data;
3163 struct drm_i915_gem_object *obj;
3164 int ret;
3165
3166 ret = i915_mutex_lock_interruptible(dev);
3167 if (ret)
3168 return ret;
3169
3170 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
3171 if (&obj->base == NULL) {
3172 ret = -ENOENT;
3173 goto unlock;
3174 }
3175
3176 args->cacheing = obj->cache_level != I915_CACHE_NONE;
3177
3178 drm_gem_object_unreference(&obj->base);
3179unlock:
3180 mutex_unlock(&dev->struct_mutex);
3181 return ret;
3182}
3183
3184int i915_gem_set_cacheing_ioctl(struct drm_device *dev, void *data,
3185 struct drm_file *file)
3186{
3187 struct drm_i915_gem_cacheing *args = data;
3188 struct drm_i915_gem_object *obj;
3189 enum i915_cache_level level;
3190 int ret;
3191
3192 ret = i915_mutex_lock_interruptible(dev);
3193 if (ret)
3194 return ret;
3195
3196 switch (args->cacheing) {
3197 case I915_CACHEING_NONE:
3198 level = I915_CACHE_NONE;
3199 break;
3200 case I915_CACHEING_CACHED:
3201 level = I915_CACHE_LLC;
3202 break;
3203 default:
3204 return -EINVAL;
3205 }
3206
3207 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
3208 if (&obj->base == NULL) {
3209 ret = -ENOENT;
3210 goto unlock;
3211 }
3212
3213 ret = i915_gem_object_set_cache_level(obj, level);
3214
3215 drm_gem_object_unreference(&obj->base);
3216unlock:
3217 mutex_unlock(&dev->struct_mutex);
3218 return ret;
3219}
3220
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003221/*
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003222 * Prepare buffer for display plane (scanout, cursors, etc).
3223 * Can be called from an uninterruptible phase (modesetting) and allows
3224 * any flushes to be pipelined (for pageflips).
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003225 */
3226int
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003227i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
3228 u32 alignment,
Chris Wilson919926a2010-11-12 13:42:53 +00003229 struct intel_ring_buffer *pipelined)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003230{
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003231 u32 old_read_domains, old_write_domain;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003232 int ret;
3233
Chris Wilson0be73282010-12-06 14:36:27 +00003234 if (pipelined != obj->ring) {
Ben Widawsky2911a352012-04-05 14:47:36 -07003235 ret = i915_gem_object_sync(obj, pipelined);
3236 if (ret)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003237 return ret;
3238 }
3239
Eric Anholta7ef0642011-03-29 16:59:54 -07003240 /* The display engine is not coherent with the LLC cache on gen6. As
3241 * a result, we make sure that the pinning that is about to occur is
3242 * done with uncached PTEs. This is lowest common denominator for all
3243 * chipsets.
3244 *
3245 * However for gen6+, we could do better by using the GFDT bit instead
3246 * of uncaching, which would allow us to flush all the LLC-cached data
3247 * with that bit in the PTE to main memory with just one PIPE_CONTROL.
3248 */
3249 ret = i915_gem_object_set_cache_level(obj, I915_CACHE_NONE);
3250 if (ret)
3251 return ret;
3252
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003253 /* As the user may map the buffer once pinned in the display plane
3254 * (e.g. libkms for the bootup splash), we have to ensure that we
3255 * always use map_and_fenceable for all scanout buffers.
3256 */
Chris Wilson86a1ee22012-08-11 15:41:04 +01003257 ret = i915_gem_object_pin(obj, alignment, true, false);
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003258 if (ret)
3259 return ret;
3260
Chris Wilsonb118c1e2010-05-27 13:18:14 +01003261 i915_gem_object_flush_cpu_write_domain(obj);
3262
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003263 old_write_domain = obj->base.write_domain;
Chris Wilson05394f32010-11-08 19:18:58 +00003264 old_read_domains = obj->base.read_domains;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003265
3266 /* It should now be out of any other write domains, and we can update
3267 * the domain values for our changes.
3268 */
Chris Wilsone5f1d962012-07-20 12:41:00 +01003269 obj->base.write_domain = 0;
Chris Wilson05394f32010-11-08 19:18:58 +00003270 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003271
3272 trace_i915_gem_object_change_domain(obj,
3273 old_read_domains,
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003274 old_write_domain);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003275
3276 return 0;
3277}
3278
Chris Wilson85345512010-11-13 09:49:11 +00003279int
Chris Wilsona8198ee2011-04-13 22:04:09 +01003280i915_gem_object_finish_gpu(struct drm_i915_gem_object *obj)
Chris Wilson85345512010-11-13 09:49:11 +00003281{
Chris Wilson88241782011-01-07 17:09:48 +00003282 int ret;
3283
Chris Wilsona8198ee2011-04-13 22:04:09 +01003284 if ((obj->base.read_domains & I915_GEM_GPU_DOMAINS) == 0)
Chris Wilson85345512010-11-13 09:49:11 +00003285 return 0;
3286
Chris Wilson0201f1e2012-07-20 12:41:01 +01003287 ret = i915_gem_object_wait_rendering(obj, false);
Chris Wilsonc501ae72011-12-14 13:57:23 +01003288 if (ret)
3289 return ret;
3290
Chris Wilsona8198ee2011-04-13 22:04:09 +01003291 /* Ensure that we invalidate the GPU's caches and TLBs. */
3292 obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
Chris Wilsonc501ae72011-12-14 13:57:23 +01003293 return 0;
Chris Wilson85345512010-11-13 09:49:11 +00003294}
3295
Eric Anholte47c68e2008-11-14 13:35:19 -08003296/**
3297 * Moves a single object to the CPU read, and possibly write domain.
3298 *
3299 * This function returns when the move is complete, including waiting on
3300 * flushes to occur.
3301 */
Chris Wilsondabdfe02012-03-26 10:10:27 +02003302int
Chris Wilson919926a2010-11-12 13:42:53 +00003303i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholte47c68e2008-11-14 13:35:19 -08003304{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003305 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003306 int ret;
3307
Chris Wilson8d7e3de2011-02-07 15:23:02 +00003308 if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
3309 return 0;
3310
Chris Wilson0201f1e2012-07-20 12:41:01 +01003311 ret = i915_gem_object_wait_rendering(obj, !write);
3312 if (ret)
3313 return ret;
Eric Anholte47c68e2008-11-14 13:35:19 -08003314
3315 i915_gem_object_flush_gtt_write_domain(obj);
3316
Chris Wilson05394f32010-11-08 19:18:58 +00003317 old_write_domain = obj->base.write_domain;
3318 old_read_domains = obj->base.read_domains;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003319
Eric Anholte47c68e2008-11-14 13:35:19 -08003320 /* Flush the CPU cache if it's still invalid. */
Chris Wilson05394f32010-11-08 19:18:58 +00003321 if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
Eric Anholte47c68e2008-11-14 13:35:19 -08003322 i915_gem_clflush_object(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003323
Chris Wilson05394f32010-11-08 19:18:58 +00003324 obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003325 }
3326
3327 /* It should now be out of any other write domains, and we can update
3328 * the domain values for our changes.
3329 */
Chris Wilson05394f32010-11-08 19:18:58 +00003330 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
Eric Anholte47c68e2008-11-14 13:35:19 -08003331
3332 /* If we're writing through the CPU, then the GPU read domains will
3333 * need to be invalidated at next use.
3334 */
3335 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00003336 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3337 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003338 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003339
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003340 trace_i915_gem_object_change_domain(obj,
3341 old_read_domains,
3342 old_write_domain);
3343
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003344 return 0;
3345}
3346
Eric Anholt673a3942008-07-30 12:06:12 -07003347/* Throttle our rendering by waiting until the ring has completed our requests
3348 * emitted over 20 msec ago.
3349 *
Eric Anholtb9624422009-06-03 07:27:35 +00003350 * Note that if we were to use the current jiffies each time around the loop,
3351 * we wouldn't escape the function with any frames outstanding if the time to
3352 * render a frame was over 20ms.
3353 *
Eric Anholt673a3942008-07-30 12:06:12 -07003354 * This should get us reasonable parallelism between CPU and GPU but also
3355 * relatively low latency when blocking on a particular request to finish.
3356 */
3357static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003358i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003359{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003360 struct drm_i915_private *dev_priv = dev->dev_private;
3361 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00003362 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003363 struct drm_i915_gem_request *request;
3364 struct intel_ring_buffer *ring = NULL;
3365 u32 seqno = 0;
3366 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003367
Chris Wilsone110e8d2011-01-26 15:39:14 +00003368 if (atomic_read(&dev_priv->mm.wedged))
3369 return -EIO;
3370
Chris Wilson1c255952010-09-26 11:03:27 +01003371 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003372 list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
Eric Anholtb9624422009-06-03 07:27:35 +00003373 if (time_after_eq(request->emitted_jiffies, recent_enough))
3374 break;
3375
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003376 ring = request->ring;
3377 seqno = request->seqno;
Eric Anholtb9624422009-06-03 07:27:35 +00003378 }
Chris Wilson1c255952010-09-26 11:03:27 +01003379 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003380
3381 if (seqno == 0)
3382 return 0;
3383
Ben Widawsky5c81fe852012-05-24 15:03:08 -07003384 ret = __wait_seqno(ring, seqno, true, NULL);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003385 if (ret == 0)
3386 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
Eric Anholtb9624422009-06-03 07:27:35 +00003387
Eric Anholt673a3942008-07-30 12:06:12 -07003388 return ret;
3389}
3390
Eric Anholt673a3942008-07-30 12:06:12 -07003391int
Chris Wilson05394f32010-11-08 19:18:58 +00003392i915_gem_object_pin(struct drm_i915_gem_object *obj,
3393 uint32_t alignment,
Chris Wilson86a1ee22012-08-11 15:41:04 +01003394 bool map_and_fenceable,
3395 bool nonblocking)
Eric Anholt673a3942008-07-30 12:06:12 -07003396{
Eric Anholt673a3942008-07-30 12:06:12 -07003397 int ret;
3398
Chris Wilson05394f32010-11-08 19:18:58 +00003399 BUG_ON(obj->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01003400
Chris Wilson05394f32010-11-08 19:18:58 +00003401 if (obj->gtt_space != NULL) {
3402 if ((alignment && obj->gtt_offset & (alignment - 1)) ||
3403 (map_and_fenceable && !obj->map_and_fenceable)) {
3404 WARN(obj->pin_count,
Chris Wilsonae7d49d2010-08-04 12:37:41 +01003405 "bo is already pinned with incorrect alignment:"
Daniel Vetter75e9e912010-11-04 17:11:09 +01003406 " offset=%x, req.alignment=%x, req.map_and_fenceable=%d,"
3407 " obj->map_and_fenceable=%d\n",
Chris Wilson05394f32010-11-08 19:18:58 +00003408 obj->gtt_offset, alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01003409 map_and_fenceable,
Chris Wilson05394f32010-11-08 19:18:58 +00003410 obj->map_and_fenceable);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01003411 ret = i915_gem_object_unbind(obj);
3412 if (ret)
3413 return ret;
3414 }
3415 }
3416
Chris Wilson05394f32010-11-08 19:18:58 +00003417 if (obj->gtt_space == NULL) {
Chris Wilsona00b10c2010-09-24 21:15:47 +01003418 ret = i915_gem_object_bind_to_gtt(obj, alignment,
Chris Wilson86a1ee22012-08-11 15:41:04 +01003419 map_and_fenceable,
3420 nonblocking);
Chris Wilson97311292009-09-21 00:22:34 +01003421 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07003422 return ret;
Chris Wilson22c344e2009-02-11 14:26:45 +00003423 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05003424
Daniel Vetter74898d72012-02-15 23:50:22 +01003425 if (!obj->has_global_gtt_mapping && map_and_fenceable)
3426 i915_gem_gtt_bind_object(obj, obj->cache_level);
3427
Chris Wilson1b502472012-04-24 15:47:30 +01003428 obj->pin_count++;
Chris Wilson6299f992010-11-24 12:23:44 +00003429 obj->pin_mappable |= map_and_fenceable;
Eric Anholt673a3942008-07-30 12:06:12 -07003430
3431 return 0;
3432}
3433
3434void
Chris Wilson05394f32010-11-08 19:18:58 +00003435i915_gem_object_unpin(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07003436{
Chris Wilson05394f32010-11-08 19:18:58 +00003437 BUG_ON(obj->pin_count == 0);
3438 BUG_ON(obj->gtt_space == NULL);
Eric Anholt673a3942008-07-30 12:06:12 -07003439
Chris Wilson1b502472012-04-24 15:47:30 +01003440 if (--obj->pin_count == 0)
Chris Wilson6299f992010-11-24 12:23:44 +00003441 obj->pin_mappable = false;
Eric Anholt673a3942008-07-30 12:06:12 -07003442}
3443
3444int
3445i915_gem_pin_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00003446 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003447{
3448 struct drm_i915_gem_pin *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003449 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07003450 int ret;
3451
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003452 ret = i915_mutex_lock_interruptible(dev);
3453 if (ret)
3454 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003455
Chris Wilson05394f32010-11-08 19:18:58 +00003456 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00003457 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003458 ret = -ENOENT;
3459 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07003460 }
Eric Anholt673a3942008-07-30 12:06:12 -07003461
Chris Wilson05394f32010-11-08 19:18:58 +00003462 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilsonbb6baf72009-09-22 14:24:13 +01003463 DRM_ERROR("Attempting to pin a purgeable buffer\n");
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003464 ret = -EINVAL;
3465 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003466 }
3467
Chris Wilson05394f32010-11-08 19:18:58 +00003468 if (obj->pin_filp != NULL && obj->pin_filp != file) {
Jesse Barnes79e53942008-11-07 14:24:08 -08003469 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
3470 args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003471 ret = -EINVAL;
3472 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08003473 }
3474
Chris Wilson05394f32010-11-08 19:18:58 +00003475 obj->user_pin_count++;
3476 obj->pin_filp = file;
3477 if (obj->user_pin_count == 1) {
Chris Wilson86a1ee22012-08-11 15:41:04 +01003478 ret = i915_gem_object_pin(obj, args->alignment, true, false);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003479 if (ret)
3480 goto out;
Eric Anholt673a3942008-07-30 12:06:12 -07003481 }
3482
3483 /* XXX - flush the CPU caches for pinned objects
3484 * as the X server doesn't manage domains yet
3485 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003486 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00003487 args->offset = obj->gtt_offset;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003488out:
Chris Wilson05394f32010-11-08 19:18:58 +00003489 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003490unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07003491 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003492 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003493}
3494
3495int
3496i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00003497 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003498{
3499 struct drm_i915_gem_pin *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003500 struct drm_i915_gem_object *obj;
Chris Wilson76c1dec2010-09-25 11:22:51 +01003501 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003502
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003503 ret = i915_mutex_lock_interruptible(dev);
3504 if (ret)
3505 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003506
Chris Wilson05394f32010-11-08 19:18:58 +00003507 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00003508 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003509 ret = -ENOENT;
3510 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07003511 }
Chris Wilson76c1dec2010-09-25 11:22:51 +01003512
Chris Wilson05394f32010-11-08 19:18:58 +00003513 if (obj->pin_filp != file) {
Jesse Barnes79e53942008-11-07 14:24:08 -08003514 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
3515 args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003516 ret = -EINVAL;
3517 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08003518 }
Chris Wilson05394f32010-11-08 19:18:58 +00003519 obj->user_pin_count--;
3520 if (obj->user_pin_count == 0) {
3521 obj->pin_filp = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08003522 i915_gem_object_unpin(obj);
3523 }
Eric Anholt673a3942008-07-30 12:06:12 -07003524
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003525out:
Chris Wilson05394f32010-11-08 19:18:58 +00003526 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003527unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07003528 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003529 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003530}
3531
3532int
3533i915_gem_busy_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00003534 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003535{
3536 struct drm_i915_gem_busy *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003537 struct drm_i915_gem_object *obj;
Chris Wilson30dbf0c2010-09-25 10:19:17 +01003538 int ret;
3539
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003540 ret = i915_mutex_lock_interruptible(dev);
3541 if (ret)
3542 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003543
Chris Wilson05394f32010-11-08 19:18:58 +00003544 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00003545 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003546 ret = -ENOENT;
3547 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07003548 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08003549
Chris Wilson0be555b2010-08-04 15:36:30 +01003550 /* Count all active objects as busy, even if they are currently not used
3551 * by the gpu. Users of this interface expect objects to eventually
3552 * become non-busy without any further actions, therefore emit any
3553 * necessary flushes here.
Eric Anholtc4de0a52008-12-14 19:05:04 -08003554 */
Daniel Vetter30dfebf2012-06-01 15:21:23 +02003555 ret = i915_gem_object_flush_active(obj);
3556
Chris Wilson05394f32010-11-08 19:18:58 +00003557 args->busy = obj->active;
Chris Wilsone9808ed2012-07-04 12:25:08 +01003558 if (obj->ring) {
3559 BUILD_BUG_ON(I915_NUM_RINGS > 16);
3560 args->busy |= intel_ring_flag(obj->ring) << 16;
3561 }
Eric Anholt673a3942008-07-30 12:06:12 -07003562
Chris Wilson05394f32010-11-08 19:18:58 +00003563 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003564unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07003565 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003566 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003567}
3568
3569int
3570i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
3571 struct drm_file *file_priv)
3572{
Akshay Joshi0206e352011-08-16 15:34:10 -04003573 return i915_gem_ring_throttle(dev, file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07003574}
3575
Chris Wilson3ef94da2009-09-14 16:50:29 +01003576int
3577i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
3578 struct drm_file *file_priv)
3579{
3580 struct drm_i915_gem_madvise *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003581 struct drm_i915_gem_object *obj;
Chris Wilson76c1dec2010-09-25 11:22:51 +01003582 int ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003583
3584 switch (args->madv) {
3585 case I915_MADV_DONTNEED:
3586 case I915_MADV_WILLNEED:
3587 break;
3588 default:
3589 return -EINVAL;
3590 }
3591
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003592 ret = i915_mutex_lock_interruptible(dev);
3593 if (ret)
3594 return ret;
3595
Chris Wilson05394f32010-11-08 19:18:58 +00003596 obj = to_intel_bo(drm_gem_object_lookup(dev, file_priv, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00003597 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003598 ret = -ENOENT;
3599 goto unlock;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003600 }
Chris Wilson3ef94da2009-09-14 16:50:29 +01003601
Chris Wilson05394f32010-11-08 19:18:58 +00003602 if (obj->pin_count) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003603 ret = -EINVAL;
3604 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003605 }
3606
Chris Wilson05394f32010-11-08 19:18:58 +00003607 if (obj->madv != __I915_MADV_PURGED)
3608 obj->madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003609
Chris Wilson6c085a72012-08-20 11:40:46 +02003610 /* if the object is no longer attached, discard its backing storage */
3611 if (i915_gem_object_is_purgeable(obj) && obj->pages == NULL)
Chris Wilson2d7ef392009-09-20 23:13:10 +01003612 i915_gem_object_truncate(obj);
3613
Chris Wilson05394f32010-11-08 19:18:58 +00003614 args->retained = obj->madv != __I915_MADV_PURGED;
Chris Wilsonbb6baf72009-09-22 14:24:13 +01003615
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003616out:
Chris Wilson05394f32010-11-08 19:18:58 +00003617 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003618unlock:
Chris Wilson3ef94da2009-09-14 16:50:29 +01003619 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003620 return ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003621}
3622
Chris Wilson37e680a2012-06-07 15:38:42 +01003623void i915_gem_object_init(struct drm_i915_gem_object *obj,
3624 const struct drm_i915_gem_object_ops *ops)
Chris Wilson0327d6b2012-08-11 15:41:06 +01003625{
Chris Wilson0327d6b2012-08-11 15:41:06 +01003626 INIT_LIST_HEAD(&obj->mm_list);
3627 INIT_LIST_HEAD(&obj->gtt_list);
3628 INIT_LIST_HEAD(&obj->ring_list);
3629 INIT_LIST_HEAD(&obj->exec_list);
3630
Chris Wilson37e680a2012-06-07 15:38:42 +01003631 obj->ops = ops;
3632
Chris Wilson0327d6b2012-08-11 15:41:06 +01003633 obj->fence_reg = I915_FENCE_REG_NONE;
3634 obj->madv = I915_MADV_WILLNEED;
3635 /* Avoid an unnecessary call to unbind on the first bind. */
3636 obj->map_and_fenceable = true;
3637
3638 i915_gem_info_add_obj(obj->base.dev->dev_private, obj->base.size);
3639}
3640
Chris Wilson37e680a2012-06-07 15:38:42 +01003641static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
3642 .get_pages = i915_gem_object_get_pages_gtt,
3643 .put_pages = i915_gem_object_put_pages_gtt,
3644};
3645
Chris Wilson05394f32010-11-08 19:18:58 +00003646struct drm_i915_gem_object *i915_gem_alloc_object(struct drm_device *dev,
3647 size_t size)
Daniel Vetterac52bc52010-04-09 19:05:06 +00003648{
Daniel Vetterc397b902010-04-09 19:05:07 +00003649 struct drm_i915_gem_object *obj;
Hugh Dickins5949eac2011-06-27 16:18:18 -07003650 struct address_space *mapping;
Chris Wilsonbed1ea92012-05-24 20:48:12 +01003651 u32 mask;
Daniel Vetterc397b902010-04-09 19:05:07 +00003652
3653 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
3654 if (obj == NULL)
3655 return NULL;
3656
3657 if (drm_gem_object_init(dev, &obj->base, size) != 0) {
3658 kfree(obj);
3659 return NULL;
3660 }
3661
Chris Wilsonbed1ea92012-05-24 20:48:12 +01003662 mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
3663 if (IS_CRESTLINE(dev) || IS_BROADWATER(dev)) {
3664 /* 965gm cannot relocate objects above 4GiB. */
3665 mask &= ~__GFP_HIGHMEM;
3666 mask |= __GFP_DMA32;
3667 }
3668
Hugh Dickins5949eac2011-06-27 16:18:18 -07003669 mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Chris Wilsonbed1ea92012-05-24 20:48:12 +01003670 mapping_set_gfp_mask(mapping, mask);
Hugh Dickins5949eac2011-06-27 16:18:18 -07003671
Chris Wilson37e680a2012-06-07 15:38:42 +01003672 i915_gem_object_init(obj, &i915_gem_object_ops);
Chris Wilson73aa8082010-09-30 11:46:12 +01003673
Daniel Vetterc397b902010-04-09 19:05:07 +00003674 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
3675 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3676
Eugeni Dodonov3d29b842012-01-17 14:43:53 -02003677 if (HAS_LLC(dev)) {
3678 /* On some devices, we can have the GPU use the LLC (the CPU
Eric Anholta1871112011-03-29 16:59:55 -07003679 * cache) for about a 10% performance improvement
3680 * compared to uncached. Graphics requests other than
3681 * display scanout are coherent with the CPU in
3682 * accessing this cache. This means in this mode we
3683 * don't need to clflush on the CPU side, and on the
3684 * GPU side we only need to flush internal caches to
3685 * get data visible to the CPU.
3686 *
3687 * However, we maintain the display planes as UC, and so
3688 * need to rebind when first used as such.
3689 */
3690 obj->cache_level = I915_CACHE_LLC;
3691 } else
3692 obj->cache_level = I915_CACHE_NONE;
3693
Chris Wilson05394f32010-11-08 19:18:58 +00003694 return obj;
Daniel Vetterac52bc52010-04-09 19:05:06 +00003695}
3696
Eric Anholt673a3942008-07-30 12:06:12 -07003697int i915_gem_init_object(struct drm_gem_object *obj)
3698{
Daniel Vetterc397b902010-04-09 19:05:07 +00003699 BUG();
Jesse Barnesde151cf2008-11-12 10:03:55 -08003700
Eric Anholt673a3942008-07-30 12:06:12 -07003701 return 0;
3702}
3703
Chris Wilson1488fc02012-04-24 15:47:31 +01003704void i915_gem_free_object(struct drm_gem_object *gem_obj)
Chris Wilsonbe726152010-07-23 23:18:50 +01003705{
Chris Wilson1488fc02012-04-24 15:47:31 +01003706 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
Chris Wilson05394f32010-11-08 19:18:58 +00003707 struct drm_device *dev = obj->base.dev;
Chris Wilsonbe726152010-07-23 23:18:50 +01003708 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonbe726152010-07-23 23:18:50 +01003709
Chris Wilson26e12f892011-03-20 11:20:19 +00003710 trace_i915_gem_object_destroy(obj);
3711
Daniel Vetter1286ff72012-05-10 15:25:09 +02003712 if (gem_obj->import_attach)
3713 drm_prime_gem_destroy(gem_obj, obj->sg_table);
3714
Chris Wilson1488fc02012-04-24 15:47:31 +01003715 if (obj->phys_obj)
3716 i915_gem_detach_phys_object(dev, obj);
3717
3718 obj->pin_count = 0;
3719 if (WARN_ON(i915_gem_object_unbind(obj) == -ERESTARTSYS)) {
3720 bool was_interruptible;
3721
3722 was_interruptible = dev_priv->mm.interruptible;
3723 dev_priv->mm.interruptible = false;
3724
3725 WARN_ON(i915_gem_object_unbind(obj));
3726
3727 dev_priv->mm.interruptible = was_interruptible;
3728 }
3729
Chris Wilsona5570172012-09-04 21:02:54 +01003730 obj->pages_pin_count = 0;
Chris Wilson37e680a2012-06-07 15:38:42 +01003731 i915_gem_object_put_pages(obj);
Chris Wilsond8cb5082012-08-11 15:41:03 +01003732 i915_gem_object_free_mmap_offset(obj);
Chris Wilsonbe726152010-07-23 23:18:50 +01003733
Chris Wilson05394f32010-11-08 19:18:58 +00003734 drm_gem_object_release(&obj->base);
3735 i915_gem_info_remove_obj(dev_priv, obj->base.size);
Chris Wilsonbe726152010-07-23 23:18:50 +01003736
Chris Wilson05394f32010-11-08 19:18:58 +00003737 kfree(obj->bit_17);
3738 kfree(obj);
Chris Wilsonbe726152010-07-23 23:18:50 +01003739}
3740
Jesse Barnes5669fca2009-02-17 15:13:31 -08003741int
Eric Anholt673a3942008-07-30 12:06:12 -07003742i915_gem_idle(struct drm_device *dev)
3743{
3744 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson29105cc2010-01-07 10:39:13 +00003745 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003746
Keith Packard6dbe2772008-10-14 21:41:13 -07003747 mutex_lock(&dev->struct_mutex);
3748
Chris Wilson87acb0a2010-10-19 10:13:00 +01003749 if (dev_priv->mm.suspended) {
Keith Packard6dbe2772008-10-14 21:41:13 -07003750 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07003751 return 0;
Keith Packard6dbe2772008-10-14 21:41:13 -07003752 }
Eric Anholt673a3942008-07-30 12:06:12 -07003753
Ben Widawskyb2da9fe2012-04-26 16:02:58 -07003754 ret = i915_gpu_idle(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07003755 if (ret) {
3756 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07003757 return ret;
Keith Packard6dbe2772008-10-14 21:41:13 -07003758 }
Ben Widawskyb2da9fe2012-04-26 16:02:58 -07003759 i915_gem_retire_requests(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07003760
Chris Wilson29105cc2010-01-07 10:39:13 +00003761 /* Under UMS, be paranoid and evict. */
Chris Wilsona39d7ef2012-04-24 18:22:52 +01003762 if (!drm_core_check_feature(dev, DRIVER_MODESET))
Chris Wilson6c085a72012-08-20 11:40:46 +02003763 i915_gem_evict_everything(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00003764
Chris Wilson312817a2010-11-22 11:50:11 +00003765 i915_gem_reset_fences(dev);
3766
Chris Wilson29105cc2010-01-07 10:39:13 +00003767 /* Hack! Don't let anybody do execbuf while we don't control the chip.
3768 * We need to replace this with a semaphore, or something.
3769 * And not confound mm.suspended!
3770 */
3771 dev_priv->mm.suspended = 1;
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02003772 del_timer_sync(&dev_priv->hangcheck_timer);
Chris Wilson29105cc2010-01-07 10:39:13 +00003773
3774 i915_kernel_lost_context(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07003775 i915_gem_cleanup_ringbuffer(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00003776
Keith Packard6dbe2772008-10-14 21:41:13 -07003777 mutex_unlock(&dev->struct_mutex);
3778
Chris Wilson29105cc2010-01-07 10:39:13 +00003779 /* Cancel the retire work handler, which should be idle now. */
3780 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
3781
Eric Anholt673a3942008-07-30 12:06:12 -07003782 return 0;
3783}
3784
Ben Widawskyb9524a12012-05-25 16:56:24 -07003785void i915_gem_l3_remap(struct drm_device *dev)
3786{
3787 drm_i915_private_t *dev_priv = dev->dev_private;
3788 u32 misccpctl;
3789 int i;
3790
3791 if (!IS_IVYBRIDGE(dev))
3792 return;
3793
3794 if (!dev_priv->mm.l3_remap_info)
3795 return;
3796
3797 misccpctl = I915_READ(GEN7_MISCCPCTL);
3798 I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
3799 POSTING_READ(GEN7_MISCCPCTL);
3800
3801 for (i = 0; i < GEN7_L3LOG_SIZE; i += 4) {
3802 u32 remap = I915_READ(GEN7_L3LOG_BASE + i);
3803 if (remap && remap != dev_priv->mm.l3_remap_info[i/4])
3804 DRM_DEBUG("0x%x was already programmed to %x\n",
3805 GEN7_L3LOG_BASE + i, remap);
3806 if (remap && !dev_priv->mm.l3_remap_info[i/4])
3807 DRM_DEBUG_DRIVER("Clearing remapped register\n");
3808 I915_WRITE(GEN7_L3LOG_BASE + i, dev_priv->mm.l3_remap_info[i/4]);
3809 }
3810
3811 /* Make sure all the writes land before disabling dop clock gating */
3812 POSTING_READ(GEN7_L3LOG_BASE);
3813
3814 I915_WRITE(GEN7_MISCCPCTL, misccpctl);
3815}
3816
Daniel Vetterf691e2f2012-02-02 09:58:12 +01003817void i915_gem_init_swizzling(struct drm_device *dev)
3818{
3819 drm_i915_private_t *dev_priv = dev->dev_private;
3820
Daniel Vetter11782b02012-01-31 16:47:55 +01003821 if (INTEL_INFO(dev)->gen < 5 ||
Daniel Vetterf691e2f2012-02-02 09:58:12 +01003822 dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
3823 return;
3824
3825 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
3826 DISP_TILE_SURFACE_SWIZZLING);
3827
Daniel Vetter11782b02012-01-31 16:47:55 +01003828 if (IS_GEN5(dev))
3829 return;
3830
Daniel Vetterf691e2f2012-02-02 09:58:12 +01003831 I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
3832 if (IS_GEN6(dev))
Daniel Vetter6b26c862012-04-24 14:04:12 +02003833 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
Daniel Vetterf691e2f2012-02-02 09:58:12 +01003834 else
Daniel Vetter6b26c862012-04-24 14:04:12 +02003835 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
Daniel Vetterf691e2f2012-02-02 09:58:12 +01003836}
Daniel Vettere21af882012-02-09 20:53:27 +01003837
3838void i915_gem_init_ppgtt(struct drm_device *dev)
3839{
3840 drm_i915_private_t *dev_priv = dev->dev_private;
3841 uint32_t pd_offset;
3842 struct intel_ring_buffer *ring;
Daniel Vetter55a254a2012-03-22 00:14:43 +01003843 struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
3844 uint32_t __iomem *pd_addr;
3845 uint32_t pd_entry;
Daniel Vettere21af882012-02-09 20:53:27 +01003846 int i;
3847
3848 if (!dev_priv->mm.aliasing_ppgtt)
3849 return;
3850
Daniel Vetter55a254a2012-03-22 00:14:43 +01003851
3852 pd_addr = dev_priv->mm.gtt->gtt + ppgtt->pd_offset/sizeof(uint32_t);
3853 for (i = 0; i < ppgtt->num_pd_entries; i++) {
3854 dma_addr_t pt_addr;
3855
3856 if (dev_priv->mm.gtt->needs_dmar)
3857 pt_addr = ppgtt->pt_dma_addr[i];
3858 else
3859 pt_addr = page_to_phys(ppgtt->pt_pages[i]);
3860
3861 pd_entry = GEN6_PDE_ADDR_ENCODE(pt_addr);
3862 pd_entry |= GEN6_PDE_VALID;
3863
3864 writel(pd_entry, pd_addr + i);
3865 }
3866 readl(pd_addr);
3867
3868 pd_offset = ppgtt->pd_offset;
Daniel Vettere21af882012-02-09 20:53:27 +01003869 pd_offset /= 64; /* in cachelines, */
3870 pd_offset <<= 16;
3871
3872 if (INTEL_INFO(dev)->gen == 6) {
Daniel Vetter48ecfa12012-04-11 20:42:40 +02003873 uint32_t ecochk, gab_ctl, ecobits;
3874
3875 ecobits = I915_READ(GAC_ECO_BITS);
3876 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B);
Daniel Vetterbe901a52012-04-11 20:42:39 +02003877
3878 gab_ctl = I915_READ(GAB_CTL);
3879 I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT);
3880
3881 ecochk = I915_READ(GAM_ECOCHK);
Daniel Vettere21af882012-02-09 20:53:27 +01003882 I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT |
3883 ECOCHK_PPGTT_CACHE64B);
Daniel Vetter6b26c862012-04-24 14:04:12 +02003884 I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Daniel Vettere21af882012-02-09 20:53:27 +01003885 } else if (INTEL_INFO(dev)->gen >= 7) {
3886 I915_WRITE(GAM_ECOCHK, ECOCHK_PPGTT_CACHE64B);
3887 /* GFX_MODE is per-ring on gen7+ */
3888 }
3889
Chris Wilsonb4519512012-05-11 14:29:30 +01003890 for_each_ring(ring, dev_priv, i) {
Daniel Vettere21af882012-02-09 20:53:27 +01003891 if (INTEL_INFO(dev)->gen >= 7)
3892 I915_WRITE(RING_MODE_GEN7(ring),
Daniel Vetter6b26c862012-04-24 14:04:12 +02003893 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Daniel Vettere21af882012-02-09 20:53:27 +01003894
3895 I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
3896 I915_WRITE(RING_PP_DIR_BASE(ring), pd_offset);
3897 }
3898}
3899
Chris Wilson67b1b572012-07-05 23:49:40 +01003900static bool
3901intel_enable_blt(struct drm_device *dev)
3902{
3903 if (!HAS_BLT(dev))
3904 return false;
3905
3906 /* The blitter was dysfunctional on early prototypes */
3907 if (IS_GEN6(dev) && dev->pdev->revision < 8) {
3908 DRM_INFO("BLT not supported on this pre-production hardware;"
3909 " graphics performance will be degraded.\n");
3910 return false;
3911 }
3912
3913 return true;
3914}
3915
Eric Anholt673a3942008-07-30 12:06:12 -07003916int
Daniel Vetterf691e2f2012-02-02 09:58:12 +01003917i915_gem_init_hw(struct drm_device *dev)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08003918{
3919 drm_i915_private_t *dev_priv = dev->dev_private;
3920 int ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01003921
Daniel Vetter8ecd1a62012-06-07 15:56:03 +02003922 if (!intel_enable_gtt())
3923 return -EIO;
3924
Ben Widawskyb9524a12012-05-25 16:56:24 -07003925 i915_gem_l3_remap(dev);
3926
Daniel Vetterf691e2f2012-02-02 09:58:12 +01003927 i915_gem_init_swizzling(dev);
3928
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08003929 ret = intel_init_render_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01003930 if (ret)
Chris Wilsonb6913e42010-11-12 10:46:37 +00003931 return ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01003932
3933 if (HAS_BSD(dev)) {
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08003934 ret = intel_init_bsd_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01003935 if (ret)
3936 goto cleanup_render_ring;
Zou Nan haid1b851f2010-05-21 09:08:57 +08003937 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01003938
Chris Wilson67b1b572012-07-05 23:49:40 +01003939 if (intel_enable_blt(dev)) {
Chris Wilson549f7362010-10-19 11:19:32 +01003940 ret = intel_init_blt_ring_buffer(dev);
3941 if (ret)
3942 goto cleanup_bsd_ring;
3943 }
3944
Chris Wilson6f392d5482010-08-07 11:01:22 +01003945 dev_priv->next_seqno = 1;
3946
Ben Widawsky254f9652012-06-04 14:42:42 -07003947 /*
3948 * XXX: There was some w/a described somewhere suggesting loading
3949 * contexts before PPGTT.
3950 */
3951 i915_gem_context_init(dev);
Daniel Vettere21af882012-02-09 20:53:27 +01003952 i915_gem_init_ppgtt(dev);
3953
Chris Wilson68f95ba2010-05-27 13:18:22 +01003954 return 0;
3955
Chris Wilson549f7362010-10-19 11:19:32 +01003956cleanup_bsd_ring:
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003957 intel_cleanup_ring_buffer(&dev_priv->ring[VCS]);
Chris Wilson68f95ba2010-05-27 13:18:22 +01003958cleanup_render_ring:
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003959 intel_cleanup_ring_buffer(&dev_priv->ring[RCS]);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08003960 return ret;
3961}
3962
Chris Wilson1070a422012-04-24 15:47:41 +01003963static bool
3964intel_enable_ppgtt(struct drm_device *dev)
3965{
3966 if (i915_enable_ppgtt >= 0)
3967 return i915_enable_ppgtt;
3968
3969#ifdef CONFIG_INTEL_IOMMU
3970 /* Disable ppgtt on SNB if VT-d is on. */
3971 if (INTEL_INFO(dev)->gen == 6 && intel_iommu_gfx_mapped)
3972 return false;
3973#endif
3974
3975 return true;
3976}
3977
3978int i915_gem_init(struct drm_device *dev)
3979{
3980 struct drm_i915_private *dev_priv = dev->dev_private;
3981 unsigned long gtt_size, mappable_size;
3982 int ret;
3983
3984 gtt_size = dev_priv->mm.gtt->gtt_total_entries << PAGE_SHIFT;
3985 mappable_size = dev_priv->mm.gtt->gtt_mappable_entries << PAGE_SHIFT;
3986
3987 mutex_lock(&dev->struct_mutex);
3988 if (intel_enable_ppgtt(dev) && HAS_ALIASING_PPGTT(dev)) {
3989 /* PPGTT pdes are stolen from global gtt ptes, so shrink the
3990 * aperture accordingly when using aliasing ppgtt. */
3991 gtt_size -= I915_PPGTT_PD_ENTRIES*PAGE_SIZE;
3992
3993 i915_gem_init_global_gtt(dev, 0, mappable_size, gtt_size);
3994
3995 ret = i915_gem_init_aliasing_ppgtt(dev);
3996 if (ret) {
3997 mutex_unlock(&dev->struct_mutex);
3998 return ret;
3999 }
4000 } else {
4001 /* Let GEM Manage all of the aperture.
4002 *
4003 * However, leave one page at the end still bound to the scratch
4004 * page. There are a number of places where the hardware
4005 * apparently prefetches past the end of the object, and we've
4006 * seen multiple hangs with the GPU head pointer stuck in a
4007 * batchbuffer bound at the last page of the aperture. One page
4008 * should be enough to keep any prefetching inside of the
4009 * aperture.
4010 */
4011 i915_gem_init_global_gtt(dev, 0, mappable_size,
4012 gtt_size);
4013 }
4014
4015 ret = i915_gem_init_hw(dev);
4016 mutex_unlock(&dev->struct_mutex);
4017 if (ret) {
4018 i915_gem_cleanup_aliasing_ppgtt(dev);
4019 return ret;
4020 }
4021
Daniel Vetter53ca26c2012-04-26 23:28:03 +02004022 /* Allow hardware batchbuffers unless told otherwise, but not for KMS. */
4023 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4024 dev_priv->dri1.allow_batchbuffer = 1;
Chris Wilson1070a422012-04-24 15:47:41 +01004025 return 0;
4026}
4027
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004028void
4029i915_gem_cleanup_ringbuffer(struct drm_device *dev)
4030{
4031 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonb4519512012-05-11 14:29:30 +01004032 struct intel_ring_buffer *ring;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00004033 int i;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004034
Chris Wilsonb4519512012-05-11 14:29:30 +01004035 for_each_ring(ring, dev_priv, i)
4036 intel_cleanup_ring_buffer(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004037}
4038
4039int
Eric Anholt673a3942008-07-30 12:06:12 -07004040i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
4041 struct drm_file *file_priv)
4042{
4043 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonb4519512012-05-11 14:29:30 +01004044 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004045
Jesse Barnes79e53942008-11-07 14:24:08 -08004046 if (drm_core_check_feature(dev, DRIVER_MODESET))
4047 return 0;
4048
Ben Gamariba1234d2009-09-14 17:48:47 -04004049 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07004050 DRM_ERROR("Reenabling wedged hardware, good luck\n");
Ben Gamariba1234d2009-09-14 17:48:47 -04004051 atomic_set(&dev_priv->mm.wedged, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004052 }
4053
Eric Anholt673a3942008-07-30 12:06:12 -07004054 mutex_lock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004055 dev_priv->mm.suspended = 0;
4056
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004057 ret = i915_gem_init_hw(dev);
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004058 if (ret != 0) {
4059 mutex_unlock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004060 return ret;
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004061 }
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004062
Chris Wilson69dc4982010-10-19 10:36:51 +01004063 BUG_ON(!list_empty(&dev_priv->mm.active_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004064 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004065 mutex_unlock(&dev->struct_mutex);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004066
Chris Wilson5f353082010-06-07 14:03:03 +01004067 ret = drm_irq_install(dev);
4068 if (ret)
4069 goto cleanup_ringbuffer;
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004070
Eric Anholt673a3942008-07-30 12:06:12 -07004071 return 0;
Chris Wilson5f353082010-06-07 14:03:03 +01004072
4073cleanup_ringbuffer:
4074 mutex_lock(&dev->struct_mutex);
4075 i915_gem_cleanup_ringbuffer(dev);
4076 dev_priv->mm.suspended = 1;
4077 mutex_unlock(&dev->struct_mutex);
4078
4079 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004080}
4081
4082int
4083i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
4084 struct drm_file *file_priv)
4085{
Jesse Barnes79e53942008-11-07 14:24:08 -08004086 if (drm_core_check_feature(dev, DRIVER_MODESET))
4087 return 0;
4088
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004089 drm_irq_uninstall(dev);
Linus Torvaldse6890f62009-09-08 17:09:24 -07004090 return i915_gem_idle(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004091}
4092
4093void
4094i915_gem_lastclose(struct drm_device *dev)
4095{
4096 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004097
Eric Anholte806b492009-01-22 09:56:58 -08004098 if (drm_core_check_feature(dev, DRIVER_MODESET))
4099 return;
4100
Keith Packard6dbe2772008-10-14 21:41:13 -07004101 ret = i915_gem_idle(dev);
4102 if (ret)
4103 DRM_ERROR("failed to idle hardware: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07004104}
4105
Chris Wilson64193402010-10-24 12:38:05 +01004106static void
4107init_ring_lists(struct intel_ring_buffer *ring)
4108{
4109 INIT_LIST_HEAD(&ring->active_list);
4110 INIT_LIST_HEAD(&ring->request_list);
Chris Wilson64193402010-10-24 12:38:05 +01004111}
4112
Eric Anholt673a3942008-07-30 12:06:12 -07004113void
4114i915_gem_load(struct drm_device *dev)
4115{
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004116 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07004117 drm_i915_private_t *dev_priv = dev->dev_private;
4118
Chris Wilson69dc4982010-10-19 10:36:51 +01004119 INIT_LIST_HEAD(&dev_priv->mm.active_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004120 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
Chris Wilson6c085a72012-08-20 11:40:46 +02004121 INIT_LIST_HEAD(&dev_priv->mm.unbound_list);
4122 INIT_LIST_HEAD(&dev_priv->mm.bound_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07004123 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00004124 for (i = 0; i < I915_NUM_RINGS; i++)
4125 init_ring_lists(&dev_priv->ring[i]);
Daniel Vetter4b9de732011-10-09 21:52:02 +02004126 for (i = 0; i < I915_MAX_NUM_FENCES; i++)
Daniel Vetter007cc8a2010-04-28 11:02:31 +02004127 INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004128 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
4129 i915_gem_retire_work_handler);
Chris Wilson30dbf0c2010-09-25 10:19:17 +01004130 init_completion(&dev_priv->error_completion);
Chris Wilson31169712009-09-14 16:50:28 +01004131
Dave Airlie94400122010-07-20 13:15:31 +10004132 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
4133 if (IS_GEN3(dev)) {
Daniel Vetter50743292012-04-26 22:02:54 +02004134 I915_WRITE(MI_ARB_STATE,
4135 _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
Dave Airlie94400122010-07-20 13:15:31 +10004136 }
4137
Chris Wilson72bfa192010-12-19 11:42:05 +00004138 dev_priv->relative_constants_mode = I915_EXEC_CONSTANTS_REL_GENERAL;
4139
Jesse Barnesde151cf2008-11-12 10:03:55 -08004140 /* Old X drivers will take 0-2 for front, back, depth buffers */
Eric Anholtb397c832010-01-26 09:43:10 -08004141 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4142 dev_priv->fence_reg_start = 3;
Jesse Barnesde151cf2008-11-12 10:03:55 -08004143
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004144 if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08004145 dev_priv->num_fence_regs = 16;
4146 else
4147 dev_priv->num_fence_regs = 8;
4148
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004149 /* Initialize fence registers to zero */
Chris Wilsonada726c2012-04-17 15:31:32 +01004150 i915_gem_reset_fences(dev);
Eric Anholt10ed13e2011-05-06 13:53:49 -07004151
Eric Anholt673a3942008-07-30 12:06:12 -07004152 i915_gem_detect_bit_6_swizzle(dev);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05004153 init_waitqueue_head(&dev_priv->pending_flip_queue);
Chris Wilson17250b72010-10-28 12:51:39 +01004154
Chris Wilsonce453d82011-02-21 14:43:56 +00004155 dev_priv->mm.interruptible = true;
4156
Chris Wilson17250b72010-10-28 12:51:39 +01004157 dev_priv->mm.inactive_shrinker.shrink = i915_gem_inactive_shrink;
4158 dev_priv->mm.inactive_shrinker.seeks = DEFAULT_SEEKS;
4159 register_shrinker(&dev_priv->mm.inactive_shrinker);
Eric Anholt673a3942008-07-30 12:06:12 -07004160}
Dave Airlie71acb5e2008-12-30 20:31:46 +10004161
4162/*
4163 * Create a physically contiguous memory object for this object
4164 * e.g. for cursor + overlay regs
4165 */
Chris Wilson995b6762010-08-20 13:23:26 +01004166static int i915_gem_init_phys_object(struct drm_device *dev,
4167 int id, int size, int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004168{
4169 drm_i915_private_t *dev_priv = dev->dev_private;
4170 struct drm_i915_gem_phys_object *phys_obj;
4171 int ret;
4172
4173 if (dev_priv->mm.phys_objs[id - 1] || !size)
4174 return 0;
4175
Eric Anholt9a298b22009-03-24 12:23:04 -07004176 phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004177 if (!phys_obj)
4178 return -ENOMEM;
4179
4180 phys_obj->id = id;
4181
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004182 phys_obj->handle = drm_pci_alloc(dev, size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004183 if (!phys_obj->handle) {
4184 ret = -ENOMEM;
4185 goto kfree_obj;
4186 }
4187#ifdef CONFIG_X86
4188 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4189#endif
4190
4191 dev_priv->mm.phys_objs[id - 1] = phys_obj;
4192
4193 return 0;
4194kfree_obj:
Eric Anholt9a298b22009-03-24 12:23:04 -07004195 kfree(phys_obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004196 return ret;
4197}
4198
Chris Wilson995b6762010-08-20 13:23:26 +01004199static void i915_gem_free_phys_object(struct drm_device *dev, int id)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004200{
4201 drm_i915_private_t *dev_priv = dev->dev_private;
4202 struct drm_i915_gem_phys_object *phys_obj;
4203
4204 if (!dev_priv->mm.phys_objs[id - 1])
4205 return;
4206
4207 phys_obj = dev_priv->mm.phys_objs[id - 1];
4208 if (phys_obj->cur_obj) {
4209 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
4210 }
4211
4212#ifdef CONFIG_X86
4213 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4214#endif
4215 drm_pci_free(dev, phys_obj->handle);
4216 kfree(phys_obj);
4217 dev_priv->mm.phys_objs[id - 1] = NULL;
4218}
4219
4220void i915_gem_free_all_phys_object(struct drm_device *dev)
4221{
4222 int i;
4223
Dave Airlie260883c2009-01-22 17:58:49 +10004224 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004225 i915_gem_free_phys_object(dev, i);
4226}
4227
4228void i915_gem_detach_phys_object(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00004229 struct drm_i915_gem_object *obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004230{
Chris Wilson05394f32010-11-08 19:18:58 +00004231 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Chris Wilsone5281cc2010-10-28 13:45:36 +01004232 char *vaddr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004233 int i;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004234 int page_count;
4235
Chris Wilson05394f32010-11-08 19:18:58 +00004236 if (!obj->phys_obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004237 return;
Chris Wilson05394f32010-11-08 19:18:58 +00004238 vaddr = obj->phys_obj->handle->vaddr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004239
Chris Wilson05394f32010-11-08 19:18:58 +00004240 page_count = obj->base.size / PAGE_SIZE;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004241 for (i = 0; i < page_count; i++) {
Hugh Dickins5949eac2011-06-27 16:18:18 -07004242 struct page *page = shmem_read_mapping_page(mapping, i);
Chris Wilsone5281cc2010-10-28 13:45:36 +01004243 if (!IS_ERR(page)) {
4244 char *dst = kmap_atomic(page);
4245 memcpy(dst, vaddr + i*PAGE_SIZE, PAGE_SIZE);
4246 kunmap_atomic(dst);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004247
Chris Wilsone5281cc2010-10-28 13:45:36 +01004248 drm_clflush_pages(&page, 1);
4249
4250 set_page_dirty(page);
4251 mark_page_accessed(page);
4252 page_cache_release(page);
4253 }
Dave Airlie71acb5e2008-12-30 20:31:46 +10004254 }
Daniel Vetter40ce6572010-11-05 18:12:18 +01004255 intel_gtt_chipset_flush();
Chris Wilsond78b47b2009-06-17 21:52:49 +01004256
Chris Wilson05394f32010-11-08 19:18:58 +00004257 obj->phys_obj->cur_obj = NULL;
4258 obj->phys_obj = NULL;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004259}
4260
4261int
4262i915_gem_attach_phys_object(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00004263 struct drm_i915_gem_object *obj,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004264 int id,
4265 int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004266{
Chris Wilson05394f32010-11-08 19:18:58 +00004267 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004268 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004269 int ret = 0;
4270 int page_count;
4271 int i;
4272
4273 if (id > I915_MAX_PHYS_OBJECT)
4274 return -EINVAL;
4275
Chris Wilson05394f32010-11-08 19:18:58 +00004276 if (obj->phys_obj) {
4277 if (obj->phys_obj->id == id)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004278 return 0;
4279 i915_gem_detach_phys_object(dev, obj);
4280 }
4281
Dave Airlie71acb5e2008-12-30 20:31:46 +10004282 /* create a new object */
4283 if (!dev_priv->mm.phys_objs[id - 1]) {
4284 ret = i915_gem_init_phys_object(dev, id,
Chris Wilson05394f32010-11-08 19:18:58 +00004285 obj->base.size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004286 if (ret) {
Chris Wilson05394f32010-11-08 19:18:58 +00004287 DRM_ERROR("failed to init phys object %d size: %zu\n",
4288 id, obj->base.size);
Chris Wilsone5281cc2010-10-28 13:45:36 +01004289 return ret;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004290 }
4291 }
4292
4293 /* bind to the object */
Chris Wilson05394f32010-11-08 19:18:58 +00004294 obj->phys_obj = dev_priv->mm.phys_objs[id - 1];
4295 obj->phys_obj->cur_obj = obj;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004296
Chris Wilson05394f32010-11-08 19:18:58 +00004297 page_count = obj->base.size / PAGE_SIZE;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004298
4299 for (i = 0; i < page_count; i++) {
Chris Wilsone5281cc2010-10-28 13:45:36 +01004300 struct page *page;
4301 char *dst, *src;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004302
Hugh Dickins5949eac2011-06-27 16:18:18 -07004303 page = shmem_read_mapping_page(mapping, i);
Chris Wilsone5281cc2010-10-28 13:45:36 +01004304 if (IS_ERR(page))
4305 return PTR_ERR(page);
4306
Chris Wilsonff75b9b2010-10-30 22:52:31 +01004307 src = kmap_atomic(page);
Chris Wilson05394f32010-11-08 19:18:58 +00004308 dst = obj->phys_obj->handle->vaddr + (i * PAGE_SIZE);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004309 memcpy(dst, src, PAGE_SIZE);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -07004310 kunmap_atomic(src);
Chris Wilsone5281cc2010-10-28 13:45:36 +01004311
4312 mark_page_accessed(page);
4313 page_cache_release(page);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004314 }
4315
4316 return 0;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004317}
4318
4319static int
Chris Wilson05394f32010-11-08 19:18:58 +00004320i915_gem_phys_pwrite(struct drm_device *dev,
4321 struct drm_i915_gem_object *obj,
Dave Airlie71acb5e2008-12-30 20:31:46 +10004322 struct drm_i915_gem_pwrite *args,
4323 struct drm_file *file_priv)
4324{
Chris Wilson05394f32010-11-08 19:18:58 +00004325 void *vaddr = obj->phys_obj->handle->vaddr + args->offset;
Chris Wilsonb47b30c2010-11-08 01:12:29 +00004326 char __user *user_data = (char __user *) (uintptr_t) args->data_ptr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004327
Chris Wilsonb47b30c2010-11-08 01:12:29 +00004328 if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) {
4329 unsigned long unwritten;
4330
4331 /* The physical object once assigned is fixed for the lifetime
4332 * of the obj, so we can safely drop the lock and continue
4333 * to access vaddr.
4334 */
4335 mutex_unlock(&dev->struct_mutex);
4336 unwritten = copy_from_user(vaddr, user_data, args->size);
4337 mutex_lock(&dev->struct_mutex);
4338 if (unwritten)
4339 return -EFAULT;
4340 }
Dave Airlie71acb5e2008-12-30 20:31:46 +10004341
Daniel Vetter40ce6572010-11-05 18:12:18 +01004342 intel_gtt_chipset_flush();
Dave Airlie71acb5e2008-12-30 20:31:46 +10004343 return 0;
4344}
Eric Anholtb9624422009-06-03 07:27:35 +00004345
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004346void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00004347{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004348 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00004349
4350 /* Clean up our request list when the client is going away, so that
4351 * later retire_requests won't dereference our soon-to-be-gone
4352 * file_priv.
4353 */
Chris Wilson1c255952010-09-26 11:03:27 +01004354 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004355 while (!list_empty(&file_priv->mm.request_list)) {
4356 struct drm_i915_gem_request *request;
4357
4358 request = list_first_entry(&file_priv->mm.request_list,
4359 struct drm_i915_gem_request,
4360 client_list);
4361 list_del(&request->client_list);
4362 request->file_priv = NULL;
4363 }
Chris Wilson1c255952010-09-26 11:03:27 +01004364 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00004365}
Chris Wilson31169712009-09-14 16:50:28 +01004366
Chris Wilson31169712009-09-14 16:50:28 +01004367static int
Ying Han1495f232011-05-24 17:12:27 -07004368i915_gem_inactive_shrink(struct shrinker *shrinker, struct shrink_control *sc)
Chris Wilson31169712009-09-14 16:50:28 +01004369{
Chris Wilson17250b72010-10-28 12:51:39 +01004370 struct drm_i915_private *dev_priv =
4371 container_of(shrinker,
4372 struct drm_i915_private,
4373 mm.inactive_shrinker);
4374 struct drm_device *dev = dev_priv->dev;
Chris Wilson6c085a72012-08-20 11:40:46 +02004375 struct drm_i915_gem_object *obj;
Ying Han1495f232011-05-24 17:12:27 -07004376 int nr_to_scan = sc->nr_to_scan;
Chris Wilson17250b72010-10-28 12:51:39 +01004377 int cnt;
4378
4379 if (!mutex_trylock(&dev->struct_mutex))
Chris Wilsonbbe2e112010-10-28 22:35:07 +01004380 return 0;
Chris Wilson31169712009-09-14 16:50:28 +01004381
Chris Wilson6c085a72012-08-20 11:40:46 +02004382 if (nr_to_scan) {
4383 nr_to_scan -= i915_gem_purge(dev_priv, nr_to_scan);
4384 if (nr_to_scan > 0)
4385 i915_gem_shrink_all(dev_priv);
Chris Wilson31169712009-09-14 16:50:28 +01004386 }
4387
Chris Wilson17250b72010-10-28 12:51:39 +01004388 cnt = 0;
Chris Wilson6c085a72012-08-20 11:40:46 +02004389 list_for_each_entry(obj, &dev_priv->mm.unbound_list, gtt_list)
Chris Wilsona5570172012-09-04 21:02:54 +01004390 if (obj->pages_pin_count == 0)
4391 cnt += obj->base.size >> PAGE_SHIFT;
Chris Wilson6c085a72012-08-20 11:40:46 +02004392 list_for_each_entry(obj, &dev_priv->mm.bound_list, gtt_list)
Chris Wilsona5570172012-09-04 21:02:54 +01004393 if (obj->pin_count == 0 && obj->pages_pin_count == 0)
Chris Wilson6c085a72012-08-20 11:40:46 +02004394 cnt += obj->base.size >> PAGE_SHIFT;
Chris Wilson31169712009-09-14 16:50:28 +01004395
Chris Wilson17250b72010-10-28 12:51:39 +01004396 mutex_unlock(&dev->struct_mutex);
Chris Wilson6c085a72012-08-20 11:40:46 +02004397 return cnt;
Chris Wilson31169712009-09-14 16:50:28 +01004398}