blob: 4cdf74264ee8062e5f436c21a6fd08eb51176c89 [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
158 vaddr = kmap_atomic(pages[page_base >> PAGE_SHIFT], KM_USER0);
159 if (vaddr == NULL)
160 return -ENOMEM;
Florian Mickler2bc43b52009-04-06 22:55:41 +0200161 unwritten = __copy_to_user_inatomic(data, vaddr + page_offset, length);
Eric Anholteb014592009-03-10 11:44:52 -0700162 kunmap_atomic(vaddr, KM_USER0);
163
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
472 /* Bounds check source.
473 *
474 * XXX: This could use review for overflow issues...
475 */
476 if (args->offset > obj->size || args->size > obj->size ||
477 args->offset + args->size > obj->size) {
Luca Barbieribc9025b2010-02-09 05:49:12 +0000478 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700479 return -EINVAL;
480 }
481
Eric Anholt280b7132009-03-12 16:56:27 -0700482 if (i915_gem_object_needs_bit17_swizzle(obj)) {
Eric Anholteb014592009-03-10 11:44:52 -0700483 ret = i915_gem_shmem_pread_slow(dev, obj, args, file_priv);
Eric Anholt280b7132009-03-12 16:56:27 -0700484 } else {
485 ret = i915_gem_shmem_pread_fast(dev, obj, args, file_priv);
486 if (ret != 0)
487 ret = i915_gem_shmem_pread_slow(dev, obj, args,
488 file_priv);
489 }
Eric Anholt673a3942008-07-30 12:06:12 -0700490
Luca Barbieribc9025b2010-02-09 05:49:12 +0000491 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700492
Eric Anholteb014592009-03-10 11:44:52 -0700493 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700494}
495
Keith Packard0839ccb2008-10-30 19:38:48 -0700496/* This is the fast write path which cannot handle
497 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700498 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700499
Keith Packard0839ccb2008-10-30 19:38:48 -0700500static inline int
501fast_user_write(struct io_mapping *mapping,
502 loff_t page_base, int page_offset,
503 char __user *user_data,
504 int length)
505{
506 char *vaddr_atomic;
507 unsigned long unwritten;
508
Chris Wilsonfca3ec02010-08-04 14:34:24 +0100509 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base, KM_USER0);
Keith Packard0839ccb2008-10-30 19:38:48 -0700510 unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
511 user_data, length);
Chris Wilsonfca3ec02010-08-04 14:34:24 +0100512 io_mapping_unmap_atomic(vaddr_atomic, KM_USER0);
Keith Packard0839ccb2008-10-30 19:38:48 -0700513 if (unwritten)
514 return -EFAULT;
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700515 return 0;
Keith Packard0839ccb2008-10-30 19:38:48 -0700516}
517
518/* Here's the write path which can sleep for
519 * page faults
520 */
521
Chris Wilsonab34c222010-05-27 14:15:35 +0100522static inline void
Eric Anholt3de09aa2009-03-09 09:42:23 -0700523slow_kernel_write(struct io_mapping *mapping,
524 loff_t gtt_base, int gtt_offset,
525 struct page *user_page, int user_offset,
526 int length)
Keith Packard0839ccb2008-10-30 19:38:48 -0700527{
Chris Wilsonab34c222010-05-27 14:15:35 +0100528 char __iomem *dst_vaddr;
529 char *src_vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -0700530
Chris Wilsonab34c222010-05-27 14:15:35 +0100531 dst_vaddr = io_mapping_map_wc(mapping, gtt_base);
532 src_vaddr = kmap(user_page);
533
534 memcpy_toio(dst_vaddr + gtt_offset,
535 src_vaddr + user_offset,
536 length);
537
538 kunmap(user_page);
539 io_mapping_unmap(dst_vaddr);
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700540}
541
Eric Anholt40123c12009-03-09 13:42:30 -0700542static inline int
543fast_shmem_write(struct page **pages,
544 loff_t page_base, int page_offset,
545 char __user *data,
546 int length)
547{
548 char __iomem *vaddr;
Dave Airlied0088772009-03-28 20:29:48 -0400549 unsigned long unwritten;
Eric Anholt40123c12009-03-09 13:42:30 -0700550
551 vaddr = kmap_atomic(pages[page_base >> PAGE_SHIFT], KM_USER0);
552 if (vaddr == NULL)
553 return -ENOMEM;
Dave Airlied0088772009-03-28 20:29:48 -0400554 unwritten = __copy_from_user_inatomic(vaddr + page_offset, data, length);
Eric Anholt40123c12009-03-09 13:42:30 -0700555 kunmap_atomic(vaddr, KM_USER0);
556
Dave Airlied0088772009-03-28 20:29:48 -0400557 if (unwritten)
558 return -EFAULT;
Eric Anholt40123c12009-03-09 13:42:30 -0700559 return 0;
560}
561
Eric Anholt3de09aa2009-03-09 09:42:23 -0700562/**
563 * This is the fast pwrite path, where we copy the data directly from the
564 * user into the GTT, uncached.
565 */
Eric Anholt673a3942008-07-30 12:06:12 -0700566static int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700567i915_gem_gtt_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
568 struct drm_i915_gem_pwrite *args,
569 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700570{
Daniel Vetter23010e42010-03-08 13:35:02 +0100571 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Keith Packard0839ccb2008-10-30 19:38:48 -0700572 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700573 ssize_t remain;
Keith Packard0839ccb2008-10-30 19:38:48 -0700574 loff_t offset, page_base;
Eric Anholt673a3942008-07-30 12:06:12 -0700575 char __user *user_data;
Keith Packard0839ccb2008-10-30 19:38:48 -0700576 int page_offset, page_length;
577 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700578
579 user_data = (char __user *) (uintptr_t) args->data_ptr;
580 remain = args->size;
581 if (!access_ok(VERIFY_READ, user_data, remain))
582 return -EFAULT;
583
584
585 mutex_lock(&dev->struct_mutex);
586 ret = i915_gem_object_pin(obj, 0);
587 if (ret) {
588 mutex_unlock(&dev->struct_mutex);
589 return ret;
590 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800591 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
Eric Anholt673a3942008-07-30 12:06:12 -0700592 if (ret)
593 goto fail;
594
Daniel Vetter23010e42010-03-08 13:35:02 +0100595 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700596 offset = obj_priv->gtt_offset + args->offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700597
598 while (remain > 0) {
599 /* Operation in this page
600 *
Keith Packard0839ccb2008-10-30 19:38:48 -0700601 * page_base = page offset within aperture
602 * page_offset = offset within page
603 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -0700604 */
Keith Packard0839ccb2008-10-30 19:38:48 -0700605 page_base = (offset & ~(PAGE_SIZE-1));
606 page_offset = offset & (PAGE_SIZE-1);
607 page_length = remain;
608 if ((page_offset + remain) > PAGE_SIZE)
609 page_length = PAGE_SIZE - page_offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700610
Keith Packard0839ccb2008-10-30 19:38:48 -0700611 ret = fast_user_write (dev_priv->mm.gtt_mapping, page_base,
612 page_offset, user_data, page_length);
Eric Anholt673a3942008-07-30 12:06:12 -0700613
Keith Packard0839ccb2008-10-30 19:38:48 -0700614 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -0700615 * source page isn't available. Return the error and we'll
616 * retry in the slow path.
Keith Packard0839ccb2008-10-30 19:38:48 -0700617 */
Eric Anholt3de09aa2009-03-09 09:42:23 -0700618 if (ret)
619 goto fail;
Eric Anholt673a3942008-07-30 12:06:12 -0700620
Keith Packard0839ccb2008-10-30 19:38:48 -0700621 remain -= page_length;
622 user_data += page_length;
623 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700624 }
Eric Anholt673a3942008-07-30 12:06:12 -0700625
626fail:
627 i915_gem_object_unpin(obj);
628 mutex_unlock(&dev->struct_mutex);
629
630 return ret;
631}
632
Eric Anholt3de09aa2009-03-09 09:42:23 -0700633/**
634 * This is the fallback GTT pwrite path, which uses get_user_pages to pin
635 * the memory and maps it using kmap_atomic for copying.
636 *
637 * This code resulted in x11perf -rgb10text consuming about 10% more CPU
638 * than using i915_gem_gtt_pwrite_fast on a G45 (32-bit).
639 */
Eric Anholt3043c602008-10-02 12:24:47 -0700640static int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700641i915_gem_gtt_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
642 struct drm_i915_gem_pwrite *args,
643 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700644{
Daniel Vetter23010e42010-03-08 13:35:02 +0100645 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700646 drm_i915_private_t *dev_priv = dev->dev_private;
647 ssize_t remain;
648 loff_t gtt_page_base, offset;
649 loff_t first_data_page, last_data_page, num_pages;
650 loff_t pinned_pages, i;
651 struct page **user_pages;
652 struct mm_struct *mm = current->mm;
653 int gtt_page_offset, data_page_offset, data_page_index, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700654 int ret;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700655 uint64_t data_ptr = args->data_ptr;
656
657 remain = args->size;
658
659 /* Pin the user pages containing the data. We can't fault while
660 * holding the struct mutex, and all of the pwrite implementations
661 * want to hold it while dereferencing the user data.
662 */
663 first_data_page = data_ptr / PAGE_SIZE;
664 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
665 num_pages = last_data_page - first_data_page + 1;
666
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700667 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
Eric Anholt3de09aa2009-03-09 09:42:23 -0700668 if (user_pages == NULL)
669 return -ENOMEM;
670
671 down_read(&mm->mmap_sem);
672 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
673 num_pages, 0, 0, user_pages, NULL);
674 up_read(&mm->mmap_sem);
675 if (pinned_pages < num_pages) {
676 ret = -EFAULT;
677 goto out_unpin_pages;
678 }
679
680 mutex_lock(&dev->struct_mutex);
681 ret = i915_gem_object_pin(obj, 0);
682 if (ret)
683 goto out_unlock;
684
685 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
686 if (ret)
687 goto out_unpin_object;
688
Daniel Vetter23010e42010-03-08 13:35:02 +0100689 obj_priv = to_intel_bo(obj);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700690 offset = obj_priv->gtt_offset + args->offset;
691
692 while (remain > 0) {
693 /* Operation in this page
694 *
695 * gtt_page_base = page offset within aperture
696 * gtt_page_offset = offset within page in aperture
697 * data_page_index = page number in get_user_pages return
698 * data_page_offset = offset with data_page_index page.
699 * page_length = bytes to copy for this page
700 */
701 gtt_page_base = offset & PAGE_MASK;
702 gtt_page_offset = offset & ~PAGE_MASK;
703 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
704 data_page_offset = data_ptr & ~PAGE_MASK;
705
706 page_length = remain;
707 if ((gtt_page_offset + page_length) > PAGE_SIZE)
708 page_length = PAGE_SIZE - gtt_page_offset;
709 if ((data_page_offset + page_length) > PAGE_SIZE)
710 page_length = PAGE_SIZE - data_page_offset;
711
Chris Wilsonab34c222010-05-27 14:15:35 +0100712 slow_kernel_write(dev_priv->mm.gtt_mapping,
713 gtt_page_base, gtt_page_offset,
714 user_pages[data_page_index],
715 data_page_offset,
716 page_length);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700717
718 remain -= page_length;
719 offset += page_length;
720 data_ptr += page_length;
721 }
722
723out_unpin_object:
724 i915_gem_object_unpin(obj);
725out_unlock:
726 mutex_unlock(&dev->struct_mutex);
727out_unpin_pages:
728 for (i = 0; i < pinned_pages; i++)
729 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700730 drm_free_large(user_pages);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700731
732 return ret;
733}
734
Eric Anholt40123c12009-03-09 13:42:30 -0700735/**
736 * This is the fast shmem pwrite path, which attempts to directly
737 * copy_from_user into the kmapped pages backing the object.
738 */
Eric Anholt673a3942008-07-30 12:06:12 -0700739static int
Eric Anholt40123c12009-03-09 13:42:30 -0700740i915_gem_shmem_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
741 struct drm_i915_gem_pwrite *args,
742 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700743{
Daniel Vetter23010e42010-03-08 13:35:02 +0100744 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700745 ssize_t remain;
746 loff_t offset, page_base;
747 char __user *user_data;
748 int page_offset, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700749 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -0700750
751 user_data = (char __user *) (uintptr_t) args->data_ptr;
752 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700753
754 mutex_lock(&dev->struct_mutex);
755
Chris Wilson4bdadb92010-01-27 13:36:32 +0000756 ret = i915_gem_object_get_pages(obj, 0);
Eric Anholt40123c12009-03-09 13:42:30 -0700757 if (ret != 0)
758 goto fail_unlock;
759
Eric Anholte47c68e2008-11-14 13:35:19 -0800760 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Eric Anholt40123c12009-03-09 13:42:30 -0700761 if (ret != 0)
762 goto fail_put_pages;
Eric Anholt673a3942008-07-30 12:06:12 -0700763
Daniel Vetter23010e42010-03-08 13:35:02 +0100764 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700765 offset = args->offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700766 obj_priv->dirty = 1;
Eric Anholt673a3942008-07-30 12:06:12 -0700767
Eric Anholt40123c12009-03-09 13:42:30 -0700768 while (remain > 0) {
769 /* Operation in this page
770 *
771 * page_base = page offset within aperture
772 * page_offset = offset within page
773 * page_length = bytes to copy for this page
774 */
775 page_base = (offset & ~(PAGE_SIZE-1));
776 page_offset = offset & (PAGE_SIZE-1);
777 page_length = remain;
778 if ((page_offset + remain) > PAGE_SIZE)
779 page_length = PAGE_SIZE - page_offset;
780
781 ret = fast_shmem_write(obj_priv->pages,
782 page_base, page_offset,
783 user_data, page_length);
784 if (ret)
785 goto fail_put_pages;
786
787 remain -= page_length;
788 user_data += page_length;
789 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700790 }
791
Eric Anholt40123c12009-03-09 13:42:30 -0700792fail_put_pages:
793 i915_gem_object_put_pages(obj);
794fail_unlock:
Eric Anholt673a3942008-07-30 12:06:12 -0700795 mutex_unlock(&dev->struct_mutex);
796
Eric Anholt40123c12009-03-09 13:42:30 -0700797 return ret;
798}
799
800/**
801 * This is the fallback shmem pwrite path, which uses get_user_pages to pin
802 * the memory and maps it using kmap_atomic for copying.
803 *
804 * This avoids taking mmap_sem for faulting on the user's address while the
805 * struct_mutex is held.
806 */
807static int
808i915_gem_shmem_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
809 struct drm_i915_gem_pwrite *args,
810 struct drm_file *file_priv)
811{
Daniel Vetter23010e42010-03-08 13:35:02 +0100812 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700813 struct mm_struct *mm = current->mm;
814 struct page **user_pages;
815 ssize_t remain;
816 loff_t offset, pinned_pages, i;
817 loff_t first_data_page, last_data_page, num_pages;
818 int shmem_page_index, shmem_page_offset;
819 int data_page_index, data_page_offset;
820 int page_length;
821 int ret;
822 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700823 int do_bit17_swizzling;
Eric Anholt40123c12009-03-09 13:42:30 -0700824
825 remain = args->size;
826
827 /* Pin the user pages containing the data. We can't fault while
828 * holding the struct mutex, and all of the pwrite implementations
829 * want to hold it while dereferencing the user data.
830 */
831 first_data_page = data_ptr / PAGE_SIZE;
832 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
833 num_pages = last_data_page - first_data_page + 1;
834
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700835 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
Eric Anholt40123c12009-03-09 13:42:30 -0700836 if (user_pages == NULL)
837 return -ENOMEM;
838
839 down_read(&mm->mmap_sem);
840 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
841 num_pages, 0, 0, user_pages, NULL);
842 up_read(&mm->mmap_sem);
843 if (pinned_pages < num_pages) {
844 ret = -EFAULT;
845 goto fail_put_user_pages;
846 }
847
Eric Anholt280b7132009-03-12 16:56:27 -0700848 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
849
Eric Anholt40123c12009-03-09 13:42:30 -0700850 mutex_lock(&dev->struct_mutex);
851
Chris Wilson07f73f62009-09-14 16:50:30 +0100852 ret = i915_gem_object_get_pages_or_evict(obj);
853 if (ret)
Eric Anholt40123c12009-03-09 13:42:30 -0700854 goto fail_unlock;
855
856 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
857 if (ret != 0)
858 goto fail_put_pages;
859
Daniel Vetter23010e42010-03-08 13:35:02 +0100860 obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700861 offset = args->offset;
862 obj_priv->dirty = 1;
863
864 while (remain > 0) {
865 /* Operation in this page
866 *
867 * shmem_page_index = page number within shmem file
868 * shmem_page_offset = offset within page in shmem file
869 * data_page_index = page number in get_user_pages return
870 * data_page_offset = offset with data_page_index page.
871 * page_length = bytes to copy for this page
872 */
873 shmem_page_index = offset / PAGE_SIZE;
874 shmem_page_offset = offset & ~PAGE_MASK;
875 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
876 data_page_offset = data_ptr & ~PAGE_MASK;
877
878 page_length = remain;
879 if ((shmem_page_offset + page_length) > PAGE_SIZE)
880 page_length = PAGE_SIZE - shmem_page_offset;
881 if ((data_page_offset + page_length) > PAGE_SIZE)
882 page_length = PAGE_SIZE - data_page_offset;
883
Eric Anholt280b7132009-03-12 16:56:27 -0700884 if (do_bit17_swizzling) {
Chris Wilson99a03df2010-05-27 14:15:34 +0100885 slow_shmem_bit17_copy(obj_priv->pages[shmem_page_index],
Eric Anholt280b7132009-03-12 16:56:27 -0700886 shmem_page_offset,
887 user_pages[data_page_index],
888 data_page_offset,
Chris Wilson99a03df2010-05-27 14:15:34 +0100889 page_length,
890 0);
891 } else {
892 slow_shmem_copy(obj_priv->pages[shmem_page_index],
893 shmem_page_offset,
894 user_pages[data_page_index],
895 data_page_offset,
896 page_length);
Eric Anholt280b7132009-03-12 16:56:27 -0700897 }
Eric Anholt40123c12009-03-09 13:42:30 -0700898
899 remain -= page_length;
900 data_ptr += page_length;
901 offset += page_length;
902 }
903
904fail_put_pages:
905 i915_gem_object_put_pages(obj);
906fail_unlock:
907 mutex_unlock(&dev->struct_mutex);
908fail_put_user_pages:
909 for (i = 0; i < pinned_pages; i++)
910 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700911 drm_free_large(user_pages);
Eric Anholt40123c12009-03-09 13:42:30 -0700912
913 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700914}
915
916/**
917 * Writes data to the object referenced by handle.
918 *
919 * On error, the contents of the buffer that were to be modified are undefined.
920 */
921int
922i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
923 struct drm_file *file_priv)
924{
925 struct drm_i915_gem_pwrite *args = data;
926 struct drm_gem_object *obj;
927 struct drm_i915_gem_object *obj_priv;
928 int ret = 0;
929
930 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
931 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100932 return -ENOENT;
Daniel Vetter23010e42010-03-08 13:35:02 +0100933 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700934
935 /* Bounds check destination.
936 *
937 * XXX: This could use review for overflow issues...
938 */
939 if (args->offset > obj->size || args->size > obj->size ||
940 args->offset + args->size > obj->size) {
Luca Barbieribc9025b2010-02-09 05:49:12 +0000941 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700942 return -EINVAL;
943 }
944
945 /* We can only do the GTT pwrite on untiled buffers, as otherwise
946 * it would end up going through the fenced access, and we'll get
947 * different detiling behavior between reading and writing.
948 * pread/pwrite currently are reading and writing from the CPU
949 * perspective, requiring manual detiling by the client.
950 */
Dave Airlie71acb5e2008-12-30 20:31:46 +1000951 if (obj_priv->phys_obj)
952 ret = i915_gem_phys_pwrite(dev, obj, args, file_priv);
953 else if (obj_priv->tiling_mode == I915_TILING_NONE &&
Chris Wilson9b8c4a02010-05-27 14:21:01 +0100954 dev->gtt_total != 0 &&
955 obj->write_domain != I915_GEM_DOMAIN_CPU) {
Eric Anholt3de09aa2009-03-09 09:42:23 -0700956 ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file_priv);
957 if (ret == -EFAULT) {
958 ret = i915_gem_gtt_pwrite_slow(dev, obj, args,
959 file_priv);
960 }
Eric Anholt280b7132009-03-12 16:56:27 -0700961 } else if (i915_gem_object_needs_bit17_swizzle(obj)) {
962 ret = i915_gem_shmem_pwrite_slow(dev, obj, args, file_priv);
Eric Anholt40123c12009-03-09 13:42:30 -0700963 } else {
964 ret = i915_gem_shmem_pwrite_fast(dev, obj, args, file_priv);
965 if (ret == -EFAULT) {
966 ret = i915_gem_shmem_pwrite_slow(dev, obj, args,
967 file_priv);
968 }
969 }
Eric Anholt673a3942008-07-30 12:06:12 -0700970
971#if WATCH_PWRITE
972 if (ret)
973 DRM_INFO("pwrite failed %d\n", ret);
974#endif
975
Luca Barbieribc9025b2010-02-09 05:49:12 +0000976 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700977
978 return ret;
979}
980
981/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800982 * Called when user space prepares to use an object with the CPU, either
983 * through the mmap ioctl's mapping or a GTT mapping.
Eric Anholt673a3942008-07-30 12:06:12 -0700984 */
985int
986i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
987 struct drm_file *file_priv)
988{
Eric Anholta09ba7f2009-08-29 12:49:51 -0700989 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700990 struct drm_i915_gem_set_domain *args = data;
991 struct drm_gem_object *obj;
Jesse Barnes652c3932009-08-17 13:31:43 -0700992 struct drm_i915_gem_object *obj_priv;
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800993 uint32_t read_domains = args->read_domains;
994 uint32_t write_domain = args->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -0700995 int ret;
996
997 if (!(dev->driver->driver_features & DRIVER_GEM))
998 return -ENODEV;
999
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001000 /* Only handle setting domains to types used by the CPU. */
Chris Wilson21d509e2009-06-06 09:46:02 +01001001 if (write_domain & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001002 return -EINVAL;
1003
Chris Wilson21d509e2009-06-06 09:46:02 +01001004 if (read_domains & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001005 return -EINVAL;
1006
1007 /* Having something in the write domain implies it's in the read
1008 * domain, and only that read domain. Enforce that in the request.
1009 */
1010 if (write_domain != 0 && read_domains != write_domain)
1011 return -EINVAL;
1012
Eric Anholt673a3942008-07-30 12:06:12 -07001013 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1014 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001015 return -ENOENT;
Daniel Vetter23010e42010-03-08 13:35:02 +01001016 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001017
1018 mutex_lock(&dev->struct_mutex);
Jesse Barnes652c3932009-08-17 13:31:43 -07001019
1020 intel_mark_busy(dev, obj);
1021
Eric Anholt673a3942008-07-30 12:06:12 -07001022#if WATCH_BUF
Krzysztof Halasacfd43c02009-06-20 00:31:28 +02001023 DRM_INFO("set_domain_ioctl %p(%zd), %08x %08x\n",
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001024 obj, obj->size, read_domains, write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07001025#endif
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001026 if (read_domains & I915_GEM_DOMAIN_GTT) {
1027 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
Eric Anholt02354392008-11-26 13:58:13 -08001028
Eric Anholta09ba7f2009-08-29 12:49:51 -07001029 /* Update the LRU on the fence for the CPU access that's
1030 * about to occur.
1031 */
1032 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001033 struct drm_i915_fence_reg *reg =
1034 &dev_priv->fence_regs[obj_priv->fence_reg];
1035 list_move_tail(&reg->lru_list,
Eric Anholta09ba7f2009-08-29 12:49:51 -07001036 &dev_priv->mm.fence_list);
1037 }
1038
Eric Anholt02354392008-11-26 13:58:13 -08001039 /* Silently promote "you're not bound, there was nothing to do"
1040 * to success, since the client was just asking us to
1041 * make sure everything was done.
1042 */
1043 if (ret == -EINVAL)
1044 ret = 0;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001045 } else {
Eric Anholte47c68e2008-11-14 13:35:19 -08001046 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001047 }
1048
Chris Wilson7d1c4802010-08-07 21:45:03 +01001049
1050 /* Maintain LRU order of "inactive" objects */
1051 if (ret == 0 && i915_gem_object_is_inactive(obj_priv))
1052 list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
1053
Eric Anholt673a3942008-07-30 12:06:12 -07001054 drm_gem_object_unreference(obj);
1055 mutex_unlock(&dev->struct_mutex);
1056 return ret;
1057}
1058
1059/**
1060 * Called when user space has done writes to this buffer
1061 */
1062int
1063i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
1064 struct drm_file *file_priv)
1065{
1066 struct drm_i915_gem_sw_finish *args = data;
1067 struct drm_gem_object *obj;
1068 struct drm_i915_gem_object *obj_priv;
1069 int ret = 0;
1070
1071 if (!(dev->driver->driver_features & DRIVER_GEM))
1072 return -ENODEV;
1073
1074 mutex_lock(&dev->struct_mutex);
1075 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1076 if (obj == NULL) {
1077 mutex_unlock(&dev->struct_mutex);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001078 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001079 }
1080
1081#if WATCH_BUF
Krzysztof Halasacfd43c02009-06-20 00:31:28 +02001082 DRM_INFO("%s: sw_finish %d (%p %zd)\n",
Eric Anholt673a3942008-07-30 12:06:12 -07001083 __func__, args->handle, obj, obj->size);
1084#endif
Daniel Vetter23010e42010-03-08 13:35:02 +01001085 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001086
1087 /* Pinned buffers may be scanout, so flush the cache */
Eric Anholte47c68e2008-11-14 13:35:19 -08001088 if (obj_priv->pin_count)
1089 i915_gem_object_flush_cpu_write_domain(obj);
1090
Eric Anholt673a3942008-07-30 12:06:12 -07001091 drm_gem_object_unreference(obj);
1092 mutex_unlock(&dev->struct_mutex);
1093 return ret;
1094}
1095
1096/**
1097 * Maps the contents of an object, returning the address it is mapped
1098 * into.
1099 *
1100 * While the mapping holds a reference on the contents of the object, it doesn't
1101 * imply a ref on the object itself.
1102 */
1103int
1104i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
1105 struct drm_file *file_priv)
1106{
1107 struct drm_i915_gem_mmap *args = data;
1108 struct drm_gem_object *obj;
1109 loff_t offset;
1110 unsigned long addr;
1111
1112 if (!(dev->driver->driver_features & DRIVER_GEM))
1113 return -ENODEV;
1114
1115 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1116 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001117 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001118
1119 offset = args->offset;
1120
1121 down_write(&current->mm->mmap_sem);
1122 addr = do_mmap(obj->filp, 0, args->size,
1123 PROT_READ | PROT_WRITE, MAP_SHARED,
1124 args->offset);
1125 up_write(&current->mm->mmap_sem);
Luca Barbieribc9025b2010-02-09 05:49:12 +00001126 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001127 if (IS_ERR((void *)addr))
1128 return addr;
1129
1130 args->addr_ptr = (uint64_t) addr;
1131
1132 return 0;
1133}
1134
Jesse Barnesde151cf2008-11-12 10:03:55 -08001135/**
1136 * i915_gem_fault - fault a page into the GTT
1137 * vma: VMA in question
1138 * vmf: fault info
1139 *
1140 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1141 * from userspace. The fault handler takes care of binding the object to
1142 * the GTT (if needed), allocating and programming a fence register (again,
1143 * only if needed based on whether the old reg is still valid or the object
1144 * is tiled) and inserting a new PTE into the faulting process.
1145 *
1146 * Note that the faulting process may involve evicting existing objects
1147 * from the GTT and/or fence registers to make room. So performance may
1148 * suffer if the GTT working set is large or there are few fence registers
1149 * left.
1150 */
1151int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1152{
1153 struct drm_gem_object *obj = vma->vm_private_data;
1154 struct drm_device *dev = obj->dev;
Chris Wilson7d1c4802010-08-07 21:45:03 +01001155 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001156 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001157 pgoff_t page_offset;
1158 unsigned long pfn;
1159 int ret = 0;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001160 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001161
1162 /* We don't use vmf->pgoff since that has the fake offset */
1163 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1164 PAGE_SHIFT;
1165
1166 /* Now bind it into the GTT if needed */
1167 mutex_lock(&dev->struct_mutex);
1168 if (!obj_priv->gtt_space) {
Chris Wilsone67b8ce2009-09-14 16:50:26 +01001169 ret = i915_gem_object_bind_to_gtt(obj, 0);
Chris Wilsonc7150892009-09-23 00:43:56 +01001170 if (ret)
1171 goto unlock;
Kristian Høgsberg07f4f3e2009-05-27 14:37:28 -04001172
Jesse Barnesde151cf2008-11-12 10:03:55 -08001173 ret = i915_gem_object_set_to_gtt_domain(obj, write);
Chris Wilsonc7150892009-09-23 00:43:56 +01001174 if (ret)
1175 goto unlock;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001176 }
1177
1178 /* Need a new fence register? */
Eric Anholta09ba7f2009-08-29 12:49:51 -07001179 if (obj_priv->tiling_mode != I915_TILING_NONE) {
Chris Wilson8c4b8c32009-06-17 22:08:52 +01001180 ret = i915_gem_object_get_fence_reg(obj);
Chris Wilsonc7150892009-09-23 00:43:56 +01001181 if (ret)
1182 goto unlock;
Eric Anholtd9ddcb92009-01-27 10:33:49 -08001183 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001184
Chris Wilson7d1c4802010-08-07 21:45:03 +01001185 if (i915_gem_object_is_inactive(obj_priv))
1186 list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
1187
Jesse Barnesde151cf2008-11-12 10:03:55 -08001188 pfn = ((dev->agp->base + obj_priv->gtt_offset) >> PAGE_SHIFT) +
1189 page_offset;
1190
1191 /* Finally, remap it using the new GTT offset */
1192 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
Chris Wilsonc7150892009-09-23 00:43:56 +01001193unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001194 mutex_unlock(&dev->struct_mutex);
1195
1196 switch (ret) {
Chris Wilsonc7150892009-09-23 00:43:56 +01001197 case 0:
1198 case -ERESTARTSYS:
1199 return VM_FAULT_NOPAGE;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001200 case -ENOMEM:
1201 case -EAGAIN:
1202 return VM_FAULT_OOM;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001203 default:
Chris Wilsonc7150892009-09-23 00:43:56 +01001204 return VM_FAULT_SIGBUS;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001205 }
1206}
1207
1208/**
1209 * i915_gem_create_mmap_offset - create a fake mmap offset for an object
1210 * @obj: obj in question
1211 *
1212 * GEM memory mapping works by handing back to userspace a fake mmap offset
1213 * it can use in a subsequent mmap(2) call. The DRM core code then looks
1214 * up the object based on the offset and sets up the various memory mapping
1215 * structures.
1216 *
1217 * This routine allocates and attaches a fake offset for @obj.
1218 */
1219static int
1220i915_gem_create_mmap_offset(struct drm_gem_object *obj)
1221{
1222 struct drm_device *dev = obj->dev;
1223 struct drm_gem_mm *mm = dev->mm_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001224 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001225 struct drm_map_list *list;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +11001226 struct drm_local_map *map;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001227 int ret = 0;
1228
1229 /* Set the object up for mmap'ing */
1230 list = &obj->map_list;
Eric Anholt9a298b22009-03-24 12:23:04 -07001231 list->map = kzalloc(sizeof(struct drm_map_list), GFP_KERNEL);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001232 if (!list->map)
1233 return -ENOMEM;
1234
1235 map = list->map;
1236 map->type = _DRM_GEM;
1237 map->size = obj->size;
1238 map->handle = obj;
1239
1240 /* Get a DRM GEM mmap offset allocated... */
1241 list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
1242 obj->size / PAGE_SIZE, 0, 0);
1243 if (!list->file_offset_node) {
1244 DRM_ERROR("failed to allocate offset for bo %d\n", obj->name);
1245 ret = -ENOMEM;
1246 goto out_free_list;
1247 }
1248
1249 list->file_offset_node = drm_mm_get_block(list->file_offset_node,
1250 obj->size / PAGE_SIZE, 0);
1251 if (!list->file_offset_node) {
1252 ret = -ENOMEM;
1253 goto out_free_list;
1254 }
1255
1256 list->hash.key = list->file_offset_node->start;
1257 if (drm_ht_insert_item(&mm->offset_hash, &list->hash)) {
1258 DRM_ERROR("failed to add to map hash\n");
Chris Wilson5618ca62009-12-02 15:15:30 +00001259 ret = -ENOMEM;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001260 goto out_free_mm;
1261 }
1262
1263 /* By now we should be all set, any drm_mmap request on the offset
1264 * below will get to our mmap & fault handler */
1265 obj_priv->mmap_offset = ((uint64_t) list->hash.key) << PAGE_SHIFT;
1266
1267 return 0;
1268
1269out_free_mm:
1270 drm_mm_put_block(list->file_offset_node);
1271out_free_list:
Eric Anholt9a298b22009-03-24 12:23:04 -07001272 kfree(list->map);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001273
1274 return ret;
1275}
1276
Chris Wilson901782b2009-07-10 08:18:50 +01001277/**
1278 * i915_gem_release_mmap - remove physical page mappings
1279 * @obj: obj in question
1280 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001281 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01001282 * relinquish ownership of the pages back to the system.
1283 *
1284 * It is vital that we remove the page mapping if we have mapped a tiled
1285 * object through the GTT and then lose the fence register due to
1286 * resource pressure. Similarly if the object has been moved out of the
1287 * aperture, than pages mapped into userspace must be revoked. Removing the
1288 * mapping will then trigger a page fault on the next user access, allowing
1289 * fixup by i915_gem_fault().
1290 */
Eric Anholtd05ca302009-07-10 13:02:26 -07001291void
Chris Wilson901782b2009-07-10 08:18:50 +01001292i915_gem_release_mmap(struct drm_gem_object *obj)
1293{
1294 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001295 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson901782b2009-07-10 08:18:50 +01001296
1297 if (dev->dev_mapping)
1298 unmap_mapping_range(dev->dev_mapping,
1299 obj_priv->mmap_offset, obj->size, 1);
1300}
1301
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001302static void
1303i915_gem_free_mmap_offset(struct drm_gem_object *obj)
1304{
1305 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001306 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001307 struct drm_gem_mm *mm = dev->mm_private;
1308 struct drm_map_list *list;
1309
1310 list = &obj->map_list;
1311 drm_ht_remove_item(&mm->offset_hash, &list->hash);
1312
1313 if (list->file_offset_node) {
1314 drm_mm_put_block(list->file_offset_node);
1315 list->file_offset_node = NULL;
1316 }
1317
1318 if (list->map) {
Eric Anholt9a298b22009-03-24 12:23:04 -07001319 kfree(list->map);
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001320 list->map = NULL;
1321 }
1322
1323 obj_priv->mmap_offset = 0;
1324}
1325
Jesse Barnesde151cf2008-11-12 10:03:55 -08001326/**
1327 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1328 * @obj: object to check
1329 *
1330 * Return the required GTT alignment for an object, taking into account
1331 * potential fence register mapping if needed.
1332 */
1333static uint32_t
1334i915_gem_get_gtt_alignment(struct drm_gem_object *obj)
1335{
1336 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001337 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001338 int start, i;
1339
1340 /*
1341 * Minimum alignment is 4k (GTT page size), but might be greater
1342 * if a fence register is needed for the object.
1343 */
1344 if (IS_I965G(dev) || obj_priv->tiling_mode == I915_TILING_NONE)
1345 return 4096;
1346
1347 /*
1348 * Previous chips need to be aligned to the size of the smallest
1349 * fence register that can contain the object.
1350 */
1351 if (IS_I9XX(dev))
1352 start = 1024*1024;
1353 else
1354 start = 512*1024;
1355
1356 for (i = start; i < obj->size; i <<= 1)
1357 ;
1358
1359 return i;
1360}
1361
1362/**
1363 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1364 * @dev: DRM device
1365 * @data: GTT mapping ioctl data
1366 * @file_priv: GEM object info
1367 *
1368 * Simply returns the fake offset to userspace so it can mmap it.
1369 * The mmap call will end up in drm_gem_mmap(), which will set things
1370 * up so we can get faults in the handler above.
1371 *
1372 * The fault handler will take care of binding the object into the GTT
1373 * (since it may have been evicted to make room for something), allocating
1374 * a fence register, and mapping the appropriate aperture address into
1375 * userspace.
1376 */
1377int
1378i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
1379 struct drm_file *file_priv)
1380{
1381 struct drm_i915_gem_mmap_gtt *args = data;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001382 struct drm_gem_object *obj;
1383 struct drm_i915_gem_object *obj_priv;
1384 int ret;
1385
1386 if (!(dev->driver->driver_features & DRIVER_GEM))
1387 return -ENODEV;
1388
1389 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1390 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001391 return -ENOENT;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001392
1393 mutex_lock(&dev->struct_mutex);
1394
Daniel Vetter23010e42010-03-08 13:35:02 +01001395 obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001396
Chris Wilsonab182822009-09-22 18:46:17 +01001397 if (obj_priv->madv != I915_MADV_WILLNEED) {
1398 DRM_ERROR("Attempting to mmap a purgeable buffer\n");
1399 drm_gem_object_unreference(obj);
1400 mutex_unlock(&dev->struct_mutex);
1401 return -EINVAL;
1402 }
1403
1404
Jesse Barnesde151cf2008-11-12 10:03:55 -08001405 if (!obj_priv->mmap_offset) {
1406 ret = i915_gem_create_mmap_offset(obj);
Chris Wilson13af1062009-02-11 14:26:31 +00001407 if (ret) {
1408 drm_gem_object_unreference(obj);
1409 mutex_unlock(&dev->struct_mutex);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001410 return ret;
Chris Wilson13af1062009-02-11 14:26:31 +00001411 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001412 }
1413
1414 args->offset = obj_priv->mmap_offset;
1415
Jesse Barnesde151cf2008-11-12 10:03:55 -08001416 /*
1417 * Pull it into the GTT so that we have a page list (makes the
1418 * initial fault faster and any subsequent flushing possible).
1419 */
1420 if (!obj_priv->agp_mem) {
Chris Wilsone67b8ce2009-09-14 16:50:26 +01001421 ret = i915_gem_object_bind_to_gtt(obj, 0);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001422 if (ret) {
1423 drm_gem_object_unreference(obj);
1424 mutex_unlock(&dev->struct_mutex);
1425 return ret;
1426 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001427 }
1428
1429 drm_gem_object_unreference(obj);
1430 mutex_unlock(&dev->struct_mutex);
1431
1432 return 0;
1433}
1434
Ben Gamari6911a9b2009-04-02 11:24:54 -07001435void
Eric Anholt856fa192009-03-19 14:10:50 -07001436i915_gem_object_put_pages(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07001437{
Daniel Vetter23010e42010-03-08 13:35:02 +01001438 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001439 int page_count = obj->size / PAGE_SIZE;
1440 int i;
1441
Eric Anholt856fa192009-03-19 14:10:50 -07001442 BUG_ON(obj_priv->pages_refcount == 0);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001443 BUG_ON(obj_priv->madv == __I915_MADV_PURGED);
Eric Anholt856fa192009-03-19 14:10:50 -07001444
1445 if (--obj_priv->pages_refcount != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07001446 return;
1447
Eric Anholt280b7132009-03-12 16:56:27 -07001448 if (obj_priv->tiling_mode != I915_TILING_NONE)
1449 i915_gem_object_save_bit_17_swizzle(obj);
1450
Chris Wilson3ef94da2009-09-14 16:50:29 +01001451 if (obj_priv->madv == I915_MADV_DONTNEED)
Chris Wilson13a05fd2009-09-20 23:03:19 +01001452 obj_priv->dirty = 0;
Chris Wilson3ef94da2009-09-14 16:50:29 +01001453
1454 for (i = 0; i < page_count; i++) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01001455 if (obj_priv->dirty)
1456 set_page_dirty(obj_priv->pages[i]);
1457
1458 if (obj_priv->madv == I915_MADV_WILLNEED)
Eric Anholt856fa192009-03-19 14:10:50 -07001459 mark_page_accessed(obj_priv->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001460
1461 page_cache_release(obj_priv->pages[i]);
1462 }
Eric Anholt673a3942008-07-30 12:06:12 -07001463 obj_priv->dirty = 0;
1464
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07001465 drm_free_large(obj_priv->pages);
Eric Anholt856fa192009-03-19 14:10:50 -07001466 obj_priv->pages = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001467}
1468
1469static void
Zou Nan hai852835f2010-05-21 09:08:56 +08001470i915_gem_object_move_to_active(struct drm_gem_object *obj, uint32_t seqno,
1471 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001472{
1473 struct drm_device *dev = obj->dev;
1474 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001475 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Zou Nan hai852835f2010-05-21 09:08:56 +08001476 BUG_ON(ring == NULL);
1477 obj_priv->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001478
1479 /* Add a reference if we're newly entering the active list. */
1480 if (!obj_priv->active) {
1481 drm_gem_object_reference(obj);
1482 obj_priv->active = 1;
1483 }
1484 /* Move from whatever list we were on to the tail of execution. */
Carl Worth5e118f42009-03-20 11:54:25 -07001485 spin_lock(&dev_priv->mm.active_list_lock);
Zou Nan hai852835f2010-05-21 09:08:56 +08001486 list_move_tail(&obj_priv->list, &ring->active_list);
Carl Worth5e118f42009-03-20 11:54:25 -07001487 spin_unlock(&dev_priv->mm.active_list_lock);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001488 obj_priv->last_rendering_seqno = seqno;
Eric Anholt673a3942008-07-30 12:06:12 -07001489}
1490
Eric Anholtce44b0e2008-11-06 16:00:31 -08001491static void
1492i915_gem_object_move_to_flushing(struct drm_gem_object *obj)
1493{
1494 struct drm_device *dev = obj->dev;
1495 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001496 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001497
1498 BUG_ON(!obj_priv->active);
1499 list_move_tail(&obj_priv->list, &dev_priv->mm.flushing_list);
1500 obj_priv->last_rendering_seqno = 0;
1501}
Eric Anholt673a3942008-07-30 12:06:12 -07001502
Chris Wilson963b4832009-09-20 23:03:54 +01001503/* Immediately discard the backing storage */
1504static void
1505i915_gem_object_truncate(struct drm_gem_object *obj)
1506{
Daniel Vetter23010e42010-03-08 13:35:02 +01001507 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001508 struct inode *inode;
Chris Wilson963b4832009-09-20 23:03:54 +01001509
Chris Wilsonae9fed62010-08-07 11:01:30 +01001510 /* Our goal here is to return as much of the memory as
1511 * is possible back to the system as we are called from OOM.
1512 * To do this we must instruct the shmfs to drop all of its
1513 * backing pages, *now*. Here we mirror the actions taken
1514 * when by shmem_delete_inode() to release the backing store.
1515 */
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001516 inode = obj->filp->f_path.dentry->d_inode;
Chris Wilsonae9fed62010-08-07 11:01:30 +01001517 truncate_inode_pages(inode->i_mapping, 0);
1518 if (inode->i_op->truncate_range)
1519 inode->i_op->truncate_range(inode, 0, (loff_t)-1);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001520
1521 obj_priv->madv = __I915_MADV_PURGED;
Chris Wilson963b4832009-09-20 23:03:54 +01001522}
1523
1524static inline int
1525i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj_priv)
1526{
1527 return obj_priv->madv == I915_MADV_DONTNEED;
1528}
1529
Eric Anholt673a3942008-07-30 12:06:12 -07001530static void
1531i915_gem_object_move_to_inactive(struct drm_gem_object *obj)
1532{
1533 struct drm_device *dev = obj->dev;
1534 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001535 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001536
1537 i915_verify_inactive(dev, __FILE__, __LINE__);
1538 if (obj_priv->pin_count != 0)
1539 list_del_init(&obj_priv->list);
1540 else
1541 list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
1542
Daniel Vetter99fcb762010-02-07 16:20:18 +01001543 BUG_ON(!list_empty(&obj_priv->gpu_write_list));
1544
Eric Anholtce44b0e2008-11-06 16:00:31 -08001545 obj_priv->last_rendering_seqno = 0;
Zou Nan hai852835f2010-05-21 09:08:56 +08001546 obj_priv->ring = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001547 if (obj_priv->active) {
1548 obj_priv->active = 0;
1549 drm_gem_object_unreference(obj);
1550 }
1551 i915_verify_inactive(dev, __FILE__, __LINE__);
1552}
1553
Daniel Vetter63560392010-02-19 11:51:59 +01001554static void
1555i915_gem_process_flushing_list(struct drm_device *dev,
Zou Nan hai852835f2010-05-21 09:08:56 +08001556 uint32_t flush_domains, uint32_t seqno,
1557 struct intel_ring_buffer *ring)
Daniel Vetter63560392010-02-19 11:51:59 +01001558{
1559 drm_i915_private_t *dev_priv = dev->dev_private;
1560 struct drm_i915_gem_object *obj_priv, *next;
1561
1562 list_for_each_entry_safe(obj_priv, next,
1563 &dev_priv->mm.gpu_write_list,
1564 gpu_write_list) {
Daniel Vettera8089e82010-04-09 19:05:09 +00001565 struct drm_gem_object *obj = &obj_priv->base;
Daniel Vetter63560392010-02-19 11:51:59 +01001566
1567 if ((obj->write_domain & flush_domains) ==
Zou Nan hai852835f2010-05-21 09:08:56 +08001568 obj->write_domain &&
1569 obj_priv->ring->ring_flag == ring->ring_flag) {
Daniel Vetter63560392010-02-19 11:51:59 +01001570 uint32_t old_write_domain = obj->write_domain;
1571
1572 obj->write_domain = 0;
1573 list_del_init(&obj_priv->gpu_write_list);
Zou Nan hai852835f2010-05-21 09:08:56 +08001574 i915_gem_object_move_to_active(obj, seqno, ring);
Daniel Vetter63560392010-02-19 11:51:59 +01001575
1576 /* update the fence lru list */
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001577 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
1578 struct drm_i915_fence_reg *reg =
1579 &dev_priv->fence_regs[obj_priv->fence_reg];
1580 list_move_tail(&reg->lru_list,
Daniel Vetter63560392010-02-19 11:51:59 +01001581 &dev_priv->mm.fence_list);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001582 }
Daniel Vetter63560392010-02-19 11:51:59 +01001583
1584 trace_i915_gem_object_change_domain(obj,
1585 obj->read_domains,
1586 old_write_domain);
1587 }
1588 }
1589}
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001590
Daniel Vetter5a5a0c62009-09-15 22:57:36 +02001591uint32_t
Eric Anholtb9624422009-06-03 07:27:35 +00001592i915_add_request(struct drm_device *dev, struct drm_file *file_priv,
Zou Nan hai852835f2010-05-21 09:08:56 +08001593 uint32_t flush_domains, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001594{
1595 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtb9624422009-06-03 07:27:35 +00001596 struct drm_i915_file_private *i915_file_priv = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001597 struct drm_i915_gem_request *request;
1598 uint32_t seqno;
1599 int was_empty;
Eric Anholt673a3942008-07-30 12:06:12 -07001600
Eric Anholtb9624422009-06-03 07:27:35 +00001601 if (file_priv != NULL)
1602 i915_file_priv = file_priv->driver_priv;
1603
Eric Anholt9a298b22009-03-24 12:23:04 -07001604 request = kzalloc(sizeof(*request), GFP_KERNEL);
Eric Anholt673a3942008-07-30 12:06:12 -07001605 if (request == NULL)
1606 return 0;
1607
Zou Nan hai852835f2010-05-21 09:08:56 +08001608 seqno = ring->add_request(dev, ring, file_priv, flush_domains);
Eric Anholt673a3942008-07-30 12:06:12 -07001609
1610 request->seqno = seqno;
Zou Nan hai852835f2010-05-21 09:08:56 +08001611 request->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001612 request->emitted_jiffies = jiffies;
Zou Nan hai852835f2010-05-21 09:08:56 +08001613 was_empty = list_empty(&ring->request_list);
1614 list_add_tail(&request->list, &ring->request_list);
1615
Eric Anholtb9624422009-06-03 07:27:35 +00001616 if (i915_file_priv) {
1617 list_add_tail(&request->client_list,
1618 &i915_file_priv->mm.request_list);
1619 } else {
1620 INIT_LIST_HEAD(&request->client_list);
1621 }
Eric Anholt673a3942008-07-30 12:06:12 -07001622
Eric Anholtce44b0e2008-11-06 16:00:31 -08001623 /* Associate any objects on the flushing list matching the write
1624 * domain we're flushing with our flush.
1625 */
Daniel Vetter63560392010-02-19 11:51:59 +01001626 if (flush_domains != 0)
Zou Nan hai852835f2010-05-21 09:08:56 +08001627 i915_gem_process_flushing_list(dev, flush_domains, seqno, ring);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001628
Ben Gamarif65d9422009-09-14 17:48:44 -04001629 if (!dev_priv->mm.suspended) {
1630 mod_timer(&dev_priv->hangcheck_timer, jiffies + DRM_I915_HANGCHECK_PERIOD);
1631 if (was_empty)
1632 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
1633 }
Eric Anholt673a3942008-07-30 12:06:12 -07001634 return seqno;
1635}
1636
1637/**
1638 * Command execution barrier
1639 *
1640 * Ensures that all commands in the ring are finished
1641 * before signalling the CPU
1642 */
Eric Anholt3043c602008-10-02 12:24:47 -07001643static uint32_t
Zou Nan hai852835f2010-05-21 09:08:56 +08001644i915_retire_commands(struct drm_device *dev, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001645{
Eric Anholt673a3942008-07-30 12:06:12 -07001646 uint32_t flush_domains = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001647
1648 /* The sampler always gets flushed on i965 (sigh) */
1649 if (IS_I965G(dev))
1650 flush_domains |= I915_GEM_DOMAIN_SAMPLER;
Zou Nan hai852835f2010-05-21 09:08:56 +08001651
1652 ring->flush(dev, ring,
1653 I915_GEM_DOMAIN_COMMAND, flush_domains);
Eric Anholt673a3942008-07-30 12:06:12 -07001654 return flush_domains;
1655}
1656
1657/**
1658 * Moves buffers associated only with the given active seqno from the active
1659 * to inactive list, potentially freeing them.
1660 */
1661static void
1662i915_gem_retire_request(struct drm_device *dev,
1663 struct drm_i915_gem_request *request)
1664{
1665 drm_i915_private_t *dev_priv = dev->dev_private;
1666
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001667 trace_i915_gem_request_retire(dev, request->seqno);
1668
Eric Anholt673a3942008-07-30 12:06:12 -07001669 /* Move any buffers on the active list that are no longer referenced
1670 * by the ringbuffer to the flushing/inactive lists as appropriate.
1671 */
Carl Worth5e118f42009-03-20 11:54:25 -07001672 spin_lock(&dev_priv->mm.active_list_lock);
Zou Nan hai852835f2010-05-21 09:08:56 +08001673 while (!list_empty(&request->ring->active_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001674 struct drm_gem_object *obj;
1675 struct drm_i915_gem_object *obj_priv;
1676
Zou Nan hai852835f2010-05-21 09:08:56 +08001677 obj_priv = list_first_entry(&request->ring->active_list,
Eric Anholt673a3942008-07-30 12:06:12 -07001678 struct drm_i915_gem_object,
1679 list);
Daniel Vettera8089e82010-04-09 19:05:09 +00001680 obj = &obj_priv->base;
Eric Anholt673a3942008-07-30 12:06:12 -07001681
1682 /* If the seqno being retired doesn't match the oldest in the
1683 * list, then the oldest in the list must still be newer than
1684 * this seqno.
1685 */
1686 if (obj_priv->last_rendering_seqno != request->seqno)
Carl Worth5e118f42009-03-20 11:54:25 -07001687 goto out;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001688
Eric Anholt673a3942008-07-30 12:06:12 -07001689#if WATCH_LRU
1690 DRM_INFO("%s: retire %d moves to inactive list %p\n",
1691 __func__, request->seqno, obj);
1692#endif
1693
Eric Anholtce44b0e2008-11-06 16:00:31 -08001694 if (obj->write_domain != 0)
1695 i915_gem_object_move_to_flushing(obj);
Shaohua Li68c84342009-04-08 10:58:23 +08001696 else {
1697 /* Take a reference on the object so it won't be
1698 * freed while the spinlock is held. The list
1699 * protection for this spinlock is safe when breaking
1700 * the lock like this since the next thing we do
1701 * is just get the head of the list again.
1702 */
1703 drm_gem_object_reference(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001704 i915_gem_object_move_to_inactive(obj);
Shaohua Li68c84342009-04-08 10:58:23 +08001705 spin_unlock(&dev_priv->mm.active_list_lock);
1706 drm_gem_object_unreference(obj);
1707 spin_lock(&dev_priv->mm.active_list_lock);
1708 }
Eric Anholt673a3942008-07-30 12:06:12 -07001709 }
Carl Worth5e118f42009-03-20 11:54:25 -07001710out:
1711 spin_unlock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001712}
1713
1714/**
1715 * Returns true if seq1 is later than seq2.
1716 */
Ben Gamari22be1722009-09-14 17:48:43 -04001717bool
Eric Anholt673a3942008-07-30 12:06:12 -07001718i915_seqno_passed(uint32_t seq1, uint32_t seq2)
1719{
1720 return (int32_t)(seq1 - seq2) >= 0;
1721}
1722
1723uint32_t
Zou Nan hai852835f2010-05-21 09:08:56 +08001724i915_get_gem_seqno(struct drm_device *dev,
Zou Nan haid1b851f2010-05-21 09:08:57 +08001725 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001726{
Zou Nan hai852835f2010-05-21 09:08:56 +08001727 return ring->get_gem_seqno(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07001728}
1729
1730/**
1731 * This function clears the request list as sequence numbers are passed.
1732 */
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001733static void
1734i915_gem_retire_requests_ring(struct drm_device *dev,
1735 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001736{
1737 drm_i915_private_t *dev_priv = dev->dev_private;
1738 uint32_t seqno;
1739
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001740 if (!ring->status_page.page_addr
Zou Nan hai852835f2010-05-21 09:08:56 +08001741 || list_empty(&ring->request_list))
Karsten Wiese6c0594a2009-02-23 15:07:57 +01001742 return;
1743
Zou Nan hai852835f2010-05-21 09:08:56 +08001744 seqno = i915_get_gem_seqno(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07001745
Zou Nan hai852835f2010-05-21 09:08:56 +08001746 while (!list_empty(&ring->request_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001747 struct drm_i915_gem_request *request;
1748 uint32_t retiring_seqno;
1749
Zou Nan hai852835f2010-05-21 09:08:56 +08001750 request = list_first_entry(&ring->request_list,
Eric Anholt673a3942008-07-30 12:06:12 -07001751 struct drm_i915_gem_request,
1752 list);
1753 retiring_seqno = request->seqno;
1754
1755 if (i915_seqno_passed(seqno, retiring_seqno) ||
Ben Gamariba1234d2009-09-14 17:48:47 -04001756 atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001757 i915_gem_retire_request(dev, request);
1758
1759 list_del(&request->list);
Eric Anholtb9624422009-06-03 07:27:35 +00001760 list_del(&request->client_list);
Eric Anholt9a298b22009-03-24 12:23:04 -07001761 kfree(request);
Eric Anholt673a3942008-07-30 12:06:12 -07001762 } else
1763 break;
1764 }
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001765
1766 if (unlikely (dev_priv->trace_irq_seqno &&
1767 i915_seqno_passed(dev_priv->trace_irq_seqno, seqno))) {
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001768
1769 ring->user_irq_put(dev, ring);
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001770 dev_priv->trace_irq_seqno = 0;
1771 }
Eric Anholt673a3942008-07-30 12:06:12 -07001772}
1773
1774void
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001775i915_gem_retire_requests(struct drm_device *dev)
1776{
1777 drm_i915_private_t *dev_priv = dev->dev_private;
1778
Chris Wilsonbe726152010-07-23 23:18:50 +01001779 if (!list_empty(&dev_priv->mm.deferred_free_list)) {
1780 struct drm_i915_gem_object *obj_priv, *tmp;
1781
1782 /* We must be careful that during unbind() we do not
1783 * accidentally infinitely recurse into retire requests.
1784 * Currently:
1785 * retire -> free -> unbind -> wait -> retire_ring
1786 */
1787 list_for_each_entry_safe(obj_priv, tmp,
1788 &dev_priv->mm.deferred_free_list,
1789 list)
1790 i915_gem_free_object_tail(&obj_priv->base);
1791 }
1792
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001793 i915_gem_retire_requests_ring(dev, &dev_priv->render_ring);
1794 if (HAS_BSD(dev))
1795 i915_gem_retire_requests_ring(dev, &dev_priv->bsd_ring);
1796}
1797
1798void
Eric Anholt673a3942008-07-30 12:06:12 -07001799i915_gem_retire_work_handler(struct work_struct *work)
1800{
1801 drm_i915_private_t *dev_priv;
1802 struct drm_device *dev;
1803
1804 dev_priv = container_of(work, drm_i915_private_t,
1805 mm.retire_work.work);
1806 dev = dev_priv->dev;
1807
1808 mutex_lock(&dev->struct_mutex);
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001809 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08001810
Keith Packard6dbe2772008-10-14 21:41:13 -07001811 if (!dev_priv->mm.suspended &&
Zou Nan haid1b851f2010-05-21 09:08:57 +08001812 (!list_empty(&dev_priv->render_ring.request_list) ||
1813 (HAS_BSD(dev) &&
1814 !list_empty(&dev_priv->bsd_ring.request_list))))
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001815 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
Eric Anholt673a3942008-07-30 12:06:12 -07001816 mutex_unlock(&dev->struct_mutex);
1817}
1818
Daniel Vetter5a5a0c62009-09-15 22:57:36 +02001819int
Zou Nan hai852835f2010-05-21 09:08:56 +08001820i915_do_wait_request(struct drm_device *dev, uint32_t seqno,
1821 int interruptible, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001822{
1823 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001824 u32 ier;
Eric Anholt673a3942008-07-30 12:06:12 -07001825 int ret = 0;
1826
1827 BUG_ON(seqno == 0);
1828
Ben Gamariba1234d2009-09-14 17:48:47 -04001829 if (atomic_read(&dev_priv->mm.wedged))
Ben Gamariffed1d02009-09-14 17:48:41 -04001830 return -EIO;
1831
Zou Nan hai852835f2010-05-21 09:08:56 +08001832 if (!i915_seqno_passed(ring->get_gem_seqno(dev, ring), seqno)) {
Eric Anholtbad720f2009-10-22 16:11:14 -07001833 if (HAS_PCH_SPLIT(dev))
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001834 ier = I915_READ(DEIER) | I915_READ(GTIER);
1835 else
1836 ier = I915_READ(IER);
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001837 if (!ier) {
1838 DRM_ERROR("something (likely vbetool) disabled "
1839 "interrupts, re-enabling\n");
1840 i915_driver_irq_preinstall(dev);
1841 i915_driver_irq_postinstall(dev);
1842 }
1843
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001844 trace_i915_gem_request_wait_begin(dev, seqno);
1845
Zou Nan hai852835f2010-05-21 09:08:56 +08001846 ring->waiting_gem_seqno = seqno;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001847 ring->user_irq_get(dev, ring);
Daniel Vetter48764bf2009-09-15 22:57:32 +02001848 if (interruptible)
Zou Nan hai852835f2010-05-21 09:08:56 +08001849 ret = wait_event_interruptible(ring->irq_queue,
1850 i915_seqno_passed(
1851 ring->get_gem_seqno(dev, ring), seqno)
1852 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02001853 else
Zou Nan hai852835f2010-05-21 09:08:56 +08001854 wait_event(ring->irq_queue,
1855 i915_seqno_passed(
1856 ring->get_gem_seqno(dev, ring), seqno)
1857 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02001858
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001859 ring->user_irq_put(dev, ring);
Zou Nan hai852835f2010-05-21 09:08:56 +08001860 ring->waiting_gem_seqno = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001861
1862 trace_i915_gem_request_wait_end(dev, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07001863 }
Ben Gamariba1234d2009-09-14 17:48:47 -04001864 if (atomic_read(&dev_priv->mm.wedged))
Eric Anholt673a3942008-07-30 12:06:12 -07001865 ret = -EIO;
1866
1867 if (ret && ret != -ERESTARTSYS)
1868 DRM_ERROR("%s returns %d (awaiting %d at %d)\n",
Zou Nan hai852835f2010-05-21 09:08:56 +08001869 __func__, ret, seqno, ring->get_gem_seqno(dev, ring));
Eric Anholt673a3942008-07-30 12:06:12 -07001870
1871 /* Directly dispatch request retiring. While we have the work queue
1872 * to handle this, the waiter on a request often wants an associated
1873 * buffer to have made it to the inactive list, and we would need
1874 * a separate wait queue to handle that.
1875 */
1876 if (ret == 0)
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001877 i915_gem_retire_requests_ring(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07001878
1879 return ret;
1880}
1881
Daniel Vetter48764bf2009-09-15 22:57:32 +02001882/**
1883 * Waits for a sequence number to be signaled, and cleans up the
1884 * request and object lists appropriately for that event.
1885 */
1886static int
Zou Nan hai852835f2010-05-21 09:08:56 +08001887i915_wait_request(struct drm_device *dev, uint32_t seqno,
1888 struct intel_ring_buffer *ring)
Daniel Vetter48764bf2009-09-15 22:57:32 +02001889{
Zou Nan hai852835f2010-05-21 09:08:56 +08001890 return i915_do_wait_request(dev, seqno, 1, ring);
Daniel Vetter48764bf2009-09-15 22:57:32 +02001891}
1892
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001893static void
1894i915_gem_flush(struct drm_device *dev,
1895 uint32_t invalidate_domains,
1896 uint32_t flush_domains)
1897{
1898 drm_i915_private_t *dev_priv = dev->dev_private;
1899 if (flush_domains & I915_GEM_DOMAIN_CPU)
1900 drm_agp_chipset_flush(dev);
1901 dev_priv->render_ring.flush(dev, &dev_priv->render_ring,
1902 invalidate_domains,
1903 flush_domains);
Zou Nan haid1b851f2010-05-21 09:08:57 +08001904
1905 if (HAS_BSD(dev))
1906 dev_priv->bsd_ring.flush(dev, &dev_priv->bsd_ring,
1907 invalidate_domains,
1908 flush_domains);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001909}
1910
Eric Anholt673a3942008-07-30 12:06:12 -07001911/**
1912 * Ensures that all rendering to the object has completed and the object is
1913 * safe to unbind from the GTT or access from the CPU.
1914 */
1915static int
1916i915_gem_object_wait_rendering(struct drm_gem_object *obj)
1917{
1918 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001919 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001920 int ret;
1921
Eric Anholte47c68e2008-11-14 13:35:19 -08001922 /* This function only exists to support waiting for existing rendering,
1923 * not for emitting required flushes.
Eric Anholt673a3942008-07-30 12:06:12 -07001924 */
Eric Anholte47c68e2008-11-14 13:35:19 -08001925 BUG_ON((obj->write_domain & I915_GEM_GPU_DOMAINS) != 0);
Eric Anholt673a3942008-07-30 12:06:12 -07001926
1927 /* If there is rendering queued on the buffer being evicted, wait for
1928 * it.
1929 */
1930 if (obj_priv->active) {
1931#if WATCH_BUF
1932 DRM_INFO("%s: object %p wait for seqno %08x\n",
1933 __func__, obj, obj_priv->last_rendering_seqno);
1934#endif
Zou Nan hai852835f2010-05-21 09:08:56 +08001935 ret = i915_wait_request(dev,
1936 obj_priv->last_rendering_seqno, obj_priv->ring);
Eric Anholt673a3942008-07-30 12:06:12 -07001937 if (ret != 0)
1938 return ret;
1939 }
1940
1941 return 0;
1942}
1943
1944/**
1945 * Unbinds an object from the GTT aperture.
1946 */
Jesse Barnes0f973f22009-01-26 17:10:45 -08001947int
Eric Anholt673a3942008-07-30 12:06:12 -07001948i915_gem_object_unbind(struct drm_gem_object *obj)
1949{
1950 struct drm_device *dev = obj->dev;
Daniel Vetter4a87b8c2010-02-19 11:51:57 +01001951 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001952 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001953 int ret = 0;
1954
1955#if WATCH_BUF
1956 DRM_INFO("%s:%d %p\n", __func__, __LINE__, obj);
1957 DRM_INFO("gtt_space %p\n", obj_priv->gtt_space);
1958#endif
1959 if (obj_priv->gtt_space == NULL)
1960 return 0;
1961
1962 if (obj_priv->pin_count != 0) {
1963 DRM_ERROR("Attempting to unbind pinned buffer\n");
1964 return -EINVAL;
1965 }
1966
Eric Anholt5323fd02009-09-09 11:50:45 -07001967 /* blow away mappings if mapped through GTT */
1968 i915_gem_release_mmap(obj);
1969
Eric Anholt673a3942008-07-30 12:06:12 -07001970 /* Move the object to the CPU domain to ensure that
1971 * any possible CPU writes while it's not in the GTT
1972 * are flushed when we go to remap it. This will
1973 * also ensure that all pending GPU writes are finished
1974 * before we unbind.
1975 */
Eric Anholte47c68e2008-11-14 13:35:19 -08001976 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Chris Wilson8dc17752010-07-23 23:18:51 +01001977 if (ret == -ERESTARTSYS)
Eric Anholt673a3942008-07-30 12:06:12 -07001978 return ret;
Chris Wilson8dc17752010-07-23 23:18:51 +01001979 /* Continue on if we fail due to EIO, the GPU is hung so we
1980 * should be safe and we need to cleanup or else we might
1981 * cause memory corruption through use-after-free.
1982 */
Eric Anholt673a3942008-07-30 12:06:12 -07001983
Daniel Vetter96b47b62009-12-15 17:50:00 +01001984 /* release the fence reg _after_ flushing */
1985 if (obj_priv->fence_reg != I915_FENCE_REG_NONE)
1986 i915_gem_clear_fence_reg(obj);
1987
Eric Anholt673a3942008-07-30 12:06:12 -07001988 if (obj_priv->agp_mem != NULL) {
1989 drm_unbind_agp(obj_priv->agp_mem);
1990 drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE);
1991 obj_priv->agp_mem = NULL;
1992 }
1993
Eric Anholt856fa192009-03-19 14:10:50 -07001994 i915_gem_object_put_pages(obj);
Chris Wilsona32808c2009-09-20 21:29:47 +01001995 BUG_ON(obj_priv->pages_refcount);
Eric Anholt673a3942008-07-30 12:06:12 -07001996
1997 if (obj_priv->gtt_space) {
1998 atomic_dec(&dev->gtt_count);
1999 atomic_sub(obj->size, &dev->gtt_memory);
2000
2001 drm_mm_put_block(obj_priv->gtt_space);
2002 obj_priv->gtt_space = NULL;
2003 }
2004
2005 /* Remove ourselves from the LRU list if present. */
Daniel Vetter4a87b8c2010-02-19 11:51:57 +01002006 spin_lock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07002007 if (!list_empty(&obj_priv->list))
2008 list_del_init(&obj_priv->list);
Daniel Vetter4a87b8c2010-02-19 11:51:57 +01002009 spin_unlock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07002010
Chris Wilson963b4832009-09-20 23:03:54 +01002011 if (i915_gem_object_is_purgeable(obj_priv))
2012 i915_gem_object_truncate(obj);
2013
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002014 trace_i915_gem_object_unbind(obj);
2015
Chris Wilson8dc17752010-07-23 23:18:51 +01002016 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002017}
2018
Chris Wilsonb47eb4a2010-08-07 11:01:23 +01002019int
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002020i915_gpu_idle(struct drm_device *dev)
2021{
2022 drm_i915_private_t *dev_priv = dev->dev_private;
2023 bool lists_empty;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002024 uint32_t seqno1, seqno2;
Zou Nan hai852835f2010-05-21 09:08:56 +08002025 int ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002026
2027 spin_lock(&dev_priv->mm.active_list_lock);
Zou Nan haid1b851f2010-05-21 09:08:57 +08002028 lists_empty = (list_empty(&dev_priv->mm.flushing_list) &&
2029 list_empty(&dev_priv->render_ring.active_list) &&
2030 (!HAS_BSD(dev) ||
2031 list_empty(&dev_priv->bsd_ring.active_list)));
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002032 spin_unlock(&dev_priv->mm.active_list_lock);
2033
2034 if (lists_empty)
2035 return 0;
2036
2037 /* Flush everything onto the inactive list. */
2038 i915_gem_flush(dev, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
Zou Nan haid1b851f2010-05-21 09:08:57 +08002039 seqno1 = i915_add_request(dev, NULL, I915_GEM_GPU_DOMAINS,
Zou Nan hai852835f2010-05-21 09:08:56 +08002040 &dev_priv->render_ring);
Zou Nan haid1b851f2010-05-21 09:08:57 +08002041 if (seqno1 == 0)
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002042 return -ENOMEM;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002043 ret = i915_wait_request(dev, seqno1, &dev_priv->render_ring);
2044
2045 if (HAS_BSD(dev)) {
2046 seqno2 = i915_add_request(dev, NULL, I915_GEM_GPU_DOMAINS,
2047 &dev_priv->bsd_ring);
2048 if (seqno2 == 0)
2049 return -ENOMEM;
2050
2051 ret = i915_wait_request(dev, seqno2, &dev_priv->bsd_ring);
2052 if (ret)
2053 return ret;
2054 }
2055
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002056
Zou Nan hai852835f2010-05-21 09:08:56 +08002057 return ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002058}
2059
Ben Gamari6911a9b2009-04-02 11:24:54 -07002060int
Chris Wilson4bdadb92010-01-27 13:36:32 +00002061i915_gem_object_get_pages(struct drm_gem_object *obj,
2062 gfp_t gfpmask)
Eric Anholt673a3942008-07-30 12:06:12 -07002063{
Daniel Vetter23010e42010-03-08 13:35:02 +01002064 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002065 int page_count, i;
2066 struct address_space *mapping;
2067 struct inode *inode;
2068 struct page *page;
Eric Anholt673a3942008-07-30 12:06:12 -07002069
Daniel Vetter778c3542010-05-13 11:49:44 +02002070 BUG_ON(obj_priv->pages_refcount
2071 == DRM_I915_GEM_OBJECT_MAX_PAGES_REFCOUNT);
2072
Eric Anholt856fa192009-03-19 14:10:50 -07002073 if (obj_priv->pages_refcount++ != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07002074 return 0;
2075
2076 /* Get the list of pages out of our struct file. They'll be pinned
2077 * at this point until we release them.
2078 */
2079 page_count = obj->size / PAGE_SIZE;
Eric Anholt856fa192009-03-19 14:10:50 -07002080 BUG_ON(obj_priv->pages != NULL);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07002081 obj_priv->pages = drm_calloc_large(page_count, sizeof(struct page *));
Eric Anholt856fa192009-03-19 14:10:50 -07002082 if (obj_priv->pages == NULL) {
Eric Anholt856fa192009-03-19 14:10:50 -07002083 obj_priv->pages_refcount--;
Eric Anholt673a3942008-07-30 12:06:12 -07002084 return -ENOMEM;
2085 }
2086
2087 inode = obj->filp->f_path.dentry->d_inode;
2088 mapping = inode->i_mapping;
2089 for (i = 0; i < page_count; i++) {
Chris Wilson4bdadb92010-01-27 13:36:32 +00002090 page = read_cache_page_gfp(mapping, i,
Linus Torvalds985b8232010-07-02 10:04:42 +10002091 GFP_HIGHUSER |
Chris Wilson4bdadb92010-01-27 13:36:32 +00002092 __GFP_COLD |
Linus Torvaldscd9f0402010-07-18 09:44:37 -07002093 __GFP_RECLAIMABLE |
Chris Wilson4bdadb92010-01-27 13:36:32 +00002094 gfpmask);
Chris Wilson1f2b1012010-03-12 19:52:55 +00002095 if (IS_ERR(page))
2096 goto err_pages;
2097
Eric Anholt856fa192009-03-19 14:10:50 -07002098 obj_priv->pages[i] = page;
Eric Anholt673a3942008-07-30 12:06:12 -07002099 }
Eric Anholt280b7132009-03-12 16:56:27 -07002100
2101 if (obj_priv->tiling_mode != I915_TILING_NONE)
2102 i915_gem_object_do_bit_17_swizzle(obj);
2103
Eric Anholt673a3942008-07-30 12:06:12 -07002104 return 0;
Chris Wilson1f2b1012010-03-12 19:52:55 +00002105
2106err_pages:
2107 while (i--)
2108 page_cache_release(obj_priv->pages[i]);
2109
2110 drm_free_large(obj_priv->pages);
2111 obj_priv->pages = NULL;
2112 obj_priv->pages_refcount--;
2113 return PTR_ERR(page);
Eric Anholt673a3942008-07-30 12:06:12 -07002114}
2115
Eric Anholt4e901fd2009-10-26 16:44:17 -07002116static void sandybridge_write_fence_reg(struct drm_i915_fence_reg *reg)
2117{
2118 struct drm_gem_object *obj = reg->obj;
2119 struct drm_device *dev = obj->dev;
2120 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002121 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt4e901fd2009-10-26 16:44:17 -07002122 int regnum = obj_priv->fence_reg;
2123 uint64_t val;
2124
2125 val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
2126 0xfffff000) << 32;
2127 val |= obj_priv->gtt_offset & 0xfffff000;
2128 val |= (uint64_t)((obj_priv->stride / 128) - 1) <<
2129 SANDYBRIDGE_FENCE_PITCH_SHIFT;
2130
2131 if (obj_priv->tiling_mode == I915_TILING_Y)
2132 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2133 val |= I965_FENCE_REG_VALID;
2134
2135 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (regnum * 8), val);
2136}
2137
Jesse Barnesde151cf2008-11-12 10:03:55 -08002138static void i965_write_fence_reg(struct drm_i915_fence_reg *reg)
2139{
2140 struct drm_gem_object *obj = reg->obj;
2141 struct drm_device *dev = obj->dev;
2142 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002143 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002144 int regnum = obj_priv->fence_reg;
2145 uint64_t val;
2146
2147 val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
2148 0xfffff000) << 32;
2149 val |= obj_priv->gtt_offset & 0xfffff000;
2150 val |= ((obj_priv->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
2151 if (obj_priv->tiling_mode == I915_TILING_Y)
2152 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2153 val |= I965_FENCE_REG_VALID;
2154
2155 I915_WRITE64(FENCE_REG_965_0 + (regnum * 8), val);
2156}
2157
2158static void i915_write_fence_reg(struct drm_i915_fence_reg *reg)
2159{
2160 struct drm_gem_object *obj = reg->obj;
2161 struct drm_device *dev = obj->dev;
2162 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002163 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002164 int regnum = obj_priv->fence_reg;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002165 int tile_width;
Eric Anholtdc529a42009-03-10 22:34:49 -07002166 uint32_t fence_reg, val;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002167 uint32_t pitch_val;
2168
2169 if ((obj_priv->gtt_offset & ~I915_FENCE_START_MASK) ||
2170 (obj_priv->gtt_offset & (obj->size - 1))) {
Linus Torvaldsf06da262009-02-09 08:57:29 -08002171 WARN(1, "%s: object 0x%08x not 1M or size (0x%zx) aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002172 __func__, obj_priv->gtt_offset, obj->size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002173 return;
2174 }
2175
Jesse Barnes0f973f22009-01-26 17:10:45 -08002176 if (obj_priv->tiling_mode == I915_TILING_Y &&
2177 HAS_128_BYTE_Y_TILING(dev))
2178 tile_width = 128;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002179 else
Jesse Barnes0f973f22009-01-26 17:10:45 -08002180 tile_width = 512;
2181
2182 /* Note: pitch better be a power of two tile widths */
2183 pitch_val = obj_priv->stride / tile_width;
2184 pitch_val = ffs(pitch_val) - 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002185
Daniel Vetterc36a2a62010-04-17 15:12:03 +02002186 if (obj_priv->tiling_mode == I915_TILING_Y &&
2187 HAS_128_BYTE_Y_TILING(dev))
2188 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2189 else
2190 WARN_ON(pitch_val > I915_FENCE_MAX_PITCH_VAL);
2191
Jesse Barnesde151cf2008-11-12 10:03:55 -08002192 val = obj_priv->gtt_offset;
2193 if (obj_priv->tiling_mode == I915_TILING_Y)
2194 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2195 val |= I915_FENCE_SIZE_BITS(obj->size);
2196 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2197 val |= I830_FENCE_REG_VALID;
2198
Eric Anholtdc529a42009-03-10 22:34:49 -07002199 if (regnum < 8)
2200 fence_reg = FENCE_REG_830_0 + (regnum * 4);
2201 else
2202 fence_reg = FENCE_REG_945_8 + ((regnum - 8) * 4);
2203 I915_WRITE(fence_reg, val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002204}
2205
2206static void i830_write_fence_reg(struct drm_i915_fence_reg *reg)
2207{
2208 struct drm_gem_object *obj = reg->obj;
2209 struct drm_device *dev = obj->dev;
2210 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002211 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002212 int regnum = obj_priv->fence_reg;
2213 uint32_t val;
2214 uint32_t pitch_val;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002215 uint32_t fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002216
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002217 if ((obj_priv->gtt_offset & ~I830_FENCE_START_MASK) ||
Jesse Barnesde151cf2008-11-12 10:03:55 -08002218 (obj_priv->gtt_offset & (obj->size - 1))) {
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002219 WARN(1, "%s: object 0x%08x not 512K or size aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002220 __func__, obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002221 return;
2222 }
2223
Eric Anholte76a16d2009-05-26 17:44:56 -07002224 pitch_val = obj_priv->stride / 128;
2225 pitch_val = ffs(pitch_val) - 1;
2226 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2227
Jesse Barnesde151cf2008-11-12 10:03:55 -08002228 val = obj_priv->gtt_offset;
2229 if (obj_priv->tiling_mode == I915_TILING_Y)
2230 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002231 fence_size_bits = I830_FENCE_SIZE_BITS(obj->size);
2232 WARN_ON(fence_size_bits & ~0x00000f00);
2233 val |= fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002234 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2235 val |= I830_FENCE_REG_VALID;
2236
2237 I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002238}
2239
Daniel Vetterae3db242010-02-19 11:51:58 +01002240static int i915_find_fence_reg(struct drm_device *dev)
2241{
2242 struct drm_i915_fence_reg *reg = NULL;
2243 struct drm_i915_gem_object *obj_priv = NULL;
2244 struct drm_i915_private *dev_priv = dev->dev_private;
2245 struct drm_gem_object *obj = NULL;
2246 int i, avail, ret;
2247
2248 /* First try to find a free reg */
2249 avail = 0;
2250 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2251 reg = &dev_priv->fence_regs[i];
2252 if (!reg->obj)
2253 return i;
2254
Daniel Vetter23010e42010-03-08 13:35:02 +01002255 obj_priv = to_intel_bo(reg->obj);
Daniel Vetterae3db242010-02-19 11:51:58 +01002256 if (!obj_priv->pin_count)
2257 avail++;
2258 }
2259
2260 if (avail == 0)
2261 return -ENOSPC;
2262
2263 /* None available, try to steal one or wait for a user to finish */
2264 i = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002265 list_for_each_entry(reg, &dev_priv->mm.fence_list,
2266 lru_list) {
2267 obj = reg->obj;
2268 obj_priv = to_intel_bo(obj);
Daniel Vetterae3db242010-02-19 11:51:58 +01002269
2270 if (obj_priv->pin_count)
2271 continue;
2272
2273 /* found one! */
2274 i = obj_priv->fence_reg;
2275 break;
2276 }
2277
2278 BUG_ON(i == I915_FENCE_REG_NONE);
2279
2280 /* We only have a reference on obj from the active list. put_fence_reg
2281 * might drop that one, causing a use-after-free in it. So hold a
2282 * private reference to obj like the other callers of put_fence_reg
2283 * (set_tiling ioctl) do. */
2284 drm_gem_object_reference(obj);
2285 ret = i915_gem_object_put_fence_reg(obj);
2286 drm_gem_object_unreference(obj);
2287 if (ret != 0)
2288 return ret;
2289
2290 return i;
2291}
2292
Jesse Barnesde151cf2008-11-12 10:03:55 -08002293/**
2294 * i915_gem_object_get_fence_reg - set up a fence reg for an object
2295 * @obj: object to map through a fence reg
2296 *
2297 * When mapping objects through the GTT, userspace wants to be able to write
2298 * to them without having to worry about swizzling if the object is tiled.
2299 *
2300 * This function walks the fence regs looking for a free one for @obj,
2301 * stealing one if it can't find any.
2302 *
2303 * It then sets up the reg based on the object's properties: address, pitch
2304 * and tiling format.
2305 */
Chris Wilson8c4b8c32009-06-17 22:08:52 +01002306int
2307i915_gem_object_get_fence_reg(struct drm_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002308{
2309 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002310 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002311 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002312 struct drm_i915_fence_reg *reg = NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002313 int ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002314
Eric Anholta09ba7f2009-08-29 12:49:51 -07002315 /* Just update our place in the LRU if our fence is getting used. */
2316 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002317 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
2318 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002319 return 0;
2320 }
2321
Jesse Barnesde151cf2008-11-12 10:03:55 -08002322 switch (obj_priv->tiling_mode) {
2323 case I915_TILING_NONE:
2324 WARN(1, "allocating a fence for non-tiled object?\n");
2325 break;
2326 case I915_TILING_X:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002327 if (!obj_priv->stride)
2328 return -EINVAL;
2329 WARN((obj_priv->stride & (512 - 1)),
2330 "object 0x%08x is X tiled but has non-512B pitch\n",
2331 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002332 break;
2333 case I915_TILING_Y:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002334 if (!obj_priv->stride)
2335 return -EINVAL;
2336 WARN((obj_priv->stride & (128 - 1)),
2337 "object 0x%08x is Y tiled but has non-128B pitch\n",
2338 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002339 break;
2340 }
2341
Daniel Vetterae3db242010-02-19 11:51:58 +01002342 ret = i915_find_fence_reg(dev);
2343 if (ret < 0)
2344 return ret;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002345
Daniel Vetterae3db242010-02-19 11:51:58 +01002346 obj_priv->fence_reg = ret;
2347 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002348 list_add_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002349
Jesse Barnesde151cf2008-11-12 10:03:55 -08002350 reg->obj = obj;
2351
Chris Wilsone259bef2010-09-17 00:32:02 +01002352 switch (INTEL_INFO(dev)->gen) {
2353 case 6:
Eric Anholt4e901fd2009-10-26 16:44:17 -07002354 sandybridge_write_fence_reg(reg);
Chris Wilsone259bef2010-09-17 00:32:02 +01002355 break;
2356 case 5:
2357 case 4:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002358 i965_write_fence_reg(reg);
Chris Wilsone259bef2010-09-17 00:32:02 +01002359 break;
2360 case 3:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002361 i915_write_fence_reg(reg);
Chris Wilsone259bef2010-09-17 00:32:02 +01002362 break;
2363 case 2:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002364 i830_write_fence_reg(reg);
Chris Wilsone259bef2010-09-17 00:32:02 +01002365 break;
2366 }
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002367
Daniel Vetterae3db242010-02-19 11:51:58 +01002368 trace_i915_gem_object_get_fence(obj, obj_priv->fence_reg,
2369 obj_priv->tiling_mode);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002370
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002371 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002372}
2373
2374/**
2375 * i915_gem_clear_fence_reg - clear out fence register info
2376 * @obj: object to clear
2377 *
2378 * Zeroes out the fence register itself and clears out the associated
2379 * data structures in dev_priv and obj_priv.
2380 */
2381static void
2382i915_gem_clear_fence_reg(struct drm_gem_object *obj)
2383{
2384 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002385 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002386 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002387 struct drm_i915_fence_reg *reg =
2388 &dev_priv->fence_regs[obj_priv->fence_reg];
Chris Wilsone259bef2010-09-17 00:32:02 +01002389 uint32_t fence_reg;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002390
Chris Wilsone259bef2010-09-17 00:32:02 +01002391 switch (INTEL_INFO(dev)->gen) {
2392 case 6:
Eric Anholt4e901fd2009-10-26 16:44:17 -07002393 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 +
2394 (obj_priv->fence_reg * 8), 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002395 break;
2396 case 5:
2397 case 4:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002398 I915_WRITE64(FENCE_REG_965_0 + (obj_priv->fence_reg * 8), 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002399 break;
2400 case 3:
2401 if (obj_priv->fence_reg > 8)
2402 fence_reg = FENCE_REG_945_8 + (obj_priv->fence_reg - 8) * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002403 else
Chris Wilsone259bef2010-09-17 00:32:02 +01002404 case 2:
2405 fence_reg = FENCE_REG_830_0 + obj_priv->fence_reg * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002406
2407 I915_WRITE(fence_reg, 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002408 break;
Eric Anholtdc529a42009-03-10 22:34:49 -07002409 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002410
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002411 reg->obj = NULL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002412 obj_priv->fence_reg = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002413 list_del_init(&reg->lru_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002414}
2415
Eric Anholt673a3942008-07-30 12:06:12 -07002416/**
Chris Wilson52dc7d32009-06-06 09:46:01 +01002417 * i915_gem_object_put_fence_reg - waits on outstanding fenced access
2418 * to the buffer to finish, and then resets the fence register.
2419 * @obj: tiled object holding a fence register.
2420 *
2421 * Zeroes out the fence register itself and clears out the associated
2422 * data structures in dev_priv and obj_priv.
2423 */
2424int
2425i915_gem_object_put_fence_reg(struct drm_gem_object *obj)
2426{
2427 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01002428 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson52dc7d32009-06-06 09:46:01 +01002429
2430 if (obj_priv->fence_reg == I915_FENCE_REG_NONE)
2431 return 0;
2432
Daniel Vetter10ae9bd2010-02-01 13:59:17 +01002433 /* If we've changed tiling, GTT-mappings of the object
2434 * need to re-fault to ensure that the correct fence register
2435 * setup is in place.
2436 */
2437 i915_gem_release_mmap(obj);
2438
Chris Wilson52dc7d32009-06-06 09:46:01 +01002439 /* On the i915, GPU access to tiled buffers is via a fence,
2440 * therefore we must wait for any outstanding access to complete
2441 * before clearing the fence.
2442 */
2443 if (!IS_I965G(dev)) {
2444 int ret;
2445
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002446 ret = i915_gem_object_flush_gpu_write_domain(obj);
2447 if (ret != 0)
2448 return ret;
2449
Chris Wilson52dc7d32009-06-06 09:46:01 +01002450 ret = i915_gem_object_wait_rendering(obj);
2451 if (ret != 0)
2452 return ret;
2453 }
2454
Daniel Vetter4a726612010-02-01 13:59:16 +01002455 i915_gem_object_flush_gtt_write_domain(obj);
Chris Wilson52dc7d32009-06-06 09:46:01 +01002456 i915_gem_clear_fence_reg (obj);
2457
2458 return 0;
2459}
2460
2461/**
Eric Anholt673a3942008-07-30 12:06:12 -07002462 * Finds free space in the GTT aperture and binds the object there.
2463 */
2464static int
2465i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment)
2466{
2467 struct drm_device *dev = obj->dev;
2468 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002469 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002470 struct drm_mm_node *free_space;
Chris Wilson4bdadb92010-01-27 13:36:32 +00002471 gfp_t gfpmask = __GFP_NORETRY | __GFP_NOWARN;
Chris Wilson07f73f62009-09-14 16:50:30 +01002472 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002473
Chris Wilsonbb6baf72009-09-22 14:24:13 +01002474 if (obj_priv->madv != I915_MADV_WILLNEED) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01002475 DRM_ERROR("Attempting to bind a purgeable object\n");
2476 return -EINVAL;
2477 }
2478
Eric Anholt673a3942008-07-30 12:06:12 -07002479 if (alignment == 0)
Jesse Barnes0f973f22009-01-26 17:10:45 -08002480 alignment = i915_gem_get_gtt_alignment(obj);
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002481 if (alignment & (i915_gem_get_gtt_alignment(obj) - 1)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002482 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2483 return -EINVAL;
2484 }
2485
Chris Wilson654fc602010-05-27 13:18:21 +01002486 /* If the object is bigger than the entire aperture, reject it early
2487 * before evicting everything in a vain attempt to find space.
2488 */
2489 if (obj->size > dev->gtt_total) {
2490 DRM_ERROR("Attempting to bind an object larger than the aperture\n");
2491 return -E2BIG;
2492 }
2493
Eric Anholt673a3942008-07-30 12:06:12 -07002494 search_free:
2495 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
2496 obj->size, alignment, 0);
2497 if (free_space != NULL) {
2498 obj_priv->gtt_space = drm_mm_get_block(free_space, obj->size,
2499 alignment);
Daniel Vetterdb3307a2010-07-02 15:02:12 +01002500 if (obj_priv->gtt_space != NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002501 obj_priv->gtt_offset = obj_priv->gtt_space->start;
Eric Anholt673a3942008-07-30 12:06:12 -07002502 }
2503 if (obj_priv->gtt_space == NULL) {
2504 /* If the gtt is empty and we're still having trouble
2505 * fitting our object in, we're out of memory.
2506 */
2507#if WATCH_LRU
2508 DRM_INFO("%s: GTT full, evicting something\n", __func__);
2509#endif
Daniel Vetter0108a3e2010-08-07 11:01:21 +01002510 ret = i915_gem_evict_something(dev, obj->size, alignment);
Chris Wilson97311292009-09-21 00:22:34 +01002511 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002512 return ret;
Chris Wilson97311292009-09-21 00:22:34 +01002513
Eric Anholt673a3942008-07-30 12:06:12 -07002514 goto search_free;
2515 }
2516
2517#if WATCH_BUF
Krzysztof Halasacfd43c02009-06-20 00:31:28 +02002518 DRM_INFO("Binding object of size %zd at 0x%08x\n",
Eric Anholt673a3942008-07-30 12:06:12 -07002519 obj->size, obj_priv->gtt_offset);
2520#endif
Chris Wilson4bdadb92010-01-27 13:36:32 +00002521 ret = i915_gem_object_get_pages(obj, gfpmask);
Eric Anholt673a3942008-07-30 12:06:12 -07002522 if (ret) {
2523 drm_mm_put_block(obj_priv->gtt_space);
2524 obj_priv->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002525
2526 if (ret == -ENOMEM) {
2527 /* first try to clear up some space from the GTT */
Daniel Vetter0108a3e2010-08-07 11:01:21 +01002528 ret = i915_gem_evict_something(dev, obj->size,
2529 alignment);
Chris Wilson07f73f62009-09-14 16:50:30 +01002530 if (ret) {
Chris Wilson07f73f62009-09-14 16:50:30 +01002531 /* now try to shrink everyone else */
Chris Wilson4bdadb92010-01-27 13:36:32 +00002532 if (gfpmask) {
2533 gfpmask = 0;
2534 goto search_free;
Chris Wilson07f73f62009-09-14 16:50:30 +01002535 }
2536
2537 return ret;
2538 }
2539
2540 goto search_free;
2541 }
2542
Eric Anholt673a3942008-07-30 12:06:12 -07002543 return ret;
2544 }
2545
Eric Anholt673a3942008-07-30 12:06:12 -07002546 /* Create an AGP memory structure pointing at our pages, and bind it
2547 * into the GTT.
2548 */
2549 obj_priv->agp_mem = drm_agp_bind_pages(dev,
Eric Anholt856fa192009-03-19 14:10:50 -07002550 obj_priv->pages,
Chris Wilson07f73f62009-09-14 16:50:30 +01002551 obj->size >> PAGE_SHIFT,
Keith Packardba1eb1d2008-10-14 19:55:10 -07002552 obj_priv->gtt_offset,
2553 obj_priv->agp_type);
Eric Anholt673a3942008-07-30 12:06:12 -07002554 if (obj_priv->agp_mem == NULL) {
Eric Anholt856fa192009-03-19 14:10:50 -07002555 i915_gem_object_put_pages(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002556 drm_mm_put_block(obj_priv->gtt_space);
2557 obj_priv->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002558
Daniel Vetter0108a3e2010-08-07 11:01:21 +01002559 ret = i915_gem_evict_something(dev, obj->size, alignment);
Chris Wilson97311292009-09-21 00:22:34 +01002560 if (ret)
Chris Wilson07f73f62009-09-14 16:50:30 +01002561 return ret;
Chris Wilson07f73f62009-09-14 16:50:30 +01002562
2563 goto search_free;
Eric Anholt673a3942008-07-30 12:06:12 -07002564 }
2565 atomic_inc(&dev->gtt_count);
2566 atomic_add(obj->size, &dev->gtt_memory);
2567
Chris Wilsonbf1a1092010-08-07 11:01:20 +01002568 /* keep track of bounds object by adding it to the inactive list */
2569 list_add_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
2570
Eric Anholt673a3942008-07-30 12:06:12 -07002571 /* Assert that the object is not currently in any GPU domain. As it
2572 * wasn't in the GTT, there shouldn't be any way it could have been in
2573 * a GPU cache
2574 */
Chris Wilson21d509e2009-06-06 09:46:02 +01002575 BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS);
2576 BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS);
Eric Anholt673a3942008-07-30 12:06:12 -07002577
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002578 trace_i915_gem_object_bind(obj, obj_priv->gtt_offset);
2579
Eric Anholt673a3942008-07-30 12:06:12 -07002580 return 0;
2581}
2582
2583void
2584i915_gem_clflush_object(struct drm_gem_object *obj)
2585{
Daniel Vetter23010e42010-03-08 13:35:02 +01002586 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002587
2588 /* If we don't have a page list set up, then we're not pinned
2589 * to GPU, and we can ignore the cache flush because it'll happen
2590 * again at bind time.
2591 */
Eric Anholt856fa192009-03-19 14:10:50 -07002592 if (obj_priv->pages == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002593 return;
2594
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002595 trace_i915_gem_object_clflush(obj);
Eric Anholtcfa16a02009-05-26 18:46:16 -07002596
Eric Anholt856fa192009-03-19 14:10:50 -07002597 drm_clflush_pages(obj_priv->pages, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002598}
2599
Eric Anholte47c68e2008-11-14 13:35:19 -08002600/** Flushes any GPU write domain for the object if it's dirty. */
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002601static int
Eric Anholte47c68e2008-11-14 13:35:19 -08002602i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj)
2603{
2604 struct drm_device *dev = obj->dev;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002605 uint32_t old_write_domain;
Zou Nan hai852835f2010-05-21 09:08:56 +08002606 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08002607
2608 if ((obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002609 return 0;
Eric Anholte47c68e2008-11-14 13:35:19 -08002610
2611 /* Queue the GPU write cache flushing we need. */
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002612 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002613 i915_gem_flush(dev, 0, obj->write_domain);
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002614 if (i915_add_request(dev, NULL, obj->write_domain, obj_priv->ring) == 0)
2615 return -ENOMEM;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002616
2617 trace_i915_gem_object_change_domain(obj,
2618 obj->read_domains,
2619 old_write_domain);
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002620 return 0;
Eric Anholte47c68e2008-11-14 13:35:19 -08002621}
2622
2623/** Flushes the GTT write domain for the object if it's dirty. */
2624static void
2625i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj)
2626{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002627 uint32_t old_write_domain;
2628
Eric Anholte47c68e2008-11-14 13:35:19 -08002629 if (obj->write_domain != I915_GEM_DOMAIN_GTT)
2630 return;
2631
2632 /* No actual flushing is required for the GTT write domain. Writes
2633 * to it immediately go to main memory as far as we know, so there's
2634 * no chipset flush. It also doesn't land in render cache.
2635 */
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002636 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002637 obj->write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002638
2639 trace_i915_gem_object_change_domain(obj,
2640 obj->read_domains,
2641 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002642}
2643
2644/** Flushes the CPU write domain for the object if it's dirty. */
2645static void
2646i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj)
2647{
2648 struct drm_device *dev = obj->dev;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002649 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002650
2651 if (obj->write_domain != I915_GEM_DOMAIN_CPU)
2652 return;
2653
2654 i915_gem_clflush_object(obj);
2655 drm_agp_chipset_flush(dev);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002656 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002657 obj->write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002658
2659 trace_i915_gem_object_change_domain(obj,
2660 obj->read_domains,
2661 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002662}
2663
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002664int
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002665i915_gem_object_flush_write_domain(struct drm_gem_object *obj)
2666{
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002667 int ret = 0;
2668
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002669 switch (obj->write_domain) {
2670 case I915_GEM_DOMAIN_GTT:
2671 i915_gem_object_flush_gtt_write_domain(obj);
2672 break;
2673 case I915_GEM_DOMAIN_CPU:
2674 i915_gem_object_flush_cpu_write_domain(obj);
2675 break;
2676 default:
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002677 ret = i915_gem_object_flush_gpu_write_domain(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002678 break;
2679 }
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002680
2681 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002682}
2683
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002684/**
2685 * Moves a single object to the GTT read, and possibly write domain.
2686 *
2687 * This function returns when the move is complete, including waiting on
2688 * flushes to occur.
2689 */
Jesse Barnes79e53942008-11-07 14:24:08 -08002690int
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002691i915_gem_object_set_to_gtt_domain(struct drm_gem_object *obj, int write)
2692{
Daniel Vetter23010e42010-03-08 13:35:02 +01002693 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002694 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002695 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002696
Eric Anholt02354392008-11-26 13:58:13 -08002697 /* Not valid to be called on unbound objects. */
2698 if (obj_priv->gtt_space == NULL)
2699 return -EINVAL;
2700
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002701 ret = i915_gem_object_flush_gpu_write_domain(obj);
2702 if (ret != 0)
2703 return ret;
2704
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002705 /* Wait on any GPU rendering and flushing to occur. */
Eric Anholte47c68e2008-11-14 13:35:19 -08002706 ret = i915_gem_object_wait_rendering(obj);
2707 if (ret != 0)
2708 return ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002709
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002710 old_write_domain = obj->write_domain;
2711 old_read_domains = obj->read_domains;
2712
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002713 /* If we're writing through the GTT domain, then CPU and GPU caches
2714 * will need to be invalidated at next use.
2715 */
2716 if (write)
Eric Anholte47c68e2008-11-14 13:35:19 -08002717 obj->read_domains &= I915_GEM_DOMAIN_GTT;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002718
Eric Anholte47c68e2008-11-14 13:35:19 -08002719 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002720
2721 /* It should now be out of any other write domains, and we can update
2722 * the domain values for our changes.
2723 */
2724 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2725 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002726 if (write) {
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002727 obj->write_domain = I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002728 obj_priv->dirty = 1;
2729 }
2730
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002731 trace_i915_gem_object_change_domain(obj,
2732 old_read_domains,
2733 old_write_domain);
2734
Eric Anholte47c68e2008-11-14 13:35:19 -08002735 return 0;
2736}
2737
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002738/*
2739 * Prepare buffer for display plane. Use uninterruptible for possible flush
2740 * wait, as in modesetting process we're not supposed to be interrupted.
2741 */
2742int
2743i915_gem_object_set_to_display_plane(struct drm_gem_object *obj)
2744{
2745 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01002746 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002747 uint32_t old_write_domain, old_read_domains;
2748 int ret;
2749
2750 /* Not valid to be called on unbound objects. */
2751 if (obj_priv->gtt_space == NULL)
2752 return -EINVAL;
2753
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002754 ret = i915_gem_object_flush_gpu_write_domain(obj);
2755 if (ret)
2756 return ret;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002757
2758 /* Wait on any GPU rendering and flushing to occur. */
2759 if (obj_priv->active) {
2760#if WATCH_BUF
2761 DRM_INFO("%s: object %p wait for seqno %08x\n",
2762 __func__, obj, obj_priv->last_rendering_seqno);
2763#endif
Zou Nan hai852835f2010-05-21 09:08:56 +08002764 ret = i915_do_wait_request(dev,
2765 obj_priv->last_rendering_seqno,
2766 0,
2767 obj_priv->ring);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002768 if (ret != 0)
2769 return ret;
2770 }
2771
Chris Wilsonb118c1e2010-05-27 13:18:14 +01002772 i915_gem_object_flush_cpu_write_domain(obj);
2773
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002774 old_write_domain = obj->write_domain;
2775 old_read_domains = obj->read_domains;
2776
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002777 /* It should now be out of any other write domains, and we can update
2778 * the domain values for our changes.
2779 */
2780 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
Chris Wilsonb118c1e2010-05-27 13:18:14 +01002781 obj->read_domains = I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002782 obj->write_domain = I915_GEM_DOMAIN_GTT;
2783 obj_priv->dirty = 1;
2784
2785 trace_i915_gem_object_change_domain(obj,
2786 old_read_domains,
2787 old_write_domain);
2788
2789 return 0;
2790}
2791
Eric Anholte47c68e2008-11-14 13:35:19 -08002792/**
2793 * Moves a single object to the CPU read, and possibly write domain.
2794 *
2795 * This function returns when the move is complete, including waiting on
2796 * flushes to occur.
2797 */
2798static int
2799i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj, int write)
2800{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002801 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002802 int ret;
2803
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002804 ret = i915_gem_object_flush_gpu_write_domain(obj);
2805 if (ret)
2806 return ret;
2807
Eric Anholte47c68e2008-11-14 13:35:19 -08002808 /* Wait on any GPU rendering and flushing to occur. */
2809 ret = i915_gem_object_wait_rendering(obj);
2810 if (ret != 0)
2811 return ret;
2812
2813 i915_gem_object_flush_gtt_write_domain(obj);
2814
2815 /* If we have a partially-valid cache of the object in the CPU,
2816 * finish invalidating it and free the per-page flags.
2817 */
2818 i915_gem_object_set_to_full_cpu_read_domain(obj);
2819
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002820 old_write_domain = obj->write_domain;
2821 old_read_domains = obj->read_domains;
2822
Eric Anholte47c68e2008-11-14 13:35:19 -08002823 /* Flush the CPU cache if it's still invalid. */
2824 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
2825 i915_gem_clflush_object(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08002826
2827 obj->read_domains |= I915_GEM_DOMAIN_CPU;
2828 }
2829
2830 /* It should now be out of any other write domains, and we can update
2831 * the domain values for our changes.
2832 */
2833 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
2834
2835 /* If we're writing through the CPU, then the GPU read domains will
2836 * need to be invalidated at next use.
2837 */
2838 if (write) {
2839 obj->read_domains &= I915_GEM_DOMAIN_CPU;
2840 obj->write_domain = I915_GEM_DOMAIN_CPU;
2841 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002842
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002843 trace_i915_gem_object_change_domain(obj,
2844 old_read_domains,
2845 old_write_domain);
2846
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002847 return 0;
2848}
2849
Eric Anholt673a3942008-07-30 12:06:12 -07002850/*
2851 * Set the next domain for the specified object. This
2852 * may not actually perform the necessary flushing/invaliding though,
2853 * as that may want to be batched with other set_domain operations
2854 *
2855 * This is (we hope) the only really tricky part of gem. The goal
2856 * is fairly simple -- track which caches hold bits of the object
2857 * and make sure they remain coherent. A few concrete examples may
2858 * help to explain how it works. For shorthand, we use the notation
2859 * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
2860 * a pair of read and write domain masks.
2861 *
2862 * Case 1: the batch buffer
2863 *
2864 * 1. Allocated
2865 * 2. Written by CPU
2866 * 3. Mapped to GTT
2867 * 4. Read by GPU
2868 * 5. Unmapped from GTT
2869 * 6. Freed
2870 *
2871 * Let's take these a step at a time
2872 *
2873 * 1. Allocated
2874 * Pages allocated from the kernel may still have
2875 * cache contents, so we set them to (CPU, CPU) always.
2876 * 2. Written by CPU (using pwrite)
2877 * The pwrite function calls set_domain (CPU, CPU) and
2878 * this function does nothing (as nothing changes)
2879 * 3. Mapped by GTT
2880 * This function asserts that the object is not
2881 * currently in any GPU-based read or write domains
2882 * 4. Read by GPU
2883 * i915_gem_execbuffer calls set_domain (COMMAND, 0).
2884 * As write_domain is zero, this function adds in the
2885 * current read domains (CPU+COMMAND, 0).
2886 * flush_domains is set to CPU.
2887 * invalidate_domains is set to COMMAND
2888 * clflush is run to get data out of the CPU caches
2889 * then i915_dev_set_domain calls i915_gem_flush to
2890 * emit an MI_FLUSH and drm_agp_chipset_flush
2891 * 5. Unmapped from GTT
2892 * i915_gem_object_unbind calls set_domain (CPU, CPU)
2893 * flush_domains and invalidate_domains end up both zero
2894 * so no flushing/invalidating happens
2895 * 6. Freed
2896 * yay, done
2897 *
2898 * Case 2: The shared render buffer
2899 *
2900 * 1. Allocated
2901 * 2. Mapped to GTT
2902 * 3. Read/written by GPU
2903 * 4. set_domain to (CPU,CPU)
2904 * 5. Read/written by CPU
2905 * 6. Read/written by GPU
2906 *
2907 * 1. Allocated
2908 * Same as last example, (CPU, CPU)
2909 * 2. Mapped to GTT
2910 * Nothing changes (assertions find that it is not in the GPU)
2911 * 3. Read/written by GPU
2912 * execbuffer calls set_domain (RENDER, RENDER)
2913 * flush_domains gets CPU
2914 * invalidate_domains gets GPU
2915 * clflush (obj)
2916 * MI_FLUSH and drm_agp_chipset_flush
2917 * 4. set_domain (CPU, CPU)
2918 * flush_domains gets GPU
2919 * invalidate_domains gets CPU
2920 * wait_rendering (obj) to make sure all drawing is complete.
2921 * This will include an MI_FLUSH to get the data from GPU
2922 * to memory
2923 * clflush (obj) to invalidate the CPU cache
2924 * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
2925 * 5. Read/written by CPU
2926 * cache lines are loaded and dirtied
2927 * 6. Read written by GPU
2928 * Same as last GPU access
2929 *
2930 * Case 3: The constant buffer
2931 *
2932 * 1. Allocated
2933 * 2. Written by CPU
2934 * 3. Read by GPU
2935 * 4. Updated (written) by CPU again
2936 * 5. Read by GPU
2937 *
2938 * 1. Allocated
2939 * (CPU, CPU)
2940 * 2. Written by CPU
2941 * (CPU, CPU)
2942 * 3. Read by GPU
2943 * (CPU+RENDER, 0)
2944 * flush_domains = CPU
2945 * invalidate_domains = RENDER
2946 * clflush (obj)
2947 * MI_FLUSH
2948 * drm_agp_chipset_flush
2949 * 4. Updated (written) by CPU again
2950 * (CPU, CPU)
2951 * flush_domains = 0 (no previous write domain)
2952 * invalidate_domains = 0 (no new read domains)
2953 * 5. Read by GPU
2954 * (CPU+RENDER, 0)
2955 * flush_domains = CPU
2956 * invalidate_domains = RENDER
2957 * clflush (obj)
2958 * MI_FLUSH
2959 * drm_agp_chipset_flush
2960 */
Keith Packardc0d90822008-11-20 23:11:08 -08002961static void
Eric Anholt8b0e3782009-02-19 14:40:50 -08002962i915_gem_object_set_to_gpu_domain(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002963{
2964 struct drm_device *dev = obj->dev;
Chris Wilson88f356b2010-08-04 13:55:32 +01002965 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002966 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002967 uint32_t invalidate_domains = 0;
2968 uint32_t flush_domains = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002969 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002970
Eric Anholt8b0e3782009-02-19 14:40:50 -08002971 BUG_ON(obj->pending_read_domains & I915_GEM_DOMAIN_CPU);
2972 BUG_ON(obj->pending_write_domain == I915_GEM_DOMAIN_CPU);
Eric Anholt673a3942008-07-30 12:06:12 -07002973
Jesse Barnes652c3932009-08-17 13:31:43 -07002974 intel_mark_busy(dev, obj);
2975
Eric Anholt673a3942008-07-30 12:06:12 -07002976#if WATCH_BUF
2977 DRM_INFO("%s: object %p read %08x -> %08x write %08x -> %08x\n",
2978 __func__, obj,
Eric Anholt8b0e3782009-02-19 14:40:50 -08002979 obj->read_domains, obj->pending_read_domains,
2980 obj->write_domain, obj->pending_write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07002981#endif
2982 /*
2983 * If the object isn't moving to a new write domain,
2984 * let the object stay in multiple read domains
2985 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08002986 if (obj->pending_write_domain == 0)
2987 obj->pending_read_domains |= obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07002988 else
2989 obj_priv->dirty = 1;
2990
2991 /*
2992 * Flush the current write domain if
2993 * the new read domains don't match. Invalidate
2994 * any read domains which differ from the old
2995 * write domain
2996 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08002997 if (obj->write_domain &&
2998 obj->write_domain != obj->pending_read_domains) {
Eric Anholt673a3942008-07-30 12:06:12 -07002999 flush_domains |= obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08003000 invalidate_domains |=
3001 obj->pending_read_domains & ~obj->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07003002 }
3003 /*
3004 * Invalidate any read caches which may have
3005 * stale data. That is, any new read domains.
3006 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003007 invalidate_domains |= obj->pending_read_domains & ~obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07003008 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU) {
3009#if WATCH_BUF
3010 DRM_INFO("%s: CPU domain flush %08x invalidate %08x\n",
3011 __func__, flush_domains, invalidate_domains);
3012#endif
Eric Anholt673a3942008-07-30 12:06:12 -07003013 i915_gem_clflush_object(obj);
3014 }
3015
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003016 old_read_domains = obj->read_domains;
3017
Eric Anholtefbeed92009-02-19 14:54:51 -08003018 /* The actual obj->write_domain will be updated with
3019 * pending_write_domain after we emit the accumulated flush for all
3020 * of our domain changes in execbuffers (which clears objects'
3021 * write_domains). So if we have a current write domain that we
3022 * aren't changing, set pending_write_domain to that.
3023 */
3024 if (flush_domains == 0 && obj->pending_write_domain == 0)
3025 obj->pending_write_domain = obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08003026 obj->read_domains = obj->pending_read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07003027
Chris Wilson88f356b2010-08-04 13:55:32 +01003028 if (flush_domains & I915_GEM_GPU_DOMAINS) {
3029 if (obj_priv->ring == &dev_priv->render_ring)
3030 dev_priv->flush_rings |= FLUSH_RENDER_RING;
3031 else if (obj_priv->ring == &dev_priv->bsd_ring)
3032 dev_priv->flush_rings |= FLUSH_BSD_RING;
3033 }
3034
Eric Anholt673a3942008-07-30 12:06:12 -07003035 dev->invalidate_domains |= invalidate_domains;
3036 dev->flush_domains |= flush_domains;
3037#if WATCH_BUF
3038 DRM_INFO("%s: read %08x write %08x invalidate %08x flush %08x\n",
3039 __func__,
3040 obj->read_domains, obj->write_domain,
3041 dev->invalidate_domains, dev->flush_domains);
3042#endif
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003043
3044 trace_i915_gem_object_change_domain(obj,
3045 old_read_domains,
3046 obj->write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07003047}
3048
3049/**
Eric Anholte47c68e2008-11-14 13:35:19 -08003050 * Moves the object from a partially CPU read to a full one.
Eric Anholt673a3942008-07-30 12:06:12 -07003051 *
Eric Anholte47c68e2008-11-14 13:35:19 -08003052 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
3053 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
3054 */
3055static void
3056i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj)
3057{
Daniel Vetter23010e42010-03-08 13:35:02 +01003058 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003059
3060 if (!obj_priv->page_cpu_valid)
3061 return;
3062
3063 /* If we're partially in the CPU read domain, finish moving it in.
3064 */
3065 if (obj->read_domains & I915_GEM_DOMAIN_CPU) {
3066 int i;
3067
3068 for (i = 0; i <= (obj->size - 1) / PAGE_SIZE; i++) {
3069 if (obj_priv->page_cpu_valid[i])
3070 continue;
Eric Anholt856fa192009-03-19 14:10:50 -07003071 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholte47c68e2008-11-14 13:35:19 -08003072 }
Eric Anholte47c68e2008-11-14 13:35:19 -08003073 }
3074
3075 /* Free the page_cpu_valid mappings which are now stale, whether
3076 * or not we've got I915_GEM_DOMAIN_CPU.
3077 */
Eric Anholt9a298b22009-03-24 12:23:04 -07003078 kfree(obj_priv->page_cpu_valid);
Eric Anholte47c68e2008-11-14 13:35:19 -08003079 obj_priv->page_cpu_valid = NULL;
3080}
3081
3082/**
3083 * Set the CPU read domain on a range of the object.
3084 *
3085 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
3086 * not entirely valid. The page_cpu_valid member of the object flags which
3087 * pages have been flushed, and will be respected by
3088 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
3089 * of the whole object.
3090 *
3091 * This function returns when the move is complete, including waiting on
3092 * flushes to occur.
Eric Anholt673a3942008-07-30 12:06:12 -07003093 */
3094static int
Eric Anholte47c68e2008-11-14 13:35:19 -08003095i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
3096 uint64_t offset, uint64_t size)
Eric Anholt673a3942008-07-30 12:06:12 -07003097{
Daniel Vetter23010e42010-03-08 13:35:02 +01003098 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003099 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003100 int i, ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003101
Eric Anholte47c68e2008-11-14 13:35:19 -08003102 if (offset == 0 && size == obj->size)
3103 return i915_gem_object_set_to_cpu_domain(obj, 0);
3104
Chris Wilson2dafb1e2010-06-07 14:03:05 +01003105 ret = i915_gem_object_flush_gpu_write_domain(obj);
3106 if (ret)
3107 return ret;
3108
Eric Anholte47c68e2008-11-14 13:35:19 -08003109 /* Wait on any GPU rendering and flushing to occur. */
3110 ret = i915_gem_object_wait_rendering(obj);
3111 if (ret != 0)
3112 return ret;
3113 i915_gem_object_flush_gtt_write_domain(obj);
3114
3115 /* If we're already fully in the CPU read domain, we're done. */
3116 if (obj_priv->page_cpu_valid == NULL &&
3117 (obj->read_domains & I915_GEM_DOMAIN_CPU) != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07003118 return 0;
3119
Eric Anholte47c68e2008-11-14 13:35:19 -08003120 /* Otherwise, create/clear the per-page CPU read domain flag if we're
3121 * newly adding I915_GEM_DOMAIN_CPU
3122 */
Eric Anholt673a3942008-07-30 12:06:12 -07003123 if (obj_priv->page_cpu_valid == NULL) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003124 obj_priv->page_cpu_valid = kzalloc(obj->size / PAGE_SIZE,
3125 GFP_KERNEL);
Eric Anholte47c68e2008-11-14 13:35:19 -08003126 if (obj_priv->page_cpu_valid == NULL)
3127 return -ENOMEM;
3128 } else if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0)
3129 memset(obj_priv->page_cpu_valid, 0, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07003130
3131 /* Flush the cache on any pages that are still invalid from the CPU's
3132 * perspective.
3133 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003134 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
3135 i++) {
Eric Anholt673a3942008-07-30 12:06:12 -07003136 if (obj_priv->page_cpu_valid[i])
3137 continue;
3138
Eric Anholt856fa192009-03-19 14:10:50 -07003139 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07003140
3141 obj_priv->page_cpu_valid[i] = 1;
3142 }
3143
Eric Anholte47c68e2008-11-14 13:35:19 -08003144 /* It should now be out of any other write domains, and we can update
3145 * the domain values for our changes.
3146 */
3147 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
3148
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003149 old_read_domains = obj->read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003150 obj->read_domains |= I915_GEM_DOMAIN_CPU;
3151
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003152 trace_i915_gem_object_change_domain(obj,
3153 old_read_domains,
3154 obj->write_domain);
3155
Eric Anholt673a3942008-07-30 12:06:12 -07003156 return 0;
3157}
3158
3159/**
Eric Anholt673a3942008-07-30 12:06:12 -07003160 * Pin an object to the GTT and evaluate the relocations landing in it.
3161 */
3162static int
3163i915_gem_object_pin_and_relocate(struct drm_gem_object *obj,
3164 struct drm_file *file_priv,
Jesse Barnes76446ca2009-12-17 22:05:42 -05003165 struct drm_i915_gem_exec_object2 *entry,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003166 struct drm_i915_gem_relocation_entry *relocs)
Eric Anholt673a3942008-07-30 12:06:12 -07003167{
3168 struct drm_device *dev = obj->dev;
Keith Packard0839ccb2008-10-30 19:38:48 -07003169 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01003170 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003171 int i, ret;
Keith Packard0839ccb2008-10-30 19:38:48 -07003172 void __iomem *reloc_page;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003173 bool need_fence;
3174
3175 need_fence = entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
3176 obj_priv->tiling_mode != I915_TILING_NONE;
3177
3178 /* Check fence reg constraints and rebind if necessary */
Chris Wilson808b24d62010-05-27 13:18:15 +01003179 if (need_fence &&
3180 !i915_gem_object_fence_offset_ok(obj,
3181 obj_priv->tiling_mode)) {
3182 ret = i915_gem_object_unbind(obj);
3183 if (ret)
3184 return ret;
3185 }
Eric Anholt673a3942008-07-30 12:06:12 -07003186
3187 /* Choose the GTT offset for our buffer and put it there. */
3188 ret = i915_gem_object_pin(obj, (uint32_t) entry->alignment);
3189 if (ret)
3190 return ret;
3191
Jesse Barnes76446ca2009-12-17 22:05:42 -05003192 /*
3193 * Pre-965 chips need a fence register set up in order to
3194 * properly handle blits to/from tiled surfaces.
3195 */
3196 if (need_fence) {
3197 ret = i915_gem_object_get_fence_reg(obj);
3198 if (ret != 0) {
Jesse Barnes76446ca2009-12-17 22:05:42 -05003199 i915_gem_object_unpin(obj);
3200 return ret;
3201 }
3202 }
3203
Eric Anholt673a3942008-07-30 12:06:12 -07003204 entry->offset = obj_priv->gtt_offset;
3205
Eric Anholt673a3942008-07-30 12:06:12 -07003206 /* Apply the relocations, using the GTT aperture to avoid cache
3207 * flushing requirements.
3208 */
3209 for (i = 0; i < entry->relocation_count; i++) {
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003210 struct drm_i915_gem_relocation_entry *reloc= &relocs[i];
Eric Anholt673a3942008-07-30 12:06:12 -07003211 struct drm_gem_object *target_obj;
3212 struct drm_i915_gem_object *target_obj_priv;
Eric Anholt3043c602008-10-02 12:24:47 -07003213 uint32_t reloc_val, reloc_offset;
3214 uint32_t __iomem *reloc_entry;
Eric Anholt673a3942008-07-30 12:06:12 -07003215
Eric Anholt673a3942008-07-30 12:06:12 -07003216 target_obj = drm_gem_object_lookup(obj->dev, file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003217 reloc->target_handle);
Eric Anholt673a3942008-07-30 12:06:12 -07003218 if (target_obj == NULL) {
3219 i915_gem_object_unpin(obj);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01003220 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07003221 }
Daniel Vetter23010e42010-03-08 13:35:02 +01003222 target_obj_priv = to_intel_bo(target_obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003223
Chris Wilson8542a0b2009-09-09 21:15:15 +01003224#if WATCH_RELOC
3225 DRM_INFO("%s: obj %p offset %08x target %d "
3226 "read %08x write %08x gtt %08x "
3227 "presumed %08x delta %08x\n",
3228 __func__,
3229 obj,
3230 (int) reloc->offset,
3231 (int) reloc->target_handle,
3232 (int) reloc->read_domains,
3233 (int) reloc->write_domain,
3234 (int) target_obj_priv->gtt_offset,
3235 (int) reloc->presumed_offset,
3236 reloc->delta);
3237#endif
3238
Eric Anholt673a3942008-07-30 12:06:12 -07003239 /* The target buffer should have appeared before us in the
3240 * exec_object list, so it should have a GTT space bound by now.
3241 */
3242 if (target_obj_priv->gtt_space == NULL) {
3243 DRM_ERROR("No GTT space found for object %d\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003244 reloc->target_handle);
Eric Anholt673a3942008-07-30 12:06:12 -07003245 drm_gem_object_unreference(target_obj);
3246 i915_gem_object_unpin(obj);
3247 return -EINVAL;
3248 }
3249
Chris Wilson8542a0b2009-09-09 21:15:15 +01003250 /* Validate that the target is in a valid r/w GPU domain */
Daniel Vetter16edd552010-02-19 11:52:02 +01003251 if (reloc->write_domain & (reloc->write_domain - 1)) {
3252 DRM_ERROR("reloc with multiple write domains: "
3253 "obj %p target %d offset %d "
3254 "read %08x write %08x",
3255 obj, reloc->target_handle,
3256 (int) reloc->offset,
3257 reloc->read_domains,
3258 reloc->write_domain);
3259 return -EINVAL;
3260 }
Chris Wilson8542a0b2009-09-09 21:15:15 +01003261 if (reloc->write_domain & I915_GEM_DOMAIN_CPU ||
3262 reloc->read_domains & I915_GEM_DOMAIN_CPU) {
3263 DRM_ERROR("reloc with read/write CPU domains: "
3264 "obj %p target %d offset %d "
3265 "read %08x write %08x",
3266 obj, reloc->target_handle,
3267 (int) reloc->offset,
3268 reloc->read_domains,
3269 reloc->write_domain);
3270 drm_gem_object_unreference(target_obj);
3271 i915_gem_object_unpin(obj);
3272 return -EINVAL;
3273 }
3274 if (reloc->write_domain && target_obj->pending_write_domain &&
3275 reloc->write_domain != target_obj->pending_write_domain) {
3276 DRM_ERROR("Write domain conflict: "
3277 "obj %p target %d offset %d "
3278 "new %08x old %08x\n",
3279 obj, reloc->target_handle,
3280 (int) reloc->offset,
3281 reloc->write_domain,
3282 target_obj->pending_write_domain);
3283 drm_gem_object_unreference(target_obj);
3284 i915_gem_object_unpin(obj);
3285 return -EINVAL;
3286 }
3287
3288 target_obj->pending_read_domains |= reloc->read_domains;
3289 target_obj->pending_write_domain |= reloc->write_domain;
3290
3291 /* If the relocation already has the right value in it, no
3292 * more work needs to be done.
3293 */
3294 if (target_obj_priv->gtt_offset == reloc->presumed_offset) {
3295 drm_gem_object_unreference(target_obj);
3296 continue;
3297 }
3298
3299 /* Check that the relocation address is valid... */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003300 if (reloc->offset > obj->size - 4) {
Eric Anholt673a3942008-07-30 12:06:12 -07003301 DRM_ERROR("Relocation beyond object bounds: "
3302 "obj %p target %d offset %d size %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003303 obj, reloc->target_handle,
3304 (int) reloc->offset, (int) obj->size);
Eric Anholt673a3942008-07-30 12:06:12 -07003305 drm_gem_object_unreference(target_obj);
3306 i915_gem_object_unpin(obj);
3307 return -EINVAL;
3308 }
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003309 if (reloc->offset & 3) {
Eric Anholt673a3942008-07-30 12:06:12 -07003310 DRM_ERROR("Relocation not 4-byte aligned: "
3311 "obj %p target %d offset %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003312 obj, reloc->target_handle,
3313 (int) reloc->offset);
Eric Anholt673a3942008-07-30 12:06:12 -07003314 drm_gem_object_unreference(target_obj);
3315 i915_gem_object_unpin(obj);
3316 return -EINVAL;
3317 }
3318
Chris Wilson8542a0b2009-09-09 21:15:15 +01003319 /* and points to somewhere within the target object. */
Chris Wilsoncd0b9fb2009-09-15 23:23:18 +01003320 if (reloc->delta >= target_obj->size) {
3321 DRM_ERROR("Relocation beyond target object bounds: "
3322 "obj %p target %d delta %d size %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003323 obj, reloc->target_handle,
Chris Wilsoncd0b9fb2009-09-15 23:23:18 +01003324 (int) reloc->delta, (int) target_obj->size);
Chris Wilson491152b2009-02-11 14:26:32 +00003325 drm_gem_object_unreference(target_obj);
3326 i915_gem_object_unpin(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003327 return -EINVAL;
3328 }
3329
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003330 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
3331 if (ret != 0) {
3332 drm_gem_object_unreference(target_obj);
3333 i915_gem_object_unpin(obj);
3334 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -07003335 }
3336
3337 /* Map the page containing the relocation we're going to
3338 * perform.
3339 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003340 reloc_offset = obj_priv->gtt_offset + reloc->offset;
Keith Packard0839ccb2008-10-30 19:38:48 -07003341 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
3342 (reloc_offset &
Chris Wilsonfca3ec02010-08-04 14:34:24 +01003343 ~(PAGE_SIZE - 1)),
3344 KM_USER0);
Eric Anholt3043c602008-10-02 12:24:47 -07003345 reloc_entry = (uint32_t __iomem *)(reloc_page +
Keith Packard0839ccb2008-10-30 19:38:48 -07003346 (reloc_offset & (PAGE_SIZE - 1)));
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003347 reloc_val = target_obj_priv->gtt_offset + reloc->delta;
Eric Anholt673a3942008-07-30 12:06:12 -07003348
3349#if WATCH_BUF
3350 DRM_INFO("Applied relocation: %p@0x%08x %08x -> %08x\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003351 obj, (unsigned int) reloc->offset,
Eric Anholt673a3942008-07-30 12:06:12 -07003352 readl(reloc_entry), reloc_val);
3353#endif
3354 writel(reloc_val, reloc_entry);
Chris Wilsonfca3ec02010-08-04 14:34:24 +01003355 io_mapping_unmap_atomic(reloc_page, KM_USER0);
Eric Anholt673a3942008-07-30 12:06:12 -07003356
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003357 /* The updated presumed offset for this entry will be
3358 * copied back out to the user.
Eric Anholt673a3942008-07-30 12:06:12 -07003359 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003360 reloc->presumed_offset = target_obj_priv->gtt_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003361
3362 drm_gem_object_unreference(target_obj);
3363 }
3364
Eric Anholt673a3942008-07-30 12:06:12 -07003365#if WATCH_BUF
3366 if (0)
3367 i915_gem_dump_object(obj, 128, __func__, ~0);
3368#endif
3369 return 0;
3370}
3371
Eric Anholt673a3942008-07-30 12:06:12 -07003372/* Throttle our rendering by waiting until the ring has completed our requests
3373 * emitted over 20 msec ago.
3374 *
Eric Anholtb9624422009-06-03 07:27:35 +00003375 * Note that if we were to use the current jiffies each time around the loop,
3376 * we wouldn't escape the function with any frames outstanding if the time to
3377 * render a frame was over 20ms.
3378 *
Eric Anholt673a3942008-07-30 12:06:12 -07003379 * This should get us reasonable parallelism between CPU and GPU but also
3380 * relatively low latency when blocking on a particular request to finish.
3381 */
3382static int
3383i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file_priv)
3384{
3385 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
3386 int ret = 0;
Eric Anholtb9624422009-06-03 07:27:35 +00003387 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
Eric Anholt673a3942008-07-30 12:06:12 -07003388
3389 mutex_lock(&dev->struct_mutex);
Eric Anholtb9624422009-06-03 07:27:35 +00003390 while (!list_empty(&i915_file_priv->mm.request_list)) {
3391 struct drm_i915_gem_request *request;
3392
3393 request = list_first_entry(&i915_file_priv->mm.request_list,
3394 struct drm_i915_gem_request,
3395 client_list);
3396
3397 if (time_after_eq(request->emitted_jiffies, recent_enough))
3398 break;
3399
Zou Nan hai852835f2010-05-21 09:08:56 +08003400 ret = i915_wait_request(dev, request->seqno, request->ring);
Eric Anholtb9624422009-06-03 07:27:35 +00003401 if (ret != 0)
3402 break;
3403 }
Eric Anholt673a3942008-07-30 12:06:12 -07003404 mutex_unlock(&dev->struct_mutex);
Eric Anholtb9624422009-06-03 07:27:35 +00003405
Eric Anholt673a3942008-07-30 12:06:12 -07003406 return ret;
3407}
3408
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003409static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003410i915_gem_get_relocs_from_user(struct drm_i915_gem_exec_object2 *exec_list,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003411 uint32_t buffer_count,
3412 struct drm_i915_gem_relocation_entry **relocs)
3413{
3414 uint32_t reloc_count = 0, reloc_index = 0, i;
3415 int ret;
3416
3417 *relocs = NULL;
3418 for (i = 0; i < buffer_count; i++) {
3419 if (reloc_count + exec_list[i].relocation_count < reloc_count)
3420 return -EINVAL;
3421 reloc_count += exec_list[i].relocation_count;
3422 }
3423
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003424 *relocs = drm_calloc_large(reloc_count, sizeof(**relocs));
Jesse Barnes76446ca2009-12-17 22:05:42 -05003425 if (*relocs == NULL) {
3426 DRM_ERROR("failed to alloc relocs, count %d\n", reloc_count);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003427 return -ENOMEM;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003428 }
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003429
3430 for (i = 0; i < buffer_count; i++) {
3431 struct drm_i915_gem_relocation_entry __user *user_relocs;
3432
3433 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3434
3435 ret = copy_from_user(&(*relocs)[reloc_index],
3436 user_relocs,
3437 exec_list[i].relocation_count *
3438 sizeof(**relocs));
3439 if (ret != 0) {
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003440 drm_free_large(*relocs);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003441 *relocs = NULL;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003442 return -EFAULT;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003443 }
3444
3445 reloc_index += exec_list[i].relocation_count;
3446 }
3447
Florian Mickler2bc43b52009-04-06 22:55:41 +02003448 return 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003449}
3450
3451static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003452i915_gem_put_relocs_to_user(struct drm_i915_gem_exec_object2 *exec_list,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003453 uint32_t buffer_count,
3454 struct drm_i915_gem_relocation_entry *relocs)
3455{
3456 uint32_t reloc_count = 0, i;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003457 int ret = 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003458
Chris Wilson93533c22010-01-31 10:40:48 +00003459 if (relocs == NULL)
3460 return 0;
3461
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003462 for (i = 0; i < buffer_count; i++) {
3463 struct drm_i915_gem_relocation_entry __user *user_relocs;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003464 int unwritten;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003465
3466 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3467
Florian Mickler2bc43b52009-04-06 22:55:41 +02003468 unwritten = copy_to_user(user_relocs,
3469 &relocs[reloc_count],
3470 exec_list[i].relocation_count *
3471 sizeof(*relocs));
3472
3473 if (unwritten) {
3474 ret = -EFAULT;
3475 goto err;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003476 }
3477
3478 reloc_count += exec_list[i].relocation_count;
3479 }
3480
Florian Mickler2bc43b52009-04-06 22:55:41 +02003481err:
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003482 drm_free_large(relocs);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003483
3484 return ret;
3485}
3486
Chris Wilson83d60792009-06-06 09:45:57 +01003487static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003488i915_gem_check_execbuffer (struct drm_i915_gem_execbuffer2 *exec,
Chris Wilson83d60792009-06-06 09:45:57 +01003489 uint64_t exec_offset)
3490{
3491 uint32_t exec_start, exec_len;
3492
3493 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
3494 exec_len = (uint32_t) exec->batch_len;
3495
3496 if ((exec_start | exec_len) & 0x7)
3497 return -EINVAL;
3498
3499 if (!exec_start)
3500 return -EINVAL;
3501
3502 return 0;
3503}
3504
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003505static int
3506i915_gem_wait_for_pending_flip(struct drm_device *dev,
3507 struct drm_gem_object **object_list,
3508 int count)
3509{
3510 drm_i915_private_t *dev_priv = dev->dev_private;
3511 struct drm_i915_gem_object *obj_priv;
3512 DEFINE_WAIT(wait);
3513 int i, ret = 0;
3514
3515 for (;;) {
3516 prepare_to_wait(&dev_priv->pending_flip_queue,
3517 &wait, TASK_INTERRUPTIBLE);
3518 for (i = 0; i < count; i++) {
Daniel Vetter23010e42010-03-08 13:35:02 +01003519 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003520 if (atomic_read(&obj_priv->pending_flip) > 0)
3521 break;
3522 }
3523 if (i == count)
3524 break;
3525
3526 if (!signal_pending(current)) {
3527 mutex_unlock(&dev->struct_mutex);
3528 schedule();
3529 mutex_lock(&dev->struct_mutex);
3530 continue;
3531 }
3532 ret = -ERESTARTSYS;
3533 break;
3534 }
3535 finish_wait(&dev_priv->pending_flip_queue, &wait);
3536
3537 return ret;
3538}
3539
Chris Wilson43b27f42010-07-02 08:57:15 +01003540
Eric Anholt673a3942008-07-30 12:06:12 -07003541int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003542i915_gem_do_execbuffer(struct drm_device *dev, void *data,
3543 struct drm_file *file_priv,
3544 struct drm_i915_gem_execbuffer2 *args,
3545 struct drm_i915_gem_exec_object2 *exec_list)
Eric Anholt673a3942008-07-30 12:06:12 -07003546{
3547 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003548 struct drm_gem_object **object_list = NULL;
3549 struct drm_gem_object *batch_obj;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003550 struct drm_i915_gem_object *obj_priv;
Eric Anholt201361a2009-03-11 12:30:04 -07003551 struct drm_clip_rect *cliprects = NULL;
Chris Wilson93533c22010-01-31 10:40:48 +00003552 struct drm_i915_gem_relocation_entry *relocs = NULL;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003553 int ret = 0, ret2, i, pinned = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003554 uint64_t exec_offset;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003555 uint32_t seqno, flush_domains, reloc_index;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003556 int pin_tries, flips;
Eric Anholt673a3942008-07-30 12:06:12 -07003557
Zou Nan hai852835f2010-05-21 09:08:56 +08003558 struct intel_ring_buffer *ring = NULL;
3559
Eric Anholt673a3942008-07-30 12:06:12 -07003560#if WATCH_EXEC
3561 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3562 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3563#endif
Zou Nan haid1b851f2010-05-21 09:08:57 +08003564 if (args->flags & I915_EXEC_BSD) {
3565 if (!HAS_BSD(dev)) {
3566 DRM_ERROR("execbuf with wrong flag\n");
3567 return -EINVAL;
3568 }
3569 ring = &dev_priv->bsd_ring;
3570 } else {
3571 ring = &dev_priv->render_ring;
3572 }
3573
Eric Anholt4f481ed2008-09-10 14:22:49 -07003574 if (args->buffer_count < 1) {
3575 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3576 return -EINVAL;
3577 }
Eric Anholtc8e0f932009-11-22 03:49:37 +01003578 object_list = drm_malloc_ab(sizeof(*object_list), args->buffer_count);
Jesse Barnes76446ca2009-12-17 22:05:42 -05003579 if (object_list == NULL) {
3580 DRM_ERROR("Failed to allocate object list for %d buffers\n",
Eric Anholt673a3942008-07-30 12:06:12 -07003581 args->buffer_count);
3582 ret = -ENOMEM;
3583 goto pre_mutex_err;
3584 }
Eric Anholt673a3942008-07-30 12:06:12 -07003585
Eric Anholt201361a2009-03-11 12:30:04 -07003586 if (args->num_cliprects != 0) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003587 cliprects = kcalloc(args->num_cliprects, sizeof(*cliprects),
3588 GFP_KERNEL);
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003589 if (cliprects == NULL) {
3590 ret = -ENOMEM;
Eric Anholt201361a2009-03-11 12:30:04 -07003591 goto pre_mutex_err;
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003592 }
Eric Anholt201361a2009-03-11 12:30:04 -07003593
3594 ret = copy_from_user(cliprects,
3595 (struct drm_clip_rect __user *)
3596 (uintptr_t) args->cliprects_ptr,
3597 sizeof(*cliprects) * args->num_cliprects);
3598 if (ret != 0) {
3599 DRM_ERROR("copy %d cliprects failed: %d\n",
3600 args->num_cliprects, ret);
Dan Carpenterc877cdc2010-06-23 19:03:01 +02003601 ret = -EFAULT;
Eric Anholt201361a2009-03-11 12:30:04 -07003602 goto pre_mutex_err;
3603 }
3604 }
3605
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003606 ret = i915_gem_get_relocs_from_user(exec_list, args->buffer_count,
3607 &relocs);
3608 if (ret != 0)
3609 goto pre_mutex_err;
3610
Eric Anholt673a3942008-07-30 12:06:12 -07003611 mutex_lock(&dev->struct_mutex);
3612
3613 i915_verify_inactive(dev, __FILE__, __LINE__);
3614
Ben Gamariba1234d2009-09-14 17:48:47 -04003615 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07003616 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003617 ret = -EIO;
3618 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003619 }
3620
3621 if (dev_priv->mm.suspended) {
Eric Anholt673a3942008-07-30 12:06:12 -07003622 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003623 ret = -EBUSY;
3624 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003625 }
3626
Keith Packardac94a962008-11-20 23:30:27 -08003627 /* Look up object handles */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003628 flips = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003629 for (i = 0; i < args->buffer_count; i++) {
3630 object_list[i] = drm_gem_object_lookup(dev, file_priv,
3631 exec_list[i].handle);
3632 if (object_list[i] == NULL) {
3633 DRM_ERROR("Invalid object handle %d at index %d\n",
3634 exec_list[i].handle, i);
Chris Wilson0ce907f2010-01-23 20:26:35 +00003635 /* prevent error path from reading uninitialized data */
3636 args->buffer_count = i + 1;
Chris Wilsonbf79cb92010-08-04 14:19:46 +01003637 ret = -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07003638 goto err;
3639 }
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003640
Daniel Vetter23010e42010-03-08 13:35:02 +01003641 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003642 if (obj_priv->in_execbuffer) {
3643 DRM_ERROR("Object %p appears more than once in object list\n",
3644 object_list[i]);
Chris Wilson0ce907f2010-01-23 20:26:35 +00003645 /* prevent error path from reading uninitialized data */
3646 args->buffer_count = i + 1;
Chris Wilsonbf79cb92010-08-04 14:19:46 +01003647 ret = -EINVAL;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003648 goto err;
3649 }
3650 obj_priv->in_execbuffer = true;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003651 flips += atomic_read(&obj_priv->pending_flip);
3652 }
3653
3654 if (flips > 0) {
3655 ret = i915_gem_wait_for_pending_flip(dev, object_list,
3656 args->buffer_count);
3657 if (ret)
3658 goto err;
Keith Packardac94a962008-11-20 23:30:27 -08003659 }
Eric Anholt673a3942008-07-30 12:06:12 -07003660
Keith Packardac94a962008-11-20 23:30:27 -08003661 /* Pin and relocate */
3662 for (pin_tries = 0; ; pin_tries++) {
3663 ret = 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003664 reloc_index = 0;
3665
Keith Packardac94a962008-11-20 23:30:27 -08003666 for (i = 0; i < args->buffer_count; i++) {
3667 object_list[i]->pending_read_domains = 0;
3668 object_list[i]->pending_write_domain = 0;
3669 ret = i915_gem_object_pin_and_relocate(object_list[i],
3670 file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003671 &exec_list[i],
3672 &relocs[reloc_index]);
Keith Packardac94a962008-11-20 23:30:27 -08003673 if (ret)
3674 break;
3675 pinned = i + 1;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003676 reloc_index += exec_list[i].relocation_count;
Keith Packardac94a962008-11-20 23:30:27 -08003677 }
3678 /* success */
3679 if (ret == 0)
3680 break;
3681
3682 /* error other than GTT full, or we've already tried again */
Chris Wilson2939e1f2009-06-06 09:46:03 +01003683 if (ret != -ENOSPC || pin_tries >= 1) {
Chris Wilson07f73f62009-09-14 16:50:30 +01003684 if (ret != -ERESTARTSYS) {
3685 unsigned long long total_size = 0;
Chris Wilson3d1cc472010-05-27 13:18:19 +01003686 int num_fences = 0;
3687 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson43b27f42010-07-02 08:57:15 +01003688 obj_priv = to_intel_bo(object_list[i]);
Chris Wilson3d1cc472010-05-27 13:18:19 +01003689
Chris Wilson07f73f62009-09-14 16:50:30 +01003690 total_size += object_list[i]->size;
Chris Wilson3d1cc472010-05-27 13:18:19 +01003691 num_fences +=
3692 exec_list[i].flags & EXEC_OBJECT_NEEDS_FENCE &&
3693 obj_priv->tiling_mode != I915_TILING_NONE;
3694 }
3695 DRM_ERROR("Failed to pin buffer %d of %d, total %llu bytes, %d fences: %d\n",
Chris Wilson07f73f62009-09-14 16:50:30 +01003696 pinned+1, args->buffer_count,
Chris Wilson3d1cc472010-05-27 13:18:19 +01003697 total_size, num_fences,
3698 ret);
Chris Wilson07f73f62009-09-14 16:50:30 +01003699 DRM_ERROR("%d objects [%d pinned], "
3700 "%d object bytes [%d pinned], "
3701 "%d/%d gtt bytes\n",
3702 atomic_read(&dev->object_count),
3703 atomic_read(&dev->pin_count),
3704 atomic_read(&dev->object_memory),
3705 atomic_read(&dev->pin_memory),
3706 atomic_read(&dev->gtt_memory),
3707 dev->gtt_total);
3708 }
Eric Anholt673a3942008-07-30 12:06:12 -07003709 goto err;
3710 }
Keith Packardac94a962008-11-20 23:30:27 -08003711
3712 /* unpin all of our buffers */
3713 for (i = 0; i < pinned; i++)
3714 i915_gem_object_unpin(object_list[i]);
Eric Anholtb1177632008-12-10 10:09:41 -08003715 pinned = 0;
Keith Packardac94a962008-11-20 23:30:27 -08003716
3717 /* evict everyone we can from the aperture */
3718 ret = i915_gem_evict_everything(dev);
Chris Wilson07f73f62009-09-14 16:50:30 +01003719 if (ret && ret != -ENOSPC)
Keith Packardac94a962008-11-20 23:30:27 -08003720 goto err;
Eric Anholt673a3942008-07-30 12:06:12 -07003721 }
3722
3723 /* Set the pending read domains for the batch buffer to COMMAND */
3724 batch_obj = object_list[args->buffer_count-1];
Chris Wilson5f26a2c2009-06-06 09:45:58 +01003725 if (batch_obj->pending_write_domain) {
3726 DRM_ERROR("Attempting to use self-modifying batch buffer\n");
3727 ret = -EINVAL;
3728 goto err;
3729 }
3730 batch_obj->pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
Eric Anholt673a3942008-07-30 12:06:12 -07003731
Chris Wilson83d60792009-06-06 09:45:57 +01003732 /* Sanity check the batch buffer, prior to moving objects */
3733 exec_offset = exec_list[args->buffer_count - 1].offset;
3734 ret = i915_gem_check_execbuffer (args, exec_offset);
3735 if (ret != 0) {
3736 DRM_ERROR("execbuf with invalid offset/length\n");
3737 goto err;
3738 }
3739
Eric Anholt673a3942008-07-30 12:06:12 -07003740 i915_verify_inactive(dev, __FILE__, __LINE__);
3741
Keith Packard646f0f62008-11-20 23:23:03 -08003742 /* Zero the global flush/invalidate flags. These
3743 * will be modified as new domains are computed
3744 * for each object
3745 */
3746 dev->invalidate_domains = 0;
3747 dev->flush_domains = 0;
Chris Wilson88f356b2010-08-04 13:55:32 +01003748 dev_priv->flush_rings = 0;
Keith Packard646f0f62008-11-20 23:23:03 -08003749
Eric Anholt673a3942008-07-30 12:06:12 -07003750 for (i = 0; i < args->buffer_count; i++) {
3751 struct drm_gem_object *obj = object_list[i];
Eric Anholt673a3942008-07-30 12:06:12 -07003752
Keith Packard646f0f62008-11-20 23:23:03 -08003753 /* Compute new gpu domains and update invalidate/flush */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003754 i915_gem_object_set_to_gpu_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003755 }
3756
3757 i915_verify_inactive(dev, __FILE__, __LINE__);
3758
Keith Packard646f0f62008-11-20 23:23:03 -08003759 if (dev->invalidate_domains | dev->flush_domains) {
3760#if WATCH_EXEC
3761 DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
3762 __func__,
3763 dev->invalidate_domains,
3764 dev->flush_domains);
3765#endif
3766 i915_gem_flush(dev,
3767 dev->invalidate_domains,
3768 dev->flush_domains);
Chris Wilson88f356b2010-08-04 13:55:32 +01003769 if (dev_priv->flush_rings & FLUSH_RENDER_RING)
Eric Anholtb9624422009-06-03 07:27:35 +00003770 (void)i915_add_request(dev, file_priv,
Chris Wilson88f356b2010-08-04 13:55:32 +01003771 dev->flush_domains,
3772 &dev_priv->render_ring);
3773 if (dev_priv->flush_rings & FLUSH_BSD_RING)
3774 (void)i915_add_request(dev, file_priv,
3775 dev->flush_domains,
3776 &dev_priv->bsd_ring);
Keith Packard646f0f62008-11-20 23:23:03 -08003777 }
Eric Anholt673a3942008-07-30 12:06:12 -07003778
Eric Anholtefbeed92009-02-19 14:54:51 -08003779 for (i = 0; i < args->buffer_count; i++) {
3780 struct drm_gem_object *obj = object_list[i];
Daniel Vetter23010e42010-03-08 13:35:02 +01003781 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003782 uint32_t old_write_domain = obj->write_domain;
Eric Anholtefbeed92009-02-19 14:54:51 -08003783
3784 obj->write_domain = obj->pending_write_domain;
Daniel Vetter99fcb762010-02-07 16:20:18 +01003785 if (obj->write_domain)
3786 list_move_tail(&obj_priv->gpu_write_list,
3787 &dev_priv->mm.gpu_write_list);
3788 else
3789 list_del_init(&obj_priv->gpu_write_list);
3790
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003791 trace_i915_gem_object_change_domain(obj,
3792 obj->read_domains,
3793 old_write_domain);
Eric Anholtefbeed92009-02-19 14:54:51 -08003794 }
3795
Eric Anholt673a3942008-07-30 12:06:12 -07003796 i915_verify_inactive(dev, __FILE__, __LINE__);
3797
3798#if WATCH_COHERENCY
3799 for (i = 0; i < args->buffer_count; i++) {
3800 i915_gem_object_check_coherency(object_list[i],
3801 exec_list[i].handle);
3802 }
3803#endif
3804
Eric Anholt673a3942008-07-30 12:06:12 -07003805#if WATCH_EXEC
Ben Gamari6911a9b2009-04-02 11:24:54 -07003806 i915_gem_dump_object(batch_obj,
Eric Anholt673a3942008-07-30 12:06:12 -07003807 args->batch_len,
3808 __func__,
3809 ~0);
3810#endif
3811
Eric Anholt673a3942008-07-30 12:06:12 -07003812 /* Exec the batchbuffer */
Zou Nan hai852835f2010-05-21 09:08:56 +08003813 ret = ring->dispatch_gem_execbuffer(dev, ring, args,
3814 cliprects, exec_offset);
Eric Anholt673a3942008-07-30 12:06:12 -07003815 if (ret) {
3816 DRM_ERROR("dispatch failed %d\n", ret);
3817 goto err;
3818 }
3819
3820 /*
3821 * Ensure that the commands in the batch buffer are
3822 * finished before the interrupt fires
3823 */
Zou Nan hai852835f2010-05-21 09:08:56 +08003824 flush_domains = i915_retire_commands(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07003825
3826 i915_verify_inactive(dev, __FILE__, __LINE__);
3827
3828 /*
3829 * Get a seqno representing the execution of the current buffer,
3830 * which we can wait on. We would like to mitigate these interrupts,
3831 * likely by only creating seqnos occasionally (so that we have
3832 * *some* interrupts representing completion of buffers that we can
3833 * wait on when trying to clear up gtt space).
3834 */
Zou Nan hai852835f2010-05-21 09:08:56 +08003835 seqno = i915_add_request(dev, file_priv, flush_domains, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07003836 BUG_ON(seqno == 0);
Eric Anholt673a3942008-07-30 12:06:12 -07003837 for (i = 0; i < args->buffer_count; i++) {
3838 struct drm_gem_object *obj = object_list[i];
Zou Nan hai852835f2010-05-21 09:08:56 +08003839 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003840
Zou Nan hai852835f2010-05-21 09:08:56 +08003841 i915_gem_object_move_to_active(obj, seqno, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07003842#if WATCH_LRU
3843 DRM_INFO("%s: move to exec list %p\n", __func__, obj);
3844#endif
3845 }
3846#if WATCH_LRU
3847 i915_dump_lru(dev, __func__);
3848#endif
3849
3850 i915_verify_inactive(dev, __FILE__, __LINE__);
3851
Eric Anholt673a3942008-07-30 12:06:12 -07003852err:
Julia Lawallaad87df2008-12-21 16:28:47 +01003853 for (i = 0; i < pinned; i++)
3854 i915_gem_object_unpin(object_list[i]);
Eric Anholt673a3942008-07-30 12:06:12 -07003855
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003856 for (i = 0; i < args->buffer_count; i++) {
3857 if (object_list[i]) {
Daniel Vetter23010e42010-03-08 13:35:02 +01003858 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003859 obj_priv->in_execbuffer = false;
3860 }
Julia Lawallaad87df2008-12-21 16:28:47 +01003861 drm_gem_object_unreference(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003862 }
Julia Lawallaad87df2008-12-21 16:28:47 +01003863
Eric Anholt673a3942008-07-30 12:06:12 -07003864 mutex_unlock(&dev->struct_mutex);
3865
Chris Wilson93533c22010-01-31 10:40:48 +00003866pre_mutex_err:
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003867 /* Copy the updated relocations out regardless of current error
3868 * state. Failure to update the relocs would mean that the next
3869 * time userland calls execbuf, it would do so with presumed offset
3870 * state that didn't match the actual object state.
3871 */
3872 ret2 = i915_gem_put_relocs_to_user(exec_list, args->buffer_count,
3873 relocs);
3874 if (ret2 != 0) {
3875 DRM_ERROR("Failed to copy relocations back out: %d\n", ret2);
3876
3877 if (ret == 0)
3878 ret = ret2;
3879 }
3880
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003881 drm_free_large(object_list);
Eric Anholt9a298b22009-03-24 12:23:04 -07003882 kfree(cliprects);
Eric Anholt673a3942008-07-30 12:06:12 -07003883
3884 return ret;
3885}
3886
Jesse Barnes76446ca2009-12-17 22:05:42 -05003887/*
3888 * Legacy execbuffer just creates an exec2 list from the original exec object
3889 * list array and passes it to the real function.
3890 */
3891int
3892i915_gem_execbuffer(struct drm_device *dev, void *data,
3893 struct drm_file *file_priv)
3894{
3895 struct drm_i915_gem_execbuffer *args = data;
3896 struct drm_i915_gem_execbuffer2 exec2;
3897 struct drm_i915_gem_exec_object *exec_list = NULL;
3898 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
3899 int ret, i;
3900
3901#if WATCH_EXEC
3902 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3903 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3904#endif
3905
3906 if (args->buffer_count < 1) {
3907 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3908 return -EINVAL;
3909 }
3910
3911 /* Copy in the exec list from userland */
3912 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
3913 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
3914 if (exec_list == NULL || exec2_list == NULL) {
3915 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
3916 args->buffer_count);
3917 drm_free_large(exec_list);
3918 drm_free_large(exec2_list);
3919 return -ENOMEM;
3920 }
3921 ret = copy_from_user(exec_list,
3922 (struct drm_i915_relocation_entry __user *)
3923 (uintptr_t) args->buffers_ptr,
3924 sizeof(*exec_list) * args->buffer_count);
3925 if (ret != 0) {
3926 DRM_ERROR("copy %d exec entries failed %d\n",
3927 args->buffer_count, ret);
3928 drm_free_large(exec_list);
3929 drm_free_large(exec2_list);
3930 return -EFAULT;
3931 }
3932
3933 for (i = 0; i < args->buffer_count; i++) {
3934 exec2_list[i].handle = exec_list[i].handle;
3935 exec2_list[i].relocation_count = exec_list[i].relocation_count;
3936 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
3937 exec2_list[i].alignment = exec_list[i].alignment;
3938 exec2_list[i].offset = exec_list[i].offset;
3939 if (!IS_I965G(dev))
3940 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
3941 else
3942 exec2_list[i].flags = 0;
3943 }
3944
3945 exec2.buffers_ptr = args->buffers_ptr;
3946 exec2.buffer_count = args->buffer_count;
3947 exec2.batch_start_offset = args->batch_start_offset;
3948 exec2.batch_len = args->batch_len;
3949 exec2.DR1 = args->DR1;
3950 exec2.DR4 = args->DR4;
3951 exec2.num_cliprects = args->num_cliprects;
3952 exec2.cliprects_ptr = args->cliprects_ptr;
Zou Nan hai852835f2010-05-21 09:08:56 +08003953 exec2.flags = I915_EXEC_RENDER;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003954
3955 ret = i915_gem_do_execbuffer(dev, data, file_priv, &exec2, exec2_list);
3956 if (!ret) {
3957 /* Copy the new buffer offsets back to the user's exec list. */
3958 for (i = 0; i < args->buffer_count; i++)
3959 exec_list[i].offset = exec2_list[i].offset;
3960 /* ... and back out to userspace */
3961 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
3962 (uintptr_t) args->buffers_ptr,
3963 exec_list,
3964 sizeof(*exec_list) * args->buffer_count);
3965 if (ret) {
3966 ret = -EFAULT;
3967 DRM_ERROR("failed to copy %d exec entries "
3968 "back to user (%d)\n",
3969 args->buffer_count, ret);
3970 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05003971 }
3972
3973 drm_free_large(exec_list);
3974 drm_free_large(exec2_list);
3975 return ret;
3976}
3977
3978int
3979i915_gem_execbuffer2(struct drm_device *dev, void *data,
3980 struct drm_file *file_priv)
3981{
3982 struct drm_i915_gem_execbuffer2 *args = data;
3983 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
3984 int ret;
3985
3986#if WATCH_EXEC
3987 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3988 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3989#endif
3990
3991 if (args->buffer_count < 1) {
3992 DRM_ERROR("execbuf2 with %d buffers\n", args->buffer_count);
3993 return -EINVAL;
3994 }
3995
3996 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
3997 if (exec2_list == NULL) {
3998 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
3999 args->buffer_count);
4000 return -ENOMEM;
4001 }
4002 ret = copy_from_user(exec2_list,
4003 (struct drm_i915_relocation_entry __user *)
4004 (uintptr_t) args->buffers_ptr,
4005 sizeof(*exec2_list) * args->buffer_count);
4006 if (ret != 0) {
4007 DRM_ERROR("copy %d exec entries failed %d\n",
4008 args->buffer_count, ret);
4009 drm_free_large(exec2_list);
4010 return -EFAULT;
4011 }
4012
4013 ret = i915_gem_do_execbuffer(dev, data, file_priv, args, exec2_list);
4014 if (!ret) {
4015 /* Copy the new buffer offsets back to the user's exec list. */
4016 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
4017 (uintptr_t) args->buffers_ptr,
4018 exec2_list,
4019 sizeof(*exec2_list) * args->buffer_count);
4020 if (ret) {
4021 ret = -EFAULT;
4022 DRM_ERROR("failed to copy %d exec entries "
4023 "back to user (%d)\n",
4024 args->buffer_count, ret);
4025 }
4026 }
4027
4028 drm_free_large(exec2_list);
4029 return ret;
4030}
4031
Eric Anholt673a3942008-07-30 12:06:12 -07004032int
4033i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment)
4034{
4035 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01004036 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004037 int ret;
4038
Daniel Vetter778c3542010-05-13 11:49:44 +02004039 BUG_ON(obj_priv->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT);
4040
Eric Anholt673a3942008-07-30 12:06:12 -07004041 i915_verify_inactive(dev, __FILE__, __LINE__);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01004042
4043 if (obj_priv->gtt_space != NULL) {
4044 if (alignment == 0)
4045 alignment = i915_gem_get_gtt_alignment(obj);
4046 if (obj_priv->gtt_offset & (alignment - 1)) {
Chris Wilsonae7d49d2010-08-04 12:37:41 +01004047 WARN(obj_priv->pin_count,
4048 "bo is already pinned with incorrect alignment:"
4049 " offset=%x, req.alignment=%x\n",
4050 obj_priv->gtt_offset, alignment);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01004051 ret = i915_gem_object_unbind(obj);
4052 if (ret)
4053 return ret;
4054 }
4055 }
4056
Eric Anholt673a3942008-07-30 12:06:12 -07004057 if (obj_priv->gtt_space == NULL) {
4058 ret = i915_gem_object_bind_to_gtt(obj, alignment);
Chris Wilson97311292009-09-21 00:22:34 +01004059 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07004060 return ret;
Chris Wilson22c344e2009-02-11 14:26:45 +00004061 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05004062
Eric Anholt673a3942008-07-30 12:06:12 -07004063 obj_priv->pin_count++;
4064
4065 /* If the object is not active and not pending a flush,
4066 * remove it from the inactive list
4067 */
4068 if (obj_priv->pin_count == 1) {
4069 atomic_inc(&dev->pin_count);
4070 atomic_add(obj->size, &dev->pin_memory);
4071 if (!obj_priv->active &&
Chris Wilsonbf1a1092010-08-07 11:01:20 +01004072 (obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
Eric Anholt673a3942008-07-30 12:06:12 -07004073 list_del_init(&obj_priv->list);
4074 }
4075 i915_verify_inactive(dev, __FILE__, __LINE__);
4076
4077 return 0;
4078}
4079
4080void
4081i915_gem_object_unpin(struct drm_gem_object *obj)
4082{
4083 struct drm_device *dev = obj->dev;
4084 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01004085 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004086
4087 i915_verify_inactive(dev, __FILE__, __LINE__);
4088 obj_priv->pin_count--;
4089 BUG_ON(obj_priv->pin_count < 0);
4090 BUG_ON(obj_priv->gtt_space == NULL);
4091
4092 /* If the object is no longer pinned, and is
4093 * neither active nor being flushed, then stick it on
4094 * the inactive list
4095 */
4096 if (obj_priv->pin_count == 0) {
4097 if (!obj_priv->active &&
Chris Wilson21d509e2009-06-06 09:46:02 +01004098 (obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
Eric Anholt673a3942008-07-30 12:06:12 -07004099 list_move_tail(&obj_priv->list,
4100 &dev_priv->mm.inactive_list);
4101 atomic_dec(&dev->pin_count);
4102 atomic_sub(obj->size, &dev->pin_memory);
4103 }
4104 i915_verify_inactive(dev, __FILE__, __LINE__);
4105}
4106
4107int
4108i915_gem_pin_ioctl(struct drm_device *dev, void *data,
4109 struct drm_file *file_priv)
4110{
4111 struct drm_i915_gem_pin *args = data;
4112 struct drm_gem_object *obj;
4113 struct drm_i915_gem_object *obj_priv;
4114 int ret;
4115
4116 mutex_lock(&dev->struct_mutex);
4117
4118 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4119 if (obj == NULL) {
4120 DRM_ERROR("Bad handle in i915_gem_pin_ioctl(): %d\n",
4121 args->handle);
4122 mutex_unlock(&dev->struct_mutex);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01004123 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07004124 }
Daniel Vetter23010e42010-03-08 13:35:02 +01004125 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004126
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004127 if (obj_priv->madv != I915_MADV_WILLNEED) {
4128 DRM_ERROR("Attempting to pin a purgeable buffer\n");
Chris Wilson3ef94da2009-09-14 16:50:29 +01004129 drm_gem_object_unreference(obj);
4130 mutex_unlock(&dev->struct_mutex);
4131 return -EINVAL;
4132 }
4133
Jesse Barnes79e53942008-11-07 14:24:08 -08004134 if (obj_priv->pin_filp != NULL && obj_priv->pin_filp != file_priv) {
4135 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
4136 args->handle);
Chris Wilson96dec612009-02-08 19:08:04 +00004137 drm_gem_object_unreference(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004138 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08004139 return -EINVAL;
4140 }
4141
4142 obj_priv->user_pin_count++;
4143 obj_priv->pin_filp = file_priv;
4144 if (obj_priv->user_pin_count == 1) {
4145 ret = i915_gem_object_pin(obj, args->alignment);
4146 if (ret != 0) {
4147 drm_gem_object_unreference(obj);
4148 mutex_unlock(&dev->struct_mutex);
4149 return ret;
4150 }
Eric Anholt673a3942008-07-30 12:06:12 -07004151 }
4152
4153 /* XXX - flush the CPU caches for pinned objects
4154 * as the X server doesn't manage domains yet
4155 */
Eric Anholte47c68e2008-11-14 13:35:19 -08004156 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004157 args->offset = obj_priv->gtt_offset;
4158 drm_gem_object_unreference(obj);
4159 mutex_unlock(&dev->struct_mutex);
4160
4161 return 0;
4162}
4163
4164int
4165i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
4166 struct drm_file *file_priv)
4167{
4168 struct drm_i915_gem_pin *args = data;
4169 struct drm_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08004170 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07004171
4172 mutex_lock(&dev->struct_mutex);
4173
4174 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4175 if (obj == NULL) {
4176 DRM_ERROR("Bad handle in i915_gem_unpin_ioctl(): %d\n",
4177 args->handle);
4178 mutex_unlock(&dev->struct_mutex);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01004179 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07004180 }
4181
Daniel Vetter23010e42010-03-08 13:35:02 +01004182 obj_priv = to_intel_bo(obj);
Jesse Barnes79e53942008-11-07 14:24:08 -08004183 if (obj_priv->pin_filp != file_priv) {
4184 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
4185 args->handle);
4186 drm_gem_object_unreference(obj);
4187 mutex_unlock(&dev->struct_mutex);
4188 return -EINVAL;
4189 }
4190 obj_priv->user_pin_count--;
4191 if (obj_priv->user_pin_count == 0) {
4192 obj_priv->pin_filp = NULL;
4193 i915_gem_object_unpin(obj);
4194 }
Eric Anholt673a3942008-07-30 12:06:12 -07004195
4196 drm_gem_object_unreference(obj);
4197 mutex_unlock(&dev->struct_mutex);
4198 return 0;
4199}
4200
4201int
4202i915_gem_busy_ioctl(struct drm_device *dev, void *data,
4203 struct drm_file *file_priv)
4204{
4205 struct drm_i915_gem_busy *args = data;
4206 struct drm_gem_object *obj;
4207 struct drm_i915_gem_object *obj_priv;
4208
Eric Anholt673a3942008-07-30 12:06:12 -07004209 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4210 if (obj == NULL) {
4211 DRM_ERROR("Bad handle in i915_gem_busy_ioctl(): %d\n",
4212 args->handle);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01004213 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07004214 }
4215
Chris Wilsonb1ce7862009-06-06 09:46:00 +01004216 mutex_lock(&dev->struct_mutex);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004217
Chris Wilson0be555b2010-08-04 15:36:30 +01004218 /* Count all active objects as busy, even if they are currently not used
4219 * by the gpu. Users of this interface expect objects to eventually
4220 * become non-busy without any further actions, therefore emit any
4221 * necessary flushes here.
Eric Anholtc4de0a52008-12-14 19:05:04 -08004222 */
Chris Wilson0be555b2010-08-04 15:36:30 +01004223 obj_priv = to_intel_bo(obj);
4224 args->busy = obj_priv->active;
4225 if (args->busy) {
4226 /* Unconditionally flush objects, even when the gpu still uses this
4227 * object. Userspace calling this function indicates that it wants to
4228 * use this buffer rather sooner than later, so issuing the required
4229 * flush earlier is beneficial.
4230 */
4231 if (obj->write_domain) {
4232 i915_gem_flush(dev, 0, obj->write_domain);
4233 (void)i915_add_request(dev, file_priv, obj->write_domain, obj_priv->ring);
4234 }
4235
4236 /* Update the active list for the hardware's current position.
4237 * Otherwise this only updates on a delayed timer or when irqs
4238 * are actually unmasked, and our working set ends up being
4239 * larger than required.
4240 */
4241 i915_gem_retire_requests_ring(dev, obj_priv->ring);
4242
4243 args->busy = obj_priv->active;
4244 }
Eric Anholt673a3942008-07-30 12:06:12 -07004245
4246 drm_gem_object_unreference(obj);
4247 mutex_unlock(&dev->struct_mutex);
4248 return 0;
4249}
4250
4251int
4252i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4253 struct drm_file *file_priv)
4254{
4255 return i915_gem_ring_throttle(dev, file_priv);
4256}
4257
Chris Wilson3ef94da2009-09-14 16:50:29 +01004258int
4259i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4260 struct drm_file *file_priv)
4261{
4262 struct drm_i915_gem_madvise *args = data;
4263 struct drm_gem_object *obj;
4264 struct drm_i915_gem_object *obj_priv;
4265
4266 switch (args->madv) {
4267 case I915_MADV_DONTNEED:
4268 case I915_MADV_WILLNEED:
4269 break;
4270 default:
4271 return -EINVAL;
4272 }
4273
4274 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4275 if (obj == NULL) {
4276 DRM_ERROR("Bad handle in i915_gem_madvise_ioctl(): %d\n",
4277 args->handle);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01004278 return -ENOENT;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004279 }
4280
4281 mutex_lock(&dev->struct_mutex);
Daniel Vetter23010e42010-03-08 13:35:02 +01004282 obj_priv = to_intel_bo(obj);
Chris Wilson3ef94da2009-09-14 16:50:29 +01004283
4284 if (obj_priv->pin_count) {
4285 drm_gem_object_unreference(obj);
4286 mutex_unlock(&dev->struct_mutex);
4287
4288 DRM_ERROR("Attempted i915_gem_madvise_ioctl() on a pinned object\n");
4289 return -EINVAL;
4290 }
4291
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004292 if (obj_priv->madv != __I915_MADV_PURGED)
4293 obj_priv->madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004294
Chris Wilson2d7ef392009-09-20 23:13:10 +01004295 /* if the object is no longer bound, discard its backing storage */
4296 if (i915_gem_object_is_purgeable(obj_priv) &&
4297 obj_priv->gtt_space == NULL)
4298 i915_gem_object_truncate(obj);
4299
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004300 args->retained = obj_priv->madv != __I915_MADV_PURGED;
4301
Chris Wilson3ef94da2009-09-14 16:50:29 +01004302 drm_gem_object_unreference(obj);
4303 mutex_unlock(&dev->struct_mutex);
4304
4305 return 0;
4306}
4307
Daniel Vetterac52bc52010-04-09 19:05:06 +00004308struct drm_gem_object * i915_gem_alloc_object(struct drm_device *dev,
4309 size_t size)
4310{
Daniel Vetterc397b902010-04-09 19:05:07 +00004311 struct drm_i915_gem_object *obj;
4312
4313 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
4314 if (obj == NULL)
4315 return NULL;
4316
4317 if (drm_gem_object_init(dev, &obj->base, size) != 0) {
4318 kfree(obj);
4319 return NULL;
4320 }
4321
4322 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4323 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4324
4325 obj->agp_type = AGP_USER_MEMORY;
Daniel Vetter62b8b212010-04-09 19:05:08 +00004326 obj->base.driver_private = NULL;
Daniel Vetterc397b902010-04-09 19:05:07 +00004327 obj->fence_reg = I915_FENCE_REG_NONE;
4328 INIT_LIST_HEAD(&obj->list);
4329 INIT_LIST_HEAD(&obj->gpu_write_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00004330 obj->madv = I915_MADV_WILLNEED;
4331
4332 trace_i915_gem_object_create(&obj->base);
4333
4334 return &obj->base;
Daniel Vetterac52bc52010-04-09 19:05:06 +00004335}
4336
Eric Anholt673a3942008-07-30 12:06:12 -07004337int i915_gem_init_object(struct drm_gem_object *obj)
4338{
Daniel Vetterc397b902010-04-09 19:05:07 +00004339 BUG();
Jesse Barnesde151cf2008-11-12 10:03:55 -08004340
Eric Anholt673a3942008-07-30 12:06:12 -07004341 return 0;
4342}
4343
Chris Wilsonbe726152010-07-23 23:18:50 +01004344static void i915_gem_free_object_tail(struct drm_gem_object *obj)
4345{
4346 struct drm_device *dev = obj->dev;
4347 drm_i915_private_t *dev_priv = dev->dev_private;
4348 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
4349 int ret;
4350
4351 ret = i915_gem_object_unbind(obj);
4352 if (ret == -ERESTARTSYS) {
4353 list_move(&obj_priv->list,
4354 &dev_priv->mm.deferred_free_list);
4355 return;
4356 }
4357
4358 if (obj_priv->mmap_offset)
4359 i915_gem_free_mmap_offset(obj);
4360
4361 drm_gem_object_release(obj);
4362
4363 kfree(obj_priv->page_cpu_valid);
4364 kfree(obj_priv->bit_17);
4365 kfree(obj_priv);
4366}
4367
Eric Anholt673a3942008-07-30 12:06:12 -07004368void i915_gem_free_object(struct drm_gem_object *obj)
4369{
Jesse Barnesde151cf2008-11-12 10:03:55 -08004370 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01004371 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004372
Chris Wilson1c5d22f2009-08-25 11:15:50 +01004373 trace_i915_gem_object_destroy(obj);
4374
Eric Anholt673a3942008-07-30 12:06:12 -07004375 while (obj_priv->pin_count > 0)
4376 i915_gem_object_unpin(obj);
4377
Dave Airlie71acb5e2008-12-30 20:31:46 +10004378 if (obj_priv->phys_obj)
4379 i915_gem_detach_phys_object(dev, obj);
4380
Chris Wilsonbe726152010-07-23 23:18:50 +01004381 i915_gem_free_object_tail(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004382}
4383
Jesse Barnes5669fca2009-02-17 15:13:31 -08004384int
Eric Anholt673a3942008-07-30 12:06:12 -07004385i915_gem_idle(struct drm_device *dev)
4386{
4387 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson29105cc2010-01-07 10:39:13 +00004388 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004389
Keith Packard6dbe2772008-10-14 21:41:13 -07004390 mutex_lock(&dev->struct_mutex);
4391
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004392 if (dev_priv->mm.suspended ||
Zou Nan haid1b851f2010-05-21 09:08:57 +08004393 (dev_priv->render_ring.gem_object == NULL) ||
4394 (HAS_BSD(dev) &&
4395 dev_priv->bsd_ring.gem_object == NULL)) {
Keith Packard6dbe2772008-10-14 21:41:13 -07004396 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004397 return 0;
Keith Packard6dbe2772008-10-14 21:41:13 -07004398 }
Eric Anholt673a3942008-07-30 12:06:12 -07004399
Chris Wilson29105cc2010-01-07 10:39:13 +00004400 ret = i915_gpu_idle(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004401 if (ret) {
4402 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004403 return ret;
Keith Packard6dbe2772008-10-14 21:41:13 -07004404 }
Eric Anholt673a3942008-07-30 12:06:12 -07004405
Chris Wilson29105cc2010-01-07 10:39:13 +00004406 /* Under UMS, be paranoid and evict. */
4407 if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
Chris Wilsonb47eb4a2010-08-07 11:01:23 +01004408 ret = i915_gem_evict_inactive(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00004409 if (ret) {
4410 mutex_unlock(&dev->struct_mutex);
4411 return ret;
4412 }
4413 }
4414
4415 /* Hack! Don't let anybody do execbuf while we don't control the chip.
4416 * We need to replace this with a semaphore, or something.
4417 * And not confound mm.suspended!
4418 */
4419 dev_priv->mm.suspended = 1;
4420 del_timer(&dev_priv->hangcheck_timer);
4421
4422 i915_kernel_lost_context(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004423 i915_gem_cleanup_ringbuffer(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00004424
Keith Packard6dbe2772008-10-14 21:41:13 -07004425 mutex_unlock(&dev->struct_mutex);
4426
Chris Wilson29105cc2010-01-07 10:39:13 +00004427 /* Cancel the retire work handler, which should be idle now. */
4428 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
4429
Eric Anholt673a3942008-07-30 12:06:12 -07004430 return 0;
4431}
4432
Jesse Barnese552eb72010-04-21 11:39:23 -07004433/*
4434 * 965+ support PIPE_CONTROL commands, which provide finer grained control
4435 * over cache flushing.
4436 */
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004437static int
Jesse Barnese552eb72010-04-21 11:39:23 -07004438i915_gem_init_pipe_control(struct drm_device *dev)
4439{
4440 drm_i915_private_t *dev_priv = dev->dev_private;
4441 struct drm_gem_object *obj;
4442 struct drm_i915_gem_object *obj_priv;
4443 int ret;
4444
Eric Anholt34dc4d42010-05-07 14:30:03 -07004445 obj = i915_gem_alloc_object(dev, 4096);
Jesse Barnese552eb72010-04-21 11:39:23 -07004446 if (obj == NULL) {
4447 DRM_ERROR("Failed to allocate seqno page\n");
4448 ret = -ENOMEM;
4449 goto err;
4450 }
4451 obj_priv = to_intel_bo(obj);
4452 obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
4453
4454 ret = i915_gem_object_pin(obj, 4096);
4455 if (ret)
4456 goto err_unref;
4457
4458 dev_priv->seqno_gfx_addr = obj_priv->gtt_offset;
4459 dev_priv->seqno_page = kmap(obj_priv->pages[0]);
4460 if (dev_priv->seqno_page == NULL)
4461 goto err_unpin;
4462
4463 dev_priv->seqno_obj = obj;
4464 memset(dev_priv->seqno_page, 0, PAGE_SIZE);
4465
4466 return 0;
4467
4468err_unpin:
4469 i915_gem_object_unpin(obj);
4470err_unref:
4471 drm_gem_object_unreference(obj);
4472err:
4473 return ret;
4474}
4475
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004476
4477static void
Jesse Barnese552eb72010-04-21 11:39:23 -07004478i915_gem_cleanup_pipe_control(struct drm_device *dev)
4479{
4480 drm_i915_private_t *dev_priv = dev->dev_private;
4481 struct drm_gem_object *obj;
4482 struct drm_i915_gem_object *obj_priv;
4483
4484 obj = dev_priv->seqno_obj;
4485 obj_priv = to_intel_bo(obj);
4486 kunmap(obj_priv->pages[0]);
4487 i915_gem_object_unpin(obj);
4488 drm_gem_object_unreference(obj);
4489 dev_priv->seqno_obj = NULL;
4490
4491 dev_priv->seqno_page = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07004492}
4493
Eric Anholt673a3942008-07-30 12:06:12 -07004494int
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004495i915_gem_init_ringbuffer(struct drm_device *dev)
4496{
4497 drm_i915_private_t *dev_priv = dev->dev_private;
4498 int ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01004499
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004500 dev_priv->render_ring = render_ring;
Chris Wilson68f95ba2010-05-27 13:18:22 +01004501
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004502 if (!I915_NEED_GFX_HWS(dev)) {
4503 dev_priv->render_ring.status_page.page_addr
4504 = dev_priv->status_page_dmah->vaddr;
4505 memset(dev_priv->render_ring.status_page.page_addr,
4506 0, PAGE_SIZE);
4507 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004508
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004509 if (HAS_PIPE_CONTROL(dev)) {
4510 ret = i915_gem_init_pipe_control(dev);
4511 if (ret)
4512 return ret;
4513 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004514
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004515 ret = intel_init_ring_buffer(dev, &dev_priv->render_ring);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004516 if (ret)
4517 goto cleanup_pipe_control;
4518
4519 if (HAS_BSD(dev)) {
Zou Nan haid1b851f2010-05-21 09:08:57 +08004520 dev_priv->bsd_ring = bsd_ring;
4521 ret = intel_init_ring_buffer(dev, &dev_priv->bsd_ring);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004522 if (ret)
4523 goto cleanup_render_ring;
Zou Nan haid1b851f2010-05-21 09:08:57 +08004524 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004525
Chris Wilson6f392d52010-08-07 11:01:22 +01004526 dev_priv->next_seqno = 1;
4527
Chris Wilson68f95ba2010-05-27 13:18:22 +01004528 return 0;
4529
4530cleanup_render_ring:
4531 intel_cleanup_ring_buffer(dev, &dev_priv->render_ring);
4532cleanup_pipe_control:
4533 if (HAS_PIPE_CONTROL(dev))
4534 i915_gem_cleanup_pipe_control(dev);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004535 return ret;
4536}
4537
4538void
4539i915_gem_cleanup_ringbuffer(struct drm_device *dev)
4540{
4541 drm_i915_private_t *dev_priv = dev->dev_private;
4542
4543 intel_cleanup_ring_buffer(dev, &dev_priv->render_ring);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004544 if (HAS_BSD(dev))
4545 intel_cleanup_ring_buffer(dev, &dev_priv->bsd_ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004546 if (HAS_PIPE_CONTROL(dev))
4547 i915_gem_cleanup_pipe_control(dev);
4548}
4549
4550int
Eric Anholt673a3942008-07-30 12:06:12 -07004551i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
4552 struct drm_file *file_priv)
4553{
4554 drm_i915_private_t *dev_priv = dev->dev_private;
4555 int ret;
4556
Jesse Barnes79e53942008-11-07 14:24:08 -08004557 if (drm_core_check_feature(dev, DRIVER_MODESET))
4558 return 0;
4559
Ben Gamariba1234d2009-09-14 17:48:47 -04004560 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07004561 DRM_ERROR("Reenabling wedged hardware, good luck\n");
Ben Gamariba1234d2009-09-14 17:48:47 -04004562 atomic_set(&dev_priv->mm.wedged, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004563 }
4564
Eric Anholt673a3942008-07-30 12:06:12 -07004565 mutex_lock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004566 dev_priv->mm.suspended = 0;
4567
4568 ret = i915_gem_init_ringbuffer(dev);
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004569 if (ret != 0) {
4570 mutex_unlock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004571 return ret;
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004572 }
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004573
Carl Worth5e118f42009-03-20 11:54:25 -07004574 spin_lock(&dev_priv->mm.active_list_lock);
Zou Nan hai852835f2010-05-21 09:08:56 +08004575 BUG_ON(!list_empty(&dev_priv->render_ring.active_list));
Zou Nan haid1b851f2010-05-21 09:08:57 +08004576 BUG_ON(HAS_BSD(dev) && !list_empty(&dev_priv->bsd_ring.active_list));
Carl Worth5e118f42009-03-20 11:54:25 -07004577 spin_unlock(&dev_priv->mm.active_list_lock);
4578
Eric Anholt673a3942008-07-30 12:06:12 -07004579 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
4580 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
Zou Nan hai852835f2010-05-21 09:08:56 +08004581 BUG_ON(!list_empty(&dev_priv->render_ring.request_list));
Zou Nan haid1b851f2010-05-21 09:08:57 +08004582 BUG_ON(HAS_BSD(dev) && !list_empty(&dev_priv->bsd_ring.request_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004583 mutex_unlock(&dev->struct_mutex);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004584
Chris Wilson5f353082010-06-07 14:03:03 +01004585 ret = drm_irq_install(dev);
4586 if (ret)
4587 goto cleanup_ringbuffer;
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004588
Eric Anholt673a3942008-07-30 12:06:12 -07004589 return 0;
Chris Wilson5f353082010-06-07 14:03:03 +01004590
4591cleanup_ringbuffer:
4592 mutex_lock(&dev->struct_mutex);
4593 i915_gem_cleanup_ringbuffer(dev);
4594 dev_priv->mm.suspended = 1;
4595 mutex_unlock(&dev->struct_mutex);
4596
4597 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004598}
4599
4600int
4601i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
4602 struct drm_file *file_priv)
4603{
Jesse Barnes79e53942008-11-07 14:24:08 -08004604 if (drm_core_check_feature(dev, DRIVER_MODESET))
4605 return 0;
4606
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004607 drm_irq_uninstall(dev);
Linus Torvaldse6890f62009-09-08 17:09:24 -07004608 return i915_gem_idle(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004609}
4610
4611void
4612i915_gem_lastclose(struct drm_device *dev)
4613{
4614 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004615
Eric Anholte806b492009-01-22 09:56:58 -08004616 if (drm_core_check_feature(dev, DRIVER_MODESET))
4617 return;
4618
Keith Packard6dbe2772008-10-14 21:41:13 -07004619 ret = i915_gem_idle(dev);
4620 if (ret)
4621 DRM_ERROR("failed to idle hardware: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07004622}
4623
4624void
4625i915_gem_load(struct drm_device *dev)
4626{
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004627 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07004628 drm_i915_private_t *dev_priv = dev->dev_private;
4629
Carl Worth5e118f42009-03-20 11:54:25 -07004630 spin_lock_init(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07004631 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
Daniel Vetter99fcb762010-02-07 16:20:18 +01004632 INIT_LIST_HEAD(&dev_priv->mm.gpu_write_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004633 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07004634 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Chris Wilsonbe726152010-07-23 23:18:50 +01004635 INIT_LIST_HEAD(&dev_priv->mm.deferred_free_list);
Zou Nan hai852835f2010-05-21 09:08:56 +08004636 INIT_LIST_HEAD(&dev_priv->render_ring.active_list);
4637 INIT_LIST_HEAD(&dev_priv->render_ring.request_list);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004638 if (HAS_BSD(dev)) {
4639 INIT_LIST_HEAD(&dev_priv->bsd_ring.active_list);
4640 INIT_LIST_HEAD(&dev_priv->bsd_ring.request_list);
4641 }
Daniel Vetter007cc8a2010-04-28 11:02:31 +02004642 for (i = 0; i < 16; i++)
4643 INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004644 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
4645 i915_gem_retire_work_handler);
Chris Wilson31169712009-09-14 16:50:28 +01004646 spin_lock(&shrink_list_lock);
4647 list_add(&dev_priv->mm.shrink_list, &shrink_list);
4648 spin_unlock(&shrink_list_lock);
4649
Dave Airlie94400122010-07-20 13:15:31 +10004650 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
4651 if (IS_GEN3(dev)) {
4652 u32 tmp = I915_READ(MI_ARB_STATE);
4653 if (!(tmp & MI_ARB_C3_LP_WRITE_ENABLE)) {
4654 /* arb state is a masked write, so set bit + bit in mask */
4655 tmp = MI_ARB_C3_LP_WRITE_ENABLE | (MI_ARB_C3_LP_WRITE_ENABLE << MI_ARB_MASK_SHIFT);
4656 I915_WRITE(MI_ARB_STATE, tmp);
4657 }
4658 }
4659
Jesse Barnesde151cf2008-11-12 10:03:55 -08004660 /* Old X drivers will take 0-2 for front, back, depth buffers */
Eric Anholtb397c832010-01-26 09:43:10 -08004661 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4662 dev_priv->fence_reg_start = 3;
Jesse Barnesde151cf2008-11-12 10:03:55 -08004663
Jesse Barnes0f973f22009-01-26 17:10:45 -08004664 if (IS_I965G(dev) || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08004665 dev_priv->num_fence_regs = 16;
4666 else
4667 dev_priv->num_fence_regs = 8;
4668
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004669 /* Initialize fence registers to zero */
4670 if (IS_I965G(dev)) {
4671 for (i = 0; i < 16; i++)
4672 I915_WRITE64(FENCE_REG_965_0 + (i * 8), 0);
4673 } else {
4674 for (i = 0; i < 8; i++)
4675 I915_WRITE(FENCE_REG_830_0 + (i * 4), 0);
4676 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4677 for (i = 0; i < 8; i++)
4678 I915_WRITE(FENCE_REG_945_8 + (i * 4), 0);
4679 }
Eric Anholt673a3942008-07-30 12:06:12 -07004680 i915_gem_detect_bit_6_swizzle(dev);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05004681 init_waitqueue_head(&dev_priv->pending_flip_queue);
Eric Anholt673a3942008-07-30 12:06:12 -07004682}
Dave Airlie71acb5e2008-12-30 20:31:46 +10004683
4684/*
4685 * Create a physically contiguous memory object for this object
4686 * e.g. for cursor + overlay regs
4687 */
4688int i915_gem_init_phys_object(struct drm_device *dev,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004689 int id, int size, int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004690{
4691 drm_i915_private_t *dev_priv = dev->dev_private;
4692 struct drm_i915_gem_phys_object *phys_obj;
4693 int ret;
4694
4695 if (dev_priv->mm.phys_objs[id - 1] || !size)
4696 return 0;
4697
Eric Anholt9a298b22009-03-24 12:23:04 -07004698 phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004699 if (!phys_obj)
4700 return -ENOMEM;
4701
4702 phys_obj->id = id;
4703
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004704 phys_obj->handle = drm_pci_alloc(dev, size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004705 if (!phys_obj->handle) {
4706 ret = -ENOMEM;
4707 goto kfree_obj;
4708 }
4709#ifdef CONFIG_X86
4710 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4711#endif
4712
4713 dev_priv->mm.phys_objs[id - 1] = phys_obj;
4714
4715 return 0;
4716kfree_obj:
Eric Anholt9a298b22009-03-24 12:23:04 -07004717 kfree(phys_obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004718 return ret;
4719}
4720
4721void i915_gem_free_phys_object(struct drm_device *dev, int id)
4722{
4723 drm_i915_private_t *dev_priv = dev->dev_private;
4724 struct drm_i915_gem_phys_object *phys_obj;
4725
4726 if (!dev_priv->mm.phys_objs[id - 1])
4727 return;
4728
4729 phys_obj = dev_priv->mm.phys_objs[id - 1];
4730 if (phys_obj->cur_obj) {
4731 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
4732 }
4733
4734#ifdef CONFIG_X86
4735 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4736#endif
4737 drm_pci_free(dev, phys_obj->handle);
4738 kfree(phys_obj);
4739 dev_priv->mm.phys_objs[id - 1] = NULL;
4740}
4741
4742void i915_gem_free_all_phys_object(struct drm_device *dev)
4743{
4744 int i;
4745
Dave Airlie260883c2009-01-22 17:58:49 +10004746 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004747 i915_gem_free_phys_object(dev, i);
4748}
4749
4750void i915_gem_detach_phys_object(struct drm_device *dev,
4751 struct drm_gem_object *obj)
4752{
4753 struct drm_i915_gem_object *obj_priv;
4754 int i;
4755 int ret;
4756 int page_count;
4757
Daniel Vetter23010e42010-03-08 13:35:02 +01004758 obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004759 if (!obj_priv->phys_obj)
4760 return;
4761
Chris Wilson4bdadb92010-01-27 13:36:32 +00004762 ret = i915_gem_object_get_pages(obj, 0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004763 if (ret)
4764 goto out;
4765
4766 page_count = obj->size / PAGE_SIZE;
4767
4768 for (i = 0; i < page_count; i++) {
Eric Anholt856fa192009-03-19 14:10:50 -07004769 char *dst = kmap_atomic(obj_priv->pages[i], KM_USER0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004770 char *src = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4771
4772 memcpy(dst, src, PAGE_SIZE);
4773 kunmap_atomic(dst, KM_USER0);
4774 }
Eric Anholt856fa192009-03-19 14:10:50 -07004775 drm_clflush_pages(obj_priv->pages, page_count);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004776 drm_agp_chipset_flush(dev);
Chris Wilsond78b47b2009-06-17 21:52:49 +01004777
4778 i915_gem_object_put_pages(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004779out:
4780 obj_priv->phys_obj->cur_obj = NULL;
4781 obj_priv->phys_obj = NULL;
4782}
4783
4784int
4785i915_gem_attach_phys_object(struct drm_device *dev,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004786 struct drm_gem_object *obj,
4787 int id,
4788 int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004789{
4790 drm_i915_private_t *dev_priv = dev->dev_private;
4791 struct drm_i915_gem_object *obj_priv;
4792 int ret = 0;
4793 int page_count;
4794 int i;
4795
4796 if (id > I915_MAX_PHYS_OBJECT)
4797 return -EINVAL;
4798
Daniel Vetter23010e42010-03-08 13:35:02 +01004799 obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004800
4801 if (obj_priv->phys_obj) {
4802 if (obj_priv->phys_obj->id == id)
4803 return 0;
4804 i915_gem_detach_phys_object(dev, obj);
4805 }
4806
Dave Airlie71acb5e2008-12-30 20:31:46 +10004807 /* create a new object */
4808 if (!dev_priv->mm.phys_objs[id - 1]) {
4809 ret = i915_gem_init_phys_object(dev, id,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004810 obj->size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004811 if (ret) {
Linus Torvaldsaeb565d2009-01-26 10:01:53 -08004812 DRM_ERROR("failed to init phys object %d size: %zu\n", id, obj->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004813 goto out;
4814 }
4815 }
4816
4817 /* bind to the object */
4818 obj_priv->phys_obj = dev_priv->mm.phys_objs[id - 1];
4819 obj_priv->phys_obj->cur_obj = obj;
4820
Chris Wilson4bdadb92010-01-27 13:36:32 +00004821 ret = i915_gem_object_get_pages(obj, 0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004822 if (ret) {
4823 DRM_ERROR("failed to get page list\n");
4824 goto out;
4825 }
4826
4827 page_count = obj->size / PAGE_SIZE;
4828
4829 for (i = 0; i < page_count; i++) {
Eric Anholt856fa192009-03-19 14:10:50 -07004830 char *src = kmap_atomic(obj_priv->pages[i], KM_USER0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004831 char *dst = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4832
4833 memcpy(dst, src, PAGE_SIZE);
4834 kunmap_atomic(src, KM_USER0);
4835 }
4836
Chris Wilsond78b47b2009-06-17 21:52:49 +01004837 i915_gem_object_put_pages(obj);
4838
Dave Airlie71acb5e2008-12-30 20:31:46 +10004839 return 0;
4840out:
4841 return ret;
4842}
4843
4844static int
4845i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
4846 struct drm_i915_gem_pwrite *args,
4847 struct drm_file *file_priv)
4848{
Daniel Vetter23010e42010-03-08 13:35:02 +01004849 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004850 void *obj_addr;
4851 int ret;
4852 char __user *user_data;
4853
4854 user_data = (char __user *) (uintptr_t) args->data_ptr;
4855 obj_addr = obj_priv->phys_obj->handle->vaddr + args->offset;
4856
Zhao Yakui44d98a62009-10-09 11:39:40 +08004857 DRM_DEBUG_DRIVER("obj_addr %p, %lld\n", obj_addr, args->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004858 ret = copy_from_user(obj_addr, user_data, args->size);
4859 if (ret)
4860 return -EFAULT;
4861
4862 drm_agp_chipset_flush(dev);
4863 return 0;
4864}
Eric Anholtb9624422009-06-03 07:27:35 +00004865
4866void i915_gem_release(struct drm_device * dev, struct drm_file *file_priv)
4867{
4868 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
4869
4870 /* Clean up our request list when the client is going away, so that
4871 * later retire_requests won't dereference our soon-to-be-gone
4872 * file_priv.
4873 */
4874 mutex_lock(&dev->struct_mutex);
4875 while (!list_empty(&i915_file_priv->mm.request_list))
4876 list_del_init(i915_file_priv->mm.request_list.next);
4877 mutex_unlock(&dev->struct_mutex);
4878}
Chris Wilson31169712009-09-14 16:50:28 +01004879
Chris Wilson31169712009-09-14 16:50:28 +01004880static int
Chris Wilson1637ef42010-04-20 17:10:35 +01004881i915_gpu_is_active(struct drm_device *dev)
4882{
4883 drm_i915_private_t *dev_priv = dev->dev_private;
4884 int lists_empty;
4885
4886 spin_lock(&dev_priv->mm.active_list_lock);
4887 lists_empty = list_empty(&dev_priv->mm.flushing_list) &&
Zou Nan hai852835f2010-05-21 09:08:56 +08004888 list_empty(&dev_priv->render_ring.active_list);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004889 if (HAS_BSD(dev))
4890 lists_empty &= list_empty(&dev_priv->bsd_ring.active_list);
Chris Wilson1637ef42010-04-20 17:10:35 +01004891 spin_unlock(&dev_priv->mm.active_list_lock);
4892
4893 return !lists_empty;
4894}
4895
4896static int
Dave Chinner7f8275d2010-07-19 14:56:17 +10004897i915_gem_shrink(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask)
Chris Wilson31169712009-09-14 16:50:28 +01004898{
4899 drm_i915_private_t *dev_priv, *next_dev;
4900 struct drm_i915_gem_object *obj_priv, *next_obj;
4901 int cnt = 0;
4902 int would_deadlock = 1;
4903
4904 /* "fast-path" to count number of available objects */
4905 if (nr_to_scan == 0) {
4906 spin_lock(&shrink_list_lock);
4907 list_for_each_entry(dev_priv, &shrink_list, mm.shrink_list) {
4908 struct drm_device *dev = dev_priv->dev;
4909
4910 if (mutex_trylock(&dev->struct_mutex)) {
4911 list_for_each_entry(obj_priv,
4912 &dev_priv->mm.inactive_list,
4913 list)
4914 cnt++;
4915 mutex_unlock(&dev->struct_mutex);
4916 }
4917 }
4918 spin_unlock(&shrink_list_lock);
4919
4920 return (cnt / 100) * sysctl_vfs_cache_pressure;
4921 }
4922
4923 spin_lock(&shrink_list_lock);
4924
Chris Wilson1637ef42010-04-20 17:10:35 +01004925rescan:
Chris Wilson31169712009-09-14 16:50:28 +01004926 /* first scan for clean buffers */
4927 list_for_each_entry_safe(dev_priv, next_dev,
4928 &shrink_list, mm.shrink_list) {
4929 struct drm_device *dev = dev_priv->dev;
4930
4931 if (! mutex_trylock(&dev->struct_mutex))
4932 continue;
4933
4934 spin_unlock(&shrink_list_lock);
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01004935 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004936
Chris Wilson31169712009-09-14 16:50:28 +01004937 list_for_each_entry_safe(obj_priv, next_obj,
4938 &dev_priv->mm.inactive_list,
4939 list) {
4940 if (i915_gem_object_is_purgeable(obj_priv)) {
Daniel Vettera8089e82010-04-09 19:05:09 +00004941 i915_gem_object_unbind(&obj_priv->base);
Chris Wilson31169712009-09-14 16:50:28 +01004942 if (--nr_to_scan <= 0)
4943 break;
4944 }
4945 }
4946
4947 spin_lock(&shrink_list_lock);
4948 mutex_unlock(&dev->struct_mutex);
4949
Chris Wilson963b4832009-09-20 23:03:54 +01004950 would_deadlock = 0;
4951
Chris Wilson31169712009-09-14 16:50:28 +01004952 if (nr_to_scan <= 0)
4953 break;
4954 }
4955
4956 /* second pass, evict/count anything still on the inactive list */
4957 list_for_each_entry_safe(dev_priv, next_dev,
4958 &shrink_list, mm.shrink_list) {
4959 struct drm_device *dev = dev_priv->dev;
4960
4961 if (! mutex_trylock(&dev->struct_mutex))
4962 continue;
4963
4964 spin_unlock(&shrink_list_lock);
4965
4966 list_for_each_entry_safe(obj_priv, next_obj,
4967 &dev_priv->mm.inactive_list,
4968 list) {
4969 if (nr_to_scan > 0) {
Daniel Vettera8089e82010-04-09 19:05:09 +00004970 i915_gem_object_unbind(&obj_priv->base);
Chris Wilson31169712009-09-14 16:50:28 +01004971 nr_to_scan--;
4972 } else
4973 cnt++;
4974 }
4975
4976 spin_lock(&shrink_list_lock);
4977 mutex_unlock(&dev->struct_mutex);
4978
4979 would_deadlock = 0;
4980 }
4981
Chris Wilson1637ef42010-04-20 17:10:35 +01004982 if (nr_to_scan) {
4983 int active = 0;
4984
4985 /*
4986 * We are desperate for pages, so as a last resort, wait
4987 * for the GPU to finish and discard whatever we can.
4988 * This has a dramatic impact to reduce the number of
4989 * OOM-killer events whilst running the GPU aggressively.
4990 */
4991 list_for_each_entry(dev_priv, &shrink_list, mm.shrink_list) {
4992 struct drm_device *dev = dev_priv->dev;
4993
4994 if (!mutex_trylock(&dev->struct_mutex))
4995 continue;
4996
4997 spin_unlock(&shrink_list_lock);
4998
4999 if (i915_gpu_is_active(dev)) {
5000 i915_gpu_idle(dev);
5001 active++;
5002 }
5003
5004 spin_lock(&shrink_list_lock);
5005 mutex_unlock(&dev->struct_mutex);
5006 }
5007
5008 if (active)
5009 goto rescan;
5010 }
5011
Chris Wilson31169712009-09-14 16:50:28 +01005012 spin_unlock(&shrink_list_lock);
5013
5014 if (would_deadlock)
5015 return -1;
5016 else if (cnt > 0)
5017 return (cnt / 100) * sysctl_vfs_cache_pressure;
5018 else
5019 return 0;
5020}
5021
5022static struct shrinker shrinker = {
5023 .shrink = i915_gem_shrink,
5024 .seeks = DEFAULT_SEEKS,
5025};
5026
5027__init void
5028i915_gem_shrinker_init(void)
5029{
5030 register_shrinker(&shrinker);
5031}
5032
5033__exit void
5034i915_gem_shrinker_exit(void)
5035{
5036 unregister_shrinker(&shrinker);
5037}