blob: 1c02798bb7e42f1c63a10f35f1b06ba0dc3ca769 [file] [log] [blame]
Eric Anholt673a3942008-07-30 12:06:12 -07001/*
2 * Copyright © 2008 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 *
26 */
27
28#include "drmP.h"
29#include "drm.h"
30#include "i915_drm.h"
31#include "i915_drv.h"
Chris Wilson1c5d22f2009-08-25 11:15:50 +010032#include "i915_trace.h"
Jesse Barnes652c3932009-08-17 13:31:43 -070033#include "intel_drv.h"
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Eric Anholt673a3942008-07-30 12:06:12 -070035#include <linux/swap.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080036#include <linux/pci.h>
Zhenyu Wangf8f235e2010-08-27 11:08:57 +080037#include <linux/intel-gtt.h>
Eric Anholt673a3942008-07-30 12:06:12 -070038
Daniel Vetter0108a3e2010-08-07 11:01:21 +010039static uint32_t i915_gem_get_gtt_alignment(struct drm_gem_object *obj);
Daniel Vetterba3d8d72010-02-11 22:37:04 +010040
41static int i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj,
42 bool pipelined);
Eric Anholte47c68e2008-11-14 13:35:19 -080043static void i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj);
44static void i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj);
Eric Anholte47c68e2008-11-14 13:35:19 -080045static int i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj,
46 int write);
47static int i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
48 uint64_t offset,
49 uint64_t size);
50static void i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj);
Chris Wilson2cf34d72010-09-14 13:03:28 +010051static int i915_gem_object_wait_rendering(struct drm_gem_object *obj,
52 bool interruptible);
Jesse Barnesde151cf2008-11-12 10:03:55 -080053static int i915_gem_object_bind_to_gtt(struct drm_gem_object *obj,
54 unsigned alignment);
Jesse Barnesde151cf2008-11-12 10:03:55 -080055static void i915_gem_clear_fence_reg(struct drm_gem_object *obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +100056static int i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
57 struct drm_i915_gem_pwrite *args,
58 struct drm_file *file_priv);
Chris Wilsonbe726152010-07-23 23:18:50 +010059static void i915_gem_free_object_tail(struct drm_gem_object *obj);
Eric Anholt673a3942008-07-30 12:06:12 -070060
Chris Wilson31169712009-09-14 16:50:28 +010061static LIST_HEAD(shrink_list);
62static DEFINE_SPINLOCK(shrink_list_lock);
63
Chris Wilson7d1c4802010-08-07 21:45:03 +010064static inline bool
65i915_gem_object_is_inactive(struct drm_i915_gem_object *obj_priv)
66{
67 return obj_priv->gtt_space &&
68 !obj_priv->active &&
69 obj_priv->pin_count == 0;
70}
71
Jesse Barnes79e53942008-11-07 14:24:08 -080072int i915_gem_do_init(struct drm_device *dev, unsigned long start,
73 unsigned long end)
74{
75 drm_i915_private_t *dev_priv = dev->dev_private;
76
77 if (start >= end ||
78 (start & (PAGE_SIZE - 1)) != 0 ||
79 (end & (PAGE_SIZE - 1)) != 0) {
80 return -EINVAL;
81 }
82
83 drm_mm_init(&dev_priv->mm.gtt_space, start,
84 end - start);
85
86 dev->gtt_total = (uint32_t) (end - start);
87
88 return 0;
89}
Keith Packard6dbe2772008-10-14 21:41:13 -070090
Eric Anholt673a3942008-07-30 12:06:12 -070091int
92i915_gem_init_ioctl(struct drm_device *dev, void *data,
93 struct drm_file *file_priv)
94{
Eric Anholt673a3942008-07-30 12:06:12 -070095 struct drm_i915_gem_init *args = data;
Jesse Barnes79e53942008-11-07 14:24:08 -080096 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -070097
98 mutex_lock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080099 ret = i915_gem_do_init(dev, args->gtt_start, args->gtt_end);
Eric Anholt673a3942008-07-30 12:06:12 -0700100 mutex_unlock(&dev->struct_mutex);
101
Jesse Barnes79e53942008-11-07 14:24:08 -0800102 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700103}
104
Eric Anholt5a125c32008-10-22 21:40:13 -0700105int
106i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
107 struct drm_file *file_priv)
108{
Eric Anholt5a125c32008-10-22 21:40:13 -0700109 struct drm_i915_gem_get_aperture *args = data;
Eric Anholt5a125c32008-10-22 21:40:13 -0700110
111 if (!(dev->driver->driver_features & DRIVER_GEM))
112 return -ENODEV;
113
114 args->aper_size = dev->gtt_total;
Keith Packard2678d9d2008-11-20 22:54:54 -0800115 args->aper_available_size = (args->aper_size -
116 atomic_read(&dev->pin_memory));
Eric Anholt5a125c32008-10-22 21:40:13 -0700117
118 return 0;
119}
120
Eric Anholt673a3942008-07-30 12:06:12 -0700121
122/**
123 * Creates a new mm object and returns a handle to it.
124 */
125int
126i915_gem_create_ioctl(struct drm_device *dev, void *data,
127 struct drm_file *file_priv)
128{
129 struct drm_i915_gem_create *args = data;
130 struct drm_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300131 int ret;
132 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700133
134 args->size = roundup(args->size, PAGE_SIZE);
135
136 /* Allocate the new object */
Daniel Vetterac52bc52010-04-09 19:05:06 +0000137 obj = i915_gem_alloc_object(dev, args->size);
Eric Anholt673a3942008-07-30 12:06:12 -0700138 if (obj == NULL)
139 return -ENOMEM;
140
141 ret = drm_gem_handle_create(file_priv, obj, &handle);
Chris Wilson1dfd9752010-09-06 14:44:14 +0100142 if (ret) {
143 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700144 return ret;
Chris Wilson1dfd9752010-09-06 14:44:14 +0100145 }
146
147 /* Sink the floating reference from kref_init(handlecount) */
148 drm_gem_object_handle_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700149
150 args->handle = handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700151 return 0;
152}
153
Eric Anholt40123c12009-03-09 13:42:30 -0700154static inline int
Eric Anholteb014592009-03-10 11:44:52 -0700155fast_shmem_read(struct page **pages,
156 loff_t page_base, int page_offset,
157 char __user *data,
158 int length)
159{
160 char __iomem *vaddr;
Florian Mickler2bc43b52009-04-06 22:55:41 +0200161 int unwritten;
Eric Anholteb014592009-03-10 11:44:52 -0700162
163 vaddr = kmap_atomic(pages[page_base >> PAGE_SHIFT], KM_USER0);
164 if (vaddr == NULL)
165 return -ENOMEM;
Florian Mickler2bc43b52009-04-06 22:55:41 +0200166 unwritten = __copy_to_user_inatomic(data, vaddr + page_offset, length);
Eric Anholteb014592009-03-10 11:44:52 -0700167 kunmap_atomic(vaddr, KM_USER0);
168
Florian Mickler2bc43b52009-04-06 22:55:41 +0200169 if (unwritten)
170 return -EFAULT;
171
172 return 0;
Eric Anholteb014592009-03-10 11:44:52 -0700173}
174
Eric Anholt280b7132009-03-12 16:56:27 -0700175static int i915_gem_object_needs_bit17_swizzle(struct drm_gem_object *obj)
176{
177 drm_i915_private_t *dev_priv = obj->dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +0100178 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt280b7132009-03-12 16:56:27 -0700179
180 return dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_9_10_17 &&
181 obj_priv->tiling_mode != I915_TILING_NONE;
182}
183
Chris Wilson99a03df2010-05-27 14:15:34 +0100184static inline void
Eric Anholt40123c12009-03-09 13:42:30 -0700185slow_shmem_copy(struct page *dst_page,
186 int dst_offset,
187 struct page *src_page,
188 int src_offset,
189 int length)
190{
191 char *dst_vaddr, *src_vaddr;
192
Chris Wilson99a03df2010-05-27 14:15:34 +0100193 dst_vaddr = kmap(dst_page);
194 src_vaddr = kmap(src_page);
Eric Anholt40123c12009-03-09 13:42:30 -0700195
196 memcpy(dst_vaddr + dst_offset, src_vaddr + src_offset, length);
197
Chris Wilson99a03df2010-05-27 14:15:34 +0100198 kunmap(src_page);
199 kunmap(dst_page);
Eric Anholt40123c12009-03-09 13:42:30 -0700200}
201
Chris Wilson99a03df2010-05-27 14:15:34 +0100202static inline void
Eric Anholt280b7132009-03-12 16:56:27 -0700203slow_shmem_bit17_copy(struct page *gpu_page,
204 int gpu_offset,
205 struct page *cpu_page,
206 int cpu_offset,
207 int length,
208 int is_read)
209{
210 char *gpu_vaddr, *cpu_vaddr;
211
212 /* Use the unswizzled path if this page isn't affected. */
213 if ((page_to_phys(gpu_page) & (1 << 17)) == 0) {
214 if (is_read)
215 return slow_shmem_copy(cpu_page, cpu_offset,
216 gpu_page, gpu_offset, length);
217 else
218 return slow_shmem_copy(gpu_page, gpu_offset,
219 cpu_page, cpu_offset, length);
220 }
221
Chris Wilson99a03df2010-05-27 14:15:34 +0100222 gpu_vaddr = kmap(gpu_page);
223 cpu_vaddr = kmap(cpu_page);
Eric Anholt280b7132009-03-12 16:56:27 -0700224
225 /* Copy the data, XORing A6 with A17 (1). The user already knows he's
226 * XORing with the other bits (A9 for Y, A9 and A10 for X)
227 */
228 while (length > 0) {
229 int cacheline_end = ALIGN(gpu_offset + 1, 64);
230 int this_length = min(cacheline_end - gpu_offset, length);
231 int swizzled_gpu_offset = gpu_offset ^ 64;
232
233 if (is_read) {
234 memcpy(cpu_vaddr + cpu_offset,
235 gpu_vaddr + swizzled_gpu_offset,
236 this_length);
237 } else {
238 memcpy(gpu_vaddr + swizzled_gpu_offset,
239 cpu_vaddr + cpu_offset,
240 this_length);
241 }
242 cpu_offset += this_length;
243 gpu_offset += this_length;
244 length -= this_length;
245 }
246
Chris Wilson99a03df2010-05-27 14:15:34 +0100247 kunmap(cpu_page);
248 kunmap(gpu_page);
Eric Anholt280b7132009-03-12 16:56:27 -0700249}
250
Eric Anholt673a3942008-07-30 12:06:12 -0700251/**
Eric Anholteb014592009-03-10 11:44:52 -0700252 * This is the fast shmem pread path, which attempts to copy_from_user directly
253 * from the backing pages of the object to the user's address space. On a
254 * fault, it fails so we can fall back to i915_gem_shmem_pwrite_slow().
255 */
256static int
257i915_gem_shmem_pread_fast(struct drm_device *dev, struct drm_gem_object *obj,
258 struct drm_i915_gem_pread *args,
259 struct drm_file *file_priv)
260{
Daniel Vetter23010e42010-03-08 13:35:02 +0100261 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700262 ssize_t remain;
263 loff_t offset, page_base;
264 char __user *user_data;
265 int page_offset, page_length;
266 int ret;
267
268 user_data = (char __user *) (uintptr_t) args->data_ptr;
269 remain = args->size;
270
271 mutex_lock(&dev->struct_mutex);
272
Chris Wilson4bdadb92010-01-27 13:36:32 +0000273 ret = i915_gem_object_get_pages(obj, 0);
Eric Anholteb014592009-03-10 11:44:52 -0700274 if (ret != 0)
275 goto fail_unlock;
276
277 ret = i915_gem_object_set_cpu_read_domain_range(obj, args->offset,
278 args->size);
279 if (ret != 0)
280 goto fail_put_pages;
281
Daniel Vetter23010e42010-03-08 13:35:02 +0100282 obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700283 offset = args->offset;
284
285 while (remain > 0) {
286 /* Operation in this page
287 *
288 * page_base = page offset within aperture
289 * page_offset = offset within page
290 * page_length = bytes to copy for this page
291 */
292 page_base = (offset & ~(PAGE_SIZE-1));
293 page_offset = offset & (PAGE_SIZE-1);
294 page_length = remain;
295 if ((page_offset + remain) > PAGE_SIZE)
296 page_length = PAGE_SIZE - page_offset;
297
298 ret = fast_shmem_read(obj_priv->pages,
299 page_base, page_offset,
300 user_data, page_length);
301 if (ret)
302 goto fail_put_pages;
303
304 remain -= page_length;
305 user_data += page_length;
306 offset += page_length;
307 }
308
309fail_put_pages:
310 i915_gem_object_put_pages(obj);
311fail_unlock:
312 mutex_unlock(&dev->struct_mutex);
313
314 return ret;
315}
316
Chris Wilson07f73f62009-09-14 16:50:30 +0100317static int
318i915_gem_object_get_pages_or_evict(struct drm_gem_object *obj)
319{
320 int ret;
321
Chris Wilson4bdadb92010-01-27 13:36:32 +0000322 ret = i915_gem_object_get_pages(obj, __GFP_NORETRY | __GFP_NOWARN);
Chris Wilson07f73f62009-09-14 16:50:30 +0100323
324 /* If we've insufficient memory to map in the pages, attempt
325 * to make some space by throwing out some old buffers.
326 */
327 if (ret == -ENOMEM) {
328 struct drm_device *dev = obj->dev;
Chris Wilson07f73f62009-09-14 16:50:30 +0100329
Daniel Vetter0108a3e2010-08-07 11:01:21 +0100330 ret = i915_gem_evict_something(dev, obj->size,
331 i915_gem_get_gtt_alignment(obj));
Chris Wilson07f73f62009-09-14 16:50:30 +0100332 if (ret)
333 return ret;
334
Chris Wilson4bdadb92010-01-27 13:36:32 +0000335 ret = i915_gem_object_get_pages(obj, 0);
Chris Wilson07f73f62009-09-14 16:50:30 +0100336 }
337
338 return ret;
339}
340
Eric Anholteb014592009-03-10 11:44:52 -0700341/**
342 * This is the fallback shmem pread path, which allocates temporary storage
343 * in kernel space to copy_to_user into outside of the struct_mutex, so we
344 * can copy out of the object's backing pages while holding the struct mutex
345 * and not take page faults.
346 */
347static int
348i915_gem_shmem_pread_slow(struct drm_device *dev, struct drm_gem_object *obj,
349 struct drm_i915_gem_pread *args,
350 struct drm_file *file_priv)
351{
Daniel Vetter23010e42010-03-08 13:35:02 +0100352 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700353 struct mm_struct *mm = current->mm;
354 struct page **user_pages;
355 ssize_t remain;
356 loff_t offset, pinned_pages, i;
357 loff_t first_data_page, last_data_page, num_pages;
358 int shmem_page_index, shmem_page_offset;
359 int data_page_index, data_page_offset;
360 int page_length;
361 int ret;
362 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700363 int do_bit17_swizzling;
Eric Anholteb014592009-03-10 11:44:52 -0700364
365 remain = args->size;
366
367 /* Pin the user pages containing the data. We can't fault while
368 * holding the struct mutex, yet we want to hold it while
369 * dereferencing the user data.
370 */
371 first_data_page = data_ptr / PAGE_SIZE;
372 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
373 num_pages = last_data_page - first_data_page + 1;
374
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700375 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
Eric Anholteb014592009-03-10 11:44:52 -0700376 if (user_pages == NULL)
377 return -ENOMEM;
378
379 down_read(&mm->mmap_sem);
380 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
Eric Anholte5e9ecd2009-04-07 16:01:22 -0700381 num_pages, 1, 0, user_pages, NULL);
Eric Anholteb014592009-03-10 11:44:52 -0700382 up_read(&mm->mmap_sem);
383 if (pinned_pages < num_pages) {
384 ret = -EFAULT;
385 goto fail_put_user_pages;
386 }
387
Eric Anholt280b7132009-03-12 16:56:27 -0700388 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
389
Eric Anholteb014592009-03-10 11:44:52 -0700390 mutex_lock(&dev->struct_mutex);
391
Chris Wilson07f73f62009-09-14 16:50:30 +0100392 ret = i915_gem_object_get_pages_or_evict(obj);
393 if (ret)
Eric Anholteb014592009-03-10 11:44:52 -0700394 goto fail_unlock;
395
396 ret = i915_gem_object_set_cpu_read_domain_range(obj, args->offset,
397 args->size);
398 if (ret != 0)
399 goto fail_put_pages;
400
Daniel Vetter23010e42010-03-08 13:35:02 +0100401 obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700402 offset = args->offset;
403
404 while (remain > 0) {
405 /* Operation in this page
406 *
407 * shmem_page_index = page number within shmem file
408 * shmem_page_offset = offset within page in shmem file
409 * data_page_index = page number in get_user_pages return
410 * data_page_offset = offset with data_page_index page.
411 * page_length = bytes to copy for this page
412 */
413 shmem_page_index = offset / PAGE_SIZE;
414 shmem_page_offset = offset & ~PAGE_MASK;
415 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
416 data_page_offset = data_ptr & ~PAGE_MASK;
417
418 page_length = remain;
419 if ((shmem_page_offset + page_length) > PAGE_SIZE)
420 page_length = PAGE_SIZE - shmem_page_offset;
421 if ((data_page_offset + page_length) > PAGE_SIZE)
422 page_length = PAGE_SIZE - data_page_offset;
423
Eric Anholt280b7132009-03-12 16:56:27 -0700424 if (do_bit17_swizzling) {
Chris Wilson99a03df2010-05-27 14:15:34 +0100425 slow_shmem_bit17_copy(obj_priv->pages[shmem_page_index],
Eric Anholt280b7132009-03-12 16:56:27 -0700426 shmem_page_offset,
Chris Wilson99a03df2010-05-27 14:15:34 +0100427 user_pages[data_page_index],
428 data_page_offset,
429 page_length,
430 1);
431 } else {
432 slow_shmem_copy(user_pages[data_page_index],
433 data_page_offset,
434 obj_priv->pages[shmem_page_index],
435 shmem_page_offset,
436 page_length);
Eric Anholt280b7132009-03-12 16:56:27 -0700437 }
Eric Anholteb014592009-03-10 11:44:52 -0700438
439 remain -= page_length;
440 data_ptr += page_length;
441 offset += page_length;
442 }
443
444fail_put_pages:
445 i915_gem_object_put_pages(obj);
446fail_unlock:
447 mutex_unlock(&dev->struct_mutex);
448fail_put_user_pages:
449 for (i = 0; i < pinned_pages; i++) {
450 SetPageDirty(user_pages[i]);
451 page_cache_release(user_pages[i]);
452 }
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700453 drm_free_large(user_pages);
Eric Anholteb014592009-03-10 11:44:52 -0700454
455 return ret;
456}
457
Eric Anholt673a3942008-07-30 12:06:12 -0700458/**
459 * Reads data from the object referenced by handle.
460 *
461 * On error, the contents of *data are undefined.
462 */
463int
464i915_gem_pread_ioctl(struct drm_device *dev, void *data,
465 struct drm_file *file_priv)
466{
467 struct drm_i915_gem_pread *args = data;
468 struct drm_gem_object *obj;
469 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -0700470 int ret;
471
472 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
473 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100474 return -ENOENT;
Daniel Vetter23010e42010-03-08 13:35:02 +0100475 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700476
477 /* Bounds check source.
478 *
479 * XXX: This could use review for overflow issues...
480 */
481 if (args->offset > obj->size || args->size > obj->size ||
482 args->offset + args->size > obj->size) {
Luca Barbieribc9025b2010-02-09 05:49:12 +0000483 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700484 return -EINVAL;
485 }
486
Eric Anholt280b7132009-03-12 16:56:27 -0700487 if (i915_gem_object_needs_bit17_swizzle(obj)) {
Eric Anholteb014592009-03-10 11:44:52 -0700488 ret = i915_gem_shmem_pread_slow(dev, obj, args, file_priv);
Eric Anholt280b7132009-03-12 16:56:27 -0700489 } else {
490 ret = i915_gem_shmem_pread_fast(dev, obj, args, file_priv);
491 if (ret != 0)
492 ret = i915_gem_shmem_pread_slow(dev, obj, args,
493 file_priv);
494 }
Eric Anholt673a3942008-07-30 12:06:12 -0700495
Luca Barbieribc9025b2010-02-09 05:49:12 +0000496 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700497
Eric Anholteb014592009-03-10 11:44:52 -0700498 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700499}
500
Keith Packard0839ccb2008-10-30 19:38:48 -0700501/* This is the fast write path which cannot handle
502 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700503 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700504
Keith Packard0839ccb2008-10-30 19:38:48 -0700505static inline int
506fast_user_write(struct io_mapping *mapping,
507 loff_t page_base, int page_offset,
508 char __user *user_data,
509 int length)
510{
511 char *vaddr_atomic;
512 unsigned long unwritten;
513
Chris Wilsonfca3ec02010-08-04 14:34:24 +0100514 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base, KM_USER0);
Keith Packard0839ccb2008-10-30 19:38:48 -0700515 unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
516 user_data, length);
Chris Wilsonfca3ec02010-08-04 14:34:24 +0100517 io_mapping_unmap_atomic(vaddr_atomic, KM_USER0);
Keith Packard0839ccb2008-10-30 19:38:48 -0700518 if (unwritten)
519 return -EFAULT;
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700520 return 0;
Keith Packard0839ccb2008-10-30 19:38:48 -0700521}
522
523/* Here's the write path which can sleep for
524 * page faults
525 */
526
Chris Wilsonab34c222010-05-27 14:15:35 +0100527static inline void
Eric Anholt3de09aa2009-03-09 09:42:23 -0700528slow_kernel_write(struct io_mapping *mapping,
529 loff_t gtt_base, int gtt_offset,
530 struct page *user_page, int user_offset,
531 int length)
Keith Packard0839ccb2008-10-30 19:38:48 -0700532{
Chris Wilsonab34c222010-05-27 14:15:35 +0100533 char __iomem *dst_vaddr;
534 char *src_vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -0700535
Chris Wilsonab34c222010-05-27 14:15:35 +0100536 dst_vaddr = io_mapping_map_wc(mapping, gtt_base);
537 src_vaddr = kmap(user_page);
538
539 memcpy_toio(dst_vaddr + gtt_offset,
540 src_vaddr + user_offset,
541 length);
542
543 kunmap(user_page);
544 io_mapping_unmap(dst_vaddr);
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700545}
546
Eric Anholt40123c12009-03-09 13:42:30 -0700547static inline int
548fast_shmem_write(struct page **pages,
549 loff_t page_base, int page_offset,
550 char __user *data,
551 int length)
552{
553 char __iomem *vaddr;
Dave Airlied0088772009-03-28 20:29:48 -0400554 unsigned long unwritten;
Eric Anholt40123c12009-03-09 13:42:30 -0700555
556 vaddr = kmap_atomic(pages[page_base >> PAGE_SHIFT], KM_USER0);
557 if (vaddr == NULL)
558 return -ENOMEM;
Dave Airlied0088772009-03-28 20:29:48 -0400559 unwritten = __copy_from_user_inatomic(vaddr + page_offset, data, length);
Eric Anholt40123c12009-03-09 13:42:30 -0700560 kunmap_atomic(vaddr, KM_USER0);
561
Dave Airlied0088772009-03-28 20:29:48 -0400562 if (unwritten)
563 return -EFAULT;
Eric Anholt40123c12009-03-09 13:42:30 -0700564 return 0;
565}
566
Eric Anholt3de09aa2009-03-09 09:42:23 -0700567/**
568 * This is the fast pwrite path, where we copy the data directly from the
569 * user into the GTT, uncached.
570 */
Eric Anholt673a3942008-07-30 12:06:12 -0700571static int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700572i915_gem_gtt_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
573 struct drm_i915_gem_pwrite *args,
574 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700575{
Daniel Vetter23010e42010-03-08 13:35:02 +0100576 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Keith Packard0839ccb2008-10-30 19:38:48 -0700577 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700578 ssize_t remain;
Keith Packard0839ccb2008-10-30 19:38:48 -0700579 loff_t offset, page_base;
Eric Anholt673a3942008-07-30 12:06:12 -0700580 char __user *user_data;
Keith Packard0839ccb2008-10-30 19:38:48 -0700581 int page_offset, page_length;
582 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700583
584 user_data = (char __user *) (uintptr_t) args->data_ptr;
585 remain = args->size;
586 if (!access_ok(VERIFY_READ, user_data, remain))
587 return -EFAULT;
588
589
590 mutex_lock(&dev->struct_mutex);
591 ret = i915_gem_object_pin(obj, 0);
592 if (ret) {
593 mutex_unlock(&dev->struct_mutex);
594 return ret;
595 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800596 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
Eric Anholt673a3942008-07-30 12:06:12 -0700597 if (ret)
598 goto fail;
599
Daniel Vetter23010e42010-03-08 13:35:02 +0100600 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700601 offset = obj_priv->gtt_offset + args->offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700602
603 while (remain > 0) {
604 /* Operation in this page
605 *
Keith Packard0839ccb2008-10-30 19:38:48 -0700606 * page_base = page offset within aperture
607 * page_offset = offset within page
608 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -0700609 */
Keith Packard0839ccb2008-10-30 19:38:48 -0700610 page_base = (offset & ~(PAGE_SIZE-1));
611 page_offset = offset & (PAGE_SIZE-1);
612 page_length = remain;
613 if ((page_offset + remain) > PAGE_SIZE)
614 page_length = PAGE_SIZE - page_offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700615
Keith Packard0839ccb2008-10-30 19:38:48 -0700616 ret = fast_user_write (dev_priv->mm.gtt_mapping, page_base,
617 page_offset, user_data, page_length);
Eric Anholt673a3942008-07-30 12:06:12 -0700618
Keith Packard0839ccb2008-10-30 19:38:48 -0700619 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -0700620 * source page isn't available. Return the error and we'll
621 * retry in the slow path.
Keith Packard0839ccb2008-10-30 19:38:48 -0700622 */
Eric Anholt3de09aa2009-03-09 09:42:23 -0700623 if (ret)
624 goto fail;
Eric Anholt673a3942008-07-30 12:06:12 -0700625
Keith Packard0839ccb2008-10-30 19:38:48 -0700626 remain -= page_length;
627 user_data += page_length;
628 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700629 }
Eric Anholt673a3942008-07-30 12:06:12 -0700630
631fail:
632 i915_gem_object_unpin(obj);
633 mutex_unlock(&dev->struct_mutex);
634
635 return ret;
636}
637
Eric Anholt3de09aa2009-03-09 09:42:23 -0700638/**
639 * This is the fallback GTT pwrite path, which uses get_user_pages to pin
640 * the memory and maps it using kmap_atomic for copying.
641 *
642 * This code resulted in x11perf -rgb10text consuming about 10% more CPU
643 * than using i915_gem_gtt_pwrite_fast on a G45 (32-bit).
644 */
Eric Anholt3043c602008-10-02 12:24:47 -0700645static int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700646i915_gem_gtt_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
647 struct drm_i915_gem_pwrite *args,
648 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700649{
Daniel Vetter23010e42010-03-08 13:35:02 +0100650 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700651 drm_i915_private_t *dev_priv = dev->dev_private;
652 ssize_t remain;
653 loff_t gtt_page_base, offset;
654 loff_t first_data_page, last_data_page, num_pages;
655 loff_t pinned_pages, i;
656 struct page **user_pages;
657 struct mm_struct *mm = current->mm;
658 int gtt_page_offset, data_page_offset, data_page_index, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700659 int ret;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700660 uint64_t data_ptr = args->data_ptr;
661
662 remain = args->size;
663
664 /* Pin the user pages containing the data. We can't fault while
665 * holding the struct mutex, and all of the pwrite implementations
666 * want to hold it while dereferencing the user data.
667 */
668 first_data_page = data_ptr / PAGE_SIZE;
669 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
670 num_pages = last_data_page - first_data_page + 1;
671
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700672 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
Eric Anholt3de09aa2009-03-09 09:42:23 -0700673 if (user_pages == NULL)
674 return -ENOMEM;
675
676 down_read(&mm->mmap_sem);
677 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
678 num_pages, 0, 0, user_pages, NULL);
679 up_read(&mm->mmap_sem);
680 if (pinned_pages < num_pages) {
681 ret = -EFAULT;
682 goto out_unpin_pages;
683 }
684
685 mutex_lock(&dev->struct_mutex);
686 ret = i915_gem_object_pin(obj, 0);
687 if (ret)
688 goto out_unlock;
689
690 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
691 if (ret)
692 goto out_unpin_object;
693
Daniel Vetter23010e42010-03-08 13:35:02 +0100694 obj_priv = to_intel_bo(obj);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700695 offset = obj_priv->gtt_offset + args->offset;
696
697 while (remain > 0) {
698 /* Operation in this page
699 *
700 * gtt_page_base = page offset within aperture
701 * gtt_page_offset = offset within page in aperture
702 * data_page_index = page number in get_user_pages return
703 * data_page_offset = offset with data_page_index page.
704 * page_length = bytes to copy for this page
705 */
706 gtt_page_base = offset & PAGE_MASK;
707 gtt_page_offset = offset & ~PAGE_MASK;
708 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
709 data_page_offset = data_ptr & ~PAGE_MASK;
710
711 page_length = remain;
712 if ((gtt_page_offset + page_length) > PAGE_SIZE)
713 page_length = PAGE_SIZE - gtt_page_offset;
714 if ((data_page_offset + page_length) > PAGE_SIZE)
715 page_length = PAGE_SIZE - data_page_offset;
716
Chris Wilsonab34c222010-05-27 14:15:35 +0100717 slow_kernel_write(dev_priv->mm.gtt_mapping,
718 gtt_page_base, gtt_page_offset,
719 user_pages[data_page_index],
720 data_page_offset,
721 page_length);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700722
723 remain -= page_length;
724 offset += page_length;
725 data_ptr += page_length;
726 }
727
728out_unpin_object:
729 i915_gem_object_unpin(obj);
730out_unlock:
731 mutex_unlock(&dev->struct_mutex);
732out_unpin_pages:
733 for (i = 0; i < pinned_pages; i++)
734 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700735 drm_free_large(user_pages);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700736
737 return ret;
738}
739
Eric Anholt40123c12009-03-09 13:42:30 -0700740/**
741 * This is the fast shmem pwrite path, which attempts to directly
742 * copy_from_user into the kmapped pages backing the object.
743 */
Eric Anholt673a3942008-07-30 12:06:12 -0700744static int
Eric Anholt40123c12009-03-09 13:42:30 -0700745i915_gem_shmem_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
746 struct drm_i915_gem_pwrite *args,
747 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700748{
Daniel Vetter23010e42010-03-08 13:35:02 +0100749 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700750 ssize_t remain;
751 loff_t offset, page_base;
752 char __user *user_data;
753 int page_offset, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700754 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -0700755
756 user_data = (char __user *) (uintptr_t) args->data_ptr;
757 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700758
759 mutex_lock(&dev->struct_mutex);
760
Chris Wilson4bdadb92010-01-27 13:36:32 +0000761 ret = i915_gem_object_get_pages(obj, 0);
Eric Anholt40123c12009-03-09 13:42:30 -0700762 if (ret != 0)
763 goto fail_unlock;
764
Eric Anholte47c68e2008-11-14 13:35:19 -0800765 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Eric Anholt40123c12009-03-09 13:42:30 -0700766 if (ret != 0)
767 goto fail_put_pages;
Eric Anholt673a3942008-07-30 12:06:12 -0700768
Daniel Vetter23010e42010-03-08 13:35:02 +0100769 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700770 offset = args->offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700771 obj_priv->dirty = 1;
Eric Anholt673a3942008-07-30 12:06:12 -0700772
Eric Anholt40123c12009-03-09 13:42:30 -0700773 while (remain > 0) {
774 /* Operation in this page
775 *
776 * page_base = page offset within aperture
777 * page_offset = offset within page
778 * page_length = bytes to copy for this page
779 */
780 page_base = (offset & ~(PAGE_SIZE-1));
781 page_offset = offset & (PAGE_SIZE-1);
782 page_length = remain;
783 if ((page_offset + remain) > PAGE_SIZE)
784 page_length = PAGE_SIZE - page_offset;
785
786 ret = fast_shmem_write(obj_priv->pages,
787 page_base, page_offset,
788 user_data, page_length);
789 if (ret)
790 goto fail_put_pages;
791
792 remain -= page_length;
793 user_data += page_length;
794 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700795 }
796
Eric Anholt40123c12009-03-09 13:42:30 -0700797fail_put_pages:
798 i915_gem_object_put_pages(obj);
799fail_unlock:
Eric Anholt673a3942008-07-30 12:06:12 -0700800 mutex_unlock(&dev->struct_mutex);
801
Eric Anholt40123c12009-03-09 13:42:30 -0700802 return ret;
803}
804
805/**
806 * This is the fallback shmem pwrite path, which uses get_user_pages to pin
807 * the memory and maps it using kmap_atomic for copying.
808 *
809 * This avoids taking mmap_sem for faulting on the user's address while the
810 * struct_mutex is held.
811 */
812static int
813i915_gem_shmem_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
814 struct drm_i915_gem_pwrite *args,
815 struct drm_file *file_priv)
816{
Daniel Vetter23010e42010-03-08 13:35:02 +0100817 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700818 struct mm_struct *mm = current->mm;
819 struct page **user_pages;
820 ssize_t remain;
821 loff_t offset, pinned_pages, i;
822 loff_t first_data_page, last_data_page, num_pages;
823 int shmem_page_index, shmem_page_offset;
824 int data_page_index, data_page_offset;
825 int page_length;
826 int ret;
827 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700828 int do_bit17_swizzling;
Eric Anholt40123c12009-03-09 13:42:30 -0700829
830 remain = args->size;
831
832 /* Pin the user pages containing the data. We can't fault while
833 * holding the struct mutex, and all of the pwrite implementations
834 * want to hold it while dereferencing the user data.
835 */
836 first_data_page = data_ptr / PAGE_SIZE;
837 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
838 num_pages = last_data_page - first_data_page + 1;
839
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700840 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
Eric Anholt40123c12009-03-09 13:42:30 -0700841 if (user_pages == NULL)
842 return -ENOMEM;
843
844 down_read(&mm->mmap_sem);
845 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
846 num_pages, 0, 0, user_pages, NULL);
847 up_read(&mm->mmap_sem);
848 if (pinned_pages < num_pages) {
849 ret = -EFAULT;
850 goto fail_put_user_pages;
851 }
852
Eric Anholt280b7132009-03-12 16:56:27 -0700853 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
854
Eric Anholt40123c12009-03-09 13:42:30 -0700855 mutex_lock(&dev->struct_mutex);
856
Chris Wilson07f73f62009-09-14 16:50:30 +0100857 ret = i915_gem_object_get_pages_or_evict(obj);
858 if (ret)
Eric Anholt40123c12009-03-09 13:42:30 -0700859 goto fail_unlock;
860
861 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
862 if (ret != 0)
863 goto fail_put_pages;
864
Daniel Vetter23010e42010-03-08 13:35:02 +0100865 obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700866 offset = args->offset;
867 obj_priv->dirty = 1;
868
869 while (remain > 0) {
870 /* Operation in this page
871 *
872 * shmem_page_index = page number within shmem file
873 * shmem_page_offset = offset within page in shmem file
874 * data_page_index = page number in get_user_pages return
875 * data_page_offset = offset with data_page_index page.
876 * page_length = bytes to copy for this page
877 */
878 shmem_page_index = offset / PAGE_SIZE;
879 shmem_page_offset = offset & ~PAGE_MASK;
880 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
881 data_page_offset = data_ptr & ~PAGE_MASK;
882
883 page_length = remain;
884 if ((shmem_page_offset + page_length) > PAGE_SIZE)
885 page_length = PAGE_SIZE - shmem_page_offset;
886 if ((data_page_offset + page_length) > PAGE_SIZE)
887 page_length = PAGE_SIZE - data_page_offset;
888
Eric Anholt280b7132009-03-12 16:56:27 -0700889 if (do_bit17_swizzling) {
Chris Wilson99a03df2010-05-27 14:15:34 +0100890 slow_shmem_bit17_copy(obj_priv->pages[shmem_page_index],
Eric Anholt280b7132009-03-12 16:56:27 -0700891 shmem_page_offset,
892 user_pages[data_page_index],
893 data_page_offset,
Chris Wilson99a03df2010-05-27 14:15:34 +0100894 page_length,
895 0);
896 } else {
897 slow_shmem_copy(obj_priv->pages[shmem_page_index],
898 shmem_page_offset,
899 user_pages[data_page_index],
900 data_page_offset,
901 page_length);
Eric Anholt280b7132009-03-12 16:56:27 -0700902 }
Eric Anholt40123c12009-03-09 13:42:30 -0700903
904 remain -= page_length;
905 data_ptr += page_length;
906 offset += page_length;
907 }
908
909fail_put_pages:
910 i915_gem_object_put_pages(obj);
911fail_unlock:
912 mutex_unlock(&dev->struct_mutex);
913fail_put_user_pages:
914 for (i = 0; i < pinned_pages; i++)
915 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700916 drm_free_large(user_pages);
Eric Anholt40123c12009-03-09 13:42:30 -0700917
918 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700919}
920
921/**
922 * Writes data to the object referenced by handle.
923 *
924 * On error, the contents of the buffer that were to be modified are undefined.
925 */
926int
927i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
928 struct drm_file *file_priv)
929{
930 struct drm_i915_gem_pwrite *args = data;
931 struct drm_gem_object *obj;
932 struct drm_i915_gem_object *obj_priv;
933 int ret = 0;
934
935 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
936 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100937 return -ENOENT;
Daniel Vetter23010e42010-03-08 13:35:02 +0100938 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700939
940 /* Bounds check destination.
941 *
942 * XXX: This could use review for overflow issues...
943 */
944 if (args->offset > obj->size || args->size > obj->size ||
945 args->offset + args->size > obj->size) {
Luca Barbieribc9025b2010-02-09 05:49:12 +0000946 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700947 return -EINVAL;
948 }
949
950 /* We can only do the GTT pwrite on untiled buffers, as otherwise
951 * it would end up going through the fenced access, and we'll get
952 * different detiling behavior between reading and writing.
953 * pread/pwrite currently are reading and writing from the CPU
954 * perspective, requiring manual detiling by the client.
955 */
Dave Airlie71acb5e2008-12-30 20:31:46 +1000956 if (obj_priv->phys_obj)
957 ret = i915_gem_phys_pwrite(dev, obj, args, file_priv);
958 else if (obj_priv->tiling_mode == I915_TILING_NONE &&
Chris Wilson9b8c4a02010-05-27 14:21:01 +0100959 dev->gtt_total != 0 &&
960 obj->write_domain != I915_GEM_DOMAIN_CPU) {
Eric Anholt3de09aa2009-03-09 09:42:23 -0700961 ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file_priv);
962 if (ret == -EFAULT) {
963 ret = i915_gem_gtt_pwrite_slow(dev, obj, args,
964 file_priv);
965 }
Eric Anholt280b7132009-03-12 16:56:27 -0700966 } else if (i915_gem_object_needs_bit17_swizzle(obj)) {
967 ret = i915_gem_shmem_pwrite_slow(dev, obj, args, file_priv);
Eric Anholt40123c12009-03-09 13:42:30 -0700968 } else {
969 ret = i915_gem_shmem_pwrite_fast(dev, obj, args, file_priv);
970 if (ret == -EFAULT) {
971 ret = i915_gem_shmem_pwrite_slow(dev, obj, args,
972 file_priv);
973 }
974 }
Eric Anholt673a3942008-07-30 12:06:12 -0700975
976#if WATCH_PWRITE
977 if (ret)
978 DRM_INFO("pwrite failed %d\n", ret);
979#endif
980
Luca Barbieribc9025b2010-02-09 05:49:12 +0000981 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700982
983 return ret;
984}
985
986/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800987 * Called when user space prepares to use an object with the CPU, either
988 * through the mmap ioctl's mapping or a GTT mapping.
Eric Anholt673a3942008-07-30 12:06:12 -0700989 */
990int
991i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
992 struct drm_file *file_priv)
993{
Eric Anholta09ba7f2009-08-29 12:49:51 -0700994 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700995 struct drm_i915_gem_set_domain *args = data;
996 struct drm_gem_object *obj;
Jesse Barnes652c3932009-08-17 13:31:43 -0700997 struct drm_i915_gem_object *obj_priv;
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800998 uint32_t read_domains = args->read_domains;
999 uint32_t write_domain = args->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07001000 int ret;
1001
1002 if (!(dev->driver->driver_features & DRIVER_GEM))
1003 return -ENODEV;
1004
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001005 /* Only handle setting domains to types used by the CPU. */
Chris Wilson21d509e2009-06-06 09:46:02 +01001006 if (write_domain & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001007 return -EINVAL;
1008
Chris Wilson21d509e2009-06-06 09:46:02 +01001009 if (read_domains & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001010 return -EINVAL;
1011
1012 /* Having something in the write domain implies it's in the read
1013 * domain, and only that read domain. Enforce that in the request.
1014 */
1015 if (write_domain != 0 && read_domains != write_domain)
1016 return -EINVAL;
1017
Eric Anholt673a3942008-07-30 12:06:12 -07001018 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1019 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001020 return -ENOENT;
Daniel Vetter23010e42010-03-08 13:35:02 +01001021 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001022
1023 mutex_lock(&dev->struct_mutex);
Jesse Barnes652c3932009-08-17 13:31:43 -07001024
1025 intel_mark_busy(dev, obj);
1026
Eric Anholt673a3942008-07-30 12:06:12 -07001027#if WATCH_BUF
Krzysztof Halasacfd43c02009-06-20 00:31:28 +02001028 DRM_INFO("set_domain_ioctl %p(%zd), %08x %08x\n",
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001029 obj, obj->size, read_domains, write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07001030#endif
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001031 if (read_domains & I915_GEM_DOMAIN_GTT) {
1032 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
Eric Anholt02354392008-11-26 13:58:13 -08001033
Eric Anholta09ba7f2009-08-29 12:49:51 -07001034 /* Update the LRU on the fence for the CPU access that's
1035 * about to occur.
1036 */
1037 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001038 struct drm_i915_fence_reg *reg =
1039 &dev_priv->fence_regs[obj_priv->fence_reg];
1040 list_move_tail(&reg->lru_list,
Eric Anholta09ba7f2009-08-29 12:49:51 -07001041 &dev_priv->mm.fence_list);
1042 }
1043
Eric Anholt02354392008-11-26 13:58:13 -08001044 /* Silently promote "you're not bound, there was nothing to do"
1045 * to success, since the client was just asking us to
1046 * make sure everything was done.
1047 */
1048 if (ret == -EINVAL)
1049 ret = 0;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001050 } else {
Eric Anholte47c68e2008-11-14 13:35:19 -08001051 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001052 }
1053
Chris Wilson7d1c4802010-08-07 21:45:03 +01001054
1055 /* Maintain LRU order of "inactive" objects */
1056 if (ret == 0 && i915_gem_object_is_inactive(obj_priv))
1057 list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
1058
Eric Anholt673a3942008-07-30 12:06:12 -07001059 drm_gem_object_unreference(obj);
1060 mutex_unlock(&dev->struct_mutex);
1061 return ret;
1062}
1063
1064/**
1065 * Called when user space has done writes to this buffer
1066 */
1067int
1068i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
1069 struct drm_file *file_priv)
1070{
1071 struct drm_i915_gem_sw_finish *args = data;
1072 struct drm_gem_object *obj;
1073 struct drm_i915_gem_object *obj_priv;
1074 int ret = 0;
1075
1076 if (!(dev->driver->driver_features & DRIVER_GEM))
1077 return -ENODEV;
1078
1079 mutex_lock(&dev->struct_mutex);
1080 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1081 if (obj == NULL) {
1082 mutex_unlock(&dev->struct_mutex);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001083 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001084 }
1085
1086#if WATCH_BUF
Krzysztof Halasacfd43c02009-06-20 00:31:28 +02001087 DRM_INFO("%s: sw_finish %d (%p %zd)\n",
Eric Anholt673a3942008-07-30 12:06:12 -07001088 __func__, args->handle, obj, obj->size);
1089#endif
Daniel Vetter23010e42010-03-08 13:35:02 +01001090 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001091
1092 /* Pinned buffers may be scanout, so flush the cache */
Eric Anholte47c68e2008-11-14 13:35:19 -08001093 if (obj_priv->pin_count)
1094 i915_gem_object_flush_cpu_write_domain(obj);
1095
Eric Anholt673a3942008-07-30 12:06:12 -07001096 drm_gem_object_unreference(obj);
1097 mutex_unlock(&dev->struct_mutex);
1098 return ret;
1099}
1100
1101/**
1102 * Maps the contents of an object, returning the address it is mapped
1103 * into.
1104 *
1105 * While the mapping holds a reference on the contents of the object, it doesn't
1106 * imply a ref on the object itself.
1107 */
1108int
1109i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
1110 struct drm_file *file_priv)
1111{
1112 struct drm_i915_gem_mmap *args = data;
1113 struct drm_gem_object *obj;
1114 loff_t offset;
1115 unsigned long addr;
1116
1117 if (!(dev->driver->driver_features & DRIVER_GEM))
1118 return -ENODEV;
1119
1120 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1121 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001122 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001123
1124 offset = args->offset;
1125
1126 down_write(&current->mm->mmap_sem);
1127 addr = do_mmap(obj->filp, 0, args->size,
1128 PROT_READ | PROT_WRITE, MAP_SHARED,
1129 args->offset);
1130 up_write(&current->mm->mmap_sem);
Luca Barbieribc9025b2010-02-09 05:49:12 +00001131 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001132 if (IS_ERR((void *)addr))
1133 return addr;
1134
1135 args->addr_ptr = (uint64_t) addr;
1136
1137 return 0;
1138}
1139
Jesse Barnesde151cf2008-11-12 10:03:55 -08001140/**
1141 * i915_gem_fault - fault a page into the GTT
1142 * vma: VMA in question
1143 * vmf: fault info
1144 *
1145 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1146 * from userspace. The fault handler takes care of binding the object to
1147 * the GTT (if needed), allocating and programming a fence register (again,
1148 * only if needed based on whether the old reg is still valid or the object
1149 * is tiled) and inserting a new PTE into the faulting process.
1150 *
1151 * Note that the faulting process may involve evicting existing objects
1152 * from the GTT and/or fence registers to make room. So performance may
1153 * suffer if the GTT working set is large or there are few fence registers
1154 * left.
1155 */
1156int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1157{
1158 struct drm_gem_object *obj = vma->vm_private_data;
1159 struct drm_device *dev = obj->dev;
Chris Wilson7d1c4802010-08-07 21:45:03 +01001160 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001161 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001162 pgoff_t page_offset;
1163 unsigned long pfn;
1164 int ret = 0;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001165 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001166
1167 /* We don't use vmf->pgoff since that has the fake offset */
1168 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1169 PAGE_SHIFT;
1170
1171 /* Now bind it into the GTT if needed */
1172 mutex_lock(&dev->struct_mutex);
1173 if (!obj_priv->gtt_space) {
Chris Wilsone67b8ce2009-09-14 16:50:26 +01001174 ret = i915_gem_object_bind_to_gtt(obj, 0);
Chris Wilsonc7150892009-09-23 00:43:56 +01001175 if (ret)
1176 goto unlock;
Kristian Høgsberg07f4f3e2009-05-27 14:37:28 -04001177
Jesse Barnesde151cf2008-11-12 10:03:55 -08001178 ret = i915_gem_object_set_to_gtt_domain(obj, write);
Chris Wilsonc7150892009-09-23 00:43:56 +01001179 if (ret)
1180 goto unlock;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001181 }
1182
1183 /* Need a new fence register? */
Eric Anholta09ba7f2009-08-29 12:49:51 -07001184 if (obj_priv->tiling_mode != I915_TILING_NONE) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01001185 ret = i915_gem_object_get_fence_reg(obj, true);
Chris Wilsonc7150892009-09-23 00:43:56 +01001186 if (ret)
1187 goto unlock;
Eric Anholtd9ddcb92009-01-27 10:33:49 -08001188 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001189
Chris Wilson7d1c4802010-08-07 21:45:03 +01001190 if (i915_gem_object_is_inactive(obj_priv))
1191 list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
1192
Jesse Barnesde151cf2008-11-12 10:03:55 -08001193 pfn = ((dev->agp->base + obj_priv->gtt_offset) >> PAGE_SHIFT) +
1194 page_offset;
1195
1196 /* Finally, remap it using the new GTT offset */
1197 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
Chris Wilsonc7150892009-09-23 00:43:56 +01001198unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001199 mutex_unlock(&dev->struct_mutex);
1200
1201 switch (ret) {
Chris Wilsonc7150892009-09-23 00:43:56 +01001202 case 0:
1203 case -ERESTARTSYS:
1204 return VM_FAULT_NOPAGE;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001205 case -ENOMEM:
1206 case -EAGAIN:
1207 return VM_FAULT_OOM;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001208 default:
Chris Wilsonc7150892009-09-23 00:43:56 +01001209 return VM_FAULT_SIGBUS;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001210 }
1211}
1212
1213/**
1214 * i915_gem_create_mmap_offset - create a fake mmap offset for an object
1215 * @obj: obj in question
1216 *
1217 * GEM memory mapping works by handing back to userspace a fake mmap offset
1218 * it can use in a subsequent mmap(2) call. The DRM core code then looks
1219 * up the object based on the offset and sets up the various memory mapping
1220 * structures.
1221 *
1222 * This routine allocates and attaches a fake offset for @obj.
1223 */
1224static int
1225i915_gem_create_mmap_offset(struct drm_gem_object *obj)
1226{
1227 struct drm_device *dev = obj->dev;
1228 struct drm_gem_mm *mm = dev->mm_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001229 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001230 struct drm_map_list *list;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +11001231 struct drm_local_map *map;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001232 int ret = 0;
1233
1234 /* Set the object up for mmap'ing */
1235 list = &obj->map_list;
Eric Anholt9a298b22009-03-24 12:23:04 -07001236 list->map = kzalloc(sizeof(struct drm_map_list), GFP_KERNEL);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001237 if (!list->map)
1238 return -ENOMEM;
1239
1240 map = list->map;
1241 map->type = _DRM_GEM;
1242 map->size = obj->size;
1243 map->handle = obj;
1244
1245 /* Get a DRM GEM mmap offset allocated... */
1246 list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
1247 obj->size / PAGE_SIZE, 0, 0);
1248 if (!list->file_offset_node) {
1249 DRM_ERROR("failed to allocate offset for bo %d\n", obj->name);
1250 ret = -ENOMEM;
1251 goto out_free_list;
1252 }
1253
1254 list->file_offset_node = drm_mm_get_block(list->file_offset_node,
1255 obj->size / PAGE_SIZE, 0);
1256 if (!list->file_offset_node) {
1257 ret = -ENOMEM;
1258 goto out_free_list;
1259 }
1260
1261 list->hash.key = list->file_offset_node->start;
1262 if (drm_ht_insert_item(&mm->offset_hash, &list->hash)) {
1263 DRM_ERROR("failed to add to map hash\n");
Chris Wilson5618ca62009-12-02 15:15:30 +00001264 ret = -ENOMEM;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001265 goto out_free_mm;
1266 }
1267
1268 /* By now we should be all set, any drm_mmap request on the offset
1269 * below will get to our mmap & fault handler */
1270 obj_priv->mmap_offset = ((uint64_t) list->hash.key) << PAGE_SHIFT;
1271
1272 return 0;
1273
1274out_free_mm:
1275 drm_mm_put_block(list->file_offset_node);
1276out_free_list:
Eric Anholt9a298b22009-03-24 12:23:04 -07001277 kfree(list->map);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001278
1279 return ret;
1280}
1281
Chris Wilson901782b2009-07-10 08:18:50 +01001282/**
1283 * i915_gem_release_mmap - remove physical page mappings
1284 * @obj: obj in question
1285 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001286 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01001287 * relinquish ownership of the pages back to the system.
1288 *
1289 * It is vital that we remove the page mapping if we have mapped a tiled
1290 * object through the GTT and then lose the fence register due to
1291 * resource pressure. Similarly if the object has been moved out of the
1292 * aperture, than pages mapped into userspace must be revoked. Removing the
1293 * mapping will then trigger a page fault on the next user access, allowing
1294 * fixup by i915_gem_fault().
1295 */
Eric Anholtd05ca302009-07-10 13:02:26 -07001296void
Chris Wilson901782b2009-07-10 08:18:50 +01001297i915_gem_release_mmap(struct drm_gem_object *obj)
1298{
1299 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001300 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson901782b2009-07-10 08:18:50 +01001301
1302 if (dev->dev_mapping)
1303 unmap_mapping_range(dev->dev_mapping,
1304 obj_priv->mmap_offset, obj->size, 1);
1305}
1306
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001307static void
1308i915_gem_free_mmap_offset(struct drm_gem_object *obj)
1309{
1310 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001311 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001312 struct drm_gem_mm *mm = dev->mm_private;
1313 struct drm_map_list *list;
1314
1315 list = &obj->map_list;
1316 drm_ht_remove_item(&mm->offset_hash, &list->hash);
1317
1318 if (list->file_offset_node) {
1319 drm_mm_put_block(list->file_offset_node);
1320 list->file_offset_node = NULL;
1321 }
1322
1323 if (list->map) {
Eric Anholt9a298b22009-03-24 12:23:04 -07001324 kfree(list->map);
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001325 list->map = NULL;
1326 }
1327
1328 obj_priv->mmap_offset = 0;
1329}
1330
Jesse Barnesde151cf2008-11-12 10:03:55 -08001331/**
1332 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1333 * @obj: object to check
1334 *
1335 * Return the required GTT alignment for an object, taking into account
1336 * potential fence register mapping if needed.
1337 */
1338static uint32_t
1339i915_gem_get_gtt_alignment(struct drm_gem_object *obj)
1340{
1341 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001342 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001343 int start, i;
1344
1345 /*
1346 * Minimum alignment is 4k (GTT page size), but might be greater
1347 * if a fence register is needed for the object.
1348 */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001349 if (INTEL_INFO(dev)->gen >= 4 || obj_priv->tiling_mode == I915_TILING_NONE)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001350 return 4096;
1351
1352 /*
1353 * Previous chips need to be aligned to the size of the smallest
1354 * fence register that can contain the object.
1355 */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001356 if (INTEL_INFO(dev)->gen == 3)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001357 start = 1024*1024;
1358 else
1359 start = 512*1024;
1360
1361 for (i = start; i < obj->size; i <<= 1)
1362 ;
1363
1364 return i;
1365}
1366
1367/**
1368 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1369 * @dev: DRM device
1370 * @data: GTT mapping ioctl data
1371 * @file_priv: GEM object info
1372 *
1373 * Simply returns the fake offset to userspace so it can mmap it.
1374 * The mmap call will end up in drm_gem_mmap(), which will set things
1375 * up so we can get faults in the handler above.
1376 *
1377 * The fault handler will take care of binding the object into the GTT
1378 * (since it may have been evicted to make room for something), allocating
1379 * a fence register, and mapping the appropriate aperture address into
1380 * userspace.
1381 */
1382int
1383i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
1384 struct drm_file *file_priv)
1385{
1386 struct drm_i915_gem_mmap_gtt *args = data;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001387 struct drm_gem_object *obj;
1388 struct drm_i915_gem_object *obj_priv;
1389 int ret;
1390
1391 if (!(dev->driver->driver_features & DRIVER_GEM))
1392 return -ENODEV;
1393
1394 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1395 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001396 return -ENOENT;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001397
1398 mutex_lock(&dev->struct_mutex);
1399
Daniel Vetter23010e42010-03-08 13:35:02 +01001400 obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001401
Chris Wilsonab182822009-09-22 18:46:17 +01001402 if (obj_priv->madv != I915_MADV_WILLNEED) {
1403 DRM_ERROR("Attempting to mmap a purgeable buffer\n");
1404 drm_gem_object_unreference(obj);
1405 mutex_unlock(&dev->struct_mutex);
1406 return -EINVAL;
1407 }
1408
1409
Jesse Barnesde151cf2008-11-12 10:03:55 -08001410 if (!obj_priv->mmap_offset) {
1411 ret = i915_gem_create_mmap_offset(obj);
Chris Wilson13af1062009-02-11 14:26:31 +00001412 if (ret) {
1413 drm_gem_object_unreference(obj);
1414 mutex_unlock(&dev->struct_mutex);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001415 return ret;
Chris Wilson13af1062009-02-11 14:26:31 +00001416 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001417 }
1418
1419 args->offset = obj_priv->mmap_offset;
1420
Jesse Barnesde151cf2008-11-12 10:03:55 -08001421 /*
1422 * Pull it into the GTT so that we have a page list (makes the
1423 * initial fault faster and any subsequent flushing possible).
1424 */
1425 if (!obj_priv->agp_mem) {
Chris Wilsone67b8ce2009-09-14 16:50:26 +01001426 ret = i915_gem_object_bind_to_gtt(obj, 0);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001427 if (ret) {
1428 drm_gem_object_unreference(obj);
1429 mutex_unlock(&dev->struct_mutex);
1430 return ret;
1431 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001432 }
1433
1434 drm_gem_object_unreference(obj);
1435 mutex_unlock(&dev->struct_mutex);
1436
1437 return 0;
1438}
1439
Ben Gamari6911a9b2009-04-02 11:24:54 -07001440void
Eric Anholt856fa192009-03-19 14:10:50 -07001441i915_gem_object_put_pages(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07001442{
Daniel Vetter23010e42010-03-08 13:35:02 +01001443 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001444 int page_count = obj->size / PAGE_SIZE;
1445 int i;
1446
Eric Anholt856fa192009-03-19 14:10:50 -07001447 BUG_ON(obj_priv->pages_refcount == 0);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001448 BUG_ON(obj_priv->madv == __I915_MADV_PURGED);
Eric Anholt856fa192009-03-19 14:10:50 -07001449
1450 if (--obj_priv->pages_refcount != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07001451 return;
1452
Eric Anholt280b7132009-03-12 16:56:27 -07001453 if (obj_priv->tiling_mode != I915_TILING_NONE)
1454 i915_gem_object_save_bit_17_swizzle(obj);
1455
Chris Wilson3ef94da2009-09-14 16:50:29 +01001456 if (obj_priv->madv == I915_MADV_DONTNEED)
Chris Wilson13a05fd2009-09-20 23:03:19 +01001457 obj_priv->dirty = 0;
Chris Wilson3ef94da2009-09-14 16:50:29 +01001458
1459 for (i = 0; i < page_count; i++) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01001460 if (obj_priv->dirty)
1461 set_page_dirty(obj_priv->pages[i]);
1462
1463 if (obj_priv->madv == I915_MADV_WILLNEED)
Eric Anholt856fa192009-03-19 14:10:50 -07001464 mark_page_accessed(obj_priv->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001465
1466 page_cache_release(obj_priv->pages[i]);
1467 }
Eric Anholt673a3942008-07-30 12:06:12 -07001468 obj_priv->dirty = 0;
1469
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07001470 drm_free_large(obj_priv->pages);
Eric Anholt856fa192009-03-19 14:10:50 -07001471 obj_priv->pages = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001472}
1473
Daniel Vettere35a41d2010-02-11 22:13:59 +01001474static uint32_t
Daniel Vettera6910432010-02-02 17:08:37 +01001475i915_gem_next_request_seqno(struct drm_device *dev,
1476 struct intel_ring_buffer *ring)
Daniel Vettere35a41d2010-02-11 22:13:59 +01001477{
1478 drm_i915_private_t *dev_priv = dev->dev_private;
1479
Daniel Vettera6910432010-02-02 17:08:37 +01001480 ring->outstanding_lazy_request = true;
1481
Daniel Vettere35a41d2010-02-11 22:13:59 +01001482 return dev_priv->next_seqno;
1483}
1484
Eric Anholt673a3942008-07-30 12:06:12 -07001485static void
Daniel Vetter617dbe22010-02-11 22:16:02 +01001486i915_gem_object_move_to_active(struct drm_gem_object *obj,
Zou Nan hai852835f2010-05-21 09:08:56 +08001487 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001488{
1489 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001490 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Daniel Vetter617dbe22010-02-11 22:16:02 +01001491 uint32_t seqno = i915_gem_next_request_seqno(dev, ring);
1492
Zou Nan hai852835f2010-05-21 09:08:56 +08001493 BUG_ON(ring == NULL);
1494 obj_priv->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001495
1496 /* Add a reference if we're newly entering the active list. */
1497 if (!obj_priv->active) {
1498 drm_gem_object_reference(obj);
1499 obj_priv->active = 1;
1500 }
Daniel Vettere35a41d2010-02-11 22:13:59 +01001501
Eric Anholt673a3942008-07-30 12:06:12 -07001502 /* Move from whatever list we were on to the tail of execution. */
Zou Nan hai852835f2010-05-21 09:08:56 +08001503 list_move_tail(&obj_priv->list, &ring->active_list);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001504 obj_priv->last_rendering_seqno = seqno;
Eric Anholt673a3942008-07-30 12:06:12 -07001505}
1506
Eric Anholtce44b0e2008-11-06 16:00:31 -08001507static void
1508i915_gem_object_move_to_flushing(struct drm_gem_object *obj)
1509{
1510 struct drm_device *dev = obj->dev;
1511 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001512 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001513
1514 BUG_ON(!obj_priv->active);
1515 list_move_tail(&obj_priv->list, &dev_priv->mm.flushing_list);
1516 obj_priv->last_rendering_seqno = 0;
1517}
Eric Anholt673a3942008-07-30 12:06:12 -07001518
Chris Wilson963b4832009-09-20 23:03:54 +01001519/* Immediately discard the backing storage */
1520static void
1521i915_gem_object_truncate(struct drm_gem_object *obj)
1522{
Daniel Vetter23010e42010-03-08 13:35:02 +01001523 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001524 struct inode *inode;
Chris Wilson963b4832009-09-20 23:03:54 +01001525
Chris Wilsonae9fed62010-08-07 11:01:30 +01001526 /* Our goal here is to return as much of the memory as
1527 * is possible back to the system as we are called from OOM.
1528 * To do this we must instruct the shmfs to drop all of its
1529 * backing pages, *now*. Here we mirror the actions taken
1530 * when by shmem_delete_inode() to release the backing store.
1531 */
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001532 inode = obj->filp->f_path.dentry->d_inode;
Chris Wilsonae9fed62010-08-07 11:01:30 +01001533 truncate_inode_pages(inode->i_mapping, 0);
1534 if (inode->i_op->truncate_range)
1535 inode->i_op->truncate_range(inode, 0, (loff_t)-1);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001536
1537 obj_priv->madv = __I915_MADV_PURGED;
Chris Wilson963b4832009-09-20 23:03:54 +01001538}
1539
1540static inline int
1541i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj_priv)
1542{
1543 return obj_priv->madv == I915_MADV_DONTNEED;
1544}
1545
Eric Anholt673a3942008-07-30 12:06:12 -07001546static void
1547i915_gem_object_move_to_inactive(struct drm_gem_object *obj)
1548{
1549 struct drm_device *dev = obj->dev;
1550 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001551 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001552
1553 i915_verify_inactive(dev, __FILE__, __LINE__);
1554 if (obj_priv->pin_count != 0)
1555 list_del_init(&obj_priv->list);
1556 else
1557 list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
1558
Daniel Vetter99fcb762010-02-07 16:20:18 +01001559 BUG_ON(!list_empty(&obj_priv->gpu_write_list));
1560
Eric Anholtce44b0e2008-11-06 16:00:31 -08001561 obj_priv->last_rendering_seqno = 0;
Zou Nan hai852835f2010-05-21 09:08:56 +08001562 obj_priv->ring = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001563 if (obj_priv->active) {
1564 obj_priv->active = 0;
1565 drm_gem_object_unreference(obj);
1566 }
1567 i915_verify_inactive(dev, __FILE__, __LINE__);
1568}
1569
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001570void
Daniel Vetter63560392010-02-19 11:51:59 +01001571i915_gem_process_flushing_list(struct drm_device *dev,
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001572 uint32_t flush_domains,
Zou Nan hai852835f2010-05-21 09:08:56 +08001573 struct intel_ring_buffer *ring)
Daniel Vetter63560392010-02-19 11:51:59 +01001574{
1575 drm_i915_private_t *dev_priv = dev->dev_private;
1576 struct drm_i915_gem_object *obj_priv, *next;
1577
1578 list_for_each_entry_safe(obj_priv, next,
1579 &dev_priv->mm.gpu_write_list,
1580 gpu_write_list) {
Daniel Vettera8089e82010-04-09 19:05:09 +00001581 struct drm_gem_object *obj = &obj_priv->base;
Daniel Vetter63560392010-02-19 11:51:59 +01001582
Chris Wilson2b6efaa2010-09-14 17:04:02 +01001583 if (obj->write_domain & flush_domains &&
1584 obj_priv->ring == ring) {
Daniel Vetter63560392010-02-19 11:51:59 +01001585 uint32_t old_write_domain = obj->write_domain;
1586
1587 obj->write_domain = 0;
1588 list_del_init(&obj_priv->gpu_write_list);
Daniel Vetter617dbe22010-02-11 22:16:02 +01001589 i915_gem_object_move_to_active(obj, ring);
Daniel Vetter63560392010-02-19 11:51:59 +01001590
1591 /* update the fence lru list */
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001592 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
1593 struct drm_i915_fence_reg *reg =
1594 &dev_priv->fence_regs[obj_priv->fence_reg];
1595 list_move_tail(&reg->lru_list,
Daniel Vetter63560392010-02-19 11:51:59 +01001596 &dev_priv->mm.fence_list);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001597 }
Daniel Vetter63560392010-02-19 11:51:59 +01001598
1599 trace_i915_gem_object_change_domain(obj,
1600 obj->read_domains,
1601 old_write_domain);
1602 }
1603 }
1604}
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001605
Daniel Vetter5a5a0c62009-09-15 22:57:36 +02001606uint32_t
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001607i915_add_request(struct drm_device *dev,
1608 struct drm_file *file_priv,
Chris Wilson8dc5d142010-08-12 12:36:12 +01001609 struct drm_i915_gem_request *request,
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001610 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001611{
1612 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtb9624422009-06-03 07:27:35 +00001613 struct drm_i915_file_private *i915_file_priv = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001614 uint32_t seqno;
1615 int was_empty;
Eric Anholt673a3942008-07-30 12:06:12 -07001616
Eric Anholtb9624422009-06-03 07:27:35 +00001617 if (file_priv != NULL)
1618 i915_file_priv = file_priv->driver_priv;
1619
Chris Wilson8dc5d142010-08-12 12:36:12 +01001620 if (request == NULL) {
1621 request = kzalloc(sizeof(*request), GFP_KERNEL);
1622 if (request == NULL)
1623 return 0;
1624 }
Eric Anholt673a3942008-07-30 12:06:12 -07001625
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001626 seqno = ring->add_request(dev, ring, file_priv, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07001627
1628 request->seqno = seqno;
Zou Nan hai852835f2010-05-21 09:08:56 +08001629 request->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001630 request->emitted_jiffies = jiffies;
Zou Nan hai852835f2010-05-21 09:08:56 +08001631 was_empty = list_empty(&ring->request_list);
1632 list_add_tail(&request->list, &ring->request_list);
1633
Eric Anholtb9624422009-06-03 07:27:35 +00001634 if (i915_file_priv) {
1635 list_add_tail(&request->client_list,
1636 &i915_file_priv->mm.request_list);
1637 } else {
1638 INIT_LIST_HEAD(&request->client_list);
1639 }
Eric Anholt673a3942008-07-30 12:06:12 -07001640
Ben Gamarif65d9422009-09-14 17:48:44 -04001641 if (!dev_priv->mm.suspended) {
Chris Wilsonb3b079d2010-09-13 23:44:34 +01001642 mod_timer(&dev_priv->hangcheck_timer,
1643 jiffies + msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
Ben Gamarif65d9422009-09-14 17:48:44 -04001644 if (was_empty)
Chris Wilsonb3b079d2010-09-13 23:44:34 +01001645 queue_delayed_work(dev_priv->wq,
1646 &dev_priv->mm.retire_work, HZ);
Ben Gamarif65d9422009-09-14 17:48:44 -04001647 }
Eric Anholt673a3942008-07-30 12:06:12 -07001648 return seqno;
1649}
1650
1651/**
1652 * Command execution barrier
1653 *
1654 * Ensures that all commands in the ring are finished
1655 * before signalling the CPU
1656 */
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001657static void
Zou Nan hai852835f2010-05-21 09:08:56 +08001658i915_retire_commands(struct drm_device *dev, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001659{
Eric Anholt673a3942008-07-30 12:06:12 -07001660 uint32_t flush_domains = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001661
1662 /* The sampler always gets flushed on i965 (sigh) */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001663 if (INTEL_INFO(dev)->gen >= 4)
Eric Anholt673a3942008-07-30 12:06:12 -07001664 flush_domains |= I915_GEM_DOMAIN_SAMPLER;
Zou Nan hai852835f2010-05-21 09:08:56 +08001665
1666 ring->flush(dev, ring,
1667 I915_GEM_DOMAIN_COMMAND, flush_domains);
Eric Anholt673a3942008-07-30 12:06:12 -07001668}
1669
1670/**
Eric Anholt673a3942008-07-30 12:06:12 -07001671 * Returns true if seq1 is later than seq2.
1672 */
Ben Gamari22be1722009-09-14 17:48:43 -04001673bool
Eric Anholt673a3942008-07-30 12:06:12 -07001674i915_seqno_passed(uint32_t seq1, uint32_t seq2)
1675{
1676 return (int32_t)(seq1 - seq2) >= 0;
1677}
1678
1679uint32_t
Zou Nan hai852835f2010-05-21 09:08:56 +08001680i915_get_gem_seqno(struct drm_device *dev,
Zou Nan haid1b851f2010-05-21 09:08:57 +08001681 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001682{
Zou Nan hai852835f2010-05-21 09:08:56 +08001683 return ring->get_gem_seqno(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07001684}
1685
1686/**
1687 * This function clears the request list as sequence numbers are passed.
1688 */
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001689static void
1690i915_gem_retire_requests_ring(struct drm_device *dev,
1691 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001692{
1693 drm_i915_private_t *dev_priv = dev->dev_private;
1694 uint32_t seqno;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001695 bool wedged;
Eric Anholt673a3942008-07-30 12:06:12 -07001696
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001697 if (!ring->status_page.page_addr ||
1698 list_empty(&ring->request_list))
Karsten Wiese6c0594a2009-02-23 15:07:57 +01001699 return;
1700
Zou Nan hai852835f2010-05-21 09:08:56 +08001701 seqno = i915_get_gem_seqno(dev, ring);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001702 wedged = atomic_read(&dev_priv->mm.wedged);
Eric Anholt673a3942008-07-30 12:06:12 -07001703
Zou Nan hai852835f2010-05-21 09:08:56 +08001704 while (!list_empty(&ring->request_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001705 struct drm_i915_gem_request *request;
Eric Anholt673a3942008-07-30 12:06:12 -07001706
Zou Nan hai852835f2010-05-21 09:08:56 +08001707 request = list_first_entry(&ring->request_list,
Eric Anholt673a3942008-07-30 12:06:12 -07001708 struct drm_i915_gem_request,
1709 list);
Eric Anholt673a3942008-07-30 12:06:12 -07001710
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001711 if (!wedged && !i915_seqno_passed(seqno, request->seqno))
Eric Anholt673a3942008-07-30 12:06:12 -07001712 break;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001713
1714 trace_i915_gem_request_retire(dev, request->seqno);
1715
1716 list_del(&request->list);
1717 list_del(&request->client_list);
1718 kfree(request);
1719 }
1720
1721 /* Move any buffers on the active list that are no longer referenced
1722 * by the ringbuffer to the flushing/inactive lists as appropriate.
1723 */
1724 while (!list_empty(&ring->active_list)) {
1725 struct drm_gem_object *obj;
1726 struct drm_i915_gem_object *obj_priv;
1727
1728 obj_priv = list_first_entry(&ring->active_list,
1729 struct drm_i915_gem_object,
1730 list);
1731
1732 if (!wedged &&
1733 !i915_seqno_passed(seqno, obj_priv->last_rendering_seqno))
1734 break;
1735
1736 obj = &obj_priv->base;
1737
1738#if WATCH_LRU
1739 DRM_INFO("%s: retire %d moves to inactive list %p\n",
1740 __func__, request->seqno, obj);
1741#endif
1742
1743 if (obj->write_domain != 0)
1744 i915_gem_object_move_to_flushing(obj);
1745 else
1746 i915_gem_object_move_to_inactive(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001747 }
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001748
1749 if (unlikely (dev_priv->trace_irq_seqno &&
1750 i915_seqno_passed(dev_priv->trace_irq_seqno, seqno))) {
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001751 ring->user_irq_put(dev, ring);
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001752 dev_priv->trace_irq_seqno = 0;
1753 }
Eric Anholt673a3942008-07-30 12:06:12 -07001754}
1755
1756void
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001757i915_gem_retire_requests(struct drm_device *dev)
1758{
1759 drm_i915_private_t *dev_priv = dev->dev_private;
1760
Chris Wilsonbe726152010-07-23 23:18:50 +01001761 if (!list_empty(&dev_priv->mm.deferred_free_list)) {
1762 struct drm_i915_gem_object *obj_priv, *tmp;
1763
1764 /* We must be careful that during unbind() we do not
1765 * accidentally infinitely recurse into retire requests.
1766 * Currently:
1767 * retire -> free -> unbind -> wait -> retire_ring
1768 */
1769 list_for_each_entry_safe(obj_priv, tmp,
1770 &dev_priv->mm.deferred_free_list,
1771 list)
1772 i915_gem_free_object_tail(&obj_priv->base);
1773 }
1774
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001775 i915_gem_retire_requests_ring(dev, &dev_priv->render_ring);
1776 if (HAS_BSD(dev))
1777 i915_gem_retire_requests_ring(dev, &dev_priv->bsd_ring);
1778}
1779
Daniel Vetter75ef9da2010-08-21 00:25:16 +02001780static void
Eric Anholt673a3942008-07-30 12:06:12 -07001781i915_gem_retire_work_handler(struct work_struct *work)
1782{
1783 drm_i915_private_t *dev_priv;
1784 struct drm_device *dev;
1785
1786 dev_priv = container_of(work, drm_i915_private_t,
1787 mm.retire_work.work);
1788 dev = dev_priv->dev;
1789
1790 mutex_lock(&dev->struct_mutex);
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001791 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08001792
Keith Packard6dbe2772008-10-14 21:41:13 -07001793 if (!dev_priv->mm.suspended &&
Zou Nan haid1b851f2010-05-21 09:08:57 +08001794 (!list_empty(&dev_priv->render_ring.request_list) ||
1795 (HAS_BSD(dev) &&
1796 !list_empty(&dev_priv->bsd_ring.request_list))))
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001797 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
Eric Anholt673a3942008-07-30 12:06:12 -07001798 mutex_unlock(&dev->struct_mutex);
1799}
1800
Daniel Vetter5a5a0c62009-09-15 22:57:36 +02001801int
Zou Nan hai852835f2010-05-21 09:08:56 +08001802i915_do_wait_request(struct drm_device *dev, uint32_t seqno,
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001803 bool interruptible, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001804{
1805 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001806 u32 ier;
Eric Anholt673a3942008-07-30 12:06:12 -07001807 int ret = 0;
1808
1809 BUG_ON(seqno == 0);
1810
Daniel Vettere35a41d2010-02-11 22:13:59 +01001811 if (seqno == dev_priv->next_seqno) {
Chris Wilson8dc5d142010-08-12 12:36:12 +01001812 seqno = i915_add_request(dev, NULL, NULL, ring);
Daniel Vettere35a41d2010-02-11 22:13:59 +01001813 if (seqno == 0)
1814 return -ENOMEM;
1815 }
1816
Ben Gamariba1234d2009-09-14 17:48:47 -04001817 if (atomic_read(&dev_priv->mm.wedged))
Ben Gamariffed1d02009-09-14 17:48:41 -04001818 return -EIO;
1819
Zou Nan hai852835f2010-05-21 09:08:56 +08001820 if (!i915_seqno_passed(ring->get_gem_seqno(dev, ring), seqno)) {
Eric Anholtbad720f2009-10-22 16:11:14 -07001821 if (HAS_PCH_SPLIT(dev))
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001822 ier = I915_READ(DEIER) | I915_READ(GTIER);
1823 else
1824 ier = I915_READ(IER);
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001825 if (!ier) {
1826 DRM_ERROR("something (likely vbetool) disabled "
1827 "interrupts, re-enabling\n");
1828 i915_driver_irq_preinstall(dev);
1829 i915_driver_irq_postinstall(dev);
1830 }
1831
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001832 trace_i915_gem_request_wait_begin(dev, seqno);
1833
Zou Nan hai852835f2010-05-21 09:08:56 +08001834 ring->waiting_gem_seqno = seqno;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001835 ring->user_irq_get(dev, ring);
Daniel Vetter48764bf2009-09-15 22:57:32 +02001836 if (interruptible)
Zou Nan hai852835f2010-05-21 09:08:56 +08001837 ret = wait_event_interruptible(ring->irq_queue,
1838 i915_seqno_passed(
1839 ring->get_gem_seqno(dev, ring), seqno)
1840 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02001841 else
Zou Nan hai852835f2010-05-21 09:08:56 +08001842 wait_event(ring->irq_queue,
1843 i915_seqno_passed(
1844 ring->get_gem_seqno(dev, ring), seqno)
1845 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02001846
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001847 ring->user_irq_put(dev, ring);
Zou Nan hai852835f2010-05-21 09:08:56 +08001848 ring->waiting_gem_seqno = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001849
1850 trace_i915_gem_request_wait_end(dev, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07001851 }
Ben Gamariba1234d2009-09-14 17:48:47 -04001852 if (atomic_read(&dev_priv->mm.wedged))
Eric Anholt673a3942008-07-30 12:06:12 -07001853 ret = -EIO;
1854
1855 if (ret && ret != -ERESTARTSYS)
Daniel Vetter8bff9172010-02-11 22:19:40 +01001856 DRM_ERROR("%s returns %d (awaiting %d at %d, next %d)\n",
1857 __func__, ret, seqno, ring->get_gem_seqno(dev, ring),
1858 dev_priv->next_seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07001859
1860 /* Directly dispatch request retiring. While we have the work queue
1861 * to handle this, the waiter on a request often wants an associated
1862 * buffer to have made it to the inactive list, and we would need
1863 * a separate wait queue to handle that.
1864 */
1865 if (ret == 0)
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001866 i915_gem_retire_requests_ring(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07001867
1868 return ret;
1869}
1870
Daniel Vetter48764bf2009-09-15 22:57:32 +02001871/**
1872 * Waits for a sequence number to be signaled, and cleans up the
1873 * request and object lists appropriately for that event.
1874 */
1875static int
Zou Nan hai852835f2010-05-21 09:08:56 +08001876i915_wait_request(struct drm_device *dev, uint32_t seqno,
1877 struct intel_ring_buffer *ring)
Daniel Vetter48764bf2009-09-15 22:57:32 +02001878{
Zou Nan hai852835f2010-05-21 09:08:56 +08001879 return i915_do_wait_request(dev, seqno, 1, ring);
Daniel Vetter48764bf2009-09-15 22:57:32 +02001880}
1881
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001882static void
1883i915_gem_flush(struct drm_device *dev,
1884 uint32_t invalidate_domains,
1885 uint32_t flush_domains)
1886{
1887 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter8bff9172010-02-11 22:19:40 +01001888
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001889 if (flush_domains & I915_GEM_DOMAIN_CPU)
1890 drm_agp_chipset_flush(dev);
Daniel Vetter8bff9172010-02-11 22:19:40 +01001891
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001892 dev_priv->render_ring.flush(dev, &dev_priv->render_ring,
1893 invalidate_domains,
1894 flush_domains);
Zou Nan haid1b851f2010-05-21 09:08:57 +08001895
1896 if (HAS_BSD(dev))
1897 dev_priv->bsd_ring.flush(dev, &dev_priv->bsd_ring,
1898 invalidate_domains,
1899 flush_domains);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001900}
1901
Eric Anholt673a3942008-07-30 12:06:12 -07001902/**
1903 * Ensures that all rendering to the object has completed and the object is
1904 * safe to unbind from the GTT or access from the CPU.
1905 */
1906static int
Chris Wilson2cf34d72010-09-14 13:03:28 +01001907i915_gem_object_wait_rendering(struct drm_gem_object *obj,
1908 bool interruptible)
Eric Anholt673a3942008-07-30 12:06:12 -07001909{
1910 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001911 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001912 int ret;
1913
Eric Anholte47c68e2008-11-14 13:35:19 -08001914 /* This function only exists to support waiting for existing rendering,
1915 * not for emitting required flushes.
Eric Anholt673a3942008-07-30 12:06:12 -07001916 */
Eric Anholte47c68e2008-11-14 13:35:19 -08001917 BUG_ON((obj->write_domain & I915_GEM_GPU_DOMAINS) != 0);
Eric Anholt673a3942008-07-30 12:06:12 -07001918
1919 /* If there is rendering queued on the buffer being evicted, wait for
1920 * it.
1921 */
1922 if (obj_priv->active) {
1923#if WATCH_BUF
1924 DRM_INFO("%s: object %p wait for seqno %08x\n",
1925 __func__, obj, obj_priv->last_rendering_seqno);
1926#endif
Chris Wilson2cf34d72010-09-14 13:03:28 +01001927 ret = i915_do_wait_request(dev,
1928 obj_priv->last_rendering_seqno,
1929 interruptible,
1930 obj_priv->ring);
1931 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07001932 return ret;
1933 }
1934
1935 return 0;
1936}
1937
1938/**
1939 * Unbinds an object from the GTT aperture.
1940 */
Jesse Barnes0f973f22009-01-26 17:10:45 -08001941int
Eric Anholt673a3942008-07-30 12:06:12 -07001942i915_gem_object_unbind(struct drm_gem_object *obj)
1943{
1944 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001945 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001946 int ret = 0;
1947
1948#if WATCH_BUF
1949 DRM_INFO("%s:%d %p\n", __func__, __LINE__, obj);
1950 DRM_INFO("gtt_space %p\n", obj_priv->gtt_space);
1951#endif
1952 if (obj_priv->gtt_space == NULL)
1953 return 0;
1954
1955 if (obj_priv->pin_count != 0) {
1956 DRM_ERROR("Attempting to unbind pinned buffer\n");
1957 return -EINVAL;
1958 }
1959
Eric Anholt5323fd02009-09-09 11:50:45 -07001960 /* blow away mappings if mapped through GTT */
1961 i915_gem_release_mmap(obj);
1962
Eric Anholt673a3942008-07-30 12:06:12 -07001963 /* Move the object to the CPU domain to ensure that
1964 * any possible CPU writes while it's not in the GTT
1965 * are flushed when we go to remap it. This will
1966 * also ensure that all pending GPU writes are finished
1967 * before we unbind.
1968 */
Eric Anholte47c68e2008-11-14 13:35:19 -08001969 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Chris Wilson8dc17752010-07-23 23:18:51 +01001970 if (ret == -ERESTARTSYS)
Eric Anholt673a3942008-07-30 12:06:12 -07001971 return ret;
Chris Wilson8dc17752010-07-23 23:18:51 +01001972 /* Continue on if we fail due to EIO, the GPU is hung so we
1973 * should be safe and we need to cleanup or else we might
1974 * cause memory corruption through use-after-free.
1975 */
Eric Anholt673a3942008-07-30 12:06:12 -07001976
Daniel Vetter96b47b62009-12-15 17:50:00 +01001977 /* release the fence reg _after_ flushing */
1978 if (obj_priv->fence_reg != I915_FENCE_REG_NONE)
1979 i915_gem_clear_fence_reg(obj);
1980
Eric Anholt673a3942008-07-30 12:06:12 -07001981 if (obj_priv->agp_mem != NULL) {
1982 drm_unbind_agp(obj_priv->agp_mem);
1983 drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE);
1984 obj_priv->agp_mem = NULL;
1985 }
1986
Eric Anholt856fa192009-03-19 14:10:50 -07001987 i915_gem_object_put_pages(obj);
Chris Wilsona32808c2009-09-20 21:29:47 +01001988 BUG_ON(obj_priv->pages_refcount);
Eric Anholt673a3942008-07-30 12:06:12 -07001989
1990 if (obj_priv->gtt_space) {
1991 atomic_dec(&dev->gtt_count);
1992 atomic_sub(obj->size, &dev->gtt_memory);
1993
1994 drm_mm_put_block(obj_priv->gtt_space);
1995 obj_priv->gtt_space = NULL;
1996 }
1997
1998 /* Remove ourselves from the LRU list if present. */
1999 if (!list_empty(&obj_priv->list))
2000 list_del_init(&obj_priv->list);
2001
Chris Wilson963b4832009-09-20 23:03:54 +01002002 if (i915_gem_object_is_purgeable(obj_priv))
2003 i915_gem_object_truncate(obj);
2004
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002005 trace_i915_gem_object_unbind(obj);
2006
Chris Wilson8dc17752010-07-23 23:18:51 +01002007 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002008}
2009
Chris Wilsonb47eb4a2010-08-07 11:01:23 +01002010int
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002011i915_gpu_idle(struct drm_device *dev)
2012{
2013 drm_i915_private_t *dev_priv = dev->dev_private;
2014 bool lists_empty;
Zou Nan hai852835f2010-05-21 09:08:56 +08002015 int ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002016
Zou Nan haid1b851f2010-05-21 09:08:57 +08002017 lists_empty = (list_empty(&dev_priv->mm.flushing_list) &&
2018 list_empty(&dev_priv->render_ring.active_list) &&
2019 (!HAS_BSD(dev) ||
2020 list_empty(&dev_priv->bsd_ring.active_list)));
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002021 if (lists_empty)
2022 return 0;
2023
2024 /* Flush everything onto the inactive list. */
2025 i915_gem_flush(dev, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
Daniel Vetter4fc6ee72010-02-11 22:53:20 +01002026
2027 ret = i915_wait_request(dev,
2028 i915_gem_next_request_seqno(dev, &dev_priv->render_ring),
2029 &dev_priv->render_ring);
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002030 if (ret)
2031 return ret;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002032
2033 if (HAS_BSD(dev)) {
Daniel Vetter4fc6ee72010-02-11 22:53:20 +01002034 ret = i915_wait_request(dev,
2035 i915_gem_next_request_seqno(dev, &dev_priv->bsd_ring),
2036 &dev_priv->bsd_ring);
Zou Nan haid1b851f2010-05-21 09:08:57 +08002037 if (ret)
2038 return ret;
2039 }
2040
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002041 return 0;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002042}
2043
Ben Gamari6911a9b2009-04-02 11:24:54 -07002044int
Chris Wilson4bdadb92010-01-27 13:36:32 +00002045i915_gem_object_get_pages(struct drm_gem_object *obj,
2046 gfp_t gfpmask)
Eric Anholt673a3942008-07-30 12:06:12 -07002047{
Daniel Vetter23010e42010-03-08 13:35:02 +01002048 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002049 int page_count, i;
2050 struct address_space *mapping;
2051 struct inode *inode;
2052 struct page *page;
Eric Anholt673a3942008-07-30 12:06:12 -07002053
Daniel Vetter778c3542010-05-13 11:49:44 +02002054 BUG_ON(obj_priv->pages_refcount
2055 == DRM_I915_GEM_OBJECT_MAX_PAGES_REFCOUNT);
2056
Eric Anholt856fa192009-03-19 14:10:50 -07002057 if (obj_priv->pages_refcount++ != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07002058 return 0;
2059
2060 /* Get the list of pages out of our struct file. They'll be pinned
2061 * at this point until we release them.
2062 */
2063 page_count = obj->size / PAGE_SIZE;
Eric Anholt856fa192009-03-19 14:10:50 -07002064 BUG_ON(obj_priv->pages != NULL);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07002065 obj_priv->pages = drm_calloc_large(page_count, sizeof(struct page *));
Eric Anholt856fa192009-03-19 14:10:50 -07002066 if (obj_priv->pages == NULL) {
Eric Anholt856fa192009-03-19 14:10:50 -07002067 obj_priv->pages_refcount--;
Eric Anholt673a3942008-07-30 12:06:12 -07002068 return -ENOMEM;
2069 }
2070
2071 inode = obj->filp->f_path.dentry->d_inode;
2072 mapping = inode->i_mapping;
2073 for (i = 0; i < page_count; i++) {
Chris Wilson4bdadb92010-01-27 13:36:32 +00002074 page = read_cache_page_gfp(mapping, i,
Linus Torvalds985b8232010-07-02 10:04:42 +10002075 GFP_HIGHUSER |
Chris Wilson4bdadb92010-01-27 13:36:32 +00002076 __GFP_COLD |
Linus Torvaldscd9f0402010-07-18 09:44:37 -07002077 __GFP_RECLAIMABLE |
Chris Wilson4bdadb92010-01-27 13:36:32 +00002078 gfpmask);
Chris Wilson1f2b1012010-03-12 19:52:55 +00002079 if (IS_ERR(page))
2080 goto err_pages;
2081
Eric Anholt856fa192009-03-19 14:10:50 -07002082 obj_priv->pages[i] = page;
Eric Anholt673a3942008-07-30 12:06:12 -07002083 }
Eric Anholt280b7132009-03-12 16:56:27 -07002084
2085 if (obj_priv->tiling_mode != I915_TILING_NONE)
2086 i915_gem_object_do_bit_17_swizzle(obj);
2087
Eric Anholt673a3942008-07-30 12:06:12 -07002088 return 0;
Chris Wilson1f2b1012010-03-12 19:52:55 +00002089
2090err_pages:
2091 while (i--)
2092 page_cache_release(obj_priv->pages[i]);
2093
2094 drm_free_large(obj_priv->pages);
2095 obj_priv->pages = NULL;
2096 obj_priv->pages_refcount--;
2097 return PTR_ERR(page);
Eric Anholt673a3942008-07-30 12:06:12 -07002098}
2099
Eric Anholt4e901fd2009-10-26 16:44:17 -07002100static void sandybridge_write_fence_reg(struct drm_i915_fence_reg *reg)
2101{
2102 struct drm_gem_object *obj = reg->obj;
2103 struct drm_device *dev = obj->dev;
2104 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002105 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt4e901fd2009-10-26 16:44:17 -07002106 int regnum = obj_priv->fence_reg;
2107 uint64_t val;
2108
2109 val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
2110 0xfffff000) << 32;
2111 val |= obj_priv->gtt_offset & 0xfffff000;
2112 val |= (uint64_t)((obj_priv->stride / 128) - 1) <<
2113 SANDYBRIDGE_FENCE_PITCH_SHIFT;
2114
2115 if (obj_priv->tiling_mode == I915_TILING_Y)
2116 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2117 val |= I965_FENCE_REG_VALID;
2118
2119 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (regnum * 8), val);
2120}
2121
Jesse Barnesde151cf2008-11-12 10:03:55 -08002122static void i965_write_fence_reg(struct drm_i915_fence_reg *reg)
2123{
2124 struct drm_gem_object *obj = reg->obj;
2125 struct drm_device *dev = obj->dev;
2126 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002127 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002128 int regnum = obj_priv->fence_reg;
2129 uint64_t val;
2130
2131 val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
2132 0xfffff000) << 32;
2133 val |= obj_priv->gtt_offset & 0xfffff000;
2134 val |= ((obj_priv->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
2135 if (obj_priv->tiling_mode == I915_TILING_Y)
2136 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2137 val |= I965_FENCE_REG_VALID;
2138
2139 I915_WRITE64(FENCE_REG_965_0 + (regnum * 8), val);
2140}
2141
2142static void i915_write_fence_reg(struct drm_i915_fence_reg *reg)
2143{
2144 struct drm_gem_object *obj = reg->obj;
2145 struct drm_device *dev = obj->dev;
2146 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002147 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002148 int regnum = obj_priv->fence_reg;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002149 int tile_width;
Eric Anholtdc529a42009-03-10 22:34:49 -07002150 uint32_t fence_reg, val;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002151 uint32_t pitch_val;
2152
2153 if ((obj_priv->gtt_offset & ~I915_FENCE_START_MASK) ||
2154 (obj_priv->gtt_offset & (obj->size - 1))) {
Linus Torvaldsf06da262009-02-09 08:57:29 -08002155 WARN(1, "%s: object 0x%08x not 1M or size (0x%zx) aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002156 __func__, obj_priv->gtt_offset, obj->size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002157 return;
2158 }
2159
Jesse Barnes0f973f22009-01-26 17:10:45 -08002160 if (obj_priv->tiling_mode == I915_TILING_Y &&
2161 HAS_128_BYTE_Y_TILING(dev))
2162 tile_width = 128;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002163 else
Jesse Barnes0f973f22009-01-26 17:10:45 -08002164 tile_width = 512;
2165
2166 /* Note: pitch better be a power of two tile widths */
2167 pitch_val = obj_priv->stride / tile_width;
2168 pitch_val = ffs(pitch_val) - 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002169
Daniel Vetterc36a2a62010-04-17 15:12:03 +02002170 if (obj_priv->tiling_mode == I915_TILING_Y &&
2171 HAS_128_BYTE_Y_TILING(dev))
2172 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2173 else
2174 WARN_ON(pitch_val > I915_FENCE_MAX_PITCH_VAL);
2175
Jesse Barnesde151cf2008-11-12 10:03:55 -08002176 val = obj_priv->gtt_offset;
2177 if (obj_priv->tiling_mode == I915_TILING_Y)
2178 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2179 val |= I915_FENCE_SIZE_BITS(obj->size);
2180 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2181 val |= I830_FENCE_REG_VALID;
2182
Eric Anholtdc529a42009-03-10 22:34:49 -07002183 if (regnum < 8)
2184 fence_reg = FENCE_REG_830_0 + (regnum * 4);
2185 else
2186 fence_reg = FENCE_REG_945_8 + ((regnum - 8) * 4);
2187 I915_WRITE(fence_reg, val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002188}
2189
2190static void i830_write_fence_reg(struct drm_i915_fence_reg *reg)
2191{
2192 struct drm_gem_object *obj = reg->obj;
2193 struct drm_device *dev = obj->dev;
2194 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002195 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002196 int regnum = obj_priv->fence_reg;
2197 uint32_t val;
2198 uint32_t pitch_val;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002199 uint32_t fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002200
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002201 if ((obj_priv->gtt_offset & ~I830_FENCE_START_MASK) ||
Jesse Barnesde151cf2008-11-12 10:03:55 -08002202 (obj_priv->gtt_offset & (obj->size - 1))) {
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002203 WARN(1, "%s: object 0x%08x not 512K or size aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002204 __func__, obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002205 return;
2206 }
2207
Eric Anholte76a16d2009-05-26 17:44:56 -07002208 pitch_val = obj_priv->stride / 128;
2209 pitch_val = ffs(pitch_val) - 1;
2210 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2211
Jesse Barnesde151cf2008-11-12 10:03:55 -08002212 val = obj_priv->gtt_offset;
2213 if (obj_priv->tiling_mode == I915_TILING_Y)
2214 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002215 fence_size_bits = I830_FENCE_SIZE_BITS(obj->size);
2216 WARN_ON(fence_size_bits & ~0x00000f00);
2217 val |= fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002218 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2219 val |= I830_FENCE_REG_VALID;
2220
2221 I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002222}
2223
Chris Wilson2cf34d72010-09-14 13:03:28 +01002224static int i915_find_fence_reg(struct drm_device *dev,
2225 bool interruptible)
Daniel Vetterae3db242010-02-19 11:51:58 +01002226{
2227 struct drm_i915_fence_reg *reg = NULL;
2228 struct drm_i915_gem_object *obj_priv = NULL;
2229 struct drm_i915_private *dev_priv = dev->dev_private;
2230 struct drm_gem_object *obj = NULL;
2231 int i, avail, ret;
2232
2233 /* First try to find a free reg */
2234 avail = 0;
2235 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2236 reg = &dev_priv->fence_regs[i];
2237 if (!reg->obj)
2238 return i;
2239
Daniel Vetter23010e42010-03-08 13:35:02 +01002240 obj_priv = to_intel_bo(reg->obj);
Daniel Vetterae3db242010-02-19 11:51:58 +01002241 if (!obj_priv->pin_count)
2242 avail++;
2243 }
2244
2245 if (avail == 0)
2246 return -ENOSPC;
2247
2248 /* None available, try to steal one or wait for a user to finish */
2249 i = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002250 list_for_each_entry(reg, &dev_priv->mm.fence_list,
2251 lru_list) {
2252 obj = reg->obj;
2253 obj_priv = to_intel_bo(obj);
Daniel Vetterae3db242010-02-19 11:51:58 +01002254
2255 if (obj_priv->pin_count)
2256 continue;
2257
2258 /* found one! */
2259 i = obj_priv->fence_reg;
2260 break;
2261 }
2262
2263 BUG_ON(i == I915_FENCE_REG_NONE);
2264
2265 /* We only have a reference on obj from the active list. put_fence_reg
2266 * might drop that one, causing a use-after-free in it. So hold a
2267 * private reference to obj like the other callers of put_fence_reg
2268 * (set_tiling ioctl) do. */
2269 drm_gem_object_reference(obj);
Chris Wilson2cf34d72010-09-14 13:03:28 +01002270 ret = i915_gem_object_put_fence_reg(obj, interruptible);
Daniel Vetterae3db242010-02-19 11:51:58 +01002271 drm_gem_object_unreference(obj);
2272 if (ret != 0)
2273 return ret;
2274
2275 return i;
2276}
2277
Jesse Barnesde151cf2008-11-12 10:03:55 -08002278/**
2279 * i915_gem_object_get_fence_reg - set up a fence reg for an object
2280 * @obj: object to map through a fence reg
2281 *
2282 * When mapping objects through the GTT, userspace wants to be able to write
2283 * to them without having to worry about swizzling if the object is tiled.
2284 *
2285 * This function walks the fence regs looking for a free one for @obj,
2286 * stealing one if it can't find any.
2287 *
2288 * It then sets up the reg based on the object's properties: address, pitch
2289 * and tiling format.
2290 */
Chris Wilson8c4b8c32009-06-17 22:08:52 +01002291int
Chris Wilson2cf34d72010-09-14 13:03:28 +01002292i915_gem_object_get_fence_reg(struct drm_gem_object *obj,
2293 bool interruptible)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002294{
2295 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002296 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002297 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002298 struct drm_i915_fence_reg *reg = NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002299 int ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002300
Eric Anholta09ba7f2009-08-29 12:49:51 -07002301 /* Just update our place in the LRU if our fence is getting used. */
2302 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002303 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
2304 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002305 return 0;
2306 }
2307
Jesse Barnesde151cf2008-11-12 10:03:55 -08002308 switch (obj_priv->tiling_mode) {
2309 case I915_TILING_NONE:
2310 WARN(1, "allocating a fence for non-tiled object?\n");
2311 break;
2312 case I915_TILING_X:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002313 if (!obj_priv->stride)
2314 return -EINVAL;
2315 WARN((obj_priv->stride & (512 - 1)),
2316 "object 0x%08x is X tiled but has non-512B pitch\n",
2317 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002318 break;
2319 case I915_TILING_Y:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002320 if (!obj_priv->stride)
2321 return -EINVAL;
2322 WARN((obj_priv->stride & (128 - 1)),
2323 "object 0x%08x is Y tiled but has non-128B pitch\n",
2324 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002325 break;
2326 }
2327
Chris Wilson2cf34d72010-09-14 13:03:28 +01002328 ret = i915_find_fence_reg(dev, interruptible);
Daniel Vetterae3db242010-02-19 11:51:58 +01002329 if (ret < 0)
2330 return ret;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002331
Daniel Vetterae3db242010-02-19 11:51:58 +01002332 obj_priv->fence_reg = ret;
2333 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002334 list_add_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002335
Jesse Barnesde151cf2008-11-12 10:03:55 -08002336 reg->obj = obj;
2337
Chris Wilsone259bef2010-09-17 00:32:02 +01002338 switch (INTEL_INFO(dev)->gen) {
2339 case 6:
Eric Anholt4e901fd2009-10-26 16:44:17 -07002340 sandybridge_write_fence_reg(reg);
Chris Wilsone259bef2010-09-17 00:32:02 +01002341 break;
2342 case 5:
2343 case 4:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002344 i965_write_fence_reg(reg);
Chris Wilsone259bef2010-09-17 00:32:02 +01002345 break;
2346 case 3:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002347 i915_write_fence_reg(reg);
Chris Wilsone259bef2010-09-17 00:32:02 +01002348 break;
2349 case 2:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002350 i830_write_fence_reg(reg);
Chris Wilsone259bef2010-09-17 00:32:02 +01002351 break;
2352 }
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002353
Daniel Vetterae3db242010-02-19 11:51:58 +01002354 trace_i915_gem_object_get_fence(obj, obj_priv->fence_reg,
2355 obj_priv->tiling_mode);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002356
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002357 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002358}
2359
2360/**
2361 * i915_gem_clear_fence_reg - clear out fence register info
2362 * @obj: object to clear
2363 *
2364 * Zeroes out the fence register itself and clears out the associated
2365 * data structures in dev_priv and obj_priv.
2366 */
2367static void
2368i915_gem_clear_fence_reg(struct drm_gem_object *obj)
2369{
2370 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002371 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002372 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002373 struct drm_i915_fence_reg *reg =
2374 &dev_priv->fence_regs[obj_priv->fence_reg];
Chris Wilsone259bef2010-09-17 00:32:02 +01002375 uint32_t fence_reg;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002376
Chris Wilsone259bef2010-09-17 00:32:02 +01002377 switch (INTEL_INFO(dev)->gen) {
2378 case 6:
Eric Anholt4e901fd2009-10-26 16:44:17 -07002379 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 +
2380 (obj_priv->fence_reg * 8), 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002381 break;
2382 case 5:
2383 case 4:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002384 I915_WRITE64(FENCE_REG_965_0 + (obj_priv->fence_reg * 8), 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002385 break;
2386 case 3:
2387 if (obj_priv->fence_reg > 8)
2388 fence_reg = FENCE_REG_945_8 + (obj_priv->fence_reg - 8) * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002389 else
Chris Wilsone259bef2010-09-17 00:32:02 +01002390 case 2:
2391 fence_reg = FENCE_REG_830_0 + obj_priv->fence_reg * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002392
2393 I915_WRITE(fence_reg, 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002394 break;
Eric Anholtdc529a42009-03-10 22:34:49 -07002395 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002396
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002397 reg->obj = NULL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002398 obj_priv->fence_reg = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002399 list_del_init(&reg->lru_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002400}
2401
Eric Anholt673a3942008-07-30 12:06:12 -07002402/**
Chris Wilson52dc7d32009-06-06 09:46:01 +01002403 * i915_gem_object_put_fence_reg - waits on outstanding fenced access
2404 * to the buffer to finish, and then resets the fence register.
2405 * @obj: tiled object holding a fence register.
Chris Wilson2cf34d72010-09-14 13:03:28 +01002406 * @bool: whether the wait upon the fence is interruptible
Chris Wilson52dc7d32009-06-06 09:46:01 +01002407 *
2408 * Zeroes out the fence register itself and clears out the associated
2409 * data structures in dev_priv and obj_priv.
2410 */
2411int
Chris Wilson2cf34d72010-09-14 13:03:28 +01002412i915_gem_object_put_fence_reg(struct drm_gem_object *obj,
2413 bool interruptible)
Chris Wilson52dc7d32009-06-06 09:46:01 +01002414{
2415 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01002416 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson52dc7d32009-06-06 09:46:01 +01002417
2418 if (obj_priv->fence_reg == I915_FENCE_REG_NONE)
2419 return 0;
2420
Daniel Vetter10ae9bd2010-02-01 13:59:17 +01002421 /* If we've changed tiling, GTT-mappings of the object
2422 * need to re-fault to ensure that the correct fence register
2423 * setup is in place.
2424 */
2425 i915_gem_release_mmap(obj);
2426
Chris Wilson52dc7d32009-06-06 09:46:01 +01002427 /* On the i915, GPU access to tiled buffers is via a fence,
2428 * therefore we must wait for any outstanding access to complete
2429 * before clearing the fence.
2430 */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002431 if (INTEL_INFO(dev)->gen < 4) {
Chris Wilson52dc7d32009-06-06 09:46:01 +01002432 int ret;
2433
Chris Wilson2cf34d72010-09-14 13:03:28 +01002434 ret = i915_gem_object_flush_gpu_write_domain(obj, true);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002435 if (ret)
2436 return ret;
2437
Chris Wilson2cf34d72010-09-14 13:03:28 +01002438 ret = i915_gem_object_wait_rendering(obj, interruptible);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002439 if (ret)
Chris Wilson52dc7d32009-06-06 09:46:01 +01002440 return ret;
2441 }
2442
Daniel Vetter4a726612010-02-01 13:59:16 +01002443 i915_gem_object_flush_gtt_write_domain(obj);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002444 i915_gem_clear_fence_reg(obj);
Chris Wilson52dc7d32009-06-06 09:46:01 +01002445
2446 return 0;
2447}
2448
2449/**
Eric Anholt673a3942008-07-30 12:06:12 -07002450 * Finds free space in the GTT aperture and binds the object there.
2451 */
2452static int
2453i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment)
2454{
2455 struct drm_device *dev = obj->dev;
2456 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002457 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002458 struct drm_mm_node *free_space;
Chris Wilson4bdadb92010-01-27 13:36:32 +00002459 gfp_t gfpmask = __GFP_NORETRY | __GFP_NOWARN;
Chris Wilson07f73f62009-09-14 16:50:30 +01002460 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002461
Chris Wilsonbb6baf72009-09-22 14:24:13 +01002462 if (obj_priv->madv != I915_MADV_WILLNEED) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01002463 DRM_ERROR("Attempting to bind a purgeable object\n");
2464 return -EINVAL;
2465 }
2466
Eric Anholt673a3942008-07-30 12:06:12 -07002467 if (alignment == 0)
Jesse Barnes0f973f22009-01-26 17:10:45 -08002468 alignment = i915_gem_get_gtt_alignment(obj);
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002469 if (alignment & (i915_gem_get_gtt_alignment(obj) - 1)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002470 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2471 return -EINVAL;
2472 }
2473
Chris Wilson654fc602010-05-27 13:18:21 +01002474 /* If the object is bigger than the entire aperture, reject it early
2475 * before evicting everything in a vain attempt to find space.
2476 */
2477 if (obj->size > dev->gtt_total) {
2478 DRM_ERROR("Attempting to bind an object larger than the aperture\n");
2479 return -E2BIG;
2480 }
2481
Eric Anholt673a3942008-07-30 12:06:12 -07002482 search_free:
2483 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
2484 obj->size, alignment, 0);
2485 if (free_space != NULL) {
2486 obj_priv->gtt_space = drm_mm_get_block(free_space, obj->size,
2487 alignment);
Daniel Vetterdb3307a2010-07-02 15:02:12 +01002488 if (obj_priv->gtt_space != NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002489 obj_priv->gtt_offset = obj_priv->gtt_space->start;
Eric Anholt673a3942008-07-30 12:06:12 -07002490 }
2491 if (obj_priv->gtt_space == NULL) {
2492 /* If the gtt is empty and we're still having trouble
2493 * fitting our object in, we're out of memory.
2494 */
2495#if WATCH_LRU
2496 DRM_INFO("%s: GTT full, evicting something\n", __func__);
2497#endif
Daniel Vetter0108a3e2010-08-07 11:01:21 +01002498 ret = i915_gem_evict_something(dev, obj->size, alignment);
Chris Wilson97311292009-09-21 00:22:34 +01002499 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002500 return ret;
Chris Wilson97311292009-09-21 00:22:34 +01002501
Eric Anholt673a3942008-07-30 12:06:12 -07002502 goto search_free;
2503 }
2504
2505#if WATCH_BUF
Krzysztof Halasacfd43c02009-06-20 00:31:28 +02002506 DRM_INFO("Binding object of size %zd at 0x%08x\n",
Eric Anholt673a3942008-07-30 12:06:12 -07002507 obj->size, obj_priv->gtt_offset);
2508#endif
Chris Wilson4bdadb92010-01-27 13:36:32 +00002509 ret = i915_gem_object_get_pages(obj, gfpmask);
Eric Anholt673a3942008-07-30 12:06:12 -07002510 if (ret) {
2511 drm_mm_put_block(obj_priv->gtt_space);
2512 obj_priv->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002513
2514 if (ret == -ENOMEM) {
2515 /* first try to clear up some space from the GTT */
Daniel Vetter0108a3e2010-08-07 11:01:21 +01002516 ret = i915_gem_evict_something(dev, obj->size,
2517 alignment);
Chris Wilson07f73f62009-09-14 16:50:30 +01002518 if (ret) {
Chris Wilson07f73f62009-09-14 16:50:30 +01002519 /* now try to shrink everyone else */
Chris Wilson4bdadb92010-01-27 13:36:32 +00002520 if (gfpmask) {
2521 gfpmask = 0;
2522 goto search_free;
Chris Wilson07f73f62009-09-14 16:50:30 +01002523 }
2524
2525 return ret;
2526 }
2527
2528 goto search_free;
2529 }
2530
Eric Anholt673a3942008-07-30 12:06:12 -07002531 return ret;
2532 }
2533
Eric Anholt673a3942008-07-30 12:06:12 -07002534 /* Create an AGP memory structure pointing at our pages, and bind it
2535 * into the GTT.
2536 */
2537 obj_priv->agp_mem = drm_agp_bind_pages(dev,
Eric Anholt856fa192009-03-19 14:10:50 -07002538 obj_priv->pages,
Chris Wilson07f73f62009-09-14 16:50:30 +01002539 obj->size >> PAGE_SHIFT,
Keith Packardba1eb1d2008-10-14 19:55:10 -07002540 obj_priv->gtt_offset,
2541 obj_priv->agp_type);
Eric Anholt673a3942008-07-30 12:06:12 -07002542 if (obj_priv->agp_mem == NULL) {
Eric Anholt856fa192009-03-19 14:10:50 -07002543 i915_gem_object_put_pages(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002544 drm_mm_put_block(obj_priv->gtt_space);
2545 obj_priv->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002546
Daniel Vetter0108a3e2010-08-07 11:01:21 +01002547 ret = i915_gem_evict_something(dev, obj->size, alignment);
Chris Wilson97311292009-09-21 00:22:34 +01002548 if (ret)
Chris Wilson07f73f62009-09-14 16:50:30 +01002549 return ret;
Chris Wilson07f73f62009-09-14 16:50:30 +01002550
2551 goto search_free;
Eric Anholt673a3942008-07-30 12:06:12 -07002552 }
2553 atomic_inc(&dev->gtt_count);
2554 atomic_add(obj->size, &dev->gtt_memory);
2555
Chris Wilsonbf1a1092010-08-07 11:01:20 +01002556 /* keep track of bounds object by adding it to the inactive list */
2557 list_add_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
2558
Eric Anholt673a3942008-07-30 12:06:12 -07002559 /* Assert that the object is not currently in any GPU domain. As it
2560 * wasn't in the GTT, there shouldn't be any way it could have been in
2561 * a GPU cache
2562 */
Chris Wilson21d509e2009-06-06 09:46:02 +01002563 BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS);
2564 BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS);
Eric Anholt673a3942008-07-30 12:06:12 -07002565
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002566 trace_i915_gem_object_bind(obj, obj_priv->gtt_offset);
2567
Eric Anholt673a3942008-07-30 12:06:12 -07002568 return 0;
2569}
2570
2571void
2572i915_gem_clflush_object(struct drm_gem_object *obj)
2573{
Daniel Vetter23010e42010-03-08 13:35:02 +01002574 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002575
2576 /* If we don't have a page list set up, then we're not pinned
2577 * to GPU, and we can ignore the cache flush because it'll happen
2578 * again at bind time.
2579 */
Eric Anholt856fa192009-03-19 14:10:50 -07002580 if (obj_priv->pages == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002581 return;
2582
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002583 trace_i915_gem_object_clflush(obj);
Eric Anholtcfa16a02009-05-26 18:46:16 -07002584
Eric Anholt856fa192009-03-19 14:10:50 -07002585 drm_clflush_pages(obj_priv->pages, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002586}
2587
Eric Anholte47c68e2008-11-14 13:35:19 -08002588/** Flushes any GPU write domain for the object if it's dirty. */
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002589static int
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002590i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj,
2591 bool pipelined)
Eric Anholte47c68e2008-11-14 13:35:19 -08002592{
2593 struct drm_device *dev = obj->dev;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002594 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002595
2596 if ((obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002597 return 0;
Eric Anholte47c68e2008-11-14 13:35:19 -08002598
2599 /* Queue the GPU write cache flushing we need. */
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002600 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002601 i915_gem_flush(dev, 0, obj->write_domain);
Chris Wilson48b956c2010-09-14 12:50:34 +01002602 BUG_ON(obj->write_domain);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002603
2604 trace_i915_gem_object_change_domain(obj,
2605 obj->read_domains,
2606 old_write_domain);
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002607
2608 if (pipelined)
2609 return 0;
2610
Chris Wilson2cf34d72010-09-14 13:03:28 +01002611 return i915_gem_object_wait_rendering(obj, true);
Eric Anholte47c68e2008-11-14 13:35:19 -08002612}
2613
2614/** Flushes the GTT write domain for the object if it's dirty. */
2615static void
2616i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj)
2617{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002618 uint32_t old_write_domain;
2619
Eric Anholte47c68e2008-11-14 13:35:19 -08002620 if (obj->write_domain != I915_GEM_DOMAIN_GTT)
2621 return;
2622
2623 /* No actual flushing is required for the GTT write domain. Writes
2624 * to it immediately go to main memory as far as we know, so there's
2625 * no chipset flush. It also doesn't land in render cache.
2626 */
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002627 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002628 obj->write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002629
2630 trace_i915_gem_object_change_domain(obj,
2631 obj->read_domains,
2632 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002633}
2634
2635/** Flushes the CPU write domain for the object if it's dirty. */
2636static void
2637i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj)
2638{
2639 struct drm_device *dev = obj->dev;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002640 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002641
2642 if (obj->write_domain != I915_GEM_DOMAIN_CPU)
2643 return;
2644
2645 i915_gem_clflush_object(obj);
2646 drm_agp_chipset_flush(dev);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002647 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002648 obj->write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002649
2650 trace_i915_gem_object_change_domain(obj,
2651 obj->read_domains,
2652 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002653}
2654
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002655/**
2656 * Moves a single object to the GTT read, and possibly write domain.
2657 *
2658 * This function returns when the move is complete, including waiting on
2659 * flushes to occur.
2660 */
Jesse Barnes79e53942008-11-07 14:24:08 -08002661int
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002662i915_gem_object_set_to_gtt_domain(struct drm_gem_object *obj, int write)
2663{
Daniel Vetter23010e42010-03-08 13:35:02 +01002664 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002665 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002666 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002667
Eric Anholt02354392008-11-26 13:58:13 -08002668 /* Not valid to be called on unbound objects. */
2669 if (obj_priv->gtt_space == NULL)
2670 return -EINVAL;
2671
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002672 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Eric Anholte47c68e2008-11-14 13:35:19 -08002673 if (ret != 0)
2674 return ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002675
Chris Wilson72133422010-09-13 23:56:38 +01002676 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002677
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002678 if (write) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01002679 ret = i915_gem_object_wait_rendering(obj, true);
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002680 if (ret)
2681 return ret;
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002682 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002683
Chris Wilson72133422010-09-13 23:56:38 +01002684 old_write_domain = obj->write_domain;
2685 old_read_domains = obj->read_domains;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002686
2687 /* It should now be out of any other write domains, and we can update
2688 * the domain values for our changes.
2689 */
2690 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2691 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002692 if (write) {
Chris Wilson72133422010-09-13 23:56:38 +01002693 obj->read_domains = I915_GEM_DOMAIN_GTT;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002694 obj->write_domain = I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002695 obj_priv->dirty = 1;
2696 }
2697
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002698 trace_i915_gem_object_change_domain(obj,
2699 old_read_domains,
2700 old_write_domain);
2701
Eric Anholte47c68e2008-11-14 13:35:19 -08002702 return 0;
2703}
2704
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002705/*
2706 * Prepare buffer for display plane. Use uninterruptible for possible flush
2707 * wait, as in modesetting process we're not supposed to be interrupted.
2708 */
2709int
Chris Wilson48b956c2010-09-14 12:50:34 +01002710i915_gem_object_set_to_display_plane(struct drm_gem_object *obj,
2711 bool pipelined)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002712{
Daniel Vetter23010e42010-03-08 13:35:02 +01002713 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002714 uint32_t old_read_domains;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002715 int ret;
2716
2717 /* Not valid to be called on unbound objects. */
2718 if (obj_priv->gtt_space == NULL)
2719 return -EINVAL;
2720
Chris Wilson48b956c2010-09-14 12:50:34 +01002721 ret = i915_gem_object_flush_gpu_write_domain(obj, pipelined);
2722 if (ret)
Daniel Vettere35a41d2010-02-11 22:13:59 +01002723 return ret;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002724
Chris Wilsonb118c1e2010-05-27 13:18:14 +01002725 i915_gem_object_flush_cpu_write_domain(obj);
2726
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002727 old_read_domains = obj->read_domains;
Chris Wilsonb118c1e2010-05-27 13:18:14 +01002728 obj->read_domains = I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002729
2730 trace_i915_gem_object_change_domain(obj,
2731 old_read_domains,
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002732 obj->write_domain);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002733
2734 return 0;
2735}
2736
Eric Anholte47c68e2008-11-14 13:35:19 -08002737/**
2738 * Moves a single object to the CPU read, and possibly write domain.
2739 *
2740 * This function returns when the move is complete, including waiting on
2741 * flushes to occur.
2742 */
2743static int
2744i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj, int write)
2745{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002746 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002747 int ret;
2748
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002749 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Eric Anholte47c68e2008-11-14 13:35:19 -08002750 if (ret != 0)
2751 return ret;
2752
2753 i915_gem_object_flush_gtt_write_domain(obj);
2754
2755 /* If we have a partially-valid cache of the object in the CPU,
2756 * finish invalidating it and free the per-page flags.
2757 */
2758 i915_gem_object_set_to_full_cpu_read_domain(obj);
2759
Chris Wilson72133422010-09-13 23:56:38 +01002760 if (write) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01002761 ret = i915_gem_object_wait_rendering(obj, true);
Chris Wilson72133422010-09-13 23:56:38 +01002762 if (ret)
2763 return ret;
2764 }
2765
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002766 old_write_domain = obj->write_domain;
2767 old_read_domains = obj->read_domains;
2768
Eric Anholte47c68e2008-11-14 13:35:19 -08002769 /* Flush the CPU cache if it's still invalid. */
2770 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
2771 i915_gem_clflush_object(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08002772
2773 obj->read_domains |= I915_GEM_DOMAIN_CPU;
2774 }
2775
2776 /* It should now be out of any other write domains, and we can update
2777 * the domain values for our changes.
2778 */
2779 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
2780
2781 /* If we're writing through the CPU, then the GPU read domains will
2782 * need to be invalidated at next use.
2783 */
2784 if (write) {
2785 obj->read_domains &= I915_GEM_DOMAIN_CPU;
2786 obj->write_domain = I915_GEM_DOMAIN_CPU;
2787 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002788
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002789 trace_i915_gem_object_change_domain(obj,
2790 old_read_domains,
2791 old_write_domain);
2792
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002793 return 0;
2794}
2795
Eric Anholt673a3942008-07-30 12:06:12 -07002796/*
2797 * Set the next domain for the specified object. This
2798 * may not actually perform the necessary flushing/invaliding though,
2799 * as that may want to be batched with other set_domain operations
2800 *
2801 * This is (we hope) the only really tricky part of gem. The goal
2802 * is fairly simple -- track which caches hold bits of the object
2803 * and make sure they remain coherent. A few concrete examples may
2804 * help to explain how it works. For shorthand, we use the notation
2805 * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
2806 * a pair of read and write domain masks.
2807 *
2808 * Case 1: the batch buffer
2809 *
2810 * 1. Allocated
2811 * 2. Written by CPU
2812 * 3. Mapped to GTT
2813 * 4. Read by GPU
2814 * 5. Unmapped from GTT
2815 * 6. Freed
2816 *
2817 * Let's take these a step at a time
2818 *
2819 * 1. Allocated
2820 * Pages allocated from the kernel may still have
2821 * cache contents, so we set them to (CPU, CPU) always.
2822 * 2. Written by CPU (using pwrite)
2823 * The pwrite function calls set_domain (CPU, CPU) and
2824 * this function does nothing (as nothing changes)
2825 * 3. Mapped by GTT
2826 * This function asserts that the object is not
2827 * currently in any GPU-based read or write domains
2828 * 4. Read by GPU
2829 * i915_gem_execbuffer calls set_domain (COMMAND, 0).
2830 * As write_domain is zero, this function adds in the
2831 * current read domains (CPU+COMMAND, 0).
2832 * flush_domains is set to CPU.
2833 * invalidate_domains is set to COMMAND
2834 * clflush is run to get data out of the CPU caches
2835 * then i915_dev_set_domain calls i915_gem_flush to
2836 * emit an MI_FLUSH and drm_agp_chipset_flush
2837 * 5. Unmapped from GTT
2838 * i915_gem_object_unbind calls set_domain (CPU, CPU)
2839 * flush_domains and invalidate_domains end up both zero
2840 * so no flushing/invalidating happens
2841 * 6. Freed
2842 * yay, done
2843 *
2844 * Case 2: The shared render buffer
2845 *
2846 * 1. Allocated
2847 * 2. Mapped to GTT
2848 * 3. Read/written by GPU
2849 * 4. set_domain to (CPU,CPU)
2850 * 5. Read/written by CPU
2851 * 6. Read/written by GPU
2852 *
2853 * 1. Allocated
2854 * Same as last example, (CPU, CPU)
2855 * 2. Mapped to GTT
2856 * Nothing changes (assertions find that it is not in the GPU)
2857 * 3. Read/written by GPU
2858 * execbuffer calls set_domain (RENDER, RENDER)
2859 * flush_domains gets CPU
2860 * invalidate_domains gets GPU
2861 * clflush (obj)
2862 * MI_FLUSH and drm_agp_chipset_flush
2863 * 4. set_domain (CPU, CPU)
2864 * flush_domains gets GPU
2865 * invalidate_domains gets CPU
2866 * wait_rendering (obj) to make sure all drawing is complete.
2867 * This will include an MI_FLUSH to get the data from GPU
2868 * to memory
2869 * clflush (obj) to invalidate the CPU cache
2870 * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
2871 * 5. Read/written by CPU
2872 * cache lines are loaded and dirtied
2873 * 6. Read written by GPU
2874 * Same as last GPU access
2875 *
2876 * Case 3: The constant buffer
2877 *
2878 * 1. Allocated
2879 * 2. Written by CPU
2880 * 3. Read by GPU
2881 * 4. Updated (written) by CPU again
2882 * 5. Read by GPU
2883 *
2884 * 1. Allocated
2885 * (CPU, CPU)
2886 * 2. Written by CPU
2887 * (CPU, CPU)
2888 * 3. Read by GPU
2889 * (CPU+RENDER, 0)
2890 * flush_domains = CPU
2891 * invalidate_domains = RENDER
2892 * clflush (obj)
2893 * MI_FLUSH
2894 * drm_agp_chipset_flush
2895 * 4. Updated (written) by CPU again
2896 * (CPU, CPU)
2897 * flush_domains = 0 (no previous write domain)
2898 * invalidate_domains = 0 (no new read domains)
2899 * 5. Read by GPU
2900 * (CPU+RENDER, 0)
2901 * flush_domains = CPU
2902 * invalidate_domains = RENDER
2903 * clflush (obj)
2904 * MI_FLUSH
2905 * drm_agp_chipset_flush
2906 */
Keith Packardc0d90822008-11-20 23:11:08 -08002907static void
Eric Anholt8b0e3782009-02-19 14:40:50 -08002908i915_gem_object_set_to_gpu_domain(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002909{
2910 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01002911 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002912 uint32_t invalidate_domains = 0;
2913 uint32_t flush_domains = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002914 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002915
Eric Anholt8b0e3782009-02-19 14:40:50 -08002916 BUG_ON(obj->pending_read_domains & I915_GEM_DOMAIN_CPU);
2917 BUG_ON(obj->pending_write_domain == I915_GEM_DOMAIN_CPU);
Eric Anholt673a3942008-07-30 12:06:12 -07002918
Jesse Barnes652c3932009-08-17 13:31:43 -07002919 intel_mark_busy(dev, obj);
2920
Eric Anholt673a3942008-07-30 12:06:12 -07002921#if WATCH_BUF
2922 DRM_INFO("%s: object %p read %08x -> %08x write %08x -> %08x\n",
2923 __func__, obj,
Eric Anholt8b0e3782009-02-19 14:40:50 -08002924 obj->read_domains, obj->pending_read_domains,
2925 obj->write_domain, obj->pending_write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07002926#endif
2927 /*
2928 * If the object isn't moving to a new write domain,
2929 * let the object stay in multiple read domains
2930 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08002931 if (obj->pending_write_domain == 0)
2932 obj->pending_read_domains |= obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07002933 else
2934 obj_priv->dirty = 1;
2935
2936 /*
2937 * Flush the current write domain if
2938 * the new read domains don't match. Invalidate
2939 * any read domains which differ from the old
2940 * write domain
2941 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08002942 if (obj->write_domain &&
2943 obj->write_domain != obj->pending_read_domains) {
Eric Anholt673a3942008-07-30 12:06:12 -07002944 flush_domains |= obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08002945 invalidate_domains |=
2946 obj->pending_read_domains & ~obj->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07002947 }
2948 /*
2949 * Invalidate any read caches which may have
2950 * stale data. That is, any new read domains.
2951 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08002952 invalidate_domains |= obj->pending_read_domains & ~obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07002953 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU) {
2954#if WATCH_BUF
2955 DRM_INFO("%s: CPU domain flush %08x invalidate %08x\n",
2956 __func__, flush_domains, invalidate_domains);
2957#endif
Eric Anholt673a3942008-07-30 12:06:12 -07002958 i915_gem_clflush_object(obj);
2959 }
2960
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002961 old_read_domains = obj->read_domains;
2962
Eric Anholtefbeed92009-02-19 14:54:51 -08002963 /* The actual obj->write_domain will be updated with
2964 * pending_write_domain after we emit the accumulated flush for all
2965 * of our domain changes in execbuffers (which clears objects'
2966 * write_domains). So if we have a current write domain that we
2967 * aren't changing, set pending_write_domain to that.
2968 */
2969 if (flush_domains == 0 && obj->pending_write_domain == 0)
2970 obj->pending_write_domain = obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08002971 obj->read_domains = obj->pending_read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07002972
2973 dev->invalidate_domains |= invalidate_domains;
2974 dev->flush_domains |= flush_domains;
2975#if WATCH_BUF
2976 DRM_INFO("%s: read %08x write %08x invalidate %08x flush %08x\n",
2977 __func__,
2978 obj->read_domains, obj->write_domain,
2979 dev->invalidate_domains, dev->flush_domains);
2980#endif
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002981
2982 trace_i915_gem_object_change_domain(obj,
2983 old_read_domains,
2984 obj->write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07002985}
2986
2987/**
Eric Anholte47c68e2008-11-14 13:35:19 -08002988 * Moves the object from a partially CPU read to a full one.
Eric Anholt673a3942008-07-30 12:06:12 -07002989 *
Eric Anholte47c68e2008-11-14 13:35:19 -08002990 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
2991 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
2992 */
2993static void
2994i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj)
2995{
Daniel Vetter23010e42010-03-08 13:35:02 +01002996 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08002997
2998 if (!obj_priv->page_cpu_valid)
2999 return;
3000
3001 /* If we're partially in the CPU read domain, finish moving it in.
3002 */
3003 if (obj->read_domains & I915_GEM_DOMAIN_CPU) {
3004 int i;
3005
3006 for (i = 0; i <= (obj->size - 1) / PAGE_SIZE; i++) {
3007 if (obj_priv->page_cpu_valid[i])
3008 continue;
Eric Anholt856fa192009-03-19 14:10:50 -07003009 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholte47c68e2008-11-14 13:35:19 -08003010 }
Eric Anholte47c68e2008-11-14 13:35:19 -08003011 }
3012
3013 /* Free the page_cpu_valid mappings which are now stale, whether
3014 * or not we've got I915_GEM_DOMAIN_CPU.
3015 */
Eric Anholt9a298b22009-03-24 12:23:04 -07003016 kfree(obj_priv->page_cpu_valid);
Eric Anholte47c68e2008-11-14 13:35:19 -08003017 obj_priv->page_cpu_valid = NULL;
3018}
3019
3020/**
3021 * Set the CPU read domain on a range of the object.
3022 *
3023 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
3024 * not entirely valid. The page_cpu_valid member of the object flags which
3025 * pages have been flushed, and will be respected by
3026 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
3027 * of the whole object.
3028 *
3029 * This function returns when the move is complete, including waiting on
3030 * flushes to occur.
Eric Anholt673a3942008-07-30 12:06:12 -07003031 */
3032static int
Eric Anholte47c68e2008-11-14 13:35:19 -08003033i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
3034 uint64_t offset, uint64_t size)
Eric Anholt673a3942008-07-30 12:06:12 -07003035{
Daniel Vetter23010e42010-03-08 13:35:02 +01003036 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003037 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003038 int i, ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003039
Eric Anholte47c68e2008-11-14 13:35:19 -08003040 if (offset == 0 && size == obj->size)
3041 return i915_gem_object_set_to_cpu_domain(obj, 0);
3042
Daniel Vetterba3d8d72010-02-11 22:37:04 +01003043 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Eric Anholte47c68e2008-11-14 13:35:19 -08003044 if (ret != 0)
3045 return ret;
3046 i915_gem_object_flush_gtt_write_domain(obj);
3047
3048 /* If we're already fully in the CPU read domain, we're done. */
3049 if (obj_priv->page_cpu_valid == NULL &&
3050 (obj->read_domains & I915_GEM_DOMAIN_CPU) != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07003051 return 0;
3052
Eric Anholte47c68e2008-11-14 13:35:19 -08003053 /* Otherwise, create/clear the per-page CPU read domain flag if we're
3054 * newly adding I915_GEM_DOMAIN_CPU
3055 */
Eric Anholt673a3942008-07-30 12:06:12 -07003056 if (obj_priv->page_cpu_valid == NULL) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003057 obj_priv->page_cpu_valid = kzalloc(obj->size / PAGE_SIZE,
3058 GFP_KERNEL);
Eric Anholte47c68e2008-11-14 13:35:19 -08003059 if (obj_priv->page_cpu_valid == NULL)
3060 return -ENOMEM;
3061 } else if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0)
3062 memset(obj_priv->page_cpu_valid, 0, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07003063
3064 /* Flush the cache on any pages that are still invalid from the CPU's
3065 * perspective.
3066 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003067 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
3068 i++) {
Eric Anholt673a3942008-07-30 12:06:12 -07003069 if (obj_priv->page_cpu_valid[i])
3070 continue;
3071
Eric Anholt856fa192009-03-19 14:10:50 -07003072 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07003073
3074 obj_priv->page_cpu_valid[i] = 1;
3075 }
3076
Eric Anholte47c68e2008-11-14 13:35:19 -08003077 /* It should now be out of any other write domains, and we can update
3078 * the domain values for our changes.
3079 */
3080 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
3081
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003082 old_read_domains = obj->read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003083 obj->read_domains |= I915_GEM_DOMAIN_CPU;
3084
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003085 trace_i915_gem_object_change_domain(obj,
3086 old_read_domains,
3087 obj->write_domain);
3088
Eric Anholt673a3942008-07-30 12:06:12 -07003089 return 0;
3090}
3091
3092/**
Eric Anholt673a3942008-07-30 12:06:12 -07003093 * Pin an object to the GTT and evaluate the relocations landing in it.
3094 */
3095static int
3096i915_gem_object_pin_and_relocate(struct drm_gem_object *obj,
3097 struct drm_file *file_priv,
Jesse Barnes76446ca2009-12-17 22:05:42 -05003098 struct drm_i915_gem_exec_object2 *entry,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003099 struct drm_i915_gem_relocation_entry *relocs)
Eric Anholt673a3942008-07-30 12:06:12 -07003100{
3101 struct drm_device *dev = obj->dev;
Keith Packard0839ccb2008-10-30 19:38:48 -07003102 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01003103 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003104 int i, ret;
Keith Packard0839ccb2008-10-30 19:38:48 -07003105 void __iomem *reloc_page;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003106 bool need_fence;
3107
3108 need_fence = entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
3109 obj_priv->tiling_mode != I915_TILING_NONE;
3110
3111 /* Check fence reg constraints and rebind if necessary */
Chris Wilson808b24d62010-05-27 13:18:15 +01003112 if (need_fence &&
3113 !i915_gem_object_fence_offset_ok(obj,
3114 obj_priv->tiling_mode)) {
3115 ret = i915_gem_object_unbind(obj);
3116 if (ret)
3117 return ret;
3118 }
Eric Anholt673a3942008-07-30 12:06:12 -07003119
3120 /* Choose the GTT offset for our buffer and put it there. */
3121 ret = i915_gem_object_pin(obj, (uint32_t) entry->alignment);
3122 if (ret)
3123 return ret;
3124
Jesse Barnes76446ca2009-12-17 22:05:42 -05003125 /*
3126 * Pre-965 chips need a fence register set up in order to
3127 * properly handle blits to/from tiled surfaces.
3128 */
3129 if (need_fence) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01003130 ret = i915_gem_object_get_fence_reg(obj, false);
Jesse Barnes76446ca2009-12-17 22:05:42 -05003131 if (ret != 0) {
Jesse Barnes76446ca2009-12-17 22:05:42 -05003132 i915_gem_object_unpin(obj);
3133 return ret;
3134 }
3135 }
3136
Eric Anholt673a3942008-07-30 12:06:12 -07003137 entry->offset = obj_priv->gtt_offset;
3138
Eric Anholt673a3942008-07-30 12:06:12 -07003139 /* Apply the relocations, using the GTT aperture to avoid cache
3140 * flushing requirements.
3141 */
3142 for (i = 0; i < entry->relocation_count; i++) {
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003143 struct drm_i915_gem_relocation_entry *reloc= &relocs[i];
Eric Anholt673a3942008-07-30 12:06:12 -07003144 struct drm_gem_object *target_obj;
3145 struct drm_i915_gem_object *target_obj_priv;
Eric Anholt3043c602008-10-02 12:24:47 -07003146 uint32_t reloc_val, reloc_offset;
3147 uint32_t __iomem *reloc_entry;
Eric Anholt673a3942008-07-30 12:06:12 -07003148
Eric Anholt673a3942008-07-30 12:06:12 -07003149 target_obj = drm_gem_object_lookup(obj->dev, file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003150 reloc->target_handle);
Eric Anholt673a3942008-07-30 12:06:12 -07003151 if (target_obj == NULL) {
3152 i915_gem_object_unpin(obj);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01003153 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07003154 }
Daniel Vetter23010e42010-03-08 13:35:02 +01003155 target_obj_priv = to_intel_bo(target_obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003156
Chris Wilson8542a0b2009-09-09 21:15:15 +01003157#if WATCH_RELOC
3158 DRM_INFO("%s: obj %p offset %08x target %d "
3159 "read %08x write %08x gtt %08x "
3160 "presumed %08x delta %08x\n",
3161 __func__,
3162 obj,
3163 (int) reloc->offset,
3164 (int) reloc->target_handle,
3165 (int) reloc->read_domains,
3166 (int) reloc->write_domain,
3167 (int) target_obj_priv->gtt_offset,
3168 (int) reloc->presumed_offset,
3169 reloc->delta);
3170#endif
3171
Eric Anholt673a3942008-07-30 12:06:12 -07003172 /* The target buffer should have appeared before us in the
3173 * exec_object list, so it should have a GTT space bound by now.
3174 */
3175 if (target_obj_priv->gtt_space == NULL) {
3176 DRM_ERROR("No GTT space found for object %d\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003177 reloc->target_handle);
Eric Anholt673a3942008-07-30 12:06:12 -07003178 drm_gem_object_unreference(target_obj);
3179 i915_gem_object_unpin(obj);
3180 return -EINVAL;
3181 }
3182
Chris Wilson8542a0b2009-09-09 21:15:15 +01003183 /* Validate that the target is in a valid r/w GPU domain */
Daniel Vetter16edd552010-02-19 11:52:02 +01003184 if (reloc->write_domain & (reloc->write_domain - 1)) {
3185 DRM_ERROR("reloc with multiple write domains: "
3186 "obj %p target %d offset %d "
3187 "read %08x write %08x",
3188 obj, reloc->target_handle,
3189 (int) reloc->offset,
3190 reloc->read_domains,
3191 reloc->write_domain);
3192 return -EINVAL;
3193 }
Chris Wilson8542a0b2009-09-09 21:15:15 +01003194 if (reloc->write_domain & I915_GEM_DOMAIN_CPU ||
3195 reloc->read_domains & I915_GEM_DOMAIN_CPU) {
3196 DRM_ERROR("reloc with read/write CPU domains: "
3197 "obj %p target %d offset %d "
3198 "read %08x write %08x",
3199 obj, reloc->target_handle,
3200 (int) reloc->offset,
3201 reloc->read_domains,
3202 reloc->write_domain);
3203 drm_gem_object_unreference(target_obj);
3204 i915_gem_object_unpin(obj);
3205 return -EINVAL;
3206 }
3207 if (reloc->write_domain && target_obj->pending_write_domain &&
3208 reloc->write_domain != target_obj->pending_write_domain) {
3209 DRM_ERROR("Write domain conflict: "
3210 "obj %p target %d offset %d "
3211 "new %08x old %08x\n",
3212 obj, reloc->target_handle,
3213 (int) reloc->offset,
3214 reloc->write_domain,
3215 target_obj->pending_write_domain);
3216 drm_gem_object_unreference(target_obj);
3217 i915_gem_object_unpin(obj);
3218 return -EINVAL;
3219 }
3220
3221 target_obj->pending_read_domains |= reloc->read_domains;
3222 target_obj->pending_write_domain |= reloc->write_domain;
3223
3224 /* If the relocation already has the right value in it, no
3225 * more work needs to be done.
3226 */
3227 if (target_obj_priv->gtt_offset == reloc->presumed_offset) {
3228 drm_gem_object_unreference(target_obj);
3229 continue;
3230 }
3231
3232 /* Check that the relocation address is valid... */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003233 if (reloc->offset > obj->size - 4) {
Eric Anholt673a3942008-07-30 12:06:12 -07003234 DRM_ERROR("Relocation beyond object bounds: "
3235 "obj %p target %d offset %d size %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003236 obj, reloc->target_handle,
3237 (int) reloc->offset, (int) obj->size);
Eric Anholt673a3942008-07-30 12:06:12 -07003238 drm_gem_object_unreference(target_obj);
3239 i915_gem_object_unpin(obj);
3240 return -EINVAL;
3241 }
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003242 if (reloc->offset & 3) {
Eric Anholt673a3942008-07-30 12:06:12 -07003243 DRM_ERROR("Relocation not 4-byte aligned: "
3244 "obj %p target %d offset %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003245 obj, reloc->target_handle,
3246 (int) reloc->offset);
Eric Anholt673a3942008-07-30 12:06:12 -07003247 drm_gem_object_unreference(target_obj);
3248 i915_gem_object_unpin(obj);
3249 return -EINVAL;
3250 }
3251
Chris Wilson8542a0b2009-09-09 21:15:15 +01003252 /* and points to somewhere within the target object. */
Chris Wilsoncd0b9fb2009-09-15 23:23:18 +01003253 if (reloc->delta >= target_obj->size) {
3254 DRM_ERROR("Relocation beyond target object bounds: "
3255 "obj %p target %d delta %d size %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003256 obj, reloc->target_handle,
Chris Wilsoncd0b9fb2009-09-15 23:23:18 +01003257 (int) reloc->delta, (int) target_obj->size);
Chris Wilson491152b2009-02-11 14:26:32 +00003258 drm_gem_object_unreference(target_obj);
3259 i915_gem_object_unpin(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003260 return -EINVAL;
3261 }
3262
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003263 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
3264 if (ret != 0) {
3265 drm_gem_object_unreference(target_obj);
3266 i915_gem_object_unpin(obj);
3267 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -07003268 }
3269
3270 /* Map the page containing the relocation we're going to
3271 * perform.
3272 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003273 reloc_offset = obj_priv->gtt_offset + reloc->offset;
Keith Packard0839ccb2008-10-30 19:38:48 -07003274 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
3275 (reloc_offset &
Chris Wilsonfca3ec02010-08-04 14:34:24 +01003276 ~(PAGE_SIZE - 1)),
3277 KM_USER0);
Eric Anholt3043c602008-10-02 12:24:47 -07003278 reloc_entry = (uint32_t __iomem *)(reloc_page +
Keith Packard0839ccb2008-10-30 19:38:48 -07003279 (reloc_offset & (PAGE_SIZE - 1)));
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003280 reloc_val = target_obj_priv->gtt_offset + reloc->delta;
Eric Anholt673a3942008-07-30 12:06:12 -07003281
3282#if WATCH_BUF
3283 DRM_INFO("Applied relocation: %p@0x%08x %08x -> %08x\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003284 obj, (unsigned int) reloc->offset,
Eric Anholt673a3942008-07-30 12:06:12 -07003285 readl(reloc_entry), reloc_val);
3286#endif
3287 writel(reloc_val, reloc_entry);
Chris Wilsonfca3ec02010-08-04 14:34:24 +01003288 io_mapping_unmap_atomic(reloc_page, KM_USER0);
Eric Anholt673a3942008-07-30 12:06:12 -07003289
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003290 /* The updated presumed offset for this entry will be
3291 * copied back out to the user.
Eric Anholt673a3942008-07-30 12:06:12 -07003292 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003293 reloc->presumed_offset = target_obj_priv->gtt_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003294
3295 drm_gem_object_unreference(target_obj);
3296 }
3297
Eric Anholt673a3942008-07-30 12:06:12 -07003298#if WATCH_BUF
3299 if (0)
3300 i915_gem_dump_object(obj, 128, __func__, ~0);
3301#endif
3302 return 0;
3303}
3304
Eric Anholt673a3942008-07-30 12:06:12 -07003305/* Throttle our rendering by waiting until the ring has completed our requests
3306 * emitted over 20 msec ago.
3307 *
Eric Anholtb9624422009-06-03 07:27:35 +00003308 * Note that if we were to use the current jiffies each time around the loop,
3309 * we wouldn't escape the function with any frames outstanding if the time to
3310 * render a frame was over 20ms.
3311 *
Eric Anholt673a3942008-07-30 12:06:12 -07003312 * This should get us reasonable parallelism between CPU and GPU but also
3313 * relatively low latency when blocking on a particular request to finish.
3314 */
3315static int
3316i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file_priv)
3317{
3318 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
3319 int ret = 0;
Eric Anholtb9624422009-06-03 07:27:35 +00003320 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
Eric Anholt673a3942008-07-30 12:06:12 -07003321
3322 mutex_lock(&dev->struct_mutex);
Eric Anholtb9624422009-06-03 07:27:35 +00003323 while (!list_empty(&i915_file_priv->mm.request_list)) {
3324 struct drm_i915_gem_request *request;
3325
3326 request = list_first_entry(&i915_file_priv->mm.request_list,
3327 struct drm_i915_gem_request,
3328 client_list);
3329
3330 if (time_after_eq(request->emitted_jiffies, recent_enough))
3331 break;
3332
Zou Nan hai852835f2010-05-21 09:08:56 +08003333 ret = i915_wait_request(dev, request->seqno, request->ring);
Eric Anholtb9624422009-06-03 07:27:35 +00003334 if (ret != 0)
3335 break;
3336 }
Eric Anholt673a3942008-07-30 12:06:12 -07003337 mutex_unlock(&dev->struct_mutex);
Eric Anholtb9624422009-06-03 07:27:35 +00003338
Eric Anholt673a3942008-07-30 12:06:12 -07003339 return ret;
3340}
3341
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003342static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003343i915_gem_get_relocs_from_user(struct drm_i915_gem_exec_object2 *exec_list,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003344 uint32_t buffer_count,
3345 struct drm_i915_gem_relocation_entry **relocs)
3346{
3347 uint32_t reloc_count = 0, reloc_index = 0, i;
3348 int ret;
3349
3350 *relocs = NULL;
3351 for (i = 0; i < buffer_count; i++) {
3352 if (reloc_count + exec_list[i].relocation_count < reloc_count)
3353 return -EINVAL;
3354 reloc_count += exec_list[i].relocation_count;
3355 }
3356
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003357 *relocs = drm_calloc_large(reloc_count, sizeof(**relocs));
Jesse Barnes76446ca2009-12-17 22:05:42 -05003358 if (*relocs == NULL) {
3359 DRM_ERROR("failed to alloc relocs, count %d\n", reloc_count);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003360 return -ENOMEM;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003361 }
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003362
3363 for (i = 0; i < buffer_count; i++) {
3364 struct drm_i915_gem_relocation_entry __user *user_relocs;
3365
3366 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3367
3368 ret = copy_from_user(&(*relocs)[reloc_index],
3369 user_relocs,
3370 exec_list[i].relocation_count *
3371 sizeof(**relocs));
3372 if (ret != 0) {
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003373 drm_free_large(*relocs);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003374 *relocs = NULL;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003375 return -EFAULT;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003376 }
3377
3378 reloc_index += exec_list[i].relocation_count;
3379 }
3380
Florian Mickler2bc43b52009-04-06 22:55:41 +02003381 return 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003382}
3383
3384static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003385i915_gem_put_relocs_to_user(struct drm_i915_gem_exec_object2 *exec_list,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003386 uint32_t buffer_count,
3387 struct drm_i915_gem_relocation_entry *relocs)
3388{
3389 uint32_t reloc_count = 0, i;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003390 int ret = 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003391
Chris Wilson93533c22010-01-31 10:40:48 +00003392 if (relocs == NULL)
3393 return 0;
3394
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003395 for (i = 0; i < buffer_count; i++) {
3396 struct drm_i915_gem_relocation_entry __user *user_relocs;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003397 int unwritten;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003398
3399 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3400
Florian Mickler2bc43b52009-04-06 22:55:41 +02003401 unwritten = copy_to_user(user_relocs,
3402 &relocs[reloc_count],
3403 exec_list[i].relocation_count *
3404 sizeof(*relocs));
3405
3406 if (unwritten) {
3407 ret = -EFAULT;
3408 goto err;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003409 }
3410
3411 reloc_count += exec_list[i].relocation_count;
3412 }
3413
Florian Mickler2bc43b52009-04-06 22:55:41 +02003414err:
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003415 drm_free_large(relocs);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003416
3417 return ret;
3418}
3419
Chris Wilson83d60792009-06-06 09:45:57 +01003420static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003421i915_gem_check_execbuffer (struct drm_i915_gem_execbuffer2 *exec,
Chris Wilson83d60792009-06-06 09:45:57 +01003422 uint64_t exec_offset)
3423{
3424 uint32_t exec_start, exec_len;
3425
3426 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
3427 exec_len = (uint32_t) exec->batch_len;
3428
3429 if ((exec_start | exec_len) & 0x7)
3430 return -EINVAL;
3431
3432 if (!exec_start)
3433 return -EINVAL;
3434
3435 return 0;
3436}
3437
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003438static int
3439i915_gem_wait_for_pending_flip(struct drm_device *dev,
3440 struct drm_gem_object **object_list,
3441 int count)
3442{
3443 drm_i915_private_t *dev_priv = dev->dev_private;
3444 struct drm_i915_gem_object *obj_priv;
3445 DEFINE_WAIT(wait);
3446 int i, ret = 0;
3447
3448 for (;;) {
3449 prepare_to_wait(&dev_priv->pending_flip_queue,
3450 &wait, TASK_INTERRUPTIBLE);
3451 for (i = 0; i < count; i++) {
Daniel Vetter23010e42010-03-08 13:35:02 +01003452 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003453 if (atomic_read(&obj_priv->pending_flip) > 0)
3454 break;
3455 }
3456 if (i == count)
3457 break;
3458
3459 if (!signal_pending(current)) {
3460 mutex_unlock(&dev->struct_mutex);
3461 schedule();
3462 mutex_lock(&dev->struct_mutex);
3463 continue;
3464 }
3465 ret = -ERESTARTSYS;
3466 break;
3467 }
3468 finish_wait(&dev_priv->pending_flip_queue, &wait);
3469
3470 return ret;
3471}
3472
Chris Wilson8dc5d142010-08-12 12:36:12 +01003473static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003474i915_gem_do_execbuffer(struct drm_device *dev, void *data,
3475 struct drm_file *file_priv,
3476 struct drm_i915_gem_execbuffer2 *args,
3477 struct drm_i915_gem_exec_object2 *exec_list)
Eric Anholt673a3942008-07-30 12:06:12 -07003478{
3479 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003480 struct drm_gem_object **object_list = NULL;
3481 struct drm_gem_object *batch_obj;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003482 struct drm_i915_gem_object *obj_priv;
Eric Anholt201361a2009-03-11 12:30:04 -07003483 struct drm_clip_rect *cliprects = NULL;
Chris Wilson93533c22010-01-31 10:40:48 +00003484 struct drm_i915_gem_relocation_entry *relocs = NULL;
Chris Wilson8dc5d142010-08-12 12:36:12 +01003485 struct drm_i915_gem_request *request = NULL;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003486 int ret = 0, ret2, i, pinned = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003487 uint64_t exec_offset;
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01003488 uint32_t seqno, reloc_index;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003489 int pin_tries, flips;
Eric Anholt673a3942008-07-30 12:06:12 -07003490
Zou Nan hai852835f2010-05-21 09:08:56 +08003491 struct intel_ring_buffer *ring = NULL;
3492
Eric Anholt673a3942008-07-30 12:06:12 -07003493#if WATCH_EXEC
3494 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3495 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3496#endif
Zou Nan haid1b851f2010-05-21 09:08:57 +08003497 if (args->flags & I915_EXEC_BSD) {
3498 if (!HAS_BSD(dev)) {
3499 DRM_ERROR("execbuf with wrong flag\n");
3500 return -EINVAL;
3501 }
3502 ring = &dev_priv->bsd_ring;
3503 } else {
3504 ring = &dev_priv->render_ring;
3505 }
3506
Eric Anholt4f481ed2008-09-10 14:22:49 -07003507 if (args->buffer_count < 1) {
3508 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3509 return -EINVAL;
3510 }
Eric Anholtc8e0f932009-11-22 03:49:37 +01003511 object_list = drm_malloc_ab(sizeof(*object_list), args->buffer_count);
Jesse Barnes76446ca2009-12-17 22:05:42 -05003512 if (object_list == NULL) {
3513 DRM_ERROR("Failed to allocate object list for %d buffers\n",
Eric Anholt673a3942008-07-30 12:06:12 -07003514 args->buffer_count);
3515 ret = -ENOMEM;
3516 goto pre_mutex_err;
3517 }
Eric Anholt673a3942008-07-30 12:06:12 -07003518
Eric Anholt201361a2009-03-11 12:30:04 -07003519 if (args->num_cliprects != 0) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003520 cliprects = kcalloc(args->num_cliprects, sizeof(*cliprects),
3521 GFP_KERNEL);
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003522 if (cliprects == NULL) {
3523 ret = -ENOMEM;
Eric Anholt201361a2009-03-11 12:30:04 -07003524 goto pre_mutex_err;
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003525 }
Eric Anholt201361a2009-03-11 12:30:04 -07003526
3527 ret = copy_from_user(cliprects,
3528 (struct drm_clip_rect __user *)
3529 (uintptr_t) args->cliprects_ptr,
3530 sizeof(*cliprects) * args->num_cliprects);
3531 if (ret != 0) {
3532 DRM_ERROR("copy %d cliprects failed: %d\n",
3533 args->num_cliprects, ret);
Dan Carpenterc877cdc2010-06-23 19:03:01 +02003534 ret = -EFAULT;
Eric Anholt201361a2009-03-11 12:30:04 -07003535 goto pre_mutex_err;
3536 }
3537 }
3538
Chris Wilson8dc5d142010-08-12 12:36:12 +01003539 request = kzalloc(sizeof(*request), GFP_KERNEL);
3540 if (request == NULL) {
3541 ret = -ENOMEM;
3542 goto pre_mutex_err;
3543 }
3544
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003545 ret = i915_gem_get_relocs_from_user(exec_list, args->buffer_count,
3546 &relocs);
3547 if (ret != 0)
3548 goto pre_mutex_err;
3549
Eric Anholt673a3942008-07-30 12:06:12 -07003550 mutex_lock(&dev->struct_mutex);
3551
3552 i915_verify_inactive(dev, __FILE__, __LINE__);
3553
Ben Gamariba1234d2009-09-14 17:48:47 -04003554 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07003555 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003556 ret = -EIO;
3557 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003558 }
3559
3560 if (dev_priv->mm.suspended) {
Eric Anholt673a3942008-07-30 12:06:12 -07003561 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003562 ret = -EBUSY;
3563 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003564 }
3565
Keith Packardac94a962008-11-20 23:30:27 -08003566 /* Look up object handles */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003567 flips = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003568 for (i = 0; i < args->buffer_count; i++) {
3569 object_list[i] = drm_gem_object_lookup(dev, file_priv,
3570 exec_list[i].handle);
3571 if (object_list[i] == NULL) {
3572 DRM_ERROR("Invalid object handle %d at index %d\n",
3573 exec_list[i].handle, i);
Chris Wilson0ce907f2010-01-23 20:26:35 +00003574 /* prevent error path from reading uninitialized data */
3575 args->buffer_count = i + 1;
Chris Wilsonbf79cb92010-08-04 14:19:46 +01003576 ret = -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07003577 goto err;
3578 }
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003579
Daniel Vetter23010e42010-03-08 13:35:02 +01003580 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003581 if (obj_priv->in_execbuffer) {
3582 DRM_ERROR("Object %p appears more than once in object list\n",
3583 object_list[i]);
Chris Wilson0ce907f2010-01-23 20:26:35 +00003584 /* prevent error path from reading uninitialized data */
3585 args->buffer_count = i + 1;
Chris Wilsonbf79cb92010-08-04 14:19:46 +01003586 ret = -EINVAL;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003587 goto err;
3588 }
3589 obj_priv->in_execbuffer = true;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003590 flips += atomic_read(&obj_priv->pending_flip);
3591 }
3592
3593 if (flips > 0) {
3594 ret = i915_gem_wait_for_pending_flip(dev, object_list,
3595 args->buffer_count);
3596 if (ret)
3597 goto err;
Keith Packardac94a962008-11-20 23:30:27 -08003598 }
Eric Anholt673a3942008-07-30 12:06:12 -07003599
Keith Packardac94a962008-11-20 23:30:27 -08003600 /* Pin and relocate */
3601 for (pin_tries = 0; ; pin_tries++) {
3602 ret = 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003603 reloc_index = 0;
3604
Keith Packardac94a962008-11-20 23:30:27 -08003605 for (i = 0; i < args->buffer_count; i++) {
3606 object_list[i]->pending_read_domains = 0;
3607 object_list[i]->pending_write_domain = 0;
3608 ret = i915_gem_object_pin_and_relocate(object_list[i],
3609 file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003610 &exec_list[i],
3611 &relocs[reloc_index]);
Keith Packardac94a962008-11-20 23:30:27 -08003612 if (ret)
3613 break;
3614 pinned = i + 1;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003615 reloc_index += exec_list[i].relocation_count;
Keith Packardac94a962008-11-20 23:30:27 -08003616 }
3617 /* success */
3618 if (ret == 0)
3619 break;
3620
3621 /* error other than GTT full, or we've already tried again */
Chris Wilson2939e1f2009-06-06 09:46:03 +01003622 if (ret != -ENOSPC || pin_tries >= 1) {
Chris Wilson07f73f62009-09-14 16:50:30 +01003623 if (ret != -ERESTARTSYS) {
3624 unsigned long long total_size = 0;
Chris Wilson3d1cc472010-05-27 13:18:19 +01003625 int num_fences = 0;
3626 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson43b27f42010-07-02 08:57:15 +01003627 obj_priv = to_intel_bo(object_list[i]);
Chris Wilson3d1cc472010-05-27 13:18:19 +01003628
Chris Wilson07f73f62009-09-14 16:50:30 +01003629 total_size += object_list[i]->size;
Chris Wilson3d1cc472010-05-27 13:18:19 +01003630 num_fences +=
3631 exec_list[i].flags & EXEC_OBJECT_NEEDS_FENCE &&
3632 obj_priv->tiling_mode != I915_TILING_NONE;
3633 }
3634 DRM_ERROR("Failed to pin buffer %d of %d, total %llu bytes, %d fences: %d\n",
Chris Wilson07f73f62009-09-14 16:50:30 +01003635 pinned+1, args->buffer_count,
Chris Wilson3d1cc472010-05-27 13:18:19 +01003636 total_size, num_fences,
3637 ret);
Chris Wilson07f73f62009-09-14 16:50:30 +01003638 DRM_ERROR("%d objects [%d pinned], "
3639 "%d object bytes [%d pinned], "
3640 "%d/%d gtt bytes\n",
3641 atomic_read(&dev->object_count),
3642 atomic_read(&dev->pin_count),
3643 atomic_read(&dev->object_memory),
3644 atomic_read(&dev->pin_memory),
3645 atomic_read(&dev->gtt_memory),
3646 dev->gtt_total);
3647 }
Eric Anholt673a3942008-07-30 12:06:12 -07003648 goto err;
3649 }
Keith Packardac94a962008-11-20 23:30:27 -08003650
3651 /* unpin all of our buffers */
3652 for (i = 0; i < pinned; i++)
3653 i915_gem_object_unpin(object_list[i]);
Eric Anholtb1177632008-12-10 10:09:41 -08003654 pinned = 0;
Keith Packardac94a962008-11-20 23:30:27 -08003655
3656 /* evict everyone we can from the aperture */
3657 ret = i915_gem_evict_everything(dev);
Chris Wilson07f73f62009-09-14 16:50:30 +01003658 if (ret && ret != -ENOSPC)
Keith Packardac94a962008-11-20 23:30:27 -08003659 goto err;
Eric Anholt673a3942008-07-30 12:06:12 -07003660 }
3661
3662 /* Set the pending read domains for the batch buffer to COMMAND */
3663 batch_obj = object_list[args->buffer_count-1];
Chris Wilson5f26a2c2009-06-06 09:45:58 +01003664 if (batch_obj->pending_write_domain) {
3665 DRM_ERROR("Attempting to use self-modifying batch buffer\n");
3666 ret = -EINVAL;
3667 goto err;
3668 }
3669 batch_obj->pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
Eric Anholt673a3942008-07-30 12:06:12 -07003670
Chris Wilson83d60792009-06-06 09:45:57 +01003671 /* Sanity check the batch buffer, prior to moving objects */
3672 exec_offset = exec_list[args->buffer_count - 1].offset;
3673 ret = i915_gem_check_execbuffer (args, exec_offset);
3674 if (ret != 0) {
3675 DRM_ERROR("execbuf with invalid offset/length\n");
3676 goto err;
3677 }
3678
Eric Anholt673a3942008-07-30 12:06:12 -07003679 i915_verify_inactive(dev, __FILE__, __LINE__);
3680
Keith Packard646f0f62008-11-20 23:23:03 -08003681 /* Zero the global flush/invalidate flags. These
3682 * will be modified as new domains are computed
3683 * for each object
3684 */
3685 dev->invalidate_domains = 0;
3686 dev->flush_domains = 0;
3687
Eric Anholt673a3942008-07-30 12:06:12 -07003688 for (i = 0; i < args->buffer_count; i++) {
3689 struct drm_gem_object *obj = object_list[i];
Eric Anholt673a3942008-07-30 12:06:12 -07003690
Keith Packard646f0f62008-11-20 23:23:03 -08003691 /* Compute new gpu domains and update invalidate/flush */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003692 i915_gem_object_set_to_gpu_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003693 }
3694
3695 i915_verify_inactive(dev, __FILE__, __LINE__);
3696
Keith Packard646f0f62008-11-20 23:23:03 -08003697 if (dev->invalidate_domains | dev->flush_domains) {
3698#if WATCH_EXEC
3699 DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
3700 __func__,
3701 dev->invalidate_domains,
3702 dev->flush_domains);
3703#endif
3704 i915_gem_flush(dev,
3705 dev->invalidate_domains,
3706 dev->flush_domains);
Daniel Vettera6910432010-02-02 17:08:37 +01003707 }
3708
3709 if (dev_priv->render_ring.outstanding_lazy_request) {
Chris Wilson8dc5d142010-08-12 12:36:12 +01003710 (void)i915_add_request(dev, file_priv, NULL, &dev_priv->render_ring);
Daniel Vettera6910432010-02-02 17:08:37 +01003711 dev_priv->render_ring.outstanding_lazy_request = false;
3712 }
3713 if (dev_priv->bsd_ring.outstanding_lazy_request) {
Chris Wilson8dc5d142010-08-12 12:36:12 +01003714 (void)i915_add_request(dev, file_priv, NULL, &dev_priv->bsd_ring);
Daniel Vettera6910432010-02-02 17:08:37 +01003715 dev_priv->bsd_ring.outstanding_lazy_request = false;
Keith Packard646f0f62008-11-20 23:23:03 -08003716 }
Eric Anholt673a3942008-07-30 12:06:12 -07003717
Eric Anholtefbeed92009-02-19 14:54:51 -08003718 for (i = 0; i < args->buffer_count; i++) {
3719 struct drm_gem_object *obj = object_list[i];
Daniel Vetter23010e42010-03-08 13:35:02 +01003720 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003721 uint32_t old_write_domain = obj->write_domain;
Eric Anholtefbeed92009-02-19 14:54:51 -08003722
3723 obj->write_domain = obj->pending_write_domain;
Daniel Vetter99fcb762010-02-07 16:20:18 +01003724 if (obj->write_domain)
3725 list_move_tail(&obj_priv->gpu_write_list,
3726 &dev_priv->mm.gpu_write_list);
3727 else
3728 list_del_init(&obj_priv->gpu_write_list);
3729
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003730 trace_i915_gem_object_change_domain(obj,
3731 obj->read_domains,
3732 old_write_domain);
Eric Anholtefbeed92009-02-19 14:54:51 -08003733 }
3734
Eric Anholt673a3942008-07-30 12:06:12 -07003735 i915_verify_inactive(dev, __FILE__, __LINE__);
3736
3737#if WATCH_COHERENCY
3738 for (i = 0; i < args->buffer_count; i++) {
3739 i915_gem_object_check_coherency(object_list[i],
3740 exec_list[i].handle);
3741 }
3742#endif
3743
Eric Anholt673a3942008-07-30 12:06:12 -07003744#if WATCH_EXEC
Ben Gamari6911a9b2009-04-02 11:24:54 -07003745 i915_gem_dump_object(batch_obj,
Eric Anholt673a3942008-07-30 12:06:12 -07003746 args->batch_len,
3747 __func__,
3748 ~0);
3749#endif
3750
Eric Anholt673a3942008-07-30 12:06:12 -07003751 /* Exec the batchbuffer */
Zou Nan hai852835f2010-05-21 09:08:56 +08003752 ret = ring->dispatch_gem_execbuffer(dev, ring, args,
3753 cliprects, exec_offset);
Eric Anholt673a3942008-07-30 12:06:12 -07003754 if (ret) {
3755 DRM_ERROR("dispatch failed %d\n", ret);
3756 goto err;
3757 }
3758
3759 /*
3760 * Ensure that the commands in the batch buffer are
3761 * finished before the interrupt fires
3762 */
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01003763 i915_retire_commands(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07003764
3765 i915_verify_inactive(dev, __FILE__, __LINE__);
3766
Daniel Vetter617dbe22010-02-11 22:16:02 +01003767 for (i = 0; i < args->buffer_count; i++) {
3768 struct drm_gem_object *obj = object_list[i];
3769 obj_priv = to_intel_bo(obj);
3770
3771 i915_gem_object_move_to_active(obj, ring);
3772#if WATCH_LRU
3773 DRM_INFO("%s: move to exec list %p\n", __func__, obj);
3774#endif
3775 }
3776
Eric Anholt673a3942008-07-30 12:06:12 -07003777 /*
3778 * Get a seqno representing the execution of the current buffer,
3779 * which we can wait on. We would like to mitigate these interrupts,
3780 * likely by only creating seqnos occasionally (so that we have
3781 * *some* interrupts representing completion of buffers that we can
3782 * wait on when trying to clear up gtt space).
3783 */
Chris Wilson8dc5d142010-08-12 12:36:12 +01003784 seqno = i915_add_request(dev, file_priv, request, ring);
3785 request = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07003786
Eric Anholt673a3942008-07-30 12:06:12 -07003787#if WATCH_LRU
3788 i915_dump_lru(dev, __func__);
3789#endif
3790
3791 i915_verify_inactive(dev, __FILE__, __LINE__);
3792
Eric Anholt673a3942008-07-30 12:06:12 -07003793err:
Julia Lawallaad87df2008-12-21 16:28:47 +01003794 for (i = 0; i < pinned; i++)
3795 i915_gem_object_unpin(object_list[i]);
Eric Anholt673a3942008-07-30 12:06:12 -07003796
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003797 for (i = 0; i < args->buffer_count; i++) {
3798 if (object_list[i]) {
Daniel Vetter23010e42010-03-08 13:35:02 +01003799 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003800 obj_priv->in_execbuffer = false;
3801 }
Julia Lawallaad87df2008-12-21 16:28:47 +01003802 drm_gem_object_unreference(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003803 }
Julia Lawallaad87df2008-12-21 16:28:47 +01003804
Eric Anholt673a3942008-07-30 12:06:12 -07003805 mutex_unlock(&dev->struct_mutex);
3806
Chris Wilson93533c22010-01-31 10:40:48 +00003807pre_mutex_err:
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003808 /* Copy the updated relocations out regardless of current error
3809 * state. Failure to update the relocs would mean that the next
3810 * time userland calls execbuf, it would do so with presumed offset
3811 * state that didn't match the actual object state.
3812 */
3813 ret2 = i915_gem_put_relocs_to_user(exec_list, args->buffer_count,
3814 relocs);
3815 if (ret2 != 0) {
3816 DRM_ERROR("Failed to copy relocations back out: %d\n", ret2);
3817
3818 if (ret == 0)
3819 ret = ret2;
3820 }
3821
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003822 drm_free_large(object_list);
Eric Anholt9a298b22009-03-24 12:23:04 -07003823 kfree(cliprects);
Chris Wilson8dc5d142010-08-12 12:36:12 +01003824 kfree(request);
Eric Anholt673a3942008-07-30 12:06:12 -07003825
3826 return ret;
3827}
3828
Jesse Barnes76446ca2009-12-17 22:05:42 -05003829/*
3830 * Legacy execbuffer just creates an exec2 list from the original exec object
3831 * list array and passes it to the real function.
3832 */
3833int
3834i915_gem_execbuffer(struct drm_device *dev, void *data,
3835 struct drm_file *file_priv)
3836{
3837 struct drm_i915_gem_execbuffer *args = data;
3838 struct drm_i915_gem_execbuffer2 exec2;
3839 struct drm_i915_gem_exec_object *exec_list = NULL;
3840 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
3841 int ret, i;
3842
3843#if WATCH_EXEC
3844 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3845 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3846#endif
3847
3848 if (args->buffer_count < 1) {
3849 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3850 return -EINVAL;
3851 }
3852
3853 /* Copy in the exec list from userland */
3854 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
3855 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
3856 if (exec_list == NULL || exec2_list == NULL) {
3857 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
3858 args->buffer_count);
3859 drm_free_large(exec_list);
3860 drm_free_large(exec2_list);
3861 return -ENOMEM;
3862 }
3863 ret = copy_from_user(exec_list,
3864 (struct drm_i915_relocation_entry __user *)
3865 (uintptr_t) args->buffers_ptr,
3866 sizeof(*exec_list) * args->buffer_count);
3867 if (ret != 0) {
3868 DRM_ERROR("copy %d exec entries failed %d\n",
3869 args->buffer_count, ret);
3870 drm_free_large(exec_list);
3871 drm_free_large(exec2_list);
3872 return -EFAULT;
3873 }
3874
3875 for (i = 0; i < args->buffer_count; i++) {
3876 exec2_list[i].handle = exec_list[i].handle;
3877 exec2_list[i].relocation_count = exec_list[i].relocation_count;
3878 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
3879 exec2_list[i].alignment = exec_list[i].alignment;
3880 exec2_list[i].offset = exec_list[i].offset;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01003881 if (INTEL_INFO(dev)->gen < 4)
Jesse Barnes76446ca2009-12-17 22:05:42 -05003882 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
3883 else
3884 exec2_list[i].flags = 0;
3885 }
3886
3887 exec2.buffers_ptr = args->buffers_ptr;
3888 exec2.buffer_count = args->buffer_count;
3889 exec2.batch_start_offset = args->batch_start_offset;
3890 exec2.batch_len = args->batch_len;
3891 exec2.DR1 = args->DR1;
3892 exec2.DR4 = args->DR4;
3893 exec2.num_cliprects = args->num_cliprects;
3894 exec2.cliprects_ptr = args->cliprects_ptr;
Zou Nan hai852835f2010-05-21 09:08:56 +08003895 exec2.flags = I915_EXEC_RENDER;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003896
3897 ret = i915_gem_do_execbuffer(dev, data, file_priv, &exec2, exec2_list);
3898 if (!ret) {
3899 /* Copy the new buffer offsets back to the user's exec list. */
3900 for (i = 0; i < args->buffer_count; i++)
3901 exec_list[i].offset = exec2_list[i].offset;
3902 /* ... and back out to userspace */
3903 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
3904 (uintptr_t) args->buffers_ptr,
3905 exec_list,
3906 sizeof(*exec_list) * args->buffer_count);
3907 if (ret) {
3908 ret = -EFAULT;
3909 DRM_ERROR("failed to copy %d exec entries "
3910 "back to user (%d)\n",
3911 args->buffer_count, ret);
3912 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05003913 }
3914
3915 drm_free_large(exec_list);
3916 drm_free_large(exec2_list);
3917 return ret;
3918}
3919
3920int
3921i915_gem_execbuffer2(struct drm_device *dev, void *data,
3922 struct drm_file *file_priv)
3923{
3924 struct drm_i915_gem_execbuffer2 *args = data;
3925 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
3926 int ret;
3927
3928#if WATCH_EXEC
3929 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3930 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3931#endif
3932
3933 if (args->buffer_count < 1) {
3934 DRM_ERROR("execbuf2 with %d buffers\n", args->buffer_count);
3935 return -EINVAL;
3936 }
3937
3938 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
3939 if (exec2_list == NULL) {
3940 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
3941 args->buffer_count);
3942 return -ENOMEM;
3943 }
3944 ret = copy_from_user(exec2_list,
3945 (struct drm_i915_relocation_entry __user *)
3946 (uintptr_t) args->buffers_ptr,
3947 sizeof(*exec2_list) * args->buffer_count);
3948 if (ret != 0) {
3949 DRM_ERROR("copy %d exec entries failed %d\n",
3950 args->buffer_count, ret);
3951 drm_free_large(exec2_list);
3952 return -EFAULT;
3953 }
3954
3955 ret = i915_gem_do_execbuffer(dev, data, file_priv, args, exec2_list);
3956 if (!ret) {
3957 /* Copy the new buffer offsets back to the user's exec list. */
3958 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
3959 (uintptr_t) args->buffers_ptr,
3960 exec2_list,
3961 sizeof(*exec2_list) * args->buffer_count);
3962 if (ret) {
3963 ret = -EFAULT;
3964 DRM_ERROR("failed to copy %d exec entries "
3965 "back to user (%d)\n",
3966 args->buffer_count, ret);
3967 }
3968 }
3969
3970 drm_free_large(exec2_list);
3971 return ret;
3972}
3973
Eric Anholt673a3942008-07-30 12:06:12 -07003974int
3975i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment)
3976{
3977 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01003978 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003979 int ret;
3980
Daniel Vetter778c3542010-05-13 11:49:44 +02003981 BUG_ON(obj_priv->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT);
3982
Eric Anholt673a3942008-07-30 12:06:12 -07003983 i915_verify_inactive(dev, __FILE__, __LINE__);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01003984
3985 if (obj_priv->gtt_space != NULL) {
3986 if (alignment == 0)
3987 alignment = i915_gem_get_gtt_alignment(obj);
3988 if (obj_priv->gtt_offset & (alignment - 1)) {
Chris Wilsonae7d49d2010-08-04 12:37:41 +01003989 WARN(obj_priv->pin_count,
3990 "bo is already pinned with incorrect alignment:"
3991 " offset=%x, req.alignment=%x\n",
3992 obj_priv->gtt_offset, alignment);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01003993 ret = i915_gem_object_unbind(obj);
3994 if (ret)
3995 return ret;
3996 }
3997 }
3998
Eric Anholt673a3942008-07-30 12:06:12 -07003999 if (obj_priv->gtt_space == NULL) {
4000 ret = i915_gem_object_bind_to_gtt(obj, alignment);
Chris Wilson97311292009-09-21 00:22:34 +01004001 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07004002 return ret;
Chris Wilson22c344e2009-02-11 14:26:45 +00004003 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05004004
Eric Anholt673a3942008-07-30 12:06:12 -07004005 obj_priv->pin_count++;
4006
4007 /* If the object is not active and not pending a flush,
4008 * remove it from the inactive list
4009 */
4010 if (obj_priv->pin_count == 1) {
4011 atomic_inc(&dev->pin_count);
4012 atomic_add(obj->size, &dev->pin_memory);
4013 if (!obj_priv->active &&
Chris Wilsonbf1a1092010-08-07 11:01:20 +01004014 (obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
Eric Anholt673a3942008-07-30 12:06:12 -07004015 list_del_init(&obj_priv->list);
4016 }
4017 i915_verify_inactive(dev, __FILE__, __LINE__);
4018
4019 return 0;
4020}
4021
4022void
4023i915_gem_object_unpin(struct drm_gem_object *obj)
4024{
4025 struct drm_device *dev = obj->dev;
4026 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01004027 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004028
4029 i915_verify_inactive(dev, __FILE__, __LINE__);
4030 obj_priv->pin_count--;
4031 BUG_ON(obj_priv->pin_count < 0);
4032 BUG_ON(obj_priv->gtt_space == NULL);
4033
4034 /* If the object is no longer pinned, and is
4035 * neither active nor being flushed, then stick it on
4036 * the inactive list
4037 */
4038 if (obj_priv->pin_count == 0) {
4039 if (!obj_priv->active &&
Chris Wilson21d509e2009-06-06 09:46:02 +01004040 (obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
Eric Anholt673a3942008-07-30 12:06:12 -07004041 list_move_tail(&obj_priv->list,
4042 &dev_priv->mm.inactive_list);
4043 atomic_dec(&dev->pin_count);
4044 atomic_sub(obj->size, &dev->pin_memory);
4045 }
4046 i915_verify_inactive(dev, __FILE__, __LINE__);
4047}
4048
4049int
4050i915_gem_pin_ioctl(struct drm_device *dev, void *data,
4051 struct drm_file *file_priv)
4052{
4053 struct drm_i915_gem_pin *args = data;
4054 struct drm_gem_object *obj;
4055 struct drm_i915_gem_object *obj_priv;
4056 int ret;
4057
4058 mutex_lock(&dev->struct_mutex);
4059
4060 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4061 if (obj == NULL) {
4062 DRM_ERROR("Bad handle in i915_gem_pin_ioctl(): %d\n",
4063 args->handle);
4064 mutex_unlock(&dev->struct_mutex);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01004065 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07004066 }
Daniel Vetter23010e42010-03-08 13:35:02 +01004067 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004068
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004069 if (obj_priv->madv != I915_MADV_WILLNEED) {
4070 DRM_ERROR("Attempting to pin a purgeable buffer\n");
Chris Wilson3ef94da2009-09-14 16:50:29 +01004071 drm_gem_object_unreference(obj);
4072 mutex_unlock(&dev->struct_mutex);
4073 return -EINVAL;
4074 }
4075
Jesse Barnes79e53942008-11-07 14:24:08 -08004076 if (obj_priv->pin_filp != NULL && obj_priv->pin_filp != file_priv) {
4077 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
4078 args->handle);
Chris Wilson96dec612009-02-08 19:08:04 +00004079 drm_gem_object_unreference(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004080 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08004081 return -EINVAL;
4082 }
4083
4084 obj_priv->user_pin_count++;
4085 obj_priv->pin_filp = file_priv;
4086 if (obj_priv->user_pin_count == 1) {
4087 ret = i915_gem_object_pin(obj, args->alignment);
4088 if (ret != 0) {
4089 drm_gem_object_unreference(obj);
4090 mutex_unlock(&dev->struct_mutex);
4091 return ret;
4092 }
Eric Anholt673a3942008-07-30 12:06:12 -07004093 }
4094
4095 /* XXX - flush the CPU caches for pinned objects
4096 * as the X server doesn't manage domains yet
4097 */
Eric Anholte47c68e2008-11-14 13:35:19 -08004098 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004099 args->offset = obj_priv->gtt_offset;
4100 drm_gem_object_unreference(obj);
4101 mutex_unlock(&dev->struct_mutex);
4102
4103 return 0;
4104}
4105
4106int
4107i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
4108 struct drm_file *file_priv)
4109{
4110 struct drm_i915_gem_pin *args = data;
4111 struct drm_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08004112 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07004113
4114 mutex_lock(&dev->struct_mutex);
4115
4116 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4117 if (obj == NULL) {
4118 DRM_ERROR("Bad handle in i915_gem_unpin_ioctl(): %d\n",
4119 args->handle);
4120 mutex_unlock(&dev->struct_mutex);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01004121 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07004122 }
4123
Daniel Vetter23010e42010-03-08 13:35:02 +01004124 obj_priv = to_intel_bo(obj);
Jesse Barnes79e53942008-11-07 14:24:08 -08004125 if (obj_priv->pin_filp != file_priv) {
4126 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
4127 args->handle);
4128 drm_gem_object_unreference(obj);
4129 mutex_unlock(&dev->struct_mutex);
4130 return -EINVAL;
4131 }
4132 obj_priv->user_pin_count--;
4133 if (obj_priv->user_pin_count == 0) {
4134 obj_priv->pin_filp = NULL;
4135 i915_gem_object_unpin(obj);
4136 }
Eric Anholt673a3942008-07-30 12:06:12 -07004137
4138 drm_gem_object_unreference(obj);
4139 mutex_unlock(&dev->struct_mutex);
4140 return 0;
4141}
4142
4143int
4144i915_gem_busy_ioctl(struct drm_device *dev, void *data,
4145 struct drm_file *file_priv)
4146{
4147 struct drm_i915_gem_busy *args = data;
4148 struct drm_gem_object *obj;
4149 struct drm_i915_gem_object *obj_priv;
4150
Eric Anholt673a3942008-07-30 12:06:12 -07004151 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4152 if (obj == NULL) {
4153 DRM_ERROR("Bad handle in i915_gem_busy_ioctl(): %d\n",
4154 args->handle);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01004155 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07004156 }
4157
Chris Wilsonb1ce7862009-06-06 09:46:00 +01004158 mutex_lock(&dev->struct_mutex);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004159
Chris Wilson0be555b2010-08-04 15:36:30 +01004160 /* Count all active objects as busy, even if they are currently not used
4161 * by the gpu. Users of this interface expect objects to eventually
4162 * become non-busy without any further actions, therefore emit any
4163 * necessary flushes here.
Eric Anholtc4de0a52008-12-14 19:05:04 -08004164 */
Chris Wilson0be555b2010-08-04 15:36:30 +01004165 obj_priv = to_intel_bo(obj);
4166 args->busy = obj_priv->active;
4167 if (args->busy) {
4168 /* Unconditionally flush objects, even when the gpu still uses this
4169 * object. Userspace calling this function indicates that it wants to
4170 * use this buffer rather sooner than later, so issuing the required
4171 * flush earlier is beneficial.
4172 */
4173 if (obj->write_domain) {
4174 i915_gem_flush(dev, 0, obj->write_domain);
Chris Wilson8dc5d142010-08-12 12:36:12 +01004175 (void)i915_add_request(dev, file_priv, NULL, obj_priv->ring);
Chris Wilson0be555b2010-08-04 15:36:30 +01004176 }
4177
4178 /* Update the active list for the hardware's current position.
4179 * Otherwise this only updates on a delayed timer or when irqs
4180 * are actually unmasked, and our working set ends up being
4181 * larger than required.
4182 */
4183 i915_gem_retire_requests_ring(dev, obj_priv->ring);
4184
4185 args->busy = obj_priv->active;
4186 }
Eric Anholt673a3942008-07-30 12:06:12 -07004187
4188 drm_gem_object_unreference(obj);
4189 mutex_unlock(&dev->struct_mutex);
4190 return 0;
4191}
4192
4193int
4194i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4195 struct drm_file *file_priv)
4196{
4197 return i915_gem_ring_throttle(dev, file_priv);
4198}
4199
Chris Wilson3ef94da2009-09-14 16:50:29 +01004200int
4201i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4202 struct drm_file *file_priv)
4203{
4204 struct drm_i915_gem_madvise *args = data;
4205 struct drm_gem_object *obj;
4206 struct drm_i915_gem_object *obj_priv;
4207
4208 switch (args->madv) {
4209 case I915_MADV_DONTNEED:
4210 case I915_MADV_WILLNEED:
4211 break;
4212 default:
4213 return -EINVAL;
4214 }
4215
4216 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4217 if (obj == NULL) {
4218 DRM_ERROR("Bad handle in i915_gem_madvise_ioctl(): %d\n",
4219 args->handle);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01004220 return -ENOENT;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004221 }
4222
4223 mutex_lock(&dev->struct_mutex);
Daniel Vetter23010e42010-03-08 13:35:02 +01004224 obj_priv = to_intel_bo(obj);
Chris Wilson3ef94da2009-09-14 16:50:29 +01004225
4226 if (obj_priv->pin_count) {
4227 drm_gem_object_unreference(obj);
4228 mutex_unlock(&dev->struct_mutex);
4229
4230 DRM_ERROR("Attempted i915_gem_madvise_ioctl() on a pinned object\n");
4231 return -EINVAL;
4232 }
4233
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004234 if (obj_priv->madv != __I915_MADV_PURGED)
4235 obj_priv->madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004236
Chris Wilson2d7ef392009-09-20 23:13:10 +01004237 /* if the object is no longer bound, discard its backing storage */
4238 if (i915_gem_object_is_purgeable(obj_priv) &&
4239 obj_priv->gtt_space == NULL)
4240 i915_gem_object_truncate(obj);
4241
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004242 args->retained = obj_priv->madv != __I915_MADV_PURGED;
4243
Chris Wilson3ef94da2009-09-14 16:50:29 +01004244 drm_gem_object_unreference(obj);
4245 mutex_unlock(&dev->struct_mutex);
4246
4247 return 0;
4248}
4249
Daniel Vetterac52bc52010-04-09 19:05:06 +00004250struct drm_gem_object * i915_gem_alloc_object(struct drm_device *dev,
4251 size_t size)
4252{
Daniel Vetterc397b902010-04-09 19:05:07 +00004253 struct drm_i915_gem_object *obj;
4254
4255 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
4256 if (obj == NULL)
4257 return NULL;
4258
4259 if (drm_gem_object_init(dev, &obj->base, size) != 0) {
4260 kfree(obj);
4261 return NULL;
4262 }
4263
4264 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4265 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4266
4267 obj->agp_type = AGP_USER_MEMORY;
Daniel Vetter62b8b212010-04-09 19:05:08 +00004268 obj->base.driver_private = NULL;
Daniel Vetterc397b902010-04-09 19:05:07 +00004269 obj->fence_reg = I915_FENCE_REG_NONE;
4270 INIT_LIST_HEAD(&obj->list);
4271 INIT_LIST_HEAD(&obj->gpu_write_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00004272 obj->madv = I915_MADV_WILLNEED;
4273
4274 trace_i915_gem_object_create(&obj->base);
4275
4276 return &obj->base;
Daniel Vetterac52bc52010-04-09 19:05:06 +00004277}
4278
Eric Anholt673a3942008-07-30 12:06:12 -07004279int i915_gem_init_object(struct drm_gem_object *obj)
4280{
Daniel Vetterc397b902010-04-09 19:05:07 +00004281 BUG();
Jesse Barnesde151cf2008-11-12 10:03:55 -08004282
Eric Anholt673a3942008-07-30 12:06:12 -07004283 return 0;
4284}
4285
Chris Wilsonbe726152010-07-23 23:18:50 +01004286static void i915_gem_free_object_tail(struct drm_gem_object *obj)
4287{
4288 struct drm_device *dev = obj->dev;
4289 drm_i915_private_t *dev_priv = dev->dev_private;
4290 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
4291 int ret;
4292
4293 ret = i915_gem_object_unbind(obj);
4294 if (ret == -ERESTARTSYS) {
4295 list_move(&obj_priv->list,
4296 &dev_priv->mm.deferred_free_list);
4297 return;
4298 }
4299
4300 if (obj_priv->mmap_offset)
4301 i915_gem_free_mmap_offset(obj);
4302
4303 drm_gem_object_release(obj);
4304
4305 kfree(obj_priv->page_cpu_valid);
4306 kfree(obj_priv->bit_17);
4307 kfree(obj_priv);
4308}
4309
Eric Anholt673a3942008-07-30 12:06:12 -07004310void i915_gem_free_object(struct drm_gem_object *obj)
4311{
Jesse Barnesde151cf2008-11-12 10:03:55 -08004312 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01004313 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004314
Chris Wilson1c5d22f2009-08-25 11:15:50 +01004315 trace_i915_gem_object_destroy(obj);
4316
Eric Anholt673a3942008-07-30 12:06:12 -07004317 while (obj_priv->pin_count > 0)
4318 i915_gem_object_unpin(obj);
4319
Dave Airlie71acb5e2008-12-30 20:31:46 +10004320 if (obj_priv->phys_obj)
4321 i915_gem_detach_phys_object(dev, obj);
4322
Chris Wilsonbe726152010-07-23 23:18:50 +01004323 i915_gem_free_object_tail(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004324}
4325
Jesse Barnes5669fca2009-02-17 15:13:31 -08004326int
Eric Anholt673a3942008-07-30 12:06:12 -07004327i915_gem_idle(struct drm_device *dev)
4328{
4329 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson29105cc2010-01-07 10:39:13 +00004330 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004331
Keith Packard6dbe2772008-10-14 21:41:13 -07004332 mutex_lock(&dev->struct_mutex);
4333
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004334 if (dev_priv->mm.suspended ||
Zou Nan haid1b851f2010-05-21 09:08:57 +08004335 (dev_priv->render_ring.gem_object == NULL) ||
4336 (HAS_BSD(dev) &&
4337 dev_priv->bsd_ring.gem_object == NULL)) {
Keith Packard6dbe2772008-10-14 21:41:13 -07004338 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004339 return 0;
Keith Packard6dbe2772008-10-14 21:41:13 -07004340 }
Eric Anholt673a3942008-07-30 12:06:12 -07004341
Chris Wilson29105cc2010-01-07 10:39:13 +00004342 ret = i915_gpu_idle(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004343 if (ret) {
4344 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004345 return ret;
Keith Packard6dbe2772008-10-14 21:41:13 -07004346 }
Eric Anholt673a3942008-07-30 12:06:12 -07004347
Chris Wilson29105cc2010-01-07 10:39:13 +00004348 /* Under UMS, be paranoid and evict. */
4349 if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
Chris Wilsonb47eb4a2010-08-07 11:01:23 +01004350 ret = i915_gem_evict_inactive(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00004351 if (ret) {
4352 mutex_unlock(&dev->struct_mutex);
4353 return ret;
4354 }
4355 }
4356
4357 /* Hack! Don't let anybody do execbuf while we don't control the chip.
4358 * We need to replace this with a semaphore, or something.
4359 * And not confound mm.suspended!
4360 */
4361 dev_priv->mm.suspended = 1;
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02004362 del_timer_sync(&dev_priv->hangcheck_timer);
Chris Wilson29105cc2010-01-07 10:39:13 +00004363
4364 i915_kernel_lost_context(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004365 i915_gem_cleanup_ringbuffer(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00004366
Keith Packard6dbe2772008-10-14 21:41:13 -07004367 mutex_unlock(&dev->struct_mutex);
4368
Chris Wilson29105cc2010-01-07 10:39:13 +00004369 /* Cancel the retire work handler, which should be idle now. */
4370 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
4371
Eric Anholt673a3942008-07-30 12:06:12 -07004372 return 0;
4373}
4374
Jesse Barnese552eb72010-04-21 11:39:23 -07004375/*
4376 * 965+ support PIPE_CONTROL commands, which provide finer grained control
4377 * over cache flushing.
4378 */
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004379static int
Jesse Barnese552eb72010-04-21 11:39:23 -07004380i915_gem_init_pipe_control(struct drm_device *dev)
4381{
4382 drm_i915_private_t *dev_priv = dev->dev_private;
4383 struct drm_gem_object *obj;
4384 struct drm_i915_gem_object *obj_priv;
4385 int ret;
4386
Eric Anholt34dc4d42010-05-07 14:30:03 -07004387 obj = i915_gem_alloc_object(dev, 4096);
Jesse Barnese552eb72010-04-21 11:39:23 -07004388 if (obj == NULL) {
4389 DRM_ERROR("Failed to allocate seqno page\n");
4390 ret = -ENOMEM;
4391 goto err;
4392 }
4393 obj_priv = to_intel_bo(obj);
4394 obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
4395
4396 ret = i915_gem_object_pin(obj, 4096);
4397 if (ret)
4398 goto err_unref;
4399
4400 dev_priv->seqno_gfx_addr = obj_priv->gtt_offset;
4401 dev_priv->seqno_page = kmap(obj_priv->pages[0]);
4402 if (dev_priv->seqno_page == NULL)
4403 goto err_unpin;
4404
4405 dev_priv->seqno_obj = obj;
4406 memset(dev_priv->seqno_page, 0, PAGE_SIZE);
4407
4408 return 0;
4409
4410err_unpin:
4411 i915_gem_object_unpin(obj);
4412err_unref:
4413 drm_gem_object_unreference(obj);
4414err:
4415 return ret;
4416}
4417
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004418
4419static void
Jesse Barnese552eb72010-04-21 11:39:23 -07004420i915_gem_cleanup_pipe_control(struct drm_device *dev)
4421{
4422 drm_i915_private_t *dev_priv = dev->dev_private;
4423 struct drm_gem_object *obj;
4424 struct drm_i915_gem_object *obj_priv;
4425
4426 obj = dev_priv->seqno_obj;
4427 obj_priv = to_intel_bo(obj);
4428 kunmap(obj_priv->pages[0]);
4429 i915_gem_object_unpin(obj);
4430 drm_gem_object_unreference(obj);
4431 dev_priv->seqno_obj = NULL;
4432
4433 dev_priv->seqno_page = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07004434}
4435
Eric Anholt673a3942008-07-30 12:06:12 -07004436int
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004437i915_gem_init_ringbuffer(struct drm_device *dev)
4438{
4439 drm_i915_private_t *dev_priv = dev->dev_private;
4440 int ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01004441
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004442 dev_priv->render_ring = render_ring;
Chris Wilson68f95ba2010-05-27 13:18:22 +01004443
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004444 if (!I915_NEED_GFX_HWS(dev)) {
4445 dev_priv->render_ring.status_page.page_addr
4446 = dev_priv->status_page_dmah->vaddr;
4447 memset(dev_priv->render_ring.status_page.page_addr,
4448 0, PAGE_SIZE);
4449 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004450
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004451 if (HAS_PIPE_CONTROL(dev)) {
4452 ret = i915_gem_init_pipe_control(dev);
4453 if (ret)
4454 return ret;
4455 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004456
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004457 ret = intel_init_ring_buffer(dev, &dev_priv->render_ring);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004458 if (ret)
4459 goto cleanup_pipe_control;
4460
4461 if (HAS_BSD(dev)) {
Zou Nan haid1b851f2010-05-21 09:08:57 +08004462 dev_priv->bsd_ring = bsd_ring;
4463 ret = intel_init_ring_buffer(dev, &dev_priv->bsd_ring);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004464 if (ret)
4465 goto cleanup_render_ring;
Zou Nan haid1b851f2010-05-21 09:08:57 +08004466 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004467
Chris Wilson6f392d5482010-08-07 11:01:22 +01004468 dev_priv->next_seqno = 1;
4469
Chris Wilson68f95ba2010-05-27 13:18:22 +01004470 return 0;
4471
4472cleanup_render_ring:
4473 intel_cleanup_ring_buffer(dev, &dev_priv->render_ring);
4474cleanup_pipe_control:
4475 if (HAS_PIPE_CONTROL(dev))
4476 i915_gem_cleanup_pipe_control(dev);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004477 return ret;
4478}
4479
4480void
4481i915_gem_cleanup_ringbuffer(struct drm_device *dev)
4482{
4483 drm_i915_private_t *dev_priv = dev->dev_private;
4484
4485 intel_cleanup_ring_buffer(dev, &dev_priv->render_ring);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004486 if (HAS_BSD(dev))
4487 intel_cleanup_ring_buffer(dev, &dev_priv->bsd_ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004488 if (HAS_PIPE_CONTROL(dev))
4489 i915_gem_cleanup_pipe_control(dev);
4490}
4491
4492int
Eric Anholt673a3942008-07-30 12:06:12 -07004493i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
4494 struct drm_file *file_priv)
4495{
4496 drm_i915_private_t *dev_priv = dev->dev_private;
4497 int ret;
4498
Jesse Barnes79e53942008-11-07 14:24:08 -08004499 if (drm_core_check_feature(dev, DRIVER_MODESET))
4500 return 0;
4501
Ben Gamariba1234d2009-09-14 17:48:47 -04004502 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07004503 DRM_ERROR("Reenabling wedged hardware, good luck\n");
Ben Gamariba1234d2009-09-14 17:48:47 -04004504 atomic_set(&dev_priv->mm.wedged, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004505 }
4506
Eric Anholt673a3942008-07-30 12:06:12 -07004507 mutex_lock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004508 dev_priv->mm.suspended = 0;
4509
4510 ret = i915_gem_init_ringbuffer(dev);
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004511 if (ret != 0) {
4512 mutex_unlock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004513 return ret;
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004514 }
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004515
Zou Nan hai852835f2010-05-21 09:08:56 +08004516 BUG_ON(!list_empty(&dev_priv->render_ring.active_list));
Zou Nan haid1b851f2010-05-21 09:08:57 +08004517 BUG_ON(HAS_BSD(dev) && !list_empty(&dev_priv->bsd_ring.active_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004518 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
4519 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
Zou Nan hai852835f2010-05-21 09:08:56 +08004520 BUG_ON(!list_empty(&dev_priv->render_ring.request_list));
Zou Nan haid1b851f2010-05-21 09:08:57 +08004521 BUG_ON(HAS_BSD(dev) && !list_empty(&dev_priv->bsd_ring.request_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004522 mutex_unlock(&dev->struct_mutex);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004523
Chris Wilson5f353082010-06-07 14:03:03 +01004524 ret = drm_irq_install(dev);
4525 if (ret)
4526 goto cleanup_ringbuffer;
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004527
Eric Anholt673a3942008-07-30 12:06:12 -07004528 return 0;
Chris Wilson5f353082010-06-07 14:03:03 +01004529
4530cleanup_ringbuffer:
4531 mutex_lock(&dev->struct_mutex);
4532 i915_gem_cleanup_ringbuffer(dev);
4533 dev_priv->mm.suspended = 1;
4534 mutex_unlock(&dev->struct_mutex);
4535
4536 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004537}
4538
4539int
4540i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
4541 struct drm_file *file_priv)
4542{
Jesse Barnes79e53942008-11-07 14:24:08 -08004543 if (drm_core_check_feature(dev, DRIVER_MODESET))
4544 return 0;
4545
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004546 drm_irq_uninstall(dev);
Linus Torvaldse6890f62009-09-08 17:09:24 -07004547 return i915_gem_idle(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004548}
4549
4550void
4551i915_gem_lastclose(struct drm_device *dev)
4552{
4553 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004554
Eric Anholte806b492009-01-22 09:56:58 -08004555 if (drm_core_check_feature(dev, DRIVER_MODESET))
4556 return;
4557
Keith Packard6dbe2772008-10-14 21:41:13 -07004558 ret = i915_gem_idle(dev);
4559 if (ret)
4560 DRM_ERROR("failed to idle hardware: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07004561}
4562
4563void
4564i915_gem_load(struct drm_device *dev)
4565{
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004566 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07004567 drm_i915_private_t *dev_priv = dev->dev_private;
4568
Eric Anholt673a3942008-07-30 12:06:12 -07004569 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
Daniel Vetter99fcb762010-02-07 16:20:18 +01004570 INIT_LIST_HEAD(&dev_priv->mm.gpu_write_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004571 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07004572 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Chris Wilsonbe726152010-07-23 23:18:50 +01004573 INIT_LIST_HEAD(&dev_priv->mm.deferred_free_list);
Zou Nan hai852835f2010-05-21 09:08:56 +08004574 INIT_LIST_HEAD(&dev_priv->render_ring.active_list);
4575 INIT_LIST_HEAD(&dev_priv->render_ring.request_list);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004576 if (HAS_BSD(dev)) {
4577 INIT_LIST_HEAD(&dev_priv->bsd_ring.active_list);
4578 INIT_LIST_HEAD(&dev_priv->bsd_ring.request_list);
4579 }
Daniel Vetter007cc8a2010-04-28 11:02:31 +02004580 for (i = 0; i < 16; i++)
4581 INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004582 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
4583 i915_gem_retire_work_handler);
Chris Wilson31169712009-09-14 16:50:28 +01004584 spin_lock(&shrink_list_lock);
4585 list_add(&dev_priv->mm.shrink_list, &shrink_list);
4586 spin_unlock(&shrink_list_lock);
4587
Dave Airlie94400122010-07-20 13:15:31 +10004588 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
4589 if (IS_GEN3(dev)) {
4590 u32 tmp = I915_READ(MI_ARB_STATE);
4591 if (!(tmp & MI_ARB_C3_LP_WRITE_ENABLE)) {
4592 /* arb state is a masked write, so set bit + bit in mask */
4593 tmp = MI_ARB_C3_LP_WRITE_ENABLE | (MI_ARB_C3_LP_WRITE_ENABLE << MI_ARB_MASK_SHIFT);
4594 I915_WRITE(MI_ARB_STATE, tmp);
4595 }
4596 }
4597
Jesse Barnesde151cf2008-11-12 10:03:55 -08004598 /* Old X drivers will take 0-2 for front, back, depth buffers */
Eric Anholtb397c832010-01-26 09:43:10 -08004599 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4600 dev_priv->fence_reg_start = 3;
Jesse Barnesde151cf2008-11-12 10:03:55 -08004601
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004602 if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08004603 dev_priv->num_fence_regs = 16;
4604 else
4605 dev_priv->num_fence_regs = 8;
4606
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004607 /* Initialize fence registers to zero */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004608 switch (INTEL_INFO(dev)->gen) {
4609 case 6:
4610 for (i = 0; i < 16; i++)
4611 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (i * 8), 0);
4612 break;
4613 case 5:
4614 case 4:
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004615 for (i = 0; i < 16; i++)
4616 I915_WRITE64(FENCE_REG_965_0 + (i * 8), 0);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004617 break;
4618 case 3:
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004619 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4620 for (i = 0; i < 8; i++)
4621 I915_WRITE(FENCE_REG_945_8 + (i * 4), 0);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004622 case 2:
4623 for (i = 0; i < 8; i++)
4624 I915_WRITE(FENCE_REG_830_0 + (i * 4), 0);
4625 break;
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004626 }
Eric Anholt673a3942008-07-30 12:06:12 -07004627 i915_gem_detect_bit_6_swizzle(dev);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05004628 init_waitqueue_head(&dev_priv->pending_flip_queue);
Eric Anholt673a3942008-07-30 12:06:12 -07004629}
Dave Airlie71acb5e2008-12-30 20:31:46 +10004630
4631/*
4632 * Create a physically contiguous memory object for this object
4633 * e.g. for cursor + overlay regs
4634 */
Chris Wilson995b6762010-08-20 13:23:26 +01004635static int i915_gem_init_phys_object(struct drm_device *dev,
4636 int id, int size, int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004637{
4638 drm_i915_private_t *dev_priv = dev->dev_private;
4639 struct drm_i915_gem_phys_object *phys_obj;
4640 int ret;
4641
4642 if (dev_priv->mm.phys_objs[id - 1] || !size)
4643 return 0;
4644
Eric Anholt9a298b22009-03-24 12:23:04 -07004645 phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004646 if (!phys_obj)
4647 return -ENOMEM;
4648
4649 phys_obj->id = id;
4650
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004651 phys_obj->handle = drm_pci_alloc(dev, size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004652 if (!phys_obj->handle) {
4653 ret = -ENOMEM;
4654 goto kfree_obj;
4655 }
4656#ifdef CONFIG_X86
4657 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4658#endif
4659
4660 dev_priv->mm.phys_objs[id - 1] = phys_obj;
4661
4662 return 0;
4663kfree_obj:
Eric Anholt9a298b22009-03-24 12:23:04 -07004664 kfree(phys_obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004665 return ret;
4666}
4667
Chris Wilson995b6762010-08-20 13:23:26 +01004668static void i915_gem_free_phys_object(struct drm_device *dev, int id)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004669{
4670 drm_i915_private_t *dev_priv = dev->dev_private;
4671 struct drm_i915_gem_phys_object *phys_obj;
4672
4673 if (!dev_priv->mm.phys_objs[id - 1])
4674 return;
4675
4676 phys_obj = dev_priv->mm.phys_objs[id - 1];
4677 if (phys_obj->cur_obj) {
4678 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
4679 }
4680
4681#ifdef CONFIG_X86
4682 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4683#endif
4684 drm_pci_free(dev, phys_obj->handle);
4685 kfree(phys_obj);
4686 dev_priv->mm.phys_objs[id - 1] = NULL;
4687}
4688
4689void i915_gem_free_all_phys_object(struct drm_device *dev)
4690{
4691 int i;
4692
Dave Airlie260883c2009-01-22 17:58:49 +10004693 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004694 i915_gem_free_phys_object(dev, i);
4695}
4696
4697void i915_gem_detach_phys_object(struct drm_device *dev,
4698 struct drm_gem_object *obj)
4699{
4700 struct drm_i915_gem_object *obj_priv;
4701 int i;
4702 int ret;
4703 int page_count;
4704
Daniel Vetter23010e42010-03-08 13:35:02 +01004705 obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004706 if (!obj_priv->phys_obj)
4707 return;
4708
Chris Wilson4bdadb92010-01-27 13:36:32 +00004709 ret = i915_gem_object_get_pages(obj, 0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004710 if (ret)
4711 goto out;
4712
4713 page_count = obj->size / PAGE_SIZE;
4714
4715 for (i = 0; i < page_count; i++) {
Eric Anholt856fa192009-03-19 14:10:50 -07004716 char *dst = kmap_atomic(obj_priv->pages[i], KM_USER0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004717 char *src = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4718
4719 memcpy(dst, src, PAGE_SIZE);
4720 kunmap_atomic(dst, KM_USER0);
4721 }
Eric Anholt856fa192009-03-19 14:10:50 -07004722 drm_clflush_pages(obj_priv->pages, page_count);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004723 drm_agp_chipset_flush(dev);
Chris Wilsond78b47b2009-06-17 21:52:49 +01004724
4725 i915_gem_object_put_pages(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004726out:
4727 obj_priv->phys_obj->cur_obj = NULL;
4728 obj_priv->phys_obj = NULL;
4729}
4730
4731int
4732i915_gem_attach_phys_object(struct drm_device *dev,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004733 struct drm_gem_object *obj,
4734 int id,
4735 int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004736{
4737 drm_i915_private_t *dev_priv = dev->dev_private;
4738 struct drm_i915_gem_object *obj_priv;
4739 int ret = 0;
4740 int page_count;
4741 int i;
4742
4743 if (id > I915_MAX_PHYS_OBJECT)
4744 return -EINVAL;
4745
Daniel Vetter23010e42010-03-08 13:35:02 +01004746 obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004747
4748 if (obj_priv->phys_obj) {
4749 if (obj_priv->phys_obj->id == id)
4750 return 0;
4751 i915_gem_detach_phys_object(dev, obj);
4752 }
4753
Dave Airlie71acb5e2008-12-30 20:31:46 +10004754 /* create a new object */
4755 if (!dev_priv->mm.phys_objs[id - 1]) {
4756 ret = i915_gem_init_phys_object(dev, id,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004757 obj->size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004758 if (ret) {
Linus Torvaldsaeb565d2009-01-26 10:01:53 -08004759 DRM_ERROR("failed to init phys object %d size: %zu\n", id, obj->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004760 goto out;
4761 }
4762 }
4763
4764 /* bind to the object */
4765 obj_priv->phys_obj = dev_priv->mm.phys_objs[id - 1];
4766 obj_priv->phys_obj->cur_obj = obj;
4767
Chris Wilson4bdadb92010-01-27 13:36:32 +00004768 ret = i915_gem_object_get_pages(obj, 0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004769 if (ret) {
4770 DRM_ERROR("failed to get page list\n");
4771 goto out;
4772 }
4773
4774 page_count = obj->size / PAGE_SIZE;
4775
4776 for (i = 0; i < page_count; i++) {
Eric Anholt856fa192009-03-19 14:10:50 -07004777 char *src = kmap_atomic(obj_priv->pages[i], KM_USER0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004778 char *dst = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4779
4780 memcpy(dst, src, PAGE_SIZE);
4781 kunmap_atomic(src, KM_USER0);
4782 }
4783
Chris Wilsond78b47b2009-06-17 21:52:49 +01004784 i915_gem_object_put_pages(obj);
4785
Dave Airlie71acb5e2008-12-30 20:31:46 +10004786 return 0;
4787out:
4788 return ret;
4789}
4790
4791static int
4792i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
4793 struct drm_i915_gem_pwrite *args,
4794 struct drm_file *file_priv)
4795{
Daniel Vetter23010e42010-03-08 13:35:02 +01004796 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004797 void *obj_addr;
4798 int ret;
4799 char __user *user_data;
4800
4801 user_data = (char __user *) (uintptr_t) args->data_ptr;
4802 obj_addr = obj_priv->phys_obj->handle->vaddr + args->offset;
4803
Zhao Yakui44d98a62009-10-09 11:39:40 +08004804 DRM_DEBUG_DRIVER("obj_addr %p, %lld\n", obj_addr, args->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004805 ret = copy_from_user(obj_addr, user_data, args->size);
4806 if (ret)
4807 return -EFAULT;
4808
4809 drm_agp_chipset_flush(dev);
4810 return 0;
4811}
Eric Anholtb9624422009-06-03 07:27:35 +00004812
4813void i915_gem_release(struct drm_device * dev, struct drm_file *file_priv)
4814{
4815 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
4816
4817 /* Clean up our request list when the client is going away, so that
4818 * later retire_requests won't dereference our soon-to-be-gone
4819 * file_priv.
4820 */
4821 mutex_lock(&dev->struct_mutex);
4822 while (!list_empty(&i915_file_priv->mm.request_list))
4823 list_del_init(i915_file_priv->mm.request_list.next);
4824 mutex_unlock(&dev->struct_mutex);
4825}
Chris Wilson31169712009-09-14 16:50:28 +01004826
Chris Wilson31169712009-09-14 16:50:28 +01004827static int
Chris Wilson1637ef42010-04-20 17:10:35 +01004828i915_gpu_is_active(struct drm_device *dev)
4829{
4830 drm_i915_private_t *dev_priv = dev->dev_private;
4831 int lists_empty;
4832
Chris Wilson1637ef42010-04-20 17:10:35 +01004833 lists_empty = list_empty(&dev_priv->mm.flushing_list) &&
Zou Nan hai852835f2010-05-21 09:08:56 +08004834 list_empty(&dev_priv->render_ring.active_list);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004835 if (HAS_BSD(dev))
4836 lists_empty &= list_empty(&dev_priv->bsd_ring.active_list);
Chris Wilson1637ef42010-04-20 17:10:35 +01004837
4838 return !lists_empty;
4839}
4840
4841static int
Dave Chinner7f8275d2010-07-19 14:56:17 +10004842i915_gem_shrink(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask)
Chris Wilson31169712009-09-14 16:50:28 +01004843{
4844 drm_i915_private_t *dev_priv, *next_dev;
4845 struct drm_i915_gem_object *obj_priv, *next_obj;
4846 int cnt = 0;
4847 int would_deadlock = 1;
4848
4849 /* "fast-path" to count number of available objects */
4850 if (nr_to_scan == 0) {
4851 spin_lock(&shrink_list_lock);
4852 list_for_each_entry(dev_priv, &shrink_list, mm.shrink_list) {
4853 struct drm_device *dev = dev_priv->dev;
4854
4855 if (mutex_trylock(&dev->struct_mutex)) {
4856 list_for_each_entry(obj_priv,
4857 &dev_priv->mm.inactive_list,
4858 list)
4859 cnt++;
4860 mutex_unlock(&dev->struct_mutex);
4861 }
4862 }
4863 spin_unlock(&shrink_list_lock);
4864
4865 return (cnt / 100) * sysctl_vfs_cache_pressure;
4866 }
4867
4868 spin_lock(&shrink_list_lock);
4869
Chris Wilson1637ef42010-04-20 17:10:35 +01004870rescan:
Chris Wilson31169712009-09-14 16:50:28 +01004871 /* first scan for clean buffers */
4872 list_for_each_entry_safe(dev_priv, next_dev,
4873 &shrink_list, mm.shrink_list) {
4874 struct drm_device *dev = dev_priv->dev;
4875
4876 if (! mutex_trylock(&dev->struct_mutex))
4877 continue;
4878
4879 spin_unlock(&shrink_list_lock);
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01004880 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004881
Chris Wilson31169712009-09-14 16:50:28 +01004882 list_for_each_entry_safe(obj_priv, next_obj,
4883 &dev_priv->mm.inactive_list,
4884 list) {
4885 if (i915_gem_object_is_purgeable(obj_priv)) {
Daniel Vettera8089e82010-04-09 19:05:09 +00004886 i915_gem_object_unbind(&obj_priv->base);
Chris Wilson31169712009-09-14 16:50:28 +01004887 if (--nr_to_scan <= 0)
4888 break;
4889 }
4890 }
4891
4892 spin_lock(&shrink_list_lock);
4893 mutex_unlock(&dev->struct_mutex);
4894
Chris Wilson963b4832009-09-20 23:03:54 +01004895 would_deadlock = 0;
4896
Chris Wilson31169712009-09-14 16:50:28 +01004897 if (nr_to_scan <= 0)
4898 break;
4899 }
4900
4901 /* second pass, evict/count anything still on the inactive list */
4902 list_for_each_entry_safe(dev_priv, next_dev,
4903 &shrink_list, mm.shrink_list) {
4904 struct drm_device *dev = dev_priv->dev;
4905
4906 if (! mutex_trylock(&dev->struct_mutex))
4907 continue;
4908
4909 spin_unlock(&shrink_list_lock);
4910
4911 list_for_each_entry_safe(obj_priv, next_obj,
4912 &dev_priv->mm.inactive_list,
4913 list) {
4914 if (nr_to_scan > 0) {
Daniel Vettera8089e82010-04-09 19:05:09 +00004915 i915_gem_object_unbind(&obj_priv->base);
Chris Wilson31169712009-09-14 16:50:28 +01004916 nr_to_scan--;
4917 } else
4918 cnt++;
4919 }
4920
4921 spin_lock(&shrink_list_lock);
4922 mutex_unlock(&dev->struct_mutex);
4923
4924 would_deadlock = 0;
4925 }
4926
Chris Wilson1637ef42010-04-20 17:10:35 +01004927 if (nr_to_scan) {
4928 int active = 0;
4929
4930 /*
4931 * We are desperate for pages, so as a last resort, wait
4932 * for the GPU to finish and discard whatever we can.
4933 * This has a dramatic impact to reduce the number of
4934 * OOM-killer events whilst running the GPU aggressively.
4935 */
4936 list_for_each_entry(dev_priv, &shrink_list, mm.shrink_list) {
4937 struct drm_device *dev = dev_priv->dev;
4938
4939 if (!mutex_trylock(&dev->struct_mutex))
4940 continue;
4941
4942 spin_unlock(&shrink_list_lock);
4943
4944 if (i915_gpu_is_active(dev)) {
4945 i915_gpu_idle(dev);
4946 active++;
4947 }
4948
4949 spin_lock(&shrink_list_lock);
4950 mutex_unlock(&dev->struct_mutex);
4951 }
4952
4953 if (active)
4954 goto rescan;
4955 }
4956
Chris Wilson31169712009-09-14 16:50:28 +01004957 spin_unlock(&shrink_list_lock);
4958
4959 if (would_deadlock)
4960 return -1;
4961 else if (cnt > 0)
4962 return (cnt / 100) * sysctl_vfs_cache_pressure;
4963 else
4964 return 0;
4965}
4966
4967static struct shrinker shrinker = {
4968 .shrink = i915_gem_shrink,
4969 .seeks = DEFAULT_SEEKS,
4970};
4971
4972__init void
4973i915_gem_shrinker_init(void)
4974{
4975 register_shrinker(&shrinker);
4976}
4977
4978__exit void
4979i915_gem_shrinker_exit(void)
4980{
4981 unregister_shrinker(&shrinker);
4982}