blob: 748c26340c35eb37c33a432dfd5df276066fda6f [file] [log] [blame]
Eric Anholt673a3942008-07-30 12:06:12 -07001/*
2 * Copyright © 2008 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 *
26 */
27
28#include "drmP.h"
29#include "drm.h"
30#include "i915_drm.h"
31#include "i915_drv.h"
Chris Wilson1c5d22f2009-08-25 11:15:50 +010032#include "i915_trace.h"
Jesse Barnes652c3932009-08-17 13:31:43 -070033#include "intel_drv.h"
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Eric Anholt673a3942008-07-30 12:06:12 -070035#include <linux/swap.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080036#include <linux/pci.h>
Eric Anholt673a3942008-07-30 12:06:12 -070037
Daniel Vetter0108a3e2010-08-07 11:01:21 +010038static uint32_t i915_gem_get_gtt_alignment(struct drm_gem_object *obj);
Chris Wilson2dafb1e2010-06-07 14:03:05 +010039static int i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj);
Eric Anholte47c68e2008-11-14 13:35:19 -080040static void i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj);
41static void i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj);
Eric Anholte47c68e2008-11-14 13:35:19 -080042static int i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj,
43 int write);
44static int i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
45 uint64_t offset,
46 uint64_t size);
47static void i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj);
Eric Anholt673a3942008-07-30 12:06:12 -070048static int i915_gem_object_wait_rendering(struct drm_gem_object *obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -080049static int i915_gem_object_bind_to_gtt(struct drm_gem_object *obj,
50 unsigned alignment);
Jesse Barnesde151cf2008-11-12 10:03:55 -080051static void i915_gem_clear_fence_reg(struct drm_gem_object *obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +100052static int i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
53 struct drm_i915_gem_pwrite *args,
54 struct drm_file *file_priv);
Chris Wilsonbe726152010-07-23 23:18:50 +010055static void i915_gem_free_object_tail(struct drm_gem_object *obj);
Eric Anholt673a3942008-07-30 12:06:12 -070056
Chris Wilson31169712009-09-14 16:50:28 +010057static LIST_HEAD(shrink_list);
58static DEFINE_SPINLOCK(shrink_list_lock);
59
Chris Wilson7d1c4802010-08-07 21:45:03 +010060static inline bool
61i915_gem_object_is_inactive(struct drm_i915_gem_object *obj_priv)
62{
63 return obj_priv->gtt_space &&
64 !obj_priv->active &&
65 obj_priv->pin_count == 0;
66}
67
Jesse Barnes79e53942008-11-07 14:24:08 -080068int i915_gem_do_init(struct drm_device *dev, unsigned long start,
69 unsigned long end)
70{
71 drm_i915_private_t *dev_priv = dev->dev_private;
72
73 if (start >= end ||
74 (start & (PAGE_SIZE - 1)) != 0 ||
75 (end & (PAGE_SIZE - 1)) != 0) {
76 return -EINVAL;
77 }
78
79 drm_mm_init(&dev_priv->mm.gtt_space, start,
80 end - start);
81
82 dev->gtt_total = (uint32_t) (end - start);
83
84 return 0;
85}
Keith Packard6dbe2772008-10-14 21:41:13 -070086
Eric Anholt673a3942008-07-30 12:06:12 -070087int
88i915_gem_init_ioctl(struct drm_device *dev, void *data,
89 struct drm_file *file_priv)
90{
Eric Anholt673a3942008-07-30 12:06:12 -070091 struct drm_i915_gem_init *args = data;
Jesse Barnes79e53942008-11-07 14:24:08 -080092 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -070093
94 mutex_lock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080095 ret = i915_gem_do_init(dev, args->gtt_start, args->gtt_end);
Eric Anholt673a3942008-07-30 12:06:12 -070096 mutex_unlock(&dev->struct_mutex);
97
Jesse Barnes79e53942008-11-07 14:24:08 -080098 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -070099}
100
Eric Anholt5a125c32008-10-22 21:40:13 -0700101int
102i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
103 struct drm_file *file_priv)
104{
Eric Anholt5a125c32008-10-22 21:40:13 -0700105 struct drm_i915_gem_get_aperture *args = data;
Eric Anholt5a125c32008-10-22 21:40:13 -0700106
107 if (!(dev->driver->driver_features & DRIVER_GEM))
108 return -ENODEV;
109
110 args->aper_size = dev->gtt_total;
Keith Packard2678d9d2008-11-20 22:54:54 -0800111 args->aper_available_size = (args->aper_size -
112 atomic_read(&dev->pin_memory));
Eric Anholt5a125c32008-10-22 21:40:13 -0700113
114 return 0;
115}
116
Eric Anholt673a3942008-07-30 12:06:12 -0700117
118/**
119 * Creates a new mm object and returns a handle to it.
120 */
121int
122i915_gem_create_ioctl(struct drm_device *dev, void *data,
123 struct drm_file *file_priv)
124{
125 struct drm_i915_gem_create *args = data;
126 struct drm_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300127 int ret;
128 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700129
130 args->size = roundup(args->size, PAGE_SIZE);
131
132 /* Allocate the new object */
Daniel Vetterac52bc52010-04-09 19:05:06 +0000133 obj = i915_gem_alloc_object(dev, args->size);
Eric Anholt673a3942008-07-30 12:06:12 -0700134 if (obj == NULL)
135 return -ENOMEM;
136
137 ret = drm_gem_handle_create(file_priv, obj, &handle);
Chris Wilson1dfd9752010-09-06 14:44:14 +0100138 if (ret) {
139 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700140 return ret;
Chris Wilson1dfd9752010-09-06 14:44:14 +0100141 }
142
143 /* Sink the floating reference from kref_init(handlecount) */
144 drm_gem_object_handle_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700145
146 args->handle = handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700147 return 0;
148}
149
Eric Anholt40123c12009-03-09 13:42:30 -0700150static inline int
Eric Anholteb014592009-03-10 11:44:52 -0700151fast_shmem_read(struct page **pages,
152 loff_t page_base, int page_offset,
153 char __user *data,
154 int length)
155{
156 char __iomem *vaddr;
Florian Mickler2bc43b52009-04-06 22:55:41 +0200157 int unwritten;
Eric Anholteb014592009-03-10 11:44:52 -0700158
159 vaddr = kmap_atomic(pages[page_base >> PAGE_SHIFT], KM_USER0);
160 if (vaddr == NULL)
161 return -ENOMEM;
Florian Mickler2bc43b52009-04-06 22:55:41 +0200162 unwritten = __copy_to_user_inatomic(data, vaddr + page_offset, length);
Eric Anholteb014592009-03-10 11:44:52 -0700163 kunmap_atomic(vaddr, KM_USER0);
164
Florian Mickler2bc43b52009-04-06 22:55:41 +0200165 if (unwritten)
166 return -EFAULT;
167
168 return 0;
Eric Anholteb014592009-03-10 11:44:52 -0700169}
170
Eric Anholt280b7132009-03-12 16:56:27 -0700171static int i915_gem_object_needs_bit17_swizzle(struct drm_gem_object *obj)
172{
173 drm_i915_private_t *dev_priv = obj->dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +0100174 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt280b7132009-03-12 16:56:27 -0700175
176 return dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_9_10_17 &&
177 obj_priv->tiling_mode != I915_TILING_NONE;
178}
179
Chris Wilson99a03df2010-05-27 14:15:34 +0100180static inline void
Eric Anholt40123c12009-03-09 13:42:30 -0700181slow_shmem_copy(struct page *dst_page,
182 int dst_offset,
183 struct page *src_page,
184 int src_offset,
185 int length)
186{
187 char *dst_vaddr, *src_vaddr;
188
Chris Wilson99a03df2010-05-27 14:15:34 +0100189 dst_vaddr = kmap(dst_page);
190 src_vaddr = kmap(src_page);
Eric Anholt40123c12009-03-09 13:42:30 -0700191
192 memcpy(dst_vaddr + dst_offset, src_vaddr + src_offset, length);
193
Chris Wilson99a03df2010-05-27 14:15:34 +0100194 kunmap(src_page);
195 kunmap(dst_page);
Eric Anholt40123c12009-03-09 13:42:30 -0700196}
197
Chris Wilson99a03df2010-05-27 14:15:34 +0100198static inline void
Eric Anholt280b7132009-03-12 16:56:27 -0700199slow_shmem_bit17_copy(struct page *gpu_page,
200 int gpu_offset,
201 struct page *cpu_page,
202 int cpu_offset,
203 int length,
204 int is_read)
205{
206 char *gpu_vaddr, *cpu_vaddr;
207
208 /* Use the unswizzled path if this page isn't affected. */
209 if ((page_to_phys(gpu_page) & (1 << 17)) == 0) {
210 if (is_read)
211 return slow_shmem_copy(cpu_page, cpu_offset,
212 gpu_page, gpu_offset, length);
213 else
214 return slow_shmem_copy(gpu_page, gpu_offset,
215 cpu_page, cpu_offset, length);
216 }
217
Chris Wilson99a03df2010-05-27 14:15:34 +0100218 gpu_vaddr = kmap(gpu_page);
219 cpu_vaddr = kmap(cpu_page);
Eric Anholt280b7132009-03-12 16:56:27 -0700220
221 /* Copy the data, XORing A6 with A17 (1). The user already knows he's
222 * XORing with the other bits (A9 for Y, A9 and A10 for X)
223 */
224 while (length > 0) {
225 int cacheline_end = ALIGN(gpu_offset + 1, 64);
226 int this_length = min(cacheline_end - gpu_offset, length);
227 int swizzled_gpu_offset = gpu_offset ^ 64;
228
229 if (is_read) {
230 memcpy(cpu_vaddr + cpu_offset,
231 gpu_vaddr + swizzled_gpu_offset,
232 this_length);
233 } else {
234 memcpy(gpu_vaddr + swizzled_gpu_offset,
235 cpu_vaddr + cpu_offset,
236 this_length);
237 }
238 cpu_offset += this_length;
239 gpu_offset += this_length;
240 length -= this_length;
241 }
242
Chris Wilson99a03df2010-05-27 14:15:34 +0100243 kunmap(cpu_page);
244 kunmap(gpu_page);
Eric Anholt280b7132009-03-12 16:56:27 -0700245}
246
Eric Anholt673a3942008-07-30 12:06:12 -0700247/**
Eric Anholteb014592009-03-10 11:44:52 -0700248 * This is the fast shmem pread path, which attempts to copy_from_user directly
249 * from the backing pages of the object to the user's address space. On a
250 * fault, it fails so we can fall back to i915_gem_shmem_pwrite_slow().
251 */
252static int
253i915_gem_shmem_pread_fast(struct drm_device *dev, struct drm_gem_object *obj,
254 struct drm_i915_gem_pread *args,
255 struct drm_file *file_priv)
256{
Daniel Vetter23010e42010-03-08 13:35:02 +0100257 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700258 ssize_t remain;
259 loff_t offset, page_base;
260 char __user *user_data;
261 int page_offset, page_length;
262 int ret;
263
264 user_data = (char __user *) (uintptr_t) args->data_ptr;
265 remain = args->size;
266
267 mutex_lock(&dev->struct_mutex);
268
Chris Wilson4bdadb92010-01-27 13:36:32 +0000269 ret = i915_gem_object_get_pages(obj, 0);
Eric Anholteb014592009-03-10 11:44:52 -0700270 if (ret != 0)
271 goto fail_unlock;
272
273 ret = i915_gem_object_set_cpu_read_domain_range(obj, args->offset,
274 args->size);
275 if (ret != 0)
276 goto fail_put_pages;
277
Daniel Vetter23010e42010-03-08 13:35:02 +0100278 obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700279 offset = args->offset;
280
281 while (remain > 0) {
282 /* Operation in this page
283 *
284 * page_base = page offset within aperture
285 * page_offset = offset within page
286 * page_length = bytes to copy for this page
287 */
288 page_base = (offset & ~(PAGE_SIZE-1));
289 page_offset = offset & (PAGE_SIZE-1);
290 page_length = remain;
291 if ((page_offset + remain) > PAGE_SIZE)
292 page_length = PAGE_SIZE - page_offset;
293
294 ret = fast_shmem_read(obj_priv->pages,
295 page_base, page_offset,
296 user_data, page_length);
297 if (ret)
298 goto fail_put_pages;
299
300 remain -= page_length;
301 user_data += page_length;
302 offset += page_length;
303 }
304
305fail_put_pages:
306 i915_gem_object_put_pages(obj);
307fail_unlock:
308 mutex_unlock(&dev->struct_mutex);
309
310 return ret;
311}
312
Chris Wilson07f73f62009-09-14 16:50:30 +0100313static int
314i915_gem_object_get_pages_or_evict(struct drm_gem_object *obj)
315{
316 int ret;
317
Chris Wilson4bdadb92010-01-27 13:36:32 +0000318 ret = i915_gem_object_get_pages(obj, __GFP_NORETRY | __GFP_NOWARN);
Chris Wilson07f73f62009-09-14 16:50:30 +0100319
320 /* If we've insufficient memory to map in the pages, attempt
321 * to make some space by throwing out some old buffers.
322 */
323 if (ret == -ENOMEM) {
324 struct drm_device *dev = obj->dev;
Chris Wilson07f73f62009-09-14 16:50:30 +0100325
Daniel Vetter0108a3e2010-08-07 11:01:21 +0100326 ret = i915_gem_evict_something(dev, obj->size,
327 i915_gem_get_gtt_alignment(obj));
Chris Wilson07f73f62009-09-14 16:50:30 +0100328 if (ret)
329 return ret;
330
Chris Wilson4bdadb92010-01-27 13:36:32 +0000331 ret = i915_gem_object_get_pages(obj, 0);
Chris Wilson07f73f62009-09-14 16:50:30 +0100332 }
333
334 return ret;
335}
336
Eric Anholteb014592009-03-10 11:44:52 -0700337/**
338 * This is the fallback shmem pread path, which allocates temporary storage
339 * in kernel space to copy_to_user into outside of the struct_mutex, so we
340 * can copy out of the object's backing pages while holding the struct mutex
341 * and not take page faults.
342 */
343static int
344i915_gem_shmem_pread_slow(struct drm_device *dev, struct drm_gem_object *obj,
345 struct drm_i915_gem_pread *args,
346 struct drm_file *file_priv)
347{
Daniel Vetter23010e42010-03-08 13:35:02 +0100348 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700349 struct mm_struct *mm = current->mm;
350 struct page **user_pages;
351 ssize_t remain;
352 loff_t offset, pinned_pages, i;
353 loff_t first_data_page, last_data_page, num_pages;
354 int shmem_page_index, shmem_page_offset;
355 int data_page_index, data_page_offset;
356 int page_length;
357 int ret;
358 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700359 int do_bit17_swizzling;
Eric Anholteb014592009-03-10 11:44:52 -0700360
361 remain = args->size;
362
363 /* Pin the user pages containing the data. We can't fault while
364 * holding the struct mutex, yet we want to hold it while
365 * dereferencing the user data.
366 */
367 first_data_page = data_ptr / PAGE_SIZE;
368 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
369 num_pages = last_data_page - first_data_page + 1;
370
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700371 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
Eric Anholteb014592009-03-10 11:44:52 -0700372 if (user_pages == NULL)
373 return -ENOMEM;
374
375 down_read(&mm->mmap_sem);
376 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
Eric Anholte5e9ecd2009-04-07 16:01:22 -0700377 num_pages, 1, 0, user_pages, NULL);
Eric Anholteb014592009-03-10 11:44:52 -0700378 up_read(&mm->mmap_sem);
379 if (pinned_pages < num_pages) {
380 ret = -EFAULT;
381 goto fail_put_user_pages;
382 }
383
Eric Anholt280b7132009-03-12 16:56:27 -0700384 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
385
Eric Anholteb014592009-03-10 11:44:52 -0700386 mutex_lock(&dev->struct_mutex);
387
Chris Wilson07f73f62009-09-14 16:50:30 +0100388 ret = i915_gem_object_get_pages_or_evict(obj);
389 if (ret)
Eric Anholteb014592009-03-10 11:44:52 -0700390 goto fail_unlock;
391
392 ret = i915_gem_object_set_cpu_read_domain_range(obj, args->offset,
393 args->size);
394 if (ret != 0)
395 goto fail_put_pages;
396
Daniel Vetter23010e42010-03-08 13:35:02 +0100397 obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700398 offset = args->offset;
399
400 while (remain > 0) {
401 /* Operation in this page
402 *
403 * shmem_page_index = page number within shmem file
404 * shmem_page_offset = offset within page in shmem file
405 * data_page_index = page number in get_user_pages return
406 * data_page_offset = offset with data_page_index page.
407 * page_length = bytes to copy for this page
408 */
409 shmem_page_index = offset / PAGE_SIZE;
410 shmem_page_offset = offset & ~PAGE_MASK;
411 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
412 data_page_offset = data_ptr & ~PAGE_MASK;
413
414 page_length = remain;
415 if ((shmem_page_offset + page_length) > PAGE_SIZE)
416 page_length = PAGE_SIZE - shmem_page_offset;
417 if ((data_page_offset + page_length) > PAGE_SIZE)
418 page_length = PAGE_SIZE - data_page_offset;
419
Eric Anholt280b7132009-03-12 16:56:27 -0700420 if (do_bit17_swizzling) {
Chris Wilson99a03df2010-05-27 14:15:34 +0100421 slow_shmem_bit17_copy(obj_priv->pages[shmem_page_index],
Eric Anholt280b7132009-03-12 16:56:27 -0700422 shmem_page_offset,
Chris Wilson99a03df2010-05-27 14:15:34 +0100423 user_pages[data_page_index],
424 data_page_offset,
425 page_length,
426 1);
427 } else {
428 slow_shmem_copy(user_pages[data_page_index],
429 data_page_offset,
430 obj_priv->pages[shmem_page_index],
431 shmem_page_offset,
432 page_length);
Eric Anholt280b7132009-03-12 16:56:27 -0700433 }
Eric Anholteb014592009-03-10 11:44:52 -0700434
435 remain -= page_length;
436 data_ptr += page_length;
437 offset += page_length;
438 }
439
440fail_put_pages:
441 i915_gem_object_put_pages(obj);
442fail_unlock:
443 mutex_unlock(&dev->struct_mutex);
444fail_put_user_pages:
445 for (i = 0; i < pinned_pages; i++) {
446 SetPageDirty(user_pages[i]);
447 page_cache_release(user_pages[i]);
448 }
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700449 drm_free_large(user_pages);
Eric Anholteb014592009-03-10 11:44:52 -0700450
451 return ret;
452}
453
Eric Anholt673a3942008-07-30 12:06:12 -0700454/**
455 * Reads data from the object referenced by handle.
456 *
457 * On error, the contents of *data are undefined.
458 */
459int
460i915_gem_pread_ioctl(struct drm_device *dev, void *data,
461 struct drm_file *file_priv)
462{
463 struct drm_i915_gem_pread *args = data;
464 struct drm_gem_object *obj;
465 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -0700466 int ret;
467
468 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
469 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100470 return -ENOENT;
Daniel Vetter23010e42010-03-08 13:35:02 +0100471 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700472
473 /* Bounds check source.
474 *
475 * XXX: This could use review for overflow issues...
476 */
477 if (args->offset > obj->size || args->size > obj->size ||
478 args->offset + args->size > obj->size) {
Luca Barbieribc9025b2010-02-09 05:49:12 +0000479 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700480 return -EINVAL;
481 }
482
Eric Anholt280b7132009-03-12 16:56:27 -0700483 if (i915_gem_object_needs_bit17_swizzle(obj)) {
Eric Anholteb014592009-03-10 11:44:52 -0700484 ret = i915_gem_shmem_pread_slow(dev, obj, args, file_priv);
Eric Anholt280b7132009-03-12 16:56:27 -0700485 } else {
486 ret = i915_gem_shmem_pread_fast(dev, obj, args, file_priv);
487 if (ret != 0)
488 ret = i915_gem_shmem_pread_slow(dev, obj, args,
489 file_priv);
490 }
Eric Anholt673a3942008-07-30 12:06:12 -0700491
Luca Barbieribc9025b2010-02-09 05:49:12 +0000492 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700493
Eric Anholteb014592009-03-10 11:44:52 -0700494 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700495}
496
Keith Packard0839ccb2008-10-30 19:38:48 -0700497/* This is the fast write path which cannot handle
498 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700499 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700500
Keith Packard0839ccb2008-10-30 19:38:48 -0700501static inline int
502fast_user_write(struct io_mapping *mapping,
503 loff_t page_base, int page_offset,
504 char __user *user_data,
505 int length)
506{
507 char *vaddr_atomic;
508 unsigned long unwritten;
509
Chris Wilsonfca3ec02010-08-04 14:34:24 +0100510 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base, KM_USER0);
Keith Packard0839ccb2008-10-30 19:38:48 -0700511 unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
512 user_data, length);
Chris Wilsonfca3ec02010-08-04 14:34:24 +0100513 io_mapping_unmap_atomic(vaddr_atomic, KM_USER0);
Keith Packard0839ccb2008-10-30 19:38:48 -0700514 if (unwritten)
515 return -EFAULT;
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700516 return 0;
Keith Packard0839ccb2008-10-30 19:38:48 -0700517}
518
519/* Here's the write path which can sleep for
520 * page faults
521 */
522
Chris Wilsonab34c222010-05-27 14:15:35 +0100523static inline void
Eric Anholt3de09aa2009-03-09 09:42:23 -0700524slow_kernel_write(struct io_mapping *mapping,
525 loff_t gtt_base, int gtt_offset,
526 struct page *user_page, int user_offset,
527 int length)
Keith Packard0839ccb2008-10-30 19:38:48 -0700528{
Chris Wilsonab34c222010-05-27 14:15:35 +0100529 char __iomem *dst_vaddr;
530 char *src_vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -0700531
Chris Wilsonab34c222010-05-27 14:15:35 +0100532 dst_vaddr = io_mapping_map_wc(mapping, gtt_base);
533 src_vaddr = kmap(user_page);
534
535 memcpy_toio(dst_vaddr + gtt_offset,
536 src_vaddr + user_offset,
537 length);
538
539 kunmap(user_page);
540 io_mapping_unmap(dst_vaddr);
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700541}
542
Eric Anholt40123c12009-03-09 13:42:30 -0700543static inline int
544fast_shmem_write(struct page **pages,
545 loff_t page_base, int page_offset,
546 char __user *data,
547 int length)
548{
549 char __iomem *vaddr;
Dave Airlied0088772009-03-28 20:29:48 -0400550 unsigned long unwritten;
Eric Anholt40123c12009-03-09 13:42:30 -0700551
552 vaddr = kmap_atomic(pages[page_base >> PAGE_SHIFT], KM_USER0);
553 if (vaddr == NULL)
554 return -ENOMEM;
Dave Airlied0088772009-03-28 20:29:48 -0400555 unwritten = __copy_from_user_inatomic(vaddr + page_offset, data, length);
Eric Anholt40123c12009-03-09 13:42:30 -0700556 kunmap_atomic(vaddr, KM_USER0);
557
Dave Airlied0088772009-03-28 20:29:48 -0400558 if (unwritten)
559 return -EFAULT;
Eric Anholt40123c12009-03-09 13:42:30 -0700560 return 0;
561}
562
Eric Anholt3de09aa2009-03-09 09:42:23 -0700563/**
564 * This is the fast pwrite path, where we copy the data directly from the
565 * user into the GTT, uncached.
566 */
Eric Anholt673a3942008-07-30 12:06:12 -0700567static int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700568i915_gem_gtt_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
569 struct drm_i915_gem_pwrite *args,
570 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700571{
Daniel Vetter23010e42010-03-08 13:35:02 +0100572 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Keith Packard0839ccb2008-10-30 19:38:48 -0700573 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700574 ssize_t remain;
Keith Packard0839ccb2008-10-30 19:38:48 -0700575 loff_t offset, page_base;
Eric Anholt673a3942008-07-30 12:06:12 -0700576 char __user *user_data;
Keith Packard0839ccb2008-10-30 19:38:48 -0700577 int page_offset, page_length;
578 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700579
580 user_data = (char __user *) (uintptr_t) args->data_ptr;
581 remain = args->size;
582 if (!access_ok(VERIFY_READ, user_data, remain))
583 return -EFAULT;
584
585
586 mutex_lock(&dev->struct_mutex);
587 ret = i915_gem_object_pin(obj, 0);
588 if (ret) {
589 mutex_unlock(&dev->struct_mutex);
590 return ret;
591 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800592 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
Eric Anholt673a3942008-07-30 12:06:12 -0700593 if (ret)
594 goto fail;
595
Daniel Vetter23010e42010-03-08 13:35:02 +0100596 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700597 offset = obj_priv->gtt_offset + args->offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700598
599 while (remain > 0) {
600 /* Operation in this page
601 *
Keith Packard0839ccb2008-10-30 19:38:48 -0700602 * page_base = page offset within aperture
603 * page_offset = offset within page
604 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -0700605 */
Keith Packard0839ccb2008-10-30 19:38:48 -0700606 page_base = (offset & ~(PAGE_SIZE-1));
607 page_offset = offset & (PAGE_SIZE-1);
608 page_length = remain;
609 if ((page_offset + remain) > PAGE_SIZE)
610 page_length = PAGE_SIZE - page_offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700611
Keith Packard0839ccb2008-10-30 19:38:48 -0700612 ret = fast_user_write (dev_priv->mm.gtt_mapping, page_base,
613 page_offset, user_data, page_length);
Eric Anholt673a3942008-07-30 12:06:12 -0700614
Keith Packard0839ccb2008-10-30 19:38:48 -0700615 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -0700616 * source page isn't available. Return the error and we'll
617 * retry in the slow path.
Keith Packard0839ccb2008-10-30 19:38:48 -0700618 */
Eric Anholt3de09aa2009-03-09 09:42:23 -0700619 if (ret)
620 goto fail;
Eric Anholt673a3942008-07-30 12:06:12 -0700621
Keith Packard0839ccb2008-10-30 19:38:48 -0700622 remain -= page_length;
623 user_data += page_length;
624 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700625 }
Eric Anholt673a3942008-07-30 12:06:12 -0700626
627fail:
628 i915_gem_object_unpin(obj);
629 mutex_unlock(&dev->struct_mutex);
630
631 return ret;
632}
633
Eric Anholt3de09aa2009-03-09 09:42:23 -0700634/**
635 * This is the fallback GTT pwrite path, which uses get_user_pages to pin
636 * the memory and maps it using kmap_atomic for copying.
637 *
638 * This code resulted in x11perf -rgb10text consuming about 10% more CPU
639 * than using i915_gem_gtt_pwrite_fast on a G45 (32-bit).
640 */
Eric Anholt3043c602008-10-02 12:24:47 -0700641static int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700642i915_gem_gtt_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
643 struct drm_i915_gem_pwrite *args,
644 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700645{
Daniel Vetter23010e42010-03-08 13:35:02 +0100646 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700647 drm_i915_private_t *dev_priv = dev->dev_private;
648 ssize_t remain;
649 loff_t gtt_page_base, offset;
650 loff_t first_data_page, last_data_page, num_pages;
651 loff_t pinned_pages, i;
652 struct page **user_pages;
653 struct mm_struct *mm = current->mm;
654 int gtt_page_offset, data_page_offset, data_page_index, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700655 int ret;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700656 uint64_t data_ptr = args->data_ptr;
657
658 remain = args->size;
659
660 /* Pin the user pages containing the data. We can't fault while
661 * holding the struct mutex, and all of the pwrite implementations
662 * want to hold it while dereferencing the user data.
663 */
664 first_data_page = data_ptr / PAGE_SIZE;
665 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
666 num_pages = last_data_page - first_data_page + 1;
667
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700668 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
Eric Anholt3de09aa2009-03-09 09:42:23 -0700669 if (user_pages == NULL)
670 return -ENOMEM;
671
672 down_read(&mm->mmap_sem);
673 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
674 num_pages, 0, 0, user_pages, NULL);
675 up_read(&mm->mmap_sem);
676 if (pinned_pages < num_pages) {
677 ret = -EFAULT;
678 goto out_unpin_pages;
679 }
680
681 mutex_lock(&dev->struct_mutex);
682 ret = i915_gem_object_pin(obj, 0);
683 if (ret)
684 goto out_unlock;
685
686 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
687 if (ret)
688 goto out_unpin_object;
689
Daniel Vetter23010e42010-03-08 13:35:02 +0100690 obj_priv = to_intel_bo(obj);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700691 offset = obj_priv->gtt_offset + args->offset;
692
693 while (remain > 0) {
694 /* Operation in this page
695 *
696 * gtt_page_base = page offset within aperture
697 * gtt_page_offset = offset within page in aperture
698 * data_page_index = page number in get_user_pages return
699 * data_page_offset = offset with data_page_index page.
700 * page_length = bytes to copy for this page
701 */
702 gtt_page_base = offset & PAGE_MASK;
703 gtt_page_offset = offset & ~PAGE_MASK;
704 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
705 data_page_offset = data_ptr & ~PAGE_MASK;
706
707 page_length = remain;
708 if ((gtt_page_offset + page_length) > PAGE_SIZE)
709 page_length = PAGE_SIZE - gtt_page_offset;
710 if ((data_page_offset + page_length) > PAGE_SIZE)
711 page_length = PAGE_SIZE - data_page_offset;
712
Chris Wilsonab34c222010-05-27 14:15:35 +0100713 slow_kernel_write(dev_priv->mm.gtt_mapping,
714 gtt_page_base, gtt_page_offset,
715 user_pages[data_page_index],
716 data_page_offset,
717 page_length);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700718
719 remain -= page_length;
720 offset += page_length;
721 data_ptr += page_length;
722 }
723
724out_unpin_object:
725 i915_gem_object_unpin(obj);
726out_unlock:
727 mutex_unlock(&dev->struct_mutex);
728out_unpin_pages:
729 for (i = 0; i < pinned_pages; i++)
730 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700731 drm_free_large(user_pages);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700732
733 return ret;
734}
735
Eric Anholt40123c12009-03-09 13:42:30 -0700736/**
737 * This is the fast shmem pwrite path, which attempts to directly
738 * copy_from_user into the kmapped pages backing the object.
739 */
Eric Anholt673a3942008-07-30 12:06:12 -0700740static int
Eric Anholt40123c12009-03-09 13:42:30 -0700741i915_gem_shmem_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
742 struct drm_i915_gem_pwrite *args,
743 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700744{
Daniel Vetter23010e42010-03-08 13:35:02 +0100745 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700746 ssize_t remain;
747 loff_t offset, page_base;
748 char __user *user_data;
749 int page_offset, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700750 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -0700751
752 user_data = (char __user *) (uintptr_t) args->data_ptr;
753 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700754
755 mutex_lock(&dev->struct_mutex);
756
Chris Wilson4bdadb92010-01-27 13:36:32 +0000757 ret = i915_gem_object_get_pages(obj, 0);
Eric Anholt40123c12009-03-09 13:42:30 -0700758 if (ret != 0)
759 goto fail_unlock;
760
Eric Anholte47c68e2008-11-14 13:35:19 -0800761 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Eric Anholt40123c12009-03-09 13:42:30 -0700762 if (ret != 0)
763 goto fail_put_pages;
Eric Anholt673a3942008-07-30 12:06:12 -0700764
Daniel Vetter23010e42010-03-08 13:35:02 +0100765 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700766 offset = args->offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700767 obj_priv->dirty = 1;
Eric Anholt673a3942008-07-30 12:06:12 -0700768
Eric Anholt40123c12009-03-09 13:42:30 -0700769 while (remain > 0) {
770 /* Operation in this page
771 *
772 * page_base = page offset within aperture
773 * page_offset = offset within page
774 * page_length = bytes to copy for this page
775 */
776 page_base = (offset & ~(PAGE_SIZE-1));
777 page_offset = offset & (PAGE_SIZE-1);
778 page_length = remain;
779 if ((page_offset + remain) > PAGE_SIZE)
780 page_length = PAGE_SIZE - page_offset;
781
782 ret = fast_shmem_write(obj_priv->pages,
783 page_base, page_offset,
784 user_data, page_length);
785 if (ret)
786 goto fail_put_pages;
787
788 remain -= page_length;
789 user_data += page_length;
790 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700791 }
792
Eric Anholt40123c12009-03-09 13:42:30 -0700793fail_put_pages:
794 i915_gem_object_put_pages(obj);
795fail_unlock:
Eric Anholt673a3942008-07-30 12:06:12 -0700796 mutex_unlock(&dev->struct_mutex);
797
Eric Anholt40123c12009-03-09 13:42:30 -0700798 return ret;
799}
800
801/**
802 * This is the fallback shmem pwrite path, which uses get_user_pages to pin
803 * the memory and maps it using kmap_atomic for copying.
804 *
805 * This avoids taking mmap_sem for faulting on the user's address while the
806 * struct_mutex is held.
807 */
808static int
809i915_gem_shmem_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
810 struct drm_i915_gem_pwrite *args,
811 struct drm_file *file_priv)
812{
Daniel Vetter23010e42010-03-08 13:35:02 +0100813 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700814 struct mm_struct *mm = current->mm;
815 struct page **user_pages;
816 ssize_t remain;
817 loff_t offset, pinned_pages, i;
818 loff_t first_data_page, last_data_page, num_pages;
819 int shmem_page_index, shmem_page_offset;
820 int data_page_index, data_page_offset;
821 int page_length;
822 int ret;
823 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700824 int do_bit17_swizzling;
Eric Anholt40123c12009-03-09 13:42:30 -0700825
826 remain = args->size;
827
828 /* Pin the user pages containing the data. We can't fault while
829 * holding the struct mutex, and all of the pwrite implementations
830 * want to hold it while dereferencing the user data.
831 */
832 first_data_page = data_ptr / PAGE_SIZE;
833 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
834 num_pages = last_data_page - first_data_page + 1;
835
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700836 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
Eric Anholt40123c12009-03-09 13:42:30 -0700837 if (user_pages == NULL)
838 return -ENOMEM;
839
840 down_read(&mm->mmap_sem);
841 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
842 num_pages, 0, 0, user_pages, NULL);
843 up_read(&mm->mmap_sem);
844 if (pinned_pages < num_pages) {
845 ret = -EFAULT;
846 goto fail_put_user_pages;
847 }
848
Eric Anholt280b7132009-03-12 16:56:27 -0700849 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
850
Eric Anholt40123c12009-03-09 13:42:30 -0700851 mutex_lock(&dev->struct_mutex);
852
Chris Wilson07f73f62009-09-14 16:50:30 +0100853 ret = i915_gem_object_get_pages_or_evict(obj);
854 if (ret)
Eric Anholt40123c12009-03-09 13:42:30 -0700855 goto fail_unlock;
856
857 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
858 if (ret != 0)
859 goto fail_put_pages;
860
Daniel Vetter23010e42010-03-08 13:35:02 +0100861 obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700862 offset = args->offset;
863 obj_priv->dirty = 1;
864
865 while (remain > 0) {
866 /* Operation in this page
867 *
868 * shmem_page_index = page number within shmem file
869 * shmem_page_offset = offset within page in shmem file
870 * data_page_index = page number in get_user_pages return
871 * data_page_offset = offset with data_page_index page.
872 * page_length = bytes to copy for this page
873 */
874 shmem_page_index = offset / PAGE_SIZE;
875 shmem_page_offset = offset & ~PAGE_MASK;
876 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
877 data_page_offset = data_ptr & ~PAGE_MASK;
878
879 page_length = remain;
880 if ((shmem_page_offset + page_length) > PAGE_SIZE)
881 page_length = PAGE_SIZE - shmem_page_offset;
882 if ((data_page_offset + page_length) > PAGE_SIZE)
883 page_length = PAGE_SIZE - data_page_offset;
884
Eric Anholt280b7132009-03-12 16:56:27 -0700885 if (do_bit17_swizzling) {
Chris Wilson99a03df2010-05-27 14:15:34 +0100886 slow_shmem_bit17_copy(obj_priv->pages[shmem_page_index],
Eric Anholt280b7132009-03-12 16:56:27 -0700887 shmem_page_offset,
888 user_pages[data_page_index],
889 data_page_offset,
Chris Wilson99a03df2010-05-27 14:15:34 +0100890 page_length,
891 0);
892 } else {
893 slow_shmem_copy(obj_priv->pages[shmem_page_index],
894 shmem_page_offset,
895 user_pages[data_page_index],
896 data_page_offset,
897 page_length);
Eric Anholt280b7132009-03-12 16:56:27 -0700898 }
Eric Anholt40123c12009-03-09 13:42:30 -0700899
900 remain -= page_length;
901 data_ptr += page_length;
902 offset += page_length;
903 }
904
905fail_put_pages:
906 i915_gem_object_put_pages(obj);
907fail_unlock:
908 mutex_unlock(&dev->struct_mutex);
909fail_put_user_pages:
910 for (i = 0; i < pinned_pages; i++)
911 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700912 drm_free_large(user_pages);
Eric Anholt40123c12009-03-09 13:42:30 -0700913
914 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700915}
916
917/**
918 * Writes data to the object referenced by handle.
919 *
920 * On error, the contents of the buffer that were to be modified are undefined.
921 */
922int
923i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
924 struct drm_file *file_priv)
925{
926 struct drm_i915_gem_pwrite *args = data;
927 struct drm_gem_object *obj;
928 struct drm_i915_gem_object *obj_priv;
929 int ret = 0;
930
931 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
932 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100933 return -ENOENT;
Daniel Vetter23010e42010-03-08 13:35:02 +0100934 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700935
936 /* Bounds check destination.
937 *
938 * XXX: This could use review for overflow issues...
939 */
940 if (args->offset > obj->size || args->size > obj->size ||
941 args->offset + args->size > obj->size) {
Luca Barbieribc9025b2010-02-09 05:49:12 +0000942 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700943 return -EINVAL;
944 }
945
946 /* We can only do the GTT pwrite on untiled buffers, as otherwise
947 * it would end up going through the fenced access, and we'll get
948 * different detiling behavior between reading and writing.
949 * pread/pwrite currently are reading and writing from the CPU
950 * perspective, requiring manual detiling by the client.
951 */
Dave Airlie71acb5e2008-12-30 20:31:46 +1000952 if (obj_priv->phys_obj)
953 ret = i915_gem_phys_pwrite(dev, obj, args, file_priv);
954 else if (obj_priv->tiling_mode == I915_TILING_NONE &&
Chris Wilson9b8c4a02010-05-27 14:21:01 +0100955 dev->gtt_total != 0 &&
956 obj->write_domain != I915_GEM_DOMAIN_CPU) {
Eric Anholt3de09aa2009-03-09 09:42:23 -0700957 ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file_priv);
958 if (ret == -EFAULT) {
959 ret = i915_gem_gtt_pwrite_slow(dev, obj, args,
960 file_priv);
961 }
Eric Anholt280b7132009-03-12 16:56:27 -0700962 } else if (i915_gem_object_needs_bit17_swizzle(obj)) {
963 ret = i915_gem_shmem_pwrite_slow(dev, obj, args, file_priv);
Eric Anholt40123c12009-03-09 13:42:30 -0700964 } else {
965 ret = i915_gem_shmem_pwrite_fast(dev, obj, args, file_priv);
966 if (ret == -EFAULT) {
967 ret = i915_gem_shmem_pwrite_slow(dev, obj, args,
968 file_priv);
969 }
970 }
Eric Anholt673a3942008-07-30 12:06:12 -0700971
972#if WATCH_PWRITE
973 if (ret)
974 DRM_INFO("pwrite failed %d\n", ret);
975#endif
976
Luca Barbieribc9025b2010-02-09 05:49:12 +0000977 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700978
979 return ret;
980}
981
982/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800983 * Called when user space prepares to use an object with the CPU, either
984 * through the mmap ioctl's mapping or a GTT mapping.
Eric Anholt673a3942008-07-30 12:06:12 -0700985 */
986int
987i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
988 struct drm_file *file_priv)
989{
Eric Anholta09ba7f2009-08-29 12:49:51 -0700990 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700991 struct drm_i915_gem_set_domain *args = data;
992 struct drm_gem_object *obj;
Jesse Barnes652c3932009-08-17 13:31:43 -0700993 struct drm_i915_gem_object *obj_priv;
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800994 uint32_t read_domains = args->read_domains;
995 uint32_t write_domain = args->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -0700996 int ret;
997
998 if (!(dev->driver->driver_features & DRIVER_GEM))
999 return -ENODEV;
1000
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001001 /* Only handle setting domains to types used by the CPU. */
Chris Wilson21d509e2009-06-06 09:46:02 +01001002 if (write_domain & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001003 return -EINVAL;
1004
Chris Wilson21d509e2009-06-06 09:46:02 +01001005 if (read_domains & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001006 return -EINVAL;
1007
1008 /* Having something in the write domain implies it's in the read
1009 * domain, and only that read domain. Enforce that in the request.
1010 */
1011 if (write_domain != 0 && read_domains != write_domain)
1012 return -EINVAL;
1013
Eric Anholt673a3942008-07-30 12:06:12 -07001014 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1015 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001016 return -ENOENT;
Daniel Vetter23010e42010-03-08 13:35:02 +01001017 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001018
1019 mutex_lock(&dev->struct_mutex);
Jesse Barnes652c3932009-08-17 13:31:43 -07001020
1021 intel_mark_busy(dev, obj);
1022
Eric Anholt673a3942008-07-30 12:06:12 -07001023#if WATCH_BUF
Krzysztof Halasacfd43c02009-06-20 00:31:28 +02001024 DRM_INFO("set_domain_ioctl %p(%zd), %08x %08x\n",
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001025 obj, obj->size, read_domains, write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07001026#endif
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001027 if (read_domains & I915_GEM_DOMAIN_GTT) {
1028 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
Eric Anholt02354392008-11-26 13:58:13 -08001029
Eric Anholta09ba7f2009-08-29 12:49:51 -07001030 /* Update the LRU on the fence for the CPU access that's
1031 * about to occur.
1032 */
1033 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001034 struct drm_i915_fence_reg *reg =
1035 &dev_priv->fence_regs[obj_priv->fence_reg];
1036 list_move_tail(&reg->lru_list,
Eric Anholta09ba7f2009-08-29 12:49:51 -07001037 &dev_priv->mm.fence_list);
1038 }
1039
Eric Anholt02354392008-11-26 13:58:13 -08001040 /* Silently promote "you're not bound, there was nothing to do"
1041 * to success, since the client was just asking us to
1042 * make sure everything was done.
1043 */
1044 if (ret == -EINVAL)
1045 ret = 0;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001046 } else {
Eric Anholte47c68e2008-11-14 13:35:19 -08001047 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001048 }
1049
Chris Wilson7d1c4802010-08-07 21:45:03 +01001050
1051 /* Maintain LRU order of "inactive" objects */
1052 if (ret == 0 && i915_gem_object_is_inactive(obj_priv))
1053 list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
1054
Eric Anholt673a3942008-07-30 12:06:12 -07001055 drm_gem_object_unreference(obj);
1056 mutex_unlock(&dev->struct_mutex);
1057 return ret;
1058}
1059
1060/**
1061 * Called when user space has done writes to this buffer
1062 */
1063int
1064i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
1065 struct drm_file *file_priv)
1066{
1067 struct drm_i915_gem_sw_finish *args = data;
1068 struct drm_gem_object *obj;
1069 struct drm_i915_gem_object *obj_priv;
1070 int ret = 0;
1071
1072 if (!(dev->driver->driver_features & DRIVER_GEM))
1073 return -ENODEV;
1074
1075 mutex_lock(&dev->struct_mutex);
1076 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1077 if (obj == NULL) {
1078 mutex_unlock(&dev->struct_mutex);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001079 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001080 }
1081
1082#if WATCH_BUF
Krzysztof Halasacfd43c02009-06-20 00:31:28 +02001083 DRM_INFO("%s: sw_finish %d (%p %zd)\n",
Eric Anholt673a3942008-07-30 12:06:12 -07001084 __func__, args->handle, obj, obj->size);
1085#endif
Daniel Vetter23010e42010-03-08 13:35:02 +01001086 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001087
1088 /* Pinned buffers may be scanout, so flush the cache */
Eric Anholte47c68e2008-11-14 13:35:19 -08001089 if (obj_priv->pin_count)
1090 i915_gem_object_flush_cpu_write_domain(obj);
1091
Eric Anholt673a3942008-07-30 12:06:12 -07001092 drm_gem_object_unreference(obj);
1093 mutex_unlock(&dev->struct_mutex);
1094 return ret;
1095}
1096
1097/**
1098 * Maps the contents of an object, returning the address it is mapped
1099 * into.
1100 *
1101 * While the mapping holds a reference on the contents of the object, it doesn't
1102 * imply a ref on the object itself.
1103 */
1104int
1105i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
1106 struct drm_file *file_priv)
1107{
1108 struct drm_i915_gem_mmap *args = data;
1109 struct drm_gem_object *obj;
1110 loff_t offset;
1111 unsigned long addr;
1112
1113 if (!(dev->driver->driver_features & DRIVER_GEM))
1114 return -ENODEV;
1115
1116 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1117 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001118 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001119
1120 offset = args->offset;
1121
1122 down_write(&current->mm->mmap_sem);
1123 addr = do_mmap(obj->filp, 0, args->size,
1124 PROT_READ | PROT_WRITE, MAP_SHARED,
1125 args->offset);
1126 up_write(&current->mm->mmap_sem);
Luca Barbieribc9025b2010-02-09 05:49:12 +00001127 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001128 if (IS_ERR((void *)addr))
1129 return addr;
1130
1131 args->addr_ptr = (uint64_t) addr;
1132
1133 return 0;
1134}
1135
Jesse Barnesde151cf2008-11-12 10:03:55 -08001136/**
1137 * i915_gem_fault - fault a page into the GTT
1138 * vma: VMA in question
1139 * vmf: fault info
1140 *
1141 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1142 * from userspace. The fault handler takes care of binding the object to
1143 * the GTT (if needed), allocating and programming a fence register (again,
1144 * only if needed based on whether the old reg is still valid or the object
1145 * is tiled) and inserting a new PTE into the faulting process.
1146 *
1147 * Note that the faulting process may involve evicting existing objects
1148 * from the GTT and/or fence registers to make room. So performance may
1149 * suffer if the GTT working set is large or there are few fence registers
1150 * left.
1151 */
1152int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1153{
1154 struct drm_gem_object *obj = vma->vm_private_data;
1155 struct drm_device *dev = obj->dev;
Chris Wilson7d1c4802010-08-07 21:45:03 +01001156 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001157 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001158 pgoff_t page_offset;
1159 unsigned long pfn;
1160 int ret = 0;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001161 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001162
1163 /* We don't use vmf->pgoff since that has the fake offset */
1164 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1165 PAGE_SHIFT;
1166
1167 /* Now bind it into the GTT if needed */
1168 mutex_lock(&dev->struct_mutex);
1169 if (!obj_priv->gtt_space) {
Chris Wilsone67b8ce2009-09-14 16:50:26 +01001170 ret = i915_gem_object_bind_to_gtt(obj, 0);
Chris Wilsonc7150892009-09-23 00:43:56 +01001171 if (ret)
1172 goto unlock;
Kristian Høgsberg07f4f3e2009-05-27 14:37:28 -04001173
Jesse Barnesde151cf2008-11-12 10:03:55 -08001174 ret = i915_gem_object_set_to_gtt_domain(obj, write);
Chris Wilsonc7150892009-09-23 00:43:56 +01001175 if (ret)
1176 goto unlock;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001177 }
1178
1179 /* Need a new fence register? */
Eric Anholta09ba7f2009-08-29 12:49:51 -07001180 if (obj_priv->tiling_mode != I915_TILING_NONE) {
Chris Wilson8c4b8c32009-06-17 22:08:52 +01001181 ret = i915_gem_object_get_fence_reg(obj);
Chris Wilsonc7150892009-09-23 00:43:56 +01001182 if (ret)
1183 goto unlock;
Eric Anholtd9ddcb92009-01-27 10:33:49 -08001184 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001185
Chris Wilson7d1c4802010-08-07 21:45:03 +01001186 if (i915_gem_object_is_inactive(obj_priv))
1187 list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
1188
Jesse Barnesde151cf2008-11-12 10:03:55 -08001189 pfn = ((dev->agp->base + obj_priv->gtt_offset) >> PAGE_SHIFT) +
1190 page_offset;
1191
1192 /* Finally, remap it using the new GTT offset */
1193 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
Chris Wilsonc7150892009-09-23 00:43:56 +01001194unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001195 mutex_unlock(&dev->struct_mutex);
1196
1197 switch (ret) {
Chris Wilsonc7150892009-09-23 00:43:56 +01001198 case 0:
1199 case -ERESTARTSYS:
1200 return VM_FAULT_NOPAGE;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001201 case -ENOMEM:
1202 case -EAGAIN:
1203 return VM_FAULT_OOM;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001204 default:
Chris Wilsonc7150892009-09-23 00:43:56 +01001205 return VM_FAULT_SIGBUS;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001206 }
1207}
1208
1209/**
1210 * i915_gem_create_mmap_offset - create a fake mmap offset for an object
1211 * @obj: obj in question
1212 *
1213 * GEM memory mapping works by handing back to userspace a fake mmap offset
1214 * it can use in a subsequent mmap(2) call. The DRM core code then looks
1215 * up the object based on the offset and sets up the various memory mapping
1216 * structures.
1217 *
1218 * This routine allocates and attaches a fake offset for @obj.
1219 */
1220static int
1221i915_gem_create_mmap_offset(struct drm_gem_object *obj)
1222{
1223 struct drm_device *dev = obj->dev;
1224 struct drm_gem_mm *mm = dev->mm_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001225 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001226 struct drm_map_list *list;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +11001227 struct drm_local_map *map;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001228 int ret = 0;
1229
1230 /* Set the object up for mmap'ing */
1231 list = &obj->map_list;
Eric Anholt9a298b22009-03-24 12:23:04 -07001232 list->map = kzalloc(sizeof(struct drm_map_list), GFP_KERNEL);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001233 if (!list->map)
1234 return -ENOMEM;
1235
1236 map = list->map;
1237 map->type = _DRM_GEM;
1238 map->size = obj->size;
1239 map->handle = obj;
1240
1241 /* Get a DRM GEM mmap offset allocated... */
1242 list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
1243 obj->size / PAGE_SIZE, 0, 0);
1244 if (!list->file_offset_node) {
1245 DRM_ERROR("failed to allocate offset for bo %d\n", obj->name);
1246 ret = -ENOMEM;
1247 goto out_free_list;
1248 }
1249
1250 list->file_offset_node = drm_mm_get_block(list->file_offset_node,
1251 obj->size / PAGE_SIZE, 0);
1252 if (!list->file_offset_node) {
1253 ret = -ENOMEM;
1254 goto out_free_list;
1255 }
1256
1257 list->hash.key = list->file_offset_node->start;
1258 if (drm_ht_insert_item(&mm->offset_hash, &list->hash)) {
1259 DRM_ERROR("failed to add to map hash\n");
Chris Wilson5618ca62009-12-02 15:15:30 +00001260 ret = -ENOMEM;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001261 goto out_free_mm;
1262 }
1263
1264 /* By now we should be all set, any drm_mmap request on the offset
1265 * below will get to our mmap & fault handler */
1266 obj_priv->mmap_offset = ((uint64_t) list->hash.key) << PAGE_SHIFT;
1267
1268 return 0;
1269
1270out_free_mm:
1271 drm_mm_put_block(list->file_offset_node);
1272out_free_list:
Eric Anholt9a298b22009-03-24 12:23:04 -07001273 kfree(list->map);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001274
1275 return ret;
1276}
1277
Chris Wilson901782b2009-07-10 08:18:50 +01001278/**
1279 * i915_gem_release_mmap - remove physical page mappings
1280 * @obj: obj in question
1281 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001282 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01001283 * relinquish ownership of the pages back to the system.
1284 *
1285 * It is vital that we remove the page mapping if we have mapped a tiled
1286 * object through the GTT and then lose the fence register due to
1287 * resource pressure. Similarly if the object has been moved out of the
1288 * aperture, than pages mapped into userspace must be revoked. Removing the
1289 * mapping will then trigger a page fault on the next user access, allowing
1290 * fixup by i915_gem_fault().
1291 */
Eric Anholtd05ca302009-07-10 13:02:26 -07001292void
Chris Wilson901782b2009-07-10 08:18:50 +01001293i915_gem_release_mmap(struct drm_gem_object *obj)
1294{
1295 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001296 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson901782b2009-07-10 08:18:50 +01001297
1298 if (dev->dev_mapping)
1299 unmap_mapping_range(dev->dev_mapping,
1300 obj_priv->mmap_offset, obj->size, 1);
1301}
1302
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001303static void
1304i915_gem_free_mmap_offset(struct drm_gem_object *obj)
1305{
1306 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001307 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001308 struct drm_gem_mm *mm = dev->mm_private;
1309 struct drm_map_list *list;
1310
1311 list = &obj->map_list;
1312 drm_ht_remove_item(&mm->offset_hash, &list->hash);
1313
1314 if (list->file_offset_node) {
1315 drm_mm_put_block(list->file_offset_node);
1316 list->file_offset_node = NULL;
1317 }
1318
1319 if (list->map) {
Eric Anholt9a298b22009-03-24 12:23:04 -07001320 kfree(list->map);
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001321 list->map = NULL;
1322 }
1323
1324 obj_priv->mmap_offset = 0;
1325}
1326
Jesse Barnesde151cf2008-11-12 10:03:55 -08001327/**
1328 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1329 * @obj: object to check
1330 *
1331 * Return the required GTT alignment for an object, taking into account
1332 * potential fence register mapping if needed.
1333 */
1334static uint32_t
1335i915_gem_get_gtt_alignment(struct drm_gem_object *obj)
1336{
1337 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001338 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001339 int start, i;
1340
1341 /*
1342 * Minimum alignment is 4k (GTT page size), but might be greater
1343 * if a fence register is needed for the object.
1344 */
1345 if (IS_I965G(dev) || obj_priv->tiling_mode == I915_TILING_NONE)
1346 return 4096;
1347
1348 /*
1349 * Previous chips need to be aligned to the size of the smallest
1350 * fence register that can contain the object.
1351 */
1352 if (IS_I9XX(dev))
1353 start = 1024*1024;
1354 else
1355 start = 512*1024;
1356
1357 for (i = start; i < obj->size; i <<= 1)
1358 ;
1359
1360 return i;
1361}
1362
1363/**
1364 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1365 * @dev: DRM device
1366 * @data: GTT mapping ioctl data
1367 * @file_priv: GEM object info
1368 *
1369 * Simply returns the fake offset to userspace so it can mmap it.
1370 * The mmap call will end up in drm_gem_mmap(), which will set things
1371 * up so we can get faults in the handler above.
1372 *
1373 * The fault handler will take care of binding the object into the GTT
1374 * (since it may have been evicted to make room for something), allocating
1375 * a fence register, and mapping the appropriate aperture address into
1376 * userspace.
1377 */
1378int
1379i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
1380 struct drm_file *file_priv)
1381{
1382 struct drm_i915_gem_mmap_gtt *args = data;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001383 struct drm_gem_object *obj;
1384 struct drm_i915_gem_object *obj_priv;
1385 int ret;
1386
1387 if (!(dev->driver->driver_features & DRIVER_GEM))
1388 return -ENODEV;
1389
1390 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1391 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001392 return -ENOENT;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001393
1394 mutex_lock(&dev->struct_mutex);
1395
Daniel Vetter23010e42010-03-08 13:35:02 +01001396 obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001397
Chris Wilsonab182822009-09-22 18:46:17 +01001398 if (obj_priv->madv != I915_MADV_WILLNEED) {
1399 DRM_ERROR("Attempting to mmap a purgeable buffer\n");
1400 drm_gem_object_unreference(obj);
1401 mutex_unlock(&dev->struct_mutex);
1402 return -EINVAL;
1403 }
1404
1405
Jesse Barnesde151cf2008-11-12 10:03:55 -08001406 if (!obj_priv->mmap_offset) {
1407 ret = i915_gem_create_mmap_offset(obj);
Chris Wilson13af1062009-02-11 14:26:31 +00001408 if (ret) {
1409 drm_gem_object_unreference(obj);
1410 mutex_unlock(&dev->struct_mutex);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001411 return ret;
Chris Wilson13af1062009-02-11 14:26:31 +00001412 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001413 }
1414
1415 args->offset = obj_priv->mmap_offset;
1416
Jesse Barnesde151cf2008-11-12 10:03:55 -08001417 /*
1418 * Pull it into the GTT so that we have a page list (makes the
1419 * initial fault faster and any subsequent flushing possible).
1420 */
1421 if (!obj_priv->agp_mem) {
Chris Wilsone67b8ce2009-09-14 16:50:26 +01001422 ret = i915_gem_object_bind_to_gtt(obj, 0);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001423 if (ret) {
1424 drm_gem_object_unreference(obj);
1425 mutex_unlock(&dev->struct_mutex);
1426 return ret;
1427 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001428 }
1429
1430 drm_gem_object_unreference(obj);
1431 mutex_unlock(&dev->struct_mutex);
1432
1433 return 0;
1434}
1435
Ben Gamari6911a9b2009-04-02 11:24:54 -07001436void
Eric Anholt856fa192009-03-19 14:10:50 -07001437i915_gem_object_put_pages(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07001438{
Daniel Vetter23010e42010-03-08 13:35:02 +01001439 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001440 int page_count = obj->size / PAGE_SIZE;
1441 int i;
1442
Eric Anholt856fa192009-03-19 14:10:50 -07001443 BUG_ON(obj_priv->pages_refcount == 0);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001444 BUG_ON(obj_priv->madv == __I915_MADV_PURGED);
Eric Anholt856fa192009-03-19 14:10:50 -07001445
1446 if (--obj_priv->pages_refcount != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07001447 return;
1448
Eric Anholt280b7132009-03-12 16:56:27 -07001449 if (obj_priv->tiling_mode != I915_TILING_NONE)
1450 i915_gem_object_save_bit_17_swizzle(obj);
1451
Chris Wilson3ef94da2009-09-14 16:50:29 +01001452 if (obj_priv->madv == I915_MADV_DONTNEED)
Chris Wilson13a05fd2009-09-20 23:03:19 +01001453 obj_priv->dirty = 0;
Chris Wilson3ef94da2009-09-14 16:50:29 +01001454
1455 for (i = 0; i < page_count; i++) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01001456 if (obj_priv->dirty)
1457 set_page_dirty(obj_priv->pages[i]);
1458
1459 if (obj_priv->madv == I915_MADV_WILLNEED)
Eric Anholt856fa192009-03-19 14:10:50 -07001460 mark_page_accessed(obj_priv->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001461
1462 page_cache_release(obj_priv->pages[i]);
1463 }
Eric Anholt673a3942008-07-30 12:06:12 -07001464 obj_priv->dirty = 0;
1465
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07001466 drm_free_large(obj_priv->pages);
Eric Anholt856fa192009-03-19 14:10:50 -07001467 obj_priv->pages = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001468}
1469
1470static void
Zou Nan hai852835f2010-05-21 09:08:56 +08001471i915_gem_object_move_to_active(struct drm_gem_object *obj, uint32_t seqno,
1472 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001473{
1474 struct drm_device *dev = obj->dev;
1475 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001476 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Zou Nan hai852835f2010-05-21 09:08:56 +08001477 BUG_ON(ring == NULL);
1478 obj_priv->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001479
1480 /* Add a reference if we're newly entering the active list. */
1481 if (!obj_priv->active) {
1482 drm_gem_object_reference(obj);
1483 obj_priv->active = 1;
1484 }
1485 /* Move from whatever list we were on to the tail of execution. */
Carl Worth5e118f42009-03-20 11:54:25 -07001486 spin_lock(&dev_priv->mm.active_list_lock);
Zou Nan hai852835f2010-05-21 09:08:56 +08001487 list_move_tail(&obj_priv->list, &ring->active_list);
Carl Worth5e118f42009-03-20 11:54:25 -07001488 spin_unlock(&dev_priv->mm.active_list_lock);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001489 obj_priv->last_rendering_seqno = seqno;
Eric Anholt673a3942008-07-30 12:06:12 -07001490}
1491
Eric Anholtce44b0e2008-11-06 16:00:31 -08001492static void
1493i915_gem_object_move_to_flushing(struct drm_gem_object *obj)
1494{
1495 struct drm_device *dev = obj->dev;
1496 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001497 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001498
1499 BUG_ON(!obj_priv->active);
1500 list_move_tail(&obj_priv->list, &dev_priv->mm.flushing_list);
1501 obj_priv->last_rendering_seqno = 0;
1502}
Eric Anholt673a3942008-07-30 12:06:12 -07001503
Chris Wilson963b4832009-09-20 23:03:54 +01001504/* Immediately discard the backing storage */
1505static void
1506i915_gem_object_truncate(struct drm_gem_object *obj)
1507{
Daniel Vetter23010e42010-03-08 13:35:02 +01001508 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001509 struct inode *inode;
Chris Wilson963b4832009-09-20 23:03:54 +01001510
Chris Wilsonae9fed62010-08-07 11:01:30 +01001511 /* Our goal here is to return as much of the memory as
1512 * is possible back to the system as we are called from OOM.
1513 * To do this we must instruct the shmfs to drop all of its
1514 * backing pages, *now*. Here we mirror the actions taken
1515 * when by shmem_delete_inode() to release the backing store.
1516 */
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001517 inode = obj->filp->f_path.dentry->d_inode;
Chris Wilsonae9fed62010-08-07 11:01:30 +01001518 truncate_inode_pages(inode->i_mapping, 0);
1519 if (inode->i_op->truncate_range)
1520 inode->i_op->truncate_range(inode, 0, (loff_t)-1);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001521
1522 obj_priv->madv = __I915_MADV_PURGED;
Chris Wilson963b4832009-09-20 23:03:54 +01001523}
1524
1525static inline int
1526i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj_priv)
1527{
1528 return obj_priv->madv == I915_MADV_DONTNEED;
1529}
1530
Eric Anholt673a3942008-07-30 12:06:12 -07001531static void
1532i915_gem_object_move_to_inactive(struct drm_gem_object *obj)
1533{
1534 struct drm_device *dev = obj->dev;
1535 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001536 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001537
1538 i915_verify_inactive(dev, __FILE__, __LINE__);
1539 if (obj_priv->pin_count != 0)
1540 list_del_init(&obj_priv->list);
1541 else
1542 list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
1543
Daniel Vetter99fcb762010-02-07 16:20:18 +01001544 BUG_ON(!list_empty(&obj_priv->gpu_write_list));
1545
Eric Anholtce44b0e2008-11-06 16:00:31 -08001546 obj_priv->last_rendering_seqno = 0;
Zou Nan hai852835f2010-05-21 09:08:56 +08001547 obj_priv->ring = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001548 if (obj_priv->active) {
1549 obj_priv->active = 0;
1550 drm_gem_object_unreference(obj);
1551 }
1552 i915_verify_inactive(dev, __FILE__, __LINE__);
1553}
1554
Daniel Vetter63560392010-02-19 11:51:59 +01001555static void
1556i915_gem_process_flushing_list(struct drm_device *dev,
Zou Nan hai852835f2010-05-21 09:08:56 +08001557 uint32_t flush_domains, uint32_t seqno,
1558 struct intel_ring_buffer *ring)
Daniel Vetter63560392010-02-19 11:51:59 +01001559{
1560 drm_i915_private_t *dev_priv = dev->dev_private;
1561 struct drm_i915_gem_object *obj_priv, *next;
1562
1563 list_for_each_entry_safe(obj_priv, next,
1564 &dev_priv->mm.gpu_write_list,
1565 gpu_write_list) {
Daniel Vettera8089e82010-04-09 19:05:09 +00001566 struct drm_gem_object *obj = &obj_priv->base;
Daniel Vetter63560392010-02-19 11:51:59 +01001567
1568 if ((obj->write_domain & flush_domains) ==
Zou Nan hai852835f2010-05-21 09:08:56 +08001569 obj->write_domain &&
1570 obj_priv->ring->ring_flag == ring->ring_flag) {
Daniel Vetter63560392010-02-19 11:51:59 +01001571 uint32_t old_write_domain = obj->write_domain;
1572
1573 obj->write_domain = 0;
1574 list_del_init(&obj_priv->gpu_write_list);
Zou Nan hai852835f2010-05-21 09:08:56 +08001575 i915_gem_object_move_to_active(obj, seqno, ring);
Daniel Vetter63560392010-02-19 11:51:59 +01001576
1577 /* update the fence lru list */
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001578 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
1579 struct drm_i915_fence_reg *reg =
1580 &dev_priv->fence_regs[obj_priv->fence_reg];
1581 list_move_tail(&reg->lru_list,
Daniel Vetter63560392010-02-19 11:51:59 +01001582 &dev_priv->mm.fence_list);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001583 }
Daniel Vetter63560392010-02-19 11:51:59 +01001584
1585 trace_i915_gem_object_change_domain(obj,
1586 obj->read_domains,
1587 old_write_domain);
1588 }
1589 }
1590}
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001591
Daniel Vetter5a5a0c62009-09-15 22:57:36 +02001592uint32_t
Eric Anholtb9624422009-06-03 07:27:35 +00001593i915_add_request(struct drm_device *dev, struct drm_file *file_priv,
Zou Nan hai852835f2010-05-21 09:08:56 +08001594 uint32_t flush_domains, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001595{
1596 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtb9624422009-06-03 07:27:35 +00001597 struct drm_i915_file_private *i915_file_priv = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001598 struct drm_i915_gem_request *request;
1599 uint32_t seqno;
1600 int was_empty;
Eric Anholt673a3942008-07-30 12:06:12 -07001601
Eric Anholtb9624422009-06-03 07:27:35 +00001602 if (file_priv != NULL)
1603 i915_file_priv = file_priv->driver_priv;
1604
Eric Anholt9a298b22009-03-24 12:23:04 -07001605 request = kzalloc(sizeof(*request), GFP_KERNEL);
Eric Anholt673a3942008-07-30 12:06:12 -07001606 if (request == NULL)
1607 return 0;
1608
Zou Nan hai852835f2010-05-21 09:08:56 +08001609 seqno = ring->add_request(dev, ring, file_priv, flush_domains);
Eric Anholt673a3942008-07-30 12:06:12 -07001610
1611 request->seqno = seqno;
Zou Nan hai852835f2010-05-21 09:08:56 +08001612 request->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001613 request->emitted_jiffies = jiffies;
Zou Nan hai852835f2010-05-21 09:08:56 +08001614 was_empty = list_empty(&ring->request_list);
1615 list_add_tail(&request->list, &ring->request_list);
1616
Eric Anholtb9624422009-06-03 07:27:35 +00001617 if (i915_file_priv) {
1618 list_add_tail(&request->client_list,
1619 &i915_file_priv->mm.request_list);
1620 } else {
1621 INIT_LIST_HEAD(&request->client_list);
1622 }
Eric Anholt673a3942008-07-30 12:06:12 -07001623
Eric Anholtce44b0e2008-11-06 16:00:31 -08001624 /* Associate any objects on the flushing list matching the write
1625 * domain we're flushing with our flush.
1626 */
Daniel Vetter63560392010-02-19 11:51:59 +01001627 if (flush_domains != 0)
Zou Nan hai852835f2010-05-21 09:08:56 +08001628 i915_gem_process_flushing_list(dev, flush_domains, seqno, ring);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001629
Ben Gamarif65d9422009-09-14 17:48:44 -04001630 if (!dev_priv->mm.suspended) {
1631 mod_timer(&dev_priv->hangcheck_timer, jiffies + DRM_I915_HANGCHECK_PERIOD);
1632 if (was_empty)
1633 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
1634 }
Eric Anholt673a3942008-07-30 12:06:12 -07001635 return seqno;
1636}
1637
1638/**
1639 * Command execution barrier
1640 *
1641 * Ensures that all commands in the ring are finished
1642 * before signalling the CPU
1643 */
Eric Anholt3043c602008-10-02 12:24:47 -07001644static uint32_t
Zou Nan hai852835f2010-05-21 09:08:56 +08001645i915_retire_commands(struct drm_device *dev, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001646{
Eric Anholt673a3942008-07-30 12:06:12 -07001647 uint32_t flush_domains = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001648
1649 /* The sampler always gets flushed on i965 (sigh) */
1650 if (IS_I965G(dev))
1651 flush_domains |= I915_GEM_DOMAIN_SAMPLER;
Zou Nan hai852835f2010-05-21 09:08:56 +08001652
1653 ring->flush(dev, ring,
1654 I915_GEM_DOMAIN_COMMAND, flush_domains);
Eric Anholt673a3942008-07-30 12:06:12 -07001655 return flush_domains;
1656}
1657
1658/**
1659 * Moves buffers associated only with the given active seqno from the active
1660 * to inactive list, potentially freeing them.
1661 */
1662static void
1663i915_gem_retire_request(struct drm_device *dev,
1664 struct drm_i915_gem_request *request)
1665{
1666 drm_i915_private_t *dev_priv = dev->dev_private;
1667
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001668 trace_i915_gem_request_retire(dev, request->seqno);
1669
Eric Anholt673a3942008-07-30 12:06:12 -07001670 /* Move any buffers on the active list that are no longer referenced
1671 * by the ringbuffer to the flushing/inactive lists as appropriate.
1672 */
Carl Worth5e118f42009-03-20 11:54:25 -07001673 spin_lock(&dev_priv->mm.active_list_lock);
Zou Nan hai852835f2010-05-21 09:08:56 +08001674 while (!list_empty(&request->ring->active_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001675 struct drm_gem_object *obj;
1676 struct drm_i915_gem_object *obj_priv;
1677
Zou Nan hai852835f2010-05-21 09:08:56 +08001678 obj_priv = list_first_entry(&request->ring->active_list,
Eric Anholt673a3942008-07-30 12:06:12 -07001679 struct drm_i915_gem_object,
1680 list);
Daniel Vettera8089e82010-04-09 19:05:09 +00001681 obj = &obj_priv->base;
Eric Anholt673a3942008-07-30 12:06:12 -07001682
1683 /* If the seqno being retired doesn't match the oldest in the
1684 * list, then the oldest in the list must still be newer than
1685 * this seqno.
1686 */
1687 if (obj_priv->last_rendering_seqno != request->seqno)
Carl Worth5e118f42009-03-20 11:54:25 -07001688 goto out;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001689
Eric Anholt673a3942008-07-30 12:06:12 -07001690#if WATCH_LRU
1691 DRM_INFO("%s: retire %d moves to inactive list %p\n",
1692 __func__, request->seqno, obj);
1693#endif
1694
Eric Anholtce44b0e2008-11-06 16:00:31 -08001695 if (obj->write_domain != 0)
1696 i915_gem_object_move_to_flushing(obj);
Shaohua Li68c84342009-04-08 10:58:23 +08001697 else {
1698 /* Take a reference on the object so it won't be
1699 * freed while the spinlock is held. The list
1700 * protection for this spinlock is safe when breaking
1701 * the lock like this since the next thing we do
1702 * is just get the head of the list again.
1703 */
1704 drm_gem_object_reference(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001705 i915_gem_object_move_to_inactive(obj);
Shaohua Li68c84342009-04-08 10:58:23 +08001706 spin_unlock(&dev_priv->mm.active_list_lock);
1707 drm_gem_object_unreference(obj);
1708 spin_lock(&dev_priv->mm.active_list_lock);
1709 }
Eric Anholt673a3942008-07-30 12:06:12 -07001710 }
Carl Worth5e118f42009-03-20 11:54:25 -07001711out:
1712 spin_unlock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001713}
1714
1715/**
1716 * Returns true if seq1 is later than seq2.
1717 */
Ben Gamari22be1722009-09-14 17:48:43 -04001718bool
Eric Anholt673a3942008-07-30 12:06:12 -07001719i915_seqno_passed(uint32_t seq1, uint32_t seq2)
1720{
1721 return (int32_t)(seq1 - seq2) >= 0;
1722}
1723
1724uint32_t
Zou Nan hai852835f2010-05-21 09:08:56 +08001725i915_get_gem_seqno(struct drm_device *dev,
Zou Nan haid1b851f2010-05-21 09:08:57 +08001726 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001727{
Zou Nan hai852835f2010-05-21 09:08:56 +08001728 return ring->get_gem_seqno(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07001729}
1730
1731/**
1732 * This function clears the request list as sequence numbers are passed.
1733 */
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001734static void
1735i915_gem_retire_requests_ring(struct drm_device *dev,
1736 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001737{
1738 drm_i915_private_t *dev_priv = dev->dev_private;
1739 uint32_t seqno;
1740
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001741 if (!ring->status_page.page_addr
Zou Nan hai852835f2010-05-21 09:08:56 +08001742 || list_empty(&ring->request_list))
Karsten Wiese6c0594a2009-02-23 15:07:57 +01001743 return;
1744
Zou Nan hai852835f2010-05-21 09:08:56 +08001745 seqno = i915_get_gem_seqno(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07001746
Zou Nan hai852835f2010-05-21 09:08:56 +08001747 while (!list_empty(&ring->request_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001748 struct drm_i915_gem_request *request;
1749 uint32_t retiring_seqno;
1750
Zou Nan hai852835f2010-05-21 09:08:56 +08001751 request = list_first_entry(&ring->request_list,
Eric Anholt673a3942008-07-30 12:06:12 -07001752 struct drm_i915_gem_request,
1753 list);
1754 retiring_seqno = request->seqno;
1755
1756 if (i915_seqno_passed(seqno, retiring_seqno) ||
Ben Gamariba1234d2009-09-14 17:48:47 -04001757 atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001758 i915_gem_retire_request(dev, request);
1759
1760 list_del(&request->list);
Eric Anholtb9624422009-06-03 07:27:35 +00001761 list_del(&request->client_list);
Eric Anholt9a298b22009-03-24 12:23:04 -07001762 kfree(request);
Eric Anholt673a3942008-07-30 12:06:12 -07001763 } else
1764 break;
1765 }
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001766
1767 if (unlikely (dev_priv->trace_irq_seqno &&
1768 i915_seqno_passed(dev_priv->trace_irq_seqno, seqno))) {
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001769
1770 ring->user_irq_put(dev, ring);
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001771 dev_priv->trace_irq_seqno = 0;
1772 }
Eric Anholt673a3942008-07-30 12:06:12 -07001773}
1774
1775void
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001776i915_gem_retire_requests(struct drm_device *dev)
1777{
1778 drm_i915_private_t *dev_priv = dev->dev_private;
1779
Chris Wilsonbe726152010-07-23 23:18:50 +01001780 if (!list_empty(&dev_priv->mm.deferred_free_list)) {
1781 struct drm_i915_gem_object *obj_priv, *tmp;
1782
1783 /* We must be careful that during unbind() we do not
1784 * accidentally infinitely recurse into retire requests.
1785 * Currently:
1786 * retire -> free -> unbind -> wait -> retire_ring
1787 */
1788 list_for_each_entry_safe(obj_priv, tmp,
1789 &dev_priv->mm.deferred_free_list,
1790 list)
1791 i915_gem_free_object_tail(&obj_priv->base);
1792 }
1793
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001794 i915_gem_retire_requests_ring(dev, &dev_priv->render_ring);
1795 if (HAS_BSD(dev))
1796 i915_gem_retire_requests_ring(dev, &dev_priv->bsd_ring);
1797}
1798
1799void
Eric Anholt673a3942008-07-30 12:06:12 -07001800i915_gem_retire_work_handler(struct work_struct *work)
1801{
1802 drm_i915_private_t *dev_priv;
1803 struct drm_device *dev;
1804
1805 dev_priv = container_of(work, drm_i915_private_t,
1806 mm.retire_work.work);
1807 dev = dev_priv->dev;
1808
1809 mutex_lock(&dev->struct_mutex);
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001810 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08001811
Keith Packard6dbe2772008-10-14 21:41:13 -07001812 if (!dev_priv->mm.suspended &&
Zou Nan haid1b851f2010-05-21 09:08:57 +08001813 (!list_empty(&dev_priv->render_ring.request_list) ||
1814 (HAS_BSD(dev) &&
1815 !list_empty(&dev_priv->bsd_ring.request_list))))
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001816 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
Eric Anholt673a3942008-07-30 12:06:12 -07001817 mutex_unlock(&dev->struct_mutex);
1818}
1819
Daniel Vetter5a5a0c62009-09-15 22:57:36 +02001820int
Zou Nan hai852835f2010-05-21 09:08:56 +08001821i915_do_wait_request(struct drm_device *dev, uint32_t seqno,
1822 int interruptible, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001823{
1824 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001825 u32 ier;
Eric Anholt673a3942008-07-30 12:06:12 -07001826 int ret = 0;
1827
1828 BUG_ON(seqno == 0);
1829
Ben Gamariba1234d2009-09-14 17:48:47 -04001830 if (atomic_read(&dev_priv->mm.wedged))
Ben Gamariffed1d02009-09-14 17:48:41 -04001831 return -EIO;
1832
Zou Nan hai852835f2010-05-21 09:08:56 +08001833 if (!i915_seqno_passed(ring->get_gem_seqno(dev, ring), seqno)) {
Eric Anholtbad720f2009-10-22 16:11:14 -07001834 if (HAS_PCH_SPLIT(dev))
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001835 ier = I915_READ(DEIER) | I915_READ(GTIER);
1836 else
1837 ier = I915_READ(IER);
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001838 if (!ier) {
1839 DRM_ERROR("something (likely vbetool) disabled "
1840 "interrupts, re-enabling\n");
1841 i915_driver_irq_preinstall(dev);
1842 i915_driver_irq_postinstall(dev);
1843 }
1844
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001845 trace_i915_gem_request_wait_begin(dev, seqno);
1846
Zou Nan hai852835f2010-05-21 09:08:56 +08001847 ring->waiting_gem_seqno = seqno;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001848 ring->user_irq_get(dev, ring);
Daniel Vetter48764bf2009-09-15 22:57:32 +02001849 if (interruptible)
Zou Nan hai852835f2010-05-21 09:08:56 +08001850 ret = wait_event_interruptible(ring->irq_queue,
1851 i915_seqno_passed(
1852 ring->get_gem_seqno(dev, ring), seqno)
1853 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02001854 else
Zou Nan hai852835f2010-05-21 09:08:56 +08001855 wait_event(ring->irq_queue,
1856 i915_seqno_passed(
1857 ring->get_gem_seqno(dev, ring), seqno)
1858 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02001859
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001860 ring->user_irq_put(dev, ring);
Zou Nan hai852835f2010-05-21 09:08:56 +08001861 ring->waiting_gem_seqno = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001862
1863 trace_i915_gem_request_wait_end(dev, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07001864 }
Ben Gamariba1234d2009-09-14 17:48:47 -04001865 if (atomic_read(&dev_priv->mm.wedged))
Eric Anholt673a3942008-07-30 12:06:12 -07001866 ret = -EIO;
1867
1868 if (ret && ret != -ERESTARTSYS)
1869 DRM_ERROR("%s returns %d (awaiting %d at %d)\n",
Zou Nan hai852835f2010-05-21 09:08:56 +08001870 __func__, ret, seqno, ring->get_gem_seqno(dev, ring));
Eric Anholt673a3942008-07-30 12:06:12 -07001871
1872 /* Directly dispatch request retiring. While we have the work queue
1873 * to handle this, the waiter on a request often wants an associated
1874 * buffer to have made it to the inactive list, and we would need
1875 * a separate wait queue to handle that.
1876 */
1877 if (ret == 0)
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001878 i915_gem_retire_requests_ring(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07001879
1880 return ret;
1881}
1882
Daniel Vetter48764bf2009-09-15 22:57:32 +02001883/**
1884 * Waits for a sequence number to be signaled, and cleans up the
1885 * request and object lists appropriately for that event.
1886 */
1887static int
Zou Nan hai852835f2010-05-21 09:08:56 +08001888i915_wait_request(struct drm_device *dev, uint32_t seqno,
1889 struct intel_ring_buffer *ring)
Daniel Vetter48764bf2009-09-15 22:57:32 +02001890{
Zou Nan hai852835f2010-05-21 09:08:56 +08001891 return i915_do_wait_request(dev, seqno, 1, ring);
Daniel Vetter48764bf2009-09-15 22:57:32 +02001892}
1893
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001894static void
1895i915_gem_flush(struct drm_device *dev,
1896 uint32_t invalidate_domains,
1897 uint32_t flush_domains)
1898{
1899 drm_i915_private_t *dev_priv = dev->dev_private;
1900 if (flush_domains & I915_GEM_DOMAIN_CPU)
1901 drm_agp_chipset_flush(dev);
1902 dev_priv->render_ring.flush(dev, &dev_priv->render_ring,
1903 invalidate_domains,
1904 flush_domains);
Zou Nan haid1b851f2010-05-21 09:08:57 +08001905
1906 if (HAS_BSD(dev))
1907 dev_priv->bsd_ring.flush(dev, &dev_priv->bsd_ring,
1908 invalidate_domains,
1909 flush_domains);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001910}
1911
Eric Anholt673a3942008-07-30 12:06:12 -07001912/**
1913 * Ensures that all rendering to the object has completed and the object is
1914 * safe to unbind from the GTT or access from the CPU.
1915 */
1916static int
1917i915_gem_object_wait_rendering(struct drm_gem_object *obj)
1918{
1919 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001920 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001921 int ret;
1922
Eric Anholte47c68e2008-11-14 13:35:19 -08001923 /* This function only exists to support waiting for existing rendering,
1924 * not for emitting required flushes.
Eric Anholt673a3942008-07-30 12:06:12 -07001925 */
Eric Anholte47c68e2008-11-14 13:35:19 -08001926 BUG_ON((obj->write_domain & I915_GEM_GPU_DOMAINS) != 0);
Eric Anholt673a3942008-07-30 12:06:12 -07001927
1928 /* If there is rendering queued on the buffer being evicted, wait for
1929 * it.
1930 */
1931 if (obj_priv->active) {
1932#if WATCH_BUF
1933 DRM_INFO("%s: object %p wait for seqno %08x\n",
1934 __func__, obj, obj_priv->last_rendering_seqno);
1935#endif
Zou Nan hai852835f2010-05-21 09:08:56 +08001936 ret = i915_wait_request(dev,
1937 obj_priv->last_rendering_seqno, obj_priv->ring);
Eric Anholt673a3942008-07-30 12:06:12 -07001938 if (ret != 0)
1939 return ret;
1940 }
1941
1942 return 0;
1943}
1944
1945/**
1946 * Unbinds an object from the GTT aperture.
1947 */
Jesse Barnes0f973f22009-01-26 17:10:45 -08001948int
Eric Anholt673a3942008-07-30 12:06:12 -07001949i915_gem_object_unbind(struct drm_gem_object *obj)
1950{
1951 struct drm_device *dev = obj->dev;
Daniel Vetter4a87b8c2010-02-19 11:51:57 +01001952 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001953 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001954 int ret = 0;
1955
1956#if WATCH_BUF
1957 DRM_INFO("%s:%d %p\n", __func__, __LINE__, obj);
1958 DRM_INFO("gtt_space %p\n", obj_priv->gtt_space);
1959#endif
1960 if (obj_priv->gtt_space == NULL)
1961 return 0;
1962
1963 if (obj_priv->pin_count != 0) {
1964 DRM_ERROR("Attempting to unbind pinned buffer\n");
1965 return -EINVAL;
1966 }
1967
Eric Anholt5323fd02009-09-09 11:50:45 -07001968 /* blow away mappings if mapped through GTT */
1969 i915_gem_release_mmap(obj);
1970
Eric Anholt673a3942008-07-30 12:06:12 -07001971 /* Move the object to the CPU domain to ensure that
1972 * any possible CPU writes while it's not in the GTT
1973 * are flushed when we go to remap it. This will
1974 * also ensure that all pending GPU writes are finished
1975 * before we unbind.
1976 */
Eric Anholte47c68e2008-11-14 13:35:19 -08001977 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Chris Wilson8dc17752010-07-23 23:18:51 +01001978 if (ret == -ERESTARTSYS)
Eric Anholt673a3942008-07-30 12:06:12 -07001979 return ret;
Chris Wilson8dc17752010-07-23 23:18:51 +01001980 /* Continue on if we fail due to EIO, the GPU is hung so we
1981 * should be safe and we need to cleanup or else we might
1982 * cause memory corruption through use-after-free.
1983 */
Eric Anholt673a3942008-07-30 12:06:12 -07001984
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
Chris Wilson8dc17752010-07-23 23:18:51 +01002017 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002018}
2019
Chris Wilsonb47eb4a2010-08-07 11:01:23 +01002020int
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002021i915_gpu_idle(struct drm_device *dev)
2022{
2023 drm_i915_private_t *dev_priv = dev->dev_private;
2024 bool lists_empty;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002025 uint32_t seqno1, seqno2;
Zou Nan hai852835f2010-05-21 09:08:56 +08002026 int ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002027
2028 spin_lock(&dev_priv->mm.active_list_lock);
Zou Nan haid1b851f2010-05-21 09:08:57 +08002029 lists_empty = (list_empty(&dev_priv->mm.flushing_list) &&
2030 list_empty(&dev_priv->render_ring.active_list) &&
2031 (!HAS_BSD(dev) ||
2032 list_empty(&dev_priv->bsd_ring.active_list)));
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002033 spin_unlock(&dev_priv->mm.active_list_lock);
2034
2035 if (lists_empty)
2036 return 0;
2037
2038 /* Flush everything onto the inactive list. */
2039 i915_gem_flush(dev, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
Zou Nan haid1b851f2010-05-21 09:08:57 +08002040 seqno1 = i915_add_request(dev, NULL, I915_GEM_GPU_DOMAINS,
Zou Nan hai852835f2010-05-21 09:08:56 +08002041 &dev_priv->render_ring);
Zou Nan haid1b851f2010-05-21 09:08:57 +08002042 if (seqno1 == 0)
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002043 return -ENOMEM;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002044 ret = i915_wait_request(dev, seqno1, &dev_priv->render_ring);
2045
2046 if (HAS_BSD(dev)) {
2047 seqno2 = i915_add_request(dev, NULL, I915_GEM_GPU_DOMAINS,
2048 &dev_priv->bsd_ring);
2049 if (seqno2 == 0)
2050 return -ENOMEM;
2051
2052 ret = i915_wait_request(dev, seqno2, &dev_priv->bsd_ring);
2053 if (ret)
2054 return ret;
2055 }
2056
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002057
Zou Nan hai852835f2010-05-21 09:08:56 +08002058 return ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002059}
2060
Ben Gamari6911a9b2009-04-02 11:24:54 -07002061int
Chris Wilson4bdadb92010-01-27 13:36:32 +00002062i915_gem_object_get_pages(struct drm_gem_object *obj,
2063 gfp_t gfpmask)
Eric Anholt673a3942008-07-30 12:06:12 -07002064{
Daniel Vetter23010e42010-03-08 13:35:02 +01002065 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002066 int page_count, i;
2067 struct address_space *mapping;
2068 struct inode *inode;
2069 struct page *page;
Eric Anholt673a3942008-07-30 12:06:12 -07002070
Daniel Vetter778c3542010-05-13 11:49:44 +02002071 BUG_ON(obj_priv->pages_refcount
2072 == DRM_I915_GEM_OBJECT_MAX_PAGES_REFCOUNT);
2073
Eric Anholt856fa192009-03-19 14:10:50 -07002074 if (obj_priv->pages_refcount++ != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07002075 return 0;
2076
2077 /* Get the list of pages out of our struct file. They'll be pinned
2078 * at this point until we release them.
2079 */
2080 page_count = obj->size / PAGE_SIZE;
Eric Anholt856fa192009-03-19 14:10:50 -07002081 BUG_ON(obj_priv->pages != NULL);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07002082 obj_priv->pages = drm_calloc_large(page_count, sizeof(struct page *));
Eric Anholt856fa192009-03-19 14:10:50 -07002083 if (obj_priv->pages == NULL) {
Eric Anholt856fa192009-03-19 14:10:50 -07002084 obj_priv->pages_refcount--;
Eric Anholt673a3942008-07-30 12:06:12 -07002085 return -ENOMEM;
2086 }
2087
2088 inode = obj->filp->f_path.dentry->d_inode;
2089 mapping = inode->i_mapping;
2090 for (i = 0; i < page_count; i++) {
Chris Wilson4bdadb92010-01-27 13:36:32 +00002091 page = read_cache_page_gfp(mapping, i,
Linus Torvalds985b8232010-07-02 10:04:42 +10002092 GFP_HIGHUSER |
Chris Wilson4bdadb92010-01-27 13:36:32 +00002093 __GFP_COLD |
Linus Torvaldscd9f0402010-07-18 09:44:37 -07002094 __GFP_RECLAIMABLE |
Chris Wilson4bdadb92010-01-27 13:36:32 +00002095 gfpmask);
Chris Wilson1f2b1012010-03-12 19:52:55 +00002096 if (IS_ERR(page))
2097 goto err_pages;
2098
Eric Anholt856fa192009-03-19 14:10:50 -07002099 obj_priv->pages[i] = page;
Eric Anholt673a3942008-07-30 12:06:12 -07002100 }
Eric Anholt280b7132009-03-12 16:56:27 -07002101
2102 if (obj_priv->tiling_mode != I915_TILING_NONE)
2103 i915_gem_object_do_bit_17_swizzle(obj);
2104
Eric Anholt673a3942008-07-30 12:06:12 -07002105 return 0;
Chris Wilson1f2b1012010-03-12 19:52:55 +00002106
2107err_pages:
2108 while (i--)
2109 page_cache_release(obj_priv->pages[i]);
2110
2111 drm_free_large(obj_priv->pages);
2112 obj_priv->pages = NULL;
2113 obj_priv->pages_refcount--;
2114 return PTR_ERR(page);
Eric Anholt673a3942008-07-30 12:06:12 -07002115}
2116
Eric Anholt4e901fd2009-10-26 16:44:17 -07002117static void sandybridge_write_fence_reg(struct drm_i915_fence_reg *reg)
2118{
2119 struct drm_gem_object *obj = reg->obj;
2120 struct drm_device *dev = obj->dev;
2121 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002122 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt4e901fd2009-10-26 16:44:17 -07002123 int regnum = obj_priv->fence_reg;
2124 uint64_t val;
2125
2126 val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
2127 0xfffff000) << 32;
2128 val |= obj_priv->gtt_offset & 0xfffff000;
2129 val |= (uint64_t)((obj_priv->stride / 128) - 1) <<
2130 SANDYBRIDGE_FENCE_PITCH_SHIFT;
2131
2132 if (obj_priv->tiling_mode == I915_TILING_Y)
2133 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2134 val |= I965_FENCE_REG_VALID;
2135
2136 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (regnum * 8), val);
2137}
2138
Jesse Barnesde151cf2008-11-12 10:03:55 -08002139static void i965_write_fence_reg(struct drm_i915_fence_reg *reg)
2140{
2141 struct drm_gem_object *obj = reg->obj;
2142 struct drm_device *dev = obj->dev;
2143 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002144 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002145 int regnum = obj_priv->fence_reg;
2146 uint64_t val;
2147
2148 val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
2149 0xfffff000) << 32;
2150 val |= obj_priv->gtt_offset & 0xfffff000;
2151 val |= ((obj_priv->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
2152 if (obj_priv->tiling_mode == I915_TILING_Y)
2153 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2154 val |= I965_FENCE_REG_VALID;
2155
2156 I915_WRITE64(FENCE_REG_965_0 + (regnum * 8), val);
2157}
2158
2159static void i915_write_fence_reg(struct drm_i915_fence_reg *reg)
2160{
2161 struct drm_gem_object *obj = reg->obj;
2162 struct drm_device *dev = obj->dev;
2163 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002164 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002165 int regnum = obj_priv->fence_reg;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002166 int tile_width;
Eric Anholtdc529a42009-03-10 22:34:49 -07002167 uint32_t fence_reg, val;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002168 uint32_t pitch_val;
2169
2170 if ((obj_priv->gtt_offset & ~I915_FENCE_START_MASK) ||
2171 (obj_priv->gtt_offset & (obj->size - 1))) {
Linus Torvaldsf06da262009-02-09 08:57:29 -08002172 WARN(1, "%s: object 0x%08x not 1M or size (0x%zx) aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002173 __func__, obj_priv->gtt_offset, obj->size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002174 return;
2175 }
2176
Jesse Barnes0f973f22009-01-26 17:10:45 -08002177 if (obj_priv->tiling_mode == I915_TILING_Y &&
2178 HAS_128_BYTE_Y_TILING(dev))
2179 tile_width = 128;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002180 else
Jesse Barnes0f973f22009-01-26 17:10:45 -08002181 tile_width = 512;
2182
2183 /* Note: pitch better be a power of two tile widths */
2184 pitch_val = obj_priv->stride / tile_width;
2185 pitch_val = ffs(pitch_val) - 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002186
Daniel Vetterc36a2a62010-04-17 15:12:03 +02002187 if (obj_priv->tiling_mode == I915_TILING_Y &&
2188 HAS_128_BYTE_Y_TILING(dev))
2189 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2190 else
2191 WARN_ON(pitch_val > I915_FENCE_MAX_PITCH_VAL);
2192
Jesse Barnesde151cf2008-11-12 10:03:55 -08002193 val = obj_priv->gtt_offset;
2194 if (obj_priv->tiling_mode == I915_TILING_Y)
2195 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2196 val |= I915_FENCE_SIZE_BITS(obj->size);
2197 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2198 val |= I830_FENCE_REG_VALID;
2199
Eric Anholtdc529a42009-03-10 22:34:49 -07002200 if (regnum < 8)
2201 fence_reg = FENCE_REG_830_0 + (regnum * 4);
2202 else
2203 fence_reg = FENCE_REG_945_8 + ((regnum - 8) * 4);
2204 I915_WRITE(fence_reg, val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002205}
2206
2207static void i830_write_fence_reg(struct drm_i915_fence_reg *reg)
2208{
2209 struct drm_gem_object *obj = reg->obj;
2210 struct drm_device *dev = obj->dev;
2211 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002212 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002213 int regnum = obj_priv->fence_reg;
2214 uint32_t val;
2215 uint32_t pitch_val;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002216 uint32_t fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002217
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002218 if ((obj_priv->gtt_offset & ~I830_FENCE_START_MASK) ||
Jesse Barnesde151cf2008-11-12 10:03:55 -08002219 (obj_priv->gtt_offset & (obj->size - 1))) {
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002220 WARN(1, "%s: object 0x%08x not 512K or size aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002221 __func__, obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002222 return;
2223 }
2224
Eric Anholte76a16d2009-05-26 17:44:56 -07002225 pitch_val = obj_priv->stride / 128;
2226 pitch_val = ffs(pitch_val) - 1;
2227 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2228
Jesse Barnesde151cf2008-11-12 10:03:55 -08002229 val = obj_priv->gtt_offset;
2230 if (obj_priv->tiling_mode == I915_TILING_Y)
2231 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002232 fence_size_bits = I830_FENCE_SIZE_BITS(obj->size);
2233 WARN_ON(fence_size_bits & ~0x00000f00);
2234 val |= fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002235 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2236 val |= I830_FENCE_REG_VALID;
2237
2238 I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002239}
2240
Daniel Vetterae3db242010-02-19 11:51:58 +01002241static int i915_find_fence_reg(struct drm_device *dev)
2242{
2243 struct drm_i915_fence_reg *reg = NULL;
2244 struct drm_i915_gem_object *obj_priv = NULL;
2245 struct drm_i915_private *dev_priv = dev->dev_private;
2246 struct drm_gem_object *obj = NULL;
2247 int i, avail, ret;
2248
2249 /* First try to find a free reg */
2250 avail = 0;
2251 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2252 reg = &dev_priv->fence_regs[i];
2253 if (!reg->obj)
2254 return i;
2255
Daniel Vetter23010e42010-03-08 13:35:02 +01002256 obj_priv = to_intel_bo(reg->obj);
Daniel Vetterae3db242010-02-19 11:51:58 +01002257 if (!obj_priv->pin_count)
2258 avail++;
2259 }
2260
2261 if (avail == 0)
2262 return -ENOSPC;
2263
2264 /* None available, try to steal one or wait for a user to finish */
2265 i = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002266 list_for_each_entry(reg, &dev_priv->mm.fence_list,
2267 lru_list) {
2268 obj = reg->obj;
2269 obj_priv = to_intel_bo(obj);
Daniel Vetterae3db242010-02-19 11:51:58 +01002270
2271 if (obj_priv->pin_count)
2272 continue;
2273
2274 /* found one! */
2275 i = obj_priv->fence_reg;
2276 break;
2277 }
2278
2279 BUG_ON(i == I915_FENCE_REG_NONE);
2280
2281 /* We only have a reference on obj from the active list. put_fence_reg
2282 * might drop that one, causing a use-after-free in it. So hold a
2283 * private reference to obj like the other callers of put_fence_reg
2284 * (set_tiling ioctl) do. */
2285 drm_gem_object_reference(obj);
2286 ret = i915_gem_object_put_fence_reg(obj);
2287 drm_gem_object_unreference(obj);
2288 if (ret != 0)
2289 return ret;
2290
2291 return i;
2292}
2293
Jesse Barnesde151cf2008-11-12 10:03:55 -08002294/**
2295 * i915_gem_object_get_fence_reg - set up a fence reg for an object
2296 * @obj: object to map through a fence reg
2297 *
2298 * When mapping objects through the GTT, userspace wants to be able to write
2299 * to them without having to worry about swizzling if the object is tiled.
2300 *
2301 * This function walks the fence regs looking for a free one for @obj,
2302 * stealing one if it can't find any.
2303 *
2304 * It then sets up the reg based on the object's properties: address, pitch
2305 * and tiling format.
2306 */
Chris Wilson8c4b8c32009-06-17 22:08:52 +01002307int
2308i915_gem_object_get_fence_reg(struct drm_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002309{
2310 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002311 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002312 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002313 struct drm_i915_fence_reg *reg = NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002314 int ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002315
Eric Anholta09ba7f2009-08-29 12:49:51 -07002316 /* Just update our place in the LRU if our fence is getting used. */
2317 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002318 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
2319 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002320 return 0;
2321 }
2322
Jesse Barnesde151cf2008-11-12 10:03:55 -08002323 switch (obj_priv->tiling_mode) {
2324 case I915_TILING_NONE:
2325 WARN(1, "allocating a fence for non-tiled object?\n");
2326 break;
2327 case I915_TILING_X:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002328 if (!obj_priv->stride)
2329 return -EINVAL;
2330 WARN((obj_priv->stride & (512 - 1)),
2331 "object 0x%08x is X tiled but has non-512B pitch\n",
2332 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002333 break;
2334 case I915_TILING_Y:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002335 if (!obj_priv->stride)
2336 return -EINVAL;
2337 WARN((obj_priv->stride & (128 - 1)),
2338 "object 0x%08x is Y tiled but has non-128B pitch\n",
2339 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002340 break;
2341 }
2342
Daniel Vetterae3db242010-02-19 11:51:58 +01002343 ret = i915_find_fence_reg(dev);
2344 if (ret < 0)
2345 return ret;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002346
Daniel Vetterae3db242010-02-19 11:51:58 +01002347 obj_priv->fence_reg = ret;
2348 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002349 list_add_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002350
Jesse Barnesde151cf2008-11-12 10:03:55 -08002351 reg->obj = obj;
2352
Eric Anholt4e901fd2009-10-26 16:44:17 -07002353 if (IS_GEN6(dev))
2354 sandybridge_write_fence_reg(reg);
2355 else if (IS_I965G(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08002356 i965_write_fence_reg(reg);
2357 else if (IS_I9XX(dev))
2358 i915_write_fence_reg(reg);
2359 else
2360 i830_write_fence_reg(reg);
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002361
Daniel Vetterae3db242010-02-19 11:51:58 +01002362 trace_i915_gem_object_get_fence(obj, obj_priv->fence_reg,
2363 obj_priv->tiling_mode);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002364
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002365 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002366}
2367
2368/**
2369 * i915_gem_clear_fence_reg - clear out fence register info
2370 * @obj: object to clear
2371 *
2372 * Zeroes out the fence register itself and clears out the associated
2373 * data structures in dev_priv and obj_priv.
2374 */
2375static void
2376i915_gem_clear_fence_reg(struct drm_gem_object *obj)
2377{
2378 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002379 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002380 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002381 struct drm_i915_fence_reg *reg =
2382 &dev_priv->fence_regs[obj_priv->fence_reg];
Jesse Barnesde151cf2008-11-12 10:03:55 -08002383
Eric Anholt4e901fd2009-10-26 16:44:17 -07002384 if (IS_GEN6(dev)) {
2385 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 +
2386 (obj_priv->fence_reg * 8), 0);
2387 } else if (IS_I965G(dev)) {
Jesse Barnesde151cf2008-11-12 10:03:55 -08002388 I915_WRITE64(FENCE_REG_965_0 + (obj_priv->fence_reg * 8), 0);
Eric Anholt4e901fd2009-10-26 16:44:17 -07002389 } else {
Eric Anholtdc529a42009-03-10 22:34:49 -07002390 uint32_t fence_reg;
2391
2392 if (obj_priv->fence_reg < 8)
2393 fence_reg = FENCE_REG_830_0 + obj_priv->fence_reg * 4;
2394 else
2395 fence_reg = FENCE_REG_945_8 + (obj_priv->fence_reg -
2396 8) * 4;
2397
2398 I915_WRITE(fence_reg, 0);
2399 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002400
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002401 reg->obj = NULL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002402 obj_priv->fence_reg = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002403 list_del_init(&reg->lru_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002404}
2405
Eric Anholt673a3942008-07-30 12:06:12 -07002406/**
Chris Wilson52dc7d32009-06-06 09:46:01 +01002407 * i915_gem_object_put_fence_reg - waits on outstanding fenced access
2408 * to the buffer to finish, and then resets the fence register.
2409 * @obj: tiled object holding a fence register.
2410 *
2411 * Zeroes out the fence register itself and clears out the associated
2412 * data structures in dev_priv and obj_priv.
2413 */
2414int
2415i915_gem_object_put_fence_reg(struct drm_gem_object *obj)
2416{
2417 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01002418 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson52dc7d32009-06-06 09:46:01 +01002419
2420 if (obj_priv->fence_reg == I915_FENCE_REG_NONE)
2421 return 0;
2422
Daniel Vetter10ae9bd2010-02-01 13:59:17 +01002423 /* If we've changed tiling, GTT-mappings of the object
2424 * need to re-fault to ensure that the correct fence register
2425 * setup is in place.
2426 */
2427 i915_gem_release_mmap(obj);
2428
Chris Wilson52dc7d32009-06-06 09:46:01 +01002429 /* On the i915, GPU access to tiled buffers is via a fence,
2430 * therefore we must wait for any outstanding access to complete
2431 * before clearing the fence.
2432 */
2433 if (!IS_I965G(dev)) {
2434 int ret;
2435
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002436 ret = i915_gem_object_flush_gpu_write_domain(obj);
2437 if (ret != 0)
2438 return ret;
2439
Chris Wilson52dc7d32009-06-06 09:46:01 +01002440 ret = i915_gem_object_wait_rendering(obj);
2441 if (ret != 0)
2442 return ret;
2443 }
2444
Daniel Vetter4a726612010-02-01 13:59:16 +01002445 i915_gem_object_flush_gtt_write_domain(obj);
Chris Wilson52dc7d32009-06-06 09:46:01 +01002446 i915_gem_clear_fence_reg (obj);
2447
2448 return 0;
2449}
2450
2451/**
Eric Anholt673a3942008-07-30 12:06:12 -07002452 * Finds free space in the GTT aperture and binds the object there.
2453 */
2454static int
2455i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment)
2456{
2457 struct drm_device *dev = obj->dev;
2458 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002459 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002460 struct drm_mm_node *free_space;
Chris Wilson4bdadb92010-01-27 13:36:32 +00002461 gfp_t gfpmask = __GFP_NORETRY | __GFP_NOWARN;
Chris Wilson07f73f62009-09-14 16:50:30 +01002462 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002463
Chris Wilsonbb6baf72009-09-22 14:24:13 +01002464 if (obj_priv->madv != I915_MADV_WILLNEED) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01002465 DRM_ERROR("Attempting to bind a purgeable object\n");
2466 return -EINVAL;
2467 }
2468
Eric Anholt673a3942008-07-30 12:06:12 -07002469 if (alignment == 0)
Jesse Barnes0f973f22009-01-26 17:10:45 -08002470 alignment = i915_gem_get_gtt_alignment(obj);
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002471 if (alignment & (i915_gem_get_gtt_alignment(obj) - 1)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002472 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2473 return -EINVAL;
2474 }
2475
Chris Wilson654fc602010-05-27 13:18:21 +01002476 /* If the object is bigger than the entire aperture, reject it early
2477 * before evicting everything in a vain attempt to find space.
2478 */
2479 if (obj->size > dev->gtt_total) {
2480 DRM_ERROR("Attempting to bind an object larger than the aperture\n");
2481 return -E2BIG;
2482 }
2483
Eric Anholt673a3942008-07-30 12:06:12 -07002484 search_free:
2485 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
2486 obj->size, alignment, 0);
2487 if (free_space != NULL) {
2488 obj_priv->gtt_space = drm_mm_get_block(free_space, obj->size,
2489 alignment);
Daniel Vetterdb3307a2010-07-02 15:02:12 +01002490 if (obj_priv->gtt_space != NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002491 obj_priv->gtt_offset = obj_priv->gtt_space->start;
Eric Anholt673a3942008-07-30 12:06:12 -07002492 }
2493 if (obj_priv->gtt_space == NULL) {
2494 /* If the gtt is empty and we're still having trouble
2495 * fitting our object in, we're out of memory.
2496 */
2497#if WATCH_LRU
2498 DRM_INFO("%s: GTT full, evicting something\n", __func__);
2499#endif
Daniel Vetter0108a3e2010-08-07 11:01:21 +01002500 ret = i915_gem_evict_something(dev, obj->size, alignment);
Chris Wilson97311292009-09-21 00:22:34 +01002501 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002502 return ret;
Chris Wilson97311292009-09-21 00:22:34 +01002503
Eric Anholt673a3942008-07-30 12:06:12 -07002504 goto search_free;
2505 }
2506
2507#if WATCH_BUF
Krzysztof Halasacfd43c02009-06-20 00:31:28 +02002508 DRM_INFO("Binding object of size %zd at 0x%08x\n",
Eric Anholt673a3942008-07-30 12:06:12 -07002509 obj->size, obj_priv->gtt_offset);
2510#endif
Chris Wilson4bdadb92010-01-27 13:36:32 +00002511 ret = i915_gem_object_get_pages(obj, gfpmask);
Eric Anholt673a3942008-07-30 12:06:12 -07002512 if (ret) {
2513 drm_mm_put_block(obj_priv->gtt_space);
2514 obj_priv->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002515
2516 if (ret == -ENOMEM) {
2517 /* first try to clear up some space from the GTT */
Daniel Vetter0108a3e2010-08-07 11:01:21 +01002518 ret = i915_gem_evict_something(dev, obj->size,
2519 alignment);
Chris Wilson07f73f62009-09-14 16:50:30 +01002520 if (ret) {
Chris Wilson07f73f62009-09-14 16:50:30 +01002521 /* now try to shrink everyone else */
Chris Wilson4bdadb92010-01-27 13:36:32 +00002522 if (gfpmask) {
2523 gfpmask = 0;
2524 goto search_free;
Chris Wilson07f73f62009-09-14 16:50:30 +01002525 }
2526
2527 return ret;
2528 }
2529
2530 goto search_free;
2531 }
2532
Eric Anholt673a3942008-07-30 12:06:12 -07002533 return ret;
2534 }
2535
Eric Anholt673a3942008-07-30 12:06:12 -07002536 /* Create an AGP memory structure pointing at our pages, and bind it
2537 * into the GTT.
2538 */
2539 obj_priv->agp_mem = drm_agp_bind_pages(dev,
Eric Anholt856fa192009-03-19 14:10:50 -07002540 obj_priv->pages,
Chris Wilson07f73f62009-09-14 16:50:30 +01002541 obj->size >> PAGE_SHIFT,
Keith Packardba1eb1d2008-10-14 19:55:10 -07002542 obj_priv->gtt_offset,
2543 obj_priv->agp_type);
Eric Anholt673a3942008-07-30 12:06:12 -07002544 if (obj_priv->agp_mem == NULL) {
Eric Anholt856fa192009-03-19 14:10:50 -07002545 i915_gem_object_put_pages(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002546 drm_mm_put_block(obj_priv->gtt_space);
2547 obj_priv->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002548
Daniel Vetter0108a3e2010-08-07 11:01:21 +01002549 ret = i915_gem_evict_something(dev, obj->size, alignment);
Chris Wilson97311292009-09-21 00:22:34 +01002550 if (ret)
Chris Wilson07f73f62009-09-14 16:50:30 +01002551 return ret;
Chris Wilson07f73f62009-09-14 16:50:30 +01002552
2553 goto search_free;
Eric Anholt673a3942008-07-30 12:06:12 -07002554 }
2555 atomic_inc(&dev->gtt_count);
2556 atomic_add(obj->size, &dev->gtt_memory);
2557
Chris Wilsonbf1a1092010-08-07 11:01:20 +01002558 /* keep track of bounds object by adding it to the inactive list */
2559 list_add_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
2560
Eric Anholt673a3942008-07-30 12:06:12 -07002561 /* Assert that the object is not currently in any GPU domain. As it
2562 * wasn't in the GTT, there shouldn't be any way it could have been in
2563 * a GPU cache
2564 */
Chris Wilson21d509e2009-06-06 09:46:02 +01002565 BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS);
2566 BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS);
Eric Anholt673a3942008-07-30 12:06:12 -07002567
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002568 trace_i915_gem_object_bind(obj, obj_priv->gtt_offset);
2569
Eric Anholt673a3942008-07-30 12:06:12 -07002570 return 0;
2571}
2572
2573void
2574i915_gem_clflush_object(struct drm_gem_object *obj)
2575{
Daniel Vetter23010e42010-03-08 13:35:02 +01002576 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002577
2578 /* If we don't have a page list set up, then we're not pinned
2579 * to GPU, and we can ignore the cache flush because it'll happen
2580 * again at bind time.
2581 */
Eric Anholt856fa192009-03-19 14:10:50 -07002582 if (obj_priv->pages == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002583 return;
2584
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002585 trace_i915_gem_object_clflush(obj);
Eric Anholtcfa16a02009-05-26 18:46:16 -07002586
Eric Anholt856fa192009-03-19 14:10:50 -07002587 drm_clflush_pages(obj_priv->pages, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002588}
2589
Eric Anholte47c68e2008-11-14 13:35:19 -08002590/** Flushes any GPU write domain for the object if it's dirty. */
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002591static int
Eric Anholte47c68e2008-11-14 13:35:19 -08002592i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj)
2593{
2594 struct drm_device *dev = obj->dev;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002595 uint32_t old_write_domain;
Zou Nan hai852835f2010-05-21 09:08:56 +08002596 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08002597
2598 if ((obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002599 return 0;
Eric Anholte47c68e2008-11-14 13:35:19 -08002600
2601 /* Queue the GPU write cache flushing we need. */
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002602 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002603 i915_gem_flush(dev, 0, obj->write_domain);
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002604 if (i915_add_request(dev, NULL, obj->write_domain, obj_priv->ring) == 0)
2605 return -ENOMEM;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002606
2607 trace_i915_gem_object_change_domain(obj,
2608 obj->read_domains,
2609 old_write_domain);
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002610 return 0;
Eric Anholte47c68e2008-11-14 13:35:19 -08002611}
2612
2613/** Flushes the GTT write domain for the object if it's dirty. */
2614static void
2615i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj)
2616{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002617 uint32_t old_write_domain;
2618
Eric Anholte47c68e2008-11-14 13:35:19 -08002619 if (obj->write_domain != I915_GEM_DOMAIN_GTT)
2620 return;
2621
2622 /* No actual flushing is required for the GTT write domain. Writes
2623 * to it immediately go to main memory as far as we know, so there's
2624 * no chipset flush. It also doesn't land in render cache.
2625 */
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002626 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002627 obj->write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002628
2629 trace_i915_gem_object_change_domain(obj,
2630 obj->read_domains,
2631 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002632}
2633
2634/** Flushes the CPU write domain for the object if it's dirty. */
2635static void
2636i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj)
2637{
2638 struct drm_device *dev = obj->dev;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002639 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002640
2641 if (obj->write_domain != I915_GEM_DOMAIN_CPU)
2642 return;
2643
2644 i915_gem_clflush_object(obj);
2645 drm_agp_chipset_flush(dev);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002646 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002647 obj->write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002648
2649 trace_i915_gem_object_change_domain(obj,
2650 obj->read_domains,
2651 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002652}
2653
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002654int
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002655i915_gem_object_flush_write_domain(struct drm_gem_object *obj)
2656{
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002657 int ret = 0;
2658
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002659 switch (obj->write_domain) {
2660 case I915_GEM_DOMAIN_GTT:
2661 i915_gem_object_flush_gtt_write_domain(obj);
2662 break;
2663 case I915_GEM_DOMAIN_CPU:
2664 i915_gem_object_flush_cpu_write_domain(obj);
2665 break;
2666 default:
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002667 ret = i915_gem_object_flush_gpu_write_domain(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002668 break;
2669 }
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002670
2671 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002672}
2673
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002674/**
2675 * Moves a single object to the GTT read, and possibly write domain.
2676 *
2677 * This function returns when the move is complete, including waiting on
2678 * flushes to occur.
2679 */
Jesse Barnes79e53942008-11-07 14:24:08 -08002680int
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002681i915_gem_object_set_to_gtt_domain(struct drm_gem_object *obj, int write)
2682{
Daniel Vetter23010e42010-03-08 13:35:02 +01002683 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002684 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002685 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002686
Eric Anholt02354392008-11-26 13:58:13 -08002687 /* Not valid to be called on unbound objects. */
2688 if (obj_priv->gtt_space == NULL)
2689 return -EINVAL;
2690
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002691 ret = i915_gem_object_flush_gpu_write_domain(obj);
2692 if (ret != 0)
2693 return ret;
2694
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002695 /* Wait on any GPU rendering and flushing to occur. */
Eric Anholte47c68e2008-11-14 13:35:19 -08002696 ret = i915_gem_object_wait_rendering(obj);
2697 if (ret != 0)
2698 return ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002699
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002700 old_write_domain = obj->write_domain;
2701 old_read_domains = obj->read_domains;
2702
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002703 /* If we're writing through the GTT domain, then CPU and GPU caches
2704 * will need to be invalidated at next use.
2705 */
2706 if (write)
Eric Anholte47c68e2008-11-14 13:35:19 -08002707 obj->read_domains &= I915_GEM_DOMAIN_GTT;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002708
Eric Anholte47c68e2008-11-14 13:35:19 -08002709 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002710
2711 /* It should now be out of any other write domains, and we can update
2712 * the domain values for our changes.
2713 */
2714 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2715 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002716 if (write) {
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002717 obj->write_domain = I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002718 obj_priv->dirty = 1;
2719 }
2720
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002721 trace_i915_gem_object_change_domain(obj,
2722 old_read_domains,
2723 old_write_domain);
2724
Eric Anholte47c68e2008-11-14 13:35:19 -08002725 return 0;
2726}
2727
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002728/*
2729 * Prepare buffer for display plane. Use uninterruptible for possible flush
2730 * wait, as in modesetting process we're not supposed to be interrupted.
2731 */
2732int
2733i915_gem_object_set_to_display_plane(struct drm_gem_object *obj)
2734{
2735 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01002736 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002737 uint32_t old_write_domain, old_read_domains;
2738 int ret;
2739
2740 /* Not valid to be called on unbound objects. */
2741 if (obj_priv->gtt_space == NULL)
2742 return -EINVAL;
2743
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002744 ret = i915_gem_object_flush_gpu_write_domain(obj);
2745 if (ret)
2746 return ret;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002747
2748 /* Wait on any GPU rendering and flushing to occur. */
2749 if (obj_priv->active) {
2750#if WATCH_BUF
2751 DRM_INFO("%s: object %p wait for seqno %08x\n",
2752 __func__, obj, obj_priv->last_rendering_seqno);
2753#endif
Zou Nan hai852835f2010-05-21 09:08:56 +08002754 ret = i915_do_wait_request(dev,
2755 obj_priv->last_rendering_seqno,
2756 0,
2757 obj_priv->ring);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002758 if (ret != 0)
2759 return ret;
2760 }
2761
Chris Wilsonb118c1e2010-05-27 13:18:14 +01002762 i915_gem_object_flush_cpu_write_domain(obj);
2763
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002764 old_write_domain = obj->write_domain;
2765 old_read_domains = obj->read_domains;
2766
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002767 /* It should now be out of any other write domains, and we can update
2768 * the domain values for our changes.
2769 */
2770 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
Chris Wilsonb118c1e2010-05-27 13:18:14 +01002771 obj->read_domains = I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002772 obj->write_domain = I915_GEM_DOMAIN_GTT;
2773 obj_priv->dirty = 1;
2774
2775 trace_i915_gem_object_change_domain(obj,
2776 old_read_domains,
2777 old_write_domain);
2778
2779 return 0;
2780}
2781
Eric Anholte47c68e2008-11-14 13:35:19 -08002782/**
2783 * Moves a single object to the CPU read, and possibly write domain.
2784 *
2785 * This function returns when the move is complete, including waiting on
2786 * flushes to occur.
2787 */
2788static int
2789i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj, int write)
2790{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002791 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002792 int ret;
2793
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002794 ret = i915_gem_object_flush_gpu_write_domain(obj);
2795 if (ret)
2796 return ret;
2797
Eric Anholte47c68e2008-11-14 13:35:19 -08002798 /* Wait on any GPU rendering and flushing to occur. */
2799 ret = i915_gem_object_wait_rendering(obj);
2800 if (ret != 0)
2801 return ret;
2802
2803 i915_gem_object_flush_gtt_write_domain(obj);
2804
2805 /* If we have a partially-valid cache of the object in the CPU,
2806 * finish invalidating it and free the per-page flags.
2807 */
2808 i915_gem_object_set_to_full_cpu_read_domain(obj);
2809
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002810 old_write_domain = obj->write_domain;
2811 old_read_domains = obj->read_domains;
2812
Eric Anholte47c68e2008-11-14 13:35:19 -08002813 /* Flush the CPU cache if it's still invalid. */
2814 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
2815 i915_gem_clflush_object(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08002816
2817 obj->read_domains |= I915_GEM_DOMAIN_CPU;
2818 }
2819
2820 /* It should now be out of any other write domains, and we can update
2821 * the domain values for our changes.
2822 */
2823 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
2824
2825 /* If we're writing through the CPU, then the GPU read domains will
2826 * need to be invalidated at next use.
2827 */
2828 if (write) {
2829 obj->read_domains &= I915_GEM_DOMAIN_CPU;
2830 obj->write_domain = I915_GEM_DOMAIN_CPU;
2831 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002832
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002833 trace_i915_gem_object_change_domain(obj,
2834 old_read_domains,
2835 old_write_domain);
2836
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002837 return 0;
2838}
2839
Eric Anholt673a3942008-07-30 12:06:12 -07002840/*
2841 * Set the next domain for the specified object. This
2842 * may not actually perform the necessary flushing/invaliding though,
2843 * as that may want to be batched with other set_domain operations
2844 *
2845 * This is (we hope) the only really tricky part of gem. The goal
2846 * is fairly simple -- track which caches hold bits of the object
2847 * and make sure they remain coherent. A few concrete examples may
2848 * help to explain how it works. For shorthand, we use the notation
2849 * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
2850 * a pair of read and write domain masks.
2851 *
2852 * Case 1: the batch buffer
2853 *
2854 * 1. Allocated
2855 * 2. Written by CPU
2856 * 3. Mapped to GTT
2857 * 4. Read by GPU
2858 * 5. Unmapped from GTT
2859 * 6. Freed
2860 *
2861 * Let's take these a step at a time
2862 *
2863 * 1. Allocated
2864 * Pages allocated from the kernel may still have
2865 * cache contents, so we set them to (CPU, CPU) always.
2866 * 2. Written by CPU (using pwrite)
2867 * The pwrite function calls set_domain (CPU, CPU) and
2868 * this function does nothing (as nothing changes)
2869 * 3. Mapped by GTT
2870 * This function asserts that the object is not
2871 * currently in any GPU-based read or write domains
2872 * 4. Read by GPU
2873 * i915_gem_execbuffer calls set_domain (COMMAND, 0).
2874 * As write_domain is zero, this function adds in the
2875 * current read domains (CPU+COMMAND, 0).
2876 * flush_domains is set to CPU.
2877 * invalidate_domains is set to COMMAND
2878 * clflush is run to get data out of the CPU caches
2879 * then i915_dev_set_domain calls i915_gem_flush to
2880 * emit an MI_FLUSH and drm_agp_chipset_flush
2881 * 5. Unmapped from GTT
2882 * i915_gem_object_unbind calls set_domain (CPU, CPU)
2883 * flush_domains and invalidate_domains end up both zero
2884 * so no flushing/invalidating happens
2885 * 6. Freed
2886 * yay, done
2887 *
2888 * Case 2: The shared render buffer
2889 *
2890 * 1. Allocated
2891 * 2. Mapped to GTT
2892 * 3. Read/written by GPU
2893 * 4. set_domain to (CPU,CPU)
2894 * 5. Read/written by CPU
2895 * 6. Read/written by GPU
2896 *
2897 * 1. Allocated
2898 * Same as last example, (CPU, CPU)
2899 * 2. Mapped to GTT
2900 * Nothing changes (assertions find that it is not in the GPU)
2901 * 3. Read/written by GPU
2902 * execbuffer calls set_domain (RENDER, RENDER)
2903 * flush_domains gets CPU
2904 * invalidate_domains gets GPU
2905 * clflush (obj)
2906 * MI_FLUSH and drm_agp_chipset_flush
2907 * 4. set_domain (CPU, CPU)
2908 * flush_domains gets GPU
2909 * invalidate_domains gets CPU
2910 * wait_rendering (obj) to make sure all drawing is complete.
2911 * This will include an MI_FLUSH to get the data from GPU
2912 * to memory
2913 * clflush (obj) to invalidate the CPU cache
2914 * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
2915 * 5. Read/written by CPU
2916 * cache lines are loaded and dirtied
2917 * 6. Read written by GPU
2918 * Same as last GPU access
2919 *
2920 * Case 3: The constant buffer
2921 *
2922 * 1. Allocated
2923 * 2. Written by CPU
2924 * 3. Read by GPU
2925 * 4. Updated (written) by CPU again
2926 * 5. Read by GPU
2927 *
2928 * 1. Allocated
2929 * (CPU, CPU)
2930 * 2. Written by CPU
2931 * (CPU, CPU)
2932 * 3. Read by GPU
2933 * (CPU+RENDER, 0)
2934 * flush_domains = CPU
2935 * invalidate_domains = RENDER
2936 * clflush (obj)
2937 * MI_FLUSH
2938 * drm_agp_chipset_flush
2939 * 4. Updated (written) by CPU again
2940 * (CPU, CPU)
2941 * flush_domains = 0 (no previous write domain)
2942 * invalidate_domains = 0 (no new read domains)
2943 * 5. Read by GPU
2944 * (CPU+RENDER, 0)
2945 * flush_domains = CPU
2946 * invalidate_domains = RENDER
2947 * clflush (obj)
2948 * MI_FLUSH
2949 * drm_agp_chipset_flush
2950 */
Keith Packardc0d90822008-11-20 23:11:08 -08002951static void
Eric Anholt8b0e3782009-02-19 14:40:50 -08002952i915_gem_object_set_to_gpu_domain(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002953{
2954 struct drm_device *dev = obj->dev;
Chris Wilson88f356b2010-08-04 13:55:32 +01002955 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002956 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002957 uint32_t invalidate_domains = 0;
2958 uint32_t flush_domains = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002959 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002960
Eric Anholt8b0e3782009-02-19 14:40:50 -08002961 BUG_ON(obj->pending_read_domains & I915_GEM_DOMAIN_CPU);
2962 BUG_ON(obj->pending_write_domain == I915_GEM_DOMAIN_CPU);
Eric Anholt673a3942008-07-30 12:06:12 -07002963
Jesse Barnes652c3932009-08-17 13:31:43 -07002964 intel_mark_busy(dev, obj);
2965
Eric Anholt673a3942008-07-30 12:06:12 -07002966#if WATCH_BUF
2967 DRM_INFO("%s: object %p read %08x -> %08x write %08x -> %08x\n",
2968 __func__, obj,
Eric Anholt8b0e3782009-02-19 14:40:50 -08002969 obj->read_domains, obj->pending_read_domains,
2970 obj->write_domain, obj->pending_write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07002971#endif
2972 /*
2973 * If the object isn't moving to a new write domain,
2974 * let the object stay in multiple read domains
2975 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08002976 if (obj->pending_write_domain == 0)
2977 obj->pending_read_domains |= obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07002978 else
2979 obj_priv->dirty = 1;
2980
2981 /*
2982 * Flush the current write domain if
2983 * the new read domains don't match. Invalidate
2984 * any read domains which differ from the old
2985 * write domain
2986 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08002987 if (obj->write_domain &&
2988 obj->write_domain != obj->pending_read_domains) {
Eric Anholt673a3942008-07-30 12:06:12 -07002989 flush_domains |= obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08002990 invalidate_domains |=
2991 obj->pending_read_domains & ~obj->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07002992 }
2993 /*
2994 * Invalidate any read caches which may have
2995 * stale data. That is, any new read domains.
2996 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08002997 invalidate_domains |= obj->pending_read_domains & ~obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07002998 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU) {
2999#if WATCH_BUF
3000 DRM_INFO("%s: CPU domain flush %08x invalidate %08x\n",
3001 __func__, flush_domains, invalidate_domains);
3002#endif
Eric Anholt673a3942008-07-30 12:06:12 -07003003 i915_gem_clflush_object(obj);
3004 }
3005
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003006 old_read_domains = obj->read_domains;
3007
Eric Anholtefbeed92009-02-19 14:54:51 -08003008 /* The actual obj->write_domain will be updated with
3009 * pending_write_domain after we emit the accumulated flush for all
3010 * of our domain changes in execbuffers (which clears objects'
3011 * write_domains). So if we have a current write domain that we
3012 * aren't changing, set pending_write_domain to that.
3013 */
3014 if (flush_domains == 0 && obj->pending_write_domain == 0)
3015 obj->pending_write_domain = obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08003016 obj->read_domains = obj->pending_read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07003017
Chris Wilson88f356b2010-08-04 13:55:32 +01003018 if (flush_domains & I915_GEM_GPU_DOMAINS) {
3019 if (obj_priv->ring == &dev_priv->render_ring)
3020 dev_priv->flush_rings |= FLUSH_RENDER_RING;
3021 else if (obj_priv->ring == &dev_priv->bsd_ring)
3022 dev_priv->flush_rings |= FLUSH_BSD_RING;
3023 }
3024
Eric Anholt673a3942008-07-30 12:06:12 -07003025 dev->invalidate_domains |= invalidate_domains;
3026 dev->flush_domains |= flush_domains;
3027#if WATCH_BUF
3028 DRM_INFO("%s: read %08x write %08x invalidate %08x flush %08x\n",
3029 __func__,
3030 obj->read_domains, obj->write_domain,
3031 dev->invalidate_domains, dev->flush_domains);
3032#endif
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003033
3034 trace_i915_gem_object_change_domain(obj,
3035 old_read_domains,
3036 obj->write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07003037}
3038
3039/**
Eric Anholte47c68e2008-11-14 13:35:19 -08003040 * Moves the object from a partially CPU read to a full one.
Eric Anholt673a3942008-07-30 12:06:12 -07003041 *
Eric Anholte47c68e2008-11-14 13:35:19 -08003042 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
3043 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
3044 */
3045static void
3046i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj)
3047{
Daniel Vetter23010e42010-03-08 13:35:02 +01003048 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003049
3050 if (!obj_priv->page_cpu_valid)
3051 return;
3052
3053 /* If we're partially in the CPU read domain, finish moving it in.
3054 */
3055 if (obj->read_domains & I915_GEM_DOMAIN_CPU) {
3056 int i;
3057
3058 for (i = 0; i <= (obj->size - 1) / PAGE_SIZE; i++) {
3059 if (obj_priv->page_cpu_valid[i])
3060 continue;
Eric Anholt856fa192009-03-19 14:10:50 -07003061 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholte47c68e2008-11-14 13:35:19 -08003062 }
Eric Anholte47c68e2008-11-14 13:35:19 -08003063 }
3064
3065 /* Free the page_cpu_valid mappings which are now stale, whether
3066 * or not we've got I915_GEM_DOMAIN_CPU.
3067 */
Eric Anholt9a298b22009-03-24 12:23:04 -07003068 kfree(obj_priv->page_cpu_valid);
Eric Anholte47c68e2008-11-14 13:35:19 -08003069 obj_priv->page_cpu_valid = NULL;
3070}
3071
3072/**
3073 * Set the CPU read domain on a range of the object.
3074 *
3075 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
3076 * not entirely valid. The page_cpu_valid member of the object flags which
3077 * pages have been flushed, and will be respected by
3078 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
3079 * of the whole object.
3080 *
3081 * This function returns when the move is complete, including waiting on
3082 * flushes to occur.
Eric Anholt673a3942008-07-30 12:06:12 -07003083 */
3084static int
Eric Anholte47c68e2008-11-14 13:35:19 -08003085i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
3086 uint64_t offset, uint64_t size)
Eric Anholt673a3942008-07-30 12:06:12 -07003087{
Daniel Vetter23010e42010-03-08 13:35:02 +01003088 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003089 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003090 int i, ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003091
Eric Anholte47c68e2008-11-14 13:35:19 -08003092 if (offset == 0 && size == obj->size)
3093 return i915_gem_object_set_to_cpu_domain(obj, 0);
3094
Chris Wilson2dafb1e2010-06-07 14:03:05 +01003095 ret = i915_gem_object_flush_gpu_write_domain(obj);
3096 if (ret)
3097 return ret;
3098
Eric Anholte47c68e2008-11-14 13:35:19 -08003099 /* Wait on any GPU rendering and flushing to occur. */
3100 ret = i915_gem_object_wait_rendering(obj);
3101 if (ret != 0)
3102 return ret;
3103 i915_gem_object_flush_gtt_write_domain(obj);
3104
3105 /* If we're already fully in the CPU read domain, we're done. */
3106 if (obj_priv->page_cpu_valid == NULL &&
3107 (obj->read_domains & I915_GEM_DOMAIN_CPU) != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07003108 return 0;
3109
Eric Anholte47c68e2008-11-14 13:35:19 -08003110 /* Otherwise, create/clear the per-page CPU read domain flag if we're
3111 * newly adding I915_GEM_DOMAIN_CPU
3112 */
Eric Anholt673a3942008-07-30 12:06:12 -07003113 if (obj_priv->page_cpu_valid == NULL) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003114 obj_priv->page_cpu_valid = kzalloc(obj->size / PAGE_SIZE,
3115 GFP_KERNEL);
Eric Anholte47c68e2008-11-14 13:35:19 -08003116 if (obj_priv->page_cpu_valid == NULL)
3117 return -ENOMEM;
3118 } else if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0)
3119 memset(obj_priv->page_cpu_valid, 0, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07003120
3121 /* Flush the cache on any pages that are still invalid from the CPU's
3122 * perspective.
3123 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003124 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
3125 i++) {
Eric Anholt673a3942008-07-30 12:06:12 -07003126 if (obj_priv->page_cpu_valid[i])
3127 continue;
3128
Eric Anholt856fa192009-03-19 14:10:50 -07003129 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07003130
3131 obj_priv->page_cpu_valid[i] = 1;
3132 }
3133
Eric Anholte47c68e2008-11-14 13:35:19 -08003134 /* It should now be out of any other write domains, and we can update
3135 * the domain values for our changes.
3136 */
3137 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
3138
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003139 old_read_domains = obj->read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003140 obj->read_domains |= I915_GEM_DOMAIN_CPU;
3141
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003142 trace_i915_gem_object_change_domain(obj,
3143 old_read_domains,
3144 obj->write_domain);
3145
Eric Anholt673a3942008-07-30 12:06:12 -07003146 return 0;
3147}
3148
3149/**
Eric Anholt673a3942008-07-30 12:06:12 -07003150 * Pin an object to the GTT and evaluate the relocations landing in it.
3151 */
3152static int
3153i915_gem_object_pin_and_relocate(struct drm_gem_object *obj,
3154 struct drm_file *file_priv,
Jesse Barnes76446ca2009-12-17 22:05:42 -05003155 struct drm_i915_gem_exec_object2 *entry,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003156 struct drm_i915_gem_relocation_entry *relocs)
Eric Anholt673a3942008-07-30 12:06:12 -07003157{
3158 struct drm_device *dev = obj->dev;
Keith Packard0839ccb2008-10-30 19:38:48 -07003159 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01003160 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003161 int i, ret;
Keith Packard0839ccb2008-10-30 19:38:48 -07003162 void __iomem *reloc_page;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003163 bool need_fence;
3164
3165 need_fence = entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
3166 obj_priv->tiling_mode != I915_TILING_NONE;
3167
3168 /* Check fence reg constraints and rebind if necessary */
Chris Wilson808b24d62010-05-27 13:18:15 +01003169 if (need_fence &&
3170 !i915_gem_object_fence_offset_ok(obj,
3171 obj_priv->tiling_mode)) {
3172 ret = i915_gem_object_unbind(obj);
3173 if (ret)
3174 return ret;
3175 }
Eric Anholt673a3942008-07-30 12:06:12 -07003176
3177 /* Choose the GTT offset for our buffer and put it there. */
3178 ret = i915_gem_object_pin(obj, (uint32_t) entry->alignment);
3179 if (ret)
3180 return ret;
3181
Jesse Barnes76446ca2009-12-17 22:05:42 -05003182 /*
3183 * Pre-965 chips need a fence register set up in order to
3184 * properly handle blits to/from tiled surfaces.
3185 */
3186 if (need_fence) {
3187 ret = i915_gem_object_get_fence_reg(obj);
3188 if (ret != 0) {
Jesse Barnes76446ca2009-12-17 22:05:42 -05003189 i915_gem_object_unpin(obj);
3190 return ret;
3191 }
3192 }
3193
Eric Anholt673a3942008-07-30 12:06:12 -07003194 entry->offset = obj_priv->gtt_offset;
3195
Eric Anholt673a3942008-07-30 12:06:12 -07003196 /* Apply the relocations, using the GTT aperture to avoid cache
3197 * flushing requirements.
3198 */
3199 for (i = 0; i < entry->relocation_count; i++) {
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003200 struct drm_i915_gem_relocation_entry *reloc= &relocs[i];
Eric Anholt673a3942008-07-30 12:06:12 -07003201 struct drm_gem_object *target_obj;
3202 struct drm_i915_gem_object *target_obj_priv;
Eric Anholt3043c602008-10-02 12:24:47 -07003203 uint32_t reloc_val, reloc_offset;
3204 uint32_t __iomem *reloc_entry;
Eric Anholt673a3942008-07-30 12:06:12 -07003205
Eric Anholt673a3942008-07-30 12:06:12 -07003206 target_obj = drm_gem_object_lookup(obj->dev, file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003207 reloc->target_handle);
Eric Anholt673a3942008-07-30 12:06:12 -07003208 if (target_obj == NULL) {
3209 i915_gem_object_unpin(obj);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01003210 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07003211 }
Daniel Vetter23010e42010-03-08 13:35:02 +01003212 target_obj_priv = to_intel_bo(target_obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003213
Chris Wilson8542a0b2009-09-09 21:15:15 +01003214#if WATCH_RELOC
3215 DRM_INFO("%s: obj %p offset %08x target %d "
3216 "read %08x write %08x gtt %08x "
3217 "presumed %08x delta %08x\n",
3218 __func__,
3219 obj,
3220 (int) reloc->offset,
3221 (int) reloc->target_handle,
3222 (int) reloc->read_domains,
3223 (int) reloc->write_domain,
3224 (int) target_obj_priv->gtt_offset,
3225 (int) reloc->presumed_offset,
3226 reloc->delta);
3227#endif
3228
Eric Anholt673a3942008-07-30 12:06:12 -07003229 /* The target buffer should have appeared before us in the
3230 * exec_object list, so it should have a GTT space bound by now.
3231 */
3232 if (target_obj_priv->gtt_space == NULL) {
3233 DRM_ERROR("No GTT space found for object %d\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003234 reloc->target_handle);
Eric Anholt673a3942008-07-30 12:06:12 -07003235 drm_gem_object_unreference(target_obj);
3236 i915_gem_object_unpin(obj);
3237 return -EINVAL;
3238 }
3239
Chris Wilson8542a0b2009-09-09 21:15:15 +01003240 /* Validate that the target is in a valid r/w GPU domain */
Daniel Vetter16edd552010-02-19 11:52:02 +01003241 if (reloc->write_domain & (reloc->write_domain - 1)) {
3242 DRM_ERROR("reloc with multiple write domains: "
3243 "obj %p target %d offset %d "
3244 "read %08x write %08x",
3245 obj, reloc->target_handle,
3246 (int) reloc->offset,
3247 reloc->read_domains,
3248 reloc->write_domain);
3249 return -EINVAL;
3250 }
Chris Wilson8542a0b2009-09-09 21:15:15 +01003251 if (reloc->write_domain & I915_GEM_DOMAIN_CPU ||
3252 reloc->read_domains & I915_GEM_DOMAIN_CPU) {
3253 DRM_ERROR("reloc with read/write CPU domains: "
3254 "obj %p target %d offset %d "
3255 "read %08x write %08x",
3256 obj, reloc->target_handle,
3257 (int) reloc->offset,
3258 reloc->read_domains,
3259 reloc->write_domain);
3260 drm_gem_object_unreference(target_obj);
3261 i915_gem_object_unpin(obj);
3262 return -EINVAL;
3263 }
3264 if (reloc->write_domain && target_obj->pending_write_domain &&
3265 reloc->write_domain != target_obj->pending_write_domain) {
3266 DRM_ERROR("Write domain conflict: "
3267 "obj %p target %d offset %d "
3268 "new %08x old %08x\n",
3269 obj, reloc->target_handle,
3270 (int) reloc->offset,
3271 reloc->write_domain,
3272 target_obj->pending_write_domain);
3273 drm_gem_object_unreference(target_obj);
3274 i915_gem_object_unpin(obj);
3275 return -EINVAL;
3276 }
3277
3278 target_obj->pending_read_domains |= reloc->read_domains;
3279 target_obj->pending_write_domain |= reloc->write_domain;
3280
3281 /* If the relocation already has the right value in it, no
3282 * more work needs to be done.
3283 */
3284 if (target_obj_priv->gtt_offset == reloc->presumed_offset) {
3285 drm_gem_object_unreference(target_obj);
3286 continue;
3287 }
3288
3289 /* Check that the relocation address is valid... */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003290 if (reloc->offset > obj->size - 4) {
Eric Anholt673a3942008-07-30 12:06:12 -07003291 DRM_ERROR("Relocation beyond object bounds: "
3292 "obj %p target %d offset %d size %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003293 obj, reloc->target_handle,
3294 (int) reloc->offset, (int) obj->size);
Eric Anholt673a3942008-07-30 12:06:12 -07003295 drm_gem_object_unreference(target_obj);
3296 i915_gem_object_unpin(obj);
3297 return -EINVAL;
3298 }
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003299 if (reloc->offset & 3) {
Eric Anholt673a3942008-07-30 12:06:12 -07003300 DRM_ERROR("Relocation not 4-byte aligned: "
3301 "obj %p target %d offset %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003302 obj, reloc->target_handle,
3303 (int) reloc->offset);
Eric Anholt673a3942008-07-30 12:06:12 -07003304 drm_gem_object_unreference(target_obj);
3305 i915_gem_object_unpin(obj);
3306 return -EINVAL;
3307 }
3308
Chris Wilson8542a0b2009-09-09 21:15:15 +01003309 /* and points to somewhere within the target object. */
Chris Wilsoncd0b9fb2009-09-15 23:23:18 +01003310 if (reloc->delta >= target_obj->size) {
3311 DRM_ERROR("Relocation beyond target object bounds: "
3312 "obj %p target %d delta %d size %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003313 obj, reloc->target_handle,
Chris Wilsoncd0b9fb2009-09-15 23:23:18 +01003314 (int) reloc->delta, (int) target_obj->size);
Chris Wilson491152b2009-02-11 14:26:32 +00003315 drm_gem_object_unreference(target_obj);
3316 i915_gem_object_unpin(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003317 return -EINVAL;
3318 }
3319
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003320 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
3321 if (ret != 0) {
3322 drm_gem_object_unreference(target_obj);
3323 i915_gem_object_unpin(obj);
3324 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -07003325 }
3326
3327 /* Map the page containing the relocation we're going to
3328 * perform.
3329 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003330 reloc_offset = obj_priv->gtt_offset + reloc->offset;
Keith Packard0839ccb2008-10-30 19:38:48 -07003331 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
3332 (reloc_offset &
Chris Wilsonfca3ec02010-08-04 14:34:24 +01003333 ~(PAGE_SIZE - 1)),
3334 KM_USER0);
Eric Anholt3043c602008-10-02 12:24:47 -07003335 reloc_entry = (uint32_t __iomem *)(reloc_page +
Keith Packard0839ccb2008-10-30 19:38:48 -07003336 (reloc_offset & (PAGE_SIZE - 1)));
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003337 reloc_val = target_obj_priv->gtt_offset + reloc->delta;
Eric Anholt673a3942008-07-30 12:06:12 -07003338
3339#if WATCH_BUF
3340 DRM_INFO("Applied relocation: %p@0x%08x %08x -> %08x\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003341 obj, (unsigned int) reloc->offset,
Eric Anholt673a3942008-07-30 12:06:12 -07003342 readl(reloc_entry), reloc_val);
3343#endif
3344 writel(reloc_val, reloc_entry);
Chris Wilsonfca3ec02010-08-04 14:34:24 +01003345 io_mapping_unmap_atomic(reloc_page, KM_USER0);
Eric Anholt673a3942008-07-30 12:06:12 -07003346
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003347 /* The updated presumed offset for this entry will be
3348 * copied back out to the user.
Eric Anholt673a3942008-07-30 12:06:12 -07003349 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003350 reloc->presumed_offset = target_obj_priv->gtt_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003351
3352 drm_gem_object_unreference(target_obj);
3353 }
3354
Eric Anholt673a3942008-07-30 12:06:12 -07003355#if WATCH_BUF
3356 if (0)
3357 i915_gem_dump_object(obj, 128, __func__, ~0);
3358#endif
3359 return 0;
3360}
3361
Eric Anholt673a3942008-07-30 12:06:12 -07003362/* Throttle our rendering by waiting until the ring has completed our requests
3363 * emitted over 20 msec ago.
3364 *
Eric Anholtb9624422009-06-03 07:27:35 +00003365 * Note that if we were to use the current jiffies each time around the loop,
3366 * we wouldn't escape the function with any frames outstanding if the time to
3367 * render a frame was over 20ms.
3368 *
Eric Anholt673a3942008-07-30 12:06:12 -07003369 * This should get us reasonable parallelism between CPU and GPU but also
3370 * relatively low latency when blocking on a particular request to finish.
3371 */
3372static int
3373i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file_priv)
3374{
3375 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
3376 int ret = 0;
Eric Anholtb9624422009-06-03 07:27:35 +00003377 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
Eric Anholt673a3942008-07-30 12:06:12 -07003378
3379 mutex_lock(&dev->struct_mutex);
Eric Anholtb9624422009-06-03 07:27:35 +00003380 while (!list_empty(&i915_file_priv->mm.request_list)) {
3381 struct drm_i915_gem_request *request;
3382
3383 request = list_first_entry(&i915_file_priv->mm.request_list,
3384 struct drm_i915_gem_request,
3385 client_list);
3386
3387 if (time_after_eq(request->emitted_jiffies, recent_enough))
3388 break;
3389
Zou Nan hai852835f2010-05-21 09:08:56 +08003390 ret = i915_wait_request(dev, request->seqno, request->ring);
Eric Anholtb9624422009-06-03 07:27:35 +00003391 if (ret != 0)
3392 break;
3393 }
Eric Anholt673a3942008-07-30 12:06:12 -07003394 mutex_unlock(&dev->struct_mutex);
Eric Anholtb9624422009-06-03 07:27:35 +00003395
Eric Anholt673a3942008-07-30 12:06:12 -07003396 return ret;
3397}
3398
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003399static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003400i915_gem_get_relocs_from_user(struct drm_i915_gem_exec_object2 *exec_list,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003401 uint32_t buffer_count,
3402 struct drm_i915_gem_relocation_entry **relocs)
3403{
3404 uint32_t reloc_count = 0, reloc_index = 0, i;
3405 int ret;
3406
3407 *relocs = NULL;
3408 for (i = 0; i < buffer_count; i++) {
3409 if (reloc_count + exec_list[i].relocation_count < reloc_count)
3410 return -EINVAL;
3411 reloc_count += exec_list[i].relocation_count;
3412 }
3413
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003414 *relocs = drm_calloc_large(reloc_count, sizeof(**relocs));
Jesse Barnes76446ca2009-12-17 22:05:42 -05003415 if (*relocs == NULL) {
3416 DRM_ERROR("failed to alloc relocs, count %d\n", reloc_count);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003417 return -ENOMEM;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003418 }
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003419
3420 for (i = 0; i < buffer_count; i++) {
3421 struct drm_i915_gem_relocation_entry __user *user_relocs;
3422
3423 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3424
3425 ret = copy_from_user(&(*relocs)[reloc_index],
3426 user_relocs,
3427 exec_list[i].relocation_count *
3428 sizeof(**relocs));
3429 if (ret != 0) {
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003430 drm_free_large(*relocs);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003431 *relocs = NULL;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003432 return -EFAULT;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003433 }
3434
3435 reloc_index += exec_list[i].relocation_count;
3436 }
3437
Florian Mickler2bc43b52009-04-06 22:55:41 +02003438 return 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003439}
3440
3441static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003442i915_gem_put_relocs_to_user(struct drm_i915_gem_exec_object2 *exec_list,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003443 uint32_t buffer_count,
3444 struct drm_i915_gem_relocation_entry *relocs)
3445{
3446 uint32_t reloc_count = 0, i;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003447 int ret = 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003448
Chris Wilson93533c22010-01-31 10:40:48 +00003449 if (relocs == NULL)
3450 return 0;
3451
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003452 for (i = 0; i < buffer_count; i++) {
3453 struct drm_i915_gem_relocation_entry __user *user_relocs;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003454 int unwritten;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003455
3456 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3457
Florian Mickler2bc43b52009-04-06 22:55:41 +02003458 unwritten = copy_to_user(user_relocs,
3459 &relocs[reloc_count],
3460 exec_list[i].relocation_count *
3461 sizeof(*relocs));
3462
3463 if (unwritten) {
3464 ret = -EFAULT;
3465 goto err;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003466 }
3467
3468 reloc_count += exec_list[i].relocation_count;
3469 }
3470
Florian Mickler2bc43b52009-04-06 22:55:41 +02003471err:
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003472 drm_free_large(relocs);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003473
3474 return ret;
3475}
3476
Chris Wilson83d60792009-06-06 09:45:57 +01003477static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003478i915_gem_check_execbuffer (struct drm_i915_gem_execbuffer2 *exec,
Chris Wilson83d60792009-06-06 09:45:57 +01003479 uint64_t exec_offset)
3480{
3481 uint32_t exec_start, exec_len;
3482
3483 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
3484 exec_len = (uint32_t) exec->batch_len;
3485
3486 if ((exec_start | exec_len) & 0x7)
3487 return -EINVAL;
3488
3489 if (!exec_start)
3490 return -EINVAL;
3491
3492 return 0;
3493}
3494
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003495static int
3496i915_gem_wait_for_pending_flip(struct drm_device *dev,
3497 struct drm_gem_object **object_list,
3498 int count)
3499{
3500 drm_i915_private_t *dev_priv = dev->dev_private;
3501 struct drm_i915_gem_object *obj_priv;
3502 DEFINE_WAIT(wait);
3503 int i, ret = 0;
3504
3505 for (;;) {
3506 prepare_to_wait(&dev_priv->pending_flip_queue,
3507 &wait, TASK_INTERRUPTIBLE);
3508 for (i = 0; i < count; i++) {
Daniel Vetter23010e42010-03-08 13:35:02 +01003509 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003510 if (atomic_read(&obj_priv->pending_flip) > 0)
3511 break;
3512 }
3513 if (i == count)
3514 break;
3515
3516 if (!signal_pending(current)) {
3517 mutex_unlock(&dev->struct_mutex);
3518 schedule();
3519 mutex_lock(&dev->struct_mutex);
3520 continue;
3521 }
3522 ret = -ERESTARTSYS;
3523 break;
3524 }
3525 finish_wait(&dev_priv->pending_flip_queue, &wait);
3526
3527 return ret;
3528}
3529
Chris Wilson43b27f42010-07-02 08:57:15 +01003530
Eric Anholt673a3942008-07-30 12:06:12 -07003531int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003532i915_gem_do_execbuffer(struct drm_device *dev, void *data,
3533 struct drm_file *file_priv,
3534 struct drm_i915_gem_execbuffer2 *args,
3535 struct drm_i915_gem_exec_object2 *exec_list)
Eric Anholt673a3942008-07-30 12:06:12 -07003536{
3537 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003538 struct drm_gem_object **object_list = NULL;
3539 struct drm_gem_object *batch_obj;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003540 struct drm_i915_gem_object *obj_priv;
Eric Anholt201361a2009-03-11 12:30:04 -07003541 struct drm_clip_rect *cliprects = NULL;
Chris Wilson93533c22010-01-31 10:40:48 +00003542 struct drm_i915_gem_relocation_entry *relocs = NULL;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003543 int ret = 0, ret2, i, pinned = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003544 uint64_t exec_offset;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003545 uint32_t seqno, flush_domains, reloc_index;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003546 int pin_tries, flips;
Eric Anholt673a3942008-07-30 12:06:12 -07003547
Zou Nan hai852835f2010-05-21 09:08:56 +08003548 struct intel_ring_buffer *ring = NULL;
3549
Eric Anholt673a3942008-07-30 12:06:12 -07003550#if WATCH_EXEC
3551 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3552 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3553#endif
Zou Nan haid1b851f2010-05-21 09:08:57 +08003554 if (args->flags & I915_EXEC_BSD) {
3555 if (!HAS_BSD(dev)) {
3556 DRM_ERROR("execbuf with wrong flag\n");
3557 return -EINVAL;
3558 }
3559 ring = &dev_priv->bsd_ring;
3560 } else {
3561 ring = &dev_priv->render_ring;
3562 }
3563
Eric Anholt4f481ed2008-09-10 14:22:49 -07003564 if (args->buffer_count < 1) {
3565 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3566 return -EINVAL;
3567 }
Eric Anholtc8e0f932009-11-22 03:49:37 +01003568 object_list = drm_malloc_ab(sizeof(*object_list), args->buffer_count);
Jesse Barnes76446ca2009-12-17 22:05:42 -05003569 if (object_list == NULL) {
3570 DRM_ERROR("Failed to allocate object list for %d buffers\n",
Eric Anholt673a3942008-07-30 12:06:12 -07003571 args->buffer_count);
3572 ret = -ENOMEM;
3573 goto pre_mutex_err;
3574 }
Eric Anholt673a3942008-07-30 12:06:12 -07003575
Eric Anholt201361a2009-03-11 12:30:04 -07003576 if (args->num_cliprects != 0) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003577 cliprects = kcalloc(args->num_cliprects, sizeof(*cliprects),
3578 GFP_KERNEL);
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003579 if (cliprects == NULL) {
3580 ret = -ENOMEM;
Eric Anholt201361a2009-03-11 12:30:04 -07003581 goto pre_mutex_err;
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003582 }
Eric Anholt201361a2009-03-11 12:30:04 -07003583
3584 ret = copy_from_user(cliprects,
3585 (struct drm_clip_rect __user *)
3586 (uintptr_t) args->cliprects_ptr,
3587 sizeof(*cliprects) * args->num_cliprects);
3588 if (ret != 0) {
3589 DRM_ERROR("copy %d cliprects failed: %d\n",
3590 args->num_cliprects, ret);
Dan Carpenterc877cdc2010-06-23 19:03:01 +02003591 ret = -EFAULT;
Eric Anholt201361a2009-03-11 12:30:04 -07003592 goto pre_mutex_err;
3593 }
3594 }
3595
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003596 ret = i915_gem_get_relocs_from_user(exec_list, args->buffer_count,
3597 &relocs);
3598 if (ret != 0)
3599 goto pre_mutex_err;
3600
Eric Anholt673a3942008-07-30 12:06:12 -07003601 mutex_lock(&dev->struct_mutex);
3602
3603 i915_verify_inactive(dev, __FILE__, __LINE__);
3604
Ben Gamariba1234d2009-09-14 17:48:47 -04003605 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07003606 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003607 ret = -EIO;
3608 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003609 }
3610
3611 if (dev_priv->mm.suspended) {
Eric Anholt673a3942008-07-30 12:06:12 -07003612 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003613 ret = -EBUSY;
3614 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003615 }
3616
Keith Packardac94a962008-11-20 23:30:27 -08003617 /* Look up object handles */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003618 flips = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003619 for (i = 0; i < args->buffer_count; i++) {
3620 object_list[i] = drm_gem_object_lookup(dev, file_priv,
3621 exec_list[i].handle);
3622 if (object_list[i] == NULL) {
3623 DRM_ERROR("Invalid object handle %d at index %d\n",
3624 exec_list[i].handle, i);
Chris Wilson0ce907f2010-01-23 20:26:35 +00003625 /* prevent error path from reading uninitialized data */
3626 args->buffer_count = i + 1;
Chris Wilsonbf79cb92010-08-04 14:19:46 +01003627 ret = -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07003628 goto err;
3629 }
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003630
Daniel Vetter23010e42010-03-08 13:35:02 +01003631 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003632 if (obj_priv->in_execbuffer) {
3633 DRM_ERROR("Object %p appears more than once in object list\n",
3634 object_list[i]);
Chris Wilson0ce907f2010-01-23 20:26:35 +00003635 /* prevent error path from reading uninitialized data */
3636 args->buffer_count = i + 1;
Chris Wilsonbf79cb92010-08-04 14:19:46 +01003637 ret = -EINVAL;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003638 goto err;
3639 }
3640 obj_priv->in_execbuffer = true;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003641 flips += atomic_read(&obj_priv->pending_flip);
3642 }
3643
3644 if (flips > 0) {
3645 ret = i915_gem_wait_for_pending_flip(dev, object_list,
3646 args->buffer_count);
3647 if (ret)
3648 goto err;
Keith Packardac94a962008-11-20 23:30:27 -08003649 }
Eric Anholt673a3942008-07-30 12:06:12 -07003650
Keith Packardac94a962008-11-20 23:30:27 -08003651 /* Pin and relocate */
3652 for (pin_tries = 0; ; pin_tries++) {
3653 ret = 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003654 reloc_index = 0;
3655
Keith Packardac94a962008-11-20 23:30:27 -08003656 for (i = 0; i < args->buffer_count; i++) {
3657 object_list[i]->pending_read_domains = 0;
3658 object_list[i]->pending_write_domain = 0;
3659 ret = i915_gem_object_pin_and_relocate(object_list[i],
3660 file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003661 &exec_list[i],
3662 &relocs[reloc_index]);
Keith Packardac94a962008-11-20 23:30:27 -08003663 if (ret)
3664 break;
3665 pinned = i + 1;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003666 reloc_index += exec_list[i].relocation_count;
Keith Packardac94a962008-11-20 23:30:27 -08003667 }
3668 /* success */
3669 if (ret == 0)
3670 break;
3671
3672 /* error other than GTT full, or we've already tried again */
Chris Wilson2939e1f2009-06-06 09:46:03 +01003673 if (ret != -ENOSPC || pin_tries >= 1) {
Chris Wilson07f73f62009-09-14 16:50:30 +01003674 if (ret != -ERESTARTSYS) {
3675 unsigned long long total_size = 0;
Chris Wilson3d1cc472010-05-27 13:18:19 +01003676 int num_fences = 0;
3677 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson43b27f42010-07-02 08:57:15 +01003678 obj_priv = to_intel_bo(object_list[i]);
Chris Wilson3d1cc472010-05-27 13:18:19 +01003679
Chris Wilson07f73f62009-09-14 16:50:30 +01003680 total_size += object_list[i]->size;
Chris Wilson3d1cc472010-05-27 13:18:19 +01003681 num_fences +=
3682 exec_list[i].flags & EXEC_OBJECT_NEEDS_FENCE &&
3683 obj_priv->tiling_mode != I915_TILING_NONE;
3684 }
3685 DRM_ERROR("Failed to pin buffer %d of %d, total %llu bytes, %d fences: %d\n",
Chris Wilson07f73f62009-09-14 16:50:30 +01003686 pinned+1, args->buffer_count,
Chris Wilson3d1cc472010-05-27 13:18:19 +01003687 total_size, num_fences,
3688 ret);
Chris Wilson07f73f62009-09-14 16:50:30 +01003689 DRM_ERROR("%d objects [%d pinned], "
3690 "%d object bytes [%d pinned], "
3691 "%d/%d gtt bytes\n",
3692 atomic_read(&dev->object_count),
3693 atomic_read(&dev->pin_count),
3694 atomic_read(&dev->object_memory),
3695 atomic_read(&dev->pin_memory),
3696 atomic_read(&dev->gtt_memory),
3697 dev->gtt_total);
3698 }
Eric Anholt673a3942008-07-30 12:06:12 -07003699 goto err;
3700 }
Keith Packardac94a962008-11-20 23:30:27 -08003701
3702 /* unpin all of our buffers */
3703 for (i = 0; i < pinned; i++)
3704 i915_gem_object_unpin(object_list[i]);
Eric Anholtb1177632008-12-10 10:09:41 -08003705 pinned = 0;
Keith Packardac94a962008-11-20 23:30:27 -08003706
3707 /* evict everyone we can from the aperture */
3708 ret = i915_gem_evict_everything(dev);
Chris Wilson07f73f62009-09-14 16:50:30 +01003709 if (ret && ret != -ENOSPC)
Keith Packardac94a962008-11-20 23:30:27 -08003710 goto err;
Eric Anholt673a3942008-07-30 12:06:12 -07003711 }
3712
3713 /* Set the pending read domains for the batch buffer to COMMAND */
3714 batch_obj = object_list[args->buffer_count-1];
Chris Wilson5f26a2c2009-06-06 09:45:58 +01003715 if (batch_obj->pending_write_domain) {
3716 DRM_ERROR("Attempting to use self-modifying batch buffer\n");
3717 ret = -EINVAL;
3718 goto err;
3719 }
3720 batch_obj->pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
Eric Anholt673a3942008-07-30 12:06:12 -07003721
Chris Wilson83d60792009-06-06 09:45:57 +01003722 /* Sanity check the batch buffer, prior to moving objects */
3723 exec_offset = exec_list[args->buffer_count - 1].offset;
3724 ret = i915_gem_check_execbuffer (args, exec_offset);
3725 if (ret != 0) {
3726 DRM_ERROR("execbuf with invalid offset/length\n");
3727 goto err;
3728 }
3729
Eric Anholt673a3942008-07-30 12:06:12 -07003730 i915_verify_inactive(dev, __FILE__, __LINE__);
3731
Keith Packard646f0f62008-11-20 23:23:03 -08003732 /* Zero the global flush/invalidate flags. These
3733 * will be modified as new domains are computed
3734 * for each object
3735 */
3736 dev->invalidate_domains = 0;
3737 dev->flush_domains = 0;
Chris Wilson88f356b2010-08-04 13:55:32 +01003738 dev_priv->flush_rings = 0;
Keith Packard646f0f62008-11-20 23:23:03 -08003739
Eric Anholt673a3942008-07-30 12:06:12 -07003740 for (i = 0; i < args->buffer_count; i++) {
3741 struct drm_gem_object *obj = object_list[i];
Eric Anholt673a3942008-07-30 12:06:12 -07003742
Keith Packard646f0f62008-11-20 23:23:03 -08003743 /* Compute new gpu domains and update invalidate/flush */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003744 i915_gem_object_set_to_gpu_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003745 }
3746
3747 i915_verify_inactive(dev, __FILE__, __LINE__);
3748
Keith Packard646f0f62008-11-20 23:23:03 -08003749 if (dev->invalidate_domains | dev->flush_domains) {
3750#if WATCH_EXEC
3751 DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
3752 __func__,
3753 dev->invalidate_domains,
3754 dev->flush_domains);
3755#endif
3756 i915_gem_flush(dev,
3757 dev->invalidate_domains,
3758 dev->flush_domains);
Chris Wilson88f356b2010-08-04 13:55:32 +01003759 if (dev_priv->flush_rings & FLUSH_RENDER_RING)
Eric Anholtb9624422009-06-03 07:27:35 +00003760 (void)i915_add_request(dev, file_priv,
Chris Wilson88f356b2010-08-04 13:55:32 +01003761 dev->flush_domains,
3762 &dev_priv->render_ring);
3763 if (dev_priv->flush_rings & FLUSH_BSD_RING)
3764 (void)i915_add_request(dev, file_priv,
3765 dev->flush_domains,
3766 &dev_priv->bsd_ring);
Keith Packard646f0f62008-11-20 23:23:03 -08003767 }
Eric Anholt673a3942008-07-30 12:06:12 -07003768
Eric Anholtefbeed92009-02-19 14:54:51 -08003769 for (i = 0; i < args->buffer_count; i++) {
3770 struct drm_gem_object *obj = object_list[i];
Daniel Vetter23010e42010-03-08 13:35:02 +01003771 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003772 uint32_t old_write_domain = obj->write_domain;
Eric Anholtefbeed92009-02-19 14:54:51 -08003773
3774 obj->write_domain = obj->pending_write_domain;
Daniel Vetter99fcb762010-02-07 16:20:18 +01003775 if (obj->write_domain)
3776 list_move_tail(&obj_priv->gpu_write_list,
3777 &dev_priv->mm.gpu_write_list);
3778 else
3779 list_del_init(&obj_priv->gpu_write_list);
3780
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003781 trace_i915_gem_object_change_domain(obj,
3782 obj->read_domains,
3783 old_write_domain);
Eric Anholtefbeed92009-02-19 14:54:51 -08003784 }
3785
Eric Anholt673a3942008-07-30 12:06:12 -07003786 i915_verify_inactive(dev, __FILE__, __LINE__);
3787
3788#if WATCH_COHERENCY
3789 for (i = 0; i < args->buffer_count; i++) {
3790 i915_gem_object_check_coherency(object_list[i],
3791 exec_list[i].handle);
3792 }
3793#endif
3794
Eric Anholt673a3942008-07-30 12:06:12 -07003795#if WATCH_EXEC
Ben Gamari6911a9b2009-04-02 11:24:54 -07003796 i915_gem_dump_object(batch_obj,
Eric Anholt673a3942008-07-30 12:06:12 -07003797 args->batch_len,
3798 __func__,
3799 ~0);
3800#endif
3801
Eric Anholt673a3942008-07-30 12:06:12 -07003802 /* Exec the batchbuffer */
Zou Nan hai852835f2010-05-21 09:08:56 +08003803 ret = ring->dispatch_gem_execbuffer(dev, ring, args,
3804 cliprects, exec_offset);
Eric Anholt673a3942008-07-30 12:06:12 -07003805 if (ret) {
3806 DRM_ERROR("dispatch failed %d\n", ret);
3807 goto err;
3808 }
3809
3810 /*
3811 * Ensure that the commands in the batch buffer are
3812 * finished before the interrupt fires
3813 */
Zou Nan hai852835f2010-05-21 09:08:56 +08003814 flush_domains = i915_retire_commands(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07003815
3816 i915_verify_inactive(dev, __FILE__, __LINE__);
3817
3818 /*
3819 * Get a seqno representing the execution of the current buffer,
3820 * which we can wait on. We would like to mitigate these interrupts,
3821 * likely by only creating seqnos occasionally (so that we have
3822 * *some* interrupts representing completion of buffers that we can
3823 * wait on when trying to clear up gtt space).
3824 */
Zou Nan hai852835f2010-05-21 09:08:56 +08003825 seqno = i915_add_request(dev, file_priv, flush_domains, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07003826 BUG_ON(seqno == 0);
Eric Anholt673a3942008-07-30 12:06:12 -07003827 for (i = 0; i < args->buffer_count; i++) {
3828 struct drm_gem_object *obj = object_list[i];
Zou Nan hai852835f2010-05-21 09:08:56 +08003829 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003830
Zou Nan hai852835f2010-05-21 09:08:56 +08003831 i915_gem_object_move_to_active(obj, seqno, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07003832#if WATCH_LRU
3833 DRM_INFO("%s: move to exec list %p\n", __func__, obj);
3834#endif
3835 }
3836#if WATCH_LRU
3837 i915_dump_lru(dev, __func__);
3838#endif
3839
3840 i915_verify_inactive(dev, __FILE__, __LINE__);
3841
Eric Anholt673a3942008-07-30 12:06:12 -07003842err:
Julia Lawallaad87df2008-12-21 16:28:47 +01003843 for (i = 0; i < pinned; i++)
3844 i915_gem_object_unpin(object_list[i]);
Eric Anholt673a3942008-07-30 12:06:12 -07003845
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003846 for (i = 0; i < args->buffer_count; i++) {
3847 if (object_list[i]) {
Daniel Vetter23010e42010-03-08 13:35:02 +01003848 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003849 obj_priv->in_execbuffer = false;
3850 }
Julia Lawallaad87df2008-12-21 16:28:47 +01003851 drm_gem_object_unreference(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003852 }
Julia Lawallaad87df2008-12-21 16:28:47 +01003853
Eric Anholt673a3942008-07-30 12:06:12 -07003854 mutex_unlock(&dev->struct_mutex);
3855
Chris Wilson93533c22010-01-31 10:40:48 +00003856pre_mutex_err:
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003857 /* Copy the updated relocations out regardless of current error
3858 * state. Failure to update the relocs would mean that the next
3859 * time userland calls execbuf, it would do so with presumed offset
3860 * state that didn't match the actual object state.
3861 */
3862 ret2 = i915_gem_put_relocs_to_user(exec_list, args->buffer_count,
3863 relocs);
3864 if (ret2 != 0) {
3865 DRM_ERROR("Failed to copy relocations back out: %d\n", ret2);
3866
3867 if (ret == 0)
3868 ret = ret2;
3869 }
3870
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003871 drm_free_large(object_list);
Eric Anholt9a298b22009-03-24 12:23:04 -07003872 kfree(cliprects);
Eric Anholt673a3942008-07-30 12:06:12 -07003873
3874 return ret;
3875}
3876
Jesse Barnes76446ca2009-12-17 22:05:42 -05003877/*
3878 * Legacy execbuffer just creates an exec2 list from the original exec object
3879 * list array and passes it to the real function.
3880 */
3881int
3882i915_gem_execbuffer(struct drm_device *dev, void *data,
3883 struct drm_file *file_priv)
3884{
3885 struct drm_i915_gem_execbuffer *args = data;
3886 struct drm_i915_gem_execbuffer2 exec2;
3887 struct drm_i915_gem_exec_object *exec_list = NULL;
3888 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
3889 int ret, i;
3890
3891#if WATCH_EXEC
3892 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3893 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3894#endif
3895
3896 if (args->buffer_count < 1) {
3897 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3898 return -EINVAL;
3899 }
3900
3901 /* Copy in the exec list from userland */
3902 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
3903 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
3904 if (exec_list == NULL || exec2_list == NULL) {
3905 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
3906 args->buffer_count);
3907 drm_free_large(exec_list);
3908 drm_free_large(exec2_list);
3909 return -ENOMEM;
3910 }
3911 ret = copy_from_user(exec_list,
3912 (struct drm_i915_relocation_entry __user *)
3913 (uintptr_t) args->buffers_ptr,
3914 sizeof(*exec_list) * args->buffer_count);
3915 if (ret != 0) {
3916 DRM_ERROR("copy %d exec entries failed %d\n",
3917 args->buffer_count, ret);
3918 drm_free_large(exec_list);
3919 drm_free_large(exec2_list);
3920 return -EFAULT;
3921 }
3922
3923 for (i = 0; i < args->buffer_count; i++) {
3924 exec2_list[i].handle = exec_list[i].handle;
3925 exec2_list[i].relocation_count = exec_list[i].relocation_count;
3926 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
3927 exec2_list[i].alignment = exec_list[i].alignment;
3928 exec2_list[i].offset = exec_list[i].offset;
3929 if (!IS_I965G(dev))
3930 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
3931 else
3932 exec2_list[i].flags = 0;
3933 }
3934
3935 exec2.buffers_ptr = args->buffers_ptr;
3936 exec2.buffer_count = args->buffer_count;
3937 exec2.batch_start_offset = args->batch_start_offset;
3938 exec2.batch_len = args->batch_len;
3939 exec2.DR1 = args->DR1;
3940 exec2.DR4 = args->DR4;
3941 exec2.num_cliprects = args->num_cliprects;
3942 exec2.cliprects_ptr = args->cliprects_ptr;
Zou Nan hai852835f2010-05-21 09:08:56 +08003943 exec2.flags = I915_EXEC_RENDER;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003944
3945 ret = i915_gem_do_execbuffer(dev, data, file_priv, &exec2, exec2_list);
3946 if (!ret) {
3947 /* Copy the new buffer offsets back to the user's exec list. */
3948 for (i = 0; i < args->buffer_count; i++)
3949 exec_list[i].offset = exec2_list[i].offset;
3950 /* ... and back out to userspace */
3951 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
3952 (uintptr_t) args->buffers_ptr,
3953 exec_list,
3954 sizeof(*exec_list) * args->buffer_count);
3955 if (ret) {
3956 ret = -EFAULT;
3957 DRM_ERROR("failed to copy %d exec entries "
3958 "back to user (%d)\n",
3959 args->buffer_count, ret);
3960 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05003961 }
3962
3963 drm_free_large(exec_list);
3964 drm_free_large(exec2_list);
3965 return ret;
3966}
3967
3968int
3969i915_gem_execbuffer2(struct drm_device *dev, void *data,
3970 struct drm_file *file_priv)
3971{
3972 struct drm_i915_gem_execbuffer2 *args = data;
3973 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
3974 int ret;
3975
3976#if WATCH_EXEC
3977 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3978 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3979#endif
3980
3981 if (args->buffer_count < 1) {
3982 DRM_ERROR("execbuf2 with %d buffers\n", args->buffer_count);
3983 return -EINVAL;
3984 }
3985
3986 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
3987 if (exec2_list == NULL) {
3988 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
3989 args->buffer_count);
3990 return -ENOMEM;
3991 }
3992 ret = copy_from_user(exec2_list,
3993 (struct drm_i915_relocation_entry __user *)
3994 (uintptr_t) args->buffers_ptr,
3995 sizeof(*exec2_list) * args->buffer_count);
3996 if (ret != 0) {
3997 DRM_ERROR("copy %d exec entries failed %d\n",
3998 args->buffer_count, ret);
3999 drm_free_large(exec2_list);
4000 return -EFAULT;
4001 }
4002
4003 ret = i915_gem_do_execbuffer(dev, data, file_priv, args, exec2_list);
4004 if (!ret) {
4005 /* Copy the new buffer offsets back to the user's exec list. */
4006 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
4007 (uintptr_t) args->buffers_ptr,
4008 exec2_list,
4009 sizeof(*exec2_list) * args->buffer_count);
4010 if (ret) {
4011 ret = -EFAULT;
4012 DRM_ERROR("failed to copy %d exec entries "
4013 "back to user (%d)\n",
4014 args->buffer_count, ret);
4015 }
4016 }
4017
4018 drm_free_large(exec2_list);
4019 return ret;
4020}
4021
Eric Anholt673a3942008-07-30 12:06:12 -07004022int
4023i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment)
4024{
4025 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01004026 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004027 int ret;
4028
Daniel Vetter778c3542010-05-13 11:49:44 +02004029 BUG_ON(obj_priv->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT);
4030
Eric Anholt673a3942008-07-30 12:06:12 -07004031 i915_verify_inactive(dev, __FILE__, __LINE__);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01004032
4033 if (obj_priv->gtt_space != NULL) {
4034 if (alignment == 0)
4035 alignment = i915_gem_get_gtt_alignment(obj);
4036 if (obj_priv->gtt_offset & (alignment - 1)) {
Chris Wilsonae7d49d2010-08-04 12:37:41 +01004037 WARN(obj_priv->pin_count,
4038 "bo is already pinned with incorrect alignment:"
4039 " offset=%x, req.alignment=%x\n",
4040 obj_priv->gtt_offset, alignment);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01004041 ret = i915_gem_object_unbind(obj);
4042 if (ret)
4043 return ret;
4044 }
4045 }
4046
Eric Anholt673a3942008-07-30 12:06:12 -07004047 if (obj_priv->gtt_space == NULL) {
4048 ret = i915_gem_object_bind_to_gtt(obj, alignment);
Chris Wilson97311292009-09-21 00:22:34 +01004049 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07004050 return ret;
Chris Wilson22c344e2009-02-11 14:26:45 +00004051 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05004052
Eric Anholt673a3942008-07-30 12:06:12 -07004053 obj_priv->pin_count++;
4054
4055 /* If the object is not active and not pending a flush,
4056 * remove it from the inactive list
4057 */
4058 if (obj_priv->pin_count == 1) {
4059 atomic_inc(&dev->pin_count);
4060 atomic_add(obj->size, &dev->pin_memory);
4061 if (!obj_priv->active &&
Chris Wilsonbf1a1092010-08-07 11:01:20 +01004062 (obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
Eric Anholt673a3942008-07-30 12:06:12 -07004063 list_del_init(&obj_priv->list);
4064 }
4065 i915_verify_inactive(dev, __FILE__, __LINE__);
4066
4067 return 0;
4068}
4069
4070void
4071i915_gem_object_unpin(struct drm_gem_object *obj)
4072{
4073 struct drm_device *dev = obj->dev;
4074 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01004075 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004076
4077 i915_verify_inactive(dev, __FILE__, __LINE__);
4078 obj_priv->pin_count--;
4079 BUG_ON(obj_priv->pin_count < 0);
4080 BUG_ON(obj_priv->gtt_space == NULL);
4081
4082 /* If the object is no longer pinned, and is
4083 * neither active nor being flushed, then stick it on
4084 * the inactive list
4085 */
4086 if (obj_priv->pin_count == 0) {
4087 if (!obj_priv->active &&
Chris Wilson21d509e2009-06-06 09:46:02 +01004088 (obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
Eric Anholt673a3942008-07-30 12:06:12 -07004089 list_move_tail(&obj_priv->list,
4090 &dev_priv->mm.inactive_list);
4091 atomic_dec(&dev->pin_count);
4092 atomic_sub(obj->size, &dev->pin_memory);
4093 }
4094 i915_verify_inactive(dev, __FILE__, __LINE__);
4095}
4096
4097int
4098i915_gem_pin_ioctl(struct drm_device *dev, void *data,
4099 struct drm_file *file_priv)
4100{
4101 struct drm_i915_gem_pin *args = data;
4102 struct drm_gem_object *obj;
4103 struct drm_i915_gem_object *obj_priv;
4104 int ret;
4105
4106 mutex_lock(&dev->struct_mutex);
4107
4108 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4109 if (obj == NULL) {
4110 DRM_ERROR("Bad handle in i915_gem_pin_ioctl(): %d\n",
4111 args->handle);
4112 mutex_unlock(&dev->struct_mutex);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01004113 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07004114 }
Daniel Vetter23010e42010-03-08 13:35:02 +01004115 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004116
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004117 if (obj_priv->madv != I915_MADV_WILLNEED) {
4118 DRM_ERROR("Attempting to pin a purgeable buffer\n");
Chris Wilson3ef94da2009-09-14 16:50:29 +01004119 drm_gem_object_unreference(obj);
4120 mutex_unlock(&dev->struct_mutex);
4121 return -EINVAL;
4122 }
4123
Jesse Barnes79e53942008-11-07 14:24:08 -08004124 if (obj_priv->pin_filp != NULL && obj_priv->pin_filp != file_priv) {
4125 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
4126 args->handle);
Chris Wilson96dec612009-02-08 19:08:04 +00004127 drm_gem_object_unreference(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004128 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08004129 return -EINVAL;
4130 }
4131
4132 obj_priv->user_pin_count++;
4133 obj_priv->pin_filp = file_priv;
4134 if (obj_priv->user_pin_count == 1) {
4135 ret = i915_gem_object_pin(obj, args->alignment);
4136 if (ret != 0) {
4137 drm_gem_object_unreference(obj);
4138 mutex_unlock(&dev->struct_mutex);
4139 return ret;
4140 }
Eric Anholt673a3942008-07-30 12:06:12 -07004141 }
4142
4143 /* XXX - flush the CPU caches for pinned objects
4144 * as the X server doesn't manage domains yet
4145 */
Eric Anholte47c68e2008-11-14 13:35:19 -08004146 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004147 args->offset = obj_priv->gtt_offset;
4148 drm_gem_object_unreference(obj);
4149 mutex_unlock(&dev->struct_mutex);
4150
4151 return 0;
4152}
4153
4154int
4155i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
4156 struct drm_file *file_priv)
4157{
4158 struct drm_i915_gem_pin *args = data;
4159 struct drm_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08004160 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07004161
4162 mutex_lock(&dev->struct_mutex);
4163
4164 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4165 if (obj == NULL) {
4166 DRM_ERROR("Bad handle in i915_gem_unpin_ioctl(): %d\n",
4167 args->handle);
4168 mutex_unlock(&dev->struct_mutex);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01004169 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07004170 }
4171
Daniel Vetter23010e42010-03-08 13:35:02 +01004172 obj_priv = to_intel_bo(obj);
Jesse Barnes79e53942008-11-07 14:24:08 -08004173 if (obj_priv->pin_filp != file_priv) {
4174 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
4175 args->handle);
4176 drm_gem_object_unreference(obj);
4177 mutex_unlock(&dev->struct_mutex);
4178 return -EINVAL;
4179 }
4180 obj_priv->user_pin_count--;
4181 if (obj_priv->user_pin_count == 0) {
4182 obj_priv->pin_filp = NULL;
4183 i915_gem_object_unpin(obj);
4184 }
Eric Anholt673a3942008-07-30 12:06:12 -07004185
4186 drm_gem_object_unreference(obj);
4187 mutex_unlock(&dev->struct_mutex);
4188 return 0;
4189}
4190
4191int
4192i915_gem_busy_ioctl(struct drm_device *dev, void *data,
4193 struct drm_file *file_priv)
4194{
4195 struct drm_i915_gem_busy *args = data;
4196 struct drm_gem_object *obj;
4197 struct drm_i915_gem_object *obj_priv;
4198
Eric Anholt673a3942008-07-30 12:06:12 -07004199 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4200 if (obj == NULL) {
4201 DRM_ERROR("Bad handle in i915_gem_busy_ioctl(): %d\n",
4202 args->handle);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01004203 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07004204 }
4205
Chris Wilsonb1ce7862009-06-06 09:46:00 +01004206 mutex_lock(&dev->struct_mutex);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004207
Chris Wilson0be555b2010-08-04 15:36:30 +01004208 /* Count all active objects as busy, even if they are currently not used
4209 * by the gpu. Users of this interface expect objects to eventually
4210 * become non-busy without any further actions, therefore emit any
4211 * necessary flushes here.
Eric Anholtc4de0a52008-12-14 19:05:04 -08004212 */
Chris Wilson0be555b2010-08-04 15:36:30 +01004213 obj_priv = to_intel_bo(obj);
4214 args->busy = obj_priv->active;
4215 if (args->busy) {
4216 /* Unconditionally flush objects, even when the gpu still uses this
4217 * object. Userspace calling this function indicates that it wants to
4218 * use this buffer rather sooner than later, so issuing the required
4219 * flush earlier is beneficial.
4220 */
4221 if (obj->write_domain) {
4222 i915_gem_flush(dev, 0, obj->write_domain);
4223 (void)i915_add_request(dev, file_priv, obj->write_domain, obj_priv->ring);
4224 }
4225
4226 /* Update the active list for the hardware's current position.
4227 * Otherwise this only updates on a delayed timer or when irqs
4228 * are actually unmasked, and our working set ends up being
4229 * larger than required.
4230 */
4231 i915_gem_retire_requests_ring(dev, obj_priv->ring);
4232
4233 args->busy = obj_priv->active;
4234 }
Eric Anholt673a3942008-07-30 12:06:12 -07004235
4236 drm_gem_object_unreference(obj);
4237 mutex_unlock(&dev->struct_mutex);
4238 return 0;
4239}
4240
4241int
4242i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4243 struct drm_file *file_priv)
4244{
4245 return i915_gem_ring_throttle(dev, file_priv);
4246}
4247
Chris Wilson3ef94da2009-09-14 16:50:29 +01004248int
4249i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4250 struct drm_file *file_priv)
4251{
4252 struct drm_i915_gem_madvise *args = data;
4253 struct drm_gem_object *obj;
4254 struct drm_i915_gem_object *obj_priv;
4255
4256 switch (args->madv) {
4257 case I915_MADV_DONTNEED:
4258 case I915_MADV_WILLNEED:
4259 break;
4260 default:
4261 return -EINVAL;
4262 }
4263
4264 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4265 if (obj == NULL) {
4266 DRM_ERROR("Bad handle in i915_gem_madvise_ioctl(): %d\n",
4267 args->handle);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01004268 return -ENOENT;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004269 }
4270
4271 mutex_lock(&dev->struct_mutex);
Daniel Vetter23010e42010-03-08 13:35:02 +01004272 obj_priv = to_intel_bo(obj);
Chris Wilson3ef94da2009-09-14 16:50:29 +01004273
4274 if (obj_priv->pin_count) {
4275 drm_gem_object_unreference(obj);
4276 mutex_unlock(&dev->struct_mutex);
4277
4278 DRM_ERROR("Attempted i915_gem_madvise_ioctl() on a pinned object\n");
4279 return -EINVAL;
4280 }
4281
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004282 if (obj_priv->madv != __I915_MADV_PURGED)
4283 obj_priv->madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004284
Chris Wilson2d7ef392009-09-20 23:13:10 +01004285 /* if the object is no longer bound, discard its backing storage */
4286 if (i915_gem_object_is_purgeable(obj_priv) &&
4287 obj_priv->gtt_space == NULL)
4288 i915_gem_object_truncate(obj);
4289
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004290 args->retained = obj_priv->madv != __I915_MADV_PURGED;
4291
Chris Wilson3ef94da2009-09-14 16:50:29 +01004292 drm_gem_object_unreference(obj);
4293 mutex_unlock(&dev->struct_mutex);
4294
4295 return 0;
4296}
4297
Daniel Vetterac52bc52010-04-09 19:05:06 +00004298struct drm_gem_object * i915_gem_alloc_object(struct drm_device *dev,
4299 size_t size)
4300{
Daniel Vetterc397b902010-04-09 19:05:07 +00004301 struct drm_i915_gem_object *obj;
4302
4303 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
4304 if (obj == NULL)
4305 return NULL;
4306
4307 if (drm_gem_object_init(dev, &obj->base, size) != 0) {
4308 kfree(obj);
4309 return NULL;
4310 }
4311
4312 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4313 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4314
4315 obj->agp_type = AGP_USER_MEMORY;
Daniel Vetter62b8b212010-04-09 19:05:08 +00004316 obj->base.driver_private = NULL;
Daniel Vetterc397b902010-04-09 19:05:07 +00004317 obj->fence_reg = I915_FENCE_REG_NONE;
4318 INIT_LIST_HEAD(&obj->list);
4319 INIT_LIST_HEAD(&obj->gpu_write_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00004320 obj->madv = I915_MADV_WILLNEED;
4321
4322 trace_i915_gem_object_create(&obj->base);
4323
4324 return &obj->base;
Daniel Vetterac52bc52010-04-09 19:05:06 +00004325}
4326
Eric Anholt673a3942008-07-30 12:06:12 -07004327int i915_gem_init_object(struct drm_gem_object *obj)
4328{
Daniel Vetterc397b902010-04-09 19:05:07 +00004329 BUG();
Jesse Barnesde151cf2008-11-12 10:03:55 -08004330
Eric Anholt673a3942008-07-30 12:06:12 -07004331 return 0;
4332}
4333
Chris Wilsonbe726152010-07-23 23:18:50 +01004334static void i915_gem_free_object_tail(struct drm_gem_object *obj)
4335{
4336 struct drm_device *dev = obj->dev;
4337 drm_i915_private_t *dev_priv = dev->dev_private;
4338 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
4339 int ret;
4340
4341 ret = i915_gem_object_unbind(obj);
4342 if (ret == -ERESTARTSYS) {
4343 list_move(&obj_priv->list,
4344 &dev_priv->mm.deferred_free_list);
4345 return;
4346 }
4347
4348 if (obj_priv->mmap_offset)
4349 i915_gem_free_mmap_offset(obj);
4350
4351 drm_gem_object_release(obj);
4352
4353 kfree(obj_priv->page_cpu_valid);
4354 kfree(obj_priv->bit_17);
4355 kfree(obj_priv);
4356}
4357
Eric Anholt673a3942008-07-30 12:06:12 -07004358void i915_gem_free_object(struct drm_gem_object *obj)
4359{
Jesse Barnesde151cf2008-11-12 10:03:55 -08004360 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01004361 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004362
Chris Wilson1c5d22f2009-08-25 11:15:50 +01004363 trace_i915_gem_object_destroy(obj);
4364
Eric Anholt673a3942008-07-30 12:06:12 -07004365 while (obj_priv->pin_count > 0)
4366 i915_gem_object_unpin(obj);
4367
Dave Airlie71acb5e2008-12-30 20:31:46 +10004368 if (obj_priv->phys_obj)
4369 i915_gem_detach_phys_object(dev, obj);
4370
Chris Wilsonbe726152010-07-23 23:18:50 +01004371 i915_gem_free_object_tail(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004372}
4373
Jesse Barnes5669fca2009-02-17 15:13:31 -08004374int
Eric Anholt673a3942008-07-30 12:06:12 -07004375i915_gem_idle(struct drm_device *dev)
4376{
4377 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson29105cc2010-01-07 10:39:13 +00004378 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004379
Keith Packard6dbe2772008-10-14 21:41:13 -07004380 mutex_lock(&dev->struct_mutex);
4381
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004382 if (dev_priv->mm.suspended ||
Zou Nan haid1b851f2010-05-21 09:08:57 +08004383 (dev_priv->render_ring.gem_object == NULL) ||
4384 (HAS_BSD(dev) &&
4385 dev_priv->bsd_ring.gem_object == NULL)) {
Keith Packard6dbe2772008-10-14 21:41:13 -07004386 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004387 return 0;
Keith Packard6dbe2772008-10-14 21:41:13 -07004388 }
Eric Anholt673a3942008-07-30 12:06:12 -07004389
Chris Wilson29105cc2010-01-07 10:39:13 +00004390 ret = i915_gpu_idle(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004391 if (ret) {
4392 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004393 return ret;
Keith Packard6dbe2772008-10-14 21:41:13 -07004394 }
Eric Anholt673a3942008-07-30 12:06:12 -07004395
Chris Wilson29105cc2010-01-07 10:39:13 +00004396 /* Under UMS, be paranoid and evict. */
4397 if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
Chris Wilsonb47eb4a2010-08-07 11:01:23 +01004398 ret = i915_gem_evict_inactive(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00004399 if (ret) {
4400 mutex_unlock(&dev->struct_mutex);
4401 return ret;
4402 }
4403 }
4404
4405 /* Hack! Don't let anybody do execbuf while we don't control the chip.
4406 * We need to replace this with a semaphore, or something.
4407 * And not confound mm.suspended!
4408 */
4409 dev_priv->mm.suspended = 1;
4410 del_timer(&dev_priv->hangcheck_timer);
4411
4412 i915_kernel_lost_context(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004413 i915_gem_cleanup_ringbuffer(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00004414
Keith Packard6dbe2772008-10-14 21:41:13 -07004415 mutex_unlock(&dev->struct_mutex);
4416
Chris Wilson29105cc2010-01-07 10:39:13 +00004417 /* Cancel the retire work handler, which should be idle now. */
4418 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
4419
Eric Anholt673a3942008-07-30 12:06:12 -07004420 return 0;
4421}
4422
Jesse Barnese552eb72010-04-21 11:39:23 -07004423/*
4424 * 965+ support PIPE_CONTROL commands, which provide finer grained control
4425 * over cache flushing.
4426 */
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004427static int
Jesse Barnese552eb72010-04-21 11:39:23 -07004428i915_gem_init_pipe_control(struct drm_device *dev)
4429{
4430 drm_i915_private_t *dev_priv = dev->dev_private;
4431 struct drm_gem_object *obj;
4432 struct drm_i915_gem_object *obj_priv;
4433 int ret;
4434
Eric Anholt34dc4d42010-05-07 14:30:03 -07004435 obj = i915_gem_alloc_object(dev, 4096);
Jesse Barnese552eb72010-04-21 11:39:23 -07004436 if (obj == NULL) {
4437 DRM_ERROR("Failed to allocate seqno page\n");
4438 ret = -ENOMEM;
4439 goto err;
4440 }
4441 obj_priv = to_intel_bo(obj);
4442 obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
4443
4444 ret = i915_gem_object_pin(obj, 4096);
4445 if (ret)
4446 goto err_unref;
4447
4448 dev_priv->seqno_gfx_addr = obj_priv->gtt_offset;
4449 dev_priv->seqno_page = kmap(obj_priv->pages[0]);
4450 if (dev_priv->seqno_page == NULL)
4451 goto err_unpin;
4452
4453 dev_priv->seqno_obj = obj;
4454 memset(dev_priv->seqno_page, 0, PAGE_SIZE);
4455
4456 return 0;
4457
4458err_unpin:
4459 i915_gem_object_unpin(obj);
4460err_unref:
4461 drm_gem_object_unreference(obj);
4462err:
4463 return ret;
4464}
4465
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004466
4467static void
Jesse Barnese552eb72010-04-21 11:39:23 -07004468i915_gem_cleanup_pipe_control(struct drm_device *dev)
4469{
4470 drm_i915_private_t *dev_priv = dev->dev_private;
4471 struct drm_gem_object *obj;
4472 struct drm_i915_gem_object *obj_priv;
4473
4474 obj = dev_priv->seqno_obj;
4475 obj_priv = to_intel_bo(obj);
4476 kunmap(obj_priv->pages[0]);
4477 i915_gem_object_unpin(obj);
4478 drm_gem_object_unreference(obj);
4479 dev_priv->seqno_obj = NULL;
4480
4481 dev_priv->seqno_page = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07004482}
4483
Eric Anholt673a3942008-07-30 12:06:12 -07004484int
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004485i915_gem_init_ringbuffer(struct drm_device *dev)
4486{
4487 drm_i915_private_t *dev_priv = dev->dev_private;
4488 int ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01004489
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004490 dev_priv->render_ring = render_ring;
Chris Wilson68f95ba2010-05-27 13:18:22 +01004491
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004492 if (!I915_NEED_GFX_HWS(dev)) {
4493 dev_priv->render_ring.status_page.page_addr
4494 = dev_priv->status_page_dmah->vaddr;
4495 memset(dev_priv->render_ring.status_page.page_addr,
4496 0, PAGE_SIZE);
4497 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004498
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004499 if (HAS_PIPE_CONTROL(dev)) {
4500 ret = i915_gem_init_pipe_control(dev);
4501 if (ret)
4502 return ret;
4503 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004504
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004505 ret = intel_init_ring_buffer(dev, &dev_priv->render_ring);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004506 if (ret)
4507 goto cleanup_pipe_control;
4508
4509 if (HAS_BSD(dev)) {
Zou Nan haid1b851f2010-05-21 09:08:57 +08004510 dev_priv->bsd_ring = bsd_ring;
4511 ret = intel_init_ring_buffer(dev, &dev_priv->bsd_ring);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004512 if (ret)
4513 goto cleanup_render_ring;
Zou Nan haid1b851f2010-05-21 09:08:57 +08004514 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004515
Chris Wilson6f392d52010-08-07 11:01:22 +01004516 dev_priv->next_seqno = 1;
4517
Chris Wilson68f95ba2010-05-27 13:18:22 +01004518 return 0;
4519
4520cleanup_render_ring:
4521 intel_cleanup_ring_buffer(dev, &dev_priv->render_ring);
4522cleanup_pipe_control:
4523 if (HAS_PIPE_CONTROL(dev))
4524 i915_gem_cleanup_pipe_control(dev);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004525 return ret;
4526}
4527
4528void
4529i915_gem_cleanup_ringbuffer(struct drm_device *dev)
4530{
4531 drm_i915_private_t *dev_priv = dev->dev_private;
4532
4533 intel_cleanup_ring_buffer(dev, &dev_priv->render_ring);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004534 if (HAS_BSD(dev))
4535 intel_cleanup_ring_buffer(dev, &dev_priv->bsd_ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004536 if (HAS_PIPE_CONTROL(dev))
4537 i915_gem_cleanup_pipe_control(dev);
4538}
4539
4540int
Eric Anholt673a3942008-07-30 12:06:12 -07004541i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
4542 struct drm_file *file_priv)
4543{
4544 drm_i915_private_t *dev_priv = dev->dev_private;
4545 int ret;
4546
Jesse Barnes79e53942008-11-07 14:24:08 -08004547 if (drm_core_check_feature(dev, DRIVER_MODESET))
4548 return 0;
4549
Ben Gamariba1234d2009-09-14 17:48:47 -04004550 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07004551 DRM_ERROR("Reenabling wedged hardware, good luck\n");
Ben Gamariba1234d2009-09-14 17:48:47 -04004552 atomic_set(&dev_priv->mm.wedged, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004553 }
4554
Eric Anholt673a3942008-07-30 12:06:12 -07004555 mutex_lock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004556 dev_priv->mm.suspended = 0;
4557
4558 ret = i915_gem_init_ringbuffer(dev);
Wu Fengguangd816f6ac2009-04-18 10:43:32 +08004559 if (ret != 0) {
4560 mutex_unlock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004561 return ret;
Wu Fengguangd816f6ac2009-04-18 10:43:32 +08004562 }
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004563
Carl Worth5e118f42009-03-20 11:54:25 -07004564 spin_lock(&dev_priv->mm.active_list_lock);
Zou Nan hai852835f2010-05-21 09:08:56 +08004565 BUG_ON(!list_empty(&dev_priv->render_ring.active_list));
Zou Nan haid1b851f2010-05-21 09:08:57 +08004566 BUG_ON(HAS_BSD(dev) && !list_empty(&dev_priv->bsd_ring.active_list));
Carl Worth5e118f42009-03-20 11:54:25 -07004567 spin_unlock(&dev_priv->mm.active_list_lock);
4568
Eric Anholt673a3942008-07-30 12:06:12 -07004569 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
4570 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
Zou Nan hai852835f2010-05-21 09:08:56 +08004571 BUG_ON(!list_empty(&dev_priv->render_ring.request_list));
Zou Nan haid1b851f2010-05-21 09:08:57 +08004572 BUG_ON(HAS_BSD(dev) && !list_empty(&dev_priv->bsd_ring.request_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004573 mutex_unlock(&dev->struct_mutex);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004574
Chris Wilson5f353082010-06-07 14:03:03 +01004575 ret = drm_irq_install(dev);
4576 if (ret)
4577 goto cleanup_ringbuffer;
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004578
Eric Anholt673a3942008-07-30 12:06:12 -07004579 return 0;
Chris Wilson5f353082010-06-07 14:03:03 +01004580
4581cleanup_ringbuffer:
4582 mutex_lock(&dev->struct_mutex);
4583 i915_gem_cleanup_ringbuffer(dev);
4584 dev_priv->mm.suspended = 1;
4585 mutex_unlock(&dev->struct_mutex);
4586
4587 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004588}
4589
4590int
4591i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
4592 struct drm_file *file_priv)
4593{
Jesse Barnes79e53942008-11-07 14:24:08 -08004594 if (drm_core_check_feature(dev, DRIVER_MODESET))
4595 return 0;
4596
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004597 drm_irq_uninstall(dev);
Linus Torvaldse6890f62009-09-08 17:09:24 -07004598 return i915_gem_idle(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004599}
4600
4601void
4602i915_gem_lastclose(struct drm_device *dev)
4603{
4604 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004605
Eric Anholte806b492009-01-22 09:56:58 -08004606 if (drm_core_check_feature(dev, DRIVER_MODESET))
4607 return;
4608
Keith Packard6dbe2772008-10-14 21:41:13 -07004609 ret = i915_gem_idle(dev);
4610 if (ret)
4611 DRM_ERROR("failed to idle hardware: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07004612}
4613
4614void
4615i915_gem_load(struct drm_device *dev)
4616{
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004617 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07004618 drm_i915_private_t *dev_priv = dev->dev_private;
4619
Carl Worth5e118f42009-03-20 11:54:25 -07004620 spin_lock_init(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07004621 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
Daniel Vetter99fcb762010-02-07 16:20:18 +01004622 INIT_LIST_HEAD(&dev_priv->mm.gpu_write_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004623 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07004624 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Chris Wilsonbe726152010-07-23 23:18:50 +01004625 INIT_LIST_HEAD(&dev_priv->mm.deferred_free_list);
Zou Nan hai852835f2010-05-21 09:08:56 +08004626 INIT_LIST_HEAD(&dev_priv->render_ring.active_list);
4627 INIT_LIST_HEAD(&dev_priv->render_ring.request_list);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004628 if (HAS_BSD(dev)) {
4629 INIT_LIST_HEAD(&dev_priv->bsd_ring.active_list);
4630 INIT_LIST_HEAD(&dev_priv->bsd_ring.request_list);
4631 }
Daniel Vetter007cc8a2010-04-28 11:02:31 +02004632 for (i = 0; i < 16; i++)
4633 INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004634 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
4635 i915_gem_retire_work_handler);
Chris Wilson31169712009-09-14 16:50:28 +01004636 spin_lock(&shrink_list_lock);
4637 list_add(&dev_priv->mm.shrink_list, &shrink_list);
4638 spin_unlock(&shrink_list_lock);
4639
Dave Airlie94400122010-07-20 13:15:31 +10004640 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
4641 if (IS_GEN3(dev)) {
4642 u32 tmp = I915_READ(MI_ARB_STATE);
4643 if (!(tmp & MI_ARB_C3_LP_WRITE_ENABLE)) {
4644 /* arb state is a masked write, so set bit + bit in mask */
4645 tmp = MI_ARB_C3_LP_WRITE_ENABLE | (MI_ARB_C3_LP_WRITE_ENABLE << MI_ARB_MASK_SHIFT);
4646 I915_WRITE(MI_ARB_STATE, tmp);
4647 }
4648 }
4649
Jesse Barnesde151cf2008-11-12 10:03:55 -08004650 /* Old X drivers will take 0-2 for front, back, depth buffers */
Eric Anholtb397c832010-01-26 09:43:10 -08004651 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4652 dev_priv->fence_reg_start = 3;
Jesse Barnesde151cf2008-11-12 10:03:55 -08004653
Jesse Barnes0f973f22009-01-26 17:10:45 -08004654 if (IS_I965G(dev) || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08004655 dev_priv->num_fence_regs = 16;
4656 else
4657 dev_priv->num_fence_regs = 8;
4658
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004659 /* Initialize fence registers to zero */
4660 if (IS_I965G(dev)) {
4661 for (i = 0; i < 16; i++)
4662 I915_WRITE64(FENCE_REG_965_0 + (i * 8), 0);
4663 } else {
4664 for (i = 0; i < 8; i++)
4665 I915_WRITE(FENCE_REG_830_0 + (i * 4), 0);
4666 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4667 for (i = 0; i < 8; i++)
4668 I915_WRITE(FENCE_REG_945_8 + (i * 4), 0);
4669 }
Eric Anholt673a3942008-07-30 12:06:12 -07004670 i915_gem_detect_bit_6_swizzle(dev);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05004671 init_waitqueue_head(&dev_priv->pending_flip_queue);
Eric Anholt673a3942008-07-30 12:06:12 -07004672}
Dave Airlie71acb5e2008-12-30 20:31:46 +10004673
4674/*
4675 * Create a physically contiguous memory object for this object
4676 * e.g. for cursor + overlay regs
4677 */
4678int i915_gem_init_phys_object(struct drm_device *dev,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004679 int id, int size, int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004680{
4681 drm_i915_private_t *dev_priv = dev->dev_private;
4682 struct drm_i915_gem_phys_object *phys_obj;
4683 int ret;
4684
4685 if (dev_priv->mm.phys_objs[id - 1] || !size)
4686 return 0;
4687
Eric Anholt9a298b22009-03-24 12:23:04 -07004688 phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004689 if (!phys_obj)
4690 return -ENOMEM;
4691
4692 phys_obj->id = id;
4693
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004694 phys_obj->handle = drm_pci_alloc(dev, size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004695 if (!phys_obj->handle) {
4696 ret = -ENOMEM;
4697 goto kfree_obj;
4698 }
4699#ifdef CONFIG_X86
4700 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4701#endif
4702
4703 dev_priv->mm.phys_objs[id - 1] = phys_obj;
4704
4705 return 0;
4706kfree_obj:
Eric Anholt9a298b22009-03-24 12:23:04 -07004707 kfree(phys_obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004708 return ret;
4709}
4710
4711void i915_gem_free_phys_object(struct drm_device *dev, int id)
4712{
4713 drm_i915_private_t *dev_priv = dev->dev_private;
4714 struct drm_i915_gem_phys_object *phys_obj;
4715
4716 if (!dev_priv->mm.phys_objs[id - 1])
4717 return;
4718
4719 phys_obj = dev_priv->mm.phys_objs[id - 1];
4720 if (phys_obj->cur_obj) {
4721 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
4722 }
4723
4724#ifdef CONFIG_X86
4725 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4726#endif
4727 drm_pci_free(dev, phys_obj->handle);
4728 kfree(phys_obj);
4729 dev_priv->mm.phys_objs[id - 1] = NULL;
4730}
4731
4732void i915_gem_free_all_phys_object(struct drm_device *dev)
4733{
4734 int i;
4735
Dave Airlie260883c2009-01-22 17:58:49 +10004736 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004737 i915_gem_free_phys_object(dev, i);
4738}
4739
4740void i915_gem_detach_phys_object(struct drm_device *dev,
4741 struct drm_gem_object *obj)
4742{
4743 struct drm_i915_gem_object *obj_priv;
4744 int i;
4745 int ret;
4746 int page_count;
4747
Daniel Vetter23010e42010-03-08 13:35:02 +01004748 obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004749 if (!obj_priv->phys_obj)
4750 return;
4751
Chris Wilson4bdadb92010-01-27 13:36:32 +00004752 ret = i915_gem_object_get_pages(obj, 0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004753 if (ret)
4754 goto out;
4755
4756 page_count = obj->size / PAGE_SIZE;
4757
4758 for (i = 0; i < page_count; i++) {
Eric Anholt856fa192009-03-19 14:10:50 -07004759 char *dst = kmap_atomic(obj_priv->pages[i], KM_USER0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004760 char *src = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4761
4762 memcpy(dst, src, PAGE_SIZE);
4763 kunmap_atomic(dst, KM_USER0);
4764 }
Eric Anholt856fa192009-03-19 14:10:50 -07004765 drm_clflush_pages(obj_priv->pages, page_count);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004766 drm_agp_chipset_flush(dev);
Chris Wilsond78b47b2009-06-17 21:52:49 +01004767
4768 i915_gem_object_put_pages(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004769out:
4770 obj_priv->phys_obj->cur_obj = NULL;
4771 obj_priv->phys_obj = NULL;
4772}
4773
4774int
4775i915_gem_attach_phys_object(struct drm_device *dev,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004776 struct drm_gem_object *obj,
4777 int id,
4778 int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004779{
4780 drm_i915_private_t *dev_priv = dev->dev_private;
4781 struct drm_i915_gem_object *obj_priv;
4782 int ret = 0;
4783 int page_count;
4784 int i;
4785
4786 if (id > I915_MAX_PHYS_OBJECT)
4787 return -EINVAL;
4788
Daniel Vetter23010e42010-03-08 13:35:02 +01004789 obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004790
4791 if (obj_priv->phys_obj) {
4792 if (obj_priv->phys_obj->id == id)
4793 return 0;
4794 i915_gem_detach_phys_object(dev, obj);
4795 }
4796
Dave Airlie71acb5e2008-12-30 20:31:46 +10004797 /* create a new object */
4798 if (!dev_priv->mm.phys_objs[id - 1]) {
4799 ret = i915_gem_init_phys_object(dev, id,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004800 obj->size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004801 if (ret) {
Linus Torvaldsaeb565d2009-01-26 10:01:53 -08004802 DRM_ERROR("failed to init phys object %d size: %zu\n", id, obj->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004803 goto out;
4804 }
4805 }
4806
4807 /* bind to the object */
4808 obj_priv->phys_obj = dev_priv->mm.phys_objs[id - 1];
4809 obj_priv->phys_obj->cur_obj = obj;
4810
Chris Wilson4bdadb92010-01-27 13:36:32 +00004811 ret = i915_gem_object_get_pages(obj, 0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004812 if (ret) {
4813 DRM_ERROR("failed to get page list\n");
4814 goto out;
4815 }
4816
4817 page_count = obj->size / PAGE_SIZE;
4818
4819 for (i = 0; i < page_count; i++) {
Eric Anholt856fa192009-03-19 14:10:50 -07004820 char *src = kmap_atomic(obj_priv->pages[i], KM_USER0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004821 char *dst = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4822
4823 memcpy(dst, src, PAGE_SIZE);
4824 kunmap_atomic(src, KM_USER0);
4825 }
4826
Chris Wilsond78b47b2009-06-17 21:52:49 +01004827 i915_gem_object_put_pages(obj);
4828
Dave Airlie71acb5e2008-12-30 20:31:46 +10004829 return 0;
4830out:
4831 return ret;
4832}
4833
4834static int
4835i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
4836 struct drm_i915_gem_pwrite *args,
4837 struct drm_file *file_priv)
4838{
Daniel Vetter23010e42010-03-08 13:35:02 +01004839 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004840 void *obj_addr;
4841 int ret;
4842 char __user *user_data;
4843
4844 user_data = (char __user *) (uintptr_t) args->data_ptr;
4845 obj_addr = obj_priv->phys_obj->handle->vaddr + args->offset;
4846
Zhao Yakui44d98a62009-10-09 11:39:40 +08004847 DRM_DEBUG_DRIVER("obj_addr %p, %lld\n", obj_addr, args->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004848 ret = copy_from_user(obj_addr, user_data, args->size);
4849 if (ret)
4850 return -EFAULT;
4851
4852 drm_agp_chipset_flush(dev);
4853 return 0;
4854}
Eric Anholtb9624422009-06-03 07:27:35 +00004855
4856void i915_gem_release(struct drm_device * dev, struct drm_file *file_priv)
4857{
4858 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
4859
4860 /* Clean up our request list when the client is going away, so that
4861 * later retire_requests won't dereference our soon-to-be-gone
4862 * file_priv.
4863 */
4864 mutex_lock(&dev->struct_mutex);
4865 while (!list_empty(&i915_file_priv->mm.request_list))
4866 list_del_init(i915_file_priv->mm.request_list.next);
4867 mutex_unlock(&dev->struct_mutex);
4868}
Chris Wilson31169712009-09-14 16:50:28 +01004869
Chris Wilson31169712009-09-14 16:50:28 +01004870static int
Chris Wilson1637ef42010-04-20 17:10:35 +01004871i915_gpu_is_active(struct drm_device *dev)
4872{
4873 drm_i915_private_t *dev_priv = dev->dev_private;
4874 int lists_empty;
4875
4876 spin_lock(&dev_priv->mm.active_list_lock);
4877 lists_empty = list_empty(&dev_priv->mm.flushing_list) &&
Zou Nan hai852835f2010-05-21 09:08:56 +08004878 list_empty(&dev_priv->render_ring.active_list);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004879 if (HAS_BSD(dev))
4880 lists_empty &= list_empty(&dev_priv->bsd_ring.active_list);
Chris Wilson1637ef42010-04-20 17:10:35 +01004881 spin_unlock(&dev_priv->mm.active_list_lock);
4882
4883 return !lists_empty;
4884}
4885
4886static int
Dave Chinner7f8275d2010-07-19 14:56:17 +10004887i915_gem_shrink(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask)
Chris Wilson31169712009-09-14 16:50:28 +01004888{
4889 drm_i915_private_t *dev_priv, *next_dev;
4890 struct drm_i915_gem_object *obj_priv, *next_obj;
4891 int cnt = 0;
4892 int would_deadlock = 1;
4893
4894 /* "fast-path" to count number of available objects */
4895 if (nr_to_scan == 0) {
4896 spin_lock(&shrink_list_lock);
4897 list_for_each_entry(dev_priv, &shrink_list, mm.shrink_list) {
4898 struct drm_device *dev = dev_priv->dev;
4899
4900 if (mutex_trylock(&dev->struct_mutex)) {
4901 list_for_each_entry(obj_priv,
4902 &dev_priv->mm.inactive_list,
4903 list)
4904 cnt++;
4905 mutex_unlock(&dev->struct_mutex);
4906 }
4907 }
4908 spin_unlock(&shrink_list_lock);
4909
4910 return (cnt / 100) * sysctl_vfs_cache_pressure;
4911 }
4912
4913 spin_lock(&shrink_list_lock);
4914
Chris Wilson1637ef42010-04-20 17:10:35 +01004915rescan:
Chris Wilson31169712009-09-14 16:50:28 +01004916 /* first scan for clean buffers */
4917 list_for_each_entry_safe(dev_priv, next_dev,
4918 &shrink_list, mm.shrink_list) {
4919 struct drm_device *dev = dev_priv->dev;
4920
4921 if (! mutex_trylock(&dev->struct_mutex))
4922 continue;
4923
4924 spin_unlock(&shrink_list_lock);
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01004925 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004926
Chris Wilson31169712009-09-14 16:50:28 +01004927 list_for_each_entry_safe(obj_priv, next_obj,
4928 &dev_priv->mm.inactive_list,
4929 list) {
4930 if (i915_gem_object_is_purgeable(obj_priv)) {
Daniel Vettera8089e82010-04-09 19:05:09 +00004931 i915_gem_object_unbind(&obj_priv->base);
Chris Wilson31169712009-09-14 16:50:28 +01004932 if (--nr_to_scan <= 0)
4933 break;
4934 }
4935 }
4936
4937 spin_lock(&shrink_list_lock);
4938 mutex_unlock(&dev->struct_mutex);
4939
Chris Wilson963b4832009-09-20 23:03:54 +01004940 would_deadlock = 0;
4941
Chris Wilson31169712009-09-14 16:50:28 +01004942 if (nr_to_scan <= 0)
4943 break;
4944 }
4945
4946 /* second pass, evict/count anything still on the inactive list */
4947 list_for_each_entry_safe(dev_priv, next_dev,
4948 &shrink_list, mm.shrink_list) {
4949 struct drm_device *dev = dev_priv->dev;
4950
4951 if (! mutex_trylock(&dev->struct_mutex))
4952 continue;
4953
4954 spin_unlock(&shrink_list_lock);
4955
4956 list_for_each_entry_safe(obj_priv, next_obj,
4957 &dev_priv->mm.inactive_list,
4958 list) {
4959 if (nr_to_scan > 0) {
Daniel Vettera8089e82010-04-09 19:05:09 +00004960 i915_gem_object_unbind(&obj_priv->base);
Chris Wilson31169712009-09-14 16:50:28 +01004961 nr_to_scan--;
4962 } else
4963 cnt++;
4964 }
4965
4966 spin_lock(&shrink_list_lock);
4967 mutex_unlock(&dev->struct_mutex);
4968
4969 would_deadlock = 0;
4970 }
4971
Chris Wilson1637ef42010-04-20 17:10:35 +01004972 if (nr_to_scan) {
4973 int active = 0;
4974
4975 /*
4976 * We are desperate for pages, so as a last resort, wait
4977 * for the GPU to finish and discard whatever we can.
4978 * This has a dramatic impact to reduce the number of
4979 * OOM-killer events whilst running the GPU aggressively.
4980 */
4981 list_for_each_entry(dev_priv, &shrink_list, mm.shrink_list) {
4982 struct drm_device *dev = dev_priv->dev;
4983
4984 if (!mutex_trylock(&dev->struct_mutex))
4985 continue;
4986
4987 spin_unlock(&shrink_list_lock);
4988
4989 if (i915_gpu_is_active(dev)) {
4990 i915_gpu_idle(dev);
4991 active++;
4992 }
4993
4994 spin_lock(&shrink_list_lock);
4995 mutex_unlock(&dev->struct_mutex);
4996 }
4997
4998 if (active)
4999 goto rescan;
5000 }
5001
Chris Wilson31169712009-09-14 16:50:28 +01005002 spin_unlock(&shrink_list_lock);
5003
5004 if (would_deadlock)
5005 return -1;
5006 else if (cnt > 0)
5007 return (cnt / 100) * sysctl_vfs_cache_pressure;
5008 else
5009 return 0;
5010}
5011
5012static struct shrinker shrinker = {
5013 .shrink = i915_gem_shrink,
5014 .seeks = DEFAULT_SEEKS,
5015};
5016
5017__init void
5018i915_gem_shrinker_init(void)
5019{
5020 register_shrinker(&shrinker);
5021}
5022
5023__exit void
5024i915_gem_shrinker_exit(void)
5025{
5026 unregister_shrinker(&shrinker);
5027}