blob: 138ca6ea5ef797779ceb503a919a78565df9fe1a [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);
Eric Anholt673a3942008-07-30 12:06:12 -070056
Chris Wilson31169712009-09-14 16:50:28 +010057static LIST_HEAD(shrink_list);
58static DEFINE_SPINLOCK(shrink_list_lock);
59
Jesse Barnes79e53942008-11-07 14:24:08 -080060int i915_gem_do_init(struct drm_device *dev, unsigned long start,
61 unsigned long end)
62{
63 drm_i915_private_t *dev_priv = dev->dev_private;
64
65 if (start >= end ||
66 (start & (PAGE_SIZE - 1)) != 0 ||
67 (end & (PAGE_SIZE - 1)) != 0) {
68 return -EINVAL;
69 }
70
71 drm_mm_init(&dev_priv->mm.gtt_space, start,
72 end - start);
73
74 dev->gtt_total = (uint32_t) (end - start);
75
76 return 0;
77}
Keith Packard6dbe2772008-10-14 21:41:13 -070078
Eric Anholt673a3942008-07-30 12:06:12 -070079int
80i915_gem_init_ioctl(struct drm_device *dev, void *data,
81 struct drm_file *file_priv)
82{
Eric Anholt673a3942008-07-30 12:06:12 -070083 struct drm_i915_gem_init *args = data;
Jesse Barnes79e53942008-11-07 14:24:08 -080084 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -070085
86 mutex_lock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080087 ret = i915_gem_do_init(dev, args->gtt_start, args->gtt_end);
Eric Anholt673a3942008-07-30 12:06:12 -070088 mutex_unlock(&dev->struct_mutex);
89
Jesse Barnes79e53942008-11-07 14:24:08 -080090 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -070091}
92
Eric Anholt5a125c32008-10-22 21:40:13 -070093int
94i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
95 struct drm_file *file_priv)
96{
Eric Anholt5a125c32008-10-22 21:40:13 -070097 struct drm_i915_gem_get_aperture *args = data;
Eric Anholt5a125c32008-10-22 21:40:13 -070098
99 if (!(dev->driver->driver_features & DRIVER_GEM))
100 return -ENODEV;
101
102 args->aper_size = dev->gtt_total;
Keith Packard2678d9d2008-11-20 22:54:54 -0800103 args->aper_available_size = (args->aper_size -
104 atomic_read(&dev->pin_memory));
Eric Anholt5a125c32008-10-22 21:40:13 -0700105
106 return 0;
107}
108
Eric Anholt673a3942008-07-30 12:06:12 -0700109
110/**
111 * Creates a new mm object and returns a handle to it.
112 */
113int
114i915_gem_create_ioctl(struct drm_device *dev, void *data,
115 struct drm_file *file_priv)
116{
117 struct drm_i915_gem_create *args = data;
118 struct drm_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300119 int ret;
120 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700121
122 args->size = roundup(args->size, PAGE_SIZE);
123
124 /* Allocate the new object */
Daniel Vetterac52bc52010-04-09 19:05:06 +0000125 obj = i915_gem_alloc_object(dev, args->size);
Eric Anholt673a3942008-07-30 12:06:12 -0700126 if (obj == NULL)
127 return -ENOMEM;
128
129 ret = drm_gem_handle_create(file_priv, obj, &handle);
Luca Barbieribc9025b2010-02-09 05:49:12 +0000130 drm_gem_object_handle_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700131
132 if (ret)
133 return ret;
134
135 args->handle = handle;
136
137 return 0;
138}
139
Eric Anholt40123c12009-03-09 13:42:30 -0700140static inline int
Eric Anholteb014592009-03-10 11:44:52 -0700141fast_shmem_read(struct page **pages,
142 loff_t page_base, int page_offset,
143 char __user *data,
144 int length)
145{
146 char __iomem *vaddr;
Florian Mickler2bc43b52009-04-06 22:55:41 +0200147 int unwritten;
Eric Anholteb014592009-03-10 11:44:52 -0700148
149 vaddr = kmap_atomic(pages[page_base >> PAGE_SHIFT], KM_USER0);
150 if (vaddr == NULL)
151 return -ENOMEM;
Florian Mickler2bc43b52009-04-06 22:55:41 +0200152 unwritten = __copy_to_user_inatomic(data, vaddr + page_offset, length);
Eric Anholteb014592009-03-10 11:44:52 -0700153 kunmap_atomic(vaddr, KM_USER0);
154
Florian Mickler2bc43b52009-04-06 22:55:41 +0200155 if (unwritten)
156 return -EFAULT;
157
158 return 0;
Eric Anholteb014592009-03-10 11:44:52 -0700159}
160
Eric Anholt280b7132009-03-12 16:56:27 -0700161static int i915_gem_object_needs_bit17_swizzle(struct drm_gem_object *obj)
162{
163 drm_i915_private_t *dev_priv = obj->dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +0100164 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt280b7132009-03-12 16:56:27 -0700165
166 return dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_9_10_17 &&
167 obj_priv->tiling_mode != I915_TILING_NONE;
168}
169
Chris Wilson99a03df2010-05-27 14:15:34 +0100170static inline void
Eric Anholt40123c12009-03-09 13:42:30 -0700171slow_shmem_copy(struct page *dst_page,
172 int dst_offset,
173 struct page *src_page,
174 int src_offset,
175 int length)
176{
177 char *dst_vaddr, *src_vaddr;
178
Chris Wilson99a03df2010-05-27 14:15:34 +0100179 dst_vaddr = kmap(dst_page);
180 src_vaddr = kmap(src_page);
Eric Anholt40123c12009-03-09 13:42:30 -0700181
182 memcpy(dst_vaddr + dst_offset, src_vaddr + src_offset, length);
183
Chris Wilson99a03df2010-05-27 14:15:34 +0100184 kunmap(src_page);
185 kunmap(dst_page);
Eric Anholt40123c12009-03-09 13:42:30 -0700186}
187
Chris Wilson99a03df2010-05-27 14:15:34 +0100188static inline void
Eric Anholt280b7132009-03-12 16:56:27 -0700189slow_shmem_bit17_copy(struct page *gpu_page,
190 int gpu_offset,
191 struct page *cpu_page,
192 int cpu_offset,
193 int length,
194 int is_read)
195{
196 char *gpu_vaddr, *cpu_vaddr;
197
198 /* Use the unswizzled path if this page isn't affected. */
199 if ((page_to_phys(gpu_page) & (1 << 17)) == 0) {
200 if (is_read)
201 return slow_shmem_copy(cpu_page, cpu_offset,
202 gpu_page, gpu_offset, length);
203 else
204 return slow_shmem_copy(gpu_page, gpu_offset,
205 cpu_page, cpu_offset, length);
206 }
207
Chris Wilson99a03df2010-05-27 14:15:34 +0100208 gpu_vaddr = kmap(gpu_page);
209 cpu_vaddr = kmap(cpu_page);
Eric Anholt280b7132009-03-12 16:56:27 -0700210
211 /* Copy the data, XORing A6 with A17 (1). The user already knows he's
212 * XORing with the other bits (A9 for Y, A9 and A10 for X)
213 */
214 while (length > 0) {
215 int cacheline_end = ALIGN(gpu_offset + 1, 64);
216 int this_length = min(cacheline_end - gpu_offset, length);
217 int swizzled_gpu_offset = gpu_offset ^ 64;
218
219 if (is_read) {
220 memcpy(cpu_vaddr + cpu_offset,
221 gpu_vaddr + swizzled_gpu_offset,
222 this_length);
223 } else {
224 memcpy(gpu_vaddr + swizzled_gpu_offset,
225 cpu_vaddr + cpu_offset,
226 this_length);
227 }
228 cpu_offset += this_length;
229 gpu_offset += this_length;
230 length -= this_length;
231 }
232
Chris Wilson99a03df2010-05-27 14:15:34 +0100233 kunmap(cpu_page);
234 kunmap(gpu_page);
Eric Anholt280b7132009-03-12 16:56:27 -0700235}
236
Eric Anholt673a3942008-07-30 12:06:12 -0700237/**
Eric Anholteb014592009-03-10 11:44:52 -0700238 * This is the fast shmem pread path, which attempts to copy_from_user directly
239 * from the backing pages of the object to the user's address space. On a
240 * fault, it fails so we can fall back to i915_gem_shmem_pwrite_slow().
241 */
242static int
243i915_gem_shmem_pread_fast(struct drm_device *dev, struct drm_gem_object *obj,
244 struct drm_i915_gem_pread *args,
245 struct drm_file *file_priv)
246{
Daniel Vetter23010e42010-03-08 13:35:02 +0100247 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700248 ssize_t remain;
249 loff_t offset, page_base;
250 char __user *user_data;
251 int page_offset, page_length;
252 int ret;
253
254 user_data = (char __user *) (uintptr_t) args->data_ptr;
255 remain = args->size;
256
257 mutex_lock(&dev->struct_mutex);
258
Chris Wilson4bdadb92010-01-27 13:36:32 +0000259 ret = i915_gem_object_get_pages(obj, 0);
Eric Anholteb014592009-03-10 11:44:52 -0700260 if (ret != 0)
261 goto fail_unlock;
262
263 ret = i915_gem_object_set_cpu_read_domain_range(obj, args->offset,
264 args->size);
265 if (ret != 0)
266 goto fail_put_pages;
267
Daniel Vetter23010e42010-03-08 13:35:02 +0100268 obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700269 offset = args->offset;
270
271 while (remain > 0) {
272 /* Operation in this page
273 *
274 * page_base = page offset within aperture
275 * page_offset = offset within page
276 * page_length = bytes to copy for this page
277 */
278 page_base = (offset & ~(PAGE_SIZE-1));
279 page_offset = offset & (PAGE_SIZE-1);
280 page_length = remain;
281 if ((page_offset + remain) > PAGE_SIZE)
282 page_length = PAGE_SIZE - page_offset;
283
284 ret = fast_shmem_read(obj_priv->pages,
285 page_base, page_offset,
286 user_data, page_length);
287 if (ret)
288 goto fail_put_pages;
289
290 remain -= page_length;
291 user_data += page_length;
292 offset += page_length;
293 }
294
295fail_put_pages:
296 i915_gem_object_put_pages(obj);
297fail_unlock:
298 mutex_unlock(&dev->struct_mutex);
299
300 return ret;
301}
302
Chris Wilson07f73f62009-09-14 16:50:30 +0100303static int
304i915_gem_object_get_pages_or_evict(struct drm_gem_object *obj)
305{
306 int ret;
307
Chris Wilson4bdadb92010-01-27 13:36:32 +0000308 ret = i915_gem_object_get_pages(obj, __GFP_NORETRY | __GFP_NOWARN);
Chris Wilson07f73f62009-09-14 16:50:30 +0100309
310 /* If we've insufficient memory to map in the pages, attempt
311 * to make some space by throwing out some old buffers.
312 */
313 if (ret == -ENOMEM) {
314 struct drm_device *dev = obj->dev;
Chris Wilson07f73f62009-09-14 16:50:30 +0100315
316 ret = i915_gem_evict_something(dev, obj->size);
317 if (ret)
318 return ret;
319
Chris Wilson4bdadb92010-01-27 13:36:32 +0000320 ret = i915_gem_object_get_pages(obj, 0);
Chris Wilson07f73f62009-09-14 16:50:30 +0100321 }
322
323 return ret;
324}
325
Eric Anholteb014592009-03-10 11:44:52 -0700326/**
327 * This is the fallback shmem pread path, which allocates temporary storage
328 * in kernel space to copy_to_user into outside of the struct_mutex, so we
329 * can copy out of the object's backing pages while holding the struct mutex
330 * and not take page faults.
331 */
332static int
333i915_gem_shmem_pread_slow(struct drm_device *dev, struct drm_gem_object *obj,
334 struct drm_i915_gem_pread *args,
335 struct drm_file *file_priv)
336{
Daniel Vetter23010e42010-03-08 13:35:02 +0100337 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700338 struct mm_struct *mm = current->mm;
339 struct page **user_pages;
340 ssize_t remain;
341 loff_t offset, pinned_pages, i;
342 loff_t first_data_page, last_data_page, num_pages;
343 int shmem_page_index, shmem_page_offset;
344 int data_page_index, data_page_offset;
345 int page_length;
346 int ret;
347 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700348 int do_bit17_swizzling;
Eric Anholteb014592009-03-10 11:44:52 -0700349
350 remain = args->size;
351
352 /* Pin the user pages containing the data. We can't fault while
353 * holding the struct mutex, yet we want to hold it while
354 * dereferencing the user data.
355 */
356 first_data_page = data_ptr / PAGE_SIZE;
357 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
358 num_pages = last_data_page - first_data_page + 1;
359
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700360 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
Eric Anholteb014592009-03-10 11:44:52 -0700361 if (user_pages == NULL)
362 return -ENOMEM;
363
364 down_read(&mm->mmap_sem);
365 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
Eric Anholte5e9ecd2009-04-07 16:01:22 -0700366 num_pages, 1, 0, user_pages, NULL);
Eric Anholteb014592009-03-10 11:44:52 -0700367 up_read(&mm->mmap_sem);
368 if (pinned_pages < num_pages) {
369 ret = -EFAULT;
370 goto fail_put_user_pages;
371 }
372
Eric Anholt280b7132009-03-12 16:56:27 -0700373 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
374
Eric Anholteb014592009-03-10 11:44:52 -0700375 mutex_lock(&dev->struct_mutex);
376
Chris Wilson07f73f62009-09-14 16:50:30 +0100377 ret = i915_gem_object_get_pages_or_evict(obj);
378 if (ret)
Eric Anholteb014592009-03-10 11:44:52 -0700379 goto fail_unlock;
380
381 ret = i915_gem_object_set_cpu_read_domain_range(obj, args->offset,
382 args->size);
383 if (ret != 0)
384 goto fail_put_pages;
385
Daniel Vetter23010e42010-03-08 13:35:02 +0100386 obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700387 offset = args->offset;
388
389 while (remain > 0) {
390 /* Operation in this page
391 *
392 * shmem_page_index = page number within shmem file
393 * shmem_page_offset = offset within page in shmem file
394 * data_page_index = page number in get_user_pages return
395 * data_page_offset = offset with data_page_index page.
396 * page_length = bytes to copy for this page
397 */
398 shmem_page_index = offset / PAGE_SIZE;
399 shmem_page_offset = offset & ~PAGE_MASK;
400 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
401 data_page_offset = data_ptr & ~PAGE_MASK;
402
403 page_length = remain;
404 if ((shmem_page_offset + page_length) > PAGE_SIZE)
405 page_length = PAGE_SIZE - shmem_page_offset;
406 if ((data_page_offset + page_length) > PAGE_SIZE)
407 page_length = PAGE_SIZE - data_page_offset;
408
Eric Anholt280b7132009-03-12 16:56:27 -0700409 if (do_bit17_swizzling) {
Chris Wilson99a03df2010-05-27 14:15:34 +0100410 slow_shmem_bit17_copy(obj_priv->pages[shmem_page_index],
Eric Anholt280b7132009-03-12 16:56:27 -0700411 shmem_page_offset,
Chris Wilson99a03df2010-05-27 14:15:34 +0100412 user_pages[data_page_index],
413 data_page_offset,
414 page_length,
415 1);
416 } else {
417 slow_shmem_copy(user_pages[data_page_index],
418 data_page_offset,
419 obj_priv->pages[shmem_page_index],
420 shmem_page_offset,
421 page_length);
Eric Anholt280b7132009-03-12 16:56:27 -0700422 }
Eric Anholteb014592009-03-10 11:44:52 -0700423
424 remain -= page_length;
425 data_ptr += page_length;
426 offset += page_length;
427 }
428
429fail_put_pages:
430 i915_gem_object_put_pages(obj);
431fail_unlock:
432 mutex_unlock(&dev->struct_mutex);
433fail_put_user_pages:
434 for (i = 0; i < pinned_pages; i++) {
435 SetPageDirty(user_pages[i]);
436 page_cache_release(user_pages[i]);
437 }
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700438 drm_free_large(user_pages);
Eric Anholteb014592009-03-10 11:44:52 -0700439
440 return ret;
441}
442
Eric Anholt673a3942008-07-30 12:06:12 -0700443/**
444 * Reads data from the object referenced by handle.
445 *
446 * On error, the contents of *data are undefined.
447 */
448int
449i915_gem_pread_ioctl(struct drm_device *dev, void *data,
450 struct drm_file *file_priv)
451{
452 struct drm_i915_gem_pread *args = data;
453 struct drm_gem_object *obj;
454 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -0700455 int ret;
456
457 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
458 if (obj == NULL)
459 return -EBADF;
Daniel Vetter23010e42010-03-08 13:35:02 +0100460 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700461
462 /* Bounds check source.
463 *
464 * XXX: This could use review for overflow issues...
465 */
466 if (args->offset > obj->size || args->size > obj->size ||
467 args->offset + args->size > obj->size) {
Luca Barbieribc9025b2010-02-09 05:49:12 +0000468 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700469 return -EINVAL;
470 }
471
Eric Anholt280b7132009-03-12 16:56:27 -0700472 if (i915_gem_object_needs_bit17_swizzle(obj)) {
Eric Anholteb014592009-03-10 11:44:52 -0700473 ret = i915_gem_shmem_pread_slow(dev, obj, args, file_priv);
Eric Anholt280b7132009-03-12 16:56:27 -0700474 } else {
475 ret = i915_gem_shmem_pread_fast(dev, obj, args, file_priv);
476 if (ret != 0)
477 ret = i915_gem_shmem_pread_slow(dev, obj, args,
478 file_priv);
479 }
Eric Anholt673a3942008-07-30 12:06:12 -0700480
Luca Barbieribc9025b2010-02-09 05:49:12 +0000481 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700482
Eric Anholteb014592009-03-10 11:44:52 -0700483 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700484}
485
Keith Packard0839ccb2008-10-30 19:38:48 -0700486/* This is the fast write path which cannot handle
487 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700488 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700489
Keith Packard0839ccb2008-10-30 19:38:48 -0700490static inline int
491fast_user_write(struct io_mapping *mapping,
492 loff_t page_base, int page_offset,
493 char __user *user_data,
494 int length)
495{
496 char *vaddr_atomic;
497 unsigned long unwritten;
498
499 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
500 unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
501 user_data, length);
502 io_mapping_unmap_atomic(vaddr_atomic);
503 if (unwritten)
504 return -EFAULT;
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700505 return 0;
Keith Packard0839ccb2008-10-30 19:38:48 -0700506}
507
508/* Here's the write path which can sleep for
509 * page faults
510 */
511
Chris Wilsonab34c222010-05-27 14:15:35 +0100512static inline void
Eric Anholt3de09aa2009-03-09 09:42:23 -0700513slow_kernel_write(struct io_mapping *mapping,
514 loff_t gtt_base, int gtt_offset,
515 struct page *user_page, int user_offset,
516 int length)
Keith Packard0839ccb2008-10-30 19:38:48 -0700517{
Chris Wilsonab34c222010-05-27 14:15:35 +0100518 char __iomem *dst_vaddr;
519 char *src_vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -0700520
Chris Wilsonab34c222010-05-27 14:15:35 +0100521 dst_vaddr = io_mapping_map_wc(mapping, gtt_base);
522 src_vaddr = kmap(user_page);
523
524 memcpy_toio(dst_vaddr + gtt_offset,
525 src_vaddr + user_offset,
526 length);
527
528 kunmap(user_page);
529 io_mapping_unmap(dst_vaddr);
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700530}
531
Eric Anholt40123c12009-03-09 13:42:30 -0700532static inline int
533fast_shmem_write(struct page **pages,
534 loff_t page_base, int page_offset,
535 char __user *data,
536 int length)
537{
538 char __iomem *vaddr;
Dave Airlied0088772009-03-28 20:29:48 -0400539 unsigned long unwritten;
Eric Anholt40123c12009-03-09 13:42:30 -0700540
541 vaddr = kmap_atomic(pages[page_base >> PAGE_SHIFT], KM_USER0);
542 if (vaddr == NULL)
543 return -ENOMEM;
Dave Airlied0088772009-03-28 20:29:48 -0400544 unwritten = __copy_from_user_inatomic(vaddr + page_offset, data, length);
Eric Anholt40123c12009-03-09 13:42:30 -0700545 kunmap_atomic(vaddr, KM_USER0);
546
Dave Airlied0088772009-03-28 20:29:48 -0400547 if (unwritten)
548 return -EFAULT;
Eric Anholt40123c12009-03-09 13:42:30 -0700549 return 0;
550}
551
Eric Anholt3de09aa2009-03-09 09:42:23 -0700552/**
553 * This is the fast pwrite path, where we copy the data directly from the
554 * user into the GTT, uncached.
555 */
Eric Anholt673a3942008-07-30 12:06:12 -0700556static int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700557i915_gem_gtt_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
558 struct drm_i915_gem_pwrite *args,
559 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700560{
Daniel Vetter23010e42010-03-08 13:35:02 +0100561 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Keith Packard0839ccb2008-10-30 19:38:48 -0700562 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700563 ssize_t remain;
Keith Packard0839ccb2008-10-30 19:38:48 -0700564 loff_t offset, page_base;
Eric Anholt673a3942008-07-30 12:06:12 -0700565 char __user *user_data;
Keith Packard0839ccb2008-10-30 19:38:48 -0700566 int page_offset, page_length;
567 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700568
569 user_data = (char __user *) (uintptr_t) args->data_ptr;
570 remain = args->size;
571 if (!access_ok(VERIFY_READ, user_data, remain))
572 return -EFAULT;
573
574
575 mutex_lock(&dev->struct_mutex);
576 ret = i915_gem_object_pin(obj, 0);
577 if (ret) {
578 mutex_unlock(&dev->struct_mutex);
579 return ret;
580 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800581 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
Eric Anholt673a3942008-07-30 12:06:12 -0700582 if (ret)
583 goto fail;
584
Daniel Vetter23010e42010-03-08 13:35:02 +0100585 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700586 offset = obj_priv->gtt_offset + args->offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700587
588 while (remain > 0) {
589 /* Operation in this page
590 *
Keith Packard0839ccb2008-10-30 19:38:48 -0700591 * page_base = page offset within aperture
592 * page_offset = offset within page
593 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -0700594 */
Keith Packard0839ccb2008-10-30 19:38:48 -0700595 page_base = (offset & ~(PAGE_SIZE-1));
596 page_offset = offset & (PAGE_SIZE-1);
597 page_length = remain;
598 if ((page_offset + remain) > PAGE_SIZE)
599 page_length = PAGE_SIZE - page_offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700600
Keith Packard0839ccb2008-10-30 19:38:48 -0700601 ret = fast_user_write (dev_priv->mm.gtt_mapping, page_base,
602 page_offset, user_data, page_length);
Eric Anholt673a3942008-07-30 12:06:12 -0700603
Keith Packard0839ccb2008-10-30 19:38:48 -0700604 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -0700605 * source page isn't available. Return the error and we'll
606 * retry in the slow path.
Keith Packard0839ccb2008-10-30 19:38:48 -0700607 */
Eric Anholt3de09aa2009-03-09 09:42:23 -0700608 if (ret)
609 goto fail;
Eric Anholt673a3942008-07-30 12:06:12 -0700610
Keith Packard0839ccb2008-10-30 19:38:48 -0700611 remain -= page_length;
612 user_data += page_length;
613 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700614 }
Eric Anholt673a3942008-07-30 12:06:12 -0700615
616fail:
617 i915_gem_object_unpin(obj);
618 mutex_unlock(&dev->struct_mutex);
619
620 return ret;
621}
622
Eric Anholt3de09aa2009-03-09 09:42:23 -0700623/**
624 * This is the fallback GTT pwrite path, which uses get_user_pages to pin
625 * the memory and maps it using kmap_atomic for copying.
626 *
627 * This code resulted in x11perf -rgb10text consuming about 10% more CPU
628 * than using i915_gem_gtt_pwrite_fast on a G45 (32-bit).
629 */
Eric Anholt3043c602008-10-02 12:24:47 -0700630static int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700631i915_gem_gtt_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
632 struct drm_i915_gem_pwrite *args,
633 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700634{
Daniel Vetter23010e42010-03-08 13:35:02 +0100635 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700636 drm_i915_private_t *dev_priv = dev->dev_private;
637 ssize_t remain;
638 loff_t gtt_page_base, offset;
639 loff_t first_data_page, last_data_page, num_pages;
640 loff_t pinned_pages, i;
641 struct page **user_pages;
642 struct mm_struct *mm = current->mm;
643 int gtt_page_offset, data_page_offset, data_page_index, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700644 int ret;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700645 uint64_t data_ptr = args->data_ptr;
646
647 remain = args->size;
648
649 /* Pin the user pages containing the data. We can't fault while
650 * holding the struct mutex, and all of the pwrite implementations
651 * want to hold it while dereferencing the user data.
652 */
653 first_data_page = data_ptr / PAGE_SIZE;
654 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
655 num_pages = last_data_page - first_data_page + 1;
656
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700657 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
Eric Anholt3de09aa2009-03-09 09:42:23 -0700658 if (user_pages == NULL)
659 return -ENOMEM;
660
661 down_read(&mm->mmap_sem);
662 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
663 num_pages, 0, 0, user_pages, NULL);
664 up_read(&mm->mmap_sem);
665 if (pinned_pages < num_pages) {
666 ret = -EFAULT;
667 goto out_unpin_pages;
668 }
669
670 mutex_lock(&dev->struct_mutex);
671 ret = i915_gem_object_pin(obj, 0);
672 if (ret)
673 goto out_unlock;
674
675 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
676 if (ret)
677 goto out_unpin_object;
678
Daniel Vetter23010e42010-03-08 13:35:02 +0100679 obj_priv = to_intel_bo(obj);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700680 offset = obj_priv->gtt_offset + args->offset;
681
682 while (remain > 0) {
683 /* Operation in this page
684 *
685 * gtt_page_base = page offset within aperture
686 * gtt_page_offset = offset within page in aperture
687 * data_page_index = page number in get_user_pages return
688 * data_page_offset = offset with data_page_index page.
689 * page_length = bytes to copy for this page
690 */
691 gtt_page_base = offset & PAGE_MASK;
692 gtt_page_offset = offset & ~PAGE_MASK;
693 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
694 data_page_offset = data_ptr & ~PAGE_MASK;
695
696 page_length = remain;
697 if ((gtt_page_offset + page_length) > PAGE_SIZE)
698 page_length = PAGE_SIZE - gtt_page_offset;
699 if ((data_page_offset + page_length) > PAGE_SIZE)
700 page_length = PAGE_SIZE - data_page_offset;
701
Chris Wilsonab34c222010-05-27 14:15:35 +0100702 slow_kernel_write(dev_priv->mm.gtt_mapping,
703 gtt_page_base, gtt_page_offset,
704 user_pages[data_page_index],
705 data_page_offset,
706 page_length);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700707
708 remain -= page_length;
709 offset += page_length;
710 data_ptr += page_length;
711 }
712
713out_unpin_object:
714 i915_gem_object_unpin(obj);
715out_unlock:
716 mutex_unlock(&dev->struct_mutex);
717out_unpin_pages:
718 for (i = 0; i < pinned_pages; i++)
719 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700720 drm_free_large(user_pages);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700721
722 return ret;
723}
724
Eric Anholt40123c12009-03-09 13:42:30 -0700725/**
726 * This is the fast shmem pwrite path, which attempts to directly
727 * copy_from_user into the kmapped pages backing the object.
728 */
Eric Anholt673a3942008-07-30 12:06:12 -0700729static int
Eric Anholt40123c12009-03-09 13:42:30 -0700730i915_gem_shmem_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
731 struct drm_i915_gem_pwrite *args,
732 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700733{
Daniel Vetter23010e42010-03-08 13:35:02 +0100734 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700735 ssize_t remain;
736 loff_t offset, page_base;
737 char __user *user_data;
738 int page_offset, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700739 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -0700740
741 user_data = (char __user *) (uintptr_t) args->data_ptr;
742 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700743
744 mutex_lock(&dev->struct_mutex);
745
Chris Wilson4bdadb92010-01-27 13:36:32 +0000746 ret = i915_gem_object_get_pages(obj, 0);
Eric Anholt40123c12009-03-09 13:42:30 -0700747 if (ret != 0)
748 goto fail_unlock;
749
Eric Anholte47c68e2008-11-14 13:35:19 -0800750 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Eric Anholt40123c12009-03-09 13:42:30 -0700751 if (ret != 0)
752 goto fail_put_pages;
Eric Anholt673a3942008-07-30 12:06:12 -0700753
Daniel Vetter23010e42010-03-08 13:35:02 +0100754 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700755 offset = args->offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700756 obj_priv->dirty = 1;
Eric Anholt673a3942008-07-30 12:06:12 -0700757
Eric Anholt40123c12009-03-09 13:42:30 -0700758 while (remain > 0) {
759 /* Operation in this page
760 *
761 * page_base = page offset within aperture
762 * page_offset = offset within page
763 * page_length = bytes to copy for this page
764 */
765 page_base = (offset & ~(PAGE_SIZE-1));
766 page_offset = offset & (PAGE_SIZE-1);
767 page_length = remain;
768 if ((page_offset + remain) > PAGE_SIZE)
769 page_length = PAGE_SIZE - page_offset;
770
771 ret = fast_shmem_write(obj_priv->pages,
772 page_base, page_offset,
773 user_data, page_length);
774 if (ret)
775 goto fail_put_pages;
776
777 remain -= page_length;
778 user_data += page_length;
779 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700780 }
781
Eric Anholt40123c12009-03-09 13:42:30 -0700782fail_put_pages:
783 i915_gem_object_put_pages(obj);
784fail_unlock:
Eric Anholt673a3942008-07-30 12:06:12 -0700785 mutex_unlock(&dev->struct_mutex);
786
Eric Anholt40123c12009-03-09 13:42:30 -0700787 return ret;
788}
789
790/**
791 * This is the fallback shmem pwrite path, which uses get_user_pages to pin
792 * the memory and maps it using kmap_atomic for copying.
793 *
794 * This avoids taking mmap_sem for faulting on the user's address while the
795 * struct_mutex is held.
796 */
797static int
798i915_gem_shmem_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
799 struct drm_i915_gem_pwrite *args,
800 struct drm_file *file_priv)
801{
Daniel Vetter23010e42010-03-08 13:35:02 +0100802 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700803 struct mm_struct *mm = current->mm;
804 struct page **user_pages;
805 ssize_t remain;
806 loff_t offset, pinned_pages, i;
807 loff_t first_data_page, last_data_page, num_pages;
808 int shmem_page_index, shmem_page_offset;
809 int data_page_index, data_page_offset;
810 int page_length;
811 int ret;
812 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700813 int do_bit17_swizzling;
Eric Anholt40123c12009-03-09 13:42:30 -0700814
815 remain = args->size;
816
817 /* Pin the user pages containing the data. We can't fault while
818 * holding the struct mutex, and all of the pwrite implementations
819 * want to hold it while dereferencing the user data.
820 */
821 first_data_page = data_ptr / PAGE_SIZE;
822 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
823 num_pages = last_data_page - first_data_page + 1;
824
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700825 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
Eric Anholt40123c12009-03-09 13:42:30 -0700826 if (user_pages == NULL)
827 return -ENOMEM;
828
829 down_read(&mm->mmap_sem);
830 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
831 num_pages, 0, 0, user_pages, NULL);
832 up_read(&mm->mmap_sem);
833 if (pinned_pages < num_pages) {
834 ret = -EFAULT;
835 goto fail_put_user_pages;
836 }
837
Eric Anholt280b7132009-03-12 16:56:27 -0700838 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
839
Eric Anholt40123c12009-03-09 13:42:30 -0700840 mutex_lock(&dev->struct_mutex);
841
Chris Wilson07f73f62009-09-14 16:50:30 +0100842 ret = i915_gem_object_get_pages_or_evict(obj);
843 if (ret)
Eric Anholt40123c12009-03-09 13:42:30 -0700844 goto fail_unlock;
845
846 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
847 if (ret != 0)
848 goto fail_put_pages;
849
Daniel Vetter23010e42010-03-08 13:35:02 +0100850 obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700851 offset = args->offset;
852 obj_priv->dirty = 1;
853
854 while (remain > 0) {
855 /* Operation in this page
856 *
857 * shmem_page_index = page number within shmem file
858 * shmem_page_offset = offset within page in shmem file
859 * data_page_index = page number in get_user_pages return
860 * data_page_offset = offset with data_page_index page.
861 * page_length = bytes to copy for this page
862 */
863 shmem_page_index = offset / PAGE_SIZE;
864 shmem_page_offset = offset & ~PAGE_MASK;
865 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
866 data_page_offset = data_ptr & ~PAGE_MASK;
867
868 page_length = remain;
869 if ((shmem_page_offset + page_length) > PAGE_SIZE)
870 page_length = PAGE_SIZE - shmem_page_offset;
871 if ((data_page_offset + page_length) > PAGE_SIZE)
872 page_length = PAGE_SIZE - data_page_offset;
873
Eric Anholt280b7132009-03-12 16:56:27 -0700874 if (do_bit17_swizzling) {
Chris Wilson99a03df2010-05-27 14:15:34 +0100875 slow_shmem_bit17_copy(obj_priv->pages[shmem_page_index],
Eric Anholt280b7132009-03-12 16:56:27 -0700876 shmem_page_offset,
877 user_pages[data_page_index],
878 data_page_offset,
Chris Wilson99a03df2010-05-27 14:15:34 +0100879 page_length,
880 0);
881 } else {
882 slow_shmem_copy(obj_priv->pages[shmem_page_index],
883 shmem_page_offset,
884 user_pages[data_page_index],
885 data_page_offset,
886 page_length);
Eric Anholt280b7132009-03-12 16:56:27 -0700887 }
Eric Anholt40123c12009-03-09 13:42:30 -0700888
889 remain -= page_length;
890 data_ptr += page_length;
891 offset += page_length;
892 }
893
894fail_put_pages:
895 i915_gem_object_put_pages(obj);
896fail_unlock:
897 mutex_unlock(&dev->struct_mutex);
898fail_put_user_pages:
899 for (i = 0; i < pinned_pages; i++)
900 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700901 drm_free_large(user_pages);
Eric Anholt40123c12009-03-09 13:42:30 -0700902
903 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700904}
905
906/**
907 * Writes data to the object referenced by handle.
908 *
909 * On error, the contents of the buffer that were to be modified are undefined.
910 */
911int
912i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
913 struct drm_file *file_priv)
914{
915 struct drm_i915_gem_pwrite *args = data;
916 struct drm_gem_object *obj;
917 struct drm_i915_gem_object *obj_priv;
918 int ret = 0;
919
920 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
921 if (obj == NULL)
922 return -EBADF;
Daniel Vetter23010e42010-03-08 13:35:02 +0100923 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700924
925 /* Bounds check destination.
926 *
927 * XXX: This could use review for overflow issues...
928 */
929 if (args->offset > obj->size || args->size > obj->size ||
930 args->offset + args->size > obj->size) {
Luca Barbieribc9025b2010-02-09 05:49:12 +0000931 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700932 return -EINVAL;
933 }
934
935 /* We can only do the GTT pwrite on untiled buffers, as otherwise
936 * it would end up going through the fenced access, and we'll get
937 * different detiling behavior between reading and writing.
938 * pread/pwrite currently are reading and writing from the CPU
939 * perspective, requiring manual detiling by the client.
940 */
Dave Airlie71acb5e2008-12-30 20:31:46 +1000941 if (obj_priv->phys_obj)
942 ret = i915_gem_phys_pwrite(dev, obj, args, file_priv);
943 else if (obj_priv->tiling_mode == I915_TILING_NONE &&
Chris Wilson9b8c4a02010-05-27 14:21:01 +0100944 dev->gtt_total != 0 &&
945 obj->write_domain != I915_GEM_DOMAIN_CPU) {
Eric Anholt3de09aa2009-03-09 09:42:23 -0700946 ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file_priv);
947 if (ret == -EFAULT) {
948 ret = i915_gem_gtt_pwrite_slow(dev, obj, args,
949 file_priv);
950 }
Eric Anholt280b7132009-03-12 16:56:27 -0700951 } else if (i915_gem_object_needs_bit17_swizzle(obj)) {
952 ret = i915_gem_shmem_pwrite_slow(dev, obj, args, file_priv);
Eric Anholt40123c12009-03-09 13:42:30 -0700953 } else {
954 ret = i915_gem_shmem_pwrite_fast(dev, obj, args, file_priv);
955 if (ret == -EFAULT) {
956 ret = i915_gem_shmem_pwrite_slow(dev, obj, args,
957 file_priv);
958 }
959 }
Eric Anholt673a3942008-07-30 12:06:12 -0700960
961#if WATCH_PWRITE
962 if (ret)
963 DRM_INFO("pwrite failed %d\n", ret);
964#endif
965
Luca Barbieribc9025b2010-02-09 05:49:12 +0000966 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700967
968 return ret;
969}
970
971/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800972 * Called when user space prepares to use an object with the CPU, either
973 * through the mmap ioctl's mapping or a GTT mapping.
Eric Anholt673a3942008-07-30 12:06:12 -0700974 */
975int
976i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
977 struct drm_file *file_priv)
978{
Eric Anholta09ba7f2009-08-29 12:49:51 -0700979 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700980 struct drm_i915_gem_set_domain *args = data;
981 struct drm_gem_object *obj;
Jesse Barnes652c3932009-08-17 13:31:43 -0700982 struct drm_i915_gem_object *obj_priv;
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800983 uint32_t read_domains = args->read_domains;
984 uint32_t write_domain = args->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -0700985 int ret;
986
987 if (!(dev->driver->driver_features & DRIVER_GEM))
988 return -ENODEV;
989
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800990 /* Only handle setting domains to types used by the CPU. */
Chris Wilson21d509e2009-06-06 09:46:02 +0100991 if (write_domain & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800992 return -EINVAL;
993
Chris Wilson21d509e2009-06-06 09:46:02 +0100994 if (read_domains & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800995 return -EINVAL;
996
997 /* Having something in the write domain implies it's in the read
998 * domain, and only that read domain. Enforce that in the request.
999 */
1000 if (write_domain != 0 && read_domains != write_domain)
1001 return -EINVAL;
1002
Eric Anholt673a3942008-07-30 12:06:12 -07001003 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1004 if (obj == NULL)
1005 return -EBADF;
Daniel Vetter23010e42010-03-08 13:35:02 +01001006 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001007
1008 mutex_lock(&dev->struct_mutex);
Jesse Barnes652c3932009-08-17 13:31:43 -07001009
1010 intel_mark_busy(dev, obj);
1011
Eric Anholt673a3942008-07-30 12:06:12 -07001012#if WATCH_BUF
Krzysztof Halasacfd43c02009-06-20 00:31:28 +02001013 DRM_INFO("set_domain_ioctl %p(%zd), %08x %08x\n",
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001014 obj, obj->size, read_domains, write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07001015#endif
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001016 if (read_domains & I915_GEM_DOMAIN_GTT) {
1017 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
Eric Anholt02354392008-11-26 13:58:13 -08001018
Eric Anholta09ba7f2009-08-29 12:49:51 -07001019 /* Update the LRU on the fence for the CPU access that's
1020 * about to occur.
1021 */
1022 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001023 struct drm_i915_fence_reg *reg =
1024 &dev_priv->fence_regs[obj_priv->fence_reg];
1025 list_move_tail(&reg->lru_list,
Eric Anholta09ba7f2009-08-29 12:49:51 -07001026 &dev_priv->mm.fence_list);
1027 }
1028
Eric Anholt02354392008-11-26 13:58:13 -08001029 /* Silently promote "you're not bound, there was nothing to do"
1030 * to success, since the client was just asking us to
1031 * make sure everything was done.
1032 */
1033 if (ret == -EINVAL)
1034 ret = 0;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001035 } else {
Eric Anholte47c68e2008-11-14 13:35:19 -08001036 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001037 }
1038
Eric Anholt673a3942008-07-30 12:06:12 -07001039 drm_gem_object_unreference(obj);
1040 mutex_unlock(&dev->struct_mutex);
1041 return ret;
1042}
1043
1044/**
1045 * Called when user space has done writes to this buffer
1046 */
1047int
1048i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
1049 struct drm_file *file_priv)
1050{
1051 struct drm_i915_gem_sw_finish *args = data;
1052 struct drm_gem_object *obj;
1053 struct drm_i915_gem_object *obj_priv;
1054 int ret = 0;
1055
1056 if (!(dev->driver->driver_features & DRIVER_GEM))
1057 return -ENODEV;
1058
1059 mutex_lock(&dev->struct_mutex);
1060 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1061 if (obj == NULL) {
1062 mutex_unlock(&dev->struct_mutex);
1063 return -EBADF;
1064 }
1065
1066#if WATCH_BUF
Krzysztof Halasacfd43c02009-06-20 00:31:28 +02001067 DRM_INFO("%s: sw_finish %d (%p %zd)\n",
Eric Anholt673a3942008-07-30 12:06:12 -07001068 __func__, args->handle, obj, obj->size);
1069#endif
Daniel Vetter23010e42010-03-08 13:35:02 +01001070 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001071
1072 /* Pinned buffers may be scanout, so flush the cache */
Eric Anholte47c68e2008-11-14 13:35:19 -08001073 if (obj_priv->pin_count)
1074 i915_gem_object_flush_cpu_write_domain(obj);
1075
Eric Anholt673a3942008-07-30 12:06:12 -07001076 drm_gem_object_unreference(obj);
1077 mutex_unlock(&dev->struct_mutex);
1078 return ret;
1079}
1080
1081/**
1082 * Maps the contents of an object, returning the address it is mapped
1083 * into.
1084 *
1085 * While the mapping holds a reference on the contents of the object, it doesn't
1086 * imply a ref on the object itself.
1087 */
1088int
1089i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
1090 struct drm_file *file_priv)
1091{
1092 struct drm_i915_gem_mmap *args = data;
1093 struct drm_gem_object *obj;
1094 loff_t offset;
1095 unsigned long addr;
1096
1097 if (!(dev->driver->driver_features & DRIVER_GEM))
1098 return -ENODEV;
1099
1100 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1101 if (obj == NULL)
1102 return -EBADF;
1103
1104 offset = args->offset;
1105
1106 down_write(&current->mm->mmap_sem);
1107 addr = do_mmap(obj->filp, 0, args->size,
1108 PROT_READ | PROT_WRITE, MAP_SHARED,
1109 args->offset);
1110 up_write(&current->mm->mmap_sem);
Luca Barbieribc9025b2010-02-09 05:49:12 +00001111 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001112 if (IS_ERR((void *)addr))
1113 return addr;
1114
1115 args->addr_ptr = (uint64_t) addr;
1116
1117 return 0;
1118}
1119
Jesse Barnesde151cf2008-11-12 10:03:55 -08001120/**
1121 * i915_gem_fault - fault a page into the GTT
1122 * vma: VMA in question
1123 * vmf: fault info
1124 *
1125 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1126 * from userspace. The fault handler takes care of binding the object to
1127 * the GTT (if needed), allocating and programming a fence register (again,
1128 * only if needed based on whether the old reg is still valid or the object
1129 * is tiled) and inserting a new PTE into the faulting process.
1130 *
1131 * Note that the faulting process may involve evicting existing objects
1132 * from the GTT and/or fence registers to make room. So performance may
1133 * suffer if the GTT working set is large or there are few fence registers
1134 * left.
1135 */
1136int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1137{
1138 struct drm_gem_object *obj = vma->vm_private_data;
1139 struct drm_device *dev = obj->dev;
1140 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001141 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001142 pgoff_t page_offset;
1143 unsigned long pfn;
1144 int ret = 0;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001145 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001146
1147 /* We don't use vmf->pgoff since that has the fake offset */
1148 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1149 PAGE_SHIFT;
1150
1151 /* Now bind it into the GTT if needed */
1152 mutex_lock(&dev->struct_mutex);
1153 if (!obj_priv->gtt_space) {
Chris Wilsone67b8ce2009-09-14 16:50:26 +01001154 ret = i915_gem_object_bind_to_gtt(obj, 0);
Chris Wilsonc7150892009-09-23 00:43:56 +01001155 if (ret)
1156 goto unlock;
Kristian Høgsberg07f4f3e2009-05-27 14:37:28 -04001157
Jesse Barnes14b60392009-05-20 16:47:08 -04001158 list_add_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001159
1160 ret = i915_gem_object_set_to_gtt_domain(obj, write);
Chris Wilsonc7150892009-09-23 00:43:56 +01001161 if (ret)
1162 goto unlock;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001163 }
1164
1165 /* Need a new fence register? */
Eric Anholta09ba7f2009-08-29 12:49:51 -07001166 if (obj_priv->tiling_mode != I915_TILING_NONE) {
Chris Wilson8c4b8c32009-06-17 22:08:52 +01001167 ret = i915_gem_object_get_fence_reg(obj);
Chris Wilsonc7150892009-09-23 00:43:56 +01001168 if (ret)
1169 goto unlock;
Eric Anholtd9ddcb92009-01-27 10:33:49 -08001170 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001171
1172 pfn = ((dev->agp->base + obj_priv->gtt_offset) >> PAGE_SHIFT) +
1173 page_offset;
1174
1175 /* Finally, remap it using the new GTT offset */
1176 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
Chris Wilsonc7150892009-09-23 00:43:56 +01001177unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001178 mutex_unlock(&dev->struct_mutex);
1179
1180 switch (ret) {
Chris Wilsonc7150892009-09-23 00:43:56 +01001181 case 0:
1182 case -ERESTARTSYS:
1183 return VM_FAULT_NOPAGE;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001184 case -ENOMEM:
1185 case -EAGAIN:
1186 return VM_FAULT_OOM;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001187 default:
Chris Wilsonc7150892009-09-23 00:43:56 +01001188 return VM_FAULT_SIGBUS;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001189 }
1190}
1191
1192/**
1193 * i915_gem_create_mmap_offset - create a fake mmap offset for an object
1194 * @obj: obj in question
1195 *
1196 * GEM memory mapping works by handing back to userspace a fake mmap offset
1197 * it can use in a subsequent mmap(2) call. The DRM core code then looks
1198 * up the object based on the offset and sets up the various memory mapping
1199 * structures.
1200 *
1201 * This routine allocates and attaches a fake offset for @obj.
1202 */
1203static int
1204i915_gem_create_mmap_offset(struct drm_gem_object *obj)
1205{
1206 struct drm_device *dev = obj->dev;
1207 struct drm_gem_mm *mm = dev->mm_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001208 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001209 struct drm_map_list *list;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +11001210 struct drm_local_map *map;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001211 int ret = 0;
1212
1213 /* Set the object up for mmap'ing */
1214 list = &obj->map_list;
Eric Anholt9a298b22009-03-24 12:23:04 -07001215 list->map = kzalloc(sizeof(struct drm_map_list), GFP_KERNEL);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001216 if (!list->map)
1217 return -ENOMEM;
1218
1219 map = list->map;
1220 map->type = _DRM_GEM;
1221 map->size = obj->size;
1222 map->handle = obj;
1223
1224 /* Get a DRM GEM mmap offset allocated... */
1225 list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
1226 obj->size / PAGE_SIZE, 0, 0);
1227 if (!list->file_offset_node) {
1228 DRM_ERROR("failed to allocate offset for bo %d\n", obj->name);
1229 ret = -ENOMEM;
1230 goto out_free_list;
1231 }
1232
1233 list->file_offset_node = drm_mm_get_block(list->file_offset_node,
1234 obj->size / PAGE_SIZE, 0);
1235 if (!list->file_offset_node) {
1236 ret = -ENOMEM;
1237 goto out_free_list;
1238 }
1239
1240 list->hash.key = list->file_offset_node->start;
1241 if (drm_ht_insert_item(&mm->offset_hash, &list->hash)) {
1242 DRM_ERROR("failed to add to map hash\n");
Chris Wilson5618ca62009-12-02 15:15:30 +00001243 ret = -ENOMEM;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001244 goto out_free_mm;
1245 }
1246
1247 /* By now we should be all set, any drm_mmap request on the offset
1248 * below will get to our mmap & fault handler */
1249 obj_priv->mmap_offset = ((uint64_t) list->hash.key) << PAGE_SHIFT;
1250
1251 return 0;
1252
1253out_free_mm:
1254 drm_mm_put_block(list->file_offset_node);
1255out_free_list:
Eric Anholt9a298b22009-03-24 12:23:04 -07001256 kfree(list->map);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001257
1258 return ret;
1259}
1260
Chris Wilson901782b2009-07-10 08:18:50 +01001261/**
1262 * i915_gem_release_mmap - remove physical page mappings
1263 * @obj: obj in question
1264 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001265 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01001266 * relinquish ownership of the pages back to the system.
1267 *
1268 * It is vital that we remove the page mapping if we have mapped a tiled
1269 * object through the GTT and then lose the fence register due to
1270 * resource pressure. Similarly if the object has been moved out of the
1271 * aperture, than pages mapped into userspace must be revoked. Removing the
1272 * mapping will then trigger a page fault on the next user access, allowing
1273 * fixup by i915_gem_fault().
1274 */
Eric Anholtd05ca302009-07-10 13:02:26 -07001275void
Chris Wilson901782b2009-07-10 08:18:50 +01001276i915_gem_release_mmap(struct drm_gem_object *obj)
1277{
1278 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001279 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson901782b2009-07-10 08:18:50 +01001280
1281 if (dev->dev_mapping)
1282 unmap_mapping_range(dev->dev_mapping,
1283 obj_priv->mmap_offset, obj->size, 1);
1284}
1285
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001286static void
1287i915_gem_free_mmap_offset(struct drm_gem_object *obj)
1288{
1289 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001290 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001291 struct drm_gem_mm *mm = dev->mm_private;
1292 struct drm_map_list *list;
1293
1294 list = &obj->map_list;
1295 drm_ht_remove_item(&mm->offset_hash, &list->hash);
1296
1297 if (list->file_offset_node) {
1298 drm_mm_put_block(list->file_offset_node);
1299 list->file_offset_node = NULL;
1300 }
1301
1302 if (list->map) {
Eric Anholt9a298b22009-03-24 12:23:04 -07001303 kfree(list->map);
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001304 list->map = NULL;
1305 }
1306
1307 obj_priv->mmap_offset = 0;
1308}
1309
Jesse Barnesde151cf2008-11-12 10:03:55 -08001310/**
1311 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1312 * @obj: object to check
1313 *
1314 * Return the required GTT alignment for an object, taking into account
1315 * potential fence register mapping if needed.
1316 */
1317static uint32_t
1318i915_gem_get_gtt_alignment(struct drm_gem_object *obj)
1319{
1320 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001321 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001322 int start, i;
1323
1324 /*
1325 * Minimum alignment is 4k (GTT page size), but might be greater
1326 * if a fence register is needed for the object.
1327 */
1328 if (IS_I965G(dev) || obj_priv->tiling_mode == I915_TILING_NONE)
1329 return 4096;
1330
1331 /*
1332 * Previous chips need to be aligned to the size of the smallest
1333 * fence register that can contain the object.
1334 */
1335 if (IS_I9XX(dev))
1336 start = 1024*1024;
1337 else
1338 start = 512*1024;
1339
1340 for (i = start; i < obj->size; i <<= 1)
1341 ;
1342
1343 return i;
1344}
1345
1346/**
1347 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1348 * @dev: DRM device
1349 * @data: GTT mapping ioctl data
1350 * @file_priv: GEM object info
1351 *
1352 * Simply returns the fake offset to userspace so it can mmap it.
1353 * The mmap call will end up in drm_gem_mmap(), which will set things
1354 * up so we can get faults in the handler above.
1355 *
1356 * The fault handler will take care of binding the object into the GTT
1357 * (since it may have been evicted to make room for something), allocating
1358 * a fence register, and mapping the appropriate aperture address into
1359 * userspace.
1360 */
1361int
1362i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
1363 struct drm_file *file_priv)
1364{
1365 struct drm_i915_gem_mmap_gtt *args = data;
1366 struct drm_i915_private *dev_priv = dev->dev_private;
1367 struct drm_gem_object *obj;
1368 struct drm_i915_gem_object *obj_priv;
1369 int ret;
1370
1371 if (!(dev->driver->driver_features & DRIVER_GEM))
1372 return -ENODEV;
1373
1374 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1375 if (obj == NULL)
1376 return -EBADF;
1377
1378 mutex_lock(&dev->struct_mutex);
1379
Daniel Vetter23010e42010-03-08 13:35:02 +01001380 obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001381
Chris Wilsonab182822009-09-22 18:46:17 +01001382 if (obj_priv->madv != I915_MADV_WILLNEED) {
1383 DRM_ERROR("Attempting to mmap a purgeable buffer\n");
1384 drm_gem_object_unreference(obj);
1385 mutex_unlock(&dev->struct_mutex);
1386 return -EINVAL;
1387 }
1388
1389
Jesse Barnesde151cf2008-11-12 10:03:55 -08001390 if (!obj_priv->mmap_offset) {
1391 ret = i915_gem_create_mmap_offset(obj);
Chris Wilson13af1062009-02-11 14:26:31 +00001392 if (ret) {
1393 drm_gem_object_unreference(obj);
1394 mutex_unlock(&dev->struct_mutex);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001395 return ret;
Chris Wilson13af1062009-02-11 14:26:31 +00001396 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001397 }
1398
1399 args->offset = obj_priv->mmap_offset;
1400
Jesse Barnesde151cf2008-11-12 10:03:55 -08001401 /*
1402 * Pull it into the GTT so that we have a page list (makes the
1403 * initial fault faster and any subsequent flushing possible).
1404 */
1405 if (!obj_priv->agp_mem) {
Chris Wilsone67b8ce2009-09-14 16:50:26 +01001406 ret = i915_gem_object_bind_to_gtt(obj, 0);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001407 if (ret) {
1408 drm_gem_object_unreference(obj);
1409 mutex_unlock(&dev->struct_mutex);
1410 return ret;
1411 }
Jesse Barnes14b60392009-05-20 16:47:08 -04001412 list_add_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001413 }
1414
1415 drm_gem_object_unreference(obj);
1416 mutex_unlock(&dev->struct_mutex);
1417
1418 return 0;
1419}
1420
Ben Gamari6911a9b2009-04-02 11:24:54 -07001421void
Eric Anholt856fa192009-03-19 14:10:50 -07001422i915_gem_object_put_pages(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07001423{
Daniel Vetter23010e42010-03-08 13:35:02 +01001424 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001425 int page_count = obj->size / PAGE_SIZE;
1426 int i;
1427
Eric Anholt856fa192009-03-19 14:10:50 -07001428 BUG_ON(obj_priv->pages_refcount == 0);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001429 BUG_ON(obj_priv->madv == __I915_MADV_PURGED);
Eric Anholt856fa192009-03-19 14:10:50 -07001430
1431 if (--obj_priv->pages_refcount != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07001432 return;
1433
Eric Anholt280b7132009-03-12 16:56:27 -07001434 if (obj_priv->tiling_mode != I915_TILING_NONE)
1435 i915_gem_object_save_bit_17_swizzle(obj);
1436
Chris Wilson3ef94da2009-09-14 16:50:29 +01001437 if (obj_priv->madv == I915_MADV_DONTNEED)
Chris Wilson13a05fd2009-09-20 23:03:19 +01001438 obj_priv->dirty = 0;
Chris Wilson3ef94da2009-09-14 16:50:29 +01001439
1440 for (i = 0; i < page_count; i++) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01001441 if (obj_priv->dirty)
1442 set_page_dirty(obj_priv->pages[i]);
1443
1444 if (obj_priv->madv == I915_MADV_WILLNEED)
Eric Anholt856fa192009-03-19 14:10:50 -07001445 mark_page_accessed(obj_priv->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001446
1447 page_cache_release(obj_priv->pages[i]);
1448 }
Eric Anholt673a3942008-07-30 12:06:12 -07001449 obj_priv->dirty = 0;
1450
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07001451 drm_free_large(obj_priv->pages);
Eric Anholt856fa192009-03-19 14:10:50 -07001452 obj_priv->pages = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001453}
1454
1455static void
Zou Nan hai852835f2010-05-21 09:08:56 +08001456i915_gem_object_move_to_active(struct drm_gem_object *obj, uint32_t seqno,
1457 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001458{
1459 struct drm_device *dev = obj->dev;
1460 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001461 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Zou Nan hai852835f2010-05-21 09:08:56 +08001462 BUG_ON(ring == NULL);
1463 obj_priv->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001464
1465 /* Add a reference if we're newly entering the active list. */
1466 if (!obj_priv->active) {
1467 drm_gem_object_reference(obj);
1468 obj_priv->active = 1;
1469 }
1470 /* Move from whatever list we were on to the tail of execution. */
Carl Worth5e118f42009-03-20 11:54:25 -07001471 spin_lock(&dev_priv->mm.active_list_lock);
Zou Nan hai852835f2010-05-21 09:08:56 +08001472 list_move_tail(&obj_priv->list, &ring->active_list);
Carl Worth5e118f42009-03-20 11:54:25 -07001473 spin_unlock(&dev_priv->mm.active_list_lock);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001474 obj_priv->last_rendering_seqno = seqno;
Eric Anholt673a3942008-07-30 12:06:12 -07001475}
1476
Eric Anholtce44b0e2008-11-06 16:00:31 -08001477static void
1478i915_gem_object_move_to_flushing(struct drm_gem_object *obj)
1479{
1480 struct drm_device *dev = obj->dev;
1481 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001482 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001483
1484 BUG_ON(!obj_priv->active);
1485 list_move_tail(&obj_priv->list, &dev_priv->mm.flushing_list);
1486 obj_priv->last_rendering_seqno = 0;
1487}
Eric Anholt673a3942008-07-30 12:06:12 -07001488
Chris Wilson963b4832009-09-20 23:03:54 +01001489/* Immediately discard the backing storage */
1490static void
1491i915_gem_object_truncate(struct drm_gem_object *obj)
1492{
Daniel Vetter23010e42010-03-08 13:35:02 +01001493 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001494 struct inode *inode;
Chris Wilson963b4832009-09-20 23:03:54 +01001495
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001496 inode = obj->filp->f_path.dentry->d_inode;
1497 if (inode->i_op->truncate)
1498 inode->i_op->truncate (inode);
1499
1500 obj_priv->madv = __I915_MADV_PURGED;
Chris Wilson963b4832009-09-20 23:03:54 +01001501}
1502
1503static inline int
1504i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj_priv)
1505{
1506 return obj_priv->madv == I915_MADV_DONTNEED;
1507}
1508
Eric Anholt673a3942008-07-30 12:06:12 -07001509static void
1510i915_gem_object_move_to_inactive(struct drm_gem_object *obj)
1511{
1512 struct drm_device *dev = obj->dev;
1513 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001514 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001515
1516 i915_verify_inactive(dev, __FILE__, __LINE__);
1517 if (obj_priv->pin_count != 0)
1518 list_del_init(&obj_priv->list);
1519 else
1520 list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
1521
Daniel Vetter99fcb762010-02-07 16:20:18 +01001522 BUG_ON(!list_empty(&obj_priv->gpu_write_list));
1523
Eric Anholtce44b0e2008-11-06 16:00:31 -08001524 obj_priv->last_rendering_seqno = 0;
Zou Nan hai852835f2010-05-21 09:08:56 +08001525 obj_priv->ring = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001526 if (obj_priv->active) {
1527 obj_priv->active = 0;
1528 drm_gem_object_unreference(obj);
1529 }
1530 i915_verify_inactive(dev, __FILE__, __LINE__);
1531}
1532
Daniel Vetter63560392010-02-19 11:51:59 +01001533static void
1534i915_gem_process_flushing_list(struct drm_device *dev,
Zou Nan hai852835f2010-05-21 09:08:56 +08001535 uint32_t flush_domains, uint32_t seqno,
1536 struct intel_ring_buffer *ring)
Daniel Vetter63560392010-02-19 11:51:59 +01001537{
1538 drm_i915_private_t *dev_priv = dev->dev_private;
1539 struct drm_i915_gem_object *obj_priv, *next;
1540
1541 list_for_each_entry_safe(obj_priv, next,
1542 &dev_priv->mm.gpu_write_list,
1543 gpu_write_list) {
Daniel Vettera8089e82010-04-09 19:05:09 +00001544 struct drm_gem_object *obj = &obj_priv->base;
Daniel Vetter63560392010-02-19 11:51:59 +01001545
1546 if ((obj->write_domain & flush_domains) ==
Zou Nan hai852835f2010-05-21 09:08:56 +08001547 obj->write_domain &&
1548 obj_priv->ring->ring_flag == ring->ring_flag) {
Daniel Vetter63560392010-02-19 11:51:59 +01001549 uint32_t old_write_domain = obj->write_domain;
1550
1551 obj->write_domain = 0;
1552 list_del_init(&obj_priv->gpu_write_list);
Zou Nan hai852835f2010-05-21 09:08:56 +08001553 i915_gem_object_move_to_active(obj, seqno, ring);
Daniel Vetter63560392010-02-19 11:51:59 +01001554
1555 /* update the fence lru list */
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001556 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
1557 struct drm_i915_fence_reg *reg =
1558 &dev_priv->fence_regs[obj_priv->fence_reg];
1559 list_move_tail(&reg->lru_list,
Daniel Vetter63560392010-02-19 11:51:59 +01001560 &dev_priv->mm.fence_list);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001561 }
Daniel Vetter63560392010-02-19 11:51:59 +01001562
1563 trace_i915_gem_object_change_domain(obj,
1564 obj->read_domains,
1565 old_write_domain);
1566 }
1567 }
1568}
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001569
Daniel Vetter5a5a0c62009-09-15 22:57:36 +02001570uint32_t
Eric Anholtb9624422009-06-03 07:27:35 +00001571i915_add_request(struct drm_device *dev, struct drm_file *file_priv,
Zou Nan hai852835f2010-05-21 09:08:56 +08001572 uint32_t flush_domains, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001573{
1574 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtb9624422009-06-03 07:27:35 +00001575 struct drm_i915_file_private *i915_file_priv = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001576 struct drm_i915_gem_request *request;
1577 uint32_t seqno;
1578 int was_empty;
Eric Anholt673a3942008-07-30 12:06:12 -07001579
Eric Anholtb9624422009-06-03 07:27:35 +00001580 if (file_priv != NULL)
1581 i915_file_priv = file_priv->driver_priv;
1582
Eric Anholt9a298b22009-03-24 12:23:04 -07001583 request = kzalloc(sizeof(*request), GFP_KERNEL);
Eric Anholt673a3942008-07-30 12:06:12 -07001584 if (request == NULL)
1585 return 0;
1586
Zou Nan hai852835f2010-05-21 09:08:56 +08001587 seqno = ring->add_request(dev, ring, file_priv, flush_domains);
Eric Anholt673a3942008-07-30 12:06:12 -07001588
1589 request->seqno = seqno;
Zou Nan hai852835f2010-05-21 09:08:56 +08001590 request->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001591 request->emitted_jiffies = jiffies;
Zou Nan hai852835f2010-05-21 09:08:56 +08001592 was_empty = list_empty(&ring->request_list);
1593 list_add_tail(&request->list, &ring->request_list);
1594
Eric Anholtb9624422009-06-03 07:27:35 +00001595 if (i915_file_priv) {
1596 list_add_tail(&request->client_list,
1597 &i915_file_priv->mm.request_list);
1598 } else {
1599 INIT_LIST_HEAD(&request->client_list);
1600 }
Eric Anholt673a3942008-07-30 12:06:12 -07001601
Eric Anholtce44b0e2008-11-06 16:00:31 -08001602 /* Associate any objects on the flushing list matching the write
1603 * domain we're flushing with our flush.
1604 */
Daniel Vetter63560392010-02-19 11:51:59 +01001605 if (flush_domains != 0)
Zou Nan hai852835f2010-05-21 09:08:56 +08001606 i915_gem_process_flushing_list(dev, flush_domains, seqno, ring);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001607
Ben Gamarif65d9422009-09-14 17:48:44 -04001608 if (!dev_priv->mm.suspended) {
1609 mod_timer(&dev_priv->hangcheck_timer, jiffies + DRM_I915_HANGCHECK_PERIOD);
1610 if (was_empty)
1611 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
1612 }
Eric Anholt673a3942008-07-30 12:06:12 -07001613 return seqno;
1614}
1615
1616/**
1617 * Command execution barrier
1618 *
1619 * Ensures that all commands in the ring are finished
1620 * before signalling the CPU
1621 */
Eric Anholt3043c602008-10-02 12:24:47 -07001622static uint32_t
Zou Nan hai852835f2010-05-21 09:08:56 +08001623i915_retire_commands(struct drm_device *dev, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001624{
Eric Anholt673a3942008-07-30 12:06:12 -07001625 uint32_t flush_domains = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001626
1627 /* The sampler always gets flushed on i965 (sigh) */
1628 if (IS_I965G(dev))
1629 flush_domains |= I915_GEM_DOMAIN_SAMPLER;
Zou Nan hai852835f2010-05-21 09:08:56 +08001630
1631 ring->flush(dev, ring,
1632 I915_GEM_DOMAIN_COMMAND, flush_domains);
Eric Anholt673a3942008-07-30 12:06:12 -07001633 return flush_domains;
1634}
1635
1636/**
1637 * Moves buffers associated only with the given active seqno from the active
1638 * to inactive list, potentially freeing them.
1639 */
1640static void
1641i915_gem_retire_request(struct drm_device *dev,
1642 struct drm_i915_gem_request *request)
1643{
1644 drm_i915_private_t *dev_priv = dev->dev_private;
1645
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001646 trace_i915_gem_request_retire(dev, request->seqno);
1647
Eric Anholt673a3942008-07-30 12:06:12 -07001648 /* Move any buffers on the active list that are no longer referenced
1649 * by the ringbuffer to the flushing/inactive lists as appropriate.
1650 */
Carl Worth5e118f42009-03-20 11:54:25 -07001651 spin_lock(&dev_priv->mm.active_list_lock);
Zou Nan hai852835f2010-05-21 09:08:56 +08001652 while (!list_empty(&request->ring->active_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001653 struct drm_gem_object *obj;
1654 struct drm_i915_gem_object *obj_priv;
1655
Zou Nan hai852835f2010-05-21 09:08:56 +08001656 obj_priv = list_first_entry(&request->ring->active_list,
Eric Anholt673a3942008-07-30 12:06:12 -07001657 struct drm_i915_gem_object,
1658 list);
Daniel Vettera8089e82010-04-09 19:05:09 +00001659 obj = &obj_priv->base;
Eric Anholt673a3942008-07-30 12:06:12 -07001660
1661 /* If the seqno being retired doesn't match the oldest in the
1662 * list, then the oldest in the list must still be newer than
1663 * this seqno.
1664 */
1665 if (obj_priv->last_rendering_seqno != request->seqno)
Carl Worth5e118f42009-03-20 11:54:25 -07001666 goto out;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001667
Eric Anholt673a3942008-07-30 12:06:12 -07001668#if WATCH_LRU
1669 DRM_INFO("%s: retire %d moves to inactive list %p\n",
1670 __func__, request->seqno, obj);
1671#endif
1672
Eric Anholtce44b0e2008-11-06 16:00:31 -08001673 if (obj->write_domain != 0)
1674 i915_gem_object_move_to_flushing(obj);
Shaohua Li68c84342009-04-08 10:58:23 +08001675 else {
1676 /* Take a reference on the object so it won't be
1677 * freed while the spinlock is held. The list
1678 * protection for this spinlock is safe when breaking
1679 * the lock like this since the next thing we do
1680 * is just get the head of the list again.
1681 */
1682 drm_gem_object_reference(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001683 i915_gem_object_move_to_inactive(obj);
Shaohua Li68c84342009-04-08 10:58:23 +08001684 spin_unlock(&dev_priv->mm.active_list_lock);
1685 drm_gem_object_unreference(obj);
1686 spin_lock(&dev_priv->mm.active_list_lock);
1687 }
Eric Anholt673a3942008-07-30 12:06:12 -07001688 }
Carl Worth5e118f42009-03-20 11:54:25 -07001689out:
1690 spin_unlock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001691}
1692
1693/**
1694 * Returns true if seq1 is later than seq2.
1695 */
Ben Gamari22be1722009-09-14 17:48:43 -04001696bool
Eric Anholt673a3942008-07-30 12:06:12 -07001697i915_seqno_passed(uint32_t seq1, uint32_t seq2)
1698{
1699 return (int32_t)(seq1 - seq2) >= 0;
1700}
1701
1702uint32_t
Zou Nan hai852835f2010-05-21 09:08:56 +08001703i915_get_gem_seqno(struct drm_device *dev,
Zou Nan haid1b851f2010-05-21 09:08:57 +08001704 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001705{
Zou Nan hai852835f2010-05-21 09:08:56 +08001706 return ring->get_gem_seqno(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07001707}
1708
1709/**
1710 * This function clears the request list as sequence numbers are passed.
1711 */
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
1758 i915_gem_retire_requests_ring(dev, &dev_priv->render_ring);
1759 if (HAS_BSD(dev))
1760 i915_gem_retire_requests_ring(dev, &dev_priv->bsd_ring);
1761}
1762
1763void
Eric Anholt673a3942008-07-30 12:06:12 -07001764i915_gem_retire_work_handler(struct work_struct *work)
1765{
1766 drm_i915_private_t *dev_priv;
1767 struct drm_device *dev;
1768
1769 dev_priv = container_of(work, drm_i915_private_t,
1770 mm.retire_work.work);
1771 dev = dev_priv->dev;
1772
1773 mutex_lock(&dev->struct_mutex);
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001774 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08001775
Keith Packard6dbe2772008-10-14 21:41:13 -07001776 if (!dev_priv->mm.suspended &&
Zou Nan haid1b851f2010-05-21 09:08:57 +08001777 (!list_empty(&dev_priv->render_ring.request_list) ||
1778 (HAS_BSD(dev) &&
1779 !list_empty(&dev_priv->bsd_ring.request_list))))
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001780 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
Eric Anholt673a3942008-07-30 12:06:12 -07001781 mutex_unlock(&dev->struct_mutex);
1782}
1783
Daniel Vetter5a5a0c62009-09-15 22:57:36 +02001784int
Zou Nan hai852835f2010-05-21 09:08:56 +08001785i915_do_wait_request(struct drm_device *dev, uint32_t seqno,
1786 int interruptible, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001787{
1788 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001789 u32 ier;
Eric Anholt673a3942008-07-30 12:06:12 -07001790 int ret = 0;
1791
1792 BUG_ON(seqno == 0);
1793
Ben Gamariba1234d2009-09-14 17:48:47 -04001794 if (atomic_read(&dev_priv->mm.wedged))
Ben Gamariffed1d02009-09-14 17:48:41 -04001795 return -EIO;
1796
Zou Nan hai852835f2010-05-21 09:08:56 +08001797 if (!i915_seqno_passed(ring->get_gem_seqno(dev, ring), seqno)) {
Eric Anholtbad720f2009-10-22 16:11:14 -07001798 if (HAS_PCH_SPLIT(dev))
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001799 ier = I915_READ(DEIER) | I915_READ(GTIER);
1800 else
1801 ier = I915_READ(IER);
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001802 if (!ier) {
1803 DRM_ERROR("something (likely vbetool) disabled "
1804 "interrupts, re-enabling\n");
1805 i915_driver_irq_preinstall(dev);
1806 i915_driver_irq_postinstall(dev);
1807 }
1808
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001809 trace_i915_gem_request_wait_begin(dev, seqno);
1810
Zou Nan hai852835f2010-05-21 09:08:56 +08001811 ring->waiting_gem_seqno = seqno;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001812 ring->user_irq_get(dev, ring);
Daniel Vetter48764bf2009-09-15 22:57:32 +02001813 if (interruptible)
Zou Nan hai852835f2010-05-21 09:08:56 +08001814 ret = wait_event_interruptible(ring->irq_queue,
1815 i915_seqno_passed(
1816 ring->get_gem_seqno(dev, ring), seqno)
1817 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02001818 else
Zou Nan hai852835f2010-05-21 09:08:56 +08001819 wait_event(ring->irq_queue,
1820 i915_seqno_passed(
1821 ring->get_gem_seqno(dev, ring), seqno)
1822 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02001823
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001824 ring->user_irq_put(dev, ring);
Zou Nan hai852835f2010-05-21 09:08:56 +08001825 ring->waiting_gem_seqno = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001826
1827 trace_i915_gem_request_wait_end(dev, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07001828 }
Ben Gamariba1234d2009-09-14 17:48:47 -04001829 if (atomic_read(&dev_priv->mm.wedged))
Eric Anholt673a3942008-07-30 12:06:12 -07001830 ret = -EIO;
1831
1832 if (ret && ret != -ERESTARTSYS)
1833 DRM_ERROR("%s returns %d (awaiting %d at %d)\n",
Zou Nan hai852835f2010-05-21 09:08:56 +08001834 __func__, ret, seqno, ring->get_gem_seqno(dev, ring));
Eric Anholt673a3942008-07-30 12:06:12 -07001835
1836 /* Directly dispatch request retiring. While we have the work queue
1837 * to handle this, the waiter on a request often wants an associated
1838 * buffer to have made it to the inactive list, and we would need
1839 * a separate wait queue to handle that.
1840 */
1841 if (ret == 0)
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001842 i915_gem_retire_requests_ring(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07001843
1844 return ret;
1845}
1846
Daniel Vetter48764bf2009-09-15 22:57:32 +02001847/**
1848 * Waits for a sequence number to be signaled, and cleans up the
1849 * request and object lists appropriately for that event.
1850 */
1851static int
Zou Nan hai852835f2010-05-21 09:08:56 +08001852i915_wait_request(struct drm_device *dev, uint32_t seqno,
1853 struct intel_ring_buffer *ring)
Daniel Vetter48764bf2009-09-15 22:57:32 +02001854{
Zou Nan hai852835f2010-05-21 09:08:56 +08001855 return i915_do_wait_request(dev, seqno, 1, ring);
Daniel Vetter48764bf2009-09-15 22:57:32 +02001856}
1857
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001858static void
1859i915_gem_flush(struct drm_device *dev,
1860 uint32_t invalidate_domains,
1861 uint32_t flush_domains)
1862{
1863 drm_i915_private_t *dev_priv = dev->dev_private;
1864 if (flush_domains & I915_GEM_DOMAIN_CPU)
1865 drm_agp_chipset_flush(dev);
1866 dev_priv->render_ring.flush(dev, &dev_priv->render_ring,
1867 invalidate_domains,
1868 flush_domains);
Zou Nan haid1b851f2010-05-21 09:08:57 +08001869
1870 if (HAS_BSD(dev))
1871 dev_priv->bsd_ring.flush(dev, &dev_priv->bsd_ring,
1872 invalidate_domains,
1873 flush_domains);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001874}
1875
Zou Nan hai852835f2010-05-21 09:08:56 +08001876static void
1877i915_gem_flush_ring(struct drm_device *dev,
1878 uint32_t invalidate_domains,
1879 uint32_t flush_domains,
1880 struct intel_ring_buffer *ring)
1881{
1882 if (flush_domains & I915_GEM_DOMAIN_CPU)
1883 drm_agp_chipset_flush(dev);
1884 ring->flush(dev, ring,
1885 invalidate_domains,
1886 flush_domains);
1887}
1888
Eric Anholt673a3942008-07-30 12:06:12 -07001889/**
1890 * Ensures that all rendering to the object has completed and the object is
1891 * safe to unbind from the GTT or access from the CPU.
1892 */
1893static int
1894i915_gem_object_wait_rendering(struct drm_gem_object *obj)
1895{
1896 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001897 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001898 int ret;
1899
Eric Anholte47c68e2008-11-14 13:35:19 -08001900 /* This function only exists to support waiting for existing rendering,
1901 * not for emitting required flushes.
Eric Anholt673a3942008-07-30 12:06:12 -07001902 */
Eric Anholte47c68e2008-11-14 13:35:19 -08001903 BUG_ON((obj->write_domain & I915_GEM_GPU_DOMAINS) != 0);
Eric Anholt673a3942008-07-30 12:06:12 -07001904
1905 /* If there is rendering queued on the buffer being evicted, wait for
1906 * it.
1907 */
1908 if (obj_priv->active) {
1909#if WATCH_BUF
1910 DRM_INFO("%s: object %p wait for seqno %08x\n",
1911 __func__, obj, obj_priv->last_rendering_seqno);
1912#endif
Zou Nan hai852835f2010-05-21 09:08:56 +08001913 ret = i915_wait_request(dev,
1914 obj_priv->last_rendering_seqno, obj_priv->ring);
Eric Anholt673a3942008-07-30 12:06:12 -07001915 if (ret != 0)
1916 return ret;
1917 }
1918
1919 return 0;
1920}
1921
1922/**
1923 * Unbinds an object from the GTT aperture.
1924 */
Jesse Barnes0f973f22009-01-26 17:10:45 -08001925int
Eric Anholt673a3942008-07-30 12:06:12 -07001926i915_gem_object_unbind(struct drm_gem_object *obj)
1927{
1928 struct drm_device *dev = obj->dev;
Daniel Vetter4a87b8c2010-02-19 11:51:57 +01001929 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001930 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001931 int ret = 0;
1932
1933#if WATCH_BUF
1934 DRM_INFO("%s:%d %p\n", __func__, __LINE__, obj);
1935 DRM_INFO("gtt_space %p\n", obj_priv->gtt_space);
1936#endif
1937 if (obj_priv->gtt_space == NULL)
1938 return 0;
1939
1940 if (obj_priv->pin_count != 0) {
1941 DRM_ERROR("Attempting to unbind pinned buffer\n");
1942 return -EINVAL;
1943 }
1944
Eric Anholt5323fd02009-09-09 11:50:45 -07001945 /* blow away mappings if mapped through GTT */
1946 i915_gem_release_mmap(obj);
1947
Eric Anholt673a3942008-07-30 12:06:12 -07001948 /* Move the object to the CPU domain to ensure that
1949 * any possible CPU writes while it's not in the GTT
1950 * are flushed when we go to remap it. This will
1951 * also ensure that all pending GPU writes are finished
1952 * before we unbind.
1953 */
Eric Anholte47c68e2008-11-14 13:35:19 -08001954 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07001955 if (ret) {
Eric Anholte47c68e2008-11-14 13:35:19 -08001956 if (ret != -ERESTARTSYS)
1957 DRM_ERROR("set_domain failed: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07001958 return ret;
1959 }
1960
Eric Anholt5323fd02009-09-09 11:50:45 -07001961 BUG_ON(obj_priv->active);
1962
Daniel Vetter96b47b62009-12-15 17:50:00 +01001963 /* release the fence reg _after_ flushing */
1964 if (obj_priv->fence_reg != I915_FENCE_REG_NONE)
1965 i915_gem_clear_fence_reg(obj);
1966
Eric Anholt673a3942008-07-30 12:06:12 -07001967 if (obj_priv->agp_mem != NULL) {
1968 drm_unbind_agp(obj_priv->agp_mem);
1969 drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE);
1970 obj_priv->agp_mem = NULL;
1971 }
1972
Eric Anholt856fa192009-03-19 14:10:50 -07001973 i915_gem_object_put_pages(obj);
Chris Wilsona32808c2009-09-20 21:29:47 +01001974 BUG_ON(obj_priv->pages_refcount);
Eric Anholt673a3942008-07-30 12:06:12 -07001975
1976 if (obj_priv->gtt_space) {
1977 atomic_dec(&dev->gtt_count);
1978 atomic_sub(obj->size, &dev->gtt_memory);
1979
1980 drm_mm_put_block(obj_priv->gtt_space);
1981 obj_priv->gtt_space = NULL;
1982 }
1983
1984 /* Remove ourselves from the LRU list if present. */
Daniel Vetter4a87b8c2010-02-19 11:51:57 +01001985 spin_lock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001986 if (!list_empty(&obj_priv->list))
1987 list_del_init(&obj_priv->list);
Daniel Vetter4a87b8c2010-02-19 11:51:57 +01001988 spin_unlock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001989
Chris Wilson963b4832009-09-20 23:03:54 +01001990 if (i915_gem_object_is_purgeable(obj_priv))
1991 i915_gem_object_truncate(obj);
1992
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001993 trace_i915_gem_object_unbind(obj);
1994
Eric Anholt673a3942008-07-30 12:06:12 -07001995 return 0;
1996}
1997
Chris Wilson07f73f62009-09-14 16:50:30 +01001998static struct drm_gem_object *
1999i915_gem_find_inactive_object(struct drm_device *dev, int min_size)
2000{
2001 drm_i915_private_t *dev_priv = dev->dev_private;
2002 struct drm_i915_gem_object *obj_priv;
2003 struct drm_gem_object *best = NULL;
2004 struct drm_gem_object *first = NULL;
2005
2006 /* Try to find the smallest clean object */
2007 list_for_each_entry(obj_priv, &dev_priv->mm.inactive_list, list) {
Daniel Vettera8089e82010-04-09 19:05:09 +00002008 struct drm_gem_object *obj = &obj_priv->base;
Chris Wilson07f73f62009-09-14 16:50:30 +01002009 if (obj->size >= min_size) {
Chris Wilson963b4832009-09-20 23:03:54 +01002010 if ((!obj_priv->dirty ||
2011 i915_gem_object_is_purgeable(obj_priv)) &&
Chris Wilson07f73f62009-09-14 16:50:30 +01002012 (!best || obj->size < best->size)) {
2013 best = obj;
2014 if (best->size == min_size)
2015 return best;
2016 }
2017 if (!first)
2018 first = obj;
2019 }
2020 }
2021
2022 return best ? best : first;
2023}
2024
Eric Anholt673a3942008-07-30 12:06:12 -07002025static int
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002026i915_gpu_idle(struct drm_device *dev)
2027{
2028 drm_i915_private_t *dev_priv = dev->dev_private;
2029 bool lists_empty;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002030 uint32_t seqno1, seqno2;
Zou Nan hai852835f2010-05-21 09:08:56 +08002031 int ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002032
2033 spin_lock(&dev_priv->mm.active_list_lock);
Zou Nan haid1b851f2010-05-21 09:08:57 +08002034 lists_empty = (list_empty(&dev_priv->mm.flushing_list) &&
2035 list_empty(&dev_priv->render_ring.active_list) &&
2036 (!HAS_BSD(dev) ||
2037 list_empty(&dev_priv->bsd_ring.active_list)));
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002038 spin_unlock(&dev_priv->mm.active_list_lock);
2039
2040 if (lists_empty)
2041 return 0;
2042
2043 /* Flush everything onto the inactive list. */
2044 i915_gem_flush(dev, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
Zou Nan haid1b851f2010-05-21 09:08:57 +08002045 seqno1 = i915_add_request(dev, NULL, I915_GEM_GPU_DOMAINS,
Zou Nan hai852835f2010-05-21 09:08:56 +08002046 &dev_priv->render_ring);
Zou Nan haid1b851f2010-05-21 09:08:57 +08002047 if (seqno1 == 0)
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002048 return -ENOMEM;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002049 ret = i915_wait_request(dev, seqno1, &dev_priv->render_ring);
2050
2051 if (HAS_BSD(dev)) {
2052 seqno2 = i915_add_request(dev, NULL, I915_GEM_GPU_DOMAINS,
2053 &dev_priv->bsd_ring);
2054 if (seqno2 == 0)
2055 return -ENOMEM;
2056
2057 ret = i915_wait_request(dev, seqno2, &dev_priv->bsd_ring);
2058 if (ret)
2059 return ret;
2060 }
2061
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002062
Zou Nan hai852835f2010-05-21 09:08:56 +08002063 return ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002064}
2065
2066static int
Chris Wilson07f73f62009-09-14 16:50:30 +01002067i915_gem_evict_everything(struct drm_device *dev)
2068{
2069 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson07f73f62009-09-14 16:50:30 +01002070 int ret;
2071 bool lists_empty;
2072
Chris Wilson07f73f62009-09-14 16:50:30 +01002073 spin_lock(&dev_priv->mm.active_list_lock);
2074 lists_empty = (list_empty(&dev_priv->mm.inactive_list) &&
2075 list_empty(&dev_priv->mm.flushing_list) &&
Zou Nan haid1b851f2010-05-21 09:08:57 +08002076 list_empty(&dev_priv->render_ring.active_list) &&
2077 (!HAS_BSD(dev)
2078 || list_empty(&dev_priv->bsd_ring.active_list)));
Chris Wilson07f73f62009-09-14 16:50:30 +01002079 spin_unlock(&dev_priv->mm.active_list_lock);
2080
Chris Wilson97311292009-09-21 00:22:34 +01002081 if (lists_empty)
Chris Wilson07f73f62009-09-14 16:50:30 +01002082 return -ENOSPC;
Chris Wilson07f73f62009-09-14 16:50:30 +01002083
2084 /* Flush everything (on to the inactive lists) and evict */
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002085 ret = i915_gpu_idle(dev);
Chris Wilson07f73f62009-09-14 16:50:30 +01002086 if (ret)
2087 return ret;
2088
Daniel Vetter99fcb762010-02-07 16:20:18 +01002089 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
2090
Chris Wilsonab5ee572009-09-20 19:25:47 +01002091 ret = i915_gem_evict_from_inactive_list(dev);
Chris Wilson07f73f62009-09-14 16:50:30 +01002092 if (ret)
2093 return ret;
2094
2095 spin_lock(&dev_priv->mm.active_list_lock);
2096 lists_empty = (list_empty(&dev_priv->mm.inactive_list) &&
2097 list_empty(&dev_priv->mm.flushing_list) &&
Zou Nan haid1b851f2010-05-21 09:08:57 +08002098 list_empty(&dev_priv->render_ring.active_list) &&
2099 (!HAS_BSD(dev)
2100 || list_empty(&dev_priv->bsd_ring.active_list)));
Chris Wilson07f73f62009-09-14 16:50:30 +01002101 spin_unlock(&dev_priv->mm.active_list_lock);
2102 BUG_ON(!lists_empty);
2103
Eric Anholt673a3942008-07-30 12:06:12 -07002104 return 0;
2105}
2106
2107static int
Chris Wilson07f73f62009-09-14 16:50:30 +01002108i915_gem_evict_something(struct drm_device *dev, int min_size)
Eric Anholt673a3942008-07-30 12:06:12 -07002109{
2110 drm_i915_private_t *dev_priv = dev->dev_private;
2111 struct drm_gem_object *obj;
Chris Wilson07f73f62009-09-14 16:50:30 +01002112 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002113
Zou Nan hai852835f2010-05-21 09:08:56 +08002114 struct intel_ring_buffer *render_ring = &dev_priv->render_ring;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002115 struct intel_ring_buffer *bsd_ring = &dev_priv->bsd_ring;
Eric Anholt673a3942008-07-30 12:06:12 -07002116 for (;;) {
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002117 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08002118
Eric Anholt673a3942008-07-30 12:06:12 -07002119 /* If there's an inactive buffer available now, grab it
2120 * and be done.
2121 */
Chris Wilson07f73f62009-09-14 16:50:30 +01002122 obj = i915_gem_find_inactive_object(dev, min_size);
2123 if (obj) {
2124 struct drm_i915_gem_object *obj_priv;
2125
Eric Anholt673a3942008-07-30 12:06:12 -07002126#if WATCH_LRU
2127 DRM_INFO("%s: evicting %p\n", __func__, obj);
2128#endif
Daniel Vetter23010e42010-03-08 13:35:02 +01002129 obj_priv = to_intel_bo(obj);
Chris Wilson07f73f62009-09-14 16:50:30 +01002130 BUG_ON(obj_priv->pin_count != 0);
Eric Anholt673a3942008-07-30 12:06:12 -07002131 BUG_ON(obj_priv->active);
2132
2133 /* Wait on the rendering and unbind the buffer. */
Chris Wilson07f73f62009-09-14 16:50:30 +01002134 return i915_gem_object_unbind(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002135 }
2136
2137 /* If we didn't get anything, but the ring is still processing
Chris Wilson07f73f62009-09-14 16:50:30 +01002138 * things, wait for the next to finish and hopefully leave us
2139 * a buffer to evict.
Eric Anholt673a3942008-07-30 12:06:12 -07002140 */
Zou Nan hai852835f2010-05-21 09:08:56 +08002141 if (!list_empty(&render_ring->request_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002142 struct drm_i915_gem_request *request;
2143
Zou Nan hai852835f2010-05-21 09:08:56 +08002144 request = list_first_entry(&render_ring->request_list,
Eric Anholt673a3942008-07-30 12:06:12 -07002145 struct drm_i915_gem_request,
2146 list);
2147
Zou Nan hai852835f2010-05-21 09:08:56 +08002148 ret = i915_wait_request(dev,
2149 request->seqno, request->ring);
Eric Anholt673a3942008-07-30 12:06:12 -07002150 if (ret)
Chris Wilson07f73f62009-09-14 16:50:30 +01002151 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002152
Chris Wilson07f73f62009-09-14 16:50:30 +01002153 continue;
Eric Anholt673a3942008-07-30 12:06:12 -07002154 }
2155
Zou Nan haid1b851f2010-05-21 09:08:57 +08002156 if (HAS_BSD(dev) && !list_empty(&bsd_ring->request_list)) {
2157 struct drm_i915_gem_request *request;
2158
2159 request = list_first_entry(&bsd_ring->request_list,
2160 struct drm_i915_gem_request,
2161 list);
2162
2163 ret = i915_wait_request(dev,
2164 request->seqno, request->ring);
2165 if (ret)
2166 return ret;
2167
2168 continue;
2169 }
2170
Eric Anholt673a3942008-07-30 12:06:12 -07002171 /* If we didn't have anything on the request list but there
2172 * are buffers awaiting a flush, emit one and try again.
2173 * When we wait on it, those buffers waiting for that flush
2174 * will get moved to inactive.
2175 */
2176 if (!list_empty(&dev_priv->mm.flushing_list)) {
Chris Wilson07f73f62009-09-14 16:50:30 +01002177 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07002178
Chris Wilson9a1e2582009-09-20 20:16:50 +01002179 /* Find an object that we can immediately reuse */
2180 list_for_each_entry(obj_priv, &dev_priv->mm.flushing_list, list) {
Daniel Vettera8089e82010-04-09 19:05:09 +00002181 obj = &obj_priv->base;
Chris Wilson9a1e2582009-09-20 20:16:50 +01002182 if (obj->size >= min_size)
2183 break;
Eric Anholt673a3942008-07-30 12:06:12 -07002184
Chris Wilson9a1e2582009-09-20 20:16:50 +01002185 obj = NULL;
2186 }
Eric Anholt673a3942008-07-30 12:06:12 -07002187
Chris Wilson9a1e2582009-09-20 20:16:50 +01002188 if (obj != NULL) {
2189 uint32_t seqno;
Chris Wilson07f73f62009-09-14 16:50:30 +01002190
Zou Nan hai852835f2010-05-21 09:08:56 +08002191 i915_gem_flush_ring(dev,
Chris Wilson9a1e2582009-09-20 20:16:50 +01002192 obj->write_domain,
Zou Nan hai852835f2010-05-21 09:08:56 +08002193 obj->write_domain,
2194 obj_priv->ring);
2195 seqno = i915_add_request(dev, NULL,
2196 obj->write_domain,
2197 obj_priv->ring);
Chris Wilson9a1e2582009-09-20 20:16:50 +01002198 if (seqno == 0)
2199 return -ENOMEM;
Chris Wilson9a1e2582009-09-20 20:16:50 +01002200 continue;
2201 }
Eric Anholt673a3942008-07-30 12:06:12 -07002202 }
2203
Chris Wilson07f73f62009-09-14 16:50:30 +01002204 /* If we didn't do any of the above, there's no single buffer
2205 * large enough to swap out for the new one, so just evict
2206 * everything and start again. (This should be rare.)
Eric Anholt673a3942008-07-30 12:06:12 -07002207 */
Chris Wilson97311292009-09-21 00:22:34 +01002208 if (!list_empty (&dev_priv->mm.inactive_list))
Chris Wilsonab5ee572009-09-20 19:25:47 +01002209 return i915_gem_evict_from_inactive_list(dev);
Chris Wilson97311292009-09-21 00:22:34 +01002210 else
Chris Wilson07f73f62009-09-14 16:50:30 +01002211 return i915_gem_evict_everything(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07002212 }
Keith Packardac94a962008-11-20 23:30:27 -08002213}
2214
Ben Gamari6911a9b2009-04-02 11:24:54 -07002215int
Chris Wilson4bdadb92010-01-27 13:36:32 +00002216i915_gem_object_get_pages(struct drm_gem_object *obj,
2217 gfp_t gfpmask)
Eric Anholt673a3942008-07-30 12:06:12 -07002218{
Daniel Vetter23010e42010-03-08 13:35:02 +01002219 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002220 int page_count, i;
2221 struct address_space *mapping;
2222 struct inode *inode;
2223 struct page *page;
Eric Anholt673a3942008-07-30 12:06:12 -07002224
Daniel Vetter778c3542010-05-13 11:49:44 +02002225 BUG_ON(obj_priv->pages_refcount
2226 == DRM_I915_GEM_OBJECT_MAX_PAGES_REFCOUNT);
2227
Eric Anholt856fa192009-03-19 14:10:50 -07002228 if (obj_priv->pages_refcount++ != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07002229 return 0;
2230
2231 /* Get the list of pages out of our struct file. They'll be pinned
2232 * at this point until we release them.
2233 */
2234 page_count = obj->size / PAGE_SIZE;
Eric Anholt856fa192009-03-19 14:10:50 -07002235 BUG_ON(obj_priv->pages != NULL);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07002236 obj_priv->pages = drm_calloc_large(page_count, sizeof(struct page *));
Eric Anholt856fa192009-03-19 14:10:50 -07002237 if (obj_priv->pages == NULL) {
Eric Anholt856fa192009-03-19 14:10:50 -07002238 obj_priv->pages_refcount--;
Eric Anholt673a3942008-07-30 12:06:12 -07002239 return -ENOMEM;
2240 }
2241
2242 inode = obj->filp->f_path.dentry->d_inode;
2243 mapping = inode->i_mapping;
2244 for (i = 0; i < page_count; i++) {
Chris Wilson4bdadb92010-01-27 13:36:32 +00002245 page = read_cache_page_gfp(mapping, i,
Linus Torvalds985b8232010-07-02 10:04:42 +10002246 GFP_HIGHUSER |
Chris Wilson4bdadb92010-01-27 13:36:32 +00002247 __GFP_COLD |
Linus Torvaldscd9f0402010-07-18 09:44:37 -07002248 __GFP_RECLAIMABLE |
Chris Wilson4bdadb92010-01-27 13:36:32 +00002249 gfpmask);
Chris Wilson1f2b1012010-03-12 19:52:55 +00002250 if (IS_ERR(page))
2251 goto err_pages;
2252
Eric Anholt856fa192009-03-19 14:10:50 -07002253 obj_priv->pages[i] = page;
Eric Anholt673a3942008-07-30 12:06:12 -07002254 }
Eric Anholt280b7132009-03-12 16:56:27 -07002255
2256 if (obj_priv->tiling_mode != I915_TILING_NONE)
2257 i915_gem_object_do_bit_17_swizzle(obj);
2258
Eric Anholt673a3942008-07-30 12:06:12 -07002259 return 0;
Chris Wilson1f2b1012010-03-12 19:52:55 +00002260
2261err_pages:
2262 while (i--)
2263 page_cache_release(obj_priv->pages[i]);
2264
2265 drm_free_large(obj_priv->pages);
2266 obj_priv->pages = NULL;
2267 obj_priv->pages_refcount--;
2268 return PTR_ERR(page);
Eric Anholt673a3942008-07-30 12:06:12 -07002269}
2270
Eric Anholt4e901fd2009-10-26 16:44:17 -07002271static void sandybridge_write_fence_reg(struct drm_i915_fence_reg *reg)
2272{
2273 struct drm_gem_object *obj = reg->obj;
2274 struct drm_device *dev = obj->dev;
2275 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002276 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt4e901fd2009-10-26 16:44:17 -07002277 int regnum = obj_priv->fence_reg;
2278 uint64_t val;
2279
2280 val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
2281 0xfffff000) << 32;
2282 val |= obj_priv->gtt_offset & 0xfffff000;
2283 val |= (uint64_t)((obj_priv->stride / 128) - 1) <<
2284 SANDYBRIDGE_FENCE_PITCH_SHIFT;
2285
2286 if (obj_priv->tiling_mode == I915_TILING_Y)
2287 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2288 val |= I965_FENCE_REG_VALID;
2289
2290 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (regnum * 8), val);
2291}
2292
Jesse Barnesde151cf2008-11-12 10:03:55 -08002293static void i965_write_fence_reg(struct drm_i915_fence_reg *reg)
2294{
2295 struct drm_gem_object *obj = reg->obj;
2296 struct drm_device *dev = obj->dev;
2297 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002298 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002299 int regnum = obj_priv->fence_reg;
2300 uint64_t val;
2301
2302 val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
2303 0xfffff000) << 32;
2304 val |= obj_priv->gtt_offset & 0xfffff000;
2305 val |= ((obj_priv->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
2306 if (obj_priv->tiling_mode == I915_TILING_Y)
2307 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2308 val |= I965_FENCE_REG_VALID;
2309
2310 I915_WRITE64(FENCE_REG_965_0 + (regnum * 8), val);
2311}
2312
2313static void i915_write_fence_reg(struct drm_i915_fence_reg *reg)
2314{
2315 struct drm_gem_object *obj = reg->obj;
2316 struct drm_device *dev = obj->dev;
2317 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002318 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002319 int regnum = obj_priv->fence_reg;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002320 int tile_width;
Eric Anholtdc529a42009-03-10 22:34:49 -07002321 uint32_t fence_reg, val;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002322 uint32_t pitch_val;
2323
2324 if ((obj_priv->gtt_offset & ~I915_FENCE_START_MASK) ||
2325 (obj_priv->gtt_offset & (obj->size - 1))) {
Linus Torvaldsf06da262009-02-09 08:57:29 -08002326 WARN(1, "%s: object 0x%08x not 1M or size (0x%zx) aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002327 __func__, obj_priv->gtt_offset, obj->size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002328 return;
2329 }
2330
Jesse Barnes0f973f22009-01-26 17:10:45 -08002331 if (obj_priv->tiling_mode == I915_TILING_Y &&
2332 HAS_128_BYTE_Y_TILING(dev))
2333 tile_width = 128;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002334 else
Jesse Barnes0f973f22009-01-26 17:10:45 -08002335 tile_width = 512;
2336
2337 /* Note: pitch better be a power of two tile widths */
2338 pitch_val = obj_priv->stride / tile_width;
2339 pitch_val = ffs(pitch_val) - 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002340
Daniel Vetterc36a2a62010-04-17 15:12:03 +02002341 if (obj_priv->tiling_mode == I915_TILING_Y &&
2342 HAS_128_BYTE_Y_TILING(dev))
2343 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2344 else
2345 WARN_ON(pitch_val > I915_FENCE_MAX_PITCH_VAL);
2346
Jesse Barnesde151cf2008-11-12 10:03:55 -08002347 val = obj_priv->gtt_offset;
2348 if (obj_priv->tiling_mode == I915_TILING_Y)
2349 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2350 val |= I915_FENCE_SIZE_BITS(obj->size);
2351 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2352 val |= I830_FENCE_REG_VALID;
2353
Eric Anholtdc529a42009-03-10 22:34:49 -07002354 if (regnum < 8)
2355 fence_reg = FENCE_REG_830_0 + (regnum * 4);
2356 else
2357 fence_reg = FENCE_REG_945_8 + ((regnum - 8) * 4);
2358 I915_WRITE(fence_reg, val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002359}
2360
2361static void i830_write_fence_reg(struct drm_i915_fence_reg *reg)
2362{
2363 struct drm_gem_object *obj = reg->obj;
2364 struct drm_device *dev = obj->dev;
2365 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002366 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002367 int regnum = obj_priv->fence_reg;
2368 uint32_t val;
2369 uint32_t pitch_val;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002370 uint32_t fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002371
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002372 if ((obj_priv->gtt_offset & ~I830_FENCE_START_MASK) ||
Jesse Barnesde151cf2008-11-12 10:03:55 -08002373 (obj_priv->gtt_offset & (obj->size - 1))) {
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002374 WARN(1, "%s: object 0x%08x not 512K or size aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002375 __func__, obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002376 return;
2377 }
2378
Eric Anholte76a16d2009-05-26 17:44:56 -07002379 pitch_val = obj_priv->stride / 128;
2380 pitch_val = ffs(pitch_val) - 1;
2381 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2382
Jesse Barnesde151cf2008-11-12 10:03:55 -08002383 val = obj_priv->gtt_offset;
2384 if (obj_priv->tiling_mode == I915_TILING_Y)
2385 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002386 fence_size_bits = I830_FENCE_SIZE_BITS(obj->size);
2387 WARN_ON(fence_size_bits & ~0x00000f00);
2388 val |= fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002389 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2390 val |= I830_FENCE_REG_VALID;
2391
2392 I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002393}
2394
Daniel Vetterae3db242010-02-19 11:51:58 +01002395static int i915_find_fence_reg(struct drm_device *dev)
2396{
2397 struct drm_i915_fence_reg *reg = NULL;
2398 struct drm_i915_gem_object *obj_priv = NULL;
2399 struct drm_i915_private *dev_priv = dev->dev_private;
2400 struct drm_gem_object *obj = NULL;
2401 int i, avail, ret;
2402
2403 /* First try to find a free reg */
2404 avail = 0;
2405 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2406 reg = &dev_priv->fence_regs[i];
2407 if (!reg->obj)
2408 return i;
2409
Daniel Vetter23010e42010-03-08 13:35:02 +01002410 obj_priv = to_intel_bo(reg->obj);
Daniel Vetterae3db242010-02-19 11:51:58 +01002411 if (!obj_priv->pin_count)
2412 avail++;
2413 }
2414
2415 if (avail == 0)
2416 return -ENOSPC;
2417
2418 /* None available, try to steal one or wait for a user to finish */
2419 i = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002420 list_for_each_entry(reg, &dev_priv->mm.fence_list,
2421 lru_list) {
2422 obj = reg->obj;
2423 obj_priv = to_intel_bo(obj);
Daniel Vetterae3db242010-02-19 11:51:58 +01002424
2425 if (obj_priv->pin_count)
2426 continue;
2427
2428 /* found one! */
2429 i = obj_priv->fence_reg;
2430 break;
2431 }
2432
2433 BUG_ON(i == I915_FENCE_REG_NONE);
2434
2435 /* We only have a reference on obj from the active list. put_fence_reg
2436 * might drop that one, causing a use-after-free in it. So hold a
2437 * private reference to obj like the other callers of put_fence_reg
2438 * (set_tiling ioctl) do. */
2439 drm_gem_object_reference(obj);
2440 ret = i915_gem_object_put_fence_reg(obj);
2441 drm_gem_object_unreference(obj);
2442 if (ret != 0)
2443 return ret;
2444
2445 return i;
2446}
2447
Jesse Barnesde151cf2008-11-12 10:03:55 -08002448/**
2449 * i915_gem_object_get_fence_reg - set up a fence reg for an object
2450 * @obj: object to map through a fence reg
2451 *
2452 * When mapping objects through the GTT, userspace wants to be able to write
2453 * to them without having to worry about swizzling if the object is tiled.
2454 *
2455 * This function walks the fence regs looking for a free one for @obj,
2456 * stealing one if it can't find any.
2457 *
2458 * It then sets up the reg based on the object's properties: address, pitch
2459 * and tiling format.
2460 */
Chris Wilson8c4b8c32009-06-17 22:08:52 +01002461int
2462i915_gem_object_get_fence_reg(struct drm_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002463{
2464 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002465 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002466 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002467 struct drm_i915_fence_reg *reg = NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002468 int ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002469
Eric Anholta09ba7f2009-08-29 12:49:51 -07002470 /* Just update our place in the LRU if our fence is getting used. */
2471 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002472 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
2473 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002474 return 0;
2475 }
2476
Jesse Barnesde151cf2008-11-12 10:03:55 -08002477 switch (obj_priv->tiling_mode) {
2478 case I915_TILING_NONE:
2479 WARN(1, "allocating a fence for non-tiled object?\n");
2480 break;
2481 case I915_TILING_X:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002482 if (!obj_priv->stride)
2483 return -EINVAL;
2484 WARN((obj_priv->stride & (512 - 1)),
2485 "object 0x%08x is X tiled but has non-512B pitch\n",
2486 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002487 break;
2488 case I915_TILING_Y:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002489 if (!obj_priv->stride)
2490 return -EINVAL;
2491 WARN((obj_priv->stride & (128 - 1)),
2492 "object 0x%08x is Y tiled but has non-128B pitch\n",
2493 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002494 break;
2495 }
2496
Daniel Vetterae3db242010-02-19 11:51:58 +01002497 ret = i915_find_fence_reg(dev);
2498 if (ret < 0)
2499 return ret;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002500
Daniel Vetterae3db242010-02-19 11:51:58 +01002501 obj_priv->fence_reg = ret;
2502 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002503 list_add_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002504
Jesse Barnesde151cf2008-11-12 10:03:55 -08002505 reg->obj = obj;
2506
Eric Anholt4e901fd2009-10-26 16:44:17 -07002507 if (IS_GEN6(dev))
2508 sandybridge_write_fence_reg(reg);
2509 else if (IS_I965G(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08002510 i965_write_fence_reg(reg);
2511 else if (IS_I9XX(dev))
2512 i915_write_fence_reg(reg);
2513 else
2514 i830_write_fence_reg(reg);
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002515
Daniel Vetterae3db242010-02-19 11:51:58 +01002516 trace_i915_gem_object_get_fence(obj, obj_priv->fence_reg,
2517 obj_priv->tiling_mode);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002518
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002519 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002520}
2521
2522/**
2523 * i915_gem_clear_fence_reg - clear out fence register info
2524 * @obj: object to clear
2525 *
2526 * Zeroes out the fence register itself and clears out the associated
2527 * data structures in dev_priv and obj_priv.
2528 */
2529static void
2530i915_gem_clear_fence_reg(struct drm_gem_object *obj)
2531{
2532 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002533 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002534 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002535 struct drm_i915_fence_reg *reg =
2536 &dev_priv->fence_regs[obj_priv->fence_reg];
Jesse Barnesde151cf2008-11-12 10:03:55 -08002537
Eric Anholt4e901fd2009-10-26 16:44:17 -07002538 if (IS_GEN6(dev)) {
2539 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 +
2540 (obj_priv->fence_reg * 8), 0);
2541 } else if (IS_I965G(dev)) {
Jesse Barnesde151cf2008-11-12 10:03:55 -08002542 I915_WRITE64(FENCE_REG_965_0 + (obj_priv->fence_reg * 8), 0);
Eric Anholt4e901fd2009-10-26 16:44:17 -07002543 } else {
Eric Anholtdc529a42009-03-10 22:34:49 -07002544 uint32_t fence_reg;
2545
2546 if (obj_priv->fence_reg < 8)
2547 fence_reg = FENCE_REG_830_0 + obj_priv->fence_reg * 4;
2548 else
2549 fence_reg = FENCE_REG_945_8 + (obj_priv->fence_reg -
2550 8) * 4;
2551
2552 I915_WRITE(fence_reg, 0);
2553 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002554
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002555 reg->obj = NULL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002556 obj_priv->fence_reg = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002557 list_del_init(&reg->lru_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002558}
2559
Eric Anholt673a3942008-07-30 12:06:12 -07002560/**
Chris Wilson52dc7d32009-06-06 09:46:01 +01002561 * i915_gem_object_put_fence_reg - waits on outstanding fenced access
2562 * to the buffer to finish, and then resets the fence register.
2563 * @obj: tiled object holding a fence register.
2564 *
2565 * Zeroes out the fence register itself and clears out the associated
2566 * data structures in dev_priv and obj_priv.
2567 */
2568int
2569i915_gem_object_put_fence_reg(struct drm_gem_object *obj)
2570{
2571 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01002572 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson52dc7d32009-06-06 09:46:01 +01002573
2574 if (obj_priv->fence_reg == I915_FENCE_REG_NONE)
2575 return 0;
2576
Daniel Vetter10ae9bd2010-02-01 13:59:17 +01002577 /* If we've changed tiling, GTT-mappings of the object
2578 * need to re-fault to ensure that the correct fence register
2579 * setup is in place.
2580 */
2581 i915_gem_release_mmap(obj);
2582
Chris Wilson52dc7d32009-06-06 09:46:01 +01002583 /* On the i915, GPU access to tiled buffers is via a fence,
2584 * therefore we must wait for any outstanding access to complete
2585 * before clearing the fence.
2586 */
2587 if (!IS_I965G(dev)) {
2588 int ret;
2589
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002590 ret = i915_gem_object_flush_gpu_write_domain(obj);
2591 if (ret != 0)
2592 return ret;
2593
Chris Wilson52dc7d32009-06-06 09:46:01 +01002594 ret = i915_gem_object_wait_rendering(obj);
2595 if (ret != 0)
2596 return ret;
2597 }
2598
Daniel Vetter4a726612010-02-01 13:59:16 +01002599 i915_gem_object_flush_gtt_write_domain(obj);
Chris Wilson52dc7d32009-06-06 09:46:01 +01002600 i915_gem_clear_fence_reg (obj);
2601
2602 return 0;
2603}
2604
2605/**
Eric Anholt673a3942008-07-30 12:06:12 -07002606 * Finds free space in the GTT aperture and binds the object there.
2607 */
2608static int
2609i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment)
2610{
2611 struct drm_device *dev = obj->dev;
2612 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002613 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002614 struct drm_mm_node *free_space;
Chris Wilson4bdadb92010-01-27 13:36:32 +00002615 gfp_t gfpmask = __GFP_NORETRY | __GFP_NOWARN;
Chris Wilson07f73f62009-09-14 16:50:30 +01002616 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002617
Chris Wilsonbb6baf72009-09-22 14:24:13 +01002618 if (obj_priv->madv != I915_MADV_WILLNEED) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01002619 DRM_ERROR("Attempting to bind a purgeable object\n");
2620 return -EINVAL;
2621 }
2622
Eric Anholt673a3942008-07-30 12:06:12 -07002623 if (alignment == 0)
Jesse Barnes0f973f22009-01-26 17:10:45 -08002624 alignment = i915_gem_get_gtt_alignment(obj);
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002625 if (alignment & (i915_gem_get_gtt_alignment(obj) - 1)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002626 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2627 return -EINVAL;
2628 }
2629
Chris Wilson654fc602010-05-27 13:18:21 +01002630 /* If the object is bigger than the entire aperture, reject it early
2631 * before evicting everything in a vain attempt to find space.
2632 */
2633 if (obj->size > dev->gtt_total) {
2634 DRM_ERROR("Attempting to bind an object larger than the aperture\n");
2635 return -E2BIG;
2636 }
2637
Eric Anholt673a3942008-07-30 12:06:12 -07002638 search_free:
2639 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
2640 obj->size, alignment, 0);
2641 if (free_space != NULL) {
2642 obj_priv->gtt_space = drm_mm_get_block(free_space, obj->size,
2643 alignment);
Daniel Vetterdb3307a2010-07-02 15:02:12 +01002644 if (obj_priv->gtt_space != NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002645 obj_priv->gtt_offset = obj_priv->gtt_space->start;
Eric Anholt673a3942008-07-30 12:06:12 -07002646 }
2647 if (obj_priv->gtt_space == NULL) {
2648 /* If the gtt is empty and we're still having trouble
2649 * fitting our object in, we're out of memory.
2650 */
2651#if WATCH_LRU
2652 DRM_INFO("%s: GTT full, evicting something\n", __func__);
2653#endif
Chris Wilson07f73f62009-09-14 16:50:30 +01002654 ret = i915_gem_evict_something(dev, obj->size);
Chris Wilson97311292009-09-21 00:22:34 +01002655 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002656 return ret;
Chris Wilson97311292009-09-21 00:22:34 +01002657
Eric Anholt673a3942008-07-30 12:06:12 -07002658 goto search_free;
2659 }
2660
2661#if WATCH_BUF
Krzysztof Halasacfd43c02009-06-20 00:31:28 +02002662 DRM_INFO("Binding object of size %zd at 0x%08x\n",
Eric Anholt673a3942008-07-30 12:06:12 -07002663 obj->size, obj_priv->gtt_offset);
2664#endif
Chris Wilson4bdadb92010-01-27 13:36:32 +00002665 ret = i915_gem_object_get_pages(obj, gfpmask);
Eric Anholt673a3942008-07-30 12:06:12 -07002666 if (ret) {
2667 drm_mm_put_block(obj_priv->gtt_space);
2668 obj_priv->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002669
2670 if (ret == -ENOMEM) {
2671 /* first try to clear up some space from the GTT */
2672 ret = i915_gem_evict_something(dev, obj->size);
2673 if (ret) {
Chris Wilson07f73f62009-09-14 16:50:30 +01002674 /* now try to shrink everyone else */
Chris Wilson4bdadb92010-01-27 13:36:32 +00002675 if (gfpmask) {
2676 gfpmask = 0;
2677 goto search_free;
Chris Wilson07f73f62009-09-14 16:50:30 +01002678 }
2679
2680 return ret;
2681 }
2682
2683 goto search_free;
2684 }
2685
Eric Anholt673a3942008-07-30 12:06:12 -07002686 return ret;
2687 }
2688
Eric Anholt673a3942008-07-30 12:06:12 -07002689 /* Create an AGP memory structure pointing at our pages, and bind it
2690 * into the GTT.
2691 */
2692 obj_priv->agp_mem = drm_agp_bind_pages(dev,
Eric Anholt856fa192009-03-19 14:10:50 -07002693 obj_priv->pages,
Chris Wilson07f73f62009-09-14 16:50:30 +01002694 obj->size >> PAGE_SHIFT,
Keith Packardba1eb1d2008-10-14 19:55:10 -07002695 obj_priv->gtt_offset,
2696 obj_priv->agp_type);
Eric Anholt673a3942008-07-30 12:06:12 -07002697 if (obj_priv->agp_mem == NULL) {
Eric Anholt856fa192009-03-19 14:10:50 -07002698 i915_gem_object_put_pages(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002699 drm_mm_put_block(obj_priv->gtt_space);
2700 obj_priv->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002701
2702 ret = i915_gem_evict_something(dev, obj->size);
Chris Wilson97311292009-09-21 00:22:34 +01002703 if (ret)
Chris Wilson07f73f62009-09-14 16:50:30 +01002704 return ret;
Chris Wilson07f73f62009-09-14 16:50:30 +01002705
2706 goto search_free;
Eric Anholt673a3942008-07-30 12:06:12 -07002707 }
2708 atomic_inc(&dev->gtt_count);
2709 atomic_add(obj->size, &dev->gtt_memory);
2710
2711 /* Assert that the object is not currently in any GPU domain. As it
2712 * wasn't in the GTT, there shouldn't be any way it could have been in
2713 * a GPU cache
2714 */
Chris Wilson21d509e2009-06-06 09:46:02 +01002715 BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS);
2716 BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS);
Eric Anholt673a3942008-07-30 12:06:12 -07002717
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002718 trace_i915_gem_object_bind(obj, obj_priv->gtt_offset);
2719
Eric Anholt673a3942008-07-30 12:06:12 -07002720 return 0;
2721}
2722
2723void
2724i915_gem_clflush_object(struct drm_gem_object *obj)
2725{
Daniel Vetter23010e42010-03-08 13:35:02 +01002726 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002727
2728 /* If we don't have a page list set up, then we're not pinned
2729 * to GPU, and we can ignore the cache flush because it'll happen
2730 * again at bind time.
2731 */
Eric Anholt856fa192009-03-19 14:10:50 -07002732 if (obj_priv->pages == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002733 return;
2734
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002735 trace_i915_gem_object_clflush(obj);
Eric Anholtcfa16a02009-05-26 18:46:16 -07002736
Eric Anholt856fa192009-03-19 14:10:50 -07002737 drm_clflush_pages(obj_priv->pages, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002738}
2739
Eric Anholte47c68e2008-11-14 13:35:19 -08002740/** Flushes any GPU write domain for the object if it's dirty. */
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002741static int
Eric Anholte47c68e2008-11-14 13:35:19 -08002742i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj)
2743{
2744 struct drm_device *dev = obj->dev;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002745 uint32_t old_write_domain;
Zou Nan hai852835f2010-05-21 09:08:56 +08002746 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08002747
2748 if ((obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002749 return 0;
Eric Anholte47c68e2008-11-14 13:35:19 -08002750
2751 /* Queue the GPU write cache flushing we need. */
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002752 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002753 i915_gem_flush(dev, 0, obj->write_domain);
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002754 if (i915_add_request(dev, NULL, obj->write_domain, obj_priv->ring) == 0)
2755 return -ENOMEM;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002756
2757 trace_i915_gem_object_change_domain(obj,
2758 obj->read_domains,
2759 old_write_domain);
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002760 return 0;
Eric Anholte47c68e2008-11-14 13:35:19 -08002761}
2762
2763/** Flushes the GTT write domain for the object if it's dirty. */
2764static void
2765i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj)
2766{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002767 uint32_t old_write_domain;
2768
Eric Anholte47c68e2008-11-14 13:35:19 -08002769 if (obj->write_domain != I915_GEM_DOMAIN_GTT)
2770 return;
2771
2772 /* No actual flushing is required for the GTT write domain. Writes
2773 * to it immediately go to main memory as far as we know, so there's
2774 * no chipset flush. It also doesn't land in render cache.
2775 */
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002776 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002777 obj->write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002778
2779 trace_i915_gem_object_change_domain(obj,
2780 obj->read_domains,
2781 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002782}
2783
2784/** Flushes the CPU write domain for the object if it's dirty. */
2785static void
2786i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj)
2787{
2788 struct drm_device *dev = obj->dev;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002789 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002790
2791 if (obj->write_domain != I915_GEM_DOMAIN_CPU)
2792 return;
2793
2794 i915_gem_clflush_object(obj);
2795 drm_agp_chipset_flush(dev);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002796 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002797 obj->write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002798
2799 trace_i915_gem_object_change_domain(obj,
2800 obj->read_domains,
2801 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002802}
2803
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002804int
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002805i915_gem_object_flush_write_domain(struct drm_gem_object *obj)
2806{
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002807 int ret = 0;
2808
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002809 switch (obj->write_domain) {
2810 case I915_GEM_DOMAIN_GTT:
2811 i915_gem_object_flush_gtt_write_domain(obj);
2812 break;
2813 case I915_GEM_DOMAIN_CPU:
2814 i915_gem_object_flush_cpu_write_domain(obj);
2815 break;
2816 default:
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002817 ret = i915_gem_object_flush_gpu_write_domain(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002818 break;
2819 }
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002820
2821 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002822}
2823
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002824/**
2825 * Moves a single object to the GTT read, and possibly write domain.
2826 *
2827 * This function returns when the move is complete, including waiting on
2828 * flushes to occur.
2829 */
Jesse Barnes79e53942008-11-07 14:24:08 -08002830int
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002831i915_gem_object_set_to_gtt_domain(struct drm_gem_object *obj, int write)
2832{
Daniel Vetter23010e42010-03-08 13:35:02 +01002833 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002834 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002835 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002836
Eric Anholt02354392008-11-26 13:58:13 -08002837 /* Not valid to be called on unbound objects. */
2838 if (obj_priv->gtt_space == NULL)
2839 return -EINVAL;
2840
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002841 ret = i915_gem_object_flush_gpu_write_domain(obj);
2842 if (ret != 0)
2843 return ret;
2844
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002845 /* Wait on any GPU rendering and flushing to occur. */
Eric Anholte47c68e2008-11-14 13:35:19 -08002846 ret = i915_gem_object_wait_rendering(obj);
2847 if (ret != 0)
2848 return ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002849
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002850 old_write_domain = obj->write_domain;
2851 old_read_domains = obj->read_domains;
2852
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002853 /* If we're writing through the GTT domain, then CPU and GPU caches
2854 * will need to be invalidated at next use.
2855 */
2856 if (write)
Eric Anholte47c68e2008-11-14 13:35:19 -08002857 obj->read_domains &= I915_GEM_DOMAIN_GTT;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002858
Eric Anholte47c68e2008-11-14 13:35:19 -08002859 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002860
2861 /* It should now be out of any other write domains, and we can update
2862 * the domain values for our changes.
2863 */
2864 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2865 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002866 if (write) {
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002867 obj->write_domain = I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002868 obj_priv->dirty = 1;
2869 }
2870
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002871 trace_i915_gem_object_change_domain(obj,
2872 old_read_domains,
2873 old_write_domain);
2874
Eric Anholte47c68e2008-11-14 13:35:19 -08002875 return 0;
2876}
2877
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002878/*
2879 * Prepare buffer for display plane. Use uninterruptible for possible flush
2880 * wait, as in modesetting process we're not supposed to be interrupted.
2881 */
2882int
2883i915_gem_object_set_to_display_plane(struct drm_gem_object *obj)
2884{
2885 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01002886 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002887 uint32_t old_write_domain, old_read_domains;
2888 int ret;
2889
2890 /* Not valid to be called on unbound objects. */
2891 if (obj_priv->gtt_space == NULL)
2892 return -EINVAL;
2893
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002894 ret = i915_gem_object_flush_gpu_write_domain(obj);
2895 if (ret)
2896 return ret;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002897
2898 /* Wait on any GPU rendering and flushing to occur. */
2899 if (obj_priv->active) {
2900#if WATCH_BUF
2901 DRM_INFO("%s: object %p wait for seqno %08x\n",
2902 __func__, obj, obj_priv->last_rendering_seqno);
2903#endif
Zou Nan hai852835f2010-05-21 09:08:56 +08002904 ret = i915_do_wait_request(dev,
2905 obj_priv->last_rendering_seqno,
2906 0,
2907 obj_priv->ring);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002908 if (ret != 0)
2909 return ret;
2910 }
2911
Chris Wilsonb118c1e2010-05-27 13:18:14 +01002912 i915_gem_object_flush_cpu_write_domain(obj);
2913
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002914 old_write_domain = obj->write_domain;
2915 old_read_domains = obj->read_domains;
2916
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002917 /* It should now be out of any other write domains, and we can update
2918 * the domain values for our changes.
2919 */
2920 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
Chris Wilsonb118c1e2010-05-27 13:18:14 +01002921 obj->read_domains = I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002922 obj->write_domain = I915_GEM_DOMAIN_GTT;
2923 obj_priv->dirty = 1;
2924
2925 trace_i915_gem_object_change_domain(obj,
2926 old_read_domains,
2927 old_write_domain);
2928
2929 return 0;
2930}
2931
Eric Anholte47c68e2008-11-14 13:35:19 -08002932/**
2933 * Moves a single object to the CPU read, and possibly write domain.
2934 *
2935 * This function returns when the move is complete, including waiting on
2936 * flushes to occur.
2937 */
2938static int
2939i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj, int write)
2940{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002941 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002942 int ret;
2943
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002944 ret = i915_gem_object_flush_gpu_write_domain(obj);
2945 if (ret)
2946 return ret;
2947
Eric Anholte47c68e2008-11-14 13:35:19 -08002948 /* Wait on any GPU rendering and flushing to occur. */
2949 ret = i915_gem_object_wait_rendering(obj);
2950 if (ret != 0)
2951 return ret;
2952
2953 i915_gem_object_flush_gtt_write_domain(obj);
2954
2955 /* If we have a partially-valid cache of the object in the CPU,
2956 * finish invalidating it and free the per-page flags.
2957 */
2958 i915_gem_object_set_to_full_cpu_read_domain(obj);
2959
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002960 old_write_domain = obj->write_domain;
2961 old_read_domains = obj->read_domains;
2962
Eric Anholte47c68e2008-11-14 13:35:19 -08002963 /* Flush the CPU cache if it's still invalid. */
2964 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
2965 i915_gem_clflush_object(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08002966
2967 obj->read_domains |= I915_GEM_DOMAIN_CPU;
2968 }
2969
2970 /* It should now be out of any other write domains, and we can update
2971 * the domain values for our changes.
2972 */
2973 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
2974
2975 /* If we're writing through the CPU, then the GPU read domains will
2976 * need to be invalidated at next use.
2977 */
2978 if (write) {
2979 obj->read_domains &= I915_GEM_DOMAIN_CPU;
2980 obj->write_domain = I915_GEM_DOMAIN_CPU;
2981 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002982
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002983 trace_i915_gem_object_change_domain(obj,
2984 old_read_domains,
2985 old_write_domain);
2986
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002987 return 0;
2988}
2989
Eric Anholt673a3942008-07-30 12:06:12 -07002990/*
2991 * Set the next domain for the specified object. This
2992 * may not actually perform the necessary flushing/invaliding though,
2993 * as that may want to be batched with other set_domain operations
2994 *
2995 * This is (we hope) the only really tricky part of gem. The goal
2996 * is fairly simple -- track which caches hold bits of the object
2997 * and make sure they remain coherent. A few concrete examples may
2998 * help to explain how it works. For shorthand, we use the notation
2999 * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
3000 * a pair of read and write domain masks.
3001 *
3002 * Case 1: the batch buffer
3003 *
3004 * 1. Allocated
3005 * 2. Written by CPU
3006 * 3. Mapped to GTT
3007 * 4. Read by GPU
3008 * 5. Unmapped from GTT
3009 * 6. Freed
3010 *
3011 * Let's take these a step at a time
3012 *
3013 * 1. Allocated
3014 * Pages allocated from the kernel may still have
3015 * cache contents, so we set them to (CPU, CPU) always.
3016 * 2. Written by CPU (using pwrite)
3017 * The pwrite function calls set_domain (CPU, CPU) and
3018 * this function does nothing (as nothing changes)
3019 * 3. Mapped by GTT
3020 * This function asserts that the object is not
3021 * currently in any GPU-based read or write domains
3022 * 4. Read by GPU
3023 * i915_gem_execbuffer calls set_domain (COMMAND, 0).
3024 * As write_domain is zero, this function adds in the
3025 * current read domains (CPU+COMMAND, 0).
3026 * flush_domains is set to CPU.
3027 * invalidate_domains is set to COMMAND
3028 * clflush is run to get data out of the CPU caches
3029 * then i915_dev_set_domain calls i915_gem_flush to
3030 * emit an MI_FLUSH and drm_agp_chipset_flush
3031 * 5. Unmapped from GTT
3032 * i915_gem_object_unbind calls set_domain (CPU, CPU)
3033 * flush_domains and invalidate_domains end up both zero
3034 * so no flushing/invalidating happens
3035 * 6. Freed
3036 * yay, done
3037 *
3038 * Case 2: The shared render buffer
3039 *
3040 * 1. Allocated
3041 * 2. Mapped to GTT
3042 * 3. Read/written by GPU
3043 * 4. set_domain to (CPU,CPU)
3044 * 5. Read/written by CPU
3045 * 6. Read/written by GPU
3046 *
3047 * 1. Allocated
3048 * Same as last example, (CPU, CPU)
3049 * 2. Mapped to GTT
3050 * Nothing changes (assertions find that it is not in the GPU)
3051 * 3. Read/written by GPU
3052 * execbuffer calls set_domain (RENDER, RENDER)
3053 * flush_domains gets CPU
3054 * invalidate_domains gets GPU
3055 * clflush (obj)
3056 * MI_FLUSH and drm_agp_chipset_flush
3057 * 4. set_domain (CPU, CPU)
3058 * flush_domains gets GPU
3059 * invalidate_domains gets CPU
3060 * wait_rendering (obj) to make sure all drawing is complete.
3061 * This will include an MI_FLUSH to get the data from GPU
3062 * to memory
3063 * clflush (obj) to invalidate the CPU cache
3064 * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
3065 * 5. Read/written by CPU
3066 * cache lines are loaded and dirtied
3067 * 6. Read written by GPU
3068 * Same as last GPU access
3069 *
3070 * Case 3: The constant buffer
3071 *
3072 * 1. Allocated
3073 * 2. Written by CPU
3074 * 3. Read by GPU
3075 * 4. Updated (written) by CPU again
3076 * 5. Read by GPU
3077 *
3078 * 1. Allocated
3079 * (CPU, CPU)
3080 * 2. Written by CPU
3081 * (CPU, CPU)
3082 * 3. Read by GPU
3083 * (CPU+RENDER, 0)
3084 * flush_domains = CPU
3085 * invalidate_domains = RENDER
3086 * clflush (obj)
3087 * MI_FLUSH
3088 * drm_agp_chipset_flush
3089 * 4. Updated (written) by CPU again
3090 * (CPU, CPU)
3091 * flush_domains = 0 (no previous write domain)
3092 * invalidate_domains = 0 (no new read domains)
3093 * 5. Read by GPU
3094 * (CPU+RENDER, 0)
3095 * flush_domains = CPU
3096 * invalidate_domains = RENDER
3097 * clflush (obj)
3098 * MI_FLUSH
3099 * drm_agp_chipset_flush
3100 */
Keith Packardc0d90822008-11-20 23:11:08 -08003101static void
Eric Anholt8b0e3782009-02-19 14:40:50 -08003102i915_gem_object_set_to_gpu_domain(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07003103{
3104 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01003105 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003106 uint32_t invalidate_domains = 0;
3107 uint32_t flush_domains = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003108 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003109
Eric Anholt8b0e3782009-02-19 14:40:50 -08003110 BUG_ON(obj->pending_read_domains & I915_GEM_DOMAIN_CPU);
3111 BUG_ON(obj->pending_write_domain == I915_GEM_DOMAIN_CPU);
Eric Anholt673a3942008-07-30 12:06:12 -07003112
Jesse Barnes652c3932009-08-17 13:31:43 -07003113 intel_mark_busy(dev, obj);
3114
Eric Anholt673a3942008-07-30 12:06:12 -07003115#if WATCH_BUF
3116 DRM_INFO("%s: object %p read %08x -> %08x write %08x -> %08x\n",
3117 __func__, obj,
Eric Anholt8b0e3782009-02-19 14:40:50 -08003118 obj->read_domains, obj->pending_read_domains,
3119 obj->write_domain, obj->pending_write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07003120#endif
3121 /*
3122 * If the object isn't moving to a new write domain,
3123 * let the object stay in multiple read domains
3124 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003125 if (obj->pending_write_domain == 0)
3126 obj->pending_read_domains |= obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07003127 else
3128 obj_priv->dirty = 1;
3129
3130 /*
3131 * Flush the current write domain if
3132 * the new read domains don't match. Invalidate
3133 * any read domains which differ from the old
3134 * write domain
3135 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003136 if (obj->write_domain &&
3137 obj->write_domain != obj->pending_read_domains) {
Eric Anholt673a3942008-07-30 12:06:12 -07003138 flush_domains |= obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08003139 invalidate_domains |=
3140 obj->pending_read_domains & ~obj->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07003141 }
3142 /*
3143 * Invalidate any read caches which may have
3144 * stale data. That is, any new read domains.
3145 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003146 invalidate_domains |= obj->pending_read_domains & ~obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07003147 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU) {
3148#if WATCH_BUF
3149 DRM_INFO("%s: CPU domain flush %08x invalidate %08x\n",
3150 __func__, flush_domains, invalidate_domains);
3151#endif
Eric Anholt673a3942008-07-30 12:06:12 -07003152 i915_gem_clflush_object(obj);
3153 }
3154
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003155 old_read_domains = obj->read_domains;
3156
Eric Anholtefbeed92009-02-19 14:54:51 -08003157 /* The actual obj->write_domain will be updated with
3158 * pending_write_domain after we emit the accumulated flush for all
3159 * of our domain changes in execbuffers (which clears objects'
3160 * write_domains). So if we have a current write domain that we
3161 * aren't changing, set pending_write_domain to that.
3162 */
3163 if (flush_domains == 0 && obj->pending_write_domain == 0)
3164 obj->pending_write_domain = obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08003165 obj->read_domains = obj->pending_read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07003166
3167 dev->invalidate_domains |= invalidate_domains;
3168 dev->flush_domains |= flush_domains;
3169#if WATCH_BUF
3170 DRM_INFO("%s: read %08x write %08x invalidate %08x flush %08x\n",
3171 __func__,
3172 obj->read_domains, obj->write_domain,
3173 dev->invalidate_domains, dev->flush_domains);
3174#endif
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003175
3176 trace_i915_gem_object_change_domain(obj,
3177 old_read_domains,
3178 obj->write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07003179}
3180
3181/**
Eric Anholte47c68e2008-11-14 13:35:19 -08003182 * Moves the object from a partially CPU read to a full one.
Eric Anholt673a3942008-07-30 12:06:12 -07003183 *
Eric Anholte47c68e2008-11-14 13:35:19 -08003184 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
3185 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
3186 */
3187static void
3188i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj)
3189{
Daniel Vetter23010e42010-03-08 13:35:02 +01003190 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003191
3192 if (!obj_priv->page_cpu_valid)
3193 return;
3194
3195 /* If we're partially in the CPU read domain, finish moving it in.
3196 */
3197 if (obj->read_domains & I915_GEM_DOMAIN_CPU) {
3198 int i;
3199
3200 for (i = 0; i <= (obj->size - 1) / PAGE_SIZE; i++) {
3201 if (obj_priv->page_cpu_valid[i])
3202 continue;
Eric Anholt856fa192009-03-19 14:10:50 -07003203 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholte47c68e2008-11-14 13:35:19 -08003204 }
Eric Anholte47c68e2008-11-14 13:35:19 -08003205 }
3206
3207 /* Free the page_cpu_valid mappings which are now stale, whether
3208 * or not we've got I915_GEM_DOMAIN_CPU.
3209 */
Eric Anholt9a298b22009-03-24 12:23:04 -07003210 kfree(obj_priv->page_cpu_valid);
Eric Anholte47c68e2008-11-14 13:35:19 -08003211 obj_priv->page_cpu_valid = NULL;
3212}
3213
3214/**
3215 * Set the CPU read domain on a range of the object.
3216 *
3217 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
3218 * not entirely valid. The page_cpu_valid member of the object flags which
3219 * pages have been flushed, and will be respected by
3220 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
3221 * of the whole object.
3222 *
3223 * This function returns when the move is complete, including waiting on
3224 * flushes to occur.
Eric Anholt673a3942008-07-30 12:06:12 -07003225 */
3226static int
Eric Anholte47c68e2008-11-14 13:35:19 -08003227i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
3228 uint64_t offset, uint64_t size)
Eric Anholt673a3942008-07-30 12:06:12 -07003229{
Daniel Vetter23010e42010-03-08 13:35:02 +01003230 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003231 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003232 int i, ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003233
Eric Anholte47c68e2008-11-14 13:35:19 -08003234 if (offset == 0 && size == obj->size)
3235 return i915_gem_object_set_to_cpu_domain(obj, 0);
3236
Chris Wilson2dafb1e2010-06-07 14:03:05 +01003237 ret = i915_gem_object_flush_gpu_write_domain(obj);
3238 if (ret)
3239 return ret;
3240
Eric Anholte47c68e2008-11-14 13:35:19 -08003241 /* Wait on any GPU rendering and flushing to occur. */
3242 ret = i915_gem_object_wait_rendering(obj);
3243 if (ret != 0)
3244 return ret;
3245 i915_gem_object_flush_gtt_write_domain(obj);
3246
3247 /* If we're already fully in the CPU read domain, we're done. */
3248 if (obj_priv->page_cpu_valid == NULL &&
3249 (obj->read_domains & I915_GEM_DOMAIN_CPU) != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07003250 return 0;
3251
Eric Anholte47c68e2008-11-14 13:35:19 -08003252 /* Otherwise, create/clear the per-page CPU read domain flag if we're
3253 * newly adding I915_GEM_DOMAIN_CPU
3254 */
Eric Anholt673a3942008-07-30 12:06:12 -07003255 if (obj_priv->page_cpu_valid == NULL) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003256 obj_priv->page_cpu_valid = kzalloc(obj->size / PAGE_SIZE,
3257 GFP_KERNEL);
Eric Anholte47c68e2008-11-14 13:35:19 -08003258 if (obj_priv->page_cpu_valid == NULL)
3259 return -ENOMEM;
3260 } else if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0)
3261 memset(obj_priv->page_cpu_valid, 0, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07003262
3263 /* Flush the cache on any pages that are still invalid from the CPU's
3264 * perspective.
3265 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003266 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
3267 i++) {
Eric Anholt673a3942008-07-30 12:06:12 -07003268 if (obj_priv->page_cpu_valid[i])
3269 continue;
3270
Eric Anholt856fa192009-03-19 14:10:50 -07003271 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07003272
3273 obj_priv->page_cpu_valid[i] = 1;
3274 }
3275
Eric Anholte47c68e2008-11-14 13:35:19 -08003276 /* It should now be out of any other write domains, and we can update
3277 * the domain values for our changes.
3278 */
3279 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
3280
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003281 old_read_domains = obj->read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003282 obj->read_domains |= I915_GEM_DOMAIN_CPU;
3283
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003284 trace_i915_gem_object_change_domain(obj,
3285 old_read_domains,
3286 obj->write_domain);
3287
Eric Anholt673a3942008-07-30 12:06:12 -07003288 return 0;
3289}
3290
3291/**
Eric Anholt673a3942008-07-30 12:06:12 -07003292 * Pin an object to the GTT and evaluate the relocations landing in it.
3293 */
3294static int
3295i915_gem_object_pin_and_relocate(struct drm_gem_object *obj,
3296 struct drm_file *file_priv,
Jesse Barnes76446ca2009-12-17 22:05:42 -05003297 struct drm_i915_gem_exec_object2 *entry,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003298 struct drm_i915_gem_relocation_entry *relocs)
Eric Anholt673a3942008-07-30 12:06:12 -07003299{
3300 struct drm_device *dev = obj->dev;
Keith Packard0839ccb2008-10-30 19:38:48 -07003301 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01003302 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003303 int i, ret;
Keith Packard0839ccb2008-10-30 19:38:48 -07003304 void __iomem *reloc_page;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003305 bool need_fence;
3306
3307 need_fence = entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
3308 obj_priv->tiling_mode != I915_TILING_NONE;
3309
3310 /* Check fence reg constraints and rebind if necessary */
Chris Wilson808b24d62010-05-27 13:18:15 +01003311 if (need_fence &&
3312 !i915_gem_object_fence_offset_ok(obj,
3313 obj_priv->tiling_mode)) {
3314 ret = i915_gem_object_unbind(obj);
3315 if (ret)
3316 return ret;
3317 }
Eric Anholt673a3942008-07-30 12:06:12 -07003318
3319 /* Choose the GTT offset for our buffer and put it there. */
3320 ret = i915_gem_object_pin(obj, (uint32_t) entry->alignment);
3321 if (ret)
3322 return ret;
3323
Jesse Barnes76446ca2009-12-17 22:05:42 -05003324 /*
3325 * Pre-965 chips need a fence register set up in order to
3326 * properly handle blits to/from tiled surfaces.
3327 */
3328 if (need_fence) {
3329 ret = i915_gem_object_get_fence_reg(obj);
3330 if (ret != 0) {
Jesse Barnes76446ca2009-12-17 22:05:42 -05003331 i915_gem_object_unpin(obj);
3332 return ret;
3333 }
3334 }
3335
Eric Anholt673a3942008-07-30 12:06:12 -07003336 entry->offset = obj_priv->gtt_offset;
3337
Eric Anholt673a3942008-07-30 12:06:12 -07003338 /* Apply the relocations, using the GTT aperture to avoid cache
3339 * flushing requirements.
3340 */
3341 for (i = 0; i < entry->relocation_count; i++) {
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003342 struct drm_i915_gem_relocation_entry *reloc= &relocs[i];
Eric Anholt673a3942008-07-30 12:06:12 -07003343 struct drm_gem_object *target_obj;
3344 struct drm_i915_gem_object *target_obj_priv;
Eric Anholt3043c602008-10-02 12:24:47 -07003345 uint32_t reloc_val, reloc_offset;
3346 uint32_t __iomem *reloc_entry;
Eric Anholt673a3942008-07-30 12:06:12 -07003347
Eric Anholt673a3942008-07-30 12:06:12 -07003348 target_obj = drm_gem_object_lookup(obj->dev, file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003349 reloc->target_handle);
Eric Anholt673a3942008-07-30 12:06:12 -07003350 if (target_obj == NULL) {
3351 i915_gem_object_unpin(obj);
3352 return -EBADF;
3353 }
Daniel Vetter23010e42010-03-08 13:35:02 +01003354 target_obj_priv = to_intel_bo(target_obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003355
Chris Wilson8542a0b2009-09-09 21:15:15 +01003356#if WATCH_RELOC
3357 DRM_INFO("%s: obj %p offset %08x target %d "
3358 "read %08x write %08x gtt %08x "
3359 "presumed %08x delta %08x\n",
3360 __func__,
3361 obj,
3362 (int) reloc->offset,
3363 (int) reloc->target_handle,
3364 (int) reloc->read_domains,
3365 (int) reloc->write_domain,
3366 (int) target_obj_priv->gtt_offset,
3367 (int) reloc->presumed_offset,
3368 reloc->delta);
3369#endif
3370
Eric Anholt673a3942008-07-30 12:06:12 -07003371 /* The target buffer should have appeared before us in the
3372 * exec_object list, so it should have a GTT space bound by now.
3373 */
3374 if (target_obj_priv->gtt_space == NULL) {
3375 DRM_ERROR("No GTT space found for object %d\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003376 reloc->target_handle);
Eric Anholt673a3942008-07-30 12:06:12 -07003377 drm_gem_object_unreference(target_obj);
3378 i915_gem_object_unpin(obj);
3379 return -EINVAL;
3380 }
3381
Chris Wilson8542a0b2009-09-09 21:15:15 +01003382 /* Validate that the target is in a valid r/w GPU domain */
Daniel Vetter16edd552010-02-19 11:52:02 +01003383 if (reloc->write_domain & (reloc->write_domain - 1)) {
3384 DRM_ERROR("reloc with multiple write domains: "
3385 "obj %p target %d offset %d "
3386 "read %08x write %08x",
3387 obj, reloc->target_handle,
3388 (int) reloc->offset,
3389 reloc->read_domains,
3390 reloc->write_domain);
3391 return -EINVAL;
3392 }
Chris Wilson8542a0b2009-09-09 21:15:15 +01003393 if (reloc->write_domain & I915_GEM_DOMAIN_CPU ||
3394 reloc->read_domains & I915_GEM_DOMAIN_CPU) {
3395 DRM_ERROR("reloc with read/write CPU domains: "
3396 "obj %p target %d offset %d "
3397 "read %08x write %08x",
3398 obj, reloc->target_handle,
3399 (int) reloc->offset,
3400 reloc->read_domains,
3401 reloc->write_domain);
3402 drm_gem_object_unreference(target_obj);
3403 i915_gem_object_unpin(obj);
3404 return -EINVAL;
3405 }
3406 if (reloc->write_domain && target_obj->pending_write_domain &&
3407 reloc->write_domain != target_obj->pending_write_domain) {
3408 DRM_ERROR("Write domain conflict: "
3409 "obj %p target %d offset %d "
3410 "new %08x old %08x\n",
3411 obj, reloc->target_handle,
3412 (int) reloc->offset,
3413 reloc->write_domain,
3414 target_obj->pending_write_domain);
3415 drm_gem_object_unreference(target_obj);
3416 i915_gem_object_unpin(obj);
3417 return -EINVAL;
3418 }
3419
3420 target_obj->pending_read_domains |= reloc->read_domains;
3421 target_obj->pending_write_domain |= reloc->write_domain;
3422
3423 /* If the relocation already has the right value in it, no
3424 * more work needs to be done.
3425 */
3426 if (target_obj_priv->gtt_offset == reloc->presumed_offset) {
3427 drm_gem_object_unreference(target_obj);
3428 continue;
3429 }
3430
3431 /* Check that the relocation address is valid... */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003432 if (reloc->offset > obj->size - 4) {
Eric Anholt673a3942008-07-30 12:06:12 -07003433 DRM_ERROR("Relocation beyond object bounds: "
3434 "obj %p target %d offset %d size %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003435 obj, reloc->target_handle,
3436 (int) reloc->offset, (int) obj->size);
Eric Anholt673a3942008-07-30 12:06:12 -07003437 drm_gem_object_unreference(target_obj);
3438 i915_gem_object_unpin(obj);
3439 return -EINVAL;
3440 }
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003441 if (reloc->offset & 3) {
Eric Anholt673a3942008-07-30 12:06:12 -07003442 DRM_ERROR("Relocation not 4-byte aligned: "
3443 "obj %p target %d offset %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003444 obj, reloc->target_handle,
3445 (int) reloc->offset);
Eric Anholt673a3942008-07-30 12:06:12 -07003446 drm_gem_object_unreference(target_obj);
3447 i915_gem_object_unpin(obj);
3448 return -EINVAL;
3449 }
3450
Chris Wilson8542a0b2009-09-09 21:15:15 +01003451 /* and points to somewhere within the target object. */
Chris Wilsoncd0b9fb2009-09-15 23:23:18 +01003452 if (reloc->delta >= target_obj->size) {
3453 DRM_ERROR("Relocation beyond target object bounds: "
3454 "obj %p target %d delta %d size %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003455 obj, reloc->target_handle,
Chris Wilsoncd0b9fb2009-09-15 23:23:18 +01003456 (int) reloc->delta, (int) target_obj->size);
Chris Wilson491152b2009-02-11 14:26:32 +00003457 drm_gem_object_unreference(target_obj);
3458 i915_gem_object_unpin(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003459 return -EINVAL;
3460 }
3461
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003462 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
3463 if (ret != 0) {
3464 drm_gem_object_unreference(target_obj);
3465 i915_gem_object_unpin(obj);
3466 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -07003467 }
3468
3469 /* Map the page containing the relocation we're going to
3470 * perform.
3471 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003472 reloc_offset = obj_priv->gtt_offset + reloc->offset;
Keith Packard0839ccb2008-10-30 19:38:48 -07003473 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
3474 (reloc_offset &
3475 ~(PAGE_SIZE - 1)));
Eric Anholt3043c602008-10-02 12:24:47 -07003476 reloc_entry = (uint32_t __iomem *)(reloc_page +
Keith Packard0839ccb2008-10-30 19:38:48 -07003477 (reloc_offset & (PAGE_SIZE - 1)));
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003478 reloc_val = target_obj_priv->gtt_offset + reloc->delta;
Eric Anholt673a3942008-07-30 12:06:12 -07003479
3480#if WATCH_BUF
3481 DRM_INFO("Applied relocation: %p@0x%08x %08x -> %08x\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003482 obj, (unsigned int) reloc->offset,
Eric Anholt673a3942008-07-30 12:06:12 -07003483 readl(reloc_entry), reloc_val);
3484#endif
3485 writel(reloc_val, reloc_entry);
Keith Packard0839ccb2008-10-30 19:38:48 -07003486 io_mapping_unmap_atomic(reloc_page);
Eric Anholt673a3942008-07-30 12:06:12 -07003487
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003488 /* The updated presumed offset for this entry will be
3489 * copied back out to the user.
Eric Anholt673a3942008-07-30 12:06:12 -07003490 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003491 reloc->presumed_offset = target_obj_priv->gtt_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003492
3493 drm_gem_object_unreference(target_obj);
3494 }
3495
Eric Anholt673a3942008-07-30 12:06:12 -07003496#if WATCH_BUF
3497 if (0)
3498 i915_gem_dump_object(obj, 128, __func__, ~0);
3499#endif
3500 return 0;
3501}
3502
Eric Anholt673a3942008-07-30 12:06:12 -07003503/* Throttle our rendering by waiting until the ring has completed our requests
3504 * emitted over 20 msec ago.
3505 *
Eric Anholtb9624422009-06-03 07:27:35 +00003506 * Note that if we were to use the current jiffies each time around the loop,
3507 * we wouldn't escape the function with any frames outstanding if the time to
3508 * render a frame was over 20ms.
3509 *
Eric Anholt673a3942008-07-30 12:06:12 -07003510 * This should get us reasonable parallelism between CPU and GPU but also
3511 * relatively low latency when blocking on a particular request to finish.
3512 */
3513static int
3514i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file_priv)
3515{
3516 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
3517 int ret = 0;
Eric Anholtb9624422009-06-03 07:27:35 +00003518 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
Eric Anholt673a3942008-07-30 12:06:12 -07003519
3520 mutex_lock(&dev->struct_mutex);
Eric Anholtb9624422009-06-03 07:27:35 +00003521 while (!list_empty(&i915_file_priv->mm.request_list)) {
3522 struct drm_i915_gem_request *request;
3523
3524 request = list_first_entry(&i915_file_priv->mm.request_list,
3525 struct drm_i915_gem_request,
3526 client_list);
3527
3528 if (time_after_eq(request->emitted_jiffies, recent_enough))
3529 break;
3530
Zou Nan hai852835f2010-05-21 09:08:56 +08003531 ret = i915_wait_request(dev, request->seqno, request->ring);
Eric Anholtb9624422009-06-03 07:27:35 +00003532 if (ret != 0)
3533 break;
3534 }
Eric Anholt673a3942008-07-30 12:06:12 -07003535 mutex_unlock(&dev->struct_mutex);
Eric Anholtb9624422009-06-03 07:27:35 +00003536
Eric Anholt673a3942008-07-30 12:06:12 -07003537 return ret;
3538}
3539
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003540static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003541i915_gem_get_relocs_from_user(struct drm_i915_gem_exec_object2 *exec_list,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003542 uint32_t buffer_count,
3543 struct drm_i915_gem_relocation_entry **relocs)
3544{
3545 uint32_t reloc_count = 0, reloc_index = 0, i;
3546 int ret;
3547
3548 *relocs = NULL;
3549 for (i = 0; i < buffer_count; i++) {
3550 if (reloc_count + exec_list[i].relocation_count < reloc_count)
3551 return -EINVAL;
3552 reloc_count += exec_list[i].relocation_count;
3553 }
3554
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003555 *relocs = drm_calloc_large(reloc_count, sizeof(**relocs));
Jesse Barnes76446ca2009-12-17 22:05:42 -05003556 if (*relocs == NULL) {
3557 DRM_ERROR("failed to alloc relocs, count %d\n", reloc_count);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003558 return -ENOMEM;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003559 }
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003560
3561 for (i = 0; i < buffer_count; i++) {
3562 struct drm_i915_gem_relocation_entry __user *user_relocs;
3563
3564 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3565
3566 ret = copy_from_user(&(*relocs)[reloc_index],
3567 user_relocs,
3568 exec_list[i].relocation_count *
3569 sizeof(**relocs));
3570 if (ret != 0) {
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003571 drm_free_large(*relocs);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003572 *relocs = NULL;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003573 return -EFAULT;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003574 }
3575
3576 reloc_index += exec_list[i].relocation_count;
3577 }
3578
Florian Mickler2bc43b52009-04-06 22:55:41 +02003579 return 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003580}
3581
3582static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003583i915_gem_put_relocs_to_user(struct drm_i915_gem_exec_object2 *exec_list,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003584 uint32_t buffer_count,
3585 struct drm_i915_gem_relocation_entry *relocs)
3586{
3587 uint32_t reloc_count = 0, i;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003588 int ret = 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003589
Chris Wilson93533c22010-01-31 10:40:48 +00003590 if (relocs == NULL)
3591 return 0;
3592
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003593 for (i = 0; i < buffer_count; i++) {
3594 struct drm_i915_gem_relocation_entry __user *user_relocs;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003595 int unwritten;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003596
3597 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3598
Florian Mickler2bc43b52009-04-06 22:55:41 +02003599 unwritten = copy_to_user(user_relocs,
3600 &relocs[reloc_count],
3601 exec_list[i].relocation_count *
3602 sizeof(*relocs));
3603
3604 if (unwritten) {
3605 ret = -EFAULT;
3606 goto err;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003607 }
3608
3609 reloc_count += exec_list[i].relocation_count;
3610 }
3611
Florian Mickler2bc43b52009-04-06 22:55:41 +02003612err:
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003613 drm_free_large(relocs);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003614
3615 return ret;
3616}
3617
Chris Wilson83d60792009-06-06 09:45:57 +01003618static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003619i915_gem_check_execbuffer (struct drm_i915_gem_execbuffer2 *exec,
Chris Wilson83d60792009-06-06 09:45:57 +01003620 uint64_t exec_offset)
3621{
3622 uint32_t exec_start, exec_len;
3623
3624 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
3625 exec_len = (uint32_t) exec->batch_len;
3626
3627 if ((exec_start | exec_len) & 0x7)
3628 return -EINVAL;
3629
3630 if (!exec_start)
3631 return -EINVAL;
3632
3633 return 0;
3634}
3635
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003636static int
3637i915_gem_wait_for_pending_flip(struct drm_device *dev,
3638 struct drm_gem_object **object_list,
3639 int count)
3640{
3641 drm_i915_private_t *dev_priv = dev->dev_private;
3642 struct drm_i915_gem_object *obj_priv;
3643 DEFINE_WAIT(wait);
3644 int i, ret = 0;
3645
3646 for (;;) {
3647 prepare_to_wait(&dev_priv->pending_flip_queue,
3648 &wait, TASK_INTERRUPTIBLE);
3649 for (i = 0; i < count; i++) {
Daniel Vetter23010e42010-03-08 13:35:02 +01003650 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003651 if (atomic_read(&obj_priv->pending_flip) > 0)
3652 break;
3653 }
3654 if (i == count)
3655 break;
3656
3657 if (!signal_pending(current)) {
3658 mutex_unlock(&dev->struct_mutex);
3659 schedule();
3660 mutex_lock(&dev->struct_mutex);
3661 continue;
3662 }
3663 ret = -ERESTARTSYS;
3664 break;
3665 }
3666 finish_wait(&dev_priv->pending_flip_queue, &wait);
3667
3668 return ret;
3669}
3670
Chris Wilson43b27f42010-07-02 08:57:15 +01003671
Eric Anholt673a3942008-07-30 12:06:12 -07003672int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003673i915_gem_do_execbuffer(struct drm_device *dev, void *data,
3674 struct drm_file *file_priv,
3675 struct drm_i915_gem_execbuffer2 *args,
3676 struct drm_i915_gem_exec_object2 *exec_list)
Eric Anholt673a3942008-07-30 12:06:12 -07003677{
3678 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003679 struct drm_gem_object **object_list = NULL;
3680 struct drm_gem_object *batch_obj;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003681 struct drm_i915_gem_object *obj_priv;
Eric Anholt201361a2009-03-11 12:30:04 -07003682 struct drm_clip_rect *cliprects = NULL;
Chris Wilson93533c22010-01-31 10:40:48 +00003683 struct drm_i915_gem_relocation_entry *relocs = NULL;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003684 int ret = 0, ret2, i, pinned = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003685 uint64_t exec_offset;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003686 uint32_t seqno, flush_domains, reloc_index;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003687 int pin_tries, flips;
Eric Anholt673a3942008-07-30 12:06:12 -07003688
Zou Nan hai852835f2010-05-21 09:08:56 +08003689 struct intel_ring_buffer *ring = NULL;
3690
Eric Anholt673a3942008-07-30 12:06:12 -07003691#if WATCH_EXEC
3692 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3693 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3694#endif
Zou Nan haid1b851f2010-05-21 09:08:57 +08003695 if (args->flags & I915_EXEC_BSD) {
3696 if (!HAS_BSD(dev)) {
3697 DRM_ERROR("execbuf with wrong flag\n");
3698 return -EINVAL;
3699 }
3700 ring = &dev_priv->bsd_ring;
3701 } else {
3702 ring = &dev_priv->render_ring;
3703 }
3704
Eric Anholt673a3942008-07-30 12:06:12 -07003705
Eric Anholt4f481ed2008-09-10 14:22:49 -07003706 if (args->buffer_count < 1) {
3707 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3708 return -EINVAL;
3709 }
Eric Anholtc8e0f932009-11-22 03:49:37 +01003710 object_list = drm_malloc_ab(sizeof(*object_list), args->buffer_count);
Jesse Barnes76446ca2009-12-17 22:05:42 -05003711 if (object_list == NULL) {
3712 DRM_ERROR("Failed to allocate object list for %d buffers\n",
Eric Anholt673a3942008-07-30 12:06:12 -07003713 args->buffer_count);
3714 ret = -ENOMEM;
3715 goto pre_mutex_err;
3716 }
Eric Anholt673a3942008-07-30 12:06:12 -07003717
Eric Anholt201361a2009-03-11 12:30:04 -07003718 if (args->num_cliprects != 0) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003719 cliprects = kcalloc(args->num_cliprects, sizeof(*cliprects),
3720 GFP_KERNEL);
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003721 if (cliprects == NULL) {
3722 ret = -ENOMEM;
Eric Anholt201361a2009-03-11 12:30:04 -07003723 goto pre_mutex_err;
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003724 }
Eric Anholt201361a2009-03-11 12:30:04 -07003725
3726 ret = copy_from_user(cliprects,
3727 (struct drm_clip_rect __user *)
3728 (uintptr_t) args->cliprects_ptr,
3729 sizeof(*cliprects) * args->num_cliprects);
3730 if (ret != 0) {
3731 DRM_ERROR("copy %d cliprects failed: %d\n",
3732 args->num_cliprects, ret);
3733 goto pre_mutex_err;
3734 }
3735 }
3736
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003737 ret = i915_gem_get_relocs_from_user(exec_list, args->buffer_count,
3738 &relocs);
3739 if (ret != 0)
3740 goto pre_mutex_err;
3741
Eric Anholt673a3942008-07-30 12:06:12 -07003742 mutex_lock(&dev->struct_mutex);
3743
3744 i915_verify_inactive(dev, __FILE__, __LINE__);
3745
Ben Gamariba1234d2009-09-14 17:48:47 -04003746 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07003747 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003748 ret = -EIO;
3749 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003750 }
3751
3752 if (dev_priv->mm.suspended) {
Eric Anholt673a3942008-07-30 12:06:12 -07003753 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003754 ret = -EBUSY;
3755 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003756 }
3757
Keith Packardac94a962008-11-20 23:30:27 -08003758 /* Look up object handles */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003759 flips = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003760 for (i = 0; i < args->buffer_count; i++) {
3761 object_list[i] = drm_gem_object_lookup(dev, file_priv,
3762 exec_list[i].handle);
3763 if (object_list[i] == NULL) {
3764 DRM_ERROR("Invalid object handle %d at index %d\n",
3765 exec_list[i].handle, i);
Chris Wilson0ce907f2010-01-23 20:26:35 +00003766 /* prevent error path from reading uninitialized data */
3767 args->buffer_count = i + 1;
Eric Anholt673a3942008-07-30 12:06:12 -07003768 ret = -EBADF;
3769 goto err;
3770 }
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003771
Daniel Vetter23010e42010-03-08 13:35:02 +01003772 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003773 if (obj_priv->in_execbuffer) {
3774 DRM_ERROR("Object %p appears more than once in object list\n",
3775 object_list[i]);
Chris Wilson0ce907f2010-01-23 20:26:35 +00003776 /* prevent error path from reading uninitialized data */
3777 args->buffer_count = i + 1;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003778 ret = -EBADF;
3779 goto err;
3780 }
3781 obj_priv->in_execbuffer = true;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003782 flips += atomic_read(&obj_priv->pending_flip);
3783 }
3784
3785 if (flips > 0) {
3786 ret = i915_gem_wait_for_pending_flip(dev, object_list,
3787 args->buffer_count);
3788 if (ret)
3789 goto err;
Keith Packardac94a962008-11-20 23:30:27 -08003790 }
Eric Anholt673a3942008-07-30 12:06:12 -07003791
Keith Packardac94a962008-11-20 23:30:27 -08003792 /* Pin and relocate */
3793 for (pin_tries = 0; ; pin_tries++) {
3794 ret = 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003795 reloc_index = 0;
3796
Keith Packardac94a962008-11-20 23:30:27 -08003797 for (i = 0; i < args->buffer_count; i++) {
3798 object_list[i]->pending_read_domains = 0;
3799 object_list[i]->pending_write_domain = 0;
3800 ret = i915_gem_object_pin_and_relocate(object_list[i],
3801 file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003802 &exec_list[i],
3803 &relocs[reloc_index]);
Keith Packardac94a962008-11-20 23:30:27 -08003804 if (ret)
3805 break;
3806 pinned = i + 1;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003807 reloc_index += exec_list[i].relocation_count;
Keith Packardac94a962008-11-20 23:30:27 -08003808 }
3809 /* success */
3810 if (ret == 0)
3811 break;
3812
3813 /* error other than GTT full, or we've already tried again */
Chris Wilson2939e1f2009-06-06 09:46:03 +01003814 if (ret != -ENOSPC || pin_tries >= 1) {
Chris Wilson07f73f62009-09-14 16:50:30 +01003815 if (ret != -ERESTARTSYS) {
3816 unsigned long long total_size = 0;
Chris Wilson3d1cc472010-05-27 13:18:19 +01003817 int num_fences = 0;
3818 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson43b27f42010-07-02 08:57:15 +01003819 obj_priv = to_intel_bo(object_list[i]);
Chris Wilson3d1cc472010-05-27 13:18:19 +01003820
Chris Wilson07f73f62009-09-14 16:50:30 +01003821 total_size += object_list[i]->size;
Chris Wilson3d1cc472010-05-27 13:18:19 +01003822 num_fences +=
3823 exec_list[i].flags & EXEC_OBJECT_NEEDS_FENCE &&
3824 obj_priv->tiling_mode != I915_TILING_NONE;
3825 }
3826 DRM_ERROR("Failed to pin buffer %d of %d, total %llu bytes, %d fences: %d\n",
Chris Wilson07f73f62009-09-14 16:50:30 +01003827 pinned+1, args->buffer_count,
Chris Wilson3d1cc472010-05-27 13:18:19 +01003828 total_size, num_fences,
3829 ret);
Chris Wilson07f73f62009-09-14 16:50:30 +01003830 DRM_ERROR("%d objects [%d pinned], "
3831 "%d object bytes [%d pinned], "
3832 "%d/%d gtt bytes\n",
3833 atomic_read(&dev->object_count),
3834 atomic_read(&dev->pin_count),
3835 atomic_read(&dev->object_memory),
3836 atomic_read(&dev->pin_memory),
3837 atomic_read(&dev->gtt_memory),
3838 dev->gtt_total);
3839 }
Eric Anholt673a3942008-07-30 12:06:12 -07003840 goto err;
3841 }
Keith Packardac94a962008-11-20 23:30:27 -08003842
3843 /* unpin all of our buffers */
3844 for (i = 0; i < pinned; i++)
3845 i915_gem_object_unpin(object_list[i]);
Eric Anholtb1177632008-12-10 10:09:41 -08003846 pinned = 0;
Keith Packardac94a962008-11-20 23:30:27 -08003847
3848 /* evict everyone we can from the aperture */
3849 ret = i915_gem_evict_everything(dev);
Chris Wilson07f73f62009-09-14 16:50:30 +01003850 if (ret && ret != -ENOSPC)
Keith Packardac94a962008-11-20 23:30:27 -08003851 goto err;
Eric Anholt673a3942008-07-30 12:06:12 -07003852 }
3853
3854 /* Set the pending read domains for the batch buffer to COMMAND */
3855 batch_obj = object_list[args->buffer_count-1];
Chris Wilson5f26a2c2009-06-06 09:45:58 +01003856 if (batch_obj->pending_write_domain) {
3857 DRM_ERROR("Attempting to use self-modifying batch buffer\n");
3858 ret = -EINVAL;
3859 goto err;
3860 }
3861 batch_obj->pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
Eric Anholt673a3942008-07-30 12:06:12 -07003862
Chris Wilson83d60792009-06-06 09:45:57 +01003863 /* Sanity check the batch buffer, prior to moving objects */
3864 exec_offset = exec_list[args->buffer_count - 1].offset;
3865 ret = i915_gem_check_execbuffer (args, exec_offset);
3866 if (ret != 0) {
3867 DRM_ERROR("execbuf with invalid offset/length\n");
3868 goto err;
3869 }
3870
Eric Anholt673a3942008-07-30 12:06:12 -07003871 i915_verify_inactive(dev, __FILE__, __LINE__);
3872
Keith Packard646f0f62008-11-20 23:23:03 -08003873 /* Zero the global flush/invalidate flags. These
3874 * will be modified as new domains are computed
3875 * for each object
3876 */
3877 dev->invalidate_domains = 0;
3878 dev->flush_domains = 0;
3879
Eric Anholt673a3942008-07-30 12:06:12 -07003880 for (i = 0; i < args->buffer_count; i++) {
3881 struct drm_gem_object *obj = object_list[i];
Eric Anholt673a3942008-07-30 12:06:12 -07003882
Keith Packard646f0f62008-11-20 23:23:03 -08003883 /* Compute new gpu domains and update invalidate/flush */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003884 i915_gem_object_set_to_gpu_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003885 }
3886
3887 i915_verify_inactive(dev, __FILE__, __LINE__);
3888
Keith Packard646f0f62008-11-20 23:23:03 -08003889 if (dev->invalidate_domains | dev->flush_domains) {
3890#if WATCH_EXEC
3891 DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
3892 __func__,
3893 dev->invalidate_domains,
3894 dev->flush_domains);
3895#endif
3896 i915_gem_flush(dev,
3897 dev->invalidate_domains,
3898 dev->flush_domains);
Zou Nan hai852835f2010-05-21 09:08:56 +08003899 if (dev->flush_domains & I915_GEM_GPU_DOMAINS) {
Eric Anholtb9624422009-06-03 07:27:35 +00003900 (void)i915_add_request(dev, file_priv,
Zou Nan hai852835f2010-05-21 09:08:56 +08003901 dev->flush_domains,
3902 &dev_priv->render_ring);
3903
Zou Nan haid1b851f2010-05-21 09:08:57 +08003904 if (HAS_BSD(dev))
3905 (void)i915_add_request(dev, file_priv,
3906 dev->flush_domains,
3907 &dev_priv->bsd_ring);
Zou Nan hai852835f2010-05-21 09:08:56 +08003908 }
Keith Packard646f0f62008-11-20 23:23:03 -08003909 }
Eric Anholt673a3942008-07-30 12:06:12 -07003910
Eric Anholtefbeed92009-02-19 14:54:51 -08003911 for (i = 0; i < args->buffer_count; i++) {
3912 struct drm_gem_object *obj = object_list[i];
Daniel Vetter23010e42010-03-08 13:35:02 +01003913 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003914 uint32_t old_write_domain = obj->write_domain;
Eric Anholtefbeed92009-02-19 14:54:51 -08003915
3916 obj->write_domain = obj->pending_write_domain;
Daniel Vetter99fcb762010-02-07 16:20:18 +01003917 if (obj->write_domain)
3918 list_move_tail(&obj_priv->gpu_write_list,
3919 &dev_priv->mm.gpu_write_list);
3920 else
3921 list_del_init(&obj_priv->gpu_write_list);
3922
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003923 trace_i915_gem_object_change_domain(obj,
3924 obj->read_domains,
3925 old_write_domain);
Eric Anholtefbeed92009-02-19 14:54:51 -08003926 }
3927
Eric Anholt673a3942008-07-30 12:06:12 -07003928 i915_verify_inactive(dev, __FILE__, __LINE__);
3929
3930#if WATCH_COHERENCY
3931 for (i = 0; i < args->buffer_count; i++) {
3932 i915_gem_object_check_coherency(object_list[i],
3933 exec_list[i].handle);
3934 }
3935#endif
3936
Eric Anholt673a3942008-07-30 12:06:12 -07003937#if WATCH_EXEC
Ben Gamari6911a9b2009-04-02 11:24:54 -07003938 i915_gem_dump_object(batch_obj,
Eric Anholt673a3942008-07-30 12:06:12 -07003939 args->batch_len,
3940 __func__,
3941 ~0);
3942#endif
3943
Eric Anholt673a3942008-07-30 12:06:12 -07003944 /* Exec the batchbuffer */
Zou Nan hai852835f2010-05-21 09:08:56 +08003945 ret = ring->dispatch_gem_execbuffer(dev, ring, args,
3946 cliprects, exec_offset);
Eric Anholt673a3942008-07-30 12:06:12 -07003947 if (ret) {
3948 DRM_ERROR("dispatch failed %d\n", ret);
3949 goto err;
3950 }
3951
3952 /*
3953 * Ensure that the commands in the batch buffer are
3954 * finished before the interrupt fires
3955 */
Zou Nan hai852835f2010-05-21 09:08:56 +08003956 flush_domains = i915_retire_commands(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07003957
3958 i915_verify_inactive(dev, __FILE__, __LINE__);
3959
3960 /*
3961 * Get a seqno representing the execution of the current buffer,
3962 * which we can wait on. We would like to mitigate these interrupts,
3963 * likely by only creating seqnos occasionally (so that we have
3964 * *some* interrupts representing completion of buffers that we can
3965 * wait on when trying to clear up gtt space).
3966 */
Zou Nan hai852835f2010-05-21 09:08:56 +08003967 seqno = i915_add_request(dev, file_priv, flush_domains, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07003968 BUG_ON(seqno == 0);
Eric Anholt673a3942008-07-30 12:06:12 -07003969 for (i = 0; i < args->buffer_count; i++) {
3970 struct drm_gem_object *obj = object_list[i];
Zou Nan hai852835f2010-05-21 09:08:56 +08003971 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003972
Zou Nan hai852835f2010-05-21 09:08:56 +08003973 i915_gem_object_move_to_active(obj, seqno, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07003974#if WATCH_LRU
3975 DRM_INFO("%s: move to exec list %p\n", __func__, obj);
3976#endif
3977 }
3978#if WATCH_LRU
3979 i915_dump_lru(dev, __func__);
3980#endif
3981
3982 i915_verify_inactive(dev, __FILE__, __LINE__);
3983
Eric Anholt673a3942008-07-30 12:06:12 -07003984err:
Julia Lawallaad87df2008-12-21 16:28:47 +01003985 for (i = 0; i < pinned; i++)
3986 i915_gem_object_unpin(object_list[i]);
Eric Anholt673a3942008-07-30 12:06:12 -07003987
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003988 for (i = 0; i < args->buffer_count; i++) {
3989 if (object_list[i]) {
Daniel Vetter23010e42010-03-08 13:35:02 +01003990 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003991 obj_priv->in_execbuffer = false;
3992 }
Julia Lawallaad87df2008-12-21 16:28:47 +01003993 drm_gem_object_unreference(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003994 }
Julia Lawallaad87df2008-12-21 16:28:47 +01003995
Eric Anholt673a3942008-07-30 12:06:12 -07003996 mutex_unlock(&dev->struct_mutex);
3997
Chris Wilson93533c22010-01-31 10:40:48 +00003998pre_mutex_err:
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003999 /* Copy the updated relocations out regardless of current error
4000 * state. Failure to update the relocs would mean that the next
4001 * time userland calls execbuf, it would do so with presumed offset
4002 * state that didn't match the actual object state.
4003 */
4004 ret2 = i915_gem_put_relocs_to_user(exec_list, args->buffer_count,
4005 relocs);
4006 if (ret2 != 0) {
4007 DRM_ERROR("Failed to copy relocations back out: %d\n", ret2);
4008
4009 if (ret == 0)
4010 ret = ret2;
4011 }
4012
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07004013 drm_free_large(object_list);
Eric Anholt9a298b22009-03-24 12:23:04 -07004014 kfree(cliprects);
Eric Anholt673a3942008-07-30 12:06:12 -07004015
4016 return ret;
4017}
4018
Jesse Barnes76446ca2009-12-17 22:05:42 -05004019/*
4020 * Legacy execbuffer just creates an exec2 list from the original exec object
4021 * list array and passes it to the real function.
4022 */
4023int
4024i915_gem_execbuffer(struct drm_device *dev, void *data,
4025 struct drm_file *file_priv)
4026{
4027 struct drm_i915_gem_execbuffer *args = data;
4028 struct drm_i915_gem_execbuffer2 exec2;
4029 struct drm_i915_gem_exec_object *exec_list = NULL;
4030 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
4031 int ret, i;
4032
4033#if WATCH_EXEC
4034 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
4035 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
4036#endif
4037
4038 if (args->buffer_count < 1) {
4039 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
4040 return -EINVAL;
4041 }
4042
4043 /* Copy in the exec list from userland */
4044 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
4045 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
4046 if (exec_list == NULL || exec2_list == NULL) {
4047 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
4048 args->buffer_count);
4049 drm_free_large(exec_list);
4050 drm_free_large(exec2_list);
4051 return -ENOMEM;
4052 }
4053 ret = copy_from_user(exec_list,
4054 (struct drm_i915_relocation_entry __user *)
4055 (uintptr_t) args->buffers_ptr,
4056 sizeof(*exec_list) * args->buffer_count);
4057 if (ret != 0) {
4058 DRM_ERROR("copy %d exec entries failed %d\n",
4059 args->buffer_count, ret);
4060 drm_free_large(exec_list);
4061 drm_free_large(exec2_list);
4062 return -EFAULT;
4063 }
4064
4065 for (i = 0; i < args->buffer_count; i++) {
4066 exec2_list[i].handle = exec_list[i].handle;
4067 exec2_list[i].relocation_count = exec_list[i].relocation_count;
4068 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
4069 exec2_list[i].alignment = exec_list[i].alignment;
4070 exec2_list[i].offset = exec_list[i].offset;
4071 if (!IS_I965G(dev))
4072 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
4073 else
4074 exec2_list[i].flags = 0;
4075 }
4076
4077 exec2.buffers_ptr = args->buffers_ptr;
4078 exec2.buffer_count = args->buffer_count;
4079 exec2.batch_start_offset = args->batch_start_offset;
4080 exec2.batch_len = args->batch_len;
4081 exec2.DR1 = args->DR1;
4082 exec2.DR4 = args->DR4;
4083 exec2.num_cliprects = args->num_cliprects;
4084 exec2.cliprects_ptr = args->cliprects_ptr;
Zou Nan hai852835f2010-05-21 09:08:56 +08004085 exec2.flags = I915_EXEC_RENDER;
Jesse Barnes76446ca2009-12-17 22:05:42 -05004086
4087 ret = i915_gem_do_execbuffer(dev, data, file_priv, &exec2, exec2_list);
4088 if (!ret) {
4089 /* Copy the new buffer offsets back to the user's exec list. */
4090 for (i = 0; i < args->buffer_count; i++)
4091 exec_list[i].offset = exec2_list[i].offset;
4092 /* ... and back out to userspace */
4093 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
4094 (uintptr_t) args->buffers_ptr,
4095 exec_list,
4096 sizeof(*exec_list) * args->buffer_count);
4097 if (ret) {
4098 ret = -EFAULT;
4099 DRM_ERROR("failed to copy %d exec entries "
4100 "back to user (%d)\n",
4101 args->buffer_count, ret);
4102 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05004103 }
4104
4105 drm_free_large(exec_list);
4106 drm_free_large(exec2_list);
4107 return ret;
4108}
4109
4110int
4111i915_gem_execbuffer2(struct drm_device *dev, void *data,
4112 struct drm_file *file_priv)
4113{
4114 struct drm_i915_gem_execbuffer2 *args = data;
4115 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
4116 int ret;
4117
4118#if WATCH_EXEC
4119 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
4120 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
4121#endif
4122
4123 if (args->buffer_count < 1) {
4124 DRM_ERROR("execbuf2 with %d buffers\n", args->buffer_count);
4125 return -EINVAL;
4126 }
4127
4128 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
4129 if (exec2_list == NULL) {
4130 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
4131 args->buffer_count);
4132 return -ENOMEM;
4133 }
4134 ret = copy_from_user(exec2_list,
4135 (struct drm_i915_relocation_entry __user *)
4136 (uintptr_t) args->buffers_ptr,
4137 sizeof(*exec2_list) * args->buffer_count);
4138 if (ret != 0) {
4139 DRM_ERROR("copy %d exec entries failed %d\n",
4140 args->buffer_count, ret);
4141 drm_free_large(exec2_list);
4142 return -EFAULT;
4143 }
4144
4145 ret = i915_gem_do_execbuffer(dev, data, file_priv, args, exec2_list);
4146 if (!ret) {
4147 /* Copy the new buffer offsets back to the user's exec list. */
4148 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
4149 (uintptr_t) args->buffers_ptr,
4150 exec2_list,
4151 sizeof(*exec2_list) * args->buffer_count);
4152 if (ret) {
4153 ret = -EFAULT;
4154 DRM_ERROR("failed to copy %d exec entries "
4155 "back to user (%d)\n",
4156 args->buffer_count, ret);
4157 }
4158 }
4159
4160 drm_free_large(exec2_list);
4161 return ret;
4162}
4163
Eric Anholt673a3942008-07-30 12:06:12 -07004164int
4165i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment)
4166{
4167 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01004168 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004169 int ret;
4170
Daniel Vetter778c3542010-05-13 11:49:44 +02004171 BUG_ON(obj_priv->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT);
4172
Eric Anholt673a3942008-07-30 12:06:12 -07004173 i915_verify_inactive(dev, __FILE__, __LINE__);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01004174
4175 if (obj_priv->gtt_space != NULL) {
4176 if (alignment == 0)
4177 alignment = i915_gem_get_gtt_alignment(obj);
4178 if (obj_priv->gtt_offset & (alignment - 1)) {
4179 ret = i915_gem_object_unbind(obj);
4180 if (ret)
4181 return ret;
4182 }
4183 }
4184
Eric Anholt673a3942008-07-30 12:06:12 -07004185 if (obj_priv->gtt_space == NULL) {
4186 ret = i915_gem_object_bind_to_gtt(obj, alignment);
Chris Wilson97311292009-09-21 00:22:34 +01004187 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07004188 return ret;
Chris Wilson22c344e2009-02-11 14:26:45 +00004189 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05004190
Eric Anholt673a3942008-07-30 12:06:12 -07004191 obj_priv->pin_count++;
4192
4193 /* If the object is not active and not pending a flush,
4194 * remove it from the inactive list
4195 */
4196 if (obj_priv->pin_count == 1) {
4197 atomic_inc(&dev->pin_count);
4198 atomic_add(obj->size, &dev->pin_memory);
4199 if (!obj_priv->active &&
Chris Wilson21d509e2009-06-06 09:46:02 +01004200 (obj->write_domain & I915_GEM_GPU_DOMAINS) == 0 &&
Eric Anholt673a3942008-07-30 12:06:12 -07004201 !list_empty(&obj_priv->list))
4202 list_del_init(&obj_priv->list);
4203 }
4204 i915_verify_inactive(dev, __FILE__, __LINE__);
4205
4206 return 0;
4207}
4208
4209void
4210i915_gem_object_unpin(struct drm_gem_object *obj)
4211{
4212 struct drm_device *dev = obj->dev;
4213 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01004214 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004215
4216 i915_verify_inactive(dev, __FILE__, __LINE__);
4217 obj_priv->pin_count--;
4218 BUG_ON(obj_priv->pin_count < 0);
4219 BUG_ON(obj_priv->gtt_space == NULL);
4220
4221 /* If the object is no longer pinned, and is
4222 * neither active nor being flushed, then stick it on
4223 * the inactive list
4224 */
4225 if (obj_priv->pin_count == 0) {
4226 if (!obj_priv->active &&
Chris Wilson21d509e2009-06-06 09:46:02 +01004227 (obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
Eric Anholt673a3942008-07-30 12:06:12 -07004228 list_move_tail(&obj_priv->list,
4229 &dev_priv->mm.inactive_list);
4230 atomic_dec(&dev->pin_count);
4231 atomic_sub(obj->size, &dev->pin_memory);
4232 }
4233 i915_verify_inactive(dev, __FILE__, __LINE__);
4234}
4235
4236int
4237i915_gem_pin_ioctl(struct drm_device *dev, void *data,
4238 struct drm_file *file_priv)
4239{
4240 struct drm_i915_gem_pin *args = data;
4241 struct drm_gem_object *obj;
4242 struct drm_i915_gem_object *obj_priv;
4243 int ret;
4244
4245 mutex_lock(&dev->struct_mutex);
4246
4247 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4248 if (obj == NULL) {
4249 DRM_ERROR("Bad handle in i915_gem_pin_ioctl(): %d\n",
4250 args->handle);
4251 mutex_unlock(&dev->struct_mutex);
4252 return -EBADF;
4253 }
Daniel Vetter23010e42010-03-08 13:35:02 +01004254 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004255
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004256 if (obj_priv->madv != I915_MADV_WILLNEED) {
4257 DRM_ERROR("Attempting to pin a purgeable buffer\n");
Chris Wilson3ef94da2009-09-14 16:50:29 +01004258 drm_gem_object_unreference(obj);
4259 mutex_unlock(&dev->struct_mutex);
4260 return -EINVAL;
4261 }
4262
Jesse Barnes79e53942008-11-07 14:24:08 -08004263 if (obj_priv->pin_filp != NULL && obj_priv->pin_filp != file_priv) {
4264 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
4265 args->handle);
Chris Wilson96dec612009-02-08 19:08:04 +00004266 drm_gem_object_unreference(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004267 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08004268 return -EINVAL;
4269 }
4270
4271 obj_priv->user_pin_count++;
4272 obj_priv->pin_filp = file_priv;
4273 if (obj_priv->user_pin_count == 1) {
4274 ret = i915_gem_object_pin(obj, args->alignment);
4275 if (ret != 0) {
4276 drm_gem_object_unreference(obj);
4277 mutex_unlock(&dev->struct_mutex);
4278 return ret;
4279 }
Eric Anholt673a3942008-07-30 12:06:12 -07004280 }
4281
4282 /* XXX - flush the CPU caches for pinned objects
4283 * as the X server doesn't manage domains yet
4284 */
Eric Anholte47c68e2008-11-14 13:35:19 -08004285 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004286 args->offset = obj_priv->gtt_offset;
4287 drm_gem_object_unreference(obj);
4288 mutex_unlock(&dev->struct_mutex);
4289
4290 return 0;
4291}
4292
4293int
4294i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
4295 struct drm_file *file_priv)
4296{
4297 struct drm_i915_gem_pin *args = data;
4298 struct drm_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08004299 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07004300
4301 mutex_lock(&dev->struct_mutex);
4302
4303 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4304 if (obj == NULL) {
4305 DRM_ERROR("Bad handle in i915_gem_unpin_ioctl(): %d\n",
4306 args->handle);
4307 mutex_unlock(&dev->struct_mutex);
4308 return -EBADF;
4309 }
4310
Daniel Vetter23010e42010-03-08 13:35:02 +01004311 obj_priv = to_intel_bo(obj);
Jesse Barnes79e53942008-11-07 14:24:08 -08004312 if (obj_priv->pin_filp != file_priv) {
4313 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
4314 args->handle);
4315 drm_gem_object_unreference(obj);
4316 mutex_unlock(&dev->struct_mutex);
4317 return -EINVAL;
4318 }
4319 obj_priv->user_pin_count--;
4320 if (obj_priv->user_pin_count == 0) {
4321 obj_priv->pin_filp = NULL;
4322 i915_gem_object_unpin(obj);
4323 }
Eric Anholt673a3942008-07-30 12:06:12 -07004324
4325 drm_gem_object_unreference(obj);
4326 mutex_unlock(&dev->struct_mutex);
4327 return 0;
4328}
4329
4330int
4331i915_gem_busy_ioctl(struct drm_device *dev, void *data,
4332 struct drm_file *file_priv)
4333{
4334 struct drm_i915_gem_busy *args = data;
4335 struct drm_gem_object *obj;
4336 struct drm_i915_gem_object *obj_priv;
4337
Eric Anholt673a3942008-07-30 12:06:12 -07004338 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4339 if (obj == NULL) {
4340 DRM_ERROR("Bad handle in i915_gem_busy_ioctl(): %d\n",
4341 args->handle);
Eric Anholt673a3942008-07-30 12:06:12 -07004342 return -EBADF;
4343 }
4344
Chris Wilsonb1ce7862009-06-06 09:46:00 +01004345 mutex_lock(&dev->struct_mutex);
Eric Anholtf21289b2009-02-18 09:44:56 -08004346 /* Update the active list for the hardware's current position.
4347 * Otherwise this only updates on a delayed timer or when irqs are
4348 * actually unmasked, and our working set ends up being larger than
4349 * required.
4350 */
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01004351 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004352
Daniel Vetter23010e42010-03-08 13:35:02 +01004353 obj_priv = to_intel_bo(obj);
Eric Anholtc4de0a52008-12-14 19:05:04 -08004354 /* Don't count being on the flushing list against the object being
4355 * done. Otherwise, a buffer left on the flushing list but not getting
4356 * flushed (because nobody's flushing that domain) won't ever return
4357 * unbusy and get reused by libdrm's bo cache. The other expected
4358 * consumer of this interface, OpenGL's occlusion queries, also specs
4359 * that the objects get unbusy "eventually" without any interference.
4360 */
4361 args->busy = obj_priv->active && obj_priv->last_rendering_seqno != 0;
Eric Anholt673a3942008-07-30 12:06:12 -07004362
4363 drm_gem_object_unreference(obj);
4364 mutex_unlock(&dev->struct_mutex);
4365 return 0;
4366}
4367
4368int
4369i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4370 struct drm_file *file_priv)
4371{
4372 return i915_gem_ring_throttle(dev, file_priv);
4373}
4374
Chris Wilson3ef94da2009-09-14 16:50:29 +01004375int
4376i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4377 struct drm_file *file_priv)
4378{
4379 struct drm_i915_gem_madvise *args = data;
4380 struct drm_gem_object *obj;
4381 struct drm_i915_gem_object *obj_priv;
4382
4383 switch (args->madv) {
4384 case I915_MADV_DONTNEED:
4385 case I915_MADV_WILLNEED:
4386 break;
4387 default:
4388 return -EINVAL;
4389 }
4390
4391 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4392 if (obj == NULL) {
4393 DRM_ERROR("Bad handle in i915_gem_madvise_ioctl(): %d\n",
4394 args->handle);
4395 return -EBADF;
4396 }
4397
4398 mutex_lock(&dev->struct_mutex);
Daniel Vetter23010e42010-03-08 13:35:02 +01004399 obj_priv = to_intel_bo(obj);
Chris Wilson3ef94da2009-09-14 16:50:29 +01004400
4401 if (obj_priv->pin_count) {
4402 drm_gem_object_unreference(obj);
4403 mutex_unlock(&dev->struct_mutex);
4404
4405 DRM_ERROR("Attempted i915_gem_madvise_ioctl() on a pinned object\n");
4406 return -EINVAL;
4407 }
4408
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004409 if (obj_priv->madv != __I915_MADV_PURGED)
4410 obj_priv->madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004411
Chris Wilson2d7ef392009-09-20 23:13:10 +01004412 /* if the object is no longer bound, discard its backing storage */
4413 if (i915_gem_object_is_purgeable(obj_priv) &&
4414 obj_priv->gtt_space == NULL)
4415 i915_gem_object_truncate(obj);
4416
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004417 args->retained = obj_priv->madv != __I915_MADV_PURGED;
4418
Chris Wilson3ef94da2009-09-14 16:50:29 +01004419 drm_gem_object_unreference(obj);
4420 mutex_unlock(&dev->struct_mutex);
4421
4422 return 0;
4423}
4424
Daniel Vetterac52bc52010-04-09 19:05:06 +00004425struct drm_gem_object * i915_gem_alloc_object(struct drm_device *dev,
4426 size_t size)
4427{
Daniel Vetterc397b902010-04-09 19:05:07 +00004428 struct drm_i915_gem_object *obj;
4429
4430 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
4431 if (obj == NULL)
4432 return NULL;
4433
4434 if (drm_gem_object_init(dev, &obj->base, size) != 0) {
4435 kfree(obj);
4436 return NULL;
4437 }
4438
4439 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4440 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4441
4442 obj->agp_type = AGP_USER_MEMORY;
Daniel Vetter62b8b212010-04-09 19:05:08 +00004443 obj->base.driver_private = NULL;
Daniel Vetterc397b902010-04-09 19:05:07 +00004444 obj->fence_reg = I915_FENCE_REG_NONE;
4445 INIT_LIST_HEAD(&obj->list);
4446 INIT_LIST_HEAD(&obj->gpu_write_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00004447 obj->madv = I915_MADV_WILLNEED;
4448
4449 trace_i915_gem_object_create(&obj->base);
4450
4451 return &obj->base;
Daniel Vetterac52bc52010-04-09 19:05:06 +00004452}
4453
Eric Anholt673a3942008-07-30 12:06:12 -07004454int i915_gem_init_object(struct drm_gem_object *obj)
4455{
Daniel Vetterc397b902010-04-09 19:05:07 +00004456 BUG();
Jesse Barnesde151cf2008-11-12 10:03:55 -08004457
Eric Anholt673a3942008-07-30 12:06:12 -07004458 return 0;
4459}
4460
4461void i915_gem_free_object(struct drm_gem_object *obj)
4462{
Jesse Barnesde151cf2008-11-12 10:03:55 -08004463 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01004464 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004465
Chris Wilson1c5d22f2009-08-25 11:15:50 +01004466 trace_i915_gem_object_destroy(obj);
4467
Eric Anholt673a3942008-07-30 12:06:12 -07004468 while (obj_priv->pin_count > 0)
4469 i915_gem_object_unpin(obj);
4470
Dave Airlie71acb5e2008-12-30 20:31:46 +10004471 if (obj_priv->phys_obj)
4472 i915_gem_detach_phys_object(dev, obj);
4473
Eric Anholt673a3942008-07-30 12:06:12 -07004474 i915_gem_object_unbind(obj);
4475
Chris Wilson7e616152009-09-10 08:53:04 +01004476 if (obj_priv->mmap_offset)
4477 i915_gem_free_mmap_offset(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08004478
Daniel Vetterc397b902010-04-09 19:05:07 +00004479 drm_gem_object_release(obj);
4480
Eric Anholt9a298b22009-03-24 12:23:04 -07004481 kfree(obj_priv->page_cpu_valid);
Eric Anholt280b7132009-03-12 16:56:27 -07004482 kfree(obj_priv->bit_17);
Daniel Vetterc397b902010-04-09 19:05:07 +00004483 kfree(obj_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07004484}
4485
Chris Wilsonab5ee572009-09-20 19:25:47 +01004486/** Unbinds all inactive objects. */
Eric Anholt673a3942008-07-30 12:06:12 -07004487static int
Chris Wilsonab5ee572009-09-20 19:25:47 +01004488i915_gem_evict_from_inactive_list(struct drm_device *dev)
Eric Anholt673a3942008-07-30 12:06:12 -07004489{
Chris Wilsonab5ee572009-09-20 19:25:47 +01004490 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07004491
Chris Wilsonab5ee572009-09-20 19:25:47 +01004492 while (!list_empty(&dev_priv->mm.inactive_list)) {
4493 struct drm_gem_object *obj;
4494 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004495
Daniel Vettera8089e82010-04-09 19:05:09 +00004496 obj = &list_first_entry(&dev_priv->mm.inactive_list,
4497 struct drm_i915_gem_object,
4498 list)->base;
Eric Anholt673a3942008-07-30 12:06:12 -07004499
4500 ret = i915_gem_object_unbind(obj);
4501 if (ret != 0) {
Chris Wilsonab5ee572009-09-20 19:25:47 +01004502 DRM_ERROR("Error unbinding object: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07004503 return ret;
4504 }
4505 }
4506
Eric Anholt673a3942008-07-30 12:06:12 -07004507 return 0;
4508}
4509
Jesse Barnes5669fca2009-02-17 15:13:31 -08004510int
Eric Anholt673a3942008-07-30 12:06:12 -07004511i915_gem_idle(struct drm_device *dev)
4512{
4513 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson29105cc2010-01-07 10:39:13 +00004514 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004515
Keith Packard6dbe2772008-10-14 21:41:13 -07004516 mutex_lock(&dev->struct_mutex);
4517
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004518 if (dev_priv->mm.suspended ||
Zou Nan haid1b851f2010-05-21 09:08:57 +08004519 (dev_priv->render_ring.gem_object == NULL) ||
4520 (HAS_BSD(dev) &&
4521 dev_priv->bsd_ring.gem_object == NULL)) {
Keith Packard6dbe2772008-10-14 21:41:13 -07004522 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004523 return 0;
Keith Packard6dbe2772008-10-14 21:41:13 -07004524 }
Eric Anholt673a3942008-07-30 12:06:12 -07004525
Chris Wilson29105cc2010-01-07 10:39:13 +00004526 ret = i915_gpu_idle(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004527 if (ret) {
4528 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004529 return ret;
Keith Packard6dbe2772008-10-14 21:41:13 -07004530 }
Eric Anholt673a3942008-07-30 12:06:12 -07004531
Chris Wilson29105cc2010-01-07 10:39:13 +00004532 /* Under UMS, be paranoid and evict. */
4533 if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
4534 ret = i915_gem_evict_from_inactive_list(dev);
4535 if (ret) {
4536 mutex_unlock(&dev->struct_mutex);
4537 return ret;
4538 }
4539 }
4540
4541 /* Hack! Don't let anybody do execbuf while we don't control the chip.
4542 * We need to replace this with a semaphore, or something.
4543 * And not confound mm.suspended!
4544 */
4545 dev_priv->mm.suspended = 1;
4546 del_timer(&dev_priv->hangcheck_timer);
4547
4548 i915_kernel_lost_context(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004549 i915_gem_cleanup_ringbuffer(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00004550
Keith Packard6dbe2772008-10-14 21:41:13 -07004551 mutex_unlock(&dev->struct_mutex);
4552
Chris Wilson29105cc2010-01-07 10:39:13 +00004553 /* Cancel the retire work handler, which should be idle now. */
4554 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
4555
Eric Anholt673a3942008-07-30 12:06:12 -07004556 return 0;
4557}
4558
Jesse Barnese552eb72010-04-21 11:39:23 -07004559/*
4560 * 965+ support PIPE_CONTROL commands, which provide finer grained control
4561 * over cache flushing.
4562 */
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004563static int
Jesse Barnese552eb72010-04-21 11:39:23 -07004564i915_gem_init_pipe_control(struct drm_device *dev)
4565{
4566 drm_i915_private_t *dev_priv = dev->dev_private;
4567 struct drm_gem_object *obj;
4568 struct drm_i915_gem_object *obj_priv;
4569 int ret;
4570
Eric Anholt34dc4d42010-05-07 14:30:03 -07004571 obj = i915_gem_alloc_object(dev, 4096);
Jesse Barnese552eb72010-04-21 11:39:23 -07004572 if (obj == NULL) {
4573 DRM_ERROR("Failed to allocate seqno page\n");
4574 ret = -ENOMEM;
4575 goto err;
4576 }
4577 obj_priv = to_intel_bo(obj);
4578 obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
4579
4580 ret = i915_gem_object_pin(obj, 4096);
4581 if (ret)
4582 goto err_unref;
4583
4584 dev_priv->seqno_gfx_addr = obj_priv->gtt_offset;
4585 dev_priv->seqno_page = kmap(obj_priv->pages[0]);
4586 if (dev_priv->seqno_page == NULL)
4587 goto err_unpin;
4588
4589 dev_priv->seqno_obj = obj;
4590 memset(dev_priv->seqno_page, 0, PAGE_SIZE);
4591
4592 return 0;
4593
4594err_unpin:
4595 i915_gem_object_unpin(obj);
4596err_unref:
4597 drm_gem_object_unreference(obj);
4598err:
4599 return ret;
4600}
4601
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004602
4603static void
Jesse Barnese552eb72010-04-21 11:39:23 -07004604i915_gem_cleanup_pipe_control(struct drm_device *dev)
4605{
4606 drm_i915_private_t *dev_priv = dev->dev_private;
4607 struct drm_gem_object *obj;
4608 struct drm_i915_gem_object *obj_priv;
4609
4610 obj = dev_priv->seqno_obj;
4611 obj_priv = to_intel_bo(obj);
4612 kunmap(obj_priv->pages[0]);
4613 i915_gem_object_unpin(obj);
4614 drm_gem_object_unreference(obj);
4615 dev_priv->seqno_obj = NULL;
4616
4617 dev_priv->seqno_page = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07004618}
4619
Eric Anholt673a3942008-07-30 12:06:12 -07004620int
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004621i915_gem_init_ringbuffer(struct drm_device *dev)
4622{
4623 drm_i915_private_t *dev_priv = dev->dev_private;
4624 int ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01004625
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004626 dev_priv->render_ring = render_ring;
Chris Wilson68f95ba2010-05-27 13:18:22 +01004627
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004628 if (!I915_NEED_GFX_HWS(dev)) {
4629 dev_priv->render_ring.status_page.page_addr
4630 = dev_priv->status_page_dmah->vaddr;
4631 memset(dev_priv->render_ring.status_page.page_addr,
4632 0, PAGE_SIZE);
4633 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004634
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004635 if (HAS_PIPE_CONTROL(dev)) {
4636 ret = i915_gem_init_pipe_control(dev);
4637 if (ret)
4638 return ret;
4639 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004640
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004641 ret = intel_init_ring_buffer(dev, &dev_priv->render_ring);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004642 if (ret)
4643 goto cleanup_pipe_control;
4644
4645 if (HAS_BSD(dev)) {
Zou Nan haid1b851f2010-05-21 09:08:57 +08004646 dev_priv->bsd_ring = bsd_ring;
4647 ret = intel_init_ring_buffer(dev, &dev_priv->bsd_ring);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004648 if (ret)
4649 goto cleanup_render_ring;
Zou Nan haid1b851f2010-05-21 09:08:57 +08004650 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004651
4652 return 0;
4653
4654cleanup_render_ring:
4655 intel_cleanup_ring_buffer(dev, &dev_priv->render_ring);
4656cleanup_pipe_control:
4657 if (HAS_PIPE_CONTROL(dev))
4658 i915_gem_cleanup_pipe_control(dev);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004659 return ret;
4660}
4661
4662void
4663i915_gem_cleanup_ringbuffer(struct drm_device *dev)
4664{
4665 drm_i915_private_t *dev_priv = dev->dev_private;
4666
4667 intel_cleanup_ring_buffer(dev, &dev_priv->render_ring);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004668 if (HAS_BSD(dev))
4669 intel_cleanup_ring_buffer(dev, &dev_priv->bsd_ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004670 if (HAS_PIPE_CONTROL(dev))
4671 i915_gem_cleanup_pipe_control(dev);
4672}
4673
4674int
Eric Anholt673a3942008-07-30 12:06:12 -07004675i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
4676 struct drm_file *file_priv)
4677{
4678 drm_i915_private_t *dev_priv = dev->dev_private;
4679 int ret;
4680
Jesse Barnes79e53942008-11-07 14:24:08 -08004681 if (drm_core_check_feature(dev, DRIVER_MODESET))
4682 return 0;
4683
Ben Gamariba1234d2009-09-14 17:48:47 -04004684 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07004685 DRM_ERROR("Reenabling wedged hardware, good luck\n");
Ben Gamariba1234d2009-09-14 17:48:47 -04004686 atomic_set(&dev_priv->mm.wedged, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004687 }
4688
Eric Anholt673a3942008-07-30 12:06:12 -07004689 mutex_lock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004690 dev_priv->mm.suspended = 0;
4691
4692 ret = i915_gem_init_ringbuffer(dev);
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004693 if (ret != 0) {
4694 mutex_unlock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004695 return ret;
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004696 }
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004697
Carl Worth5e118f42009-03-20 11:54:25 -07004698 spin_lock(&dev_priv->mm.active_list_lock);
Zou Nan hai852835f2010-05-21 09:08:56 +08004699 BUG_ON(!list_empty(&dev_priv->render_ring.active_list));
Zou Nan haid1b851f2010-05-21 09:08:57 +08004700 BUG_ON(HAS_BSD(dev) && !list_empty(&dev_priv->bsd_ring.active_list));
Carl Worth5e118f42009-03-20 11:54:25 -07004701 spin_unlock(&dev_priv->mm.active_list_lock);
4702
Eric Anholt673a3942008-07-30 12:06:12 -07004703 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
4704 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
Zou Nan hai852835f2010-05-21 09:08:56 +08004705 BUG_ON(!list_empty(&dev_priv->render_ring.request_list));
Zou Nan haid1b851f2010-05-21 09:08:57 +08004706 BUG_ON(HAS_BSD(dev) && !list_empty(&dev_priv->bsd_ring.request_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004707 mutex_unlock(&dev->struct_mutex);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004708
Chris Wilson5f353082010-06-07 14:03:03 +01004709 ret = drm_irq_install(dev);
4710 if (ret)
4711 goto cleanup_ringbuffer;
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004712
Eric Anholt673a3942008-07-30 12:06:12 -07004713 return 0;
Chris Wilson5f353082010-06-07 14:03:03 +01004714
4715cleanup_ringbuffer:
4716 mutex_lock(&dev->struct_mutex);
4717 i915_gem_cleanup_ringbuffer(dev);
4718 dev_priv->mm.suspended = 1;
4719 mutex_unlock(&dev->struct_mutex);
4720
4721 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004722}
4723
4724int
4725i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
4726 struct drm_file *file_priv)
4727{
Jesse Barnes79e53942008-11-07 14:24:08 -08004728 if (drm_core_check_feature(dev, DRIVER_MODESET))
4729 return 0;
4730
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004731 drm_irq_uninstall(dev);
Linus Torvaldse6890f62009-09-08 17:09:24 -07004732 return i915_gem_idle(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004733}
4734
4735void
4736i915_gem_lastclose(struct drm_device *dev)
4737{
4738 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004739
Eric Anholte806b492009-01-22 09:56:58 -08004740 if (drm_core_check_feature(dev, DRIVER_MODESET))
4741 return;
4742
Keith Packard6dbe2772008-10-14 21:41:13 -07004743 ret = i915_gem_idle(dev);
4744 if (ret)
4745 DRM_ERROR("failed to idle hardware: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07004746}
4747
4748void
4749i915_gem_load(struct drm_device *dev)
4750{
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004751 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07004752 drm_i915_private_t *dev_priv = dev->dev_private;
4753
Carl Worth5e118f42009-03-20 11:54:25 -07004754 spin_lock_init(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07004755 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
Daniel Vetter99fcb762010-02-07 16:20:18 +01004756 INIT_LIST_HEAD(&dev_priv->mm.gpu_write_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004757 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07004758 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Zou Nan hai852835f2010-05-21 09:08:56 +08004759 INIT_LIST_HEAD(&dev_priv->render_ring.active_list);
4760 INIT_LIST_HEAD(&dev_priv->render_ring.request_list);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004761 if (HAS_BSD(dev)) {
4762 INIT_LIST_HEAD(&dev_priv->bsd_ring.active_list);
4763 INIT_LIST_HEAD(&dev_priv->bsd_ring.request_list);
4764 }
Daniel Vetter007cc8a2010-04-28 11:02:31 +02004765 for (i = 0; i < 16; i++)
4766 INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004767 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
4768 i915_gem_retire_work_handler);
Chris Wilson31169712009-09-14 16:50:28 +01004769 spin_lock(&shrink_list_lock);
4770 list_add(&dev_priv->mm.shrink_list, &shrink_list);
4771 spin_unlock(&shrink_list_lock);
4772
Dave Airlie94400122010-07-20 13:15:31 +10004773 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
4774 if (IS_GEN3(dev)) {
4775 u32 tmp = I915_READ(MI_ARB_STATE);
4776 if (!(tmp & MI_ARB_C3_LP_WRITE_ENABLE)) {
4777 /* arb state is a masked write, so set bit + bit in mask */
4778 tmp = MI_ARB_C3_LP_WRITE_ENABLE | (MI_ARB_C3_LP_WRITE_ENABLE << MI_ARB_MASK_SHIFT);
4779 I915_WRITE(MI_ARB_STATE, tmp);
4780 }
4781 }
4782
Jesse Barnesde151cf2008-11-12 10:03:55 -08004783 /* Old X drivers will take 0-2 for front, back, depth buffers */
Eric Anholtb397c832010-01-26 09:43:10 -08004784 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4785 dev_priv->fence_reg_start = 3;
Jesse Barnesde151cf2008-11-12 10:03:55 -08004786
Jesse Barnes0f973f22009-01-26 17:10:45 -08004787 if (IS_I965G(dev) || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08004788 dev_priv->num_fence_regs = 16;
4789 else
4790 dev_priv->num_fence_regs = 8;
4791
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004792 /* Initialize fence registers to zero */
4793 if (IS_I965G(dev)) {
4794 for (i = 0; i < 16; i++)
4795 I915_WRITE64(FENCE_REG_965_0 + (i * 8), 0);
4796 } else {
4797 for (i = 0; i < 8; i++)
4798 I915_WRITE(FENCE_REG_830_0 + (i * 4), 0);
4799 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4800 for (i = 0; i < 8; i++)
4801 I915_WRITE(FENCE_REG_945_8 + (i * 4), 0);
4802 }
Eric Anholt673a3942008-07-30 12:06:12 -07004803 i915_gem_detect_bit_6_swizzle(dev);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05004804 init_waitqueue_head(&dev_priv->pending_flip_queue);
Eric Anholt673a3942008-07-30 12:06:12 -07004805}
Dave Airlie71acb5e2008-12-30 20:31:46 +10004806
4807/*
4808 * Create a physically contiguous memory object for this object
4809 * e.g. for cursor + overlay regs
4810 */
4811int i915_gem_init_phys_object(struct drm_device *dev,
4812 int id, int size)
4813{
4814 drm_i915_private_t *dev_priv = dev->dev_private;
4815 struct drm_i915_gem_phys_object *phys_obj;
4816 int ret;
4817
4818 if (dev_priv->mm.phys_objs[id - 1] || !size)
4819 return 0;
4820
Eric Anholt9a298b22009-03-24 12:23:04 -07004821 phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004822 if (!phys_obj)
4823 return -ENOMEM;
4824
4825 phys_obj->id = id;
4826
Zhenyu Wange6be8d92010-01-05 11:25:05 +08004827 phys_obj->handle = drm_pci_alloc(dev, size, 0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004828 if (!phys_obj->handle) {
4829 ret = -ENOMEM;
4830 goto kfree_obj;
4831 }
4832#ifdef CONFIG_X86
4833 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4834#endif
4835
4836 dev_priv->mm.phys_objs[id - 1] = phys_obj;
4837
4838 return 0;
4839kfree_obj:
Eric Anholt9a298b22009-03-24 12:23:04 -07004840 kfree(phys_obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004841 return ret;
4842}
4843
4844void i915_gem_free_phys_object(struct drm_device *dev, int id)
4845{
4846 drm_i915_private_t *dev_priv = dev->dev_private;
4847 struct drm_i915_gem_phys_object *phys_obj;
4848
4849 if (!dev_priv->mm.phys_objs[id - 1])
4850 return;
4851
4852 phys_obj = dev_priv->mm.phys_objs[id - 1];
4853 if (phys_obj->cur_obj) {
4854 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
4855 }
4856
4857#ifdef CONFIG_X86
4858 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4859#endif
4860 drm_pci_free(dev, phys_obj->handle);
4861 kfree(phys_obj);
4862 dev_priv->mm.phys_objs[id - 1] = NULL;
4863}
4864
4865void i915_gem_free_all_phys_object(struct drm_device *dev)
4866{
4867 int i;
4868
Dave Airlie260883c2009-01-22 17:58:49 +10004869 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004870 i915_gem_free_phys_object(dev, i);
4871}
4872
4873void i915_gem_detach_phys_object(struct drm_device *dev,
4874 struct drm_gem_object *obj)
4875{
4876 struct drm_i915_gem_object *obj_priv;
4877 int i;
4878 int ret;
4879 int page_count;
4880
Daniel Vetter23010e42010-03-08 13:35:02 +01004881 obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004882 if (!obj_priv->phys_obj)
4883 return;
4884
Chris Wilson4bdadb92010-01-27 13:36:32 +00004885 ret = i915_gem_object_get_pages(obj, 0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004886 if (ret)
4887 goto out;
4888
4889 page_count = obj->size / PAGE_SIZE;
4890
4891 for (i = 0; i < page_count; i++) {
Eric Anholt856fa192009-03-19 14:10:50 -07004892 char *dst = kmap_atomic(obj_priv->pages[i], KM_USER0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004893 char *src = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4894
4895 memcpy(dst, src, PAGE_SIZE);
4896 kunmap_atomic(dst, KM_USER0);
4897 }
Eric Anholt856fa192009-03-19 14:10:50 -07004898 drm_clflush_pages(obj_priv->pages, page_count);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004899 drm_agp_chipset_flush(dev);
Chris Wilsond78b47b2009-06-17 21:52:49 +01004900
4901 i915_gem_object_put_pages(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004902out:
4903 obj_priv->phys_obj->cur_obj = NULL;
4904 obj_priv->phys_obj = NULL;
4905}
4906
4907int
4908i915_gem_attach_phys_object(struct drm_device *dev,
4909 struct drm_gem_object *obj, int id)
4910{
4911 drm_i915_private_t *dev_priv = dev->dev_private;
4912 struct drm_i915_gem_object *obj_priv;
4913 int ret = 0;
4914 int page_count;
4915 int i;
4916
4917 if (id > I915_MAX_PHYS_OBJECT)
4918 return -EINVAL;
4919
Daniel Vetter23010e42010-03-08 13:35:02 +01004920 obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004921
4922 if (obj_priv->phys_obj) {
4923 if (obj_priv->phys_obj->id == id)
4924 return 0;
4925 i915_gem_detach_phys_object(dev, obj);
4926 }
4927
4928
4929 /* create a new object */
4930 if (!dev_priv->mm.phys_objs[id - 1]) {
4931 ret = i915_gem_init_phys_object(dev, id,
4932 obj->size);
4933 if (ret) {
Linus Torvaldsaeb565d2009-01-26 10:01:53 -08004934 DRM_ERROR("failed to init phys object %d size: %zu\n", id, obj->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004935 goto out;
4936 }
4937 }
4938
4939 /* bind to the object */
4940 obj_priv->phys_obj = dev_priv->mm.phys_objs[id - 1];
4941 obj_priv->phys_obj->cur_obj = obj;
4942
Chris Wilson4bdadb92010-01-27 13:36:32 +00004943 ret = i915_gem_object_get_pages(obj, 0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004944 if (ret) {
4945 DRM_ERROR("failed to get page list\n");
4946 goto out;
4947 }
4948
4949 page_count = obj->size / PAGE_SIZE;
4950
4951 for (i = 0; i < page_count; i++) {
Eric Anholt856fa192009-03-19 14:10:50 -07004952 char *src = kmap_atomic(obj_priv->pages[i], KM_USER0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004953 char *dst = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4954
4955 memcpy(dst, src, PAGE_SIZE);
4956 kunmap_atomic(src, KM_USER0);
4957 }
4958
Chris Wilsond78b47b2009-06-17 21:52:49 +01004959 i915_gem_object_put_pages(obj);
4960
Dave Airlie71acb5e2008-12-30 20:31:46 +10004961 return 0;
4962out:
4963 return ret;
4964}
4965
4966static int
4967i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
4968 struct drm_i915_gem_pwrite *args,
4969 struct drm_file *file_priv)
4970{
Daniel Vetter23010e42010-03-08 13:35:02 +01004971 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004972 void *obj_addr;
4973 int ret;
4974 char __user *user_data;
4975
4976 user_data = (char __user *) (uintptr_t) args->data_ptr;
4977 obj_addr = obj_priv->phys_obj->handle->vaddr + args->offset;
4978
Zhao Yakui44d98a62009-10-09 11:39:40 +08004979 DRM_DEBUG_DRIVER("obj_addr %p, %lld\n", obj_addr, args->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004980 ret = copy_from_user(obj_addr, user_data, args->size);
4981 if (ret)
4982 return -EFAULT;
4983
4984 drm_agp_chipset_flush(dev);
4985 return 0;
4986}
Eric Anholtb9624422009-06-03 07:27:35 +00004987
4988void i915_gem_release(struct drm_device * dev, struct drm_file *file_priv)
4989{
4990 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
4991
4992 /* Clean up our request list when the client is going away, so that
4993 * later retire_requests won't dereference our soon-to-be-gone
4994 * file_priv.
4995 */
4996 mutex_lock(&dev->struct_mutex);
4997 while (!list_empty(&i915_file_priv->mm.request_list))
4998 list_del_init(i915_file_priv->mm.request_list.next);
4999 mutex_unlock(&dev->struct_mutex);
5000}
Chris Wilson31169712009-09-14 16:50:28 +01005001
Chris Wilson31169712009-09-14 16:50:28 +01005002static int
Chris Wilson1637ef42010-04-20 17:10:35 +01005003i915_gpu_is_active(struct drm_device *dev)
5004{
5005 drm_i915_private_t *dev_priv = dev->dev_private;
5006 int lists_empty;
5007
5008 spin_lock(&dev_priv->mm.active_list_lock);
5009 lists_empty = list_empty(&dev_priv->mm.flushing_list) &&
Zou Nan hai852835f2010-05-21 09:08:56 +08005010 list_empty(&dev_priv->render_ring.active_list);
Zou Nan haid1b851f2010-05-21 09:08:57 +08005011 if (HAS_BSD(dev))
5012 lists_empty &= list_empty(&dev_priv->bsd_ring.active_list);
Chris Wilson1637ef42010-04-20 17:10:35 +01005013 spin_unlock(&dev_priv->mm.active_list_lock);
5014
5015 return !lists_empty;
5016}
5017
5018static int
Dave Chinner7f8275d2010-07-19 14:56:17 +10005019i915_gem_shrink(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask)
Chris Wilson31169712009-09-14 16:50:28 +01005020{
5021 drm_i915_private_t *dev_priv, *next_dev;
5022 struct drm_i915_gem_object *obj_priv, *next_obj;
5023 int cnt = 0;
5024 int would_deadlock = 1;
5025
5026 /* "fast-path" to count number of available objects */
5027 if (nr_to_scan == 0) {
5028 spin_lock(&shrink_list_lock);
5029 list_for_each_entry(dev_priv, &shrink_list, mm.shrink_list) {
5030 struct drm_device *dev = dev_priv->dev;
5031
5032 if (mutex_trylock(&dev->struct_mutex)) {
5033 list_for_each_entry(obj_priv,
5034 &dev_priv->mm.inactive_list,
5035 list)
5036 cnt++;
5037 mutex_unlock(&dev->struct_mutex);
5038 }
5039 }
5040 spin_unlock(&shrink_list_lock);
5041
5042 return (cnt / 100) * sysctl_vfs_cache_pressure;
5043 }
5044
5045 spin_lock(&shrink_list_lock);
5046
Chris Wilson1637ef42010-04-20 17:10:35 +01005047rescan:
Chris Wilson31169712009-09-14 16:50:28 +01005048 /* first scan for clean buffers */
5049 list_for_each_entry_safe(dev_priv, next_dev,
5050 &shrink_list, mm.shrink_list) {
5051 struct drm_device *dev = dev_priv->dev;
5052
5053 if (! mutex_trylock(&dev->struct_mutex))
5054 continue;
5055
5056 spin_unlock(&shrink_list_lock);
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01005057 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08005058
Chris Wilson31169712009-09-14 16:50:28 +01005059 list_for_each_entry_safe(obj_priv, next_obj,
5060 &dev_priv->mm.inactive_list,
5061 list) {
5062 if (i915_gem_object_is_purgeable(obj_priv)) {
Daniel Vettera8089e82010-04-09 19:05:09 +00005063 i915_gem_object_unbind(&obj_priv->base);
Chris Wilson31169712009-09-14 16:50:28 +01005064 if (--nr_to_scan <= 0)
5065 break;
5066 }
5067 }
5068
5069 spin_lock(&shrink_list_lock);
5070 mutex_unlock(&dev->struct_mutex);
5071
Chris Wilson963b4832009-09-20 23:03:54 +01005072 would_deadlock = 0;
5073
Chris Wilson31169712009-09-14 16:50:28 +01005074 if (nr_to_scan <= 0)
5075 break;
5076 }
5077
5078 /* second pass, evict/count anything still on the inactive list */
5079 list_for_each_entry_safe(dev_priv, next_dev,
5080 &shrink_list, mm.shrink_list) {
5081 struct drm_device *dev = dev_priv->dev;
5082
5083 if (! mutex_trylock(&dev->struct_mutex))
5084 continue;
5085
5086 spin_unlock(&shrink_list_lock);
5087
5088 list_for_each_entry_safe(obj_priv, next_obj,
5089 &dev_priv->mm.inactive_list,
5090 list) {
5091 if (nr_to_scan > 0) {
Daniel Vettera8089e82010-04-09 19:05:09 +00005092 i915_gem_object_unbind(&obj_priv->base);
Chris Wilson31169712009-09-14 16:50:28 +01005093 nr_to_scan--;
5094 } else
5095 cnt++;
5096 }
5097
5098 spin_lock(&shrink_list_lock);
5099 mutex_unlock(&dev->struct_mutex);
5100
5101 would_deadlock = 0;
5102 }
5103
Chris Wilson1637ef42010-04-20 17:10:35 +01005104 if (nr_to_scan) {
5105 int active = 0;
5106
5107 /*
5108 * We are desperate for pages, so as a last resort, wait
5109 * for the GPU to finish and discard whatever we can.
5110 * This has a dramatic impact to reduce the number of
5111 * OOM-killer events whilst running the GPU aggressively.
5112 */
5113 list_for_each_entry(dev_priv, &shrink_list, mm.shrink_list) {
5114 struct drm_device *dev = dev_priv->dev;
5115
5116 if (!mutex_trylock(&dev->struct_mutex))
5117 continue;
5118
5119 spin_unlock(&shrink_list_lock);
5120
5121 if (i915_gpu_is_active(dev)) {
5122 i915_gpu_idle(dev);
5123 active++;
5124 }
5125
5126 spin_lock(&shrink_list_lock);
5127 mutex_unlock(&dev->struct_mutex);
5128 }
5129
5130 if (active)
5131 goto rescan;
5132 }
5133
Chris Wilson31169712009-09-14 16:50:28 +01005134 spin_unlock(&shrink_list_lock);
5135
5136 if (would_deadlock)
5137 return -1;
5138 else if (cnt > 0)
5139 return (cnt / 100) * sysctl_vfs_cache_pressure;
5140 else
5141 return 0;
5142}
5143
5144static struct shrinker shrinker = {
5145 .shrink = i915_gem_shrink,
5146 .seeks = DEFAULT_SEEKS,
5147};
5148
5149__init void
5150i915_gem_shrinker_init(void)
5151{
5152 register_shrinker(&shrinker);
5153}
5154
5155__exit void
5156i915_gem_shrinker_exit(void)
5157{
5158 unregister_shrinker(&shrinker);
5159}