blob: b5a7b00264a6b1109336d17d58ce2849c76164b2 [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
Jesse Barnes79e53942008-11-07 14:24:08 -080060int i915_gem_do_init(struct drm_device *dev, unsigned long start,
61 unsigned long end)
62{
63 drm_i915_private_t *dev_priv = dev->dev_private;
64
65 if (start >= end ||
66 (start & (PAGE_SIZE - 1)) != 0 ||
67 (end & (PAGE_SIZE - 1)) != 0) {
68 return -EINVAL;
69 }
70
71 drm_mm_init(&dev_priv->mm.gtt_space, start,
72 end - start);
73
74 dev->gtt_total = (uint32_t) (end - start);
75
76 return 0;
77}
Keith Packard6dbe2772008-10-14 21:41:13 -070078
Eric Anholt673a3942008-07-30 12:06:12 -070079int
80i915_gem_init_ioctl(struct drm_device *dev, void *data,
81 struct drm_file *file_priv)
82{
Eric Anholt673a3942008-07-30 12:06:12 -070083 struct drm_i915_gem_init *args = data;
Jesse Barnes79e53942008-11-07 14:24:08 -080084 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -070085
86 mutex_lock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080087 ret = i915_gem_do_init(dev, args->gtt_start, args->gtt_end);
Eric Anholt673a3942008-07-30 12:06:12 -070088 mutex_unlock(&dev->struct_mutex);
89
Jesse Barnes79e53942008-11-07 14:24:08 -080090 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -070091}
92
Eric Anholt5a125c32008-10-22 21:40:13 -070093int
94i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
95 struct drm_file *file_priv)
96{
Eric Anholt5a125c32008-10-22 21:40:13 -070097 struct drm_i915_gem_get_aperture *args = data;
Eric Anholt5a125c32008-10-22 21:40:13 -070098
99 if (!(dev->driver->driver_features & DRIVER_GEM))
100 return -ENODEV;
101
102 args->aper_size = dev->gtt_total;
Keith Packard2678d9d2008-11-20 22:54:54 -0800103 args->aper_available_size = (args->aper_size -
104 atomic_read(&dev->pin_memory));
Eric Anholt5a125c32008-10-22 21:40:13 -0700105
106 return 0;
107}
108
Eric Anholt673a3942008-07-30 12:06:12 -0700109
110/**
111 * Creates a new mm object and returns a handle to it.
112 */
113int
114i915_gem_create_ioctl(struct drm_device *dev, void *data,
115 struct drm_file *file_priv)
116{
117 struct drm_i915_gem_create *args = data;
118 struct drm_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300119 int ret;
120 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700121
122 args->size = roundup(args->size, PAGE_SIZE);
123
124 /* Allocate the new object */
Daniel Vetterac52bc52010-04-09 19:05:06 +0000125 obj = i915_gem_alloc_object(dev, args->size);
Eric Anholt673a3942008-07-30 12:06:12 -0700126 if (obj == NULL)
127 return -ENOMEM;
128
129 ret = drm_gem_handle_create(file_priv, obj, &handle);
Chris Wilson86f100b2010-07-24 21:03:49 +0100130 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700131 if (ret)
132 return ret;
133
134 args->handle = handle;
135
136 return 0;
137}
138
Eric Anholt40123c12009-03-09 13:42:30 -0700139static inline int
Eric Anholteb014592009-03-10 11:44:52 -0700140fast_shmem_read(struct page **pages,
141 loff_t page_base, int page_offset,
142 char __user *data,
143 int length)
144{
145 char __iomem *vaddr;
Florian Mickler2bc43b52009-04-06 22:55:41 +0200146 int unwritten;
Eric Anholteb014592009-03-10 11:44:52 -0700147
148 vaddr = kmap_atomic(pages[page_base >> PAGE_SHIFT], KM_USER0);
149 if (vaddr == NULL)
150 return -ENOMEM;
Florian Mickler2bc43b52009-04-06 22:55:41 +0200151 unwritten = __copy_to_user_inatomic(data, vaddr + page_offset, length);
Eric Anholteb014592009-03-10 11:44:52 -0700152 kunmap_atomic(vaddr, KM_USER0);
153
Florian Mickler2bc43b52009-04-06 22:55:41 +0200154 if (unwritten)
155 return -EFAULT;
156
157 return 0;
Eric Anholteb014592009-03-10 11:44:52 -0700158}
159
Eric Anholt280b7132009-03-12 16:56:27 -0700160static int i915_gem_object_needs_bit17_swizzle(struct drm_gem_object *obj)
161{
162 drm_i915_private_t *dev_priv = obj->dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +0100163 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt280b7132009-03-12 16:56:27 -0700164
165 return dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_9_10_17 &&
166 obj_priv->tiling_mode != I915_TILING_NONE;
167}
168
Chris Wilson99a03df2010-05-27 14:15:34 +0100169static inline void
Eric Anholt40123c12009-03-09 13:42:30 -0700170slow_shmem_copy(struct page *dst_page,
171 int dst_offset,
172 struct page *src_page,
173 int src_offset,
174 int length)
175{
176 char *dst_vaddr, *src_vaddr;
177
Chris Wilson99a03df2010-05-27 14:15:34 +0100178 dst_vaddr = kmap(dst_page);
179 src_vaddr = kmap(src_page);
Eric Anholt40123c12009-03-09 13:42:30 -0700180
181 memcpy(dst_vaddr + dst_offset, src_vaddr + src_offset, length);
182
Chris Wilson99a03df2010-05-27 14:15:34 +0100183 kunmap(src_page);
184 kunmap(dst_page);
Eric Anholt40123c12009-03-09 13:42:30 -0700185}
186
Chris Wilson99a03df2010-05-27 14:15:34 +0100187static inline void
Eric Anholt280b7132009-03-12 16:56:27 -0700188slow_shmem_bit17_copy(struct page *gpu_page,
189 int gpu_offset,
190 struct page *cpu_page,
191 int cpu_offset,
192 int length,
193 int is_read)
194{
195 char *gpu_vaddr, *cpu_vaddr;
196
197 /* Use the unswizzled path if this page isn't affected. */
198 if ((page_to_phys(gpu_page) & (1 << 17)) == 0) {
199 if (is_read)
200 return slow_shmem_copy(cpu_page, cpu_offset,
201 gpu_page, gpu_offset, length);
202 else
203 return slow_shmem_copy(gpu_page, gpu_offset,
204 cpu_page, cpu_offset, length);
205 }
206
Chris Wilson99a03df2010-05-27 14:15:34 +0100207 gpu_vaddr = kmap(gpu_page);
208 cpu_vaddr = kmap(cpu_page);
Eric Anholt280b7132009-03-12 16:56:27 -0700209
210 /* Copy the data, XORing A6 with A17 (1). The user already knows he's
211 * XORing with the other bits (A9 for Y, A9 and A10 for X)
212 */
213 while (length > 0) {
214 int cacheline_end = ALIGN(gpu_offset + 1, 64);
215 int this_length = min(cacheline_end - gpu_offset, length);
216 int swizzled_gpu_offset = gpu_offset ^ 64;
217
218 if (is_read) {
219 memcpy(cpu_vaddr + cpu_offset,
220 gpu_vaddr + swizzled_gpu_offset,
221 this_length);
222 } else {
223 memcpy(gpu_vaddr + swizzled_gpu_offset,
224 cpu_vaddr + cpu_offset,
225 this_length);
226 }
227 cpu_offset += this_length;
228 gpu_offset += this_length;
229 length -= this_length;
230 }
231
Chris Wilson99a03df2010-05-27 14:15:34 +0100232 kunmap(cpu_page);
233 kunmap(gpu_page);
Eric Anholt280b7132009-03-12 16:56:27 -0700234}
235
Eric Anholt673a3942008-07-30 12:06:12 -0700236/**
Eric Anholteb014592009-03-10 11:44:52 -0700237 * This is the fast shmem pread path, which attempts to copy_from_user directly
238 * from the backing pages of the object to the user's address space. On a
239 * fault, it fails so we can fall back to i915_gem_shmem_pwrite_slow().
240 */
241static int
242i915_gem_shmem_pread_fast(struct drm_device *dev, struct drm_gem_object *obj,
243 struct drm_i915_gem_pread *args,
244 struct drm_file *file_priv)
245{
Daniel Vetter23010e42010-03-08 13:35:02 +0100246 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700247 ssize_t remain;
248 loff_t offset, page_base;
249 char __user *user_data;
250 int page_offset, page_length;
251 int ret;
252
253 user_data = (char __user *) (uintptr_t) args->data_ptr;
254 remain = args->size;
255
256 mutex_lock(&dev->struct_mutex);
257
Chris Wilson4bdadb92010-01-27 13:36:32 +0000258 ret = i915_gem_object_get_pages(obj, 0);
Eric Anholteb014592009-03-10 11:44:52 -0700259 if (ret != 0)
260 goto fail_unlock;
261
262 ret = i915_gem_object_set_cpu_read_domain_range(obj, args->offset,
263 args->size);
264 if (ret != 0)
265 goto fail_put_pages;
266
Daniel Vetter23010e42010-03-08 13:35:02 +0100267 obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700268 offset = args->offset;
269
270 while (remain > 0) {
271 /* Operation in this page
272 *
273 * page_base = page offset within aperture
274 * page_offset = offset within page
275 * page_length = bytes to copy for this page
276 */
277 page_base = (offset & ~(PAGE_SIZE-1));
278 page_offset = offset & (PAGE_SIZE-1);
279 page_length = remain;
280 if ((page_offset + remain) > PAGE_SIZE)
281 page_length = PAGE_SIZE - page_offset;
282
283 ret = fast_shmem_read(obj_priv->pages,
284 page_base, page_offset,
285 user_data, page_length);
286 if (ret)
287 goto fail_put_pages;
288
289 remain -= page_length;
290 user_data += page_length;
291 offset += page_length;
292 }
293
294fail_put_pages:
295 i915_gem_object_put_pages(obj);
296fail_unlock:
297 mutex_unlock(&dev->struct_mutex);
298
299 return ret;
300}
301
Chris Wilson07f73f62009-09-14 16:50:30 +0100302static int
303i915_gem_object_get_pages_or_evict(struct drm_gem_object *obj)
304{
305 int ret;
306
Chris Wilson4bdadb92010-01-27 13:36:32 +0000307 ret = i915_gem_object_get_pages(obj, __GFP_NORETRY | __GFP_NOWARN);
Chris Wilson07f73f62009-09-14 16:50:30 +0100308
309 /* If we've insufficient memory to map in the pages, attempt
310 * to make some space by throwing out some old buffers.
311 */
312 if (ret == -ENOMEM) {
313 struct drm_device *dev = obj->dev;
Chris Wilson07f73f62009-09-14 16:50:30 +0100314
Daniel Vetter0108a3e2010-08-07 11:01:21 +0100315 ret = i915_gem_evict_something(dev, obj->size,
316 i915_gem_get_gtt_alignment(obj));
Chris Wilson07f73f62009-09-14 16:50:30 +0100317 if (ret)
318 return ret;
319
Chris Wilson4bdadb92010-01-27 13:36:32 +0000320 ret = i915_gem_object_get_pages(obj, 0);
Chris Wilson07f73f62009-09-14 16:50:30 +0100321 }
322
323 return ret;
324}
325
Eric Anholteb014592009-03-10 11:44:52 -0700326/**
327 * This is the fallback shmem pread path, which allocates temporary storage
328 * in kernel space to copy_to_user into outside of the struct_mutex, so we
329 * can copy out of the object's backing pages while holding the struct mutex
330 * and not take page faults.
331 */
332static int
333i915_gem_shmem_pread_slow(struct drm_device *dev, struct drm_gem_object *obj,
334 struct drm_i915_gem_pread *args,
335 struct drm_file *file_priv)
336{
Daniel Vetter23010e42010-03-08 13:35:02 +0100337 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700338 struct mm_struct *mm = current->mm;
339 struct page **user_pages;
340 ssize_t remain;
341 loff_t offset, pinned_pages, i;
342 loff_t first_data_page, last_data_page, num_pages;
343 int shmem_page_index, shmem_page_offset;
344 int data_page_index, data_page_offset;
345 int page_length;
346 int ret;
347 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700348 int do_bit17_swizzling;
Eric Anholteb014592009-03-10 11:44:52 -0700349
350 remain = args->size;
351
352 /* Pin the user pages containing the data. We can't fault while
353 * holding the struct mutex, yet we want to hold it while
354 * dereferencing the user data.
355 */
356 first_data_page = data_ptr / PAGE_SIZE;
357 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
358 num_pages = last_data_page - first_data_page + 1;
359
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700360 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
Eric Anholteb014592009-03-10 11:44:52 -0700361 if (user_pages == NULL)
362 return -ENOMEM;
363
364 down_read(&mm->mmap_sem);
365 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
Eric Anholte5e9ecd2009-04-07 16:01:22 -0700366 num_pages, 1, 0, user_pages, NULL);
Eric Anholteb014592009-03-10 11:44:52 -0700367 up_read(&mm->mmap_sem);
368 if (pinned_pages < num_pages) {
369 ret = -EFAULT;
370 goto fail_put_user_pages;
371 }
372
Eric Anholt280b7132009-03-12 16:56:27 -0700373 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
374
Eric Anholteb014592009-03-10 11:44:52 -0700375 mutex_lock(&dev->struct_mutex);
376
Chris Wilson07f73f62009-09-14 16:50:30 +0100377 ret = i915_gem_object_get_pages_or_evict(obj);
378 if (ret)
Eric Anholteb014592009-03-10 11:44:52 -0700379 goto fail_unlock;
380
381 ret = i915_gem_object_set_cpu_read_domain_range(obj, args->offset,
382 args->size);
383 if (ret != 0)
384 goto fail_put_pages;
385
Daniel Vetter23010e42010-03-08 13:35:02 +0100386 obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700387 offset = args->offset;
388
389 while (remain > 0) {
390 /* Operation in this page
391 *
392 * shmem_page_index = page number within shmem file
393 * shmem_page_offset = offset within page in shmem file
394 * data_page_index = page number in get_user_pages return
395 * data_page_offset = offset with data_page_index page.
396 * page_length = bytes to copy for this page
397 */
398 shmem_page_index = offset / PAGE_SIZE;
399 shmem_page_offset = offset & ~PAGE_MASK;
400 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
401 data_page_offset = data_ptr & ~PAGE_MASK;
402
403 page_length = remain;
404 if ((shmem_page_offset + page_length) > PAGE_SIZE)
405 page_length = PAGE_SIZE - shmem_page_offset;
406 if ((data_page_offset + page_length) > PAGE_SIZE)
407 page_length = PAGE_SIZE - data_page_offset;
408
Eric Anholt280b7132009-03-12 16:56:27 -0700409 if (do_bit17_swizzling) {
Chris Wilson99a03df2010-05-27 14:15:34 +0100410 slow_shmem_bit17_copy(obj_priv->pages[shmem_page_index],
Eric Anholt280b7132009-03-12 16:56:27 -0700411 shmem_page_offset,
Chris Wilson99a03df2010-05-27 14:15:34 +0100412 user_pages[data_page_index],
413 data_page_offset,
414 page_length,
415 1);
416 } else {
417 slow_shmem_copy(user_pages[data_page_index],
418 data_page_offset,
419 obj_priv->pages[shmem_page_index],
420 shmem_page_offset,
421 page_length);
Eric Anholt280b7132009-03-12 16:56:27 -0700422 }
Eric Anholteb014592009-03-10 11:44:52 -0700423
424 remain -= page_length;
425 data_ptr += page_length;
426 offset += page_length;
427 }
428
429fail_put_pages:
430 i915_gem_object_put_pages(obj);
431fail_unlock:
432 mutex_unlock(&dev->struct_mutex);
433fail_put_user_pages:
434 for (i = 0; i < pinned_pages; i++) {
435 SetPageDirty(user_pages[i]);
436 page_cache_release(user_pages[i]);
437 }
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700438 drm_free_large(user_pages);
Eric Anholteb014592009-03-10 11:44:52 -0700439
440 return ret;
441}
442
Eric Anholt673a3942008-07-30 12:06:12 -0700443/**
444 * Reads data from the object referenced by handle.
445 *
446 * On error, the contents of *data are undefined.
447 */
448int
449i915_gem_pread_ioctl(struct drm_device *dev, void *data,
450 struct drm_file *file_priv)
451{
452 struct drm_i915_gem_pread *args = data;
453 struct drm_gem_object *obj;
454 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -0700455 int ret;
456
457 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
458 if (obj == NULL)
459 return -EBADF;
Daniel Vetter23010e42010-03-08 13:35:02 +0100460 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700461
462 /* Bounds check source.
463 *
464 * XXX: This could use review for overflow issues...
465 */
466 if (args->offset > obj->size || args->size > obj->size ||
467 args->offset + args->size > obj->size) {
Luca Barbieribc9025b2010-02-09 05:49:12 +0000468 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700469 return -EINVAL;
470 }
471
Eric Anholt280b7132009-03-12 16:56:27 -0700472 if (i915_gem_object_needs_bit17_swizzle(obj)) {
Eric Anholteb014592009-03-10 11:44:52 -0700473 ret = i915_gem_shmem_pread_slow(dev, obj, args, file_priv);
Eric Anholt280b7132009-03-12 16:56:27 -0700474 } else {
475 ret = i915_gem_shmem_pread_fast(dev, obj, args, file_priv);
476 if (ret != 0)
477 ret = i915_gem_shmem_pread_slow(dev, obj, args,
478 file_priv);
479 }
Eric Anholt673a3942008-07-30 12:06:12 -0700480
Luca Barbieribc9025b2010-02-09 05:49:12 +0000481 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700482
Eric Anholteb014592009-03-10 11:44:52 -0700483 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700484}
485
Keith Packard0839ccb2008-10-30 19:38:48 -0700486/* This is the fast write path which cannot handle
487 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700488 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700489
Keith Packard0839ccb2008-10-30 19:38:48 -0700490static inline int
491fast_user_write(struct io_mapping *mapping,
492 loff_t page_base, int page_offset,
493 char __user *user_data,
494 int length)
495{
496 char *vaddr_atomic;
497 unsigned long unwritten;
498
Chris Wilsonfca3ec02010-08-04 14:34:24 +0100499 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base, KM_USER0);
Keith Packard0839ccb2008-10-30 19:38:48 -0700500 unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
501 user_data, length);
Chris Wilsonfca3ec02010-08-04 14:34:24 +0100502 io_mapping_unmap_atomic(vaddr_atomic, KM_USER0);
Keith Packard0839ccb2008-10-30 19:38:48 -0700503 if (unwritten)
504 return -EFAULT;
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700505 return 0;
Keith Packard0839ccb2008-10-30 19:38:48 -0700506}
507
508/* Here's the write path which can sleep for
509 * page faults
510 */
511
Chris Wilsonab34c222010-05-27 14:15:35 +0100512static inline void
Eric Anholt3de09aa2009-03-09 09:42:23 -0700513slow_kernel_write(struct io_mapping *mapping,
514 loff_t gtt_base, int gtt_offset,
515 struct page *user_page, int user_offset,
516 int length)
Keith Packard0839ccb2008-10-30 19:38:48 -0700517{
Chris Wilsonab34c222010-05-27 14:15:35 +0100518 char __iomem *dst_vaddr;
519 char *src_vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -0700520
Chris Wilsonab34c222010-05-27 14:15:35 +0100521 dst_vaddr = io_mapping_map_wc(mapping, gtt_base);
522 src_vaddr = kmap(user_page);
523
524 memcpy_toio(dst_vaddr + gtt_offset,
525 src_vaddr + user_offset,
526 length);
527
528 kunmap(user_page);
529 io_mapping_unmap(dst_vaddr);
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700530}
531
Eric Anholt40123c12009-03-09 13:42:30 -0700532static inline int
533fast_shmem_write(struct page **pages,
534 loff_t page_base, int page_offset,
535 char __user *data,
536 int length)
537{
538 char __iomem *vaddr;
Dave Airlied0088772009-03-28 20:29:48 -0400539 unsigned long unwritten;
Eric Anholt40123c12009-03-09 13:42:30 -0700540
541 vaddr = kmap_atomic(pages[page_base >> PAGE_SHIFT], KM_USER0);
542 if (vaddr == NULL)
543 return -ENOMEM;
Dave Airlied0088772009-03-28 20:29:48 -0400544 unwritten = __copy_from_user_inatomic(vaddr + page_offset, data, length);
Eric Anholt40123c12009-03-09 13:42:30 -0700545 kunmap_atomic(vaddr, KM_USER0);
546
Dave Airlied0088772009-03-28 20:29:48 -0400547 if (unwritten)
548 return -EFAULT;
Eric Anholt40123c12009-03-09 13:42:30 -0700549 return 0;
550}
551
Eric Anholt3de09aa2009-03-09 09:42:23 -0700552/**
553 * This is the fast pwrite path, where we copy the data directly from the
554 * user into the GTT, uncached.
555 */
Eric Anholt673a3942008-07-30 12:06:12 -0700556static int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700557i915_gem_gtt_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
558 struct drm_i915_gem_pwrite *args,
559 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700560{
Daniel Vetter23010e42010-03-08 13:35:02 +0100561 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Keith Packard0839ccb2008-10-30 19:38:48 -0700562 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700563 ssize_t remain;
Keith Packard0839ccb2008-10-30 19:38:48 -0700564 loff_t offset, page_base;
Eric Anholt673a3942008-07-30 12:06:12 -0700565 char __user *user_data;
Keith Packard0839ccb2008-10-30 19:38:48 -0700566 int page_offset, page_length;
567 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700568
569 user_data = (char __user *) (uintptr_t) args->data_ptr;
570 remain = args->size;
571 if (!access_ok(VERIFY_READ, user_data, remain))
572 return -EFAULT;
573
574
575 mutex_lock(&dev->struct_mutex);
576 ret = i915_gem_object_pin(obj, 0);
577 if (ret) {
578 mutex_unlock(&dev->struct_mutex);
579 return ret;
580 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800581 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
Eric Anholt673a3942008-07-30 12:06:12 -0700582 if (ret)
583 goto fail;
584
Daniel Vetter23010e42010-03-08 13:35:02 +0100585 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700586 offset = obj_priv->gtt_offset + args->offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700587
588 while (remain > 0) {
589 /* Operation in this page
590 *
Keith Packard0839ccb2008-10-30 19:38:48 -0700591 * page_base = page offset within aperture
592 * page_offset = offset within page
593 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -0700594 */
Keith Packard0839ccb2008-10-30 19:38:48 -0700595 page_base = (offset & ~(PAGE_SIZE-1));
596 page_offset = offset & (PAGE_SIZE-1);
597 page_length = remain;
598 if ((page_offset + remain) > PAGE_SIZE)
599 page_length = PAGE_SIZE - page_offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700600
Keith Packard0839ccb2008-10-30 19:38:48 -0700601 ret = fast_user_write (dev_priv->mm.gtt_mapping, page_base,
602 page_offset, user_data, page_length);
Eric Anholt673a3942008-07-30 12:06:12 -0700603
Keith Packard0839ccb2008-10-30 19:38:48 -0700604 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -0700605 * source page isn't available. Return the error and we'll
606 * retry in the slow path.
Keith Packard0839ccb2008-10-30 19:38:48 -0700607 */
Eric Anholt3de09aa2009-03-09 09:42:23 -0700608 if (ret)
609 goto fail;
Eric Anholt673a3942008-07-30 12:06:12 -0700610
Keith Packard0839ccb2008-10-30 19:38:48 -0700611 remain -= page_length;
612 user_data += page_length;
613 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700614 }
Eric Anholt673a3942008-07-30 12:06:12 -0700615
616fail:
617 i915_gem_object_unpin(obj);
618 mutex_unlock(&dev->struct_mutex);
619
620 return ret;
621}
622
Eric Anholt3de09aa2009-03-09 09:42:23 -0700623/**
624 * This is the fallback GTT pwrite path, which uses get_user_pages to pin
625 * the memory and maps it using kmap_atomic for copying.
626 *
627 * This code resulted in x11perf -rgb10text consuming about 10% more CPU
628 * than using i915_gem_gtt_pwrite_fast on a G45 (32-bit).
629 */
Eric Anholt3043c602008-10-02 12:24:47 -0700630static int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700631i915_gem_gtt_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
632 struct drm_i915_gem_pwrite *args,
633 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700634{
Daniel Vetter23010e42010-03-08 13:35:02 +0100635 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700636 drm_i915_private_t *dev_priv = dev->dev_private;
637 ssize_t remain;
638 loff_t gtt_page_base, offset;
639 loff_t first_data_page, last_data_page, num_pages;
640 loff_t pinned_pages, i;
641 struct page **user_pages;
642 struct mm_struct *mm = current->mm;
643 int gtt_page_offset, data_page_offset, data_page_index, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700644 int ret;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700645 uint64_t data_ptr = args->data_ptr;
646
647 remain = args->size;
648
649 /* Pin the user pages containing the data. We can't fault while
650 * holding the struct mutex, and all of the pwrite implementations
651 * want to hold it while dereferencing the user data.
652 */
653 first_data_page = data_ptr / PAGE_SIZE;
654 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
655 num_pages = last_data_page - first_data_page + 1;
656
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700657 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
Eric Anholt3de09aa2009-03-09 09:42:23 -0700658 if (user_pages == NULL)
659 return -ENOMEM;
660
661 down_read(&mm->mmap_sem);
662 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
663 num_pages, 0, 0, user_pages, NULL);
664 up_read(&mm->mmap_sem);
665 if (pinned_pages < num_pages) {
666 ret = -EFAULT;
667 goto out_unpin_pages;
668 }
669
670 mutex_lock(&dev->struct_mutex);
671 ret = i915_gem_object_pin(obj, 0);
672 if (ret)
673 goto out_unlock;
674
675 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
676 if (ret)
677 goto out_unpin_object;
678
Daniel Vetter23010e42010-03-08 13:35:02 +0100679 obj_priv = to_intel_bo(obj);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700680 offset = obj_priv->gtt_offset + args->offset;
681
682 while (remain > 0) {
683 /* Operation in this page
684 *
685 * gtt_page_base = page offset within aperture
686 * gtt_page_offset = offset within page in aperture
687 * data_page_index = page number in get_user_pages return
688 * data_page_offset = offset with data_page_index page.
689 * page_length = bytes to copy for this page
690 */
691 gtt_page_base = offset & PAGE_MASK;
692 gtt_page_offset = offset & ~PAGE_MASK;
693 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
694 data_page_offset = data_ptr & ~PAGE_MASK;
695
696 page_length = remain;
697 if ((gtt_page_offset + page_length) > PAGE_SIZE)
698 page_length = PAGE_SIZE - gtt_page_offset;
699 if ((data_page_offset + page_length) > PAGE_SIZE)
700 page_length = PAGE_SIZE - data_page_offset;
701
Chris Wilsonab34c222010-05-27 14:15:35 +0100702 slow_kernel_write(dev_priv->mm.gtt_mapping,
703 gtt_page_base, gtt_page_offset,
704 user_pages[data_page_index],
705 data_page_offset,
706 page_length);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700707
708 remain -= page_length;
709 offset += page_length;
710 data_ptr += page_length;
711 }
712
713out_unpin_object:
714 i915_gem_object_unpin(obj);
715out_unlock:
716 mutex_unlock(&dev->struct_mutex);
717out_unpin_pages:
718 for (i = 0; i < pinned_pages; i++)
719 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700720 drm_free_large(user_pages);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700721
722 return ret;
723}
724
Eric Anholt40123c12009-03-09 13:42:30 -0700725/**
726 * This is the fast shmem pwrite path, which attempts to directly
727 * copy_from_user into the kmapped pages backing the object.
728 */
Eric Anholt673a3942008-07-30 12:06:12 -0700729static int
Eric Anholt40123c12009-03-09 13:42:30 -0700730i915_gem_shmem_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
731 struct drm_i915_gem_pwrite *args,
732 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700733{
Daniel Vetter23010e42010-03-08 13:35:02 +0100734 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700735 ssize_t remain;
736 loff_t offset, page_base;
737 char __user *user_data;
738 int page_offset, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700739 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -0700740
741 user_data = (char __user *) (uintptr_t) args->data_ptr;
742 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700743
744 mutex_lock(&dev->struct_mutex);
745
Chris Wilson4bdadb92010-01-27 13:36:32 +0000746 ret = i915_gem_object_get_pages(obj, 0);
Eric Anholt40123c12009-03-09 13:42:30 -0700747 if (ret != 0)
748 goto fail_unlock;
749
Eric Anholte47c68e2008-11-14 13:35:19 -0800750 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Eric Anholt40123c12009-03-09 13:42:30 -0700751 if (ret != 0)
752 goto fail_put_pages;
Eric Anholt673a3942008-07-30 12:06:12 -0700753
Daniel Vetter23010e42010-03-08 13:35:02 +0100754 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700755 offset = args->offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700756 obj_priv->dirty = 1;
Eric Anholt673a3942008-07-30 12:06:12 -0700757
Eric Anholt40123c12009-03-09 13:42:30 -0700758 while (remain > 0) {
759 /* Operation in this page
760 *
761 * page_base = page offset within aperture
762 * page_offset = offset within page
763 * page_length = bytes to copy for this page
764 */
765 page_base = (offset & ~(PAGE_SIZE-1));
766 page_offset = offset & (PAGE_SIZE-1);
767 page_length = remain;
768 if ((page_offset + remain) > PAGE_SIZE)
769 page_length = PAGE_SIZE - page_offset;
770
771 ret = fast_shmem_write(obj_priv->pages,
772 page_base, page_offset,
773 user_data, page_length);
774 if (ret)
775 goto fail_put_pages;
776
777 remain -= page_length;
778 user_data += page_length;
779 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700780 }
781
Eric Anholt40123c12009-03-09 13:42:30 -0700782fail_put_pages:
783 i915_gem_object_put_pages(obj);
784fail_unlock:
Eric Anholt673a3942008-07-30 12:06:12 -0700785 mutex_unlock(&dev->struct_mutex);
786
Eric Anholt40123c12009-03-09 13:42:30 -0700787 return ret;
788}
789
790/**
791 * This is the fallback shmem pwrite path, which uses get_user_pages to pin
792 * the memory and maps it using kmap_atomic for copying.
793 *
794 * This avoids taking mmap_sem for faulting on the user's address while the
795 * struct_mutex is held.
796 */
797static int
798i915_gem_shmem_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
799 struct drm_i915_gem_pwrite *args,
800 struct drm_file *file_priv)
801{
Daniel Vetter23010e42010-03-08 13:35:02 +0100802 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700803 struct mm_struct *mm = current->mm;
804 struct page **user_pages;
805 ssize_t remain;
806 loff_t offset, pinned_pages, i;
807 loff_t first_data_page, last_data_page, num_pages;
808 int shmem_page_index, shmem_page_offset;
809 int data_page_index, data_page_offset;
810 int page_length;
811 int ret;
812 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700813 int do_bit17_swizzling;
Eric Anholt40123c12009-03-09 13:42:30 -0700814
815 remain = args->size;
816
817 /* Pin the user pages containing the data. We can't fault while
818 * holding the struct mutex, and all of the pwrite implementations
819 * want to hold it while dereferencing the user data.
820 */
821 first_data_page = data_ptr / PAGE_SIZE;
822 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
823 num_pages = last_data_page - first_data_page + 1;
824
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700825 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
Eric Anholt40123c12009-03-09 13:42:30 -0700826 if (user_pages == NULL)
827 return -ENOMEM;
828
829 down_read(&mm->mmap_sem);
830 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
831 num_pages, 0, 0, user_pages, NULL);
832 up_read(&mm->mmap_sem);
833 if (pinned_pages < num_pages) {
834 ret = -EFAULT;
835 goto fail_put_user_pages;
836 }
837
Eric Anholt280b7132009-03-12 16:56:27 -0700838 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
839
Eric Anholt40123c12009-03-09 13:42:30 -0700840 mutex_lock(&dev->struct_mutex);
841
Chris Wilson07f73f62009-09-14 16:50:30 +0100842 ret = i915_gem_object_get_pages_or_evict(obj);
843 if (ret)
Eric Anholt40123c12009-03-09 13:42:30 -0700844 goto fail_unlock;
845
846 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
847 if (ret != 0)
848 goto fail_put_pages;
849
Daniel Vetter23010e42010-03-08 13:35:02 +0100850 obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700851 offset = args->offset;
852 obj_priv->dirty = 1;
853
854 while (remain > 0) {
855 /* Operation in this page
856 *
857 * shmem_page_index = page number within shmem file
858 * shmem_page_offset = offset within page in shmem file
859 * data_page_index = page number in get_user_pages return
860 * data_page_offset = offset with data_page_index page.
861 * page_length = bytes to copy for this page
862 */
863 shmem_page_index = offset / PAGE_SIZE;
864 shmem_page_offset = offset & ~PAGE_MASK;
865 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
866 data_page_offset = data_ptr & ~PAGE_MASK;
867
868 page_length = remain;
869 if ((shmem_page_offset + page_length) > PAGE_SIZE)
870 page_length = PAGE_SIZE - shmem_page_offset;
871 if ((data_page_offset + page_length) > PAGE_SIZE)
872 page_length = PAGE_SIZE - data_page_offset;
873
Eric Anholt280b7132009-03-12 16:56:27 -0700874 if (do_bit17_swizzling) {
Chris Wilson99a03df2010-05-27 14:15:34 +0100875 slow_shmem_bit17_copy(obj_priv->pages[shmem_page_index],
Eric Anholt280b7132009-03-12 16:56:27 -0700876 shmem_page_offset,
877 user_pages[data_page_index],
878 data_page_offset,
Chris Wilson99a03df2010-05-27 14:15:34 +0100879 page_length,
880 0);
881 } else {
882 slow_shmem_copy(obj_priv->pages[shmem_page_index],
883 shmem_page_offset,
884 user_pages[data_page_index],
885 data_page_offset,
886 page_length);
Eric Anholt280b7132009-03-12 16:56:27 -0700887 }
Eric Anholt40123c12009-03-09 13:42:30 -0700888
889 remain -= page_length;
890 data_ptr += page_length;
891 offset += page_length;
892 }
893
894fail_put_pages:
895 i915_gem_object_put_pages(obj);
896fail_unlock:
897 mutex_unlock(&dev->struct_mutex);
898fail_put_user_pages:
899 for (i = 0; i < pinned_pages; i++)
900 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700901 drm_free_large(user_pages);
Eric Anholt40123c12009-03-09 13:42:30 -0700902
903 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700904}
905
906/**
907 * Writes data to the object referenced by handle.
908 *
909 * On error, the contents of the buffer that were to be modified are undefined.
910 */
911int
912i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
913 struct drm_file *file_priv)
914{
915 struct drm_i915_gem_pwrite *args = data;
916 struct drm_gem_object *obj;
917 struct drm_i915_gem_object *obj_priv;
918 int ret = 0;
919
920 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
921 if (obj == NULL)
922 return -EBADF;
Daniel Vetter23010e42010-03-08 13:35:02 +0100923 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700924
925 /* Bounds check destination.
926 *
927 * XXX: This could use review for overflow issues...
928 */
929 if (args->offset > obj->size || args->size > obj->size ||
930 args->offset + args->size > obj->size) {
Luca Barbieribc9025b2010-02-09 05:49:12 +0000931 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700932 return -EINVAL;
933 }
934
935 /* We can only do the GTT pwrite on untiled buffers, as otherwise
936 * it would end up going through the fenced access, and we'll get
937 * different detiling behavior between reading and writing.
938 * pread/pwrite currently are reading and writing from the CPU
939 * perspective, requiring manual detiling by the client.
940 */
Dave Airlie71acb5e2008-12-30 20:31:46 +1000941 if (obj_priv->phys_obj)
942 ret = i915_gem_phys_pwrite(dev, obj, args, file_priv);
943 else if (obj_priv->tiling_mode == I915_TILING_NONE &&
Chris Wilson9b8c4a02010-05-27 14:21:01 +0100944 dev->gtt_total != 0 &&
945 obj->write_domain != I915_GEM_DOMAIN_CPU) {
Eric Anholt3de09aa2009-03-09 09:42:23 -0700946 ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file_priv);
947 if (ret == -EFAULT) {
948 ret = i915_gem_gtt_pwrite_slow(dev, obj, args,
949 file_priv);
950 }
Eric Anholt280b7132009-03-12 16:56:27 -0700951 } else if (i915_gem_object_needs_bit17_swizzle(obj)) {
952 ret = i915_gem_shmem_pwrite_slow(dev, obj, args, file_priv);
Eric Anholt40123c12009-03-09 13:42:30 -0700953 } else {
954 ret = i915_gem_shmem_pwrite_fast(dev, obj, args, file_priv);
955 if (ret == -EFAULT) {
956 ret = i915_gem_shmem_pwrite_slow(dev, obj, args,
957 file_priv);
958 }
959 }
Eric Anholt673a3942008-07-30 12:06:12 -0700960
961#if WATCH_PWRITE
962 if (ret)
963 DRM_INFO("pwrite failed %d\n", ret);
964#endif
965
Luca Barbieribc9025b2010-02-09 05:49:12 +0000966 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700967
968 return ret;
969}
970
971/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800972 * Called when user space prepares to use an object with the CPU, either
973 * through the mmap ioctl's mapping or a GTT mapping.
Eric Anholt673a3942008-07-30 12:06:12 -0700974 */
975int
976i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
977 struct drm_file *file_priv)
978{
Eric Anholta09ba7f2009-08-29 12:49:51 -0700979 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700980 struct drm_i915_gem_set_domain *args = data;
981 struct drm_gem_object *obj;
Jesse Barnes652c3932009-08-17 13:31:43 -0700982 struct drm_i915_gem_object *obj_priv;
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800983 uint32_t read_domains = args->read_domains;
984 uint32_t write_domain = args->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -0700985 int ret;
986
987 if (!(dev->driver->driver_features & DRIVER_GEM))
988 return -ENODEV;
989
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800990 /* Only handle setting domains to types used by the CPU. */
Chris Wilson21d509e2009-06-06 09:46:02 +0100991 if (write_domain & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800992 return -EINVAL;
993
Chris Wilson21d509e2009-06-06 09:46:02 +0100994 if (read_domains & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800995 return -EINVAL;
996
997 /* Having something in the write domain implies it's in the read
998 * domain, and only that read domain. Enforce that in the request.
999 */
1000 if (write_domain != 0 && read_domains != write_domain)
1001 return -EINVAL;
1002
Eric Anholt673a3942008-07-30 12:06:12 -07001003 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1004 if (obj == NULL)
1005 return -EBADF;
Daniel Vetter23010e42010-03-08 13:35:02 +01001006 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001007
1008 mutex_lock(&dev->struct_mutex);
Jesse Barnes652c3932009-08-17 13:31:43 -07001009
1010 intel_mark_busy(dev, obj);
1011
Eric Anholt673a3942008-07-30 12:06:12 -07001012#if WATCH_BUF
Krzysztof Halasacfd43c02009-06-20 00:31:28 +02001013 DRM_INFO("set_domain_ioctl %p(%zd), %08x %08x\n",
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001014 obj, obj->size, read_domains, write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07001015#endif
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001016 if (read_domains & I915_GEM_DOMAIN_GTT) {
1017 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
Eric Anholt02354392008-11-26 13:58:13 -08001018
Eric Anholta09ba7f2009-08-29 12:49:51 -07001019 /* Update the LRU on the fence for the CPU access that's
1020 * about to occur.
1021 */
1022 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001023 struct drm_i915_fence_reg *reg =
1024 &dev_priv->fence_regs[obj_priv->fence_reg];
1025 list_move_tail(&reg->lru_list,
Eric Anholta09ba7f2009-08-29 12:49:51 -07001026 &dev_priv->mm.fence_list);
1027 }
1028
Eric Anholt02354392008-11-26 13:58:13 -08001029 /* Silently promote "you're not bound, there was nothing to do"
1030 * to success, since the client was just asking us to
1031 * make sure everything was done.
1032 */
1033 if (ret == -EINVAL)
1034 ret = 0;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001035 } else {
Eric Anholte47c68e2008-11-14 13:35:19 -08001036 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001037 }
1038
Eric Anholt673a3942008-07-30 12:06:12 -07001039 drm_gem_object_unreference(obj);
1040 mutex_unlock(&dev->struct_mutex);
1041 return ret;
1042}
1043
1044/**
1045 * Called when user space has done writes to this buffer
1046 */
1047int
1048i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
1049 struct drm_file *file_priv)
1050{
1051 struct drm_i915_gem_sw_finish *args = data;
1052 struct drm_gem_object *obj;
1053 struct drm_i915_gem_object *obj_priv;
1054 int ret = 0;
1055
1056 if (!(dev->driver->driver_features & DRIVER_GEM))
1057 return -ENODEV;
1058
1059 mutex_lock(&dev->struct_mutex);
1060 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1061 if (obj == NULL) {
1062 mutex_unlock(&dev->struct_mutex);
1063 return -EBADF;
1064 }
1065
1066#if WATCH_BUF
Krzysztof Halasacfd43c02009-06-20 00:31:28 +02001067 DRM_INFO("%s: sw_finish %d (%p %zd)\n",
Eric Anholt673a3942008-07-30 12:06:12 -07001068 __func__, args->handle, obj, obj->size);
1069#endif
Daniel Vetter23010e42010-03-08 13:35:02 +01001070 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001071
1072 /* Pinned buffers may be scanout, so flush the cache */
Eric Anholte47c68e2008-11-14 13:35:19 -08001073 if (obj_priv->pin_count)
1074 i915_gem_object_flush_cpu_write_domain(obj);
1075
Eric Anholt673a3942008-07-30 12:06:12 -07001076 drm_gem_object_unreference(obj);
1077 mutex_unlock(&dev->struct_mutex);
1078 return ret;
1079}
1080
1081/**
1082 * Maps the contents of an object, returning the address it is mapped
1083 * into.
1084 *
1085 * While the mapping holds a reference on the contents of the object, it doesn't
1086 * imply a ref on the object itself.
1087 */
1088int
1089i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
1090 struct drm_file *file_priv)
1091{
1092 struct drm_i915_gem_mmap *args = data;
1093 struct drm_gem_object *obj;
1094 loff_t offset;
1095 unsigned long addr;
1096
1097 if (!(dev->driver->driver_features & DRIVER_GEM))
1098 return -ENODEV;
1099
1100 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1101 if (obj == NULL)
1102 return -EBADF;
1103
1104 offset = args->offset;
1105
1106 down_write(&current->mm->mmap_sem);
1107 addr = do_mmap(obj->filp, 0, args->size,
1108 PROT_READ | PROT_WRITE, MAP_SHARED,
1109 args->offset);
1110 up_write(&current->mm->mmap_sem);
Luca Barbieribc9025b2010-02-09 05:49:12 +00001111 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001112 if (IS_ERR((void *)addr))
1113 return addr;
1114
1115 args->addr_ptr = (uint64_t) addr;
1116
1117 return 0;
1118}
1119
Jesse Barnesde151cf2008-11-12 10:03:55 -08001120/**
1121 * i915_gem_fault - fault a page into the GTT
1122 * vma: VMA in question
1123 * vmf: fault info
1124 *
1125 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1126 * from userspace. The fault handler takes care of binding the object to
1127 * the GTT (if needed), allocating and programming a fence register (again,
1128 * only if needed based on whether the old reg is still valid or the object
1129 * is tiled) and inserting a new PTE into the faulting process.
1130 *
1131 * Note that the faulting process may involve evicting existing objects
1132 * from the GTT and/or fence registers to make room. So performance may
1133 * suffer if the GTT working set is large or there are few fence registers
1134 * left.
1135 */
1136int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1137{
1138 struct drm_gem_object *obj = vma->vm_private_data;
1139 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001140 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001141 pgoff_t page_offset;
1142 unsigned long pfn;
1143 int ret = 0;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001144 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001145
1146 /* We don't use vmf->pgoff since that has the fake offset */
1147 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1148 PAGE_SHIFT;
1149
1150 /* Now bind it into the GTT if needed */
1151 mutex_lock(&dev->struct_mutex);
1152 if (!obj_priv->gtt_space) {
Chris Wilsone67b8ce2009-09-14 16:50:26 +01001153 ret = i915_gem_object_bind_to_gtt(obj, 0);
Chris Wilsonc7150892009-09-23 00:43:56 +01001154 if (ret)
1155 goto unlock;
Kristian Høgsberg07f4f3e2009-05-27 14:37:28 -04001156
Jesse Barnesde151cf2008-11-12 10:03:55 -08001157 ret = i915_gem_object_set_to_gtt_domain(obj, write);
Chris Wilsonc7150892009-09-23 00:43:56 +01001158 if (ret)
1159 goto unlock;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001160 }
1161
1162 /* Need a new fence register? */
Eric Anholta09ba7f2009-08-29 12:49:51 -07001163 if (obj_priv->tiling_mode != I915_TILING_NONE) {
Chris Wilson8c4b8c32009-06-17 22:08:52 +01001164 ret = i915_gem_object_get_fence_reg(obj);
Chris Wilsonc7150892009-09-23 00:43:56 +01001165 if (ret)
1166 goto unlock;
Eric Anholtd9ddcb92009-01-27 10:33:49 -08001167 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001168
1169 pfn = ((dev->agp->base + obj_priv->gtt_offset) >> PAGE_SHIFT) +
1170 page_offset;
1171
1172 /* Finally, remap it using the new GTT offset */
1173 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
Chris Wilsonc7150892009-09-23 00:43:56 +01001174unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001175 mutex_unlock(&dev->struct_mutex);
1176
1177 switch (ret) {
Chris Wilsonc7150892009-09-23 00:43:56 +01001178 case 0:
1179 case -ERESTARTSYS:
1180 return VM_FAULT_NOPAGE;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001181 case -ENOMEM:
1182 case -EAGAIN:
1183 return VM_FAULT_OOM;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001184 default:
Chris Wilsonc7150892009-09-23 00:43:56 +01001185 return VM_FAULT_SIGBUS;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001186 }
1187}
1188
1189/**
1190 * i915_gem_create_mmap_offset - create a fake mmap offset for an object
1191 * @obj: obj in question
1192 *
1193 * GEM memory mapping works by handing back to userspace a fake mmap offset
1194 * it can use in a subsequent mmap(2) call. The DRM core code then looks
1195 * up the object based on the offset and sets up the various memory mapping
1196 * structures.
1197 *
1198 * This routine allocates and attaches a fake offset for @obj.
1199 */
1200static int
1201i915_gem_create_mmap_offset(struct drm_gem_object *obj)
1202{
1203 struct drm_device *dev = obj->dev;
1204 struct drm_gem_mm *mm = dev->mm_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001205 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001206 struct drm_map_list *list;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +11001207 struct drm_local_map *map;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001208 int ret = 0;
1209
1210 /* Set the object up for mmap'ing */
1211 list = &obj->map_list;
Eric Anholt9a298b22009-03-24 12:23:04 -07001212 list->map = kzalloc(sizeof(struct drm_map_list), GFP_KERNEL);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001213 if (!list->map)
1214 return -ENOMEM;
1215
1216 map = list->map;
1217 map->type = _DRM_GEM;
1218 map->size = obj->size;
1219 map->handle = obj;
1220
1221 /* Get a DRM GEM mmap offset allocated... */
1222 list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
1223 obj->size / PAGE_SIZE, 0, 0);
1224 if (!list->file_offset_node) {
1225 DRM_ERROR("failed to allocate offset for bo %d\n", obj->name);
1226 ret = -ENOMEM;
1227 goto out_free_list;
1228 }
1229
1230 list->file_offset_node = drm_mm_get_block(list->file_offset_node,
1231 obj->size / PAGE_SIZE, 0);
1232 if (!list->file_offset_node) {
1233 ret = -ENOMEM;
1234 goto out_free_list;
1235 }
1236
1237 list->hash.key = list->file_offset_node->start;
1238 if (drm_ht_insert_item(&mm->offset_hash, &list->hash)) {
1239 DRM_ERROR("failed to add to map hash\n");
Chris Wilson5618ca62009-12-02 15:15:30 +00001240 ret = -ENOMEM;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001241 goto out_free_mm;
1242 }
1243
1244 /* By now we should be all set, any drm_mmap request on the offset
1245 * below will get to our mmap & fault handler */
1246 obj_priv->mmap_offset = ((uint64_t) list->hash.key) << PAGE_SHIFT;
1247
1248 return 0;
1249
1250out_free_mm:
1251 drm_mm_put_block(list->file_offset_node);
1252out_free_list:
Eric Anholt9a298b22009-03-24 12:23:04 -07001253 kfree(list->map);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001254
1255 return ret;
1256}
1257
Chris Wilson901782b2009-07-10 08:18:50 +01001258/**
1259 * i915_gem_release_mmap - remove physical page mappings
1260 * @obj: obj in question
1261 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001262 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01001263 * relinquish ownership of the pages back to the system.
1264 *
1265 * It is vital that we remove the page mapping if we have mapped a tiled
1266 * object through the GTT and then lose the fence register due to
1267 * resource pressure. Similarly if the object has been moved out of the
1268 * aperture, than pages mapped into userspace must be revoked. Removing the
1269 * mapping will then trigger a page fault on the next user access, allowing
1270 * fixup by i915_gem_fault().
1271 */
Eric Anholtd05ca302009-07-10 13:02:26 -07001272void
Chris Wilson901782b2009-07-10 08:18:50 +01001273i915_gem_release_mmap(struct drm_gem_object *obj)
1274{
1275 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001276 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson901782b2009-07-10 08:18:50 +01001277
1278 if (dev->dev_mapping)
1279 unmap_mapping_range(dev->dev_mapping,
1280 obj_priv->mmap_offset, obj->size, 1);
1281}
1282
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001283static void
1284i915_gem_free_mmap_offset(struct drm_gem_object *obj)
1285{
1286 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001287 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001288 struct drm_gem_mm *mm = dev->mm_private;
1289 struct drm_map_list *list;
1290
1291 list = &obj->map_list;
1292 drm_ht_remove_item(&mm->offset_hash, &list->hash);
1293
1294 if (list->file_offset_node) {
1295 drm_mm_put_block(list->file_offset_node);
1296 list->file_offset_node = NULL;
1297 }
1298
1299 if (list->map) {
Eric Anholt9a298b22009-03-24 12:23:04 -07001300 kfree(list->map);
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001301 list->map = NULL;
1302 }
1303
1304 obj_priv->mmap_offset = 0;
1305}
1306
Jesse Barnesde151cf2008-11-12 10:03:55 -08001307/**
1308 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1309 * @obj: object to check
1310 *
1311 * Return the required GTT alignment for an object, taking into account
1312 * potential fence register mapping if needed.
1313 */
1314static uint32_t
1315i915_gem_get_gtt_alignment(struct drm_gem_object *obj)
1316{
1317 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001318 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001319 int start, i;
1320
1321 /*
1322 * Minimum alignment is 4k (GTT page size), but might be greater
1323 * if a fence register is needed for the object.
1324 */
1325 if (IS_I965G(dev) || obj_priv->tiling_mode == I915_TILING_NONE)
1326 return 4096;
1327
1328 /*
1329 * Previous chips need to be aligned to the size of the smallest
1330 * fence register that can contain the object.
1331 */
1332 if (IS_I9XX(dev))
1333 start = 1024*1024;
1334 else
1335 start = 512*1024;
1336
1337 for (i = start; i < obj->size; i <<= 1)
1338 ;
1339
1340 return i;
1341}
1342
1343/**
1344 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1345 * @dev: DRM device
1346 * @data: GTT mapping ioctl data
1347 * @file_priv: GEM object info
1348 *
1349 * Simply returns the fake offset to userspace so it can mmap it.
1350 * The mmap call will end up in drm_gem_mmap(), which will set things
1351 * up so we can get faults in the handler above.
1352 *
1353 * The fault handler will take care of binding the object into the GTT
1354 * (since it may have been evicted to make room for something), allocating
1355 * a fence register, and mapping the appropriate aperture address into
1356 * userspace.
1357 */
1358int
1359i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
1360 struct drm_file *file_priv)
1361{
1362 struct drm_i915_gem_mmap_gtt *args = data;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001363 struct drm_gem_object *obj;
1364 struct drm_i915_gem_object *obj_priv;
1365 int ret;
1366
1367 if (!(dev->driver->driver_features & DRIVER_GEM))
1368 return -ENODEV;
1369
1370 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1371 if (obj == NULL)
1372 return -EBADF;
1373
1374 mutex_lock(&dev->struct_mutex);
1375
Daniel Vetter23010e42010-03-08 13:35:02 +01001376 obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001377
Chris Wilsonab182822009-09-22 18:46:17 +01001378 if (obj_priv->madv != I915_MADV_WILLNEED) {
1379 DRM_ERROR("Attempting to mmap a purgeable buffer\n");
1380 drm_gem_object_unreference(obj);
1381 mutex_unlock(&dev->struct_mutex);
1382 return -EINVAL;
1383 }
1384
1385
Jesse Barnesde151cf2008-11-12 10:03:55 -08001386 if (!obj_priv->mmap_offset) {
1387 ret = i915_gem_create_mmap_offset(obj);
Chris Wilson13af1062009-02-11 14:26:31 +00001388 if (ret) {
1389 drm_gem_object_unreference(obj);
1390 mutex_unlock(&dev->struct_mutex);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001391 return ret;
Chris Wilson13af1062009-02-11 14:26:31 +00001392 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001393 }
1394
1395 args->offset = obj_priv->mmap_offset;
1396
Jesse Barnesde151cf2008-11-12 10:03:55 -08001397 /*
1398 * Pull it into the GTT so that we have a page list (makes the
1399 * initial fault faster and any subsequent flushing possible).
1400 */
1401 if (!obj_priv->agp_mem) {
Chris Wilsone67b8ce2009-09-14 16:50:26 +01001402 ret = i915_gem_object_bind_to_gtt(obj, 0);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001403 if (ret) {
1404 drm_gem_object_unreference(obj);
1405 mutex_unlock(&dev->struct_mutex);
1406 return ret;
1407 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001408 }
1409
1410 drm_gem_object_unreference(obj);
1411 mutex_unlock(&dev->struct_mutex);
1412
1413 return 0;
1414}
1415
Ben Gamari6911a9b2009-04-02 11:24:54 -07001416void
Eric Anholt856fa192009-03-19 14:10:50 -07001417i915_gem_object_put_pages(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07001418{
Daniel Vetter23010e42010-03-08 13:35:02 +01001419 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001420 int page_count = obj->size / PAGE_SIZE;
1421 int i;
1422
Eric Anholt856fa192009-03-19 14:10:50 -07001423 BUG_ON(obj_priv->pages_refcount == 0);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001424 BUG_ON(obj_priv->madv == __I915_MADV_PURGED);
Eric Anholt856fa192009-03-19 14:10:50 -07001425
1426 if (--obj_priv->pages_refcount != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07001427 return;
1428
Eric Anholt280b7132009-03-12 16:56:27 -07001429 if (obj_priv->tiling_mode != I915_TILING_NONE)
1430 i915_gem_object_save_bit_17_swizzle(obj);
1431
Chris Wilson3ef94da2009-09-14 16:50:29 +01001432 if (obj_priv->madv == I915_MADV_DONTNEED)
Chris Wilson13a05fd2009-09-20 23:03:19 +01001433 obj_priv->dirty = 0;
Chris Wilson3ef94da2009-09-14 16:50:29 +01001434
1435 for (i = 0; i < page_count; i++) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01001436 if (obj_priv->dirty)
1437 set_page_dirty(obj_priv->pages[i]);
1438
1439 if (obj_priv->madv == I915_MADV_WILLNEED)
Eric Anholt856fa192009-03-19 14:10:50 -07001440 mark_page_accessed(obj_priv->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001441
1442 page_cache_release(obj_priv->pages[i]);
1443 }
Eric Anholt673a3942008-07-30 12:06:12 -07001444 obj_priv->dirty = 0;
1445
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07001446 drm_free_large(obj_priv->pages);
Eric Anholt856fa192009-03-19 14:10:50 -07001447 obj_priv->pages = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001448}
1449
1450static void
Zou Nan hai852835f2010-05-21 09:08:56 +08001451i915_gem_object_move_to_active(struct drm_gem_object *obj, uint32_t seqno,
1452 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001453{
1454 struct drm_device *dev = obj->dev;
1455 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001456 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Zou Nan hai852835f2010-05-21 09:08:56 +08001457 BUG_ON(ring == NULL);
1458 obj_priv->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001459
1460 /* Add a reference if we're newly entering the active list. */
1461 if (!obj_priv->active) {
1462 drm_gem_object_reference(obj);
1463 obj_priv->active = 1;
1464 }
1465 /* Move from whatever list we were on to the tail of execution. */
Carl Worth5e118f42009-03-20 11:54:25 -07001466 spin_lock(&dev_priv->mm.active_list_lock);
Zou Nan hai852835f2010-05-21 09:08:56 +08001467 list_move_tail(&obj_priv->list, &ring->active_list);
Carl Worth5e118f42009-03-20 11:54:25 -07001468 spin_unlock(&dev_priv->mm.active_list_lock);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001469 obj_priv->last_rendering_seqno = seqno;
Eric Anholt673a3942008-07-30 12:06:12 -07001470}
1471
Eric Anholtce44b0e2008-11-06 16:00:31 -08001472static void
1473i915_gem_object_move_to_flushing(struct drm_gem_object *obj)
1474{
1475 struct drm_device *dev = obj->dev;
1476 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001477 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001478
1479 BUG_ON(!obj_priv->active);
1480 list_move_tail(&obj_priv->list, &dev_priv->mm.flushing_list);
1481 obj_priv->last_rendering_seqno = 0;
1482}
Eric Anholt673a3942008-07-30 12:06:12 -07001483
Chris Wilson963b4832009-09-20 23:03:54 +01001484/* Immediately discard the backing storage */
1485static void
1486i915_gem_object_truncate(struct drm_gem_object *obj)
1487{
Daniel Vetter23010e42010-03-08 13:35:02 +01001488 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001489 struct inode *inode;
Chris Wilson963b4832009-09-20 23:03:54 +01001490
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001491 inode = obj->filp->f_path.dentry->d_inode;
1492 if (inode->i_op->truncate)
1493 inode->i_op->truncate (inode);
1494
1495 obj_priv->madv = __I915_MADV_PURGED;
Chris Wilson963b4832009-09-20 23:03:54 +01001496}
1497
1498static inline int
1499i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj_priv)
1500{
1501 return obj_priv->madv == I915_MADV_DONTNEED;
1502}
1503
Eric Anholt673a3942008-07-30 12:06:12 -07001504static void
1505i915_gem_object_move_to_inactive(struct drm_gem_object *obj)
1506{
1507 struct drm_device *dev = obj->dev;
1508 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001509 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001510
1511 i915_verify_inactive(dev, __FILE__, __LINE__);
1512 if (obj_priv->pin_count != 0)
1513 list_del_init(&obj_priv->list);
1514 else
1515 list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
1516
Daniel Vetter99fcb762010-02-07 16:20:18 +01001517 BUG_ON(!list_empty(&obj_priv->gpu_write_list));
1518
Eric Anholtce44b0e2008-11-06 16:00:31 -08001519 obj_priv->last_rendering_seqno = 0;
Zou Nan hai852835f2010-05-21 09:08:56 +08001520 obj_priv->ring = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001521 if (obj_priv->active) {
1522 obj_priv->active = 0;
1523 drm_gem_object_unreference(obj);
1524 }
1525 i915_verify_inactive(dev, __FILE__, __LINE__);
1526}
1527
Daniel Vetter63560392010-02-19 11:51:59 +01001528static void
1529i915_gem_process_flushing_list(struct drm_device *dev,
Zou Nan hai852835f2010-05-21 09:08:56 +08001530 uint32_t flush_domains, uint32_t seqno,
1531 struct intel_ring_buffer *ring)
Daniel Vetter63560392010-02-19 11:51:59 +01001532{
1533 drm_i915_private_t *dev_priv = dev->dev_private;
1534 struct drm_i915_gem_object *obj_priv, *next;
1535
1536 list_for_each_entry_safe(obj_priv, next,
1537 &dev_priv->mm.gpu_write_list,
1538 gpu_write_list) {
Daniel Vettera8089e82010-04-09 19:05:09 +00001539 struct drm_gem_object *obj = &obj_priv->base;
Daniel Vetter63560392010-02-19 11:51:59 +01001540
1541 if ((obj->write_domain & flush_domains) ==
Zou Nan hai852835f2010-05-21 09:08:56 +08001542 obj->write_domain &&
1543 obj_priv->ring->ring_flag == ring->ring_flag) {
Daniel Vetter63560392010-02-19 11:51:59 +01001544 uint32_t old_write_domain = obj->write_domain;
1545
1546 obj->write_domain = 0;
1547 list_del_init(&obj_priv->gpu_write_list);
Zou Nan hai852835f2010-05-21 09:08:56 +08001548 i915_gem_object_move_to_active(obj, seqno, ring);
Daniel Vetter63560392010-02-19 11:51:59 +01001549
1550 /* update the fence lru list */
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001551 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
1552 struct drm_i915_fence_reg *reg =
1553 &dev_priv->fence_regs[obj_priv->fence_reg];
1554 list_move_tail(&reg->lru_list,
Daniel Vetter63560392010-02-19 11:51:59 +01001555 &dev_priv->mm.fence_list);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001556 }
Daniel Vetter63560392010-02-19 11:51:59 +01001557
1558 trace_i915_gem_object_change_domain(obj,
1559 obj->read_domains,
1560 old_write_domain);
1561 }
1562 }
1563}
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001564
Daniel Vetter5a5a0c62009-09-15 22:57:36 +02001565uint32_t
Eric Anholtb9624422009-06-03 07:27:35 +00001566i915_add_request(struct drm_device *dev, struct drm_file *file_priv,
Zou Nan hai852835f2010-05-21 09:08:56 +08001567 uint32_t flush_domains, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001568{
1569 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtb9624422009-06-03 07:27:35 +00001570 struct drm_i915_file_private *i915_file_priv = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001571 struct drm_i915_gem_request *request;
1572 uint32_t seqno;
1573 int was_empty;
Eric Anholt673a3942008-07-30 12:06:12 -07001574
Eric Anholtb9624422009-06-03 07:27:35 +00001575 if (file_priv != NULL)
1576 i915_file_priv = file_priv->driver_priv;
1577
Eric Anholt9a298b22009-03-24 12:23:04 -07001578 request = kzalloc(sizeof(*request), GFP_KERNEL);
Eric Anholt673a3942008-07-30 12:06:12 -07001579 if (request == NULL)
1580 return 0;
1581
Zou Nan hai852835f2010-05-21 09:08:56 +08001582 seqno = ring->add_request(dev, ring, file_priv, flush_domains);
Eric Anholt673a3942008-07-30 12:06:12 -07001583
1584 request->seqno = seqno;
Zou Nan hai852835f2010-05-21 09:08:56 +08001585 request->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001586 request->emitted_jiffies = jiffies;
Zou Nan hai852835f2010-05-21 09:08:56 +08001587 was_empty = list_empty(&ring->request_list);
1588 list_add_tail(&request->list, &ring->request_list);
1589
Eric Anholtb9624422009-06-03 07:27:35 +00001590 if (i915_file_priv) {
1591 list_add_tail(&request->client_list,
1592 &i915_file_priv->mm.request_list);
1593 } else {
1594 INIT_LIST_HEAD(&request->client_list);
1595 }
Eric Anholt673a3942008-07-30 12:06:12 -07001596
Eric Anholtce44b0e2008-11-06 16:00:31 -08001597 /* Associate any objects on the flushing list matching the write
1598 * domain we're flushing with our flush.
1599 */
Daniel Vetter63560392010-02-19 11:51:59 +01001600 if (flush_domains != 0)
Zou Nan hai852835f2010-05-21 09:08:56 +08001601 i915_gem_process_flushing_list(dev, flush_domains, seqno, ring);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001602
Ben Gamarif65d9422009-09-14 17:48:44 -04001603 if (!dev_priv->mm.suspended) {
1604 mod_timer(&dev_priv->hangcheck_timer, jiffies + DRM_I915_HANGCHECK_PERIOD);
1605 if (was_empty)
1606 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
1607 }
Eric Anholt673a3942008-07-30 12:06:12 -07001608 return seqno;
1609}
1610
1611/**
1612 * Command execution barrier
1613 *
1614 * Ensures that all commands in the ring are finished
1615 * before signalling the CPU
1616 */
Eric Anholt3043c602008-10-02 12:24:47 -07001617static uint32_t
Zou Nan hai852835f2010-05-21 09:08:56 +08001618i915_retire_commands(struct drm_device *dev, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001619{
Eric Anholt673a3942008-07-30 12:06:12 -07001620 uint32_t flush_domains = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001621
1622 /* The sampler always gets flushed on i965 (sigh) */
1623 if (IS_I965G(dev))
1624 flush_domains |= I915_GEM_DOMAIN_SAMPLER;
Zou Nan hai852835f2010-05-21 09:08:56 +08001625
1626 ring->flush(dev, ring,
1627 I915_GEM_DOMAIN_COMMAND, flush_domains);
Eric Anholt673a3942008-07-30 12:06:12 -07001628 return flush_domains;
1629}
1630
1631/**
1632 * Moves buffers associated only with the given active seqno from the active
1633 * to inactive list, potentially freeing them.
1634 */
1635static void
1636i915_gem_retire_request(struct drm_device *dev,
1637 struct drm_i915_gem_request *request)
1638{
1639 drm_i915_private_t *dev_priv = dev->dev_private;
1640
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001641 trace_i915_gem_request_retire(dev, request->seqno);
1642
Eric Anholt673a3942008-07-30 12:06:12 -07001643 /* Move any buffers on the active list that are no longer referenced
1644 * by the ringbuffer to the flushing/inactive lists as appropriate.
1645 */
Carl Worth5e118f42009-03-20 11:54:25 -07001646 spin_lock(&dev_priv->mm.active_list_lock);
Zou Nan hai852835f2010-05-21 09:08:56 +08001647 while (!list_empty(&request->ring->active_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001648 struct drm_gem_object *obj;
1649 struct drm_i915_gem_object *obj_priv;
1650
Zou Nan hai852835f2010-05-21 09:08:56 +08001651 obj_priv = list_first_entry(&request->ring->active_list,
Eric Anholt673a3942008-07-30 12:06:12 -07001652 struct drm_i915_gem_object,
1653 list);
Daniel Vettera8089e82010-04-09 19:05:09 +00001654 obj = &obj_priv->base;
Eric Anholt673a3942008-07-30 12:06:12 -07001655
1656 /* If the seqno being retired doesn't match the oldest in the
1657 * list, then the oldest in the list must still be newer than
1658 * this seqno.
1659 */
1660 if (obj_priv->last_rendering_seqno != request->seqno)
Carl Worth5e118f42009-03-20 11:54:25 -07001661 goto out;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001662
Eric Anholt673a3942008-07-30 12:06:12 -07001663#if WATCH_LRU
1664 DRM_INFO("%s: retire %d moves to inactive list %p\n",
1665 __func__, request->seqno, obj);
1666#endif
1667
Eric Anholtce44b0e2008-11-06 16:00:31 -08001668 if (obj->write_domain != 0)
1669 i915_gem_object_move_to_flushing(obj);
Shaohua Li68c84342009-04-08 10:58:23 +08001670 else {
1671 /* Take a reference on the object so it won't be
1672 * freed while the spinlock is held. The list
1673 * protection for this spinlock is safe when breaking
1674 * the lock like this since the next thing we do
1675 * is just get the head of the list again.
1676 */
1677 drm_gem_object_reference(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001678 i915_gem_object_move_to_inactive(obj);
Shaohua Li68c84342009-04-08 10:58:23 +08001679 spin_unlock(&dev_priv->mm.active_list_lock);
1680 drm_gem_object_unreference(obj);
1681 spin_lock(&dev_priv->mm.active_list_lock);
1682 }
Eric Anholt673a3942008-07-30 12:06:12 -07001683 }
Carl Worth5e118f42009-03-20 11:54:25 -07001684out:
1685 spin_unlock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001686}
1687
1688/**
1689 * Returns true if seq1 is later than seq2.
1690 */
Ben Gamari22be1722009-09-14 17:48:43 -04001691bool
Eric Anholt673a3942008-07-30 12:06:12 -07001692i915_seqno_passed(uint32_t seq1, uint32_t seq2)
1693{
1694 return (int32_t)(seq1 - seq2) >= 0;
1695}
1696
1697uint32_t
Zou Nan hai852835f2010-05-21 09:08:56 +08001698i915_get_gem_seqno(struct drm_device *dev,
Zou Nan haid1b851f2010-05-21 09:08:57 +08001699 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001700{
Zou Nan hai852835f2010-05-21 09:08:56 +08001701 return ring->get_gem_seqno(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07001702}
1703
1704/**
1705 * This function clears the request list as sequence numbers are passed.
1706 */
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001707static void
1708i915_gem_retire_requests_ring(struct drm_device *dev,
1709 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001710{
1711 drm_i915_private_t *dev_priv = dev->dev_private;
1712 uint32_t seqno;
1713
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001714 if (!ring->status_page.page_addr
Zou Nan hai852835f2010-05-21 09:08:56 +08001715 || list_empty(&ring->request_list))
Karsten Wiese6c0594a2009-02-23 15:07:57 +01001716 return;
1717
Zou Nan hai852835f2010-05-21 09:08:56 +08001718 seqno = i915_get_gem_seqno(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07001719
Zou Nan hai852835f2010-05-21 09:08:56 +08001720 while (!list_empty(&ring->request_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001721 struct drm_i915_gem_request *request;
1722 uint32_t retiring_seqno;
1723
Zou Nan hai852835f2010-05-21 09:08:56 +08001724 request = list_first_entry(&ring->request_list,
Eric Anholt673a3942008-07-30 12:06:12 -07001725 struct drm_i915_gem_request,
1726 list);
1727 retiring_seqno = request->seqno;
1728
1729 if (i915_seqno_passed(seqno, retiring_seqno) ||
Ben Gamariba1234d2009-09-14 17:48:47 -04001730 atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001731 i915_gem_retire_request(dev, request);
1732
1733 list_del(&request->list);
Eric Anholtb9624422009-06-03 07:27:35 +00001734 list_del(&request->client_list);
Eric Anholt9a298b22009-03-24 12:23:04 -07001735 kfree(request);
Eric Anholt673a3942008-07-30 12:06:12 -07001736 } else
1737 break;
1738 }
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001739
1740 if (unlikely (dev_priv->trace_irq_seqno &&
1741 i915_seqno_passed(dev_priv->trace_irq_seqno, seqno))) {
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001742
1743 ring->user_irq_put(dev, ring);
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001744 dev_priv->trace_irq_seqno = 0;
1745 }
Eric Anholt673a3942008-07-30 12:06:12 -07001746}
1747
1748void
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001749i915_gem_retire_requests(struct drm_device *dev)
1750{
1751 drm_i915_private_t *dev_priv = dev->dev_private;
1752
Chris Wilsonbe726152010-07-23 23:18:50 +01001753 if (!list_empty(&dev_priv->mm.deferred_free_list)) {
1754 struct drm_i915_gem_object *obj_priv, *tmp;
1755
1756 /* We must be careful that during unbind() we do not
1757 * accidentally infinitely recurse into retire requests.
1758 * Currently:
1759 * retire -> free -> unbind -> wait -> retire_ring
1760 */
1761 list_for_each_entry_safe(obj_priv, tmp,
1762 &dev_priv->mm.deferred_free_list,
1763 list)
1764 i915_gem_free_object_tail(&obj_priv->base);
1765 }
1766
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001767 i915_gem_retire_requests_ring(dev, &dev_priv->render_ring);
1768 if (HAS_BSD(dev))
1769 i915_gem_retire_requests_ring(dev, &dev_priv->bsd_ring);
1770}
1771
1772void
Eric Anholt673a3942008-07-30 12:06:12 -07001773i915_gem_retire_work_handler(struct work_struct *work)
1774{
1775 drm_i915_private_t *dev_priv;
1776 struct drm_device *dev;
1777
1778 dev_priv = container_of(work, drm_i915_private_t,
1779 mm.retire_work.work);
1780 dev = dev_priv->dev;
1781
1782 mutex_lock(&dev->struct_mutex);
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001783 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08001784
Keith Packard6dbe2772008-10-14 21:41:13 -07001785 if (!dev_priv->mm.suspended &&
Zou Nan haid1b851f2010-05-21 09:08:57 +08001786 (!list_empty(&dev_priv->render_ring.request_list) ||
1787 (HAS_BSD(dev) &&
1788 !list_empty(&dev_priv->bsd_ring.request_list))))
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001789 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
Eric Anholt673a3942008-07-30 12:06:12 -07001790 mutex_unlock(&dev->struct_mutex);
1791}
1792
Daniel Vetter5a5a0c62009-09-15 22:57:36 +02001793int
Zou Nan hai852835f2010-05-21 09:08:56 +08001794i915_do_wait_request(struct drm_device *dev, uint32_t seqno,
1795 int interruptible, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001796{
1797 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001798 u32 ier;
Eric Anholt673a3942008-07-30 12:06:12 -07001799 int ret = 0;
1800
1801 BUG_ON(seqno == 0);
1802
Ben Gamariba1234d2009-09-14 17:48:47 -04001803 if (atomic_read(&dev_priv->mm.wedged))
Ben Gamariffed1d02009-09-14 17:48:41 -04001804 return -EIO;
1805
Zou Nan hai852835f2010-05-21 09:08:56 +08001806 if (!i915_seqno_passed(ring->get_gem_seqno(dev, ring), seqno)) {
Eric Anholtbad720f2009-10-22 16:11:14 -07001807 if (HAS_PCH_SPLIT(dev))
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001808 ier = I915_READ(DEIER) | I915_READ(GTIER);
1809 else
1810 ier = I915_READ(IER);
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001811 if (!ier) {
1812 DRM_ERROR("something (likely vbetool) disabled "
1813 "interrupts, re-enabling\n");
1814 i915_driver_irq_preinstall(dev);
1815 i915_driver_irq_postinstall(dev);
1816 }
1817
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001818 trace_i915_gem_request_wait_begin(dev, seqno);
1819
Zou Nan hai852835f2010-05-21 09:08:56 +08001820 ring->waiting_gem_seqno = seqno;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001821 ring->user_irq_get(dev, ring);
Daniel Vetter48764bf2009-09-15 22:57:32 +02001822 if (interruptible)
Zou Nan hai852835f2010-05-21 09:08:56 +08001823 ret = wait_event_interruptible(ring->irq_queue,
1824 i915_seqno_passed(
1825 ring->get_gem_seqno(dev, ring), seqno)
1826 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02001827 else
Zou Nan hai852835f2010-05-21 09:08:56 +08001828 wait_event(ring->irq_queue,
1829 i915_seqno_passed(
1830 ring->get_gem_seqno(dev, ring), seqno)
1831 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02001832
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001833 ring->user_irq_put(dev, ring);
Zou Nan hai852835f2010-05-21 09:08:56 +08001834 ring->waiting_gem_seqno = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001835
1836 trace_i915_gem_request_wait_end(dev, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07001837 }
Ben Gamariba1234d2009-09-14 17:48:47 -04001838 if (atomic_read(&dev_priv->mm.wedged))
Eric Anholt673a3942008-07-30 12:06:12 -07001839 ret = -EIO;
1840
1841 if (ret && ret != -ERESTARTSYS)
1842 DRM_ERROR("%s returns %d (awaiting %d at %d)\n",
Zou Nan hai852835f2010-05-21 09:08:56 +08001843 __func__, ret, seqno, ring->get_gem_seqno(dev, ring));
Eric Anholt673a3942008-07-30 12:06:12 -07001844
1845 /* Directly dispatch request retiring. While we have the work queue
1846 * to handle this, the waiter on a request often wants an associated
1847 * buffer to have made it to the inactive list, and we would need
1848 * a separate wait queue to handle that.
1849 */
1850 if (ret == 0)
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001851 i915_gem_retire_requests_ring(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07001852
1853 return ret;
1854}
1855
Daniel Vetter48764bf2009-09-15 22:57:32 +02001856/**
1857 * Waits for a sequence number to be signaled, and cleans up the
1858 * request and object lists appropriately for that event.
1859 */
1860static int
Zou Nan hai852835f2010-05-21 09:08:56 +08001861i915_wait_request(struct drm_device *dev, uint32_t seqno,
1862 struct intel_ring_buffer *ring)
Daniel Vetter48764bf2009-09-15 22:57:32 +02001863{
Zou Nan hai852835f2010-05-21 09:08:56 +08001864 return i915_do_wait_request(dev, seqno, 1, ring);
Daniel Vetter48764bf2009-09-15 22:57:32 +02001865}
1866
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001867static void
1868i915_gem_flush(struct drm_device *dev,
1869 uint32_t invalidate_domains,
1870 uint32_t flush_domains)
1871{
1872 drm_i915_private_t *dev_priv = dev->dev_private;
1873 if (flush_domains & I915_GEM_DOMAIN_CPU)
1874 drm_agp_chipset_flush(dev);
1875 dev_priv->render_ring.flush(dev, &dev_priv->render_ring,
1876 invalidate_domains,
1877 flush_domains);
Zou Nan haid1b851f2010-05-21 09:08:57 +08001878
1879 if (HAS_BSD(dev))
1880 dev_priv->bsd_ring.flush(dev, &dev_priv->bsd_ring,
1881 invalidate_domains,
1882 flush_domains);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001883}
1884
Eric Anholt673a3942008-07-30 12:06:12 -07001885/**
1886 * Ensures that all rendering to the object has completed and the object is
1887 * safe to unbind from the GTT or access from the CPU.
1888 */
1889static int
1890i915_gem_object_wait_rendering(struct drm_gem_object *obj)
1891{
1892 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001893 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001894 int ret;
1895
Eric Anholte47c68e2008-11-14 13:35:19 -08001896 /* This function only exists to support waiting for existing rendering,
1897 * not for emitting required flushes.
Eric Anholt673a3942008-07-30 12:06:12 -07001898 */
Eric Anholte47c68e2008-11-14 13:35:19 -08001899 BUG_ON((obj->write_domain & I915_GEM_GPU_DOMAINS) != 0);
Eric Anholt673a3942008-07-30 12:06:12 -07001900
1901 /* If there is rendering queued on the buffer being evicted, wait for
1902 * it.
1903 */
1904 if (obj_priv->active) {
1905#if WATCH_BUF
1906 DRM_INFO("%s: object %p wait for seqno %08x\n",
1907 __func__, obj, obj_priv->last_rendering_seqno);
1908#endif
Zou Nan hai852835f2010-05-21 09:08:56 +08001909 ret = i915_wait_request(dev,
1910 obj_priv->last_rendering_seqno, obj_priv->ring);
Eric Anholt673a3942008-07-30 12:06:12 -07001911 if (ret != 0)
1912 return ret;
1913 }
1914
1915 return 0;
1916}
1917
1918/**
1919 * Unbinds an object from the GTT aperture.
1920 */
Jesse Barnes0f973f22009-01-26 17:10:45 -08001921int
Eric Anholt673a3942008-07-30 12:06:12 -07001922i915_gem_object_unbind(struct drm_gem_object *obj)
1923{
1924 struct drm_device *dev = obj->dev;
Daniel Vetter4a87b8c2010-02-19 11:51:57 +01001925 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001926 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001927 int ret = 0;
1928
1929#if WATCH_BUF
1930 DRM_INFO("%s:%d %p\n", __func__, __LINE__, obj);
1931 DRM_INFO("gtt_space %p\n", obj_priv->gtt_space);
1932#endif
1933 if (obj_priv->gtt_space == NULL)
1934 return 0;
1935
1936 if (obj_priv->pin_count != 0) {
1937 DRM_ERROR("Attempting to unbind pinned buffer\n");
1938 return -EINVAL;
1939 }
1940
Eric Anholt5323fd02009-09-09 11:50:45 -07001941 /* blow away mappings if mapped through GTT */
1942 i915_gem_release_mmap(obj);
1943
Eric Anholt673a3942008-07-30 12:06:12 -07001944 /* Move the object to the CPU domain to ensure that
1945 * any possible CPU writes while it's not in the GTT
1946 * are flushed when we go to remap it. This will
1947 * also ensure that all pending GPU writes are finished
1948 * before we unbind.
1949 */
Eric Anholte47c68e2008-11-14 13:35:19 -08001950 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Chris Wilson8dc17752010-07-23 23:18:51 +01001951 if (ret == -ERESTARTSYS)
Eric Anholt673a3942008-07-30 12:06:12 -07001952 return ret;
Chris Wilson8dc17752010-07-23 23:18:51 +01001953 /* Continue on if we fail due to EIO, the GPU is hung so we
1954 * should be safe and we need to cleanup or else we might
1955 * cause memory corruption through use-after-free.
1956 */
Eric Anholt673a3942008-07-30 12:06:12 -07001957
Eric Anholt5323fd02009-09-09 11:50:45 -07001958 BUG_ON(obj_priv->active);
1959
Daniel Vetter96b47b62009-12-15 17:50:00 +01001960 /* release the fence reg _after_ flushing */
1961 if (obj_priv->fence_reg != I915_FENCE_REG_NONE)
1962 i915_gem_clear_fence_reg(obj);
1963
Eric Anholt673a3942008-07-30 12:06:12 -07001964 if (obj_priv->agp_mem != NULL) {
1965 drm_unbind_agp(obj_priv->agp_mem);
1966 drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE);
1967 obj_priv->agp_mem = NULL;
1968 }
1969
Eric Anholt856fa192009-03-19 14:10:50 -07001970 i915_gem_object_put_pages(obj);
Chris Wilsona32808c2009-09-20 21:29:47 +01001971 BUG_ON(obj_priv->pages_refcount);
Eric Anholt673a3942008-07-30 12:06:12 -07001972
1973 if (obj_priv->gtt_space) {
1974 atomic_dec(&dev->gtt_count);
1975 atomic_sub(obj->size, &dev->gtt_memory);
1976
1977 drm_mm_put_block(obj_priv->gtt_space);
1978 obj_priv->gtt_space = NULL;
1979 }
1980
1981 /* Remove ourselves from the LRU list if present. */
Daniel Vetter4a87b8c2010-02-19 11:51:57 +01001982 spin_lock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001983 if (!list_empty(&obj_priv->list))
1984 list_del_init(&obj_priv->list);
Daniel Vetter4a87b8c2010-02-19 11:51:57 +01001985 spin_unlock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001986
Chris Wilson963b4832009-09-20 23:03:54 +01001987 if (i915_gem_object_is_purgeable(obj_priv))
1988 i915_gem_object_truncate(obj);
1989
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001990 trace_i915_gem_object_unbind(obj);
1991
Chris Wilson8dc17752010-07-23 23:18:51 +01001992 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001993}
1994
Chris Wilsonb47eb4a2010-08-07 11:01:23 +01001995int
Daniel Vetter4df2faf2010-02-19 11:52:00 +01001996i915_gpu_idle(struct drm_device *dev)
1997{
1998 drm_i915_private_t *dev_priv = dev->dev_private;
1999 bool lists_empty;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002000 uint32_t seqno1, seqno2;
Zou Nan hai852835f2010-05-21 09:08:56 +08002001 int ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002002
2003 spin_lock(&dev_priv->mm.active_list_lock);
Zou Nan haid1b851f2010-05-21 09:08:57 +08002004 lists_empty = (list_empty(&dev_priv->mm.flushing_list) &&
2005 list_empty(&dev_priv->render_ring.active_list) &&
2006 (!HAS_BSD(dev) ||
2007 list_empty(&dev_priv->bsd_ring.active_list)));
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002008 spin_unlock(&dev_priv->mm.active_list_lock);
2009
2010 if (lists_empty)
2011 return 0;
2012
2013 /* Flush everything onto the inactive list. */
2014 i915_gem_flush(dev, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
Zou Nan haid1b851f2010-05-21 09:08:57 +08002015 seqno1 = i915_add_request(dev, NULL, I915_GEM_GPU_DOMAINS,
Zou Nan hai852835f2010-05-21 09:08:56 +08002016 &dev_priv->render_ring);
Zou Nan haid1b851f2010-05-21 09:08:57 +08002017 if (seqno1 == 0)
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002018 return -ENOMEM;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002019 ret = i915_wait_request(dev, seqno1, &dev_priv->render_ring);
2020
2021 if (HAS_BSD(dev)) {
2022 seqno2 = i915_add_request(dev, NULL, I915_GEM_GPU_DOMAINS,
2023 &dev_priv->bsd_ring);
2024 if (seqno2 == 0)
2025 return -ENOMEM;
2026
2027 ret = i915_wait_request(dev, seqno2, &dev_priv->bsd_ring);
2028 if (ret)
2029 return ret;
2030 }
2031
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002032
Zou Nan hai852835f2010-05-21 09:08:56 +08002033 return ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002034}
2035
Ben Gamari6911a9b2009-04-02 11:24:54 -07002036int
Chris Wilson4bdadb92010-01-27 13:36:32 +00002037i915_gem_object_get_pages(struct drm_gem_object *obj,
2038 gfp_t gfpmask)
Eric Anholt673a3942008-07-30 12:06:12 -07002039{
Daniel Vetter23010e42010-03-08 13:35:02 +01002040 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002041 int page_count, i;
2042 struct address_space *mapping;
2043 struct inode *inode;
2044 struct page *page;
Eric Anholt673a3942008-07-30 12:06:12 -07002045
Daniel Vetter778c3542010-05-13 11:49:44 +02002046 BUG_ON(obj_priv->pages_refcount
2047 == DRM_I915_GEM_OBJECT_MAX_PAGES_REFCOUNT);
2048
Eric Anholt856fa192009-03-19 14:10:50 -07002049 if (obj_priv->pages_refcount++ != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07002050 return 0;
2051
2052 /* Get the list of pages out of our struct file. They'll be pinned
2053 * at this point until we release them.
2054 */
2055 page_count = obj->size / PAGE_SIZE;
Eric Anholt856fa192009-03-19 14:10:50 -07002056 BUG_ON(obj_priv->pages != NULL);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07002057 obj_priv->pages = drm_calloc_large(page_count, sizeof(struct page *));
Eric Anholt856fa192009-03-19 14:10:50 -07002058 if (obj_priv->pages == NULL) {
Eric Anholt856fa192009-03-19 14:10:50 -07002059 obj_priv->pages_refcount--;
Eric Anholt673a3942008-07-30 12:06:12 -07002060 return -ENOMEM;
2061 }
2062
2063 inode = obj->filp->f_path.dentry->d_inode;
2064 mapping = inode->i_mapping;
2065 for (i = 0; i < page_count; i++) {
Chris Wilson4bdadb92010-01-27 13:36:32 +00002066 page = read_cache_page_gfp(mapping, i,
Linus Torvalds985b8232010-07-02 10:04:42 +10002067 GFP_HIGHUSER |
Chris Wilson4bdadb92010-01-27 13:36:32 +00002068 __GFP_COLD |
Linus Torvaldscd9f0402010-07-18 09:44:37 -07002069 __GFP_RECLAIMABLE |
Chris Wilson4bdadb92010-01-27 13:36:32 +00002070 gfpmask);
Chris Wilson1f2b1012010-03-12 19:52:55 +00002071 if (IS_ERR(page))
2072 goto err_pages;
2073
Eric Anholt856fa192009-03-19 14:10:50 -07002074 obj_priv->pages[i] = page;
Eric Anholt673a3942008-07-30 12:06:12 -07002075 }
Eric Anholt280b7132009-03-12 16:56:27 -07002076
2077 if (obj_priv->tiling_mode != I915_TILING_NONE)
2078 i915_gem_object_do_bit_17_swizzle(obj);
2079
Eric Anholt673a3942008-07-30 12:06:12 -07002080 return 0;
Chris Wilson1f2b1012010-03-12 19:52:55 +00002081
2082err_pages:
2083 while (i--)
2084 page_cache_release(obj_priv->pages[i]);
2085
2086 drm_free_large(obj_priv->pages);
2087 obj_priv->pages = NULL;
2088 obj_priv->pages_refcount--;
2089 return PTR_ERR(page);
Eric Anholt673a3942008-07-30 12:06:12 -07002090}
2091
Eric Anholt4e901fd2009-10-26 16:44:17 -07002092static void sandybridge_write_fence_reg(struct drm_i915_fence_reg *reg)
2093{
2094 struct drm_gem_object *obj = reg->obj;
2095 struct drm_device *dev = obj->dev;
2096 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002097 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt4e901fd2009-10-26 16:44:17 -07002098 int regnum = obj_priv->fence_reg;
2099 uint64_t val;
2100
2101 val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
2102 0xfffff000) << 32;
2103 val |= obj_priv->gtt_offset & 0xfffff000;
2104 val |= (uint64_t)((obj_priv->stride / 128) - 1) <<
2105 SANDYBRIDGE_FENCE_PITCH_SHIFT;
2106
2107 if (obj_priv->tiling_mode == I915_TILING_Y)
2108 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2109 val |= I965_FENCE_REG_VALID;
2110
2111 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (regnum * 8), val);
2112}
2113
Jesse Barnesde151cf2008-11-12 10:03:55 -08002114static void i965_write_fence_reg(struct drm_i915_fence_reg *reg)
2115{
2116 struct drm_gem_object *obj = reg->obj;
2117 struct drm_device *dev = obj->dev;
2118 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002119 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002120 int regnum = obj_priv->fence_reg;
2121 uint64_t val;
2122
2123 val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
2124 0xfffff000) << 32;
2125 val |= obj_priv->gtt_offset & 0xfffff000;
2126 val |= ((obj_priv->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
2127 if (obj_priv->tiling_mode == I915_TILING_Y)
2128 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2129 val |= I965_FENCE_REG_VALID;
2130
2131 I915_WRITE64(FENCE_REG_965_0 + (regnum * 8), val);
2132}
2133
2134static void i915_write_fence_reg(struct drm_i915_fence_reg *reg)
2135{
2136 struct drm_gem_object *obj = reg->obj;
2137 struct drm_device *dev = obj->dev;
2138 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002139 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002140 int regnum = obj_priv->fence_reg;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002141 int tile_width;
Eric Anholtdc529a42009-03-10 22:34:49 -07002142 uint32_t fence_reg, val;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002143 uint32_t pitch_val;
2144
2145 if ((obj_priv->gtt_offset & ~I915_FENCE_START_MASK) ||
2146 (obj_priv->gtt_offset & (obj->size - 1))) {
Linus Torvaldsf06da262009-02-09 08:57:29 -08002147 WARN(1, "%s: object 0x%08x not 1M or size (0x%zx) aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002148 __func__, obj_priv->gtt_offset, obj->size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002149 return;
2150 }
2151
Jesse Barnes0f973f22009-01-26 17:10:45 -08002152 if (obj_priv->tiling_mode == I915_TILING_Y &&
2153 HAS_128_BYTE_Y_TILING(dev))
2154 tile_width = 128;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002155 else
Jesse Barnes0f973f22009-01-26 17:10:45 -08002156 tile_width = 512;
2157
2158 /* Note: pitch better be a power of two tile widths */
2159 pitch_val = obj_priv->stride / tile_width;
2160 pitch_val = ffs(pitch_val) - 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002161
Daniel Vetterc36a2a62010-04-17 15:12:03 +02002162 if (obj_priv->tiling_mode == I915_TILING_Y &&
2163 HAS_128_BYTE_Y_TILING(dev))
2164 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2165 else
2166 WARN_ON(pitch_val > I915_FENCE_MAX_PITCH_VAL);
2167
Jesse Barnesde151cf2008-11-12 10:03:55 -08002168 val = obj_priv->gtt_offset;
2169 if (obj_priv->tiling_mode == I915_TILING_Y)
2170 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2171 val |= I915_FENCE_SIZE_BITS(obj->size);
2172 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2173 val |= I830_FENCE_REG_VALID;
2174
Eric Anholtdc529a42009-03-10 22:34:49 -07002175 if (regnum < 8)
2176 fence_reg = FENCE_REG_830_0 + (regnum * 4);
2177 else
2178 fence_reg = FENCE_REG_945_8 + ((regnum - 8) * 4);
2179 I915_WRITE(fence_reg, val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002180}
2181
2182static void i830_write_fence_reg(struct drm_i915_fence_reg *reg)
2183{
2184 struct drm_gem_object *obj = reg->obj;
2185 struct drm_device *dev = obj->dev;
2186 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002187 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002188 int regnum = obj_priv->fence_reg;
2189 uint32_t val;
2190 uint32_t pitch_val;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002191 uint32_t fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002192
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002193 if ((obj_priv->gtt_offset & ~I830_FENCE_START_MASK) ||
Jesse Barnesde151cf2008-11-12 10:03:55 -08002194 (obj_priv->gtt_offset & (obj->size - 1))) {
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002195 WARN(1, "%s: object 0x%08x not 512K or size aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002196 __func__, obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002197 return;
2198 }
2199
Eric Anholte76a16d2009-05-26 17:44:56 -07002200 pitch_val = obj_priv->stride / 128;
2201 pitch_val = ffs(pitch_val) - 1;
2202 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2203
Jesse Barnesde151cf2008-11-12 10:03:55 -08002204 val = obj_priv->gtt_offset;
2205 if (obj_priv->tiling_mode == I915_TILING_Y)
2206 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002207 fence_size_bits = I830_FENCE_SIZE_BITS(obj->size);
2208 WARN_ON(fence_size_bits & ~0x00000f00);
2209 val |= fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002210 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2211 val |= I830_FENCE_REG_VALID;
2212
2213 I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002214}
2215
Daniel Vetterae3db242010-02-19 11:51:58 +01002216static int i915_find_fence_reg(struct drm_device *dev)
2217{
2218 struct drm_i915_fence_reg *reg = NULL;
2219 struct drm_i915_gem_object *obj_priv = NULL;
2220 struct drm_i915_private *dev_priv = dev->dev_private;
2221 struct drm_gem_object *obj = NULL;
2222 int i, avail, ret;
2223
2224 /* First try to find a free reg */
2225 avail = 0;
2226 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2227 reg = &dev_priv->fence_regs[i];
2228 if (!reg->obj)
2229 return i;
2230
Daniel Vetter23010e42010-03-08 13:35:02 +01002231 obj_priv = to_intel_bo(reg->obj);
Daniel Vetterae3db242010-02-19 11:51:58 +01002232 if (!obj_priv->pin_count)
2233 avail++;
2234 }
2235
2236 if (avail == 0)
2237 return -ENOSPC;
2238
2239 /* None available, try to steal one or wait for a user to finish */
2240 i = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002241 list_for_each_entry(reg, &dev_priv->mm.fence_list,
2242 lru_list) {
2243 obj = reg->obj;
2244 obj_priv = to_intel_bo(obj);
Daniel Vetterae3db242010-02-19 11:51:58 +01002245
2246 if (obj_priv->pin_count)
2247 continue;
2248
2249 /* found one! */
2250 i = obj_priv->fence_reg;
2251 break;
2252 }
2253
2254 BUG_ON(i == I915_FENCE_REG_NONE);
2255
2256 /* We only have a reference on obj from the active list. put_fence_reg
2257 * might drop that one, causing a use-after-free in it. So hold a
2258 * private reference to obj like the other callers of put_fence_reg
2259 * (set_tiling ioctl) do. */
2260 drm_gem_object_reference(obj);
2261 ret = i915_gem_object_put_fence_reg(obj);
2262 drm_gem_object_unreference(obj);
2263 if (ret != 0)
2264 return ret;
2265
2266 return i;
2267}
2268
Jesse Barnesde151cf2008-11-12 10:03:55 -08002269/**
2270 * i915_gem_object_get_fence_reg - set up a fence reg for an object
2271 * @obj: object to map through a fence reg
2272 *
2273 * When mapping objects through the GTT, userspace wants to be able to write
2274 * to them without having to worry about swizzling if the object is tiled.
2275 *
2276 * This function walks the fence regs looking for a free one for @obj,
2277 * stealing one if it can't find any.
2278 *
2279 * It then sets up the reg based on the object's properties: address, pitch
2280 * and tiling format.
2281 */
Chris Wilson8c4b8c32009-06-17 22:08:52 +01002282int
2283i915_gem_object_get_fence_reg(struct drm_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002284{
2285 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002286 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002287 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002288 struct drm_i915_fence_reg *reg = NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002289 int ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002290
Eric Anholta09ba7f2009-08-29 12:49:51 -07002291 /* Just update our place in the LRU if our fence is getting used. */
2292 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002293 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
2294 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002295 return 0;
2296 }
2297
Jesse Barnesde151cf2008-11-12 10:03:55 -08002298 switch (obj_priv->tiling_mode) {
2299 case I915_TILING_NONE:
2300 WARN(1, "allocating a fence for non-tiled object?\n");
2301 break;
2302 case I915_TILING_X:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002303 if (!obj_priv->stride)
2304 return -EINVAL;
2305 WARN((obj_priv->stride & (512 - 1)),
2306 "object 0x%08x is X tiled but has non-512B pitch\n",
2307 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002308 break;
2309 case I915_TILING_Y:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002310 if (!obj_priv->stride)
2311 return -EINVAL;
2312 WARN((obj_priv->stride & (128 - 1)),
2313 "object 0x%08x is Y tiled but has non-128B pitch\n",
2314 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002315 break;
2316 }
2317
Daniel Vetterae3db242010-02-19 11:51:58 +01002318 ret = i915_find_fence_reg(dev);
2319 if (ret < 0)
2320 return ret;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002321
Daniel Vetterae3db242010-02-19 11:51:58 +01002322 obj_priv->fence_reg = ret;
2323 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002324 list_add_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002325
Jesse Barnesde151cf2008-11-12 10:03:55 -08002326 reg->obj = obj;
2327
Eric Anholt4e901fd2009-10-26 16:44:17 -07002328 if (IS_GEN6(dev))
2329 sandybridge_write_fence_reg(reg);
2330 else if (IS_I965G(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08002331 i965_write_fence_reg(reg);
2332 else if (IS_I9XX(dev))
2333 i915_write_fence_reg(reg);
2334 else
2335 i830_write_fence_reg(reg);
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002336
Daniel Vetterae3db242010-02-19 11:51:58 +01002337 trace_i915_gem_object_get_fence(obj, obj_priv->fence_reg,
2338 obj_priv->tiling_mode);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002339
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002340 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002341}
2342
2343/**
2344 * i915_gem_clear_fence_reg - clear out fence register info
2345 * @obj: object to clear
2346 *
2347 * Zeroes out the fence register itself and clears out the associated
2348 * data structures in dev_priv and obj_priv.
2349 */
2350static void
2351i915_gem_clear_fence_reg(struct drm_gem_object *obj)
2352{
2353 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002354 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002355 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002356 struct drm_i915_fence_reg *reg =
2357 &dev_priv->fence_regs[obj_priv->fence_reg];
Jesse Barnesde151cf2008-11-12 10:03:55 -08002358
Eric Anholt4e901fd2009-10-26 16:44:17 -07002359 if (IS_GEN6(dev)) {
2360 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 +
2361 (obj_priv->fence_reg * 8), 0);
2362 } else if (IS_I965G(dev)) {
Jesse Barnesde151cf2008-11-12 10:03:55 -08002363 I915_WRITE64(FENCE_REG_965_0 + (obj_priv->fence_reg * 8), 0);
Eric Anholt4e901fd2009-10-26 16:44:17 -07002364 } else {
Eric Anholtdc529a42009-03-10 22:34:49 -07002365 uint32_t fence_reg;
2366
2367 if (obj_priv->fence_reg < 8)
2368 fence_reg = FENCE_REG_830_0 + obj_priv->fence_reg * 4;
2369 else
2370 fence_reg = FENCE_REG_945_8 + (obj_priv->fence_reg -
2371 8) * 4;
2372
2373 I915_WRITE(fence_reg, 0);
2374 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002375
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002376 reg->obj = NULL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002377 obj_priv->fence_reg = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002378 list_del_init(&reg->lru_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002379}
2380
Eric Anholt673a3942008-07-30 12:06:12 -07002381/**
Chris Wilson52dc7d32009-06-06 09:46:01 +01002382 * i915_gem_object_put_fence_reg - waits on outstanding fenced access
2383 * to the buffer to finish, and then resets the fence register.
2384 * @obj: tiled object holding a fence register.
2385 *
2386 * Zeroes out the fence register itself and clears out the associated
2387 * data structures in dev_priv and obj_priv.
2388 */
2389int
2390i915_gem_object_put_fence_reg(struct drm_gem_object *obj)
2391{
2392 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01002393 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson52dc7d32009-06-06 09:46:01 +01002394
2395 if (obj_priv->fence_reg == I915_FENCE_REG_NONE)
2396 return 0;
2397
Daniel Vetter10ae9bd2010-02-01 13:59:17 +01002398 /* If we've changed tiling, GTT-mappings of the object
2399 * need to re-fault to ensure that the correct fence register
2400 * setup is in place.
2401 */
2402 i915_gem_release_mmap(obj);
2403
Chris Wilson52dc7d32009-06-06 09:46:01 +01002404 /* On the i915, GPU access to tiled buffers is via a fence,
2405 * therefore we must wait for any outstanding access to complete
2406 * before clearing the fence.
2407 */
2408 if (!IS_I965G(dev)) {
2409 int ret;
2410
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002411 ret = i915_gem_object_flush_gpu_write_domain(obj);
2412 if (ret != 0)
2413 return ret;
2414
Chris Wilson52dc7d32009-06-06 09:46:01 +01002415 ret = i915_gem_object_wait_rendering(obj);
2416 if (ret != 0)
2417 return ret;
2418 }
2419
Daniel Vetter4a726612010-02-01 13:59:16 +01002420 i915_gem_object_flush_gtt_write_domain(obj);
Chris Wilson52dc7d32009-06-06 09:46:01 +01002421 i915_gem_clear_fence_reg (obj);
2422
2423 return 0;
2424}
2425
2426/**
Eric Anholt673a3942008-07-30 12:06:12 -07002427 * Finds free space in the GTT aperture and binds the object there.
2428 */
2429static int
2430i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment)
2431{
2432 struct drm_device *dev = obj->dev;
2433 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002434 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002435 struct drm_mm_node *free_space;
Chris Wilson4bdadb92010-01-27 13:36:32 +00002436 gfp_t gfpmask = __GFP_NORETRY | __GFP_NOWARN;
Chris Wilson07f73f62009-09-14 16:50:30 +01002437 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002438
Chris Wilsonbb6baf72009-09-22 14:24:13 +01002439 if (obj_priv->madv != I915_MADV_WILLNEED) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01002440 DRM_ERROR("Attempting to bind a purgeable object\n");
2441 return -EINVAL;
2442 }
2443
Eric Anholt673a3942008-07-30 12:06:12 -07002444 if (alignment == 0)
Jesse Barnes0f973f22009-01-26 17:10:45 -08002445 alignment = i915_gem_get_gtt_alignment(obj);
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002446 if (alignment & (i915_gem_get_gtt_alignment(obj) - 1)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002447 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2448 return -EINVAL;
2449 }
2450
Chris Wilson654fc602010-05-27 13:18:21 +01002451 /* If the object is bigger than the entire aperture, reject it early
2452 * before evicting everything in a vain attempt to find space.
2453 */
2454 if (obj->size > dev->gtt_total) {
2455 DRM_ERROR("Attempting to bind an object larger than the aperture\n");
2456 return -E2BIG;
2457 }
2458
Eric Anholt673a3942008-07-30 12:06:12 -07002459 search_free:
2460 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
2461 obj->size, alignment, 0);
2462 if (free_space != NULL) {
2463 obj_priv->gtt_space = drm_mm_get_block(free_space, obj->size,
2464 alignment);
Daniel Vetterdb3307a2010-07-02 15:02:12 +01002465 if (obj_priv->gtt_space != NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002466 obj_priv->gtt_offset = obj_priv->gtt_space->start;
Eric Anholt673a3942008-07-30 12:06:12 -07002467 }
2468 if (obj_priv->gtt_space == NULL) {
2469 /* If the gtt is empty and we're still having trouble
2470 * fitting our object in, we're out of memory.
2471 */
2472#if WATCH_LRU
2473 DRM_INFO("%s: GTT full, evicting something\n", __func__);
2474#endif
Daniel Vetter0108a3e2010-08-07 11:01:21 +01002475 ret = i915_gem_evict_something(dev, obj->size, alignment);
Chris Wilson97311292009-09-21 00:22:34 +01002476 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002477 return ret;
Chris Wilson97311292009-09-21 00:22:34 +01002478
Eric Anholt673a3942008-07-30 12:06:12 -07002479 goto search_free;
2480 }
2481
2482#if WATCH_BUF
Krzysztof Halasacfd43c02009-06-20 00:31:28 +02002483 DRM_INFO("Binding object of size %zd at 0x%08x\n",
Eric Anholt673a3942008-07-30 12:06:12 -07002484 obj->size, obj_priv->gtt_offset);
2485#endif
Chris Wilson4bdadb92010-01-27 13:36:32 +00002486 ret = i915_gem_object_get_pages(obj, gfpmask);
Eric Anholt673a3942008-07-30 12:06:12 -07002487 if (ret) {
2488 drm_mm_put_block(obj_priv->gtt_space);
2489 obj_priv->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002490
2491 if (ret == -ENOMEM) {
2492 /* first try to clear up some space from the GTT */
Daniel Vetter0108a3e2010-08-07 11:01:21 +01002493 ret = i915_gem_evict_something(dev, obj->size,
2494 alignment);
Chris Wilson07f73f62009-09-14 16:50:30 +01002495 if (ret) {
Chris Wilson07f73f62009-09-14 16:50:30 +01002496 /* now try to shrink everyone else */
Chris Wilson4bdadb92010-01-27 13:36:32 +00002497 if (gfpmask) {
2498 gfpmask = 0;
2499 goto search_free;
Chris Wilson07f73f62009-09-14 16:50:30 +01002500 }
2501
2502 return ret;
2503 }
2504
2505 goto search_free;
2506 }
2507
Eric Anholt673a3942008-07-30 12:06:12 -07002508 return ret;
2509 }
2510
Eric Anholt673a3942008-07-30 12:06:12 -07002511 /* Create an AGP memory structure pointing at our pages, and bind it
2512 * into the GTT.
2513 */
2514 obj_priv->agp_mem = drm_agp_bind_pages(dev,
Eric Anholt856fa192009-03-19 14:10:50 -07002515 obj_priv->pages,
Chris Wilson07f73f62009-09-14 16:50:30 +01002516 obj->size >> PAGE_SHIFT,
Keith Packardba1eb1d2008-10-14 19:55:10 -07002517 obj_priv->gtt_offset,
2518 obj_priv->agp_type);
Eric Anholt673a3942008-07-30 12:06:12 -07002519 if (obj_priv->agp_mem == NULL) {
Eric Anholt856fa192009-03-19 14:10:50 -07002520 i915_gem_object_put_pages(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002521 drm_mm_put_block(obj_priv->gtt_space);
2522 obj_priv->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002523
Daniel Vetter0108a3e2010-08-07 11:01:21 +01002524 ret = i915_gem_evict_something(dev, obj->size, alignment);
Chris Wilson97311292009-09-21 00:22:34 +01002525 if (ret)
Chris Wilson07f73f62009-09-14 16:50:30 +01002526 return ret;
Chris Wilson07f73f62009-09-14 16:50:30 +01002527
2528 goto search_free;
Eric Anholt673a3942008-07-30 12:06:12 -07002529 }
2530 atomic_inc(&dev->gtt_count);
2531 atomic_add(obj->size, &dev->gtt_memory);
2532
Chris Wilsonbf1a1092010-08-07 11:01:20 +01002533 /* keep track of bounds object by adding it to the inactive list */
2534 list_add_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
2535
Eric Anholt673a3942008-07-30 12:06:12 -07002536 /* Assert that the object is not currently in any GPU domain. As it
2537 * wasn't in the GTT, there shouldn't be any way it could have been in
2538 * a GPU cache
2539 */
Chris Wilson21d509e2009-06-06 09:46:02 +01002540 BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS);
2541 BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS);
Eric Anholt673a3942008-07-30 12:06:12 -07002542
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002543 trace_i915_gem_object_bind(obj, obj_priv->gtt_offset);
2544
Eric Anholt673a3942008-07-30 12:06:12 -07002545 return 0;
2546}
2547
2548void
2549i915_gem_clflush_object(struct drm_gem_object *obj)
2550{
Daniel Vetter23010e42010-03-08 13:35:02 +01002551 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002552
2553 /* If we don't have a page list set up, then we're not pinned
2554 * to GPU, and we can ignore the cache flush because it'll happen
2555 * again at bind time.
2556 */
Eric Anholt856fa192009-03-19 14:10:50 -07002557 if (obj_priv->pages == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002558 return;
2559
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002560 trace_i915_gem_object_clflush(obj);
Eric Anholtcfa16a02009-05-26 18:46:16 -07002561
Eric Anholt856fa192009-03-19 14:10:50 -07002562 drm_clflush_pages(obj_priv->pages, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002563}
2564
Eric Anholte47c68e2008-11-14 13:35:19 -08002565/** Flushes any GPU write domain for the object if it's dirty. */
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002566static int
Eric Anholte47c68e2008-11-14 13:35:19 -08002567i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj)
2568{
2569 struct drm_device *dev = obj->dev;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002570 uint32_t old_write_domain;
Zou Nan hai852835f2010-05-21 09:08:56 +08002571 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08002572
2573 if ((obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002574 return 0;
Eric Anholte47c68e2008-11-14 13:35:19 -08002575
2576 /* Queue the GPU write cache flushing we need. */
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002577 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002578 i915_gem_flush(dev, 0, obj->write_domain);
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002579 if (i915_add_request(dev, NULL, obj->write_domain, obj_priv->ring) == 0)
2580 return -ENOMEM;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002581
2582 trace_i915_gem_object_change_domain(obj,
2583 obj->read_domains,
2584 old_write_domain);
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002585 return 0;
Eric Anholte47c68e2008-11-14 13:35:19 -08002586}
2587
2588/** Flushes the GTT write domain for the object if it's dirty. */
2589static void
2590i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj)
2591{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002592 uint32_t old_write_domain;
2593
Eric Anholte47c68e2008-11-14 13:35:19 -08002594 if (obj->write_domain != I915_GEM_DOMAIN_GTT)
2595 return;
2596
2597 /* No actual flushing is required for the GTT write domain. Writes
2598 * to it immediately go to main memory as far as we know, so there's
2599 * no chipset flush. It also doesn't land in render cache.
2600 */
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002601 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002602 obj->write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002603
2604 trace_i915_gem_object_change_domain(obj,
2605 obj->read_domains,
2606 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002607}
2608
2609/** Flushes the CPU write domain for the object if it's dirty. */
2610static void
2611i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj)
2612{
2613 struct drm_device *dev = obj->dev;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002614 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002615
2616 if (obj->write_domain != I915_GEM_DOMAIN_CPU)
2617 return;
2618
2619 i915_gem_clflush_object(obj);
2620 drm_agp_chipset_flush(dev);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002621 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002622 obj->write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002623
2624 trace_i915_gem_object_change_domain(obj,
2625 obj->read_domains,
2626 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002627}
2628
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002629int
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002630i915_gem_object_flush_write_domain(struct drm_gem_object *obj)
2631{
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002632 int ret = 0;
2633
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002634 switch (obj->write_domain) {
2635 case I915_GEM_DOMAIN_GTT:
2636 i915_gem_object_flush_gtt_write_domain(obj);
2637 break;
2638 case I915_GEM_DOMAIN_CPU:
2639 i915_gem_object_flush_cpu_write_domain(obj);
2640 break;
2641 default:
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002642 ret = i915_gem_object_flush_gpu_write_domain(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002643 break;
2644 }
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002645
2646 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002647}
2648
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002649/**
2650 * Moves a single object to the GTT read, and possibly write domain.
2651 *
2652 * This function returns when the move is complete, including waiting on
2653 * flushes to occur.
2654 */
Jesse Barnes79e53942008-11-07 14:24:08 -08002655int
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002656i915_gem_object_set_to_gtt_domain(struct drm_gem_object *obj, int write)
2657{
Daniel Vetter23010e42010-03-08 13:35:02 +01002658 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002659 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002660 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002661
Eric Anholt02354392008-11-26 13:58:13 -08002662 /* Not valid to be called on unbound objects. */
2663 if (obj_priv->gtt_space == NULL)
2664 return -EINVAL;
2665
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002666 ret = i915_gem_object_flush_gpu_write_domain(obj);
2667 if (ret != 0)
2668 return ret;
2669
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002670 /* Wait on any GPU rendering and flushing to occur. */
Eric Anholte47c68e2008-11-14 13:35:19 -08002671 ret = i915_gem_object_wait_rendering(obj);
2672 if (ret != 0)
2673 return ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002674
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002675 old_write_domain = obj->write_domain;
2676 old_read_domains = obj->read_domains;
2677
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002678 /* If we're writing through the GTT domain, then CPU and GPU caches
2679 * will need to be invalidated at next use.
2680 */
2681 if (write)
Eric Anholte47c68e2008-11-14 13:35:19 -08002682 obj->read_domains &= I915_GEM_DOMAIN_GTT;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002683
Eric Anholte47c68e2008-11-14 13:35:19 -08002684 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002685
2686 /* It should now be out of any other write domains, and we can update
2687 * the domain values for our changes.
2688 */
2689 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2690 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002691 if (write) {
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002692 obj->write_domain = I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002693 obj_priv->dirty = 1;
2694 }
2695
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002696 trace_i915_gem_object_change_domain(obj,
2697 old_read_domains,
2698 old_write_domain);
2699
Eric Anholte47c68e2008-11-14 13:35:19 -08002700 return 0;
2701}
2702
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002703/*
2704 * Prepare buffer for display plane. Use uninterruptible for possible flush
2705 * wait, as in modesetting process we're not supposed to be interrupted.
2706 */
2707int
2708i915_gem_object_set_to_display_plane(struct drm_gem_object *obj)
2709{
2710 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01002711 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002712 uint32_t old_write_domain, old_read_domains;
2713 int ret;
2714
2715 /* Not valid to be called on unbound objects. */
2716 if (obj_priv->gtt_space == NULL)
2717 return -EINVAL;
2718
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002719 ret = i915_gem_object_flush_gpu_write_domain(obj);
2720 if (ret)
2721 return ret;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002722
2723 /* Wait on any GPU rendering and flushing to occur. */
2724 if (obj_priv->active) {
2725#if WATCH_BUF
2726 DRM_INFO("%s: object %p wait for seqno %08x\n",
2727 __func__, obj, obj_priv->last_rendering_seqno);
2728#endif
Zou Nan hai852835f2010-05-21 09:08:56 +08002729 ret = i915_do_wait_request(dev,
2730 obj_priv->last_rendering_seqno,
2731 0,
2732 obj_priv->ring);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002733 if (ret != 0)
2734 return ret;
2735 }
2736
Chris Wilsonb118c1e2010-05-27 13:18:14 +01002737 i915_gem_object_flush_cpu_write_domain(obj);
2738
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002739 old_write_domain = obj->write_domain;
2740 old_read_domains = obj->read_domains;
2741
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002742 /* It should now be out of any other write domains, and we can update
2743 * the domain values for our changes.
2744 */
2745 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
Chris Wilsonb118c1e2010-05-27 13:18:14 +01002746 obj->read_domains = I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002747 obj->write_domain = I915_GEM_DOMAIN_GTT;
2748 obj_priv->dirty = 1;
2749
2750 trace_i915_gem_object_change_domain(obj,
2751 old_read_domains,
2752 old_write_domain);
2753
2754 return 0;
2755}
2756
Eric Anholte47c68e2008-11-14 13:35:19 -08002757/**
2758 * Moves a single object to the CPU read, and possibly write domain.
2759 *
2760 * This function returns when the move is complete, including waiting on
2761 * flushes to occur.
2762 */
2763static int
2764i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj, int write)
2765{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002766 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002767 int ret;
2768
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002769 ret = i915_gem_object_flush_gpu_write_domain(obj);
2770 if (ret)
2771 return ret;
2772
Eric Anholte47c68e2008-11-14 13:35:19 -08002773 /* Wait on any GPU rendering and flushing to occur. */
2774 ret = i915_gem_object_wait_rendering(obj);
2775 if (ret != 0)
2776 return ret;
2777
2778 i915_gem_object_flush_gtt_write_domain(obj);
2779
2780 /* If we have a partially-valid cache of the object in the CPU,
2781 * finish invalidating it and free the per-page flags.
2782 */
2783 i915_gem_object_set_to_full_cpu_read_domain(obj);
2784
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002785 old_write_domain = obj->write_domain;
2786 old_read_domains = obj->read_domains;
2787
Eric Anholte47c68e2008-11-14 13:35:19 -08002788 /* Flush the CPU cache if it's still invalid. */
2789 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
2790 i915_gem_clflush_object(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08002791
2792 obj->read_domains |= I915_GEM_DOMAIN_CPU;
2793 }
2794
2795 /* It should now be out of any other write domains, and we can update
2796 * the domain values for our changes.
2797 */
2798 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
2799
2800 /* If we're writing through the CPU, then the GPU read domains will
2801 * need to be invalidated at next use.
2802 */
2803 if (write) {
2804 obj->read_domains &= I915_GEM_DOMAIN_CPU;
2805 obj->write_domain = I915_GEM_DOMAIN_CPU;
2806 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002807
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002808 trace_i915_gem_object_change_domain(obj,
2809 old_read_domains,
2810 old_write_domain);
2811
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002812 return 0;
2813}
2814
Eric Anholt673a3942008-07-30 12:06:12 -07002815/*
2816 * Set the next domain for the specified object. This
2817 * may not actually perform the necessary flushing/invaliding though,
2818 * as that may want to be batched with other set_domain operations
2819 *
2820 * This is (we hope) the only really tricky part of gem. The goal
2821 * is fairly simple -- track which caches hold bits of the object
2822 * and make sure they remain coherent. A few concrete examples may
2823 * help to explain how it works. For shorthand, we use the notation
2824 * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
2825 * a pair of read and write domain masks.
2826 *
2827 * Case 1: the batch buffer
2828 *
2829 * 1. Allocated
2830 * 2. Written by CPU
2831 * 3. Mapped to GTT
2832 * 4. Read by GPU
2833 * 5. Unmapped from GTT
2834 * 6. Freed
2835 *
2836 * Let's take these a step at a time
2837 *
2838 * 1. Allocated
2839 * Pages allocated from the kernel may still have
2840 * cache contents, so we set them to (CPU, CPU) always.
2841 * 2. Written by CPU (using pwrite)
2842 * The pwrite function calls set_domain (CPU, CPU) and
2843 * this function does nothing (as nothing changes)
2844 * 3. Mapped by GTT
2845 * This function asserts that the object is not
2846 * currently in any GPU-based read or write domains
2847 * 4. Read by GPU
2848 * i915_gem_execbuffer calls set_domain (COMMAND, 0).
2849 * As write_domain is zero, this function adds in the
2850 * current read domains (CPU+COMMAND, 0).
2851 * flush_domains is set to CPU.
2852 * invalidate_domains is set to COMMAND
2853 * clflush is run to get data out of the CPU caches
2854 * then i915_dev_set_domain calls i915_gem_flush to
2855 * emit an MI_FLUSH and drm_agp_chipset_flush
2856 * 5. Unmapped from GTT
2857 * i915_gem_object_unbind calls set_domain (CPU, CPU)
2858 * flush_domains and invalidate_domains end up both zero
2859 * so no flushing/invalidating happens
2860 * 6. Freed
2861 * yay, done
2862 *
2863 * Case 2: The shared render buffer
2864 *
2865 * 1. Allocated
2866 * 2. Mapped to GTT
2867 * 3. Read/written by GPU
2868 * 4. set_domain to (CPU,CPU)
2869 * 5. Read/written by CPU
2870 * 6. Read/written by GPU
2871 *
2872 * 1. Allocated
2873 * Same as last example, (CPU, CPU)
2874 * 2. Mapped to GTT
2875 * Nothing changes (assertions find that it is not in the GPU)
2876 * 3. Read/written by GPU
2877 * execbuffer calls set_domain (RENDER, RENDER)
2878 * flush_domains gets CPU
2879 * invalidate_domains gets GPU
2880 * clflush (obj)
2881 * MI_FLUSH and drm_agp_chipset_flush
2882 * 4. set_domain (CPU, CPU)
2883 * flush_domains gets GPU
2884 * invalidate_domains gets CPU
2885 * wait_rendering (obj) to make sure all drawing is complete.
2886 * This will include an MI_FLUSH to get the data from GPU
2887 * to memory
2888 * clflush (obj) to invalidate the CPU cache
2889 * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
2890 * 5. Read/written by CPU
2891 * cache lines are loaded and dirtied
2892 * 6. Read written by GPU
2893 * Same as last GPU access
2894 *
2895 * Case 3: The constant buffer
2896 *
2897 * 1. Allocated
2898 * 2. Written by CPU
2899 * 3. Read by GPU
2900 * 4. Updated (written) by CPU again
2901 * 5. Read by GPU
2902 *
2903 * 1. Allocated
2904 * (CPU, CPU)
2905 * 2. Written by CPU
2906 * (CPU, CPU)
2907 * 3. Read by GPU
2908 * (CPU+RENDER, 0)
2909 * flush_domains = CPU
2910 * invalidate_domains = RENDER
2911 * clflush (obj)
2912 * MI_FLUSH
2913 * drm_agp_chipset_flush
2914 * 4. Updated (written) by CPU again
2915 * (CPU, CPU)
2916 * flush_domains = 0 (no previous write domain)
2917 * invalidate_domains = 0 (no new read domains)
2918 * 5. Read by GPU
2919 * (CPU+RENDER, 0)
2920 * flush_domains = CPU
2921 * invalidate_domains = RENDER
2922 * clflush (obj)
2923 * MI_FLUSH
2924 * drm_agp_chipset_flush
2925 */
Keith Packardc0d90822008-11-20 23:11:08 -08002926static void
Eric Anholt8b0e3782009-02-19 14:40:50 -08002927i915_gem_object_set_to_gpu_domain(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002928{
2929 struct drm_device *dev = obj->dev;
Chris Wilson88f356b2010-08-04 13:55:32 +01002930 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002931 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002932 uint32_t invalidate_domains = 0;
2933 uint32_t flush_domains = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002934 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002935
Eric Anholt8b0e3782009-02-19 14:40:50 -08002936 BUG_ON(obj->pending_read_domains & I915_GEM_DOMAIN_CPU);
2937 BUG_ON(obj->pending_write_domain == I915_GEM_DOMAIN_CPU);
Eric Anholt673a3942008-07-30 12:06:12 -07002938
Jesse Barnes652c3932009-08-17 13:31:43 -07002939 intel_mark_busy(dev, obj);
2940
Eric Anholt673a3942008-07-30 12:06:12 -07002941#if WATCH_BUF
2942 DRM_INFO("%s: object %p read %08x -> %08x write %08x -> %08x\n",
2943 __func__, obj,
Eric Anholt8b0e3782009-02-19 14:40:50 -08002944 obj->read_domains, obj->pending_read_domains,
2945 obj->write_domain, obj->pending_write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07002946#endif
2947 /*
2948 * If the object isn't moving to a new write domain,
2949 * let the object stay in multiple read domains
2950 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08002951 if (obj->pending_write_domain == 0)
2952 obj->pending_read_domains |= obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07002953 else
2954 obj_priv->dirty = 1;
2955
2956 /*
2957 * Flush the current write domain if
2958 * the new read domains don't match. Invalidate
2959 * any read domains which differ from the old
2960 * write domain
2961 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08002962 if (obj->write_domain &&
2963 obj->write_domain != obj->pending_read_domains) {
Eric Anholt673a3942008-07-30 12:06:12 -07002964 flush_domains |= obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08002965 invalidate_domains |=
2966 obj->pending_read_domains & ~obj->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07002967 }
2968 /*
2969 * Invalidate any read caches which may have
2970 * stale data. That is, any new read domains.
2971 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08002972 invalidate_domains |= obj->pending_read_domains & ~obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07002973 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU) {
2974#if WATCH_BUF
2975 DRM_INFO("%s: CPU domain flush %08x invalidate %08x\n",
2976 __func__, flush_domains, invalidate_domains);
2977#endif
Eric Anholt673a3942008-07-30 12:06:12 -07002978 i915_gem_clflush_object(obj);
2979 }
2980
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002981 old_read_domains = obj->read_domains;
2982
Eric Anholtefbeed92009-02-19 14:54:51 -08002983 /* The actual obj->write_domain will be updated with
2984 * pending_write_domain after we emit the accumulated flush for all
2985 * of our domain changes in execbuffers (which clears objects'
2986 * write_domains). So if we have a current write domain that we
2987 * aren't changing, set pending_write_domain to that.
2988 */
2989 if (flush_domains == 0 && obj->pending_write_domain == 0)
2990 obj->pending_write_domain = obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08002991 obj->read_domains = obj->pending_read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07002992
Chris Wilson88f356b2010-08-04 13:55:32 +01002993 if (flush_domains & I915_GEM_GPU_DOMAINS) {
2994 if (obj_priv->ring == &dev_priv->render_ring)
2995 dev_priv->flush_rings |= FLUSH_RENDER_RING;
2996 else if (obj_priv->ring == &dev_priv->bsd_ring)
2997 dev_priv->flush_rings |= FLUSH_BSD_RING;
2998 }
2999
Eric Anholt673a3942008-07-30 12:06:12 -07003000 dev->invalidate_domains |= invalidate_domains;
3001 dev->flush_domains |= flush_domains;
3002#if WATCH_BUF
3003 DRM_INFO("%s: read %08x write %08x invalidate %08x flush %08x\n",
3004 __func__,
3005 obj->read_domains, obj->write_domain,
3006 dev->invalidate_domains, dev->flush_domains);
3007#endif
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003008
3009 trace_i915_gem_object_change_domain(obj,
3010 old_read_domains,
3011 obj->write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07003012}
3013
3014/**
Eric Anholte47c68e2008-11-14 13:35:19 -08003015 * Moves the object from a partially CPU read to a full one.
Eric Anholt673a3942008-07-30 12:06:12 -07003016 *
Eric Anholte47c68e2008-11-14 13:35:19 -08003017 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
3018 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
3019 */
3020static void
3021i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj)
3022{
Daniel Vetter23010e42010-03-08 13:35:02 +01003023 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003024
3025 if (!obj_priv->page_cpu_valid)
3026 return;
3027
3028 /* If we're partially in the CPU read domain, finish moving it in.
3029 */
3030 if (obj->read_domains & I915_GEM_DOMAIN_CPU) {
3031 int i;
3032
3033 for (i = 0; i <= (obj->size - 1) / PAGE_SIZE; i++) {
3034 if (obj_priv->page_cpu_valid[i])
3035 continue;
Eric Anholt856fa192009-03-19 14:10:50 -07003036 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholte47c68e2008-11-14 13:35:19 -08003037 }
Eric Anholte47c68e2008-11-14 13:35:19 -08003038 }
3039
3040 /* Free the page_cpu_valid mappings which are now stale, whether
3041 * or not we've got I915_GEM_DOMAIN_CPU.
3042 */
Eric Anholt9a298b22009-03-24 12:23:04 -07003043 kfree(obj_priv->page_cpu_valid);
Eric Anholte47c68e2008-11-14 13:35:19 -08003044 obj_priv->page_cpu_valid = NULL;
3045}
3046
3047/**
3048 * Set the CPU read domain on a range of the object.
3049 *
3050 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
3051 * not entirely valid. The page_cpu_valid member of the object flags which
3052 * pages have been flushed, and will be respected by
3053 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
3054 * of the whole object.
3055 *
3056 * This function returns when the move is complete, including waiting on
3057 * flushes to occur.
Eric Anholt673a3942008-07-30 12:06:12 -07003058 */
3059static int
Eric Anholte47c68e2008-11-14 13:35:19 -08003060i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
3061 uint64_t offset, uint64_t size)
Eric Anholt673a3942008-07-30 12:06:12 -07003062{
Daniel Vetter23010e42010-03-08 13:35:02 +01003063 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003064 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003065 int i, ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003066
Eric Anholte47c68e2008-11-14 13:35:19 -08003067 if (offset == 0 && size == obj->size)
3068 return i915_gem_object_set_to_cpu_domain(obj, 0);
3069
Chris Wilson2dafb1e2010-06-07 14:03:05 +01003070 ret = i915_gem_object_flush_gpu_write_domain(obj);
3071 if (ret)
3072 return ret;
3073
Eric Anholte47c68e2008-11-14 13:35:19 -08003074 /* Wait on any GPU rendering and flushing to occur. */
3075 ret = i915_gem_object_wait_rendering(obj);
3076 if (ret != 0)
3077 return ret;
3078 i915_gem_object_flush_gtt_write_domain(obj);
3079
3080 /* If we're already fully in the CPU read domain, we're done. */
3081 if (obj_priv->page_cpu_valid == NULL &&
3082 (obj->read_domains & I915_GEM_DOMAIN_CPU) != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07003083 return 0;
3084
Eric Anholte47c68e2008-11-14 13:35:19 -08003085 /* Otherwise, create/clear the per-page CPU read domain flag if we're
3086 * newly adding I915_GEM_DOMAIN_CPU
3087 */
Eric Anholt673a3942008-07-30 12:06:12 -07003088 if (obj_priv->page_cpu_valid == NULL) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003089 obj_priv->page_cpu_valid = kzalloc(obj->size / PAGE_SIZE,
3090 GFP_KERNEL);
Eric Anholte47c68e2008-11-14 13:35:19 -08003091 if (obj_priv->page_cpu_valid == NULL)
3092 return -ENOMEM;
3093 } else if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0)
3094 memset(obj_priv->page_cpu_valid, 0, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07003095
3096 /* Flush the cache on any pages that are still invalid from the CPU's
3097 * perspective.
3098 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003099 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
3100 i++) {
Eric Anholt673a3942008-07-30 12:06:12 -07003101 if (obj_priv->page_cpu_valid[i])
3102 continue;
3103
Eric Anholt856fa192009-03-19 14:10:50 -07003104 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07003105
3106 obj_priv->page_cpu_valid[i] = 1;
3107 }
3108
Eric Anholte47c68e2008-11-14 13:35:19 -08003109 /* It should now be out of any other write domains, and we can update
3110 * the domain values for our changes.
3111 */
3112 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
3113
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003114 old_read_domains = obj->read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003115 obj->read_domains |= I915_GEM_DOMAIN_CPU;
3116
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003117 trace_i915_gem_object_change_domain(obj,
3118 old_read_domains,
3119 obj->write_domain);
3120
Eric Anholt673a3942008-07-30 12:06:12 -07003121 return 0;
3122}
3123
3124/**
Eric Anholt673a3942008-07-30 12:06:12 -07003125 * Pin an object to the GTT and evaluate the relocations landing in it.
3126 */
3127static int
3128i915_gem_object_pin_and_relocate(struct drm_gem_object *obj,
3129 struct drm_file *file_priv,
Jesse Barnes76446ca2009-12-17 22:05:42 -05003130 struct drm_i915_gem_exec_object2 *entry,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003131 struct drm_i915_gem_relocation_entry *relocs)
Eric Anholt673a3942008-07-30 12:06:12 -07003132{
3133 struct drm_device *dev = obj->dev;
Keith Packard0839ccb2008-10-30 19:38:48 -07003134 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01003135 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003136 int i, ret;
Keith Packard0839ccb2008-10-30 19:38:48 -07003137 void __iomem *reloc_page;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003138 bool need_fence;
3139
3140 need_fence = entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
3141 obj_priv->tiling_mode != I915_TILING_NONE;
3142
3143 /* Check fence reg constraints and rebind if necessary */
Chris Wilson808b24d62010-05-27 13:18:15 +01003144 if (need_fence &&
3145 !i915_gem_object_fence_offset_ok(obj,
3146 obj_priv->tiling_mode)) {
3147 ret = i915_gem_object_unbind(obj);
3148 if (ret)
3149 return ret;
3150 }
Eric Anholt673a3942008-07-30 12:06:12 -07003151
3152 /* Choose the GTT offset for our buffer and put it there. */
3153 ret = i915_gem_object_pin(obj, (uint32_t) entry->alignment);
3154 if (ret)
3155 return ret;
3156
Jesse Barnes76446ca2009-12-17 22:05:42 -05003157 /*
3158 * Pre-965 chips need a fence register set up in order to
3159 * properly handle blits to/from tiled surfaces.
3160 */
3161 if (need_fence) {
3162 ret = i915_gem_object_get_fence_reg(obj);
3163 if (ret != 0) {
Jesse Barnes76446ca2009-12-17 22:05:42 -05003164 i915_gem_object_unpin(obj);
3165 return ret;
3166 }
3167 }
3168
Eric Anholt673a3942008-07-30 12:06:12 -07003169 entry->offset = obj_priv->gtt_offset;
3170
Eric Anholt673a3942008-07-30 12:06:12 -07003171 /* Apply the relocations, using the GTT aperture to avoid cache
3172 * flushing requirements.
3173 */
3174 for (i = 0; i < entry->relocation_count; i++) {
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003175 struct drm_i915_gem_relocation_entry *reloc= &relocs[i];
Eric Anholt673a3942008-07-30 12:06:12 -07003176 struct drm_gem_object *target_obj;
3177 struct drm_i915_gem_object *target_obj_priv;
Eric Anholt3043c602008-10-02 12:24:47 -07003178 uint32_t reloc_val, reloc_offset;
3179 uint32_t __iomem *reloc_entry;
Eric Anholt673a3942008-07-30 12:06:12 -07003180
Eric Anholt673a3942008-07-30 12:06:12 -07003181 target_obj = drm_gem_object_lookup(obj->dev, file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003182 reloc->target_handle);
Eric Anholt673a3942008-07-30 12:06:12 -07003183 if (target_obj == NULL) {
3184 i915_gem_object_unpin(obj);
3185 return -EBADF;
3186 }
Daniel Vetter23010e42010-03-08 13:35:02 +01003187 target_obj_priv = to_intel_bo(target_obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003188
Chris Wilson8542a0b2009-09-09 21:15:15 +01003189#if WATCH_RELOC
3190 DRM_INFO("%s: obj %p offset %08x target %d "
3191 "read %08x write %08x gtt %08x "
3192 "presumed %08x delta %08x\n",
3193 __func__,
3194 obj,
3195 (int) reloc->offset,
3196 (int) reloc->target_handle,
3197 (int) reloc->read_domains,
3198 (int) reloc->write_domain,
3199 (int) target_obj_priv->gtt_offset,
3200 (int) reloc->presumed_offset,
3201 reloc->delta);
3202#endif
3203
Eric Anholt673a3942008-07-30 12:06:12 -07003204 /* The target buffer should have appeared before us in the
3205 * exec_object list, so it should have a GTT space bound by now.
3206 */
3207 if (target_obj_priv->gtt_space == NULL) {
3208 DRM_ERROR("No GTT space found for object %d\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003209 reloc->target_handle);
Eric Anholt673a3942008-07-30 12:06:12 -07003210 drm_gem_object_unreference(target_obj);
3211 i915_gem_object_unpin(obj);
3212 return -EINVAL;
3213 }
3214
Chris Wilson8542a0b2009-09-09 21:15:15 +01003215 /* Validate that the target is in a valid r/w GPU domain */
Daniel Vetter16edd552010-02-19 11:52:02 +01003216 if (reloc->write_domain & (reloc->write_domain - 1)) {
3217 DRM_ERROR("reloc with multiple write domains: "
3218 "obj %p target %d offset %d "
3219 "read %08x write %08x",
3220 obj, reloc->target_handle,
3221 (int) reloc->offset,
3222 reloc->read_domains,
3223 reloc->write_domain);
3224 return -EINVAL;
3225 }
Chris Wilson8542a0b2009-09-09 21:15:15 +01003226 if (reloc->write_domain & I915_GEM_DOMAIN_CPU ||
3227 reloc->read_domains & I915_GEM_DOMAIN_CPU) {
3228 DRM_ERROR("reloc with read/write CPU domains: "
3229 "obj %p target %d offset %d "
3230 "read %08x write %08x",
3231 obj, reloc->target_handle,
3232 (int) reloc->offset,
3233 reloc->read_domains,
3234 reloc->write_domain);
3235 drm_gem_object_unreference(target_obj);
3236 i915_gem_object_unpin(obj);
3237 return -EINVAL;
3238 }
3239 if (reloc->write_domain && target_obj->pending_write_domain &&
3240 reloc->write_domain != target_obj->pending_write_domain) {
3241 DRM_ERROR("Write domain conflict: "
3242 "obj %p target %d offset %d "
3243 "new %08x old %08x\n",
3244 obj, reloc->target_handle,
3245 (int) reloc->offset,
3246 reloc->write_domain,
3247 target_obj->pending_write_domain);
3248 drm_gem_object_unreference(target_obj);
3249 i915_gem_object_unpin(obj);
3250 return -EINVAL;
3251 }
3252
3253 target_obj->pending_read_domains |= reloc->read_domains;
3254 target_obj->pending_write_domain |= reloc->write_domain;
3255
3256 /* If the relocation already has the right value in it, no
3257 * more work needs to be done.
3258 */
3259 if (target_obj_priv->gtt_offset == reloc->presumed_offset) {
3260 drm_gem_object_unreference(target_obj);
3261 continue;
3262 }
3263
3264 /* Check that the relocation address is valid... */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003265 if (reloc->offset > obj->size - 4) {
Eric Anholt673a3942008-07-30 12:06:12 -07003266 DRM_ERROR("Relocation beyond object bounds: "
3267 "obj %p target %d offset %d size %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003268 obj, reloc->target_handle,
3269 (int) reloc->offset, (int) obj->size);
Eric Anholt673a3942008-07-30 12:06:12 -07003270 drm_gem_object_unreference(target_obj);
3271 i915_gem_object_unpin(obj);
3272 return -EINVAL;
3273 }
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003274 if (reloc->offset & 3) {
Eric Anholt673a3942008-07-30 12:06:12 -07003275 DRM_ERROR("Relocation not 4-byte aligned: "
3276 "obj %p target %d offset %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003277 obj, reloc->target_handle,
3278 (int) reloc->offset);
Eric Anholt673a3942008-07-30 12:06:12 -07003279 drm_gem_object_unreference(target_obj);
3280 i915_gem_object_unpin(obj);
3281 return -EINVAL;
3282 }
3283
Chris Wilson8542a0b2009-09-09 21:15:15 +01003284 /* and points to somewhere within the target object. */
Chris Wilsoncd0b9fb2009-09-15 23:23:18 +01003285 if (reloc->delta >= target_obj->size) {
3286 DRM_ERROR("Relocation beyond target object bounds: "
3287 "obj %p target %d delta %d size %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003288 obj, reloc->target_handle,
Chris Wilsoncd0b9fb2009-09-15 23:23:18 +01003289 (int) reloc->delta, (int) target_obj->size);
Chris Wilson491152b2009-02-11 14:26:32 +00003290 drm_gem_object_unreference(target_obj);
3291 i915_gem_object_unpin(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003292 return -EINVAL;
3293 }
3294
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003295 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
3296 if (ret != 0) {
3297 drm_gem_object_unreference(target_obj);
3298 i915_gem_object_unpin(obj);
3299 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -07003300 }
3301
3302 /* Map the page containing the relocation we're going to
3303 * perform.
3304 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003305 reloc_offset = obj_priv->gtt_offset + reloc->offset;
Keith Packard0839ccb2008-10-30 19:38:48 -07003306 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
3307 (reloc_offset &
Chris Wilsonfca3ec02010-08-04 14:34:24 +01003308 ~(PAGE_SIZE - 1)),
3309 KM_USER0);
Eric Anholt3043c602008-10-02 12:24:47 -07003310 reloc_entry = (uint32_t __iomem *)(reloc_page +
Keith Packard0839ccb2008-10-30 19:38:48 -07003311 (reloc_offset & (PAGE_SIZE - 1)));
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003312 reloc_val = target_obj_priv->gtt_offset + reloc->delta;
Eric Anholt673a3942008-07-30 12:06:12 -07003313
3314#if WATCH_BUF
3315 DRM_INFO("Applied relocation: %p@0x%08x %08x -> %08x\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003316 obj, (unsigned int) reloc->offset,
Eric Anholt673a3942008-07-30 12:06:12 -07003317 readl(reloc_entry), reloc_val);
3318#endif
3319 writel(reloc_val, reloc_entry);
Chris Wilsonfca3ec02010-08-04 14:34:24 +01003320 io_mapping_unmap_atomic(reloc_page, KM_USER0);
Eric Anholt673a3942008-07-30 12:06:12 -07003321
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003322 /* The updated presumed offset for this entry will be
3323 * copied back out to the user.
Eric Anholt673a3942008-07-30 12:06:12 -07003324 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003325 reloc->presumed_offset = target_obj_priv->gtt_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003326
3327 drm_gem_object_unreference(target_obj);
3328 }
3329
Eric Anholt673a3942008-07-30 12:06:12 -07003330#if WATCH_BUF
3331 if (0)
3332 i915_gem_dump_object(obj, 128, __func__, ~0);
3333#endif
3334 return 0;
3335}
3336
Eric Anholt673a3942008-07-30 12:06:12 -07003337/* Throttle our rendering by waiting until the ring has completed our requests
3338 * emitted over 20 msec ago.
3339 *
Eric Anholtb9624422009-06-03 07:27:35 +00003340 * Note that if we were to use the current jiffies each time around the loop,
3341 * we wouldn't escape the function with any frames outstanding if the time to
3342 * render a frame was over 20ms.
3343 *
Eric Anholt673a3942008-07-30 12:06:12 -07003344 * This should get us reasonable parallelism between CPU and GPU but also
3345 * relatively low latency when blocking on a particular request to finish.
3346 */
3347static int
3348i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file_priv)
3349{
3350 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
3351 int ret = 0;
Eric Anholtb9624422009-06-03 07:27:35 +00003352 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
Eric Anholt673a3942008-07-30 12:06:12 -07003353
3354 mutex_lock(&dev->struct_mutex);
Eric Anholtb9624422009-06-03 07:27:35 +00003355 while (!list_empty(&i915_file_priv->mm.request_list)) {
3356 struct drm_i915_gem_request *request;
3357
3358 request = list_first_entry(&i915_file_priv->mm.request_list,
3359 struct drm_i915_gem_request,
3360 client_list);
3361
3362 if (time_after_eq(request->emitted_jiffies, recent_enough))
3363 break;
3364
Zou Nan hai852835f2010-05-21 09:08:56 +08003365 ret = i915_wait_request(dev, request->seqno, request->ring);
Eric Anholtb9624422009-06-03 07:27:35 +00003366 if (ret != 0)
3367 break;
3368 }
Eric Anholt673a3942008-07-30 12:06:12 -07003369 mutex_unlock(&dev->struct_mutex);
Eric Anholtb9624422009-06-03 07:27:35 +00003370
Eric Anholt673a3942008-07-30 12:06:12 -07003371 return ret;
3372}
3373
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003374static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003375i915_gem_get_relocs_from_user(struct drm_i915_gem_exec_object2 *exec_list,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003376 uint32_t buffer_count,
3377 struct drm_i915_gem_relocation_entry **relocs)
3378{
3379 uint32_t reloc_count = 0, reloc_index = 0, i;
3380 int ret;
3381
3382 *relocs = NULL;
3383 for (i = 0; i < buffer_count; i++) {
3384 if (reloc_count + exec_list[i].relocation_count < reloc_count)
3385 return -EINVAL;
3386 reloc_count += exec_list[i].relocation_count;
3387 }
3388
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003389 *relocs = drm_calloc_large(reloc_count, sizeof(**relocs));
Jesse Barnes76446ca2009-12-17 22:05:42 -05003390 if (*relocs == NULL) {
3391 DRM_ERROR("failed to alloc relocs, count %d\n", reloc_count);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003392 return -ENOMEM;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003393 }
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003394
3395 for (i = 0; i < buffer_count; i++) {
3396 struct drm_i915_gem_relocation_entry __user *user_relocs;
3397
3398 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3399
3400 ret = copy_from_user(&(*relocs)[reloc_index],
3401 user_relocs,
3402 exec_list[i].relocation_count *
3403 sizeof(**relocs));
3404 if (ret != 0) {
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003405 drm_free_large(*relocs);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003406 *relocs = NULL;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003407 return -EFAULT;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003408 }
3409
3410 reloc_index += exec_list[i].relocation_count;
3411 }
3412
Florian Mickler2bc43b52009-04-06 22:55:41 +02003413 return 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003414}
3415
3416static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003417i915_gem_put_relocs_to_user(struct drm_i915_gem_exec_object2 *exec_list,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003418 uint32_t buffer_count,
3419 struct drm_i915_gem_relocation_entry *relocs)
3420{
3421 uint32_t reloc_count = 0, i;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003422 int ret = 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003423
Chris Wilson93533c22010-01-31 10:40:48 +00003424 if (relocs == NULL)
3425 return 0;
3426
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003427 for (i = 0; i < buffer_count; i++) {
3428 struct drm_i915_gem_relocation_entry __user *user_relocs;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003429 int unwritten;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003430
3431 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3432
Florian Mickler2bc43b52009-04-06 22:55:41 +02003433 unwritten = copy_to_user(user_relocs,
3434 &relocs[reloc_count],
3435 exec_list[i].relocation_count *
3436 sizeof(*relocs));
3437
3438 if (unwritten) {
3439 ret = -EFAULT;
3440 goto err;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003441 }
3442
3443 reloc_count += exec_list[i].relocation_count;
3444 }
3445
Florian Mickler2bc43b52009-04-06 22:55:41 +02003446err:
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003447 drm_free_large(relocs);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003448
3449 return ret;
3450}
3451
Chris Wilson83d60792009-06-06 09:45:57 +01003452static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003453i915_gem_check_execbuffer (struct drm_i915_gem_execbuffer2 *exec,
Chris Wilson83d60792009-06-06 09:45:57 +01003454 uint64_t exec_offset)
3455{
3456 uint32_t exec_start, exec_len;
3457
3458 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
3459 exec_len = (uint32_t) exec->batch_len;
3460
3461 if ((exec_start | exec_len) & 0x7)
3462 return -EINVAL;
3463
3464 if (!exec_start)
3465 return -EINVAL;
3466
3467 return 0;
3468}
3469
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003470static int
3471i915_gem_wait_for_pending_flip(struct drm_device *dev,
3472 struct drm_gem_object **object_list,
3473 int count)
3474{
3475 drm_i915_private_t *dev_priv = dev->dev_private;
3476 struct drm_i915_gem_object *obj_priv;
3477 DEFINE_WAIT(wait);
3478 int i, ret = 0;
3479
3480 for (;;) {
3481 prepare_to_wait(&dev_priv->pending_flip_queue,
3482 &wait, TASK_INTERRUPTIBLE);
3483 for (i = 0; i < count; i++) {
Daniel Vetter23010e42010-03-08 13:35:02 +01003484 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003485 if (atomic_read(&obj_priv->pending_flip) > 0)
3486 break;
3487 }
3488 if (i == count)
3489 break;
3490
3491 if (!signal_pending(current)) {
3492 mutex_unlock(&dev->struct_mutex);
3493 schedule();
3494 mutex_lock(&dev->struct_mutex);
3495 continue;
3496 }
3497 ret = -ERESTARTSYS;
3498 break;
3499 }
3500 finish_wait(&dev_priv->pending_flip_queue, &wait);
3501
3502 return ret;
3503}
3504
Chris Wilson43b27f42010-07-02 08:57:15 +01003505
Eric Anholt673a3942008-07-30 12:06:12 -07003506int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003507i915_gem_do_execbuffer(struct drm_device *dev, void *data,
3508 struct drm_file *file_priv,
3509 struct drm_i915_gem_execbuffer2 *args,
3510 struct drm_i915_gem_exec_object2 *exec_list)
Eric Anholt673a3942008-07-30 12:06:12 -07003511{
3512 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003513 struct drm_gem_object **object_list = NULL;
3514 struct drm_gem_object *batch_obj;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003515 struct drm_i915_gem_object *obj_priv;
Eric Anholt201361a2009-03-11 12:30:04 -07003516 struct drm_clip_rect *cliprects = NULL;
Chris Wilson93533c22010-01-31 10:40:48 +00003517 struct drm_i915_gem_relocation_entry *relocs = NULL;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003518 int ret = 0, ret2, i, pinned = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003519 uint64_t exec_offset;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003520 uint32_t seqno, flush_domains, reloc_index;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003521 int pin_tries, flips;
Eric Anholt673a3942008-07-30 12:06:12 -07003522
Zou Nan hai852835f2010-05-21 09:08:56 +08003523 struct intel_ring_buffer *ring = NULL;
3524
Eric Anholt673a3942008-07-30 12:06:12 -07003525#if WATCH_EXEC
3526 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3527 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3528#endif
Zou Nan haid1b851f2010-05-21 09:08:57 +08003529 if (args->flags & I915_EXEC_BSD) {
3530 if (!HAS_BSD(dev)) {
3531 DRM_ERROR("execbuf with wrong flag\n");
3532 return -EINVAL;
3533 }
3534 ring = &dev_priv->bsd_ring;
3535 } else {
3536 ring = &dev_priv->render_ring;
3537 }
3538
Eric Anholt4f481ed2008-09-10 14:22:49 -07003539 if (args->buffer_count < 1) {
3540 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3541 return -EINVAL;
3542 }
Eric Anholtc8e0f932009-11-22 03:49:37 +01003543 object_list = drm_malloc_ab(sizeof(*object_list), args->buffer_count);
Jesse Barnes76446ca2009-12-17 22:05:42 -05003544 if (object_list == NULL) {
3545 DRM_ERROR("Failed to allocate object list for %d buffers\n",
Eric Anholt673a3942008-07-30 12:06:12 -07003546 args->buffer_count);
3547 ret = -ENOMEM;
3548 goto pre_mutex_err;
3549 }
Eric Anholt673a3942008-07-30 12:06:12 -07003550
Eric Anholt201361a2009-03-11 12:30:04 -07003551 if (args->num_cliprects != 0) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003552 cliprects = kcalloc(args->num_cliprects, sizeof(*cliprects),
3553 GFP_KERNEL);
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003554 if (cliprects == NULL) {
3555 ret = -ENOMEM;
Eric Anholt201361a2009-03-11 12:30:04 -07003556 goto pre_mutex_err;
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003557 }
Eric Anholt201361a2009-03-11 12:30:04 -07003558
3559 ret = copy_from_user(cliprects,
3560 (struct drm_clip_rect __user *)
3561 (uintptr_t) args->cliprects_ptr,
3562 sizeof(*cliprects) * args->num_cliprects);
3563 if (ret != 0) {
3564 DRM_ERROR("copy %d cliprects failed: %d\n",
3565 args->num_cliprects, ret);
3566 goto pre_mutex_err;
3567 }
3568 }
3569
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003570 ret = i915_gem_get_relocs_from_user(exec_list, args->buffer_count,
3571 &relocs);
3572 if (ret != 0)
3573 goto pre_mutex_err;
3574
Eric Anholt673a3942008-07-30 12:06:12 -07003575 mutex_lock(&dev->struct_mutex);
3576
3577 i915_verify_inactive(dev, __FILE__, __LINE__);
3578
Ben Gamariba1234d2009-09-14 17:48:47 -04003579 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07003580 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003581 ret = -EIO;
3582 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003583 }
3584
3585 if (dev_priv->mm.suspended) {
Eric Anholt673a3942008-07-30 12:06:12 -07003586 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003587 ret = -EBUSY;
3588 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003589 }
3590
Keith Packardac94a962008-11-20 23:30:27 -08003591 /* Look up object handles */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003592 flips = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003593 for (i = 0; i < args->buffer_count; i++) {
3594 object_list[i] = drm_gem_object_lookup(dev, file_priv,
3595 exec_list[i].handle);
3596 if (object_list[i] == NULL) {
3597 DRM_ERROR("Invalid object handle %d at index %d\n",
3598 exec_list[i].handle, i);
Chris Wilson0ce907f2010-01-23 20:26:35 +00003599 /* prevent error path from reading uninitialized data */
3600 args->buffer_count = i + 1;
Eric Anholt673a3942008-07-30 12:06:12 -07003601 ret = -EBADF;
3602 goto err;
3603 }
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003604
Daniel Vetter23010e42010-03-08 13:35:02 +01003605 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003606 if (obj_priv->in_execbuffer) {
3607 DRM_ERROR("Object %p appears more than once in object list\n",
3608 object_list[i]);
Chris Wilson0ce907f2010-01-23 20:26:35 +00003609 /* prevent error path from reading uninitialized data */
3610 args->buffer_count = i + 1;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003611 ret = -EBADF;
3612 goto err;
3613 }
3614 obj_priv->in_execbuffer = true;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003615 flips += atomic_read(&obj_priv->pending_flip);
3616 }
3617
3618 if (flips > 0) {
3619 ret = i915_gem_wait_for_pending_flip(dev, object_list,
3620 args->buffer_count);
3621 if (ret)
3622 goto err;
Keith Packardac94a962008-11-20 23:30:27 -08003623 }
Eric Anholt673a3942008-07-30 12:06:12 -07003624
Keith Packardac94a962008-11-20 23:30:27 -08003625 /* Pin and relocate */
3626 for (pin_tries = 0; ; pin_tries++) {
3627 ret = 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003628 reloc_index = 0;
3629
Keith Packardac94a962008-11-20 23:30:27 -08003630 for (i = 0; i < args->buffer_count; i++) {
3631 object_list[i]->pending_read_domains = 0;
3632 object_list[i]->pending_write_domain = 0;
3633 ret = i915_gem_object_pin_and_relocate(object_list[i],
3634 file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003635 &exec_list[i],
3636 &relocs[reloc_index]);
Keith Packardac94a962008-11-20 23:30:27 -08003637 if (ret)
3638 break;
3639 pinned = i + 1;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003640 reloc_index += exec_list[i].relocation_count;
Keith Packardac94a962008-11-20 23:30:27 -08003641 }
3642 /* success */
3643 if (ret == 0)
3644 break;
3645
3646 /* error other than GTT full, or we've already tried again */
Chris Wilson2939e1f2009-06-06 09:46:03 +01003647 if (ret != -ENOSPC || pin_tries >= 1) {
Chris Wilson07f73f62009-09-14 16:50:30 +01003648 if (ret != -ERESTARTSYS) {
3649 unsigned long long total_size = 0;
Chris Wilson3d1cc472010-05-27 13:18:19 +01003650 int num_fences = 0;
3651 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson43b27f42010-07-02 08:57:15 +01003652 obj_priv = to_intel_bo(object_list[i]);
Chris Wilson3d1cc472010-05-27 13:18:19 +01003653
Chris Wilson07f73f62009-09-14 16:50:30 +01003654 total_size += object_list[i]->size;
Chris Wilson3d1cc472010-05-27 13:18:19 +01003655 num_fences +=
3656 exec_list[i].flags & EXEC_OBJECT_NEEDS_FENCE &&
3657 obj_priv->tiling_mode != I915_TILING_NONE;
3658 }
3659 DRM_ERROR("Failed to pin buffer %d of %d, total %llu bytes, %d fences: %d\n",
Chris Wilson07f73f62009-09-14 16:50:30 +01003660 pinned+1, args->buffer_count,
Chris Wilson3d1cc472010-05-27 13:18:19 +01003661 total_size, num_fences,
3662 ret);
Chris Wilson07f73f62009-09-14 16:50:30 +01003663 DRM_ERROR("%d objects [%d pinned], "
3664 "%d object bytes [%d pinned], "
3665 "%d/%d gtt bytes\n",
3666 atomic_read(&dev->object_count),
3667 atomic_read(&dev->pin_count),
3668 atomic_read(&dev->object_memory),
3669 atomic_read(&dev->pin_memory),
3670 atomic_read(&dev->gtt_memory),
3671 dev->gtt_total);
3672 }
Eric Anholt673a3942008-07-30 12:06:12 -07003673 goto err;
3674 }
Keith Packardac94a962008-11-20 23:30:27 -08003675
3676 /* unpin all of our buffers */
3677 for (i = 0; i < pinned; i++)
3678 i915_gem_object_unpin(object_list[i]);
Eric Anholtb1177632008-12-10 10:09:41 -08003679 pinned = 0;
Keith Packardac94a962008-11-20 23:30:27 -08003680
3681 /* evict everyone we can from the aperture */
3682 ret = i915_gem_evict_everything(dev);
Chris Wilson07f73f62009-09-14 16:50:30 +01003683 if (ret && ret != -ENOSPC)
Keith Packardac94a962008-11-20 23:30:27 -08003684 goto err;
Eric Anholt673a3942008-07-30 12:06:12 -07003685 }
3686
3687 /* Set the pending read domains for the batch buffer to COMMAND */
3688 batch_obj = object_list[args->buffer_count-1];
Chris Wilson5f26a2c2009-06-06 09:45:58 +01003689 if (batch_obj->pending_write_domain) {
3690 DRM_ERROR("Attempting to use self-modifying batch buffer\n");
3691 ret = -EINVAL;
3692 goto err;
3693 }
3694 batch_obj->pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
Eric Anholt673a3942008-07-30 12:06:12 -07003695
Chris Wilson83d60792009-06-06 09:45:57 +01003696 /* Sanity check the batch buffer, prior to moving objects */
3697 exec_offset = exec_list[args->buffer_count - 1].offset;
3698 ret = i915_gem_check_execbuffer (args, exec_offset);
3699 if (ret != 0) {
3700 DRM_ERROR("execbuf with invalid offset/length\n");
3701 goto err;
3702 }
3703
Eric Anholt673a3942008-07-30 12:06:12 -07003704 i915_verify_inactive(dev, __FILE__, __LINE__);
3705
Keith Packard646f0f62008-11-20 23:23:03 -08003706 /* Zero the global flush/invalidate flags. These
3707 * will be modified as new domains are computed
3708 * for each object
3709 */
3710 dev->invalidate_domains = 0;
3711 dev->flush_domains = 0;
Chris Wilson88f356b2010-08-04 13:55:32 +01003712 dev_priv->flush_rings = 0;
Keith Packard646f0f62008-11-20 23:23:03 -08003713
Eric Anholt673a3942008-07-30 12:06:12 -07003714 for (i = 0; i < args->buffer_count; i++) {
3715 struct drm_gem_object *obj = object_list[i];
Eric Anholt673a3942008-07-30 12:06:12 -07003716
Keith Packard646f0f62008-11-20 23:23:03 -08003717 /* Compute new gpu domains and update invalidate/flush */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003718 i915_gem_object_set_to_gpu_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003719 }
3720
3721 i915_verify_inactive(dev, __FILE__, __LINE__);
3722
Keith Packard646f0f62008-11-20 23:23:03 -08003723 if (dev->invalidate_domains | dev->flush_domains) {
3724#if WATCH_EXEC
3725 DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
3726 __func__,
3727 dev->invalidate_domains,
3728 dev->flush_domains);
3729#endif
3730 i915_gem_flush(dev,
3731 dev->invalidate_domains,
3732 dev->flush_domains);
Chris Wilson88f356b2010-08-04 13:55:32 +01003733 if (dev_priv->flush_rings & FLUSH_RENDER_RING)
Eric Anholtb9624422009-06-03 07:27:35 +00003734 (void)i915_add_request(dev, file_priv,
Chris Wilson88f356b2010-08-04 13:55:32 +01003735 dev->flush_domains,
3736 &dev_priv->render_ring);
3737 if (dev_priv->flush_rings & FLUSH_BSD_RING)
3738 (void)i915_add_request(dev, file_priv,
3739 dev->flush_domains,
3740 &dev_priv->bsd_ring);
Keith Packard646f0f62008-11-20 23:23:03 -08003741 }
Eric Anholt673a3942008-07-30 12:06:12 -07003742
Eric Anholtefbeed92009-02-19 14:54:51 -08003743 for (i = 0; i < args->buffer_count; i++) {
3744 struct drm_gem_object *obj = object_list[i];
Daniel Vetter23010e42010-03-08 13:35:02 +01003745 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003746 uint32_t old_write_domain = obj->write_domain;
Eric Anholtefbeed92009-02-19 14:54:51 -08003747
3748 obj->write_domain = obj->pending_write_domain;
Daniel Vetter99fcb762010-02-07 16:20:18 +01003749 if (obj->write_domain)
3750 list_move_tail(&obj_priv->gpu_write_list,
3751 &dev_priv->mm.gpu_write_list);
3752 else
3753 list_del_init(&obj_priv->gpu_write_list);
3754
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003755 trace_i915_gem_object_change_domain(obj,
3756 obj->read_domains,
3757 old_write_domain);
Eric Anholtefbeed92009-02-19 14:54:51 -08003758 }
3759
Eric Anholt673a3942008-07-30 12:06:12 -07003760 i915_verify_inactive(dev, __FILE__, __LINE__);
3761
3762#if WATCH_COHERENCY
3763 for (i = 0; i < args->buffer_count; i++) {
3764 i915_gem_object_check_coherency(object_list[i],
3765 exec_list[i].handle);
3766 }
3767#endif
3768
Eric Anholt673a3942008-07-30 12:06:12 -07003769#if WATCH_EXEC
Ben Gamari6911a9b2009-04-02 11:24:54 -07003770 i915_gem_dump_object(batch_obj,
Eric Anholt673a3942008-07-30 12:06:12 -07003771 args->batch_len,
3772 __func__,
3773 ~0);
3774#endif
3775
Eric Anholt673a3942008-07-30 12:06:12 -07003776 /* Exec the batchbuffer */
Zou Nan hai852835f2010-05-21 09:08:56 +08003777 ret = ring->dispatch_gem_execbuffer(dev, ring, args,
3778 cliprects, exec_offset);
Eric Anholt673a3942008-07-30 12:06:12 -07003779 if (ret) {
3780 DRM_ERROR("dispatch failed %d\n", ret);
3781 goto err;
3782 }
3783
3784 /*
3785 * Ensure that the commands in the batch buffer are
3786 * finished before the interrupt fires
3787 */
Zou Nan hai852835f2010-05-21 09:08:56 +08003788 flush_domains = i915_retire_commands(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07003789
3790 i915_verify_inactive(dev, __FILE__, __LINE__);
3791
3792 /*
3793 * Get a seqno representing the execution of the current buffer,
3794 * which we can wait on. We would like to mitigate these interrupts,
3795 * likely by only creating seqnos occasionally (so that we have
3796 * *some* interrupts representing completion of buffers that we can
3797 * wait on when trying to clear up gtt space).
3798 */
Zou Nan hai852835f2010-05-21 09:08:56 +08003799 seqno = i915_add_request(dev, file_priv, flush_domains, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07003800 BUG_ON(seqno == 0);
Eric Anholt673a3942008-07-30 12:06:12 -07003801 for (i = 0; i < args->buffer_count; i++) {
3802 struct drm_gem_object *obj = object_list[i];
Zou Nan hai852835f2010-05-21 09:08:56 +08003803 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003804
Zou Nan hai852835f2010-05-21 09:08:56 +08003805 i915_gem_object_move_to_active(obj, seqno, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07003806#if WATCH_LRU
3807 DRM_INFO("%s: move to exec list %p\n", __func__, obj);
3808#endif
3809 }
3810#if WATCH_LRU
3811 i915_dump_lru(dev, __func__);
3812#endif
3813
3814 i915_verify_inactive(dev, __FILE__, __LINE__);
3815
Eric Anholt673a3942008-07-30 12:06:12 -07003816err:
Julia Lawallaad87df2008-12-21 16:28:47 +01003817 for (i = 0; i < pinned; i++)
3818 i915_gem_object_unpin(object_list[i]);
Eric Anholt673a3942008-07-30 12:06:12 -07003819
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003820 for (i = 0; i < args->buffer_count; i++) {
3821 if (object_list[i]) {
Daniel Vetter23010e42010-03-08 13:35:02 +01003822 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003823 obj_priv->in_execbuffer = false;
3824 }
Julia Lawallaad87df2008-12-21 16:28:47 +01003825 drm_gem_object_unreference(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003826 }
Julia Lawallaad87df2008-12-21 16:28:47 +01003827
Eric Anholt673a3942008-07-30 12:06:12 -07003828 mutex_unlock(&dev->struct_mutex);
3829
Chris Wilson93533c22010-01-31 10:40:48 +00003830pre_mutex_err:
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003831 /* Copy the updated relocations out regardless of current error
3832 * state. Failure to update the relocs would mean that the next
3833 * time userland calls execbuf, it would do so with presumed offset
3834 * state that didn't match the actual object state.
3835 */
3836 ret2 = i915_gem_put_relocs_to_user(exec_list, args->buffer_count,
3837 relocs);
3838 if (ret2 != 0) {
3839 DRM_ERROR("Failed to copy relocations back out: %d\n", ret2);
3840
3841 if (ret == 0)
3842 ret = ret2;
3843 }
3844
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003845 drm_free_large(object_list);
Eric Anholt9a298b22009-03-24 12:23:04 -07003846 kfree(cliprects);
Eric Anholt673a3942008-07-30 12:06:12 -07003847
3848 return ret;
3849}
3850
Jesse Barnes76446ca2009-12-17 22:05:42 -05003851/*
3852 * Legacy execbuffer just creates an exec2 list from the original exec object
3853 * list array and passes it to the real function.
3854 */
3855int
3856i915_gem_execbuffer(struct drm_device *dev, void *data,
3857 struct drm_file *file_priv)
3858{
3859 struct drm_i915_gem_execbuffer *args = data;
3860 struct drm_i915_gem_execbuffer2 exec2;
3861 struct drm_i915_gem_exec_object *exec_list = NULL;
3862 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
3863 int ret, i;
3864
3865#if WATCH_EXEC
3866 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3867 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3868#endif
3869
3870 if (args->buffer_count < 1) {
3871 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3872 return -EINVAL;
3873 }
3874
3875 /* Copy in the exec list from userland */
3876 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
3877 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
3878 if (exec_list == NULL || exec2_list == NULL) {
3879 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
3880 args->buffer_count);
3881 drm_free_large(exec_list);
3882 drm_free_large(exec2_list);
3883 return -ENOMEM;
3884 }
3885 ret = copy_from_user(exec_list,
3886 (struct drm_i915_relocation_entry __user *)
3887 (uintptr_t) args->buffers_ptr,
3888 sizeof(*exec_list) * args->buffer_count);
3889 if (ret != 0) {
3890 DRM_ERROR("copy %d exec entries failed %d\n",
3891 args->buffer_count, ret);
3892 drm_free_large(exec_list);
3893 drm_free_large(exec2_list);
3894 return -EFAULT;
3895 }
3896
3897 for (i = 0; i < args->buffer_count; i++) {
3898 exec2_list[i].handle = exec_list[i].handle;
3899 exec2_list[i].relocation_count = exec_list[i].relocation_count;
3900 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
3901 exec2_list[i].alignment = exec_list[i].alignment;
3902 exec2_list[i].offset = exec_list[i].offset;
3903 if (!IS_I965G(dev))
3904 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
3905 else
3906 exec2_list[i].flags = 0;
3907 }
3908
3909 exec2.buffers_ptr = args->buffers_ptr;
3910 exec2.buffer_count = args->buffer_count;
3911 exec2.batch_start_offset = args->batch_start_offset;
3912 exec2.batch_len = args->batch_len;
3913 exec2.DR1 = args->DR1;
3914 exec2.DR4 = args->DR4;
3915 exec2.num_cliprects = args->num_cliprects;
3916 exec2.cliprects_ptr = args->cliprects_ptr;
Zou Nan hai852835f2010-05-21 09:08:56 +08003917 exec2.flags = I915_EXEC_RENDER;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003918
3919 ret = i915_gem_do_execbuffer(dev, data, file_priv, &exec2, exec2_list);
3920 if (!ret) {
3921 /* Copy the new buffer offsets back to the user's exec list. */
3922 for (i = 0; i < args->buffer_count; i++)
3923 exec_list[i].offset = exec2_list[i].offset;
3924 /* ... and back out to userspace */
3925 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
3926 (uintptr_t) args->buffers_ptr,
3927 exec_list,
3928 sizeof(*exec_list) * args->buffer_count);
3929 if (ret) {
3930 ret = -EFAULT;
3931 DRM_ERROR("failed to copy %d exec entries "
3932 "back to user (%d)\n",
3933 args->buffer_count, ret);
3934 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05003935 }
3936
3937 drm_free_large(exec_list);
3938 drm_free_large(exec2_list);
3939 return ret;
3940}
3941
3942int
3943i915_gem_execbuffer2(struct drm_device *dev, void *data,
3944 struct drm_file *file_priv)
3945{
3946 struct drm_i915_gem_execbuffer2 *args = data;
3947 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
3948 int ret;
3949
3950#if WATCH_EXEC
3951 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3952 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3953#endif
3954
3955 if (args->buffer_count < 1) {
3956 DRM_ERROR("execbuf2 with %d buffers\n", args->buffer_count);
3957 return -EINVAL;
3958 }
3959
3960 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
3961 if (exec2_list == NULL) {
3962 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
3963 args->buffer_count);
3964 return -ENOMEM;
3965 }
3966 ret = copy_from_user(exec2_list,
3967 (struct drm_i915_relocation_entry __user *)
3968 (uintptr_t) args->buffers_ptr,
3969 sizeof(*exec2_list) * args->buffer_count);
3970 if (ret != 0) {
3971 DRM_ERROR("copy %d exec entries failed %d\n",
3972 args->buffer_count, ret);
3973 drm_free_large(exec2_list);
3974 return -EFAULT;
3975 }
3976
3977 ret = i915_gem_do_execbuffer(dev, data, file_priv, args, exec2_list);
3978 if (!ret) {
3979 /* Copy the new buffer offsets back to the user's exec list. */
3980 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
3981 (uintptr_t) args->buffers_ptr,
3982 exec2_list,
3983 sizeof(*exec2_list) * args->buffer_count);
3984 if (ret) {
3985 ret = -EFAULT;
3986 DRM_ERROR("failed to copy %d exec entries "
3987 "back to user (%d)\n",
3988 args->buffer_count, ret);
3989 }
3990 }
3991
3992 drm_free_large(exec2_list);
3993 return ret;
3994}
3995
Eric Anholt673a3942008-07-30 12:06:12 -07003996int
3997i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment)
3998{
3999 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01004000 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004001 int ret;
4002
Daniel Vetter778c3542010-05-13 11:49:44 +02004003 BUG_ON(obj_priv->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT);
4004
Eric Anholt673a3942008-07-30 12:06:12 -07004005 i915_verify_inactive(dev, __FILE__, __LINE__);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01004006
4007 if (obj_priv->gtt_space != NULL) {
4008 if (alignment == 0)
4009 alignment = i915_gem_get_gtt_alignment(obj);
4010 if (obj_priv->gtt_offset & (alignment - 1)) {
Chris Wilsonae7d49d2010-08-04 12:37:41 +01004011 WARN(obj_priv->pin_count,
4012 "bo is already pinned with incorrect alignment:"
4013 " offset=%x, req.alignment=%x\n",
4014 obj_priv->gtt_offset, alignment);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01004015 ret = i915_gem_object_unbind(obj);
4016 if (ret)
4017 return ret;
4018 }
4019 }
4020
Eric Anholt673a3942008-07-30 12:06:12 -07004021 if (obj_priv->gtt_space == NULL) {
4022 ret = i915_gem_object_bind_to_gtt(obj, alignment);
Chris Wilson97311292009-09-21 00:22:34 +01004023 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07004024 return ret;
Chris Wilson22c344e2009-02-11 14:26:45 +00004025 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05004026
Eric Anholt673a3942008-07-30 12:06:12 -07004027 obj_priv->pin_count++;
4028
4029 /* If the object is not active and not pending a flush,
4030 * remove it from the inactive list
4031 */
4032 if (obj_priv->pin_count == 1) {
4033 atomic_inc(&dev->pin_count);
4034 atomic_add(obj->size, &dev->pin_memory);
4035 if (!obj_priv->active &&
Chris Wilsonbf1a1092010-08-07 11:01:20 +01004036 (obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
Eric Anholt673a3942008-07-30 12:06:12 -07004037 list_del_init(&obj_priv->list);
4038 }
4039 i915_verify_inactive(dev, __FILE__, __LINE__);
4040
4041 return 0;
4042}
4043
4044void
4045i915_gem_object_unpin(struct drm_gem_object *obj)
4046{
4047 struct drm_device *dev = obj->dev;
4048 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01004049 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004050
4051 i915_verify_inactive(dev, __FILE__, __LINE__);
4052 obj_priv->pin_count--;
4053 BUG_ON(obj_priv->pin_count < 0);
4054 BUG_ON(obj_priv->gtt_space == NULL);
4055
4056 /* If the object is no longer pinned, and is
4057 * neither active nor being flushed, then stick it on
4058 * the inactive list
4059 */
4060 if (obj_priv->pin_count == 0) {
4061 if (!obj_priv->active &&
Chris Wilson21d509e2009-06-06 09:46:02 +01004062 (obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
Eric Anholt673a3942008-07-30 12:06:12 -07004063 list_move_tail(&obj_priv->list,
4064 &dev_priv->mm.inactive_list);
4065 atomic_dec(&dev->pin_count);
4066 atomic_sub(obj->size, &dev->pin_memory);
4067 }
4068 i915_verify_inactive(dev, __FILE__, __LINE__);
4069}
4070
4071int
4072i915_gem_pin_ioctl(struct drm_device *dev, void *data,
4073 struct drm_file *file_priv)
4074{
4075 struct drm_i915_gem_pin *args = data;
4076 struct drm_gem_object *obj;
4077 struct drm_i915_gem_object *obj_priv;
4078 int ret;
4079
4080 mutex_lock(&dev->struct_mutex);
4081
4082 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4083 if (obj == NULL) {
4084 DRM_ERROR("Bad handle in i915_gem_pin_ioctl(): %d\n",
4085 args->handle);
4086 mutex_unlock(&dev->struct_mutex);
4087 return -EBADF;
4088 }
Daniel Vetter23010e42010-03-08 13:35:02 +01004089 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004090
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004091 if (obj_priv->madv != I915_MADV_WILLNEED) {
4092 DRM_ERROR("Attempting to pin a purgeable buffer\n");
Chris Wilson3ef94da2009-09-14 16:50:29 +01004093 drm_gem_object_unreference(obj);
4094 mutex_unlock(&dev->struct_mutex);
4095 return -EINVAL;
4096 }
4097
Jesse Barnes79e53942008-11-07 14:24:08 -08004098 if (obj_priv->pin_filp != NULL && obj_priv->pin_filp != file_priv) {
4099 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
4100 args->handle);
Chris Wilson96dec612009-02-08 19:08:04 +00004101 drm_gem_object_unreference(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004102 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08004103 return -EINVAL;
4104 }
4105
4106 obj_priv->user_pin_count++;
4107 obj_priv->pin_filp = file_priv;
4108 if (obj_priv->user_pin_count == 1) {
4109 ret = i915_gem_object_pin(obj, args->alignment);
4110 if (ret != 0) {
4111 drm_gem_object_unreference(obj);
4112 mutex_unlock(&dev->struct_mutex);
4113 return ret;
4114 }
Eric Anholt673a3942008-07-30 12:06:12 -07004115 }
4116
4117 /* XXX - flush the CPU caches for pinned objects
4118 * as the X server doesn't manage domains yet
4119 */
Eric Anholte47c68e2008-11-14 13:35:19 -08004120 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004121 args->offset = obj_priv->gtt_offset;
4122 drm_gem_object_unreference(obj);
4123 mutex_unlock(&dev->struct_mutex);
4124
4125 return 0;
4126}
4127
4128int
4129i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
4130 struct drm_file *file_priv)
4131{
4132 struct drm_i915_gem_pin *args = data;
4133 struct drm_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08004134 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07004135
4136 mutex_lock(&dev->struct_mutex);
4137
4138 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4139 if (obj == NULL) {
4140 DRM_ERROR("Bad handle in i915_gem_unpin_ioctl(): %d\n",
4141 args->handle);
4142 mutex_unlock(&dev->struct_mutex);
4143 return -EBADF;
4144 }
4145
Daniel Vetter23010e42010-03-08 13:35:02 +01004146 obj_priv = to_intel_bo(obj);
Jesse Barnes79e53942008-11-07 14:24:08 -08004147 if (obj_priv->pin_filp != file_priv) {
4148 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
4149 args->handle);
4150 drm_gem_object_unreference(obj);
4151 mutex_unlock(&dev->struct_mutex);
4152 return -EINVAL;
4153 }
4154 obj_priv->user_pin_count--;
4155 if (obj_priv->user_pin_count == 0) {
4156 obj_priv->pin_filp = NULL;
4157 i915_gem_object_unpin(obj);
4158 }
Eric Anholt673a3942008-07-30 12:06:12 -07004159
4160 drm_gem_object_unreference(obj);
4161 mutex_unlock(&dev->struct_mutex);
4162 return 0;
4163}
4164
4165int
4166i915_gem_busy_ioctl(struct drm_device *dev, void *data,
4167 struct drm_file *file_priv)
4168{
4169 struct drm_i915_gem_busy *args = data;
4170 struct drm_gem_object *obj;
4171 struct drm_i915_gem_object *obj_priv;
4172
Eric Anholt673a3942008-07-30 12:06:12 -07004173 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4174 if (obj == NULL) {
4175 DRM_ERROR("Bad handle in i915_gem_busy_ioctl(): %d\n",
4176 args->handle);
Eric Anholt673a3942008-07-30 12:06:12 -07004177 return -EBADF;
4178 }
4179
Chris Wilsonb1ce7862009-06-06 09:46:00 +01004180 mutex_lock(&dev->struct_mutex);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004181
Chris Wilson0be555b2010-08-04 15:36:30 +01004182 /* Count all active objects as busy, even if they are currently not used
4183 * by the gpu. Users of this interface expect objects to eventually
4184 * become non-busy without any further actions, therefore emit any
4185 * necessary flushes here.
Eric Anholtc4de0a52008-12-14 19:05:04 -08004186 */
Chris Wilson0be555b2010-08-04 15:36:30 +01004187 obj_priv = to_intel_bo(obj);
4188 args->busy = obj_priv->active;
4189 if (args->busy) {
4190 /* Unconditionally flush objects, even when the gpu still uses this
4191 * object. Userspace calling this function indicates that it wants to
4192 * use this buffer rather sooner than later, so issuing the required
4193 * flush earlier is beneficial.
4194 */
4195 if (obj->write_domain) {
4196 i915_gem_flush(dev, 0, obj->write_domain);
4197 (void)i915_add_request(dev, file_priv, obj->write_domain, obj_priv->ring);
4198 }
4199
4200 /* Update the active list for the hardware's current position.
4201 * Otherwise this only updates on a delayed timer or when irqs
4202 * are actually unmasked, and our working set ends up being
4203 * larger than required.
4204 */
4205 i915_gem_retire_requests_ring(dev, obj_priv->ring);
4206
4207 args->busy = obj_priv->active;
4208 }
Eric Anholt673a3942008-07-30 12:06:12 -07004209
4210 drm_gem_object_unreference(obj);
4211 mutex_unlock(&dev->struct_mutex);
4212 return 0;
4213}
4214
4215int
4216i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4217 struct drm_file *file_priv)
4218{
4219 return i915_gem_ring_throttle(dev, file_priv);
4220}
4221
Chris Wilson3ef94da2009-09-14 16:50:29 +01004222int
4223i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4224 struct drm_file *file_priv)
4225{
4226 struct drm_i915_gem_madvise *args = data;
4227 struct drm_gem_object *obj;
4228 struct drm_i915_gem_object *obj_priv;
4229
4230 switch (args->madv) {
4231 case I915_MADV_DONTNEED:
4232 case I915_MADV_WILLNEED:
4233 break;
4234 default:
4235 return -EINVAL;
4236 }
4237
4238 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4239 if (obj == NULL) {
4240 DRM_ERROR("Bad handle in i915_gem_madvise_ioctl(): %d\n",
4241 args->handle);
4242 return -EBADF;
4243 }
4244
4245 mutex_lock(&dev->struct_mutex);
Daniel Vetter23010e42010-03-08 13:35:02 +01004246 obj_priv = to_intel_bo(obj);
Chris Wilson3ef94da2009-09-14 16:50:29 +01004247
4248 if (obj_priv->pin_count) {
4249 drm_gem_object_unreference(obj);
4250 mutex_unlock(&dev->struct_mutex);
4251
4252 DRM_ERROR("Attempted i915_gem_madvise_ioctl() on a pinned object\n");
4253 return -EINVAL;
4254 }
4255
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004256 if (obj_priv->madv != __I915_MADV_PURGED)
4257 obj_priv->madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004258
Chris Wilson2d7ef392009-09-20 23:13:10 +01004259 /* if the object is no longer bound, discard its backing storage */
4260 if (i915_gem_object_is_purgeable(obj_priv) &&
4261 obj_priv->gtt_space == NULL)
4262 i915_gem_object_truncate(obj);
4263
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004264 args->retained = obj_priv->madv != __I915_MADV_PURGED;
4265
Chris Wilson3ef94da2009-09-14 16:50:29 +01004266 drm_gem_object_unreference(obj);
4267 mutex_unlock(&dev->struct_mutex);
4268
4269 return 0;
4270}
4271
Daniel Vetterac52bc52010-04-09 19:05:06 +00004272struct drm_gem_object * i915_gem_alloc_object(struct drm_device *dev,
4273 size_t size)
4274{
Daniel Vetterc397b902010-04-09 19:05:07 +00004275 struct drm_i915_gem_object *obj;
4276
4277 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
4278 if (obj == NULL)
4279 return NULL;
4280
4281 if (drm_gem_object_init(dev, &obj->base, size) != 0) {
4282 kfree(obj);
4283 return NULL;
4284 }
4285
4286 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4287 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4288
4289 obj->agp_type = AGP_USER_MEMORY;
Daniel Vetter62b8b212010-04-09 19:05:08 +00004290 obj->base.driver_private = NULL;
Daniel Vetterc397b902010-04-09 19:05:07 +00004291 obj->fence_reg = I915_FENCE_REG_NONE;
4292 INIT_LIST_HEAD(&obj->list);
4293 INIT_LIST_HEAD(&obj->gpu_write_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00004294 obj->madv = I915_MADV_WILLNEED;
4295
4296 trace_i915_gem_object_create(&obj->base);
4297
4298 return &obj->base;
Daniel Vetterac52bc52010-04-09 19:05:06 +00004299}
4300
Eric Anholt673a3942008-07-30 12:06:12 -07004301int i915_gem_init_object(struct drm_gem_object *obj)
4302{
Daniel Vetterc397b902010-04-09 19:05:07 +00004303 BUG();
Jesse Barnesde151cf2008-11-12 10:03:55 -08004304
Eric Anholt673a3942008-07-30 12:06:12 -07004305 return 0;
4306}
4307
Chris Wilsonbe726152010-07-23 23:18:50 +01004308static void i915_gem_free_object_tail(struct drm_gem_object *obj)
4309{
4310 struct drm_device *dev = obj->dev;
4311 drm_i915_private_t *dev_priv = dev->dev_private;
4312 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
4313 int ret;
4314
4315 ret = i915_gem_object_unbind(obj);
4316 if (ret == -ERESTARTSYS) {
4317 list_move(&obj_priv->list,
4318 &dev_priv->mm.deferred_free_list);
4319 return;
4320 }
4321
4322 if (obj_priv->mmap_offset)
4323 i915_gem_free_mmap_offset(obj);
4324
4325 drm_gem_object_release(obj);
4326
4327 kfree(obj_priv->page_cpu_valid);
4328 kfree(obj_priv->bit_17);
4329 kfree(obj_priv);
4330}
4331
Eric Anholt673a3942008-07-30 12:06:12 -07004332void i915_gem_free_object(struct drm_gem_object *obj)
4333{
Jesse Barnesde151cf2008-11-12 10:03:55 -08004334 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01004335 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004336
Chris Wilson1c5d22f2009-08-25 11:15:50 +01004337 trace_i915_gem_object_destroy(obj);
4338
Eric Anholt673a3942008-07-30 12:06:12 -07004339 while (obj_priv->pin_count > 0)
4340 i915_gem_object_unpin(obj);
4341
Dave Airlie71acb5e2008-12-30 20:31:46 +10004342 if (obj_priv->phys_obj)
4343 i915_gem_detach_phys_object(dev, obj);
4344
Chris Wilsonbe726152010-07-23 23:18:50 +01004345 i915_gem_free_object_tail(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004346}
4347
Jesse Barnes5669fca2009-02-17 15:13:31 -08004348int
Eric Anholt673a3942008-07-30 12:06:12 -07004349i915_gem_idle(struct drm_device *dev)
4350{
4351 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson29105cc2010-01-07 10:39:13 +00004352 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004353
Keith Packard6dbe2772008-10-14 21:41:13 -07004354 mutex_lock(&dev->struct_mutex);
4355
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004356 if (dev_priv->mm.suspended ||
Zou Nan haid1b851f2010-05-21 09:08:57 +08004357 (dev_priv->render_ring.gem_object == NULL) ||
4358 (HAS_BSD(dev) &&
4359 dev_priv->bsd_ring.gem_object == NULL)) {
Keith Packard6dbe2772008-10-14 21:41:13 -07004360 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004361 return 0;
Keith Packard6dbe2772008-10-14 21:41:13 -07004362 }
Eric Anholt673a3942008-07-30 12:06:12 -07004363
Chris Wilson29105cc2010-01-07 10:39:13 +00004364 ret = i915_gpu_idle(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004365 if (ret) {
4366 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004367 return ret;
Keith Packard6dbe2772008-10-14 21:41:13 -07004368 }
Eric Anholt673a3942008-07-30 12:06:12 -07004369
Chris Wilson29105cc2010-01-07 10:39:13 +00004370 /* Under UMS, be paranoid and evict. */
4371 if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
Chris Wilsonb47eb4a2010-08-07 11:01:23 +01004372 ret = i915_gem_evict_inactive(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00004373 if (ret) {
4374 mutex_unlock(&dev->struct_mutex);
4375 return ret;
4376 }
4377 }
4378
4379 /* Hack! Don't let anybody do execbuf while we don't control the chip.
4380 * We need to replace this with a semaphore, or something.
4381 * And not confound mm.suspended!
4382 */
4383 dev_priv->mm.suspended = 1;
4384 del_timer(&dev_priv->hangcheck_timer);
4385
4386 i915_kernel_lost_context(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004387 i915_gem_cleanup_ringbuffer(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00004388
Keith Packard6dbe2772008-10-14 21:41:13 -07004389 mutex_unlock(&dev->struct_mutex);
4390
Chris Wilson29105cc2010-01-07 10:39:13 +00004391 /* Cancel the retire work handler, which should be idle now. */
4392 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
4393
Eric Anholt673a3942008-07-30 12:06:12 -07004394 return 0;
4395}
4396
Jesse Barnese552eb72010-04-21 11:39:23 -07004397/*
4398 * 965+ support PIPE_CONTROL commands, which provide finer grained control
4399 * over cache flushing.
4400 */
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004401static int
Jesse Barnese552eb72010-04-21 11:39:23 -07004402i915_gem_init_pipe_control(struct drm_device *dev)
4403{
4404 drm_i915_private_t *dev_priv = dev->dev_private;
4405 struct drm_gem_object *obj;
4406 struct drm_i915_gem_object *obj_priv;
4407 int ret;
4408
Eric Anholt34dc4d42010-05-07 14:30:03 -07004409 obj = i915_gem_alloc_object(dev, 4096);
Jesse Barnese552eb72010-04-21 11:39:23 -07004410 if (obj == NULL) {
4411 DRM_ERROR("Failed to allocate seqno page\n");
4412 ret = -ENOMEM;
4413 goto err;
4414 }
4415 obj_priv = to_intel_bo(obj);
4416 obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
4417
4418 ret = i915_gem_object_pin(obj, 4096);
4419 if (ret)
4420 goto err_unref;
4421
4422 dev_priv->seqno_gfx_addr = obj_priv->gtt_offset;
4423 dev_priv->seqno_page = kmap(obj_priv->pages[0]);
4424 if (dev_priv->seqno_page == NULL)
4425 goto err_unpin;
4426
4427 dev_priv->seqno_obj = obj;
4428 memset(dev_priv->seqno_page, 0, PAGE_SIZE);
4429
4430 return 0;
4431
4432err_unpin:
4433 i915_gem_object_unpin(obj);
4434err_unref:
4435 drm_gem_object_unreference(obj);
4436err:
4437 return ret;
4438}
4439
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004440
4441static void
Jesse Barnese552eb72010-04-21 11:39:23 -07004442i915_gem_cleanup_pipe_control(struct drm_device *dev)
4443{
4444 drm_i915_private_t *dev_priv = dev->dev_private;
4445 struct drm_gem_object *obj;
4446 struct drm_i915_gem_object *obj_priv;
4447
4448 obj = dev_priv->seqno_obj;
4449 obj_priv = to_intel_bo(obj);
4450 kunmap(obj_priv->pages[0]);
4451 i915_gem_object_unpin(obj);
4452 drm_gem_object_unreference(obj);
4453 dev_priv->seqno_obj = NULL;
4454
4455 dev_priv->seqno_page = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07004456}
4457
Eric Anholt673a3942008-07-30 12:06:12 -07004458int
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004459i915_gem_init_ringbuffer(struct drm_device *dev)
4460{
4461 drm_i915_private_t *dev_priv = dev->dev_private;
4462 int ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01004463
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004464 dev_priv->render_ring = render_ring;
Chris Wilson68f95ba2010-05-27 13:18:22 +01004465
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004466 if (!I915_NEED_GFX_HWS(dev)) {
4467 dev_priv->render_ring.status_page.page_addr
4468 = dev_priv->status_page_dmah->vaddr;
4469 memset(dev_priv->render_ring.status_page.page_addr,
4470 0, PAGE_SIZE);
4471 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004472
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004473 if (HAS_PIPE_CONTROL(dev)) {
4474 ret = i915_gem_init_pipe_control(dev);
4475 if (ret)
4476 return ret;
4477 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004478
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004479 ret = intel_init_ring_buffer(dev, &dev_priv->render_ring);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004480 if (ret)
4481 goto cleanup_pipe_control;
4482
4483 if (HAS_BSD(dev)) {
Zou Nan haid1b851f2010-05-21 09:08:57 +08004484 dev_priv->bsd_ring = bsd_ring;
4485 ret = intel_init_ring_buffer(dev, &dev_priv->bsd_ring);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004486 if (ret)
4487 goto cleanup_render_ring;
Zou Nan haid1b851f2010-05-21 09:08:57 +08004488 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004489
Chris Wilson6f392d5482010-08-07 11:01:22 +01004490 dev_priv->next_seqno = 1;
4491
Chris Wilson68f95ba2010-05-27 13:18:22 +01004492 return 0;
4493
4494cleanup_render_ring:
4495 intel_cleanup_ring_buffer(dev, &dev_priv->render_ring);
4496cleanup_pipe_control:
4497 if (HAS_PIPE_CONTROL(dev))
4498 i915_gem_cleanup_pipe_control(dev);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004499 return ret;
4500}
4501
4502void
4503i915_gem_cleanup_ringbuffer(struct drm_device *dev)
4504{
4505 drm_i915_private_t *dev_priv = dev->dev_private;
4506
4507 intel_cleanup_ring_buffer(dev, &dev_priv->render_ring);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004508 if (HAS_BSD(dev))
4509 intel_cleanup_ring_buffer(dev, &dev_priv->bsd_ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004510 if (HAS_PIPE_CONTROL(dev))
4511 i915_gem_cleanup_pipe_control(dev);
4512}
4513
4514int
Eric Anholt673a3942008-07-30 12:06:12 -07004515i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
4516 struct drm_file *file_priv)
4517{
4518 drm_i915_private_t *dev_priv = dev->dev_private;
4519 int ret;
4520
Jesse Barnes79e53942008-11-07 14:24:08 -08004521 if (drm_core_check_feature(dev, DRIVER_MODESET))
4522 return 0;
4523
Ben Gamariba1234d2009-09-14 17:48:47 -04004524 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07004525 DRM_ERROR("Reenabling wedged hardware, good luck\n");
Ben Gamariba1234d2009-09-14 17:48:47 -04004526 atomic_set(&dev_priv->mm.wedged, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004527 }
4528
Eric Anholt673a3942008-07-30 12:06:12 -07004529 mutex_lock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004530 dev_priv->mm.suspended = 0;
4531
4532 ret = i915_gem_init_ringbuffer(dev);
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004533 if (ret != 0) {
4534 mutex_unlock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004535 return ret;
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004536 }
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004537
Carl Worth5e118f42009-03-20 11:54:25 -07004538 spin_lock(&dev_priv->mm.active_list_lock);
Zou Nan hai852835f2010-05-21 09:08:56 +08004539 BUG_ON(!list_empty(&dev_priv->render_ring.active_list));
Zou Nan haid1b851f2010-05-21 09:08:57 +08004540 BUG_ON(HAS_BSD(dev) && !list_empty(&dev_priv->bsd_ring.active_list));
Carl Worth5e118f42009-03-20 11:54:25 -07004541 spin_unlock(&dev_priv->mm.active_list_lock);
4542
Eric Anholt673a3942008-07-30 12:06:12 -07004543 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
4544 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
Zou Nan hai852835f2010-05-21 09:08:56 +08004545 BUG_ON(!list_empty(&dev_priv->render_ring.request_list));
Zou Nan haid1b851f2010-05-21 09:08:57 +08004546 BUG_ON(HAS_BSD(dev) && !list_empty(&dev_priv->bsd_ring.request_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004547 mutex_unlock(&dev->struct_mutex);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004548
Chris Wilson5f353082010-06-07 14:03:03 +01004549 ret = drm_irq_install(dev);
4550 if (ret)
4551 goto cleanup_ringbuffer;
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004552
Eric Anholt673a3942008-07-30 12:06:12 -07004553 return 0;
Chris Wilson5f353082010-06-07 14:03:03 +01004554
4555cleanup_ringbuffer:
4556 mutex_lock(&dev->struct_mutex);
4557 i915_gem_cleanup_ringbuffer(dev);
4558 dev_priv->mm.suspended = 1;
4559 mutex_unlock(&dev->struct_mutex);
4560
4561 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004562}
4563
4564int
4565i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
4566 struct drm_file *file_priv)
4567{
Jesse Barnes79e53942008-11-07 14:24:08 -08004568 if (drm_core_check_feature(dev, DRIVER_MODESET))
4569 return 0;
4570
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004571 drm_irq_uninstall(dev);
Linus Torvaldse6890f62009-09-08 17:09:24 -07004572 return i915_gem_idle(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004573}
4574
4575void
4576i915_gem_lastclose(struct drm_device *dev)
4577{
4578 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004579
Eric Anholte806b492009-01-22 09:56:58 -08004580 if (drm_core_check_feature(dev, DRIVER_MODESET))
4581 return;
4582
Keith Packard6dbe2772008-10-14 21:41:13 -07004583 ret = i915_gem_idle(dev);
4584 if (ret)
4585 DRM_ERROR("failed to idle hardware: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07004586}
4587
4588void
4589i915_gem_load(struct drm_device *dev)
4590{
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004591 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07004592 drm_i915_private_t *dev_priv = dev->dev_private;
4593
Carl Worth5e118f42009-03-20 11:54:25 -07004594 spin_lock_init(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07004595 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
Daniel Vetter99fcb762010-02-07 16:20:18 +01004596 INIT_LIST_HEAD(&dev_priv->mm.gpu_write_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004597 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07004598 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Chris Wilsonbe726152010-07-23 23:18:50 +01004599 INIT_LIST_HEAD(&dev_priv->mm.deferred_free_list);
Zou Nan hai852835f2010-05-21 09:08:56 +08004600 INIT_LIST_HEAD(&dev_priv->render_ring.active_list);
4601 INIT_LIST_HEAD(&dev_priv->render_ring.request_list);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004602 if (HAS_BSD(dev)) {
4603 INIT_LIST_HEAD(&dev_priv->bsd_ring.active_list);
4604 INIT_LIST_HEAD(&dev_priv->bsd_ring.request_list);
4605 }
Daniel Vetter007cc8a2010-04-28 11:02:31 +02004606 for (i = 0; i < 16; i++)
4607 INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004608 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
4609 i915_gem_retire_work_handler);
Chris Wilson31169712009-09-14 16:50:28 +01004610 spin_lock(&shrink_list_lock);
4611 list_add(&dev_priv->mm.shrink_list, &shrink_list);
4612 spin_unlock(&shrink_list_lock);
4613
Dave Airlie94400122010-07-20 13:15:31 +10004614 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
4615 if (IS_GEN3(dev)) {
4616 u32 tmp = I915_READ(MI_ARB_STATE);
4617 if (!(tmp & MI_ARB_C3_LP_WRITE_ENABLE)) {
4618 /* arb state is a masked write, so set bit + bit in mask */
4619 tmp = MI_ARB_C3_LP_WRITE_ENABLE | (MI_ARB_C3_LP_WRITE_ENABLE << MI_ARB_MASK_SHIFT);
4620 I915_WRITE(MI_ARB_STATE, tmp);
4621 }
4622 }
4623
Jesse Barnesde151cf2008-11-12 10:03:55 -08004624 /* Old X drivers will take 0-2 for front, back, depth buffers */
Eric Anholtb397c832010-01-26 09:43:10 -08004625 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4626 dev_priv->fence_reg_start = 3;
Jesse Barnesde151cf2008-11-12 10:03:55 -08004627
Jesse Barnes0f973f22009-01-26 17:10:45 -08004628 if (IS_I965G(dev) || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08004629 dev_priv->num_fence_regs = 16;
4630 else
4631 dev_priv->num_fence_regs = 8;
4632
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004633 /* Initialize fence registers to zero */
4634 if (IS_I965G(dev)) {
4635 for (i = 0; i < 16; i++)
4636 I915_WRITE64(FENCE_REG_965_0 + (i * 8), 0);
4637 } else {
4638 for (i = 0; i < 8; i++)
4639 I915_WRITE(FENCE_REG_830_0 + (i * 4), 0);
4640 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4641 for (i = 0; i < 8; i++)
4642 I915_WRITE(FENCE_REG_945_8 + (i * 4), 0);
4643 }
Eric Anholt673a3942008-07-30 12:06:12 -07004644 i915_gem_detect_bit_6_swizzle(dev);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05004645 init_waitqueue_head(&dev_priv->pending_flip_queue);
Eric Anholt673a3942008-07-30 12:06:12 -07004646}
Dave Airlie71acb5e2008-12-30 20:31:46 +10004647
4648/*
4649 * Create a physically contiguous memory object for this object
4650 * e.g. for cursor + overlay regs
4651 */
4652int i915_gem_init_phys_object(struct drm_device *dev,
4653 int id, int size)
4654{
4655 drm_i915_private_t *dev_priv = dev->dev_private;
4656 struct drm_i915_gem_phys_object *phys_obj;
4657 int ret;
4658
4659 if (dev_priv->mm.phys_objs[id - 1] || !size)
4660 return 0;
4661
Eric Anholt9a298b22009-03-24 12:23:04 -07004662 phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004663 if (!phys_obj)
4664 return -ENOMEM;
4665
4666 phys_obj->id = id;
4667
Zhenyu Wange6be8d92010-01-05 11:25:05 +08004668 phys_obj->handle = drm_pci_alloc(dev, size, 0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004669 if (!phys_obj->handle) {
4670 ret = -ENOMEM;
4671 goto kfree_obj;
4672 }
4673#ifdef CONFIG_X86
4674 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4675#endif
4676
4677 dev_priv->mm.phys_objs[id - 1] = phys_obj;
4678
4679 return 0;
4680kfree_obj:
Eric Anholt9a298b22009-03-24 12:23:04 -07004681 kfree(phys_obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004682 return ret;
4683}
4684
4685void i915_gem_free_phys_object(struct drm_device *dev, int id)
4686{
4687 drm_i915_private_t *dev_priv = dev->dev_private;
4688 struct drm_i915_gem_phys_object *phys_obj;
4689
4690 if (!dev_priv->mm.phys_objs[id - 1])
4691 return;
4692
4693 phys_obj = dev_priv->mm.phys_objs[id - 1];
4694 if (phys_obj->cur_obj) {
4695 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
4696 }
4697
4698#ifdef CONFIG_X86
4699 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4700#endif
4701 drm_pci_free(dev, phys_obj->handle);
4702 kfree(phys_obj);
4703 dev_priv->mm.phys_objs[id - 1] = NULL;
4704}
4705
4706void i915_gem_free_all_phys_object(struct drm_device *dev)
4707{
4708 int i;
4709
Dave Airlie260883c2009-01-22 17:58:49 +10004710 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004711 i915_gem_free_phys_object(dev, i);
4712}
4713
4714void i915_gem_detach_phys_object(struct drm_device *dev,
4715 struct drm_gem_object *obj)
4716{
4717 struct drm_i915_gem_object *obj_priv;
4718 int i;
4719 int ret;
4720 int page_count;
4721
Daniel Vetter23010e42010-03-08 13:35:02 +01004722 obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004723 if (!obj_priv->phys_obj)
4724 return;
4725
Chris Wilson4bdadb92010-01-27 13:36:32 +00004726 ret = i915_gem_object_get_pages(obj, 0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004727 if (ret)
4728 goto out;
4729
4730 page_count = obj->size / PAGE_SIZE;
4731
4732 for (i = 0; i < page_count; i++) {
Eric Anholt856fa192009-03-19 14:10:50 -07004733 char *dst = kmap_atomic(obj_priv->pages[i], KM_USER0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004734 char *src = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4735
4736 memcpy(dst, src, PAGE_SIZE);
4737 kunmap_atomic(dst, KM_USER0);
4738 }
Eric Anholt856fa192009-03-19 14:10:50 -07004739 drm_clflush_pages(obj_priv->pages, page_count);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004740 drm_agp_chipset_flush(dev);
Chris Wilsond78b47b2009-06-17 21:52:49 +01004741
4742 i915_gem_object_put_pages(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004743out:
4744 obj_priv->phys_obj->cur_obj = NULL;
4745 obj_priv->phys_obj = NULL;
4746}
4747
4748int
4749i915_gem_attach_phys_object(struct drm_device *dev,
4750 struct drm_gem_object *obj, int id)
4751{
4752 drm_i915_private_t *dev_priv = dev->dev_private;
4753 struct drm_i915_gem_object *obj_priv;
4754 int ret = 0;
4755 int page_count;
4756 int i;
4757
4758 if (id > I915_MAX_PHYS_OBJECT)
4759 return -EINVAL;
4760
Daniel Vetter23010e42010-03-08 13:35:02 +01004761 obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004762
4763 if (obj_priv->phys_obj) {
4764 if (obj_priv->phys_obj->id == id)
4765 return 0;
4766 i915_gem_detach_phys_object(dev, obj);
4767 }
4768
4769
4770 /* create a new object */
4771 if (!dev_priv->mm.phys_objs[id - 1]) {
4772 ret = i915_gem_init_phys_object(dev, id,
4773 obj->size);
4774 if (ret) {
Linus Torvaldsaeb565d2009-01-26 10:01:53 -08004775 DRM_ERROR("failed to init phys object %d size: %zu\n", id, obj->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004776 goto out;
4777 }
4778 }
4779
4780 /* bind to the object */
4781 obj_priv->phys_obj = dev_priv->mm.phys_objs[id - 1];
4782 obj_priv->phys_obj->cur_obj = obj;
4783
Chris Wilson4bdadb92010-01-27 13:36:32 +00004784 ret = i915_gem_object_get_pages(obj, 0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004785 if (ret) {
4786 DRM_ERROR("failed to get page list\n");
4787 goto out;
4788 }
4789
4790 page_count = obj->size / PAGE_SIZE;
4791
4792 for (i = 0; i < page_count; i++) {
Eric Anholt856fa192009-03-19 14:10:50 -07004793 char *src = kmap_atomic(obj_priv->pages[i], KM_USER0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004794 char *dst = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4795
4796 memcpy(dst, src, PAGE_SIZE);
4797 kunmap_atomic(src, KM_USER0);
4798 }
4799
Chris Wilsond78b47b2009-06-17 21:52:49 +01004800 i915_gem_object_put_pages(obj);
4801
Dave Airlie71acb5e2008-12-30 20:31:46 +10004802 return 0;
4803out:
4804 return ret;
4805}
4806
4807static int
4808i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
4809 struct drm_i915_gem_pwrite *args,
4810 struct drm_file *file_priv)
4811{
Daniel Vetter23010e42010-03-08 13:35:02 +01004812 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004813 void *obj_addr;
4814 int ret;
4815 char __user *user_data;
4816
4817 user_data = (char __user *) (uintptr_t) args->data_ptr;
4818 obj_addr = obj_priv->phys_obj->handle->vaddr + args->offset;
4819
Zhao Yakui44d98a62009-10-09 11:39:40 +08004820 DRM_DEBUG_DRIVER("obj_addr %p, %lld\n", obj_addr, args->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004821 ret = copy_from_user(obj_addr, user_data, args->size);
4822 if (ret)
4823 return -EFAULT;
4824
4825 drm_agp_chipset_flush(dev);
4826 return 0;
4827}
Eric Anholtb9624422009-06-03 07:27:35 +00004828
4829void i915_gem_release(struct drm_device * dev, struct drm_file *file_priv)
4830{
4831 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
4832
4833 /* Clean up our request list when the client is going away, so that
4834 * later retire_requests won't dereference our soon-to-be-gone
4835 * file_priv.
4836 */
4837 mutex_lock(&dev->struct_mutex);
4838 while (!list_empty(&i915_file_priv->mm.request_list))
4839 list_del_init(i915_file_priv->mm.request_list.next);
4840 mutex_unlock(&dev->struct_mutex);
4841}
Chris Wilson31169712009-09-14 16:50:28 +01004842
Chris Wilson31169712009-09-14 16:50:28 +01004843static int
Chris Wilson1637ef42010-04-20 17:10:35 +01004844i915_gpu_is_active(struct drm_device *dev)
4845{
4846 drm_i915_private_t *dev_priv = dev->dev_private;
4847 int lists_empty;
4848
4849 spin_lock(&dev_priv->mm.active_list_lock);
4850 lists_empty = list_empty(&dev_priv->mm.flushing_list) &&
Zou Nan hai852835f2010-05-21 09:08:56 +08004851 list_empty(&dev_priv->render_ring.active_list);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004852 if (HAS_BSD(dev))
4853 lists_empty &= list_empty(&dev_priv->bsd_ring.active_list);
Chris Wilson1637ef42010-04-20 17:10:35 +01004854 spin_unlock(&dev_priv->mm.active_list_lock);
4855
4856 return !lists_empty;
4857}
4858
4859static int
Dave Chinner7f8275d2010-07-19 14:56:17 +10004860i915_gem_shrink(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask)
Chris Wilson31169712009-09-14 16:50:28 +01004861{
4862 drm_i915_private_t *dev_priv, *next_dev;
4863 struct drm_i915_gem_object *obj_priv, *next_obj;
4864 int cnt = 0;
4865 int would_deadlock = 1;
4866
4867 /* "fast-path" to count number of available objects */
4868 if (nr_to_scan == 0) {
4869 spin_lock(&shrink_list_lock);
4870 list_for_each_entry(dev_priv, &shrink_list, mm.shrink_list) {
4871 struct drm_device *dev = dev_priv->dev;
4872
4873 if (mutex_trylock(&dev->struct_mutex)) {
4874 list_for_each_entry(obj_priv,
4875 &dev_priv->mm.inactive_list,
4876 list)
4877 cnt++;
4878 mutex_unlock(&dev->struct_mutex);
4879 }
4880 }
4881 spin_unlock(&shrink_list_lock);
4882
4883 return (cnt / 100) * sysctl_vfs_cache_pressure;
4884 }
4885
4886 spin_lock(&shrink_list_lock);
4887
Chris Wilson1637ef42010-04-20 17:10:35 +01004888rescan:
Chris Wilson31169712009-09-14 16:50:28 +01004889 /* first scan for clean buffers */
4890 list_for_each_entry_safe(dev_priv, next_dev,
4891 &shrink_list, mm.shrink_list) {
4892 struct drm_device *dev = dev_priv->dev;
4893
4894 if (! mutex_trylock(&dev->struct_mutex))
4895 continue;
4896
4897 spin_unlock(&shrink_list_lock);
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01004898 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004899
Chris Wilson31169712009-09-14 16:50:28 +01004900 list_for_each_entry_safe(obj_priv, next_obj,
4901 &dev_priv->mm.inactive_list,
4902 list) {
4903 if (i915_gem_object_is_purgeable(obj_priv)) {
Daniel Vettera8089e82010-04-09 19:05:09 +00004904 i915_gem_object_unbind(&obj_priv->base);
Chris Wilson31169712009-09-14 16:50:28 +01004905 if (--nr_to_scan <= 0)
4906 break;
4907 }
4908 }
4909
4910 spin_lock(&shrink_list_lock);
4911 mutex_unlock(&dev->struct_mutex);
4912
Chris Wilson963b4832009-09-20 23:03:54 +01004913 would_deadlock = 0;
4914
Chris Wilson31169712009-09-14 16:50:28 +01004915 if (nr_to_scan <= 0)
4916 break;
4917 }
4918
4919 /* second pass, evict/count anything still on the inactive list */
4920 list_for_each_entry_safe(dev_priv, next_dev,
4921 &shrink_list, mm.shrink_list) {
4922 struct drm_device *dev = dev_priv->dev;
4923
4924 if (! mutex_trylock(&dev->struct_mutex))
4925 continue;
4926
4927 spin_unlock(&shrink_list_lock);
4928
4929 list_for_each_entry_safe(obj_priv, next_obj,
4930 &dev_priv->mm.inactive_list,
4931 list) {
4932 if (nr_to_scan > 0) {
Daniel Vettera8089e82010-04-09 19:05:09 +00004933 i915_gem_object_unbind(&obj_priv->base);
Chris Wilson31169712009-09-14 16:50:28 +01004934 nr_to_scan--;
4935 } else
4936 cnt++;
4937 }
4938
4939 spin_lock(&shrink_list_lock);
4940 mutex_unlock(&dev->struct_mutex);
4941
4942 would_deadlock = 0;
4943 }
4944
Chris Wilson1637ef42010-04-20 17:10:35 +01004945 if (nr_to_scan) {
4946 int active = 0;
4947
4948 /*
4949 * We are desperate for pages, so as a last resort, wait
4950 * for the GPU to finish and discard whatever we can.
4951 * This has a dramatic impact to reduce the number of
4952 * OOM-killer events whilst running the GPU aggressively.
4953 */
4954 list_for_each_entry(dev_priv, &shrink_list, mm.shrink_list) {
4955 struct drm_device *dev = dev_priv->dev;
4956
4957 if (!mutex_trylock(&dev->struct_mutex))
4958 continue;
4959
4960 spin_unlock(&shrink_list_lock);
4961
4962 if (i915_gpu_is_active(dev)) {
4963 i915_gpu_idle(dev);
4964 active++;
4965 }
4966
4967 spin_lock(&shrink_list_lock);
4968 mutex_unlock(&dev->struct_mutex);
4969 }
4970
4971 if (active)
4972 goto rescan;
4973 }
4974
Chris Wilson31169712009-09-14 16:50:28 +01004975 spin_unlock(&shrink_list_lock);
4976
4977 if (would_deadlock)
4978 return -1;
4979 else if (cnt > 0)
4980 return (cnt / 100) * sysctl_vfs_cache_pressure;
4981 else
4982 return 0;
4983}
4984
4985static struct shrinker shrinker = {
4986 .shrink = i915_gem_shrink,
4987 .seeks = DEFAULT_SEEKS,
4988};
4989
4990__init void
4991i915_gem_shrinker_init(void)
4992{
4993 register_shrinker(&shrinker);
4994}
4995
4996__exit void
4997i915_gem_shrinker_exit(void)
4998{
4999 unregister_shrinker(&shrinker);
5000}