blob: b4b25e17d4e9b40dacab1fbc880657d90241caca [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>
Eric Anholt673a3942008-07-30 12:06:12 -070037
Daniel Vetter0108a3e2010-08-07 11:01:21 +010038static uint32_t i915_gem_get_gtt_alignment(struct drm_gem_object *obj);
Chris Wilson2dafb1e2010-06-07 14:03:05 +010039static int i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj);
Eric Anholte47c68e2008-11-14 13:35:19 -080040static void i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj);
41static void i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj);
Eric Anholte47c68e2008-11-14 13:35:19 -080042static int i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj,
43 int write);
44static int i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
45 uint64_t offset,
46 uint64_t size);
47static void i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj);
Eric Anholt673a3942008-07-30 12:06:12 -070048static int i915_gem_object_wait_rendering(struct drm_gem_object *obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -080049static int i915_gem_object_bind_to_gtt(struct drm_gem_object *obj,
50 unsigned alignment);
Jesse Barnesde151cf2008-11-12 10:03:55 -080051static void i915_gem_clear_fence_reg(struct drm_gem_object *obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +100052static int i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
53 struct drm_i915_gem_pwrite *args,
54 struct drm_file *file_priv);
Chris Wilsonbe726152010-07-23 23:18:50 +010055static void i915_gem_free_object_tail(struct drm_gem_object *obj);
Eric Anholt673a3942008-07-30 12:06:12 -070056
Chris Wilson31169712009-09-14 16:50:28 +010057static LIST_HEAD(shrink_list);
58static DEFINE_SPINLOCK(shrink_list_lock);
59
Chris Wilson7d1c4802010-08-07 21:45:03 +010060static inline bool
61i915_gem_object_is_inactive(struct drm_i915_gem_object *obj_priv)
62{
63 return obj_priv->gtt_space &&
64 !obj_priv->active &&
65 obj_priv->pin_count == 0;
66}
67
Jesse Barnes79e53942008-11-07 14:24:08 -080068int i915_gem_do_init(struct drm_device *dev, unsigned long start,
69 unsigned long end)
70{
71 drm_i915_private_t *dev_priv = dev->dev_private;
72
73 if (start >= end ||
74 (start & (PAGE_SIZE - 1)) != 0 ||
75 (end & (PAGE_SIZE - 1)) != 0) {
76 return -EINVAL;
77 }
78
79 drm_mm_init(&dev_priv->mm.gtt_space, start,
80 end - start);
81
82 dev->gtt_total = (uint32_t) (end - start);
83
84 return 0;
85}
Keith Packard6dbe2772008-10-14 21:41:13 -070086
Eric Anholt673a3942008-07-30 12:06:12 -070087int
88i915_gem_init_ioctl(struct drm_device *dev, void *data,
89 struct drm_file *file_priv)
90{
Eric Anholt673a3942008-07-30 12:06:12 -070091 struct drm_i915_gem_init *args = data;
Jesse Barnes79e53942008-11-07 14:24:08 -080092 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -070093
94 mutex_lock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080095 ret = i915_gem_do_init(dev, args->gtt_start, args->gtt_end);
Eric Anholt673a3942008-07-30 12:06:12 -070096 mutex_unlock(&dev->struct_mutex);
97
Jesse Barnes79e53942008-11-07 14:24:08 -080098 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -070099}
100
Eric Anholt5a125c32008-10-22 21:40:13 -0700101int
102i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
103 struct drm_file *file_priv)
104{
Eric Anholt5a125c32008-10-22 21:40:13 -0700105 struct drm_i915_gem_get_aperture *args = data;
Eric Anholt5a125c32008-10-22 21:40:13 -0700106
107 if (!(dev->driver->driver_features & DRIVER_GEM))
108 return -ENODEV;
109
110 args->aper_size = dev->gtt_total;
Keith Packard2678d9d2008-11-20 22:54:54 -0800111 args->aper_available_size = (args->aper_size -
112 atomic_read(&dev->pin_memory));
Eric Anholt5a125c32008-10-22 21:40:13 -0700113
114 return 0;
115}
116
Eric Anholt673a3942008-07-30 12:06:12 -0700117
118/**
119 * Creates a new mm object and returns a handle to it.
120 */
121int
122i915_gem_create_ioctl(struct drm_device *dev, void *data,
123 struct drm_file *file_priv)
124{
125 struct drm_i915_gem_create *args = data;
126 struct drm_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300127 int ret;
128 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700129
130 args->size = roundup(args->size, PAGE_SIZE);
131
132 /* Allocate the new object */
Daniel Vetterac52bc52010-04-09 19:05:06 +0000133 obj = i915_gem_alloc_object(dev, args->size);
Eric Anholt673a3942008-07-30 12:06:12 -0700134 if (obj == NULL)
135 return -ENOMEM;
136
137 ret = drm_gem_handle_create(file_priv, obj, &handle);
Chris Wilson86f100b2010-07-24 21:03:49 +0100138 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700139 if (ret)
140 return ret;
141
142 args->handle = handle;
143
144 return 0;
145}
146
Eric Anholt40123c12009-03-09 13:42:30 -0700147static inline int
Eric Anholteb014592009-03-10 11:44:52 -0700148fast_shmem_read(struct page **pages,
149 loff_t page_base, int page_offset,
150 char __user *data,
151 int length)
152{
153 char __iomem *vaddr;
Florian Mickler2bc43b52009-04-06 22:55:41 +0200154 int unwritten;
Eric Anholteb014592009-03-10 11:44:52 -0700155
156 vaddr = kmap_atomic(pages[page_base >> PAGE_SHIFT], KM_USER0);
157 if (vaddr == NULL)
158 return -ENOMEM;
Florian Mickler2bc43b52009-04-06 22:55:41 +0200159 unwritten = __copy_to_user_inatomic(data, vaddr + page_offset, length);
Eric Anholteb014592009-03-10 11:44:52 -0700160 kunmap_atomic(vaddr, KM_USER0);
161
Florian Mickler2bc43b52009-04-06 22:55:41 +0200162 if (unwritten)
163 return -EFAULT;
164
165 return 0;
Eric Anholteb014592009-03-10 11:44:52 -0700166}
167
Eric Anholt280b7132009-03-12 16:56:27 -0700168static int i915_gem_object_needs_bit17_swizzle(struct drm_gem_object *obj)
169{
170 drm_i915_private_t *dev_priv = obj->dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +0100171 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt280b7132009-03-12 16:56:27 -0700172
173 return dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_9_10_17 &&
174 obj_priv->tiling_mode != I915_TILING_NONE;
175}
176
Chris Wilson99a03df2010-05-27 14:15:34 +0100177static inline void
Eric Anholt40123c12009-03-09 13:42:30 -0700178slow_shmem_copy(struct page *dst_page,
179 int dst_offset,
180 struct page *src_page,
181 int src_offset,
182 int length)
183{
184 char *dst_vaddr, *src_vaddr;
185
Chris Wilson99a03df2010-05-27 14:15:34 +0100186 dst_vaddr = kmap(dst_page);
187 src_vaddr = kmap(src_page);
Eric Anholt40123c12009-03-09 13:42:30 -0700188
189 memcpy(dst_vaddr + dst_offset, src_vaddr + src_offset, length);
190
Chris Wilson99a03df2010-05-27 14:15:34 +0100191 kunmap(src_page);
192 kunmap(dst_page);
Eric Anholt40123c12009-03-09 13:42:30 -0700193}
194
Chris Wilson99a03df2010-05-27 14:15:34 +0100195static inline void
Eric Anholt280b7132009-03-12 16:56:27 -0700196slow_shmem_bit17_copy(struct page *gpu_page,
197 int gpu_offset,
198 struct page *cpu_page,
199 int cpu_offset,
200 int length,
201 int is_read)
202{
203 char *gpu_vaddr, *cpu_vaddr;
204
205 /* Use the unswizzled path if this page isn't affected. */
206 if ((page_to_phys(gpu_page) & (1 << 17)) == 0) {
207 if (is_read)
208 return slow_shmem_copy(cpu_page, cpu_offset,
209 gpu_page, gpu_offset, length);
210 else
211 return slow_shmem_copy(gpu_page, gpu_offset,
212 cpu_page, cpu_offset, length);
213 }
214
Chris Wilson99a03df2010-05-27 14:15:34 +0100215 gpu_vaddr = kmap(gpu_page);
216 cpu_vaddr = kmap(cpu_page);
Eric Anholt280b7132009-03-12 16:56:27 -0700217
218 /* Copy the data, XORing A6 with A17 (1). The user already knows he's
219 * XORing with the other bits (A9 for Y, A9 and A10 for X)
220 */
221 while (length > 0) {
222 int cacheline_end = ALIGN(gpu_offset + 1, 64);
223 int this_length = min(cacheline_end - gpu_offset, length);
224 int swizzled_gpu_offset = gpu_offset ^ 64;
225
226 if (is_read) {
227 memcpy(cpu_vaddr + cpu_offset,
228 gpu_vaddr + swizzled_gpu_offset,
229 this_length);
230 } else {
231 memcpy(gpu_vaddr + swizzled_gpu_offset,
232 cpu_vaddr + cpu_offset,
233 this_length);
234 }
235 cpu_offset += this_length;
236 gpu_offset += this_length;
237 length -= this_length;
238 }
239
Chris Wilson99a03df2010-05-27 14:15:34 +0100240 kunmap(cpu_page);
241 kunmap(gpu_page);
Eric Anholt280b7132009-03-12 16:56:27 -0700242}
243
Eric Anholt673a3942008-07-30 12:06:12 -0700244/**
Eric Anholteb014592009-03-10 11:44:52 -0700245 * This is the fast shmem pread path, which attempts to copy_from_user directly
246 * from the backing pages of the object to the user's address space. On a
247 * fault, it fails so we can fall back to i915_gem_shmem_pwrite_slow().
248 */
249static int
250i915_gem_shmem_pread_fast(struct drm_device *dev, struct drm_gem_object *obj,
251 struct drm_i915_gem_pread *args,
252 struct drm_file *file_priv)
253{
Daniel Vetter23010e42010-03-08 13:35:02 +0100254 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700255 ssize_t remain;
256 loff_t offset, page_base;
257 char __user *user_data;
258 int page_offset, page_length;
259 int ret;
260
261 user_data = (char __user *) (uintptr_t) args->data_ptr;
262 remain = args->size;
263
264 mutex_lock(&dev->struct_mutex);
265
Chris Wilson4bdadb92010-01-27 13:36:32 +0000266 ret = i915_gem_object_get_pages(obj, 0);
Eric Anholteb014592009-03-10 11:44:52 -0700267 if (ret != 0)
268 goto fail_unlock;
269
270 ret = i915_gem_object_set_cpu_read_domain_range(obj, args->offset,
271 args->size);
272 if (ret != 0)
273 goto fail_put_pages;
274
Daniel Vetter23010e42010-03-08 13:35:02 +0100275 obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700276 offset = args->offset;
277
278 while (remain > 0) {
279 /* Operation in this page
280 *
281 * page_base = page offset within aperture
282 * page_offset = offset within page
283 * page_length = bytes to copy for this page
284 */
285 page_base = (offset & ~(PAGE_SIZE-1));
286 page_offset = offset & (PAGE_SIZE-1);
287 page_length = remain;
288 if ((page_offset + remain) > PAGE_SIZE)
289 page_length = PAGE_SIZE - page_offset;
290
291 ret = fast_shmem_read(obj_priv->pages,
292 page_base, page_offset,
293 user_data, page_length);
294 if (ret)
295 goto fail_put_pages;
296
297 remain -= page_length;
298 user_data += page_length;
299 offset += page_length;
300 }
301
302fail_put_pages:
303 i915_gem_object_put_pages(obj);
304fail_unlock:
305 mutex_unlock(&dev->struct_mutex);
306
307 return ret;
308}
309
Chris Wilson07f73f62009-09-14 16:50:30 +0100310static int
311i915_gem_object_get_pages_or_evict(struct drm_gem_object *obj)
312{
313 int ret;
314
Chris Wilson4bdadb92010-01-27 13:36:32 +0000315 ret = i915_gem_object_get_pages(obj, __GFP_NORETRY | __GFP_NOWARN);
Chris Wilson07f73f62009-09-14 16:50:30 +0100316
317 /* If we've insufficient memory to map in the pages, attempt
318 * to make some space by throwing out some old buffers.
319 */
320 if (ret == -ENOMEM) {
321 struct drm_device *dev = obj->dev;
Chris Wilson07f73f62009-09-14 16:50:30 +0100322
Daniel Vetter0108a3e2010-08-07 11:01:21 +0100323 ret = i915_gem_evict_something(dev, obj->size,
324 i915_gem_get_gtt_alignment(obj));
Chris Wilson07f73f62009-09-14 16:50:30 +0100325 if (ret)
326 return ret;
327
Chris Wilson4bdadb92010-01-27 13:36:32 +0000328 ret = i915_gem_object_get_pages(obj, 0);
Chris Wilson07f73f62009-09-14 16:50:30 +0100329 }
330
331 return ret;
332}
333
Eric Anholteb014592009-03-10 11:44:52 -0700334/**
335 * This is the fallback shmem pread path, which allocates temporary storage
336 * in kernel space to copy_to_user into outside of the struct_mutex, so we
337 * can copy out of the object's backing pages while holding the struct mutex
338 * and not take page faults.
339 */
340static int
341i915_gem_shmem_pread_slow(struct drm_device *dev, struct drm_gem_object *obj,
342 struct drm_i915_gem_pread *args,
343 struct drm_file *file_priv)
344{
Daniel Vetter23010e42010-03-08 13:35:02 +0100345 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700346 struct mm_struct *mm = current->mm;
347 struct page **user_pages;
348 ssize_t remain;
349 loff_t offset, pinned_pages, i;
350 loff_t first_data_page, last_data_page, num_pages;
351 int shmem_page_index, shmem_page_offset;
352 int data_page_index, data_page_offset;
353 int page_length;
354 int ret;
355 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700356 int do_bit17_swizzling;
Eric Anholteb014592009-03-10 11:44:52 -0700357
358 remain = args->size;
359
360 /* Pin the user pages containing the data. We can't fault while
361 * holding the struct mutex, yet we want to hold it while
362 * dereferencing the user data.
363 */
364 first_data_page = data_ptr / PAGE_SIZE;
365 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
366 num_pages = last_data_page - first_data_page + 1;
367
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700368 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
Eric Anholteb014592009-03-10 11:44:52 -0700369 if (user_pages == NULL)
370 return -ENOMEM;
371
372 down_read(&mm->mmap_sem);
373 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
Eric Anholte5e9ecd2009-04-07 16:01:22 -0700374 num_pages, 1, 0, user_pages, NULL);
Eric Anholteb014592009-03-10 11:44:52 -0700375 up_read(&mm->mmap_sem);
376 if (pinned_pages < num_pages) {
377 ret = -EFAULT;
378 goto fail_put_user_pages;
379 }
380
Eric Anholt280b7132009-03-12 16:56:27 -0700381 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
382
Eric Anholteb014592009-03-10 11:44:52 -0700383 mutex_lock(&dev->struct_mutex);
384
Chris Wilson07f73f62009-09-14 16:50:30 +0100385 ret = i915_gem_object_get_pages_or_evict(obj);
386 if (ret)
Eric Anholteb014592009-03-10 11:44:52 -0700387 goto fail_unlock;
388
389 ret = i915_gem_object_set_cpu_read_domain_range(obj, args->offset,
390 args->size);
391 if (ret != 0)
392 goto fail_put_pages;
393
Daniel Vetter23010e42010-03-08 13:35:02 +0100394 obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700395 offset = args->offset;
396
397 while (remain > 0) {
398 /* Operation in this page
399 *
400 * shmem_page_index = page number within shmem file
401 * shmem_page_offset = offset within page in shmem file
402 * data_page_index = page number in get_user_pages return
403 * data_page_offset = offset with data_page_index page.
404 * page_length = bytes to copy for this page
405 */
406 shmem_page_index = offset / PAGE_SIZE;
407 shmem_page_offset = offset & ~PAGE_MASK;
408 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
409 data_page_offset = data_ptr & ~PAGE_MASK;
410
411 page_length = remain;
412 if ((shmem_page_offset + page_length) > PAGE_SIZE)
413 page_length = PAGE_SIZE - shmem_page_offset;
414 if ((data_page_offset + page_length) > PAGE_SIZE)
415 page_length = PAGE_SIZE - data_page_offset;
416
Eric Anholt280b7132009-03-12 16:56:27 -0700417 if (do_bit17_swizzling) {
Chris Wilson99a03df2010-05-27 14:15:34 +0100418 slow_shmem_bit17_copy(obj_priv->pages[shmem_page_index],
Eric Anholt280b7132009-03-12 16:56:27 -0700419 shmem_page_offset,
Chris Wilson99a03df2010-05-27 14:15:34 +0100420 user_pages[data_page_index],
421 data_page_offset,
422 page_length,
423 1);
424 } else {
425 slow_shmem_copy(user_pages[data_page_index],
426 data_page_offset,
427 obj_priv->pages[shmem_page_index],
428 shmem_page_offset,
429 page_length);
Eric Anholt280b7132009-03-12 16:56:27 -0700430 }
Eric Anholteb014592009-03-10 11:44:52 -0700431
432 remain -= page_length;
433 data_ptr += page_length;
434 offset += page_length;
435 }
436
437fail_put_pages:
438 i915_gem_object_put_pages(obj);
439fail_unlock:
440 mutex_unlock(&dev->struct_mutex);
441fail_put_user_pages:
442 for (i = 0; i < pinned_pages; i++) {
443 SetPageDirty(user_pages[i]);
444 page_cache_release(user_pages[i]);
445 }
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700446 drm_free_large(user_pages);
Eric Anholteb014592009-03-10 11:44:52 -0700447
448 return ret;
449}
450
Eric Anholt673a3942008-07-30 12:06:12 -0700451/**
452 * Reads data from the object referenced by handle.
453 *
454 * On error, the contents of *data are undefined.
455 */
456int
457i915_gem_pread_ioctl(struct drm_device *dev, void *data,
458 struct drm_file *file_priv)
459{
460 struct drm_i915_gem_pread *args = data;
461 struct drm_gem_object *obj;
462 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -0700463 int ret;
464
465 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
466 if (obj == NULL)
467 return -EBADF;
Daniel Vetter23010e42010-03-08 13:35:02 +0100468 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700469
470 /* Bounds check source.
471 *
472 * XXX: This could use review for overflow issues...
473 */
474 if (args->offset > obj->size || args->size > obj->size ||
475 args->offset + args->size > obj->size) {
Luca Barbieribc9025b2010-02-09 05:49:12 +0000476 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700477 return -EINVAL;
478 }
479
Eric Anholt280b7132009-03-12 16:56:27 -0700480 if (i915_gem_object_needs_bit17_swizzle(obj)) {
Eric Anholteb014592009-03-10 11:44:52 -0700481 ret = i915_gem_shmem_pread_slow(dev, obj, args, file_priv);
Eric Anholt280b7132009-03-12 16:56:27 -0700482 } else {
483 ret = i915_gem_shmem_pread_fast(dev, obj, args, file_priv);
484 if (ret != 0)
485 ret = i915_gem_shmem_pread_slow(dev, obj, args,
486 file_priv);
487 }
Eric Anholt673a3942008-07-30 12:06:12 -0700488
Luca Barbieribc9025b2010-02-09 05:49:12 +0000489 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700490
Eric Anholteb014592009-03-10 11:44:52 -0700491 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700492}
493
Keith Packard0839ccb2008-10-30 19:38:48 -0700494/* This is the fast write path which cannot handle
495 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700496 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700497
Keith Packard0839ccb2008-10-30 19:38:48 -0700498static inline int
499fast_user_write(struct io_mapping *mapping,
500 loff_t page_base, int page_offset,
501 char __user *user_data,
502 int length)
503{
504 char *vaddr_atomic;
505 unsigned long unwritten;
506
Chris Wilsonfca3ec02010-08-04 14:34:24 +0100507 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base, KM_USER0);
Keith Packard0839ccb2008-10-30 19:38:48 -0700508 unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
509 user_data, length);
Chris Wilsonfca3ec02010-08-04 14:34:24 +0100510 io_mapping_unmap_atomic(vaddr_atomic, KM_USER0);
Keith Packard0839ccb2008-10-30 19:38:48 -0700511 if (unwritten)
512 return -EFAULT;
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700513 return 0;
Keith Packard0839ccb2008-10-30 19:38:48 -0700514}
515
516/* Here's the write path which can sleep for
517 * page faults
518 */
519
Chris Wilsonab34c222010-05-27 14:15:35 +0100520static inline void
Eric Anholt3de09aa2009-03-09 09:42:23 -0700521slow_kernel_write(struct io_mapping *mapping,
522 loff_t gtt_base, int gtt_offset,
523 struct page *user_page, int user_offset,
524 int length)
Keith Packard0839ccb2008-10-30 19:38:48 -0700525{
Chris Wilsonab34c222010-05-27 14:15:35 +0100526 char __iomem *dst_vaddr;
527 char *src_vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -0700528
Chris Wilsonab34c222010-05-27 14:15:35 +0100529 dst_vaddr = io_mapping_map_wc(mapping, gtt_base);
530 src_vaddr = kmap(user_page);
531
532 memcpy_toio(dst_vaddr + gtt_offset,
533 src_vaddr + user_offset,
534 length);
535
536 kunmap(user_page);
537 io_mapping_unmap(dst_vaddr);
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700538}
539
Eric Anholt40123c12009-03-09 13:42:30 -0700540static inline int
541fast_shmem_write(struct page **pages,
542 loff_t page_base, int page_offset,
543 char __user *data,
544 int length)
545{
546 char __iomem *vaddr;
Dave Airlied0088772009-03-28 20:29:48 -0400547 unsigned long unwritten;
Eric Anholt40123c12009-03-09 13:42:30 -0700548
549 vaddr = kmap_atomic(pages[page_base >> PAGE_SHIFT], KM_USER0);
550 if (vaddr == NULL)
551 return -ENOMEM;
Dave Airlied0088772009-03-28 20:29:48 -0400552 unwritten = __copy_from_user_inatomic(vaddr + page_offset, data, length);
Eric Anholt40123c12009-03-09 13:42:30 -0700553 kunmap_atomic(vaddr, KM_USER0);
554
Dave Airlied0088772009-03-28 20:29:48 -0400555 if (unwritten)
556 return -EFAULT;
Eric Anholt40123c12009-03-09 13:42:30 -0700557 return 0;
558}
559
Eric Anholt3de09aa2009-03-09 09:42:23 -0700560/**
561 * This is the fast pwrite path, where we copy the data directly from the
562 * user into the GTT, uncached.
563 */
Eric Anholt673a3942008-07-30 12:06:12 -0700564static int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700565i915_gem_gtt_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
566 struct drm_i915_gem_pwrite *args,
567 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700568{
Daniel Vetter23010e42010-03-08 13:35:02 +0100569 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Keith Packard0839ccb2008-10-30 19:38:48 -0700570 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700571 ssize_t remain;
Keith Packard0839ccb2008-10-30 19:38:48 -0700572 loff_t offset, page_base;
Eric Anholt673a3942008-07-30 12:06:12 -0700573 char __user *user_data;
Keith Packard0839ccb2008-10-30 19:38:48 -0700574 int page_offset, page_length;
575 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700576
577 user_data = (char __user *) (uintptr_t) args->data_ptr;
578 remain = args->size;
579 if (!access_ok(VERIFY_READ, user_data, remain))
580 return -EFAULT;
581
582
583 mutex_lock(&dev->struct_mutex);
584 ret = i915_gem_object_pin(obj, 0);
585 if (ret) {
586 mutex_unlock(&dev->struct_mutex);
587 return ret;
588 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800589 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
Eric Anholt673a3942008-07-30 12:06:12 -0700590 if (ret)
591 goto fail;
592
Daniel Vetter23010e42010-03-08 13:35:02 +0100593 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700594 offset = obj_priv->gtt_offset + args->offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700595
596 while (remain > 0) {
597 /* Operation in this page
598 *
Keith Packard0839ccb2008-10-30 19:38:48 -0700599 * page_base = page offset within aperture
600 * page_offset = offset within page
601 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -0700602 */
Keith Packard0839ccb2008-10-30 19:38:48 -0700603 page_base = (offset & ~(PAGE_SIZE-1));
604 page_offset = offset & (PAGE_SIZE-1);
605 page_length = remain;
606 if ((page_offset + remain) > PAGE_SIZE)
607 page_length = PAGE_SIZE - page_offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700608
Keith Packard0839ccb2008-10-30 19:38:48 -0700609 ret = fast_user_write (dev_priv->mm.gtt_mapping, page_base,
610 page_offset, user_data, page_length);
Eric Anholt673a3942008-07-30 12:06:12 -0700611
Keith Packard0839ccb2008-10-30 19:38:48 -0700612 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -0700613 * source page isn't available. Return the error and we'll
614 * retry in the slow path.
Keith Packard0839ccb2008-10-30 19:38:48 -0700615 */
Eric Anholt3de09aa2009-03-09 09:42:23 -0700616 if (ret)
617 goto fail;
Eric Anholt673a3942008-07-30 12:06:12 -0700618
Keith Packard0839ccb2008-10-30 19:38:48 -0700619 remain -= page_length;
620 user_data += page_length;
621 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700622 }
Eric Anholt673a3942008-07-30 12:06:12 -0700623
624fail:
625 i915_gem_object_unpin(obj);
626 mutex_unlock(&dev->struct_mutex);
627
628 return ret;
629}
630
Eric Anholt3de09aa2009-03-09 09:42:23 -0700631/**
632 * This is the fallback GTT pwrite path, which uses get_user_pages to pin
633 * the memory and maps it using kmap_atomic for copying.
634 *
635 * This code resulted in x11perf -rgb10text consuming about 10% more CPU
636 * than using i915_gem_gtt_pwrite_fast on a G45 (32-bit).
637 */
Eric Anholt3043c602008-10-02 12:24:47 -0700638static int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700639i915_gem_gtt_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
640 struct drm_i915_gem_pwrite *args,
641 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700642{
Daniel Vetter23010e42010-03-08 13:35:02 +0100643 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700644 drm_i915_private_t *dev_priv = dev->dev_private;
645 ssize_t remain;
646 loff_t gtt_page_base, offset;
647 loff_t first_data_page, last_data_page, num_pages;
648 loff_t pinned_pages, i;
649 struct page **user_pages;
650 struct mm_struct *mm = current->mm;
651 int gtt_page_offset, data_page_offset, data_page_index, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700652 int ret;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700653 uint64_t data_ptr = args->data_ptr;
654
655 remain = args->size;
656
657 /* Pin the user pages containing the data. We can't fault while
658 * holding the struct mutex, and all of the pwrite implementations
659 * want to hold it while dereferencing the user data.
660 */
661 first_data_page = data_ptr / PAGE_SIZE;
662 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
663 num_pages = last_data_page - first_data_page + 1;
664
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700665 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
Eric Anholt3de09aa2009-03-09 09:42:23 -0700666 if (user_pages == NULL)
667 return -ENOMEM;
668
669 down_read(&mm->mmap_sem);
670 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
671 num_pages, 0, 0, user_pages, NULL);
672 up_read(&mm->mmap_sem);
673 if (pinned_pages < num_pages) {
674 ret = -EFAULT;
675 goto out_unpin_pages;
676 }
677
678 mutex_lock(&dev->struct_mutex);
679 ret = i915_gem_object_pin(obj, 0);
680 if (ret)
681 goto out_unlock;
682
683 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
684 if (ret)
685 goto out_unpin_object;
686
Daniel Vetter23010e42010-03-08 13:35:02 +0100687 obj_priv = to_intel_bo(obj);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700688 offset = obj_priv->gtt_offset + args->offset;
689
690 while (remain > 0) {
691 /* Operation in this page
692 *
693 * gtt_page_base = page offset within aperture
694 * gtt_page_offset = offset within page in aperture
695 * data_page_index = page number in get_user_pages return
696 * data_page_offset = offset with data_page_index page.
697 * page_length = bytes to copy for this page
698 */
699 gtt_page_base = offset & PAGE_MASK;
700 gtt_page_offset = offset & ~PAGE_MASK;
701 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
702 data_page_offset = data_ptr & ~PAGE_MASK;
703
704 page_length = remain;
705 if ((gtt_page_offset + page_length) > PAGE_SIZE)
706 page_length = PAGE_SIZE - gtt_page_offset;
707 if ((data_page_offset + page_length) > PAGE_SIZE)
708 page_length = PAGE_SIZE - data_page_offset;
709
Chris Wilsonab34c222010-05-27 14:15:35 +0100710 slow_kernel_write(dev_priv->mm.gtt_mapping,
711 gtt_page_base, gtt_page_offset,
712 user_pages[data_page_index],
713 data_page_offset,
714 page_length);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700715
716 remain -= page_length;
717 offset += page_length;
718 data_ptr += page_length;
719 }
720
721out_unpin_object:
722 i915_gem_object_unpin(obj);
723out_unlock:
724 mutex_unlock(&dev->struct_mutex);
725out_unpin_pages:
726 for (i = 0; i < pinned_pages; i++)
727 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700728 drm_free_large(user_pages);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700729
730 return ret;
731}
732
Eric Anholt40123c12009-03-09 13:42:30 -0700733/**
734 * This is the fast shmem pwrite path, which attempts to directly
735 * copy_from_user into the kmapped pages backing the object.
736 */
Eric Anholt673a3942008-07-30 12:06:12 -0700737static int
Eric Anholt40123c12009-03-09 13:42:30 -0700738i915_gem_shmem_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
739 struct drm_i915_gem_pwrite *args,
740 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700741{
Daniel Vetter23010e42010-03-08 13:35:02 +0100742 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700743 ssize_t remain;
744 loff_t offset, page_base;
745 char __user *user_data;
746 int page_offset, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700747 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -0700748
749 user_data = (char __user *) (uintptr_t) args->data_ptr;
750 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700751
752 mutex_lock(&dev->struct_mutex);
753
Chris Wilson4bdadb92010-01-27 13:36:32 +0000754 ret = i915_gem_object_get_pages(obj, 0);
Eric Anholt40123c12009-03-09 13:42:30 -0700755 if (ret != 0)
756 goto fail_unlock;
757
Eric Anholte47c68e2008-11-14 13:35:19 -0800758 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Eric Anholt40123c12009-03-09 13:42:30 -0700759 if (ret != 0)
760 goto fail_put_pages;
Eric Anholt673a3942008-07-30 12:06:12 -0700761
Daniel Vetter23010e42010-03-08 13:35:02 +0100762 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700763 offset = args->offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700764 obj_priv->dirty = 1;
Eric Anholt673a3942008-07-30 12:06:12 -0700765
Eric Anholt40123c12009-03-09 13:42:30 -0700766 while (remain > 0) {
767 /* Operation in this page
768 *
769 * page_base = page offset within aperture
770 * page_offset = offset within page
771 * page_length = bytes to copy for this page
772 */
773 page_base = (offset & ~(PAGE_SIZE-1));
774 page_offset = offset & (PAGE_SIZE-1);
775 page_length = remain;
776 if ((page_offset + remain) > PAGE_SIZE)
777 page_length = PAGE_SIZE - page_offset;
778
779 ret = fast_shmem_write(obj_priv->pages,
780 page_base, page_offset,
781 user_data, page_length);
782 if (ret)
783 goto fail_put_pages;
784
785 remain -= page_length;
786 user_data += page_length;
787 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700788 }
789
Eric Anholt40123c12009-03-09 13:42:30 -0700790fail_put_pages:
791 i915_gem_object_put_pages(obj);
792fail_unlock:
Eric Anholt673a3942008-07-30 12:06:12 -0700793 mutex_unlock(&dev->struct_mutex);
794
Eric Anholt40123c12009-03-09 13:42:30 -0700795 return ret;
796}
797
798/**
799 * This is the fallback shmem pwrite path, which uses get_user_pages to pin
800 * the memory and maps it using kmap_atomic for copying.
801 *
802 * This avoids taking mmap_sem for faulting on the user's address while the
803 * struct_mutex is held.
804 */
805static int
806i915_gem_shmem_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
807 struct drm_i915_gem_pwrite *args,
808 struct drm_file *file_priv)
809{
Daniel Vetter23010e42010-03-08 13:35:02 +0100810 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700811 struct mm_struct *mm = current->mm;
812 struct page **user_pages;
813 ssize_t remain;
814 loff_t offset, pinned_pages, i;
815 loff_t first_data_page, last_data_page, num_pages;
816 int shmem_page_index, shmem_page_offset;
817 int data_page_index, data_page_offset;
818 int page_length;
819 int ret;
820 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700821 int do_bit17_swizzling;
Eric Anholt40123c12009-03-09 13:42:30 -0700822
823 remain = args->size;
824
825 /* Pin the user pages containing the data. We can't fault while
826 * holding the struct mutex, and all of the pwrite implementations
827 * want to hold it while dereferencing the user data.
828 */
829 first_data_page = data_ptr / PAGE_SIZE;
830 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
831 num_pages = last_data_page - first_data_page + 1;
832
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700833 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
Eric Anholt40123c12009-03-09 13:42:30 -0700834 if (user_pages == NULL)
835 return -ENOMEM;
836
837 down_read(&mm->mmap_sem);
838 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
839 num_pages, 0, 0, user_pages, NULL);
840 up_read(&mm->mmap_sem);
841 if (pinned_pages < num_pages) {
842 ret = -EFAULT;
843 goto fail_put_user_pages;
844 }
845
Eric Anholt280b7132009-03-12 16:56:27 -0700846 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
847
Eric Anholt40123c12009-03-09 13:42:30 -0700848 mutex_lock(&dev->struct_mutex);
849
Chris Wilson07f73f62009-09-14 16:50:30 +0100850 ret = i915_gem_object_get_pages_or_evict(obj);
851 if (ret)
Eric Anholt40123c12009-03-09 13:42:30 -0700852 goto fail_unlock;
853
854 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
855 if (ret != 0)
856 goto fail_put_pages;
857
Daniel Vetter23010e42010-03-08 13:35:02 +0100858 obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700859 offset = args->offset;
860 obj_priv->dirty = 1;
861
862 while (remain > 0) {
863 /* Operation in this page
864 *
865 * shmem_page_index = page number within shmem file
866 * shmem_page_offset = offset within page in shmem file
867 * data_page_index = page number in get_user_pages return
868 * data_page_offset = offset with data_page_index page.
869 * page_length = bytes to copy for this page
870 */
871 shmem_page_index = offset / PAGE_SIZE;
872 shmem_page_offset = offset & ~PAGE_MASK;
873 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
874 data_page_offset = data_ptr & ~PAGE_MASK;
875
876 page_length = remain;
877 if ((shmem_page_offset + page_length) > PAGE_SIZE)
878 page_length = PAGE_SIZE - shmem_page_offset;
879 if ((data_page_offset + page_length) > PAGE_SIZE)
880 page_length = PAGE_SIZE - data_page_offset;
881
Eric Anholt280b7132009-03-12 16:56:27 -0700882 if (do_bit17_swizzling) {
Chris Wilson99a03df2010-05-27 14:15:34 +0100883 slow_shmem_bit17_copy(obj_priv->pages[shmem_page_index],
Eric Anholt280b7132009-03-12 16:56:27 -0700884 shmem_page_offset,
885 user_pages[data_page_index],
886 data_page_offset,
Chris Wilson99a03df2010-05-27 14:15:34 +0100887 page_length,
888 0);
889 } else {
890 slow_shmem_copy(obj_priv->pages[shmem_page_index],
891 shmem_page_offset,
892 user_pages[data_page_index],
893 data_page_offset,
894 page_length);
Eric Anholt280b7132009-03-12 16:56:27 -0700895 }
Eric Anholt40123c12009-03-09 13:42:30 -0700896
897 remain -= page_length;
898 data_ptr += page_length;
899 offset += page_length;
900 }
901
902fail_put_pages:
903 i915_gem_object_put_pages(obj);
904fail_unlock:
905 mutex_unlock(&dev->struct_mutex);
906fail_put_user_pages:
907 for (i = 0; i < pinned_pages; i++)
908 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700909 drm_free_large(user_pages);
Eric Anholt40123c12009-03-09 13:42:30 -0700910
911 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700912}
913
914/**
915 * Writes data to the object referenced by handle.
916 *
917 * On error, the contents of the buffer that were to be modified are undefined.
918 */
919int
920i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
921 struct drm_file *file_priv)
922{
923 struct drm_i915_gem_pwrite *args = data;
924 struct drm_gem_object *obj;
925 struct drm_i915_gem_object *obj_priv;
926 int ret = 0;
927
928 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
929 if (obj == NULL)
930 return -EBADF;
Daniel Vetter23010e42010-03-08 13:35:02 +0100931 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700932
933 /* Bounds check destination.
934 *
935 * XXX: This could use review for overflow issues...
936 */
937 if (args->offset > obj->size || args->size > obj->size ||
938 args->offset + args->size > obj->size) {
Luca Barbieribc9025b2010-02-09 05:49:12 +0000939 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700940 return -EINVAL;
941 }
942
943 /* We can only do the GTT pwrite on untiled buffers, as otherwise
944 * it would end up going through the fenced access, and we'll get
945 * different detiling behavior between reading and writing.
946 * pread/pwrite currently are reading and writing from the CPU
947 * perspective, requiring manual detiling by the client.
948 */
Dave Airlie71acb5e2008-12-30 20:31:46 +1000949 if (obj_priv->phys_obj)
950 ret = i915_gem_phys_pwrite(dev, obj, args, file_priv);
951 else if (obj_priv->tiling_mode == I915_TILING_NONE &&
Chris Wilson9b8c4a02010-05-27 14:21:01 +0100952 dev->gtt_total != 0 &&
953 obj->write_domain != I915_GEM_DOMAIN_CPU) {
Eric Anholt3de09aa2009-03-09 09:42:23 -0700954 ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file_priv);
955 if (ret == -EFAULT) {
956 ret = i915_gem_gtt_pwrite_slow(dev, obj, args,
957 file_priv);
958 }
Eric Anholt280b7132009-03-12 16:56:27 -0700959 } else if (i915_gem_object_needs_bit17_swizzle(obj)) {
960 ret = i915_gem_shmem_pwrite_slow(dev, obj, args, file_priv);
Eric Anholt40123c12009-03-09 13:42:30 -0700961 } else {
962 ret = i915_gem_shmem_pwrite_fast(dev, obj, args, file_priv);
963 if (ret == -EFAULT) {
964 ret = i915_gem_shmem_pwrite_slow(dev, obj, args,
965 file_priv);
966 }
967 }
Eric Anholt673a3942008-07-30 12:06:12 -0700968
969#if WATCH_PWRITE
970 if (ret)
971 DRM_INFO("pwrite failed %d\n", ret);
972#endif
973
Luca Barbieribc9025b2010-02-09 05:49:12 +0000974 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700975
976 return ret;
977}
978
979/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800980 * Called when user space prepares to use an object with the CPU, either
981 * through the mmap ioctl's mapping or a GTT mapping.
Eric Anholt673a3942008-07-30 12:06:12 -0700982 */
983int
984i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
985 struct drm_file *file_priv)
986{
Eric Anholta09ba7f2009-08-29 12:49:51 -0700987 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700988 struct drm_i915_gem_set_domain *args = data;
989 struct drm_gem_object *obj;
Jesse Barnes652c3932009-08-17 13:31:43 -0700990 struct drm_i915_gem_object *obj_priv;
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800991 uint32_t read_domains = args->read_domains;
992 uint32_t write_domain = args->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -0700993 int ret;
994
995 if (!(dev->driver->driver_features & DRIVER_GEM))
996 return -ENODEV;
997
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800998 /* Only handle setting domains to types used by the CPU. */
Chris Wilson21d509e2009-06-06 09:46:02 +0100999 if (write_domain & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001000 return -EINVAL;
1001
Chris Wilson21d509e2009-06-06 09:46:02 +01001002 if (read_domains & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001003 return -EINVAL;
1004
1005 /* Having something in the write domain implies it's in the read
1006 * domain, and only that read domain. Enforce that in the request.
1007 */
1008 if (write_domain != 0 && read_domains != write_domain)
1009 return -EINVAL;
1010
Eric Anholt673a3942008-07-30 12:06:12 -07001011 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1012 if (obj == NULL)
1013 return -EBADF;
Daniel Vetter23010e42010-03-08 13:35:02 +01001014 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001015
1016 mutex_lock(&dev->struct_mutex);
Jesse Barnes652c3932009-08-17 13:31:43 -07001017
1018 intel_mark_busy(dev, obj);
1019
Eric Anholt673a3942008-07-30 12:06:12 -07001020#if WATCH_BUF
Krzysztof Halasacfd43c02009-06-20 00:31:28 +02001021 DRM_INFO("set_domain_ioctl %p(%zd), %08x %08x\n",
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001022 obj, obj->size, read_domains, write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07001023#endif
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001024 if (read_domains & I915_GEM_DOMAIN_GTT) {
1025 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
Eric Anholt02354392008-11-26 13:58:13 -08001026
Eric Anholta09ba7f2009-08-29 12:49:51 -07001027 /* Update the LRU on the fence for the CPU access that's
1028 * about to occur.
1029 */
1030 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001031 struct drm_i915_fence_reg *reg =
1032 &dev_priv->fence_regs[obj_priv->fence_reg];
1033 list_move_tail(&reg->lru_list,
Eric Anholta09ba7f2009-08-29 12:49:51 -07001034 &dev_priv->mm.fence_list);
1035 }
1036
Eric Anholt02354392008-11-26 13:58:13 -08001037 /* Silently promote "you're not bound, there was nothing to do"
1038 * to success, since the client was just asking us to
1039 * make sure everything was done.
1040 */
1041 if (ret == -EINVAL)
1042 ret = 0;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001043 } else {
Eric Anholte47c68e2008-11-14 13:35:19 -08001044 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001045 }
1046
Chris Wilson7d1c4802010-08-07 21:45:03 +01001047
1048 /* Maintain LRU order of "inactive" objects */
1049 if (ret == 0 && i915_gem_object_is_inactive(obj_priv))
1050 list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
1051
Eric Anholt673a3942008-07-30 12:06:12 -07001052 drm_gem_object_unreference(obj);
1053 mutex_unlock(&dev->struct_mutex);
1054 return ret;
1055}
1056
1057/**
1058 * Called when user space has done writes to this buffer
1059 */
1060int
1061i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
1062 struct drm_file *file_priv)
1063{
1064 struct drm_i915_gem_sw_finish *args = data;
1065 struct drm_gem_object *obj;
1066 struct drm_i915_gem_object *obj_priv;
1067 int ret = 0;
1068
1069 if (!(dev->driver->driver_features & DRIVER_GEM))
1070 return -ENODEV;
1071
1072 mutex_lock(&dev->struct_mutex);
1073 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1074 if (obj == NULL) {
1075 mutex_unlock(&dev->struct_mutex);
1076 return -EBADF;
1077 }
1078
1079#if WATCH_BUF
Krzysztof Halasacfd43c02009-06-20 00:31:28 +02001080 DRM_INFO("%s: sw_finish %d (%p %zd)\n",
Eric Anholt673a3942008-07-30 12:06:12 -07001081 __func__, args->handle, obj, obj->size);
1082#endif
Daniel Vetter23010e42010-03-08 13:35:02 +01001083 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001084
1085 /* Pinned buffers may be scanout, so flush the cache */
Eric Anholte47c68e2008-11-14 13:35:19 -08001086 if (obj_priv->pin_count)
1087 i915_gem_object_flush_cpu_write_domain(obj);
1088
Eric Anholt673a3942008-07-30 12:06:12 -07001089 drm_gem_object_unreference(obj);
1090 mutex_unlock(&dev->struct_mutex);
1091 return ret;
1092}
1093
1094/**
1095 * Maps the contents of an object, returning the address it is mapped
1096 * into.
1097 *
1098 * While the mapping holds a reference on the contents of the object, it doesn't
1099 * imply a ref on the object itself.
1100 */
1101int
1102i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
1103 struct drm_file *file_priv)
1104{
1105 struct drm_i915_gem_mmap *args = data;
1106 struct drm_gem_object *obj;
1107 loff_t offset;
1108 unsigned long addr;
1109
1110 if (!(dev->driver->driver_features & DRIVER_GEM))
1111 return -ENODEV;
1112
1113 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1114 if (obj == NULL)
1115 return -EBADF;
1116
1117 offset = args->offset;
1118
1119 down_write(&current->mm->mmap_sem);
1120 addr = do_mmap(obj->filp, 0, args->size,
1121 PROT_READ | PROT_WRITE, MAP_SHARED,
1122 args->offset);
1123 up_write(&current->mm->mmap_sem);
Luca Barbieribc9025b2010-02-09 05:49:12 +00001124 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001125 if (IS_ERR((void *)addr))
1126 return addr;
1127
1128 args->addr_ptr = (uint64_t) addr;
1129
1130 return 0;
1131}
1132
Jesse Barnesde151cf2008-11-12 10:03:55 -08001133/**
1134 * i915_gem_fault - fault a page into the GTT
1135 * vma: VMA in question
1136 * vmf: fault info
1137 *
1138 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1139 * from userspace. The fault handler takes care of binding the object to
1140 * the GTT (if needed), allocating and programming a fence register (again,
1141 * only if needed based on whether the old reg is still valid or the object
1142 * is tiled) and inserting a new PTE into the faulting process.
1143 *
1144 * Note that the faulting process may involve evicting existing objects
1145 * from the GTT and/or fence registers to make room. So performance may
1146 * suffer if the GTT working set is large or there are few fence registers
1147 * left.
1148 */
1149int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1150{
1151 struct drm_gem_object *obj = vma->vm_private_data;
1152 struct drm_device *dev = obj->dev;
Chris Wilson7d1c4802010-08-07 21:45:03 +01001153 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001154 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001155 pgoff_t page_offset;
1156 unsigned long pfn;
1157 int ret = 0;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001158 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001159
1160 /* We don't use vmf->pgoff since that has the fake offset */
1161 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1162 PAGE_SHIFT;
1163
1164 /* Now bind it into the GTT if needed */
1165 mutex_lock(&dev->struct_mutex);
1166 if (!obj_priv->gtt_space) {
Chris Wilsone67b8ce2009-09-14 16:50:26 +01001167 ret = i915_gem_object_bind_to_gtt(obj, 0);
Chris Wilsonc7150892009-09-23 00:43:56 +01001168 if (ret)
1169 goto unlock;
Kristian Høgsberg07f4f3e2009-05-27 14:37:28 -04001170
Jesse Barnesde151cf2008-11-12 10:03:55 -08001171 ret = i915_gem_object_set_to_gtt_domain(obj, write);
Chris Wilsonc7150892009-09-23 00:43:56 +01001172 if (ret)
1173 goto unlock;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001174 }
1175
1176 /* Need a new fence register? */
Eric Anholta09ba7f2009-08-29 12:49:51 -07001177 if (obj_priv->tiling_mode != I915_TILING_NONE) {
Chris Wilson8c4b8c32009-06-17 22:08:52 +01001178 ret = i915_gem_object_get_fence_reg(obj);
Chris Wilsonc7150892009-09-23 00:43:56 +01001179 if (ret)
1180 goto unlock;
Eric Anholtd9ddcb92009-01-27 10:33:49 -08001181 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001182
Chris Wilson7d1c4802010-08-07 21:45:03 +01001183 if (i915_gem_object_is_inactive(obj_priv))
1184 list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
1185
Jesse Barnesde151cf2008-11-12 10:03:55 -08001186 pfn = ((dev->agp->base + obj_priv->gtt_offset) >> PAGE_SHIFT) +
1187 page_offset;
1188
1189 /* Finally, remap it using the new GTT offset */
1190 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
Chris Wilsonc7150892009-09-23 00:43:56 +01001191unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001192 mutex_unlock(&dev->struct_mutex);
1193
1194 switch (ret) {
Chris Wilsonc7150892009-09-23 00:43:56 +01001195 case 0:
1196 case -ERESTARTSYS:
1197 return VM_FAULT_NOPAGE;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001198 case -ENOMEM:
1199 case -EAGAIN:
1200 return VM_FAULT_OOM;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001201 default:
Chris Wilsonc7150892009-09-23 00:43:56 +01001202 return VM_FAULT_SIGBUS;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001203 }
1204}
1205
1206/**
1207 * i915_gem_create_mmap_offset - create a fake mmap offset for an object
1208 * @obj: obj in question
1209 *
1210 * GEM memory mapping works by handing back to userspace a fake mmap offset
1211 * it can use in a subsequent mmap(2) call. The DRM core code then looks
1212 * up the object based on the offset and sets up the various memory mapping
1213 * structures.
1214 *
1215 * This routine allocates and attaches a fake offset for @obj.
1216 */
1217static int
1218i915_gem_create_mmap_offset(struct drm_gem_object *obj)
1219{
1220 struct drm_device *dev = obj->dev;
1221 struct drm_gem_mm *mm = dev->mm_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001222 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001223 struct drm_map_list *list;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +11001224 struct drm_local_map *map;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001225 int ret = 0;
1226
1227 /* Set the object up for mmap'ing */
1228 list = &obj->map_list;
Eric Anholt9a298b22009-03-24 12:23:04 -07001229 list->map = kzalloc(sizeof(struct drm_map_list), GFP_KERNEL);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001230 if (!list->map)
1231 return -ENOMEM;
1232
1233 map = list->map;
1234 map->type = _DRM_GEM;
1235 map->size = obj->size;
1236 map->handle = obj;
1237
1238 /* Get a DRM GEM mmap offset allocated... */
1239 list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
1240 obj->size / PAGE_SIZE, 0, 0);
1241 if (!list->file_offset_node) {
1242 DRM_ERROR("failed to allocate offset for bo %d\n", obj->name);
1243 ret = -ENOMEM;
1244 goto out_free_list;
1245 }
1246
1247 list->file_offset_node = drm_mm_get_block(list->file_offset_node,
1248 obj->size / PAGE_SIZE, 0);
1249 if (!list->file_offset_node) {
1250 ret = -ENOMEM;
1251 goto out_free_list;
1252 }
1253
1254 list->hash.key = list->file_offset_node->start;
1255 if (drm_ht_insert_item(&mm->offset_hash, &list->hash)) {
1256 DRM_ERROR("failed to add to map hash\n");
Chris Wilson5618ca62009-12-02 15:15:30 +00001257 ret = -ENOMEM;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001258 goto out_free_mm;
1259 }
1260
1261 /* By now we should be all set, any drm_mmap request on the offset
1262 * below will get to our mmap & fault handler */
1263 obj_priv->mmap_offset = ((uint64_t) list->hash.key) << PAGE_SHIFT;
1264
1265 return 0;
1266
1267out_free_mm:
1268 drm_mm_put_block(list->file_offset_node);
1269out_free_list:
Eric Anholt9a298b22009-03-24 12:23:04 -07001270 kfree(list->map);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001271
1272 return ret;
1273}
1274
Chris Wilson901782b2009-07-10 08:18:50 +01001275/**
1276 * i915_gem_release_mmap - remove physical page mappings
1277 * @obj: obj in question
1278 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001279 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01001280 * relinquish ownership of the pages back to the system.
1281 *
1282 * It is vital that we remove the page mapping if we have mapped a tiled
1283 * object through the GTT and then lose the fence register due to
1284 * resource pressure. Similarly if the object has been moved out of the
1285 * aperture, than pages mapped into userspace must be revoked. Removing the
1286 * mapping will then trigger a page fault on the next user access, allowing
1287 * fixup by i915_gem_fault().
1288 */
Eric Anholtd05ca302009-07-10 13:02:26 -07001289void
Chris Wilson901782b2009-07-10 08:18:50 +01001290i915_gem_release_mmap(struct drm_gem_object *obj)
1291{
1292 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001293 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson901782b2009-07-10 08:18:50 +01001294
1295 if (dev->dev_mapping)
1296 unmap_mapping_range(dev->dev_mapping,
1297 obj_priv->mmap_offset, obj->size, 1);
1298}
1299
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001300static void
1301i915_gem_free_mmap_offset(struct drm_gem_object *obj)
1302{
1303 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001304 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001305 struct drm_gem_mm *mm = dev->mm_private;
1306 struct drm_map_list *list;
1307
1308 list = &obj->map_list;
1309 drm_ht_remove_item(&mm->offset_hash, &list->hash);
1310
1311 if (list->file_offset_node) {
1312 drm_mm_put_block(list->file_offset_node);
1313 list->file_offset_node = NULL;
1314 }
1315
1316 if (list->map) {
Eric Anholt9a298b22009-03-24 12:23:04 -07001317 kfree(list->map);
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001318 list->map = NULL;
1319 }
1320
1321 obj_priv->mmap_offset = 0;
1322}
1323
Jesse Barnesde151cf2008-11-12 10:03:55 -08001324/**
1325 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1326 * @obj: object to check
1327 *
1328 * Return the required GTT alignment for an object, taking into account
1329 * potential fence register mapping if needed.
1330 */
1331static uint32_t
1332i915_gem_get_gtt_alignment(struct drm_gem_object *obj)
1333{
1334 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001335 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001336 int start, i;
1337
1338 /*
1339 * Minimum alignment is 4k (GTT page size), but might be greater
1340 * if a fence register is needed for the object.
1341 */
1342 if (IS_I965G(dev) || obj_priv->tiling_mode == I915_TILING_NONE)
1343 return 4096;
1344
1345 /*
1346 * Previous chips need to be aligned to the size of the smallest
1347 * fence register that can contain the object.
1348 */
1349 if (IS_I9XX(dev))
1350 start = 1024*1024;
1351 else
1352 start = 512*1024;
1353
1354 for (i = start; i < obj->size; i <<= 1)
1355 ;
1356
1357 return i;
1358}
1359
1360/**
1361 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1362 * @dev: DRM device
1363 * @data: GTT mapping ioctl data
1364 * @file_priv: GEM object info
1365 *
1366 * Simply returns the fake offset to userspace so it can mmap it.
1367 * The mmap call will end up in drm_gem_mmap(), which will set things
1368 * up so we can get faults in the handler above.
1369 *
1370 * The fault handler will take care of binding the object into the GTT
1371 * (since it may have been evicted to make room for something), allocating
1372 * a fence register, and mapping the appropriate aperture address into
1373 * userspace.
1374 */
1375int
1376i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
1377 struct drm_file *file_priv)
1378{
1379 struct drm_i915_gem_mmap_gtt *args = data;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001380 struct drm_gem_object *obj;
1381 struct drm_i915_gem_object *obj_priv;
1382 int ret;
1383
1384 if (!(dev->driver->driver_features & DRIVER_GEM))
1385 return -ENODEV;
1386
1387 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1388 if (obj == NULL)
1389 return -EBADF;
1390
1391 mutex_lock(&dev->struct_mutex);
1392
Daniel Vetter23010e42010-03-08 13:35:02 +01001393 obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001394
Chris Wilsonab182822009-09-22 18:46:17 +01001395 if (obj_priv->madv != I915_MADV_WILLNEED) {
1396 DRM_ERROR("Attempting to mmap a purgeable buffer\n");
1397 drm_gem_object_unreference(obj);
1398 mutex_unlock(&dev->struct_mutex);
1399 return -EINVAL;
1400 }
1401
1402
Jesse Barnesde151cf2008-11-12 10:03:55 -08001403 if (!obj_priv->mmap_offset) {
1404 ret = i915_gem_create_mmap_offset(obj);
Chris Wilson13af1062009-02-11 14:26:31 +00001405 if (ret) {
1406 drm_gem_object_unreference(obj);
1407 mutex_unlock(&dev->struct_mutex);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001408 return ret;
Chris Wilson13af1062009-02-11 14:26:31 +00001409 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001410 }
1411
1412 args->offset = obj_priv->mmap_offset;
1413
Jesse Barnesde151cf2008-11-12 10:03:55 -08001414 /*
1415 * Pull it into the GTT so that we have a page list (makes the
1416 * initial fault faster and any subsequent flushing possible).
1417 */
1418 if (!obj_priv->agp_mem) {
Chris Wilsone67b8ce2009-09-14 16:50:26 +01001419 ret = i915_gem_object_bind_to_gtt(obj, 0);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001420 if (ret) {
1421 drm_gem_object_unreference(obj);
1422 mutex_unlock(&dev->struct_mutex);
1423 return ret;
1424 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001425 }
1426
1427 drm_gem_object_unreference(obj);
1428 mutex_unlock(&dev->struct_mutex);
1429
1430 return 0;
1431}
1432
Ben Gamari6911a9b2009-04-02 11:24:54 -07001433void
Eric Anholt856fa192009-03-19 14:10:50 -07001434i915_gem_object_put_pages(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07001435{
Daniel Vetter23010e42010-03-08 13:35:02 +01001436 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001437 int page_count = obj->size / PAGE_SIZE;
1438 int i;
1439
Eric Anholt856fa192009-03-19 14:10:50 -07001440 BUG_ON(obj_priv->pages_refcount == 0);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001441 BUG_ON(obj_priv->madv == __I915_MADV_PURGED);
Eric Anholt856fa192009-03-19 14:10:50 -07001442
1443 if (--obj_priv->pages_refcount != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07001444 return;
1445
Eric Anholt280b7132009-03-12 16:56:27 -07001446 if (obj_priv->tiling_mode != I915_TILING_NONE)
1447 i915_gem_object_save_bit_17_swizzle(obj);
1448
Chris Wilson3ef94da2009-09-14 16:50:29 +01001449 if (obj_priv->madv == I915_MADV_DONTNEED)
Chris Wilson13a05fd2009-09-20 23:03:19 +01001450 obj_priv->dirty = 0;
Chris Wilson3ef94da2009-09-14 16:50:29 +01001451
1452 for (i = 0; i < page_count; i++) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01001453 if (obj_priv->dirty)
1454 set_page_dirty(obj_priv->pages[i]);
1455
1456 if (obj_priv->madv == I915_MADV_WILLNEED)
Eric Anholt856fa192009-03-19 14:10:50 -07001457 mark_page_accessed(obj_priv->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001458
1459 page_cache_release(obj_priv->pages[i]);
1460 }
Eric Anholt673a3942008-07-30 12:06:12 -07001461 obj_priv->dirty = 0;
1462
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07001463 drm_free_large(obj_priv->pages);
Eric Anholt856fa192009-03-19 14:10:50 -07001464 obj_priv->pages = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001465}
1466
1467static void
Zou Nan hai852835f2010-05-21 09:08:56 +08001468i915_gem_object_move_to_active(struct drm_gem_object *obj, uint32_t seqno,
1469 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001470{
1471 struct drm_device *dev = obj->dev;
1472 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001473 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Zou Nan hai852835f2010-05-21 09:08:56 +08001474 BUG_ON(ring == NULL);
1475 obj_priv->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001476
1477 /* Add a reference if we're newly entering the active list. */
1478 if (!obj_priv->active) {
1479 drm_gem_object_reference(obj);
1480 obj_priv->active = 1;
1481 }
1482 /* Move from whatever list we were on to the tail of execution. */
Carl Worth5e118f42009-03-20 11:54:25 -07001483 spin_lock(&dev_priv->mm.active_list_lock);
Zou Nan hai852835f2010-05-21 09:08:56 +08001484 list_move_tail(&obj_priv->list, &ring->active_list);
Carl Worth5e118f42009-03-20 11:54:25 -07001485 spin_unlock(&dev_priv->mm.active_list_lock);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001486 obj_priv->last_rendering_seqno = seqno;
Eric Anholt673a3942008-07-30 12:06:12 -07001487}
1488
Eric Anholtce44b0e2008-11-06 16:00:31 -08001489static void
1490i915_gem_object_move_to_flushing(struct drm_gem_object *obj)
1491{
1492 struct drm_device *dev = obj->dev;
1493 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001494 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001495
1496 BUG_ON(!obj_priv->active);
1497 list_move_tail(&obj_priv->list, &dev_priv->mm.flushing_list);
1498 obj_priv->last_rendering_seqno = 0;
1499}
Eric Anholt673a3942008-07-30 12:06:12 -07001500
Chris Wilson963b4832009-09-20 23:03:54 +01001501/* Immediately discard the backing storage */
1502static void
1503i915_gem_object_truncate(struct drm_gem_object *obj)
1504{
Daniel Vetter23010e42010-03-08 13:35:02 +01001505 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001506 struct inode *inode;
Chris Wilson963b4832009-09-20 23:03:54 +01001507
Chris Wilsonae9fed62010-08-07 11:01:30 +01001508 /* Our goal here is to return as much of the memory as
1509 * is possible back to the system as we are called from OOM.
1510 * To do this we must instruct the shmfs to drop all of its
1511 * backing pages, *now*. Here we mirror the actions taken
1512 * when by shmem_delete_inode() to release the backing store.
1513 */
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001514 inode = obj->filp->f_path.dentry->d_inode;
Chris Wilsonae9fed62010-08-07 11:01:30 +01001515 truncate_inode_pages(inode->i_mapping, 0);
1516 if (inode->i_op->truncate_range)
1517 inode->i_op->truncate_range(inode, 0, (loff_t)-1);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001518
1519 obj_priv->madv = __I915_MADV_PURGED;
Chris Wilson963b4832009-09-20 23:03:54 +01001520}
1521
1522static inline int
1523i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj_priv)
1524{
1525 return obj_priv->madv == I915_MADV_DONTNEED;
1526}
1527
Eric Anholt673a3942008-07-30 12:06:12 -07001528static void
1529i915_gem_object_move_to_inactive(struct drm_gem_object *obj)
1530{
1531 struct drm_device *dev = obj->dev;
1532 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001533 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001534
1535 i915_verify_inactive(dev, __FILE__, __LINE__);
1536 if (obj_priv->pin_count != 0)
1537 list_del_init(&obj_priv->list);
1538 else
1539 list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
1540
Daniel Vetter99fcb762010-02-07 16:20:18 +01001541 BUG_ON(!list_empty(&obj_priv->gpu_write_list));
1542
Eric Anholtce44b0e2008-11-06 16:00:31 -08001543 obj_priv->last_rendering_seqno = 0;
Zou Nan hai852835f2010-05-21 09:08:56 +08001544 obj_priv->ring = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001545 if (obj_priv->active) {
1546 obj_priv->active = 0;
1547 drm_gem_object_unreference(obj);
1548 }
1549 i915_verify_inactive(dev, __FILE__, __LINE__);
1550}
1551
Daniel Vetter63560392010-02-19 11:51:59 +01001552static void
1553i915_gem_process_flushing_list(struct drm_device *dev,
Zou Nan hai852835f2010-05-21 09:08:56 +08001554 uint32_t flush_domains, uint32_t seqno,
1555 struct intel_ring_buffer *ring)
Daniel Vetter63560392010-02-19 11:51:59 +01001556{
1557 drm_i915_private_t *dev_priv = dev->dev_private;
1558 struct drm_i915_gem_object *obj_priv, *next;
1559
1560 list_for_each_entry_safe(obj_priv, next,
1561 &dev_priv->mm.gpu_write_list,
1562 gpu_write_list) {
Daniel Vettera8089e82010-04-09 19:05:09 +00001563 struct drm_gem_object *obj = &obj_priv->base;
Daniel Vetter63560392010-02-19 11:51:59 +01001564
1565 if ((obj->write_domain & flush_domains) ==
Zou Nan hai852835f2010-05-21 09:08:56 +08001566 obj->write_domain &&
1567 obj_priv->ring->ring_flag == ring->ring_flag) {
Daniel Vetter63560392010-02-19 11:51:59 +01001568 uint32_t old_write_domain = obj->write_domain;
1569
1570 obj->write_domain = 0;
1571 list_del_init(&obj_priv->gpu_write_list);
Zou Nan hai852835f2010-05-21 09:08:56 +08001572 i915_gem_object_move_to_active(obj, seqno, ring);
Daniel Vetter63560392010-02-19 11:51:59 +01001573
1574 /* update the fence lru list */
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001575 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
1576 struct drm_i915_fence_reg *reg =
1577 &dev_priv->fence_regs[obj_priv->fence_reg];
1578 list_move_tail(&reg->lru_list,
Daniel Vetter63560392010-02-19 11:51:59 +01001579 &dev_priv->mm.fence_list);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001580 }
Daniel Vetter63560392010-02-19 11:51:59 +01001581
1582 trace_i915_gem_object_change_domain(obj,
1583 obj->read_domains,
1584 old_write_domain);
1585 }
1586 }
1587}
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001588
Daniel Vetter5a5a0c62009-09-15 22:57:36 +02001589uint32_t
Eric Anholtb9624422009-06-03 07:27:35 +00001590i915_add_request(struct drm_device *dev, struct drm_file *file_priv,
Zou Nan hai852835f2010-05-21 09:08:56 +08001591 uint32_t flush_domains, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001592{
1593 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtb9624422009-06-03 07:27:35 +00001594 struct drm_i915_file_private *i915_file_priv = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001595 struct drm_i915_gem_request *request;
1596 uint32_t seqno;
1597 int was_empty;
Eric Anholt673a3942008-07-30 12:06:12 -07001598
Eric Anholtb9624422009-06-03 07:27:35 +00001599 if (file_priv != NULL)
1600 i915_file_priv = file_priv->driver_priv;
1601
Eric Anholt9a298b22009-03-24 12:23:04 -07001602 request = kzalloc(sizeof(*request), GFP_KERNEL);
Eric Anholt673a3942008-07-30 12:06:12 -07001603 if (request == NULL)
1604 return 0;
1605
Zou Nan hai852835f2010-05-21 09:08:56 +08001606 seqno = ring->add_request(dev, ring, file_priv, flush_domains);
Eric Anholt673a3942008-07-30 12:06:12 -07001607
1608 request->seqno = seqno;
Zou Nan hai852835f2010-05-21 09:08:56 +08001609 request->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001610 request->emitted_jiffies = jiffies;
Zou Nan hai852835f2010-05-21 09:08:56 +08001611 was_empty = list_empty(&ring->request_list);
1612 list_add_tail(&request->list, &ring->request_list);
1613
Eric Anholtb9624422009-06-03 07:27:35 +00001614 if (i915_file_priv) {
1615 list_add_tail(&request->client_list,
1616 &i915_file_priv->mm.request_list);
1617 } else {
1618 INIT_LIST_HEAD(&request->client_list);
1619 }
Eric Anholt673a3942008-07-30 12:06:12 -07001620
Eric Anholtce44b0e2008-11-06 16:00:31 -08001621 /* Associate any objects on the flushing list matching the write
1622 * domain we're flushing with our flush.
1623 */
Daniel Vetter63560392010-02-19 11:51:59 +01001624 if (flush_domains != 0)
Zou Nan hai852835f2010-05-21 09:08:56 +08001625 i915_gem_process_flushing_list(dev, flush_domains, seqno, ring);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001626
Ben Gamarif65d9422009-09-14 17:48:44 -04001627 if (!dev_priv->mm.suspended) {
1628 mod_timer(&dev_priv->hangcheck_timer, jiffies + DRM_I915_HANGCHECK_PERIOD);
1629 if (was_empty)
1630 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
1631 }
Eric Anholt673a3942008-07-30 12:06:12 -07001632 return seqno;
1633}
1634
1635/**
1636 * Command execution barrier
1637 *
1638 * Ensures that all commands in the ring are finished
1639 * before signalling the CPU
1640 */
Eric Anholt3043c602008-10-02 12:24:47 -07001641static uint32_t
Zou Nan hai852835f2010-05-21 09:08:56 +08001642i915_retire_commands(struct drm_device *dev, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001643{
Eric Anholt673a3942008-07-30 12:06:12 -07001644 uint32_t flush_domains = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001645
1646 /* The sampler always gets flushed on i965 (sigh) */
1647 if (IS_I965G(dev))
1648 flush_domains |= I915_GEM_DOMAIN_SAMPLER;
Zou Nan hai852835f2010-05-21 09:08:56 +08001649
1650 ring->flush(dev, ring,
1651 I915_GEM_DOMAIN_COMMAND, flush_domains);
Eric Anholt673a3942008-07-30 12:06:12 -07001652 return flush_domains;
1653}
1654
1655/**
1656 * Moves buffers associated only with the given active seqno from the active
1657 * to inactive list, potentially freeing them.
1658 */
1659static void
1660i915_gem_retire_request(struct drm_device *dev,
1661 struct drm_i915_gem_request *request)
1662{
1663 drm_i915_private_t *dev_priv = dev->dev_private;
1664
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001665 trace_i915_gem_request_retire(dev, request->seqno);
1666
Eric Anholt673a3942008-07-30 12:06:12 -07001667 /* Move any buffers on the active list that are no longer referenced
1668 * by the ringbuffer to the flushing/inactive lists as appropriate.
1669 */
Carl Worth5e118f42009-03-20 11:54:25 -07001670 spin_lock(&dev_priv->mm.active_list_lock);
Zou Nan hai852835f2010-05-21 09:08:56 +08001671 while (!list_empty(&request->ring->active_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001672 struct drm_gem_object *obj;
1673 struct drm_i915_gem_object *obj_priv;
1674
Zou Nan hai852835f2010-05-21 09:08:56 +08001675 obj_priv = list_first_entry(&request->ring->active_list,
Eric Anholt673a3942008-07-30 12:06:12 -07001676 struct drm_i915_gem_object,
1677 list);
Daniel Vettera8089e82010-04-09 19:05:09 +00001678 obj = &obj_priv->base;
Eric Anholt673a3942008-07-30 12:06:12 -07001679
1680 /* If the seqno being retired doesn't match the oldest in the
1681 * list, then the oldest in the list must still be newer than
1682 * this seqno.
1683 */
1684 if (obj_priv->last_rendering_seqno != request->seqno)
Carl Worth5e118f42009-03-20 11:54:25 -07001685 goto out;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001686
Eric Anholt673a3942008-07-30 12:06:12 -07001687#if WATCH_LRU
1688 DRM_INFO("%s: retire %d moves to inactive list %p\n",
1689 __func__, request->seqno, obj);
1690#endif
1691
Eric Anholtce44b0e2008-11-06 16:00:31 -08001692 if (obj->write_domain != 0)
1693 i915_gem_object_move_to_flushing(obj);
Shaohua Li68c84342009-04-08 10:58:23 +08001694 else {
1695 /* Take a reference on the object so it won't be
1696 * freed while the spinlock is held. The list
1697 * protection for this spinlock is safe when breaking
1698 * the lock like this since the next thing we do
1699 * is just get the head of the list again.
1700 */
1701 drm_gem_object_reference(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001702 i915_gem_object_move_to_inactive(obj);
Shaohua Li68c84342009-04-08 10:58:23 +08001703 spin_unlock(&dev_priv->mm.active_list_lock);
1704 drm_gem_object_unreference(obj);
1705 spin_lock(&dev_priv->mm.active_list_lock);
1706 }
Eric Anholt673a3942008-07-30 12:06:12 -07001707 }
Carl Worth5e118f42009-03-20 11:54:25 -07001708out:
1709 spin_unlock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001710}
1711
1712/**
1713 * Returns true if seq1 is later than seq2.
1714 */
Ben Gamari22be1722009-09-14 17:48:43 -04001715bool
Eric Anholt673a3942008-07-30 12:06:12 -07001716i915_seqno_passed(uint32_t seq1, uint32_t seq2)
1717{
1718 return (int32_t)(seq1 - seq2) >= 0;
1719}
1720
1721uint32_t
Zou Nan hai852835f2010-05-21 09:08:56 +08001722i915_get_gem_seqno(struct drm_device *dev,
Zou Nan haid1b851f2010-05-21 09:08:57 +08001723 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001724{
Zou Nan hai852835f2010-05-21 09:08:56 +08001725 return ring->get_gem_seqno(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07001726}
1727
1728/**
1729 * This function clears the request list as sequence numbers are passed.
1730 */
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001731static void
1732i915_gem_retire_requests_ring(struct drm_device *dev,
1733 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001734{
1735 drm_i915_private_t *dev_priv = dev->dev_private;
1736 uint32_t seqno;
1737
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001738 if (!ring->status_page.page_addr
Zou Nan hai852835f2010-05-21 09:08:56 +08001739 || list_empty(&ring->request_list))
Karsten Wiese6c0594a2009-02-23 15:07:57 +01001740 return;
1741
Zou Nan hai852835f2010-05-21 09:08:56 +08001742 seqno = i915_get_gem_seqno(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07001743
Zou Nan hai852835f2010-05-21 09:08:56 +08001744 while (!list_empty(&ring->request_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001745 struct drm_i915_gem_request *request;
1746 uint32_t retiring_seqno;
1747
Zou Nan hai852835f2010-05-21 09:08:56 +08001748 request = list_first_entry(&ring->request_list,
Eric Anholt673a3942008-07-30 12:06:12 -07001749 struct drm_i915_gem_request,
1750 list);
1751 retiring_seqno = request->seqno;
1752
1753 if (i915_seqno_passed(seqno, retiring_seqno) ||
Ben Gamariba1234d2009-09-14 17:48:47 -04001754 atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001755 i915_gem_retire_request(dev, request);
1756
1757 list_del(&request->list);
Eric Anholtb9624422009-06-03 07:27:35 +00001758 list_del(&request->client_list);
Eric Anholt9a298b22009-03-24 12:23:04 -07001759 kfree(request);
Eric Anholt673a3942008-07-30 12:06:12 -07001760 } else
1761 break;
1762 }
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001763
1764 if (unlikely (dev_priv->trace_irq_seqno &&
1765 i915_seqno_passed(dev_priv->trace_irq_seqno, seqno))) {
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001766
1767 ring->user_irq_put(dev, ring);
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001768 dev_priv->trace_irq_seqno = 0;
1769 }
Eric Anholt673a3942008-07-30 12:06:12 -07001770}
1771
1772void
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001773i915_gem_retire_requests(struct drm_device *dev)
1774{
1775 drm_i915_private_t *dev_priv = dev->dev_private;
1776
Chris Wilsonbe726152010-07-23 23:18:50 +01001777 if (!list_empty(&dev_priv->mm.deferred_free_list)) {
1778 struct drm_i915_gem_object *obj_priv, *tmp;
1779
1780 /* We must be careful that during unbind() we do not
1781 * accidentally infinitely recurse into retire requests.
1782 * Currently:
1783 * retire -> free -> unbind -> wait -> retire_ring
1784 */
1785 list_for_each_entry_safe(obj_priv, tmp,
1786 &dev_priv->mm.deferred_free_list,
1787 list)
1788 i915_gem_free_object_tail(&obj_priv->base);
1789 }
1790
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001791 i915_gem_retire_requests_ring(dev, &dev_priv->render_ring);
1792 if (HAS_BSD(dev))
1793 i915_gem_retire_requests_ring(dev, &dev_priv->bsd_ring);
1794}
1795
1796void
Eric Anholt673a3942008-07-30 12:06:12 -07001797i915_gem_retire_work_handler(struct work_struct *work)
1798{
1799 drm_i915_private_t *dev_priv;
1800 struct drm_device *dev;
1801
1802 dev_priv = container_of(work, drm_i915_private_t,
1803 mm.retire_work.work);
1804 dev = dev_priv->dev;
1805
1806 mutex_lock(&dev->struct_mutex);
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001807 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08001808
Keith Packard6dbe2772008-10-14 21:41:13 -07001809 if (!dev_priv->mm.suspended &&
Zou Nan haid1b851f2010-05-21 09:08:57 +08001810 (!list_empty(&dev_priv->render_ring.request_list) ||
1811 (HAS_BSD(dev) &&
1812 !list_empty(&dev_priv->bsd_ring.request_list))))
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001813 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
Eric Anholt673a3942008-07-30 12:06:12 -07001814 mutex_unlock(&dev->struct_mutex);
1815}
1816
Daniel Vetter5a5a0c62009-09-15 22:57:36 +02001817int
Zou Nan hai852835f2010-05-21 09:08:56 +08001818i915_do_wait_request(struct drm_device *dev, uint32_t seqno,
1819 int interruptible, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001820{
1821 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001822 u32 ier;
Eric Anholt673a3942008-07-30 12:06:12 -07001823 int ret = 0;
1824
1825 BUG_ON(seqno == 0);
1826
Ben Gamariba1234d2009-09-14 17:48:47 -04001827 if (atomic_read(&dev_priv->mm.wedged))
Ben Gamariffed1d02009-09-14 17:48:41 -04001828 return -EIO;
1829
Zou Nan hai852835f2010-05-21 09:08:56 +08001830 if (!i915_seqno_passed(ring->get_gem_seqno(dev, ring), seqno)) {
Eric Anholtbad720f2009-10-22 16:11:14 -07001831 if (HAS_PCH_SPLIT(dev))
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001832 ier = I915_READ(DEIER) | I915_READ(GTIER);
1833 else
1834 ier = I915_READ(IER);
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001835 if (!ier) {
1836 DRM_ERROR("something (likely vbetool) disabled "
1837 "interrupts, re-enabling\n");
1838 i915_driver_irq_preinstall(dev);
1839 i915_driver_irq_postinstall(dev);
1840 }
1841
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001842 trace_i915_gem_request_wait_begin(dev, seqno);
1843
Zou Nan hai852835f2010-05-21 09:08:56 +08001844 ring->waiting_gem_seqno = seqno;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001845 ring->user_irq_get(dev, ring);
Daniel Vetter48764bf2009-09-15 22:57:32 +02001846 if (interruptible)
Zou Nan hai852835f2010-05-21 09:08:56 +08001847 ret = wait_event_interruptible(ring->irq_queue,
1848 i915_seqno_passed(
1849 ring->get_gem_seqno(dev, ring), seqno)
1850 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02001851 else
Zou Nan hai852835f2010-05-21 09:08:56 +08001852 wait_event(ring->irq_queue,
1853 i915_seqno_passed(
1854 ring->get_gem_seqno(dev, ring), seqno)
1855 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02001856
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001857 ring->user_irq_put(dev, ring);
Zou Nan hai852835f2010-05-21 09:08:56 +08001858 ring->waiting_gem_seqno = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001859
1860 trace_i915_gem_request_wait_end(dev, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07001861 }
Ben Gamariba1234d2009-09-14 17:48:47 -04001862 if (atomic_read(&dev_priv->mm.wedged))
Eric Anholt673a3942008-07-30 12:06:12 -07001863 ret = -EIO;
1864
1865 if (ret && ret != -ERESTARTSYS)
1866 DRM_ERROR("%s returns %d (awaiting %d at %d)\n",
Zou Nan hai852835f2010-05-21 09:08:56 +08001867 __func__, ret, seqno, ring->get_gem_seqno(dev, ring));
Eric Anholt673a3942008-07-30 12:06:12 -07001868
1869 /* Directly dispatch request retiring. While we have the work queue
1870 * to handle this, the waiter on a request often wants an associated
1871 * buffer to have made it to the inactive list, and we would need
1872 * a separate wait queue to handle that.
1873 */
1874 if (ret == 0)
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001875 i915_gem_retire_requests_ring(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07001876
1877 return ret;
1878}
1879
Daniel Vetter48764bf2009-09-15 22:57:32 +02001880/**
1881 * Waits for a sequence number to be signaled, and cleans up the
1882 * request and object lists appropriately for that event.
1883 */
1884static int
Zou Nan hai852835f2010-05-21 09:08:56 +08001885i915_wait_request(struct drm_device *dev, uint32_t seqno,
1886 struct intel_ring_buffer *ring)
Daniel Vetter48764bf2009-09-15 22:57:32 +02001887{
Zou Nan hai852835f2010-05-21 09:08:56 +08001888 return i915_do_wait_request(dev, seqno, 1, ring);
Daniel Vetter48764bf2009-09-15 22:57:32 +02001889}
1890
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001891static void
1892i915_gem_flush(struct drm_device *dev,
1893 uint32_t invalidate_domains,
1894 uint32_t flush_domains)
1895{
1896 drm_i915_private_t *dev_priv = dev->dev_private;
1897 if (flush_domains & I915_GEM_DOMAIN_CPU)
1898 drm_agp_chipset_flush(dev);
1899 dev_priv->render_ring.flush(dev, &dev_priv->render_ring,
1900 invalidate_domains,
1901 flush_domains);
Zou Nan haid1b851f2010-05-21 09:08:57 +08001902
1903 if (HAS_BSD(dev))
1904 dev_priv->bsd_ring.flush(dev, &dev_priv->bsd_ring,
1905 invalidate_domains,
1906 flush_domains);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001907}
1908
Eric Anholt673a3942008-07-30 12:06:12 -07001909/**
1910 * Ensures that all rendering to the object has completed and the object is
1911 * safe to unbind from the GTT or access from the CPU.
1912 */
1913static int
1914i915_gem_object_wait_rendering(struct drm_gem_object *obj)
1915{
1916 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001917 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001918 int ret;
1919
Eric Anholte47c68e2008-11-14 13:35:19 -08001920 /* This function only exists to support waiting for existing rendering,
1921 * not for emitting required flushes.
Eric Anholt673a3942008-07-30 12:06:12 -07001922 */
Eric Anholte47c68e2008-11-14 13:35:19 -08001923 BUG_ON((obj->write_domain & I915_GEM_GPU_DOMAINS) != 0);
Eric Anholt673a3942008-07-30 12:06:12 -07001924
1925 /* If there is rendering queued on the buffer being evicted, wait for
1926 * it.
1927 */
1928 if (obj_priv->active) {
1929#if WATCH_BUF
1930 DRM_INFO("%s: object %p wait for seqno %08x\n",
1931 __func__, obj, obj_priv->last_rendering_seqno);
1932#endif
Zou Nan hai852835f2010-05-21 09:08:56 +08001933 ret = i915_wait_request(dev,
1934 obj_priv->last_rendering_seqno, obj_priv->ring);
Eric Anholt673a3942008-07-30 12:06:12 -07001935 if (ret != 0)
1936 return ret;
1937 }
1938
1939 return 0;
1940}
1941
1942/**
1943 * Unbinds an object from the GTT aperture.
1944 */
Jesse Barnes0f973f22009-01-26 17:10:45 -08001945int
Eric Anholt673a3942008-07-30 12:06:12 -07001946i915_gem_object_unbind(struct drm_gem_object *obj)
1947{
1948 struct drm_device *dev = obj->dev;
Daniel Vetter4a87b8c2010-02-19 11:51:57 +01001949 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001950 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001951 int ret = 0;
1952
1953#if WATCH_BUF
1954 DRM_INFO("%s:%d %p\n", __func__, __LINE__, obj);
1955 DRM_INFO("gtt_space %p\n", obj_priv->gtt_space);
1956#endif
1957 if (obj_priv->gtt_space == NULL)
1958 return 0;
1959
1960 if (obj_priv->pin_count != 0) {
1961 DRM_ERROR("Attempting to unbind pinned buffer\n");
1962 return -EINVAL;
1963 }
1964
Eric Anholt5323fd02009-09-09 11:50:45 -07001965 /* blow away mappings if mapped through GTT */
1966 i915_gem_release_mmap(obj);
1967
Eric Anholt673a3942008-07-30 12:06:12 -07001968 /* Move the object to the CPU domain to ensure that
1969 * any possible CPU writes while it's not in the GTT
1970 * are flushed when we go to remap it. This will
1971 * also ensure that all pending GPU writes are finished
1972 * before we unbind.
1973 */
Eric Anholte47c68e2008-11-14 13:35:19 -08001974 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Chris Wilson8dc17752010-07-23 23:18:51 +01001975 if (ret == -ERESTARTSYS)
Eric Anholt673a3942008-07-30 12:06:12 -07001976 return ret;
Chris Wilson8dc17752010-07-23 23:18:51 +01001977 /* Continue on if we fail due to EIO, the GPU is hung so we
1978 * should be safe and we need to cleanup or else we might
1979 * cause memory corruption through use-after-free.
1980 */
Eric Anholt673a3942008-07-30 12:06:12 -07001981
Eric Anholt5323fd02009-09-09 11:50:45 -07001982 BUG_ON(obj_priv->active);
1983
Daniel Vetter96b47b62009-12-15 17:50:00 +01001984 /* release the fence reg _after_ flushing */
1985 if (obj_priv->fence_reg != I915_FENCE_REG_NONE)
1986 i915_gem_clear_fence_reg(obj);
1987
Eric Anholt673a3942008-07-30 12:06:12 -07001988 if (obj_priv->agp_mem != NULL) {
1989 drm_unbind_agp(obj_priv->agp_mem);
1990 drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE);
1991 obj_priv->agp_mem = NULL;
1992 }
1993
Eric Anholt856fa192009-03-19 14:10:50 -07001994 i915_gem_object_put_pages(obj);
Chris Wilsona32808c2009-09-20 21:29:47 +01001995 BUG_ON(obj_priv->pages_refcount);
Eric Anholt673a3942008-07-30 12:06:12 -07001996
1997 if (obj_priv->gtt_space) {
1998 atomic_dec(&dev->gtt_count);
1999 atomic_sub(obj->size, &dev->gtt_memory);
2000
2001 drm_mm_put_block(obj_priv->gtt_space);
2002 obj_priv->gtt_space = NULL;
2003 }
2004
2005 /* Remove ourselves from the LRU list if present. */
Daniel Vetter4a87b8c2010-02-19 11:51:57 +01002006 spin_lock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07002007 if (!list_empty(&obj_priv->list))
2008 list_del_init(&obj_priv->list);
Daniel Vetter4a87b8c2010-02-19 11:51:57 +01002009 spin_unlock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07002010
Chris Wilson963b4832009-09-20 23:03:54 +01002011 if (i915_gem_object_is_purgeable(obj_priv))
2012 i915_gem_object_truncate(obj);
2013
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002014 trace_i915_gem_object_unbind(obj);
2015
Chris Wilson8dc17752010-07-23 23:18:51 +01002016 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002017}
2018
Chris Wilsonb47eb4a2010-08-07 11:01:23 +01002019int
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002020i915_gpu_idle(struct drm_device *dev)
2021{
2022 drm_i915_private_t *dev_priv = dev->dev_private;
2023 bool lists_empty;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002024 uint32_t seqno1, seqno2;
Zou Nan hai852835f2010-05-21 09:08:56 +08002025 int ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002026
2027 spin_lock(&dev_priv->mm.active_list_lock);
Zou Nan haid1b851f2010-05-21 09:08:57 +08002028 lists_empty = (list_empty(&dev_priv->mm.flushing_list) &&
2029 list_empty(&dev_priv->render_ring.active_list) &&
2030 (!HAS_BSD(dev) ||
2031 list_empty(&dev_priv->bsd_ring.active_list)));
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002032 spin_unlock(&dev_priv->mm.active_list_lock);
2033
2034 if (lists_empty)
2035 return 0;
2036
2037 /* Flush everything onto the inactive list. */
2038 i915_gem_flush(dev, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
Zou Nan haid1b851f2010-05-21 09:08:57 +08002039 seqno1 = i915_add_request(dev, NULL, I915_GEM_GPU_DOMAINS,
Zou Nan hai852835f2010-05-21 09:08:56 +08002040 &dev_priv->render_ring);
Zou Nan haid1b851f2010-05-21 09:08:57 +08002041 if (seqno1 == 0)
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002042 return -ENOMEM;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002043 ret = i915_wait_request(dev, seqno1, &dev_priv->render_ring);
2044
2045 if (HAS_BSD(dev)) {
2046 seqno2 = i915_add_request(dev, NULL, I915_GEM_GPU_DOMAINS,
2047 &dev_priv->bsd_ring);
2048 if (seqno2 == 0)
2049 return -ENOMEM;
2050
2051 ret = i915_wait_request(dev, seqno2, &dev_priv->bsd_ring);
2052 if (ret)
2053 return ret;
2054 }
2055
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002056
Zou Nan hai852835f2010-05-21 09:08:56 +08002057 return ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002058}
2059
Ben Gamari6911a9b2009-04-02 11:24:54 -07002060int
Chris Wilson4bdadb92010-01-27 13:36:32 +00002061i915_gem_object_get_pages(struct drm_gem_object *obj,
2062 gfp_t gfpmask)
Eric Anholt673a3942008-07-30 12:06:12 -07002063{
Daniel Vetter23010e42010-03-08 13:35:02 +01002064 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002065 int page_count, i;
2066 struct address_space *mapping;
2067 struct inode *inode;
2068 struct page *page;
Eric Anholt673a3942008-07-30 12:06:12 -07002069
Daniel Vetter778c3542010-05-13 11:49:44 +02002070 BUG_ON(obj_priv->pages_refcount
2071 == DRM_I915_GEM_OBJECT_MAX_PAGES_REFCOUNT);
2072
Eric Anholt856fa192009-03-19 14:10:50 -07002073 if (obj_priv->pages_refcount++ != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07002074 return 0;
2075
2076 /* Get the list of pages out of our struct file. They'll be pinned
2077 * at this point until we release them.
2078 */
2079 page_count = obj->size / PAGE_SIZE;
Eric Anholt856fa192009-03-19 14:10:50 -07002080 BUG_ON(obj_priv->pages != NULL);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07002081 obj_priv->pages = drm_calloc_large(page_count, sizeof(struct page *));
Eric Anholt856fa192009-03-19 14:10:50 -07002082 if (obj_priv->pages == NULL) {
Eric Anholt856fa192009-03-19 14:10:50 -07002083 obj_priv->pages_refcount--;
Eric Anholt673a3942008-07-30 12:06:12 -07002084 return -ENOMEM;
2085 }
2086
2087 inode = obj->filp->f_path.dentry->d_inode;
2088 mapping = inode->i_mapping;
2089 for (i = 0; i < page_count; i++) {
Chris Wilson4bdadb92010-01-27 13:36:32 +00002090 page = read_cache_page_gfp(mapping, i,
Linus Torvalds985b8232010-07-02 10:04:42 +10002091 GFP_HIGHUSER |
Chris Wilson4bdadb92010-01-27 13:36:32 +00002092 __GFP_COLD |
Linus Torvaldscd9f0402010-07-18 09:44:37 -07002093 __GFP_RECLAIMABLE |
Chris Wilson4bdadb92010-01-27 13:36:32 +00002094 gfpmask);
Chris Wilson1f2b1012010-03-12 19:52:55 +00002095 if (IS_ERR(page))
2096 goto err_pages;
2097
Eric Anholt856fa192009-03-19 14:10:50 -07002098 obj_priv->pages[i] = page;
Eric Anholt673a3942008-07-30 12:06:12 -07002099 }
Eric Anholt280b7132009-03-12 16:56:27 -07002100
2101 if (obj_priv->tiling_mode != I915_TILING_NONE)
2102 i915_gem_object_do_bit_17_swizzle(obj);
2103
Eric Anholt673a3942008-07-30 12:06:12 -07002104 return 0;
Chris Wilson1f2b1012010-03-12 19:52:55 +00002105
2106err_pages:
2107 while (i--)
2108 page_cache_release(obj_priv->pages[i]);
2109
2110 drm_free_large(obj_priv->pages);
2111 obj_priv->pages = NULL;
2112 obj_priv->pages_refcount--;
2113 return PTR_ERR(page);
Eric Anholt673a3942008-07-30 12:06:12 -07002114}
2115
Eric Anholt4e901fd2009-10-26 16:44:17 -07002116static void sandybridge_write_fence_reg(struct drm_i915_fence_reg *reg)
2117{
2118 struct drm_gem_object *obj = reg->obj;
2119 struct drm_device *dev = obj->dev;
2120 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002121 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt4e901fd2009-10-26 16:44:17 -07002122 int regnum = obj_priv->fence_reg;
2123 uint64_t val;
2124
2125 val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
2126 0xfffff000) << 32;
2127 val |= obj_priv->gtt_offset & 0xfffff000;
2128 val |= (uint64_t)((obj_priv->stride / 128) - 1) <<
2129 SANDYBRIDGE_FENCE_PITCH_SHIFT;
2130
2131 if (obj_priv->tiling_mode == I915_TILING_Y)
2132 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2133 val |= I965_FENCE_REG_VALID;
2134
2135 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (regnum * 8), val);
2136}
2137
Jesse Barnesde151cf2008-11-12 10:03:55 -08002138static void i965_write_fence_reg(struct drm_i915_fence_reg *reg)
2139{
2140 struct drm_gem_object *obj = reg->obj;
2141 struct drm_device *dev = obj->dev;
2142 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002143 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002144 int regnum = obj_priv->fence_reg;
2145 uint64_t val;
2146
2147 val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
2148 0xfffff000) << 32;
2149 val |= obj_priv->gtt_offset & 0xfffff000;
2150 val |= ((obj_priv->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
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_965_0 + (regnum * 8), val);
2156}
2157
2158static void i915_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;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002165 int tile_width;
Eric Anholtdc529a42009-03-10 22:34:49 -07002166 uint32_t fence_reg, val;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002167 uint32_t pitch_val;
2168
2169 if ((obj_priv->gtt_offset & ~I915_FENCE_START_MASK) ||
2170 (obj_priv->gtt_offset & (obj->size - 1))) {
Linus Torvaldsf06da262009-02-09 08:57:29 -08002171 WARN(1, "%s: object 0x%08x not 1M or size (0x%zx) aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002172 __func__, obj_priv->gtt_offset, obj->size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002173 return;
2174 }
2175
Jesse Barnes0f973f22009-01-26 17:10:45 -08002176 if (obj_priv->tiling_mode == I915_TILING_Y &&
2177 HAS_128_BYTE_Y_TILING(dev))
2178 tile_width = 128;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002179 else
Jesse Barnes0f973f22009-01-26 17:10:45 -08002180 tile_width = 512;
2181
2182 /* Note: pitch better be a power of two tile widths */
2183 pitch_val = obj_priv->stride / tile_width;
2184 pitch_val = ffs(pitch_val) - 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002185
Daniel Vetterc36a2a62010-04-17 15:12:03 +02002186 if (obj_priv->tiling_mode == I915_TILING_Y &&
2187 HAS_128_BYTE_Y_TILING(dev))
2188 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2189 else
2190 WARN_ON(pitch_val > I915_FENCE_MAX_PITCH_VAL);
2191
Jesse Barnesde151cf2008-11-12 10:03:55 -08002192 val = obj_priv->gtt_offset;
2193 if (obj_priv->tiling_mode == I915_TILING_Y)
2194 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2195 val |= I915_FENCE_SIZE_BITS(obj->size);
2196 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2197 val |= I830_FENCE_REG_VALID;
2198
Eric Anholtdc529a42009-03-10 22:34:49 -07002199 if (regnum < 8)
2200 fence_reg = FENCE_REG_830_0 + (regnum * 4);
2201 else
2202 fence_reg = FENCE_REG_945_8 + ((regnum - 8) * 4);
2203 I915_WRITE(fence_reg, val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002204}
2205
2206static void i830_write_fence_reg(struct drm_i915_fence_reg *reg)
2207{
2208 struct drm_gem_object *obj = reg->obj;
2209 struct drm_device *dev = obj->dev;
2210 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002211 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002212 int regnum = obj_priv->fence_reg;
2213 uint32_t val;
2214 uint32_t pitch_val;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002215 uint32_t fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002216
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002217 if ((obj_priv->gtt_offset & ~I830_FENCE_START_MASK) ||
Jesse Barnesde151cf2008-11-12 10:03:55 -08002218 (obj_priv->gtt_offset & (obj->size - 1))) {
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002219 WARN(1, "%s: object 0x%08x not 512K or size aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002220 __func__, obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002221 return;
2222 }
2223
Eric Anholte76a16d2009-05-26 17:44:56 -07002224 pitch_val = obj_priv->stride / 128;
2225 pitch_val = ffs(pitch_val) - 1;
2226 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2227
Jesse Barnesde151cf2008-11-12 10:03:55 -08002228 val = obj_priv->gtt_offset;
2229 if (obj_priv->tiling_mode == I915_TILING_Y)
2230 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002231 fence_size_bits = I830_FENCE_SIZE_BITS(obj->size);
2232 WARN_ON(fence_size_bits & ~0x00000f00);
2233 val |= fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002234 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2235 val |= I830_FENCE_REG_VALID;
2236
2237 I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002238}
2239
Daniel Vetterae3db242010-02-19 11:51:58 +01002240static int i915_find_fence_reg(struct drm_device *dev)
2241{
2242 struct drm_i915_fence_reg *reg = NULL;
2243 struct drm_i915_gem_object *obj_priv = NULL;
2244 struct drm_i915_private *dev_priv = dev->dev_private;
2245 struct drm_gem_object *obj = NULL;
2246 int i, avail, ret;
2247
2248 /* First try to find a free reg */
2249 avail = 0;
2250 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2251 reg = &dev_priv->fence_regs[i];
2252 if (!reg->obj)
2253 return i;
2254
Daniel Vetter23010e42010-03-08 13:35:02 +01002255 obj_priv = to_intel_bo(reg->obj);
Daniel Vetterae3db242010-02-19 11:51:58 +01002256 if (!obj_priv->pin_count)
2257 avail++;
2258 }
2259
2260 if (avail == 0)
2261 return -ENOSPC;
2262
2263 /* None available, try to steal one or wait for a user to finish */
2264 i = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002265 list_for_each_entry(reg, &dev_priv->mm.fence_list,
2266 lru_list) {
2267 obj = reg->obj;
2268 obj_priv = to_intel_bo(obj);
Daniel Vetterae3db242010-02-19 11:51:58 +01002269
2270 if (obj_priv->pin_count)
2271 continue;
2272
2273 /* found one! */
2274 i = obj_priv->fence_reg;
2275 break;
2276 }
2277
2278 BUG_ON(i == I915_FENCE_REG_NONE);
2279
2280 /* We only have a reference on obj from the active list. put_fence_reg
2281 * might drop that one, causing a use-after-free in it. So hold a
2282 * private reference to obj like the other callers of put_fence_reg
2283 * (set_tiling ioctl) do. */
2284 drm_gem_object_reference(obj);
2285 ret = i915_gem_object_put_fence_reg(obj);
2286 drm_gem_object_unreference(obj);
2287 if (ret != 0)
2288 return ret;
2289
2290 return i;
2291}
2292
Jesse Barnesde151cf2008-11-12 10:03:55 -08002293/**
2294 * i915_gem_object_get_fence_reg - set up a fence reg for an object
2295 * @obj: object to map through a fence reg
2296 *
2297 * When mapping objects through the GTT, userspace wants to be able to write
2298 * to them without having to worry about swizzling if the object is tiled.
2299 *
2300 * This function walks the fence regs looking for a free one for @obj,
2301 * stealing one if it can't find any.
2302 *
2303 * It then sets up the reg based on the object's properties: address, pitch
2304 * and tiling format.
2305 */
Chris Wilson8c4b8c32009-06-17 22:08:52 +01002306int
2307i915_gem_object_get_fence_reg(struct drm_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002308{
2309 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002310 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002311 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002312 struct drm_i915_fence_reg *reg = NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002313 int ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002314
Eric Anholta09ba7f2009-08-29 12:49:51 -07002315 /* Just update our place in the LRU if our fence is getting used. */
2316 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002317 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
2318 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002319 return 0;
2320 }
2321
Jesse Barnesde151cf2008-11-12 10:03:55 -08002322 switch (obj_priv->tiling_mode) {
2323 case I915_TILING_NONE:
2324 WARN(1, "allocating a fence for non-tiled object?\n");
2325 break;
2326 case I915_TILING_X:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002327 if (!obj_priv->stride)
2328 return -EINVAL;
2329 WARN((obj_priv->stride & (512 - 1)),
2330 "object 0x%08x is X tiled but has non-512B pitch\n",
2331 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002332 break;
2333 case I915_TILING_Y:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002334 if (!obj_priv->stride)
2335 return -EINVAL;
2336 WARN((obj_priv->stride & (128 - 1)),
2337 "object 0x%08x is Y tiled but has non-128B pitch\n",
2338 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002339 break;
2340 }
2341
Daniel Vetterae3db242010-02-19 11:51:58 +01002342 ret = i915_find_fence_reg(dev);
2343 if (ret < 0)
2344 return ret;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002345
Daniel Vetterae3db242010-02-19 11:51:58 +01002346 obj_priv->fence_reg = ret;
2347 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002348 list_add_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002349
Jesse Barnesde151cf2008-11-12 10:03:55 -08002350 reg->obj = obj;
2351
Eric Anholt4e901fd2009-10-26 16:44:17 -07002352 if (IS_GEN6(dev))
2353 sandybridge_write_fence_reg(reg);
2354 else if (IS_I965G(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08002355 i965_write_fence_reg(reg);
2356 else if (IS_I9XX(dev))
2357 i915_write_fence_reg(reg);
2358 else
2359 i830_write_fence_reg(reg);
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002360
Daniel Vetterae3db242010-02-19 11:51:58 +01002361 trace_i915_gem_object_get_fence(obj, obj_priv->fence_reg,
2362 obj_priv->tiling_mode);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002363
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002364 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002365}
2366
2367/**
2368 * i915_gem_clear_fence_reg - clear out fence register info
2369 * @obj: object to clear
2370 *
2371 * Zeroes out the fence register itself and clears out the associated
2372 * data structures in dev_priv and obj_priv.
2373 */
2374static void
2375i915_gem_clear_fence_reg(struct drm_gem_object *obj)
2376{
2377 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002378 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002379 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002380 struct drm_i915_fence_reg *reg =
2381 &dev_priv->fence_regs[obj_priv->fence_reg];
Jesse Barnesde151cf2008-11-12 10:03:55 -08002382
Eric Anholt4e901fd2009-10-26 16:44:17 -07002383 if (IS_GEN6(dev)) {
2384 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 +
2385 (obj_priv->fence_reg * 8), 0);
2386 } else if (IS_I965G(dev)) {
Jesse Barnesde151cf2008-11-12 10:03:55 -08002387 I915_WRITE64(FENCE_REG_965_0 + (obj_priv->fence_reg * 8), 0);
Eric Anholt4e901fd2009-10-26 16:44:17 -07002388 } else {
Eric Anholtdc529a42009-03-10 22:34:49 -07002389 uint32_t fence_reg;
2390
2391 if (obj_priv->fence_reg < 8)
2392 fence_reg = FENCE_REG_830_0 + obj_priv->fence_reg * 4;
2393 else
2394 fence_reg = FENCE_REG_945_8 + (obj_priv->fence_reg -
2395 8) * 4;
2396
2397 I915_WRITE(fence_reg, 0);
2398 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002399
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002400 reg->obj = NULL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002401 obj_priv->fence_reg = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002402 list_del_init(&reg->lru_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002403}
2404
Eric Anholt673a3942008-07-30 12:06:12 -07002405/**
Chris Wilson52dc7d32009-06-06 09:46:01 +01002406 * i915_gem_object_put_fence_reg - waits on outstanding fenced access
2407 * to the buffer to finish, and then resets the fence register.
2408 * @obj: tiled object holding a fence register.
2409 *
2410 * Zeroes out the fence register itself and clears out the associated
2411 * data structures in dev_priv and obj_priv.
2412 */
2413int
2414i915_gem_object_put_fence_reg(struct drm_gem_object *obj)
2415{
2416 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01002417 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson52dc7d32009-06-06 09:46:01 +01002418
2419 if (obj_priv->fence_reg == I915_FENCE_REG_NONE)
2420 return 0;
2421
Daniel Vetter10ae9bd2010-02-01 13:59:17 +01002422 /* If we've changed tiling, GTT-mappings of the object
2423 * need to re-fault to ensure that the correct fence register
2424 * setup is in place.
2425 */
2426 i915_gem_release_mmap(obj);
2427
Chris Wilson52dc7d32009-06-06 09:46:01 +01002428 /* On the i915, GPU access to tiled buffers is via a fence,
2429 * therefore we must wait for any outstanding access to complete
2430 * before clearing the fence.
2431 */
2432 if (!IS_I965G(dev)) {
2433 int ret;
2434
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002435 ret = i915_gem_object_flush_gpu_write_domain(obj);
2436 if (ret != 0)
2437 return ret;
2438
Chris Wilson52dc7d32009-06-06 09:46:01 +01002439 ret = i915_gem_object_wait_rendering(obj);
2440 if (ret != 0)
2441 return ret;
2442 }
2443
Daniel Vetter4a726612010-02-01 13:59:16 +01002444 i915_gem_object_flush_gtt_write_domain(obj);
Chris Wilson52dc7d32009-06-06 09:46:01 +01002445 i915_gem_clear_fence_reg (obj);
2446
2447 return 0;
2448}
2449
2450/**
Eric Anholt673a3942008-07-30 12:06:12 -07002451 * Finds free space in the GTT aperture and binds the object there.
2452 */
2453static int
2454i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment)
2455{
2456 struct drm_device *dev = obj->dev;
2457 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002458 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002459 struct drm_mm_node *free_space;
Chris Wilson4bdadb92010-01-27 13:36:32 +00002460 gfp_t gfpmask = __GFP_NORETRY | __GFP_NOWARN;
Chris Wilson07f73f62009-09-14 16:50:30 +01002461 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002462
Chris Wilsonbb6baf72009-09-22 14:24:13 +01002463 if (obj_priv->madv != I915_MADV_WILLNEED) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01002464 DRM_ERROR("Attempting to bind a purgeable object\n");
2465 return -EINVAL;
2466 }
2467
Eric Anholt673a3942008-07-30 12:06:12 -07002468 if (alignment == 0)
Jesse Barnes0f973f22009-01-26 17:10:45 -08002469 alignment = i915_gem_get_gtt_alignment(obj);
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002470 if (alignment & (i915_gem_get_gtt_alignment(obj) - 1)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002471 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2472 return -EINVAL;
2473 }
2474
Chris Wilson654fc602010-05-27 13:18:21 +01002475 /* If the object is bigger than the entire aperture, reject it early
2476 * before evicting everything in a vain attempt to find space.
2477 */
2478 if (obj->size > dev->gtt_total) {
2479 DRM_ERROR("Attempting to bind an object larger than the aperture\n");
2480 return -E2BIG;
2481 }
2482
Eric Anholt673a3942008-07-30 12:06:12 -07002483 search_free:
2484 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
2485 obj->size, alignment, 0);
2486 if (free_space != NULL) {
2487 obj_priv->gtt_space = drm_mm_get_block(free_space, obj->size,
2488 alignment);
Daniel Vetterdb3307a2010-07-02 15:02:12 +01002489 if (obj_priv->gtt_space != NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002490 obj_priv->gtt_offset = obj_priv->gtt_space->start;
Eric Anholt673a3942008-07-30 12:06:12 -07002491 }
2492 if (obj_priv->gtt_space == NULL) {
2493 /* If the gtt is empty and we're still having trouble
2494 * fitting our object in, we're out of memory.
2495 */
2496#if WATCH_LRU
2497 DRM_INFO("%s: GTT full, evicting something\n", __func__);
2498#endif
Daniel Vetter0108a3e2010-08-07 11:01:21 +01002499 ret = i915_gem_evict_something(dev, obj->size, alignment);
Chris Wilson97311292009-09-21 00:22:34 +01002500 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002501 return ret;
Chris Wilson97311292009-09-21 00:22:34 +01002502
Eric Anholt673a3942008-07-30 12:06:12 -07002503 goto search_free;
2504 }
2505
2506#if WATCH_BUF
Krzysztof Halasacfd43c02009-06-20 00:31:28 +02002507 DRM_INFO("Binding object of size %zd at 0x%08x\n",
Eric Anholt673a3942008-07-30 12:06:12 -07002508 obj->size, obj_priv->gtt_offset);
2509#endif
Chris Wilson4bdadb92010-01-27 13:36:32 +00002510 ret = i915_gem_object_get_pages(obj, gfpmask);
Eric Anholt673a3942008-07-30 12:06:12 -07002511 if (ret) {
2512 drm_mm_put_block(obj_priv->gtt_space);
2513 obj_priv->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002514
2515 if (ret == -ENOMEM) {
2516 /* first try to clear up some space from the GTT */
Daniel Vetter0108a3e2010-08-07 11:01:21 +01002517 ret = i915_gem_evict_something(dev, obj->size,
2518 alignment);
Chris Wilson07f73f62009-09-14 16:50:30 +01002519 if (ret) {
Chris Wilson07f73f62009-09-14 16:50:30 +01002520 /* now try to shrink everyone else */
Chris Wilson4bdadb92010-01-27 13:36:32 +00002521 if (gfpmask) {
2522 gfpmask = 0;
2523 goto search_free;
Chris Wilson07f73f62009-09-14 16:50:30 +01002524 }
2525
2526 return ret;
2527 }
2528
2529 goto search_free;
2530 }
2531
Eric Anholt673a3942008-07-30 12:06:12 -07002532 return ret;
2533 }
2534
Eric Anholt673a3942008-07-30 12:06:12 -07002535 /* Create an AGP memory structure pointing at our pages, and bind it
2536 * into the GTT.
2537 */
2538 obj_priv->agp_mem = drm_agp_bind_pages(dev,
Eric Anholt856fa192009-03-19 14:10:50 -07002539 obj_priv->pages,
Chris Wilson07f73f62009-09-14 16:50:30 +01002540 obj->size >> PAGE_SHIFT,
Keith Packardba1eb1d2008-10-14 19:55:10 -07002541 obj_priv->gtt_offset,
2542 obj_priv->agp_type);
Eric Anholt673a3942008-07-30 12:06:12 -07002543 if (obj_priv->agp_mem == NULL) {
Eric Anholt856fa192009-03-19 14:10:50 -07002544 i915_gem_object_put_pages(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002545 drm_mm_put_block(obj_priv->gtt_space);
2546 obj_priv->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002547
Daniel Vetter0108a3e2010-08-07 11:01:21 +01002548 ret = i915_gem_evict_something(dev, obj->size, alignment);
Chris Wilson97311292009-09-21 00:22:34 +01002549 if (ret)
Chris Wilson07f73f62009-09-14 16:50:30 +01002550 return ret;
Chris Wilson07f73f62009-09-14 16:50:30 +01002551
2552 goto search_free;
Eric Anholt673a3942008-07-30 12:06:12 -07002553 }
2554 atomic_inc(&dev->gtt_count);
2555 atomic_add(obj->size, &dev->gtt_memory);
2556
Chris Wilsonbf1a1092010-08-07 11:01:20 +01002557 /* keep track of bounds object by adding it to the inactive list */
2558 list_add_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
2559
Eric Anholt673a3942008-07-30 12:06:12 -07002560 /* Assert that the object is not currently in any GPU domain. As it
2561 * wasn't in the GTT, there shouldn't be any way it could have been in
2562 * a GPU cache
2563 */
Chris Wilson21d509e2009-06-06 09:46:02 +01002564 BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS);
2565 BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS);
Eric Anholt673a3942008-07-30 12:06:12 -07002566
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002567 trace_i915_gem_object_bind(obj, obj_priv->gtt_offset);
2568
Eric Anholt673a3942008-07-30 12:06:12 -07002569 return 0;
2570}
2571
2572void
2573i915_gem_clflush_object(struct drm_gem_object *obj)
2574{
Daniel Vetter23010e42010-03-08 13:35:02 +01002575 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002576
2577 /* If we don't have a page list set up, then we're not pinned
2578 * to GPU, and we can ignore the cache flush because it'll happen
2579 * again at bind time.
2580 */
Eric Anholt856fa192009-03-19 14:10:50 -07002581 if (obj_priv->pages == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002582 return;
2583
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002584 trace_i915_gem_object_clflush(obj);
Eric Anholtcfa16a02009-05-26 18:46:16 -07002585
Eric Anholt856fa192009-03-19 14:10:50 -07002586 drm_clflush_pages(obj_priv->pages, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002587}
2588
Eric Anholte47c68e2008-11-14 13:35:19 -08002589/** Flushes any GPU write domain for the object if it's dirty. */
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002590static int
Eric Anholte47c68e2008-11-14 13:35:19 -08002591i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj)
2592{
2593 struct drm_device *dev = obj->dev;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002594 uint32_t old_write_domain;
Zou Nan hai852835f2010-05-21 09:08:56 +08002595 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08002596
2597 if ((obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002598 return 0;
Eric Anholte47c68e2008-11-14 13:35:19 -08002599
2600 /* Queue the GPU write cache flushing we need. */
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002601 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002602 i915_gem_flush(dev, 0, obj->write_domain);
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002603 if (i915_add_request(dev, NULL, obj->write_domain, obj_priv->ring) == 0)
2604 return -ENOMEM;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002605
2606 trace_i915_gem_object_change_domain(obj,
2607 obj->read_domains,
2608 old_write_domain);
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002609 return 0;
Eric Anholte47c68e2008-11-14 13:35:19 -08002610}
2611
2612/** Flushes the GTT write domain for the object if it's dirty. */
2613static void
2614i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj)
2615{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002616 uint32_t old_write_domain;
2617
Eric Anholte47c68e2008-11-14 13:35:19 -08002618 if (obj->write_domain != I915_GEM_DOMAIN_GTT)
2619 return;
2620
2621 /* No actual flushing is required for the GTT write domain. Writes
2622 * to it immediately go to main memory as far as we know, so there's
2623 * no chipset flush. It also doesn't land in render cache.
2624 */
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002625 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002626 obj->write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002627
2628 trace_i915_gem_object_change_domain(obj,
2629 obj->read_domains,
2630 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002631}
2632
2633/** Flushes the CPU write domain for the object if it's dirty. */
2634static void
2635i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj)
2636{
2637 struct drm_device *dev = obj->dev;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002638 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002639
2640 if (obj->write_domain != I915_GEM_DOMAIN_CPU)
2641 return;
2642
2643 i915_gem_clflush_object(obj);
2644 drm_agp_chipset_flush(dev);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002645 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002646 obj->write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002647
2648 trace_i915_gem_object_change_domain(obj,
2649 obj->read_domains,
2650 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002651}
2652
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002653int
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002654i915_gem_object_flush_write_domain(struct drm_gem_object *obj)
2655{
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002656 int ret = 0;
2657
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002658 switch (obj->write_domain) {
2659 case I915_GEM_DOMAIN_GTT:
2660 i915_gem_object_flush_gtt_write_domain(obj);
2661 break;
2662 case I915_GEM_DOMAIN_CPU:
2663 i915_gem_object_flush_cpu_write_domain(obj);
2664 break;
2665 default:
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002666 ret = i915_gem_object_flush_gpu_write_domain(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002667 break;
2668 }
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002669
2670 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002671}
2672
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002673/**
2674 * Moves a single object to the GTT read, and possibly write domain.
2675 *
2676 * This function returns when the move is complete, including waiting on
2677 * flushes to occur.
2678 */
Jesse Barnes79e53942008-11-07 14:24:08 -08002679int
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002680i915_gem_object_set_to_gtt_domain(struct drm_gem_object *obj, int write)
2681{
Daniel Vetter23010e42010-03-08 13:35:02 +01002682 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002683 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002684 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002685
Eric Anholt02354392008-11-26 13:58:13 -08002686 /* Not valid to be called on unbound objects. */
2687 if (obj_priv->gtt_space == NULL)
2688 return -EINVAL;
2689
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002690 ret = i915_gem_object_flush_gpu_write_domain(obj);
2691 if (ret != 0)
2692 return ret;
2693
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002694 /* Wait on any GPU rendering and flushing to occur. */
Eric Anholte47c68e2008-11-14 13:35:19 -08002695 ret = i915_gem_object_wait_rendering(obj);
2696 if (ret != 0)
2697 return ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002698
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002699 old_write_domain = obj->write_domain;
2700 old_read_domains = obj->read_domains;
2701
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002702 /* If we're writing through the GTT domain, then CPU and GPU caches
2703 * will need to be invalidated at next use.
2704 */
2705 if (write)
Eric Anholte47c68e2008-11-14 13:35:19 -08002706 obj->read_domains &= I915_GEM_DOMAIN_GTT;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002707
Eric Anholte47c68e2008-11-14 13:35:19 -08002708 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002709
2710 /* It should now be out of any other write domains, and we can update
2711 * the domain values for our changes.
2712 */
2713 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2714 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002715 if (write) {
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002716 obj->write_domain = I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002717 obj_priv->dirty = 1;
2718 }
2719
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002720 trace_i915_gem_object_change_domain(obj,
2721 old_read_domains,
2722 old_write_domain);
2723
Eric Anholte47c68e2008-11-14 13:35:19 -08002724 return 0;
2725}
2726
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002727/*
2728 * Prepare buffer for display plane. Use uninterruptible for possible flush
2729 * wait, as in modesetting process we're not supposed to be interrupted.
2730 */
2731int
2732i915_gem_object_set_to_display_plane(struct drm_gem_object *obj)
2733{
2734 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01002735 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002736 uint32_t old_write_domain, old_read_domains;
2737 int ret;
2738
2739 /* Not valid to be called on unbound objects. */
2740 if (obj_priv->gtt_space == NULL)
2741 return -EINVAL;
2742
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002743 ret = i915_gem_object_flush_gpu_write_domain(obj);
2744 if (ret)
2745 return ret;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002746
2747 /* Wait on any GPU rendering and flushing to occur. */
2748 if (obj_priv->active) {
2749#if WATCH_BUF
2750 DRM_INFO("%s: object %p wait for seqno %08x\n",
2751 __func__, obj, obj_priv->last_rendering_seqno);
2752#endif
Zou Nan hai852835f2010-05-21 09:08:56 +08002753 ret = i915_do_wait_request(dev,
2754 obj_priv->last_rendering_seqno,
2755 0,
2756 obj_priv->ring);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002757 if (ret != 0)
2758 return ret;
2759 }
2760
Chris Wilsonb118c1e2010-05-27 13:18:14 +01002761 i915_gem_object_flush_cpu_write_domain(obj);
2762
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002763 old_write_domain = obj->write_domain;
2764 old_read_domains = obj->read_domains;
2765
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002766 /* It should now be out of any other write domains, and we can update
2767 * the domain values for our changes.
2768 */
2769 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
Chris Wilsonb118c1e2010-05-27 13:18:14 +01002770 obj->read_domains = I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002771 obj->write_domain = I915_GEM_DOMAIN_GTT;
2772 obj_priv->dirty = 1;
2773
2774 trace_i915_gem_object_change_domain(obj,
2775 old_read_domains,
2776 old_write_domain);
2777
2778 return 0;
2779}
2780
Eric Anholte47c68e2008-11-14 13:35:19 -08002781/**
2782 * Moves a single object to the CPU read, and possibly write domain.
2783 *
2784 * This function returns when the move is complete, including waiting on
2785 * flushes to occur.
2786 */
2787static int
2788i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj, int write)
2789{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002790 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002791 int ret;
2792
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002793 ret = i915_gem_object_flush_gpu_write_domain(obj);
2794 if (ret)
2795 return ret;
2796
Eric Anholte47c68e2008-11-14 13:35:19 -08002797 /* Wait on any GPU rendering and flushing to occur. */
2798 ret = i915_gem_object_wait_rendering(obj);
2799 if (ret != 0)
2800 return ret;
2801
2802 i915_gem_object_flush_gtt_write_domain(obj);
2803
2804 /* If we have a partially-valid cache of the object in the CPU,
2805 * finish invalidating it and free the per-page flags.
2806 */
2807 i915_gem_object_set_to_full_cpu_read_domain(obj);
2808
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002809 old_write_domain = obj->write_domain;
2810 old_read_domains = obj->read_domains;
2811
Eric Anholte47c68e2008-11-14 13:35:19 -08002812 /* Flush the CPU cache if it's still invalid. */
2813 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
2814 i915_gem_clflush_object(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08002815
2816 obj->read_domains |= I915_GEM_DOMAIN_CPU;
2817 }
2818
2819 /* It should now be out of any other write domains, and we can update
2820 * the domain values for our changes.
2821 */
2822 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
2823
2824 /* If we're writing through the CPU, then the GPU read domains will
2825 * need to be invalidated at next use.
2826 */
2827 if (write) {
2828 obj->read_domains &= I915_GEM_DOMAIN_CPU;
2829 obj->write_domain = I915_GEM_DOMAIN_CPU;
2830 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002831
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002832 trace_i915_gem_object_change_domain(obj,
2833 old_read_domains,
2834 old_write_domain);
2835
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002836 return 0;
2837}
2838
Eric Anholt673a3942008-07-30 12:06:12 -07002839/*
2840 * Set the next domain for the specified object. This
2841 * may not actually perform the necessary flushing/invaliding though,
2842 * as that may want to be batched with other set_domain operations
2843 *
2844 * This is (we hope) the only really tricky part of gem. The goal
2845 * is fairly simple -- track which caches hold bits of the object
2846 * and make sure they remain coherent. A few concrete examples may
2847 * help to explain how it works. For shorthand, we use the notation
2848 * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
2849 * a pair of read and write domain masks.
2850 *
2851 * Case 1: the batch buffer
2852 *
2853 * 1. Allocated
2854 * 2. Written by CPU
2855 * 3. Mapped to GTT
2856 * 4. Read by GPU
2857 * 5. Unmapped from GTT
2858 * 6. Freed
2859 *
2860 * Let's take these a step at a time
2861 *
2862 * 1. Allocated
2863 * Pages allocated from the kernel may still have
2864 * cache contents, so we set them to (CPU, CPU) always.
2865 * 2. Written by CPU (using pwrite)
2866 * The pwrite function calls set_domain (CPU, CPU) and
2867 * this function does nothing (as nothing changes)
2868 * 3. Mapped by GTT
2869 * This function asserts that the object is not
2870 * currently in any GPU-based read or write domains
2871 * 4. Read by GPU
2872 * i915_gem_execbuffer calls set_domain (COMMAND, 0).
2873 * As write_domain is zero, this function adds in the
2874 * current read domains (CPU+COMMAND, 0).
2875 * flush_domains is set to CPU.
2876 * invalidate_domains is set to COMMAND
2877 * clflush is run to get data out of the CPU caches
2878 * then i915_dev_set_domain calls i915_gem_flush to
2879 * emit an MI_FLUSH and drm_agp_chipset_flush
2880 * 5. Unmapped from GTT
2881 * i915_gem_object_unbind calls set_domain (CPU, CPU)
2882 * flush_domains and invalidate_domains end up both zero
2883 * so no flushing/invalidating happens
2884 * 6. Freed
2885 * yay, done
2886 *
2887 * Case 2: The shared render buffer
2888 *
2889 * 1. Allocated
2890 * 2. Mapped to GTT
2891 * 3. Read/written by GPU
2892 * 4. set_domain to (CPU,CPU)
2893 * 5. Read/written by CPU
2894 * 6. Read/written by GPU
2895 *
2896 * 1. Allocated
2897 * Same as last example, (CPU, CPU)
2898 * 2. Mapped to GTT
2899 * Nothing changes (assertions find that it is not in the GPU)
2900 * 3. Read/written by GPU
2901 * execbuffer calls set_domain (RENDER, RENDER)
2902 * flush_domains gets CPU
2903 * invalidate_domains gets GPU
2904 * clflush (obj)
2905 * MI_FLUSH and drm_agp_chipset_flush
2906 * 4. set_domain (CPU, CPU)
2907 * flush_domains gets GPU
2908 * invalidate_domains gets CPU
2909 * wait_rendering (obj) to make sure all drawing is complete.
2910 * This will include an MI_FLUSH to get the data from GPU
2911 * to memory
2912 * clflush (obj) to invalidate the CPU cache
2913 * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
2914 * 5. Read/written by CPU
2915 * cache lines are loaded and dirtied
2916 * 6. Read written by GPU
2917 * Same as last GPU access
2918 *
2919 * Case 3: The constant buffer
2920 *
2921 * 1. Allocated
2922 * 2. Written by CPU
2923 * 3. Read by GPU
2924 * 4. Updated (written) by CPU again
2925 * 5. Read by GPU
2926 *
2927 * 1. Allocated
2928 * (CPU, CPU)
2929 * 2. Written by CPU
2930 * (CPU, CPU)
2931 * 3. Read by GPU
2932 * (CPU+RENDER, 0)
2933 * flush_domains = CPU
2934 * invalidate_domains = RENDER
2935 * clflush (obj)
2936 * MI_FLUSH
2937 * drm_agp_chipset_flush
2938 * 4. Updated (written) by CPU again
2939 * (CPU, CPU)
2940 * flush_domains = 0 (no previous write domain)
2941 * invalidate_domains = 0 (no new read domains)
2942 * 5. Read by GPU
2943 * (CPU+RENDER, 0)
2944 * flush_domains = CPU
2945 * invalidate_domains = RENDER
2946 * clflush (obj)
2947 * MI_FLUSH
2948 * drm_agp_chipset_flush
2949 */
Keith Packardc0d90822008-11-20 23:11:08 -08002950static void
Eric Anholt8b0e3782009-02-19 14:40:50 -08002951i915_gem_object_set_to_gpu_domain(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002952{
2953 struct drm_device *dev = obj->dev;
Chris Wilson88f356b2010-08-04 13:55:32 +01002954 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002955 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002956 uint32_t invalidate_domains = 0;
2957 uint32_t flush_domains = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002958 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002959
Eric Anholt8b0e3782009-02-19 14:40:50 -08002960 BUG_ON(obj->pending_read_domains & I915_GEM_DOMAIN_CPU);
2961 BUG_ON(obj->pending_write_domain == I915_GEM_DOMAIN_CPU);
Eric Anholt673a3942008-07-30 12:06:12 -07002962
Jesse Barnes652c3932009-08-17 13:31:43 -07002963 intel_mark_busy(dev, obj);
2964
Eric Anholt673a3942008-07-30 12:06:12 -07002965#if WATCH_BUF
2966 DRM_INFO("%s: object %p read %08x -> %08x write %08x -> %08x\n",
2967 __func__, obj,
Eric Anholt8b0e3782009-02-19 14:40:50 -08002968 obj->read_domains, obj->pending_read_domains,
2969 obj->write_domain, obj->pending_write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07002970#endif
2971 /*
2972 * If the object isn't moving to a new write domain,
2973 * let the object stay in multiple read domains
2974 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08002975 if (obj->pending_write_domain == 0)
2976 obj->pending_read_domains |= obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07002977 else
2978 obj_priv->dirty = 1;
2979
2980 /*
2981 * Flush the current write domain if
2982 * the new read domains don't match. Invalidate
2983 * any read domains which differ from the old
2984 * write domain
2985 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08002986 if (obj->write_domain &&
2987 obj->write_domain != obj->pending_read_domains) {
Eric Anholt673a3942008-07-30 12:06:12 -07002988 flush_domains |= obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08002989 invalidate_domains |=
2990 obj->pending_read_domains & ~obj->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07002991 }
2992 /*
2993 * Invalidate any read caches which may have
2994 * stale data. That is, any new read domains.
2995 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08002996 invalidate_domains |= obj->pending_read_domains & ~obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07002997 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU) {
2998#if WATCH_BUF
2999 DRM_INFO("%s: CPU domain flush %08x invalidate %08x\n",
3000 __func__, flush_domains, invalidate_domains);
3001#endif
Eric Anholt673a3942008-07-30 12:06:12 -07003002 i915_gem_clflush_object(obj);
3003 }
3004
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003005 old_read_domains = obj->read_domains;
3006
Eric Anholtefbeed92009-02-19 14:54:51 -08003007 /* The actual obj->write_domain will be updated with
3008 * pending_write_domain after we emit the accumulated flush for all
3009 * of our domain changes in execbuffers (which clears objects'
3010 * write_domains). So if we have a current write domain that we
3011 * aren't changing, set pending_write_domain to that.
3012 */
3013 if (flush_domains == 0 && obj->pending_write_domain == 0)
3014 obj->pending_write_domain = obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08003015 obj->read_domains = obj->pending_read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07003016
Chris Wilson88f356b2010-08-04 13:55:32 +01003017 if (flush_domains & I915_GEM_GPU_DOMAINS) {
3018 if (obj_priv->ring == &dev_priv->render_ring)
3019 dev_priv->flush_rings |= FLUSH_RENDER_RING;
3020 else if (obj_priv->ring == &dev_priv->bsd_ring)
3021 dev_priv->flush_rings |= FLUSH_BSD_RING;
3022 }
3023
Eric Anholt673a3942008-07-30 12:06:12 -07003024 dev->invalidate_domains |= invalidate_domains;
3025 dev->flush_domains |= flush_domains;
3026#if WATCH_BUF
3027 DRM_INFO("%s: read %08x write %08x invalidate %08x flush %08x\n",
3028 __func__,
3029 obj->read_domains, obj->write_domain,
3030 dev->invalidate_domains, dev->flush_domains);
3031#endif
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003032
3033 trace_i915_gem_object_change_domain(obj,
3034 old_read_domains,
3035 obj->write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07003036}
3037
3038/**
Eric Anholte47c68e2008-11-14 13:35:19 -08003039 * Moves the object from a partially CPU read to a full one.
Eric Anholt673a3942008-07-30 12:06:12 -07003040 *
Eric Anholte47c68e2008-11-14 13:35:19 -08003041 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
3042 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
3043 */
3044static void
3045i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj)
3046{
Daniel Vetter23010e42010-03-08 13:35:02 +01003047 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003048
3049 if (!obj_priv->page_cpu_valid)
3050 return;
3051
3052 /* If we're partially in the CPU read domain, finish moving it in.
3053 */
3054 if (obj->read_domains & I915_GEM_DOMAIN_CPU) {
3055 int i;
3056
3057 for (i = 0; i <= (obj->size - 1) / PAGE_SIZE; i++) {
3058 if (obj_priv->page_cpu_valid[i])
3059 continue;
Eric Anholt856fa192009-03-19 14:10:50 -07003060 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholte47c68e2008-11-14 13:35:19 -08003061 }
Eric Anholte47c68e2008-11-14 13:35:19 -08003062 }
3063
3064 /* Free the page_cpu_valid mappings which are now stale, whether
3065 * or not we've got I915_GEM_DOMAIN_CPU.
3066 */
Eric Anholt9a298b22009-03-24 12:23:04 -07003067 kfree(obj_priv->page_cpu_valid);
Eric Anholte47c68e2008-11-14 13:35:19 -08003068 obj_priv->page_cpu_valid = NULL;
3069}
3070
3071/**
3072 * Set the CPU read domain on a range of the object.
3073 *
3074 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
3075 * not entirely valid. The page_cpu_valid member of the object flags which
3076 * pages have been flushed, and will be respected by
3077 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
3078 * of the whole object.
3079 *
3080 * This function returns when the move is complete, including waiting on
3081 * flushes to occur.
Eric Anholt673a3942008-07-30 12:06:12 -07003082 */
3083static int
Eric Anholte47c68e2008-11-14 13:35:19 -08003084i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
3085 uint64_t offset, uint64_t size)
Eric Anholt673a3942008-07-30 12:06:12 -07003086{
Daniel Vetter23010e42010-03-08 13:35:02 +01003087 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003088 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003089 int i, ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003090
Eric Anholte47c68e2008-11-14 13:35:19 -08003091 if (offset == 0 && size == obj->size)
3092 return i915_gem_object_set_to_cpu_domain(obj, 0);
3093
Chris Wilson2dafb1e2010-06-07 14:03:05 +01003094 ret = i915_gem_object_flush_gpu_write_domain(obj);
3095 if (ret)
3096 return ret;
3097
Eric Anholte47c68e2008-11-14 13:35:19 -08003098 /* Wait on any GPU rendering and flushing to occur. */
3099 ret = i915_gem_object_wait_rendering(obj);
3100 if (ret != 0)
3101 return ret;
3102 i915_gem_object_flush_gtt_write_domain(obj);
3103
3104 /* If we're already fully in the CPU read domain, we're done. */
3105 if (obj_priv->page_cpu_valid == NULL &&
3106 (obj->read_domains & I915_GEM_DOMAIN_CPU) != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07003107 return 0;
3108
Eric Anholte47c68e2008-11-14 13:35:19 -08003109 /* Otherwise, create/clear the per-page CPU read domain flag if we're
3110 * newly adding I915_GEM_DOMAIN_CPU
3111 */
Eric Anholt673a3942008-07-30 12:06:12 -07003112 if (obj_priv->page_cpu_valid == NULL) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003113 obj_priv->page_cpu_valid = kzalloc(obj->size / PAGE_SIZE,
3114 GFP_KERNEL);
Eric Anholte47c68e2008-11-14 13:35:19 -08003115 if (obj_priv->page_cpu_valid == NULL)
3116 return -ENOMEM;
3117 } else if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0)
3118 memset(obj_priv->page_cpu_valid, 0, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07003119
3120 /* Flush the cache on any pages that are still invalid from the CPU's
3121 * perspective.
3122 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003123 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
3124 i++) {
Eric Anholt673a3942008-07-30 12:06:12 -07003125 if (obj_priv->page_cpu_valid[i])
3126 continue;
3127
Eric Anholt856fa192009-03-19 14:10:50 -07003128 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07003129
3130 obj_priv->page_cpu_valid[i] = 1;
3131 }
3132
Eric Anholte47c68e2008-11-14 13:35:19 -08003133 /* It should now be out of any other write domains, and we can update
3134 * the domain values for our changes.
3135 */
3136 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
3137
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003138 old_read_domains = obj->read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003139 obj->read_domains |= I915_GEM_DOMAIN_CPU;
3140
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003141 trace_i915_gem_object_change_domain(obj,
3142 old_read_domains,
3143 obj->write_domain);
3144
Eric Anholt673a3942008-07-30 12:06:12 -07003145 return 0;
3146}
3147
3148/**
Eric Anholt673a3942008-07-30 12:06:12 -07003149 * Pin an object to the GTT and evaluate the relocations landing in it.
3150 */
3151static int
3152i915_gem_object_pin_and_relocate(struct drm_gem_object *obj,
3153 struct drm_file *file_priv,
Jesse Barnes76446ca2009-12-17 22:05:42 -05003154 struct drm_i915_gem_exec_object2 *entry,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003155 struct drm_i915_gem_relocation_entry *relocs)
Eric Anholt673a3942008-07-30 12:06:12 -07003156{
3157 struct drm_device *dev = obj->dev;
Keith Packard0839ccb2008-10-30 19:38:48 -07003158 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01003159 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003160 int i, ret;
Keith Packard0839ccb2008-10-30 19:38:48 -07003161 void __iomem *reloc_page;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003162 bool need_fence;
3163
3164 need_fence = entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
3165 obj_priv->tiling_mode != I915_TILING_NONE;
3166
3167 /* Check fence reg constraints and rebind if necessary */
Chris Wilson808b24d62010-05-27 13:18:15 +01003168 if (need_fence &&
3169 !i915_gem_object_fence_offset_ok(obj,
3170 obj_priv->tiling_mode)) {
3171 ret = i915_gem_object_unbind(obj);
3172 if (ret)
3173 return ret;
3174 }
Eric Anholt673a3942008-07-30 12:06:12 -07003175
3176 /* Choose the GTT offset for our buffer and put it there. */
3177 ret = i915_gem_object_pin(obj, (uint32_t) entry->alignment);
3178 if (ret)
3179 return ret;
3180
Jesse Barnes76446ca2009-12-17 22:05:42 -05003181 /*
3182 * Pre-965 chips need a fence register set up in order to
3183 * properly handle blits to/from tiled surfaces.
3184 */
3185 if (need_fence) {
3186 ret = i915_gem_object_get_fence_reg(obj);
3187 if (ret != 0) {
Jesse Barnes76446ca2009-12-17 22:05:42 -05003188 i915_gem_object_unpin(obj);
3189 return ret;
3190 }
3191 }
3192
Eric Anholt673a3942008-07-30 12:06:12 -07003193 entry->offset = obj_priv->gtt_offset;
3194
Eric Anholt673a3942008-07-30 12:06:12 -07003195 /* Apply the relocations, using the GTT aperture to avoid cache
3196 * flushing requirements.
3197 */
3198 for (i = 0; i < entry->relocation_count; i++) {
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003199 struct drm_i915_gem_relocation_entry *reloc= &relocs[i];
Eric Anholt673a3942008-07-30 12:06:12 -07003200 struct drm_gem_object *target_obj;
3201 struct drm_i915_gem_object *target_obj_priv;
Eric Anholt3043c602008-10-02 12:24:47 -07003202 uint32_t reloc_val, reloc_offset;
3203 uint32_t __iomem *reloc_entry;
Eric Anholt673a3942008-07-30 12:06:12 -07003204
Eric Anholt673a3942008-07-30 12:06:12 -07003205 target_obj = drm_gem_object_lookup(obj->dev, file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003206 reloc->target_handle);
Eric Anholt673a3942008-07-30 12:06:12 -07003207 if (target_obj == NULL) {
3208 i915_gem_object_unpin(obj);
3209 return -EBADF;
3210 }
Daniel Vetter23010e42010-03-08 13:35:02 +01003211 target_obj_priv = to_intel_bo(target_obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003212
Chris Wilson8542a0b2009-09-09 21:15:15 +01003213#if WATCH_RELOC
3214 DRM_INFO("%s: obj %p offset %08x target %d "
3215 "read %08x write %08x gtt %08x "
3216 "presumed %08x delta %08x\n",
3217 __func__,
3218 obj,
3219 (int) reloc->offset,
3220 (int) reloc->target_handle,
3221 (int) reloc->read_domains,
3222 (int) reloc->write_domain,
3223 (int) target_obj_priv->gtt_offset,
3224 (int) reloc->presumed_offset,
3225 reloc->delta);
3226#endif
3227
Eric Anholt673a3942008-07-30 12:06:12 -07003228 /* The target buffer should have appeared before us in the
3229 * exec_object list, so it should have a GTT space bound by now.
3230 */
3231 if (target_obj_priv->gtt_space == NULL) {
3232 DRM_ERROR("No GTT space found for object %d\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003233 reloc->target_handle);
Eric Anholt673a3942008-07-30 12:06:12 -07003234 drm_gem_object_unreference(target_obj);
3235 i915_gem_object_unpin(obj);
3236 return -EINVAL;
3237 }
3238
Chris Wilson8542a0b2009-09-09 21:15:15 +01003239 /* Validate that the target is in a valid r/w GPU domain */
Daniel Vetter16edd552010-02-19 11:52:02 +01003240 if (reloc->write_domain & (reloc->write_domain - 1)) {
3241 DRM_ERROR("reloc with multiple write domains: "
3242 "obj %p target %d offset %d "
3243 "read %08x write %08x",
3244 obj, reloc->target_handle,
3245 (int) reloc->offset,
3246 reloc->read_domains,
3247 reloc->write_domain);
3248 return -EINVAL;
3249 }
Chris Wilson8542a0b2009-09-09 21:15:15 +01003250 if (reloc->write_domain & I915_GEM_DOMAIN_CPU ||
3251 reloc->read_domains & I915_GEM_DOMAIN_CPU) {
3252 DRM_ERROR("reloc with read/write CPU domains: "
3253 "obj %p target %d offset %d "
3254 "read %08x write %08x",
3255 obj, reloc->target_handle,
3256 (int) reloc->offset,
3257 reloc->read_domains,
3258 reloc->write_domain);
3259 drm_gem_object_unreference(target_obj);
3260 i915_gem_object_unpin(obj);
3261 return -EINVAL;
3262 }
3263 if (reloc->write_domain && target_obj->pending_write_domain &&
3264 reloc->write_domain != target_obj->pending_write_domain) {
3265 DRM_ERROR("Write domain conflict: "
3266 "obj %p target %d offset %d "
3267 "new %08x old %08x\n",
3268 obj, reloc->target_handle,
3269 (int) reloc->offset,
3270 reloc->write_domain,
3271 target_obj->pending_write_domain);
3272 drm_gem_object_unreference(target_obj);
3273 i915_gem_object_unpin(obj);
3274 return -EINVAL;
3275 }
3276
3277 target_obj->pending_read_domains |= reloc->read_domains;
3278 target_obj->pending_write_domain |= reloc->write_domain;
3279
3280 /* If the relocation already has the right value in it, no
3281 * more work needs to be done.
3282 */
3283 if (target_obj_priv->gtt_offset == reloc->presumed_offset) {
3284 drm_gem_object_unreference(target_obj);
3285 continue;
3286 }
3287
3288 /* Check that the relocation address is valid... */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003289 if (reloc->offset > obj->size - 4) {
Eric Anholt673a3942008-07-30 12:06:12 -07003290 DRM_ERROR("Relocation beyond object bounds: "
3291 "obj %p target %d offset %d size %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003292 obj, reloc->target_handle,
3293 (int) reloc->offset, (int) obj->size);
Eric Anholt673a3942008-07-30 12:06:12 -07003294 drm_gem_object_unreference(target_obj);
3295 i915_gem_object_unpin(obj);
3296 return -EINVAL;
3297 }
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003298 if (reloc->offset & 3) {
Eric Anholt673a3942008-07-30 12:06:12 -07003299 DRM_ERROR("Relocation not 4-byte aligned: "
3300 "obj %p target %d offset %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003301 obj, reloc->target_handle,
3302 (int) reloc->offset);
Eric Anholt673a3942008-07-30 12:06:12 -07003303 drm_gem_object_unreference(target_obj);
3304 i915_gem_object_unpin(obj);
3305 return -EINVAL;
3306 }
3307
Chris Wilson8542a0b2009-09-09 21:15:15 +01003308 /* and points to somewhere within the target object. */
Chris Wilsoncd0b9fb2009-09-15 23:23:18 +01003309 if (reloc->delta >= target_obj->size) {
3310 DRM_ERROR("Relocation beyond target object bounds: "
3311 "obj %p target %d delta %d size %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003312 obj, reloc->target_handle,
Chris Wilsoncd0b9fb2009-09-15 23:23:18 +01003313 (int) reloc->delta, (int) target_obj->size);
Chris Wilson491152b2009-02-11 14:26:32 +00003314 drm_gem_object_unreference(target_obj);
3315 i915_gem_object_unpin(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003316 return -EINVAL;
3317 }
3318
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003319 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
3320 if (ret != 0) {
3321 drm_gem_object_unreference(target_obj);
3322 i915_gem_object_unpin(obj);
3323 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -07003324 }
3325
3326 /* Map the page containing the relocation we're going to
3327 * perform.
3328 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003329 reloc_offset = obj_priv->gtt_offset + reloc->offset;
Keith Packard0839ccb2008-10-30 19:38:48 -07003330 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
3331 (reloc_offset &
Chris Wilsonfca3ec02010-08-04 14:34:24 +01003332 ~(PAGE_SIZE - 1)),
3333 KM_USER0);
Eric Anholt3043c602008-10-02 12:24:47 -07003334 reloc_entry = (uint32_t __iomem *)(reloc_page +
Keith Packard0839ccb2008-10-30 19:38:48 -07003335 (reloc_offset & (PAGE_SIZE - 1)));
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003336 reloc_val = target_obj_priv->gtt_offset + reloc->delta;
Eric Anholt673a3942008-07-30 12:06:12 -07003337
3338#if WATCH_BUF
3339 DRM_INFO("Applied relocation: %p@0x%08x %08x -> %08x\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003340 obj, (unsigned int) reloc->offset,
Eric Anholt673a3942008-07-30 12:06:12 -07003341 readl(reloc_entry), reloc_val);
3342#endif
3343 writel(reloc_val, reloc_entry);
Chris Wilsonfca3ec02010-08-04 14:34:24 +01003344 io_mapping_unmap_atomic(reloc_page, KM_USER0);
Eric Anholt673a3942008-07-30 12:06:12 -07003345
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003346 /* The updated presumed offset for this entry will be
3347 * copied back out to the user.
Eric Anholt673a3942008-07-30 12:06:12 -07003348 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003349 reloc->presumed_offset = target_obj_priv->gtt_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003350
3351 drm_gem_object_unreference(target_obj);
3352 }
3353
Eric Anholt673a3942008-07-30 12:06:12 -07003354#if WATCH_BUF
3355 if (0)
3356 i915_gem_dump_object(obj, 128, __func__, ~0);
3357#endif
3358 return 0;
3359}
3360
Eric Anholt673a3942008-07-30 12:06:12 -07003361/* Throttle our rendering by waiting until the ring has completed our requests
3362 * emitted over 20 msec ago.
3363 *
Eric Anholtb9624422009-06-03 07:27:35 +00003364 * Note that if we were to use the current jiffies each time around the loop,
3365 * we wouldn't escape the function with any frames outstanding if the time to
3366 * render a frame was over 20ms.
3367 *
Eric Anholt673a3942008-07-30 12:06:12 -07003368 * This should get us reasonable parallelism between CPU and GPU but also
3369 * relatively low latency when blocking on a particular request to finish.
3370 */
3371static int
3372i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file_priv)
3373{
3374 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
3375 int ret = 0;
Eric Anholtb9624422009-06-03 07:27:35 +00003376 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
Eric Anholt673a3942008-07-30 12:06:12 -07003377
3378 mutex_lock(&dev->struct_mutex);
Eric Anholtb9624422009-06-03 07:27:35 +00003379 while (!list_empty(&i915_file_priv->mm.request_list)) {
3380 struct drm_i915_gem_request *request;
3381
3382 request = list_first_entry(&i915_file_priv->mm.request_list,
3383 struct drm_i915_gem_request,
3384 client_list);
3385
3386 if (time_after_eq(request->emitted_jiffies, recent_enough))
3387 break;
3388
Zou Nan hai852835f2010-05-21 09:08:56 +08003389 ret = i915_wait_request(dev, request->seqno, request->ring);
Eric Anholtb9624422009-06-03 07:27:35 +00003390 if (ret != 0)
3391 break;
3392 }
Eric Anholt673a3942008-07-30 12:06:12 -07003393 mutex_unlock(&dev->struct_mutex);
Eric Anholtb9624422009-06-03 07:27:35 +00003394
Eric Anholt673a3942008-07-30 12:06:12 -07003395 return ret;
3396}
3397
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003398static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003399i915_gem_get_relocs_from_user(struct drm_i915_gem_exec_object2 *exec_list,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003400 uint32_t buffer_count,
3401 struct drm_i915_gem_relocation_entry **relocs)
3402{
3403 uint32_t reloc_count = 0, reloc_index = 0, i;
3404 int ret;
3405
3406 *relocs = NULL;
3407 for (i = 0; i < buffer_count; i++) {
3408 if (reloc_count + exec_list[i].relocation_count < reloc_count)
3409 return -EINVAL;
3410 reloc_count += exec_list[i].relocation_count;
3411 }
3412
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003413 *relocs = drm_calloc_large(reloc_count, sizeof(**relocs));
Jesse Barnes76446ca2009-12-17 22:05:42 -05003414 if (*relocs == NULL) {
3415 DRM_ERROR("failed to alloc relocs, count %d\n", reloc_count);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003416 return -ENOMEM;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003417 }
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003418
3419 for (i = 0; i < buffer_count; i++) {
3420 struct drm_i915_gem_relocation_entry __user *user_relocs;
3421
3422 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3423
3424 ret = copy_from_user(&(*relocs)[reloc_index],
3425 user_relocs,
3426 exec_list[i].relocation_count *
3427 sizeof(**relocs));
3428 if (ret != 0) {
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003429 drm_free_large(*relocs);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003430 *relocs = NULL;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003431 return -EFAULT;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003432 }
3433
3434 reloc_index += exec_list[i].relocation_count;
3435 }
3436
Florian Mickler2bc43b52009-04-06 22:55:41 +02003437 return 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003438}
3439
3440static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003441i915_gem_put_relocs_to_user(struct drm_i915_gem_exec_object2 *exec_list,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003442 uint32_t buffer_count,
3443 struct drm_i915_gem_relocation_entry *relocs)
3444{
3445 uint32_t reloc_count = 0, i;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003446 int ret = 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003447
Chris Wilson93533c22010-01-31 10:40:48 +00003448 if (relocs == NULL)
3449 return 0;
3450
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003451 for (i = 0; i < buffer_count; i++) {
3452 struct drm_i915_gem_relocation_entry __user *user_relocs;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003453 int unwritten;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003454
3455 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3456
Florian Mickler2bc43b52009-04-06 22:55:41 +02003457 unwritten = copy_to_user(user_relocs,
3458 &relocs[reloc_count],
3459 exec_list[i].relocation_count *
3460 sizeof(*relocs));
3461
3462 if (unwritten) {
3463 ret = -EFAULT;
3464 goto err;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003465 }
3466
3467 reloc_count += exec_list[i].relocation_count;
3468 }
3469
Florian Mickler2bc43b52009-04-06 22:55:41 +02003470err:
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003471 drm_free_large(relocs);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003472
3473 return ret;
3474}
3475
Chris Wilson83d60792009-06-06 09:45:57 +01003476static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003477i915_gem_check_execbuffer (struct drm_i915_gem_execbuffer2 *exec,
Chris Wilson83d60792009-06-06 09:45:57 +01003478 uint64_t exec_offset)
3479{
3480 uint32_t exec_start, exec_len;
3481
3482 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
3483 exec_len = (uint32_t) exec->batch_len;
3484
3485 if ((exec_start | exec_len) & 0x7)
3486 return -EINVAL;
3487
3488 if (!exec_start)
3489 return -EINVAL;
3490
3491 return 0;
3492}
3493
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003494static int
3495i915_gem_wait_for_pending_flip(struct drm_device *dev,
3496 struct drm_gem_object **object_list,
3497 int count)
3498{
3499 drm_i915_private_t *dev_priv = dev->dev_private;
3500 struct drm_i915_gem_object *obj_priv;
3501 DEFINE_WAIT(wait);
3502 int i, ret = 0;
3503
3504 for (;;) {
3505 prepare_to_wait(&dev_priv->pending_flip_queue,
3506 &wait, TASK_INTERRUPTIBLE);
3507 for (i = 0; i < count; i++) {
Daniel Vetter23010e42010-03-08 13:35:02 +01003508 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003509 if (atomic_read(&obj_priv->pending_flip) > 0)
3510 break;
3511 }
3512 if (i == count)
3513 break;
3514
3515 if (!signal_pending(current)) {
3516 mutex_unlock(&dev->struct_mutex);
3517 schedule();
3518 mutex_lock(&dev->struct_mutex);
3519 continue;
3520 }
3521 ret = -ERESTARTSYS;
3522 break;
3523 }
3524 finish_wait(&dev_priv->pending_flip_queue, &wait);
3525
3526 return ret;
3527}
3528
Chris Wilson43b27f42010-07-02 08:57:15 +01003529
Eric Anholt673a3942008-07-30 12:06:12 -07003530int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003531i915_gem_do_execbuffer(struct drm_device *dev, void *data,
3532 struct drm_file *file_priv,
3533 struct drm_i915_gem_execbuffer2 *args,
3534 struct drm_i915_gem_exec_object2 *exec_list)
Eric Anholt673a3942008-07-30 12:06:12 -07003535{
3536 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003537 struct drm_gem_object **object_list = NULL;
3538 struct drm_gem_object *batch_obj;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003539 struct drm_i915_gem_object *obj_priv;
Eric Anholt201361a2009-03-11 12:30:04 -07003540 struct drm_clip_rect *cliprects = NULL;
Chris Wilson93533c22010-01-31 10:40:48 +00003541 struct drm_i915_gem_relocation_entry *relocs = NULL;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003542 int ret = 0, ret2, i, pinned = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003543 uint64_t exec_offset;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003544 uint32_t seqno, flush_domains, reloc_index;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003545 int pin_tries, flips;
Eric Anholt673a3942008-07-30 12:06:12 -07003546
Zou Nan hai852835f2010-05-21 09:08:56 +08003547 struct intel_ring_buffer *ring = NULL;
3548
Eric Anholt673a3942008-07-30 12:06:12 -07003549#if WATCH_EXEC
3550 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3551 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3552#endif
Zou Nan haid1b851f2010-05-21 09:08:57 +08003553 if (args->flags & I915_EXEC_BSD) {
3554 if (!HAS_BSD(dev)) {
3555 DRM_ERROR("execbuf with wrong flag\n");
3556 return -EINVAL;
3557 }
3558 ring = &dev_priv->bsd_ring;
3559 } else {
3560 ring = &dev_priv->render_ring;
3561 }
3562
Eric Anholt4f481ed2008-09-10 14:22:49 -07003563 if (args->buffer_count < 1) {
3564 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3565 return -EINVAL;
3566 }
Eric Anholtc8e0f932009-11-22 03:49:37 +01003567 object_list = drm_malloc_ab(sizeof(*object_list), args->buffer_count);
Jesse Barnes76446ca2009-12-17 22:05:42 -05003568 if (object_list == NULL) {
3569 DRM_ERROR("Failed to allocate object list for %d buffers\n",
Eric Anholt673a3942008-07-30 12:06:12 -07003570 args->buffer_count);
3571 ret = -ENOMEM;
3572 goto pre_mutex_err;
3573 }
Eric Anholt673a3942008-07-30 12:06:12 -07003574
Eric Anholt201361a2009-03-11 12:30:04 -07003575 if (args->num_cliprects != 0) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003576 cliprects = kcalloc(args->num_cliprects, sizeof(*cliprects),
3577 GFP_KERNEL);
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003578 if (cliprects == NULL) {
3579 ret = -ENOMEM;
Eric Anholt201361a2009-03-11 12:30:04 -07003580 goto pre_mutex_err;
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003581 }
Eric Anholt201361a2009-03-11 12:30:04 -07003582
3583 ret = copy_from_user(cliprects,
3584 (struct drm_clip_rect __user *)
3585 (uintptr_t) args->cliprects_ptr,
3586 sizeof(*cliprects) * args->num_cliprects);
3587 if (ret != 0) {
3588 DRM_ERROR("copy %d cliprects failed: %d\n",
3589 args->num_cliprects, ret);
3590 goto pre_mutex_err;
3591 }
3592 }
3593
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003594 ret = i915_gem_get_relocs_from_user(exec_list, args->buffer_count,
3595 &relocs);
3596 if (ret != 0)
3597 goto pre_mutex_err;
3598
Eric Anholt673a3942008-07-30 12:06:12 -07003599 mutex_lock(&dev->struct_mutex);
3600
3601 i915_verify_inactive(dev, __FILE__, __LINE__);
3602
Ben Gamariba1234d2009-09-14 17:48:47 -04003603 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07003604 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003605 ret = -EIO;
3606 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003607 }
3608
3609 if (dev_priv->mm.suspended) {
Eric Anholt673a3942008-07-30 12:06:12 -07003610 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003611 ret = -EBUSY;
3612 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003613 }
3614
Keith Packardac94a962008-11-20 23:30:27 -08003615 /* Look up object handles */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003616 flips = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003617 for (i = 0; i < args->buffer_count; i++) {
3618 object_list[i] = drm_gem_object_lookup(dev, file_priv,
3619 exec_list[i].handle);
3620 if (object_list[i] == NULL) {
3621 DRM_ERROR("Invalid object handle %d at index %d\n",
3622 exec_list[i].handle, i);
Chris Wilson0ce907f2010-01-23 20:26:35 +00003623 /* prevent error path from reading uninitialized data */
3624 args->buffer_count = i + 1;
Eric Anholt673a3942008-07-30 12:06:12 -07003625 ret = -EBADF;
3626 goto err;
3627 }
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003628
Daniel Vetter23010e42010-03-08 13:35:02 +01003629 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003630 if (obj_priv->in_execbuffer) {
3631 DRM_ERROR("Object %p appears more than once in object list\n",
3632 object_list[i]);
Chris Wilson0ce907f2010-01-23 20:26:35 +00003633 /* prevent error path from reading uninitialized data */
3634 args->buffer_count = i + 1;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003635 ret = -EBADF;
3636 goto err;
3637 }
3638 obj_priv->in_execbuffer = true;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003639 flips += atomic_read(&obj_priv->pending_flip);
3640 }
3641
3642 if (flips > 0) {
3643 ret = i915_gem_wait_for_pending_flip(dev, object_list,
3644 args->buffer_count);
3645 if (ret)
3646 goto err;
Keith Packardac94a962008-11-20 23:30:27 -08003647 }
Eric Anholt673a3942008-07-30 12:06:12 -07003648
Keith Packardac94a962008-11-20 23:30:27 -08003649 /* Pin and relocate */
3650 for (pin_tries = 0; ; pin_tries++) {
3651 ret = 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003652 reloc_index = 0;
3653
Keith Packardac94a962008-11-20 23:30:27 -08003654 for (i = 0; i < args->buffer_count; i++) {
3655 object_list[i]->pending_read_domains = 0;
3656 object_list[i]->pending_write_domain = 0;
3657 ret = i915_gem_object_pin_and_relocate(object_list[i],
3658 file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003659 &exec_list[i],
3660 &relocs[reloc_index]);
Keith Packardac94a962008-11-20 23:30:27 -08003661 if (ret)
3662 break;
3663 pinned = i + 1;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003664 reloc_index += exec_list[i].relocation_count;
Keith Packardac94a962008-11-20 23:30:27 -08003665 }
3666 /* success */
3667 if (ret == 0)
3668 break;
3669
3670 /* error other than GTT full, or we've already tried again */
Chris Wilson2939e1f2009-06-06 09:46:03 +01003671 if (ret != -ENOSPC || pin_tries >= 1) {
Chris Wilson07f73f62009-09-14 16:50:30 +01003672 if (ret != -ERESTARTSYS) {
3673 unsigned long long total_size = 0;
Chris Wilson3d1cc472010-05-27 13:18:19 +01003674 int num_fences = 0;
3675 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson43b27f42010-07-02 08:57:15 +01003676 obj_priv = to_intel_bo(object_list[i]);
Chris Wilson3d1cc472010-05-27 13:18:19 +01003677
Chris Wilson07f73f62009-09-14 16:50:30 +01003678 total_size += object_list[i]->size;
Chris Wilson3d1cc472010-05-27 13:18:19 +01003679 num_fences +=
3680 exec_list[i].flags & EXEC_OBJECT_NEEDS_FENCE &&
3681 obj_priv->tiling_mode != I915_TILING_NONE;
3682 }
3683 DRM_ERROR("Failed to pin buffer %d of %d, total %llu bytes, %d fences: %d\n",
Chris Wilson07f73f62009-09-14 16:50:30 +01003684 pinned+1, args->buffer_count,
Chris Wilson3d1cc472010-05-27 13:18:19 +01003685 total_size, num_fences,
3686 ret);
Chris Wilson07f73f62009-09-14 16:50:30 +01003687 DRM_ERROR("%d objects [%d pinned], "
3688 "%d object bytes [%d pinned], "
3689 "%d/%d gtt bytes\n",
3690 atomic_read(&dev->object_count),
3691 atomic_read(&dev->pin_count),
3692 atomic_read(&dev->object_memory),
3693 atomic_read(&dev->pin_memory),
3694 atomic_read(&dev->gtt_memory),
3695 dev->gtt_total);
3696 }
Eric Anholt673a3942008-07-30 12:06:12 -07003697 goto err;
3698 }
Keith Packardac94a962008-11-20 23:30:27 -08003699
3700 /* unpin all of our buffers */
3701 for (i = 0; i < pinned; i++)
3702 i915_gem_object_unpin(object_list[i]);
Eric Anholtb1177632008-12-10 10:09:41 -08003703 pinned = 0;
Keith Packardac94a962008-11-20 23:30:27 -08003704
3705 /* evict everyone we can from the aperture */
3706 ret = i915_gem_evict_everything(dev);
Chris Wilson07f73f62009-09-14 16:50:30 +01003707 if (ret && ret != -ENOSPC)
Keith Packardac94a962008-11-20 23:30:27 -08003708 goto err;
Eric Anholt673a3942008-07-30 12:06:12 -07003709 }
3710
3711 /* Set the pending read domains for the batch buffer to COMMAND */
3712 batch_obj = object_list[args->buffer_count-1];
Chris Wilson5f26a2c2009-06-06 09:45:58 +01003713 if (batch_obj->pending_write_domain) {
3714 DRM_ERROR("Attempting to use self-modifying batch buffer\n");
3715 ret = -EINVAL;
3716 goto err;
3717 }
3718 batch_obj->pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
Eric Anholt673a3942008-07-30 12:06:12 -07003719
Chris Wilson83d60792009-06-06 09:45:57 +01003720 /* Sanity check the batch buffer, prior to moving objects */
3721 exec_offset = exec_list[args->buffer_count - 1].offset;
3722 ret = i915_gem_check_execbuffer (args, exec_offset);
3723 if (ret != 0) {
3724 DRM_ERROR("execbuf with invalid offset/length\n");
3725 goto err;
3726 }
3727
Eric Anholt673a3942008-07-30 12:06:12 -07003728 i915_verify_inactive(dev, __FILE__, __LINE__);
3729
Keith Packard646f0f62008-11-20 23:23:03 -08003730 /* Zero the global flush/invalidate flags. These
3731 * will be modified as new domains are computed
3732 * for each object
3733 */
3734 dev->invalidate_domains = 0;
3735 dev->flush_domains = 0;
Chris Wilson88f356b2010-08-04 13:55:32 +01003736 dev_priv->flush_rings = 0;
Keith Packard646f0f62008-11-20 23:23:03 -08003737
Eric Anholt673a3942008-07-30 12:06:12 -07003738 for (i = 0; i < args->buffer_count; i++) {
3739 struct drm_gem_object *obj = object_list[i];
Eric Anholt673a3942008-07-30 12:06:12 -07003740
Keith Packard646f0f62008-11-20 23:23:03 -08003741 /* Compute new gpu domains and update invalidate/flush */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003742 i915_gem_object_set_to_gpu_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003743 }
3744
3745 i915_verify_inactive(dev, __FILE__, __LINE__);
3746
Keith Packard646f0f62008-11-20 23:23:03 -08003747 if (dev->invalidate_domains | dev->flush_domains) {
3748#if WATCH_EXEC
3749 DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
3750 __func__,
3751 dev->invalidate_domains,
3752 dev->flush_domains);
3753#endif
3754 i915_gem_flush(dev,
3755 dev->invalidate_domains,
3756 dev->flush_domains);
Chris Wilson88f356b2010-08-04 13:55:32 +01003757 if (dev_priv->flush_rings & FLUSH_RENDER_RING)
Eric Anholtb9624422009-06-03 07:27:35 +00003758 (void)i915_add_request(dev, file_priv,
Chris Wilson88f356b2010-08-04 13:55:32 +01003759 dev->flush_domains,
3760 &dev_priv->render_ring);
3761 if (dev_priv->flush_rings & FLUSH_BSD_RING)
3762 (void)i915_add_request(dev, file_priv,
3763 dev->flush_domains,
3764 &dev_priv->bsd_ring);
Keith Packard646f0f62008-11-20 23:23:03 -08003765 }
Eric Anholt673a3942008-07-30 12:06:12 -07003766
Eric Anholtefbeed92009-02-19 14:54:51 -08003767 for (i = 0; i < args->buffer_count; i++) {
3768 struct drm_gem_object *obj = object_list[i];
Daniel Vetter23010e42010-03-08 13:35:02 +01003769 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003770 uint32_t old_write_domain = obj->write_domain;
Eric Anholtefbeed92009-02-19 14:54:51 -08003771
3772 obj->write_domain = obj->pending_write_domain;
Daniel Vetter99fcb762010-02-07 16:20:18 +01003773 if (obj->write_domain)
3774 list_move_tail(&obj_priv->gpu_write_list,
3775 &dev_priv->mm.gpu_write_list);
3776 else
3777 list_del_init(&obj_priv->gpu_write_list);
3778
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003779 trace_i915_gem_object_change_domain(obj,
3780 obj->read_domains,
3781 old_write_domain);
Eric Anholtefbeed92009-02-19 14:54:51 -08003782 }
3783
Eric Anholt673a3942008-07-30 12:06:12 -07003784 i915_verify_inactive(dev, __FILE__, __LINE__);
3785
3786#if WATCH_COHERENCY
3787 for (i = 0; i < args->buffer_count; i++) {
3788 i915_gem_object_check_coherency(object_list[i],
3789 exec_list[i].handle);
3790 }
3791#endif
3792
Eric Anholt673a3942008-07-30 12:06:12 -07003793#if WATCH_EXEC
Ben Gamari6911a9b2009-04-02 11:24:54 -07003794 i915_gem_dump_object(batch_obj,
Eric Anholt673a3942008-07-30 12:06:12 -07003795 args->batch_len,
3796 __func__,
3797 ~0);
3798#endif
3799
Eric Anholt673a3942008-07-30 12:06:12 -07003800 /* Exec the batchbuffer */
Zou Nan hai852835f2010-05-21 09:08:56 +08003801 ret = ring->dispatch_gem_execbuffer(dev, ring, args,
3802 cliprects, exec_offset);
Eric Anholt673a3942008-07-30 12:06:12 -07003803 if (ret) {
3804 DRM_ERROR("dispatch failed %d\n", ret);
3805 goto err;
3806 }
3807
3808 /*
3809 * Ensure that the commands in the batch buffer are
3810 * finished before the interrupt fires
3811 */
Zou Nan hai852835f2010-05-21 09:08:56 +08003812 flush_domains = i915_retire_commands(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07003813
3814 i915_verify_inactive(dev, __FILE__, __LINE__);
3815
3816 /*
3817 * Get a seqno representing the execution of the current buffer,
3818 * which we can wait on. We would like to mitigate these interrupts,
3819 * likely by only creating seqnos occasionally (so that we have
3820 * *some* interrupts representing completion of buffers that we can
3821 * wait on when trying to clear up gtt space).
3822 */
Zou Nan hai852835f2010-05-21 09:08:56 +08003823 seqno = i915_add_request(dev, file_priv, flush_domains, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07003824 BUG_ON(seqno == 0);
Eric Anholt673a3942008-07-30 12:06:12 -07003825 for (i = 0; i < args->buffer_count; i++) {
3826 struct drm_gem_object *obj = object_list[i];
Zou Nan hai852835f2010-05-21 09:08:56 +08003827 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003828
Zou Nan hai852835f2010-05-21 09:08:56 +08003829 i915_gem_object_move_to_active(obj, seqno, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07003830#if WATCH_LRU
3831 DRM_INFO("%s: move to exec list %p\n", __func__, obj);
3832#endif
3833 }
3834#if WATCH_LRU
3835 i915_dump_lru(dev, __func__);
3836#endif
3837
3838 i915_verify_inactive(dev, __FILE__, __LINE__);
3839
Eric Anholt673a3942008-07-30 12:06:12 -07003840err:
Julia Lawallaad87df2008-12-21 16:28:47 +01003841 for (i = 0; i < pinned; i++)
3842 i915_gem_object_unpin(object_list[i]);
Eric Anholt673a3942008-07-30 12:06:12 -07003843
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003844 for (i = 0; i < args->buffer_count; i++) {
3845 if (object_list[i]) {
Daniel Vetter23010e42010-03-08 13:35:02 +01003846 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003847 obj_priv->in_execbuffer = false;
3848 }
Julia Lawallaad87df2008-12-21 16:28:47 +01003849 drm_gem_object_unreference(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003850 }
Julia Lawallaad87df2008-12-21 16:28:47 +01003851
Eric Anholt673a3942008-07-30 12:06:12 -07003852 mutex_unlock(&dev->struct_mutex);
3853
Chris Wilson93533c22010-01-31 10:40:48 +00003854pre_mutex_err:
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003855 /* Copy the updated relocations out regardless of current error
3856 * state. Failure to update the relocs would mean that the next
3857 * time userland calls execbuf, it would do so with presumed offset
3858 * state that didn't match the actual object state.
3859 */
3860 ret2 = i915_gem_put_relocs_to_user(exec_list, args->buffer_count,
3861 relocs);
3862 if (ret2 != 0) {
3863 DRM_ERROR("Failed to copy relocations back out: %d\n", ret2);
3864
3865 if (ret == 0)
3866 ret = ret2;
3867 }
3868
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003869 drm_free_large(object_list);
Eric Anholt9a298b22009-03-24 12:23:04 -07003870 kfree(cliprects);
Eric Anholt673a3942008-07-30 12:06:12 -07003871
3872 return ret;
3873}
3874
Jesse Barnes76446ca2009-12-17 22:05:42 -05003875/*
3876 * Legacy execbuffer just creates an exec2 list from the original exec object
3877 * list array and passes it to the real function.
3878 */
3879int
3880i915_gem_execbuffer(struct drm_device *dev, void *data,
3881 struct drm_file *file_priv)
3882{
3883 struct drm_i915_gem_execbuffer *args = data;
3884 struct drm_i915_gem_execbuffer2 exec2;
3885 struct drm_i915_gem_exec_object *exec_list = NULL;
3886 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
3887 int ret, i;
3888
3889#if WATCH_EXEC
3890 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3891 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3892#endif
3893
3894 if (args->buffer_count < 1) {
3895 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3896 return -EINVAL;
3897 }
3898
3899 /* Copy in the exec list from userland */
3900 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
3901 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
3902 if (exec_list == NULL || exec2_list == NULL) {
3903 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
3904 args->buffer_count);
3905 drm_free_large(exec_list);
3906 drm_free_large(exec2_list);
3907 return -ENOMEM;
3908 }
3909 ret = copy_from_user(exec_list,
3910 (struct drm_i915_relocation_entry __user *)
3911 (uintptr_t) args->buffers_ptr,
3912 sizeof(*exec_list) * args->buffer_count);
3913 if (ret != 0) {
3914 DRM_ERROR("copy %d exec entries failed %d\n",
3915 args->buffer_count, ret);
3916 drm_free_large(exec_list);
3917 drm_free_large(exec2_list);
3918 return -EFAULT;
3919 }
3920
3921 for (i = 0; i < args->buffer_count; i++) {
3922 exec2_list[i].handle = exec_list[i].handle;
3923 exec2_list[i].relocation_count = exec_list[i].relocation_count;
3924 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
3925 exec2_list[i].alignment = exec_list[i].alignment;
3926 exec2_list[i].offset = exec_list[i].offset;
3927 if (!IS_I965G(dev))
3928 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
3929 else
3930 exec2_list[i].flags = 0;
3931 }
3932
3933 exec2.buffers_ptr = args->buffers_ptr;
3934 exec2.buffer_count = args->buffer_count;
3935 exec2.batch_start_offset = args->batch_start_offset;
3936 exec2.batch_len = args->batch_len;
3937 exec2.DR1 = args->DR1;
3938 exec2.DR4 = args->DR4;
3939 exec2.num_cliprects = args->num_cliprects;
3940 exec2.cliprects_ptr = args->cliprects_ptr;
Zou Nan hai852835f2010-05-21 09:08:56 +08003941 exec2.flags = I915_EXEC_RENDER;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003942
3943 ret = i915_gem_do_execbuffer(dev, data, file_priv, &exec2, exec2_list);
3944 if (!ret) {
3945 /* Copy the new buffer offsets back to the user's exec list. */
3946 for (i = 0; i < args->buffer_count; i++)
3947 exec_list[i].offset = exec2_list[i].offset;
3948 /* ... and back out to userspace */
3949 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
3950 (uintptr_t) args->buffers_ptr,
3951 exec_list,
3952 sizeof(*exec_list) * args->buffer_count);
3953 if (ret) {
3954 ret = -EFAULT;
3955 DRM_ERROR("failed to copy %d exec entries "
3956 "back to user (%d)\n",
3957 args->buffer_count, ret);
3958 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05003959 }
3960
3961 drm_free_large(exec_list);
3962 drm_free_large(exec2_list);
3963 return ret;
3964}
3965
3966int
3967i915_gem_execbuffer2(struct drm_device *dev, void *data,
3968 struct drm_file *file_priv)
3969{
3970 struct drm_i915_gem_execbuffer2 *args = data;
3971 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
3972 int ret;
3973
3974#if WATCH_EXEC
3975 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3976 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3977#endif
3978
3979 if (args->buffer_count < 1) {
3980 DRM_ERROR("execbuf2 with %d buffers\n", args->buffer_count);
3981 return -EINVAL;
3982 }
3983
3984 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
3985 if (exec2_list == NULL) {
3986 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
3987 args->buffer_count);
3988 return -ENOMEM;
3989 }
3990 ret = copy_from_user(exec2_list,
3991 (struct drm_i915_relocation_entry __user *)
3992 (uintptr_t) args->buffers_ptr,
3993 sizeof(*exec2_list) * args->buffer_count);
3994 if (ret != 0) {
3995 DRM_ERROR("copy %d exec entries failed %d\n",
3996 args->buffer_count, ret);
3997 drm_free_large(exec2_list);
3998 return -EFAULT;
3999 }
4000
4001 ret = i915_gem_do_execbuffer(dev, data, file_priv, args, exec2_list);
4002 if (!ret) {
4003 /* Copy the new buffer offsets back to the user's exec list. */
4004 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
4005 (uintptr_t) args->buffers_ptr,
4006 exec2_list,
4007 sizeof(*exec2_list) * args->buffer_count);
4008 if (ret) {
4009 ret = -EFAULT;
4010 DRM_ERROR("failed to copy %d exec entries "
4011 "back to user (%d)\n",
4012 args->buffer_count, ret);
4013 }
4014 }
4015
4016 drm_free_large(exec2_list);
4017 return ret;
4018}
4019
Eric Anholt673a3942008-07-30 12:06:12 -07004020int
4021i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment)
4022{
4023 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01004024 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004025 int ret;
4026
Daniel Vetter778c3542010-05-13 11:49:44 +02004027 BUG_ON(obj_priv->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT);
4028
Eric Anholt673a3942008-07-30 12:06:12 -07004029 i915_verify_inactive(dev, __FILE__, __LINE__);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01004030
4031 if (obj_priv->gtt_space != NULL) {
4032 if (alignment == 0)
4033 alignment = i915_gem_get_gtt_alignment(obj);
4034 if (obj_priv->gtt_offset & (alignment - 1)) {
Chris Wilsonae7d49d2010-08-04 12:37:41 +01004035 WARN(obj_priv->pin_count,
4036 "bo is already pinned with incorrect alignment:"
4037 " offset=%x, req.alignment=%x\n",
4038 obj_priv->gtt_offset, alignment);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01004039 ret = i915_gem_object_unbind(obj);
4040 if (ret)
4041 return ret;
4042 }
4043 }
4044
Eric Anholt673a3942008-07-30 12:06:12 -07004045 if (obj_priv->gtt_space == NULL) {
4046 ret = i915_gem_object_bind_to_gtt(obj, alignment);
Chris Wilson97311292009-09-21 00:22:34 +01004047 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07004048 return ret;
Chris Wilson22c344e2009-02-11 14:26:45 +00004049 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05004050
Eric Anholt673a3942008-07-30 12:06:12 -07004051 obj_priv->pin_count++;
4052
4053 /* If the object is not active and not pending a flush,
4054 * remove it from the inactive list
4055 */
4056 if (obj_priv->pin_count == 1) {
4057 atomic_inc(&dev->pin_count);
4058 atomic_add(obj->size, &dev->pin_memory);
4059 if (!obj_priv->active &&
Chris Wilsonbf1a1092010-08-07 11:01:20 +01004060 (obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
Eric Anholt673a3942008-07-30 12:06:12 -07004061 list_del_init(&obj_priv->list);
4062 }
4063 i915_verify_inactive(dev, __FILE__, __LINE__);
4064
4065 return 0;
4066}
4067
4068void
4069i915_gem_object_unpin(struct drm_gem_object *obj)
4070{
4071 struct drm_device *dev = obj->dev;
4072 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01004073 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004074
4075 i915_verify_inactive(dev, __FILE__, __LINE__);
4076 obj_priv->pin_count--;
4077 BUG_ON(obj_priv->pin_count < 0);
4078 BUG_ON(obj_priv->gtt_space == NULL);
4079
4080 /* If the object is no longer pinned, and is
4081 * neither active nor being flushed, then stick it on
4082 * the inactive list
4083 */
4084 if (obj_priv->pin_count == 0) {
4085 if (!obj_priv->active &&
Chris Wilson21d509e2009-06-06 09:46:02 +01004086 (obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
Eric Anholt673a3942008-07-30 12:06:12 -07004087 list_move_tail(&obj_priv->list,
4088 &dev_priv->mm.inactive_list);
4089 atomic_dec(&dev->pin_count);
4090 atomic_sub(obj->size, &dev->pin_memory);
4091 }
4092 i915_verify_inactive(dev, __FILE__, __LINE__);
4093}
4094
4095int
4096i915_gem_pin_ioctl(struct drm_device *dev, void *data,
4097 struct drm_file *file_priv)
4098{
4099 struct drm_i915_gem_pin *args = data;
4100 struct drm_gem_object *obj;
4101 struct drm_i915_gem_object *obj_priv;
4102 int ret;
4103
4104 mutex_lock(&dev->struct_mutex);
4105
4106 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4107 if (obj == NULL) {
4108 DRM_ERROR("Bad handle in i915_gem_pin_ioctl(): %d\n",
4109 args->handle);
4110 mutex_unlock(&dev->struct_mutex);
4111 return -EBADF;
4112 }
Daniel Vetter23010e42010-03-08 13:35:02 +01004113 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004114
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004115 if (obj_priv->madv != I915_MADV_WILLNEED) {
4116 DRM_ERROR("Attempting to pin a purgeable buffer\n");
Chris Wilson3ef94da2009-09-14 16:50:29 +01004117 drm_gem_object_unreference(obj);
4118 mutex_unlock(&dev->struct_mutex);
4119 return -EINVAL;
4120 }
4121
Jesse Barnes79e53942008-11-07 14:24:08 -08004122 if (obj_priv->pin_filp != NULL && obj_priv->pin_filp != file_priv) {
4123 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
4124 args->handle);
Chris Wilson96dec612009-02-08 19:08:04 +00004125 drm_gem_object_unreference(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004126 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08004127 return -EINVAL;
4128 }
4129
4130 obj_priv->user_pin_count++;
4131 obj_priv->pin_filp = file_priv;
4132 if (obj_priv->user_pin_count == 1) {
4133 ret = i915_gem_object_pin(obj, args->alignment);
4134 if (ret != 0) {
4135 drm_gem_object_unreference(obj);
4136 mutex_unlock(&dev->struct_mutex);
4137 return ret;
4138 }
Eric Anholt673a3942008-07-30 12:06:12 -07004139 }
4140
4141 /* XXX - flush the CPU caches for pinned objects
4142 * as the X server doesn't manage domains yet
4143 */
Eric Anholte47c68e2008-11-14 13:35:19 -08004144 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004145 args->offset = obj_priv->gtt_offset;
4146 drm_gem_object_unreference(obj);
4147 mutex_unlock(&dev->struct_mutex);
4148
4149 return 0;
4150}
4151
4152int
4153i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
4154 struct drm_file *file_priv)
4155{
4156 struct drm_i915_gem_pin *args = data;
4157 struct drm_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08004158 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07004159
4160 mutex_lock(&dev->struct_mutex);
4161
4162 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4163 if (obj == NULL) {
4164 DRM_ERROR("Bad handle in i915_gem_unpin_ioctl(): %d\n",
4165 args->handle);
4166 mutex_unlock(&dev->struct_mutex);
4167 return -EBADF;
4168 }
4169
Daniel Vetter23010e42010-03-08 13:35:02 +01004170 obj_priv = to_intel_bo(obj);
Jesse Barnes79e53942008-11-07 14:24:08 -08004171 if (obj_priv->pin_filp != file_priv) {
4172 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
4173 args->handle);
4174 drm_gem_object_unreference(obj);
4175 mutex_unlock(&dev->struct_mutex);
4176 return -EINVAL;
4177 }
4178 obj_priv->user_pin_count--;
4179 if (obj_priv->user_pin_count == 0) {
4180 obj_priv->pin_filp = NULL;
4181 i915_gem_object_unpin(obj);
4182 }
Eric Anholt673a3942008-07-30 12:06:12 -07004183
4184 drm_gem_object_unreference(obj);
4185 mutex_unlock(&dev->struct_mutex);
4186 return 0;
4187}
4188
4189int
4190i915_gem_busy_ioctl(struct drm_device *dev, void *data,
4191 struct drm_file *file_priv)
4192{
4193 struct drm_i915_gem_busy *args = data;
4194 struct drm_gem_object *obj;
4195 struct drm_i915_gem_object *obj_priv;
4196
Eric Anholt673a3942008-07-30 12:06:12 -07004197 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4198 if (obj == NULL) {
4199 DRM_ERROR("Bad handle in i915_gem_busy_ioctl(): %d\n",
4200 args->handle);
Eric Anholt673a3942008-07-30 12:06:12 -07004201 return -EBADF;
4202 }
4203
Chris Wilsonb1ce7862009-06-06 09:46:00 +01004204 mutex_lock(&dev->struct_mutex);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004205
Chris Wilson0be555b2010-08-04 15:36:30 +01004206 /* Count all active objects as busy, even if they are currently not used
4207 * by the gpu. Users of this interface expect objects to eventually
4208 * become non-busy without any further actions, therefore emit any
4209 * necessary flushes here.
Eric Anholtc4de0a52008-12-14 19:05:04 -08004210 */
Chris Wilson0be555b2010-08-04 15:36:30 +01004211 obj_priv = to_intel_bo(obj);
4212 args->busy = obj_priv->active;
4213 if (args->busy) {
4214 /* Unconditionally flush objects, even when the gpu still uses this
4215 * object. Userspace calling this function indicates that it wants to
4216 * use this buffer rather sooner than later, so issuing the required
4217 * flush earlier is beneficial.
4218 */
4219 if (obj->write_domain) {
4220 i915_gem_flush(dev, 0, obj->write_domain);
4221 (void)i915_add_request(dev, file_priv, obj->write_domain, obj_priv->ring);
4222 }
4223
4224 /* Update the active list for the hardware's current position.
4225 * Otherwise this only updates on a delayed timer or when irqs
4226 * are actually unmasked, and our working set ends up being
4227 * larger than required.
4228 */
4229 i915_gem_retire_requests_ring(dev, obj_priv->ring);
4230
4231 args->busy = obj_priv->active;
4232 }
Eric Anholt673a3942008-07-30 12:06:12 -07004233
4234 drm_gem_object_unreference(obj);
4235 mutex_unlock(&dev->struct_mutex);
4236 return 0;
4237}
4238
4239int
4240i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4241 struct drm_file *file_priv)
4242{
4243 return i915_gem_ring_throttle(dev, file_priv);
4244}
4245
Chris Wilson3ef94da2009-09-14 16:50:29 +01004246int
4247i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4248 struct drm_file *file_priv)
4249{
4250 struct drm_i915_gem_madvise *args = data;
4251 struct drm_gem_object *obj;
4252 struct drm_i915_gem_object *obj_priv;
4253
4254 switch (args->madv) {
4255 case I915_MADV_DONTNEED:
4256 case I915_MADV_WILLNEED:
4257 break;
4258 default:
4259 return -EINVAL;
4260 }
4261
4262 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4263 if (obj == NULL) {
4264 DRM_ERROR("Bad handle in i915_gem_madvise_ioctl(): %d\n",
4265 args->handle);
4266 return -EBADF;
4267 }
4268
4269 mutex_lock(&dev->struct_mutex);
Daniel Vetter23010e42010-03-08 13:35:02 +01004270 obj_priv = to_intel_bo(obj);
Chris Wilson3ef94da2009-09-14 16:50:29 +01004271
4272 if (obj_priv->pin_count) {
4273 drm_gem_object_unreference(obj);
4274 mutex_unlock(&dev->struct_mutex);
4275
4276 DRM_ERROR("Attempted i915_gem_madvise_ioctl() on a pinned object\n");
4277 return -EINVAL;
4278 }
4279
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004280 if (obj_priv->madv != __I915_MADV_PURGED)
4281 obj_priv->madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004282
Chris Wilson2d7ef392009-09-20 23:13:10 +01004283 /* if the object is no longer bound, discard its backing storage */
4284 if (i915_gem_object_is_purgeable(obj_priv) &&
4285 obj_priv->gtt_space == NULL)
4286 i915_gem_object_truncate(obj);
4287
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004288 args->retained = obj_priv->madv != __I915_MADV_PURGED;
4289
Chris Wilson3ef94da2009-09-14 16:50:29 +01004290 drm_gem_object_unreference(obj);
4291 mutex_unlock(&dev->struct_mutex);
4292
4293 return 0;
4294}
4295
Daniel Vetterac52bc52010-04-09 19:05:06 +00004296struct drm_gem_object * i915_gem_alloc_object(struct drm_device *dev,
4297 size_t size)
4298{
Daniel Vetterc397b902010-04-09 19:05:07 +00004299 struct drm_i915_gem_object *obj;
4300
4301 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
4302 if (obj == NULL)
4303 return NULL;
4304
4305 if (drm_gem_object_init(dev, &obj->base, size) != 0) {
4306 kfree(obj);
4307 return NULL;
4308 }
4309
4310 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4311 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4312
4313 obj->agp_type = AGP_USER_MEMORY;
Daniel Vetter62b8b212010-04-09 19:05:08 +00004314 obj->base.driver_private = NULL;
Daniel Vetterc397b902010-04-09 19:05:07 +00004315 obj->fence_reg = I915_FENCE_REG_NONE;
4316 INIT_LIST_HEAD(&obj->list);
4317 INIT_LIST_HEAD(&obj->gpu_write_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00004318 obj->madv = I915_MADV_WILLNEED;
4319
4320 trace_i915_gem_object_create(&obj->base);
4321
4322 return &obj->base;
Daniel Vetterac52bc52010-04-09 19:05:06 +00004323}
4324
Eric Anholt673a3942008-07-30 12:06:12 -07004325int i915_gem_init_object(struct drm_gem_object *obj)
4326{
Daniel Vetterc397b902010-04-09 19:05:07 +00004327 BUG();
Jesse Barnesde151cf2008-11-12 10:03:55 -08004328
Eric Anholt673a3942008-07-30 12:06:12 -07004329 return 0;
4330}
4331
Chris Wilsonbe726152010-07-23 23:18:50 +01004332static void i915_gem_free_object_tail(struct drm_gem_object *obj)
4333{
4334 struct drm_device *dev = obj->dev;
4335 drm_i915_private_t *dev_priv = dev->dev_private;
4336 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
4337 int ret;
4338
4339 ret = i915_gem_object_unbind(obj);
4340 if (ret == -ERESTARTSYS) {
4341 list_move(&obj_priv->list,
4342 &dev_priv->mm.deferred_free_list);
4343 return;
4344 }
4345
4346 if (obj_priv->mmap_offset)
4347 i915_gem_free_mmap_offset(obj);
4348
4349 drm_gem_object_release(obj);
4350
4351 kfree(obj_priv->page_cpu_valid);
4352 kfree(obj_priv->bit_17);
4353 kfree(obj_priv);
4354}
4355
Eric Anholt673a3942008-07-30 12:06:12 -07004356void i915_gem_free_object(struct drm_gem_object *obj)
4357{
Jesse Barnesde151cf2008-11-12 10:03:55 -08004358 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01004359 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004360
Chris Wilson1c5d22f2009-08-25 11:15:50 +01004361 trace_i915_gem_object_destroy(obj);
4362
Eric Anholt673a3942008-07-30 12:06:12 -07004363 while (obj_priv->pin_count > 0)
4364 i915_gem_object_unpin(obj);
4365
Dave Airlie71acb5e2008-12-30 20:31:46 +10004366 if (obj_priv->phys_obj)
4367 i915_gem_detach_phys_object(dev, obj);
4368
Chris Wilsonbe726152010-07-23 23:18:50 +01004369 i915_gem_free_object_tail(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004370}
4371
Jesse Barnes5669fca2009-02-17 15:13:31 -08004372int
Eric Anholt673a3942008-07-30 12:06:12 -07004373i915_gem_idle(struct drm_device *dev)
4374{
4375 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson29105cc2010-01-07 10:39:13 +00004376 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004377
Keith Packard6dbe2772008-10-14 21:41:13 -07004378 mutex_lock(&dev->struct_mutex);
4379
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004380 if (dev_priv->mm.suspended ||
Zou Nan haid1b851f2010-05-21 09:08:57 +08004381 (dev_priv->render_ring.gem_object == NULL) ||
4382 (HAS_BSD(dev) &&
4383 dev_priv->bsd_ring.gem_object == NULL)) {
Keith Packard6dbe2772008-10-14 21:41:13 -07004384 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004385 return 0;
Keith Packard6dbe2772008-10-14 21:41:13 -07004386 }
Eric Anholt673a3942008-07-30 12:06:12 -07004387
Chris Wilson29105cc2010-01-07 10:39:13 +00004388 ret = i915_gpu_idle(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004389 if (ret) {
4390 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004391 return ret;
Keith Packard6dbe2772008-10-14 21:41:13 -07004392 }
Eric Anholt673a3942008-07-30 12:06:12 -07004393
Chris Wilson29105cc2010-01-07 10:39:13 +00004394 /* Under UMS, be paranoid and evict. */
4395 if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
Chris Wilsonb47eb4a2010-08-07 11:01:23 +01004396 ret = i915_gem_evict_inactive(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00004397 if (ret) {
4398 mutex_unlock(&dev->struct_mutex);
4399 return ret;
4400 }
4401 }
4402
4403 /* Hack! Don't let anybody do execbuf while we don't control the chip.
4404 * We need to replace this with a semaphore, or something.
4405 * And not confound mm.suspended!
4406 */
4407 dev_priv->mm.suspended = 1;
4408 del_timer(&dev_priv->hangcheck_timer);
4409
4410 i915_kernel_lost_context(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004411 i915_gem_cleanup_ringbuffer(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00004412
Keith Packard6dbe2772008-10-14 21:41:13 -07004413 mutex_unlock(&dev->struct_mutex);
4414
Chris Wilson29105cc2010-01-07 10:39:13 +00004415 /* Cancel the retire work handler, which should be idle now. */
4416 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
4417
Eric Anholt673a3942008-07-30 12:06:12 -07004418 return 0;
4419}
4420
Jesse Barnese552eb72010-04-21 11:39:23 -07004421/*
4422 * 965+ support PIPE_CONTROL commands, which provide finer grained control
4423 * over cache flushing.
4424 */
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004425static int
Jesse Barnese552eb72010-04-21 11:39:23 -07004426i915_gem_init_pipe_control(struct drm_device *dev)
4427{
4428 drm_i915_private_t *dev_priv = dev->dev_private;
4429 struct drm_gem_object *obj;
4430 struct drm_i915_gem_object *obj_priv;
4431 int ret;
4432
Eric Anholt34dc4d42010-05-07 14:30:03 -07004433 obj = i915_gem_alloc_object(dev, 4096);
Jesse Barnese552eb72010-04-21 11:39:23 -07004434 if (obj == NULL) {
4435 DRM_ERROR("Failed to allocate seqno page\n");
4436 ret = -ENOMEM;
4437 goto err;
4438 }
4439 obj_priv = to_intel_bo(obj);
4440 obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
4441
4442 ret = i915_gem_object_pin(obj, 4096);
4443 if (ret)
4444 goto err_unref;
4445
4446 dev_priv->seqno_gfx_addr = obj_priv->gtt_offset;
4447 dev_priv->seqno_page = kmap(obj_priv->pages[0]);
4448 if (dev_priv->seqno_page == NULL)
4449 goto err_unpin;
4450
4451 dev_priv->seqno_obj = obj;
4452 memset(dev_priv->seqno_page, 0, PAGE_SIZE);
4453
4454 return 0;
4455
4456err_unpin:
4457 i915_gem_object_unpin(obj);
4458err_unref:
4459 drm_gem_object_unreference(obj);
4460err:
4461 return ret;
4462}
4463
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004464
4465static void
Jesse Barnese552eb72010-04-21 11:39:23 -07004466i915_gem_cleanup_pipe_control(struct drm_device *dev)
4467{
4468 drm_i915_private_t *dev_priv = dev->dev_private;
4469 struct drm_gem_object *obj;
4470 struct drm_i915_gem_object *obj_priv;
4471
4472 obj = dev_priv->seqno_obj;
4473 obj_priv = to_intel_bo(obj);
4474 kunmap(obj_priv->pages[0]);
4475 i915_gem_object_unpin(obj);
4476 drm_gem_object_unreference(obj);
4477 dev_priv->seqno_obj = NULL;
4478
4479 dev_priv->seqno_page = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07004480}
4481
Eric Anholt673a3942008-07-30 12:06:12 -07004482int
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004483i915_gem_init_ringbuffer(struct drm_device *dev)
4484{
4485 drm_i915_private_t *dev_priv = dev->dev_private;
4486 int ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01004487
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004488 dev_priv->render_ring = render_ring;
Chris Wilson68f95ba2010-05-27 13:18:22 +01004489
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004490 if (!I915_NEED_GFX_HWS(dev)) {
4491 dev_priv->render_ring.status_page.page_addr
4492 = dev_priv->status_page_dmah->vaddr;
4493 memset(dev_priv->render_ring.status_page.page_addr,
4494 0, PAGE_SIZE);
4495 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004496
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004497 if (HAS_PIPE_CONTROL(dev)) {
4498 ret = i915_gem_init_pipe_control(dev);
4499 if (ret)
4500 return ret;
4501 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004502
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004503 ret = intel_init_ring_buffer(dev, &dev_priv->render_ring);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004504 if (ret)
4505 goto cleanup_pipe_control;
4506
4507 if (HAS_BSD(dev)) {
Zou Nan haid1b851f2010-05-21 09:08:57 +08004508 dev_priv->bsd_ring = bsd_ring;
4509 ret = intel_init_ring_buffer(dev, &dev_priv->bsd_ring);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004510 if (ret)
4511 goto cleanup_render_ring;
Zou Nan haid1b851f2010-05-21 09:08:57 +08004512 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004513
Chris Wilson6f392d5482010-08-07 11:01:22 +01004514 dev_priv->next_seqno = 1;
4515
Chris Wilson68f95ba2010-05-27 13:18:22 +01004516 return 0;
4517
4518cleanup_render_ring:
4519 intel_cleanup_ring_buffer(dev, &dev_priv->render_ring);
4520cleanup_pipe_control:
4521 if (HAS_PIPE_CONTROL(dev))
4522 i915_gem_cleanup_pipe_control(dev);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004523 return ret;
4524}
4525
4526void
4527i915_gem_cleanup_ringbuffer(struct drm_device *dev)
4528{
4529 drm_i915_private_t *dev_priv = dev->dev_private;
4530
4531 intel_cleanup_ring_buffer(dev, &dev_priv->render_ring);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004532 if (HAS_BSD(dev))
4533 intel_cleanup_ring_buffer(dev, &dev_priv->bsd_ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004534 if (HAS_PIPE_CONTROL(dev))
4535 i915_gem_cleanup_pipe_control(dev);
4536}
4537
4538int
Eric Anholt673a3942008-07-30 12:06:12 -07004539i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
4540 struct drm_file *file_priv)
4541{
4542 drm_i915_private_t *dev_priv = dev->dev_private;
4543 int ret;
4544
Jesse Barnes79e53942008-11-07 14:24:08 -08004545 if (drm_core_check_feature(dev, DRIVER_MODESET))
4546 return 0;
4547
Ben Gamariba1234d2009-09-14 17:48:47 -04004548 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07004549 DRM_ERROR("Reenabling wedged hardware, good luck\n");
Ben Gamariba1234d2009-09-14 17:48:47 -04004550 atomic_set(&dev_priv->mm.wedged, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004551 }
4552
Eric Anholt673a3942008-07-30 12:06:12 -07004553 mutex_lock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004554 dev_priv->mm.suspended = 0;
4555
4556 ret = i915_gem_init_ringbuffer(dev);
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004557 if (ret != 0) {
4558 mutex_unlock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004559 return ret;
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004560 }
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004561
Carl Worth5e118f42009-03-20 11:54:25 -07004562 spin_lock(&dev_priv->mm.active_list_lock);
Zou Nan hai852835f2010-05-21 09:08:56 +08004563 BUG_ON(!list_empty(&dev_priv->render_ring.active_list));
Zou Nan haid1b851f2010-05-21 09:08:57 +08004564 BUG_ON(HAS_BSD(dev) && !list_empty(&dev_priv->bsd_ring.active_list));
Carl Worth5e118f42009-03-20 11:54:25 -07004565 spin_unlock(&dev_priv->mm.active_list_lock);
4566
Eric Anholt673a3942008-07-30 12:06:12 -07004567 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
4568 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
Zou Nan hai852835f2010-05-21 09:08:56 +08004569 BUG_ON(!list_empty(&dev_priv->render_ring.request_list));
Zou Nan haid1b851f2010-05-21 09:08:57 +08004570 BUG_ON(HAS_BSD(dev) && !list_empty(&dev_priv->bsd_ring.request_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004571 mutex_unlock(&dev->struct_mutex);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004572
Chris Wilson5f353082010-06-07 14:03:03 +01004573 ret = drm_irq_install(dev);
4574 if (ret)
4575 goto cleanup_ringbuffer;
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004576
Eric Anholt673a3942008-07-30 12:06:12 -07004577 return 0;
Chris Wilson5f353082010-06-07 14:03:03 +01004578
4579cleanup_ringbuffer:
4580 mutex_lock(&dev->struct_mutex);
4581 i915_gem_cleanup_ringbuffer(dev);
4582 dev_priv->mm.suspended = 1;
4583 mutex_unlock(&dev->struct_mutex);
4584
4585 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004586}
4587
4588int
4589i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
4590 struct drm_file *file_priv)
4591{
Jesse Barnes79e53942008-11-07 14:24:08 -08004592 if (drm_core_check_feature(dev, DRIVER_MODESET))
4593 return 0;
4594
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004595 drm_irq_uninstall(dev);
Linus Torvaldse6890f62009-09-08 17:09:24 -07004596 return i915_gem_idle(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004597}
4598
4599void
4600i915_gem_lastclose(struct drm_device *dev)
4601{
4602 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004603
Eric Anholte806b492009-01-22 09:56:58 -08004604 if (drm_core_check_feature(dev, DRIVER_MODESET))
4605 return;
4606
Keith Packard6dbe2772008-10-14 21:41:13 -07004607 ret = i915_gem_idle(dev);
4608 if (ret)
4609 DRM_ERROR("failed to idle hardware: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07004610}
4611
4612void
4613i915_gem_load(struct drm_device *dev)
4614{
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004615 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07004616 drm_i915_private_t *dev_priv = dev->dev_private;
4617
Carl Worth5e118f42009-03-20 11:54:25 -07004618 spin_lock_init(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07004619 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
Daniel Vetter99fcb762010-02-07 16:20:18 +01004620 INIT_LIST_HEAD(&dev_priv->mm.gpu_write_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004621 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07004622 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Chris Wilsonbe726152010-07-23 23:18:50 +01004623 INIT_LIST_HEAD(&dev_priv->mm.deferred_free_list);
Zou Nan hai852835f2010-05-21 09:08:56 +08004624 INIT_LIST_HEAD(&dev_priv->render_ring.active_list);
4625 INIT_LIST_HEAD(&dev_priv->render_ring.request_list);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004626 if (HAS_BSD(dev)) {
4627 INIT_LIST_HEAD(&dev_priv->bsd_ring.active_list);
4628 INIT_LIST_HEAD(&dev_priv->bsd_ring.request_list);
4629 }
Daniel Vetter007cc8a2010-04-28 11:02:31 +02004630 for (i = 0; i < 16; i++)
4631 INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004632 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
4633 i915_gem_retire_work_handler);
Chris Wilson31169712009-09-14 16:50:28 +01004634 spin_lock(&shrink_list_lock);
4635 list_add(&dev_priv->mm.shrink_list, &shrink_list);
4636 spin_unlock(&shrink_list_lock);
4637
Dave Airlie94400122010-07-20 13:15:31 +10004638 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
4639 if (IS_GEN3(dev)) {
4640 u32 tmp = I915_READ(MI_ARB_STATE);
4641 if (!(tmp & MI_ARB_C3_LP_WRITE_ENABLE)) {
4642 /* arb state is a masked write, so set bit + bit in mask */
4643 tmp = MI_ARB_C3_LP_WRITE_ENABLE | (MI_ARB_C3_LP_WRITE_ENABLE << MI_ARB_MASK_SHIFT);
4644 I915_WRITE(MI_ARB_STATE, tmp);
4645 }
4646 }
4647
Jesse Barnesde151cf2008-11-12 10:03:55 -08004648 /* Old X drivers will take 0-2 for front, back, depth buffers */
Eric Anholtb397c832010-01-26 09:43:10 -08004649 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4650 dev_priv->fence_reg_start = 3;
Jesse Barnesde151cf2008-11-12 10:03:55 -08004651
Jesse Barnes0f973f22009-01-26 17:10:45 -08004652 if (IS_I965G(dev) || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08004653 dev_priv->num_fence_regs = 16;
4654 else
4655 dev_priv->num_fence_regs = 8;
4656
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004657 /* Initialize fence registers to zero */
4658 if (IS_I965G(dev)) {
4659 for (i = 0; i < 16; i++)
4660 I915_WRITE64(FENCE_REG_965_0 + (i * 8), 0);
4661 } else {
4662 for (i = 0; i < 8; i++)
4663 I915_WRITE(FENCE_REG_830_0 + (i * 4), 0);
4664 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4665 for (i = 0; i < 8; i++)
4666 I915_WRITE(FENCE_REG_945_8 + (i * 4), 0);
4667 }
Eric Anholt673a3942008-07-30 12:06:12 -07004668 i915_gem_detect_bit_6_swizzle(dev);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05004669 init_waitqueue_head(&dev_priv->pending_flip_queue);
Eric Anholt673a3942008-07-30 12:06:12 -07004670}
Dave Airlie71acb5e2008-12-30 20:31:46 +10004671
4672/*
4673 * Create a physically contiguous memory object for this object
4674 * e.g. for cursor + overlay regs
4675 */
4676int i915_gem_init_phys_object(struct drm_device *dev,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004677 int id, int size, int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004678{
4679 drm_i915_private_t *dev_priv = dev->dev_private;
4680 struct drm_i915_gem_phys_object *phys_obj;
4681 int ret;
4682
4683 if (dev_priv->mm.phys_objs[id - 1] || !size)
4684 return 0;
4685
Eric Anholt9a298b22009-03-24 12:23:04 -07004686 phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004687 if (!phys_obj)
4688 return -ENOMEM;
4689
4690 phys_obj->id = id;
4691
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004692 phys_obj->handle = drm_pci_alloc(dev, size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004693 if (!phys_obj->handle) {
4694 ret = -ENOMEM;
4695 goto kfree_obj;
4696 }
4697#ifdef CONFIG_X86
4698 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4699#endif
4700
4701 dev_priv->mm.phys_objs[id - 1] = phys_obj;
4702
4703 return 0;
4704kfree_obj:
Eric Anholt9a298b22009-03-24 12:23:04 -07004705 kfree(phys_obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004706 return ret;
4707}
4708
4709void i915_gem_free_phys_object(struct drm_device *dev, int id)
4710{
4711 drm_i915_private_t *dev_priv = dev->dev_private;
4712 struct drm_i915_gem_phys_object *phys_obj;
4713
4714 if (!dev_priv->mm.phys_objs[id - 1])
4715 return;
4716
4717 phys_obj = dev_priv->mm.phys_objs[id - 1];
4718 if (phys_obj->cur_obj) {
4719 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
4720 }
4721
4722#ifdef CONFIG_X86
4723 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4724#endif
4725 drm_pci_free(dev, phys_obj->handle);
4726 kfree(phys_obj);
4727 dev_priv->mm.phys_objs[id - 1] = NULL;
4728}
4729
4730void i915_gem_free_all_phys_object(struct drm_device *dev)
4731{
4732 int i;
4733
Dave Airlie260883c2009-01-22 17:58:49 +10004734 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004735 i915_gem_free_phys_object(dev, i);
4736}
4737
4738void i915_gem_detach_phys_object(struct drm_device *dev,
4739 struct drm_gem_object *obj)
4740{
4741 struct drm_i915_gem_object *obj_priv;
4742 int i;
4743 int ret;
4744 int page_count;
4745
Daniel Vetter23010e42010-03-08 13:35:02 +01004746 obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004747 if (!obj_priv->phys_obj)
4748 return;
4749
Chris Wilson4bdadb92010-01-27 13:36:32 +00004750 ret = i915_gem_object_get_pages(obj, 0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004751 if (ret)
4752 goto out;
4753
4754 page_count = obj->size / PAGE_SIZE;
4755
4756 for (i = 0; i < page_count; i++) {
Eric Anholt856fa192009-03-19 14:10:50 -07004757 char *dst = kmap_atomic(obj_priv->pages[i], KM_USER0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004758 char *src = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4759
4760 memcpy(dst, src, PAGE_SIZE);
4761 kunmap_atomic(dst, KM_USER0);
4762 }
Eric Anholt856fa192009-03-19 14:10:50 -07004763 drm_clflush_pages(obj_priv->pages, page_count);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004764 drm_agp_chipset_flush(dev);
Chris Wilsond78b47b2009-06-17 21:52:49 +01004765
4766 i915_gem_object_put_pages(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004767out:
4768 obj_priv->phys_obj->cur_obj = NULL;
4769 obj_priv->phys_obj = NULL;
4770}
4771
4772int
4773i915_gem_attach_phys_object(struct drm_device *dev,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004774 struct drm_gem_object *obj,
4775 int id,
4776 int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004777{
4778 drm_i915_private_t *dev_priv = dev->dev_private;
4779 struct drm_i915_gem_object *obj_priv;
4780 int ret = 0;
4781 int page_count;
4782 int i;
4783
4784 if (id > I915_MAX_PHYS_OBJECT)
4785 return -EINVAL;
4786
Daniel Vetter23010e42010-03-08 13:35:02 +01004787 obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004788
4789 if (obj_priv->phys_obj) {
4790 if (obj_priv->phys_obj->id == id)
4791 return 0;
4792 i915_gem_detach_phys_object(dev, obj);
4793 }
4794
Dave Airlie71acb5e2008-12-30 20:31:46 +10004795 /* create a new object */
4796 if (!dev_priv->mm.phys_objs[id - 1]) {
4797 ret = i915_gem_init_phys_object(dev, id,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004798 obj->size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004799 if (ret) {
Linus Torvaldsaeb565d2009-01-26 10:01:53 -08004800 DRM_ERROR("failed to init phys object %d size: %zu\n", id, obj->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004801 goto out;
4802 }
4803 }
4804
4805 /* bind to the object */
4806 obj_priv->phys_obj = dev_priv->mm.phys_objs[id - 1];
4807 obj_priv->phys_obj->cur_obj = obj;
4808
Chris Wilson4bdadb92010-01-27 13:36:32 +00004809 ret = i915_gem_object_get_pages(obj, 0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004810 if (ret) {
4811 DRM_ERROR("failed to get page list\n");
4812 goto out;
4813 }
4814
4815 page_count = obj->size / PAGE_SIZE;
4816
4817 for (i = 0; i < page_count; i++) {
Eric Anholt856fa192009-03-19 14:10:50 -07004818 char *src = kmap_atomic(obj_priv->pages[i], KM_USER0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004819 char *dst = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4820
4821 memcpy(dst, src, PAGE_SIZE);
4822 kunmap_atomic(src, KM_USER0);
4823 }
4824
Chris Wilsond78b47b2009-06-17 21:52:49 +01004825 i915_gem_object_put_pages(obj);
4826
Dave Airlie71acb5e2008-12-30 20:31:46 +10004827 return 0;
4828out:
4829 return ret;
4830}
4831
4832static int
4833i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
4834 struct drm_i915_gem_pwrite *args,
4835 struct drm_file *file_priv)
4836{
Daniel Vetter23010e42010-03-08 13:35:02 +01004837 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004838 void *obj_addr;
4839 int ret;
4840 char __user *user_data;
4841
4842 user_data = (char __user *) (uintptr_t) args->data_ptr;
4843 obj_addr = obj_priv->phys_obj->handle->vaddr + args->offset;
4844
Zhao Yakui44d98a62009-10-09 11:39:40 +08004845 DRM_DEBUG_DRIVER("obj_addr %p, %lld\n", obj_addr, args->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004846 ret = copy_from_user(obj_addr, user_data, args->size);
4847 if (ret)
4848 return -EFAULT;
4849
4850 drm_agp_chipset_flush(dev);
4851 return 0;
4852}
Eric Anholtb9624422009-06-03 07:27:35 +00004853
4854void i915_gem_release(struct drm_device * dev, struct drm_file *file_priv)
4855{
4856 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
4857
4858 /* Clean up our request list when the client is going away, so that
4859 * later retire_requests won't dereference our soon-to-be-gone
4860 * file_priv.
4861 */
4862 mutex_lock(&dev->struct_mutex);
4863 while (!list_empty(&i915_file_priv->mm.request_list))
4864 list_del_init(i915_file_priv->mm.request_list.next);
4865 mutex_unlock(&dev->struct_mutex);
4866}
Chris Wilson31169712009-09-14 16:50:28 +01004867
Chris Wilson31169712009-09-14 16:50:28 +01004868static int
Chris Wilson1637ef42010-04-20 17:10:35 +01004869i915_gpu_is_active(struct drm_device *dev)
4870{
4871 drm_i915_private_t *dev_priv = dev->dev_private;
4872 int lists_empty;
4873
4874 spin_lock(&dev_priv->mm.active_list_lock);
4875 lists_empty = list_empty(&dev_priv->mm.flushing_list) &&
Zou Nan hai852835f2010-05-21 09:08:56 +08004876 list_empty(&dev_priv->render_ring.active_list);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004877 if (HAS_BSD(dev))
4878 lists_empty &= list_empty(&dev_priv->bsd_ring.active_list);
Chris Wilson1637ef42010-04-20 17:10:35 +01004879 spin_unlock(&dev_priv->mm.active_list_lock);
4880
4881 return !lists_empty;
4882}
4883
4884static int
Dave Chinner7f8275d2010-07-19 14:56:17 +10004885i915_gem_shrink(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask)
Chris Wilson31169712009-09-14 16:50:28 +01004886{
4887 drm_i915_private_t *dev_priv, *next_dev;
4888 struct drm_i915_gem_object *obj_priv, *next_obj;
4889 int cnt = 0;
4890 int would_deadlock = 1;
4891
4892 /* "fast-path" to count number of available objects */
4893 if (nr_to_scan == 0) {
4894 spin_lock(&shrink_list_lock);
4895 list_for_each_entry(dev_priv, &shrink_list, mm.shrink_list) {
4896 struct drm_device *dev = dev_priv->dev;
4897
4898 if (mutex_trylock(&dev->struct_mutex)) {
4899 list_for_each_entry(obj_priv,
4900 &dev_priv->mm.inactive_list,
4901 list)
4902 cnt++;
4903 mutex_unlock(&dev->struct_mutex);
4904 }
4905 }
4906 spin_unlock(&shrink_list_lock);
4907
4908 return (cnt / 100) * sysctl_vfs_cache_pressure;
4909 }
4910
4911 spin_lock(&shrink_list_lock);
4912
Chris Wilson1637ef42010-04-20 17:10:35 +01004913rescan:
Chris Wilson31169712009-09-14 16:50:28 +01004914 /* first scan for clean buffers */
4915 list_for_each_entry_safe(dev_priv, next_dev,
4916 &shrink_list, mm.shrink_list) {
4917 struct drm_device *dev = dev_priv->dev;
4918
4919 if (! mutex_trylock(&dev->struct_mutex))
4920 continue;
4921
4922 spin_unlock(&shrink_list_lock);
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01004923 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004924
Chris Wilson31169712009-09-14 16:50:28 +01004925 list_for_each_entry_safe(obj_priv, next_obj,
4926 &dev_priv->mm.inactive_list,
4927 list) {
4928 if (i915_gem_object_is_purgeable(obj_priv)) {
Daniel Vettera8089e82010-04-09 19:05:09 +00004929 i915_gem_object_unbind(&obj_priv->base);
Chris Wilson31169712009-09-14 16:50:28 +01004930 if (--nr_to_scan <= 0)
4931 break;
4932 }
4933 }
4934
4935 spin_lock(&shrink_list_lock);
4936 mutex_unlock(&dev->struct_mutex);
4937
Chris Wilson963b4832009-09-20 23:03:54 +01004938 would_deadlock = 0;
4939
Chris Wilson31169712009-09-14 16:50:28 +01004940 if (nr_to_scan <= 0)
4941 break;
4942 }
4943
4944 /* second pass, evict/count anything still on the inactive list */
4945 list_for_each_entry_safe(dev_priv, next_dev,
4946 &shrink_list, mm.shrink_list) {
4947 struct drm_device *dev = dev_priv->dev;
4948
4949 if (! mutex_trylock(&dev->struct_mutex))
4950 continue;
4951
4952 spin_unlock(&shrink_list_lock);
4953
4954 list_for_each_entry_safe(obj_priv, next_obj,
4955 &dev_priv->mm.inactive_list,
4956 list) {
4957 if (nr_to_scan > 0) {
Daniel Vettera8089e82010-04-09 19:05:09 +00004958 i915_gem_object_unbind(&obj_priv->base);
Chris Wilson31169712009-09-14 16:50:28 +01004959 nr_to_scan--;
4960 } else
4961 cnt++;
4962 }
4963
4964 spin_lock(&shrink_list_lock);
4965 mutex_unlock(&dev->struct_mutex);
4966
4967 would_deadlock = 0;
4968 }
4969
Chris Wilson1637ef42010-04-20 17:10:35 +01004970 if (nr_to_scan) {
4971 int active = 0;
4972
4973 /*
4974 * We are desperate for pages, so as a last resort, wait
4975 * for the GPU to finish and discard whatever we can.
4976 * This has a dramatic impact to reduce the number of
4977 * OOM-killer events whilst running the GPU aggressively.
4978 */
4979 list_for_each_entry(dev_priv, &shrink_list, mm.shrink_list) {
4980 struct drm_device *dev = dev_priv->dev;
4981
4982 if (!mutex_trylock(&dev->struct_mutex))
4983 continue;
4984
4985 spin_unlock(&shrink_list_lock);
4986
4987 if (i915_gpu_is_active(dev)) {
4988 i915_gpu_idle(dev);
4989 active++;
4990 }
4991
4992 spin_lock(&shrink_list_lock);
4993 mutex_unlock(&dev->struct_mutex);
4994 }
4995
4996 if (active)
4997 goto rescan;
4998 }
4999
Chris Wilson31169712009-09-14 16:50:28 +01005000 spin_unlock(&shrink_list_lock);
5001
5002 if (would_deadlock)
5003 return -1;
5004 else if (cnt > 0)
5005 return (cnt / 100) * sysctl_vfs_cache_pressure;
5006 else
5007 return 0;
5008}
5009
5010static struct shrinker shrinker = {
5011 .shrink = i915_gem_shrink,
5012 .seeks = DEFAULT_SEEKS,
5013};
5014
5015__init void
5016i915_gem_shrinker_init(void)
5017{
5018 register_shrinker(&shrinker);
5019}
5020
5021__exit void
5022i915_gem_shrinker_exit(void)
5023{
5024 unregister_shrinker(&shrinker);
5025}