blob: 47c46ed384f1b690c1876e271df9a0de0ca034e2 [file] [log] [blame]
Eric Anholt673a3942008-07-30 12:06:12 -07001/*
2 * Copyright © 2008 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 *
26 */
27
28#include "drmP.h"
29#include "drm.h"
30#include "i915_drm.h"
31#include "i915_drv.h"
Chris Wilson1c5d22f2009-08-25 11:15:50 +010032#include "i915_trace.h"
Jesse Barnes652c3932009-08-17 13:31:43 -070033#include "intel_drv.h"
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Eric Anholt673a3942008-07-30 12:06:12 -070035#include <linux/swap.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080036#include <linux/pci.h>
Eric Anholt673a3942008-07-30 12:06:12 -070037
Eric Anholt28dfe522008-11-13 15:00:55 -080038#define I915_GEM_GPU_DOMAINS (~(I915_GEM_DOMAIN_CPU | I915_GEM_DOMAIN_GTT))
39
Eric Anholte47c68e2008-11-14 13:35:19 -080040static void i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj);
41static void i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj);
42static void i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj);
Eric Anholte47c68e2008-11-14 13:35:19 -080043static int i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj,
44 int write);
45static int i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
46 uint64_t offset,
47 uint64_t size);
48static void i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj);
Eric Anholt673a3942008-07-30 12:06:12 -070049static int i915_gem_object_wait_rendering(struct drm_gem_object *obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -080050static int i915_gem_object_bind_to_gtt(struct drm_gem_object *obj,
51 unsigned alignment);
Jesse Barnesde151cf2008-11-12 10:03:55 -080052static void i915_gem_clear_fence_reg(struct drm_gem_object *obj);
Chris Wilson07f73f62009-09-14 16:50:30 +010053static int i915_gem_evict_something(struct drm_device *dev, int min_size);
Chris Wilsonab5ee572009-09-20 19:25:47 +010054static int i915_gem_evict_from_inactive_list(struct drm_device *dev);
Dave Airlie71acb5e2008-12-30 20:31:46 +100055static int i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
56 struct drm_i915_gem_pwrite *args,
57 struct drm_file *file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -070058
Chris Wilson31169712009-09-14 16:50:28 +010059static LIST_HEAD(shrink_list);
60static DEFINE_SPINLOCK(shrink_list_lock);
61
Jesse Barnes79e53942008-11-07 14:24:08 -080062int i915_gem_do_init(struct drm_device *dev, unsigned long start,
63 unsigned long end)
64{
65 drm_i915_private_t *dev_priv = dev->dev_private;
66
67 if (start >= end ||
68 (start & (PAGE_SIZE - 1)) != 0 ||
69 (end & (PAGE_SIZE - 1)) != 0) {
70 return -EINVAL;
71 }
72
73 drm_mm_init(&dev_priv->mm.gtt_space, start,
74 end - start);
75
76 dev->gtt_total = (uint32_t) (end - start);
77
78 return 0;
79}
Keith Packard6dbe2772008-10-14 21:41:13 -070080
Eric Anholt673a3942008-07-30 12:06:12 -070081int
82i915_gem_init_ioctl(struct drm_device *dev, void *data,
83 struct drm_file *file_priv)
84{
Eric Anholt673a3942008-07-30 12:06:12 -070085 struct drm_i915_gem_init *args = data;
Jesse Barnes79e53942008-11-07 14:24:08 -080086 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -070087
88 mutex_lock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080089 ret = i915_gem_do_init(dev, args->gtt_start, args->gtt_end);
Eric Anholt673a3942008-07-30 12:06:12 -070090 mutex_unlock(&dev->struct_mutex);
91
Jesse Barnes79e53942008-11-07 14:24:08 -080092 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -070093}
94
Eric Anholt5a125c32008-10-22 21:40:13 -070095int
96i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
97 struct drm_file *file_priv)
98{
Eric Anholt5a125c32008-10-22 21:40:13 -070099 struct drm_i915_gem_get_aperture *args = data;
Eric Anholt5a125c32008-10-22 21:40:13 -0700100
101 if (!(dev->driver->driver_features & DRIVER_GEM))
102 return -ENODEV;
103
104 args->aper_size = dev->gtt_total;
Keith Packard2678d9d2008-11-20 22:54:54 -0800105 args->aper_available_size = (args->aper_size -
106 atomic_read(&dev->pin_memory));
Eric Anholt5a125c32008-10-22 21:40:13 -0700107
108 return 0;
109}
110
Eric Anholt673a3942008-07-30 12:06:12 -0700111
112/**
113 * Creates a new mm object and returns a handle to it.
114 */
115int
116i915_gem_create_ioctl(struct drm_device *dev, void *data,
117 struct drm_file *file_priv)
118{
119 struct drm_i915_gem_create *args = data;
120 struct drm_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300121 int ret;
122 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700123
124 args->size = roundup(args->size, PAGE_SIZE);
125
126 /* Allocate the new object */
Daniel Vetterac52bc52010-04-09 19:05:06 +0000127 obj = i915_gem_alloc_object(dev, args->size);
Eric Anholt673a3942008-07-30 12:06:12 -0700128 if (obj == NULL)
129 return -ENOMEM;
130
131 ret = drm_gem_handle_create(file_priv, obj, &handle);
Luca Barbieribc9025b2010-02-09 05:49:12 +0000132 drm_gem_object_handle_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700133
134 if (ret)
135 return ret;
136
137 args->handle = handle;
138
139 return 0;
140}
141
Eric Anholt40123c12009-03-09 13:42:30 -0700142static inline int
Eric Anholteb014592009-03-10 11:44:52 -0700143fast_shmem_read(struct page **pages,
144 loff_t page_base, int page_offset,
145 char __user *data,
146 int length)
147{
148 char __iomem *vaddr;
Florian Mickler2bc43b52009-04-06 22:55:41 +0200149 int unwritten;
Eric Anholteb014592009-03-10 11:44:52 -0700150
151 vaddr = kmap_atomic(pages[page_base >> PAGE_SHIFT], KM_USER0);
152 if (vaddr == NULL)
153 return -ENOMEM;
Florian Mickler2bc43b52009-04-06 22:55:41 +0200154 unwritten = __copy_to_user_inatomic(data, vaddr + page_offset, length);
Eric Anholteb014592009-03-10 11:44:52 -0700155 kunmap_atomic(vaddr, KM_USER0);
156
Florian Mickler2bc43b52009-04-06 22:55:41 +0200157 if (unwritten)
158 return -EFAULT;
159
160 return 0;
Eric Anholteb014592009-03-10 11:44:52 -0700161}
162
Eric Anholt280b7132009-03-12 16:56:27 -0700163static int i915_gem_object_needs_bit17_swizzle(struct drm_gem_object *obj)
164{
165 drm_i915_private_t *dev_priv = obj->dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +0100166 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt280b7132009-03-12 16:56:27 -0700167
168 return dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_9_10_17 &&
169 obj_priv->tiling_mode != I915_TILING_NONE;
170}
171
Eric Anholteb014592009-03-10 11:44:52 -0700172static inline int
Eric Anholt40123c12009-03-09 13:42:30 -0700173slow_shmem_copy(struct page *dst_page,
174 int dst_offset,
175 struct page *src_page,
176 int src_offset,
177 int length)
178{
179 char *dst_vaddr, *src_vaddr;
180
181 dst_vaddr = kmap_atomic(dst_page, KM_USER0);
182 if (dst_vaddr == NULL)
183 return -ENOMEM;
184
185 src_vaddr = kmap_atomic(src_page, KM_USER1);
186 if (src_vaddr == NULL) {
187 kunmap_atomic(dst_vaddr, KM_USER0);
188 return -ENOMEM;
189 }
190
191 memcpy(dst_vaddr + dst_offset, src_vaddr + src_offset, length);
192
193 kunmap_atomic(src_vaddr, KM_USER1);
194 kunmap_atomic(dst_vaddr, KM_USER0);
195
196 return 0;
197}
198
Eric Anholt280b7132009-03-12 16:56:27 -0700199static inline int
200slow_shmem_bit17_copy(struct page *gpu_page,
201 int gpu_offset,
202 struct page *cpu_page,
203 int cpu_offset,
204 int length,
205 int is_read)
206{
207 char *gpu_vaddr, *cpu_vaddr;
208
209 /* Use the unswizzled path if this page isn't affected. */
210 if ((page_to_phys(gpu_page) & (1 << 17)) == 0) {
211 if (is_read)
212 return slow_shmem_copy(cpu_page, cpu_offset,
213 gpu_page, gpu_offset, length);
214 else
215 return slow_shmem_copy(gpu_page, gpu_offset,
216 cpu_page, cpu_offset, length);
217 }
218
219 gpu_vaddr = kmap_atomic(gpu_page, KM_USER0);
220 if (gpu_vaddr == NULL)
221 return -ENOMEM;
222
223 cpu_vaddr = kmap_atomic(cpu_page, KM_USER1);
224 if (cpu_vaddr == NULL) {
225 kunmap_atomic(gpu_vaddr, KM_USER0);
226 return -ENOMEM;
227 }
228
229 /* Copy the data, XORing A6 with A17 (1). The user already knows he's
230 * XORing with the other bits (A9 for Y, A9 and A10 for X)
231 */
232 while (length > 0) {
233 int cacheline_end = ALIGN(gpu_offset + 1, 64);
234 int this_length = min(cacheline_end - gpu_offset, length);
235 int swizzled_gpu_offset = gpu_offset ^ 64;
236
237 if (is_read) {
238 memcpy(cpu_vaddr + cpu_offset,
239 gpu_vaddr + swizzled_gpu_offset,
240 this_length);
241 } else {
242 memcpy(gpu_vaddr + swizzled_gpu_offset,
243 cpu_vaddr + cpu_offset,
244 this_length);
245 }
246 cpu_offset += this_length;
247 gpu_offset += this_length;
248 length -= this_length;
249 }
250
251 kunmap_atomic(cpu_vaddr, KM_USER1);
252 kunmap_atomic(gpu_vaddr, KM_USER0);
253
254 return 0;
255}
256
Eric Anholt673a3942008-07-30 12:06:12 -0700257/**
Eric Anholteb014592009-03-10 11:44:52 -0700258 * This is the fast shmem pread path, which attempts to copy_from_user directly
259 * from the backing pages of the object to the user's address space. On a
260 * fault, it fails so we can fall back to i915_gem_shmem_pwrite_slow().
261 */
262static int
263i915_gem_shmem_pread_fast(struct drm_device *dev, struct drm_gem_object *obj,
264 struct drm_i915_gem_pread *args,
265 struct drm_file *file_priv)
266{
Daniel Vetter23010e42010-03-08 13:35:02 +0100267 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700268 ssize_t remain;
269 loff_t offset, page_base;
270 char __user *user_data;
271 int page_offset, page_length;
272 int ret;
273
274 user_data = (char __user *) (uintptr_t) args->data_ptr;
275 remain = args->size;
276
277 mutex_lock(&dev->struct_mutex);
278
Chris Wilson4bdadb92010-01-27 13:36:32 +0000279 ret = i915_gem_object_get_pages(obj, 0);
Eric Anholteb014592009-03-10 11:44:52 -0700280 if (ret != 0)
281 goto fail_unlock;
282
283 ret = i915_gem_object_set_cpu_read_domain_range(obj, args->offset,
284 args->size);
285 if (ret != 0)
286 goto fail_put_pages;
287
Daniel Vetter23010e42010-03-08 13:35:02 +0100288 obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700289 offset = args->offset;
290
291 while (remain > 0) {
292 /* Operation in this page
293 *
294 * page_base = page offset within aperture
295 * page_offset = offset within page
296 * page_length = bytes to copy for this page
297 */
298 page_base = (offset & ~(PAGE_SIZE-1));
299 page_offset = offset & (PAGE_SIZE-1);
300 page_length = remain;
301 if ((page_offset + remain) > PAGE_SIZE)
302 page_length = PAGE_SIZE - page_offset;
303
304 ret = fast_shmem_read(obj_priv->pages,
305 page_base, page_offset,
306 user_data, page_length);
307 if (ret)
308 goto fail_put_pages;
309
310 remain -= page_length;
311 user_data += page_length;
312 offset += page_length;
313 }
314
315fail_put_pages:
316 i915_gem_object_put_pages(obj);
317fail_unlock:
318 mutex_unlock(&dev->struct_mutex);
319
320 return ret;
321}
322
Chris Wilson07f73f62009-09-14 16:50:30 +0100323static int
324i915_gem_object_get_pages_or_evict(struct drm_gem_object *obj)
325{
326 int ret;
327
Chris Wilson4bdadb92010-01-27 13:36:32 +0000328 ret = i915_gem_object_get_pages(obj, __GFP_NORETRY | __GFP_NOWARN);
Chris Wilson07f73f62009-09-14 16:50:30 +0100329
330 /* If we've insufficient memory to map in the pages, attempt
331 * to make some space by throwing out some old buffers.
332 */
333 if (ret == -ENOMEM) {
334 struct drm_device *dev = obj->dev;
Chris Wilson07f73f62009-09-14 16:50:30 +0100335
336 ret = i915_gem_evict_something(dev, obj->size);
337 if (ret)
338 return ret;
339
Chris Wilson4bdadb92010-01-27 13:36:32 +0000340 ret = i915_gem_object_get_pages(obj, 0);
Chris Wilson07f73f62009-09-14 16:50:30 +0100341 }
342
343 return ret;
344}
345
Eric Anholteb014592009-03-10 11:44:52 -0700346/**
347 * This is the fallback shmem pread path, which allocates temporary storage
348 * in kernel space to copy_to_user into outside of the struct_mutex, so we
349 * can copy out of the object's backing pages while holding the struct mutex
350 * and not take page faults.
351 */
352static int
353i915_gem_shmem_pread_slow(struct drm_device *dev, struct drm_gem_object *obj,
354 struct drm_i915_gem_pread *args,
355 struct drm_file *file_priv)
356{
Daniel Vetter23010e42010-03-08 13:35:02 +0100357 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700358 struct mm_struct *mm = current->mm;
359 struct page **user_pages;
360 ssize_t remain;
361 loff_t offset, pinned_pages, i;
362 loff_t first_data_page, last_data_page, num_pages;
363 int shmem_page_index, shmem_page_offset;
364 int data_page_index, data_page_offset;
365 int page_length;
366 int ret;
367 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700368 int do_bit17_swizzling;
Eric Anholteb014592009-03-10 11:44:52 -0700369
370 remain = args->size;
371
372 /* Pin the user pages containing the data. We can't fault while
373 * holding the struct mutex, yet we want to hold it while
374 * dereferencing the user data.
375 */
376 first_data_page = data_ptr / PAGE_SIZE;
377 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
378 num_pages = last_data_page - first_data_page + 1;
379
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700380 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
Eric Anholteb014592009-03-10 11:44:52 -0700381 if (user_pages == NULL)
382 return -ENOMEM;
383
384 down_read(&mm->mmap_sem);
385 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
Eric Anholte5e9ecd2009-04-07 16:01:22 -0700386 num_pages, 1, 0, user_pages, NULL);
Eric Anholteb014592009-03-10 11:44:52 -0700387 up_read(&mm->mmap_sem);
388 if (pinned_pages < num_pages) {
389 ret = -EFAULT;
390 goto fail_put_user_pages;
391 }
392
Eric Anholt280b7132009-03-12 16:56:27 -0700393 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
394
Eric Anholteb014592009-03-10 11:44:52 -0700395 mutex_lock(&dev->struct_mutex);
396
Chris Wilson07f73f62009-09-14 16:50:30 +0100397 ret = i915_gem_object_get_pages_or_evict(obj);
398 if (ret)
Eric Anholteb014592009-03-10 11:44:52 -0700399 goto fail_unlock;
400
401 ret = i915_gem_object_set_cpu_read_domain_range(obj, args->offset,
402 args->size);
403 if (ret != 0)
404 goto fail_put_pages;
405
Daniel Vetter23010e42010-03-08 13:35:02 +0100406 obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700407 offset = args->offset;
408
409 while (remain > 0) {
410 /* Operation in this page
411 *
412 * shmem_page_index = page number within shmem file
413 * shmem_page_offset = offset within page in shmem file
414 * data_page_index = page number in get_user_pages return
415 * data_page_offset = offset with data_page_index page.
416 * page_length = bytes to copy for this page
417 */
418 shmem_page_index = offset / PAGE_SIZE;
419 shmem_page_offset = offset & ~PAGE_MASK;
420 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
421 data_page_offset = data_ptr & ~PAGE_MASK;
422
423 page_length = remain;
424 if ((shmem_page_offset + page_length) > PAGE_SIZE)
425 page_length = PAGE_SIZE - shmem_page_offset;
426 if ((data_page_offset + page_length) > PAGE_SIZE)
427 page_length = PAGE_SIZE - data_page_offset;
428
Eric Anholt280b7132009-03-12 16:56:27 -0700429 if (do_bit17_swizzling) {
430 ret = slow_shmem_bit17_copy(obj_priv->pages[shmem_page_index],
431 shmem_page_offset,
432 user_pages[data_page_index],
433 data_page_offset,
434 page_length,
435 1);
436 } else {
437 ret = slow_shmem_copy(user_pages[data_page_index],
438 data_page_offset,
439 obj_priv->pages[shmem_page_index],
440 shmem_page_offset,
441 page_length);
442 }
Eric Anholteb014592009-03-10 11:44:52 -0700443 if (ret)
444 goto fail_put_pages;
445
446 remain -= page_length;
447 data_ptr += page_length;
448 offset += page_length;
449 }
450
451fail_put_pages:
452 i915_gem_object_put_pages(obj);
453fail_unlock:
454 mutex_unlock(&dev->struct_mutex);
455fail_put_user_pages:
456 for (i = 0; i < pinned_pages; i++) {
457 SetPageDirty(user_pages[i]);
458 page_cache_release(user_pages[i]);
459 }
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700460 drm_free_large(user_pages);
Eric Anholteb014592009-03-10 11:44:52 -0700461
462 return ret;
463}
464
Eric Anholt673a3942008-07-30 12:06:12 -0700465/**
466 * Reads data from the object referenced by handle.
467 *
468 * On error, the contents of *data are undefined.
469 */
470int
471i915_gem_pread_ioctl(struct drm_device *dev, void *data,
472 struct drm_file *file_priv)
473{
474 struct drm_i915_gem_pread *args = data;
475 struct drm_gem_object *obj;
476 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -0700477 int ret;
478
479 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
480 if (obj == NULL)
481 return -EBADF;
Daniel Vetter23010e42010-03-08 13:35:02 +0100482 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700483
484 /* Bounds check source.
485 *
486 * XXX: This could use review for overflow issues...
487 */
488 if (args->offset > obj->size || args->size > obj->size ||
489 args->offset + args->size > obj->size) {
Luca Barbieribc9025b2010-02-09 05:49:12 +0000490 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700491 return -EINVAL;
492 }
493
Eric Anholt280b7132009-03-12 16:56:27 -0700494 if (i915_gem_object_needs_bit17_swizzle(obj)) {
Eric Anholteb014592009-03-10 11:44:52 -0700495 ret = i915_gem_shmem_pread_slow(dev, obj, args, file_priv);
Eric Anholt280b7132009-03-12 16:56:27 -0700496 } else {
497 ret = i915_gem_shmem_pread_fast(dev, obj, args, file_priv);
498 if (ret != 0)
499 ret = i915_gem_shmem_pread_slow(dev, obj, args,
500 file_priv);
501 }
Eric Anholt673a3942008-07-30 12:06:12 -0700502
Luca Barbieribc9025b2010-02-09 05:49:12 +0000503 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700504
Eric Anholteb014592009-03-10 11:44:52 -0700505 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700506}
507
Keith Packard0839ccb2008-10-30 19:38:48 -0700508/* This is the fast write path which cannot handle
509 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700510 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700511
Keith Packard0839ccb2008-10-30 19:38:48 -0700512static inline int
513fast_user_write(struct io_mapping *mapping,
514 loff_t page_base, int page_offset,
515 char __user *user_data,
516 int length)
517{
518 char *vaddr_atomic;
519 unsigned long unwritten;
520
521 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
522 unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
523 user_data, length);
524 io_mapping_unmap_atomic(vaddr_atomic);
525 if (unwritten)
526 return -EFAULT;
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700527 return 0;
Keith Packard0839ccb2008-10-30 19:38:48 -0700528}
529
530/* Here's the write path which can sleep for
531 * page faults
532 */
533
534static inline int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700535slow_kernel_write(struct io_mapping *mapping,
536 loff_t gtt_base, int gtt_offset,
537 struct page *user_page, int user_offset,
538 int length)
Keith Packard0839ccb2008-10-30 19:38:48 -0700539{
Eric Anholt3de09aa2009-03-09 09:42:23 -0700540 char *src_vaddr, *dst_vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -0700541 unsigned long unwritten;
542
Eric Anholt3de09aa2009-03-09 09:42:23 -0700543 dst_vaddr = io_mapping_map_atomic_wc(mapping, gtt_base);
544 src_vaddr = kmap_atomic(user_page, KM_USER1);
545 unwritten = __copy_from_user_inatomic_nocache(dst_vaddr + gtt_offset,
546 src_vaddr + user_offset,
547 length);
548 kunmap_atomic(src_vaddr, KM_USER1);
549 io_mapping_unmap_atomic(dst_vaddr);
Keith Packard0839ccb2008-10-30 19:38:48 -0700550 if (unwritten)
551 return -EFAULT;
552 return 0;
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700553}
554
Eric Anholt40123c12009-03-09 13:42:30 -0700555static inline int
556fast_shmem_write(struct page **pages,
557 loff_t page_base, int page_offset,
558 char __user *data,
559 int length)
560{
561 char __iomem *vaddr;
Dave Airlied0088772009-03-28 20:29:48 -0400562 unsigned long unwritten;
Eric Anholt40123c12009-03-09 13:42:30 -0700563
564 vaddr = kmap_atomic(pages[page_base >> PAGE_SHIFT], KM_USER0);
565 if (vaddr == NULL)
566 return -ENOMEM;
Dave Airlied0088772009-03-28 20:29:48 -0400567 unwritten = __copy_from_user_inatomic(vaddr + page_offset, data, length);
Eric Anholt40123c12009-03-09 13:42:30 -0700568 kunmap_atomic(vaddr, KM_USER0);
569
Dave Airlied0088772009-03-28 20:29:48 -0400570 if (unwritten)
571 return -EFAULT;
Eric Anholt40123c12009-03-09 13:42:30 -0700572 return 0;
573}
574
Eric Anholt3de09aa2009-03-09 09:42:23 -0700575/**
576 * This is the fast pwrite path, where we copy the data directly from the
577 * user into the GTT, uncached.
578 */
Eric Anholt673a3942008-07-30 12:06:12 -0700579static int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700580i915_gem_gtt_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
581 struct drm_i915_gem_pwrite *args,
582 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700583{
Daniel Vetter23010e42010-03-08 13:35:02 +0100584 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Keith Packard0839ccb2008-10-30 19:38:48 -0700585 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700586 ssize_t remain;
Keith Packard0839ccb2008-10-30 19:38:48 -0700587 loff_t offset, page_base;
Eric Anholt673a3942008-07-30 12:06:12 -0700588 char __user *user_data;
Keith Packard0839ccb2008-10-30 19:38:48 -0700589 int page_offset, page_length;
590 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700591
592 user_data = (char __user *) (uintptr_t) args->data_ptr;
593 remain = args->size;
594 if (!access_ok(VERIFY_READ, user_data, remain))
595 return -EFAULT;
596
597
598 mutex_lock(&dev->struct_mutex);
599 ret = i915_gem_object_pin(obj, 0);
600 if (ret) {
601 mutex_unlock(&dev->struct_mutex);
602 return ret;
603 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800604 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
Eric Anholt673a3942008-07-30 12:06:12 -0700605 if (ret)
606 goto fail;
607
Daniel Vetter23010e42010-03-08 13:35:02 +0100608 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700609 offset = obj_priv->gtt_offset + args->offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700610
611 while (remain > 0) {
612 /* Operation in this page
613 *
Keith Packard0839ccb2008-10-30 19:38:48 -0700614 * page_base = page offset within aperture
615 * page_offset = offset within page
616 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -0700617 */
Keith Packard0839ccb2008-10-30 19:38:48 -0700618 page_base = (offset & ~(PAGE_SIZE-1));
619 page_offset = offset & (PAGE_SIZE-1);
620 page_length = remain;
621 if ((page_offset + remain) > PAGE_SIZE)
622 page_length = PAGE_SIZE - page_offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700623
Keith Packard0839ccb2008-10-30 19:38:48 -0700624 ret = fast_user_write (dev_priv->mm.gtt_mapping, page_base,
625 page_offset, user_data, page_length);
Eric Anholt673a3942008-07-30 12:06:12 -0700626
Keith Packard0839ccb2008-10-30 19:38:48 -0700627 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -0700628 * source page isn't available. Return the error and we'll
629 * retry in the slow path.
Keith Packard0839ccb2008-10-30 19:38:48 -0700630 */
Eric Anholt3de09aa2009-03-09 09:42:23 -0700631 if (ret)
632 goto fail;
Eric Anholt673a3942008-07-30 12:06:12 -0700633
Keith Packard0839ccb2008-10-30 19:38:48 -0700634 remain -= page_length;
635 user_data += page_length;
636 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700637 }
Eric Anholt673a3942008-07-30 12:06:12 -0700638
639fail:
640 i915_gem_object_unpin(obj);
641 mutex_unlock(&dev->struct_mutex);
642
643 return ret;
644}
645
Eric Anholt3de09aa2009-03-09 09:42:23 -0700646/**
647 * This is the fallback GTT pwrite path, which uses get_user_pages to pin
648 * the memory and maps it using kmap_atomic for copying.
649 *
650 * This code resulted in x11perf -rgb10text consuming about 10% more CPU
651 * than using i915_gem_gtt_pwrite_fast on a G45 (32-bit).
652 */
Eric Anholt3043c602008-10-02 12:24:47 -0700653static int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700654i915_gem_gtt_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
655 struct drm_i915_gem_pwrite *args,
656 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700657{
Daniel Vetter23010e42010-03-08 13:35:02 +0100658 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700659 drm_i915_private_t *dev_priv = dev->dev_private;
660 ssize_t remain;
661 loff_t gtt_page_base, offset;
662 loff_t first_data_page, last_data_page, num_pages;
663 loff_t pinned_pages, i;
664 struct page **user_pages;
665 struct mm_struct *mm = current->mm;
666 int gtt_page_offset, data_page_offset, data_page_index, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700667 int ret;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700668 uint64_t data_ptr = args->data_ptr;
669
670 remain = args->size;
671
672 /* Pin the user pages containing the data. We can't fault while
673 * holding the struct mutex, and all of the pwrite implementations
674 * want to hold it while dereferencing the user data.
675 */
676 first_data_page = data_ptr / PAGE_SIZE;
677 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
678 num_pages = last_data_page - first_data_page + 1;
679
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700680 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
Eric Anholt3de09aa2009-03-09 09:42:23 -0700681 if (user_pages == NULL)
682 return -ENOMEM;
683
684 down_read(&mm->mmap_sem);
685 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
686 num_pages, 0, 0, user_pages, NULL);
687 up_read(&mm->mmap_sem);
688 if (pinned_pages < num_pages) {
689 ret = -EFAULT;
690 goto out_unpin_pages;
691 }
692
693 mutex_lock(&dev->struct_mutex);
694 ret = i915_gem_object_pin(obj, 0);
695 if (ret)
696 goto out_unlock;
697
698 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
699 if (ret)
700 goto out_unpin_object;
701
Daniel Vetter23010e42010-03-08 13:35:02 +0100702 obj_priv = to_intel_bo(obj);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700703 offset = obj_priv->gtt_offset + args->offset;
704
705 while (remain > 0) {
706 /* Operation in this page
707 *
708 * gtt_page_base = page offset within aperture
709 * gtt_page_offset = offset within page in aperture
710 * data_page_index = page number in get_user_pages return
711 * data_page_offset = offset with data_page_index page.
712 * page_length = bytes to copy for this page
713 */
714 gtt_page_base = offset & PAGE_MASK;
715 gtt_page_offset = offset & ~PAGE_MASK;
716 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
717 data_page_offset = data_ptr & ~PAGE_MASK;
718
719 page_length = remain;
720 if ((gtt_page_offset + page_length) > PAGE_SIZE)
721 page_length = PAGE_SIZE - gtt_page_offset;
722 if ((data_page_offset + page_length) > PAGE_SIZE)
723 page_length = PAGE_SIZE - data_page_offset;
724
725 ret = slow_kernel_write(dev_priv->mm.gtt_mapping,
726 gtt_page_base, gtt_page_offset,
727 user_pages[data_page_index],
728 data_page_offset,
729 page_length);
730
731 /* If we get a fault while copying data, then (presumably) our
732 * source page isn't available. Return the error and we'll
733 * retry in the slow path.
734 */
735 if (ret)
736 goto out_unpin_object;
737
738 remain -= page_length;
739 offset += page_length;
740 data_ptr += page_length;
741 }
742
743out_unpin_object:
744 i915_gem_object_unpin(obj);
745out_unlock:
746 mutex_unlock(&dev->struct_mutex);
747out_unpin_pages:
748 for (i = 0; i < pinned_pages; i++)
749 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700750 drm_free_large(user_pages);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700751
752 return ret;
753}
754
Eric Anholt40123c12009-03-09 13:42:30 -0700755/**
756 * This is the fast shmem pwrite path, which attempts to directly
757 * copy_from_user into the kmapped pages backing the object.
758 */
Eric Anholt673a3942008-07-30 12:06:12 -0700759static int
Eric Anholt40123c12009-03-09 13:42:30 -0700760i915_gem_shmem_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
761 struct drm_i915_gem_pwrite *args,
762 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700763{
Daniel Vetter23010e42010-03-08 13:35:02 +0100764 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700765 ssize_t remain;
766 loff_t offset, page_base;
767 char __user *user_data;
768 int page_offset, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700769 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -0700770
771 user_data = (char __user *) (uintptr_t) args->data_ptr;
772 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700773
774 mutex_lock(&dev->struct_mutex);
775
Chris Wilson4bdadb92010-01-27 13:36:32 +0000776 ret = i915_gem_object_get_pages(obj, 0);
Eric Anholt40123c12009-03-09 13:42:30 -0700777 if (ret != 0)
778 goto fail_unlock;
779
Eric Anholte47c68e2008-11-14 13:35:19 -0800780 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Eric Anholt40123c12009-03-09 13:42:30 -0700781 if (ret != 0)
782 goto fail_put_pages;
Eric Anholt673a3942008-07-30 12:06:12 -0700783
Daniel Vetter23010e42010-03-08 13:35:02 +0100784 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700785 offset = args->offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700786 obj_priv->dirty = 1;
Eric Anholt673a3942008-07-30 12:06:12 -0700787
Eric Anholt40123c12009-03-09 13:42:30 -0700788 while (remain > 0) {
789 /* Operation in this page
790 *
791 * page_base = page offset within aperture
792 * page_offset = offset within page
793 * page_length = bytes to copy for this page
794 */
795 page_base = (offset & ~(PAGE_SIZE-1));
796 page_offset = offset & (PAGE_SIZE-1);
797 page_length = remain;
798 if ((page_offset + remain) > PAGE_SIZE)
799 page_length = PAGE_SIZE - page_offset;
800
801 ret = fast_shmem_write(obj_priv->pages,
802 page_base, page_offset,
803 user_data, page_length);
804 if (ret)
805 goto fail_put_pages;
806
807 remain -= page_length;
808 user_data += page_length;
809 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700810 }
811
Eric Anholt40123c12009-03-09 13:42:30 -0700812fail_put_pages:
813 i915_gem_object_put_pages(obj);
814fail_unlock:
Eric Anholt673a3942008-07-30 12:06:12 -0700815 mutex_unlock(&dev->struct_mutex);
816
Eric Anholt40123c12009-03-09 13:42:30 -0700817 return ret;
818}
819
820/**
821 * This is the fallback shmem pwrite path, which uses get_user_pages to pin
822 * the memory and maps it using kmap_atomic for copying.
823 *
824 * This avoids taking mmap_sem for faulting on the user's address while the
825 * struct_mutex is held.
826 */
827static int
828i915_gem_shmem_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
829 struct drm_i915_gem_pwrite *args,
830 struct drm_file *file_priv)
831{
Daniel Vetter23010e42010-03-08 13:35:02 +0100832 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700833 struct mm_struct *mm = current->mm;
834 struct page **user_pages;
835 ssize_t remain;
836 loff_t offset, pinned_pages, i;
837 loff_t first_data_page, last_data_page, num_pages;
838 int shmem_page_index, shmem_page_offset;
839 int data_page_index, data_page_offset;
840 int page_length;
841 int ret;
842 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700843 int do_bit17_swizzling;
Eric Anholt40123c12009-03-09 13:42:30 -0700844
845 remain = args->size;
846
847 /* Pin the user pages containing the data. We can't fault while
848 * holding the struct mutex, and all of the pwrite implementations
849 * want to hold it while dereferencing the user data.
850 */
851 first_data_page = data_ptr / PAGE_SIZE;
852 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
853 num_pages = last_data_page - first_data_page + 1;
854
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700855 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
Eric Anholt40123c12009-03-09 13:42:30 -0700856 if (user_pages == NULL)
857 return -ENOMEM;
858
859 down_read(&mm->mmap_sem);
860 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
861 num_pages, 0, 0, user_pages, NULL);
862 up_read(&mm->mmap_sem);
863 if (pinned_pages < num_pages) {
864 ret = -EFAULT;
865 goto fail_put_user_pages;
866 }
867
Eric Anholt280b7132009-03-12 16:56:27 -0700868 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
869
Eric Anholt40123c12009-03-09 13:42:30 -0700870 mutex_lock(&dev->struct_mutex);
871
Chris Wilson07f73f62009-09-14 16:50:30 +0100872 ret = i915_gem_object_get_pages_or_evict(obj);
873 if (ret)
Eric Anholt40123c12009-03-09 13:42:30 -0700874 goto fail_unlock;
875
876 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
877 if (ret != 0)
878 goto fail_put_pages;
879
Daniel Vetter23010e42010-03-08 13:35:02 +0100880 obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700881 offset = args->offset;
882 obj_priv->dirty = 1;
883
884 while (remain > 0) {
885 /* Operation in this page
886 *
887 * shmem_page_index = page number within shmem file
888 * shmem_page_offset = offset within page in shmem file
889 * data_page_index = page number in get_user_pages return
890 * data_page_offset = offset with data_page_index page.
891 * page_length = bytes to copy for this page
892 */
893 shmem_page_index = offset / PAGE_SIZE;
894 shmem_page_offset = offset & ~PAGE_MASK;
895 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
896 data_page_offset = data_ptr & ~PAGE_MASK;
897
898 page_length = remain;
899 if ((shmem_page_offset + page_length) > PAGE_SIZE)
900 page_length = PAGE_SIZE - shmem_page_offset;
901 if ((data_page_offset + page_length) > PAGE_SIZE)
902 page_length = PAGE_SIZE - data_page_offset;
903
Eric Anholt280b7132009-03-12 16:56:27 -0700904 if (do_bit17_swizzling) {
905 ret = slow_shmem_bit17_copy(obj_priv->pages[shmem_page_index],
906 shmem_page_offset,
907 user_pages[data_page_index],
908 data_page_offset,
909 page_length,
910 0);
911 } else {
912 ret = slow_shmem_copy(obj_priv->pages[shmem_page_index],
913 shmem_page_offset,
914 user_pages[data_page_index],
915 data_page_offset,
916 page_length);
917 }
Eric Anholt40123c12009-03-09 13:42:30 -0700918 if (ret)
919 goto fail_put_pages;
920
921 remain -= page_length;
922 data_ptr += page_length;
923 offset += page_length;
924 }
925
926fail_put_pages:
927 i915_gem_object_put_pages(obj);
928fail_unlock:
929 mutex_unlock(&dev->struct_mutex);
930fail_put_user_pages:
931 for (i = 0; i < pinned_pages; i++)
932 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700933 drm_free_large(user_pages);
Eric Anholt40123c12009-03-09 13:42:30 -0700934
935 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700936}
937
938/**
939 * Writes data to the object referenced by handle.
940 *
941 * On error, the contents of the buffer that were to be modified are undefined.
942 */
943int
944i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
945 struct drm_file *file_priv)
946{
947 struct drm_i915_gem_pwrite *args = data;
948 struct drm_gem_object *obj;
949 struct drm_i915_gem_object *obj_priv;
950 int ret = 0;
951
952 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
953 if (obj == NULL)
954 return -EBADF;
Daniel Vetter23010e42010-03-08 13:35:02 +0100955 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700956
957 /* Bounds check destination.
958 *
959 * XXX: This could use review for overflow issues...
960 */
961 if (args->offset > obj->size || args->size > obj->size ||
962 args->offset + args->size > obj->size) {
Luca Barbieribc9025b2010-02-09 05:49:12 +0000963 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700964 return -EINVAL;
965 }
966
967 /* We can only do the GTT pwrite on untiled buffers, as otherwise
968 * it would end up going through the fenced access, and we'll get
969 * different detiling behavior between reading and writing.
970 * pread/pwrite currently are reading and writing from the CPU
971 * perspective, requiring manual detiling by the client.
972 */
Dave Airlie71acb5e2008-12-30 20:31:46 +1000973 if (obj_priv->phys_obj)
974 ret = i915_gem_phys_pwrite(dev, obj, args, file_priv);
975 else if (obj_priv->tiling_mode == I915_TILING_NONE &&
Eric Anholt3de09aa2009-03-09 09:42:23 -0700976 dev->gtt_total != 0) {
977 ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file_priv);
978 if (ret == -EFAULT) {
979 ret = i915_gem_gtt_pwrite_slow(dev, obj, args,
980 file_priv);
981 }
Eric Anholt280b7132009-03-12 16:56:27 -0700982 } else if (i915_gem_object_needs_bit17_swizzle(obj)) {
983 ret = i915_gem_shmem_pwrite_slow(dev, obj, args, file_priv);
Eric Anholt40123c12009-03-09 13:42:30 -0700984 } else {
985 ret = i915_gem_shmem_pwrite_fast(dev, obj, args, file_priv);
986 if (ret == -EFAULT) {
987 ret = i915_gem_shmem_pwrite_slow(dev, obj, args,
988 file_priv);
989 }
990 }
Eric Anholt673a3942008-07-30 12:06:12 -0700991
992#if WATCH_PWRITE
993 if (ret)
994 DRM_INFO("pwrite failed %d\n", ret);
995#endif
996
Luca Barbieribc9025b2010-02-09 05:49:12 +0000997 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700998
999 return ret;
1000}
1001
1002/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001003 * Called when user space prepares to use an object with the CPU, either
1004 * through the mmap ioctl's mapping or a GTT mapping.
Eric Anholt673a3942008-07-30 12:06:12 -07001005 */
1006int
1007i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
1008 struct drm_file *file_priv)
1009{
Eric Anholta09ba7f2009-08-29 12:49:51 -07001010 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07001011 struct drm_i915_gem_set_domain *args = data;
1012 struct drm_gem_object *obj;
Jesse Barnes652c3932009-08-17 13:31:43 -07001013 struct drm_i915_gem_object *obj_priv;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001014 uint32_t read_domains = args->read_domains;
1015 uint32_t write_domain = args->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07001016 int ret;
1017
1018 if (!(dev->driver->driver_features & DRIVER_GEM))
1019 return -ENODEV;
1020
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001021 /* Only handle setting domains to types used by the CPU. */
Chris Wilson21d509e2009-06-06 09:46:02 +01001022 if (write_domain & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001023 return -EINVAL;
1024
Chris Wilson21d509e2009-06-06 09:46:02 +01001025 if (read_domains & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001026 return -EINVAL;
1027
1028 /* Having something in the write domain implies it's in the read
1029 * domain, and only that read domain. Enforce that in the request.
1030 */
1031 if (write_domain != 0 && read_domains != write_domain)
1032 return -EINVAL;
1033
Eric Anholt673a3942008-07-30 12:06:12 -07001034 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1035 if (obj == NULL)
1036 return -EBADF;
Daniel Vetter23010e42010-03-08 13:35:02 +01001037 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001038
1039 mutex_lock(&dev->struct_mutex);
Jesse Barnes652c3932009-08-17 13:31:43 -07001040
1041 intel_mark_busy(dev, obj);
1042
Eric Anholt673a3942008-07-30 12:06:12 -07001043#if WATCH_BUF
Krzysztof Halasacfd43c02009-06-20 00:31:28 +02001044 DRM_INFO("set_domain_ioctl %p(%zd), %08x %08x\n",
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001045 obj, obj->size, read_domains, write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07001046#endif
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001047 if (read_domains & I915_GEM_DOMAIN_GTT) {
1048 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
Eric Anholt02354392008-11-26 13:58:13 -08001049
Eric Anholta09ba7f2009-08-29 12:49:51 -07001050 /* Update the LRU on the fence for the CPU access that's
1051 * about to occur.
1052 */
1053 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
1054 list_move_tail(&obj_priv->fence_list,
1055 &dev_priv->mm.fence_list);
1056 }
1057
Eric Anholt02354392008-11-26 13:58:13 -08001058 /* Silently promote "you're not bound, there was nothing to do"
1059 * to success, since the client was just asking us to
1060 * make sure everything was done.
1061 */
1062 if (ret == -EINVAL)
1063 ret = 0;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001064 } else {
Eric Anholte47c68e2008-11-14 13:35:19 -08001065 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001066 }
1067
Eric Anholt673a3942008-07-30 12:06:12 -07001068 drm_gem_object_unreference(obj);
1069 mutex_unlock(&dev->struct_mutex);
1070 return ret;
1071}
1072
1073/**
1074 * Called when user space has done writes to this buffer
1075 */
1076int
1077i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
1078 struct drm_file *file_priv)
1079{
1080 struct drm_i915_gem_sw_finish *args = data;
1081 struct drm_gem_object *obj;
1082 struct drm_i915_gem_object *obj_priv;
1083 int ret = 0;
1084
1085 if (!(dev->driver->driver_features & DRIVER_GEM))
1086 return -ENODEV;
1087
1088 mutex_lock(&dev->struct_mutex);
1089 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1090 if (obj == NULL) {
1091 mutex_unlock(&dev->struct_mutex);
1092 return -EBADF;
1093 }
1094
1095#if WATCH_BUF
Krzysztof Halasacfd43c02009-06-20 00:31:28 +02001096 DRM_INFO("%s: sw_finish %d (%p %zd)\n",
Eric Anholt673a3942008-07-30 12:06:12 -07001097 __func__, args->handle, obj, obj->size);
1098#endif
Daniel Vetter23010e42010-03-08 13:35:02 +01001099 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001100
1101 /* Pinned buffers may be scanout, so flush the cache */
Eric Anholte47c68e2008-11-14 13:35:19 -08001102 if (obj_priv->pin_count)
1103 i915_gem_object_flush_cpu_write_domain(obj);
1104
Eric Anholt673a3942008-07-30 12:06:12 -07001105 drm_gem_object_unreference(obj);
1106 mutex_unlock(&dev->struct_mutex);
1107 return ret;
1108}
1109
1110/**
1111 * Maps the contents of an object, returning the address it is mapped
1112 * into.
1113 *
1114 * While the mapping holds a reference on the contents of the object, it doesn't
1115 * imply a ref on the object itself.
1116 */
1117int
1118i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
1119 struct drm_file *file_priv)
1120{
1121 struct drm_i915_gem_mmap *args = data;
1122 struct drm_gem_object *obj;
1123 loff_t offset;
1124 unsigned long addr;
1125
1126 if (!(dev->driver->driver_features & DRIVER_GEM))
1127 return -ENODEV;
1128
1129 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1130 if (obj == NULL)
1131 return -EBADF;
1132
1133 offset = args->offset;
1134
1135 down_write(&current->mm->mmap_sem);
1136 addr = do_mmap(obj->filp, 0, args->size,
1137 PROT_READ | PROT_WRITE, MAP_SHARED,
1138 args->offset);
1139 up_write(&current->mm->mmap_sem);
Luca Barbieribc9025b2010-02-09 05:49:12 +00001140 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001141 if (IS_ERR((void *)addr))
1142 return addr;
1143
1144 args->addr_ptr = (uint64_t) addr;
1145
1146 return 0;
1147}
1148
Jesse Barnesde151cf2008-11-12 10:03:55 -08001149/**
1150 * i915_gem_fault - fault a page into the GTT
1151 * vma: VMA in question
1152 * vmf: fault info
1153 *
1154 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1155 * from userspace. The fault handler takes care of binding the object to
1156 * the GTT (if needed), allocating and programming a fence register (again,
1157 * only if needed based on whether the old reg is still valid or the object
1158 * is tiled) and inserting a new PTE into the faulting process.
1159 *
1160 * Note that the faulting process may involve evicting existing objects
1161 * from the GTT and/or fence registers to make room. So performance may
1162 * suffer if the GTT working set is large or there are few fence registers
1163 * left.
1164 */
1165int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1166{
1167 struct drm_gem_object *obj = vma->vm_private_data;
1168 struct drm_device *dev = obj->dev;
1169 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001170 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001171 pgoff_t page_offset;
1172 unsigned long pfn;
1173 int ret = 0;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001174 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001175
1176 /* We don't use vmf->pgoff since that has the fake offset */
1177 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1178 PAGE_SHIFT;
1179
1180 /* Now bind it into the GTT if needed */
1181 mutex_lock(&dev->struct_mutex);
1182 if (!obj_priv->gtt_space) {
Chris Wilsone67b8ce2009-09-14 16:50:26 +01001183 ret = i915_gem_object_bind_to_gtt(obj, 0);
Chris Wilsonc7150892009-09-23 00:43:56 +01001184 if (ret)
1185 goto unlock;
Kristian Høgsberg07f4f3e2009-05-27 14:37:28 -04001186
Jesse Barnes14b60392009-05-20 16:47:08 -04001187 list_add_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001188
1189 ret = i915_gem_object_set_to_gtt_domain(obj, write);
Chris Wilsonc7150892009-09-23 00:43:56 +01001190 if (ret)
1191 goto unlock;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001192 }
1193
1194 /* Need a new fence register? */
Eric Anholta09ba7f2009-08-29 12:49:51 -07001195 if (obj_priv->tiling_mode != I915_TILING_NONE) {
Chris Wilson8c4b8c32009-06-17 22:08:52 +01001196 ret = i915_gem_object_get_fence_reg(obj);
Chris Wilsonc7150892009-09-23 00:43:56 +01001197 if (ret)
1198 goto unlock;
Eric Anholtd9ddcb92009-01-27 10:33:49 -08001199 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001200
1201 pfn = ((dev->agp->base + obj_priv->gtt_offset) >> PAGE_SHIFT) +
1202 page_offset;
1203
1204 /* Finally, remap it using the new GTT offset */
1205 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
Chris Wilsonc7150892009-09-23 00:43:56 +01001206unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001207 mutex_unlock(&dev->struct_mutex);
1208
1209 switch (ret) {
Chris Wilsonc7150892009-09-23 00:43:56 +01001210 case 0:
1211 case -ERESTARTSYS:
1212 return VM_FAULT_NOPAGE;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001213 case -ENOMEM:
1214 case -EAGAIN:
1215 return VM_FAULT_OOM;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001216 default:
Chris Wilsonc7150892009-09-23 00:43:56 +01001217 return VM_FAULT_SIGBUS;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001218 }
1219}
1220
1221/**
1222 * i915_gem_create_mmap_offset - create a fake mmap offset for an object
1223 * @obj: obj in question
1224 *
1225 * GEM memory mapping works by handing back to userspace a fake mmap offset
1226 * it can use in a subsequent mmap(2) call. The DRM core code then looks
1227 * up the object based on the offset and sets up the various memory mapping
1228 * structures.
1229 *
1230 * This routine allocates and attaches a fake offset for @obj.
1231 */
1232static int
1233i915_gem_create_mmap_offset(struct drm_gem_object *obj)
1234{
1235 struct drm_device *dev = obj->dev;
1236 struct drm_gem_mm *mm = dev->mm_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001237 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001238 struct drm_map_list *list;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +11001239 struct drm_local_map *map;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001240 int ret = 0;
1241
1242 /* Set the object up for mmap'ing */
1243 list = &obj->map_list;
Eric Anholt9a298b22009-03-24 12:23:04 -07001244 list->map = kzalloc(sizeof(struct drm_map_list), GFP_KERNEL);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001245 if (!list->map)
1246 return -ENOMEM;
1247
1248 map = list->map;
1249 map->type = _DRM_GEM;
1250 map->size = obj->size;
1251 map->handle = obj;
1252
1253 /* Get a DRM GEM mmap offset allocated... */
1254 list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
1255 obj->size / PAGE_SIZE, 0, 0);
1256 if (!list->file_offset_node) {
1257 DRM_ERROR("failed to allocate offset for bo %d\n", obj->name);
1258 ret = -ENOMEM;
1259 goto out_free_list;
1260 }
1261
1262 list->file_offset_node = drm_mm_get_block(list->file_offset_node,
1263 obj->size / PAGE_SIZE, 0);
1264 if (!list->file_offset_node) {
1265 ret = -ENOMEM;
1266 goto out_free_list;
1267 }
1268
1269 list->hash.key = list->file_offset_node->start;
1270 if (drm_ht_insert_item(&mm->offset_hash, &list->hash)) {
1271 DRM_ERROR("failed to add to map hash\n");
Chris Wilson5618ca62009-12-02 15:15:30 +00001272 ret = -ENOMEM;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001273 goto out_free_mm;
1274 }
1275
1276 /* By now we should be all set, any drm_mmap request on the offset
1277 * below will get to our mmap & fault handler */
1278 obj_priv->mmap_offset = ((uint64_t) list->hash.key) << PAGE_SHIFT;
1279
1280 return 0;
1281
1282out_free_mm:
1283 drm_mm_put_block(list->file_offset_node);
1284out_free_list:
Eric Anholt9a298b22009-03-24 12:23:04 -07001285 kfree(list->map);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001286
1287 return ret;
1288}
1289
Chris Wilson901782b2009-07-10 08:18:50 +01001290/**
1291 * i915_gem_release_mmap - remove physical page mappings
1292 * @obj: obj in question
1293 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001294 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01001295 * relinquish ownership of the pages back to the system.
1296 *
1297 * It is vital that we remove the page mapping if we have mapped a tiled
1298 * object through the GTT and then lose the fence register due to
1299 * resource pressure. Similarly if the object has been moved out of the
1300 * aperture, than pages mapped into userspace must be revoked. Removing the
1301 * mapping will then trigger a page fault on the next user access, allowing
1302 * fixup by i915_gem_fault().
1303 */
Eric Anholtd05ca302009-07-10 13:02:26 -07001304void
Chris Wilson901782b2009-07-10 08:18:50 +01001305i915_gem_release_mmap(struct drm_gem_object *obj)
1306{
1307 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001308 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson901782b2009-07-10 08:18:50 +01001309
1310 if (dev->dev_mapping)
1311 unmap_mapping_range(dev->dev_mapping,
1312 obj_priv->mmap_offset, obj->size, 1);
1313}
1314
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001315static void
1316i915_gem_free_mmap_offset(struct drm_gem_object *obj)
1317{
1318 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001319 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001320 struct drm_gem_mm *mm = dev->mm_private;
1321 struct drm_map_list *list;
1322
1323 list = &obj->map_list;
1324 drm_ht_remove_item(&mm->offset_hash, &list->hash);
1325
1326 if (list->file_offset_node) {
1327 drm_mm_put_block(list->file_offset_node);
1328 list->file_offset_node = NULL;
1329 }
1330
1331 if (list->map) {
Eric Anholt9a298b22009-03-24 12:23:04 -07001332 kfree(list->map);
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001333 list->map = NULL;
1334 }
1335
1336 obj_priv->mmap_offset = 0;
1337}
1338
Jesse Barnesde151cf2008-11-12 10:03:55 -08001339/**
1340 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1341 * @obj: object to check
1342 *
1343 * Return the required GTT alignment for an object, taking into account
1344 * potential fence register mapping if needed.
1345 */
1346static uint32_t
1347i915_gem_get_gtt_alignment(struct drm_gem_object *obj)
1348{
1349 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001350 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001351 int start, i;
1352
1353 /*
1354 * Minimum alignment is 4k (GTT page size), but might be greater
1355 * if a fence register is needed for the object.
1356 */
1357 if (IS_I965G(dev) || obj_priv->tiling_mode == I915_TILING_NONE)
1358 return 4096;
1359
1360 /*
1361 * Previous chips need to be aligned to the size of the smallest
1362 * fence register that can contain the object.
1363 */
1364 if (IS_I9XX(dev))
1365 start = 1024*1024;
1366 else
1367 start = 512*1024;
1368
1369 for (i = start; i < obj->size; i <<= 1)
1370 ;
1371
1372 return i;
1373}
1374
1375/**
1376 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1377 * @dev: DRM device
1378 * @data: GTT mapping ioctl data
1379 * @file_priv: GEM object info
1380 *
1381 * Simply returns the fake offset to userspace so it can mmap it.
1382 * The mmap call will end up in drm_gem_mmap(), which will set things
1383 * up so we can get faults in the handler above.
1384 *
1385 * The fault handler will take care of binding the object into the GTT
1386 * (since it may have been evicted to make room for something), allocating
1387 * a fence register, and mapping the appropriate aperture address into
1388 * userspace.
1389 */
1390int
1391i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
1392 struct drm_file *file_priv)
1393{
1394 struct drm_i915_gem_mmap_gtt *args = data;
1395 struct drm_i915_private *dev_priv = dev->dev_private;
1396 struct drm_gem_object *obj;
1397 struct drm_i915_gem_object *obj_priv;
1398 int ret;
1399
1400 if (!(dev->driver->driver_features & DRIVER_GEM))
1401 return -ENODEV;
1402
1403 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1404 if (obj == NULL)
1405 return -EBADF;
1406
1407 mutex_lock(&dev->struct_mutex);
1408
Daniel Vetter23010e42010-03-08 13:35:02 +01001409 obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001410
Chris Wilsonab182822009-09-22 18:46:17 +01001411 if (obj_priv->madv != I915_MADV_WILLNEED) {
1412 DRM_ERROR("Attempting to mmap a purgeable buffer\n");
1413 drm_gem_object_unreference(obj);
1414 mutex_unlock(&dev->struct_mutex);
1415 return -EINVAL;
1416 }
1417
1418
Jesse Barnesde151cf2008-11-12 10:03:55 -08001419 if (!obj_priv->mmap_offset) {
1420 ret = i915_gem_create_mmap_offset(obj);
Chris Wilson13af1062009-02-11 14:26:31 +00001421 if (ret) {
1422 drm_gem_object_unreference(obj);
1423 mutex_unlock(&dev->struct_mutex);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001424 return ret;
Chris Wilson13af1062009-02-11 14:26:31 +00001425 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001426 }
1427
1428 args->offset = obj_priv->mmap_offset;
1429
Jesse Barnesde151cf2008-11-12 10:03:55 -08001430 /*
1431 * Pull it into the GTT so that we have a page list (makes the
1432 * initial fault faster and any subsequent flushing possible).
1433 */
1434 if (!obj_priv->agp_mem) {
Chris Wilsone67b8ce2009-09-14 16:50:26 +01001435 ret = i915_gem_object_bind_to_gtt(obj, 0);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001436 if (ret) {
1437 drm_gem_object_unreference(obj);
1438 mutex_unlock(&dev->struct_mutex);
1439 return ret;
1440 }
Jesse Barnes14b60392009-05-20 16:47:08 -04001441 list_add_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001442 }
1443
1444 drm_gem_object_unreference(obj);
1445 mutex_unlock(&dev->struct_mutex);
1446
1447 return 0;
1448}
1449
Ben Gamari6911a9b2009-04-02 11:24:54 -07001450void
Eric Anholt856fa192009-03-19 14:10:50 -07001451i915_gem_object_put_pages(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07001452{
Daniel Vetter23010e42010-03-08 13:35:02 +01001453 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001454 int page_count = obj->size / PAGE_SIZE;
1455 int i;
1456
Eric Anholt856fa192009-03-19 14:10:50 -07001457 BUG_ON(obj_priv->pages_refcount == 0);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001458 BUG_ON(obj_priv->madv == __I915_MADV_PURGED);
Eric Anholt856fa192009-03-19 14:10:50 -07001459
1460 if (--obj_priv->pages_refcount != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07001461 return;
1462
Eric Anholt280b7132009-03-12 16:56:27 -07001463 if (obj_priv->tiling_mode != I915_TILING_NONE)
1464 i915_gem_object_save_bit_17_swizzle(obj);
1465
Chris Wilson3ef94da2009-09-14 16:50:29 +01001466 if (obj_priv->madv == I915_MADV_DONTNEED)
Chris Wilson13a05fd2009-09-20 23:03:19 +01001467 obj_priv->dirty = 0;
Chris Wilson3ef94da2009-09-14 16:50:29 +01001468
1469 for (i = 0; i < page_count; i++) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01001470 if (obj_priv->dirty)
1471 set_page_dirty(obj_priv->pages[i]);
1472
1473 if (obj_priv->madv == I915_MADV_WILLNEED)
Eric Anholt856fa192009-03-19 14:10:50 -07001474 mark_page_accessed(obj_priv->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001475
1476 page_cache_release(obj_priv->pages[i]);
1477 }
Eric Anholt673a3942008-07-30 12:06:12 -07001478 obj_priv->dirty = 0;
1479
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07001480 drm_free_large(obj_priv->pages);
Eric Anholt856fa192009-03-19 14:10:50 -07001481 obj_priv->pages = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001482}
1483
1484static void
Eric Anholtce44b0e2008-11-06 16:00:31 -08001485i915_gem_object_move_to_active(struct drm_gem_object *obj, uint32_t seqno)
Eric Anholt673a3942008-07-30 12:06:12 -07001486{
1487 struct drm_device *dev = obj->dev;
1488 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001489 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001490
1491 /* Add a reference if we're newly entering the active list. */
1492 if (!obj_priv->active) {
1493 drm_gem_object_reference(obj);
1494 obj_priv->active = 1;
1495 }
1496 /* Move from whatever list we were on to the tail of execution. */
Carl Worth5e118f42009-03-20 11:54:25 -07001497 spin_lock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001498 list_move_tail(&obj_priv->list,
1499 &dev_priv->mm.active_list);
Carl Worth5e118f42009-03-20 11:54:25 -07001500 spin_unlock(&dev_priv->mm.active_list_lock);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001501 obj_priv->last_rendering_seqno = seqno;
Eric Anholt673a3942008-07-30 12:06:12 -07001502}
1503
Eric Anholtce44b0e2008-11-06 16:00:31 -08001504static void
1505i915_gem_object_move_to_flushing(struct drm_gem_object *obj)
1506{
1507 struct drm_device *dev = obj->dev;
1508 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001509 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001510
1511 BUG_ON(!obj_priv->active);
1512 list_move_tail(&obj_priv->list, &dev_priv->mm.flushing_list);
1513 obj_priv->last_rendering_seqno = 0;
1514}
Eric Anholt673a3942008-07-30 12:06:12 -07001515
Chris Wilson963b4832009-09-20 23:03:54 +01001516/* Immediately discard the backing storage */
1517static void
1518i915_gem_object_truncate(struct drm_gem_object *obj)
1519{
Daniel Vetter23010e42010-03-08 13:35:02 +01001520 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001521 struct inode *inode;
Chris Wilson963b4832009-09-20 23:03:54 +01001522
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001523 inode = obj->filp->f_path.dentry->d_inode;
1524 if (inode->i_op->truncate)
1525 inode->i_op->truncate (inode);
1526
1527 obj_priv->madv = __I915_MADV_PURGED;
Chris Wilson963b4832009-09-20 23:03:54 +01001528}
1529
1530static inline int
1531i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj_priv)
1532{
1533 return obj_priv->madv == I915_MADV_DONTNEED;
1534}
1535
Eric Anholt673a3942008-07-30 12:06:12 -07001536static void
1537i915_gem_object_move_to_inactive(struct drm_gem_object *obj)
1538{
1539 struct drm_device *dev = obj->dev;
1540 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001541 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001542
1543 i915_verify_inactive(dev, __FILE__, __LINE__);
1544 if (obj_priv->pin_count != 0)
1545 list_del_init(&obj_priv->list);
1546 else
1547 list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
1548
Daniel Vetter99fcb762010-02-07 16:20:18 +01001549 BUG_ON(!list_empty(&obj_priv->gpu_write_list));
1550
Eric Anholtce44b0e2008-11-06 16:00:31 -08001551 obj_priv->last_rendering_seqno = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001552 if (obj_priv->active) {
1553 obj_priv->active = 0;
1554 drm_gem_object_unreference(obj);
1555 }
1556 i915_verify_inactive(dev, __FILE__, __LINE__);
1557}
1558
Daniel Vetter63560392010-02-19 11:51:59 +01001559static void
1560i915_gem_process_flushing_list(struct drm_device *dev,
1561 uint32_t flush_domains, uint32_t seqno)
1562{
1563 drm_i915_private_t *dev_priv = dev->dev_private;
1564 struct drm_i915_gem_object *obj_priv, *next;
1565
1566 list_for_each_entry_safe(obj_priv, next,
1567 &dev_priv->mm.gpu_write_list,
1568 gpu_write_list) {
Daniel Vettera8089e82010-04-09 19:05:09 +00001569 struct drm_gem_object *obj = &obj_priv->base;
Daniel Vetter63560392010-02-19 11:51:59 +01001570
1571 if ((obj->write_domain & flush_domains) ==
1572 obj->write_domain) {
1573 uint32_t old_write_domain = obj->write_domain;
1574
1575 obj->write_domain = 0;
1576 list_del_init(&obj_priv->gpu_write_list);
1577 i915_gem_object_move_to_active(obj, seqno);
1578
1579 /* update the fence lru list */
1580 if (obj_priv->fence_reg != I915_FENCE_REG_NONE)
1581 list_move_tail(&obj_priv->fence_list,
1582 &dev_priv->mm.fence_list);
1583
1584 trace_i915_gem_object_change_domain(obj,
1585 obj->read_domains,
1586 old_write_domain);
1587 }
1588 }
1589}
1590
Eric Anholt673a3942008-07-30 12:06:12 -07001591/**
1592 * Creates a new sequence number, emitting a write of it to the status page
1593 * plus an interrupt, which will trigger i915_user_interrupt_handler.
1594 *
1595 * Must be called with struct_lock held.
1596 *
1597 * Returned sequence numbers are nonzero on success.
1598 */
Daniel Vetter5a5a0c62009-09-15 22:57:36 +02001599uint32_t
Eric Anholtb9624422009-06-03 07:27:35 +00001600i915_add_request(struct drm_device *dev, struct drm_file *file_priv,
1601 uint32_t flush_domains)
Eric Anholt673a3942008-07-30 12:06:12 -07001602{
1603 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtb9624422009-06-03 07:27:35 +00001604 struct drm_i915_file_private *i915_file_priv = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001605 struct drm_i915_gem_request *request;
1606 uint32_t seqno;
1607 int was_empty;
1608 RING_LOCALS;
1609
Eric Anholtb9624422009-06-03 07:27:35 +00001610 if (file_priv != NULL)
1611 i915_file_priv = file_priv->driver_priv;
1612
Eric Anholt9a298b22009-03-24 12:23:04 -07001613 request = kzalloc(sizeof(*request), GFP_KERNEL);
Eric Anholt673a3942008-07-30 12:06:12 -07001614 if (request == NULL)
1615 return 0;
1616
1617 /* Grab the seqno we're going to make this request be, and bump the
1618 * next (skipping 0 so it can be the reserved no-seqno value).
1619 */
1620 seqno = dev_priv->mm.next_gem_seqno;
1621 dev_priv->mm.next_gem_seqno++;
1622 if (dev_priv->mm.next_gem_seqno == 0)
1623 dev_priv->mm.next_gem_seqno++;
1624
1625 BEGIN_LP_RING(4);
1626 OUT_RING(MI_STORE_DWORD_INDEX);
1627 OUT_RING(I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
1628 OUT_RING(seqno);
1629
1630 OUT_RING(MI_USER_INTERRUPT);
1631 ADVANCE_LP_RING();
1632
Zhao Yakui44d98a62009-10-09 11:39:40 +08001633 DRM_DEBUG_DRIVER("%d\n", seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07001634
1635 request->seqno = seqno;
1636 request->emitted_jiffies = jiffies;
Eric Anholt673a3942008-07-30 12:06:12 -07001637 was_empty = list_empty(&dev_priv->mm.request_list);
1638 list_add_tail(&request->list, &dev_priv->mm.request_list);
Eric Anholtb9624422009-06-03 07:27:35 +00001639 if (i915_file_priv) {
1640 list_add_tail(&request->client_list,
1641 &i915_file_priv->mm.request_list);
1642 } else {
1643 INIT_LIST_HEAD(&request->client_list);
1644 }
Eric Anholt673a3942008-07-30 12:06:12 -07001645
Eric Anholtce44b0e2008-11-06 16:00:31 -08001646 /* Associate any objects on the flushing list matching the write
1647 * domain we're flushing with our flush.
1648 */
Daniel Vetter63560392010-02-19 11:51:59 +01001649 if (flush_domains != 0)
1650 i915_gem_process_flushing_list(dev, flush_domains, seqno);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001651
Ben Gamarif65d9422009-09-14 17:48:44 -04001652 if (!dev_priv->mm.suspended) {
1653 mod_timer(&dev_priv->hangcheck_timer, jiffies + DRM_I915_HANGCHECK_PERIOD);
1654 if (was_empty)
1655 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
1656 }
Eric Anholt673a3942008-07-30 12:06:12 -07001657 return seqno;
1658}
1659
1660/**
1661 * Command execution barrier
1662 *
1663 * Ensures that all commands in the ring are finished
1664 * before signalling the CPU
1665 */
Eric Anholt3043c602008-10-02 12:24:47 -07001666static uint32_t
Eric Anholt673a3942008-07-30 12:06:12 -07001667i915_retire_commands(struct drm_device *dev)
1668{
1669 drm_i915_private_t *dev_priv = dev->dev_private;
1670 uint32_t cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
1671 uint32_t flush_domains = 0;
1672 RING_LOCALS;
1673
1674 /* The sampler always gets flushed on i965 (sigh) */
1675 if (IS_I965G(dev))
1676 flush_domains |= I915_GEM_DOMAIN_SAMPLER;
1677 BEGIN_LP_RING(2);
1678 OUT_RING(cmd);
1679 OUT_RING(0); /* noop */
1680 ADVANCE_LP_RING();
1681 return flush_domains;
1682}
1683
1684/**
1685 * Moves buffers associated only with the given active seqno from the active
1686 * to inactive list, potentially freeing them.
1687 */
1688static void
1689i915_gem_retire_request(struct drm_device *dev,
1690 struct drm_i915_gem_request *request)
1691{
1692 drm_i915_private_t *dev_priv = dev->dev_private;
1693
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001694 trace_i915_gem_request_retire(dev, request->seqno);
1695
Eric Anholt673a3942008-07-30 12:06:12 -07001696 /* Move any buffers on the active list that are no longer referenced
1697 * by the ringbuffer to the flushing/inactive lists as appropriate.
1698 */
Carl Worth5e118f42009-03-20 11:54:25 -07001699 spin_lock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001700 while (!list_empty(&dev_priv->mm.active_list)) {
1701 struct drm_gem_object *obj;
1702 struct drm_i915_gem_object *obj_priv;
1703
1704 obj_priv = list_first_entry(&dev_priv->mm.active_list,
1705 struct drm_i915_gem_object,
1706 list);
Daniel Vettera8089e82010-04-09 19:05:09 +00001707 obj = &obj_priv->base;
Eric Anholt673a3942008-07-30 12:06:12 -07001708
1709 /* If the seqno being retired doesn't match the oldest in the
1710 * list, then the oldest in the list must still be newer than
1711 * this seqno.
1712 */
1713 if (obj_priv->last_rendering_seqno != request->seqno)
Carl Worth5e118f42009-03-20 11:54:25 -07001714 goto out;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001715
Eric Anholt673a3942008-07-30 12:06:12 -07001716#if WATCH_LRU
1717 DRM_INFO("%s: retire %d moves to inactive list %p\n",
1718 __func__, request->seqno, obj);
1719#endif
1720
Eric Anholtce44b0e2008-11-06 16:00:31 -08001721 if (obj->write_domain != 0)
1722 i915_gem_object_move_to_flushing(obj);
Shaohua Li68c84342009-04-08 10:58:23 +08001723 else {
1724 /* Take a reference on the object so it won't be
1725 * freed while the spinlock is held. The list
1726 * protection for this spinlock is safe when breaking
1727 * the lock like this since the next thing we do
1728 * is just get the head of the list again.
1729 */
1730 drm_gem_object_reference(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001731 i915_gem_object_move_to_inactive(obj);
Shaohua Li68c84342009-04-08 10:58:23 +08001732 spin_unlock(&dev_priv->mm.active_list_lock);
1733 drm_gem_object_unreference(obj);
1734 spin_lock(&dev_priv->mm.active_list_lock);
1735 }
Eric Anholt673a3942008-07-30 12:06:12 -07001736 }
Carl Worth5e118f42009-03-20 11:54:25 -07001737out:
1738 spin_unlock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001739}
1740
1741/**
1742 * Returns true if seq1 is later than seq2.
1743 */
Ben Gamari22be1722009-09-14 17:48:43 -04001744bool
Eric Anholt673a3942008-07-30 12:06:12 -07001745i915_seqno_passed(uint32_t seq1, uint32_t seq2)
1746{
1747 return (int32_t)(seq1 - seq2) >= 0;
1748}
1749
1750uint32_t
1751i915_get_gem_seqno(struct drm_device *dev)
1752{
1753 drm_i915_private_t *dev_priv = dev->dev_private;
1754
1755 return READ_HWSP(dev_priv, I915_GEM_HWS_INDEX);
1756}
1757
1758/**
1759 * This function clears the request list as sequence numbers are passed.
1760 */
1761void
1762i915_gem_retire_requests(struct drm_device *dev)
1763{
1764 drm_i915_private_t *dev_priv = dev->dev_private;
1765 uint32_t seqno;
1766
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001767 if (!dev_priv->hw_status_page || list_empty(&dev_priv->mm.request_list))
Karsten Wiese6c0594a2009-02-23 15:07:57 +01001768 return;
1769
Eric Anholt673a3942008-07-30 12:06:12 -07001770 seqno = i915_get_gem_seqno(dev);
1771
1772 while (!list_empty(&dev_priv->mm.request_list)) {
1773 struct drm_i915_gem_request *request;
1774 uint32_t retiring_seqno;
1775
1776 request = list_first_entry(&dev_priv->mm.request_list,
1777 struct drm_i915_gem_request,
1778 list);
1779 retiring_seqno = request->seqno;
1780
1781 if (i915_seqno_passed(seqno, retiring_seqno) ||
Ben Gamariba1234d2009-09-14 17:48:47 -04001782 atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001783 i915_gem_retire_request(dev, request);
1784
1785 list_del(&request->list);
Eric Anholtb9624422009-06-03 07:27:35 +00001786 list_del(&request->client_list);
Eric Anholt9a298b22009-03-24 12:23:04 -07001787 kfree(request);
Eric Anholt673a3942008-07-30 12:06:12 -07001788 } else
1789 break;
1790 }
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001791
1792 if (unlikely (dev_priv->trace_irq_seqno &&
1793 i915_seqno_passed(dev_priv->trace_irq_seqno, seqno))) {
1794 i915_user_irq_put(dev);
1795 dev_priv->trace_irq_seqno = 0;
1796 }
Eric Anholt673a3942008-07-30 12:06:12 -07001797}
1798
1799void
1800i915_gem_retire_work_handler(struct work_struct *work)
1801{
1802 drm_i915_private_t *dev_priv;
1803 struct drm_device *dev;
1804
1805 dev_priv = container_of(work, drm_i915_private_t,
1806 mm.retire_work.work);
1807 dev = dev_priv->dev;
1808
1809 mutex_lock(&dev->struct_mutex);
1810 i915_gem_retire_requests(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07001811 if (!dev_priv->mm.suspended &&
1812 !list_empty(&dev_priv->mm.request_list))
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001813 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
Eric Anholt673a3942008-07-30 12:06:12 -07001814 mutex_unlock(&dev->struct_mutex);
1815}
1816
Daniel Vetter5a5a0c62009-09-15 22:57:36 +02001817int
Daniel Vetter48764bf2009-09-15 22:57:32 +02001818i915_do_wait_request(struct drm_device *dev, uint32_t seqno, int interruptible)
Eric Anholt673a3942008-07-30 12:06:12 -07001819{
1820 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001821 u32 ier;
Eric Anholt673a3942008-07-30 12:06:12 -07001822 int ret = 0;
1823
1824 BUG_ON(seqno == 0);
1825
Ben Gamariba1234d2009-09-14 17:48:47 -04001826 if (atomic_read(&dev_priv->mm.wedged))
Ben Gamariffed1d02009-09-14 17:48:41 -04001827 return -EIO;
1828
Eric Anholt673a3942008-07-30 12:06:12 -07001829 if (!i915_seqno_passed(i915_get_gem_seqno(dev), seqno)) {
Eric Anholtbad720f2009-10-22 16:11:14 -07001830 if (HAS_PCH_SPLIT(dev))
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001831 ier = I915_READ(DEIER) | I915_READ(GTIER);
1832 else
1833 ier = I915_READ(IER);
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001834 if (!ier) {
1835 DRM_ERROR("something (likely vbetool) disabled "
1836 "interrupts, re-enabling\n");
1837 i915_driver_irq_preinstall(dev);
1838 i915_driver_irq_postinstall(dev);
1839 }
1840
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001841 trace_i915_gem_request_wait_begin(dev, seqno);
1842
Eric Anholt673a3942008-07-30 12:06:12 -07001843 dev_priv->mm.waiting_gem_seqno = seqno;
1844 i915_user_irq_get(dev);
Daniel Vetter48764bf2009-09-15 22:57:32 +02001845 if (interruptible)
1846 ret = wait_event_interruptible(dev_priv->irq_queue,
1847 i915_seqno_passed(i915_get_gem_seqno(dev), seqno) ||
1848 atomic_read(&dev_priv->mm.wedged));
1849 else
1850 wait_event(dev_priv->irq_queue,
1851 i915_seqno_passed(i915_get_gem_seqno(dev), seqno) ||
1852 atomic_read(&dev_priv->mm.wedged));
1853
Eric Anholt673a3942008-07-30 12:06:12 -07001854 i915_user_irq_put(dev);
1855 dev_priv->mm.waiting_gem_seqno = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001856
1857 trace_i915_gem_request_wait_end(dev, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07001858 }
Ben Gamariba1234d2009-09-14 17:48:47 -04001859 if (atomic_read(&dev_priv->mm.wedged))
Eric Anholt673a3942008-07-30 12:06:12 -07001860 ret = -EIO;
1861
1862 if (ret && ret != -ERESTARTSYS)
1863 DRM_ERROR("%s returns %d (awaiting %d at %d)\n",
1864 __func__, ret, seqno, i915_get_gem_seqno(dev));
1865
1866 /* Directly dispatch request retiring. While we have the work queue
1867 * to handle this, the waiter on a request often wants an associated
1868 * buffer to have made it to the inactive list, and we would need
1869 * a separate wait queue to handle that.
1870 */
1871 if (ret == 0)
1872 i915_gem_retire_requests(dev);
1873
1874 return ret;
1875}
1876
Daniel Vetter48764bf2009-09-15 22:57:32 +02001877/**
1878 * Waits for a sequence number to be signaled, and cleans up the
1879 * request and object lists appropriately for that event.
1880 */
1881static int
1882i915_wait_request(struct drm_device *dev, uint32_t seqno)
1883{
1884 return i915_do_wait_request(dev, seqno, 1);
1885}
1886
Eric Anholt673a3942008-07-30 12:06:12 -07001887static void
1888i915_gem_flush(struct drm_device *dev,
1889 uint32_t invalidate_domains,
1890 uint32_t flush_domains)
1891{
1892 drm_i915_private_t *dev_priv = dev->dev_private;
1893 uint32_t cmd;
1894 RING_LOCALS;
1895
1896#if WATCH_EXEC
1897 DRM_INFO("%s: invalidate %08x flush %08x\n", __func__,
1898 invalidate_domains, flush_domains);
1899#endif
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001900 trace_i915_gem_request_flush(dev, dev_priv->mm.next_gem_seqno,
1901 invalidate_domains, flush_domains);
Eric Anholt673a3942008-07-30 12:06:12 -07001902
1903 if (flush_domains & I915_GEM_DOMAIN_CPU)
1904 drm_agp_chipset_flush(dev);
1905
Chris Wilson21d509e2009-06-06 09:46:02 +01001906 if ((invalidate_domains | flush_domains) & I915_GEM_GPU_DOMAINS) {
Eric Anholt673a3942008-07-30 12:06:12 -07001907 /*
1908 * read/write caches:
1909 *
1910 * I915_GEM_DOMAIN_RENDER is always invalidated, but is
1911 * only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is
1912 * also flushed at 2d versus 3d pipeline switches.
1913 *
1914 * read-only caches:
1915 *
1916 * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if
1917 * MI_READ_FLUSH is set, and is always flushed on 965.
1918 *
1919 * I915_GEM_DOMAIN_COMMAND may not exist?
1920 *
1921 * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is
1922 * invalidated when MI_EXE_FLUSH is set.
1923 *
1924 * I915_GEM_DOMAIN_VERTEX, which exists on 965, is
1925 * invalidated with every MI_FLUSH.
1926 *
1927 * TLBs:
1928 *
1929 * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND
1930 * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and
1931 * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER
1932 * are flushed at any MI_FLUSH.
1933 */
1934
1935 cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
1936 if ((invalidate_domains|flush_domains) &
1937 I915_GEM_DOMAIN_RENDER)
1938 cmd &= ~MI_NO_WRITE_FLUSH;
1939 if (!IS_I965G(dev)) {
1940 /*
1941 * On the 965, the sampler cache always gets flushed
1942 * and this bit is reserved.
1943 */
1944 if (invalidate_domains & I915_GEM_DOMAIN_SAMPLER)
1945 cmd |= MI_READ_FLUSH;
1946 }
1947 if (invalidate_domains & I915_GEM_DOMAIN_INSTRUCTION)
1948 cmd |= MI_EXE_FLUSH;
1949
1950#if WATCH_EXEC
1951 DRM_INFO("%s: queue flush %08x to ring\n", __func__, cmd);
1952#endif
1953 BEGIN_LP_RING(2);
1954 OUT_RING(cmd);
Daniel Vetter48764bf2009-09-15 22:57:32 +02001955 OUT_RING(MI_NOOP);
Eric Anholt673a3942008-07-30 12:06:12 -07001956 ADVANCE_LP_RING();
1957 }
1958}
1959
1960/**
1961 * Ensures that all rendering to the object has completed and the object is
1962 * safe to unbind from the GTT or access from the CPU.
1963 */
1964static int
1965i915_gem_object_wait_rendering(struct drm_gem_object *obj)
1966{
1967 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001968 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001969 int ret;
1970
Eric Anholte47c68e2008-11-14 13:35:19 -08001971 /* This function only exists to support waiting for existing rendering,
1972 * not for emitting required flushes.
Eric Anholt673a3942008-07-30 12:06:12 -07001973 */
Eric Anholte47c68e2008-11-14 13:35:19 -08001974 BUG_ON((obj->write_domain & I915_GEM_GPU_DOMAINS) != 0);
Eric Anholt673a3942008-07-30 12:06:12 -07001975
1976 /* If there is rendering queued on the buffer being evicted, wait for
1977 * it.
1978 */
1979 if (obj_priv->active) {
1980#if WATCH_BUF
1981 DRM_INFO("%s: object %p wait for seqno %08x\n",
1982 __func__, obj, obj_priv->last_rendering_seqno);
1983#endif
1984 ret = i915_wait_request(dev, obj_priv->last_rendering_seqno);
1985 if (ret != 0)
1986 return ret;
1987 }
1988
1989 return 0;
1990}
1991
1992/**
1993 * Unbinds an object from the GTT aperture.
1994 */
Jesse Barnes0f973f22009-01-26 17:10:45 -08001995int
Eric Anholt673a3942008-07-30 12:06:12 -07001996i915_gem_object_unbind(struct drm_gem_object *obj)
1997{
1998 struct drm_device *dev = obj->dev;
Daniel Vetter4a87b8c2010-02-19 11:51:57 +01001999 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002000 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002001 int ret = 0;
2002
2003#if WATCH_BUF
2004 DRM_INFO("%s:%d %p\n", __func__, __LINE__, obj);
2005 DRM_INFO("gtt_space %p\n", obj_priv->gtt_space);
2006#endif
2007 if (obj_priv->gtt_space == NULL)
2008 return 0;
2009
2010 if (obj_priv->pin_count != 0) {
2011 DRM_ERROR("Attempting to unbind pinned buffer\n");
2012 return -EINVAL;
2013 }
2014
Eric Anholt5323fd02009-09-09 11:50:45 -07002015 /* blow away mappings if mapped through GTT */
2016 i915_gem_release_mmap(obj);
2017
Eric Anholt673a3942008-07-30 12:06:12 -07002018 /* Move the object to the CPU domain to ensure that
2019 * any possible CPU writes while it's not in the GTT
2020 * are flushed when we go to remap it. This will
2021 * also ensure that all pending GPU writes are finished
2022 * before we unbind.
2023 */
Eric Anholte47c68e2008-11-14 13:35:19 -08002024 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07002025 if (ret) {
Eric Anholte47c68e2008-11-14 13:35:19 -08002026 if (ret != -ERESTARTSYS)
2027 DRM_ERROR("set_domain failed: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07002028 return ret;
2029 }
2030
Eric Anholt5323fd02009-09-09 11:50:45 -07002031 BUG_ON(obj_priv->active);
2032
Daniel Vetter96b47b62009-12-15 17:50:00 +01002033 /* release the fence reg _after_ flushing */
2034 if (obj_priv->fence_reg != I915_FENCE_REG_NONE)
2035 i915_gem_clear_fence_reg(obj);
2036
Eric Anholt673a3942008-07-30 12:06:12 -07002037 if (obj_priv->agp_mem != NULL) {
2038 drm_unbind_agp(obj_priv->agp_mem);
2039 drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE);
2040 obj_priv->agp_mem = NULL;
2041 }
2042
Eric Anholt856fa192009-03-19 14:10:50 -07002043 i915_gem_object_put_pages(obj);
Chris Wilsona32808c2009-09-20 21:29:47 +01002044 BUG_ON(obj_priv->pages_refcount);
Eric Anholt673a3942008-07-30 12:06:12 -07002045
2046 if (obj_priv->gtt_space) {
2047 atomic_dec(&dev->gtt_count);
2048 atomic_sub(obj->size, &dev->gtt_memory);
2049
2050 drm_mm_put_block(obj_priv->gtt_space);
2051 obj_priv->gtt_space = NULL;
2052 }
2053
2054 /* Remove ourselves from the LRU list if present. */
Daniel Vetter4a87b8c2010-02-19 11:51:57 +01002055 spin_lock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07002056 if (!list_empty(&obj_priv->list))
2057 list_del_init(&obj_priv->list);
Daniel Vetter4a87b8c2010-02-19 11:51:57 +01002058 spin_unlock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07002059
Chris Wilson963b4832009-09-20 23:03:54 +01002060 if (i915_gem_object_is_purgeable(obj_priv))
2061 i915_gem_object_truncate(obj);
2062
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002063 trace_i915_gem_object_unbind(obj);
2064
Eric Anholt673a3942008-07-30 12:06:12 -07002065 return 0;
2066}
2067
Chris Wilson07f73f62009-09-14 16:50:30 +01002068static struct drm_gem_object *
2069i915_gem_find_inactive_object(struct drm_device *dev, int min_size)
2070{
2071 drm_i915_private_t *dev_priv = dev->dev_private;
2072 struct drm_i915_gem_object *obj_priv;
2073 struct drm_gem_object *best = NULL;
2074 struct drm_gem_object *first = NULL;
2075
2076 /* Try to find the smallest clean object */
2077 list_for_each_entry(obj_priv, &dev_priv->mm.inactive_list, list) {
Daniel Vettera8089e82010-04-09 19:05:09 +00002078 struct drm_gem_object *obj = &obj_priv->base;
Chris Wilson07f73f62009-09-14 16:50:30 +01002079 if (obj->size >= min_size) {
Chris Wilson963b4832009-09-20 23:03:54 +01002080 if ((!obj_priv->dirty ||
2081 i915_gem_object_is_purgeable(obj_priv)) &&
Chris Wilson07f73f62009-09-14 16:50:30 +01002082 (!best || obj->size < best->size)) {
2083 best = obj;
2084 if (best->size == min_size)
2085 return best;
2086 }
2087 if (!first)
2088 first = obj;
2089 }
2090 }
2091
2092 return best ? best : first;
2093}
2094
Eric Anholt673a3942008-07-30 12:06:12 -07002095static int
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002096i915_gpu_idle(struct drm_device *dev)
2097{
2098 drm_i915_private_t *dev_priv = dev->dev_private;
2099 bool lists_empty;
2100 uint32_t seqno;
2101
2102 spin_lock(&dev_priv->mm.active_list_lock);
2103 lists_empty = list_empty(&dev_priv->mm.flushing_list) &&
2104 list_empty(&dev_priv->mm.active_list);
2105 spin_unlock(&dev_priv->mm.active_list_lock);
2106
2107 if (lists_empty)
2108 return 0;
2109
2110 /* Flush everything onto the inactive list. */
2111 i915_gem_flush(dev, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
2112 seqno = i915_add_request(dev, NULL, I915_GEM_GPU_DOMAINS);
2113 if (seqno == 0)
2114 return -ENOMEM;
2115
2116 return i915_wait_request(dev, seqno);
2117}
2118
2119static int
Chris Wilson07f73f62009-09-14 16:50:30 +01002120i915_gem_evict_everything(struct drm_device *dev)
2121{
2122 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson07f73f62009-09-14 16:50:30 +01002123 int ret;
2124 bool lists_empty;
2125
Chris Wilson07f73f62009-09-14 16:50:30 +01002126 spin_lock(&dev_priv->mm.active_list_lock);
2127 lists_empty = (list_empty(&dev_priv->mm.inactive_list) &&
2128 list_empty(&dev_priv->mm.flushing_list) &&
2129 list_empty(&dev_priv->mm.active_list));
2130 spin_unlock(&dev_priv->mm.active_list_lock);
2131
Chris Wilson97311292009-09-21 00:22:34 +01002132 if (lists_empty)
Chris Wilson07f73f62009-09-14 16:50:30 +01002133 return -ENOSPC;
Chris Wilson07f73f62009-09-14 16:50:30 +01002134
2135 /* Flush everything (on to the inactive lists) and evict */
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002136 ret = i915_gpu_idle(dev);
Chris Wilson07f73f62009-09-14 16:50:30 +01002137 if (ret)
2138 return ret;
2139
Daniel Vetter99fcb762010-02-07 16:20:18 +01002140 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
2141
Chris Wilsonab5ee572009-09-20 19:25:47 +01002142 ret = i915_gem_evict_from_inactive_list(dev);
Chris Wilson07f73f62009-09-14 16:50:30 +01002143 if (ret)
2144 return ret;
2145
2146 spin_lock(&dev_priv->mm.active_list_lock);
2147 lists_empty = (list_empty(&dev_priv->mm.inactive_list) &&
2148 list_empty(&dev_priv->mm.flushing_list) &&
2149 list_empty(&dev_priv->mm.active_list));
2150 spin_unlock(&dev_priv->mm.active_list_lock);
2151 BUG_ON(!lists_empty);
2152
Eric Anholt673a3942008-07-30 12:06:12 -07002153 return 0;
2154}
2155
2156static int
Chris Wilson07f73f62009-09-14 16:50:30 +01002157i915_gem_evict_something(struct drm_device *dev, int min_size)
Eric Anholt673a3942008-07-30 12:06:12 -07002158{
2159 drm_i915_private_t *dev_priv = dev->dev_private;
2160 struct drm_gem_object *obj;
Chris Wilson07f73f62009-09-14 16:50:30 +01002161 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002162
2163 for (;;) {
Chris Wilson07f73f62009-09-14 16:50:30 +01002164 i915_gem_retire_requests(dev);
2165
Eric Anholt673a3942008-07-30 12:06:12 -07002166 /* If there's an inactive buffer available now, grab it
2167 * and be done.
2168 */
Chris Wilson07f73f62009-09-14 16:50:30 +01002169 obj = i915_gem_find_inactive_object(dev, min_size);
2170 if (obj) {
2171 struct drm_i915_gem_object *obj_priv;
2172
Eric Anholt673a3942008-07-30 12:06:12 -07002173#if WATCH_LRU
2174 DRM_INFO("%s: evicting %p\n", __func__, obj);
2175#endif
Daniel Vetter23010e42010-03-08 13:35:02 +01002176 obj_priv = to_intel_bo(obj);
Chris Wilson07f73f62009-09-14 16:50:30 +01002177 BUG_ON(obj_priv->pin_count != 0);
Eric Anholt673a3942008-07-30 12:06:12 -07002178 BUG_ON(obj_priv->active);
2179
2180 /* Wait on the rendering and unbind the buffer. */
Chris Wilson07f73f62009-09-14 16:50:30 +01002181 return i915_gem_object_unbind(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002182 }
2183
2184 /* If we didn't get anything, but the ring is still processing
Chris Wilson07f73f62009-09-14 16:50:30 +01002185 * things, wait for the next to finish and hopefully leave us
2186 * a buffer to evict.
Eric Anholt673a3942008-07-30 12:06:12 -07002187 */
2188 if (!list_empty(&dev_priv->mm.request_list)) {
2189 struct drm_i915_gem_request *request;
2190
2191 request = list_first_entry(&dev_priv->mm.request_list,
2192 struct drm_i915_gem_request,
2193 list);
2194
2195 ret = i915_wait_request(dev, request->seqno);
2196 if (ret)
Chris Wilson07f73f62009-09-14 16:50:30 +01002197 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002198
Chris Wilson07f73f62009-09-14 16:50:30 +01002199 continue;
Eric Anholt673a3942008-07-30 12:06:12 -07002200 }
2201
2202 /* If we didn't have anything on the request list but there
2203 * are buffers awaiting a flush, emit one and try again.
2204 * When we wait on it, those buffers waiting for that flush
2205 * will get moved to inactive.
2206 */
2207 if (!list_empty(&dev_priv->mm.flushing_list)) {
Chris Wilson07f73f62009-09-14 16:50:30 +01002208 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07002209
Chris Wilson9a1e2582009-09-20 20:16:50 +01002210 /* Find an object that we can immediately reuse */
2211 list_for_each_entry(obj_priv, &dev_priv->mm.flushing_list, list) {
Daniel Vettera8089e82010-04-09 19:05:09 +00002212 obj = &obj_priv->base;
Chris Wilson9a1e2582009-09-20 20:16:50 +01002213 if (obj->size >= min_size)
2214 break;
Eric Anholt673a3942008-07-30 12:06:12 -07002215
Chris Wilson9a1e2582009-09-20 20:16:50 +01002216 obj = NULL;
2217 }
Eric Anholt673a3942008-07-30 12:06:12 -07002218
Chris Wilson9a1e2582009-09-20 20:16:50 +01002219 if (obj != NULL) {
2220 uint32_t seqno;
Chris Wilson07f73f62009-09-14 16:50:30 +01002221
Chris Wilson9a1e2582009-09-20 20:16:50 +01002222 i915_gem_flush(dev,
2223 obj->write_domain,
2224 obj->write_domain);
2225 seqno = i915_add_request(dev, NULL, obj->write_domain);
2226 if (seqno == 0)
2227 return -ENOMEM;
Chris Wilson9a1e2582009-09-20 20:16:50 +01002228 continue;
2229 }
Eric Anholt673a3942008-07-30 12:06:12 -07002230 }
2231
Chris Wilson07f73f62009-09-14 16:50:30 +01002232 /* If we didn't do any of the above, there's no single buffer
2233 * large enough to swap out for the new one, so just evict
2234 * everything and start again. (This should be rare.)
Eric Anholt673a3942008-07-30 12:06:12 -07002235 */
Chris Wilson97311292009-09-21 00:22:34 +01002236 if (!list_empty (&dev_priv->mm.inactive_list))
Chris Wilsonab5ee572009-09-20 19:25:47 +01002237 return i915_gem_evict_from_inactive_list(dev);
Chris Wilson97311292009-09-21 00:22:34 +01002238 else
Chris Wilson07f73f62009-09-14 16:50:30 +01002239 return i915_gem_evict_everything(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07002240 }
Keith Packardac94a962008-11-20 23:30:27 -08002241}
2242
Ben Gamari6911a9b2009-04-02 11:24:54 -07002243int
Chris Wilson4bdadb92010-01-27 13:36:32 +00002244i915_gem_object_get_pages(struct drm_gem_object *obj,
2245 gfp_t gfpmask)
Eric Anholt673a3942008-07-30 12:06:12 -07002246{
Daniel Vetter23010e42010-03-08 13:35:02 +01002247 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002248 int page_count, i;
2249 struct address_space *mapping;
2250 struct inode *inode;
2251 struct page *page;
Eric Anholt673a3942008-07-30 12:06:12 -07002252
Eric Anholt856fa192009-03-19 14:10:50 -07002253 if (obj_priv->pages_refcount++ != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07002254 return 0;
2255
2256 /* Get the list of pages out of our struct file. They'll be pinned
2257 * at this point until we release them.
2258 */
2259 page_count = obj->size / PAGE_SIZE;
Eric Anholt856fa192009-03-19 14:10:50 -07002260 BUG_ON(obj_priv->pages != NULL);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07002261 obj_priv->pages = drm_calloc_large(page_count, sizeof(struct page *));
Eric Anholt856fa192009-03-19 14:10:50 -07002262 if (obj_priv->pages == NULL) {
Eric Anholt856fa192009-03-19 14:10:50 -07002263 obj_priv->pages_refcount--;
Eric Anholt673a3942008-07-30 12:06:12 -07002264 return -ENOMEM;
2265 }
2266
2267 inode = obj->filp->f_path.dentry->d_inode;
2268 mapping = inode->i_mapping;
2269 for (i = 0; i < page_count; i++) {
Chris Wilson4bdadb92010-01-27 13:36:32 +00002270 page = read_cache_page_gfp(mapping, i,
2271 mapping_gfp_mask (mapping) |
2272 __GFP_COLD |
2273 gfpmask);
Chris Wilson1f2b1012010-03-12 19:52:55 +00002274 if (IS_ERR(page))
2275 goto err_pages;
2276
Eric Anholt856fa192009-03-19 14:10:50 -07002277 obj_priv->pages[i] = page;
Eric Anholt673a3942008-07-30 12:06:12 -07002278 }
Eric Anholt280b7132009-03-12 16:56:27 -07002279
2280 if (obj_priv->tiling_mode != I915_TILING_NONE)
2281 i915_gem_object_do_bit_17_swizzle(obj);
2282
Eric Anholt673a3942008-07-30 12:06:12 -07002283 return 0;
Chris Wilson1f2b1012010-03-12 19:52:55 +00002284
2285err_pages:
2286 while (i--)
2287 page_cache_release(obj_priv->pages[i]);
2288
2289 drm_free_large(obj_priv->pages);
2290 obj_priv->pages = NULL;
2291 obj_priv->pages_refcount--;
2292 return PTR_ERR(page);
Eric Anholt673a3942008-07-30 12:06:12 -07002293}
2294
Eric Anholt4e901fd2009-10-26 16:44:17 -07002295static void sandybridge_write_fence_reg(struct drm_i915_fence_reg *reg)
2296{
2297 struct drm_gem_object *obj = reg->obj;
2298 struct drm_device *dev = obj->dev;
2299 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002300 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt4e901fd2009-10-26 16:44:17 -07002301 int regnum = obj_priv->fence_reg;
2302 uint64_t val;
2303
2304 val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
2305 0xfffff000) << 32;
2306 val |= obj_priv->gtt_offset & 0xfffff000;
2307 val |= (uint64_t)((obj_priv->stride / 128) - 1) <<
2308 SANDYBRIDGE_FENCE_PITCH_SHIFT;
2309
2310 if (obj_priv->tiling_mode == I915_TILING_Y)
2311 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2312 val |= I965_FENCE_REG_VALID;
2313
2314 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (regnum * 8), val);
2315}
2316
Jesse Barnesde151cf2008-11-12 10:03:55 -08002317static void i965_write_fence_reg(struct drm_i915_fence_reg *reg)
2318{
2319 struct drm_gem_object *obj = reg->obj;
2320 struct drm_device *dev = obj->dev;
2321 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002322 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002323 int regnum = obj_priv->fence_reg;
2324 uint64_t val;
2325
2326 val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
2327 0xfffff000) << 32;
2328 val |= obj_priv->gtt_offset & 0xfffff000;
2329 val |= ((obj_priv->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
2330 if (obj_priv->tiling_mode == I915_TILING_Y)
2331 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2332 val |= I965_FENCE_REG_VALID;
2333
2334 I915_WRITE64(FENCE_REG_965_0 + (regnum * 8), val);
2335}
2336
2337static void i915_write_fence_reg(struct drm_i915_fence_reg *reg)
2338{
2339 struct drm_gem_object *obj = reg->obj;
2340 struct drm_device *dev = obj->dev;
2341 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002342 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002343 int regnum = obj_priv->fence_reg;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002344 int tile_width;
Eric Anholtdc529a42009-03-10 22:34:49 -07002345 uint32_t fence_reg, val;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002346 uint32_t pitch_val;
2347
2348 if ((obj_priv->gtt_offset & ~I915_FENCE_START_MASK) ||
2349 (obj_priv->gtt_offset & (obj->size - 1))) {
Linus Torvaldsf06da262009-02-09 08:57:29 -08002350 WARN(1, "%s: object 0x%08x not 1M or size (0x%zx) aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002351 __func__, obj_priv->gtt_offset, obj->size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002352 return;
2353 }
2354
Jesse Barnes0f973f22009-01-26 17:10:45 -08002355 if (obj_priv->tiling_mode == I915_TILING_Y &&
2356 HAS_128_BYTE_Y_TILING(dev))
2357 tile_width = 128;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002358 else
Jesse Barnes0f973f22009-01-26 17:10:45 -08002359 tile_width = 512;
2360
2361 /* Note: pitch better be a power of two tile widths */
2362 pitch_val = obj_priv->stride / tile_width;
2363 pitch_val = ffs(pitch_val) - 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002364
2365 val = obj_priv->gtt_offset;
2366 if (obj_priv->tiling_mode == I915_TILING_Y)
2367 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2368 val |= I915_FENCE_SIZE_BITS(obj->size);
2369 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2370 val |= I830_FENCE_REG_VALID;
2371
Eric Anholtdc529a42009-03-10 22:34:49 -07002372 if (regnum < 8)
2373 fence_reg = FENCE_REG_830_0 + (regnum * 4);
2374 else
2375 fence_reg = FENCE_REG_945_8 + ((regnum - 8) * 4);
2376 I915_WRITE(fence_reg, val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002377}
2378
2379static void i830_write_fence_reg(struct drm_i915_fence_reg *reg)
2380{
2381 struct drm_gem_object *obj = reg->obj;
2382 struct drm_device *dev = obj->dev;
2383 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002384 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002385 int regnum = obj_priv->fence_reg;
2386 uint32_t val;
2387 uint32_t pitch_val;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002388 uint32_t fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002389
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002390 if ((obj_priv->gtt_offset & ~I830_FENCE_START_MASK) ||
Jesse Barnesde151cf2008-11-12 10:03:55 -08002391 (obj_priv->gtt_offset & (obj->size - 1))) {
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002392 WARN(1, "%s: object 0x%08x not 512K or size aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002393 __func__, obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002394 return;
2395 }
2396
Eric Anholte76a16d2009-05-26 17:44:56 -07002397 pitch_val = obj_priv->stride / 128;
2398 pitch_val = ffs(pitch_val) - 1;
2399 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2400
Jesse Barnesde151cf2008-11-12 10:03:55 -08002401 val = obj_priv->gtt_offset;
2402 if (obj_priv->tiling_mode == I915_TILING_Y)
2403 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002404 fence_size_bits = I830_FENCE_SIZE_BITS(obj->size);
2405 WARN_ON(fence_size_bits & ~0x00000f00);
2406 val |= fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002407 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2408 val |= I830_FENCE_REG_VALID;
2409
2410 I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002411}
2412
Daniel Vetterae3db242010-02-19 11:51:58 +01002413static int i915_find_fence_reg(struct drm_device *dev)
2414{
2415 struct drm_i915_fence_reg *reg = NULL;
2416 struct drm_i915_gem_object *obj_priv = NULL;
2417 struct drm_i915_private *dev_priv = dev->dev_private;
2418 struct drm_gem_object *obj = NULL;
2419 int i, avail, ret;
2420
2421 /* First try to find a free reg */
2422 avail = 0;
2423 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2424 reg = &dev_priv->fence_regs[i];
2425 if (!reg->obj)
2426 return i;
2427
Daniel Vetter23010e42010-03-08 13:35:02 +01002428 obj_priv = to_intel_bo(reg->obj);
Daniel Vetterae3db242010-02-19 11:51:58 +01002429 if (!obj_priv->pin_count)
2430 avail++;
2431 }
2432
2433 if (avail == 0)
2434 return -ENOSPC;
2435
2436 /* None available, try to steal one or wait for a user to finish */
2437 i = I915_FENCE_REG_NONE;
2438 list_for_each_entry(obj_priv, &dev_priv->mm.fence_list,
2439 fence_list) {
Daniel Vettera8089e82010-04-09 19:05:09 +00002440 obj = &obj_priv->base;
Daniel Vetterae3db242010-02-19 11:51:58 +01002441
2442 if (obj_priv->pin_count)
2443 continue;
2444
2445 /* found one! */
2446 i = obj_priv->fence_reg;
2447 break;
2448 }
2449
2450 BUG_ON(i == I915_FENCE_REG_NONE);
2451
2452 /* We only have a reference on obj from the active list. put_fence_reg
2453 * might drop that one, causing a use-after-free in it. So hold a
2454 * private reference to obj like the other callers of put_fence_reg
2455 * (set_tiling ioctl) do. */
2456 drm_gem_object_reference(obj);
2457 ret = i915_gem_object_put_fence_reg(obj);
2458 drm_gem_object_unreference(obj);
2459 if (ret != 0)
2460 return ret;
2461
2462 return i;
2463}
2464
Jesse Barnesde151cf2008-11-12 10:03:55 -08002465/**
2466 * i915_gem_object_get_fence_reg - set up a fence reg for an object
2467 * @obj: object to map through a fence reg
2468 *
2469 * When mapping objects through the GTT, userspace wants to be able to write
2470 * to them without having to worry about swizzling if the object is tiled.
2471 *
2472 * This function walks the fence regs looking for a free one for @obj,
2473 * stealing one if it can't find any.
2474 *
2475 * It then sets up the reg based on the object's properties: address, pitch
2476 * and tiling format.
2477 */
Chris Wilson8c4b8c32009-06-17 22:08:52 +01002478int
2479i915_gem_object_get_fence_reg(struct drm_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002480{
2481 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002482 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002483 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002484 struct drm_i915_fence_reg *reg = NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002485 int ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002486
Eric Anholta09ba7f2009-08-29 12:49:51 -07002487 /* Just update our place in the LRU if our fence is getting used. */
2488 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
2489 list_move_tail(&obj_priv->fence_list, &dev_priv->mm.fence_list);
2490 return 0;
2491 }
2492
Jesse Barnesde151cf2008-11-12 10:03:55 -08002493 switch (obj_priv->tiling_mode) {
2494 case I915_TILING_NONE:
2495 WARN(1, "allocating a fence for non-tiled object?\n");
2496 break;
2497 case I915_TILING_X:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002498 if (!obj_priv->stride)
2499 return -EINVAL;
2500 WARN((obj_priv->stride & (512 - 1)),
2501 "object 0x%08x is X tiled but has non-512B pitch\n",
2502 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002503 break;
2504 case I915_TILING_Y:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002505 if (!obj_priv->stride)
2506 return -EINVAL;
2507 WARN((obj_priv->stride & (128 - 1)),
2508 "object 0x%08x is Y tiled but has non-128B pitch\n",
2509 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002510 break;
2511 }
2512
Daniel Vetterae3db242010-02-19 11:51:58 +01002513 ret = i915_find_fence_reg(dev);
2514 if (ret < 0)
2515 return ret;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002516
Daniel Vetterae3db242010-02-19 11:51:58 +01002517 obj_priv->fence_reg = ret;
2518 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
Eric Anholta09ba7f2009-08-29 12:49:51 -07002519 list_add_tail(&obj_priv->fence_list, &dev_priv->mm.fence_list);
2520
Jesse Barnesde151cf2008-11-12 10:03:55 -08002521 reg->obj = obj;
2522
Eric Anholt4e901fd2009-10-26 16:44:17 -07002523 if (IS_GEN6(dev))
2524 sandybridge_write_fence_reg(reg);
2525 else if (IS_I965G(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08002526 i965_write_fence_reg(reg);
2527 else if (IS_I9XX(dev))
2528 i915_write_fence_reg(reg);
2529 else
2530 i830_write_fence_reg(reg);
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002531
Daniel Vetterae3db242010-02-19 11:51:58 +01002532 trace_i915_gem_object_get_fence(obj, obj_priv->fence_reg,
2533 obj_priv->tiling_mode);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002534
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002535 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002536}
2537
2538/**
2539 * i915_gem_clear_fence_reg - clear out fence register info
2540 * @obj: object to clear
2541 *
2542 * Zeroes out the fence register itself and clears out the associated
2543 * data structures in dev_priv and obj_priv.
2544 */
2545static void
2546i915_gem_clear_fence_reg(struct drm_gem_object *obj)
2547{
2548 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002549 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002550 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002551
Eric Anholt4e901fd2009-10-26 16:44:17 -07002552 if (IS_GEN6(dev)) {
2553 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 +
2554 (obj_priv->fence_reg * 8), 0);
2555 } else if (IS_I965G(dev)) {
Jesse Barnesde151cf2008-11-12 10:03:55 -08002556 I915_WRITE64(FENCE_REG_965_0 + (obj_priv->fence_reg * 8), 0);
Eric Anholt4e901fd2009-10-26 16:44:17 -07002557 } else {
Eric Anholtdc529a42009-03-10 22:34:49 -07002558 uint32_t fence_reg;
2559
2560 if (obj_priv->fence_reg < 8)
2561 fence_reg = FENCE_REG_830_0 + obj_priv->fence_reg * 4;
2562 else
2563 fence_reg = FENCE_REG_945_8 + (obj_priv->fence_reg -
2564 8) * 4;
2565
2566 I915_WRITE(fence_reg, 0);
2567 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002568
2569 dev_priv->fence_regs[obj_priv->fence_reg].obj = NULL;
2570 obj_priv->fence_reg = I915_FENCE_REG_NONE;
Eric Anholta09ba7f2009-08-29 12:49:51 -07002571 list_del_init(&obj_priv->fence_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002572}
2573
Eric Anholt673a3942008-07-30 12:06:12 -07002574/**
Chris Wilson52dc7d32009-06-06 09:46:01 +01002575 * i915_gem_object_put_fence_reg - waits on outstanding fenced access
2576 * to the buffer to finish, and then resets the fence register.
2577 * @obj: tiled object holding a fence register.
2578 *
2579 * Zeroes out the fence register itself and clears out the associated
2580 * data structures in dev_priv and obj_priv.
2581 */
2582int
2583i915_gem_object_put_fence_reg(struct drm_gem_object *obj)
2584{
2585 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01002586 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson52dc7d32009-06-06 09:46:01 +01002587
2588 if (obj_priv->fence_reg == I915_FENCE_REG_NONE)
2589 return 0;
2590
Daniel Vetter10ae9bd2010-02-01 13:59:17 +01002591 /* If we've changed tiling, GTT-mappings of the object
2592 * need to re-fault to ensure that the correct fence register
2593 * setup is in place.
2594 */
2595 i915_gem_release_mmap(obj);
2596
Chris Wilson52dc7d32009-06-06 09:46:01 +01002597 /* On the i915, GPU access to tiled buffers is via a fence,
2598 * therefore we must wait for any outstanding access to complete
2599 * before clearing the fence.
2600 */
2601 if (!IS_I965G(dev)) {
2602 int ret;
2603
2604 i915_gem_object_flush_gpu_write_domain(obj);
Chris Wilson52dc7d32009-06-06 09:46:01 +01002605 ret = i915_gem_object_wait_rendering(obj);
2606 if (ret != 0)
2607 return ret;
2608 }
2609
Daniel Vetter4a726612010-02-01 13:59:16 +01002610 i915_gem_object_flush_gtt_write_domain(obj);
Chris Wilson52dc7d32009-06-06 09:46:01 +01002611 i915_gem_clear_fence_reg (obj);
2612
2613 return 0;
2614}
2615
2616/**
Eric Anholt673a3942008-07-30 12:06:12 -07002617 * Finds free space in the GTT aperture and binds the object there.
2618 */
2619static int
2620i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment)
2621{
2622 struct drm_device *dev = obj->dev;
2623 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002624 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002625 struct drm_mm_node *free_space;
Chris Wilson4bdadb92010-01-27 13:36:32 +00002626 gfp_t gfpmask = __GFP_NORETRY | __GFP_NOWARN;
Chris Wilson07f73f62009-09-14 16:50:30 +01002627 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002628
Chris Wilsonbb6baf72009-09-22 14:24:13 +01002629 if (obj_priv->madv != I915_MADV_WILLNEED) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01002630 DRM_ERROR("Attempting to bind a purgeable object\n");
2631 return -EINVAL;
2632 }
2633
Eric Anholt673a3942008-07-30 12:06:12 -07002634 if (alignment == 0)
Jesse Barnes0f973f22009-01-26 17:10:45 -08002635 alignment = i915_gem_get_gtt_alignment(obj);
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002636 if (alignment & (i915_gem_get_gtt_alignment(obj) - 1)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002637 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2638 return -EINVAL;
2639 }
2640
2641 search_free:
2642 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
2643 obj->size, alignment, 0);
2644 if (free_space != NULL) {
2645 obj_priv->gtt_space = drm_mm_get_block(free_space, obj->size,
2646 alignment);
2647 if (obj_priv->gtt_space != NULL) {
2648 obj_priv->gtt_space->private = obj;
2649 obj_priv->gtt_offset = obj_priv->gtt_space->start;
2650 }
2651 }
2652 if (obj_priv->gtt_space == NULL) {
2653 /* If the gtt is empty and we're still having trouble
2654 * fitting our object in, we're out of memory.
2655 */
2656#if WATCH_LRU
2657 DRM_INFO("%s: GTT full, evicting something\n", __func__);
2658#endif
Chris Wilson07f73f62009-09-14 16:50:30 +01002659 ret = i915_gem_evict_something(dev, obj->size);
Chris Wilson97311292009-09-21 00:22:34 +01002660 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002661 return ret;
Chris Wilson97311292009-09-21 00:22:34 +01002662
Eric Anholt673a3942008-07-30 12:06:12 -07002663 goto search_free;
2664 }
2665
2666#if WATCH_BUF
Krzysztof Halasacfd43c02009-06-20 00:31:28 +02002667 DRM_INFO("Binding object of size %zd at 0x%08x\n",
Eric Anholt673a3942008-07-30 12:06:12 -07002668 obj->size, obj_priv->gtt_offset);
2669#endif
Chris Wilson4bdadb92010-01-27 13:36:32 +00002670 ret = i915_gem_object_get_pages(obj, gfpmask);
Eric Anholt673a3942008-07-30 12:06:12 -07002671 if (ret) {
2672 drm_mm_put_block(obj_priv->gtt_space);
2673 obj_priv->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002674
2675 if (ret == -ENOMEM) {
2676 /* first try to clear up some space from the GTT */
2677 ret = i915_gem_evict_something(dev, obj->size);
2678 if (ret) {
Chris Wilson07f73f62009-09-14 16:50:30 +01002679 /* now try to shrink everyone else */
Chris Wilson4bdadb92010-01-27 13:36:32 +00002680 if (gfpmask) {
2681 gfpmask = 0;
2682 goto search_free;
Chris Wilson07f73f62009-09-14 16:50:30 +01002683 }
2684
2685 return ret;
2686 }
2687
2688 goto search_free;
2689 }
2690
Eric Anholt673a3942008-07-30 12:06:12 -07002691 return ret;
2692 }
2693
Eric Anholt673a3942008-07-30 12:06:12 -07002694 /* Create an AGP memory structure pointing at our pages, and bind it
2695 * into the GTT.
2696 */
2697 obj_priv->agp_mem = drm_agp_bind_pages(dev,
Eric Anholt856fa192009-03-19 14:10:50 -07002698 obj_priv->pages,
Chris Wilson07f73f62009-09-14 16:50:30 +01002699 obj->size >> PAGE_SHIFT,
Keith Packardba1eb1d2008-10-14 19:55:10 -07002700 obj_priv->gtt_offset,
2701 obj_priv->agp_type);
Eric Anholt673a3942008-07-30 12:06:12 -07002702 if (obj_priv->agp_mem == NULL) {
Eric Anholt856fa192009-03-19 14:10:50 -07002703 i915_gem_object_put_pages(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002704 drm_mm_put_block(obj_priv->gtt_space);
2705 obj_priv->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002706
2707 ret = i915_gem_evict_something(dev, obj->size);
Chris Wilson97311292009-09-21 00:22:34 +01002708 if (ret)
Chris Wilson07f73f62009-09-14 16:50:30 +01002709 return ret;
Chris Wilson07f73f62009-09-14 16:50:30 +01002710
2711 goto search_free;
Eric Anholt673a3942008-07-30 12:06:12 -07002712 }
2713 atomic_inc(&dev->gtt_count);
2714 atomic_add(obj->size, &dev->gtt_memory);
2715
2716 /* Assert that the object is not currently in any GPU domain. As it
2717 * wasn't in the GTT, there shouldn't be any way it could have been in
2718 * a GPU cache
2719 */
Chris Wilson21d509e2009-06-06 09:46:02 +01002720 BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS);
2721 BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS);
Eric Anholt673a3942008-07-30 12:06:12 -07002722
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002723 trace_i915_gem_object_bind(obj, obj_priv->gtt_offset);
2724
Eric Anholt673a3942008-07-30 12:06:12 -07002725 return 0;
2726}
2727
2728void
2729i915_gem_clflush_object(struct drm_gem_object *obj)
2730{
Daniel Vetter23010e42010-03-08 13:35:02 +01002731 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002732
2733 /* If we don't have a page list set up, then we're not pinned
2734 * to GPU, and we can ignore the cache flush because it'll happen
2735 * again at bind time.
2736 */
Eric Anholt856fa192009-03-19 14:10:50 -07002737 if (obj_priv->pages == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002738 return;
2739
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002740 trace_i915_gem_object_clflush(obj);
Eric Anholtcfa16a02009-05-26 18:46:16 -07002741
Eric Anholt856fa192009-03-19 14:10:50 -07002742 drm_clflush_pages(obj_priv->pages, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002743}
2744
Eric Anholte47c68e2008-11-14 13:35:19 -08002745/** Flushes any GPU write domain for the object if it's dirty. */
2746static void
2747i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj)
2748{
2749 struct drm_device *dev = obj->dev;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002750 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002751
2752 if ((obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
2753 return;
2754
2755 /* Queue the GPU write cache flushing we need. */
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002756 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002757 i915_gem_flush(dev, 0, obj->write_domain);
Daniel Vetter922a2ef2010-02-19 11:52:01 +01002758 (void) i915_add_request(dev, NULL, obj->write_domain);
Daniel Vetter99fcb762010-02-07 16:20:18 +01002759 BUG_ON(obj->write_domain);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002760
2761 trace_i915_gem_object_change_domain(obj,
2762 obj->read_domains,
2763 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002764}
2765
2766/** Flushes the GTT write domain for the object if it's dirty. */
2767static void
2768i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj)
2769{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002770 uint32_t old_write_domain;
2771
Eric Anholte47c68e2008-11-14 13:35:19 -08002772 if (obj->write_domain != I915_GEM_DOMAIN_GTT)
2773 return;
2774
2775 /* No actual flushing is required for the GTT write domain. Writes
2776 * to it immediately go to main memory as far as we know, so there's
2777 * no chipset flush. It also doesn't land in render cache.
2778 */
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002779 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002780 obj->write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002781
2782 trace_i915_gem_object_change_domain(obj,
2783 obj->read_domains,
2784 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002785}
2786
2787/** Flushes the CPU write domain for the object if it's dirty. */
2788static void
2789i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj)
2790{
2791 struct drm_device *dev = obj->dev;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002792 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002793
2794 if (obj->write_domain != I915_GEM_DOMAIN_CPU)
2795 return;
2796
2797 i915_gem_clflush_object(obj);
2798 drm_agp_chipset_flush(dev);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002799 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002800 obj->write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002801
2802 trace_i915_gem_object_change_domain(obj,
2803 obj->read_domains,
2804 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002805}
2806
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002807void
2808i915_gem_object_flush_write_domain(struct drm_gem_object *obj)
2809{
2810 switch (obj->write_domain) {
2811 case I915_GEM_DOMAIN_GTT:
2812 i915_gem_object_flush_gtt_write_domain(obj);
2813 break;
2814 case I915_GEM_DOMAIN_CPU:
2815 i915_gem_object_flush_cpu_write_domain(obj);
2816 break;
2817 default:
2818 i915_gem_object_flush_gpu_write_domain(obj);
2819 break;
2820 }
2821}
2822
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002823/**
2824 * Moves a single object to the GTT read, and possibly write domain.
2825 *
2826 * This function returns when the move is complete, including waiting on
2827 * flushes to occur.
2828 */
Jesse Barnes79e53942008-11-07 14:24:08 -08002829int
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002830i915_gem_object_set_to_gtt_domain(struct drm_gem_object *obj, int write)
2831{
Daniel Vetter23010e42010-03-08 13:35:02 +01002832 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002833 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002834 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002835
Eric Anholt02354392008-11-26 13:58:13 -08002836 /* Not valid to be called on unbound objects. */
2837 if (obj_priv->gtt_space == NULL)
2838 return -EINVAL;
2839
Eric Anholte47c68e2008-11-14 13:35:19 -08002840 i915_gem_object_flush_gpu_write_domain(obj);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002841 /* Wait on any GPU rendering and flushing to occur. */
Eric Anholte47c68e2008-11-14 13:35:19 -08002842 ret = i915_gem_object_wait_rendering(obj);
2843 if (ret != 0)
2844 return ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002845
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002846 old_write_domain = obj->write_domain;
2847 old_read_domains = obj->read_domains;
2848
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002849 /* If we're writing through the GTT domain, then CPU and GPU caches
2850 * will need to be invalidated at next use.
2851 */
2852 if (write)
Eric Anholte47c68e2008-11-14 13:35:19 -08002853 obj->read_domains &= I915_GEM_DOMAIN_GTT;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002854
Eric Anholte47c68e2008-11-14 13:35:19 -08002855 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002856
2857 /* It should now be out of any other write domains, and we can update
2858 * the domain values for our changes.
2859 */
2860 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2861 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002862 if (write) {
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002863 obj->write_domain = I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002864 obj_priv->dirty = 1;
2865 }
2866
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002867 trace_i915_gem_object_change_domain(obj,
2868 old_read_domains,
2869 old_write_domain);
2870
Eric Anholte47c68e2008-11-14 13:35:19 -08002871 return 0;
2872}
2873
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002874/*
2875 * Prepare buffer for display plane. Use uninterruptible for possible flush
2876 * wait, as in modesetting process we're not supposed to be interrupted.
2877 */
2878int
2879i915_gem_object_set_to_display_plane(struct drm_gem_object *obj)
2880{
2881 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01002882 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002883 uint32_t old_write_domain, old_read_domains;
2884 int ret;
2885
2886 /* Not valid to be called on unbound objects. */
2887 if (obj_priv->gtt_space == NULL)
2888 return -EINVAL;
2889
2890 i915_gem_object_flush_gpu_write_domain(obj);
2891
2892 /* Wait on any GPU rendering and flushing to occur. */
2893 if (obj_priv->active) {
2894#if WATCH_BUF
2895 DRM_INFO("%s: object %p wait for seqno %08x\n",
2896 __func__, obj, obj_priv->last_rendering_seqno);
2897#endif
2898 ret = i915_do_wait_request(dev, obj_priv->last_rendering_seqno, 0);
2899 if (ret != 0)
2900 return ret;
2901 }
2902
2903 old_write_domain = obj->write_domain;
2904 old_read_domains = obj->read_domains;
2905
2906 obj->read_domains &= I915_GEM_DOMAIN_GTT;
2907
2908 i915_gem_object_flush_cpu_write_domain(obj);
2909
2910 /* It should now be out of any other write domains, and we can update
2911 * the domain values for our changes.
2912 */
2913 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2914 obj->read_domains |= I915_GEM_DOMAIN_GTT;
2915 obj->write_domain = I915_GEM_DOMAIN_GTT;
2916 obj_priv->dirty = 1;
2917
2918 trace_i915_gem_object_change_domain(obj,
2919 old_read_domains,
2920 old_write_domain);
2921
2922 return 0;
2923}
2924
Eric Anholte47c68e2008-11-14 13:35:19 -08002925/**
2926 * Moves a single object to the CPU read, and possibly write domain.
2927 *
2928 * This function returns when the move is complete, including waiting on
2929 * flushes to occur.
2930 */
2931static int
2932i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj, int write)
2933{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002934 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002935 int ret;
2936
2937 i915_gem_object_flush_gpu_write_domain(obj);
2938 /* Wait on any GPU rendering and flushing to occur. */
2939 ret = i915_gem_object_wait_rendering(obj);
2940 if (ret != 0)
2941 return ret;
2942
2943 i915_gem_object_flush_gtt_write_domain(obj);
2944
2945 /* If we have a partially-valid cache of the object in the CPU,
2946 * finish invalidating it and free the per-page flags.
2947 */
2948 i915_gem_object_set_to_full_cpu_read_domain(obj);
2949
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002950 old_write_domain = obj->write_domain;
2951 old_read_domains = obj->read_domains;
2952
Eric Anholte47c68e2008-11-14 13:35:19 -08002953 /* Flush the CPU cache if it's still invalid. */
2954 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
2955 i915_gem_clflush_object(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08002956
2957 obj->read_domains |= I915_GEM_DOMAIN_CPU;
2958 }
2959
2960 /* It should now be out of any other write domains, and we can update
2961 * the domain values for our changes.
2962 */
2963 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
2964
2965 /* If we're writing through the CPU, then the GPU read domains will
2966 * need to be invalidated at next use.
2967 */
2968 if (write) {
2969 obj->read_domains &= I915_GEM_DOMAIN_CPU;
2970 obj->write_domain = I915_GEM_DOMAIN_CPU;
2971 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002972
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002973 trace_i915_gem_object_change_domain(obj,
2974 old_read_domains,
2975 old_write_domain);
2976
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002977 return 0;
2978}
2979
Eric Anholt673a3942008-07-30 12:06:12 -07002980/*
2981 * Set the next domain for the specified object. This
2982 * may not actually perform the necessary flushing/invaliding though,
2983 * as that may want to be batched with other set_domain operations
2984 *
2985 * This is (we hope) the only really tricky part of gem. The goal
2986 * is fairly simple -- track which caches hold bits of the object
2987 * and make sure they remain coherent. A few concrete examples may
2988 * help to explain how it works. For shorthand, we use the notation
2989 * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
2990 * a pair of read and write domain masks.
2991 *
2992 * Case 1: the batch buffer
2993 *
2994 * 1. Allocated
2995 * 2. Written by CPU
2996 * 3. Mapped to GTT
2997 * 4. Read by GPU
2998 * 5. Unmapped from GTT
2999 * 6. Freed
3000 *
3001 * Let's take these a step at a time
3002 *
3003 * 1. Allocated
3004 * Pages allocated from the kernel may still have
3005 * cache contents, so we set them to (CPU, CPU) always.
3006 * 2. Written by CPU (using pwrite)
3007 * The pwrite function calls set_domain (CPU, CPU) and
3008 * this function does nothing (as nothing changes)
3009 * 3. Mapped by GTT
3010 * This function asserts that the object is not
3011 * currently in any GPU-based read or write domains
3012 * 4. Read by GPU
3013 * i915_gem_execbuffer calls set_domain (COMMAND, 0).
3014 * As write_domain is zero, this function adds in the
3015 * current read domains (CPU+COMMAND, 0).
3016 * flush_domains is set to CPU.
3017 * invalidate_domains is set to COMMAND
3018 * clflush is run to get data out of the CPU caches
3019 * then i915_dev_set_domain calls i915_gem_flush to
3020 * emit an MI_FLUSH and drm_agp_chipset_flush
3021 * 5. Unmapped from GTT
3022 * i915_gem_object_unbind calls set_domain (CPU, CPU)
3023 * flush_domains and invalidate_domains end up both zero
3024 * so no flushing/invalidating happens
3025 * 6. Freed
3026 * yay, done
3027 *
3028 * Case 2: The shared render buffer
3029 *
3030 * 1. Allocated
3031 * 2. Mapped to GTT
3032 * 3. Read/written by GPU
3033 * 4. set_domain to (CPU,CPU)
3034 * 5. Read/written by CPU
3035 * 6. Read/written by GPU
3036 *
3037 * 1. Allocated
3038 * Same as last example, (CPU, CPU)
3039 * 2. Mapped to GTT
3040 * Nothing changes (assertions find that it is not in the GPU)
3041 * 3. Read/written by GPU
3042 * execbuffer calls set_domain (RENDER, RENDER)
3043 * flush_domains gets CPU
3044 * invalidate_domains gets GPU
3045 * clflush (obj)
3046 * MI_FLUSH and drm_agp_chipset_flush
3047 * 4. set_domain (CPU, CPU)
3048 * flush_domains gets GPU
3049 * invalidate_domains gets CPU
3050 * wait_rendering (obj) to make sure all drawing is complete.
3051 * This will include an MI_FLUSH to get the data from GPU
3052 * to memory
3053 * clflush (obj) to invalidate the CPU cache
3054 * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
3055 * 5. Read/written by CPU
3056 * cache lines are loaded and dirtied
3057 * 6. Read written by GPU
3058 * Same as last GPU access
3059 *
3060 * Case 3: The constant buffer
3061 *
3062 * 1. Allocated
3063 * 2. Written by CPU
3064 * 3. Read by GPU
3065 * 4. Updated (written) by CPU again
3066 * 5. Read by GPU
3067 *
3068 * 1. Allocated
3069 * (CPU, CPU)
3070 * 2. Written by CPU
3071 * (CPU, CPU)
3072 * 3. Read by GPU
3073 * (CPU+RENDER, 0)
3074 * flush_domains = CPU
3075 * invalidate_domains = RENDER
3076 * clflush (obj)
3077 * MI_FLUSH
3078 * drm_agp_chipset_flush
3079 * 4. Updated (written) by CPU again
3080 * (CPU, CPU)
3081 * flush_domains = 0 (no previous write domain)
3082 * invalidate_domains = 0 (no new read domains)
3083 * 5. Read by GPU
3084 * (CPU+RENDER, 0)
3085 * flush_domains = CPU
3086 * invalidate_domains = RENDER
3087 * clflush (obj)
3088 * MI_FLUSH
3089 * drm_agp_chipset_flush
3090 */
Keith Packardc0d90822008-11-20 23:11:08 -08003091static void
Eric Anholt8b0e3782009-02-19 14:40:50 -08003092i915_gem_object_set_to_gpu_domain(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07003093{
3094 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01003095 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003096 uint32_t invalidate_domains = 0;
3097 uint32_t flush_domains = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003098 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003099
Eric Anholt8b0e3782009-02-19 14:40:50 -08003100 BUG_ON(obj->pending_read_domains & I915_GEM_DOMAIN_CPU);
3101 BUG_ON(obj->pending_write_domain == I915_GEM_DOMAIN_CPU);
Eric Anholt673a3942008-07-30 12:06:12 -07003102
Jesse Barnes652c3932009-08-17 13:31:43 -07003103 intel_mark_busy(dev, obj);
3104
Eric Anholt673a3942008-07-30 12:06:12 -07003105#if WATCH_BUF
3106 DRM_INFO("%s: object %p read %08x -> %08x write %08x -> %08x\n",
3107 __func__, obj,
Eric Anholt8b0e3782009-02-19 14:40:50 -08003108 obj->read_domains, obj->pending_read_domains,
3109 obj->write_domain, obj->pending_write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07003110#endif
3111 /*
3112 * If the object isn't moving to a new write domain,
3113 * let the object stay in multiple read domains
3114 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003115 if (obj->pending_write_domain == 0)
3116 obj->pending_read_domains |= obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07003117 else
3118 obj_priv->dirty = 1;
3119
3120 /*
3121 * Flush the current write domain if
3122 * the new read domains don't match. Invalidate
3123 * any read domains which differ from the old
3124 * write domain
3125 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003126 if (obj->write_domain &&
3127 obj->write_domain != obj->pending_read_domains) {
Eric Anholt673a3942008-07-30 12:06:12 -07003128 flush_domains |= obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08003129 invalidate_domains |=
3130 obj->pending_read_domains & ~obj->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07003131 }
3132 /*
3133 * Invalidate any read caches which may have
3134 * stale data. That is, any new read domains.
3135 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003136 invalidate_domains |= obj->pending_read_domains & ~obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07003137 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU) {
3138#if WATCH_BUF
3139 DRM_INFO("%s: CPU domain flush %08x invalidate %08x\n",
3140 __func__, flush_domains, invalidate_domains);
3141#endif
Eric Anholt673a3942008-07-30 12:06:12 -07003142 i915_gem_clflush_object(obj);
3143 }
3144
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003145 old_read_domains = obj->read_domains;
3146
Eric Anholtefbeed92009-02-19 14:54:51 -08003147 /* The actual obj->write_domain will be updated with
3148 * pending_write_domain after we emit the accumulated flush for all
3149 * of our domain changes in execbuffers (which clears objects'
3150 * write_domains). So if we have a current write domain that we
3151 * aren't changing, set pending_write_domain to that.
3152 */
3153 if (flush_domains == 0 && obj->pending_write_domain == 0)
3154 obj->pending_write_domain = obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08003155 obj->read_domains = obj->pending_read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07003156
3157 dev->invalidate_domains |= invalidate_domains;
3158 dev->flush_domains |= flush_domains;
3159#if WATCH_BUF
3160 DRM_INFO("%s: read %08x write %08x invalidate %08x flush %08x\n",
3161 __func__,
3162 obj->read_domains, obj->write_domain,
3163 dev->invalidate_domains, dev->flush_domains);
3164#endif
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003165
3166 trace_i915_gem_object_change_domain(obj,
3167 old_read_domains,
3168 obj->write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07003169}
3170
3171/**
Eric Anholte47c68e2008-11-14 13:35:19 -08003172 * Moves the object from a partially CPU read to a full one.
Eric Anholt673a3942008-07-30 12:06:12 -07003173 *
Eric Anholte47c68e2008-11-14 13:35:19 -08003174 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
3175 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
3176 */
3177static void
3178i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj)
3179{
Daniel Vetter23010e42010-03-08 13:35:02 +01003180 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003181
3182 if (!obj_priv->page_cpu_valid)
3183 return;
3184
3185 /* If we're partially in the CPU read domain, finish moving it in.
3186 */
3187 if (obj->read_domains & I915_GEM_DOMAIN_CPU) {
3188 int i;
3189
3190 for (i = 0; i <= (obj->size - 1) / PAGE_SIZE; i++) {
3191 if (obj_priv->page_cpu_valid[i])
3192 continue;
Eric Anholt856fa192009-03-19 14:10:50 -07003193 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholte47c68e2008-11-14 13:35:19 -08003194 }
Eric Anholte47c68e2008-11-14 13:35:19 -08003195 }
3196
3197 /* Free the page_cpu_valid mappings which are now stale, whether
3198 * or not we've got I915_GEM_DOMAIN_CPU.
3199 */
Eric Anholt9a298b22009-03-24 12:23:04 -07003200 kfree(obj_priv->page_cpu_valid);
Eric Anholte47c68e2008-11-14 13:35:19 -08003201 obj_priv->page_cpu_valid = NULL;
3202}
3203
3204/**
3205 * Set the CPU read domain on a range of the object.
3206 *
3207 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
3208 * not entirely valid. The page_cpu_valid member of the object flags which
3209 * pages have been flushed, and will be respected by
3210 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
3211 * of the whole object.
3212 *
3213 * This function returns when the move is complete, including waiting on
3214 * flushes to occur.
Eric Anholt673a3942008-07-30 12:06:12 -07003215 */
3216static int
Eric Anholte47c68e2008-11-14 13:35:19 -08003217i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
3218 uint64_t offset, uint64_t size)
Eric Anholt673a3942008-07-30 12:06:12 -07003219{
Daniel Vetter23010e42010-03-08 13:35:02 +01003220 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003221 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003222 int i, ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003223
Eric Anholte47c68e2008-11-14 13:35:19 -08003224 if (offset == 0 && size == obj->size)
3225 return i915_gem_object_set_to_cpu_domain(obj, 0);
3226
3227 i915_gem_object_flush_gpu_write_domain(obj);
3228 /* Wait on any GPU rendering and flushing to occur. */
3229 ret = i915_gem_object_wait_rendering(obj);
3230 if (ret != 0)
3231 return ret;
3232 i915_gem_object_flush_gtt_write_domain(obj);
3233
3234 /* If we're already fully in the CPU read domain, we're done. */
3235 if (obj_priv->page_cpu_valid == NULL &&
3236 (obj->read_domains & I915_GEM_DOMAIN_CPU) != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07003237 return 0;
3238
Eric Anholte47c68e2008-11-14 13:35:19 -08003239 /* Otherwise, create/clear the per-page CPU read domain flag if we're
3240 * newly adding I915_GEM_DOMAIN_CPU
3241 */
Eric Anholt673a3942008-07-30 12:06:12 -07003242 if (obj_priv->page_cpu_valid == NULL) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003243 obj_priv->page_cpu_valid = kzalloc(obj->size / PAGE_SIZE,
3244 GFP_KERNEL);
Eric Anholte47c68e2008-11-14 13:35:19 -08003245 if (obj_priv->page_cpu_valid == NULL)
3246 return -ENOMEM;
3247 } else if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0)
3248 memset(obj_priv->page_cpu_valid, 0, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07003249
3250 /* Flush the cache on any pages that are still invalid from the CPU's
3251 * perspective.
3252 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003253 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
3254 i++) {
Eric Anholt673a3942008-07-30 12:06:12 -07003255 if (obj_priv->page_cpu_valid[i])
3256 continue;
3257
Eric Anholt856fa192009-03-19 14:10:50 -07003258 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07003259
3260 obj_priv->page_cpu_valid[i] = 1;
3261 }
3262
Eric Anholte47c68e2008-11-14 13:35:19 -08003263 /* It should now be out of any other write domains, and we can update
3264 * the domain values for our changes.
3265 */
3266 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
3267
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003268 old_read_domains = obj->read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003269 obj->read_domains |= I915_GEM_DOMAIN_CPU;
3270
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003271 trace_i915_gem_object_change_domain(obj,
3272 old_read_domains,
3273 obj->write_domain);
3274
Eric Anholt673a3942008-07-30 12:06:12 -07003275 return 0;
3276}
3277
3278/**
Eric Anholt673a3942008-07-30 12:06:12 -07003279 * Pin an object to the GTT and evaluate the relocations landing in it.
3280 */
3281static int
3282i915_gem_object_pin_and_relocate(struct drm_gem_object *obj,
3283 struct drm_file *file_priv,
Jesse Barnes76446ca2009-12-17 22:05:42 -05003284 struct drm_i915_gem_exec_object2 *entry,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003285 struct drm_i915_gem_relocation_entry *relocs)
Eric Anholt673a3942008-07-30 12:06:12 -07003286{
3287 struct drm_device *dev = obj->dev;
Keith Packard0839ccb2008-10-30 19:38:48 -07003288 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01003289 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003290 int i, ret;
Keith Packard0839ccb2008-10-30 19:38:48 -07003291 void __iomem *reloc_page;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003292 bool need_fence;
3293
3294 need_fence = entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
3295 obj_priv->tiling_mode != I915_TILING_NONE;
3296
3297 /* Check fence reg constraints and rebind if necessary */
Owain Ainsworthf590d272010-02-18 15:33:00 +00003298 if (need_fence && !i915_gem_object_fence_offset_ok(obj,
3299 obj_priv->tiling_mode))
Jesse Barnes76446ca2009-12-17 22:05:42 -05003300 i915_gem_object_unbind(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003301
3302 /* Choose the GTT offset for our buffer and put it there. */
3303 ret = i915_gem_object_pin(obj, (uint32_t) entry->alignment);
3304 if (ret)
3305 return ret;
3306
Jesse Barnes76446ca2009-12-17 22:05:42 -05003307 /*
3308 * Pre-965 chips need a fence register set up in order to
3309 * properly handle blits to/from tiled surfaces.
3310 */
3311 if (need_fence) {
3312 ret = i915_gem_object_get_fence_reg(obj);
3313 if (ret != 0) {
3314 if (ret != -EBUSY && ret != -ERESTARTSYS)
3315 DRM_ERROR("Failure to install fence: %d\n",
3316 ret);
3317 i915_gem_object_unpin(obj);
3318 return ret;
3319 }
3320 }
3321
Eric Anholt673a3942008-07-30 12:06:12 -07003322 entry->offset = obj_priv->gtt_offset;
3323
Eric Anholt673a3942008-07-30 12:06:12 -07003324 /* Apply the relocations, using the GTT aperture to avoid cache
3325 * flushing requirements.
3326 */
3327 for (i = 0; i < entry->relocation_count; i++) {
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003328 struct drm_i915_gem_relocation_entry *reloc= &relocs[i];
Eric Anholt673a3942008-07-30 12:06:12 -07003329 struct drm_gem_object *target_obj;
3330 struct drm_i915_gem_object *target_obj_priv;
Eric Anholt3043c602008-10-02 12:24:47 -07003331 uint32_t reloc_val, reloc_offset;
3332 uint32_t __iomem *reloc_entry;
Eric Anholt673a3942008-07-30 12:06:12 -07003333
Eric Anholt673a3942008-07-30 12:06:12 -07003334 target_obj = drm_gem_object_lookup(obj->dev, file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003335 reloc->target_handle);
Eric Anholt673a3942008-07-30 12:06:12 -07003336 if (target_obj == NULL) {
3337 i915_gem_object_unpin(obj);
3338 return -EBADF;
3339 }
Daniel Vetter23010e42010-03-08 13:35:02 +01003340 target_obj_priv = to_intel_bo(target_obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003341
Chris Wilson8542a0b2009-09-09 21:15:15 +01003342#if WATCH_RELOC
3343 DRM_INFO("%s: obj %p offset %08x target %d "
3344 "read %08x write %08x gtt %08x "
3345 "presumed %08x delta %08x\n",
3346 __func__,
3347 obj,
3348 (int) reloc->offset,
3349 (int) reloc->target_handle,
3350 (int) reloc->read_domains,
3351 (int) reloc->write_domain,
3352 (int) target_obj_priv->gtt_offset,
3353 (int) reloc->presumed_offset,
3354 reloc->delta);
3355#endif
3356
Eric Anholt673a3942008-07-30 12:06:12 -07003357 /* The target buffer should have appeared before us in the
3358 * exec_object list, so it should have a GTT space bound by now.
3359 */
3360 if (target_obj_priv->gtt_space == NULL) {
3361 DRM_ERROR("No GTT space found for object %d\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003362 reloc->target_handle);
Eric Anholt673a3942008-07-30 12:06:12 -07003363 drm_gem_object_unreference(target_obj);
3364 i915_gem_object_unpin(obj);
3365 return -EINVAL;
3366 }
3367
Chris Wilson8542a0b2009-09-09 21:15:15 +01003368 /* Validate that the target is in a valid r/w GPU domain */
Daniel Vetter16edd552010-02-19 11:52:02 +01003369 if (reloc->write_domain & (reloc->write_domain - 1)) {
3370 DRM_ERROR("reloc with multiple write domains: "
3371 "obj %p target %d offset %d "
3372 "read %08x write %08x",
3373 obj, reloc->target_handle,
3374 (int) reloc->offset,
3375 reloc->read_domains,
3376 reloc->write_domain);
3377 return -EINVAL;
3378 }
Chris Wilson8542a0b2009-09-09 21:15:15 +01003379 if (reloc->write_domain & I915_GEM_DOMAIN_CPU ||
3380 reloc->read_domains & I915_GEM_DOMAIN_CPU) {
3381 DRM_ERROR("reloc with read/write CPU domains: "
3382 "obj %p target %d offset %d "
3383 "read %08x write %08x",
3384 obj, reloc->target_handle,
3385 (int) reloc->offset,
3386 reloc->read_domains,
3387 reloc->write_domain);
3388 drm_gem_object_unreference(target_obj);
3389 i915_gem_object_unpin(obj);
3390 return -EINVAL;
3391 }
3392 if (reloc->write_domain && target_obj->pending_write_domain &&
3393 reloc->write_domain != target_obj->pending_write_domain) {
3394 DRM_ERROR("Write domain conflict: "
3395 "obj %p target %d offset %d "
3396 "new %08x old %08x\n",
3397 obj, reloc->target_handle,
3398 (int) reloc->offset,
3399 reloc->write_domain,
3400 target_obj->pending_write_domain);
3401 drm_gem_object_unreference(target_obj);
3402 i915_gem_object_unpin(obj);
3403 return -EINVAL;
3404 }
3405
3406 target_obj->pending_read_domains |= reloc->read_domains;
3407 target_obj->pending_write_domain |= reloc->write_domain;
3408
3409 /* If the relocation already has the right value in it, no
3410 * more work needs to be done.
3411 */
3412 if (target_obj_priv->gtt_offset == reloc->presumed_offset) {
3413 drm_gem_object_unreference(target_obj);
3414 continue;
3415 }
3416
3417 /* Check that the relocation address is valid... */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003418 if (reloc->offset > obj->size - 4) {
Eric Anholt673a3942008-07-30 12:06:12 -07003419 DRM_ERROR("Relocation beyond object bounds: "
3420 "obj %p target %d offset %d size %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003421 obj, reloc->target_handle,
3422 (int) reloc->offset, (int) obj->size);
Eric Anholt673a3942008-07-30 12:06:12 -07003423 drm_gem_object_unreference(target_obj);
3424 i915_gem_object_unpin(obj);
3425 return -EINVAL;
3426 }
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003427 if (reloc->offset & 3) {
Eric Anholt673a3942008-07-30 12:06:12 -07003428 DRM_ERROR("Relocation not 4-byte aligned: "
3429 "obj %p target %d offset %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003430 obj, reloc->target_handle,
3431 (int) reloc->offset);
Eric Anholt673a3942008-07-30 12:06:12 -07003432 drm_gem_object_unreference(target_obj);
3433 i915_gem_object_unpin(obj);
3434 return -EINVAL;
3435 }
3436
Chris Wilson8542a0b2009-09-09 21:15:15 +01003437 /* and points to somewhere within the target object. */
Chris Wilsoncd0b9fb2009-09-15 23:23:18 +01003438 if (reloc->delta >= target_obj->size) {
3439 DRM_ERROR("Relocation beyond target object bounds: "
3440 "obj %p target %d delta %d size %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003441 obj, reloc->target_handle,
Chris Wilsoncd0b9fb2009-09-15 23:23:18 +01003442 (int) reloc->delta, (int) target_obj->size);
Chris Wilson491152b2009-02-11 14:26:32 +00003443 drm_gem_object_unreference(target_obj);
3444 i915_gem_object_unpin(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003445 return -EINVAL;
3446 }
3447
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003448 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
3449 if (ret != 0) {
3450 drm_gem_object_unreference(target_obj);
3451 i915_gem_object_unpin(obj);
3452 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -07003453 }
3454
3455 /* Map the page containing the relocation we're going to
3456 * perform.
3457 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003458 reloc_offset = obj_priv->gtt_offset + reloc->offset;
Keith Packard0839ccb2008-10-30 19:38:48 -07003459 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
3460 (reloc_offset &
3461 ~(PAGE_SIZE - 1)));
Eric Anholt3043c602008-10-02 12:24:47 -07003462 reloc_entry = (uint32_t __iomem *)(reloc_page +
Keith Packard0839ccb2008-10-30 19:38:48 -07003463 (reloc_offset & (PAGE_SIZE - 1)));
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003464 reloc_val = target_obj_priv->gtt_offset + reloc->delta;
Eric Anholt673a3942008-07-30 12:06:12 -07003465
3466#if WATCH_BUF
3467 DRM_INFO("Applied relocation: %p@0x%08x %08x -> %08x\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003468 obj, (unsigned int) reloc->offset,
Eric Anholt673a3942008-07-30 12:06:12 -07003469 readl(reloc_entry), reloc_val);
3470#endif
3471 writel(reloc_val, reloc_entry);
Keith Packard0839ccb2008-10-30 19:38:48 -07003472 io_mapping_unmap_atomic(reloc_page);
Eric Anholt673a3942008-07-30 12:06:12 -07003473
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003474 /* The updated presumed offset for this entry will be
3475 * copied back out to the user.
Eric Anholt673a3942008-07-30 12:06:12 -07003476 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003477 reloc->presumed_offset = target_obj_priv->gtt_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003478
3479 drm_gem_object_unreference(target_obj);
3480 }
3481
Eric Anholt673a3942008-07-30 12:06:12 -07003482#if WATCH_BUF
3483 if (0)
3484 i915_gem_dump_object(obj, 128, __func__, ~0);
3485#endif
3486 return 0;
3487}
3488
3489/** Dispatch a batchbuffer to the ring
3490 */
3491static int
3492i915_dispatch_gem_execbuffer(struct drm_device *dev,
Jesse Barnes76446ca2009-12-17 22:05:42 -05003493 struct drm_i915_gem_execbuffer2 *exec,
Eric Anholt201361a2009-03-11 12:30:04 -07003494 struct drm_clip_rect *cliprects,
Eric Anholt673a3942008-07-30 12:06:12 -07003495 uint64_t exec_offset)
3496{
3497 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003498 int nbox = exec->num_cliprects;
3499 int i = 0, count;
Chris Wilson83d60792009-06-06 09:45:57 +01003500 uint32_t exec_start, exec_len;
Eric Anholt673a3942008-07-30 12:06:12 -07003501 RING_LOCALS;
3502
3503 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
3504 exec_len = (uint32_t) exec->batch_len;
3505
Chris Wilson8f0dc5b2009-09-24 00:43:17 +01003506 trace_i915_gem_request_submit(dev, dev_priv->mm.next_gem_seqno + 1);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003507
Eric Anholt673a3942008-07-30 12:06:12 -07003508 count = nbox ? nbox : 1;
3509
3510 for (i = 0; i < count; i++) {
3511 if (i < nbox) {
Eric Anholt201361a2009-03-11 12:30:04 -07003512 int ret = i915_emit_box(dev, cliprects, i,
Eric Anholt673a3942008-07-30 12:06:12 -07003513 exec->DR1, exec->DR4);
3514 if (ret)
3515 return ret;
3516 }
3517
3518 if (IS_I830(dev) || IS_845G(dev)) {
3519 BEGIN_LP_RING(4);
3520 OUT_RING(MI_BATCH_BUFFER);
3521 OUT_RING(exec_start | MI_BATCH_NON_SECURE);
3522 OUT_RING(exec_start + exec_len - 4);
3523 OUT_RING(0);
3524 ADVANCE_LP_RING();
3525 } else {
3526 BEGIN_LP_RING(2);
3527 if (IS_I965G(dev)) {
3528 OUT_RING(MI_BATCH_BUFFER_START |
3529 (2 << 6) |
3530 MI_BATCH_NON_SECURE_I965);
3531 OUT_RING(exec_start);
3532 } else {
3533 OUT_RING(MI_BATCH_BUFFER_START |
3534 (2 << 6));
3535 OUT_RING(exec_start | MI_BATCH_NON_SECURE);
3536 }
3537 ADVANCE_LP_RING();
3538 }
3539 }
3540
3541 /* XXX breadcrumb */
3542 return 0;
3543}
3544
3545/* Throttle our rendering by waiting until the ring has completed our requests
3546 * emitted over 20 msec ago.
3547 *
Eric Anholtb9624422009-06-03 07:27:35 +00003548 * Note that if we were to use the current jiffies each time around the loop,
3549 * we wouldn't escape the function with any frames outstanding if the time to
3550 * render a frame was over 20ms.
3551 *
Eric Anholt673a3942008-07-30 12:06:12 -07003552 * This should get us reasonable parallelism between CPU and GPU but also
3553 * relatively low latency when blocking on a particular request to finish.
3554 */
3555static int
3556i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file_priv)
3557{
3558 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
3559 int ret = 0;
Eric Anholtb9624422009-06-03 07:27:35 +00003560 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
Eric Anholt673a3942008-07-30 12:06:12 -07003561
3562 mutex_lock(&dev->struct_mutex);
Eric Anholtb9624422009-06-03 07:27:35 +00003563 while (!list_empty(&i915_file_priv->mm.request_list)) {
3564 struct drm_i915_gem_request *request;
3565
3566 request = list_first_entry(&i915_file_priv->mm.request_list,
3567 struct drm_i915_gem_request,
3568 client_list);
3569
3570 if (time_after_eq(request->emitted_jiffies, recent_enough))
3571 break;
3572
3573 ret = i915_wait_request(dev, request->seqno);
3574 if (ret != 0)
3575 break;
3576 }
Eric Anholt673a3942008-07-30 12:06:12 -07003577 mutex_unlock(&dev->struct_mutex);
Eric Anholtb9624422009-06-03 07:27:35 +00003578
Eric Anholt673a3942008-07-30 12:06:12 -07003579 return ret;
3580}
3581
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003582static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003583i915_gem_get_relocs_from_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, reloc_index = 0, i;
3588 int ret;
3589
3590 *relocs = NULL;
3591 for (i = 0; i < buffer_count; i++) {
3592 if (reloc_count + exec_list[i].relocation_count < reloc_count)
3593 return -EINVAL;
3594 reloc_count += exec_list[i].relocation_count;
3595 }
3596
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003597 *relocs = drm_calloc_large(reloc_count, sizeof(**relocs));
Jesse Barnes76446ca2009-12-17 22:05:42 -05003598 if (*relocs == NULL) {
3599 DRM_ERROR("failed to alloc relocs, count %d\n", reloc_count);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003600 return -ENOMEM;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003601 }
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003602
3603 for (i = 0; i < buffer_count; i++) {
3604 struct drm_i915_gem_relocation_entry __user *user_relocs;
3605
3606 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3607
3608 ret = copy_from_user(&(*relocs)[reloc_index],
3609 user_relocs,
3610 exec_list[i].relocation_count *
3611 sizeof(**relocs));
3612 if (ret != 0) {
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003613 drm_free_large(*relocs);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003614 *relocs = NULL;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003615 return -EFAULT;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003616 }
3617
3618 reloc_index += exec_list[i].relocation_count;
3619 }
3620
Florian Mickler2bc43b52009-04-06 22:55:41 +02003621 return 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003622}
3623
3624static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003625i915_gem_put_relocs_to_user(struct drm_i915_gem_exec_object2 *exec_list,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003626 uint32_t buffer_count,
3627 struct drm_i915_gem_relocation_entry *relocs)
3628{
3629 uint32_t reloc_count = 0, i;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003630 int ret = 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003631
Chris Wilson93533c22010-01-31 10:40:48 +00003632 if (relocs == NULL)
3633 return 0;
3634
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003635 for (i = 0; i < buffer_count; i++) {
3636 struct drm_i915_gem_relocation_entry __user *user_relocs;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003637 int unwritten;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003638
3639 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3640
Florian Mickler2bc43b52009-04-06 22:55:41 +02003641 unwritten = copy_to_user(user_relocs,
3642 &relocs[reloc_count],
3643 exec_list[i].relocation_count *
3644 sizeof(*relocs));
3645
3646 if (unwritten) {
3647 ret = -EFAULT;
3648 goto err;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003649 }
3650
3651 reloc_count += exec_list[i].relocation_count;
3652 }
3653
Florian Mickler2bc43b52009-04-06 22:55:41 +02003654err:
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003655 drm_free_large(relocs);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003656
3657 return ret;
3658}
3659
Chris Wilson83d60792009-06-06 09:45:57 +01003660static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003661i915_gem_check_execbuffer (struct drm_i915_gem_execbuffer2 *exec,
Chris Wilson83d60792009-06-06 09:45:57 +01003662 uint64_t exec_offset)
3663{
3664 uint32_t exec_start, exec_len;
3665
3666 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
3667 exec_len = (uint32_t) exec->batch_len;
3668
3669 if ((exec_start | exec_len) & 0x7)
3670 return -EINVAL;
3671
3672 if (!exec_start)
3673 return -EINVAL;
3674
3675 return 0;
3676}
3677
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003678static int
3679i915_gem_wait_for_pending_flip(struct drm_device *dev,
3680 struct drm_gem_object **object_list,
3681 int count)
3682{
3683 drm_i915_private_t *dev_priv = dev->dev_private;
3684 struct drm_i915_gem_object *obj_priv;
3685 DEFINE_WAIT(wait);
3686 int i, ret = 0;
3687
3688 for (;;) {
3689 prepare_to_wait(&dev_priv->pending_flip_queue,
3690 &wait, TASK_INTERRUPTIBLE);
3691 for (i = 0; i < count; i++) {
Daniel Vetter23010e42010-03-08 13:35:02 +01003692 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003693 if (atomic_read(&obj_priv->pending_flip) > 0)
3694 break;
3695 }
3696 if (i == count)
3697 break;
3698
3699 if (!signal_pending(current)) {
3700 mutex_unlock(&dev->struct_mutex);
3701 schedule();
3702 mutex_lock(&dev->struct_mutex);
3703 continue;
3704 }
3705 ret = -ERESTARTSYS;
3706 break;
3707 }
3708 finish_wait(&dev_priv->pending_flip_queue, &wait);
3709
3710 return ret;
3711}
3712
Eric Anholt673a3942008-07-30 12:06:12 -07003713int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003714i915_gem_do_execbuffer(struct drm_device *dev, void *data,
3715 struct drm_file *file_priv,
3716 struct drm_i915_gem_execbuffer2 *args,
3717 struct drm_i915_gem_exec_object2 *exec_list)
Eric Anholt673a3942008-07-30 12:06:12 -07003718{
3719 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003720 struct drm_gem_object **object_list = NULL;
3721 struct drm_gem_object *batch_obj;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003722 struct drm_i915_gem_object *obj_priv;
Eric Anholt201361a2009-03-11 12:30:04 -07003723 struct drm_clip_rect *cliprects = NULL;
Chris Wilson93533c22010-01-31 10:40:48 +00003724 struct drm_i915_gem_relocation_entry *relocs = NULL;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003725 int ret = 0, ret2, i, pinned = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003726 uint64_t exec_offset;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003727 uint32_t seqno, flush_domains, reloc_index;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003728 int pin_tries, flips;
Eric Anholt673a3942008-07-30 12:06:12 -07003729
3730#if WATCH_EXEC
3731 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3732 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3733#endif
3734
Eric Anholt4f481ed2008-09-10 14:22:49 -07003735 if (args->buffer_count < 1) {
3736 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3737 return -EINVAL;
3738 }
Eric Anholtc8e0f932009-11-22 03:49:37 +01003739 object_list = drm_malloc_ab(sizeof(*object_list), args->buffer_count);
Jesse Barnes76446ca2009-12-17 22:05:42 -05003740 if (object_list == NULL) {
3741 DRM_ERROR("Failed to allocate object list for %d buffers\n",
Eric Anholt673a3942008-07-30 12:06:12 -07003742 args->buffer_count);
3743 ret = -ENOMEM;
3744 goto pre_mutex_err;
3745 }
Eric Anholt673a3942008-07-30 12:06:12 -07003746
Eric Anholt201361a2009-03-11 12:30:04 -07003747 if (args->num_cliprects != 0) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003748 cliprects = kcalloc(args->num_cliprects, sizeof(*cliprects),
3749 GFP_KERNEL);
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003750 if (cliprects == NULL) {
3751 ret = -ENOMEM;
Eric Anholt201361a2009-03-11 12:30:04 -07003752 goto pre_mutex_err;
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003753 }
Eric Anholt201361a2009-03-11 12:30:04 -07003754
3755 ret = copy_from_user(cliprects,
3756 (struct drm_clip_rect __user *)
3757 (uintptr_t) args->cliprects_ptr,
3758 sizeof(*cliprects) * args->num_cliprects);
3759 if (ret != 0) {
3760 DRM_ERROR("copy %d cliprects failed: %d\n",
3761 args->num_cliprects, ret);
3762 goto pre_mutex_err;
3763 }
3764 }
3765
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003766 ret = i915_gem_get_relocs_from_user(exec_list, args->buffer_count,
3767 &relocs);
3768 if (ret != 0)
3769 goto pre_mutex_err;
3770
Eric Anholt673a3942008-07-30 12:06:12 -07003771 mutex_lock(&dev->struct_mutex);
3772
3773 i915_verify_inactive(dev, __FILE__, __LINE__);
3774
Ben Gamariba1234d2009-09-14 17:48:47 -04003775 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07003776 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003777 ret = -EIO;
3778 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003779 }
3780
3781 if (dev_priv->mm.suspended) {
Eric Anholt673a3942008-07-30 12:06:12 -07003782 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003783 ret = -EBUSY;
3784 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003785 }
3786
Keith Packardac94a962008-11-20 23:30:27 -08003787 /* Look up object handles */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003788 flips = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003789 for (i = 0; i < args->buffer_count; i++) {
3790 object_list[i] = drm_gem_object_lookup(dev, file_priv,
3791 exec_list[i].handle);
3792 if (object_list[i] == NULL) {
3793 DRM_ERROR("Invalid object handle %d at index %d\n",
3794 exec_list[i].handle, i);
Chris Wilson0ce907f2010-01-23 20:26:35 +00003795 /* prevent error path from reading uninitialized data */
3796 args->buffer_count = i + 1;
Eric Anholt673a3942008-07-30 12:06:12 -07003797 ret = -EBADF;
3798 goto err;
3799 }
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003800
Daniel Vetter23010e42010-03-08 13:35:02 +01003801 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003802 if (obj_priv->in_execbuffer) {
3803 DRM_ERROR("Object %p appears more than once in object list\n",
3804 object_list[i]);
Chris Wilson0ce907f2010-01-23 20:26:35 +00003805 /* prevent error path from reading uninitialized data */
3806 args->buffer_count = i + 1;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003807 ret = -EBADF;
3808 goto err;
3809 }
3810 obj_priv->in_execbuffer = true;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003811 flips += atomic_read(&obj_priv->pending_flip);
3812 }
3813
3814 if (flips > 0) {
3815 ret = i915_gem_wait_for_pending_flip(dev, object_list,
3816 args->buffer_count);
3817 if (ret)
3818 goto err;
Keith Packardac94a962008-11-20 23:30:27 -08003819 }
Eric Anholt673a3942008-07-30 12:06:12 -07003820
Keith Packardac94a962008-11-20 23:30:27 -08003821 /* Pin and relocate */
3822 for (pin_tries = 0; ; pin_tries++) {
3823 ret = 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003824 reloc_index = 0;
3825
Keith Packardac94a962008-11-20 23:30:27 -08003826 for (i = 0; i < args->buffer_count; i++) {
3827 object_list[i]->pending_read_domains = 0;
3828 object_list[i]->pending_write_domain = 0;
3829 ret = i915_gem_object_pin_and_relocate(object_list[i],
3830 file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003831 &exec_list[i],
3832 &relocs[reloc_index]);
Keith Packardac94a962008-11-20 23:30:27 -08003833 if (ret)
3834 break;
3835 pinned = i + 1;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003836 reloc_index += exec_list[i].relocation_count;
Keith Packardac94a962008-11-20 23:30:27 -08003837 }
3838 /* success */
3839 if (ret == 0)
3840 break;
3841
3842 /* error other than GTT full, or we've already tried again */
Chris Wilson2939e1f2009-06-06 09:46:03 +01003843 if (ret != -ENOSPC || pin_tries >= 1) {
Chris Wilson07f73f62009-09-14 16:50:30 +01003844 if (ret != -ERESTARTSYS) {
3845 unsigned long long total_size = 0;
3846 for (i = 0; i < args->buffer_count; i++)
3847 total_size += object_list[i]->size;
3848 DRM_ERROR("Failed to pin buffer %d of %d, total %llu bytes: %d\n",
3849 pinned+1, args->buffer_count,
3850 total_size, ret);
3851 DRM_ERROR("%d objects [%d pinned], "
3852 "%d object bytes [%d pinned], "
3853 "%d/%d gtt bytes\n",
3854 atomic_read(&dev->object_count),
3855 atomic_read(&dev->pin_count),
3856 atomic_read(&dev->object_memory),
3857 atomic_read(&dev->pin_memory),
3858 atomic_read(&dev->gtt_memory),
3859 dev->gtt_total);
3860 }
Eric Anholt673a3942008-07-30 12:06:12 -07003861 goto err;
3862 }
Keith Packardac94a962008-11-20 23:30:27 -08003863
3864 /* unpin all of our buffers */
3865 for (i = 0; i < pinned; i++)
3866 i915_gem_object_unpin(object_list[i]);
Eric Anholtb1177632008-12-10 10:09:41 -08003867 pinned = 0;
Keith Packardac94a962008-11-20 23:30:27 -08003868
3869 /* evict everyone we can from the aperture */
3870 ret = i915_gem_evict_everything(dev);
Chris Wilson07f73f62009-09-14 16:50:30 +01003871 if (ret && ret != -ENOSPC)
Keith Packardac94a962008-11-20 23:30:27 -08003872 goto err;
Eric Anholt673a3942008-07-30 12:06:12 -07003873 }
3874
3875 /* Set the pending read domains for the batch buffer to COMMAND */
3876 batch_obj = object_list[args->buffer_count-1];
Chris Wilson5f26a2c2009-06-06 09:45:58 +01003877 if (batch_obj->pending_write_domain) {
3878 DRM_ERROR("Attempting to use self-modifying batch buffer\n");
3879 ret = -EINVAL;
3880 goto err;
3881 }
3882 batch_obj->pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
Eric Anholt673a3942008-07-30 12:06:12 -07003883
Chris Wilson83d60792009-06-06 09:45:57 +01003884 /* Sanity check the batch buffer, prior to moving objects */
3885 exec_offset = exec_list[args->buffer_count - 1].offset;
3886 ret = i915_gem_check_execbuffer (args, exec_offset);
3887 if (ret != 0) {
3888 DRM_ERROR("execbuf with invalid offset/length\n");
3889 goto err;
3890 }
3891
Eric Anholt673a3942008-07-30 12:06:12 -07003892 i915_verify_inactive(dev, __FILE__, __LINE__);
3893
Keith Packard646f0f62008-11-20 23:23:03 -08003894 /* Zero the global flush/invalidate flags. These
3895 * will be modified as new domains are computed
3896 * for each object
3897 */
3898 dev->invalidate_domains = 0;
3899 dev->flush_domains = 0;
3900
Eric Anholt673a3942008-07-30 12:06:12 -07003901 for (i = 0; i < args->buffer_count; i++) {
3902 struct drm_gem_object *obj = object_list[i];
Eric Anholt673a3942008-07-30 12:06:12 -07003903
Keith Packard646f0f62008-11-20 23:23:03 -08003904 /* Compute new gpu domains and update invalidate/flush */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003905 i915_gem_object_set_to_gpu_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003906 }
3907
3908 i915_verify_inactive(dev, __FILE__, __LINE__);
3909
Keith Packard646f0f62008-11-20 23:23:03 -08003910 if (dev->invalidate_domains | dev->flush_domains) {
3911#if WATCH_EXEC
3912 DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
3913 __func__,
3914 dev->invalidate_domains,
3915 dev->flush_domains);
3916#endif
3917 i915_gem_flush(dev,
3918 dev->invalidate_domains,
3919 dev->flush_domains);
Daniel Vetter99fcb762010-02-07 16:20:18 +01003920 if (dev->flush_domains & I915_GEM_GPU_DOMAINS)
Eric Anholtb9624422009-06-03 07:27:35 +00003921 (void)i915_add_request(dev, file_priv,
3922 dev->flush_domains);
Keith Packard646f0f62008-11-20 23:23:03 -08003923 }
Eric Anholt673a3942008-07-30 12:06:12 -07003924
Eric Anholtefbeed92009-02-19 14:54:51 -08003925 for (i = 0; i < args->buffer_count; i++) {
3926 struct drm_gem_object *obj = object_list[i];
Daniel Vetter23010e42010-03-08 13:35:02 +01003927 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003928 uint32_t old_write_domain = obj->write_domain;
Eric Anholtefbeed92009-02-19 14:54:51 -08003929
3930 obj->write_domain = obj->pending_write_domain;
Daniel Vetter99fcb762010-02-07 16:20:18 +01003931 if (obj->write_domain)
3932 list_move_tail(&obj_priv->gpu_write_list,
3933 &dev_priv->mm.gpu_write_list);
3934 else
3935 list_del_init(&obj_priv->gpu_write_list);
3936
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003937 trace_i915_gem_object_change_domain(obj,
3938 obj->read_domains,
3939 old_write_domain);
Eric Anholtefbeed92009-02-19 14:54:51 -08003940 }
3941
Eric Anholt673a3942008-07-30 12:06:12 -07003942 i915_verify_inactive(dev, __FILE__, __LINE__);
3943
3944#if WATCH_COHERENCY
3945 for (i = 0; i < args->buffer_count; i++) {
3946 i915_gem_object_check_coherency(object_list[i],
3947 exec_list[i].handle);
3948 }
3949#endif
3950
Eric Anholt673a3942008-07-30 12:06:12 -07003951#if WATCH_EXEC
Ben Gamari6911a9b2009-04-02 11:24:54 -07003952 i915_gem_dump_object(batch_obj,
Eric Anholt673a3942008-07-30 12:06:12 -07003953 args->batch_len,
3954 __func__,
3955 ~0);
3956#endif
3957
Eric Anholt673a3942008-07-30 12:06:12 -07003958 /* Exec the batchbuffer */
Eric Anholt201361a2009-03-11 12:30:04 -07003959 ret = i915_dispatch_gem_execbuffer(dev, args, cliprects, exec_offset);
Eric Anholt673a3942008-07-30 12:06:12 -07003960 if (ret) {
3961 DRM_ERROR("dispatch failed %d\n", ret);
3962 goto err;
3963 }
3964
3965 /*
3966 * Ensure that the commands in the batch buffer are
3967 * finished before the interrupt fires
3968 */
3969 flush_domains = i915_retire_commands(dev);
3970
3971 i915_verify_inactive(dev, __FILE__, __LINE__);
3972
3973 /*
3974 * Get a seqno representing the execution of the current buffer,
3975 * which we can wait on. We would like to mitigate these interrupts,
3976 * likely by only creating seqnos occasionally (so that we have
3977 * *some* interrupts representing completion of buffers that we can
3978 * wait on when trying to clear up gtt space).
3979 */
Eric Anholtb9624422009-06-03 07:27:35 +00003980 seqno = i915_add_request(dev, file_priv, flush_domains);
Eric Anholt673a3942008-07-30 12:06:12 -07003981 BUG_ON(seqno == 0);
Eric Anholt673a3942008-07-30 12:06:12 -07003982 for (i = 0; i < args->buffer_count; i++) {
3983 struct drm_gem_object *obj = object_list[i];
Eric Anholt673a3942008-07-30 12:06:12 -07003984
Eric Anholtce44b0e2008-11-06 16:00:31 -08003985 i915_gem_object_move_to_active(obj, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07003986#if WATCH_LRU
3987 DRM_INFO("%s: move to exec list %p\n", __func__, obj);
3988#endif
3989 }
3990#if WATCH_LRU
3991 i915_dump_lru(dev, __func__);
3992#endif
3993
3994 i915_verify_inactive(dev, __FILE__, __LINE__);
3995
Eric Anholt673a3942008-07-30 12:06:12 -07003996err:
Julia Lawallaad87df2008-12-21 16:28:47 +01003997 for (i = 0; i < pinned; i++)
3998 i915_gem_object_unpin(object_list[i]);
Eric Anholt673a3942008-07-30 12:06:12 -07003999
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05004000 for (i = 0; i < args->buffer_count; i++) {
4001 if (object_list[i]) {
Daniel Vetter23010e42010-03-08 13:35:02 +01004002 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05004003 obj_priv->in_execbuffer = false;
4004 }
Julia Lawallaad87df2008-12-21 16:28:47 +01004005 drm_gem_object_unreference(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05004006 }
Julia Lawallaad87df2008-12-21 16:28:47 +01004007
Eric Anholt673a3942008-07-30 12:06:12 -07004008 mutex_unlock(&dev->struct_mutex);
4009
Chris Wilson93533c22010-01-31 10:40:48 +00004010pre_mutex_err:
Eric Anholt40a5f0d2009-03-12 11:23:52 -07004011 /* Copy the updated relocations out regardless of current error
4012 * state. Failure to update the relocs would mean that the next
4013 * time userland calls execbuf, it would do so with presumed offset
4014 * state that didn't match the actual object state.
4015 */
4016 ret2 = i915_gem_put_relocs_to_user(exec_list, args->buffer_count,
4017 relocs);
4018 if (ret2 != 0) {
4019 DRM_ERROR("Failed to copy relocations back out: %d\n", ret2);
4020
4021 if (ret == 0)
4022 ret = ret2;
4023 }
4024
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07004025 drm_free_large(object_list);
Eric Anholt9a298b22009-03-24 12:23:04 -07004026 kfree(cliprects);
Eric Anholt673a3942008-07-30 12:06:12 -07004027
4028 return ret;
4029}
4030
Jesse Barnes76446ca2009-12-17 22:05:42 -05004031/*
4032 * Legacy execbuffer just creates an exec2 list from the original exec object
4033 * list array and passes it to the real function.
4034 */
4035int
4036i915_gem_execbuffer(struct drm_device *dev, void *data,
4037 struct drm_file *file_priv)
4038{
4039 struct drm_i915_gem_execbuffer *args = data;
4040 struct drm_i915_gem_execbuffer2 exec2;
4041 struct drm_i915_gem_exec_object *exec_list = NULL;
4042 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
4043 int ret, i;
4044
4045#if WATCH_EXEC
4046 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
4047 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
4048#endif
4049
4050 if (args->buffer_count < 1) {
4051 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
4052 return -EINVAL;
4053 }
4054
4055 /* Copy in the exec list from userland */
4056 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
4057 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
4058 if (exec_list == NULL || exec2_list == NULL) {
4059 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
4060 args->buffer_count);
4061 drm_free_large(exec_list);
4062 drm_free_large(exec2_list);
4063 return -ENOMEM;
4064 }
4065 ret = copy_from_user(exec_list,
4066 (struct drm_i915_relocation_entry __user *)
4067 (uintptr_t) args->buffers_ptr,
4068 sizeof(*exec_list) * args->buffer_count);
4069 if (ret != 0) {
4070 DRM_ERROR("copy %d exec entries failed %d\n",
4071 args->buffer_count, ret);
4072 drm_free_large(exec_list);
4073 drm_free_large(exec2_list);
4074 return -EFAULT;
4075 }
4076
4077 for (i = 0; i < args->buffer_count; i++) {
4078 exec2_list[i].handle = exec_list[i].handle;
4079 exec2_list[i].relocation_count = exec_list[i].relocation_count;
4080 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
4081 exec2_list[i].alignment = exec_list[i].alignment;
4082 exec2_list[i].offset = exec_list[i].offset;
4083 if (!IS_I965G(dev))
4084 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
4085 else
4086 exec2_list[i].flags = 0;
4087 }
4088
4089 exec2.buffers_ptr = args->buffers_ptr;
4090 exec2.buffer_count = args->buffer_count;
4091 exec2.batch_start_offset = args->batch_start_offset;
4092 exec2.batch_len = args->batch_len;
4093 exec2.DR1 = args->DR1;
4094 exec2.DR4 = args->DR4;
4095 exec2.num_cliprects = args->num_cliprects;
4096 exec2.cliprects_ptr = args->cliprects_ptr;
4097 exec2.flags = 0;
4098
4099 ret = i915_gem_do_execbuffer(dev, data, file_priv, &exec2, exec2_list);
4100 if (!ret) {
4101 /* Copy the new buffer offsets back to the user's exec list. */
4102 for (i = 0; i < args->buffer_count; i++)
4103 exec_list[i].offset = exec2_list[i].offset;
4104 /* ... and back out to userspace */
4105 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
4106 (uintptr_t) args->buffers_ptr,
4107 exec_list,
4108 sizeof(*exec_list) * args->buffer_count);
4109 if (ret) {
4110 ret = -EFAULT;
4111 DRM_ERROR("failed to copy %d exec entries "
4112 "back to user (%d)\n",
4113 args->buffer_count, ret);
4114 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05004115 }
4116
4117 drm_free_large(exec_list);
4118 drm_free_large(exec2_list);
4119 return ret;
4120}
4121
4122int
4123i915_gem_execbuffer2(struct drm_device *dev, void *data,
4124 struct drm_file *file_priv)
4125{
4126 struct drm_i915_gem_execbuffer2 *args = data;
4127 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
4128 int ret;
4129
4130#if WATCH_EXEC
4131 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
4132 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
4133#endif
4134
4135 if (args->buffer_count < 1) {
4136 DRM_ERROR("execbuf2 with %d buffers\n", args->buffer_count);
4137 return -EINVAL;
4138 }
4139
4140 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
4141 if (exec2_list == NULL) {
4142 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
4143 args->buffer_count);
4144 return -ENOMEM;
4145 }
4146 ret = copy_from_user(exec2_list,
4147 (struct drm_i915_relocation_entry __user *)
4148 (uintptr_t) args->buffers_ptr,
4149 sizeof(*exec2_list) * args->buffer_count);
4150 if (ret != 0) {
4151 DRM_ERROR("copy %d exec entries failed %d\n",
4152 args->buffer_count, ret);
4153 drm_free_large(exec2_list);
4154 return -EFAULT;
4155 }
4156
4157 ret = i915_gem_do_execbuffer(dev, data, file_priv, args, exec2_list);
4158 if (!ret) {
4159 /* Copy the new buffer offsets back to the user's exec list. */
4160 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
4161 (uintptr_t) args->buffers_ptr,
4162 exec2_list,
4163 sizeof(*exec2_list) * args->buffer_count);
4164 if (ret) {
4165 ret = -EFAULT;
4166 DRM_ERROR("failed to copy %d exec entries "
4167 "back to user (%d)\n",
4168 args->buffer_count, ret);
4169 }
4170 }
4171
4172 drm_free_large(exec2_list);
4173 return ret;
4174}
4175
Eric Anholt673a3942008-07-30 12:06:12 -07004176int
4177i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment)
4178{
4179 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01004180 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004181 int ret;
4182
4183 i915_verify_inactive(dev, __FILE__, __LINE__);
4184 if (obj_priv->gtt_space == NULL) {
4185 ret = i915_gem_object_bind_to_gtt(obj, alignment);
Chris Wilson97311292009-09-21 00:22:34 +01004186 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07004187 return ret;
Chris Wilson22c344e2009-02-11 14:26:45 +00004188 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05004189
Eric Anholt673a3942008-07-30 12:06:12 -07004190 obj_priv->pin_count++;
4191
4192 /* If the object is not active and not pending a flush,
4193 * remove it from the inactive list
4194 */
4195 if (obj_priv->pin_count == 1) {
4196 atomic_inc(&dev->pin_count);
4197 atomic_add(obj->size, &dev->pin_memory);
4198 if (!obj_priv->active &&
Chris Wilson21d509e2009-06-06 09:46:02 +01004199 (obj->write_domain & I915_GEM_GPU_DOMAINS) == 0 &&
Eric Anholt673a3942008-07-30 12:06:12 -07004200 !list_empty(&obj_priv->list))
4201 list_del_init(&obj_priv->list);
4202 }
4203 i915_verify_inactive(dev, __FILE__, __LINE__);
4204
4205 return 0;
4206}
4207
4208void
4209i915_gem_object_unpin(struct drm_gem_object *obj)
4210{
4211 struct drm_device *dev = obj->dev;
4212 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01004213 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004214
4215 i915_verify_inactive(dev, __FILE__, __LINE__);
4216 obj_priv->pin_count--;
4217 BUG_ON(obj_priv->pin_count < 0);
4218 BUG_ON(obj_priv->gtt_space == NULL);
4219
4220 /* If the object is no longer pinned, and is
4221 * neither active nor being flushed, then stick it on
4222 * the inactive list
4223 */
4224 if (obj_priv->pin_count == 0) {
4225 if (!obj_priv->active &&
Chris Wilson21d509e2009-06-06 09:46:02 +01004226 (obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
Eric Anholt673a3942008-07-30 12:06:12 -07004227 list_move_tail(&obj_priv->list,
4228 &dev_priv->mm.inactive_list);
4229 atomic_dec(&dev->pin_count);
4230 atomic_sub(obj->size, &dev->pin_memory);
4231 }
4232 i915_verify_inactive(dev, __FILE__, __LINE__);
4233}
4234
4235int
4236i915_gem_pin_ioctl(struct drm_device *dev, void *data,
4237 struct drm_file *file_priv)
4238{
4239 struct drm_i915_gem_pin *args = data;
4240 struct drm_gem_object *obj;
4241 struct drm_i915_gem_object *obj_priv;
4242 int ret;
4243
4244 mutex_lock(&dev->struct_mutex);
4245
4246 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4247 if (obj == NULL) {
4248 DRM_ERROR("Bad handle in i915_gem_pin_ioctl(): %d\n",
4249 args->handle);
4250 mutex_unlock(&dev->struct_mutex);
4251 return -EBADF;
4252 }
Daniel Vetter23010e42010-03-08 13:35:02 +01004253 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004254
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004255 if (obj_priv->madv != I915_MADV_WILLNEED) {
4256 DRM_ERROR("Attempting to pin a purgeable buffer\n");
Chris Wilson3ef94da2009-09-14 16:50:29 +01004257 drm_gem_object_unreference(obj);
4258 mutex_unlock(&dev->struct_mutex);
4259 return -EINVAL;
4260 }
4261
Jesse Barnes79e53942008-11-07 14:24:08 -08004262 if (obj_priv->pin_filp != NULL && obj_priv->pin_filp != file_priv) {
4263 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
4264 args->handle);
Chris Wilson96dec612009-02-08 19:08:04 +00004265 drm_gem_object_unreference(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004266 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08004267 return -EINVAL;
4268 }
4269
4270 obj_priv->user_pin_count++;
4271 obj_priv->pin_filp = file_priv;
4272 if (obj_priv->user_pin_count == 1) {
4273 ret = i915_gem_object_pin(obj, args->alignment);
4274 if (ret != 0) {
4275 drm_gem_object_unreference(obj);
4276 mutex_unlock(&dev->struct_mutex);
4277 return ret;
4278 }
Eric Anholt673a3942008-07-30 12:06:12 -07004279 }
4280
4281 /* XXX - flush the CPU caches for pinned objects
4282 * as the X server doesn't manage domains yet
4283 */
Eric Anholte47c68e2008-11-14 13:35:19 -08004284 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004285 args->offset = obj_priv->gtt_offset;
4286 drm_gem_object_unreference(obj);
4287 mutex_unlock(&dev->struct_mutex);
4288
4289 return 0;
4290}
4291
4292int
4293i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
4294 struct drm_file *file_priv)
4295{
4296 struct drm_i915_gem_pin *args = data;
4297 struct drm_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08004298 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07004299
4300 mutex_lock(&dev->struct_mutex);
4301
4302 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4303 if (obj == NULL) {
4304 DRM_ERROR("Bad handle in i915_gem_unpin_ioctl(): %d\n",
4305 args->handle);
4306 mutex_unlock(&dev->struct_mutex);
4307 return -EBADF;
4308 }
4309
Daniel Vetter23010e42010-03-08 13:35:02 +01004310 obj_priv = to_intel_bo(obj);
Jesse Barnes79e53942008-11-07 14:24:08 -08004311 if (obj_priv->pin_filp != file_priv) {
4312 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
4313 args->handle);
4314 drm_gem_object_unreference(obj);
4315 mutex_unlock(&dev->struct_mutex);
4316 return -EINVAL;
4317 }
4318 obj_priv->user_pin_count--;
4319 if (obj_priv->user_pin_count == 0) {
4320 obj_priv->pin_filp = NULL;
4321 i915_gem_object_unpin(obj);
4322 }
Eric Anholt673a3942008-07-30 12:06:12 -07004323
4324 drm_gem_object_unreference(obj);
4325 mutex_unlock(&dev->struct_mutex);
4326 return 0;
4327}
4328
4329int
4330i915_gem_busy_ioctl(struct drm_device *dev, void *data,
4331 struct drm_file *file_priv)
4332{
4333 struct drm_i915_gem_busy *args = data;
4334 struct drm_gem_object *obj;
4335 struct drm_i915_gem_object *obj_priv;
4336
Eric Anholt673a3942008-07-30 12:06:12 -07004337 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4338 if (obj == NULL) {
4339 DRM_ERROR("Bad handle in i915_gem_busy_ioctl(): %d\n",
4340 args->handle);
Eric Anholt673a3942008-07-30 12:06:12 -07004341 return -EBADF;
4342 }
4343
Chris Wilsonb1ce7862009-06-06 09:46:00 +01004344 mutex_lock(&dev->struct_mutex);
Eric Anholtf21289b2009-02-18 09:44:56 -08004345 /* Update the active list for the hardware's current position.
4346 * Otherwise this only updates on a delayed timer or when irqs are
4347 * actually unmasked, and our working set ends up being larger than
4348 * required.
4349 */
4350 i915_gem_retire_requests(dev);
4351
Daniel Vetter23010e42010-03-08 13:35:02 +01004352 obj_priv = to_intel_bo(obj);
Eric Anholtc4de0a52008-12-14 19:05:04 -08004353 /* Don't count being on the flushing list against the object being
4354 * done. Otherwise, a buffer left on the flushing list but not getting
4355 * flushed (because nobody's flushing that domain) won't ever return
4356 * unbusy and get reused by libdrm's bo cache. The other expected
4357 * consumer of this interface, OpenGL's occlusion queries, also specs
4358 * that the objects get unbusy "eventually" without any interference.
4359 */
4360 args->busy = obj_priv->active && obj_priv->last_rendering_seqno != 0;
Eric Anholt673a3942008-07-30 12:06:12 -07004361
4362 drm_gem_object_unreference(obj);
4363 mutex_unlock(&dev->struct_mutex);
4364 return 0;
4365}
4366
4367int
4368i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4369 struct drm_file *file_priv)
4370{
4371 return i915_gem_ring_throttle(dev, file_priv);
4372}
4373
Chris Wilson3ef94da2009-09-14 16:50:29 +01004374int
4375i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4376 struct drm_file *file_priv)
4377{
4378 struct drm_i915_gem_madvise *args = data;
4379 struct drm_gem_object *obj;
4380 struct drm_i915_gem_object *obj_priv;
4381
4382 switch (args->madv) {
4383 case I915_MADV_DONTNEED:
4384 case I915_MADV_WILLNEED:
4385 break;
4386 default:
4387 return -EINVAL;
4388 }
4389
4390 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4391 if (obj == NULL) {
4392 DRM_ERROR("Bad handle in i915_gem_madvise_ioctl(): %d\n",
4393 args->handle);
4394 return -EBADF;
4395 }
4396
4397 mutex_lock(&dev->struct_mutex);
Daniel Vetter23010e42010-03-08 13:35:02 +01004398 obj_priv = to_intel_bo(obj);
Chris Wilson3ef94da2009-09-14 16:50:29 +01004399
4400 if (obj_priv->pin_count) {
4401 drm_gem_object_unreference(obj);
4402 mutex_unlock(&dev->struct_mutex);
4403
4404 DRM_ERROR("Attempted i915_gem_madvise_ioctl() on a pinned object\n");
4405 return -EINVAL;
4406 }
4407
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004408 if (obj_priv->madv != __I915_MADV_PURGED)
4409 obj_priv->madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004410
Chris Wilson2d7ef392009-09-20 23:13:10 +01004411 /* if the object is no longer bound, discard its backing storage */
4412 if (i915_gem_object_is_purgeable(obj_priv) &&
4413 obj_priv->gtt_space == NULL)
4414 i915_gem_object_truncate(obj);
4415
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004416 args->retained = obj_priv->madv != __I915_MADV_PURGED;
4417
Chris Wilson3ef94da2009-09-14 16:50:29 +01004418 drm_gem_object_unreference(obj);
4419 mutex_unlock(&dev->struct_mutex);
4420
4421 return 0;
4422}
4423
Daniel Vetterac52bc52010-04-09 19:05:06 +00004424struct drm_gem_object * i915_gem_alloc_object(struct drm_device *dev,
4425 size_t size)
4426{
Daniel Vetterc397b902010-04-09 19:05:07 +00004427 struct drm_i915_gem_object *obj;
4428
4429 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
4430 if (obj == NULL)
4431 return NULL;
4432
4433 if (drm_gem_object_init(dev, &obj->base, size) != 0) {
4434 kfree(obj);
4435 return NULL;
4436 }
4437
4438 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4439 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4440
4441 obj->agp_type = AGP_USER_MEMORY;
4442
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);
4447 INIT_LIST_HEAD(&obj->fence_list);
4448 obj->madv = I915_MADV_WILLNEED;
4449
4450 trace_i915_gem_object_create(&obj->base);
4451
4452 return &obj->base;
Daniel Vetterac52bc52010-04-09 19:05:06 +00004453}
4454
Eric Anholt673a3942008-07-30 12:06:12 -07004455int i915_gem_init_object(struct drm_gem_object *obj)
4456{
Daniel Vetterc397b902010-04-09 19:05:07 +00004457 BUG();
Jesse Barnesde151cf2008-11-12 10:03:55 -08004458
Eric Anholt673a3942008-07-30 12:06:12 -07004459 return 0;
4460}
4461
4462void i915_gem_free_object(struct drm_gem_object *obj)
4463{
Jesse Barnesde151cf2008-11-12 10:03:55 -08004464 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01004465 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004466
Chris Wilson1c5d22f2009-08-25 11:15:50 +01004467 trace_i915_gem_object_destroy(obj);
4468
Eric Anholt673a3942008-07-30 12:06:12 -07004469 while (obj_priv->pin_count > 0)
4470 i915_gem_object_unpin(obj);
4471
Dave Airlie71acb5e2008-12-30 20:31:46 +10004472 if (obj_priv->phys_obj)
4473 i915_gem_detach_phys_object(dev, obj);
4474
Eric Anholt673a3942008-07-30 12:06:12 -07004475 i915_gem_object_unbind(obj);
4476
Chris Wilson7e616152009-09-10 08:53:04 +01004477 if (obj_priv->mmap_offset)
4478 i915_gem_free_mmap_offset(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08004479
Daniel Vetterc397b902010-04-09 19:05:07 +00004480 drm_gem_object_release(obj);
4481
Eric Anholt9a298b22009-03-24 12:23:04 -07004482 kfree(obj_priv->page_cpu_valid);
Eric Anholt280b7132009-03-12 16:56:27 -07004483 kfree(obj_priv->bit_17);
Daniel Vetterc397b902010-04-09 19:05:07 +00004484 kfree(obj_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07004485}
4486
Chris Wilsonab5ee572009-09-20 19:25:47 +01004487/** Unbinds all inactive objects. */
Eric Anholt673a3942008-07-30 12:06:12 -07004488static int
Chris Wilsonab5ee572009-09-20 19:25:47 +01004489i915_gem_evict_from_inactive_list(struct drm_device *dev)
Eric Anholt673a3942008-07-30 12:06:12 -07004490{
Chris Wilsonab5ee572009-09-20 19:25:47 +01004491 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07004492
Chris Wilsonab5ee572009-09-20 19:25:47 +01004493 while (!list_empty(&dev_priv->mm.inactive_list)) {
4494 struct drm_gem_object *obj;
4495 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004496
Daniel Vettera8089e82010-04-09 19:05:09 +00004497 obj = &list_first_entry(&dev_priv->mm.inactive_list,
4498 struct drm_i915_gem_object,
4499 list)->base;
Eric Anholt673a3942008-07-30 12:06:12 -07004500
4501 ret = i915_gem_object_unbind(obj);
4502 if (ret != 0) {
Chris Wilsonab5ee572009-09-20 19:25:47 +01004503 DRM_ERROR("Error unbinding object: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07004504 return ret;
4505 }
4506 }
4507
Eric Anholt673a3942008-07-30 12:06:12 -07004508 return 0;
4509}
4510
Jesse Barnes5669fca2009-02-17 15:13:31 -08004511int
Eric Anholt673a3942008-07-30 12:06:12 -07004512i915_gem_idle(struct drm_device *dev)
4513{
4514 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson29105cc2010-01-07 10:39:13 +00004515 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004516
Keith Packard6dbe2772008-10-14 21:41:13 -07004517 mutex_lock(&dev->struct_mutex);
4518
4519 if (dev_priv->mm.suspended || dev_priv->ring.ring_obj == NULL) {
4520 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004521 return 0;
Keith Packard6dbe2772008-10-14 21:41:13 -07004522 }
Eric Anholt673a3942008-07-30 12:06:12 -07004523
Chris Wilson29105cc2010-01-07 10:39:13 +00004524 ret = i915_gpu_idle(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004525 if (ret) {
4526 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004527 return ret;
Keith Packard6dbe2772008-10-14 21:41:13 -07004528 }
Eric Anholt673a3942008-07-30 12:06:12 -07004529
Chris Wilson29105cc2010-01-07 10:39:13 +00004530 /* Under UMS, be paranoid and evict. */
4531 if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
4532 ret = i915_gem_evict_from_inactive_list(dev);
4533 if (ret) {
4534 mutex_unlock(&dev->struct_mutex);
4535 return ret;
4536 }
4537 }
4538
4539 /* Hack! Don't let anybody do execbuf while we don't control the chip.
4540 * We need to replace this with a semaphore, or something.
4541 * And not confound mm.suspended!
4542 */
4543 dev_priv->mm.suspended = 1;
4544 del_timer(&dev_priv->hangcheck_timer);
4545
4546 i915_kernel_lost_context(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004547 i915_gem_cleanup_ringbuffer(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00004548
Keith Packard6dbe2772008-10-14 21:41:13 -07004549 mutex_unlock(&dev->struct_mutex);
4550
Chris Wilson29105cc2010-01-07 10:39:13 +00004551 /* Cancel the retire work handler, which should be idle now. */
4552 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
4553
Eric Anholt673a3942008-07-30 12:06:12 -07004554 return 0;
4555}
4556
4557static int
4558i915_gem_init_hws(struct drm_device *dev)
4559{
4560 drm_i915_private_t *dev_priv = dev->dev_private;
4561 struct drm_gem_object *obj;
4562 struct drm_i915_gem_object *obj_priv;
4563 int ret;
4564
4565 /* If we need a physical address for the status page, it's already
4566 * initialized at driver load time.
4567 */
4568 if (!I915_NEED_GFX_HWS(dev))
4569 return 0;
4570
Daniel Vetterac52bc52010-04-09 19:05:06 +00004571 obj = i915_gem_alloc_object(dev, 4096);
Eric Anholt673a3942008-07-30 12:06:12 -07004572 if (obj == NULL) {
4573 DRM_ERROR("Failed to allocate status page\n");
4574 return -ENOMEM;
4575 }
Daniel Vetter23010e42010-03-08 13:35:02 +01004576 obj_priv = to_intel_bo(obj);
Keith Packardba1eb1d2008-10-14 19:55:10 -07004577 obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
Eric Anholt673a3942008-07-30 12:06:12 -07004578
4579 ret = i915_gem_object_pin(obj, 4096);
4580 if (ret != 0) {
4581 drm_gem_object_unreference(obj);
4582 return ret;
4583 }
4584
4585 dev_priv->status_gfx_addr = obj_priv->gtt_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07004586
Eric Anholt856fa192009-03-19 14:10:50 -07004587 dev_priv->hw_status_page = kmap(obj_priv->pages[0]);
Keith Packardba1eb1d2008-10-14 19:55:10 -07004588 if (dev_priv->hw_status_page == NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -07004589 DRM_ERROR("Failed to map status page.\n");
4590 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
Chris Wilson3eb2ee72009-02-11 14:26:34 +00004591 i915_gem_object_unpin(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004592 drm_gem_object_unreference(obj);
4593 return -EINVAL;
4594 }
4595 dev_priv->hws_obj = obj;
Eric Anholt673a3942008-07-30 12:06:12 -07004596 memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
Eric Anholtf6e450a2009-11-02 12:08:22 -08004597 if (IS_GEN6(dev)) {
4598 I915_WRITE(HWS_PGA_GEN6, dev_priv->status_gfx_addr);
4599 I915_READ(HWS_PGA_GEN6); /* posting read */
4600 } else {
4601 I915_WRITE(HWS_PGA, dev_priv->status_gfx_addr);
4602 I915_READ(HWS_PGA); /* posting read */
4603 }
Zhao Yakui44d98a62009-10-09 11:39:40 +08004604 DRM_DEBUG_DRIVER("hws offset: 0x%08x\n", dev_priv->status_gfx_addr);
Eric Anholt673a3942008-07-30 12:06:12 -07004605
4606 return 0;
4607}
4608
Chris Wilson85a7bb92009-02-11 14:52:44 +00004609static void
4610i915_gem_cleanup_hws(struct drm_device *dev)
4611{
4612 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonbab2d1f2009-02-20 17:52:20 +00004613 struct drm_gem_object *obj;
4614 struct drm_i915_gem_object *obj_priv;
Chris Wilson85a7bb92009-02-11 14:52:44 +00004615
4616 if (dev_priv->hws_obj == NULL)
4617 return;
4618
Chris Wilsonbab2d1f2009-02-20 17:52:20 +00004619 obj = dev_priv->hws_obj;
Daniel Vetter23010e42010-03-08 13:35:02 +01004620 obj_priv = to_intel_bo(obj);
Chris Wilsonbab2d1f2009-02-20 17:52:20 +00004621
Eric Anholt856fa192009-03-19 14:10:50 -07004622 kunmap(obj_priv->pages[0]);
Chris Wilson85a7bb92009-02-11 14:52:44 +00004623 i915_gem_object_unpin(obj);
4624 drm_gem_object_unreference(obj);
4625 dev_priv->hws_obj = NULL;
Chris Wilsonbab2d1f2009-02-20 17:52:20 +00004626
Chris Wilson85a7bb92009-02-11 14:52:44 +00004627 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
4628 dev_priv->hw_status_page = NULL;
4629
4630 /* Write high address into HWS_PGA when disabling. */
4631 I915_WRITE(HWS_PGA, 0x1ffff000);
4632}
4633
Jesse Barnes79e53942008-11-07 14:24:08 -08004634int
Eric Anholt673a3942008-07-30 12:06:12 -07004635i915_gem_init_ringbuffer(struct drm_device *dev)
4636{
4637 drm_i915_private_t *dev_priv = dev->dev_private;
4638 struct drm_gem_object *obj;
4639 struct drm_i915_gem_object *obj_priv;
Jesse Barnes79e53942008-11-07 14:24:08 -08004640 drm_i915_ring_buffer_t *ring = &dev_priv->ring;
Eric Anholt673a3942008-07-30 12:06:12 -07004641 int ret;
Keith Packard50aa2532008-10-14 17:20:35 -07004642 u32 head;
Eric Anholt673a3942008-07-30 12:06:12 -07004643
4644 ret = i915_gem_init_hws(dev);
4645 if (ret != 0)
4646 return ret;
4647
Daniel Vetterac52bc52010-04-09 19:05:06 +00004648 obj = i915_gem_alloc_object(dev, 128 * 1024);
Eric Anholt673a3942008-07-30 12:06:12 -07004649 if (obj == NULL) {
4650 DRM_ERROR("Failed to allocate ringbuffer\n");
Chris Wilson85a7bb92009-02-11 14:52:44 +00004651 i915_gem_cleanup_hws(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004652 return -ENOMEM;
4653 }
Daniel Vetter23010e42010-03-08 13:35:02 +01004654 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004655
4656 ret = i915_gem_object_pin(obj, 4096);
4657 if (ret != 0) {
4658 drm_gem_object_unreference(obj);
Chris Wilson85a7bb92009-02-11 14:52:44 +00004659 i915_gem_cleanup_hws(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004660 return ret;
4661 }
4662
4663 /* Set up the kernel mapping for the ring. */
Jesse Barnes79e53942008-11-07 14:24:08 -08004664 ring->Size = obj->size;
Eric Anholt673a3942008-07-30 12:06:12 -07004665
Jesse Barnes79e53942008-11-07 14:24:08 -08004666 ring->map.offset = dev->agp->base + obj_priv->gtt_offset;
4667 ring->map.size = obj->size;
4668 ring->map.type = 0;
4669 ring->map.flags = 0;
4670 ring->map.mtrr = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07004671
Jesse Barnes79e53942008-11-07 14:24:08 -08004672 drm_core_ioremap_wc(&ring->map, dev);
4673 if (ring->map.handle == NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -07004674 DRM_ERROR("Failed to map ringbuffer.\n");
4675 memset(&dev_priv->ring, 0, sizeof(dev_priv->ring));
Chris Wilson47ed1852009-02-11 14:26:33 +00004676 i915_gem_object_unpin(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004677 drm_gem_object_unreference(obj);
Chris Wilson85a7bb92009-02-11 14:52:44 +00004678 i915_gem_cleanup_hws(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004679 return -EINVAL;
4680 }
Jesse Barnes79e53942008-11-07 14:24:08 -08004681 ring->ring_obj = obj;
4682 ring->virtual_start = ring->map.handle;
Eric Anholt673a3942008-07-30 12:06:12 -07004683
4684 /* Stop the ring if it's running. */
4685 I915_WRITE(PRB0_CTL, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004686 I915_WRITE(PRB0_TAIL, 0);
Keith Packard50aa2532008-10-14 17:20:35 -07004687 I915_WRITE(PRB0_HEAD, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004688
4689 /* Initialize the ring. */
4690 I915_WRITE(PRB0_START, obj_priv->gtt_offset);
Keith Packard50aa2532008-10-14 17:20:35 -07004691 head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
4692
4693 /* G45 ring initialization fails to reset head to zero */
4694 if (head != 0) {
4695 DRM_ERROR("Ring head not reset to zero "
4696 "ctl %08x head %08x tail %08x start %08x\n",
4697 I915_READ(PRB0_CTL),
4698 I915_READ(PRB0_HEAD),
4699 I915_READ(PRB0_TAIL),
4700 I915_READ(PRB0_START));
4701 I915_WRITE(PRB0_HEAD, 0);
4702
4703 DRM_ERROR("Ring head forced to zero "
4704 "ctl %08x head %08x tail %08x start %08x\n",
4705 I915_READ(PRB0_CTL),
4706 I915_READ(PRB0_HEAD),
4707 I915_READ(PRB0_TAIL),
4708 I915_READ(PRB0_START));
4709 }
4710
Eric Anholt673a3942008-07-30 12:06:12 -07004711 I915_WRITE(PRB0_CTL,
4712 ((obj->size - 4096) & RING_NR_PAGES) |
4713 RING_NO_REPORT |
4714 RING_VALID);
4715
Keith Packard50aa2532008-10-14 17:20:35 -07004716 head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
4717
4718 /* If the head is still not zero, the ring is dead */
4719 if (head != 0) {
4720 DRM_ERROR("Ring initialization failed "
4721 "ctl %08x head %08x tail %08x start %08x\n",
4722 I915_READ(PRB0_CTL),
4723 I915_READ(PRB0_HEAD),
4724 I915_READ(PRB0_TAIL),
4725 I915_READ(PRB0_START));
4726 return -EIO;
4727 }
4728
Eric Anholt673a3942008-07-30 12:06:12 -07004729 /* Update our cache of the ring state */
Jesse Barnes79e53942008-11-07 14:24:08 -08004730 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4731 i915_kernel_lost_context(dev);
4732 else {
4733 ring->head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
4734 ring->tail = I915_READ(PRB0_TAIL) & TAIL_ADDR;
4735 ring->space = ring->head - (ring->tail + 8);
4736 if (ring->space < 0)
4737 ring->space += ring->Size;
4738 }
Eric Anholt673a3942008-07-30 12:06:12 -07004739
Eric Anholt71cf39b2010-03-08 23:41:55 -08004740 if (IS_I9XX(dev) && !IS_GEN3(dev)) {
4741 I915_WRITE(MI_MODE,
4742 (VS_TIMER_DISPATCH) << 16 | VS_TIMER_DISPATCH);
4743 }
4744
Eric Anholt673a3942008-07-30 12:06:12 -07004745 return 0;
4746}
4747
Jesse Barnes79e53942008-11-07 14:24:08 -08004748void
Eric Anholt673a3942008-07-30 12:06:12 -07004749i915_gem_cleanup_ringbuffer(struct drm_device *dev)
4750{
4751 drm_i915_private_t *dev_priv = dev->dev_private;
4752
4753 if (dev_priv->ring.ring_obj == NULL)
4754 return;
4755
4756 drm_core_ioremapfree(&dev_priv->ring.map, dev);
4757
4758 i915_gem_object_unpin(dev_priv->ring.ring_obj);
4759 drm_gem_object_unreference(dev_priv->ring.ring_obj);
4760 dev_priv->ring.ring_obj = NULL;
4761 memset(&dev_priv->ring, 0, sizeof(dev_priv->ring));
4762
Chris Wilson85a7bb92009-02-11 14:52:44 +00004763 i915_gem_cleanup_hws(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004764}
4765
4766int
4767i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
4768 struct drm_file *file_priv)
4769{
4770 drm_i915_private_t *dev_priv = dev->dev_private;
4771 int ret;
4772
Jesse Barnes79e53942008-11-07 14:24:08 -08004773 if (drm_core_check_feature(dev, DRIVER_MODESET))
4774 return 0;
4775
Ben Gamariba1234d2009-09-14 17:48:47 -04004776 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07004777 DRM_ERROR("Reenabling wedged hardware, good luck\n");
Ben Gamariba1234d2009-09-14 17:48:47 -04004778 atomic_set(&dev_priv->mm.wedged, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004779 }
4780
Eric Anholt673a3942008-07-30 12:06:12 -07004781 mutex_lock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004782 dev_priv->mm.suspended = 0;
4783
4784 ret = i915_gem_init_ringbuffer(dev);
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004785 if (ret != 0) {
4786 mutex_unlock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004787 return ret;
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004788 }
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004789
Carl Worth5e118f42009-03-20 11:54:25 -07004790 spin_lock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07004791 BUG_ON(!list_empty(&dev_priv->mm.active_list));
Carl Worth5e118f42009-03-20 11:54:25 -07004792 spin_unlock(&dev_priv->mm.active_list_lock);
4793
Eric Anholt673a3942008-07-30 12:06:12 -07004794 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
4795 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
4796 BUG_ON(!list_empty(&dev_priv->mm.request_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004797 mutex_unlock(&dev->struct_mutex);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004798
4799 drm_irq_install(dev);
4800
Eric Anholt673a3942008-07-30 12:06:12 -07004801 return 0;
4802}
4803
4804int
4805i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
4806 struct drm_file *file_priv)
4807{
Jesse Barnes79e53942008-11-07 14:24:08 -08004808 if (drm_core_check_feature(dev, DRIVER_MODESET))
4809 return 0;
4810
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004811 drm_irq_uninstall(dev);
Linus Torvaldse6890f62009-09-08 17:09:24 -07004812 return i915_gem_idle(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004813}
4814
4815void
4816i915_gem_lastclose(struct drm_device *dev)
4817{
4818 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004819
Eric Anholte806b492009-01-22 09:56:58 -08004820 if (drm_core_check_feature(dev, DRIVER_MODESET))
4821 return;
4822
Keith Packard6dbe2772008-10-14 21:41:13 -07004823 ret = i915_gem_idle(dev);
4824 if (ret)
4825 DRM_ERROR("failed to idle hardware: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07004826}
4827
4828void
4829i915_gem_load(struct drm_device *dev)
4830{
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004831 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07004832 drm_i915_private_t *dev_priv = dev->dev_private;
4833
Carl Worth5e118f42009-03-20 11:54:25 -07004834 spin_lock_init(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07004835 INIT_LIST_HEAD(&dev_priv->mm.active_list);
4836 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
Daniel Vetter99fcb762010-02-07 16:20:18 +01004837 INIT_LIST_HEAD(&dev_priv->mm.gpu_write_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004838 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
4839 INIT_LIST_HEAD(&dev_priv->mm.request_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07004840 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004841 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
4842 i915_gem_retire_work_handler);
Eric Anholt673a3942008-07-30 12:06:12 -07004843 dev_priv->mm.next_gem_seqno = 1;
4844
Chris Wilson31169712009-09-14 16:50:28 +01004845 spin_lock(&shrink_list_lock);
4846 list_add(&dev_priv->mm.shrink_list, &shrink_list);
4847 spin_unlock(&shrink_list_lock);
4848
Jesse Barnesde151cf2008-11-12 10:03:55 -08004849 /* Old X drivers will take 0-2 for front, back, depth buffers */
Eric Anholtb397c832010-01-26 09:43:10 -08004850 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4851 dev_priv->fence_reg_start = 3;
Jesse Barnesde151cf2008-11-12 10:03:55 -08004852
Jesse Barnes0f973f22009-01-26 17:10:45 -08004853 if (IS_I965G(dev) || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08004854 dev_priv->num_fence_regs = 16;
4855 else
4856 dev_priv->num_fence_regs = 8;
4857
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004858 /* Initialize fence registers to zero */
4859 if (IS_I965G(dev)) {
4860 for (i = 0; i < 16; i++)
4861 I915_WRITE64(FENCE_REG_965_0 + (i * 8), 0);
4862 } else {
4863 for (i = 0; i < 8; i++)
4864 I915_WRITE(FENCE_REG_830_0 + (i * 4), 0);
4865 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4866 for (i = 0; i < 8; i++)
4867 I915_WRITE(FENCE_REG_945_8 + (i * 4), 0);
4868 }
Eric Anholt673a3942008-07-30 12:06:12 -07004869 i915_gem_detect_bit_6_swizzle(dev);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05004870 init_waitqueue_head(&dev_priv->pending_flip_queue);
Eric Anholt673a3942008-07-30 12:06:12 -07004871}
Dave Airlie71acb5e2008-12-30 20:31:46 +10004872
4873/*
4874 * Create a physically contiguous memory object for this object
4875 * e.g. for cursor + overlay regs
4876 */
4877int i915_gem_init_phys_object(struct drm_device *dev,
4878 int id, int size)
4879{
4880 drm_i915_private_t *dev_priv = dev->dev_private;
4881 struct drm_i915_gem_phys_object *phys_obj;
4882 int ret;
4883
4884 if (dev_priv->mm.phys_objs[id - 1] || !size)
4885 return 0;
4886
Eric Anholt9a298b22009-03-24 12:23:04 -07004887 phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004888 if (!phys_obj)
4889 return -ENOMEM;
4890
4891 phys_obj->id = id;
4892
Zhenyu Wange6be8d92010-01-05 11:25:05 +08004893 phys_obj->handle = drm_pci_alloc(dev, size, 0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004894 if (!phys_obj->handle) {
4895 ret = -ENOMEM;
4896 goto kfree_obj;
4897 }
4898#ifdef CONFIG_X86
4899 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4900#endif
4901
4902 dev_priv->mm.phys_objs[id - 1] = phys_obj;
4903
4904 return 0;
4905kfree_obj:
Eric Anholt9a298b22009-03-24 12:23:04 -07004906 kfree(phys_obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004907 return ret;
4908}
4909
4910void i915_gem_free_phys_object(struct drm_device *dev, int id)
4911{
4912 drm_i915_private_t *dev_priv = dev->dev_private;
4913 struct drm_i915_gem_phys_object *phys_obj;
4914
4915 if (!dev_priv->mm.phys_objs[id - 1])
4916 return;
4917
4918 phys_obj = dev_priv->mm.phys_objs[id - 1];
4919 if (phys_obj->cur_obj) {
4920 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
4921 }
4922
4923#ifdef CONFIG_X86
4924 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4925#endif
4926 drm_pci_free(dev, phys_obj->handle);
4927 kfree(phys_obj);
4928 dev_priv->mm.phys_objs[id - 1] = NULL;
4929}
4930
4931void i915_gem_free_all_phys_object(struct drm_device *dev)
4932{
4933 int i;
4934
Dave Airlie260883c2009-01-22 17:58:49 +10004935 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004936 i915_gem_free_phys_object(dev, i);
4937}
4938
4939void i915_gem_detach_phys_object(struct drm_device *dev,
4940 struct drm_gem_object *obj)
4941{
4942 struct drm_i915_gem_object *obj_priv;
4943 int i;
4944 int ret;
4945 int page_count;
4946
Daniel Vetter23010e42010-03-08 13:35:02 +01004947 obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004948 if (!obj_priv->phys_obj)
4949 return;
4950
Chris Wilson4bdadb92010-01-27 13:36:32 +00004951 ret = i915_gem_object_get_pages(obj, 0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004952 if (ret)
4953 goto out;
4954
4955 page_count = obj->size / PAGE_SIZE;
4956
4957 for (i = 0; i < page_count; i++) {
Eric Anholt856fa192009-03-19 14:10:50 -07004958 char *dst = kmap_atomic(obj_priv->pages[i], KM_USER0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004959 char *src = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4960
4961 memcpy(dst, src, PAGE_SIZE);
4962 kunmap_atomic(dst, KM_USER0);
4963 }
Eric Anholt856fa192009-03-19 14:10:50 -07004964 drm_clflush_pages(obj_priv->pages, page_count);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004965 drm_agp_chipset_flush(dev);
Chris Wilsond78b47b2009-06-17 21:52:49 +01004966
4967 i915_gem_object_put_pages(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004968out:
4969 obj_priv->phys_obj->cur_obj = NULL;
4970 obj_priv->phys_obj = NULL;
4971}
4972
4973int
4974i915_gem_attach_phys_object(struct drm_device *dev,
4975 struct drm_gem_object *obj, int id)
4976{
4977 drm_i915_private_t *dev_priv = dev->dev_private;
4978 struct drm_i915_gem_object *obj_priv;
4979 int ret = 0;
4980 int page_count;
4981 int i;
4982
4983 if (id > I915_MAX_PHYS_OBJECT)
4984 return -EINVAL;
4985
Daniel Vetter23010e42010-03-08 13:35:02 +01004986 obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004987
4988 if (obj_priv->phys_obj) {
4989 if (obj_priv->phys_obj->id == id)
4990 return 0;
4991 i915_gem_detach_phys_object(dev, obj);
4992 }
4993
4994
4995 /* create a new object */
4996 if (!dev_priv->mm.phys_objs[id - 1]) {
4997 ret = i915_gem_init_phys_object(dev, id,
4998 obj->size);
4999 if (ret) {
Linus Torvaldsaeb565d2009-01-26 10:01:53 -08005000 DRM_ERROR("failed to init phys object %d size: %zu\n", id, obj->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10005001 goto out;
5002 }
5003 }
5004
5005 /* bind to the object */
5006 obj_priv->phys_obj = dev_priv->mm.phys_objs[id - 1];
5007 obj_priv->phys_obj->cur_obj = obj;
5008
Chris Wilson4bdadb92010-01-27 13:36:32 +00005009 ret = i915_gem_object_get_pages(obj, 0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10005010 if (ret) {
5011 DRM_ERROR("failed to get page list\n");
5012 goto out;
5013 }
5014
5015 page_count = obj->size / PAGE_SIZE;
5016
5017 for (i = 0; i < page_count; i++) {
Eric Anholt856fa192009-03-19 14:10:50 -07005018 char *src = kmap_atomic(obj_priv->pages[i], KM_USER0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10005019 char *dst = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
5020
5021 memcpy(dst, src, PAGE_SIZE);
5022 kunmap_atomic(src, KM_USER0);
5023 }
5024
Chris Wilsond78b47b2009-06-17 21:52:49 +01005025 i915_gem_object_put_pages(obj);
5026
Dave Airlie71acb5e2008-12-30 20:31:46 +10005027 return 0;
5028out:
5029 return ret;
5030}
5031
5032static int
5033i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
5034 struct drm_i915_gem_pwrite *args,
5035 struct drm_file *file_priv)
5036{
Daniel Vetter23010e42010-03-08 13:35:02 +01005037 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10005038 void *obj_addr;
5039 int ret;
5040 char __user *user_data;
5041
5042 user_data = (char __user *) (uintptr_t) args->data_ptr;
5043 obj_addr = obj_priv->phys_obj->handle->vaddr + args->offset;
5044
Zhao Yakui44d98a62009-10-09 11:39:40 +08005045 DRM_DEBUG_DRIVER("obj_addr %p, %lld\n", obj_addr, args->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10005046 ret = copy_from_user(obj_addr, user_data, args->size);
5047 if (ret)
5048 return -EFAULT;
5049
5050 drm_agp_chipset_flush(dev);
5051 return 0;
5052}
Eric Anholtb9624422009-06-03 07:27:35 +00005053
5054void i915_gem_release(struct drm_device * dev, struct drm_file *file_priv)
5055{
5056 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
5057
5058 /* Clean up our request list when the client is going away, so that
5059 * later retire_requests won't dereference our soon-to-be-gone
5060 * file_priv.
5061 */
5062 mutex_lock(&dev->struct_mutex);
5063 while (!list_empty(&i915_file_priv->mm.request_list))
5064 list_del_init(i915_file_priv->mm.request_list.next);
5065 mutex_unlock(&dev->struct_mutex);
5066}
Chris Wilson31169712009-09-14 16:50:28 +01005067
Chris Wilson31169712009-09-14 16:50:28 +01005068static int
5069i915_gem_shrink(int nr_to_scan, gfp_t gfp_mask)
5070{
5071 drm_i915_private_t *dev_priv, *next_dev;
5072 struct drm_i915_gem_object *obj_priv, *next_obj;
5073 int cnt = 0;
5074 int would_deadlock = 1;
5075
5076 /* "fast-path" to count number of available objects */
5077 if (nr_to_scan == 0) {
5078 spin_lock(&shrink_list_lock);
5079 list_for_each_entry(dev_priv, &shrink_list, mm.shrink_list) {
5080 struct drm_device *dev = dev_priv->dev;
5081
5082 if (mutex_trylock(&dev->struct_mutex)) {
5083 list_for_each_entry(obj_priv,
5084 &dev_priv->mm.inactive_list,
5085 list)
5086 cnt++;
5087 mutex_unlock(&dev->struct_mutex);
5088 }
5089 }
5090 spin_unlock(&shrink_list_lock);
5091
5092 return (cnt / 100) * sysctl_vfs_cache_pressure;
5093 }
5094
5095 spin_lock(&shrink_list_lock);
5096
5097 /* first scan for clean buffers */
5098 list_for_each_entry_safe(dev_priv, next_dev,
5099 &shrink_list, mm.shrink_list) {
5100 struct drm_device *dev = dev_priv->dev;
5101
5102 if (! mutex_trylock(&dev->struct_mutex))
5103 continue;
5104
5105 spin_unlock(&shrink_list_lock);
5106
5107 i915_gem_retire_requests(dev);
5108
5109 list_for_each_entry_safe(obj_priv, next_obj,
5110 &dev_priv->mm.inactive_list,
5111 list) {
5112 if (i915_gem_object_is_purgeable(obj_priv)) {
Daniel Vettera8089e82010-04-09 19:05:09 +00005113 i915_gem_object_unbind(&obj_priv->base);
Chris Wilson31169712009-09-14 16:50:28 +01005114 if (--nr_to_scan <= 0)
5115 break;
5116 }
5117 }
5118
5119 spin_lock(&shrink_list_lock);
5120 mutex_unlock(&dev->struct_mutex);
5121
Chris Wilson963b4832009-09-20 23:03:54 +01005122 would_deadlock = 0;
5123
Chris Wilson31169712009-09-14 16:50:28 +01005124 if (nr_to_scan <= 0)
5125 break;
5126 }
5127
5128 /* second pass, evict/count anything still on the inactive list */
5129 list_for_each_entry_safe(dev_priv, next_dev,
5130 &shrink_list, mm.shrink_list) {
5131 struct drm_device *dev = dev_priv->dev;
5132
5133 if (! mutex_trylock(&dev->struct_mutex))
5134 continue;
5135
5136 spin_unlock(&shrink_list_lock);
5137
5138 list_for_each_entry_safe(obj_priv, next_obj,
5139 &dev_priv->mm.inactive_list,
5140 list) {
5141 if (nr_to_scan > 0) {
Daniel Vettera8089e82010-04-09 19:05:09 +00005142 i915_gem_object_unbind(&obj_priv->base);
Chris Wilson31169712009-09-14 16:50:28 +01005143 nr_to_scan--;
5144 } else
5145 cnt++;
5146 }
5147
5148 spin_lock(&shrink_list_lock);
5149 mutex_unlock(&dev->struct_mutex);
5150
5151 would_deadlock = 0;
5152 }
5153
5154 spin_unlock(&shrink_list_lock);
5155
5156 if (would_deadlock)
5157 return -1;
5158 else if (cnt > 0)
5159 return (cnt / 100) * sysctl_vfs_cache_pressure;
5160 else
5161 return 0;
5162}
5163
5164static struct shrinker shrinker = {
5165 .shrink = i915_gem_shrink,
5166 .seeks = DEFAULT_SEEKS,
5167};
5168
5169__init void
5170i915_gem_shrinker_init(void)
5171{
5172 register_shrinker(&shrinker);
5173}
5174
5175__exit void
5176i915_gem_shrinker_exit(void)
5177{
5178 unregister_shrinker(&shrinker);
5179}