blob: eb6c473c6d1b40f9d55f6cb77f6c72f43ece9b5b [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);
Chris Wilson2dafb1e2010-06-07 14:03:05 +010040static int i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj);
Eric Anholte47c68e2008-11-14 13:35:19 -080041static void i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj);
42static void i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj);
Eric Anholte47c68e2008-11-14 13:35:19 -080043static int i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj,
44 int write);
45static int i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
46 uint64_t offset,
47 uint64_t size);
48static void i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj);
Eric Anholt673a3942008-07-30 12:06:12 -070049static int i915_gem_object_wait_rendering(struct drm_gem_object *obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -080050static int i915_gem_object_bind_to_gtt(struct drm_gem_object *obj,
51 unsigned alignment);
Jesse Barnesde151cf2008-11-12 10:03:55 -080052static void i915_gem_clear_fence_reg(struct drm_gem_object *obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +100053static int i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
54 struct drm_i915_gem_pwrite *args,
55 struct drm_file *file_priv);
Chris Wilsonbe726152010-07-23 23:18:50 +010056static void i915_gem_free_object_tail(struct drm_gem_object *obj);
Eric Anholt673a3942008-07-30 12:06:12 -070057
Chris Wilson31169712009-09-14 16:50:28 +010058static LIST_HEAD(shrink_list);
59static DEFINE_SPINLOCK(shrink_list_lock);
60
Chris Wilson7d1c4802010-08-07 21:45:03 +010061static inline bool
62i915_gem_object_is_inactive(struct drm_i915_gem_object *obj_priv)
63{
64 return obj_priv->gtt_space &&
65 !obj_priv->active &&
66 obj_priv->pin_count == 0;
67}
68
Jesse Barnes79e53942008-11-07 14:24:08 -080069int i915_gem_do_init(struct drm_device *dev, unsigned long start,
70 unsigned long end)
71{
72 drm_i915_private_t *dev_priv = dev->dev_private;
73
74 if (start >= end ||
75 (start & (PAGE_SIZE - 1)) != 0 ||
76 (end & (PAGE_SIZE - 1)) != 0) {
77 return -EINVAL;
78 }
79
80 drm_mm_init(&dev_priv->mm.gtt_space, start,
81 end - start);
82
83 dev->gtt_total = (uint32_t) (end - start);
84
85 return 0;
86}
Keith Packard6dbe2772008-10-14 21:41:13 -070087
Eric Anholt673a3942008-07-30 12:06:12 -070088int
89i915_gem_init_ioctl(struct drm_device *dev, void *data,
90 struct drm_file *file_priv)
91{
Eric Anholt673a3942008-07-30 12:06:12 -070092 struct drm_i915_gem_init *args = data;
Jesse Barnes79e53942008-11-07 14:24:08 -080093 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -070094
95 mutex_lock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080096 ret = i915_gem_do_init(dev, args->gtt_start, args->gtt_end);
Eric Anholt673a3942008-07-30 12:06:12 -070097 mutex_unlock(&dev->struct_mutex);
98
Jesse Barnes79e53942008-11-07 14:24:08 -080099 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700100}
101
Eric Anholt5a125c32008-10-22 21:40:13 -0700102int
103i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
104 struct drm_file *file_priv)
105{
Eric Anholt5a125c32008-10-22 21:40:13 -0700106 struct drm_i915_gem_get_aperture *args = data;
Eric Anholt5a125c32008-10-22 21:40:13 -0700107
108 if (!(dev->driver->driver_features & DRIVER_GEM))
109 return -ENODEV;
110
111 args->aper_size = dev->gtt_total;
Keith Packard2678d9d2008-11-20 22:54:54 -0800112 args->aper_available_size = (args->aper_size -
113 atomic_read(&dev->pin_memory));
Eric Anholt5a125c32008-10-22 21:40:13 -0700114
115 return 0;
116}
117
Eric Anholt673a3942008-07-30 12:06:12 -0700118
119/**
120 * Creates a new mm object and returns a handle to it.
121 */
122int
123i915_gem_create_ioctl(struct drm_device *dev, void *data,
124 struct drm_file *file_priv)
125{
126 struct drm_i915_gem_create *args = data;
127 struct drm_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300128 int ret;
129 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700130
131 args->size = roundup(args->size, PAGE_SIZE);
132
133 /* Allocate the new object */
Daniel Vetterac52bc52010-04-09 19:05:06 +0000134 obj = i915_gem_alloc_object(dev, args->size);
Eric Anholt673a3942008-07-30 12:06:12 -0700135 if (obj == NULL)
136 return -ENOMEM;
137
138 ret = drm_gem_handle_create(file_priv, obj, &handle);
Dave Airlie29d08b32010-09-27 16:17:17 +1000139 /* drop reference from allocate - handle holds it now */
140 drm_gem_object_unreference_unlocked(obj);
Chris Wilson1dfd9752010-09-06 14:44:14 +0100141 if (ret) {
Eric Anholt673a3942008-07-30 12:06:12 -0700142 return ret;
Chris Wilson1dfd9752010-09-06 14:44:14 +0100143 }
144
Eric Anholt673a3942008-07-30 12:06:12 -0700145 args->handle = handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700146 return 0;
147}
148
Eric Anholt40123c12009-03-09 13:42:30 -0700149static inline int
Eric Anholteb014592009-03-10 11:44:52 -0700150fast_shmem_read(struct page **pages,
151 loff_t page_base, int page_offset,
152 char __user *data,
153 int length)
154{
155 char __iomem *vaddr;
Florian Mickler2bc43b52009-04-06 22:55:41 +0200156 int unwritten;
Eric Anholteb014592009-03-10 11:44:52 -0700157
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700158 vaddr = kmap_atomic(pages[page_base >> PAGE_SHIFT]);
Eric Anholteb014592009-03-10 11:44:52 -0700159 if (vaddr == NULL)
160 return -ENOMEM;
Florian Mickler2bc43b52009-04-06 22:55:41 +0200161 unwritten = __copy_to_user_inatomic(data, vaddr + page_offset, length);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700162 kunmap_atomic(vaddr);
Eric Anholteb014592009-03-10 11:44:52 -0700163
Florian Mickler2bc43b52009-04-06 22:55:41 +0200164 if (unwritten)
165 return -EFAULT;
166
167 return 0;
Eric Anholteb014592009-03-10 11:44:52 -0700168}
169
Eric Anholt280b7132009-03-12 16:56:27 -0700170static int i915_gem_object_needs_bit17_swizzle(struct drm_gem_object *obj)
171{
172 drm_i915_private_t *dev_priv = obj->dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +0100173 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt280b7132009-03-12 16:56:27 -0700174
175 return dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_9_10_17 &&
176 obj_priv->tiling_mode != I915_TILING_NONE;
177}
178
Chris Wilson99a03df2010-05-27 14:15:34 +0100179static inline void
Eric Anholt40123c12009-03-09 13:42:30 -0700180slow_shmem_copy(struct page *dst_page,
181 int dst_offset,
182 struct page *src_page,
183 int src_offset,
184 int length)
185{
186 char *dst_vaddr, *src_vaddr;
187
Chris Wilson99a03df2010-05-27 14:15:34 +0100188 dst_vaddr = kmap(dst_page);
189 src_vaddr = kmap(src_page);
Eric Anholt40123c12009-03-09 13:42:30 -0700190
191 memcpy(dst_vaddr + dst_offset, src_vaddr + src_offset, length);
192
Chris Wilson99a03df2010-05-27 14:15:34 +0100193 kunmap(src_page);
194 kunmap(dst_page);
Eric Anholt40123c12009-03-09 13:42:30 -0700195}
196
Chris Wilson99a03df2010-05-27 14:15:34 +0100197static inline void
Eric Anholt280b7132009-03-12 16:56:27 -0700198slow_shmem_bit17_copy(struct page *gpu_page,
199 int gpu_offset,
200 struct page *cpu_page,
201 int cpu_offset,
202 int length,
203 int is_read)
204{
205 char *gpu_vaddr, *cpu_vaddr;
206
207 /* Use the unswizzled path if this page isn't affected. */
208 if ((page_to_phys(gpu_page) & (1 << 17)) == 0) {
209 if (is_read)
210 return slow_shmem_copy(cpu_page, cpu_offset,
211 gpu_page, gpu_offset, length);
212 else
213 return slow_shmem_copy(gpu_page, gpu_offset,
214 cpu_page, cpu_offset, length);
215 }
216
Chris Wilson99a03df2010-05-27 14:15:34 +0100217 gpu_vaddr = kmap(gpu_page);
218 cpu_vaddr = kmap(cpu_page);
Eric Anholt280b7132009-03-12 16:56:27 -0700219
220 /* Copy the data, XORing A6 with A17 (1). The user already knows he's
221 * XORing with the other bits (A9 for Y, A9 and A10 for X)
222 */
223 while (length > 0) {
224 int cacheline_end = ALIGN(gpu_offset + 1, 64);
225 int this_length = min(cacheline_end - gpu_offset, length);
226 int swizzled_gpu_offset = gpu_offset ^ 64;
227
228 if (is_read) {
229 memcpy(cpu_vaddr + cpu_offset,
230 gpu_vaddr + swizzled_gpu_offset,
231 this_length);
232 } else {
233 memcpy(gpu_vaddr + swizzled_gpu_offset,
234 cpu_vaddr + cpu_offset,
235 this_length);
236 }
237 cpu_offset += this_length;
238 gpu_offset += this_length;
239 length -= this_length;
240 }
241
Chris Wilson99a03df2010-05-27 14:15:34 +0100242 kunmap(cpu_page);
243 kunmap(gpu_page);
Eric Anholt280b7132009-03-12 16:56:27 -0700244}
245
Eric Anholt673a3942008-07-30 12:06:12 -0700246/**
Eric Anholteb014592009-03-10 11:44:52 -0700247 * This is the fast shmem pread path, which attempts to copy_from_user directly
248 * from the backing pages of the object to the user's address space. On a
249 * fault, it fails so we can fall back to i915_gem_shmem_pwrite_slow().
250 */
251static int
252i915_gem_shmem_pread_fast(struct drm_device *dev, struct drm_gem_object *obj,
253 struct drm_i915_gem_pread *args,
254 struct drm_file *file_priv)
255{
Daniel Vetter23010e42010-03-08 13:35:02 +0100256 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700257 ssize_t remain;
258 loff_t offset, page_base;
259 char __user *user_data;
260 int page_offset, page_length;
261 int ret;
262
263 user_data = (char __user *) (uintptr_t) args->data_ptr;
264 remain = args->size;
265
266 mutex_lock(&dev->struct_mutex);
267
Chris Wilson4bdadb92010-01-27 13:36:32 +0000268 ret = i915_gem_object_get_pages(obj, 0);
Eric Anholteb014592009-03-10 11:44:52 -0700269 if (ret != 0)
270 goto fail_unlock;
271
272 ret = i915_gem_object_set_cpu_read_domain_range(obj, args->offset,
273 args->size);
274 if (ret != 0)
275 goto fail_put_pages;
276
Daniel Vetter23010e42010-03-08 13:35:02 +0100277 obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700278 offset = args->offset;
279
280 while (remain > 0) {
281 /* Operation in this page
282 *
283 * page_base = page offset within aperture
284 * page_offset = offset within page
285 * page_length = bytes to copy for this page
286 */
287 page_base = (offset & ~(PAGE_SIZE-1));
288 page_offset = offset & (PAGE_SIZE-1);
289 page_length = remain;
290 if ((page_offset + remain) > PAGE_SIZE)
291 page_length = PAGE_SIZE - page_offset;
292
293 ret = fast_shmem_read(obj_priv->pages,
294 page_base, page_offset,
295 user_data, page_length);
296 if (ret)
297 goto fail_put_pages;
298
299 remain -= page_length;
300 user_data += page_length;
301 offset += page_length;
302 }
303
304fail_put_pages:
305 i915_gem_object_put_pages(obj);
306fail_unlock:
307 mutex_unlock(&dev->struct_mutex);
308
309 return ret;
310}
311
Chris Wilson07f73f62009-09-14 16:50:30 +0100312static int
313i915_gem_object_get_pages_or_evict(struct drm_gem_object *obj)
314{
315 int ret;
316
Chris Wilson4bdadb92010-01-27 13:36:32 +0000317 ret = i915_gem_object_get_pages(obj, __GFP_NORETRY | __GFP_NOWARN);
Chris Wilson07f73f62009-09-14 16:50:30 +0100318
319 /* If we've insufficient memory to map in the pages, attempt
320 * to make some space by throwing out some old buffers.
321 */
322 if (ret == -ENOMEM) {
323 struct drm_device *dev = obj->dev;
Chris Wilson07f73f62009-09-14 16:50:30 +0100324
Daniel Vetter0108a3e2010-08-07 11:01:21 +0100325 ret = i915_gem_evict_something(dev, obj->size,
326 i915_gem_get_gtt_alignment(obj));
Chris Wilson07f73f62009-09-14 16:50:30 +0100327 if (ret)
328 return ret;
329
Chris Wilson4bdadb92010-01-27 13:36:32 +0000330 ret = i915_gem_object_get_pages(obj, 0);
Chris Wilson07f73f62009-09-14 16:50:30 +0100331 }
332
333 return ret;
334}
335
Eric Anholteb014592009-03-10 11:44:52 -0700336/**
337 * This is the fallback shmem pread path, which allocates temporary storage
338 * in kernel space to copy_to_user into outside of the struct_mutex, so we
339 * can copy out of the object's backing pages while holding the struct mutex
340 * and not take page faults.
341 */
342static int
343i915_gem_shmem_pread_slow(struct drm_device *dev, struct drm_gem_object *obj,
344 struct drm_i915_gem_pread *args,
345 struct drm_file *file_priv)
346{
Daniel Vetter23010e42010-03-08 13:35:02 +0100347 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700348 struct mm_struct *mm = current->mm;
349 struct page **user_pages;
350 ssize_t remain;
351 loff_t offset, pinned_pages, i;
352 loff_t first_data_page, last_data_page, num_pages;
353 int shmem_page_index, shmem_page_offset;
354 int data_page_index, data_page_offset;
355 int page_length;
356 int ret;
357 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700358 int do_bit17_swizzling;
Eric Anholteb014592009-03-10 11:44:52 -0700359
360 remain = args->size;
361
362 /* Pin the user pages containing the data. We can't fault while
363 * holding the struct mutex, yet we want to hold it while
364 * dereferencing the user data.
365 */
366 first_data_page = data_ptr / PAGE_SIZE;
367 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
368 num_pages = last_data_page - first_data_page + 1;
369
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700370 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
Eric Anholteb014592009-03-10 11:44:52 -0700371 if (user_pages == NULL)
372 return -ENOMEM;
373
374 down_read(&mm->mmap_sem);
375 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
Eric Anholte5e9ecd2009-04-07 16:01:22 -0700376 num_pages, 1, 0, user_pages, NULL);
Eric Anholteb014592009-03-10 11:44:52 -0700377 up_read(&mm->mmap_sem);
378 if (pinned_pages < num_pages) {
379 ret = -EFAULT;
380 goto fail_put_user_pages;
381 }
382
Eric Anholt280b7132009-03-12 16:56:27 -0700383 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
384
Eric Anholteb014592009-03-10 11:44:52 -0700385 mutex_lock(&dev->struct_mutex);
386
Chris Wilson07f73f62009-09-14 16:50:30 +0100387 ret = i915_gem_object_get_pages_or_evict(obj);
388 if (ret)
Eric Anholteb014592009-03-10 11:44:52 -0700389 goto fail_unlock;
390
391 ret = i915_gem_object_set_cpu_read_domain_range(obj, args->offset,
392 args->size);
393 if (ret != 0)
394 goto fail_put_pages;
395
Daniel Vetter23010e42010-03-08 13:35:02 +0100396 obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700397 offset = args->offset;
398
399 while (remain > 0) {
400 /* Operation in this page
401 *
402 * shmem_page_index = page number within shmem file
403 * shmem_page_offset = offset within page in shmem file
404 * data_page_index = page number in get_user_pages return
405 * data_page_offset = offset with data_page_index page.
406 * page_length = bytes to copy for this page
407 */
408 shmem_page_index = offset / PAGE_SIZE;
409 shmem_page_offset = offset & ~PAGE_MASK;
410 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
411 data_page_offset = data_ptr & ~PAGE_MASK;
412
413 page_length = remain;
414 if ((shmem_page_offset + page_length) > PAGE_SIZE)
415 page_length = PAGE_SIZE - shmem_page_offset;
416 if ((data_page_offset + page_length) > PAGE_SIZE)
417 page_length = PAGE_SIZE - data_page_offset;
418
Eric Anholt280b7132009-03-12 16:56:27 -0700419 if (do_bit17_swizzling) {
Chris Wilson99a03df2010-05-27 14:15:34 +0100420 slow_shmem_bit17_copy(obj_priv->pages[shmem_page_index],
Eric Anholt280b7132009-03-12 16:56:27 -0700421 shmem_page_offset,
Chris Wilson99a03df2010-05-27 14:15:34 +0100422 user_pages[data_page_index],
423 data_page_offset,
424 page_length,
425 1);
426 } else {
427 slow_shmem_copy(user_pages[data_page_index],
428 data_page_offset,
429 obj_priv->pages[shmem_page_index],
430 shmem_page_offset,
431 page_length);
Eric Anholt280b7132009-03-12 16:56:27 -0700432 }
Eric Anholteb014592009-03-10 11:44:52 -0700433
434 remain -= page_length;
435 data_ptr += page_length;
436 offset += page_length;
437 }
438
439fail_put_pages:
440 i915_gem_object_put_pages(obj);
441fail_unlock:
442 mutex_unlock(&dev->struct_mutex);
443fail_put_user_pages:
444 for (i = 0; i < pinned_pages; i++) {
445 SetPageDirty(user_pages[i]);
446 page_cache_release(user_pages[i]);
447 }
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700448 drm_free_large(user_pages);
Eric Anholteb014592009-03-10 11:44:52 -0700449
450 return ret;
451}
452
Eric Anholt673a3942008-07-30 12:06:12 -0700453/**
454 * Reads data from the object referenced by handle.
455 *
456 * On error, the contents of *data are undefined.
457 */
458int
459i915_gem_pread_ioctl(struct drm_device *dev, void *data,
460 struct drm_file *file_priv)
461{
462 struct drm_i915_gem_pread *args = data;
463 struct drm_gem_object *obj;
464 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -0700465 int ret;
466
467 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
468 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100469 return -ENOENT;
Daniel Vetter23010e42010-03-08 13:35:02 +0100470 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700471
Chris Wilson7dcd2492010-09-26 20:21:44 +0100472 /* Bounds check source. */
473 if (args->offset > obj->size || args->size > obj->size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +0100474 ret = -EINVAL;
475 goto err;
476 }
477
478 if (!access_ok(VERIFY_WRITE,
479 (char __user *)(uintptr_t)args->data_ptr,
480 args->size)) {
481 ret = -EFAULT;
482 goto err;
Eric Anholt673a3942008-07-30 12:06:12 -0700483 }
484
Eric Anholt280b7132009-03-12 16:56:27 -0700485 if (i915_gem_object_needs_bit17_swizzle(obj)) {
Eric Anholteb014592009-03-10 11:44:52 -0700486 ret = i915_gem_shmem_pread_slow(dev, obj, args, file_priv);
Eric Anholt280b7132009-03-12 16:56:27 -0700487 } else {
488 ret = i915_gem_shmem_pread_fast(dev, obj, args, file_priv);
489 if (ret != 0)
490 ret = i915_gem_shmem_pread_slow(dev, obj, args,
491 file_priv);
492 }
Eric Anholt673a3942008-07-30 12:06:12 -0700493
Chris Wilsonce9d4192010-09-26 20:50:05 +0100494err:
Luca Barbieribc9025b2010-02-09 05:49:12 +0000495 drm_gem_object_unreference_unlocked(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700496 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700497}
498
Keith Packard0839ccb2008-10-30 19:38:48 -0700499/* This is the fast write path which cannot handle
500 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700501 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700502
Keith Packard0839ccb2008-10-30 19:38:48 -0700503static inline int
504fast_user_write(struct io_mapping *mapping,
505 loff_t page_base, int page_offset,
506 char __user *user_data,
507 int length)
508{
509 char *vaddr_atomic;
510 unsigned long unwritten;
511
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700512 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
Keith Packard0839ccb2008-10-30 19:38:48 -0700513 unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
514 user_data, length);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700515 io_mapping_unmap_atomic(vaddr_atomic);
Keith Packard0839ccb2008-10-30 19:38:48 -0700516 if (unwritten)
517 return -EFAULT;
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700518 return 0;
Keith Packard0839ccb2008-10-30 19:38:48 -0700519}
520
521/* Here's the write path which can sleep for
522 * page faults
523 */
524
Chris Wilsonab34c222010-05-27 14:15:35 +0100525static inline void
Eric Anholt3de09aa2009-03-09 09:42:23 -0700526slow_kernel_write(struct io_mapping *mapping,
527 loff_t gtt_base, int gtt_offset,
528 struct page *user_page, int user_offset,
529 int length)
Keith Packard0839ccb2008-10-30 19:38:48 -0700530{
Chris Wilsonab34c222010-05-27 14:15:35 +0100531 char __iomem *dst_vaddr;
532 char *src_vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -0700533
Chris Wilsonab34c222010-05-27 14:15:35 +0100534 dst_vaddr = io_mapping_map_wc(mapping, gtt_base);
535 src_vaddr = kmap(user_page);
536
537 memcpy_toio(dst_vaddr + gtt_offset,
538 src_vaddr + user_offset,
539 length);
540
541 kunmap(user_page);
542 io_mapping_unmap(dst_vaddr);
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700543}
544
Eric Anholt40123c12009-03-09 13:42:30 -0700545static inline int
546fast_shmem_write(struct page **pages,
547 loff_t page_base, int page_offset,
548 char __user *data,
549 int length)
550{
551 char __iomem *vaddr;
Dave Airlied0088772009-03-28 20:29:48 -0400552 unsigned long unwritten;
Eric Anholt40123c12009-03-09 13:42:30 -0700553
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700554 vaddr = kmap_atomic(pages[page_base >> PAGE_SHIFT]);
Eric Anholt40123c12009-03-09 13:42:30 -0700555 if (vaddr == NULL)
556 return -ENOMEM;
Dave Airlied0088772009-03-28 20:29:48 -0400557 unwritten = __copy_from_user_inatomic(vaddr + page_offset, data, length);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700558 kunmap_atomic(vaddr);
Eric Anholt40123c12009-03-09 13:42:30 -0700559
Dave Airlied0088772009-03-28 20:29:48 -0400560 if (unwritten)
561 return -EFAULT;
Eric Anholt40123c12009-03-09 13:42:30 -0700562 return 0;
563}
564
Eric Anholt3de09aa2009-03-09 09:42:23 -0700565/**
566 * This is the fast pwrite path, where we copy the data directly from the
567 * user into the GTT, uncached.
568 */
Eric Anholt673a3942008-07-30 12:06:12 -0700569static int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700570i915_gem_gtt_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
571 struct drm_i915_gem_pwrite *args,
572 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700573{
Daniel Vetter23010e42010-03-08 13:35:02 +0100574 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Keith Packard0839ccb2008-10-30 19:38:48 -0700575 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700576 ssize_t remain;
Keith Packard0839ccb2008-10-30 19:38:48 -0700577 loff_t offset, page_base;
Eric Anholt673a3942008-07-30 12:06:12 -0700578 char __user *user_data;
Keith Packard0839ccb2008-10-30 19:38:48 -0700579 int page_offset, page_length;
580 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700581
582 user_data = (char __user *) (uintptr_t) args->data_ptr;
583 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700584
585
586 mutex_lock(&dev->struct_mutex);
587 ret = i915_gem_object_pin(obj, 0);
588 if (ret) {
589 mutex_unlock(&dev->struct_mutex);
590 return ret;
591 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800592 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
Eric Anholt673a3942008-07-30 12:06:12 -0700593 if (ret)
594 goto fail;
595
Daniel Vetter23010e42010-03-08 13:35:02 +0100596 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700597 offset = obj_priv->gtt_offset + args->offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700598
599 while (remain > 0) {
600 /* Operation in this page
601 *
Keith Packard0839ccb2008-10-30 19:38:48 -0700602 * page_base = page offset within aperture
603 * page_offset = offset within page
604 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -0700605 */
Keith Packard0839ccb2008-10-30 19:38:48 -0700606 page_base = (offset & ~(PAGE_SIZE-1));
607 page_offset = offset & (PAGE_SIZE-1);
608 page_length = remain;
609 if ((page_offset + remain) > PAGE_SIZE)
610 page_length = PAGE_SIZE - page_offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700611
Keith Packard0839ccb2008-10-30 19:38:48 -0700612 ret = fast_user_write (dev_priv->mm.gtt_mapping, page_base,
613 page_offset, user_data, page_length);
Eric Anholt673a3942008-07-30 12:06:12 -0700614
Keith Packard0839ccb2008-10-30 19:38:48 -0700615 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -0700616 * source page isn't available. Return the error and we'll
617 * retry in the slow path.
Keith Packard0839ccb2008-10-30 19:38:48 -0700618 */
Eric Anholt3de09aa2009-03-09 09:42:23 -0700619 if (ret)
620 goto fail;
Eric Anholt673a3942008-07-30 12:06:12 -0700621
Keith Packard0839ccb2008-10-30 19:38:48 -0700622 remain -= page_length;
623 user_data += page_length;
624 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700625 }
Eric Anholt673a3942008-07-30 12:06:12 -0700626
627fail:
628 i915_gem_object_unpin(obj);
629 mutex_unlock(&dev->struct_mutex);
630
631 return ret;
632}
633
Eric Anholt3de09aa2009-03-09 09:42:23 -0700634/**
635 * This is the fallback GTT pwrite path, which uses get_user_pages to pin
636 * the memory and maps it using kmap_atomic for copying.
637 *
638 * This code resulted in x11perf -rgb10text consuming about 10% more CPU
639 * than using i915_gem_gtt_pwrite_fast on a G45 (32-bit).
640 */
Eric Anholt3043c602008-10-02 12:24:47 -0700641static int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700642i915_gem_gtt_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
643 struct drm_i915_gem_pwrite *args,
644 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700645{
Daniel Vetter23010e42010-03-08 13:35:02 +0100646 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700647 drm_i915_private_t *dev_priv = dev->dev_private;
648 ssize_t remain;
649 loff_t gtt_page_base, offset;
650 loff_t first_data_page, last_data_page, num_pages;
651 loff_t pinned_pages, i;
652 struct page **user_pages;
653 struct mm_struct *mm = current->mm;
654 int gtt_page_offset, data_page_offset, data_page_index, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700655 int ret;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700656 uint64_t data_ptr = args->data_ptr;
657
658 remain = args->size;
659
660 /* Pin the user pages containing the data. We can't fault while
661 * holding the struct mutex, and all of the pwrite implementations
662 * want to hold it while dereferencing the user data.
663 */
664 first_data_page = data_ptr / PAGE_SIZE;
665 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
666 num_pages = last_data_page - first_data_page + 1;
667
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700668 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
Eric Anholt3de09aa2009-03-09 09:42:23 -0700669 if (user_pages == NULL)
670 return -ENOMEM;
671
672 down_read(&mm->mmap_sem);
673 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
674 num_pages, 0, 0, user_pages, NULL);
675 up_read(&mm->mmap_sem);
676 if (pinned_pages < num_pages) {
677 ret = -EFAULT;
678 goto out_unpin_pages;
679 }
680
681 mutex_lock(&dev->struct_mutex);
682 ret = i915_gem_object_pin(obj, 0);
683 if (ret)
684 goto out_unlock;
685
686 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
687 if (ret)
688 goto out_unpin_object;
689
Daniel Vetter23010e42010-03-08 13:35:02 +0100690 obj_priv = to_intel_bo(obj);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700691 offset = obj_priv->gtt_offset + args->offset;
692
693 while (remain > 0) {
694 /* Operation in this page
695 *
696 * gtt_page_base = page offset within aperture
697 * gtt_page_offset = offset within page in aperture
698 * data_page_index = page number in get_user_pages return
699 * data_page_offset = offset with data_page_index page.
700 * page_length = bytes to copy for this page
701 */
702 gtt_page_base = offset & PAGE_MASK;
703 gtt_page_offset = offset & ~PAGE_MASK;
704 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
705 data_page_offset = data_ptr & ~PAGE_MASK;
706
707 page_length = remain;
708 if ((gtt_page_offset + page_length) > PAGE_SIZE)
709 page_length = PAGE_SIZE - gtt_page_offset;
710 if ((data_page_offset + page_length) > PAGE_SIZE)
711 page_length = PAGE_SIZE - data_page_offset;
712
Chris Wilsonab34c222010-05-27 14:15:35 +0100713 slow_kernel_write(dev_priv->mm.gtt_mapping,
714 gtt_page_base, gtt_page_offset,
715 user_pages[data_page_index],
716 data_page_offset,
717 page_length);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700718
719 remain -= page_length;
720 offset += page_length;
721 data_ptr += page_length;
722 }
723
724out_unpin_object:
725 i915_gem_object_unpin(obj);
726out_unlock:
727 mutex_unlock(&dev->struct_mutex);
728out_unpin_pages:
729 for (i = 0; i < pinned_pages; i++)
730 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700731 drm_free_large(user_pages);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700732
733 return ret;
734}
735
Eric Anholt40123c12009-03-09 13:42:30 -0700736/**
737 * This is the fast shmem pwrite path, which attempts to directly
738 * copy_from_user into the kmapped pages backing the object.
739 */
Eric Anholt673a3942008-07-30 12:06:12 -0700740static int
Eric Anholt40123c12009-03-09 13:42:30 -0700741i915_gem_shmem_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
742 struct drm_i915_gem_pwrite *args,
743 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700744{
Daniel Vetter23010e42010-03-08 13:35:02 +0100745 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700746 ssize_t remain;
747 loff_t offset, page_base;
748 char __user *user_data;
749 int page_offset, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700750 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -0700751
752 user_data = (char __user *) (uintptr_t) args->data_ptr;
753 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700754
755 mutex_lock(&dev->struct_mutex);
756
Chris Wilson4bdadb92010-01-27 13:36:32 +0000757 ret = i915_gem_object_get_pages(obj, 0);
Eric Anholt40123c12009-03-09 13:42:30 -0700758 if (ret != 0)
759 goto fail_unlock;
760
Eric Anholte47c68e2008-11-14 13:35:19 -0800761 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Eric Anholt40123c12009-03-09 13:42:30 -0700762 if (ret != 0)
763 goto fail_put_pages;
Eric Anholt673a3942008-07-30 12:06:12 -0700764
Daniel Vetter23010e42010-03-08 13:35:02 +0100765 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700766 offset = args->offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700767 obj_priv->dirty = 1;
Eric Anholt673a3942008-07-30 12:06:12 -0700768
Eric Anholt40123c12009-03-09 13:42:30 -0700769 while (remain > 0) {
770 /* Operation in this page
771 *
772 * page_base = page offset within aperture
773 * page_offset = offset within page
774 * page_length = bytes to copy for this page
775 */
776 page_base = (offset & ~(PAGE_SIZE-1));
777 page_offset = offset & (PAGE_SIZE-1);
778 page_length = remain;
779 if ((page_offset + remain) > PAGE_SIZE)
780 page_length = PAGE_SIZE - page_offset;
781
782 ret = fast_shmem_write(obj_priv->pages,
783 page_base, page_offset,
784 user_data, page_length);
785 if (ret)
786 goto fail_put_pages;
787
788 remain -= page_length;
789 user_data += page_length;
790 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700791 }
792
Eric Anholt40123c12009-03-09 13:42:30 -0700793fail_put_pages:
794 i915_gem_object_put_pages(obj);
795fail_unlock:
Eric Anholt673a3942008-07-30 12:06:12 -0700796 mutex_unlock(&dev->struct_mutex);
797
Eric Anholt40123c12009-03-09 13:42:30 -0700798 return ret;
799}
800
801/**
802 * This is the fallback shmem pwrite path, which uses get_user_pages to pin
803 * the memory and maps it using kmap_atomic for copying.
804 *
805 * This avoids taking mmap_sem for faulting on the user's address while the
806 * struct_mutex is held.
807 */
808static int
809i915_gem_shmem_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
810 struct drm_i915_gem_pwrite *args,
811 struct drm_file *file_priv)
812{
Daniel Vetter23010e42010-03-08 13:35:02 +0100813 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700814 struct mm_struct *mm = current->mm;
815 struct page **user_pages;
816 ssize_t remain;
817 loff_t offset, pinned_pages, i;
818 loff_t first_data_page, last_data_page, num_pages;
819 int shmem_page_index, shmem_page_offset;
820 int data_page_index, data_page_offset;
821 int page_length;
822 int ret;
823 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700824 int do_bit17_swizzling;
Eric Anholt40123c12009-03-09 13:42:30 -0700825
826 remain = args->size;
827
828 /* Pin the user pages containing the data. We can't fault while
829 * holding the struct mutex, and all of the pwrite implementations
830 * want to hold it while dereferencing the user data.
831 */
832 first_data_page = data_ptr / PAGE_SIZE;
833 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
834 num_pages = last_data_page - first_data_page + 1;
835
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700836 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
Eric Anholt40123c12009-03-09 13:42:30 -0700837 if (user_pages == NULL)
838 return -ENOMEM;
839
840 down_read(&mm->mmap_sem);
841 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
842 num_pages, 0, 0, user_pages, NULL);
843 up_read(&mm->mmap_sem);
844 if (pinned_pages < num_pages) {
845 ret = -EFAULT;
846 goto fail_put_user_pages;
847 }
848
Eric Anholt280b7132009-03-12 16:56:27 -0700849 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
850
Eric Anholt40123c12009-03-09 13:42:30 -0700851 mutex_lock(&dev->struct_mutex);
852
Chris Wilson07f73f62009-09-14 16:50:30 +0100853 ret = i915_gem_object_get_pages_or_evict(obj);
854 if (ret)
Eric Anholt40123c12009-03-09 13:42:30 -0700855 goto fail_unlock;
856
857 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
858 if (ret != 0)
859 goto fail_put_pages;
860
Daniel Vetter23010e42010-03-08 13:35:02 +0100861 obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700862 offset = args->offset;
863 obj_priv->dirty = 1;
864
865 while (remain > 0) {
866 /* Operation in this page
867 *
868 * shmem_page_index = page number within shmem file
869 * shmem_page_offset = offset within page in shmem file
870 * data_page_index = page number in get_user_pages return
871 * data_page_offset = offset with data_page_index page.
872 * page_length = bytes to copy for this page
873 */
874 shmem_page_index = offset / PAGE_SIZE;
875 shmem_page_offset = offset & ~PAGE_MASK;
876 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
877 data_page_offset = data_ptr & ~PAGE_MASK;
878
879 page_length = remain;
880 if ((shmem_page_offset + page_length) > PAGE_SIZE)
881 page_length = PAGE_SIZE - shmem_page_offset;
882 if ((data_page_offset + page_length) > PAGE_SIZE)
883 page_length = PAGE_SIZE - data_page_offset;
884
Eric Anholt280b7132009-03-12 16:56:27 -0700885 if (do_bit17_swizzling) {
Chris Wilson99a03df2010-05-27 14:15:34 +0100886 slow_shmem_bit17_copy(obj_priv->pages[shmem_page_index],
Eric Anholt280b7132009-03-12 16:56:27 -0700887 shmem_page_offset,
888 user_pages[data_page_index],
889 data_page_offset,
Chris Wilson99a03df2010-05-27 14:15:34 +0100890 page_length,
891 0);
892 } else {
893 slow_shmem_copy(obj_priv->pages[shmem_page_index],
894 shmem_page_offset,
895 user_pages[data_page_index],
896 data_page_offset,
897 page_length);
Eric Anholt280b7132009-03-12 16:56:27 -0700898 }
Eric Anholt40123c12009-03-09 13:42:30 -0700899
900 remain -= page_length;
901 data_ptr += page_length;
902 offset += page_length;
903 }
904
905fail_put_pages:
906 i915_gem_object_put_pages(obj);
907fail_unlock:
908 mutex_unlock(&dev->struct_mutex);
909fail_put_user_pages:
910 for (i = 0; i < pinned_pages; i++)
911 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700912 drm_free_large(user_pages);
Eric Anholt40123c12009-03-09 13:42:30 -0700913
914 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700915}
916
917/**
918 * Writes data to the object referenced by handle.
919 *
920 * On error, the contents of the buffer that were to be modified are undefined.
921 */
922int
923i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
924 struct drm_file *file_priv)
925{
926 struct drm_i915_gem_pwrite *args = data;
927 struct drm_gem_object *obj;
928 struct drm_i915_gem_object *obj_priv;
929 int ret = 0;
930
931 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
932 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100933 return -ENOENT;
Daniel Vetter23010e42010-03-08 13:35:02 +0100934 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700935
Chris Wilson7dcd2492010-09-26 20:21:44 +0100936 /* Bounds check destination. */
937 if (args->offset > obj->size || args->size > obj->size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +0100938 ret = -EINVAL;
939 goto err;
940 }
941
942 if (!access_ok(VERIFY_READ,
943 (char __user *)(uintptr_t)args->data_ptr,
944 args->size)) {
945 ret = -EFAULT;
946 goto err;
Eric Anholt673a3942008-07-30 12:06:12 -0700947 }
948
949 /* We can only do the GTT pwrite on untiled buffers, as otherwise
950 * it would end up going through the fenced access, and we'll get
951 * different detiling behavior between reading and writing.
952 * pread/pwrite currently are reading and writing from the CPU
953 * perspective, requiring manual detiling by the client.
954 */
Dave Airlie71acb5e2008-12-30 20:31:46 +1000955 if (obj_priv->phys_obj)
956 ret = i915_gem_phys_pwrite(dev, obj, args, file_priv);
957 else if (obj_priv->tiling_mode == I915_TILING_NONE &&
Chris Wilson9b8c4a02010-05-27 14:21:01 +0100958 dev->gtt_total != 0 &&
959 obj->write_domain != I915_GEM_DOMAIN_CPU) {
Eric Anholt3de09aa2009-03-09 09:42:23 -0700960 ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file_priv);
961 if (ret == -EFAULT) {
962 ret = i915_gem_gtt_pwrite_slow(dev, obj, args,
963 file_priv);
964 }
Eric Anholt280b7132009-03-12 16:56:27 -0700965 } else if (i915_gem_object_needs_bit17_swizzle(obj)) {
966 ret = i915_gem_shmem_pwrite_slow(dev, obj, args, file_priv);
Eric Anholt40123c12009-03-09 13:42:30 -0700967 } else {
968 ret = i915_gem_shmem_pwrite_fast(dev, obj, args, file_priv);
969 if (ret == -EFAULT) {
970 ret = i915_gem_shmem_pwrite_slow(dev, obj, args,
971 file_priv);
972 }
973 }
Eric Anholt673a3942008-07-30 12:06:12 -0700974
975#if WATCH_PWRITE
976 if (ret)
977 DRM_INFO("pwrite failed %d\n", ret);
978#endif
979
Chris Wilsonce9d4192010-09-26 20:50:05 +0100980err:
Luca Barbieribc9025b2010-02-09 05:49:12 +0000981 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700982 return ret;
983}
984
985/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800986 * Called when user space prepares to use an object with the CPU, either
987 * through the mmap ioctl's mapping or a GTT mapping.
Eric Anholt673a3942008-07-30 12:06:12 -0700988 */
989int
990i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
991 struct drm_file *file_priv)
992{
Eric Anholta09ba7f2009-08-29 12:49:51 -0700993 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700994 struct drm_i915_gem_set_domain *args = data;
995 struct drm_gem_object *obj;
Jesse Barnes652c3932009-08-17 13:31:43 -0700996 struct drm_i915_gem_object *obj_priv;
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800997 uint32_t read_domains = args->read_domains;
998 uint32_t write_domain = args->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -0700999 int ret;
1000
1001 if (!(dev->driver->driver_features & DRIVER_GEM))
1002 return -ENODEV;
1003
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001004 /* Only handle setting domains to types used by the CPU. */
Chris Wilson21d509e2009-06-06 09:46:02 +01001005 if (write_domain & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001006 return -EINVAL;
1007
Chris Wilson21d509e2009-06-06 09:46:02 +01001008 if (read_domains & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001009 return -EINVAL;
1010
1011 /* Having something in the write domain implies it's in the read
1012 * domain, and only that read domain. Enforce that in the request.
1013 */
1014 if (write_domain != 0 && read_domains != write_domain)
1015 return -EINVAL;
1016
Eric Anholt673a3942008-07-30 12:06:12 -07001017 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1018 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001019 return -ENOENT;
Daniel Vetter23010e42010-03-08 13:35:02 +01001020 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001021
1022 mutex_lock(&dev->struct_mutex);
Jesse Barnes652c3932009-08-17 13:31:43 -07001023
1024 intel_mark_busy(dev, obj);
1025
Eric Anholt673a3942008-07-30 12:06:12 -07001026#if WATCH_BUF
Krzysztof Halasacfd43c02009-06-20 00:31:28 +02001027 DRM_INFO("set_domain_ioctl %p(%zd), %08x %08x\n",
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001028 obj, obj->size, read_domains, write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07001029#endif
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001030 if (read_domains & I915_GEM_DOMAIN_GTT) {
1031 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
Eric Anholt02354392008-11-26 13:58:13 -08001032
Eric Anholta09ba7f2009-08-29 12:49:51 -07001033 /* Update the LRU on the fence for the CPU access that's
1034 * about to occur.
1035 */
1036 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001037 struct drm_i915_fence_reg *reg =
1038 &dev_priv->fence_regs[obj_priv->fence_reg];
1039 list_move_tail(&reg->lru_list,
Eric Anholta09ba7f2009-08-29 12:49:51 -07001040 &dev_priv->mm.fence_list);
1041 }
1042
Eric Anholt02354392008-11-26 13:58:13 -08001043 /* Silently promote "you're not bound, there was nothing to do"
1044 * to success, since the client was just asking us to
1045 * make sure everything was done.
1046 */
1047 if (ret == -EINVAL)
1048 ret = 0;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001049 } else {
Eric Anholte47c68e2008-11-14 13:35:19 -08001050 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001051 }
1052
Chris Wilson7d1c4802010-08-07 21:45:03 +01001053
1054 /* Maintain LRU order of "inactive" objects */
1055 if (ret == 0 && i915_gem_object_is_inactive(obj_priv))
1056 list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
1057
Eric Anholt673a3942008-07-30 12:06:12 -07001058 drm_gem_object_unreference(obj);
1059 mutex_unlock(&dev->struct_mutex);
1060 return ret;
1061}
1062
1063/**
1064 * Called when user space has done writes to this buffer
1065 */
1066int
1067i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
1068 struct drm_file *file_priv)
1069{
1070 struct drm_i915_gem_sw_finish *args = data;
1071 struct drm_gem_object *obj;
1072 struct drm_i915_gem_object *obj_priv;
1073 int ret = 0;
1074
1075 if (!(dev->driver->driver_features & DRIVER_GEM))
1076 return -ENODEV;
1077
1078 mutex_lock(&dev->struct_mutex);
1079 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1080 if (obj == NULL) {
1081 mutex_unlock(&dev->struct_mutex);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001082 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001083 }
1084
1085#if WATCH_BUF
Krzysztof Halasacfd43c02009-06-20 00:31:28 +02001086 DRM_INFO("%s: sw_finish %d (%p %zd)\n",
Eric Anholt673a3942008-07-30 12:06:12 -07001087 __func__, args->handle, obj, obj->size);
1088#endif
Daniel Vetter23010e42010-03-08 13:35:02 +01001089 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001090
1091 /* Pinned buffers may be scanout, so flush the cache */
Eric Anholte47c68e2008-11-14 13:35:19 -08001092 if (obj_priv->pin_count)
1093 i915_gem_object_flush_cpu_write_domain(obj);
1094
Eric Anholt673a3942008-07-30 12:06:12 -07001095 drm_gem_object_unreference(obj);
1096 mutex_unlock(&dev->struct_mutex);
1097 return ret;
1098}
1099
1100/**
1101 * Maps the contents of an object, returning the address it is mapped
1102 * into.
1103 *
1104 * While the mapping holds a reference on the contents of the object, it doesn't
1105 * imply a ref on the object itself.
1106 */
1107int
1108i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
1109 struct drm_file *file_priv)
1110{
1111 struct drm_i915_gem_mmap *args = data;
1112 struct drm_gem_object *obj;
1113 loff_t offset;
1114 unsigned long addr;
1115
1116 if (!(dev->driver->driver_features & DRIVER_GEM))
1117 return -ENODEV;
1118
1119 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1120 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001121 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001122
1123 offset = args->offset;
1124
1125 down_write(&current->mm->mmap_sem);
1126 addr = do_mmap(obj->filp, 0, args->size,
1127 PROT_READ | PROT_WRITE, MAP_SHARED,
1128 args->offset);
1129 up_write(&current->mm->mmap_sem);
Luca Barbieribc9025b2010-02-09 05:49:12 +00001130 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001131 if (IS_ERR((void *)addr))
1132 return addr;
1133
1134 args->addr_ptr = (uint64_t) addr;
1135
1136 return 0;
1137}
1138
Jesse Barnesde151cf2008-11-12 10:03:55 -08001139/**
1140 * i915_gem_fault - fault a page into the GTT
1141 * vma: VMA in question
1142 * vmf: fault info
1143 *
1144 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1145 * from userspace. The fault handler takes care of binding the object to
1146 * the GTT (if needed), allocating and programming a fence register (again,
1147 * only if needed based on whether the old reg is still valid or the object
1148 * is tiled) and inserting a new PTE into the faulting process.
1149 *
1150 * Note that the faulting process may involve evicting existing objects
1151 * from the GTT and/or fence registers to make room. So performance may
1152 * suffer if the GTT working set is large or there are few fence registers
1153 * left.
1154 */
1155int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1156{
1157 struct drm_gem_object *obj = vma->vm_private_data;
1158 struct drm_device *dev = obj->dev;
Chris Wilson7d1c4802010-08-07 21:45:03 +01001159 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001160 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001161 pgoff_t page_offset;
1162 unsigned long pfn;
1163 int ret = 0;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001164 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001165
1166 /* We don't use vmf->pgoff since that has the fake offset */
1167 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1168 PAGE_SHIFT;
1169
1170 /* Now bind it into the GTT if needed */
1171 mutex_lock(&dev->struct_mutex);
1172 if (!obj_priv->gtt_space) {
Chris Wilsone67b8ce2009-09-14 16:50:26 +01001173 ret = i915_gem_object_bind_to_gtt(obj, 0);
Chris Wilsonc7150892009-09-23 00:43:56 +01001174 if (ret)
1175 goto unlock;
Kristian Høgsberg07f4f3e2009-05-27 14:37:28 -04001176
Jesse Barnesde151cf2008-11-12 10:03:55 -08001177 ret = i915_gem_object_set_to_gtt_domain(obj, write);
Chris Wilsonc7150892009-09-23 00:43:56 +01001178 if (ret)
1179 goto unlock;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001180 }
1181
1182 /* Need a new fence register? */
Eric Anholta09ba7f2009-08-29 12:49:51 -07001183 if (obj_priv->tiling_mode != I915_TILING_NONE) {
Chris Wilson8c4b8c32009-06-17 22:08:52 +01001184 ret = i915_gem_object_get_fence_reg(obj);
Chris Wilsonc7150892009-09-23 00:43:56 +01001185 if (ret)
1186 goto unlock;
Eric Anholtd9ddcb92009-01-27 10:33:49 -08001187 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001188
Chris Wilson7d1c4802010-08-07 21:45:03 +01001189 if (i915_gem_object_is_inactive(obj_priv))
1190 list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
1191
Jesse Barnesde151cf2008-11-12 10:03:55 -08001192 pfn = ((dev->agp->base + obj_priv->gtt_offset) >> PAGE_SHIFT) +
1193 page_offset;
1194
1195 /* Finally, remap it using the new GTT offset */
1196 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
Chris Wilsonc7150892009-09-23 00:43:56 +01001197unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001198 mutex_unlock(&dev->struct_mutex);
1199
1200 switch (ret) {
Chris Wilsonc7150892009-09-23 00:43:56 +01001201 case 0:
1202 case -ERESTARTSYS:
1203 return VM_FAULT_NOPAGE;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001204 case -ENOMEM:
1205 case -EAGAIN:
1206 return VM_FAULT_OOM;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001207 default:
Chris Wilsonc7150892009-09-23 00:43:56 +01001208 return VM_FAULT_SIGBUS;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001209 }
1210}
1211
1212/**
1213 * i915_gem_create_mmap_offset - create a fake mmap offset for an object
1214 * @obj: obj in question
1215 *
1216 * GEM memory mapping works by handing back to userspace a fake mmap offset
1217 * it can use in a subsequent mmap(2) call. The DRM core code then looks
1218 * up the object based on the offset and sets up the various memory mapping
1219 * structures.
1220 *
1221 * This routine allocates and attaches a fake offset for @obj.
1222 */
1223static int
1224i915_gem_create_mmap_offset(struct drm_gem_object *obj)
1225{
1226 struct drm_device *dev = obj->dev;
1227 struct drm_gem_mm *mm = dev->mm_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001228 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001229 struct drm_map_list *list;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +11001230 struct drm_local_map *map;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001231 int ret = 0;
1232
1233 /* Set the object up for mmap'ing */
1234 list = &obj->map_list;
Eric Anholt9a298b22009-03-24 12:23:04 -07001235 list->map = kzalloc(sizeof(struct drm_map_list), GFP_KERNEL);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001236 if (!list->map)
1237 return -ENOMEM;
1238
1239 map = list->map;
1240 map->type = _DRM_GEM;
1241 map->size = obj->size;
1242 map->handle = obj;
1243
1244 /* Get a DRM GEM mmap offset allocated... */
1245 list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
1246 obj->size / PAGE_SIZE, 0, 0);
1247 if (!list->file_offset_node) {
1248 DRM_ERROR("failed to allocate offset for bo %d\n", obj->name);
1249 ret = -ENOMEM;
1250 goto out_free_list;
1251 }
1252
1253 list->file_offset_node = drm_mm_get_block(list->file_offset_node,
1254 obj->size / PAGE_SIZE, 0);
1255 if (!list->file_offset_node) {
1256 ret = -ENOMEM;
1257 goto out_free_list;
1258 }
1259
1260 list->hash.key = list->file_offset_node->start;
1261 if (drm_ht_insert_item(&mm->offset_hash, &list->hash)) {
1262 DRM_ERROR("failed to add to map hash\n");
Chris Wilson5618ca62009-12-02 15:15:30 +00001263 ret = -ENOMEM;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001264 goto out_free_mm;
1265 }
1266
1267 /* By now we should be all set, any drm_mmap request on the offset
1268 * below will get to our mmap & fault handler */
1269 obj_priv->mmap_offset = ((uint64_t) list->hash.key) << PAGE_SHIFT;
1270
1271 return 0;
1272
1273out_free_mm:
1274 drm_mm_put_block(list->file_offset_node);
1275out_free_list:
Eric Anholt9a298b22009-03-24 12:23:04 -07001276 kfree(list->map);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001277
1278 return ret;
1279}
1280
Chris Wilson901782b2009-07-10 08:18:50 +01001281/**
1282 * i915_gem_release_mmap - remove physical page mappings
1283 * @obj: obj in question
1284 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001285 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01001286 * relinquish ownership of the pages back to the system.
1287 *
1288 * It is vital that we remove the page mapping if we have mapped a tiled
1289 * object through the GTT and then lose the fence register due to
1290 * resource pressure. Similarly if the object has been moved out of the
1291 * aperture, than pages mapped into userspace must be revoked. Removing the
1292 * mapping will then trigger a page fault on the next user access, allowing
1293 * fixup by i915_gem_fault().
1294 */
Eric Anholtd05ca302009-07-10 13:02:26 -07001295void
Chris Wilson901782b2009-07-10 08:18:50 +01001296i915_gem_release_mmap(struct drm_gem_object *obj)
1297{
1298 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001299 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson901782b2009-07-10 08:18:50 +01001300
1301 if (dev->dev_mapping)
1302 unmap_mapping_range(dev->dev_mapping,
1303 obj_priv->mmap_offset, obj->size, 1);
1304}
1305
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001306static void
1307i915_gem_free_mmap_offset(struct drm_gem_object *obj)
1308{
1309 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001310 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001311 struct drm_gem_mm *mm = dev->mm_private;
1312 struct drm_map_list *list;
1313
1314 list = &obj->map_list;
1315 drm_ht_remove_item(&mm->offset_hash, &list->hash);
1316
1317 if (list->file_offset_node) {
1318 drm_mm_put_block(list->file_offset_node);
1319 list->file_offset_node = NULL;
1320 }
1321
1322 if (list->map) {
Eric Anholt9a298b22009-03-24 12:23:04 -07001323 kfree(list->map);
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001324 list->map = NULL;
1325 }
1326
1327 obj_priv->mmap_offset = 0;
1328}
1329
Jesse Barnesde151cf2008-11-12 10:03:55 -08001330/**
1331 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1332 * @obj: object to check
1333 *
1334 * Return the required GTT alignment for an object, taking into account
1335 * potential fence register mapping if needed.
1336 */
1337static uint32_t
1338i915_gem_get_gtt_alignment(struct drm_gem_object *obj)
1339{
1340 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001341 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001342 int start, i;
1343
1344 /*
1345 * Minimum alignment is 4k (GTT page size), but might be greater
1346 * if a fence register is needed for the object.
1347 */
1348 if (IS_I965G(dev) || obj_priv->tiling_mode == I915_TILING_NONE)
1349 return 4096;
1350
1351 /*
1352 * Previous chips need to be aligned to the size of the smallest
1353 * fence register that can contain the object.
1354 */
1355 if (IS_I9XX(dev))
1356 start = 1024*1024;
1357 else
1358 start = 512*1024;
1359
1360 for (i = start; i < obj->size; i <<= 1)
1361 ;
1362
1363 return i;
1364}
1365
1366/**
1367 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1368 * @dev: DRM device
1369 * @data: GTT mapping ioctl data
1370 * @file_priv: GEM object info
1371 *
1372 * Simply returns the fake offset to userspace so it can mmap it.
1373 * The mmap call will end up in drm_gem_mmap(), which will set things
1374 * up so we can get faults in the handler above.
1375 *
1376 * The fault handler will take care of binding the object into the GTT
1377 * (since it may have been evicted to make room for something), allocating
1378 * a fence register, and mapping the appropriate aperture address into
1379 * userspace.
1380 */
1381int
1382i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
1383 struct drm_file *file_priv)
1384{
1385 struct drm_i915_gem_mmap_gtt *args = data;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001386 struct drm_gem_object *obj;
1387 struct drm_i915_gem_object *obj_priv;
1388 int ret;
1389
1390 if (!(dev->driver->driver_features & DRIVER_GEM))
1391 return -ENODEV;
1392
1393 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1394 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001395 return -ENOENT;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001396
1397 mutex_lock(&dev->struct_mutex);
1398
Daniel Vetter23010e42010-03-08 13:35:02 +01001399 obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001400
Chris Wilsonab182822009-09-22 18:46:17 +01001401 if (obj_priv->madv != I915_MADV_WILLNEED) {
1402 DRM_ERROR("Attempting to mmap a purgeable buffer\n");
1403 drm_gem_object_unreference(obj);
1404 mutex_unlock(&dev->struct_mutex);
1405 return -EINVAL;
1406 }
1407
1408
Jesse Barnesde151cf2008-11-12 10:03:55 -08001409 if (!obj_priv->mmap_offset) {
1410 ret = i915_gem_create_mmap_offset(obj);
Chris Wilson13af1062009-02-11 14:26:31 +00001411 if (ret) {
1412 drm_gem_object_unreference(obj);
1413 mutex_unlock(&dev->struct_mutex);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001414 return ret;
Chris Wilson13af1062009-02-11 14:26:31 +00001415 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001416 }
1417
1418 args->offset = obj_priv->mmap_offset;
1419
Jesse Barnesde151cf2008-11-12 10:03:55 -08001420 /*
1421 * Pull it into the GTT so that we have a page list (makes the
1422 * initial fault faster and any subsequent flushing possible).
1423 */
1424 if (!obj_priv->agp_mem) {
Chris Wilsone67b8ce2009-09-14 16:50:26 +01001425 ret = i915_gem_object_bind_to_gtt(obj, 0);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001426 if (ret) {
1427 drm_gem_object_unreference(obj);
1428 mutex_unlock(&dev->struct_mutex);
1429 return ret;
1430 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001431 }
1432
1433 drm_gem_object_unreference(obj);
1434 mutex_unlock(&dev->struct_mutex);
1435
1436 return 0;
1437}
1438
Ben Gamari6911a9b2009-04-02 11:24:54 -07001439void
Eric Anholt856fa192009-03-19 14:10:50 -07001440i915_gem_object_put_pages(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07001441{
Daniel Vetter23010e42010-03-08 13:35:02 +01001442 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001443 int page_count = obj->size / PAGE_SIZE;
1444 int i;
1445
Eric Anholt856fa192009-03-19 14:10:50 -07001446 BUG_ON(obj_priv->pages_refcount == 0);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001447 BUG_ON(obj_priv->madv == __I915_MADV_PURGED);
Eric Anholt856fa192009-03-19 14:10:50 -07001448
1449 if (--obj_priv->pages_refcount != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07001450 return;
1451
Eric Anholt280b7132009-03-12 16:56:27 -07001452 if (obj_priv->tiling_mode != I915_TILING_NONE)
1453 i915_gem_object_save_bit_17_swizzle(obj);
1454
Chris Wilson3ef94da2009-09-14 16:50:29 +01001455 if (obj_priv->madv == I915_MADV_DONTNEED)
Chris Wilson13a05fd2009-09-20 23:03:19 +01001456 obj_priv->dirty = 0;
Chris Wilson3ef94da2009-09-14 16:50:29 +01001457
1458 for (i = 0; i < page_count; i++) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01001459 if (obj_priv->dirty)
1460 set_page_dirty(obj_priv->pages[i]);
1461
1462 if (obj_priv->madv == I915_MADV_WILLNEED)
Eric Anholt856fa192009-03-19 14:10:50 -07001463 mark_page_accessed(obj_priv->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001464
1465 page_cache_release(obj_priv->pages[i]);
1466 }
Eric Anholt673a3942008-07-30 12:06:12 -07001467 obj_priv->dirty = 0;
1468
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07001469 drm_free_large(obj_priv->pages);
Eric Anholt856fa192009-03-19 14:10:50 -07001470 obj_priv->pages = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001471}
1472
1473static void
Zou Nan hai852835f2010-05-21 09:08:56 +08001474i915_gem_object_move_to_active(struct drm_gem_object *obj, uint32_t seqno,
1475 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001476{
1477 struct drm_device *dev = obj->dev;
1478 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001479 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Zou Nan hai852835f2010-05-21 09:08:56 +08001480 BUG_ON(ring == NULL);
1481 obj_priv->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001482
1483 /* Add a reference if we're newly entering the active list. */
1484 if (!obj_priv->active) {
1485 drm_gem_object_reference(obj);
1486 obj_priv->active = 1;
1487 }
1488 /* Move from whatever list we were on to the tail of execution. */
Carl Worth5e118f42009-03-20 11:54:25 -07001489 spin_lock(&dev_priv->mm.active_list_lock);
Zou Nan hai852835f2010-05-21 09:08:56 +08001490 list_move_tail(&obj_priv->list, &ring->active_list);
Carl Worth5e118f42009-03-20 11:54:25 -07001491 spin_unlock(&dev_priv->mm.active_list_lock);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001492 obj_priv->last_rendering_seqno = seqno;
Eric Anholt673a3942008-07-30 12:06:12 -07001493}
1494
Eric Anholtce44b0e2008-11-06 16:00:31 -08001495static void
1496i915_gem_object_move_to_flushing(struct drm_gem_object *obj)
1497{
1498 struct drm_device *dev = obj->dev;
1499 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001500 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001501
1502 BUG_ON(!obj_priv->active);
1503 list_move_tail(&obj_priv->list, &dev_priv->mm.flushing_list);
1504 obj_priv->last_rendering_seqno = 0;
1505}
Eric Anholt673a3942008-07-30 12:06:12 -07001506
Chris Wilson963b4832009-09-20 23:03:54 +01001507/* Immediately discard the backing storage */
1508static void
1509i915_gem_object_truncate(struct drm_gem_object *obj)
1510{
Daniel Vetter23010e42010-03-08 13:35:02 +01001511 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001512 struct inode *inode;
Chris Wilson963b4832009-09-20 23:03:54 +01001513
Chris Wilsonae9fed62010-08-07 11:01:30 +01001514 /* Our goal here is to return as much of the memory as
1515 * is possible back to the system as we are called from OOM.
1516 * To do this we must instruct the shmfs to drop all of its
1517 * backing pages, *now*. Here we mirror the actions taken
1518 * when by shmem_delete_inode() to release the backing store.
1519 */
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001520 inode = obj->filp->f_path.dentry->d_inode;
Chris Wilsonae9fed62010-08-07 11:01:30 +01001521 truncate_inode_pages(inode->i_mapping, 0);
1522 if (inode->i_op->truncate_range)
1523 inode->i_op->truncate_range(inode, 0, (loff_t)-1);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001524
1525 obj_priv->madv = __I915_MADV_PURGED;
Chris Wilson963b4832009-09-20 23:03:54 +01001526}
1527
1528static inline int
1529i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj_priv)
1530{
1531 return obj_priv->madv == I915_MADV_DONTNEED;
1532}
1533
Eric Anholt673a3942008-07-30 12:06:12 -07001534static void
1535i915_gem_object_move_to_inactive(struct drm_gem_object *obj)
1536{
1537 struct drm_device *dev = obj->dev;
1538 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001539 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001540
1541 i915_verify_inactive(dev, __FILE__, __LINE__);
1542 if (obj_priv->pin_count != 0)
1543 list_del_init(&obj_priv->list);
1544 else
1545 list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
1546
Daniel Vetter99fcb762010-02-07 16:20:18 +01001547 BUG_ON(!list_empty(&obj_priv->gpu_write_list));
1548
Eric Anholtce44b0e2008-11-06 16:00:31 -08001549 obj_priv->last_rendering_seqno = 0;
Zou Nan hai852835f2010-05-21 09:08:56 +08001550 obj_priv->ring = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001551 if (obj_priv->active) {
1552 obj_priv->active = 0;
1553 drm_gem_object_unreference(obj);
1554 }
1555 i915_verify_inactive(dev, __FILE__, __LINE__);
1556}
1557
Daniel Vetter63560392010-02-19 11:51:59 +01001558static void
1559i915_gem_process_flushing_list(struct drm_device *dev,
Zou Nan hai852835f2010-05-21 09:08:56 +08001560 uint32_t flush_domains, uint32_t seqno,
1561 struct intel_ring_buffer *ring)
Daniel Vetter63560392010-02-19 11:51:59 +01001562{
1563 drm_i915_private_t *dev_priv = dev->dev_private;
1564 struct drm_i915_gem_object *obj_priv, *next;
1565
1566 list_for_each_entry_safe(obj_priv, next,
1567 &dev_priv->mm.gpu_write_list,
1568 gpu_write_list) {
Daniel Vettera8089e82010-04-09 19:05:09 +00001569 struct drm_gem_object *obj = &obj_priv->base;
Daniel Vetter63560392010-02-19 11:51:59 +01001570
1571 if ((obj->write_domain & flush_domains) ==
Zou Nan hai852835f2010-05-21 09:08:56 +08001572 obj->write_domain &&
1573 obj_priv->ring->ring_flag == ring->ring_flag) {
Daniel Vetter63560392010-02-19 11:51:59 +01001574 uint32_t old_write_domain = obj->write_domain;
1575
1576 obj->write_domain = 0;
1577 list_del_init(&obj_priv->gpu_write_list);
Zou Nan hai852835f2010-05-21 09:08:56 +08001578 i915_gem_object_move_to_active(obj, seqno, ring);
Daniel Vetter63560392010-02-19 11:51:59 +01001579
1580 /* update the fence lru list */
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001581 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
1582 struct drm_i915_fence_reg *reg =
1583 &dev_priv->fence_regs[obj_priv->fence_reg];
1584 list_move_tail(&reg->lru_list,
Daniel Vetter63560392010-02-19 11:51:59 +01001585 &dev_priv->mm.fence_list);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001586 }
Daniel Vetter63560392010-02-19 11:51:59 +01001587
1588 trace_i915_gem_object_change_domain(obj,
1589 obj->read_domains,
1590 old_write_domain);
1591 }
1592 }
1593}
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001594
Daniel Vetter5a5a0c62009-09-15 22:57:36 +02001595uint32_t
Eric Anholtb9624422009-06-03 07:27:35 +00001596i915_add_request(struct drm_device *dev, struct drm_file *file_priv,
Zou Nan hai852835f2010-05-21 09:08:56 +08001597 uint32_t flush_domains, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001598{
1599 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtb9624422009-06-03 07:27:35 +00001600 struct drm_i915_file_private *i915_file_priv = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001601 struct drm_i915_gem_request *request;
1602 uint32_t seqno;
1603 int was_empty;
Eric Anholt673a3942008-07-30 12:06:12 -07001604
Eric Anholtb9624422009-06-03 07:27:35 +00001605 if (file_priv != NULL)
1606 i915_file_priv = file_priv->driver_priv;
1607
Eric Anholt9a298b22009-03-24 12:23:04 -07001608 request = kzalloc(sizeof(*request), GFP_KERNEL);
Eric Anholt673a3942008-07-30 12:06:12 -07001609 if (request == NULL)
1610 return 0;
1611
Zou Nan hai852835f2010-05-21 09:08:56 +08001612 seqno = ring->add_request(dev, ring, file_priv, flush_domains);
Eric Anholt673a3942008-07-30 12:06:12 -07001613
1614 request->seqno = seqno;
Zou Nan hai852835f2010-05-21 09:08:56 +08001615 request->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001616 request->emitted_jiffies = jiffies;
Zou Nan hai852835f2010-05-21 09:08:56 +08001617 was_empty = list_empty(&ring->request_list);
1618 list_add_tail(&request->list, &ring->request_list);
1619
Eric Anholtb9624422009-06-03 07:27:35 +00001620 if (i915_file_priv) {
1621 list_add_tail(&request->client_list,
1622 &i915_file_priv->mm.request_list);
1623 } else {
1624 INIT_LIST_HEAD(&request->client_list);
1625 }
Eric Anholt673a3942008-07-30 12:06:12 -07001626
Eric Anholtce44b0e2008-11-06 16:00:31 -08001627 /* Associate any objects on the flushing list matching the write
1628 * domain we're flushing with our flush.
1629 */
Daniel Vetter63560392010-02-19 11:51:59 +01001630 if (flush_domains != 0)
Zou Nan hai852835f2010-05-21 09:08:56 +08001631 i915_gem_process_flushing_list(dev, flush_domains, seqno, ring);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001632
Ben Gamarif65d9422009-09-14 17:48:44 -04001633 if (!dev_priv->mm.suspended) {
1634 mod_timer(&dev_priv->hangcheck_timer, jiffies + DRM_I915_HANGCHECK_PERIOD);
1635 if (was_empty)
1636 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
1637 }
Eric Anholt673a3942008-07-30 12:06:12 -07001638 return seqno;
1639}
1640
1641/**
1642 * Command execution barrier
1643 *
1644 * Ensures that all commands in the ring are finished
1645 * before signalling the CPU
1646 */
Eric Anholt3043c602008-10-02 12:24:47 -07001647static uint32_t
Zou Nan hai852835f2010-05-21 09:08:56 +08001648i915_retire_commands(struct drm_device *dev, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001649{
Eric Anholt673a3942008-07-30 12:06:12 -07001650 uint32_t flush_domains = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001651
1652 /* The sampler always gets flushed on i965 (sigh) */
1653 if (IS_I965G(dev))
1654 flush_domains |= I915_GEM_DOMAIN_SAMPLER;
Zou Nan hai852835f2010-05-21 09:08:56 +08001655
1656 ring->flush(dev, ring,
1657 I915_GEM_DOMAIN_COMMAND, flush_domains);
Eric Anholt673a3942008-07-30 12:06:12 -07001658 return flush_domains;
1659}
1660
1661/**
1662 * Moves buffers associated only with the given active seqno from the active
1663 * to inactive list, potentially freeing them.
1664 */
1665static void
1666i915_gem_retire_request(struct drm_device *dev,
1667 struct drm_i915_gem_request *request)
1668{
1669 drm_i915_private_t *dev_priv = dev->dev_private;
1670
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001671 trace_i915_gem_request_retire(dev, request->seqno);
1672
Eric Anholt673a3942008-07-30 12:06:12 -07001673 /* Move any buffers on the active list that are no longer referenced
1674 * by the ringbuffer to the flushing/inactive lists as appropriate.
1675 */
Carl Worth5e118f42009-03-20 11:54:25 -07001676 spin_lock(&dev_priv->mm.active_list_lock);
Zou Nan hai852835f2010-05-21 09:08:56 +08001677 while (!list_empty(&request->ring->active_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001678 struct drm_gem_object *obj;
1679 struct drm_i915_gem_object *obj_priv;
1680
Zou Nan hai852835f2010-05-21 09:08:56 +08001681 obj_priv = list_first_entry(&request->ring->active_list,
Eric Anholt673a3942008-07-30 12:06:12 -07001682 struct drm_i915_gem_object,
1683 list);
Daniel Vettera8089e82010-04-09 19:05:09 +00001684 obj = &obj_priv->base;
Eric Anholt673a3942008-07-30 12:06:12 -07001685
1686 /* If the seqno being retired doesn't match the oldest in the
1687 * list, then the oldest in the list must still be newer than
1688 * this seqno.
1689 */
1690 if (obj_priv->last_rendering_seqno != request->seqno)
Carl Worth5e118f42009-03-20 11:54:25 -07001691 goto out;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001692
Eric Anholt673a3942008-07-30 12:06:12 -07001693#if WATCH_LRU
1694 DRM_INFO("%s: retire %d moves to inactive list %p\n",
1695 __func__, request->seqno, obj);
1696#endif
1697
Eric Anholtce44b0e2008-11-06 16:00:31 -08001698 if (obj->write_domain != 0)
1699 i915_gem_object_move_to_flushing(obj);
Shaohua Li68c84342009-04-08 10:58:23 +08001700 else {
1701 /* Take a reference on the object so it won't be
1702 * freed while the spinlock is held. The list
1703 * protection for this spinlock is safe when breaking
1704 * the lock like this since the next thing we do
1705 * is just get the head of the list again.
1706 */
1707 drm_gem_object_reference(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001708 i915_gem_object_move_to_inactive(obj);
Shaohua Li68c84342009-04-08 10:58:23 +08001709 spin_unlock(&dev_priv->mm.active_list_lock);
1710 drm_gem_object_unreference(obj);
1711 spin_lock(&dev_priv->mm.active_list_lock);
1712 }
Eric Anholt673a3942008-07-30 12:06:12 -07001713 }
Carl Worth5e118f42009-03-20 11:54:25 -07001714out:
1715 spin_unlock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001716}
1717
1718/**
1719 * Returns true if seq1 is later than seq2.
1720 */
Ben Gamari22be1722009-09-14 17:48:43 -04001721bool
Eric Anholt673a3942008-07-30 12:06:12 -07001722i915_seqno_passed(uint32_t seq1, uint32_t seq2)
1723{
1724 return (int32_t)(seq1 - seq2) >= 0;
1725}
1726
1727uint32_t
Zou Nan hai852835f2010-05-21 09:08:56 +08001728i915_get_gem_seqno(struct drm_device *dev,
Zou Nan haid1b851f2010-05-21 09:08:57 +08001729 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001730{
Zou Nan hai852835f2010-05-21 09:08:56 +08001731 return ring->get_gem_seqno(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07001732}
1733
1734/**
1735 * This function clears the request list as sequence numbers are passed.
1736 */
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001737static void
1738i915_gem_retire_requests_ring(struct drm_device *dev,
1739 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001740{
1741 drm_i915_private_t *dev_priv = dev->dev_private;
1742 uint32_t seqno;
1743
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001744 if (!ring->status_page.page_addr
Zou Nan hai852835f2010-05-21 09:08:56 +08001745 || list_empty(&ring->request_list))
Karsten Wiese6c0594a2009-02-23 15:07:57 +01001746 return;
1747
Zou Nan hai852835f2010-05-21 09:08:56 +08001748 seqno = i915_get_gem_seqno(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07001749
Zou Nan hai852835f2010-05-21 09:08:56 +08001750 while (!list_empty(&ring->request_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001751 struct drm_i915_gem_request *request;
1752 uint32_t retiring_seqno;
1753
Zou Nan hai852835f2010-05-21 09:08:56 +08001754 request = list_first_entry(&ring->request_list,
Eric Anholt673a3942008-07-30 12:06:12 -07001755 struct drm_i915_gem_request,
1756 list);
1757 retiring_seqno = request->seqno;
1758
1759 if (i915_seqno_passed(seqno, retiring_seqno) ||
Ben Gamariba1234d2009-09-14 17:48:47 -04001760 atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001761 i915_gem_retire_request(dev, request);
1762
1763 list_del(&request->list);
Eric Anholtb9624422009-06-03 07:27:35 +00001764 list_del(&request->client_list);
Eric Anholt9a298b22009-03-24 12:23:04 -07001765 kfree(request);
Eric Anholt673a3942008-07-30 12:06:12 -07001766 } else
1767 break;
1768 }
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001769
1770 if (unlikely (dev_priv->trace_irq_seqno &&
1771 i915_seqno_passed(dev_priv->trace_irq_seqno, seqno))) {
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001772
1773 ring->user_irq_put(dev, ring);
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001774 dev_priv->trace_irq_seqno = 0;
1775 }
Eric Anholt673a3942008-07-30 12:06:12 -07001776}
1777
1778void
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001779i915_gem_retire_requests(struct drm_device *dev)
1780{
1781 drm_i915_private_t *dev_priv = dev->dev_private;
1782
Chris Wilsonbe726152010-07-23 23:18:50 +01001783 if (!list_empty(&dev_priv->mm.deferred_free_list)) {
1784 struct drm_i915_gem_object *obj_priv, *tmp;
1785
1786 /* We must be careful that during unbind() we do not
1787 * accidentally infinitely recurse into retire requests.
1788 * Currently:
1789 * retire -> free -> unbind -> wait -> retire_ring
1790 */
1791 list_for_each_entry_safe(obj_priv, tmp,
1792 &dev_priv->mm.deferred_free_list,
1793 list)
1794 i915_gem_free_object_tail(&obj_priv->base);
1795 }
1796
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001797 i915_gem_retire_requests_ring(dev, &dev_priv->render_ring);
1798 if (HAS_BSD(dev))
1799 i915_gem_retire_requests_ring(dev, &dev_priv->bsd_ring);
1800}
1801
1802void
Eric Anholt673a3942008-07-30 12:06:12 -07001803i915_gem_retire_work_handler(struct work_struct *work)
1804{
1805 drm_i915_private_t *dev_priv;
1806 struct drm_device *dev;
1807
1808 dev_priv = container_of(work, drm_i915_private_t,
1809 mm.retire_work.work);
1810 dev = dev_priv->dev;
1811
1812 mutex_lock(&dev->struct_mutex);
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001813 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08001814
Keith Packard6dbe2772008-10-14 21:41:13 -07001815 if (!dev_priv->mm.suspended &&
Zou Nan haid1b851f2010-05-21 09:08:57 +08001816 (!list_empty(&dev_priv->render_ring.request_list) ||
1817 (HAS_BSD(dev) &&
1818 !list_empty(&dev_priv->bsd_ring.request_list))))
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001819 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
Eric Anholt673a3942008-07-30 12:06:12 -07001820 mutex_unlock(&dev->struct_mutex);
1821}
1822
Daniel Vetter5a5a0c62009-09-15 22:57:36 +02001823int
Zou Nan hai852835f2010-05-21 09:08:56 +08001824i915_do_wait_request(struct drm_device *dev, uint32_t seqno,
1825 int interruptible, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001826{
1827 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001828 u32 ier;
Eric Anholt673a3942008-07-30 12:06:12 -07001829 int ret = 0;
1830
1831 BUG_ON(seqno == 0);
1832
Ben Gamariba1234d2009-09-14 17:48:47 -04001833 if (atomic_read(&dev_priv->mm.wedged))
Ben Gamariffed1d02009-09-14 17:48:41 -04001834 return -EIO;
1835
Zou Nan hai852835f2010-05-21 09:08:56 +08001836 if (!i915_seqno_passed(ring->get_gem_seqno(dev, ring), seqno)) {
Eric Anholtbad720f2009-10-22 16:11:14 -07001837 if (HAS_PCH_SPLIT(dev))
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001838 ier = I915_READ(DEIER) | I915_READ(GTIER);
1839 else
1840 ier = I915_READ(IER);
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001841 if (!ier) {
1842 DRM_ERROR("something (likely vbetool) disabled "
1843 "interrupts, re-enabling\n");
1844 i915_driver_irq_preinstall(dev);
1845 i915_driver_irq_postinstall(dev);
1846 }
1847
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001848 trace_i915_gem_request_wait_begin(dev, seqno);
1849
Zou Nan hai852835f2010-05-21 09:08:56 +08001850 ring->waiting_gem_seqno = seqno;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001851 ring->user_irq_get(dev, ring);
Daniel Vetter48764bf2009-09-15 22:57:32 +02001852 if (interruptible)
Zou Nan hai852835f2010-05-21 09:08:56 +08001853 ret = wait_event_interruptible(ring->irq_queue,
1854 i915_seqno_passed(
1855 ring->get_gem_seqno(dev, ring), seqno)
1856 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02001857 else
Zou Nan hai852835f2010-05-21 09:08:56 +08001858 wait_event(ring->irq_queue,
1859 i915_seqno_passed(
1860 ring->get_gem_seqno(dev, ring), seqno)
1861 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02001862
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001863 ring->user_irq_put(dev, ring);
Zou Nan hai852835f2010-05-21 09:08:56 +08001864 ring->waiting_gem_seqno = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001865
1866 trace_i915_gem_request_wait_end(dev, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07001867 }
Ben Gamariba1234d2009-09-14 17:48:47 -04001868 if (atomic_read(&dev_priv->mm.wedged))
Eric Anholt673a3942008-07-30 12:06:12 -07001869 ret = -EIO;
1870
1871 if (ret && ret != -ERESTARTSYS)
1872 DRM_ERROR("%s returns %d (awaiting %d at %d)\n",
Zou Nan hai852835f2010-05-21 09:08:56 +08001873 __func__, ret, seqno, ring->get_gem_seqno(dev, ring));
Eric Anholt673a3942008-07-30 12:06:12 -07001874
1875 /* Directly dispatch request retiring. While we have the work queue
1876 * to handle this, the waiter on a request often wants an associated
1877 * buffer to have made it to the inactive list, and we would need
1878 * a separate wait queue to handle that.
1879 */
1880 if (ret == 0)
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001881 i915_gem_retire_requests_ring(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07001882
1883 return ret;
1884}
1885
Daniel Vetter48764bf2009-09-15 22:57:32 +02001886/**
1887 * Waits for a sequence number to be signaled, and cleans up the
1888 * request and object lists appropriately for that event.
1889 */
1890static int
Zou Nan hai852835f2010-05-21 09:08:56 +08001891i915_wait_request(struct drm_device *dev, uint32_t seqno,
1892 struct intel_ring_buffer *ring)
Daniel Vetter48764bf2009-09-15 22:57:32 +02001893{
Zou Nan hai852835f2010-05-21 09:08:56 +08001894 return i915_do_wait_request(dev, seqno, 1, ring);
Daniel Vetter48764bf2009-09-15 22:57:32 +02001895}
1896
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001897static void
1898i915_gem_flush(struct drm_device *dev,
1899 uint32_t invalidate_domains,
1900 uint32_t flush_domains)
1901{
1902 drm_i915_private_t *dev_priv = dev->dev_private;
1903 if (flush_domains & I915_GEM_DOMAIN_CPU)
1904 drm_agp_chipset_flush(dev);
1905 dev_priv->render_ring.flush(dev, &dev_priv->render_ring,
1906 invalidate_domains,
1907 flush_domains);
Zou Nan haid1b851f2010-05-21 09:08:57 +08001908
1909 if (HAS_BSD(dev))
1910 dev_priv->bsd_ring.flush(dev, &dev_priv->bsd_ring,
1911 invalidate_domains,
1912 flush_domains);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001913}
1914
Eric Anholt673a3942008-07-30 12:06:12 -07001915/**
1916 * Ensures that all rendering to the object has completed and the object is
1917 * safe to unbind from the GTT or access from the CPU.
1918 */
1919static int
1920i915_gem_object_wait_rendering(struct drm_gem_object *obj)
1921{
1922 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001923 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001924 int ret;
1925
Eric Anholte47c68e2008-11-14 13:35:19 -08001926 /* This function only exists to support waiting for existing rendering,
1927 * not for emitting required flushes.
Eric Anholt673a3942008-07-30 12:06:12 -07001928 */
Eric Anholte47c68e2008-11-14 13:35:19 -08001929 BUG_ON((obj->write_domain & I915_GEM_GPU_DOMAINS) != 0);
Eric Anholt673a3942008-07-30 12:06:12 -07001930
1931 /* If there is rendering queued on the buffer being evicted, wait for
1932 * it.
1933 */
1934 if (obj_priv->active) {
1935#if WATCH_BUF
1936 DRM_INFO("%s: object %p wait for seqno %08x\n",
1937 __func__, obj, obj_priv->last_rendering_seqno);
1938#endif
Zou Nan hai852835f2010-05-21 09:08:56 +08001939 ret = i915_wait_request(dev,
1940 obj_priv->last_rendering_seqno, obj_priv->ring);
Eric Anholt673a3942008-07-30 12:06:12 -07001941 if (ret != 0)
1942 return ret;
1943 }
1944
1945 return 0;
1946}
1947
1948/**
1949 * Unbinds an object from the GTT aperture.
1950 */
Jesse Barnes0f973f22009-01-26 17:10:45 -08001951int
Eric Anholt673a3942008-07-30 12:06:12 -07001952i915_gem_object_unbind(struct drm_gem_object *obj)
1953{
1954 struct drm_device *dev = obj->dev;
Daniel Vetter4a87b8c2010-02-19 11:51:57 +01001955 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001956 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001957 int ret = 0;
1958
1959#if WATCH_BUF
1960 DRM_INFO("%s:%d %p\n", __func__, __LINE__, obj);
1961 DRM_INFO("gtt_space %p\n", obj_priv->gtt_space);
1962#endif
1963 if (obj_priv->gtt_space == NULL)
1964 return 0;
1965
1966 if (obj_priv->pin_count != 0) {
1967 DRM_ERROR("Attempting to unbind pinned buffer\n");
1968 return -EINVAL;
1969 }
1970
Eric Anholt5323fd02009-09-09 11:50:45 -07001971 /* blow away mappings if mapped through GTT */
1972 i915_gem_release_mmap(obj);
1973
Eric Anholt673a3942008-07-30 12:06:12 -07001974 /* Move the object to the CPU domain to ensure that
1975 * any possible CPU writes while it's not in the GTT
1976 * are flushed when we go to remap it. This will
1977 * also ensure that all pending GPU writes are finished
1978 * before we unbind.
1979 */
Eric Anholte47c68e2008-11-14 13:35:19 -08001980 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Chris Wilson8dc17752010-07-23 23:18:51 +01001981 if (ret == -ERESTARTSYS)
Eric Anholt673a3942008-07-30 12:06:12 -07001982 return ret;
Chris Wilson8dc17752010-07-23 23:18:51 +01001983 /* Continue on if we fail due to EIO, the GPU is hung so we
1984 * should be safe and we need to cleanup or else we might
1985 * cause memory corruption through use-after-free.
1986 */
Eric Anholt673a3942008-07-30 12:06:12 -07001987
Daniel Vetter96b47b62009-12-15 17:50:00 +01001988 /* release the fence reg _after_ flushing */
1989 if (obj_priv->fence_reg != I915_FENCE_REG_NONE)
1990 i915_gem_clear_fence_reg(obj);
1991
Eric Anholt673a3942008-07-30 12:06:12 -07001992 if (obj_priv->agp_mem != NULL) {
1993 drm_unbind_agp(obj_priv->agp_mem);
1994 drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE);
1995 obj_priv->agp_mem = NULL;
1996 }
1997
Eric Anholt856fa192009-03-19 14:10:50 -07001998 i915_gem_object_put_pages(obj);
Chris Wilsona32808c2009-09-20 21:29:47 +01001999 BUG_ON(obj_priv->pages_refcount);
Eric Anholt673a3942008-07-30 12:06:12 -07002000
2001 if (obj_priv->gtt_space) {
2002 atomic_dec(&dev->gtt_count);
2003 atomic_sub(obj->size, &dev->gtt_memory);
2004
2005 drm_mm_put_block(obj_priv->gtt_space);
2006 obj_priv->gtt_space = NULL;
2007 }
2008
2009 /* Remove ourselves from the LRU list if present. */
Daniel Vetter4a87b8c2010-02-19 11:51:57 +01002010 spin_lock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07002011 if (!list_empty(&obj_priv->list))
2012 list_del_init(&obj_priv->list);
Daniel Vetter4a87b8c2010-02-19 11:51:57 +01002013 spin_unlock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07002014
Chris Wilson963b4832009-09-20 23:03:54 +01002015 if (i915_gem_object_is_purgeable(obj_priv))
2016 i915_gem_object_truncate(obj);
2017
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002018 trace_i915_gem_object_unbind(obj);
2019
Chris Wilson8dc17752010-07-23 23:18:51 +01002020 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002021}
2022
Chris Wilsonb47eb4a2010-08-07 11:01:23 +01002023int
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002024i915_gpu_idle(struct drm_device *dev)
2025{
2026 drm_i915_private_t *dev_priv = dev->dev_private;
2027 bool lists_empty;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002028 uint32_t seqno1, seqno2;
Zou Nan hai852835f2010-05-21 09:08:56 +08002029 int ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002030
2031 spin_lock(&dev_priv->mm.active_list_lock);
Zou Nan haid1b851f2010-05-21 09:08:57 +08002032 lists_empty = (list_empty(&dev_priv->mm.flushing_list) &&
2033 list_empty(&dev_priv->render_ring.active_list) &&
2034 (!HAS_BSD(dev) ||
2035 list_empty(&dev_priv->bsd_ring.active_list)));
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002036 spin_unlock(&dev_priv->mm.active_list_lock);
2037
2038 if (lists_empty)
2039 return 0;
2040
2041 /* Flush everything onto the inactive list. */
2042 i915_gem_flush(dev, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
Zou Nan haid1b851f2010-05-21 09:08:57 +08002043 seqno1 = i915_add_request(dev, NULL, I915_GEM_GPU_DOMAINS,
Zou Nan hai852835f2010-05-21 09:08:56 +08002044 &dev_priv->render_ring);
Zou Nan haid1b851f2010-05-21 09:08:57 +08002045 if (seqno1 == 0)
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002046 return -ENOMEM;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002047 ret = i915_wait_request(dev, seqno1, &dev_priv->render_ring);
2048
2049 if (HAS_BSD(dev)) {
2050 seqno2 = i915_add_request(dev, NULL, I915_GEM_GPU_DOMAINS,
2051 &dev_priv->bsd_ring);
2052 if (seqno2 == 0)
2053 return -ENOMEM;
2054
2055 ret = i915_wait_request(dev, seqno2, &dev_priv->bsd_ring);
2056 if (ret)
2057 return ret;
2058 }
2059
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002060
Zou Nan hai852835f2010-05-21 09:08:56 +08002061 return ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002062}
2063
Ben Gamari6911a9b2009-04-02 11:24:54 -07002064int
Chris Wilson4bdadb92010-01-27 13:36:32 +00002065i915_gem_object_get_pages(struct drm_gem_object *obj,
2066 gfp_t gfpmask)
Eric Anholt673a3942008-07-30 12:06:12 -07002067{
Daniel Vetter23010e42010-03-08 13:35:02 +01002068 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002069 int page_count, i;
2070 struct address_space *mapping;
2071 struct inode *inode;
2072 struct page *page;
Eric Anholt673a3942008-07-30 12:06:12 -07002073
Daniel Vetter778c3542010-05-13 11:49:44 +02002074 BUG_ON(obj_priv->pages_refcount
2075 == DRM_I915_GEM_OBJECT_MAX_PAGES_REFCOUNT);
2076
Eric Anholt856fa192009-03-19 14:10:50 -07002077 if (obj_priv->pages_refcount++ != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07002078 return 0;
2079
2080 /* Get the list of pages out of our struct file. They'll be pinned
2081 * at this point until we release them.
2082 */
2083 page_count = obj->size / PAGE_SIZE;
Eric Anholt856fa192009-03-19 14:10:50 -07002084 BUG_ON(obj_priv->pages != NULL);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07002085 obj_priv->pages = drm_calloc_large(page_count, sizeof(struct page *));
Eric Anholt856fa192009-03-19 14:10:50 -07002086 if (obj_priv->pages == NULL) {
Eric Anholt856fa192009-03-19 14:10:50 -07002087 obj_priv->pages_refcount--;
Eric Anholt673a3942008-07-30 12:06:12 -07002088 return -ENOMEM;
2089 }
2090
2091 inode = obj->filp->f_path.dentry->d_inode;
2092 mapping = inode->i_mapping;
2093 for (i = 0; i < page_count; i++) {
Chris Wilson4bdadb92010-01-27 13:36:32 +00002094 page = read_cache_page_gfp(mapping, i,
Linus Torvalds985b8232010-07-02 10:04:42 +10002095 GFP_HIGHUSER |
Chris Wilson4bdadb92010-01-27 13:36:32 +00002096 __GFP_COLD |
Linus Torvaldscd9f0402010-07-18 09:44:37 -07002097 __GFP_RECLAIMABLE |
Chris Wilson4bdadb92010-01-27 13:36:32 +00002098 gfpmask);
Chris Wilson1f2b1012010-03-12 19:52:55 +00002099 if (IS_ERR(page))
2100 goto err_pages;
2101
Eric Anholt856fa192009-03-19 14:10:50 -07002102 obj_priv->pages[i] = page;
Eric Anholt673a3942008-07-30 12:06:12 -07002103 }
Eric Anholt280b7132009-03-12 16:56:27 -07002104
2105 if (obj_priv->tiling_mode != I915_TILING_NONE)
2106 i915_gem_object_do_bit_17_swizzle(obj);
2107
Eric Anholt673a3942008-07-30 12:06:12 -07002108 return 0;
Chris Wilson1f2b1012010-03-12 19:52:55 +00002109
2110err_pages:
2111 while (i--)
2112 page_cache_release(obj_priv->pages[i]);
2113
2114 drm_free_large(obj_priv->pages);
2115 obj_priv->pages = NULL;
2116 obj_priv->pages_refcount--;
2117 return PTR_ERR(page);
Eric Anholt673a3942008-07-30 12:06:12 -07002118}
2119
Eric Anholt4e901fd2009-10-26 16:44:17 -07002120static void sandybridge_write_fence_reg(struct drm_i915_fence_reg *reg)
2121{
2122 struct drm_gem_object *obj = reg->obj;
2123 struct drm_device *dev = obj->dev;
2124 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002125 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt4e901fd2009-10-26 16:44:17 -07002126 int regnum = obj_priv->fence_reg;
2127 uint64_t val;
2128
2129 val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
2130 0xfffff000) << 32;
2131 val |= obj_priv->gtt_offset & 0xfffff000;
2132 val |= (uint64_t)((obj_priv->stride / 128) - 1) <<
2133 SANDYBRIDGE_FENCE_PITCH_SHIFT;
2134
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_SANDYBRIDGE_0 + (regnum * 8), val);
2140}
2141
Jesse Barnesde151cf2008-11-12 10:03:55 -08002142static void i965_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;
2149 uint64_t val;
2150
2151 val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
2152 0xfffff000) << 32;
2153 val |= obj_priv->gtt_offset & 0xfffff000;
2154 val |= ((obj_priv->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
2155 if (obj_priv->tiling_mode == I915_TILING_Y)
2156 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2157 val |= I965_FENCE_REG_VALID;
2158
2159 I915_WRITE64(FENCE_REG_965_0 + (regnum * 8), val);
2160}
2161
2162static void i915_write_fence_reg(struct drm_i915_fence_reg *reg)
2163{
2164 struct drm_gem_object *obj = reg->obj;
2165 struct drm_device *dev = obj->dev;
2166 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002167 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002168 int regnum = obj_priv->fence_reg;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002169 int tile_width;
Eric Anholtdc529a42009-03-10 22:34:49 -07002170 uint32_t fence_reg, val;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002171 uint32_t pitch_val;
2172
2173 if ((obj_priv->gtt_offset & ~I915_FENCE_START_MASK) ||
2174 (obj_priv->gtt_offset & (obj->size - 1))) {
Linus Torvaldsf06da262009-02-09 08:57:29 -08002175 WARN(1, "%s: object 0x%08x not 1M or size (0x%zx) aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002176 __func__, obj_priv->gtt_offset, obj->size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002177 return;
2178 }
2179
Jesse Barnes0f973f22009-01-26 17:10:45 -08002180 if (obj_priv->tiling_mode == I915_TILING_Y &&
2181 HAS_128_BYTE_Y_TILING(dev))
2182 tile_width = 128;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002183 else
Jesse Barnes0f973f22009-01-26 17:10:45 -08002184 tile_width = 512;
2185
2186 /* Note: pitch better be a power of two tile widths */
2187 pitch_val = obj_priv->stride / tile_width;
2188 pitch_val = ffs(pitch_val) - 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002189
Daniel Vetterc36a2a62010-04-17 15:12:03 +02002190 if (obj_priv->tiling_mode == I915_TILING_Y &&
2191 HAS_128_BYTE_Y_TILING(dev))
2192 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2193 else
2194 WARN_ON(pitch_val > I915_FENCE_MAX_PITCH_VAL);
2195
Jesse Barnesde151cf2008-11-12 10:03:55 -08002196 val = obj_priv->gtt_offset;
2197 if (obj_priv->tiling_mode == I915_TILING_Y)
2198 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2199 val |= I915_FENCE_SIZE_BITS(obj->size);
2200 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2201 val |= I830_FENCE_REG_VALID;
2202
Eric Anholtdc529a42009-03-10 22:34:49 -07002203 if (regnum < 8)
2204 fence_reg = FENCE_REG_830_0 + (regnum * 4);
2205 else
2206 fence_reg = FENCE_REG_945_8 + ((regnum - 8) * 4);
2207 I915_WRITE(fence_reg, val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002208}
2209
2210static void i830_write_fence_reg(struct drm_i915_fence_reg *reg)
2211{
2212 struct drm_gem_object *obj = reg->obj;
2213 struct drm_device *dev = obj->dev;
2214 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002215 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002216 int regnum = obj_priv->fence_reg;
2217 uint32_t val;
2218 uint32_t pitch_val;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002219 uint32_t fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002220
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002221 if ((obj_priv->gtt_offset & ~I830_FENCE_START_MASK) ||
Jesse Barnesde151cf2008-11-12 10:03:55 -08002222 (obj_priv->gtt_offset & (obj->size - 1))) {
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002223 WARN(1, "%s: object 0x%08x not 512K or size aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002224 __func__, obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002225 return;
2226 }
2227
Eric Anholte76a16d2009-05-26 17:44:56 -07002228 pitch_val = obj_priv->stride / 128;
2229 pitch_val = ffs(pitch_val) - 1;
2230 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2231
Jesse Barnesde151cf2008-11-12 10:03:55 -08002232 val = obj_priv->gtt_offset;
2233 if (obj_priv->tiling_mode == I915_TILING_Y)
2234 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002235 fence_size_bits = I830_FENCE_SIZE_BITS(obj->size);
2236 WARN_ON(fence_size_bits & ~0x00000f00);
2237 val |= fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002238 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2239 val |= I830_FENCE_REG_VALID;
2240
2241 I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002242}
2243
Daniel Vetterae3db242010-02-19 11:51:58 +01002244static int i915_find_fence_reg(struct drm_device *dev)
2245{
2246 struct drm_i915_fence_reg *reg = NULL;
2247 struct drm_i915_gem_object *obj_priv = NULL;
2248 struct drm_i915_private *dev_priv = dev->dev_private;
2249 struct drm_gem_object *obj = NULL;
2250 int i, avail, ret;
2251
2252 /* First try to find a free reg */
2253 avail = 0;
2254 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2255 reg = &dev_priv->fence_regs[i];
2256 if (!reg->obj)
2257 return i;
2258
Daniel Vetter23010e42010-03-08 13:35:02 +01002259 obj_priv = to_intel_bo(reg->obj);
Daniel Vetterae3db242010-02-19 11:51:58 +01002260 if (!obj_priv->pin_count)
2261 avail++;
2262 }
2263
2264 if (avail == 0)
2265 return -ENOSPC;
2266
2267 /* None available, try to steal one or wait for a user to finish */
2268 i = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002269 list_for_each_entry(reg, &dev_priv->mm.fence_list,
2270 lru_list) {
2271 obj = reg->obj;
2272 obj_priv = to_intel_bo(obj);
Daniel Vetterae3db242010-02-19 11:51:58 +01002273
2274 if (obj_priv->pin_count)
2275 continue;
2276
2277 /* found one! */
2278 i = obj_priv->fence_reg;
2279 break;
2280 }
2281
2282 BUG_ON(i == I915_FENCE_REG_NONE);
2283
2284 /* We only have a reference on obj from the active list. put_fence_reg
2285 * might drop that one, causing a use-after-free in it. So hold a
2286 * private reference to obj like the other callers of put_fence_reg
2287 * (set_tiling ioctl) do. */
2288 drm_gem_object_reference(obj);
2289 ret = i915_gem_object_put_fence_reg(obj);
2290 drm_gem_object_unreference(obj);
2291 if (ret != 0)
2292 return ret;
2293
2294 return i;
2295}
2296
Jesse Barnesde151cf2008-11-12 10:03:55 -08002297/**
2298 * i915_gem_object_get_fence_reg - set up a fence reg for an object
2299 * @obj: object to map through a fence reg
2300 *
2301 * When mapping objects through the GTT, userspace wants to be able to write
2302 * to them without having to worry about swizzling if the object is tiled.
2303 *
2304 * This function walks the fence regs looking for a free one for @obj,
2305 * stealing one if it can't find any.
2306 *
2307 * It then sets up the reg based on the object's properties: address, pitch
2308 * and tiling format.
2309 */
Chris Wilson8c4b8c32009-06-17 22:08:52 +01002310int
2311i915_gem_object_get_fence_reg(struct drm_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002312{
2313 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002314 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002315 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002316 struct drm_i915_fence_reg *reg = NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002317 int ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002318
Eric Anholta09ba7f2009-08-29 12:49:51 -07002319 /* Just update our place in the LRU if our fence is getting used. */
2320 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002321 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
2322 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002323 return 0;
2324 }
2325
Jesse Barnesde151cf2008-11-12 10:03:55 -08002326 switch (obj_priv->tiling_mode) {
2327 case I915_TILING_NONE:
2328 WARN(1, "allocating a fence for non-tiled object?\n");
2329 break;
2330 case I915_TILING_X:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002331 if (!obj_priv->stride)
2332 return -EINVAL;
2333 WARN((obj_priv->stride & (512 - 1)),
2334 "object 0x%08x is X tiled but has non-512B pitch\n",
2335 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002336 break;
2337 case I915_TILING_Y:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002338 if (!obj_priv->stride)
2339 return -EINVAL;
2340 WARN((obj_priv->stride & (128 - 1)),
2341 "object 0x%08x is Y tiled but has non-128B pitch\n",
2342 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002343 break;
2344 }
2345
Daniel Vetterae3db242010-02-19 11:51:58 +01002346 ret = i915_find_fence_reg(dev);
2347 if (ret < 0)
2348 return ret;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002349
Daniel Vetterae3db242010-02-19 11:51:58 +01002350 obj_priv->fence_reg = ret;
2351 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002352 list_add_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002353
Jesse Barnesde151cf2008-11-12 10:03:55 -08002354 reg->obj = obj;
2355
Chris Wilsone259bef2010-09-17 00:32:02 +01002356 switch (INTEL_INFO(dev)->gen) {
2357 case 6:
Eric Anholt4e901fd2009-10-26 16:44:17 -07002358 sandybridge_write_fence_reg(reg);
Chris Wilsone259bef2010-09-17 00:32:02 +01002359 break;
2360 case 5:
2361 case 4:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002362 i965_write_fence_reg(reg);
Chris Wilsone259bef2010-09-17 00:32:02 +01002363 break;
2364 case 3:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002365 i915_write_fence_reg(reg);
Chris Wilsone259bef2010-09-17 00:32:02 +01002366 break;
2367 case 2:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002368 i830_write_fence_reg(reg);
Chris Wilsone259bef2010-09-17 00:32:02 +01002369 break;
2370 }
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002371
Daniel Vetterae3db242010-02-19 11:51:58 +01002372 trace_i915_gem_object_get_fence(obj, obj_priv->fence_reg,
2373 obj_priv->tiling_mode);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002374
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002375 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002376}
2377
2378/**
2379 * i915_gem_clear_fence_reg - clear out fence register info
2380 * @obj: object to clear
2381 *
2382 * Zeroes out the fence register itself and clears out the associated
2383 * data structures in dev_priv and obj_priv.
2384 */
2385static void
2386i915_gem_clear_fence_reg(struct drm_gem_object *obj)
2387{
2388 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002389 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002390 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002391 struct drm_i915_fence_reg *reg =
2392 &dev_priv->fence_regs[obj_priv->fence_reg];
Chris Wilsone259bef2010-09-17 00:32:02 +01002393 uint32_t fence_reg;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002394
Chris Wilsone259bef2010-09-17 00:32:02 +01002395 switch (INTEL_INFO(dev)->gen) {
2396 case 6:
Eric Anholt4e901fd2009-10-26 16:44:17 -07002397 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 +
2398 (obj_priv->fence_reg * 8), 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002399 break;
2400 case 5:
2401 case 4:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002402 I915_WRITE64(FENCE_REG_965_0 + (obj_priv->fence_reg * 8), 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002403 break;
2404 case 3:
Chris Wilson9b74f732010-09-22 19:10:44 +01002405 if (obj_priv->fence_reg >= 8)
Chris Wilsone259bef2010-09-17 00:32:02 +01002406 fence_reg = FENCE_REG_945_8 + (obj_priv->fence_reg - 8) * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002407 else
Chris Wilsone259bef2010-09-17 00:32:02 +01002408 case 2:
2409 fence_reg = FENCE_REG_830_0 + obj_priv->fence_reg * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002410
2411 I915_WRITE(fence_reg, 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002412 break;
Eric Anholtdc529a42009-03-10 22:34:49 -07002413 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002414
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002415 reg->obj = NULL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002416 obj_priv->fence_reg = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002417 list_del_init(&reg->lru_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002418}
2419
Eric Anholt673a3942008-07-30 12:06:12 -07002420/**
Chris Wilson52dc7d32009-06-06 09:46:01 +01002421 * i915_gem_object_put_fence_reg - waits on outstanding fenced access
2422 * to the buffer to finish, and then resets the fence register.
2423 * @obj: tiled object holding a fence register.
2424 *
2425 * Zeroes out the fence register itself and clears out the associated
2426 * data structures in dev_priv and obj_priv.
2427 */
2428int
2429i915_gem_object_put_fence_reg(struct drm_gem_object *obj)
2430{
2431 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01002432 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson52dc7d32009-06-06 09:46:01 +01002433
2434 if (obj_priv->fence_reg == I915_FENCE_REG_NONE)
2435 return 0;
2436
Daniel Vetter10ae9bd2010-02-01 13:59:17 +01002437 /* If we've changed tiling, GTT-mappings of the object
2438 * need to re-fault to ensure that the correct fence register
2439 * setup is in place.
2440 */
2441 i915_gem_release_mmap(obj);
2442
Chris Wilson52dc7d32009-06-06 09:46:01 +01002443 /* On the i915, GPU access to tiled buffers is via a fence,
2444 * therefore we must wait for any outstanding access to complete
2445 * before clearing the fence.
2446 */
2447 if (!IS_I965G(dev)) {
2448 int ret;
2449
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002450 ret = i915_gem_object_flush_gpu_write_domain(obj);
2451 if (ret != 0)
2452 return ret;
2453
Chris Wilson52dc7d32009-06-06 09:46:01 +01002454 ret = i915_gem_object_wait_rendering(obj);
2455 if (ret != 0)
2456 return ret;
2457 }
2458
Daniel Vetter4a726612010-02-01 13:59:16 +01002459 i915_gem_object_flush_gtt_write_domain(obj);
Chris Wilson52dc7d32009-06-06 09:46:01 +01002460 i915_gem_clear_fence_reg (obj);
2461
2462 return 0;
2463}
2464
2465/**
Eric Anholt673a3942008-07-30 12:06:12 -07002466 * Finds free space in the GTT aperture and binds the object there.
2467 */
2468static int
2469i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment)
2470{
2471 struct drm_device *dev = obj->dev;
2472 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002473 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002474 struct drm_mm_node *free_space;
Chris Wilson4bdadb92010-01-27 13:36:32 +00002475 gfp_t gfpmask = __GFP_NORETRY | __GFP_NOWARN;
Chris Wilson07f73f62009-09-14 16:50:30 +01002476 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002477
Chris Wilsonbb6baf72009-09-22 14:24:13 +01002478 if (obj_priv->madv != I915_MADV_WILLNEED) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01002479 DRM_ERROR("Attempting to bind a purgeable object\n");
2480 return -EINVAL;
2481 }
2482
Eric Anholt673a3942008-07-30 12:06:12 -07002483 if (alignment == 0)
Jesse Barnes0f973f22009-01-26 17:10:45 -08002484 alignment = i915_gem_get_gtt_alignment(obj);
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002485 if (alignment & (i915_gem_get_gtt_alignment(obj) - 1)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002486 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2487 return -EINVAL;
2488 }
2489
Chris Wilson654fc602010-05-27 13:18:21 +01002490 /* If the object is bigger than the entire aperture, reject it early
2491 * before evicting everything in a vain attempt to find space.
2492 */
2493 if (obj->size > dev->gtt_total) {
2494 DRM_ERROR("Attempting to bind an object larger than the aperture\n");
2495 return -E2BIG;
2496 }
2497
Eric Anholt673a3942008-07-30 12:06:12 -07002498 search_free:
2499 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
2500 obj->size, alignment, 0);
2501 if (free_space != NULL) {
2502 obj_priv->gtt_space = drm_mm_get_block(free_space, obj->size,
2503 alignment);
Daniel Vetterdb3307a2010-07-02 15:02:12 +01002504 if (obj_priv->gtt_space != NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002505 obj_priv->gtt_offset = obj_priv->gtt_space->start;
Eric Anholt673a3942008-07-30 12:06:12 -07002506 }
2507 if (obj_priv->gtt_space == NULL) {
2508 /* If the gtt is empty and we're still having trouble
2509 * fitting our object in, we're out of memory.
2510 */
2511#if WATCH_LRU
2512 DRM_INFO("%s: GTT full, evicting something\n", __func__);
2513#endif
Daniel Vetter0108a3e2010-08-07 11:01:21 +01002514 ret = i915_gem_evict_something(dev, obj->size, alignment);
Chris Wilson97311292009-09-21 00:22:34 +01002515 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002516 return ret;
Chris Wilson97311292009-09-21 00:22:34 +01002517
Eric Anholt673a3942008-07-30 12:06:12 -07002518 goto search_free;
2519 }
2520
2521#if WATCH_BUF
Krzysztof Halasacfd43c02009-06-20 00:31:28 +02002522 DRM_INFO("Binding object of size %zd at 0x%08x\n",
Eric Anholt673a3942008-07-30 12:06:12 -07002523 obj->size, obj_priv->gtt_offset);
2524#endif
Chris Wilson4bdadb92010-01-27 13:36:32 +00002525 ret = i915_gem_object_get_pages(obj, gfpmask);
Eric Anholt673a3942008-07-30 12:06:12 -07002526 if (ret) {
2527 drm_mm_put_block(obj_priv->gtt_space);
2528 obj_priv->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002529
2530 if (ret == -ENOMEM) {
2531 /* first try to clear up some space from the GTT */
Daniel Vetter0108a3e2010-08-07 11:01:21 +01002532 ret = i915_gem_evict_something(dev, obj->size,
2533 alignment);
Chris Wilson07f73f62009-09-14 16:50:30 +01002534 if (ret) {
Chris Wilson07f73f62009-09-14 16:50:30 +01002535 /* now try to shrink everyone else */
Chris Wilson4bdadb92010-01-27 13:36:32 +00002536 if (gfpmask) {
2537 gfpmask = 0;
2538 goto search_free;
Chris Wilson07f73f62009-09-14 16:50:30 +01002539 }
2540
2541 return ret;
2542 }
2543
2544 goto search_free;
2545 }
2546
Eric Anholt673a3942008-07-30 12:06:12 -07002547 return ret;
2548 }
2549
Eric Anholt673a3942008-07-30 12:06:12 -07002550 /* Create an AGP memory structure pointing at our pages, and bind it
2551 * into the GTT.
2552 */
2553 obj_priv->agp_mem = drm_agp_bind_pages(dev,
Eric Anholt856fa192009-03-19 14:10:50 -07002554 obj_priv->pages,
Chris Wilson07f73f62009-09-14 16:50:30 +01002555 obj->size >> PAGE_SHIFT,
Keith Packardba1eb1d2008-10-14 19:55:10 -07002556 obj_priv->gtt_offset,
2557 obj_priv->agp_type);
Eric Anholt673a3942008-07-30 12:06:12 -07002558 if (obj_priv->agp_mem == NULL) {
Eric Anholt856fa192009-03-19 14:10:50 -07002559 i915_gem_object_put_pages(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002560 drm_mm_put_block(obj_priv->gtt_space);
2561 obj_priv->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002562
Daniel Vetter0108a3e2010-08-07 11:01:21 +01002563 ret = i915_gem_evict_something(dev, obj->size, alignment);
Chris Wilson97311292009-09-21 00:22:34 +01002564 if (ret)
Chris Wilson07f73f62009-09-14 16:50:30 +01002565 return ret;
Chris Wilson07f73f62009-09-14 16:50:30 +01002566
2567 goto search_free;
Eric Anholt673a3942008-07-30 12:06:12 -07002568 }
2569 atomic_inc(&dev->gtt_count);
2570 atomic_add(obj->size, &dev->gtt_memory);
2571
Chris Wilsonbf1a1092010-08-07 11:01:20 +01002572 /* keep track of bounds object by adding it to the inactive list */
2573 list_add_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
2574
Eric Anholt673a3942008-07-30 12:06:12 -07002575 /* Assert that the object is not currently in any GPU domain. As it
2576 * wasn't in the GTT, there shouldn't be any way it could have been in
2577 * a GPU cache
2578 */
Chris Wilson21d509e2009-06-06 09:46:02 +01002579 BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS);
2580 BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS);
Eric Anholt673a3942008-07-30 12:06:12 -07002581
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002582 trace_i915_gem_object_bind(obj, obj_priv->gtt_offset);
2583
Eric Anholt673a3942008-07-30 12:06:12 -07002584 return 0;
2585}
2586
2587void
2588i915_gem_clflush_object(struct drm_gem_object *obj)
2589{
Daniel Vetter23010e42010-03-08 13:35:02 +01002590 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002591
2592 /* If we don't have a page list set up, then we're not pinned
2593 * to GPU, and we can ignore the cache flush because it'll happen
2594 * again at bind time.
2595 */
Eric Anholt856fa192009-03-19 14:10:50 -07002596 if (obj_priv->pages == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002597 return;
2598
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002599 trace_i915_gem_object_clflush(obj);
Eric Anholtcfa16a02009-05-26 18:46:16 -07002600
Eric Anholt856fa192009-03-19 14:10:50 -07002601 drm_clflush_pages(obj_priv->pages, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002602}
2603
Eric Anholte47c68e2008-11-14 13:35:19 -08002604/** Flushes any GPU write domain for the object if it's dirty. */
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002605static int
Eric Anholte47c68e2008-11-14 13:35:19 -08002606i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj)
2607{
2608 struct drm_device *dev = obj->dev;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002609 uint32_t old_write_domain;
Zou Nan hai852835f2010-05-21 09:08:56 +08002610 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08002611
2612 if ((obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002613 return 0;
Eric Anholte47c68e2008-11-14 13:35:19 -08002614
2615 /* Queue the GPU write cache flushing we need. */
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002616 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002617 i915_gem_flush(dev, 0, obj->write_domain);
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002618 if (i915_add_request(dev, NULL, obj->write_domain, obj_priv->ring) == 0)
2619 return -ENOMEM;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002620
2621 trace_i915_gem_object_change_domain(obj,
2622 obj->read_domains,
2623 old_write_domain);
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002624 return 0;
Eric Anholte47c68e2008-11-14 13:35:19 -08002625}
2626
2627/** Flushes the GTT write domain for the object if it's dirty. */
2628static void
2629i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj)
2630{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002631 uint32_t old_write_domain;
2632
Eric Anholte47c68e2008-11-14 13:35:19 -08002633 if (obj->write_domain != I915_GEM_DOMAIN_GTT)
2634 return;
2635
2636 /* No actual flushing is required for the GTT write domain. Writes
2637 * to it immediately go to main memory as far as we know, so there's
2638 * no chipset flush. It also doesn't land in render cache.
2639 */
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002640 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002641 obj->write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002642
2643 trace_i915_gem_object_change_domain(obj,
2644 obj->read_domains,
2645 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002646}
2647
2648/** Flushes the CPU write domain for the object if it's dirty. */
2649static void
2650i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj)
2651{
2652 struct drm_device *dev = obj->dev;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002653 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002654
2655 if (obj->write_domain != I915_GEM_DOMAIN_CPU)
2656 return;
2657
2658 i915_gem_clflush_object(obj);
2659 drm_agp_chipset_flush(dev);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002660 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002661 obj->write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002662
2663 trace_i915_gem_object_change_domain(obj,
2664 obj->read_domains,
2665 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002666}
2667
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002668int
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002669i915_gem_object_flush_write_domain(struct drm_gem_object *obj)
2670{
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002671 int ret = 0;
2672
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002673 switch (obj->write_domain) {
2674 case I915_GEM_DOMAIN_GTT:
2675 i915_gem_object_flush_gtt_write_domain(obj);
2676 break;
2677 case I915_GEM_DOMAIN_CPU:
2678 i915_gem_object_flush_cpu_write_domain(obj);
2679 break;
2680 default:
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002681 ret = i915_gem_object_flush_gpu_write_domain(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002682 break;
2683 }
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002684
2685 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002686}
2687
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002688/**
2689 * Moves a single object to the GTT read, and possibly write domain.
2690 *
2691 * This function returns when the move is complete, including waiting on
2692 * flushes to occur.
2693 */
Jesse Barnes79e53942008-11-07 14:24:08 -08002694int
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002695i915_gem_object_set_to_gtt_domain(struct drm_gem_object *obj, int write)
2696{
Daniel Vetter23010e42010-03-08 13:35:02 +01002697 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002698 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002699 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002700
Eric Anholt02354392008-11-26 13:58:13 -08002701 /* Not valid to be called on unbound objects. */
2702 if (obj_priv->gtt_space == NULL)
2703 return -EINVAL;
2704
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002705 ret = i915_gem_object_flush_gpu_write_domain(obj);
2706 if (ret != 0)
2707 return ret;
2708
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002709 /* Wait on any GPU rendering and flushing to occur. */
Eric Anholte47c68e2008-11-14 13:35:19 -08002710 ret = i915_gem_object_wait_rendering(obj);
2711 if (ret != 0)
2712 return ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002713
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002714 old_write_domain = obj->write_domain;
2715 old_read_domains = obj->read_domains;
2716
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002717 /* If we're writing through the GTT domain, then CPU and GPU caches
2718 * will need to be invalidated at next use.
2719 */
2720 if (write)
Eric Anholte47c68e2008-11-14 13:35:19 -08002721 obj->read_domains &= I915_GEM_DOMAIN_GTT;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002722
Eric Anholte47c68e2008-11-14 13:35:19 -08002723 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002724
2725 /* It should now be out of any other write domains, and we can update
2726 * the domain values for our changes.
2727 */
2728 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2729 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002730 if (write) {
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002731 obj->write_domain = I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002732 obj_priv->dirty = 1;
2733 }
2734
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002735 trace_i915_gem_object_change_domain(obj,
2736 old_read_domains,
2737 old_write_domain);
2738
Eric Anholte47c68e2008-11-14 13:35:19 -08002739 return 0;
2740}
2741
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002742/*
2743 * Prepare buffer for display plane. Use uninterruptible for possible flush
2744 * wait, as in modesetting process we're not supposed to be interrupted.
2745 */
2746int
2747i915_gem_object_set_to_display_plane(struct drm_gem_object *obj)
2748{
2749 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01002750 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002751 uint32_t old_write_domain, old_read_domains;
2752 int ret;
2753
2754 /* Not valid to be called on unbound objects. */
2755 if (obj_priv->gtt_space == NULL)
2756 return -EINVAL;
2757
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002758 ret = i915_gem_object_flush_gpu_write_domain(obj);
2759 if (ret)
2760 return ret;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002761
2762 /* Wait on any GPU rendering and flushing to occur. */
2763 if (obj_priv->active) {
2764#if WATCH_BUF
2765 DRM_INFO("%s: object %p wait for seqno %08x\n",
2766 __func__, obj, obj_priv->last_rendering_seqno);
2767#endif
Zou Nan hai852835f2010-05-21 09:08:56 +08002768 ret = i915_do_wait_request(dev,
2769 obj_priv->last_rendering_seqno,
2770 0,
2771 obj_priv->ring);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002772 if (ret != 0)
2773 return ret;
2774 }
2775
Chris Wilsonb118c1e2010-05-27 13:18:14 +01002776 i915_gem_object_flush_cpu_write_domain(obj);
2777
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002778 old_write_domain = obj->write_domain;
2779 old_read_domains = obj->read_domains;
2780
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002781 /* It should now be out of any other write domains, and we can update
2782 * the domain values for our changes.
2783 */
2784 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
Chris Wilsonb118c1e2010-05-27 13:18:14 +01002785 obj->read_domains = I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002786 obj->write_domain = I915_GEM_DOMAIN_GTT;
2787 obj_priv->dirty = 1;
2788
2789 trace_i915_gem_object_change_domain(obj,
2790 old_read_domains,
2791 old_write_domain);
2792
2793 return 0;
2794}
2795
Eric Anholte47c68e2008-11-14 13:35:19 -08002796/**
2797 * Moves a single object to the CPU read, and possibly write domain.
2798 *
2799 * This function returns when the move is complete, including waiting on
2800 * flushes to occur.
2801 */
2802static int
2803i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj, int write)
2804{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002805 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002806 int ret;
2807
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002808 ret = i915_gem_object_flush_gpu_write_domain(obj);
2809 if (ret)
2810 return ret;
2811
Eric Anholte47c68e2008-11-14 13:35:19 -08002812 /* Wait on any GPU rendering and flushing to occur. */
2813 ret = i915_gem_object_wait_rendering(obj);
2814 if (ret != 0)
2815 return ret;
2816
2817 i915_gem_object_flush_gtt_write_domain(obj);
2818
2819 /* If we have a partially-valid cache of the object in the CPU,
2820 * finish invalidating it and free the per-page flags.
2821 */
2822 i915_gem_object_set_to_full_cpu_read_domain(obj);
2823
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002824 old_write_domain = obj->write_domain;
2825 old_read_domains = obj->read_domains;
2826
Eric Anholte47c68e2008-11-14 13:35:19 -08002827 /* Flush the CPU cache if it's still invalid. */
2828 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
2829 i915_gem_clflush_object(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08002830
2831 obj->read_domains |= I915_GEM_DOMAIN_CPU;
2832 }
2833
2834 /* It should now be out of any other write domains, and we can update
2835 * the domain values for our changes.
2836 */
2837 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
2838
2839 /* If we're writing through the CPU, then the GPU read domains will
2840 * need to be invalidated at next use.
2841 */
2842 if (write) {
2843 obj->read_domains &= I915_GEM_DOMAIN_CPU;
2844 obj->write_domain = I915_GEM_DOMAIN_CPU;
2845 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002846
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002847 trace_i915_gem_object_change_domain(obj,
2848 old_read_domains,
2849 old_write_domain);
2850
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002851 return 0;
2852}
2853
Eric Anholt673a3942008-07-30 12:06:12 -07002854/*
2855 * Set the next domain for the specified object. This
2856 * may not actually perform the necessary flushing/invaliding though,
2857 * as that may want to be batched with other set_domain operations
2858 *
2859 * This is (we hope) the only really tricky part of gem. The goal
2860 * is fairly simple -- track which caches hold bits of the object
2861 * and make sure they remain coherent. A few concrete examples may
2862 * help to explain how it works. For shorthand, we use the notation
2863 * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
2864 * a pair of read and write domain masks.
2865 *
2866 * Case 1: the batch buffer
2867 *
2868 * 1. Allocated
2869 * 2. Written by CPU
2870 * 3. Mapped to GTT
2871 * 4. Read by GPU
2872 * 5. Unmapped from GTT
2873 * 6. Freed
2874 *
2875 * Let's take these a step at a time
2876 *
2877 * 1. Allocated
2878 * Pages allocated from the kernel may still have
2879 * cache contents, so we set them to (CPU, CPU) always.
2880 * 2. Written by CPU (using pwrite)
2881 * The pwrite function calls set_domain (CPU, CPU) and
2882 * this function does nothing (as nothing changes)
2883 * 3. Mapped by GTT
2884 * This function asserts that the object is not
2885 * currently in any GPU-based read or write domains
2886 * 4. Read by GPU
2887 * i915_gem_execbuffer calls set_domain (COMMAND, 0).
2888 * As write_domain is zero, this function adds in the
2889 * current read domains (CPU+COMMAND, 0).
2890 * flush_domains is set to CPU.
2891 * invalidate_domains is set to COMMAND
2892 * clflush is run to get data out of the CPU caches
2893 * then i915_dev_set_domain calls i915_gem_flush to
2894 * emit an MI_FLUSH and drm_agp_chipset_flush
2895 * 5. Unmapped from GTT
2896 * i915_gem_object_unbind calls set_domain (CPU, CPU)
2897 * flush_domains and invalidate_domains end up both zero
2898 * so no flushing/invalidating happens
2899 * 6. Freed
2900 * yay, done
2901 *
2902 * Case 2: The shared render buffer
2903 *
2904 * 1. Allocated
2905 * 2. Mapped to GTT
2906 * 3. Read/written by GPU
2907 * 4. set_domain to (CPU,CPU)
2908 * 5. Read/written by CPU
2909 * 6. Read/written by GPU
2910 *
2911 * 1. Allocated
2912 * Same as last example, (CPU, CPU)
2913 * 2. Mapped to GTT
2914 * Nothing changes (assertions find that it is not in the GPU)
2915 * 3. Read/written by GPU
2916 * execbuffer calls set_domain (RENDER, RENDER)
2917 * flush_domains gets CPU
2918 * invalidate_domains gets GPU
2919 * clflush (obj)
2920 * MI_FLUSH and drm_agp_chipset_flush
2921 * 4. set_domain (CPU, CPU)
2922 * flush_domains gets GPU
2923 * invalidate_domains gets CPU
2924 * wait_rendering (obj) to make sure all drawing is complete.
2925 * This will include an MI_FLUSH to get the data from GPU
2926 * to memory
2927 * clflush (obj) to invalidate the CPU cache
2928 * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
2929 * 5. Read/written by CPU
2930 * cache lines are loaded and dirtied
2931 * 6. Read written by GPU
2932 * Same as last GPU access
2933 *
2934 * Case 3: The constant buffer
2935 *
2936 * 1. Allocated
2937 * 2. Written by CPU
2938 * 3. Read by GPU
2939 * 4. Updated (written) by CPU again
2940 * 5. Read by GPU
2941 *
2942 * 1. Allocated
2943 * (CPU, CPU)
2944 * 2. Written by CPU
2945 * (CPU, CPU)
2946 * 3. Read by GPU
2947 * (CPU+RENDER, 0)
2948 * flush_domains = CPU
2949 * invalidate_domains = RENDER
2950 * clflush (obj)
2951 * MI_FLUSH
2952 * drm_agp_chipset_flush
2953 * 4. Updated (written) by CPU again
2954 * (CPU, CPU)
2955 * flush_domains = 0 (no previous write domain)
2956 * invalidate_domains = 0 (no new read domains)
2957 * 5. Read by GPU
2958 * (CPU+RENDER, 0)
2959 * flush_domains = CPU
2960 * invalidate_domains = RENDER
2961 * clflush (obj)
2962 * MI_FLUSH
2963 * drm_agp_chipset_flush
2964 */
Keith Packardc0d90822008-11-20 23:11:08 -08002965static void
Eric Anholt8b0e3782009-02-19 14:40:50 -08002966i915_gem_object_set_to_gpu_domain(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002967{
2968 struct drm_device *dev = obj->dev;
Chris Wilson88f356b2010-08-04 13:55:32 +01002969 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002970 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002971 uint32_t invalidate_domains = 0;
2972 uint32_t flush_domains = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002973 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002974
Eric Anholt8b0e3782009-02-19 14:40:50 -08002975 BUG_ON(obj->pending_read_domains & I915_GEM_DOMAIN_CPU);
2976 BUG_ON(obj->pending_write_domain == I915_GEM_DOMAIN_CPU);
Eric Anholt673a3942008-07-30 12:06:12 -07002977
Jesse Barnes652c3932009-08-17 13:31:43 -07002978 intel_mark_busy(dev, obj);
2979
Eric Anholt673a3942008-07-30 12:06:12 -07002980#if WATCH_BUF
2981 DRM_INFO("%s: object %p read %08x -> %08x write %08x -> %08x\n",
2982 __func__, obj,
Eric Anholt8b0e3782009-02-19 14:40:50 -08002983 obj->read_domains, obj->pending_read_domains,
2984 obj->write_domain, obj->pending_write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07002985#endif
2986 /*
2987 * If the object isn't moving to a new write domain,
2988 * let the object stay in multiple read domains
2989 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08002990 if (obj->pending_write_domain == 0)
2991 obj->pending_read_domains |= obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07002992 else
2993 obj_priv->dirty = 1;
2994
2995 /*
2996 * Flush the current write domain if
2997 * the new read domains don't match. Invalidate
2998 * any read domains which differ from the old
2999 * write domain
3000 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003001 if (obj->write_domain &&
3002 obj->write_domain != obj->pending_read_domains) {
Eric Anholt673a3942008-07-30 12:06:12 -07003003 flush_domains |= obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08003004 invalidate_domains |=
3005 obj->pending_read_domains & ~obj->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07003006 }
3007 /*
3008 * Invalidate any read caches which may have
3009 * stale data. That is, any new read domains.
3010 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003011 invalidate_domains |= obj->pending_read_domains & ~obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07003012 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU) {
3013#if WATCH_BUF
3014 DRM_INFO("%s: CPU domain flush %08x invalidate %08x\n",
3015 __func__, flush_domains, invalidate_domains);
3016#endif
Eric Anholt673a3942008-07-30 12:06:12 -07003017 i915_gem_clflush_object(obj);
3018 }
3019
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003020 old_read_domains = obj->read_domains;
3021
Eric Anholtefbeed92009-02-19 14:54:51 -08003022 /* The actual obj->write_domain will be updated with
3023 * pending_write_domain after we emit the accumulated flush for all
3024 * of our domain changes in execbuffers (which clears objects'
3025 * write_domains). So if we have a current write domain that we
3026 * aren't changing, set pending_write_domain to that.
3027 */
3028 if (flush_domains == 0 && obj->pending_write_domain == 0)
3029 obj->pending_write_domain = obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08003030 obj->read_domains = obj->pending_read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07003031
Chris Wilson88f356b2010-08-04 13:55:32 +01003032 if (flush_domains & I915_GEM_GPU_DOMAINS) {
3033 if (obj_priv->ring == &dev_priv->render_ring)
3034 dev_priv->flush_rings |= FLUSH_RENDER_RING;
3035 else if (obj_priv->ring == &dev_priv->bsd_ring)
3036 dev_priv->flush_rings |= FLUSH_BSD_RING;
3037 }
3038
Eric Anholt673a3942008-07-30 12:06:12 -07003039 dev->invalidate_domains |= invalidate_domains;
3040 dev->flush_domains |= flush_domains;
3041#if WATCH_BUF
3042 DRM_INFO("%s: read %08x write %08x invalidate %08x flush %08x\n",
3043 __func__,
3044 obj->read_domains, obj->write_domain,
3045 dev->invalidate_domains, dev->flush_domains);
3046#endif
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003047
3048 trace_i915_gem_object_change_domain(obj,
3049 old_read_domains,
3050 obj->write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07003051}
3052
3053/**
Eric Anholte47c68e2008-11-14 13:35:19 -08003054 * Moves the object from a partially CPU read to a full one.
Eric Anholt673a3942008-07-30 12:06:12 -07003055 *
Eric Anholte47c68e2008-11-14 13:35:19 -08003056 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
3057 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
3058 */
3059static void
3060i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj)
3061{
Daniel Vetter23010e42010-03-08 13:35:02 +01003062 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003063
3064 if (!obj_priv->page_cpu_valid)
3065 return;
3066
3067 /* If we're partially in the CPU read domain, finish moving it in.
3068 */
3069 if (obj->read_domains & I915_GEM_DOMAIN_CPU) {
3070 int i;
3071
3072 for (i = 0; i <= (obj->size - 1) / PAGE_SIZE; i++) {
3073 if (obj_priv->page_cpu_valid[i])
3074 continue;
Eric Anholt856fa192009-03-19 14:10:50 -07003075 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholte47c68e2008-11-14 13:35:19 -08003076 }
Eric Anholte47c68e2008-11-14 13:35:19 -08003077 }
3078
3079 /* Free the page_cpu_valid mappings which are now stale, whether
3080 * or not we've got I915_GEM_DOMAIN_CPU.
3081 */
Eric Anholt9a298b22009-03-24 12:23:04 -07003082 kfree(obj_priv->page_cpu_valid);
Eric Anholte47c68e2008-11-14 13:35:19 -08003083 obj_priv->page_cpu_valid = NULL;
3084}
3085
3086/**
3087 * Set the CPU read domain on a range of the object.
3088 *
3089 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
3090 * not entirely valid. The page_cpu_valid member of the object flags which
3091 * pages have been flushed, and will be respected by
3092 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
3093 * of the whole object.
3094 *
3095 * This function returns when the move is complete, including waiting on
3096 * flushes to occur.
Eric Anholt673a3942008-07-30 12:06:12 -07003097 */
3098static int
Eric Anholte47c68e2008-11-14 13:35:19 -08003099i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
3100 uint64_t offset, uint64_t size)
Eric Anholt673a3942008-07-30 12:06:12 -07003101{
Daniel Vetter23010e42010-03-08 13:35:02 +01003102 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003103 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003104 int i, ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003105
Eric Anholte47c68e2008-11-14 13:35:19 -08003106 if (offset == 0 && size == obj->size)
3107 return i915_gem_object_set_to_cpu_domain(obj, 0);
3108
Chris Wilson2dafb1e2010-06-07 14:03:05 +01003109 ret = i915_gem_object_flush_gpu_write_domain(obj);
3110 if (ret)
3111 return ret;
3112
Eric Anholte47c68e2008-11-14 13:35:19 -08003113 /* Wait on any GPU rendering and flushing to occur. */
3114 ret = i915_gem_object_wait_rendering(obj);
3115 if (ret != 0)
3116 return ret;
3117 i915_gem_object_flush_gtt_write_domain(obj);
3118
3119 /* If we're already fully in the CPU read domain, we're done. */
3120 if (obj_priv->page_cpu_valid == NULL &&
3121 (obj->read_domains & I915_GEM_DOMAIN_CPU) != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07003122 return 0;
3123
Eric Anholte47c68e2008-11-14 13:35:19 -08003124 /* Otherwise, create/clear the per-page CPU read domain flag if we're
3125 * newly adding I915_GEM_DOMAIN_CPU
3126 */
Eric Anholt673a3942008-07-30 12:06:12 -07003127 if (obj_priv->page_cpu_valid == NULL) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003128 obj_priv->page_cpu_valid = kzalloc(obj->size / PAGE_SIZE,
3129 GFP_KERNEL);
Eric Anholte47c68e2008-11-14 13:35:19 -08003130 if (obj_priv->page_cpu_valid == NULL)
3131 return -ENOMEM;
3132 } else if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0)
3133 memset(obj_priv->page_cpu_valid, 0, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07003134
3135 /* Flush the cache on any pages that are still invalid from the CPU's
3136 * perspective.
3137 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003138 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
3139 i++) {
Eric Anholt673a3942008-07-30 12:06:12 -07003140 if (obj_priv->page_cpu_valid[i])
3141 continue;
3142
Eric Anholt856fa192009-03-19 14:10:50 -07003143 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07003144
3145 obj_priv->page_cpu_valid[i] = 1;
3146 }
3147
Eric Anholte47c68e2008-11-14 13:35:19 -08003148 /* It should now be out of any other write domains, and we can update
3149 * the domain values for our changes.
3150 */
3151 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
3152
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003153 old_read_domains = obj->read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003154 obj->read_domains |= I915_GEM_DOMAIN_CPU;
3155
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003156 trace_i915_gem_object_change_domain(obj,
3157 old_read_domains,
3158 obj->write_domain);
3159
Eric Anholt673a3942008-07-30 12:06:12 -07003160 return 0;
3161}
3162
3163/**
Eric Anholt673a3942008-07-30 12:06:12 -07003164 * Pin an object to the GTT and evaluate the relocations landing in it.
3165 */
3166static int
3167i915_gem_object_pin_and_relocate(struct drm_gem_object *obj,
3168 struct drm_file *file_priv,
Jesse Barnes76446ca2009-12-17 22:05:42 -05003169 struct drm_i915_gem_exec_object2 *entry,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003170 struct drm_i915_gem_relocation_entry *relocs)
Eric Anholt673a3942008-07-30 12:06:12 -07003171{
3172 struct drm_device *dev = obj->dev;
Keith Packard0839ccb2008-10-30 19:38:48 -07003173 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01003174 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003175 int i, ret;
Keith Packard0839ccb2008-10-30 19:38:48 -07003176 void __iomem *reloc_page;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003177 bool need_fence;
3178
3179 need_fence = entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
3180 obj_priv->tiling_mode != I915_TILING_NONE;
3181
3182 /* Check fence reg constraints and rebind if necessary */
Chris Wilson808b24d2010-05-27 13:18:15 +01003183 if (need_fence &&
3184 !i915_gem_object_fence_offset_ok(obj,
3185 obj_priv->tiling_mode)) {
3186 ret = i915_gem_object_unbind(obj);
3187 if (ret)
3188 return ret;
3189 }
Eric Anholt673a3942008-07-30 12:06:12 -07003190
3191 /* Choose the GTT offset for our buffer and put it there. */
3192 ret = i915_gem_object_pin(obj, (uint32_t) entry->alignment);
3193 if (ret)
3194 return ret;
3195
Jesse Barnes76446ca2009-12-17 22:05:42 -05003196 /*
3197 * Pre-965 chips need a fence register set up in order to
3198 * properly handle blits to/from tiled surfaces.
3199 */
3200 if (need_fence) {
3201 ret = i915_gem_object_get_fence_reg(obj);
3202 if (ret != 0) {
Jesse Barnes76446ca2009-12-17 22:05:42 -05003203 i915_gem_object_unpin(obj);
3204 return ret;
3205 }
3206 }
3207
Eric Anholt673a3942008-07-30 12:06:12 -07003208 entry->offset = obj_priv->gtt_offset;
3209
Eric Anholt673a3942008-07-30 12:06:12 -07003210 /* Apply the relocations, using the GTT aperture to avoid cache
3211 * flushing requirements.
3212 */
3213 for (i = 0; i < entry->relocation_count; i++) {
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003214 struct drm_i915_gem_relocation_entry *reloc= &relocs[i];
Eric Anholt673a3942008-07-30 12:06:12 -07003215 struct drm_gem_object *target_obj;
3216 struct drm_i915_gem_object *target_obj_priv;
Eric Anholt3043c602008-10-02 12:24:47 -07003217 uint32_t reloc_val, reloc_offset;
3218 uint32_t __iomem *reloc_entry;
Eric Anholt673a3942008-07-30 12:06:12 -07003219
Eric Anholt673a3942008-07-30 12:06:12 -07003220 target_obj = drm_gem_object_lookup(obj->dev, file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003221 reloc->target_handle);
Eric Anholt673a3942008-07-30 12:06:12 -07003222 if (target_obj == NULL) {
3223 i915_gem_object_unpin(obj);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01003224 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07003225 }
Daniel Vetter23010e42010-03-08 13:35:02 +01003226 target_obj_priv = to_intel_bo(target_obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003227
Chris Wilson8542a0b2009-09-09 21:15:15 +01003228#if WATCH_RELOC
3229 DRM_INFO("%s: obj %p offset %08x target %d "
3230 "read %08x write %08x gtt %08x "
3231 "presumed %08x delta %08x\n",
3232 __func__,
3233 obj,
3234 (int) reloc->offset,
3235 (int) reloc->target_handle,
3236 (int) reloc->read_domains,
3237 (int) reloc->write_domain,
3238 (int) target_obj_priv->gtt_offset,
3239 (int) reloc->presumed_offset,
3240 reloc->delta);
3241#endif
3242
Eric Anholt673a3942008-07-30 12:06:12 -07003243 /* The target buffer should have appeared before us in the
3244 * exec_object list, so it should have a GTT space bound by now.
3245 */
3246 if (target_obj_priv->gtt_space == NULL) {
3247 DRM_ERROR("No GTT space found for object %d\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003248 reloc->target_handle);
Eric Anholt673a3942008-07-30 12:06:12 -07003249 drm_gem_object_unreference(target_obj);
3250 i915_gem_object_unpin(obj);
3251 return -EINVAL;
3252 }
3253
Chris Wilson8542a0b2009-09-09 21:15:15 +01003254 /* Validate that the target is in a valid r/w GPU domain */
Daniel Vetter16edd552010-02-19 11:52:02 +01003255 if (reloc->write_domain & (reloc->write_domain - 1)) {
3256 DRM_ERROR("reloc with multiple write domains: "
3257 "obj %p target %d offset %d "
3258 "read %08x write %08x",
3259 obj, reloc->target_handle,
3260 (int) reloc->offset,
3261 reloc->read_domains,
3262 reloc->write_domain);
Julia Lawall929f49b2010-10-02 15:59:17 +02003263 drm_gem_object_unreference(target_obj);
3264 i915_gem_object_unpin(obj);
Daniel Vetter16edd552010-02-19 11:52:02 +01003265 return -EINVAL;
3266 }
Chris Wilson8542a0b2009-09-09 21:15:15 +01003267 if (reloc->write_domain & I915_GEM_DOMAIN_CPU ||
3268 reloc->read_domains & I915_GEM_DOMAIN_CPU) {
3269 DRM_ERROR("reloc with read/write CPU domains: "
3270 "obj %p target %d offset %d "
3271 "read %08x write %08x",
3272 obj, reloc->target_handle,
3273 (int) reloc->offset,
3274 reloc->read_domains,
3275 reloc->write_domain);
3276 drm_gem_object_unreference(target_obj);
3277 i915_gem_object_unpin(obj);
3278 return -EINVAL;
3279 }
3280 if (reloc->write_domain && target_obj->pending_write_domain &&
3281 reloc->write_domain != target_obj->pending_write_domain) {
3282 DRM_ERROR("Write domain conflict: "
3283 "obj %p target %d offset %d "
3284 "new %08x old %08x\n",
3285 obj, reloc->target_handle,
3286 (int) reloc->offset,
3287 reloc->write_domain,
3288 target_obj->pending_write_domain);
3289 drm_gem_object_unreference(target_obj);
3290 i915_gem_object_unpin(obj);
3291 return -EINVAL;
3292 }
3293
3294 target_obj->pending_read_domains |= reloc->read_domains;
3295 target_obj->pending_write_domain |= reloc->write_domain;
3296
3297 /* If the relocation already has the right value in it, no
3298 * more work needs to be done.
3299 */
3300 if (target_obj_priv->gtt_offset == reloc->presumed_offset) {
3301 drm_gem_object_unreference(target_obj);
3302 continue;
3303 }
3304
3305 /* Check that the relocation address is valid... */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003306 if (reloc->offset > obj->size - 4) {
Eric Anholt673a3942008-07-30 12:06:12 -07003307 DRM_ERROR("Relocation beyond object bounds: "
3308 "obj %p target %d offset %d size %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003309 obj, reloc->target_handle,
3310 (int) reloc->offset, (int) obj->size);
Eric Anholt673a3942008-07-30 12:06:12 -07003311 drm_gem_object_unreference(target_obj);
3312 i915_gem_object_unpin(obj);
3313 return -EINVAL;
3314 }
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003315 if (reloc->offset & 3) {
Eric Anholt673a3942008-07-30 12:06:12 -07003316 DRM_ERROR("Relocation not 4-byte aligned: "
3317 "obj %p target %d offset %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003318 obj, reloc->target_handle,
3319 (int) reloc->offset);
Eric Anholt673a3942008-07-30 12:06:12 -07003320 drm_gem_object_unreference(target_obj);
3321 i915_gem_object_unpin(obj);
3322 return -EINVAL;
3323 }
3324
Chris Wilson8542a0b2009-09-09 21:15:15 +01003325 /* and points to somewhere within the target object. */
Chris Wilsoncd0b9fb2009-09-15 23:23:18 +01003326 if (reloc->delta >= target_obj->size) {
3327 DRM_ERROR("Relocation beyond target object bounds: "
3328 "obj %p target %d delta %d size %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003329 obj, reloc->target_handle,
Chris Wilsoncd0b9fb2009-09-15 23:23:18 +01003330 (int) reloc->delta, (int) target_obj->size);
Chris Wilson491152b2009-02-11 14:26:32 +00003331 drm_gem_object_unreference(target_obj);
3332 i915_gem_object_unpin(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003333 return -EINVAL;
3334 }
3335
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003336 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
3337 if (ret != 0) {
3338 drm_gem_object_unreference(target_obj);
3339 i915_gem_object_unpin(obj);
3340 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -07003341 }
3342
3343 /* Map the page containing the relocation we're going to
3344 * perform.
3345 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003346 reloc_offset = obj_priv->gtt_offset + reloc->offset;
Keith Packard0839ccb2008-10-30 19:38:48 -07003347 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
3348 (reloc_offset &
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -07003349 ~(PAGE_SIZE - 1)));
Eric Anholt3043c602008-10-02 12:24:47 -07003350 reloc_entry = (uint32_t __iomem *)(reloc_page +
Keith Packard0839ccb2008-10-30 19:38:48 -07003351 (reloc_offset & (PAGE_SIZE - 1)));
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003352 reloc_val = target_obj_priv->gtt_offset + reloc->delta;
Eric Anholt673a3942008-07-30 12:06:12 -07003353
3354#if WATCH_BUF
3355 DRM_INFO("Applied relocation: %p@0x%08x %08x -> %08x\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003356 obj, (unsigned int) reloc->offset,
Eric Anholt673a3942008-07-30 12:06:12 -07003357 readl(reloc_entry), reloc_val);
3358#endif
3359 writel(reloc_val, reloc_entry);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -07003360 io_mapping_unmap_atomic(reloc_page);
Eric Anholt673a3942008-07-30 12:06:12 -07003361
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003362 /* The updated presumed offset for this entry will be
3363 * copied back out to the user.
Eric Anholt673a3942008-07-30 12:06:12 -07003364 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003365 reloc->presumed_offset = target_obj_priv->gtt_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003366
3367 drm_gem_object_unreference(target_obj);
3368 }
3369
Eric Anholt673a3942008-07-30 12:06:12 -07003370#if WATCH_BUF
3371 if (0)
3372 i915_gem_dump_object(obj, 128, __func__, ~0);
3373#endif
3374 return 0;
3375}
3376
Eric Anholt673a3942008-07-30 12:06:12 -07003377/* Throttle our rendering by waiting until the ring has completed our requests
3378 * emitted over 20 msec ago.
3379 *
Eric Anholtb9624422009-06-03 07:27:35 +00003380 * Note that if we were to use the current jiffies each time around the loop,
3381 * we wouldn't escape the function with any frames outstanding if the time to
3382 * render a frame was over 20ms.
3383 *
Eric Anholt673a3942008-07-30 12:06:12 -07003384 * This should get us reasonable parallelism between CPU and GPU but also
3385 * relatively low latency when blocking on a particular request to finish.
3386 */
3387static int
3388i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file_priv)
3389{
3390 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
3391 int ret = 0;
Eric Anholtb9624422009-06-03 07:27:35 +00003392 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
Eric Anholt673a3942008-07-30 12:06:12 -07003393
3394 mutex_lock(&dev->struct_mutex);
Eric Anholtb9624422009-06-03 07:27:35 +00003395 while (!list_empty(&i915_file_priv->mm.request_list)) {
3396 struct drm_i915_gem_request *request;
3397
3398 request = list_first_entry(&i915_file_priv->mm.request_list,
3399 struct drm_i915_gem_request,
3400 client_list);
3401
3402 if (time_after_eq(request->emitted_jiffies, recent_enough))
3403 break;
3404
Zou Nan hai852835f2010-05-21 09:08:56 +08003405 ret = i915_wait_request(dev, request->seqno, request->ring);
Eric Anholtb9624422009-06-03 07:27:35 +00003406 if (ret != 0)
3407 break;
3408 }
Eric Anholt673a3942008-07-30 12:06:12 -07003409 mutex_unlock(&dev->struct_mutex);
Eric Anholtb9624422009-06-03 07:27:35 +00003410
Eric Anholt673a3942008-07-30 12:06:12 -07003411 return ret;
3412}
3413
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003414static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003415i915_gem_get_relocs_from_user(struct drm_i915_gem_exec_object2 *exec_list,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003416 uint32_t buffer_count,
3417 struct drm_i915_gem_relocation_entry **relocs)
3418{
3419 uint32_t reloc_count = 0, reloc_index = 0, i;
3420 int ret;
3421
3422 *relocs = NULL;
3423 for (i = 0; i < buffer_count; i++) {
3424 if (reloc_count + exec_list[i].relocation_count < reloc_count)
3425 return -EINVAL;
3426 reloc_count += exec_list[i].relocation_count;
3427 }
3428
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003429 *relocs = drm_calloc_large(reloc_count, sizeof(**relocs));
Jesse Barnes76446ca2009-12-17 22:05:42 -05003430 if (*relocs == NULL) {
3431 DRM_ERROR("failed to alloc relocs, count %d\n", reloc_count);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003432 return -ENOMEM;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003433 }
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003434
3435 for (i = 0; i < buffer_count; i++) {
3436 struct drm_i915_gem_relocation_entry __user *user_relocs;
3437
3438 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3439
3440 ret = copy_from_user(&(*relocs)[reloc_index],
3441 user_relocs,
3442 exec_list[i].relocation_count *
3443 sizeof(**relocs));
3444 if (ret != 0) {
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003445 drm_free_large(*relocs);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003446 *relocs = NULL;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003447 return -EFAULT;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003448 }
3449
3450 reloc_index += exec_list[i].relocation_count;
3451 }
3452
Florian Mickler2bc43b52009-04-06 22:55:41 +02003453 return 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003454}
3455
3456static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003457i915_gem_put_relocs_to_user(struct drm_i915_gem_exec_object2 *exec_list,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003458 uint32_t buffer_count,
3459 struct drm_i915_gem_relocation_entry *relocs)
3460{
3461 uint32_t reloc_count = 0, i;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003462 int ret = 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003463
Chris Wilson93533c22010-01-31 10:40:48 +00003464 if (relocs == NULL)
3465 return 0;
3466
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003467 for (i = 0; i < buffer_count; i++) {
3468 struct drm_i915_gem_relocation_entry __user *user_relocs;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003469 int unwritten;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003470
3471 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3472
Florian Mickler2bc43b52009-04-06 22:55:41 +02003473 unwritten = copy_to_user(user_relocs,
3474 &relocs[reloc_count],
3475 exec_list[i].relocation_count *
3476 sizeof(*relocs));
3477
3478 if (unwritten) {
3479 ret = -EFAULT;
3480 goto err;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003481 }
3482
3483 reloc_count += exec_list[i].relocation_count;
3484 }
3485
Florian Mickler2bc43b52009-04-06 22:55:41 +02003486err:
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003487 drm_free_large(relocs);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003488
3489 return ret;
3490}
3491
Chris Wilson83d60792009-06-06 09:45:57 +01003492static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003493i915_gem_check_execbuffer (struct drm_i915_gem_execbuffer2 *exec,
Chris Wilson83d60792009-06-06 09:45:57 +01003494 uint64_t exec_offset)
3495{
3496 uint32_t exec_start, exec_len;
3497
3498 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
3499 exec_len = (uint32_t) exec->batch_len;
3500
3501 if ((exec_start | exec_len) & 0x7)
3502 return -EINVAL;
3503
3504 if (!exec_start)
3505 return -EINVAL;
3506
3507 return 0;
3508}
3509
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003510static int
3511i915_gem_wait_for_pending_flip(struct drm_device *dev,
3512 struct drm_gem_object **object_list,
3513 int count)
3514{
3515 drm_i915_private_t *dev_priv = dev->dev_private;
3516 struct drm_i915_gem_object *obj_priv;
3517 DEFINE_WAIT(wait);
3518 int i, ret = 0;
3519
3520 for (;;) {
3521 prepare_to_wait(&dev_priv->pending_flip_queue,
3522 &wait, TASK_INTERRUPTIBLE);
3523 for (i = 0; i < count; i++) {
Daniel Vetter23010e42010-03-08 13:35:02 +01003524 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003525 if (atomic_read(&obj_priv->pending_flip) > 0)
3526 break;
3527 }
3528 if (i == count)
3529 break;
3530
3531 if (!signal_pending(current)) {
3532 mutex_unlock(&dev->struct_mutex);
3533 schedule();
3534 mutex_lock(&dev->struct_mutex);
3535 continue;
3536 }
3537 ret = -ERESTARTSYS;
3538 break;
3539 }
3540 finish_wait(&dev_priv->pending_flip_queue, &wait);
3541
3542 return ret;
3543}
3544
Chris Wilson43b27f42010-07-02 08:57:15 +01003545
Eric Anholt673a3942008-07-30 12:06:12 -07003546int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003547i915_gem_do_execbuffer(struct drm_device *dev, void *data,
3548 struct drm_file *file_priv,
3549 struct drm_i915_gem_execbuffer2 *args,
3550 struct drm_i915_gem_exec_object2 *exec_list)
Eric Anholt673a3942008-07-30 12:06:12 -07003551{
3552 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003553 struct drm_gem_object **object_list = NULL;
3554 struct drm_gem_object *batch_obj;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003555 struct drm_i915_gem_object *obj_priv;
Eric Anholt201361a2009-03-11 12:30:04 -07003556 struct drm_clip_rect *cliprects = NULL;
Chris Wilson93533c22010-01-31 10:40:48 +00003557 struct drm_i915_gem_relocation_entry *relocs = NULL;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003558 int ret = 0, ret2, i, pinned = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003559 uint64_t exec_offset;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003560 uint32_t seqno, flush_domains, reloc_index;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003561 int pin_tries, flips;
Eric Anholt673a3942008-07-30 12:06:12 -07003562
Zou Nan hai852835f2010-05-21 09:08:56 +08003563 struct intel_ring_buffer *ring = NULL;
3564
Eric Anholt673a3942008-07-30 12:06:12 -07003565#if WATCH_EXEC
3566 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3567 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3568#endif
Zou Nan haid1b851f2010-05-21 09:08:57 +08003569 if (args->flags & I915_EXEC_BSD) {
3570 if (!HAS_BSD(dev)) {
3571 DRM_ERROR("execbuf with wrong flag\n");
3572 return -EINVAL;
3573 }
3574 ring = &dev_priv->bsd_ring;
3575 } else {
3576 ring = &dev_priv->render_ring;
3577 }
3578
Eric Anholt4f481ed2008-09-10 14:22:49 -07003579 if (args->buffer_count < 1) {
3580 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3581 return -EINVAL;
3582 }
Eric Anholtc8e0f932009-11-22 03:49:37 +01003583 object_list = drm_malloc_ab(sizeof(*object_list), args->buffer_count);
Jesse Barnes76446ca2009-12-17 22:05:42 -05003584 if (object_list == NULL) {
3585 DRM_ERROR("Failed to allocate object list for %d buffers\n",
Eric Anholt673a3942008-07-30 12:06:12 -07003586 args->buffer_count);
3587 ret = -ENOMEM;
3588 goto pre_mutex_err;
3589 }
Eric Anholt673a3942008-07-30 12:06:12 -07003590
Eric Anholt201361a2009-03-11 12:30:04 -07003591 if (args->num_cliprects != 0) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003592 cliprects = kcalloc(args->num_cliprects, sizeof(*cliprects),
3593 GFP_KERNEL);
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003594 if (cliprects == NULL) {
3595 ret = -ENOMEM;
Eric Anholt201361a2009-03-11 12:30:04 -07003596 goto pre_mutex_err;
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003597 }
Eric Anholt201361a2009-03-11 12:30:04 -07003598
3599 ret = copy_from_user(cliprects,
3600 (struct drm_clip_rect __user *)
3601 (uintptr_t) args->cliprects_ptr,
3602 sizeof(*cliprects) * args->num_cliprects);
3603 if (ret != 0) {
3604 DRM_ERROR("copy %d cliprects failed: %d\n",
3605 args->num_cliprects, ret);
Dan Carpenterc877cdc2010-06-23 19:03:01 +02003606 ret = -EFAULT;
Eric Anholt201361a2009-03-11 12:30:04 -07003607 goto pre_mutex_err;
3608 }
3609 }
3610
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003611 ret = i915_gem_get_relocs_from_user(exec_list, args->buffer_count,
3612 &relocs);
3613 if (ret != 0)
3614 goto pre_mutex_err;
3615
Eric Anholt673a3942008-07-30 12:06:12 -07003616 mutex_lock(&dev->struct_mutex);
3617
3618 i915_verify_inactive(dev, __FILE__, __LINE__);
3619
Ben Gamariba1234d2009-09-14 17:48:47 -04003620 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07003621 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003622 ret = -EIO;
3623 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003624 }
3625
3626 if (dev_priv->mm.suspended) {
Eric Anholt673a3942008-07-30 12:06:12 -07003627 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003628 ret = -EBUSY;
3629 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003630 }
3631
Keith Packardac94a962008-11-20 23:30:27 -08003632 /* Look up object handles */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003633 flips = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003634 for (i = 0; i < args->buffer_count; i++) {
3635 object_list[i] = drm_gem_object_lookup(dev, file_priv,
3636 exec_list[i].handle);
3637 if (object_list[i] == NULL) {
3638 DRM_ERROR("Invalid object handle %d at index %d\n",
3639 exec_list[i].handle, i);
Chris Wilson0ce907f2010-01-23 20:26:35 +00003640 /* prevent error path from reading uninitialized data */
3641 args->buffer_count = i + 1;
Chris Wilsonbf79cb92010-08-04 14:19:46 +01003642 ret = -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07003643 goto err;
3644 }
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003645
Daniel Vetter23010e42010-03-08 13:35:02 +01003646 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003647 if (obj_priv->in_execbuffer) {
3648 DRM_ERROR("Object %p appears more than once in object list\n",
3649 object_list[i]);
Chris Wilson0ce907f2010-01-23 20:26:35 +00003650 /* prevent error path from reading uninitialized data */
3651 args->buffer_count = i + 1;
Chris Wilsonbf79cb92010-08-04 14:19:46 +01003652 ret = -EINVAL;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003653 goto err;
3654 }
3655 obj_priv->in_execbuffer = true;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003656 flips += atomic_read(&obj_priv->pending_flip);
3657 }
3658
3659 if (flips > 0) {
3660 ret = i915_gem_wait_for_pending_flip(dev, object_list,
3661 args->buffer_count);
3662 if (ret)
3663 goto err;
Keith Packardac94a962008-11-20 23:30:27 -08003664 }
Eric Anholt673a3942008-07-30 12:06:12 -07003665
Keith Packardac94a962008-11-20 23:30:27 -08003666 /* Pin and relocate */
3667 for (pin_tries = 0; ; pin_tries++) {
3668 ret = 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003669 reloc_index = 0;
3670
Keith Packardac94a962008-11-20 23:30:27 -08003671 for (i = 0; i < args->buffer_count; i++) {
3672 object_list[i]->pending_read_domains = 0;
3673 object_list[i]->pending_write_domain = 0;
3674 ret = i915_gem_object_pin_and_relocate(object_list[i],
3675 file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003676 &exec_list[i],
3677 &relocs[reloc_index]);
Keith Packardac94a962008-11-20 23:30:27 -08003678 if (ret)
3679 break;
3680 pinned = i + 1;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003681 reloc_index += exec_list[i].relocation_count;
Keith Packardac94a962008-11-20 23:30:27 -08003682 }
3683 /* success */
3684 if (ret == 0)
3685 break;
3686
3687 /* error other than GTT full, or we've already tried again */
Chris Wilson2939e1f2009-06-06 09:46:03 +01003688 if (ret != -ENOSPC || pin_tries >= 1) {
Chris Wilson07f73f62009-09-14 16:50:30 +01003689 if (ret != -ERESTARTSYS) {
3690 unsigned long long total_size = 0;
Chris Wilson3d1cc472010-05-27 13:18:19 +01003691 int num_fences = 0;
3692 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson43b27f42010-07-02 08:57:15 +01003693 obj_priv = to_intel_bo(object_list[i]);
Chris Wilson3d1cc472010-05-27 13:18:19 +01003694
Chris Wilson07f73f62009-09-14 16:50:30 +01003695 total_size += object_list[i]->size;
Chris Wilson3d1cc472010-05-27 13:18:19 +01003696 num_fences +=
3697 exec_list[i].flags & EXEC_OBJECT_NEEDS_FENCE &&
3698 obj_priv->tiling_mode != I915_TILING_NONE;
3699 }
3700 DRM_ERROR("Failed to pin buffer %d of %d, total %llu bytes, %d fences: %d\n",
Chris Wilson07f73f62009-09-14 16:50:30 +01003701 pinned+1, args->buffer_count,
Chris Wilson3d1cc472010-05-27 13:18:19 +01003702 total_size, num_fences,
3703 ret);
Chris Wilson07f73f62009-09-14 16:50:30 +01003704 DRM_ERROR("%d objects [%d pinned], "
3705 "%d object bytes [%d pinned], "
3706 "%d/%d gtt bytes\n",
3707 atomic_read(&dev->object_count),
3708 atomic_read(&dev->pin_count),
3709 atomic_read(&dev->object_memory),
3710 atomic_read(&dev->pin_memory),
3711 atomic_read(&dev->gtt_memory),
3712 dev->gtt_total);
3713 }
Eric Anholt673a3942008-07-30 12:06:12 -07003714 goto err;
3715 }
Keith Packardac94a962008-11-20 23:30:27 -08003716
3717 /* unpin all of our buffers */
3718 for (i = 0; i < pinned; i++)
3719 i915_gem_object_unpin(object_list[i]);
Eric Anholtb1177632008-12-10 10:09:41 -08003720 pinned = 0;
Keith Packardac94a962008-11-20 23:30:27 -08003721
3722 /* evict everyone we can from the aperture */
3723 ret = i915_gem_evict_everything(dev);
Chris Wilson07f73f62009-09-14 16:50:30 +01003724 if (ret && ret != -ENOSPC)
Keith Packardac94a962008-11-20 23:30:27 -08003725 goto err;
Eric Anholt673a3942008-07-30 12:06:12 -07003726 }
3727
3728 /* Set the pending read domains for the batch buffer to COMMAND */
3729 batch_obj = object_list[args->buffer_count-1];
Chris Wilson5f26a2c2009-06-06 09:45:58 +01003730 if (batch_obj->pending_write_domain) {
3731 DRM_ERROR("Attempting to use self-modifying batch buffer\n");
3732 ret = -EINVAL;
3733 goto err;
3734 }
3735 batch_obj->pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
Eric Anholt673a3942008-07-30 12:06:12 -07003736
Chris Wilson83d60792009-06-06 09:45:57 +01003737 /* Sanity check the batch buffer, prior to moving objects */
3738 exec_offset = exec_list[args->buffer_count - 1].offset;
3739 ret = i915_gem_check_execbuffer (args, exec_offset);
3740 if (ret != 0) {
3741 DRM_ERROR("execbuf with invalid offset/length\n");
3742 goto err;
3743 }
3744
Eric Anholt673a3942008-07-30 12:06:12 -07003745 i915_verify_inactive(dev, __FILE__, __LINE__);
3746
Keith Packard646f0f62008-11-20 23:23:03 -08003747 /* Zero the global flush/invalidate flags. These
3748 * will be modified as new domains are computed
3749 * for each object
3750 */
3751 dev->invalidate_domains = 0;
3752 dev->flush_domains = 0;
Chris Wilson88f356b2010-08-04 13:55:32 +01003753 dev_priv->flush_rings = 0;
Keith Packard646f0f62008-11-20 23:23:03 -08003754
Eric Anholt673a3942008-07-30 12:06:12 -07003755 for (i = 0; i < args->buffer_count; i++) {
3756 struct drm_gem_object *obj = object_list[i];
Eric Anholt673a3942008-07-30 12:06:12 -07003757
Keith Packard646f0f62008-11-20 23:23:03 -08003758 /* Compute new gpu domains and update invalidate/flush */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003759 i915_gem_object_set_to_gpu_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003760 }
3761
3762 i915_verify_inactive(dev, __FILE__, __LINE__);
3763
Keith Packard646f0f62008-11-20 23:23:03 -08003764 if (dev->invalidate_domains | dev->flush_domains) {
3765#if WATCH_EXEC
3766 DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
3767 __func__,
3768 dev->invalidate_domains,
3769 dev->flush_domains);
3770#endif
3771 i915_gem_flush(dev,
3772 dev->invalidate_domains,
3773 dev->flush_domains);
Chris Wilson88f356b2010-08-04 13:55:32 +01003774 if (dev_priv->flush_rings & FLUSH_RENDER_RING)
Eric Anholtb9624422009-06-03 07:27:35 +00003775 (void)i915_add_request(dev, file_priv,
Chris Wilson88f356b2010-08-04 13:55:32 +01003776 dev->flush_domains,
3777 &dev_priv->render_ring);
3778 if (dev_priv->flush_rings & FLUSH_BSD_RING)
3779 (void)i915_add_request(dev, file_priv,
3780 dev->flush_domains,
3781 &dev_priv->bsd_ring);
Keith Packard646f0f62008-11-20 23:23:03 -08003782 }
Eric Anholt673a3942008-07-30 12:06:12 -07003783
Eric Anholtefbeed92009-02-19 14:54:51 -08003784 for (i = 0; i < args->buffer_count; i++) {
3785 struct drm_gem_object *obj = object_list[i];
Daniel Vetter23010e42010-03-08 13:35:02 +01003786 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003787 uint32_t old_write_domain = obj->write_domain;
Eric Anholtefbeed92009-02-19 14:54:51 -08003788
3789 obj->write_domain = obj->pending_write_domain;
Daniel Vetter99fcb762010-02-07 16:20:18 +01003790 if (obj->write_domain)
3791 list_move_tail(&obj_priv->gpu_write_list,
3792 &dev_priv->mm.gpu_write_list);
3793 else
3794 list_del_init(&obj_priv->gpu_write_list);
3795
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003796 trace_i915_gem_object_change_domain(obj,
3797 obj->read_domains,
3798 old_write_domain);
Eric Anholtefbeed92009-02-19 14:54:51 -08003799 }
3800
Eric Anholt673a3942008-07-30 12:06:12 -07003801 i915_verify_inactive(dev, __FILE__, __LINE__);
3802
3803#if WATCH_COHERENCY
3804 for (i = 0; i < args->buffer_count; i++) {
3805 i915_gem_object_check_coherency(object_list[i],
3806 exec_list[i].handle);
3807 }
3808#endif
3809
Eric Anholt673a3942008-07-30 12:06:12 -07003810#if WATCH_EXEC
Ben Gamari6911a9b2009-04-02 11:24:54 -07003811 i915_gem_dump_object(batch_obj,
Eric Anholt673a3942008-07-30 12:06:12 -07003812 args->batch_len,
3813 __func__,
3814 ~0);
3815#endif
3816
Eric Anholt673a3942008-07-30 12:06:12 -07003817 /* Exec the batchbuffer */
Zou Nan hai852835f2010-05-21 09:08:56 +08003818 ret = ring->dispatch_gem_execbuffer(dev, ring, args,
3819 cliprects, exec_offset);
Eric Anholt673a3942008-07-30 12:06:12 -07003820 if (ret) {
3821 DRM_ERROR("dispatch failed %d\n", ret);
3822 goto err;
3823 }
3824
3825 /*
3826 * Ensure that the commands in the batch buffer are
3827 * finished before the interrupt fires
3828 */
Zou Nan hai852835f2010-05-21 09:08:56 +08003829 flush_domains = i915_retire_commands(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07003830
3831 i915_verify_inactive(dev, __FILE__, __LINE__);
3832
3833 /*
3834 * Get a seqno representing the execution of the current buffer,
3835 * which we can wait on. We would like to mitigate these interrupts,
3836 * likely by only creating seqnos occasionally (so that we have
3837 * *some* interrupts representing completion of buffers that we can
3838 * wait on when trying to clear up gtt space).
3839 */
Zou Nan hai852835f2010-05-21 09:08:56 +08003840 seqno = i915_add_request(dev, file_priv, flush_domains, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07003841 BUG_ON(seqno == 0);
Eric Anholt673a3942008-07-30 12:06:12 -07003842 for (i = 0; i < args->buffer_count; i++) {
3843 struct drm_gem_object *obj = object_list[i];
Zou Nan hai852835f2010-05-21 09:08:56 +08003844 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003845
Zou Nan hai852835f2010-05-21 09:08:56 +08003846 i915_gem_object_move_to_active(obj, seqno, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07003847#if WATCH_LRU
3848 DRM_INFO("%s: move to exec list %p\n", __func__, obj);
3849#endif
3850 }
3851#if WATCH_LRU
3852 i915_dump_lru(dev, __func__);
3853#endif
3854
3855 i915_verify_inactive(dev, __FILE__, __LINE__);
3856
Eric Anholt673a3942008-07-30 12:06:12 -07003857err:
Julia Lawallaad87df2008-12-21 16:28:47 +01003858 for (i = 0; i < pinned; i++)
3859 i915_gem_object_unpin(object_list[i]);
Eric Anholt673a3942008-07-30 12:06:12 -07003860
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003861 for (i = 0; i < args->buffer_count; i++) {
3862 if (object_list[i]) {
Daniel Vetter23010e42010-03-08 13:35:02 +01003863 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003864 obj_priv->in_execbuffer = false;
3865 }
Julia Lawallaad87df2008-12-21 16:28:47 +01003866 drm_gem_object_unreference(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003867 }
Julia Lawallaad87df2008-12-21 16:28:47 +01003868
Eric Anholt673a3942008-07-30 12:06:12 -07003869 mutex_unlock(&dev->struct_mutex);
3870
Chris Wilson93533c22010-01-31 10:40:48 +00003871pre_mutex_err:
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003872 /* Copy the updated relocations out regardless of current error
3873 * state. Failure to update the relocs would mean that the next
3874 * time userland calls execbuf, it would do so with presumed offset
3875 * state that didn't match the actual object state.
3876 */
3877 ret2 = i915_gem_put_relocs_to_user(exec_list, args->buffer_count,
3878 relocs);
3879 if (ret2 != 0) {
3880 DRM_ERROR("Failed to copy relocations back out: %d\n", ret2);
3881
3882 if (ret == 0)
3883 ret = ret2;
3884 }
3885
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003886 drm_free_large(object_list);
Eric Anholt9a298b22009-03-24 12:23:04 -07003887 kfree(cliprects);
Eric Anholt673a3942008-07-30 12:06:12 -07003888
3889 return ret;
3890}
3891
Jesse Barnes76446ca2009-12-17 22:05:42 -05003892/*
3893 * Legacy execbuffer just creates an exec2 list from the original exec object
3894 * list array and passes it to the real function.
3895 */
3896int
3897i915_gem_execbuffer(struct drm_device *dev, void *data,
3898 struct drm_file *file_priv)
3899{
3900 struct drm_i915_gem_execbuffer *args = data;
3901 struct drm_i915_gem_execbuffer2 exec2;
3902 struct drm_i915_gem_exec_object *exec_list = NULL;
3903 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
3904 int ret, i;
3905
3906#if WATCH_EXEC
3907 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3908 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3909#endif
3910
3911 if (args->buffer_count < 1) {
3912 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3913 return -EINVAL;
3914 }
3915
3916 /* Copy in the exec list from userland */
3917 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
3918 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
3919 if (exec_list == NULL || exec2_list == NULL) {
3920 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
3921 args->buffer_count);
3922 drm_free_large(exec_list);
3923 drm_free_large(exec2_list);
3924 return -ENOMEM;
3925 }
3926 ret = copy_from_user(exec_list,
3927 (struct drm_i915_relocation_entry __user *)
3928 (uintptr_t) args->buffers_ptr,
3929 sizeof(*exec_list) * args->buffer_count);
3930 if (ret != 0) {
3931 DRM_ERROR("copy %d exec entries failed %d\n",
3932 args->buffer_count, ret);
3933 drm_free_large(exec_list);
3934 drm_free_large(exec2_list);
3935 return -EFAULT;
3936 }
3937
3938 for (i = 0; i < args->buffer_count; i++) {
3939 exec2_list[i].handle = exec_list[i].handle;
3940 exec2_list[i].relocation_count = exec_list[i].relocation_count;
3941 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
3942 exec2_list[i].alignment = exec_list[i].alignment;
3943 exec2_list[i].offset = exec_list[i].offset;
3944 if (!IS_I965G(dev))
3945 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
3946 else
3947 exec2_list[i].flags = 0;
3948 }
3949
3950 exec2.buffers_ptr = args->buffers_ptr;
3951 exec2.buffer_count = args->buffer_count;
3952 exec2.batch_start_offset = args->batch_start_offset;
3953 exec2.batch_len = args->batch_len;
3954 exec2.DR1 = args->DR1;
3955 exec2.DR4 = args->DR4;
3956 exec2.num_cliprects = args->num_cliprects;
3957 exec2.cliprects_ptr = args->cliprects_ptr;
Zou Nan hai852835f2010-05-21 09:08:56 +08003958 exec2.flags = I915_EXEC_RENDER;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003959
3960 ret = i915_gem_do_execbuffer(dev, data, file_priv, &exec2, exec2_list);
3961 if (!ret) {
3962 /* Copy the new buffer offsets back to the user's exec list. */
3963 for (i = 0; i < args->buffer_count; i++)
3964 exec_list[i].offset = exec2_list[i].offset;
3965 /* ... and back out to userspace */
3966 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
3967 (uintptr_t) args->buffers_ptr,
3968 exec_list,
3969 sizeof(*exec_list) * args->buffer_count);
3970 if (ret) {
3971 ret = -EFAULT;
3972 DRM_ERROR("failed to copy %d exec entries "
3973 "back to user (%d)\n",
3974 args->buffer_count, ret);
3975 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05003976 }
3977
3978 drm_free_large(exec_list);
3979 drm_free_large(exec2_list);
3980 return ret;
3981}
3982
3983int
3984i915_gem_execbuffer2(struct drm_device *dev, void *data,
3985 struct drm_file *file_priv)
3986{
3987 struct drm_i915_gem_execbuffer2 *args = data;
3988 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
3989 int ret;
3990
3991#if WATCH_EXEC
3992 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3993 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3994#endif
3995
3996 if (args->buffer_count < 1) {
3997 DRM_ERROR("execbuf2 with %d buffers\n", args->buffer_count);
3998 return -EINVAL;
3999 }
4000
4001 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
4002 if (exec2_list == NULL) {
4003 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
4004 args->buffer_count);
4005 return -ENOMEM;
4006 }
4007 ret = copy_from_user(exec2_list,
4008 (struct drm_i915_relocation_entry __user *)
4009 (uintptr_t) args->buffers_ptr,
4010 sizeof(*exec2_list) * args->buffer_count);
4011 if (ret != 0) {
4012 DRM_ERROR("copy %d exec entries failed %d\n",
4013 args->buffer_count, ret);
4014 drm_free_large(exec2_list);
4015 return -EFAULT;
4016 }
4017
4018 ret = i915_gem_do_execbuffer(dev, data, file_priv, args, exec2_list);
4019 if (!ret) {
4020 /* Copy the new buffer offsets back to the user's exec list. */
4021 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
4022 (uintptr_t) args->buffers_ptr,
4023 exec2_list,
4024 sizeof(*exec2_list) * args->buffer_count);
4025 if (ret) {
4026 ret = -EFAULT;
4027 DRM_ERROR("failed to copy %d exec entries "
4028 "back to user (%d)\n",
4029 args->buffer_count, ret);
4030 }
4031 }
4032
4033 drm_free_large(exec2_list);
4034 return ret;
4035}
4036
Eric Anholt673a3942008-07-30 12:06:12 -07004037int
4038i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment)
4039{
4040 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01004041 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004042 int ret;
4043
Daniel Vetter778c3542010-05-13 11:49:44 +02004044 BUG_ON(obj_priv->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT);
4045
Eric Anholt673a3942008-07-30 12:06:12 -07004046 i915_verify_inactive(dev, __FILE__, __LINE__);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01004047
4048 if (obj_priv->gtt_space != NULL) {
4049 if (alignment == 0)
4050 alignment = i915_gem_get_gtt_alignment(obj);
4051 if (obj_priv->gtt_offset & (alignment - 1)) {
Chris Wilsonae7d49d2010-08-04 12:37:41 +01004052 WARN(obj_priv->pin_count,
4053 "bo is already pinned with incorrect alignment:"
4054 " offset=%x, req.alignment=%x\n",
4055 obj_priv->gtt_offset, alignment);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01004056 ret = i915_gem_object_unbind(obj);
4057 if (ret)
4058 return ret;
4059 }
4060 }
4061
Eric Anholt673a3942008-07-30 12:06:12 -07004062 if (obj_priv->gtt_space == NULL) {
4063 ret = i915_gem_object_bind_to_gtt(obj, alignment);
Chris Wilson97311292009-09-21 00:22:34 +01004064 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07004065 return ret;
Chris Wilson22c344e2009-02-11 14:26:45 +00004066 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05004067
Eric Anholt673a3942008-07-30 12:06:12 -07004068 obj_priv->pin_count++;
4069
4070 /* If the object is not active and not pending a flush,
4071 * remove it from the inactive list
4072 */
4073 if (obj_priv->pin_count == 1) {
4074 atomic_inc(&dev->pin_count);
4075 atomic_add(obj->size, &dev->pin_memory);
4076 if (!obj_priv->active &&
Chris Wilsonbf1a1092010-08-07 11:01:20 +01004077 (obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
Eric Anholt673a3942008-07-30 12:06:12 -07004078 list_del_init(&obj_priv->list);
4079 }
4080 i915_verify_inactive(dev, __FILE__, __LINE__);
4081
4082 return 0;
4083}
4084
4085void
4086i915_gem_object_unpin(struct drm_gem_object *obj)
4087{
4088 struct drm_device *dev = obj->dev;
4089 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01004090 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004091
4092 i915_verify_inactive(dev, __FILE__, __LINE__);
4093 obj_priv->pin_count--;
4094 BUG_ON(obj_priv->pin_count < 0);
4095 BUG_ON(obj_priv->gtt_space == NULL);
4096
4097 /* If the object is no longer pinned, and is
4098 * neither active nor being flushed, then stick it on
4099 * the inactive list
4100 */
4101 if (obj_priv->pin_count == 0) {
4102 if (!obj_priv->active &&
Chris Wilson21d509e2009-06-06 09:46:02 +01004103 (obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
Eric Anholt673a3942008-07-30 12:06:12 -07004104 list_move_tail(&obj_priv->list,
4105 &dev_priv->mm.inactive_list);
4106 atomic_dec(&dev->pin_count);
4107 atomic_sub(obj->size, &dev->pin_memory);
4108 }
4109 i915_verify_inactive(dev, __FILE__, __LINE__);
4110}
4111
4112int
4113i915_gem_pin_ioctl(struct drm_device *dev, void *data,
4114 struct drm_file *file_priv)
4115{
4116 struct drm_i915_gem_pin *args = data;
4117 struct drm_gem_object *obj;
4118 struct drm_i915_gem_object *obj_priv;
4119 int ret;
4120
4121 mutex_lock(&dev->struct_mutex);
4122
4123 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4124 if (obj == NULL) {
4125 DRM_ERROR("Bad handle in i915_gem_pin_ioctl(): %d\n",
4126 args->handle);
4127 mutex_unlock(&dev->struct_mutex);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01004128 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07004129 }
Daniel Vetter23010e42010-03-08 13:35:02 +01004130 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004131
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004132 if (obj_priv->madv != I915_MADV_WILLNEED) {
4133 DRM_ERROR("Attempting to pin a purgeable buffer\n");
Chris Wilson3ef94da2009-09-14 16:50:29 +01004134 drm_gem_object_unreference(obj);
4135 mutex_unlock(&dev->struct_mutex);
4136 return -EINVAL;
4137 }
4138
Jesse Barnes79e53942008-11-07 14:24:08 -08004139 if (obj_priv->pin_filp != NULL && obj_priv->pin_filp != file_priv) {
4140 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
4141 args->handle);
Chris Wilson96dec612009-02-08 19:08:04 +00004142 drm_gem_object_unreference(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004143 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08004144 return -EINVAL;
4145 }
4146
4147 obj_priv->user_pin_count++;
4148 obj_priv->pin_filp = file_priv;
4149 if (obj_priv->user_pin_count == 1) {
4150 ret = i915_gem_object_pin(obj, args->alignment);
4151 if (ret != 0) {
4152 drm_gem_object_unreference(obj);
4153 mutex_unlock(&dev->struct_mutex);
4154 return ret;
4155 }
Eric Anholt673a3942008-07-30 12:06:12 -07004156 }
4157
4158 /* XXX - flush the CPU caches for pinned objects
4159 * as the X server doesn't manage domains yet
4160 */
Eric Anholte47c68e2008-11-14 13:35:19 -08004161 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004162 args->offset = obj_priv->gtt_offset;
4163 drm_gem_object_unreference(obj);
4164 mutex_unlock(&dev->struct_mutex);
4165
4166 return 0;
4167}
4168
4169int
4170i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
4171 struct drm_file *file_priv)
4172{
4173 struct drm_i915_gem_pin *args = data;
4174 struct drm_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08004175 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07004176
4177 mutex_lock(&dev->struct_mutex);
4178
4179 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4180 if (obj == NULL) {
4181 DRM_ERROR("Bad handle in i915_gem_unpin_ioctl(): %d\n",
4182 args->handle);
4183 mutex_unlock(&dev->struct_mutex);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01004184 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07004185 }
4186
Daniel Vetter23010e42010-03-08 13:35:02 +01004187 obj_priv = to_intel_bo(obj);
Jesse Barnes79e53942008-11-07 14:24:08 -08004188 if (obj_priv->pin_filp != file_priv) {
4189 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
4190 args->handle);
4191 drm_gem_object_unreference(obj);
4192 mutex_unlock(&dev->struct_mutex);
4193 return -EINVAL;
4194 }
4195 obj_priv->user_pin_count--;
4196 if (obj_priv->user_pin_count == 0) {
4197 obj_priv->pin_filp = NULL;
4198 i915_gem_object_unpin(obj);
4199 }
Eric Anholt673a3942008-07-30 12:06:12 -07004200
4201 drm_gem_object_unreference(obj);
4202 mutex_unlock(&dev->struct_mutex);
4203 return 0;
4204}
4205
4206int
4207i915_gem_busy_ioctl(struct drm_device *dev, void *data,
4208 struct drm_file *file_priv)
4209{
4210 struct drm_i915_gem_busy *args = data;
4211 struct drm_gem_object *obj;
4212 struct drm_i915_gem_object *obj_priv;
4213
Eric Anholt673a3942008-07-30 12:06:12 -07004214 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4215 if (obj == NULL) {
4216 DRM_ERROR("Bad handle in i915_gem_busy_ioctl(): %d\n",
4217 args->handle);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01004218 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07004219 }
4220
Chris Wilsonb1ce7862009-06-06 09:46:00 +01004221 mutex_lock(&dev->struct_mutex);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004222
Chris Wilson0be555b2010-08-04 15:36:30 +01004223 /* Count all active objects as busy, even if they are currently not used
4224 * by the gpu. Users of this interface expect objects to eventually
4225 * become non-busy without any further actions, therefore emit any
4226 * necessary flushes here.
Eric Anholtc4de0a52008-12-14 19:05:04 -08004227 */
Chris Wilson0be555b2010-08-04 15:36:30 +01004228 obj_priv = to_intel_bo(obj);
4229 args->busy = obj_priv->active;
4230 if (args->busy) {
4231 /* Unconditionally flush objects, even when the gpu still uses this
4232 * object. Userspace calling this function indicates that it wants to
4233 * use this buffer rather sooner than later, so issuing the required
4234 * flush earlier is beneficial.
4235 */
4236 if (obj->write_domain) {
4237 i915_gem_flush(dev, 0, obj->write_domain);
4238 (void)i915_add_request(dev, file_priv, obj->write_domain, obj_priv->ring);
4239 }
4240
4241 /* Update the active list for the hardware's current position.
4242 * Otherwise this only updates on a delayed timer or when irqs
4243 * are actually unmasked, and our working set ends up being
4244 * larger than required.
4245 */
4246 i915_gem_retire_requests_ring(dev, obj_priv->ring);
4247
4248 args->busy = obj_priv->active;
4249 }
Eric Anholt673a3942008-07-30 12:06:12 -07004250
4251 drm_gem_object_unreference(obj);
4252 mutex_unlock(&dev->struct_mutex);
4253 return 0;
4254}
4255
4256int
4257i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4258 struct drm_file *file_priv)
4259{
4260 return i915_gem_ring_throttle(dev, file_priv);
4261}
4262
Chris Wilson3ef94da2009-09-14 16:50:29 +01004263int
4264i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4265 struct drm_file *file_priv)
4266{
4267 struct drm_i915_gem_madvise *args = data;
4268 struct drm_gem_object *obj;
4269 struct drm_i915_gem_object *obj_priv;
4270
4271 switch (args->madv) {
4272 case I915_MADV_DONTNEED:
4273 case I915_MADV_WILLNEED:
4274 break;
4275 default:
4276 return -EINVAL;
4277 }
4278
4279 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4280 if (obj == NULL) {
4281 DRM_ERROR("Bad handle in i915_gem_madvise_ioctl(): %d\n",
4282 args->handle);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01004283 return -ENOENT;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004284 }
4285
4286 mutex_lock(&dev->struct_mutex);
Daniel Vetter23010e42010-03-08 13:35:02 +01004287 obj_priv = to_intel_bo(obj);
Chris Wilson3ef94da2009-09-14 16:50:29 +01004288
4289 if (obj_priv->pin_count) {
4290 drm_gem_object_unreference(obj);
4291 mutex_unlock(&dev->struct_mutex);
4292
4293 DRM_ERROR("Attempted i915_gem_madvise_ioctl() on a pinned object\n");
4294 return -EINVAL;
4295 }
4296
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004297 if (obj_priv->madv != __I915_MADV_PURGED)
4298 obj_priv->madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004299
Chris Wilson2d7ef392009-09-20 23:13:10 +01004300 /* if the object is no longer bound, discard its backing storage */
4301 if (i915_gem_object_is_purgeable(obj_priv) &&
4302 obj_priv->gtt_space == NULL)
4303 i915_gem_object_truncate(obj);
4304
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004305 args->retained = obj_priv->madv != __I915_MADV_PURGED;
4306
Chris Wilson3ef94da2009-09-14 16:50:29 +01004307 drm_gem_object_unreference(obj);
4308 mutex_unlock(&dev->struct_mutex);
4309
4310 return 0;
4311}
4312
Daniel Vetterac52bc52010-04-09 19:05:06 +00004313struct drm_gem_object * i915_gem_alloc_object(struct drm_device *dev,
4314 size_t size)
4315{
Daniel Vetterc397b902010-04-09 19:05:07 +00004316 struct drm_i915_gem_object *obj;
4317
4318 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
4319 if (obj == NULL)
4320 return NULL;
4321
4322 if (drm_gem_object_init(dev, &obj->base, size) != 0) {
4323 kfree(obj);
4324 return NULL;
4325 }
4326
4327 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4328 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4329
4330 obj->agp_type = AGP_USER_MEMORY;
Daniel Vetter62b8b212010-04-09 19:05:08 +00004331 obj->base.driver_private = NULL;
Daniel Vetterc397b902010-04-09 19:05:07 +00004332 obj->fence_reg = I915_FENCE_REG_NONE;
4333 INIT_LIST_HEAD(&obj->list);
4334 INIT_LIST_HEAD(&obj->gpu_write_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00004335 obj->madv = I915_MADV_WILLNEED;
4336
4337 trace_i915_gem_object_create(&obj->base);
4338
4339 return &obj->base;
Daniel Vetterac52bc52010-04-09 19:05:06 +00004340}
4341
Eric Anholt673a3942008-07-30 12:06:12 -07004342int i915_gem_init_object(struct drm_gem_object *obj)
4343{
Daniel Vetterc397b902010-04-09 19:05:07 +00004344 BUG();
Jesse Barnesde151cf2008-11-12 10:03:55 -08004345
Eric Anholt673a3942008-07-30 12:06:12 -07004346 return 0;
4347}
4348
Chris Wilsonbe726152010-07-23 23:18:50 +01004349static void i915_gem_free_object_tail(struct drm_gem_object *obj)
4350{
4351 struct drm_device *dev = obj->dev;
4352 drm_i915_private_t *dev_priv = dev->dev_private;
4353 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
4354 int ret;
4355
4356 ret = i915_gem_object_unbind(obj);
4357 if (ret == -ERESTARTSYS) {
4358 list_move(&obj_priv->list,
4359 &dev_priv->mm.deferred_free_list);
4360 return;
4361 }
4362
4363 if (obj_priv->mmap_offset)
4364 i915_gem_free_mmap_offset(obj);
4365
4366 drm_gem_object_release(obj);
4367
4368 kfree(obj_priv->page_cpu_valid);
4369 kfree(obj_priv->bit_17);
4370 kfree(obj_priv);
4371}
4372
Eric Anholt673a3942008-07-30 12:06:12 -07004373void i915_gem_free_object(struct drm_gem_object *obj)
4374{
Jesse Barnesde151cf2008-11-12 10:03:55 -08004375 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01004376 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004377
Chris Wilson1c5d22f2009-08-25 11:15:50 +01004378 trace_i915_gem_object_destroy(obj);
4379
Eric Anholt673a3942008-07-30 12:06:12 -07004380 while (obj_priv->pin_count > 0)
4381 i915_gem_object_unpin(obj);
4382
Dave Airlie71acb5e2008-12-30 20:31:46 +10004383 if (obj_priv->phys_obj)
4384 i915_gem_detach_phys_object(dev, obj);
4385
Chris Wilsonbe726152010-07-23 23:18:50 +01004386 i915_gem_free_object_tail(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004387}
4388
Jesse Barnes5669fca2009-02-17 15:13:31 -08004389int
Eric Anholt673a3942008-07-30 12:06:12 -07004390i915_gem_idle(struct drm_device *dev)
4391{
4392 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson29105cc2010-01-07 10:39:13 +00004393 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004394
Keith Packard6dbe2772008-10-14 21:41:13 -07004395 mutex_lock(&dev->struct_mutex);
4396
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004397 if (dev_priv->mm.suspended ||
Zou Nan haid1b851f2010-05-21 09:08:57 +08004398 (dev_priv->render_ring.gem_object == NULL) ||
4399 (HAS_BSD(dev) &&
4400 dev_priv->bsd_ring.gem_object == NULL)) {
Keith Packard6dbe2772008-10-14 21:41:13 -07004401 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004402 return 0;
Keith Packard6dbe2772008-10-14 21:41:13 -07004403 }
Eric Anholt673a3942008-07-30 12:06:12 -07004404
Chris Wilson29105cc2010-01-07 10:39:13 +00004405 ret = i915_gpu_idle(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004406 if (ret) {
4407 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004408 return ret;
Keith Packard6dbe2772008-10-14 21:41:13 -07004409 }
Eric Anholt673a3942008-07-30 12:06:12 -07004410
Chris Wilson29105cc2010-01-07 10:39:13 +00004411 /* Under UMS, be paranoid and evict. */
4412 if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
Chris Wilsonb47eb4a2010-08-07 11:01:23 +01004413 ret = i915_gem_evict_inactive(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00004414 if (ret) {
4415 mutex_unlock(&dev->struct_mutex);
4416 return ret;
4417 }
4418 }
4419
4420 /* Hack! Don't let anybody do execbuf while we don't control the chip.
4421 * We need to replace this with a semaphore, or something.
4422 * And not confound mm.suspended!
4423 */
4424 dev_priv->mm.suspended = 1;
4425 del_timer(&dev_priv->hangcheck_timer);
4426
4427 i915_kernel_lost_context(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004428 i915_gem_cleanup_ringbuffer(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00004429
Keith Packard6dbe2772008-10-14 21:41:13 -07004430 mutex_unlock(&dev->struct_mutex);
4431
Chris Wilson29105cc2010-01-07 10:39:13 +00004432 /* Cancel the retire work handler, which should be idle now. */
4433 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
4434
Eric Anholt673a3942008-07-30 12:06:12 -07004435 return 0;
4436}
4437
Jesse Barnese552eb72010-04-21 11:39:23 -07004438/*
4439 * 965+ support PIPE_CONTROL commands, which provide finer grained control
4440 * over cache flushing.
4441 */
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004442static int
Jesse Barnese552eb72010-04-21 11:39:23 -07004443i915_gem_init_pipe_control(struct drm_device *dev)
4444{
4445 drm_i915_private_t *dev_priv = dev->dev_private;
4446 struct drm_gem_object *obj;
4447 struct drm_i915_gem_object *obj_priv;
4448 int ret;
4449
Eric Anholt34dc4d42010-05-07 14:30:03 -07004450 obj = i915_gem_alloc_object(dev, 4096);
Jesse Barnese552eb72010-04-21 11:39:23 -07004451 if (obj == NULL) {
4452 DRM_ERROR("Failed to allocate seqno page\n");
4453 ret = -ENOMEM;
4454 goto err;
4455 }
4456 obj_priv = to_intel_bo(obj);
4457 obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
4458
4459 ret = i915_gem_object_pin(obj, 4096);
4460 if (ret)
4461 goto err_unref;
4462
4463 dev_priv->seqno_gfx_addr = obj_priv->gtt_offset;
4464 dev_priv->seqno_page = kmap(obj_priv->pages[0]);
4465 if (dev_priv->seqno_page == NULL)
4466 goto err_unpin;
4467
4468 dev_priv->seqno_obj = obj;
4469 memset(dev_priv->seqno_page, 0, PAGE_SIZE);
4470
4471 return 0;
4472
4473err_unpin:
4474 i915_gem_object_unpin(obj);
4475err_unref:
4476 drm_gem_object_unreference(obj);
4477err:
4478 return ret;
4479}
4480
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004481
4482static void
Jesse Barnese552eb72010-04-21 11:39:23 -07004483i915_gem_cleanup_pipe_control(struct drm_device *dev)
4484{
4485 drm_i915_private_t *dev_priv = dev->dev_private;
4486 struct drm_gem_object *obj;
4487 struct drm_i915_gem_object *obj_priv;
4488
4489 obj = dev_priv->seqno_obj;
4490 obj_priv = to_intel_bo(obj);
4491 kunmap(obj_priv->pages[0]);
4492 i915_gem_object_unpin(obj);
4493 drm_gem_object_unreference(obj);
4494 dev_priv->seqno_obj = NULL;
4495
4496 dev_priv->seqno_page = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07004497}
4498
Eric Anholt673a3942008-07-30 12:06:12 -07004499int
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004500i915_gem_init_ringbuffer(struct drm_device *dev)
4501{
4502 drm_i915_private_t *dev_priv = dev->dev_private;
4503 int ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01004504
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004505 dev_priv->render_ring = render_ring;
Chris Wilson68f95ba2010-05-27 13:18:22 +01004506
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004507 if (!I915_NEED_GFX_HWS(dev)) {
4508 dev_priv->render_ring.status_page.page_addr
4509 = dev_priv->status_page_dmah->vaddr;
4510 memset(dev_priv->render_ring.status_page.page_addr,
4511 0, PAGE_SIZE);
4512 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004513
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004514 if (HAS_PIPE_CONTROL(dev)) {
4515 ret = i915_gem_init_pipe_control(dev);
4516 if (ret)
4517 return ret;
4518 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004519
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004520 ret = intel_init_ring_buffer(dev, &dev_priv->render_ring);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004521 if (ret)
4522 goto cleanup_pipe_control;
4523
4524 if (HAS_BSD(dev)) {
Zou Nan haid1b851f2010-05-21 09:08:57 +08004525 dev_priv->bsd_ring = bsd_ring;
4526 ret = intel_init_ring_buffer(dev, &dev_priv->bsd_ring);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004527 if (ret)
4528 goto cleanup_render_ring;
Zou Nan haid1b851f2010-05-21 09:08:57 +08004529 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004530
Chris Wilson6f392d52010-08-07 11:01:22 +01004531 dev_priv->next_seqno = 1;
4532
Chris Wilson68f95ba2010-05-27 13:18:22 +01004533 return 0;
4534
4535cleanup_render_ring:
4536 intel_cleanup_ring_buffer(dev, &dev_priv->render_ring);
4537cleanup_pipe_control:
4538 if (HAS_PIPE_CONTROL(dev))
4539 i915_gem_cleanup_pipe_control(dev);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004540 return ret;
4541}
4542
4543void
4544i915_gem_cleanup_ringbuffer(struct drm_device *dev)
4545{
4546 drm_i915_private_t *dev_priv = dev->dev_private;
4547
4548 intel_cleanup_ring_buffer(dev, &dev_priv->render_ring);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004549 if (HAS_BSD(dev))
4550 intel_cleanup_ring_buffer(dev, &dev_priv->bsd_ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004551 if (HAS_PIPE_CONTROL(dev))
4552 i915_gem_cleanup_pipe_control(dev);
4553}
4554
4555int
Eric Anholt673a3942008-07-30 12:06:12 -07004556i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
4557 struct drm_file *file_priv)
4558{
4559 drm_i915_private_t *dev_priv = dev->dev_private;
4560 int ret;
4561
Jesse Barnes79e53942008-11-07 14:24:08 -08004562 if (drm_core_check_feature(dev, DRIVER_MODESET))
4563 return 0;
4564
Ben Gamariba1234d2009-09-14 17:48:47 -04004565 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07004566 DRM_ERROR("Reenabling wedged hardware, good luck\n");
Ben Gamariba1234d2009-09-14 17:48:47 -04004567 atomic_set(&dev_priv->mm.wedged, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004568 }
4569
Eric Anholt673a3942008-07-30 12:06:12 -07004570 mutex_lock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004571 dev_priv->mm.suspended = 0;
4572
4573 ret = i915_gem_init_ringbuffer(dev);
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004574 if (ret != 0) {
4575 mutex_unlock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004576 return ret;
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004577 }
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004578
Carl Worth5e118f42009-03-20 11:54:25 -07004579 spin_lock(&dev_priv->mm.active_list_lock);
Zou Nan hai852835f2010-05-21 09:08:56 +08004580 BUG_ON(!list_empty(&dev_priv->render_ring.active_list));
Zou Nan haid1b851f2010-05-21 09:08:57 +08004581 BUG_ON(HAS_BSD(dev) && !list_empty(&dev_priv->bsd_ring.active_list));
Carl Worth5e118f42009-03-20 11:54:25 -07004582 spin_unlock(&dev_priv->mm.active_list_lock);
4583
Eric Anholt673a3942008-07-30 12:06:12 -07004584 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
4585 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
Zou Nan hai852835f2010-05-21 09:08:56 +08004586 BUG_ON(!list_empty(&dev_priv->render_ring.request_list));
Zou Nan haid1b851f2010-05-21 09:08:57 +08004587 BUG_ON(HAS_BSD(dev) && !list_empty(&dev_priv->bsd_ring.request_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004588 mutex_unlock(&dev->struct_mutex);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004589
Chris Wilson5f353082010-06-07 14:03:03 +01004590 ret = drm_irq_install(dev);
4591 if (ret)
4592 goto cleanup_ringbuffer;
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004593
Eric Anholt673a3942008-07-30 12:06:12 -07004594 return 0;
Chris Wilson5f353082010-06-07 14:03:03 +01004595
4596cleanup_ringbuffer:
4597 mutex_lock(&dev->struct_mutex);
4598 i915_gem_cleanup_ringbuffer(dev);
4599 dev_priv->mm.suspended = 1;
4600 mutex_unlock(&dev->struct_mutex);
4601
4602 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004603}
4604
4605int
4606i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
4607 struct drm_file *file_priv)
4608{
Jesse Barnes79e53942008-11-07 14:24:08 -08004609 if (drm_core_check_feature(dev, DRIVER_MODESET))
4610 return 0;
4611
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004612 drm_irq_uninstall(dev);
Linus Torvaldse6890f62009-09-08 17:09:24 -07004613 return i915_gem_idle(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004614}
4615
4616void
4617i915_gem_lastclose(struct drm_device *dev)
4618{
4619 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004620
Eric Anholte806b492009-01-22 09:56:58 -08004621 if (drm_core_check_feature(dev, DRIVER_MODESET))
4622 return;
4623
Keith Packard6dbe2772008-10-14 21:41:13 -07004624 ret = i915_gem_idle(dev);
4625 if (ret)
4626 DRM_ERROR("failed to idle hardware: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07004627}
4628
4629void
4630i915_gem_load(struct drm_device *dev)
4631{
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004632 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07004633 drm_i915_private_t *dev_priv = dev->dev_private;
4634
Carl Worth5e118f42009-03-20 11:54:25 -07004635 spin_lock_init(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07004636 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
Daniel Vetter99fcb762010-02-07 16:20:18 +01004637 INIT_LIST_HEAD(&dev_priv->mm.gpu_write_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004638 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07004639 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Chris Wilsonbe726152010-07-23 23:18:50 +01004640 INIT_LIST_HEAD(&dev_priv->mm.deferred_free_list);
Zou Nan hai852835f2010-05-21 09:08:56 +08004641 INIT_LIST_HEAD(&dev_priv->render_ring.active_list);
4642 INIT_LIST_HEAD(&dev_priv->render_ring.request_list);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004643 if (HAS_BSD(dev)) {
4644 INIT_LIST_HEAD(&dev_priv->bsd_ring.active_list);
4645 INIT_LIST_HEAD(&dev_priv->bsd_ring.request_list);
4646 }
Daniel Vetter007cc8a2010-04-28 11:02:31 +02004647 for (i = 0; i < 16; i++)
4648 INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004649 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
4650 i915_gem_retire_work_handler);
Chris Wilson31169712009-09-14 16:50:28 +01004651 spin_lock(&shrink_list_lock);
4652 list_add(&dev_priv->mm.shrink_list, &shrink_list);
4653 spin_unlock(&shrink_list_lock);
4654
Dave Airlie94400122010-07-20 13:15:31 +10004655 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
4656 if (IS_GEN3(dev)) {
4657 u32 tmp = I915_READ(MI_ARB_STATE);
4658 if (!(tmp & MI_ARB_C3_LP_WRITE_ENABLE)) {
4659 /* arb state is a masked write, so set bit + bit in mask */
4660 tmp = MI_ARB_C3_LP_WRITE_ENABLE | (MI_ARB_C3_LP_WRITE_ENABLE << MI_ARB_MASK_SHIFT);
4661 I915_WRITE(MI_ARB_STATE, tmp);
4662 }
4663 }
4664
Jesse Barnesde151cf2008-11-12 10:03:55 -08004665 /* Old X drivers will take 0-2 for front, back, depth buffers */
Eric Anholtb397c832010-01-26 09:43:10 -08004666 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4667 dev_priv->fence_reg_start = 3;
Jesse Barnesde151cf2008-11-12 10:03:55 -08004668
Jesse Barnes0f973f22009-01-26 17:10:45 -08004669 if (IS_I965G(dev) || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08004670 dev_priv->num_fence_regs = 16;
4671 else
4672 dev_priv->num_fence_regs = 8;
4673
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004674 /* Initialize fence registers to zero */
4675 if (IS_I965G(dev)) {
4676 for (i = 0; i < 16; i++)
4677 I915_WRITE64(FENCE_REG_965_0 + (i * 8), 0);
4678 } else {
4679 for (i = 0; i < 8; i++)
4680 I915_WRITE(FENCE_REG_830_0 + (i * 4), 0);
4681 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4682 for (i = 0; i < 8; i++)
4683 I915_WRITE(FENCE_REG_945_8 + (i * 4), 0);
4684 }
Eric Anholt673a3942008-07-30 12:06:12 -07004685 i915_gem_detect_bit_6_swizzle(dev);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05004686 init_waitqueue_head(&dev_priv->pending_flip_queue);
Eric Anholt673a3942008-07-30 12:06:12 -07004687}
Dave Airlie71acb5e2008-12-30 20:31:46 +10004688
4689/*
4690 * Create a physically contiguous memory object for this object
4691 * e.g. for cursor + overlay regs
4692 */
4693int i915_gem_init_phys_object(struct drm_device *dev,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004694 int id, int size, int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004695{
4696 drm_i915_private_t *dev_priv = dev->dev_private;
4697 struct drm_i915_gem_phys_object *phys_obj;
4698 int ret;
4699
4700 if (dev_priv->mm.phys_objs[id - 1] || !size)
4701 return 0;
4702
Eric Anholt9a298b22009-03-24 12:23:04 -07004703 phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004704 if (!phys_obj)
4705 return -ENOMEM;
4706
4707 phys_obj->id = id;
4708
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004709 phys_obj->handle = drm_pci_alloc(dev, size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004710 if (!phys_obj->handle) {
4711 ret = -ENOMEM;
4712 goto kfree_obj;
4713 }
4714#ifdef CONFIG_X86
4715 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4716#endif
4717
4718 dev_priv->mm.phys_objs[id - 1] = phys_obj;
4719
4720 return 0;
4721kfree_obj:
Eric Anholt9a298b22009-03-24 12:23:04 -07004722 kfree(phys_obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004723 return ret;
4724}
4725
4726void i915_gem_free_phys_object(struct drm_device *dev, int id)
4727{
4728 drm_i915_private_t *dev_priv = dev->dev_private;
4729 struct drm_i915_gem_phys_object *phys_obj;
4730
4731 if (!dev_priv->mm.phys_objs[id - 1])
4732 return;
4733
4734 phys_obj = dev_priv->mm.phys_objs[id - 1];
4735 if (phys_obj->cur_obj) {
4736 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
4737 }
4738
4739#ifdef CONFIG_X86
4740 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4741#endif
4742 drm_pci_free(dev, phys_obj->handle);
4743 kfree(phys_obj);
4744 dev_priv->mm.phys_objs[id - 1] = NULL;
4745}
4746
4747void i915_gem_free_all_phys_object(struct drm_device *dev)
4748{
4749 int i;
4750
Dave Airlie260883c2009-01-22 17:58:49 +10004751 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004752 i915_gem_free_phys_object(dev, i);
4753}
4754
4755void i915_gem_detach_phys_object(struct drm_device *dev,
4756 struct drm_gem_object *obj)
4757{
4758 struct drm_i915_gem_object *obj_priv;
4759 int i;
4760 int ret;
4761 int page_count;
4762
Daniel Vetter23010e42010-03-08 13:35:02 +01004763 obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004764 if (!obj_priv->phys_obj)
4765 return;
4766
Chris Wilson4bdadb92010-01-27 13:36:32 +00004767 ret = i915_gem_object_get_pages(obj, 0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004768 if (ret)
4769 goto out;
4770
4771 page_count = obj->size / PAGE_SIZE;
4772
4773 for (i = 0; i < page_count; i++) {
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -07004774 char *dst = kmap_atomic(obj_priv->pages[i]);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004775 char *src = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4776
4777 memcpy(dst, src, PAGE_SIZE);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -07004778 kunmap_atomic(dst);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004779 }
Eric Anholt856fa192009-03-19 14:10:50 -07004780 drm_clflush_pages(obj_priv->pages, page_count);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004781 drm_agp_chipset_flush(dev);
Chris Wilsond78b47b2009-06-17 21:52:49 +01004782
4783 i915_gem_object_put_pages(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004784out:
4785 obj_priv->phys_obj->cur_obj = NULL;
4786 obj_priv->phys_obj = NULL;
4787}
4788
4789int
4790i915_gem_attach_phys_object(struct drm_device *dev,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004791 struct drm_gem_object *obj,
4792 int id,
4793 int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004794{
4795 drm_i915_private_t *dev_priv = dev->dev_private;
4796 struct drm_i915_gem_object *obj_priv;
4797 int ret = 0;
4798 int page_count;
4799 int i;
4800
4801 if (id > I915_MAX_PHYS_OBJECT)
4802 return -EINVAL;
4803
Daniel Vetter23010e42010-03-08 13:35:02 +01004804 obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004805
4806 if (obj_priv->phys_obj) {
4807 if (obj_priv->phys_obj->id == id)
4808 return 0;
4809 i915_gem_detach_phys_object(dev, obj);
4810 }
4811
Dave Airlie71acb5e2008-12-30 20:31:46 +10004812 /* create a new object */
4813 if (!dev_priv->mm.phys_objs[id - 1]) {
4814 ret = i915_gem_init_phys_object(dev, id,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004815 obj->size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004816 if (ret) {
Linus Torvaldsaeb565d2009-01-26 10:01:53 -08004817 DRM_ERROR("failed to init phys object %d size: %zu\n", id, obj->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004818 goto out;
4819 }
4820 }
4821
4822 /* bind to the object */
4823 obj_priv->phys_obj = dev_priv->mm.phys_objs[id - 1];
4824 obj_priv->phys_obj->cur_obj = obj;
4825
Chris Wilson4bdadb92010-01-27 13:36:32 +00004826 ret = i915_gem_object_get_pages(obj, 0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004827 if (ret) {
4828 DRM_ERROR("failed to get page list\n");
4829 goto out;
4830 }
4831
4832 page_count = obj->size / PAGE_SIZE;
4833
4834 for (i = 0; i < page_count; i++) {
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -07004835 char *src = kmap_atomic(obj_priv->pages[i]);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004836 char *dst = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4837
4838 memcpy(dst, src, PAGE_SIZE);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -07004839 kunmap_atomic(src);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004840 }
4841
Chris Wilsond78b47b2009-06-17 21:52:49 +01004842 i915_gem_object_put_pages(obj);
4843
Dave Airlie71acb5e2008-12-30 20:31:46 +10004844 return 0;
4845out:
4846 return ret;
4847}
4848
4849static int
4850i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
4851 struct drm_i915_gem_pwrite *args,
4852 struct drm_file *file_priv)
4853{
Daniel Vetter23010e42010-03-08 13:35:02 +01004854 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004855 void *obj_addr;
4856 int ret;
4857 char __user *user_data;
4858
4859 user_data = (char __user *) (uintptr_t) args->data_ptr;
4860 obj_addr = obj_priv->phys_obj->handle->vaddr + args->offset;
4861
Zhao Yakui44d98a62009-10-09 11:39:40 +08004862 DRM_DEBUG_DRIVER("obj_addr %p, %lld\n", obj_addr, args->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004863 ret = copy_from_user(obj_addr, user_data, args->size);
4864 if (ret)
4865 return -EFAULT;
4866
4867 drm_agp_chipset_flush(dev);
4868 return 0;
4869}
Eric Anholtb9624422009-06-03 07:27:35 +00004870
4871void i915_gem_release(struct drm_device * dev, struct drm_file *file_priv)
4872{
4873 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
4874
4875 /* Clean up our request list when the client is going away, so that
4876 * later retire_requests won't dereference our soon-to-be-gone
4877 * file_priv.
4878 */
4879 mutex_lock(&dev->struct_mutex);
4880 while (!list_empty(&i915_file_priv->mm.request_list))
4881 list_del_init(i915_file_priv->mm.request_list.next);
4882 mutex_unlock(&dev->struct_mutex);
4883}
Chris Wilson31169712009-09-14 16:50:28 +01004884
Chris Wilson31169712009-09-14 16:50:28 +01004885static int
Chris Wilson1637ef42010-04-20 17:10:35 +01004886i915_gpu_is_active(struct drm_device *dev)
4887{
4888 drm_i915_private_t *dev_priv = dev->dev_private;
4889 int lists_empty;
4890
4891 spin_lock(&dev_priv->mm.active_list_lock);
4892 lists_empty = list_empty(&dev_priv->mm.flushing_list) &&
Zou Nan hai852835f2010-05-21 09:08:56 +08004893 list_empty(&dev_priv->render_ring.active_list);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004894 if (HAS_BSD(dev))
4895 lists_empty &= list_empty(&dev_priv->bsd_ring.active_list);
Chris Wilson1637ef42010-04-20 17:10:35 +01004896 spin_unlock(&dev_priv->mm.active_list_lock);
4897
4898 return !lists_empty;
4899}
4900
4901static int
Dave Chinner7f8275d2010-07-19 14:56:17 +10004902i915_gem_shrink(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask)
Chris Wilson31169712009-09-14 16:50:28 +01004903{
4904 drm_i915_private_t *dev_priv, *next_dev;
4905 struct drm_i915_gem_object *obj_priv, *next_obj;
4906 int cnt = 0;
4907 int would_deadlock = 1;
4908
4909 /* "fast-path" to count number of available objects */
4910 if (nr_to_scan == 0) {
4911 spin_lock(&shrink_list_lock);
4912 list_for_each_entry(dev_priv, &shrink_list, mm.shrink_list) {
4913 struct drm_device *dev = dev_priv->dev;
4914
4915 if (mutex_trylock(&dev->struct_mutex)) {
4916 list_for_each_entry(obj_priv,
4917 &dev_priv->mm.inactive_list,
4918 list)
4919 cnt++;
4920 mutex_unlock(&dev->struct_mutex);
4921 }
4922 }
4923 spin_unlock(&shrink_list_lock);
4924
4925 return (cnt / 100) * sysctl_vfs_cache_pressure;
4926 }
4927
4928 spin_lock(&shrink_list_lock);
4929
Chris Wilson1637ef42010-04-20 17:10:35 +01004930rescan:
Chris Wilson31169712009-09-14 16:50:28 +01004931 /* first scan for clean buffers */
4932 list_for_each_entry_safe(dev_priv, next_dev,
4933 &shrink_list, mm.shrink_list) {
4934 struct drm_device *dev = dev_priv->dev;
4935
4936 if (! mutex_trylock(&dev->struct_mutex))
4937 continue;
4938
4939 spin_unlock(&shrink_list_lock);
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01004940 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004941
Chris Wilson31169712009-09-14 16:50:28 +01004942 list_for_each_entry_safe(obj_priv, next_obj,
4943 &dev_priv->mm.inactive_list,
4944 list) {
4945 if (i915_gem_object_is_purgeable(obj_priv)) {
Daniel Vettera8089e82010-04-09 19:05:09 +00004946 i915_gem_object_unbind(&obj_priv->base);
Chris Wilson31169712009-09-14 16:50:28 +01004947 if (--nr_to_scan <= 0)
4948 break;
4949 }
4950 }
4951
4952 spin_lock(&shrink_list_lock);
4953 mutex_unlock(&dev->struct_mutex);
4954
Chris Wilson963b4832009-09-20 23:03:54 +01004955 would_deadlock = 0;
4956
Chris Wilson31169712009-09-14 16:50:28 +01004957 if (nr_to_scan <= 0)
4958 break;
4959 }
4960
4961 /* second pass, evict/count anything still on the inactive list */
4962 list_for_each_entry_safe(dev_priv, next_dev,
4963 &shrink_list, mm.shrink_list) {
4964 struct drm_device *dev = dev_priv->dev;
4965
4966 if (! mutex_trylock(&dev->struct_mutex))
4967 continue;
4968
4969 spin_unlock(&shrink_list_lock);
4970
4971 list_for_each_entry_safe(obj_priv, next_obj,
4972 &dev_priv->mm.inactive_list,
4973 list) {
4974 if (nr_to_scan > 0) {
Daniel Vettera8089e82010-04-09 19:05:09 +00004975 i915_gem_object_unbind(&obj_priv->base);
Chris Wilson31169712009-09-14 16:50:28 +01004976 nr_to_scan--;
4977 } else
4978 cnt++;
4979 }
4980
4981 spin_lock(&shrink_list_lock);
4982 mutex_unlock(&dev->struct_mutex);
4983
4984 would_deadlock = 0;
4985 }
4986
Chris Wilson1637ef42010-04-20 17:10:35 +01004987 if (nr_to_scan) {
4988 int active = 0;
4989
4990 /*
4991 * We are desperate for pages, so as a last resort, wait
4992 * for the GPU to finish and discard whatever we can.
4993 * This has a dramatic impact to reduce the number of
4994 * OOM-killer events whilst running the GPU aggressively.
4995 */
4996 list_for_each_entry(dev_priv, &shrink_list, mm.shrink_list) {
4997 struct drm_device *dev = dev_priv->dev;
4998
4999 if (!mutex_trylock(&dev->struct_mutex))
5000 continue;
5001
5002 spin_unlock(&shrink_list_lock);
5003
5004 if (i915_gpu_is_active(dev)) {
5005 i915_gpu_idle(dev);
5006 active++;
5007 }
5008
5009 spin_lock(&shrink_list_lock);
5010 mutex_unlock(&dev->struct_mutex);
5011 }
5012
5013 if (active)
5014 goto rescan;
5015 }
5016
Chris Wilson31169712009-09-14 16:50:28 +01005017 spin_unlock(&shrink_list_lock);
5018
5019 if (would_deadlock)
5020 return -1;
5021 else if (cnt > 0)
5022 return (cnt / 100) * sysctl_vfs_cache_pressure;
5023 else
5024 return 0;
5025}
5026
5027static struct shrinker shrinker = {
5028 .shrink = i915_gem_shrink,
5029 .seeks = DEFAULT_SEEKS,
5030};
5031
5032__init void
5033i915_gem_shrinker_init(void)
5034{
5035 register_shrinker(&shrinker);
5036}
5037
5038__exit void
5039i915_gem_shrinker_exit(void)
5040{
5041 unregister_shrinker(&shrinker);
5042}