blob: 24ee4622484f7832533e77827d71ca49b39298c0 [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;
1140 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001141 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001142 pgoff_t page_offset;
1143 unsigned long pfn;
1144 int ret = 0;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001145 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001146
1147 /* We don't use vmf->pgoff since that has the fake offset */
1148 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1149 PAGE_SHIFT;
1150
1151 /* Now bind it into the GTT if needed */
1152 mutex_lock(&dev->struct_mutex);
1153 if (!obj_priv->gtt_space) {
Chris Wilsone67b8ce2009-09-14 16:50:26 +01001154 ret = i915_gem_object_bind_to_gtt(obj, 0);
Chris Wilsonc7150892009-09-23 00:43:56 +01001155 if (ret)
1156 goto unlock;
Kristian Høgsberg07f4f3e2009-05-27 14:37:28 -04001157
Jesse Barnes14b60392009-05-20 16:47:08 -04001158 list_add_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001159
1160 ret = i915_gem_object_set_to_gtt_domain(obj, write);
Chris Wilsonc7150892009-09-23 00:43:56 +01001161 if (ret)
1162 goto unlock;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001163 }
1164
1165 /* Need a new fence register? */
Eric Anholta09ba7f2009-08-29 12:49:51 -07001166 if (obj_priv->tiling_mode != I915_TILING_NONE) {
Chris Wilson8c4b8c32009-06-17 22:08:52 +01001167 ret = i915_gem_object_get_fence_reg(obj);
Chris Wilsonc7150892009-09-23 00:43:56 +01001168 if (ret)
1169 goto unlock;
Eric Anholtd9ddcb92009-01-27 10:33:49 -08001170 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001171
1172 pfn = ((dev->agp->base + obj_priv->gtt_offset) >> PAGE_SHIFT) +
1173 page_offset;
1174
1175 /* Finally, remap it using the new GTT offset */
1176 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
Chris Wilsonc7150892009-09-23 00:43:56 +01001177unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001178 mutex_unlock(&dev->struct_mutex);
1179
1180 switch (ret) {
Chris Wilsonc7150892009-09-23 00:43:56 +01001181 case 0:
1182 case -ERESTARTSYS:
1183 return VM_FAULT_NOPAGE;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001184 case -ENOMEM:
1185 case -EAGAIN:
1186 return VM_FAULT_OOM;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001187 default:
Chris Wilsonc7150892009-09-23 00:43:56 +01001188 return VM_FAULT_SIGBUS;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001189 }
1190}
1191
1192/**
1193 * i915_gem_create_mmap_offset - create a fake mmap offset for an object
1194 * @obj: obj in question
1195 *
1196 * GEM memory mapping works by handing back to userspace a fake mmap offset
1197 * it can use in a subsequent mmap(2) call. The DRM core code then looks
1198 * up the object based on the offset and sets up the various memory mapping
1199 * structures.
1200 *
1201 * This routine allocates and attaches a fake offset for @obj.
1202 */
1203static int
1204i915_gem_create_mmap_offset(struct drm_gem_object *obj)
1205{
1206 struct drm_device *dev = obj->dev;
1207 struct drm_gem_mm *mm = dev->mm_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001208 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001209 struct drm_map_list *list;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +11001210 struct drm_local_map *map;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001211 int ret = 0;
1212
1213 /* Set the object up for mmap'ing */
1214 list = &obj->map_list;
Eric Anholt9a298b22009-03-24 12:23:04 -07001215 list->map = kzalloc(sizeof(struct drm_map_list), GFP_KERNEL);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001216 if (!list->map)
1217 return -ENOMEM;
1218
1219 map = list->map;
1220 map->type = _DRM_GEM;
1221 map->size = obj->size;
1222 map->handle = obj;
1223
1224 /* Get a DRM GEM mmap offset allocated... */
1225 list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
1226 obj->size / PAGE_SIZE, 0, 0);
1227 if (!list->file_offset_node) {
1228 DRM_ERROR("failed to allocate offset for bo %d\n", obj->name);
1229 ret = -ENOMEM;
1230 goto out_free_list;
1231 }
1232
1233 list->file_offset_node = drm_mm_get_block(list->file_offset_node,
1234 obj->size / PAGE_SIZE, 0);
1235 if (!list->file_offset_node) {
1236 ret = -ENOMEM;
1237 goto out_free_list;
1238 }
1239
1240 list->hash.key = list->file_offset_node->start;
1241 if (drm_ht_insert_item(&mm->offset_hash, &list->hash)) {
1242 DRM_ERROR("failed to add to map hash\n");
Chris Wilson5618ca62009-12-02 15:15:30 +00001243 ret = -ENOMEM;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001244 goto out_free_mm;
1245 }
1246
1247 /* By now we should be all set, any drm_mmap request on the offset
1248 * below will get to our mmap & fault handler */
1249 obj_priv->mmap_offset = ((uint64_t) list->hash.key) << PAGE_SHIFT;
1250
1251 return 0;
1252
1253out_free_mm:
1254 drm_mm_put_block(list->file_offset_node);
1255out_free_list:
Eric Anholt9a298b22009-03-24 12:23:04 -07001256 kfree(list->map);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001257
1258 return ret;
1259}
1260
Chris Wilson901782b2009-07-10 08:18:50 +01001261/**
1262 * i915_gem_release_mmap - remove physical page mappings
1263 * @obj: obj in question
1264 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001265 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01001266 * relinquish ownership of the pages back to the system.
1267 *
1268 * It is vital that we remove the page mapping if we have mapped a tiled
1269 * object through the GTT and then lose the fence register due to
1270 * resource pressure. Similarly if the object has been moved out of the
1271 * aperture, than pages mapped into userspace must be revoked. Removing the
1272 * mapping will then trigger a page fault on the next user access, allowing
1273 * fixup by i915_gem_fault().
1274 */
Eric Anholtd05ca302009-07-10 13:02:26 -07001275void
Chris Wilson901782b2009-07-10 08:18:50 +01001276i915_gem_release_mmap(struct drm_gem_object *obj)
1277{
1278 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001279 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson901782b2009-07-10 08:18:50 +01001280
1281 if (dev->dev_mapping)
1282 unmap_mapping_range(dev->dev_mapping,
1283 obj_priv->mmap_offset, obj->size, 1);
1284}
1285
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001286static void
1287i915_gem_free_mmap_offset(struct drm_gem_object *obj)
1288{
1289 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001290 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001291 struct drm_gem_mm *mm = dev->mm_private;
1292 struct drm_map_list *list;
1293
1294 list = &obj->map_list;
1295 drm_ht_remove_item(&mm->offset_hash, &list->hash);
1296
1297 if (list->file_offset_node) {
1298 drm_mm_put_block(list->file_offset_node);
1299 list->file_offset_node = NULL;
1300 }
1301
1302 if (list->map) {
Eric Anholt9a298b22009-03-24 12:23:04 -07001303 kfree(list->map);
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001304 list->map = NULL;
1305 }
1306
1307 obj_priv->mmap_offset = 0;
1308}
1309
Jesse Barnesde151cf2008-11-12 10:03:55 -08001310/**
1311 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1312 * @obj: object to check
1313 *
1314 * Return the required GTT alignment for an object, taking into account
1315 * potential fence register mapping if needed.
1316 */
1317static uint32_t
1318i915_gem_get_gtt_alignment(struct drm_gem_object *obj)
1319{
1320 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001321 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001322 int start, i;
1323
1324 /*
1325 * Minimum alignment is 4k (GTT page size), but might be greater
1326 * if a fence register is needed for the object.
1327 */
1328 if (IS_I965G(dev) || obj_priv->tiling_mode == I915_TILING_NONE)
1329 return 4096;
1330
1331 /*
1332 * Previous chips need to be aligned to the size of the smallest
1333 * fence register that can contain the object.
1334 */
1335 if (IS_I9XX(dev))
1336 start = 1024*1024;
1337 else
1338 start = 512*1024;
1339
1340 for (i = start; i < obj->size; i <<= 1)
1341 ;
1342
1343 return i;
1344}
1345
1346/**
1347 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1348 * @dev: DRM device
1349 * @data: GTT mapping ioctl data
1350 * @file_priv: GEM object info
1351 *
1352 * Simply returns the fake offset to userspace so it can mmap it.
1353 * The mmap call will end up in drm_gem_mmap(), which will set things
1354 * up so we can get faults in the handler above.
1355 *
1356 * The fault handler will take care of binding the object into the GTT
1357 * (since it may have been evicted to make room for something), allocating
1358 * a fence register, and mapping the appropriate aperture address into
1359 * userspace.
1360 */
1361int
1362i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
1363 struct drm_file *file_priv)
1364{
1365 struct drm_i915_gem_mmap_gtt *args = data;
1366 struct drm_i915_private *dev_priv = dev->dev_private;
1367 struct drm_gem_object *obj;
1368 struct drm_i915_gem_object *obj_priv;
1369 int ret;
1370
1371 if (!(dev->driver->driver_features & DRIVER_GEM))
1372 return -ENODEV;
1373
1374 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1375 if (obj == NULL)
1376 return -EBADF;
1377
1378 mutex_lock(&dev->struct_mutex);
1379
Daniel Vetter23010e42010-03-08 13:35:02 +01001380 obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001381
Chris Wilsonab182822009-09-22 18:46:17 +01001382 if (obj_priv->madv != I915_MADV_WILLNEED) {
1383 DRM_ERROR("Attempting to mmap a purgeable buffer\n");
1384 drm_gem_object_unreference(obj);
1385 mutex_unlock(&dev->struct_mutex);
1386 return -EINVAL;
1387 }
1388
1389
Jesse Barnesde151cf2008-11-12 10:03:55 -08001390 if (!obj_priv->mmap_offset) {
1391 ret = i915_gem_create_mmap_offset(obj);
Chris Wilson13af1062009-02-11 14:26:31 +00001392 if (ret) {
1393 drm_gem_object_unreference(obj);
1394 mutex_unlock(&dev->struct_mutex);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001395 return ret;
Chris Wilson13af1062009-02-11 14:26:31 +00001396 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001397 }
1398
1399 args->offset = obj_priv->mmap_offset;
1400
Jesse Barnesde151cf2008-11-12 10:03:55 -08001401 /*
1402 * Pull it into the GTT so that we have a page list (makes the
1403 * initial fault faster and any subsequent flushing possible).
1404 */
1405 if (!obj_priv->agp_mem) {
Chris Wilsone67b8ce2009-09-14 16:50:26 +01001406 ret = i915_gem_object_bind_to_gtt(obj, 0);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001407 if (ret) {
1408 drm_gem_object_unreference(obj);
1409 mutex_unlock(&dev->struct_mutex);
1410 return ret;
1411 }
Jesse Barnes14b60392009-05-20 16:47:08 -04001412 list_add_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001413 }
1414
1415 drm_gem_object_unreference(obj);
1416 mutex_unlock(&dev->struct_mutex);
1417
1418 return 0;
1419}
1420
Ben Gamari6911a9b2009-04-02 11:24:54 -07001421void
Eric Anholt856fa192009-03-19 14:10:50 -07001422i915_gem_object_put_pages(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07001423{
Daniel Vetter23010e42010-03-08 13:35:02 +01001424 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001425 int page_count = obj->size / PAGE_SIZE;
1426 int i;
1427
Eric Anholt856fa192009-03-19 14:10:50 -07001428 BUG_ON(obj_priv->pages_refcount == 0);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001429 BUG_ON(obj_priv->madv == __I915_MADV_PURGED);
Eric Anholt856fa192009-03-19 14:10:50 -07001430
1431 if (--obj_priv->pages_refcount != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07001432 return;
1433
Eric Anholt280b7132009-03-12 16:56:27 -07001434 if (obj_priv->tiling_mode != I915_TILING_NONE)
1435 i915_gem_object_save_bit_17_swizzle(obj);
1436
Chris Wilson3ef94da2009-09-14 16:50:29 +01001437 if (obj_priv->madv == I915_MADV_DONTNEED)
Chris Wilson13a05fd2009-09-20 23:03:19 +01001438 obj_priv->dirty = 0;
Chris Wilson3ef94da2009-09-14 16:50:29 +01001439
1440 for (i = 0; i < page_count; i++) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01001441 if (obj_priv->dirty)
1442 set_page_dirty(obj_priv->pages[i]);
1443
1444 if (obj_priv->madv == I915_MADV_WILLNEED)
Eric Anholt856fa192009-03-19 14:10:50 -07001445 mark_page_accessed(obj_priv->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001446
1447 page_cache_release(obj_priv->pages[i]);
1448 }
Eric Anholt673a3942008-07-30 12:06:12 -07001449 obj_priv->dirty = 0;
1450
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07001451 drm_free_large(obj_priv->pages);
Eric Anholt856fa192009-03-19 14:10:50 -07001452 obj_priv->pages = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001453}
1454
1455static void
Zou Nan hai852835f2010-05-21 09:08:56 +08001456i915_gem_object_move_to_active(struct drm_gem_object *obj, uint32_t seqno,
1457 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001458{
1459 struct drm_device *dev = obj->dev;
1460 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001461 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Zou Nan hai852835f2010-05-21 09:08:56 +08001462 BUG_ON(ring == NULL);
1463 obj_priv->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001464
1465 /* Add a reference if we're newly entering the active list. */
1466 if (!obj_priv->active) {
1467 drm_gem_object_reference(obj);
1468 obj_priv->active = 1;
1469 }
1470 /* Move from whatever list we were on to the tail of execution. */
Carl Worth5e118f42009-03-20 11:54:25 -07001471 spin_lock(&dev_priv->mm.active_list_lock);
Zou Nan hai852835f2010-05-21 09:08:56 +08001472 list_move_tail(&obj_priv->list, &ring->active_list);
Carl Worth5e118f42009-03-20 11:54:25 -07001473 spin_unlock(&dev_priv->mm.active_list_lock);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001474 obj_priv->last_rendering_seqno = seqno;
Eric Anholt673a3942008-07-30 12:06:12 -07001475}
1476
Eric Anholtce44b0e2008-11-06 16:00:31 -08001477static void
1478i915_gem_object_move_to_flushing(struct drm_gem_object *obj)
1479{
1480 struct drm_device *dev = obj->dev;
1481 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001482 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001483
1484 BUG_ON(!obj_priv->active);
1485 list_move_tail(&obj_priv->list, &dev_priv->mm.flushing_list);
1486 obj_priv->last_rendering_seqno = 0;
1487}
Eric Anholt673a3942008-07-30 12:06:12 -07001488
Chris Wilson963b4832009-09-20 23:03:54 +01001489/* Immediately discard the backing storage */
1490static void
1491i915_gem_object_truncate(struct drm_gem_object *obj)
1492{
Daniel Vetter23010e42010-03-08 13:35:02 +01001493 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001494 struct inode *inode;
Chris Wilson963b4832009-09-20 23:03:54 +01001495
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001496 inode = obj->filp->f_path.dentry->d_inode;
1497 if (inode->i_op->truncate)
1498 inode->i_op->truncate (inode);
1499
1500 obj_priv->madv = __I915_MADV_PURGED;
Chris Wilson963b4832009-09-20 23:03:54 +01001501}
1502
1503static inline int
1504i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj_priv)
1505{
1506 return obj_priv->madv == I915_MADV_DONTNEED;
1507}
1508
Eric Anholt673a3942008-07-30 12:06:12 -07001509static void
1510i915_gem_object_move_to_inactive(struct drm_gem_object *obj)
1511{
1512 struct drm_device *dev = obj->dev;
1513 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001514 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001515
1516 i915_verify_inactive(dev, __FILE__, __LINE__);
1517 if (obj_priv->pin_count != 0)
1518 list_del_init(&obj_priv->list);
1519 else
1520 list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
1521
Daniel Vetter99fcb762010-02-07 16:20:18 +01001522 BUG_ON(!list_empty(&obj_priv->gpu_write_list));
1523
Eric Anholtce44b0e2008-11-06 16:00:31 -08001524 obj_priv->last_rendering_seqno = 0;
Zou Nan hai852835f2010-05-21 09:08:56 +08001525 obj_priv->ring = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001526 if (obj_priv->active) {
1527 obj_priv->active = 0;
1528 drm_gem_object_unreference(obj);
1529 }
1530 i915_verify_inactive(dev, __FILE__, __LINE__);
1531}
1532
Daniel Vetter63560392010-02-19 11:51:59 +01001533static void
1534i915_gem_process_flushing_list(struct drm_device *dev,
Zou Nan hai852835f2010-05-21 09:08:56 +08001535 uint32_t flush_domains, uint32_t seqno,
1536 struct intel_ring_buffer *ring)
Daniel Vetter63560392010-02-19 11:51:59 +01001537{
1538 drm_i915_private_t *dev_priv = dev->dev_private;
1539 struct drm_i915_gem_object *obj_priv, *next;
1540
1541 list_for_each_entry_safe(obj_priv, next,
1542 &dev_priv->mm.gpu_write_list,
1543 gpu_write_list) {
Daniel Vettera8089e82010-04-09 19:05:09 +00001544 struct drm_gem_object *obj = &obj_priv->base;
Daniel Vetter63560392010-02-19 11:51:59 +01001545
1546 if ((obj->write_domain & flush_domains) ==
Zou Nan hai852835f2010-05-21 09:08:56 +08001547 obj->write_domain &&
1548 obj_priv->ring->ring_flag == ring->ring_flag) {
Daniel Vetter63560392010-02-19 11:51:59 +01001549 uint32_t old_write_domain = obj->write_domain;
1550
1551 obj->write_domain = 0;
1552 list_del_init(&obj_priv->gpu_write_list);
Zou Nan hai852835f2010-05-21 09:08:56 +08001553 i915_gem_object_move_to_active(obj, seqno, ring);
Daniel Vetter63560392010-02-19 11:51:59 +01001554
1555 /* update the fence lru list */
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001556 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
1557 struct drm_i915_fence_reg *reg =
1558 &dev_priv->fence_regs[obj_priv->fence_reg];
1559 list_move_tail(&reg->lru_list,
Daniel Vetter63560392010-02-19 11:51:59 +01001560 &dev_priv->mm.fence_list);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001561 }
Daniel Vetter63560392010-02-19 11:51:59 +01001562
1563 trace_i915_gem_object_change_domain(obj,
1564 obj->read_domains,
1565 old_write_domain);
1566 }
1567 }
1568}
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001569
Daniel Vetter5a5a0c62009-09-15 22:57:36 +02001570uint32_t
Eric Anholtb9624422009-06-03 07:27:35 +00001571i915_add_request(struct drm_device *dev, struct drm_file *file_priv,
Zou Nan hai852835f2010-05-21 09:08:56 +08001572 uint32_t flush_domains, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001573{
1574 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtb9624422009-06-03 07:27:35 +00001575 struct drm_i915_file_private *i915_file_priv = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001576 struct drm_i915_gem_request *request;
1577 uint32_t seqno;
1578 int was_empty;
Eric Anholt673a3942008-07-30 12:06:12 -07001579
Eric Anholtb9624422009-06-03 07:27:35 +00001580 if (file_priv != NULL)
1581 i915_file_priv = file_priv->driver_priv;
1582
Eric Anholt9a298b22009-03-24 12:23:04 -07001583 request = kzalloc(sizeof(*request), GFP_KERNEL);
Eric Anholt673a3942008-07-30 12:06:12 -07001584 if (request == NULL)
1585 return 0;
1586
Zou Nan hai852835f2010-05-21 09:08:56 +08001587 seqno = ring->add_request(dev, ring, file_priv, flush_domains);
Eric Anholt673a3942008-07-30 12:06:12 -07001588
1589 request->seqno = seqno;
Zou Nan hai852835f2010-05-21 09:08:56 +08001590 request->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001591 request->emitted_jiffies = jiffies;
Zou Nan hai852835f2010-05-21 09:08:56 +08001592 was_empty = list_empty(&ring->request_list);
1593 list_add_tail(&request->list, &ring->request_list);
1594
Eric Anholtb9624422009-06-03 07:27:35 +00001595 if (i915_file_priv) {
1596 list_add_tail(&request->client_list,
1597 &i915_file_priv->mm.request_list);
1598 } else {
1599 INIT_LIST_HEAD(&request->client_list);
1600 }
Eric Anholt673a3942008-07-30 12:06:12 -07001601
Eric Anholtce44b0e2008-11-06 16:00:31 -08001602 /* Associate any objects on the flushing list matching the write
1603 * domain we're flushing with our flush.
1604 */
Daniel Vetter63560392010-02-19 11:51:59 +01001605 if (flush_domains != 0)
Zou Nan hai852835f2010-05-21 09:08:56 +08001606 i915_gem_process_flushing_list(dev, flush_domains, seqno, ring);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001607
Ben Gamarif65d9422009-09-14 17:48:44 -04001608 if (!dev_priv->mm.suspended) {
1609 mod_timer(&dev_priv->hangcheck_timer, jiffies + DRM_I915_HANGCHECK_PERIOD);
1610 if (was_empty)
1611 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
1612 }
Eric Anholt673a3942008-07-30 12:06:12 -07001613 return seqno;
1614}
1615
1616/**
1617 * Command execution barrier
1618 *
1619 * Ensures that all commands in the ring are finished
1620 * before signalling the CPU
1621 */
Eric Anholt3043c602008-10-02 12:24:47 -07001622static uint32_t
Zou Nan hai852835f2010-05-21 09:08:56 +08001623i915_retire_commands(struct drm_device *dev, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001624{
Eric Anholt673a3942008-07-30 12:06:12 -07001625 uint32_t flush_domains = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001626
1627 /* The sampler always gets flushed on i965 (sigh) */
1628 if (IS_I965G(dev))
1629 flush_domains |= I915_GEM_DOMAIN_SAMPLER;
Zou Nan hai852835f2010-05-21 09:08:56 +08001630
1631 ring->flush(dev, ring,
1632 I915_GEM_DOMAIN_COMMAND, flush_domains);
Eric Anholt673a3942008-07-30 12:06:12 -07001633 return flush_domains;
1634}
1635
1636/**
1637 * Moves buffers associated only with the given active seqno from the active
1638 * to inactive list, potentially freeing them.
1639 */
1640static void
1641i915_gem_retire_request(struct drm_device *dev,
1642 struct drm_i915_gem_request *request)
1643{
1644 drm_i915_private_t *dev_priv = dev->dev_private;
1645
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001646 trace_i915_gem_request_retire(dev, request->seqno);
1647
Eric Anholt673a3942008-07-30 12:06:12 -07001648 /* Move any buffers on the active list that are no longer referenced
1649 * by the ringbuffer to the flushing/inactive lists as appropriate.
1650 */
Carl Worth5e118f42009-03-20 11:54:25 -07001651 spin_lock(&dev_priv->mm.active_list_lock);
Zou Nan hai852835f2010-05-21 09:08:56 +08001652 while (!list_empty(&request->ring->active_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001653 struct drm_gem_object *obj;
1654 struct drm_i915_gem_object *obj_priv;
1655
Zou Nan hai852835f2010-05-21 09:08:56 +08001656 obj_priv = list_first_entry(&request->ring->active_list,
Eric Anholt673a3942008-07-30 12:06:12 -07001657 struct drm_i915_gem_object,
1658 list);
Daniel Vettera8089e82010-04-09 19:05:09 +00001659 obj = &obj_priv->base;
Eric Anholt673a3942008-07-30 12:06:12 -07001660
1661 /* If the seqno being retired doesn't match the oldest in the
1662 * list, then the oldest in the list must still be newer than
1663 * this seqno.
1664 */
1665 if (obj_priv->last_rendering_seqno != request->seqno)
Carl Worth5e118f42009-03-20 11:54:25 -07001666 goto out;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001667
Eric Anholt673a3942008-07-30 12:06:12 -07001668#if WATCH_LRU
1669 DRM_INFO("%s: retire %d moves to inactive list %p\n",
1670 __func__, request->seqno, obj);
1671#endif
1672
Eric Anholtce44b0e2008-11-06 16:00:31 -08001673 if (obj->write_domain != 0)
1674 i915_gem_object_move_to_flushing(obj);
Shaohua Li68c84342009-04-08 10:58:23 +08001675 else {
1676 /* Take a reference on the object so it won't be
1677 * freed while the spinlock is held. The list
1678 * protection for this spinlock is safe when breaking
1679 * the lock like this since the next thing we do
1680 * is just get the head of the list again.
1681 */
1682 drm_gem_object_reference(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001683 i915_gem_object_move_to_inactive(obj);
Shaohua Li68c84342009-04-08 10:58:23 +08001684 spin_unlock(&dev_priv->mm.active_list_lock);
1685 drm_gem_object_unreference(obj);
1686 spin_lock(&dev_priv->mm.active_list_lock);
1687 }
Eric Anholt673a3942008-07-30 12:06:12 -07001688 }
Carl Worth5e118f42009-03-20 11:54:25 -07001689out:
1690 spin_unlock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001691}
1692
1693/**
1694 * Returns true if seq1 is later than seq2.
1695 */
Ben Gamari22be1722009-09-14 17:48:43 -04001696bool
Eric Anholt673a3942008-07-30 12:06:12 -07001697i915_seqno_passed(uint32_t seq1, uint32_t seq2)
1698{
1699 return (int32_t)(seq1 - seq2) >= 0;
1700}
1701
1702uint32_t
Zou Nan hai852835f2010-05-21 09:08:56 +08001703i915_get_gem_seqno(struct drm_device *dev,
Zou Nan haid1b851f2010-05-21 09:08:57 +08001704 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001705{
Zou Nan hai852835f2010-05-21 09:08:56 +08001706 return ring->get_gem_seqno(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07001707}
1708
1709/**
1710 * This function clears the request list as sequence numbers are passed.
1711 */
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001712static void
1713i915_gem_retire_requests_ring(struct drm_device *dev,
1714 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001715{
1716 drm_i915_private_t *dev_priv = dev->dev_private;
1717 uint32_t seqno;
1718
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001719 if (!ring->status_page.page_addr
Zou Nan hai852835f2010-05-21 09:08:56 +08001720 || list_empty(&ring->request_list))
Karsten Wiese6c0594a2009-02-23 15:07:57 +01001721 return;
1722
Zou Nan hai852835f2010-05-21 09:08:56 +08001723 seqno = i915_get_gem_seqno(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07001724
Zou Nan hai852835f2010-05-21 09:08:56 +08001725 while (!list_empty(&ring->request_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001726 struct drm_i915_gem_request *request;
1727 uint32_t retiring_seqno;
1728
Zou Nan hai852835f2010-05-21 09:08:56 +08001729 request = list_first_entry(&ring->request_list,
Eric Anholt673a3942008-07-30 12:06:12 -07001730 struct drm_i915_gem_request,
1731 list);
1732 retiring_seqno = request->seqno;
1733
1734 if (i915_seqno_passed(seqno, retiring_seqno) ||
Ben Gamariba1234d2009-09-14 17:48:47 -04001735 atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001736 i915_gem_retire_request(dev, request);
1737
1738 list_del(&request->list);
Eric Anholtb9624422009-06-03 07:27:35 +00001739 list_del(&request->client_list);
Eric Anholt9a298b22009-03-24 12:23:04 -07001740 kfree(request);
Eric Anholt673a3942008-07-30 12:06:12 -07001741 } else
1742 break;
1743 }
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001744
1745 if (unlikely (dev_priv->trace_irq_seqno &&
1746 i915_seqno_passed(dev_priv->trace_irq_seqno, seqno))) {
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001747
1748 ring->user_irq_put(dev, ring);
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001749 dev_priv->trace_irq_seqno = 0;
1750 }
Eric Anholt673a3942008-07-30 12:06:12 -07001751}
1752
1753void
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001754i915_gem_retire_requests(struct drm_device *dev)
1755{
1756 drm_i915_private_t *dev_priv = dev->dev_private;
1757
Chris Wilsonbe726152010-07-23 23:18:50 +01001758 if (!list_empty(&dev_priv->mm.deferred_free_list)) {
1759 struct drm_i915_gem_object *obj_priv, *tmp;
1760
1761 /* We must be careful that during unbind() we do not
1762 * accidentally infinitely recurse into retire requests.
1763 * Currently:
1764 * retire -> free -> unbind -> wait -> retire_ring
1765 */
1766 list_for_each_entry_safe(obj_priv, tmp,
1767 &dev_priv->mm.deferred_free_list,
1768 list)
1769 i915_gem_free_object_tail(&obj_priv->base);
1770 }
1771
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001772 i915_gem_retire_requests_ring(dev, &dev_priv->render_ring);
1773 if (HAS_BSD(dev))
1774 i915_gem_retire_requests_ring(dev, &dev_priv->bsd_ring);
1775}
1776
1777void
Eric Anholt673a3942008-07-30 12:06:12 -07001778i915_gem_retire_work_handler(struct work_struct *work)
1779{
1780 drm_i915_private_t *dev_priv;
1781 struct drm_device *dev;
1782
1783 dev_priv = container_of(work, drm_i915_private_t,
1784 mm.retire_work.work);
1785 dev = dev_priv->dev;
1786
1787 mutex_lock(&dev->struct_mutex);
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001788 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08001789
Keith Packard6dbe2772008-10-14 21:41:13 -07001790 if (!dev_priv->mm.suspended &&
Zou Nan haid1b851f2010-05-21 09:08:57 +08001791 (!list_empty(&dev_priv->render_ring.request_list) ||
1792 (HAS_BSD(dev) &&
1793 !list_empty(&dev_priv->bsd_ring.request_list))))
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001794 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
Eric Anholt673a3942008-07-30 12:06:12 -07001795 mutex_unlock(&dev->struct_mutex);
1796}
1797
Daniel Vetter5a5a0c62009-09-15 22:57:36 +02001798int
Zou Nan hai852835f2010-05-21 09:08:56 +08001799i915_do_wait_request(struct drm_device *dev, uint32_t seqno,
1800 int interruptible, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001801{
1802 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001803 u32 ier;
Eric Anholt673a3942008-07-30 12:06:12 -07001804 int ret = 0;
1805
1806 BUG_ON(seqno == 0);
1807
Ben Gamariba1234d2009-09-14 17:48:47 -04001808 if (atomic_read(&dev_priv->mm.wedged))
Ben Gamariffed1d02009-09-14 17:48:41 -04001809 return -EIO;
1810
Zou Nan hai852835f2010-05-21 09:08:56 +08001811 if (!i915_seqno_passed(ring->get_gem_seqno(dev, ring), seqno)) {
Eric Anholtbad720f2009-10-22 16:11:14 -07001812 if (HAS_PCH_SPLIT(dev))
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001813 ier = I915_READ(DEIER) | I915_READ(GTIER);
1814 else
1815 ier = I915_READ(IER);
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001816 if (!ier) {
1817 DRM_ERROR("something (likely vbetool) disabled "
1818 "interrupts, re-enabling\n");
1819 i915_driver_irq_preinstall(dev);
1820 i915_driver_irq_postinstall(dev);
1821 }
1822
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001823 trace_i915_gem_request_wait_begin(dev, seqno);
1824
Zou Nan hai852835f2010-05-21 09:08:56 +08001825 ring->waiting_gem_seqno = seqno;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001826 ring->user_irq_get(dev, ring);
Daniel Vetter48764bf2009-09-15 22:57:32 +02001827 if (interruptible)
Zou Nan hai852835f2010-05-21 09:08:56 +08001828 ret = wait_event_interruptible(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 else
Zou Nan hai852835f2010-05-21 09:08:56 +08001833 wait_event(ring->irq_queue,
1834 i915_seqno_passed(
1835 ring->get_gem_seqno(dev, ring), seqno)
1836 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02001837
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001838 ring->user_irq_put(dev, ring);
Zou Nan hai852835f2010-05-21 09:08:56 +08001839 ring->waiting_gem_seqno = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001840
1841 trace_i915_gem_request_wait_end(dev, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07001842 }
Ben Gamariba1234d2009-09-14 17:48:47 -04001843 if (atomic_read(&dev_priv->mm.wedged))
Eric Anholt673a3942008-07-30 12:06:12 -07001844 ret = -EIO;
1845
1846 if (ret && ret != -ERESTARTSYS)
1847 DRM_ERROR("%s returns %d (awaiting %d at %d)\n",
Zou Nan hai852835f2010-05-21 09:08:56 +08001848 __func__, ret, seqno, ring->get_gem_seqno(dev, ring));
Eric Anholt673a3942008-07-30 12:06:12 -07001849
1850 /* Directly dispatch request retiring. While we have the work queue
1851 * to handle this, the waiter on a request often wants an associated
1852 * buffer to have made it to the inactive list, and we would need
1853 * a separate wait queue to handle that.
1854 */
1855 if (ret == 0)
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001856 i915_gem_retire_requests_ring(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07001857
1858 return ret;
1859}
1860
Daniel Vetter48764bf2009-09-15 22:57:32 +02001861/**
1862 * Waits for a sequence number to be signaled, and cleans up the
1863 * request and object lists appropriately for that event.
1864 */
1865static int
Zou Nan hai852835f2010-05-21 09:08:56 +08001866i915_wait_request(struct drm_device *dev, uint32_t seqno,
1867 struct intel_ring_buffer *ring)
Daniel Vetter48764bf2009-09-15 22:57:32 +02001868{
Zou Nan hai852835f2010-05-21 09:08:56 +08001869 return i915_do_wait_request(dev, seqno, 1, ring);
Daniel Vetter48764bf2009-09-15 22:57:32 +02001870}
1871
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001872static void
1873i915_gem_flush(struct drm_device *dev,
1874 uint32_t invalidate_domains,
1875 uint32_t flush_domains)
1876{
1877 drm_i915_private_t *dev_priv = dev->dev_private;
1878 if (flush_domains & I915_GEM_DOMAIN_CPU)
1879 drm_agp_chipset_flush(dev);
1880 dev_priv->render_ring.flush(dev, &dev_priv->render_ring,
1881 invalidate_domains,
1882 flush_domains);
Zou Nan haid1b851f2010-05-21 09:08:57 +08001883
1884 if (HAS_BSD(dev))
1885 dev_priv->bsd_ring.flush(dev, &dev_priv->bsd_ring,
1886 invalidate_domains,
1887 flush_domains);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001888}
1889
Zou Nan hai852835f2010-05-21 09:08:56 +08001890static void
1891i915_gem_flush_ring(struct drm_device *dev,
1892 uint32_t invalidate_domains,
1893 uint32_t flush_domains,
1894 struct intel_ring_buffer *ring)
1895{
1896 if (flush_domains & I915_GEM_DOMAIN_CPU)
1897 drm_agp_chipset_flush(dev);
1898 ring->flush(dev, ring,
1899 invalidate_domains,
1900 flush_domains);
1901}
1902
Eric Anholt673a3942008-07-30 12:06:12 -07001903/**
1904 * Ensures that all rendering to the object has completed and the object is
1905 * safe to unbind from the GTT or access from the CPU.
1906 */
1907static int
1908i915_gem_object_wait_rendering(struct drm_gem_object *obj)
1909{
1910 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001911 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001912 int ret;
1913
Eric Anholte47c68e2008-11-14 13:35:19 -08001914 /* This function only exists to support waiting for existing rendering,
1915 * not for emitting required flushes.
Eric Anholt673a3942008-07-30 12:06:12 -07001916 */
Eric Anholte47c68e2008-11-14 13:35:19 -08001917 BUG_ON((obj->write_domain & I915_GEM_GPU_DOMAINS) != 0);
Eric Anholt673a3942008-07-30 12:06:12 -07001918
1919 /* If there is rendering queued on the buffer being evicted, wait for
1920 * it.
1921 */
1922 if (obj_priv->active) {
1923#if WATCH_BUF
1924 DRM_INFO("%s: object %p wait for seqno %08x\n",
1925 __func__, obj, obj_priv->last_rendering_seqno);
1926#endif
Zou Nan hai852835f2010-05-21 09:08:56 +08001927 ret = i915_wait_request(dev,
1928 obj_priv->last_rendering_seqno, obj_priv->ring);
Eric Anholt673a3942008-07-30 12:06:12 -07001929 if (ret != 0)
1930 return ret;
1931 }
1932
1933 return 0;
1934}
1935
1936/**
1937 * Unbinds an object from the GTT aperture.
1938 */
Jesse Barnes0f973f22009-01-26 17:10:45 -08001939int
Eric Anholt673a3942008-07-30 12:06:12 -07001940i915_gem_object_unbind(struct drm_gem_object *obj)
1941{
1942 struct drm_device *dev = obj->dev;
Daniel Vetter4a87b8c2010-02-19 11:51:57 +01001943 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001944 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001945 int ret = 0;
1946
1947#if WATCH_BUF
1948 DRM_INFO("%s:%d %p\n", __func__, __LINE__, obj);
1949 DRM_INFO("gtt_space %p\n", obj_priv->gtt_space);
1950#endif
1951 if (obj_priv->gtt_space == NULL)
1952 return 0;
1953
1954 if (obj_priv->pin_count != 0) {
1955 DRM_ERROR("Attempting to unbind pinned buffer\n");
1956 return -EINVAL;
1957 }
1958
Eric Anholt5323fd02009-09-09 11:50:45 -07001959 /* blow away mappings if mapped through GTT */
1960 i915_gem_release_mmap(obj);
1961
Eric Anholt673a3942008-07-30 12:06:12 -07001962 /* Move the object to the CPU domain to ensure that
1963 * any possible CPU writes while it's not in the GTT
1964 * are flushed when we go to remap it. This will
1965 * also ensure that all pending GPU writes are finished
1966 * before we unbind.
1967 */
Eric Anholte47c68e2008-11-14 13:35:19 -08001968 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Chris Wilson8dc17752010-07-23 23:18:51 +01001969 if (ret == -ERESTARTSYS)
Eric Anholt673a3942008-07-30 12:06:12 -07001970 return ret;
Chris Wilson8dc17752010-07-23 23:18:51 +01001971 /* Continue on if we fail due to EIO, the GPU is hung so we
1972 * should be safe and we need to cleanup or else we might
1973 * cause memory corruption through use-after-free.
1974 */
Eric Anholt673a3942008-07-30 12:06:12 -07001975
Eric Anholt5323fd02009-09-09 11:50:45 -07001976 BUG_ON(obj_priv->active);
1977
Daniel Vetter96b47b62009-12-15 17:50:00 +01001978 /* release the fence reg _after_ flushing */
1979 if (obj_priv->fence_reg != I915_FENCE_REG_NONE)
1980 i915_gem_clear_fence_reg(obj);
1981
Eric Anholt673a3942008-07-30 12:06:12 -07001982 if (obj_priv->agp_mem != NULL) {
1983 drm_unbind_agp(obj_priv->agp_mem);
1984 drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE);
1985 obj_priv->agp_mem = NULL;
1986 }
1987
Eric Anholt856fa192009-03-19 14:10:50 -07001988 i915_gem_object_put_pages(obj);
Chris Wilsona32808c2009-09-20 21:29:47 +01001989 BUG_ON(obj_priv->pages_refcount);
Eric Anholt673a3942008-07-30 12:06:12 -07001990
1991 if (obj_priv->gtt_space) {
1992 atomic_dec(&dev->gtt_count);
1993 atomic_sub(obj->size, &dev->gtt_memory);
1994
1995 drm_mm_put_block(obj_priv->gtt_space);
1996 obj_priv->gtt_space = NULL;
1997 }
1998
1999 /* Remove ourselves from the LRU list if present. */
Daniel Vetter4a87b8c2010-02-19 11:51:57 +01002000 spin_lock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07002001 if (!list_empty(&obj_priv->list))
2002 list_del_init(&obj_priv->list);
Daniel Vetter4a87b8c2010-02-19 11:51:57 +01002003 spin_unlock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07002004
Chris Wilson963b4832009-09-20 23:03:54 +01002005 if (i915_gem_object_is_purgeable(obj_priv))
2006 i915_gem_object_truncate(obj);
2007
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002008 trace_i915_gem_object_unbind(obj);
2009
Chris Wilson8dc17752010-07-23 23:18:51 +01002010 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002011}
2012
Chris Wilson07f73f62009-09-14 16:50:30 +01002013static struct drm_gem_object *
2014i915_gem_find_inactive_object(struct drm_device *dev, int min_size)
2015{
2016 drm_i915_private_t *dev_priv = dev->dev_private;
2017 struct drm_i915_gem_object *obj_priv;
2018 struct drm_gem_object *best = NULL;
2019 struct drm_gem_object *first = NULL;
2020
2021 /* Try to find the smallest clean object */
2022 list_for_each_entry(obj_priv, &dev_priv->mm.inactive_list, list) {
Daniel Vettera8089e82010-04-09 19:05:09 +00002023 struct drm_gem_object *obj = &obj_priv->base;
Chris Wilson07f73f62009-09-14 16:50:30 +01002024 if (obj->size >= min_size) {
Chris Wilson963b4832009-09-20 23:03:54 +01002025 if ((!obj_priv->dirty ||
2026 i915_gem_object_is_purgeable(obj_priv)) &&
Chris Wilson07f73f62009-09-14 16:50:30 +01002027 (!best || obj->size < best->size)) {
2028 best = obj;
2029 if (best->size == min_size)
2030 return best;
2031 }
2032 if (!first)
2033 first = obj;
2034 }
2035 }
2036
2037 return best ? best : first;
2038}
2039
Eric Anholt673a3942008-07-30 12:06:12 -07002040static int
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002041i915_gpu_idle(struct drm_device *dev)
2042{
2043 drm_i915_private_t *dev_priv = dev->dev_private;
2044 bool lists_empty;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002045 uint32_t seqno1, seqno2;
Zou Nan hai852835f2010-05-21 09:08:56 +08002046 int ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002047
2048 spin_lock(&dev_priv->mm.active_list_lock);
Zou Nan haid1b851f2010-05-21 09:08:57 +08002049 lists_empty = (list_empty(&dev_priv->mm.flushing_list) &&
2050 list_empty(&dev_priv->render_ring.active_list) &&
2051 (!HAS_BSD(dev) ||
2052 list_empty(&dev_priv->bsd_ring.active_list)));
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002053 spin_unlock(&dev_priv->mm.active_list_lock);
2054
2055 if (lists_empty)
2056 return 0;
2057
2058 /* Flush everything onto the inactive list. */
2059 i915_gem_flush(dev, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
Zou Nan haid1b851f2010-05-21 09:08:57 +08002060 seqno1 = i915_add_request(dev, NULL, I915_GEM_GPU_DOMAINS,
Zou Nan hai852835f2010-05-21 09:08:56 +08002061 &dev_priv->render_ring);
Zou Nan haid1b851f2010-05-21 09:08:57 +08002062 if (seqno1 == 0)
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002063 return -ENOMEM;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002064 ret = i915_wait_request(dev, seqno1, &dev_priv->render_ring);
2065
2066 if (HAS_BSD(dev)) {
2067 seqno2 = i915_add_request(dev, NULL, I915_GEM_GPU_DOMAINS,
2068 &dev_priv->bsd_ring);
2069 if (seqno2 == 0)
2070 return -ENOMEM;
2071
2072 ret = i915_wait_request(dev, seqno2, &dev_priv->bsd_ring);
2073 if (ret)
2074 return ret;
2075 }
2076
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002077
Zou Nan hai852835f2010-05-21 09:08:56 +08002078 return ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002079}
2080
2081static int
Chris Wilson07f73f62009-09-14 16:50:30 +01002082i915_gem_evict_everything(struct drm_device *dev)
2083{
2084 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson07f73f62009-09-14 16:50:30 +01002085 int ret;
2086 bool lists_empty;
2087
Chris Wilson07f73f62009-09-14 16:50:30 +01002088 spin_lock(&dev_priv->mm.active_list_lock);
2089 lists_empty = (list_empty(&dev_priv->mm.inactive_list) &&
2090 list_empty(&dev_priv->mm.flushing_list) &&
Zou Nan haid1b851f2010-05-21 09:08:57 +08002091 list_empty(&dev_priv->render_ring.active_list) &&
2092 (!HAS_BSD(dev)
2093 || list_empty(&dev_priv->bsd_ring.active_list)));
Chris Wilson07f73f62009-09-14 16:50:30 +01002094 spin_unlock(&dev_priv->mm.active_list_lock);
2095
Chris Wilson97311292009-09-21 00:22:34 +01002096 if (lists_empty)
Chris Wilson07f73f62009-09-14 16:50:30 +01002097 return -ENOSPC;
Chris Wilson07f73f62009-09-14 16:50:30 +01002098
2099 /* Flush everything (on to the inactive lists) and evict */
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002100 ret = i915_gpu_idle(dev);
Chris Wilson07f73f62009-09-14 16:50:30 +01002101 if (ret)
2102 return ret;
2103
Daniel Vetter99fcb762010-02-07 16:20:18 +01002104 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
2105
Chris Wilsonab5ee572009-09-20 19:25:47 +01002106 ret = i915_gem_evict_from_inactive_list(dev);
Chris Wilson07f73f62009-09-14 16:50:30 +01002107 if (ret)
2108 return ret;
2109
2110 spin_lock(&dev_priv->mm.active_list_lock);
2111 lists_empty = (list_empty(&dev_priv->mm.inactive_list) &&
2112 list_empty(&dev_priv->mm.flushing_list) &&
Zou Nan haid1b851f2010-05-21 09:08:57 +08002113 list_empty(&dev_priv->render_ring.active_list) &&
2114 (!HAS_BSD(dev)
2115 || list_empty(&dev_priv->bsd_ring.active_list)));
Chris Wilson07f73f62009-09-14 16:50:30 +01002116 spin_unlock(&dev_priv->mm.active_list_lock);
2117 BUG_ON(!lists_empty);
2118
Eric Anholt673a3942008-07-30 12:06:12 -07002119 return 0;
2120}
2121
2122static int
Chris Wilson07f73f62009-09-14 16:50:30 +01002123i915_gem_evict_something(struct drm_device *dev, int min_size)
Eric Anholt673a3942008-07-30 12:06:12 -07002124{
2125 drm_i915_private_t *dev_priv = dev->dev_private;
2126 struct drm_gem_object *obj;
Chris Wilson07f73f62009-09-14 16:50:30 +01002127 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002128
Zou Nan hai852835f2010-05-21 09:08:56 +08002129 struct intel_ring_buffer *render_ring = &dev_priv->render_ring;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002130 struct intel_ring_buffer *bsd_ring = &dev_priv->bsd_ring;
Eric Anholt673a3942008-07-30 12:06:12 -07002131 for (;;) {
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002132 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08002133
Eric Anholt673a3942008-07-30 12:06:12 -07002134 /* If there's an inactive buffer available now, grab it
2135 * and be done.
2136 */
Chris Wilson07f73f62009-09-14 16:50:30 +01002137 obj = i915_gem_find_inactive_object(dev, min_size);
2138 if (obj) {
2139 struct drm_i915_gem_object *obj_priv;
2140
Eric Anholt673a3942008-07-30 12:06:12 -07002141#if WATCH_LRU
2142 DRM_INFO("%s: evicting %p\n", __func__, obj);
2143#endif
Daniel Vetter23010e42010-03-08 13:35:02 +01002144 obj_priv = to_intel_bo(obj);
Chris Wilson07f73f62009-09-14 16:50:30 +01002145 BUG_ON(obj_priv->pin_count != 0);
Eric Anholt673a3942008-07-30 12:06:12 -07002146 BUG_ON(obj_priv->active);
2147
2148 /* Wait on the rendering and unbind the buffer. */
Chris Wilson07f73f62009-09-14 16:50:30 +01002149 return i915_gem_object_unbind(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002150 }
2151
2152 /* If we didn't get anything, but the ring is still processing
Chris Wilson07f73f62009-09-14 16:50:30 +01002153 * things, wait for the next to finish and hopefully leave us
2154 * a buffer to evict.
Eric Anholt673a3942008-07-30 12:06:12 -07002155 */
Zou Nan hai852835f2010-05-21 09:08:56 +08002156 if (!list_empty(&render_ring->request_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002157 struct drm_i915_gem_request *request;
2158
Zou Nan hai852835f2010-05-21 09:08:56 +08002159 request = list_first_entry(&render_ring->request_list,
Eric Anholt673a3942008-07-30 12:06:12 -07002160 struct drm_i915_gem_request,
2161 list);
2162
Zou Nan hai852835f2010-05-21 09:08:56 +08002163 ret = i915_wait_request(dev,
2164 request->seqno, request->ring);
Eric Anholt673a3942008-07-30 12:06:12 -07002165 if (ret)
Chris Wilson07f73f62009-09-14 16:50:30 +01002166 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002167
Chris Wilson07f73f62009-09-14 16:50:30 +01002168 continue;
Eric Anholt673a3942008-07-30 12:06:12 -07002169 }
2170
Zou Nan haid1b851f2010-05-21 09:08:57 +08002171 if (HAS_BSD(dev) && !list_empty(&bsd_ring->request_list)) {
2172 struct drm_i915_gem_request *request;
2173
2174 request = list_first_entry(&bsd_ring->request_list,
2175 struct drm_i915_gem_request,
2176 list);
2177
2178 ret = i915_wait_request(dev,
2179 request->seqno, request->ring);
2180 if (ret)
2181 return ret;
2182
2183 continue;
2184 }
2185
Eric Anholt673a3942008-07-30 12:06:12 -07002186 /* If we didn't have anything on the request list but there
2187 * are buffers awaiting a flush, emit one and try again.
2188 * When we wait on it, those buffers waiting for that flush
2189 * will get moved to inactive.
2190 */
2191 if (!list_empty(&dev_priv->mm.flushing_list)) {
Chris Wilson07f73f62009-09-14 16:50:30 +01002192 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07002193
Chris Wilson9a1e2582009-09-20 20:16:50 +01002194 /* Find an object that we can immediately reuse */
2195 list_for_each_entry(obj_priv, &dev_priv->mm.flushing_list, list) {
Daniel Vettera8089e82010-04-09 19:05:09 +00002196 obj = &obj_priv->base;
Chris Wilson9a1e2582009-09-20 20:16:50 +01002197 if (obj->size >= min_size)
2198 break;
Eric Anholt673a3942008-07-30 12:06:12 -07002199
Chris Wilson9a1e2582009-09-20 20:16:50 +01002200 obj = NULL;
2201 }
Eric Anholt673a3942008-07-30 12:06:12 -07002202
Chris Wilson9a1e2582009-09-20 20:16:50 +01002203 if (obj != NULL) {
2204 uint32_t seqno;
Chris Wilson07f73f62009-09-14 16:50:30 +01002205
Zou Nan hai852835f2010-05-21 09:08:56 +08002206 i915_gem_flush_ring(dev,
Chris Wilson9a1e2582009-09-20 20:16:50 +01002207 obj->write_domain,
Zou Nan hai852835f2010-05-21 09:08:56 +08002208 obj->write_domain,
2209 obj_priv->ring);
2210 seqno = i915_add_request(dev, NULL,
2211 obj->write_domain,
2212 obj_priv->ring);
Chris Wilson9a1e2582009-09-20 20:16:50 +01002213 if (seqno == 0)
2214 return -ENOMEM;
Chris Wilson9a1e2582009-09-20 20:16:50 +01002215 continue;
2216 }
Eric Anholt673a3942008-07-30 12:06:12 -07002217 }
2218
Chris Wilson07f73f62009-09-14 16:50:30 +01002219 /* If we didn't do any of the above, there's no single buffer
2220 * large enough to swap out for the new one, so just evict
2221 * everything and start again. (This should be rare.)
Eric Anholt673a3942008-07-30 12:06:12 -07002222 */
Chris Wilson97311292009-09-21 00:22:34 +01002223 if (!list_empty (&dev_priv->mm.inactive_list))
Chris Wilsonab5ee572009-09-20 19:25:47 +01002224 return i915_gem_evict_from_inactive_list(dev);
Chris Wilson97311292009-09-21 00:22:34 +01002225 else
Chris Wilson07f73f62009-09-14 16:50:30 +01002226 return i915_gem_evict_everything(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07002227 }
Keith Packardac94a962008-11-20 23:30:27 -08002228}
2229
Ben Gamari6911a9b2009-04-02 11:24:54 -07002230int
Chris Wilson4bdadb92010-01-27 13:36:32 +00002231i915_gem_object_get_pages(struct drm_gem_object *obj,
2232 gfp_t gfpmask)
Eric Anholt673a3942008-07-30 12:06:12 -07002233{
Daniel Vetter23010e42010-03-08 13:35:02 +01002234 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002235 int page_count, i;
2236 struct address_space *mapping;
2237 struct inode *inode;
2238 struct page *page;
Eric Anholt673a3942008-07-30 12:06:12 -07002239
Daniel Vetter778c3542010-05-13 11:49:44 +02002240 BUG_ON(obj_priv->pages_refcount
2241 == DRM_I915_GEM_OBJECT_MAX_PAGES_REFCOUNT);
2242
Eric Anholt856fa192009-03-19 14:10:50 -07002243 if (obj_priv->pages_refcount++ != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07002244 return 0;
2245
2246 /* Get the list of pages out of our struct file. They'll be pinned
2247 * at this point until we release them.
2248 */
2249 page_count = obj->size / PAGE_SIZE;
Eric Anholt856fa192009-03-19 14:10:50 -07002250 BUG_ON(obj_priv->pages != NULL);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07002251 obj_priv->pages = drm_calloc_large(page_count, sizeof(struct page *));
Eric Anholt856fa192009-03-19 14:10:50 -07002252 if (obj_priv->pages == NULL) {
Eric Anholt856fa192009-03-19 14:10:50 -07002253 obj_priv->pages_refcount--;
Eric Anholt673a3942008-07-30 12:06:12 -07002254 return -ENOMEM;
2255 }
2256
2257 inode = obj->filp->f_path.dentry->d_inode;
2258 mapping = inode->i_mapping;
2259 for (i = 0; i < page_count; i++) {
Chris Wilson4bdadb92010-01-27 13:36:32 +00002260 page = read_cache_page_gfp(mapping, i,
Linus Torvalds985b8232010-07-02 10:04:42 +10002261 GFP_HIGHUSER |
Chris Wilson4bdadb92010-01-27 13:36:32 +00002262 __GFP_COLD |
Linus Torvaldscd9f0402010-07-18 09:44:37 -07002263 __GFP_RECLAIMABLE |
Chris Wilson4bdadb92010-01-27 13:36:32 +00002264 gfpmask);
Chris Wilson1f2b1012010-03-12 19:52:55 +00002265 if (IS_ERR(page))
2266 goto err_pages;
2267
Eric Anholt856fa192009-03-19 14:10:50 -07002268 obj_priv->pages[i] = page;
Eric Anholt673a3942008-07-30 12:06:12 -07002269 }
Eric Anholt280b7132009-03-12 16:56:27 -07002270
2271 if (obj_priv->tiling_mode != I915_TILING_NONE)
2272 i915_gem_object_do_bit_17_swizzle(obj);
2273
Eric Anholt673a3942008-07-30 12:06:12 -07002274 return 0;
Chris Wilson1f2b1012010-03-12 19:52:55 +00002275
2276err_pages:
2277 while (i--)
2278 page_cache_release(obj_priv->pages[i]);
2279
2280 drm_free_large(obj_priv->pages);
2281 obj_priv->pages = NULL;
2282 obj_priv->pages_refcount--;
2283 return PTR_ERR(page);
Eric Anholt673a3942008-07-30 12:06:12 -07002284}
2285
Eric Anholt4e901fd2009-10-26 16:44:17 -07002286static void sandybridge_write_fence_reg(struct drm_i915_fence_reg *reg)
2287{
2288 struct drm_gem_object *obj = reg->obj;
2289 struct drm_device *dev = obj->dev;
2290 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002291 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt4e901fd2009-10-26 16:44:17 -07002292 int regnum = obj_priv->fence_reg;
2293 uint64_t val;
2294
2295 val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
2296 0xfffff000) << 32;
2297 val |= obj_priv->gtt_offset & 0xfffff000;
2298 val |= (uint64_t)((obj_priv->stride / 128) - 1) <<
2299 SANDYBRIDGE_FENCE_PITCH_SHIFT;
2300
2301 if (obj_priv->tiling_mode == I915_TILING_Y)
2302 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2303 val |= I965_FENCE_REG_VALID;
2304
2305 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (regnum * 8), val);
2306}
2307
Jesse Barnesde151cf2008-11-12 10:03:55 -08002308static void i965_write_fence_reg(struct drm_i915_fence_reg *reg)
2309{
2310 struct drm_gem_object *obj = reg->obj;
2311 struct drm_device *dev = obj->dev;
2312 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002313 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002314 int regnum = obj_priv->fence_reg;
2315 uint64_t val;
2316
2317 val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
2318 0xfffff000) << 32;
2319 val |= obj_priv->gtt_offset & 0xfffff000;
2320 val |= ((obj_priv->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
2321 if (obj_priv->tiling_mode == I915_TILING_Y)
2322 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2323 val |= I965_FENCE_REG_VALID;
2324
2325 I915_WRITE64(FENCE_REG_965_0 + (regnum * 8), val);
2326}
2327
2328static void i915_write_fence_reg(struct drm_i915_fence_reg *reg)
2329{
2330 struct drm_gem_object *obj = reg->obj;
2331 struct drm_device *dev = obj->dev;
2332 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002333 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002334 int regnum = obj_priv->fence_reg;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002335 int tile_width;
Eric Anholtdc529a42009-03-10 22:34:49 -07002336 uint32_t fence_reg, val;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002337 uint32_t pitch_val;
2338
2339 if ((obj_priv->gtt_offset & ~I915_FENCE_START_MASK) ||
2340 (obj_priv->gtt_offset & (obj->size - 1))) {
Linus Torvaldsf06da262009-02-09 08:57:29 -08002341 WARN(1, "%s: object 0x%08x not 1M or size (0x%zx) aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002342 __func__, obj_priv->gtt_offset, obj->size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002343 return;
2344 }
2345
Jesse Barnes0f973f22009-01-26 17:10:45 -08002346 if (obj_priv->tiling_mode == I915_TILING_Y &&
2347 HAS_128_BYTE_Y_TILING(dev))
2348 tile_width = 128;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002349 else
Jesse Barnes0f973f22009-01-26 17:10:45 -08002350 tile_width = 512;
2351
2352 /* Note: pitch better be a power of two tile widths */
2353 pitch_val = obj_priv->stride / tile_width;
2354 pitch_val = ffs(pitch_val) - 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002355
Daniel Vetterc36a2a62010-04-17 15:12:03 +02002356 if (obj_priv->tiling_mode == I915_TILING_Y &&
2357 HAS_128_BYTE_Y_TILING(dev))
2358 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2359 else
2360 WARN_ON(pitch_val > I915_FENCE_MAX_PITCH_VAL);
2361
Jesse Barnesde151cf2008-11-12 10:03:55 -08002362 val = obj_priv->gtt_offset;
2363 if (obj_priv->tiling_mode == I915_TILING_Y)
2364 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2365 val |= I915_FENCE_SIZE_BITS(obj->size);
2366 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2367 val |= I830_FENCE_REG_VALID;
2368
Eric Anholtdc529a42009-03-10 22:34:49 -07002369 if (regnum < 8)
2370 fence_reg = FENCE_REG_830_0 + (regnum * 4);
2371 else
2372 fence_reg = FENCE_REG_945_8 + ((regnum - 8) * 4);
2373 I915_WRITE(fence_reg, val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002374}
2375
2376static void i830_write_fence_reg(struct drm_i915_fence_reg *reg)
2377{
2378 struct drm_gem_object *obj = reg->obj;
2379 struct drm_device *dev = obj->dev;
2380 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002381 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002382 int regnum = obj_priv->fence_reg;
2383 uint32_t val;
2384 uint32_t pitch_val;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002385 uint32_t fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002386
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002387 if ((obj_priv->gtt_offset & ~I830_FENCE_START_MASK) ||
Jesse Barnesde151cf2008-11-12 10:03:55 -08002388 (obj_priv->gtt_offset & (obj->size - 1))) {
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002389 WARN(1, "%s: object 0x%08x not 512K or size aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002390 __func__, obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002391 return;
2392 }
2393
Eric Anholte76a16d2009-05-26 17:44:56 -07002394 pitch_val = obj_priv->stride / 128;
2395 pitch_val = ffs(pitch_val) - 1;
2396 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2397
Jesse Barnesde151cf2008-11-12 10:03:55 -08002398 val = obj_priv->gtt_offset;
2399 if (obj_priv->tiling_mode == I915_TILING_Y)
2400 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002401 fence_size_bits = I830_FENCE_SIZE_BITS(obj->size);
2402 WARN_ON(fence_size_bits & ~0x00000f00);
2403 val |= fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002404 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2405 val |= I830_FENCE_REG_VALID;
2406
2407 I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002408}
2409
Daniel Vetterae3db242010-02-19 11:51:58 +01002410static int i915_find_fence_reg(struct drm_device *dev)
2411{
2412 struct drm_i915_fence_reg *reg = NULL;
2413 struct drm_i915_gem_object *obj_priv = NULL;
2414 struct drm_i915_private *dev_priv = dev->dev_private;
2415 struct drm_gem_object *obj = NULL;
2416 int i, avail, ret;
2417
2418 /* First try to find a free reg */
2419 avail = 0;
2420 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2421 reg = &dev_priv->fence_regs[i];
2422 if (!reg->obj)
2423 return i;
2424
Daniel Vetter23010e42010-03-08 13:35:02 +01002425 obj_priv = to_intel_bo(reg->obj);
Daniel Vetterae3db242010-02-19 11:51:58 +01002426 if (!obj_priv->pin_count)
2427 avail++;
2428 }
2429
2430 if (avail == 0)
2431 return -ENOSPC;
2432
2433 /* None available, try to steal one or wait for a user to finish */
2434 i = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002435 list_for_each_entry(reg, &dev_priv->mm.fence_list,
2436 lru_list) {
2437 obj = reg->obj;
2438 obj_priv = to_intel_bo(obj);
Daniel Vetterae3db242010-02-19 11:51:58 +01002439
2440 if (obj_priv->pin_count)
2441 continue;
2442
2443 /* found one! */
2444 i = obj_priv->fence_reg;
2445 break;
2446 }
2447
2448 BUG_ON(i == I915_FENCE_REG_NONE);
2449
2450 /* We only have a reference on obj from the active list. put_fence_reg
2451 * might drop that one, causing a use-after-free in it. So hold a
2452 * private reference to obj like the other callers of put_fence_reg
2453 * (set_tiling ioctl) do. */
2454 drm_gem_object_reference(obj);
2455 ret = i915_gem_object_put_fence_reg(obj);
2456 drm_gem_object_unreference(obj);
2457 if (ret != 0)
2458 return ret;
2459
2460 return i;
2461}
2462
Jesse Barnesde151cf2008-11-12 10:03:55 -08002463/**
2464 * i915_gem_object_get_fence_reg - set up a fence reg for an object
2465 * @obj: object to map through a fence reg
2466 *
2467 * When mapping objects through the GTT, userspace wants to be able to write
2468 * to them without having to worry about swizzling if the object is tiled.
2469 *
2470 * This function walks the fence regs looking for a free one for @obj,
2471 * stealing one if it can't find any.
2472 *
2473 * It then sets up the reg based on the object's properties: address, pitch
2474 * and tiling format.
2475 */
Chris Wilson8c4b8c32009-06-17 22:08:52 +01002476int
2477i915_gem_object_get_fence_reg(struct drm_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002478{
2479 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002480 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002481 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002482 struct drm_i915_fence_reg *reg = NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002483 int ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002484
Eric Anholta09ba7f2009-08-29 12:49:51 -07002485 /* Just update our place in the LRU if our fence is getting used. */
2486 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002487 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
2488 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002489 return 0;
2490 }
2491
Jesse Barnesde151cf2008-11-12 10:03:55 -08002492 switch (obj_priv->tiling_mode) {
2493 case I915_TILING_NONE:
2494 WARN(1, "allocating a fence for non-tiled object?\n");
2495 break;
2496 case I915_TILING_X:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002497 if (!obj_priv->stride)
2498 return -EINVAL;
2499 WARN((obj_priv->stride & (512 - 1)),
2500 "object 0x%08x is X tiled but has non-512B pitch\n",
2501 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002502 break;
2503 case I915_TILING_Y:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002504 if (!obj_priv->stride)
2505 return -EINVAL;
2506 WARN((obj_priv->stride & (128 - 1)),
2507 "object 0x%08x is Y tiled but has non-128B pitch\n",
2508 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002509 break;
2510 }
2511
Daniel Vetterae3db242010-02-19 11:51:58 +01002512 ret = i915_find_fence_reg(dev);
2513 if (ret < 0)
2514 return ret;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002515
Daniel Vetterae3db242010-02-19 11:51:58 +01002516 obj_priv->fence_reg = ret;
2517 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002518 list_add_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002519
Jesse Barnesde151cf2008-11-12 10:03:55 -08002520 reg->obj = obj;
2521
Eric Anholt4e901fd2009-10-26 16:44:17 -07002522 if (IS_GEN6(dev))
2523 sandybridge_write_fence_reg(reg);
2524 else if (IS_I965G(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08002525 i965_write_fence_reg(reg);
2526 else if (IS_I9XX(dev))
2527 i915_write_fence_reg(reg);
2528 else
2529 i830_write_fence_reg(reg);
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002530
Daniel Vetterae3db242010-02-19 11:51:58 +01002531 trace_i915_gem_object_get_fence(obj, obj_priv->fence_reg,
2532 obj_priv->tiling_mode);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002533
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002534 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002535}
2536
2537/**
2538 * i915_gem_clear_fence_reg - clear out fence register info
2539 * @obj: object to clear
2540 *
2541 * Zeroes out the fence register itself and clears out the associated
2542 * data structures in dev_priv and obj_priv.
2543 */
2544static void
2545i915_gem_clear_fence_reg(struct drm_gem_object *obj)
2546{
2547 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002548 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002549 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002550 struct drm_i915_fence_reg *reg =
2551 &dev_priv->fence_regs[obj_priv->fence_reg];
Jesse Barnesde151cf2008-11-12 10:03:55 -08002552
Eric Anholt4e901fd2009-10-26 16:44:17 -07002553 if (IS_GEN6(dev)) {
2554 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 +
2555 (obj_priv->fence_reg * 8), 0);
2556 } else if (IS_I965G(dev)) {
Jesse Barnesde151cf2008-11-12 10:03:55 -08002557 I915_WRITE64(FENCE_REG_965_0 + (obj_priv->fence_reg * 8), 0);
Eric Anholt4e901fd2009-10-26 16:44:17 -07002558 } else {
Eric Anholtdc529a42009-03-10 22:34:49 -07002559 uint32_t fence_reg;
2560
2561 if (obj_priv->fence_reg < 8)
2562 fence_reg = FENCE_REG_830_0 + obj_priv->fence_reg * 4;
2563 else
2564 fence_reg = FENCE_REG_945_8 + (obj_priv->fence_reg -
2565 8) * 4;
2566
2567 I915_WRITE(fence_reg, 0);
2568 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002569
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002570 reg->obj = NULL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002571 obj_priv->fence_reg = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002572 list_del_init(&reg->lru_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002573}
2574
Eric Anholt673a3942008-07-30 12:06:12 -07002575/**
Chris Wilson52dc7d32009-06-06 09:46:01 +01002576 * i915_gem_object_put_fence_reg - waits on outstanding fenced access
2577 * to the buffer to finish, and then resets the fence register.
2578 * @obj: tiled object holding a fence register.
2579 *
2580 * Zeroes out the fence register itself and clears out the associated
2581 * data structures in dev_priv and obj_priv.
2582 */
2583int
2584i915_gem_object_put_fence_reg(struct drm_gem_object *obj)
2585{
2586 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01002587 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson52dc7d32009-06-06 09:46:01 +01002588
2589 if (obj_priv->fence_reg == I915_FENCE_REG_NONE)
2590 return 0;
2591
Daniel Vetter10ae9bd2010-02-01 13:59:17 +01002592 /* If we've changed tiling, GTT-mappings of the object
2593 * need to re-fault to ensure that the correct fence register
2594 * setup is in place.
2595 */
2596 i915_gem_release_mmap(obj);
2597
Chris Wilson52dc7d32009-06-06 09:46:01 +01002598 /* On the i915, GPU access to tiled buffers is via a fence,
2599 * therefore we must wait for any outstanding access to complete
2600 * before clearing the fence.
2601 */
2602 if (!IS_I965G(dev)) {
2603 int ret;
2604
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002605 ret = i915_gem_object_flush_gpu_write_domain(obj);
2606 if (ret != 0)
2607 return ret;
2608
Chris Wilson52dc7d32009-06-06 09:46:01 +01002609 ret = i915_gem_object_wait_rendering(obj);
2610 if (ret != 0)
2611 return ret;
2612 }
2613
Daniel Vetter4a726612010-02-01 13:59:16 +01002614 i915_gem_object_flush_gtt_write_domain(obj);
Chris Wilson52dc7d32009-06-06 09:46:01 +01002615 i915_gem_clear_fence_reg (obj);
2616
2617 return 0;
2618}
2619
2620/**
Eric Anholt673a3942008-07-30 12:06:12 -07002621 * Finds free space in the GTT aperture and binds the object there.
2622 */
2623static int
2624i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment)
2625{
2626 struct drm_device *dev = obj->dev;
2627 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002628 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002629 struct drm_mm_node *free_space;
Chris Wilson4bdadb92010-01-27 13:36:32 +00002630 gfp_t gfpmask = __GFP_NORETRY | __GFP_NOWARN;
Chris Wilson07f73f62009-09-14 16:50:30 +01002631 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002632
Chris Wilsonbb6baf72009-09-22 14:24:13 +01002633 if (obj_priv->madv != I915_MADV_WILLNEED) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01002634 DRM_ERROR("Attempting to bind a purgeable object\n");
2635 return -EINVAL;
2636 }
2637
Eric Anholt673a3942008-07-30 12:06:12 -07002638 if (alignment == 0)
Jesse Barnes0f973f22009-01-26 17:10:45 -08002639 alignment = i915_gem_get_gtt_alignment(obj);
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002640 if (alignment & (i915_gem_get_gtt_alignment(obj) - 1)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002641 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2642 return -EINVAL;
2643 }
2644
Chris Wilson654fc602010-05-27 13:18:21 +01002645 /* If the object is bigger than the entire aperture, reject it early
2646 * before evicting everything in a vain attempt to find space.
2647 */
2648 if (obj->size > dev->gtt_total) {
2649 DRM_ERROR("Attempting to bind an object larger than the aperture\n");
2650 return -E2BIG;
2651 }
2652
Eric Anholt673a3942008-07-30 12:06:12 -07002653 search_free:
2654 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
2655 obj->size, alignment, 0);
2656 if (free_space != NULL) {
2657 obj_priv->gtt_space = drm_mm_get_block(free_space, obj->size,
2658 alignment);
Daniel Vetterdb3307a2010-07-02 15:02:12 +01002659 if (obj_priv->gtt_space != NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002660 obj_priv->gtt_offset = obj_priv->gtt_space->start;
Eric Anholt673a3942008-07-30 12:06:12 -07002661 }
2662 if (obj_priv->gtt_space == NULL) {
2663 /* If the gtt is empty and we're still having trouble
2664 * fitting our object in, we're out of memory.
2665 */
2666#if WATCH_LRU
2667 DRM_INFO("%s: GTT full, evicting something\n", __func__);
2668#endif
Chris Wilson07f73f62009-09-14 16:50:30 +01002669 ret = i915_gem_evict_something(dev, obj->size);
Chris Wilson97311292009-09-21 00:22:34 +01002670 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002671 return ret;
Chris Wilson97311292009-09-21 00:22:34 +01002672
Eric Anholt673a3942008-07-30 12:06:12 -07002673 goto search_free;
2674 }
2675
2676#if WATCH_BUF
Krzysztof Halasacfd43c02009-06-20 00:31:28 +02002677 DRM_INFO("Binding object of size %zd at 0x%08x\n",
Eric Anholt673a3942008-07-30 12:06:12 -07002678 obj->size, obj_priv->gtt_offset);
2679#endif
Chris Wilson4bdadb92010-01-27 13:36:32 +00002680 ret = i915_gem_object_get_pages(obj, gfpmask);
Eric Anholt673a3942008-07-30 12:06:12 -07002681 if (ret) {
2682 drm_mm_put_block(obj_priv->gtt_space);
2683 obj_priv->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002684
2685 if (ret == -ENOMEM) {
2686 /* first try to clear up some space from the GTT */
2687 ret = i915_gem_evict_something(dev, obj->size);
2688 if (ret) {
Chris Wilson07f73f62009-09-14 16:50:30 +01002689 /* now try to shrink everyone else */
Chris Wilson4bdadb92010-01-27 13:36:32 +00002690 if (gfpmask) {
2691 gfpmask = 0;
2692 goto search_free;
Chris Wilson07f73f62009-09-14 16:50:30 +01002693 }
2694
2695 return ret;
2696 }
2697
2698 goto search_free;
2699 }
2700
Eric Anholt673a3942008-07-30 12:06:12 -07002701 return ret;
2702 }
2703
Eric Anholt673a3942008-07-30 12:06:12 -07002704 /* Create an AGP memory structure pointing at our pages, and bind it
2705 * into the GTT.
2706 */
2707 obj_priv->agp_mem = drm_agp_bind_pages(dev,
Eric Anholt856fa192009-03-19 14:10:50 -07002708 obj_priv->pages,
Chris Wilson07f73f62009-09-14 16:50:30 +01002709 obj->size >> PAGE_SHIFT,
Keith Packardba1eb1d2008-10-14 19:55:10 -07002710 obj_priv->gtt_offset,
2711 obj_priv->agp_type);
Eric Anholt673a3942008-07-30 12:06:12 -07002712 if (obj_priv->agp_mem == NULL) {
Eric Anholt856fa192009-03-19 14:10:50 -07002713 i915_gem_object_put_pages(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002714 drm_mm_put_block(obj_priv->gtt_space);
2715 obj_priv->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002716
2717 ret = i915_gem_evict_something(dev, obj->size);
Chris Wilson97311292009-09-21 00:22:34 +01002718 if (ret)
Chris Wilson07f73f62009-09-14 16:50:30 +01002719 return ret;
Chris Wilson07f73f62009-09-14 16:50:30 +01002720
2721 goto search_free;
Eric Anholt673a3942008-07-30 12:06:12 -07002722 }
2723 atomic_inc(&dev->gtt_count);
2724 atomic_add(obj->size, &dev->gtt_memory);
2725
2726 /* Assert that the object is not currently in any GPU domain. As it
2727 * wasn't in the GTT, there shouldn't be any way it could have been in
2728 * a GPU cache
2729 */
Chris Wilson21d509e2009-06-06 09:46:02 +01002730 BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS);
2731 BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS);
Eric Anholt673a3942008-07-30 12:06:12 -07002732
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002733 trace_i915_gem_object_bind(obj, obj_priv->gtt_offset);
2734
Eric Anholt673a3942008-07-30 12:06:12 -07002735 return 0;
2736}
2737
2738void
2739i915_gem_clflush_object(struct drm_gem_object *obj)
2740{
Daniel Vetter23010e42010-03-08 13:35:02 +01002741 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002742
2743 /* If we don't have a page list set up, then we're not pinned
2744 * to GPU, and we can ignore the cache flush because it'll happen
2745 * again at bind time.
2746 */
Eric Anholt856fa192009-03-19 14:10:50 -07002747 if (obj_priv->pages == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002748 return;
2749
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002750 trace_i915_gem_object_clflush(obj);
Eric Anholtcfa16a02009-05-26 18:46:16 -07002751
Eric Anholt856fa192009-03-19 14:10:50 -07002752 drm_clflush_pages(obj_priv->pages, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002753}
2754
Eric Anholte47c68e2008-11-14 13:35:19 -08002755/** Flushes any GPU write domain for the object if it's dirty. */
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002756static int
Eric Anholte47c68e2008-11-14 13:35:19 -08002757i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj)
2758{
2759 struct drm_device *dev = obj->dev;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002760 uint32_t old_write_domain;
Zou Nan hai852835f2010-05-21 09:08:56 +08002761 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08002762
2763 if ((obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002764 return 0;
Eric Anholte47c68e2008-11-14 13:35:19 -08002765
2766 /* Queue the GPU write cache flushing we need. */
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002767 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002768 i915_gem_flush(dev, 0, obj->write_domain);
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002769 if (i915_add_request(dev, NULL, obj->write_domain, obj_priv->ring) == 0)
2770 return -ENOMEM;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002771
2772 trace_i915_gem_object_change_domain(obj,
2773 obj->read_domains,
2774 old_write_domain);
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002775 return 0;
Eric Anholte47c68e2008-11-14 13:35:19 -08002776}
2777
2778/** Flushes the GTT write domain for the object if it's dirty. */
2779static void
2780i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj)
2781{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002782 uint32_t old_write_domain;
2783
Eric Anholte47c68e2008-11-14 13:35:19 -08002784 if (obj->write_domain != I915_GEM_DOMAIN_GTT)
2785 return;
2786
2787 /* No actual flushing is required for the GTT write domain. Writes
2788 * to it immediately go to main memory as far as we know, so there's
2789 * no chipset flush. It also doesn't land in render cache.
2790 */
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002791 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002792 obj->write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002793
2794 trace_i915_gem_object_change_domain(obj,
2795 obj->read_domains,
2796 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002797}
2798
2799/** Flushes the CPU write domain for the object if it's dirty. */
2800static void
2801i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj)
2802{
2803 struct drm_device *dev = obj->dev;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002804 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002805
2806 if (obj->write_domain != I915_GEM_DOMAIN_CPU)
2807 return;
2808
2809 i915_gem_clflush_object(obj);
2810 drm_agp_chipset_flush(dev);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002811 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002812 obj->write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002813
2814 trace_i915_gem_object_change_domain(obj,
2815 obj->read_domains,
2816 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002817}
2818
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002819int
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002820i915_gem_object_flush_write_domain(struct drm_gem_object *obj)
2821{
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002822 int ret = 0;
2823
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002824 switch (obj->write_domain) {
2825 case I915_GEM_DOMAIN_GTT:
2826 i915_gem_object_flush_gtt_write_domain(obj);
2827 break;
2828 case I915_GEM_DOMAIN_CPU:
2829 i915_gem_object_flush_cpu_write_domain(obj);
2830 break;
2831 default:
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002832 ret = i915_gem_object_flush_gpu_write_domain(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002833 break;
2834 }
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002835
2836 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002837}
2838
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002839/**
2840 * Moves a single object to the GTT read, and possibly write domain.
2841 *
2842 * This function returns when the move is complete, including waiting on
2843 * flushes to occur.
2844 */
Jesse Barnes79e53942008-11-07 14:24:08 -08002845int
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002846i915_gem_object_set_to_gtt_domain(struct drm_gem_object *obj, int write)
2847{
Daniel Vetter23010e42010-03-08 13:35:02 +01002848 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002849 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002850 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002851
Eric Anholt02354392008-11-26 13:58:13 -08002852 /* Not valid to be called on unbound objects. */
2853 if (obj_priv->gtt_space == NULL)
2854 return -EINVAL;
2855
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002856 ret = i915_gem_object_flush_gpu_write_domain(obj);
2857 if (ret != 0)
2858 return ret;
2859
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002860 /* Wait on any GPU rendering and flushing to occur. */
Eric Anholte47c68e2008-11-14 13:35:19 -08002861 ret = i915_gem_object_wait_rendering(obj);
2862 if (ret != 0)
2863 return ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002864
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002865 old_write_domain = obj->write_domain;
2866 old_read_domains = obj->read_domains;
2867
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002868 /* If we're writing through the GTT domain, then CPU and GPU caches
2869 * will need to be invalidated at next use.
2870 */
2871 if (write)
Eric Anholte47c68e2008-11-14 13:35:19 -08002872 obj->read_domains &= I915_GEM_DOMAIN_GTT;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002873
Eric Anholte47c68e2008-11-14 13:35:19 -08002874 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002875
2876 /* It should now be out of any other write domains, and we can update
2877 * the domain values for our changes.
2878 */
2879 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2880 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002881 if (write) {
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002882 obj->write_domain = I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002883 obj_priv->dirty = 1;
2884 }
2885
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002886 trace_i915_gem_object_change_domain(obj,
2887 old_read_domains,
2888 old_write_domain);
2889
Eric Anholte47c68e2008-11-14 13:35:19 -08002890 return 0;
2891}
2892
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002893/*
2894 * Prepare buffer for display plane. Use uninterruptible for possible flush
2895 * wait, as in modesetting process we're not supposed to be interrupted.
2896 */
2897int
2898i915_gem_object_set_to_display_plane(struct drm_gem_object *obj)
2899{
2900 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01002901 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002902 uint32_t old_write_domain, old_read_domains;
2903 int ret;
2904
2905 /* Not valid to be called on unbound objects. */
2906 if (obj_priv->gtt_space == NULL)
2907 return -EINVAL;
2908
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002909 ret = i915_gem_object_flush_gpu_write_domain(obj);
2910 if (ret)
2911 return ret;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002912
2913 /* Wait on any GPU rendering and flushing to occur. */
2914 if (obj_priv->active) {
2915#if WATCH_BUF
2916 DRM_INFO("%s: object %p wait for seqno %08x\n",
2917 __func__, obj, obj_priv->last_rendering_seqno);
2918#endif
Zou Nan hai852835f2010-05-21 09:08:56 +08002919 ret = i915_do_wait_request(dev,
2920 obj_priv->last_rendering_seqno,
2921 0,
2922 obj_priv->ring);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002923 if (ret != 0)
2924 return ret;
2925 }
2926
Chris Wilsonb118c1e2010-05-27 13:18:14 +01002927 i915_gem_object_flush_cpu_write_domain(obj);
2928
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002929 old_write_domain = obj->write_domain;
2930 old_read_domains = obj->read_domains;
2931
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002932 /* It should now be out of any other write domains, and we can update
2933 * the domain values for our changes.
2934 */
2935 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
Chris Wilsonb118c1e2010-05-27 13:18:14 +01002936 obj->read_domains = I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002937 obj->write_domain = I915_GEM_DOMAIN_GTT;
2938 obj_priv->dirty = 1;
2939
2940 trace_i915_gem_object_change_domain(obj,
2941 old_read_domains,
2942 old_write_domain);
2943
2944 return 0;
2945}
2946
Eric Anholte47c68e2008-11-14 13:35:19 -08002947/**
2948 * Moves a single object to the CPU read, and possibly write domain.
2949 *
2950 * This function returns when the move is complete, including waiting on
2951 * flushes to occur.
2952 */
2953static int
2954i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj, int write)
2955{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002956 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002957 int ret;
2958
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002959 ret = i915_gem_object_flush_gpu_write_domain(obj);
2960 if (ret)
2961 return ret;
2962
Eric Anholte47c68e2008-11-14 13:35:19 -08002963 /* Wait on any GPU rendering and flushing to occur. */
2964 ret = i915_gem_object_wait_rendering(obj);
2965 if (ret != 0)
2966 return ret;
2967
2968 i915_gem_object_flush_gtt_write_domain(obj);
2969
2970 /* If we have a partially-valid cache of the object in the CPU,
2971 * finish invalidating it and free the per-page flags.
2972 */
2973 i915_gem_object_set_to_full_cpu_read_domain(obj);
2974
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002975 old_write_domain = obj->write_domain;
2976 old_read_domains = obj->read_domains;
2977
Eric Anholte47c68e2008-11-14 13:35:19 -08002978 /* Flush the CPU cache if it's still invalid. */
2979 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
2980 i915_gem_clflush_object(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08002981
2982 obj->read_domains |= I915_GEM_DOMAIN_CPU;
2983 }
2984
2985 /* It should now be out of any other write domains, and we can update
2986 * the domain values for our changes.
2987 */
2988 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
2989
2990 /* If we're writing through the CPU, then the GPU read domains will
2991 * need to be invalidated at next use.
2992 */
2993 if (write) {
2994 obj->read_domains &= I915_GEM_DOMAIN_CPU;
2995 obj->write_domain = I915_GEM_DOMAIN_CPU;
2996 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002997
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002998 trace_i915_gem_object_change_domain(obj,
2999 old_read_domains,
3000 old_write_domain);
3001
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003002 return 0;
3003}
3004
Eric Anholt673a3942008-07-30 12:06:12 -07003005/*
3006 * Set the next domain for the specified object. This
3007 * may not actually perform the necessary flushing/invaliding though,
3008 * as that may want to be batched with other set_domain operations
3009 *
3010 * This is (we hope) the only really tricky part of gem. The goal
3011 * is fairly simple -- track which caches hold bits of the object
3012 * and make sure they remain coherent. A few concrete examples may
3013 * help to explain how it works. For shorthand, we use the notation
3014 * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
3015 * a pair of read and write domain masks.
3016 *
3017 * Case 1: the batch buffer
3018 *
3019 * 1. Allocated
3020 * 2. Written by CPU
3021 * 3. Mapped to GTT
3022 * 4. Read by GPU
3023 * 5. Unmapped from GTT
3024 * 6. Freed
3025 *
3026 * Let's take these a step at a time
3027 *
3028 * 1. Allocated
3029 * Pages allocated from the kernel may still have
3030 * cache contents, so we set them to (CPU, CPU) always.
3031 * 2. Written by CPU (using pwrite)
3032 * The pwrite function calls set_domain (CPU, CPU) and
3033 * this function does nothing (as nothing changes)
3034 * 3. Mapped by GTT
3035 * This function asserts that the object is not
3036 * currently in any GPU-based read or write domains
3037 * 4. Read by GPU
3038 * i915_gem_execbuffer calls set_domain (COMMAND, 0).
3039 * As write_domain is zero, this function adds in the
3040 * current read domains (CPU+COMMAND, 0).
3041 * flush_domains is set to CPU.
3042 * invalidate_domains is set to COMMAND
3043 * clflush is run to get data out of the CPU caches
3044 * then i915_dev_set_domain calls i915_gem_flush to
3045 * emit an MI_FLUSH and drm_agp_chipset_flush
3046 * 5. Unmapped from GTT
3047 * i915_gem_object_unbind calls set_domain (CPU, CPU)
3048 * flush_domains and invalidate_domains end up both zero
3049 * so no flushing/invalidating happens
3050 * 6. Freed
3051 * yay, done
3052 *
3053 * Case 2: The shared render buffer
3054 *
3055 * 1. Allocated
3056 * 2. Mapped to GTT
3057 * 3. Read/written by GPU
3058 * 4. set_domain to (CPU,CPU)
3059 * 5. Read/written by CPU
3060 * 6. Read/written by GPU
3061 *
3062 * 1. Allocated
3063 * Same as last example, (CPU, CPU)
3064 * 2. Mapped to GTT
3065 * Nothing changes (assertions find that it is not in the GPU)
3066 * 3. Read/written by GPU
3067 * execbuffer calls set_domain (RENDER, RENDER)
3068 * flush_domains gets CPU
3069 * invalidate_domains gets GPU
3070 * clflush (obj)
3071 * MI_FLUSH and drm_agp_chipset_flush
3072 * 4. set_domain (CPU, CPU)
3073 * flush_domains gets GPU
3074 * invalidate_domains gets CPU
3075 * wait_rendering (obj) to make sure all drawing is complete.
3076 * This will include an MI_FLUSH to get the data from GPU
3077 * to memory
3078 * clflush (obj) to invalidate the CPU cache
3079 * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
3080 * 5. Read/written by CPU
3081 * cache lines are loaded and dirtied
3082 * 6. Read written by GPU
3083 * Same as last GPU access
3084 *
3085 * Case 3: The constant buffer
3086 *
3087 * 1. Allocated
3088 * 2. Written by CPU
3089 * 3. Read by GPU
3090 * 4. Updated (written) by CPU again
3091 * 5. Read by GPU
3092 *
3093 * 1. Allocated
3094 * (CPU, CPU)
3095 * 2. Written by CPU
3096 * (CPU, CPU)
3097 * 3. Read by GPU
3098 * (CPU+RENDER, 0)
3099 * flush_domains = CPU
3100 * invalidate_domains = RENDER
3101 * clflush (obj)
3102 * MI_FLUSH
3103 * drm_agp_chipset_flush
3104 * 4. Updated (written) by CPU again
3105 * (CPU, CPU)
3106 * flush_domains = 0 (no previous write domain)
3107 * invalidate_domains = 0 (no new read domains)
3108 * 5. Read by GPU
3109 * (CPU+RENDER, 0)
3110 * flush_domains = CPU
3111 * invalidate_domains = RENDER
3112 * clflush (obj)
3113 * MI_FLUSH
3114 * drm_agp_chipset_flush
3115 */
Keith Packardc0d90822008-11-20 23:11:08 -08003116static void
Eric Anholt8b0e3782009-02-19 14:40:50 -08003117i915_gem_object_set_to_gpu_domain(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07003118{
3119 struct drm_device *dev = obj->dev;
Chris Wilson88f356b2010-08-04 13:55:32 +01003120 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01003121 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003122 uint32_t invalidate_domains = 0;
3123 uint32_t flush_domains = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003124 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003125
Eric Anholt8b0e3782009-02-19 14:40:50 -08003126 BUG_ON(obj->pending_read_domains & I915_GEM_DOMAIN_CPU);
3127 BUG_ON(obj->pending_write_domain == I915_GEM_DOMAIN_CPU);
Eric Anholt673a3942008-07-30 12:06:12 -07003128
Jesse Barnes652c3932009-08-17 13:31:43 -07003129 intel_mark_busy(dev, obj);
3130
Eric Anholt673a3942008-07-30 12:06:12 -07003131#if WATCH_BUF
3132 DRM_INFO("%s: object %p read %08x -> %08x write %08x -> %08x\n",
3133 __func__, obj,
Eric Anholt8b0e3782009-02-19 14:40:50 -08003134 obj->read_domains, obj->pending_read_domains,
3135 obj->write_domain, obj->pending_write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07003136#endif
3137 /*
3138 * If the object isn't moving to a new write domain,
3139 * let the object stay in multiple read domains
3140 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003141 if (obj->pending_write_domain == 0)
3142 obj->pending_read_domains |= obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07003143 else
3144 obj_priv->dirty = 1;
3145
3146 /*
3147 * Flush the current write domain if
3148 * the new read domains don't match. Invalidate
3149 * any read domains which differ from the old
3150 * write domain
3151 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003152 if (obj->write_domain &&
3153 obj->write_domain != obj->pending_read_domains) {
Eric Anholt673a3942008-07-30 12:06:12 -07003154 flush_domains |= obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08003155 invalidate_domains |=
3156 obj->pending_read_domains & ~obj->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07003157 }
3158 /*
3159 * Invalidate any read caches which may have
3160 * stale data. That is, any new read domains.
3161 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003162 invalidate_domains |= obj->pending_read_domains & ~obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07003163 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU) {
3164#if WATCH_BUF
3165 DRM_INFO("%s: CPU domain flush %08x invalidate %08x\n",
3166 __func__, flush_domains, invalidate_domains);
3167#endif
Eric Anholt673a3942008-07-30 12:06:12 -07003168 i915_gem_clflush_object(obj);
3169 }
3170
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003171 old_read_domains = obj->read_domains;
3172
Eric Anholtefbeed92009-02-19 14:54:51 -08003173 /* The actual obj->write_domain will be updated with
3174 * pending_write_domain after we emit the accumulated flush for all
3175 * of our domain changes in execbuffers (which clears objects'
3176 * write_domains). So if we have a current write domain that we
3177 * aren't changing, set pending_write_domain to that.
3178 */
3179 if (flush_domains == 0 && obj->pending_write_domain == 0)
3180 obj->pending_write_domain = obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08003181 obj->read_domains = obj->pending_read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07003182
Chris Wilson88f356b2010-08-04 13:55:32 +01003183 if (flush_domains & I915_GEM_GPU_DOMAINS) {
3184 if (obj_priv->ring == &dev_priv->render_ring)
3185 dev_priv->flush_rings |= FLUSH_RENDER_RING;
3186 else if (obj_priv->ring == &dev_priv->bsd_ring)
3187 dev_priv->flush_rings |= FLUSH_BSD_RING;
3188 }
3189
Eric Anholt673a3942008-07-30 12:06:12 -07003190 dev->invalidate_domains |= invalidate_domains;
3191 dev->flush_domains |= flush_domains;
3192#if WATCH_BUF
3193 DRM_INFO("%s: read %08x write %08x invalidate %08x flush %08x\n",
3194 __func__,
3195 obj->read_domains, obj->write_domain,
3196 dev->invalidate_domains, dev->flush_domains);
3197#endif
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003198
3199 trace_i915_gem_object_change_domain(obj,
3200 old_read_domains,
3201 obj->write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07003202}
3203
3204/**
Eric Anholte47c68e2008-11-14 13:35:19 -08003205 * Moves the object from a partially CPU read to a full one.
Eric Anholt673a3942008-07-30 12:06:12 -07003206 *
Eric Anholte47c68e2008-11-14 13:35:19 -08003207 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
3208 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
3209 */
3210static void
3211i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj)
3212{
Daniel Vetter23010e42010-03-08 13:35:02 +01003213 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003214
3215 if (!obj_priv->page_cpu_valid)
3216 return;
3217
3218 /* If we're partially in the CPU read domain, finish moving it in.
3219 */
3220 if (obj->read_domains & I915_GEM_DOMAIN_CPU) {
3221 int i;
3222
3223 for (i = 0; i <= (obj->size - 1) / PAGE_SIZE; i++) {
3224 if (obj_priv->page_cpu_valid[i])
3225 continue;
Eric Anholt856fa192009-03-19 14:10:50 -07003226 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholte47c68e2008-11-14 13:35:19 -08003227 }
Eric Anholte47c68e2008-11-14 13:35:19 -08003228 }
3229
3230 /* Free the page_cpu_valid mappings which are now stale, whether
3231 * or not we've got I915_GEM_DOMAIN_CPU.
3232 */
Eric Anholt9a298b22009-03-24 12:23:04 -07003233 kfree(obj_priv->page_cpu_valid);
Eric Anholte47c68e2008-11-14 13:35:19 -08003234 obj_priv->page_cpu_valid = NULL;
3235}
3236
3237/**
3238 * Set the CPU read domain on a range of the object.
3239 *
3240 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
3241 * not entirely valid. The page_cpu_valid member of the object flags which
3242 * pages have been flushed, and will be respected by
3243 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
3244 * of the whole object.
3245 *
3246 * This function returns when the move is complete, including waiting on
3247 * flushes to occur.
Eric Anholt673a3942008-07-30 12:06:12 -07003248 */
3249static int
Eric Anholte47c68e2008-11-14 13:35:19 -08003250i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
3251 uint64_t offset, uint64_t size)
Eric Anholt673a3942008-07-30 12:06:12 -07003252{
Daniel Vetter23010e42010-03-08 13:35:02 +01003253 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003254 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003255 int i, ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003256
Eric Anholte47c68e2008-11-14 13:35:19 -08003257 if (offset == 0 && size == obj->size)
3258 return i915_gem_object_set_to_cpu_domain(obj, 0);
3259
Chris Wilson2dafb1e2010-06-07 14:03:05 +01003260 ret = i915_gem_object_flush_gpu_write_domain(obj);
3261 if (ret)
3262 return ret;
3263
Eric Anholte47c68e2008-11-14 13:35:19 -08003264 /* Wait on any GPU rendering and flushing to occur. */
3265 ret = i915_gem_object_wait_rendering(obj);
3266 if (ret != 0)
3267 return ret;
3268 i915_gem_object_flush_gtt_write_domain(obj);
3269
3270 /* If we're already fully in the CPU read domain, we're done. */
3271 if (obj_priv->page_cpu_valid == NULL &&
3272 (obj->read_domains & I915_GEM_DOMAIN_CPU) != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07003273 return 0;
3274
Eric Anholte47c68e2008-11-14 13:35:19 -08003275 /* Otherwise, create/clear the per-page CPU read domain flag if we're
3276 * newly adding I915_GEM_DOMAIN_CPU
3277 */
Eric Anholt673a3942008-07-30 12:06:12 -07003278 if (obj_priv->page_cpu_valid == NULL) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003279 obj_priv->page_cpu_valid = kzalloc(obj->size / PAGE_SIZE,
3280 GFP_KERNEL);
Eric Anholte47c68e2008-11-14 13:35:19 -08003281 if (obj_priv->page_cpu_valid == NULL)
3282 return -ENOMEM;
3283 } else if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0)
3284 memset(obj_priv->page_cpu_valid, 0, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07003285
3286 /* Flush the cache on any pages that are still invalid from the CPU's
3287 * perspective.
3288 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003289 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
3290 i++) {
Eric Anholt673a3942008-07-30 12:06:12 -07003291 if (obj_priv->page_cpu_valid[i])
3292 continue;
3293
Eric Anholt856fa192009-03-19 14:10:50 -07003294 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07003295
3296 obj_priv->page_cpu_valid[i] = 1;
3297 }
3298
Eric Anholte47c68e2008-11-14 13:35:19 -08003299 /* It should now be out of any other write domains, and we can update
3300 * the domain values for our changes.
3301 */
3302 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
3303
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003304 old_read_domains = obj->read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003305 obj->read_domains |= I915_GEM_DOMAIN_CPU;
3306
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003307 trace_i915_gem_object_change_domain(obj,
3308 old_read_domains,
3309 obj->write_domain);
3310
Eric Anholt673a3942008-07-30 12:06:12 -07003311 return 0;
3312}
3313
3314/**
Eric Anholt673a3942008-07-30 12:06:12 -07003315 * Pin an object to the GTT and evaluate the relocations landing in it.
3316 */
3317static int
3318i915_gem_object_pin_and_relocate(struct drm_gem_object *obj,
3319 struct drm_file *file_priv,
Jesse Barnes76446ca2009-12-17 22:05:42 -05003320 struct drm_i915_gem_exec_object2 *entry,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003321 struct drm_i915_gem_relocation_entry *relocs)
Eric Anholt673a3942008-07-30 12:06:12 -07003322{
3323 struct drm_device *dev = obj->dev;
Keith Packard0839ccb2008-10-30 19:38:48 -07003324 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01003325 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003326 int i, ret;
Keith Packard0839ccb2008-10-30 19:38:48 -07003327 void __iomem *reloc_page;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003328 bool need_fence;
3329
3330 need_fence = entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
3331 obj_priv->tiling_mode != I915_TILING_NONE;
3332
3333 /* Check fence reg constraints and rebind if necessary */
Chris Wilson808b24d2010-05-27 13:18:15 +01003334 if (need_fence &&
3335 !i915_gem_object_fence_offset_ok(obj,
3336 obj_priv->tiling_mode)) {
3337 ret = i915_gem_object_unbind(obj);
3338 if (ret)
3339 return ret;
3340 }
Eric Anholt673a3942008-07-30 12:06:12 -07003341
3342 /* Choose the GTT offset for our buffer and put it there. */
3343 ret = i915_gem_object_pin(obj, (uint32_t) entry->alignment);
3344 if (ret)
3345 return ret;
3346
Jesse Barnes76446ca2009-12-17 22:05:42 -05003347 /*
3348 * Pre-965 chips need a fence register set up in order to
3349 * properly handle blits to/from tiled surfaces.
3350 */
3351 if (need_fence) {
3352 ret = i915_gem_object_get_fence_reg(obj);
3353 if (ret != 0) {
Jesse Barnes76446ca2009-12-17 22:05:42 -05003354 i915_gem_object_unpin(obj);
3355 return ret;
3356 }
3357 }
3358
Eric Anholt673a3942008-07-30 12:06:12 -07003359 entry->offset = obj_priv->gtt_offset;
3360
Eric Anholt673a3942008-07-30 12:06:12 -07003361 /* Apply the relocations, using the GTT aperture to avoid cache
3362 * flushing requirements.
3363 */
3364 for (i = 0; i < entry->relocation_count; i++) {
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003365 struct drm_i915_gem_relocation_entry *reloc= &relocs[i];
Eric Anholt673a3942008-07-30 12:06:12 -07003366 struct drm_gem_object *target_obj;
3367 struct drm_i915_gem_object *target_obj_priv;
Eric Anholt3043c602008-10-02 12:24:47 -07003368 uint32_t reloc_val, reloc_offset;
3369 uint32_t __iomem *reloc_entry;
Eric Anholt673a3942008-07-30 12:06:12 -07003370
Eric Anholt673a3942008-07-30 12:06:12 -07003371 target_obj = drm_gem_object_lookup(obj->dev, file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003372 reloc->target_handle);
Eric Anholt673a3942008-07-30 12:06:12 -07003373 if (target_obj == NULL) {
3374 i915_gem_object_unpin(obj);
3375 return -EBADF;
3376 }
Daniel Vetter23010e42010-03-08 13:35:02 +01003377 target_obj_priv = to_intel_bo(target_obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003378
Chris Wilson8542a0b2009-09-09 21:15:15 +01003379#if WATCH_RELOC
3380 DRM_INFO("%s: obj %p offset %08x target %d "
3381 "read %08x write %08x gtt %08x "
3382 "presumed %08x delta %08x\n",
3383 __func__,
3384 obj,
3385 (int) reloc->offset,
3386 (int) reloc->target_handle,
3387 (int) reloc->read_domains,
3388 (int) reloc->write_domain,
3389 (int) target_obj_priv->gtt_offset,
3390 (int) reloc->presumed_offset,
3391 reloc->delta);
3392#endif
3393
Eric Anholt673a3942008-07-30 12:06:12 -07003394 /* The target buffer should have appeared before us in the
3395 * exec_object list, so it should have a GTT space bound by now.
3396 */
3397 if (target_obj_priv->gtt_space == NULL) {
3398 DRM_ERROR("No GTT space found for object %d\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003399 reloc->target_handle);
Eric Anholt673a3942008-07-30 12:06:12 -07003400 drm_gem_object_unreference(target_obj);
3401 i915_gem_object_unpin(obj);
3402 return -EINVAL;
3403 }
3404
Chris Wilson8542a0b2009-09-09 21:15:15 +01003405 /* Validate that the target is in a valid r/w GPU domain */
Daniel Vetter16edd552010-02-19 11:52:02 +01003406 if (reloc->write_domain & (reloc->write_domain - 1)) {
3407 DRM_ERROR("reloc with multiple write domains: "
3408 "obj %p target %d offset %d "
3409 "read %08x write %08x",
3410 obj, reloc->target_handle,
3411 (int) reloc->offset,
3412 reloc->read_domains,
3413 reloc->write_domain);
3414 return -EINVAL;
3415 }
Chris Wilson8542a0b2009-09-09 21:15:15 +01003416 if (reloc->write_domain & I915_GEM_DOMAIN_CPU ||
3417 reloc->read_domains & I915_GEM_DOMAIN_CPU) {
3418 DRM_ERROR("reloc with read/write CPU domains: "
3419 "obj %p target %d offset %d "
3420 "read %08x write %08x",
3421 obj, reloc->target_handle,
3422 (int) reloc->offset,
3423 reloc->read_domains,
3424 reloc->write_domain);
3425 drm_gem_object_unreference(target_obj);
3426 i915_gem_object_unpin(obj);
3427 return -EINVAL;
3428 }
3429 if (reloc->write_domain && target_obj->pending_write_domain &&
3430 reloc->write_domain != target_obj->pending_write_domain) {
3431 DRM_ERROR("Write domain conflict: "
3432 "obj %p target %d offset %d "
3433 "new %08x old %08x\n",
3434 obj, reloc->target_handle,
3435 (int) reloc->offset,
3436 reloc->write_domain,
3437 target_obj->pending_write_domain);
3438 drm_gem_object_unreference(target_obj);
3439 i915_gem_object_unpin(obj);
3440 return -EINVAL;
3441 }
3442
3443 target_obj->pending_read_domains |= reloc->read_domains;
3444 target_obj->pending_write_domain |= reloc->write_domain;
3445
3446 /* If the relocation already has the right value in it, no
3447 * more work needs to be done.
3448 */
3449 if (target_obj_priv->gtt_offset == reloc->presumed_offset) {
3450 drm_gem_object_unreference(target_obj);
3451 continue;
3452 }
3453
3454 /* Check that the relocation address is valid... */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003455 if (reloc->offset > obj->size - 4) {
Eric Anholt673a3942008-07-30 12:06:12 -07003456 DRM_ERROR("Relocation beyond object bounds: "
3457 "obj %p target %d offset %d size %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003458 obj, reloc->target_handle,
3459 (int) reloc->offset, (int) obj->size);
Eric Anholt673a3942008-07-30 12:06:12 -07003460 drm_gem_object_unreference(target_obj);
3461 i915_gem_object_unpin(obj);
3462 return -EINVAL;
3463 }
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003464 if (reloc->offset & 3) {
Eric Anholt673a3942008-07-30 12:06:12 -07003465 DRM_ERROR("Relocation not 4-byte aligned: "
3466 "obj %p target %d offset %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003467 obj, reloc->target_handle,
3468 (int) reloc->offset);
Eric Anholt673a3942008-07-30 12:06:12 -07003469 drm_gem_object_unreference(target_obj);
3470 i915_gem_object_unpin(obj);
3471 return -EINVAL;
3472 }
3473
Chris Wilson8542a0b2009-09-09 21:15:15 +01003474 /* and points to somewhere within the target object. */
Chris Wilsoncd0b9fb2009-09-15 23:23:18 +01003475 if (reloc->delta >= target_obj->size) {
3476 DRM_ERROR("Relocation beyond target object bounds: "
3477 "obj %p target %d delta %d size %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003478 obj, reloc->target_handle,
Chris Wilsoncd0b9fb2009-09-15 23:23:18 +01003479 (int) reloc->delta, (int) target_obj->size);
Chris Wilson491152b2009-02-11 14:26:32 +00003480 drm_gem_object_unreference(target_obj);
3481 i915_gem_object_unpin(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003482 return -EINVAL;
3483 }
3484
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003485 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
3486 if (ret != 0) {
3487 drm_gem_object_unreference(target_obj);
3488 i915_gem_object_unpin(obj);
3489 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -07003490 }
3491
3492 /* Map the page containing the relocation we're going to
3493 * perform.
3494 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003495 reloc_offset = obj_priv->gtt_offset + reloc->offset;
Keith Packard0839ccb2008-10-30 19:38:48 -07003496 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
3497 (reloc_offset &
Chris Wilsonfca3ec02010-08-04 14:34:24 +01003498 ~(PAGE_SIZE - 1)),
3499 KM_USER0);
Eric Anholt3043c602008-10-02 12:24:47 -07003500 reloc_entry = (uint32_t __iomem *)(reloc_page +
Keith Packard0839ccb2008-10-30 19:38:48 -07003501 (reloc_offset & (PAGE_SIZE - 1)));
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003502 reloc_val = target_obj_priv->gtt_offset + reloc->delta;
Eric Anholt673a3942008-07-30 12:06:12 -07003503
3504#if WATCH_BUF
3505 DRM_INFO("Applied relocation: %p@0x%08x %08x -> %08x\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003506 obj, (unsigned int) reloc->offset,
Eric Anholt673a3942008-07-30 12:06:12 -07003507 readl(reloc_entry), reloc_val);
3508#endif
3509 writel(reloc_val, reloc_entry);
Chris Wilsonfca3ec02010-08-04 14:34:24 +01003510 io_mapping_unmap_atomic(reloc_page, KM_USER0);
Eric Anholt673a3942008-07-30 12:06:12 -07003511
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003512 /* The updated presumed offset for this entry will be
3513 * copied back out to the user.
Eric Anholt673a3942008-07-30 12:06:12 -07003514 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003515 reloc->presumed_offset = target_obj_priv->gtt_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003516
3517 drm_gem_object_unreference(target_obj);
3518 }
3519
Eric Anholt673a3942008-07-30 12:06:12 -07003520#if WATCH_BUF
3521 if (0)
3522 i915_gem_dump_object(obj, 128, __func__, ~0);
3523#endif
3524 return 0;
3525}
3526
Eric Anholt673a3942008-07-30 12:06:12 -07003527/* Throttle our rendering by waiting until the ring has completed our requests
3528 * emitted over 20 msec ago.
3529 *
Eric Anholtb9624422009-06-03 07:27:35 +00003530 * Note that if we were to use the current jiffies each time around the loop,
3531 * we wouldn't escape the function with any frames outstanding if the time to
3532 * render a frame was over 20ms.
3533 *
Eric Anholt673a3942008-07-30 12:06:12 -07003534 * This should get us reasonable parallelism between CPU and GPU but also
3535 * relatively low latency when blocking on a particular request to finish.
3536 */
3537static int
3538i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file_priv)
3539{
3540 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
3541 int ret = 0;
Eric Anholtb9624422009-06-03 07:27:35 +00003542 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
Eric Anholt673a3942008-07-30 12:06:12 -07003543
3544 mutex_lock(&dev->struct_mutex);
Eric Anholtb9624422009-06-03 07:27:35 +00003545 while (!list_empty(&i915_file_priv->mm.request_list)) {
3546 struct drm_i915_gem_request *request;
3547
3548 request = list_first_entry(&i915_file_priv->mm.request_list,
3549 struct drm_i915_gem_request,
3550 client_list);
3551
3552 if (time_after_eq(request->emitted_jiffies, recent_enough))
3553 break;
3554
Zou Nan hai852835f2010-05-21 09:08:56 +08003555 ret = i915_wait_request(dev, request->seqno, request->ring);
Eric Anholtb9624422009-06-03 07:27:35 +00003556 if (ret != 0)
3557 break;
3558 }
Eric Anholt673a3942008-07-30 12:06:12 -07003559 mutex_unlock(&dev->struct_mutex);
Eric Anholtb9624422009-06-03 07:27:35 +00003560
Eric Anholt673a3942008-07-30 12:06:12 -07003561 return ret;
3562}
3563
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003564static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003565i915_gem_get_relocs_from_user(struct drm_i915_gem_exec_object2 *exec_list,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003566 uint32_t buffer_count,
3567 struct drm_i915_gem_relocation_entry **relocs)
3568{
3569 uint32_t reloc_count = 0, reloc_index = 0, i;
3570 int ret;
3571
3572 *relocs = NULL;
3573 for (i = 0; i < buffer_count; i++) {
3574 if (reloc_count + exec_list[i].relocation_count < reloc_count)
3575 return -EINVAL;
3576 reloc_count += exec_list[i].relocation_count;
3577 }
3578
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003579 *relocs = drm_calloc_large(reloc_count, sizeof(**relocs));
Jesse Barnes76446ca2009-12-17 22:05:42 -05003580 if (*relocs == NULL) {
3581 DRM_ERROR("failed to alloc relocs, count %d\n", reloc_count);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003582 return -ENOMEM;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003583 }
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003584
3585 for (i = 0; i < buffer_count; i++) {
3586 struct drm_i915_gem_relocation_entry __user *user_relocs;
3587
3588 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3589
3590 ret = copy_from_user(&(*relocs)[reloc_index],
3591 user_relocs,
3592 exec_list[i].relocation_count *
3593 sizeof(**relocs));
3594 if (ret != 0) {
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003595 drm_free_large(*relocs);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003596 *relocs = NULL;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003597 return -EFAULT;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003598 }
3599
3600 reloc_index += exec_list[i].relocation_count;
3601 }
3602
Florian Mickler2bc43b52009-04-06 22:55:41 +02003603 return 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003604}
3605
3606static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003607i915_gem_put_relocs_to_user(struct drm_i915_gem_exec_object2 *exec_list,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003608 uint32_t buffer_count,
3609 struct drm_i915_gem_relocation_entry *relocs)
3610{
3611 uint32_t reloc_count = 0, i;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003612 int ret = 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003613
Chris Wilson93533c22010-01-31 10:40:48 +00003614 if (relocs == NULL)
3615 return 0;
3616
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003617 for (i = 0; i < buffer_count; i++) {
3618 struct drm_i915_gem_relocation_entry __user *user_relocs;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003619 int unwritten;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003620
3621 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3622
Florian Mickler2bc43b52009-04-06 22:55:41 +02003623 unwritten = copy_to_user(user_relocs,
3624 &relocs[reloc_count],
3625 exec_list[i].relocation_count *
3626 sizeof(*relocs));
3627
3628 if (unwritten) {
3629 ret = -EFAULT;
3630 goto err;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003631 }
3632
3633 reloc_count += exec_list[i].relocation_count;
3634 }
3635
Florian Mickler2bc43b52009-04-06 22:55:41 +02003636err:
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003637 drm_free_large(relocs);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003638
3639 return ret;
3640}
3641
Chris Wilson83d60792009-06-06 09:45:57 +01003642static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003643i915_gem_check_execbuffer (struct drm_i915_gem_execbuffer2 *exec,
Chris Wilson83d60792009-06-06 09:45:57 +01003644 uint64_t exec_offset)
3645{
3646 uint32_t exec_start, exec_len;
3647
3648 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
3649 exec_len = (uint32_t) exec->batch_len;
3650
3651 if ((exec_start | exec_len) & 0x7)
3652 return -EINVAL;
3653
3654 if (!exec_start)
3655 return -EINVAL;
3656
3657 return 0;
3658}
3659
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003660static int
3661i915_gem_wait_for_pending_flip(struct drm_device *dev,
3662 struct drm_gem_object **object_list,
3663 int count)
3664{
3665 drm_i915_private_t *dev_priv = dev->dev_private;
3666 struct drm_i915_gem_object *obj_priv;
3667 DEFINE_WAIT(wait);
3668 int i, ret = 0;
3669
3670 for (;;) {
3671 prepare_to_wait(&dev_priv->pending_flip_queue,
3672 &wait, TASK_INTERRUPTIBLE);
3673 for (i = 0; i < count; i++) {
Daniel Vetter23010e42010-03-08 13:35:02 +01003674 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003675 if (atomic_read(&obj_priv->pending_flip) > 0)
3676 break;
3677 }
3678 if (i == count)
3679 break;
3680
3681 if (!signal_pending(current)) {
3682 mutex_unlock(&dev->struct_mutex);
3683 schedule();
3684 mutex_lock(&dev->struct_mutex);
3685 continue;
3686 }
3687 ret = -ERESTARTSYS;
3688 break;
3689 }
3690 finish_wait(&dev_priv->pending_flip_queue, &wait);
3691
3692 return ret;
3693}
3694
Chris Wilson43b27f42010-07-02 08:57:15 +01003695
Eric Anholt673a3942008-07-30 12:06:12 -07003696int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003697i915_gem_do_execbuffer(struct drm_device *dev, void *data,
3698 struct drm_file *file_priv,
3699 struct drm_i915_gem_execbuffer2 *args,
3700 struct drm_i915_gem_exec_object2 *exec_list)
Eric Anholt673a3942008-07-30 12:06:12 -07003701{
3702 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003703 struct drm_gem_object **object_list = NULL;
3704 struct drm_gem_object *batch_obj;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003705 struct drm_i915_gem_object *obj_priv;
Eric Anholt201361a2009-03-11 12:30:04 -07003706 struct drm_clip_rect *cliprects = NULL;
Chris Wilson93533c22010-01-31 10:40:48 +00003707 struct drm_i915_gem_relocation_entry *relocs = NULL;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003708 int ret = 0, ret2, i, pinned = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003709 uint64_t exec_offset;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003710 uint32_t seqno, flush_domains, reloc_index;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003711 int pin_tries, flips;
Eric Anholt673a3942008-07-30 12:06:12 -07003712
Zou Nan hai852835f2010-05-21 09:08:56 +08003713 struct intel_ring_buffer *ring = NULL;
3714
Eric Anholt673a3942008-07-30 12:06:12 -07003715#if WATCH_EXEC
3716 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3717 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3718#endif
Zou Nan haid1b851f2010-05-21 09:08:57 +08003719 if (args->flags & I915_EXEC_BSD) {
3720 if (!HAS_BSD(dev)) {
3721 DRM_ERROR("execbuf with wrong flag\n");
3722 return -EINVAL;
3723 }
3724 ring = &dev_priv->bsd_ring;
3725 } else {
3726 ring = &dev_priv->render_ring;
3727 }
3728
Eric Anholt4f481ed2008-09-10 14:22:49 -07003729 if (args->buffer_count < 1) {
3730 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3731 return -EINVAL;
3732 }
Eric Anholtc8e0f932009-11-22 03:49:37 +01003733 object_list = drm_malloc_ab(sizeof(*object_list), args->buffer_count);
Jesse Barnes76446ca2009-12-17 22:05:42 -05003734 if (object_list == NULL) {
3735 DRM_ERROR("Failed to allocate object list for %d buffers\n",
Eric Anholt673a3942008-07-30 12:06:12 -07003736 args->buffer_count);
3737 ret = -ENOMEM;
3738 goto pre_mutex_err;
3739 }
Eric Anholt673a3942008-07-30 12:06:12 -07003740
Eric Anholt201361a2009-03-11 12:30:04 -07003741 if (args->num_cliprects != 0) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003742 cliprects = kcalloc(args->num_cliprects, sizeof(*cliprects),
3743 GFP_KERNEL);
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003744 if (cliprects == NULL) {
3745 ret = -ENOMEM;
Eric Anholt201361a2009-03-11 12:30:04 -07003746 goto pre_mutex_err;
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003747 }
Eric Anholt201361a2009-03-11 12:30:04 -07003748
3749 ret = copy_from_user(cliprects,
3750 (struct drm_clip_rect __user *)
3751 (uintptr_t) args->cliprects_ptr,
3752 sizeof(*cliprects) * args->num_cliprects);
3753 if (ret != 0) {
3754 DRM_ERROR("copy %d cliprects failed: %d\n",
3755 args->num_cliprects, ret);
3756 goto pre_mutex_err;
3757 }
3758 }
3759
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003760 ret = i915_gem_get_relocs_from_user(exec_list, args->buffer_count,
3761 &relocs);
3762 if (ret != 0)
3763 goto pre_mutex_err;
3764
Eric Anholt673a3942008-07-30 12:06:12 -07003765 mutex_lock(&dev->struct_mutex);
3766
3767 i915_verify_inactive(dev, __FILE__, __LINE__);
3768
Ben Gamariba1234d2009-09-14 17:48:47 -04003769 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07003770 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003771 ret = -EIO;
3772 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003773 }
3774
3775 if (dev_priv->mm.suspended) {
Eric Anholt673a3942008-07-30 12:06:12 -07003776 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003777 ret = -EBUSY;
3778 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003779 }
3780
Keith Packardac94a962008-11-20 23:30:27 -08003781 /* Look up object handles */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003782 flips = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003783 for (i = 0; i < args->buffer_count; i++) {
3784 object_list[i] = drm_gem_object_lookup(dev, file_priv,
3785 exec_list[i].handle);
3786 if (object_list[i] == NULL) {
3787 DRM_ERROR("Invalid object handle %d at index %d\n",
3788 exec_list[i].handle, i);
Chris Wilson0ce907f2010-01-23 20:26:35 +00003789 /* prevent error path from reading uninitialized data */
3790 args->buffer_count = i + 1;
Eric Anholt673a3942008-07-30 12:06:12 -07003791 ret = -EBADF;
3792 goto err;
3793 }
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003794
Daniel Vetter23010e42010-03-08 13:35:02 +01003795 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003796 if (obj_priv->in_execbuffer) {
3797 DRM_ERROR("Object %p appears more than once in object list\n",
3798 object_list[i]);
Chris Wilson0ce907f2010-01-23 20:26:35 +00003799 /* prevent error path from reading uninitialized data */
3800 args->buffer_count = i + 1;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003801 ret = -EBADF;
3802 goto err;
3803 }
3804 obj_priv->in_execbuffer = true;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003805 flips += atomic_read(&obj_priv->pending_flip);
3806 }
3807
3808 if (flips > 0) {
3809 ret = i915_gem_wait_for_pending_flip(dev, object_list,
3810 args->buffer_count);
3811 if (ret)
3812 goto err;
Keith Packardac94a962008-11-20 23:30:27 -08003813 }
Eric Anholt673a3942008-07-30 12:06:12 -07003814
Keith Packardac94a962008-11-20 23:30:27 -08003815 /* Pin and relocate */
3816 for (pin_tries = 0; ; pin_tries++) {
3817 ret = 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003818 reloc_index = 0;
3819
Keith Packardac94a962008-11-20 23:30:27 -08003820 for (i = 0; i < args->buffer_count; i++) {
3821 object_list[i]->pending_read_domains = 0;
3822 object_list[i]->pending_write_domain = 0;
3823 ret = i915_gem_object_pin_and_relocate(object_list[i],
3824 file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003825 &exec_list[i],
3826 &relocs[reloc_index]);
Keith Packardac94a962008-11-20 23:30:27 -08003827 if (ret)
3828 break;
3829 pinned = i + 1;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003830 reloc_index += exec_list[i].relocation_count;
Keith Packardac94a962008-11-20 23:30:27 -08003831 }
3832 /* success */
3833 if (ret == 0)
3834 break;
3835
3836 /* error other than GTT full, or we've already tried again */
Chris Wilson2939e1f2009-06-06 09:46:03 +01003837 if (ret != -ENOSPC || pin_tries >= 1) {
Chris Wilson07f73f62009-09-14 16:50:30 +01003838 if (ret != -ERESTARTSYS) {
3839 unsigned long long total_size = 0;
Chris Wilson3d1cc472010-05-27 13:18:19 +01003840 int num_fences = 0;
3841 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson43b27f42010-07-02 08:57:15 +01003842 obj_priv = to_intel_bo(object_list[i]);
Chris Wilson3d1cc472010-05-27 13:18:19 +01003843
Chris Wilson07f73f62009-09-14 16:50:30 +01003844 total_size += object_list[i]->size;
Chris Wilson3d1cc472010-05-27 13:18:19 +01003845 num_fences +=
3846 exec_list[i].flags & EXEC_OBJECT_NEEDS_FENCE &&
3847 obj_priv->tiling_mode != I915_TILING_NONE;
3848 }
3849 DRM_ERROR("Failed to pin buffer %d of %d, total %llu bytes, %d fences: %d\n",
Chris Wilson07f73f62009-09-14 16:50:30 +01003850 pinned+1, args->buffer_count,
Chris Wilson3d1cc472010-05-27 13:18:19 +01003851 total_size, num_fences,
3852 ret);
Chris Wilson07f73f62009-09-14 16:50:30 +01003853 DRM_ERROR("%d objects [%d pinned], "
3854 "%d object bytes [%d pinned], "
3855 "%d/%d gtt bytes\n",
3856 atomic_read(&dev->object_count),
3857 atomic_read(&dev->pin_count),
3858 atomic_read(&dev->object_memory),
3859 atomic_read(&dev->pin_memory),
3860 atomic_read(&dev->gtt_memory),
3861 dev->gtt_total);
3862 }
Eric Anholt673a3942008-07-30 12:06:12 -07003863 goto err;
3864 }
Keith Packardac94a962008-11-20 23:30:27 -08003865
3866 /* unpin all of our buffers */
3867 for (i = 0; i < pinned; i++)
3868 i915_gem_object_unpin(object_list[i]);
Eric Anholtb1177632008-12-10 10:09:41 -08003869 pinned = 0;
Keith Packardac94a962008-11-20 23:30:27 -08003870
3871 /* evict everyone we can from the aperture */
3872 ret = i915_gem_evict_everything(dev);
Chris Wilson07f73f62009-09-14 16:50:30 +01003873 if (ret && ret != -ENOSPC)
Keith Packardac94a962008-11-20 23:30:27 -08003874 goto err;
Eric Anholt673a3942008-07-30 12:06:12 -07003875 }
3876
3877 /* Set the pending read domains for the batch buffer to COMMAND */
3878 batch_obj = object_list[args->buffer_count-1];
Chris Wilson5f26a2c2009-06-06 09:45:58 +01003879 if (batch_obj->pending_write_domain) {
3880 DRM_ERROR("Attempting to use self-modifying batch buffer\n");
3881 ret = -EINVAL;
3882 goto err;
3883 }
3884 batch_obj->pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
Eric Anholt673a3942008-07-30 12:06:12 -07003885
Chris Wilson83d60792009-06-06 09:45:57 +01003886 /* Sanity check the batch buffer, prior to moving objects */
3887 exec_offset = exec_list[args->buffer_count - 1].offset;
3888 ret = i915_gem_check_execbuffer (args, exec_offset);
3889 if (ret != 0) {
3890 DRM_ERROR("execbuf with invalid offset/length\n");
3891 goto err;
3892 }
3893
Eric Anholt673a3942008-07-30 12:06:12 -07003894 i915_verify_inactive(dev, __FILE__, __LINE__);
3895
Keith Packard646f0f62008-11-20 23:23:03 -08003896 /* Zero the global flush/invalidate flags. These
3897 * will be modified as new domains are computed
3898 * for each object
3899 */
3900 dev->invalidate_domains = 0;
3901 dev->flush_domains = 0;
Chris Wilson88f356b2010-08-04 13:55:32 +01003902 dev_priv->flush_rings = 0;
Keith Packard646f0f62008-11-20 23:23:03 -08003903
Eric Anholt673a3942008-07-30 12:06:12 -07003904 for (i = 0; i < args->buffer_count; i++) {
3905 struct drm_gem_object *obj = object_list[i];
Eric Anholt673a3942008-07-30 12:06:12 -07003906
Keith Packard646f0f62008-11-20 23:23:03 -08003907 /* Compute new gpu domains and update invalidate/flush */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003908 i915_gem_object_set_to_gpu_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003909 }
3910
3911 i915_verify_inactive(dev, __FILE__, __LINE__);
3912
Keith Packard646f0f62008-11-20 23:23:03 -08003913 if (dev->invalidate_domains | dev->flush_domains) {
3914#if WATCH_EXEC
3915 DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
3916 __func__,
3917 dev->invalidate_domains,
3918 dev->flush_domains);
3919#endif
3920 i915_gem_flush(dev,
3921 dev->invalidate_domains,
3922 dev->flush_domains);
Chris Wilson88f356b2010-08-04 13:55:32 +01003923 if (dev_priv->flush_rings & FLUSH_RENDER_RING)
Eric Anholtb9624422009-06-03 07:27:35 +00003924 (void)i915_add_request(dev, file_priv,
Chris Wilson88f356b2010-08-04 13:55:32 +01003925 dev->flush_domains,
3926 &dev_priv->render_ring);
3927 if (dev_priv->flush_rings & FLUSH_BSD_RING)
3928 (void)i915_add_request(dev, file_priv,
3929 dev->flush_domains,
3930 &dev_priv->bsd_ring);
Keith Packard646f0f62008-11-20 23:23:03 -08003931 }
Eric Anholt673a3942008-07-30 12:06:12 -07003932
Eric Anholtefbeed92009-02-19 14:54:51 -08003933 for (i = 0; i < args->buffer_count; i++) {
3934 struct drm_gem_object *obj = object_list[i];
Daniel Vetter23010e42010-03-08 13:35:02 +01003935 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003936 uint32_t old_write_domain = obj->write_domain;
Eric Anholtefbeed92009-02-19 14:54:51 -08003937
3938 obj->write_domain = obj->pending_write_domain;
Daniel Vetter99fcb762010-02-07 16:20:18 +01003939 if (obj->write_domain)
3940 list_move_tail(&obj_priv->gpu_write_list,
3941 &dev_priv->mm.gpu_write_list);
3942 else
3943 list_del_init(&obj_priv->gpu_write_list);
3944
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003945 trace_i915_gem_object_change_domain(obj,
3946 obj->read_domains,
3947 old_write_domain);
Eric Anholtefbeed92009-02-19 14:54:51 -08003948 }
3949
Eric Anholt673a3942008-07-30 12:06:12 -07003950 i915_verify_inactive(dev, __FILE__, __LINE__);
3951
3952#if WATCH_COHERENCY
3953 for (i = 0; i < args->buffer_count; i++) {
3954 i915_gem_object_check_coherency(object_list[i],
3955 exec_list[i].handle);
3956 }
3957#endif
3958
Eric Anholt673a3942008-07-30 12:06:12 -07003959#if WATCH_EXEC
Ben Gamari6911a9b2009-04-02 11:24:54 -07003960 i915_gem_dump_object(batch_obj,
Eric Anholt673a3942008-07-30 12:06:12 -07003961 args->batch_len,
3962 __func__,
3963 ~0);
3964#endif
3965
Eric Anholt673a3942008-07-30 12:06:12 -07003966 /* Exec the batchbuffer */
Zou Nan hai852835f2010-05-21 09:08:56 +08003967 ret = ring->dispatch_gem_execbuffer(dev, ring, args,
3968 cliprects, exec_offset);
Eric Anholt673a3942008-07-30 12:06:12 -07003969 if (ret) {
3970 DRM_ERROR("dispatch failed %d\n", ret);
3971 goto err;
3972 }
3973
3974 /*
3975 * Ensure that the commands in the batch buffer are
3976 * finished before the interrupt fires
3977 */
Zou Nan hai852835f2010-05-21 09:08:56 +08003978 flush_domains = i915_retire_commands(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07003979
3980 i915_verify_inactive(dev, __FILE__, __LINE__);
3981
3982 /*
3983 * Get a seqno representing the execution of the current buffer,
3984 * which we can wait on. We would like to mitigate these interrupts,
3985 * likely by only creating seqnos occasionally (so that we have
3986 * *some* interrupts representing completion of buffers that we can
3987 * wait on when trying to clear up gtt space).
3988 */
Zou Nan hai852835f2010-05-21 09:08:56 +08003989 seqno = i915_add_request(dev, file_priv, flush_domains, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07003990 BUG_ON(seqno == 0);
Eric Anholt673a3942008-07-30 12:06:12 -07003991 for (i = 0; i < args->buffer_count; i++) {
3992 struct drm_gem_object *obj = object_list[i];
Zou Nan hai852835f2010-05-21 09:08:56 +08003993 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003994
Zou Nan hai852835f2010-05-21 09:08:56 +08003995 i915_gem_object_move_to_active(obj, seqno, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07003996#if WATCH_LRU
3997 DRM_INFO("%s: move to exec list %p\n", __func__, obj);
3998#endif
3999 }
4000#if WATCH_LRU
4001 i915_dump_lru(dev, __func__);
4002#endif
4003
4004 i915_verify_inactive(dev, __FILE__, __LINE__);
4005
Eric Anholt673a3942008-07-30 12:06:12 -07004006err:
Julia Lawallaad87df2008-12-21 16:28:47 +01004007 for (i = 0; i < pinned; i++)
4008 i915_gem_object_unpin(object_list[i]);
Eric Anholt673a3942008-07-30 12:06:12 -07004009
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05004010 for (i = 0; i < args->buffer_count; i++) {
4011 if (object_list[i]) {
Daniel Vetter23010e42010-03-08 13:35:02 +01004012 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05004013 obj_priv->in_execbuffer = false;
4014 }
Julia Lawallaad87df2008-12-21 16:28:47 +01004015 drm_gem_object_unreference(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05004016 }
Julia Lawallaad87df2008-12-21 16:28:47 +01004017
Eric Anholt673a3942008-07-30 12:06:12 -07004018 mutex_unlock(&dev->struct_mutex);
4019
Chris Wilson93533c22010-01-31 10:40:48 +00004020pre_mutex_err:
Eric Anholt40a5f0d2009-03-12 11:23:52 -07004021 /* Copy the updated relocations out regardless of current error
4022 * state. Failure to update the relocs would mean that the next
4023 * time userland calls execbuf, it would do so with presumed offset
4024 * state that didn't match the actual object state.
4025 */
4026 ret2 = i915_gem_put_relocs_to_user(exec_list, args->buffer_count,
4027 relocs);
4028 if (ret2 != 0) {
4029 DRM_ERROR("Failed to copy relocations back out: %d\n", ret2);
4030
4031 if (ret == 0)
4032 ret = ret2;
4033 }
4034
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07004035 drm_free_large(object_list);
Eric Anholt9a298b22009-03-24 12:23:04 -07004036 kfree(cliprects);
Eric Anholt673a3942008-07-30 12:06:12 -07004037
4038 return ret;
4039}
4040
Jesse Barnes76446ca2009-12-17 22:05:42 -05004041/*
4042 * Legacy execbuffer just creates an exec2 list from the original exec object
4043 * list array and passes it to the real function.
4044 */
4045int
4046i915_gem_execbuffer(struct drm_device *dev, void *data,
4047 struct drm_file *file_priv)
4048{
4049 struct drm_i915_gem_execbuffer *args = data;
4050 struct drm_i915_gem_execbuffer2 exec2;
4051 struct drm_i915_gem_exec_object *exec_list = NULL;
4052 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
4053 int ret, i;
4054
4055#if WATCH_EXEC
4056 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
4057 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
4058#endif
4059
4060 if (args->buffer_count < 1) {
4061 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
4062 return -EINVAL;
4063 }
4064
4065 /* Copy in the exec list from userland */
4066 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
4067 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
4068 if (exec_list == NULL || exec2_list == NULL) {
4069 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
4070 args->buffer_count);
4071 drm_free_large(exec_list);
4072 drm_free_large(exec2_list);
4073 return -ENOMEM;
4074 }
4075 ret = copy_from_user(exec_list,
4076 (struct drm_i915_relocation_entry __user *)
4077 (uintptr_t) args->buffers_ptr,
4078 sizeof(*exec_list) * args->buffer_count);
4079 if (ret != 0) {
4080 DRM_ERROR("copy %d exec entries failed %d\n",
4081 args->buffer_count, ret);
4082 drm_free_large(exec_list);
4083 drm_free_large(exec2_list);
4084 return -EFAULT;
4085 }
4086
4087 for (i = 0; i < args->buffer_count; i++) {
4088 exec2_list[i].handle = exec_list[i].handle;
4089 exec2_list[i].relocation_count = exec_list[i].relocation_count;
4090 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
4091 exec2_list[i].alignment = exec_list[i].alignment;
4092 exec2_list[i].offset = exec_list[i].offset;
4093 if (!IS_I965G(dev))
4094 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
4095 else
4096 exec2_list[i].flags = 0;
4097 }
4098
4099 exec2.buffers_ptr = args->buffers_ptr;
4100 exec2.buffer_count = args->buffer_count;
4101 exec2.batch_start_offset = args->batch_start_offset;
4102 exec2.batch_len = args->batch_len;
4103 exec2.DR1 = args->DR1;
4104 exec2.DR4 = args->DR4;
4105 exec2.num_cliprects = args->num_cliprects;
4106 exec2.cliprects_ptr = args->cliprects_ptr;
Zou Nan hai852835f2010-05-21 09:08:56 +08004107 exec2.flags = I915_EXEC_RENDER;
Jesse Barnes76446ca2009-12-17 22:05:42 -05004108
4109 ret = i915_gem_do_execbuffer(dev, data, file_priv, &exec2, exec2_list);
4110 if (!ret) {
4111 /* Copy the new buffer offsets back to the user's exec list. */
4112 for (i = 0; i < args->buffer_count; i++)
4113 exec_list[i].offset = exec2_list[i].offset;
4114 /* ... and back out to userspace */
4115 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
4116 (uintptr_t) args->buffers_ptr,
4117 exec_list,
4118 sizeof(*exec_list) * args->buffer_count);
4119 if (ret) {
4120 ret = -EFAULT;
4121 DRM_ERROR("failed to copy %d exec entries "
4122 "back to user (%d)\n",
4123 args->buffer_count, ret);
4124 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05004125 }
4126
4127 drm_free_large(exec_list);
4128 drm_free_large(exec2_list);
4129 return ret;
4130}
4131
4132int
4133i915_gem_execbuffer2(struct drm_device *dev, void *data,
4134 struct drm_file *file_priv)
4135{
4136 struct drm_i915_gem_execbuffer2 *args = data;
4137 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
4138 int ret;
4139
4140#if WATCH_EXEC
4141 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
4142 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
4143#endif
4144
4145 if (args->buffer_count < 1) {
4146 DRM_ERROR("execbuf2 with %d buffers\n", args->buffer_count);
4147 return -EINVAL;
4148 }
4149
4150 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
4151 if (exec2_list == NULL) {
4152 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
4153 args->buffer_count);
4154 return -ENOMEM;
4155 }
4156 ret = copy_from_user(exec2_list,
4157 (struct drm_i915_relocation_entry __user *)
4158 (uintptr_t) args->buffers_ptr,
4159 sizeof(*exec2_list) * args->buffer_count);
4160 if (ret != 0) {
4161 DRM_ERROR("copy %d exec entries failed %d\n",
4162 args->buffer_count, ret);
4163 drm_free_large(exec2_list);
4164 return -EFAULT;
4165 }
4166
4167 ret = i915_gem_do_execbuffer(dev, data, file_priv, args, exec2_list);
4168 if (!ret) {
4169 /* Copy the new buffer offsets back to the user's exec list. */
4170 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
4171 (uintptr_t) args->buffers_ptr,
4172 exec2_list,
4173 sizeof(*exec2_list) * args->buffer_count);
4174 if (ret) {
4175 ret = -EFAULT;
4176 DRM_ERROR("failed to copy %d exec entries "
4177 "back to user (%d)\n",
4178 args->buffer_count, ret);
4179 }
4180 }
4181
4182 drm_free_large(exec2_list);
4183 return ret;
4184}
4185
Eric Anholt673a3942008-07-30 12:06:12 -07004186int
4187i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment)
4188{
4189 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01004190 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004191 int ret;
4192
Daniel Vetter778c3542010-05-13 11:49:44 +02004193 BUG_ON(obj_priv->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT);
4194
Eric Anholt673a3942008-07-30 12:06:12 -07004195 i915_verify_inactive(dev, __FILE__, __LINE__);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01004196
4197 if (obj_priv->gtt_space != NULL) {
4198 if (alignment == 0)
4199 alignment = i915_gem_get_gtt_alignment(obj);
4200 if (obj_priv->gtt_offset & (alignment - 1)) {
4201 ret = i915_gem_object_unbind(obj);
4202 if (ret)
4203 return ret;
4204 }
4205 }
4206
Eric Anholt673a3942008-07-30 12:06:12 -07004207 if (obj_priv->gtt_space == NULL) {
4208 ret = i915_gem_object_bind_to_gtt(obj, alignment);
Chris Wilson97311292009-09-21 00:22:34 +01004209 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07004210 return ret;
Chris Wilson22c344e2009-02-11 14:26:45 +00004211 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05004212
Eric Anholt673a3942008-07-30 12:06:12 -07004213 obj_priv->pin_count++;
4214
4215 /* If the object is not active and not pending a flush,
4216 * remove it from the inactive list
4217 */
4218 if (obj_priv->pin_count == 1) {
4219 atomic_inc(&dev->pin_count);
4220 atomic_add(obj->size, &dev->pin_memory);
4221 if (!obj_priv->active &&
Chris Wilson21d509e2009-06-06 09:46:02 +01004222 (obj->write_domain & I915_GEM_GPU_DOMAINS) == 0 &&
Eric Anholt673a3942008-07-30 12:06:12 -07004223 !list_empty(&obj_priv->list))
4224 list_del_init(&obj_priv->list);
4225 }
4226 i915_verify_inactive(dev, __FILE__, __LINE__);
4227
4228 return 0;
4229}
4230
4231void
4232i915_gem_object_unpin(struct drm_gem_object *obj)
4233{
4234 struct drm_device *dev = obj->dev;
4235 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01004236 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004237
4238 i915_verify_inactive(dev, __FILE__, __LINE__);
4239 obj_priv->pin_count--;
4240 BUG_ON(obj_priv->pin_count < 0);
4241 BUG_ON(obj_priv->gtt_space == NULL);
4242
4243 /* If the object is no longer pinned, and is
4244 * neither active nor being flushed, then stick it on
4245 * the inactive list
4246 */
4247 if (obj_priv->pin_count == 0) {
4248 if (!obj_priv->active &&
Chris Wilson21d509e2009-06-06 09:46:02 +01004249 (obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
Eric Anholt673a3942008-07-30 12:06:12 -07004250 list_move_tail(&obj_priv->list,
4251 &dev_priv->mm.inactive_list);
4252 atomic_dec(&dev->pin_count);
4253 atomic_sub(obj->size, &dev->pin_memory);
4254 }
4255 i915_verify_inactive(dev, __FILE__, __LINE__);
4256}
4257
4258int
4259i915_gem_pin_ioctl(struct drm_device *dev, void *data,
4260 struct drm_file *file_priv)
4261{
4262 struct drm_i915_gem_pin *args = data;
4263 struct drm_gem_object *obj;
4264 struct drm_i915_gem_object *obj_priv;
4265 int ret;
4266
4267 mutex_lock(&dev->struct_mutex);
4268
4269 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4270 if (obj == NULL) {
4271 DRM_ERROR("Bad handle in i915_gem_pin_ioctl(): %d\n",
4272 args->handle);
4273 mutex_unlock(&dev->struct_mutex);
4274 return -EBADF;
4275 }
Daniel Vetter23010e42010-03-08 13:35:02 +01004276 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004277
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004278 if (obj_priv->madv != I915_MADV_WILLNEED) {
4279 DRM_ERROR("Attempting to pin a purgeable buffer\n");
Chris Wilson3ef94da2009-09-14 16:50:29 +01004280 drm_gem_object_unreference(obj);
4281 mutex_unlock(&dev->struct_mutex);
4282 return -EINVAL;
4283 }
4284
Jesse Barnes79e53942008-11-07 14:24:08 -08004285 if (obj_priv->pin_filp != NULL && obj_priv->pin_filp != file_priv) {
4286 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
4287 args->handle);
Chris Wilson96dec612009-02-08 19:08:04 +00004288 drm_gem_object_unreference(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004289 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08004290 return -EINVAL;
4291 }
4292
4293 obj_priv->user_pin_count++;
4294 obj_priv->pin_filp = file_priv;
4295 if (obj_priv->user_pin_count == 1) {
4296 ret = i915_gem_object_pin(obj, args->alignment);
4297 if (ret != 0) {
4298 drm_gem_object_unreference(obj);
4299 mutex_unlock(&dev->struct_mutex);
4300 return ret;
4301 }
Eric Anholt673a3942008-07-30 12:06:12 -07004302 }
4303
4304 /* XXX - flush the CPU caches for pinned objects
4305 * as the X server doesn't manage domains yet
4306 */
Eric Anholte47c68e2008-11-14 13:35:19 -08004307 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004308 args->offset = obj_priv->gtt_offset;
4309 drm_gem_object_unreference(obj);
4310 mutex_unlock(&dev->struct_mutex);
4311
4312 return 0;
4313}
4314
4315int
4316i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
4317 struct drm_file *file_priv)
4318{
4319 struct drm_i915_gem_pin *args = data;
4320 struct drm_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08004321 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07004322
4323 mutex_lock(&dev->struct_mutex);
4324
4325 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4326 if (obj == NULL) {
4327 DRM_ERROR("Bad handle in i915_gem_unpin_ioctl(): %d\n",
4328 args->handle);
4329 mutex_unlock(&dev->struct_mutex);
4330 return -EBADF;
4331 }
4332
Daniel Vetter23010e42010-03-08 13:35:02 +01004333 obj_priv = to_intel_bo(obj);
Jesse Barnes79e53942008-11-07 14:24:08 -08004334 if (obj_priv->pin_filp != file_priv) {
4335 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
4336 args->handle);
4337 drm_gem_object_unreference(obj);
4338 mutex_unlock(&dev->struct_mutex);
4339 return -EINVAL;
4340 }
4341 obj_priv->user_pin_count--;
4342 if (obj_priv->user_pin_count == 0) {
4343 obj_priv->pin_filp = NULL;
4344 i915_gem_object_unpin(obj);
4345 }
Eric Anholt673a3942008-07-30 12:06:12 -07004346
4347 drm_gem_object_unreference(obj);
4348 mutex_unlock(&dev->struct_mutex);
4349 return 0;
4350}
4351
4352int
4353i915_gem_busy_ioctl(struct drm_device *dev, void *data,
4354 struct drm_file *file_priv)
4355{
4356 struct drm_i915_gem_busy *args = data;
4357 struct drm_gem_object *obj;
4358 struct drm_i915_gem_object *obj_priv;
4359
Eric Anholt673a3942008-07-30 12:06:12 -07004360 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4361 if (obj == NULL) {
4362 DRM_ERROR("Bad handle in i915_gem_busy_ioctl(): %d\n",
4363 args->handle);
Eric Anholt673a3942008-07-30 12:06:12 -07004364 return -EBADF;
4365 }
4366
Chris Wilsonb1ce7862009-06-06 09:46:00 +01004367 mutex_lock(&dev->struct_mutex);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004368
Chris Wilson0be555b2010-08-04 15:36:30 +01004369 /* Count all active objects as busy, even if they are currently not used
4370 * by the gpu. Users of this interface expect objects to eventually
4371 * become non-busy without any further actions, therefore emit any
4372 * necessary flushes here.
Eric Anholtc4de0a52008-12-14 19:05:04 -08004373 */
Chris Wilson0be555b2010-08-04 15:36:30 +01004374 obj_priv = to_intel_bo(obj);
4375 args->busy = obj_priv->active;
4376 if (args->busy) {
4377 /* Unconditionally flush objects, even when the gpu still uses this
4378 * object. Userspace calling this function indicates that it wants to
4379 * use this buffer rather sooner than later, so issuing the required
4380 * flush earlier is beneficial.
4381 */
4382 if (obj->write_domain) {
4383 i915_gem_flush(dev, 0, obj->write_domain);
4384 (void)i915_add_request(dev, file_priv, obj->write_domain, obj_priv->ring);
4385 }
4386
4387 /* Update the active list for the hardware's current position.
4388 * Otherwise this only updates on a delayed timer or when irqs
4389 * are actually unmasked, and our working set ends up being
4390 * larger than required.
4391 */
4392 i915_gem_retire_requests_ring(dev, obj_priv->ring);
4393
4394 args->busy = obj_priv->active;
4395 }
Eric Anholt673a3942008-07-30 12:06:12 -07004396
4397 drm_gem_object_unreference(obj);
4398 mutex_unlock(&dev->struct_mutex);
4399 return 0;
4400}
4401
4402int
4403i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4404 struct drm_file *file_priv)
4405{
4406 return i915_gem_ring_throttle(dev, file_priv);
4407}
4408
Chris Wilson3ef94da2009-09-14 16:50:29 +01004409int
4410i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4411 struct drm_file *file_priv)
4412{
4413 struct drm_i915_gem_madvise *args = data;
4414 struct drm_gem_object *obj;
4415 struct drm_i915_gem_object *obj_priv;
4416
4417 switch (args->madv) {
4418 case I915_MADV_DONTNEED:
4419 case I915_MADV_WILLNEED:
4420 break;
4421 default:
4422 return -EINVAL;
4423 }
4424
4425 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4426 if (obj == NULL) {
4427 DRM_ERROR("Bad handle in i915_gem_madvise_ioctl(): %d\n",
4428 args->handle);
4429 return -EBADF;
4430 }
4431
4432 mutex_lock(&dev->struct_mutex);
Daniel Vetter23010e42010-03-08 13:35:02 +01004433 obj_priv = to_intel_bo(obj);
Chris Wilson3ef94da2009-09-14 16:50:29 +01004434
4435 if (obj_priv->pin_count) {
4436 drm_gem_object_unreference(obj);
4437 mutex_unlock(&dev->struct_mutex);
4438
4439 DRM_ERROR("Attempted i915_gem_madvise_ioctl() on a pinned object\n");
4440 return -EINVAL;
4441 }
4442
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004443 if (obj_priv->madv != __I915_MADV_PURGED)
4444 obj_priv->madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004445
Chris Wilson2d7ef392009-09-20 23:13:10 +01004446 /* if the object is no longer bound, discard its backing storage */
4447 if (i915_gem_object_is_purgeable(obj_priv) &&
4448 obj_priv->gtt_space == NULL)
4449 i915_gem_object_truncate(obj);
4450
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004451 args->retained = obj_priv->madv != __I915_MADV_PURGED;
4452
Chris Wilson3ef94da2009-09-14 16:50:29 +01004453 drm_gem_object_unreference(obj);
4454 mutex_unlock(&dev->struct_mutex);
4455
4456 return 0;
4457}
4458
Daniel Vetterac52bc52010-04-09 19:05:06 +00004459struct drm_gem_object * i915_gem_alloc_object(struct drm_device *dev,
4460 size_t size)
4461{
Daniel Vetterc397b902010-04-09 19:05:07 +00004462 struct drm_i915_gem_object *obj;
4463
4464 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
4465 if (obj == NULL)
4466 return NULL;
4467
4468 if (drm_gem_object_init(dev, &obj->base, size) != 0) {
4469 kfree(obj);
4470 return NULL;
4471 }
4472
4473 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4474 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4475
4476 obj->agp_type = AGP_USER_MEMORY;
Daniel Vetter62b8b212010-04-09 19:05:08 +00004477 obj->base.driver_private = NULL;
Daniel Vetterc397b902010-04-09 19:05:07 +00004478 obj->fence_reg = I915_FENCE_REG_NONE;
4479 INIT_LIST_HEAD(&obj->list);
4480 INIT_LIST_HEAD(&obj->gpu_write_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00004481 obj->madv = I915_MADV_WILLNEED;
4482
4483 trace_i915_gem_object_create(&obj->base);
4484
4485 return &obj->base;
Daniel Vetterac52bc52010-04-09 19:05:06 +00004486}
4487
Eric Anholt673a3942008-07-30 12:06:12 -07004488int i915_gem_init_object(struct drm_gem_object *obj)
4489{
Daniel Vetterc397b902010-04-09 19:05:07 +00004490 BUG();
Jesse Barnesde151cf2008-11-12 10:03:55 -08004491
Eric Anholt673a3942008-07-30 12:06:12 -07004492 return 0;
4493}
4494
Chris Wilsonbe726152010-07-23 23:18:50 +01004495static void i915_gem_free_object_tail(struct drm_gem_object *obj)
4496{
4497 struct drm_device *dev = obj->dev;
4498 drm_i915_private_t *dev_priv = dev->dev_private;
4499 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
4500 int ret;
4501
4502 ret = i915_gem_object_unbind(obj);
4503 if (ret == -ERESTARTSYS) {
4504 list_move(&obj_priv->list,
4505 &dev_priv->mm.deferred_free_list);
4506 return;
4507 }
4508
4509 if (obj_priv->mmap_offset)
4510 i915_gem_free_mmap_offset(obj);
4511
4512 drm_gem_object_release(obj);
4513
4514 kfree(obj_priv->page_cpu_valid);
4515 kfree(obj_priv->bit_17);
4516 kfree(obj_priv);
4517}
4518
Eric Anholt673a3942008-07-30 12:06:12 -07004519void i915_gem_free_object(struct drm_gem_object *obj)
4520{
Jesse Barnesde151cf2008-11-12 10:03:55 -08004521 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01004522 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004523
Chris Wilson1c5d22f2009-08-25 11:15:50 +01004524 trace_i915_gem_object_destroy(obj);
4525
Eric Anholt673a3942008-07-30 12:06:12 -07004526 while (obj_priv->pin_count > 0)
4527 i915_gem_object_unpin(obj);
4528
Dave Airlie71acb5e2008-12-30 20:31:46 +10004529 if (obj_priv->phys_obj)
4530 i915_gem_detach_phys_object(dev, obj);
4531
Chris Wilsonbe726152010-07-23 23:18:50 +01004532 i915_gem_free_object_tail(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004533}
4534
Chris Wilsonab5ee572009-09-20 19:25:47 +01004535/** Unbinds all inactive objects. */
Eric Anholt673a3942008-07-30 12:06:12 -07004536static int
Chris Wilsonab5ee572009-09-20 19:25:47 +01004537i915_gem_evict_from_inactive_list(struct drm_device *dev)
Eric Anholt673a3942008-07-30 12:06:12 -07004538{
Chris Wilsonab5ee572009-09-20 19:25:47 +01004539 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07004540
Chris Wilsonab5ee572009-09-20 19:25:47 +01004541 while (!list_empty(&dev_priv->mm.inactive_list)) {
4542 struct drm_gem_object *obj;
4543 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004544
Daniel Vettera8089e82010-04-09 19:05:09 +00004545 obj = &list_first_entry(&dev_priv->mm.inactive_list,
4546 struct drm_i915_gem_object,
4547 list)->base;
Eric Anholt673a3942008-07-30 12:06:12 -07004548
4549 ret = i915_gem_object_unbind(obj);
4550 if (ret != 0) {
Chris Wilsonab5ee572009-09-20 19:25:47 +01004551 DRM_ERROR("Error unbinding object: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07004552 return ret;
4553 }
4554 }
4555
Eric Anholt673a3942008-07-30 12:06:12 -07004556 return 0;
4557}
4558
Jesse Barnes5669fca2009-02-17 15:13:31 -08004559int
Eric Anholt673a3942008-07-30 12:06:12 -07004560i915_gem_idle(struct drm_device *dev)
4561{
4562 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson29105cc2010-01-07 10:39:13 +00004563 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004564
Keith Packard6dbe2772008-10-14 21:41:13 -07004565 mutex_lock(&dev->struct_mutex);
4566
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004567 if (dev_priv->mm.suspended ||
Zou Nan haid1b851f2010-05-21 09:08:57 +08004568 (dev_priv->render_ring.gem_object == NULL) ||
4569 (HAS_BSD(dev) &&
4570 dev_priv->bsd_ring.gem_object == NULL)) {
Keith Packard6dbe2772008-10-14 21:41:13 -07004571 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004572 return 0;
Keith Packard6dbe2772008-10-14 21:41:13 -07004573 }
Eric Anholt673a3942008-07-30 12:06:12 -07004574
Chris Wilson29105cc2010-01-07 10:39:13 +00004575 ret = i915_gpu_idle(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004576 if (ret) {
4577 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004578 return ret;
Keith Packard6dbe2772008-10-14 21:41:13 -07004579 }
Eric Anholt673a3942008-07-30 12:06:12 -07004580
Chris Wilson29105cc2010-01-07 10:39:13 +00004581 /* Under UMS, be paranoid and evict. */
4582 if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
4583 ret = i915_gem_evict_from_inactive_list(dev);
4584 if (ret) {
4585 mutex_unlock(&dev->struct_mutex);
4586 return ret;
4587 }
4588 }
4589
4590 /* Hack! Don't let anybody do execbuf while we don't control the chip.
4591 * We need to replace this with a semaphore, or something.
4592 * And not confound mm.suspended!
4593 */
4594 dev_priv->mm.suspended = 1;
4595 del_timer(&dev_priv->hangcheck_timer);
4596
4597 i915_kernel_lost_context(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004598 i915_gem_cleanup_ringbuffer(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00004599
Keith Packard6dbe2772008-10-14 21:41:13 -07004600 mutex_unlock(&dev->struct_mutex);
4601
Chris Wilson29105cc2010-01-07 10:39:13 +00004602 /* Cancel the retire work handler, which should be idle now. */
4603 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
4604
Eric Anholt673a3942008-07-30 12:06:12 -07004605 return 0;
4606}
4607
Jesse Barnese552eb72010-04-21 11:39:23 -07004608/*
4609 * 965+ support PIPE_CONTROL commands, which provide finer grained control
4610 * over cache flushing.
4611 */
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004612static int
Jesse Barnese552eb72010-04-21 11:39:23 -07004613i915_gem_init_pipe_control(struct drm_device *dev)
4614{
4615 drm_i915_private_t *dev_priv = dev->dev_private;
4616 struct drm_gem_object *obj;
4617 struct drm_i915_gem_object *obj_priv;
4618 int ret;
4619
Eric Anholt34dc4d42010-05-07 14:30:03 -07004620 obj = i915_gem_alloc_object(dev, 4096);
Jesse Barnese552eb72010-04-21 11:39:23 -07004621 if (obj == NULL) {
4622 DRM_ERROR("Failed to allocate seqno page\n");
4623 ret = -ENOMEM;
4624 goto err;
4625 }
4626 obj_priv = to_intel_bo(obj);
4627 obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
4628
4629 ret = i915_gem_object_pin(obj, 4096);
4630 if (ret)
4631 goto err_unref;
4632
4633 dev_priv->seqno_gfx_addr = obj_priv->gtt_offset;
4634 dev_priv->seqno_page = kmap(obj_priv->pages[0]);
4635 if (dev_priv->seqno_page == NULL)
4636 goto err_unpin;
4637
4638 dev_priv->seqno_obj = obj;
4639 memset(dev_priv->seqno_page, 0, PAGE_SIZE);
4640
4641 return 0;
4642
4643err_unpin:
4644 i915_gem_object_unpin(obj);
4645err_unref:
4646 drm_gem_object_unreference(obj);
4647err:
4648 return ret;
4649}
4650
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004651
4652static void
Jesse Barnese552eb72010-04-21 11:39:23 -07004653i915_gem_cleanup_pipe_control(struct drm_device *dev)
4654{
4655 drm_i915_private_t *dev_priv = dev->dev_private;
4656 struct drm_gem_object *obj;
4657 struct drm_i915_gem_object *obj_priv;
4658
4659 obj = dev_priv->seqno_obj;
4660 obj_priv = to_intel_bo(obj);
4661 kunmap(obj_priv->pages[0]);
4662 i915_gem_object_unpin(obj);
4663 drm_gem_object_unreference(obj);
4664 dev_priv->seqno_obj = NULL;
4665
4666 dev_priv->seqno_page = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07004667}
4668
Eric Anholt673a3942008-07-30 12:06:12 -07004669int
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004670i915_gem_init_ringbuffer(struct drm_device *dev)
4671{
4672 drm_i915_private_t *dev_priv = dev->dev_private;
4673 int ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01004674
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004675 dev_priv->render_ring = render_ring;
Chris Wilson68f95ba2010-05-27 13:18:22 +01004676
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004677 if (!I915_NEED_GFX_HWS(dev)) {
4678 dev_priv->render_ring.status_page.page_addr
4679 = dev_priv->status_page_dmah->vaddr;
4680 memset(dev_priv->render_ring.status_page.page_addr,
4681 0, PAGE_SIZE);
4682 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004683
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004684 if (HAS_PIPE_CONTROL(dev)) {
4685 ret = i915_gem_init_pipe_control(dev);
4686 if (ret)
4687 return ret;
4688 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004689
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004690 ret = intel_init_ring_buffer(dev, &dev_priv->render_ring);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004691 if (ret)
4692 goto cleanup_pipe_control;
4693
4694 if (HAS_BSD(dev)) {
Zou Nan haid1b851f2010-05-21 09:08:57 +08004695 dev_priv->bsd_ring = bsd_ring;
4696 ret = intel_init_ring_buffer(dev, &dev_priv->bsd_ring);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004697 if (ret)
4698 goto cleanup_render_ring;
Zou Nan haid1b851f2010-05-21 09:08:57 +08004699 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004700
4701 return 0;
4702
4703cleanup_render_ring:
4704 intel_cleanup_ring_buffer(dev, &dev_priv->render_ring);
4705cleanup_pipe_control:
4706 if (HAS_PIPE_CONTROL(dev))
4707 i915_gem_cleanup_pipe_control(dev);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004708 return ret;
4709}
4710
4711void
4712i915_gem_cleanup_ringbuffer(struct drm_device *dev)
4713{
4714 drm_i915_private_t *dev_priv = dev->dev_private;
4715
4716 intel_cleanup_ring_buffer(dev, &dev_priv->render_ring);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004717 if (HAS_BSD(dev))
4718 intel_cleanup_ring_buffer(dev, &dev_priv->bsd_ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004719 if (HAS_PIPE_CONTROL(dev))
4720 i915_gem_cleanup_pipe_control(dev);
4721}
4722
4723int
Eric Anholt673a3942008-07-30 12:06:12 -07004724i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
4725 struct drm_file *file_priv)
4726{
4727 drm_i915_private_t *dev_priv = dev->dev_private;
4728 int ret;
4729
Jesse Barnes79e53942008-11-07 14:24:08 -08004730 if (drm_core_check_feature(dev, DRIVER_MODESET))
4731 return 0;
4732
Ben Gamariba1234d2009-09-14 17:48:47 -04004733 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07004734 DRM_ERROR("Reenabling wedged hardware, good luck\n");
Ben Gamariba1234d2009-09-14 17:48:47 -04004735 atomic_set(&dev_priv->mm.wedged, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004736 }
4737
Eric Anholt673a3942008-07-30 12:06:12 -07004738 mutex_lock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004739 dev_priv->mm.suspended = 0;
4740
4741 ret = i915_gem_init_ringbuffer(dev);
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004742 if (ret != 0) {
4743 mutex_unlock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004744 return ret;
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004745 }
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004746
Carl Worth5e118f42009-03-20 11:54:25 -07004747 spin_lock(&dev_priv->mm.active_list_lock);
Zou Nan hai852835f2010-05-21 09:08:56 +08004748 BUG_ON(!list_empty(&dev_priv->render_ring.active_list));
Zou Nan haid1b851f2010-05-21 09:08:57 +08004749 BUG_ON(HAS_BSD(dev) && !list_empty(&dev_priv->bsd_ring.active_list));
Carl Worth5e118f42009-03-20 11:54:25 -07004750 spin_unlock(&dev_priv->mm.active_list_lock);
4751
Eric Anholt673a3942008-07-30 12:06:12 -07004752 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
4753 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
Zou Nan hai852835f2010-05-21 09:08:56 +08004754 BUG_ON(!list_empty(&dev_priv->render_ring.request_list));
Zou Nan haid1b851f2010-05-21 09:08:57 +08004755 BUG_ON(HAS_BSD(dev) && !list_empty(&dev_priv->bsd_ring.request_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004756 mutex_unlock(&dev->struct_mutex);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004757
Chris Wilson5f353082010-06-07 14:03:03 +01004758 ret = drm_irq_install(dev);
4759 if (ret)
4760 goto cleanup_ringbuffer;
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004761
Eric Anholt673a3942008-07-30 12:06:12 -07004762 return 0;
Chris Wilson5f353082010-06-07 14:03:03 +01004763
4764cleanup_ringbuffer:
4765 mutex_lock(&dev->struct_mutex);
4766 i915_gem_cleanup_ringbuffer(dev);
4767 dev_priv->mm.suspended = 1;
4768 mutex_unlock(&dev->struct_mutex);
4769
4770 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004771}
4772
4773int
4774i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
4775 struct drm_file *file_priv)
4776{
Jesse Barnes79e53942008-11-07 14:24:08 -08004777 if (drm_core_check_feature(dev, DRIVER_MODESET))
4778 return 0;
4779
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004780 drm_irq_uninstall(dev);
Linus Torvaldse6890f62009-09-08 17:09:24 -07004781 return i915_gem_idle(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004782}
4783
4784void
4785i915_gem_lastclose(struct drm_device *dev)
4786{
4787 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004788
Eric Anholte806b492009-01-22 09:56:58 -08004789 if (drm_core_check_feature(dev, DRIVER_MODESET))
4790 return;
4791
Keith Packard6dbe2772008-10-14 21:41:13 -07004792 ret = i915_gem_idle(dev);
4793 if (ret)
4794 DRM_ERROR("failed to idle hardware: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07004795}
4796
4797void
4798i915_gem_load(struct drm_device *dev)
4799{
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004800 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07004801 drm_i915_private_t *dev_priv = dev->dev_private;
4802
Carl Worth5e118f42009-03-20 11:54:25 -07004803 spin_lock_init(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07004804 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
Daniel Vetter99fcb762010-02-07 16:20:18 +01004805 INIT_LIST_HEAD(&dev_priv->mm.gpu_write_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004806 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07004807 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Chris Wilsonbe726152010-07-23 23:18:50 +01004808 INIT_LIST_HEAD(&dev_priv->mm.deferred_free_list);
Zou Nan hai852835f2010-05-21 09:08:56 +08004809 INIT_LIST_HEAD(&dev_priv->render_ring.active_list);
4810 INIT_LIST_HEAD(&dev_priv->render_ring.request_list);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004811 if (HAS_BSD(dev)) {
4812 INIT_LIST_HEAD(&dev_priv->bsd_ring.active_list);
4813 INIT_LIST_HEAD(&dev_priv->bsd_ring.request_list);
4814 }
Daniel Vetter007cc8a2010-04-28 11:02:31 +02004815 for (i = 0; i < 16; i++)
4816 INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004817 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
4818 i915_gem_retire_work_handler);
Chris Wilson31169712009-09-14 16:50:28 +01004819 spin_lock(&shrink_list_lock);
4820 list_add(&dev_priv->mm.shrink_list, &shrink_list);
4821 spin_unlock(&shrink_list_lock);
4822
Dave Airlie94400122010-07-20 13:15:31 +10004823 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
4824 if (IS_GEN3(dev)) {
4825 u32 tmp = I915_READ(MI_ARB_STATE);
4826 if (!(tmp & MI_ARB_C3_LP_WRITE_ENABLE)) {
4827 /* arb state is a masked write, so set bit + bit in mask */
4828 tmp = MI_ARB_C3_LP_WRITE_ENABLE | (MI_ARB_C3_LP_WRITE_ENABLE << MI_ARB_MASK_SHIFT);
4829 I915_WRITE(MI_ARB_STATE, tmp);
4830 }
4831 }
4832
Jesse Barnesde151cf2008-11-12 10:03:55 -08004833 /* Old X drivers will take 0-2 for front, back, depth buffers */
Eric Anholtb397c832010-01-26 09:43:10 -08004834 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4835 dev_priv->fence_reg_start = 3;
Jesse Barnesde151cf2008-11-12 10:03:55 -08004836
Jesse Barnes0f973f22009-01-26 17:10:45 -08004837 if (IS_I965G(dev) || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08004838 dev_priv->num_fence_regs = 16;
4839 else
4840 dev_priv->num_fence_regs = 8;
4841
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004842 /* Initialize fence registers to zero */
4843 if (IS_I965G(dev)) {
4844 for (i = 0; i < 16; i++)
4845 I915_WRITE64(FENCE_REG_965_0 + (i * 8), 0);
4846 } else {
4847 for (i = 0; i < 8; i++)
4848 I915_WRITE(FENCE_REG_830_0 + (i * 4), 0);
4849 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4850 for (i = 0; i < 8; i++)
4851 I915_WRITE(FENCE_REG_945_8 + (i * 4), 0);
4852 }
Eric Anholt673a3942008-07-30 12:06:12 -07004853 i915_gem_detect_bit_6_swizzle(dev);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05004854 init_waitqueue_head(&dev_priv->pending_flip_queue);
Eric Anholt673a3942008-07-30 12:06:12 -07004855}
Dave Airlie71acb5e2008-12-30 20:31:46 +10004856
4857/*
4858 * Create a physically contiguous memory object for this object
4859 * e.g. for cursor + overlay regs
4860 */
4861int i915_gem_init_phys_object(struct drm_device *dev,
4862 int id, int size)
4863{
4864 drm_i915_private_t *dev_priv = dev->dev_private;
4865 struct drm_i915_gem_phys_object *phys_obj;
4866 int ret;
4867
4868 if (dev_priv->mm.phys_objs[id - 1] || !size)
4869 return 0;
4870
Eric Anholt9a298b22009-03-24 12:23:04 -07004871 phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004872 if (!phys_obj)
4873 return -ENOMEM;
4874
4875 phys_obj->id = id;
4876
Zhenyu Wange6be8d92010-01-05 11:25:05 +08004877 phys_obj->handle = drm_pci_alloc(dev, size, 0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004878 if (!phys_obj->handle) {
4879 ret = -ENOMEM;
4880 goto kfree_obj;
4881 }
4882#ifdef CONFIG_X86
4883 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4884#endif
4885
4886 dev_priv->mm.phys_objs[id - 1] = phys_obj;
4887
4888 return 0;
4889kfree_obj:
Eric Anholt9a298b22009-03-24 12:23:04 -07004890 kfree(phys_obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004891 return ret;
4892}
4893
4894void i915_gem_free_phys_object(struct drm_device *dev, int id)
4895{
4896 drm_i915_private_t *dev_priv = dev->dev_private;
4897 struct drm_i915_gem_phys_object *phys_obj;
4898
4899 if (!dev_priv->mm.phys_objs[id - 1])
4900 return;
4901
4902 phys_obj = dev_priv->mm.phys_objs[id - 1];
4903 if (phys_obj->cur_obj) {
4904 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
4905 }
4906
4907#ifdef CONFIG_X86
4908 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4909#endif
4910 drm_pci_free(dev, phys_obj->handle);
4911 kfree(phys_obj);
4912 dev_priv->mm.phys_objs[id - 1] = NULL;
4913}
4914
4915void i915_gem_free_all_phys_object(struct drm_device *dev)
4916{
4917 int i;
4918
Dave Airlie260883c2009-01-22 17:58:49 +10004919 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004920 i915_gem_free_phys_object(dev, i);
4921}
4922
4923void i915_gem_detach_phys_object(struct drm_device *dev,
4924 struct drm_gem_object *obj)
4925{
4926 struct drm_i915_gem_object *obj_priv;
4927 int i;
4928 int ret;
4929 int page_count;
4930
Daniel Vetter23010e42010-03-08 13:35:02 +01004931 obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004932 if (!obj_priv->phys_obj)
4933 return;
4934
Chris Wilson4bdadb92010-01-27 13:36:32 +00004935 ret = i915_gem_object_get_pages(obj, 0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004936 if (ret)
4937 goto out;
4938
4939 page_count = obj->size / PAGE_SIZE;
4940
4941 for (i = 0; i < page_count; i++) {
Eric Anholt856fa192009-03-19 14:10:50 -07004942 char *dst = kmap_atomic(obj_priv->pages[i], KM_USER0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004943 char *src = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4944
4945 memcpy(dst, src, PAGE_SIZE);
4946 kunmap_atomic(dst, KM_USER0);
4947 }
Eric Anholt856fa192009-03-19 14:10:50 -07004948 drm_clflush_pages(obj_priv->pages, page_count);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004949 drm_agp_chipset_flush(dev);
Chris Wilsond78b47b2009-06-17 21:52:49 +01004950
4951 i915_gem_object_put_pages(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004952out:
4953 obj_priv->phys_obj->cur_obj = NULL;
4954 obj_priv->phys_obj = NULL;
4955}
4956
4957int
4958i915_gem_attach_phys_object(struct drm_device *dev,
4959 struct drm_gem_object *obj, int id)
4960{
4961 drm_i915_private_t *dev_priv = dev->dev_private;
4962 struct drm_i915_gem_object *obj_priv;
4963 int ret = 0;
4964 int page_count;
4965 int i;
4966
4967 if (id > I915_MAX_PHYS_OBJECT)
4968 return -EINVAL;
4969
Daniel Vetter23010e42010-03-08 13:35:02 +01004970 obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004971
4972 if (obj_priv->phys_obj) {
4973 if (obj_priv->phys_obj->id == id)
4974 return 0;
4975 i915_gem_detach_phys_object(dev, obj);
4976 }
4977
4978
4979 /* create a new object */
4980 if (!dev_priv->mm.phys_objs[id - 1]) {
4981 ret = i915_gem_init_phys_object(dev, id,
4982 obj->size);
4983 if (ret) {
Linus Torvaldsaeb565d2009-01-26 10:01:53 -08004984 DRM_ERROR("failed to init phys object %d size: %zu\n", id, obj->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004985 goto out;
4986 }
4987 }
4988
4989 /* bind to the object */
4990 obj_priv->phys_obj = dev_priv->mm.phys_objs[id - 1];
4991 obj_priv->phys_obj->cur_obj = obj;
4992
Chris Wilson4bdadb92010-01-27 13:36:32 +00004993 ret = i915_gem_object_get_pages(obj, 0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004994 if (ret) {
4995 DRM_ERROR("failed to get page list\n");
4996 goto out;
4997 }
4998
4999 page_count = obj->size / PAGE_SIZE;
5000
5001 for (i = 0; i < page_count; i++) {
Eric Anholt856fa192009-03-19 14:10:50 -07005002 char *src = kmap_atomic(obj_priv->pages[i], KM_USER0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10005003 char *dst = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
5004
5005 memcpy(dst, src, PAGE_SIZE);
5006 kunmap_atomic(src, KM_USER0);
5007 }
5008
Chris Wilsond78b47b2009-06-17 21:52:49 +01005009 i915_gem_object_put_pages(obj);
5010
Dave Airlie71acb5e2008-12-30 20:31:46 +10005011 return 0;
5012out:
5013 return ret;
5014}
5015
5016static int
5017i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
5018 struct drm_i915_gem_pwrite *args,
5019 struct drm_file *file_priv)
5020{
Daniel Vetter23010e42010-03-08 13:35:02 +01005021 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10005022 void *obj_addr;
5023 int ret;
5024 char __user *user_data;
5025
5026 user_data = (char __user *) (uintptr_t) args->data_ptr;
5027 obj_addr = obj_priv->phys_obj->handle->vaddr + args->offset;
5028
Zhao Yakui44d98a62009-10-09 11:39:40 +08005029 DRM_DEBUG_DRIVER("obj_addr %p, %lld\n", obj_addr, args->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10005030 ret = copy_from_user(obj_addr, user_data, args->size);
5031 if (ret)
5032 return -EFAULT;
5033
5034 drm_agp_chipset_flush(dev);
5035 return 0;
5036}
Eric Anholtb9624422009-06-03 07:27:35 +00005037
5038void i915_gem_release(struct drm_device * dev, struct drm_file *file_priv)
5039{
5040 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
5041
5042 /* Clean up our request list when the client is going away, so that
5043 * later retire_requests won't dereference our soon-to-be-gone
5044 * file_priv.
5045 */
5046 mutex_lock(&dev->struct_mutex);
5047 while (!list_empty(&i915_file_priv->mm.request_list))
5048 list_del_init(i915_file_priv->mm.request_list.next);
5049 mutex_unlock(&dev->struct_mutex);
5050}
Chris Wilson31169712009-09-14 16:50:28 +01005051
Chris Wilson31169712009-09-14 16:50:28 +01005052static int
Chris Wilson1637ef42010-04-20 17:10:35 +01005053i915_gpu_is_active(struct drm_device *dev)
5054{
5055 drm_i915_private_t *dev_priv = dev->dev_private;
5056 int lists_empty;
5057
5058 spin_lock(&dev_priv->mm.active_list_lock);
5059 lists_empty = list_empty(&dev_priv->mm.flushing_list) &&
Zou Nan hai852835f2010-05-21 09:08:56 +08005060 list_empty(&dev_priv->render_ring.active_list);
Zou Nan haid1b851f2010-05-21 09:08:57 +08005061 if (HAS_BSD(dev))
5062 lists_empty &= list_empty(&dev_priv->bsd_ring.active_list);
Chris Wilson1637ef42010-04-20 17:10:35 +01005063 spin_unlock(&dev_priv->mm.active_list_lock);
5064
5065 return !lists_empty;
5066}
5067
5068static int
Dave Chinner7f8275d2010-07-19 14:56:17 +10005069i915_gem_shrink(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask)
Chris Wilson31169712009-09-14 16:50:28 +01005070{
5071 drm_i915_private_t *dev_priv, *next_dev;
5072 struct drm_i915_gem_object *obj_priv, *next_obj;
5073 int cnt = 0;
5074 int would_deadlock = 1;
5075
5076 /* "fast-path" to count number of available objects */
5077 if (nr_to_scan == 0) {
5078 spin_lock(&shrink_list_lock);
5079 list_for_each_entry(dev_priv, &shrink_list, mm.shrink_list) {
5080 struct drm_device *dev = dev_priv->dev;
5081
5082 if (mutex_trylock(&dev->struct_mutex)) {
5083 list_for_each_entry(obj_priv,
5084 &dev_priv->mm.inactive_list,
5085 list)
5086 cnt++;
5087 mutex_unlock(&dev->struct_mutex);
5088 }
5089 }
5090 spin_unlock(&shrink_list_lock);
5091
5092 return (cnt / 100) * sysctl_vfs_cache_pressure;
5093 }
5094
5095 spin_lock(&shrink_list_lock);
5096
Chris Wilson1637ef42010-04-20 17:10:35 +01005097rescan:
Chris Wilson31169712009-09-14 16:50:28 +01005098 /* first scan for clean buffers */
5099 list_for_each_entry_safe(dev_priv, next_dev,
5100 &shrink_list, mm.shrink_list) {
5101 struct drm_device *dev = dev_priv->dev;
5102
5103 if (! mutex_trylock(&dev->struct_mutex))
5104 continue;
5105
5106 spin_unlock(&shrink_list_lock);
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01005107 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08005108
Chris Wilson31169712009-09-14 16:50:28 +01005109 list_for_each_entry_safe(obj_priv, next_obj,
5110 &dev_priv->mm.inactive_list,
5111 list) {
5112 if (i915_gem_object_is_purgeable(obj_priv)) {
Daniel Vettera8089e82010-04-09 19:05:09 +00005113 i915_gem_object_unbind(&obj_priv->base);
Chris Wilson31169712009-09-14 16:50:28 +01005114 if (--nr_to_scan <= 0)
5115 break;
5116 }
5117 }
5118
5119 spin_lock(&shrink_list_lock);
5120 mutex_unlock(&dev->struct_mutex);
5121
Chris Wilson963b4832009-09-20 23:03:54 +01005122 would_deadlock = 0;
5123
Chris Wilson31169712009-09-14 16:50:28 +01005124 if (nr_to_scan <= 0)
5125 break;
5126 }
5127
5128 /* second pass, evict/count anything still on the inactive list */
5129 list_for_each_entry_safe(dev_priv, next_dev,
5130 &shrink_list, mm.shrink_list) {
5131 struct drm_device *dev = dev_priv->dev;
5132
5133 if (! mutex_trylock(&dev->struct_mutex))
5134 continue;
5135
5136 spin_unlock(&shrink_list_lock);
5137
5138 list_for_each_entry_safe(obj_priv, next_obj,
5139 &dev_priv->mm.inactive_list,
5140 list) {
5141 if (nr_to_scan > 0) {
Daniel Vettera8089e82010-04-09 19:05:09 +00005142 i915_gem_object_unbind(&obj_priv->base);
Chris Wilson31169712009-09-14 16:50:28 +01005143 nr_to_scan--;
5144 } else
5145 cnt++;
5146 }
5147
5148 spin_lock(&shrink_list_lock);
5149 mutex_unlock(&dev->struct_mutex);
5150
5151 would_deadlock = 0;
5152 }
5153
Chris Wilson1637ef42010-04-20 17:10:35 +01005154 if (nr_to_scan) {
5155 int active = 0;
5156
5157 /*
5158 * We are desperate for pages, so as a last resort, wait
5159 * for the GPU to finish and discard whatever we can.
5160 * This has a dramatic impact to reduce the number of
5161 * OOM-killer events whilst running the GPU aggressively.
5162 */
5163 list_for_each_entry(dev_priv, &shrink_list, mm.shrink_list) {
5164 struct drm_device *dev = dev_priv->dev;
5165
5166 if (!mutex_trylock(&dev->struct_mutex))
5167 continue;
5168
5169 spin_unlock(&shrink_list_lock);
5170
5171 if (i915_gpu_is_active(dev)) {
5172 i915_gpu_idle(dev);
5173 active++;
5174 }
5175
5176 spin_lock(&shrink_list_lock);
5177 mutex_unlock(&dev->struct_mutex);
5178 }
5179
5180 if (active)
5181 goto rescan;
5182 }
5183
Chris Wilson31169712009-09-14 16:50:28 +01005184 spin_unlock(&shrink_list_lock);
5185
5186 if (would_deadlock)
5187 return -1;
5188 else if (cnt > 0)
5189 return (cnt / 100) * sysctl_vfs_cache_pressure;
5190 else
5191 return 0;
5192}
5193
5194static struct shrinker shrinker = {
5195 .shrink = i915_gem_shrink,
5196 .seeks = DEFAULT_SEEKS,
5197};
5198
5199__init void
5200i915_gem_shrinker_init(void)
5201{
5202 register_shrinker(&shrinker);
5203}
5204
5205__exit void
5206i915_gem_shrinker_exit(void)
5207{
5208 unregister_shrinker(&shrinker);
5209}