blob: 9185f098822d5c9d08379e11a9af5ed5ca3e81d3 [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>
Zhenyu Wangf8f235e2010-08-27 11:08:57 +080037#include <linux/intel-gtt.h>
Eric Anholt673a3942008-07-30 12:06:12 -070038
Daniel Vetter0108a3e2010-08-07 11:01:21 +010039static uint32_t i915_gem_get_gtt_alignment(struct drm_gem_object *obj);
Daniel Vetterba3d8d72010-02-11 22:37:04 +010040
41static int i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj,
42 bool pipelined);
Eric Anholte47c68e2008-11-14 13:35:19 -080043static void i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj);
44static void i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj);
Eric Anholte47c68e2008-11-14 13:35:19 -080045static int i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj,
46 int write);
47static int i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
48 uint64_t offset,
49 uint64_t size);
50static void i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj);
Chris Wilson2cf34d72010-09-14 13:03:28 +010051static int i915_gem_object_wait_rendering(struct drm_gem_object *obj,
52 bool interruptible);
Jesse Barnesde151cf2008-11-12 10:03:55 -080053static int i915_gem_object_bind_to_gtt(struct drm_gem_object *obj,
54 unsigned alignment);
Jesse Barnesde151cf2008-11-12 10:03:55 -080055static void i915_gem_clear_fence_reg(struct drm_gem_object *obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +100056static int i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
57 struct drm_i915_gem_pwrite *args,
58 struct drm_file *file_priv);
Chris Wilsonbe726152010-07-23 23:18:50 +010059static void i915_gem_free_object_tail(struct drm_gem_object *obj);
Eric Anholt673a3942008-07-30 12:06:12 -070060
Chris Wilson31169712009-09-14 16:50:28 +010061static LIST_HEAD(shrink_list);
62static DEFINE_SPINLOCK(shrink_list_lock);
63
Chris Wilson7d1c4802010-08-07 21:45:03 +010064static inline bool
65i915_gem_object_is_inactive(struct drm_i915_gem_object *obj_priv)
66{
67 return obj_priv->gtt_space &&
68 !obj_priv->active &&
69 obj_priv->pin_count == 0;
70}
71
Jesse Barnes79e53942008-11-07 14:24:08 -080072int i915_gem_do_init(struct drm_device *dev, unsigned long start,
73 unsigned long end)
74{
75 drm_i915_private_t *dev_priv = dev->dev_private;
76
77 if (start >= end ||
78 (start & (PAGE_SIZE - 1)) != 0 ||
79 (end & (PAGE_SIZE - 1)) != 0) {
80 return -EINVAL;
81 }
82
83 drm_mm_init(&dev_priv->mm.gtt_space, start,
84 end - start);
85
86 dev->gtt_total = (uint32_t) (end - start);
87
88 return 0;
89}
Keith Packard6dbe2772008-10-14 21:41:13 -070090
Eric Anholt673a3942008-07-30 12:06:12 -070091int
92i915_gem_init_ioctl(struct drm_device *dev, void *data,
93 struct drm_file *file_priv)
94{
Eric Anholt673a3942008-07-30 12:06:12 -070095 struct drm_i915_gem_init *args = data;
Jesse Barnes79e53942008-11-07 14:24:08 -080096 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -070097
98 mutex_lock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080099 ret = i915_gem_do_init(dev, args->gtt_start, args->gtt_end);
Eric Anholt673a3942008-07-30 12:06:12 -0700100 mutex_unlock(&dev->struct_mutex);
101
Jesse Barnes79e53942008-11-07 14:24:08 -0800102 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700103}
104
Eric Anholt5a125c32008-10-22 21:40:13 -0700105int
106i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
107 struct drm_file *file_priv)
108{
Eric Anholt5a125c32008-10-22 21:40:13 -0700109 struct drm_i915_gem_get_aperture *args = data;
Eric Anholt5a125c32008-10-22 21:40:13 -0700110
111 if (!(dev->driver->driver_features & DRIVER_GEM))
112 return -ENODEV;
113
114 args->aper_size = dev->gtt_total;
Keith Packard2678d9d2008-11-20 22:54:54 -0800115 args->aper_available_size = (args->aper_size -
116 atomic_read(&dev->pin_memory));
Eric Anholt5a125c32008-10-22 21:40:13 -0700117
118 return 0;
119}
120
Eric Anholt673a3942008-07-30 12:06:12 -0700121
122/**
123 * Creates a new mm object and returns a handle to it.
124 */
125int
126i915_gem_create_ioctl(struct drm_device *dev, void *data,
127 struct drm_file *file_priv)
128{
129 struct drm_i915_gem_create *args = data;
130 struct drm_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300131 int ret;
132 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700133
134 args->size = roundup(args->size, PAGE_SIZE);
135
136 /* Allocate the new object */
Daniel Vetterac52bc52010-04-09 19:05:06 +0000137 obj = i915_gem_alloc_object(dev, args->size);
Eric Anholt673a3942008-07-30 12:06:12 -0700138 if (obj == NULL)
139 return -ENOMEM;
140
141 ret = drm_gem_handle_create(file_priv, obj, &handle);
Chris Wilson1dfd9752010-09-06 14:44:14 +0100142 if (ret) {
143 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700144 return ret;
Chris Wilson1dfd9752010-09-06 14:44:14 +0100145 }
146
147 /* Sink the floating reference from kref_init(handlecount) */
148 drm_gem_object_handle_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700149
150 args->handle = handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700151 return 0;
152}
153
Eric Anholt40123c12009-03-09 13:42:30 -0700154static inline int
Eric Anholteb014592009-03-10 11:44:52 -0700155fast_shmem_read(struct page **pages,
156 loff_t page_base, int page_offset,
157 char __user *data,
158 int length)
159{
160 char __iomem *vaddr;
Florian Mickler2bc43b52009-04-06 22:55:41 +0200161 int unwritten;
Eric Anholteb014592009-03-10 11:44:52 -0700162
163 vaddr = kmap_atomic(pages[page_base >> PAGE_SHIFT], KM_USER0);
164 if (vaddr == NULL)
165 return -ENOMEM;
Florian Mickler2bc43b52009-04-06 22:55:41 +0200166 unwritten = __copy_to_user_inatomic(data, vaddr + page_offset, length);
Eric Anholteb014592009-03-10 11:44:52 -0700167 kunmap_atomic(vaddr, KM_USER0);
168
Florian Mickler2bc43b52009-04-06 22:55:41 +0200169 if (unwritten)
170 return -EFAULT;
171
172 return 0;
Eric Anholteb014592009-03-10 11:44:52 -0700173}
174
Eric Anholt280b7132009-03-12 16:56:27 -0700175static int i915_gem_object_needs_bit17_swizzle(struct drm_gem_object *obj)
176{
177 drm_i915_private_t *dev_priv = obj->dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +0100178 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt280b7132009-03-12 16:56:27 -0700179
180 return dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_9_10_17 &&
181 obj_priv->tiling_mode != I915_TILING_NONE;
182}
183
Chris Wilson99a03df2010-05-27 14:15:34 +0100184static inline void
Eric Anholt40123c12009-03-09 13:42:30 -0700185slow_shmem_copy(struct page *dst_page,
186 int dst_offset,
187 struct page *src_page,
188 int src_offset,
189 int length)
190{
191 char *dst_vaddr, *src_vaddr;
192
Chris Wilson99a03df2010-05-27 14:15:34 +0100193 dst_vaddr = kmap(dst_page);
194 src_vaddr = kmap(src_page);
Eric Anholt40123c12009-03-09 13:42:30 -0700195
196 memcpy(dst_vaddr + dst_offset, src_vaddr + src_offset, length);
197
Chris Wilson99a03df2010-05-27 14:15:34 +0100198 kunmap(src_page);
199 kunmap(dst_page);
Eric Anholt40123c12009-03-09 13:42:30 -0700200}
201
Chris Wilson99a03df2010-05-27 14:15:34 +0100202static inline void
Eric Anholt280b7132009-03-12 16:56:27 -0700203slow_shmem_bit17_copy(struct page *gpu_page,
204 int gpu_offset,
205 struct page *cpu_page,
206 int cpu_offset,
207 int length,
208 int is_read)
209{
210 char *gpu_vaddr, *cpu_vaddr;
211
212 /* Use the unswizzled path if this page isn't affected. */
213 if ((page_to_phys(gpu_page) & (1 << 17)) == 0) {
214 if (is_read)
215 return slow_shmem_copy(cpu_page, cpu_offset,
216 gpu_page, gpu_offset, length);
217 else
218 return slow_shmem_copy(gpu_page, gpu_offset,
219 cpu_page, cpu_offset, length);
220 }
221
Chris Wilson99a03df2010-05-27 14:15:34 +0100222 gpu_vaddr = kmap(gpu_page);
223 cpu_vaddr = kmap(cpu_page);
Eric Anholt280b7132009-03-12 16:56:27 -0700224
225 /* Copy the data, XORing A6 with A17 (1). The user already knows he's
226 * XORing with the other bits (A9 for Y, A9 and A10 for X)
227 */
228 while (length > 0) {
229 int cacheline_end = ALIGN(gpu_offset + 1, 64);
230 int this_length = min(cacheline_end - gpu_offset, length);
231 int swizzled_gpu_offset = gpu_offset ^ 64;
232
233 if (is_read) {
234 memcpy(cpu_vaddr + cpu_offset,
235 gpu_vaddr + swizzled_gpu_offset,
236 this_length);
237 } else {
238 memcpy(gpu_vaddr + swizzled_gpu_offset,
239 cpu_vaddr + cpu_offset,
240 this_length);
241 }
242 cpu_offset += this_length;
243 gpu_offset += this_length;
244 length -= this_length;
245 }
246
Chris Wilson99a03df2010-05-27 14:15:34 +0100247 kunmap(cpu_page);
248 kunmap(gpu_page);
Eric Anholt280b7132009-03-12 16:56:27 -0700249}
250
Eric Anholt673a3942008-07-30 12:06:12 -0700251/**
Eric Anholteb014592009-03-10 11:44:52 -0700252 * This is the fast shmem pread path, which attempts to copy_from_user directly
253 * from the backing pages of the object to the user's address space. On a
254 * fault, it fails so we can fall back to i915_gem_shmem_pwrite_slow().
255 */
256static int
257i915_gem_shmem_pread_fast(struct drm_device *dev, struct drm_gem_object *obj,
258 struct drm_i915_gem_pread *args,
259 struct drm_file *file_priv)
260{
Daniel Vetter23010e42010-03-08 13:35:02 +0100261 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700262 ssize_t remain;
263 loff_t offset, page_base;
264 char __user *user_data;
265 int page_offset, page_length;
266 int ret;
267
268 user_data = (char __user *) (uintptr_t) args->data_ptr;
269 remain = args->size;
270
271 mutex_lock(&dev->struct_mutex);
272
Chris Wilson4bdadb92010-01-27 13:36:32 +0000273 ret = i915_gem_object_get_pages(obj, 0);
Eric Anholteb014592009-03-10 11:44:52 -0700274 if (ret != 0)
275 goto fail_unlock;
276
277 ret = i915_gem_object_set_cpu_read_domain_range(obj, args->offset,
278 args->size);
279 if (ret != 0)
280 goto fail_put_pages;
281
Daniel Vetter23010e42010-03-08 13:35:02 +0100282 obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700283 offset = args->offset;
284
285 while (remain > 0) {
286 /* Operation in this page
287 *
288 * page_base = page offset within aperture
289 * page_offset = offset within page
290 * page_length = bytes to copy for this page
291 */
292 page_base = (offset & ~(PAGE_SIZE-1));
293 page_offset = offset & (PAGE_SIZE-1);
294 page_length = remain;
295 if ((page_offset + remain) > PAGE_SIZE)
296 page_length = PAGE_SIZE - page_offset;
297
298 ret = fast_shmem_read(obj_priv->pages,
299 page_base, page_offset,
300 user_data, page_length);
301 if (ret)
302 goto fail_put_pages;
303
304 remain -= page_length;
305 user_data += page_length;
306 offset += page_length;
307 }
308
309fail_put_pages:
310 i915_gem_object_put_pages(obj);
311fail_unlock:
312 mutex_unlock(&dev->struct_mutex);
313
314 return ret;
315}
316
Chris Wilson07f73f62009-09-14 16:50:30 +0100317static int
318i915_gem_object_get_pages_or_evict(struct drm_gem_object *obj)
319{
320 int ret;
321
Chris Wilson4bdadb92010-01-27 13:36:32 +0000322 ret = i915_gem_object_get_pages(obj, __GFP_NORETRY | __GFP_NOWARN);
Chris Wilson07f73f62009-09-14 16:50:30 +0100323
324 /* If we've insufficient memory to map in the pages, attempt
325 * to make some space by throwing out some old buffers.
326 */
327 if (ret == -ENOMEM) {
328 struct drm_device *dev = obj->dev;
Chris Wilson07f73f62009-09-14 16:50:30 +0100329
Daniel Vetter0108a3e2010-08-07 11:01:21 +0100330 ret = i915_gem_evict_something(dev, obj->size,
331 i915_gem_get_gtt_alignment(obj));
Chris Wilson07f73f62009-09-14 16:50:30 +0100332 if (ret)
333 return ret;
334
Chris Wilson4bdadb92010-01-27 13:36:32 +0000335 ret = i915_gem_object_get_pages(obj, 0);
Chris Wilson07f73f62009-09-14 16:50:30 +0100336 }
337
338 return ret;
339}
340
Eric Anholteb014592009-03-10 11:44:52 -0700341/**
342 * This is the fallback shmem pread path, which allocates temporary storage
343 * in kernel space to copy_to_user into outside of the struct_mutex, so we
344 * can copy out of the object's backing pages while holding the struct mutex
345 * and not take page faults.
346 */
347static int
348i915_gem_shmem_pread_slow(struct drm_device *dev, struct drm_gem_object *obj,
349 struct drm_i915_gem_pread *args,
350 struct drm_file *file_priv)
351{
Daniel Vetter23010e42010-03-08 13:35:02 +0100352 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700353 struct mm_struct *mm = current->mm;
354 struct page **user_pages;
355 ssize_t remain;
356 loff_t offset, pinned_pages, i;
357 loff_t first_data_page, last_data_page, num_pages;
358 int shmem_page_index, shmem_page_offset;
359 int data_page_index, data_page_offset;
360 int page_length;
361 int ret;
362 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700363 int do_bit17_swizzling;
Eric Anholteb014592009-03-10 11:44:52 -0700364
365 remain = args->size;
366
367 /* Pin the user pages containing the data. We can't fault while
368 * holding the struct mutex, yet we want to hold it while
369 * dereferencing the user data.
370 */
371 first_data_page = data_ptr / PAGE_SIZE;
372 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
373 num_pages = last_data_page - first_data_page + 1;
374
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700375 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
Eric Anholteb014592009-03-10 11:44:52 -0700376 if (user_pages == NULL)
377 return -ENOMEM;
378
379 down_read(&mm->mmap_sem);
380 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
Eric Anholte5e9ecd2009-04-07 16:01:22 -0700381 num_pages, 1, 0, user_pages, NULL);
Eric Anholteb014592009-03-10 11:44:52 -0700382 up_read(&mm->mmap_sem);
383 if (pinned_pages < num_pages) {
384 ret = -EFAULT;
385 goto fail_put_user_pages;
386 }
387
Eric Anholt280b7132009-03-12 16:56:27 -0700388 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
389
Eric Anholteb014592009-03-10 11:44:52 -0700390 mutex_lock(&dev->struct_mutex);
391
Chris Wilson07f73f62009-09-14 16:50:30 +0100392 ret = i915_gem_object_get_pages_or_evict(obj);
393 if (ret)
Eric Anholteb014592009-03-10 11:44:52 -0700394 goto fail_unlock;
395
396 ret = i915_gem_object_set_cpu_read_domain_range(obj, args->offset,
397 args->size);
398 if (ret != 0)
399 goto fail_put_pages;
400
Daniel Vetter23010e42010-03-08 13:35:02 +0100401 obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700402 offset = args->offset;
403
404 while (remain > 0) {
405 /* Operation in this page
406 *
407 * shmem_page_index = page number within shmem file
408 * shmem_page_offset = offset within page in shmem file
409 * data_page_index = page number in get_user_pages return
410 * data_page_offset = offset with data_page_index page.
411 * page_length = bytes to copy for this page
412 */
413 shmem_page_index = offset / PAGE_SIZE;
414 shmem_page_offset = offset & ~PAGE_MASK;
415 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
416 data_page_offset = data_ptr & ~PAGE_MASK;
417
418 page_length = remain;
419 if ((shmem_page_offset + page_length) > PAGE_SIZE)
420 page_length = PAGE_SIZE - shmem_page_offset;
421 if ((data_page_offset + page_length) > PAGE_SIZE)
422 page_length = PAGE_SIZE - data_page_offset;
423
Eric Anholt280b7132009-03-12 16:56:27 -0700424 if (do_bit17_swizzling) {
Chris Wilson99a03df2010-05-27 14:15:34 +0100425 slow_shmem_bit17_copy(obj_priv->pages[shmem_page_index],
Eric Anholt280b7132009-03-12 16:56:27 -0700426 shmem_page_offset,
Chris Wilson99a03df2010-05-27 14:15:34 +0100427 user_pages[data_page_index],
428 data_page_offset,
429 page_length,
430 1);
431 } else {
432 slow_shmem_copy(user_pages[data_page_index],
433 data_page_offset,
434 obj_priv->pages[shmem_page_index],
435 shmem_page_offset,
436 page_length);
Eric Anholt280b7132009-03-12 16:56:27 -0700437 }
Eric Anholteb014592009-03-10 11:44:52 -0700438
439 remain -= page_length;
440 data_ptr += page_length;
441 offset += page_length;
442 }
443
444fail_put_pages:
445 i915_gem_object_put_pages(obj);
446fail_unlock:
447 mutex_unlock(&dev->struct_mutex);
448fail_put_user_pages:
449 for (i = 0; i < pinned_pages; i++) {
450 SetPageDirty(user_pages[i]);
451 page_cache_release(user_pages[i]);
452 }
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700453 drm_free_large(user_pages);
Eric Anholteb014592009-03-10 11:44:52 -0700454
455 return ret;
456}
457
Eric Anholt673a3942008-07-30 12:06:12 -0700458/**
459 * Reads data from the object referenced by handle.
460 *
461 * On error, the contents of *data are undefined.
462 */
463int
464i915_gem_pread_ioctl(struct drm_device *dev, void *data,
465 struct drm_file *file_priv)
466{
467 struct drm_i915_gem_pread *args = data;
468 struct drm_gem_object *obj;
469 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -0700470 int ret;
471
472 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
473 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100474 return -ENOENT;
Daniel Vetter23010e42010-03-08 13:35:02 +0100475 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700476
477 /* Bounds check source.
478 *
479 * XXX: This could use review for overflow issues...
480 */
481 if (args->offset > obj->size || args->size > obj->size ||
482 args->offset + args->size > obj->size) {
Luca Barbieribc9025b2010-02-09 05:49:12 +0000483 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700484 return -EINVAL;
485 }
486
Eric Anholt280b7132009-03-12 16:56:27 -0700487 if (i915_gem_object_needs_bit17_swizzle(obj)) {
Eric Anholteb014592009-03-10 11:44:52 -0700488 ret = i915_gem_shmem_pread_slow(dev, obj, args, file_priv);
Eric Anholt280b7132009-03-12 16:56:27 -0700489 } else {
490 ret = i915_gem_shmem_pread_fast(dev, obj, args, file_priv);
491 if (ret != 0)
492 ret = i915_gem_shmem_pread_slow(dev, obj, args,
493 file_priv);
494 }
Eric Anholt673a3942008-07-30 12:06:12 -0700495
Luca Barbieribc9025b2010-02-09 05:49:12 +0000496 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700497
Eric Anholteb014592009-03-10 11:44:52 -0700498 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700499}
500
Keith Packard0839ccb2008-10-30 19:38:48 -0700501/* This is the fast write path which cannot handle
502 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700503 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700504
Keith Packard0839ccb2008-10-30 19:38:48 -0700505static inline int
506fast_user_write(struct io_mapping *mapping,
507 loff_t page_base, int page_offset,
508 char __user *user_data,
509 int length)
510{
511 char *vaddr_atomic;
512 unsigned long unwritten;
513
Chris Wilsonfca3ec02010-08-04 14:34:24 +0100514 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base, KM_USER0);
Keith Packard0839ccb2008-10-30 19:38:48 -0700515 unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
516 user_data, length);
Chris Wilsonfca3ec02010-08-04 14:34:24 +0100517 io_mapping_unmap_atomic(vaddr_atomic, KM_USER0);
Keith Packard0839ccb2008-10-30 19:38:48 -0700518 if (unwritten)
519 return -EFAULT;
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700520 return 0;
Keith Packard0839ccb2008-10-30 19:38:48 -0700521}
522
523/* Here's the write path which can sleep for
524 * page faults
525 */
526
Chris Wilsonab34c222010-05-27 14:15:35 +0100527static inline void
Eric Anholt3de09aa2009-03-09 09:42:23 -0700528slow_kernel_write(struct io_mapping *mapping,
529 loff_t gtt_base, int gtt_offset,
530 struct page *user_page, int user_offset,
531 int length)
Keith Packard0839ccb2008-10-30 19:38:48 -0700532{
Chris Wilsonab34c222010-05-27 14:15:35 +0100533 char __iomem *dst_vaddr;
534 char *src_vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -0700535
Chris Wilsonab34c222010-05-27 14:15:35 +0100536 dst_vaddr = io_mapping_map_wc(mapping, gtt_base);
537 src_vaddr = kmap(user_page);
538
539 memcpy_toio(dst_vaddr + gtt_offset,
540 src_vaddr + user_offset,
541 length);
542
543 kunmap(user_page);
544 io_mapping_unmap(dst_vaddr);
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700545}
546
Eric Anholt40123c12009-03-09 13:42:30 -0700547static inline int
548fast_shmem_write(struct page **pages,
549 loff_t page_base, int page_offset,
550 char __user *data,
551 int length)
552{
553 char __iomem *vaddr;
Dave Airlied0088772009-03-28 20:29:48 -0400554 unsigned long unwritten;
Eric Anholt40123c12009-03-09 13:42:30 -0700555
556 vaddr = kmap_atomic(pages[page_base >> PAGE_SHIFT], KM_USER0);
557 if (vaddr == NULL)
558 return -ENOMEM;
Dave Airlied0088772009-03-28 20:29:48 -0400559 unwritten = __copy_from_user_inatomic(vaddr + page_offset, data, length);
Eric Anholt40123c12009-03-09 13:42:30 -0700560 kunmap_atomic(vaddr, KM_USER0);
561
Dave Airlied0088772009-03-28 20:29:48 -0400562 if (unwritten)
563 return -EFAULT;
Eric Anholt40123c12009-03-09 13:42:30 -0700564 return 0;
565}
566
Eric Anholt3de09aa2009-03-09 09:42:23 -0700567/**
568 * This is the fast pwrite path, where we copy the data directly from the
569 * user into the GTT, uncached.
570 */
Eric Anholt673a3942008-07-30 12:06:12 -0700571static int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700572i915_gem_gtt_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
573 struct drm_i915_gem_pwrite *args,
574 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700575{
Daniel Vetter23010e42010-03-08 13:35:02 +0100576 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Keith Packard0839ccb2008-10-30 19:38:48 -0700577 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700578 ssize_t remain;
Keith Packard0839ccb2008-10-30 19:38:48 -0700579 loff_t offset, page_base;
Eric Anholt673a3942008-07-30 12:06:12 -0700580 char __user *user_data;
Keith Packard0839ccb2008-10-30 19:38:48 -0700581 int page_offset, page_length;
582 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700583
584 user_data = (char __user *) (uintptr_t) args->data_ptr;
585 remain = args->size;
586 if (!access_ok(VERIFY_READ, user_data, remain))
587 return -EFAULT;
588
589
590 mutex_lock(&dev->struct_mutex);
591 ret = i915_gem_object_pin(obj, 0);
592 if (ret) {
593 mutex_unlock(&dev->struct_mutex);
594 return ret;
595 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800596 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
Eric Anholt673a3942008-07-30 12:06:12 -0700597 if (ret)
598 goto fail;
599
Daniel Vetter23010e42010-03-08 13:35:02 +0100600 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700601 offset = obj_priv->gtt_offset + args->offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700602
603 while (remain > 0) {
604 /* Operation in this page
605 *
Keith Packard0839ccb2008-10-30 19:38:48 -0700606 * page_base = page offset within aperture
607 * page_offset = offset within page
608 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -0700609 */
Keith Packard0839ccb2008-10-30 19:38:48 -0700610 page_base = (offset & ~(PAGE_SIZE-1));
611 page_offset = offset & (PAGE_SIZE-1);
612 page_length = remain;
613 if ((page_offset + remain) > PAGE_SIZE)
614 page_length = PAGE_SIZE - page_offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700615
Keith Packard0839ccb2008-10-30 19:38:48 -0700616 ret = fast_user_write (dev_priv->mm.gtt_mapping, page_base,
617 page_offset, user_data, page_length);
Eric Anholt673a3942008-07-30 12:06:12 -0700618
Keith Packard0839ccb2008-10-30 19:38:48 -0700619 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -0700620 * source page isn't available. Return the error and we'll
621 * retry in the slow path.
Keith Packard0839ccb2008-10-30 19:38:48 -0700622 */
Eric Anholt3de09aa2009-03-09 09:42:23 -0700623 if (ret)
624 goto fail;
Eric Anholt673a3942008-07-30 12:06:12 -0700625
Keith Packard0839ccb2008-10-30 19:38:48 -0700626 remain -= page_length;
627 user_data += page_length;
628 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700629 }
Eric Anholt673a3942008-07-30 12:06:12 -0700630
631fail:
632 i915_gem_object_unpin(obj);
633 mutex_unlock(&dev->struct_mutex);
634
635 return ret;
636}
637
Eric Anholt3de09aa2009-03-09 09:42:23 -0700638/**
639 * This is the fallback GTT pwrite path, which uses get_user_pages to pin
640 * the memory and maps it using kmap_atomic for copying.
641 *
642 * This code resulted in x11perf -rgb10text consuming about 10% more CPU
643 * than using i915_gem_gtt_pwrite_fast on a G45 (32-bit).
644 */
Eric Anholt3043c602008-10-02 12:24:47 -0700645static int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700646i915_gem_gtt_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
647 struct drm_i915_gem_pwrite *args,
648 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700649{
Daniel Vetter23010e42010-03-08 13:35:02 +0100650 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700651 drm_i915_private_t *dev_priv = dev->dev_private;
652 ssize_t remain;
653 loff_t gtt_page_base, offset;
654 loff_t first_data_page, last_data_page, num_pages;
655 loff_t pinned_pages, i;
656 struct page **user_pages;
657 struct mm_struct *mm = current->mm;
658 int gtt_page_offset, data_page_offset, data_page_index, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700659 int ret;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700660 uint64_t data_ptr = args->data_ptr;
661
662 remain = args->size;
663
664 /* Pin the user pages containing the data. We can't fault while
665 * holding the struct mutex, and all of the pwrite implementations
666 * want to hold it while dereferencing the user data.
667 */
668 first_data_page = data_ptr / PAGE_SIZE;
669 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
670 num_pages = last_data_page - first_data_page + 1;
671
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700672 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
Eric Anholt3de09aa2009-03-09 09:42:23 -0700673 if (user_pages == NULL)
674 return -ENOMEM;
675
676 down_read(&mm->mmap_sem);
677 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
678 num_pages, 0, 0, user_pages, NULL);
679 up_read(&mm->mmap_sem);
680 if (pinned_pages < num_pages) {
681 ret = -EFAULT;
682 goto out_unpin_pages;
683 }
684
685 mutex_lock(&dev->struct_mutex);
686 ret = i915_gem_object_pin(obj, 0);
687 if (ret)
688 goto out_unlock;
689
690 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
691 if (ret)
692 goto out_unpin_object;
693
Daniel Vetter23010e42010-03-08 13:35:02 +0100694 obj_priv = to_intel_bo(obj);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700695 offset = obj_priv->gtt_offset + args->offset;
696
697 while (remain > 0) {
698 /* Operation in this page
699 *
700 * gtt_page_base = page offset within aperture
701 * gtt_page_offset = offset within page in aperture
702 * data_page_index = page number in get_user_pages return
703 * data_page_offset = offset with data_page_index page.
704 * page_length = bytes to copy for this page
705 */
706 gtt_page_base = offset & PAGE_MASK;
707 gtt_page_offset = offset & ~PAGE_MASK;
708 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
709 data_page_offset = data_ptr & ~PAGE_MASK;
710
711 page_length = remain;
712 if ((gtt_page_offset + page_length) > PAGE_SIZE)
713 page_length = PAGE_SIZE - gtt_page_offset;
714 if ((data_page_offset + page_length) > PAGE_SIZE)
715 page_length = PAGE_SIZE - data_page_offset;
716
Chris Wilsonab34c222010-05-27 14:15:35 +0100717 slow_kernel_write(dev_priv->mm.gtt_mapping,
718 gtt_page_base, gtt_page_offset,
719 user_pages[data_page_index],
720 data_page_offset,
721 page_length);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700722
723 remain -= page_length;
724 offset += page_length;
725 data_ptr += page_length;
726 }
727
728out_unpin_object:
729 i915_gem_object_unpin(obj);
730out_unlock:
731 mutex_unlock(&dev->struct_mutex);
732out_unpin_pages:
733 for (i = 0; i < pinned_pages; i++)
734 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700735 drm_free_large(user_pages);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700736
737 return ret;
738}
739
Eric Anholt40123c12009-03-09 13:42:30 -0700740/**
741 * This is the fast shmem pwrite path, which attempts to directly
742 * copy_from_user into the kmapped pages backing the object.
743 */
Eric Anholt673a3942008-07-30 12:06:12 -0700744static int
Eric Anholt40123c12009-03-09 13:42:30 -0700745i915_gem_shmem_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
746 struct drm_i915_gem_pwrite *args,
747 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700748{
Daniel Vetter23010e42010-03-08 13:35:02 +0100749 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700750 ssize_t remain;
751 loff_t offset, page_base;
752 char __user *user_data;
753 int page_offset, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700754 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -0700755
756 user_data = (char __user *) (uintptr_t) args->data_ptr;
757 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700758
759 mutex_lock(&dev->struct_mutex);
760
Chris Wilson4bdadb92010-01-27 13:36:32 +0000761 ret = i915_gem_object_get_pages(obj, 0);
Eric Anholt40123c12009-03-09 13:42:30 -0700762 if (ret != 0)
763 goto fail_unlock;
764
Eric Anholte47c68e2008-11-14 13:35:19 -0800765 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Eric Anholt40123c12009-03-09 13:42:30 -0700766 if (ret != 0)
767 goto fail_put_pages;
Eric Anholt673a3942008-07-30 12:06:12 -0700768
Daniel Vetter23010e42010-03-08 13:35:02 +0100769 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700770 offset = args->offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700771 obj_priv->dirty = 1;
Eric Anholt673a3942008-07-30 12:06:12 -0700772
Eric Anholt40123c12009-03-09 13:42:30 -0700773 while (remain > 0) {
774 /* Operation in this page
775 *
776 * page_base = page offset within aperture
777 * page_offset = offset within page
778 * page_length = bytes to copy for this page
779 */
780 page_base = (offset & ~(PAGE_SIZE-1));
781 page_offset = offset & (PAGE_SIZE-1);
782 page_length = remain;
783 if ((page_offset + remain) > PAGE_SIZE)
784 page_length = PAGE_SIZE - page_offset;
785
786 ret = fast_shmem_write(obj_priv->pages,
787 page_base, page_offset,
788 user_data, page_length);
789 if (ret)
790 goto fail_put_pages;
791
792 remain -= page_length;
793 user_data += page_length;
794 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700795 }
796
Eric Anholt40123c12009-03-09 13:42:30 -0700797fail_put_pages:
798 i915_gem_object_put_pages(obj);
799fail_unlock:
Eric Anholt673a3942008-07-30 12:06:12 -0700800 mutex_unlock(&dev->struct_mutex);
801
Eric Anholt40123c12009-03-09 13:42:30 -0700802 return ret;
803}
804
805/**
806 * This is the fallback shmem pwrite path, which uses get_user_pages to pin
807 * the memory and maps it using kmap_atomic for copying.
808 *
809 * This avoids taking mmap_sem for faulting on the user's address while the
810 * struct_mutex is held.
811 */
812static int
813i915_gem_shmem_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
814 struct drm_i915_gem_pwrite *args,
815 struct drm_file *file_priv)
816{
Daniel Vetter23010e42010-03-08 13:35:02 +0100817 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700818 struct mm_struct *mm = current->mm;
819 struct page **user_pages;
820 ssize_t remain;
821 loff_t offset, pinned_pages, i;
822 loff_t first_data_page, last_data_page, num_pages;
823 int shmem_page_index, shmem_page_offset;
824 int data_page_index, data_page_offset;
825 int page_length;
826 int ret;
827 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700828 int do_bit17_swizzling;
Eric Anholt40123c12009-03-09 13:42:30 -0700829
830 remain = args->size;
831
832 /* Pin the user pages containing the data. We can't fault while
833 * holding the struct mutex, and all of the pwrite implementations
834 * want to hold it while dereferencing the user data.
835 */
836 first_data_page = data_ptr / PAGE_SIZE;
837 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
838 num_pages = last_data_page - first_data_page + 1;
839
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700840 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
Eric Anholt40123c12009-03-09 13:42:30 -0700841 if (user_pages == NULL)
842 return -ENOMEM;
843
844 down_read(&mm->mmap_sem);
845 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
846 num_pages, 0, 0, user_pages, NULL);
847 up_read(&mm->mmap_sem);
848 if (pinned_pages < num_pages) {
849 ret = -EFAULT;
850 goto fail_put_user_pages;
851 }
852
Eric Anholt280b7132009-03-12 16:56:27 -0700853 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
854
Eric Anholt40123c12009-03-09 13:42:30 -0700855 mutex_lock(&dev->struct_mutex);
856
Chris Wilson07f73f62009-09-14 16:50:30 +0100857 ret = i915_gem_object_get_pages_or_evict(obj);
858 if (ret)
Eric Anholt40123c12009-03-09 13:42:30 -0700859 goto fail_unlock;
860
861 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
862 if (ret != 0)
863 goto fail_put_pages;
864
Daniel Vetter23010e42010-03-08 13:35:02 +0100865 obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700866 offset = args->offset;
867 obj_priv->dirty = 1;
868
869 while (remain > 0) {
870 /* Operation in this page
871 *
872 * shmem_page_index = page number within shmem file
873 * shmem_page_offset = offset within page in shmem file
874 * data_page_index = page number in get_user_pages return
875 * data_page_offset = offset with data_page_index page.
876 * page_length = bytes to copy for this page
877 */
878 shmem_page_index = offset / PAGE_SIZE;
879 shmem_page_offset = offset & ~PAGE_MASK;
880 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
881 data_page_offset = data_ptr & ~PAGE_MASK;
882
883 page_length = remain;
884 if ((shmem_page_offset + page_length) > PAGE_SIZE)
885 page_length = PAGE_SIZE - shmem_page_offset;
886 if ((data_page_offset + page_length) > PAGE_SIZE)
887 page_length = PAGE_SIZE - data_page_offset;
888
Eric Anholt280b7132009-03-12 16:56:27 -0700889 if (do_bit17_swizzling) {
Chris Wilson99a03df2010-05-27 14:15:34 +0100890 slow_shmem_bit17_copy(obj_priv->pages[shmem_page_index],
Eric Anholt280b7132009-03-12 16:56:27 -0700891 shmem_page_offset,
892 user_pages[data_page_index],
893 data_page_offset,
Chris Wilson99a03df2010-05-27 14:15:34 +0100894 page_length,
895 0);
896 } else {
897 slow_shmem_copy(obj_priv->pages[shmem_page_index],
898 shmem_page_offset,
899 user_pages[data_page_index],
900 data_page_offset,
901 page_length);
Eric Anholt280b7132009-03-12 16:56:27 -0700902 }
Eric Anholt40123c12009-03-09 13:42:30 -0700903
904 remain -= page_length;
905 data_ptr += page_length;
906 offset += page_length;
907 }
908
909fail_put_pages:
910 i915_gem_object_put_pages(obj);
911fail_unlock:
912 mutex_unlock(&dev->struct_mutex);
913fail_put_user_pages:
914 for (i = 0; i < pinned_pages; i++)
915 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700916 drm_free_large(user_pages);
Eric Anholt40123c12009-03-09 13:42:30 -0700917
918 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700919}
920
921/**
922 * Writes data to the object referenced by handle.
923 *
924 * On error, the contents of the buffer that were to be modified are undefined.
925 */
926int
927i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
928 struct drm_file *file_priv)
929{
930 struct drm_i915_gem_pwrite *args = data;
931 struct drm_gem_object *obj;
932 struct drm_i915_gem_object *obj_priv;
933 int ret = 0;
934
935 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
936 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100937 return -ENOENT;
Daniel Vetter23010e42010-03-08 13:35:02 +0100938 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700939
940 /* Bounds check destination.
941 *
942 * XXX: This could use review for overflow issues...
943 */
944 if (args->offset > obj->size || args->size > obj->size ||
945 args->offset + args->size > obj->size) {
Luca Barbieribc9025b2010-02-09 05:49:12 +0000946 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700947 return -EINVAL;
948 }
949
950 /* We can only do the GTT pwrite on untiled buffers, as otherwise
951 * it would end up going through the fenced access, and we'll get
952 * different detiling behavior between reading and writing.
953 * pread/pwrite currently are reading and writing from the CPU
954 * perspective, requiring manual detiling by the client.
955 */
Dave Airlie71acb5e2008-12-30 20:31:46 +1000956 if (obj_priv->phys_obj)
957 ret = i915_gem_phys_pwrite(dev, obj, args, file_priv);
958 else if (obj_priv->tiling_mode == I915_TILING_NONE &&
Chris Wilson9b8c4a02010-05-27 14:21:01 +0100959 dev->gtt_total != 0 &&
960 obj->write_domain != I915_GEM_DOMAIN_CPU) {
Eric Anholt3de09aa2009-03-09 09:42:23 -0700961 ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file_priv);
962 if (ret == -EFAULT) {
963 ret = i915_gem_gtt_pwrite_slow(dev, obj, args,
964 file_priv);
965 }
Eric Anholt280b7132009-03-12 16:56:27 -0700966 } else if (i915_gem_object_needs_bit17_swizzle(obj)) {
967 ret = i915_gem_shmem_pwrite_slow(dev, obj, args, file_priv);
Eric Anholt40123c12009-03-09 13:42:30 -0700968 } else {
969 ret = i915_gem_shmem_pwrite_fast(dev, obj, args, file_priv);
970 if (ret == -EFAULT) {
971 ret = i915_gem_shmem_pwrite_slow(dev, obj, args,
972 file_priv);
973 }
974 }
Eric Anholt673a3942008-07-30 12:06:12 -0700975
976#if WATCH_PWRITE
977 if (ret)
978 DRM_INFO("pwrite failed %d\n", ret);
979#endif
980
Luca Barbieribc9025b2010-02-09 05:49:12 +0000981 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700982
983 return ret;
984}
985
986/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800987 * Called when user space prepares to use an object with the CPU, either
988 * through the mmap ioctl's mapping or a GTT mapping.
Eric Anholt673a3942008-07-30 12:06:12 -0700989 */
990int
991i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
992 struct drm_file *file_priv)
993{
Eric Anholta09ba7f2009-08-29 12:49:51 -0700994 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700995 struct drm_i915_gem_set_domain *args = data;
996 struct drm_gem_object *obj;
Jesse Barnes652c3932009-08-17 13:31:43 -0700997 struct drm_i915_gem_object *obj_priv;
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800998 uint32_t read_domains = args->read_domains;
999 uint32_t write_domain = args->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07001000 int ret;
1001
1002 if (!(dev->driver->driver_features & DRIVER_GEM))
1003 return -ENODEV;
1004
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001005 /* Only handle setting domains to types used by the CPU. */
Chris Wilson21d509e2009-06-06 09:46:02 +01001006 if (write_domain & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001007 return -EINVAL;
1008
Chris Wilson21d509e2009-06-06 09:46:02 +01001009 if (read_domains & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001010 return -EINVAL;
1011
1012 /* Having something in the write domain implies it's in the read
1013 * domain, and only that read domain. Enforce that in the request.
1014 */
1015 if (write_domain != 0 && read_domains != write_domain)
1016 return -EINVAL;
1017
Eric Anholt673a3942008-07-30 12:06:12 -07001018 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1019 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001020 return -ENOENT;
Daniel Vetter23010e42010-03-08 13:35:02 +01001021 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001022
1023 mutex_lock(&dev->struct_mutex);
Jesse Barnes652c3932009-08-17 13:31:43 -07001024
1025 intel_mark_busy(dev, obj);
1026
Eric Anholt673a3942008-07-30 12:06:12 -07001027#if WATCH_BUF
Krzysztof Halasacfd43c02009-06-20 00:31:28 +02001028 DRM_INFO("set_domain_ioctl %p(%zd), %08x %08x\n",
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001029 obj, obj->size, read_domains, write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07001030#endif
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001031 if (read_domains & I915_GEM_DOMAIN_GTT) {
1032 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
Eric Anholt02354392008-11-26 13:58:13 -08001033
Eric Anholta09ba7f2009-08-29 12:49:51 -07001034 /* Update the LRU on the fence for the CPU access that's
1035 * about to occur.
1036 */
1037 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001038 struct drm_i915_fence_reg *reg =
1039 &dev_priv->fence_regs[obj_priv->fence_reg];
1040 list_move_tail(&reg->lru_list,
Eric Anholta09ba7f2009-08-29 12:49:51 -07001041 &dev_priv->mm.fence_list);
1042 }
1043
Eric Anholt02354392008-11-26 13:58:13 -08001044 /* Silently promote "you're not bound, there was nothing to do"
1045 * to success, since the client was just asking us to
1046 * make sure everything was done.
1047 */
1048 if (ret == -EINVAL)
1049 ret = 0;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001050 } else {
Eric Anholte47c68e2008-11-14 13:35:19 -08001051 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001052 }
1053
Chris Wilson7d1c4802010-08-07 21:45:03 +01001054 /* Maintain LRU order of "inactive" objects */
1055 if (ret == 0 && i915_gem_object_is_inactive(obj_priv))
1056 list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
1057
Eric Anholt673a3942008-07-30 12:06:12 -07001058 drm_gem_object_unreference(obj);
1059 mutex_unlock(&dev->struct_mutex);
1060 return ret;
1061}
1062
1063/**
1064 * Called when user space has done writes to this buffer
1065 */
1066int
1067i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
1068 struct drm_file *file_priv)
1069{
1070 struct drm_i915_gem_sw_finish *args = data;
1071 struct drm_gem_object *obj;
1072 struct drm_i915_gem_object *obj_priv;
1073 int ret = 0;
1074
1075 if (!(dev->driver->driver_features & DRIVER_GEM))
1076 return -ENODEV;
1077
1078 mutex_lock(&dev->struct_mutex);
1079 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1080 if (obj == NULL) {
1081 mutex_unlock(&dev->struct_mutex);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001082 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001083 }
1084
1085#if WATCH_BUF
Krzysztof Halasacfd43c02009-06-20 00:31:28 +02001086 DRM_INFO("%s: sw_finish %d (%p %zd)\n",
Eric Anholt673a3942008-07-30 12:06:12 -07001087 __func__, args->handle, obj, obj->size);
1088#endif
Daniel Vetter23010e42010-03-08 13:35:02 +01001089 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001090
1091 /* Pinned buffers may be scanout, so flush the cache */
Eric Anholte47c68e2008-11-14 13:35:19 -08001092 if (obj_priv->pin_count)
1093 i915_gem_object_flush_cpu_write_domain(obj);
1094
Eric Anholt673a3942008-07-30 12:06:12 -07001095 drm_gem_object_unreference(obj);
1096 mutex_unlock(&dev->struct_mutex);
1097 return ret;
1098}
1099
1100/**
1101 * Maps the contents of an object, returning the address it is mapped
1102 * into.
1103 *
1104 * While the mapping holds a reference on the contents of the object, it doesn't
1105 * imply a ref on the object itself.
1106 */
1107int
1108i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
1109 struct drm_file *file_priv)
1110{
1111 struct drm_i915_gem_mmap *args = data;
1112 struct drm_gem_object *obj;
1113 loff_t offset;
1114 unsigned long addr;
1115
1116 if (!(dev->driver->driver_features & DRIVER_GEM))
1117 return -ENODEV;
1118
1119 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1120 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001121 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001122
1123 offset = args->offset;
1124
1125 down_write(&current->mm->mmap_sem);
1126 addr = do_mmap(obj->filp, 0, args->size,
1127 PROT_READ | PROT_WRITE, MAP_SHARED,
1128 args->offset);
1129 up_write(&current->mm->mmap_sem);
Luca Barbieribc9025b2010-02-09 05:49:12 +00001130 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001131 if (IS_ERR((void *)addr))
1132 return addr;
1133
1134 args->addr_ptr = (uint64_t) addr;
1135
1136 return 0;
1137}
1138
Jesse Barnesde151cf2008-11-12 10:03:55 -08001139/**
1140 * i915_gem_fault - fault a page into the GTT
1141 * vma: VMA in question
1142 * vmf: fault info
1143 *
1144 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1145 * from userspace. The fault handler takes care of binding the object to
1146 * the GTT (if needed), allocating and programming a fence register (again,
1147 * only if needed based on whether the old reg is still valid or the object
1148 * is tiled) and inserting a new PTE into the faulting process.
1149 *
1150 * Note that the faulting process may involve evicting existing objects
1151 * from the GTT and/or fence registers to make room. So performance may
1152 * suffer if the GTT working set is large or there are few fence registers
1153 * left.
1154 */
1155int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1156{
1157 struct drm_gem_object *obj = vma->vm_private_data;
1158 struct drm_device *dev = obj->dev;
Chris Wilson7d1c4802010-08-07 21:45:03 +01001159 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001160 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001161 pgoff_t page_offset;
1162 unsigned long pfn;
1163 int ret = 0;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001164 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001165
1166 /* We don't use vmf->pgoff since that has the fake offset */
1167 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1168 PAGE_SHIFT;
1169
1170 /* Now bind it into the GTT if needed */
1171 mutex_lock(&dev->struct_mutex);
1172 if (!obj_priv->gtt_space) {
Chris Wilsone67b8ce2009-09-14 16:50:26 +01001173 ret = i915_gem_object_bind_to_gtt(obj, 0);
Chris Wilsonc7150892009-09-23 00:43:56 +01001174 if (ret)
1175 goto unlock;
Kristian Høgsberg07f4f3e2009-05-27 14:37:28 -04001176
Jesse Barnesde151cf2008-11-12 10:03:55 -08001177 ret = i915_gem_object_set_to_gtt_domain(obj, write);
Chris Wilsonc7150892009-09-23 00:43:56 +01001178 if (ret)
1179 goto unlock;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001180 }
1181
1182 /* Need a new fence register? */
Eric Anholta09ba7f2009-08-29 12:49:51 -07001183 if (obj_priv->tiling_mode != I915_TILING_NONE) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01001184 ret = i915_gem_object_get_fence_reg(obj, true);
Chris Wilsonc7150892009-09-23 00:43:56 +01001185 if (ret)
1186 goto unlock;
Eric Anholtd9ddcb92009-01-27 10:33:49 -08001187 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001188
Chris Wilson7d1c4802010-08-07 21:45:03 +01001189 if (i915_gem_object_is_inactive(obj_priv))
1190 list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
1191
Jesse Barnesde151cf2008-11-12 10:03:55 -08001192 pfn = ((dev->agp->base + obj_priv->gtt_offset) >> PAGE_SHIFT) +
1193 page_offset;
1194
1195 /* Finally, remap it using the new GTT offset */
1196 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
Chris Wilsonc7150892009-09-23 00:43:56 +01001197unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001198 mutex_unlock(&dev->struct_mutex);
1199
1200 switch (ret) {
Chris Wilsonc7150892009-09-23 00:43:56 +01001201 case 0:
1202 case -ERESTARTSYS:
1203 return VM_FAULT_NOPAGE;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001204 case -ENOMEM:
1205 case -EAGAIN:
1206 return VM_FAULT_OOM;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001207 default:
Chris Wilsonc7150892009-09-23 00:43:56 +01001208 return VM_FAULT_SIGBUS;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001209 }
1210}
1211
1212/**
1213 * i915_gem_create_mmap_offset - create a fake mmap offset for an object
1214 * @obj: obj in question
1215 *
1216 * GEM memory mapping works by handing back to userspace a fake mmap offset
1217 * it can use in a subsequent mmap(2) call. The DRM core code then looks
1218 * up the object based on the offset and sets up the various memory mapping
1219 * structures.
1220 *
1221 * This routine allocates and attaches a fake offset for @obj.
1222 */
1223static int
1224i915_gem_create_mmap_offset(struct drm_gem_object *obj)
1225{
1226 struct drm_device *dev = obj->dev;
1227 struct drm_gem_mm *mm = dev->mm_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001228 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001229 struct drm_map_list *list;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +11001230 struct drm_local_map *map;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001231 int ret = 0;
1232
1233 /* Set the object up for mmap'ing */
1234 list = &obj->map_list;
Eric Anholt9a298b22009-03-24 12:23:04 -07001235 list->map = kzalloc(sizeof(struct drm_map_list), GFP_KERNEL);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001236 if (!list->map)
1237 return -ENOMEM;
1238
1239 map = list->map;
1240 map->type = _DRM_GEM;
1241 map->size = obj->size;
1242 map->handle = obj;
1243
1244 /* Get a DRM GEM mmap offset allocated... */
1245 list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
1246 obj->size / PAGE_SIZE, 0, 0);
1247 if (!list->file_offset_node) {
1248 DRM_ERROR("failed to allocate offset for bo %d\n", obj->name);
Chris Wilson9e0ae5342010-09-21 15:05:24 +01001249 ret = -ENOSPC;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001250 goto out_free_list;
1251 }
1252
1253 list->file_offset_node = drm_mm_get_block(list->file_offset_node,
1254 obj->size / PAGE_SIZE, 0);
1255 if (!list->file_offset_node) {
1256 ret = -ENOMEM;
1257 goto out_free_list;
1258 }
1259
1260 list->hash.key = list->file_offset_node->start;
Chris Wilson9e0ae5342010-09-21 15:05:24 +01001261 ret = drm_ht_insert_item(&mm->offset_hash, &list->hash);
1262 if (ret) {
Jesse Barnesde151cf2008-11-12 10:03:55 -08001263 DRM_ERROR("failed to add to map hash\n");
1264 goto out_free_mm;
1265 }
1266
1267 /* By now we should be all set, any drm_mmap request on the offset
1268 * below will get to our mmap & fault handler */
1269 obj_priv->mmap_offset = ((uint64_t) list->hash.key) << PAGE_SHIFT;
1270
1271 return 0;
1272
1273out_free_mm:
1274 drm_mm_put_block(list->file_offset_node);
1275out_free_list:
Eric Anholt9a298b22009-03-24 12:23:04 -07001276 kfree(list->map);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001277
1278 return ret;
1279}
1280
Chris Wilson901782b2009-07-10 08:18:50 +01001281/**
1282 * i915_gem_release_mmap - remove physical page mappings
1283 * @obj: obj in question
1284 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001285 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01001286 * relinquish ownership of the pages back to the system.
1287 *
1288 * It is vital that we remove the page mapping if we have mapped a tiled
1289 * object through the GTT and then lose the fence register due to
1290 * resource pressure. Similarly if the object has been moved out of the
1291 * aperture, than pages mapped into userspace must be revoked. Removing the
1292 * mapping will then trigger a page fault on the next user access, allowing
1293 * fixup by i915_gem_fault().
1294 */
Eric Anholtd05ca302009-07-10 13:02:26 -07001295void
Chris Wilson901782b2009-07-10 08:18:50 +01001296i915_gem_release_mmap(struct drm_gem_object *obj)
1297{
1298 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001299 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson901782b2009-07-10 08:18:50 +01001300
1301 if (dev->dev_mapping)
1302 unmap_mapping_range(dev->dev_mapping,
1303 obj_priv->mmap_offset, obj->size, 1);
1304}
1305
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001306static void
1307i915_gem_free_mmap_offset(struct drm_gem_object *obj)
1308{
1309 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001310 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001311 struct drm_gem_mm *mm = dev->mm_private;
1312 struct drm_map_list *list;
1313
1314 list = &obj->map_list;
1315 drm_ht_remove_item(&mm->offset_hash, &list->hash);
1316
1317 if (list->file_offset_node) {
1318 drm_mm_put_block(list->file_offset_node);
1319 list->file_offset_node = NULL;
1320 }
1321
1322 if (list->map) {
Eric Anholt9a298b22009-03-24 12:23:04 -07001323 kfree(list->map);
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001324 list->map = NULL;
1325 }
1326
1327 obj_priv->mmap_offset = 0;
1328}
1329
Jesse Barnesde151cf2008-11-12 10:03:55 -08001330/**
1331 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1332 * @obj: object to check
1333 *
1334 * Return the required GTT alignment for an object, taking into account
1335 * potential fence register mapping if needed.
1336 */
1337static uint32_t
1338i915_gem_get_gtt_alignment(struct drm_gem_object *obj)
1339{
1340 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001341 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001342 int start, i;
1343
1344 /*
1345 * Minimum alignment is 4k (GTT page size), but might be greater
1346 * if a fence register is needed for the object.
1347 */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001348 if (INTEL_INFO(dev)->gen >= 4 || obj_priv->tiling_mode == I915_TILING_NONE)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001349 return 4096;
1350
1351 /*
1352 * Previous chips need to be aligned to the size of the smallest
1353 * fence register that can contain the object.
1354 */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001355 if (INTEL_INFO(dev)->gen == 3)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001356 start = 1024*1024;
1357 else
1358 start = 512*1024;
1359
1360 for (i = start; i < obj->size; i <<= 1)
1361 ;
1362
1363 return i;
1364}
1365
1366/**
1367 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1368 * @dev: DRM device
1369 * @data: GTT mapping ioctl data
1370 * @file_priv: GEM object info
1371 *
1372 * Simply returns the fake offset to userspace so it can mmap it.
1373 * The mmap call will end up in drm_gem_mmap(), which will set things
1374 * up so we can get faults in the handler above.
1375 *
1376 * The fault handler will take care of binding the object into the GTT
1377 * (since it may have been evicted to make room for something), allocating
1378 * a fence register, and mapping the appropriate aperture address into
1379 * userspace.
1380 */
1381int
1382i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
1383 struct drm_file *file_priv)
1384{
1385 struct drm_i915_gem_mmap_gtt *args = data;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001386 struct drm_gem_object *obj;
1387 struct drm_i915_gem_object *obj_priv;
1388 int ret;
1389
1390 if (!(dev->driver->driver_features & DRIVER_GEM))
1391 return -ENODEV;
1392
1393 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1394 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001395 return -ENOENT;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001396
1397 mutex_lock(&dev->struct_mutex);
1398
Daniel Vetter23010e42010-03-08 13:35:02 +01001399 obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001400
Chris Wilsonab182822009-09-22 18:46:17 +01001401 if (obj_priv->madv != I915_MADV_WILLNEED) {
1402 DRM_ERROR("Attempting to mmap a purgeable buffer\n");
1403 drm_gem_object_unreference(obj);
1404 mutex_unlock(&dev->struct_mutex);
1405 return -EINVAL;
1406 }
1407
1408
Jesse Barnesde151cf2008-11-12 10:03:55 -08001409 if (!obj_priv->mmap_offset) {
1410 ret = i915_gem_create_mmap_offset(obj);
Chris Wilson13af1062009-02-11 14:26:31 +00001411 if (ret) {
1412 drm_gem_object_unreference(obj);
1413 mutex_unlock(&dev->struct_mutex);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001414 return ret;
Chris Wilson13af1062009-02-11 14:26:31 +00001415 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001416 }
1417
1418 args->offset = obj_priv->mmap_offset;
1419
Jesse Barnesde151cf2008-11-12 10:03:55 -08001420 /*
1421 * Pull it into the GTT so that we have a page list (makes the
1422 * initial fault faster and any subsequent flushing possible).
1423 */
1424 if (!obj_priv->agp_mem) {
Chris Wilsone67b8ce2009-09-14 16:50:26 +01001425 ret = i915_gem_object_bind_to_gtt(obj, 0);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001426 if (ret) {
1427 drm_gem_object_unreference(obj);
1428 mutex_unlock(&dev->struct_mutex);
1429 return ret;
1430 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001431 }
1432
1433 drm_gem_object_unreference(obj);
1434 mutex_unlock(&dev->struct_mutex);
1435
1436 return 0;
1437}
1438
Ben Gamari6911a9b2009-04-02 11:24:54 -07001439void
Eric Anholt856fa192009-03-19 14:10:50 -07001440i915_gem_object_put_pages(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07001441{
Daniel Vetter23010e42010-03-08 13:35:02 +01001442 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001443 int page_count = obj->size / PAGE_SIZE;
1444 int i;
1445
Eric Anholt856fa192009-03-19 14:10:50 -07001446 BUG_ON(obj_priv->pages_refcount == 0);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001447 BUG_ON(obj_priv->madv == __I915_MADV_PURGED);
Eric Anholt856fa192009-03-19 14:10:50 -07001448
1449 if (--obj_priv->pages_refcount != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07001450 return;
1451
Eric Anholt280b7132009-03-12 16:56:27 -07001452 if (obj_priv->tiling_mode != I915_TILING_NONE)
1453 i915_gem_object_save_bit_17_swizzle(obj);
1454
Chris Wilson3ef94da2009-09-14 16:50:29 +01001455 if (obj_priv->madv == I915_MADV_DONTNEED)
Chris Wilson13a05fd2009-09-20 23:03:19 +01001456 obj_priv->dirty = 0;
Chris Wilson3ef94da2009-09-14 16:50:29 +01001457
1458 for (i = 0; i < page_count; i++) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01001459 if (obj_priv->dirty)
1460 set_page_dirty(obj_priv->pages[i]);
1461
1462 if (obj_priv->madv == I915_MADV_WILLNEED)
Eric Anholt856fa192009-03-19 14:10:50 -07001463 mark_page_accessed(obj_priv->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001464
1465 page_cache_release(obj_priv->pages[i]);
1466 }
Eric Anholt673a3942008-07-30 12:06:12 -07001467 obj_priv->dirty = 0;
1468
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07001469 drm_free_large(obj_priv->pages);
Eric Anholt856fa192009-03-19 14:10:50 -07001470 obj_priv->pages = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001471}
1472
1473static void
Daniel Vetter617dbe22010-02-11 22:16:02 +01001474i915_gem_object_move_to_active(struct drm_gem_object *obj,
Zou Nan hai852835f2010-05-21 09:08:56 +08001475 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001476{
Chris Wilson5c12a07e2010-09-22 11:22:30 +01001477 struct drm_i915_private *dev_priv = obj->dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001478 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Daniel Vetter617dbe22010-02-11 22:16:02 +01001479
Zou Nan hai852835f2010-05-21 09:08:56 +08001480 BUG_ON(ring == NULL);
1481 obj_priv->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001482
1483 /* Add a reference if we're newly entering the active list. */
1484 if (!obj_priv->active) {
1485 drm_gem_object_reference(obj);
1486 obj_priv->active = 1;
1487 }
Daniel Vettere35a41d2010-02-11 22:13:59 +01001488
Eric Anholt673a3942008-07-30 12:06:12 -07001489 /* Move from whatever list we were on to the tail of execution. */
Zou Nan hai852835f2010-05-21 09:08:56 +08001490 list_move_tail(&obj_priv->list, &ring->active_list);
Chris Wilson5c12a07e2010-09-22 11:22:30 +01001491 obj_priv->last_rendering_seqno = dev_priv->next_seqno;
Eric Anholt673a3942008-07-30 12:06:12 -07001492}
1493
Eric Anholtce44b0e2008-11-06 16:00:31 -08001494static void
1495i915_gem_object_move_to_flushing(struct drm_gem_object *obj)
1496{
1497 struct drm_device *dev = obj->dev;
1498 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001499 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001500
1501 BUG_ON(!obj_priv->active);
1502 list_move_tail(&obj_priv->list, &dev_priv->mm.flushing_list);
1503 obj_priv->last_rendering_seqno = 0;
1504}
Eric Anholt673a3942008-07-30 12:06:12 -07001505
Chris Wilson963b4832009-09-20 23:03:54 +01001506/* Immediately discard the backing storage */
1507static void
1508i915_gem_object_truncate(struct drm_gem_object *obj)
1509{
Daniel Vetter23010e42010-03-08 13:35:02 +01001510 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001511 struct inode *inode;
Chris Wilson963b4832009-09-20 23:03:54 +01001512
Chris Wilsonae9fed62010-08-07 11:01:30 +01001513 /* Our goal here is to return as much of the memory as
1514 * is possible back to the system as we are called from OOM.
1515 * To do this we must instruct the shmfs to drop all of its
1516 * backing pages, *now*. Here we mirror the actions taken
1517 * when by shmem_delete_inode() to release the backing store.
1518 */
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001519 inode = obj->filp->f_path.dentry->d_inode;
Chris Wilsonae9fed62010-08-07 11:01:30 +01001520 truncate_inode_pages(inode->i_mapping, 0);
1521 if (inode->i_op->truncate_range)
1522 inode->i_op->truncate_range(inode, 0, (loff_t)-1);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001523
1524 obj_priv->madv = __I915_MADV_PURGED;
Chris Wilson963b4832009-09-20 23:03:54 +01001525}
1526
1527static inline int
1528i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj_priv)
1529{
1530 return obj_priv->madv == I915_MADV_DONTNEED;
1531}
1532
Eric Anholt673a3942008-07-30 12:06:12 -07001533static void
1534i915_gem_object_move_to_inactive(struct drm_gem_object *obj)
1535{
1536 struct drm_device *dev = obj->dev;
1537 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001538 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001539
1540 i915_verify_inactive(dev, __FILE__, __LINE__);
1541 if (obj_priv->pin_count != 0)
Chris Wilsonf13d3f72010-09-20 17:36:15 +01001542 list_move_tail(&obj_priv->list, &dev_priv->mm.pinned_list);
Eric Anholt673a3942008-07-30 12:06:12 -07001543 else
1544 list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
1545
Daniel Vetter99fcb762010-02-07 16:20:18 +01001546 BUG_ON(!list_empty(&obj_priv->gpu_write_list));
1547
Eric Anholtce44b0e2008-11-06 16:00:31 -08001548 obj_priv->last_rendering_seqno = 0;
Zou Nan hai852835f2010-05-21 09:08:56 +08001549 obj_priv->ring = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001550 if (obj_priv->active) {
1551 obj_priv->active = 0;
1552 drm_gem_object_unreference(obj);
1553 }
1554 i915_verify_inactive(dev, __FILE__, __LINE__);
1555}
1556
Chris Wilson92204342010-09-18 11:02:01 +01001557static void
Daniel Vetter63560392010-02-19 11:51:59 +01001558i915_gem_process_flushing_list(struct drm_device *dev,
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001559 uint32_t flush_domains,
Zou Nan hai852835f2010-05-21 09:08:56 +08001560 struct intel_ring_buffer *ring)
Daniel Vetter63560392010-02-19 11:51:59 +01001561{
1562 drm_i915_private_t *dev_priv = dev->dev_private;
1563 struct drm_i915_gem_object *obj_priv, *next;
1564
1565 list_for_each_entry_safe(obj_priv, next,
1566 &dev_priv->mm.gpu_write_list,
1567 gpu_write_list) {
Daniel Vettera8089e82010-04-09 19:05:09 +00001568 struct drm_gem_object *obj = &obj_priv->base;
Daniel Vetter63560392010-02-19 11:51:59 +01001569
Chris Wilson2b6efaa2010-09-14 17:04:02 +01001570 if (obj->write_domain & flush_domains &&
1571 obj_priv->ring == ring) {
Daniel Vetter63560392010-02-19 11:51:59 +01001572 uint32_t old_write_domain = obj->write_domain;
1573
1574 obj->write_domain = 0;
1575 list_del_init(&obj_priv->gpu_write_list);
Daniel Vetter617dbe22010-02-11 22:16:02 +01001576 i915_gem_object_move_to_active(obj, ring);
Daniel Vetter63560392010-02-19 11:51:59 +01001577
1578 /* update the fence lru list */
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001579 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
1580 struct drm_i915_fence_reg *reg =
1581 &dev_priv->fence_regs[obj_priv->fence_reg];
1582 list_move_tail(&reg->lru_list,
Daniel Vetter63560392010-02-19 11:51:59 +01001583 &dev_priv->mm.fence_list);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001584 }
Daniel Vetter63560392010-02-19 11:51:59 +01001585
1586 trace_i915_gem_object_change_domain(obj,
1587 obj->read_domains,
1588 old_write_domain);
1589 }
1590 }
1591}
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001592
Daniel Vetter5a5a0c62009-09-15 22:57:36 +02001593uint32_t
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001594i915_add_request(struct drm_device *dev,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001595 struct drm_file *file,
Chris Wilson8dc5d142010-08-12 12:36:12 +01001596 struct drm_i915_gem_request *request,
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001597 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001598{
1599 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001600 struct drm_i915_file_private *file_priv = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001601 uint32_t seqno;
1602 int was_empty;
Eric Anholt673a3942008-07-30 12:06:12 -07001603
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001604 if (file != NULL)
1605 file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00001606
Chris Wilson8dc5d142010-08-12 12:36:12 +01001607 if (request == NULL) {
1608 request = kzalloc(sizeof(*request), GFP_KERNEL);
1609 if (request == NULL)
1610 return 0;
1611 }
Eric Anholt673a3942008-07-30 12:06:12 -07001612
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001613 seqno = ring->add_request(dev, ring, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07001614
1615 request->seqno = seqno;
Zou Nan hai852835f2010-05-21 09:08:56 +08001616 request->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001617 request->emitted_jiffies = jiffies;
Zou Nan hai852835f2010-05-21 09:08:56 +08001618 was_empty = list_empty(&ring->request_list);
1619 list_add_tail(&request->list, &ring->request_list);
1620
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001621 if (file_priv) {
1622 mutex_lock(&file_priv->mutex);
1623 request->file_priv = file_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00001624 list_add_tail(&request->client_list,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001625 &file_priv->mm.request_list);
1626 mutex_unlock(&file_priv->mutex);
Eric Anholtb9624422009-06-03 07:27:35 +00001627 }
Eric Anholt673a3942008-07-30 12:06:12 -07001628
Ben Gamarif65d9422009-09-14 17:48:44 -04001629 if (!dev_priv->mm.suspended) {
Chris Wilsonb3b079d2010-09-13 23:44:34 +01001630 mod_timer(&dev_priv->hangcheck_timer,
1631 jiffies + msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
Ben Gamarif65d9422009-09-14 17:48:44 -04001632 if (was_empty)
Chris Wilsonb3b079d2010-09-13 23:44:34 +01001633 queue_delayed_work(dev_priv->wq,
1634 &dev_priv->mm.retire_work, HZ);
Ben Gamarif65d9422009-09-14 17:48:44 -04001635 }
Eric Anholt673a3942008-07-30 12:06:12 -07001636 return seqno;
1637}
1638
1639/**
1640 * Command execution barrier
1641 *
1642 * Ensures that all commands in the ring are finished
1643 * before signalling the CPU
1644 */
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001645static void
Zou Nan hai852835f2010-05-21 09:08:56 +08001646i915_retire_commands(struct drm_device *dev, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001647{
Eric Anholt673a3942008-07-30 12:06:12 -07001648 uint32_t flush_domains = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001649
1650 /* The sampler always gets flushed on i965 (sigh) */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001651 if (INTEL_INFO(dev)->gen >= 4)
Eric Anholt673a3942008-07-30 12:06:12 -07001652 flush_domains |= I915_GEM_DOMAIN_SAMPLER;
Zou Nan hai852835f2010-05-21 09:08:56 +08001653
1654 ring->flush(dev, ring,
1655 I915_GEM_DOMAIN_COMMAND, flush_domains);
Eric Anholt673a3942008-07-30 12:06:12 -07001656}
1657
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001658static inline void
1659i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
Eric Anholt673a3942008-07-30 12:06:12 -07001660{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001661 if (request->file_priv) {
1662 mutex_lock(&request->file_priv->mutex);
1663 list_del(&request->client_list);
1664 mutex_unlock(&request->file_priv->mutex);
1665 }
Eric Anholt673a3942008-07-30 12:06:12 -07001666}
1667
Chris Wilsondfaae392010-09-22 10:31:52 +01001668static void i915_gem_reset_ring_lists(struct drm_i915_private *dev_priv,
1669 struct intel_ring_buffer *ring)
Chris Wilson9375e442010-09-19 12:21:28 +01001670{
Chris Wilsondfaae392010-09-22 10:31:52 +01001671 while (!list_empty(&ring->request_list)) {
1672 struct drm_i915_gem_request *request;
Chris Wilson9375e442010-09-19 12:21:28 +01001673
Chris Wilsondfaae392010-09-22 10:31:52 +01001674 request = list_first_entry(&ring->request_list,
1675 struct drm_i915_gem_request,
1676 list);
1677
1678 list_del(&request->list);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001679 i915_gem_request_remove_from_client(request);
Chris Wilsondfaae392010-09-22 10:31:52 +01001680 kfree(request);
1681 }
1682
1683 while (!list_empty(&ring->active_list)) {
Chris Wilson9375e442010-09-19 12:21:28 +01001684 struct drm_i915_gem_object *obj_priv;
1685
Chris Wilsondfaae392010-09-22 10:31:52 +01001686 obj_priv = list_first_entry(&ring->active_list,
1687 struct drm_i915_gem_object,
1688 list);
1689
1690 obj_priv->base.write_domain = 0;
1691 list_del_init(&obj_priv->gpu_write_list);
1692 i915_gem_object_move_to_inactive(&obj_priv->base);
1693 }
1694}
1695
1696void i915_gem_reset_lists(struct drm_device *dev)
1697{
1698 struct drm_i915_private *dev_priv = dev->dev_private;
1699 struct drm_i915_gem_object *obj_priv;
1700
1701 i915_gem_reset_ring_lists(dev_priv, &dev_priv->render_ring);
1702 if (HAS_BSD(dev))
1703 i915_gem_reset_ring_lists(dev_priv, &dev_priv->bsd_ring);
1704
1705 /* Remove anything from the flushing lists. The GPU cache is likely
1706 * to be lost on reset along with the data, so simply move the
1707 * lost bo to the inactive list.
1708 */
1709 while (!list_empty(&dev_priv->mm.flushing_list)) {
Chris Wilson9375e442010-09-19 12:21:28 +01001710 obj_priv = list_first_entry(&dev_priv->mm.flushing_list,
1711 struct drm_i915_gem_object,
1712 list);
1713
1714 obj_priv->base.write_domain = 0;
Chris Wilsondfaae392010-09-22 10:31:52 +01001715 list_del_init(&obj_priv->gpu_write_list);
Chris Wilson9375e442010-09-19 12:21:28 +01001716 i915_gem_object_move_to_inactive(&obj_priv->base);
1717 }
Chris Wilson9375e442010-09-19 12:21:28 +01001718
Chris Wilsondfaae392010-09-22 10:31:52 +01001719 /* Move everything out of the GPU domains to ensure we do any
1720 * necessary invalidation upon reuse.
1721 */
Chris Wilson77f01232010-09-19 12:31:36 +01001722 list_for_each_entry(obj_priv,
1723 &dev_priv->mm.inactive_list,
1724 list)
1725 {
1726 obj_priv->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
1727 }
1728}
1729
Eric Anholt673a3942008-07-30 12:06:12 -07001730/**
1731 * This function clears the request list as sequence numbers are passed.
1732 */
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001733static void
1734i915_gem_retire_requests_ring(struct drm_device *dev,
1735 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001736{
1737 drm_i915_private_t *dev_priv = dev->dev_private;
1738 uint32_t seqno;
1739
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001740 if (!ring->status_page.page_addr ||
1741 list_empty(&ring->request_list))
Karsten Wiese6c0594a2009-02-23 15:07:57 +01001742 return;
1743
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001744 seqno = ring->get_seqno(dev, ring);
Zou Nan hai852835f2010-05-21 09:08:56 +08001745 while (!list_empty(&ring->request_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001746 struct drm_i915_gem_request *request;
Eric Anholt673a3942008-07-30 12:06:12 -07001747
Zou Nan hai852835f2010-05-21 09:08:56 +08001748 request = list_first_entry(&ring->request_list,
Eric Anholt673a3942008-07-30 12:06:12 -07001749 struct drm_i915_gem_request,
1750 list);
Eric Anholt673a3942008-07-30 12:06:12 -07001751
Chris Wilsondfaae392010-09-22 10:31:52 +01001752 if (!i915_seqno_passed(seqno, request->seqno))
Eric Anholt673a3942008-07-30 12:06:12 -07001753 break;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001754
1755 trace_i915_gem_request_retire(dev, request->seqno);
1756
1757 list_del(&request->list);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001758 i915_gem_request_remove_from_client(request);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001759 kfree(request);
1760 }
1761
1762 /* Move any buffers on the active list that are no longer referenced
1763 * by the ringbuffer to the flushing/inactive lists as appropriate.
1764 */
1765 while (!list_empty(&ring->active_list)) {
1766 struct drm_gem_object *obj;
1767 struct drm_i915_gem_object *obj_priv;
1768
1769 obj_priv = list_first_entry(&ring->active_list,
1770 struct drm_i915_gem_object,
1771 list);
1772
Chris Wilsondfaae392010-09-22 10:31:52 +01001773 if (!i915_seqno_passed(seqno, obj_priv->last_rendering_seqno))
Chris Wilsonb84d5f02010-09-18 01:38:04 +01001774 break;
1775
1776 obj = &obj_priv->base;
1777
1778#if WATCH_LRU
1779 DRM_INFO("%s: retire %d moves to inactive list %p\n",
1780 __func__, request->seqno, obj);
1781#endif
1782
1783 if (obj->write_domain != 0)
1784 i915_gem_object_move_to_flushing(obj);
1785 else
1786 i915_gem_object_move_to_inactive(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001787 }
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001788
1789 if (unlikely (dev_priv->trace_irq_seqno &&
1790 i915_seqno_passed(dev_priv->trace_irq_seqno, seqno))) {
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001791 ring->user_irq_put(dev, ring);
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001792 dev_priv->trace_irq_seqno = 0;
1793 }
Eric Anholt673a3942008-07-30 12:06:12 -07001794}
1795
1796void
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001797i915_gem_retire_requests(struct drm_device *dev)
1798{
1799 drm_i915_private_t *dev_priv = dev->dev_private;
1800
Chris Wilsonbe726152010-07-23 23:18:50 +01001801 if (!list_empty(&dev_priv->mm.deferred_free_list)) {
1802 struct drm_i915_gem_object *obj_priv, *tmp;
1803
1804 /* We must be careful that during unbind() we do not
1805 * accidentally infinitely recurse into retire requests.
1806 * Currently:
1807 * retire -> free -> unbind -> wait -> retire_ring
1808 */
1809 list_for_each_entry_safe(obj_priv, tmp,
1810 &dev_priv->mm.deferred_free_list,
1811 list)
1812 i915_gem_free_object_tail(&obj_priv->base);
1813 }
1814
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001815 i915_gem_retire_requests_ring(dev, &dev_priv->render_ring);
1816 if (HAS_BSD(dev))
1817 i915_gem_retire_requests_ring(dev, &dev_priv->bsd_ring);
1818}
1819
Daniel Vetter75ef9da2010-08-21 00:25:16 +02001820static void
Eric Anholt673a3942008-07-30 12:06:12 -07001821i915_gem_retire_work_handler(struct work_struct *work)
1822{
1823 drm_i915_private_t *dev_priv;
1824 struct drm_device *dev;
1825
1826 dev_priv = container_of(work, drm_i915_private_t,
1827 mm.retire_work.work);
1828 dev = dev_priv->dev;
1829
1830 mutex_lock(&dev->struct_mutex);
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001831 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08001832
Keith Packard6dbe2772008-10-14 21:41:13 -07001833 if (!dev_priv->mm.suspended &&
Zou Nan haid1b851f2010-05-21 09:08:57 +08001834 (!list_empty(&dev_priv->render_ring.request_list) ||
1835 (HAS_BSD(dev) &&
1836 !list_empty(&dev_priv->bsd_ring.request_list))))
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001837 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
Eric Anholt673a3942008-07-30 12:06:12 -07001838 mutex_unlock(&dev->struct_mutex);
1839}
1840
Daniel Vetter5a5a0c62009-09-15 22:57:36 +02001841int
Zou Nan hai852835f2010-05-21 09:08:56 +08001842i915_do_wait_request(struct drm_device *dev, uint32_t seqno,
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01001843 bool interruptible, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001844{
1845 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001846 u32 ier;
Eric Anholt673a3942008-07-30 12:06:12 -07001847 int ret = 0;
1848
1849 BUG_ON(seqno == 0);
1850
Daniel Vettere35a41d2010-02-11 22:13:59 +01001851 if (seqno == dev_priv->next_seqno) {
Chris Wilson8dc5d142010-08-12 12:36:12 +01001852 seqno = i915_add_request(dev, NULL, NULL, ring);
Daniel Vettere35a41d2010-02-11 22:13:59 +01001853 if (seqno == 0)
1854 return -ENOMEM;
1855 }
1856
Ben Gamariba1234d2009-09-14 17:48:47 -04001857 if (atomic_read(&dev_priv->mm.wedged))
Ben Gamariffed1d02009-09-14 17:48:41 -04001858 return -EIO;
1859
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001860 if (!i915_seqno_passed(ring->get_seqno(dev, ring), seqno)) {
Eric Anholtbad720f2009-10-22 16:11:14 -07001861 if (HAS_PCH_SPLIT(dev))
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001862 ier = I915_READ(DEIER) | I915_READ(GTIER);
1863 else
1864 ier = I915_READ(IER);
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001865 if (!ier) {
1866 DRM_ERROR("something (likely vbetool) disabled "
1867 "interrupts, re-enabling\n");
1868 i915_driver_irq_preinstall(dev);
1869 i915_driver_irq_postinstall(dev);
1870 }
1871
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001872 trace_i915_gem_request_wait_begin(dev, seqno);
1873
Zou Nan hai852835f2010-05-21 09:08:56 +08001874 ring->waiting_gem_seqno = seqno;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001875 ring->user_irq_get(dev, ring);
Daniel Vetter48764bf2009-09-15 22:57:32 +02001876 if (interruptible)
Zou Nan hai852835f2010-05-21 09:08:56 +08001877 ret = wait_event_interruptible(ring->irq_queue,
1878 i915_seqno_passed(
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001879 ring->get_seqno(dev, ring), seqno)
Zou Nan hai852835f2010-05-21 09:08:56 +08001880 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02001881 else
Zou Nan hai852835f2010-05-21 09:08:56 +08001882 wait_event(ring->irq_queue,
1883 i915_seqno_passed(
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001884 ring->get_seqno(dev, ring), seqno)
Zou Nan hai852835f2010-05-21 09:08:56 +08001885 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02001886
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001887 ring->user_irq_put(dev, ring);
Zou Nan hai852835f2010-05-21 09:08:56 +08001888 ring->waiting_gem_seqno = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001889
1890 trace_i915_gem_request_wait_end(dev, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07001891 }
Ben Gamariba1234d2009-09-14 17:48:47 -04001892 if (atomic_read(&dev_priv->mm.wedged))
Eric Anholt673a3942008-07-30 12:06:12 -07001893 ret = -EIO;
1894
1895 if (ret && ret != -ERESTARTSYS)
Daniel Vetter8bff9172010-02-11 22:19:40 +01001896 DRM_ERROR("%s returns %d (awaiting %d at %d, next %d)\n",
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001897 __func__, ret, seqno, ring->get_seqno(dev, ring),
Daniel Vetter8bff9172010-02-11 22:19:40 +01001898 dev_priv->next_seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07001899
1900 /* Directly dispatch request retiring. While we have the work queue
1901 * to handle this, the waiter on a request often wants an associated
1902 * buffer to have made it to the inactive list, and we would need
1903 * a separate wait queue to handle that.
1904 */
1905 if (ret == 0)
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001906 i915_gem_retire_requests_ring(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07001907
1908 return ret;
1909}
1910
Daniel Vetter48764bf2009-09-15 22:57:32 +02001911/**
1912 * Waits for a sequence number to be signaled, and cleans up the
1913 * request and object lists appropriately for that event.
1914 */
1915static int
Zou Nan hai852835f2010-05-21 09:08:56 +08001916i915_wait_request(struct drm_device *dev, uint32_t seqno,
1917 struct intel_ring_buffer *ring)
Daniel Vetter48764bf2009-09-15 22:57:32 +02001918{
Zou Nan hai852835f2010-05-21 09:08:56 +08001919 return i915_do_wait_request(dev, seqno, 1, ring);
Daniel Vetter48764bf2009-09-15 22:57:32 +02001920}
1921
Chris Wilson20f0cd52010-09-23 11:00:38 +01001922static void
Chris Wilson92204342010-09-18 11:02:01 +01001923i915_gem_flush_ring(struct drm_device *dev,
Chris Wilsonc78ec302010-09-20 12:50:23 +01001924 struct drm_file *file_priv,
Chris Wilson92204342010-09-18 11:02:01 +01001925 struct intel_ring_buffer *ring,
1926 uint32_t invalidate_domains,
1927 uint32_t flush_domains)
1928{
1929 ring->flush(dev, ring, invalidate_domains, flush_domains);
1930 i915_gem_process_flushing_list(dev, flush_domains, ring);
1931}
1932
1933static void
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001934i915_gem_flush(struct drm_device *dev,
Chris Wilsonc78ec302010-09-20 12:50:23 +01001935 struct drm_file *file_priv,
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001936 uint32_t invalidate_domains,
Chris Wilson92204342010-09-18 11:02:01 +01001937 uint32_t flush_domains,
1938 uint32_t flush_rings)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001939{
1940 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter8bff9172010-02-11 22:19:40 +01001941
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001942 if (flush_domains & I915_GEM_DOMAIN_CPU)
1943 drm_agp_chipset_flush(dev);
Daniel Vetter8bff9172010-02-11 22:19:40 +01001944
Chris Wilson92204342010-09-18 11:02:01 +01001945 if ((flush_domains | invalidate_domains) & I915_GEM_GPU_DOMAINS) {
1946 if (flush_rings & RING_RENDER)
Chris Wilsonc78ec302010-09-20 12:50:23 +01001947 i915_gem_flush_ring(dev, file_priv,
Chris Wilson92204342010-09-18 11:02:01 +01001948 &dev_priv->render_ring,
1949 invalidate_domains, flush_domains);
1950 if (flush_rings & RING_BSD)
Chris Wilsonc78ec302010-09-20 12:50:23 +01001951 i915_gem_flush_ring(dev, file_priv,
Chris Wilson92204342010-09-18 11:02:01 +01001952 &dev_priv->bsd_ring,
1953 invalidate_domains, flush_domains);
1954 }
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001955}
1956
Eric Anholt673a3942008-07-30 12:06:12 -07001957/**
1958 * Ensures that all rendering to the object has completed and the object is
1959 * safe to unbind from the GTT or access from the CPU.
1960 */
1961static int
Chris Wilson2cf34d72010-09-14 13:03:28 +01001962i915_gem_object_wait_rendering(struct drm_gem_object *obj,
1963 bool interruptible)
Eric Anholt673a3942008-07-30 12:06:12 -07001964{
1965 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001966 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001967 int ret;
1968
Eric Anholte47c68e2008-11-14 13:35:19 -08001969 /* This function only exists to support waiting for existing rendering,
1970 * not for emitting required flushes.
Eric Anholt673a3942008-07-30 12:06:12 -07001971 */
Eric Anholte47c68e2008-11-14 13:35:19 -08001972 BUG_ON((obj->write_domain & I915_GEM_GPU_DOMAINS) != 0);
Eric Anholt673a3942008-07-30 12:06:12 -07001973
1974 /* If there is rendering queued on the buffer being evicted, wait for
1975 * it.
1976 */
1977 if (obj_priv->active) {
1978#if WATCH_BUF
1979 DRM_INFO("%s: object %p wait for seqno %08x\n",
1980 __func__, obj, obj_priv->last_rendering_seqno);
1981#endif
Chris Wilson2cf34d72010-09-14 13:03:28 +01001982 ret = i915_do_wait_request(dev,
1983 obj_priv->last_rendering_seqno,
1984 interruptible,
1985 obj_priv->ring);
1986 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07001987 return ret;
1988 }
1989
1990 return 0;
1991}
1992
1993/**
1994 * Unbinds an object from the GTT aperture.
1995 */
Jesse Barnes0f973f22009-01-26 17:10:45 -08001996int
Eric Anholt673a3942008-07-30 12:06:12 -07001997i915_gem_object_unbind(struct drm_gem_object *obj)
1998{
1999 struct drm_device *dev = obj->dev;
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);
Chris Wilson8dc17752010-07-23 23:18:51 +01002025 if (ret == -ERESTARTSYS)
Eric Anholt673a3942008-07-30 12:06:12 -07002026 return ret;
Chris Wilson8dc17752010-07-23 23:18:51 +01002027 /* Continue on if we fail due to EIO, the GPU is hung so we
2028 * should be safe and we need to cleanup or else we might
2029 * cause memory corruption through use-after-free.
2030 */
Eric Anholt673a3942008-07-30 12:06:12 -07002031
Daniel Vetter96b47b62009-12-15 17:50:00 +01002032 /* release the fence reg _after_ flushing */
2033 if (obj_priv->fence_reg != I915_FENCE_REG_NONE)
2034 i915_gem_clear_fence_reg(obj);
2035
Eric Anholt673a3942008-07-30 12:06:12 -07002036 if (obj_priv->agp_mem != NULL) {
2037 drm_unbind_agp(obj_priv->agp_mem);
2038 drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE);
2039 obj_priv->agp_mem = NULL;
2040 }
2041
Eric Anholt856fa192009-03-19 14:10:50 -07002042 i915_gem_object_put_pages(obj);
Chris Wilsona32808c2009-09-20 21:29:47 +01002043 BUG_ON(obj_priv->pages_refcount);
Eric Anholt673a3942008-07-30 12:06:12 -07002044
2045 if (obj_priv->gtt_space) {
2046 atomic_dec(&dev->gtt_count);
2047 atomic_sub(obj->size, &dev->gtt_memory);
2048
2049 drm_mm_put_block(obj_priv->gtt_space);
2050 obj_priv->gtt_space = NULL;
2051 }
2052
Chris Wilsonf13d3f72010-09-20 17:36:15 +01002053 list_del_init(&obj_priv->list);
Eric Anholt673a3942008-07-30 12:06:12 -07002054
Chris Wilson963b4832009-09-20 23:03:54 +01002055 if (i915_gem_object_is_purgeable(obj_priv))
2056 i915_gem_object_truncate(obj);
2057
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002058 trace_i915_gem_object_unbind(obj);
2059
Chris Wilson8dc17752010-07-23 23:18:51 +01002060 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002061}
2062
Chris Wilsonb47eb4a2010-08-07 11:01:23 +01002063int
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002064i915_gpu_idle(struct drm_device *dev)
2065{
2066 drm_i915_private_t *dev_priv = dev->dev_private;
2067 bool lists_empty;
Chris Wilsonc78ec302010-09-20 12:50:23 +01002068 u32 seqno;
Zou Nan hai852835f2010-05-21 09:08:56 +08002069 int ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002070
Zou Nan haid1b851f2010-05-21 09:08:57 +08002071 lists_empty = (list_empty(&dev_priv->mm.flushing_list) &&
2072 list_empty(&dev_priv->render_ring.active_list) &&
2073 (!HAS_BSD(dev) ||
2074 list_empty(&dev_priv->bsd_ring.active_list)));
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002075 if (lists_empty)
2076 return 0;
2077
2078 /* Flush everything onto the inactive list. */
Chris Wilson5c12a07e2010-09-22 11:22:30 +01002079 seqno = dev_priv->next_seqno;
Chris Wilsonc78ec302010-09-20 12:50:23 +01002080 i915_gem_flush_ring(dev, NULL, &dev_priv->render_ring,
Chris Wilson92204342010-09-18 11:02:01 +01002081 I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
Chris Wilsonc78ec302010-09-20 12:50:23 +01002082 ret = i915_wait_request(dev, seqno, &dev_priv->render_ring);
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002083 if (ret)
2084 return ret;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002085
2086 if (HAS_BSD(dev)) {
Chris Wilson5c12a07e2010-09-22 11:22:30 +01002087 seqno = dev_priv->next_seqno;
Chris Wilsonc78ec302010-09-20 12:50:23 +01002088 i915_gem_flush_ring(dev, NULL, &dev_priv->bsd_ring,
Chris Wilson92204342010-09-18 11:02:01 +01002089 I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
Chris Wilsonc78ec302010-09-20 12:50:23 +01002090 ret = i915_wait_request(dev, seqno, &dev_priv->bsd_ring);
Zou Nan haid1b851f2010-05-21 09:08:57 +08002091 if (ret)
2092 return ret;
2093 }
2094
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002095 return 0;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002096}
2097
Ben Gamari6911a9b2009-04-02 11:24:54 -07002098int
Chris Wilson4bdadb92010-01-27 13:36:32 +00002099i915_gem_object_get_pages(struct drm_gem_object *obj,
2100 gfp_t gfpmask)
Eric Anholt673a3942008-07-30 12:06:12 -07002101{
Daniel Vetter23010e42010-03-08 13:35:02 +01002102 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002103 int page_count, i;
2104 struct address_space *mapping;
2105 struct inode *inode;
2106 struct page *page;
Eric Anholt673a3942008-07-30 12:06:12 -07002107
Daniel Vetter778c3542010-05-13 11:49:44 +02002108 BUG_ON(obj_priv->pages_refcount
2109 == DRM_I915_GEM_OBJECT_MAX_PAGES_REFCOUNT);
2110
Eric Anholt856fa192009-03-19 14:10:50 -07002111 if (obj_priv->pages_refcount++ != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07002112 return 0;
2113
2114 /* Get the list of pages out of our struct file. They'll be pinned
2115 * at this point until we release them.
2116 */
2117 page_count = obj->size / PAGE_SIZE;
Eric Anholt856fa192009-03-19 14:10:50 -07002118 BUG_ON(obj_priv->pages != NULL);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07002119 obj_priv->pages = drm_calloc_large(page_count, sizeof(struct page *));
Eric Anholt856fa192009-03-19 14:10:50 -07002120 if (obj_priv->pages == NULL) {
Eric Anholt856fa192009-03-19 14:10:50 -07002121 obj_priv->pages_refcount--;
Eric Anholt673a3942008-07-30 12:06:12 -07002122 return -ENOMEM;
2123 }
2124
2125 inode = obj->filp->f_path.dentry->d_inode;
2126 mapping = inode->i_mapping;
2127 for (i = 0; i < page_count; i++) {
Chris Wilson4bdadb92010-01-27 13:36:32 +00002128 page = read_cache_page_gfp(mapping, i,
Linus Torvalds985b8232010-07-02 10:04:42 +10002129 GFP_HIGHUSER |
Chris Wilson4bdadb92010-01-27 13:36:32 +00002130 __GFP_COLD |
Linus Torvaldscd9f0402010-07-18 09:44:37 -07002131 __GFP_RECLAIMABLE |
Chris Wilson4bdadb92010-01-27 13:36:32 +00002132 gfpmask);
Chris Wilson1f2b1012010-03-12 19:52:55 +00002133 if (IS_ERR(page))
2134 goto err_pages;
2135
Eric Anholt856fa192009-03-19 14:10:50 -07002136 obj_priv->pages[i] = page;
Eric Anholt673a3942008-07-30 12:06:12 -07002137 }
Eric Anholt280b7132009-03-12 16:56:27 -07002138
2139 if (obj_priv->tiling_mode != I915_TILING_NONE)
2140 i915_gem_object_do_bit_17_swizzle(obj);
2141
Eric Anholt673a3942008-07-30 12:06:12 -07002142 return 0;
Chris Wilson1f2b1012010-03-12 19:52:55 +00002143
2144err_pages:
2145 while (i--)
2146 page_cache_release(obj_priv->pages[i]);
2147
2148 drm_free_large(obj_priv->pages);
2149 obj_priv->pages = NULL;
2150 obj_priv->pages_refcount--;
2151 return PTR_ERR(page);
Eric Anholt673a3942008-07-30 12:06:12 -07002152}
2153
Eric Anholt4e901fd2009-10-26 16:44:17 -07002154static void sandybridge_write_fence_reg(struct drm_i915_fence_reg *reg)
2155{
2156 struct drm_gem_object *obj = reg->obj;
2157 struct drm_device *dev = obj->dev;
2158 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002159 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt4e901fd2009-10-26 16:44:17 -07002160 int regnum = obj_priv->fence_reg;
2161 uint64_t val;
2162
2163 val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
2164 0xfffff000) << 32;
2165 val |= obj_priv->gtt_offset & 0xfffff000;
2166 val |= (uint64_t)((obj_priv->stride / 128) - 1) <<
2167 SANDYBRIDGE_FENCE_PITCH_SHIFT;
2168
2169 if (obj_priv->tiling_mode == I915_TILING_Y)
2170 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2171 val |= I965_FENCE_REG_VALID;
2172
2173 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (regnum * 8), val);
2174}
2175
Jesse Barnesde151cf2008-11-12 10:03:55 -08002176static void i965_write_fence_reg(struct drm_i915_fence_reg *reg)
2177{
2178 struct drm_gem_object *obj = reg->obj;
2179 struct drm_device *dev = obj->dev;
2180 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002181 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002182 int regnum = obj_priv->fence_reg;
2183 uint64_t val;
2184
2185 val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
2186 0xfffff000) << 32;
2187 val |= obj_priv->gtt_offset & 0xfffff000;
2188 val |= ((obj_priv->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
2189 if (obj_priv->tiling_mode == I915_TILING_Y)
2190 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2191 val |= I965_FENCE_REG_VALID;
2192
2193 I915_WRITE64(FENCE_REG_965_0 + (regnum * 8), val);
2194}
2195
2196static void i915_write_fence_reg(struct drm_i915_fence_reg *reg)
2197{
2198 struct drm_gem_object *obj = reg->obj;
2199 struct drm_device *dev = obj->dev;
2200 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002201 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002202 int regnum = obj_priv->fence_reg;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002203 int tile_width;
Eric Anholtdc529a42009-03-10 22:34:49 -07002204 uint32_t fence_reg, val;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002205 uint32_t pitch_val;
2206
2207 if ((obj_priv->gtt_offset & ~I915_FENCE_START_MASK) ||
2208 (obj_priv->gtt_offset & (obj->size - 1))) {
Linus Torvaldsf06da262009-02-09 08:57:29 -08002209 WARN(1, "%s: object 0x%08x not 1M or size (0x%zx) aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002210 __func__, obj_priv->gtt_offset, obj->size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002211 return;
2212 }
2213
Jesse Barnes0f973f22009-01-26 17:10:45 -08002214 if (obj_priv->tiling_mode == I915_TILING_Y &&
2215 HAS_128_BYTE_Y_TILING(dev))
2216 tile_width = 128;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002217 else
Jesse Barnes0f973f22009-01-26 17:10:45 -08002218 tile_width = 512;
2219
2220 /* Note: pitch better be a power of two tile widths */
2221 pitch_val = obj_priv->stride / tile_width;
2222 pitch_val = ffs(pitch_val) - 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002223
Daniel Vetterc36a2a62010-04-17 15:12:03 +02002224 if (obj_priv->tiling_mode == I915_TILING_Y &&
2225 HAS_128_BYTE_Y_TILING(dev))
2226 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2227 else
2228 WARN_ON(pitch_val > I915_FENCE_MAX_PITCH_VAL);
2229
Jesse Barnesde151cf2008-11-12 10:03:55 -08002230 val = obj_priv->gtt_offset;
2231 if (obj_priv->tiling_mode == I915_TILING_Y)
2232 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2233 val |= I915_FENCE_SIZE_BITS(obj->size);
2234 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2235 val |= I830_FENCE_REG_VALID;
2236
Eric Anholtdc529a42009-03-10 22:34:49 -07002237 if (regnum < 8)
2238 fence_reg = FENCE_REG_830_0 + (regnum * 4);
2239 else
2240 fence_reg = FENCE_REG_945_8 + ((regnum - 8) * 4);
2241 I915_WRITE(fence_reg, val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002242}
2243
2244static void i830_write_fence_reg(struct drm_i915_fence_reg *reg)
2245{
2246 struct drm_gem_object *obj = reg->obj;
2247 struct drm_device *dev = obj->dev;
2248 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002249 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002250 int regnum = obj_priv->fence_reg;
2251 uint32_t val;
2252 uint32_t pitch_val;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002253 uint32_t fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002254
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002255 if ((obj_priv->gtt_offset & ~I830_FENCE_START_MASK) ||
Jesse Barnesde151cf2008-11-12 10:03:55 -08002256 (obj_priv->gtt_offset & (obj->size - 1))) {
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002257 WARN(1, "%s: object 0x%08x not 512K or size aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002258 __func__, obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002259 return;
2260 }
2261
Eric Anholte76a16d2009-05-26 17:44:56 -07002262 pitch_val = obj_priv->stride / 128;
2263 pitch_val = ffs(pitch_val) - 1;
2264 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2265
Jesse Barnesde151cf2008-11-12 10:03:55 -08002266 val = obj_priv->gtt_offset;
2267 if (obj_priv->tiling_mode == I915_TILING_Y)
2268 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002269 fence_size_bits = I830_FENCE_SIZE_BITS(obj->size);
2270 WARN_ON(fence_size_bits & ~0x00000f00);
2271 val |= fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002272 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2273 val |= I830_FENCE_REG_VALID;
2274
2275 I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002276}
2277
Chris Wilson2cf34d72010-09-14 13:03:28 +01002278static int i915_find_fence_reg(struct drm_device *dev,
2279 bool interruptible)
Daniel Vetterae3db242010-02-19 11:51:58 +01002280{
2281 struct drm_i915_fence_reg *reg = NULL;
2282 struct drm_i915_gem_object *obj_priv = NULL;
2283 struct drm_i915_private *dev_priv = dev->dev_private;
2284 struct drm_gem_object *obj = NULL;
2285 int i, avail, ret;
2286
2287 /* First try to find a free reg */
2288 avail = 0;
2289 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2290 reg = &dev_priv->fence_regs[i];
2291 if (!reg->obj)
2292 return i;
2293
Daniel Vetter23010e42010-03-08 13:35:02 +01002294 obj_priv = to_intel_bo(reg->obj);
Daniel Vetterae3db242010-02-19 11:51:58 +01002295 if (!obj_priv->pin_count)
2296 avail++;
2297 }
2298
2299 if (avail == 0)
2300 return -ENOSPC;
2301
2302 /* None available, try to steal one or wait for a user to finish */
2303 i = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002304 list_for_each_entry(reg, &dev_priv->mm.fence_list,
2305 lru_list) {
2306 obj = reg->obj;
2307 obj_priv = to_intel_bo(obj);
Daniel Vetterae3db242010-02-19 11:51:58 +01002308
2309 if (obj_priv->pin_count)
2310 continue;
2311
2312 /* found one! */
2313 i = obj_priv->fence_reg;
2314 break;
2315 }
2316
2317 BUG_ON(i == I915_FENCE_REG_NONE);
2318
2319 /* We only have a reference on obj from the active list. put_fence_reg
2320 * might drop that one, causing a use-after-free in it. So hold a
2321 * private reference to obj like the other callers of put_fence_reg
2322 * (set_tiling ioctl) do. */
2323 drm_gem_object_reference(obj);
Chris Wilson2cf34d72010-09-14 13:03:28 +01002324 ret = i915_gem_object_put_fence_reg(obj, interruptible);
Daniel Vetterae3db242010-02-19 11:51:58 +01002325 drm_gem_object_unreference(obj);
2326 if (ret != 0)
2327 return ret;
2328
2329 return i;
2330}
2331
Jesse Barnesde151cf2008-11-12 10:03:55 -08002332/**
2333 * i915_gem_object_get_fence_reg - set up a fence reg for an object
2334 * @obj: object to map through a fence reg
2335 *
2336 * When mapping objects through the GTT, userspace wants to be able to write
2337 * to them without having to worry about swizzling if the object is tiled.
2338 *
2339 * This function walks the fence regs looking for a free one for @obj,
2340 * stealing one if it can't find any.
2341 *
2342 * It then sets up the reg based on the object's properties: address, pitch
2343 * and tiling format.
2344 */
Chris Wilson8c4b8c32009-06-17 22:08:52 +01002345int
Chris Wilson2cf34d72010-09-14 13:03:28 +01002346i915_gem_object_get_fence_reg(struct drm_gem_object *obj,
2347 bool interruptible)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002348{
2349 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002350 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002351 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002352 struct drm_i915_fence_reg *reg = NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002353 int ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002354
Eric Anholta09ba7f2009-08-29 12:49:51 -07002355 /* Just update our place in the LRU if our fence is getting used. */
2356 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002357 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
2358 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002359 return 0;
2360 }
2361
Jesse Barnesde151cf2008-11-12 10:03:55 -08002362 switch (obj_priv->tiling_mode) {
2363 case I915_TILING_NONE:
2364 WARN(1, "allocating a fence for non-tiled object?\n");
2365 break;
2366 case I915_TILING_X:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002367 if (!obj_priv->stride)
2368 return -EINVAL;
2369 WARN((obj_priv->stride & (512 - 1)),
2370 "object 0x%08x is X tiled but has non-512B pitch\n",
2371 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002372 break;
2373 case I915_TILING_Y:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002374 if (!obj_priv->stride)
2375 return -EINVAL;
2376 WARN((obj_priv->stride & (128 - 1)),
2377 "object 0x%08x is Y tiled but has non-128B pitch\n",
2378 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002379 break;
2380 }
2381
Chris Wilson2cf34d72010-09-14 13:03:28 +01002382 ret = i915_find_fence_reg(dev, interruptible);
Daniel Vetterae3db242010-02-19 11:51:58 +01002383 if (ret < 0)
2384 return ret;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002385
Daniel Vetterae3db242010-02-19 11:51:58 +01002386 obj_priv->fence_reg = ret;
2387 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002388 list_add_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002389
Jesse Barnesde151cf2008-11-12 10:03:55 -08002390 reg->obj = obj;
2391
Chris Wilsone259bef2010-09-17 00:32:02 +01002392 switch (INTEL_INFO(dev)->gen) {
2393 case 6:
Eric Anholt4e901fd2009-10-26 16:44:17 -07002394 sandybridge_write_fence_reg(reg);
Chris Wilsone259bef2010-09-17 00:32:02 +01002395 break;
2396 case 5:
2397 case 4:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002398 i965_write_fence_reg(reg);
Chris Wilsone259bef2010-09-17 00:32:02 +01002399 break;
2400 case 3:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002401 i915_write_fence_reg(reg);
Chris Wilsone259bef2010-09-17 00:32:02 +01002402 break;
2403 case 2:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002404 i830_write_fence_reg(reg);
Chris Wilsone259bef2010-09-17 00:32:02 +01002405 break;
2406 }
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002407
Daniel Vetterae3db242010-02-19 11:51:58 +01002408 trace_i915_gem_object_get_fence(obj, obj_priv->fence_reg,
2409 obj_priv->tiling_mode);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002410
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002411 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002412}
2413
2414/**
2415 * i915_gem_clear_fence_reg - clear out fence register info
2416 * @obj: object to clear
2417 *
2418 * Zeroes out the fence register itself and clears out the associated
2419 * data structures in dev_priv and obj_priv.
2420 */
2421static void
2422i915_gem_clear_fence_reg(struct drm_gem_object *obj)
2423{
2424 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002425 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002426 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002427 struct drm_i915_fence_reg *reg =
2428 &dev_priv->fence_regs[obj_priv->fence_reg];
Chris Wilsone259bef2010-09-17 00:32:02 +01002429 uint32_t fence_reg;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002430
Chris Wilsone259bef2010-09-17 00:32:02 +01002431 switch (INTEL_INFO(dev)->gen) {
2432 case 6:
Eric Anholt4e901fd2009-10-26 16:44:17 -07002433 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 +
2434 (obj_priv->fence_reg * 8), 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002435 break;
2436 case 5:
2437 case 4:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002438 I915_WRITE64(FENCE_REG_965_0 + (obj_priv->fence_reg * 8), 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002439 break;
2440 case 3:
2441 if (obj_priv->fence_reg > 8)
2442 fence_reg = FENCE_REG_945_8 + (obj_priv->fence_reg - 8) * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002443 else
Chris Wilsone259bef2010-09-17 00:32:02 +01002444 case 2:
2445 fence_reg = FENCE_REG_830_0 + obj_priv->fence_reg * 4;
Eric Anholtdc529a42009-03-10 22:34:49 -07002446
2447 I915_WRITE(fence_reg, 0);
Chris Wilsone259bef2010-09-17 00:32:02 +01002448 break;
Eric Anholtdc529a42009-03-10 22:34:49 -07002449 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002450
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002451 reg->obj = NULL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002452 obj_priv->fence_reg = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002453 list_del_init(&reg->lru_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002454}
2455
Eric Anholt673a3942008-07-30 12:06:12 -07002456/**
Chris Wilson52dc7d32009-06-06 09:46:01 +01002457 * i915_gem_object_put_fence_reg - waits on outstanding fenced access
2458 * to the buffer to finish, and then resets the fence register.
2459 * @obj: tiled object holding a fence register.
Chris Wilson2cf34d72010-09-14 13:03:28 +01002460 * @bool: whether the wait upon the fence is interruptible
Chris Wilson52dc7d32009-06-06 09:46:01 +01002461 *
2462 * Zeroes out the fence register itself and clears out the associated
2463 * data structures in dev_priv and obj_priv.
2464 */
2465int
Chris Wilson2cf34d72010-09-14 13:03:28 +01002466i915_gem_object_put_fence_reg(struct drm_gem_object *obj,
2467 bool interruptible)
Chris Wilson52dc7d32009-06-06 09:46:01 +01002468{
2469 struct drm_device *dev = obj->dev;
Chris Wilson53640e12010-09-20 11:40:50 +01002470 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002471 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson53640e12010-09-20 11:40:50 +01002472 struct drm_i915_fence_reg *reg;
Chris Wilson52dc7d32009-06-06 09:46:01 +01002473
2474 if (obj_priv->fence_reg == I915_FENCE_REG_NONE)
2475 return 0;
2476
Daniel Vetter10ae9bd2010-02-01 13:59:17 +01002477 /* If we've changed tiling, GTT-mappings of the object
2478 * need to re-fault to ensure that the correct fence register
2479 * setup is in place.
2480 */
2481 i915_gem_release_mmap(obj);
2482
Chris Wilson52dc7d32009-06-06 09:46:01 +01002483 /* On the i915, GPU access to tiled buffers is via a fence,
2484 * therefore we must wait for any outstanding access to complete
2485 * before clearing the fence.
2486 */
Chris Wilson53640e12010-09-20 11:40:50 +01002487 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
2488 if (reg->gpu) {
Chris Wilson52dc7d32009-06-06 09:46:01 +01002489 int ret;
2490
Chris Wilson2cf34d72010-09-14 13:03:28 +01002491 ret = i915_gem_object_flush_gpu_write_domain(obj, true);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002492 if (ret)
2493 return ret;
2494
Chris Wilson2cf34d72010-09-14 13:03:28 +01002495 ret = i915_gem_object_wait_rendering(obj, interruptible);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002496 if (ret)
Chris Wilson52dc7d32009-06-06 09:46:01 +01002497 return ret;
Chris Wilson53640e12010-09-20 11:40:50 +01002498
2499 reg->gpu = false;
Chris Wilson52dc7d32009-06-06 09:46:01 +01002500 }
2501
Daniel Vetter4a726612010-02-01 13:59:16 +01002502 i915_gem_object_flush_gtt_write_domain(obj);
Chris Wilson0bc23aa2010-09-14 10:22:23 +01002503 i915_gem_clear_fence_reg(obj);
Chris Wilson52dc7d32009-06-06 09:46:01 +01002504
2505 return 0;
2506}
2507
2508/**
Eric Anholt673a3942008-07-30 12:06:12 -07002509 * Finds free space in the GTT aperture and binds the object there.
2510 */
2511static int
2512i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment)
2513{
2514 struct drm_device *dev = obj->dev;
2515 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002516 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002517 struct drm_mm_node *free_space;
Chris Wilson4bdadb92010-01-27 13:36:32 +00002518 gfp_t gfpmask = __GFP_NORETRY | __GFP_NOWARN;
Chris Wilson07f73f62009-09-14 16:50:30 +01002519 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002520
Chris Wilsonbb6baf72009-09-22 14:24:13 +01002521 if (obj_priv->madv != I915_MADV_WILLNEED) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01002522 DRM_ERROR("Attempting to bind a purgeable object\n");
2523 return -EINVAL;
2524 }
2525
Eric Anholt673a3942008-07-30 12:06:12 -07002526 if (alignment == 0)
Jesse Barnes0f973f22009-01-26 17:10:45 -08002527 alignment = i915_gem_get_gtt_alignment(obj);
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002528 if (alignment & (i915_gem_get_gtt_alignment(obj) - 1)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002529 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2530 return -EINVAL;
2531 }
2532
Chris Wilson654fc602010-05-27 13:18:21 +01002533 /* If the object is bigger than the entire aperture, reject it early
2534 * before evicting everything in a vain attempt to find space.
2535 */
2536 if (obj->size > dev->gtt_total) {
2537 DRM_ERROR("Attempting to bind an object larger than the aperture\n");
2538 return -E2BIG;
2539 }
2540
Eric Anholt673a3942008-07-30 12:06:12 -07002541 search_free:
2542 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
2543 obj->size, alignment, 0);
2544 if (free_space != NULL) {
2545 obj_priv->gtt_space = drm_mm_get_block(free_space, obj->size,
2546 alignment);
Daniel Vetterdb3307a2010-07-02 15:02:12 +01002547 if (obj_priv->gtt_space != NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002548 obj_priv->gtt_offset = obj_priv->gtt_space->start;
Eric Anholt673a3942008-07-30 12:06:12 -07002549 }
2550 if (obj_priv->gtt_space == NULL) {
2551 /* If the gtt is empty and we're still having trouble
2552 * fitting our object in, we're out of memory.
2553 */
2554#if WATCH_LRU
2555 DRM_INFO("%s: GTT full, evicting something\n", __func__);
2556#endif
Daniel Vetter0108a3e2010-08-07 11:01:21 +01002557 ret = i915_gem_evict_something(dev, obj->size, alignment);
Chris Wilson97311292009-09-21 00:22:34 +01002558 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002559 return ret;
Chris Wilson97311292009-09-21 00:22:34 +01002560
Eric Anholt673a3942008-07-30 12:06:12 -07002561 goto search_free;
2562 }
2563
2564#if WATCH_BUF
Krzysztof Halasacfd43c02009-06-20 00:31:28 +02002565 DRM_INFO("Binding object of size %zd at 0x%08x\n",
Eric Anholt673a3942008-07-30 12:06:12 -07002566 obj->size, obj_priv->gtt_offset);
2567#endif
Chris Wilson4bdadb92010-01-27 13:36:32 +00002568 ret = i915_gem_object_get_pages(obj, gfpmask);
Eric Anholt673a3942008-07-30 12:06:12 -07002569 if (ret) {
2570 drm_mm_put_block(obj_priv->gtt_space);
2571 obj_priv->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002572
2573 if (ret == -ENOMEM) {
2574 /* first try to clear up some space from the GTT */
Daniel Vetter0108a3e2010-08-07 11:01:21 +01002575 ret = i915_gem_evict_something(dev, obj->size,
2576 alignment);
Chris Wilson07f73f62009-09-14 16:50:30 +01002577 if (ret) {
Chris Wilson07f73f62009-09-14 16:50:30 +01002578 /* now try to shrink everyone else */
Chris Wilson4bdadb92010-01-27 13:36:32 +00002579 if (gfpmask) {
2580 gfpmask = 0;
2581 goto search_free;
Chris Wilson07f73f62009-09-14 16:50:30 +01002582 }
2583
2584 return ret;
2585 }
2586
2587 goto search_free;
2588 }
2589
Eric Anholt673a3942008-07-30 12:06:12 -07002590 return ret;
2591 }
2592
Eric Anholt673a3942008-07-30 12:06:12 -07002593 /* Create an AGP memory structure pointing at our pages, and bind it
2594 * into the GTT.
2595 */
2596 obj_priv->agp_mem = drm_agp_bind_pages(dev,
Eric Anholt856fa192009-03-19 14:10:50 -07002597 obj_priv->pages,
Chris Wilson07f73f62009-09-14 16:50:30 +01002598 obj->size >> PAGE_SHIFT,
Keith Packardba1eb1d2008-10-14 19:55:10 -07002599 obj_priv->gtt_offset,
2600 obj_priv->agp_type);
Eric Anholt673a3942008-07-30 12:06:12 -07002601 if (obj_priv->agp_mem == NULL) {
Eric Anholt856fa192009-03-19 14:10:50 -07002602 i915_gem_object_put_pages(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002603 drm_mm_put_block(obj_priv->gtt_space);
2604 obj_priv->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002605
Daniel Vetter0108a3e2010-08-07 11:01:21 +01002606 ret = i915_gem_evict_something(dev, obj->size, alignment);
Chris Wilson97311292009-09-21 00:22:34 +01002607 if (ret)
Chris Wilson07f73f62009-09-14 16:50:30 +01002608 return ret;
Chris Wilson07f73f62009-09-14 16:50:30 +01002609
2610 goto search_free;
Eric Anholt673a3942008-07-30 12:06:12 -07002611 }
2612 atomic_inc(&dev->gtt_count);
2613 atomic_add(obj->size, &dev->gtt_memory);
2614
Chris Wilsonbf1a1092010-08-07 11:01:20 +01002615 /* keep track of bounds object by adding it to the inactive list */
2616 list_add_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
2617
Eric Anholt673a3942008-07-30 12:06:12 -07002618 /* Assert that the object is not currently in any GPU domain. As it
2619 * wasn't in the GTT, there shouldn't be any way it could have been in
2620 * a GPU cache
2621 */
Chris Wilson21d509e2009-06-06 09:46:02 +01002622 BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS);
2623 BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS);
Eric Anholt673a3942008-07-30 12:06:12 -07002624
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002625 trace_i915_gem_object_bind(obj, obj_priv->gtt_offset);
2626
Eric Anholt673a3942008-07-30 12:06:12 -07002627 return 0;
2628}
2629
2630void
2631i915_gem_clflush_object(struct drm_gem_object *obj)
2632{
Daniel Vetter23010e42010-03-08 13:35:02 +01002633 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002634
2635 /* If we don't have a page list set up, then we're not pinned
2636 * to GPU, and we can ignore the cache flush because it'll happen
2637 * again at bind time.
2638 */
Eric Anholt856fa192009-03-19 14:10:50 -07002639 if (obj_priv->pages == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002640 return;
2641
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002642 trace_i915_gem_object_clflush(obj);
Eric Anholtcfa16a02009-05-26 18:46:16 -07002643
Eric Anholt856fa192009-03-19 14:10:50 -07002644 drm_clflush_pages(obj_priv->pages, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002645}
2646
Eric Anholte47c68e2008-11-14 13:35:19 -08002647/** Flushes any GPU write domain for the object if it's dirty. */
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002648static int
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002649i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj,
2650 bool pipelined)
Eric Anholte47c68e2008-11-14 13:35:19 -08002651{
2652 struct drm_device *dev = obj->dev;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002653 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002654
2655 if ((obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002656 return 0;
Eric Anholte47c68e2008-11-14 13:35:19 -08002657
2658 /* Queue the GPU write cache flushing we need. */
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002659 old_write_domain = obj->write_domain;
Chris Wilsonc78ec302010-09-20 12:50:23 +01002660 i915_gem_flush_ring(dev, NULL,
Chris Wilson92204342010-09-18 11:02:01 +01002661 to_intel_bo(obj)->ring,
2662 0, obj->write_domain);
Chris Wilson48b956c2010-09-14 12:50:34 +01002663 BUG_ON(obj->write_domain);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002664
2665 trace_i915_gem_object_change_domain(obj,
2666 obj->read_domains,
2667 old_write_domain);
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002668
2669 if (pipelined)
2670 return 0;
2671
Chris Wilson2cf34d72010-09-14 13:03:28 +01002672 return i915_gem_object_wait_rendering(obj, true);
Eric Anholte47c68e2008-11-14 13:35:19 -08002673}
2674
2675/** Flushes the GTT write domain for the object if it's dirty. */
2676static void
2677i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj)
2678{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002679 uint32_t old_write_domain;
2680
Eric Anholte47c68e2008-11-14 13:35:19 -08002681 if (obj->write_domain != I915_GEM_DOMAIN_GTT)
2682 return;
2683
2684 /* No actual flushing is required for the GTT write domain. Writes
2685 * to it immediately go to main memory as far as we know, so there's
2686 * no chipset flush. It also doesn't land in render cache.
2687 */
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002688 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002689 obj->write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002690
2691 trace_i915_gem_object_change_domain(obj,
2692 obj->read_domains,
2693 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002694}
2695
2696/** Flushes the CPU write domain for the object if it's dirty. */
2697static void
2698i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj)
2699{
2700 struct drm_device *dev = obj->dev;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002701 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002702
2703 if (obj->write_domain != I915_GEM_DOMAIN_CPU)
2704 return;
2705
2706 i915_gem_clflush_object(obj);
2707 drm_agp_chipset_flush(dev);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002708 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002709 obj->write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002710
2711 trace_i915_gem_object_change_domain(obj,
2712 obj->read_domains,
2713 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002714}
2715
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002716/**
2717 * Moves a single object to the GTT read, and possibly write domain.
2718 *
2719 * This function returns when the move is complete, including waiting on
2720 * flushes to occur.
2721 */
Jesse Barnes79e53942008-11-07 14:24:08 -08002722int
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002723i915_gem_object_set_to_gtt_domain(struct drm_gem_object *obj, int write)
2724{
Daniel Vetter23010e42010-03-08 13:35:02 +01002725 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002726 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002727 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002728
Eric Anholt02354392008-11-26 13:58:13 -08002729 /* Not valid to be called on unbound objects. */
2730 if (obj_priv->gtt_space == NULL)
2731 return -EINVAL;
2732
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002733 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Eric Anholte47c68e2008-11-14 13:35:19 -08002734 if (ret != 0)
2735 return ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002736
Chris Wilson72133422010-09-13 23:56:38 +01002737 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002738
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002739 if (write) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01002740 ret = i915_gem_object_wait_rendering(obj, true);
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002741 if (ret)
2742 return ret;
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002743 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002744
Chris Wilson72133422010-09-13 23:56:38 +01002745 old_write_domain = obj->write_domain;
2746 old_read_domains = obj->read_domains;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002747
2748 /* It should now be out of any other write domains, and we can update
2749 * the domain values for our changes.
2750 */
2751 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2752 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002753 if (write) {
Chris Wilson72133422010-09-13 23:56:38 +01002754 obj->read_domains = I915_GEM_DOMAIN_GTT;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002755 obj->write_domain = I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002756 obj_priv->dirty = 1;
2757 }
2758
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002759 trace_i915_gem_object_change_domain(obj,
2760 old_read_domains,
2761 old_write_domain);
2762
Eric Anholte47c68e2008-11-14 13:35:19 -08002763 return 0;
2764}
2765
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002766/*
2767 * Prepare buffer for display plane. Use uninterruptible for possible flush
2768 * wait, as in modesetting process we're not supposed to be interrupted.
2769 */
2770int
Chris Wilson48b956c2010-09-14 12:50:34 +01002771i915_gem_object_set_to_display_plane(struct drm_gem_object *obj,
2772 bool pipelined)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002773{
Daniel Vetter23010e42010-03-08 13:35:02 +01002774 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002775 uint32_t old_read_domains;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002776 int ret;
2777
2778 /* Not valid to be called on unbound objects. */
2779 if (obj_priv->gtt_space == NULL)
2780 return -EINVAL;
2781
Chris Wilson48b956c2010-09-14 12:50:34 +01002782 ret = i915_gem_object_flush_gpu_write_domain(obj, pipelined);
2783 if (ret)
Daniel Vettere35a41d2010-02-11 22:13:59 +01002784 return ret;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002785
Chris Wilsonb118c1e2010-05-27 13:18:14 +01002786 i915_gem_object_flush_cpu_write_domain(obj);
2787
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002788 old_read_domains = obj->read_domains;
Chris Wilsonc78ec302010-09-20 12:50:23 +01002789 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002790
2791 trace_i915_gem_object_change_domain(obj,
2792 old_read_domains,
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002793 obj->write_domain);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002794
2795 return 0;
2796}
2797
Eric Anholte47c68e2008-11-14 13:35:19 -08002798/**
2799 * Moves a single object to the CPU read, and possibly write domain.
2800 *
2801 * This function returns when the move is complete, including waiting on
2802 * flushes to occur.
2803 */
2804static int
2805i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj, int write)
2806{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002807 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002808 int ret;
2809
Daniel Vetterba3d8d72010-02-11 22:37:04 +01002810 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Eric Anholte47c68e2008-11-14 13:35:19 -08002811 if (ret != 0)
2812 return ret;
2813
2814 i915_gem_object_flush_gtt_write_domain(obj);
2815
2816 /* If we have a partially-valid cache of the object in the CPU,
2817 * finish invalidating it and free the per-page flags.
2818 */
2819 i915_gem_object_set_to_full_cpu_read_domain(obj);
2820
Chris Wilson72133422010-09-13 23:56:38 +01002821 if (write) {
Chris Wilson2cf34d72010-09-14 13:03:28 +01002822 ret = i915_gem_object_wait_rendering(obj, true);
Chris Wilson72133422010-09-13 23:56:38 +01002823 if (ret)
2824 return ret;
2825 }
2826
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002827 old_write_domain = obj->write_domain;
2828 old_read_domains = obj->read_domains;
2829
Eric Anholte47c68e2008-11-14 13:35:19 -08002830 /* Flush the CPU cache if it's still invalid. */
2831 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
2832 i915_gem_clflush_object(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08002833
2834 obj->read_domains |= I915_GEM_DOMAIN_CPU;
2835 }
2836
2837 /* It should now be out of any other write domains, and we can update
2838 * the domain values for our changes.
2839 */
2840 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
2841
2842 /* If we're writing through the CPU, then the GPU read domains will
2843 * need to be invalidated at next use.
2844 */
2845 if (write) {
Chris Wilsonc78ec302010-09-20 12:50:23 +01002846 obj->read_domains = I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08002847 obj->write_domain = I915_GEM_DOMAIN_CPU;
2848 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002849
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002850 trace_i915_gem_object_change_domain(obj,
2851 old_read_domains,
2852 old_write_domain);
2853
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002854 return 0;
2855}
2856
Eric Anholt673a3942008-07-30 12:06:12 -07002857/*
2858 * Set the next domain for the specified object. This
2859 * may not actually perform the necessary flushing/invaliding though,
2860 * as that may want to be batched with other set_domain operations
2861 *
2862 * This is (we hope) the only really tricky part of gem. The goal
2863 * is fairly simple -- track which caches hold bits of the object
2864 * and make sure they remain coherent. A few concrete examples may
2865 * help to explain how it works. For shorthand, we use the notation
2866 * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
2867 * a pair of read and write domain masks.
2868 *
2869 * Case 1: the batch buffer
2870 *
2871 * 1. Allocated
2872 * 2. Written by CPU
2873 * 3. Mapped to GTT
2874 * 4. Read by GPU
2875 * 5. Unmapped from GTT
2876 * 6. Freed
2877 *
2878 * Let's take these a step at a time
2879 *
2880 * 1. Allocated
2881 * Pages allocated from the kernel may still have
2882 * cache contents, so we set them to (CPU, CPU) always.
2883 * 2. Written by CPU (using pwrite)
2884 * The pwrite function calls set_domain (CPU, CPU) and
2885 * this function does nothing (as nothing changes)
2886 * 3. Mapped by GTT
2887 * This function asserts that the object is not
2888 * currently in any GPU-based read or write domains
2889 * 4. Read by GPU
2890 * i915_gem_execbuffer calls set_domain (COMMAND, 0).
2891 * As write_domain is zero, this function adds in the
2892 * current read domains (CPU+COMMAND, 0).
2893 * flush_domains is set to CPU.
2894 * invalidate_domains is set to COMMAND
2895 * clflush is run to get data out of the CPU caches
2896 * then i915_dev_set_domain calls i915_gem_flush to
2897 * emit an MI_FLUSH and drm_agp_chipset_flush
2898 * 5. Unmapped from GTT
2899 * i915_gem_object_unbind calls set_domain (CPU, CPU)
2900 * flush_domains and invalidate_domains end up both zero
2901 * so no flushing/invalidating happens
2902 * 6. Freed
2903 * yay, done
2904 *
2905 * Case 2: The shared render buffer
2906 *
2907 * 1. Allocated
2908 * 2. Mapped to GTT
2909 * 3. Read/written by GPU
2910 * 4. set_domain to (CPU,CPU)
2911 * 5. Read/written by CPU
2912 * 6. Read/written by GPU
2913 *
2914 * 1. Allocated
2915 * Same as last example, (CPU, CPU)
2916 * 2. Mapped to GTT
2917 * Nothing changes (assertions find that it is not in the GPU)
2918 * 3. Read/written by GPU
2919 * execbuffer calls set_domain (RENDER, RENDER)
2920 * flush_domains gets CPU
2921 * invalidate_domains gets GPU
2922 * clflush (obj)
2923 * MI_FLUSH and drm_agp_chipset_flush
2924 * 4. set_domain (CPU, CPU)
2925 * flush_domains gets GPU
2926 * invalidate_domains gets CPU
2927 * wait_rendering (obj) to make sure all drawing is complete.
2928 * This will include an MI_FLUSH to get the data from GPU
2929 * to memory
2930 * clflush (obj) to invalidate the CPU cache
2931 * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
2932 * 5. Read/written by CPU
2933 * cache lines are loaded and dirtied
2934 * 6. Read written by GPU
2935 * Same as last GPU access
2936 *
2937 * Case 3: The constant buffer
2938 *
2939 * 1. Allocated
2940 * 2. Written by CPU
2941 * 3. Read by GPU
2942 * 4. Updated (written) by CPU again
2943 * 5. Read by GPU
2944 *
2945 * 1. Allocated
2946 * (CPU, CPU)
2947 * 2. Written by CPU
2948 * (CPU, CPU)
2949 * 3. Read by GPU
2950 * (CPU+RENDER, 0)
2951 * flush_domains = CPU
2952 * invalidate_domains = RENDER
2953 * clflush (obj)
2954 * MI_FLUSH
2955 * drm_agp_chipset_flush
2956 * 4. Updated (written) by CPU again
2957 * (CPU, CPU)
2958 * flush_domains = 0 (no previous write domain)
2959 * invalidate_domains = 0 (no new read domains)
2960 * 5. Read by GPU
2961 * (CPU+RENDER, 0)
2962 * flush_domains = CPU
2963 * invalidate_domains = RENDER
2964 * clflush (obj)
2965 * MI_FLUSH
2966 * drm_agp_chipset_flush
2967 */
Keith Packardc0d90822008-11-20 23:11:08 -08002968static void
Eric Anholt8b0e3782009-02-19 14:40:50 -08002969i915_gem_object_set_to_gpu_domain(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002970{
2971 struct drm_device *dev = obj->dev;
Chris Wilson92204342010-09-18 11:02:01 +01002972 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002973 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002974 uint32_t invalidate_domains = 0;
2975 uint32_t flush_domains = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002976 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002977
Eric Anholt8b0e3782009-02-19 14:40:50 -08002978 BUG_ON(obj->pending_read_domains & I915_GEM_DOMAIN_CPU);
2979 BUG_ON(obj->pending_write_domain == I915_GEM_DOMAIN_CPU);
Eric Anholt673a3942008-07-30 12:06:12 -07002980
Jesse Barnes652c3932009-08-17 13:31:43 -07002981 intel_mark_busy(dev, obj);
2982
Eric Anholt673a3942008-07-30 12:06:12 -07002983#if WATCH_BUF
2984 DRM_INFO("%s: object %p read %08x -> %08x write %08x -> %08x\n",
2985 __func__, obj,
Eric Anholt8b0e3782009-02-19 14:40:50 -08002986 obj->read_domains, obj->pending_read_domains,
2987 obj->write_domain, obj->pending_write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07002988#endif
2989 /*
2990 * If the object isn't moving to a new write domain,
2991 * let the object stay in multiple read domains
2992 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08002993 if (obj->pending_write_domain == 0)
2994 obj->pending_read_domains |= obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07002995 else
2996 obj_priv->dirty = 1;
2997
2998 /*
2999 * Flush the current write domain if
3000 * the new read domains don't match. Invalidate
3001 * any read domains which differ from the old
3002 * write domain
3003 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003004 if (obj->write_domain &&
3005 obj->write_domain != obj->pending_read_domains) {
Eric Anholt673a3942008-07-30 12:06:12 -07003006 flush_domains |= obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08003007 invalidate_domains |=
3008 obj->pending_read_domains & ~obj->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07003009 }
3010 /*
3011 * Invalidate any read caches which may have
3012 * stale data. That is, any new read domains.
3013 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003014 invalidate_domains |= obj->pending_read_domains & ~obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07003015 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU) {
3016#if WATCH_BUF
3017 DRM_INFO("%s: CPU domain flush %08x invalidate %08x\n",
3018 __func__, flush_domains, invalidate_domains);
3019#endif
Eric Anholt673a3942008-07-30 12:06:12 -07003020 i915_gem_clflush_object(obj);
3021 }
3022
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003023 old_read_domains = obj->read_domains;
3024
Eric Anholtefbeed92009-02-19 14:54:51 -08003025 /* The actual obj->write_domain will be updated with
3026 * pending_write_domain after we emit the accumulated flush for all
3027 * of our domain changes in execbuffers (which clears objects'
3028 * write_domains). So if we have a current write domain that we
3029 * aren't changing, set pending_write_domain to that.
3030 */
3031 if (flush_domains == 0 && obj->pending_write_domain == 0)
3032 obj->pending_write_domain = obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08003033 obj->read_domains = obj->pending_read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07003034
3035 dev->invalidate_domains |= invalidate_domains;
3036 dev->flush_domains |= flush_domains;
Chris Wilson92204342010-09-18 11:02:01 +01003037 if (obj_priv->ring)
3038 dev_priv->mm.flush_rings |= obj_priv->ring->id;
Eric Anholt673a3942008-07-30 12:06:12 -07003039#if WATCH_BUF
3040 DRM_INFO("%s: read %08x write %08x invalidate %08x flush %08x\n",
3041 __func__,
3042 obj->read_domains, obj->write_domain,
3043 dev->invalidate_domains, dev->flush_domains);
3044#endif
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003045
3046 trace_i915_gem_object_change_domain(obj,
3047 old_read_domains,
3048 obj->write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07003049}
3050
3051/**
Eric Anholte47c68e2008-11-14 13:35:19 -08003052 * Moves the object from a partially CPU read to a full one.
Eric Anholt673a3942008-07-30 12:06:12 -07003053 *
Eric Anholte47c68e2008-11-14 13:35:19 -08003054 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
3055 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
3056 */
3057static void
3058i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj)
3059{
Daniel Vetter23010e42010-03-08 13:35:02 +01003060 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003061
3062 if (!obj_priv->page_cpu_valid)
3063 return;
3064
3065 /* If we're partially in the CPU read domain, finish moving it in.
3066 */
3067 if (obj->read_domains & I915_GEM_DOMAIN_CPU) {
3068 int i;
3069
3070 for (i = 0; i <= (obj->size - 1) / PAGE_SIZE; i++) {
3071 if (obj_priv->page_cpu_valid[i])
3072 continue;
Eric Anholt856fa192009-03-19 14:10:50 -07003073 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholte47c68e2008-11-14 13:35:19 -08003074 }
Eric Anholte47c68e2008-11-14 13:35:19 -08003075 }
3076
3077 /* Free the page_cpu_valid mappings which are now stale, whether
3078 * or not we've got I915_GEM_DOMAIN_CPU.
3079 */
Eric Anholt9a298b22009-03-24 12:23:04 -07003080 kfree(obj_priv->page_cpu_valid);
Eric Anholte47c68e2008-11-14 13:35:19 -08003081 obj_priv->page_cpu_valid = NULL;
3082}
3083
3084/**
3085 * Set the CPU read domain on a range of the object.
3086 *
3087 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
3088 * not entirely valid. The page_cpu_valid member of the object flags which
3089 * pages have been flushed, and will be respected by
3090 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
3091 * of the whole object.
3092 *
3093 * This function returns when the move is complete, including waiting on
3094 * flushes to occur.
Eric Anholt673a3942008-07-30 12:06:12 -07003095 */
3096static int
Eric Anholte47c68e2008-11-14 13:35:19 -08003097i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
3098 uint64_t offset, uint64_t size)
Eric Anholt673a3942008-07-30 12:06:12 -07003099{
Daniel Vetter23010e42010-03-08 13:35:02 +01003100 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003101 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003102 int i, ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003103
Eric Anholte47c68e2008-11-14 13:35:19 -08003104 if (offset == 0 && size == obj->size)
3105 return i915_gem_object_set_to_cpu_domain(obj, 0);
3106
Daniel Vetterba3d8d72010-02-11 22:37:04 +01003107 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
Eric Anholte47c68e2008-11-14 13:35:19 -08003108 if (ret != 0)
3109 return ret;
3110 i915_gem_object_flush_gtt_write_domain(obj);
3111
3112 /* If we're already fully in the CPU read domain, we're done. */
3113 if (obj_priv->page_cpu_valid == NULL &&
3114 (obj->read_domains & I915_GEM_DOMAIN_CPU) != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07003115 return 0;
3116
Eric Anholte47c68e2008-11-14 13:35:19 -08003117 /* Otherwise, create/clear the per-page CPU read domain flag if we're
3118 * newly adding I915_GEM_DOMAIN_CPU
3119 */
Eric Anholt673a3942008-07-30 12:06:12 -07003120 if (obj_priv->page_cpu_valid == NULL) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003121 obj_priv->page_cpu_valid = kzalloc(obj->size / PAGE_SIZE,
3122 GFP_KERNEL);
Eric Anholte47c68e2008-11-14 13:35:19 -08003123 if (obj_priv->page_cpu_valid == NULL)
3124 return -ENOMEM;
3125 } else if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0)
3126 memset(obj_priv->page_cpu_valid, 0, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07003127
3128 /* Flush the cache on any pages that are still invalid from the CPU's
3129 * perspective.
3130 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003131 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
3132 i++) {
Eric Anholt673a3942008-07-30 12:06:12 -07003133 if (obj_priv->page_cpu_valid[i])
3134 continue;
3135
Eric Anholt856fa192009-03-19 14:10:50 -07003136 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07003137
3138 obj_priv->page_cpu_valid[i] = 1;
3139 }
3140
Eric Anholte47c68e2008-11-14 13:35:19 -08003141 /* It should now be out of any other write domains, and we can update
3142 * the domain values for our changes.
3143 */
3144 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
3145
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003146 old_read_domains = obj->read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003147 obj->read_domains |= I915_GEM_DOMAIN_CPU;
3148
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003149 trace_i915_gem_object_change_domain(obj,
3150 old_read_domains,
3151 obj->write_domain);
3152
Eric Anholt673a3942008-07-30 12:06:12 -07003153 return 0;
3154}
3155
3156/**
Eric Anholt673a3942008-07-30 12:06:12 -07003157 * Pin an object to the GTT and evaluate the relocations landing in it.
3158 */
3159static int
3160i915_gem_object_pin_and_relocate(struct drm_gem_object *obj,
3161 struct drm_file *file_priv,
Jesse Barnes76446ca2009-12-17 22:05:42 -05003162 struct drm_i915_gem_exec_object2 *entry,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003163 struct drm_i915_gem_relocation_entry *relocs)
Eric Anholt673a3942008-07-30 12:06:12 -07003164{
3165 struct drm_device *dev = obj->dev;
Keith Packard0839ccb2008-10-30 19:38:48 -07003166 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01003167 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003168 int i, ret;
Keith Packard0839ccb2008-10-30 19:38:48 -07003169 void __iomem *reloc_page;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003170 bool need_fence;
3171
3172 need_fence = entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
3173 obj_priv->tiling_mode != I915_TILING_NONE;
3174
3175 /* Check fence reg constraints and rebind if necessary */
Chris Wilson808b24d62010-05-27 13:18:15 +01003176 if (need_fence &&
3177 !i915_gem_object_fence_offset_ok(obj,
3178 obj_priv->tiling_mode)) {
3179 ret = i915_gem_object_unbind(obj);
3180 if (ret)
3181 return ret;
3182 }
Eric Anholt673a3942008-07-30 12:06:12 -07003183
3184 /* Choose the GTT offset for our buffer and put it there. */
3185 ret = i915_gem_object_pin(obj, (uint32_t) entry->alignment);
3186 if (ret)
3187 return ret;
3188
Jesse Barnes76446ca2009-12-17 22:05:42 -05003189 /*
3190 * Pre-965 chips need a fence register set up in order to
3191 * properly handle blits to/from tiled surfaces.
3192 */
3193 if (need_fence) {
Chris Wilson53640e12010-09-20 11:40:50 +01003194 ret = i915_gem_object_get_fence_reg(obj, true);
Jesse Barnes76446ca2009-12-17 22:05:42 -05003195 if (ret != 0) {
Jesse Barnes76446ca2009-12-17 22:05:42 -05003196 i915_gem_object_unpin(obj);
3197 return ret;
3198 }
Chris Wilson53640e12010-09-20 11:40:50 +01003199
3200 dev_priv->fence_regs[obj_priv->fence_reg].gpu = true;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003201 }
3202
Eric Anholt673a3942008-07-30 12:06:12 -07003203 entry->offset = obj_priv->gtt_offset;
3204
Eric Anholt673a3942008-07-30 12:06:12 -07003205 /* Apply the relocations, using the GTT aperture to avoid cache
3206 * flushing requirements.
3207 */
3208 for (i = 0; i < entry->relocation_count; i++) {
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003209 struct drm_i915_gem_relocation_entry *reloc= &relocs[i];
Eric Anholt673a3942008-07-30 12:06:12 -07003210 struct drm_gem_object *target_obj;
3211 struct drm_i915_gem_object *target_obj_priv;
Eric Anholt3043c602008-10-02 12:24:47 -07003212 uint32_t reloc_val, reloc_offset;
3213 uint32_t __iomem *reloc_entry;
Eric Anholt673a3942008-07-30 12:06:12 -07003214
Eric Anholt673a3942008-07-30 12:06:12 -07003215 target_obj = drm_gem_object_lookup(obj->dev, file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003216 reloc->target_handle);
Eric Anholt673a3942008-07-30 12:06:12 -07003217 if (target_obj == NULL) {
3218 i915_gem_object_unpin(obj);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01003219 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07003220 }
Daniel Vetter23010e42010-03-08 13:35:02 +01003221 target_obj_priv = to_intel_bo(target_obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003222
Chris Wilson8542a0b2009-09-09 21:15:15 +01003223#if WATCH_RELOC
3224 DRM_INFO("%s: obj %p offset %08x target %d "
3225 "read %08x write %08x gtt %08x "
3226 "presumed %08x delta %08x\n",
3227 __func__,
3228 obj,
3229 (int) reloc->offset,
3230 (int) reloc->target_handle,
3231 (int) reloc->read_domains,
3232 (int) reloc->write_domain,
3233 (int) target_obj_priv->gtt_offset,
3234 (int) reloc->presumed_offset,
3235 reloc->delta);
3236#endif
3237
Eric Anholt673a3942008-07-30 12:06:12 -07003238 /* The target buffer should have appeared before us in the
3239 * exec_object list, so it should have a GTT space bound by now.
3240 */
3241 if (target_obj_priv->gtt_space == NULL) {
3242 DRM_ERROR("No GTT space found for object %d\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003243 reloc->target_handle);
Eric Anholt673a3942008-07-30 12:06:12 -07003244 drm_gem_object_unreference(target_obj);
3245 i915_gem_object_unpin(obj);
3246 return -EINVAL;
3247 }
3248
Chris Wilson8542a0b2009-09-09 21:15:15 +01003249 /* Validate that the target is in a valid r/w GPU domain */
Daniel Vetter16edd552010-02-19 11:52:02 +01003250 if (reloc->write_domain & (reloc->write_domain - 1)) {
3251 DRM_ERROR("reloc with multiple write domains: "
3252 "obj %p target %d offset %d "
3253 "read %08x write %08x",
3254 obj, reloc->target_handle,
3255 (int) reloc->offset,
3256 reloc->read_domains,
3257 reloc->write_domain);
3258 return -EINVAL;
3259 }
Chris Wilson8542a0b2009-09-09 21:15:15 +01003260 if (reloc->write_domain & I915_GEM_DOMAIN_CPU ||
3261 reloc->read_domains & I915_GEM_DOMAIN_CPU) {
3262 DRM_ERROR("reloc with read/write CPU domains: "
3263 "obj %p target %d offset %d "
3264 "read %08x write %08x",
3265 obj, reloc->target_handle,
3266 (int) reloc->offset,
3267 reloc->read_domains,
3268 reloc->write_domain);
3269 drm_gem_object_unreference(target_obj);
3270 i915_gem_object_unpin(obj);
3271 return -EINVAL;
3272 }
3273 if (reloc->write_domain && target_obj->pending_write_domain &&
3274 reloc->write_domain != target_obj->pending_write_domain) {
3275 DRM_ERROR("Write domain conflict: "
3276 "obj %p target %d offset %d "
3277 "new %08x old %08x\n",
3278 obj, reloc->target_handle,
3279 (int) reloc->offset,
3280 reloc->write_domain,
3281 target_obj->pending_write_domain);
3282 drm_gem_object_unreference(target_obj);
3283 i915_gem_object_unpin(obj);
3284 return -EINVAL;
3285 }
3286
3287 target_obj->pending_read_domains |= reloc->read_domains;
3288 target_obj->pending_write_domain |= reloc->write_domain;
3289
3290 /* If the relocation already has the right value in it, no
3291 * more work needs to be done.
3292 */
3293 if (target_obj_priv->gtt_offset == reloc->presumed_offset) {
3294 drm_gem_object_unreference(target_obj);
3295 continue;
3296 }
3297
3298 /* Check that the relocation address is valid... */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003299 if (reloc->offset > obj->size - 4) {
Eric Anholt673a3942008-07-30 12:06:12 -07003300 DRM_ERROR("Relocation beyond object bounds: "
3301 "obj %p target %d offset %d size %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003302 obj, reloc->target_handle,
3303 (int) reloc->offset, (int) obj->size);
Eric Anholt673a3942008-07-30 12:06:12 -07003304 drm_gem_object_unreference(target_obj);
3305 i915_gem_object_unpin(obj);
3306 return -EINVAL;
3307 }
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003308 if (reloc->offset & 3) {
Eric Anholt673a3942008-07-30 12:06:12 -07003309 DRM_ERROR("Relocation not 4-byte aligned: "
3310 "obj %p target %d offset %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003311 obj, reloc->target_handle,
3312 (int) reloc->offset);
Eric Anholt673a3942008-07-30 12:06:12 -07003313 drm_gem_object_unreference(target_obj);
3314 i915_gem_object_unpin(obj);
3315 return -EINVAL;
3316 }
3317
Chris Wilson8542a0b2009-09-09 21:15:15 +01003318 /* and points to somewhere within the target object. */
Chris Wilsoncd0b9fb2009-09-15 23:23:18 +01003319 if (reloc->delta >= target_obj->size) {
3320 DRM_ERROR("Relocation beyond target object bounds: "
3321 "obj %p target %d delta %d size %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003322 obj, reloc->target_handle,
Chris Wilsoncd0b9fb2009-09-15 23:23:18 +01003323 (int) reloc->delta, (int) target_obj->size);
Chris Wilson491152b2009-02-11 14:26:32 +00003324 drm_gem_object_unreference(target_obj);
3325 i915_gem_object_unpin(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003326 return -EINVAL;
3327 }
3328
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003329 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
3330 if (ret != 0) {
3331 drm_gem_object_unreference(target_obj);
3332 i915_gem_object_unpin(obj);
3333 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -07003334 }
3335
3336 /* Map the page containing the relocation we're going to
3337 * perform.
3338 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003339 reloc_offset = obj_priv->gtt_offset + reloc->offset;
Keith Packard0839ccb2008-10-30 19:38:48 -07003340 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
3341 (reloc_offset &
Chris Wilsonfca3ec02010-08-04 14:34:24 +01003342 ~(PAGE_SIZE - 1)),
3343 KM_USER0);
Eric Anholt3043c602008-10-02 12:24:47 -07003344 reloc_entry = (uint32_t __iomem *)(reloc_page +
Keith Packard0839ccb2008-10-30 19:38:48 -07003345 (reloc_offset & (PAGE_SIZE - 1)));
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003346 reloc_val = target_obj_priv->gtt_offset + reloc->delta;
Eric Anholt673a3942008-07-30 12:06:12 -07003347
3348#if WATCH_BUF
3349 DRM_INFO("Applied relocation: %p@0x%08x %08x -> %08x\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003350 obj, (unsigned int) reloc->offset,
Eric Anholt673a3942008-07-30 12:06:12 -07003351 readl(reloc_entry), reloc_val);
3352#endif
3353 writel(reloc_val, reloc_entry);
Chris Wilsonfca3ec02010-08-04 14:34:24 +01003354 io_mapping_unmap_atomic(reloc_page, KM_USER0);
Eric Anholt673a3942008-07-30 12:06:12 -07003355
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003356 /* The updated presumed offset for this entry will be
3357 * copied back out to the user.
Eric Anholt673a3942008-07-30 12:06:12 -07003358 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003359 reloc->presumed_offset = target_obj_priv->gtt_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003360
3361 drm_gem_object_unreference(target_obj);
3362 }
3363
Eric Anholt673a3942008-07-30 12:06:12 -07003364#if WATCH_BUF
3365 if (0)
3366 i915_gem_dump_object(obj, 128, __func__, ~0);
3367#endif
3368 return 0;
3369}
3370
Eric Anholt673a3942008-07-30 12:06:12 -07003371/* Throttle our rendering by waiting until the ring has completed our requests
3372 * emitted over 20 msec ago.
3373 *
Eric Anholtb9624422009-06-03 07:27:35 +00003374 * Note that if we were to use the current jiffies each time around the loop,
3375 * we wouldn't escape the function with any frames outstanding if the time to
3376 * render a frame was over 20ms.
3377 *
Eric Anholt673a3942008-07-30 12:06:12 -07003378 * This should get us reasonable parallelism between CPU and GPU but also
3379 * relatively low latency when blocking on a particular request to finish.
3380 */
3381static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003382i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003383{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003384 struct drm_i915_private *dev_priv = dev->dev_private;
3385 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00003386 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003387 struct drm_i915_gem_request *request;
3388 struct intel_ring_buffer *ring = NULL;
3389 u32 seqno = 0;
3390 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003391
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003392 mutex_lock(&file_priv->mutex);
3393 list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
Eric Anholtb9624422009-06-03 07:27:35 +00003394 if (time_after_eq(request->emitted_jiffies, recent_enough))
3395 break;
3396
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003397 ring = request->ring;
3398 seqno = request->seqno;
Eric Anholtb9624422009-06-03 07:27:35 +00003399 }
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003400 mutex_unlock(&file_priv->mutex);
3401
3402 if (seqno == 0)
3403 return 0;
3404
3405 ret = 0;
3406 if (!i915_seqno_passed(ring->get_seqno(dev, ring), seqno)) {
3407 /* And wait for the seqno passing without holding any locks and
3408 * causing extra latency for others. This is safe as the irq
3409 * generation is designed to be run atomically and so is
3410 * lockless.
3411 */
3412 ring->user_irq_get(dev, ring);
3413 ret = wait_event_interruptible(ring->irq_queue,
3414 i915_seqno_passed(ring->get_seqno(dev, ring), seqno)
3415 || atomic_read(&dev_priv->mm.wedged));
3416 ring->user_irq_put(dev, ring);
3417
3418 if (ret == 0 && atomic_read(&dev_priv->mm.wedged))
3419 ret = -EIO;
3420 }
3421
3422 if (ret == 0)
3423 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
Eric Anholtb9624422009-06-03 07:27:35 +00003424
Eric Anholt673a3942008-07-30 12:06:12 -07003425 return ret;
3426}
3427
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003428static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003429i915_gem_get_relocs_from_user(struct drm_i915_gem_exec_object2 *exec_list,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003430 uint32_t buffer_count,
3431 struct drm_i915_gem_relocation_entry **relocs)
3432{
3433 uint32_t reloc_count = 0, reloc_index = 0, i;
3434 int ret;
3435
3436 *relocs = NULL;
3437 for (i = 0; i < buffer_count; i++) {
3438 if (reloc_count + exec_list[i].relocation_count < reloc_count)
3439 return -EINVAL;
3440 reloc_count += exec_list[i].relocation_count;
3441 }
3442
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003443 *relocs = drm_calloc_large(reloc_count, sizeof(**relocs));
Jesse Barnes76446ca2009-12-17 22:05:42 -05003444 if (*relocs == NULL) {
3445 DRM_ERROR("failed to alloc relocs, count %d\n", reloc_count);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003446 return -ENOMEM;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003447 }
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003448
3449 for (i = 0; i < buffer_count; i++) {
3450 struct drm_i915_gem_relocation_entry __user *user_relocs;
3451
3452 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3453
3454 ret = copy_from_user(&(*relocs)[reloc_index],
3455 user_relocs,
3456 exec_list[i].relocation_count *
3457 sizeof(**relocs));
3458 if (ret != 0) {
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003459 drm_free_large(*relocs);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003460 *relocs = NULL;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003461 return -EFAULT;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003462 }
3463
3464 reloc_index += exec_list[i].relocation_count;
3465 }
3466
Florian Mickler2bc43b52009-04-06 22:55:41 +02003467 return 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003468}
3469
3470static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003471i915_gem_put_relocs_to_user(struct drm_i915_gem_exec_object2 *exec_list,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003472 uint32_t buffer_count,
3473 struct drm_i915_gem_relocation_entry *relocs)
3474{
3475 uint32_t reloc_count = 0, i;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003476 int ret = 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003477
Chris Wilson93533c22010-01-31 10:40:48 +00003478 if (relocs == NULL)
3479 return 0;
3480
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003481 for (i = 0; i < buffer_count; i++) {
3482 struct drm_i915_gem_relocation_entry __user *user_relocs;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003483 int unwritten;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003484
3485 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3486
Florian Mickler2bc43b52009-04-06 22:55:41 +02003487 unwritten = copy_to_user(user_relocs,
3488 &relocs[reloc_count],
3489 exec_list[i].relocation_count *
3490 sizeof(*relocs));
3491
3492 if (unwritten) {
3493 ret = -EFAULT;
3494 goto err;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003495 }
3496
3497 reloc_count += exec_list[i].relocation_count;
3498 }
3499
Florian Mickler2bc43b52009-04-06 22:55:41 +02003500err:
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003501 drm_free_large(relocs);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003502
3503 return ret;
3504}
3505
Chris Wilson83d60792009-06-06 09:45:57 +01003506static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003507i915_gem_check_execbuffer (struct drm_i915_gem_execbuffer2 *exec,
Chris Wilson83d60792009-06-06 09:45:57 +01003508 uint64_t exec_offset)
3509{
3510 uint32_t exec_start, exec_len;
3511
3512 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
3513 exec_len = (uint32_t) exec->batch_len;
3514
3515 if ((exec_start | exec_len) & 0x7)
3516 return -EINVAL;
3517
3518 if (!exec_start)
3519 return -EINVAL;
3520
3521 return 0;
3522}
3523
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003524static int
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003525i915_gem_wait_for_pending_flip(struct drm_device *dev,
3526 struct drm_gem_object **object_list,
3527 int count)
3528{
3529 drm_i915_private_t *dev_priv = dev->dev_private;
3530 struct drm_i915_gem_object *obj_priv;
3531 DEFINE_WAIT(wait);
3532 int i, ret = 0;
3533
3534 for (;;) {
3535 prepare_to_wait(&dev_priv->pending_flip_queue,
3536 &wait, TASK_INTERRUPTIBLE);
3537 for (i = 0; i < count; i++) {
Daniel Vetter23010e42010-03-08 13:35:02 +01003538 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003539 if (atomic_read(&obj_priv->pending_flip) > 0)
3540 break;
3541 }
3542 if (i == count)
3543 break;
3544
3545 if (!signal_pending(current)) {
3546 mutex_unlock(&dev->struct_mutex);
3547 schedule();
3548 mutex_lock(&dev->struct_mutex);
3549 continue;
3550 }
3551 ret = -ERESTARTSYS;
3552 break;
3553 }
3554 finish_wait(&dev_priv->pending_flip_queue, &wait);
3555
3556 return ret;
3557}
3558
Chris Wilson8dc5d142010-08-12 12:36:12 +01003559static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003560i915_gem_do_execbuffer(struct drm_device *dev, void *data,
3561 struct drm_file *file_priv,
3562 struct drm_i915_gem_execbuffer2 *args,
3563 struct drm_i915_gem_exec_object2 *exec_list)
Eric Anholt673a3942008-07-30 12:06:12 -07003564{
3565 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003566 struct drm_gem_object **object_list = NULL;
3567 struct drm_gem_object *batch_obj;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003568 struct drm_i915_gem_object *obj_priv;
Eric Anholt201361a2009-03-11 12:30:04 -07003569 struct drm_clip_rect *cliprects = NULL;
Chris Wilson93533c22010-01-31 10:40:48 +00003570 struct drm_i915_gem_relocation_entry *relocs = NULL;
Chris Wilson8dc5d142010-08-12 12:36:12 +01003571 struct drm_i915_gem_request *request = NULL;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003572 int ret = 0, ret2, i, pinned = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003573 uint64_t exec_offset;
Chris Wilson5c12a07e2010-09-22 11:22:30 +01003574 uint32_t reloc_index;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003575 int pin_tries, flips;
Eric Anholt673a3942008-07-30 12:06:12 -07003576
Zou Nan hai852835f2010-05-21 09:08:56 +08003577 struct intel_ring_buffer *ring = NULL;
3578
Eric Anholt673a3942008-07-30 12:06:12 -07003579#if WATCH_EXEC
3580 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3581 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3582#endif
Zou Nan haid1b851f2010-05-21 09:08:57 +08003583 if (args->flags & I915_EXEC_BSD) {
3584 if (!HAS_BSD(dev)) {
3585 DRM_ERROR("execbuf with wrong flag\n");
3586 return -EINVAL;
3587 }
3588 ring = &dev_priv->bsd_ring;
3589 } else {
3590 ring = &dev_priv->render_ring;
3591 }
3592
Eric Anholt4f481ed2008-09-10 14:22:49 -07003593 if (args->buffer_count < 1) {
3594 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3595 return -EINVAL;
3596 }
Eric Anholtc8e0f932009-11-22 03:49:37 +01003597 object_list = drm_malloc_ab(sizeof(*object_list), args->buffer_count);
Jesse Barnes76446ca2009-12-17 22:05:42 -05003598 if (object_list == NULL) {
3599 DRM_ERROR("Failed to allocate object list for %d buffers\n",
Eric Anholt673a3942008-07-30 12:06:12 -07003600 args->buffer_count);
3601 ret = -ENOMEM;
3602 goto pre_mutex_err;
3603 }
Eric Anholt673a3942008-07-30 12:06:12 -07003604
Eric Anholt201361a2009-03-11 12:30:04 -07003605 if (args->num_cliprects != 0) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003606 cliprects = kcalloc(args->num_cliprects, sizeof(*cliprects),
3607 GFP_KERNEL);
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003608 if (cliprects == NULL) {
3609 ret = -ENOMEM;
Eric Anholt201361a2009-03-11 12:30:04 -07003610 goto pre_mutex_err;
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003611 }
Eric Anholt201361a2009-03-11 12:30:04 -07003612
3613 ret = copy_from_user(cliprects,
3614 (struct drm_clip_rect __user *)
3615 (uintptr_t) args->cliprects_ptr,
3616 sizeof(*cliprects) * args->num_cliprects);
3617 if (ret != 0) {
3618 DRM_ERROR("copy %d cliprects failed: %d\n",
3619 args->num_cliprects, ret);
Dan Carpenterc877cdc2010-06-23 19:03:01 +02003620 ret = -EFAULT;
Eric Anholt201361a2009-03-11 12:30:04 -07003621 goto pre_mutex_err;
3622 }
3623 }
3624
Chris Wilson8dc5d142010-08-12 12:36:12 +01003625 request = kzalloc(sizeof(*request), GFP_KERNEL);
3626 if (request == NULL) {
3627 ret = -ENOMEM;
3628 goto pre_mutex_err;
3629 }
3630
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003631 ret = i915_gem_get_relocs_from_user(exec_list, args->buffer_count,
3632 &relocs);
3633 if (ret != 0)
3634 goto pre_mutex_err;
3635
Eric Anholt673a3942008-07-30 12:06:12 -07003636 mutex_lock(&dev->struct_mutex);
3637
3638 i915_verify_inactive(dev, __FILE__, __LINE__);
3639
Ben Gamariba1234d2009-09-14 17:48:47 -04003640 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07003641 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003642 ret = -EIO;
3643 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003644 }
3645
3646 if (dev_priv->mm.suspended) {
Eric Anholt673a3942008-07-30 12:06:12 -07003647 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003648 ret = -EBUSY;
3649 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003650 }
3651
Keith Packardac94a962008-11-20 23:30:27 -08003652 /* Look up object handles */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003653 flips = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003654 for (i = 0; i < args->buffer_count; i++) {
3655 object_list[i] = drm_gem_object_lookup(dev, file_priv,
3656 exec_list[i].handle);
3657 if (object_list[i] == NULL) {
3658 DRM_ERROR("Invalid object handle %d at index %d\n",
3659 exec_list[i].handle, i);
Chris Wilson0ce907f2010-01-23 20:26:35 +00003660 /* prevent error path from reading uninitialized data */
3661 args->buffer_count = i + 1;
Chris Wilsonbf79cb92010-08-04 14:19:46 +01003662 ret = -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07003663 goto err;
3664 }
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003665
Daniel Vetter23010e42010-03-08 13:35:02 +01003666 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003667 if (obj_priv->in_execbuffer) {
3668 DRM_ERROR("Object %p appears more than once in object list\n",
3669 object_list[i]);
Chris Wilson0ce907f2010-01-23 20:26:35 +00003670 /* prevent error path from reading uninitialized data */
3671 args->buffer_count = i + 1;
Chris Wilsonbf79cb92010-08-04 14:19:46 +01003672 ret = -EINVAL;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003673 goto err;
3674 }
3675 obj_priv->in_execbuffer = true;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003676 flips += atomic_read(&obj_priv->pending_flip);
3677 }
3678
3679 if (flips > 0) {
3680 ret = i915_gem_wait_for_pending_flip(dev, object_list,
3681 args->buffer_count);
3682 if (ret)
3683 goto err;
Keith Packardac94a962008-11-20 23:30:27 -08003684 }
Eric Anholt673a3942008-07-30 12:06:12 -07003685
Keith Packardac94a962008-11-20 23:30:27 -08003686 /* Pin and relocate */
3687 for (pin_tries = 0; ; pin_tries++) {
3688 ret = 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003689 reloc_index = 0;
3690
Keith Packardac94a962008-11-20 23:30:27 -08003691 for (i = 0; i < args->buffer_count; i++) {
3692 object_list[i]->pending_read_domains = 0;
3693 object_list[i]->pending_write_domain = 0;
3694 ret = i915_gem_object_pin_and_relocate(object_list[i],
3695 file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003696 &exec_list[i],
3697 &relocs[reloc_index]);
Keith Packardac94a962008-11-20 23:30:27 -08003698 if (ret)
3699 break;
3700 pinned = i + 1;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003701 reloc_index += exec_list[i].relocation_count;
Keith Packardac94a962008-11-20 23:30:27 -08003702 }
3703 /* success */
3704 if (ret == 0)
3705 break;
3706
3707 /* error other than GTT full, or we've already tried again */
Chris Wilson2939e1f2009-06-06 09:46:03 +01003708 if (ret != -ENOSPC || pin_tries >= 1) {
Chris Wilson07f73f62009-09-14 16:50:30 +01003709 if (ret != -ERESTARTSYS) {
3710 unsigned long long total_size = 0;
Chris Wilson3d1cc472010-05-27 13:18:19 +01003711 int num_fences = 0;
3712 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson43b27f42010-07-02 08:57:15 +01003713 obj_priv = to_intel_bo(object_list[i]);
Chris Wilson3d1cc472010-05-27 13:18:19 +01003714
Chris Wilson07f73f62009-09-14 16:50:30 +01003715 total_size += object_list[i]->size;
Chris Wilson3d1cc472010-05-27 13:18:19 +01003716 num_fences +=
3717 exec_list[i].flags & EXEC_OBJECT_NEEDS_FENCE &&
3718 obj_priv->tiling_mode != I915_TILING_NONE;
3719 }
3720 DRM_ERROR("Failed to pin buffer %d of %d, total %llu bytes, %d fences: %d\n",
Chris Wilson07f73f62009-09-14 16:50:30 +01003721 pinned+1, args->buffer_count,
Chris Wilson3d1cc472010-05-27 13:18:19 +01003722 total_size, num_fences,
3723 ret);
Chris Wilson07f73f62009-09-14 16:50:30 +01003724 DRM_ERROR("%d objects [%d pinned], "
3725 "%d object bytes [%d pinned], "
3726 "%d/%d gtt bytes\n",
3727 atomic_read(&dev->object_count),
3728 atomic_read(&dev->pin_count),
3729 atomic_read(&dev->object_memory),
3730 atomic_read(&dev->pin_memory),
3731 atomic_read(&dev->gtt_memory),
3732 dev->gtt_total);
3733 }
Eric Anholt673a3942008-07-30 12:06:12 -07003734 goto err;
3735 }
Keith Packardac94a962008-11-20 23:30:27 -08003736
3737 /* unpin all of our buffers */
3738 for (i = 0; i < pinned; i++)
3739 i915_gem_object_unpin(object_list[i]);
Eric Anholtb1177632008-12-10 10:09:41 -08003740 pinned = 0;
Keith Packardac94a962008-11-20 23:30:27 -08003741
3742 /* evict everyone we can from the aperture */
3743 ret = i915_gem_evict_everything(dev);
Chris Wilson07f73f62009-09-14 16:50:30 +01003744 if (ret && ret != -ENOSPC)
Keith Packardac94a962008-11-20 23:30:27 -08003745 goto err;
Eric Anholt673a3942008-07-30 12:06:12 -07003746 }
3747
3748 /* Set the pending read domains for the batch buffer to COMMAND */
3749 batch_obj = object_list[args->buffer_count-1];
Chris Wilson5f26a2c2009-06-06 09:45:58 +01003750 if (batch_obj->pending_write_domain) {
3751 DRM_ERROR("Attempting to use self-modifying batch buffer\n");
3752 ret = -EINVAL;
3753 goto err;
3754 }
3755 batch_obj->pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
Eric Anholt673a3942008-07-30 12:06:12 -07003756
Chris Wilson83d60792009-06-06 09:45:57 +01003757 /* Sanity check the batch buffer, prior to moving objects */
3758 exec_offset = exec_list[args->buffer_count - 1].offset;
3759 ret = i915_gem_check_execbuffer (args, exec_offset);
3760 if (ret != 0) {
3761 DRM_ERROR("execbuf with invalid offset/length\n");
3762 goto err;
3763 }
3764
Eric Anholt673a3942008-07-30 12:06:12 -07003765 i915_verify_inactive(dev, __FILE__, __LINE__);
3766
Keith Packard646f0f62008-11-20 23:23:03 -08003767 /* Zero the global flush/invalidate flags. These
3768 * will be modified as new domains are computed
3769 * for each object
3770 */
3771 dev->invalidate_domains = 0;
3772 dev->flush_domains = 0;
Chris Wilson92204342010-09-18 11:02:01 +01003773 dev_priv->mm.flush_rings = 0;
Keith Packard646f0f62008-11-20 23:23:03 -08003774
Eric Anholt673a3942008-07-30 12:06:12 -07003775 for (i = 0; i < args->buffer_count; i++) {
3776 struct drm_gem_object *obj = object_list[i];
Eric Anholt673a3942008-07-30 12:06:12 -07003777
Keith Packard646f0f62008-11-20 23:23:03 -08003778 /* Compute new gpu domains and update invalidate/flush */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003779 i915_gem_object_set_to_gpu_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003780 }
3781
3782 i915_verify_inactive(dev, __FILE__, __LINE__);
3783
Keith Packard646f0f62008-11-20 23:23:03 -08003784 if (dev->invalidate_domains | dev->flush_domains) {
3785#if WATCH_EXEC
3786 DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
3787 __func__,
3788 dev->invalidate_domains,
3789 dev->flush_domains);
3790#endif
Chris Wilsonc78ec302010-09-20 12:50:23 +01003791 i915_gem_flush(dev, file_priv,
Keith Packard646f0f62008-11-20 23:23:03 -08003792 dev->invalidate_domains,
Chris Wilson92204342010-09-18 11:02:01 +01003793 dev->flush_domains,
3794 dev_priv->mm.flush_rings);
Daniel Vettera6910432010-02-02 17:08:37 +01003795 }
3796
Eric Anholtefbeed92009-02-19 14:54:51 -08003797 for (i = 0; i < args->buffer_count; i++) {
3798 struct drm_gem_object *obj = object_list[i];
Daniel Vetter23010e42010-03-08 13:35:02 +01003799 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003800 uint32_t old_write_domain = obj->write_domain;
Eric Anholtefbeed92009-02-19 14:54:51 -08003801
3802 obj->write_domain = obj->pending_write_domain;
Daniel Vetter99fcb762010-02-07 16:20:18 +01003803 if (obj->write_domain)
3804 list_move_tail(&obj_priv->gpu_write_list,
3805 &dev_priv->mm.gpu_write_list);
3806 else
3807 list_del_init(&obj_priv->gpu_write_list);
3808
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003809 trace_i915_gem_object_change_domain(obj,
3810 obj->read_domains,
3811 old_write_domain);
Eric Anholtefbeed92009-02-19 14:54:51 -08003812 }
3813
Eric Anholt673a3942008-07-30 12:06:12 -07003814 i915_verify_inactive(dev, __FILE__, __LINE__);
3815
3816#if WATCH_COHERENCY
3817 for (i = 0; i < args->buffer_count; i++) {
3818 i915_gem_object_check_coherency(object_list[i],
3819 exec_list[i].handle);
3820 }
3821#endif
3822
Eric Anholt673a3942008-07-30 12:06:12 -07003823#if WATCH_EXEC
Ben Gamari6911a9b2009-04-02 11:24:54 -07003824 i915_gem_dump_object(batch_obj,
Eric Anholt673a3942008-07-30 12:06:12 -07003825 args->batch_len,
3826 __func__,
3827 ~0);
3828#endif
3829
Eric Anholt673a3942008-07-30 12:06:12 -07003830 /* Exec the batchbuffer */
Zou Nan hai852835f2010-05-21 09:08:56 +08003831 ret = ring->dispatch_gem_execbuffer(dev, ring, args,
3832 cliprects, exec_offset);
Eric Anholt673a3942008-07-30 12:06:12 -07003833 if (ret) {
3834 DRM_ERROR("dispatch failed %d\n", ret);
3835 goto err;
3836 }
3837
3838 /*
3839 * Ensure that the commands in the batch buffer are
3840 * finished before the interrupt fires
3841 */
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01003842 i915_retire_commands(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07003843
3844 i915_verify_inactive(dev, __FILE__, __LINE__);
3845
Daniel Vetter617dbe22010-02-11 22:16:02 +01003846 for (i = 0; i < args->buffer_count; i++) {
3847 struct drm_gem_object *obj = object_list[i];
3848 obj_priv = to_intel_bo(obj);
3849
3850 i915_gem_object_move_to_active(obj, ring);
3851#if WATCH_LRU
3852 DRM_INFO("%s: move to exec list %p\n", __func__, obj);
3853#endif
3854 }
Chris Wilson5c12a07e2010-09-22 11:22:30 +01003855 i915_add_request(dev, file_priv, request, ring);
Chris Wilson8dc5d142010-08-12 12:36:12 +01003856 request = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07003857
Eric Anholt673a3942008-07-30 12:06:12 -07003858#if WATCH_LRU
3859 i915_dump_lru(dev, __func__);
3860#endif
3861
3862 i915_verify_inactive(dev, __FILE__, __LINE__);
3863
Eric Anholt673a3942008-07-30 12:06:12 -07003864err:
Julia Lawallaad87df2008-12-21 16:28:47 +01003865 for (i = 0; i < pinned; i++)
3866 i915_gem_object_unpin(object_list[i]);
Eric Anholt673a3942008-07-30 12:06:12 -07003867
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003868 for (i = 0; i < args->buffer_count; i++) {
3869 if (object_list[i]) {
Daniel Vetter23010e42010-03-08 13:35:02 +01003870 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003871 obj_priv->in_execbuffer = false;
3872 }
Julia Lawallaad87df2008-12-21 16:28:47 +01003873 drm_gem_object_unreference(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003874 }
Julia Lawallaad87df2008-12-21 16:28:47 +01003875
Eric Anholt673a3942008-07-30 12:06:12 -07003876 mutex_unlock(&dev->struct_mutex);
3877
Chris Wilson93533c22010-01-31 10:40:48 +00003878pre_mutex_err:
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003879 /* Copy the updated relocations out regardless of current error
3880 * state. Failure to update the relocs would mean that the next
3881 * time userland calls execbuf, it would do so with presumed offset
3882 * state that didn't match the actual object state.
3883 */
3884 ret2 = i915_gem_put_relocs_to_user(exec_list, args->buffer_count,
3885 relocs);
3886 if (ret2 != 0) {
3887 DRM_ERROR("Failed to copy relocations back out: %d\n", ret2);
3888
3889 if (ret == 0)
3890 ret = ret2;
3891 }
3892
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003893 drm_free_large(object_list);
Eric Anholt9a298b22009-03-24 12:23:04 -07003894 kfree(cliprects);
Chris Wilson8dc5d142010-08-12 12:36:12 +01003895 kfree(request);
Eric Anholt673a3942008-07-30 12:06:12 -07003896
3897 return ret;
3898}
3899
Jesse Barnes76446ca2009-12-17 22:05:42 -05003900/*
3901 * Legacy execbuffer just creates an exec2 list from the original exec object
3902 * list array and passes it to the real function.
3903 */
3904int
3905i915_gem_execbuffer(struct drm_device *dev, void *data,
3906 struct drm_file *file_priv)
3907{
3908 struct drm_i915_gem_execbuffer *args = data;
3909 struct drm_i915_gem_execbuffer2 exec2;
3910 struct drm_i915_gem_exec_object *exec_list = NULL;
3911 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
3912 int ret, i;
3913
3914#if WATCH_EXEC
3915 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3916 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3917#endif
3918
3919 if (args->buffer_count < 1) {
3920 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3921 return -EINVAL;
3922 }
3923
3924 /* Copy in the exec list from userland */
3925 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
3926 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
3927 if (exec_list == NULL || exec2_list == NULL) {
3928 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
3929 args->buffer_count);
3930 drm_free_large(exec_list);
3931 drm_free_large(exec2_list);
3932 return -ENOMEM;
3933 }
3934 ret = copy_from_user(exec_list,
3935 (struct drm_i915_relocation_entry __user *)
3936 (uintptr_t) args->buffers_ptr,
3937 sizeof(*exec_list) * args->buffer_count);
3938 if (ret != 0) {
3939 DRM_ERROR("copy %d exec entries failed %d\n",
3940 args->buffer_count, ret);
3941 drm_free_large(exec_list);
3942 drm_free_large(exec2_list);
3943 return -EFAULT;
3944 }
3945
3946 for (i = 0; i < args->buffer_count; i++) {
3947 exec2_list[i].handle = exec_list[i].handle;
3948 exec2_list[i].relocation_count = exec_list[i].relocation_count;
3949 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
3950 exec2_list[i].alignment = exec_list[i].alignment;
3951 exec2_list[i].offset = exec_list[i].offset;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01003952 if (INTEL_INFO(dev)->gen < 4)
Jesse Barnes76446ca2009-12-17 22:05:42 -05003953 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
3954 else
3955 exec2_list[i].flags = 0;
3956 }
3957
3958 exec2.buffers_ptr = args->buffers_ptr;
3959 exec2.buffer_count = args->buffer_count;
3960 exec2.batch_start_offset = args->batch_start_offset;
3961 exec2.batch_len = args->batch_len;
3962 exec2.DR1 = args->DR1;
3963 exec2.DR4 = args->DR4;
3964 exec2.num_cliprects = args->num_cliprects;
3965 exec2.cliprects_ptr = args->cliprects_ptr;
Zou Nan hai852835f2010-05-21 09:08:56 +08003966 exec2.flags = I915_EXEC_RENDER;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003967
3968 ret = i915_gem_do_execbuffer(dev, data, file_priv, &exec2, exec2_list);
3969 if (!ret) {
3970 /* Copy the new buffer offsets back to the user's exec list. */
3971 for (i = 0; i < args->buffer_count; i++)
3972 exec_list[i].offset = exec2_list[i].offset;
3973 /* ... and back out to userspace */
3974 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
3975 (uintptr_t) args->buffers_ptr,
3976 exec_list,
3977 sizeof(*exec_list) * args->buffer_count);
3978 if (ret) {
3979 ret = -EFAULT;
3980 DRM_ERROR("failed to copy %d exec entries "
3981 "back to user (%d)\n",
3982 args->buffer_count, ret);
3983 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05003984 }
3985
3986 drm_free_large(exec_list);
3987 drm_free_large(exec2_list);
3988 return ret;
3989}
3990
3991int
3992i915_gem_execbuffer2(struct drm_device *dev, void *data,
3993 struct drm_file *file_priv)
3994{
3995 struct drm_i915_gem_execbuffer2 *args = data;
3996 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
3997 int ret;
3998
3999#if WATCH_EXEC
4000 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
4001 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
4002#endif
4003
4004 if (args->buffer_count < 1) {
4005 DRM_ERROR("execbuf2 with %d buffers\n", args->buffer_count);
4006 return -EINVAL;
4007 }
4008
4009 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
4010 if (exec2_list == NULL) {
4011 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
4012 args->buffer_count);
4013 return -ENOMEM;
4014 }
4015 ret = copy_from_user(exec2_list,
4016 (struct drm_i915_relocation_entry __user *)
4017 (uintptr_t) args->buffers_ptr,
4018 sizeof(*exec2_list) * args->buffer_count);
4019 if (ret != 0) {
4020 DRM_ERROR("copy %d exec entries failed %d\n",
4021 args->buffer_count, ret);
4022 drm_free_large(exec2_list);
4023 return -EFAULT;
4024 }
4025
4026 ret = i915_gem_do_execbuffer(dev, data, file_priv, args, exec2_list);
4027 if (!ret) {
4028 /* Copy the new buffer offsets back to the user's exec list. */
4029 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
4030 (uintptr_t) args->buffers_ptr,
4031 exec2_list,
4032 sizeof(*exec2_list) * args->buffer_count);
4033 if (ret) {
4034 ret = -EFAULT;
4035 DRM_ERROR("failed to copy %d exec entries "
4036 "back to user (%d)\n",
4037 args->buffer_count, ret);
4038 }
4039 }
4040
4041 drm_free_large(exec2_list);
4042 return ret;
4043}
4044
Eric Anholt673a3942008-07-30 12:06:12 -07004045int
4046i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment)
4047{
4048 struct drm_device *dev = obj->dev;
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004049 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01004050 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004051 int ret;
4052
Daniel Vetter778c3542010-05-13 11:49:44 +02004053 BUG_ON(obj_priv->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT);
4054
Eric Anholt673a3942008-07-30 12:06:12 -07004055 i915_verify_inactive(dev, __FILE__, __LINE__);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01004056
4057 if (obj_priv->gtt_space != NULL) {
4058 if (alignment == 0)
4059 alignment = i915_gem_get_gtt_alignment(obj);
4060 if (obj_priv->gtt_offset & (alignment - 1)) {
Chris Wilsonae7d49d2010-08-04 12:37:41 +01004061 WARN(obj_priv->pin_count,
4062 "bo is already pinned with incorrect alignment:"
4063 " offset=%x, req.alignment=%x\n",
4064 obj_priv->gtt_offset, alignment);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01004065 ret = i915_gem_object_unbind(obj);
4066 if (ret)
4067 return ret;
4068 }
4069 }
4070
Eric Anholt673a3942008-07-30 12:06:12 -07004071 if (obj_priv->gtt_space == NULL) {
4072 ret = i915_gem_object_bind_to_gtt(obj, alignment);
Chris Wilson97311292009-09-21 00:22:34 +01004073 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07004074 return ret;
Chris Wilson22c344e2009-02-11 14:26:45 +00004075 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05004076
Eric Anholt673a3942008-07-30 12:06:12 -07004077 obj_priv->pin_count++;
4078
4079 /* If the object is not active and not pending a flush,
4080 * remove it from the inactive list
4081 */
4082 if (obj_priv->pin_count == 1) {
4083 atomic_inc(&dev->pin_count);
4084 atomic_add(obj->size, &dev->pin_memory);
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004085 if (!obj_priv->active)
4086 list_move_tail(&obj_priv->list,
4087 &dev_priv->mm.pinned_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004088 }
4089 i915_verify_inactive(dev, __FILE__, __LINE__);
4090
4091 return 0;
4092}
4093
4094void
4095i915_gem_object_unpin(struct drm_gem_object *obj)
4096{
4097 struct drm_device *dev = obj->dev;
4098 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01004099 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004100
4101 i915_verify_inactive(dev, __FILE__, __LINE__);
4102 obj_priv->pin_count--;
4103 BUG_ON(obj_priv->pin_count < 0);
4104 BUG_ON(obj_priv->gtt_space == NULL);
4105
4106 /* If the object is no longer pinned, and is
4107 * neither active nor being flushed, then stick it on
4108 * the inactive list
4109 */
4110 if (obj_priv->pin_count == 0) {
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004111 if (!obj_priv->active)
Eric Anholt673a3942008-07-30 12:06:12 -07004112 list_move_tail(&obj_priv->list,
4113 &dev_priv->mm.inactive_list);
4114 atomic_dec(&dev->pin_count);
4115 atomic_sub(obj->size, &dev->pin_memory);
4116 }
4117 i915_verify_inactive(dev, __FILE__, __LINE__);
4118}
4119
4120int
4121i915_gem_pin_ioctl(struct drm_device *dev, void *data,
4122 struct drm_file *file_priv)
4123{
4124 struct drm_i915_gem_pin *args = data;
4125 struct drm_gem_object *obj;
4126 struct drm_i915_gem_object *obj_priv;
4127 int ret;
4128
4129 mutex_lock(&dev->struct_mutex);
4130
4131 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4132 if (obj == NULL) {
4133 DRM_ERROR("Bad handle in i915_gem_pin_ioctl(): %d\n",
4134 args->handle);
4135 mutex_unlock(&dev->struct_mutex);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01004136 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07004137 }
Daniel Vetter23010e42010-03-08 13:35:02 +01004138 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004139
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004140 if (obj_priv->madv != I915_MADV_WILLNEED) {
4141 DRM_ERROR("Attempting to pin a purgeable buffer\n");
Chris Wilson3ef94da2009-09-14 16:50:29 +01004142 drm_gem_object_unreference(obj);
4143 mutex_unlock(&dev->struct_mutex);
4144 return -EINVAL;
4145 }
4146
Jesse Barnes79e53942008-11-07 14:24:08 -08004147 if (obj_priv->pin_filp != NULL && obj_priv->pin_filp != file_priv) {
4148 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
4149 args->handle);
Chris Wilson96dec612009-02-08 19:08:04 +00004150 drm_gem_object_unreference(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004151 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08004152 return -EINVAL;
4153 }
4154
4155 obj_priv->user_pin_count++;
4156 obj_priv->pin_filp = file_priv;
4157 if (obj_priv->user_pin_count == 1) {
4158 ret = i915_gem_object_pin(obj, args->alignment);
4159 if (ret != 0) {
4160 drm_gem_object_unreference(obj);
4161 mutex_unlock(&dev->struct_mutex);
4162 return ret;
4163 }
Eric Anholt673a3942008-07-30 12:06:12 -07004164 }
4165
4166 /* XXX - flush the CPU caches for pinned objects
4167 * as the X server doesn't manage domains yet
4168 */
Eric Anholte47c68e2008-11-14 13:35:19 -08004169 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004170 args->offset = obj_priv->gtt_offset;
4171 drm_gem_object_unreference(obj);
4172 mutex_unlock(&dev->struct_mutex);
4173
4174 return 0;
4175}
4176
4177int
4178i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
4179 struct drm_file *file_priv)
4180{
4181 struct drm_i915_gem_pin *args = data;
4182 struct drm_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08004183 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07004184
4185 mutex_lock(&dev->struct_mutex);
4186
4187 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4188 if (obj == NULL) {
4189 DRM_ERROR("Bad handle in i915_gem_unpin_ioctl(): %d\n",
4190 args->handle);
4191 mutex_unlock(&dev->struct_mutex);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01004192 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07004193 }
4194
Daniel Vetter23010e42010-03-08 13:35:02 +01004195 obj_priv = to_intel_bo(obj);
Jesse Barnes79e53942008-11-07 14:24:08 -08004196 if (obj_priv->pin_filp != file_priv) {
4197 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
4198 args->handle);
4199 drm_gem_object_unreference(obj);
4200 mutex_unlock(&dev->struct_mutex);
4201 return -EINVAL;
4202 }
4203 obj_priv->user_pin_count--;
4204 if (obj_priv->user_pin_count == 0) {
4205 obj_priv->pin_filp = NULL;
4206 i915_gem_object_unpin(obj);
4207 }
Eric Anholt673a3942008-07-30 12:06:12 -07004208
4209 drm_gem_object_unreference(obj);
4210 mutex_unlock(&dev->struct_mutex);
4211 return 0;
4212}
4213
4214int
4215i915_gem_busy_ioctl(struct drm_device *dev, void *data,
4216 struct drm_file *file_priv)
4217{
4218 struct drm_i915_gem_busy *args = data;
4219 struct drm_gem_object *obj;
4220 struct drm_i915_gem_object *obj_priv;
4221
Eric Anholt673a3942008-07-30 12:06:12 -07004222 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4223 if (obj == NULL) {
4224 DRM_ERROR("Bad handle in i915_gem_busy_ioctl(): %d\n",
4225 args->handle);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01004226 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07004227 }
4228
Chris Wilsonb1ce7862009-06-06 09:46:00 +01004229 mutex_lock(&dev->struct_mutex);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004230
Chris Wilson0be555b2010-08-04 15:36:30 +01004231 /* Count all active objects as busy, even if they are currently not used
4232 * by the gpu. Users of this interface expect objects to eventually
4233 * become non-busy without any further actions, therefore emit any
4234 * necessary flushes here.
Eric Anholtc4de0a52008-12-14 19:05:04 -08004235 */
Chris Wilson0be555b2010-08-04 15:36:30 +01004236 obj_priv = to_intel_bo(obj);
4237 args->busy = obj_priv->active;
4238 if (args->busy) {
4239 /* Unconditionally flush objects, even when the gpu still uses this
4240 * object. Userspace calling this function indicates that it wants to
4241 * use this buffer rather sooner than later, so issuing the required
4242 * flush earlier is beneficial.
4243 */
Chris Wilsonc78ec302010-09-20 12:50:23 +01004244 if (obj->write_domain & I915_GEM_GPU_DOMAINS)
4245 i915_gem_flush_ring(dev, file_priv,
Chris Wilson92204342010-09-18 11:02:01 +01004246 obj_priv->ring,
4247 0, obj->write_domain);
Chris Wilson0be555b2010-08-04 15:36:30 +01004248
4249 /* Update the active list for the hardware's current position.
4250 * Otherwise this only updates on a delayed timer or when irqs
4251 * are actually unmasked, and our working set ends up being
4252 * larger than required.
4253 */
4254 i915_gem_retire_requests_ring(dev, obj_priv->ring);
4255
4256 args->busy = obj_priv->active;
4257 }
Eric Anholt673a3942008-07-30 12:06:12 -07004258
4259 drm_gem_object_unreference(obj);
4260 mutex_unlock(&dev->struct_mutex);
4261 return 0;
4262}
4263
4264int
4265i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4266 struct drm_file *file_priv)
4267{
4268 return i915_gem_ring_throttle(dev, file_priv);
4269}
4270
Chris Wilson3ef94da2009-09-14 16:50:29 +01004271int
4272i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4273 struct drm_file *file_priv)
4274{
4275 struct drm_i915_gem_madvise *args = data;
4276 struct drm_gem_object *obj;
4277 struct drm_i915_gem_object *obj_priv;
4278
4279 switch (args->madv) {
4280 case I915_MADV_DONTNEED:
4281 case I915_MADV_WILLNEED:
4282 break;
4283 default:
4284 return -EINVAL;
4285 }
4286
4287 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4288 if (obj == NULL) {
4289 DRM_ERROR("Bad handle in i915_gem_madvise_ioctl(): %d\n",
4290 args->handle);
Chris Wilsonbf79cb92010-08-04 14:19:46 +01004291 return -ENOENT;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004292 }
4293
4294 mutex_lock(&dev->struct_mutex);
Daniel Vetter23010e42010-03-08 13:35:02 +01004295 obj_priv = to_intel_bo(obj);
Chris Wilson3ef94da2009-09-14 16:50:29 +01004296
4297 if (obj_priv->pin_count) {
4298 drm_gem_object_unreference(obj);
4299 mutex_unlock(&dev->struct_mutex);
4300
4301 DRM_ERROR("Attempted i915_gem_madvise_ioctl() on a pinned object\n");
4302 return -EINVAL;
4303 }
4304
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004305 if (obj_priv->madv != __I915_MADV_PURGED)
4306 obj_priv->madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004307
Chris Wilson2d7ef392009-09-20 23:13:10 +01004308 /* if the object is no longer bound, discard its backing storage */
4309 if (i915_gem_object_is_purgeable(obj_priv) &&
4310 obj_priv->gtt_space == NULL)
4311 i915_gem_object_truncate(obj);
4312
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004313 args->retained = obj_priv->madv != __I915_MADV_PURGED;
4314
Chris Wilson3ef94da2009-09-14 16:50:29 +01004315 drm_gem_object_unreference(obj);
4316 mutex_unlock(&dev->struct_mutex);
4317
4318 return 0;
4319}
4320
Daniel Vetterac52bc52010-04-09 19:05:06 +00004321struct drm_gem_object * i915_gem_alloc_object(struct drm_device *dev,
4322 size_t size)
4323{
Daniel Vetterc397b902010-04-09 19:05:07 +00004324 struct drm_i915_gem_object *obj;
4325
4326 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
4327 if (obj == NULL)
4328 return NULL;
4329
4330 if (drm_gem_object_init(dev, &obj->base, size) != 0) {
4331 kfree(obj);
4332 return NULL;
4333 }
4334
4335 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4336 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4337
4338 obj->agp_type = AGP_USER_MEMORY;
Daniel Vetter62b8b212010-04-09 19:05:08 +00004339 obj->base.driver_private = NULL;
Daniel Vetterc397b902010-04-09 19:05:07 +00004340 obj->fence_reg = I915_FENCE_REG_NONE;
4341 INIT_LIST_HEAD(&obj->list);
4342 INIT_LIST_HEAD(&obj->gpu_write_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00004343 obj->madv = I915_MADV_WILLNEED;
4344
4345 trace_i915_gem_object_create(&obj->base);
4346
4347 return &obj->base;
Daniel Vetterac52bc52010-04-09 19:05:06 +00004348}
4349
Eric Anholt673a3942008-07-30 12:06:12 -07004350int i915_gem_init_object(struct drm_gem_object *obj)
4351{
Daniel Vetterc397b902010-04-09 19:05:07 +00004352 BUG();
Jesse Barnesde151cf2008-11-12 10:03:55 -08004353
Eric Anholt673a3942008-07-30 12:06:12 -07004354 return 0;
4355}
4356
Chris Wilsonbe726152010-07-23 23:18:50 +01004357static void i915_gem_free_object_tail(struct drm_gem_object *obj)
4358{
4359 struct drm_device *dev = obj->dev;
4360 drm_i915_private_t *dev_priv = dev->dev_private;
4361 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
4362 int ret;
4363
4364 ret = i915_gem_object_unbind(obj);
4365 if (ret == -ERESTARTSYS) {
4366 list_move(&obj_priv->list,
4367 &dev_priv->mm.deferred_free_list);
4368 return;
4369 }
4370
4371 if (obj_priv->mmap_offset)
4372 i915_gem_free_mmap_offset(obj);
4373
4374 drm_gem_object_release(obj);
4375
4376 kfree(obj_priv->page_cpu_valid);
4377 kfree(obj_priv->bit_17);
4378 kfree(obj_priv);
4379}
4380
Eric Anholt673a3942008-07-30 12:06:12 -07004381void i915_gem_free_object(struct drm_gem_object *obj)
4382{
Jesse Barnesde151cf2008-11-12 10:03:55 -08004383 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01004384 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004385
Chris Wilson1c5d22f2009-08-25 11:15:50 +01004386 trace_i915_gem_object_destroy(obj);
4387
Eric Anholt673a3942008-07-30 12:06:12 -07004388 while (obj_priv->pin_count > 0)
4389 i915_gem_object_unpin(obj);
4390
Dave Airlie71acb5e2008-12-30 20:31:46 +10004391 if (obj_priv->phys_obj)
4392 i915_gem_detach_phys_object(dev, obj);
4393
Chris Wilsonbe726152010-07-23 23:18:50 +01004394 i915_gem_free_object_tail(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004395}
4396
Jesse Barnes5669fca2009-02-17 15:13:31 -08004397int
Eric Anholt673a3942008-07-30 12:06:12 -07004398i915_gem_idle(struct drm_device *dev)
4399{
4400 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson29105cc2010-01-07 10:39:13 +00004401 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004402
Keith Packard6dbe2772008-10-14 21:41:13 -07004403 mutex_lock(&dev->struct_mutex);
4404
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004405 if (dev_priv->mm.suspended ||
Zou Nan haid1b851f2010-05-21 09:08:57 +08004406 (dev_priv->render_ring.gem_object == NULL) ||
4407 (HAS_BSD(dev) &&
4408 dev_priv->bsd_ring.gem_object == NULL)) {
Keith Packard6dbe2772008-10-14 21:41:13 -07004409 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004410 return 0;
Keith Packard6dbe2772008-10-14 21:41:13 -07004411 }
Eric Anholt673a3942008-07-30 12:06:12 -07004412
Chris Wilson29105cc2010-01-07 10:39:13 +00004413 ret = i915_gpu_idle(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004414 if (ret) {
4415 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004416 return ret;
Keith Packard6dbe2772008-10-14 21:41:13 -07004417 }
Eric Anholt673a3942008-07-30 12:06:12 -07004418
Chris Wilson29105cc2010-01-07 10:39:13 +00004419 /* Under UMS, be paranoid and evict. */
4420 if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
Chris Wilsonb47eb4a2010-08-07 11:01:23 +01004421 ret = i915_gem_evict_inactive(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00004422 if (ret) {
4423 mutex_unlock(&dev->struct_mutex);
4424 return ret;
4425 }
4426 }
4427
4428 /* Hack! Don't let anybody do execbuf while we don't control the chip.
4429 * We need to replace this with a semaphore, or something.
4430 * And not confound mm.suspended!
4431 */
4432 dev_priv->mm.suspended = 1;
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02004433 del_timer_sync(&dev_priv->hangcheck_timer);
Chris Wilson29105cc2010-01-07 10:39:13 +00004434
4435 i915_kernel_lost_context(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004436 i915_gem_cleanup_ringbuffer(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00004437
Keith Packard6dbe2772008-10-14 21:41:13 -07004438 mutex_unlock(&dev->struct_mutex);
4439
Chris Wilson29105cc2010-01-07 10:39:13 +00004440 /* Cancel the retire work handler, which should be idle now. */
4441 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
4442
Eric Anholt673a3942008-07-30 12:06:12 -07004443 return 0;
4444}
4445
Jesse Barnese552eb72010-04-21 11:39:23 -07004446/*
4447 * 965+ support PIPE_CONTROL commands, which provide finer grained control
4448 * over cache flushing.
4449 */
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004450static int
Jesse Barnese552eb72010-04-21 11:39:23 -07004451i915_gem_init_pipe_control(struct drm_device *dev)
4452{
4453 drm_i915_private_t *dev_priv = dev->dev_private;
4454 struct drm_gem_object *obj;
4455 struct drm_i915_gem_object *obj_priv;
4456 int ret;
4457
Eric Anholt34dc4d42010-05-07 14:30:03 -07004458 obj = i915_gem_alloc_object(dev, 4096);
Jesse Barnese552eb72010-04-21 11:39:23 -07004459 if (obj == NULL) {
4460 DRM_ERROR("Failed to allocate seqno page\n");
4461 ret = -ENOMEM;
4462 goto err;
4463 }
4464 obj_priv = to_intel_bo(obj);
4465 obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
4466
4467 ret = i915_gem_object_pin(obj, 4096);
4468 if (ret)
4469 goto err_unref;
4470
4471 dev_priv->seqno_gfx_addr = obj_priv->gtt_offset;
4472 dev_priv->seqno_page = kmap(obj_priv->pages[0]);
4473 if (dev_priv->seqno_page == NULL)
4474 goto err_unpin;
4475
4476 dev_priv->seqno_obj = obj;
4477 memset(dev_priv->seqno_page, 0, PAGE_SIZE);
4478
4479 return 0;
4480
4481err_unpin:
4482 i915_gem_object_unpin(obj);
4483err_unref:
4484 drm_gem_object_unreference(obj);
4485err:
4486 return ret;
4487}
4488
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004489
4490static void
Jesse Barnese552eb72010-04-21 11:39:23 -07004491i915_gem_cleanup_pipe_control(struct drm_device *dev)
4492{
4493 drm_i915_private_t *dev_priv = dev->dev_private;
4494 struct drm_gem_object *obj;
4495 struct drm_i915_gem_object *obj_priv;
4496
4497 obj = dev_priv->seqno_obj;
4498 obj_priv = to_intel_bo(obj);
4499 kunmap(obj_priv->pages[0]);
4500 i915_gem_object_unpin(obj);
4501 drm_gem_object_unreference(obj);
4502 dev_priv->seqno_obj = NULL;
4503
4504 dev_priv->seqno_page = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07004505}
4506
Eric Anholt673a3942008-07-30 12:06:12 -07004507int
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004508i915_gem_init_ringbuffer(struct drm_device *dev)
4509{
4510 drm_i915_private_t *dev_priv = dev->dev_private;
4511 int ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01004512
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004513 if (HAS_PIPE_CONTROL(dev)) {
4514 ret = i915_gem_init_pipe_control(dev);
4515 if (ret)
4516 return ret;
4517 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004518
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08004519 ret = intel_init_render_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004520 if (ret)
4521 goto cleanup_pipe_control;
4522
4523 if (HAS_BSD(dev)) {
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08004524 ret = intel_init_bsd_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004525 if (ret)
4526 goto cleanup_render_ring;
Zou Nan haid1b851f2010-05-21 09:08:57 +08004527 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004528
Chris Wilson6f392d5482010-08-07 11:01:22 +01004529 dev_priv->next_seqno = 1;
4530
Chris Wilson68f95ba2010-05-27 13:18:22 +01004531 return 0;
4532
4533cleanup_render_ring:
4534 intel_cleanup_ring_buffer(dev, &dev_priv->render_ring);
4535cleanup_pipe_control:
4536 if (HAS_PIPE_CONTROL(dev))
4537 i915_gem_cleanup_pipe_control(dev);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004538 return ret;
4539}
4540
4541void
4542i915_gem_cleanup_ringbuffer(struct drm_device *dev)
4543{
4544 drm_i915_private_t *dev_priv = dev->dev_private;
4545
4546 intel_cleanup_ring_buffer(dev, &dev_priv->render_ring);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004547 if (HAS_BSD(dev))
4548 intel_cleanup_ring_buffer(dev, &dev_priv->bsd_ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004549 if (HAS_PIPE_CONTROL(dev))
4550 i915_gem_cleanup_pipe_control(dev);
4551}
4552
4553int
Eric Anholt673a3942008-07-30 12:06:12 -07004554i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
4555 struct drm_file *file_priv)
4556{
4557 drm_i915_private_t *dev_priv = dev->dev_private;
4558 int ret;
4559
Jesse Barnes79e53942008-11-07 14:24:08 -08004560 if (drm_core_check_feature(dev, DRIVER_MODESET))
4561 return 0;
4562
Ben Gamariba1234d2009-09-14 17:48:47 -04004563 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07004564 DRM_ERROR("Reenabling wedged hardware, good luck\n");
Ben Gamariba1234d2009-09-14 17:48:47 -04004565 atomic_set(&dev_priv->mm.wedged, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004566 }
4567
Eric Anholt673a3942008-07-30 12:06:12 -07004568 mutex_lock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004569 dev_priv->mm.suspended = 0;
4570
4571 ret = i915_gem_init_ringbuffer(dev);
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004572 if (ret != 0) {
4573 mutex_unlock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004574 return ret;
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004575 }
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004576
Zou Nan hai852835f2010-05-21 09:08:56 +08004577 BUG_ON(!list_empty(&dev_priv->render_ring.active_list));
Zou Nan haid1b851f2010-05-21 09:08:57 +08004578 BUG_ON(HAS_BSD(dev) && !list_empty(&dev_priv->bsd_ring.active_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004579 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
4580 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
Zou Nan hai852835f2010-05-21 09:08:56 +08004581 BUG_ON(!list_empty(&dev_priv->render_ring.request_list));
Zou Nan haid1b851f2010-05-21 09:08:57 +08004582 BUG_ON(HAS_BSD(dev) && !list_empty(&dev_priv->bsd_ring.request_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004583 mutex_unlock(&dev->struct_mutex);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004584
Chris Wilson5f353082010-06-07 14:03:03 +01004585 ret = drm_irq_install(dev);
4586 if (ret)
4587 goto cleanup_ringbuffer;
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004588
Eric Anholt673a3942008-07-30 12:06:12 -07004589 return 0;
Chris Wilson5f353082010-06-07 14:03:03 +01004590
4591cleanup_ringbuffer:
4592 mutex_lock(&dev->struct_mutex);
4593 i915_gem_cleanup_ringbuffer(dev);
4594 dev_priv->mm.suspended = 1;
4595 mutex_unlock(&dev->struct_mutex);
4596
4597 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004598}
4599
4600int
4601i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
4602 struct drm_file *file_priv)
4603{
Jesse Barnes79e53942008-11-07 14:24:08 -08004604 if (drm_core_check_feature(dev, DRIVER_MODESET))
4605 return 0;
4606
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004607 drm_irq_uninstall(dev);
Linus Torvaldse6890f62009-09-08 17:09:24 -07004608 return i915_gem_idle(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004609}
4610
4611void
4612i915_gem_lastclose(struct drm_device *dev)
4613{
4614 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004615
Eric Anholte806b492009-01-22 09:56:58 -08004616 if (drm_core_check_feature(dev, DRIVER_MODESET))
4617 return;
4618
Keith Packard6dbe2772008-10-14 21:41:13 -07004619 ret = i915_gem_idle(dev);
4620 if (ret)
4621 DRM_ERROR("failed to idle hardware: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07004622}
4623
4624void
4625i915_gem_load(struct drm_device *dev)
4626{
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004627 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07004628 drm_i915_private_t *dev_priv = dev->dev_private;
4629
Eric Anholt673a3942008-07-30 12:06:12 -07004630 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
Daniel Vetter99fcb762010-02-07 16:20:18 +01004631 INIT_LIST_HEAD(&dev_priv->mm.gpu_write_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004632 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
Chris Wilsonf13d3f72010-09-20 17:36:15 +01004633 INIT_LIST_HEAD(&dev_priv->mm.pinned_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07004634 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Chris Wilsonbe726152010-07-23 23:18:50 +01004635 INIT_LIST_HEAD(&dev_priv->mm.deferred_free_list);
Zou Nan hai852835f2010-05-21 09:08:56 +08004636 INIT_LIST_HEAD(&dev_priv->render_ring.active_list);
4637 INIT_LIST_HEAD(&dev_priv->render_ring.request_list);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004638 if (HAS_BSD(dev)) {
4639 INIT_LIST_HEAD(&dev_priv->bsd_ring.active_list);
4640 INIT_LIST_HEAD(&dev_priv->bsd_ring.request_list);
4641 }
Daniel Vetter007cc8a2010-04-28 11:02:31 +02004642 for (i = 0; i < 16; i++)
4643 INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004644 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
4645 i915_gem_retire_work_handler);
Chris Wilson31169712009-09-14 16:50:28 +01004646 spin_lock(&shrink_list_lock);
4647 list_add(&dev_priv->mm.shrink_list, &shrink_list);
4648 spin_unlock(&shrink_list_lock);
4649
Dave Airlie94400122010-07-20 13:15:31 +10004650 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
4651 if (IS_GEN3(dev)) {
4652 u32 tmp = I915_READ(MI_ARB_STATE);
4653 if (!(tmp & MI_ARB_C3_LP_WRITE_ENABLE)) {
4654 /* arb state is a masked write, so set bit + bit in mask */
4655 tmp = MI_ARB_C3_LP_WRITE_ENABLE | (MI_ARB_C3_LP_WRITE_ENABLE << MI_ARB_MASK_SHIFT);
4656 I915_WRITE(MI_ARB_STATE, tmp);
4657 }
4658 }
4659
Jesse Barnesde151cf2008-11-12 10:03:55 -08004660 /* Old X drivers will take 0-2 for front, back, depth buffers */
Eric Anholtb397c832010-01-26 09:43:10 -08004661 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4662 dev_priv->fence_reg_start = 3;
Jesse Barnesde151cf2008-11-12 10:03:55 -08004663
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004664 if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08004665 dev_priv->num_fence_regs = 16;
4666 else
4667 dev_priv->num_fence_regs = 8;
4668
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004669 /* Initialize fence registers to zero */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004670 switch (INTEL_INFO(dev)->gen) {
4671 case 6:
4672 for (i = 0; i < 16; i++)
4673 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (i * 8), 0);
4674 break;
4675 case 5:
4676 case 4:
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004677 for (i = 0; i < 16; i++)
4678 I915_WRITE64(FENCE_REG_965_0 + (i * 8), 0);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004679 break;
4680 case 3:
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004681 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4682 for (i = 0; i < 8; i++)
4683 I915_WRITE(FENCE_REG_945_8 + (i * 4), 0);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004684 case 2:
4685 for (i = 0; i < 8; i++)
4686 I915_WRITE(FENCE_REG_830_0 + (i * 4), 0);
4687 break;
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004688 }
Eric Anholt673a3942008-07-30 12:06:12 -07004689 i915_gem_detect_bit_6_swizzle(dev);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05004690 init_waitqueue_head(&dev_priv->pending_flip_queue);
Eric Anholt673a3942008-07-30 12:06:12 -07004691}
Dave Airlie71acb5e2008-12-30 20:31:46 +10004692
4693/*
4694 * Create a physically contiguous memory object for this object
4695 * e.g. for cursor + overlay regs
4696 */
Chris Wilson995b6762010-08-20 13:23:26 +01004697static int i915_gem_init_phys_object(struct drm_device *dev,
4698 int id, int size, int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004699{
4700 drm_i915_private_t *dev_priv = dev->dev_private;
4701 struct drm_i915_gem_phys_object *phys_obj;
4702 int ret;
4703
4704 if (dev_priv->mm.phys_objs[id - 1] || !size)
4705 return 0;
4706
Eric Anholt9a298b22009-03-24 12:23:04 -07004707 phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004708 if (!phys_obj)
4709 return -ENOMEM;
4710
4711 phys_obj->id = id;
4712
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004713 phys_obj->handle = drm_pci_alloc(dev, size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004714 if (!phys_obj->handle) {
4715 ret = -ENOMEM;
4716 goto kfree_obj;
4717 }
4718#ifdef CONFIG_X86
4719 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4720#endif
4721
4722 dev_priv->mm.phys_objs[id - 1] = phys_obj;
4723
4724 return 0;
4725kfree_obj:
Eric Anholt9a298b22009-03-24 12:23:04 -07004726 kfree(phys_obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004727 return ret;
4728}
4729
Chris Wilson995b6762010-08-20 13:23:26 +01004730static void i915_gem_free_phys_object(struct drm_device *dev, int id)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004731{
4732 drm_i915_private_t *dev_priv = dev->dev_private;
4733 struct drm_i915_gem_phys_object *phys_obj;
4734
4735 if (!dev_priv->mm.phys_objs[id - 1])
4736 return;
4737
4738 phys_obj = dev_priv->mm.phys_objs[id - 1];
4739 if (phys_obj->cur_obj) {
4740 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
4741 }
4742
4743#ifdef CONFIG_X86
4744 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4745#endif
4746 drm_pci_free(dev, phys_obj->handle);
4747 kfree(phys_obj);
4748 dev_priv->mm.phys_objs[id - 1] = NULL;
4749}
4750
4751void i915_gem_free_all_phys_object(struct drm_device *dev)
4752{
4753 int i;
4754
Dave Airlie260883c2009-01-22 17:58:49 +10004755 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004756 i915_gem_free_phys_object(dev, i);
4757}
4758
4759void i915_gem_detach_phys_object(struct drm_device *dev,
4760 struct drm_gem_object *obj)
4761{
4762 struct drm_i915_gem_object *obj_priv;
4763 int i;
4764 int ret;
4765 int page_count;
4766
Daniel Vetter23010e42010-03-08 13:35:02 +01004767 obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004768 if (!obj_priv->phys_obj)
4769 return;
4770
Chris Wilson4bdadb92010-01-27 13:36:32 +00004771 ret = i915_gem_object_get_pages(obj, 0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004772 if (ret)
4773 goto out;
4774
4775 page_count = obj->size / PAGE_SIZE;
4776
4777 for (i = 0; i < page_count; i++) {
Eric Anholt856fa192009-03-19 14:10:50 -07004778 char *dst = kmap_atomic(obj_priv->pages[i], KM_USER0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004779 char *src = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4780
4781 memcpy(dst, src, PAGE_SIZE);
4782 kunmap_atomic(dst, KM_USER0);
4783 }
Eric Anholt856fa192009-03-19 14:10:50 -07004784 drm_clflush_pages(obj_priv->pages, page_count);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004785 drm_agp_chipset_flush(dev);
Chris Wilsond78b47b2009-06-17 21:52:49 +01004786
4787 i915_gem_object_put_pages(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004788out:
4789 obj_priv->phys_obj->cur_obj = NULL;
4790 obj_priv->phys_obj = NULL;
4791}
4792
4793int
4794i915_gem_attach_phys_object(struct drm_device *dev,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004795 struct drm_gem_object *obj,
4796 int id,
4797 int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004798{
4799 drm_i915_private_t *dev_priv = dev->dev_private;
4800 struct drm_i915_gem_object *obj_priv;
4801 int ret = 0;
4802 int page_count;
4803 int i;
4804
4805 if (id > I915_MAX_PHYS_OBJECT)
4806 return -EINVAL;
4807
Daniel Vetter23010e42010-03-08 13:35:02 +01004808 obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004809
4810 if (obj_priv->phys_obj) {
4811 if (obj_priv->phys_obj->id == id)
4812 return 0;
4813 i915_gem_detach_phys_object(dev, obj);
4814 }
4815
Dave Airlie71acb5e2008-12-30 20:31:46 +10004816 /* create a new object */
4817 if (!dev_priv->mm.phys_objs[id - 1]) {
4818 ret = i915_gem_init_phys_object(dev, id,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004819 obj->size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004820 if (ret) {
Linus Torvaldsaeb565d2009-01-26 10:01:53 -08004821 DRM_ERROR("failed to init phys object %d size: %zu\n", id, obj->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004822 goto out;
4823 }
4824 }
4825
4826 /* bind to the object */
4827 obj_priv->phys_obj = dev_priv->mm.phys_objs[id - 1];
4828 obj_priv->phys_obj->cur_obj = obj;
4829
Chris Wilson4bdadb92010-01-27 13:36:32 +00004830 ret = i915_gem_object_get_pages(obj, 0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004831 if (ret) {
4832 DRM_ERROR("failed to get page list\n");
4833 goto out;
4834 }
4835
4836 page_count = obj->size / PAGE_SIZE;
4837
4838 for (i = 0; i < page_count; i++) {
Eric Anholt856fa192009-03-19 14:10:50 -07004839 char *src = kmap_atomic(obj_priv->pages[i], KM_USER0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004840 char *dst = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4841
4842 memcpy(dst, src, PAGE_SIZE);
4843 kunmap_atomic(src, KM_USER0);
4844 }
4845
Chris Wilsond78b47b2009-06-17 21:52:49 +01004846 i915_gem_object_put_pages(obj);
4847
Dave Airlie71acb5e2008-12-30 20:31:46 +10004848 return 0;
4849out:
4850 return ret;
4851}
4852
4853static int
4854i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
4855 struct drm_i915_gem_pwrite *args,
4856 struct drm_file *file_priv)
4857{
Daniel Vetter23010e42010-03-08 13:35:02 +01004858 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004859 void *obj_addr;
4860 int ret;
4861 char __user *user_data;
4862
4863 user_data = (char __user *) (uintptr_t) args->data_ptr;
4864 obj_addr = obj_priv->phys_obj->handle->vaddr + args->offset;
4865
Zhao Yakui44d98a62009-10-09 11:39:40 +08004866 DRM_DEBUG_DRIVER("obj_addr %p, %lld\n", obj_addr, args->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004867 ret = copy_from_user(obj_addr, user_data, args->size);
4868 if (ret)
4869 return -EFAULT;
4870
4871 drm_agp_chipset_flush(dev);
4872 return 0;
4873}
Eric Anholtb9624422009-06-03 07:27:35 +00004874
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004875void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00004876{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004877 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00004878
4879 /* Clean up our request list when the client is going away, so that
4880 * later retire_requests won't dereference our soon-to-be-gone
4881 * file_priv.
4882 */
4883 mutex_lock(&dev->struct_mutex);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004884 mutex_lock(&file_priv->mutex);
4885 while (!list_empty(&file_priv->mm.request_list)) {
4886 struct drm_i915_gem_request *request;
4887
4888 request = list_first_entry(&file_priv->mm.request_list,
4889 struct drm_i915_gem_request,
4890 client_list);
4891 list_del(&request->client_list);
4892 request->file_priv = NULL;
4893 }
4894 mutex_unlock(&file_priv->mutex);
Eric Anholtb9624422009-06-03 07:27:35 +00004895 mutex_unlock(&dev->struct_mutex);
4896}
Chris Wilson31169712009-09-14 16:50:28 +01004897
Chris Wilson31169712009-09-14 16:50:28 +01004898static int
Chris Wilson1637ef42010-04-20 17:10:35 +01004899i915_gpu_is_active(struct drm_device *dev)
4900{
4901 drm_i915_private_t *dev_priv = dev->dev_private;
4902 int lists_empty;
4903
Chris Wilson1637ef42010-04-20 17:10:35 +01004904 lists_empty = list_empty(&dev_priv->mm.flushing_list) &&
Zou Nan hai852835f2010-05-21 09:08:56 +08004905 list_empty(&dev_priv->render_ring.active_list);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004906 if (HAS_BSD(dev))
4907 lists_empty &= list_empty(&dev_priv->bsd_ring.active_list);
Chris Wilson1637ef42010-04-20 17:10:35 +01004908
4909 return !lists_empty;
4910}
4911
4912static int
Dave Chinner7f8275d2010-07-19 14:56:17 +10004913i915_gem_shrink(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask)
Chris Wilson31169712009-09-14 16:50:28 +01004914{
4915 drm_i915_private_t *dev_priv, *next_dev;
4916 struct drm_i915_gem_object *obj_priv, *next_obj;
4917 int cnt = 0;
4918 int would_deadlock = 1;
4919
4920 /* "fast-path" to count number of available objects */
4921 if (nr_to_scan == 0) {
4922 spin_lock(&shrink_list_lock);
4923 list_for_each_entry(dev_priv, &shrink_list, mm.shrink_list) {
4924 struct drm_device *dev = dev_priv->dev;
4925
4926 if (mutex_trylock(&dev->struct_mutex)) {
4927 list_for_each_entry(obj_priv,
4928 &dev_priv->mm.inactive_list,
4929 list)
4930 cnt++;
4931 mutex_unlock(&dev->struct_mutex);
4932 }
4933 }
4934 spin_unlock(&shrink_list_lock);
4935
4936 return (cnt / 100) * sysctl_vfs_cache_pressure;
4937 }
4938
4939 spin_lock(&shrink_list_lock);
4940
Chris Wilson1637ef42010-04-20 17:10:35 +01004941rescan:
Chris Wilson31169712009-09-14 16:50:28 +01004942 /* first scan for clean buffers */
4943 list_for_each_entry_safe(dev_priv, next_dev,
4944 &shrink_list, mm.shrink_list) {
4945 struct drm_device *dev = dev_priv->dev;
4946
4947 if (! mutex_trylock(&dev->struct_mutex))
4948 continue;
4949
4950 spin_unlock(&shrink_list_lock);
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01004951 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004952
Chris Wilson31169712009-09-14 16:50:28 +01004953 list_for_each_entry_safe(obj_priv, next_obj,
4954 &dev_priv->mm.inactive_list,
4955 list) {
4956 if (i915_gem_object_is_purgeable(obj_priv)) {
Daniel Vettera8089e82010-04-09 19:05:09 +00004957 i915_gem_object_unbind(&obj_priv->base);
Chris Wilson31169712009-09-14 16:50:28 +01004958 if (--nr_to_scan <= 0)
4959 break;
4960 }
4961 }
4962
4963 spin_lock(&shrink_list_lock);
4964 mutex_unlock(&dev->struct_mutex);
4965
Chris Wilson963b4832009-09-20 23:03:54 +01004966 would_deadlock = 0;
4967
Chris Wilson31169712009-09-14 16:50:28 +01004968 if (nr_to_scan <= 0)
4969 break;
4970 }
4971
4972 /* second pass, evict/count anything still on the inactive list */
4973 list_for_each_entry_safe(dev_priv, next_dev,
4974 &shrink_list, mm.shrink_list) {
4975 struct drm_device *dev = dev_priv->dev;
4976
4977 if (! mutex_trylock(&dev->struct_mutex))
4978 continue;
4979
4980 spin_unlock(&shrink_list_lock);
4981
4982 list_for_each_entry_safe(obj_priv, next_obj,
4983 &dev_priv->mm.inactive_list,
4984 list) {
4985 if (nr_to_scan > 0) {
Daniel Vettera8089e82010-04-09 19:05:09 +00004986 i915_gem_object_unbind(&obj_priv->base);
Chris Wilson31169712009-09-14 16:50:28 +01004987 nr_to_scan--;
4988 } else
4989 cnt++;
4990 }
4991
4992 spin_lock(&shrink_list_lock);
4993 mutex_unlock(&dev->struct_mutex);
4994
4995 would_deadlock = 0;
4996 }
4997
Chris Wilson1637ef42010-04-20 17:10:35 +01004998 if (nr_to_scan) {
4999 int active = 0;
5000
5001 /*
5002 * We are desperate for pages, so as a last resort, wait
5003 * for the GPU to finish and discard whatever we can.
5004 * This has a dramatic impact to reduce the number of
5005 * OOM-killer events whilst running the GPU aggressively.
5006 */
5007 list_for_each_entry(dev_priv, &shrink_list, mm.shrink_list) {
5008 struct drm_device *dev = dev_priv->dev;
5009
5010 if (!mutex_trylock(&dev->struct_mutex))
5011 continue;
5012
5013 spin_unlock(&shrink_list_lock);
5014
5015 if (i915_gpu_is_active(dev)) {
5016 i915_gpu_idle(dev);
5017 active++;
5018 }
5019
5020 spin_lock(&shrink_list_lock);
5021 mutex_unlock(&dev->struct_mutex);
5022 }
5023
5024 if (active)
5025 goto rescan;
5026 }
5027
Chris Wilson31169712009-09-14 16:50:28 +01005028 spin_unlock(&shrink_list_lock);
5029
5030 if (would_deadlock)
5031 return -1;
5032 else if (cnt > 0)
5033 return (cnt / 100) * sysctl_vfs_cache_pressure;
5034 else
5035 return 0;
5036}
5037
5038static struct shrinker shrinker = {
5039 .shrink = i915_gem_shrink,
5040 .seeks = DEFAULT_SEEKS,
5041};
5042
5043__init void
5044i915_gem_shrinker_init(void)
5045{
5046 register_shrinker(&shrinker);
5047}
5048
5049__exit void
5050i915_gem_shrinker_exit(void)
5051{
5052 unregister_shrinker(&shrinker);
5053}