blob: 75e7b899e0336ff0a2672dae6378294b20b9e089 [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
Chris Wilson2dafb1e2010-06-07 14:03:05 +010038static int i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj);
Eric Anholte47c68e2008-11-14 13:35:19 -080039static 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);
Chris Wilsonbe726152010-07-23 23:18:50 +010056static void i915_gem_free_object_tail(struct drm_gem_object *obj);
Eric Anholt673a3942008-07-30 12:06:12 -070057
Chris Wilson31169712009-09-14 16:50:28 +010058static LIST_HEAD(shrink_list);
59static DEFINE_SPINLOCK(shrink_list_lock);
60
Jesse Barnes79e53942008-11-07 14:24:08 -080061int i915_gem_do_init(struct drm_device *dev, unsigned long start,
62 unsigned long end)
63{
64 drm_i915_private_t *dev_priv = dev->dev_private;
65
66 if (start >= end ||
67 (start & (PAGE_SIZE - 1)) != 0 ||
68 (end & (PAGE_SIZE - 1)) != 0) {
69 return -EINVAL;
70 }
71
72 drm_mm_init(&dev_priv->mm.gtt_space, start,
73 end - start);
74
75 dev->gtt_total = (uint32_t) (end - start);
76
77 return 0;
78}
Keith Packard6dbe2772008-10-14 21:41:13 -070079
Eric Anholt673a3942008-07-30 12:06:12 -070080int
81i915_gem_init_ioctl(struct drm_device *dev, void *data,
82 struct drm_file *file_priv)
83{
Eric Anholt673a3942008-07-30 12:06:12 -070084 struct drm_i915_gem_init *args = data;
Jesse Barnes79e53942008-11-07 14:24:08 -080085 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -070086
87 mutex_lock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080088 ret = i915_gem_do_init(dev, args->gtt_start, args->gtt_end);
Eric Anholt673a3942008-07-30 12:06:12 -070089 mutex_unlock(&dev->struct_mutex);
90
Jesse Barnes79e53942008-11-07 14:24:08 -080091 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -070092}
93
Eric Anholt5a125c32008-10-22 21:40:13 -070094int
95i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
96 struct drm_file *file_priv)
97{
Eric Anholt5a125c32008-10-22 21:40:13 -070098 struct drm_i915_gem_get_aperture *args = data;
Eric Anholt5a125c32008-10-22 21:40:13 -070099
100 if (!(dev->driver->driver_features & DRIVER_GEM))
101 return -ENODEV;
102
103 args->aper_size = dev->gtt_total;
Keith Packard2678d9d2008-11-20 22:54:54 -0800104 args->aper_available_size = (args->aper_size -
105 atomic_read(&dev->pin_memory));
Eric Anholt5a125c32008-10-22 21:40:13 -0700106
107 return 0;
108}
109
Eric Anholt673a3942008-07-30 12:06:12 -0700110
111/**
112 * Creates a new mm object and returns a handle to it.
113 */
114int
115i915_gem_create_ioctl(struct drm_device *dev, void *data,
116 struct drm_file *file_priv)
117{
118 struct drm_i915_gem_create *args = data;
119 struct drm_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300120 int ret;
121 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700122
123 args->size = roundup(args->size, PAGE_SIZE);
124
125 /* Allocate the new object */
Daniel Vetterac52bc52010-04-09 19:05:06 +0000126 obj = i915_gem_alloc_object(dev, args->size);
Eric Anholt673a3942008-07-30 12:06:12 -0700127 if (obj == NULL)
128 return -ENOMEM;
129
130 ret = drm_gem_handle_create(file_priv, obj, &handle);
Chris Wilson86f100b2010-07-24 21:03:49 +0100131 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700132 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
Chris Wilsonfca3ec02010-08-04 14:34:24 +0100499 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base, KM_USER0);
Keith Packard0839ccb2008-10-30 19:38:48 -0700500 unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
501 user_data, length);
Chris Wilsonfca3ec02010-08-04 14:34:24 +0100502 io_mapping_unmap_atomic(vaddr_atomic, KM_USER0);
Keith Packard0839ccb2008-10-30 19:38:48 -0700503 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;
Daniel Vetter23010e42010-03-08 13:35:02 +01001140 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001141 pgoff_t page_offset;
1142 unsigned long pfn;
1143 int ret = 0;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001144 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001145
1146 /* We don't use vmf->pgoff since that has the fake offset */
1147 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1148 PAGE_SHIFT;
1149
1150 /* Now bind it into the GTT if needed */
1151 mutex_lock(&dev->struct_mutex);
1152 if (!obj_priv->gtt_space) {
Chris Wilsone67b8ce2009-09-14 16:50:26 +01001153 ret = i915_gem_object_bind_to_gtt(obj, 0);
Chris Wilsonc7150892009-09-23 00:43:56 +01001154 if (ret)
1155 goto unlock;
Kristian Høgsberg07f4f3e2009-05-27 14:37:28 -04001156
Jesse Barnesde151cf2008-11-12 10:03:55 -08001157 ret = i915_gem_object_set_to_gtt_domain(obj, write);
Chris Wilsonc7150892009-09-23 00:43:56 +01001158 if (ret)
1159 goto unlock;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001160 }
1161
1162 /* Need a new fence register? */
Eric Anholta09ba7f2009-08-29 12:49:51 -07001163 if (obj_priv->tiling_mode != I915_TILING_NONE) {
Chris Wilson8c4b8c32009-06-17 22:08:52 +01001164 ret = i915_gem_object_get_fence_reg(obj);
Chris Wilsonc7150892009-09-23 00:43:56 +01001165 if (ret)
1166 goto unlock;
Eric Anholtd9ddcb92009-01-27 10:33:49 -08001167 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001168
1169 pfn = ((dev->agp->base + obj_priv->gtt_offset) >> PAGE_SHIFT) +
1170 page_offset;
1171
1172 /* Finally, remap it using the new GTT offset */
1173 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
Chris Wilsonc7150892009-09-23 00:43:56 +01001174unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001175 mutex_unlock(&dev->struct_mutex);
1176
1177 switch (ret) {
Chris Wilsonc7150892009-09-23 00:43:56 +01001178 case 0:
1179 case -ERESTARTSYS:
1180 return VM_FAULT_NOPAGE;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001181 case -ENOMEM:
1182 case -EAGAIN:
1183 return VM_FAULT_OOM;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001184 default:
Chris Wilsonc7150892009-09-23 00:43:56 +01001185 return VM_FAULT_SIGBUS;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001186 }
1187}
1188
1189/**
1190 * i915_gem_create_mmap_offset - create a fake mmap offset for an object
1191 * @obj: obj in question
1192 *
1193 * GEM memory mapping works by handing back to userspace a fake mmap offset
1194 * it can use in a subsequent mmap(2) call. The DRM core code then looks
1195 * up the object based on the offset and sets up the various memory mapping
1196 * structures.
1197 *
1198 * This routine allocates and attaches a fake offset for @obj.
1199 */
1200static int
1201i915_gem_create_mmap_offset(struct drm_gem_object *obj)
1202{
1203 struct drm_device *dev = obj->dev;
1204 struct drm_gem_mm *mm = dev->mm_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001205 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001206 struct drm_map_list *list;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +11001207 struct drm_local_map *map;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001208 int ret = 0;
1209
1210 /* Set the object up for mmap'ing */
1211 list = &obj->map_list;
Eric Anholt9a298b22009-03-24 12:23:04 -07001212 list->map = kzalloc(sizeof(struct drm_map_list), GFP_KERNEL);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001213 if (!list->map)
1214 return -ENOMEM;
1215
1216 map = list->map;
1217 map->type = _DRM_GEM;
1218 map->size = obj->size;
1219 map->handle = obj;
1220
1221 /* Get a DRM GEM mmap offset allocated... */
1222 list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
1223 obj->size / PAGE_SIZE, 0, 0);
1224 if (!list->file_offset_node) {
1225 DRM_ERROR("failed to allocate offset for bo %d\n", obj->name);
1226 ret = -ENOMEM;
1227 goto out_free_list;
1228 }
1229
1230 list->file_offset_node = drm_mm_get_block(list->file_offset_node,
1231 obj->size / PAGE_SIZE, 0);
1232 if (!list->file_offset_node) {
1233 ret = -ENOMEM;
1234 goto out_free_list;
1235 }
1236
1237 list->hash.key = list->file_offset_node->start;
1238 if (drm_ht_insert_item(&mm->offset_hash, &list->hash)) {
1239 DRM_ERROR("failed to add to map hash\n");
Chris Wilson5618ca62009-12-02 15:15:30 +00001240 ret = -ENOMEM;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001241 goto out_free_mm;
1242 }
1243
1244 /* By now we should be all set, any drm_mmap request on the offset
1245 * below will get to our mmap & fault handler */
1246 obj_priv->mmap_offset = ((uint64_t) list->hash.key) << PAGE_SHIFT;
1247
1248 return 0;
1249
1250out_free_mm:
1251 drm_mm_put_block(list->file_offset_node);
1252out_free_list:
Eric Anholt9a298b22009-03-24 12:23:04 -07001253 kfree(list->map);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001254
1255 return ret;
1256}
1257
Chris Wilson901782b2009-07-10 08:18:50 +01001258/**
1259 * i915_gem_release_mmap - remove physical page mappings
1260 * @obj: obj in question
1261 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001262 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01001263 * relinquish ownership of the pages back to the system.
1264 *
1265 * It is vital that we remove the page mapping if we have mapped a tiled
1266 * object through the GTT and then lose the fence register due to
1267 * resource pressure. Similarly if the object has been moved out of the
1268 * aperture, than pages mapped into userspace must be revoked. Removing the
1269 * mapping will then trigger a page fault on the next user access, allowing
1270 * fixup by i915_gem_fault().
1271 */
Eric Anholtd05ca302009-07-10 13:02:26 -07001272void
Chris Wilson901782b2009-07-10 08:18:50 +01001273i915_gem_release_mmap(struct drm_gem_object *obj)
1274{
1275 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001276 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson901782b2009-07-10 08:18:50 +01001277
1278 if (dev->dev_mapping)
1279 unmap_mapping_range(dev->dev_mapping,
1280 obj_priv->mmap_offset, obj->size, 1);
1281}
1282
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001283static void
1284i915_gem_free_mmap_offset(struct drm_gem_object *obj)
1285{
1286 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001287 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001288 struct drm_gem_mm *mm = dev->mm_private;
1289 struct drm_map_list *list;
1290
1291 list = &obj->map_list;
1292 drm_ht_remove_item(&mm->offset_hash, &list->hash);
1293
1294 if (list->file_offset_node) {
1295 drm_mm_put_block(list->file_offset_node);
1296 list->file_offset_node = NULL;
1297 }
1298
1299 if (list->map) {
Eric Anholt9a298b22009-03-24 12:23:04 -07001300 kfree(list->map);
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001301 list->map = NULL;
1302 }
1303
1304 obj_priv->mmap_offset = 0;
1305}
1306
Jesse Barnesde151cf2008-11-12 10:03:55 -08001307/**
1308 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1309 * @obj: object to check
1310 *
1311 * Return the required GTT alignment for an object, taking into account
1312 * potential fence register mapping if needed.
1313 */
1314static uint32_t
1315i915_gem_get_gtt_alignment(struct drm_gem_object *obj)
1316{
1317 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001318 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001319 int start, i;
1320
1321 /*
1322 * Minimum alignment is 4k (GTT page size), but might be greater
1323 * if a fence register is needed for the object.
1324 */
1325 if (IS_I965G(dev) || obj_priv->tiling_mode == I915_TILING_NONE)
1326 return 4096;
1327
1328 /*
1329 * Previous chips need to be aligned to the size of the smallest
1330 * fence register that can contain the object.
1331 */
1332 if (IS_I9XX(dev))
1333 start = 1024*1024;
1334 else
1335 start = 512*1024;
1336
1337 for (i = start; i < obj->size; i <<= 1)
1338 ;
1339
1340 return i;
1341}
1342
1343/**
1344 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1345 * @dev: DRM device
1346 * @data: GTT mapping ioctl data
1347 * @file_priv: GEM object info
1348 *
1349 * Simply returns the fake offset to userspace so it can mmap it.
1350 * The mmap call will end up in drm_gem_mmap(), which will set things
1351 * up so we can get faults in the handler above.
1352 *
1353 * The fault handler will take care of binding the object into the GTT
1354 * (since it may have been evicted to make room for something), allocating
1355 * a fence register, and mapping the appropriate aperture address into
1356 * userspace.
1357 */
1358int
1359i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
1360 struct drm_file *file_priv)
1361{
1362 struct drm_i915_gem_mmap_gtt *args = data;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001363 struct drm_gem_object *obj;
1364 struct drm_i915_gem_object *obj_priv;
1365 int ret;
1366
1367 if (!(dev->driver->driver_features & DRIVER_GEM))
1368 return -ENODEV;
1369
1370 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1371 if (obj == NULL)
1372 return -EBADF;
1373
1374 mutex_lock(&dev->struct_mutex);
1375
Daniel Vetter23010e42010-03-08 13:35:02 +01001376 obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001377
Chris Wilsonab182822009-09-22 18:46:17 +01001378 if (obj_priv->madv != I915_MADV_WILLNEED) {
1379 DRM_ERROR("Attempting to mmap a purgeable buffer\n");
1380 drm_gem_object_unreference(obj);
1381 mutex_unlock(&dev->struct_mutex);
1382 return -EINVAL;
1383 }
1384
1385
Jesse Barnesde151cf2008-11-12 10:03:55 -08001386 if (!obj_priv->mmap_offset) {
1387 ret = i915_gem_create_mmap_offset(obj);
Chris Wilson13af1062009-02-11 14:26:31 +00001388 if (ret) {
1389 drm_gem_object_unreference(obj);
1390 mutex_unlock(&dev->struct_mutex);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001391 return ret;
Chris Wilson13af1062009-02-11 14:26:31 +00001392 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001393 }
1394
1395 args->offset = obj_priv->mmap_offset;
1396
Jesse Barnesde151cf2008-11-12 10:03:55 -08001397 /*
1398 * Pull it into the GTT so that we have a page list (makes the
1399 * initial fault faster and any subsequent flushing possible).
1400 */
1401 if (!obj_priv->agp_mem) {
Chris Wilsone67b8ce2009-09-14 16:50:26 +01001402 ret = i915_gem_object_bind_to_gtt(obj, 0);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001403 if (ret) {
1404 drm_gem_object_unreference(obj);
1405 mutex_unlock(&dev->struct_mutex);
1406 return ret;
1407 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001408 }
1409
1410 drm_gem_object_unreference(obj);
1411 mutex_unlock(&dev->struct_mutex);
1412
1413 return 0;
1414}
1415
Ben Gamari6911a9b2009-04-02 11:24:54 -07001416void
Eric Anholt856fa192009-03-19 14:10:50 -07001417i915_gem_object_put_pages(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07001418{
Daniel Vetter23010e42010-03-08 13:35:02 +01001419 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001420 int page_count = obj->size / PAGE_SIZE;
1421 int i;
1422
Eric Anholt856fa192009-03-19 14:10:50 -07001423 BUG_ON(obj_priv->pages_refcount == 0);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001424 BUG_ON(obj_priv->madv == __I915_MADV_PURGED);
Eric Anholt856fa192009-03-19 14:10:50 -07001425
1426 if (--obj_priv->pages_refcount != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07001427 return;
1428
Eric Anholt280b7132009-03-12 16:56:27 -07001429 if (obj_priv->tiling_mode != I915_TILING_NONE)
1430 i915_gem_object_save_bit_17_swizzle(obj);
1431
Chris Wilson3ef94da2009-09-14 16:50:29 +01001432 if (obj_priv->madv == I915_MADV_DONTNEED)
Chris Wilson13a05fd2009-09-20 23:03:19 +01001433 obj_priv->dirty = 0;
Chris Wilson3ef94da2009-09-14 16:50:29 +01001434
1435 for (i = 0; i < page_count; i++) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01001436 if (obj_priv->dirty)
1437 set_page_dirty(obj_priv->pages[i]);
1438
1439 if (obj_priv->madv == I915_MADV_WILLNEED)
Eric Anholt856fa192009-03-19 14:10:50 -07001440 mark_page_accessed(obj_priv->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001441
1442 page_cache_release(obj_priv->pages[i]);
1443 }
Eric Anholt673a3942008-07-30 12:06:12 -07001444 obj_priv->dirty = 0;
1445
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07001446 drm_free_large(obj_priv->pages);
Eric Anholt856fa192009-03-19 14:10:50 -07001447 obj_priv->pages = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001448}
1449
1450static void
Zou Nan hai852835f2010-05-21 09:08:56 +08001451i915_gem_object_move_to_active(struct drm_gem_object *obj, uint32_t seqno,
1452 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001453{
1454 struct drm_device *dev = obj->dev;
1455 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001456 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Zou Nan hai852835f2010-05-21 09:08:56 +08001457 BUG_ON(ring == NULL);
1458 obj_priv->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001459
1460 /* Add a reference if we're newly entering the active list. */
1461 if (!obj_priv->active) {
1462 drm_gem_object_reference(obj);
1463 obj_priv->active = 1;
1464 }
1465 /* Move from whatever list we were on to the tail of execution. */
Carl Worth5e118f42009-03-20 11:54:25 -07001466 spin_lock(&dev_priv->mm.active_list_lock);
Zou Nan hai852835f2010-05-21 09:08:56 +08001467 list_move_tail(&obj_priv->list, &ring->active_list);
Carl Worth5e118f42009-03-20 11:54:25 -07001468 spin_unlock(&dev_priv->mm.active_list_lock);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001469 obj_priv->last_rendering_seqno = seqno;
Eric Anholt673a3942008-07-30 12:06:12 -07001470}
1471
Eric Anholtce44b0e2008-11-06 16:00:31 -08001472static void
1473i915_gem_object_move_to_flushing(struct drm_gem_object *obj)
1474{
1475 struct drm_device *dev = obj->dev;
1476 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001477 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001478
1479 BUG_ON(!obj_priv->active);
1480 list_move_tail(&obj_priv->list, &dev_priv->mm.flushing_list);
1481 obj_priv->last_rendering_seqno = 0;
1482}
Eric Anholt673a3942008-07-30 12:06:12 -07001483
Chris Wilson963b4832009-09-20 23:03:54 +01001484/* Immediately discard the backing storage */
1485static void
1486i915_gem_object_truncate(struct drm_gem_object *obj)
1487{
Daniel Vetter23010e42010-03-08 13:35:02 +01001488 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001489 struct inode *inode;
Chris Wilson963b4832009-09-20 23:03:54 +01001490
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001491 inode = obj->filp->f_path.dentry->d_inode;
1492 if (inode->i_op->truncate)
1493 inode->i_op->truncate (inode);
1494
1495 obj_priv->madv = __I915_MADV_PURGED;
Chris Wilson963b4832009-09-20 23:03:54 +01001496}
1497
1498static inline int
1499i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj_priv)
1500{
1501 return obj_priv->madv == I915_MADV_DONTNEED;
1502}
1503
Eric Anholt673a3942008-07-30 12:06:12 -07001504static void
1505i915_gem_object_move_to_inactive(struct drm_gem_object *obj)
1506{
1507 struct drm_device *dev = obj->dev;
1508 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001509 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001510
1511 i915_verify_inactive(dev, __FILE__, __LINE__);
1512 if (obj_priv->pin_count != 0)
1513 list_del_init(&obj_priv->list);
1514 else
1515 list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
1516
Daniel Vetter99fcb762010-02-07 16:20:18 +01001517 BUG_ON(!list_empty(&obj_priv->gpu_write_list));
1518
Eric Anholtce44b0e2008-11-06 16:00:31 -08001519 obj_priv->last_rendering_seqno = 0;
Zou Nan hai852835f2010-05-21 09:08:56 +08001520 obj_priv->ring = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001521 if (obj_priv->active) {
1522 obj_priv->active = 0;
1523 drm_gem_object_unreference(obj);
1524 }
1525 i915_verify_inactive(dev, __FILE__, __LINE__);
1526}
1527
Daniel Vetter63560392010-02-19 11:51:59 +01001528static void
1529i915_gem_process_flushing_list(struct drm_device *dev,
Zou Nan hai852835f2010-05-21 09:08:56 +08001530 uint32_t flush_domains, uint32_t seqno,
1531 struct intel_ring_buffer *ring)
Daniel Vetter63560392010-02-19 11:51:59 +01001532{
1533 drm_i915_private_t *dev_priv = dev->dev_private;
1534 struct drm_i915_gem_object *obj_priv, *next;
1535
1536 list_for_each_entry_safe(obj_priv, next,
1537 &dev_priv->mm.gpu_write_list,
1538 gpu_write_list) {
Daniel Vettera8089e82010-04-09 19:05:09 +00001539 struct drm_gem_object *obj = &obj_priv->base;
Daniel Vetter63560392010-02-19 11:51:59 +01001540
1541 if ((obj->write_domain & flush_domains) ==
Zou Nan hai852835f2010-05-21 09:08:56 +08001542 obj->write_domain &&
1543 obj_priv->ring->ring_flag == ring->ring_flag) {
Daniel Vetter63560392010-02-19 11:51:59 +01001544 uint32_t old_write_domain = obj->write_domain;
1545
1546 obj->write_domain = 0;
1547 list_del_init(&obj_priv->gpu_write_list);
Zou Nan hai852835f2010-05-21 09:08:56 +08001548 i915_gem_object_move_to_active(obj, seqno, ring);
Daniel Vetter63560392010-02-19 11:51:59 +01001549
1550 /* update the fence lru list */
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001551 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
1552 struct drm_i915_fence_reg *reg =
1553 &dev_priv->fence_regs[obj_priv->fence_reg];
1554 list_move_tail(&reg->lru_list,
Daniel Vetter63560392010-02-19 11:51:59 +01001555 &dev_priv->mm.fence_list);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001556 }
Daniel Vetter63560392010-02-19 11:51:59 +01001557
1558 trace_i915_gem_object_change_domain(obj,
1559 obj->read_domains,
1560 old_write_domain);
1561 }
1562 }
1563}
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001564
Daniel Vetter5a5a0c62009-09-15 22:57:36 +02001565uint32_t
Eric Anholtb9624422009-06-03 07:27:35 +00001566i915_add_request(struct drm_device *dev, struct drm_file *file_priv,
Zou Nan hai852835f2010-05-21 09:08:56 +08001567 uint32_t flush_domains, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001568{
1569 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtb9624422009-06-03 07:27:35 +00001570 struct drm_i915_file_private *i915_file_priv = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001571 struct drm_i915_gem_request *request;
1572 uint32_t seqno;
1573 int was_empty;
Eric Anholt673a3942008-07-30 12:06:12 -07001574
Eric Anholtb9624422009-06-03 07:27:35 +00001575 if (file_priv != NULL)
1576 i915_file_priv = file_priv->driver_priv;
1577
Eric Anholt9a298b22009-03-24 12:23:04 -07001578 request = kzalloc(sizeof(*request), GFP_KERNEL);
Eric Anholt673a3942008-07-30 12:06:12 -07001579 if (request == NULL)
1580 return 0;
1581
Zou Nan hai852835f2010-05-21 09:08:56 +08001582 seqno = ring->add_request(dev, ring, file_priv, flush_domains);
Eric Anholt673a3942008-07-30 12:06:12 -07001583
1584 request->seqno = seqno;
Zou Nan hai852835f2010-05-21 09:08:56 +08001585 request->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001586 request->emitted_jiffies = jiffies;
Zou Nan hai852835f2010-05-21 09:08:56 +08001587 was_empty = list_empty(&ring->request_list);
1588 list_add_tail(&request->list, &ring->request_list);
1589
Eric Anholtb9624422009-06-03 07:27:35 +00001590 if (i915_file_priv) {
1591 list_add_tail(&request->client_list,
1592 &i915_file_priv->mm.request_list);
1593 } else {
1594 INIT_LIST_HEAD(&request->client_list);
1595 }
Eric Anholt673a3942008-07-30 12:06:12 -07001596
Eric Anholtce44b0e2008-11-06 16:00:31 -08001597 /* Associate any objects on the flushing list matching the write
1598 * domain we're flushing with our flush.
1599 */
Daniel Vetter63560392010-02-19 11:51:59 +01001600 if (flush_domains != 0)
Zou Nan hai852835f2010-05-21 09:08:56 +08001601 i915_gem_process_flushing_list(dev, flush_domains, seqno, ring);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001602
Ben Gamarif65d9422009-09-14 17:48:44 -04001603 if (!dev_priv->mm.suspended) {
1604 mod_timer(&dev_priv->hangcheck_timer, jiffies + DRM_I915_HANGCHECK_PERIOD);
1605 if (was_empty)
1606 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
1607 }
Eric Anholt673a3942008-07-30 12:06:12 -07001608 return seqno;
1609}
1610
1611/**
1612 * Command execution barrier
1613 *
1614 * Ensures that all commands in the ring are finished
1615 * before signalling the CPU
1616 */
Eric Anholt3043c602008-10-02 12:24:47 -07001617static uint32_t
Zou Nan hai852835f2010-05-21 09:08:56 +08001618i915_retire_commands(struct drm_device *dev, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001619{
Eric Anholt673a3942008-07-30 12:06:12 -07001620 uint32_t flush_domains = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001621
1622 /* The sampler always gets flushed on i965 (sigh) */
1623 if (IS_I965G(dev))
1624 flush_domains |= I915_GEM_DOMAIN_SAMPLER;
Zou Nan hai852835f2010-05-21 09:08:56 +08001625
1626 ring->flush(dev, ring,
1627 I915_GEM_DOMAIN_COMMAND, flush_domains);
Eric Anholt673a3942008-07-30 12:06:12 -07001628 return flush_domains;
1629}
1630
1631/**
1632 * Moves buffers associated only with the given active seqno from the active
1633 * to inactive list, potentially freeing them.
1634 */
1635static void
1636i915_gem_retire_request(struct drm_device *dev,
1637 struct drm_i915_gem_request *request)
1638{
1639 drm_i915_private_t *dev_priv = dev->dev_private;
1640
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001641 trace_i915_gem_request_retire(dev, request->seqno);
1642
Eric Anholt673a3942008-07-30 12:06:12 -07001643 /* Move any buffers on the active list that are no longer referenced
1644 * by the ringbuffer to the flushing/inactive lists as appropriate.
1645 */
Carl Worth5e118f42009-03-20 11:54:25 -07001646 spin_lock(&dev_priv->mm.active_list_lock);
Zou Nan hai852835f2010-05-21 09:08:56 +08001647 while (!list_empty(&request->ring->active_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001648 struct drm_gem_object *obj;
1649 struct drm_i915_gem_object *obj_priv;
1650
Zou Nan hai852835f2010-05-21 09:08:56 +08001651 obj_priv = list_first_entry(&request->ring->active_list,
Eric Anholt673a3942008-07-30 12:06:12 -07001652 struct drm_i915_gem_object,
1653 list);
Daniel Vettera8089e82010-04-09 19:05:09 +00001654 obj = &obj_priv->base;
Eric Anholt673a3942008-07-30 12:06:12 -07001655
1656 /* If the seqno being retired doesn't match the oldest in the
1657 * list, then the oldest in the list must still be newer than
1658 * this seqno.
1659 */
1660 if (obj_priv->last_rendering_seqno != request->seqno)
Carl Worth5e118f42009-03-20 11:54:25 -07001661 goto out;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001662
Eric Anholt673a3942008-07-30 12:06:12 -07001663#if WATCH_LRU
1664 DRM_INFO("%s: retire %d moves to inactive list %p\n",
1665 __func__, request->seqno, obj);
1666#endif
1667
Eric Anholtce44b0e2008-11-06 16:00:31 -08001668 if (obj->write_domain != 0)
1669 i915_gem_object_move_to_flushing(obj);
Shaohua Li68c84342009-04-08 10:58:23 +08001670 else {
1671 /* Take a reference on the object so it won't be
1672 * freed while the spinlock is held. The list
1673 * protection for this spinlock is safe when breaking
1674 * the lock like this since the next thing we do
1675 * is just get the head of the list again.
1676 */
1677 drm_gem_object_reference(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001678 i915_gem_object_move_to_inactive(obj);
Shaohua Li68c84342009-04-08 10:58:23 +08001679 spin_unlock(&dev_priv->mm.active_list_lock);
1680 drm_gem_object_unreference(obj);
1681 spin_lock(&dev_priv->mm.active_list_lock);
1682 }
Eric Anholt673a3942008-07-30 12:06:12 -07001683 }
Carl Worth5e118f42009-03-20 11:54:25 -07001684out:
1685 spin_unlock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001686}
1687
1688/**
1689 * Returns true if seq1 is later than seq2.
1690 */
Ben Gamari22be1722009-09-14 17:48:43 -04001691bool
Eric Anholt673a3942008-07-30 12:06:12 -07001692i915_seqno_passed(uint32_t seq1, uint32_t seq2)
1693{
1694 return (int32_t)(seq1 - seq2) >= 0;
1695}
1696
1697uint32_t
Zou Nan hai852835f2010-05-21 09:08:56 +08001698i915_get_gem_seqno(struct drm_device *dev,
Zou Nan haid1b851f2010-05-21 09:08:57 +08001699 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001700{
Zou Nan hai852835f2010-05-21 09:08:56 +08001701 return ring->get_gem_seqno(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07001702}
1703
1704/**
1705 * This function clears the request list as sequence numbers are passed.
1706 */
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001707static void
1708i915_gem_retire_requests_ring(struct drm_device *dev,
1709 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001710{
1711 drm_i915_private_t *dev_priv = dev->dev_private;
1712 uint32_t seqno;
1713
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001714 if (!ring->status_page.page_addr
Zou Nan hai852835f2010-05-21 09:08:56 +08001715 || list_empty(&ring->request_list))
Karsten Wiese6c0594a2009-02-23 15:07:57 +01001716 return;
1717
Zou Nan hai852835f2010-05-21 09:08:56 +08001718 seqno = i915_get_gem_seqno(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07001719
Zou Nan hai852835f2010-05-21 09:08:56 +08001720 while (!list_empty(&ring->request_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001721 struct drm_i915_gem_request *request;
1722 uint32_t retiring_seqno;
1723
Zou Nan hai852835f2010-05-21 09:08:56 +08001724 request = list_first_entry(&ring->request_list,
Eric Anholt673a3942008-07-30 12:06:12 -07001725 struct drm_i915_gem_request,
1726 list);
1727 retiring_seqno = request->seqno;
1728
1729 if (i915_seqno_passed(seqno, retiring_seqno) ||
Ben Gamariba1234d2009-09-14 17:48:47 -04001730 atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001731 i915_gem_retire_request(dev, request);
1732
1733 list_del(&request->list);
Eric Anholtb9624422009-06-03 07:27:35 +00001734 list_del(&request->client_list);
Eric Anholt9a298b22009-03-24 12:23:04 -07001735 kfree(request);
Eric Anholt673a3942008-07-30 12:06:12 -07001736 } else
1737 break;
1738 }
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001739
1740 if (unlikely (dev_priv->trace_irq_seqno &&
1741 i915_seqno_passed(dev_priv->trace_irq_seqno, seqno))) {
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001742
1743 ring->user_irq_put(dev, ring);
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001744 dev_priv->trace_irq_seqno = 0;
1745 }
Eric Anholt673a3942008-07-30 12:06:12 -07001746}
1747
1748void
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001749i915_gem_retire_requests(struct drm_device *dev)
1750{
1751 drm_i915_private_t *dev_priv = dev->dev_private;
1752
Chris Wilsonbe726152010-07-23 23:18:50 +01001753 if (!list_empty(&dev_priv->mm.deferred_free_list)) {
1754 struct drm_i915_gem_object *obj_priv, *tmp;
1755
1756 /* We must be careful that during unbind() we do not
1757 * accidentally infinitely recurse into retire requests.
1758 * Currently:
1759 * retire -> free -> unbind -> wait -> retire_ring
1760 */
1761 list_for_each_entry_safe(obj_priv, tmp,
1762 &dev_priv->mm.deferred_free_list,
1763 list)
1764 i915_gem_free_object_tail(&obj_priv->base);
1765 }
1766
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001767 i915_gem_retire_requests_ring(dev, &dev_priv->render_ring);
1768 if (HAS_BSD(dev))
1769 i915_gem_retire_requests_ring(dev, &dev_priv->bsd_ring);
1770}
1771
1772void
Eric Anholt673a3942008-07-30 12:06:12 -07001773i915_gem_retire_work_handler(struct work_struct *work)
1774{
1775 drm_i915_private_t *dev_priv;
1776 struct drm_device *dev;
1777
1778 dev_priv = container_of(work, drm_i915_private_t,
1779 mm.retire_work.work);
1780 dev = dev_priv->dev;
1781
1782 mutex_lock(&dev->struct_mutex);
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001783 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08001784
Keith Packard6dbe2772008-10-14 21:41:13 -07001785 if (!dev_priv->mm.suspended &&
Zou Nan haid1b851f2010-05-21 09:08:57 +08001786 (!list_empty(&dev_priv->render_ring.request_list) ||
1787 (HAS_BSD(dev) &&
1788 !list_empty(&dev_priv->bsd_ring.request_list))))
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001789 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
Eric Anholt673a3942008-07-30 12:06:12 -07001790 mutex_unlock(&dev->struct_mutex);
1791}
1792
Daniel Vetter5a5a0c62009-09-15 22:57:36 +02001793int
Zou Nan hai852835f2010-05-21 09:08:56 +08001794i915_do_wait_request(struct drm_device *dev, uint32_t seqno,
1795 int interruptible, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001796{
1797 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001798 u32 ier;
Eric Anholt673a3942008-07-30 12:06:12 -07001799 int ret = 0;
1800
1801 BUG_ON(seqno == 0);
1802
Ben Gamariba1234d2009-09-14 17:48:47 -04001803 if (atomic_read(&dev_priv->mm.wedged))
Ben Gamariffed1d02009-09-14 17:48:41 -04001804 return -EIO;
1805
Zou Nan hai852835f2010-05-21 09:08:56 +08001806 if (!i915_seqno_passed(ring->get_gem_seqno(dev, ring), seqno)) {
Eric Anholtbad720f2009-10-22 16:11:14 -07001807 if (HAS_PCH_SPLIT(dev))
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001808 ier = I915_READ(DEIER) | I915_READ(GTIER);
1809 else
1810 ier = I915_READ(IER);
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001811 if (!ier) {
1812 DRM_ERROR("something (likely vbetool) disabled "
1813 "interrupts, re-enabling\n");
1814 i915_driver_irq_preinstall(dev);
1815 i915_driver_irq_postinstall(dev);
1816 }
1817
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001818 trace_i915_gem_request_wait_begin(dev, seqno);
1819
Zou Nan hai852835f2010-05-21 09:08:56 +08001820 ring->waiting_gem_seqno = seqno;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001821 ring->user_irq_get(dev, ring);
Daniel Vetter48764bf2009-09-15 22:57:32 +02001822 if (interruptible)
Zou Nan hai852835f2010-05-21 09:08:56 +08001823 ret = wait_event_interruptible(ring->irq_queue,
1824 i915_seqno_passed(
1825 ring->get_gem_seqno(dev, ring), seqno)
1826 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02001827 else
Zou Nan hai852835f2010-05-21 09:08:56 +08001828 wait_event(ring->irq_queue,
1829 i915_seqno_passed(
1830 ring->get_gem_seqno(dev, ring), seqno)
1831 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02001832
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001833 ring->user_irq_put(dev, ring);
Zou Nan hai852835f2010-05-21 09:08:56 +08001834 ring->waiting_gem_seqno = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001835
1836 trace_i915_gem_request_wait_end(dev, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07001837 }
Ben Gamariba1234d2009-09-14 17:48:47 -04001838 if (atomic_read(&dev_priv->mm.wedged))
Eric Anholt673a3942008-07-30 12:06:12 -07001839 ret = -EIO;
1840
1841 if (ret && ret != -ERESTARTSYS)
1842 DRM_ERROR("%s returns %d (awaiting %d at %d)\n",
Zou Nan hai852835f2010-05-21 09:08:56 +08001843 __func__, ret, seqno, ring->get_gem_seqno(dev, ring));
Eric Anholt673a3942008-07-30 12:06:12 -07001844
1845 /* Directly dispatch request retiring. While we have the work queue
1846 * to handle this, the waiter on a request often wants an associated
1847 * buffer to have made it to the inactive list, and we would need
1848 * a separate wait queue to handle that.
1849 */
1850 if (ret == 0)
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001851 i915_gem_retire_requests_ring(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07001852
1853 return ret;
1854}
1855
Daniel Vetter48764bf2009-09-15 22:57:32 +02001856/**
1857 * Waits for a sequence number to be signaled, and cleans up the
1858 * request and object lists appropriately for that event.
1859 */
1860static int
Zou Nan hai852835f2010-05-21 09:08:56 +08001861i915_wait_request(struct drm_device *dev, uint32_t seqno,
1862 struct intel_ring_buffer *ring)
Daniel Vetter48764bf2009-09-15 22:57:32 +02001863{
Zou Nan hai852835f2010-05-21 09:08:56 +08001864 return i915_do_wait_request(dev, seqno, 1, ring);
Daniel Vetter48764bf2009-09-15 22:57:32 +02001865}
1866
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001867static void
1868i915_gem_flush(struct drm_device *dev,
1869 uint32_t invalidate_domains,
1870 uint32_t flush_domains)
1871{
1872 drm_i915_private_t *dev_priv = dev->dev_private;
1873 if (flush_domains & I915_GEM_DOMAIN_CPU)
1874 drm_agp_chipset_flush(dev);
1875 dev_priv->render_ring.flush(dev, &dev_priv->render_ring,
1876 invalidate_domains,
1877 flush_domains);
Zou Nan haid1b851f2010-05-21 09:08:57 +08001878
1879 if (HAS_BSD(dev))
1880 dev_priv->bsd_ring.flush(dev, &dev_priv->bsd_ring,
1881 invalidate_domains,
1882 flush_domains);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001883}
1884
Zou Nan hai852835f2010-05-21 09:08:56 +08001885static void
1886i915_gem_flush_ring(struct drm_device *dev,
1887 uint32_t invalidate_domains,
1888 uint32_t flush_domains,
1889 struct intel_ring_buffer *ring)
1890{
1891 if (flush_domains & I915_GEM_DOMAIN_CPU)
1892 drm_agp_chipset_flush(dev);
1893 ring->flush(dev, ring,
1894 invalidate_domains,
1895 flush_domains);
1896}
1897
Eric Anholt673a3942008-07-30 12:06:12 -07001898/**
1899 * Ensures that all rendering to the object has completed and the object is
1900 * safe to unbind from the GTT or access from the CPU.
1901 */
1902static int
1903i915_gem_object_wait_rendering(struct drm_gem_object *obj)
1904{
1905 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001906 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001907 int ret;
1908
Eric Anholte47c68e2008-11-14 13:35:19 -08001909 /* This function only exists to support waiting for existing rendering,
1910 * not for emitting required flushes.
Eric Anholt673a3942008-07-30 12:06:12 -07001911 */
Eric Anholte47c68e2008-11-14 13:35:19 -08001912 BUG_ON((obj->write_domain & I915_GEM_GPU_DOMAINS) != 0);
Eric Anholt673a3942008-07-30 12:06:12 -07001913
1914 /* If there is rendering queued on the buffer being evicted, wait for
1915 * it.
1916 */
1917 if (obj_priv->active) {
1918#if WATCH_BUF
1919 DRM_INFO("%s: object %p wait for seqno %08x\n",
1920 __func__, obj, obj_priv->last_rendering_seqno);
1921#endif
Zou Nan hai852835f2010-05-21 09:08:56 +08001922 ret = i915_wait_request(dev,
1923 obj_priv->last_rendering_seqno, obj_priv->ring);
Eric Anholt673a3942008-07-30 12:06:12 -07001924 if (ret != 0)
1925 return ret;
1926 }
1927
1928 return 0;
1929}
1930
1931/**
1932 * Unbinds an object from the GTT aperture.
1933 */
Jesse Barnes0f973f22009-01-26 17:10:45 -08001934int
Eric Anholt673a3942008-07-30 12:06:12 -07001935i915_gem_object_unbind(struct drm_gem_object *obj)
1936{
1937 struct drm_device *dev = obj->dev;
Daniel Vetter4a87b8c2010-02-19 11:51:57 +01001938 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001939 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001940 int ret = 0;
1941
1942#if WATCH_BUF
1943 DRM_INFO("%s:%d %p\n", __func__, __LINE__, obj);
1944 DRM_INFO("gtt_space %p\n", obj_priv->gtt_space);
1945#endif
1946 if (obj_priv->gtt_space == NULL)
1947 return 0;
1948
1949 if (obj_priv->pin_count != 0) {
1950 DRM_ERROR("Attempting to unbind pinned buffer\n");
1951 return -EINVAL;
1952 }
1953
Eric Anholt5323fd02009-09-09 11:50:45 -07001954 /* blow away mappings if mapped through GTT */
1955 i915_gem_release_mmap(obj);
1956
Eric Anholt673a3942008-07-30 12:06:12 -07001957 /* Move the object to the CPU domain to ensure that
1958 * any possible CPU writes while it's not in the GTT
1959 * are flushed when we go to remap it. This will
1960 * also ensure that all pending GPU writes are finished
1961 * before we unbind.
1962 */
Eric Anholte47c68e2008-11-14 13:35:19 -08001963 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Chris Wilson8dc17752010-07-23 23:18:51 +01001964 if (ret == -ERESTARTSYS)
Eric Anholt673a3942008-07-30 12:06:12 -07001965 return ret;
Chris Wilson8dc17752010-07-23 23:18:51 +01001966 /* Continue on if we fail due to EIO, the GPU is hung so we
1967 * should be safe and we need to cleanup or else we might
1968 * cause memory corruption through use-after-free.
1969 */
Eric Anholt673a3942008-07-30 12:06:12 -07001970
Eric Anholt5323fd02009-09-09 11:50:45 -07001971 BUG_ON(obj_priv->active);
1972
Daniel Vetter96b47b62009-12-15 17:50:00 +01001973 /* release the fence reg _after_ flushing */
1974 if (obj_priv->fence_reg != I915_FENCE_REG_NONE)
1975 i915_gem_clear_fence_reg(obj);
1976
Eric Anholt673a3942008-07-30 12:06:12 -07001977 if (obj_priv->agp_mem != NULL) {
1978 drm_unbind_agp(obj_priv->agp_mem);
1979 drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE);
1980 obj_priv->agp_mem = NULL;
1981 }
1982
Eric Anholt856fa192009-03-19 14:10:50 -07001983 i915_gem_object_put_pages(obj);
Chris Wilsona32808c2009-09-20 21:29:47 +01001984 BUG_ON(obj_priv->pages_refcount);
Eric Anholt673a3942008-07-30 12:06:12 -07001985
1986 if (obj_priv->gtt_space) {
1987 atomic_dec(&dev->gtt_count);
1988 atomic_sub(obj->size, &dev->gtt_memory);
1989
1990 drm_mm_put_block(obj_priv->gtt_space);
1991 obj_priv->gtt_space = NULL;
1992 }
1993
1994 /* Remove ourselves from the LRU list if present. */
Daniel Vetter4a87b8c2010-02-19 11:51:57 +01001995 spin_lock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001996 if (!list_empty(&obj_priv->list))
1997 list_del_init(&obj_priv->list);
Daniel Vetter4a87b8c2010-02-19 11:51:57 +01001998 spin_unlock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001999
Chris Wilson963b4832009-09-20 23:03:54 +01002000 if (i915_gem_object_is_purgeable(obj_priv))
2001 i915_gem_object_truncate(obj);
2002
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002003 trace_i915_gem_object_unbind(obj);
2004
Chris Wilson8dc17752010-07-23 23:18:51 +01002005 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002006}
2007
Chris Wilson07f73f62009-09-14 16:50:30 +01002008static struct drm_gem_object *
2009i915_gem_find_inactive_object(struct drm_device *dev, int min_size)
2010{
2011 drm_i915_private_t *dev_priv = dev->dev_private;
2012 struct drm_i915_gem_object *obj_priv;
2013 struct drm_gem_object *best = NULL;
2014 struct drm_gem_object *first = NULL;
2015
2016 /* Try to find the smallest clean object */
2017 list_for_each_entry(obj_priv, &dev_priv->mm.inactive_list, list) {
Daniel Vettera8089e82010-04-09 19:05:09 +00002018 struct drm_gem_object *obj = &obj_priv->base;
Chris Wilson07f73f62009-09-14 16:50:30 +01002019 if (obj->size >= min_size) {
Chris Wilson963b4832009-09-20 23:03:54 +01002020 if ((!obj_priv->dirty ||
2021 i915_gem_object_is_purgeable(obj_priv)) &&
Chris Wilson07f73f62009-09-14 16:50:30 +01002022 (!best || obj->size < best->size)) {
2023 best = obj;
2024 if (best->size == min_size)
2025 return best;
2026 }
2027 if (!first)
2028 first = obj;
2029 }
2030 }
2031
2032 return best ? best : first;
2033}
2034
Eric Anholt673a3942008-07-30 12:06:12 -07002035static int
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002036i915_gpu_idle(struct drm_device *dev)
2037{
2038 drm_i915_private_t *dev_priv = dev->dev_private;
2039 bool lists_empty;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002040 uint32_t seqno1, seqno2;
Zou Nan hai852835f2010-05-21 09:08:56 +08002041 int ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002042
2043 spin_lock(&dev_priv->mm.active_list_lock);
Zou Nan haid1b851f2010-05-21 09:08:57 +08002044 lists_empty = (list_empty(&dev_priv->mm.flushing_list) &&
2045 list_empty(&dev_priv->render_ring.active_list) &&
2046 (!HAS_BSD(dev) ||
2047 list_empty(&dev_priv->bsd_ring.active_list)));
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002048 spin_unlock(&dev_priv->mm.active_list_lock);
2049
2050 if (lists_empty)
2051 return 0;
2052
2053 /* Flush everything onto the inactive list. */
2054 i915_gem_flush(dev, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
Zou Nan haid1b851f2010-05-21 09:08:57 +08002055 seqno1 = i915_add_request(dev, NULL, I915_GEM_GPU_DOMAINS,
Zou Nan hai852835f2010-05-21 09:08:56 +08002056 &dev_priv->render_ring);
Zou Nan haid1b851f2010-05-21 09:08:57 +08002057 if (seqno1 == 0)
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002058 return -ENOMEM;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002059 ret = i915_wait_request(dev, seqno1, &dev_priv->render_ring);
2060
2061 if (HAS_BSD(dev)) {
2062 seqno2 = i915_add_request(dev, NULL, I915_GEM_GPU_DOMAINS,
2063 &dev_priv->bsd_ring);
2064 if (seqno2 == 0)
2065 return -ENOMEM;
2066
2067 ret = i915_wait_request(dev, seqno2, &dev_priv->bsd_ring);
2068 if (ret)
2069 return ret;
2070 }
2071
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002072
Zou Nan hai852835f2010-05-21 09:08:56 +08002073 return ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002074}
2075
2076static int
Chris Wilson07f73f62009-09-14 16:50:30 +01002077i915_gem_evict_everything(struct drm_device *dev)
2078{
2079 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson07f73f62009-09-14 16:50:30 +01002080 int ret;
2081 bool lists_empty;
2082
Chris Wilson07f73f62009-09-14 16:50:30 +01002083 spin_lock(&dev_priv->mm.active_list_lock);
2084 lists_empty = (list_empty(&dev_priv->mm.inactive_list) &&
2085 list_empty(&dev_priv->mm.flushing_list) &&
Zou Nan haid1b851f2010-05-21 09:08:57 +08002086 list_empty(&dev_priv->render_ring.active_list) &&
2087 (!HAS_BSD(dev)
2088 || list_empty(&dev_priv->bsd_ring.active_list)));
Chris Wilson07f73f62009-09-14 16:50:30 +01002089 spin_unlock(&dev_priv->mm.active_list_lock);
2090
Chris Wilson97311292009-09-21 00:22:34 +01002091 if (lists_empty)
Chris Wilson07f73f62009-09-14 16:50:30 +01002092 return -ENOSPC;
Chris Wilson07f73f62009-09-14 16:50:30 +01002093
2094 /* Flush everything (on to the inactive lists) and evict */
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002095 ret = i915_gpu_idle(dev);
Chris Wilson07f73f62009-09-14 16:50:30 +01002096 if (ret)
2097 return ret;
2098
Daniel Vetter99fcb762010-02-07 16:20:18 +01002099 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
2100
Chris Wilsonab5ee572009-09-20 19:25:47 +01002101 ret = i915_gem_evict_from_inactive_list(dev);
Chris Wilson07f73f62009-09-14 16:50:30 +01002102 if (ret)
2103 return ret;
2104
2105 spin_lock(&dev_priv->mm.active_list_lock);
2106 lists_empty = (list_empty(&dev_priv->mm.inactive_list) &&
2107 list_empty(&dev_priv->mm.flushing_list) &&
Zou Nan haid1b851f2010-05-21 09:08:57 +08002108 list_empty(&dev_priv->render_ring.active_list) &&
2109 (!HAS_BSD(dev)
2110 || list_empty(&dev_priv->bsd_ring.active_list)));
Chris Wilson07f73f62009-09-14 16:50:30 +01002111 spin_unlock(&dev_priv->mm.active_list_lock);
2112 BUG_ON(!lists_empty);
2113
Eric Anholt673a3942008-07-30 12:06:12 -07002114 return 0;
2115}
2116
2117static int
Chris Wilson07f73f62009-09-14 16:50:30 +01002118i915_gem_evict_something(struct drm_device *dev, int min_size)
Eric Anholt673a3942008-07-30 12:06:12 -07002119{
2120 drm_i915_private_t *dev_priv = dev->dev_private;
2121 struct drm_gem_object *obj;
Chris Wilson07f73f62009-09-14 16:50:30 +01002122 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002123
Zou Nan hai852835f2010-05-21 09:08:56 +08002124 struct intel_ring_buffer *render_ring = &dev_priv->render_ring;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002125 struct intel_ring_buffer *bsd_ring = &dev_priv->bsd_ring;
Eric Anholt673a3942008-07-30 12:06:12 -07002126 for (;;) {
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002127 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08002128
Eric Anholt673a3942008-07-30 12:06:12 -07002129 /* If there's an inactive buffer available now, grab it
2130 * and be done.
2131 */
Chris Wilson07f73f62009-09-14 16:50:30 +01002132 obj = i915_gem_find_inactive_object(dev, min_size);
2133 if (obj) {
2134 struct drm_i915_gem_object *obj_priv;
2135
Eric Anholt673a3942008-07-30 12:06:12 -07002136#if WATCH_LRU
2137 DRM_INFO("%s: evicting %p\n", __func__, obj);
2138#endif
Daniel Vetter23010e42010-03-08 13:35:02 +01002139 obj_priv = to_intel_bo(obj);
Chris Wilson07f73f62009-09-14 16:50:30 +01002140 BUG_ON(obj_priv->pin_count != 0);
Eric Anholt673a3942008-07-30 12:06:12 -07002141 BUG_ON(obj_priv->active);
2142
2143 /* Wait on the rendering and unbind the buffer. */
Chris Wilson07f73f62009-09-14 16:50:30 +01002144 return i915_gem_object_unbind(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002145 }
2146
2147 /* If we didn't get anything, but the ring is still processing
Chris Wilson07f73f62009-09-14 16:50:30 +01002148 * things, wait for the next to finish and hopefully leave us
2149 * a buffer to evict.
Eric Anholt673a3942008-07-30 12:06:12 -07002150 */
Zou Nan hai852835f2010-05-21 09:08:56 +08002151 if (!list_empty(&render_ring->request_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002152 struct drm_i915_gem_request *request;
2153
Zou Nan hai852835f2010-05-21 09:08:56 +08002154 request = list_first_entry(&render_ring->request_list,
Eric Anholt673a3942008-07-30 12:06:12 -07002155 struct drm_i915_gem_request,
2156 list);
2157
Zou Nan hai852835f2010-05-21 09:08:56 +08002158 ret = i915_wait_request(dev,
2159 request->seqno, request->ring);
Eric Anholt673a3942008-07-30 12:06:12 -07002160 if (ret)
Chris Wilson07f73f62009-09-14 16:50:30 +01002161 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002162
Chris Wilson07f73f62009-09-14 16:50:30 +01002163 continue;
Eric Anholt673a3942008-07-30 12:06:12 -07002164 }
2165
Zou Nan haid1b851f2010-05-21 09:08:57 +08002166 if (HAS_BSD(dev) && !list_empty(&bsd_ring->request_list)) {
2167 struct drm_i915_gem_request *request;
2168
2169 request = list_first_entry(&bsd_ring->request_list,
2170 struct drm_i915_gem_request,
2171 list);
2172
2173 ret = i915_wait_request(dev,
2174 request->seqno, request->ring);
2175 if (ret)
2176 return ret;
2177
2178 continue;
2179 }
2180
Eric Anholt673a3942008-07-30 12:06:12 -07002181 /* If we didn't have anything on the request list but there
2182 * are buffers awaiting a flush, emit one and try again.
2183 * When we wait on it, those buffers waiting for that flush
2184 * will get moved to inactive.
2185 */
2186 if (!list_empty(&dev_priv->mm.flushing_list)) {
Chris Wilson07f73f62009-09-14 16:50:30 +01002187 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07002188
Chris Wilson9a1e2582009-09-20 20:16:50 +01002189 /* Find an object that we can immediately reuse */
2190 list_for_each_entry(obj_priv, &dev_priv->mm.flushing_list, list) {
Daniel Vettera8089e82010-04-09 19:05:09 +00002191 obj = &obj_priv->base;
Chris Wilson9a1e2582009-09-20 20:16:50 +01002192 if (obj->size >= min_size)
2193 break;
Eric Anholt673a3942008-07-30 12:06:12 -07002194
Chris Wilson9a1e2582009-09-20 20:16:50 +01002195 obj = NULL;
2196 }
Eric Anholt673a3942008-07-30 12:06:12 -07002197
Chris Wilson9a1e2582009-09-20 20:16:50 +01002198 if (obj != NULL) {
2199 uint32_t seqno;
Chris Wilson07f73f62009-09-14 16:50:30 +01002200
Zou Nan hai852835f2010-05-21 09:08:56 +08002201 i915_gem_flush_ring(dev,
Chris Wilson9a1e2582009-09-20 20:16:50 +01002202 obj->write_domain,
Zou Nan hai852835f2010-05-21 09:08:56 +08002203 obj->write_domain,
2204 obj_priv->ring);
2205 seqno = i915_add_request(dev, NULL,
2206 obj->write_domain,
2207 obj_priv->ring);
Chris Wilson9a1e2582009-09-20 20:16:50 +01002208 if (seqno == 0)
2209 return -ENOMEM;
Chris Wilson9a1e2582009-09-20 20:16:50 +01002210 continue;
2211 }
Eric Anholt673a3942008-07-30 12:06:12 -07002212 }
2213
Chris Wilson07f73f62009-09-14 16:50:30 +01002214 /* If we didn't do any of the above, there's no single buffer
2215 * large enough to swap out for the new one, so just evict
2216 * everything and start again. (This should be rare.)
Eric Anholt673a3942008-07-30 12:06:12 -07002217 */
Chris Wilson97311292009-09-21 00:22:34 +01002218 if (!list_empty (&dev_priv->mm.inactive_list))
Chris Wilsonab5ee572009-09-20 19:25:47 +01002219 return i915_gem_evict_from_inactive_list(dev);
Chris Wilson97311292009-09-21 00:22:34 +01002220 else
Chris Wilson07f73f62009-09-14 16:50:30 +01002221 return i915_gem_evict_everything(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07002222 }
Keith Packardac94a962008-11-20 23:30:27 -08002223}
2224
Ben Gamari6911a9b2009-04-02 11:24:54 -07002225int
Chris Wilson4bdadb92010-01-27 13:36:32 +00002226i915_gem_object_get_pages(struct drm_gem_object *obj,
2227 gfp_t gfpmask)
Eric Anholt673a3942008-07-30 12:06:12 -07002228{
Daniel Vetter23010e42010-03-08 13:35:02 +01002229 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002230 int page_count, i;
2231 struct address_space *mapping;
2232 struct inode *inode;
2233 struct page *page;
Eric Anholt673a3942008-07-30 12:06:12 -07002234
Daniel Vetter778c3542010-05-13 11:49:44 +02002235 BUG_ON(obj_priv->pages_refcount
2236 == DRM_I915_GEM_OBJECT_MAX_PAGES_REFCOUNT);
2237
Eric Anholt856fa192009-03-19 14:10:50 -07002238 if (obj_priv->pages_refcount++ != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07002239 return 0;
2240
2241 /* Get the list of pages out of our struct file. They'll be pinned
2242 * at this point until we release them.
2243 */
2244 page_count = obj->size / PAGE_SIZE;
Eric Anholt856fa192009-03-19 14:10:50 -07002245 BUG_ON(obj_priv->pages != NULL);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07002246 obj_priv->pages = drm_calloc_large(page_count, sizeof(struct page *));
Eric Anholt856fa192009-03-19 14:10:50 -07002247 if (obj_priv->pages == NULL) {
Eric Anholt856fa192009-03-19 14:10:50 -07002248 obj_priv->pages_refcount--;
Eric Anholt673a3942008-07-30 12:06:12 -07002249 return -ENOMEM;
2250 }
2251
2252 inode = obj->filp->f_path.dentry->d_inode;
2253 mapping = inode->i_mapping;
2254 for (i = 0; i < page_count; i++) {
Chris Wilson4bdadb92010-01-27 13:36:32 +00002255 page = read_cache_page_gfp(mapping, i,
Linus Torvalds985b8232010-07-02 10:04:42 +10002256 GFP_HIGHUSER |
Chris Wilson4bdadb92010-01-27 13:36:32 +00002257 __GFP_COLD |
Linus Torvaldscd9f0402010-07-18 09:44:37 -07002258 __GFP_RECLAIMABLE |
Chris Wilson4bdadb92010-01-27 13:36:32 +00002259 gfpmask);
Chris Wilson1f2b1012010-03-12 19:52:55 +00002260 if (IS_ERR(page))
2261 goto err_pages;
2262
Eric Anholt856fa192009-03-19 14:10:50 -07002263 obj_priv->pages[i] = page;
Eric Anholt673a3942008-07-30 12:06:12 -07002264 }
Eric Anholt280b7132009-03-12 16:56:27 -07002265
2266 if (obj_priv->tiling_mode != I915_TILING_NONE)
2267 i915_gem_object_do_bit_17_swizzle(obj);
2268
Eric Anholt673a3942008-07-30 12:06:12 -07002269 return 0;
Chris Wilson1f2b1012010-03-12 19:52:55 +00002270
2271err_pages:
2272 while (i--)
2273 page_cache_release(obj_priv->pages[i]);
2274
2275 drm_free_large(obj_priv->pages);
2276 obj_priv->pages = NULL;
2277 obj_priv->pages_refcount--;
2278 return PTR_ERR(page);
Eric Anholt673a3942008-07-30 12:06:12 -07002279}
2280
Eric Anholt4e901fd2009-10-26 16:44:17 -07002281static void sandybridge_write_fence_reg(struct drm_i915_fence_reg *reg)
2282{
2283 struct drm_gem_object *obj = reg->obj;
2284 struct drm_device *dev = obj->dev;
2285 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002286 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt4e901fd2009-10-26 16:44:17 -07002287 int regnum = obj_priv->fence_reg;
2288 uint64_t val;
2289
2290 val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
2291 0xfffff000) << 32;
2292 val |= obj_priv->gtt_offset & 0xfffff000;
2293 val |= (uint64_t)((obj_priv->stride / 128) - 1) <<
2294 SANDYBRIDGE_FENCE_PITCH_SHIFT;
2295
2296 if (obj_priv->tiling_mode == I915_TILING_Y)
2297 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2298 val |= I965_FENCE_REG_VALID;
2299
2300 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (regnum * 8), val);
2301}
2302
Jesse Barnesde151cf2008-11-12 10:03:55 -08002303static void i965_write_fence_reg(struct drm_i915_fence_reg *reg)
2304{
2305 struct drm_gem_object *obj = reg->obj;
2306 struct drm_device *dev = obj->dev;
2307 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002308 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002309 int regnum = obj_priv->fence_reg;
2310 uint64_t val;
2311
2312 val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
2313 0xfffff000) << 32;
2314 val |= obj_priv->gtt_offset & 0xfffff000;
2315 val |= ((obj_priv->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
2316 if (obj_priv->tiling_mode == I915_TILING_Y)
2317 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2318 val |= I965_FENCE_REG_VALID;
2319
2320 I915_WRITE64(FENCE_REG_965_0 + (regnum * 8), val);
2321}
2322
2323static void i915_write_fence_reg(struct drm_i915_fence_reg *reg)
2324{
2325 struct drm_gem_object *obj = reg->obj;
2326 struct drm_device *dev = obj->dev;
2327 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002328 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002329 int regnum = obj_priv->fence_reg;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002330 int tile_width;
Eric Anholtdc529a42009-03-10 22:34:49 -07002331 uint32_t fence_reg, val;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002332 uint32_t pitch_val;
2333
2334 if ((obj_priv->gtt_offset & ~I915_FENCE_START_MASK) ||
2335 (obj_priv->gtt_offset & (obj->size - 1))) {
Linus Torvaldsf06da262009-02-09 08:57:29 -08002336 WARN(1, "%s: object 0x%08x not 1M or size (0x%zx) aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002337 __func__, obj_priv->gtt_offset, obj->size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002338 return;
2339 }
2340
Jesse Barnes0f973f22009-01-26 17:10:45 -08002341 if (obj_priv->tiling_mode == I915_TILING_Y &&
2342 HAS_128_BYTE_Y_TILING(dev))
2343 tile_width = 128;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002344 else
Jesse Barnes0f973f22009-01-26 17:10:45 -08002345 tile_width = 512;
2346
2347 /* Note: pitch better be a power of two tile widths */
2348 pitch_val = obj_priv->stride / tile_width;
2349 pitch_val = ffs(pitch_val) - 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002350
Daniel Vetterc36a2a62010-04-17 15:12:03 +02002351 if (obj_priv->tiling_mode == I915_TILING_Y &&
2352 HAS_128_BYTE_Y_TILING(dev))
2353 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2354 else
2355 WARN_ON(pitch_val > I915_FENCE_MAX_PITCH_VAL);
2356
Jesse Barnesde151cf2008-11-12 10:03:55 -08002357 val = obj_priv->gtt_offset;
2358 if (obj_priv->tiling_mode == I915_TILING_Y)
2359 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2360 val |= I915_FENCE_SIZE_BITS(obj->size);
2361 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2362 val |= I830_FENCE_REG_VALID;
2363
Eric Anholtdc529a42009-03-10 22:34:49 -07002364 if (regnum < 8)
2365 fence_reg = FENCE_REG_830_0 + (regnum * 4);
2366 else
2367 fence_reg = FENCE_REG_945_8 + ((regnum - 8) * 4);
2368 I915_WRITE(fence_reg, val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002369}
2370
2371static void i830_write_fence_reg(struct drm_i915_fence_reg *reg)
2372{
2373 struct drm_gem_object *obj = reg->obj;
2374 struct drm_device *dev = obj->dev;
2375 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002376 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002377 int regnum = obj_priv->fence_reg;
2378 uint32_t val;
2379 uint32_t pitch_val;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002380 uint32_t fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002381
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002382 if ((obj_priv->gtt_offset & ~I830_FENCE_START_MASK) ||
Jesse Barnesde151cf2008-11-12 10:03:55 -08002383 (obj_priv->gtt_offset & (obj->size - 1))) {
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002384 WARN(1, "%s: object 0x%08x not 512K or size aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002385 __func__, obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002386 return;
2387 }
2388
Eric Anholte76a16d2009-05-26 17:44:56 -07002389 pitch_val = obj_priv->stride / 128;
2390 pitch_val = ffs(pitch_val) - 1;
2391 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2392
Jesse Barnesde151cf2008-11-12 10:03:55 -08002393 val = obj_priv->gtt_offset;
2394 if (obj_priv->tiling_mode == I915_TILING_Y)
2395 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002396 fence_size_bits = I830_FENCE_SIZE_BITS(obj->size);
2397 WARN_ON(fence_size_bits & ~0x00000f00);
2398 val |= fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002399 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2400 val |= I830_FENCE_REG_VALID;
2401
2402 I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002403}
2404
Daniel Vetterae3db242010-02-19 11:51:58 +01002405static int i915_find_fence_reg(struct drm_device *dev)
2406{
2407 struct drm_i915_fence_reg *reg = NULL;
2408 struct drm_i915_gem_object *obj_priv = NULL;
2409 struct drm_i915_private *dev_priv = dev->dev_private;
2410 struct drm_gem_object *obj = NULL;
2411 int i, avail, ret;
2412
2413 /* First try to find a free reg */
2414 avail = 0;
2415 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2416 reg = &dev_priv->fence_regs[i];
2417 if (!reg->obj)
2418 return i;
2419
Daniel Vetter23010e42010-03-08 13:35:02 +01002420 obj_priv = to_intel_bo(reg->obj);
Daniel Vetterae3db242010-02-19 11:51:58 +01002421 if (!obj_priv->pin_count)
2422 avail++;
2423 }
2424
2425 if (avail == 0)
2426 return -ENOSPC;
2427
2428 /* None available, try to steal one or wait for a user to finish */
2429 i = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002430 list_for_each_entry(reg, &dev_priv->mm.fence_list,
2431 lru_list) {
2432 obj = reg->obj;
2433 obj_priv = to_intel_bo(obj);
Daniel Vetterae3db242010-02-19 11:51:58 +01002434
2435 if (obj_priv->pin_count)
2436 continue;
2437
2438 /* found one! */
2439 i = obj_priv->fence_reg;
2440 break;
2441 }
2442
2443 BUG_ON(i == I915_FENCE_REG_NONE);
2444
2445 /* We only have a reference on obj from the active list. put_fence_reg
2446 * might drop that one, causing a use-after-free in it. So hold a
2447 * private reference to obj like the other callers of put_fence_reg
2448 * (set_tiling ioctl) do. */
2449 drm_gem_object_reference(obj);
2450 ret = i915_gem_object_put_fence_reg(obj);
2451 drm_gem_object_unreference(obj);
2452 if (ret != 0)
2453 return ret;
2454
2455 return i;
2456}
2457
Jesse Barnesde151cf2008-11-12 10:03:55 -08002458/**
2459 * i915_gem_object_get_fence_reg - set up a fence reg for an object
2460 * @obj: object to map through a fence reg
2461 *
2462 * When mapping objects through the GTT, userspace wants to be able to write
2463 * to them without having to worry about swizzling if the object is tiled.
2464 *
2465 * This function walks the fence regs looking for a free one for @obj,
2466 * stealing one if it can't find any.
2467 *
2468 * It then sets up the reg based on the object's properties: address, pitch
2469 * and tiling format.
2470 */
Chris Wilson8c4b8c32009-06-17 22:08:52 +01002471int
2472i915_gem_object_get_fence_reg(struct drm_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002473{
2474 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002475 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002476 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002477 struct drm_i915_fence_reg *reg = NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002478 int ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002479
Eric Anholta09ba7f2009-08-29 12:49:51 -07002480 /* Just update our place in the LRU if our fence is getting used. */
2481 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002482 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
2483 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002484 return 0;
2485 }
2486
Jesse Barnesde151cf2008-11-12 10:03:55 -08002487 switch (obj_priv->tiling_mode) {
2488 case I915_TILING_NONE:
2489 WARN(1, "allocating a fence for non-tiled object?\n");
2490 break;
2491 case I915_TILING_X:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002492 if (!obj_priv->stride)
2493 return -EINVAL;
2494 WARN((obj_priv->stride & (512 - 1)),
2495 "object 0x%08x is X tiled but has non-512B pitch\n",
2496 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002497 break;
2498 case I915_TILING_Y:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002499 if (!obj_priv->stride)
2500 return -EINVAL;
2501 WARN((obj_priv->stride & (128 - 1)),
2502 "object 0x%08x is Y tiled but has non-128B pitch\n",
2503 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002504 break;
2505 }
2506
Daniel Vetterae3db242010-02-19 11:51:58 +01002507 ret = i915_find_fence_reg(dev);
2508 if (ret < 0)
2509 return ret;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002510
Daniel Vetterae3db242010-02-19 11:51:58 +01002511 obj_priv->fence_reg = ret;
2512 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002513 list_add_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002514
Jesse Barnesde151cf2008-11-12 10:03:55 -08002515 reg->obj = obj;
2516
Eric Anholt4e901fd2009-10-26 16:44:17 -07002517 if (IS_GEN6(dev))
2518 sandybridge_write_fence_reg(reg);
2519 else if (IS_I965G(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08002520 i965_write_fence_reg(reg);
2521 else if (IS_I9XX(dev))
2522 i915_write_fence_reg(reg);
2523 else
2524 i830_write_fence_reg(reg);
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002525
Daniel Vetterae3db242010-02-19 11:51:58 +01002526 trace_i915_gem_object_get_fence(obj, obj_priv->fence_reg,
2527 obj_priv->tiling_mode);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002528
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002529 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002530}
2531
2532/**
2533 * i915_gem_clear_fence_reg - clear out fence register info
2534 * @obj: object to clear
2535 *
2536 * Zeroes out the fence register itself and clears out the associated
2537 * data structures in dev_priv and obj_priv.
2538 */
2539static void
2540i915_gem_clear_fence_reg(struct drm_gem_object *obj)
2541{
2542 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002543 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002544 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002545 struct drm_i915_fence_reg *reg =
2546 &dev_priv->fence_regs[obj_priv->fence_reg];
Jesse Barnesde151cf2008-11-12 10:03:55 -08002547
Eric Anholt4e901fd2009-10-26 16:44:17 -07002548 if (IS_GEN6(dev)) {
2549 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 +
2550 (obj_priv->fence_reg * 8), 0);
2551 } else if (IS_I965G(dev)) {
Jesse Barnesde151cf2008-11-12 10:03:55 -08002552 I915_WRITE64(FENCE_REG_965_0 + (obj_priv->fence_reg * 8), 0);
Eric Anholt4e901fd2009-10-26 16:44:17 -07002553 } else {
Eric Anholtdc529a42009-03-10 22:34:49 -07002554 uint32_t fence_reg;
2555
2556 if (obj_priv->fence_reg < 8)
2557 fence_reg = FENCE_REG_830_0 + obj_priv->fence_reg * 4;
2558 else
2559 fence_reg = FENCE_REG_945_8 + (obj_priv->fence_reg -
2560 8) * 4;
2561
2562 I915_WRITE(fence_reg, 0);
2563 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002564
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002565 reg->obj = NULL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002566 obj_priv->fence_reg = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002567 list_del_init(&reg->lru_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002568}
2569
Eric Anholt673a3942008-07-30 12:06:12 -07002570/**
Chris Wilson52dc7d32009-06-06 09:46:01 +01002571 * i915_gem_object_put_fence_reg - waits on outstanding fenced access
2572 * to the buffer to finish, and then resets the fence register.
2573 * @obj: tiled object holding a fence register.
2574 *
2575 * Zeroes out the fence register itself and clears out the associated
2576 * data structures in dev_priv and obj_priv.
2577 */
2578int
2579i915_gem_object_put_fence_reg(struct drm_gem_object *obj)
2580{
2581 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01002582 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson52dc7d32009-06-06 09:46:01 +01002583
2584 if (obj_priv->fence_reg == I915_FENCE_REG_NONE)
2585 return 0;
2586
Daniel Vetter10ae9bd2010-02-01 13:59:17 +01002587 /* If we've changed tiling, GTT-mappings of the object
2588 * need to re-fault to ensure that the correct fence register
2589 * setup is in place.
2590 */
2591 i915_gem_release_mmap(obj);
2592
Chris Wilson52dc7d32009-06-06 09:46:01 +01002593 /* On the i915, GPU access to tiled buffers is via a fence,
2594 * therefore we must wait for any outstanding access to complete
2595 * before clearing the fence.
2596 */
2597 if (!IS_I965G(dev)) {
2598 int ret;
2599
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002600 ret = i915_gem_object_flush_gpu_write_domain(obj);
2601 if (ret != 0)
2602 return ret;
2603
Chris Wilson52dc7d32009-06-06 09:46:01 +01002604 ret = i915_gem_object_wait_rendering(obj);
2605 if (ret != 0)
2606 return ret;
2607 }
2608
Daniel Vetter4a726612010-02-01 13:59:16 +01002609 i915_gem_object_flush_gtt_write_domain(obj);
Chris Wilson52dc7d32009-06-06 09:46:01 +01002610 i915_gem_clear_fence_reg (obj);
2611
2612 return 0;
2613}
2614
2615/**
Eric Anholt673a3942008-07-30 12:06:12 -07002616 * Finds free space in the GTT aperture and binds the object there.
2617 */
2618static int
2619i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment)
2620{
2621 struct drm_device *dev = obj->dev;
2622 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002623 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002624 struct drm_mm_node *free_space;
Chris Wilson4bdadb92010-01-27 13:36:32 +00002625 gfp_t gfpmask = __GFP_NORETRY | __GFP_NOWARN;
Chris Wilson07f73f62009-09-14 16:50:30 +01002626 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002627
Chris Wilsonbb6baf72009-09-22 14:24:13 +01002628 if (obj_priv->madv != I915_MADV_WILLNEED) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01002629 DRM_ERROR("Attempting to bind a purgeable object\n");
2630 return -EINVAL;
2631 }
2632
Eric Anholt673a3942008-07-30 12:06:12 -07002633 if (alignment == 0)
Jesse Barnes0f973f22009-01-26 17:10:45 -08002634 alignment = i915_gem_get_gtt_alignment(obj);
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002635 if (alignment & (i915_gem_get_gtt_alignment(obj) - 1)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002636 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2637 return -EINVAL;
2638 }
2639
Chris Wilson654fc602010-05-27 13:18:21 +01002640 /* If the object is bigger than the entire aperture, reject it early
2641 * before evicting everything in a vain attempt to find space.
2642 */
2643 if (obj->size > dev->gtt_total) {
2644 DRM_ERROR("Attempting to bind an object larger than the aperture\n");
2645 return -E2BIG;
2646 }
2647
Eric Anholt673a3942008-07-30 12:06:12 -07002648 search_free:
2649 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
2650 obj->size, alignment, 0);
2651 if (free_space != NULL) {
2652 obj_priv->gtt_space = drm_mm_get_block(free_space, obj->size,
2653 alignment);
Daniel Vetterdb3307a2010-07-02 15:02:12 +01002654 if (obj_priv->gtt_space != NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002655 obj_priv->gtt_offset = obj_priv->gtt_space->start;
Eric Anholt673a3942008-07-30 12:06:12 -07002656 }
2657 if (obj_priv->gtt_space == NULL) {
2658 /* If the gtt is empty and we're still having trouble
2659 * fitting our object in, we're out of memory.
2660 */
2661#if WATCH_LRU
2662 DRM_INFO("%s: GTT full, evicting something\n", __func__);
2663#endif
Chris Wilson07f73f62009-09-14 16:50:30 +01002664 ret = i915_gem_evict_something(dev, obj->size);
Chris Wilson97311292009-09-21 00:22:34 +01002665 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002666 return ret;
Chris Wilson97311292009-09-21 00:22:34 +01002667
Eric Anholt673a3942008-07-30 12:06:12 -07002668 goto search_free;
2669 }
2670
2671#if WATCH_BUF
Krzysztof Halasacfd43c02009-06-20 00:31:28 +02002672 DRM_INFO("Binding object of size %zd at 0x%08x\n",
Eric Anholt673a3942008-07-30 12:06:12 -07002673 obj->size, obj_priv->gtt_offset);
2674#endif
Chris Wilson4bdadb92010-01-27 13:36:32 +00002675 ret = i915_gem_object_get_pages(obj, gfpmask);
Eric Anholt673a3942008-07-30 12:06:12 -07002676 if (ret) {
2677 drm_mm_put_block(obj_priv->gtt_space);
2678 obj_priv->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002679
2680 if (ret == -ENOMEM) {
2681 /* first try to clear up some space from the GTT */
2682 ret = i915_gem_evict_something(dev, obj->size);
2683 if (ret) {
Chris Wilson07f73f62009-09-14 16:50:30 +01002684 /* now try to shrink everyone else */
Chris Wilson4bdadb92010-01-27 13:36:32 +00002685 if (gfpmask) {
2686 gfpmask = 0;
2687 goto search_free;
Chris Wilson07f73f62009-09-14 16:50:30 +01002688 }
2689
2690 return ret;
2691 }
2692
2693 goto search_free;
2694 }
2695
Eric Anholt673a3942008-07-30 12:06:12 -07002696 return ret;
2697 }
2698
Eric Anholt673a3942008-07-30 12:06:12 -07002699 /* Create an AGP memory structure pointing at our pages, and bind it
2700 * into the GTT.
2701 */
2702 obj_priv->agp_mem = drm_agp_bind_pages(dev,
Eric Anholt856fa192009-03-19 14:10:50 -07002703 obj_priv->pages,
Chris Wilson07f73f62009-09-14 16:50:30 +01002704 obj->size >> PAGE_SHIFT,
Keith Packardba1eb1d2008-10-14 19:55:10 -07002705 obj_priv->gtt_offset,
2706 obj_priv->agp_type);
Eric Anholt673a3942008-07-30 12:06:12 -07002707 if (obj_priv->agp_mem == NULL) {
Eric Anholt856fa192009-03-19 14:10:50 -07002708 i915_gem_object_put_pages(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002709 drm_mm_put_block(obj_priv->gtt_space);
2710 obj_priv->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002711
2712 ret = i915_gem_evict_something(dev, obj->size);
Chris Wilson97311292009-09-21 00:22:34 +01002713 if (ret)
Chris Wilson07f73f62009-09-14 16:50:30 +01002714 return ret;
Chris Wilson07f73f62009-09-14 16:50:30 +01002715
2716 goto search_free;
Eric Anholt673a3942008-07-30 12:06:12 -07002717 }
2718 atomic_inc(&dev->gtt_count);
2719 atomic_add(obj->size, &dev->gtt_memory);
2720
Chris Wilsonbf1a1092010-08-07 11:01:20 +01002721 /* keep track of bounds object by adding it to the inactive list */
2722 list_add_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
2723
Eric Anholt673a3942008-07-30 12:06:12 -07002724 /* Assert that the object is not currently in any GPU domain. As it
2725 * wasn't in the GTT, there shouldn't be any way it could have been in
2726 * a GPU cache
2727 */
Chris Wilson21d509e2009-06-06 09:46:02 +01002728 BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS);
2729 BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS);
Eric Anholt673a3942008-07-30 12:06:12 -07002730
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002731 trace_i915_gem_object_bind(obj, obj_priv->gtt_offset);
2732
Eric Anholt673a3942008-07-30 12:06:12 -07002733 return 0;
2734}
2735
2736void
2737i915_gem_clflush_object(struct drm_gem_object *obj)
2738{
Daniel Vetter23010e42010-03-08 13:35:02 +01002739 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002740
2741 /* If we don't have a page list set up, then we're not pinned
2742 * to GPU, and we can ignore the cache flush because it'll happen
2743 * again at bind time.
2744 */
Eric Anholt856fa192009-03-19 14:10:50 -07002745 if (obj_priv->pages == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002746 return;
2747
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002748 trace_i915_gem_object_clflush(obj);
Eric Anholtcfa16a02009-05-26 18:46:16 -07002749
Eric Anholt856fa192009-03-19 14:10:50 -07002750 drm_clflush_pages(obj_priv->pages, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002751}
2752
Eric Anholte47c68e2008-11-14 13:35:19 -08002753/** Flushes any GPU write domain for the object if it's dirty. */
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002754static int
Eric Anholte47c68e2008-11-14 13:35:19 -08002755i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj)
2756{
2757 struct drm_device *dev = obj->dev;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002758 uint32_t old_write_domain;
Zou Nan hai852835f2010-05-21 09:08:56 +08002759 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08002760
2761 if ((obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002762 return 0;
Eric Anholte47c68e2008-11-14 13:35:19 -08002763
2764 /* Queue the GPU write cache flushing we need. */
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002765 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002766 i915_gem_flush(dev, 0, obj->write_domain);
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002767 if (i915_add_request(dev, NULL, obj->write_domain, obj_priv->ring) == 0)
2768 return -ENOMEM;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002769
2770 trace_i915_gem_object_change_domain(obj,
2771 obj->read_domains,
2772 old_write_domain);
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002773 return 0;
Eric Anholte47c68e2008-11-14 13:35:19 -08002774}
2775
2776/** Flushes the GTT write domain for the object if it's dirty. */
2777static void
2778i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj)
2779{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002780 uint32_t old_write_domain;
2781
Eric Anholte47c68e2008-11-14 13:35:19 -08002782 if (obj->write_domain != I915_GEM_DOMAIN_GTT)
2783 return;
2784
2785 /* No actual flushing is required for the GTT write domain. Writes
2786 * to it immediately go to main memory as far as we know, so there's
2787 * no chipset flush. It also doesn't land in render cache.
2788 */
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002789 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002790 obj->write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002791
2792 trace_i915_gem_object_change_domain(obj,
2793 obj->read_domains,
2794 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002795}
2796
2797/** Flushes the CPU write domain for the object if it's dirty. */
2798static void
2799i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj)
2800{
2801 struct drm_device *dev = obj->dev;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002802 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002803
2804 if (obj->write_domain != I915_GEM_DOMAIN_CPU)
2805 return;
2806
2807 i915_gem_clflush_object(obj);
2808 drm_agp_chipset_flush(dev);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002809 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002810 obj->write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002811
2812 trace_i915_gem_object_change_domain(obj,
2813 obj->read_domains,
2814 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002815}
2816
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002817int
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002818i915_gem_object_flush_write_domain(struct drm_gem_object *obj)
2819{
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002820 int ret = 0;
2821
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002822 switch (obj->write_domain) {
2823 case I915_GEM_DOMAIN_GTT:
2824 i915_gem_object_flush_gtt_write_domain(obj);
2825 break;
2826 case I915_GEM_DOMAIN_CPU:
2827 i915_gem_object_flush_cpu_write_domain(obj);
2828 break;
2829 default:
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002830 ret = i915_gem_object_flush_gpu_write_domain(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002831 break;
2832 }
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002833
2834 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002835}
2836
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002837/**
2838 * Moves a single object to the GTT read, and possibly write domain.
2839 *
2840 * This function returns when the move is complete, including waiting on
2841 * flushes to occur.
2842 */
Jesse Barnes79e53942008-11-07 14:24:08 -08002843int
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002844i915_gem_object_set_to_gtt_domain(struct drm_gem_object *obj, int write)
2845{
Daniel Vetter23010e42010-03-08 13:35:02 +01002846 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002847 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002848 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002849
Eric Anholt02354392008-11-26 13:58:13 -08002850 /* Not valid to be called on unbound objects. */
2851 if (obj_priv->gtt_space == NULL)
2852 return -EINVAL;
2853
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002854 ret = i915_gem_object_flush_gpu_write_domain(obj);
2855 if (ret != 0)
2856 return ret;
2857
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002858 /* Wait on any GPU rendering and flushing to occur. */
Eric Anholte47c68e2008-11-14 13:35:19 -08002859 ret = i915_gem_object_wait_rendering(obj);
2860 if (ret != 0)
2861 return ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002862
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002863 old_write_domain = obj->write_domain;
2864 old_read_domains = obj->read_domains;
2865
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002866 /* If we're writing through the GTT domain, then CPU and GPU caches
2867 * will need to be invalidated at next use.
2868 */
2869 if (write)
Eric Anholte47c68e2008-11-14 13:35:19 -08002870 obj->read_domains &= I915_GEM_DOMAIN_GTT;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002871
Eric Anholte47c68e2008-11-14 13:35:19 -08002872 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002873
2874 /* It should now be out of any other write domains, and we can update
2875 * the domain values for our changes.
2876 */
2877 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2878 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002879 if (write) {
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002880 obj->write_domain = I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002881 obj_priv->dirty = 1;
2882 }
2883
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002884 trace_i915_gem_object_change_domain(obj,
2885 old_read_domains,
2886 old_write_domain);
2887
Eric Anholte47c68e2008-11-14 13:35:19 -08002888 return 0;
2889}
2890
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002891/*
2892 * Prepare buffer for display plane. Use uninterruptible for possible flush
2893 * wait, as in modesetting process we're not supposed to be interrupted.
2894 */
2895int
2896i915_gem_object_set_to_display_plane(struct drm_gem_object *obj)
2897{
2898 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01002899 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002900 uint32_t old_write_domain, old_read_domains;
2901 int ret;
2902
2903 /* Not valid to be called on unbound objects. */
2904 if (obj_priv->gtt_space == NULL)
2905 return -EINVAL;
2906
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002907 ret = i915_gem_object_flush_gpu_write_domain(obj);
2908 if (ret)
2909 return ret;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002910
2911 /* Wait on any GPU rendering and flushing to occur. */
2912 if (obj_priv->active) {
2913#if WATCH_BUF
2914 DRM_INFO("%s: object %p wait for seqno %08x\n",
2915 __func__, obj, obj_priv->last_rendering_seqno);
2916#endif
Zou Nan hai852835f2010-05-21 09:08:56 +08002917 ret = i915_do_wait_request(dev,
2918 obj_priv->last_rendering_seqno,
2919 0,
2920 obj_priv->ring);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002921 if (ret != 0)
2922 return ret;
2923 }
2924
Chris Wilsonb118c1e2010-05-27 13:18:14 +01002925 i915_gem_object_flush_cpu_write_domain(obj);
2926
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002927 old_write_domain = obj->write_domain;
2928 old_read_domains = obj->read_domains;
2929
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002930 /* It should now be out of any other write domains, and we can update
2931 * the domain values for our changes.
2932 */
2933 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
Chris Wilsonb118c1e2010-05-27 13:18:14 +01002934 obj->read_domains = I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002935 obj->write_domain = I915_GEM_DOMAIN_GTT;
2936 obj_priv->dirty = 1;
2937
2938 trace_i915_gem_object_change_domain(obj,
2939 old_read_domains,
2940 old_write_domain);
2941
2942 return 0;
2943}
2944
Eric Anholte47c68e2008-11-14 13:35:19 -08002945/**
2946 * Moves a single object to the CPU read, and possibly write domain.
2947 *
2948 * This function returns when the move is complete, including waiting on
2949 * flushes to occur.
2950 */
2951static int
2952i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj, int write)
2953{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002954 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002955 int ret;
2956
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002957 ret = i915_gem_object_flush_gpu_write_domain(obj);
2958 if (ret)
2959 return ret;
2960
Eric Anholte47c68e2008-11-14 13:35:19 -08002961 /* Wait on any GPU rendering and flushing to occur. */
2962 ret = i915_gem_object_wait_rendering(obj);
2963 if (ret != 0)
2964 return ret;
2965
2966 i915_gem_object_flush_gtt_write_domain(obj);
2967
2968 /* If we have a partially-valid cache of the object in the CPU,
2969 * finish invalidating it and free the per-page flags.
2970 */
2971 i915_gem_object_set_to_full_cpu_read_domain(obj);
2972
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002973 old_write_domain = obj->write_domain;
2974 old_read_domains = obj->read_domains;
2975
Eric Anholte47c68e2008-11-14 13:35:19 -08002976 /* Flush the CPU cache if it's still invalid. */
2977 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
2978 i915_gem_clflush_object(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08002979
2980 obj->read_domains |= I915_GEM_DOMAIN_CPU;
2981 }
2982
2983 /* It should now be out of any other write domains, and we can update
2984 * the domain values for our changes.
2985 */
2986 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
2987
2988 /* If we're writing through the CPU, then the GPU read domains will
2989 * need to be invalidated at next use.
2990 */
2991 if (write) {
2992 obj->read_domains &= I915_GEM_DOMAIN_CPU;
2993 obj->write_domain = I915_GEM_DOMAIN_CPU;
2994 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002995
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002996 trace_i915_gem_object_change_domain(obj,
2997 old_read_domains,
2998 old_write_domain);
2999
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003000 return 0;
3001}
3002
Eric Anholt673a3942008-07-30 12:06:12 -07003003/*
3004 * Set the next domain for the specified object. This
3005 * may not actually perform the necessary flushing/invaliding though,
3006 * as that may want to be batched with other set_domain operations
3007 *
3008 * This is (we hope) the only really tricky part of gem. The goal
3009 * is fairly simple -- track which caches hold bits of the object
3010 * and make sure they remain coherent. A few concrete examples may
3011 * help to explain how it works. For shorthand, we use the notation
3012 * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
3013 * a pair of read and write domain masks.
3014 *
3015 * Case 1: the batch buffer
3016 *
3017 * 1. Allocated
3018 * 2. Written by CPU
3019 * 3. Mapped to GTT
3020 * 4. Read by GPU
3021 * 5. Unmapped from GTT
3022 * 6. Freed
3023 *
3024 * Let's take these a step at a time
3025 *
3026 * 1. Allocated
3027 * Pages allocated from the kernel may still have
3028 * cache contents, so we set them to (CPU, CPU) always.
3029 * 2. Written by CPU (using pwrite)
3030 * The pwrite function calls set_domain (CPU, CPU) and
3031 * this function does nothing (as nothing changes)
3032 * 3. Mapped by GTT
3033 * This function asserts that the object is not
3034 * currently in any GPU-based read or write domains
3035 * 4. Read by GPU
3036 * i915_gem_execbuffer calls set_domain (COMMAND, 0).
3037 * As write_domain is zero, this function adds in the
3038 * current read domains (CPU+COMMAND, 0).
3039 * flush_domains is set to CPU.
3040 * invalidate_domains is set to COMMAND
3041 * clflush is run to get data out of the CPU caches
3042 * then i915_dev_set_domain calls i915_gem_flush to
3043 * emit an MI_FLUSH and drm_agp_chipset_flush
3044 * 5. Unmapped from GTT
3045 * i915_gem_object_unbind calls set_domain (CPU, CPU)
3046 * flush_domains and invalidate_domains end up both zero
3047 * so no flushing/invalidating happens
3048 * 6. Freed
3049 * yay, done
3050 *
3051 * Case 2: The shared render buffer
3052 *
3053 * 1. Allocated
3054 * 2. Mapped to GTT
3055 * 3. Read/written by GPU
3056 * 4. set_domain to (CPU,CPU)
3057 * 5. Read/written by CPU
3058 * 6. Read/written by GPU
3059 *
3060 * 1. Allocated
3061 * Same as last example, (CPU, CPU)
3062 * 2. Mapped to GTT
3063 * Nothing changes (assertions find that it is not in the GPU)
3064 * 3. Read/written by GPU
3065 * execbuffer calls set_domain (RENDER, RENDER)
3066 * flush_domains gets CPU
3067 * invalidate_domains gets GPU
3068 * clflush (obj)
3069 * MI_FLUSH and drm_agp_chipset_flush
3070 * 4. set_domain (CPU, CPU)
3071 * flush_domains gets GPU
3072 * invalidate_domains gets CPU
3073 * wait_rendering (obj) to make sure all drawing is complete.
3074 * This will include an MI_FLUSH to get the data from GPU
3075 * to memory
3076 * clflush (obj) to invalidate the CPU cache
3077 * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
3078 * 5. Read/written by CPU
3079 * cache lines are loaded and dirtied
3080 * 6. Read written by GPU
3081 * Same as last GPU access
3082 *
3083 * Case 3: The constant buffer
3084 *
3085 * 1. Allocated
3086 * 2. Written by CPU
3087 * 3. Read by GPU
3088 * 4. Updated (written) by CPU again
3089 * 5. Read by GPU
3090 *
3091 * 1. Allocated
3092 * (CPU, CPU)
3093 * 2. Written by CPU
3094 * (CPU, CPU)
3095 * 3. Read by GPU
3096 * (CPU+RENDER, 0)
3097 * flush_domains = CPU
3098 * invalidate_domains = RENDER
3099 * clflush (obj)
3100 * MI_FLUSH
3101 * drm_agp_chipset_flush
3102 * 4. Updated (written) by CPU again
3103 * (CPU, CPU)
3104 * flush_domains = 0 (no previous write domain)
3105 * invalidate_domains = 0 (no new read domains)
3106 * 5. Read by GPU
3107 * (CPU+RENDER, 0)
3108 * flush_domains = CPU
3109 * invalidate_domains = RENDER
3110 * clflush (obj)
3111 * MI_FLUSH
3112 * drm_agp_chipset_flush
3113 */
Keith Packardc0d90822008-11-20 23:11:08 -08003114static void
Eric Anholt8b0e3782009-02-19 14:40:50 -08003115i915_gem_object_set_to_gpu_domain(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07003116{
3117 struct drm_device *dev = obj->dev;
Chris Wilson88f356b2010-08-04 13:55:32 +01003118 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01003119 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003120 uint32_t invalidate_domains = 0;
3121 uint32_t flush_domains = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003122 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003123
Eric Anholt8b0e3782009-02-19 14:40:50 -08003124 BUG_ON(obj->pending_read_domains & I915_GEM_DOMAIN_CPU);
3125 BUG_ON(obj->pending_write_domain == I915_GEM_DOMAIN_CPU);
Eric Anholt673a3942008-07-30 12:06:12 -07003126
Jesse Barnes652c3932009-08-17 13:31:43 -07003127 intel_mark_busy(dev, obj);
3128
Eric Anholt673a3942008-07-30 12:06:12 -07003129#if WATCH_BUF
3130 DRM_INFO("%s: object %p read %08x -> %08x write %08x -> %08x\n",
3131 __func__, obj,
Eric Anholt8b0e3782009-02-19 14:40:50 -08003132 obj->read_domains, obj->pending_read_domains,
3133 obj->write_domain, obj->pending_write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07003134#endif
3135 /*
3136 * If the object isn't moving to a new write domain,
3137 * let the object stay in multiple read domains
3138 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003139 if (obj->pending_write_domain == 0)
3140 obj->pending_read_domains |= obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07003141 else
3142 obj_priv->dirty = 1;
3143
3144 /*
3145 * Flush the current write domain if
3146 * the new read domains don't match. Invalidate
3147 * any read domains which differ from the old
3148 * write domain
3149 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003150 if (obj->write_domain &&
3151 obj->write_domain != obj->pending_read_domains) {
Eric Anholt673a3942008-07-30 12:06:12 -07003152 flush_domains |= obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08003153 invalidate_domains |=
3154 obj->pending_read_domains & ~obj->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07003155 }
3156 /*
3157 * Invalidate any read caches which may have
3158 * stale data. That is, any new read domains.
3159 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003160 invalidate_domains |= obj->pending_read_domains & ~obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07003161 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU) {
3162#if WATCH_BUF
3163 DRM_INFO("%s: CPU domain flush %08x invalidate %08x\n",
3164 __func__, flush_domains, invalidate_domains);
3165#endif
Eric Anholt673a3942008-07-30 12:06:12 -07003166 i915_gem_clflush_object(obj);
3167 }
3168
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003169 old_read_domains = obj->read_domains;
3170
Eric Anholtefbeed92009-02-19 14:54:51 -08003171 /* The actual obj->write_domain will be updated with
3172 * pending_write_domain after we emit the accumulated flush for all
3173 * of our domain changes in execbuffers (which clears objects'
3174 * write_domains). So if we have a current write domain that we
3175 * aren't changing, set pending_write_domain to that.
3176 */
3177 if (flush_domains == 0 && obj->pending_write_domain == 0)
3178 obj->pending_write_domain = obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08003179 obj->read_domains = obj->pending_read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07003180
Chris Wilson88f356b2010-08-04 13:55:32 +01003181 if (flush_domains & I915_GEM_GPU_DOMAINS) {
3182 if (obj_priv->ring == &dev_priv->render_ring)
3183 dev_priv->flush_rings |= FLUSH_RENDER_RING;
3184 else if (obj_priv->ring == &dev_priv->bsd_ring)
3185 dev_priv->flush_rings |= FLUSH_BSD_RING;
3186 }
3187
Eric Anholt673a3942008-07-30 12:06:12 -07003188 dev->invalidate_domains |= invalidate_domains;
3189 dev->flush_domains |= flush_domains;
3190#if WATCH_BUF
3191 DRM_INFO("%s: read %08x write %08x invalidate %08x flush %08x\n",
3192 __func__,
3193 obj->read_domains, obj->write_domain,
3194 dev->invalidate_domains, dev->flush_domains);
3195#endif
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003196
3197 trace_i915_gem_object_change_domain(obj,
3198 old_read_domains,
3199 obj->write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07003200}
3201
3202/**
Eric Anholte47c68e2008-11-14 13:35:19 -08003203 * Moves the object from a partially CPU read to a full one.
Eric Anholt673a3942008-07-30 12:06:12 -07003204 *
Eric Anholte47c68e2008-11-14 13:35:19 -08003205 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
3206 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
3207 */
3208static void
3209i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj)
3210{
Daniel Vetter23010e42010-03-08 13:35:02 +01003211 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003212
3213 if (!obj_priv->page_cpu_valid)
3214 return;
3215
3216 /* If we're partially in the CPU read domain, finish moving it in.
3217 */
3218 if (obj->read_domains & I915_GEM_DOMAIN_CPU) {
3219 int i;
3220
3221 for (i = 0; i <= (obj->size - 1) / PAGE_SIZE; i++) {
3222 if (obj_priv->page_cpu_valid[i])
3223 continue;
Eric Anholt856fa192009-03-19 14:10:50 -07003224 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholte47c68e2008-11-14 13:35:19 -08003225 }
Eric Anholte47c68e2008-11-14 13:35:19 -08003226 }
3227
3228 /* Free the page_cpu_valid mappings which are now stale, whether
3229 * or not we've got I915_GEM_DOMAIN_CPU.
3230 */
Eric Anholt9a298b22009-03-24 12:23:04 -07003231 kfree(obj_priv->page_cpu_valid);
Eric Anholte47c68e2008-11-14 13:35:19 -08003232 obj_priv->page_cpu_valid = NULL;
3233}
3234
3235/**
3236 * Set the CPU read domain on a range of the object.
3237 *
3238 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
3239 * not entirely valid. The page_cpu_valid member of the object flags which
3240 * pages have been flushed, and will be respected by
3241 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
3242 * of the whole object.
3243 *
3244 * This function returns when the move is complete, including waiting on
3245 * flushes to occur.
Eric Anholt673a3942008-07-30 12:06:12 -07003246 */
3247static int
Eric Anholte47c68e2008-11-14 13:35:19 -08003248i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
3249 uint64_t offset, uint64_t size)
Eric Anholt673a3942008-07-30 12:06:12 -07003250{
Daniel Vetter23010e42010-03-08 13:35:02 +01003251 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003252 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003253 int i, ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003254
Eric Anholte47c68e2008-11-14 13:35:19 -08003255 if (offset == 0 && size == obj->size)
3256 return i915_gem_object_set_to_cpu_domain(obj, 0);
3257
Chris Wilson2dafb1e2010-06-07 14:03:05 +01003258 ret = i915_gem_object_flush_gpu_write_domain(obj);
3259 if (ret)
3260 return ret;
3261
Eric Anholte47c68e2008-11-14 13:35:19 -08003262 /* Wait on any GPU rendering and flushing to occur. */
3263 ret = i915_gem_object_wait_rendering(obj);
3264 if (ret != 0)
3265 return ret;
3266 i915_gem_object_flush_gtt_write_domain(obj);
3267
3268 /* If we're already fully in the CPU read domain, we're done. */
3269 if (obj_priv->page_cpu_valid == NULL &&
3270 (obj->read_domains & I915_GEM_DOMAIN_CPU) != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07003271 return 0;
3272
Eric Anholte47c68e2008-11-14 13:35:19 -08003273 /* Otherwise, create/clear the per-page CPU read domain flag if we're
3274 * newly adding I915_GEM_DOMAIN_CPU
3275 */
Eric Anholt673a3942008-07-30 12:06:12 -07003276 if (obj_priv->page_cpu_valid == NULL) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003277 obj_priv->page_cpu_valid = kzalloc(obj->size / PAGE_SIZE,
3278 GFP_KERNEL);
Eric Anholte47c68e2008-11-14 13:35:19 -08003279 if (obj_priv->page_cpu_valid == NULL)
3280 return -ENOMEM;
3281 } else if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0)
3282 memset(obj_priv->page_cpu_valid, 0, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07003283
3284 /* Flush the cache on any pages that are still invalid from the CPU's
3285 * perspective.
3286 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003287 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
3288 i++) {
Eric Anholt673a3942008-07-30 12:06:12 -07003289 if (obj_priv->page_cpu_valid[i])
3290 continue;
3291
Eric Anholt856fa192009-03-19 14:10:50 -07003292 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07003293
3294 obj_priv->page_cpu_valid[i] = 1;
3295 }
3296
Eric Anholte47c68e2008-11-14 13:35:19 -08003297 /* It should now be out of any other write domains, and we can update
3298 * the domain values for our changes.
3299 */
3300 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
3301
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003302 old_read_domains = obj->read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003303 obj->read_domains |= I915_GEM_DOMAIN_CPU;
3304
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003305 trace_i915_gem_object_change_domain(obj,
3306 old_read_domains,
3307 obj->write_domain);
3308
Eric Anholt673a3942008-07-30 12:06:12 -07003309 return 0;
3310}
3311
3312/**
Eric Anholt673a3942008-07-30 12:06:12 -07003313 * Pin an object to the GTT and evaluate the relocations landing in it.
3314 */
3315static int
3316i915_gem_object_pin_and_relocate(struct drm_gem_object *obj,
3317 struct drm_file *file_priv,
Jesse Barnes76446ca2009-12-17 22:05:42 -05003318 struct drm_i915_gem_exec_object2 *entry,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003319 struct drm_i915_gem_relocation_entry *relocs)
Eric Anholt673a3942008-07-30 12:06:12 -07003320{
3321 struct drm_device *dev = obj->dev;
Keith Packard0839ccb2008-10-30 19:38:48 -07003322 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01003323 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003324 int i, ret;
Keith Packard0839ccb2008-10-30 19:38:48 -07003325 void __iomem *reloc_page;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003326 bool need_fence;
3327
3328 need_fence = entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
3329 obj_priv->tiling_mode != I915_TILING_NONE;
3330
3331 /* Check fence reg constraints and rebind if necessary */
Chris Wilson808b24d62010-05-27 13:18:15 +01003332 if (need_fence &&
3333 !i915_gem_object_fence_offset_ok(obj,
3334 obj_priv->tiling_mode)) {
3335 ret = i915_gem_object_unbind(obj);
3336 if (ret)
3337 return ret;
3338 }
Eric Anholt673a3942008-07-30 12:06:12 -07003339
3340 /* Choose the GTT offset for our buffer and put it there. */
3341 ret = i915_gem_object_pin(obj, (uint32_t) entry->alignment);
3342 if (ret)
3343 return ret;
3344
Jesse Barnes76446ca2009-12-17 22:05:42 -05003345 /*
3346 * Pre-965 chips need a fence register set up in order to
3347 * properly handle blits to/from tiled surfaces.
3348 */
3349 if (need_fence) {
3350 ret = i915_gem_object_get_fence_reg(obj);
3351 if (ret != 0) {
Jesse Barnes76446ca2009-12-17 22:05:42 -05003352 i915_gem_object_unpin(obj);
3353 return ret;
3354 }
3355 }
3356
Eric Anholt673a3942008-07-30 12:06:12 -07003357 entry->offset = obj_priv->gtt_offset;
3358
Eric Anholt673a3942008-07-30 12:06:12 -07003359 /* Apply the relocations, using the GTT aperture to avoid cache
3360 * flushing requirements.
3361 */
3362 for (i = 0; i < entry->relocation_count; i++) {
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003363 struct drm_i915_gem_relocation_entry *reloc= &relocs[i];
Eric Anholt673a3942008-07-30 12:06:12 -07003364 struct drm_gem_object *target_obj;
3365 struct drm_i915_gem_object *target_obj_priv;
Eric Anholt3043c602008-10-02 12:24:47 -07003366 uint32_t reloc_val, reloc_offset;
3367 uint32_t __iomem *reloc_entry;
Eric Anholt673a3942008-07-30 12:06:12 -07003368
Eric Anholt673a3942008-07-30 12:06:12 -07003369 target_obj = drm_gem_object_lookup(obj->dev, file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003370 reloc->target_handle);
Eric Anholt673a3942008-07-30 12:06:12 -07003371 if (target_obj == NULL) {
3372 i915_gem_object_unpin(obj);
3373 return -EBADF;
3374 }
Daniel Vetter23010e42010-03-08 13:35:02 +01003375 target_obj_priv = to_intel_bo(target_obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003376
Chris Wilson8542a0b2009-09-09 21:15:15 +01003377#if WATCH_RELOC
3378 DRM_INFO("%s: obj %p offset %08x target %d "
3379 "read %08x write %08x gtt %08x "
3380 "presumed %08x delta %08x\n",
3381 __func__,
3382 obj,
3383 (int) reloc->offset,
3384 (int) reloc->target_handle,
3385 (int) reloc->read_domains,
3386 (int) reloc->write_domain,
3387 (int) target_obj_priv->gtt_offset,
3388 (int) reloc->presumed_offset,
3389 reloc->delta);
3390#endif
3391
Eric Anholt673a3942008-07-30 12:06:12 -07003392 /* The target buffer should have appeared before us in the
3393 * exec_object list, so it should have a GTT space bound by now.
3394 */
3395 if (target_obj_priv->gtt_space == NULL) {
3396 DRM_ERROR("No GTT space found for object %d\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003397 reloc->target_handle);
Eric Anholt673a3942008-07-30 12:06:12 -07003398 drm_gem_object_unreference(target_obj);
3399 i915_gem_object_unpin(obj);
3400 return -EINVAL;
3401 }
3402
Chris Wilson8542a0b2009-09-09 21:15:15 +01003403 /* Validate that the target is in a valid r/w GPU domain */
Daniel Vetter16edd552010-02-19 11:52:02 +01003404 if (reloc->write_domain & (reloc->write_domain - 1)) {
3405 DRM_ERROR("reloc with multiple write domains: "
3406 "obj %p target %d offset %d "
3407 "read %08x write %08x",
3408 obj, reloc->target_handle,
3409 (int) reloc->offset,
3410 reloc->read_domains,
3411 reloc->write_domain);
3412 return -EINVAL;
3413 }
Chris Wilson8542a0b2009-09-09 21:15:15 +01003414 if (reloc->write_domain & I915_GEM_DOMAIN_CPU ||
3415 reloc->read_domains & I915_GEM_DOMAIN_CPU) {
3416 DRM_ERROR("reloc with read/write CPU domains: "
3417 "obj %p target %d offset %d "
3418 "read %08x write %08x",
3419 obj, reloc->target_handle,
3420 (int) reloc->offset,
3421 reloc->read_domains,
3422 reloc->write_domain);
3423 drm_gem_object_unreference(target_obj);
3424 i915_gem_object_unpin(obj);
3425 return -EINVAL;
3426 }
3427 if (reloc->write_domain && target_obj->pending_write_domain &&
3428 reloc->write_domain != target_obj->pending_write_domain) {
3429 DRM_ERROR("Write domain conflict: "
3430 "obj %p target %d offset %d "
3431 "new %08x old %08x\n",
3432 obj, reloc->target_handle,
3433 (int) reloc->offset,
3434 reloc->write_domain,
3435 target_obj->pending_write_domain);
3436 drm_gem_object_unreference(target_obj);
3437 i915_gem_object_unpin(obj);
3438 return -EINVAL;
3439 }
3440
3441 target_obj->pending_read_domains |= reloc->read_domains;
3442 target_obj->pending_write_domain |= reloc->write_domain;
3443
3444 /* If the relocation already has the right value in it, no
3445 * more work needs to be done.
3446 */
3447 if (target_obj_priv->gtt_offset == reloc->presumed_offset) {
3448 drm_gem_object_unreference(target_obj);
3449 continue;
3450 }
3451
3452 /* Check that the relocation address is valid... */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003453 if (reloc->offset > obj->size - 4) {
Eric Anholt673a3942008-07-30 12:06:12 -07003454 DRM_ERROR("Relocation beyond object bounds: "
3455 "obj %p target %d offset %d size %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003456 obj, reloc->target_handle,
3457 (int) reloc->offset, (int) obj->size);
Eric Anholt673a3942008-07-30 12:06:12 -07003458 drm_gem_object_unreference(target_obj);
3459 i915_gem_object_unpin(obj);
3460 return -EINVAL;
3461 }
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003462 if (reloc->offset & 3) {
Eric Anholt673a3942008-07-30 12:06:12 -07003463 DRM_ERROR("Relocation not 4-byte aligned: "
3464 "obj %p target %d offset %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003465 obj, reloc->target_handle,
3466 (int) reloc->offset);
Eric Anholt673a3942008-07-30 12:06:12 -07003467 drm_gem_object_unreference(target_obj);
3468 i915_gem_object_unpin(obj);
3469 return -EINVAL;
3470 }
3471
Chris Wilson8542a0b2009-09-09 21:15:15 +01003472 /* and points to somewhere within the target object. */
Chris Wilsoncd0b9fb2009-09-15 23:23:18 +01003473 if (reloc->delta >= target_obj->size) {
3474 DRM_ERROR("Relocation beyond target object bounds: "
3475 "obj %p target %d delta %d size %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003476 obj, reloc->target_handle,
Chris Wilsoncd0b9fb2009-09-15 23:23:18 +01003477 (int) reloc->delta, (int) target_obj->size);
Chris Wilson491152b2009-02-11 14:26:32 +00003478 drm_gem_object_unreference(target_obj);
3479 i915_gem_object_unpin(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003480 return -EINVAL;
3481 }
3482
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003483 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
3484 if (ret != 0) {
3485 drm_gem_object_unreference(target_obj);
3486 i915_gem_object_unpin(obj);
3487 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -07003488 }
3489
3490 /* Map the page containing the relocation we're going to
3491 * perform.
3492 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003493 reloc_offset = obj_priv->gtt_offset + reloc->offset;
Keith Packard0839ccb2008-10-30 19:38:48 -07003494 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
3495 (reloc_offset &
Chris Wilsonfca3ec02010-08-04 14:34:24 +01003496 ~(PAGE_SIZE - 1)),
3497 KM_USER0);
Eric Anholt3043c602008-10-02 12:24:47 -07003498 reloc_entry = (uint32_t __iomem *)(reloc_page +
Keith Packard0839ccb2008-10-30 19:38:48 -07003499 (reloc_offset & (PAGE_SIZE - 1)));
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003500 reloc_val = target_obj_priv->gtt_offset + reloc->delta;
Eric Anholt673a3942008-07-30 12:06:12 -07003501
3502#if WATCH_BUF
3503 DRM_INFO("Applied relocation: %p@0x%08x %08x -> %08x\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003504 obj, (unsigned int) reloc->offset,
Eric Anholt673a3942008-07-30 12:06:12 -07003505 readl(reloc_entry), reloc_val);
3506#endif
3507 writel(reloc_val, reloc_entry);
Chris Wilsonfca3ec02010-08-04 14:34:24 +01003508 io_mapping_unmap_atomic(reloc_page, KM_USER0);
Eric Anholt673a3942008-07-30 12:06:12 -07003509
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003510 /* The updated presumed offset for this entry will be
3511 * copied back out to the user.
Eric Anholt673a3942008-07-30 12:06:12 -07003512 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003513 reloc->presumed_offset = target_obj_priv->gtt_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003514
3515 drm_gem_object_unreference(target_obj);
3516 }
3517
Eric Anholt673a3942008-07-30 12:06:12 -07003518#if WATCH_BUF
3519 if (0)
3520 i915_gem_dump_object(obj, 128, __func__, ~0);
3521#endif
3522 return 0;
3523}
3524
Eric Anholt673a3942008-07-30 12:06:12 -07003525/* Throttle our rendering by waiting until the ring has completed our requests
3526 * emitted over 20 msec ago.
3527 *
Eric Anholtb9624422009-06-03 07:27:35 +00003528 * Note that if we were to use the current jiffies each time around the loop,
3529 * we wouldn't escape the function with any frames outstanding if the time to
3530 * render a frame was over 20ms.
3531 *
Eric Anholt673a3942008-07-30 12:06:12 -07003532 * This should get us reasonable parallelism between CPU and GPU but also
3533 * relatively low latency when blocking on a particular request to finish.
3534 */
3535static int
3536i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file_priv)
3537{
3538 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
3539 int ret = 0;
Eric Anholtb9624422009-06-03 07:27:35 +00003540 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
Eric Anholt673a3942008-07-30 12:06:12 -07003541
3542 mutex_lock(&dev->struct_mutex);
Eric Anholtb9624422009-06-03 07:27:35 +00003543 while (!list_empty(&i915_file_priv->mm.request_list)) {
3544 struct drm_i915_gem_request *request;
3545
3546 request = list_first_entry(&i915_file_priv->mm.request_list,
3547 struct drm_i915_gem_request,
3548 client_list);
3549
3550 if (time_after_eq(request->emitted_jiffies, recent_enough))
3551 break;
3552
Zou Nan hai852835f2010-05-21 09:08:56 +08003553 ret = i915_wait_request(dev, request->seqno, request->ring);
Eric Anholtb9624422009-06-03 07:27:35 +00003554 if (ret != 0)
3555 break;
3556 }
Eric Anholt673a3942008-07-30 12:06:12 -07003557 mutex_unlock(&dev->struct_mutex);
Eric Anholtb9624422009-06-03 07:27:35 +00003558
Eric Anholt673a3942008-07-30 12:06:12 -07003559 return ret;
3560}
3561
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003562static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003563i915_gem_get_relocs_from_user(struct drm_i915_gem_exec_object2 *exec_list,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003564 uint32_t buffer_count,
3565 struct drm_i915_gem_relocation_entry **relocs)
3566{
3567 uint32_t reloc_count = 0, reloc_index = 0, i;
3568 int ret;
3569
3570 *relocs = NULL;
3571 for (i = 0; i < buffer_count; i++) {
3572 if (reloc_count + exec_list[i].relocation_count < reloc_count)
3573 return -EINVAL;
3574 reloc_count += exec_list[i].relocation_count;
3575 }
3576
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003577 *relocs = drm_calloc_large(reloc_count, sizeof(**relocs));
Jesse Barnes76446ca2009-12-17 22:05:42 -05003578 if (*relocs == NULL) {
3579 DRM_ERROR("failed to alloc relocs, count %d\n", reloc_count);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003580 return -ENOMEM;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003581 }
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003582
3583 for (i = 0; i < buffer_count; i++) {
3584 struct drm_i915_gem_relocation_entry __user *user_relocs;
3585
3586 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3587
3588 ret = copy_from_user(&(*relocs)[reloc_index],
3589 user_relocs,
3590 exec_list[i].relocation_count *
3591 sizeof(**relocs));
3592 if (ret != 0) {
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003593 drm_free_large(*relocs);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003594 *relocs = NULL;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003595 return -EFAULT;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003596 }
3597
3598 reloc_index += exec_list[i].relocation_count;
3599 }
3600
Florian Mickler2bc43b52009-04-06 22:55:41 +02003601 return 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003602}
3603
3604static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003605i915_gem_put_relocs_to_user(struct drm_i915_gem_exec_object2 *exec_list,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003606 uint32_t buffer_count,
3607 struct drm_i915_gem_relocation_entry *relocs)
3608{
3609 uint32_t reloc_count = 0, i;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003610 int ret = 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003611
Chris Wilson93533c22010-01-31 10:40:48 +00003612 if (relocs == NULL)
3613 return 0;
3614
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003615 for (i = 0; i < buffer_count; i++) {
3616 struct drm_i915_gem_relocation_entry __user *user_relocs;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003617 int unwritten;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003618
3619 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3620
Florian Mickler2bc43b52009-04-06 22:55:41 +02003621 unwritten = copy_to_user(user_relocs,
3622 &relocs[reloc_count],
3623 exec_list[i].relocation_count *
3624 sizeof(*relocs));
3625
3626 if (unwritten) {
3627 ret = -EFAULT;
3628 goto err;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003629 }
3630
3631 reloc_count += exec_list[i].relocation_count;
3632 }
3633
Florian Mickler2bc43b52009-04-06 22:55:41 +02003634err:
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003635 drm_free_large(relocs);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003636
3637 return ret;
3638}
3639
Chris Wilson83d60792009-06-06 09:45:57 +01003640static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003641i915_gem_check_execbuffer (struct drm_i915_gem_execbuffer2 *exec,
Chris Wilson83d60792009-06-06 09:45:57 +01003642 uint64_t exec_offset)
3643{
3644 uint32_t exec_start, exec_len;
3645
3646 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
3647 exec_len = (uint32_t) exec->batch_len;
3648
3649 if ((exec_start | exec_len) & 0x7)
3650 return -EINVAL;
3651
3652 if (!exec_start)
3653 return -EINVAL;
3654
3655 return 0;
3656}
3657
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003658static int
3659i915_gem_wait_for_pending_flip(struct drm_device *dev,
3660 struct drm_gem_object **object_list,
3661 int count)
3662{
3663 drm_i915_private_t *dev_priv = dev->dev_private;
3664 struct drm_i915_gem_object *obj_priv;
3665 DEFINE_WAIT(wait);
3666 int i, ret = 0;
3667
3668 for (;;) {
3669 prepare_to_wait(&dev_priv->pending_flip_queue,
3670 &wait, TASK_INTERRUPTIBLE);
3671 for (i = 0; i < count; i++) {
Daniel Vetter23010e42010-03-08 13:35:02 +01003672 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003673 if (atomic_read(&obj_priv->pending_flip) > 0)
3674 break;
3675 }
3676 if (i == count)
3677 break;
3678
3679 if (!signal_pending(current)) {
3680 mutex_unlock(&dev->struct_mutex);
3681 schedule();
3682 mutex_lock(&dev->struct_mutex);
3683 continue;
3684 }
3685 ret = -ERESTARTSYS;
3686 break;
3687 }
3688 finish_wait(&dev_priv->pending_flip_queue, &wait);
3689
3690 return ret;
3691}
3692
Chris Wilson43b27f42010-07-02 08:57:15 +01003693
Eric Anholt673a3942008-07-30 12:06:12 -07003694int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003695i915_gem_do_execbuffer(struct drm_device *dev, void *data,
3696 struct drm_file *file_priv,
3697 struct drm_i915_gem_execbuffer2 *args,
3698 struct drm_i915_gem_exec_object2 *exec_list)
Eric Anholt673a3942008-07-30 12:06:12 -07003699{
3700 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003701 struct drm_gem_object **object_list = NULL;
3702 struct drm_gem_object *batch_obj;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003703 struct drm_i915_gem_object *obj_priv;
Eric Anholt201361a2009-03-11 12:30:04 -07003704 struct drm_clip_rect *cliprects = NULL;
Chris Wilson93533c22010-01-31 10:40:48 +00003705 struct drm_i915_gem_relocation_entry *relocs = NULL;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003706 int ret = 0, ret2, i, pinned = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003707 uint64_t exec_offset;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003708 uint32_t seqno, flush_domains, reloc_index;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003709 int pin_tries, flips;
Eric Anholt673a3942008-07-30 12:06:12 -07003710
Zou Nan hai852835f2010-05-21 09:08:56 +08003711 struct intel_ring_buffer *ring = NULL;
3712
Eric Anholt673a3942008-07-30 12:06:12 -07003713#if WATCH_EXEC
3714 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3715 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3716#endif
Zou Nan haid1b851f2010-05-21 09:08:57 +08003717 if (args->flags & I915_EXEC_BSD) {
3718 if (!HAS_BSD(dev)) {
3719 DRM_ERROR("execbuf with wrong flag\n");
3720 return -EINVAL;
3721 }
3722 ring = &dev_priv->bsd_ring;
3723 } else {
3724 ring = &dev_priv->render_ring;
3725 }
3726
Eric Anholt4f481ed2008-09-10 14:22:49 -07003727 if (args->buffer_count < 1) {
3728 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3729 return -EINVAL;
3730 }
Eric Anholtc8e0f932009-11-22 03:49:37 +01003731 object_list = drm_malloc_ab(sizeof(*object_list), args->buffer_count);
Jesse Barnes76446ca2009-12-17 22:05:42 -05003732 if (object_list == NULL) {
3733 DRM_ERROR("Failed to allocate object list for %d buffers\n",
Eric Anholt673a3942008-07-30 12:06:12 -07003734 args->buffer_count);
3735 ret = -ENOMEM;
3736 goto pre_mutex_err;
3737 }
Eric Anholt673a3942008-07-30 12:06:12 -07003738
Eric Anholt201361a2009-03-11 12:30:04 -07003739 if (args->num_cliprects != 0) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003740 cliprects = kcalloc(args->num_cliprects, sizeof(*cliprects),
3741 GFP_KERNEL);
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003742 if (cliprects == NULL) {
3743 ret = -ENOMEM;
Eric Anholt201361a2009-03-11 12:30:04 -07003744 goto pre_mutex_err;
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003745 }
Eric Anholt201361a2009-03-11 12:30:04 -07003746
3747 ret = copy_from_user(cliprects,
3748 (struct drm_clip_rect __user *)
3749 (uintptr_t) args->cliprects_ptr,
3750 sizeof(*cliprects) * args->num_cliprects);
3751 if (ret != 0) {
3752 DRM_ERROR("copy %d cliprects failed: %d\n",
3753 args->num_cliprects, ret);
3754 goto pre_mutex_err;
3755 }
3756 }
3757
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003758 ret = i915_gem_get_relocs_from_user(exec_list, args->buffer_count,
3759 &relocs);
3760 if (ret != 0)
3761 goto pre_mutex_err;
3762
Eric Anholt673a3942008-07-30 12:06:12 -07003763 mutex_lock(&dev->struct_mutex);
3764
3765 i915_verify_inactive(dev, __FILE__, __LINE__);
3766
Ben Gamariba1234d2009-09-14 17:48:47 -04003767 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07003768 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003769 ret = -EIO;
3770 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003771 }
3772
3773 if (dev_priv->mm.suspended) {
Eric Anholt673a3942008-07-30 12:06:12 -07003774 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003775 ret = -EBUSY;
3776 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003777 }
3778
Keith Packardac94a962008-11-20 23:30:27 -08003779 /* Look up object handles */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003780 flips = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003781 for (i = 0; i < args->buffer_count; i++) {
3782 object_list[i] = drm_gem_object_lookup(dev, file_priv,
3783 exec_list[i].handle);
3784 if (object_list[i] == NULL) {
3785 DRM_ERROR("Invalid object handle %d at index %d\n",
3786 exec_list[i].handle, i);
Chris Wilson0ce907f2010-01-23 20:26:35 +00003787 /* prevent error path from reading uninitialized data */
3788 args->buffer_count = i + 1;
Eric Anholt673a3942008-07-30 12:06:12 -07003789 ret = -EBADF;
3790 goto err;
3791 }
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003792
Daniel Vetter23010e42010-03-08 13:35:02 +01003793 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003794 if (obj_priv->in_execbuffer) {
3795 DRM_ERROR("Object %p appears more than once in object list\n",
3796 object_list[i]);
Chris Wilson0ce907f2010-01-23 20:26:35 +00003797 /* prevent error path from reading uninitialized data */
3798 args->buffer_count = i + 1;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003799 ret = -EBADF;
3800 goto err;
3801 }
3802 obj_priv->in_execbuffer = true;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003803 flips += atomic_read(&obj_priv->pending_flip);
3804 }
3805
3806 if (flips > 0) {
3807 ret = i915_gem_wait_for_pending_flip(dev, object_list,
3808 args->buffer_count);
3809 if (ret)
3810 goto err;
Keith Packardac94a962008-11-20 23:30:27 -08003811 }
Eric Anholt673a3942008-07-30 12:06:12 -07003812
Keith Packardac94a962008-11-20 23:30:27 -08003813 /* Pin and relocate */
3814 for (pin_tries = 0; ; pin_tries++) {
3815 ret = 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003816 reloc_index = 0;
3817
Keith Packardac94a962008-11-20 23:30:27 -08003818 for (i = 0; i < args->buffer_count; i++) {
3819 object_list[i]->pending_read_domains = 0;
3820 object_list[i]->pending_write_domain = 0;
3821 ret = i915_gem_object_pin_and_relocate(object_list[i],
3822 file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003823 &exec_list[i],
3824 &relocs[reloc_index]);
Keith Packardac94a962008-11-20 23:30:27 -08003825 if (ret)
3826 break;
3827 pinned = i + 1;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003828 reloc_index += exec_list[i].relocation_count;
Keith Packardac94a962008-11-20 23:30:27 -08003829 }
3830 /* success */
3831 if (ret == 0)
3832 break;
3833
3834 /* error other than GTT full, or we've already tried again */
Chris Wilson2939e1f2009-06-06 09:46:03 +01003835 if (ret != -ENOSPC || pin_tries >= 1) {
Chris Wilson07f73f62009-09-14 16:50:30 +01003836 if (ret != -ERESTARTSYS) {
3837 unsigned long long total_size = 0;
Chris Wilson3d1cc472010-05-27 13:18:19 +01003838 int num_fences = 0;
3839 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson43b27f42010-07-02 08:57:15 +01003840 obj_priv = to_intel_bo(object_list[i]);
Chris Wilson3d1cc472010-05-27 13:18:19 +01003841
Chris Wilson07f73f62009-09-14 16:50:30 +01003842 total_size += object_list[i]->size;
Chris Wilson3d1cc472010-05-27 13:18:19 +01003843 num_fences +=
3844 exec_list[i].flags & EXEC_OBJECT_NEEDS_FENCE &&
3845 obj_priv->tiling_mode != I915_TILING_NONE;
3846 }
3847 DRM_ERROR("Failed to pin buffer %d of %d, total %llu bytes, %d fences: %d\n",
Chris Wilson07f73f62009-09-14 16:50:30 +01003848 pinned+1, args->buffer_count,
Chris Wilson3d1cc472010-05-27 13:18:19 +01003849 total_size, num_fences,
3850 ret);
Chris Wilson07f73f62009-09-14 16:50:30 +01003851 DRM_ERROR("%d objects [%d pinned], "
3852 "%d object bytes [%d pinned], "
3853 "%d/%d gtt bytes\n",
3854 atomic_read(&dev->object_count),
3855 atomic_read(&dev->pin_count),
3856 atomic_read(&dev->object_memory),
3857 atomic_read(&dev->pin_memory),
3858 atomic_read(&dev->gtt_memory),
3859 dev->gtt_total);
3860 }
Eric Anholt673a3942008-07-30 12:06:12 -07003861 goto err;
3862 }
Keith Packardac94a962008-11-20 23:30:27 -08003863
3864 /* unpin all of our buffers */
3865 for (i = 0; i < pinned; i++)
3866 i915_gem_object_unpin(object_list[i]);
Eric Anholtb1177632008-12-10 10:09:41 -08003867 pinned = 0;
Keith Packardac94a962008-11-20 23:30:27 -08003868
3869 /* evict everyone we can from the aperture */
3870 ret = i915_gem_evict_everything(dev);
Chris Wilson07f73f62009-09-14 16:50:30 +01003871 if (ret && ret != -ENOSPC)
Keith Packardac94a962008-11-20 23:30:27 -08003872 goto err;
Eric Anholt673a3942008-07-30 12:06:12 -07003873 }
3874
3875 /* Set the pending read domains for the batch buffer to COMMAND */
3876 batch_obj = object_list[args->buffer_count-1];
Chris Wilson5f26a2c2009-06-06 09:45:58 +01003877 if (batch_obj->pending_write_domain) {
3878 DRM_ERROR("Attempting to use self-modifying batch buffer\n");
3879 ret = -EINVAL;
3880 goto err;
3881 }
3882 batch_obj->pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
Eric Anholt673a3942008-07-30 12:06:12 -07003883
Chris Wilson83d60792009-06-06 09:45:57 +01003884 /* Sanity check the batch buffer, prior to moving objects */
3885 exec_offset = exec_list[args->buffer_count - 1].offset;
3886 ret = i915_gem_check_execbuffer (args, exec_offset);
3887 if (ret != 0) {
3888 DRM_ERROR("execbuf with invalid offset/length\n");
3889 goto err;
3890 }
3891
Eric Anholt673a3942008-07-30 12:06:12 -07003892 i915_verify_inactive(dev, __FILE__, __LINE__);
3893
Keith Packard646f0f62008-11-20 23:23:03 -08003894 /* Zero the global flush/invalidate flags. These
3895 * will be modified as new domains are computed
3896 * for each object
3897 */
3898 dev->invalidate_domains = 0;
3899 dev->flush_domains = 0;
Chris Wilson88f356b2010-08-04 13:55:32 +01003900 dev_priv->flush_rings = 0;
Keith Packard646f0f62008-11-20 23:23:03 -08003901
Eric Anholt673a3942008-07-30 12:06:12 -07003902 for (i = 0; i < args->buffer_count; i++) {
3903 struct drm_gem_object *obj = object_list[i];
Eric Anholt673a3942008-07-30 12:06:12 -07003904
Keith Packard646f0f62008-11-20 23:23:03 -08003905 /* Compute new gpu domains and update invalidate/flush */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003906 i915_gem_object_set_to_gpu_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003907 }
3908
3909 i915_verify_inactive(dev, __FILE__, __LINE__);
3910
Keith Packard646f0f62008-11-20 23:23:03 -08003911 if (dev->invalidate_domains | dev->flush_domains) {
3912#if WATCH_EXEC
3913 DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
3914 __func__,
3915 dev->invalidate_domains,
3916 dev->flush_domains);
3917#endif
3918 i915_gem_flush(dev,
3919 dev->invalidate_domains,
3920 dev->flush_domains);
Chris Wilson88f356b2010-08-04 13:55:32 +01003921 if (dev_priv->flush_rings & FLUSH_RENDER_RING)
Eric Anholtb9624422009-06-03 07:27:35 +00003922 (void)i915_add_request(dev, file_priv,
Chris Wilson88f356b2010-08-04 13:55:32 +01003923 dev->flush_domains,
3924 &dev_priv->render_ring);
3925 if (dev_priv->flush_rings & FLUSH_BSD_RING)
3926 (void)i915_add_request(dev, file_priv,
3927 dev->flush_domains,
3928 &dev_priv->bsd_ring);
Keith Packard646f0f62008-11-20 23:23:03 -08003929 }
Eric Anholt673a3942008-07-30 12:06:12 -07003930
Eric Anholtefbeed92009-02-19 14:54:51 -08003931 for (i = 0; i < args->buffer_count; i++) {
3932 struct drm_gem_object *obj = object_list[i];
Daniel Vetter23010e42010-03-08 13:35:02 +01003933 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003934 uint32_t old_write_domain = obj->write_domain;
Eric Anholtefbeed92009-02-19 14:54:51 -08003935
3936 obj->write_domain = obj->pending_write_domain;
Daniel Vetter99fcb762010-02-07 16:20:18 +01003937 if (obj->write_domain)
3938 list_move_tail(&obj_priv->gpu_write_list,
3939 &dev_priv->mm.gpu_write_list);
3940 else
3941 list_del_init(&obj_priv->gpu_write_list);
3942
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003943 trace_i915_gem_object_change_domain(obj,
3944 obj->read_domains,
3945 old_write_domain);
Eric Anholtefbeed92009-02-19 14:54:51 -08003946 }
3947
Eric Anholt673a3942008-07-30 12:06:12 -07003948 i915_verify_inactive(dev, __FILE__, __LINE__);
3949
3950#if WATCH_COHERENCY
3951 for (i = 0; i < args->buffer_count; i++) {
3952 i915_gem_object_check_coherency(object_list[i],
3953 exec_list[i].handle);
3954 }
3955#endif
3956
Eric Anholt673a3942008-07-30 12:06:12 -07003957#if WATCH_EXEC
Ben Gamari6911a9b2009-04-02 11:24:54 -07003958 i915_gem_dump_object(batch_obj,
Eric Anholt673a3942008-07-30 12:06:12 -07003959 args->batch_len,
3960 __func__,
3961 ~0);
3962#endif
3963
Eric Anholt673a3942008-07-30 12:06:12 -07003964 /* Exec the batchbuffer */
Zou Nan hai852835f2010-05-21 09:08:56 +08003965 ret = ring->dispatch_gem_execbuffer(dev, ring, args,
3966 cliprects, exec_offset);
Eric Anholt673a3942008-07-30 12:06:12 -07003967 if (ret) {
3968 DRM_ERROR("dispatch failed %d\n", ret);
3969 goto err;
3970 }
3971
3972 /*
3973 * Ensure that the commands in the batch buffer are
3974 * finished before the interrupt fires
3975 */
Zou Nan hai852835f2010-05-21 09:08:56 +08003976 flush_domains = i915_retire_commands(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07003977
3978 i915_verify_inactive(dev, __FILE__, __LINE__);
3979
3980 /*
3981 * Get a seqno representing the execution of the current buffer,
3982 * which we can wait on. We would like to mitigate these interrupts,
3983 * likely by only creating seqnos occasionally (so that we have
3984 * *some* interrupts representing completion of buffers that we can
3985 * wait on when trying to clear up gtt space).
3986 */
Zou Nan hai852835f2010-05-21 09:08:56 +08003987 seqno = i915_add_request(dev, file_priv, flush_domains, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07003988 BUG_ON(seqno == 0);
Eric Anholt673a3942008-07-30 12:06:12 -07003989 for (i = 0; i < args->buffer_count; i++) {
3990 struct drm_gem_object *obj = object_list[i];
Zou Nan hai852835f2010-05-21 09:08:56 +08003991 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003992
Zou Nan hai852835f2010-05-21 09:08:56 +08003993 i915_gem_object_move_to_active(obj, seqno, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07003994#if WATCH_LRU
3995 DRM_INFO("%s: move to exec list %p\n", __func__, obj);
3996#endif
3997 }
3998#if WATCH_LRU
3999 i915_dump_lru(dev, __func__);
4000#endif
4001
4002 i915_verify_inactive(dev, __FILE__, __LINE__);
4003
Eric Anholt673a3942008-07-30 12:06:12 -07004004err:
Julia Lawallaad87df2008-12-21 16:28:47 +01004005 for (i = 0; i < pinned; i++)
4006 i915_gem_object_unpin(object_list[i]);
Eric Anholt673a3942008-07-30 12:06:12 -07004007
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05004008 for (i = 0; i < args->buffer_count; i++) {
4009 if (object_list[i]) {
Daniel Vetter23010e42010-03-08 13:35:02 +01004010 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05004011 obj_priv->in_execbuffer = false;
4012 }
Julia Lawallaad87df2008-12-21 16:28:47 +01004013 drm_gem_object_unreference(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05004014 }
Julia Lawallaad87df2008-12-21 16:28:47 +01004015
Eric Anholt673a3942008-07-30 12:06:12 -07004016 mutex_unlock(&dev->struct_mutex);
4017
Chris Wilson93533c22010-01-31 10:40:48 +00004018pre_mutex_err:
Eric Anholt40a5f0d2009-03-12 11:23:52 -07004019 /* Copy the updated relocations out regardless of current error
4020 * state. Failure to update the relocs would mean that the next
4021 * time userland calls execbuf, it would do so with presumed offset
4022 * state that didn't match the actual object state.
4023 */
4024 ret2 = i915_gem_put_relocs_to_user(exec_list, args->buffer_count,
4025 relocs);
4026 if (ret2 != 0) {
4027 DRM_ERROR("Failed to copy relocations back out: %d\n", ret2);
4028
4029 if (ret == 0)
4030 ret = ret2;
4031 }
4032
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07004033 drm_free_large(object_list);
Eric Anholt9a298b22009-03-24 12:23:04 -07004034 kfree(cliprects);
Eric Anholt673a3942008-07-30 12:06:12 -07004035
4036 return ret;
4037}
4038
Jesse Barnes76446ca2009-12-17 22:05:42 -05004039/*
4040 * Legacy execbuffer just creates an exec2 list from the original exec object
4041 * list array and passes it to the real function.
4042 */
4043int
4044i915_gem_execbuffer(struct drm_device *dev, void *data,
4045 struct drm_file *file_priv)
4046{
4047 struct drm_i915_gem_execbuffer *args = data;
4048 struct drm_i915_gem_execbuffer2 exec2;
4049 struct drm_i915_gem_exec_object *exec_list = NULL;
4050 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
4051 int ret, i;
4052
4053#if WATCH_EXEC
4054 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
4055 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
4056#endif
4057
4058 if (args->buffer_count < 1) {
4059 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
4060 return -EINVAL;
4061 }
4062
4063 /* Copy in the exec list from userland */
4064 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
4065 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
4066 if (exec_list == NULL || exec2_list == NULL) {
4067 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
4068 args->buffer_count);
4069 drm_free_large(exec_list);
4070 drm_free_large(exec2_list);
4071 return -ENOMEM;
4072 }
4073 ret = copy_from_user(exec_list,
4074 (struct drm_i915_relocation_entry __user *)
4075 (uintptr_t) args->buffers_ptr,
4076 sizeof(*exec_list) * args->buffer_count);
4077 if (ret != 0) {
4078 DRM_ERROR("copy %d exec entries failed %d\n",
4079 args->buffer_count, ret);
4080 drm_free_large(exec_list);
4081 drm_free_large(exec2_list);
4082 return -EFAULT;
4083 }
4084
4085 for (i = 0; i < args->buffer_count; i++) {
4086 exec2_list[i].handle = exec_list[i].handle;
4087 exec2_list[i].relocation_count = exec_list[i].relocation_count;
4088 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
4089 exec2_list[i].alignment = exec_list[i].alignment;
4090 exec2_list[i].offset = exec_list[i].offset;
4091 if (!IS_I965G(dev))
4092 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
4093 else
4094 exec2_list[i].flags = 0;
4095 }
4096
4097 exec2.buffers_ptr = args->buffers_ptr;
4098 exec2.buffer_count = args->buffer_count;
4099 exec2.batch_start_offset = args->batch_start_offset;
4100 exec2.batch_len = args->batch_len;
4101 exec2.DR1 = args->DR1;
4102 exec2.DR4 = args->DR4;
4103 exec2.num_cliprects = args->num_cliprects;
4104 exec2.cliprects_ptr = args->cliprects_ptr;
Zou Nan hai852835f2010-05-21 09:08:56 +08004105 exec2.flags = I915_EXEC_RENDER;
Jesse Barnes76446ca2009-12-17 22:05:42 -05004106
4107 ret = i915_gem_do_execbuffer(dev, data, file_priv, &exec2, exec2_list);
4108 if (!ret) {
4109 /* Copy the new buffer offsets back to the user's exec list. */
4110 for (i = 0; i < args->buffer_count; i++)
4111 exec_list[i].offset = exec2_list[i].offset;
4112 /* ... and back out to userspace */
4113 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
4114 (uintptr_t) args->buffers_ptr,
4115 exec_list,
4116 sizeof(*exec_list) * args->buffer_count);
4117 if (ret) {
4118 ret = -EFAULT;
4119 DRM_ERROR("failed to copy %d exec entries "
4120 "back to user (%d)\n",
4121 args->buffer_count, ret);
4122 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05004123 }
4124
4125 drm_free_large(exec_list);
4126 drm_free_large(exec2_list);
4127 return ret;
4128}
4129
4130int
4131i915_gem_execbuffer2(struct drm_device *dev, void *data,
4132 struct drm_file *file_priv)
4133{
4134 struct drm_i915_gem_execbuffer2 *args = data;
4135 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
4136 int ret;
4137
4138#if WATCH_EXEC
4139 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
4140 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
4141#endif
4142
4143 if (args->buffer_count < 1) {
4144 DRM_ERROR("execbuf2 with %d buffers\n", args->buffer_count);
4145 return -EINVAL;
4146 }
4147
4148 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
4149 if (exec2_list == NULL) {
4150 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
4151 args->buffer_count);
4152 return -ENOMEM;
4153 }
4154 ret = copy_from_user(exec2_list,
4155 (struct drm_i915_relocation_entry __user *)
4156 (uintptr_t) args->buffers_ptr,
4157 sizeof(*exec2_list) * args->buffer_count);
4158 if (ret != 0) {
4159 DRM_ERROR("copy %d exec entries failed %d\n",
4160 args->buffer_count, ret);
4161 drm_free_large(exec2_list);
4162 return -EFAULT;
4163 }
4164
4165 ret = i915_gem_do_execbuffer(dev, data, file_priv, args, exec2_list);
4166 if (!ret) {
4167 /* Copy the new buffer offsets back to the user's exec list. */
4168 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
4169 (uintptr_t) args->buffers_ptr,
4170 exec2_list,
4171 sizeof(*exec2_list) * args->buffer_count);
4172 if (ret) {
4173 ret = -EFAULT;
4174 DRM_ERROR("failed to copy %d exec entries "
4175 "back to user (%d)\n",
4176 args->buffer_count, ret);
4177 }
4178 }
4179
4180 drm_free_large(exec2_list);
4181 return ret;
4182}
4183
Eric Anholt673a3942008-07-30 12:06:12 -07004184int
4185i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment)
4186{
4187 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01004188 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004189 int ret;
4190
Daniel Vetter778c3542010-05-13 11:49:44 +02004191 BUG_ON(obj_priv->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT);
4192
Eric Anholt673a3942008-07-30 12:06:12 -07004193 i915_verify_inactive(dev, __FILE__, __LINE__);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01004194
4195 if (obj_priv->gtt_space != NULL) {
4196 if (alignment == 0)
4197 alignment = i915_gem_get_gtt_alignment(obj);
4198 if (obj_priv->gtt_offset & (alignment - 1)) {
Chris Wilsonae7d49d2010-08-04 12:37:41 +01004199 WARN(obj_priv->pin_count,
4200 "bo is already pinned with incorrect alignment:"
4201 " offset=%x, req.alignment=%x\n",
4202 obj_priv->gtt_offset, alignment);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01004203 ret = i915_gem_object_unbind(obj);
4204 if (ret)
4205 return ret;
4206 }
4207 }
4208
Eric Anholt673a3942008-07-30 12:06:12 -07004209 if (obj_priv->gtt_space == NULL) {
4210 ret = i915_gem_object_bind_to_gtt(obj, alignment);
Chris Wilson97311292009-09-21 00:22:34 +01004211 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07004212 return ret;
Chris Wilson22c344e2009-02-11 14:26:45 +00004213 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05004214
Eric Anholt673a3942008-07-30 12:06:12 -07004215 obj_priv->pin_count++;
4216
4217 /* If the object is not active and not pending a flush,
4218 * remove it from the inactive list
4219 */
4220 if (obj_priv->pin_count == 1) {
4221 atomic_inc(&dev->pin_count);
4222 atomic_add(obj->size, &dev->pin_memory);
4223 if (!obj_priv->active &&
Chris Wilsonbf1a1092010-08-07 11:01:20 +01004224 (obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
Eric Anholt673a3942008-07-30 12:06:12 -07004225 list_del_init(&obj_priv->list);
4226 }
4227 i915_verify_inactive(dev, __FILE__, __LINE__);
4228
4229 return 0;
4230}
4231
4232void
4233i915_gem_object_unpin(struct drm_gem_object *obj)
4234{
4235 struct drm_device *dev = obj->dev;
4236 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01004237 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004238
4239 i915_verify_inactive(dev, __FILE__, __LINE__);
4240 obj_priv->pin_count--;
4241 BUG_ON(obj_priv->pin_count < 0);
4242 BUG_ON(obj_priv->gtt_space == NULL);
4243
4244 /* If the object is no longer pinned, and is
4245 * neither active nor being flushed, then stick it on
4246 * the inactive list
4247 */
4248 if (obj_priv->pin_count == 0) {
4249 if (!obj_priv->active &&
Chris Wilson21d509e2009-06-06 09:46:02 +01004250 (obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
Eric Anholt673a3942008-07-30 12:06:12 -07004251 list_move_tail(&obj_priv->list,
4252 &dev_priv->mm.inactive_list);
4253 atomic_dec(&dev->pin_count);
4254 atomic_sub(obj->size, &dev->pin_memory);
4255 }
4256 i915_verify_inactive(dev, __FILE__, __LINE__);
4257}
4258
4259int
4260i915_gem_pin_ioctl(struct drm_device *dev, void *data,
4261 struct drm_file *file_priv)
4262{
4263 struct drm_i915_gem_pin *args = data;
4264 struct drm_gem_object *obj;
4265 struct drm_i915_gem_object *obj_priv;
4266 int ret;
4267
4268 mutex_lock(&dev->struct_mutex);
4269
4270 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4271 if (obj == NULL) {
4272 DRM_ERROR("Bad handle in i915_gem_pin_ioctl(): %d\n",
4273 args->handle);
4274 mutex_unlock(&dev->struct_mutex);
4275 return -EBADF;
4276 }
Daniel Vetter23010e42010-03-08 13:35:02 +01004277 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004278
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004279 if (obj_priv->madv != I915_MADV_WILLNEED) {
4280 DRM_ERROR("Attempting to pin a purgeable buffer\n");
Chris Wilson3ef94da2009-09-14 16:50:29 +01004281 drm_gem_object_unreference(obj);
4282 mutex_unlock(&dev->struct_mutex);
4283 return -EINVAL;
4284 }
4285
Jesse Barnes79e53942008-11-07 14:24:08 -08004286 if (obj_priv->pin_filp != NULL && obj_priv->pin_filp != file_priv) {
4287 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
4288 args->handle);
Chris Wilson96dec612009-02-08 19:08:04 +00004289 drm_gem_object_unreference(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004290 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08004291 return -EINVAL;
4292 }
4293
4294 obj_priv->user_pin_count++;
4295 obj_priv->pin_filp = file_priv;
4296 if (obj_priv->user_pin_count == 1) {
4297 ret = i915_gem_object_pin(obj, args->alignment);
4298 if (ret != 0) {
4299 drm_gem_object_unreference(obj);
4300 mutex_unlock(&dev->struct_mutex);
4301 return ret;
4302 }
Eric Anholt673a3942008-07-30 12:06:12 -07004303 }
4304
4305 /* XXX - flush the CPU caches for pinned objects
4306 * as the X server doesn't manage domains yet
4307 */
Eric Anholte47c68e2008-11-14 13:35:19 -08004308 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004309 args->offset = obj_priv->gtt_offset;
4310 drm_gem_object_unreference(obj);
4311 mutex_unlock(&dev->struct_mutex);
4312
4313 return 0;
4314}
4315
4316int
4317i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
4318 struct drm_file *file_priv)
4319{
4320 struct drm_i915_gem_pin *args = data;
4321 struct drm_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08004322 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07004323
4324 mutex_lock(&dev->struct_mutex);
4325
4326 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4327 if (obj == NULL) {
4328 DRM_ERROR("Bad handle in i915_gem_unpin_ioctl(): %d\n",
4329 args->handle);
4330 mutex_unlock(&dev->struct_mutex);
4331 return -EBADF;
4332 }
4333
Daniel Vetter23010e42010-03-08 13:35:02 +01004334 obj_priv = to_intel_bo(obj);
Jesse Barnes79e53942008-11-07 14:24:08 -08004335 if (obj_priv->pin_filp != file_priv) {
4336 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
4337 args->handle);
4338 drm_gem_object_unreference(obj);
4339 mutex_unlock(&dev->struct_mutex);
4340 return -EINVAL;
4341 }
4342 obj_priv->user_pin_count--;
4343 if (obj_priv->user_pin_count == 0) {
4344 obj_priv->pin_filp = NULL;
4345 i915_gem_object_unpin(obj);
4346 }
Eric Anholt673a3942008-07-30 12:06:12 -07004347
4348 drm_gem_object_unreference(obj);
4349 mutex_unlock(&dev->struct_mutex);
4350 return 0;
4351}
4352
4353int
4354i915_gem_busy_ioctl(struct drm_device *dev, void *data,
4355 struct drm_file *file_priv)
4356{
4357 struct drm_i915_gem_busy *args = data;
4358 struct drm_gem_object *obj;
4359 struct drm_i915_gem_object *obj_priv;
4360
Eric Anholt673a3942008-07-30 12:06:12 -07004361 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4362 if (obj == NULL) {
4363 DRM_ERROR("Bad handle in i915_gem_busy_ioctl(): %d\n",
4364 args->handle);
Eric Anholt673a3942008-07-30 12:06:12 -07004365 return -EBADF;
4366 }
4367
Chris Wilsonb1ce7862009-06-06 09:46:00 +01004368 mutex_lock(&dev->struct_mutex);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004369
Chris Wilson0be555b2010-08-04 15:36:30 +01004370 /* Count all active objects as busy, even if they are currently not used
4371 * by the gpu. Users of this interface expect objects to eventually
4372 * become non-busy without any further actions, therefore emit any
4373 * necessary flushes here.
Eric Anholtc4de0a52008-12-14 19:05:04 -08004374 */
Chris Wilson0be555b2010-08-04 15:36:30 +01004375 obj_priv = to_intel_bo(obj);
4376 args->busy = obj_priv->active;
4377 if (args->busy) {
4378 /* Unconditionally flush objects, even when the gpu still uses this
4379 * object. Userspace calling this function indicates that it wants to
4380 * use this buffer rather sooner than later, so issuing the required
4381 * flush earlier is beneficial.
4382 */
4383 if (obj->write_domain) {
4384 i915_gem_flush(dev, 0, obj->write_domain);
4385 (void)i915_add_request(dev, file_priv, obj->write_domain, obj_priv->ring);
4386 }
4387
4388 /* Update the active list for the hardware's current position.
4389 * Otherwise this only updates on a delayed timer or when irqs
4390 * are actually unmasked, and our working set ends up being
4391 * larger than required.
4392 */
4393 i915_gem_retire_requests_ring(dev, obj_priv->ring);
4394
4395 args->busy = obj_priv->active;
4396 }
Eric Anholt673a3942008-07-30 12:06:12 -07004397
4398 drm_gem_object_unreference(obj);
4399 mutex_unlock(&dev->struct_mutex);
4400 return 0;
4401}
4402
4403int
4404i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4405 struct drm_file *file_priv)
4406{
4407 return i915_gem_ring_throttle(dev, file_priv);
4408}
4409
Chris Wilson3ef94da2009-09-14 16:50:29 +01004410int
4411i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4412 struct drm_file *file_priv)
4413{
4414 struct drm_i915_gem_madvise *args = data;
4415 struct drm_gem_object *obj;
4416 struct drm_i915_gem_object *obj_priv;
4417
4418 switch (args->madv) {
4419 case I915_MADV_DONTNEED:
4420 case I915_MADV_WILLNEED:
4421 break;
4422 default:
4423 return -EINVAL;
4424 }
4425
4426 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4427 if (obj == NULL) {
4428 DRM_ERROR("Bad handle in i915_gem_madvise_ioctl(): %d\n",
4429 args->handle);
4430 return -EBADF;
4431 }
4432
4433 mutex_lock(&dev->struct_mutex);
Daniel Vetter23010e42010-03-08 13:35:02 +01004434 obj_priv = to_intel_bo(obj);
Chris Wilson3ef94da2009-09-14 16:50:29 +01004435
4436 if (obj_priv->pin_count) {
4437 drm_gem_object_unreference(obj);
4438 mutex_unlock(&dev->struct_mutex);
4439
4440 DRM_ERROR("Attempted i915_gem_madvise_ioctl() on a pinned object\n");
4441 return -EINVAL;
4442 }
4443
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004444 if (obj_priv->madv != __I915_MADV_PURGED)
4445 obj_priv->madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004446
Chris Wilson2d7ef392009-09-20 23:13:10 +01004447 /* if the object is no longer bound, discard its backing storage */
4448 if (i915_gem_object_is_purgeable(obj_priv) &&
4449 obj_priv->gtt_space == NULL)
4450 i915_gem_object_truncate(obj);
4451
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004452 args->retained = obj_priv->madv != __I915_MADV_PURGED;
4453
Chris Wilson3ef94da2009-09-14 16:50:29 +01004454 drm_gem_object_unreference(obj);
4455 mutex_unlock(&dev->struct_mutex);
4456
4457 return 0;
4458}
4459
Daniel Vetterac52bc52010-04-09 19:05:06 +00004460struct drm_gem_object * i915_gem_alloc_object(struct drm_device *dev,
4461 size_t size)
4462{
Daniel Vetterc397b902010-04-09 19:05:07 +00004463 struct drm_i915_gem_object *obj;
4464
4465 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
4466 if (obj == NULL)
4467 return NULL;
4468
4469 if (drm_gem_object_init(dev, &obj->base, size) != 0) {
4470 kfree(obj);
4471 return NULL;
4472 }
4473
4474 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4475 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4476
4477 obj->agp_type = AGP_USER_MEMORY;
Daniel Vetter62b8b212010-04-09 19:05:08 +00004478 obj->base.driver_private = NULL;
Daniel Vetterc397b902010-04-09 19:05:07 +00004479 obj->fence_reg = I915_FENCE_REG_NONE;
4480 INIT_LIST_HEAD(&obj->list);
4481 INIT_LIST_HEAD(&obj->gpu_write_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00004482 obj->madv = I915_MADV_WILLNEED;
4483
4484 trace_i915_gem_object_create(&obj->base);
4485
4486 return &obj->base;
Daniel Vetterac52bc52010-04-09 19:05:06 +00004487}
4488
Eric Anholt673a3942008-07-30 12:06:12 -07004489int i915_gem_init_object(struct drm_gem_object *obj)
4490{
Daniel Vetterc397b902010-04-09 19:05:07 +00004491 BUG();
Jesse Barnesde151cf2008-11-12 10:03:55 -08004492
Eric Anholt673a3942008-07-30 12:06:12 -07004493 return 0;
4494}
4495
Chris Wilsonbe726152010-07-23 23:18:50 +01004496static void i915_gem_free_object_tail(struct drm_gem_object *obj)
4497{
4498 struct drm_device *dev = obj->dev;
4499 drm_i915_private_t *dev_priv = dev->dev_private;
4500 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
4501 int ret;
4502
4503 ret = i915_gem_object_unbind(obj);
4504 if (ret == -ERESTARTSYS) {
4505 list_move(&obj_priv->list,
4506 &dev_priv->mm.deferred_free_list);
4507 return;
4508 }
4509
4510 if (obj_priv->mmap_offset)
4511 i915_gem_free_mmap_offset(obj);
4512
4513 drm_gem_object_release(obj);
4514
4515 kfree(obj_priv->page_cpu_valid);
4516 kfree(obj_priv->bit_17);
4517 kfree(obj_priv);
4518}
4519
Eric Anholt673a3942008-07-30 12:06:12 -07004520void i915_gem_free_object(struct drm_gem_object *obj)
4521{
Jesse Barnesde151cf2008-11-12 10:03:55 -08004522 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01004523 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004524
Chris Wilson1c5d22f2009-08-25 11:15:50 +01004525 trace_i915_gem_object_destroy(obj);
4526
Eric Anholt673a3942008-07-30 12:06:12 -07004527 while (obj_priv->pin_count > 0)
4528 i915_gem_object_unpin(obj);
4529
Dave Airlie71acb5e2008-12-30 20:31:46 +10004530 if (obj_priv->phys_obj)
4531 i915_gem_detach_phys_object(dev, obj);
4532
Chris Wilsonbe726152010-07-23 23:18:50 +01004533 i915_gem_free_object_tail(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004534}
4535
Chris Wilsonab5ee572009-09-20 19:25:47 +01004536/** Unbinds all inactive objects. */
Eric Anholt673a3942008-07-30 12:06:12 -07004537static int
Chris Wilsonab5ee572009-09-20 19:25:47 +01004538i915_gem_evict_from_inactive_list(struct drm_device *dev)
Eric Anholt673a3942008-07-30 12:06:12 -07004539{
Chris Wilsonab5ee572009-09-20 19:25:47 +01004540 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07004541
Chris Wilsonab5ee572009-09-20 19:25:47 +01004542 while (!list_empty(&dev_priv->mm.inactive_list)) {
4543 struct drm_gem_object *obj;
4544 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004545
Daniel Vettera8089e82010-04-09 19:05:09 +00004546 obj = &list_first_entry(&dev_priv->mm.inactive_list,
4547 struct drm_i915_gem_object,
4548 list)->base;
Eric Anholt673a3942008-07-30 12:06:12 -07004549
4550 ret = i915_gem_object_unbind(obj);
4551 if (ret != 0) {
Chris Wilsonab5ee572009-09-20 19:25:47 +01004552 DRM_ERROR("Error unbinding object: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07004553 return ret;
4554 }
4555 }
4556
Eric Anholt673a3942008-07-30 12:06:12 -07004557 return 0;
4558}
4559
Jesse Barnes5669fca2009-02-17 15:13:31 -08004560int
Eric Anholt673a3942008-07-30 12:06:12 -07004561i915_gem_idle(struct drm_device *dev)
4562{
4563 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson29105cc2010-01-07 10:39:13 +00004564 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004565
Keith Packard6dbe2772008-10-14 21:41:13 -07004566 mutex_lock(&dev->struct_mutex);
4567
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004568 if (dev_priv->mm.suspended ||
Zou Nan haid1b851f2010-05-21 09:08:57 +08004569 (dev_priv->render_ring.gem_object == NULL) ||
4570 (HAS_BSD(dev) &&
4571 dev_priv->bsd_ring.gem_object == NULL)) {
Keith Packard6dbe2772008-10-14 21:41:13 -07004572 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004573 return 0;
Keith Packard6dbe2772008-10-14 21:41:13 -07004574 }
Eric Anholt673a3942008-07-30 12:06:12 -07004575
Chris Wilson29105cc2010-01-07 10:39:13 +00004576 ret = i915_gpu_idle(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004577 if (ret) {
4578 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004579 return ret;
Keith Packard6dbe2772008-10-14 21:41:13 -07004580 }
Eric Anholt673a3942008-07-30 12:06:12 -07004581
Chris Wilson29105cc2010-01-07 10:39:13 +00004582 /* Under UMS, be paranoid and evict. */
4583 if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
4584 ret = i915_gem_evict_from_inactive_list(dev);
4585 if (ret) {
4586 mutex_unlock(&dev->struct_mutex);
4587 return ret;
4588 }
4589 }
4590
4591 /* Hack! Don't let anybody do execbuf while we don't control the chip.
4592 * We need to replace this with a semaphore, or something.
4593 * And not confound mm.suspended!
4594 */
4595 dev_priv->mm.suspended = 1;
4596 del_timer(&dev_priv->hangcheck_timer);
4597
4598 i915_kernel_lost_context(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004599 i915_gem_cleanup_ringbuffer(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00004600
Keith Packard6dbe2772008-10-14 21:41:13 -07004601 mutex_unlock(&dev->struct_mutex);
4602
Chris Wilson29105cc2010-01-07 10:39:13 +00004603 /* Cancel the retire work handler, which should be idle now. */
4604 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
4605
Eric Anholt673a3942008-07-30 12:06:12 -07004606 return 0;
4607}
4608
Jesse Barnese552eb72010-04-21 11:39:23 -07004609/*
4610 * 965+ support PIPE_CONTROL commands, which provide finer grained control
4611 * over cache flushing.
4612 */
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004613static int
Jesse Barnese552eb72010-04-21 11:39:23 -07004614i915_gem_init_pipe_control(struct drm_device *dev)
4615{
4616 drm_i915_private_t *dev_priv = dev->dev_private;
4617 struct drm_gem_object *obj;
4618 struct drm_i915_gem_object *obj_priv;
4619 int ret;
4620
Eric Anholt34dc4d42010-05-07 14:30:03 -07004621 obj = i915_gem_alloc_object(dev, 4096);
Jesse Barnese552eb72010-04-21 11:39:23 -07004622 if (obj == NULL) {
4623 DRM_ERROR("Failed to allocate seqno page\n");
4624 ret = -ENOMEM;
4625 goto err;
4626 }
4627 obj_priv = to_intel_bo(obj);
4628 obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
4629
4630 ret = i915_gem_object_pin(obj, 4096);
4631 if (ret)
4632 goto err_unref;
4633
4634 dev_priv->seqno_gfx_addr = obj_priv->gtt_offset;
4635 dev_priv->seqno_page = kmap(obj_priv->pages[0]);
4636 if (dev_priv->seqno_page == NULL)
4637 goto err_unpin;
4638
4639 dev_priv->seqno_obj = obj;
4640 memset(dev_priv->seqno_page, 0, PAGE_SIZE);
4641
4642 return 0;
4643
4644err_unpin:
4645 i915_gem_object_unpin(obj);
4646err_unref:
4647 drm_gem_object_unreference(obj);
4648err:
4649 return ret;
4650}
4651
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004652
4653static void
Jesse Barnese552eb72010-04-21 11:39:23 -07004654i915_gem_cleanup_pipe_control(struct drm_device *dev)
4655{
4656 drm_i915_private_t *dev_priv = dev->dev_private;
4657 struct drm_gem_object *obj;
4658 struct drm_i915_gem_object *obj_priv;
4659
4660 obj = dev_priv->seqno_obj;
4661 obj_priv = to_intel_bo(obj);
4662 kunmap(obj_priv->pages[0]);
4663 i915_gem_object_unpin(obj);
4664 drm_gem_object_unreference(obj);
4665 dev_priv->seqno_obj = NULL;
4666
4667 dev_priv->seqno_page = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07004668}
4669
Eric Anholt673a3942008-07-30 12:06:12 -07004670int
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004671i915_gem_init_ringbuffer(struct drm_device *dev)
4672{
4673 drm_i915_private_t *dev_priv = dev->dev_private;
4674 int ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01004675
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004676 dev_priv->render_ring = render_ring;
Chris Wilson68f95ba2010-05-27 13:18:22 +01004677
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004678 if (!I915_NEED_GFX_HWS(dev)) {
4679 dev_priv->render_ring.status_page.page_addr
4680 = dev_priv->status_page_dmah->vaddr;
4681 memset(dev_priv->render_ring.status_page.page_addr,
4682 0, PAGE_SIZE);
4683 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004684
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004685 if (HAS_PIPE_CONTROL(dev)) {
4686 ret = i915_gem_init_pipe_control(dev);
4687 if (ret)
4688 return ret;
4689 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004690
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004691 ret = intel_init_ring_buffer(dev, &dev_priv->render_ring);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004692 if (ret)
4693 goto cleanup_pipe_control;
4694
4695 if (HAS_BSD(dev)) {
Zou Nan haid1b851f2010-05-21 09:08:57 +08004696 dev_priv->bsd_ring = bsd_ring;
4697 ret = intel_init_ring_buffer(dev, &dev_priv->bsd_ring);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004698 if (ret)
4699 goto cleanup_render_ring;
Zou Nan haid1b851f2010-05-21 09:08:57 +08004700 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004701
4702 return 0;
4703
4704cleanup_render_ring:
4705 intel_cleanup_ring_buffer(dev, &dev_priv->render_ring);
4706cleanup_pipe_control:
4707 if (HAS_PIPE_CONTROL(dev))
4708 i915_gem_cleanup_pipe_control(dev);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004709 return ret;
4710}
4711
4712void
4713i915_gem_cleanup_ringbuffer(struct drm_device *dev)
4714{
4715 drm_i915_private_t *dev_priv = dev->dev_private;
4716
4717 intel_cleanup_ring_buffer(dev, &dev_priv->render_ring);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004718 if (HAS_BSD(dev))
4719 intel_cleanup_ring_buffer(dev, &dev_priv->bsd_ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004720 if (HAS_PIPE_CONTROL(dev))
4721 i915_gem_cleanup_pipe_control(dev);
4722}
4723
4724int
Eric Anholt673a3942008-07-30 12:06:12 -07004725i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
4726 struct drm_file *file_priv)
4727{
4728 drm_i915_private_t *dev_priv = dev->dev_private;
4729 int ret;
4730
Jesse Barnes79e53942008-11-07 14:24:08 -08004731 if (drm_core_check_feature(dev, DRIVER_MODESET))
4732 return 0;
4733
Ben Gamariba1234d2009-09-14 17:48:47 -04004734 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07004735 DRM_ERROR("Reenabling wedged hardware, good luck\n");
Ben Gamariba1234d2009-09-14 17:48:47 -04004736 atomic_set(&dev_priv->mm.wedged, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004737 }
4738
Eric Anholt673a3942008-07-30 12:06:12 -07004739 mutex_lock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004740 dev_priv->mm.suspended = 0;
4741
4742 ret = i915_gem_init_ringbuffer(dev);
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004743 if (ret != 0) {
4744 mutex_unlock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004745 return ret;
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004746 }
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004747
Carl Worth5e118f42009-03-20 11:54:25 -07004748 spin_lock(&dev_priv->mm.active_list_lock);
Zou Nan hai852835f2010-05-21 09:08:56 +08004749 BUG_ON(!list_empty(&dev_priv->render_ring.active_list));
Zou Nan haid1b851f2010-05-21 09:08:57 +08004750 BUG_ON(HAS_BSD(dev) && !list_empty(&dev_priv->bsd_ring.active_list));
Carl Worth5e118f42009-03-20 11:54:25 -07004751 spin_unlock(&dev_priv->mm.active_list_lock);
4752
Eric Anholt673a3942008-07-30 12:06:12 -07004753 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
4754 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
Zou Nan hai852835f2010-05-21 09:08:56 +08004755 BUG_ON(!list_empty(&dev_priv->render_ring.request_list));
Zou Nan haid1b851f2010-05-21 09:08:57 +08004756 BUG_ON(HAS_BSD(dev) && !list_empty(&dev_priv->bsd_ring.request_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004757 mutex_unlock(&dev->struct_mutex);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004758
Chris Wilson5f353082010-06-07 14:03:03 +01004759 ret = drm_irq_install(dev);
4760 if (ret)
4761 goto cleanup_ringbuffer;
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004762
Eric Anholt673a3942008-07-30 12:06:12 -07004763 return 0;
Chris Wilson5f353082010-06-07 14:03:03 +01004764
4765cleanup_ringbuffer:
4766 mutex_lock(&dev->struct_mutex);
4767 i915_gem_cleanup_ringbuffer(dev);
4768 dev_priv->mm.suspended = 1;
4769 mutex_unlock(&dev->struct_mutex);
4770
4771 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004772}
4773
4774int
4775i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
4776 struct drm_file *file_priv)
4777{
Jesse Barnes79e53942008-11-07 14:24:08 -08004778 if (drm_core_check_feature(dev, DRIVER_MODESET))
4779 return 0;
4780
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004781 drm_irq_uninstall(dev);
Linus Torvaldse6890f62009-09-08 17:09:24 -07004782 return i915_gem_idle(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004783}
4784
4785void
4786i915_gem_lastclose(struct drm_device *dev)
4787{
4788 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004789
Eric Anholte806b492009-01-22 09:56:58 -08004790 if (drm_core_check_feature(dev, DRIVER_MODESET))
4791 return;
4792
Keith Packard6dbe2772008-10-14 21:41:13 -07004793 ret = i915_gem_idle(dev);
4794 if (ret)
4795 DRM_ERROR("failed to idle hardware: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07004796}
4797
4798void
4799i915_gem_load(struct drm_device *dev)
4800{
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004801 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07004802 drm_i915_private_t *dev_priv = dev->dev_private;
4803
Carl Worth5e118f42009-03-20 11:54:25 -07004804 spin_lock_init(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07004805 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
Daniel Vetter99fcb762010-02-07 16:20:18 +01004806 INIT_LIST_HEAD(&dev_priv->mm.gpu_write_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004807 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07004808 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Chris Wilsonbe726152010-07-23 23:18:50 +01004809 INIT_LIST_HEAD(&dev_priv->mm.deferred_free_list);
Zou Nan hai852835f2010-05-21 09:08:56 +08004810 INIT_LIST_HEAD(&dev_priv->render_ring.active_list);
4811 INIT_LIST_HEAD(&dev_priv->render_ring.request_list);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004812 if (HAS_BSD(dev)) {
4813 INIT_LIST_HEAD(&dev_priv->bsd_ring.active_list);
4814 INIT_LIST_HEAD(&dev_priv->bsd_ring.request_list);
4815 }
Daniel Vetter007cc8a2010-04-28 11:02:31 +02004816 for (i = 0; i < 16; i++)
4817 INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004818 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
4819 i915_gem_retire_work_handler);
Chris Wilson31169712009-09-14 16:50:28 +01004820 spin_lock(&shrink_list_lock);
4821 list_add(&dev_priv->mm.shrink_list, &shrink_list);
4822 spin_unlock(&shrink_list_lock);
4823
Dave Airlie94400122010-07-20 13:15:31 +10004824 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
4825 if (IS_GEN3(dev)) {
4826 u32 tmp = I915_READ(MI_ARB_STATE);
4827 if (!(tmp & MI_ARB_C3_LP_WRITE_ENABLE)) {
4828 /* arb state is a masked write, so set bit + bit in mask */
4829 tmp = MI_ARB_C3_LP_WRITE_ENABLE | (MI_ARB_C3_LP_WRITE_ENABLE << MI_ARB_MASK_SHIFT);
4830 I915_WRITE(MI_ARB_STATE, tmp);
4831 }
4832 }
4833
Jesse Barnesde151cf2008-11-12 10:03:55 -08004834 /* Old X drivers will take 0-2 for front, back, depth buffers */
Eric Anholtb397c832010-01-26 09:43:10 -08004835 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4836 dev_priv->fence_reg_start = 3;
Jesse Barnesde151cf2008-11-12 10:03:55 -08004837
Jesse Barnes0f973f22009-01-26 17:10:45 -08004838 if (IS_I965G(dev) || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08004839 dev_priv->num_fence_regs = 16;
4840 else
4841 dev_priv->num_fence_regs = 8;
4842
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004843 /* Initialize fence registers to zero */
4844 if (IS_I965G(dev)) {
4845 for (i = 0; i < 16; i++)
4846 I915_WRITE64(FENCE_REG_965_0 + (i * 8), 0);
4847 } else {
4848 for (i = 0; i < 8; i++)
4849 I915_WRITE(FENCE_REG_830_0 + (i * 4), 0);
4850 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4851 for (i = 0; i < 8; i++)
4852 I915_WRITE(FENCE_REG_945_8 + (i * 4), 0);
4853 }
Eric Anholt673a3942008-07-30 12:06:12 -07004854 i915_gem_detect_bit_6_swizzle(dev);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05004855 init_waitqueue_head(&dev_priv->pending_flip_queue);
Eric Anholt673a3942008-07-30 12:06:12 -07004856}
Dave Airlie71acb5e2008-12-30 20:31:46 +10004857
4858/*
4859 * Create a physically contiguous memory object for this object
4860 * e.g. for cursor + overlay regs
4861 */
4862int i915_gem_init_phys_object(struct drm_device *dev,
4863 int id, int size)
4864{
4865 drm_i915_private_t *dev_priv = dev->dev_private;
4866 struct drm_i915_gem_phys_object *phys_obj;
4867 int ret;
4868
4869 if (dev_priv->mm.phys_objs[id - 1] || !size)
4870 return 0;
4871
Eric Anholt9a298b22009-03-24 12:23:04 -07004872 phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004873 if (!phys_obj)
4874 return -ENOMEM;
4875
4876 phys_obj->id = id;
4877
Zhenyu Wange6be8d92010-01-05 11:25:05 +08004878 phys_obj->handle = drm_pci_alloc(dev, size, 0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004879 if (!phys_obj->handle) {
4880 ret = -ENOMEM;
4881 goto kfree_obj;
4882 }
4883#ifdef CONFIG_X86
4884 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4885#endif
4886
4887 dev_priv->mm.phys_objs[id - 1] = phys_obj;
4888
4889 return 0;
4890kfree_obj:
Eric Anholt9a298b22009-03-24 12:23:04 -07004891 kfree(phys_obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004892 return ret;
4893}
4894
4895void i915_gem_free_phys_object(struct drm_device *dev, int id)
4896{
4897 drm_i915_private_t *dev_priv = dev->dev_private;
4898 struct drm_i915_gem_phys_object *phys_obj;
4899
4900 if (!dev_priv->mm.phys_objs[id - 1])
4901 return;
4902
4903 phys_obj = dev_priv->mm.phys_objs[id - 1];
4904 if (phys_obj->cur_obj) {
4905 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
4906 }
4907
4908#ifdef CONFIG_X86
4909 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4910#endif
4911 drm_pci_free(dev, phys_obj->handle);
4912 kfree(phys_obj);
4913 dev_priv->mm.phys_objs[id - 1] = NULL;
4914}
4915
4916void i915_gem_free_all_phys_object(struct drm_device *dev)
4917{
4918 int i;
4919
Dave Airlie260883c2009-01-22 17:58:49 +10004920 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004921 i915_gem_free_phys_object(dev, i);
4922}
4923
4924void i915_gem_detach_phys_object(struct drm_device *dev,
4925 struct drm_gem_object *obj)
4926{
4927 struct drm_i915_gem_object *obj_priv;
4928 int i;
4929 int ret;
4930 int page_count;
4931
Daniel Vetter23010e42010-03-08 13:35:02 +01004932 obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004933 if (!obj_priv->phys_obj)
4934 return;
4935
Chris Wilson4bdadb92010-01-27 13:36:32 +00004936 ret = i915_gem_object_get_pages(obj, 0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004937 if (ret)
4938 goto out;
4939
4940 page_count = obj->size / PAGE_SIZE;
4941
4942 for (i = 0; i < page_count; i++) {
Eric Anholt856fa192009-03-19 14:10:50 -07004943 char *dst = kmap_atomic(obj_priv->pages[i], KM_USER0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004944 char *src = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4945
4946 memcpy(dst, src, PAGE_SIZE);
4947 kunmap_atomic(dst, KM_USER0);
4948 }
Eric Anholt856fa192009-03-19 14:10:50 -07004949 drm_clflush_pages(obj_priv->pages, page_count);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004950 drm_agp_chipset_flush(dev);
Chris Wilsond78b47b2009-06-17 21:52:49 +01004951
4952 i915_gem_object_put_pages(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004953out:
4954 obj_priv->phys_obj->cur_obj = NULL;
4955 obj_priv->phys_obj = NULL;
4956}
4957
4958int
4959i915_gem_attach_phys_object(struct drm_device *dev,
4960 struct drm_gem_object *obj, int id)
4961{
4962 drm_i915_private_t *dev_priv = dev->dev_private;
4963 struct drm_i915_gem_object *obj_priv;
4964 int ret = 0;
4965 int page_count;
4966 int i;
4967
4968 if (id > I915_MAX_PHYS_OBJECT)
4969 return -EINVAL;
4970
Daniel Vetter23010e42010-03-08 13:35:02 +01004971 obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004972
4973 if (obj_priv->phys_obj) {
4974 if (obj_priv->phys_obj->id == id)
4975 return 0;
4976 i915_gem_detach_phys_object(dev, obj);
4977 }
4978
4979
4980 /* create a new object */
4981 if (!dev_priv->mm.phys_objs[id - 1]) {
4982 ret = i915_gem_init_phys_object(dev, id,
4983 obj->size);
4984 if (ret) {
Linus Torvaldsaeb565d2009-01-26 10:01:53 -08004985 DRM_ERROR("failed to init phys object %d size: %zu\n", id, obj->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004986 goto out;
4987 }
4988 }
4989
4990 /* bind to the object */
4991 obj_priv->phys_obj = dev_priv->mm.phys_objs[id - 1];
4992 obj_priv->phys_obj->cur_obj = obj;
4993
Chris Wilson4bdadb92010-01-27 13:36:32 +00004994 ret = i915_gem_object_get_pages(obj, 0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004995 if (ret) {
4996 DRM_ERROR("failed to get page list\n");
4997 goto out;
4998 }
4999
5000 page_count = obj->size / PAGE_SIZE;
5001
5002 for (i = 0; i < page_count; i++) {
Eric Anholt856fa192009-03-19 14:10:50 -07005003 char *src = kmap_atomic(obj_priv->pages[i], KM_USER0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10005004 char *dst = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
5005
5006 memcpy(dst, src, PAGE_SIZE);
5007 kunmap_atomic(src, KM_USER0);
5008 }
5009
Chris Wilsond78b47b2009-06-17 21:52:49 +01005010 i915_gem_object_put_pages(obj);
5011
Dave Airlie71acb5e2008-12-30 20:31:46 +10005012 return 0;
5013out:
5014 return ret;
5015}
5016
5017static int
5018i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
5019 struct drm_i915_gem_pwrite *args,
5020 struct drm_file *file_priv)
5021{
Daniel Vetter23010e42010-03-08 13:35:02 +01005022 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10005023 void *obj_addr;
5024 int ret;
5025 char __user *user_data;
5026
5027 user_data = (char __user *) (uintptr_t) args->data_ptr;
5028 obj_addr = obj_priv->phys_obj->handle->vaddr + args->offset;
5029
Zhao Yakui44d98a62009-10-09 11:39:40 +08005030 DRM_DEBUG_DRIVER("obj_addr %p, %lld\n", obj_addr, args->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10005031 ret = copy_from_user(obj_addr, user_data, args->size);
5032 if (ret)
5033 return -EFAULT;
5034
5035 drm_agp_chipset_flush(dev);
5036 return 0;
5037}
Eric Anholtb9624422009-06-03 07:27:35 +00005038
5039void i915_gem_release(struct drm_device * dev, struct drm_file *file_priv)
5040{
5041 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
5042
5043 /* Clean up our request list when the client is going away, so that
5044 * later retire_requests won't dereference our soon-to-be-gone
5045 * file_priv.
5046 */
5047 mutex_lock(&dev->struct_mutex);
5048 while (!list_empty(&i915_file_priv->mm.request_list))
5049 list_del_init(i915_file_priv->mm.request_list.next);
5050 mutex_unlock(&dev->struct_mutex);
5051}
Chris Wilson31169712009-09-14 16:50:28 +01005052
Chris Wilson31169712009-09-14 16:50:28 +01005053static int
Chris Wilson1637ef42010-04-20 17:10:35 +01005054i915_gpu_is_active(struct drm_device *dev)
5055{
5056 drm_i915_private_t *dev_priv = dev->dev_private;
5057 int lists_empty;
5058
5059 spin_lock(&dev_priv->mm.active_list_lock);
5060 lists_empty = list_empty(&dev_priv->mm.flushing_list) &&
Zou Nan hai852835f2010-05-21 09:08:56 +08005061 list_empty(&dev_priv->render_ring.active_list);
Zou Nan haid1b851f2010-05-21 09:08:57 +08005062 if (HAS_BSD(dev))
5063 lists_empty &= list_empty(&dev_priv->bsd_ring.active_list);
Chris Wilson1637ef42010-04-20 17:10:35 +01005064 spin_unlock(&dev_priv->mm.active_list_lock);
5065
5066 return !lists_empty;
5067}
5068
5069static int
Dave Chinner7f8275d2010-07-19 14:56:17 +10005070i915_gem_shrink(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask)
Chris Wilson31169712009-09-14 16:50:28 +01005071{
5072 drm_i915_private_t *dev_priv, *next_dev;
5073 struct drm_i915_gem_object *obj_priv, *next_obj;
5074 int cnt = 0;
5075 int would_deadlock = 1;
5076
5077 /* "fast-path" to count number of available objects */
5078 if (nr_to_scan == 0) {
5079 spin_lock(&shrink_list_lock);
5080 list_for_each_entry(dev_priv, &shrink_list, mm.shrink_list) {
5081 struct drm_device *dev = dev_priv->dev;
5082
5083 if (mutex_trylock(&dev->struct_mutex)) {
5084 list_for_each_entry(obj_priv,
5085 &dev_priv->mm.inactive_list,
5086 list)
5087 cnt++;
5088 mutex_unlock(&dev->struct_mutex);
5089 }
5090 }
5091 spin_unlock(&shrink_list_lock);
5092
5093 return (cnt / 100) * sysctl_vfs_cache_pressure;
5094 }
5095
5096 spin_lock(&shrink_list_lock);
5097
Chris Wilson1637ef42010-04-20 17:10:35 +01005098rescan:
Chris Wilson31169712009-09-14 16:50:28 +01005099 /* first scan for clean buffers */
5100 list_for_each_entry_safe(dev_priv, next_dev,
5101 &shrink_list, mm.shrink_list) {
5102 struct drm_device *dev = dev_priv->dev;
5103
5104 if (! mutex_trylock(&dev->struct_mutex))
5105 continue;
5106
5107 spin_unlock(&shrink_list_lock);
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01005108 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08005109
Chris Wilson31169712009-09-14 16:50:28 +01005110 list_for_each_entry_safe(obj_priv, next_obj,
5111 &dev_priv->mm.inactive_list,
5112 list) {
5113 if (i915_gem_object_is_purgeable(obj_priv)) {
Daniel Vettera8089e82010-04-09 19:05:09 +00005114 i915_gem_object_unbind(&obj_priv->base);
Chris Wilson31169712009-09-14 16:50:28 +01005115 if (--nr_to_scan <= 0)
5116 break;
5117 }
5118 }
5119
5120 spin_lock(&shrink_list_lock);
5121 mutex_unlock(&dev->struct_mutex);
5122
Chris Wilson963b4832009-09-20 23:03:54 +01005123 would_deadlock = 0;
5124
Chris Wilson31169712009-09-14 16:50:28 +01005125 if (nr_to_scan <= 0)
5126 break;
5127 }
5128
5129 /* second pass, evict/count anything still on the inactive list */
5130 list_for_each_entry_safe(dev_priv, next_dev,
5131 &shrink_list, mm.shrink_list) {
5132 struct drm_device *dev = dev_priv->dev;
5133
5134 if (! mutex_trylock(&dev->struct_mutex))
5135 continue;
5136
5137 spin_unlock(&shrink_list_lock);
5138
5139 list_for_each_entry_safe(obj_priv, next_obj,
5140 &dev_priv->mm.inactive_list,
5141 list) {
5142 if (nr_to_scan > 0) {
Daniel Vettera8089e82010-04-09 19:05:09 +00005143 i915_gem_object_unbind(&obj_priv->base);
Chris Wilson31169712009-09-14 16:50:28 +01005144 nr_to_scan--;
5145 } else
5146 cnt++;
5147 }
5148
5149 spin_lock(&shrink_list_lock);
5150 mutex_unlock(&dev->struct_mutex);
5151
5152 would_deadlock = 0;
5153 }
5154
Chris Wilson1637ef42010-04-20 17:10:35 +01005155 if (nr_to_scan) {
5156 int active = 0;
5157
5158 /*
5159 * We are desperate for pages, so as a last resort, wait
5160 * for the GPU to finish and discard whatever we can.
5161 * This has a dramatic impact to reduce the number of
5162 * OOM-killer events whilst running the GPU aggressively.
5163 */
5164 list_for_each_entry(dev_priv, &shrink_list, mm.shrink_list) {
5165 struct drm_device *dev = dev_priv->dev;
5166
5167 if (!mutex_trylock(&dev->struct_mutex))
5168 continue;
5169
5170 spin_unlock(&shrink_list_lock);
5171
5172 if (i915_gpu_is_active(dev)) {
5173 i915_gpu_idle(dev);
5174 active++;
5175 }
5176
5177 spin_lock(&shrink_list_lock);
5178 mutex_unlock(&dev->struct_mutex);
5179 }
5180
5181 if (active)
5182 goto rescan;
5183 }
5184
Chris Wilson31169712009-09-14 16:50:28 +01005185 spin_unlock(&shrink_list_lock);
5186
5187 if (would_deadlock)
5188 return -1;
5189 else if (cnt > 0)
5190 return (cnt / 100) * sysctl_vfs_cache_pressure;
5191 else
5192 return 0;
5193}
5194
5195static struct shrinker shrinker = {
5196 .shrink = i915_gem_shrink,
5197 .seeks = DEFAULT_SEEKS,
5198};
5199
5200__init void
5201i915_gem_shrinker_init(void)
5202{
5203 register_shrinker(&shrinker);
5204}
5205
5206__exit void
5207i915_gem_shrinker_exit(void)
5208{
5209 unregister_shrinker(&shrinker);
5210}