blob: 0bf4bcd53e97b224624f7fbbcac59a2ebf51d53e [file] [log] [blame]
Eric Anholt673a3942008-07-30 12:06:12 -07001/*
2 * Copyright © 2008 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 *
26 */
27
28#include "drmP.h"
29#include "drm.h"
30#include "i915_drm.h"
31#include "i915_drv.h"
Chris Wilson1c5d22f2009-08-25 11:15:50 +010032#include "i915_trace.h"
Jesse Barnes652c3932009-08-17 13:31:43 -070033#include "intel_drv.h"
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Eric Anholt673a3942008-07-30 12:06:12 -070035#include <linux/swap.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080036#include <linux/pci.h>
Eric Anholt673a3942008-07-30 12:06:12 -070037
Eric Anholte47c68e2008-11-14 13:35:19 -080038static void i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj);
39static void i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj);
40static void i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj);
Eric Anholte47c68e2008-11-14 13:35:19 -080041static int i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj,
42 int write);
43static int i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
44 uint64_t offset,
45 uint64_t size);
46static void i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj);
Eric Anholt673a3942008-07-30 12:06:12 -070047static int i915_gem_object_wait_rendering(struct drm_gem_object *obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -080048static int i915_gem_object_bind_to_gtt(struct drm_gem_object *obj,
49 unsigned alignment);
Jesse Barnesde151cf2008-11-12 10:03:55 -080050static void i915_gem_clear_fence_reg(struct drm_gem_object *obj);
Chris Wilson07f73f62009-09-14 16:50:30 +010051static int i915_gem_evict_something(struct drm_device *dev, int min_size);
Chris Wilsonab5ee572009-09-20 19:25:47 +010052static int i915_gem_evict_from_inactive_list(struct drm_device *dev);
Dave Airlie71acb5e2008-12-30 20:31:46 +100053static int i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
54 struct drm_i915_gem_pwrite *args,
55 struct drm_file *file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -070056
Chris Wilson31169712009-09-14 16:50:28 +010057static LIST_HEAD(shrink_list);
58static DEFINE_SPINLOCK(shrink_list_lock);
59
Jesse Barnes79e53942008-11-07 14:24:08 -080060int i915_gem_do_init(struct drm_device *dev, unsigned long start,
61 unsigned long end)
62{
63 drm_i915_private_t *dev_priv = dev->dev_private;
64
65 if (start >= end ||
66 (start & (PAGE_SIZE - 1)) != 0 ||
67 (end & (PAGE_SIZE - 1)) != 0) {
68 return -EINVAL;
69 }
70
71 drm_mm_init(&dev_priv->mm.gtt_space, start,
72 end - start);
73
74 dev->gtt_total = (uint32_t) (end - start);
75
76 return 0;
77}
Keith Packard6dbe2772008-10-14 21:41:13 -070078
Eric Anholt673a3942008-07-30 12:06:12 -070079int
80i915_gem_init_ioctl(struct drm_device *dev, void *data,
81 struct drm_file *file_priv)
82{
Eric Anholt673a3942008-07-30 12:06:12 -070083 struct drm_i915_gem_init *args = data;
Jesse Barnes79e53942008-11-07 14:24:08 -080084 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -070085
86 mutex_lock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080087 ret = i915_gem_do_init(dev, args->gtt_start, args->gtt_end);
Eric Anholt673a3942008-07-30 12:06:12 -070088 mutex_unlock(&dev->struct_mutex);
89
Jesse Barnes79e53942008-11-07 14:24:08 -080090 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -070091}
92
Eric Anholt5a125c32008-10-22 21:40:13 -070093int
94i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
95 struct drm_file *file_priv)
96{
Eric Anholt5a125c32008-10-22 21:40:13 -070097 struct drm_i915_gem_get_aperture *args = data;
Eric Anholt5a125c32008-10-22 21:40:13 -070098
99 if (!(dev->driver->driver_features & DRIVER_GEM))
100 return -ENODEV;
101
102 args->aper_size = dev->gtt_total;
Keith Packard2678d9d2008-11-20 22:54:54 -0800103 args->aper_available_size = (args->aper_size -
104 atomic_read(&dev->pin_memory));
Eric Anholt5a125c32008-10-22 21:40:13 -0700105
106 return 0;
107}
108
Eric Anholt673a3942008-07-30 12:06:12 -0700109
110/**
111 * Creates a new mm object and returns a handle to it.
112 */
113int
114i915_gem_create_ioctl(struct drm_device *dev, void *data,
115 struct drm_file *file_priv)
116{
117 struct drm_i915_gem_create *args = data;
118 struct drm_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300119 int ret;
120 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700121
122 args->size = roundup(args->size, PAGE_SIZE);
123
124 /* Allocate the new object */
Daniel Vetterac52bc52010-04-09 19:05:06 +0000125 obj = i915_gem_alloc_object(dev, args->size);
Eric Anholt673a3942008-07-30 12:06:12 -0700126 if (obj == NULL)
127 return -ENOMEM;
128
129 ret = drm_gem_handle_create(file_priv, obj, &handle);
Luca Barbieribc9025b2010-02-09 05:49:12 +0000130 drm_gem_object_handle_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700131
132 if (ret)
133 return ret;
134
135 args->handle = handle;
136
137 return 0;
138}
139
Eric Anholt40123c12009-03-09 13:42:30 -0700140static inline int
Eric Anholteb014592009-03-10 11:44:52 -0700141fast_shmem_read(struct page **pages,
142 loff_t page_base, int page_offset,
143 char __user *data,
144 int length)
145{
146 char __iomem *vaddr;
Florian Mickler2bc43b52009-04-06 22:55:41 +0200147 int unwritten;
Eric Anholteb014592009-03-10 11:44:52 -0700148
149 vaddr = kmap_atomic(pages[page_base >> PAGE_SHIFT], KM_USER0);
150 if (vaddr == NULL)
151 return -ENOMEM;
Florian Mickler2bc43b52009-04-06 22:55:41 +0200152 unwritten = __copy_to_user_inatomic(data, vaddr + page_offset, length);
Eric Anholteb014592009-03-10 11:44:52 -0700153 kunmap_atomic(vaddr, KM_USER0);
154
Florian Mickler2bc43b52009-04-06 22:55:41 +0200155 if (unwritten)
156 return -EFAULT;
157
158 return 0;
Eric Anholteb014592009-03-10 11:44:52 -0700159}
160
Eric Anholt280b7132009-03-12 16:56:27 -0700161static int i915_gem_object_needs_bit17_swizzle(struct drm_gem_object *obj)
162{
163 drm_i915_private_t *dev_priv = obj->dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +0100164 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt280b7132009-03-12 16:56:27 -0700165
166 return dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_9_10_17 &&
167 obj_priv->tiling_mode != I915_TILING_NONE;
168}
169
Chris Wilson99a03df2010-05-27 14:15:34 +0100170static inline void
Eric Anholt40123c12009-03-09 13:42:30 -0700171slow_shmem_copy(struct page *dst_page,
172 int dst_offset,
173 struct page *src_page,
174 int src_offset,
175 int length)
176{
177 char *dst_vaddr, *src_vaddr;
178
Chris Wilson99a03df2010-05-27 14:15:34 +0100179 dst_vaddr = kmap(dst_page);
180 src_vaddr = kmap(src_page);
Eric Anholt40123c12009-03-09 13:42:30 -0700181
182 memcpy(dst_vaddr + dst_offset, src_vaddr + src_offset, length);
183
Chris Wilson99a03df2010-05-27 14:15:34 +0100184 kunmap(src_page);
185 kunmap(dst_page);
Eric Anholt40123c12009-03-09 13:42:30 -0700186}
187
Chris Wilson99a03df2010-05-27 14:15:34 +0100188static inline void
Eric Anholt280b7132009-03-12 16:56:27 -0700189slow_shmem_bit17_copy(struct page *gpu_page,
190 int gpu_offset,
191 struct page *cpu_page,
192 int cpu_offset,
193 int length,
194 int is_read)
195{
196 char *gpu_vaddr, *cpu_vaddr;
197
198 /* Use the unswizzled path if this page isn't affected. */
199 if ((page_to_phys(gpu_page) & (1 << 17)) == 0) {
200 if (is_read)
201 return slow_shmem_copy(cpu_page, cpu_offset,
202 gpu_page, gpu_offset, length);
203 else
204 return slow_shmem_copy(gpu_page, gpu_offset,
205 cpu_page, cpu_offset, length);
206 }
207
Chris Wilson99a03df2010-05-27 14:15:34 +0100208 gpu_vaddr = kmap(gpu_page);
209 cpu_vaddr = kmap(cpu_page);
Eric Anholt280b7132009-03-12 16:56:27 -0700210
211 /* Copy the data, XORing A6 with A17 (1). The user already knows he's
212 * XORing with the other bits (A9 for Y, A9 and A10 for X)
213 */
214 while (length > 0) {
215 int cacheline_end = ALIGN(gpu_offset + 1, 64);
216 int this_length = min(cacheline_end - gpu_offset, length);
217 int swizzled_gpu_offset = gpu_offset ^ 64;
218
219 if (is_read) {
220 memcpy(cpu_vaddr + cpu_offset,
221 gpu_vaddr + swizzled_gpu_offset,
222 this_length);
223 } else {
224 memcpy(gpu_vaddr + swizzled_gpu_offset,
225 cpu_vaddr + cpu_offset,
226 this_length);
227 }
228 cpu_offset += this_length;
229 gpu_offset += this_length;
230 length -= this_length;
231 }
232
Chris Wilson99a03df2010-05-27 14:15:34 +0100233 kunmap(cpu_page);
234 kunmap(gpu_page);
Eric Anholt280b7132009-03-12 16:56:27 -0700235}
236
Eric Anholt673a3942008-07-30 12:06:12 -0700237/**
Eric Anholteb014592009-03-10 11:44:52 -0700238 * This is the fast shmem pread path, which attempts to copy_from_user directly
239 * from the backing pages of the object to the user's address space. On a
240 * fault, it fails so we can fall back to i915_gem_shmem_pwrite_slow().
241 */
242static int
243i915_gem_shmem_pread_fast(struct drm_device *dev, struct drm_gem_object *obj,
244 struct drm_i915_gem_pread *args,
245 struct drm_file *file_priv)
246{
Daniel Vetter23010e42010-03-08 13:35:02 +0100247 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700248 ssize_t remain;
249 loff_t offset, page_base;
250 char __user *user_data;
251 int page_offset, page_length;
252 int ret;
253
254 user_data = (char __user *) (uintptr_t) args->data_ptr;
255 remain = args->size;
256
257 mutex_lock(&dev->struct_mutex);
258
Chris Wilson4bdadb92010-01-27 13:36:32 +0000259 ret = i915_gem_object_get_pages(obj, 0);
Eric Anholteb014592009-03-10 11:44:52 -0700260 if (ret != 0)
261 goto fail_unlock;
262
263 ret = i915_gem_object_set_cpu_read_domain_range(obj, args->offset,
264 args->size);
265 if (ret != 0)
266 goto fail_put_pages;
267
Daniel Vetter23010e42010-03-08 13:35:02 +0100268 obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700269 offset = args->offset;
270
271 while (remain > 0) {
272 /* Operation in this page
273 *
274 * page_base = page offset within aperture
275 * page_offset = offset within page
276 * page_length = bytes to copy for this page
277 */
278 page_base = (offset & ~(PAGE_SIZE-1));
279 page_offset = offset & (PAGE_SIZE-1);
280 page_length = remain;
281 if ((page_offset + remain) > PAGE_SIZE)
282 page_length = PAGE_SIZE - page_offset;
283
284 ret = fast_shmem_read(obj_priv->pages,
285 page_base, page_offset,
286 user_data, page_length);
287 if (ret)
288 goto fail_put_pages;
289
290 remain -= page_length;
291 user_data += page_length;
292 offset += page_length;
293 }
294
295fail_put_pages:
296 i915_gem_object_put_pages(obj);
297fail_unlock:
298 mutex_unlock(&dev->struct_mutex);
299
300 return ret;
301}
302
Chris Wilson07f73f62009-09-14 16:50:30 +0100303static int
304i915_gem_object_get_pages_or_evict(struct drm_gem_object *obj)
305{
306 int ret;
307
Chris Wilson4bdadb92010-01-27 13:36:32 +0000308 ret = i915_gem_object_get_pages(obj, __GFP_NORETRY | __GFP_NOWARN);
Chris Wilson07f73f62009-09-14 16:50:30 +0100309
310 /* If we've insufficient memory to map in the pages, attempt
311 * to make some space by throwing out some old buffers.
312 */
313 if (ret == -ENOMEM) {
314 struct drm_device *dev = obj->dev;
Chris Wilson07f73f62009-09-14 16:50:30 +0100315
316 ret = i915_gem_evict_something(dev, obj->size);
317 if (ret)
318 return ret;
319
Chris Wilson4bdadb92010-01-27 13:36:32 +0000320 ret = i915_gem_object_get_pages(obj, 0);
Chris Wilson07f73f62009-09-14 16:50:30 +0100321 }
322
323 return ret;
324}
325
Eric Anholteb014592009-03-10 11:44:52 -0700326/**
327 * This is the fallback shmem pread path, which allocates temporary storage
328 * in kernel space to copy_to_user into outside of the struct_mutex, so we
329 * can copy out of the object's backing pages while holding the struct mutex
330 * and not take page faults.
331 */
332static int
333i915_gem_shmem_pread_slow(struct drm_device *dev, struct drm_gem_object *obj,
334 struct drm_i915_gem_pread *args,
335 struct drm_file *file_priv)
336{
Daniel Vetter23010e42010-03-08 13:35:02 +0100337 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700338 struct mm_struct *mm = current->mm;
339 struct page **user_pages;
340 ssize_t remain;
341 loff_t offset, pinned_pages, i;
342 loff_t first_data_page, last_data_page, num_pages;
343 int shmem_page_index, shmem_page_offset;
344 int data_page_index, data_page_offset;
345 int page_length;
346 int ret;
347 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700348 int do_bit17_swizzling;
Eric Anholteb014592009-03-10 11:44:52 -0700349
350 remain = args->size;
351
352 /* Pin the user pages containing the data. We can't fault while
353 * holding the struct mutex, yet we want to hold it while
354 * dereferencing the user data.
355 */
356 first_data_page = data_ptr / PAGE_SIZE;
357 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
358 num_pages = last_data_page - first_data_page + 1;
359
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700360 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
Eric Anholteb014592009-03-10 11:44:52 -0700361 if (user_pages == NULL)
362 return -ENOMEM;
363
364 down_read(&mm->mmap_sem);
365 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
Eric Anholte5e9ecd2009-04-07 16:01:22 -0700366 num_pages, 1, 0, user_pages, NULL);
Eric Anholteb014592009-03-10 11:44:52 -0700367 up_read(&mm->mmap_sem);
368 if (pinned_pages < num_pages) {
369 ret = -EFAULT;
370 goto fail_put_user_pages;
371 }
372
Eric Anholt280b7132009-03-12 16:56:27 -0700373 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
374
Eric Anholteb014592009-03-10 11:44:52 -0700375 mutex_lock(&dev->struct_mutex);
376
Chris Wilson07f73f62009-09-14 16:50:30 +0100377 ret = i915_gem_object_get_pages_or_evict(obj);
378 if (ret)
Eric Anholteb014592009-03-10 11:44:52 -0700379 goto fail_unlock;
380
381 ret = i915_gem_object_set_cpu_read_domain_range(obj, args->offset,
382 args->size);
383 if (ret != 0)
384 goto fail_put_pages;
385
Daniel Vetter23010e42010-03-08 13:35:02 +0100386 obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700387 offset = args->offset;
388
389 while (remain > 0) {
390 /* Operation in this page
391 *
392 * shmem_page_index = page number within shmem file
393 * shmem_page_offset = offset within page in shmem file
394 * data_page_index = page number in get_user_pages return
395 * data_page_offset = offset with data_page_index page.
396 * page_length = bytes to copy for this page
397 */
398 shmem_page_index = offset / PAGE_SIZE;
399 shmem_page_offset = offset & ~PAGE_MASK;
400 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
401 data_page_offset = data_ptr & ~PAGE_MASK;
402
403 page_length = remain;
404 if ((shmem_page_offset + page_length) > PAGE_SIZE)
405 page_length = PAGE_SIZE - shmem_page_offset;
406 if ((data_page_offset + page_length) > PAGE_SIZE)
407 page_length = PAGE_SIZE - data_page_offset;
408
Eric Anholt280b7132009-03-12 16:56:27 -0700409 if (do_bit17_swizzling) {
Chris Wilson99a03df2010-05-27 14:15:34 +0100410 slow_shmem_bit17_copy(obj_priv->pages[shmem_page_index],
Eric Anholt280b7132009-03-12 16:56:27 -0700411 shmem_page_offset,
Chris Wilson99a03df2010-05-27 14:15:34 +0100412 user_pages[data_page_index],
413 data_page_offset,
414 page_length,
415 1);
416 } else {
417 slow_shmem_copy(user_pages[data_page_index],
418 data_page_offset,
419 obj_priv->pages[shmem_page_index],
420 shmem_page_offset,
421 page_length);
Eric Anholt280b7132009-03-12 16:56:27 -0700422 }
Eric Anholteb014592009-03-10 11:44:52 -0700423
424 remain -= page_length;
425 data_ptr += page_length;
426 offset += page_length;
427 }
428
429fail_put_pages:
430 i915_gem_object_put_pages(obj);
431fail_unlock:
432 mutex_unlock(&dev->struct_mutex);
433fail_put_user_pages:
434 for (i = 0; i < pinned_pages; i++) {
435 SetPageDirty(user_pages[i]);
436 page_cache_release(user_pages[i]);
437 }
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700438 drm_free_large(user_pages);
Eric Anholteb014592009-03-10 11:44:52 -0700439
440 return ret;
441}
442
Eric Anholt673a3942008-07-30 12:06:12 -0700443/**
444 * Reads data from the object referenced by handle.
445 *
446 * On error, the contents of *data are undefined.
447 */
448int
449i915_gem_pread_ioctl(struct drm_device *dev, void *data,
450 struct drm_file *file_priv)
451{
452 struct drm_i915_gem_pread *args = data;
453 struct drm_gem_object *obj;
454 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -0700455 int ret;
456
457 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
458 if (obj == NULL)
459 return -EBADF;
Daniel Vetter23010e42010-03-08 13:35:02 +0100460 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700461
462 /* Bounds check source.
463 *
464 * XXX: This could use review for overflow issues...
465 */
466 if (args->offset > obj->size || args->size > obj->size ||
467 args->offset + args->size > obj->size) {
Luca Barbieribc9025b2010-02-09 05:49:12 +0000468 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700469 return -EINVAL;
470 }
471
Eric Anholt280b7132009-03-12 16:56:27 -0700472 if (i915_gem_object_needs_bit17_swizzle(obj)) {
Eric Anholteb014592009-03-10 11:44:52 -0700473 ret = i915_gem_shmem_pread_slow(dev, obj, args, file_priv);
Eric Anholt280b7132009-03-12 16:56:27 -0700474 } else {
475 ret = i915_gem_shmem_pread_fast(dev, obj, args, file_priv);
476 if (ret != 0)
477 ret = i915_gem_shmem_pread_slow(dev, obj, args,
478 file_priv);
479 }
Eric Anholt673a3942008-07-30 12:06:12 -0700480
Luca Barbieribc9025b2010-02-09 05:49:12 +0000481 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700482
Eric Anholteb014592009-03-10 11:44:52 -0700483 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700484}
485
Keith Packard0839ccb2008-10-30 19:38:48 -0700486/* This is the fast write path which cannot handle
487 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700488 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700489
Keith Packard0839ccb2008-10-30 19:38:48 -0700490static inline int
491fast_user_write(struct io_mapping *mapping,
492 loff_t page_base, int page_offset,
493 char __user *user_data,
494 int length)
495{
496 char *vaddr_atomic;
497 unsigned long unwritten;
498
499 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
500 unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
501 user_data, length);
502 io_mapping_unmap_atomic(vaddr_atomic);
503 if (unwritten)
504 return -EFAULT;
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700505 return 0;
Keith Packard0839ccb2008-10-30 19:38:48 -0700506}
507
508/* Here's the write path which can sleep for
509 * page faults
510 */
511
Chris Wilsonab34c222010-05-27 14:15:35 +0100512static inline void
Eric Anholt3de09aa2009-03-09 09:42:23 -0700513slow_kernel_write(struct io_mapping *mapping,
514 loff_t gtt_base, int gtt_offset,
515 struct page *user_page, int user_offset,
516 int length)
Keith Packard0839ccb2008-10-30 19:38:48 -0700517{
Chris Wilsonab34c222010-05-27 14:15:35 +0100518 char __iomem *dst_vaddr;
519 char *src_vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -0700520
Chris Wilsonab34c222010-05-27 14:15:35 +0100521 dst_vaddr = io_mapping_map_wc(mapping, gtt_base);
522 src_vaddr = kmap(user_page);
523
524 memcpy_toio(dst_vaddr + gtt_offset,
525 src_vaddr + user_offset,
526 length);
527
528 kunmap(user_page);
529 io_mapping_unmap(dst_vaddr);
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700530}
531
Eric Anholt40123c12009-03-09 13:42:30 -0700532static inline int
533fast_shmem_write(struct page **pages,
534 loff_t page_base, int page_offset,
535 char __user *data,
536 int length)
537{
538 char __iomem *vaddr;
Dave Airlied0088772009-03-28 20:29:48 -0400539 unsigned long unwritten;
Eric Anholt40123c12009-03-09 13:42:30 -0700540
541 vaddr = kmap_atomic(pages[page_base >> PAGE_SHIFT], KM_USER0);
542 if (vaddr == NULL)
543 return -ENOMEM;
Dave Airlied0088772009-03-28 20:29:48 -0400544 unwritten = __copy_from_user_inatomic(vaddr + page_offset, data, length);
Eric Anholt40123c12009-03-09 13:42:30 -0700545 kunmap_atomic(vaddr, KM_USER0);
546
Dave Airlied0088772009-03-28 20:29:48 -0400547 if (unwritten)
548 return -EFAULT;
Eric Anholt40123c12009-03-09 13:42:30 -0700549 return 0;
550}
551
Eric Anholt3de09aa2009-03-09 09:42:23 -0700552/**
553 * This is the fast pwrite path, where we copy the data directly from the
554 * user into the GTT, uncached.
555 */
Eric Anholt673a3942008-07-30 12:06:12 -0700556static int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700557i915_gem_gtt_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
558 struct drm_i915_gem_pwrite *args,
559 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700560{
Daniel Vetter23010e42010-03-08 13:35:02 +0100561 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Keith Packard0839ccb2008-10-30 19:38:48 -0700562 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700563 ssize_t remain;
Keith Packard0839ccb2008-10-30 19:38:48 -0700564 loff_t offset, page_base;
Eric Anholt673a3942008-07-30 12:06:12 -0700565 char __user *user_data;
Keith Packard0839ccb2008-10-30 19:38:48 -0700566 int page_offset, page_length;
567 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700568
569 user_data = (char __user *) (uintptr_t) args->data_ptr;
570 remain = args->size;
571 if (!access_ok(VERIFY_READ, user_data, remain))
572 return -EFAULT;
573
574
575 mutex_lock(&dev->struct_mutex);
576 ret = i915_gem_object_pin(obj, 0);
577 if (ret) {
578 mutex_unlock(&dev->struct_mutex);
579 return ret;
580 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800581 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
Eric Anholt673a3942008-07-30 12:06:12 -0700582 if (ret)
583 goto fail;
584
Daniel Vetter23010e42010-03-08 13:35:02 +0100585 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700586 offset = obj_priv->gtt_offset + args->offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700587
588 while (remain > 0) {
589 /* Operation in this page
590 *
Keith Packard0839ccb2008-10-30 19:38:48 -0700591 * page_base = page offset within aperture
592 * page_offset = offset within page
593 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -0700594 */
Keith Packard0839ccb2008-10-30 19:38:48 -0700595 page_base = (offset & ~(PAGE_SIZE-1));
596 page_offset = offset & (PAGE_SIZE-1);
597 page_length = remain;
598 if ((page_offset + remain) > PAGE_SIZE)
599 page_length = PAGE_SIZE - page_offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700600
Keith Packard0839ccb2008-10-30 19:38:48 -0700601 ret = fast_user_write (dev_priv->mm.gtt_mapping, page_base,
602 page_offset, user_data, page_length);
Eric Anholt673a3942008-07-30 12:06:12 -0700603
Keith Packard0839ccb2008-10-30 19:38:48 -0700604 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -0700605 * source page isn't available. Return the error and we'll
606 * retry in the slow path.
Keith Packard0839ccb2008-10-30 19:38:48 -0700607 */
Eric Anholt3de09aa2009-03-09 09:42:23 -0700608 if (ret)
609 goto fail;
Eric Anholt673a3942008-07-30 12:06:12 -0700610
Keith Packard0839ccb2008-10-30 19:38:48 -0700611 remain -= page_length;
612 user_data += page_length;
613 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700614 }
Eric Anholt673a3942008-07-30 12:06:12 -0700615
616fail:
617 i915_gem_object_unpin(obj);
618 mutex_unlock(&dev->struct_mutex);
619
620 return ret;
621}
622
Eric Anholt3de09aa2009-03-09 09:42:23 -0700623/**
624 * This is the fallback GTT pwrite path, which uses get_user_pages to pin
625 * the memory and maps it using kmap_atomic for copying.
626 *
627 * This code resulted in x11perf -rgb10text consuming about 10% more CPU
628 * than using i915_gem_gtt_pwrite_fast on a G45 (32-bit).
629 */
Eric Anholt3043c602008-10-02 12:24:47 -0700630static int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700631i915_gem_gtt_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
632 struct drm_i915_gem_pwrite *args,
633 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700634{
Daniel Vetter23010e42010-03-08 13:35:02 +0100635 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700636 drm_i915_private_t *dev_priv = dev->dev_private;
637 ssize_t remain;
638 loff_t gtt_page_base, offset;
639 loff_t first_data_page, last_data_page, num_pages;
640 loff_t pinned_pages, i;
641 struct page **user_pages;
642 struct mm_struct *mm = current->mm;
643 int gtt_page_offset, data_page_offset, data_page_index, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700644 int ret;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700645 uint64_t data_ptr = args->data_ptr;
646
647 remain = args->size;
648
649 /* Pin the user pages containing the data. We can't fault while
650 * holding the struct mutex, and all of the pwrite implementations
651 * want to hold it while dereferencing the user data.
652 */
653 first_data_page = data_ptr / PAGE_SIZE;
654 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
655 num_pages = last_data_page - first_data_page + 1;
656
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700657 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
Eric Anholt3de09aa2009-03-09 09:42:23 -0700658 if (user_pages == NULL)
659 return -ENOMEM;
660
661 down_read(&mm->mmap_sem);
662 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
663 num_pages, 0, 0, user_pages, NULL);
664 up_read(&mm->mmap_sem);
665 if (pinned_pages < num_pages) {
666 ret = -EFAULT;
667 goto out_unpin_pages;
668 }
669
670 mutex_lock(&dev->struct_mutex);
671 ret = i915_gem_object_pin(obj, 0);
672 if (ret)
673 goto out_unlock;
674
675 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
676 if (ret)
677 goto out_unpin_object;
678
Daniel Vetter23010e42010-03-08 13:35:02 +0100679 obj_priv = to_intel_bo(obj);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700680 offset = obj_priv->gtt_offset + args->offset;
681
682 while (remain > 0) {
683 /* Operation in this page
684 *
685 * gtt_page_base = page offset within aperture
686 * gtt_page_offset = offset within page in aperture
687 * data_page_index = page number in get_user_pages return
688 * data_page_offset = offset with data_page_index page.
689 * page_length = bytes to copy for this page
690 */
691 gtt_page_base = offset & PAGE_MASK;
692 gtt_page_offset = offset & ~PAGE_MASK;
693 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
694 data_page_offset = data_ptr & ~PAGE_MASK;
695
696 page_length = remain;
697 if ((gtt_page_offset + page_length) > PAGE_SIZE)
698 page_length = PAGE_SIZE - gtt_page_offset;
699 if ((data_page_offset + page_length) > PAGE_SIZE)
700 page_length = PAGE_SIZE - data_page_offset;
701
Chris Wilsonab34c222010-05-27 14:15:35 +0100702 slow_kernel_write(dev_priv->mm.gtt_mapping,
703 gtt_page_base, gtt_page_offset,
704 user_pages[data_page_index],
705 data_page_offset,
706 page_length);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700707
708 remain -= page_length;
709 offset += page_length;
710 data_ptr += page_length;
711 }
712
713out_unpin_object:
714 i915_gem_object_unpin(obj);
715out_unlock:
716 mutex_unlock(&dev->struct_mutex);
717out_unpin_pages:
718 for (i = 0; i < pinned_pages; i++)
719 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700720 drm_free_large(user_pages);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700721
722 return ret;
723}
724
Eric Anholt40123c12009-03-09 13:42:30 -0700725/**
726 * This is the fast shmem pwrite path, which attempts to directly
727 * copy_from_user into the kmapped pages backing the object.
728 */
Eric Anholt673a3942008-07-30 12:06:12 -0700729static int
Eric Anholt40123c12009-03-09 13:42:30 -0700730i915_gem_shmem_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
731 struct drm_i915_gem_pwrite *args,
732 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700733{
Daniel Vetter23010e42010-03-08 13:35:02 +0100734 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700735 ssize_t remain;
736 loff_t offset, page_base;
737 char __user *user_data;
738 int page_offset, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700739 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -0700740
741 user_data = (char __user *) (uintptr_t) args->data_ptr;
742 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700743
744 mutex_lock(&dev->struct_mutex);
745
Chris Wilson4bdadb92010-01-27 13:36:32 +0000746 ret = i915_gem_object_get_pages(obj, 0);
Eric Anholt40123c12009-03-09 13:42:30 -0700747 if (ret != 0)
748 goto fail_unlock;
749
Eric Anholte47c68e2008-11-14 13:35:19 -0800750 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Eric Anholt40123c12009-03-09 13:42:30 -0700751 if (ret != 0)
752 goto fail_put_pages;
Eric Anholt673a3942008-07-30 12:06:12 -0700753
Daniel Vetter23010e42010-03-08 13:35:02 +0100754 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700755 offset = args->offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700756 obj_priv->dirty = 1;
Eric Anholt673a3942008-07-30 12:06:12 -0700757
Eric Anholt40123c12009-03-09 13:42:30 -0700758 while (remain > 0) {
759 /* Operation in this page
760 *
761 * page_base = page offset within aperture
762 * page_offset = offset within page
763 * page_length = bytes to copy for this page
764 */
765 page_base = (offset & ~(PAGE_SIZE-1));
766 page_offset = offset & (PAGE_SIZE-1);
767 page_length = remain;
768 if ((page_offset + remain) > PAGE_SIZE)
769 page_length = PAGE_SIZE - page_offset;
770
771 ret = fast_shmem_write(obj_priv->pages,
772 page_base, page_offset,
773 user_data, page_length);
774 if (ret)
775 goto fail_put_pages;
776
777 remain -= page_length;
778 user_data += page_length;
779 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700780 }
781
Eric Anholt40123c12009-03-09 13:42:30 -0700782fail_put_pages:
783 i915_gem_object_put_pages(obj);
784fail_unlock:
Eric Anholt673a3942008-07-30 12:06:12 -0700785 mutex_unlock(&dev->struct_mutex);
786
Eric Anholt40123c12009-03-09 13:42:30 -0700787 return ret;
788}
789
790/**
791 * This is the fallback shmem pwrite path, which uses get_user_pages to pin
792 * the memory and maps it using kmap_atomic for copying.
793 *
794 * This avoids taking mmap_sem for faulting on the user's address while the
795 * struct_mutex is held.
796 */
797static int
798i915_gem_shmem_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
799 struct drm_i915_gem_pwrite *args,
800 struct drm_file *file_priv)
801{
Daniel Vetter23010e42010-03-08 13:35:02 +0100802 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700803 struct mm_struct *mm = current->mm;
804 struct page **user_pages;
805 ssize_t remain;
806 loff_t offset, pinned_pages, i;
807 loff_t first_data_page, last_data_page, num_pages;
808 int shmem_page_index, shmem_page_offset;
809 int data_page_index, data_page_offset;
810 int page_length;
811 int ret;
812 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700813 int do_bit17_swizzling;
Eric Anholt40123c12009-03-09 13:42:30 -0700814
815 remain = args->size;
816
817 /* Pin the user pages containing the data. We can't fault while
818 * holding the struct mutex, and all of the pwrite implementations
819 * want to hold it while dereferencing the user data.
820 */
821 first_data_page = data_ptr / PAGE_SIZE;
822 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
823 num_pages = last_data_page - first_data_page + 1;
824
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700825 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
Eric Anholt40123c12009-03-09 13:42:30 -0700826 if (user_pages == NULL)
827 return -ENOMEM;
828
829 down_read(&mm->mmap_sem);
830 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
831 num_pages, 0, 0, user_pages, NULL);
832 up_read(&mm->mmap_sem);
833 if (pinned_pages < num_pages) {
834 ret = -EFAULT;
835 goto fail_put_user_pages;
836 }
837
Eric Anholt280b7132009-03-12 16:56:27 -0700838 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
839
Eric Anholt40123c12009-03-09 13:42:30 -0700840 mutex_lock(&dev->struct_mutex);
841
Chris Wilson07f73f62009-09-14 16:50:30 +0100842 ret = i915_gem_object_get_pages_or_evict(obj);
843 if (ret)
Eric Anholt40123c12009-03-09 13:42:30 -0700844 goto fail_unlock;
845
846 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
847 if (ret != 0)
848 goto fail_put_pages;
849
Daniel Vetter23010e42010-03-08 13:35:02 +0100850 obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700851 offset = args->offset;
852 obj_priv->dirty = 1;
853
854 while (remain > 0) {
855 /* Operation in this page
856 *
857 * shmem_page_index = page number within shmem file
858 * shmem_page_offset = offset within page in shmem file
859 * data_page_index = page number in get_user_pages return
860 * data_page_offset = offset with data_page_index page.
861 * page_length = bytes to copy for this page
862 */
863 shmem_page_index = offset / PAGE_SIZE;
864 shmem_page_offset = offset & ~PAGE_MASK;
865 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
866 data_page_offset = data_ptr & ~PAGE_MASK;
867
868 page_length = remain;
869 if ((shmem_page_offset + page_length) > PAGE_SIZE)
870 page_length = PAGE_SIZE - shmem_page_offset;
871 if ((data_page_offset + page_length) > PAGE_SIZE)
872 page_length = PAGE_SIZE - data_page_offset;
873
Eric Anholt280b7132009-03-12 16:56:27 -0700874 if (do_bit17_swizzling) {
Chris Wilson99a03df2010-05-27 14:15:34 +0100875 slow_shmem_bit17_copy(obj_priv->pages[shmem_page_index],
Eric Anholt280b7132009-03-12 16:56:27 -0700876 shmem_page_offset,
877 user_pages[data_page_index],
878 data_page_offset,
Chris Wilson99a03df2010-05-27 14:15:34 +0100879 page_length,
880 0);
881 } else {
882 slow_shmem_copy(obj_priv->pages[shmem_page_index],
883 shmem_page_offset,
884 user_pages[data_page_index],
885 data_page_offset,
886 page_length);
Eric Anholt280b7132009-03-12 16:56:27 -0700887 }
Eric Anholt40123c12009-03-09 13:42:30 -0700888
889 remain -= page_length;
890 data_ptr += page_length;
891 offset += page_length;
892 }
893
894fail_put_pages:
895 i915_gem_object_put_pages(obj);
896fail_unlock:
897 mutex_unlock(&dev->struct_mutex);
898fail_put_user_pages:
899 for (i = 0; i < pinned_pages; i++)
900 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700901 drm_free_large(user_pages);
Eric Anholt40123c12009-03-09 13:42:30 -0700902
903 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700904}
905
906/**
907 * Writes data to the object referenced by handle.
908 *
909 * On error, the contents of the buffer that were to be modified are undefined.
910 */
911int
912i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
913 struct drm_file *file_priv)
914{
915 struct drm_i915_gem_pwrite *args = data;
916 struct drm_gem_object *obj;
917 struct drm_i915_gem_object *obj_priv;
918 int ret = 0;
919
920 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
921 if (obj == NULL)
922 return -EBADF;
Daniel Vetter23010e42010-03-08 13:35:02 +0100923 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700924
925 /* Bounds check destination.
926 *
927 * XXX: This could use review for overflow issues...
928 */
929 if (args->offset > obj->size || args->size > obj->size ||
930 args->offset + args->size > obj->size) {
Luca Barbieribc9025b2010-02-09 05:49:12 +0000931 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700932 return -EINVAL;
933 }
934
935 /* We can only do the GTT pwrite on untiled buffers, as otherwise
936 * it would end up going through the fenced access, and we'll get
937 * different detiling behavior between reading and writing.
938 * pread/pwrite currently are reading and writing from the CPU
939 * perspective, requiring manual detiling by the client.
940 */
Dave Airlie71acb5e2008-12-30 20:31:46 +1000941 if (obj_priv->phys_obj)
942 ret = i915_gem_phys_pwrite(dev, obj, args, file_priv);
943 else if (obj_priv->tiling_mode == I915_TILING_NONE &&
Chris Wilson9b8c4a02010-05-27 14:21:01 +0100944 dev->gtt_total != 0 &&
945 obj->write_domain != I915_GEM_DOMAIN_CPU) {
Eric Anholt3de09aa2009-03-09 09:42:23 -0700946 ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file_priv);
947 if (ret == -EFAULT) {
948 ret = i915_gem_gtt_pwrite_slow(dev, obj, args,
949 file_priv);
950 }
Eric Anholt280b7132009-03-12 16:56:27 -0700951 } else if (i915_gem_object_needs_bit17_swizzle(obj)) {
952 ret = i915_gem_shmem_pwrite_slow(dev, obj, args, file_priv);
Eric Anholt40123c12009-03-09 13:42:30 -0700953 } else {
954 ret = i915_gem_shmem_pwrite_fast(dev, obj, args, file_priv);
955 if (ret == -EFAULT) {
956 ret = i915_gem_shmem_pwrite_slow(dev, obj, args,
957 file_priv);
958 }
959 }
Eric Anholt673a3942008-07-30 12:06:12 -0700960
961#if WATCH_PWRITE
962 if (ret)
963 DRM_INFO("pwrite failed %d\n", ret);
964#endif
965
Luca Barbieribc9025b2010-02-09 05:49:12 +0000966 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700967
968 return ret;
969}
970
971/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800972 * Called when user space prepares to use an object with the CPU, either
973 * through the mmap ioctl's mapping or a GTT mapping.
Eric Anholt673a3942008-07-30 12:06:12 -0700974 */
975int
976i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
977 struct drm_file *file_priv)
978{
Eric Anholta09ba7f2009-08-29 12:49:51 -0700979 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700980 struct drm_i915_gem_set_domain *args = data;
981 struct drm_gem_object *obj;
Jesse Barnes652c3932009-08-17 13:31:43 -0700982 struct drm_i915_gem_object *obj_priv;
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800983 uint32_t read_domains = args->read_domains;
984 uint32_t write_domain = args->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -0700985 int ret;
986
987 if (!(dev->driver->driver_features & DRIVER_GEM))
988 return -ENODEV;
989
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800990 /* Only handle setting domains to types used by the CPU. */
Chris Wilson21d509e2009-06-06 09:46:02 +0100991 if (write_domain & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800992 return -EINVAL;
993
Chris Wilson21d509e2009-06-06 09:46:02 +0100994 if (read_domains & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800995 return -EINVAL;
996
997 /* Having something in the write domain implies it's in the read
998 * domain, and only that read domain. Enforce that in the request.
999 */
1000 if (write_domain != 0 && read_domains != write_domain)
1001 return -EINVAL;
1002
Eric Anholt673a3942008-07-30 12:06:12 -07001003 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1004 if (obj == NULL)
1005 return -EBADF;
Daniel Vetter23010e42010-03-08 13:35:02 +01001006 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001007
1008 mutex_lock(&dev->struct_mutex);
Jesse Barnes652c3932009-08-17 13:31:43 -07001009
1010 intel_mark_busy(dev, obj);
1011
Eric Anholt673a3942008-07-30 12:06:12 -07001012#if WATCH_BUF
Krzysztof Halasacfd43c02009-06-20 00:31:28 +02001013 DRM_INFO("set_domain_ioctl %p(%zd), %08x %08x\n",
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001014 obj, obj->size, read_domains, write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07001015#endif
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001016 if (read_domains & I915_GEM_DOMAIN_GTT) {
1017 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
Eric Anholt02354392008-11-26 13:58:13 -08001018
Eric Anholta09ba7f2009-08-29 12:49:51 -07001019 /* Update the LRU on the fence for the CPU access that's
1020 * about to occur.
1021 */
1022 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001023 struct drm_i915_fence_reg *reg =
1024 &dev_priv->fence_regs[obj_priv->fence_reg];
1025 list_move_tail(&reg->lru_list,
Eric Anholta09ba7f2009-08-29 12:49:51 -07001026 &dev_priv->mm.fence_list);
1027 }
1028
Eric Anholt02354392008-11-26 13:58:13 -08001029 /* Silently promote "you're not bound, there was nothing to do"
1030 * to success, since the client was just asking us to
1031 * make sure everything was done.
1032 */
1033 if (ret == -EINVAL)
1034 ret = 0;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001035 } else {
Eric Anholte47c68e2008-11-14 13:35:19 -08001036 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001037 }
1038
Eric Anholt673a3942008-07-30 12:06:12 -07001039 drm_gem_object_unreference(obj);
1040 mutex_unlock(&dev->struct_mutex);
1041 return ret;
1042}
1043
1044/**
1045 * Called when user space has done writes to this buffer
1046 */
1047int
1048i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
1049 struct drm_file *file_priv)
1050{
1051 struct drm_i915_gem_sw_finish *args = data;
1052 struct drm_gem_object *obj;
1053 struct drm_i915_gem_object *obj_priv;
1054 int ret = 0;
1055
1056 if (!(dev->driver->driver_features & DRIVER_GEM))
1057 return -ENODEV;
1058
1059 mutex_lock(&dev->struct_mutex);
1060 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1061 if (obj == NULL) {
1062 mutex_unlock(&dev->struct_mutex);
1063 return -EBADF;
1064 }
1065
1066#if WATCH_BUF
Krzysztof Halasacfd43c02009-06-20 00:31:28 +02001067 DRM_INFO("%s: sw_finish %d (%p %zd)\n",
Eric Anholt673a3942008-07-30 12:06:12 -07001068 __func__, args->handle, obj, obj->size);
1069#endif
Daniel Vetter23010e42010-03-08 13:35:02 +01001070 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001071
1072 /* Pinned buffers may be scanout, so flush the cache */
Eric Anholte47c68e2008-11-14 13:35:19 -08001073 if (obj_priv->pin_count)
1074 i915_gem_object_flush_cpu_write_domain(obj);
1075
Eric Anholt673a3942008-07-30 12:06:12 -07001076 drm_gem_object_unreference(obj);
1077 mutex_unlock(&dev->struct_mutex);
1078 return ret;
1079}
1080
1081/**
1082 * Maps the contents of an object, returning the address it is mapped
1083 * into.
1084 *
1085 * While the mapping holds a reference on the contents of the object, it doesn't
1086 * imply a ref on the object itself.
1087 */
1088int
1089i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
1090 struct drm_file *file_priv)
1091{
1092 struct drm_i915_gem_mmap *args = data;
1093 struct drm_gem_object *obj;
1094 loff_t offset;
1095 unsigned long addr;
1096
1097 if (!(dev->driver->driver_features & DRIVER_GEM))
1098 return -ENODEV;
1099
1100 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1101 if (obj == NULL)
1102 return -EBADF;
1103
1104 offset = args->offset;
1105
1106 down_write(&current->mm->mmap_sem);
1107 addr = do_mmap(obj->filp, 0, args->size,
1108 PROT_READ | PROT_WRITE, MAP_SHARED,
1109 args->offset);
1110 up_write(&current->mm->mmap_sem);
Luca Barbieribc9025b2010-02-09 05:49:12 +00001111 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001112 if (IS_ERR((void *)addr))
1113 return addr;
1114
1115 args->addr_ptr = (uint64_t) addr;
1116
1117 return 0;
1118}
1119
Jesse Barnesde151cf2008-11-12 10:03:55 -08001120/**
1121 * i915_gem_fault - fault a page into the GTT
1122 * vma: VMA in question
1123 * vmf: fault info
1124 *
1125 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1126 * from userspace. The fault handler takes care of binding the object to
1127 * the GTT (if needed), allocating and programming a fence register (again,
1128 * only if needed based on whether the old reg is still valid or the object
1129 * is tiled) and inserting a new PTE into the faulting process.
1130 *
1131 * Note that the faulting process may involve evicting existing objects
1132 * from the GTT and/or fence registers to make room. So performance may
1133 * suffer if the GTT working set is large or there are few fence registers
1134 * left.
1135 */
1136int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1137{
1138 struct drm_gem_object *obj = vma->vm_private_data;
1139 struct drm_device *dev = obj->dev;
1140 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001141 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001142 pgoff_t page_offset;
1143 unsigned long pfn;
1144 int ret = 0;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001145 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001146
1147 /* We don't use vmf->pgoff since that has the fake offset */
1148 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1149 PAGE_SHIFT;
1150
1151 /* Now bind it into the GTT if needed */
1152 mutex_lock(&dev->struct_mutex);
1153 if (!obj_priv->gtt_space) {
Chris Wilsone67b8ce2009-09-14 16:50:26 +01001154 ret = i915_gem_object_bind_to_gtt(obj, 0);
Chris Wilsonc7150892009-09-23 00:43:56 +01001155 if (ret)
1156 goto unlock;
Kristian Høgsberg07f4f3e2009-05-27 14:37:28 -04001157
Jesse Barnes14b60392009-05-20 16:47:08 -04001158 list_add_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001159
1160 ret = i915_gem_object_set_to_gtt_domain(obj, write);
Chris Wilsonc7150892009-09-23 00:43:56 +01001161 if (ret)
1162 goto unlock;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001163 }
1164
1165 /* Need a new fence register? */
Eric Anholta09ba7f2009-08-29 12:49:51 -07001166 if (obj_priv->tiling_mode != I915_TILING_NONE) {
Chris Wilson8c4b8c32009-06-17 22:08:52 +01001167 ret = i915_gem_object_get_fence_reg(obj);
Chris Wilsonc7150892009-09-23 00:43:56 +01001168 if (ret)
1169 goto unlock;
Eric Anholtd9ddcb92009-01-27 10:33:49 -08001170 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001171
1172 pfn = ((dev->agp->base + obj_priv->gtt_offset) >> PAGE_SHIFT) +
1173 page_offset;
1174
1175 /* Finally, remap it using the new GTT offset */
1176 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
Chris Wilsonc7150892009-09-23 00:43:56 +01001177unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001178 mutex_unlock(&dev->struct_mutex);
1179
1180 switch (ret) {
Chris Wilsonc7150892009-09-23 00:43:56 +01001181 case 0:
1182 case -ERESTARTSYS:
1183 return VM_FAULT_NOPAGE;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001184 case -ENOMEM:
1185 case -EAGAIN:
1186 return VM_FAULT_OOM;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001187 default:
Chris Wilsonc7150892009-09-23 00:43:56 +01001188 return VM_FAULT_SIGBUS;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001189 }
1190}
1191
1192/**
1193 * i915_gem_create_mmap_offset - create a fake mmap offset for an object
1194 * @obj: obj in question
1195 *
1196 * GEM memory mapping works by handing back to userspace a fake mmap offset
1197 * it can use in a subsequent mmap(2) call. The DRM core code then looks
1198 * up the object based on the offset and sets up the various memory mapping
1199 * structures.
1200 *
1201 * This routine allocates and attaches a fake offset for @obj.
1202 */
1203static int
1204i915_gem_create_mmap_offset(struct drm_gem_object *obj)
1205{
1206 struct drm_device *dev = obj->dev;
1207 struct drm_gem_mm *mm = dev->mm_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001208 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001209 struct drm_map_list *list;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +11001210 struct drm_local_map *map;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001211 int ret = 0;
1212
1213 /* Set the object up for mmap'ing */
1214 list = &obj->map_list;
Eric Anholt9a298b22009-03-24 12:23:04 -07001215 list->map = kzalloc(sizeof(struct drm_map_list), GFP_KERNEL);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001216 if (!list->map)
1217 return -ENOMEM;
1218
1219 map = list->map;
1220 map->type = _DRM_GEM;
1221 map->size = obj->size;
1222 map->handle = obj;
1223
1224 /* Get a DRM GEM mmap offset allocated... */
1225 list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
1226 obj->size / PAGE_SIZE, 0, 0);
1227 if (!list->file_offset_node) {
1228 DRM_ERROR("failed to allocate offset for bo %d\n", obj->name);
1229 ret = -ENOMEM;
1230 goto out_free_list;
1231 }
1232
1233 list->file_offset_node = drm_mm_get_block(list->file_offset_node,
1234 obj->size / PAGE_SIZE, 0);
1235 if (!list->file_offset_node) {
1236 ret = -ENOMEM;
1237 goto out_free_list;
1238 }
1239
1240 list->hash.key = list->file_offset_node->start;
1241 if (drm_ht_insert_item(&mm->offset_hash, &list->hash)) {
1242 DRM_ERROR("failed to add to map hash\n");
Chris Wilson5618ca62009-12-02 15:15:30 +00001243 ret = -ENOMEM;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001244 goto out_free_mm;
1245 }
1246
1247 /* By now we should be all set, any drm_mmap request on the offset
1248 * below will get to our mmap & fault handler */
1249 obj_priv->mmap_offset = ((uint64_t) list->hash.key) << PAGE_SHIFT;
1250
1251 return 0;
1252
1253out_free_mm:
1254 drm_mm_put_block(list->file_offset_node);
1255out_free_list:
Eric Anholt9a298b22009-03-24 12:23:04 -07001256 kfree(list->map);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001257
1258 return ret;
1259}
1260
Chris Wilson901782b2009-07-10 08:18:50 +01001261/**
1262 * i915_gem_release_mmap - remove physical page mappings
1263 * @obj: obj in question
1264 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001265 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01001266 * relinquish ownership of the pages back to the system.
1267 *
1268 * It is vital that we remove the page mapping if we have mapped a tiled
1269 * object through the GTT and then lose the fence register due to
1270 * resource pressure. Similarly if the object has been moved out of the
1271 * aperture, than pages mapped into userspace must be revoked. Removing the
1272 * mapping will then trigger a page fault on the next user access, allowing
1273 * fixup by i915_gem_fault().
1274 */
Eric Anholtd05ca302009-07-10 13:02:26 -07001275void
Chris Wilson901782b2009-07-10 08:18:50 +01001276i915_gem_release_mmap(struct drm_gem_object *obj)
1277{
1278 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001279 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson901782b2009-07-10 08:18:50 +01001280
1281 if (dev->dev_mapping)
1282 unmap_mapping_range(dev->dev_mapping,
1283 obj_priv->mmap_offset, obj->size, 1);
1284}
1285
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001286static void
1287i915_gem_free_mmap_offset(struct drm_gem_object *obj)
1288{
1289 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001290 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001291 struct drm_gem_mm *mm = dev->mm_private;
1292 struct drm_map_list *list;
1293
1294 list = &obj->map_list;
1295 drm_ht_remove_item(&mm->offset_hash, &list->hash);
1296
1297 if (list->file_offset_node) {
1298 drm_mm_put_block(list->file_offset_node);
1299 list->file_offset_node = NULL;
1300 }
1301
1302 if (list->map) {
Eric Anholt9a298b22009-03-24 12:23:04 -07001303 kfree(list->map);
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001304 list->map = NULL;
1305 }
1306
1307 obj_priv->mmap_offset = 0;
1308}
1309
Jesse Barnesde151cf2008-11-12 10:03:55 -08001310/**
1311 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1312 * @obj: object to check
1313 *
1314 * Return the required GTT alignment for an object, taking into account
1315 * potential fence register mapping if needed.
1316 */
1317static uint32_t
1318i915_gem_get_gtt_alignment(struct drm_gem_object *obj)
1319{
1320 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001321 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001322 int start, i;
1323
1324 /*
1325 * Minimum alignment is 4k (GTT page size), but might be greater
1326 * if a fence register is needed for the object.
1327 */
1328 if (IS_I965G(dev) || obj_priv->tiling_mode == I915_TILING_NONE)
1329 return 4096;
1330
1331 /*
1332 * Previous chips need to be aligned to the size of the smallest
1333 * fence register that can contain the object.
1334 */
1335 if (IS_I9XX(dev))
1336 start = 1024*1024;
1337 else
1338 start = 512*1024;
1339
1340 for (i = start; i < obj->size; i <<= 1)
1341 ;
1342
1343 return i;
1344}
1345
1346/**
1347 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1348 * @dev: DRM device
1349 * @data: GTT mapping ioctl data
1350 * @file_priv: GEM object info
1351 *
1352 * Simply returns the fake offset to userspace so it can mmap it.
1353 * The mmap call will end up in drm_gem_mmap(), which will set things
1354 * up so we can get faults in the handler above.
1355 *
1356 * The fault handler will take care of binding the object into the GTT
1357 * (since it may have been evicted to make room for something), allocating
1358 * a fence register, and mapping the appropriate aperture address into
1359 * userspace.
1360 */
1361int
1362i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
1363 struct drm_file *file_priv)
1364{
1365 struct drm_i915_gem_mmap_gtt *args = data;
1366 struct drm_i915_private *dev_priv = dev->dev_private;
1367 struct drm_gem_object *obj;
1368 struct drm_i915_gem_object *obj_priv;
1369 int ret;
1370
1371 if (!(dev->driver->driver_features & DRIVER_GEM))
1372 return -ENODEV;
1373
1374 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1375 if (obj == NULL)
1376 return -EBADF;
1377
1378 mutex_lock(&dev->struct_mutex);
1379
Daniel Vetter23010e42010-03-08 13:35:02 +01001380 obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001381
Chris Wilsonab182822009-09-22 18:46:17 +01001382 if (obj_priv->madv != I915_MADV_WILLNEED) {
1383 DRM_ERROR("Attempting to mmap a purgeable buffer\n");
1384 drm_gem_object_unreference(obj);
1385 mutex_unlock(&dev->struct_mutex);
1386 return -EINVAL;
1387 }
1388
1389
Jesse Barnesde151cf2008-11-12 10:03:55 -08001390 if (!obj_priv->mmap_offset) {
1391 ret = i915_gem_create_mmap_offset(obj);
Chris Wilson13af1062009-02-11 14:26:31 +00001392 if (ret) {
1393 drm_gem_object_unreference(obj);
1394 mutex_unlock(&dev->struct_mutex);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001395 return ret;
Chris Wilson13af1062009-02-11 14:26:31 +00001396 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001397 }
1398
1399 args->offset = obj_priv->mmap_offset;
1400
Jesse Barnesde151cf2008-11-12 10:03:55 -08001401 /*
1402 * Pull it into the GTT so that we have a page list (makes the
1403 * initial fault faster and any subsequent flushing possible).
1404 */
1405 if (!obj_priv->agp_mem) {
Chris Wilsone67b8ce2009-09-14 16:50:26 +01001406 ret = i915_gem_object_bind_to_gtt(obj, 0);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001407 if (ret) {
1408 drm_gem_object_unreference(obj);
1409 mutex_unlock(&dev->struct_mutex);
1410 return ret;
1411 }
Jesse Barnes14b60392009-05-20 16:47:08 -04001412 list_add_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001413 }
1414
1415 drm_gem_object_unreference(obj);
1416 mutex_unlock(&dev->struct_mutex);
1417
1418 return 0;
1419}
1420
Ben Gamari6911a9b2009-04-02 11:24:54 -07001421void
Eric Anholt856fa192009-03-19 14:10:50 -07001422i915_gem_object_put_pages(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07001423{
Daniel Vetter23010e42010-03-08 13:35:02 +01001424 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001425 int page_count = obj->size / PAGE_SIZE;
1426 int i;
1427
Eric Anholt856fa192009-03-19 14:10:50 -07001428 BUG_ON(obj_priv->pages_refcount == 0);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001429 BUG_ON(obj_priv->madv == __I915_MADV_PURGED);
Eric Anholt856fa192009-03-19 14:10:50 -07001430
1431 if (--obj_priv->pages_refcount != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07001432 return;
1433
Eric Anholt280b7132009-03-12 16:56:27 -07001434 if (obj_priv->tiling_mode != I915_TILING_NONE)
1435 i915_gem_object_save_bit_17_swizzle(obj);
1436
Chris Wilson3ef94da2009-09-14 16:50:29 +01001437 if (obj_priv->madv == I915_MADV_DONTNEED)
Chris Wilson13a05fd2009-09-20 23:03:19 +01001438 obj_priv->dirty = 0;
Chris Wilson3ef94da2009-09-14 16:50:29 +01001439
1440 for (i = 0; i < page_count; i++) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01001441 if (obj_priv->dirty)
1442 set_page_dirty(obj_priv->pages[i]);
1443
1444 if (obj_priv->madv == I915_MADV_WILLNEED)
Eric Anholt856fa192009-03-19 14:10:50 -07001445 mark_page_accessed(obj_priv->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001446
1447 page_cache_release(obj_priv->pages[i]);
1448 }
Eric Anholt673a3942008-07-30 12:06:12 -07001449 obj_priv->dirty = 0;
1450
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07001451 drm_free_large(obj_priv->pages);
Eric Anholt856fa192009-03-19 14:10:50 -07001452 obj_priv->pages = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001453}
1454
1455static void
Zou Nan hai852835f2010-05-21 09:08:56 +08001456i915_gem_object_move_to_active(struct drm_gem_object *obj, uint32_t seqno,
1457 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001458{
1459 struct drm_device *dev = obj->dev;
1460 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001461 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Zou Nan hai852835f2010-05-21 09:08:56 +08001462 BUG_ON(ring == NULL);
1463 obj_priv->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001464
1465 /* Add a reference if we're newly entering the active list. */
1466 if (!obj_priv->active) {
1467 drm_gem_object_reference(obj);
1468 obj_priv->active = 1;
1469 }
1470 /* Move from whatever list we were on to the tail of execution. */
Carl Worth5e118f42009-03-20 11:54:25 -07001471 spin_lock(&dev_priv->mm.active_list_lock);
Zou Nan hai852835f2010-05-21 09:08:56 +08001472 list_move_tail(&obj_priv->list, &ring->active_list);
Carl Worth5e118f42009-03-20 11:54:25 -07001473 spin_unlock(&dev_priv->mm.active_list_lock);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001474 obj_priv->last_rendering_seqno = seqno;
Eric Anholt673a3942008-07-30 12:06:12 -07001475}
1476
Eric Anholtce44b0e2008-11-06 16:00:31 -08001477static void
1478i915_gem_object_move_to_flushing(struct drm_gem_object *obj)
1479{
1480 struct drm_device *dev = obj->dev;
1481 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001482 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001483
1484 BUG_ON(!obj_priv->active);
1485 list_move_tail(&obj_priv->list, &dev_priv->mm.flushing_list);
1486 obj_priv->last_rendering_seqno = 0;
1487}
Eric Anholt673a3942008-07-30 12:06:12 -07001488
Chris Wilson963b4832009-09-20 23:03:54 +01001489/* Immediately discard the backing storage */
1490static void
1491i915_gem_object_truncate(struct drm_gem_object *obj)
1492{
Daniel Vetter23010e42010-03-08 13:35:02 +01001493 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001494 struct inode *inode;
Chris Wilson963b4832009-09-20 23:03:54 +01001495
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001496 inode = obj->filp->f_path.dentry->d_inode;
1497 if (inode->i_op->truncate)
1498 inode->i_op->truncate (inode);
1499
1500 obj_priv->madv = __I915_MADV_PURGED;
Chris Wilson963b4832009-09-20 23:03:54 +01001501}
1502
1503static inline int
1504i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj_priv)
1505{
1506 return obj_priv->madv == I915_MADV_DONTNEED;
1507}
1508
Eric Anholt673a3942008-07-30 12:06:12 -07001509static void
1510i915_gem_object_move_to_inactive(struct drm_gem_object *obj)
1511{
1512 struct drm_device *dev = obj->dev;
1513 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001514 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001515
1516 i915_verify_inactive(dev, __FILE__, __LINE__);
1517 if (obj_priv->pin_count != 0)
1518 list_del_init(&obj_priv->list);
1519 else
1520 list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
1521
Daniel Vetter99fcb762010-02-07 16:20:18 +01001522 BUG_ON(!list_empty(&obj_priv->gpu_write_list));
1523
Eric Anholtce44b0e2008-11-06 16:00:31 -08001524 obj_priv->last_rendering_seqno = 0;
Zou Nan hai852835f2010-05-21 09:08:56 +08001525 obj_priv->ring = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001526 if (obj_priv->active) {
1527 obj_priv->active = 0;
1528 drm_gem_object_unreference(obj);
1529 }
1530 i915_verify_inactive(dev, __FILE__, __LINE__);
1531}
1532
Daniel Vetter63560392010-02-19 11:51:59 +01001533static void
1534i915_gem_process_flushing_list(struct drm_device *dev,
Zou Nan hai852835f2010-05-21 09:08:56 +08001535 uint32_t flush_domains, uint32_t seqno,
1536 struct intel_ring_buffer *ring)
Daniel Vetter63560392010-02-19 11:51:59 +01001537{
1538 drm_i915_private_t *dev_priv = dev->dev_private;
1539 struct drm_i915_gem_object *obj_priv, *next;
1540
1541 list_for_each_entry_safe(obj_priv, next,
1542 &dev_priv->mm.gpu_write_list,
1543 gpu_write_list) {
Daniel Vettera8089e82010-04-09 19:05:09 +00001544 struct drm_gem_object *obj = &obj_priv->base;
Daniel Vetter63560392010-02-19 11:51:59 +01001545
1546 if ((obj->write_domain & flush_domains) ==
Zou Nan hai852835f2010-05-21 09:08:56 +08001547 obj->write_domain &&
1548 obj_priv->ring->ring_flag == ring->ring_flag) {
Daniel Vetter63560392010-02-19 11:51:59 +01001549 uint32_t old_write_domain = obj->write_domain;
1550
1551 obj->write_domain = 0;
1552 list_del_init(&obj_priv->gpu_write_list);
Zou Nan hai852835f2010-05-21 09:08:56 +08001553 i915_gem_object_move_to_active(obj, seqno, ring);
Daniel Vetter63560392010-02-19 11:51:59 +01001554
1555 /* update the fence lru list */
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001556 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
1557 struct drm_i915_fence_reg *reg =
1558 &dev_priv->fence_regs[obj_priv->fence_reg];
1559 list_move_tail(&reg->lru_list,
Daniel Vetter63560392010-02-19 11:51:59 +01001560 &dev_priv->mm.fence_list);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001561 }
Daniel Vetter63560392010-02-19 11:51:59 +01001562
1563 trace_i915_gem_object_change_domain(obj,
1564 obj->read_domains,
1565 old_write_domain);
1566 }
1567 }
1568}
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001569
Daniel Vetter5a5a0c62009-09-15 22:57:36 +02001570uint32_t
Eric Anholtb9624422009-06-03 07:27:35 +00001571i915_add_request(struct drm_device *dev, struct drm_file *file_priv,
Zou Nan hai852835f2010-05-21 09:08:56 +08001572 uint32_t flush_domains, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001573{
1574 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtb9624422009-06-03 07:27:35 +00001575 struct drm_i915_file_private *i915_file_priv = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001576 struct drm_i915_gem_request *request;
1577 uint32_t seqno;
1578 int was_empty;
Eric Anholt673a3942008-07-30 12:06:12 -07001579
Eric Anholtb9624422009-06-03 07:27:35 +00001580 if (file_priv != NULL)
1581 i915_file_priv = file_priv->driver_priv;
1582
Eric Anholt9a298b22009-03-24 12:23:04 -07001583 request = kzalloc(sizeof(*request), GFP_KERNEL);
Eric Anholt673a3942008-07-30 12:06:12 -07001584 if (request == NULL)
1585 return 0;
1586
Zou Nan hai852835f2010-05-21 09:08:56 +08001587 seqno = ring->add_request(dev, ring, file_priv, flush_domains);
Eric Anholt673a3942008-07-30 12:06:12 -07001588
1589 request->seqno = seqno;
Zou Nan hai852835f2010-05-21 09:08:56 +08001590 request->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001591 request->emitted_jiffies = jiffies;
Zou Nan hai852835f2010-05-21 09:08:56 +08001592 was_empty = list_empty(&ring->request_list);
1593 list_add_tail(&request->list, &ring->request_list);
1594
Eric Anholtb9624422009-06-03 07:27:35 +00001595 if (i915_file_priv) {
1596 list_add_tail(&request->client_list,
1597 &i915_file_priv->mm.request_list);
1598 } else {
1599 INIT_LIST_HEAD(&request->client_list);
1600 }
Eric Anholt673a3942008-07-30 12:06:12 -07001601
Eric Anholtce44b0e2008-11-06 16:00:31 -08001602 /* Associate any objects on the flushing list matching the write
1603 * domain we're flushing with our flush.
1604 */
Daniel Vetter63560392010-02-19 11:51:59 +01001605 if (flush_domains != 0)
Zou Nan hai852835f2010-05-21 09:08:56 +08001606 i915_gem_process_flushing_list(dev, flush_domains, seqno, ring);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001607
Ben Gamarif65d9422009-09-14 17:48:44 -04001608 if (!dev_priv->mm.suspended) {
1609 mod_timer(&dev_priv->hangcheck_timer, jiffies + DRM_I915_HANGCHECK_PERIOD);
1610 if (was_empty)
1611 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
1612 }
Eric Anholt673a3942008-07-30 12:06:12 -07001613 return seqno;
1614}
1615
1616/**
1617 * Command execution barrier
1618 *
1619 * Ensures that all commands in the ring are finished
1620 * before signalling the CPU
1621 */
Eric Anholt3043c602008-10-02 12:24:47 -07001622static uint32_t
Zou Nan hai852835f2010-05-21 09:08:56 +08001623i915_retire_commands(struct drm_device *dev, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001624{
Eric Anholt673a3942008-07-30 12:06:12 -07001625 uint32_t flush_domains = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001626
1627 /* The sampler always gets flushed on i965 (sigh) */
1628 if (IS_I965G(dev))
1629 flush_domains |= I915_GEM_DOMAIN_SAMPLER;
Zou Nan hai852835f2010-05-21 09:08:56 +08001630
1631 ring->flush(dev, ring,
1632 I915_GEM_DOMAIN_COMMAND, flush_domains);
Eric Anholt673a3942008-07-30 12:06:12 -07001633 return flush_domains;
1634}
1635
1636/**
1637 * Moves buffers associated only with the given active seqno from the active
1638 * to inactive list, potentially freeing them.
1639 */
1640static void
1641i915_gem_retire_request(struct drm_device *dev,
1642 struct drm_i915_gem_request *request)
1643{
1644 drm_i915_private_t *dev_priv = dev->dev_private;
1645
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001646 trace_i915_gem_request_retire(dev, request->seqno);
1647
Eric Anholt673a3942008-07-30 12:06:12 -07001648 /* Move any buffers on the active list that are no longer referenced
1649 * by the ringbuffer to the flushing/inactive lists as appropriate.
1650 */
Carl Worth5e118f42009-03-20 11:54:25 -07001651 spin_lock(&dev_priv->mm.active_list_lock);
Zou Nan hai852835f2010-05-21 09:08:56 +08001652 while (!list_empty(&request->ring->active_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001653 struct drm_gem_object *obj;
1654 struct drm_i915_gem_object *obj_priv;
1655
Zou Nan hai852835f2010-05-21 09:08:56 +08001656 obj_priv = list_first_entry(&request->ring->active_list,
Eric Anholt673a3942008-07-30 12:06:12 -07001657 struct drm_i915_gem_object,
1658 list);
Daniel Vettera8089e82010-04-09 19:05:09 +00001659 obj = &obj_priv->base;
Eric Anholt673a3942008-07-30 12:06:12 -07001660
1661 /* If the seqno being retired doesn't match the oldest in the
1662 * list, then the oldest in the list must still be newer than
1663 * this seqno.
1664 */
1665 if (obj_priv->last_rendering_seqno != request->seqno)
Carl Worth5e118f42009-03-20 11:54:25 -07001666 goto out;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001667
Eric Anholt673a3942008-07-30 12:06:12 -07001668#if WATCH_LRU
1669 DRM_INFO("%s: retire %d moves to inactive list %p\n",
1670 __func__, request->seqno, obj);
1671#endif
1672
Eric Anholtce44b0e2008-11-06 16:00:31 -08001673 if (obj->write_domain != 0)
1674 i915_gem_object_move_to_flushing(obj);
Shaohua Li68c84342009-04-08 10:58:23 +08001675 else {
1676 /* Take a reference on the object so it won't be
1677 * freed while the spinlock is held. The list
1678 * protection for this spinlock is safe when breaking
1679 * the lock like this since the next thing we do
1680 * is just get the head of the list again.
1681 */
1682 drm_gem_object_reference(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001683 i915_gem_object_move_to_inactive(obj);
Shaohua Li68c84342009-04-08 10:58:23 +08001684 spin_unlock(&dev_priv->mm.active_list_lock);
1685 drm_gem_object_unreference(obj);
1686 spin_lock(&dev_priv->mm.active_list_lock);
1687 }
Eric Anholt673a3942008-07-30 12:06:12 -07001688 }
Carl Worth5e118f42009-03-20 11:54:25 -07001689out:
1690 spin_unlock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001691}
1692
1693/**
1694 * Returns true if seq1 is later than seq2.
1695 */
Ben Gamari22be1722009-09-14 17:48:43 -04001696bool
Eric Anholt673a3942008-07-30 12:06:12 -07001697i915_seqno_passed(uint32_t seq1, uint32_t seq2)
1698{
1699 return (int32_t)(seq1 - seq2) >= 0;
1700}
1701
1702uint32_t
Zou Nan hai852835f2010-05-21 09:08:56 +08001703i915_get_gem_seqno(struct drm_device *dev,
Zou Nan haid1b851f2010-05-21 09:08:57 +08001704 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001705{
Zou Nan hai852835f2010-05-21 09:08:56 +08001706 return ring->get_gem_seqno(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07001707}
1708
1709/**
1710 * This function clears the request list as sequence numbers are passed.
1711 */
1712void
Zou Nan hai852835f2010-05-21 09:08:56 +08001713i915_gem_retire_requests(struct drm_device *dev,
1714 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001715{
1716 drm_i915_private_t *dev_priv = dev->dev_private;
1717 uint32_t seqno;
1718
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001719 if (!ring->status_page.page_addr
Zou Nan hai852835f2010-05-21 09:08:56 +08001720 || list_empty(&ring->request_list))
Karsten Wiese6c0594a2009-02-23 15:07:57 +01001721 return;
1722
Zou Nan hai852835f2010-05-21 09:08:56 +08001723 seqno = i915_get_gem_seqno(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07001724
Zou Nan hai852835f2010-05-21 09:08:56 +08001725 while (!list_empty(&ring->request_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001726 struct drm_i915_gem_request *request;
1727 uint32_t retiring_seqno;
1728
Zou Nan hai852835f2010-05-21 09:08:56 +08001729 request = list_first_entry(&ring->request_list,
Eric Anholt673a3942008-07-30 12:06:12 -07001730 struct drm_i915_gem_request,
1731 list);
1732 retiring_seqno = request->seqno;
1733
1734 if (i915_seqno_passed(seqno, retiring_seqno) ||
Ben Gamariba1234d2009-09-14 17:48:47 -04001735 atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001736 i915_gem_retire_request(dev, request);
1737
1738 list_del(&request->list);
Eric Anholtb9624422009-06-03 07:27:35 +00001739 list_del(&request->client_list);
Eric Anholt9a298b22009-03-24 12:23:04 -07001740 kfree(request);
Eric Anholt673a3942008-07-30 12:06:12 -07001741 } else
1742 break;
1743 }
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001744
1745 if (unlikely (dev_priv->trace_irq_seqno &&
1746 i915_seqno_passed(dev_priv->trace_irq_seqno, seqno))) {
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001747
1748 ring->user_irq_put(dev, ring);
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001749 dev_priv->trace_irq_seqno = 0;
1750 }
Eric Anholt673a3942008-07-30 12:06:12 -07001751}
1752
1753void
1754i915_gem_retire_work_handler(struct work_struct *work)
1755{
1756 drm_i915_private_t *dev_priv;
1757 struct drm_device *dev;
1758
1759 dev_priv = container_of(work, drm_i915_private_t,
1760 mm.retire_work.work);
1761 dev = dev_priv->dev;
1762
1763 mutex_lock(&dev->struct_mutex);
Zou Nan hai852835f2010-05-21 09:08:56 +08001764 i915_gem_retire_requests(dev, &dev_priv->render_ring);
1765
Zou Nan haid1b851f2010-05-21 09:08:57 +08001766 if (HAS_BSD(dev))
1767 i915_gem_retire_requests(dev, &dev_priv->bsd_ring);
1768
Keith Packard6dbe2772008-10-14 21:41:13 -07001769 if (!dev_priv->mm.suspended &&
Zou Nan haid1b851f2010-05-21 09:08:57 +08001770 (!list_empty(&dev_priv->render_ring.request_list) ||
1771 (HAS_BSD(dev) &&
1772 !list_empty(&dev_priv->bsd_ring.request_list))))
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001773 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
Eric Anholt673a3942008-07-30 12:06:12 -07001774 mutex_unlock(&dev->struct_mutex);
1775}
1776
Daniel Vetter5a5a0c62009-09-15 22:57:36 +02001777int
Zou Nan hai852835f2010-05-21 09:08:56 +08001778i915_do_wait_request(struct drm_device *dev, uint32_t seqno,
1779 int interruptible, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001780{
1781 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001782 u32 ier;
Eric Anholt673a3942008-07-30 12:06:12 -07001783 int ret = 0;
1784
1785 BUG_ON(seqno == 0);
1786
Ben Gamariba1234d2009-09-14 17:48:47 -04001787 if (atomic_read(&dev_priv->mm.wedged))
Ben Gamariffed1d02009-09-14 17:48:41 -04001788 return -EIO;
1789
Zou Nan hai852835f2010-05-21 09:08:56 +08001790 if (!i915_seqno_passed(ring->get_gem_seqno(dev, ring), seqno)) {
Eric Anholtbad720f2009-10-22 16:11:14 -07001791 if (HAS_PCH_SPLIT(dev))
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001792 ier = I915_READ(DEIER) | I915_READ(GTIER);
1793 else
1794 ier = I915_READ(IER);
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001795 if (!ier) {
1796 DRM_ERROR("something (likely vbetool) disabled "
1797 "interrupts, re-enabling\n");
1798 i915_driver_irq_preinstall(dev);
1799 i915_driver_irq_postinstall(dev);
1800 }
1801
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001802 trace_i915_gem_request_wait_begin(dev, seqno);
1803
Zou Nan hai852835f2010-05-21 09:08:56 +08001804 ring->waiting_gem_seqno = seqno;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001805 ring->user_irq_get(dev, ring);
Daniel Vetter48764bf2009-09-15 22:57:32 +02001806 if (interruptible)
Zou Nan hai852835f2010-05-21 09:08:56 +08001807 ret = wait_event_interruptible(ring->irq_queue,
1808 i915_seqno_passed(
1809 ring->get_gem_seqno(dev, ring), seqno)
1810 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02001811 else
Zou Nan hai852835f2010-05-21 09:08:56 +08001812 wait_event(ring->irq_queue,
1813 i915_seqno_passed(
1814 ring->get_gem_seqno(dev, ring), seqno)
1815 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02001816
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001817 ring->user_irq_put(dev, ring);
Zou Nan hai852835f2010-05-21 09:08:56 +08001818 ring->waiting_gem_seqno = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001819
1820 trace_i915_gem_request_wait_end(dev, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07001821 }
Ben Gamariba1234d2009-09-14 17:48:47 -04001822 if (atomic_read(&dev_priv->mm.wedged))
Eric Anholt673a3942008-07-30 12:06:12 -07001823 ret = -EIO;
1824
1825 if (ret && ret != -ERESTARTSYS)
1826 DRM_ERROR("%s returns %d (awaiting %d at %d)\n",
Zou Nan hai852835f2010-05-21 09:08:56 +08001827 __func__, ret, seqno, ring->get_gem_seqno(dev, ring));
Eric Anholt673a3942008-07-30 12:06:12 -07001828
1829 /* Directly dispatch request retiring. While we have the work queue
1830 * to handle this, the waiter on a request often wants an associated
1831 * buffer to have made it to the inactive list, and we would need
1832 * a separate wait queue to handle that.
1833 */
1834 if (ret == 0)
Zou Nan hai852835f2010-05-21 09:08:56 +08001835 i915_gem_retire_requests(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07001836
1837 return ret;
1838}
1839
Daniel Vetter48764bf2009-09-15 22:57:32 +02001840/**
1841 * Waits for a sequence number to be signaled, and cleans up the
1842 * request and object lists appropriately for that event.
1843 */
1844static int
Zou Nan hai852835f2010-05-21 09:08:56 +08001845i915_wait_request(struct drm_device *dev, uint32_t seqno,
1846 struct intel_ring_buffer *ring)
Daniel Vetter48764bf2009-09-15 22:57:32 +02001847{
Zou Nan hai852835f2010-05-21 09:08:56 +08001848 return i915_do_wait_request(dev, seqno, 1, ring);
Daniel Vetter48764bf2009-09-15 22:57:32 +02001849}
1850
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001851static void
1852i915_gem_flush(struct drm_device *dev,
1853 uint32_t invalidate_domains,
1854 uint32_t flush_domains)
1855{
1856 drm_i915_private_t *dev_priv = dev->dev_private;
1857 if (flush_domains & I915_GEM_DOMAIN_CPU)
1858 drm_agp_chipset_flush(dev);
1859 dev_priv->render_ring.flush(dev, &dev_priv->render_ring,
1860 invalidate_domains,
1861 flush_domains);
Zou Nan haid1b851f2010-05-21 09:08:57 +08001862
1863 if (HAS_BSD(dev))
1864 dev_priv->bsd_ring.flush(dev, &dev_priv->bsd_ring,
1865 invalidate_domains,
1866 flush_domains);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001867}
1868
Zou Nan hai852835f2010-05-21 09:08:56 +08001869static void
1870i915_gem_flush_ring(struct drm_device *dev,
1871 uint32_t invalidate_domains,
1872 uint32_t flush_domains,
1873 struct intel_ring_buffer *ring)
1874{
1875 if (flush_domains & I915_GEM_DOMAIN_CPU)
1876 drm_agp_chipset_flush(dev);
1877 ring->flush(dev, ring,
1878 invalidate_domains,
1879 flush_domains);
1880}
1881
Eric Anholt673a3942008-07-30 12:06:12 -07001882/**
1883 * Ensures that all rendering to the object has completed and the object is
1884 * safe to unbind from the GTT or access from the CPU.
1885 */
1886static int
1887i915_gem_object_wait_rendering(struct drm_gem_object *obj)
1888{
1889 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001890 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001891 int ret;
1892
Eric Anholte47c68e2008-11-14 13:35:19 -08001893 /* This function only exists to support waiting for existing rendering,
1894 * not for emitting required flushes.
Eric Anholt673a3942008-07-30 12:06:12 -07001895 */
Eric Anholte47c68e2008-11-14 13:35:19 -08001896 BUG_ON((obj->write_domain & I915_GEM_GPU_DOMAINS) != 0);
Eric Anholt673a3942008-07-30 12:06:12 -07001897
1898 /* If there is rendering queued on the buffer being evicted, wait for
1899 * it.
1900 */
1901 if (obj_priv->active) {
1902#if WATCH_BUF
1903 DRM_INFO("%s: object %p wait for seqno %08x\n",
1904 __func__, obj, obj_priv->last_rendering_seqno);
1905#endif
Zou Nan hai852835f2010-05-21 09:08:56 +08001906 ret = i915_wait_request(dev,
1907 obj_priv->last_rendering_seqno, obj_priv->ring);
Eric Anholt673a3942008-07-30 12:06:12 -07001908 if (ret != 0)
1909 return ret;
1910 }
1911
1912 return 0;
1913}
1914
1915/**
1916 * Unbinds an object from the GTT aperture.
1917 */
Jesse Barnes0f973f22009-01-26 17:10:45 -08001918int
Eric Anholt673a3942008-07-30 12:06:12 -07001919i915_gem_object_unbind(struct drm_gem_object *obj)
1920{
1921 struct drm_device *dev = obj->dev;
Daniel Vetter4a87b8c2010-02-19 11:51:57 +01001922 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001923 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001924 int ret = 0;
1925
1926#if WATCH_BUF
1927 DRM_INFO("%s:%d %p\n", __func__, __LINE__, obj);
1928 DRM_INFO("gtt_space %p\n", obj_priv->gtt_space);
1929#endif
1930 if (obj_priv->gtt_space == NULL)
1931 return 0;
1932
1933 if (obj_priv->pin_count != 0) {
1934 DRM_ERROR("Attempting to unbind pinned buffer\n");
1935 return -EINVAL;
1936 }
1937
Eric Anholt5323fd02009-09-09 11:50:45 -07001938 /* blow away mappings if mapped through GTT */
1939 i915_gem_release_mmap(obj);
1940
Eric Anholt673a3942008-07-30 12:06:12 -07001941 /* Move the object to the CPU domain to ensure that
1942 * any possible CPU writes while it's not in the GTT
1943 * are flushed when we go to remap it. This will
1944 * also ensure that all pending GPU writes are finished
1945 * before we unbind.
1946 */
Eric Anholte47c68e2008-11-14 13:35:19 -08001947 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07001948 if (ret) {
Eric Anholte47c68e2008-11-14 13:35:19 -08001949 if (ret != -ERESTARTSYS)
1950 DRM_ERROR("set_domain failed: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07001951 return ret;
1952 }
1953
Eric Anholt5323fd02009-09-09 11:50:45 -07001954 BUG_ON(obj_priv->active);
1955
Daniel Vetter96b47b62009-12-15 17:50:00 +01001956 /* release the fence reg _after_ flushing */
1957 if (obj_priv->fence_reg != I915_FENCE_REG_NONE)
1958 i915_gem_clear_fence_reg(obj);
1959
Eric Anholt673a3942008-07-30 12:06:12 -07001960 if (obj_priv->agp_mem != NULL) {
1961 drm_unbind_agp(obj_priv->agp_mem);
1962 drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE);
1963 obj_priv->agp_mem = NULL;
1964 }
1965
Eric Anholt856fa192009-03-19 14:10:50 -07001966 i915_gem_object_put_pages(obj);
Chris Wilsona32808c2009-09-20 21:29:47 +01001967 BUG_ON(obj_priv->pages_refcount);
Eric Anholt673a3942008-07-30 12:06:12 -07001968
1969 if (obj_priv->gtt_space) {
1970 atomic_dec(&dev->gtt_count);
1971 atomic_sub(obj->size, &dev->gtt_memory);
1972
1973 drm_mm_put_block(obj_priv->gtt_space);
1974 obj_priv->gtt_space = NULL;
1975 }
1976
1977 /* Remove ourselves from the LRU list if present. */
Daniel Vetter4a87b8c2010-02-19 11:51:57 +01001978 spin_lock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001979 if (!list_empty(&obj_priv->list))
1980 list_del_init(&obj_priv->list);
Daniel Vetter4a87b8c2010-02-19 11:51:57 +01001981 spin_unlock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001982
Chris Wilson963b4832009-09-20 23:03:54 +01001983 if (i915_gem_object_is_purgeable(obj_priv))
1984 i915_gem_object_truncate(obj);
1985
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001986 trace_i915_gem_object_unbind(obj);
1987
Eric Anholt673a3942008-07-30 12:06:12 -07001988 return 0;
1989}
1990
Chris Wilson07f73f62009-09-14 16:50:30 +01001991static struct drm_gem_object *
1992i915_gem_find_inactive_object(struct drm_device *dev, int min_size)
1993{
1994 drm_i915_private_t *dev_priv = dev->dev_private;
1995 struct drm_i915_gem_object *obj_priv;
1996 struct drm_gem_object *best = NULL;
1997 struct drm_gem_object *first = NULL;
1998
1999 /* Try to find the smallest clean object */
2000 list_for_each_entry(obj_priv, &dev_priv->mm.inactive_list, list) {
Daniel Vettera8089e82010-04-09 19:05:09 +00002001 struct drm_gem_object *obj = &obj_priv->base;
Chris Wilson07f73f62009-09-14 16:50:30 +01002002 if (obj->size >= min_size) {
Chris Wilson963b4832009-09-20 23:03:54 +01002003 if ((!obj_priv->dirty ||
2004 i915_gem_object_is_purgeable(obj_priv)) &&
Chris Wilson07f73f62009-09-14 16:50:30 +01002005 (!best || obj->size < best->size)) {
2006 best = obj;
2007 if (best->size == min_size)
2008 return best;
2009 }
2010 if (!first)
2011 first = obj;
2012 }
2013 }
2014
2015 return best ? best : first;
2016}
2017
Eric Anholt673a3942008-07-30 12:06:12 -07002018static int
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002019i915_gpu_idle(struct drm_device *dev)
2020{
2021 drm_i915_private_t *dev_priv = dev->dev_private;
2022 bool lists_empty;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002023 uint32_t seqno1, seqno2;
Zou Nan hai852835f2010-05-21 09:08:56 +08002024 int ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002025
2026 spin_lock(&dev_priv->mm.active_list_lock);
Zou Nan haid1b851f2010-05-21 09:08:57 +08002027 lists_empty = (list_empty(&dev_priv->mm.flushing_list) &&
2028 list_empty(&dev_priv->render_ring.active_list) &&
2029 (!HAS_BSD(dev) ||
2030 list_empty(&dev_priv->bsd_ring.active_list)));
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002031 spin_unlock(&dev_priv->mm.active_list_lock);
2032
2033 if (lists_empty)
2034 return 0;
2035
2036 /* Flush everything onto the inactive list. */
2037 i915_gem_flush(dev, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
Zou Nan haid1b851f2010-05-21 09:08:57 +08002038 seqno1 = i915_add_request(dev, NULL, I915_GEM_GPU_DOMAINS,
Zou Nan hai852835f2010-05-21 09:08:56 +08002039 &dev_priv->render_ring);
Zou Nan haid1b851f2010-05-21 09:08:57 +08002040 if (seqno1 == 0)
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002041 return -ENOMEM;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002042 ret = i915_wait_request(dev, seqno1, &dev_priv->render_ring);
2043
2044 if (HAS_BSD(dev)) {
2045 seqno2 = i915_add_request(dev, NULL, I915_GEM_GPU_DOMAINS,
2046 &dev_priv->bsd_ring);
2047 if (seqno2 == 0)
2048 return -ENOMEM;
2049
2050 ret = i915_wait_request(dev, seqno2, &dev_priv->bsd_ring);
2051 if (ret)
2052 return ret;
2053 }
2054
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002055
Zou Nan hai852835f2010-05-21 09:08:56 +08002056 return ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002057}
2058
2059static int
Chris Wilson07f73f62009-09-14 16:50:30 +01002060i915_gem_evict_everything(struct drm_device *dev)
2061{
2062 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson07f73f62009-09-14 16:50:30 +01002063 int ret;
2064 bool lists_empty;
2065
Chris Wilson07f73f62009-09-14 16:50:30 +01002066 spin_lock(&dev_priv->mm.active_list_lock);
2067 lists_empty = (list_empty(&dev_priv->mm.inactive_list) &&
2068 list_empty(&dev_priv->mm.flushing_list) &&
Zou Nan haid1b851f2010-05-21 09:08:57 +08002069 list_empty(&dev_priv->render_ring.active_list) &&
2070 (!HAS_BSD(dev)
2071 || list_empty(&dev_priv->bsd_ring.active_list)));
Chris Wilson07f73f62009-09-14 16:50:30 +01002072 spin_unlock(&dev_priv->mm.active_list_lock);
2073
Chris Wilson97311292009-09-21 00:22:34 +01002074 if (lists_empty)
Chris Wilson07f73f62009-09-14 16:50:30 +01002075 return -ENOSPC;
Chris Wilson07f73f62009-09-14 16:50:30 +01002076
2077 /* Flush everything (on to the inactive lists) and evict */
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002078 ret = i915_gpu_idle(dev);
Chris Wilson07f73f62009-09-14 16:50:30 +01002079 if (ret)
2080 return ret;
2081
Daniel Vetter99fcb762010-02-07 16:20:18 +01002082 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
2083
Chris Wilsonab5ee572009-09-20 19:25:47 +01002084 ret = i915_gem_evict_from_inactive_list(dev);
Chris Wilson07f73f62009-09-14 16:50:30 +01002085 if (ret)
2086 return ret;
2087
2088 spin_lock(&dev_priv->mm.active_list_lock);
2089 lists_empty = (list_empty(&dev_priv->mm.inactive_list) &&
2090 list_empty(&dev_priv->mm.flushing_list) &&
Zou Nan haid1b851f2010-05-21 09:08:57 +08002091 list_empty(&dev_priv->render_ring.active_list) &&
2092 (!HAS_BSD(dev)
2093 || list_empty(&dev_priv->bsd_ring.active_list)));
Chris Wilson07f73f62009-09-14 16:50:30 +01002094 spin_unlock(&dev_priv->mm.active_list_lock);
2095 BUG_ON(!lists_empty);
2096
Eric Anholt673a3942008-07-30 12:06:12 -07002097 return 0;
2098}
2099
2100static int
Chris Wilson07f73f62009-09-14 16:50:30 +01002101i915_gem_evict_something(struct drm_device *dev, int min_size)
Eric Anholt673a3942008-07-30 12:06:12 -07002102{
2103 drm_i915_private_t *dev_priv = dev->dev_private;
2104 struct drm_gem_object *obj;
Chris Wilson07f73f62009-09-14 16:50:30 +01002105 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002106
Zou Nan hai852835f2010-05-21 09:08:56 +08002107 struct intel_ring_buffer *render_ring = &dev_priv->render_ring;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002108 struct intel_ring_buffer *bsd_ring = &dev_priv->bsd_ring;
Eric Anholt673a3942008-07-30 12:06:12 -07002109 for (;;) {
Zou Nan hai852835f2010-05-21 09:08:56 +08002110 i915_gem_retire_requests(dev, render_ring);
Chris Wilson07f73f62009-09-14 16:50:30 +01002111
Zou Nan haid1b851f2010-05-21 09:08:57 +08002112 if (HAS_BSD(dev))
2113 i915_gem_retire_requests(dev, bsd_ring);
2114
Eric Anholt673a3942008-07-30 12:06:12 -07002115 /* If there's an inactive buffer available now, grab it
2116 * and be done.
2117 */
Chris Wilson07f73f62009-09-14 16:50:30 +01002118 obj = i915_gem_find_inactive_object(dev, min_size);
2119 if (obj) {
2120 struct drm_i915_gem_object *obj_priv;
2121
Eric Anholt673a3942008-07-30 12:06:12 -07002122#if WATCH_LRU
2123 DRM_INFO("%s: evicting %p\n", __func__, obj);
2124#endif
Daniel Vetter23010e42010-03-08 13:35:02 +01002125 obj_priv = to_intel_bo(obj);
Chris Wilson07f73f62009-09-14 16:50:30 +01002126 BUG_ON(obj_priv->pin_count != 0);
Eric Anholt673a3942008-07-30 12:06:12 -07002127 BUG_ON(obj_priv->active);
2128
2129 /* Wait on the rendering and unbind the buffer. */
Chris Wilson07f73f62009-09-14 16:50:30 +01002130 return i915_gem_object_unbind(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002131 }
2132
2133 /* If we didn't get anything, but the ring is still processing
Chris Wilson07f73f62009-09-14 16:50:30 +01002134 * things, wait for the next to finish and hopefully leave us
2135 * a buffer to evict.
Eric Anholt673a3942008-07-30 12:06:12 -07002136 */
Zou Nan hai852835f2010-05-21 09:08:56 +08002137 if (!list_empty(&render_ring->request_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002138 struct drm_i915_gem_request *request;
2139
Zou Nan hai852835f2010-05-21 09:08:56 +08002140 request = list_first_entry(&render_ring->request_list,
Eric Anholt673a3942008-07-30 12:06:12 -07002141 struct drm_i915_gem_request,
2142 list);
2143
Zou Nan hai852835f2010-05-21 09:08:56 +08002144 ret = i915_wait_request(dev,
2145 request->seqno, request->ring);
Eric Anholt673a3942008-07-30 12:06:12 -07002146 if (ret)
Chris Wilson07f73f62009-09-14 16:50:30 +01002147 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002148
Chris Wilson07f73f62009-09-14 16:50:30 +01002149 continue;
Eric Anholt673a3942008-07-30 12:06:12 -07002150 }
2151
Zou Nan haid1b851f2010-05-21 09:08:57 +08002152 if (HAS_BSD(dev) && !list_empty(&bsd_ring->request_list)) {
2153 struct drm_i915_gem_request *request;
2154
2155 request = list_first_entry(&bsd_ring->request_list,
2156 struct drm_i915_gem_request,
2157 list);
2158
2159 ret = i915_wait_request(dev,
2160 request->seqno, request->ring);
2161 if (ret)
2162 return ret;
2163
2164 continue;
2165 }
2166
Eric Anholt673a3942008-07-30 12:06:12 -07002167 /* If we didn't have anything on the request list but there
2168 * are buffers awaiting a flush, emit one and try again.
2169 * When we wait on it, those buffers waiting for that flush
2170 * will get moved to inactive.
2171 */
2172 if (!list_empty(&dev_priv->mm.flushing_list)) {
Chris Wilson07f73f62009-09-14 16:50:30 +01002173 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07002174
Chris Wilson9a1e2582009-09-20 20:16:50 +01002175 /* Find an object that we can immediately reuse */
2176 list_for_each_entry(obj_priv, &dev_priv->mm.flushing_list, list) {
Daniel Vettera8089e82010-04-09 19:05:09 +00002177 obj = &obj_priv->base;
Chris Wilson9a1e2582009-09-20 20:16:50 +01002178 if (obj->size >= min_size)
2179 break;
Eric Anholt673a3942008-07-30 12:06:12 -07002180
Chris Wilson9a1e2582009-09-20 20:16:50 +01002181 obj = NULL;
2182 }
Eric Anholt673a3942008-07-30 12:06:12 -07002183
Chris Wilson9a1e2582009-09-20 20:16:50 +01002184 if (obj != NULL) {
2185 uint32_t seqno;
Chris Wilson07f73f62009-09-14 16:50:30 +01002186
Zou Nan hai852835f2010-05-21 09:08:56 +08002187 i915_gem_flush_ring(dev,
Chris Wilson9a1e2582009-09-20 20:16:50 +01002188 obj->write_domain,
Zou Nan hai852835f2010-05-21 09:08:56 +08002189 obj->write_domain,
2190 obj_priv->ring);
2191 seqno = i915_add_request(dev, NULL,
2192 obj->write_domain,
2193 obj_priv->ring);
Chris Wilson9a1e2582009-09-20 20:16:50 +01002194 if (seqno == 0)
2195 return -ENOMEM;
Chris Wilson9a1e2582009-09-20 20:16:50 +01002196 continue;
2197 }
Eric Anholt673a3942008-07-30 12:06:12 -07002198 }
2199
Chris Wilson07f73f62009-09-14 16:50:30 +01002200 /* If we didn't do any of the above, there's no single buffer
2201 * large enough to swap out for the new one, so just evict
2202 * everything and start again. (This should be rare.)
Eric Anholt673a3942008-07-30 12:06:12 -07002203 */
Chris Wilson97311292009-09-21 00:22:34 +01002204 if (!list_empty (&dev_priv->mm.inactive_list))
Chris Wilsonab5ee572009-09-20 19:25:47 +01002205 return i915_gem_evict_from_inactive_list(dev);
Chris Wilson97311292009-09-21 00:22:34 +01002206 else
Chris Wilson07f73f62009-09-14 16:50:30 +01002207 return i915_gem_evict_everything(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07002208 }
Keith Packardac94a962008-11-20 23:30:27 -08002209}
2210
Ben Gamari6911a9b2009-04-02 11:24:54 -07002211int
Chris Wilson4bdadb92010-01-27 13:36:32 +00002212i915_gem_object_get_pages(struct drm_gem_object *obj,
2213 gfp_t gfpmask)
Eric Anholt673a3942008-07-30 12:06:12 -07002214{
Daniel Vetter23010e42010-03-08 13:35:02 +01002215 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002216 int page_count, i;
2217 struct address_space *mapping;
2218 struct inode *inode;
2219 struct page *page;
Eric Anholt673a3942008-07-30 12:06:12 -07002220
Daniel Vetter778c3542010-05-13 11:49:44 +02002221 BUG_ON(obj_priv->pages_refcount
2222 == DRM_I915_GEM_OBJECT_MAX_PAGES_REFCOUNT);
2223
Eric Anholt856fa192009-03-19 14:10:50 -07002224 if (obj_priv->pages_refcount++ != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07002225 return 0;
2226
2227 /* Get the list of pages out of our struct file. They'll be pinned
2228 * at this point until we release them.
2229 */
2230 page_count = obj->size / PAGE_SIZE;
Eric Anholt856fa192009-03-19 14:10:50 -07002231 BUG_ON(obj_priv->pages != NULL);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07002232 obj_priv->pages = drm_calloc_large(page_count, sizeof(struct page *));
Eric Anholt856fa192009-03-19 14:10:50 -07002233 if (obj_priv->pages == NULL) {
Eric Anholt856fa192009-03-19 14:10:50 -07002234 obj_priv->pages_refcount--;
Eric Anholt673a3942008-07-30 12:06:12 -07002235 return -ENOMEM;
2236 }
2237
2238 inode = obj->filp->f_path.dentry->d_inode;
2239 mapping = inode->i_mapping;
2240 for (i = 0; i < page_count; i++) {
Chris Wilson4bdadb92010-01-27 13:36:32 +00002241 page = read_cache_page_gfp(mapping, i,
Linus Torvalds985b8232010-07-02 10:04:42 +10002242 GFP_HIGHUSER |
Chris Wilson4bdadb92010-01-27 13:36:32 +00002243 __GFP_COLD |
Linus Torvaldscd9f0402010-07-18 09:44:37 -07002244 __GFP_RECLAIMABLE |
Chris Wilson4bdadb92010-01-27 13:36:32 +00002245 gfpmask);
Chris Wilson1f2b1012010-03-12 19:52:55 +00002246 if (IS_ERR(page))
2247 goto err_pages;
2248
Eric Anholt856fa192009-03-19 14:10:50 -07002249 obj_priv->pages[i] = page;
Eric Anholt673a3942008-07-30 12:06:12 -07002250 }
Eric Anholt280b7132009-03-12 16:56:27 -07002251
2252 if (obj_priv->tiling_mode != I915_TILING_NONE)
2253 i915_gem_object_do_bit_17_swizzle(obj);
2254
Eric Anholt673a3942008-07-30 12:06:12 -07002255 return 0;
Chris Wilson1f2b1012010-03-12 19:52:55 +00002256
2257err_pages:
2258 while (i--)
2259 page_cache_release(obj_priv->pages[i]);
2260
2261 drm_free_large(obj_priv->pages);
2262 obj_priv->pages = NULL;
2263 obj_priv->pages_refcount--;
2264 return PTR_ERR(page);
Eric Anholt673a3942008-07-30 12:06:12 -07002265}
2266
Eric Anholt4e901fd2009-10-26 16:44:17 -07002267static void sandybridge_write_fence_reg(struct drm_i915_fence_reg *reg)
2268{
2269 struct drm_gem_object *obj = reg->obj;
2270 struct drm_device *dev = obj->dev;
2271 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002272 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt4e901fd2009-10-26 16:44:17 -07002273 int regnum = obj_priv->fence_reg;
2274 uint64_t val;
2275
2276 val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
2277 0xfffff000) << 32;
2278 val |= obj_priv->gtt_offset & 0xfffff000;
2279 val |= (uint64_t)((obj_priv->stride / 128) - 1) <<
2280 SANDYBRIDGE_FENCE_PITCH_SHIFT;
2281
2282 if (obj_priv->tiling_mode == I915_TILING_Y)
2283 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2284 val |= I965_FENCE_REG_VALID;
2285
2286 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (regnum * 8), val);
2287}
2288
Jesse Barnesde151cf2008-11-12 10:03:55 -08002289static void i965_write_fence_reg(struct drm_i915_fence_reg *reg)
2290{
2291 struct drm_gem_object *obj = reg->obj;
2292 struct drm_device *dev = obj->dev;
2293 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002294 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002295 int regnum = obj_priv->fence_reg;
2296 uint64_t val;
2297
2298 val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
2299 0xfffff000) << 32;
2300 val |= obj_priv->gtt_offset & 0xfffff000;
2301 val |= ((obj_priv->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
2302 if (obj_priv->tiling_mode == I915_TILING_Y)
2303 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2304 val |= I965_FENCE_REG_VALID;
2305
2306 I915_WRITE64(FENCE_REG_965_0 + (regnum * 8), val);
2307}
2308
2309static void i915_write_fence_reg(struct drm_i915_fence_reg *reg)
2310{
2311 struct drm_gem_object *obj = reg->obj;
2312 struct drm_device *dev = obj->dev;
2313 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002314 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002315 int regnum = obj_priv->fence_reg;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002316 int tile_width;
Eric Anholtdc529a42009-03-10 22:34:49 -07002317 uint32_t fence_reg, val;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002318 uint32_t pitch_val;
2319
2320 if ((obj_priv->gtt_offset & ~I915_FENCE_START_MASK) ||
2321 (obj_priv->gtt_offset & (obj->size - 1))) {
Linus Torvaldsf06da262009-02-09 08:57:29 -08002322 WARN(1, "%s: object 0x%08x not 1M or size (0x%zx) aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002323 __func__, obj_priv->gtt_offset, obj->size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002324 return;
2325 }
2326
Jesse Barnes0f973f22009-01-26 17:10:45 -08002327 if (obj_priv->tiling_mode == I915_TILING_Y &&
2328 HAS_128_BYTE_Y_TILING(dev))
2329 tile_width = 128;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002330 else
Jesse Barnes0f973f22009-01-26 17:10:45 -08002331 tile_width = 512;
2332
2333 /* Note: pitch better be a power of two tile widths */
2334 pitch_val = obj_priv->stride / tile_width;
2335 pitch_val = ffs(pitch_val) - 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002336
Daniel Vetterc36a2a62010-04-17 15:12:03 +02002337 if (obj_priv->tiling_mode == I915_TILING_Y &&
2338 HAS_128_BYTE_Y_TILING(dev))
2339 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2340 else
2341 WARN_ON(pitch_val > I915_FENCE_MAX_PITCH_VAL);
2342
Jesse Barnesde151cf2008-11-12 10:03:55 -08002343 val = obj_priv->gtt_offset;
2344 if (obj_priv->tiling_mode == I915_TILING_Y)
2345 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2346 val |= I915_FENCE_SIZE_BITS(obj->size);
2347 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2348 val |= I830_FENCE_REG_VALID;
2349
Eric Anholtdc529a42009-03-10 22:34:49 -07002350 if (regnum < 8)
2351 fence_reg = FENCE_REG_830_0 + (regnum * 4);
2352 else
2353 fence_reg = FENCE_REG_945_8 + ((regnum - 8) * 4);
2354 I915_WRITE(fence_reg, val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002355}
2356
2357static void i830_write_fence_reg(struct drm_i915_fence_reg *reg)
2358{
2359 struct drm_gem_object *obj = reg->obj;
2360 struct drm_device *dev = obj->dev;
2361 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002362 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002363 int regnum = obj_priv->fence_reg;
2364 uint32_t val;
2365 uint32_t pitch_val;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002366 uint32_t fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002367
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002368 if ((obj_priv->gtt_offset & ~I830_FENCE_START_MASK) ||
Jesse Barnesde151cf2008-11-12 10:03:55 -08002369 (obj_priv->gtt_offset & (obj->size - 1))) {
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002370 WARN(1, "%s: object 0x%08x not 512K or size aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002371 __func__, obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002372 return;
2373 }
2374
Eric Anholte76a16d2009-05-26 17:44:56 -07002375 pitch_val = obj_priv->stride / 128;
2376 pitch_val = ffs(pitch_val) - 1;
2377 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2378
Jesse Barnesde151cf2008-11-12 10:03:55 -08002379 val = obj_priv->gtt_offset;
2380 if (obj_priv->tiling_mode == I915_TILING_Y)
2381 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002382 fence_size_bits = I830_FENCE_SIZE_BITS(obj->size);
2383 WARN_ON(fence_size_bits & ~0x00000f00);
2384 val |= fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002385 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2386 val |= I830_FENCE_REG_VALID;
2387
2388 I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002389}
2390
Daniel Vetterae3db242010-02-19 11:51:58 +01002391static int i915_find_fence_reg(struct drm_device *dev)
2392{
2393 struct drm_i915_fence_reg *reg = NULL;
2394 struct drm_i915_gem_object *obj_priv = NULL;
2395 struct drm_i915_private *dev_priv = dev->dev_private;
2396 struct drm_gem_object *obj = NULL;
2397 int i, avail, ret;
2398
2399 /* First try to find a free reg */
2400 avail = 0;
2401 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2402 reg = &dev_priv->fence_regs[i];
2403 if (!reg->obj)
2404 return i;
2405
Daniel Vetter23010e42010-03-08 13:35:02 +01002406 obj_priv = to_intel_bo(reg->obj);
Daniel Vetterae3db242010-02-19 11:51:58 +01002407 if (!obj_priv->pin_count)
2408 avail++;
2409 }
2410
2411 if (avail == 0)
2412 return -ENOSPC;
2413
2414 /* None available, try to steal one or wait for a user to finish */
2415 i = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002416 list_for_each_entry(reg, &dev_priv->mm.fence_list,
2417 lru_list) {
2418 obj = reg->obj;
2419 obj_priv = to_intel_bo(obj);
Daniel Vetterae3db242010-02-19 11:51:58 +01002420
2421 if (obj_priv->pin_count)
2422 continue;
2423
2424 /* found one! */
2425 i = obj_priv->fence_reg;
2426 break;
2427 }
2428
2429 BUG_ON(i == I915_FENCE_REG_NONE);
2430
2431 /* We only have a reference on obj from the active list. put_fence_reg
2432 * might drop that one, causing a use-after-free in it. So hold a
2433 * private reference to obj like the other callers of put_fence_reg
2434 * (set_tiling ioctl) do. */
2435 drm_gem_object_reference(obj);
2436 ret = i915_gem_object_put_fence_reg(obj);
2437 drm_gem_object_unreference(obj);
2438 if (ret != 0)
2439 return ret;
2440
2441 return i;
2442}
2443
Jesse Barnesde151cf2008-11-12 10:03:55 -08002444/**
2445 * i915_gem_object_get_fence_reg - set up a fence reg for an object
2446 * @obj: object to map through a fence reg
2447 *
2448 * When mapping objects through the GTT, userspace wants to be able to write
2449 * to them without having to worry about swizzling if the object is tiled.
2450 *
2451 * This function walks the fence regs looking for a free one for @obj,
2452 * stealing one if it can't find any.
2453 *
2454 * It then sets up the reg based on the object's properties: address, pitch
2455 * and tiling format.
2456 */
Chris Wilson8c4b8c32009-06-17 22:08:52 +01002457int
2458i915_gem_object_get_fence_reg(struct drm_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002459{
2460 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002461 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002462 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002463 struct drm_i915_fence_reg *reg = NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002464 int ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002465
Eric Anholta09ba7f2009-08-29 12:49:51 -07002466 /* Just update our place in the LRU if our fence is getting used. */
2467 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002468 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
2469 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002470 return 0;
2471 }
2472
Jesse Barnesde151cf2008-11-12 10:03:55 -08002473 switch (obj_priv->tiling_mode) {
2474 case I915_TILING_NONE:
2475 WARN(1, "allocating a fence for non-tiled object?\n");
2476 break;
2477 case I915_TILING_X:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002478 if (!obj_priv->stride)
2479 return -EINVAL;
2480 WARN((obj_priv->stride & (512 - 1)),
2481 "object 0x%08x is X tiled but has non-512B pitch\n",
2482 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002483 break;
2484 case I915_TILING_Y:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002485 if (!obj_priv->stride)
2486 return -EINVAL;
2487 WARN((obj_priv->stride & (128 - 1)),
2488 "object 0x%08x is Y tiled but has non-128B pitch\n",
2489 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002490 break;
2491 }
2492
Daniel Vetterae3db242010-02-19 11:51:58 +01002493 ret = i915_find_fence_reg(dev);
2494 if (ret < 0)
2495 return ret;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002496
Daniel Vetterae3db242010-02-19 11:51:58 +01002497 obj_priv->fence_reg = ret;
2498 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002499 list_add_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002500
Jesse Barnesde151cf2008-11-12 10:03:55 -08002501 reg->obj = obj;
2502
Eric Anholt4e901fd2009-10-26 16:44:17 -07002503 if (IS_GEN6(dev))
2504 sandybridge_write_fence_reg(reg);
2505 else if (IS_I965G(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08002506 i965_write_fence_reg(reg);
2507 else if (IS_I9XX(dev))
2508 i915_write_fence_reg(reg);
2509 else
2510 i830_write_fence_reg(reg);
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002511
Daniel Vetterae3db242010-02-19 11:51:58 +01002512 trace_i915_gem_object_get_fence(obj, obj_priv->fence_reg,
2513 obj_priv->tiling_mode);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002514
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002515 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002516}
2517
2518/**
2519 * i915_gem_clear_fence_reg - clear out fence register info
2520 * @obj: object to clear
2521 *
2522 * Zeroes out the fence register itself and clears out the associated
2523 * data structures in dev_priv and obj_priv.
2524 */
2525static void
2526i915_gem_clear_fence_reg(struct drm_gem_object *obj)
2527{
2528 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002529 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002530 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002531 struct drm_i915_fence_reg *reg =
2532 &dev_priv->fence_regs[obj_priv->fence_reg];
Jesse Barnesde151cf2008-11-12 10:03:55 -08002533
Eric Anholt4e901fd2009-10-26 16:44:17 -07002534 if (IS_GEN6(dev)) {
2535 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 +
2536 (obj_priv->fence_reg * 8), 0);
2537 } else if (IS_I965G(dev)) {
Jesse Barnesde151cf2008-11-12 10:03:55 -08002538 I915_WRITE64(FENCE_REG_965_0 + (obj_priv->fence_reg * 8), 0);
Eric Anholt4e901fd2009-10-26 16:44:17 -07002539 } else {
Eric Anholtdc529a42009-03-10 22:34:49 -07002540 uint32_t fence_reg;
2541
2542 if (obj_priv->fence_reg < 8)
2543 fence_reg = FENCE_REG_830_0 + obj_priv->fence_reg * 4;
2544 else
2545 fence_reg = FENCE_REG_945_8 + (obj_priv->fence_reg -
2546 8) * 4;
2547
2548 I915_WRITE(fence_reg, 0);
2549 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002550
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002551 reg->obj = NULL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002552 obj_priv->fence_reg = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002553 list_del_init(&reg->lru_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002554}
2555
Eric Anholt673a3942008-07-30 12:06:12 -07002556/**
Chris Wilson52dc7d32009-06-06 09:46:01 +01002557 * i915_gem_object_put_fence_reg - waits on outstanding fenced access
2558 * to the buffer to finish, and then resets the fence register.
2559 * @obj: tiled object holding a fence register.
2560 *
2561 * Zeroes out the fence register itself and clears out the associated
2562 * data structures in dev_priv and obj_priv.
2563 */
2564int
2565i915_gem_object_put_fence_reg(struct drm_gem_object *obj)
2566{
2567 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01002568 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson52dc7d32009-06-06 09:46:01 +01002569
2570 if (obj_priv->fence_reg == I915_FENCE_REG_NONE)
2571 return 0;
2572
Daniel Vetter10ae9bd2010-02-01 13:59:17 +01002573 /* If we've changed tiling, GTT-mappings of the object
2574 * need to re-fault to ensure that the correct fence register
2575 * setup is in place.
2576 */
2577 i915_gem_release_mmap(obj);
2578
Chris Wilson52dc7d32009-06-06 09:46:01 +01002579 /* On the i915, GPU access to tiled buffers is via a fence,
2580 * therefore we must wait for any outstanding access to complete
2581 * before clearing the fence.
2582 */
2583 if (!IS_I965G(dev)) {
2584 int ret;
2585
2586 i915_gem_object_flush_gpu_write_domain(obj);
Chris Wilson52dc7d32009-06-06 09:46:01 +01002587 ret = i915_gem_object_wait_rendering(obj);
2588 if (ret != 0)
2589 return ret;
2590 }
2591
Daniel Vetter4a726612010-02-01 13:59:16 +01002592 i915_gem_object_flush_gtt_write_domain(obj);
Chris Wilson52dc7d32009-06-06 09:46:01 +01002593 i915_gem_clear_fence_reg (obj);
2594
2595 return 0;
2596}
2597
2598/**
Eric Anholt673a3942008-07-30 12:06:12 -07002599 * Finds free space in the GTT aperture and binds the object there.
2600 */
2601static int
2602i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment)
2603{
2604 struct drm_device *dev = obj->dev;
2605 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002606 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002607 struct drm_mm_node *free_space;
Chris Wilson4bdadb92010-01-27 13:36:32 +00002608 gfp_t gfpmask = __GFP_NORETRY | __GFP_NOWARN;
Chris Wilson07f73f62009-09-14 16:50:30 +01002609 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002610
Chris Wilsonbb6baf72009-09-22 14:24:13 +01002611 if (obj_priv->madv != I915_MADV_WILLNEED) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01002612 DRM_ERROR("Attempting to bind a purgeable object\n");
2613 return -EINVAL;
2614 }
2615
Eric Anholt673a3942008-07-30 12:06:12 -07002616 if (alignment == 0)
Jesse Barnes0f973f22009-01-26 17:10:45 -08002617 alignment = i915_gem_get_gtt_alignment(obj);
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002618 if (alignment & (i915_gem_get_gtt_alignment(obj) - 1)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002619 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2620 return -EINVAL;
2621 }
2622
Chris Wilson654fc602010-05-27 13:18:21 +01002623 /* If the object is bigger than the entire aperture, reject it early
2624 * before evicting everything in a vain attempt to find space.
2625 */
2626 if (obj->size > dev->gtt_total) {
2627 DRM_ERROR("Attempting to bind an object larger than the aperture\n");
2628 return -E2BIG;
2629 }
2630
Eric Anholt673a3942008-07-30 12:06:12 -07002631 search_free:
2632 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
2633 obj->size, alignment, 0);
2634 if (free_space != NULL) {
2635 obj_priv->gtt_space = drm_mm_get_block(free_space, obj->size,
2636 alignment);
Daniel Vetterdb3307a2010-07-02 15:02:12 +01002637 if (obj_priv->gtt_space != NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002638 obj_priv->gtt_offset = obj_priv->gtt_space->start;
Eric Anholt673a3942008-07-30 12:06:12 -07002639 }
2640 if (obj_priv->gtt_space == NULL) {
2641 /* If the gtt is empty and we're still having trouble
2642 * fitting our object in, we're out of memory.
2643 */
2644#if WATCH_LRU
2645 DRM_INFO("%s: GTT full, evicting something\n", __func__);
2646#endif
Chris Wilson07f73f62009-09-14 16:50:30 +01002647 ret = i915_gem_evict_something(dev, obj->size);
Chris Wilson97311292009-09-21 00:22:34 +01002648 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002649 return ret;
Chris Wilson97311292009-09-21 00:22:34 +01002650
Eric Anholt673a3942008-07-30 12:06:12 -07002651 goto search_free;
2652 }
2653
2654#if WATCH_BUF
Krzysztof Halasacfd43c02009-06-20 00:31:28 +02002655 DRM_INFO("Binding object of size %zd at 0x%08x\n",
Eric Anholt673a3942008-07-30 12:06:12 -07002656 obj->size, obj_priv->gtt_offset);
2657#endif
Chris Wilson4bdadb92010-01-27 13:36:32 +00002658 ret = i915_gem_object_get_pages(obj, gfpmask);
Eric Anholt673a3942008-07-30 12:06:12 -07002659 if (ret) {
2660 drm_mm_put_block(obj_priv->gtt_space);
2661 obj_priv->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002662
2663 if (ret == -ENOMEM) {
2664 /* first try to clear up some space from the GTT */
2665 ret = i915_gem_evict_something(dev, obj->size);
2666 if (ret) {
Chris Wilson07f73f62009-09-14 16:50:30 +01002667 /* now try to shrink everyone else */
Chris Wilson4bdadb92010-01-27 13:36:32 +00002668 if (gfpmask) {
2669 gfpmask = 0;
2670 goto search_free;
Chris Wilson07f73f62009-09-14 16:50:30 +01002671 }
2672
2673 return ret;
2674 }
2675
2676 goto search_free;
2677 }
2678
Eric Anholt673a3942008-07-30 12:06:12 -07002679 return ret;
2680 }
2681
Eric Anholt673a3942008-07-30 12:06:12 -07002682 /* Create an AGP memory structure pointing at our pages, and bind it
2683 * into the GTT.
2684 */
2685 obj_priv->agp_mem = drm_agp_bind_pages(dev,
Eric Anholt856fa192009-03-19 14:10:50 -07002686 obj_priv->pages,
Chris Wilson07f73f62009-09-14 16:50:30 +01002687 obj->size >> PAGE_SHIFT,
Keith Packardba1eb1d2008-10-14 19:55:10 -07002688 obj_priv->gtt_offset,
2689 obj_priv->agp_type);
Eric Anholt673a3942008-07-30 12:06:12 -07002690 if (obj_priv->agp_mem == NULL) {
Eric Anholt856fa192009-03-19 14:10:50 -07002691 i915_gem_object_put_pages(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002692 drm_mm_put_block(obj_priv->gtt_space);
2693 obj_priv->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002694
2695 ret = i915_gem_evict_something(dev, obj->size);
Chris Wilson97311292009-09-21 00:22:34 +01002696 if (ret)
Chris Wilson07f73f62009-09-14 16:50:30 +01002697 return ret;
Chris Wilson07f73f62009-09-14 16:50:30 +01002698
2699 goto search_free;
Eric Anholt673a3942008-07-30 12:06:12 -07002700 }
2701 atomic_inc(&dev->gtt_count);
2702 atomic_add(obj->size, &dev->gtt_memory);
2703
2704 /* Assert that the object is not currently in any GPU domain. As it
2705 * wasn't in the GTT, there shouldn't be any way it could have been in
2706 * a GPU cache
2707 */
Chris Wilson21d509e2009-06-06 09:46:02 +01002708 BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS);
2709 BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS);
Eric Anholt673a3942008-07-30 12:06:12 -07002710
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002711 trace_i915_gem_object_bind(obj, obj_priv->gtt_offset);
2712
Eric Anholt673a3942008-07-30 12:06:12 -07002713 return 0;
2714}
2715
2716void
2717i915_gem_clflush_object(struct drm_gem_object *obj)
2718{
Daniel Vetter23010e42010-03-08 13:35:02 +01002719 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002720
2721 /* If we don't have a page list set up, then we're not pinned
2722 * to GPU, and we can ignore the cache flush because it'll happen
2723 * again at bind time.
2724 */
Eric Anholt856fa192009-03-19 14:10:50 -07002725 if (obj_priv->pages == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002726 return;
2727
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002728 trace_i915_gem_object_clflush(obj);
Eric Anholtcfa16a02009-05-26 18:46:16 -07002729
Eric Anholt856fa192009-03-19 14:10:50 -07002730 drm_clflush_pages(obj_priv->pages, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002731}
2732
Eric Anholte47c68e2008-11-14 13:35:19 -08002733/** Flushes any GPU write domain for the object if it's dirty. */
2734static void
2735i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj)
2736{
2737 struct drm_device *dev = obj->dev;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002738 uint32_t old_write_domain;
Zou Nan hai852835f2010-05-21 09:08:56 +08002739 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08002740
2741 if ((obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
2742 return;
2743
2744 /* Queue the GPU write cache flushing we need. */
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002745 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002746 i915_gem_flush(dev, 0, obj->write_domain);
Zou Nan hai852835f2010-05-21 09:08:56 +08002747 (void) i915_add_request(dev, NULL, obj->write_domain, obj_priv->ring);
Daniel Vetter99fcb762010-02-07 16:20:18 +01002748 BUG_ON(obj->write_domain);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002749
2750 trace_i915_gem_object_change_domain(obj,
2751 obj->read_domains,
2752 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002753}
2754
2755/** Flushes the GTT write domain for the object if it's dirty. */
2756static void
2757i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj)
2758{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002759 uint32_t old_write_domain;
2760
Eric Anholte47c68e2008-11-14 13:35:19 -08002761 if (obj->write_domain != I915_GEM_DOMAIN_GTT)
2762 return;
2763
2764 /* No actual flushing is required for the GTT write domain. Writes
2765 * to it immediately go to main memory as far as we know, so there's
2766 * no chipset flush. It also doesn't land in render cache.
2767 */
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002768 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002769 obj->write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002770
2771 trace_i915_gem_object_change_domain(obj,
2772 obj->read_domains,
2773 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002774}
2775
2776/** Flushes the CPU write domain for the object if it's dirty. */
2777static void
2778i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj)
2779{
2780 struct drm_device *dev = obj->dev;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002781 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002782
2783 if (obj->write_domain != I915_GEM_DOMAIN_CPU)
2784 return;
2785
2786 i915_gem_clflush_object(obj);
2787 drm_agp_chipset_flush(dev);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002788 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002789 obj->write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002790
2791 trace_i915_gem_object_change_domain(obj,
2792 obj->read_domains,
2793 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002794}
2795
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002796void
2797i915_gem_object_flush_write_domain(struct drm_gem_object *obj)
2798{
2799 switch (obj->write_domain) {
2800 case I915_GEM_DOMAIN_GTT:
2801 i915_gem_object_flush_gtt_write_domain(obj);
2802 break;
2803 case I915_GEM_DOMAIN_CPU:
2804 i915_gem_object_flush_cpu_write_domain(obj);
2805 break;
2806 default:
2807 i915_gem_object_flush_gpu_write_domain(obj);
2808 break;
2809 }
2810}
2811
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002812/**
2813 * Moves a single object to the GTT read, and possibly write domain.
2814 *
2815 * This function returns when the move is complete, including waiting on
2816 * flushes to occur.
2817 */
Jesse Barnes79e53942008-11-07 14:24:08 -08002818int
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002819i915_gem_object_set_to_gtt_domain(struct drm_gem_object *obj, int write)
2820{
Daniel Vetter23010e42010-03-08 13:35:02 +01002821 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002822 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002823 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002824
Eric Anholt02354392008-11-26 13:58:13 -08002825 /* Not valid to be called on unbound objects. */
2826 if (obj_priv->gtt_space == NULL)
2827 return -EINVAL;
2828
Eric Anholte47c68e2008-11-14 13:35:19 -08002829 i915_gem_object_flush_gpu_write_domain(obj);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002830 /* Wait on any GPU rendering and flushing to occur. */
Eric Anholte47c68e2008-11-14 13:35:19 -08002831 ret = i915_gem_object_wait_rendering(obj);
2832 if (ret != 0)
2833 return ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002834
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002835 old_write_domain = obj->write_domain;
2836 old_read_domains = obj->read_domains;
2837
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002838 /* If we're writing through the GTT domain, then CPU and GPU caches
2839 * will need to be invalidated at next use.
2840 */
2841 if (write)
Eric Anholte47c68e2008-11-14 13:35:19 -08002842 obj->read_domains &= I915_GEM_DOMAIN_GTT;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002843
Eric Anholte47c68e2008-11-14 13:35:19 -08002844 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002845
2846 /* It should now be out of any other write domains, and we can update
2847 * the domain values for our changes.
2848 */
2849 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2850 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002851 if (write) {
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002852 obj->write_domain = I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002853 obj_priv->dirty = 1;
2854 }
2855
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002856 trace_i915_gem_object_change_domain(obj,
2857 old_read_domains,
2858 old_write_domain);
2859
Eric Anholte47c68e2008-11-14 13:35:19 -08002860 return 0;
2861}
2862
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002863/*
2864 * Prepare buffer for display plane. Use uninterruptible for possible flush
2865 * wait, as in modesetting process we're not supposed to be interrupted.
2866 */
2867int
2868i915_gem_object_set_to_display_plane(struct drm_gem_object *obj)
2869{
2870 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01002871 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002872 uint32_t old_write_domain, old_read_domains;
2873 int ret;
2874
2875 /* Not valid to be called on unbound objects. */
2876 if (obj_priv->gtt_space == NULL)
2877 return -EINVAL;
2878
2879 i915_gem_object_flush_gpu_write_domain(obj);
2880
2881 /* Wait on any GPU rendering and flushing to occur. */
2882 if (obj_priv->active) {
2883#if WATCH_BUF
2884 DRM_INFO("%s: object %p wait for seqno %08x\n",
2885 __func__, obj, obj_priv->last_rendering_seqno);
2886#endif
Zou Nan hai852835f2010-05-21 09:08:56 +08002887 ret = i915_do_wait_request(dev,
2888 obj_priv->last_rendering_seqno,
2889 0,
2890 obj_priv->ring);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002891 if (ret != 0)
2892 return ret;
2893 }
2894
Chris Wilsonb118c1e2010-05-27 13:18:14 +01002895 i915_gem_object_flush_cpu_write_domain(obj);
2896
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002897 old_write_domain = obj->write_domain;
2898 old_read_domains = obj->read_domains;
2899
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002900 /* It should now be out of any other write domains, and we can update
2901 * the domain values for our changes.
2902 */
2903 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
Chris Wilsonb118c1e2010-05-27 13:18:14 +01002904 obj->read_domains = I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002905 obj->write_domain = I915_GEM_DOMAIN_GTT;
2906 obj_priv->dirty = 1;
2907
2908 trace_i915_gem_object_change_domain(obj,
2909 old_read_domains,
2910 old_write_domain);
2911
2912 return 0;
2913}
2914
Eric Anholte47c68e2008-11-14 13:35:19 -08002915/**
2916 * Moves a single object to the CPU read, and possibly write domain.
2917 *
2918 * This function returns when the move is complete, including waiting on
2919 * flushes to occur.
2920 */
2921static int
2922i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj, int write)
2923{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002924 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002925 int ret;
2926
2927 i915_gem_object_flush_gpu_write_domain(obj);
2928 /* Wait on any GPU rendering and flushing to occur. */
2929 ret = i915_gem_object_wait_rendering(obj);
2930 if (ret != 0)
2931 return ret;
2932
2933 i915_gem_object_flush_gtt_write_domain(obj);
2934
2935 /* If we have a partially-valid cache of the object in the CPU,
2936 * finish invalidating it and free the per-page flags.
2937 */
2938 i915_gem_object_set_to_full_cpu_read_domain(obj);
2939
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002940 old_write_domain = obj->write_domain;
2941 old_read_domains = obj->read_domains;
2942
Eric Anholte47c68e2008-11-14 13:35:19 -08002943 /* Flush the CPU cache if it's still invalid. */
2944 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
2945 i915_gem_clflush_object(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08002946
2947 obj->read_domains |= I915_GEM_DOMAIN_CPU;
2948 }
2949
2950 /* It should now be out of any other write domains, and we can update
2951 * the domain values for our changes.
2952 */
2953 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
2954
2955 /* If we're writing through the CPU, then the GPU read domains will
2956 * need to be invalidated at next use.
2957 */
2958 if (write) {
2959 obj->read_domains &= I915_GEM_DOMAIN_CPU;
2960 obj->write_domain = I915_GEM_DOMAIN_CPU;
2961 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002962
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002963 trace_i915_gem_object_change_domain(obj,
2964 old_read_domains,
2965 old_write_domain);
2966
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002967 return 0;
2968}
2969
Eric Anholt673a3942008-07-30 12:06:12 -07002970/*
2971 * Set the next domain for the specified object. This
2972 * may not actually perform the necessary flushing/invaliding though,
2973 * as that may want to be batched with other set_domain operations
2974 *
2975 * This is (we hope) the only really tricky part of gem. The goal
2976 * is fairly simple -- track which caches hold bits of the object
2977 * and make sure they remain coherent. A few concrete examples may
2978 * help to explain how it works. For shorthand, we use the notation
2979 * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
2980 * a pair of read and write domain masks.
2981 *
2982 * Case 1: the batch buffer
2983 *
2984 * 1. Allocated
2985 * 2. Written by CPU
2986 * 3. Mapped to GTT
2987 * 4. Read by GPU
2988 * 5. Unmapped from GTT
2989 * 6. Freed
2990 *
2991 * Let's take these a step at a time
2992 *
2993 * 1. Allocated
2994 * Pages allocated from the kernel may still have
2995 * cache contents, so we set them to (CPU, CPU) always.
2996 * 2. Written by CPU (using pwrite)
2997 * The pwrite function calls set_domain (CPU, CPU) and
2998 * this function does nothing (as nothing changes)
2999 * 3. Mapped by GTT
3000 * This function asserts that the object is not
3001 * currently in any GPU-based read or write domains
3002 * 4. Read by GPU
3003 * i915_gem_execbuffer calls set_domain (COMMAND, 0).
3004 * As write_domain is zero, this function adds in the
3005 * current read domains (CPU+COMMAND, 0).
3006 * flush_domains is set to CPU.
3007 * invalidate_domains is set to COMMAND
3008 * clflush is run to get data out of the CPU caches
3009 * then i915_dev_set_domain calls i915_gem_flush to
3010 * emit an MI_FLUSH and drm_agp_chipset_flush
3011 * 5. Unmapped from GTT
3012 * i915_gem_object_unbind calls set_domain (CPU, CPU)
3013 * flush_domains and invalidate_domains end up both zero
3014 * so no flushing/invalidating happens
3015 * 6. Freed
3016 * yay, done
3017 *
3018 * Case 2: The shared render buffer
3019 *
3020 * 1. Allocated
3021 * 2. Mapped to GTT
3022 * 3. Read/written by GPU
3023 * 4. set_domain to (CPU,CPU)
3024 * 5. Read/written by CPU
3025 * 6. Read/written by GPU
3026 *
3027 * 1. Allocated
3028 * Same as last example, (CPU, CPU)
3029 * 2. Mapped to GTT
3030 * Nothing changes (assertions find that it is not in the GPU)
3031 * 3. Read/written by GPU
3032 * execbuffer calls set_domain (RENDER, RENDER)
3033 * flush_domains gets CPU
3034 * invalidate_domains gets GPU
3035 * clflush (obj)
3036 * MI_FLUSH and drm_agp_chipset_flush
3037 * 4. set_domain (CPU, CPU)
3038 * flush_domains gets GPU
3039 * invalidate_domains gets CPU
3040 * wait_rendering (obj) to make sure all drawing is complete.
3041 * This will include an MI_FLUSH to get the data from GPU
3042 * to memory
3043 * clflush (obj) to invalidate the CPU cache
3044 * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
3045 * 5. Read/written by CPU
3046 * cache lines are loaded and dirtied
3047 * 6. Read written by GPU
3048 * Same as last GPU access
3049 *
3050 * Case 3: The constant buffer
3051 *
3052 * 1. Allocated
3053 * 2. Written by CPU
3054 * 3. Read by GPU
3055 * 4. Updated (written) by CPU again
3056 * 5. Read by GPU
3057 *
3058 * 1. Allocated
3059 * (CPU, CPU)
3060 * 2. Written by CPU
3061 * (CPU, CPU)
3062 * 3. Read by GPU
3063 * (CPU+RENDER, 0)
3064 * flush_domains = CPU
3065 * invalidate_domains = RENDER
3066 * clflush (obj)
3067 * MI_FLUSH
3068 * drm_agp_chipset_flush
3069 * 4. Updated (written) by CPU again
3070 * (CPU, CPU)
3071 * flush_domains = 0 (no previous write domain)
3072 * invalidate_domains = 0 (no new read domains)
3073 * 5. Read by GPU
3074 * (CPU+RENDER, 0)
3075 * flush_domains = CPU
3076 * invalidate_domains = RENDER
3077 * clflush (obj)
3078 * MI_FLUSH
3079 * drm_agp_chipset_flush
3080 */
Keith Packardc0d90822008-11-20 23:11:08 -08003081static void
Eric Anholt8b0e3782009-02-19 14:40:50 -08003082i915_gem_object_set_to_gpu_domain(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07003083{
3084 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01003085 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003086 uint32_t invalidate_domains = 0;
3087 uint32_t flush_domains = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003088 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003089
Eric Anholt8b0e3782009-02-19 14:40:50 -08003090 BUG_ON(obj->pending_read_domains & I915_GEM_DOMAIN_CPU);
3091 BUG_ON(obj->pending_write_domain == I915_GEM_DOMAIN_CPU);
Eric Anholt673a3942008-07-30 12:06:12 -07003092
Jesse Barnes652c3932009-08-17 13:31:43 -07003093 intel_mark_busy(dev, obj);
3094
Eric Anholt673a3942008-07-30 12:06:12 -07003095#if WATCH_BUF
3096 DRM_INFO("%s: object %p read %08x -> %08x write %08x -> %08x\n",
3097 __func__, obj,
Eric Anholt8b0e3782009-02-19 14:40:50 -08003098 obj->read_domains, obj->pending_read_domains,
3099 obj->write_domain, obj->pending_write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07003100#endif
3101 /*
3102 * If the object isn't moving to a new write domain,
3103 * let the object stay in multiple read domains
3104 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003105 if (obj->pending_write_domain == 0)
3106 obj->pending_read_domains |= obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07003107 else
3108 obj_priv->dirty = 1;
3109
3110 /*
3111 * Flush the current write domain if
3112 * the new read domains don't match. Invalidate
3113 * any read domains which differ from the old
3114 * write domain
3115 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003116 if (obj->write_domain &&
3117 obj->write_domain != obj->pending_read_domains) {
Eric Anholt673a3942008-07-30 12:06:12 -07003118 flush_domains |= obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08003119 invalidate_domains |=
3120 obj->pending_read_domains & ~obj->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07003121 }
3122 /*
3123 * Invalidate any read caches which may have
3124 * stale data. That is, any new read domains.
3125 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003126 invalidate_domains |= obj->pending_read_domains & ~obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07003127 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU) {
3128#if WATCH_BUF
3129 DRM_INFO("%s: CPU domain flush %08x invalidate %08x\n",
3130 __func__, flush_domains, invalidate_domains);
3131#endif
Eric Anholt673a3942008-07-30 12:06:12 -07003132 i915_gem_clflush_object(obj);
3133 }
3134
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003135 old_read_domains = obj->read_domains;
3136
Eric Anholtefbeed92009-02-19 14:54:51 -08003137 /* The actual obj->write_domain will be updated with
3138 * pending_write_domain after we emit the accumulated flush for all
3139 * of our domain changes in execbuffers (which clears objects'
3140 * write_domains). So if we have a current write domain that we
3141 * aren't changing, set pending_write_domain to that.
3142 */
3143 if (flush_domains == 0 && obj->pending_write_domain == 0)
3144 obj->pending_write_domain = obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08003145 obj->read_domains = obj->pending_read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07003146
3147 dev->invalidate_domains |= invalidate_domains;
3148 dev->flush_domains |= flush_domains;
3149#if WATCH_BUF
3150 DRM_INFO("%s: read %08x write %08x invalidate %08x flush %08x\n",
3151 __func__,
3152 obj->read_domains, obj->write_domain,
3153 dev->invalidate_domains, dev->flush_domains);
3154#endif
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003155
3156 trace_i915_gem_object_change_domain(obj,
3157 old_read_domains,
3158 obj->write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07003159}
3160
3161/**
Eric Anholte47c68e2008-11-14 13:35:19 -08003162 * Moves the object from a partially CPU read to a full one.
Eric Anholt673a3942008-07-30 12:06:12 -07003163 *
Eric Anholte47c68e2008-11-14 13:35:19 -08003164 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
3165 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
3166 */
3167static void
3168i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj)
3169{
Daniel Vetter23010e42010-03-08 13:35:02 +01003170 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003171
3172 if (!obj_priv->page_cpu_valid)
3173 return;
3174
3175 /* If we're partially in the CPU read domain, finish moving it in.
3176 */
3177 if (obj->read_domains & I915_GEM_DOMAIN_CPU) {
3178 int i;
3179
3180 for (i = 0; i <= (obj->size - 1) / PAGE_SIZE; i++) {
3181 if (obj_priv->page_cpu_valid[i])
3182 continue;
Eric Anholt856fa192009-03-19 14:10:50 -07003183 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholte47c68e2008-11-14 13:35:19 -08003184 }
Eric Anholte47c68e2008-11-14 13:35:19 -08003185 }
3186
3187 /* Free the page_cpu_valid mappings which are now stale, whether
3188 * or not we've got I915_GEM_DOMAIN_CPU.
3189 */
Eric Anholt9a298b22009-03-24 12:23:04 -07003190 kfree(obj_priv->page_cpu_valid);
Eric Anholte47c68e2008-11-14 13:35:19 -08003191 obj_priv->page_cpu_valid = NULL;
3192}
3193
3194/**
3195 * Set the CPU read domain on a range of the object.
3196 *
3197 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
3198 * not entirely valid. The page_cpu_valid member of the object flags which
3199 * pages have been flushed, and will be respected by
3200 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
3201 * of the whole object.
3202 *
3203 * This function returns when the move is complete, including waiting on
3204 * flushes to occur.
Eric Anholt673a3942008-07-30 12:06:12 -07003205 */
3206static int
Eric Anholte47c68e2008-11-14 13:35:19 -08003207i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
3208 uint64_t offset, uint64_t size)
Eric Anholt673a3942008-07-30 12:06:12 -07003209{
Daniel Vetter23010e42010-03-08 13:35:02 +01003210 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003211 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003212 int i, ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003213
Eric Anholte47c68e2008-11-14 13:35:19 -08003214 if (offset == 0 && size == obj->size)
3215 return i915_gem_object_set_to_cpu_domain(obj, 0);
3216
3217 i915_gem_object_flush_gpu_write_domain(obj);
3218 /* Wait on any GPU rendering and flushing to occur. */
3219 ret = i915_gem_object_wait_rendering(obj);
3220 if (ret != 0)
3221 return ret;
3222 i915_gem_object_flush_gtt_write_domain(obj);
3223
3224 /* If we're already fully in the CPU read domain, we're done. */
3225 if (obj_priv->page_cpu_valid == NULL &&
3226 (obj->read_domains & I915_GEM_DOMAIN_CPU) != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07003227 return 0;
3228
Eric Anholte47c68e2008-11-14 13:35:19 -08003229 /* Otherwise, create/clear the per-page CPU read domain flag if we're
3230 * newly adding I915_GEM_DOMAIN_CPU
3231 */
Eric Anholt673a3942008-07-30 12:06:12 -07003232 if (obj_priv->page_cpu_valid == NULL) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003233 obj_priv->page_cpu_valid = kzalloc(obj->size / PAGE_SIZE,
3234 GFP_KERNEL);
Eric Anholte47c68e2008-11-14 13:35:19 -08003235 if (obj_priv->page_cpu_valid == NULL)
3236 return -ENOMEM;
3237 } else if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0)
3238 memset(obj_priv->page_cpu_valid, 0, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07003239
3240 /* Flush the cache on any pages that are still invalid from the CPU's
3241 * perspective.
3242 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003243 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
3244 i++) {
Eric Anholt673a3942008-07-30 12:06:12 -07003245 if (obj_priv->page_cpu_valid[i])
3246 continue;
3247
Eric Anholt856fa192009-03-19 14:10:50 -07003248 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07003249
3250 obj_priv->page_cpu_valid[i] = 1;
3251 }
3252
Eric Anholte47c68e2008-11-14 13:35:19 -08003253 /* It should now be out of any other write domains, and we can update
3254 * the domain values for our changes.
3255 */
3256 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
3257
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003258 old_read_domains = obj->read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003259 obj->read_domains |= I915_GEM_DOMAIN_CPU;
3260
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003261 trace_i915_gem_object_change_domain(obj,
3262 old_read_domains,
3263 obj->write_domain);
3264
Eric Anholt673a3942008-07-30 12:06:12 -07003265 return 0;
3266}
3267
3268/**
Eric Anholt673a3942008-07-30 12:06:12 -07003269 * Pin an object to the GTT and evaluate the relocations landing in it.
3270 */
3271static int
3272i915_gem_object_pin_and_relocate(struct drm_gem_object *obj,
3273 struct drm_file *file_priv,
Jesse Barnes76446ca2009-12-17 22:05:42 -05003274 struct drm_i915_gem_exec_object2 *entry,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003275 struct drm_i915_gem_relocation_entry *relocs)
Eric Anholt673a3942008-07-30 12:06:12 -07003276{
3277 struct drm_device *dev = obj->dev;
Keith Packard0839ccb2008-10-30 19:38:48 -07003278 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01003279 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003280 int i, ret;
Keith Packard0839ccb2008-10-30 19:38:48 -07003281 void __iomem *reloc_page;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003282 bool need_fence;
3283
3284 need_fence = entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
3285 obj_priv->tiling_mode != I915_TILING_NONE;
3286
3287 /* Check fence reg constraints and rebind if necessary */
Chris Wilson808b24d2010-05-27 13:18:15 +01003288 if (need_fence &&
3289 !i915_gem_object_fence_offset_ok(obj,
3290 obj_priv->tiling_mode)) {
3291 ret = i915_gem_object_unbind(obj);
3292 if (ret)
3293 return ret;
3294 }
Eric Anholt673a3942008-07-30 12:06:12 -07003295
3296 /* Choose the GTT offset for our buffer and put it there. */
3297 ret = i915_gem_object_pin(obj, (uint32_t) entry->alignment);
3298 if (ret)
3299 return ret;
3300
Jesse Barnes76446ca2009-12-17 22:05:42 -05003301 /*
3302 * Pre-965 chips need a fence register set up in order to
3303 * properly handle blits to/from tiled surfaces.
3304 */
3305 if (need_fence) {
3306 ret = i915_gem_object_get_fence_reg(obj);
3307 if (ret != 0) {
Jesse Barnes76446ca2009-12-17 22:05:42 -05003308 i915_gem_object_unpin(obj);
3309 return ret;
3310 }
3311 }
3312
Eric Anholt673a3942008-07-30 12:06:12 -07003313 entry->offset = obj_priv->gtt_offset;
3314
Eric Anholt673a3942008-07-30 12:06:12 -07003315 /* Apply the relocations, using the GTT aperture to avoid cache
3316 * flushing requirements.
3317 */
3318 for (i = 0; i < entry->relocation_count; i++) {
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003319 struct drm_i915_gem_relocation_entry *reloc= &relocs[i];
Eric Anholt673a3942008-07-30 12:06:12 -07003320 struct drm_gem_object *target_obj;
3321 struct drm_i915_gem_object *target_obj_priv;
Eric Anholt3043c602008-10-02 12:24:47 -07003322 uint32_t reloc_val, reloc_offset;
3323 uint32_t __iomem *reloc_entry;
Eric Anholt673a3942008-07-30 12:06:12 -07003324
Eric Anholt673a3942008-07-30 12:06:12 -07003325 target_obj = drm_gem_object_lookup(obj->dev, file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003326 reloc->target_handle);
Eric Anholt673a3942008-07-30 12:06:12 -07003327 if (target_obj == NULL) {
3328 i915_gem_object_unpin(obj);
3329 return -EBADF;
3330 }
Daniel Vetter23010e42010-03-08 13:35:02 +01003331 target_obj_priv = to_intel_bo(target_obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003332
Chris Wilson8542a0b2009-09-09 21:15:15 +01003333#if WATCH_RELOC
3334 DRM_INFO("%s: obj %p offset %08x target %d "
3335 "read %08x write %08x gtt %08x "
3336 "presumed %08x delta %08x\n",
3337 __func__,
3338 obj,
3339 (int) reloc->offset,
3340 (int) reloc->target_handle,
3341 (int) reloc->read_domains,
3342 (int) reloc->write_domain,
3343 (int) target_obj_priv->gtt_offset,
3344 (int) reloc->presumed_offset,
3345 reloc->delta);
3346#endif
3347
Eric Anholt673a3942008-07-30 12:06:12 -07003348 /* The target buffer should have appeared before us in the
3349 * exec_object list, so it should have a GTT space bound by now.
3350 */
3351 if (target_obj_priv->gtt_space == NULL) {
3352 DRM_ERROR("No GTT space found for object %d\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003353 reloc->target_handle);
Eric Anholt673a3942008-07-30 12:06:12 -07003354 drm_gem_object_unreference(target_obj);
3355 i915_gem_object_unpin(obj);
3356 return -EINVAL;
3357 }
3358
Chris Wilson8542a0b2009-09-09 21:15:15 +01003359 /* Validate that the target is in a valid r/w GPU domain */
Daniel Vetter16edd552010-02-19 11:52:02 +01003360 if (reloc->write_domain & (reloc->write_domain - 1)) {
3361 DRM_ERROR("reloc with multiple write domains: "
3362 "obj %p target %d offset %d "
3363 "read %08x write %08x",
3364 obj, reloc->target_handle,
3365 (int) reloc->offset,
3366 reloc->read_domains,
3367 reloc->write_domain);
3368 return -EINVAL;
3369 }
Chris Wilson8542a0b2009-09-09 21:15:15 +01003370 if (reloc->write_domain & I915_GEM_DOMAIN_CPU ||
3371 reloc->read_domains & I915_GEM_DOMAIN_CPU) {
3372 DRM_ERROR("reloc with read/write CPU domains: "
3373 "obj %p target %d offset %d "
3374 "read %08x write %08x",
3375 obj, reloc->target_handle,
3376 (int) reloc->offset,
3377 reloc->read_domains,
3378 reloc->write_domain);
3379 drm_gem_object_unreference(target_obj);
3380 i915_gem_object_unpin(obj);
3381 return -EINVAL;
3382 }
3383 if (reloc->write_domain && target_obj->pending_write_domain &&
3384 reloc->write_domain != target_obj->pending_write_domain) {
3385 DRM_ERROR("Write domain conflict: "
3386 "obj %p target %d offset %d "
3387 "new %08x old %08x\n",
3388 obj, reloc->target_handle,
3389 (int) reloc->offset,
3390 reloc->write_domain,
3391 target_obj->pending_write_domain);
3392 drm_gem_object_unreference(target_obj);
3393 i915_gem_object_unpin(obj);
3394 return -EINVAL;
3395 }
3396
3397 target_obj->pending_read_domains |= reloc->read_domains;
3398 target_obj->pending_write_domain |= reloc->write_domain;
3399
3400 /* If the relocation already has the right value in it, no
3401 * more work needs to be done.
3402 */
3403 if (target_obj_priv->gtt_offset == reloc->presumed_offset) {
3404 drm_gem_object_unreference(target_obj);
3405 continue;
3406 }
3407
3408 /* Check that the relocation address is valid... */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003409 if (reloc->offset > obj->size - 4) {
Eric Anholt673a3942008-07-30 12:06:12 -07003410 DRM_ERROR("Relocation beyond object bounds: "
3411 "obj %p target %d offset %d size %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003412 obj, reloc->target_handle,
3413 (int) reloc->offset, (int) obj->size);
Eric Anholt673a3942008-07-30 12:06:12 -07003414 drm_gem_object_unreference(target_obj);
3415 i915_gem_object_unpin(obj);
3416 return -EINVAL;
3417 }
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003418 if (reloc->offset & 3) {
Eric Anholt673a3942008-07-30 12:06:12 -07003419 DRM_ERROR("Relocation not 4-byte aligned: "
3420 "obj %p target %d offset %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003421 obj, reloc->target_handle,
3422 (int) reloc->offset);
Eric Anholt673a3942008-07-30 12:06:12 -07003423 drm_gem_object_unreference(target_obj);
3424 i915_gem_object_unpin(obj);
3425 return -EINVAL;
3426 }
3427
Chris Wilson8542a0b2009-09-09 21:15:15 +01003428 /* and points to somewhere within the target object. */
Chris Wilsoncd0b9fb2009-09-15 23:23:18 +01003429 if (reloc->delta >= target_obj->size) {
3430 DRM_ERROR("Relocation beyond target object bounds: "
3431 "obj %p target %d delta %d size %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003432 obj, reloc->target_handle,
Chris Wilsoncd0b9fb2009-09-15 23:23:18 +01003433 (int) reloc->delta, (int) target_obj->size);
Chris Wilson491152b2009-02-11 14:26:32 +00003434 drm_gem_object_unreference(target_obj);
3435 i915_gem_object_unpin(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003436 return -EINVAL;
3437 }
3438
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003439 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
3440 if (ret != 0) {
3441 drm_gem_object_unreference(target_obj);
3442 i915_gem_object_unpin(obj);
3443 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -07003444 }
3445
3446 /* Map the page containing the relocation we're going to
3447 * perform.
3448 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003449 reloc_offset = obj_priv->gtt_offset + reloc->offset;
Keith Packard0839ccb2008-10-30 19:38:48 -07003450 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
3451 (reloc_offset &
3452 ~(PAGE_SIZE - 1)));
Eric Anholt3043c602008-10-02 12:24:47 -07003453 reloc_entry = (uint32_t __iomem *)(reloc_page +
Keith Packard0839ccb2008-10-30 19:38:48 -07003454 (reloc_offset & (PAGE_SIZE - 1)));
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003455 reloc_val = target_obj_priv->gtt_offset + reloc->delta;
Eric Anholt673a3942008-07-30 12:06:12 -07003456
3457#if WATCH_BUF
3458 DRM_INFO("Applied relocation: %p@0x%08x %08x -> %08x\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003459 obj, (unsigned int) reloc->offset,
Eric Anholt673a3942008-07-30 12:06:12 -07003460 readl(reloc_entry), reloc_val);
3461#endif
3462 writel(reloc_val, reloc_entry);
Keith Packard0839ccb2008-10-30 19:38:48 -07003463 io_mapping_unmap_atomic(reloc_page);
Eric Anholt673a3942008-07-30 12:06:12 -07003464
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003465 /* The updated presumed offset for this entry will be
3466 * copied back out to the user.
Eric Anholt673a3942008-07-30 12:06:12 -07003467 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003468 reloc->presumed_offset = target_obj_priv->gtt_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003469
3470 drm_gem_object_unreference(target_obj);
3471 }
3472
Eric Anholt673a3942008-07-30 12:06:12 -07003473#if WATCH_BUF
3474 if (0)
3475 i915_gem_dump_object(obj, 128, __func__, ~0);
3476#endif
3477 return 0;
3478}
3479
Eric Anholt673a3942008-07-30 12:06:12 -07003480/* Throttle our rendering by waiting until the ring has completed our requests
3481 * emitted over 20 msec ago.
3482 *
Eric Anholtb9624422009-06-03 07:27:35 +00003483 * Note that if we were to use the current jiffies each time around the loop,
3484 * we wouldn't escape the function with any frames outstanding if the time to
3485 * render a frame was over 20ms.
3486 *
Eric Anholt673a3942008-07-30 12:06:12 -07003487 * This should get us reasonable parallelism between CPU and GPU but also
3488 * relatively low latency when blocking on a particular request to finish.
3489 */
3490static int
3491i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file_priv)
3492{
3493 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
3494 int ret = 0;
Eric Anholtb9624422009-06-03 07:27:35 +00003495 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
Eric Anholt673a3942008-07-30 12:06:12 -07003496
3497 mutex_lock(&dev->struct_mutex);
Eric Anholtb9624422009-06-03 07:27:35 +00003498 while (!list_empty(&i915_file_priv->mm.request_list)) {
3499 struct drm_i915_gem_request *request;
3500
3501 request = list_first_entry(&i915_file_priv->mm.request_list,
3502 struct drm_i915_gem_request,
3503 client_list);
3504
3505 if (time_after_eq(request->emitted_jiffies, recent_enough))
3506 break;
3507
Zou Nan hai852835f2010-05-21 09:08:56 +08003508 ret = i915_wait_request(dev, request->seqno, request->ring);
Eric Anholtb9624422009-06-03 07:27:35 +00003509 if (ret != 0)
3510 break;
3511 }
Eric Anholt673a3942008-07-30 12:06:12 -07003512 mutex_unlock(&dev->struct_mutex);
Eric Anholtb9624422009-06-03 07:27:35 +00003513
Eric Anholt673a3942008-07-30 12:06:12 -07003514 return ret;
3515}
3516
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003517static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003518i915_gem_get_relocs_from_user(struct drm_i915_gem_exec_object2 *exec_list,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003519 uint32_t buffer_count,
3520 struct drm_i915_gem_relocation_entry **relocs)
3521{
3522 uint32_t reloc_count = 0, reloc_index = 0, i;
3523 int ret;
3524
3525 *relocs = NULL;
3526 for (i = 0; i < buffer_count; i++) {
3527 if (reloc_count + exec_list[i].relocation_count < reloc_count)
3528 return -EINVAL;
3529 reloc_count += exec_list[i].relocation_count;
3530 }
3531
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003532 *relocs = drm_calloc_large(reloc_count, sizeof(**relocs));
Jesse Barnes76446ca2009-12-17 22:05:42 -05003533 if (*relocs == NULL) {
3534 DRM_ERROR("failed to alloc relocs, count %d\n", reloc_count);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003535 return -ENOMEM;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003536 }
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003537
3538 for (i = 0; i < buffer_count; i++) {
3539 struct drm_i915_gem_relocation_entry __user *user_relocs;
3540
3541 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3542
3543 ret = copy_from_user(&(*relocs)[reloc_index],
3544 user_relocs,
3545 exec_list[i].relocation_count *
3546 sizeof(**relocs));
3547 if (ret != 0) {
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003548 drm_free_large(*relocs);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003549 *relocs = NULL;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003550 return -EFAULT;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003551 }
3552
3553 reloc_index += exec_list[i].relocation_count;
3554 }
3555
Florian Mickler2bc43b52009-04-06 22:55:41 +02003556 return 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003557}
3558
3559static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003560i915_gem_put_relocs_to_user(struct drm_i915_gem_exec_object2 *exec_list,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003561 uint32_t buffer_count,
3562 struct drm_i915_gem_relocation_entry *relocs)
3563{
3564 uint32_t reloc_count = 0, i;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003565 int ret = 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003566
Chris Wilson93533c22010-01-31 10:40:48 +00003567 if (relocs == NULL)
3568 return 0;
3569
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003570 for (i = 0; i < buffer_count; i++) {
3571 struct drm_i915_gem_relocation_entry __user *user_relocs;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003572 int unwritten;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003573
3574 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3575
Florian Mickler2bc43b52009-04-06 22:55:41 +02003576 unwritten = copy_to_user(user_relocs,
3577 &relocs[reloc_count],
3578 exec_list[i].relocation_count *
3579 sizeof(*relocs));
3580
3581 if (unwritten) {
3582 ret = -EFAULT;
3583 goto err;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003584 }
3585
3586 reloc_count += exec_list[i].relocation_count;
3587 }
3588
Florian Mickler2bc43b52009-04-06 22:55:41 +02003589err:
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003590 drm_free_large(relocs);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003591
3592 return ret;
3593}
3594
Chris Wilson83d60792009-06-06 09:45:57 +01003595static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003596i915_gem_check_execbuffer (struct drm_i915_gem_execbuffer2 *exec,
Chris Wilson83d60792009-06-06 09:45:57 +01003597 uint64_t exec_offset)
3598{
3599 uint32_t exec_start, exec_len;
3600
3601 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
3602 exec_len = (uint32_t) exec->batch_len;
3603
3604 if ((exec_start | exec_len) & 0x7)
3605 return -EINVAL;
3606
3607 if (!exec_start)
3608 return -EINVAL;
3609
3610 return 0;
3611}
3612
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003613static int
3614i915_gem_wait_for_pending_flip(struct drm_device *dev,
3615 struct drm_gem_object **object_list,
3616 int count)
3617{
3618 drm_i915_private_t *dev_priv = dev->dev_private;
3619 struct drm_i915_gem_object *obj_priv;
3620 DEFINE_WAIT(wait);
3621 int i, ret = 0;
3622
3623 for (;;) {
3624 prepare_to_wait(&dev_priv->pending_flip_queue,
3625 &wait, TASK_INTERRUPTIBLE);
3626 for (i = 0; i < count; i++) {
Daniel Vetter23010e42010-03-08 13:35:02 +01003627 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003628 if (atomic_read(&obj_priv->pending_flip) > 0)
3629 break;
3630 }
3631 if (i == count)
3632 break;
3633
3634 if (!signal_pending(current)) {
3635 mutex_unlock(&dev->struct_mutex);
3636 schedule();
3637 mutex_lock(&dev->struct_mutex);
3638 continue;
3639 }
3640 ret = -ERESTARTSYS;
3641 break;
3642 }
3643 finish_wait(&dev_priv->pending_flip_queue, &wait);
3644
3645 return ret;
3646}
3647
Chris Wilson43b27f42010-07-02 08:57:15 +01003648
Eric Anholt673a3942008-07-30 12:06:12 -07003649int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003650i915_gem_do_execbuffer(struct drm_device *dev, void *data,
3651 struct drm_file *file_priv,
3652 struct drm_i915_gem_execbuffer2 *args,
3653 struct drm_i915_gem_exec_object2 *exec_list)
Eric Anholt673a3942008-07-30 12:06:12 -07003654{
3655 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003656 struct drm_gem_object **object_list = NULL;
3657 struct drm_gem_object *batch_obj;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003658 struct drm_i915_gem_object *obj_priv;
Eric Anholt201361a2009-03-11 12:30:04 -07003659 struct drm_clip_rect *cliprects = NULL;
Chris Wilson93533c22010-01-31 10:40:48 +00003660 struct drm_i915_gem_relocation_entry *relocs = NULL;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003661 int ret = 0, ret2, i, pinned = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003662 uint64_t exec_offset;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003663 uint32_t seqno, flush_domains, reloc_index;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003664 int pin_tries, flips;
Eric Anholt673a3942008-07-30 12:06:12 -07003665
Zou Nan hai852835f2010-05-21 09:08:56 +08003666 struct intel_ring_buffer *ring = NULL;
3667
Eric Anholt673a3942008-07-30 12:06:12 -07003668#if WATCH_EXEC
3669 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3670 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3671#endif
Zou Nan haid1b851f2010-05-21 09:08:57 +08003672 if (args->flags & I915_EXEC_BSD) {
3673 if (!HAS_BSD(dev)) {
3674 DRM_ERROR("execbuf with wrong flag\n");
3675 return -EINVAL;
3676 }
3677 ring = &dev_priv->bsd_ring;
3678 } else {
3679 ring = &dev_priv->render_ring;
3680 }
3681
Eric Anholt673a3942008-07-30 12:06:12 -07003682
Eric Anholt4f481ed2008-09-10 14:22:49 -07003683 if (args->buffer_count < 1) {
3684 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3685 return -EINVAL;
3686 }
Eric Anholtc8e0f932009-11-22 03:49:37 +01003687 object_list = drm_malloc_ab(sizeof(*object_list), args->buffer_count);
Jesse Barnes76446ca2009-12-17 22:05:42 -05003688 if (object_list == NULL) {
3689 DRM_ERROR("Failed to allocate object list for %d buffers\n",
Eric Anholt673a3942008-07-30 12:06:12 -07003690 args->buffer_count);
3691 ret = -ENOMEM;
3692 goto pre_mutex_err;
3693 }
Eric Anholt673a3942008-07-30 12:06:12 -07003694
Eric Anholt201361a2009-03-11 12:30:04 -07003695 if (args->num_cliprects != 0) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003696 cliprects = kcalloc(args->num_cliprects, sizeof(*cliprects),
3697 GFP_KERNEL);
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003698 if (cliprects == NULL) {
3699 ret = -ENOMEM;
Eric Anholt201361a2009-03-11 12:30:04 -07003700 goto pre_mutex_err;
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003701 }
Eric Anholt201361a2009-03-11 12:30:04 -07003702
3703 ret = copy_from_user(cliprects,
3704 (struct drm_clip_rect __user *)
3705 (uintptr_t) args->cliprects_ptr,
3706 sizeof(*cliprects) * args->num_cliprects);
3707 if (ret != 0) {
3708 DRM_ERROR("copy %d cliprects failed: %d\n",
3709 args->num_cliprects, ret);
3710 goto pre_mutex_err;
3711 }
3712 }
3713
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003714 ret = i915_gem_get_relocs_from_user(exec_list, args->buffer_count,
3715 &relocs);
3716 if (ret != 0)
3717 goto pre_mutex_err;
3718
Eric Anholt673a3942008-07-30 12:06:12 -07003719 mutex_lock(&dev->struct_mutex);
3720
3721 i915_verify_inactive(dev, __FILE__, __LINE__);
3722
Ben Gamariba1234d2009-09-14 17:48:47 -04003723 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07003724 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003725 ret = -EIO;
3726 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003727 }
3728
3729 if (dev_priv->mm.suspended) {
Eric Anholt673a3942008-07-30 12:06:12 -07003730 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003731 ret = -EBUSY;
3732 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003733 }
3734
Keith Packardac94a962008-11-20 23:30:27 -08003735 /* Look up object handles */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003736 flips = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003737 for (i = 0; i < args->buffer_count; i++) {
3738 object_list[i] = drm_gem_object_lookup(dev, file_priv,
3739 exec_list[i].handle);
3740 if (object_list[i] == NULL) {
3741 DRM_ERROR("Invalid object handle %d at index %d\n",
3742 exec_list[i].handle, i);
Chris Wilson0ce907f2010-01-23 20:26:35 +00003743 /* prevent error path from reading uninitialized data */
3744 args->buffer_count = i + 1;
Eric Anholt673a3942008-07-30 12:06:12 -07003745 ret = -EBADF;
3746 goto err;
3747 }
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003748
Daniel Vetter23010e42010-03-08 13:35:02 +01003749 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003750 if (obj_priv->in_execbuffer) {
3751 DRM_ERROR("Object %p appears more than once in object list\n",
3752 object_list[i]);
Chris Wilson0ce907f2010-01-23 20:26:35 +00003753 /* prevent error path from reading uninitialized data */
3754 args->buffer_count = i + 1;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003755 ret = -EBADF;
3756 goto err;
3757 }
3758 obj_priv->in_execbuffer = true;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003759 flips += atomic_read(&obj_priv->pending_flip);
3760 }
3761
3762 if (flips > 0) {
3763 ret = i915_gem_wait_for_pending_flip(dev, object_list,
3764 args->buffer_count);
3765 if (ret)
3766 goto err;
Keith Packardac94a962008-11-20 23:30:27 -08003767 }
Eric Anholt673a3942008-07-30 12:06:12 -07003768
Keith Packardac94a962008-11-20 23:30:27 -08003769 /* Pin and relocate */
3770 for (pin_tries = 0; ; pin_tries++) {
3771 ret = 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003772 reloc_index = 0;
3773
Keith Packardac94a962008-11-20 23:30:27 -08003774 for (i = 0; i < args->buffer_count; i++) {
3775 object_list[i]->pending_read_domains = 0;
3776 object_list[i]->pending_write_domain = 0;
3777 ret = i915_gem_object_pin_and_relocate(object_list[i],
3778 file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003779 &exec_list[i],
3780 &relocs[reloc_index]);
Keith Packardac94a962008-11-20 23:30:27 -08003781 if (ret)
3782 break;
3783 pinned = i + 1;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003784 reloc_index += exec_list[i].relocation_count;
Keith Packardac94a962008-11-20 23:30:27 -08003785 }
3786 /* success */
3787 if (ret == 0)
3788 break;
3789
3790 /* error other than GTT full, or we've already tried again */
Chris Wilson2939e1f2009-06-06 09:46:03 +01003791 if (ret != -ENOSPC || pin_tries >= 1) {
Chris Wilson07f73f62009-09-14 16:50:30 +01003792 if (ret != -ERESTARTSYS) {
3793 unsigned long long total_size = 0;
Chris Wilson3d1cc472010-05-27 13:18:19 +01003794 int num_fences = 0;
3795 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson43b27f42010-07-02 08:57:15 +01003796 obj_priv = to_intel_bo(object_list[i]);
Chris Wilson3d1cc472010-05-27 13:18:19 +01003797
Chris Wilson07f73f62009-09-14 16:50:30 +01003798 total_size += object_list[i]->size;
Chris Wilson3d1cc472010-05-27 13:18:19 +01003799 num_fences +=
3800 exec_list[i].flags & EXEC_OBJECT_NEEDS_FENCE &&
3801 obj_priv->tiling_mode != I915_TILING_NONE;
3802 }
3803 DRM_ERROR("Failed to pin buffer %d of %d, total %llu bytes, %d fences: %d\n",
Chris Wilson07f73f62009-09-14 16:50:30 +01003804 pinned+1, args->buffer_count,
Chris Wilson3d1cc472010-05-27 13:18:19 +01003805 total_size, num_fences,
3806 ret);
Chris Wilson07f73f62009-09-14 16:50:30 +01003807 DRM_ERROR("%d objects [%d pinned], "
3808 "%d object bytes [%d pinned], "
3809 "%d/%d gtt bytes\n",
3810 atomic_read(&dev->object_count),
3811 atomic_read(&dev->pin_count),
3812 atomic_read(&dev->object_memory),
3813 atomic_read(&dev->pin_memory),
3814 atomic_read(&dev->gtt_memory),
3815 dev->gtt_total);
3816 }
Eric Anholt673a3942008-07-30 12:06:12 -07003817 goto err;
3818 }
Keith Packardac94a962008-11-20 23:30:27 -08003819
3820 /* unpin all of our buffers */
3821 for (i = 0; i < pinned; i++)
3822 i915_gem_object_unpin(object_list[i]);
Eric Anholtb1177632008-12-10 10:09:41 -08003823 pinned = 0;
Keith Packardac94a962008-11-20 23:30:27 -08003824
3825 /* evict everyone we can from the aperture */
3826 ret = i915_gem_evict_everything(dev);
Chris Wilson07f73f62009-09-14 16:50:30 +01003827 if (ret && ret != -ENOSPC)
Keith Packardac94a962008-11-20 23:30:27 -08003828 goto err;
Eric Anholt673a3942008-07-30 12:06:12 -07003829 }
3830
3831 /* Set the pending read domains for the batch buffer to COMMAND */
3832 batch_obj = object_list[args->buffer_count-1];
Chris Wilson5f26a2c2009-06-06 09:45:58 +01003833 if (batch_obj->pending_write_domain) {
3834 DRM_ERROR("Attempting to use self-modifying batch buffer\n");
3835 ret = -EINVAL;
3836 goto err;
3837 }
3838 batch_obj->pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
Eric Anholt673a3942008-07-30 12:06:12 -07003839
Chris Wilson83d60792009-06-06 09:45:57 +01003840 /* Sanity check the batch buffer, prior to moving objects */
3841 exec_offset = exec_list[args->buffer_count - 1].offset;
3842 ret = i915_gem_check_execbuffer (args, exec_offset);
3843 if (ret != 0) {
3844 DRM_ERROR("execbuf with invalid offset/length\n");
3845 goto err;
3846 }
3847
Eric Anholt673a3942008-07-30 12:06:12 -07003848 i915_verify_inactive(dev, __FILE__, __LINE__);
3849
Keith Packard646f0f62008-11-20 23:23:03 -08003850 /* Zero the global flush/invalidate flags. These
3851 * will be modified as new domains are computed
3852 * for each object
3853 */
3854 dev->invalidate_domains = 0;
3855 dev->flush_domains = 0;
3856
Eric Anholt673a3942008-07-30 12:06:12 -07003857 for (i = 0; i < args->buffer_count; i++) {
3858 struct drm_gem_object *obj = object_list[i];
Eric Anholt673a3942008-07-30 12:06:12 -07003859
Keith Packard646f0f62008-11-20 23:23:03 -08003860 /* Compute new gpu domains and update invalidate/flush */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003861 i915_gem_object_set_to_gpu_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003862 }
3863
3864 i915_verify_inactive(dev, __FILE__, __LINE__);
3865
Keith Packard646f0f62008-11-20 23:23:03 -08003866 if (dev->invalidate_domains | dev->flush_domains) {
3867#if WATCH_EXEC
3868 DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
3869 __func__,
3870 dev->invalidate_domains,
3871 dev->flush_domains);
3872#endif
3873 i915_gem_flush(dev,
3874 dev->invalidate_domains,
3875 dev->flush_domains);
Zou Nan hai852835f2010-05-21 09:08:56 +08003876 if (dev->flush_domains & I915_GEM_GPU_DOMAINS) {
Eric Anholtb9624422009-06-03 07:27:35 +00003877 (void)i915_add_request(dev, file_priv,
Zou Nan hai852835f2010-05-21 09:08:56 +08003878 dev->flush_domains,
3879 &dev_priv->render_ring);
3880
Zou Nan haid1b851f2010-05-21 09:08:57 +08003881 if (HAS_BSD(dev))
3882 (void)i915_add_request(dev, file_priv,
3883 dev->flush_domains,
3884 &dev_priv->bsd_ring);
Zou Nan hai852835f2010-05-21 09:08:56 +08003885 }
Keith Packard646f0f62008-11-20 23:23:03 -08003886 }
Eric Anholt673a3942008-07-30 12:06:12 -07003887
Eric Anholtefbeed92009-02-19 14:54:51 -08003888 for (i = 0; i < args->buffer_count; i++) {
3889 struct drm_gem_object *obj = object_list[i];
Daniel Vetter23010e42010-03-08 13:35:02 +01003890 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003891 uint32_t old_write_domain = obj->write_domain;
Eric Anholtefbeed92009-02-19 14:54:51 -08003892
3893 obj->write_domain = obj->pending_write_domain;
Daniel Vetter99fcb762010-02-07 16:20:18 +01003894 if (obj->write_domain)
3895 list_move_tail(&obj_priv->gpu_write_list,
3896 &dev_priv->mm.gpu_write_list);
3897 else
3898 list_del_init(&obj_priv->gpu_write_list);
3899
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003900 trace_i915_gem_object_change_domain(obj,
3901 obj->read_domains,
3902 old_write_domain);
Eric Anholtefbeed92009-02-19 14:54:51 -08003903 }
3904
Eric Anholt673a3942008-07-30 12:06:12 -07003905 i915_verify_inactive(dev, __FILE__, __LINE__);
3906
3907#if WATCH_COHERENCY
3908 for (i = 0; i < args->buffer_count; i++) {
3909 i915_gem_object_check_coherency(object_list[i],
3910 exec_list[i].handle);
3911 }
3912#endif
3913
Eric Anholt673a3942008-07-30 12:06:12 -07003914#if WATCH_EXEC
Ben Gamari6911a9b2009-04-02 11:24:54 -07003915 i915_gem_dump_object(batch_obj,
Eric Anholt673a3942008-07-30 12:06:12 -07003916 args->batch_len,
3917 __func__,
3918 ~0);
3919#endif
3920
Eric Anholt673a3942008-07-30 12:06:12 -07003921 /* Exec the batchbuffer */
Zou Nan hai852835f2010-05-21 09:08:56 +08003922 ret = ring->dispatch_gem_execbuffer(dev, ring, args,
3923 cliprects, exec_offset);
Eric Anholt673a3942008-07-30 12:06:12 -07003924 if (ret) {
3925 DRM_ERROR("dispatch failed %d\n", ret);
3926 goto err;
3927 }
3928
3929 /*
3930 * Ensure that the commands in the batch buffer are
3931 * finished before the interrupt fires
3932 */
Zou Nan hai852835f2010-05-21 09:08:56 +08003933 flush_domains = i915_retire_commands(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07003934
3935 i915_verify_inactive(dev, __FILE__, __LINE__);
3936
3937 /*
3938 * Get a seqno representing the execution of the current buffer,
3939 * which we can wait on. We would like to mitigate these interrupts,
3940 * likely by only creating seqnos occasionally (so that we have
3941 * *some* interrupts representing completion of buffers that we can
3942 * wait on when trying to clear up gtt space).
3943 */
Zou Nan hai852835f2010-05-21 09:08:56 +08003944 seqno = i915_add_request(dev, file_priv, flush_domains, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07003945 BUG_ON(seqno == 0);
Eric Anholt673a3942008-07-30 12:06:12 -07003946 for (i = 0; i < args->buffer_count; i++) {
3947 struct drm_gem_object *obj = object_list[i];
Zou Nan hai852835f2010-05-21 09:08:56 +08003948 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003949
Zou Nan hai852835f2010-05-21 09:08:56 +08003950 i915_gem_object_move_to_active(obj, seqno, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07003951#if WATCH_LRU
3952 DRM_INFO("%s: move to exec list %p\n", __func__, obj);
3953#endif
3954 }
3955#if WATCH_LRU
3956 i915_dump_lru(dev, __func__);
3957#endif
3958
3959 i915_verify_inactive(dev, __FILE__, __LINE__);
3960
Eric Anholt673a3942008-07-30 12:06:12 -07003961err:
Julia Lawallaad87df2008-12-21 16:28:47 +01003962 for (i = 0; i < pinned; i++)
3963 i915_gem_object_unpin(object_list[i]);
Eric Anholt673a3942008-07-30 12:06:12 -07003964
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003965 for (i = 0; i < args->buffer_count; i++) {
3966 if (object_list[i]) {
Daniel Vetter23010e42010-03-08 13:35:02 +01003967 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003968 obj_priv->in_execbuffer = false;
3969 }
Julia Lawallaad87df2008-12-21 16:28:47 +01003970 drm_gem_object_unreference(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003971 }
Julia Lawallaad87df2008-12-21 16:28:47 +01003972
Eric Anholt673a3942008-07-30 12:06:12 -07003973 mutex_unlock(&dev->struct_mutex);
3974
Chris Wilson93533c22010-01-31 10:40:48 +00003975pre_mutex_err:
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003976 /* Copy the updated relocations out regardless of current error
3977 * state. Failure to update the relocs would mean that the next
3978 * time userland calls execbuf, it would do so with presumed offset
3979 * state that didn't match the actual object state.
3980 */
3981 ret2 = i915_gem_put_relocs_to_user(exec_list, args->buffer_count,
3982 relocs);
3983 if (ret2 != 0) {
3984 DRM_ERROR("Failed to copy relocations back out: %d\n", ret2);
3985
3986 if (ret == 0)
3987 ret = ret2;
3988 }
3989
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003990 drm_free_large(object_list);
Eric Anholt9a298b22009-03-24 12:23:04 -07003991 kfree(cliprects);
Eric Anholt673a3942008-07-30 12:06:12 -07003992
3993 return ret;
3994}
3995
Jesse Barnes76446ca2009-12-17 22:05:42 -05003996/*
3997 * Legacy execbuffer just creates an exec2 list from the original exec object
3998 * list array and passes it to the real function.
3999 */
4000int
4001i915_gem_execbuffer(struct drm_device *dev, void *data,
4002 struct drm_file *file_priv)
4003{
4004 struct drm_i915_gem_execbuffer *args = data;
4005 struct drm_i915_gem_execbuffer2 exec2;
4006 struct drm_i915_gem_exec_object *exec_list = NULL;
4007 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
4008 int ret, i;
4009
4010#if WATCH_EXEC
4011 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
4012 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
4013#endif
4014
4015 if (args->buffer_count < 1) {
4016 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
4017 return -EINVAL;
4018 }
4019
4020 /* Copy in the exec list from userland */
4021 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
4022 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
4023 if (exec_list == NULL || exec2_list == NULL) {
4024 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
4025 args->buffer_count);
4026 drm_free_large(exec_list);
4027 drm_free_large(exec2_list);
4028 return -ENOMEM;
4029 }
4030 ret = copy_from_user(exec_list,
4031 (struct drm_i915_relocation_entry __user *)
4032 (uintptr_t) args->buffers_ptr,
4033 sizeof(*exec_list) * args->buffer_count);
4034 if (ret != 0) {
4035 DRM_ERROR("copy %d exec entries failed %d\n",
4036 args->buffer_count, ret);
4037 drm_free_large(exec_list);
4038 drm_free_large(exec2_list);
4039 return -EFAULT;
4040 }
4041
4042 for (i = 0; i < args->buffer_count; i++) {
4043 exec2_list[i].handle = exec_list[i].handle;
4044 exec2_list[i].relocation_count = exec_list[i].relocation_count;
4045 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
4046 exec2_list[i].alignment = exec_list[i].alignment;
4047 exec2_list[i].offset = exec_list[i].offset;
4048 if (!IS_I965G(dev))
4049 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
4050 else
4051 exec2_list[i].flags = 0;
4052 }
4053
4054 exec2.buffers_ptr = args->buffers_ptr;
4055 exec2.buffer_count = args->buffer_count;
4056 exec2.batch_start_offset = args->batch_start_offset;
4057 exec2.batch_len = args->batch_len;
4058 exec2.DR1 = args->DR1;
4059 exec2.DR4 = args->DR4;
4060 exec2.num_cliprects = args->num_cliprects;
4061 exec2.cliprects_ptr = args->cliprects_ptr;
Zou Nan hai852835f2010-05-21 09:08:56 +08004062 exec2.flags = I915_EXEC_RENDER;
Jesse Barnes76446ca2009-12-17 22:05:42 -05004063
4064 ret = i915_gem_do_execbuffer(dev, data, file_priv, &exec2, exec2_list);
4065 if (!ret) {
4066 /* Copy the new buffer offsets back to the user's exec list. */
4067 for (i = 0; i < args->buffer_count; i++)
4068 exec_list[i].offset = exec2_list[i].offset;
4069 /* ... and back out to userspace */
4070 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
4071 (uintptr_t) args->buffers_ptr,
4072 exec_list,
4073 sizeof(*exec_list) * args->buffer_count);
4074 if (ret) {
4075 ret = -EFAULT;
4076 DRM_ERROR("failed to copy %d exec entries "
4077 "back to user (%d)\n",
4078 args->buffer_count, ret);
4079 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05004080 }
4081
4082 drm_free_large(exec_list);
4083 drm_free_large(exec2_list);
4084 return ret;
4085}
4086
4087int
4088i915_gem_execbuffer2(struct drm_device *dev, void *data,
4089 struct drm_file *file_priv)
4090{
4091 struct drm_i915_gem_execbuffer2 *args = data;
4092 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
4093 int ret;
4094
4095#if WATCH_EXEC
4096 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
4097 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
4098#endif
4099
4100 if (args->buffer_count < 1) {
4101 DRM_ERROR("execbuf2 with %d buffers\n", args->buffer_count);
4102 return -EINVAL;
4103 }
4104
4105 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
4106 if (exec2_list == NULL) {
4107 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
4108 args->buffer_count);
4109 return -ENOMEM;
4110 }
4111 ret = copy_from_user(exec2_list,
4112 (struct drm_i915_relocation_entry __user *)
4113 (uintptr_t) args->buffers_ptr,
4114 sizeof(*exec2_list) * args->buffer_count);
4115 if (ret != 0) {
4116 DRM_ERROR("copy %d exec entries failed %d\n",
4117 args->buffer_count, ret);
4118 drm_free_large(exec2_list);
4119 return -EFAULT;
4120 }
4121
4122 ret = i915_gem_do_execbuffer(dev, data, file_priv, args, exec2_list);
4123 if (!ret) {
4124 /* Copy the new buffer offsets back to the user's exec list. */
4125 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
4126 (uintptr_t) args->buffers_ptr,
4127 exec2_list,
4128 sizeof(*exec2_list) * args->buffer_count);
4129 if (ret) {
4130 ret = -EFAULT;
4131 DRM_ERROR("failed to copy %d exec entries "
4132 "back to user (%d)\n",
4133 args->buffer_count, ret);
4134 }
4135 }
4136
4137 drm_free_large(exec2_list);
4138 return ret;
4139}
4140
Eric Anholt673a3942008-07-30 12:06:12 -07004141int
4142i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment)
4143{
4144 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01004145 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004146 int ret;
4147
Daniel Vetter778c3542010-05-13 11:49:44 +02004148 BUG_ON(obj_priv->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT);
4149
Eric Anholt673a3942008-07-30 12:06:12 -07004150 i915_verify_inactive(dev, __FILE__, __LINE__);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01004151
4152 if (obj_priv->gtt_space != NULL) {
4153 if (alignment == 0)
4154 alignment = i915_gem_get_gtt_alignment(obj);
4155 if (obj_priv->gtt_offset & (alignment - 1)) {
4156 ret = i915_gem_object_unbind(obj);
4157 if (ret)
4158 return ret;
4159 }
4160 }
4161
Eric Anholt673a3942008-07-30 12:06:12 -07004162 if (obj_priv->gtt_space == NULL) {
4163 ret = i915_gem_object_bind_to_gtt(obj, alignment);
Chris Wilson97311292009-09-21 00:22:34 +01004164 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07004165 return ret;
Chris Wilson22c344e2009-02-11 14:26:45 +00004166 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05004167
Eric Anholt673a3942008-07-30 12:06:12 -07004168 obj_priv->pin_count++;
4169
4170 /* If the object is not active and not pending a flush,
4171 * remove it from the inactive list
4172 */
4173 if (obj_priv->pin_count == 1) {
4174 atomic_inc(&dev->pin_count);
4175 atomic_add(obj->size, &dev->pin_memory);
4176 if (!obj_priv->active &&
Chris Wilson21d509e2009-06-06 09:46:02 +01004177 (obj->write_domain & I915_GEM_GPU_DOMAINS) == 0 &&
Eric Anholt673a3942008-07-30 12:06:12 -07004178 !list_empty(&obj_priv->list))
4179 list_del_init(&obj_priv->list);
4180 }
4181 i915_verify_inactive(dev, __FILE__, __LINE__);
4182
4183 return 0;
4184}
4185
4186void
4187i915_gem_object_unpin(struct drm_gem_object *obj)
4188{
4189 struct drm_device *dev = obj->dev;
4190 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01004191 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004192
4193 i915_verify_inactive(dev, __FILE__, __LINE__);
4194 obj_priv->pin_count--;
4195 BUG_ON(obj_priv->pin_count < 0);
4196 BUG_ON(obj_priv->gtt_space == NULL);
4197
4198 /* If the object is no longer pinned, and is
4199 * neither active nor being flushed, then stick it on
4200 * the inactive list
4201 */
4202 if (obj_priv->pin_count == 0) {
4203 if (!obj_priv->active &&
Chris Wilson21d509e2009-06-06 09:46:02 +01004204 (obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
Eric Anholt673a3942008-07-30 12:06:12 -07004205 list_move_tail(&obj_priv->list,
4206 &dev_priv->mm.inactive_list);
4207 atomic_dec(&dev->pin_count);
4208 atomic_sub(obj->size, &dev->pin_memory);
4209 }
4210 i915_verify_inactive(dev, __FILE__, __LINE__);
4211}
4212
4213int
4214i915_gem_pin_ioctl(struct drm_device *dev, void *data,
4215 struct drm_file *file_priv)
4216{
4217 struct drm_i915_gem_pin *args = data;
4218 struct drm_gem_object *obj;
4219 struct drm_i915_gem_object *obj_priv;
4220 int ret;
4221
4222 mutex_lock(&dev->struct_mutex);
4223
4224 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4225 if (obj == NULL) {
4226 DRM_ERROR("Bad handle in i915_gem_pin_ioctl(): %d\n",
4227 args->handle);
4228 mutex_unlock(&dev->struct_mutex);
4229 return -EBADF;
4230 }
Daniel Vetter23010e42010-03-08 13:35:02 +01004231 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004232
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004233 if (obj_priv->madv != I915_MADV_WILLNEED) {
4234 DRM_ERROR("Attempting to pin a purgeable buffer\n");
Chris Wilson3ef94da2009-09-14 16:50:29 +01004235 drm_gem_object_unreference(obj);
4236 mutex_unlock(&dev->struct_mutex);
4237 return -EINVAL;
4238 }
4239
Jesse Barnes79e53942008-11-07 14:24:08 -08004240 if (obj_priv->pin_filp != NULL && obj_priv->pin_filp != file_priv) {
4241 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
4242 args->handle);
Chris Wilson96dec612009-02-08 19:08:04 +00004243 drm_gem_object_unreference(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004244 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08004245 return -EINVAL;
4246 }
4247
4248 obj_priv->user_pin_count++;
4249 obj_priv->pin_filp = file_priv;
4250 if (obj_priv->user_pin_count == 1) {
4251 ret = i915_gem_object_pin(obj, args->alignment);
4252 if (ret != 0) {
4253 drm_gem_object_unreference(obj);
4254 mutex_unlock(&dev->struct_mutex);
4255 return ret;
4256 }
Eric Anholt673a3942008-07-30 12:06:12 -07004257 }
4258
4259 /* XXX - flush the CPU caches for pinned objects
4260 * as the X server doesn't manage domains yet
4261 */
Eric Anholte47c68e2008-11-14 13:35:19 -08004262 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004263 args->offset = obj_priv->gtt_offset;
4264 drm_gem_object_unreference(obj);
4265 mutex_unlock(&dev->struct_mutex);
4266
4267 return 0;
4268}
4269
4270int
4271i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
4272 struct drm_file *file_priv)
4273{
4274 struct drm_i915_gem_pin *args = data;
4275 struct drm_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08004276 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07004277
4278 mutex_lock(&dev->struct_mutex);
4279
4280 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4281 if (obj == NULL) {
4282 DRM_ERROR("Bad handle in i915_gem_unpin_ioctl(): %d\n",
4283 args->handle);
4284 mutex_unlock(&dev->struct_mutex);
4285 return -EBADF;
4286 }
4287
Daniel Vetter23010e42010-03-08 13:35:02 +01004288 obj_priv = to_intel_bo(obj);
Jesse Barnes79e53942008-11-07 14:24:08 -08004289 if (obj_priv->pin_filp != file_priv) {
4290 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
4291 args->handle);
4292 drm_gem_object_unreference(obj);
4293 mutex_unlock(&dev->struct_mutex);
4294 return -EINVAL;
4295 }
4296 obj_priv->user_pin_count--;
4297 if (obj_priv->user_pin_count == 0) {
4298 obj_priv->pin_filp = NULL;
4299 i915_gem_object_unpin(obj);
4300 }
Eric Anholt673a3942008-07-30 12:06:12 -07004301
4302 drm_gem_object_unreference(obj);
4303 mutex_unlock(&dev->struct_mutex);
4304 return 0;
4305}
4306
4307int
4308i915_gem_busy_ioctl(struct drm_device *dev, void *data,
4309 struct drm_file *file_priv)
4310{
4311 struct drm_i915_gem_busy *args = data;
4312 struct drm_gem_object *obj;
4313 struct drm_i915_gem_object *obj_priv;
Zou Nan hai852835f2010-05-21 09:08:56 +08004314 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07004315
Eric Anholt673a3942008-07-30 12:06:12 -07004316 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4317 if (obj == NULL) {
4318 DRM_ERROR("Bad handle in i915_gem_busy_ioctl(): %d\n",
4319 args->handle);
Eric Anholt673a3942008-07-30 12:06:12 -07004320 return -EBADF;
4321 }
4322
Chris Wilsonb1ce7862009-06-06 09:46:00 +01004323 mutex_lock(&dev->struct_mutex);
Eric Anholtf21289b2009-02-18 09:44:56 -08004324 /* Update the active list for the hardware's current position.
4325 * Otherwise this only updates on a delayed timer or when irqs are
4326 * actually unmasked, and our working set ends up being larger than
4327 * required.
4328 */
Zou Nan hai852835f2010-05-21 09:08:56 +08004329 i915_gem_retire_requests(dev, &dev_priv->render_ring);
Eric Anholtf21289b2009-02-18 09:44:56 -08004330
Zou Nan haid1b851f2010-05-21 09:08:57 +08004331 if (HAS_BSD(dev))
4332 i915_gem_retire_requests(dev, &dev_priv->bsd_ring);
4333
Daniel Vetter23010e42010-03-08 13:35:02 +01004334 obj_priv = to_intel_bo(obj);
Eric Anholtc4de0a52008-12-14 19:05:04 -08004335 /* Don't count being on the flushing list against the object being
4336 * done. Otherwise, a buffer left on the flushing list but not getting
4337 * flushed (because nobody's flushing that domain) won't ever return
4338 * unbusy and get reused by libdrm's bo cache. The other expected
4339 * consumer of this interface, OpenGL's occlusion queries, also specs
4340 * that the objects get unbusy "eventually" without any interference.
4341 */
4342 args->busy = obj_priv->active && obj_priv->last_rendering_seqno != 0;
Eric Anholt673a3942008-07-30 12:06:12 -07004343
4344 drm_gem_object_unreference(obj);
4345 mutex_unlock(&dev->struct_mutex);
4346 return 0;
4347}
4348
4349int
4350i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4351 struct drm_file *file_priv)
4352{
4353 return i915_gem_ring_throttle(dev, file_priv);
4354}
4355
Chris Wilson3ef94da2009-09-14 16:50:29 +01004356int
4357i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4358 struct drm_file *file_priv)
4359{
4360 struct drm_i915_gem_madvise *args = data;
4361 struct drm_gem_object *obj;
4362 struct drm_i915_gem_object *obj_priv;
4363
4364 switch (args->madv) {
4365 case I915_MADV_DONTNEED:
4366 case I915_MADV_WILLNEED:
4367 break;
4368 default:
4369 return -EINVAL;
4370 }
4371
4372 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4373 if (obj == NULL) {
4374 DRM_ERROR("Bad handle in i915_gem_madvise_ioctl(): %d\n",
4375 args->handle);
4376 return -EBADF;
4377 }
4378
4379 mutex_lock(&dev->struct_mutex);
Daniel Vetter23010e42010-03-08 13:35:02 +01004380 obj_priv = to_intel_bo(obj);
Chris Wilson3ef94da2009-09-14 16:50:29 +01004381
4382 if (obj_priv->pin_count) {
4383 drm_gem_object_unreference(obj);
4384 mutex_unlock(&dev->struct_mutex);
4385
4386 DRM_ERROR("Attempted i915_gem_madvise_ioctl() on a pinned object\n");
4387 return -EINVAL;
4388 }
4389
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004390 if (obj_priv->madv != __I915_MADV_PURGED)
4391 obj_priv->madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004392
Chris Wilson2d7ef392009-09-20 23:13:10 +01004393 /* if the object is no longer bound, discard its backing storage */
4394 if (i915_gem_object_is_purgeable(obj_priv) &&
4395 obj_priv->gtt_space == NULL)
4396 i915_gem_object_truncate(obj);
4397
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004398 args->retained = obj_priv->madv != __I915_MADV_PURGED;
4399
Chris Wilson3ef94da2009-09-14 16:50:29 +01004400 drm_gem_object_unreference(obj);
4401 mutex_unlock(&dev->struct_mutex);
4402
4403 return 0;
4404}
4405
Daniel Vetterac52bc52010-04-09 19:05:06 +00004406struct drm_gem_object * i915_gem_alloc_object(struct drm_device *dev,
4407 size_t size)
4408{
Daniel Vetterc397b902010-04-09 19:05:07 +00004409 struct drm_i915_gem_object *obj;
4410
4411 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
4412 if (obj == NULL)
4413 return NULL;
4414
4415 if (drm_gem_object_init(dev, &obj->base, size) != 0) {
4416 kfree(obj);
4417 return NULL;
4418 }
4419
4420 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4421 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4422
4423 obj->agp_type = AGP_USER_MEMORY;
Daniel Vetter62b8b212010-04-09 19:05:08 +00004424 obj->base.driver_private = NULL;
Daniel Vetterc397b902010-04-09 19:05:07 +00004425 obj->fence_reg = I915_FENCE_REG_NONE;
4426 INIT_LIST_HEAD(&obj->list);
4427 INIT_LIST_HEAD(&obj->gpu_write_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00004428 obj->madv = I915_MADV_WILLNEED;
4429
4430 trace_i915_gem_object_create(&obj->base);
4431
4432 return &obj->base;
Daniel Vetterac52bc52010-04-09 19:05:06 +00004433}
4434
Eric Anholt673a3942008-07-30 12:06:12 -07004435int i915_gem_init_object(struct drm_gem_object *obj)
4436{
Daniel Vetterc397b902010-04-09 19:05:07 +00004437 BUG();
Jesse Barnesde151cf2008-11-12 10:03:55 -08004438
Eric Anholt673a3942008-07-30 12:06:12 -07004439 return 0;
4440}
4441
4442void i915_gem_free_object(struct drm_gem_object *obj)
4443{
Jesse Barnesde151cf2008-11-12 10:03:55 -08004444 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01004445 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004446
Chris Wilson1c5d22f2009-08-25 11:15:50 +01004447 trace_i915_gem_object_destroy(obj);
4448
Eric Anholt673a3942008-07-30 12:06:12 -07004449 while (obj_priv->pin_count > 0)
4450 i915_gem_object_unpin(obj);
4451
Dave Airlie71acb5e2008-12-30 20:31:46 +10004452 if (obj_priv->phys_obj)
4453 i915_gem_detach_phys_object(dev, obj);
4454
Eric Anholt673a3942008-07-30 12:06:12 -07004455 i915_gem_object_unbind(obj);
4456
Chris Wilson7e616152009-09-10 08:53:04 +01004457 if (obj_priv->mmap_offset)
4458 i915_gem_free_mmap_offset(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08004459
Daniel Vetterc397b902010-04-09 19:05:07 +00004460 drm_gem_object_release(obj);
4461
Eric Anholt9a298b22009-03-24 12:23:04 -07004462 kfree(obj_priv->page_cpu_valid);
Eric Anholt280b7132009-03-12 16:56:27 -07004463 kfree(obj_priv->bit_17);
Daniel Vetterc397b902010-04-09 19:05:07 +00004464 kfree(obj_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07004465}
4466
Chris Wilsonab5ee572009-09-20 19:25:47 +01004467/** Unbinds all inactive objects. */
Eric Anholt673a3942008-07-30 12:06:12 -07004468static int
Chris Wilsonab5ee572009-09-20 19:25:47 +01004469i915_gem_evict_from_inactive_list(struct drm_device *dev)
Eric Anholt673a3942008-07-30 12:06:12 -07004470{
Chris Wilsonab5ee572009-09-20 19:25:47 +01004471 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07004472
Chris Wilsonab5ee572009-09-20 19:25:47 +01004473 while (!list_empty(&dev_priv->mm.inactive_list)) {
4474 struct drm_gem_object *obj;
4475 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004476
Daniel Vettera8089e82010-04-09 19:05:09 +00004477 obj = &list_first_entry(&dev_priv->mm.inactive_list,
4478 struct drm_i915_gem_object,
4479 list)->base;
Eric Anholt673a3942008-07-30 12:06:12 -07004480
4481 ret = i915_gem_object_unbind(obj);
4482 if (ret != 0) {
Chris Wilsonab5ee572009-09-20 19:25:47 +01004483 DRM_ERROR("Error unbinding object: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07004484 return ret;
4485 }
4486 }
4487
Eric Anholt673a3942008-07-30 12:06:12 -07004488 return 0;
4489}
4490
Jesse Barnes5669fca2009-02-17 15:13:31 -08004491int
Eric Anholt673a3942008-07-30 12:06:12 -07004492i915_gem_idle(struct drm_device *dev)
4493{
4494 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson29105cc2010-01-07 10:39:13 +00004495 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004496
Keith Packard6dbe2772008-10-14 21:41:13 -07004497 mutex_lock(&dev->struct_mutex);
4498
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004499 if (dev_priv->mm.suspended ||
Zou Nan haid1b851f2010-05-21 09:08:57 +08004500 (dev_priv->render_ring.gem_object == NULL) ||
4501 (HAS_BSD(dev) &&
4502 dev_priv->bsd_ring.gem_object == NULL)) {
Keith Packard6dbe2772008-10-14 21:41:13 -07004503 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004504 return 0;
Keith Packard6dbe2772008-10-14 21:41:13 -07004505 }
Eric Anholt673a3942008-07-30 12:06:12 -07004506
Chris Wilson29105cc2010-01-07 10:39:13 +00004507 ret = i915_gpu_idle(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004508 if (ret) {
4509 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004510 return ret;
Keith Packard6dbe2772008-10-14 21:41:13 -07004511 }
Eric Anholt673a3942008-07-30 12:06:12 -07004512
Chris Wilson29105cc2010-01-07 10:39:13 +00004513 /* Under UMS, be paranoid and evict. */
4514 if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
4515 ret = i915_gem_evict_from_inactive_list(dev);
4516 if (ret) {
4517 mutex_unlock(&dev->struct_mutex);
4518 return ret;
4519 }
4520 }
4521
4522 /* Hack! Don't let anybody do execbuf while we don't control the chip.
4523 * We need to replace this with a semaphore, or something.
4524 * And not confound mm.suspended!
4525 */
4526 dev_priv->mm.suspended = 1;
4527 del_timer(&dev_priv->hangcheck_timer);
4528
4529 i915_kernel_lost_context(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004530 i915_gem_cleanup_ringbuffer(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00004531
Keith Packard6dbe2772008-10-14 21:41:13 -07004532 mutex_unlock(&dev->struct_mutex);
4533
Chris Wilson29105cc2010-01-07 10:39:13 +00004534 /* Cancel the retire work handler, which should be idle now. */
4535 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
4536
Eric Anholt673a3942008-07-30 12:06:12 -07004537 return 0;
4538}
4539
Jesse Barnese552eb72010-04-21 11:39:23 -07004540/*
4541 * 965+ support PIPE_CONTROL commands, which provide finer grained control
4542 * over cache flushing.
4543 */
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004544static int
Jesse Barnese552eb72010-04-21 11:39:23 -07004545i915_gem_init_pipe_control(struct drm_device *dev)
4546{
4547 drm_i915_private_t *dev_priv = dev->dev_private;
4548 struct drm_gem_object *obj;
4549 struct drm_i915_gem_object *obj_priv;
4550 int ret;
4551
Eric Anholt34dc4d42010-05-07 14:30:03 -07004552 obj = i915_gem_alloc_object(dev, 4096);
Jesse Barnese552eb72010-04-21 11:39:23 -07004553 if (obj == NULL) {
4554 DRM_ERROR("Failed to allocate seqno page\n");
4555 ret = -ENOMEM;
4556 goto err;
4557 }
4558 obj_priv = to_intel_bo(obj);
4559 obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
4560
4561 ret = i915_gem_object_pin(obj, 4096);
4562 if (ret)
4563 goto err_unref;
4564
4565 dev_priv->seqno_gfx_addr = obj_priv->gtt_offset;
4566 dev_priv->seqno_page = kmap(obj_priv->pages[0]);
4567 if (dev_priv->seqno_page == NULL)
4568 goto err_unpin;
4569
4570 dev_priv->seqno_obj = obj;
4571 memset(dev_priv->seqno_page, 0, PAGE_SIZE);
4572
4573 return 0;
4574
4575err_unpin:
4576 i915_gem_object_unpin(obj);
4577err_unref:
4578 drm_gem_object_unreference(obj);
4579err:
4580 return ret;
4581}
4582
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004583
4584static void
Jesse Barnese552eb72010-04-21 11:39:23 -07004585i915_gem_cleanup_pipe_control(struct drm_device *dev)
4586{
4587 drm_i915_private_t *dev_priv = dev->dev_private;
4588 struct drm_gem_object *obj;
4589 struct drm_i915_gem_object *obj_priv;
4590
4591 obj = dev_priv->seqno_obj;
4592 obj_priv = to_intel_bo(obj);
4593 kunmap(obj_priv->pages[0]);
4594 i915_gem_object_unpin(obj);
4595 drm_gem_object_unreference(obj);
4596 dev_priv->seqno_obj = NULL;
4597
4598 dev_priv->seqno_page = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07004599}
4600
Eric Anholt673a3942008-07-30 12:06:12 -07004601int
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004602i915_gem_init_ringbuffer(struct drm_device *dev)
4603{
4604 drm_i915_private_t *dev_priv = dev->dev_private;
4605 int ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01004606
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004607 dev_priv->render_ring = render_ring;
Chris Wilson68f95ba2010-05-27 13:18:22 +01004608
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004609 if (!I915_NEED_GFX_HWS(dev)) {
4610 dev_priv->render_ring.status_page.page_addr
4611 = dev_priv->status_page_dmah->vaddr;
4612 memset(dev_priv->render_ring.status_page.page_addr,
4613 0, PAGE_SIZE);
4614 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004615
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004616 if (HAS_PIPE_CONTROL(dev)) {
4617 ret = i915_gem_init_pipe_control(dev);
4618 if (ret)
4619 return ret;
4620 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004621
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004622 ret = intel_init_ring_buffer(dev, &dev_priv->render_ring);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004623 if (ret)
4624 goto cleanup_pipe_control;
4625
4626 if (HAS_BSD(dev)) {
Zou Nan haid1b851f2010-05-21 09:08:57 +08004627 dev_priv->bsd_ring = bsd_ring;
4628 ret = intel_init_ring_buffer(dev, &dev_priv->bsd_ring);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004629 if (ret)
4630 goto cleanup_render_ring;
Zou Nan haid1b851f2010-05-21 09:08:57 +08004631 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004632
4633 return 0;
4634
4635cleanup_render_ring:
4636 intel_cleanup_ring_buffer(dev, &dev_priv->render_ring);
4637cleanup_pipe_control:
4638 if (HAS_PIPE_CONTROL(dev))
4639 i915_gem_cleanup_pipe_control(dev);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004640 return ret;
4641}
4642
4643void
4644i915_gem_cleanup_ringbuffer(struct drm_device *dev)
4645{
4646 drm_i915_private_t *dev_priv = dev->dev_private;
4647
4648 intel_cleanup_ring_buffer(dev, &dev_priv->render_ring);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004649 if (HAS_BSD(dev))
4650 intel_cleanup_ring_buffer(dev, &dev_priv->bsd_ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004651 if (HAS_PIPE_CONTROL(dev))
4652 i915_gem_cleanup_pipe_control(dev);
4653}
4654
4655int
Eric Anholt673a3942008-07-30 12:06:12 -07004656i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
4657 struct drm_file *file_priv)
4658{
4659 drm_i915_private_t *dev_priv = dev->dev_private;
4660 int ret;
4661
Jesse Barnes79e53942008-11-07 14:24:08 -08004662 if (drm_core_check_feature(dev, DRIVER_MODESET))
4663 return 0;
4664
Ben Gamariba1234d2009-09-14 17:48:47 -04004665 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07004666 DRM_ERROR("Reenabling wedged hardware, good luck\n");
Ben Gamariba1234d2009-09-14 17:48:47 -04004667 atomic_set(&dev_priv->mm.wedged, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004668 }
4669
Eric Anholt673a3942008-07-30 12:06:12 -07004670 mutex_lock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004671 dev_priv->mm.suspended = 0;
4672
4673 ret = i915_gem_init_ringbuffer(dev);
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004674 if (ret != 0) {
4675 mutex_unlock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004676 return ret;
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004677 }
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004678
Carl Worth5e118f42009-03-20 11:54:25 -07004679 spin_lock(&dev_priv->mm.active_list_lock);
Zou Nan hai852835f2010-05-21 09:08:56 +08004680 BUG_ON(!list_empty(&dev_priv->render_ring.active_list));
Zou Nan haid1b851f2010-05-21 09:08:57 +08004681 BUG_ON(HAS_BSD(dev) && !list_empty(&dev_priv->bsd_ring.active_list));
Carl Worth5e118f42009-03-20 11:54:25 -07004682 spin_unlock(&dev_priv->mm.active_list_lock);
4683
Eric Anholt673a3942008-07-30 12:06:12 -07004684 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
4685 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
Zou Nan hai852835f2010-05-21 09:08:56 +08004686 BUG_ON(!list_empty(&dev_priv->render_ring.request_list));
Zou Nan haid1b851f2010-05-21 09:08:57 +08004687 BUG_ON(HAS_BSD(dev) && !list_empty(&dev_priv->bsd_ring.request_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004688 mutex_unlock(&dev->struct_mutex);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004689
Chris Wilson5f353082010-06-07 14:03:03 +01004690 ret = drm_irq_install(dev);
4691 if (ret)
4692 goto cleanup_ringbuffer;
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004693
Eric Anholt673a3942008-07-30 12:06:12 -07004694 return 0;
Chris Wilson5f353082010-06-07 14:03:03 +01004695
4696cleanup_ringbuffer:
4697 mutex_lock(&dev->struct_mutex);
4698 i915_gem_cleanup_ringbuffer(dev);
4699 dev_priv->mm.suspended = 1;
4700 mutex_unlock(&dev->struct_mutex);
4701
4702 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004703}
4704
4705int
4706i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
4707 struct drm_file *file_priv)
4708{
Jesse Barnes79e53942008-11-07 14:24:08 -08004709 if (drm_core_check_feature(dev, DRIVER_MODESET))
4710 return 0;
4711
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004712 drm_irq_uninstall(dev);
Linus Torvaldse6890f62009-09-08 17:09:24 -07004713 return i915_gem_idle(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004714}
4715
4716void
4717i915_gem_lastclose(struct drm_device *dev)
4718{
4719 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004720
Eric Anholte806b492009-01-22 09:56:58 -08004721 if (drm_core_check_feature(dev, DRIVER_MODESET))
4722 return;
4723
Keith Packard6dbe2772008-10-14 21:41:13 -07004724 ret = i915_gem_idle(dev);
4725 if (ret)
4726 DRM_ERROR("failed to idle hardware: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07004727}
4728
4729void
4730i915_gem_load(struct drm_device *dev)
4731{
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004732 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07004733 drm_i915_private_t *dev_priv = dev->dev_private;
4734
Carl Worth5e118f42009-03-20 11:54:25 -07004735 spin_lock_init(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07004736 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
Daniel Vetter99fcb762010-02-07 16:20:18 +01004737 INIT_LIST_HEAD(&dev_priv->mm.gpu_write_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004738 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07004739 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Zou Nan hai852835f2010-05-21 09:08:56 +08004740 INIT_LIST_HEAD(&dev_priv->render_ring.active_list);
4741 INIT_LIST_HEAD(&dev_priv->render_ring.request_list);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004742 if (HAS_BSD(dev)) {
4743 INIT_LIST_HEAD(&dev_priv->bsd_ring.active_list);
4744 INIT_LIST_HEAD(&dev_priv->bsd_ring.request_list);
4745 }
Daniel Vetter007cc8a2010-04-28 11:02:31 +02004746 for (i = 0; i < 16; i++)
4747 INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004748 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
4749 i915_gem_retire_work_handler);
Chris Wilson31169712009-09-14 16:50:28 +01004750 spin_lock(&shrink_list_lock);
4751 list_add(&dev_priv->mm.shrink_list, &shrink_list);
4752 spin_unlock(&shrink_list_lock);
4753
Dave Airlie94400122010-07-20 13:15:31 +10004754 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
4755 if (IS_GEN3(dev)) {
4756 u32 tmp = I915_READ(MI_ARB_STATE);
4757 if (!(tmp & MI_ARB_C3_LP_WRITE_ENABLE)) {
4758 /* arb state is a masked write, so set bit + bit in mask */
4759 tmp = MI_ARB_C3_LP_WRITE_ENABLE | (MI_ARB_C3_LP_WRITE_ENABLE << MI_ARB_MASK_SHIFT);
4760 I915_WRITE(MI_ARB_STATE, tmp);
4761 }
4762 }
4763
Jesse Barnesde151cf2008-11-12 10:03:55 -08004764 /* Old X drivers will take 0-2 for front, back, depth buffers */
Eric Anholtb397c832010-01-26 09:43:10 -08004765 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4766 dev_priv->fence_reg_start = 3;
Jesse Barnesde151cf2008-11-12 10:03:55 -08004767
Jesse Barnes0f973f22009-01-26 17:10:45 -08004768 if (IS_I965G(dev) || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08004769 dev_priv->num_fence_regs = 16;
4770 else
4771 dev_priv->num_fence_regs = 8;
4772
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004773 /* Initialize fence registers to zero */
4774 if (IS_I965G(dev)) {
4775 for (i = 0; i < 16; i++)
4776 I915_WRITE64(FENCE_REG_965_0 + (i * 8), 0);
4777 } else {
4778 for (i = 0; i < 8; i++)
4779 I915_WRITE(FENCE_REG_830_0 + (i * 4), 0);
4780 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4781 for (i = 0; i < 8; i++)
4782 I915_WRITE(FENCE_REG_945_8 + (i * 4), 0);
4783 }
Eric Anholt673a3942008-07-30 12:06:12 -07004784 i915_gem_detect_bit_6_swizzle(dev);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05004785 init_waitqueue_head(&dev_priv->pending_flip_queue);
Eric Anholt673a3942008-07-30 12:06:12 -07004786}
Dave Airlie71acb5e2008-12-30 20:31:46 +10004787
4788/*
4789 * Create a physically contiguous memory object for this object
4790 * e.g. for cursor + overlay regs
4791 */
4792int i915_gem_init_phys_object(struct drm_device *dev,
4793 int id, int size)
4794{
4795 drm_i915_private_t *dev_priv = dev->dev_private;
4796 struct drm_i915_gem_phys_object *phys_obj;
4797 int ret;
4798
4799 if (dev_priv->mm.phys_objs[id - 1] || !size)
4800 return 0;
4801
Eric Anholt9a298b22009-03-24 12:23:04 -07004802 phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004803 if (!phys_obj)
4804 return -ENOMEM;
4805
4806 phys_obj->id = id;
4807
Zhenyu Wange6be8d92010-01-05 11:25:05 +08004808 phys_obj->handle = drm_pci_alloc(dev, size, 0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004809 if (!phys_obj->handle) {
4810 ret = -ENOMEM;
4811 goto kfree_obj;
4812 }
4813#ifdef CONFIG_X86
4814 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4815#endif
4816
4817 dev_priv->mm.phys_objs[id - 1] = phys_obj;
4818
4819 return 0;
4820kfree_obj:
Eric Anholt9a298b22009-03-24 12:23:04 -07004821 kfree(phys_obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004822 return ret;
4823}
4824
4825void i915_gem_free_phys_object(struct drm_device *dev, int id)
4826{
4827 drm_i915_private_t *dev_priv = dev->dev_private;
4828 struct drm_i915_gem_phys_object *phys_obj;
4829
4830 if (!dev_priv->mm.phys_objs[id - 1])
4831 return;
4832
4833 phys_obj = dev_priv->mm.phys_objs[id - 1];
4834 if (phys_obj->cur_obj) {
4835 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
4836 }
4837
4838#ifdef CONFIG_X86
4839 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4840#endif
4841 drm_pci_free(dev, phys_obj->handle);
4842 kfree(phys_obj);
4843 dev_priv->mm.phys_objs[id - 1] = NULL;
4844}
4845
4846void i915_gem_free_all_phys_object(struct drm_device *dev)
4847{
4848 int i;
4849
Dave Airlie260883c2009-01-22 17:58:49 +10004850 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004851 i915_gem_free_phys_object(dev, i);
4852}
4853
4854void i915_gem_detach_phys_object(struct drm_device *dev,
4855 struct drm_gem_object *obj)
4856{
4857 struct drm_i915_gem_object *obj_priv;
4858 int i;
4859 int ret;
4860 int page_count;
4861
Daniel Vetter23010e42010-03-08 13:35:02 +01004862 obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004863 if (!obj_priv->phys_obj)
4864 return;
4865
Chris Wilson4bdadb92010-01-27 13:36:32 +00004866 ret = i915_gem_object_get_pages(obj, 0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004867 if (ret)
4868 goto out;
4869
4870 page_count = obj->size / PAGE_SIZE;
4871
4872 for (i = 0; i < page_count; i++) {
Eric Anholt856fa192009-03-19 14:10:50 -07004873 char *dst = kmap_atomic(obj_priv->pages[i], KM_USER0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004874 char *src = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4875
4876 memcpy(dst, src, PAGE_SIZE);
4877 kunmap_atomic(dst, KM_USER0);
4878 }
Eric Anholt856fa192009-03-19 14:10:50 -07004879 drm_clflush_pages(obj_priv->pages, page_count);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004880 drm_agp_chipset_flush(dev);
Chris Wilsond78b47b2009-06-17 21:52:49 +01004881
4882 i915_gem_object_put_pages(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004883out:
4884 obj_priv->phys_obj->cur_obj = NULL;
4885 obj_priv->phys_obj = NULL;
4886}
4887
4888int
4889i915_gem_attach_phys_object(struct drm_device *dev,
4890 struct drm_gem_object *obj, int id)
4891{
4892 drm_i915_private_t *dev_priv = dev->dev_private;
4893 struct drm_i915_gem_object *obj_priv;
4894 int ret = 0;
4895 int page_count;
4896 int i;
4897
4898 if (id > I915_MAX_PHYS_OBJECT)
4899 return -EINVAL;
4900
Daniel Vetter23010e42010-03-08 13:35:02 +01004901 obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004902
4903 if (obj_priv->phys_obj) {
4904 if (obj_priv->phys_obj->id == id)
4905 return 0;
4906 i915_gem_detach_phys_object(dev, obj);
4907 }
4908
4909
4910 /* create a new object */
4911 if (!dev_priv->mm.phys_objs[id - 1]) {
4912 ret = i915_gem_init_phys_object(dev, id,
4913 obj->size);
4914 if (ret) {
Linus Torvaldsaeb565d2009-01-26 10:01:53 -08004915 DRM_ERROR("failed to init phys object %d size: %zu\n", id, obj->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004916 goto out;
4917 }
4918 }
4919
4920 /* bind to the object */
4921 obj_priv->phys_obj = dev_priv->mm.phys_objs[id - 1];
4922 obj_priv->phys_obj->cur_obj = obj;
4923
Chris Wilson4bdadb92010-01-27 13:36:32 +00004924 ret = i915_gem_object_get_pages(obj, 0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004925 if (ret) {
4926 DRM_ERROR("failed to get page list\n");
4927 goto out;
4928 }
4929
4930 page_count = obj->size / PAGE_SIZE;
4931
4932 for (i = 0; i < page_count; i++) {
Eric Anholt856fa192009-03-19 14:10:50 -07004933 char *src = kmap_atomic(obj_priv->pages[i], KM_USER0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004934 char *dst = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4935
4936 memcpy(dst, src, PAGE_SIZE);
4937 kunmap_atomic(src, KM_USER0);
4938 }
4939
Chris Wilsond78b47b2009-06-17 21:52:49 +01004940 i915_gem_object_put_pages(obj);
4941
Dave Airlie71acb5e2008-12-30 20:31:46 +10004942 return 0;
4943out:
4944 return ret;
4945}
4946
4947static int
4948i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
4949 struct drm_i915_gem_pwrite *args,
4950 struct drm_file *file_priv)
4951{
Daniel Vetter23010e42010-03-08 13:35:02 +01004952 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004953 void *obj_addr;
4954 int ret;
4955 char __user *user_data;
4956
4957 user_data = (char __user *) (uintptr_t) args->data_ptr;
4958 obj_addr = obj_priv->phys_obj->handle->vaddr + args->offset;
4959
Zhao Yakui44d98a62009-10-09 11:39:40 +08004960 DRM_DEBUG_DRIVER("obj_addr %p, %lld\n", obj_addr, args->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004961 ret = copy_from_user(obj_addr, user_data, args->size);
4962 if (ret)
4963 return -EFAULT;
4964
4965 drm_agp_chipset_flush(dev);
4966 return 0;
4967}
Eric Anholtb9624422009-06-03 07:27:35 +00004968
4969void i915_gem_release(struct drm_device * dev, struct drm_file *file_priv)
4970{
4971 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
4972
4973 /* Clean up our request list when the client is going away, so that
4974 * later retire_requests won't dereference our soon-to-be-gone
4975 * file_priv.
4976 */
4977 mutex_lock(&dev->struct_mutex);
4978 while (!list_empty(&i915_file_priv->mm.request_list))
4979 list_del_init(i915_file_priv->mm.request_list.next);
4980 mutex_unlock(&dev->struct_mutex);
4981}
Chris Wilson31169712009-09-14 16:50:28 +01004982
Chris Wilson31169712009-09-14 16:50:28 +01004983static int
Chris Wilson1637ef42010-04-20 17:10:35 +01004984i915_gpu_is_active(struct drm_device *dev)
4985{
4986 drm_i915_private_t *dev_priv = dev->dev_private;
4987 int lists_empty;
4988
4989 spin_lock(&dev_priv->mm.active_list_lock);
4990 lists_empty = list_empty(&dev_priv->mm.flushing_list) &&
Zou Nan hai852835f2010-05-21 09:08:56 +08004991 list_empty(&dev_priv->render_ring.active_list);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004992 if (HAS_BSD(dev))
4993 lists_empty &= list_empty(&dev_priv->bsd_ring.active_list);
Chris Wilson1637ef42010-04-20 17:10:35 +01004994 spin_unlock(&dev_priv->mm.active_list_lock);
4995
4996 return !lists_empty;
4997}
4998
4999static int
Dave Chinner7f8275d2010-07-19 14:56:17 +10005000i915_gem_shrink(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask)
Chris Wilson31169712009-09-14 16:50:28 +01005001{
5002 drm_i915_private_t *dev_priv, *next_dev;
5003 struct drm_i915_gem_object *obj_priv, *next_obj;
5004 int cnt = 0;
5005 int would_deadlock = 1;
5006
5007 /* "fast-path" to count number of available objects */
5008 if (nr_to_scan == 0) {
5009 spin_lock(&shrink_list_lock);
5010 list_for_each_entry(dev_priv, &shrink_list, mm.shrink_list) {
5011 struct drm_device *dev = dev_priv->dev;
5012
5013 if (mutex_trylock(&dev->struct_mutex)) {
5014 list_for_each_entry(obj_priv,
5015 &dev_priv->mm.inactive_list,
5016 list)
5017 cnt++;
5018 mutex_unlock(&dev->struct_mutex);
5019 }
5020 }
5021 spin_unlock(&shrink_list_lock);
5022
5023 return (cnt / 100) * sysctl_vfs_cache_pressure;
5024 }
5025
5026 spin_lock(&shrink_list_lock);
5027
Chris Wilson1637ef42010-04-20 17:10:35 +01005028rescan:
Chris Wilson31169712009-09-14 16:50:28 +01005029 /* first scan for clean buffers */
5030 list_for_each_entry_safe(dev_priv, next_dev,
5031 &shrink_list, mm.shrink_list) {
5032 struct drm_device *dev = dev_priv->dev;
5033
5034 if (! mutex_trylock(&dev->struct_mutex))
5035 continue;
5036
5037 spin_unlock(&shrink_list_lock);
Zou Nan hai852835f2010-05-21 09:08:56 +08005038 i915_gem_retire_requests(dev, &dev_priv->render_ring);
Chris Wilson31169712009-09-14 16:50:28 +01005039
Zou Nan haid1b851f2010-05-21 09:08:57 +08005040 if (HAS_BSD(dev))
5041 i915_gem_retire_requests(dev, &dev_priv->bsd_ring);
5042
Chris Wilson31169712009-09-14 16:50:28 +01005043 list_for_each_entry_safe(obj_priv, next_obj,
5044 &dev_priv->mm.inactive_list,
5045 list) {
5046 if (i915_gem_object_is_purgeable(obj_priv)) {
Daniel Vettera8089e82010-04-09 19:05:09 +00005047 i915_gem_object_unbind(&obj_priv->base);
Chris Wilson31169712009-09-14 16:50:28 +01005048 if (--nr_to_scan <= 0)
5049 break;
5050 }
5051 }
5052
5053 spin_lock(&shrink_list_lock);
5054 mutex_unlock(&dev->struct_mutex);
5055
Chris Wilson963b4832009-09-20 23:03:54 +01005056 would_deadlock = 0;
5057
Chris Wilson31169712009-09-14 16:50:28 +01005058 if (nr_to_scan <= 0)
5059 break;
5060 }
5061
5062 /* second pass, evict/count anything still on the inactive list */
5063 list_for_each_entry_safe(dev_priv, next_dev,
5064 &shrink_list, mm.shrink_list) {
5065 struct drm_device *dev = dev_priv->dev;
5066
5067 if (! mutex_trylock(&dev->struct_mutex))
5068 continue;
5069
5070 spin_unlock(&shrink_list_lock);
5071
5072 list_for_each_entry_safe(obj_priv, next_obj,
5073 &dev_priv->mm.inactive_list,
5074 list) {
5075 if (nr_to_scan > 0) {
Daniel Vettera8089e82010-04-09 19:05:09 +00005076 i915_gem_object_unbind(&obj_priv->base);
Chris Wilson31169712009-09-14 16:50:28 +01005077 nr_to_scan--;
5078 } else
5079 cnt++;
5080 }
5081
5082 spin_lock(&shrink_list_lock);
5083 mutex_unlock(&dev->struct_mutex);
5084
5085 would_deadlock = 0;
5086 }
5087
Chris Wilson1637ef42010-04-20 17:10:35 +01005088 if (nr_to_scan) {
5089 int active = 0;
5090
5091 /*
5092 * We are desperate for pages, so as a last resort, wait
5093 * for the GPU to finish and discard whatever we can.
5094 * This has a dramatic impact to reduce the number of
5095 * OOM-killer events whilst running the GPU aggressively.
5096 */
5097 list_for_each_entry(dev_priv, &shrink_list, mm.shrink_list) {
5098 struct drm_device *dev = dev_priv->dev;
5099
5100 if (!mutex_trylock(&dev->struct_mutex))
5101 continue;
5102
5103 spin_unlock(&shrink_list_lock);
5104
5105 if (i915_gpu_is_active(dev)) {
5106 i915_gpu_idle(dev);
5107 active++;
5108 }
5109
5110 spin_lock(&shrink_list_lock);
5111 mutex_unlock(&dev->struct_mutex);
5112 }
5113
5114 if (active)
5115 goto rescan;
5116 }
5117
Chris Wilson31169712009-09-14 16:50:28 +01005118 spin_unlock(&shrink_list_lock);
5119
5120 if (would_deadlock)
5121 return -1;
5122 else if (cnt > 0)
5123 return (cnt / 100) * sysctl_vfs_cache_pressure;
5124 else
5125 return 0;
5126}
5127
5128static struct shrinker shrinker = {
5129 .shrink = i915_gem_shrink,
5130 .seeks = DEFAULT_SEEKS,
5131};
5132
5133__init void
5134i915_gem_shrinker_init(void)
5135{
5136 register_shrinker(&shrinker);
5137}
5138
5139__exit void
5140i915_gem_shrinker_exit(void)
5141{
5142 unregister_shrinker(&shrinker);
5143}