blob: afe4a9b0a03d2526a6869215db453c98b42da848 [file] [log] [blame]
Eric Anholt673a3942008-07-30 12:06:12 -07001/*
2 * Copyright © 2008 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 *
26 */
27
28#include "drmP.h"
29#include "drm.h"
30#include "i915_drm.h"
31#include "i915_drv.h"
Chris Wilson1c5d22f2009-08-25 11:15:50 +010032#include "i915_trace.h"
Jesse Barnes652c3932009-08-17 13:31:43 -070033#include "intel_drv.h"
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Eric Anholt673a3942008-07-30 12:06:12 -070035#include <linux/swap.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080036#include <linux/pci.h>
Zhenyu Wangf8f235e2010-08-27 11:08:57 +080037#include <linux/intel-gtt.h>
Eric Anholt673a3942008-07-30 12:06:12 -070038
Daniel Vetter0108a3e2010-08-07 11:01:21 +010039static uint32_t i915_gem_get_gtt_alignment(struct drm_gem_object *obj);
Daniel Vetterba3d8d72010-02-11 22:37:04 +010040
41static int i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj,
42 bool pipelined);
Eric Anholte47c68e2008-11-14 13:35:19 -080043static void i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj);
44static void i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj);
Eric Anholte47c68e2008-11-14 13:35:19 -080045static int i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj,
46 int write);
47static int i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
48 uint64_t offset,
49 uint64_t size);
50static void i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj);
Daniel Vetterba3d8d72010-02-11 22:37:04 +010051static int i915_gem_object_wait_rendering(struct drm_gem_object *obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -080052static int i915_gem_object_bind_to_gtt(struct drm_gem_object *obj,
53 unsigned alignment);
Jesse Barnesde151cf2008-11-12 10:03:55 -080054static void i915_gem_clear_fence_reg(struct drm_gem_object *obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +100055static int i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
56 struct drm_i915_gem_pwrite *args,
57 struct drm_file *file_priv);
Chris Wilsonbe726152010-07-23 23:18:50 +010058static void i915_gem_free_object_tail(struct drm_gem_object *obj);
Eric Anholt673a3942008-07-30 12:06:12 -070059
Chris Wilson31169712009-09-14 16:50:28 +010060static LIST_HEAD(shrink_list);
61static DEFINE_SPINLOCK(shrink_list_lock);
62
Chris Wilson7d1c4802010-08-07 21:45:03 +010063static inline bool
64i915_gem_object_is_inactive(struct drm_i915_gem_object *obj_priv)
65{
66 return obj_priv->gtt_space &&
67 !obj_priv->active &&
68 obj_priv->pin_count == 0;
69}
70
Jesse Barnes79e53942008-11-07 14:24:08 -080071int i915_gem_do_init(struct drm_device *dev, unsigned long start,
72 unsigned long end)
73{
74 drm_i915_private_t *dev_priv = dev->dev_private;
75
76 if (start >= end ||
77 (start & (PAGE_SIZE - 1)) != 0 ||
78 (end & (PAGE_SIZE - 1)) != 0) {
79 return -EINVAL;
80 }
81
82 drm_mm_init(&dev_priv->mm.gtt_space, start,
83 end - start);
84
85 dev->gtt_total = (uint32_t) (end - start);
86
87 return 0;
88}
Keith Packard6dbe2772008-10-14 21:41:13 -070089
Eric Anholt673a3942008-07-30 12:06:12 -070090int
91i915_gem_init_ioctl(struct drm_device *dev, void *data,
92 struct drm_file *file_priv)
93{
Eric Anholt673a3942008-07-30 12:06:12 -070094 struct drm_i915_gem_init *args = data;
Jesse Barnes79e53942008-11-07 14:24:08 -080095 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -070096
97 mutex_lock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080098 ret = i915_gem_do_init(dev, args->gtt_start, args->gtt_end);
Eric Anholt673a3942008-07-30 12:06:12 -070099 mutex_unlock(&dev->struct_mutex);
100
Jesse Barnes79e53942008-11-07 14:24:08 -0800101 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700102}
103
Eric Anholt5a125c32008-10-22 21:40:13 -0700104int
105i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
106 struct drm_file *file_priv)
107{
Eric Anholt5a125c32008-10-22 21:40:13 -0700108 struct drm_i915_gem_get_aperture *args = data;
Eric Anholt5a125c32008-10-22 21:40:13 -0700109
110 if (!(dev->driver->driver_features & DRIVER_GEM))
111 return -ENODEV;
112
113 args->aper_size = dev->gtt_total;
Keith Packard2678d9d2008-11-20 22:54:54 -0800114 args->aper_available_size = (args->aper_size -
115 atomic_read(&dev->pin_memory));
Eric Anholt5a125c32008-10-22 21:40:13 -0700116
117 return 0;
118}
119
Eric Anholt673a3942008-07-30 12:06:12 -0700120
121/**
122 * Creates a new mm object and returns a handle to it.
123 */
124int
125i915_gem_create_ioctl(struct drm_device *dev, void *data,
126 struct drm_file *file_priv)
127{
128 struct drm_i915_gem_create *args = data;
129 struct drm_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300130 int ret;
131 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700132
133 args->size = roundup(args->size, PAGE_SIZE);
134
135 /* Allocate the new object */
Daniel Vetterac52bc52010-04-09 19:05:06 +0000136 obj = i915_gem_alloc_object(dev, args->size);
Eric Anholt673a3942008-07-30 12:06:12 -0700137 if (obj == NULL)
138 return -ENOMEM;
139
140 ret = drm_gem_handle_create(file_priv, obj, &handle);
Chris Wilson1dfd9752010-09-06 14:44:14 +0100141 if (ret) {
142 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700143 return ret;
Chris Wilson1dfd9752010-09-06 14:44:14 +0100144 }
145
146 /* Sink the floating reference from kref_init(handlecount) */
147 drm_gem_object_handle_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700148
149 args->handle = handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700150 return 0;
151}
152
Eric Anholt40123c12009-03-09 13:42:30 -0700153static inline int
Eric Anholteb014592009-03-10 11:44:52 -0700154fast_shmem_read(struct page **pages,
155 loff_t page_base, int page_offset,
156 char __user *data,
157 int length)
158{
159 char __iomem *vaddr;
Florian Mickler2bc43b52009-04-06 22:55:41 +0200160 int unwritten;
Eric Anholteb014592009-03-10 11:44:52 -0700161
162 vaddr = kmap_atomic(pages[page_base >> PAGE_SHIFT], KM_USER0);
163 if (vaddr == NULL)
164 return -ENOMEM;
Florian Mickler2bc43b52009-04-06 22:55:41 +0200165 unwritten = __copy_to_user_inatomic(data, vaddr + page_offset, length);
Eric Anholteb014592009-03-10 11:44:52 -0700166 kunmap_atomic(vaddr, KM_USER0);
167
Florian Mickler2bc43b52009-04-06 22:55:41 +0200168 if (unwritten)
169 return -EFAULT;
170
171 return 0;
Eric Anholteb014592009-03-10 11:44:52 -0700172}
173
Eric Anholt280b7132009-03-12 16:56:27 -0700174static int i915_gem_object_needs_bit17_swizzle(struct drm_gem_object *obj)
175{
176 drm_i915_private_t *dev_priv = obj->dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +0100177 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt280b7132009-03-12 16:56:27 -0700178
179 return dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_9_10_17 &&
180 obj_priv->tiling_mode != I915_TILING_NONE;
181}
182
Chris Wilson99a03df2010-05-27 14:15:34 +0100183static inline void
Eric Anholt40123c12009-03-09 13:42:30 -0700184slow_shmem_copy(struct page *dst_page,
185 int dst_offset,
186 struct page *src_page,
187 int src_offset,
188 int length)
189{
190 char *dst_vaddr, *src_vaddr;
191
Chris Wilson99a03df2010-05-27 14:15:34 +0100192 dst_vaddr = kmap(dst_page);
193 src_vaddr = kmap(src_page);
Eric Anholt40123c12009-03-09 13:42:30 -0700194
195 memcpy(dst_vaddr + dst_offset, src_vaddr + src_offset, length);
196
Chris Wilson99a03df2010-05-27 14:15:34 +0100197 kunmap(src_page);
198 kunmap(dst_page);
Eric Anholt40123c12009-03-09 13:42:30 -0700199}
200
Chris Wilson99a03df2010-05-27 14:15:34 +0100201static inline void
Eric Anholt280b7132009-03-12 16:56:27 -0700202slow_shmem_bit17_copy(struct page *gpu_page,
203 int gpu_offset,
204 struct page *cpu_page,
205 int cpu_offset,
206 int length,
207 int is_read)
208{
209 char *gpu_vaddr, *cpu_vaddr;
210
211 /* Use the unswizzled path if this page isn't affected. */
212 if ((page_to_phys(gpu_page) & (1 << 17)) == 0) {
213 if (is_read)
214 return slow_shmem_copy(cpu_page, cpu_offset,
215 gpu_page, gpu_offset, length);
216 else
217 return slow_shmem_copy(gpu_page, gpu_offset,
218 cpu_page, cpu_offset, length);
219 }
220
Chris Wilson99a03df2010-05-27 14:15:34 +0100221 gpu_vaddr = kmap(gpu_page);
222 cpu_vaddr = kmap(cpu_page);
Eric Anholt280b7132009-03-12 16:56:27 -0700223
224 /* Copy the data, XORing A6 with A17 (1). The user already knows he's
225 * XORing with the other bits (A9 for Y, A9 and A10 for X)
226 */
227 while (length > 0) {
228 int cacheline_end = ALIGN(gpu_offset + 1, 64);
229 int this_length = min(cacheline_end - gpu_offset, length);
230 int swizzled_gpu_offset = gpu_offset ^ 64;
231
232 if (is_read) {
233 memcpy(cpu_vaddr + cpu_offset,
234 gpu_vaddr + swizzled_gpu_offset,
235 this_length);
236 } else {
237 memcpy(gpu_vaddr + swizzled_gpu_offset,
238 cpu_vaddr + cpu_offset,
239 this_length);
240 }
241 cpu_offset += this_length;
242 gpu_offset += this_length;
243 length -= this_length;
244 }
245
Chris Wilson99a03df2010-05-27 14:15:34 +0100246 kunmap(cpu_page);
247 kunmap(gpu_page);
Eric Anholt280b7132009-03-12 16:56:27 -0700248}
249
Eric Anholt673a3942008-07-30 12:06:12 -0700250/**
Eric Anholteb014592009-03-10 11:44:52 -0700251 * This is the fast shmem pread path, which attempts to copy_from_user directly
252 * from the backing pages of the object to the user's address space. On a
253 * fault, it fails so we can fall back to i915_gem_shmem_pwrite_slow().
254 */
255static int
256i915_gem_shmem_pread_fast(struct drm_device *dev, struct drm_gem_object *obj,
257 struct drm_i915_gem_pread *args,
258 struct drm_file *file_priv)
259{
Daniel Vetter23010e42010-03-08 13:35:02 +0100260 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700261 ssize_t remain;
262 loff_t offset, page_base;
263 char __user *user_data;
264 int page_offset, page_length;
265 int ret;
266
267 user_data = (char __user *) (uintptr_t) args->data_ptr;
268 remain = args->size;
269
270 mutex_lock(&dev->struct_mutex);
271
Chris Wilson4bdadb92010-01-27 13:36:32 +0000272 ret = i915_gem_object_get_pages(obj, 0);
Eric Anholteb014592009-03-10 11:44:52 -0700273 if (ret != 0)
274 goto fail_unlock;
275
276 ret = i915_gem_object_set_cpu_read_domain_range(obj, args->offset,
277 args->size);
278 if (ret != 0)
279 goto fail_put_pages;
280
Daniel Vetter23010e42010-03-08 13:35:02 +0100281 obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700282 offset = args->offset;
283
284 while (remain > 0) {
285 /* Operation in this page
286 *
287 * page_base = page offset within aperture
288 * page_offset = offset within page
289 * page_length = bytes to copy for this page
290 */
291 page_base = (offset & ~(PAGE_SIZE-1));
292 page_offset = offset & (PAGE_SIZE-1);
293 page_length = remain;
294 if ((page_offset + remain) > PAGE_SIZE)
295 page_length = PAGE_SIZE - page_offset;
296
297 ret = fast_shmem_read(obj_priv->pages,
298 page_base, page_offset,
299 user_data, page_length);
300 if (ret)
301 goto fail_put_pages;
302
303 remain -= page_length;
304 user_data += page_length;
305 offset += page_length;
306 }
307
308fail_put_pages:
309 i915_gem_object_put_pages(obj);
310fail_unlock:
311 mutex_unlock(&dev->struct_mutex);
312
313 return ret;
314}
315
Chris Wilson07f73f62009-09-14 16:50:30 +0100316static int
317i915_gem_object_get_pages_or_evict(struct drm_gem_object *obj)
318{
319 int ret;
320
Chris Wilson4bdadb92010-01-27 13:36:32 +0000321 ret = i915_gem_object_get_pages(obj, __GFP_NORETRY | __GFP_NOWARN);
Chris Wilson07f73f62009-09-14 16:50:30 +0100322
323 /* If we've insufficient memory to map in the pages, attempt
324 * to make some space by throwing out some old buffers.
325 */
326 if (ret == -ENOMEM) {
327 struct drm_device *dev = obj->dev;
Chris Wilson07f73f62009-09-14 16:50:30 +0100328
Daniel Vetter0108a3e2010-08-07 11:01:21 +0100329 ret = i915_gem_evict_something(dev, obj->size,
330 i915_gem_get_gtt_alignment(obj));
Chris Wilson07f73f62009-09-14 16:50:30 +0100331 if (ret)
332 return ret;
333
Chris Wilson4bdadb92010-01-27 13:36:32 +0000334 ret = i915_gem_object_get_pages(obj, 0);
Chris Wilson07f73f62009-09-14 16:50:30 +0100335 }
336
337 return ret;
338}
339
Eric Anholteb014592009-03-10 11:44:52 -0700340/**
341 * This is the fallback shmem pread path, which allocates temporary storage
342 * in kernel space to copy_to_user into outside of the struct_mutex, so we
343 * can copy out of the object's backing pages while holding the struct mutex
344 * and not take page faults.
345 */
346static int
347i915_gem_shmem_pread_slow(struct drm_device *dev, struct drm_gem_object *obj,
348 struct drm_i915_gem_pread *args,
349 struct drm_file *file_priv)
350{
Daniel Vetter23010e42010-03-08 13:35:02 +0100351 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700352 struct mm_struct *mm = current->mm;
353 struct page **user_pages;
354 ssize_t remain;
355 loff_t offset, pinned_pages, i;
356 loff_t first_data_page, last_data_page, num_pages;
357 int shmem_page_index, shmem_page_offset;
358 int data_page_index, data_page_offset;
359 int page_length;
360 int ret;
361 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700362 int do_bit17_swizzling;
Eric Anholteb014592009-03-10 11:44:52 -0700363
364 remain = args->size;
365
366 /* Pin the user pages containing the data. We can't fault while
367 * holding the struct mutex, yet we want to hold it while
368 * dereferencing the user data.
369 */
370 first_data_page = data_ptr / PAGE_SIZE;
371 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
372 num_pages = last_data_page - first_data_page + 1;
373
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700374 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
Eric Anholteb014592009-03-10 11:44:52 -0700375 if (user_pages == NULL)
376 return -ENOMEM;
377
378 down_read(&mm->mmap_sem);
379 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
Eric Anholte5e9ecd2009-04-07 16:01:22 -0700380 num_pages, 1, 0, user_pages, NULL);
Eric Anholteb014592009-03-10 11:44:52 -0700381 up_read(&mm->mmap_sem);
382 if (pinned_pages < num_pages) {
383 ret = -EFAULT;
384 goto fail_put_user_pages;
385 }
386
Eric Anholt280b7132009-03-12 16:56:27 -0700387 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
388
Eric Anholteb014592009-03-10 11:44:52 -0700389 mutex_lock(&dev->struct_mutex);
390
Chris Wilson07f73f62009-09-14 16:50:30 +0100391 ret = i915_gem_object_get_pages_or_evict(obj);
392 if (ret)
Eric Anholteb014592009-03-10 11:44:52 -0700393 goto fail_unlock;
394
395 ret = i915_gem_object_set_cpu_read_domain_range(obj, args->offset,
396 args->size);
397 if (ret != 0)
398 goto fail_put_pages;
399
Daniel Vetter23010e42010-03-08 13:35:02 +0100400 obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700401 offset = args->offset;
402
403 while (remain > 0) {
404 /* Operation in this page
405 *
406 * shmem_page_index = page number within shmem file
407 * shmem_page_offset = offset within page in shmem file
408 * data_page_index = page number in get_user_pages return
409 * data_page_offset = offset with data_page_index page.
410 * page_length = bytes to copy for this page
411 */
412 shmem_page_index = offset / PAGE_SIZE;
413 shmem_page_offset = offset & ~PAGE_MASK;
414 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
415 data_page_offset = data_ptr & ~PAGE_MASK;
416
417 page_length = remain;
418 if ((shmem_page_offset + page_length) > PAGE_SIZE)
419 page_length = PAGE_SIZE - shmem_page_offset;
420 if ((data_page_offset + page_length) > PAGE_SIZE)
421 page_length = PAGE_SIZE - data_page_offset;
422
Eric Anholt280b7132009-03-12 16:56:27 -0700423 if (do_bit17_swizzling) {
Chris Wilson99a03df2010-05-27 14:15:34 +0100424 slow_shmem_bit17_copy(obj_priv->pages[shmem_page_index],
Eric Anholt280b7132009-03-12 16:56:27 -0700425 shmem_page_offset,
Chris Wilson99a03df2010-05-27 14:15:34 +0100426 user_pages[data_page_index],
427 data_page_offset,
428 page_length,
429 1);
430 } else {
431 slow_shmem_copy(user_pages[data_page_index],
432 data_page_offset,
433 obj_priv->pages[shmem_page_index],
434 shmem_page_offset,
435 page_length);
Eric Anholt280b7132009-03-12 16:56:27 -0700436 }
Eric Anholteb014592009-03-10 11:44:52 -0700437
438 remain -= page_length;
439 data_ptr += page_length;
440 offset += page_length;
441 }
442
443fail_put_pages:
444 i915_gem_object_put_pages(obj);
445fail_unlock:
446 mutex_unlock(&dev->struct_mutex);
447fail_put_user_pages:
448 for (i = 0; i < pinned_pages; i++) {
449 SetPageDirty(user_pages[i]);
450 page_cache_release(user_pages[i]);
451 }
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700452 drm_free_large(user_pages);
Eric Anholteb014592009-03-10 11:44:52 -0700453
454 return ret;
455}
456
Eric Anholt673a3942008-07-30 12:06:12 -0700457/**
458 * Reads data from the object referenced by handle.
459 *
460 * On error, the contents of *data are undefined.
461 */
462int
463i915_gem_pread_ioctl(struct drm_device *dev, void *data,
464 struct drm_file *file_priv)
465{
466 struct drm_i915_gem_pread *args = data;
467 struct drm_gem_object *obj;
468 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -0700469 int ret;
470
471 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
472 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100473 return -ENOENT;
Daniel Vetter23010e42010-03-08 13:35:02 +0100474 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700475
476 /* Bounds check source.
477 *
478 * XXX: This could use review for overflow issues...
479 */
480 if (args->offset > obj->size || args->size > obj->size ||
481 args->offset + args->size > obj->size) {
Luca Barbieribc9025b2010-02-09 05:49:12 +0000482 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700483 return -EINVAL;
484 }
485
Eric Anholt280b7132009-03-12 16:56:27 -0700486 if (i915_gem_object_needs_bit17_swizzle(obj)) {
Eric Anholteb014592009-03-10 11:44:52 -0700487 ret = i915_gem_shmem_pread_slow(dev, obj, args, file_priv);
Eric Anholt280b7132009-03-12 16:56:27 -0700488 } else {
489 ret = i915_gem_shmem_pread_fast(dev, obj, args, file_priv);
490 if (ret != 0)
491 ret = i915_gem_shmem_pread_slow(dev, obj, args,
492 file_priv);
493 }
Eric Anholt673a3942008-07-30 12:06:12 -0700494
Luca Barbieribc9025b2010-02-09 05:49:12 +0000495 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700496
Eric Anholteb014592009-03-10 11:44:52 -0700497 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700498}
499
Keith Packard0839ccb2008-10-30 19:38:48 -0700500/* This is the fast write path which cannot handle
501 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700502 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700503
Keith Packard0839ccb2008-10-30 19:38:48 -0700504static inline int
505fast_user_write(struct io_mapping *mapping,
506 loff_t page_base, int page_offset,
507 char __user *user_data,
508 int length)
509{
510 char *vaddr_atomic;
511 unsigned long unwritten;
512
Chris Wilsonfca3ec02010-08-04 14:34:24 +0100513 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base, KM_USER0);
Keith Packard0839ccb2008-10-30 19:38:48 -0700514 unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
515 user_data, length);
Chris Wilsonfca3ec02010-08-04 14:34:24 +0100516 io_mapping_unmap_atomic(vaddr_atomic, KM_USER0);
Keith Packard0839ccb2008-10-30 19:38:48 -0700517 if (unwritten)
518 return -EFAULT;
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700519 return 0;
Keith Packard0839ccb2008-10-30 19:38:48 -0700520}
521
522/* Here's the write path which can sleep for
523 * page faults
524 */
525
Chris Wilsonab34c222010-05-27 14:15:35 +0100526static inline void
Eric Anholt3de09aa2009-03-09 09:42:23 -0700527slow_kernel_write(struct io_mapping *mapping,
528 loff_t gtt_base, int gtt_offset,
529 struct page *user_page, int user_offset,
530 int length)
Keith Packard0839ccb2008-10-30 19:38:48 -0700531{
Chris Wilsonab34c222010-05-27 14:15:35 +0100532 char __iomem *dst_vaddr;
533 char *src_vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -0700534
Chris Wilsonab34c222010-05-27 14:15:35 +0100535 dst_vaddr = io_mapping_map_wc(mapping, gtt_base);
536 src_vaddr = kmap(user_page);
537
538 memcpy_toio(dst_vaddr + gtt_offset,
539 src_vaddr + user_offset,
540 length);
541
542 kunmap(user_page);
543 io_mapping_unmap(dst_vaddr);
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700544}
545
Eric Anholt40123c12009-03-09 13:42:30 -0700546static inline int
547fast_shmem_write(struct page **pages,
548 loff_t page_base, int page_offset,
549 char __user *data,
550 int length)
551{
552 char __iomem *vaddr;
Dave Airlied0088772009-03-28 20:29:48 -0400553 unsigned long unwritten;
Eric Anholt40123c12009-03-09 13:42:30 -0700554
555 vaddr = kmap_atomic(pages[page_base >> PAGE_SHIFT], KM_USER0);
556 if (vaddr == NULL)
557 return -ENOMEM;
Dave Airlied0088772009-03-28 20:29:48 -0400558 unwritten = __copy_from_user_inatomic(vaddr + page_offset, data, length);
Eric Anholt40123c12009-03-09 13:42:30 -0700559 kunmap_atomic(vaddr, KM_USER0);
560
Dave Airlied0088772009-03-28 20:29:48 -0400561 if (unwritten)
562 return -EFAULT;
Eric Anholt40123c12009-03-09 13:42:30 -0700563 return 0;
564}
565
Eric Anholt3de09aa2009-03-09 09:42:23 -0700566/**
567 * This is the fast pwrite path, where we copy the data directly from the
568 * user into the GTT, uncached.
569 */
Eric Anholt673a3942008-07-30 12:06:12 -0700570static int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700571i915_gem_gtt_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
572 struct drm_i915_gem_pwrite *args,
573 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700574{
Daniel Vetter23010e42010-03-08 13:35:02 +0100575 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Keith Packard0839ccb2008-10-30 19:38:48 -0700576 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700577 ssize_t remain;
Keith Packard0839ccb2008-10-30 19:38:48 -0700578 loff_t offset, page_base;
Eric Anholt673a3942008-07-30 12:06:12 -0700579 char __user *user_data;
Keith Packard0839ccb2008-10-30 19:38:48 -0700580 int page_offset, page_length;
581 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700582
583 user_data = (char __user *) (uintptr_t) args->data_ptr;
584 remain = args->size;
585 if (!access_ok(VERIFY_READ, user_data, remain))
586 return -EFAULT;
587
588
589 mutex_lock(&dev->struct_mutex);
590 ret = i915_gem_object_pin(obj, 0);
591 if (ret) {
592 mutex_unlock(&dev->struct_mutex);
593 return ret;
594 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800595 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
Eric Anholt673a3942008-07-30 12:06:12 -0700596 if (ret)
597 goto fail;
598
Daniel Vetter23010e42010-03-08 13:35:02 +0100599 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700600 offset = obj_priv->gtt_offset + args->offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700601
602 while (remain > 0) {
603 /* Operation in this page
604 *
Keith Packard0839ccb2008-10-30 19:38:48 -0700605 * page_base = page offset within aperture
606 * page_offset = offset within page
607 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -0700608 */
Keith Packard0839ccb2008-10-30 19:38:48 -0700609 page_base = (offset & ~(PAGE_SIZE-1));
610 page_offset = offset & (PAGE_SIZE-1);
611 page_length = remain;
612 if ((page_offset + remain) > PAGE_SIZE)
613 page_length = PAGE_SIZE - page_offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700614
Keith Packard0839ccb2008-10-30 19:38:48 -0700615 ret = fast_user_write (dev_priv->mm.gtt_mapping, page_base,
616 page_offset, user_data, page_length);
Eric Anholt673a3942008-07-30 12:06:12 -0700617
Keith Packard0839ccb2008-10-30 19:38:48 -0700618 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -0700619 * source page isn't available. Return the error and we'll
620 * retry in the slow path.
Keith Packard0839ccb2008-10-30 19:38:48 -0700621 */
Eric Anholt3de09aa2009-03-09 09:42:23 -0700622 if (ret)
623 goto fail;
Eric Anholt673a3942008-07-30 12:06:12 -0700624
Keith Packard0839ccb2008-10-30 19:38:48 -0700625 remain -= page_length;
626 user_data += page_length;
627 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700628 }
Eric Anholt673a3942008-07-30 12:06:12 -0700629
630fail:
631 i915_gem_object_unpin(obj);
632 mutex_unlock(&dev->struct_mutex);
633
634 return ret;
635}
636
Eric Anholt3de09aa2009-03-09 09:42:23 -0700637/**
638 * This is the fallback GTT pwrite path, which uses get_user_pages to pin
639 * the memory and maps it using kmap_atomic for copying.
640 *
641 * This code resulted in x11perf -rgb10text consuming about 10% more CPU
642 * than using i915_gem_gtt_pwrite_fast on a G45 (32-bit).
643 */
Eric Anholt3043c602008-10-02 12:24:47 -0700644static int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700645i915_gem_gtt_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
646 struct drm_i915_gem_pwrite *args,
647 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700648{
Daniel Vetter23010e42010-03-08 13:35:02 +0100649 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700650 drm_i915_private_t *dev_priv = dev->dev_private;
651 ssize_t remain;
652 loff_t gtt_page_base, offset;
653 loff_t first_data_page, last_data_page, num_pages;
654 loff_t pinned_pages, i;
655 struct page **user_pages;
656 struct mm_struct *mm = current->mm;
657 int gtt_page_offset, data_page_offset, data_page_index, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700658 int ret;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700659 uint64_t data_ptr = args->data_ptr;
660
661 remain = args->size;
662
663 /* Pin the user pages containing the data. We can't fault while
664 * holding the struct mutex, and all of the pwrite implementations
665 * want to hold it while dereferencing the user data.
666 */
667 first_data_page = data_ptr / PAGE_SIZE;
668 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
669 num_pages = last_data_page - first_data_page + 1;
670
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700671 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
Eric Anholt3de09aa2009-03-09 09:42:23 -0700672 if (user_pages == NULL)
673 return -ENOMEM;
674
675 down_read(&mm->mmap_sem);
676 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
677 num_pages, 0, 0, user_pages, NULL);
678 up_read(&mm->mmap_sem);
679 if (pinned_pages < num_pages) {
680 ret = -EFAULT;
681 goto out_unpin_pages;
682 }
683
684 mutex_lock(&dev->struct_mutex);
685 ret = i915_gem_object_pin(obj, 0);
686 if (ret)
687 goto out_unlock;
688
689 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
690 if (ret)
691 goto out_unpin_object;
692
Daniel Vetter23010e42010-03-08 13:35:02 +0100693 obj_priv = to_intel_bo(obj);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700694 offset = obj_priv->gtt_offset + args->offset;
695
696 while (remain > 0) {
697 /* Operation in this page
698 *
699 * gtt_page_base = page offset within aperture
700 * gtt_page_offset = offset within page in aperture
701 * data_page_index = page number in get_user_pages return
702 * data_page_offset = offset with data_page_index page.
703 * page_length = bytes to copy for this page
704 */
705 gtt_page_base = offset & PAGE_MASK;
706 gtt_page_offset = offset & ~PAGE_MASK;
707 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
708 data_page_offset = data_ptr & ~PAGE_MASK;
709
710 page_length = remain;
711 if ((gtt_page_offset + page_length) > PAGE_SIZE)
712 page_length = PAGE_SIZE - gtt_page_offset;
713 if ((data_page_offset + page_length) > PAGE_SIZE)
714 page_length = PAGE_SIZE - data_page_offset;
715
Chris Wilsonab34c222010-05-27 14:15:35 +0100716 slow_kernel_write(dev_priv->mm.gtt_mapping,
717 gtt_page_base, gtt_page_offset,
718 user_pages[data_page_index],
719 data_page_offset,
720 page_length);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700721
722 remain -= page_length;
723 offset += page_length;
724 data_ptr += page_length;
725 }
726
727out_unpin_object:
728 i915_gem_object_unpin(obj);
729out_unlock:
730 mutex_unlock(&dev->struct_mutex);
731out_unpin_pages:
732 for (i = 0; i < pinned_pages; i++)
733 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700734 drm_free_large(user_pages);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700735
736 return ret;
737}
738
Eric Anholt40123c12009-03-09 13:42:30 -0700739/**
740 * This is the fast shmem pwrite path, which attempts to directly
741 * copy_from_user into the kmapped pages backing the object.
742 */
Eric Anholt673a3942008-07-30 12:06:12 -0700743static int
Eric Anholt40123c12009-03-09 13:42:30 -0700744i915_gem_shmem_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
745 struct drm_i915_gem_pwrite *args,
746 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700747{
Daniel Vetter23010e42010-03-08 13:35:02 +0100748 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700749 ssize_t remain;
750 loff_t offset, page_base;
751 char __user *user_data;
752 int page_offset, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700753 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -0700754
755 user_data = (char __user *) (uintptr_t) args->data_ptr;
756 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700757
758 mutex_lock(&dev->struct_mutex);
759
Chris Wilson4bdadb92010-01-27 13:36:32 +0000760 ret = i915_gem_object_get_pages(obj, 0);
Eric Anholt40123c12009-03-09 13:42:30 -0700761 if (ret != 0)
762 goto fail_unlock;
763
Eric Anholte47c68e2008-11-14 13:35:19 -0800764 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Eric Anholt40123c12009-03-09 13:42:30 -0700765 if (ret != 0)
766 goto fail_put_pages;
Eric Anholt673a3942008-07-30 12:06:12 -0700767
Daniel Vetter23010e42010-03-08 13:35:02 +0100768 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700769 offset = args->offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700770 obj_priv->dirty = 1;
Eric Anholt673a3942008-07-30 12:06:12 -0700771
Eric Anholt40123c12009-03-09 13:42:30 -0700772 while (remain > 0) {
773 /* Operation in this page
774 *
775 * page_base = page offset within aperture
776 * page_offset = offset within page
777 * page_length = bytes to copy for this page
778 */
779 page_base = (offset & ~(PAGE_SIZE-1));
780 page_offset = offset & (PAGE_SIZE-1);
781 page_length = remain;
782 if ((page_offset + remain) > PAGE_SIZE)
783 page_length = PAGE_SIZE - page_offset;
784
785 ret = fast_shmem_write(obj_priv->pages,
786 page_base, page_offset,
787 user_data, page_length);
788 if (ret)
789 goto fail_put_pages;
790
791 remain -= page_length;
792 user_data += page_length;
793 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700794 }
795
Eric Anholt40123c12009-03-09 13:42:30 -0700796fail_put_pages:
797 i915_gem_object_put_pages(obj);
798fail_unlock:
Eric Anholt673a3942008-07-30 12:06:12 -0700799 mutex_unlock(&dev->struct_mutex);
800
Eric Anholt40123c12009-03-09 13:42:30 -0700801 return ret;
802}
803
804/**
805 * This is the fallback shmem pwrite path, which uses get_user_pages to pin
806 * the memory and maps it using kmap_atomic for copying.
807 *
808 * This avoids taking mmap_sem for faulting on the user's address while the
809 * struct_mutex is held.
810 */
811static int
812i915_gem_shmem_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
813 struct drm_i915_gem_pwrite *args,
814 struct drm_file *file_priv)
815{
Daniel Vetter23010e42010-03-08 13:35:02 +0100816 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700817 struct mm_struct *mm = current->mm;
818 struct page **user_pages;
819 ssize_t remain;
820 loff_t offset, pinned_pages, i;
821 loff_t first_data_page, last_data_page, num_pages;
822 int shmem_page_index, shmem_page_offset;
823 int data_page_index, data_page_offset;
824 int page_length;
825 int ret;
826 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700827 int do_bit17_swizzling;
Eric Anholt40123c12009-03-09 13:42:30 -0700828
829 remain = args->size;
830
831 /* Pin the user pages containing the data. We can't fault while
832 * holding the struct mutex, and all of the pwrite implementations
833 * want to hold it while dereferencing the user data.
834 */
835 first_data_page = data_ptr / PAGE_SIZE;
836 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
837 num_pages = last_data_page - first_data_page + 1;
838
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700839 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
Eric Anholt40123c12009-03-09 13:42:30 -0700840 if (user_pages == NULL)
841 return -ENOMEM;
842
843 down_read(&mm->mmap_sem);
844 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
845 num_pages, 0, 0, user_pages, NULL);
846 up_read(&mm->mmap_sem);
847 if (pinned_pages < num_pages) {
848 ret = -EFAULT;
849 goto fail_put_user_pages;
850 }
851
Eric Anholt280b7132009-03-12 16:56:27 -0700852 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
853
Eric Anholt40123c12009-03-09 13:42:30 -0700854 mutex_lock(&dev->struct_mutex);
855
Chris Wilson07f73f62009-09-14 16:50:30 +0100856 ret = i915_gem_object_get_pages_or_evict(obj);
857 if (ret)
Eric Anholt40123c12009-03-09 13:42:30 -0700858 goto fail_unlock;
859
860 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
861 if (ret != 0)
862 goto fail_put_pages;
863
Daniel Vetter23010e42010-03-08 13:35:02 +0100864 obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700865 offset = args->offset;
866 obj_priv->dirty = 1;
867
868 while (remain > 0) {
869 /* Operation in this page
870 *
871 * shmem_page_index = page number within shmem file
872 * shmem_page_offset = offset within page in shmem file
873 * data_page_index = page number in get_user_pages return
874 * data_page_offset = offset with data_page_index page.
875 * page_length = bytes to copy for this page
876 */
877 shmem_page_index = offset / PAGE_SIZE;
878 shmem_page_offset = offset & ~PAGE_MASK;
879 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
880 data_page_offset = data_ptr & ~PAGE_MASK;
881
882 page_length = remain;
883 if ((shmem_page_offset + page_length) > PAGE_SIZE)
884 page_length = PAGE_SIZE - shmem_page_offset;
885 if ((data_page_offset + page_length) > PAGE_SIZE)
886 page_length = PAGE_SIZE - data_page_offset;
887
Eric Anholt280b7132009-03-12 16:56:27 -0700888 if (do_bit17_swizzling) {
Chris Wilson99a03df2010-05-27 14:15:34 +0100889 slow_shmem_bit17_copy(obj_priv->pages[shmem_page_index],
Eric Anholt280b7132009-03-12 16:56:27 -0700890 shmem_page_offset,
891 user_pages[data_page_index],
892 data_page_offset,
Chris Wilson99a03df2010-05-27 14:15:34 +0100893 page_length,
894 0);
895 } else {
896 slow_shmem_copy(obj_priv->pages[shmem_page_index],
897 shmem_page_offset,
898 user_pages[data_page_index],
899 data_page_offset,
900 page_length);
Eric Anholt280b7132009-03-12 16:56:27 -0700901 }
Eric Anholt40123c12009-03-09 13:42:30 -0700902
903 remain -= page_length;
904 data_ptr += page_length;
905 offset += page_length;
906 }
907
908fail_put_pages:
909 i915_gem_object_put_pages(obj);
910fail_unlock:
911 mutex_unlock(&dev->struct_mutex);
912fail_put_user_pages:
913 for (i = 0; i < pinned_pages; i++)
914 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700915 drm_free_large(user_pages);
Eric Anholt40123c12009-03-09 13:42:30 -0700916
917 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700918}
919
920/**
921 * Writes data to the object referenced by handle.
922 *
923 * On error, the contents of the buffer that were to be modified are undefined.
924 */
925int
926i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
927 struct drm_file *file_priv)
928{
929 struct drm_i915_gem_pwrite *args = data;
930 struct drm_gem_object *obj;
931 struct drm_i915_gem_object *obj_priv;
932 int ret = 0;
933
934 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
935 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100936 return -ENOENT;
Daniel Vetter23010e42010-03-08 13:35:02 +0100937 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700938
939 /* Bounds check destination.
940 *
941 * XXX: This could use review for overflow issues...
942 */
943 if (args->offset > obj->size || args->size > obj->size ||
944 args->offset + args->size > obj->size) {
Luca Barbieribc9025b2010-02-09 05:49:12 +0000945 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700946 return -EINVAL;
947 }
948
949 /* We can only do the GTT pwrite on untiled buffers, as otherwise
950 * it would end up going through the fenced access, and we'll get
951 * different detiling behavior between reading and writing.
952 * pread/pwrite currently are reading and writing from the CPU
953 * perspective, requiring manual detiling by the client.
954 */
Dave Airlie71acb5e2008-12-30 20:31:46 +1000955 if (obj_priv->phys_obj)
956 ret = i915_gem_phys_pwrite(dev, obj, args, file_priv);
957 else if (obj_priv->tiling_mode == I915_TILING_NONE &&
Chris Wilson9b8c4a02010-05-27 14:21:01 +0100958 dev->gtt_total != 0 &&
959 obj->write_domain != I915_GEM_DOMAIN_CPU) {
Eric Anholt3de09aa2009-03-09 09:42:23 -0700960 ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file_priv);
961 if (ret == -EFAULT) {
962 ret = i915_gem_gtt_pwrite_slow(dev, obj, args,
963 file_priv);
964 }
Eric Anholt280b7132009-03-12 16:56:27 -0700965 } else if (i915_gem_object_needs_bit17_swizzle(obj)) {
966 ret = i915_gem_shmem_pwrite_slow(dev, obj, args, file_priv);
Eric Anholt40123c12009-03-09 13:42:30 -0700967 } else {
968 ret = i915_gem_shmem_pwrite_fast(dev, obj, args, file_priv);
969 if (ret == -EFAULT) {
970 ret = i915_gem_shmem_pwrite_slow(dev, obj, args,
971 file_priv);
972 }
973 }
Eric Anholt673a3942008-07-30 12:06:12 -0700974
975#if WATCH_PWRITE
976 if (ret)
977 DRM_INFO("pwrite failed %d\n", ret);
978#endif
979
Luca Barbieribc9025b2010-02-09 05:49:12 +0000980 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700981
982 return ret;
983}
984
985/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800986 * Called when user space prepares to use an object with the CPU, either
987 * through the mmap ioctl's mapping or a GTT mapping.
Eric Anholt673a3942008-07-30 12:06:12 -0700988 */
989int
990i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
991 struct drm_file *file_priv)
992{
Eric Anholta09ba7f2009-08-29 12:49:51 -0700993 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700994 struct drm_i915_gem_set_domain *args = data;
995 struct drm_gem_object *obj;
Jesse Barnes652c3932009-08-17 13:31:43 -0700996 struct drm_i915_gem_object *obj_priv;
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800997 uint32_t read_domains = args->read_domains;
998 uint32_t write_domain = args->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -0700999 int ret;
1000
1001 if (!(dev->driver->driver_features & DRIVER_GEM))
1002 return -ENODEV;
1003
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001004 /* Only handle setting domains to types used by the CPU. */
Chris Wilson21d509e2009-06-06 09:46:02 +01001005 if (write_domain & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001006 return -EINVAL;
1007
Chris Wilson21d509e2009-06-06 09:46:02 +01001008 if (read_domains & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001009 return -EINVAL;
1010
1011 /* Having something in the write domain implies it's in the read
1012 * domain, and only that read domain. Enforce that in the request.
1013 */
1014 if (write_domain != 0 && read_domains != write_domain)
1015 return -EINVAL;
1016
Eric Anholt673a3942008-07-30 12:06:12 -07001017 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1018 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001019 return -ENOENT;
Daniel Vetter23010e42010-03-08 13:35:02 +01001020 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001021
1022 mutex_lock(&dev->struct_mutex);
Jesse Barnes652c3932009-08-17 13:31:43 -07001023
1024 intel_mark_busy(dev, obj);
1025
Eric Anholt673a3942008-07-30 12:06:12 -07001026#if WATCH_BUF
Krzysztof Halasacfd43c02009-06-20 00:31:28 +02001027 DRM_INFO("set_domain_ioctl %p(%zd), %08x %08x\n",
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001028 obj, obj->size, read_domains, write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07001029#endif
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001030 if (read_domains & I915_GEM_DOMAIN_GTT) {
1031 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
Eric Anholt02354392008-11-26 13:58:13 -08001032
Eric Anholta09ba7f2009-08-29 12:49:51 -07001033 /* Update the LRU on the fence for the CPU access that's
1034 * about to occur.
1035 */
1036 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001037 struct drm_i915_fence_reg *reg =
1038 &dev_priv->fence_regs[obj_priv->fence_reg];
1039 list_move_tail(&reg->lru_list,
Eric Anholta09ba7f2009-08-29 12:49:51 -07001040 &dev_priv->mm.fence_list);
1041 }
1042
Eric Anholt02354392008-11-26 13:58:13 -08001043 /* Silently promote "you're not bound, there was nothing to do"
1044 * to success, since the client was just asking us to
1045 * make sure everything was done.
1046 */
1047 if (ret == -EINVAL)
1048 ret = 0;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001049 } else {
Eric Anholte47c68e2008-11-14 13:35:19 -08001050 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001051 }
1052
Chris Wilson7d1c4802010-08-07 21:45:03 +01001053
1054 /* Maintain LRU order of "inactive" objects */
1055 if (ret == 0 && i915_gem_object_is_inactive(obj_priv))
1056 list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
1057
Eric Anholt673a3942008-07-30 12:06:12 -07001058 drm_gem_object_unreference(obj);
1059 mutex_unlock(&dev->struct_mutex);
1060 return ret;
1061}
1062
1063/**
1064 * Called when user space has done writes to this buffer
1065 */
1066int
1067i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
1068 struct drm_file *file_priv)
1069{
1070 struct drm_i915_gem_sw_finish *args = data;
1071 struct drm_gem_object *obj;
1072 struct drm_i915_gem_object *obj_priv;
1073 int ret = 0;
1074
1075 if (!(dev->driver->driver_features & DRIVER_GEM))
1076 return -ENODEV;
1077
1078 mutex_lock(&dev->struct_mutex);
1079 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1080 if (obj == NULL) {
1081 mutex_unlock(&dev->struct_mutex);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001082 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001083 }
1084
1085#if WATCH_BUF
Krzysztof Halasacfd43c02009-06-20 00:31:28 +02001086 DRM_INFO("%s: sw_finish %d (%p %zd)\n",
Eric Anholt673a3942008-07-30 12:06:12 -07001087 __func__, args->handle, obj, obj->size);
1088#endif
Daniel Vetter23010e42010-03-08 13:35:02 +01001089 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001090
1091 /* Pinned buffers may be scanout, so flush the cache */
Eric Anholte47c68e2008-11-14 13:35:19 -08001092 if (obj_priv->pin_count)
1093 i915_gem_object_flush_cpu_write_domain(obj);
1094
Eric Anholt673a3942008-07-30 12:06:12 -07001095 drm_gem_object_unreference(obj);
1096 mutex_unlock(&dev->struct_mutex);
1097 return ret;
1098}
1099
1100/**
1101 * Maps the contents of an object, returning the address it is mapped
1102 * into.
1103 *
1104 * While the mapping holds a reference on the contents of the object, it doesn't
1105 * imply a ref on the object itself.
1106 */
1107int
1108i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
1109 struct drm_file *file_priv)
1110{
1111 struct drm_i915_gem_mmap *args = data;
1112 struct drm_gem_object *obj;
1113 loff_t offset;
1114 unsigned long addr;
1115
1116 if (!(dev->driver->driver_features & DRIVER_GEM))
1117 return -ENODEV;
1118
1119 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1120 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001121 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001122
1123 offset = args->offset;
1124
1125 down_write(&current->mm->mmap_sem);
1126 addr = do_mmap(obj->filp, 0, args->size,
1127 PROT_READ | PROT_WRITE, MAP_SHARED,
1128 args->offset);
1129 up_write(&current->mm->mmap_sem);
Luca Barbieribc9025b2010-02-09 05:49:12 +00001130 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001131 if (IS_ERR((void *)addr))
1132 return addr;
1133
1134 args->addr_ptr = (uint64_t) addr;
1135
1136 return 0;
1137}
1138
Jesse Barnesde151cf2008-11-12 10:03:55 -08001139/**
1140 * i915_gem_fault - fault a page into the GTT
1141 * vma: VMA in question
1142 * vmf: fault info
1143 *
1144 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1145 * from userspace. The fault handler takes care of binding the object to
1146 * the GTT (if needed), allocating and programming a fence register (again,
1147 * only if needed based on whether the old reg is still valid or the object
1148 * is tiled) and inserting a new PTE into the faulting process.
1149 *
1150 * Note that the faulting process may involve evicting existing objects
1151 * from the GTT and/or fence registers to make room. So performance may
1152 * suffer if the GTT working set is large or there are few fence registers
1153 * left.
1154 */
1155int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1156{
1157 struct drm_gem_object *obj = vma->vm_private_data;
1158 struct drm_device *dev = obj->dev;
Chris Wilson7d1c4802010-08-07 21:45:03 +01001159 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001160 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001161 pgoff_t page_offset;
1162 unsigned long pfn;
1163 int ret = 0;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001164 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001165
1166 /* We don't use vmf->pgoff since that has the fake offset */
1167 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1168 PAGE_SHIFT;
1169
1170 /* Now bind it into the GTT if needed */
1171 mutex_lock(&dev->struct_mutex);
1172 if (!obj_priv->gtt_space) {
Chris Wilsone67b8ce2009-09-14 16:50:26 +01001173 ret = i915_gem_object_bind_to_gtt(obj, 0);
Chris Wilsonc7150892009-09-23 00:43:56 +01001174 if (ret)
1175 goto unlock;
Kristian Høgsberg07f4f3e2009-05-27 14:37:28 -04001176
Jesse Barnesde151cf2008-11-12 10:03:55 -08001177 ret = i915_gem_object_set_to_gtt_domain(obj, write);
Chris Wilsonc7150892009-09-23 00:43:56 +01001178 if (ret)
1179 goto unlock;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001180 }
1181
1182 /* Need a new fence register? */
Eric Anholta09ba7f2009-08-29 12:49:51 -07001183 if (obj_priv->tiling_mode != I915_TILING_NONE) {
Chris Wilson8c4b8c32009-06-17 22:08:52 +01001184 ret = i915_gem_object_get_fence_reg(obj);
Chris Wilsonc7150892009-09-23 00:43:56 +01001185 if (ret)
1186 goto unlock;
Eric Anholtd9ddcb92009-01-27 10:33:49 -08001187 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001188
Chris Wilson7d1c4802010-08-07 21:45:03 +01001189 if (i915_gem_object_is_inactive(obj_priv))
1190 list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
1191
Jesse Barnesde151cf2008-11-12 10:03:55 -08001192 pfn = ((dev->agp->base + obj_priv->gtt_offset) >> PAGE_SHIFT) +
1193 page_offset;
1194
1195 /* Finally, remap it using the new GTT offset */
1196 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
Chris Wilsonc7150892009-09-23 00:43:56 +01001197unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001198 mutex_unlock(&dev->struct_mutex);
1199
1200 switch (ret) {
Chris Wilsonc7150892009-09-23 00:43:56 +01001201 case 0:
1202 case -ERESTARTSYS:
1203 return VM_FAULT_NOPAGE;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001204 case -ENOMEM:
1205 case -EAGAIN:
1206 return VM_FAULT_OOM;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001207 default:
Chris Wilsonc7150892009-09-23 00:43:56 +01001208 return VM_FAULT_SIGBUS;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001209 }
1210}
1211
1212/**
1213 * i915_gem_create_mmap_offset - create a fake mmap offset for an object
1214 * @obj: obj in question
1215 *
1216 * GEM memory mapping works by handing back to userspace a fake mmap offset
1217 * it can use in a subsequent mmap(2) call. The DRM core code then looks
1218 * up the object based on the offset and sets up the various memory mapping
1219 * structures.
1220 *
1221 * This routine allocates and attaches a fake offset for @obj.
1222 */
1223static int
1224i915_gem_create_mmap_offset(struct drm_gem_object *obj)
1225{
1226 struct drm_device *dev = obj->dev;
1227 struct drm_gem_mm *mm = dev->mm_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001228 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001229 struct drm_map_list *list;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +11001230 struct drm_local_map *map;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001231 int ret = 0;
1232
1233 /* Set the object up for mmap'ing */
1234 list = &obj->map_list;
Eric Anholt9a298b22009-03-24 12:23:04 -07001235 list->map = kzalloc(sizeof(struct drm_map_list), GFP_KERNEL);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001236 if (!list->map)
1237 return -ENOMEM;
1238
1239 map = list->map;
1240 map->type = _DRM_GEM;
1241 map->size = obj->size;
1242 map->handle = obj;
1243
1244 /* Get a DRM GEM mmap offset allocated... */
1245 list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
1246 obj->size / PAGE_SIZE, 0, 0);
1247 if (!list->file_offset_node) {
1248 DRM_ERROR("failed to allocate offset for bo %d\n", obj->name);
1249 ret = -ENOMEM;
1250 goto out_free_list;
1251 }
1252
1253 list->file_offset_node = drm_mm_get_block(list->file_offset_node,
1254 obj->size / PAGE_SIZE, 0);
1255 if (!list->file_offset_node) {
1256 ret = -ENOMEM;
1257 goto out_free_list;
1258 }
1259
1260 list->hash.key = list->file_offset_node->start;
1261 if (drm_ht_insert_item(&mm->offset_hash, &list->hash)) {
1262 DRM_ERROR("failed to add to map hash\n");
Chris Wilson5618ca62009-12-02 15:15:30 +00001263 ret = -ENOMEM;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001264 goto out_free_mm;
1265 }
1266
1267 /* By now we should be all set, any drm_mmap request on the offset
1268 * below will get to our mmap & fault handler */
1269 obj_priv->mmap_offset = ((uint64_t) list->hash.key) << PAGE_SHIFT;
1270
1271 return 0;
1272
1273out_free_mm:
1274 drm_mm_put_block(list->file_offset_node);
1275out_free_list:
Eric Anholt9a298b22009-03-24 12:23:04 -07001276 kfree(list->map);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001277
1278 return ret;
1279}
1280
Chris Wilson901782b2009-07-10 08:18:50 +01001281/**
1282 * i915_gem_release_mmap - remove physical page mappings
1283 * @obj: obj in question
1284 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001285 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01001286 * relinquish ownership of the pages back to the system.
1287 *
1288 * It is vital that we remove the page mapping if we have mapped a tiled
1289 * object through the GTT and then lose the fence register due to
1290 * resource pressure. Similarly if the object has been moved out of the
1291 * aperture, than pages mapped into userspace must be revoked. Removing the
1292 * mapping will then trigger a page fault on the next user access, allowing
1293 * fixup by i915_gem_fault().
1294 */
Eric Anholtd05ca302009-07-10 13:02:26 -07001295void
Chris Wilson901782b2009-07-10 08:18:50 +01001296i915_gem_release_mmap(struct drm_gem_object *obj)
1297{
1298 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001299 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson901782b2009-07-10 08:18:50 +01001300
1301 if (dev->dev_mapping)
1302 unmap_mapping_range(dev->dev_mapping,
1303 obj_priv->mmap_offset, obj->size, 1);
1304}
1305
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001306static void
1307i915_gem_free_mmap_offset(struct drm_gem_object *obj)
1308{
1309 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001310 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001311 struct drm_gem_mm *mm = dev->mm_private;
1312 struct drm_map_list *list;
1313
1314 list = &obj->map_list;
1315 drm_ht_remove_item(&mm->offset_hash, &list->hash);
1316
1317 if (list->file_offset_node) {
1318 drm_mm_put_block(list->file_offset_node);
1319 list->file_offset_node = NULL;
1320 }
1321
1322 if (list->map) {
Eric Anholt9a298b22009-03-24 12:23:04 -07001323 kfree(list->map);
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001324 list->map = NULL;
1325 }
1326
1327 obj_priv->mmap_offset = 0;
1328}
1329
Jesse Barnesde151cf2008-11-12 10:03:55 -08001330/**
1331 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1332 * @obj: object to check
1333 *
1334 * Return the required GTT alignment for an object, taking into account
1335 * potential fence register mapping if needed.
1336 */
1337static uint32_t
1338i915_gem_get_gtt_alignment(struct drm_gem_object *obj)
1339{
1340 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001341 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001342 int start, i;
1343
1344 /*
1345 * Minimum alignment is 4k (GTT page size), but might be greater
1346 * if a fence register is needed for the object.
1347 */
1348 if (IS_I965G(dev) || obj_priv->tiling_mode == I915_TILING_NONE)
1349 return 4096;
1350
1351 /*
1352 * Previous chips need to be aligned to the size of the smallest
1353 * fence register that can contain the object.
1354 */
1355 if (IS_I9XX(dev))
1356 start = 1024*1024;
1357 else
1358 start = 512*1024;
1359
1360 for (i = start; i < obj->size; i <<= 1)
1361 ;
1362
1363 return i;
1364}
1365
1366/**
1367 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1368 * @dev: DRM device
1369 * @data: GTT mapping ioctl data
1370 * @file_priv: GEM object info
1371 *
1372 * Simply returns the fake offset to userspace so it can mmap it.
1373 * The mmap call will end up in drm_gem_mmap(), which will set things
1374 * up so we can get faults in the handler above.
1375 *
1376 * The fault handler will take care of binding the object into the GTT
1377 * (since it may have been evicted to make room for something), allocating
1378 * a fence register, and mapping the appropriate aperture address into
1379 * userspace.
1380 */
1381int
1382i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
1383 struct drm_file *file_priv)
1384{
1385 struct drm_i915_gem_mmap_gtt *args = data;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001386 struct drm_gem_object *obj;
1387 struct drm_i915_gem_object *obj_priv;
1388 int ret;
1389
1390 if (!(dev->driver->driver_features & DRIVER_GEM))
1391 return -ENODEV;
1392
1393 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1394 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001395 return -ENOENT;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001396
1397 mutex_lock(&dev->struct_mutex);
1398
Daniel Vetter23010e42010-03-08 13:35:02 +01001399 obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001400
Chris Wilsonab182822009-09-22 18:46:17 +01001401 if (obj_priv->madv != I915_MADV_WILLNEED) {
1402 DRM_ERROR("Attempting to mmap a purgeable buffer\n");
1403 drm_gem_object_unreference(obj);
1404 mutex_unlock(&dev->struct_mutex);
1405 return -EINVAL;
1406 }
1407
1408
Jesse Barnesde151cf2008-11-12 10:03:55 -08001409 if (!obj_priv->mmap_offset) {
1410 ret = i915_gem_create_mmap_offset(obj);
Chris Wilson13af1062009-02-11 14:26:31 +00001411 if (ret) {
1412 drm_gem_object_unreference(obj);
1413 mutex_unlock(&dev->struct_mutex);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001414 return ret;
Chris Wilson13af1062009-02-11 14:26:31 +00001415 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001416 }
1417
1418 args->offset = obj_priv->mmap_offset;
1419
Jesse Barnesde151cf2008-11-12 10:03:55 -08001420 /*
1421 * Pull it into the GTT so that we have a page list (makes the
1422 * initial fault faster and any subsequent flushing possible).
1423 */
1424 if (!obj_priv->agp_mem) {
Chris Wilsone67b8ce2009-09-14 16:50:26 +01001425 ret = i915_gem_object_bind_to_gtt(obj, 0);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001426 if (ret) {
1427 drm_gem_object_unreference(obj);
1428 mutex_unlock(&dev->struct_mutex);
1429 return ret;
1430 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001431 }
1432
1433 drm_gem_object_unreference(obj);
1434 mutex_unlock(&dev->struct_mutex);
1435
1436 return 0;
1437}
1438
Ben Gamari6911a9b2009-04-02 11:24:54 -07001439void
Eric Anholt856fa192009-03-19 14:10:50 -07001440i915_gem_object_put_pages(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07001441{
Daniel Vetter23010e42010-03-08 13:35:02 +01001442 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001443 int page_count = obj->size / PAGE_SIZE;
1444 int i;
1445
Eric Anholt856fa192009-03-19 14:10:50 -07001446 BUG_ON(obj_priv->pages_refcount == 0);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001447 BUG_ON(obj_priv->madv == __I915_MADV_PURGED);
Eric Anholt856fa192009-03-19 14:10:50 -07001448
1449 if (--obj_priv->pages_refcount != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07001450 return;
1451
Eric Anholt280b7132009-03-12 16:56:27 -07001452 if (obj_priv->tiling_mode != I915_TILING_NONE)
1453 i915_gem_object_save_bit_17_swizzle(obj);
1454
Chris Wilson3ef94da2009-09-14 16:50:29 +01001455 if (obj_priv->madv == I915_MADV_DONTNEED)
Chris Wilson13a05fd2009-09-20 23:03:19 +01001456 obj_priv->dirty = 0;
Chris Wilson3ef94da2009-09-14 16:50:29 +01001457
1458 for (i = 0; i < page_count; i++) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01001459 if (obj_priv->dirty)
1460 set_page_dirty(obj_priv->pages[i]);
1461
1462 if (obj_priv->madv == I915_MADV_WILLNEED)
Eric Anholt856fa192009-03-19 14:10:50 -07001463 mark_page_accessed(obj_priv->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001464
1465 page_cache_release(obj_priv->pages[i]);
1466 }
Eric Anholt673a3942008-07-30 12:06:12 -07001467 obj_priv->dirty = 0;
1468
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07001469 drm_free_large(obj_priv->pages);
Eric Anholt856fa192009-03-19 14:10:50 -07001470 obj_priv->pages = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001471}
1472
Daniel Vettere35a41d2010-02-11 22:13:59 +01001473static uint32_t
Daniel Vettera6910432010-02-02 17:08:37 +01001474i915_gem_next_request_seqno(struct drm_device *dev,
1475 struct intel_ring_buffer *ring)
Daniel Vettere35a41d2010-02-11 22:13:59 +01001476{
1477 drm_i915_private_t *dev_priv = dev->dev_private;
1478
Daniel Vettera6910432010-02-02 17:08:37 +01001479 ring->outstanding_lazy_request = true;
1480
Daniel Vettere35a41d2010-02-11 22:13:59 +01001481 return dev_priv->next_seqno;
1482}
1483
Eric Anholt673a3942008-07-30 12:06:12 -07001484static void
Daniel Vetter617dbe22010-02-11 22:16:02 +01001485i915_gem_object_move_to_active(struct drm_gem_object *obj,
Zou Nan hai852835f2010-05-21 09:08:56 +08001486 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001487{
1488 struct drm_device *dev = obj->dev;
1489 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001490 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Daniel Vetter617dbe22010-02-11 22:16:02 +01001491 uint32_t seqno = i915_gem_next_request_seqno(dev, ring);
1492
Zou Nan hai852835f2010-05-21 09:08:56 +08001493 BUG_ON(ring == NULL);
1494 obj_priv->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001495
1496 /* Add a reference if we're newly entering the active list. */
1497 if (!obj_priv->active) {
1498 drm_gem_object_reference(obj);
1499 obj_priv->active = 1;
1500 }
Daniel Vettere35a41d2010-02-11 22:13:59 +01001501
Eric Anholt673a3942008-07-30 12:06:12 -07001502 /* Move from whatever list we were on to the tail of execution. */
Carl Worth5e118f42009-03-20 11:54:25 -07001503 spin_lock(&dev_priv->mm.active_list_lock);
Zou Nan hai852835f2010-05-21 09:08:56 +08001504 list_move_tail(&obj_priv->list, &ring->active_list);
Carl Worth5e118f42009-03-20 11:54:25 -07001505 spin_unlock(&dev_priv->mm.active_list_lock);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001506 obj_priv->last_rendering_seqno = seqno;
Eric Anholt673a3942008-07-30 12:06:12 -07001507}
1508
Eric Anholtce44b0e2008-11-06 16:00:31 -08001509static void
1510i915_gem_object_move_to_flushing(struct drm_gem_object *obj)
1511{
1512 struct drm_device *dev = obj->dev;
1513 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001514 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001515
1516 BUG_ON(!obj_priv->active);
1517 list_move_tail(&obj_priv->list, &dev_priv->mm.flushing_list);
1518 obj_priv->last_rendering_seqno = 0;
1519}
Eric Anholt673a3942008-07-30 12:06:12 -07001520
Chris Wilson963b4832009-09-20 23:03:54 +01001521/* Immediately discard the backing storage */
1522static void
1523i915_gem_object_truncate(struct drm_gem_object *obj)
1524{
Daniel Vetter23010e42010-03-08 13:35:02 +01001525 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001526 struct inode *inode;
Chris Wilson963b4832009-09-20 23:03:54 +01001527
Chris Wilsonae9fed62010-08-07 11:01:30 +01001528 /* Our goal here is to return as much of the memory as
1529 * is possible back to the system as we are called from OOM.
1530 * To do this we must instruct the shmfs to drop all of its
1531 * backing pages, *now*. Here we mirror the actions taken
1532 * when by shmem_delete_inode() to release the backing store.
1533 */
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001534 inode = obj->filp->f_path.dentry->d_inode;
Chris Wilsonae9fed62010-08-07 11:01:30 +01001535 truncate_inode_pages(inode->i_mapping, 0);
1536 if (inode->i_op->truncate_range)
1537 inode->i_op->truncate_range(inode, 0, (loff_t)-1);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001538
1539 obj_priv->madv = __I915_MADV_PURGED;
Chris Wilson963b4832009-09-20 23:03:54 +01001540}
1541
1542static inline int
1543i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj_priv)
1544{
1545 return obj_priv->madv == I915_MADV_DONTNEED;
1546}
1547
Eric Anholt673a3942008-07-30 12:06:12 -07001548static void
1549i915_gem_object_move_to_inactive(struct drm_gem_object *obj)
1550{
1551 struct drm_device *dev = obj->dev;
1552 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001553 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001554
1555 i915_verify_inactive(dev, __FILE__, __LINE__);
1556 if (obj_priv->pin_count != 0)
1557 list_del_init(&obj_priv->list);
1558 else
1559 list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
1560
Daniel Vetter99fcb762010-02-07 16:20:18 +01001561 BUG_ON(!list_empty(&obj_priv->gpu_write_list));
1562
Eric Anholtce44b0e2008-11-06 16:00:31 -08001563 obj_priv->last_rendering_seqno = 0;
Zou Nan hai852835f2010-05-21 09:08:56 +08001564 obj_priv->ring = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001565 if (obj_priv->active) {
1566 obj_priv->active = 0;
1567 drm_gem_object_unreference(obj);
1568 }
1569 i915_verify_inactive(dev, __FILE__, __LINE__);
1570}
1571
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001572void
Daniel Vetter63560392010-02-19 11:51:59 +01001573i915_gem_process_flushing_list(struct drm_device *dev,
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001574 uint32_t flush_domains,
Zou Nan hai852835f2010-05-21 09:08:56 +08001575 struct intel_ring_buffer *ring)
Daniel Vetter63560392010-02-19 11:51:59 +01001576{
1577 drm_i915_private_t *dev_priv = dev->dev_private;
1578 struct drm_i915_gem_object *obj_priv, *next;
1579
1580 list_for_each_entry_safe(obj_priv, next,
1581 &dev_priv->mm.gpu_write_list,
1582 gpu_write_list) {
Daniel Vettera8089e82010-04-09 19:05:09 +00001583 struct drm_gem_object *obj = &obj_priv->base;
Daniel Vetter63560392010-02-19 11:51:59 +01001584
1585 if ((obj->write_domain & flush_domains) ==
Zou Nan hai852835f2010-05-21 09:08:56 +08001586 obj->write_domain &&
1587 obj_priv->ring->ring_flag == ring->ring_flag) {
Daniel Vetter63560392010-02-19 11:51:59 +01001588 uint32_t old_write_domain = obj->write_domain;
1589
1590 obj->write_domain = 0;
1591 list_del_init(&obj_priv->gpu_write_list);
Daniel Vetter617dbe22010-02-11 22:16:02 +01001592 i915_gem_object_move_to_active(obj, ring);
Daniel Vetter63560392010-02-19 11:51:59 +01001593
1594 /* update the fence lru list */
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001595 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
1596 struct drm_i915_fence_reg *reg =
1597 &dev_priv->fence_regs[obj_priv->fence_reg];
1598 list_move_tail(&reg->lru_list,
Daniel Vetter63560392010-02-19 11:51:59 +01001599 &dev_priv->mm.fence_list);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001600 }
Daniel Vetter63560392010-02-19 11:51:59 +01001601
1602 trace_i915_gem_object_change_domain(obj,
1603 obj->read_domains,
1604 old_write_domain);
1605 }
1606 }
1607}
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001608
Daniel Vetter5a5a0c62009-09-15 22:57:36 +02001609uint32_t
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001610i915_add_request(struct drm_device *dev,
1611 struct drm_file *file_priv,
Chris Wilson8dc5d142010-08-12 12:36:12 +01001612 struct drm_i915_gem_request *request,
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001613 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001614{
1615 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtb9624422009-06-03 07:27:35 +00001616 struct drm_i915_file_private *i915_file_priv = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001617 uint32_t seqno;
1618 int was_empty;
Eric Anholt673a3942008-07-30 12:06:12 -07001619
Eric Anholtb9624422009-06-03 07:27:35 +00001620 if (file_priv != NULL)
1621 i915_file_priv = file_priv->driver_priv;
1622
Chris Wilson8dc5d142010-08-12 12:36:12 +01001623 if (request == NULL) {
1624 request = kzalloc(sizeof(*request), GFP_KERNEL);
1625 if (request == NULL)
1626 return 0;
1627 }
Eric Anholt673a3942008-07-30 12:06:12 -07001628
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001629 seqno = ring->add_request(dev, ring, file_priv, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07001630
1631 request->seqno = seqno;
Zou Nan hai852835f2010-05-21 09:08:56 +08001632 request->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001633 request->emitted_jiffies = jiffies;
Zou Nan hai852835f2010-05-21 09:08:56 +08001634 was_empty = list_empty(&ring->request_list);
1635 list_add_tail(&request->list, &ring->request_list);
1636
Eric Anholtb9624422009-06-03 07:27:35 +00001637 if (i915_file_priv) {
1638 list_add_tail(&request->client_list,
1639 &i915_file_priv->mm.request_list);
1640 } else {
1641 INIT_LIST_HEAD(&request->client_list);
1642 }
Eric Anholt673a3942008-07-30 12:06:12 -07001643
Ben Gamarif65d9422009-09-14 17:48:44 -04001644 if (!dev_priv->mm.suspended) {
1645 mod_timer(&dev_priv->hangcheck_timer, jiffies + DRM_I915_HANGCHECK_PERIOD);
1646 if (was_empty)
1647 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
1648 }
Eric Anholt673a3942008-07-30 12:06:12 -07001649 return seqno;
1650}
1651
1652/**
1653 * Command execution barrier
1654 *
1655 * Ensures that all commands in the ring are finished
1656 * before signalling the CPU
1657 */
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001658static void
Zou Nan hai852835f2010-05-21 09:08:56 +08001659i915_retire_commands(struct drm_device *dev, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001660{
Eric Anholt673a3942008-07-30 12:06:12 -07001661 uint32_t flush_domains = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001662
1663 /* The sampler always gets flushed on i965 (sigh) */
1664 if (IS_I965G(dev))
1665 flush_domains |= I915_GEM_DOMAIN_SAMPLER;
Zou Nan hai852835f2010-05-21 09:08:56 +08001666
1667 ring->flush(dev, ring,
1668 I915_GEM_DOMAIN_COMMAND, flush_domains);
Eric Anholt673a3942008-07-30 12:06:12 -07001669}
1670
1671/**
1672 * Moves buffers associated only with the given active seqno from the active
1673 * to inactive list, potentially freeing them.
1674 */
1675static void
1676i915_gem_retire_request(struct drm_device *dev,
1677 struct drm_i915_gem_request *request)
1678{
1679 drm_i915_private_t *dev_priv = dev->dev_private;
1680
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001681 trace_i915_gem_request_retire(dev, request->seqno);
1682
Eric Anholt673a3942008-07-30 12:06:12 -07001683 /* Move any buffers on the active list that are no longer referenced
1684 * by the ringbuffer to the flushing/inactive lists as appropriate.
1685 */
Carl Worth5e118f42009-03-20 11:54:25 -07001686 spin_lock(&dev_priv->mm.active_list_lock);
Zou Nan hai852835f2010-05-21 09:08:56 +08001687 while (!list_empty(&request->ring->active_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001688 struct drm_gem_object *obj;
1689 struct drm_i915_gem_object *obj_priv;
1690
Zou Nan hai852835f2010-05-21 09:08:56 +08001691 obj_priv = list_first_entry(&request->ring->active_list,
Eric Anholt673a3942008-07-30 12:06:12 -07001692 struct drm_i915_gem_object,
1693 list);
Daniel Vettera8089e82010-04-09 19:05:09 +00001694 obj = &obj_priv->base;
Eric Anholt673a3942008-07-30 12:06:12 -07001695
1696 /* If the seqno being retired doesn't match the oldest in the
1697 * list, then the oldest in the list must still be newer than
1698 * this seqno.
1699 */
1700 if (obj_priv->last_rendering_seqno != request->seqno)
Carl Worth5e118f42009-03-20 11:54:25 -07001701 goto out;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001702
Eric Anholt673a3942008-07-30 12:06:12 -07001703#if WATCH_LRU
1704 DRM_INFO("%s: retire %d moves to inactive list %p\n",
1705 __func__, request->seqno, obj);
1706#endif
1707
Eric Anholtce44b0e2008-11-06 16:00:31 -08001708 if (obj->write_domain != 0)
1709 i915_gem_object_move_to_flushing(obj);
Shaohua Li68c84342009-04-08 10:58:23 +08001710 else {
1711 /* Take a reference on the object so it won't be
1712 * freed while the spinlock is held. The list
1713 * protection for this spinlock is safe when breaking
1714 * the lock like this since the next thing we do
1715 * is just get the head of the list again.
1716 */
1717 drm_gem_object_reference(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001718 i915_gem_object_move_to_inactive(obj);
Shaohua Li68c84342009-04-08 10:58:23 +08001719 spin_unlock(&dev_priv->mm.active_list_lock);
1720 drm_gem_object_unreference(obj);
1721 spin_lock(&dev_priv->mm.active_list_lock);
1722 }
Eric Anholt673a3942008-07-30 12:06:12 -07001723 }
Carl Worth5e118f42009-03-20 11:54:25 -07001724out:
1725 spin_unlock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001726}
1727
1728/**
1729 * Returns true if seq1 is later than seq2.
1730 */
Ben Gamari22be1722009-09-14 17:48:43 -04001731bool
Eric Anholt673a3942008-07-30 12:06:12 -07001732i915_seqno_passed(uint32_t seq1, uint32_t seq2)
1733{
1734 return (int32_t)(seq1 - seq2) >= 0;
1735}
1736
1737uint32_t
Zou Nan hai852835f2010-05-21 09:08:56 +08001738i915_get_gem_seqno(struct drm_device *dev,
Zou Nan haid1b851f2010-05-21 09:08:57 +08001739 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001740{
Zou Nan hai852835f2010-05-21 09:08:56 +08001741 return ring->get_gem_seqno(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07001742}
1743
1744/**
1745 * This function clears the request list as sequence numbers are passed.
1746 */
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001747static void
1748i915_gem_retire_requests_ring(struct drm_device *dev,
1749 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001750{
1751 drm_i915_private_t *dev_priv = dev->dev_private;
1752 uint32_t seqno;
1753
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001754 if (!ring->status_page.page_addr
Zou Nan hai852835f2010-05-21 09:08:56 +08001755 || list_empty(&ring->request_list))
Karsten Wiese6c0594a2009-02-23 15:07:57 +01001756 return;
1757
Zou Nan hai852835f2010-05-21 09:08:56 +08001758 seqno = i915_get_gem_seqno(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07001759
Zou Nan hai852835f2010-05-21 09:08:56 +08001760 while (!list_empty(&ring->request_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001761 struct drm_i915_gem_request *request;
1762 uint32_t retiring_seqno;
1763
Zou Nan hai852835f2010-05-21 09:08:56 +08001764 request = list_first_entry(&ring->request_list,
Eric Anholt673a3942008-07-30 12:06:12 -07001765 struct drm_i915_gem_request,
1766 list);
1767 retiring_seqno = request->seqno;
1768
1769 if (i915_seqno_passed(seqno, retiring_seqno) ||
Ben Gamariba1234d2009-09-14 17:48:47 -04001770 atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001771 i915_gem_retire_request(dev, request);
1772
1773 list_del(&request->list);
Eric Anholtb9624422009-06-03 07:27:35 +00001774 list_del(&request->client_list);
Eric Anholt9a298b22009-03-24 12:23:04 -07001775 kfree(request);
Eric Anholt673a3942008-07-30 12:06:12 -07001776 } else
1777 break;
1778 }
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001779
1780 if (unlikely (dev_priv->trace_irq_seqno &&
1781 i915_seqno_passed(dev_priv->trace_irq_seqno, seqno))) {
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001782
1783 ring->user_irq_put(dev, ring);
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001784 dev_priv->trace_irq_seqno = 0;
1785 }
Eric Anholt673a3942008-07-30 12:06:12 -07001786}
1787
1788void
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001789i915_gem_retire_requests(struct drm_device *dev)
1790{
1791 drm_i915_private_t *dev_priv = dev->dev_private;
1792
Chris Wilsonbe726152010-07-23 23:18:50 +01001793 if (!list_empty(&dev_priv->mm.deferred_free_list)) {
1794 struct drm_i915_gem_object *obj_priv, *tmp;
1795
1796 /* We must be careful that during unbind() we do not
1797 * accidentally infinitely recurse into retire requests.
1798 * Currently:
1799 * retire -> free -> unbind -> wait -> retire_ring
1800 */
1801 list_for_each_entry_safe(obj_priv, tmp,
1802 &dev_priv->mm.deferred_free_list,
1803 list)
1804 i915_gem_free_object_tail(&obj_priv->base);
1805 }
1806
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001807 i915_gem_retire_requests_ring(dev, &dev_priv->render_ring);
1808 if (HAS_BSD(dev))
1809 i915_gem_retire_requests_ring(dev, &dev_priv->bsd_ring);
1810}
1811
Daniel Vetter75ef9da2010-08-21 00:25:16 +02001812static void
Eric Anholt673a3942008-07-30 12:06:12 -07001813i915_gem_retire_work_handler(struct work_struct *work)
1814{
1815 drm_i915_private_t *dev_priv;
1816 struct drm_device *dev;
1817
1818 dev_priv = container_of(work, drm_i915_private_t,
1819 mm.retire_work.work);
1820 dev = dev_priv->dev;
1821
1822 mutex_lock(&dev->struct_mutex);
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001823 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08001824
Keith Packard6dbe2772008-10-14 21:41:13 -07001825 if (!dev_priv->mm.suspended &&
Zou Nan haid1b851f2010-05-21 09:08:57 +08001826 (!list_empty(&dev_priv->render_ring.request_list) ||
1827 (HAS_BSD(dev) &&
1828 !list_empty(&dev_priv->bsd_ring.request_list))))
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001829 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
Eric Anholt673a3942008-07-30 12:06:12 -07001830 mutex_unlock(&dev->struct_mutex);
1831}
1832
Daniel Vetter5a5a0c62009-09-15 22:57:36 +02001833int
Zou Nan hai852835f2010-05-21 09:08:56 +08001834i915_do_wait_request(struct drm_device *dev, uint32_t seqno,
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001835 bool interruptible, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001836{
1837 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001838 u32 ier;
Eric Anholt673a3942008-07-30 12:06:12 -07001839 int ret = 0;
1840
1841 BUG_ON(seqno == 0);
1842
Daniel Vettere35a41d2010-02-11 22:13:59 +01001843 if (seqno == dev_priv->next_seqno) {
Chris Wilson8dc5d142010-08-12 12:36:12 +01001844 seqno = i915_add_request(dev, NULL, NULL, ring);
Daniel Vettere35a41d2010-02-11 22:13:59 +01001845 if (seqno == 0)
1846 return -ENOMEM;
1847 }
1848
Ben Gamariba1234d2009-09-14 17:48:47 -04001849 if (atomic_read(&dev_priv->mm.wedged))
Ben Gamariffed1d02009-09-14 17:48:41 -04001850 return -EIO;
1851
Zou Nan hai852835f2010-05-21 09:08:56 +08001852 if (!i915_seqno_passed(ring->get_gem_seqno(dev, ring), seqno)) {
Eric Anholtbad720f2009-10-22 16:11:14 -07001853 if (HAS_PCH_SPLIT(dev))
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001854 ier = I915_READ(DEIER) | I915_READ(GTIER);
1855 else
1856 ier = I915_READ(IER);
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001857 if (!ier) {
1858 DRM_ERROR("something (likely vbetool) disabled "
1859 "interrupts, re-enabling\n");
1860 i915_driver_irq_preinstall(dev);
1861 i915_driver_irq_postinstall(dev);
1862 }
1863
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001864 trace_i915_gem_request_wait_begin(dev, seqno);
1865
Zou Nan hai852835f2010-05-21 09:08:56 +08001866 ring->waiting_gem_seqno = seqno;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001867 ring->user_irq_get(dev, ring);
Daniel Vetter48764bf2009-09-15 22:57:32 +02001868 if (interruptible)
Zou Nan hai852835f2010-05-21 09:08:56 +08001869 ret = wait_event_interruptible(ring->irq_queue,
1870 i915_seqno_passed(
1871 ring->get_gem_seqno(dev, ring), seqno)
1872 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02001873 else
Zou Nan hai852835f2010-05-21 09:08:56 +08001874 wait_event(ring->irq_queue,
1875 i915_seqno_passed(
1876 ring->get_gem_seqno(dev, ring), seqno)
1877 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02001878
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001879 ring->user_irq_put(dev, ring);
Zou Nan hai852835f2010-05-21 09:08:56 +08001880 ring->waiting_gem_seqno = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001881
1882 trace_i915_gem_request_wait_end(dev, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07001883 }
Ben Gamariba1234d2009-09-14 17:48:47 -04001884 if (atomic_read(&dev_priv->mm.wedged))
Eric Anholt673a3942008-07-30 12:06:12 -07001885 ret = -EIO;
1886
1887 if (ret && ret != -ERESTARTSYS)
Daniel Vetter8bff9172010-02-11 22:19:40 +01001888 DRM_ERROR("%s returns %d (awaiting %d at %d, next %d)\n",
1889 __func__, ret, seqno, ring->get_gem_seqno(dev, ring),
1890 dev_priv->next_seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07001891
1892 /* Directly dispatch request retiring. While we have the work queue
1893 * to handle this, the waiter on a request often wants an associated
1894 * buffer to have made it to the inactive list, and we would need
1895 * a separate wait queue to handle that.
1896 */
1897 if (ret == 0)
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001898 i915_gem_retire_requests_ring(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07001899
1900 return ret;
1901}
1902
Daniel Vetter48764bf2009-09-15 22:57:32 +02001903/**
1904 * Waits for a sequence number to be signaled, and cleans up the
1905 * request and object lists appropriately for that event.
1906 */
1907static int
Zou Nan hai852835f2010-05-21 09:08:56 +08001908i915_wait_request(struct drm_device *dev, uint32_t seqno,
1909 struct intel_ring_buffer *ring)
Daniel Vetter48764bf2009-09-15 22:57:32 +02001910{
Zou Nan hai852835f2010-05-21 09:08:56 +08001911 return i915_do_wait_request(dev, seqno, 1, ring);
Daniel Vetter48764bf2009-09-15 22:57:32 +02001912}
1913
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001914static void
1915i915_gem_flush(struct drm_device *dev,
1916 uint32_t invalidate_domains,
1917 uint32_t flush_domains)
1918{
1919 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter8bff9172010-02-11 22:19:40 +01001920
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001921 if (flush_domains & I915_GEM_DOMAIN_CPU)
1922 drm_agp_chipset_flush(dev);
Daniel Vetter8bff9172010-02-11 22:19:40 +01001923
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001924 dev_priv->render_ring.flush(dev, &dev_priv->render_ring,
1925 invalidate_domains,
1926 flush_domains);
Zou Nan haid1b851f2010-05-21 09:08:57 +08001927
1928 if (HAS_BSD(dev))
1929 dev_priv->bsd_ring.flush(dev, &dev_priv->bsd_ring,
1930 invalidate_domains,
1931 flush_domains);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001932}
1933
Eric Anholt673a3942008-07-30 12:06:12 -07001934/**
1935 * Ensures that all rendering to the object has completed and the object is
1936 * safe to unbind from the GTT or access from the CPU.
1937 */
1938static int
Daniel Vetterba3d8d72010-02-11 22:37:04 +01001939i915_gem_object_wait_rendering(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07001940{
1941 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001942 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001943 int ret;
1944
Eric Anholte47c68e2008-11-14 13:35:19 -08001945 /* This function only exists to support waiting for existing rendering,
1946 * not for emitting required flushes.
Eric Anholt673a3942008-07-30 12:06:12 -07001947 */
Eric Anholte47c68e2008-11-14 13:35:19 -08001948 BUG_ON((obj->write_domain & I915_GEM_GPU_DOMAINS) != 0);
Eric Anholt673a3942008-07-30 12:06:12 -07001949
1950 /* If there is rendering queued on the buffer being evicted, wait for
1951 * it.
1952 */
1953 if (obj_priv->active) {
1954#if WATCH_BUF
1955 DRM_INFO("%s: object %p wait for seqno %08x\n",
1956 __func__, obj, obj_priv->last_rendering_seqno);
1957#endif
Daniel Vetterba3d8d72010-02-11 22:37:04 +01001958 ret = i915_wait_request(dev,
1959 obj_priv->last_rendering_seqno,
1960 obj_priv->ring);
Eric Anholt673a3942008-07-30 12:06:12 -07001961 if (ret != 0)
1962 return ret;
1963 }
1964
1965 return 0;
1966}
1967
1968/**
1969 * Unbinds an object from the GTT aperture.
1970 */
Jesse Barnes0f973f22009-01-26 17:10:45 -08001971int
Eric Anholt673a3942008-07-30 12:06:12 -07001972i915_gem_object_unbind(struct drm_gem_object *obj)
1973{
1974 struct drm_device *dev = obj->dev;
Daniel Vetter4a87b8c2010-02-19 11:51:57 +01001975 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001976 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001977 int ret = 0;
1978
1979#if WATCH_BUF
1980 DRM_INFO("%s:%d %p\n", __func__, __LINE__, obj);
1981 DRM_INFO("gtt_space %p\n", obj_priv->gtt_space);
1982#endif
1983 if (obj_priv->gtt_space == NULL)
1984 return 0;
1985
1986 if (obj_priv->pin_count != 0) {
1987 DRM_ERROR("Attempting to unbind pinned buffer\n");
1988 return -EINVAL;
1989 }
1990
Eric Anholt5323fd02009-09-09 11:50:45 -07001991 /* blow away mappings if mapped through GTT */
1992 i915_gem_release_mmap(obj);
1993
Eric Anholt673a3942008-07-30 12:06:12 -07001994 /* Move the object to the CPU domain to ensure that
1995 * any possible CPU writes while it's not in the GTT
1996 * are flushed when we go to remap it. This will
1997 * also ensure that all pending GPU writes are finished
1998 * before we unbind.
1999 */
Eric Anholte47c68e2008-11-14 13:35:19 -08002000 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Chris Wilson8dc17752010-07-23 23:18:51 +01002001 if (ret == -ERESTARTSYS)
Eric Anholt673a3942008-07-30 12:06:12 -07002002 return ret;
Chris Wilson8dc17752010-07-23 23:18:51 +01002003 /* Continue on if we fail due to EIO, the GPU is hung so we
2004 * should be safe and we need to cleanup or else we might
2005 * cause memory corruption through use-after-free.
2006 */
Eric Anholt673a3942008-07-30 12:06:12 -07002007
Daniel Vetter96b47b62009-12-15 17:50:00 +01002008 /* release the fence reg _after_ flushing */
2009 if (obj_priv->fence_reg != I915_FENCE_REG_NONE)
2010 i915_gem_clear_fence_reg(obj);
2011
Eric Anholt673a3942008-07-30 12:06:12 -07002012 if (obj_priv->agp_mem != NULL) {
2013 drm_unbind_agp(obj_priv->agp_mem);
2014 drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE);
2015 obj_priv->agp_mem = NULL;
2016 }
2017
Eric Anholt856fa192009-03-19 14:10:50 -07002018 i915_gem_object_put_pages(obj);
Chris Wilsona32808c2009-09-20 21:29:47 +01002019 BUG_ON(obj_priv->pages_refcount);
Eric Anholt673a3942008-07-30 12:06:12 -07002020
2021 if (obj_priv->gtt_space) {
2022 atomic_dec(&dev->gtt_count);
2023 atomic_sub(obj->size, &dev->gtt_memory);
2024
2025 drm_mm_put_block(obj_priv->gtt_space);
2026 obj_priv->gtt_space = NULL;
2027 }
2028
2029 /* Remove ourselves from the LRU list if present. */
Daniel Vetter4a87b8c2010-02-19 11:51:57 +01002030 spin_lock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07002031 if (!list_empty(&obj_priv->list))
2032 list_del_init(&obj_priv->list);
Daniel Vetter4a87b8c2010-02-19 11:51:57 +01002033 spin_unlock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07002034
Chris Wilson963b4832009-09-20 23:03:54 +01002035 if (i915_gem_object_is_purgeable(obj_priv))
2036 i915_gem_object_truncate(obj);
2037
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002038 trace_i915_gem_object_unbind(obj);
2039
Chris Wilson8dc17752010-07-23 23:18:51 +01002040 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002041}
2042
Chris Wilsonb47eb4a2010-08-07 11:01:23 +01002043int
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002044i915_gpu_idle(struct drm_device *dev)
2045{
2046 drm_i915_private_t *dev_priv = dev->dev_private;
2047 bool lists_empty;
Zou Nan hai852835f2010-05-21 09:08:56 +08002048 int ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002049
2050 spin_lock(&dev_priv->mm.active_list_lock);
Zou Nan haid1b851f2010-05-21 09:08:57 +08002051 lists_empty = (list_empty(&dev_priv->mm.flushing_list) &&
2052 list_empty(&dev_priv->render_ring.active_list) &&
2053 (!HAS_BSD(dev) ||
2054 list_empty(&dev_priv->bsd_ring.active_list)));
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002055 spin_unlock(&dev_priv->mm.active_list_lock);
2056
2057 if (lists_empty)
2058 return 0;
2059
2060 /* Flush everything onto the inactive list. */
2061 i915_gem_flush(dev, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
Daniel Vetter4fc6ee72010-02-11 22:53:20 +01002062
2063 ret = i915_wait_request(dev,
2064 i915_gem_next_request_seqno(dev, &dev_priv->render_ring),
2065 &dev_priv->render_ring);
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002066 if (ret)
2067 return ret;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002068
2069 if (HAS_BSD(dev)) {
Daniel Vetter4fc6ee72010-02-11 22:53:20 +01002070 ret = i915_wait_request(dev,
2071 i915_gem_next_request_seqno(dev, &dev_priv->bsd_ring),
2072 &dev_priv->bsd_ring);
Zou Nan haid1b851f2010-05-21 09:08:57 +08002073 if (ret)
2074 return ret;
2075 }
2076
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002077 return 0;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002078}
2079
Ben Gamari6911a9b2009-04-02 11:24:54 -07002080int
Chris Wilson4bdadb92010-01-27 13:36:32 +00002081i915_gem_object_get_pages(struct drm_gem_object *obj,
2082 gfp_t gfpmask)
Eric Anholt673a3942008-07-30 12:06:12 -07002083{
Daniel Vetter23010e42010-03-08 13:35:02 +01002084 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002085 int page_count, i;
2086 struct address_space *mapping;
2087 struct inode *inode;
2088 struct page *page;
Eric Anholt673a3942008-07-30 12:06:12 -07002089
Daniel Vetter778c3542010-05-13 11:49:44 +02002090 BUG_ON(obj_priv->pages_refcount
2091 == DRM_I915_GEM_OBJECT_MAX_PAGES_REFCOUNT);
2092
Eric Anholt856fa192009-03-19 14:10:50 -07002093 if (obj_priv->pages_refcount++ != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07002094 return 0;
2095
2096 /* Get the list of pages out of our struct file. They'll be pinned
2097 * at this point until we release them.
2098 */
2099 page_count = obj->size / PAGE_SIZE;
Eric Anholt856fa192009-03-19 14:10:50 -07002100 BUG_ON(obj_priv->pages != NULL);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07002101 obj_priv->pages = drm_calloc_large(page_count, sizeof(struct page *));
Eric Anholt856fa192009-03-19 14:10:50 -07002102 if (obj_priv->pages == NULL) {
Eric Anholt856fa192009-03-19 14:10:50 -07002103 obj_priv->pages_refcount--;
Eric Anholt673a3942008-07-30 12:06:12 -07002104 return -ENOMEM;
2105 }
2106
2107 inode = obj->filp->f_path.dentry->d_inode;
2108 mapping = inode->i_mapping;
2109 for (i = 0; i < page_count; i++) {
Chris Wilson4bdadb92010-01-27 13:36:32 +00002110 page = read_cache_page_gfp(mapping, i,
Linus Torvalds985b8232010-07-02 10:04:42 +10002111 GFP_HIGHUSER |
Chris Wilson4bdadb92010-01-27 13:36:32 +00002112 __GFP_COLD |
Linus Torvaldscd9f0402010-07-18 09:44:37 -07002113 __GFP_RECLAIMABLE |
Chris Wilson4bdadb92010-01-27 13:36:32 +00002114 gfpmask);
Chris Wilson1f2b1012010-03-12 19:52:55 +00002115 if (IS_ERR(page))
2116 goto err_pages;
2117
Eric Anholt856fa192009-03-19 14:10:50 -07002118 obj_priv->pages[i] = page;
Eric Anholt673a3942008-07-30 12:06:12 -07002119 }
Eric Anholt280b7132009-03-12 16:56:27 -07002120
2121 if (obj_priv->tiling_mode != I915_TILING_NONE)
2122 i915_gem_object_do_bit_17_swizzle(obj);
2123
Eric Anholt673a3942008-07-30 12:06:12 -07002124 return 0;
Chris Wilson1f2b1012010-03-12 19:52:55 +00002125
2126err_pages:
2127 while (i--)
2128 page_cache_release(obj_priv->pages[i]);
2129
2130 drm_free_large(obj_priv->pages);
2131 obj_priv->pages = NULL;
2132 obj_priv->pages_refcount--;
2133 return PTR_ERR(page);
Eric Anholt673a3942008-07-30 12:06:12 -07002134}
2135
Eric Anholt4e901fd2009-10-26 16:44:17 -07002136static void sandybridge_write_fence_reg(struct drm_i915_fence_reg *reg)
2137{
2138 struct drm_gem_object *obj = reg->obj;
2139 struct drm_device *dev = obj->dev;
2140 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002141 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt4e901fd2009-10-26 16:44:17 -07002142 int regnum = obj_priv->fence_reg;
2143 uint64_t val;
2144
2145 val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
2146 0xfffff000) << 32;
2147 val |= obj_priv->gtt_offset & 0xfffff000;
2148 val |= (uint64_t)((obj_priv->stride / 128) - 1) <<
2149 SANDYBRIDGE_FENCE_PITCH_SHIFT;
2150
2151 if (obj_priv->tiling_mode == I915_TILING_Y)
2152 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2153 val |= I965_FENCE_REG_VALID;
2154
2155 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (regnum * 8), val);
2156}
2157
Jesse Barnesde151cf2008-11-12 10:03:55 -08002158static void i965_write_fence_reg(struct drm_i915_fence_reg *reg)
2159{
2160 struct drm_gem_object *obj = reg->obj;
2161 struct drm_device *dev = obj->dev;
2162 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002163 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002164 int regnum = obj_priv->fence_reg;
2165 uint64_t val;
2166
2167 val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
2168 0xfffff000) << 32;
2169 val |= obj_priv->gtt_offset & 0xfffff000;
2170 val |= ((obj_priv->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
2171 if (obj_priv->tiling_mode == I915_TILING_Y)
2172 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2173 val |= I965_FENCE_REG_VALID;
2174
2175 I915_WRITE64(FENCE_REG_965_0 + (regnum * 8), val);
2176}
2177
2178static void i915_write_fence_reg(struct drm_i915_fence_reg *reg)
2179{
2180 struct drm_gem_object *obj = reg->obj;
2181 struct drm_device *dev = obj->dev;
2182 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002183 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002184 int regnum = obj_priv->fence_reg;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002185 int tile_width;
Eric Anholtdc529a42009-03-10 22:34:49 -07002186 uint32_t fence_reg, val;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002187 uint32_t pitch_val;
2188
2189 if ((obj_priv->gtt_offset & ~I915_FENCE_START_MASK) ||
2190 (obj_priv->gtt_offset & (obj->size - 1))) {
Linus Torvaldsf06da262009-02-09 08:57:29 -08002191 WARN(1, "%s: object 0x%08x not 1M or size (0x%zx) aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002192 __func__, obj_priv->gtt_offset, obj->size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002193 return;
2194 }
2195
Jesse Barnes0f973f22009-01-26 17:10:45 -08002196 if (obj_priv->tiling_mode == I915_TILING_Y &&
2197 HAS_128_BYTE_Y_TILING(dev))
2198 tile_width = 128;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002199 else
Jesse Barnes0f973f22009-01-26 17:10:45 -08002200 tile_width = 512;
2201
2202 /* Note: pitch better be a power of two tile widths */
2203 pitch_val = obj_priv->stride / tile_width;
2204 pitch_val = ffs(pitch_val) - 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002205
Daniel Vetterc36a2a62010-04-17 15:12:03 +02002206 if (obj_priv->tiling_mode == I915_TILING_Y &&
2207 HAS_128_BYTE_Y_TILING(dev))
2208 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2209 else
2210 WARN_ON(pitch_val > I915_FENCE_MAX_PITCH_VAL);
2211
Jesse Barnesde151cf2008-11-12 10:03:55 -08002212 val = obj_priv->gtt_offset;
2213 if (obj_priv->tiling_mode == I915_TILING_Y)
2214 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2215 val |= I915_FENCE_SIZE_BITS(obj->size);
2216 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2217 val |= I830_FENCE_REG_VALID;
2218
Eric Anholtdc529a42009-03-10 22:34:49 -07002219 if (regnum < 8)
2220 fence_reg = FENCE_REG_830_0 + (regnum * 4);
2221 else
2222 fence_reg = FENCE_REG_945_8 + ((regnum - 8) * 4);
2223 I915_WRITE(fence_reg, val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002224}
2225
2226static void i830_write_fence_reg(struct drm_i915_fence_reg *reg)
2227{
2228 struct drm_gem_object *obj = reg->obj;
2229 struct drm_device *dev = obj->dev;
2230 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002231 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002232 int regnum = obj_priv->fence_reg;
2233 uint32_t val;
2234 uint32_t pitch_val;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002235 uint32_t fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002236
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002237 if ((obj_priv->gtt_offset & ~I830_FENCE_START_MASK) ||
Jesse Barnesde151cf2008-11-12 10:03:55 -08002238 (obj_priv->gtt_offset & (obj->size - 1))) {
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002239 WARN(1, "%s: object 0x%08x not 512K or size aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002240 __func__, obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002241 return;
2242 }
2243
Eric Anholte76a16d2009-05-26 17:44:56 -07002244 pitch_val = obj_priv->stride / 128;
2245 pitch_val = ffs(pitch_val) - 1;
2246 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2247
Jesse Barnesde151cf2008-11-12 10:03:55 -08002248 val = obj_priv->gtt_offset;
2249 if (obj_priv->tiling_mode == I915_TILING_Y)
2250 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002251 fence_size_bits = I830_FENCE_SIZE_BITS(obj->size);
2252 WARN_ON(fence_size_bits & ~0x00000f00);
2253 val |= fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002254 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2255 val |= I830_FENCE_REG_VALID;
2256
2257 I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002258}
2259
Daniel Vetterae3db242010-02-19 11:51:58 +01002260static int i915_find_fence_reg(struct drm_device *dev)
2261{
2262 struct drm_i915_fence_reg *reg = NULL;
2263 struct drm_i915_gem_object *obj_priv = NULL;
2264 struct drm_i915_private *dev_priv = dev->dev_private;
2265 struct drm_gem_object *obj = NULL;
2266 int i, avail, ret;
2267
2268 /* First try to find a free reg */
2269 avail = 0;
2270 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2271 reg = &dev_priv->fence_regs[i];
2272 if (!reg->obj)
2273 return i;
2274
Daniel Vetter23010e42010-03-08 13:35:02 +01002275 obj_priv = to_intel_bo(reg->obj);
Daniel Vetterae3db242010-02-19 11:51:58 +01002276 if (!obj_priv->pin_count)
2277 avail++;
2278 }
2279
2280 if (avail == 0)
2281 return -ENOSPC;
2282
2283 /* None available, try to steal one or wait for a user to finish */
2284 i = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002285 list_for_each_entry(reg, &dev_priv->mm.fence_list,
2286 lru_list) {
2287 obj = reg->obj;
2288 obj_priv = to_intel_bo(obj);
Daniel Vetterae3db242010-02-19 11:51:58 +01002289
2290 if (obj_priv->pin_count)
2291 continue;
2292
2293 /* found one! */
2294 i = obj_priv->fence_reg;
2295 break;
2296 }
2297
2298 BUG_ON(i == I915_FENCE_REG_NONE);
2299
2300 /* We only have a reference on obj from the active list. put_fence_reg
2301 * might drop that one, causing a use-after-free in it. So hold a
2302 * private reference to obj like the other callers of put_fence_reg
2303 * (set_tiling ioctl) do. */
2304 drm_gem_object_reference(obj);
2305 ret = i915_gem_object_put_fence_reg(obj);
2306 drm_gem_object_unreference(obj);
2307 if (ret != 0)
2308 return ret;
2309
2310 return i;
2311}
2312
Jesse Barnesde151cf2008-11-12 10:03:55 -08002313/**
2314 * i915_gem_object_get_fence_reg - set up a fence reg for an object
2315 * @obj: object to map through a fence reg
2316 *
2317 * When mapping objects through the GTT, userspace wants to be able to write
2318 * to them without having to worry about swizzling if the object is tiled.
2319 *
2320 * This function walks the fence regs looking for a free one for @obj,
2321 * stealing one if it can't find any.
2322 *
2323 * It then sets up the reg based on the object's properties: address, pitch
2324 * and tiling format.
2325 */
Chris Wilson8c4b8c32009-06-17 22:08:52 +01002326int
2327i915_gem_object_get_fence_reg(struct drm_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002328{
2329 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002330 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002331 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002332 struct drm_i915_fence_reg *reg = NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002333 int ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002334
Eric Anholta09ba7f2009-08-29 12:49:51 -07002335 /* Just update our place in the LRU if our fence is getting used. */
2336 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002337 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
2338 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002339 return 0;
2340 }
2341
Jesse Barnesde151cf2008-11-12 10:03:55 -08002342 switch (obj_priv->tiling_mode) {
2343 case I915_TILING_NONE:
2344 WARN(1, "allocating a fence for non-tiled object?\n");
2345 break;
2346 case I915_TILING_X:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002347 if (!obj_priv->stride)
2348 return -EINVAL;
2349 WARN((obj_priv->stride & (512 - 1)),
2350 "object 0x%08x is X tiled but has non-512B pitch\n",
2351 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002352 break;
2353 case I915_TILING_Y:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002354 if (!obj_priv->stride)
2355 return -EINVAL;
2356 WARN((obj_priv->stride & (128 - 1)),
2357 "object 0x%08x is Y tiled but has non-128B pitch\n",
2358 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002359 break;
2360 }
2361
Daniel Vetterae3db242010-02-19 11:51:58 +01002362 ret = i915_find_fence_reg(dev);
2363 if (ret < 0)
2364 return ret;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002365
Daniel Vetterae3db242010-02-19 11:51:58 +01002366 obj_priv->fence_reg = ret;
2367 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002368 list_add_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002369
Jesse Barnesde151cf2008-11-12 10:03:55 -08002370 reg->obj = obj;
2371
Eric Anholt4e901fd2009-10-26 16:44:17 -07002372 if (IS_GEN6(dev))
2373 sandybridge_write_fence_reg(reg);
2374 else if (IS_I965G(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08002375 i965_write_fence_reg(reg);
2376 else if (IS_I9XX(dev))
2377 i915_write_fence_reg(reg);
2378 else
2379 i830_write_fence_reg(reg);
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002380
Daniel Vetterae3db242010-02-19 11:51:58 +01002381 trace_i915_gem_object_get_fence(obj, obj_priv->fence_reg,
2382 obj_priv->tiling_mode);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002383
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002384 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002385}
2386
2387/**
2388 * i915_gem_clear_fence_reg - clear out fence register info
2389 * @obj: object to clear
2390 *
2391 * Zeroes out the fence register itself and clears out the associated
2392 * data structures in dev_priv and obj_priv.
2393 */
2394static void
2395i915_gem_clear_fence_reg(struct drm_gem_object *obj)
2396{
2397 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002398 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002399 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002400 struct drm_i915_fence_reg *reg =
2401 &dev_priv->fence_regs[obj_priv->fence_reg];
Jesse Barnesde151cf2008-11-12 10:03:55 -08002402
Eric Anholt4e901fd2009-10-26 16:44:17 -07002403 if (IS_GEN6(dev)) {
2404 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 +
2405 (obj_priv->fence_reg * 8), 0);
2406 } else if (IS_I965G(dev)) {
Jesse Barnesde151cf2008-11-12 10:03:55 -08002407 I915_WRITE64(FENCE_REG_965_0 + (obj_priv->fence_reg * 8), 0);
Eric Anholt4e901fd2009-10-26 16:44:17 -07002408 } else {
Eric Anholtdc529a42009-03-10 22:34:49 -07002409 uint32_t fence_reg;
2410
2411 if (obj_priv->fence_reg < 8)
2412 fence_reg = FENCE_REG_830_0 + obj_priv->fence_reg * 4;
2413 else
2414 fence_reg = FENCE_REG_945_8 + (obj_priv->fence_reg -
2415 8) * 4;
2416
2417 I915_WRITE(fence_reg, 0);
2418 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002419
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002420 reg->obj = NULL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002421 obj_priv->fence_reg = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002422 list_del_init(&reg->lru_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002423}
2424
Eric Anholt673a3942008-07-30 12:06:12 -07002425/**
Chris Wilson52dc7d32009-06-06 09:46:01 +01002426 * i915_gem_object_put_fence_reg - waits on outstanding fenced access
2427 * to the buffer to finish, and then resets the fence register.
2428 * @obj: tiled object holding a fence register.
2429 *
2430 * Zeroes out the fence register itself and clears out the associated
2431 * data structures in dev_priv and obj_priv.
2432 */
2433int
2434i915_gem_object_put_fence_reg(struct drm_gem_object *obj)
2435{
2436 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01002437 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson52dc7d32009-06-06 09:46:01 +01002438
2439 if (obj_priv->fence_reg == I915_FENCE_REG_NONE)
2440 return 0;
2441
Daniel Vetter10ae9bd2010-02-01 13:59:17 +01002442 /* If we've changed tiling, GTT-mappings of the object
2443 * need to re-fault to ensure that the correct fence register
2444 * setup is in place.
2445 */
2446 i915_gem_release_mmap(obj);
2447
Chris Wilson52dc7d32009-06-06 09:46:01 +01002448 /* On the i915, GPU access to tiled buffers is via a fence,
2449 * therefore we must wait for any outstanding access to complete
2450 * before clearing the fence.
2451 */
2452 if (!IS_I965G(dev)) {
2453 int ret;
2454
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002455 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Chris Wilson52dc7d32009-06-06 09:46:01 +01002456 if (ret != 0)
2457 return ret;
2458 }
2459
Daniel Vetter4a726612010-02-01 13:59:16 +01002460 i915_gem_object_flush_gtt_write_domain(obj);
Chris Wilson52dc7d32009-06-06 09:46:01 +01002461 i915_gem_clear_fence_reg (obj);
2462
2463 return 0;
2464}
2465
2466/**
Eric Anholt673a3942008-07-30 12:06:12 -07002467 * Finds free space in the GTT aperture and binds the object there.
2468 */
2469static int
2470i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment)
2471{
2472 struct drm_device *dev = obj->dev;
2473 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002474 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002475 struct drm_mm_node *free_space;
Chris Wilson4bdadb92010-01-27 13:36:32 +00002476 gfp_t gfpmask = __GFP_NORETRY | __GFP_NOWARN;
Chris Wilson07f73f62009-09-14 16:50:30 +01002477 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002478
Chris Wilsonbb6baf72009-09-22 14:24:13 +01002479 if (obj_priv->madv != I915_MADV_WILLNEED) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01002480 DRM_ERROR("Attempting to bind a purgeable object\n");
2481 return -EINVAL;
2482 }
2483
Eric Anholt673a3942008-07-30 12:06:12 -07002484 if (alignment == 0)
Jesse Barnes0f973f22009-01-26 17:10:45 -08002485 alignment = i915_gem_get_gtt_alignment(obj);
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002486 if (alignment & (i915_gem_get_gtt_alignment(obj) - 1)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002487 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2488 return -EINVAL;
2489 }
2490
Chris Wilson654fc602010-05-27 13:18:21 +01002491 /* If the object is bigger than the entire aperture, reject it early
2492 * before evicting everything in a vain attempt to find space.
2493 */
2494 if (obj->size > dev->gtt_total) {
2495 DRM_ERROR("Attempting to bind an object larger than the aperture\n");
2496 return -E2BIG;
2497 }
2498
Eric Anholt673a3942008-07-30 12:06:12 -07002499 search_free:
2500 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
2501 obj->size, alignment, 0);
2502 if (free_space != NULL) {
2503 obj_priv->gtt_space = drm_mm_get_block(free_space, obj->size,
2504 alignment);
Daniel Vetterdb3307a2010-07-02 15:02:12 +01002505 if (obj_priv->gtt_space != NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002506 obj_priv->gtt_offset = obj_priv->gtt_space->start;
Eric Anholt673a3942008-07-30 12:06:12 -07002507 }
2508 if (obj_priv->gtt_space == NULL) {
2509 /* If the gtt is empty and we're still having trouble
2510 * fitting our object in, we're out of memory.
2511 */
2512#if WATCH_LRU
2513 DRM_INFO("%s: GTT full, evicting something\n", __func__);
2514#endif
Daniel Vetter0108a3e2010-08-07 11:01:21 +01002515 ret = i915_gem_evict_something(dev, obj->size, alignment);
Chris Wilson97311292009-09-21 00:22:34 +01002516 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002517 return ret;
Chris Wilson97311292009-09-21 00:22:34 +01002518
Eric Anholt673a3942008-07-30 12:06:12 -07002519 goto search_free;
2520 }
2521
2522#if WATCH_BUF
Krzysztof Halasacfd43c02009-06-20 00:31:28 +02002523 DRM_INFO("Binding object of size %zd at 0x%08x\n",
Eric Anholt673a3942008-07-30 12:06:12 -07002524 obj->size, obj_priv->gtt_offset);
2525#endif
Chris Wilson4bdadb92010-01-27 13:36:32 +00002526 ret = i915_gem_object_get_pages(obj, gfpmask);
Eric Anholt673a3942008-07-30 12:06:12 -07002527 if (ret) {
2528 drm_mm_put_block(obj_priv->gtt_space);
2529 obj_priv->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002530
2531 if (ret == -ENOMEM) {
2532 /* first try to clear up some space from the GTT */
Daniel Vetter0108a3e2010-08-07 11:01:21 +01002533 ret = i915_gem_evict_something(dev, obj->size,
2534 alignment);
Chris Wilson07f73f62009-09-14 16:50:30 +01002535 if (ret) {
Chris Wilson07f73f62009-09-14 16:50:30 +01002536 /* now try to shrink everyone else */
Chris Wilson4bdadb92010-01-27 13:36:32 +00002537 if (gfpmask) {
2538 gfpmask = 0;
2539 goto search_free;
Chris Wilson07f73f62009-09-14 16:50:30 +01002540 }
2541
2542 return ret;
2543 }
2544
2545 goto search_free;
2546 }
2547
Eric Anholt673a3942008-07-30 12:06:12 -07002548 return ret;
2549 }
2550
Eric Anholt673a3942008-07-30 12:06:12 -07002551 /* Create an AGP memory structure pointing at our pages, and bind it
2552 * into the GTT.
2553 */
2554 obj_priv->agp_mem = drm_agp_bind_pages(dev,
Eric Anholt856fa192009-03-19 14:10:50 -07002555 obj_priv->pages,
Chris Wilson07f73f62009-09-14 16:50:30 +01002556 obj->size >> PAGE_SHIFT,
Keith Packardba1eb1d2008-10-14 19:55:10 -07002557 obj_priv->gtt_offset,
2558 obj_priv->agp_type);
Eric Anholt673a3942008-07-30 12:06:12 -07002559 if (obj_priv->agp_mem == NULL) {
Eric Anholt856fa192009-03-19 14:10:50 -07002560 i915_gem_object_put_pages(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002561 drm_mm_put_block(obj_priv->gtt_space);
2562 obj_priv->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002563
Daniel Vetter0108a3e2010-08-07 11:01:21 +01002564 ret = i915_gem_evict_something(dev, obj->size, alignment);
Chris Wilson97311292009-09-21 00:22:34 +01002565 if (ret)
Chris Wilson07f73f62009-09-14 16:50:30 +01002566 return ret;
Chris Wilson07f73f62009-09-14 16:50:30 +01002567
2568 goto search_free;
Eric Anholt673a3942008-07-30 12:06:12 -07002569 }
2570 atomic_inc(&dev->gtt_count);
2571 atomic_add(obj->size, &dev->gtt_memory);
2572
Chris Wilsonbf1a1092010-08-07 11:01:20 +01002573 /* keep track of bounds object by adding it to the inactive list */
2574 list_add_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
2575
Eric Anholt673a3942008-07-30 12:06:12 -07002576 /* Assert that the object is not currently in any GPU domain. As it
2577 * wasn't in the GTT, there shouldn't be any way it could have been in
2578 * a GPU cache
2579 */
Chris Wilson21d509e2009-06-06 09:46:02 +01002580 BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS);
2581 BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS);
Eric Anholt673a3942008-07-30 12:06:12 -07002582
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002583 trace_i915_gem_object_bind(obj, obj_priv->gtt_offset);
2584
Eric Anholt673a3942008-07-30 12:06:12 -07002585 return 0;
2586}
2587
2588void
2589i915_gem_clflush_object(struct drm_gem_object *obj)
2590{
Daniel Vetter23010e42010-03-08 13:35:02 +01002591 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002592
2593 /* If we don't have a page list set up, then we're not pinned
2594 * to GPU, and we can ignore the cache flush because it'll happen
2595 * again at bind time.
2596 */
Eric Anholt856fa192009-03-19 14:10:50 -07002597 if (obj_priv->pages == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002598 return;
2599
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002600 trace_i915_gem_object_clflush(obj);
Eric Anholtcfa16a02009-05-26 18:46:16 -07002601
Eric Anholt856fa192009-03-19 14:10:50 -07002602 drm_clflush_pages(obj_priv->pages, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002603}
2604
Eric Anholte47c68e2008-11-14 13:35:19 -08002605/** Flushes any GPU write domain for the object if it's dirty. */
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002606static int
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002607i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj,
2608 bool pipelined)
Eric Anholte47c68e2008-11-14 13:35:19 -08002609{
2610 struct drm_device *dev = obj->dev;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002611 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002612
2613 if ((obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002614 return 0;
Eric Anholte47c68e2008-11-14 13:35:19 -08002615
2616 /* Queue the GPU write cache flushing we need. */
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002617 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002618 i915_gem_flush(dev, 0, obj->write_domain);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002619
2620 trace_i915_gem_object_change_domain(obj,
2621 obj->read_domains,
2622 old_write_domain);
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002623
2624 if (pipelined)
2625 return 0;
2626
2627 return i915_gem_object_wait_rendering(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08002628}
2629
2630/** Flushes the GTT write domain for the object if it's dirty. */
2631static void
2632i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj)
2633{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002634 uint32_t old_write_domain;
2635
Eric Anholte47c68e2008-11-14 13:35:19 -08002636 if (obj->write_domain != I915_GEM_DOMAIN_GTT)
2637 return;
2638
2639 /* No actual flushing is required for the GTT write domain. Writes
2640 * to it immediately go to main memory as far as we know, so there's
2641 * no chipset flush. It also doesn't land in render cache.
2642 */
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002643 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002644 obj->write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002645
2646 trace_i915_gem_object_change_domain(obj,
2647 obj->read_domains,
2648 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002649}
2650
2651/** Flushes the CPU write domain for the object if it's dirty. */
2652static void
2653i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj)
2654{
2655 struct drm_device *dev = obj->dev;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002656 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002657
2658 if (obj->write_domain != I915_GEM_DOMAIN_CPU)
2659 return;
2660
2661 i915_gem_clflush_object(obj);
2662 drm_agp_chipset_flush(dev);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002663 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002664 obj->write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002665
2666 trace_i915_gem_object_change_domain(obj,
2667 obj->read_domains,
2668 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002669}
2670
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002671int
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002672i915_gem_object_flush_write_domain(struct drm_gem_object *obj)
2673{
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002674 int ret = 0;
2675
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002676 switch (obj->write_domain) {
2677 case I915_GEM_DOMAIN_GTT:
2678 i915_gem_object_flush_gtt_write_domain(obj);
2679 break;
2680 case I915_GEM_DOMAIN_CPU:
2681 i915_gem_object_flush_cpu_write_domain(obj);
2682 break;
2683 default:
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002684 ret = i915_gem_object_flush_gpu_write_domain(obj, true);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002685 break;
2686 }
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002687
2688 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002689}
2690
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002691/**
2692 * Moves a single object to the GTT read, and possibly write domain.
2693 *
2694 * This function returns when the move is complete, including waiting on
2695 * flushes to occur.
2696 */
Jesse Barnes79e53942008-11-07 14:24:08 -08002697int
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002698i915_gem_object_set_to_gtt_domain(struct drm_gem_object *obj, int write)
2699{
Daniel Vetter23010e42010-03-08 13:35:02 +01002700 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002701 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002702 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002703
Eric Anholt02354392008-11-26 13:58:13 -08002704 /* Not valid to be called on unbound objects. */
2705 if (obj_priv->gtt_space == NULL)
2706 return -EINVAL;
2707
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002708 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Eric Anholte47c68e2008-11-14 13:35:19 -08002709 if (ret != 0)
2710 return ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002711
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002712 old_write_domain = obj->write_domain;
2713 old_read_domains = obj->read_domains;
2714
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002715 /* If we're writing through the GTT domain, then CPU and GPU caches
2716 * will need to be invalidated at next use.
2717 */
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002718 if (write) {
2719 ret = i915_gem_object_wait_rendering(obj);
2720 if (ret)
2721 return ret;
2722
Eric Anholte47c68e2008-11-14 13:35:19 -08002723 obj->read_domains &= I915_GEM_DOMAIN_GTT;
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002724 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002725
Eric Anholte47c68e2008-11-14 13:35:19 -08002726 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002727
2728 /* It should now be out of any other write domains, and we can update
2729 * the domain values for our changes.
2730 */
2731 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2732 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002733 if (write) {
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002734 obj->write_domain = I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002735 obj_priv->dirty = 1;
2736 }
2737
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002738 trace_i915_gem_object_change_domain(obj,
2739 old_read_domains,
2740 old_write_domain);
2741
Eric Anholte47c68e2008-11-14 13:35:19 -08002742 return 0;
2743}
2744
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002745/*
2746 * Prepare buffer for display plane. Use uninterruptible for possible flush
2747 * wait, as in modesetting process we're not supposed to be interrupted.
2748 */
2749int
2750i915_gem_object_set_to_display_plane(struct drm_gem_object *obj)
2751{
Daniel Vetter23010e42010-03-08 13:35:02 +01002752 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002753 uint32_t old_read_domains;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002754 int ret;
2755
2756 /* Not valid to be called on unbound objects. */
2757 if (obj_priv->gtt_space == NULL)
2758 return -EINVAL;
2759
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002760 ret = i915_gem_object_flush_gpu_write_domain(obj, true);
Daniel Vettere35a41d2010-02-11 22:13:59 +01002761 if (ret != 0)
2762 return ret;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002763
Chris Wilsonb118c1e2010-05-27 13:18:14 +01002764 i915_gem_object_flush_cpu_write_domain(obj);
2765
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002766 old_read_domains = obj->read_domains;
Chris Wilsonb118c1e2010-05-27 13:18:14 +01002767 obj->read_domains = I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002768
2769 trace_i915_gem_object_change_domain(obj,
2770 old_read_domains,
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002771 obj->write_domain);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002772
2773 return 0;
2774}
2775
Eric Anholte47c68e2008-11-14 13:35:19 -08002776/**
2777 * Moves a single object to the CPU read, and possibly write domain.
2778 *
2779 * This function returns when the move is complete, including waiting on
2780 * flushes to occur.
2781 */
2782static int
2783i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj, int write)
2784{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002785 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002786 int ret;
2787
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002788 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Eric Anholte47c68e2008-11-14 13:35:19 -08002789 if (ret != 0)
2790 return ret;
2791
2792 i915_gem_object_flush_gtt_write_domain(obj);
2793
2794 /* If we have a partially-valid cache of the object in the CPU,
2795 * finish invalidating it and free the per-page flags.
2796 */
2797 i915_gem_object_set_to_full_cpu_read_domain(obj);
2798
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002799 old_write_domain = obj->write_domain;
2800 old_read_domains = obj->read_domains;
2801
Eric Anholte47c68e2008-11-14 13:35:19 -08002802 /* Flush the CPU cache if it's still invalid. */
2803 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
2804 i915_gem_clflush_object(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08002805
2806 obj->read_domains |= I915_GEM_DOMAIN_CPU;
2807 }
2808
2809 /* It should now be out of any other write domains, and we can update
2810 * the domain values for our changes.
2811 */
2812 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
2813
2814 /* If we're writing through the CPU, then the GPU read domains will
2815 * need to be invalidated at next use.
2816 */
2817 if (write) {
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002818 ret = i915_gem_object_wait_rendering(obj);
2819 if (ret)
2820 return ret;
2821
Eric Anholte47c68e2008-11-14 13:35:19 -08002822 obj->read_domains &= I915_GEM_DOMAIN_CPU;
2823 obj->write_domain = I915_GEM_DOMAIN_CPU;
2824 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002825
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002826 trace_i915_gem_object_change_domain(obj,
2827 old_read_domains,
2828 old_write_domain);
2829
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002830 return 0;
2831}
2832
Eric Anholt673a3942008-07-30 12:06:12 -07002833/*
2834 * Set the next domain for the specified object. This
2835 * may not actually perform the necessary flushing/invaliding though,
2836 * as that may want to be batched with other set_domain operations
2837 *
2838 * This is (we hope) the only really tricky part of gem. The goal
2839 * is fairly simple -- track which caches hold bits of the object
2840 * and make sure they remain coherent. A few concrete examples may
2841 * help to explain how it works. For shorthand, we use the notation
2842 * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
2843 * a pair of read and write domain masks.
2844 *
2845 * Case 1: the batch buffer
2846 *
2847 * 1. Allocated
2848 * 2. Written by CPU
2849 * 3. Mapped to GTT
2850 * 4. Read by GPU
2851 * 5. Unmapped from GTT
2852 * 6. Freed
2853 *
2854 * Let's take these a step at a time
2855 *
2856 * 1. Allocated
2857 * Pages allocated from the kernel may still have
2858 * cache contents, so we set them to (CPU, CPU) always.
2859 * 2. Written by CPU (using pwrite)
2860 * The pwrite function calls set_domain (CPU, CPU) and
2861 * this function does nothing (as nothing changes)
2862 * 3. Mapped by GTT
2863 * This function asserts that the object is not
2864 * currently in any GPU-based read or write domains
2865 * 4. Read by GPU
2866 * i915_gem_execbuffer calls set_domain (COMMAND, 0).
2867 * As write_domain is zero, this function adds in the
2868 * current read domains (CPU+COMMAND, 0).
2869 * flush_domains is set to CPU.
2870 * invalidate_domains is set to COMMAND
2871 * clflush is run to get data out of the CPU caches
2872 * then i915_dev_set_domain calls i915_gem_flush to
2873 * emit an MI_FLUSH and drm_agp_chipset_flush
2874 * 5. Unmapped from GTT
2875 * i915_gem_object_unbind calls set_domain (CPU, CPU)
2876 * flush_domains and invalidate_domains end up both zero
2877 * so no flushing/invalidating happens
2878 * 6. Freed
2879 * yay, done
2880 *
2881 * Case 2: The shared render buffer
2882 *
2883 * 1. Allocated
2884 * 2. Mapped to GTT
2885 * 3. Read/written by GPU
2886 * 4. set_domain to (CPU,CPU)
2887 * 5. Read/written by CPU
2888 * 6. Read/written by GPU
2889 *
2890 * 1. Allocated
2891 * Same as last example, (CPU, CPU)
2892 * 2. Mapped to GTT
2893 * Nothing changes (assertions find that it is not in the GPU)
2894 * 3. Read/written by GPU
2895 * execbuffer calls set_domain (RENDER, RENDER)
2896 * flush_domains gets CPU
2897 * invalidate_domains gets GPU
2898 * clflush (obj)
2899 * MI_FLUSH and drm_agp_chipset_flush
2900 * 4. set_domain (CPU, CPU)
2901 * flush_domains gets GPU
2902 * invalidate_domains gets CPU
2903 * wait_rendering (obj) to make sure all drawing is complete.
2904 * This will include an MI_FLUSH to get the data from GPU
2905 * to memory
2906 * clflush (obj) to invalidate the CPU cache
2907 * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
2908 * 5. Read/written by CPU
2909 * cache lines are loaded and dirtied
2910 * 6. Read written by GPU
2911 * Same as last GPU access
2912 *
2913 * Case 3: The constant buffer
2914 *
2915 * 1. Allocated
2916 * 2. Written by CPU
2917 * 3. Read by GPU
2918 * 4. Updated (written) by CPU again
2919 * 5. Read by GPU
2920 *
2921 * 1. Allocated
2922 * (CPU, CPU)
2923 * 2. Written by CPU
2924 * (CPU, CPU)
2925 * 3. Read by GPU
2926 * (CPU+RENDER, 0)
2927 * flush_domains = CPU
2928 * invalidate_domains = RENDER
2929 * clflush (obj)
2930 * MI_FLUSH
2931 * drm_agp_chipset_flush
2932 * 4. Updated (written) by CPU again
2933 * (CPU, CPU)
2934 * flush_domains = 0 (no previous write domain)
2935 * invalidate_domains = 0 (no new read domains)
2936 * 5. Read by GPU
2937 * (CPU+RENDER, 0)
2938 * flush_domains = CPU
2939 * invalidate_domains = RENDER
2940 * clflush (obj)
2941 * MI_FLUSH
2942 * drm_agp_chipset_flush
2943 */
Keith Packardc0d90822008-11-20 23:11:08 -08002944static void
Eric Anholt8b0e3782009-02-19 14:40:50 -08002945i915_gem_object_set_to_gpu_domain(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002946{
2947 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01002948 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002949 uint32_t invalidate_domains = 0;
2950 uint32_t flush_domains = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002951 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002952
Eric Anholt8b0e3782009-02-19 14:40:50 -08002953 BUG_ON(obj->pending_read_domains & I915_GEM_DOMAIN_CPU);
2954 BUG_ON(obj->pending_write_domain == I915_GEM_DOMAIN_CPU);
Eric Anholt673a3942008-07-30 12:06:12 -07002955
Jesse Barnes652c3932009-08-17 13:31:43 -07002956 intel_mark_busy(dev, obj);
2957
Eric Anholt673a3942008-07-30 12:06:12 -07002958#if WATCH_BUF
2959 DRM_INFO("%s: object %p read %08x -> %08x write %08x -> %08x\n",
2960 __func__, obj,
Eric Anholt8b0e3782009-02-19 14:40:50 -08002961 obj->read_domains, obj->pending_read_domains,
2962 obj->write_domain, obj->pending_write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07002963#endif
2964 /*
2965 * If the object isn't moving to a new write domain,
2966 * let the object stay in multiple read domains
2967 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08002968 if (obj->pending_write_domain == 0)
2969 obj->pending_read_domains |= obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07002970 else
2971 obj_priv->dirty = 1;
2972
2973 /*
2974 * Flush the current write domain if
2975 * the new read domains don't match. Invalidate
2976 * any read domains which differ from the old
2977 * write domain
2978 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08002979 if (obj->write_domain &&
2980 obj->write_domain != obj->pending_read_domains) {
Eric Anholt673a3942008-07-30 12:06:12 -07002981 flush_domains |= obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08002982 invalidate_domains |=
2983 obj->pending_read_domains & ~obj->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07002984 }
2985 /*
2986 * Invalidate any read caches which may have
2987 * stale data. That is, any new read domains.
2988 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08002989 invalidate_domains |= obj->pending_read_domains & ~obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07002990 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU) {
2991#if WATCH_BUF
2992 DRM_INFO("%s: CPU domain flush %08x invalidate %08x\n",
2993 __func__, flush_domains, invalidate_domains);
2994#endif
Eric Anholt673a3942008-07-30 12:06:12 -07002995 i915_gem_clflush_object(obj);
2996 }
2997
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002998 old_read_domains = obj->read_domains;
2999
Eric Anholtefbeed92009-02-19 14:54:51 -08003000 /* The actual obj->write_domain will be updated with
3001 * pending_write_domain after we emit the accumulated flush for all
3002 * of our domain changes in execbuffers (which clears objects'
3003 * write_domains). So if we have a current write domain that we
3004 * aren't changing, set pending_write_domain to that.
3005 */
3006 if (flush_domains == 0 && obj->pending_write_domain == 0)
3007 obj->pending_write_domain = obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08003008 obj->read_domains = obj->pending_read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07003009
3010 dev->invalidate_domains |= invalidate_domains;
3011 dev->flush_domains |= flush_domains;
3012#if WATCH_BUF
3013 DRM_INFO("%s: read %08x write %08x invalidate %08x flush %08x\n",
3014 __func__,
3015 obj->read_domains, obj->write_domain,
3016 dev->invalidate_domains, dev->flush_domains);
3017#endif
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003018
3019 trace_i915_gem_object_change_domain(obj,
3020 old_read_domains,
3021 obj->write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07003022}
3023
3024/**
Eric Anholte47c68e2008-11-14 13:35:19 -08003025 * Moves the object from a partially CPU read to a full one.
Eric Anholt673a3942008-07-30 12:06:12 -07003026 *
Eric Anholte47c68e2008-11-14 13:35:19 -08003027 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
3028 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
3029 */
3030static void
3031i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj)
3032{
Daniel Vetter23010e42010-03-08 13:35:02 +01003033 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003034
3035 if (!obj_priv->page_cpu_valid)
3036 return;
3037
3038 /* If we're partially in the CPU read domain, finish moving it in.
3039 */
3040 if (obj->read_domains & I915_GEM_DOMAIN_CPU) {
3041 int i;
3042
3043 for (i = 0; i <= (obj->size - 1) / PAGE_SIZE; i++) {
3044 if (obj_priv->page_cpu_valid[i])
3045 continue;
Eric Anholt856fa192009-03-19 14:10:50 -07003046 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholte47c68e2008-11-14 13:35:19 -08003047 }
Eric Anholte47c68e2008-11-14 13:35:19 -08003048 }
3049
3050 /* Free the page_cpu_valid mappings which are now stale, whether
3051 * or not we've got I915_GEM_DOMAIN_CPU.
3052 */
Eric Anholt9a298b22009-03-24 12:23:04 -07003053 kfree(obj_priv->page_cpu_valid);
Eric Anholte47c68e2008-11-14 13:35:19 -08003054 obj_priv->page_cpu_valid = NULL;
3055}
3056
3057/**
3058 * Set the CPU read domain on a range of the object.
3059 *
3060 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
3061 * not entirely valid. The page_cpu_valid member of the object flags which
3062 * pages have been flushed, and will be respected by
3063 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
3064 * of the whole object.
3065 *
3066 * This function returns when the move is complete, including waiting on
3067 * flushes to occur.
Eric Anholt673a3942008-07-30 12:06:12 -07003068 */
3069static int
Eric Anholte47c68e2008-11-14 13:35:19 -08003070i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
3071 uint64_t offset, uint64_t size)
Eric Anholt673a3942008-07-30 12:06:12 -07003072{
Daniel Vetter23010e42010-03-08 13:35:02 +01003073 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003074 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003075 int i, ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003076
Eric Anholte47c68e2008-11-14 13:35:19 -08003077 if (offset == 0 && size == obj->size)
3078 return i915_gem_object_set_to_cpu_domain(obj, 0);
3079
Daniel Vetterba3d8d72010-02-11 22:37:04 +01003080 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Eric Anholte47c68e2008-11-14 13:35:19 -08003081 if (ret != 0)
3082 return ret;
3083 i915_gem_object_flush_gtt_write_domain(obj);
3084
3085 /* If we're already fully in the CPU read domain, we're done. */
3086 if (obj_priv->page_cpu_valid == NULL &&
3087 (obj->read_domains & I915_GEM_DOMAIN_CPU) != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07003088 return 0;
3089
Eric Anholte47c68e2008-11-14 13:35:19 -08003090 /* Otherwise, create/clear the per-page CPU read domain flag if we're
3091 * newly adding I915_GEM_DOMAIN_CPU
3092 */
Eric Anholt673a3942008-07-30 12:06:12 -07003093 if (obj_priv->page_cpu_valid == NULL) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003094 obj_priv->page_cpu_valid = kzalloc(obj->size / PAGE_SIZE,
3095 GFP_KERNEL);
Eric Anholte47c68e2008-11-14 13:35:19 -08003096 if (obj_priv->page_cpu_valid == NULL)
3097 return -ENOMEM;
3098 } else if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0)
3099 memset(obj_priv->page_cpu_valid, 0, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07003100
3101 /* Flush the cache on any pages that are still invalid from the CPU's
3102 * perspective.
3103 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003104 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
3105 i++) {
Eric Anholt673a3942008-07-30 12:06:12 -07003106 if (obj_priv->page_cpu_valid[i])
3107 continue;
3108
Eric Anholt856fa192009-03-19 14:10:50 -07003109 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07003110
3111 obj_priv->page_cpu_valid[i] = 1;
3112 }
3113
Eric Anholte47c68e2008-11-14 13:35:19 -08003114 /* It should now be out of any other write domains, and we can update
3115 * the domain values for our changes.
3116 */
3117 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
3118
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003119 old_read_domains = obj->read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003120 obj->read_domains |= I915_GEM_DOMAIN_CPU;
3121
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003122 trace_i915_gem_object_change_domain(obj,
3123 old_read_domains,
3124 obj->write_domain);
3125
Eric Anholt673a3942008-07-30 12:06:12 -07003126 return 0;
3127}
3128
3129/**
Eric Anholt673a3942008-07-30 12:06:12 -07003130 * Pin an object to the GTT and evaluate the relocations landing in it.
3131 */
3132static int
3133i915_gem_object_pin_and_relocate(struct drm_gem_object *obj,
3134 struct drm_file *file_priv,
Jesse Barnes76446ca2009-12-17 22:05:42 -05003135 struct drm_i915_gem_exec_object2 *entry,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003136 struct drm_i915_gem_relocation_entry *relocs)
Eric Anholt673a3942008-07-30 12:06:12 -07003137{
3138 struct drm_device *dev = obj->dev;
Keith Packard0839ccb2008-10-30 19:38:48 -07003139 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01003140 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003141 int i, ret;
Keith Packard0839ccb2008-10-30 19:38:48 -07003142 void __iomem *reloc_page;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003143 bool need_fence;
3144
3145 need_fence = entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
3146 obj_priv->tiling_mode != I915_TILING_NONE;
3147
3148 /* Check fence reg constraints and rebind if necessary */
Chris Wilson808b24d62010-05-27 13:18:15 +01003149 if (need_fence &&
3150 !i915_gem_object_fence_offset_ok(obj,
3151 obj_priv->tiling_mode)) {
3152 ret = i915_gem_object_unbind(obj);
3153 if (ret)
3154 return ret;
3155 }
Eric Anholt673a3942008-07-30 12:06:12 -07003156
3157 /* Choose the GTT offset for our buffer and put it there. */
3158 ret = i915_gem_object_pin(obj, (uint32_t) entry->alignment);
3159 if (ret)
3160 return ret;
3161
Jesse Barnes76446ca2009-12-17 22:05:42 -05003162 /*
3163 * Pre-965 chips need a fence register set up in order to
3164 * properly handle blits to/from tiled surfaces.
3165 */
3166 if (need_fence) {
3167 ret = i915_gem_object_get_fence_reg(obj);
3168 if (ret != 0) {
Jesse Barnes76446ca2009-12-17 22:05:42 -05003169 i915_gem_object_unpin(obj);
3170 return ret;
3171 }
3172 }
3173
Eric Anholt673a3942008-07-30 12:06:12 -07003174 entry->offset = obj_priv->gtt_offset;
3175
Eric Anholt673a3942008-07-30 12:06:12 -07003176 /* Apply the relocations, using the GTT aperture to avoid cache
3177 * flushing requirements.
3178 */
3179 for (i = 0; i < entry->relocation_count; i++) {
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003180 struct drm_i915_gem_relocation_entry *reloc= &relocs[i];
Eric Anholt673a3942008-07-30 12:06:12 -07003181 struct drm_gem_object *target_obj;
3182 struct drm_i915_gem_object *target_obj_priv;
Eric Anholt3043c602008-10-02 12:24:47 -07003183 uint32_t reloc_val, reloc_offset;
3184 uint32_t __iomem *reloc_entry;
Eric Anholt673a3942008-07-30 12:06:12 -07003185
Eric Anholt673a3942008-07-30 12:06:12 -07003186 target_obj = drm_gem_object_lookup(obj->dev, file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003187 reloc->target_handle);
Eric Anholt673a3942008-07-30 12:06:12 -07003188 if (target_obj == NULL) {
3189 i915_gem_object_unpin(obj);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01003190 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07003191 }
Daniel Vetter23010e42010-03-08 13:35:02 +01003192 target_obj_priv = to_intel_bo(target_obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003193
Chris Wilson8542a0b2009-09-09 21:15:15 +01003194#if WATCH_RELOC
3195 DRM_INFO("%s: obj %p offset %08x target %d "
3196 "read %08x write %08x gtt %08x "
3197 "presumed %08x delta %08x\n",
3198 __func__,
3199 obj,
3200 (int) reloc->offset,
3201 (int) reloc->target_handle,
3202 (int) reloc->read_domains,
3203 (int) reloc->write_domain,
3204 (int) target_obj_priv->gtt_offset,
3205 (int) reloc->presumed_offset,
3206 reloc->delta);
3207#endif
3208
Eric Anholt673a3942008-07-30 12:06:12 -07003209 /* The target buffer should have appeared before us in the
3210 * exec_object list, so it should have a GTT space bound by now.
3211 */
3212 if (target_obj_priv->gtt_space == NULL) {
3213 DRM_ERROR("No GTT space found for object %d\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003214 reloc->target_handle);
Eric Anholt673a3942008-07-30 12:06:12 -07003215 drm_gem_object_unreference(target_obj);
3216 i915_gem_object_unpin(obj);
3217 return -EINVAL;
3218 }
3219
Chris Wilson8542a0b2009-09-09 21:15:15 +01003220 /* Validate that the target is in a valid r/w GPU domain */
Daniel Vetter16edd552010-02-19 11:52:02 +01003221 if (reloc->write_domain & (reloc->write_domain - 1)) {
3222 DRM_ERROR("reloc with multiple write domains: "
3223 "obj %p target %d offset %d "
3224 "read %08x write %08x",
3225 obj, reloc->target_handle,
3226 (int) reloc->offset,
3227 reloc->read_domains,
3228 reloc->write_domain);
3229 return -EINVAL;
3230 }
Chris Wilson8542a0b2009-09-09 21:15:15 +01003231 if (reloc->write_domain & I915_GEM_DOMAIN_CPU ||
3232 reloc->read_domains & I915_GEM_DOMAIN_CPU) {
3233 DRM_ERROR("reloc with read/write CPU domains: "
3234 "obj %p target %d offset %d "
3235 "read %08x write %08x",
3236 obj, reloc->target_handle,
3237 (int) reloc->offset,
3238 reloc->read_domains,
3239 reloc->write_domain);
3240 drm_gem_object_unreference(target_obj);
3241 i915_gem_object_unpin(obj);
3242 return -EINVAL;
3243 }
3244 if (reloc->write_domain && target_obj->pending_write_domain &&
3245 reloc->write_domain != target_obj->pending_write_domain) {
3246 DRM_ERROR("Write domain conflict: "
3247 "obj %p target %d offset %d "
3248 "new %08x old %08x\n",
3249 obj, reloc->target_handle,
3250 (int) reloc->offset,
3251 reloc->write_domain,
3252 target_obj->pending_write_domain);
3253 drm_gem_object_unreference(target_obj);
3254 i915_gem_object_unpin(obj);
3255 return -EINVAL;
3256 }
3257
3258 target_obj->pending_read_domains |= reloc->read_domains;
3259 target_obj->pending_write_domain |= reloc->write_domain;
3260
3261 /* If the relocation already has the right value in it, no
3262 * more work needs to be done.
3263 */
3264 if (target_obj_priv->gtt_offset == reloc->presumed_offset) {
3265 drm_gem_object_unreference(target_obj);
3266 continue;
3267 }
3268
3269 /* Check that the relocation address is valid... */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003270 if (reloc->offset > obj->size - 4) {
Eric Anholt673a3942008-07-30 12:06:12 -07003271 DRM_ERROR("Relocation beyond object bounds: "
3272 "obj %p target %d offset %d size %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003273 obj, reloc->target_handle,
3274 (int) reloc->offset, (int) obj->size);
Eric Anholt673a3942008-07-30 12:06:12 -07003275 drm_gem_object_unreference(target_obj);
3276 i915_gem_object_unpin(obj);
3277 return -EINVAL;
3278 }
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003279 if (reloc->offset & 3) {
Eric Anholt673a3942008-07-30 12:06:12 -07003280 DRM_ERROR("Relocation not 4-byte aligned: "
3281 "obj %p target %d offset %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003282 obj, reloc->target_handle,
3283 (int) reloc->offset);
Eric Anholt673a3942008-07-30 12:06:12 -07003284 drm_gem_object_unreference(target_obj);
3285 i915_gem_object_unpin(obj);
3286 return -EINVAL;
3287 }
3288
Chris Wilson8542a0b2009-09-09 21:15:15 +01003289 /* and points to somewhere within the target object. */
Chris Wilsoncd0b9fb2009-09-15 23:23:18 +01003290 if (reloc->delta >= target_obj->size) {
3291 DRM_ERROR("Relocation beyond target object bounds: "
3292 "obj %p target %d delta %d size %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003293 obj, reloc->target_handle,
Chris Wilsoncd0b9fb2009-09-15 23:23:18 +01003294 (int) reloc->delta, (int) target_obj->size);
Chris Wilson491152b2009-02-11 14:26:32 +00003295 drm_gem_object_unreference(target_obj);
3296 i915_gem_object_unpin(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003297 return -EINVAL;
3298 }
3299
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003300 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
3301 if (ret != 0) {
3302 drm_gem_object_unreference(target_obj);
3303 i915_gem_object_unpin(obj);
3304 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -07003305 }
3306
3307 /* Map the page containing the relocation we're going to
3308 * perform.
3309 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003310 reloc_offset = obj_priv->gtt_offset + reloc->offset;
Keith Packard0839ccb2008-10-30 19:38:48 -07003311 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
3312 (reloc_offset &
Chris Wilsonfca3ec02010-08-04 14:34:24 +01003313 ~(PAGE_SIZE - 1)),
3314 KM_USER0);
Eric Anholt3043c602008-10-02 12:24:47 -07003315 reloc_entry = (uint32_t __iomem *)(reloc_page +
Keith Packard0839ccb2008-10-30 19:38:48 -07003316 (reloc_offset & (PAGE_SIZE - 1)));
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003317 reloc_val = target_obj_priv->gtt_offset + reloc->delta;
Eric Anholt673a3942008-07-30 12:06:12 -07003318
3319#if WATCH_BUF
3320 DRM_INFO("Applied relocation: %p@0x%08x %08x -> %08x\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003321 obj, (unsigned int) reloc->offset,
Eric Anholt673a3942008-07-30 12:06:12 -07003322 readl(reloc_entry), reloc_val);
3323#endif
3324 writel(reloc_val, reloc_entry);
Chris Wilsonfca3ec02010-08-04 14:34:24 +01003325 io_mapping_unmap_atomic(reloc_page, KM_USER0);
Eric Anholt673a3942008-07-30 12:06:12 -07003326
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003327 /* The updated presumed offset for this entry will be
3328 * copied back out to the user.
Eric Anholt673a3942008-07-30 12:06:12 -07003329 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003330 reloc->presumed_offset = target_obj_priv->gtt_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003331
3332 drm_gem_object_unreference(target_obj);
3333 }
3334
Eric Anholt673a3942008-07-30 12:06:12 -07003335#if WATCH_BUF
3336 if (0)
3337 i915_gem_dump_object(obj, 128, __func__, ~0);
3338#endif
3339 return 0;
3340}
3341
Eric Anholt673a3942008-07-30 12:06:12 -07003342/* Throttle our rendering by waiting until the ring has completed our requests
3343 * emitted over 20 msec ago.
3344 *
Eric Anholtb9624422009-06-03 07:27:35 +00003345 * Note that if we were to use the current jiffies each time around the loop,
3346 * we wouldn't escape the function with any frames outstanding if the time to
3347 * render a frame was over 20ms.
3348 *
Eric Anholt673a3942008-07-30 12:06:12 -07003349 * This should get us reasonable parallelism between CPU and GPU but also
3350 * relatively low latency when blocking on a particular request to finish.
3351 */
3352static int
3353i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file_priv)
3354{
3355 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
3356 int ret = 0;
Eric Anholtb9624422009-06-03 07:27:35 +00003357 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
Eric Anholt673a3942008-07-30 12:06:12 -07003358
3359 mutex_lock(&dev->struct_mutex);
Eric Anholtb9624422009-06-03 07:27:35 +00003360 while (!list_empty(&i915_file_priv->mm.request_list)) {
3361 struct drm_i915_gem_request *request;
3362
3363 request = list_first_entry(&i915_file_priv->mm.request_list,
3364 struct drm_i915_gem_request,
3365 client_list);
3366
3367 if (time_after_eq(request->emitted_jiffies, recent_enough))
3368 break;
3369
Zou Nan hai852835f2010-05-21 09:08:56 +08003370 ret = i915_wait_request(dev, request->seqno, request->ring);
Eric Anholtb9624422009-06-03 07:27:35 +00003371 if (ret != 0)
3372 break;
3373 }
Eric Anholt673a3942008-07-30 12:06:12 -07003374 mutex_unlock(&dev->struct_mutex);
Eric Anholtb9624422009-06-03 07:27:35 +00003375
Eric Anholt673a3942008-07-30 12:06:12 -07003376 return ret;
3377}
3378
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003379static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003380i915_gem_get_relocs_from_user(struct drm_i915_gem_exec_object2 *exec_list,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003381 uint32_t buffer_count,
3382 struct drm_i915_gem_relocation_entry **relocs)
3383{
3384 uint32_t reloc_count = 0, reloc_index = 0, i;
3385 int ret;
3386
3387 *relocs = NULL;
3388 for (i = 0; i < buffer_count; i++) {
3389 if (reloc_count + exec_list[i].relocation_count < reloc_count)
3390 return -EINVAL;
3391 reloc_count += exec_list[i].relocation_count;
3392 }
3393
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003394 *relocs = drm_calloc_large(reloc_count, sizeof(**relocs));
Jesse Barnes76446ca2009-12-17 22:05:42 -05003395 if (*relocs == NULL) {
3396 DRM_ERROR("failed to alloc relocs, count %d\n", reloc_count);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003397 return -ENOMEM;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003398 }
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003399
3400 for (i = 0; i < buffer_count; i++) {
3401 struct drm_i915_gem_relocation_entry __user *user_relocs;
3402
3403 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3404
3405 ret = copy_from_user(&(*relocs)[reloc_index],
3406 user_relocs,
3407 exec_list[i].relocation_count *
3408 sizeof(**relocs));
3409 if (ret != 0) {
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003410 drm_free_large(*relocs);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003411 *relocs = NULL;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003412 return -EFAULT;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003413 }
3414
3415 reloc_index += exec_list[i].relocation_count;
3416 }
3417
Florian Mickler2bc43b52009-04-06 22:55:41 +02003418 return 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003419}
3420
3421static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003422i915_gem_put_relocs_to_user(struct drm_i915_gem_exec_object2 *exec_list,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003423 uint32_t buffer_count,
3424 struct drm_i915_gem_relocation_entry *relocs)
3425{
3426 uint32_t reloc_count = 0, i;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003427 int ret = 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003428
Chris Wilson93533c22010-01-31 10:40:48 +00003429 if (relocs == NULL)
3430 return 0;
3431
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003432 for (i = 0; i < buffer_count; i++) {
3433 struct drm_i915_gem_relocation_entry __user *user_relocs;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003434 int unwritten;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003435
3436 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3437
Florian Mickler2bc43b52009-04-06 22:55:41 +02003438 unwritten = copy_to_user(user_relocs,
3439 &relocs[reloc_count],
3440 exec_list[i].relocation_count *
3441 sizeof(*relocs));
3442
3443 if (unwritten) {
3444 ret = -EFAULT;
3445 goto err;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003446 }
3447
3448 reloc_count += exec_list[i].relocation_count;
3449 }
3450
Florian Mickler2bc43b52009-04-06 22:55:41 +02003451err:
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003452 drm_free_large(relocs);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003453
3454 return ret;
3455}
3456
Chris Wilson83d60792009-06-06 09:45:57 +01003457static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003458i915_gem_check_execbuffer (struct drm_i915_gem_execbuffer2 *exec,
Chris Wilson83d60792009-06-06 09:45:57 +01003459 uint64_t exec_offset)
3460{
3461 uint32_t exec_start, exec_len;
3462
3463 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
3464 exec_len = (uint32_t) exec->batch_len;
3465
3466 if ((exec_start | exec_len) & 0x7)
3467 return -EINVAL;
3468
3469 if (!exec_start)
3470 return -EINVAL;
3471
3472 return 0;
3473}
3474
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003475static int
3476i915_gem_wait_for_pending_flip(struct drm_device *dev,
3477 struct drm_gem_object **object_list,
3478 int count)
3479{
3480 drm_i915_private_t *dev_priv = dev->dev_private;
3481 struct drm_i915_gem_object *obj_priv;
3482 DEFINE_WAIT(wait);
3483 int i, ret = 0;
3484
3485 for (;;) {
3486 prepare_to_wait(&dev_priv->pending_flip_queue,
3487 &wait, TASK_INTERRUPTIBLE);
3488 for (i = 0; i < count; i++) {
Daniel Vetter23010e42010-03-08 13:35:02 +01003489 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003490 if (atomic_read(&obj_priv->pending_flip) > 0)
3491 break;
3492 }
3493 if (i == count)
3494 break;
3495
3496 if (!signal_pending(current)) {
3497 mutex_unlock(&dev->struct_mutex);
3498 schedule();
3499 mutex_lock(&dev->struct_mutex);
3500 continue;
3501 }
3502 ret = -ERESTARTSYS;
3503 break;
3504 }
3505 finish_wait(&dev_priv->pending_flip_queue, &wait);
3506
3507 return ret;
3508}
3509
Chris Wilson8dc5d142010-08-12 12:36:12 +01003510static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003511i915_gem_do_execbuffer(struct drm_device *dev, void *data,
3512 struct drm_file *file_priv,
3513 struct drm_i915_gem_execbuffer2 *args,
3514 struct drm_i915_gem_exec_object2 *exec_list)
Eric Anholt673a3942008-07-30 12:06:12 -07003515{
3516 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003517 struct drm_gem_object **object_list = NULL;
3518 struct drm_gem_object *batch_obj;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003519 struct drm_i915_gem_object *obj_priv;
Eric Anholt201361a2009-03-11 12:30:04 -07003520 struct drm_clip_rect *cliprects = NULL;
Chris Wilson93533c22010-01-31 10:40:48 +00003521 struct drm_i915_gem_relocation_entry *relocs = NULL;
Chris Wilson8dc5d142010-08-12 12:36:12 +01003522 struct drm_i915_gem_request *request = NULL;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003523 int ret = 0, ret2, i, pinned = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003524 uint64_t exec_offset;
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01003525 uint32_t seqno, reloc_index;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003526 int pin_tries, flips;
Eric Anholt673a3942008-07-30 12:06:12 -07003527
Zou Nan hai852835f2010-05-21 09:08:56 +08003528 struct intel_ring_buffer *ring = NULL;
3529
Eric Anholt673a3942008-07-30 12:06:12 -07003530#if WATCH_EXEC
3531 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3532 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3533#endif
Zou Nan haid1b851f2010-05-21 09:08:57 +08003534 if (args->flags & I915_EXEC_BSD) {
3535 if (!HAS_BSD(dev)) {
3536 DRM_ERROR("execbuf with wrong flag\n");
3537 return -EINVAL;
3538 }
3539 ring = &dev_priv->bsd_ring;
3540 } else {
3541 ring = &dev_priv->render_ring;
3542 }
3543
Eric Anholt4f481ed2008-09-10 14:22:49 -07003544 if (args->buffer_count < 1) {
3545 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3546 return -EINVAL;
3547 }
Eric Anholtc8e0f932009-11-22 03:49:37 +01003548 object_list = drm_malloc_ab(sizeof(*object_list), args->buffer_count);
Jesse Barnes76446ca2009-12-17 22:05:42 -05003549 if (object_list == NULL) {
3550 DRM_ERROR("Failed to allocate object list for %d buffers\n",
Eric Anholt673a3942008-07-30 12:06:12 -07003551 args->buffer_count);
3552 ret = -ENOMEM;
3553 goto pre_mutex_err;
3554 }
Eric Anholt673a3942008-07-30 12:06:12 -07003555
Eric Anholt201361a2009-03-11 12:30:04 -07003556 if (args->num_cliprects != 0) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003557 cliprects = kcalloc(args->num_cliprects, sizeof(*cliprects),
3558 GFP_KERNEL);
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003559 if (cliprects == NULL) {
3560 ret = -ENOMEM;
Eric Anholt201361a2009-03-11 12:30:04 -07003561 goto pre_mutex_err;
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003562 }
Eric Anholt201361a2009-03-11 12:30:04 -07003563
3564 ret = copy_from_user(cliprects,
3565 (struct drm_clip_rect __user *)
3566 (uintptr_t) args->cliprects_ptr,
3567 sizeof(*cliprects) * args->num_cliprects);
3568 if (ret != 0) {
3569 DRM_ERROR("copy %d cliprects failed: %d\n",
3570 args->num_cliprects, ret);
Dan Carpenterc877cdc2010-06-23 19:03:01 +02003571 ret = -EFAULT;
Eric Anholt201361a2009-03-11 12:30:04 -07003572 goto pre_mutex_err;
3573 }
3574 }
3575
Chris Wilson8dc5d142010-08-12 12:36:12 +01003576 request = kzalloc(sizeof(*request), GFP_KERNEL);
3577 if (request == NULL) {
3578 ret = -ENOMEM;
3579 goto pre_mutex_err;
3580 }
3581
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003582 ret = i915_gem_get_relocs_from_user(exec_list, args->buffer_count,
3583 &relocs);
3584 if (ret != 0)
3585 goto pre_mutex_err;
3586
Eric Anholt673a3942008-07-30 12:06:12 -07003587 mutex_lock(&dev->struct_mutex);
3588
3589 i915_verify_inactive(dev, __FILE__, __LINE__);
3590
Ben Gamariba1234d2009-09-14 17:48:47 -04003591 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07003592 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003593 ret = -EIO;
3594 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003595 }
3596
3597 if (dev_priv->mm.suspended) {
Eric Anholt673a3942008-07-30 12:06:12 -07003598 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003599 ret = -EBUSY;
3600 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003601 }
3602
Keith Packardac94a962008-11-20 23:30:27 -08003603 /* Look up object handles */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003604 flips = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003605 for (i = 0; i < args->buffer_count; i++) {
3606 object_list[i] = drm_gem_object_lookup(dev, file_priv,
3607 exec_list[i].handle);
3608 if (object_list[i] == NULL) {
3609 DRM_ERROR("Invalid object handle %d at index %d\n",
3610 exec_list[i].handle, i);
Chris Wilson0ce907f2010-01-23 20:26:35 +00003611 /* prevent error path from reading uninitialized data */
3612 args->buffer_count = i + 1;
Chris Wilsonbf79cb92010-08-04 14:19:46 +01003613 ret = -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07003614 goto err;
3615 }
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003616
Daniel Vetter23010e42010-03-08 13:35:02 +01003617 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003618 if (obj_priv->in_execbuffer) {
3619 DRM_ERROR("Object %p appears more than once in object list\n",
3620 object_list[i]);
Chris Wilson0ce907f2010-01-23 20:26:35 +00003621 /* prevent error path from reading uninitialized data */
3622 args->buffer_count = i + 1;
Chris Wilsonbf79cb92010-08-04 14:19:46 +01003623 ret = -EINVAL;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003624 goto err;
3625 }
3626 obj_priv->in_execbuffer = true;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003627 flips += atomic_read(&obj_priv->pending_flip);
3628 }
3629
3630 if (flips > 0) {
3631 ret = i915_gem_wait_for_pending_flip(dev, object_list,
3632 args->buffer_count);
3633 if (ret)
3634 goto err;
Keith Packardac94a962008-11-20 23:30:27 -08003635 }
Eric Anholt673a3942008-07-30 12:06:12 -07003636
Keith Packardac94a962008-11-20 23:30:27 -08003637 /* Pin and relocate */
3638 for (pin_tries = 0; ; pin_tries++) {
3639 ret = 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003640 reloc_index = 0;
3641
Keith Packardac94a962008-11-20 23:30:27 -08003642 for (i = 0; i < args->buffer_count; i++) {
3643 object_list[i]->pending_read_domains = 0;
3644 object_list[i]->pending_write_domain = 0;
3645 ret = i915_gem_object_pin_and_relocate(object_list[i],
3646 file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003647 &exec_list[i],
3648 &relocs[reloc_index]);
Keith Packardac94a962008-11-20 23:30:27 -08003649 if (ret)
3650 break;
3651 pinned = i + 1;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003652 reloc_index += exec_list[i].relocation_count;
Keith Packardac94a962008-11-20 23:30:27 -08003653 }
3654 /* success */
3655 if (ret == 0)
3656 break;
3657
3658 /* error other than GTT full, or we've already tried again */
Chris Wilson2939e1f2009-06-06 09:46:03 +01003659 if (ret != -ENOSPC || pin_tries >= 1) {
Chris Wilson07f73f62009-09-14 16:50:30 +01003660 if (ret != -ERESTARTSYS) {
3661 unsigned long long total_size = 0;
Chris Wilson3d1cc472010-05-27 13:18:19 +01003662 int num_fences = 0;
3663 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson43b27f42010-07-02 08:57:15 +01003664 obj_priv = to_intel_bo(object_list[i]);
Chris Wilson3d1cc472010-05-27 13:18:19 +01003665
Chris Wilson07f73f62009-09-14 16:50:30 +01003666 total_size += object_list[i]->size;
Chris Wilson3d1cc472010-05-27 13:18:19 +01003667 num_fences +=
3668 exec_list[i].flags & EXEC_OBJECT_NEEDS_FENCE &&
3669 obj_priv->tiling_mode != I915_TILING_NONE;
3670 }
3671 DRM_ERROR("Failed to pin buffer %d of %d, total %llu bytes, %d fences: %d\n",
Chris Wilson07f73f62009-09-14 16:50:30 +01003672 pinned+1, args->buffer_count,
Chris Wilson3d1cc472010-05-27 13:18:19 +01003673 total_size, num_fences,
3674 ret);
Chris Wilson07f73f62009-09-14 16:50:30 +01003675 DRM_ERROR("%d objects [%d pinned], "
3676 "%d object bytes [%d pinned], "
3677 "%d/%d gtt bytes\n",
3678 atomic_read(&dev->object_count),
3679 atomic_read(&dev->pin_count),
3680 atomic_read(&dev->object_memory),
3681 atomic_read(&dev->pin_memory),
3682 atomic_read(&dev->gtt_memory),
3683 dev->gtt_total);
3684 }
Eric Anholt673a3942008-07-30 12:06:12 -07003685 goto err;
3686 }
Keith Packardac94a962008-11-20 23:30:27 -08003687
3688 /* unpin all of our buffers */
3689 for (i = 0; i < pinned; i++)
3690 i915_gem_object_unpin(object_list[i]);
Eric Anholtb1177632008-12-10 10:09:41 -08003691 pinned = 0;
Keith Packardac94a962008-11-20 23:30:27 -08003692
3693 /* evict everyone we can from the aperture */
3694 ret = i915_gem_evict_everything(dev);
Chris Wilson07f73f62009-09-14 16:50:30 +01003695 if (ret && ret != -ENOSPC)
Keith Packardac94a962008-11-20 23:30:27 -08003696 goto err;
Eric Anholt673a3942008-07-30 12:06:12 -07003697 }
3698
3699 /* Set the pending read domains for the batch buffer to COMMAND */
3700 batch_obj = object_list[args->buffer_count-1];
Chris Wilson5f26a2c2009-06-06 09:45:58 +01003701 if (batch_obj->pending_write_domain) {
3702 DRM_ERROR("Attempting to use self-modifying batch buffer\n");
3703 ret = -EINVAL;
3704 goto err;
3705 }
3706 batch_obj->pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
Eric Anholt673a3942008-07-30 12:06:12 -07003707
Chris Wilson83d60792009-06-06 09:45:57 +01003708 /* Sanity check the batch buffer, prior to moving objects */
3709 exec_offset = exec_list[args->buffer_count - 1].offset;
3710 ret = i915_gem_check_execbuffer (args, exec_offset);
3711 if (ret != 0) {
3712 DRM_ERROR("execbuf with invalid offset/length\n");
3713 goto err;
3714 }
3715
Eric Anholt673a3942008-07-30 12:06:12 -07003716 i915_verify_inactive(dev, __FILE__, __LINE__);
3717
Keith Packard646f0f62008-11-20 23:23:03 -08003718 /* Zero the global flush/invalidate flags. These
3719 * will be modified as new domains are computed
3720 * for each object
3721 */
3722 dev->invalidate_domains = 0;
3723 dev->flush_domains = 0;
3724
Eric Anholt673a3942008-07-30 12:06:12 -07003725 for (i = 0; i < args->buffer_count; i++) {
3726 struct drm_gem_object *obj = object_list[i];
Eric Anholt673a3942008-07-30 12:06:12 -07003727
Keith Packard646f0f62008-11-20 23:23:03 -08003728 /* Compute new gpu domains and update invalidate/flush */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003729 i915_gem_object_set_to_gpu_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003730 }
3731
3732 i915_verify_inactive(dev, __FILE__, __LINE__);
3733
Keith Packard646f0f62008-11-20 23:23:03 -08003734 if (dev->invalidate_domains | dev->flush_domains) {
3735#if WATCH_EXEC
3736 DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
3737 __func__,
3738 dev->invalidate_domains,
3739 dev->flush_domains);
3740#endif
3741 i915_gem_flush(dev,
3742 dev->invalidate_domains,
3743 dev->flush_domains);
Daniel Vettera6910432010-02-02 17:08:37 +01003744 }
3745
3746 if (dev_priv->render_ring.outstanding_lazy_request) {
Chris Wilson8dc5d142010-08-12 12:36:12 +01003747 (void)i915_add_request(dev, file_priv, NULL, &dev_priv->render_ring);
Daniel Vettera6910432010-02-02 17:08:37 +01003748 dev_priv->render_ring.outstanding_lazy_request = false;
3749 }
3750 if (dev_priv->bsd_ring.outstanding_lazy_request) {
Chris Wilson8dc5d142010-08-12 12:36:12 +01003751 (void)i915_add_request(dev, file_priv, NULL, &dev_priv->bsd_ring);
Daniel Vettera6910432010-02-02 17:08:37 +01003752 dev_priv->bsd_ring.outstanding_lazy_request = false;
Keith Packard646f0f62008-11-20 23:23:03 -08003753 }
Eric Anholt673a3942008-07-30 12:06:12 -07003754
Eric Anholtefbeed92009-02-19 14:54:51 -08003755 for (i = 0; i < args->buffer_count; i++) {
3756 struct drm_gem_object *obj = object_list[i];
Daniel Vetter23010e42010-03-08 13:35:02 +01003757 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003758 uint32_t old_write_domain = obj->write_domain;
Eric Anholtefbeed92009-02-19 14:54:51 -08003759
3760 obj->write_domain = obj->pending_write_domain;
Daniel Vetter99fcb762010-02-07 16:20:18 +01003761 if (obj->write_domain)
3762 list_move_tail(&obj_priv->gpu_write_list,
3763 &dev_priv->mm.gpu_write_list);
3764 else
3765 list_del_init(&obj_priv->gpu_write_list);
3766
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003767 trace_i915_gem_object_change_domain(obj,
3768 obj->read_domains,
3769 old_write_domain);
Eric Anholtefbeed92009-02-19 14:54:51 -08003770 }
3771
Eric Anholt673a3942008-07-30 12:06:12 -07003772 i915_verify_inactive(dev, __FILE__, __LINE__);
3773
3774#if WATCH_COHERENCY
3775 for (i = 0; i < args->buffer_count; i++) {
3776 i915_gem_object_check_coherency(object_list[i],
3777 exec_list[i].handle);
3778 }
3779#endif
3780
Eric Anholt673a3942008-07-30 12:06:12 -07003781#if WATCH_EXEC
Ben Gamari6911a9b2009-04-02 11:24:54 -07003782 i915_gem_dump_object(batch_obj,
Eric Anholt673a3942008-07-30 12:06:12 -07003783 args->batch_len,
3784 __func__,
3785 ~0);
3786#endif
3787
Eric Anholt673a3942008-07-30 12:06:12 -07003788 /* Exec the batchbuffer */
Zou Nan hai852835f2010-05-21 09:08:56 +08003789 ret = ring->dispatch_gem_execbuffer(dev, ring, args,
3790 cliprects, exec_offset);
Eric Anholt673a3942008-07-30 12:06:12 -07003791 if (ret) {
3792 DRM_ERROR("dispatch failed %d\n", ret);
3793 goto err;
3794 }
3795
3796 /*
3797 * Ensure that the commands in the batch buffer are
3798 * finished before the interrupt fires
3799 */
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01003800 i915_retire_commands(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07003801
3802 i915_verify_inactive(dev, __FILE__, __LINE__);
3803
Daniel Vetter617dbe22010-02-11 22:16:02 +01003804 for (i = 0; i < args->buffer_count; i++) {
3805 struct drm_gem_object *obj = object_list[i];
3806 obj_priv = to_intel_bo(obj);
3807
3808 i915_gem_object_move_to_active(obj, ring);
3809#if WATCH_LRU
3810 DRM_INFO("%s: move to exec list %p\n", __func__, obj);
3811#endif
3812 }
3813
Eric Anholt673a3942008-07-30 12:06:12 -07003814 /*
3815 * Get a seqno representing the execution of the current buffer,
3816 * which we can wait on. We would like to mitigate these interrupts,
3817 * likely by only creating seqnos occasionally (so that we have
3818 * *some* interrupts representing completion of buffers that we can
3819 * wait on when trying to clear up gtt space).
3820 */
Chris Wilson8dc5d142010-08-12 12:36:12 +01003821 seqno = i915_add_request(dev, file_priv, request, ring);
3822 request = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07003823
Eric Anholt673a3942008-07-30 12:06:12 -07003824#if WATCH_LRU
3825 i915_dump_lru(dev, __func__);
3826#endif
3827
3828 i915_verify_inactive(dev, __FILE__, __LINE__);
3829
Eric Anholt673a3942008-07-30 12:06:12 -07003830err:
Julia Lawallaad87df2008-12-21 16:28:47 +01003831 for (i = 0; i < pinned; i++)
3832 i915_gem_object_unpin(object_list[i]);
Eric Anholt673a3942008-07-30 12:06:12 -07003833
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003834 for (i = 0; i < args->buffer_count; i++) {
3835 if (object_list[i]) {
Daniel Vetter23010e42010-03-08 13:35:02 +01003836 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003837 obj_priv->in_execbuffer = false;
3838 }
Julia Lawallaad87df2008-12-21 16:28:47 +01003839 drm_gem_object_unreference(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003840 }
Julia Lawallaad87df2008-12-21 16:28:47 +01003841
Eric Anholt673a3942008-07-30 12:06:12 -07003842 mutex_unlock(&dev->struct_mutex);
3843
Chris Wilson93533c22010-01-31 10:40:48 +00003844pre_mutex_err:
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003845 /* Copy the updated relocations out regardless of current error
3846 * state. Failure to update the relocs would mean that the next
3847 * time userland calls execbuf, it would do so with presumed offset
3848 * state that didn't match the actual object state.
3849 */
3850 ret2 = i915_gem_put_relocs_to_user(exec_list, args->buffer_count,
3851 relocs);
3852 if (ret2 != 0) {
3853 DRM_ERROR("Failed to copy relocations back out: %d\n", ret2);
3854
3855 if (ret == 0)
3856 ret = ret2;
3857 }
3858
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003859 drm_free_large(object_list);
Eric Anholt9a298b22009-03-24 12:23:04 -07003860 kfree(cliprects);
Chris Wilson8dc5d142010-08-12 12:36:12 +01003861 kfree(request);
Eric Anholt673a3942008-07-30 12:06:12 -07003862
3863 return ret;
3864}
3865
Jesse Barnes76446ca2009-12-17 22:05:42 -05003866/*
3867 * Legacy execbuffer just creates an exec2 list from the original exec object
3868 * list array and passes it to the real function.
3869 */
3870int
3871i915_gem_execbuffer(struct drm_device *dev, void *data,
3872 struct drm_file *file_priv)
3873{
3874 struct drm_i915_gem_execbuffer *args = data;
3875 struct drm_i915_gem_execbuffer2 exec2;
3876 struct drm_i915_gem_exec_object *exec_list = NULL;
3877 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
3878 int ret, i;
3879
3880#if WATCH_EXEC
3881 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3882 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3883#endif
3884
3885 if (args->buffer_count < 1) {
3886 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3887 return -EINVAL;
3888 }
3889
3890 /* Copy in the exec list from userland */
3891 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
3892 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
3893 if (exec_list == NULL || exec2_list == NULL) {
3894 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
3895 args->buffer_count);
3896 drm_free_large(exec_list);
3897 drm_free_large(exec2_list);
3898 return -ENOMEM;
3899 }
3900 ret = copy_from_user(exec_list,
3901 (struct drm_i915_relocation_entry __user *)
3902 (uintptr_t) args->buffers_ptr,
3903 sizeof(*exec_list) * args->buffer_count);
3904 if (ret != 0) {
3905 DRM_ERROR("copy %d exec entries failed %d\n",
3906 args->buffer_count, ret);
3907 drm_free_large(exec_list);
3908 drm_free_large(exec2_list);
3909 return -EFAULT;
3910 }
3911
3912 for (i = 0; i < args->buffer_count; i++) {
3913 exec2_list[i].handle = exec_list[i].handle;
3914 exec2_list[i].relocation_count = exec_list[i].relocation_count;
3915 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
3916 exec2_list[i].alignment = exec_list[i].alignment;
3917 exec2_list[i].offset = exec_list[i].offset;
3918 if (!IS_I965G(dev))
3919 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
3920 else
3921 exec2_list[i].flags = 0;
3922 }
3923
3924 exec2.buffers_ptr = args->buffers_ptr;
3925 exec2.buffer_count = args->buffer_count;
3926 exec2.batch_start_offset = args->batch_start_offset;
3927 exec2.batch_len = args->batch_len;
3928 exec2.DR1 = args->DR1;
3929 exec2.DR4 = args->DR4;
3930 exec2.num_cliprects = args->num_cliprects;
3931 exec2.cliprects_ptr = args->cliprects_ptr;
Zou Nan hai852835f2010-05-21 09:08:56 +08003932 exec2.flags = I915_EXEC_RENDER;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003933
3934 ret = i915_gem_do_execbuffer(dev, data, file_priv, &exec2, exec2_list);
3935 if (!ret) {
3936 /* Copy the new buffer offsets back to the user's exec list. */
3937 for (i = 0; i < args->buffer_count; i++)
3938 exec_list[i].offset = exec2_list[i].offset;
3939 /* ... and back out to userspace */
3940 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
3941 (uintptr_t) args->buffers_ptr,
3942 exec_list,
3943 sizeof(*exec_list) * args->buffer_count);
3944 if (ret) {
3945 ret = -EFAULT;
3946 DRM_ERROR("failed to copy %d exec entries "
3947 "back to user (%d)\n",
3948 args->buffer_count, ret);
3949 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05003950 }
3951
3952 drm_free_large(exec_list);
3953 drm_free_large(exec2_list);
3954 return ret;
3955}
3956
3957int
3958i915_gem_execbuffer2(struct drm_device *dev, void *data,
3959 struct drm_file *file_priv)
3960{
3961 struct drm_i915_gem_execbuffer2 *args = data;
3962 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
3963 int ret;
3964
3965#if WATCH_EXEC
3966 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3967 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3968#endif
3969
3970 if (args->buffer_count < 1) {
3971 DRM_ERROR("execbuf2 with %d buffers\n", args->buffer_count);
3972 return -EINVAL;
3973 }
3974
3975 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
3976 if (exec2_list == NULL) {
3977 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
3978 args->buffer_count);
3979 return -ENOMEM;
3980 }
3981 ret = copy_from_user(exec2_list,
3982 (struct drm_i915_relocation_entry __user *)
3983 (uintptr_t) args->buffers_ptr,
3984 sizeof(*exec2_list) * args->buffer_count);
3985 if (ret != 0) {
3986 DRM_ERROR("copy %d exec entries failed %d\n",
3987 args->buffer_count, ret);
3988 drm_free_large(exec2_list);
3989 return -EFAULT;
3990 }
3991
3992 ret = i915_gem_do_execbuffer(dev, data, file_priv, args, exec2_list);
3993 if (!ret) {
3994 /* Copy the new buffer offsets back to the user's exec list. */
3995 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
3996 (uintptr_t) args->buffers_ptr,
3997 exec2_list,
3998 sizeof(*exec2_list) * args->buffer_count);
3999 if (ret) {
4000 ret = -EFAULT;
4001 DRM_ERROR("failed to copy %d exec entries "
4002 "back to user (%d)\n",
4003 args->buffer_count, ret);
4004 }
4005 }
4006
4007 drm_free_large(exec2_list);
4008 return ret;
4009}
4010
Eric Anholt673a3942008-07-30 12:06:12 -07004011int
4012i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment)
4013{
4014 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01004015 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004016 int ret;
4017
Daniel Vetter778c3542010-05-13 11:49:44 +02004018 BUG_ON(obj_priv->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT);
4019
Eric Anholt673a3942008-07-30 12:06:12 -07004020 i915_verify_inactive(dev, __FILE__, __LINE__);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01004021
4022 if (obj_priv->gtt_space != NULL) {
4023 if (alignment == 0)
4024 alignment = i915_gem_get_gtt_alignment(obj);
4025 if (obj_priv->gtt_offset & (alignment - 1)) {
Chris Wilsonae7d49d2010-08-04 12:37:41 +01004026 WARN(obj_priv->pin_count,
4027 "bo is already pinned with incorrect alignment:"
4028 " offset=%x, req.alignment=%x\n",
4029 obj_priv->gtt_offset, alignment);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01004030 ret = i915_gem_object_unbind(obj);
4031 if (ret)
4032 return ret;
4033 }
4034 }
4035
Eric Anholt673a3942008-07-30 12:06:12 -07004036 if (obj_priv->gtt_space == NULL) {
4037 ret = i915_gem_object_bind_to_gtt(obj, alignment);
Chris Wilson97311292009-09-21 00:22:34 +01004038 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07004039 return ret;
Chris Wilson22c344e2009-02-11 14:26:45 +00004040 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05004041
Eric Anholt673a3942008-07-30 12:06:12 -07004042 obj_priv->pin_count++;
4043
4044 /* If the object is not active and not pending a flush,
4045 * remove it from the inactive list
4046 */
4047 if (obj_priv->pin_count == 1) {
4048 atomic_inc(&dev->pin_count);
4049 atomic_add(obj->size, &dev->pin_memory);
4050 if (!obj_priv->active &&
Chris Wilsonbf1a1092010-08-07 11:01:20 +01004051 (obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
Eric Anholt673a3942008-07-30 12:06:12 -07004052 list_del_init(&obj_priv->list);
4053 }
4054 i915_verify_inactive(dev, __FILE__, __LINE__);
4055
4056 return 0;
4057}
4058
4059void
4060i915_gem_object_unpin(struct drm_gem_object *obj)
4061{
4062 struct drm_device *dev = obj->dev;
4063 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01004064 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004065
4066 i915_verify_inactive(dev, __FILE__, __LINE__);
4067 obj_priv->pin_count--;
4068 BUG_ON(obj_priv->pin_count < 0);
4069 BUG_ON(obj_priv->gtt_space == NULL);
4070
4071 /* If the object is no longer pinned, and is
4072 * neither active nor being flushed, then stick it on
4073 * the inactive list
4074 */
4075 if (obj_priv->pin_count == 0) {
4076 if (!obj_priv->active &&
Chris Wilson21d509e2009-06-06 09:46:02 +01004077 (obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
Eric Anholt673a3942008-07-30 12:06:12 -07004078 list_move_tail(&obj_priv->list,
4079 &dev_priv->mm.inactive_list);
4080 atomic_dec(&dev->pin_count);
4081 atomic_sub(obj->size, &dev->pin_memory);
4082 }
4083 i915_verify_inactive(dev, __FILE__, __LINE__);
4084}
4085
4086int
4087i915_gem_pin_ioctl(struct drm_device *dev, void *data,
4088 struct drm_file *file_priv)
4089{
4090 struct drm_i915_gem_pin *args = data;
4091 struct drm_gem_object *obj;
4092 struct drm_i915_gem_object *obj_priv;
4093 int ret;
4094
4095 mutex_lock(&dev->struct_mutex);
4096
4097 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4098 if (obj == NULL) {
4099 DRM_ERROR("Bad handle in i915_gem_pin_ioctl(): %d\n",
4100 args->handle);
4101 mutex_unlock(&dev->struct_mutex);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01004102 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07004103 }
Daniel Vetter23010e42010-03-08 13:35:02 +01004104 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004105
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004106 if (obj_priv->madv != I915_MADV_WILLNEED) {
4107 DRM_ERROR("Attempting to pin a purgeable buffer\n");
Chris Wilson3ef94da2009-09-14 16:50:29 +01004108 drm_gem_object_unreference(obj);
4109 mutex_unlock(&dev->struct_mutex);
4110 return -EINVAL;
4111 }
4112
Jesse Barnes79e53942008-11-07 14:24:08 -08004113 if (obj_priv->pin_filp != NULL && obj_priv->pin_filp != file_priv) {
4114 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
4115 args->handle);
Chris Wilson96dec612009-02-08 19:08:04 +00004116 drm_gem_object_unreference(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004117 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08004118 return -EINVAL;
4119 }
4120
4121 obj_priv->user_pin_count++;
4122 obj_priv->pin_filp = file_priv;
4123 if (obj_priv->user_pin_count == 1) {
4124 ret = i915_gem_object_pin(obj, args->alignment);
4125 if (ret != 0) {
4126 drm_gem_object_unreference(obj);
4127 mutex_unlock(&dev->struct_mutex);
4128 return ret;
4129 }
Eric Anholt673a3942008-07-30 12:06:12 -07004130 }
4131
4132 /* XXX - flush the CPU caches for pinned objects
4133 * as the X server doesn't manage domains yet
4134 */
Eric Anholte47c68e2008-11-14 13:35:19 -08004135 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004136 args->offset = obj_priv->gtt_offset;
4137 drm_gem_object_unreference(obj);
4138 mutex_unlock(&dev->struct_mutex);
4139
4140 return 0;
4141}
4142
4143int
4144i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
4145 struct drm_file *file_priv)
4146{
4147 struct drm_i915_gem_pin *args = data;
4148 struct drm_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08004149 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07004150
4151 mutex_lock(&dev->struct_mutex);
4152
4153 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4154 if (obj == NULL) {
4155 DRM_ERROR("Bad handle in i915_gem_unpin_ioctl(): %d\n",
4156 args->handle);
4157 mutex_unlock(&dev->struct_mutex);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01004158 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07004159 }
4160
Daniel Vetter23010e42010-03-08 13:35:02 +01004161 obj_priv = to_intel_bo(obj);
Jesse Barnes79e53942008-11-07 14:24:08 -08004162 if (obj_priv->pin_filp != file_priv) {
4163 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
4164 args->handle);
4165 drm_gem_object_unreference(obj);
4166 mutex_unlock(&dev->struct_mutex);
4167 return -EINVAL;
4168 }
4169 obj_priv->user_pin_count--;
4170 if (obj_priv->user_pin_count == 0) {
4171 obj_priv->pin_filp = NULL;
4172 i915_gem_object_unpin(obj);
4173 }
Eric Anholt673a3942008-07-30 12:06:12 -07004174
4175 drm_gem_object_unreference(obj);
4176 mutex_unlock(&dev->struct_mutex);
4177 return 0;
4178}
4179
4180int
4181i915_gem_busy_ioctl(struct drm_device *dev, void *data,
4182 struct drm_file *file_priv)
4183{
4184 struct drm_i915_gem_busy *args = data;
4185 struct drm_gem_object *obj;
4186 struct drm_i915_gem_object *obj_priv;
4187
Eric Anholt673a3942008-07-30 12:06:12 -07004188 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4189 if (obj == NULL) {
4190 DRM_ERROR("Bad handle in i915_gem_busy_ioctl(): %d\n",
4191 args->handle);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01004192 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07004193 }
4194
Chris Wilsonb1ce7862009-06-06 09:46:00 +01004195 mutex_lock(&dev->struct_mutex);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004196
Chris Wilson0be555b2010-08-04 15:36:30 +01004197 /* Count all active objects as busy, even if they are currently not used
4198 * by the gpu. Users of this interface expect objects to eventually
4199 * become non-busy without any further actions, therefore emit any
4200 * necessary flushes here.
Eric Anholtc4de0a52008-12-14 19:05:04 -08004201 */
Chris Wilson0be555b2010-08-04 15:36:30 +01004202 obj_priv = to_intel_bo(obj);
4203 args->busy = obj_priv->active;
4204 if (args->busy) {
4205 /* Unconditionally flush objects, even when the gpu still uses this
4206 * object. Userspace calling this function indicates that it wants to
4207 * use this buffer rather sooner than later, so issuing the required
4208 * flush earlier is beneficial.
4209 */
4210 if (obj->write_domain) {
4211 i915_gem_flush(dev, 0, obj->write_domain);
Chris Wilson8dc5d142010-08-12 12:36:12 +01004212 (void)i915_add_request(dev, file_priv, NULL, obj_priv->ring);
Chris Wilson0be555b2010-08-04 15:36:30 +01004213 }
4214
4215 /* Update the active list for the hardware's current position.
4216 * Otherwise this only updates on a delayed timer or when irqs
4217 * are actually unmasked, and our working set ends up being
4218 * larger than required.
4219 */
4220 i915_gem_retire_requests_ring(dev, obj_priv->ring);
4221
4222 args->busy = obj_priv->active;
4223 }
Eric Anholt673a3942008-07-30 12:06:12 -07004224
4225 drm_gem_object_unreference(obj);
4226 mutex_unlock(&dev->struct_mutex);
4227 return 0;
4228}
4229
4230int
4231i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4232 struct drm_file *file_priv)
4233{
4234 return i915_gem_ring_throttle(dev, file_priv);
4235}
4236
Chris Wilson3ef94da2009-09-14 16:50:29 +01004237int
4238i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4239 struct drm_file *file_priv)
4240{
4241 struct drm_i915_gem_madvise *args = data;
4242 struct drm_gem_object *obj;
4243 struct drm_i915_gem_object *obj_priv;
4244
4245 switch (args->madv) {
4246 case I915_MADV_DONTNEED:
4247 case I915_MADV_WILLNEED:
4248 break;
4249 default:
4250 return -EINVAL;
4251 }
4252
4253 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4254 if (obj == NULL) {
4255 DRM_ERROR("Bad handle in i915_gem_madvise_ioctl(): %d\n",
4256 args->handle);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01004257 return -ENOENT;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004258 }
4259
4260 mutex_lock(&dev->struct_mutex);
Daniel Vetter23010e42010-03-08 13:35:02 +01004261 obj_priv = to_intel_bo(obj);
Chris Wilson3ef94da2009-09-14 16:50:29 +01004262
4263 if (obj_priv->pin_count) {
4264 drm_gem_object_unreference(obj);
4265 mutex_unlock(&dev->struct_mutex);
4266
4267 DRM_ERROR("Attempted i915_gem_madvise_ioctl() on a pinned object\n");
4268 return -EINVAL;
4269 }
4270
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004271 if (obj_priv->madv != __I915_MADV_PURGED)
4272 obj_priv->madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004273
Chris Wilson2d7ef392009-09-20 23:13:10 +01004274 /* if the object is no longer bound, discard its backing storage */
4275 if (i915_gem_object_is_purgeable(obj_priv) &&
4276 obj_priv->gtt_space == NULL)
4277 i915_gem_object_truncate(obj);
4278
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004279 args->retained = obj_priv->madv != __I915_MADV_PURGED;
4280
Chris Wilson3ef94da2009-09-14 16:50:29 +01004281 drm_gem_object_unreference(obj);
4282 mutex_unlock(&dev->struct_mutex);
4283
4284 return 0;
4285}
4286
Daniel Vetterac52bc52010-04-09 19:05:06 +00004287struct drm_gem_object * i915_gem_alloc_object(struct drm_device *dev,
4288 size_t size)
4289{
Daniel Vetterc397b902010-04-09 19:05:07 +00004290 struct drm_i915_gem_object *obj;
4291
4292 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
4293 if (obj == NULL)
4294 return NULL;
4295
4296 if (drm_gem_object_init(dev, &obj->base, size) != 0) {
4297 kfree(obj);
4298 return NULL;
4299 }
4300
4301 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4302 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4303
4304 obj->agp_type = AGP_USER_MEMORY;
Daniel Vetter62b8b212010-04-09 19:05:08 +00004305 obj->base.driver_private = NULL;
Daniel Vetterc397b902010-04-09 19:05:07 +00004306 obj->fence_reg = I915_FENCE_REG_NONE;
4307 INIT_LIST_HEAD(&obj->list);
4308 INIT_LIST_HEAD(&obj->gpu_write_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00004309 obj->madv = I915_MADV_WILLNEED;
4310
4311 trace_i915_gem_object_create(&obj->base);
4312
4313 return &obj->base;
Daniel Vetterac52bc52010-04-09 19:05:06 +00004314}
4315
Eric Anholt673a3942008-07-30 12:06:12 -07004316int i915_gem_init_object(struct drm_gem_object *obj)
4317{
Daniel Vetterc397b902010-04-09 19:05:07 +00004318 BUG();
Jesse Barnesde151cf2008-11-12 10:03:55 -08004319
Eric Anholt673a3942008-07-30 12:06:12 -07004320 return 0;
4321}
4322
Chris Wilsonbe726152010-07-23 23:18:50 +01004323static void i915_gem_free_object_tail(struct drm_gem_object *obj)
4324{
4325 struct drm_device *dev = obj->dev;
4326 drm_i915_private_t *dev_priv = dev->dev_private;
4327 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
4328 int ret;
4329
4330 ret = i915_gem_object_unbind(obj);
4331 if (ret == -ERESTARTSYS) {
4332 list_move(&obj_priv->list,
4333 &dev_priv->mm.deferred_free_list);
4334 return;
4335 }
4336
4337 if (obj_priv->mmap_offset)
4338 i915_gem_free_mmap_offset(obj);
4339
4340 drm_gem_object_release(obj);
4341
4342 kfree(obj_priv->page_cpu_valid);
4343 kfree(obj_priv->bit_17);
4344 kfree(obj_priv);
4345}
4346
Eric Anholt673a3942008-07-30 12:06:12 -07004347void i915_gem_free_object(struct drm_gem_object *obj)
4348{
Jesse Barnesde151cf2008-11-12 10:03:55 -08004349 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01004350 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004351
Chris Wilson1c5d22f2009-08-25 11:15:50 +01004352 trace_i915_gem_object_destroy(obj);
4353
Eric Anholt673a3942008-07-30 12:06:12 -07004354 while (obj_priv->pin_count > 0)
4355 i915_gem_object_unpin(obj);
4356
Dave Airlie71acb5e2008-12-30 20:31:46 +10004357 if (obj_priv->phys_obj)
4358 i915_gem_detach_phys_object(dev, obj);
4359
Chris Wilsonbe726152010-07-23 23:18:50 +01004360 i915_gem_free_object_tail(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004361}
4362
Jesse Barnes5669fca2009-02-17 15:13:31 -08004363int
Eric Anholt673a3942008-07-30 12:06:12 -07004364i915_gem_idle(struct drm_device *dev)
4365{
4366 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson29105cc2010-01-07 10:39:13 +00004367 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004368
Keith Packard6dbe2772008-10-14 21:41:13 -07004369 mutex_lock(&dev->struct_mutex);
4370
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004371 if (dev_priv->mm.suspended ||
Zou Nan haid1b851f2010-05-21 09:08:57 +08004372 (dev_priv->render_ring.gem_object == NULL) ||
4373 (HAS_BSD(dev) &&
4374 dev_priv->bsd_ring.gem_object == NULL)) {
Keith Packard6dbe2772008-10-14 21:41:13 -07004375 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004376 return 0;
Keith Packard6dbe2772008-10-14 21:41:13 -07004377 }
Eric Anholt673a3942008-07-30 12:06:12 -07004378
Chris Wilson29105cc2010-01-07 10:39:13 +00004379 ret = i915_gpu_idle(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004380 if (ret) {
4381 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004382 return ret;
Keith Packard6dbe2772008-10-14 21:41:13 -07004383 }
Eric Anholt673a3942008-07-30 12:06:12 -07004384
Chris Wilson29105cc2010-01-07 10:39:13 +00004385 /* Under UMS, be paranoid and evict. */
4386 if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
Chris Wilsonb47eb4a2010-08-07 11:01:23 +01004387 ret = i915_gem_evict_inactive(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00004388 if (ret) {
4389 mutex_unlock(&dev->struct_mutex);
4390 return ret;
4391 }
4392 }
4393
4394 /* Hack! Don't let anybody do execbuf while we don't control the chip.
4395 * We need to replace this with a semaphore, or something.
4396 * And not confound mm.suspended!
4397 */
4398 dev_priv->mm.suspended = 1;
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02004399 del_timer_sync(&dev_priv->hangcheck_timer);
Chris Wilson29105cc2010-01-07 10:39:13 +00004400
4401 i915_kernel_lost_context(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004402 i915_gem_cleanup_ringbuffer(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00004403
Keith Packard6dbe2772008-10-14 21:41:13 -07004404 mutex_unlock(&dev->struct_mutex);
4405
Chris Wilson29105cc2010-01-07 10:39:13 +00004406 /* Cancel the retire work handler, which should be idle now. */
4407 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
4408
Eric Anholt673a3942008-07-30 12:06:12 -07004409 return 0;
4410}
4411
Jesse Barnese552eb72010-04-21 11:39:23 -07004412/*
4413 * 965+ support PIPE_CONTROL commands, which provide finer grained control
4414 * over cache flushing.
4415 */
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004416static int
Jesse Barnese552eb72010-04-21 11:39:23 -07004417i915_gem_init_pipe_control(struct drm_device *dev)
4418{
4419 drm_i915_private_t *dev_priv = dev->dev_private;
4420 struct drm_gem_object *obj;
4421 struct drm_i915_gem_object *obj_priv;
4422 int ret;
4423
Eric Anholt34dc4d42010-05-07 14:30:03 -07004424 obj = i915_gem_alloc_object(dev, 4096);
Jesse Barnese552eb72010-04-21 11:39:23 -07004425 if (obj == NULL) {
4426 DRM_ERROR("Failed to allocate seqno page\n");
4427 ret = -ENOMEM;
4428 goto err;
4429 }
4430 obj_priv = to_intel_bo(obj);
4431 obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
4432
4433 ret = i915_gem_object_pin(obj, 4096);
4434 if (ret)
4435 goto err_unref;
4436
4437 dev_priv->seqno_gfx_addr = obj_priv->gtt_offset;
4438 dev_priv->seqno_page = kmap(obj_priv->pages[0]);
4439 if (dev_priv->seqno_page == NULL)
4440 goto err_unpin;
4441
4442 dev_priv->seqno_obj = obj;
4443 memset(dev_priv->seqno_page, 0, PAGE_SIZE);
4444
4445 return 0;
4446
4447err_unpin:
4448 i915_gem_object_unpin(obj);
4449err_unref:
4450 drm_gem_object_unreference(obj);
4451err:
4452 return ret;
4453}
4454
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004455
4456static void
Jesse Barnese552eb72010-04-21 11:39:23 -07004457i915_gem_cleanup_pipe_control(struct drm_device *dev)
4458{
4459 drm_i915_private_t *dev_priv = dev->dev_private;
4460 struct drm_gem_object *obj;
4461 struct drm_i915_gem_object *obj_priv;
4462
4463 obj = dev_priv->seqno_obj;
4464 obj_priv = to_intel_bo(obj);
4465 kunmap(obj_priv->pages[0]);
4466 i915_gem_object_unpin(obj);
4467 drm_gem_object_unreference(obj);
4468 dev_priv->seqno_obj = NULL;
4469
4470 dev_priv->seqno_page = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07004471}
4472
Eric Anholt673a3942008-07-30 12:06:12 -07004473int
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004474i915_gem_init_ringbuffer(struct drm_device *dev)
4475{
4476 drm_i915_private_t *dev_priv = dev->dev_private;
4477 int ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01004478
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004479 dev_priv->render_ring = render_ring;
Chris Wilson68f95ba2010-05-27 13:18:22 +01004480
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004481 if (!I915_NEED_GFX_HWS(dev)) {
4482 dev_priv->render_ring.status_page.page_addr
4483 = dev_priv->status_page_dmah->vaddr;
4484 memset(dev_priv->render_ring.status_page.page_addr,
4485 0, PAGE_SIZE);
4486 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004487
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004488 if (HAS_PIPE_CONTROL(dev)) {
4489 ret = i915_gem_init_pipe_control(dev);
4490 if (ret)
4491 return ret;
4492 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004493
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004494 ret = intel_init_ring_buffer(dev, &dev_priv->render_ring);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004495 if (ret)
4496 goto cleanup_pipe_control;
4497
4498 if (HAS_BSD(dev)) {
Zou Nan haid1b851f2010-05-21 09:08:57 +08004499 dev_priv->bsd_ring = bsd_ring;
4500 ret = intel_init_ring_buffer(dev, &dev_priv->bsd_ring);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004501 if (ret)
4502 goto cleanup_render_ring;
Zou Nan haid1b851f2010-05-21 09:08:57 +08004503 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004504
Chris Wilson6f392d5482010-08-07 11:01:22 +01004505 dev_priv->next_seqno = 1;
4506
Chris Wilson68f95ba2010-05-27 13:18:22 +01004507 return 0;
4508
4509cleanup_render_ring:
4510 intel_cleanup_ring_buffer(dev, &dev_priv->render_ring);
4511cleanup_pipe_control:
4512 if (HAS_PIPE_CONTROL(dev))
4513 i915_gem_cleanup_pipe_control(dev);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004514 return ret;
4515}
4516
4517void
4518i915_gem_cleanup_ringbuffer(struct drm_device *dev)
4519{
4520 drm_i915_private_t *dev_priv = dev->dev_private;
4521
4522 intel_cleanup_ring_buffer(dev, &dev_priv->render_ring);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004523 if (HAS_BSD(dev))
4524 intel_cleanup_ring_buffer(dev, &dev_priv->bsd_ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004525 if (HAS_PIPE_CONTROL(dev))
4526 i915_gem_cleanup_pipe_control(dev);
4527}
4528
4529int
Eric Anholt673a3942008-07-30 12:06:12 -07004530i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
4531 struct drm_file *file_priv)
4532{
4533 drm_i915_private_t *dev_priv = dev->dev_private;
4534 int ret;
4535
Jesse Barnes79e53942008-11-07 14:24:08 -08004536 if (drm_core_check_feature(dev, DRIVER_MODESET))
4537 return 0;
4538
Ben Gamariba1234d2009-09-14 17:48:47 -04004539 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07004540 DRM_ERROR("Reenabling wedged hardware, good luck\n");
Ben Gamariba1234d2009-09-14 17:48:47 -04004541 atomic_set(&dev_priv->mm.wedged, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004542 }
4543
Eric Anholt673a3942008-07-30 12:06:12 -07004544 mutex_lock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004545 dev_priv->mm.suspended = 0;
4546
4547 ret = i915_gem_init_ringbuffer(dev);
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004548 if (ret != 0) {
4549 mutex_unlock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004550 return ret;
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004551 }
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004552
Carl Worth5e118f42009-03-20 11:54:25 -07004553 spin_lock(&dev_priv->mm.active_list_lock);
Zou Nan hai852835f2010-05-21 09:08:56 +08004554 BUG_ON(!list_empty(&dev_priv->render_ring.active_list));
Zou Nan haid1b851f2010-05-21 09:08:57 +08004555 BUG_ON(HAS_BSD(dev) && !list_empty(&dev_priv->bsd_ring.active_list));
Carl Worth5e118f42009-03-20 11:54:25 -07004556 spin_unlock(&dev_priv->mm.active_list_lock);
4557
Eric Anholt673a3942008-07-30 12:06:12 -07004558 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
4559 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
Zou Nan hai852835f2010-05-21 09:08:56 +08004560 BUG_ON(!list_empty(&dev_priv->render_ring.request_list));
Zou Nan haid1b851f2010-05-21 09:08:57 +08004561 BUG_ON(HAS_BSD(dev) && !list_empty(&dev_priv->bsd_ring.request_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004562 mutex_unlock(&dev->struct_mutex);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004563
Chris Wilson5f353082010-06-07 14:03:03 +01004564 ret = drm_irq_install(dev);
4565 if (ret)
4566 goto cleanup_ringbuffer;
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004567
Eric Anholt673a3942008-07-30 12:06:12 -07004568 return 0;
Chris Wilson5f353082010-06-07 14:03:03 +01004569
4570cleanup_ringbuffer:
4571 mutex_lock(&dev->struct_mutex);
4572 i915_gem_cleanup_ringbuffer(dev);
4573 dev_priv->mm.suspended = 1;
4574 mutex_unlock(&dev->struct_mutex);
4575
4576 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004577}
4578
4579int
4580i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
4581 struct drm_file *file_priv)
4582{
Jesse Barnes79e53942008-11-07 14:24:08 -08004583 if (drm_core_check_feature(dev, DRIVER_MODESET))
4584 return 0;
4585
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004586 drm_irq_uninstall(dev);
Linus Torvaldse6890f62009-09-08 17:09:24 -07004587 return i915_gem_idle(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004588}
4589
4590void
4591i915_gem_lastclose(struct drm_device *dev)
4592{
4593 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004594
Eric Anholte806b492009-01-22 09:56:58 -08004595 if (drm_core_check_feature(dev, DRIVER_MODESET))
4596 return;
4597
Keith Packard6dbe2772008-10-14 21:41:13 -07004598 ret = i915_gem_idle(dev);
4599 if (ret)
4600 DRM_ERROR("failed to idle hardware: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07004601}
4602
4603void
4604i915_gem_load(struct drm_device *dev)
4605{
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004606 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07004607 drm_i915_private_t *dev_priv = dev->dev_private;
4608
Carl Worth5e118f42009-03-20 11:54:25 -07004609 spin_lock_init(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07004610 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
Daniel Vetter99fcb762010-02-07 16:20:18 +01004611 INIT_LIST_HEAD(&dev_priv->mm.gpu_write_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004612 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07004613 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Chris Wilsonbe726152010-07-23 23:18:50 +01004614 INIT_LIST_HEAD(&dev_priv->mm.deferred_free_list);
Zou Nan hai852835f2010-05-21 09:08:56 +08004615 INIT_LIST_HEAD(&dev_priv->render_ring.active_list);
4616 INIT_LIST_HEAD(&dev_priv->render_ring.request_list);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004617 if (HAS_BSD(dev)) {
4618 INIT_LIST_HEAD(&dev_priv->bsd_ring.active_list);
4619 INIT_LIST_HEAD(&dev_priv->bsd_ring.request_list);
4620 }
Daniel Vetter007cc8a2010-04-28 11:02:31 +02004621 for (i = 0; i < 16; i++)
4622 INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004623 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
4624 i915_gem_retire_work_handler);
Chris Wilson31169712009-09-14 16:50:28 +01004625 spin_lock(&shrink_list_lock);
4626 list_add(&dev_priv->mm.shrink_list, &shrink_list);
4627 spin_unlock(&shrink_list_lock);
4628
Dave Airlie94400122010-07-20 13:15:31 +10004629 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
4630 if (IS_GEN3(dev)) {
4631 u32 tmp = I915_READ(MI_ARB_STATE);
4632 if (!(tmp & MI_ARB_C3_LP_WRITE_ENABLE)) {
4633 /* arb state is a masked write, so set bit + bit in mask */
4634 tmp = MI_ARB_C3_LP_WRITE_ENABLE | (MI_ARB_C3_LP_WRITE_ENABLE << MI_ARB_MASK_SHIFT);
4635 I915_WRITE(MI_ARB_STATE, tmp);
4636 }
4637 }
4638
Jesse Barnesde151cf2008-11-12 10:03:55 -08004639 /* Old X drivers will take 0-2 for front, back, depth buffers */
Eric Anholtb397c832010-01-26 09:43:10 -08004640 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4641 dev_priv->fence_reg_start = 3;
Jesse Barnesde151cf2008-11-12 10:03:55 -08004642
Jesse Barnes0f973f22009-01-26 17:10:45 -08004643 if (IS_I965G(dev) || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08004644 dev_priv->num_fence_regs = 16;
4645 else
4646 dev_priv->num_fence_regs = 8;
4647
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004648 /* Initialize fence registers to zero */
4649 if (IS_I965G(dev)) {
4650 for (i = 0; i < 16; i++)
4651 I915_WRITE64(FENCE_REG_965_0 + (i * 8), 0);
4652 } else {
4653 for (i = 0; i < 8; i++)
4654 I915_WRITE(FENCE_REG_830_0 + (i * 4), 0);
4655 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4656 for (i = 0; i < 8; i++)
4657 I915_WRITE(FENCE_REG_945_8 + (i * 4), 0);
4658 }
Eric Anholt673a3942008-07-30 12:06:12 -07004659 i915_gem_detect_bit_6_swizzle(dev);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05004660 init_waitqueue_head(&dev_priv->pending_flip_queue);
Eric Anholt673a3942008-07-30 12:06:12 -07004661}
Dave Airlie71acb5e2008-12-30 20:31:46 +10004662
4663/*
4664 * Create a physically contiguous memory object for this object
4665 * e.g. for cursor + overlay regs
4666 */
4667int i915_gem_init_phys_object(struct drm_device *dev,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004668 int id, int size, int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004669{
4670 drm_i915_private_t *dev_priv = dev->dev_private;
4671 struct drm_i915_gem_phys_object *phys_obj;
4672 int ret;
4673
4674 if (dev_priv->mm.phys_objs[id - 1] || !size)
4675 return 0;
4676
Eric Anholt9a298b22009-03-24 12:23:04 -07004677 phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004678 if (!phys_obj)
4679 return -ENOMEM;
4680
4681 phys_obj->id = id;
4682
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004683 phys_obj->handle = drm_pci_alloc(dev, size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004684 if (!phys_obj->handle) {
4685 ret = -ENOMEM;
4686 goto kfree_obj;
4687 }
4688#ifdef CONFIG_X86
4689 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4690#endif
4691
4692 dev_priv->mm.phys_objs[id - 1] = phys_obj;
4693
4694 return 0;
4695kfree_obj:
Eric Anholt9a298b22009-03-24 12:23:04 -07004696 kfree(phys_obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004697 return ret;
4698}
4699
4700void i915_gem_free_phys_object(struct drm_device *dev, int id)
4701{
4702 drm_i915_private_t *dev_priv = dev->dev_private;
4703 struct drm_i915_gem_phys_object *phys_obj;
4704
4705 if (!dev_priv->mm.phys_objs[id - 1])
4706 return;
4707
4708 phys_obj = dev_priv->mm.phys_objs[id - 1];
4709 if (phys_obj->cur_obj) {
4710 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
4711 }
4712
4713#ifdef CONFIG_X86
4714 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4715#endif
4716 drm_pci_free(dev, phys_obj->handle);
4717 kfree(phys_obj);
4718 dev_priv->mm.phys_objs[id - 1] = NULL;
4719}
4720
4721void i915_gem_free_all_phys_object(struct drm_device *dev)
4722{
4723 int i;
4724
Dave Airlie260883c2009-01-22 17:58:49 +10004725 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004726 i915_gem_free_phys_object(dev, i);
4727}
4728
4729void i915_gem_detach_phys_object(struct drm_device *dev,
4730 struct drm_gem_object *obj)
4731{
4732 struct drm_i915_gem_object *obj_priv;
4733 int i;
4734 int ret;
4735 int page_count;
4736
Daniel Vetter23010e42010-03-08 13:35:02 +01004737 obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004738 if (!obj_priv->phys_obj)
4739 return;
4740
Chris Wilson4bdadb92010-01-27 13:36:32 +00004741 ret = i915_gem_object_get_pages(obj, 0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004742 if (ret)
4743 goto out;
4744
4745 page_count = obj->size / PAGE_SIZE;
4746
4747 for (i = 0; i < page_count; i++) {
Eric Anholt856fa192009-03-19 14:10:50 -07004748 char *dst = kmap_atomic(obj_priv->pages[i], KM_USER0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004749 char *src = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4750
4751 memcpy(dst, src, PAGE_SIZE);
4752 kunmap_atomic(dst, KM_USER0);
4753 }
Eric Anholt856fa192009-03-19 14:10:50 -07004754 drm_clflush_pages(obj_priv->pages, page_count);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004755 drm_agp_chipset_flush(dev);
Chris Wilsond78b47b2009-06-17 21:52:49 +01004756
4757 i915_gem_object_put_pages(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004758out:
4759 obj_priv->phys_obj->cur_obj = NULL;
4760 obj_priv->phys_obj = NULL;
4761}
4762
4763int
4764i915_gem_attach_phys_object(struct drm_device *dev,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004765 struct drm_gem_object *obj,
4766 int id,
4767 int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004768{
4769 drm_i915_private_t *dev_priv = dev->dev_private;
4770 struct drm_i915_gem_object *obj_priv;
4771 int ret = 0;
4772 int page_count;
4773 int i;
4774
4775 if (id > I915_MAX_PHYS_OBJECT)
4776 return -EINVAL;
4777
Daniel Vetter23010e42010-03-08 13:35:02 +01004778 obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004779
4780 if (obj_priv->phys_obj) {
4781 if (obj_priv->phys_obj->id == id)
4782 return 0;
4783 i915_gem_detach_phys_object(dev, obj);
4784 }
4785
Dave Airlie71acb5e2008-12-30 20:31:46 +10004786 /* create a new object */
4787 if (!dev_priv->mm.phys_objs[id - 1]) {
4788 ret = i915_gem_init_phys_object(dev, id,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004789 obj->size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004790 if (ret) {
Linus Torvaldsaeb565d2009-01-26 10:01:53 -08004791 DRM_ERROR("failed to init phys object %d size: %zu\n", id, obj->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004792 goto out;
4793 }
4794 }
4795
4796 /* bind to the object */
4797 obj_priv->phys_obj = dev_priv->mm.phys_objs[id - 1];
4798 obj_priv->phys_obj->cur_obj = obj;
4799
Chris Wilson4bdadb92010-01-27 13:36:32 +00004800 ret = i915_gem_object_get_pages(obj, 0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004801 if (ret) {
4802 DRM_ERROR("failed to get page list\n");
4803 goto out;
4804 }
4805
4806 page_count = obj->size / PAGE_SIZE;
4807
4808 for (i = 0; i < page_count; i++) {
Eric Anholt856fa192009-03-19 14:10:50 -07004809 char *src = kmap_atomic(obj_priv->pages[i], KM_USER0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004810 char *dst = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4811
4812 memcpy(dst, src, PAGE_SIZE);
4813 kunmap_atomic(src, KM_USER0);
4814 }
4815
Chris Wilsond78b47b2009-06-17 21:52:49 +01004816 i915_gem_object_put_pages(obj);
4817
Dave Airlie71acb5e2008-12-30 20:31:46 +10004818 return 0;
4819out:
4820 return ret;
4821}
4822
4823static int
4824i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
4825 struct drm_i915_gem_pwrite *args,
4826 struct drm_file *file_priv)
4827{
Daniel Vetter23010e42010-03-08 13:35:02 +01004828 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004829 void *obj_addr;
4830 int ret;
4831 char __user *user_data;
4832
4833 user_data = (char __user *) (uintptr_t) args->data_ptr;
4834 obj_addr = obj_priv->phys_obj->handle->vaddr + args->offset;
4835
Zhao Yakui44d98a62009-10-09 11:39:40 +08004836 DRM_DEBUG_DRIVER("obj_addr %p, %lld\n", obj_addr, args->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004837 ret = copy_from_user(obj_addr, user_data, args->size);
4838 if (ret)
4839 return -EFAULT;
4840
4841 drm_agp_chipset_flush(dev);
4842 return 0;
4843}
Eric Anholtb9624422009-06-03 07:27:35 +00004844
4845void i915_gem_release(struct drm_device * dev, struct drm_file *file_priv)
4846{
4847 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
4848
4849 /* Clean up our request list when the client is going away, so that
4850 * later retire_requests won't dereference our soon-to-be-gone
4851 * file_priv.
4852 */
4853 mutex_lock(&dev->struct_mutex);
4854 while (!list_empty(&i915_file_priv->mm.request_list))
4855 list_del_init(i915_file_priv->mm.request_list.next);
4856 mutex_unlock(&dev->struct_mutex);
4857}
Chris Wilson31169712009-09-14 16:50:28 +01004858
Chris Wilson31169712009-09-14 16:50:28 +01004859static int
Chris Wilson1637ef42010-04-20 17:10:35 +01004860i915_gpu_is_active(struct drm_device *dev)
4861{
4862 drm_i915_private_t *dev_priv = dev->dev_private;
4863 int lists_empty;
4864
4865 spin_lock(&dev_priv->mm.active_list_lock);
4866 lists_empty = list_empty(&dev_priv->mm.flushing_list) &&
Zou Nan hai852835f2010-05-21 09:08:56 +08004867 list_empty(&dev_priv->render_ring.active_list);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004868 if (HAS_BSD(dev))
4869 lists_empty &= list_empty(&dev_priv->bsd_ring.active_list);
Chris Wilson1637ef42010-04-20 17:10:35 +01004870 spin_unlock(&dev_priv->mm.active_list_lock);
4871
4872 return !lists_empty;
4873}
4874
4875static int
Dave Chinner7f8275d2010-07-19 14:56:17 +10004876i915_gem_shrink(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask)
Chris Wilson31169712009-09-14 16:50:28 +01004877{
4878 drm_i915_private_t *dev_priv, *next_dev;
4879 struct drm_i915_gem_object *obj_priv, *next_obj;
4880 int cnt = 0;
4881 int would_deadlock = 1;
4882
4883 /* "fast-path" to count number of available objects */
4884 if (nr_to_scan == 0) {
4885 spin_lock(&shrink_list_lock);
4886 list_for_each_entry(dev_priv, &shrink_list, mm.shrink_list) {
4887 struct drm_device *dev = dev_priv->dev;
4888
4889 if (mutex_trylock(&dev->struct_mutex)) {
4890 list_for_each_entry(obj_priv,
4891 &dev_priv->mm.inactive_list,
4892 list)
4893 cnt++;
4894 mutex_unlock(&dev->struct_mutex);
4895 }
4896 }
4897 spin_unlock(&shrink_list_lock);
4898
4899 return (cnt / 100) * sysctl_vfs_cache_pressure;
4900 }
4901
4902 spin_lock(&shrink_list_lock);
4903
Chris Wilson1637ef42010-04-20 17:10:35 +01004904rescan:
Chris Wilson31169712009-09-14 16:50:28 +01004905 /* first scan for clean buffers */
4906 list_for_each_entry_safe(dev_priv, next_dev,
4907 &shrink_list, mm.shrink_list) {
4908 struct drm_device *dev = dev_priv->dev;
4909
4910 if (! mutex_trylock(&dev->struct_mutex))
4911 continue;
4912
4913 spin_unlock(&shrink_list_lock);
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01004914 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004915
Chris Wilson31169712009-09-14 16:50:28 +01004916 list_for_each_entry_safe(obj_priv, next_obj,
4917 &dev_priv->mm.inactive_list,
4918 list) {
4919 if (i915_gem_object_is_purgeable(obj_priv)) {
Daniel Vettera8089e82010-04-09 19:05:09 +00004920 i915_gem_object_unbind(&obj_priv->base);
Chris Wilson31169712009-09-14 16:50:28 +01004921 if (--nr_to_scan <= 0)
4922 break;
4923 }
4924 }
4925
4926 spin_lock(&shrink_list_lock);
4927 mutex_unlock(&dev->struct_mutex);
4928
Chris Wilson963b4832009-09-20 23:03:54 +01004929 would_deadlock = 0;
4930
Chris Wilson31169712009-09-14 16:50:28 +01004931 if (nr_to_scan <= 0)
4932 break;
4933 }
4934
4935 /* second pass, evict/count anything still on the inactive list */
4936 list_for_each_entry_safe(dev_priv, next_dev,
4937 &shrink_list, mm.shrink_list) {
4938 struct drm_device *dev = dev_priv->dev;
4939
4940 if (! mutex_trylock(&dev->struct_mutex))
4941 continue;
4942
4943 spin_unlock(&shrink_list_lock);
4944
4945 list_for_each_entry_safe(obj_priv, next_obj,
4946 &dev_priv->mm.inactive_list,
4947 list) {
4948 if (nr_to_scan > 0) {
Daniel Vettera8089e82010-04-09 19:05:09 +00004949 i915_gem_object_unbind(&obj_priv->base);
Chris Wilson31169712009-09-14 16:50:28 +01004950 nr_to_scan--;
4951 } else
4952 cnt++;
4953 }
4954
4955 spin_lock(&shrink_list_lock);
4956 mutex_unlock(&dev->struct_mutex);
4957
4958 would_deadlock = 0;
4959 }
4960
Chris Wilson1637ef42010-04-20 17:10:35 +01004961 if (nr_to_scan) {
4962 int active = 0;
4963
4964 /*
4965 * We are desperate for pages, so as a last resort, wait
4966 * for the GPU to finish and discard whatever we can.
4967 * This has a dramatic impact to reduce the number of
4968 * OOM-killer events whilst running the GPU aggressively.
4969 */
4970 list_for_each_entry(dev_priv, &shrink_list, mm.shrink_list) {
4971 struct drm_device *dev = dev_priv->dev;
4972
4973 if (!mutex_trylock(&dev->struct_mutex))
4974 continue;
4975
4976 spin_unlock(&shrink_list_lock);
4977
4978 if (i915_gpu_is_active(dev)) {
4979 i915_gpu_idle(dev);
4980 active++;
4981 }
4982
4983 spin_lock(&shrink_list_lock);
4984 mutex_unlock(&dev->struct_mutex);
4985 }
4986
4987 if (active)
4988 goto rescan;
4989 }
4990
Chris Wilson31169712009-09-14 16:50:28 +01004991 spin_unlock(&shrink_list_lock);
4992
4993 if (would_deadlock)
4994 return -1;
4995 else if (cnt > 0)
4996 return (cnt / 100) * sysctl_vfs_cache_pressure;
4997 else
4998 return 0;
4999}
5000
5001static struct shrinker shrinker = {
5002 .shrink = i915_gem_shrink,
5003 .seeks = DEFAULT_SEEKS,
5004};
5005
5006__init void
5007i915_gem_shrinker_init(void)
5008{
5009 register_shrinker(&shrinker);
5010}
5011
5012__exit void
5013i915_gem_shrinker_exit(void)
5014{
5015 unregister_shrinker(&shrinker);
5016}