blob: f84c8e982dcb398c810eb915bcd5c352686419a0 [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
Eric Anholte47c68e2008-11-14 13:35:19 -080038static void i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj);
39static void i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj);
40static void i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj);
Eric Anholte47c68e2008-11-14 13:35:19 -080041static int i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj,
42 int write);
43static int i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
44 uint64_t offset,
45 uint64_t size);
46static void i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj);
Eric Anholt673a3942008-07-30 12:06:12 -070047static int i915_gem_object_wait_rendering(struct drm_gem_object *obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -080048static int i915_gem_object_bind_to_gtt(struct drm_gem_object *obj,
49 unsigned alignment);
Jesse Barnesde151cf2008-11-12 10:03:55 -080050static void i915_gem_clear_fence_reg(struct drm_gem_object *obj);
Chris Wilson07f73f62009-09-14 16:50:30 +010051static int i915_gem_evict_something(struct drm_device *dev, int min_size);
Chris Wilsonab5ee572009-09-20 19:25:47 +010052static int i915_gem_evict_from_inactive_list(struct drm_device *dev);
Dave Airlie71acb5e2008-12-30 20:31:46 +100053static int i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
54 struct drm_i915_gem_pwrite *args,
55 struct drm_file *file_priv);
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);
Luca Barbieribc9025b2010-02-09 05:49:12 +0000130 drm_gem_object_handle_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700131
132 if (ret)
133 return ret;
134
135 args->handle = handle;
136
137 return 0;
138}
139
Eric Anholt40123c12009-03-09 13:42:30 -0700140static inline int
Eric Anholteb014592009-03-10 11:44:52 -0700141fast_shmem_read(struct page **pages,
142 loff_t page_base, int page_offset,
143 char __user *data,
144 int length)
145{
146 char __iomem *vaddr;
Florian Mickler2bc43b52009-04-06 22:55:41 +0200147 int unwritten;
Eric Anholteb014592009-03-10 11:44:52 -0700148
149 vaddr = kmap_atomic(pages[page_base >> PAGE_SHIFT], KM_USER0);
150 if (vaddr == NULL)
151 return -ENOMEM;
Florian Mickler2bc43b52009-04-06 22:55:41 +0200152 unwritten = __copy_to_user_inatomic(data, vaddr + page_offset, length);
Eric Anholteb014592009-03-10 11:44:52 -0700153 kunmap_atomic(vaddr, KM_USER0);
154
Florian Mickler2bc43b52009-04-06 22:55:41 +0200155 if (unwritten)
156 return -EFAULT;
157
158 return 0;
Eric Anholteb014592009-03-10 11:44:52 -0700159}
160
Eric Anholt280b7132009-03-12 16:56:27 -0700161static int i915_gem_object_needs_bit17_swizzle(struct drm_gem_object *obj)
162{
163 drm_i915_private_t *dev_priv = obj->dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +0100164 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt280b7132009-03-12 16:56:27 -0700165
166 return dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_9_10_17 &&
167 obj_priv->tiling_mode != I915_TILING_NONE;
168}
169
Eric Anholteb014592009-03-10 11:44:52 -0700170static inline int
Eric Anholt40123c12009-03-09 13:42:30 -0700171slow_shmem_copy(struct page *dst_page,
172 int dst_offset,
173 struct page *src_page,
174 int src_offset,
175 int length)
176{
177 char *dst_vaddr, *src_vaddr;
178
179 dst_vaddr = kmap_atomic(dst_page, KM_USER0);
180 if (dst_vaddr == NULL)
181 return -ENOMEM;
182
183 src_vaddr = kmap_atomic(src_page, KM_USER1);
184 if (src_vaddr == NULL) {
185 kunmap_atomic(dst_vaddr, KM_USER0);
186 return -ENOMEM;
187 }
188
189 memcpy(dst_vaddr + dst_offset, src_vaddr + src_offset, length);
190
191 kunmap_atomic(src_vaddr, KM_USER1);
192 kunmap_atomic(dst_vaddr, KM_USER0);
193
194 return 0;
195}
196
Eric Anholt280b7132009-03-12 16:56:27 -0700197static inline int
198slow_shmem_bit17_copy(struct page *gpu_page,
199 int gpu_offset,
200 struct page *cpu_page,
201 int cpu_offset,
202 int length,
203 int is_read)
204{
205 char *gpu_vaddr, *cpu_vaddr;
206
207 /* Use the unswizzled path if this page isn't affected. */
208 if ((page_to_phys(gpu_page) & (1 << 17)) == 0) {
209 if (is_read)
210 return slow_shmem_copy(cpu_page, cpu_offset,
211 gpu_page, gpu_offset, length);
212 else
213 return slow_shmem_copy(gpu_page, gpu_offset,
214 cpu_page, cpu_offset, length);
215 }
216
217 gpu_vaddr = kmap_atomic(gpu_page, KM_USER0);
218 if (gpu_vaddr == NULL)
219 return -ENOMEM;
220
221 cpu_vaddr = kmap_atomic(cpu_page, KM_USER1);
222 if (cpu_vaddr == NULL) {
223 kunmap_atomic(gpu_vaddr, KM_USER0);
224 return -ENOMEM;
225 }
226
227 /* Copy the data, XORing A6 with A17 (1). The user already knows he's
228 * XORing with the other bits (A9 for Y, A9 and A10 for X)
229 */
230 while (length > 0) {
231 int cacheline_end = ALIGN(gpu_offset + 1, 64);
232 int this_length = min(cacheline_end - gpu_offset, length);
233 int swizzled_gpu_offset = gpu_offset ^ 64;
234
235 if (is_read) {
236 memcpy(cpu_vaddr + cpu_offset,
237 gpu_vaddr + swizzled_gpu_offset,
238 this_length);
239 } else {
240 memcpy(gpu_vaddr + swizzled_gpu_offset,
241 cpu_vaddr + cpu_offset,
242 this_length);
243 }
244 cpu_offset += this_length;
245 gpu_offset += this_length;
246 length -= this_length;
247 }
248
249 kunmap_atomic(cpu_vaddr, KM_USER1);
250 kunmap_atomic(gpu_vaddr, KM_USER0);
251
252 return 0;
253}
254
Eric Anholt673a3942008-07-30 12:06:12 -0700255/**
Eric Anholteb014592009-03-10 11:44:52 -0700256 * This is the fast shmem pread path, which attempts to copy_from_user directly
257 * from the backing pages of the object to the user's address space. On a
258 * fault, it fails so we can fall back to i915_gem_shmem_pwrite_slow().
259 */
260static int
261i915_gem_shmem_pread_fast(struct drm_device *dev, struct drm_gem_object *obj,
262 struct drm_i915_gem_pread *args,
263 struct drm_file *file_priv)
264{
Daniel Vetter23010e42010-03-08 13:35:02 +0100265 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700266 ssize_t remain;
267 loff_t offset, page_base;
268 char __user *user_data;
269 int page_offset, page_length;
270 int ret;
271
272 user_data = (char __user *) (uintptr_t) args->data_ptr;
273 remain = args->size;
274
275 mutex_lock(&dev->struct_mutex);
276
Chris Wilson4bdadb92010-01-27 13:36:32 +0000277 ret = i915_gem_object_get_pages(obj, 0);
Eric Anholteb014592009-03-10 11:44:52 -0700278 if (ret != 0)
279 goto fail_unlock;
280
281 ret = i915_gem_object_set_cpu_read_domain_range(obj, args->offset,
282 args->size);
283 if (ret != 0)
284 goto fail_put_pages;
285
Daniel Vetter23010e42010-03-08 13:35:02 +0100286 obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700287 offset = args->offset;
288
289 while (remain > 0) {
290 /* Operation in this page
291 *
292 * page_base = page offset within aperture
293 * page_offset = offset within page
294 * page_length = bytes to copy for this page
295 */
296 page_base = (offset & ~(PAGE_SIZE-1));
297 page_offset = offset & (PAGE_SIZE-1);
298 page_length = remain;
299 if ((page_offset + remain) > PAGE_SIZE)
300 page_length = PAGE_SIZE - page_offset;
301
302 ret = fast_shmem_read(obj_priv->pages,
303 page_base, page_offset,
304 user_data, page_length);
305 if (ret)
306 goto fail_put_pages;
307
308 remain -= page_length;
309 user_data += page_length;
310 offset += page_length;
311 }
312
313fail_put_pages:
314 i915_gem_object_put_pages(obj);
315fail_unlock:
316 mutex_unlock(&dev->struct_mutex);
317
318 return ret;
319}
320
Chris Wilson07f73f62009-09-14 16:50:30 +0100321static int
322i915_gem_object_get_pages_or_evict(struct drm_gem_object *obj)
323{
324 int ret;
325
Chris Wilson4bdadb92010-01-27 13:36:32 +0000326 ret = i915_gem_object_get_pages(obj, __GFP_NORETRY | __GFP_NOWARN);
Chris Wilson07f73f62009-09-14 16:50:30 +0100327
328 /* If we've insufficient memory to map in the pages, attempt
329 * to make some space by throwing out some old buffers.
330 */
331 if (ret == -ENOMEM) {
332 struct drm_device *dev = obj->dev;
Chris Wilson07f73f62009-09-14 16:50:30 +0100333
334 ret = i915_gem_evict_something(dev, obj->size);
335 if (ret)
336 return ret;
337
Chris Wilson4bdadb92010-01-27 13:36:32 +0000338 ret = i915_gem_object_get_pages(obj, 0);
Chris Wilson07f73f62009-09-14 16:50:30 +0100339 }
340
341 return ret;
342}
343
Eric Anholteb014592009-03-10 11:44:52 -0700344/**
345 * This is the fallback shmem pread path, which allocates temporary storage
346 * in kernel space to copy_to_user into outside of the struct_mutex, so we
347 * can copy out of the object's backing pages while holding the struct mutex
348 * and not take page faults.
349 */
350static int
351i915_gem_shmem_pread_slow(struct drm_device *dev, struct drm_gem_object *obj,
352 struct drm_i915_gem_pread *args,
353 struct drm_file *file_priv)
354{
Daniel Vetter23010e42010-03-08 13:35:02 +0100355 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700356 struct mm_struct *mm = current->mm;
357 struct page **user_pages;
358 ssize_t remain;
359 loff_t offset, pinned_pages, i;
360 loff_t first_data_page, last_data_page, num_pages;
361 int shmem_page_index, shmem_page_offset;
362 int data_page_index, data_page_offset;
363 int page_length;
364 int ret;
365 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700366 int do_bit17_swizzling;
Eric Anholteb014592009-03-10 11:44:52 -0700367
368 remain = args->size;
369
370 /* Pin the user pages containing the data. We can't fault while
371 * holding the struct mutex, yet we want to hold it while
372 * dereferencing the user data.
373 */
374 first_data_page = data_ptr / PAGE_SIZE;
375 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
376 num_pages = last_data_page - first_data_page + 1;
377
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700378 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
Eric Anholteb014592009-03-10 11:44:52 -0700379 if (user_pages == NULL)
380 return -ENOMEM;
381
382 down_read(&mm->mmap_sem);
383 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
Eric Anholte5e9ecd2009-04-07 16:01:22 -0700384 num_pages, 1, 0, user_pages, NULL);
Eric Anholteb014592009-03-10 11:44:52 -0700385 up_read(&mm->mmap_sem);
386 if (pinned_pages < num_pages) {
387 ret = -EFAULT;
388 goto fail_put_user_pages;
389 }
390
Eric Anholt280b7132009-03-12 16:56:27 -0700391 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
392
Eric Anholteb014592009-03-10 11:44:52 -0700393 mutex_lock(&dev->struct_mutex);
394
Chris Wilson07f73f62009-09-14 16:50:30 +0100395 ret = i915_gem_object_get_pages_or_evict(obj);
396 if (ret)
Eric Anholteb014592009-03-10 11:44:52 -0700397 goto fail_unlock;
398
399 ret = i915_gem_object_set_cpu_read_domain_range(obj, args->offset,
400 args->size);
401 if (ret != 0)
402 goto fail_put_pages;
403
Daniel Vetter23010e42010-03-08 13:35:02 +0100404 obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700405 offset = args->offset;
406
407 while (remain > 0) {
408 /* Operation in this page
409 *
410 * shmem_page_index = page number within shmem file
411 * shmem_page_offset = offset within page in shmem file
412 * data_page_index = page number in get_user_pages return
413 * data_page_offset = offset with data_page_index page.
414 * page_length = bytes to copy for this page
415 */
416 shmem_page_index = offset / PAGE_SIZE;
417 shmem_page_offset = offset & ~PAGE_MASK;
418 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
419 data_page_offset = data_ptr & ~PAGE_MASK;
420
421 page_length = remain;
422 if ((shmem_page_offset + page_length) > PAGE_SIZE)
423 page_length = PAGE_SIZE - shmem_page_offset;
424 if ((data_page_offset + page_length) > PAGE_SIZE)
425 page_length = PAGE_SIZE - data_page_offset;
426
Eric Anholt280b7132009-03-12 16:56:27 -0700427 if (do_bit17_swizzling) {
428 ret = slow_shmem_bit17_copy(obj_priv->pages[shmem_page_index],
429 shmem_page_offset,
430 user_pages[data_page_index],
431 data_page_offset,
432 page_length,
433 1);
434 } else {
435 ret = slow_shmem_copy(user_pages[data_page_index],
436 data_page_offset,
437 obj_priv->pages[shmem_page_index],
438 shmem_page_offset,
439 page_length);
440 }
Eric Anholteb014592009-03-10 11:44:52 -0700441 if (ret)
442 goto fail_put_pages;
443
444 remain -= page_length;
445 data_ptr += page_length;
446 offset += page_length;
447 }
448
449fail_put_pages:
450 i915_gem_object_put_pages(obj);
451fail_unlock:
452 mutex_unlock(&dev->struct_mutex);
453fail_put_user_pages:
454 for (i = 0; i < pinned_pages; i++) {
455 SetPageDirty(user_pages[i]);
456 page_cache_release(user_pages[i]);
457 }
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700458 drm_free_large(user_pages);
Eric Anholteb014592009-03-10 11:44:52 -0700459
460 return ret;
461}
462
Eric Anholt673a3942008-07-30 12:06:12 -0700463/**
464 * Reads data from the object referenced by handle.
465 *
466 * On error, the contents of *data are undefined.
467 */
468int
469i915_gem_pread_ioctl(struct drm_device *dev, void *data,
470 struct drm_file *file_priv)
471{
472 struct drm_i915_gem_pread *args = data;
473 struct drm_gem_object *obj;
474 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -0700475 int ret;
476
477 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
478 if (obj == NULL)
479 return -EBADF;
Daniel Vetter23010e42010-03-08 13:35:02 +0100480 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700481
482 /* Bounds check source.
483 *
484 * XXX: This could use review for overflow issues...
485 */
486 if (args->offset > obj->size || args->size > obj->size ||
487 args->offset + args->size > obj->size) {
Luca Barbieribc9025b2010-02-09 05:49:12 +0000488 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700489 return -EINVAL;
490 }
491
Eric Anholt280b7132009-03-12 16:56:27 -0700492 if (i915_gem_object_needs_bit17_swizzle(obj)) {
Eric Anholteb014592009-03-10 11:44:52 -0700493 ret = i915_gem_shmem_pread_slow(dev, obj, args, file_priv);
Eric Anholt280b7132009-03-12 16:56:27 -0700494 } else {
495 ret = i915_gem_shmem_pread_fast(dev, obj, args, file_priv);
496 if (ret != 0)
497 ret = i915_gem_shmem_pread_slow(dev, obj, args,
498 file_priv);
499 }
Eric Anholt673a3942008-07-30 12:06:12 -0700500
Luca Barbieribc9025b2010-02-09 05:49:12 +0000501 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700502
Eric Anholteb014592009-03-10 11:44:52 -0700503 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700504}
505
Keith Packard0839ccb2008-10-30 19:38:48 -0700506/* This is the fast write path which cannot handle
507 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700508 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700509
Keith Packard0839ccb2008-10-30 19:38:48 -0700510static inline int
511fast_user_write(struct io_mapping *mapping,
512 loff_t page_base, int page_offset,
513 char __user *user_data,
514 int length)
515{
516 char *vaddr_atomic;
517 unsigned long unwritten;
518
519 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
520 unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
521 user_data, length);
522 io_mapping_unmap_atomic(vaddr_atomic);
523 if (unwritten)
524 return -EFAULT;
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700525 return 0;
Keith Packard0839ccb2008-10-30 19:38:48 -0700526}
527
528/* Here's the write path which can sleep for
529 * page faults
530 */
531
532static inline int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700533slow_kernel_write(struct io_mapping *mapping,
534 loff_t gtt_base, int gtt_offset,
535 struct page *user_page, int user_offset,
536 int length)
Keith Packard0839ccb2008-10-30 19:38:48 -0700537{
Eric Anholt3de09aa2009-03-09 09:42:23 -0700538 char *src_vaddr, *dst_vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -0700539 unsigned long unwritten;
540
Eric Anholt3de09aa2009-03-09 09:42:23 -0700541 dst_vaddr = io_mapping_map_atomic_wc(mapping, gtt_base);
542 src_vaddr = kmap_atomic(user_page, KM_USER1);
543 unwritten = __copy_from_user_inatomic_nocache(dst_vaddr + gtt_offset,
544 src_vaddr + user_offset,
545 length);
546 kunmap_atomic(src_vaddr, KM_USER1);
547 io_mapping_unmap_atomic(dst_vaddr);
Keith Packard0839ccb2008-10-30 19:38:48 -0700548 if (unwritten)
549 return -EFAULT;
550 return 0;
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700551}
552
Eric Anholt40123c12009-03-09 13:42:30 -0700553static inline int
554fast_shmem_write(struct page **pages,
555 loff_t page_base, int page_offset,
556 char __user *data,
557 int length)
558{
559 char __iomem *vaddr;
Dave Airlied0088772009-03-28 20:29:48 -0400560 unsigned long unwritten;
Eric Anholt40123c12009-03-09 13:42:30 -0700561
562 vaddr = kmap_atomic(pages[page_base >> PAGE_SHIFT], KM_USER0);
563 if (vaddr == NULL)
564 return -ENOMEM;
Dave Airlied0088772009-03-28 20:29:48 -0400565 unwritten = __copy_from_user_inatomic(vaddr + page_offset, data, length);
Eric Anholt40123c12009-03-09 13:42:30 -0700566 kunmap_atomic(vaddr, KM_USER0);
567
Dave Airlied0088772009-03-28 20:29:48 -0400568 if (unwritten)
569 return -EFAULT;
Eric Anholt40123c12009-03-09 13:42:30 -0700570 return 0;
571}
572
Eric Anholt3de09aa2009-03-09 09:42:23 -0700573/**
574 * This is the fast pwrite path, where we copy the data directly from the
575 * user into the GTT, uncached.
576 */
Eric Anholt673a3942008-07-30 12:06:12 -0700577static int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700578i915_gem_gtt_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
579 struct drm_i915_gem_pwrite *args,
580 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700581{
Daniel Vetter23010e42010-03-08 13:35:02 +0100582 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Keith Packard0839ccb2008-10-30 19:38:48 -0700583 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700584 ssize_t remain;
Keith Packard0839ccb2008-10-30 19:38:48 -0700585 loff_t offset, page_base;
Eric Anholt673a3942008-07-30 12:06:12 -0700586 char __user *user_data;
Keith Packard0839ccb2008-10-30 19:38:48 -0700587 int page_offset, page_length;
588 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700589
590 user_data = (char __user *) (uintptr_t) args->data_ptr;
591 remain = args->size;
592 if (!access_ok(VERIFY_READ, user_data, remain))
593 return -EFAULT;
594
595
596 mutex_lock(&dev->struct_mutex);
597 ret = i915_gem_object_pin(obj, 0);
598 if (ret) {
599 mutex_unlock(&dev->struct_mutex);
600 return ret;
601 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800602 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
Eric Anholt673a3942008-07-30 12:06:12 -0700603 if (ret)
604 goto fail;
605
Daniel Vetter23010e42010-03-08 13:35:02 +0100606 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700607 offset = obj_priv->gtt_offset + args->offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700608
609 while (remain > 0) {
610 /* Operation in this page
611 *
Keith Packard0839ccb2008-10-30 19:38:48 -0700612 * page_base = page offset within aperture
613 * page_offset = offset within page
614 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -0700615 */
Keith Packard0839ccb2008-10-30 19:38:48 -0700616 page_base = (offset & ~(PAGE_SIZE-1));
617 page_offset = offset & (PAGE_SIZE-1);
618 page_length = remain;
619 if ((page_offset + remain) > PAGE_SIZE)
620 page_length = PAGE_SIZE - page_offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700621
Keith Packard0839ccb2008-10-30 19:38:48 -0700622 ret = fast_user_write (dev_priv->mm.gtt_mapping, page_base,
623 page_offset, user_data, page_length);
Eric Anholt673a3942008-07-30 12:06:12 -0700624
Keith Packard0839ccb2008-10-30 19:38:48 -0700625 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -0700626 * source page isn't available. Return the error and we'll
627 * retry in the slow path.
Keith Packard0839ccb2008-10-30 19:38:48 -0700628 */
Eric Anholt3de09aa2009-03-09 09:42:23 -0700629 if (ret)
630 goto fail;
Eric Anholt673a3942008-07-30 12:06:12 -0700631
Keith Packard0839ccb2008-10-30 19:38:48 -0700632 remain -= page_length;
633 user_data += page_length;
634 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700635 }
Eric Anholt673a3942008-07-30 12:06:12 -0700636
637fail:
638 i915_gem_object_unpin(obj);
639 mutex_unlock(&dev->struct_mutex);
640
641 return ret;
642}
643
Eric Anholt3de09aa2009-03-09 09:42:23 -0700644/**
645 * This is the fallback GTT pwrite path, which uses get_user_pages to pin
646 * the memory and maps it using kmap_atomic for copying.
647 *
648 * This code resulted in x11perf -rgb10text consuming about 10% more CPU
649 * than using i915_gem_gtt_pwrite_fast on a G45 (32-bit).
650 */
Eric Anholt3043c602008-10-02 12:24:47 -0700651static int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700652i915_gem_gtt_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
653 struct drm_i915_gem_pwrite *args,
654 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700655{
Daniel Vetter23010e42010-03-08 13:35:02 +0100656 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700657 drm_i915_private_t *dev_priv = dev->dev_private;
658 ssize_t remain;
659 loff_t gtt_page_base, offset;
660 loff_t first_data_page, last_data_page, num_pages;
661 loff_t pinned_pages, i;
662 struct page **user_pages;
663 struct mm_struct *mm = current->mm;
664 int gtt_page_offset, data_page_offset, data_page_index, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700665 int ret;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700666 uint64_t data_ptr = args->data_ptr;
667
668 remain = args->size;
669
670 /* Pin the user pages containing the data. We can't fault while
671 * holding the struct mutex, and all of the pwrite implementations
672 * want to hold it while dereferencing the user data.
673 */
674 first_data_page = data_ptr / PAGE_SIZE;
675 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
676 num_pages = last_data_page - first_data_page + 1;
677
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700678 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
Eric Anholt3de09aa2009-03-09 09:42:23 -0700679 if (user_pages == NULL)
680 return -ENOMEM;
681
682 down_read(&mm->mmap_sem);
683 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
684 num_pages, 0, 0, user_pages, NULL);
685 up_read(&mm->mmap_sem);
686 if (pinned_pages < num_pages) {
687 ret = -EFAULT;
688 goto out_unpin_pages;
689 }
690
691 mutex_lock(&dev->struct_mutex);
692 ret = i915_gem_object_pin(obj, 0);
693 if (ret)
694 goto out_unlock;
695
696 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
697 if (ret)
698 goto out_unpin_object;
699
Daniel Vetter23010e42010-03-08 13:35:02 +0100700 obj_priv = to_intel_bo(obj);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700701 offset = obj_priv->gtt_offset + args->offset;
702
703 while (remain > 0) {
704 /* Operation in this page
705 *
706 * gtt_page_base = page offset within aperture
707 * gtt_page_offset = offset within page in aperture
708 * data_page_index = page number in get_user_pages return
709 * data_page_offset = offset with data_page_index page.
710 * page_length = bytes to copy for this page
711 */
712 gtt_page_base = offset & PAGE_MASK;
713 gtt_page_offset = offset & ~PAGE_MASK;
714 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
715 data_page_offset = data_ptr & ~PAGE_MASK;
716
717 page_length = remain;
718 if ((gtt_page_offset + page_length) > PAGE_SIZE)
719 page_length = PAGE_SIZE - gtt_page_offset;
720 if ((data_page_offset + page_length) > PAGE_SIZE)
721 page_length = PAGE_SIZE - data_page_offset;
722
723 ret = slow_kernel_write(dev_priv->mm.gtt_mapping,
724 gtt_page_base, gtt_page_offset,
725 user_pages[data_page_index],
726 data_page_offset,
727 page_length);
728
729 /* If we get a fault while copying data, then (presumably) our
730 * source page isn't available. Return the error and we'll
731 * retry in the slow path.
732 */
733 if (ret)
734 goto out_unpin_object;
735
736 remain -= page_length;
737 offset += page_length;
738 data_ptr += page_length;
739 }
740
741out_unpin_object:
742 i915_gem_object_unpin(obj);
743out_unlock:
744 mutex_unlock(&dev->struct_mutex);
745out_unpin_pages:
746 for (i = 0; i < pinned_pages; i++)
747 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700748 drm_free_large(user_pages);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700749
750 return ret;
751}
752
Eric Anholt40123c12009-03-09 13:42:30 -0700753/**
754 * This is the fast shmem pwrite path, which attempts to directly
755 * copy_from_user into the kmapped pages backing the object.
756 */
Eric Anholt673a3942008-07-30 12:06:12 -0700757static int
Eric Anholt40123c12009-03-09 13:42:30 -0700758i915_gem_shmem_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
759 struct drm_i915_gem_pwrite *args,
760 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700761{
Daniel Vetter23010e42010-03-08 13:35:02 +0100762 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700763 ssize_t remain;
764 loff_t offset, page_base;
765 char __user *user_data;
766 int page_offset, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700767 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -0700768
769 user_data = (char __user *) (uintptr_t) args->data_ptr;
770 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700771
772 mutex_lock(&dev->struct_mutex);
773
Chris Wilson4bdadb92010-01-27 13:36:32 +0000774 ret = i915_gem_object_get_pages(obj, 0);
Eric Anholt40123c12009-03-09 13:42:30 -0700775 if (ret != 0)
776 goto fail_unlock;
777
Eric Anholte47c68e2008-11-14 13:35:19 -0800778 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Eric Anholt40123c12009-03-09 13:42:30 -0700779 if (ret != 0)
780 goto fail_put_pages;
Eric Anholt673a3942008-07-30 12:06:12 -0700781
Daniel Vetter23010e42010-03-08 13:35:02 +0100782 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700783 offset = args->offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700784 obj_priv->dirty = 1;
Eric Anholt673a3942008-07-30 12:06:12 -0700785
Eric Anholt40123c12009-03-09 13:42:30 -0700786 while (remain > 0) {
787 /* Operation in this page
788 *
789 * page_base = page offset within aperture
790 * page_offset = offset within page
791 * page_length = bytes to copy for this page
792 */
793 page_base = (offset & ~(PAGE_SIZE-1));
794 page_offset = offset & (PAGE_SIZE-1);
795 page_length = remain;
796 if ((page_offset + remain) > PAGE_SIZE)
797 page_length = PAGE_SIZE - page_offset;
798
799 ret = fast_shmem_write(obj_priv->pages,
800 page_base, page_offset,
801 user_data, page_length);
802 if (ret)
803 goto fail_put_pages;
804
805 remain -= page_length;
806 user_data += page_length;
807 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700808 }
809
Eric Anholt40123c12009-03-09 13:42:30 -0700810fail_put_pages:
811 i915_gem_object_put_pages(obj);
812fail_unlock:
Eric Anholt673a3942008-07-30 12:06:12 -0700813 mutex_unlock(&dev->struct_mutex);
814
Eric Anholt40123c12009-03-09 13:42:30 -0700815 return ret;
816}
817
818/**
819 * This is the fallback shmem pwrite path, which uses get_user_pages to pin
820 * the memory and maps it using kmap_atomic for copying.
821 *
822 * This avoids taking mmap_sem for faulting on the user's address while the
823 * struct_mutex is held.
824 */
825static int
826i915_gem_shmem_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
827 struct drm_i915_gem_pwrite *args,
828 struct drm_file *file_priv)
829{
Daniel Vetter23010e42010-03-08 13:35:02 +0100830 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700831 struct mm_struct *mm = current->mm;
832 struct page **user_pages;
833 ssize_t remain;
834 loff_t offset, pinned_pages, i;
835 loff_t first_data_page, last_data_page, num_pages;
836 int shmem_page_index, shmem_page_offset;
837 int data_page_index, data_page_offset;
838 int page_length;
839 int ret;
840 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700841 int do_bit17_swizzling;
Eric Anholt40123c12009-03-09 13:42:30 -0700842
843 remain = args->size;
844
845 /* Pin the user pages containing the data. We can't fault while
846 * holding the struct mutex, and all of the pwrite implementations
847 * want to hold it while dereferencing the user data.
848 */
849 first_data_page = data_ptr / PAGE_SIZE;
850 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
851 num_pages = last_data_page - first_data_page + 1;
852
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700853 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
Eric Anholt40123c12009-03-09 13:42:30 -0700854 if (user_pages == NULL)
855 return -ENOMEM;
856
857 down_read(&mm->mmap_sem);
858 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
859 num_pages, 0, 0, user_pages, NULL);
860 up_read(&mm->mmap_sem);
861 if (pinned_pages < num_pages) {
862 ret = -EFAULT;
863 goto fail_put_user_pages;
864 }
865
Eric Anholt280b7132009-03-12 16:56:27 -0700866 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
867
Eric Anholt40123c12009-03-09 13:42:30 -0700868 mutex_lock(&dev->struct_mutex);
869
Chris Wilson07f73f62009-09-14 16:50:30 +0100870 ret = i915_gem_object_get_pages_or_evict(obj);
871 if (ret)
Eric Anholt40123c12009-03-09 13:42:30 -0700872 goto fail_unlock;
873
874 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
875 if (ret != 0)
876 goto fail_put_pages;
877
Daniel Vetter23010e42010-03-08 13:35:02 +0100878 obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700879 offset = args->offset;
880 obj_priv->dirty = 1;
881
882 while (remain > 0) {
883 /* Operation in this page
884 *
885 * shmem_page_index = page number within shmem file
886 * shmem_page_offset = offset within page in shmem file
887 * data_page_index = page number in get_user_pages return
888 * data_page_offset = offset with data_page_index page.
889 * page_length = bytes to copy for this page
890 */
891 shmem_page_index = offset / PAGE_SIZE;
892 shmem_page_offset = offset & ~PAGE_MASK;
893 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
894 data_page_offset = data_ptr & ~PAGE_MASK;
895
896 page_length = remain;
897 if ((shmem_page_offset + page_length) > PAGE_SIZE)
898 page_length = PAGE_SIZE - shmem_page_offset;
899 if ((data_page_offset + page_length) > PAGE_SIZE)
900 page_length = PAGE_SIZE - data_page_offset;
901
Eric Anholt280b7132009-03-12 16:56:27 -0700902 if (do_bit17_swizzling) {
903 ret = slow_shmem_bit17_copy(obj_priv->pages[shmem_page_index],
904 shmem_page_offset,
905 user_pages[data_page_index],
906 data_page_offset,
907 page_length,
908 0);
909 } else {
910 ret = slow_shmem_copy(obj_priv->pages[shmem_page_index],
911 shmem_page_offset,
912 user_pages[data_page_index],
913 data_page_offset,
914 page_length);
915 }
Eric Anholt40123c12009-03-09 13:42:30 -0700916 if (ret)
917 goto fail_put_pages;
918
919 remain -= page_length;
920 data_ptr += page_length;
921 offset += page_length;
922 }
923
924fail_put_pages:
925 i915_gem_object_put_pages(obj);
926fail_unlock:
927 mutex_unlock(&dev->struct_mutex);
928fail_put_user_pages:
929 for (i = 0; i < pinned_pages; i++)
930 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700931 drm_free_large(user_pages);
Eric Anholt40123c12009-03-09 13:42:30 -0700932
933 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700934}
935
936/**
937 * Writes data to the object referenced by handle.
938 *
939 * On error, the contents of the buffer that were to be modified are undefined.
940 */
941int
942i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
943 struct drm_file *file_priv)
944{
945 struct drm_i915_gem_pwrite *args = data;
946 struct drm_gem_object *obj;
947 struct drm_i915_gem_object *obj_priv;
948 int ret = 0;
949
950 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
951 if (obj == NULL)
952 return -EBADF;
Daniel Vetter23010e42010-03-08 13:35:02 +0100953 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700954
955 /* Bounds check destination.
956 *
957 * XXX: This could use review for overflow issues...
958 */
959 if (args->offset > obj->size || args->size > obj->size ||
960 args->offset + args->size > obj->size) {
Luca Barbieribc9025b2010-02-09 05:49:12 +0000961 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700962 return -EINVAL;
963 }
964
965 /* We can only do the GTT pwrite on untiled buffers, as otherwise
966 * it would end up going through the fenced access, and we'll get
967 * different detiling behavior between reading and writing.
968 * pread/pwrite currently are reading and writing from the CPU
969 * perspective, requiring manual detiling by the client.
970 */
Dave Airlie71acb5e2008-12-30 20:31:46 +1000971 if (obj_priv->phys_obj)
972 ret = i915_gem_phys_pwrite(dev, obj, args, file_priv);
973 else if (obj_priv->tiling_mode == I915_TILING_NONE &&
Eric Anholt3de09aa2009-03-09 09:42:23 -0700974 dev->gtt_total != 0) {
975 ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file_priv);
976 if (ret == -EFAULT) {
977 ret = i915_gem_gtt_pwrite_slow(dev, obj, args,
978 file_priv);
979 }
Eric Anholt280b7132009-03-12 16:56:27 -0700980 } else if (i915_gem_object_needs_bit17_swizzle(obj)) {
981 ret = i915_gem_shmem_pwrite_slow(dev, obj, args, file_priv);
Eric Anholt40123c12009-03-09 13:42:30 -0700982 } else {
983 ret = i915_gem_shmem_pwrite_fast(dev, obj, args, file_priv);
984 if (ret == -EFAULT) {
985 ret = i915_gem_shmem_pwrite_slow(dev, obj, args,
986 file_priv);
987 }
988 }
Eric Anholt673a3942008-07-30 12:06:12 -0700989
990#if WATCH_PWRITE
991 if (ret)
992 DRM_INFO("pwrite failed %d\n", ret);
993#endif
994
Luca Barbieribc9025b2010-02-09 05:49:12 +0000995 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700996
997 return ret;
998}
999
1000/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001001 * Called when user space prepares to use an object with the CPU, either
1002 * through the mmap ioctl's mapping or a GTT mapping.
Eric Anholt673a3942008-07-30 12:06:12 -07001003 */
1004int
1005i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
1006 struct drm_file *file_priv)
1007{
Eric Anholta09ba7f2009-08-29 12:49:51 -07001008 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07001009 struct drm_i915_gem_set_domain *args = data;
1010 struct drm_gem_object *obj;
Jesse Barnes652c3932009-08-17 13:31:43 -07001011 struct drm_i915_gem_object *obj_priv;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001012 uint32_t read_domains = args->read_domains;
1013 uint32_t write_domain = args->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07001014 int ret;
1015
1016 if (!(dev->driver->driver_features & DRIVER_GEM))
1017 return -ENODEV;
1018
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001019 /* Only handle setting domains to types used by the CPU. */
Chris Wilson21d509e2009-06-06 09:46:02 +01001020 if (write_domain & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001021 return -EINVAL;
1022
Chris Wilson21d509e2009-06-06 09:46:02 +01001023 if (read_domains & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001024 return -EINVAL;
1025
1026 /* Having something in the write domain implies it's in the read
1027 * domain, and only that read domain. Enforce that in the request.
1028 */
1029 if (write_domain != 0 && read_domains != write_domain)
1030 return -EINVAL;
1031
Eric Anholt673a3942008-07-30 12:06:12 -07001032 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1033 if (obj == NULL)
1034 return -EBADF;
Daniel Vetter23010e42010-03-08 13:35:02 +01001035 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001036
1037 mutex_lock(&dev->struct_mutex);
Jesse Barnes652c3932009-08-17 13:31:43 -07001038
1039 intel_mark_busy(dev, obj);
1040
Eric Anholt673a3942008-07-30 12:06:12 -07001041#if WATCH_BUF
Krzysztof Halasacfd43c02009-06-20 00:31:28 +02001042 DRM_INFO("set_domain_ioctl %p(%zd), %08x %08x\n",
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001043 obj, obj->size, read_domains, write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07001044#endif
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001045 if (read_domains & I915_GEM_DOMAIN_GTT) {
1046 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
Eric Anholt02354392008-11-26 13:58:13 -08001047
Eric Anholta09ba7f2009-08-29 12:49:51 -07001048 /* Update the LRU on the fence for the CPU access that's
1049 * about to occur.
1050 */
1051 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001052 struct drm_i915_fence_reg *reg =
1053 &dev_priv->fence_regs[obj_priv->fence_reg];
1054 list_move_tail(&reg->lru_list,
Eric Anholta09ba7f2009-08-29 12:49:51 -07001055 &dev_priv->mm.fence_list);
1056 }
1057
Eric Anholt02354392008-11-26 13:58:13 -08001058 /* Silently promote "you're not bound, there was nothing to do"
1059 * to success, since the client was just asking us to
1060 * make sure everything was done.
1061 */
1062 if (ret == -EINVAL)
1063 ret = 0;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001064 } else {
Eric Anholte47c68e2008-11-14 13:35:19 -08001065 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001066 }
1067
Eric Anholt673a3942008-07-30 12:06:12 -07001068 drm_gem_object_unreference(obj);
1069 mutex_unlock(&dev->struct_mutex);
1070 return ret;
1071}
1072
1073/**
1074 * Called when user space has done writes to this buffer
1075 */
1076int
1077i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
1078 struct drm_file *file_priv)
1079{
1080 struct drm_i915_gem_sw_finish *args = data;
1081 struct drm_gem_object *obj;
1082 struct drm_i915_gem_object *obj_priv;
1083 int ret = 0;
1084
1085 if (!(dev->driver->driver_features & DRIVER_GEM))
1086 return -ENODEV;
1087
1088 mutex_lock(&dev->struct_mutex);
1089 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1090 if (obj == NULL) {
1091 mutex_unlock(&dev->struct_mutex);
1092 return -EBADF;
1093 }
1094
1095#if WATCH_BUF
Krzysztof Halasacfd43c02009-06-20 00:31:28 +02001096 DRM_INFO("%s: sw_finish %d (%p %zd)\n",
Eric Anholt673a3942008-07-30 12:06:12 -07001097 __func__, args->handle, obj, obj->size);
1098#endif
Daniel Vetter23010e42010-03-08 13:35:02 +01001099 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001100
1101 /* Pinned buffers may be scanout, so flush the cache */
Eric Anholte47c68e2008-11-14 13:35:19 -08001102 if (obj_priv->pin_count)
1103 i915_gem_object_flush_cpu_write_domain(obj);
1104
Eric Anholt673a3942008-07-30 12:06:12 -07001105 drm_gem_object_unreference(obj);
1106 mutex_unlock(&dev->struct_mutex);
1107 return ret;
1108}
1109
1110/**
1111 * Maps the contents of an object, returning the address it is mapped
1112 * into.
1113 *
1114 * While the mapping holds a reference on the contents of the object, it doesn't
1115 * imply a ref on the object itself.
1116 */
1117int
1118i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
1119 struct drm_file *file_priv)
1120{
1121 struct drm_i915_gem_mmap *args = data;
1122 struct drm_gem_object *obj;
1123 loff_t offset;
1124 unsigned long addr;
1125
1126 if (!(dev->driver->driver_features & DRIVER_GEM))
1127 return -ENODEV;
1128
1129 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1130 if (obj == NULL)
1131 return -EBADF;
1132
1133 offset = args->offset;
1134
1135 down_write(&current->mm->mmap_sem);
1136 addr = do_mmap(obj->filp, 0, args->size,
1137 PROT_READ | PROT_WRITE, MAP_SHARED,
1138 args->offset);
1139 up_write(&current->mm->mmap_sem);
Luca Barbieribc9025b2010-02-09 05:49:12 +00001140 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001141 if (IS_ERR((void *)addr))
1142 return addr;
1143
1144 args->addr_ptr = (uint64_t) addr;
1145
1146 return 0;
1147}
1148
Jesse Barnesde151cf2008-11-12 10:03:55 -08001149/**
1150 * i915_gem_fault - fault a page into the GTT
1151 * vma: VMA in question
1152 * vmf: fault info
1153 *
1154 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1155 * from userspace. The fault handler takes care of binding the object to
1156 * the GTT (if needed), allocating and programming a fence register (again,
1157 * only if needed based on whether the old reg is still valid or the object
1158 * is tiled) and inserting a new PTE into the faulting process.
1159 *
1160 * Note that the faulting process may involve evicting existing objects
1161 * from the GTT and/or fence registers to make room. So performance may
1162 * suffer if the GTT working set is large or there are few fence registers
1163 * left.
1164 */
1165int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1166{
1167 struct drm_gem_object *obj = vma->vm_private_data;
1168 struct drm_device *dev = obj->dev;
1169 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001170 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001171 pgoff_t page_offset;
1172 unsigned long pfn;
1173 int ret = 0;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001174 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001175
1176 /* We don't use vmf->pgoff since that has the fake offset */
1177 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1178 PAGE_SHIFT;
1179
1180 /* Now bind it into the GTT if needed */
1181 mutex_lock(&dev->struct_mutex);
1182 if (!obj_priv->gtt_space) {
Chris Wilsone67b8ce2009-09-14 16:50:26 +01001183 ret = i915_gem_object_bind_to_gtt(obj, 0);
Chris Wilsonc7150892009-09-23 00:43:56 +01001184 if (ret)
1185 goto unlock;
Kristian Høgsberg07f4f3e2009-05-27 14:37:28 -04001186
Jesse Barnes14b60392009-05-20 16:47:08 -04001187 list_add_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001188
1189 ret = i915_gem_object_set_to_gtt_domain(obj, write);
Chris Wilsonc7150892009-09-23 00:43:56 +01001190 if (ret)
1191 goto unlock;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001192 }
1193
1194 /* Need a new fence register? */
Eric Anholta09ba7f2009-08-29 12:49:51 -07001195 if (obj_priv->tiling_mode != I915_TILING_NONE) {
Chris Wilson8c4b8c32009-06-17 22:08:52 +01001196 ret = i915_gem_object_get_fence_reg(obj);
Chris Wilsonc7150892009-09-23 00:43:56 +01001197 if (ret)
1198 goto unlock;
Eric Anholtd9ddcb92009-01-27 10:33:49 -08001199 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001200
1201 pfn = ((dev->agp->base + obj_priv->gtt_offset) >> PAGE_SHIFT) +
1202 page_offset;
1203
1204 /* Finally, remap it using the new GTT offset */
1205 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
Chris Wilsonc7150892009-09-23 00:43:56 +01001206unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001207 mutex_unlock(&dev->struct_mutex);
1208
1209 switch (ret) {
Chris Wilsonc7150892009-09-23 00:43:56 +01001210 case 0:
1211 case -ERESTARTSYS:
1212 return VM_FAULT_NOPAGE;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001213 case -ENOMEM:
1214 case -EAGAIN:
1215 return VM_FAULT_OOM;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001216 default:
Chris Wilsonc7150892009-09-23 00:43:56 +01001217 return VM_FAULT_SIGBUS;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001218 }
1219}
1220
1221/**
1222 * i915_gem_create_mmap_offset - create a fake mmap offset for an object
1223 * @obj: obj in question
1224 *
1225 * GEM memory mapping works by handing back to userspace a fake mmap offset
1226 * it can use in a subsequent mmap(2) call. The DRM core code then looks
1227 * up the object based on the offset and sets up the various memory mapping
1228 * structures.
1229 *
1230 * This routine allocates and attaches a fake offset for @obj.
1231 */
1232static int
1233i915_gem_create_mmap_offset(struct drm_gem_object *obj)
1234{
1235 struct drm_device *dev = obj->dev;
1236 struct drm_gem_mm *mm = dev->mm_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001237 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001238 struct drm_map_list *list;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +11001239 struct drm_local_map *map;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001240 int ret = 0;
1241
1242 /* Set the object up for mmap'ing */
1243 list = &obj->map_list;
Eric Anholt9a298b22009-03-24 12:23:04 -07001244 list->map = kzalloc(sizeof(struct drm_map_list), GFP_KERNEL);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001245 if (!list->map)
1246 return -ENOMEM;
1247
1248 map = list->map;
1249 map->type = _DRM_GEM;
1250 map->size = obj->size;
1251 map->handle = obj;
1252
1253 /* Get a DRM GEM mmap offset allocated... */
1254 list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
1255 obj->size / PAGE_SIZE, 0, 0);
1256 if (!list->file_offset_node) {
1257 DRM_ERROR("failed to allocate offset for bo %d\n", obj->name);
1258 ret = -ENOMEM;
1259 goto out_free_list;
1260 }
1261
1262 list->file_offset_node = drm_mm_get_block(list->file_offset_node,
1263 obj->size / PAGE_SIZE, 0);
1264 if (!list->file_offset_node) {
1265 ret = -ENOMEM;
1266 goto out_free_list;
1267 }
1268
1269 list->hash.key = list->file_offset_node->start;
1270 if (drm_ht_insert_item(&mm->offset_hash, &list->hash)) {
1271 DRM_ERROR("failed to add to map hash\n");
Chris Wilson5618ca62009-12-02 15:15:30 +00001272 ret = -ENOMEM;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001273 goto out_free_mm;
1274 }
1275
1276 /* By now we should be all set, any drm_mmap request on the offset
1277 * below will get to our mmap & fault handler */
1278 obj_priv->mmap_offset = ((uint64_t) list->hash.key) << PAGE_SHIFT;
1279
1280 return 0;
1281
1282out_free_mm:
1283 drm_mm_put_block(list->file_offset_node);
1284out_free_list:
Eric Anholt9a298b22009-03-24 12:23:04 -07001285 kfree(list->map);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001286
1287 return ret;
1288}
1289
Chris Wilson901782b2009-07-10 08:18:50 +01001290/**
1291 * i915_gem_release_mmap - remove physical page mappings
1292 * @obj: obj in question
1293 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001294 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01001295 * relinquish ownership of the pages back to the system.
1296 *
1297 * It is vital that we remove the page mapping if we have mapped a tiled
1298 * object through the GTT and then lose the fence register due to
1299 * resource pressure. Similarly if the object has been moved out of the
1300 * aperture, than pages mapped into userspace must be revoked. Removing the
1301 * mapping will then trigger a page fault on the next user access, allowing
1302 * fixup by i915_gem_fault().
1303 */
Eric Anholtd05ca302009-07-10 13:02:26 -07001304void
Chris Wilson901782b2009-07-10 08:18:50 +01001305i915_gem_release_mmap(struct drm_gem_object *obj)
1306{
1307 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001308 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson901782b2009-07-10 08:18:50 +01001309
1310 if (dev->dev_mapping)
1311 unmap_mapping_range(dev->dev_mapping,
1312 obj_priv->mmap_offset, obj->size, 1);
1313}
1314
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001315static void
1316i915_gem_free_mmap_offset(struct drm_gem_object *obj)
1317{
1318 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001319 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001320 struct drm_gem_mm *mm = dev->mm_private;
1321 struct drm_map_list *list;
1322
1323 list = &obj->map_list;
1324 drm_ht_remove_item(&mm->offset_hash, &list->hash);
1325
1326 if (list->file_offset_node) {
1327 drm_mm_put_block(list->file_offset_node);
1328 list->file_offset_node = NULL;
1329 }
1330
1331 if (list->map) {
Eric Anholt9a298b22009-03-24 12:23:04 -07001332 kfree(list->map);
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001333 list->map = NULL;
1334 }
1335
1336 obj_priv->mmap_offset = 0;
1337}
1338
Jesse Barnesde151cf2008-11-12 10:03:55 -08001339/**
1340 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1341 * @obj: object to check
1342 *
1343 * Return the required GTT alignment for an object, taking into account
1344 * potential fence register mapping if needed.
1345 */
1346static uint32_t
1347i915_gem_get_gtt_alignment(struct drm_gem_object *obj)
1348{
1349 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001350 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001351 int start, i;
1352
1353 /*
1354 * Minimum alignment is 4k (GTT page size), but might be greater
1355 * if a fence register is needed for the object.
1356 */
1357 if (IS_I965G(dev) || obj_priv->tiling_mode == I915_TILING_NONE)
1358 return 4096;
1359
1360 /*
1361 * Previous chips need to be aligned to the size of the smallest
1362 * fence register that can contain the object.
1363 */
1364 if (IS_I9XX(dev))
1365 start = 1024*1024;
1366 else
1367 start = 512*1024;
1368
1369 for (i = start; i < obj->size; i <<= 1)
1370 ;
1371
1372 return i;
1373}
1374
1375/**
1376 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1377 * @dev: DRM device
1378 * @data: GTT mapping ioctl data
1379 * @file_priv: GEM object info
1380 *
1381 * Simply returns the fake offset to userspace so it can mmap it.
1382 * The mmap call will end up in drm_gem_mmap(), which will set things
1383 * up so we can get faults in the handler above.
1384 *
1385 * The fault handler will take care of binding the object into the GTT
1386 * (since it may have been evicted to make room for something), allocating
1387 * a fence register, and mapping the appropriate aperture address into
1388 * userspace.
1389 */
1390int
1391i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
1392 struct drm_file *file_priv)
1393{
1394 struct drm_i915_gem_mmap_gtt *args = data;
1395 struct drm_i915_private *dev_priv = dev->dev_private;
1396 struct drm_gem_object *obj;
1397 struct drm_i915_gem_object *obj_priv;
1398 int ret;
1399
1400 if (!(dev->driver->driver_features & DRIVER_GEM))
1401 return -ENODEV;
1402
1403 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1404 if (obj == NULL)
1405 return -EBADF;
1406
1407 mutex_lock(&dev->struct_mutex);
1408
Daniel Vetter23010e42010-03-08 13:35:02 +01001409 obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001410
Chris Wilsonab182822009-09-22 18:46:17 +01001411 if (obj_priv->madv != I915_MADV_WILLNEED) {
1412 DRM_ERROR("Attempting to mmap a purgeable buffer\n");
1413 drm_gem_object_unreference(obj);
1414 mutex_unlock(&dev->struct_mutex);
1415 return -EINVAL;
1416 }
1417
1418
Jesse Barnesde151cf2008-11-12 10:03:55 -08001419 if (!obj_priv->mmap_offset) {
1420 ret = i915_gem_create_mmap_offset(obj);
Chris Wilson13af1062009-02-11 14:26:31 +00001421 if (ret) {
1422 drm_gem_object_unreference(obj);
1423 mutex_unlock(&dev->struct_mutex);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001424 return ret;
Chris Wilson13af1062009-02-11 14:26:31 +00001425 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001426 }
1427
1428 args->offset = obj_priv->mmap_offset;
1429
Jesse Barnesde151cf2008-11-12 10:03:55 -08001430 /*
1431 * Pull it into the GTT so that we have a page list (makes the
1432 * initial fault faster and any subsequent flushing possible).
1433 */
1434 if (!obj_priv->agp_mem) {
Chris Wilsone67b8ce2009-09-14 16:50:26 +01001435 ret = i915_gem_object_bind_to_gtt(obj, 0);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001436 if (ret) {
1437 drm_gem_object_unreference(obj);
1438 mutex_unlock(&dev->struct_mutex);
1439 return ret;
1440 }
Jesse Barnes14b60392009-05-20 16:47:08 -04001441 list_add_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001442 }
1443
1444 drm_gem_object_unreference(obj);
1445 mutex_unlock(&dev->struct_mutex);
1446
1447 return 0;
1448}
1449
Ben Gamari6911a9b2009-04-02 11:24:54 -07001450void
Eric Anholt856fa192009-03-19 14:10:50 -07001451i915_gem_object_put_pages(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07001452{
Daniel Vetter23010e42010-03-08 13:35:02 +01001453 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001454 int page_count = obj->size / PAGE_SIZE;
1455 int i;
1456
Eric Anholt856fa192009-03-19 14:10:50 -07001457 BUG_ON(obj_priv->pages_refcount == 0);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001458 BUG_ON(obj_priv->madv == __I915_MADV_PURGED);
Eric Anholt856fa192009-03-19 14:10:50 -07001459
1460 if (--obj_priv->pages_refcount != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07001461 return;
1462
Eric Anholt280b7132009-03-12 16:56:27 -07001463 if (obj_priv->tiling_mode != I915_TILING_NONE)
1464 i915_gem_object_save_bit_17_swizzle(obj);
1465
Chris Wilson3ef94da2009-09-14 16:50:29 +01001466 if (obj_priv->madv == I915_MADV_DONTNEED)
Chris Wilson13a05fd2009-09-20 23:03:19 +01001467 obj_priv->dirty = 0;
Chris Wilson3ef94da2009-09-14 16:50:29 +01001468
1469 for (i = 0; i < page_count; i++) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01001470 if (obj_priv->dirty)
1471 set_page_dirty(obj_priv->pages[i]);
1472
1473 if (obj_priv->madv == I915_MADV_WILLNEED)
Eric Anholt856fa192009-03-19 14:10:50 -07001474 mark_page_accessed(obj_priv->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001475
1476 page_cache_release(obj_priv->pages[i]);
1477 }
Eric Anholt673a3942008-07-30 12:06:12 -07001478 obj_priv->dirty = 0;
1479
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07001480 drm_free_large(obj_priv->pages);
Eric Anholt856fa192009-03-19 14:10:50 -07001481 obj_priv->pages = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001482}
1483
1484static void
Zou Nan hai852835f2010-05-21 09:08:56 +08001485i915_gem_object_move_to_active(struct drm_gem_object *obj, uint32_t seqno,
1486 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001487{
1488 struct drm_device *dev = obj->dev;
1489 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001490 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Zou Nan hai852835f2010-05-21 09:08:56 +08001491 BUG_ON(ring == NULL);
1492 obj_priv->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001493
1494 /* Add a reference if we're newly entering the active list. */
1495 if (!obj_priv->active) {
1496 drm_gem_object_reference(obj);
1497 obj_priv->active = 1;
1498 }
1499 /* Move from whatever list we were on to the tail of execution. */
Carl Worth5e118f42009-03-20 11:54:25 -07001500 spin_lock(&dev_priv->mm.active_list_lock);
Zou Nan hai852835f2010-05-21 09:08:56 +08001501 list_move_tail(&obj_priv->list, &ring->active_list);
Carl Worth5e118f42009-03-20 11:54:25 -07001502 spin_unlock(&dev_priv->mm.active_list_lock);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001503 obj_priv->last_rendering_seqno = seqno;
Eric Anholt673a3942008-07-30 12:06:12 -07001504}
1505
Eric Anholtce44b0e2008-11-06 16:00:31 -08001506static void
1507i915_gem_object_move_to_flushing(struct drm_gem_object *obj)
1508{
1509 struct drm_device *dev = obj->dev;
1510 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001511 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001512
1513 BUG_ON(!obj_priv->active);
1514 list_move_tail(&obj_priv->list, &dev_priv->mm.flushing_list);
1515 obj_priv->last_rendering_seqno = 0;
1516}
Eric Anholt673a3942008-07-30 12:06:12 -07001517
Chris Wilson963b4832009-09-20 23:03:54 +01001518/* Immediately discard the backing storage */
1519static void
1520i915_gem_object_truncate(struct drm_gem_object *obj)
1521{
Daniel Vetter23010e42010-03-08 13:35:02 +01001522 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001523 struct inode *inode;
Chris Wilson963b4832009-09-20 23:03:54 +01001524
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001525 inode = obj->filp->f_path.dentry->d_inode;
1526 if (inode->i_op->truncate)
1527 inode->i_op->truncate (inode);
1528
1529 obj_priv->madv = __I915_MADV_PURGED;
Chris Wilson963b4832009-09-20 23:03:54 +01001530}
1531
1532static inline int
1533i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj_priv)
1534{
1535 return obj_priv->madv == I915_MADV_DONTNEED;
1536}
1537
Eric Anholt673a3942008-07-30 12:06:12 -07001538static void
1539i915_gem_object_move_to_inactive(struct drm_gem_object *obj)
1540{
1541 struct drm_device *dev = obj->dev;
1542 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001543 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001544
1545 i915_verify_inactive(dev, __FILE__, __LINE__);
1546 if (obj_priv->pin_count != 0)
1547 list_del_init(&obj_priv->list);
1548 else
1549 list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
1550
Daniel Vetter99fcb762010-02-07 16:20:18 +01001551 BUG_ON(!list_empty(&obj_priv->gpu_write_list));
1552
Eric Anholtce44b0e2008-11-06 16:00:31 -08001553 obj_priv->last_rendering_seqno = 0;
Zou Nan hai852835f2010-05-21 09:08:56 +08001554 obj_priv->ring = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001555 if (obj_priv->active) {
1556 obj_priv->active = 0;
1557 drm_gem_object_unreference(obj);
1558 }
1559 i915_verify_inactive(dev, __FILE__, __LINE__);
1560}
1561
Daniel Vetter63560392010-02-19 11:51:59 +01001562static void
1563i915_gem_process_flushing_list(struct drm_device *dev,
Zou Nan hai852835f2010-05-21 09:08:56 +08001564 uint32_t flush_domains, uint32_t seqno,
1565 struct intel_ring_buffer *ring)
Daniel Vetter63560392010-02-19 11:51:59 +01001566{
1567 drm_i915_private_t *dev_priv = dev->dev_private;
1568 struct drm_i915_gem_object *obj_priv, *next;
1569
1570 list_for_each_entry_safe(obj_priv, next,
1571 &dev_priv->mm.gpu_write_list,
1572 gpu_write_list) {
Daniel Vettera8089e82010-04-09 19:05:09 +00001573 struct drm_gem_object *obj = &obj_priv->base;
Daniel Vetter63560392010-02-19 11:51:59 +01001574
1575 if ((obj->write_domain & flush_domains) ==
Zou Nan hai852835f2010-05-21 09:08:56 +08001576 obj->write_domain &&
1577 obj_priv->ring->ring_flag == ring->ring_flag) {
Daniel Vetter63560392010-02-19 11:51:59 +01001578 uint32_t old_write_domain = obj->write_domain;
1579
1580 obj->write_domain = 0;
1581 list_del_init(&obj_priv->gpu_write_list);
Zou Nan hai852835f2010-05-21 09:08:56 +08001582 i915_gem_object_move_to_active(obj, seqno, ring);
Daniel Vetter63560392010-02-19 11:51:59 +01001583
1584 /* update the fence lru list */
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001585 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
1586 struct drm_i915_fence_reg *reg =
1587 &dev_priv->fence_regs[obj_priv->fence_reg];
1588 list_move_tail(&reg->lru_list,
Daniel Vetter63560392010-02-19 11:51:59 +01001589 &dev_priv->mm.fence_list);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001590 }
Daniel Vetter63560392010-02-19 11:51:59 +01001591
1592 trace_i915_gem_object_change_domain(obj,
1593 obj->read_domains,
1594 old_write_domain);
1595 }
1596 }
1597}
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001598
Daniel Vetter5a5a0c62009-09-15 22:57:36 +02001599uint32_t
Eric Anholtb9624422009-06-03 07:27:35 +00001600i915_add_request(struct drm_device *dev, struct drm_file *file_priv,
Zou Nan hai852835f2010-05-21 09:08:56 +08001601 uint32_t flush_domains, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001602{
1603 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtb9624422009-06-03 07:27:35 +00001604 struct drm_i915_file_private *i915_file_priv = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001605 struct drm_i915_gem_request *request;
1606 uint32_t seqno;
1607 int was_empty;
Eric Anholt673a3942008-07-30 12:06:12 -07001608
Eric Anholtb9624422009-06-03 07:27:35 +00001609 if (file_priv != NULL)
1610 i915_file_priv = file_priv->driver_priv;
1611
Eric Anholt9a298b22009-03-24 12:23:04 -07001612 request = kzalloc(sizeof(*request), GFP_KERNEL);
Eric Anholt673a3942008-07-30 12:06:12 -07001613 if (request == NULL)
1614 return 0;
1615
Zou Nan hai852835f2010-05-21 09:08:56 +08001616 seqno = ring->add_request(dev, ring, file_priv, flush_domains);
Eric Anholt673a3942008-07-30 12:06:12 -07001617
1618 request->seqno = seqno;
Zou Nan hai852835f2010-05-21 09:08:56 +08001619 request->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001620 request->emitted_jiffies = jiffies;
Zou Nan hai852835f2010-05-21 09:08:56 +08001621 was_empty = list_empty(&ring->request_list);
1622 list_add_tail(&request->list, &ring->request_list);
1623
Eric Anholtb9624422009-06-03 07:27:35 +00001624 if (i915_file_priv) {
1625 list_add_tail(&request->client_list,
1626 &i915_file_priv->mm.request_list);
1627 } else {
1628 INIT_LIST_HEAD(&request->client_list);
1629 }
Eric Anholt673a3942008-07-30 12:06:12 -07001630
Eric Anholtce44b0e2008-11-06 16:00:31 -08001631 /* Associate any objects on the flushing list matching the write
1632 * domain we're flushing with our flush.
1633 */
Daniel Vetter63560392010-02-19 11:51:59 +01001634 if (flush_domains != 0)
Zou Nan hai852835f2010-05-21 09:08:56 +08001635 i915_gem_process_flushing_list(dev, flush_domains, seqno, ring);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001636
Ben Gamarif65d9422009-09-14 17:48:44 -04001637 if (!dev_priv->mm.suspended) {
1638 mod_timer(&dev_priv->hangcheck_timer, jiffies + DRM_I915_HANGCHECK_PERIOD);
1639 if (was_empty)
1640 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
1641 }
Eric Anholt673a3942008-07-30 12:06:12 -07001642 return seqno;
1643}
1644
1645/**
1646 * Command execution barrier
1647 *
1648 * Ensures that all commands in the ring are finished
1649 * before signalling the CPU
1650 */
Eric Anholt3043c602008-10-02 12:24:47 -07001651static uint32_t
Zou Nan hai852835f2010-05-21 09:08:56 +08001652i915_retire_commands(struct drm_device *dev, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001653{
Eric Anholt673a3942008-07-30 12:06:12 -07001654 uint32_t flush_domains = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001655
1656 /* The sampler always gets flushed on i965 (sigh) */
1657 if (IS_I965G(dev))
1658 flush_domains |= I915_GEM_DOMAIN_SAMPLER;
Zou Nan hai852835f2010-05-21 09:08:56 +08001659
1660 ring->flush(dev, ring,
1661 I915_GEM_DOMAIN_COMMAND, flush_domains);
Eric Anholt673a3942008-07-30 12:06:12 -07001662 return flush_domains;
1663}
1664
1665/**
1666 * Moves buffers associated only with the given active seqno from the active
1667 * to inactive list, potentially freeing them.
1668 */
1669static void
1670i915_gem_retire_request(struct drm_device *dev,
1671 struct drm_i915_gem_request *request)
1672{
1673 drm_i915_private_t *dev_priv = dev->dev_private;
1674
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001675 trace_i915_gem_request_retire(dev, request->seqno);
1676
Eric Anholt673a3942008-07-30 12:06:12 -07001677 /* Move any buffers on the active list that are no longer referenced
1678 * by the ringbuffer to the flushing/inactive lists as appropriate.
1679 */
Carl Worth5e118f42009-03-20 11:54:25 -07001680 spin_lock(&dev_priv->mm.active_list_lock);
Zou Nan hai852835f2010-05-21 09:08:56 +08001681 while (!list_empty(&request->ring->active_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001682 struct drm_gem_object *obj;
1683 struct drm_i915_gem_object *obj_priv;
1684
Zou Nan hai852835f2010-05-21 09:08:56 +08001685 obj_priv = list_first_entry(&request->ring->active_list,
Eric Anholt673a3942008-07-30 12:06:12 -07001686 struct drm_i915_gem_object,
1687 list);
Daniel Vettera8089e82010-04-09 19:05:09 +00001688 obj = &obj_priv->base;
Eric Anholt673a3942008-07-30 12:06:12 -07001689
1690 /* If the seqno being retired doesn't match the oldest in the
1691 * list, then the oldest in the list must still be newer than
1692 * this seqno.
1693 */
1694 if (obj_priv->last_rendering_seqno != request->seqno)
Carl Worth5e118f42009-03-20 11:54:25 -07001695 goto out;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001696
Eric Anholt673a3942008-07-30 12:06:12 -07001697#if WATCH_LRU
1698 DRM_INFO("%s: retire %d moves to inactive list %p\n",
1699 __func__, request->seqno, obj);
1700#endif
1701
Eric Anholtce44b0e2008-11-06 16:00:31 -08001702 if (obj->write_domain != 0)
1703 i915_gem_object_move_to_flushing(obj);
Shaohua Li68c84342009-04-08 10:58:23 +08001704 else {
1705 /* Take a reference on the object so it won't be
1706 * freed while the spinlock is held. The list
1707 * protection for this spinlock is safe when breaking
1708 * the lock like this since the next thing we do
1709 * is just get the head of the list again.
1710 */
1711 drm_gem_object_reference(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001712 i915_gem_object_move_to_inactive(obj);
Shaohua Li68c84342009-04-08 10:58:23 +08001713 spin_unlock(&dev_priv->mm.active_list_lock);
1714 drm_gem_object_unreference(obj);
1715 spin_lock(&dev_priv->mm.active_list_lock);
1716 }
Eric Anholt673a3942008-07-30 12:06:12 -07001717 }
Carl Worth5e118f42009-03-20 11:54:25 -07001718out:
1719 spin_unlock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001720}
1721
1722/**
1723 * Returns true if seq1 is later than seq2.
1724 */
Ben Gamari22be1722009-09-14 17:48:43 -04001725bool
Eric Anholt673a3942008-07-30 12:06:12 -07001726i915_seqno_passed(uint32_t seq1, uint32_t seq2)
1727{
1728 return (int32_t)(seq1 - seq2) >= 0;
1729}
1730
1731uint32_t
Zou Nan hai852835f2010-05-21 09:08:56 +08001732i915_get_gem_seqno(struct drm_device *dev,
Zou Nan haid1b851f2010-05-21 09:08:57 +08001733 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001734{
Zou Nan hai852835f2010-05-21 09:08:56 +08001735 return ring->get_gem_seqno(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07001736}
1737
1738/**
1739 * This function clears the request list as sequence numbers are passed.
1740 */
1741void
Zou Nan hai852835f2010-05-21 09:08:56 +08001742i915_gem_retire_requests(struct drm_device *dev,
1743 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001744{
1745 drm_i915_private_t *dev_priv = dev->dev_private;
1746 uint32_t seqno;
1747
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001748 if (!ring->status_page.page_addr
Zou Nan hai852835f2010-05-21 09:08:56 +08001749 || list_empty(&ring->request_list))
Karsten Wiese6c0594a2009-02-23 15:07:57 +01001750 return;
1751
Zou Nan hai852835f2010-05-21 09:08:56 +08001752 seqno = i915_get_gem_seqno(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07001753
Zou Nan hai852835f2010-05-21 09:08:56 +08001754 while (!list_empty(&ring->request_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001755 struct drm_i915_gem_request *request;
1756 uint32_t retiring_seqno;
1757
Zou Nan hai852835f2010-05-21 09:08:56 +08001758 request = list_first_entry(&ring->request_list,
Eric Anholt673a3942008-07-30 12:06:12 -07001759 struct drm_i915_gem_request,
1760 list);
1761 retiring_seqno = request->seqno;
1762
1763 if (i915_seqno_passed(seqno, retiring_seqno) ||
Ben Gamariba1234d2009-09-14 17:48:47 -04001764 atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001765 i915_gem_retire_request(dev, request);
1766
1767 list_del(&request->list);
Eric Anholtb9624422009-06-03 07:27:35 +00001768 list_del(&request->client_list);
Eric Anholt9a298b22009-03-24 12:23:04 -07001769 kfree(request);
Eric Anholt673a3942008-07-30 12:06:12 -07001770 } else
1771 break;
1772 }
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001773
1774 if (unlikely (dev_priv->trace_irq_seqno &&
1775 i915_seqno_passed(dev_priv->trace_irq_seqno, seqno))) {
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001776
1777 ring->user_irq_put(dev, ring);
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001778 dev_priv->trace_irq_seqno = 0;
1779 }
Eric Anholt673a3942008-07-30 12:06:12 -07001780}
1781
1782void
1783i915_gem_retire_work_handler(struct work_struct *work)
1784{
1785 drm_i915_private_t *dev_priv;
1786 struct drm_device *dev;
1787
1788 dev_priv = container_of(work, drm_i915_private_t,
1789 mm.retire_work.work);
1790 dev = dev_priv->dev;
1791
1792 mutex_lock(&dev->struct_mutex);
Zou Nan hai852835f2010-05-21 09:08:56 +08001793 i915_gem_retire_requests(dev, &dev_priv->render_ring);
1794
Zou Nan haid1b851f2010-05-21 09:08:57 +08001795 if (HAS_BSD(dev))
1796 i915_gem_retire_requests(dev, &dev_priv->bsd_ring);
1797
Keith Packard6dbe2772008-10-14 21:41:13 -07001798 if (!dev_priv->mm.suspended &&
Zou Nan haid1b851f2010-05-21 09:08:57 +08001799 (!list_empty(&dev_priv->render_ring.request_list) ||
1800 (HAS_BSD(dev) &&
1801 !list_empty(&dev_priv->bsd_ring.request_list))))
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001802 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
Eric Anholt673a3942008-07-30 12:06:12 -07001803 mutex_unlock(&dev->struct_mutex);
1804}
1805
Daniel Vetter5a5a0c62009-09-15 22:57:36 +02001806int
Zou Nan hai852835f2010-05-21 09:08:56 +08001807i915_do_wait_request(struct drm_device *dev, uint32_t seqno,
1808 int interruptible, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001809{
1810 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001811 u32 ier;
Eric Anholt673a3942008-07-30 12:06:12 -07001812 int ret = 0;
1813
1814 BUG_ON(seqno == 0);
1815
Ben Gamariba1234d2009-09-14 17:48:47 -04001816 if (atomic_read(&dev_priv->mm.wedged))
Ben Gamariffed1d02009-09-14 17:48:41 -04001817 return -EIO;
1818
Zou Nan hai852835f2010-05-21 09:08:56 +08001819 if (!i915_seqno_passed(ring->get_gem_seqno(dev, ring), seqno)) {
Eric Anholtbad720f2009-10-22 16:11:14 -07001820 if (HAS_PCH_SPLIT(dev))
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001821 ier = I915_READ(DEIER) | I915_READ(GTIER);
1822 else
1823 ier = I915_READ(IER);
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001824 if (!ier) {
1825 DRM_ERROR("something (likely vbetool) disabled "
1826 "interrupts, re-enabling\n");
1827 i915_driver_irq_preinstall(dev);
1828 i915_driver_irq_postinstall(dev);
1829 }
1830
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001831 trace_i915_gem_request_wait_begin(dev, seqno);
1832
Zou Nan hai852835f2010-05-21 09:08:56 +08001833 ring->waiting_gem_seqno = seqno;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001834 ring->user_irq_get(dev, ring);
Daniel Vetter48764bf2009-09-15 22:57:32 +02001835 if (interruptible)
Zou Nan hai852835f2010-05-21 09:08:56 +08001836 ret = wait_event_interruptible(ring->irq_queue,
1837 i915_seqno_passed(
1838 ring->get_gem_seqno(dev, ring), seqno)
1839 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02001840 else
Zou Nan hai852835f2010-05-21 09:08:56 +08001841 wait_event(ring->irq_queue,
1842 i915_seqno_passed(
1843 ring->get_gem_seqno(dev, ring), seqno)
1844 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02001845
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001846 ring->user_irq_put(dev, ring);
Zou Nan hai852835f2010-05-21 09:08:56 +08001847 ring->waiting_gem_seqno = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001848
1849 trace_i915_gem_request_wait_end(dev, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07001850 }
Ben Gamariba1234d2009-09-14 17:48:47 -04001851 if (atomic_read(&dev_priv->mm.wedged))
Eric Anholt673a3942008-07-30 12:06:12 -07001852 ret = -EIO;
1853
1854 if (ret && ret != -ERESTARTSYS)
1855 DRM_ERROR("%s returns %d (awaiting %d at %d)\n",
Zou Nan hai852835f2010-05-21 09:08:56 +08001856 __func__, ret, seqno, ring->get_gem_seqno(dev, ring));
Eric Anholt673a3942008-07-30 12:06:12 -07001857
1858 /* Directly dispatch request retiring. While we have the work queue
1859 * to handle this, the waiter on a request often wants an associated
1860 * buffer to have made it to the inactive list, and we would need
1861 * a separate wait queue to handle that.
1862 */
1863 if (ret == 0)
Zou Nan hai852835f2010-05-21 09:08:56 +08001864 i915_gem_retire_requests(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07001865
1866 return ret;
1867}
1868
Daniel Vetter48764bf2009-09-15 22:57:32 +02001869/**
1870 * Waits for a sequence number to be signaled, and cleans up the
1871 * request and object lists appropriately for that event.
1872 */
1873static int
Zou Nan hai852835f2010-05-21 09:08:56 +08001874i915_wait_request(struct drm_device *dev, uint32_t seqno,
1875 struct intel_ring_buffer *ring)
Daniel Vetter48764bf2009-09-15 22:57:32 +02001876{
Zou Nan hai852835f2010-05-21 09:08:56 +08001877 return i915_do_wait_request(dev, seqno, 1, ring);
Daniel Vetter48764bf2009-09-15 22:57:32 +02001878}
1879
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001880static void
1881i915_gem_flush(struct drm_device *dev,
1882 uint32_t invalidate_domains,
1883 uint32_t flush_domains)
1884{
1885 drm_i915_private_t *dev_priv = dev->dev_private;
1886 if (flush_domains & I915_GEM_DOMAIN_CPU)
1887 drm_agp_chipset_flush(dev);
1888 dev_priv->render_ring.flush(dev, &dev_priv->render_ring,
1889 invalidate_domains,
1890 flush_domains);
Zou Nan haid1b851f2010-05-21 09:08:57 +08001891
1892 if (HAS_BSD(dev))
1893 dev_priv->bsd_ring.flush(dev, &dev_priv->bsd_ring,
1894 invalidate_domains,
1895 flush_domains);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001896}
1897
Zou Nan hai852835f2010-05-21 09:08:56 +08001898static void
1899i915_gem_flush_ring(struct drm_device *dev,
1900 uint32_t invalidate_domains,
1901 uint32_t flush_domains,
1902 struct intel_ring_buffer *ring)
1903{
1904 if (flush_domains & I915_GEM_DOMAIN_CPU)
1905 drm_agp_chipset_flush(dev);
1906 ring->flush(dev, ring,
1907 invalidate_domains,
1908 flush_domains);
1909}
1910
Eric Anholt673a3942008-07-30 12:06:12 -07001911/**
1912 * Ensures that all rendering to the object has completed and the object is
1913 * safe to unbind from the GTT or access from the CPU.
1914 */
1915static int
1916i915_gem_object_wait_rendering(struct drm_gem_object *obj)
1917{
1918 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001919 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001920 int ret;
1921
Eric Anholte47c68e2008-11-14 13:35:19 -08001922 /* This function only exists to support waiting for existing rendering,
1923 * not for emitting required flushes.
Eric Anholt673a3942008-07-30 12:06:12 -07001924 */
Eric Anholte47c68e2008-11-14 13:35:19 -08001925 BUG_ON((obj->write_domain & I915_GEM_GPU_DOMAINS) != 0);
Eric Anholt673a3942008-07-30 12:06:12 -07001926
1927 /* If there is rendering queued on the buffer being evicted, wait for
1928 * it.
1929 */
1930 if (obj_priv->active) {
1931#if WATCH_BUF
1932 DRM_INFO("%s: object %p wait for seqno %08x\n",
1933 __func__, obj, obj_priv->last_rendering_seqno);
1934#endif
Zou Nan hai852835f2010-05-21 09:08:56 +08001935 ret = i915_wait_request(dev,
1936 obj_priv->last_rendering_seqno, obj_priv->ring);
Eric Anholt673a3942008-07-30 12:06:12 -07001937 if (ret != 0)
1938 return ret;
1939 }
1940
1941 return 0;
1942}
1943
1944/**
1945 * Unbinds an object from the GTT aperture.
1946 */
Jesse Barnes0f973f22009-01-26 17:10:45 -08001947int
Eric Anholt673a3942008-07-30 12:06:12 -07001948i915_gem_object_unbind(struct drm_gem_object *obj)
1949{
1950 struct drm_device *dev = obj->dev;
Daniel Vetter4a87b8c2010-02-19 11:51:57 +01001951 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001952 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001953 int ret = 0;
1954
1955#if WATCH_BUF
1956 DRM_INFO("%s:%d %p\n", __func__, __LINE__, obj);
1957 DRM_INFO("gtt_space %p\n", obj_priv->gtt_space);
1958#endif
1959 if (obj_priv->gtt_space == NULL)
1960 return 0;
1961
1962 if (obj_priv->pin_count != 0) {
1963 DRM_ERROR("Attempting to unbind pinned buffer\n");
1964 return -EINVAL;
1965 }
1966
Eric Anholt5323fd02009-09-09 11:50:45 -07001967 /* blow away mappings if mapped through GTT */
1968 i915_gem_release_mmap(obj);
1969
Eric Anholt673a3942008-07-30 12:06:12 -07001970 /* Move the object to the CPU domain to ensure that
1971 * any possible CPU writes while it's not in the GTT
1972 * are flushed when we go to remap it. This will
1973 * also ensure that all pending GPU writes are finished
1974 * before we unbind.
1975 */
Eric Anholte47c68e2008-11-14 13:35:19 -08001976 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07001977 if (ret) {
Eric Anholte47c68e2008-11-14 13:35:19 -08001978 if (ret != -ERESTARTSYS)
1979 DRM_ERROR("set_domain failed: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07001980 return ret;
1981 }
1982
Eric Anholt5323fd02009-09-09 11:50:45 -07001983 BUG_ON(obj_priv->active);
1984
Daniel Vetter96b47b62009-12-15 17:50:00 +01001985 /* release the fence reg _after_ flushing */
1986 if (obj_priv->fence_reg != I915_FENCE_REG_NONE)
1987 i915_gem_clear_fence_reg(obj);
1988
Eric Anholt673a3942008-07-30 12:06:12 -07001989 if (obj_priv->agp_mem != NULL) {
1990 drm_unbind_agp(obj_priv->agp_mem);
1991 drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE);
1992 obj_priv->agp_mem = NULL;
1993 }
1994
Eric Anholt856fa192009-03-19 14:10:50 -07001995 i915_gem_object_put_pages(obj);
Chris Wilsona32808c2009-09-20 21:29:47 +01001996 BUG_ON(obj_priv->pages_refcount);
Eric Anholt673a3942008-07-30 12:06:12 -07001997
1998 if (obj_priv->gtt_space) {
1999 atomic_dec(&dev->gtt_count);
2000 atomic_sub(obj->size, &dev->gtt_memory);
2001
2002 drm_mm_put_block(obj_priv->gtt_space);
2003 obj_priv->gtt_space = NULL;
2004 }
2005
2006 /* Remove ourselves from the LRU list if present. */
Daniel Vetter4a87b8c2010-02-19 11:51:57 +01002007 spin_lock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07002008 if (!list_empty(&obj_priv->list))
2009 list_del_init(&obj_priv->list);
Daniel Vetter4a87b8c2010-02-19 11:51:57 +01002010 spin_unlock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07002011
Chris Wilson963b4832009-09-20 23:03:54 +01002012 if (i915_gem_object_is_purgeable(obj_priv))
2013 i915_gem_object_truncate(obj);
2014
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002015 trace_i915_gem_object_unbind(obj);
2016
Eric Anholt673a3942008-07-30 12:06:12 -07002017 return 0;
2018}
2019
Chris Wilson07f73f62009-09-14 16:50:30 +01002020static struct drm_gem_object *
2021i915_gem_find_inactive_object(struct drm_device *dev, int min_size)
2022{
2023 drm_i915_private_t *dev_priv = dev->dev_private;
2024 struct drm_i915_gem_object *obj_priv;
2025 struct drm_gem_object *best = NULL;
2026 struct drm_gem_object *first = NULL;
2027
2028 /* Try to find the smallest clean object */
2029 list_for_each_entry(obj_priv, &dev_priv->mm.inactive_list, list) {
Daniel Vettera8089e82010-04-09 19:05:09 +00002030 struct drm_gem_object *obj = &obj_priv->base;
Chris Wilson07f73f62009-09-14 16:50:30 +01002031 if (obj->size >= min_size) {
Chris Wilson963b4832009-09-20 23:03:54 +01002032 if ((!obj_priv->dirty ||
2033 i915_gem_object_is_purgeable(obj_priv)) &&
Chris Wilson07f73f62009-09-14 16:50:30 +01002034 (!best || obj->size < best->size)) {
2035 best = obj;
2036 if (best->size == min_size)
2037 return best;
2038 }
2039 if (!first)
2040 first = obj;
2041 }
2042 }
2043
2044 return best ? best : first;
2045}
2046
Eric Anholt673a3942008-07-30 12:06:12 -07002047static int
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002048i915_gpu_idle(struct drm_device *dev)
2049{
2050 drm_i915_private_t *dev_priv = dev->dev_private;
2051 bool lists_empty;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002052 uint32_t seqno1, seqno2;
Zou Nan hai852835f2010-05-21 09:08:56 +08002053 int ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002054
2055 spin_lock(&dev_priv->mm.active_list_lock);
Zou Nan haid1b851f2010-05-21 09:08:57 +08002056 lists_empty = (list_empty(&dev_priv->mm.flushing_list) &&
2057 list_empty(&dev_priv->render_ring.active_list) &&
2058 (!HAS_BSD(dev) ||
2059 list_empty(&dev_priv->bsd_ring.active_list)));
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002060 spin_unlock(&dev_priv->mm.active_list_lock);
2061
2062 if (lists_empty)
2063 return 0;
2064
2065 /* Flush everything onto the inactive list. */
2066 i915_gem_flush(dev, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
Zou Nan haid1b851f2010-05-21 09:08:57 +08002067 seqno1 = i915_add_request(dev, NULL, I915_GEM_GPU_DOMAINS,
Zou Nan hai852835f2010-05-21 09:08:56 +08002068 &dev_priv->render_ring);
Zou Nan haid1b851f2010-05-21 09:08:57 +08002069 if (seqno1 == 0)
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002070 return -ENOMEM;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002071 ret = i915_wait_request(dev, seqno1, &dev_priv->render_ring);
2072
2073 if (HAS_BSD(dev)) {
2074 seqno2 = i915_add_request(dev, NULL, I915_GEM_GPU_DOMAINS,
2075 &dev_priv->bsd_ring);
2076 if (seqno2 == 0)
2077 return -ENOMEM;
2078
2079 ret = i915_wait_request(dev, seqno2, &dev_priv->bsd_ring);
2080 if (ret)
2081 return ret;
2082 }
2083
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002084
Zou Nan hai852835f2010-05-21 09:08:56 +08002085 return ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002086}
2087
2088static int
Chris Wilson07f73f62009-09-14 16:50:30 +01002089i915_gem_evict_everything(struct drm_device *dev)
2090{
2091 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson07f73f62009-09-14 16:50:30 +01002092 int ret;
2093 bool lists_empty;
2094
Chris Wilson07f73f62009-09-14 16:50:30 +01002095 spin_lock(&dev_priv->mm.active_list_lock);
2096 lists_empty = (list_empty(&dev_priv->mm.inactive_list) &&
2097 list_empty(&dev_priv->mm.flushing_list) &&
Zou Nan haid1b851f2010-05-21 09:08:57 +08002098 list_empty(&dev_priv->render_ring.active_list) &&
2099 (!HAS_BSD(dev)
2100 || list_empty(&dev_priv->bsd_ring.active_list)));
Chris Wilson07f73f62009-09-14 16:50:30 +01002101 spin_unlock(&dev_priv->mm.active_list_lock);
2102
Chris Wilson97311292009-09-21 00:22:34 +01002103 if (lists_empty)
Chris Wilson07f73f62009-09-14 16:50:30 +01002104 return -ENOSPC;
Chris Wilson07f73f62009-09-14 16:50:30 +01002105
2106 /* Flush everything (on to the inactive lists) and evict */
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002107 ret = i915_gpu_idle(dev);
Chris Wilson07f73f62009-09-14 16:50:30 +01002108 if (ret)
2109 return ret;
2110
Daniel Vetter99fcb762010-02-07 16:20:18 +01002111 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
2112
Chris Wilsonab5ee572009-09-20 19:25:47 +01002113 ret = i915_gem_evict_from_inactive_list(dev);
Chris Wilson07f73f62009-09-14 16:50:30 +01002114 if (ret)
2115 return ret;
2116
2117 spin_lock(&dev_priv->mm.active_list_lock);
2118 lists_empty = (list_empty(&dev_priv->mm.inactive_list) &&
2119 list_empty(&dev_priv->mm.flushing_list) &&
Zou Nan haid1b851f2010-05-21 09:08:57 +08002120 list_empty(&dev_priv->render_ring.active_list) &&
2121 (!HAS_BSD(dev)
2122 || list_empty(&dev_priv->bsd_ring.active_list)));
Chris Wilson07f73f62009-09-14 16:50:30 +01002123 spin_unlock(&dev_priv->mm.active_list_lock);
2124 BUG_ON(!lists_empty);
2125
Eric Anholt673a3942008-07-30 12:06:12 -07002126 return 0;
2127}
2128
2129static int
Chris Wilson07f73f62009-09-14 16:50:30 +01002130i915_gem_evict_something(struct drm_device *dev, int min_size)
Eric Anholt673a3942008-07-30 12:06:12 -07002131{
2132 drm_i915_private_t *dev_priv = dev->dev_private;
2133 struct drm_gem_object *obj;
Chris Wilson07f73f62009-09-14 16:50:30 +01002134 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002135
Zou Nan hai852835f2010-05-21 09:08:56 +08002136 struct intel_ring_buffer *render_ring = &dev_priv->render_ring;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002137 struct intel_ring_buffer *bsd_ring = &dev_priv->bsd_ring;
Eric Anholt673a3942008-07-30 12:06:12 -07002138 for (;;) {
Zou Nan hai852835f2010-05-21 09:08:56 +08002139 i915_gem_retire_requests(dev, render_ring);
Chris Wilson07f73f62009-09-14 16:50:30 +01002140
Zou Nan haid1b851f2010-05-21 09:08:57 +08002141 if (HAS_BSD(dev))
2142 i915_gem_retire_requests(dev, bsd_ring);
2143
Eric Anholt673a3942008-07-30 12:06:12 -07002144 /* If there's an inactive buffer available now, grab it
2145 * and be done.
2146 */
Chris Wilson07f73f62009-09-14 16:50:30 +01002147 obj = i915_gem_find_inactive_object(dev, min_size);
2148 if (obj) {
2149 struct drm_i915_gem_object *obj_priv;
2150
Eric Anholt673a3942008-07-30 12:06:12 -07002151#if WATCH_LRU
2152 DRM_INFO("%s: evicting %p\n", __func__, obj);
2153#endif
Daniel Vetter23010e42010-03-08 13:35:02 +01002154 obj_priv = to_intel_bo(obj);
Chris Wilson07f73f62009-09-14 16:50:30 +01002155 BUG_ON(obj_priv->pin_count != 0);
Eric Anholt673a3942008-07-30 12:06:12 -07002156 BUG_ON(obj_priv->active);
2157
2158 /* Wait on the rendering and unbind the buffer. */
Chris Wilson07f73f62009-09-14 16:50:30 +01002159 return i915_gem_object_unbind(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002160 }
2161
2162 /* If we didn't get anything, but the ring is still processing
Chris Wilson07f73f62009-09-14 16:50:30 +01002163 * things, wait for the next to finish and hopefully leave us
2164 * a buffer to evict.
Eric Anholt673a3942008-07-30 12:06:12 -07002165 */
Zou Nan hai852835f2010-05-21 09:08:56 +08002166 if (!list_empty(&render_ring->request_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002167 struct drm_i915_gem_request *request;
2168
Zou Nan hai852835f2010-05-21 09:08:56 +08002169 request = list_first_entry(&render_ring->request_list,
Eric Anholt673a3942008-07-30 12:06:12 -07002170 struct drm_i915_gem_request,
2171 list);
2172
Zou Nan hai852835f2010-05-21 09:08:56 +08002173 ret = i915_wait_request(dev,
2174 request->seqno, request->ring);
Eric Anholt673a3942008-07-30 12:06:12 -07002175 if (ret)
Chris Wilson07f73f62009-09-14 16:50:30 +01002176 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002177
Chris Wilson07f73f62009-09-14 16:50:30 +01002178 continue;
Eric Anholt673a3942008-07-30 12:06:12 -07002179 }
2180
Zou Nan haid1b851f2010-05-21 09:08:57 +08002181 if (HAS_BSD(dev) && !list_empty(&bsd_ring->request_list)) {
2182 struct drm_i915_gem_request *request;
2183
2184 request = list_first_entry(&bsd_ring->request_list,
2185 struct drm_i915_gem_request,
2186 list);
2187
2188 ret = i915_wait_request(dev,
2189 request->seqno, request->ring);
2190 if (ret)
2191 return ret;
2192
2193 continue;
2194 }
2195
Eric Anholt673a3942008-07-30 12:06:12 -07002196 /* If we didn't have anything on the request list but there
2197 * are buffers awaiting a flush, emit one and try again.
2198 * When we wait on it, those buffers waiting for that flush
2199 * will get moved to inactive.
2200 */
2201 if (!list_empty(&dev_priv->mm.flushing_list)) {
Chris Wilson07f73f62009-09-14 16:50:30 +01002202 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07002203
Chris Wilson9a1e2582009-09-20 20:16:50 +01002204 /* Find an object that we can immediately reuse */
2205 list_for_each_entry(obj_priv, &dev_priv->mm.flushing_list, list) {
Daniel Vettera8089e82010-04-09 19:05:09 +00002206 obj = &obj_priv->base;
Chris Wilson9a1e2582009-09-20 20:16:50 +01002207 if (obj->size >= min_size)
2208 break;
Eric Anholt673a3942008-07-30 12:06:12 -07002209
Chris Wilson9a1e2582009-09-20 20:16:50 +01002210 obj = NULL;
2211 }
Eric Anholt673a3942008-07-30 12:06:12 -07002212
Chris Wilson9a1e2582009-09-20 20:16:50 +01002213 if (obj != NULL) {
2214 uint32_t seqno;
Chris Wilson07f73f62009-09-14 16:50:30 +01002215
Zou Nan hai852835f2010-05-21 09:08:56 +08002216 i915_gem_flush_ring(dev,
Chris Wilson9a1e2582009-09-20 20:16:50 +01002217 obj->write_domain,
Zou Nan hai852835f2010-05-21 09:08:56 +08002218 obj->write_domain,
2219 obj_priv->ring);
2220 seqno = i915_add_request(dev, NULL,
2221 obj->write_domain,
2222 obj_priv->ring);
Chris Wilson9a1e2582009-09-20 20:16:50 +01002223 if (seqno == 0)
2224 return -ENOMEM;
Chris Wilson9a1e2582009-09-20 20:16:50 +01002225 continue;
2226 }
Eric Anholt673a3942008-07-30 12:06:12 -07002227 }
2228
Chris Wilson07f73f62009-09-14 16:50:30 +01002229 /* If we didn't do any of the above, there's no single buffer
2230 * large enough to swap out for the new one, so just evict
2231 * everything and start again. (This should be rare.)
Eric Anholt673a3942008-07-30 12:06:12 -07002232 */
Chris Wilson97311292009-09-21 00:22:34 +01002233 if (!list_empty (&dev_priv->mm.inactive_list))
Chris Wilsonab5ee572009-09-20 19:25:47 +01002234 return i915_gem_evict_from_inactive_list(dev);
Chris Wilson97311292009-09-21 00:22:34 +01002235 else
Chris Wilson07f73f62009-09-14 16:50:30 +01002236 return i915_gem_evict_everything(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07002237 }
Keith Packardac94a962008-11-20 23:30:27 -08002238}
2239
Ben Gamari6911a9b2009-04-02 11:24:54 -07002240int
Chris Wilson4bdadb92010-01-27 13:36:32 +00002241i915_gem_object_get_pages(struct drm_gem_object *obj,
2242 gfp_t gfpmask)
Eric Anholt673a3942008-07-30 12:06:12 -07002243{
Daniel Vetter23010e42010-03-08 13:35:02 +01002244 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002245 int page_count, i;
2246 struct address_space *mapping;
2247 struct inode *inode;
2248 struct page *page;
Eric Anholt673a3942008-07-30 12:06:12 -07002249
Daniel Vetter778c3542010-05-13 11:49:44 +02002250 BUG_ON(obj_priv->pages_refcount
2251 == DRM_I915_GEM_OBJECT_MAX_PAGES_REFCOUNT);
2252
Eric Anholt856fa192009-03-19 14:10:50 -07002253 if (obj_priv->pages_refcount++ != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07002254 return 0;
2255
2256 /* Get the list of pages out of our struct file. They'll be pinned
2257 * at this point until we release them.
2258 */
2259 page_count = obj->size / PAGE_SIZE;
Eric Anholt856fa192009-03-19 14:10:50 -07002260 BUG_ON(obj_priv->pages != NULL);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07002261 obj_priv->pages = drm_calloc_large(page_count, sizeof(struct page *));
Eric Anholt856fa192009-03-19 14:10:50 -07002262 if (obj_priv->pages == NULL) {
Eric Anholt856fa192009-03-19 14:10:50 -07002263 obj_priv->pages_refcount--;
Eric Anholt673a3942008-07-30 12:06:12 -07002264 return -ENOMEM;
2265 }
2266
2267 inode = obj->filp->f_path.dentry->d_inode;
2268 mapping = inode->i_mapping;
2269 for (i = 0; i < page_count; i++) {
Chris Wilson4bdadb92010-01-27 13:36:32 +00002270 page = read_cache_page_gfp(mapping, i,
2271 mapping_gfp_mask (mapping) |
2272 __GFP_COLD |
2273 gfpmask);
Chris Wilson1f2b1012010-03-12 19:52:55 +00002274 if (IS_ERR(page))
2275 goto err_pages;
2276
Eric Anholt856fa192009-03-19 14:10:50 -07002277 obj_priv->pages[i] = page;
Eric Anholt673a3942008-07-30 12:06:12 -07002278 }
Eric Anholt280b7132009-03-12 16:56:27 -07002279
2280 if (obj_priv->tiling_mode != I915_TILING_NONE)
2281 i915_gem_object_do_bit_17_swizzle(obj);
2282
Eric Anholt673a3942008-07-30 12:06:12 -07002283 return 0;
Chris Wilson1f2b1012010-03-12 19:52:55 +00002284
2285err_pages:
2286 while (i--)
2287 page_cache_release(obj_priv->pages[i]);
2288
2289 drm_free_large(obj_priv->pages);
2290 obj_priv->pages = NULL;
2291 obj_priv->pages_refcount--;
2292 return PTR_ERR(page);
Eric Anholt673a3942008-07-30 12:06:12 -07002293}
2294
Eric Anholt4e901fd2009-10-26 16:44:17 -07002295static void sandybridge_write_fence_reg(struct drm_i915_fence_reg *reg)
2296{
2297 struct drm_gem_object *obj = reg->obj;
2298 struct drm_device *dev = obj->dev;
2299 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002300 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt4e901fd2009-10-26 16:44:17 -07002301 int regnum = obj_priv->fence_reg;
2302 uint64_t val;
2303
2304 val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
2305 0xfffff000) << 32;
2306 val |= obj_priv->gtt_offset & 0xfffff000;
2307 val |= (uint64_t)((obj_priv->stride / 128) - 1) <<
2308 SANDYBRIDGE_FENCE_PITCH_SHIFT;
2309
2310 if (obj_priv->tiling_mode == I915_TILING_Y)
2311 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2312 val |= I965_FENCE_REG_VALID;
2313
2314 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (regnum * 8), val);
2315}
2316
Jesse Barnesde151cf2008-11-12 10:03:55 -08002317static void i965_write_fence_reg(struct drm_i915_fence_reg *reg)
2318{
2319 struct drm_gem_object *obj = reg->obj;
2320 struct drm_device *dev = obj->dev;
2321 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002322 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002323 int regnum = obj_priv->fence_reg;
2324 uint64_t val;
2325
2326 val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
2327 0xfffff000) << 32;
2328 val |= obj_priv->gtt_offset & 0xfffff000;
2329 val |= ((obj_priv->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
2330 if (obj_priv->tiling_mode == I915_TILING_Y)
2331 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2332 val |= I965_FENCE_REG_VALID;
2333
2334 I915_WRITE64(FENCE_REG_965_0 + (regnum * 8), val);
2335}
2336
2337static void i915_write_fence_reg(struct drm_i915_fence_reg *reg)
2338{
2339 struct drm_gem_object *obj = reg->obj;
2340 struct drm_device *dev = obj->dev;
2341 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002342 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002343 int regnum = obj_priv->fence_reg;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002344 int tile_width;
Eric Anholtdc529a42009-03-10 22:34:49 -07002345 uint32_t fence_reg, val;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002346 uint32_t pitch_val;
2347
2348 if ((obj_priv->gtt_offset & ~I915_FENCE_START_MASK) ||
2349 (obj_priv->gtt_offset & (obj->size - 1))) {
Linus Torvaldsf06da262009-02-09 08:57:29 -08002350 WARN(1, "%s: object 0x%08x not 1M or size (0x%zx) aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002351 __func__, obj_priv->gtt_offset, obj->size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002352 return;
2353 }
2354
Jesse Barnes0f973f22009-01-26 17:10:45 -08002355 if (obj_priv->tiling_mode == I915_TILING_Y &&
2356 HAS_128_BYTE_Y_TILING(dev))
2357 tile_width = 128;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002358 else
Jesse Barnes0f973f22009-01-26 17:10:45 -08002359 tile_width = 512;
2360
2361 /* Note: pitch better be a power of two tile widths */
2362 pitch_val = obj_priv->stride / tile_width;
2363 pitch_val = ffs(pitch_val) - 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002364
Daniel Vetterc36a2a62010-04-17 15:12:03 +02002365 if (obj_priv->tiling_mode == I915_TILING_Y &&
2366 HAS_128_BYTE_Y_TILING(dev))
2367 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2368 else
2369 WARN_ON(pitch_val > I915_FENCE_MAX_PITCH_VAL);
2370
Jesse Barnesde151cf2008-11-12 10:03:55 -08002371 val = obj_priv->gtt_offset;
2372 if (obj_priv->tiling_mode == I915_TILING_Y)
2373 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2374 val |= I915_FENCE_SIZE_BITS(obj->size);
2375 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2376 val |= I830_FENCE_REG_VALID;
2377
Eric Anholtdc529a42009-03-10 22:34:49 -07002378 if (regnum < 8)
2379 fence_reg = FENCE_REG_830_0 + (regnum * 4);
2380 else
2381 fence_reg = FENCE_REG_945_8 + ((regnum - 8) * 4);
2382 I915_WRITE(fence_reg, val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002383}
2384
2385static void i830_write_fence_reg(struct drm_i915_fence_reg *reg)
2386{
2387 struct drm_gem_object *obj = reg->obj;
2388 struct drm_device *dev = obj->dev;
2389 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002390 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002391 int regnum = obj_priv->fence_reg;
2392 uint32_t val;
2393 uint32_t pitch_val;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002394 uint32_t fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002395
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002396 if ((obj_priv->gtt_offset & ~I830_FENCE_START_MASK) ||
Jesse Barnesde151cf2008-11-12 10:03:55 -08002397 (obj_priv->gtt_offset & (obj->size - 1))) {
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002398 WARN(1, "%s: object 0x%08x not 512K or size aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002399 __func__, obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002400 return;
2401 }
2402
Eric Anholte76a16d2009-05-26 17:44:56 -07002403 pitch_val = obj_priv->stride / 128;
2404 pitch_val = ffs(pitch_val) - 1;
2405 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2406
Jesse Barnesde151cf2008-11-12 10:03:55 -08002407 val = obj_priv->gtt_offset;
2408 if (obj_priv->tiling_mode == I915_TILING_Y)
2409 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002410 fence_size_bits = I830_FENCE_SIZE_BITS(obj->size);
2411 WARN_ON(fence_size_bits & ~0x00000f00);
2412 val |= fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002413 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2414 val |= I830_FENCE_REG_VALID;
2415
2416 I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002417}
2418
Daniel Vetterae3db242010-02-19 11:51:58 +01002419static int i915_find_fence_reg(struct drm_device *dev)
2420{
2421 struct drm_i915_fence_reg *reg = NULL;
2422 struct drm_i915_gem_object *obj_priv = NULL;
2423 struct drm_i915_private *dev_priv = dev->dev_private;
2424 struct drm_gem_object *obj = NULL;
2425 int i, avail, ret;
2426
2427 /* First try to find a free reg */
2428 avail = 0;
2429 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2430 reg = &dev_priv->fence_regs[i];
2431 if (!reg->obj)
2432 return i;
2433
Daniel Vetter23010e42010-03-08 13:35:02 +01002434 obj_priv = to_intel_bo(reg->obj);
Daniel Vetterae3db242010-02-19 11:51:58 +01002435 if (!obj_priv->pin_count)
2436 avail++;
2437 }
2438
2439 if (avail == 0)
2440 return -ENOSPC;
2441
2442 /* None available, try to steal one or wait for a user to finish */
2443 i = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002444 list_for_each_entry(reg, &dev_priv->mm.fence_list,
2445 lru_list) {
2446 obj = reg->obj;
2447 obj_priv = to_intel_bo(obj);
Daniel Vetterae3db242010-02-19 11:51:58 +01002448
2449 if (obj_priv->pin_count)
2450 continue;
2451
2452 /* found one! */
2453 i = obj_priv->fence_reg;
2454 break;
2455 }
2456
2457 BUG_ON(i == I915_FENCE_REG_NONE);
2458
2459 /* We only have a reference on obj from the active list. put_fence_reg
2460 * might drop that one, causing a use-after-free in it. So hold a
2461 * private reference to obj like the other callers of put_fence_reg
2462 * (set_tiling ioctl) do. */
2463 drm_gem_object_reference(obj);
2464 ret = i915_gem_object_put_fence_reg(obj);
2465 drm_gem_object_unreference(obj);
2466 if (ret != 0)
2467 return ret;
2468
2469 return i;
2470}
2471
Jesse Barnesde151cf2008-11-12 10:03:55 -08002472/**
2473 * i915_gem_object_get_fence_reg - set up a fence reg for an object
2474 * @obj: object to map through a fence reg
2475 *
2476 * When mapping objects through the GTT, userspace wants to be able to write
2477 * to them without having to worry about swizzling if the object is tiled.
2478 *
2479 * This function walks the fence regs looking for a free one for @obj,
2480 * stealing one if it can't find any.
2481 *
2482 * It then sets up the reg based on the object's properties: address, pitch
2483 * and tiling format.
2484 */
Chris Wilson8c4b8c32009-06-17 22:08:52 +01002485int
2486i915_gem_object_get_fence_reg(struct drm_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002487{
2488 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002489 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002490 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002491 struct drm_i915_fence_reg *reg = NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002492 int ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002493
Eric Anholta09ba7f2009-08-29 12:49:51 -07002494 /* Just update our place in the LRU if our fence is getting used. */
2495 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002496 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
2497 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002498 return 0;
2499 }
2500
Jesse Barnesde151cf2008-11-12 10:03:55 -08002501 switch (obj_priv->tiling_mode) {
2502 case I915_TILING_NONE:
2503 WARN(1, "allocating a fence for non-tiled object?\n");
2504 break;
2505 case I915_TILING_X:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002506 if (!obj_priv->stride)
2507 return -EINVAL;
2508 WARN((obj_priv->stride & (512 - 1)),
2509 "object 0x%08x is X tiled but has non-512B pitch\n",
2510 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002511 break;
2512 case I915_TILING_Y:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002513 if (!obj_priv->stride)
2514 return -EINVAL;
2515 WARN((obj_priv->stride & (128 - 1)),
2516 "object 0x%08x is Y tiled but has non-128B pitch\n",
2517 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002518 break;
2519 }
2520
Daniel Vetterae3db242010-02-19 11:51:58 +01002521 ret = i915_find_fence_reg(dev);
2522 if (ret < 0)
2523 return ret;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002524
Daniel Vetterae3db242010-02-19 11:51:58 +01002525 obj_priv->fence_reg = ret;
2526 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002527 list_add_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002528
Jesse Barnesde151cf2008-11-12 10:03:55 -08002529 reg->obj = obj;
2530
Eric Anholt4e901fd2009-10-26 16:44:17 -07002531 if (IS_GEN6(dev))
2532 sandybridge_write_fence_reg(reg);
2533 else if (IS_I965G(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08002534 i965_write_fence_reg(reg);
2535 else if (IS_I9XX(dev))
2536 i915_write_fence_reg(reg);
2537 else
2538 i830_write_fence_reg(reg);
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002539
Daniel Vetterae3db242010-02-19 11:51:58 +01002540 trace_i915_gem_object_get_fence(obj, obj_priv->fence_reg,
2541 obj_priv->tiling_mode);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002542
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002543 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002544}
2545
2546/**
2547 * i915_gem_clear_fence_reg - clear out fence register info
2548 * @obj: object to clear
2549 *
2550 * Zeroes out the fence register itself and clears out the associated
2551 * data structures in dev_priv and obj_priv.
2552 */
2553static void
2554i915_gem_clear_fence_reg(struct drm_gem_object *obj)
2555{
2556 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002557 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002558 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002559 struct drm_i915_fence_reg *reg =
2560 &dev_priv->fence_regs[obj_priv->fence_reg];
Jesse Barnesde151cf2008-11-12 10:03:55 -08002561
Eric Anholt4e901fd2009-10-26 16:44:17 -07002562 if (IS_GEN6(dev)) {
2563 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 +
2564 (obj_priv->fence_reg * 8), 0);
2565 } else if (IS_I965G(dev)) {
Jesse Barnesde151cf2008-11-12 10:03:55 -08002566 I915_WRITE64(FENCE_REG_965_0 + (obj_priv->fence_reg * 8), 0);
Eric Anholt4e901fd2009-10-26 16:44:17 -07002567 } else {
Eric Anholtdc529a42009-03-10 22:34:49 -07002568 uint32_t fence_reg;
2569
2570 if (obj_priv->fence_reg < 8)
2571 fence_reg = FENCE_REG_830_0 + obj_priv->fence_reg * 4;
2572 else
2573 fence_reg = FENCE_REG_945_8 + (obj_priv->fence_reg -
2574 8) * 4;
2575
2576 I915_WRITE(fence_reg, 0);
2577 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002578
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002579 reg->obj = NULL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002580 obj_priv->fence_reg = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002581 list_del_init(&reg->lru_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002582}
2583
Eric Anholt673a3942008-07-30 12:06:12 -07002584/**
Chris Wilson52dc7d32009-06-06 09:46:01 +01002585 * i915_gem_object_put_fence_reg - waits on outstanding fenced access
2586 * to the buffer to finish, and then resets the fence register.
2587 * @obj: tiled object holding a fence register.
2588 *
2589 * Zeroes out the fence register itself and clears out the associated
2590 * data structures in dev_priv and obj_priv.
2591 */
2592int
2593i915_gem_object_put_fence_reg(struct drm_gem_object *obj)
2594{
2595 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01002596 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson52dc7d32009-06-06 09:46:01 +01002597
2598 if (obj_priv->fence_reg == I915_FENCE_REG_NONE)
2599 return 0;
2600
Daniel Vetter10ae9bd2010-02-01 13:59:17 +01002601 /* If we've changed tiling, GTT-mappings of the object
2602 * need to re-fault to ensure that the correct fence register
2603 * setup is in place.
2604 */
2605 i915_gem_release_mmap(obj);
2606
Chris Wilson52dc7d32009-06-06 09:46:01 +01002607 /* On the i915, GPU access to tiled buffers is via a fence,
2608 * therefore we must wait for any outstanding access to complete
2609 * before clearing the fence.
2610 */
2611 if (!IS_I965G(dev)) {
2612 int ret;
2613
2614 i915_gem_object_flush_gpu_write_domain(obj);
Chris Wilson52dc7d32009-06-06 09:46:01 +01002615 ret = i915_gem_object_wait_rendering(obj);
2616 if (ret != 0)
2617 return ret;
2618 }
2619
Daniel Vetter4a726612010-02-01 13:59:16 +01002620 i915_gem_object_flush_gtt_write_domain(obj);
Chris Wilson52dc7d32009-06-06 09:46:01 +01002621 i915_gem_clear_fence_reg (obj);
2622
2623 return 0;
2624}
2625
2626/**
Eric Anholt673a3942008-07-30 12:06:12 -07002627 * Finds free space in the GTT aperture and binds the object there.
2628 */
2629static int
2630i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment)
2631{
2632 struct drm_device *dev = obj->dev;
2633 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002634 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002635 struct drm_mm_node *free_space;
Chris Wilson4bdadb92010-01-27 13:36:32 +00002636 gfp_t gfpmask = __GFP_NORETRY | __GFP_NOWARN;
Chris Wilson07f73f62009-09-14 16:50:30 +01002637 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002638
Chris Wilsonbb6baf72009-09-22 14:24:13 +01002639 if (obj_priv->madv != I915_MADV_WILLNEED) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01002640 DRM_ERROR("Attempting to bind a purgeable object\n");
2641 return -EINVAL;
2642 }
2643
Eric Anholt673a3942008-07-30 12:06:12 -07002644 if (alignment == 0)
Jesse Barnes0f973f22009-01-26 17:10:45 -08002645 alignment = i915_gem_get_gtt_alignment(obj);
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002646 if (alignment & (i915_gem_get_gtt_alignment(obj) - 1)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002647 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2648 return -EINVAL;
2649 }
2650
Chris Wilson654fc602010-05-27 13:18:21 +01002651 /* If the object is bigger than the entire aperture, reject it early
2652 * before evicting everything in a vain attempt to find space.
2653 */
2654 if (obj->size > dev->gtt_total) {
2655 DRM_ERROR("Attempting to bind an object larger than the aperture\n");
2656 return -E2BIG;
2657 }
2658
Eric Anholt673a3942008-07-30 12:06:12 -07002659 search_free:
2660 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
2661 obj->size, alignment, 0);
2662 if (free_space != NULL) {
2663 obj_priv->gtt_space = drm_mm_get_block(free_space, obj->size,
2664 alignment);
2665 if (obj_priv->gtt_space != NULL) {
2666 obj_priv->gtt_space->private = obj;
2667 obj_priv->gtt_offset = obj_priv->gtt_space->start;
2668 }
2669 }
2670 if (obj_priv->gtt_space == NULL) {
2671 /* If the gtt is empty and we're still having trouble
2672 * fitting our object in, we're out of memory.
2673 */
2674#if WATCH_LRU
2675 DRM_INFO("%s: GTT full, evicting something\n", __func__);
2676#endif
Chris Wilson07f73f62009-09-14 16:50:30 +01002677 ret = i915_gem_evict_something(dev, obj->size);
Chris Wilson97311292009-09-21 00:22:34 +01002678 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002679 return ret;
Chris Wilson97311292009-09-21 00:22:34 +01002680
Eric Anholt673a3942008-07-30 12:06:12 -07002681 goto search_free;
2682 }
2683
2684#if WATCH_BUF
Krzysztof Halasacfd43c02009-06-20 00:31:28 +02002685 DRM_INFO("Binding object of size %zd at 0x%08x\n",
Eric Anholt673a3942008-07-30 12:06:12 -07002686 obj->size, obj_priv->gtt_offset);
2687#endif
Chris Wilson4bdadb92010-01-27 13:36:32 +00002688 ret = i915_gem_object_get_pages(obj, gfpmask);
Eric Anholt673a3942008-07-30 12:06:12 -07002689 if (ret) {
2690 drm_mm_put_block(obj_priv->gtt_space);
2691 obj_priv->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002692
2693 if (ret == -ENOMEM) {
2694 /* first try to clear up some space from the GTT */
2695 ret = i915_gem_evict_something(dev, obj->size);
2696 if (ret) {
Chris Wilson07f73f62009-09-14 16:50:30 +01002697 /* now try to shrink everyone else */
Chris Wilson4bdadb92010-01-27 13:36:32 +00002698 if (gfpmask) {
2699 gfpmask = 0;
2700 goto search_free;
Chris Wilson07f73f62009-09-14 16:50:30 +01002701 }
2702
2703 return ret;
2704 }
2705
2706 goto search_free;
2707 }
2708
Eric Anholt673a3942008-07-30 12:06:12 -07002709 return ret;
2710 }
2711
Eric Anholt673a3942008-07-30 12:06:12 -07002712 /* Create an AGP memory structure pointing at our pages, and bind it
2713 * into the GTT.
2714 */
2715 obj_priv->agp_mem = drm_agp_bind_pages(dev,
Eric Anholt856fa192009-03-19 14:10:50 -07002716 obj_priv->pages,
Chris Wilson07f73f62009-09-14 16:50:30 +01002717 obj->size >> PAGE_SHIFT,
Keith Packardba1eb1d2008-10-14 19:55:10 -07002718 obj_priv->gtt_offset,
2719 obj_priv->agp_type);
Eric Anholt673a3942008-07-30 12:06:12 -07002720 if (obj_priv->agp_mem == NULL) {
Eric Anholt856fa192009-03-19 14:10:50 -07002721 i915_gem_object_put_pages(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002722 drm_mm_put_block(obj_priv->gtt_space);
2723 obj_priv->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002724
2725 ret = i915_gem_evict_something(dev, obj->size);
Chris Wilson97311292009-09-21 00:22:34 +01002726 if (ret)
Chris Wilson07f73f62009-09-14 16:50:30 +01002727 return ret;
Chris Wilson07f73f62009-09-14 16:50:30 +01002728
2729 goto search_free;
Eric Anholt673a3942008-07-30 12:06:12 -07002730 }
2731 atomic_inc(&dev->gtt_count);
2732 atomic_add(obj->size, &dev->gtt_memory);
2733
2734 /* Assert that the object is not currently in any GPU domain. As it
2735 * wasn't in the GTT, there shouldn't be any way it could have been in
2736 * a GPU cache
2737 */
Chris Wilson21d509e2009-06-06 09:46:02 +01002738 BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS);
2739 BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS);
Eric Anholt673a3942008-07-30 12:06:12 -07002740
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002741 trace_i915_gem_object_bind(obj, obj_priv->gtt_offset);
2742
Eric Anholt673a3942008-07-30 12:06:12 -07002743 return 0;
2744}
2745
2746void
2747i915_gem_clflush_object(struct drm_gem_object *obj)
2748{
Daniel Vetter23010e42010-03-08 13:35:02 +01002749 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002750
2751 /* If we don't have a page list set up, then we're not pinned
2752 * to GPU, and we can ignore the cache flush because it'll happen
2753 * again at bind time.
2754 */
Eric Anholt856fa192009-03-19 14:10:50 -07002755 if (obj_priv->pages == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002756 return;
2757
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002758 trace_i915_gem_object_clflush(obj);
Eric Anholtcfa16a02009-05-26 18:46:16 -07002759
Eric Anholt856fa192009-03-19 14:10:50 -07002760 drm_clflush_pages(obj_priv->pages, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002761}
2762
Eric Anholte47c68e2008-11-14 13:35:19 -08002763/** Flushes any GPU write domain for the object if it's dirty. */
2764static void
2765i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj)
2766{
2767 struct drm_device *dev = obj->dev;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002768 uint32_t old_write_domain;
Zou Nan hai852835f2010-05-21 09:08:56 +08002769 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08002770
2771 if ((obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
2772 return;
2773
2774 /* Queue the GPU write cache flushing we need. */
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002775 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002776 i915_gem_flush(dev, 0, obj->write_domain);
Zou Nan hai852835f2010-05-21 09:08:56 +08002777 (void) i915_add_request(dev, NULL, obj->write_domain, obj_priv->ring);
Daniel Vetter99fcb762010-02-07 16:20:18 +01002778 BUG_ON(obj->write_domain);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002779
2780 trace_i915_gem_object_change_domain(obj,
2781 obj->read_domains,
2782 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002783}
2784
2785/** Flushes the GTT write domain for the object if it's dirty. */
2786static void
2787i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj)
2788{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002789 uint32_t old_write_domain;
2790
Eric Anholte47c68e2008-11-14 13:35:19 -08002791 if (obj->write_domain != I915_GEM_DOMAIN_GTT)
2792 return;
2793
2794 /* No actual flushing is required for the GTT write domain. Writes
2795 * to it immediately go to main memory as far as we know, so there's
2796 * no chipset flush. It also doesn't land in render cache.
2797 */
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002798 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002799 obj->write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002800
2801 trace_i915_gem_object_change_domain(obj,
2802 obj->read_domains,
2803 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002804}
2805
2806/** Flushes the CPU write domain for the object if it's dirty. */
2807static void
2808i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj)
2809{
2810 struct drm_device *dev = obj->dev;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002811 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002812
2813 if (obj->write_domain != I915_GEM_DOMAIN_CPU)
2814 return;
2815
2816 i915_gem_clflush_object(obj);
2817 drm_agp_chipset_flush(dev);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002818 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002819 obj->write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002820
2821 trace_i915_gem_object_change_domain(obj,
2822 obj->read_domains,
2823 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002824}
2825
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002826void
2827i915_gem_object_flush_write_domain(struct drm_gem_object *obj)
2828{
2829 switch (obj->write_domain) {
2830 case I915_GEM_DOMAIN_GTT:
2831 i915_gem_object_flush_gtt_write_domain(obj);
2832 break;
2833 case I915_GEM_DOMAIN_CPU:
2834 i915_gem_object_flush_cpu_write_domain(obj);
2835 break;
2836 default:
2837 i915_gem_object_flush_gpu_write_domain(obj);
2838 break;
2839 }
2840}
2841
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002842/**
2843 * Moves a single object to the GTT read, and possibly write domain.
2844 *
2845 * This function returns when the move is complete, including waiting on
2846 * flushes to occur.
2847 */
Jesse Barnes79e53942008-11-07 14:24:08 -08002848int
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002849i915_gem_object_set_to_gtt_domain(struct drm_gem_object *obj, int write)
2850{
Daniel Vetter23010e42010-03-08 13:35:02 +01002851 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002852 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002853 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002854
Eric Anholt02354392008-11-26 13:58:13 -08002855 /* Not valid to be called on unbound objects. */
2856 if (obj_priv->gtt_space == NULL)
2857 return -EINVAL;
2858
Eric Anholte47c68e2008-11-14 13:35:19 -08002859 i915_gem_object_flush_gpu_write_domain(obj);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002860 /* Wait on any GPU rendering and flushing to occur. */
Eric Anholte47c68e2008-11-14 13:35:19 -08002861 ret = i915_gem_object_wait_rendering(obj);
2862 if (ret != 0)
2863 return ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002864
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002865 old_write_domain = obj->write_domain;
2866 old_read_domains = obj->read_domains;
2867
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002868 /* If we're writing through the GTT domain, then CPU and GPU caches
2869 * will need to be invalidated at next use.
2870 */
2871 if (write)
Eric Anholte47c68e2008-11-14 13:35:19 -08002872 obj->read_domains &= I915_GEM_DOMAIN_GTT;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002873
Eric Anholte47c68e2008-11-14 13:35:19 -08002874 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002875
2876 /* It should now be out of any other write domains, and we can update
2877 * the domain values for our changes.
2878 */
2879 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2880 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002881 if (write) {
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002882 obj->write_domain = I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002883 obj_priv->dirty = 1;
2884 }
2885
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002886 trace_i915_gem_object_change_domain(obj,
2887 old_read_domains,
2888 old_write_domain);
2889
Eric Anholte47c68e2008-11-14 13:35:19 -08002890 return 0;
2891}
2892
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002893/*
2894 * Prepare buffer for display plane. Use uninterruptible for possible flush
2895 * wait, as in modesetting process we're not supposed to be interrupted.
2896 */
2897int
2898i915_gem_object_set_to_display_plane(struct drm_gem_object *obj)
2899{
2900 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01002901 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002902 uint32_t old_write_domain, old_read_domains;
2903 int ret;
2904
2905 /* Not valid to be called on unbound objects. */
2906 if (obj_priv->gtt_space == NULL)
2907 return -EINVAL;
2908
2909 i915_gem_object_flush_gpu_write_domain(obj);
2910
2911 /* Wait on any GPU rendering and flushing to occur. */
2912 if (obj_priv->active) {
2913#if WATCH_BUF
2914 DRM_INFO("%s: object %p wait for seqno %08x\n",
2915 __func__, obj, obj_priv->last_rendering_seqno);
2916#endif
Zou Nan hai852835f2010-05-21 09:08:56 +08002917 ret = i915_do_wait_request(dev,
2918 obj_priv->last_rendering_seqno,
2919 0,
2920 obj_priv->ring);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002921 if (ret != 0)
2922 return ret;
2923 }
2924
Chris Wilsonb118c1e2010-05-27 13:18:14 +01002925 i915_gem_object_flush_cpu_write_domain(obj);
2926
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002927 old_write_domain = obj->write_domain;
2928 old_read_domains = obj->read_domains;
2929
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002930 /* It should now be out of any other write domains, and we can update
2931 * the domain values for our changes.
2932 */
2933 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
Chris Wilsonb118c1e2010-05-27 13:18:14 +01002934 obj->read_domains = I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002935 obj->write_domain = I915_GEM_DOMAIN_GTT;
2936 obj_priv->dirty = 1;
2937
2938 trace_i915_gem_object_change_domain(obj,
2939 old_read_domains,
2940 old_write_domain);
2941
2942 return 0;
2943}
2944
Eric Anholte47c68e2008-11-14 13:35:19 -08002945/**
2946 * Moves a single object to the CPU read, and possibly write domain.
2947 *
2948 * This function returns when the move is complete, including waiting on
2949 * flushes to occur.
2950 */
2951static int
2952i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj, int write)
2953{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002954 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002955 int ret;
2956
2957 i915_gem_object_flush_gpu_write_domain(obj);
2958 /* Wait on any GPU rendering and flushing to occur. */
2959 ret = i915_gem_object_wait_rendering(obj);
2960 if (ret != 0)
2961 return ret;
2962
2963 i915_gem_object_flush_gtt_write_domain(obj);
2964
2965 /* If we have a partially-valid cache of the object in the CPU,
2966 * finish invalidating it and free the per-page flags.
2967 */
2968 i915_gem_object_set_to_full_cpu_read_domain(obj);
2969
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002970 old_write_domain = obj->write_domain;
2971 old_read_domains = obj->read_domains;
2972
Eric Anholte47c68e2008-11-14 13:35:19 -08002973 /* Flush the CPU cache if it's still invalid. */
2974 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
2975 i915_gem_clflush_object(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08002976
2977 obj->read_domains |= I915_GEM_DOMAIN_CPU;
2978 }
2979
2980 /* It should now be out of any other write domains, and we can update
2981 * the domain values for our changes.
2982 */
2983 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
2984
2985 /* If we're writing through the CPU, then the GPU read domains will
2986 * need to be invalidated at next use.
2987 */
2988 if (write) {
2989 obj->read_domains &= I915_GEM_DOMAIN_CPU;
2990 obj->write_domain = I915_GEM_DOMAIN_CPU;
2991 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002992
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002993 trace_i915_gem_object_change_domain(obj,
2994 old_read_domains,
2995 old_write_domain);
2996
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002997 return 0;
2998}
2999
Eric Anholt673a3942008-07-30 12:06:12 -07003000/*
3001 * Set the next domain for the specified object. This
3002 * may not actually perform the necessary flushing/invaliding though,
3003 * as that may want to be batched with other set_domain operations
3004 *
3005 * This is (we hope) the only really tricky part of gem. The goal
3006 * is fairly simple -- track which caches hold bits of the object
3007 * and make sure they remain coherent. A few concrete examples may
3008 * help to explain how it works. For shorthand, we use the notation
3009 * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
3010 * a pair of read and write domain masks.
3011 *
3012 * Case 1: the batch buffer
3013 *
3014 * 1. Allocated
3015 * 2. Written by CPU
3016 * 3. Mapped to GTT
3017 * 4. Read by GPU
3018 * 5. Unmapped from GTT
3019 * 6. Freed
3020 *
3021 * Let's take these a step at a time
3022 *
3023 * 1. Allocated
3024 * Pages allocated from the kernel may still have
3025 * cache contents, so we set them to (CPU, CPU) always.
3026 * 2. Written by CPU (using pwrite)
3027 * The pwrite function calls set_domain (CPU, CPU) and
3028 * this function does nothing (as nothing changes)
3029 * 3. Mapped by GTT
3030 * This function asserts that the object is not
3031 * currently in any GPU-based read or write domains
3032 * 4. Read by GPU
3033 * i915_gem_execbuffer calls set_domain (COMMAND, 0).
3034 * As write_domain is zero, this function adds in the
3035 * current read domains (CPU+COMMAND, 0).
3036 * flush_domains is set to CPU.
3037 * invalidate_domains is set to COMMAND
3038 * clflush is run to get data out of the CPU caches
3039 * then i915_dev_set_domain calls i915_gem_flush to
3040 * emit an MI_FLUSH and drm_agp_chipset_flush
3041 * 5. Unmapped from GTT
3042 * i915_gem_object_unbind calls set_domain (CPU, CPU)
3043 * flush_domains and invalidate_domains end up both zero
3044 * so no flushing/invalidating happens
3045 * 6. Freed
3046 * yay, done
3047 *
3048 * Case 2: The shared render buffer
3049 *
3050 * 1. Allocated
3051 * 2. Mapped to GTT
3052 * 3. Read/written by GPU
3053 * 4. set_domain to (CPU,CPU)
3054 * 5. Read/written by CPU
3055 * 6. Read/written by GPU
3056 *
3057 * 1. Allocated
3058 * Same as last example, (CPU, CPU)
3059 * 2. Mapped to GTT
3060 * Nothing changes (assertions find that it is not in the GPU)
3061 * 3. Read/written by GPU
3062 * execbuffer calls set_domain (RENDER, RENDER)
3063 * flush_domains gets CPU
3064 * invalidate_domains gets GPU
3065 * clflush (obj)
3066 * MI_FLUSH and drm_agp_chipset_flush
3067 * 4. set_domain (CPU, CPU)
3068 * flush_domains gets GPU
3069 * invalidate_domains gets CPU
3070 * wait_rendering (obj) to make sure all drawing is complete.
3071 * This will include an MI_FLUSH to get the data from GPU
3072 * to memory
3073 * clflush (obj) to invalidate the CPU cache
3074 * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
3075 * 5. Read/written by CPU
3076 * cache lines are loaded and dirtied
3077 * 6. Read written by GPU
3078 * Same as last GPU access
3079 *
3080 * Case 3: The constant buffer
3081 *
3082 * 1. Allocated
3083 * 2. Written by CPU
3084 * 3. Read by GPU
3085 * 4. Updated (written) by CPU again
3086 * 5. Read by GPU
3087 *
3088 * 1. Allocated
3089 * (CPU, CPU)
3090 * 2. Written by CPU
3091 * (CPU, CPU)
3092 * 3. Read by GPU
3093 * (CPU+RENDER, 0)
3094 * flush_domains = CPU
3095 * invalidate_domains = RENDER
3096 * clflush (obj)
3097 * MI_FLUSH
3098 * drm_agp_chipset_flush
3099 * 4. Updated (written) by CPU again
3100 * (CPU, CPU)
3101 * flush_domains = 0 (no previous write domain)
3102 * invalidate_domains = 0 (no new read domains)
3103 * 5. Read by GPU
3104 * (CPU+RENDER, 0)
3105 * flush_domains = CPU
3106 * invalidate_domains = RENDER
3107 * clflush (obj)
3108 * MI_FLUSH
3109 * drm_agp_chipset_flush
3110 */
Keith Packardc0d90822008-11-20 23:11:08 -08003111static void
Eric Anholt8b0e3782009-02-19 14:40:50 -08003112i915_gem_object_set_to_gpu_domain(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07003113{
3114 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01003115 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003116 uint32_t invalidate_domains = 0;
3117 uint32_t flush_domains = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003118 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003119
Eric Anholt8b0e3782009-02-19 14:40:50 -08003120 BUG_ON(obj->pending_read_domains & I915_GEM_DOMAIN_CPU);
3121 BUG_ON(obj->pending_write_domain == I915_GEM_DOMAIN_CPU);
Eric Anholt673a3942008-07-30 12:06:12 -07003122
Jesse Barnes652c3932009-08-17 13:31:43 -07003123 intel_mark_busy(dev, obj);
3124
Eric Anholt673a3942008-07-30 12:06:12 -07003125#if WATCH_BUF
3126 DRM_INFO("%s: object %p read %08x -> %08x write %08x -> %08x\n",
3127 __func__, obj,
Eric Anholt8b0e3782009-02-19 14:40:50 -08003128 obj->read_domains, obj->pending_read_domains,
3129 obj->write_domain, obj->pending_write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07003130#endif
3131 /*
3132 * If the object isn't moving to a new write domain,
3133 * let the object stay in multiple read domains
3134 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003135 if (obj->pending_write_domain == 0)
3136 obj->pending_read_domains |= obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07003137 else
3138 obj_priv->dirty = 1;
3139
3140 /*
3141 * Flush the current write domain if
3142 * the new read domains don't match. Invalidate
3143 * any read domains which differ from the old
3144 * write domain
3145 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003146 if (obj->write_domain &&
3147 obj->write_domain != obj->pending_read_domains) {
Eric Anholt673a3942008-07-30 12:06:12 -07003148 flush_domains |= obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08003149 invalidate_domains |=
3150 obj->pending_read_domains & ~obj->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07003151 }
3152 /*
3153 * Invalidate any read caches which may have
3154 * stale data. That is, any new read domains.
3155 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003156 invalidate_domains |= obj->pending_read_domains & ~obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07003157 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU) {
3158#if WATCH_BUF
3159 DRM_INFO("%s: CPU domain flush %08x invalidate %08x\n",
3160 __func__, flush_domains, invalidate_domains);
3161#endif
Eric Anholt673a3942008-07-30 12:06:12 -07003162 i915_gem_clflush_object(obj);
3163 }
3164
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003165 old_read_domains = obj->read_domains;
3166
Eric Anholtefbeed92009-02-19 14:54:51 -08003167 /* The actual obj->write_domain will be updated with
3168 * pending_write_domain after we emit the accumulated flush for all
3169 * of our domain changes in execbuffers (which clears objects'
3170 * write_domains). So if we have a current write domain that we
3171 * aren't changing, set pending_write_domain to that.
3172 */
3173 if (flush_domains == 0 && obj->pending_write_domain == 0)
3174 obj->pending_write_domain = obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08003175 obj->read_domains = obj->pending_read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07003176
3177 dev->invalidate_domains |= invalidate_domains;
3178 dev->flush_domains |= flush_domains;
3179#if WATCH_BUF
3180 DRM_INFO("%s: read %08x write %08x invalidate %08x flush %08x\n",
3181 __func__,
3182 obj->read_domains, obj->write_domain,
3183 dev->invalidate_domains, dev->flush_domains);
3184#endif
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003185
3186 trace_i915_gem_object_change_domain(obj,
3187 old_read_domains,
3188 obj->write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07003189}
3190
3191/**
Eric Anholte47c68e2008-11-14 13:35:19 -08003192 * Moves the object from a partially CPU read to a full one.
Eric Anholt673a3942008-07-30 12:06:12 -07003193 *
Eric Anholte47c68e2008-11-14 13:35:19 -08003194 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
3195 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
3196 */
3197static void
3198i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj)
3199{
Daniel Vetter23010e42010-03-08 13:35:02 +01003200 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003201
3202 if (!obj_priv->page_cpu_valid)
3203 return;
3204
3205 /* If we're partially in the CPU read domain, finish moving it in.
3206 */
3207 if (obj->read_domains & I915_GEM_DOMAIN_CPU) {
3208 int i;
3209
3210 for (i = 0; i <= (obj->size - 1) / PAGE_SIZE; i++) {
3211 if (obj_priv->page_cpu_valid[i])
3212 continue;
Eric Anholt856fa192009-03-19 14:10:50 -07003213 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholte47c68e2008-11-14 13:35:19 -08003214 }
Eric Anholte47c68e2008-11-14 13:35:19 -08003215 }
3216
3217 /* Free the page_cpu_valid mappings which are now stale, whether
3218 * or not we've got I915_GEM_DOMAIN_CPU.
3219 */
Eric Anholt9a298b22009-03-24 12:23:04 -07003220 kfree(obj_priv->page_cpu_valid);
Eric Anholte47c68e2008-11-14 13:35:19 -08003221 obj_priv->page_cpu_valid = NULL;
3222}
3223
3224/**
3225 * Set the CPU read domain on a range of the object.
3226 *
3227 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
3228 * not entirely valid. The page_cpu_valid member of the object flags which
3229 * pages have been flushed, and will be respected by
3230 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
3231 * of the whole object.
3232 *
3233 * This function returns when the move is complete, including waiting on
3234 * flushes to occur.
Eric Anholt673a3942008-07-30 12:06:12 -07003235 */
3236static int
Eric Anholte47c68e2008-11-14 13:35:19 -08003237i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
3238 uint64_t offset, uint64_t size)
Eric Anholt673a3942008-07-30 12:06:12 -07003239{
Daniel Vetter23010e42010-03-08 13:35:02 +01003240 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003241 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003242 int i, ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003243
Eric Anholte47c68e2008-11-14 13:35:19 -08003244 if (offset == 0 && size == obj->size)
3245 return i915_gem_object_set_to_cpu_domain(obj, 0);
3246
3247 i915_gem_object_flush_gpu_write_domain(obj);
3248 /* Wait on any GPU rendering and flushing to occur. */
3249 ret = i915_gem_object_wait_rendering(obj);
3250 if (ret != 0)
3251 return ret;
3252 i915_gem_object_flush_gtt_write_domain(obj);
3253
3254 /* If we're already fully in the CPU read domain, we're done. */
3255 if (obj_priv->page_cpu_valid == NULL &&
3256 (obj->read_domains & I915_GEM_DOMAIN_CPU) != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07003257 return 0;
3258
Eric Anholte47c68e2008-11-14 13:35:19 -08003259 /* Otherwise, create/clear the per-page CPU read domain flag if we're
3260 * newly adding I915_GEM_DOMAIN_CPU
3261 */
Eric Anholt673a3942008-07-30 12:06:12 -07003262 if (obj_priv->page_cpu_valid == NULL) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003263 obj_priv->page_cpu_valid = kzalloc(obj->size / PAGE_SIZE,
3264 GFP_KERNEL);
Eric Anholte47c68e2008-11-14 13:35:19 -08003265 if (obj_priv->page_cpu_valid == NULL)
3266 return -ENOMEM;
3267 } else if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0)
3268 memset(obj_priv->page_cpu_valid, 0, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07003269
3270 /* Flush the cache on any pages that are still invalid from the CPU's
3271 * perspective.
3272 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003273 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
3274 i++) {
Eric Anholt673a3942008-07-30 12:06:12 -07003275 if (obj_priv->page_cpu_valid[i])
3276 continue;
3277
Eric Anholt856fa192009-03-19 14:10:50 -07003278 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07003279
3280 obj_priv->page_cpu_valid[i] = 1;
3281 }
3282
Eric Anholte47c68e2008-11-14 13:35:19 -08003283 /* It should now be out of any other write domains, and we can update
3284 * the domain values for our changes.
3285 */
3286 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
3287
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003288 old_read_domains = obj->read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003289 obj->read_domains |= I915_GEM_DOMAIN_CPU;
3290
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003291 trace_i915_gem_object_change_domain(obj,
3292 old_read_domains,
3293 obj->write_domain);
3294
Eric Anholt673a3942008-07-30 12:06:12 -07003295 return 0;
3296}
3297
3298/**
Eric Anholt673a3942008-07-30 12:06:12 -07003299 * Pin an object to the GTT and evaluate the relocations landing in it.
3300 */
3301static int
3302i915_gem_object_pin_and_relocate(struct drm_gem_object *obj,
3303 struct drm_file *file_priv,
Jesse Barnes76446ca2009-12-17 22:05:42 -05003304 struct drm_i915_gem_exec_object2 *entry,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003305 struct drm_i915_gem_relocation_entry *relocs)
Eric Anholt673a3942008-07-30 12:06:12 -07003306{
3307 struct drm_device *dev = obj->dev;
Keith Packard0839ccb2008-10-30 19:38:48 -07003308 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01003309 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003310 int i, ret;
Keith Packard0839ccb2008-10-30 19:38:48 -07003311 void __iomem *reloc_page;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003312 bool need_fence;
3313
3314 need_fence = entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
3315 obj_priv->tiling_mode != I915_TILING_NONE;
3316
3317 /* Check fence reg constraints and rebind if necessary */
Chris Wilson808b24d2010-05-27 13:18:15 +01003318 if (need_fence &&
3319 !i915_gem_object_fence_offset_ok(obj,
3320 obj_priv->tiling_mode)) {
3321 ret = i915_gem_object_unbind(obj);
3322 if (ret)
3323 return ret;
3324 }
Eric Anholt673a3942008-07-30 12:06:12 -07003325
3326 /* Choose the GTT offset for our buffer and put it there. */
3327 ret = i915_gem_object_pin(obj, (uint32_t) entry->alignment);
3328 if (ret)
3329 return ret;
3330
Jesse Barnes76446ca2009-12-17 22:05:42 -05003331 /*
3332 * Pre-965 chips need a fence register set up in order to
3333 * properly handle blits to/from tiled surfaces.
3334 */
3335 if (need_fence) {
3336 ret = i915_gem_object_get_fence_reg(obj);
3337 if (ret != 0) {
Jesse Barnes76446ca2009-12-17 22:05:42 -05003338 i915_gem_object_unpin(obj);
3339 return ret;
3340 }
3341 }
3342
Eric Anholt673a3942008-07-30 12:06:12 -07003343 entry->offset = obj_priv->gtt_offset;
3344
Eric Anholt673a3942008-07-30 12:06:12 -07003345 /* Apply the relocations, using the GTT aperture to avoid cache
3346 * flushing requirements.
3347 */
3348 for (i = 0; i < entry->relocation_count; i++) {
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003349 struct drm_i915_gem_relocation_entry *reloc= &relocs[i];
Eric Anholt673a3942008-07-30 12:06:12 -07003350 struct drm_gem_object *target_obj;
3351 struct drm_i915_gem_object *target_obj_priv;
Eric Anholt3043c602008-10-02 12:24:47 -07003352 uint32_t reloc_val, reloc_offset;
3353 uint32_t __iomem *reloc_entry;
Eric Anholt673a3942008-07-30 12:06:12 -07003354
Eric Anholt673a3942008-07-30 12:06:12 -07003355 target_obj = drm_gem_object_lookup(obj->dev, file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003356 reloc->target_handle);
Eric Anholt673a3942008-07-30 12:06:12 -07003357 if (target_obj == NULL) {
3358 i915_gem_object_unpin(obj);
3359 return -EBADF;
3360 }
Daniel Vetter23010e42010-03-08 13:35:02 +01003361 target_obj_priv = to_intel_bo(target_obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003362
Chris Wilson8542a0b2009-09-09 21:15:15 +01003363#if WATCH_RELOC
3364 DRM_INFO("%s: obj %p offset %08x target %d "
3365 "read %08x write %08x gtt %08x "
3366 "presumed %08x delta %08x\n",
3367 __func__,
3368 obj,
3369 (int) reloc->offset,
3370 (int) reloc->target_handle,
3371 (int) reloc->read_domains,
3372 (int) reloc->write_domain,
3373 (int) target_obj_priv->gtt_offset,
3374 (int) reloc->presumed_offset,
3375 reloc->delta);
3376#endif
3377
Eric Anholt673a3942008-07-30 12:06:12 -07003378 /* The target buffer should have appeared before us in the
3379 * exec_object list, so it should have a GTT space bound by now.
3380 */
3381 if (target_obj_priv->gtt_space == NULL) {
3382 DRM_ERROR("No GTT space found for object %d\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003383 reloc->target_handle);
Eric Anholt673a3942008-07-30 12:06:12 -07003384 drm_gem_object_unreference(target_obj);
3385 i915_gem_object_unpin(obj);
3386 return -EINVAL;
3387 }
3388
Chris Wilson8542a0b2009-09-09 21:15:15 +01003389 /* Validate that the target is in a valid r/w GPU domain */
Daniel Vetter16edd552010-02-19 11:52:02 +01003390 if (reloc->write_domain & (reloc->write_domain - 1)) {
3391 DRM_ERROR("reloc with multiple write domains: "
3392 "obj %p target %d offset %d "
3393 "read %08x write %08x",
3394 obj, reloc->target_handle,
3395 (int) reloc->offset,
3396 reloc->read_domains,
3397 reloc->write_domain);
3398 return -EINVAL;
3399 }
Chris Wilson8542a0b2009-09-09 21:15:15 +01003400 if (reloc->write_domain & I915_GEM_DOMAIN_CPU ||
3401 reloc->read_domains & I915_GEM_DOMAIN_CPU) {
3402 DRM_ERROR("reloc with read/write CPU domains: "
3403 "obj %p target %d offset %d "
3404 "read %08x write %08x",
3405 obj, reloc->target_handle,
3406 (int) reloc->offset,
3407 reloc->read_domains,
3408 reloc->write_domain);
3409 drm_gem_object_unreference(target_obj);
3410 i915_gem_object_unpin(obj);
3411 return -EINVAL;
3412 }
3413 if (reloc->write_domain && target_obj->pending_write_domain &&
3414 reloc->write_domain != target_obj->pending_write_domain) {
3415 DRM_ERROR("Write domain conflict: "
3416 "obj %p target %d offset %d "
3417 "new %08x old %08x\n",
3418 obj, reloc->target_handle,
3419 (int) reloc->offset,
3420 reloc->write_domain,
3421 target_obj->pending_write_domain);
3422 drm_gem_object_unreference(target_obj);
3423 i915_gem_object_unpin(obj);
3424 return -EINVAL;
3425 }
3426
3427 target_obj->pending_read_domains |= reloc->read_domains;
3428 target_obj->pending_write_domain |= reloc->write_domain;
3429
3430 /* If the relocation already has the right value in it, no
3431 * more work needs to be done.
3432 */
3433 if (target_obj_priv->gtt_offset == reloc->presumed_offset) {
3434 drm_gem_object_unreference(target_obj);
3435 continue;
3436 }
3437
3438 /* Check that the relocation address is valid... */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003439 if (reloc->offset > obj->size - 4) {
Eric Anholt673a3942008-07-30 12:06:12 -07003440 DRM_ERROR("Relocation beyond object bounds: "
3441 "obj %p target %d offset %d size %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003442 obj, reloc->target_handle,
3443 (int) reloc->offset, (int) obj->size);
Eric Anholt673a3942008-07-30 12:06:12 -07003444 drm_gem_object_unreference(target_obj);
3445 i915_gem_object_unpin(obj);
3446 return -EINVAL;
3447 }
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003448 if (reloc->offset & 3) {
Eric Anholt673a3942008-07-30 12:06:12 -07003449 DRM_ERROR("Relocation not 4-byte aligned: "
3450 "obj %p target %d offset %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003451 obj, reloc->target_handle,
3452 (int) reloc->offset);
Eric Anholt673a3942008-07-30 12:06:12 -07003453 drm_gem_object_unreference(target_obj);
3454 i915_gem_object_unpin(obj);
3455 return -EINVAL;
3456 }
3457
Chris Wilson8542a0b2009-09-09 21:15:15 +01003458 /* and points to somewhere within the target object. */
Chris Wilsoncd0b9fb2009-09-15 23:23:18 +01003459 if (reloc->delta >= target_obj->size) {
3460 DRM_ERROR("Relocation beyond target object bounds: "
3461 "obj %p target %d delta %d size %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003462 obj, reloc->target_handle,
Chris Wilsoncd0b9fb2009-09-15 23:23:18 +01003463 (int) reloc->delta, (int) target_obj->size);
Chris Wilson491152b2009-02-11 14:26:32 +00003464 drm_gem_object_unreference(target_obj);
3465 i915_gem_object_unpin(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003466 return -EINVAL;
3467 }
3468
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003469 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
3470 if (ret != 0) {
3471 drm_gem_object_unreference(target_obj);
3472 i915_gem_object_unpin(obj);
3473 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -07003474 }
3475
3476 /* Map the page containing the relocation we're going to
3477 * perform.
3478 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003479 reloc_offset = obj_priv->gtt_offset + reloc->offset;
Keith Packard0839ccb2008-10-30 19:38:48 -07003480 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
3481 (reloc_offset &
3482 ~(PAGE_SIZE - 1)));
Eric Anholt3043c602008-10-02 12:24:47 -07003483 reloc_entry = (uint32_t __iomem *)(reloc_page +
Keith Packard0839ccb2008-10-30 19:38:48 -07003484 (reloc_offset & (PAGE_SIZE - 1)));
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003485 reloc_val = target_obj_priv->gtt_offset + reloc->delta;
Eric Anholt673a3942008-07-30 12:06:12 -07003486
3487#if WATCH_BUF
3488 DRM_INFO("Applied relocation: %p@0x%08x %08x -> %08x\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003489 obj, (unsigned int) reloc->offset,
Eric Anholt673a3942008-07-30 12:06:12 -07003490 readl(reloc_entry), reloc_val);
3491#endif
3492 writel(reloc_val, reloc_entry);
Keith Packard0839ccb2008-10-30 19:38:48 -07003493 io_mapping_unmap_atomic(reloc_page);
Eric Anholt673a3942008-07-30 12:06:12 -07003494
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003495 /* The updated presumed offset for this entry will be
3496 * copied back out to the user.
Eric Anholt673a3942008-07-30 12:06:12 -07003497 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003498 reloc->presumed_offset = target_obj_priv->gtt_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003499
3500 drm_gem_object_unreference(target_obj);
3501 }
3502
Eric Anholt673a3942008-07-30 12:06:12 -07003503#if WATCH_BUF
3504 if (0)
3505 i915_gem_dump_object(obj, 128, __func__, ~0);
3506#endif
3507 return 0;
3508}
3509
Eric Anholt673a3942008-07-30 12:06:12 -07003510/* Throttle our rendering by waiting until the ring has completed our requests
3511 * emitted over 20 msec ago.
3512 *
Eric Anholtb9624422009-06-03 07:27:35 +00003513 * Note that if we were to use the current jiffies each time around the loop,
3514 * we wouldn't escape the function with any frames outstanding if the time to
3515 * render a frame was over 20ms.
3516 *
Eric Anholt673a3942008-07-30 12:06:12 -07003517 * This should get us reasonable parallelism between CPU and GPU but also
3518 * relatively low latency when blocking on a particular request to finish.
3519 */
3520static int
3521i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file_priv)
3522{
3523 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
3524 int ret = 0;
Eric Anholtb9624422009-06-03 07:27:35 +00003525 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
Eric Anholt673a3942008-07-30 12:06:12 -07003526
3527 mutex_lock(&dev->struct_mutex);
Eric Anholtb9624422009-06-03 07:27:35 +00003528 while (!list_empty(&i915_file_priv->mm.request_list)) {
3529 struct drm_i915_gem_request *request;
3530
3531 request = list_first_entry(&i915_file_priv->mm.request_list,
3532 struct drm_i915_gem_request,
3533 client_list);
3534
3535 if (time_after_eq(request->emitted_jiffies, recent_enough))
3536 break;
3537
Zou Nan hai852835f2010-05-21 09:08:56 +08003538 ret = i915_wait_request(dev, request->seqno, request->ring);
Eric Anholtb9624422009-06-03 07:27:35 +00003539 if (ret != 0)
3540 break;
3541 }
Eric Anholt673a3942008-07-30 12:06:12 -07003542 mutex_unlock(&dev->struct_mutex);
Eric Anholtb9624422009-06-03 07:27:35 +00003543
Eric Anholt673a3942008-07-30 12:06:12 -07003544 return ret;
3545}
3546
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003547static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003548i915_gem_get_relocs_from_user(struct drm_i915_gem_exec_object2 *exec_list,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003549 uint32_t buffer_count,
3550 struct drm_i915_gem_relocation_entry **relocs)
3551{
3552 uint32_t reloc_count = 0, reloc_index = 0, i;
3553 int ret;
3554
3555 *relocs = NULL;
3556 for (i = 0; i < buffer_count; i++) {
3557 if (reloc_count + exec_list[i].relocation_count < reloc_count)
3558 return -EINVAL;
3559 reloc_count += exec_list[i].relocation_count;
3560 }
3561
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003562 *relocs = drm_calloc_large(reloc_count, sizeof(**relocs));
Jesse Barnes76446ca2009-12-17 22:05:42 -05003563 if (*relocs == NULL) {
3564 DRM_ERROR("failed to alloc relocs, count %d\n", reloc_count);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003565 return -ENOMEM;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003566 }
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003567
3568 for (i = 0; i < buffer_count; i++) {
3569 struct drm_i915_gem_relocation_entry __user *user_relocs;
3570
3571 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3572
3573 ret = copy_from_user(&(*relocs)[reloc_index],
3574 user_relocs,
3575 exec_list[i].relocation_count *
3576 sizeof(**relocs));
3577 if (ret != 0) {
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003578 drm_free_large(*relocs);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003579 *relocs = NULL;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003580 return -EFAULT;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003581 }
3582
3583 reloc_index += exec_list[i].relocation_count;
3584 }
3585
Florian Mickler2bc43b52009-04-06 22:55:41 +02003586 return 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003587}
3588
3589static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003590i915_gem_put_relocs_to_user(struct drm_i915_gem_exec_object2 *exec_list,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003591 uint32_t buffer_count,
3592 struct drm_i915_gem_relocation_entry *relocs)
3593{
3594 uint32_t reloc_count = 0, i;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003595 int ret = 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003596
Chris Wilson93533c22010-01-31 10:40:48 +00003597 if (relocs == NULL)
3598 return 0;
3599
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003600 for (i = 0; i < buffer_count; i++) {
3601 struct drm_i915_gem_relocation_entry __user *user_relocs;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003602 int unwritten;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003603
3604 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3605
Florian Mickler2bc43b52009-04-06 22:55:41 +02003606 unwritten = copy_to_user(user_relocs,
3607 &relocs[reloc_count],
3608 exec_list[i].relocation_count *
3609 sizeof(*relocs));
3610
3611 if (unwritten) {
3612 ret = -EFAULT;
3613 goto err;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003614 }
3615
3616 reloc_count += exec_list[i].relocation_count;
3617 }
3618
Florian Mickler2bc43b52009-04-06 22:55:41 +02003619err:
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003620 drm_free_large(relocs);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003621
3622 return ret;
3623}
3624
Chris Wilson83d60792009-06-06 09:45:57 +01003625static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003626i915_gem_check_execbuffer (struct drm_i915_gem_execbuffer2 *exec,
Chris Wilson83d60792009-06-06 09:45:57 +01003627 uint64_t exec_offset)
3628{
3629 uint32_t exec_start, exec_len;
3630
3631 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
3632 exec_len = (uint32_t) exec->batch_len;
3633
3634 if ((exec_start | exec_len) & 0x7)
3635 return -EINVAL;
3636
3637 if (!exec_start)
3638 return -EINVAL;
3639
3640 return 0;
3641}
3642
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003643static int
3644i915_gem_wait_for_pending_flip(struct drm_device *dev,
3645 struct drm_gem_object **object_list,
3646 int count)
3647{
3648 drm_i915_private_t *dev_priv = dev->dev_private;
3649 struct drm_i915_gem_object *obj_priv;
3650 DEFINE_WAIT(wait);
3651 int i, ret = 0;
3652
3653 for (;;) {
3654 prepare_to_wait(&dev_priv->pending_flip_queue,
3655 &wait, TASK_INTERRUPTIBLE);
3656 for (i = 0; i < count; i++) {
Daniel Vetter23010e42010-03-08 13:35:02 +01003657 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003658 if (atomic_read(&obj_priv->pending_flip) > 0)
3659 break;
3660 }
3661 if (i == count)
3662 break;
3663
3664 if (!signal_pending(current)) {
3665 mutex_unlock(&dev->struct_mutex);
3666 schedule();
3667 mutex_lock(&dev->struct_mutex);
3668 continue;
3669 }
3670 ret = -ERESTARTSYS;
3671 break;
3672 }
3673 finish_wait(&dev_priv->pending_flip_queue, &wait);
3674
3675 return ret;
3676}
3677
Eric Anholt673a3942008-07-30 12:06:12 -07003678int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003679i915_gem_do_execbuffer(struct drm_device *dev, void *data,
3680 struct drm_file *file_priv,
3681 struct drm_i915_gem_execbuffer2 *args,
3682 struct drm_i915_gem_exec_object2 *exec_list)
Eric Anholt673a3942008-07-30 12:06:12 -07003683{
3684 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003685 struct drm_gem_object **object_list = NULL;
3686 struct drm_gem_object *batch_obj;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003687 struct drm_i915_gem_object *obj_priv;
Eric Anholt201361a2009-03-11 12:30:04 -07003688 struct drm_clip_rect *cliprects = NULL;
Chris Wilson93533c22010-01-31 10:40:48 +00003689 struct drm_i915_gem_relocation_entry *relocs = NULL;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003690 int ret = 0, ret2, i, pinned = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003691 uint64_t exec_offset;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003692 uint32_t seqno, flush_domains, reloc_index;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003693 int pin_tries, flips;
Eric Anholt673a3942008-07-30 12:06:12 -07003694
Zou Nan hai852835f2010-05-21 09:08:56 +08003695 struct intel_ring_buffer *ring = NULL;
3696
Eric Anholt673a3942008-07-30 12:06:12 -07003697#if WATCH_EXEC
3698 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3699 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3700#endif
Zou Nan haid1b851f2010-05-21 09:08:57 +08003701 if (args->flags & I915_EXEC_BSD) {
3702 if (!HAS_BSD(dev)) {
3703 DRM_ERROR("execbuf with wrong flag\n");
3704 return -EINVAL;
3705 }
3706 ring = &dev_priv->bsd_ring;
3707 } else {
3708 ring = &dev_priv->render_ring;
3709 }
3710
Eric Anholt673a3942008-07-30 12:06:12 -07003711
Eric Anholt4f481ed2008-09-10 14:22:49 -07003712 if (args->buffer_count < 1) {
3713 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3714 return -EINVAL;
3715 }
Eric Anholtc8e0f932009-11-22 03:49:37 +01003716 object_list = drm_malloc_ab(sizeof(*object_list), args->buffer_count);
Jesse Barnes76446ca2009-12-17 22:05:42 -05003717 if (object_list == NULL) {
3718 DRM_ERROR("Failed to allocate object list for %d buffers\n",
Eric Anholt673a3942008-07-30 12:06:12 -07003719 args->buffer_count);
3720 ret = -ENOMEM;
3721 goto pre_mutex_err;
3722 }
Eric Anholt673a3942008-07-30 12:06:12 -07003723
Eric Anholt201361a2009-03-11 12:30:04 -07003724 if (args->num_cliprects != 0) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003725 cliprects = kcalloc(args->num_cliprects, sizeof(*cliprects),
3726 GFP_KERNEL);
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003727 if (cliprects == NULL) {
3728 ret = -ENOMEM;
Eric Anholt201361a2009-03-11 12:30:04 -07003729 goto pre_mutex_err;
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003730 }
Eric Anholt201361a2009-03-11 12:30:04 -07003731
3732 ret = copy_from_user(cliprects,
3733 (struct drm_clip_rect __user *)
3734 (uintptr_t) args->cliprects_ptr,
3735 sizeof(*cliprects) * args->num_cliprects);
3736 if (ret != 0) {
3737 DRM_ERROR("copy %d cliprects failed: %d\n",
3738 args->num_cliprects, ret);
3739 goto pre_mutex_err;
3740 }
3741 }
3742
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003743 ret = i915_gem_get_relocs_from_user(exec_list, args->buffer_count,
3744 &relocs);
3745 if (ret != 0)
3746 goto pre_mutex_err;
3747
Eric Anholt673a3942008-07-30 12:06:12 -07003748 mutex_lock(&dev->struct_mutex);
3749
3750 i915_verify_inactive(dev, __FILE__, __LINE__);
3751
Ben Gamariba1234d2009-09-14 17:48:47 -04003752 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07003753 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003754 ret = -EIO;
3755 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003756 }
3757
3758 if (dev_priv->mm.suspended) {
Eric Anholt673a3942008-07-30 12:06:12 -07003759 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003760 ret = -EBUSY;
3761 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003762 }
3763
Keith Packardac94a962008-11-20 23:30:27 -08003764 /* Look up object handles */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003765 flips = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003766 for (i = 0; i < args->buffer_count; i++) {
3767 object_list[i] = drm_gem_object_lookup(dev, file_priv,
3768 exec_list[i].handle);
3769 if (object_list[i] == NULL) {
3770 DRM_ERROR("Invalid object handle %d at index %d\n",
3771 exec_list[i].handle, i);
Chris Wilson0ce907f2010-01-23 20:26:35 +00003772 /* prevent error path from reading uninitialized data */
3773 args->buffer_count = i + 1;
Eric Anholt673a3942008-07-30 12:06:12 -07003774 ret = -EBADF;
3775 goto err;
3776 }
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003777
Daniel Vetter23010e42010-03-08 13:35:02 +01003778 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003779 if (obj_priv->in_execbuffer) {
3780 DRM_ERROR("Object %p appears more than once in object list\n",
3781 object_list[i]);
Chris Wilson0ce907f2010-01-23 20:26:35 +00003782 /* prevent error path from reading uninitialized data */
3783 args->buffer_count = i + 1;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003784 ret = -EBADF;
3785 goto err;
3786 }
3787 obj_priv->in_execbuffer = true;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003788 flips += atomic_read(&obj_priv->pending_flip);
3789 }
3790
3791 if (flips > 0) {
3792 ret = i915_gem_wait_for_pending_flip(dev, object_list,
3793 args->buffer_count);
3794 if (ret)
3795 goto err;
Keith Packardac94a962008-11-20 23:30:27 -08003796 }
Eric Anholt673a3942008-07-30 12:06:12 -07003797
Keith Packardac94a962008-11-20 23:30:27 -08003798 /* Pin and relocate */
3799 for (pin_tries = 0; ; pin_tries++) {
3800 ret = 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003801 reloc_index = 0;
3802
Keith Packardac94a962008-11-20 23:30:27 -08003803 for (i = 0; i < args->buffer_count; i++) {
3804 object_list[i]->pending_read_domains = 0;
3805 object_list[i]->pending_write_domain = 0;
3806 ret = i915_gem_object_pin_and_relocate(object_list[i],
3807 file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003808 &exec_list[i],
3809 &relocs[reloc_index]);
Keith Packardac94a962008-11-20 23:30:27 -08003810 if (ret)
3811 break;
3812 pinned = i + 1;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003813 reloc_index += exec_list[i].relocation_count;
Keith Packardac94a962008-11-20 23:30:27 -08003814 }
3815 /* success */
3816 if (ret == 0)
3817 break;
3818
3819 /* error other than GTT full, or we've already tried again */
Chris Wilson2939e1f2009-06-06 09:46:03 +01003820 if (ret != -ENOSPC || pin_tries >= 1) {
Chris Wilson07f73f62009-09-14 16:50:30 +01003821 if (ret != -ERESTARTSYS) {
3822 unsigned long long total_size = 0;
Chris Wilson3d1cc472010-05-27 13:18:19 +01003823 int num_fences = 0;
3824 for (i = 0; i < args->buffer_count; i++) {
3825 obj_priv = object_list[i]->driver_private;
3826
Chris Wilson07f73f62009-09-14 16:50:30 +01003827 total_size += object_list[i]->size;
Chris Wilson3d1cc472010-05-27 13:18:19 +01003828 num_fences +=
3829 exec_list[i].flags & EXEC_OBJECT_NEEDS_FENCE &&
3830 obj_priv->tiling_mode != I915_TILING_NONE;
3831 }
3832 DRM_ERROR("Failed to pin buffer %d of %d, total %llu bytes, %d fences: %d\n",
Chris Wilson07f73f62009-09-14 16:50:30 +01003833 pinned+1, args->buffer_count,
Chris Wilson3d1cc472010-05-27 13:18:19 +01003834 total_size, num_fences,
3835 ret);
Chris Wilson07f73f62009-09-14 16:50:30 +01003836 DRM_ERROR("%d objects [%d pinned], "
3837 "%d object bytes [%d pinned], "
3838 "%d/%d gtt bytes\n",
3839 atomic_read(&dev->object_count),
3840 atomic_read(&dev->pin_count),
3841 atomic_read(&dev->object_memory),
3842 atomic_read(&dev->pin_memory),
3843 atomic_read(&dev->gtt_memory),
3844 dev->gtt_total);
3845 }
Eric Anholt673a3942008-07-30 12:06:12 -07003846 goto err;
3847 }
Keith Packardac94a962008-11-20 23:30:27 -08003848
3849 /* unpin all of our buffers */
3850 for (i = 0; i < pinned; i++)
3851 i915_gem_object_unpin(object_list[i]);
Eric Anholtb1177632008-12-10 10:09:41 -08003852 pinned = 0;
Keith Packardac94a962008-11-20 23:30:27 -08003853
3854 /* evict everyone we can from the aperture */
3855 ret = i915_gem_evict_everything(dev);
Chris Wilson07f73f62009-09-14 16:50:30 +01003856 if (ret && ret != -ENOSPC)
Keith Packardac94a962008-11-20 23:30:27 -08003857 goto err;
Eric Anholt673a3942008-07-30 12:06:12 -07003858 }
3859
3860 /* Set the pending read domains for the batch buffer to COMMAND */
3861 batch_obj = object_list[args->buffer_count-1];
Chris Wilson5f26a2c2009-06-06 09:45:58 +01003862 if (batch_obj->pending_write_domain) {
3863 DRM_ERROR("Attempting to use self-modifying batch buffer\n");
3864 ret = -EINVAL;
3865 goto err;
3866 }
3867 batch_obj->pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
Eric Anholt673a3942008-07-30 12:06:12 -07003868
Chris Wilson83d60792009-06-06 09:45:57 +01003869 /* Sanity check the batch buffer, prior to moving objects */
3870 exec_offset = exec_list[args->buffer_count - 1].offset;
3871 ret = i915_gem_check_execbuffer (args, exec_offset);
3872 if (ret != 0) {
3873 DRM_ERROR("execbuf with invalid offset/length\n");
3874 goto err;
3875 }
3876
Eric Anholt673a3942008-07-30 12:06:12 -07003877 i915_verify_inactive(dev, __FILE__, __LINE__);
3878
Keith Packard646f0f62008-11-20 23:23:03 -08003879 /* Zero the global flush/invalidate flags. These
3880 * will be modified as new domains are computed
3881 * for each object
3882 */
3883 dev->invalidate_domains = 0;
3884 dev->flush_domains = 0;
3885
Eric Anholt673a3942008-07-30 12:06:12 -07003886 for (i = 0; i < args->buffer_count; i++) {
3887 struct drm_gem_object *obj = object_list[i];
Eric Anholt673a3942008-07-30 12:06:12 -07003888
Keith Packard646f0f62008-11-20 23:23:03 -08003889 /* Compute new gpu domains and update invalidate/flush */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003890 i915_gem_object_set_to_gpu_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003891 }
3892
3893 i915_verify_inactive(dev, __FILE__, __LINE__);
3894
Keith Packard646f0f62008-11-20 23:23:03 -08003895 if (dev->invalidate_domains | dev->flush_domains) {
3896#if WATCH_EXEC
3897 DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
3898 __func__,
3899 dev->invalidate_domains,
3900 dev->flush_domains);
3901#endif
3902 i915_gem_flush(dev,
3903 dev->invalidate_domains,
3904 dev->flush_domains);
Zou Nan hai852835f2010-05-21 09:08:56 +08003905 if (dev->flush_domains & I915_GEM_GPU_DOMAINS) {
Eric Anholtb9624422009-06-03 07:27:35 +00003906 (void)i915_add_request(dev, file_priv,
Zou Nan hai852835f2010-05-21 09:08:56 +08003907 dev->flush_domains,
3908 &dev_priv->render_ring);
3909
Zou Nan haid1b851f2010-05-21 09:08:57 +08003910 if (HAS_BSD(dev))
3911 (void)i915_add_request(dev, file_priv,
3912 dev->flush_domains,
3913 &dev_priv->bsd_ring);
Zou Nan hai852835f2010-05-21 09:08:56 +08003914 }
Keith Packard646f0f62008-11-20 23:23:03 -08003915 }
Eric Anholt673a3942008-07-30 12:06:12 -07003916
Eric Anholtefbeed92009-02-19 14:54:51 -08003917 for (i = 0; i < args->buffer_count; i++) {
3918 struct drm_gem_object *obj = object_list[i];
Daniel Vetter23010e42010-03-08 13:35:02 +01003919 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003920 uint32_t old_write_domain = obj->write_domain;
Eric Anholtefbeed92009-02-19 14:54:51 -08003921
3922 obj->write_domain = obj->pending_write_domain;
Daniel Vetter99fcb762010-02-07 16:20:18 +01003923 if (obj->write_domain)
3924 list_move_tail(&obj_priv->gpu_write_list,
3925 &dev_priv->mm.gpu_write_list);
3926 else
3927 list_del_init(&obj_priv->gpu_write_list);
3928
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003929 trace_i915_gem_object_change_domain(obj,
3930 obj->read_domains,
3931 old_write_domain);
Eric Anholtefbeed92009-02-19 14:54:51 -08003932 }
3933
Eric Anholt673a3942008-07-30 12:06:12 -07003934 i915_verify_inactive(dev, __FILE__, __LINE__);
3935
3936#if WATCH_COHERENCY
3937 for (i = 0; i < args->buffer_count; i++) {
3938 i915_gem_object_check_coherency(object_list[i],
3939 exec_list[i].handle);
3940 }
3941#endif
3942
Eric Anholt673a3942008-07-30 12:06:12 -07003943#if WATCH_EXEC
Ben Gamari6911a9b2009-04-02 11:24:54 -07003944 i915_gem_dump_object(batch_obj,
Eric Anholt673a3942008-07-30 12:06:12 -07003945 args->batch_len,
3946 __func__,
3947 ~0);
3948#endif
3949
Eric Anholt673a3942008-07-30 12:06:12 -07003950 /* Exec the batchbuffer */
Zou Nan hai852835f2010-05-21 09:08:56 +08003951 ret = ring->dispatch_gem_execbuffer(dev, ring, args,
3952 cliprects, exec_offset);
Eric Anholt673a3942008-07-30 12:06:12 -07003953 if (ret) {
3954 DRM_ERROR("dispatch failed %d\n", ret);
3955 goto err;
3956 }
3957
3958 /*
3959 * Ensure that the commands in the batch buffer are
3960 * finished before the interrupt fires
3961 */
Zou Nan hai852835f2010-05-21 09:08:56 +08003962 flush_domains = i915_retire_commands(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07003963
3964 i915_verify_inactive(dev, __FILE__, __LINE__);
3965
3966 /*
3967 * Get a seqno representing the execution of the current buffer,
3968 * which we can wait on. We would like to mitigate these interrupts,
3969 * likely by only creating seqnos occasionally (so that we have
3970 * *some* interrupts representing completion of buffers that we can
3971 * wait on when trying to clear up gtt space).
3972 */
Zou Nan hai852835f2010-05-21 09:08:56 +08003973 seqno = i915_add_request(dev, file_priv, flush_domains, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07003974 BUG_ON(seqno == 0);
Eric Anholt673a3942008-07-30 12:06:12 -07003975 for (i = 0; i < args->buffer_count; i++) {
3976 struct drm_gem_object *obj = object_list[i];
Zou Nan hai852835f2010-05-21 09:08:56 +08003977 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003978
Zou Nan hai852835f2010-05-21 09:08:56 +08003979 i915_gem_object_move_to_active(obj, seqno, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07003980#if WATCH_LRU
3981 DRM_INFO("%s: move to exec list %p\n", __func__, obj);
3982#endif
3983 }
3984#if WATCH_LRU
3985 i915_dump_lru(dev, __func__);
3986#endif
3987
3988 i915_verify_inactive(dev, __FILE__, __LINE__);
3989
Eric Anholt673a3942008-07-30 12:06:12 -07003990err:
Julia Lawallaad87df2008-12-21 16:28:47 +01003991 for (i = 0; i < pinned; i++)
3992 i915_gem_object_unpin(object_list[i]);
Eric Anholt673a3942008-07-30 12:06:12 -07003993
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003994 for (i = 0; i < args->buffer_count; i++) {
3995 if (object_list[i]) {
Daniel Vetter23010e42010-03-08 13:35:02 +01003996 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003997 obj_priv->in_execbuffer = false;
3998 }
Julia Lawallaad87df2008-12-21 16:28:47 +01003999 drm_gem_object_unreference(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05004000 }
Julia Lawallaad87df2008-12-21 16:28:47 +01004001
Eric Anholt673a3942008-07-30 12:06:12 -07004002 mutex_unlock(&dev->struct_mutex);
4003
Chris Wilson93533c22010-01-31 10:40:48 +00004004pre_mutex_err:
Eric Anholt40a5f0d2009-03-12 11:23:52 -07004005 /* Copy the updated relocations out regardless of current error
4006 * state. Failure to update the relocs would mean that the next
4007 * time userland calls execbuf, it would do so with presumed offset
4008 * state that didn't match the actual object state.
4009 */
4010 ret2 = i915_gem_put_relocs_to_user(exec_list, args->buffer_count,
4011 relocs);
4012 if (ret2 != 0) {
4013 DRM_ERROR("Failed to copy relocations back out: %d\n", ret2);
4014
4015 if (ret == 0)
4016 ret = ret2;
4017 }
4018
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07004019 drm_free_large(object_list);
Eric Anholt9a298b22009-03-24 12:23:04 -07004020 kfree(cliprects);
Eric Anholt673a3942008-07-30 12:06:12 -07004021
4022 return ret;
4023}
4024
Jesse Barnes76446ca2009-12-17 22:05:42 -05004025/*
4026 * Legacy execbuffer just creates an exec2 list from the original exec object
4027 * list array and passes it to the real function.
4028 */
4029int
4030i915_gem_execbuffer(struct drm_device *dev, void *data,
4031 struct drm_file *file_priv)
4032{
4033 struct drm_i915_gem_execbuffer *args = data;
4034 struct drm_i915_gem_execbuffer2 exec2;
4035 struct drm_i915_gem_exec_object *exec_list = NULL;
4036 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
4037 int ret, i;
4038
4039#if WATCH_EXEC
4040 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
4041 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
4042#endif
4043
4044 if (args->buffer_count < 1) {
4045 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
4046 return -EINVAL;
4047 }
4048
4049 /* Copy in the exec list from userland */
4050 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
4051 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
4052 if (exec_list == NULL || exec2_list == NULL) {
4053 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
4054 args->buffer_count);
4055 drm_free_large(exec_list);
4056 drm_free_large(exec2_list);
4057 return -ENOMEM;
4058 }
4059 ret = copy_from_user(exec_list,
4060 (struct drm_i915_relocation_entry __user *)
4061 (uintptr_t) args->buffers_ptr,
4062 sizeof(*exec_list) * args->buffer_count);
4063 if (ret != 0) {
4064 DRM_ERROR("copy %d exec entries failed %d\n",
4065 args->buffer_count, ret);
4066 drm_free_large(exec_list);
4067 drm_free_large(exec2_list);
4068 return -EFAULT;
4069 }
4070
4071 for (i = 0; i < args->buffer_count; i++) {
4072 exec2_list[i].handle = exec_list[i].handle;
4073 exec2_list[i].relocation_count = exec_list[i].relocation_count;
4074 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
4075 exec2_list[i].alignment = exec_list[i].alignment;
4076 exec2_list[i].offset = exec_list[i].offset;
4077 if (!IS_I965G(dev))
4078 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
4079 else
4080 exec2_list[i].flags = 0;
4081 }
4082
4083 exec2.buffers_ptr = args->buffers_ptr;
4084 exec2.buffer_count = args->buffer_count;
4085 exec2.batch_start_offset = args->batch_start_offset;
4086 exec2.batch_len = args->batch_len;
4087 exec2.DR1 = args->DR1;
4088 exec2.DR4 = args->DR4;
4089 exec2.num_cliprects = args->num_cliprects;
4090 exec2.cliprects_ptr = args->cliprects_ptr;
Zou Nan hai852835f2010-05-21 09:08:56 +08004091 exec2.flags = I915_EXEC_RENDER;
Jesse Barnes76446ca2009-12-17 22:05:42 -05004092
4093 ret = i915_gem_do_execbuffer(dev, data, file_priv, &exec2, exec2_list);
4094 if (!ret) {
4095 /* Copy the new buffer offsets back to the user's exec list. */
4096 for (i = 0; i < args->buffer_count; i++)
4097 exec_list[i].offset = exec2_list[i].offset;
4098 /* ... and back out to userspace */
4099 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
4100 (uintptr_t) args->buffers_ptr,
4101 exec_list,
4102 sizeof(*exec_list) * args->buffer_count);
4103 if (ret) {
4104 ret = -EFAULT;
4105 DRM_ERROR("failed to copy %d exec entries "
4106 "back to user (%d)\n",
4107 args->buffer_count, ret);
4108 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05004109 }
4110
4111 drm_free_large(exec_list);
4112 drm_free_large(exec2_list);
4113 return ret;
4114}
4115
4116int
4117i915_gem_execbuffer2(struct drm_device *dev, void *data,
4118 struct drm_file *file_priv)
4119{
4120 struct drm_i915_gem_execbuffer2 *args = data;
4121 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
4122 int ret;
4123
4124#if WATCH_EXEC
4125 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
4126 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
4127#endif
4128
4129 if (args->buffer_count < 1) {
4130 DRM_ERROR("execbuf2 with %d buffers\n", args->buffer_count);
4131 return -EINVAL;
4132 }
4133
4134 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
4135 if (exec2_list == NULL) {
4136 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
4137 args->buffer_count);
4138 return -ENOMEM;
4139 }
4140 ret = copy_from_user(exec2_list,
4141 (struct drm_i915_relocation_entry __user *)
4142 (uintptr_t) args->buffers_ptr,
4143 sizeof(*exec2_list) * args->buffer_count);
4144 if (ret != 0) {
4145 DRM_ERROR("copy %d exec entries failed %d\n",
4146 args->buffer_count, ret);
4147 drm_free_large(exec2_list);
4148 return -EFAULT;
4149 }
4150
4151 ret = i915_gem_do_execbuffer(dev, data, file_priv, args, exec2_list);
4152 if (!ret) {
4153 /* Copy the new buffer offsets back to the user's exec list. */
4154 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
4155 (uintptr_t) args->buffers_ptr,
4156 exec2_list,
4157 sizeof(*exec2_list) * args->buffer_count);
4158 if (ret) {
4159 ret = -EFAULT;
4160 DRM_ERROR("failed to copy %d exec entries "
4161 "back to user (%d)\n",
4162 args->buffer_count, ret);
4163 }
4164 }
4165
4166 drm_free_large(exec2_list);
4167 return ret;
4168}
4169
Eric Anholt673a3942008-07-30 12:06:12 -07004170int
4171i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment)
4172{
4173 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01004174 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004175 int ret;
4176
Daniel Vetter778c3542010-05-13 11:49:44 +02004177 BUG_ON(obj_priv->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT);
4178
Eric Anholt673a3942008-07-30 12:06:12 -07004179 i915_verify_inactive(dev, __FILE__, __LINE__);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01004180
4181 if (obj_priv->gtt_space != NULL) {
4182 if (alignment == 0)
4183 alignment = i915_gem_get_gtt_alignment(obj);
4184 if (obj_priv->gtt_offset & (alignment - 1)) {
4185 ret = i915_gem_object_unbind(obj);
4186 if (ret)
4187 return ret;
4188 }
4189 }
4190
Eric Anholt673a3942008-07-30 12:06:12 -07004191 if (obj_priv->gtt_space == NULL) {
4192 ret = i915_gem_object_bind_to_gtt(obj, alignment);
Chris Wilson97311292009-09-21 00:22:34 +01004193 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07004194 return ret;
Chris Wilson22c344e2009-02-11 14:26:45 +00004195 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05004196
Eric Anholt673a3942008-07-30 12:06:12 -07004197 obj_priv->pin_count++;
4198
4199 /* If the object is not active and not pending a flush,
4200 * remove it from the inactive list
4201 */
4202 if (obj_priv->pin_count == 1) {
4203 atomic_inc(&dev->pin_count);
4204 atomic_add(obj->size, &dev->pin_memory);
4205 if (!obj_priv->active &&
Chris Wilson21d509e2009-06-06 09:46:02 +01004206 (obj->write_domain & I915_GEM_GPU_DOMAINS) == 0 &&
Eric Anholt673a3942008-07-30 12:06:12 -07004207 !list_empty(&obj_priv->list))
4208 list_del_init(&obj_priv->list);
4209 }
4210 i915_verify_inactive(dev, __FILE__, __LINE__);
4211
4212 return 0;
4213}
4214
4215void
4216i915_gem_object_unpin(struct drm_gem_object *obj)
4217{
4218 struct drm_device *dev = obj->dev;
4219 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01004220 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004221
4222 i915_verify_inactive(dev, __FILE__, __LINE__);
4223 obj_priv->pin_count--;
4224 BUG_ON(obj_priv->pin_count < 0);
4225 BUG_ON(obj_priv->gtt_space == NULL);
4226
4227 /* If the object is no longer pinned, and is
4228 * neither active nor being flushed, then stick it on
4229 * the inactive list
4230 */
4231 if (obj_priv->pin_count == 0) {
4232 if (!obj_priv->active &&
Chris Wilson21d509e2009-06-06 09:46:02 +01004233 (obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
Eric Anholt673a3942008-07-30 12:06:12 -07004234 list_move_tail(&obj_priv->list,
4235 &dev_priv->mm.inactive_list);
4236 atomic_dec(&dev->pin_count);
4237 atomic_sub(obj->size, &dev->pin_memory);
4238 }
4239 i915_verify_inactive(dev, __FILE__, __LINE__);
4240}
4241
4242int
4243i915_gem_pin_ioctl(struct drm_device *dev, void *data,
4244 struct drm_file *file_priv)
4245{
4246 struct drm_i915_gem_pin *args = data;
4247 struct drm_gem_object *obj;
4248 struct drm_i915_gem_object *obj_priv;
4249 int ret;
4250
4251 mutex_lock(&dev->struct_mutex);
4252
4253 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4254 if (obj == NULL) {
4255 DRM_ERROR("Bad handle in i915_gem_pin_ioctl(): %d\n",
4256 args->handle);
4257 mutex_unlock(&dev->struct_mutex);
4258 return -EBADF;
4259 }
Daniel Vetter23010e42010-03-08 13:35:02 +01004260 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004261
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004262 if (obj_priv->madv != I915_MADV_WILLNEED) {
4263 DRM_ERROR("Attempting to pin a purgeable buffer\n");
Chris Wilson3ef94da2009-09-14 16:50:29 +01004264 drm_gem_object_unreference(obj);
4265 mutex_unlock(&dev->struct_mutex);
4266 return -EINVAL;
4267 }
4268
Jesse Barnes79e53942008-11-07 14:24:08 -08004269 if (obj_priv->pin_filp != NULL && obj_priv->pin_filp != file_priv) {
4270 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
4271 args->handle);
Chris Wilson96dec612009-02-08 19:08:04 +00004272 drm_gem_object_unreference(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004273 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08004274 return -EINVAL;
4275 }
4276
4277 obj_priv->user_pin_count++;
4278 obj_priv->pin_filp = file_priv;
4279 if (obj_priv->user_pin_count == 1) {
4280 ret = i915_gem_object_pin(obj, args->alignment);
4281 if (ret != 0) {
4282 drm_gem_object_unreference(obj);
4283 mutex_unlock(&dev->struct_mutex);
4284 return ret;
4285 }
Eric Anholt673a3942008-07-30 12:06:12 -07004286 }
4287
4288 /* XXX - flush the CPU caches for pinned objects
4289 * as the X server doesn't manage domains yet
4290 */
Eric Anholte47c68e2008-11-14 13:35:19 -08004291 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004292 args->offset = obj_priv->gtt_offset;
4293 drm_gem_object_unreference(obj);
4294 mutex_unlock(&dev->struct_mutex);
4295
4296 return 0;
4297}
4298
4299int
4300i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
4301 struct drm_file *file_priv)
4302{
4303 struct drm_i915_gem_pin *args = data;
4304 struct drm_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08004305 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07004306
4307 mutex_lock(&dev->struct_mutex);
4308
4309 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4310 if (obj == NULL) {
4311 DRM_ERROR("Bad handle in i915_gem_unpin_ioctl(): %d\n",
4312 args->handle);
4313 mutex_unlock(&dev->struct_mutex);
4314 return -EBADF;
4315 }
4316
Daniel Vetter23010e42010-03-08 13:35:02 +01004317 obj_priv = to_intel_bo(obj);
Jesse Barnes79e53942008-11-07 14:24:08 -08004318 if (obj_priv->pin_filp != file_priv) {
4319 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
4320 args->handle);
4321 drm_gem_object_unreference(obj);
4322 mutex_unlock(&dev->struct_mutex);
4323 return -EINVAL;
4324 }
4325 obj_priv->user_pin_count--;
4326 if (obj_priv->user_pin_count == 0) {
4327 obj_priv->pin_filp = NULL;
4328 i915_gem_object_unpin(obj);
4329 }
Eric Anholt673a3942008-07-30 12:06:12 -07004330
4331 drm_gem_object_unreference(obj);
4332 mutex_unlock(&dev->struct_mutex);
4333 return 0;
4334}
4335
4336int
4337i915_gem_busy_ioctl(struct drm_device *dev, void *data,
4338 struct drm_file *file_priv)
4339{
4340 struct drm_i915_gem_busy *args = data;
4341 struct drm_gem_object *obj;
4342 struct drm_i915_gem_object *obj_priv;
Zou Nan hai852835f2010-05-21 09:08:56 +08004343 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07004344
Eric Anholt673a3942008-07-30 12:06:12 -07004345 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4346 if (obj == NULL) {
4347 DRM_ERROR("Bad handle in i915_gem_busy_ioctl(): %d\n",
4348 args->handle);
Eric Anholt673a3942008-07-30 12:06:12 -07004349 return -EBADF;
4350 }
4351
Chris Wilsonb1ce7862009-06-06 09:46:00 +01004352 mutex_lock(&dev->struct_mutex);
Eric Anholtf21289b2009-02-18 09:44:56 -08004353 /* Update the active list for the hardware's current position.
4354 * Otherwise this only updates on a delayed timer or when irqs are
4355 * actually unmasked, and our working set ends up being larger than
4356 * required.
4357 */
Zou Nan hai852835f2010-05-21 09:08:56 +08004358 i915_gem_retire_requests(dev, &dev_priv->render_ring);
Eric Anholtf21289b2009-02-18 09:44:56 -08004359
Zou Nan haid1b851f2010-05-21 09:08:57 +08004360 if (HAS_BSD(dev))
4361 i915_gem_retire_requests(dev, &dev_priv->bsd_ring);
4362
Daniel Vetter23010e42010-03-08 13:35:02 +01004363 obj_priv = to_intel_bo(obj);
Eric Anholtc4de0a52008-12-14 19:05:04 -08004364 /* Don't count being on the flushing list against the object being
4365 * done. Otherwise, a buffer left on the flushing list but not getting
4366 * flushed (because nobody's flushing that domain) won't ever return
4367 * unbusy and get reused by libdrm's bo cache. The other expected
4368 * consumer of this interface, OpenGL's occlusion queries, also specs
4369 * that the objects get unbusy "eventually" without any interference.
4370 */
4371 args->busy = obj_priv->active && obj_priv->last_rendering_seqno != 0;
Eric Anholt673a3942008-07-30 12:06:12 -07004372
4373 drm_gem_object_unreference(obj);
4374 mutex_unlock(&dev->struct_mutex);
4375 return 0;
4376}
4377
4378int
4379i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4380 struct drm_file *file_priv)
4381{
4382 return i915_gem_ring_throttle(dev, file_priv);
4383}
4384
Chris Wilson3ef94da2009-09-14 16:50:29 +01004385int
4386i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4387 struct drm_file *file_priv)
4388{
4389 struct drm_i915_gem_madvise *args = data;
4390 struct drm_gem_object *obj;
4391 struct drm_i915_gem_object *obj_priv;
4392
4393 switch (args->madv) {
4394 case I915_MADV_DONTNEED:
4395 case I915_MADV_WILLNEED:
4396 break;
4397 default:
4398 return -EINVAL;
4399 }
4400
4401 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4402 if (obj == NULL) {
4403 DRM_ERROR("Bad handle in i915_gem_madvise_ioctl(): %d\n",
4404 args->handle);
4405 return -EBADF;
4406 }
4407
4408 mutex_lock(&dev->struct_mutex);
Daniel Vetter23010e42010-03-08 13:35:02 +01004409 obj_priv = to_intel_bo(obj);
Chris Wilson3ef94da2009-09-14 16:50:29 +01004410
4411 if (obj_priv->pin_count) {
4412 drm_gem_object_unreference(obj);
4413 mutex_unlock(&dev->struct_mutex);
4414
4415 DRM_ERROR("Attempted i915_gem_madvise_ioctl() on a pinned object\n");
4416 return -EINVAL;
4417 }
4418
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004419 if (obj_priv->madv != __I915_MADV_PURGED)
4420 obj_priv->madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004421
Chris Wilson2d7ef392009-09-20 23:13:10 +01004422 /* if the object is no longer bound, discard its backing storage */
4423 if (i915_gem_object_is_purgeable(obj_priv) &&
4424 obj_priv->gtt_space == NULL)
4425 i915_gem_object_truncate(obj);
4426
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004427 args->retained = obj_priv->madv != __I915_MADV_PURGED;
4428
Chris Wilson3ef94da2009-09-14 16:50:29 +01004429 drm_gem_object_unreference(obj);
4430 mutex_unlock(&dev->struct_mutex);
4431
4432 return 0;
4433}
4434
Daniel Vetterac52bc52010-04-09 19:05:06 +00004435struct drm_gem_object * i915_gem_alloc_object(struct drm_device *dev,
4436 size_t size)
4437{
Daniel Vetterc397b902010-04-09 19:05:07 +00004438 struct drm_i915_gem_object *obj;
4439
4440 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
4441 if (obj == NULL)
4442 return NULL;
4443
4444 if (drm_gem_object_init(dev, &obj->base, size) != 0) {
4445 kfree(obj);
4446 return NULL;
4447 }
4448
4449 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4450 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4451
4452 obj->agp_type = AGP_USER_MEMORY;
Daniel Vetter62b8b212010-04-09 19:05:08 +00004453 obj->base.driver_private = NULL;
Daniel Vetterc397b902010-04-09 19:05:07 +00004454 obj->fence_reg = I915_FENCE_REG_NONE;
4455 INIT_LIST_HEAD(&obj->list);
4456 INIT_LIST_HEAD(&obj->gpu_write_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00004457 obj->madv = I915_MADV_WILLNEED;
4458
4459 trace_i915_gem_object_create(&obj->base);
4460
4461 return &obj->base;
Daniel Vetterac52bc52010-04-09 19:05:06 +00004462}
4463
Eric Anholt673a3942008-07-30 12:06:12 -07004464int i915_gem_init_object(struct drm_gem_object *obj)
4465{
Daniel Vetterc397b902010-04-09 19:05:07 +00004466 BUG();
Jesse Barnesde151cf2008-11-12 10:03:55 -08004467
Eric Anholt673a3942008-07-30 12:06:12 -07004468 return 0;
4469}
4470
4471void i915_gem_free_object(struct drm_gem_object *obj)
4472{
Jesse Barnesde151cf2008-11-12 10:03:55 -08004473 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01004474 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004475
Chris Wilson1c5d22f2009-08-25 11:15:50 +01004476 trace_i915_gem_object_destroy(obj);
4477
Eric Anholt673a3942008-07-30 12:06:12 -07004478 while (obj_priv->pin_count > 0)
4479 i915_gem_object_unpin(obj);
4480
Dave Airlie71acb5e2008-12-30 20:31:46 +10004481 if (obj_priv->phys_obj)
4482 i915_gem_detach_phys_object(dev, obj);
4483
Eric Anholt673a3942008-07-30 12:06:12 -07004484 i915_gem_object_unbind(obj);
4485
Chris Wilson7e616152009-09-10 08:53:04 +01004486 if (obj_priv->mmap_offset)
4487 i915_gem_free_mmap_offset(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08004488
Daniel Vetterc397b902010-04-09 19:05:07 +00004489 drm_gem_object_release(obj);
4490
Eric Anholt9a298b22009-03-24 12:23:04 -07004491 kfree(obj_priv->page_cpu_valid);
Eric Anholt280b7132009-03-12 16:56:27 -07004492 kfree(obj_priv->bit_17);
Daniel Vetterc397b902010-04-09 19:05:07 +00004493 kfree(obj_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07004494}
4495
Chris Wilsonab5ee572009-09-20 19:25:47 +01004496/** Unbinds all inactive objects. */
Eric Anholt673a3942008-07-30 12:06:12 -07004497static int
Chris Wilsonab5ee572009-09-20 19:25:47 +01004498i915_gem_evict_from_inactive_list(struct drm_device *dev)
Eric Anholt673a3942008-07-30 12:06:12 -07004499{
Chris Wilsonab5ee572009-09-20 19:25:47 +01004500 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07004501
Chris Wilsonab5ee572009-09-20 19:25:47 +01004502 while (!list_empty(&dev_priv->mm.inactive_list)) {
4503 struct drm_gem_object *obj;
4504 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004505
Daniel Vettera8089e82010-04-09 19:05:09 +00004506 obj = &list_first_entry(&dev_priv->mm.inactive_list,
4507 struct drm_i915_gem_object,
4508 list)->base;
Eric Anholt673a3942008-07-30 12:06:12 -07004509
4510 ret = i915_gem_object_unbind(obj);
4511 if (ret != 0) {
Chris Wilsonab5ee572009-09-20 19:25:47 +01004512 DRM_ERROR("Error unbinding object: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07004513 return ret;
4514 }
4515 }
4516
Eric Anholt673a3942008-07-30 12:06:12 -07004517 return 0;
4518}
4519
Jesse Barnes5669fca2009-02-17 15:13:31 -08004520int
Eric Anholt673a3942008-07-30 12:06:12 -07004521i915_gem_idle(struct drm_device *dev)
4522{
4523 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson29105cc2010-01-07 10:39:13 +00004524 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004525
Keith Packard6dbe2772008-10-14 21:41:13 -07004526 mutex_lock(&dev->struct_mutex);
4527
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004528 if (dev_priv->mm.suspended ||
Zou Nan haid1b851f2010-05-21 09:08:57 +08004529 (dev_priv->render_ring.gem_object == NULL) ||
4530 (HAS_BSD(dev) &&
4531 dev_priv->bsd_ring.gem_object == NULL)) {
Keith Packard6dbe2772008-10-14 21:41:13 -07004532 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004533 return 0;
Keith Packard6dbe2772008-10-14 21:41:13 -07004534 }
Eric Anholt673a3942008-07-30 12:06:12 -07004535
Chris Wilson29105cc2010-01-07 10:39:13 +00004536 ret = i915_gpu_idle(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004537 if (ret) {
4538 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004539 return ret;
Keith Packard6dbe2772008-10-14 21:41:13 -07004540 }
Eric Anholt673a3942008-07-30 12:06:12 -07004541
Chris Wilson29105cc2010-01-07 10:39:13 +00004542 /* Under UMS, be paranoid and evict. */
4543 if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
4544 ret = i915_gem_evict_from_inactive_list(dev);
4545 if (ret) {
4546 mutex_unlock(&dev->struct_mutex);
4547 return ret;
4548 }
4549 }
4550
4551 /* Hack! Don't let anybody do execbuf while we don't control the chip.
4552 * We need to replace this with a semaphore, or something.
4553 * And not confound mm.suspended!
4554 */
4555 dev_priv->mm.suspended = 1;
4556 del_timer(&dev_priv->hangcheck_timer);
4557
4558 i915_kernel_lost_context(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004559 i915_gem_cleanup_ringbuffer(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00004560
Keith Packard6dbe2772008-10-14 21:41:13 -07004561 mutex_unlock(&dev->struct_mutex);
4562
Chris Wilson29105cc2010-01-07 10:39:13 +00004563 /* Cancel the retire work handler, which should be idle now. */
4564 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
4565
Eric Anholt673a3942008-07-30 12:06:12 -07004566 return 0;
4567}
4568
Jesse Barnese552eb72010-04-21 11:39:23 -07004569/*
4570 * 965+ support PIPE_CONTROL commands, which provide finer grained control
4571 * over cache flushing.
4572 */
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004573static int
Jesse Barnese552eb72010-04-21 11:39:23 -07004574i915_gem_init_pipe_control(struct drm_device *dev)
4575{
4576 drm_i915_private_t *dev_priv = dev->dev_private;
4577 struct drm_gem_object *obj;
4578 struct drm_i915_gem_object *obj_priv;
4579 int ret;
4580
Eric Anholt34dc4d42010-05-07 14:30:03 -07004581 obj = i915_gem_alloc_object(dev, 4096);
Jesse Barnese552eb72010-04-21 11:39:23 -07004582 if (obj == NULL) {
4583 DRM_ERROR("Failed to allocate seqno page\n");
4584 ret = -ENOMEM;
4585 goto err;
4586 }
4587 obj_priv = to_intel_bo(obj);
4588 obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
4589
4590 ret = i915_gem_object_pin(obj, 4096);
4591 if (ret)
4592 goto err_unref;
4593
4594 dev_priv->seqno_gfx_addr = obj_priv->gtt_offset;
4595 dev_priv->seqno_page = kmap(obj_priv->pages[0]);
4596 if (dev_priv->seqno_page == NULL)
4597 goto err_unpin;
4598
4599 dev_priv->seqno_obj = obj;
4600 memset(dev_priv->seqno_page, 0, PAGE_SIZE);
4601
4602 return 0;
4603
4604err_unpin:
4605 i915_gem_object_unpin(obj);
4606err_unref:
4607 drm_gem_object_unreference(obj);
4608err:
4609 return ret;
4610}
4611
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004612
4613static void
Jesse Barnese552eb72010-04-21 11:39:23 -07004614i915_gem_cleanup_pipe_control(struct drm_device *dev)
4615{
4616 drm_i915_private_t *dev_priv = dev->dev_private;
4617 struct drm_gem_object *obj;
4618 struct drm_i915_gem_object *obj_priv;
4619
4620 obj = dev_priv->seqno_obj;
4621 obj_priv = to_intel_bo(obj);
4622 kunmap(obj_priv->pages[0]);
4623 i915_gem_object_unpin(obj);
4624 drm_gem_object_unreference(obj);
4625 dev_priv->seqno_obj = NULL;
4626
4627 dev_priv->seqno_page = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07004628}
4629
Eric Anholt673a3942008-07-30 12:06:12 -07004630int
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004631i915_gem_init_ringbuffer(struct drm_device *dev)
4632{
4633 drm_i915_private_t *dev_priv = dev->dev_private;
4634 int ret;
4635 dev_priv->render_ring = render_ring;
4636 if (!I915_NEED_GFX_HWS(dev)) {
4637 dev_priv->render_ring.status_page.page_addr
4638 = dev_priv->status_page_dmah->vaddr;
4639 memset(dev_priv->render_ring.status_page.page_addr,
4640 0, PAGE_SIZE);
4641 }
4642 if (HAS_PIPE_CONTROL(dev)) {
4643 ret = i915_gem_init_pipe_control(dev);
4644 if (ret)
4645 return ret;
4646 }
4647 ret = intel_init_ring_buffer(dev, &dev_priv->render_ring);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004648 if (!ret && HAS_BSD(dev)) {
4649 dev_priv->bsd_ring = bsd_ring;
4650 ret = intel_init_ring_buffer(dev, &dev_priv->bsd_ring);
4651 }
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004652 return ret;
4653}
4654
4655void
4656i915_gem_cleanup_ringbuffer(struct drm_device *dev)
4657{
4658 drm_i915_private_t *dev_priv = dev->dev_private;
4659
4660 intel_cleanup_ring_buffer(dev, &dev_priv->render_ring);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004661 if (HAS_BSD(dev))
4662 intel_cleanup_ring_buffer(dev, &dev_priv->bsd_ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004663 if (HAS_PIPE_CONTROL(dev))
4664 i915_gem_cleanup_pipe_control(dev);
4665}
4666
4667int
Eric Anholt673a3942008-07-30 12:06:12 -07004668i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
4669 struct drm_file *file_priv)
4670{
4671 drm_i915_private_t *dev_priv = dev->dev_private;
4672 int ret;
4673
Jesse Barnes79e53942008-11-07 14:24:08 -08004674 if (drm_core_check_feature(dev, DRIVER_MODESET))
4675 return 0;
4676
Ben Gamariba1234d2009-09-14 17:48:47 -04004677 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07004678 DRM_ERROR("Reenabling wedged hardware, good luck\n");
Ben Gamariba1234d2009-09-14 17:48:47 -04004679 atomic_set(&dev_priv->mm.wedged, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004680 }
4681
Eric Anholt673a3942008-07-30 12:06:12 -07004682 mutex_lock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004683 dev_priv->mm.suspended = 0;
4684
4685 ret = i915_gem_init_ringbuffer(dev);
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004686 if (ret != 0) {
4687 mutex_unlock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004688 return ret;
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004689 }
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004690
Carl Worth5e118f42009-03-20 11:54:25 -07004691 spin_lock(&dev_priv->mm.active_list_lock);
Zou Nan hai852835f2010-05-21 09:08:56 +08004692 BUG_ON(!list_empty(&dev_priv->render_ring.active_list));
Zou Nan haid1b851f2010-05-21 09:08:57 +08004693 BUG_ON(HAS_BSD(dev) && !list_empty(&dev_priv->bsd_ring.active_list));
Carl Worth5e118f42009-03-20 11:54:25 -07004694 spin_unlock(&dev_priv->mm.active_list_lock);
4695
Eric Anholt673a3942008-07-30 12:06:12 -07004696 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
4697 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
Zou Nan hai852835f2010-05-21 09:08:56 +08004698 BUG_ON(!list_empty(&dev_priv->render_ring.request_list));
Zou Nan haid1b851f2010-05-21 09:08:57 +08004699 BUG_ON(HAS_BSD(dev) && !list_empty(&dev_priv->bsd_ring.request_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004700 mutex_unlock(&dev->struct_mutex);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004701
4702 drm_irq_install(dev);
4703
Eric Anholt673a3942008-07-30 12:06:12 -07004704 return 0;
4705}
4706
4707int
4708i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
4709 struct drm_file *file_priv)
4710{
Jesse Barnes79e53942008-11-07 14:24:08 -08004711 if (drm_core_check_feature(dev, DRIVER_MODESET))
4712 return 0;
4713
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004714 drm_irq_uninstall(dev);
Linus Torvaldse6890f62009-09-08 17:09:24 -07004715 return i915_gem_idle(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004716}
4717
4718void
4719i915_gem_lastclose(struct drm_device *dev)
4720{
4721 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004722
Eric Anholte806b492009-01-22 09:56:58 -08004723 if (drm_core_check_feature(dev, DRIVER_MODESET))
4724 return;
4725
Keith Packard6dbe2772008-10-14 21:41:13 -07004726 ret = i915_gem_idle(dev);
4727 if (ret)
4728 DRM_ERROR("failed to idle hardware: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07004729}
4730
4731void
4732i915_gem_load(struct drm_device *dev)
4733{
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004734 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07004735 drm_i915_private_t *dev_priv = dev->dev_private;
4736
Carl Worth5e118f42009-03-20 11:54:25 -07004737 spin_lock_init(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07004738 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
Daniel Vetter99fcb762010-02-07 16:20:18 +01004739 INIT_LIST_HEAD(&dev_priv->mm.gpu_write_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004740 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07004741 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Zou Nan hai852835f2010-05-21 09:08:56 +08004742 INIT_LIST_HEAD(&dev_priv->render_ring.active_list);
4743 INIT_LIST_HEAD(&dev_priv->render_ring.request_list);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004744 if (HAS_BSD(dev)) {
4745 INIT_LIST_HEAD(&dev_priv->bsd_ring.active_list);
4746 INIT_LIST_HEAD(&dev_priv->bsd_ring.request_list);
4747 }
Daniel Vetter007cc8a2010-04-28 11:02:31 +02004748 for (i = 0; i < 16; i++)
4749 INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004750 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
4751 i915_gem_retire_work_handler);
Chris Wilson31169712009-09-14 16:50:28 +01004752 spin_lock(&shrink_list_lock);
4753 list_add(&dev_priv->mm.shrink_list, &shrink_list);
4754 spin_unlock(&shrink_list_lock);
4755
Jesse Barnesde151cf2008-11-12 10:03:55 -08004756 /* Old X drivers will take 0-2 for front, back, depth buffers */
Eric Anholtb397c832010-01-26 09:43:10 -08004757 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4758 dev_priv->fence_reg_start = 3;
Jesse Barnesde151cf2008-11-12 10:03:55 -08004759
Jesse Barnes0f973f22009-01-26 17:10:45 -08004760 if (IS_I965G(dev) || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08004761 dev_priv->num_fence_regs = 16;
4762 else
4763 dev_priv->num_fence_regs = 8;
4764
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004765 /* Initialize fence registers to zero */
4766 if (IS_I965G(dev)) {
4767 for (i = 0; i < 16; i++)
4768 I915_WRITE64(FENCE_REG_965_0 + (i * 8), 0);
4769 } else {
4770 for (i = 0; i < 8; i++)
4771 I915_WRITE(FENCE_REG_830_0 + (i * 4), 0);
4772 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4773 for (i = 0; i < 8; i++)
4774 I915_WRITE(FENCE_REG_945_8 + (i * 4), 0);
4775 }
Eric Anholt673a3942008-07-30 12:06:12 -07004776 i915_gem_detect_bit_6_swizzle(dev);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05004777 init_waitqueue_head(&dev_priv->pending_flip_queue);
Eric Anholt673a3942008-07-30 12:06:12 -07004778}
Dave Airlie71acb5e2008-12-30 20:31:46 +10004779
4780/*
4781 * Create a physically contiguous memory object for this object
4782 * e.g. for cursor + overlay regs
4783 */
4784int i915_gem_init_phys_object(struct drm_device *dev,
4785 int id, int size)
4786{
4787 drm_i915_private_t *dev_priv = dev->dev_private;
4788 struct drm_i915_gem_phys_object *phys_obj;
4789 int ret;
4790
4791 if (dev_priv->mm.phys_objs[id - 1] || !size)
4792 return 0;
4793
Eric Anholt9a298b22009-03-24 12:23:04 -07004794 phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004795 if (!phys_obj)
4796 return -ENOMEM;
4797
4798 phys_obj->id = id;
4799
Zhenyu Wange6be8d92010-01-05 11:25:05 +08004800 phys_obj->handle = drm_pci_alloc(dev, size, 0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004801 if (!phys_obj->handle) {
4802 ret = -ENOMEM;
4803 goto kfree_obj;
4804 }
4805#ifdef CONFIG_X86
4806 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4807#endif
4808
4809 dev_priv->mm.phys_objs[id - 1] = phys_obj;
4810
4811 return 0;
4812kfree_obj:
Eric Anholt9a298b22009-03-24 12:23:04 -07004813 kfree(phys_obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004814 return ret;
4815}
4816
4817void i915_gem_free_phys_object(struct drm_device *dev, int id)
4818{
4819 drm_i915_private_t *dev_priv = dev->dev_private;
4820 struct drm_i915_gem_phys_object *phys_obj;
4821
4822 if (!dev_priv->mm.phys_objs[id - 1])
4823 return;
4824
4825 phys_obj = dev_priv->mm.phys_objs[id - 1];
4826 if (phys_obj->cur_obj) {
4827 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
4828 }
4829
4830#ifdef CONFIG_X86
4831 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4832#endif
4833 drm_pci_free(dev, phys_obj->handle);
4834 kfree(phys_obj);
4835 dev_priv->mm.phys_objs[id - 1] = NULL;
4836}
4837
4838void i915_gem_free_all_phys_object(struct drm_device *dev)
4839{
4840 int i;
4841
Dave Airlie260883c2009-01-22 17:58:49 +10004842 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004843 i915_gem_free_phys_object(dev, i);
4844}
4845
4846void i915_gem_detach_phys_object(struct drm_device *dev,
4847 struct drm_gem_object *obj)
4848{
4849 struct drm_i915_gem_object *obj_priv;
4850 int i;
4851 int ret;
4852 int page_count;
4853
Daniel Vetter23010e42010-03-08 13:35:02 +01004854 obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004855 if (!obj_priv->phys_obj)
4856 return;
4857
Chris Wilson4bdadb92010-01-27 13:36:32 +00004858 ret = i915_gem_object_get_pages(obj, 0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004859 if (ret)
4860 goto out;
4861
4862 page_count = obj->size / PAGE_SIZE;
4863
4864 for (i = 0; i < page_count; i++) {
Eric Anholt856fa192009-03-19 14:10:50 -07004865 char *dst = kmap_atomic(obj_priv->pages[i], KM_USER0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004866 char *src = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4867
4868 memcpy(dst, src, PAGE_SIZE);
4869 kunmap_atomic(dst, KM_USER0);
4870 }
Eric Anholt856fa192009-03-19 14:10:50 -07004871 drm_clflush_pages(obj_priv->pages, page_count);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004872 drm_agp_chipset_flush(dev);
Chris Wilsond78b47b2009-06-17 21:52:49 +01004873
4874 i915_gem_object_put_pages(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004875out:
4876 obj_priv->phys_obj->cur_obj = NULL;
4877 obj_priv->phys_obj = NULL;
4878}
4879
4880int
4881i915_gem_attach_phys_object(struct drm_device *dev,
4882 struct drm_gem_object *obj, int id)
4883{
4884 drm_i915_private_t *dev_priv = dev->dev_private;
4885 struct drm_i915_gem_object *obj_priv;
4886 int ret = 0;
4887 int page_count;
4888 int i;
4889
4890 if (id > I915_MAX_PHYS_OBJECT)
4891 return -EINVAL;
4892
Daniel Vetter23010e42010-03-08 13:35:02 +01004893 obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004894
4895 if (obj_priv->phys_obj) {
4896 if (obj_priv->phys_obj->id == id)
4897 return 0;
4898 i915_gem_detach_phys_object(dev, obj);
4899 }
4900
4901
4902 /* create a new object */
4903 if (!dev_priv->mm.phys_objs[id - 1]) {
4904 ret = i915_gem_init_phys_object(dev, id,
4905 obj->size);
4906 if (ret) {
Linus Torvaldsaeb565d2009-01-26 10:01:53 -08004907 DRM_ERROR("failed to init phys object %d size: %zu\n", id, obj->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004908 goto out;
4909 }
4910 }
4911
4912 /* bind to the object */
4913 obj_priv->phys_obj = dev_priv->mm.phys_objs[id - 1];
4914 obj_priv->phys_obj->cur_obj = obj;
4915
Chris Wilson4bdadb92010-01-27 13:36:32 +00004916 ret = i915_gem_object_get_pages(obj, 0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004917 if (ret) {
4918 DRM_ERROR("failed to get page list\n");
4919 goto out;
4920 }
4921
4922 page_count = obj->size / PAGE_SIZE;
4923
4924 for (i = 0; i < page_count; i++) {
Eric Anholt856fa192009-03-19 14:10:50 -07004925 char *src = kmap_atomic(obj_priv->pages[i], KM_USER0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004926 char *dst = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4927
4928 memcpy(dst, src, PAGE_SIZE);
4929 kunmap_atomic(src, KM_USER0);
4930 }
4931
Chris Wilsond78b47b2009-06-17 21:52:49 +01004932 i915_gem_object_put_pages(obj);
4933
Dave Airlie71acb5e2008-12-30 20:31:46 +10004934 return 0;
4935out:
4936 return ret;
4937}
4938
4939static int
4940i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
4941 struct drm_i915_gem_pwrite *args,
4942 struct drm_file *file_priv)
4943{
Daniel Vetter23010e42010-03-08 13:35:02 +01004944 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004945 void *obj_addr;
4946 int ret;
4947 char __user *user_data;
4948
4949 user_data = (char __user *) (uintptr_t) args->data_ptr;
4950 obj_addr = obj_priv->phys_obj->handle->vaddr + args->offset;
4951
Zhao Yakui44d98a62009-10-09 11:39:40 +08004952 DRM_DEBUG_DRIVER("obj_addr %p, %lld\n", obj_addr, args->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004953 ret = copy_from_user(obj_addr, user_data, args->size);
4954 if (ret)
4955 return -EFAULT;
4956
4957 drm_agp_chipset_flush(dev);
4958 return 0;
4959}
Eric Anholtb9624422009-06-03 07:27:35 +00004960
4961void i915_gem_release(struct drm_device * dev, struct drm_file *file_priv)
4962{
4963 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
4964
4965 /* Clean up our request list when the client is going away, so that
4966 * later retire_requests won't dereference our soon-to-be-gone
4967 * file_priv.
4968 */
4969 mutex_lock(&dev->struct_mutex);
4970 while (!list_empty(&i915_file_priv->mm.request_list))
4971 list_del_init(i915_file_priv->mm.request_list.next);
4972 mutex_unlock(&dev->struct_mutex);
4973}
Chris Wilson31169712009-09-14 16:50:28 +01004974
Chris Wilson31169712009-09-14 16:50:28 +01004975static int
Chris Wilson1637ef42010-04-20 17:10:35 +01004976i915_gpu_is_active(struct drm_device *dev)
4977{
4978 drm_i915_private_t *dev_priv = dev->dev_private;
4979 int lists_empty;
4980
4981 spin_lock(&dev_priv->mm.active_list_lock);
4982 lists_empty = list_empty(&dev_priv->mm.flushing_list) &&
Zou Nan hai852835f2010-05-21 09:08:56 +08004983 list_empty(&dev_priv->render_ring.active_list);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004984 if (HAS_BSD(dev))
4985 lists_empty &= list_empty(&dev_priv->bsd_ring.active_list);
Chris Wilson1637ef42010-04-20 17:10:35 +01004986 spin_unlock(&dev_priv->mm.active_list_lock);
4987
4988 return !lists_empty;
4989}
4990
4991static int
Chris Wilson31169712009-09-14 16:50:28 +01004992i915_gem_shrink(int nr_to_scan, gfp_t gfp_mask)
4993{
4994 drm_i915_private_t *dev_priv, *next_dev;
4995 struct drm_i915_gem_object *obj_priv, *next_obj;
4996 int cnt = 0;
4997 int would_deadlock = 1;
4998
4999 /* "fast-path" to count number of available objects */
5000 if (nr_to_scan == 0) {
5001 spin_lock(&shrink_list_lock);
5002 list_for_each_entry(dev_priv, &shrink_list, mm.shrink_list) {
5003 struct drm_device *dev = dev_priv->dev;
5004
5005 if (mutex_trylock(&dev->struct_mutex)) {
5006 list_for_each_entry(obj_priv,
5007 &dev_priv->mm.inactive_list,
5008 list)
5009 cnt++;
5010 mutex_unlock(&dev->struct_mutex);
5011 }
5012 }
5013 spin_unlock(&shrink_list_lock);
5014
5015 return (cnt / 100) * sysctl_vfs_cache_pressure;
5016 }
5017
5018 spin_lock(&shrink_list_lock);
5019
Chris Wilson1637ef42010-04-20 17:10:35 +01005020rescan:
Chris Wilson31169712009-09-14 16:50:28 +01005021 /* first scan for clean buffers */
5022 list_for_each_entry_safe(dev_priv, next_dev,
5023 &shrink_list, mm.shrink_list) {
5024 struct drm_device *dev = dev_priv->dev;
5025
5026 if (! mutex_trylock(&dev->struct_mutex))
5027 continue;
5028
5029 spin_unlock(&shrink_list_lock);
Zou Nan hai852835f2010-05-21 09:08:56 +08005030 i915_gem_retire_requests(dev, &dev_priv->render_ring);
Chris Wilson31169712009-09-14 16:50:28 +01005031
Zou Nan haid1b851f2010-05-21 09:08:57 +08005032 if (HAS_BSD(dev))
5033 i915_gem_retire_requests(dev, &dev_priv->bsd_ring);
5034
Chris Wilson31169712009-09-14 16:50:28 +01005035 list_for_each_entry_safe(obj_priv, next_obj,
5036 &dev_priv->mm.inactive_list,
5037 list) {
5038 if (i915_gem_object_is_purgeable(obj_priv)) {
Daniel Vettera8089e82010-04-09 19:05:09 +00005039 i915_gem_object_unbind(&obj_priv->base);
Chris Wilson31169712009-09-14 16:50:28 +01005040 if (--nr_to_scan <= 0)
5041 break;
5042 }
5043 }
5044
5045 spin_lock(&shrink_list_lock);
5046 mutex_unlock(&dev->struct_mutex);
5047
Chris Wilson963b4832009-09-20 23:03:54 +01005048 would_deadlock = 0;
5049
Chris Wilson31169712009-09-14 16:50:28 +01005050 if (nr_to_scan <= 0)
5051 break;
5052 }
5053
5054 /* second pass, evict/count anything still on the inactive list */
5055 list_for_each_entry_safe(dev_priv, next_dev,
5056 &shrink_list, mm.shrink_list) {
5057 struct drm_device *dev = dev_priv->dev;
5058
5059 if (! mutex_trylock(&dev->struct_mutex))
5060 continue;
5061
5062 spin_unlock(&shrink_list_lock);
5063
5064 list_for_each_entry_safe(obj_priv, next_obj,
5065 &dev_priv->mm.inactive_list,
5066 list) {
5067 if (nr_to_scan > 0) {
Daniel Vettera8089e82010-04-09 19:05:09 +00005068 i915_gem_object_unbind(&obj_priv->base);
Chris Wilson31169712009-09-14 16:50:28 +01005069 nr_to_scan--;
5070 } else
5071 cnt++;
5072 }
5073
5074 spin_lock(&shrink_list_lock);
5075 mutex_unlock(&dev->struct_mutex);
5076
5077 would_deadlock = 0;
5078 }
5079
Chris Wilson1637ef42010-04-20 17:10:35 +01005080 if (nr_to_scan) {
5081 int active = 0;
5082
5083 /*
5084 * We are desperate for pages, so as a last resort, wait
5085 * for the GPU to finish and discard whatever we can.
5086 * This has a dramatic impact to reduce the number of
5087 * OOM-killer events whilst running the GPU aggressively.
5088 */
5089 list_for_each_entry(dev_priv, &shrink_list, mm.shrink_list) {
5090 struct drm_device *dev = dev_priv->dev;
5091
5092 if (!mutex_trylock(&dev->struct_mutex))
5093 continue;
5094
5095 spin_unlock(&shrink_list_lock);
5096
5097 if (i915_gpu_is_active(dev)) {
5098 i915_gpu_idle(dev);
5099 active++;
5100 }
5101
5102 spin_lock(&shrink_list_lock);
5103 mutex_unlock(&dev->struct_mutex);
5104 }
5105
5106 if (active)
5107 goto rescan;
5108 }
5109
Chris Wilson31169712009-09-14 16:50:28 +01005110 spin_unlock(&shrink_list_lock);
5111
5112 if (would_deadlock)
5113 return -1;
5114 else if (cnt > 0)
5115 return (cnt / 100) * sysctl_vfs_cache_pressure;
5116 else
5117 return 0;
5118}
5119
5120static struct shrinker shrinker = {
5121 .shrink = i915_gem_shrink,
5122 .seeks = DEFAULT_SEEKS,
5123};
5124
5125__init void
5126i915_gem_shrinker_init(void)
5127{
5128 register_shrinker(&shrinker);
5129}
5130
5131__exit void
5132i915_gem_shrinker_exit(void)
5133{
5134 unregister_shrinker(&shrinker);
5135}