blob: 45b998218d0c70a111905f7e5e9cb933d224891f [file] [log] [blame]
Eric Anholt673a3942008-07-30 12:06:12 -07001/*
2 * Copyright © 2008 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 *
26 */
27
28#include "drmP.h"
29#include "drm.h"
30#include "i915_drm.h"
31#include "i915_drv.h"
Chris Wilson1c5d22f2009-08-25 11:15:50 +010032#include "i915_trace.h"
Jesse Barnes652c3932009-08-17 13:31:43 -070033#include "intel_drv.h"
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Eric Anholt673a3942008-07-30 12:06:12 -070035#include <linux/swap.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080036#include <linux/pci.h>
Eric Anholt673a3942008-07-30 12:06:12 -070037
Daniel Vetter0108a3e2010-08-07 11:01:21 +010038static uint32_t i915_gem_get_gtt_alignment(struct drm_gem_object *obj);
Chris Wilson2dafb1e2010-06-07 14:03:05 +010039static int i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj);
Eric Anholte47c68e2008-11-14 13:35:19 -080040static void i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj);
41static void i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj);
Eric Anholte47c68e2008-11-14 13:35:19 -080042static int i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj,
43 int write);
44static int i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
45 uint64_t offset,
46 uint64_t size);
47static void i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj);
Eric Anholt673a3942008-07-30 12:06:12 -070048static int i915_gem_object_wait_rendering(struct drm_gem_object *obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -080049static int i915_gem_object_bind_to_gtt(struct drm_gem_object *obj,
50 unsigned alignment);
Jesse Barnesde151cf2008-11-12 10:03:55 -080051static void i915_gem_clear_fence_reg(struct drm_gem_object *obj);
Daniel Vetter0108a3e2010-08-07 11:01:21 +010052static int i915_gem_evict_something(struct drm_device *dev, int min_size,
53 unsigned alignment);
Chris Wilsonab5ee572009-09-20 19:25:47 +010054static int i915_gem_evict_from_inactive_list(struct drm_device *dev);
Dave Airlie71acb5e2008-12-30 20:31:46 +100055static int i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
56 struct drm_i915_gem_pwrite *args,
57 struct drm_file *file_priv);
Chris Wilsonbe726152010-07-23 23:18:50 +010058static void i915_gem_free_object_tail(struct drm_gem_object *obj);
Eric Anholt673a3942008-07-30 12:06:12 -070059
Chris Wilson31169712009-09-14 16:50:28 +010060static LIST_HEAD(shrink_list);
61static DEFINE_SPINLOCK(shrink_list_lock);
62
Jesse Barnes79e53942008-11-07 14:24:08 -080063int i915_gem_do_init(struct drm_device *dev, unsigned long start,
64 unsigned long end)
65{
66 drm_i915_private_t *dev_priv = dev->dev_private;
67
68 if (start >= end ||
69 (start & (PAGE_SIZE - 1)) != 0 ||
70 (end & (PAGE_SIZE - 1)) != 0) {
71 return -EINVAL;
72 }
73
74 drm_mm_init(&dev_priv->mm.gtt_space, start,
75 end - start);
76
77 dev->gtt_total = (uint32_t) (end - start);
78
79 return 0;
80}
Keith Packard6dbe2772008-10-14 21:41:13 -070081
Eric Anholt673a3942008-07-30 12:06:12 -070082int
83i915_gem_init_ioctl(struct drm_device *dev, void *data,
84 struct drm_file *file_priv)
85{
Eric Anholt673a3942008-07-30 12:06:12 -070086 struct drm_i915_gem_init *args = data;
Jesse Barnes79e53942008-11-07 14:24:08 -080087 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -070088
89 mutex_lock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080090 ret = i915_gem_do_init(dev, args->gtt_start, args->gtt_end);
Eric Anholt673a3942008-07-30 12:06:12 -070091 mutex_unlock(&dev->struct_mutex);
92
Jesse Barnes79e53942008-11-07 14:24:08 -080093 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -070094}
95
Eric Anholt5a125c32008-10-22 21:40:13 -070096int
97i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
98 struct drm_file *file_priv)
99{
Eric Anholt5a125c32008-10-22 21:40:13 -0700100 struct drm_i915_gem_get_aperture *args = data;
Eric Anholt5a125c32008-10-22 21:40:13 -0700101
102 if (!(dev->driver->driver_features & DRIVER_GEM))
103 return -ENODEV;
104
105 args->aper_size = dev->gtt_total;
Keith Packard2678d9d2008-11-20 22:54:54 -0800106 args->aper_available_size = (args->aper_size -
107 atomic_read(&dev->pin_memory));
Eric Anholt5a125c32008-10-22 21:40:13 -0700108
109 return 0;
110}
111
Eric Anholt673a3942008-07-30 12:06:12 -0700112
113/**
114 * Creates a new mm object and returns a handle to it.
115 */
116int
117i915_gem_create_ioctl(struct drm_device *dev, void *data,
118 struct drm_file *file_priv)
119{
120 struct drm_i915_gem_create *args = data;
121 struct drm_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300122 int ret;
123 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700124
125 args->size = roundup(args->size, PAGE_SIZE);
126
127 /* Allocate the new object */
Daniel Vetterac52bc52010-04-09 19:05:06 +0000128 obj = i915_gem_alloc_object(dev, args->size);
Eric Anholt673a3942008-07-30 12:06:12 -0700129 if (obj == NULL)
130 return -ENOMEM;
131
132 ret = drm_gem_handle_create(file_priv, obj, &handle);
Chris Wilson86f100b2010-07-24 21:03:49 +0100133 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700134 if (ret)
135 return ret;
136
137 args->handle = handle;
138
139 return 0;
140}
141
Eric Anholt40123c12009-03-09 13:42:30 -0700142static inline int
Eric Anholteb014592009-03-10 11:44:52 -0700143fast_shmem_read(struct page **pages,
144 loff_t page_base, int page_offset,
145 char __user *data,
146 int length)
147{
148 char __iomem *vaddr;
Florian Mickler2bc43b52009-04-06 22:55:41 +0200149 int unwritten;
Eric Anholteb014592009-03-10 11:44:52 -0700150
151 vaddr = kmap_atomic(pages[page_base >> PAGE_SHIFT], KM_USER0);
152 if (vaddr == NULL)
153 return -ENOMEM;
Florian Mickler2bc43b52009-04-06 22:55:41 +0200154 unwritten = __copy_to_user_inatomic(data, vaddr + page_offset, length);
Eric Anholteb014592009-03-10 11:44:52 -0700155 kunmap_atomic(vaddr, KM_USER0);
156
Florian Mickler2bc43b52009-04-06 22:55:41 +0200157 if (unwritten)
158 return -EFAULT;
159
160 return 0;
Eric Anholteb014592009-03-10 11:44:52 -0700161}
162
Eric Anholt280b7132009-03-12 16:56:27 -0700163static int i915_gem_object_needs_bit17_swizzle(struct drm_gem_object *obj)
164{
165 drm_i915_private_t *dev_priv = obj->dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +0100166 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt280b7132009-03-12 16:56:27 -0700167
168 return dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_9_10_17 &&
169 obj_priv->tiling_mode != I915_TILING_NONE;
170}
171
Chris Wilson99a03df2010-05-27 14:15:34 +0100172static inline void
Eric Anholt40123c12009-03-09 13:42:30 -0700173slow_shmem_copy(struct page *dst_page,
174 int dst_offset,
175 struct page *src_page,
176 int src_offset,
177 int length)
178{
179 char *dst_vaddr, *src_vaddr;
180
Chris Wilson99a03df2010-05-27 14:15:34 +0100181 dst_vaddr = kmap(dst_page);
182 src_vaddr = kmap(src_page);
Eric Anholt40123c12009-03-09 13:42:30 -0700183
184 memcpy(dst_vaddr + dst_offset, src_vaddr + src_offset, length);
185
Chris Wilson99a03df2010-05-27 14:15:34 +0100186 kunmap(src_page);
187 kunmap(dst_page);
Eric Anholt40123c12009-03-09 13:42:30 -0700188}
189
Chris Wilson99a03df2010-05-27 14:15:34 +0100190static inline void
Eric Anholt280b7132009-03-12 16:56:27 -0700191slow_shmem_bit17_copy(struct page *gpu_page,
192 int gpu_offset,
193 struct page *cpu_page,
194 int cpu_offset,
195 int length,
196 int is_read)
197{
198 char *gpu_vaddr, *cpu_vaddr;
199
200 /* Use the unswizzled path if this page isn't affected. */
201 if ((page_to_phys(gpu_page) & (1 << 17)) == 0) {
202 if (is_read)
203 return slow_shmem_copy(cpu_page, cpu_offset,
204 gpu_page, gpu_offset, length);
205 else
206 return slow_shmem_copy(gpu_page, gpu_offset,
207 cpu_page, cpu_offset, length);
208 }
209
Chris Wilson99a03df2010-05-27 14:15:34 +0100210 gpu_vaddr = kmap(gpu_page);
211 cpu_vaddr = kmap(cpu_page);
Eric Anholt280b7132009-03-12 16:56:27 -0700212
213 /* Copy the data, XORing A6 with A17 (1). The user already knows he's
214 * XORing with the other bits (A9 for Y, A9 and A10 for X)
215 */
216 while (length > 0) {
217 int cacheline_end = ALIGN(gpu_offset + 1, 64);
218 int this_length = min(cacheline_end - gpu_offset, length);
219 int swizzled_gpu_offset = gpu_offset ^ 64;
220
221 if (is_read) {
222 memcpy(cpu_vaddr + cpu_offset,
223 gpu_vaddr + swizzled_gpu_offset,
224 this_length);
225 } else {
226 memcpy(gpu_vaddr + swizzled_gpu_offset,
227 cpu_vaddr + cpu_offset,
228 this_length);
229 }
230 cpu_offset += this_length;
231 gpu_offset += this_length;
232 length -= this_length;
233 }
234
Chris Wilson99a03df2010-05-27 14:15:34 +0100235 kunmap(cpu_page);
236 kunmap(gpu_page);
Eric Anholt280b7132009-03-12 16:56:27 -0700237}
238
Eric Anholt673a3942008-07-30 12:06:12 -0700239/**
Eric Anholteb014592009-03-10 11:44:52 -0700240 * This is the fast shmem pread path, which attempts to copy_from_user directly
241 * from the backing pages of the object to the user's address space. On a
242 * fault, it fails so we can fall back to i915_gem_shmem_pwrite_slow().
243 */
244static int
245i915_gem_shmem_pread_fast(struct drm_device *dev, struct drm_gem_object *obj,
246 struct drm_i915_gem_pread *args,
247 struct drm_file *file_priv)
248{
Daniel Vetter23010e42010-03-08 13:35:02 +0100249 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700250 ssize_t remain;
251 loff_t offset, page_base;
252 char __user *user_data;
253 int page_offset, page_length;
254 int ret;
255
256 user_data = (char __user *) (uintptr_t) args->data_ptr;
257 remain = args->size;
258
259 mutex_lock(&dev->struct_mutex);
260
Chris Wilson4bdadb92010-01-27 13:36:32 +0000261 ret = i915_gem_object_get_pages(obj, 0);
Eric Anholteb014592009-03-10 11:44:52 -0700262 if (ret != 0)
263 goto fail_unlock;
264
265 ret = i915_gem_object_set_cpu_read_domain_range(obj, args->offset,
266 args->size);
267 if (ret != 0)
268 goto fail_put_pages;
269
Daniel Vetter23010e42010-03-08 13:35:02 +0100270 obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700271 offset = args->offset;
272
273 while (remain > 0) {
274 /* Operation in this page
275 *
276 * page_base = page offset within aperture
277 * page_offset = offset within page
278 * page_length = bytes to copy for this page
279 */
280 page_base = (offset & ~(PAGE_SIZE-1));
281 page_offset = offset & (PAGE_SIZE-1);
282 page_length = remain;
283 if ((page_offset + remain) > PAGE_SIZE)
284 page_length = PAGE_SIZE - page_offset;
285
286 ret = fast_shmem_read(obj_priv->pages,
287 page_base, page_offset,
288 user_data, page_length);
289 if (ret)
290 goto fail_put_pages;
291
292 remain -= page_length;
293 user_data += page_length;
294 offset += page_length;
295 }
296
297fail_put_pages:
298 i915_gem_object_put_pages(obj);
299fail_unlock:
300 mutex_unlock(&dev->struct_mutex);
301
302 return ret;
303}
304
Chris Wilson07f73f62009-09-14 16:50:30 +0100305static int
306i915_gem_object_get_pages_or_evict(struct drm_gem_object *obj)
307{
308 int ret;
309
Chris Wilson4bdadb92010-01-27 13:36:32 +0000310 ret = i915_gem_object_get_pages(obj, __GFP_NORETRY | __GFP_NOWARN);
Chris Wilson07f73f62009-09-14 16:50:30 +0100311
312 /* If we've insufficient memory to map in the pages, attempt
313 * to make some space by throwing out some old buffers.
314 */
315 if (ret == -ENOMEM) {
316 struct drm_device *dev = obj->dev;
Chris Wilson07f73f62009-09-14 16:50:30 +0100317
Daniel Vetter0108a3e2010-08-07 11:01:21 +0100318 ret = i915_gem_evict_something(dev, obj->size,
319 i915_gem_get_gtt_alignment(obj));
Chris Wilson07f73f62009-09-14 16:50:30 +0100320 if (ret)
321 return ret;
322
Chris Wilson4bdadb92010-01-27 13:36:32 +0000323 ret = i915_gem_object_get_pages(obj, 0);
Chris Wilson07f73f62009-09-14 16:50:30 +0100324 }
325
326 return ret;
327}
328
Eric Anholteb014592009-03-10 11:44:52 -0700329/**
330 * This is the fallback shmem pread path, which allocates temporary storage
331 * in kernel space to copy_to_user into outside of the struct_mutex, so we
332 * can copy out of the object's backing pages while holding the struct mutex
333 * and not take page faults.
334 */
335static int
336i915_gem_shmem_pread_slow(struct drm_device *dev, struct drm_gem_object *obj,
337 struct drm_i915_gem_pread *args,
338 struct drm_file *file_priv)
339{
Daniel Vetter23010e42010-03-08 13:35:02 +0100340 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700341 struct mm_struct *mm = current->mm;
342 struct page **user_pages;
343 ssize_t remain;
344 loff_t offset, pinned_pages, i;
345 loff_t first_data_page, last_data_page, num_pages;
346 int shmem_page_index, shmem_page_offset;
347 int data_page_index, data_page_offset;
348 int page_length;
349 int ret;
350 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700351 int do_bit17_swizzling;
Eric Anholteb014592009-03-10 11:44:52 -0700352
353 remain = args->size;
354
355 /* Pin the user pages containing the data. We can't fault while
356 * holding the struct mutex, yet we want to hold it while
357 * dereferencing the user data.
358 */
359 first_data_page = data_ptr / PAGE_SIZE;
360 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
361 num_pages = last_data_page - first_data_page + 1;
362
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700363 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
Eric Anholteb014592009-03-10 11:44:52 -0700364 if (user_pages == NULL)
365 return -ENOMEM;
366
367 down_read(&mm->mmap_sem);
368 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
Eric Anholte5e9ecd2009-04-07 16:01:22 -0700369 num_pages, 1, 0, user_pages, NULL);
Eric Anholteb014592009-03-10 11:44:52 -0700370 up_read(&mm->mmap_sem);
371 if (pinned_pages < num_pages) {
372 ret = -EFAULT;
373 goto fail_put_user_pages;
374 }
375
Eric Anholt280b7132009-03-12 16:56:27 -0700376 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
377
Eric Anholteb014592009-03-10 11:44:52 -0700378 mutex_lock(&dev->struct_mutex);
379
Chris Wilson07f73f62009-09-14 16:50:30 +0100380 ret = i915_gem_object_get_pages_or_evict(obj);
381 if (ret)
Eric Anholteb014592009-03-10 11:44:52 -0700382 goto fail_unlock;
383
384 ret = i915_gem_object_set_cpu_read_domain_range(obj, args->offset,
385 args->size);
386 if (ret != 0)
387 goto fail_put_pages;
388
Daniel Vetter23010e42010-03-08 13:35:02 +0100389 obj_priv = to_intel_bo(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700390 offset = args->offset;
391
392 while (remain > 0) {
393 /* Operation in this page
394 *
395 * shmem_page_index = page number within shmem file
396 * shmem_page_offset = offset within page in shmem file
397 * data_page_index = page number in get_user_pages return
398 * data_page_offset = offset with data_page_index page.
399 * page_length = bytes to copy for this page
400 */
401 shmem_page_index = offset / PAGE_SIZE;
402 shmem_page_offset = offset & ~PAGE_MASK;
403 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
404 data_page_offset = data_ptr & ~PAGE_MASK;
405
406 page_length = remain;
407 if ((shmem_page_offset + page_length) > PAGE_SIZE)
408 page_length = PAGE_SIZE - shmem_page_offset;
409 if ((data_page_offset + page_length) > PAGE_SIZE)
410 page_length = PAGE_SIZE - data_page_offset;
411
Eric Anholt280b7132009-03-12 16:56:27 -0700412 if (do_bit17_swizzling) {
Chris Wilson99a03df2010-05-27 14:15:34 +0100413 slow_shmem_bit17_copy(obj_priv->pages[shmem_page_index],
Eric Anholt280b7132009-03-12 16:56:27 -0700414 shmem_page_offset,
Chris Wilson99a03df2010-05-27 14:15:34 +0100415 user_pages[data_page_index],
416 data_page_offset,
417 page_length,
418 1);
419 } else {
420 slow_shmem_copy(user_pages[data_page_index],
421 data_page_offset,
422 obj_priv->pages[shmem_page_index],
423 shmem_page_offset,
424 page_length);
Eric Anholt280b7132009-03-12 16:56:27 -0700425 }
Eric Anholteb014592009-03-10 11:44:52 -0700426
427 remain -= page_length;
428 data_ptr += page_length;
429 offset += page_length;
430 }
431
432fail_put_pages:
433 i915_gem_object_put_pages(obj);
434fail_unlock:
435 mutex_unlock(&dev->struct_mutex);
436fail_put_user_pages:
437 for (i = 0; i < pinned_pages; i++) {
438 SetPageDirty(user_pages[i]);
439 page_cache_release(user_pages[i]);
440 }
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700441 drm_free_large(user_pages);
Eric Anholteb014592009-03-10 11:44:52 -0700442
443 return ret;
444}
445
Eric Anholt673a3942008-07-30 12:06:12 -0700446/**
447 * Reads data from the object referenced by handle.
448 *
449 * On error, the contents of *data are undefined.
450 */
451int
452i915_gem_pread_ioctl(struct drm_device *dev, void *data,
453 struct drm_file *file_priv)
454{
455 struct drm_i915_gem_pread *args = data;
456 struct drm_gem_object *obj;
457 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -0700458 int ret;
459
460 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
461 if (obj == NULL)
462 return -EBADF;
Daniel Vetter23010e42010-03-08 13:35:02 +0100463 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700464
465 /* Bounds check source.
466 *
467 * XXX: This could use review for overflow issues...
468 */
469 if (args->offset > obj->size || args->size > obj->size ||
470 args->offset + args->size > obj->size) {
Luca Barbieribc9025b2010-02-09 05:49:12 +0000471 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700472 return -EINVAL;
473 }
474
Eric Anholt280b7132009-03-12 16:56:27 -0700475 if (i915_gem_object_needs_bit17_swizzle(obj)) {
Eric Anholteb014592009-03-10 11:44:52 -0700476 ret = i915_gem_shmem_pread_slow(dev, obj, args, file_priv);
Eric Anholt280b7132009-03-12 16:56:27 -0700477 } else {
478 ret = i915_gem_shmem_pread_fast(dev, obj, args, file_priv);
479 if (ret != 0)
480 ret = i915_gem_shmem_pread_slow(dev, obj, args,
481 file_priv);
482 }
Eric Anholt673a3942008-07-30 12:06:12 -0700483
Luca Barbieribc9025b2010-02-09 05:49:12 +0000484 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700485
Eric Anholteb014592009-03-10 11:44:52 -0700486 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700487}
488
Keith Packard0839ccb2008-10-30 19:38:48 -0700489/* This is the fast write path which cannot handle
490 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700491 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700492
Keith Packard0839ccb2008-10-30 19:38:48 -0700493static inline int
494fast_user_write(struct io_mapping *mapping,
495 loff_t page_base, int page_offset,
496 char __user *user_data,
497 int length)
498{
499 char *vaddr_atomic;
500 unsigned long unwritten;
501
Chris Wilsonfca3ec02010-08-04 14:34:24 +0100502 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base, KM_USER0);
Keith Packard0839ccb2008-10-30 19:38:48 -0700503 unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
504 user_data, length);
Chris Wilsonfca3ec02010-08-04 14:34:24 +0100505 io_mapping_unmap_atomic(vaddr_atomic, KM_USER0);
Keith Packard0839ccb2008-10-30 19:38:48 -0700506 if (unwritten)
507 return -EFAULT;
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700508 return 0;
Keith Packard0839ccb2008-10-30 19:38:48 -0700509}
510
511/* Here's the write path which can sleep for
512 * page faults
513 */
514
Chris Wilsonab34c222010-05-27 14:15:35 +0100515static inline void
Eric Anholt3de09aa2009-03-09 09:42:23 -0700516slow_kernel_write(struct io_mapping *mapping,
517 loff_t gtt_base, int gtt_offset,
518 struct page *user_page, int user_offset,
519 int length)
Keith Packard0839ccb2008-10-30 19:38:48 -0700520{
Chris Wilsonab34c222010-05-27 14:15:35 +0100521 char __iomem *dst_vaddr;
522 char *src_vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -0700523
Chris Wilsonab34c222010-05-27 14:15:35 +0100524 dst_vaddr = io_mapping_map_wc(mapping, gtt_base);
525 src_vaddr = kmap(user_page);
526
527 memcpy_toio(dst_vaddr + gtt_offset,
528 src_vaddr + user_offset,
529 length);
530
531 kunmap(user_page);
532 io_mapping_unmap(dst_vaddr);
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700533}
534
Eric Anholt40123c12009-03-09 13:42:30 -0700535static inline int
536fast_shmem_write(struct page **pages,
537 loff_t page_base, int page_offset,
538 char __user *data,
539 int length)
540{
541 char __iomem *vaddr;
Dave Airlied0088772009-03-28 20:29:48 -0400542 unsigned long unwritten;
Eric Anholt40123c12009-03-09 13:42:30 -0700543
544 vaddr = kmap_atomic(pages[page_base >> PAGE_SHIFT], KM_USER0);
545 if (vaddr == NULL)
546 return -ENOMEM;
Dave Airlied0088772009-03-28 20:29:48 -0400547 unwritten = __copy_from_user_inatomic(vaddr + page_offset, data, length);
Eric Anholt40123c12009-03-09 13:42:30 -0700548 kunmap_atomic(vaddr, KM_USER0);
549
Dave Airlied0088772009-03-28 20:29:48 -0400550 if (unwritten)
551 return -EFAULT;
Eric Anholt40123c12009-03-09 13:42:30 -0700552 return 0;
553}
554
Eric Anholt3de09aa2009-03-09 09:42:23 -0700555/**
556 * This is the fast pwrite path, where we copy the data directly from the
557 * user into the GTT, uncached.
558 */
Eric Anholt673a3942008-07-30 12:06:12 -0700559static int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700560i915_gem_gtt_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
561 struct drm_i915_gem_pwrite *args,
562 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700563{
Daniel Vetter23010e42010-03-08 13:35:02 +0100564 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Keith Packard0839ccb2008-10-30 19:38:48 -0700565 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700566 ssize_t remain;
Keith Packard0839ccb2008-10-30 19:38:48 -0700567 loff_t offset, page_base;
Eric Anholt673a3942008-07-30 12:06:12 -0700568 char __user *user_data;
Keith Packard0839ccb2008-10-30 19:38:48 -0700569 int page_offset, page_length;
570 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700571
572 user_data = (char __user *) (uintptr_t) args->data_ptr;
573 remain = args->size;
574 if (!access_ok(VERIFY_READ, user_data, remain))
575 return -EFAULT;
576
577
578 mutex_lock(&dev->struct_mutex);
579 ret = i915_gem_object_pin(obj, 0);
580 if (ret) {
581 mutex_unlock(&dev->struct_mutex);
582 return ret;
583 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800584 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
Eric Anholt673a3942008-07-30 12:06:12 -0700585 if (ret)
586 goto fail;
587
Daniel Vetter23010e42010-03-08 13:35:02 +0100588 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700589 offset = obj_priv->gtt_offset + args->offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700590
591 while (remain > 0) {
592 /* Operation in this page
593 *
Keith Packard0839ccb2008-10-30 19:38:48 -0700594 * page_base = page offset within aperture
595 * page_offset = offset within page
596 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -0700597 */
Keith Packard0839ccb2008-10-30 19:38:48 -0700598 page_base = (offset & ~(PAGE_SIZE-1));
599 page_offset = offset & (PAGE_SIZE-1);
600 page_length = remain;
601 if ((page_offset + remain) > PAGE_SIZE)
602 page_length = PAGE_SIZE - page_offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700603
Keith Packard0839ccb2008-10-30 19:38:48 -0700604 ret = fast_user_write (dev_priv->mm.gtt_mapping, page_base,
605 page_offset, user_data, page_length);
Eric Anholt673a3942008-07-30 12:06:12 -0700606
Keith Packard0839ccb2008-10-30 19:38:48 -0700607 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -0700608 * source page isn't available. Return the error and we'll
609 * retry in the slow path.
Keith Packard0839ccb2008-10-30 19:38:48 -0700610 */
Eric Anholt3de09aa2009-03-09 09:42:23 -0700611 if (ret)
612 goto fail;
Eric Anholt673a3942008-07-30 12:06:12 -0700613
Keith Packard0839ccb2008-10-30 19:38:48 -0700614 remain -= page_length;
615 user_data += page_length;
616 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700617 }
Eric Anholt673a3942008-07-30 12:06:12 -0700618
619fail:
620 i915_gem_object_unpin(obj);
621 mutex_unlock(&dev->struct_mutex);
622
623 return ret;
624}
625
Eric Anholt3de09aa2009-03-09 09:42:23 -0700626/**
627 * This is the fallback GTT pwrite path, which uses get_user_pages to pin
628 * the memory and maps it using kmap_atomic for copying.
629 *
630 * This code resulted in x11perf -rgb10text consuming about 10% more CPU
631 * than using i915_gem_gtt_pwrite_fast on a G45 (32-bit).
632 */
Eric Anholt3043c602008-10-02 12:24:47 -0700633static int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700634i915_gem_gtt_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
635 struct drm_i915_gem_pwrite *args,
636 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700637{
Daniel Vetter23010e42010-03-08 13:35:02 +0100638 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700639 drm_i915_private_t *dev_priv = dev->dev_private;
640 ssize_t remain;
641 loff_t gtt_page_base, offset;
642 loff_t first_data_page, last_data_page, num_pages;
643 loff_t pinned_pages, i;
644 struct page **user_pages;
645 struct mm_struct *mm = current->mm;
646 int gtt_page_offset, data_page_offset, data_page_index, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700647 int ret;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700648 uint64_t data_ptr = args->data_ptr;
649
650 remain = args->size;
651
652 /* Pin the user pages containing the data. We can't fault while
653 * holding the struct mutex, and all of the pwrite implementations
654 * want to hold it while dereferencing the user data.
655 */
656 first_data_page = data_ptr / PAGE_SIZE;
657 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
658 num_pages = last_data_page - first_data_page + 1;
659
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700660 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
Eric Anholt3de09aa2009-03-09 09:42:23 -0700661 if (user_pages == NULL)
662 return -ENOMEM;
663
664 down_read(&mm->mmap_sem);
665 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
666 num_pages, 0, 0, user_pages, NULL);
667 up_read(&mm->mmap_sem);
668 if (pinned_pages < num_pages) {
669 ret = -EFAULT;
670 goto out_unpin_pages;
671 }
672
673 mutex_lock(&dev->struct_mutex);
674 ret = i915_gem_object_pin(obj, 0);
675 if (ret)
676 goto out_unlock;
677
678 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
679 if (ret)
680 goto out_unpin_object;
681
Daniel Vetter23010e42010-03-08 13:35:02 +0100682 obj_priv = to_intel_bo(obj);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700683 offset = obj_priv->gtt_offset + args->offset;
684
685 while (remain > 0) {
686 /* Operation in this page
687 *
688 * gtt_page_base = page offset within aperture
689 * gtt_page_offset = offset within page in aperture
690 * data_page_index = page number in get_user_pages return
691 * data_page_offset = offset with data_page_index page.
692 * page_length = bytes to copy for this page
693 */
694 gtt_page_base = offset & PAGE_MASK;
695 gtt_page_offset = offset & ~PAGE_MASK;
696 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
697 data_page_offset = data_ptr & ~PAGE_MASK;
698
699 page_length = remain;
700 if ((gtt_page_offset + page_length) > PAGE_SIZE)
701 page_length = PAGE_SIZE - gtt_page_offset;
702 if ((data_page_offset + page_length) > PAGE_SIZE)
703 page_length = PAGE_SIZE - data_page_offset;
704
Chris Wilsonab34c222010-05-27 14:15:35 +0100705 slow_kernel_write(dev_priv->mm.gtt_mapping,
706 gtt_page_base, gtt_page_offset,
707 user_pages[data_page_index],
708 data_page_offset,
709 page_length);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700710
711 remain -= page_length;
712 offset += page_length;
713 data_ptr += page_length;
714 }
715
716out_unpin_object:
717 i915_gem_object_unpin(obj);
718out_unlock:
719 mutex_unlock(&dev->struct_mutex);
720out_unpin_pages:
721 for (i = 0; i < pinned_pages; i++)
722 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700723 drm_free_large(user_pages);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700724
725 return ret;
726}
727
Eric Anholt40123c12009-03-09 13:42:30 -0700728/**
729 * This is the fast shmem pwrite path, which attempts to directly
730 * copy_from_user into the kmapped pages backing the object.
731 */
Eric Anholt673a3942008-07-30 12:06:12 -0700732static int
Eric Anholt40123c12009-03-09 13:42:30 -0700733i915_gem_shmem_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
734 struct drm_i915_gem_pwrite *args,
735 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700736{
Daniel Vetter23010e42010-03-08 13:35:02 +0100737 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700738 ssize_t remain;
739 loff_t offset, page_base;
740 char __user *user_data;
741 int page_offset, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700742 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -0700743
744 user_data = (char __user *) (uintptr_t) args->data_ptr;
745 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700746
747 mutex_lock(&dev->struct_mutex);
748
Chris Wilson4bdadb92010-01-27 13:36:32 +0000749 ret = i915_gem_object_get_pages(obj, 0);
Eric Anholt40123c12009-03-09 13:42:30 -0700750 if (ret != 0)
751 goto fail_unlock;
752
Eric Anholte47c68e2008-11-14 13:35:19 -0800753 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Eric Anholt40123c12009-03-09 13:42:30 -0700754 if (ret != 0)
755 goto fail_put_pages;
Eric Anholt673a3942008-07-30 12:06:12 -0700756
Daniel Vetter23010e42010-03-08 13:35:02 +0100757 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700758 offset = args->offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700759 obj_priv->dirty = 1;
Eric Anholt673a3942008-07-30 12:06:12 -0700760
Eric Anholt40123c12009-03-09 13:42:30 -0700761 while (remain > 0) {
762 /* Operation in this page
763 *
764 * page_base = page offset within aperture
765 * page_offset = offset within page
766 * page_length = bytes to copy for this page
767 */
768 page_base = (offset & ~(PAGE_SIZE-1));
769 page_offset = offset & (PAGE_SIZE-1);
770 page_length = remain;
771 if ((page_offset + remain) > PAGE_SIZE)
772 page_length = PAGE_SIZE - page_offset;
773
774 ret = fast_shmem_write(obj_priv->pages,
775 page_base, page_offset,
776 user_data, page_length);
777 if (ret)
778 goto fail_put_pages;
779
780 remain -= page_length;
781 user_data += page_length;
782 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700783 }
784
Eric Anholt40123c12009-03-09 13:42:30 -0700785fail_put_pages:
786 i915_gem_object_put_pages(obj);
787fail_unlock:
Eric Anholt673a3942008-07-30 12:06:12 -0700788 mutex_unlock(&dev->struct_mutex);
789
Eric Anholt40123c12009-03-09 13:42:30 -0700790 return ret;
791}
792
793/**
794 * This is the fallback shmem pwrite path, which uses get_user_pages to pin
795 * the memory and maps it using kmap_atomic for copying.
796 *
797 * This avoids taking mmap_sem for faulting on the user's address while the
798 * struct_mutex is held.
799 */
800static int
801i915_gem_shmem_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
802 struct drm_i915_gem_pwrite *args,
803 struct drm_file *file_priv)
804{
Daniel Vetter23010e42010-03-08 13:35:02 +0100805 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700806 struct mm_struct *mm = current->mm;
807 struct page **user_pages;
808 ssize_t remain;
809 loff_t offset, pinned_pages, i;
810 loff_t first_data_page, last_data_page, num_pages;
811 int shmem_page_index, shmem_page_offset;
812 int data_page_index, data_page_offset;
813 int page_length;
814 int ret;
815 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700816 int do_bit17_swizzling;
Eric Anholt40123c12009-03-09 13:42:30 -0700817
818 remain = args->size;
819
820 /* Pin the user pages containing the data. We can't fault while
821 * holding the struct mutex, and all of the pwrite implementations
822 * want to hold it while dereferencing the user data.
823 */
824 first_data_page = data_ptr / PAGE_SIZE;
825 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
826 num_pages = last_data_page - first_data_page + 1;
827
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700828 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
Eric Anholt40123c12009-03-09 13:42:30 -0700829 if (user_pages == NULL)
830 return -ENOMEM;
831
832 down_read(&mm->mmap_sem);
833 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
834 num_pages, 0, 0, user_pages, NULL);
835 up_read(&mm->mmap_sem);
836 if (pinned_pages < num_pages) {
837 ret = -EFAULT;
838 goto fail_put_user_pages;
839 }
840
Eric Anholt280b7132009-03-12 16:56:27 -0700841 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
842
Eric Anholt40123c12009-03-09 13:42:30 -0700843 mutex_lock(&dev->struct_mutex);
844
Chris Wilson07f73f62009-09-14 16:50:30 +0100845 ret = i915_gem_object_get_pages_or_evict(obj);
846 if (ret)
Eric Anholt40123c12009-03-09 13:42:30 -0700847 goto fail_unlock;
848
849 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
850 if (ret != 0)
851 goto fail_put_pages;
852
Daniel Vetter23010e42010-03-08 13:35:02 +0100853 obj_priv = to_intel_bo(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700854 offset = args->offset;
855 obj_priv->dirty = 1;
856
857 while (remain > 0) {
858 /* Operation in this page
859 *
860 * shmem_page_index = page number within shmem file
861 * shmem_page_offset = offset within page in shmem file
862 * data_page_index = page number in get_user_pages return
863 * data_page_offset = offset with data_page_index page.
864 * page_length = bytes to copy for this page
865 */
866 shmem_page_index = offset / PAGE_SIZE;
867 shmem_page_offset = offset & ~PAGE_MASK;
868 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
869 data_page_offset = data_ptr & ~PAGE_MASK;
870
871 page_length = remain;
872 if ((shmem_page_offset + page_length) > PAGE_SIZE)
873 page_length = PAGE_SIZE - shmem_page_offset;
874 if ((data_page_offset + page_length) > PAGE_SIZE)
875 page_length = PAGE_SIZE - data_page_offset;
876
Eric Anholt280b7132009-03-12 16:56:27 -0700877 if (do_bit17_swizzling) {
Chris Wilson99a03df2010-05-27 14:15:34 +0100878 slow_shmem_bit17_copy(obj_priv->pages[shmem_page_index],
Eric Anholt280b7132009-03-12 16:56:27 -0700879 shmem_page_offset,
880 user_pages[data_page_index],
881 data_page_offset,
Chris Wilson99a03df2010-05-27 14:15:34 +0100882 page_length,
883 0);
884 } else {
885 slow_shmem_copy(obj_priv->pages[shmem_page_index],
886 shmem_page_offset,
887 user_pages[data_page_index],
888 data_page_offset,
889 page_length);
Eric Anholt280b7132009-03-12 16:56:27 -0700890 }
Eric Anholt40123c12009-03-09 13:42:30 -0700891
892 remain -= page_length;
893 data_ptr += page_length;
894 offset += page_length;
895 }
896
897fail_put_pages:
898 i915_gem_object_put_pages(obj);
899fail_unlock:
900 mutex_unlock(&dev->struct_mutex);
901fail_put_user_pages:
902 for (i = 0; i < pinned_pages; i++)
903 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700904 drm_free_large(user_pages);
Eric Anholt40123c12009-03-09 13:42:30 -0700905
906 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700907}
908
909/**
910 * Writes data to the object referenced by handle.
911 *
912 * On error, the contents of the buffer that were to be modified are undefined.
913 */
914int
915i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
916 struct drm_file *file_priv)
917{
918 struct drm_i915_gem_pwrite *args = data;
919 struct drm_gem_object *obj;
920 struct drm_i915_gem_object *obj_priv;
921 int ret = 0;
922
923 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
924 if (obj == NULL)
925 return -EBADF;
Daniel Vetter23010e42010-03-08 13:35:02 +0100926 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700927
928 /* Bounds check destination.
929 *
930 * XXX: This could use review for overflow issues...
931 */
932 if (args->offset > obj->size || args->size > obj->size ||
933 args->offset + args->size > obj->size) {
Luca Barbieribc9025b2010-02-09 05:49:12 +0000934 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700935 return -EINVAL;
936 }
937
938 /* We can only do the GTT pwrite on untiled buffers, as otherwise
939 * it would end up going through the fenced access, and we'll get
940 * different detiling behavior between reading and writing.
941 * pread/pwrite currently are reading and writing from the CPU
942 * perspective, requiring manual detiling by the client.
943 */
Dave Airlie71acb5e2008-12-30 20:31:46 +1000944 if (obj_priv->phys_obj)
945 ret = i915_gem_phys_pwrite(dev, obj, args, file_priv);
946 else if (obj_priv->tiling_mode == I915_TILING_NONE &&
Chris Wilson9b8c4a02010-05-27 14:21:01 +0100947 dev->gtt_total != 0 &&
948 obj->write_domain != I915_GEM_DOMAIN_CPU) {
Eric Anholt3de09aa2009-03-09 09:42:23 -0700949 ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file_priv);
950 if (ret == -EFAULT) {
951 ret = i915_gem_gtt_pwrite_slow(dev, obj, args,
952 file_priv);
953 }
Eric Anholt280b7132009-03-12 16:56:27 -0700954 } else if (i915_gem_object_needs_bit17_swizzle(obj)) {
955 ret = i915_gem_shmem_pwrite_slow(dev, obj, args, file_priv);
Eric Anholt40123c12009-03-09 13:42:30 -0700956 } else {
957 ret = i915_gem_shmem_pwrite_fast(dev, obj, args, file_priv);
958 if (ret == -EFAULT) {
959 ret = i915_gem_shmem_pwrite_slow(dev, obj, args,
960 file_priv);
961 }
962 }
Eric Anholt673a3942008-07-30 12:06:12 -0700963
964#if WATCH_PWRITE
965 if (ret)
966 DRM_INFO("pwrite failed %d\n", ret);
967#endif
968
Luca Barbieribc9025b2010-02-09 05:49:12 +0000969 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700970
971 return ret;
972}
973
974/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800975 * Called when user space prepares to use an object with the CPU, either
976 * through the mmap ioctl's mapping or a GTT mapping.
Eric Anholt673a3942008-07-30 12:06:12 -0700977 */
978int
979i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
980 struct drm_file *file_priv)
981{
Eric Anholta09ba7f2009-08-29 12:49:51 -0700982 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700983 struct drm_i915_gem_set_domain *args = data;
984 struct drm_gem_object *obj;
Jesse Barnes652c3932009-08-17 13:31:43 -0700985 struct drm_i915_gem_object *obj_priv;
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800986 uint32_t read_domains = args->read_domains;
987 uint32_t write_domain = args->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -0700988 int ret;
989
990 if (!(dev->driver->driver_features & DRIVER_GEM))
991 return -ENODEV;
992
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800993 /* Only handle setting domains to types used by the CPU. */
Chris Wilson21d509e2009-06-06 09:46:02 +0100994 if (write_domain & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800995 return -EINVAL;
996
Chris Wilson21d509e2009-06-06 09:46:02 +0100997 if (read_domains & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800998 return -EINVAL;
999
1000 /* Having something in the write domain implies it's in the read
1001 * domain, and only that read domain. Enforce that in the request.
1002 */
1003 if (write_domain != 0 && read_domains != write_domain)
1004 return -EINVAL;
1005
Eric Anholt673a3942008-07-30 12:06:12 -07001006 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1007 if (obj == NULL)
1008 return -EBADF;
Daniel Vetter23010e42010-03-08 13:35:02 +01001009 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001010
1011 mutex_lock(&dev->struct_mutex);
Jesse Barnes652c3932009-08-17 13:31:43 -07001012
1013 intel_mark_busy(dev, obj);
1014
Eric Anholt673a3942008-07-30 12:06:12 -07001015#if WATCH_BUF
Krzysztof Halasacfd43c02009-06-20 00:31:28 +02001016 DRM_INFO("set_domain_ioctl %p(%zd), %08x %08x\n",
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001017 obj, obj->size, read_domains, write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07001018#endif
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001019 if (read_domains & I915_GEM_DOMAIN_GTT) {
1020 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
Eric Anholt02354392008-11-26 13:58:13 -08001021
Eric Anholta09ba7f2009-08-29 12:49:51 -07001022 /* Update the LRU on the fence for the CPU access that's
1023 * about to occur.
1024 */
1025 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001026 struct drm_i915_fence_reg *reg =
1027 &dev_priv->fence_regs[obj_priv->fence_reg];
1028 list_move_tail(&reg->lru_list,
Eric Anholta09ba7f2009-08-29 12:49:51 -07001029 &dev_priv->mm.fence_list);
1030 }
1031
Eric Anholt02354392008-11-26 13:58:13 -08001032 /* Silently promote "you're not bound, there was nothing to do"
1033 * to success, since the client was just asking us to
1034 * make sure everything was done.
1035 */
1036 if (ret == -EINVAL)
1037 ret = 0;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001038 } else {
Eric Anholte47c68e2008-11-14 13:35:19 -08001039 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001040 }
1041
Eric Anholt673a3942008-07-30 12:06:12 -07001042 drm_gem_object_unreference(obj);
1043 mutex_unlock(&dev->struct_mutex);
1044 return ret;
1045}
1046
1047/**
1048 * Called when user space has done writes to this buffer
1049 */
1050int
1051i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
1052 struct drm_file *file_priv)
1053{
1054 struct drm_i915_gem_sw_finish *args = data;
1055 struct drm_gem_object *obj;
1056 struct drm_i915_gem_object *obj_priv;
1057 int ret = 0;
1058
1059 if (!(dev->driver->driver_features & DRIVER_GEM))
1060 return -ENODEV;
1061
1062 mutex_lock(&dev->struct_mutex);
1063 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1064 if (obj == NULL) {
1065 mutex_unlock(&dev->struct_mutex);
1066 return -EBADF;
1067 }
1068
1069#if WATCH_BUF
Krzysztof Halasacfd43c02009-06-20 00:31:28 +02001070 DRM_INFO("%s: sw_finish %d (%p %zd)\n",
Eric Anholt673a3942008-07-30 12:06:12 -07001071 __func__, args->handle, obj, obj->size);
1072#endif
Daniel Vetter23010e42010-03-08 13:35:02 +01001073 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001074
1075 /* Pinned buffers may be scanout, so flush the cache */
Eric Anholte47c68e2008-11-14 13:35:19 -08001076 if (obj_priv->pin_count)
1077 i915_gem_object_flush_cpu_write_domain(obj);
1078
Eric Anholt673a3942008-07-30 12:06:12 -07001079 drm_gem_object_unreference(obj);
1080 mutex_unlock(&dev->struct_mutex);
1081 return ret;
1082}
1083
1084/**
1085 * Maps the contents of an object, returning the address it is mapped
1086 * into.
1087 *
1088 * While the mapping holds a reference on the contents of the object, it doesn't
1089 * imply a ref on the object itself.
1090 */
1091int
1092i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
1093 struct drm_file *file_priv)
1094{
1095 struct drm_i915_gem_mmap *args = data;
1096 struct drm_gem_object *obj;
1097 loff_t offset;
1098 unsigned long addr;
1099
1100 if (!(dev->driver->driver_features & DRIVER_GEM))
1101 return -ENODEV;
1102
1103 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1104 if (obj == NULL)
1105 return -EBADF;
1106
1107 offset = args->offset;
1108
1109 down_write(&current->mm->mmap_sem);
1110 addr = do_mmap(obj->filp, 0, args->size,
1111 PROT_READ | PROT_WRITE, MAP_SHARED,
1112 args->offset);
1113 up_write(&current->mm->mmap_sem);
Luca Barbieribc9025b2010-02-09 05:49:12 +00001114 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001115 if (IS_ERR((void *)addr))
1116 return addr;
1117
1118 args->addr_ptr = (uint64_t) addr;
1119
1120 return 0;
1121}
1122
Jesse Barnesde151cf2008-11-12 10:03:55 -08001123/**
1124 * i915_gem_fault - fault a page into the GTT
1125 * vma: VMA in question
1126 * vmf: fault info
1127 *
1128 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1129 * from userspace. The fault handler takes care of binding the object to
1130 * the GTT (if needed), allocating and programming a fence register (again,
1131 * only if needed based on whether the old reg is still valid or the object
1132 * is tiled) and inserting a new PTE into the faulting process.
1133 *
1134 * Note that the faulting process may involve evicting existing objects
1135 * from the GTT and/or fence registers to make room. So performance may
1136 * suffer if the GTT working set is large or there are few fence registers
1137 * left.
1138 */
1139int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1140{
1141 struct drm_gem_object *obj = vma->vm_private_data;
1142 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001143 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001144 pgoff_t page_offset;
1145 unsigned long pfn;
1146 int ret = 0;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001147 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001148
1149 /* We don't use vmf->pgoff since that has the fake offset */
1150 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1151 PAGE_SHIFT;
1152
1153 /* Now bind it into the GTT if needed */
1154 mutex_lock(&dev->struct_mutex);
1155 if (!obj_priv->gtt_space) {
Chris Wilsone67b8ce2009-09-14 16:50:26 +01001156 ret = i915_gem_object_bind_to_gtt(obj, 0);
Chris Wilsonc7150892009-09-23 00:43:56 +01001157 if (ret)
1158 goto unlock;
Kristian Høgsberg07f4f3e2009-05-27 14:37:28 -04001159
Jesse Barnesde151cf2008-11-12 10:03:55 -08001160 ret = i915_gem_object_set_to_gtt_domain(obj, write);
Chris Wilsonc7150892009-09-23 00:43:56 +01001161 if (ret)
1162 goto unlock;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001163 }
1164
1165 /* Need a new fence register? */
Eric Anholta09ba7f2009-08-29 12:49:51 -07001166 if (obj_priv->tiling_mode != I915_TILING_NONE) {
Chris Wilson8c4b8c32009-06-17 22:08:52 +01001167 ret = i915_gem_object_get_fence_reg(obj);
Chris Wilsonc7150892009-09-23 00:43:56 +01001168 if (ret)
1169 goto unlock;
Eric Anholtd9ddcb92009-01-27 10:33:49 -08001170 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001171
1172 pfn = ((dev->agp->base + obj_priv->gtt_offset) >> PAGE_SHIFT) +
1173 page_offset;
1174
1175 /* Finally, remap it using the new GTT offset */
1176 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
Chris Wilsonc7150892009-09-23 00:43:56 +01001177unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001178 mutex_unlock(&dev->struct_mutex);
1179
1180 switch (ret) {
Chris Wilsonc7150892009-09-23 00:43:56 +01001181 case 0:
1182 case -ERESTARTSYS:
1183 return VM_FAULT_NOPAGE;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001184 case -ENOMEM:
1185 case -EAGAIN:
1186 return VM_FAULT_OOM;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001187 default:
Chris Wilsonc7150892009-09-23 00:43:56 +01001188 return VM_FAULT_SIGBUS;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001189 }
1190}
1191
1192/**
1193 * i915_gem_create_mmap_offset - create a fake mmap offset for an object
1194 * @obj: obj in question
1195 *
1196 * GEM memory mapping works by handing back to userspace a fake mmap offset
1197 * it can use in a subsequent mmap(2) call. The DRM core code then looks
1198 * up the object based on the offset and sets up the various memory mapping
1199 * structures.
1200 *
1201 * This routine allocates and attaches a fake offset for @obj.
1202 */
1203static int
1204i915_gem_create_mmap_offset(struct drm_gem_object *obj)
1205{
1206 struct drm_device *dev = obj->dev;
1207 struct drm_gem_mm *mm = dev->mm_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001208 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001209 struct drm_map_list *list;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +11001210 struct drm_local_map *map;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001211 int ret = 0;
1212
1213 /* Set the object up for mmap'ing */
1214 list = &obj->map_list;
Eric Anholt9a298b22009-03-24 12:23:04 -07001215 list->map = kzalloc(sizeof(struct drm_map_list), GFP_KERNEL);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001216 if (!list->map)
1217 return -ENOMEM;
1218
1219 map = list->map;
1220 map->type = _DRM_GEM;
1221 map->size = obj->size;
1222 map->handle = obj;
1223
1224 /* Get a DRM GEM mmap offset allocated... */
1225 list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
1226 obj->size / PAGE_SIZE, 0, 0);
1227 if (!list->file_offset_node) {
1228 DRM_ERROR("failed to allocate offset for bo %d\n", obj->name);
1229 ret = -ENOMEM;
1230 goto out_free_list;
1231 }
1232
1233 list->file_offset_node = drm_mm_get_block(list->file_offset_node,
1234 obj->size / PAGE_SIZE, 0);
1235 if (!list->file_offset_node) {
1236 ret = -ENOMEM;
1237 goto out_free_list;
1238 }
1239
1240 list->hash.key = list->file_offset_node->start;
1241 if (drm_ht_insert_item(&mm->offset_hash, &list->hash)) {
1242 DRM_ERROR("failed to add to map hash\n");
Chris Wilson5618ca62009-12-02 15:15:30 +00001243 ret = -ENOMEM;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001244 goto out_free_mm;
1245 }
1246
1247 /* By now we should be all set, any drm_mmap request on the offset
1248 * below will get to our mmap & fault handler */
1249 obj_priv->mmap_offset = ((uint64_t) list->hash.key) << PAGE_SHIFT;
1250
1251 return 0;
1252
1253out_free_mm:
1254 drm_mm_put_block(list->file_offset_node);
1255out_free_list:
Eric Anholt9a298b22009-03-24 12:23:04 -07001256 kfree(list->map);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001257
1258 return ret;
1259}
1260
Chris Wilson901782b2009-07-10 08:18:50 +01001261/**
1262 * i915_gem_release_mmap - remove physical page mappings
1263 * @obj: obj in question
1264 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001265 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01001266 * relinquish ownership of the pages back to the system.
1267 *
1268 * It is vital that we remove the page mapping if we have mapped a tiled
1269 * object through the GTT and then lose the fence register due to
1270 * resource pressure. Similarly if the object has been moved out of the
1271 * aperture, than pages mapped into userspace must be revoked. Removing the
1272 * mapping will then trigger a page fault on the next user access, allowing
1273 * fixup by i915_gem_fault().
1274 */
Eric Anholtd05ca302009-07-10 13:02:26 -07001275void
Chris Wilson901782b2009-07-10 08:18:50 +01001276i915_gem_release_mmap(struct drm_gem_object *obj)
1277{
1278 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001279 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson901782b2009-07-10 08:18:50 +01001280
1281 if (dev->dev_mapping)
1282 unmap_mapping_range(dev->dev_mapping,
1283 obj_priv->mmap_offset, obj->size, 1);
1284}
1285
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001286static void
1287i915_gem_free_mmap_offset(struct drm_gem_object *obj)
1288{
1289 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001290 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001291 struct drm_gem_mm *mm = dev->mm_private;
1292 struct drm_map_list *list;
1293
1294 list = &obj->map_list;
1295 drm_ht_remove_item(&mm->offset_hash, &list->hash);
1296
1297 if (list->file_offset_node) {
1298 drm_mm_put_block(list->file_offset_node);
1299 list->file_offset_node = NULL;
1300 }
1301
1302 if (list->map) {
Eric Anholt9a298b22009-03-24 12:23:04 -07001303 kfree(list->map);
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001304 list->map = NULL;
1305 }
1306
1307 obj_priv->mmap_offset = 0;
1308}
1309
Jesse Barnesde151cf2008-11-12 10:03:55 -08001310/**
1311 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1312 * @obj: object to check
1313 *
1314 * Return the required GTT alignment for an object, taking into account
1315 * potential fence register mapping if needed.
1316 */
1317static uint32_t
1318i915_gem_get_gtt_alignment(struct drm_gem_object *obj)
1319{
1320 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001321 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001322 int start, i;
1323
1324 /*
1325 * Minimum alignment is 4k (GTT page size), but might be greater
1326 * if a fence register is needed for the object.
1327 */
1328 if (IS_I965G(dev) || obj_priv->tiling_mode == I915_TILING_NONE)
1329 return 4096;
1330
1331 /*
1332 * Previous chips need to be aligned to the size of the smallest
1333 * fence register that can contain the object.
1334 */
1335 if (IS_I9XX(dev))
1336 start = 1024*1024;
1337 else
1338 start = 512*1024;
1339
1340 for (i = start; i < obj->size; i <<= 1)
1341 ;
1342
1343 return i;
1344}
1345
1346/**
1347 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1348 * @dev: DRM device
1349 * @data: GTT mapping ioctl data
1350 * @file_priv: GEM object info
1351 *
1352 * Simply returns the fake offset to userspace so it can mmap it.
1353 * The mmap call will end up in drm_gem_mmap(), which will set things
1354 * up so we can get faults in the handler above.
1355 *
1356 * The fault handler will take care of binding the object into the GTT
1357 * (since it may have been evicted to make room for something), allocating
1358 * a fence register, and mapping the appropriate aperture address into
1359 * userspace.
1360 */
1361int
1362i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
1363 struct drm_file *file_priv)
1364{
1365 struct drm_i915_gem_mmap_gtt *args = data;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001366 struct drm_gem_object *obj;
1367 struct drm_i915_gem_object *obj_priv;
1368 int ret;
1369
1370 if (!(dev->driver->driver_features & DRIVER_GEM))
1371 return -ENODEV;
1372
1373 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1374 if (obj == NULL)
1375 return -EBADF;
1376
1377 mutex_lock(&dev->struct_mutex);
1378
Daniel Vetter23010e42010-03-08 13:35:02 +01001379 obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001380
Chris Wilsonab182822009-09-22 18:46:17 +01001381 if (obj_priv->madv != I915_MADV_WILLNEED) {
1382 DRM_ERROR("Attempting to mmap a purgeable buffer\n");
1383 drm_gem_object_unreference(obj);
1384 mutex_unlock(&dev->struct_mutex);
1385 return -EINVAL;
1386 }
1387
1388
Jesse Barnesde151cf2008-11-12 10:03:55 -08001389 if (!obj_priv->mmap_offset) {
1390 ret = i915_gem_create_mmap_offset(obj);
Chris Wilson13af1062009-02-11 14:26:31 +00001391 if (ret) {
1392 drm_gem_object_unreference(obj);
1393 mutex_unlock(&dev->struct_mutex);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001394 return ret;
Chris Wilson13af1062009-02-11 14:26:31 +00001395 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001396 }
1397
1398 args->offset = obj_priv->mmap_offset;
1399
Jesse Barnesde151cf2008-11-12 10:03:55 -08001400 /*
1401 * Pull it into the GTT so that we have a page list (makes the
1402 * initial fault faster and any subsequent flushing possible).
1403 */
1404 if (!obj_priv->agp_mem) {
Chris Wilsone67b8ce2009-09-14 16:50:26 +01001405 ret = i915_gem_object_bind_to_gtt(obj, 0);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001406 if (ret) {
1407 drm_gem_object_unreference(obj);
1408 mutex_unlock(&dev->struct_mutex);
1409 return ret;
1410 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001411 }
1412
1413 drm_gem_object_unreference(obj);
1414 mutex_unlock(&dev->struct_mutex);
1415
1416 return 0;
1417}
1418
Ben Gamari6911a9b2009-04-02 11:24:54 -07001419void
Eric Anholt856fa192009-03-19 14:10:50 -07001420i915_gem_object_put_pages(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07001421{
Daniel Vetter23010e42010-03-08 13:35:02 +01001422 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001423 int page_count = obj->size / PAGE_SIZE;
1424 int i;
1425
Eric Anholt856fa192009-03-19 14:10:50 -07001426 BUG_ON(obj_priv->pages_refcount == 0);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001427 BUG_ON(obj_priv->madv == __I915_MADV_PURGED);
Eric Anholt856fa192009-03-19 14:10:50 -07001428
1429 if (--obj_priv->pages_refcount != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07001430 return;
1431
Eric Anholt280b7132009-03-12 16:56:27 -07001432 if (obj_priv->tiling_mode != I915_TILING_NONE)
1433 i915_gem_object_save_bit_17_swizzle(obj);
1434
Chris Wilson3ef94da2009-09-14 16:50:29 +01001435 if (obj_priv->madv == I915_MADV_DONTNEED)
Chris Wilson13a05fd2009-09-20 23:03:19 +01001436 obj_priv->dirty = 0;
Chris Wilson3ef94da2009-09-14 16:50:29 +01001437
1438 for (i = 0; i < page_count; i++) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01001439 if (obj_priv->dirty)
1440 set_page_dirty(obj_priv->pages[i]);
1441
1442 if (obj_priv->madv == I915_MADV_WILLNEED)
Eric Anholt856fa192009-03-19 14:10:50 -07001443 mark_page_accessed(obj_priv->pages[i]);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001444
1445 page_cache_release(obj_priv->pages[i]);
1446 }
Eric Anholt673a3942008-07-30 12:06:12 -07001447 obj_priv->dirty = 0;
1448
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07001449 drm_free_large(obj_priv->pages);
Eric Anholt856fa192009-03-19 14:10:50 -07001450 obj_priv->pages = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001451}
1452
1453static void
Zou Nan hai852835f2010-05-21 09:08:56 +08001454i915_gem_object_move_to_active(struct drm_gem_object *obj, uint32_t seqno,
1455 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001456{
1457 struct drm_device *dev = obj->dev;
1458 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001459 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Zou Nan hai852835f2010-05-21 09:08:56 +08001460 BUG_ON(ring == NULL);
1461 obj_priv->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001462
1463 /* Add a reference if we're newly entering the active list. */
1464 if (!obj_priv->active) {
1465 drm_gem_object_reference(obj);
1466 obj_priv->active = 1;
1467 }
1468 /* Move from whatever list we were on to the tail of execution. */
Carl Worth5e118f42009-03-20 11:54:25 -07001469 spin_lock(&dev_priv->mm.active_list_lock);
Zou Nan hai852835f2010-05-21 09:08:56 +08001470 list_move_tail(&obj_priv->list, &ring->active_list);
Carl Worth5e118f42009-03-20 11:54:25 -07001471 spin_unlock(&dev_priv->mm.active_list_lock);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001472 obj_priv->last_rendering_seqno = seqno;
Eric Anholt673a3942008-07-30 12:06:12 -07001473}
1474
Eric Anholtce44b0e2008-11-06 16:00:31 -08001475static void
1476i915_gem_object_move_to_flushing(struct drm_gem_object *obj)
1477{
1478 struct drm_device *dev = obj->dev;
1479 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001480 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001481
1482 BUG_ON(!obj_priv->active);
1483 list_move_tail(&obj_priv->list, &dev_priv->mm.flushing_list);
1484 obj_priv->last_rendering_seqno = 0;
1485}
Eric Anholt673a3942008-07-30 12:06:12 -07001486
Chris Wilson963b4832009-09-20 23:03:54 +01001487/* Immediately discard the backing storage */
1488static void
1489i915_gem_object_truncate(struct drm_gem_object *obj)
1490{
Daniel Vetter23010e42010-03-08 13:35:02 +01001491 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001492 struct inode *inode;
Chris Wilson963b4832009-09-20 23:03:54 +01001493
Chris Wilsonbb6baf72009-09-22 14:24:13 +01001494 inode = obj->filp->f_path.dentry->d_inode;
1495 if (inode->i_op->truncate)
1496 inode->i_op->truncate (inode);
1497
1498 obj_priv->madv = __I915_MADV_PURGED;
Chris Wilson963b4832009-09-20 23:03:54 +01001499}
1500
1501static inline int
1502i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj_priv)
1503{
1504 return obj_priv->madv == I915_MADV_DONTNEED;
1505}
1506
Eric Anholt673a3942008-07-30 12:06:12 -07001507static void
1508i915_gem_object_move_to_inactive(struct drm_gem_object *obj)
1509{
1510 struct drm_device *dev = obj->dev;
1511 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001512 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001513
1514 i915_verify_inactive(dev, __FILE__, __LINE__);
1515 if (obj_priv->pin_count != 0)
1516 list_del_init(&obj_priv->list);
1517 else
1518 list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
1519
Daniel Vetter99fcb762010-02-07 16:20:18 +01001520 BUG_ON(!list_empty(&obj_priv->gpu_write_list));
1521
Eric Anholtce44b0e2008-11-06 16:00:31 -08001522 obj_priv->last_rendering_seqno = 0;
Zou Nan hai852835f2010-05-21 09:08:56 +08001523 obj_priv->ring = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001524 if (obj_priv->active) {
1525 obj_priv->active = 0;
1526 drm_gem_object_unreference(obj);
1527 }
1528 i915_verify_inactive(dev, __FILE__, __LINE__);
1529}
1530
Daniel Vetter63560392010-02-19 11:51:59 +01001531static void
1532i915_gem_process_flushing_list(struct drm_device *dev,
Zou Nan hai852835f2010-05-21 09:08:56 +08001533 uint32_t flush_domains, uint32_t seqno,
1534 struct intel_ring_buffer *ring)
Daniel Vetter63560392010-02-19 11:51:59 +01001535{
1536 drm_i915_private_t *dev_priv = dev->dev_private;
1537 struct drm_i915_gem_object *obj_priv, *next;
1538
1539 list_for_each_entry_safe(obj_priv, next,
1540 &dev_priv->mm.gpu_write_list,
1541 gpu_write_list) {
Daniel Vettera8089e82010-04-09 19:05:09 +00001542 struct drm_gem_object *obj = &obj_priv->base;
Daniel Vetter63560392010-02-19 11:51:59 +01001543
1544 if ((obj->write_domain & flush_domains) ==
Zou Nan hai852835f2010-05-21 09:08:56 +08001545 obj->write_domain &&
1546 obj_priv->ring->ring_flag == ring->ring_flag) {
Daniel Vetter63560392010-02-19 11:51:59 +01001547 uint32_t old_write_domain = obj->write_domain;
1548
1549 obj->write_domain = 0;
1550 list_del_init(&obj_priv->gpu_write_list);
Zou Nan hai852835f2010-05-21 09:08:56 +08001551 i915_gem_object_move_to_active(obj, seqno, ring);
Daniel Vetter63560392010-02-19 11:51:59 +01001552
1553 /* update the fence lru list */
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001554 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
1555 struct drm_i915_fence_reg *reg =
1556 &dev_priv->fence_regs[obj_priv->fence_reg];
1557 list_move_tail(&reg->lru_list,
Daniel Vetter63560392010-02-19 11:51:59 +01001558 &dev_priv->mm.fence_list);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02001559 }
Daniel Vetter63560392010-02-19 11:51:59 +01001560
1561 trace_i915_gem_object_change_domain(obj,
1562 obj->read_domains,
1563 old_write_domain);
1564 }
1565 }
1566}
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001567
Daniel Vetter5a5a0c62009-09-15 22:57:36 +02001568uint32_t
Eric Anholtb9624422009-06-03 07:27:35 +00001569i915_add_request(struct drm_device *dev, struct drm_file *file_priv,
Zou Nan hai852835f2010-05-21 09:08:56 +08001570 uint32_t flush_domains, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001571{
1572 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtb9624422009-06-03 07:27:35 +00001573 struct drm_i915_file_private *i915_file_priv = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001574 struct drm_i915_gem_request *request;
1575 uint32_t seqno;
1576 int was_empty;
Eric Anholt673a3942008-07-30 12:06:12 -07001577
Eric Anholtb9624422009-06-03 07:27:35 +00001578 if (file_priv != NULL)
1579 i915_file_priv = file_priv->driver_priv;
1580
Eric Anholt9a298b22009-03-24 12:23:04 -07001581 request = kzalloc(sizeof(*request), GFP_KERNEL);
Eric Anholt673a3942008-07-30 12:06:12 -07001582 if (request == NULL)
1583 return 0;
1584
Zou Nan hai852835f2010-05-21 09:08:56 +08001585 seqno = ring->add_request(dev, ring, file_priv, flush_domains);
Eric Anholt673a3942008-07-30 12:06:12 -07001586
1587 request->seqno = seqno;
Zou Nan hai852835f2010-05-21 09:08:56 +08001588 request->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001589 request->emitted_jiffies = jiffies;
Zou Nan hai852835f2010-05-21 09:08:56 +08001590 was_empty = list_empty(&ring->request_list);
1591 list_add_tail(&request->list, &ring->request_list);
1592
Eric Anholtb9624422009-06-03 07:27:35 +00001593 if (i915_file_priv) {
1594 list_add_tail(&request->client_list,
1595 &i915_file_priv->mm.request_list);
1596 } else {
1597 INIT_LIST_HEAD(&request->client_list);
1598 }
Eric Anholt673a3942008-07-30 12:06:12 -07001599
Eric Anholtce44b0e2008-11-06 16:00:31 -08001600 /* Associate any objects on the flushing list matching the write
1601 * domain we're flushing with our flush.
1602 */
Daniel Vetter63560392010-02-19 11:51:59 +01001603 if (flush_domains != 0)
Zou Nan hai852835f2010-05-21 09:08:56 +08001604 i915_gem_process_flushing_list(dev, flush_domains, seqno, ring);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001605
Ben Gamarif65d9422009-09-14 17:48:44 -04001606 if (!dev_priv->mm.suspended) {
1607 mod_timer(&dev_priv->hangcheck_timer, jiffies + DRM_I915_HANGCHECK_PERIOD);
1608 if (was_empty)
1609 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
1610 }
Eric Anholt673a3942008-07-30 12:06:12 -07001611 return seqno;
1612}
1613
1614/**
1615 * Command execution barrier
1616 *
1617 * Ensures that all commands in the ring are finished
1618 * before signalling the CPU
1619 */
Eric Anholt3043c602008-10-02 12:24:47 -07001620static uint32_t
Zou Nan hai852835f2010-05-21 09:08:56 +08001621i915_retire_commands(struct drm_device *dev, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001622{
Eric Anholt673a3942008-07-30 12:06:12 -07001623 uint32_t flush_domains = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001624
1625 /* The sampler always gets flushed on i965 (sigh) */
1626 if (IS_I965G(dev))
1627 flush_domains |= I915_GEM_DOMAIN_SAMPLER;
Zou Nan hai852835f2010-05-21 09:08:56 +08001628
1629 ring->flush(dev, ring,
1630 I915_GEM_DOMAIN_COMMAND, flush_domains);
Eric Anholt673a3942008-07-30 12:06:12 -07001631 return flush_domains;
1632}
1633
1634/**
1635 * Moves buffers associated only with the given active seqno from the active
1636 * to inactive list, potentially freeing them.
1637 */
1638static void
1639i915_gem_retire_request(struct drm_device *dev,
1640 struct drm_i915_gem_request *request)
1641{
1642 drm_i915_private_t *dev_priv = dev->dev_private;
1643
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001644 trace_i915_gem_request_retire(dev, request->seqno);
1645
Eric Anholt673a3942008-07-30 12:06:12 -07001646 /* Move any buffers on the active list that are no longer referenced
1647 * by the ringbuffer to the flushing/inactive lists as appropriate.
1648 */
Carl Worth5e118f42009-03-20 11:54:25 -07001649 spin_lock(&dev_priv->mm.active_list_lock);
Zou Nan hai852835f2010-05-21 09:08:56 +08001650 while (!list_empty(&request->ring->active_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001651 struct drm_gem_object *obj;
1652 struct drm_i915_gem_object *obj_priv;
1653
Zou Nan hai852835f2010-05-21 09:08:56 +08001654 obj_priv = list_first_entry(&request->ring->active_list,
Eric Anholt673a3942008-07-30 12:06:12 -07001655 struct drm_i915_gem_object,
1656 list);
Daniel Vettera8089e82010-04-09 19:05:09 +00001657 obj = &obj_priv->base;
Eric Anholt673a3942008-07-30 12:06:12 -07001658
1659 /* If the seqno being retired doesn't match the oldest in the
1660 * list, then the oldest in the list must still be newer than
1661 * this seqno.
1662 */
1663 if (obj_priv->last_rendering_seqno != request->seqno)
Carl Worth5e118f42009-03-20 11:54:25 -07001664 goto out;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001665
Eric Anholt673a3942008-07-30 12:06:12 -07001666#if WATCH_LRU
1667 DRM_INFO("%s: retire %d moves to inactive list %p\n",
1668 __func__, request->seqno, obj);
1669#endif
1670
Eric Anholtce44b0e2008-11-06 16:00:31 -08001671 if (obj->write_domain != 0)
1672 i915_gem_object_move_to_flushing(obj);
Shaohua Li68c84342009-04-08 10:58:23 +08001673 else {
1674 /* Take a reference on the object so it won't be
1675 * freed while the spinlock is held. The list
1676 * protection for this spinlock is safe when breaking
1677 * the lock like this since the next thing we do
1678 * is just get the head of the list again.
1679 */
1680 drm_gem_object_reference(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001681 i915_gem_object_move_to_inactive(obj);
Shaohua Li68c84342009-04-08 10:58:23 +08001682 spin_unlock(&dev_priv->mm.active_list_lock);
1683 drm_gem_object_unreference(obj);
1684 spin_lock(&dev_priv->mm.active_list_lock);
1685 }
Eric Anholt673a3942008-07-30 12:06:12 -07001686 }
Carl Worth5e118f42009-03-20 11:54:25 -07001687out:
1688 spin_unlock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001689}
1690
1691/**
1692 * Returns true if seq1 is later than seq2.
1693 */
Ben Gamari22be1722009-09-14 17:48:43 -04001694bool
Eric Anholt673a3942008-07-30 12:06:12 -07001695i915_seqno_passed(uint32_t seq1, uint32_t seq2)
1696{
1697 return (int32_t)(seq1 - seq2) >= 0;
1698}
1699
1700uint32_t
Zou Nan hai852835f2010-05-21 09:08:56 +08001701i915_get_gem_seqno(struct drm_device *dev,
Zou Nan haid1b851f2010-05-21 09:08:57 +08001702 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001703{
Zou Nan hai852835f2010-05-21 09:08:56 +08001704 return ring->get_gem_seqno(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07001705}
1706
1707/**
1708 * This function clears the request list as sequence numbers are passed.
1709 */
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001710static void
1711i915_gem_retire_requests_ring(struct drm_device *dev,
1712 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001713{
1714 drm_i915_private_t *dev_priv = dev->dev_private;
1715 uint32_t seqno;
1716
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001717 if (!ring->status_page.page_addr
Zou Nan hai852835f2010-05-21 09:08:56 +08001718 || list_empty(&ring->request_list))
Karsten Wiese6c0594a2009-02-23 15:07:57 +01001719 return;
1720
Zou Nan hai852835f2010-05-21 09:08:56 +08001721 seqno = i915_get_gem_seqno(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07001722
Zou Nan hai852835f2010-05-21 09:08:56 +08001723 while (!list_empty(&ring->request_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001724 struct drm_i915_gem_request *request;
1725 uint32_t retiring_seqno;
1726
Zou Nan hai852835f2010-05-21 09:08:56 +08001727 request = list_first_entry(&ring->request_list,
Eric Anholt673a3942008-07-30 12:06:12 -07001728 struct drm_i915_gem_request,
1729 list);
1730 retiring_seqno = request->seqno;
1731
1732 if (i915_seqno_passed(seqno, retiring_seqno) ||
Ben Gamariba1234d2009-09-14 17:48:47 -04001733 atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001734 i915_gem_retire_request(dev, request);
1735
1736 list_del(&request->list);
Eric Anholtb9624422009-06-03 07:27:35 +00001737 list_del(&request->client_list);
Eric Anholt9a298b22009-03-24 12:23:04 -07001738 kfree(request);
Eric Anholt673a3942008-07-30 12:06:12 -07001739 } else
1740 break;
1741 }
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001742
1743 if (unlikely (dev_priv->trace_irq_seqno &&
1744 i915_seqno_passed(dev_priv->trace_irq_seqno, seqno))) {
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001745
1746 ring->user_irq_put(dev, ring);
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001747 dev_priv->trace_irq_seqno = 0;
1748 }
Eric Anholt673a3942008-07-30 12:06:12 -07001749}
1750
1751void
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001752i915_gem_retire_requests(struct drm_device *dev)
1753{
1754 drm_i915_private_t *dev_priv = dev->dev_private;
1755
Chris Wilsonbe726152010-07-23 23:18:50 +01001756 if (!list_empty(&dev_priv->mm.deferred_free_list)) {
1757 struct drm_i915_gem_object *obj_priv, *tmp;
1758
1759 /* We must be careful that during unbind() we do not
1760 * accidentally infinitely recurse into retire requests.
1761 * Currently:
1762 * retire -> free -> unbind -> wait -> retire_ring
1763 */
1764 list_for_each_entry_safe(obj_priv, tmp,
1765 &dev_priv->mm.deferred_free_list,
1766 list)
1767 i915_gem_free_object_tail(&obj_priv->base);
1768 }
1769
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001770 i915_gem_retire_requests_ring(dev, &dev_priv->render_ring);
1771 if (HAS_BSD(dev))
1772 i915_gem_retire_requests_ring(dev, &dev_priv->bsd_ring);
1773}
1774
1775void
Eric Anholt673a3942008-07-30 12:06:12 -07001776i915_gem_retire_work_handler(struct work_struct *work)
1777{
1778 drm_i915_private_t *dev_priv;
1779 struct drm_device *dev;
1780
1781 dev_priv = container_of(work, drm_i915_private_t,
1782 mm.retire_work.work);
1783 dev = dev_priv->dev;
1784
1785 mutex_lock(&dev->struct_mutex);
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001786 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08001787
Keith Packard6dbe2772008-10-14 21:41:13 -07001788 if (!dev_priv->mm.suspended &&
Zou Nan haid1b851f2010-05-21 09:08:57 +08001789 (!list_empty(&dev_priv->render_ring.request_list) ||
1790 (HAS_BSD(dev) &&
1791 !list_empty(&dev_priv->bsd_ring.request_list))))
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001792 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
Eric Anholt673a3942008-07-30 12:06:12 -07001793 mutex_unlock(&dev->struct_mutex);
1794}
1795
Daniel Vetter5a5a0c62009-09-15 22:57:36 +02001796int
Zou Nan hai852835f2010-05-21 09:08:56 +08001797i915_do_wait_request(struct drm_device *dev, uint32_t seqno,
1798 int interruptible, struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001799{
1800 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001801 u32 ier;
Eric Anholt673a3942008-07-30 12:06:12 -07001802 int ret = 0;
1803
1804 BUG_ON(seqno == 0);
1805
Ben Gamariba1234d2009-09-14 17:48:47 -04001806 if (atomic_read(&dev_priv->mm.wedged))
Ben Gamariffed1d02009-09-14 17:48:41 -04001807 return -EIO;
1808
Zou Nan hai852835f2010-05-21 09:08:56 +08001809 if (!i915_seqno_passed(ring->get_gem_seqno(dev, ring), seqno)) {
Eric Anholtbad720f2009-10-22 16:11:14 -07001810 if (HAS_PCH_SPLIT(dev))
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001811 ier = I915_READ(DEIER) | I915_READ(GTIER);
1812 else
1813 ier = I915_READ(IER);
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001814 if (!ier) {
1815 DRM_ERROR("something (likely vbetool) disabled "
1816 "interrupts, re-enabling\n");
1817 i915_driver_irq_preinstall(dev);
1818 i915_driver_irq_postinstall(dev);
1819 }
1820
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001821 trace_i915_gem_request_wait_begin(dev, seqno);
1822
Zou Nan hai852835f2010-05-21 09:08:56 +08001823 ring->waiting_gem_seqno = seqno;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001824 ring->user_irq_get(dev, ring);
Daniel Vetter48764bf2009-09-15 22:57:32 +02001825 if (interruptible)
Zou Nan hai852835f2010-05-21 09:08:56 +08001826 ret = wait_event_interruptible(ring->irq_queue,
1827 i915_seqno_passed(
1828 ring->get_gem_seqno(dev, ring), seqno)
1829 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02001830 else
Zou Nan hai852835f2010-05-21 09:08:56 +08001831 wait_event(ring->irq_queue,
1832 i915_seqno_passed(
1833 ring->get_gem_seqno(dev, ring), seqno)
1834 || atomic_read(&dev_priv->mm.wedged));
Daniel Vetter48764bf2009-09-15 22:57:32 +02001835
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001836 ring->user_irq_put(dev, ring);
Zou Nan hai852835f2010-05-21 09:08:56 +08001837 ring->waiting_gem_seqno = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01001838
1839 trace_i915_gem_request_wait_end(dev, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07001840 }
Ben Gamariba1234d2009-09-14 17:48:47 -04001841 if (atomic_read(&dev_priv->mm.wedged))
Eric Anholt673a3942008-07-30 12:06:12 -07001842 ret = -EIO;
1843
1844 if (ret && ret != -ERESTARTSYS)
1845 DRM_ERROR("%s returns %d (awaiting %d at %d)\n",
Zou Nan hai852835f2010-05-21 09:08:56 +08001846 __func__, ret, seqno, ring->get_gem_seqno(dev, ring));
Eric Anholt673a3942008-07-30 12:06:12 -07001847
1848 /* Directly dispatch request retiring. While we have the work queue
1849 * to handle this, the waiter on a request often wants an associated
1850 * buffer to have made it to the inactive list, and we would need
1851 * a separate wait queue to handle that.
1852 */
1853 if (ret == 0)
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01001854 i915_gem_retire_requests_ring(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07001855
1856 return ret;
1857}
1858
Daniel Vetter48764bf2009-09-15 22:57:32 +02001859/**
1860 * Waits for a sequence number to be signaled, and cleans up the
1861 * request and object lists appropriately for that event.
1862 */
1863static int
Zou Nan hai852835f2010-05-21 09:08:56 +08001864i915_wait_request(struct drm_device *dev, uint32_t seqno,
1865 struct intel_ring_buffer *ring)
Daniel Vetter48764bf2009-09-15 22:57:32 +02001866{
Zou Nan hai852835f2010-05-21 09:08:56 +08001867 return i915_do_wait_request(dev, seqno, 1, ring);
Daniel Vetter48764bf2009-09-15 22:57:32 +02001868}
1869
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001870static void
1871i915_gem_flush(struct drm_device *dev,
1872 uint32_t invalidate_domains,
1873 uint32_t flush_domains)
1874{
1875 drm_i915_private_t *dev_priv = dev->dev_private;
1876 if (flush_domains & I915_GEM_DOMAIN_CPU)
1877 drm_agp_chipset_flush(dev);
1878 dev_priv->render_ring.flush(dev, &dev_priv->render_ring,
1879 invalidate_domains,
1880 flush_domains);
Zou Nan haid1b851f2010-05-21 09:08:57 +08001881
1882 if (HAS_BSD(dev))
1883 dev_priv->bsd_ring.flush(dev, &dev_priv->bsd_ring,
1884 invalidate_domains,
1885 flush_domains);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001886}
1887
Zou Nan hai852835f2010-05-21 09:08:56 +08001888static void
1889i915_gem_flush_ring(struct drm_device *dev,
1890 uint32_t invalidate_domains,
1891 uint32_t flush_domains,
1892 struct intel_ring_buffer *ring)
1893{
1894 if (flush_domains & I915_GEM_DOMAIN_CPU)
1895 drm_agp_chipset_flush(dev);
1896 ring->flush(dev, ring,
1897 invalidate_domains,
1898 flush_domains);
1899}
1900
Eric Anholt673a3942008-07-30 12:06:12 -07001901/**
1902 * Ensures that all rendering to the object has completed and the object is
1903 * safe to unbind from the GTT or access from the CPU.
1904 */
1905static int
1906i915_gem_object_wait_rendering(struct drm_gem_object *obj)
1907{
1908 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01001909 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001910 int ret;
1911
Eric Anholte47c68e2008-11-14 13:35:19 -08001912 /* This function only exists to support waiting for existing rendering,
1913 * not for emitting required flushes.
Eric Anholt673a3942008-07-30 12:06:12 -07001914 */
Eric Anholte47c68e2008-11-14 13:35:19 -08001915 BUG_ON((obj->write_domain & I915_GEM_GPU_DOMAINS) != 0);
Eric Anholt673a3942008-07-30 12:06:12 -07001916
1917 /* If there is rendering queued on the buffer being evicted, wait for
1918 * it.
1919 */
1920 if (obj_priv->active) {
1921#if WATCH_BUF
1922 DRM_INFO("%s: object %p wait for seqno %08x\n",
1923 __func__, obj, obj_priv->last_rendering_seqno);
1924#endif
Zou Nan hai852835f2010-05-21 09:08:56 +08001925 ret = i915_wait_request(dev,
1926 obj_priv->last_rendering_seqno, obj_priv->ring);
Eric Anholt673a3942008-07-30 12:06:12 -07001927 if (ret != 0)
1928 return ret;
1929 }
1930
1931 return 0;
1932}
1933
1934/**
1935 * Unbinds an object from the GTT aperture.
1936 */
Jesse Barnes0f973f22009-01-26 17:10:45 -08001937int
Eric Anholt673a3942008-07-30 12:06:12 -07001938i915_gem_object_unbind(struct drm_gem_object *obj)
1939{
1940 struct drm_device *dev = obj->dev;
Daniel Vetter4a87b8c2010-02-19 11:51:57 +01001941 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01001942 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001943 int ret = 0;
1944
1945#if WATCH_BUF
1946 DRM_INFO("%s:%d %p\n", __func__, __LINE__, obj);
1947 DRM_INFO("gtt_space %p\n", obj_priv->gtt_space);
1948#endif
1949 if (obj_priv->gtt_space == NULL)
1950 return 0;
1951
1952 if (obj_priv->pin_count != 0) {
1953 DRM_ERROR("Attempting to unbind pinned buffer\n");
1954 return -EINVAL;
1955 }
1956
Eric Anholt5323fd02009-09-09 11:50:45 -07001957 /* blow away mappings if mapped through GTT */
1958 i915_gem_release_mmap(obj);
1959
Eric Anholt673a3942008-07-30 12:06:12 -07001960 /* Move the object to the CPU domain to ensure that
1961 * any possible CPU writes while it's not in the GTT
1962 * are flushed when we go to remap it. This will
1963 * also ensure that all pending GPU writes are finished
1964 * before we unbind.
1965 */
Eric Anholte47c68e2008-11-14 13:35:19 -08001966 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Chris Wilson8dc17752010-07-23 23:18:51 +01001967 if (ret == -ERESTARTSYS)
Eric Anholt673a3942008-07-30 12:06:12 -07001968 return ret;
Chris Wilson8dc17752010-07-23 23:18:51 +01001969 /* Continue on if we fail due to EIO, the GPU is hung so we
1970 * should be safe and we need to cleanup or else we might
1971 * cause memory corruption through use-after-free.
1972 */
Eric Anholt673a3942008-07-30 12:06:12 -07001973
Eric Anholt5323fd02009-09-09 11:50:45 -07001974 BUG_ON(obj_priv->active);
1975
Daniel Vetter96b47b62009-12-15 17:50:00 +01001976 /* release the fence reg _after_ flushing */
1977 if (obj_priv->fence_reg != I915_FENCE_REG_NONE)
1978 i915_gem_clear_fence_reg(obj);
1979
Eric Anholt673a3942008-07-30 12:06:12 -07001980 if (obj_priv->agp_mem != NULL) {
1981 drm_unbind_agp(obj_priv->agp_mem);
1982 drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE);
1983 obj_priv->agp_mem = NULL;
1984 }
1985
Eric Anholt856fa192009-03-19 14:10:50 -07001986 i915_gem_object_put_pages(obj);
Chris Wilsona32808c2009-09-20 21:29:47 +01001987 BUG_ON(obj_priv->pages_refcount);
Eric Anholt673a3942008-07-30 12:06:12 -07001988
1989 if (obj_priv->gtt_space) {
1990 atomic_dec(&dev->gtt_count);
1991 atomic_sub(obj->size, &dev->gtt_memory);
1992
1993 drm_mm_put_block(obj_priv->gtt_space);
1994 obj_priv->gtt_space = NULL;
1995 }
1996
1997 /* Remove ourselves from the LRU list if present. */
Daniel Vetter4a87b8c2010-02-19 11:51:57 +01001998 spin_lock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001999 if (!list_empty(&obj_priv->list))
2000 list_del_init(&obj_priv->list);
Daniel Vetter4a87b8c2010-02-19 11:51:57 +01002001 spin_unlock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07002002
Chris Wilson963b4832009-09-20 23:03:54 +01002003 if (i915_gem_object_is_purgeable(obj_priv))
2004 i915_gem_object_truncate(obj);
2005
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002006 trace_i915_gem_object_unbind(obj);
2007
Chris Wilson8dc17752010-07-23 23:18:51 +01002008 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002009}
2010
Daniel Vetter0108a3e2010-08-07 11:01:21 +01002011static int
2012i915_gem_scan_inactive_list_and_evict(struct drm_device *dev, int min_size,
2013 unsigned alignment, int *found)
Chris Wilson07f73f62009-09-14 16:50:30 +01002014{
2015 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter0108a3e2010-08-07 11:01:21 +01002016 struct drm_gem_object *obj;
Chris Wilson07f73f62009-09-14 16:50:30 +01002017 struct drm_i915_gem_object *obj_priv;
2018 struct drm_gem_object *best = NULL;
2019 struct drm_gem_object *first = NULL;
2020
2021 /* Try to find the smallest clean object */
2022 list_for_each_entry(obj_priv, &dev_priv->mm.inactive_list, list) {
Daniel Vettera8089e82010-04-09 19:05:09 +00002023 struct drm_gem_object *obj = &obj_priv->base;
Chris Wilson07f73f62009-09-14 16:50:30 +01002024 if (obj->size >= min_size) {
Chris Wilson963b4832009-09-20 23:03:54 +01002025 if ((!obj_priv->dirty ||
2026 i915_gem_object_is_purgeable(obj_priv)) &&
Chris Wilson07f73f62009-09-14 16:50:30 +01002027 (!best || obj->size < best->size)) {
2028 best = obj;
2029 if (best->size == min_size)
Daniel Vetter0108a3e2010-08-07 11:01:21 +01002030 break;
Chris Wilson07f73f62009-09-14 16:50:30 +01002031 }
2032 if (!first)
2033 first = obj;
2034 }
2035 }
2036
Daniel Vetter0108a3e2010-08-07 11:01:21 +01002037 obj = best ? best : first;
2038
2039 if (!obj) {
2040 *found = 0;
2041 return 0;
2042 }
2043
2044 *found = 1;
2045
2046#if WATCH_LRU
2047 DRM_INFO("%s: evicting %p\n", __func__, obj);
2048#endif
2049 obj_priv = to_intel_bo(obj);
2050 BUG_ON(obj_priv->pin_count != 0);
2051 BUG_ON(obj_priv->active);
2052
2053 /* Wait on the rendering and unbind the buffer. */
2054 return i915_gem_object_unbind(obj);
Chris Wilson07f73f62009-09-14 16:50:30 +01002055}
2056
Eric Anholt673a3942008-07-30 12:06:12 -07002057static int
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002058i915_gpu_idle(struct drm_device *dev)
2059{
2060 drm_i915_private_t *dev_priv = dev->dev_private;
2061 bool lists_empty;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002062 uint32_t seqno1, seqno2;
Zou Nan hai852835f2010-05-21 09:08:56 +08002063 int ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002064
2065 spin_lock(&dev_priv->mm.active_list_lock);
Zou Nan haid1b851f2010-05-21 09:08:57 +08002066 lists_empty = (list_empty(&dev_priv->mm.flushing_list) &&
2067 list_empty(&dev_priv->render_ring.active_list) &&
2068 (!HAS_BSD(dev) ||
2069 list_empty(&dev_priv->bsd_ring.active_list)));
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002070 spin_unlock(&dev_priv->mm.active_list_lock);
2071
2072 if (lists_empty)
2073 return 0;
2074
2075 /* Flush everything onto the inactive list. */
2076 i915_gem_flush(dev, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
Zou Nan haid1b851f2010-05-21 09:08:57 +08002077 seqno1 = i915_add_request(dev, NULL, I915_GEM_GPU_DOMAINS,
Zou Nan hai852835f2010-05-21 09:08:56 +08002078 &dev_priv->render_ring);
Zou Nan haid1b851f2010-05-21 09:08:57 +08002079 if (seqno1 == 0)
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002080 return -ENOMEM;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002081 ret = i915_wait_request(dev, seqno1, &dev_priv->render_ring);
2082
2083 if (HAS_BSD(dev)) {
2084 seqno2 = i915_add_request(dev, NULL, I915_GEM_GPU_DOMAINS,
2085 &dev_priv->bsd_ring);
2086 if (seqno2 == 0)
2087 return -ENOMEM;
2088
2089 ret = i915_wait_request(dev, seqno2, &dev_priv->bsd_ring);
2090 if (ret)
2091 return ret;
2092 }
2093
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002094
Zou Nan hai852835f2010-05-21 09:08:56 +08002095 return ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002096}
2097
2098static int
Chris Wilson07f73f62009-09-14 16:50:30 +01002099i915_gem_evict_everything(struct drm_device *dev)
2100{
2101 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson07f73f62009-09-14 16:50:30 +01002102 int ret;
2103 bool lists_empty;
2104
Chris Wilson07f73f62009-09-14 16:50:30 +01002105 spin_lock(&dev_priv->mm.active_list_lock);
2106 lists_empty = (list_empty(&dev_priv->mm.inactive_list) &&
2107 list_empty(&dev_priv->mm.flushing_list) &&
Zou Nan haid1b851f2010-05-21 09:08:57 +08002108 list_empty(&dev_priv->render_ring.active_list) &&
2109 (!HAS_BSD(dev)
2110 || list_empty(&dev_priv->bsd_ring.active_list)));
Chris Wilson07f73f62009-09-14 16:50:30 +01002111 spin_unlock(&dev_priv->mm.active_list_lock);
2112
Chris Wilson97311292009-09-21 00:22:34 +01002113 if (lists_empty)
Chris Wilson07f73f62009-09-14 16:50:30 +01002114 return -ENOSPC;
Chris Wilson07f73f62009-09-14 16:50:30 +01002115
2116 /* Flush everything (on to the inactive lists) and evict */
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002117 ret = i915_gpu_idle(dev);
Chris Wilson07f73f62009-09-14 16:50:30 +01002118 if (ret)
2119 return ret;
2120
Daniel Vetter99fcb762010-02-07 16:20:18 +01002121 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
2122
Chris Wilsonab5ee572009-09-20 19:25:47 +01002123 ret = i915_gem_evict_from_inactive_list(dev);
Chris Wilson07f73f62009-09-14 16:50:30 +01002124 if (ret)
2125 return ret;
2126
2127 spin_lock(&dev_priv->mm.active_list_lock);
2128 lists_empty = (list_empty(&dev_priv->mm.inactive_list) &&
2129 list_empty(&dev_priv->mm.flushing_list) &&
Zou Nan haid1b851f2010-05-21 09:08:57 +08002130 list_empty(&dev_priv->render_ring.active_list) &&
2131 (!HAS_BSD(dev)
2132 || list_empty(&dev_priv->bsd_ring.active_list)));
Chris Wilson07f73f62009-09-14 16:50:30 +01002133 spin_unlock(&dev_priv->mm.active_list_lock);
2134 BUG_ON(!lists_empty);
2135
Eric Anholt673a3942008-07-30 12:06:12 -07002136 return 0;
2137}
2138
2139static int
Daniel Vetter0108a3e2010-08-07 11:01:21 +01002140i915_gem_evict_something(struct drm_device *dev,
2141 int min_size, unsigned alignment)
Eric Anholt673a3942008-07-30 12:06:12 -07002142{
2143 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter0108a3e2010-08-07 11:01:21 +01002144 int ret, found;
Eric Anholt673a3942008-07-30 12:06:12 -07002145
Zou Nan hai852835f2010-05-21 09:08:56 +08002146 struct intel_ring_buffer *render_ring = &dev_priv->render_ring;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002147 struct intel_ring_buffer *bsd_ring = &dev_priv->bsd_ring;
Eric Anholt673a3942008-07-30 12:06:12 -07002148 for (;;) {
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002149 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08002150
Eric Anholt673a3942008-07-30 12:06:12 -07002151 /* If there's an inactive buffer available now, grab it
2152 * and be done.
2153 */
Daniel Vetter0108a3e2010-08-07 11:01:21 +01002154 ret = i915_gem_scan_inactive_list_and_evict(dev, min_size,
2155 alignment,
2156 &found);
2157 if (found)
2158 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002159
2160 /* If we didn't get anything, but the ring is still processing
Chris Wilson07f73f62009-09-14 16:50:30 +01002161 * things, wait for the next to finish and hopefully leave us
2162 * a buffer to evict.
Eric Anholt673a3942008-07-30 12:06:12 -07002163 */
Zou Nan hai852835f2010-05-21 09:08:56 +08002164 if (!list_empty(&render_ring->request_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002165 struct drm_i915_gem_request *request;
2166
Zou Nan hai852835f2010-05-21 09:08:56 +08002167 request = list_first_entry(&render_ring->request_list,
Eric Anholt673a3942008-07-30 12:06:12 -07002168 struct drm_i915_gem_request,
2169 list);
2170
Zou Nan hai852835f2010-05-21 09:08:56 +08002171 ret = i915_wait_request(dev,
2172 request->seqno, request->ring);
Eric Anholt673a3942008-07-30 12:06:12 -07002173 if (ret)
Chris Wilson07f73f62009-09-14 16:50:30 +01002174 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002175
Chris Wilson07f73f62009-09-14 16:50:30 +01002176 continue;
Eric Anholt673a3942008-07-30 12:06:12 -07002177 }
2178
Zou Nan haid1b851f2010-05-21 09:08:57 +08002179 if (HAS_BSD(dev) && !list_empty(&bsd_ring->request_list)) {
2180 struct drm_i915_gem_request *request;
2181
2182 request = list_first_entry(&bsd_ring->request_list,
2183 struct drm_i915_gem_request,
2184 list);
2185
2186 ret = i915_wait_request(dev,
2187 request->seqno, request->ring);
2188 if (ret)
2189 return ret;
2190
2191 continue;
2192 }
2193
Eric Anholt673a3942008-07-30 12:06:12 -07002194 /* If we didn't have anything on the request list but there
2195 * are buffers awaiting a flush, emit one and try again.
2196 * When we wait on it, those buffers waiting for that flush
2197 * will get moved to inactive.
2198 */
2199 if (!list_empty(&dev_priv->mm.flushing_list)) {
Daniel Vetter0108a3e2010-08-07 11:01:21 +01002200 struct drm_gem_object *obj = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002201 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07002202
Chris Wilson9a1e2582009-09-20 20:16:50 +01002203 /* Find an object that we can immediately reuse */
2204 list_for_each_entry(obj_priv, &dev_priv->mm.flushing_list, list) {
Daniel Vettera8089e82010-04-09 19:05:09 +00002205 obj = &obj_priv->base;
Chris Wilson9a1e2582009-09-20 20:16:50 +01002206 if (obj->size >= min_size)
2207 break;
Eric Anholt673a3942008-07-30 12:06:12 -07002208
Chris Wilson9a1e2582009-09-20 20:16:50 +01002209 obj = NULL;
2210 }
Eric Anholt673a3942008-07-30 12:06:12 -07002211
Chris Wilson9a1e2582009-09-20 20:16:50 +01002212 if (obj != NULL) {
2213 uint32_t seqno;
Chris Wilson07f73f62009-09-14 16:50:30 +01002214
Zou Nan hai852835f2010-05-21 09:08:56 +08002215 i915_gem_flush_ring(dev,
Chris Wilson9a1e2582009-09-20 20:16:50 +01002216 obj->write_domain,
Zou Nan hai852835f2010-05-21 09:08:56 +08002217 obj->write_domain,
2218 obj_priv->ring);
2219 seqno = i915_add_request(dev, NULL,
2220 obj->write_domain,
2221 obj_priv->ring);
Chris Wilson9a1e2582009-09-20 20:16:50 +01002222 if (seqno == 0)
2223 return -ENOMEM;
Chris Wilson9a1e2582009-09-20 20:16:50 +01002224 continue;
2225 }
Eric Anholt673a3942008-07-30 12:06:12 -07002226 }
2227
Chris Wilson07f73f62009-09-14 16:50:30 +01002228 /* If we didn't do any of the above, there's no single buffer
2229 * large enough to swap out for the new one, so just evict
2230 * everything and start again. (This should be rare.)
Eric Anholt673a3942008-07-30 12:06:12 -07002231 */
Chris Wilson97311292009-09-21 00:22:34 +01002232 if (!list_empty (&dev_priv->mm.inactive_list))
Chris Wilsonab5ee572009-09-20 19:25:47 +01002233 return i915_gem_evict_from_inactive_list(dev);
Chris Wilson97311292009-09-21 00:22:34 +01002234 else
Chris Wilson07f73f62009-09-14 16:50:30 +01002235 return i915_gem_evict_everything(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07002236 }
Keith Packardac94a962008-11-20 23:30:27 -08002237}
2238
Ben Gamari6911a9b2009-04-02 11:24:54 -07002239int
Chris Wilson4bdadb92010-01-27 13:36:32 +00002240i915_gem_object_get_pages(struct drm_gem_object *obj,
2241 gfp_t gfpmask)
Eric Anholt673a3942008-07-30 12:06:12 -07002242{
Daniel Vetter23010e42010-03-08 13:35:02 +01002243 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002244 int page_count, i;
2245 struct address_space *mapping;
2246 struct inode *inode;
2247 struct page *page;
Eric Anholt673a3942008-07-30 12:06:12 -07002248
Daniel Vetter778c3542010-05-13 11:49:44 +02002249 BUG_ON(obj_priv->pages_refcount
2250 == DRM_I915_GEM_OBJECT_MAX_PAGES_REFCOUNT);
2251
Eric Anholt856fa192009-03-19 14:10:50 -07002252 if (obj_priv->pages_refcount++ != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07002253 return 0;
2254
2255 /* Get the list of pages out of our struct file. They'll be pinned
2256 * at this point until we release them.
2257 */
2258 page_count = obj->size / PAGE_SIZE;
Eric Anholt856fa192009-03-19 14:10:50 -07002259 BUG_ON(obj_priv->pages != NULL);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07002260 obj_priv->pages = drm_calloc_large(page_count, sizeof(struct page *));
Eric Anholt856fa192009-03-19 14:10:50 -07002261 if (obj_priv->pages == NULL) {
Eric Anholt856fa192009-03-19 14:10:50 -07002262 obj_priv->pages_refcount--;
Eric Anholt673a3942008-07-30 12:06:12 -07002263 return -ENOMEM;
2264 }
2265
2266 inode = obj->filp->f_path.dentry->d_inode;
2267 mapping = inode->i_mapping;
2268 for (i = 0; i < page_count; i++) {
Chris Wilson4bdadb92010-01-27 13:36:32 +00002269 page = read_cache_page_gfp(mapping, i,
Linus Torvalds985b8232010-07-02 10:04:42 +10002270 GFP_HIGHUSER |
Chris Wilson4bdadb92010-01-27 13:36:32 +00002271 __GFP_COLD |
Linus Torvaldscd9f0402010-07-18 09:44:37 -07002272 __GFP_RECLAIMABLE |
Chris Wilson4bdadb92010-01-27 13:36:32 +00002273 gfpmask);
Chris Wilson1f2b1012010-03-12 19:52:55 +00002274 if (IS_ERR(page))
2275 goto err_pages;
2276
Eric Anholt856fa192009-03-19 14:10:50 -07002277 obj_priv->pages[i] = page;
Eric Anholt673a3942008-07-30 12:06:12 -07002278 }
Eric Anholt280b7132009-03-12 16:56:27 -07002279
2280 if (obj_priv->tiling_mode != I915_TILING_NONE)
2281 i915_gem_object_do_bit_17_swizzle(obj);
2282
Eric Anholt673a3942008-07-30 12:06:12 -07002283 return 0;
Chris Wilson1f2b1012010-03-12 19:52:55 +00002284
2285err_pages:
2286 while (i--)
2287 page_cache_release(obj_priv->pages[i]);
2288
2289 drm_free_large(obj_priv->pages);
2290 obj_priv->pages = NULL;
2291 obj_priv->pages_refcount--;
2292 return PTR_ERR(page);
Eric Anholt673a3942008-07-30 12:06:12 -07002293}
2294
Eric Anholt4e901fd2009-10-26 16:44:17 -07002295static void sandybridge_write_fence_reg(struct drm_i915_fence_reg *reg)
2296{
2297 struct drm_gem_object *obj = reg->obj;
2298 struct drm_device *dev = obj->dev;
2299 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002300 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt4e901fd2009-10-26 16:44:17 -07002301 int regnum = obj_priv->fence_reg;
2302 uint64_t val;
2303
2304 val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
2305 0xfffff000) << 32;
2306 val |= obj_priv->gtt_offset & 0xfffff000;
2307 val |= (uint64_t)((obj_priv->stride / 128) - 1) <<
2308 SANDYBRIDGE_FENCE_PITCH_SHIFT;
2309
2310 if (obj_priv->tiling_mode == I915_TILING_Y)
2311 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2312 val |= I965_FENCE_REG_VALID;
2313
2314 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (regnum * 8), val);
2315}
2316
Jesse Barnesde151cf2008-11-12 10:03:55 -08002317static void i965_write_fence_reg(struct drm_i915_fence_reg *reg)
2318{
2319 struct drm_gem_object *obj = reg->obj;
2320 struct drm_device *dev = obj->dev;
2321 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002322 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002323 int regnum = obj_priv->fence_reg;
2324 uint64_t val;
2325
2326 val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
2327 0xfffff000) << 32;
2328 val |= obj_priv->gtt_offset & 0xfffff000;
2329 val |= ((obj_priv->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
2330 if (obj_priv->tiling_mode == I915_TILING_Y)
2331 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2332 val |= I965_FENCE_REG_VALID;
2333
2334 I915_WRITE64(FENCE_REG_965_0 + (regnum * 8), val);
2335}
2336
2337static void i915_write_fence_reg(struct drm_i915_fence_reg *reg)
2338{
2339 struct drm_gem_object *obj = reg->obj;
2340 struct drm_device *dev = obj->dev;
2341 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002342 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002343 int regnum = obj_priv->fence_reg;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002344 int tile_width;
Eric Anholtdc529a42009-03-10 22:34:49 -07002345 uint32_t fence_reg, val;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002346 uint32_t pitch_val;
2347
2348 if ((obj_priv->gtt_offset & ~I915_FENCE_START_MASK) ||
2349 (obj_priv->gtt_offset & (obj->size - 1))) {
Linus Torvaldsf06da262009-02-09 08:57:29 -08002350 WARN(1, "%s: object 0x%08x not 1M or size (0x%zx) aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002351 __func__, obj_priv->gtt_offset, obj->size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002352 return;
2353 }
2354
Jesse Barnes0f973f22009-01-26 17:10:45 -08002355 if (obj_priv->tiling_mode == I915_TILING_Y &&
2356 HAS_128_BYTE_Y_TILING(dev))
2357 tile_width = 128;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002358 else
Jesse Barnes0f973f22009-01-26 17:10:45 -08002359 tile_width = 512;
2360
2361 /* Note: pitch better be a power of two tile widths */
2362 pitch_val = obj_priv->stride / tile_width;
2363 pitch_val = ffs(pitch_val) - 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002364
Daniel Vetterc36a2a62010-04-17 15:12:03 +02002365 if (obj_priv->tiling_mode == I915_TILING_Y &&
2366 HAS_128_BYTE_Y_TILING(dev))
2367 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2368 else
2369 WARN_ON(pitch_val > I915_FENCE_MAX_PITCH_VAL);
2370
Jesse Barnesde151cf2008-11-12 10:03:55 -08002371 val = obj_priv->gtt_offset;
2372 if (obj_priv->tiling_mode == I915_TILING_Y)
2373 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2374 val |= I915_FENCE_SIZE_BITS(obj->size);
2375 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2376 val |= I830_FENCE_REG_VALID;
2377
Eric Anholtdc529a42009-03-10 22:34:49 -07002378 if (regnum < 8)
2379 fence_reg = FENCE_REG_830_0 + (regnum * 4);
2380 else
2381 fence_reg = FENCE_REG_945_8 + ((regnum - 8) * 4);
2382 I915_WRITE(fence_reg, val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002383}
2384
2385static void i830_write_fence_reg(struct drm_i915_fence_reg *reg)
2386{
2387 struct drm_gem_object *obj = reg->obj;
2388 struct drm_device *dev = obj->dev;
2389 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002390 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002391 int regnum = obj_priv->fence_reg;
2392 uint32_t val;
2393 uint32_t pitch_val;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002394 uint32_t fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002395
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002396 if ((obj_priv->gtt_offset & ~I830_FENCE_START_MASK) ||
Jesse Barnesde151cf2008-11-12 10:03:55 -08002397 (obj_priv->gtt_offset & (obj->size - 1))) {
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002398 WARN(1, "%s: object 0x%08x not 512K or size aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002399 __func__, obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002400 return;
2401 }
2402
Eric Anholte76a16d2009-05-26 17:44:56 -07002403 pitch_val = obj_priv->stride / 128;
2404 pitch_val = ffs(pitch_val) - 1;
2405 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2406
Jesse Barnesde151cf2008-11-12 10:03:55 -08002407 val = obj_priv->gtt_offset;
2408 if (obj_priv->tiling_mode == I915_TILING_Y)
2409 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002410 fence_size_bits = I830_FENCE_SIZE_BITS(obj->size);
2411 WARN_ON(fence_size_bits & ~0x00000f00);
2412 val |= fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002413 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2414 val |= I830_FENCE_REG_VALID;
2415
2416 I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002417}
2418
Daniel Vetterae3db242010-02-19 11:51:58 +01002419static int i915_find_fence_reg(struct drm_device *dev)
2420{
2421 struct drm_i915_fence_reg *reg = NULL;
2422 struct drm_i915_gem_object *obj_priv = NULL;
2423 struct drm_i915_private *dev_priv = dev->dev_private;
2424 struct drm_gem_object *obj = NULL;
2425 int i, avail, ret;
2426
2427 /* First try to find a free reg */
2428 avail = 0;
2429 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2430 reg = &dev_priv->fence_regs[i];
2431 if (!reg->obj)
2432 return i;
2433
Daniel Vetter23010e42010-03-08 13:35:02 +01002434 obj_priv = to_intel_bo(reg->obj);
Daniel Vetterae3db242010-02-19 11:51:58 +01002435 if (!obj_priv->pin_count)
2436 avail++;
2437 }
2438
2439 if (avail == 0)
2440 return -ENOSPC;
2441
2442 /* None available, try to steal one or wait for a user to finish */
2443 i = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002444 list_for_each_entry(reg, &dev_priv->mm.fence_list,
2445 lru_list) {
2446 obj = reg->obj;
2447 obj_priv = to_intel_bo(obj);
Daniel Vetterae3db242010-02-19 11:51:58 +01002448
2449 if (obj_priv->pin_count)
2450 continue;
2451
2452 /* found one! */
2453 i = obj_priv->fence_reg;
2454 break;
2455 }
2456
2457 BUG_ON(i == I915_FENCE_REG_NONE);
2458
2459 /* We only have a reference on obj from the active list. put_fence_reg
2460 * might drop that one, causing a use-after-free in it. So hold a
2461 * private reference to obj like the other callers of put_fence_reg
2462 * (set_tiling ioctl) do. */
2463 drm_gem_object_reference(obj);
2464 ret = i915_gem_object_put_fence_reg(obj);
2465 drm_gem_object_unreference(obj);
2466 if (ret != 0)
2467 return ret;
2468
2469 return i;
2470}
2471
Jesse Barnesde151cf2008-11-12 10:03:55 -08002472/**
2473 * i915_gem_object_get_fence_reg - set up a fence reg for an object
2474 * @obj: object to map through a fence reg
2475 *
2476 * When mapping objects through the GTT, userspace wants to be able to write
2477 * to them without having to worry about swizzling if the object is tiled.
2478 *
2479 * This function walks the fence regs looking for a free one for @obj,
2480 * stealing one if it can't find any.
2481 *
2482 * It then sets up the reg based on the object's properties: address, pitch
2483 * and tiling format.
2484 */
Chris Wilson8c4b8c32009-06-17 22:08:52 +01002485int
2486i915_gem_object_get_fence_reg(struct drm_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002487{
2488 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002489 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002490 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002491 struct drm_i915_fence_reg *reg = NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002492 int ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002493
Eric Anholta09ba7f2009-08-29 12:49:51 -07002494 /* Just update our place in the LRU if our fence is getting used. */
2495 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002496 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
2497 list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002498 return 0;
2499 }
2500
Jesse Barnesde151cf2008-11-12 10:03:55 -08002501 switch (obj_priv->tiling_mode) {
2502 case I915_TILING_NONE:
2503 WARN(1, "allocating a fence for non-tiled object?\n");
2504 break;
2505 case I915_TILING_X:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002506 if (!obj_priv->stride)
2507 return -EINVAL;
2508 WARN((obj_priv->stride & (512 - 1)),
2509 "object 0x%08x is X tiled but has non-512B pitch\n",
2510 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002511 break;
2512 case I915_TILING_Y:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002513 if (!obj_priv->stride)
2514 return -EINVAL;
2515 WARN((obj_priv->stride & (128 - 1)),
2516 "object 0x%08x is Y tiled but has non-128B pitch\n",
2517 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002518 break;
2519 }
2520
Daniel Vetterae3db242010-02-19 11:51:58 +01002521 ret = i915_find_fence_reg(dev);
2522 if (ret < 0)
2523 return ret;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002524
Daniel Vetterae3db242010-02-19 11:51:58 +01002525 obj_priv->fence_reg = ret;
2526 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002527 list_add_tail(&reg->lru_list, &dev_priv->mm.fence_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07002528
Jesse Barnesde151cf2008-11-12 10:03:55 -08002529 reg->obj = obj;
2530
Eric Anholt4e901fd2009-10-26 16:44:17 -07002531 if (IS_GEN6(dev))
2532 sandybridge_write_fence_reg(reg);
2533 else if (IS_I965G(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08002534 i965_write_fence_reg(reg);
2535 else if (IS_I9XX(dev))
2536 i915_write_fence_reg(reg);
2537 else
2538 i830_write_fence_reg(reg);
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002539
Daniel Vetterae3db242010-02-19 11:51:58 +01002540 trace_i915_gem_object_get_fence(obj, obj_priv->fence_reg,
2541 obj_priv->tiling_mode);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002542
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002543 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002544}
2545
2546/**
2547 * i915_gem_clear_fence_reg - clear out fence register info
2548 * @obj: object to clear
2549 *
2550 * Zeroes out the fence register itself and clears out the associated
2551 * data structures in dev_priv and obj_priv.
2552 */
2553static void
2554i915_gem_clear_fence_reg(struct drm_gem_object *obj)
2555{
2556 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002557 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002558 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002559 struct drm_i915_fence_reg *reg =
2560 &dev_priv->fence_regs[obj_priv->fence_reg];
Jesse Barnesde151cf2008-11-12 10:03:55 -08002561
Eric Anholt4e901fd2009-10-26 16:44:17 -07002562 if (IS_GEN6(dev)) {
2563 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 +
2564 (obj_priv->fence_reg * 8), 0);
2565 } else if (IS_I965G(dev)) {
Jesse Barnesde151cf2008-11-12 10:03:55 -08002566 I915_WRITE64(FENCE_REG_965_0 + (obj_priv->fence_reg * 8), 0);
Eric Anholt4e901fd2009-10-26 16:44:17 -07002567 } else {
Eric Anholtdc529a42009-03-10 22:34:49 -07002568 uint32_t fence_reg;
2569
2570 if (obj_priv->fence_reg < 8)
2571 fence_reg = FENCE_REG_830_0 + obj_priv->fence_reg * 4;
2572 else
2573 fence_reg = FENCE_REG_945_8 + (obj_priv->fence_reg -
2574 8) * 4;
2575
2576 I915_WRITE(fence_reg, 0);
2577 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002578
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002579 reg->obj = NULL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002580 obj_priv->fence_reg = I915_FENCE_REG_NONE;
Daniel Vetter007cc8a2010-04-28 11:02:31 +02002581 list_del_init(&reg->lru_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002582}
2583
Eric Anholt673a3942008-07-30 12:06:12 -07002584/**
Chris Wilson52dc7d32009-06-06 09:46:01 +01002585 * i915_gem_object_put_fence_reg - waits on outstanding fenced access
2586 * to the buffer to finish, and then resets the fence register.
2587 * @obj: tiled object holding a fence register.
2588 *
2589 * Zeroes out the fence register itself and clears out the associated
2590 * data structures in dev_priv and obj_priv.
2591 */
2592int
2593i915_gem_object_put_fence_reg(struct drm_gem_object *obj)
2594{
2595 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01002596 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson52dc7d32009-06-06 09:46:01 +01002597
2598 if (obj_priv->fence_reg == I915_FENCE_REG_NONE)
2599 return 0;
2600
Daniel Vetter10ae9bd2010-02-01 13:59:17 +01002601 /* If we've changed tiling, GTT-mappings of the object
2602 * need to re-fault to ensure that the correct fence register
2603 * setup is in place.
2604 */
2605 i915_gem_release_mmap(obj);
2606
Chris Wilson52dc7d32009-06-06 09:46:01 +01002607 /* On the i915, GPU access to tiled buffers is via a fence,
2608 * therefore we must wait for any outstanding access to complete
2609 * before clearing the fence.
2610 */
2611 if (!IS_I965G(dev)) {
2612 int ret;
2613
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002614 ret = i915_gem_object_flush_gpu_write_domain(obj);
2615 if (ret != 0)
2616 return ret;
2617
Chris Wilson52dc7d32009-06-06 09:46:01 +01002618 ret = i915_gem_object_wait_rendering(obj);
2619 if (ret != 0)
2620 return ret;
2621 }
2622
Daniel Vetter4a726612010-02-01 13:59:16 +01002623 i915_gem_object_flush_gtt_write_domain(obj);
Chris Wilson52dc7d32009-06-06 09:46:01 +01002624 i915_gem_clear_fence_reg (obj);
2625
2626 return 0;
2627}
2628
2629/**
Eric Anholt673a3942008-07-30 12:06:12 -07002630 * Finds free space in the GTT aperture and binds the object there.
2631 */
2632static int
2633i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment)
2634{
2635 struct drm_device *dev = obj->dev;
2636 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01002637 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002638 struct drm_mm_node *free_space;
Chris Wilson4bdadb92010-01-27 13:36:32 +00002639 gfp_t gfpmask = __GFP_NORETRY | __GFP_NOWARN;
Chris Wilson07f73f62009-09-14 16:50:30 +01002640 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002641
Chris Wilsonbb6baf72009-09-22 14:24:13 +01002642 if (obj_priv->madv != I915_MADV_WILLNEED) {
Chris Wilson3ef94da2009-09-14 16:50:29 +01002643 DRM_ERROR("Attempting to bind a purgeable object\n");
2644 return -EINVAL;
2645 }
2646
Eric Anholt673a3942008-07-30 12:06:12 -07002647 if (alignment == 0)
Jesse Barnes0f973f22009-01-26 17:10:45 -08002648 alignment = i915_gem_get_gtt_alignment(obj);
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002649 if (alignment & (i915_gem_get_gtt_alignment(obj) - 1)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002650 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2651 return -EINVAL;
2652 }
2653
Chris Wilson654fc602010-05-27 13:18:21 +01002654 /* If the object is bigger than the entire aperture, reject it early
2655 * before evicting everything in a vain attempt to find space.
2656 */
2657 if (obj->size > dev->gtt_total) {
2658 DRM_ERROR("Attempting to bind an object larger than the aperture\n");
2659 return -E2BIG;
2660 }
2661
Eric Anholt673a3942008-07-30 12:06:12 -07002662 search_free:
2663 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
2664 obj->size, alignment, 0);
2665 if (free_space != NULL) {
2666 obj_priv->gtt_space = drm_mm_get_block(free_space, obj->size,
2667 alignment);
Daniel Vetterdb3307a2010-07-02 15:02:12 +01002668 if (obj_priv->gtt_space != NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002669 obj_priv->gtt_offset = obj_priv->gtt_space->start;
Eric Anholt673a3942008-07-30 12:06:12 -07002670 }
2671 if (obj_priv->gtt_space == NULL) {
2672 /* If the gtt is empty and we're still having trouble
2673 * fitting our object in, we're out of memory.
2674 */
2675#if WATCH_LRU
2676 DRM_INFO("%s: GTT full, evicting something\n", __func__);
2677#endif
Daniel Vetter0108a3e2010-08-07 11:01:21 +01002678 ret = i915_gem_evict_something(dev, obj->size, alignment);
Chris Wilson97311292009-09-21 00:22:34 +01002679 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002680 return ret;
Chris Wilson97311292009-09-21 00:22:34 +01002681
Eric Anholt673a3942008-07-30 12:06:12 -07002682 goto search_free;
2683 }
2684
2685#if WATCH_BUF
Krzysztof Halasacfd43c02009-06-20 00:31:28 +02002686 DRM_INFO("Binding object of size %zd at 0x%08x\n",
Eric Anholt673a3942008-07-30 12:06:12 -07002687 obj->size, obj_priv->gtt_offset);
2688#endif
Chris Wilson4bdadb92010-01-27 13:36:32 +00002689 ret = i915_gem_object_get_pages(obj, gfpmask);
Eric Anholt673a3942008-07-30 12:06:12 -07002690 if (ret) {
2691 drm_mm_put_block(obj_priv->gtt_space);
2692 obj_priv->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002693
2694 if (ret == -ENOMEM) {
2695 /* first try to clear up some space from the GTT */
Daniel Vetter0108a3e2010-08-07 11:01:21 +01002696 ret = i915_gem_evict_something(dev, obj->size,
2697 alignment);
Chris Wilson07f73f62009-09-14 16:50:30 +01002698 if (ret) {
Chris Wilson07f73f62009-09-14 16:50:30 +01002699 /* now try to shrink everyone else */
Chris Wilson4bdadb92010-01-27 13:36:32 +00002700 if (gfpmask) {
2701 gfpmask = 0;
2702 goto search_free;
Chris Wilson07f73f62009-09-14 16:50:30 +01002703 }
2704
2705 return ret;
2706 }
2707
2708 goto search_free;
2709 }
2710
Eric Anholt673a3942008-07-30 12:06:12 -07002711 return ret;
2712 }
2713
Eric Anholt673a3942008-07-30 12:06:12 -07002714 /* Create an AGP memory structure pointing at our pages, and bind it
2715 * into the GTT.
2716 */
2717 obj_priv->agp_mem = drm_agp_bind_pages(dev,
Eric Anholt856fa192009-03-19 14:10:50 -07002718 obj_priv->pages,
Chris Wilson07f73f62009-09-14 16:50:30 +01002719 obj->size >> PAGE_SHIFT,
Keith Packardba1eb1d2008-10-14 19:55:10 -07002720 obj_priv->gtt_offset,
2721 obj_priv->agp_type);
Eric Anholt673a3942008-07-30 12:06:12 -07002722 if (obj_priv->agp_mem == NULL) {
Eric Anholt856fa192009-03-19 14:10:50 -07002723 i915_gem_object_put_pages(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002724 drm_mm_put_block(obj_priv->gtt_space);
2725 obj_priv->gtt_space = NULL;
Chris Wilson07f73f62009-09-14 16:50:30 +01002726
Daniel Vetter0108a3e2010-08-07 11:01:21 +01002727 ret = i915_gem_evict_something(dev, obj->size, alignment);
Chris Wilson97311292009-09-21 00:22:34 +01002728 if (ret)
Chris Wilson07f73f62009-09-14 16:50:30 +01002729 return ret;
Chris Wilson07f73f62009-09-14 16:50:30 +01002730
2731 goto search_free;
Eric Anholt673a3942008-07-30 12:06:12 -07002732 }
2733 atomic_inc(&dev->gtt_count);
2734 atomic_add(obj->size, &dev->gtt_memory);
2735
Chris Wilsonbf1a1092010-08-07 11:01:20 +01002736 /* keep track of bounds object by adding it to the inactive list */
2737 list_add_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
2738
Eric Anholt673a3942008-07-30 12:06:12 -07002739 /* Assert that the object is not currently in any GPU domain. As it
2740 * wasn't in the GTT, there shouldn't be any way it could have been in
2741 * a GPU cache
2742 */
Chris Wilson21d509e2009-06-06 09:46:02 +01002743 BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS);
2744 BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS);
Eric Anholt673a3942008-07-30 12:06:12 -07002745
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002746 trace_i915_gem_object_bind(obj, obj_priv->gtt_offset);
2747
Eric Anholt673a3942008-07-30 12:06:12 -07002748 return 0;
2749}
2750
2751void
2752i915_gem_clflush_object(struct drm_gem_object *obj)
2753{
Daniel Vetter23010e42010-03-08 13:35:02 +01002754 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002755
2756 /* If we don't have a page list set up, then we're not pinned
2757 * to GPU, and we can ignore the cache flush because it'll happen
2758 * again at bind time.
2759 */
Eric Anholt856fa192009-03-19 14:10:50 -07002760 if (obj_priv->pages == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002761 return;
2762
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002763 trace_i915_gem_object_clflush(obj);
Eric Anholtcfa16a02009-05-26 18:46:16 -07002764
Eric Anholt856fa192009-03-19 14:10:50 -07002765 drm_clflush_pages(obj_priv->pages, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002766}
2767
Eric Anholte47c68e2008-11-14 13:35:19 -08002768/** Flushes any GPU write domain for the object if it's dirty. */
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002769static int
Eric Anholte47c68e2008-11-14 13:35:19 -08002770i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj)
2771{
2772 struct drm_device *dev = obj->dev;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002773 uint32_t old_write_domain;
Zou Nan hai852835f2010-05-21 09:08:56 +08002774 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08002775
2776 if ((obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002777 return 0;
Eric Anholte47c68e2008-11-14 13:35:19 -08002778
2779 /* Queue the GPU write cache flushing we need. */
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002780 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002781 i915_gem_flush(dev, 0, obj->write_domain);
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002782 if (i915_add_request(dev, NULL, obj->write_domain, obj_priv->ring) == 0)
2783 return -ENOMEM;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002784
2785 trace_i915_gem_object_change_domain(obj,
2786 obj->read_domains,
2787 old_write_domain);
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002788 return 0;
Eric Anholte47c68e2008-11-14 13:35:19 -08002789}
2790
2791/** Flushes the GTT write domain for the object if it's dirty. */
2792static void
2793i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj)
2794{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002795 uint32_t old_write_domain;
2796
Eric Anholte47c68e2008-11-14 13:35:19 -08002797 if (obj->write_domain != I915_GEM_DOMAIN_GTT)
2798 return;
2799
2800 /* No actual flushing is required for the GTT write domain. Writes
2801 * to it immediately go to main memory as far as we know, so there's
2802 * no chipset flush. It also doesn't land in render cache.
2803 */
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002804 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002805 obj->write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002806
2807 trace_i915_gem_object_change_domain(obj,
2808 obj->read_domains,
2809 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002810}
2811
2812/** Flushes the CPU write domain for the object if it's dirty. */
2813static void
2814i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj)
2815{
2816 struct drm_device *dev = obj->dev;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002817 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002818
2819 if (obj->write_domain != I915_GEM_DOMAIN_CPU)
2820 return;
2821
2822 i915_gem_clflush_object(obj);
2823 drm_agp_chipset_flush(dev);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002824 old_write_domain = obj->write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08002825 obj->write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002826
2827 trace_i915_gem_object_change_domain(obj,
2828 obj->read_domains,
2829 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002830}
2831
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002832int
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002833i915_gem_object_flush_write_domain(struct drm_gem_object *obj)
2834{
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002835 int ret = 0;
2836
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002837 switch (obj->write_domain) {
2838 case I915_GEM_DOMAIN_GTT:
2839 i915_gem_object_flush_gtt_write_domain(obj);
2840 break;
2841 case I915_GEM_DOMAIN_CPU:
2842 i915_gem_object_flush_cpu_write_domain(obj);
2843 break;
2844 default:
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002845 ret = i915_gem_object_flush_gpu_write_domain(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002846 break;
2847 }
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002848
2849 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002850}
2851
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002852/**
2853 * Moves a single object to the GTT read, and possibly write domain.
2854 *
2855 * This function returns when the move is complete, including waiting on
2856 * flushes to occur.
2857 */
Jesse Barnes79e53942008-11-07 14:24:08 -08002858int
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002859i915_gem_object_set_to_gtt_domain(struct drm_gem_object *obj, int write)
2860{
Daniel Vetter23010e42010-03-08 13:35:02 +01002861 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002862 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002863 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002864
Eric Anholt02354392008-11-26 13:58:13 -08002865 /* Not valid to be called on unbound objects. */
2866 if (obj_priv->gtt_space == NULL)
2867 return -EINVAL;
2868
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002869 ret = i915_gem_object_flush_gpu_write_domain(obj);
2870 if (ret != 0)
2871 return ret;
2872
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002873 /* Wait on any GPU rendering and flushing to occur. */
Eric Anholte47c68e2008-11-14 13:35:19 -08002874 ret = i915_gem_object_wait_rendering(obj);
2875 if (ret != 0)
2876 return ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002877
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002878 old_write_domain = obj->write_domain;
2879 old_read_domains = obj->read_domains;
2880
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002881 /* If we're writing through the GTT domain, then CPU and GPU caches
2882 * will need to be invalidated at next use.
2883 */
2884 if (write)
Eric Anholte47c68e2008-11-14 13:35:19 -08002885 obj->read_domains &= I915_GEM_DOMAIN_GTT;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002886
Eric Anholte47c68e2008-11-14 13:35:19 -08002887 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002888
2889 /* It should now be out of any other write domains, and we can update
2890 * the domain values for our changes.
2891 */
2892 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2893 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002894 if (write) {
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002895 obj->write_domain = I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002896 obj_priv->dirty = 1;
2897 }
2898
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002899 trace_i915_gem_object_change_domain(obj,
2900 old_read_domains,
2901 old_write_domain);
2902
Eric Anholte47c68e2008-11-14 13:35:19 -08002903 return 0;
2904}
2905
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002906/*
2907 * Prepare buffer for display plane. Use uninterruptible for possible flush
2908 * wait, as in modesetting process we're not supposed to be interrupted.
2909 */
2910int
2911i915_gem_object_set_to_display_plane(struct drm_gem_object *obj)
2912{
2913 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01002914 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002915 uint32_t old_write_domain, old_read_domains;
2916 int ret;
2917
2918 /* Not valid to be called on unbound objects. */
2919 if (obj_priv->gtt_space == NULL)
2920 return -EINVAL;
2921
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002922 ret = i915_gem_object_flush_gpu_write_domain(obj);
2923 if (ret)
2924 return ret;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002925
2926 /* Wait on any GPU rendering and flushing to occur. */
2927 if (obj_priv->active) {
2928#if WATCH_BUF
2929 DRM_INFO("%s: object %p wait for seqno %08x\n",
2930 __func__, obj, obj_priv->last_rendering_seqno);
2931#endif
Zou Nan hai852835f2010-05-21 09:08:56 +08002932 ret = i915_do_wait_request(dev,
2933 obj_priv->last_rendering_seqno,
2934 0,
2935 obj_priv->ring);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002936 if (ret != 0)
2937 return ret;
2938 }
2939
Chris Wilsonb118c1e2010-05-27 13:18:14 +01002940 i915_gem_object_flush_cpu_write_domain(obj);
2941
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002942 old_write_domain = obj->write_domain;
2943 old_read_domains = obj->read_domains;
2944
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002945 /* It should now be out of any other write domains, and we can update
2946 * the domain values for our changes.
2947 */
2948 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
Chris Wilsonb118c1e2010-05-27 13:18:14 +01002949 obj->read_domains = I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08002950 obj->write_domain = I915_GEM_DOMAIN_GTT;
2951 obj_priv->dirty = 1;
2952
2953 trace_i915_gem_object_change_domain(obj,
2954 old_read_domains,
2955 old_write_domain);
2956
2957 return 0;
2958}
2959
Eric Anholte47c68e2008-11-14 13:35:19 -08002960/**
2961 * Moves a single object to the CPU read, and possibly write domain.
2962 *
2963 * This function returns when the move is complete, including waiting on
2964 * flushes to occur.
2965 */
2966static int
2967i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj, int write)
2968{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002969 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08002970 int ret;
2971
Chris Wilson2dafb1e2010-06-07 14:03:05 +01002972 ret = i915_gem_object_flush_gpu_write_domain(obj);
2973 if (ret)
2974 return ret;
2975
Eric Anholte47c68e2008-11-14 13:35:19 -08002976 /* Wait on any GPU rendering and flushing to occur. */
2977 ret = i915_gem_object_wait_rendering(obj);
2978 if (ret != 0)
2979 return ret;
2980
2981 i915_gem_object_flush_gtt_write_domain(obj);
2982
2983 /* If we have a partially-valid cache of the object in the CPU,
2984 * finish invalidating it and free the per-page flags.
2985 */
2986 i915_gem_object_set_to_full_cpu_read_domain(obj);
2987
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002988 old_write_domain = obj->write_domain;
2989 old_read_domains = obj->read_domains;
2990
Eric Anholte47c68e2008-11-14 13:35:19 -08002991 /* Flush the CPU cache if it's still invalid. */
2992 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
2993 i915_gem_clflush_object(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08002994
2995 obj->read_domains |= I915_GEM_DOMAIN_CPU;
2996 }
2997
2998 /* It should now be out of any other write domains, and we can update
2999 * the domain values for our changes.
3000 */
3001 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
3002
3003 /* If we're writing through the CPU, then the GPU read domains will
3004 * need to be invalidated at next use.
3005 */
3006 if (write) {
3007 obj->read_domains &= I915_GEM_DOMAIN_CPU;
3008 obj->write_domain = I915_GEM_DOMAIN_CPU;
3009 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003010
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003011 trace_i915_gem_object_change_domain(obj,
3012 old_read_domains,
3013 old_write_domain);
3014
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003015 return 0;
3016}
3017
Eric Anholt673a3942008-07-30 12:06:12 -07003018/*
3019 * Set the next domain for the specified object. This
3020 * may not actually perform the necessary flushing/invaliding though,
3021 * as that may want to be batched with other set_domain operations
3022 *
3023 * This is (we hope) the only really tricky part of gem. The goal
3024 * is fairly simple -- track which caches hold bits of the object
3025 * and make sure they remain coherent. A few concrete examples may
3026 * help to explain how it works. For shorthand, we use the notation
3027 * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
3028 * a pair of read and write domain masks.
3029 *
3030 * Case 1: the batch buffer
3031 *
3032 * 1. Allocated
3033 * 2. Written by CPU
3034 * 3. Mapped to GTT
3035 * 4. Read by GPU
3036 * 5. Unmapped from GTT
3037 * 6. Freed
3038 *
3039 * Let's take these a step at a time
3040 *
3041 * 1. Allocated
3042 * Pages allocated from the kernel may still have
3043 * cache contents, so we set them to (CPU, CPU) always.
3044 * 2. Written by CPU (using pwrite)
3045 * The pwrite function calls set_domain (CPU, CPU) and
3046 * this function does nothing (as nothing changes)
3047 * 3. Mapped by GTT
3048 * This function asserts that the object is not
3049 * currently in any GPU-based read or write domains
3050 * 4. Read by GPU
3051 * i915_gem_execbuffer calls set_domain (COMMAND, 0).
3052 * As write_domain is zero, this function adds in the
3053 * current read domains (CPU+COMMAND, 0).
3054 * flush_domains is set to CPU.
3055 * invalidate_domains is set to COMMAND
3056 * clflush is run to get data out of the CPU caches
3057 * then i915_dev_set_domain calls i915_gem_flush to
3058 * emit an MI_FLUSH and drm_agp_chipset_flush
3059 * 5. Unmapped from GTT
3060 * i915_gem_object_unbind calls set_domain (CPU, CPU)
3061 * flush_domains and invalidate_domains end up both zero
3062 * so no flushing/invalidating happens
3063 * 6. Freed
3064 * yay, done
3065 *
3066 * Case 2: The shared render buffer
3067 *
3068 * 1. Allocated
3069 * 2. Mapped to GTT
3070 * 3. Read/written by GPU
3071 * 4. set_domain to (CPU,CPU)
3072 * 5. Read/written by CPU
3073 * 6. Read/written by GPU
3074 *
3075 * 1. Allocated
3076 * Same as last example, (CPU, CPU)
3077 * 2. Mapped to GTT
3078 * Nothing changes (assertions find that it is not in the GPU)
3079 * 3. Read/written by GPU
3080 * execbuffer calls set_domain (RENDER, RENDER)
3081 * flush_domains gets CPU
3082 * invalidate_domains gets GPU
3083 * clflush (obj)
3084 * MI_FLUSH and drm_agp_chipset_flush
3085 * 4. set_domain (CPU, CPU)
3086 * flush_domains gets GPU
3087 * invalidate_domains gets CPU
3088 * wait_rendering (obj) to make sure all drawing is complete.
3089 * This will include an MI_FLUSH to get the data from GPU
3090 * to memory
3091 * clflush (obj) to invalidate the CPU cache
3092 * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
3093 * 5. Read/written by CPU
3094 * cache lines are loaded and dirtied
3095 * 6. Read written by GPU
3096 * Same as last GPU access
3097 *
3098 * Case 3: The constant buffer
3099 *
3100 * 1. Allocated
3101 * 2. Written by CPU
3102 * 3. Read by GPU
3103 * 4. Updated (written) by CPU again
3104 * 5. Read by GPU
3105 *
3106 * 1. Allocated
3107 * (CPU, CPU)
3108 * 2. Written by CPU
3109 * (CPU, CPU)
3110 * 3. Read by GPU
3111 * (CPU+RENDER, 0)
3112 * flush_domains = CPU
3113 * invalidate_domains = RENDER
3114 * clflush (obj)
3115 * MI_FLUSH
3116 * drm_agp_chipset_flush
3117 * 4. Updated (written) by CPU again
3118 * (CPU, CPU)
3119 * flush_domains = 0 (no previous write domain)
3120 * invalidate_domains = 0 (no new read domains)
3121 * 5. Read by GPU
3122 * (CPU+RENDER, 0)
3123 * flush_domains = CPU
3124 * invalidate_domains = RENDER
3125 * clflush (obj)
3126 * MI_FLUSH
3127 * drm_agp_chipset_flush
3128 */
Keith Packardc0d90822008-11-20 23:11:08 -08003129static void
Eric Anholt8b0e3782009-02-19 14:40:50 -08003130i915_gem_object_set_to_gpu_domain(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07003131{
3132 struct drm_device *dev = obj->dev;
Chris Wilson88f356b2010-08-04 13:55:32 +01003133 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01003134 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003135 uint32_t invalidate_domains = 0;
3136 uint32_t flush_domains = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003137 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003138
Eric Anholt8b0e3782009-02-19 14:40:50 -08003139 BUG_ON(obj->pending_read_domains & I915_GEM_DOMAIN_CPU);
3140 BUG_ON(obj->pending_write_domain == I915_GEM_DOMAIN_CPU);
Eric Anholt673a3942008-07-30 12:06:12 -07003141
Jesse Barnes652c3932009-08-17 13:31:43 -07003142 intel_mark_busy(dev, obj);
3143
Eric Anholt673a3942008-07-30 12:06:12 -07003144#if WATCH_BUF
3145 DRM_INFO("%s: object %p read %08x -> %08x write %08x -> %08x\n",
3146 __func__, obj,
Eric Anholt8b0e3782009-02-19 14:40:50 -08003147 obj->read_domains, obj->pending_read_domains,
3148 obj->write_domain, obj->pending_write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07003149#endif
3150 /*
3151 * If the object isn't moving to a new write domain,
3152 * let the object stay in multiple read domains
3153 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003154 if (obj->pending_write_domain == 0)
3155 obj->pending_read_domains |= obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07003156 else
3157 obj_priv->dirty = 1;
3158
3159 /*
3160 * Flush the current write domain if
3161 * the new read domains don't match. Invalidate
3162 * any read domains which differ from the old
3163 * write domain
3164 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003165 if (obj->write_domain &&
3166 obj->write_domain != obj->pending_read_domains) {
Eric Anholt673a3942008-07-30 12:06:12 -07003167 flush_domains |= obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08003168 invalidate_domains |=
3169 obj->pending_read_domains & ~obj->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07003170 }
3171 /*
3172 * Invalidate any read caches which may have
3173 * stale data. That is, any new read domains.
3174 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003175 invalidate_domains |= obj->pending_read_domains & ~obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07003176 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU) {
3177#if WATCH_BUF
3178 DRM_INFO("%s: CPU domain flush %08x invalidate %08x\n",
3179 __func__, flush_domains, invalidate_domains);
3180#endif
Eric Anholt673a3942008-07-30 12:06:12 -07003181 i915_gem_clflush_object(obj);
3182 }
3183
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003184 old_read_domains = obj->read_domains;
3185
Eric Anholtefbeed92009-02-19 14:54:51 -08003186 /* The actual obj->write_domain will be updated with
3187 * pending_write_domain after we emit the accumulated flush for all
3188 * of our domain changes in execbuffers (which clears objects'
3189 * write_domains). So if we have a current write domain that we
3190 * aren't changing, set pending_write_domain to that.
3191 */
3192 if (flush_domains == 0 && obj->pending_write_domain == 0)
3193 obj->pending_write_domain = obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08003194 obj->read_domains = obj->pending_read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07003195
Chris Wilson88f356b2010-08-04 13:55:32 +01003196 if (flush_domains & I915_GEM_GPU_DOMAINS) {
3197 if (obj_priv->ring == &dev_priv->render_ring)
3198 dev_priv->flush_rings |= FLUSH_RENDER_RING;
3199 else if (obj_priv->ring == &dev_priv->bsd_ring)
3200 dev_priv->flush_rings |= FLUSH_BSD_RING;
3201 }
3202
Eric Anholt673a3942008-07-30 12:06:12 -07003203 dev->invalidate_domains |= invalidate_domains;
3204 dev->flush_domains |= flush_domains;
3205#if WATCH_BUF
3206 DRM_INFO("%s: read %08x write %08x invalidate %08x flush %08x\n",
3207 __func__,
3208 obj->read_domains, obj->write_domain,
3209 dev->invalidate_domains, dev->flush_domains);
3210#endif
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003211
3212 trace_i915_gem_object_change_domain(obj,
3213 old_read_domains,
3214 obj->write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07003215}
3216
3217/**
Eric Anholte47c68e2008-11-14 13:35:19 -08003218 * Moves the object from a partially CPU read to a full one.
Eric Anholt673a3942008-07-30 12:06:12 -07003219 *
Eric Anholte47c68e2008-11-14 13:35:19 -08003220 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
3221 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
3222 */
3223static void
3224i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj)
3225{
Daniel Vetter23010e42010-03-08 13:35:02 +01003226 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003227
3228 if (!obj_priv->page_cpu_valid)
3229 return;
3230
3231 /* If we're partially in the CPU read domain, finish moving it in.
3232 */
3233 if (obj->read_domains & I915_GEM_DOMAIN_CPU) {
3234 int i;
3235
3236 for (i = 0; i <= (obj->size - 1) / PAGE_SIZE; i++) {
3237 if (obj_priv->page_cpu_valid[i])
3238 continue;
Eric Anholt856fa192009-03-19 14:10:50 -07003239 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholte47c68e2008-11-14 13:35:19 -08003240 }
Eric Anholte47c68e2008-11-14 13:35:19 -08003241 }
3242
3243 /* Free the page_cpu_valid mappings which are now stale, whether
3244 * or not we've got I915_GEM_DOMAIN_CPU.
3245 */
Eric Anholt9a298b22009-03-24 12:23:04 -07003246 kfree(obj_priv->page_cpu_valid);
Eric Anholte47c68e2008-11-14 13:35:19 -08003247 obj_priv->page_cpu_valid = NULL;
3248}
3249
3250/**
3251 * Set the CPU read domain on a range of the object.
3252 *
3253 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
3254 * not entirely valid. The page_cpu_valid member of the object flags which
3255 * pages have been flushed, and will be respected by
3256 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
3257 * of the whole object.
3258 *
3259 * This function returns when the move is complete, including waiting on
3260 * flushes to occur.
Eric Anholt673a3942008-07-30 12:06:12 -07003261 */
3262static int
Eric Anholte47c68e2008-11-14 13:35:19 -08003263i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
3264 uint64_t offset, uint64_t size)
Eric Anholt673a3942008-07-30 12:06:12 -07003265{
Daniel Vetter23010e42010-03-08 13:35:02 +01003266 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003267 uint32_t old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003268 int i, ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003269
Eric Anholte47c68e2008-11-14 13:35:19 -08003270 if (offset == 0 && size == obj->size)
3271 return i915_gem_object_set_to_cpu_domain(obj, 0);
3272
Chris Wilson2dafb1e2010-06-07 14:03:05 +01003273 ret = i915_gem_object_flush_gpu_write_domain(obj);
3274 if (ret)
3275 return ret;
3276
Eric Anholte47c68e2008-11-14 13:35:19 -08003277 /* Wait on any GPU rendering and flushing to occur. */
3278 ret = i915_gem_object_wait_rendering(obj);
3279 if (ret != 0)
3280 return ret;
3281 i915_gem_object_flush_gtt_write_domain(obj);
3282
3283 /* If we're already fully in the CPU read domain, we're done. */
3284 if (obj_priv->page_cpu_valid == NULL &&
3285 (obj->read_domains & I915_GEM_DOMAIN_CPU) != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07003286 return 0;
3287
Eric Anholte47c68e2008-11-14 13:35:19 -08003288 /* Otherwise, create/clear the per-page CPU read domain flag if we're
3289 * newly adding I915_GEM_DOMAIN_CPU
3290 */
Eric Anholt673a3942008-07-30 12:06:12 -07003291 if (obj_priv->page_cpu_valid == NULL) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003292 obj_priv->page_cpu_valid = kzalloc(obj->size / PAGE_SIZE,
3293 GFP_KERNEL);
Eric Anholte47c68e2008-11-14 13:35:19 -08003294 if (obj_priv->page_cpu_valid == NULL)
3295 return -ENOMEM;
3296 } else if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0)
3297 memset(obj_priv->page_cpu_valid, 0, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07003298
3299 /* Flush the cache on any pages that are still invalid from the CPU's
3300 * perspective.
3301 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003302 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
3303 i++) {
Eric Anholt673a3942008-07-30 12:06:12 -07003304 if (obj_priv->page_cpu_valid[i])
3305 continue;
3306
Eric Anholt856fa192009-03-19 14:10:50 -07003307 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07003308
3309 obj_priv->page_cpu_valid[i] = 1;
3310 }
3311
Eric Anholte47c68e2008-11-14 13:35:19 -08003312 /* It should now be out of any other write domains, and we can update
3313 * the domain values for our changes.
3314 */
3315 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
3316
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003317 old_read_domains = obj->read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003318 obj->read_domains |= I915_GEM_DOMAIN_CPU;
3319
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003320 trace_i915_gem_object_change_domain(obj,
3321 old_read_domains,
3322 obj->write_domain);
3323
Eric Anholt673a3942008-07-30 12:06:12 -07003324 return 0;
3325}
3326
3327/**
Eric Anholt673a3942008-07-30 12:06:12 -07003328 * Pin an object to the GTT and evaluate the relocations landing in it.
3329 */
3330static int
3331i915_gem_object_pin_and_relocate(struct drm_gem_object *obj,
3332 struct drm_file *file_priv,
Jesse Barnes76446ca2009-12-17 22:05:42 -05003333 struct drm_i915_gem_exec_object2 *entry,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003334 struct drm_i915_gem_relocation_entry *relocs)
Eric Anholt673a3942008-07-30 12:06:12 -07003335{
3336 struct drm_device *dev = obj->dev;
Keith Packard0839ccb2008-10-30 19:38:48 -07003337 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01003338 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003339 int i, ret;
Keith Packard0839ccb2008-10-30 19:38:48 -07003340 void __iomem *reloc_page;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003341 bool need_fence;
3342
3343 need_fence = entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
3344 obj_priv->tiling_mode != I915_TILING_NONE;
3345
3346 /* Check fence reg constraints and rebind if necessary */
Chris Wilson808b24d62010-05-27 13:18:15 +01003347 if (need_fence &&
3348 !i915_gem_object_fence_offset_ok(obj,
3349 obj_priv->tiling_mode)) {
3350 ret = i915_gem_object_unbind(obj);
3351 if (ret)
3352 return ret;
3353 }
Eric Anholt673a3942008-07-30 12:06:12 -07003354
3355 /* Choose the GTT offset for our buffer and put it there. */
3356 ret = i915_gem_object_pin(obj, (uint32_t) entry->alignment);
3357 if (ret)
3358 return ret;
3359
Jesse Barnes76446ca2009-12-17 22:05:42 -05003360 /*
3361 * Pre-965 chips need a fence register set up in order to
3362 * properly handle blits to/from tiled surfaces.
3363 */
3364 if (need_fence) {
3365 ret = i915_gem_object_get_fence_reg(obj);
3366 if (ret != 0) {
Jesse Barnes76446ca2009-12-17 22:05:42 -05003367 i915_gem_object_unpin(obj);
3368 return ret;
3369 }
3370 }
3371
Eric Anholt673a3942008-07-30 12:06:12 -07003372 entry->offset = obj_priv->gtt_offset;
3373
Eric Anholt673a3942008-07-30 12:06:12 -07003374 /* Apply the relocations, using the GTT aperture to avoid cache
3375 * flushing requirements.
3376 */
3377 for (i = 0; i < entry->relocation_count; i++) {
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003378 struct drm_i915_gem_relocation_entry *reloc= &relocs[i];
Eric Anholt673a3942008-07-30 12:06:12 -07003379 struct drm_gem_object *target_obj;
3380 struct drm_i915_gem_object *target_obj_priv;
Eric Anholt3043c602008-10-02 12:24:47 -07003381 uint32_t reloc_val, reloc_offset;
3382 uint32_t __iomem *reloc_entry;
Eric Anholt673a3942008-07-30 12:06:12 -07003383
Eric Anholt673a3942008-07-30 12:06:12 -07003384 target_obj = drm_gem_object_lookup(obj->dev, file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003385 reloc->target_handle);
Eric Anholt673a3942008-07-30 12:06:12 -07003386 if (target_obj == NULL) {
3387 i915_gem_object_unpin(obj);
3388 return -EBADF;
3389 }
Daniel Vetter23010e42010-03-08 13:35:02 +01003390 target_obj_priv = to_intel_bo(target_obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003391
Chris Wilson8542a0b2009-09-09 21:15:15 +01003392#if WATCH_RELOC
3393 DRM_INFO("%s: obj %p offset %08x target %d "
3394 "read %08x write %08x gtt %08x "
3395 "presumed %08x delta %08x\n",
3396 __func__,
3397 obj,
3398 (int) reloc->offset,
3399 (int) reloc->target_handle,
3400 (int) reloc->read_domains,
3401 (int) reloc->write_domain,
3402 (int) target_obj_priv->gtt_offset,
3403 (int) reloc->presumed_offset,
3404 reloc->delta);
3405#endif
3406
Eric Anholt673a3942008-07-30 12:06:12 -07003407 /* The target buffer should have appeared before us in the
3408 * exec_object list, so it should have a GTT space bound by now.
3409 */
3410 if (target_obj_priv->gtt_space == NULL) {
3411 DRM_ERROR("No GTT space found for object %d\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003412 reloc->target_handle);
Eric Anholt673a3942008-07-30 12:06:12 -07003413 drm_gem_object_unreference(target_obj);
3414 i915_gem_object_unpin(obj);
3415 return -EINVAL;
3416 }
3417
Chris Wilson8542a0b2009-09-09 21:15:15 +01003418 /* Validate that the target is in a valid r/w GPU domain */
Daniel Vetter16edd552010-02-19 11:52:02 +01003419 if (reloc->write_domain & (reloc->write_domain - 1)) {
3420 DRM_ERROR("reloc with multiple write domains: "
3421 "obj %p target %d offset %d "
3422 "read %08x write %08x",
3423 obj, reloc->target_handle,
3424 (int) reloc->offset,
3425 reloc->read_domains,
3426 reloc->write_domain);
3427 return -EINVAL;
3428 }
Chris Wilson8542a0b2009-09-09 21:15:15 +01003429 if (reloc->write_domain & I915_GEM_DOMAIN_CPU ||
3430 reloc->read_domains & I915_GEM_DOMAIN_CPU) {
3431 DRM_ERROR("reloc with read/write CPU domains: "
3432 "obj %p target %d offset %d "
3433 "read %08x write %08x",
3434 obj, reloc->target_handle,
3435 (int) reloc->offset,
3436 reloc->read_domains,
3437 reloc->write_domain);
3438 drm_gem_object_unreference(target_obj);
3439 i915_gem_object_unpin(obj);
3440 return -EINVAL;
3441 }
3442 if (reloc->write_domain && target_obj->pending_write_domain &&
3443 reloc->write_domain != target_obj->pending_write_domain) {
3444 DRM_ERROR("Write domain conflict: "
3445 "obj %p target %d offset %d "
3446 "new %08x old %08x\n",
3447 obj, reloc->target_handle,
3448 (int) reloc->offset,
3449 reloc->write_domain,
3450 target_obj->pending_write_domain);
3451 drm_gem_object_unreference(target_obj);
3452 i915_gem_object_unpin(obj);
3453 return -EINVAL;
3454 }
3455
3456 target_obj->pending_read_domains |= reloc->read_domains;
3457 target_obj->pending_write_domain |= reloc->write_domain;
3458
3459 /* If the relocation already has the right value in it, no
3460 * more work needs to be done.
3461 */
3462 if (target_obj_priv->gtt_offset == reloc->presumed_offset) {
3463 drm_gem_object_unreference(target_obj);
3464 continue;
3465 }
3466
3467 /* Check that the relocation address is valid... */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003468 if (reloc->offset > obj->size - 4) {
Eric Anholt673a3942008-07-30 12:06:12 -07003469 DRM_ERROR("Relocation beyond object bounds: "
3470 "obj %p target %d offset %d size %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003471 obj, reloc->target_handle,
3472 (int) reloc->offset, (int) obj->size);
Eric Anholt673a3942008-07-30 12:06:12 -07003473 drm_gem_object_unreference(target_obj);
3474 i915_gem_object_unpin(obj);
3475 return -EINVAL;
3476 }
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003477 if (reloc->offset & 3) {
Eric Anholt673a3942008-07-30 12:06:12 -07003478 DRM_ERROR("Relocation not 4-byte aligned: "
3479 "obj %p target %d offset %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003480 obj, reloc->target_handle,
3481 (int) reloc->offset);
Eric Anholt673a3942008-07-30 12:06:12 -07003482 drm_gem_object_unreference(target_obj);
3483 i915_gem_object_unpin(obj);
3484 return -EINVAL;
3485 }
3486
Chris Wilson8542a0b2009-09-09 21:15:15 +01003487 /* and points to somewhere within the target object. */
Chris Wilsoncd0b9fb2009-09-15 23:23:18 +01003488 if (reloc->delta >= target_obj->size) {
3489 DRM_ERROR("Relocation beyond target object bounds: "
3490 "obj %p target %d delta %d size %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003491 obj, reloc->target_handle,
Chris Wilsoncd0b9fb2009-09-15 23:23:18 +01003492 (int) reloc->delta, (int) target_obj->size);
Chris Wilson491152b2009-02-11 14:26:32 +00003493 drm_gem_object_unreference(target_obj);
3494 i915_gem_object_unpin(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003495 return -EINVAL;
3496 }
3497
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003498 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
3499 if (ret != 0) {
3500 drm_gem_object_unreference(target_obj);
3501 i915_gem_object_unpin(obj);
3502 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -07003503 }
3504
3505 /* Map the page containing the relocation we're going to
3506 * perform.
3507 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003508 reloc_offset = obj_priv->gtt_offset + reloc->offset;
Keith Packard0839ccb2008-10-30 19:38:48 -07003509 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
3510 (reloc_offset &
Chris Wilsonfca3ec02010-08-04 14:34:24 +01003511 ~(PAGE_SIZE - 1)),
3512 KM_USER0);
Eric Anholt3043c602008-10-02 12:24:47 -07003513 reloc_entry = (uint32_t __iomem *)(reloc_page +
Keith Packard0839ccb2008-10-30 19:38:48 -07003514 (reloc_offset & (PAGE_SIZE - 1)));
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003515 reloc_val = target_obj_priv->gtt_offset + reloc->delta;
Eric Anholt673a3942008-07-30 12:06:12 -07003516
3517#if WATCH_BUF
3518 DRM_INFO("Applied relocation: %p@0x%08x %08x -> %08x\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003519 obj, (unsigned int) reloc->offset,
Eric Anholt673a3942008-07-30 12:06:12 -07003520 readl(reloc_entry), reloc_val);
3521#endif
3522 writel(reloc_val, reloc_entry);
Chris Wilsonfca3ec02010-08-04 14:34:24 +01003523 io_mapping_unmap_atomic(reloc_page, KM_USER0);
Eric Anholt673a3942008-07-30 12:06:12 -07003524
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003525 /* The updated presumed offset for this entry will be
3526 * copied back out to the user.
Eric Anholt673a3942008-07-30 12:06:12 -07003527 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003528 reloc->presumed_offset = target_obj_priv->gtt_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003529
3530 drm_gem_object_unreference(target_obj);
3531 }
3532
Eric Anholt673a3942008-07-30 12:06:12 -07003533#if WATCH_BUF
3534 if (0)
3535 i915_gem_dump_object(obj, 128, __func__, ~0);
3536#endif
3537 return 0;
3538}
3539
Eric Anholt673a3942008-07-30 12:06:12 -07003540/* Throttle our rendering by waiting until the ring has completed our requests
3541 * emitted over 20 msec ago.
3542 *
Eric Anholtb9624422009-06-03 07:27:35 +00003543 * Note that if we were to use the current jiffies each time around the loop,
3544 * we wouldn't escape the function with any frames outstanding if the time to
3545 * render a frame was over 20ms.
3546 *
Eric Anholt673a3942008-07-30 12:06:12 -07003547 * This should get us reasonable parallelism between CPU and GPU but also
3548 * relatively low latency when blocking on a particular request to finish.
3549 */
3550static int
3551i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file_priv)
3552{
3553 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
3554 int ret = 0;
Eric Anholtb9624422009-06-03 07:27:35 +00003555 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
Eric Anholt673a3942008-07-30 12:06:12 -07003556
3557 mutex_lock(&dev->struct_mutex);
Eric Anholtb9624422009-06-03 07:27:35 +00003558 while (!list_empty(&i915_file_priv->mm.request_list)) {
3559 struct drm_i915_gem_request *request;
3560
3561 request = list_first_entry(&i915_file_priv->mm.request_list,
3562 struct drm_i915_gem_request,
3563 client_list);
3564
3565 if (time_after_eq(request->emitted_jiffies, recent_enough))
3566 break;
3567
Zou Nan hai852835f2010-05-21 09:08:56 +08003568 ret = i915_wait_request(dev, request->seqno, request->ring);
Eric Anholtb9624422009-06-03 07:27:35 +00003569 if (ret != 0)
3570 break;
3571 }
Eric Anholt673a3942008-07-30 12:06:12 -07003572 mutex_unlock(&dev->struct_mutex);
Eric Anholtb9624422009-06-03 07:27:35 +00003573
Eric Anholt673a3942008-07-30 12:06:12 -07003574 return ret;
3575}
3576
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003577static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003578i915_gem_get_relocs_from_user(struct drm_i915_gem_exec_object2 *exec_list,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003579 uint32_t buffer_count,
3580 struct drm_i915_gem_relocation_entry **relocs)
3581{
3582 uint32_t reloc_count = 0, reloc_index = 0, i;
3583 int ret;
3584
3585 *relocs = NULL;
3586 for (i = 0; i < buffer_count; i++) {
3587 if (reloc_count + exec_list[i].relocation_count < reloc_count)
3588 return -EINVAL;
3589 reloc_count += exec_list[i].relocation_count;
3590 }
3591
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003592 *relocs = drm_calloc_large(reloc_count, sizeof(**relocs));
Jesse Barnes76446ca2009-12-17 22:05:42 -05003593 if (*relocs == NULL) {
3594 DRM_ERROR("failed to alloc relocs, count %d\n", reloc_count);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003595 return -ENOMEM;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003596 }
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003597
3598 for (i = 0; i < buffer_count; i++) {
3599 struct drm_i915_gem_relocation_entry __user *user_relocs;
3600
3601 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3602
3603 ret = copy_from_user(&(*relocs)[reloc_index],
3604 user_relocs,
3605 exec_list[i].relocation_count *
3606 sizeof(**relocs));
3607 if (ret != 0) {
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003608 drm_free_large(*relocs);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003609 *relocs = NULL;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003610 return -EFAULT;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003611 }
3612
3613 reloc_index += exec_list[i].relocation_count;
3614 }
3615
Florian Mickler2bc43b52009-04-06 22:55:41 +02003616 return 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003617}
3618
3619static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003620i915_gem_put_relocs_to_user(struct drm_i915_gem_exec_object2 *exec_list,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003621 uint32_t buffer_count,
3622 struct drm_i915_gem_relocation_entry *relocs)
3623{
3624 uint32_t reloc_count = 0, i;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003625 int ret = 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003626
Chris Wilson93533c22010-01-31 10:40:48 +00003627 if (relocs == NULL)
3628 return 0;
3629
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003630 for (i = 0; i < buffer_count; i++) {
3631 struct drm_i915_gem_relocation_entry __user *user_relocs;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003632 int unwritten;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003633
3634 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3635
Florian Mickler2bc43b52009-04-06 22:55:41 +02003636 unwritten = copy_to_user(user_relocs,
3637 &relocs[reloc_count],
3638 exec_list[i].relocation_count *
3639 sizeof(*relocs));
3640
3641 if (unwritten) {
3642 ret = -EFAULT;
3643 goto err;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003644 }
3645
3646 reloc_count += exec_list[i].relocation_count;
3647 }
3648
Florian Mickler2bc43b52009-04-06 22:55:41 +02003649err:
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003650 drm_free_large(relocs);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003651
3652 return ret;
3653}
3654
Chris Wilson83d60792009-06-06 09:45:57 +01003655static int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003656i915_gem_check_execbuffer (struct drm_i915_gem_execbuffer2 *exec,
Chris Wilson83d60792009-06-06 09:45:57 +01003657 uint64_t exec_offset)
3658{
3659 uint32_t exec_start, exec_len;
3660
3661 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
3662 exec_len = (uint32_t) exec->batch_len;
3663
3664 if ((exec_start | exec_len) & 0x7)
3665 return -EINVAL;
3666
3667 if (!exec_start)
3668 return -EINVAL;
3669
3670 return 0;
3671}
3672
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003673static int
3674i915_gem_wait_for_pending_flip(struct drm_device *dev,
3675 struct drm_gem_object **object_list,
3676 int count)
3677{
3678 drm_i915_private_t *dev_priv = dev->dev_private;
3679 struct drm_i915_gem_object *obj_priv;
3680 DEFINE_WAIT(wait);
3681 int i, ret = 0;
3682
3683 for (;;) {
3684 prepare_to_wait(&dev_priv->pending_flip_queue,
3685 &wait, TASK_INTERRUPTIBLE);
3686 for (i = 0; i < count; i++) {
Daniel Vetter23010e42010-03-08 13:35:02 +01003687 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003688 if (atomic_read(&obj_priv->pending_flip) > 0)
3689 break;
3690 }
3691 if (i == count)
3692 break;
3693
3694 if (!signal_pending(current)) {
3695 mutex_unlock(&dev->struct_mutex);
3696 schedule();
3697 mutex_lock(&dev->struct_mutex);
3698 continue;
3699 }
3700 ret = -ERESTARTSYS;
3701 break;
3702 }
3703 finish_wait(&dev_priv->pending_flip_queue, &wait);
3704
3705 return ret;
3706}
3707
Chris Wilson43b27f42010-07-02 08:57:15 +01003708
Eric Anholt673a3942008-07-30 12:06:12 -07003709int
Jesse Barnes76446ca2009-12-17 22:05:42 -05003710i915_gem_do_execbuffer(struct drm_device *dev, void *data,
3711 struct drm_file *file_priv,
3712 struct drm_i915_gem_execbuffer2 *args,
3713 struct drm_i915_gem_exec_object2 *exec_list)
Eric Anholt673a3942008-07-30 12:06:12 -07003714{
3715 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003716 struct drm_gem_object **object_list = NULL;
3717 struct drm_gem_object *batch_obj;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003718 struct drm_i915_gem_object *obj_priv;
Eric Anholt201361a2009-03-11 12:30:04 -07003719 struct drm_clip_rect *cliprects = NULL;
Chris Wilson93533c22010-01-31 10:40:48 +00003720 struct drm_i915_gem_relocation_entry *relocs = NULL;
Jesse Barnes76446ca2009-12-17 22:05:42 -05003721 int ret = 0, ret2, i, pinned = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003722 uint64_t exec_offset;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003723 uint32_t seqno, flush_domains, reloc_index;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003724 int pin_tries, flips;
Eric Anholt673a3942008-07-30 12:06:12 -07003725
Zou Nan hai852835f2010-05-21 09:08:56 +08003726 struct intel_ring_buffer *ring = NULL;
3727
Eric Anholt673a3942008-07-30 12:06:12 -07003728#if WATCH_EXEC
3729 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3730 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3731#endif
Zou Nan haid1b851f2010-05-21 09:08:57 +08003732 if (args->flags & I915_EXEC_BSD) {
3733 if (!HAS_BSD(dev)) {
3734 DRM_ERROR("execbuf with wrong flag\n");
3735 return -EINVAL;
3736 }
3737 ring = &dev_priv->bsd_ring;
3738 } else {
3739 ring = &dev_priv->render_ring;
3740 }
3741
Eric Anholt4f481ed2008-09-10 14:22:49 -07003742 if (args->buffer_count < 1) {
3743 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3744 return -EINVAL;
3745 }
Eric Anholtc8e0f932009-11-22 03:49:37 +01003746 object_list = drm_malloc_ab(sizeof(*object_list), args->buffer_count);
Jesse Barnes76446ca2009-12-17 22:05:42 -05003747 if (object_list == NULL) {
3748 DRM_ERROR("Failed to allocate object list for %d buffers\n",
Eric Anholt673a3942008-07-30 12:06:12 -07003749 args->buffer_count);
3750 ret = -ENOMEM;
3751 goto pre_mutex_err;
3752 }
Eric Anholt673a3942008-07-30 12:06:12 -07003753
Eric Anholt201361a2009-03-11 12:30:04 -07003754 if (args->num_cliprects != 0) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003755 cliprects = kcalloc(args->num_cliprects, sizeof(*cliprects),
3756 GFP_KERNEL);
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003757 if (cliprects == NULL) {
3758 ret = -ENOMEM;
Eric Anholt201361a2009-03-11 12:30:04 -07003759 goto pre_mutex_err;
Owain Ainswortha40e8d32010-02-09 14:25:55 +00003760 }
Eric Anholt201361a2009-03-11 12:30:04 -07003761
3762 ret = copy_from_user(cliprects,
3763 (struct drm_clip_rect __user *)
3764 (uintptr_t) args->cliprects_ptr,
3765 sizeof(*cliprects) * args->num_cliprects);
3766 if (ret != 0) {
3767 DRM_ERROR("copy %d cliprects failed: %d\n",
3768 args->num_cliprects, ret);
3769 goto pre_mutex_err;
3770 }
3771 }
3772
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003773 ret = i915_gem_get_relocs_from_user(exec_list, args->buffer_count,
3774 &relocs);
3775 if (ret != 0)
3776 goto pre_mutex_err;
3777
Eric Anholt673a3942008-07-30 12:06:12 -07003778 mutex_lock(&dev->struct_mutex);
3779
3780 i915_verify_inactive(dev, __FILE__, __LINE__);
3781
Ben Gamariba1234d2009-09-14 17:48:47 -04003782 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07003783 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003784 ret = -EIO;
3785 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003786 }
3787
3788 if (dev_priv->mm.suspended) {
Eric Anholt673a3942008-07-30 12:06:12 -07003789 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003790 ret = -EBUSY;
3791 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003792 }
3793
Keith Packardac94a962008-11-20 23:30:27 -08003794 /* Look up object handles */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003795 flips = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003796 for (i = 0; i < args->buffer_count; i++) {
3797 object_list[i] = drm_gem_object_lookup(dev, file_priv,
3798 exec_list[i].handle);
3799 if (object_list[i] == NULL) {
3800 DRM_ERROR("Invalid object handle %d at index %d\n",
3801 exec_list[i].handle, i);
Chris Wilson0ce907f2010-01-23 20:26:35 +00003802 /* prevent error path from reading uninitialized data */
3803 args->buffer_count = i + 1;
Eric Anholt673a3942008-07-30 12:06:12 -07003804 ret = -EBADF;
3805 goto err;
3806 }
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003807
Daniel Vetter23010e42010-03-08 13:35:02 +01003808 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003809 if (obj_priv->in_execbuffer) {
3810 DRM_ERROR("Object %p appears more than once in object list\n",
3811 object_list[i]);
Chris Wilson0ce907f2010-01-23 20:26:35 +00003812 /* prevent error path from reading uninitialized data */
3813 args->buffer_count = i + 1;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003814 ret = -EBADF;
3815 goto err;
3816 }
3817 obj_priv->in_execbuffer = true;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003818 flips += atomic_read(&obj_priv->pending_flip);
3819 }
3820
3821 if (flips > 0) {
3822 ret = i915_gem_wait_for_pending_flip(dev, object_list,
3823 args->buffer_count);
3824 if (ret)
3825 goto err;
Keith Packardac94a962008-11-20 23:30:27 -08003826 }
Eric Anholt673a3942008-07-30 12:06:12 -07003827
Keith Packardac94a962008-11-20 23:30:27 -08003828 /* Pin and relocate */
3829 for (pin_tries = 0; ; pin_tries++) {
3830 ret = 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003831 reloc_index = 0;
3832
Keith Packardac94a962008-11-20 23:30:27 -08003833 for (i = 0; i < args->buffer_count; i++) {
3834 object_list[i]->pending_read_domains = 0;
3835 object_list[i]->pending_write_domain = 0;
3836 ret = i915_gem_object_pin_and_relocate(object_list[i],
3837 file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003838 &exec_list[i],
3839 &relocs[reloc_index]);
Keith Packardac94a962008-11-20 23:30:27 -08003840 if (ret)
3841 break;
3842 pinned = i + 1;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003843 reloc_index += exec_list[i].relocation_count;
Keith Packardac94a962008-11-20 23:30:27 -08003844 }
3845 /* success */
3846 if (ret == 0)
3847 break;
3848
3849 /* error other than GTT full, or we've already tried again */
Chris Wilson2939e1f2009-06-06 09:46:03 +01003850 if (ret != -ENOSPC || pin_tries >= 1) {
Chris Wilson07f73f62009-09-14 16:50:30 +01003851 if (ret != -ERESTARTSYS) {
3852 unsigned long long total_size = 0;
Chris Wilson3d1cc472010-05-27 13:18:19 +01003853 int num_fences = 0;
3854 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson43b27f42010-07-02 08:57:15 +01003855 obj_priv = to_intel_bo(object_list[i]);
Chris Wilson3d1cc472010-05-27 13:18:19 +01003856
Chris Wilson07f73f62009-09-14 16:50:30 +01003857 total_size += object_list[i]->size;
Chris Wilson3d1cc472010-05-27 13:18:19 +01003858 num_fences +=
3859 exec_list[i].flags & EXEC_OBJECT_NEEDS_FENCE &&
3860 obj_priv->tiling_mode != I915_TILING_NONE;
3861 }
3862 DRM_ERROR("Failed to pin buffer %d of %d, total %llu bytes, %d fences: %d\n",
Chris Wilson07f73f62009-09-14 16:50:30 +01003863 pinned+1, args->buffer_count,
Chris Wilson3d1cc472010-05-27 13:18:19 +01003864 total_size, num_fences,
3865 ret);
Chris Wilson07f73f62009-09-14 16:50:30 +01003866 DRM_ERROR("%d objects [%d pinned], "
3867 "%d object bytes [%d pinned], "
3868 "%d/%d gtt bytes\n",
3869 atomic_read(&dev->object_count),
3870 atomic_read(&dev->pin_count),
3871 atomic_read(&dev->object_memory),
3872 atomic_read(&dev->pin_memory),
3873 atomic_read(&dev->gtt_memory),
3874 dev->gtt_total);
3875 }
Eric Anholt673a3942008-07-30 12:06:12 -07003876 goto err;
3877 }
Keith Packardac94a962008-11-20 23:30:27 -08003878
3879 /* unpin all of our buffers */
3880 for (i = 0; i < pinned; i++)
3881 i915_gem_object_unpin(object_list[i]);
Eric Anholtb1177632008-12-10 10:09:41 -08003882 pinned = 0;
Keith Packardac94a962008-11-20 23:30:27 -08003883
3884 /* evict everyone we can from the aperture */
3885 ret = i915_gem_evict_everything(dev);
Chris Wilson07f73f62009-09-14 16:50:30 +01003886 if (ret && ret != -ENOSPC)
Keith Packardac94a962008-11-20 23:30:27 -08003887 goto err;
Eric Anholt673a3942008-07-30 12:06:12 -07003888 }
3889
3890 /* Set the pending read domains for the batch buffer to COMMAND */
3891 batch_obj = object_list[args->buffer_count-1];
Chris Wilson5f26a2c2009-06-06 09:45:58 +01003892 if (batch_obj->pending_write_domain) {
3893 DRM_ERROR("Attempting to use self-modifying batch buffer\n");
3894 ret = -EINVAL;
3895 goto err;
3896 }
3897 batch_obj->pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
Eric Anholt673a3942008-07-30 12:06:12 -07003898
Chris Wilson83d60792009-06-06 09:45:57 +01003899 /* Sanity check the batch buffer, prior to moving objects */
3900 exec_offset = exec_list[args->buffer_count - 1].offset;
3901 ret = i915_gem_check_execbuffer (args, exec_offset);
3902 if (ret != 0) {
3903 DRM_ERROR("execbuf with invalid offset/length\n");
3904 goto err;
3905 }
3906
Eric Anholt673a3942008-07-30 12:06:12 -07003907 i915_verify_inactive(dev, __FILE__, __LINE__);
3908
Keith Packard646f0f62008-11-20 23:23:03 -08003909 /* Zero the global flush/invalidate flags. These
3910 * will be modified as new domains are computed
3911 * for each object
3912 */
3913 dev->invalidate_domains = 0;
3914 dev->flush_domains = 0;
Chris Wilson88f356b2010-08-04 13:55:32 +01003915 dev_priv->flush_rings = 0;
Keith Packard646f0f62008-11-20 23:23:03 -08003916
Eric Anholt673a3942008-07-30 12:06:12 -07003917 for (i = 0; i < args->buffer_count; i++) {
3918 struct drm_gem_object *obj = object_list[i];
Eric Anholt673a3942008-07-30 12:06:12 -07003919
Keith Packard646f0f62008-11-20 23:23:03 -08003920 /* Compute new gpu domains and update invalidate/flush */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003921 i915_gem_object_set_to_gpu_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003922 }
3923
3924 i915_verify_inactive(dev, __FILE__, __LINE__);
3925
Keith Packard646f0f62008-11-20 23:23:03 -08003926 if (dev->invalidate_domains | dev->flush_domains) {
3927#if WATCH_EXEC
3928 DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
3929 __func__,
3930 dev->invalidate_domains,
3931 dev->flush_domains);
3932#endif
3933 i915_gem_flush(dev,
3934 dev->invalidate_domains,
3935 dev->flush_domains);
Chris Wilson88f356b2010-08-04 13:55:32 +01003936 if (dev_priv->flush_rings & FLUSH_RENDER_RING)
Eric Anholtb9624422009-06-03 07:27:35 +00003937 (void)i915_add_request(dev, file_priv,
Chris Wilson88f356b2010-08-04 13:55:32 +01003938 dev->flush_domains,
3939 &dev_priv->render_ring);
3940 if (dev_priv->flush_rings & FLUSH_BSD_RING)
3941 (void)i915_add_request(dev, file_priv,
3942 dev->flush_domains,
3943 &dev_priv->bsd_ring);
Keith Packard646f0f62008-11-20 23:23:03 -08003944 }
Eric Anholt673a3942008-07-30 12:06:12 -07003945
Eric Anholtefbeed92009-02-19 14:54:51 -08003946 for (i = 0; i < args->buffer_count; i++) {
3947 struct drm_gem_object *obj = object_list[i];
Daniel Vetter23010e42010-03-08 13:35:02 +01003948 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003949 uint32_t old_write_domain = obj->write_domain;
Eric Anholtefbeed92009-02-19 14:54:51 -08003950
3951 obj->write_domain = obj->pending_write_domain;
Daniel Vetter99fcb762010-02-07 16:20:18 +01003952 if (obj->write_domain)
3953 list_move_tail(&obj_priv->gpu_write_list,
3954 &dev_priv->mm.gpu_write_list);
3955 else
3956 list_del_init(&obj_priv->gpu_write_list);
3957
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003958 trace_i915_gem_object_change_domain(obj,
3959 obj->read_domains,
3960 old_write_domain);
Eric Anholtefbeed92009-02-19 14:54:51 -08003961 }
3962
Eric Anholt673a3942008-07-30 12:06:12 -07003963 i915_verify_inactive(dev, __FILE__, __LINE__);
3964
3965#if WATCH_COHERENCY
3966 for (i = 0; i < args->buffer_count; i++) {
3967 i915_gem_object_check_coherency(object_list[i],
3968 exec_list[i].handle);
3969 }
3970#endif
3971
Eric Anholt673a3942008-07-30 12:06:12 -07003972#if WATCH_EXEC
Ben Gamari6911a9b2009-04-02 11:24:54 -07003973 i915_gem_dump_object(batch_obj,
Eric Anholt673a3942008-07-30 12:06:12 -07003974 args->batch_len,
3975 __func__,
3976 ~0);
3977#endif
3978
Eric Anholt673a3942008-07-30 12:06:12 -07003979 /* Exec the batchbuffer */
Zou Nan hai852835f2010-05-21 09:08:56 +08003980 ret = ring->dispatch_gem_execbuffer(dev, ring, args,
3981 cliprects, exec_offset);
Eric Anholt673a3942008-07-30 12:06:12 -07003982 if (ret) {
3983 DRM_ERROR("dispatch failed %d\n", ret);
3984 goto err;
3985 }
3986
3987 /*
3988 * Ensure that the commands in the batch buffer are
3989 * finished before the interrupt fires
3990 */
Zou Nan hai852835f2010-05-21 09:08:56 +08003991 flush_domains = i915_retire_commands(dev, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07003992
3993 i915_verify_inactive(dev, __FILE__, __LINE__);
3994
3995 /*
3996 * Get a seqno representing the execution of the current buffer,
3997 * which we can wait on. We would like to mitigate these interrupts,
3998 * likely by only creating seqnos occasionally (so that we have
3999 * *some* interrupts representing completion of buffers that we can
4000 * wait on when trying to clear up gtt space).
4001 */
Zou Nan hai852835f2010-05-21 09:08:56 +08004002 seqno = i915_add_request(dev, file_priv, flush_domains, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07004003 BUG_ON(seqno == 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004004 for (i = 0; i < args->buffer_count; i++) {
4005 struct drm_gem_object *obj = object_list[i];
Zou Nan hai852835f2010-05-21 09:08:56 +08004006 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004007
Zou Nan hai852835f2010-05-21 09:08:56 +08004008 i915_gem_object_move_to_active(obj, seqno, ring);
Eric Anholt673a3942008-07-30 12:06:12 -07004009#if WATCH_LRU
4010 DRM_INFO("%s: move to exec list %p\n", __func__, obj);
4011#endif
4012 }
4013#if WATCH_LRU
4014 i915_dump_lru(dev, __func__);
4015#endif
4016
4017 i915_verify_inactive(dev, __FILE__, __LINE__);
4018
Eric Anholt673a3942008-07-30 12:06:12 -07004019err:
Julia Lawallaad87df2008-12-21 16:28:47 +01004020 for (i = 0; i < pinned; i++)
4021 i915_gem_object_unpin(object_list[i]);
Eric Anholt673a3942008-07-30 12:06:12 -07004022
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05004023 for (i = 0; i < args->buffer_count; i++) {
4024 if (object_list[i]) {
Daniel Vetter23010e42010-03-08 13:35:02 +01004025 obj_priv = to_intel_bo(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05004026 obj_priv->in_execbuffer = false;
4027 }
Julia Lawallaad87df2008-12-21 16:28:47 +01004028 drm_gem_object_unreference(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05004029 }
Julia Lawallaad87df2008-12-21 16:28:47 +01004030
Eric Anholt673a3942008-07-30 12:06:12 -07004031 mutex_unlock(&dev->struct_mutex);
4032
Chris Wilson93533c22010-01-31 10:40:48 +00004033pre_mutex_err:
Eric Anholt40a5f0d2009-03-12 11:23:52 -07004034 /* Copy the updated relocations out regardless of current error
4035 * state. Failure to update the relocs would mean that the next
4036 * time userland calls execbuf, it would do so with presumed offset
4037 * state that didn't match the actual object state.
4038 */
4039 ret2 = i915_gem_put_relocs_to_user(exec_list, args->buffer_count,
4040 relocs);
4041 if (ret2 != 0) {
4042 DRM_ERROR("Failed to copy relocations back out: %d\n", ret2);
4043
4044 if (ret == 0)
4045 ret = ret2;
4046 }
4047
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07004048 drm_free_large(object_list);
Eric Anholt9a298b22009-03-24 12:23:04 -07004049 kfree(cliprects);
Eric Anholt673a3942008-07-30 12:06:12 -07004050
4051 return ret;
4052}
4053
Jesse Barnes76446ca2009-12-17 22:05:42 -05004054/*
4055 * Legacy execbuffer just creates an exec2 list from the original exec object
4056 * list array and passes it to the real function.
4057 */
4058int
4059i915_gem_execbuffer(struct drm_device *dev, void *data,
4060 struct drm_file *file_priv)
4061{
4062 struct drm_i915_gem_execbuffer *args = data;
4063 struct drm_i915_gem_execbuffer2 exec2;
4064 struct drm_i915_gem_exec_object *exec_list = NULL;
4065 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
4066 int ret, i;
4067
4068#if WATCH_EXEC
4069 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
4070 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
4071#endif
4072
4073 if (args->buffer_count < 1) {
4074 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
4075 return -EINVAL;
4076 }
4077
4078 /* Copy in the exec list from userland */
4079 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
4080 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
4081 if (exec_list == NULL || exec2_list == NULL) {
4082 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
4083 args->buffer_count);
4084 drm_free_large(exec_list);
4085 drm_free_large(exec2_list);
4086 return -ENOMEM;
4087 }
4088 ret = copy_from_user(exec_list,
4089 (struct drm_i915_relocation_entry __user *)
4090 (uintptr_t) args->buffers_ptr,
4091 sizeof(*exec_list) * args->buffer_count);
4092 if (ret != 0) {
4093 DRM_ERROR("copy %d exec entries failed %d\n",
4094 args->buffer_count, ret);
4095 drm_free_large(exec_list);
4096 drm_free_large(exec2_list);
4097 return -EFAULT;
4098 }
4099
4100 for (i = 0; i < args->buffer_count; i++) {
4101 exec2_list[i].handle = exec_list[i].handle;
4102 exec2_list[i].relocation_count = exec_list[i].relocation_count;
4103 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
4104 exec2_list[i].alignment = exec_list[i].alignment;
4105 exec2_list[i].offset = exec_list[i].offset;
4106 if (!IS_I965G(dev))
4107 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
4108 else
4109 exec2_list[i].flags = 0;
4110 }
4111
4112 exec2.buffers_ptr = args->buffers_ptr;
4113 exec2.buffer_count = args->buffer_count;
4114 exec2.batch_start_offset = args->batch_start_offset;
4115 exec2.batch_len = args->batch_len;
4116 exec2.DR1 = args->DR1;
4117 exec2.DR4 = args->DR4;
4118 exec2.num_cliprects = args->num_cliprects;
4119 exec2.cliprects_ptr = args->cliprects_ptr;
Zou Nan hai852835f2010-05-21 09:08:56 +08004120 exec2.flags = I915_EXEC_RENDER;
Jesse Barnes76446ca2009-12-17 22:05:42 -05004121
4122 ret = i915_gem_do_execbuffer(dev, data, file_priv, &exec2, exec2_list);
4123 if (!ret) {
4124 /* Copy the new buffer offsets back to the user's exec list. */
4125 for (i = 0; i < args->buffer_count; i++)
4126 exec_list[i].offset = exec2_list[i].offset;
4127 /* ... and back out to userspace */
4128 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
4129 (uintptr_t) args->buffers_ptr,
4130 exec_list,
4131 sizeof(*exec_list) * args->buffer_count);
4132 if (ret) {
4133 ret = -EFAULT;
4134 DRM_ERROR("failed to copy %d exec entries "
4135 "back to user (%d)\n",
4136 args->buffer_count, ret);
4137 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05004138 }
4139
4140 drm_free_large(exec_list);
4141 drm_free_large(exec2_list);
4142 return ret;
4143}
4144
4145int
4146i915_gem_execbuffer2(struct drm_device *dev, void *data,
4147 struct drm_file *file_priv)
4148{
4149 struct drm_i915_gem_execbuffer2 *args = data;
4150 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
4151 int ret;
4152
4153#if WATCH_EXEC
4154 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
4155 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
4156#endif
4157
4158 if (args->buffer_count < 1) {
4159 DRM_ERROR("execbuf2 with %d buffers\n", args->buffer_count);
4160 return -EINVAL;
4161 }
4162
4163 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
4164 if (exec2_list == NULL) {
4165 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
4166 args->buffer_count);
4167 return -ENOMEM;
4168 }
4169 ret = copy_from_user(exec2_list,
4170 (struct drm_i915_relocation_entry __user *)
4171 (uintptr_t) args->buffers_ptr,
4172 sizeof(*exec2_list) * args->buffer_count);
4173 if (ret != 0) {
4174 DRM_ERROR("copy %d exec entries failed %d\n",
4175 args->buffer_count, ret);
4176 drm_free_large(exec2_list);
4177 return -EFAULT;
4178 }
4179
4180 ret = i915_gem_do_execbuffer(dev, data, file_priv, args, exec2_list);
4181 if (!ret) {
4182 /* Copy the new buffer offsets back to the user's exec list. */
4183 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
4184 (uintptr_t) args->buffers_ptr,
4185 exec2_list,
4186 sizeof(*exec2_list) * args->buffer_count);
4187 if (ret) {
4188 ret = -EFAULT;
4189 DRM_ERROR("failed to copy %d exec entries "
4190 "back to user (%d)\n",
4191 args->buffer_count, ret);
4192 }
4193 }
4194
4195 drm_free_large(exec2_list);
4196 return ret;
4197}
4198
Eric Anholt673a3942008-07-30 12:06:12 -07004199int
4200i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment)
4201{
4202 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01004203 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004204 int ret;
4205
Daniel Vetter778c3542010-05-13 11:49:44 +02004206 BUG_ON(obj_priv->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT);
4207
Eric Anholt673a3942008-07-30 12:06:12 -07004208 i915_verify_inactive(dev, __FILE__, __LINE__);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01004209
4210 if (obj_priv->gtt_space != NULL) {
4211 if (alignment == 0)
4212 alignment = i915_gem_get_gtt_alignment(obj);
4213 if (obj_priv->gtt_offset & (alignment - 1)) {
Chris Wilsonae7d49d2010-08-04 12:37:41 +01004214 WARN(obj_priv->pin_count,
4215 "bo is already pinned with incorrect alignment:"
4216 " offset=%x, req.alignment=%x\n",
4217 obj_priv->gtt_offset, alignment);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01004218 ret = i915_gem_object_unbind(obj);
4219 if (ret)
4220 return ret;
4221 }
4222 }
4223
Eric Anholt673a3942008-07-30 12:06:12 -07004224 if (obj_priv->gtt_space == NULL) {
4225 ret = i915_gem_object_bind_to_gtt(obj, alignment);
Chris Wilson97311292009-09-21 00:22:34 +01004226 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07004227 return ret;
Chris Wilson22c344e2009-02-11 14:26:45 +00004228 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05004229
Eric Anholt673a3942008-07-30 12:06:12 -07004230 obj_priv->pin_count++;
4231
4232 /* If the object is not active and not pending a flush,
4233 * remove it from the inactive list
4234 */
4235 if (obj_priv->pin_count == 1) {
4236 atomic_inc(&dev->pin_count);
4237 atomic_add(obj->size, &dev->pin_memory);
4238 if (!obj_priv->active &&
Chris Wilsonbf1a1092010-08-07 11:01:20 +01004239 (obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
Eric Anholt673a3942008-07-30 12:06:12 -07004240 list_del_init(&obj_priv->list);
4241 }
4242 i915_verify_inactive(dev, __FILE__, __LINE__);
4243
4244 return 0;
4245}
4246
4247void
4248i915_gem_object_unpin(struct drm_gem_object *obj)
4249{
4250 struct drm_device *dev = obj->dev;
4251 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter23010e42010-03-08 13:35:02 +01004252 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004253
4254 i915_verify_inactive(dev, __FILE__, __LINE__);
4255 obj_priv->pin_count--;
4256 BUG_ON(obj_priv->pin_count < 0);
4257 BUG_ON(obj_priv->gtt_space == NULL);
4258
4259 /* If the object is no longer pinned, and is
4260 * neither active nor being flushed, then stick it on
4261 * the inactive list
4262 */
4263 if (obj_priv->pin_count == 0) {
4264 if (!obj_priv->active &&
Chris Wilson21d509e2009-06-06 09:46:02 +01004265 (obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
Eric Anholt673a3942008-07-30 12:06:12 -07004266 list_move_tail(&obj_priv->list,
4267 &dev_priv->mm.inactive_list);
4268 atomic_dec(&dev->pin_count);
4269 atomic_sub(obj->size, &dev->pin_memory);
4270 }
4271 i915_verify_inactive(dev, __FILE__, __LINE__);
4272}
4273
4274int
4275i915_gem_pin_ioctl(struct drm_device *dev, void *data,
4276 struct drm_file *file_priv)
4277{
4278 struct drm_i915_gem_pin *args = data;
4279 struct drm_gem_object *obj;
4280 struct drm_i915_gem_object *obj_priv;
4281 int ret;
4282
4283 mutex_lock(&dev->struct_mutex);
4284
4285 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4286 if (obj == NULL) {
4287 DRM_ERROR("Bad handle in i915_gem_pin_ioctl(): %d\n",
4288 args->handle);
4289 mutex_unlock(&dev->struct_mutex);
4290 return -EBADF;
4291 }
Daniel Vetter23010e42010-03-08 13:35:02 +01004292 obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004293
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004294 if (obj_priv->madv != I915_MADV_WILLNEED) {
4295 DRM_ERROR("Attempting to pin a purgeable buffer\n");
Chris Wilson3ef94da2009-09-14 16:50:29 +01004296 drm_gem_object_unreference(obj);
4297 mutex_unlock(&dev->struct_mutex);
4298 return -EINVAL;
4299 }
4300
Jesse Barnes79e53942008-11-07 14:24:08 -08004301 if (obj_priv->pin_filp != NULL && obj_priv->pin_filp != file_priv) {
4302 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
4303 args->handle);
Chris Wilson96dec612009-02-08 19:08:04 +00004304 drm_gem_object_unreference(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004305 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08004306 return -EINVAL;
4307 }
4308
4309 obj_priv->user_pin_count++;
4310 obj_priv->pin_filp = file_priv;
4311 if (obj_priv->user_pin_count == 1) {
4312 ret = i915_gem_object_pin(obj, args->alignment);
4313 if (ret != 0) {
4314 drm_gem_object_unreference(obj);
4315 mutex_unlock(&dev->struct_mutex);
4316 return ret;
4317 }
Eric Anholt673a3942008-07-30 12:06:12 -07004318 }
4319
4320 /* XXX - flush the CPU caches for pinned objects
4321 * as the X server doesn't manage domains yet
4322 */
Eric Anholte47c68e2008-11-14 13:35:19 -08004323 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004324 args->offset = obj_priv->gtt_offset;
4325 drm_gem_object_unreference(obj);
4326 mutex_unlock(&dev->struct_mutex);
4327
4328 return 0;
4329}
4330
4331int
4332i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
4333 struct drm_file *file_priv)
4334{
4335 struct drm_i915_gem_pin *args = data;
4336 struct drm_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08004337 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07004338
4339 mutex_lock(&dev->struct_mutex);
4340
4341 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4342 if (obj == NULL) {
4343 DRM_ERROR("Bad handle in i915_gem_unpin_ioctl(): %d\n",
4344 args->handle);
4345 mutex_unlock(&dev->struct_mutex);
4346 return -EBADF;
4347 }
4348
Daniel Vetter23010e42010-03-08 13:35:02 +01004349 obj_priv = to_intel_bo(obj);
Jesse Barnes79e53942008-11-07 14:24:08 -08004350 if (obj_priv->pin_filp != file_priv) {
4351 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
4352 args->handle);
4353 drm_gem_object_unreference(obj);
4354 mutex_unlock(&dev->struct_mutex);
4355 return -EINVAL;
4356 }
4357 obj_priv->user_pin_count--;
4358 if (obj_priv->user_pin_count == 0) {
4359 obj_priv->pin_filp = NULL;
4360 i915_gem_object_unpin(obj);
4361 }
Eric Anholt673a3942008-07-30 12:06:12 -07004362
4363 drm_gem_object_unreference(obj);
4364 mutex_unlock(&dev->struct_mutex);
4365 return 0;
4366}
4367
4368int
4369i915_gem_busy_ioctl(struct drm_device *dev, void *data,
4370 struct drm_file *file_priv)
4371{
4372 struct drm_i915_gem_busy *args = data;
4373 struct drm_gem_object *obj;
4374 struct drm_i915_gem_object *obj_priv;
4375
Eric Anholt673a3942008-07-30 12:06:12 -07004376 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4377 if (obj == NULL) {
4378 DRM_ERROR("Bad handle in i915_gem_busy_ioctl(): %d\n",
4379 args->handle);
Eric Anholt673a3942008-07-30 12:06:12 -07004380 return -EBADF;
4381 }
4382
Chris Wilsonb1ce7862009-06-06 09:46:00 +01004383 mutex_lock(&dev->struct_mutex);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004384
Chris Wilson0be555b2010-08-04 15:36:30 +01004385 /* Count all active objects as busy, even if they are currently not used
4386 * by the gpu. Users of this interface expect objects to eventually
4387 * become non-busy without any further actions, therefore emit any
4388 * necessary flushes here.
Eric Anholtc4de0a52008-12-14 19:05:04 -08004389 */
Chris Wilson0be555b2010-08-04 15:36:30 +01004390 obj_priv = to_intel_bo(obj);
4391 args->busy = obj_priv->active;
4392 if (args->busy) {
4393 /* Unconditionally flush objects, even when the gpu still uses this
4394 * object. Userspace calling this function indicates that it wants to
4395 * use this buffer rather sooner than later, so issuing the required
4396 * flush earlier is beneficial.
4397 */
4398 if (obj->write_domain) {
4399 i915_gem_flush(dev, 0, obj->write_domain);
4400 (void)i915_add_request(dev, file_priv, obj->write_domain, obj_priv->ring);
4401 }
4402
4403 /* Update the active list for the hardware's current position.
4404 * Otherwise this only updates on a delayed timer or when irqs
4405 * are actually unmasked, and our working set ends up being
4406 * larger than required.
4407 */
4408 i915_gem_retire_requests_ring(dev, obj_priv->ring);
4409
4410 args->busy = obj_priv->active;
4411 }
Eric Anholt673a3942008-07-30 12:06:12 -07004412
4413 drm_gem_object_unreference(obj);
4414 mutex_unlock(&dev->struct_mutex);
4415 return 0;
4416}
4417
4418int
4419i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4420 struct drm_file *file_priv)
4421{
4422 return i915_gem_ring_throttle(dev, file_priv);
4423}
4424
Chris Wilson3ef94da2009-09-14 16:50:29 +01004425int
4426i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4427 struct drm_file *file_priv)
4428{
4429 struct drm_i915_gem_madvise *args = data;
4430 struct drm_gem_object *obj;
4431 struct drm_i915_gem_object *obj_priv;
4432
4433 switch (args->madv) {
4434 case I915_MADV_DONTNEED:
4435 case I915_MADV_WILLNEED:
4436 break;
4437 default:
4438 return -EINVAL;
4439 }
4440
4441 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4442 if (obj == NULL) {
4443 DRM_ERROR("Bad handle in i915_gem_madvise_ioctl(): %d\n",
4444 args->handle);
4445 return -EBADF;
4446 }
4447
4448 mutex_lock(&dev->struct_mutex);
Daniel Vetter23010e42010-03-08 13:35:02 +01004449 obj_priv = to_intel_bo(obj);
Chris Wilson3ef94da2009-09-14 16:50:29 +01004450
4451 if (obj_priv->pin_count) {
4452 drm_gem_object_unreference(obj);
4453 mutex_unlock(&dev->struct_mutex);
4454
4455 DRM_ERROR("Attempted i915_gem_madvise_ioctl() on a pinned object\n");
4456 return -EINVAL;
4457 }
4458
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004459 if (obj_priv->madv != __I915_MADV_PURGED)
4460 obj_priv->madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004461
Chris Wilson2d7ef392009-09-20 23:13:10 +01004462 /* if the object is no longer bound, discard its backing storage */
4463 if (i915_gem_object_is_purgeable(obj_priv) &&
4464 obj_priv->gtt_space == NULL)
4465 i915_gem_object_truncate(obj);
4466
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004467 args->retained = obj_priv->madv != __I915_MADV_PURGED;
4468
Chris Wilson3ef94da2009-09-14 16:50:29 +01004469 drm_gem_object_unreference(obj);
4470 mutex_unlock(&dev->struct_mutex);
4471
4472 return 0;
4473}
4474
Daniel Vetterac52bc52010-04-09 19:05:06 +00004475struct drm_gem_object * i915_gem_alloc_object(struct drm_device *dev,
4476 size_t size)
4477{
Daniel Vetterc397b902010-04-09 19:05:07 +00004478 struct drm_i915_gem_object *obj;
4479
4480 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
4481 if (obj == NULL)
4482 return NULL;
4483
4484 if (drm_gem_object_init(dev, &obj->base, size) != 0) {
4485 kfree(obj);
4486 return NULL;
4487 }
4488
4489 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4490 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4491
4492 obj->agp_type = AGP_USER_MEMORY;
Daniel Vetter62b8b212010-04-09 19:05:08 +00004493 obj->base.driver_private = NULL;
Daniel Vetterc397b902010-04-09 19:05:07 +00004494 obj->fence_reg = I915_FENCE_REG_NONE;
4495 INIT_LIST_HEAD(&obj->list);
4496 INIT_LIST_HEAD(&obj->gpu_write_list);
Daniel Vetterc397b902010-04-09 19:05:07 +00004497 obj->madv = I915_MADV_WILLNEED;
4498
4499 trace_i915_gem_object_create(&obj->base);
4500
4501 return &obj->base;
Daniel Vetterac52bc52010-04-09 19:05:06 +00004502}
4503
Eric Anholt673a3942008-07-30 12:06:12 -07004504int i915_gem_init_object(struct drm_gem_object *obj)
4505{
Daniel Vetterc397b902010-04-09 19:05:07 +00004506 BUG();
Jesse Barnesde151cf2008-11-12 10:03:55 -08004507
Eric Anholt673a3942008-07-30 12:06:12 -07004508 return 0;
4509}
4510
Chris Wilsonbe726152010-07-23 23:18:50 +01004511static void i915_gem_free_object_tail(struct drm_gem_object *obj)
4512{
4513 struct drm_device *dev = obj->dev;
4514 drm_i915_private_t *dev_priv = dev->dev_private;
4515 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
4516 int ret;
4517
4518 ret = i915_gem_object_unbind(obj);
4519 if (ret == -ERESTARTSYS) {
4520 list_move(&obj_priv->list,
4521 &dev_priv->mm.deferred_free_list);
4522 return;
4523 }
4524
4525 if (obj_priv->mmap_offset)
4526 i915_gem_free_mmap_offset(obj);
4527
4528 drm_gem_object_release(obj);
4529
4530 kfree(obj_priv->page_cpu_valid);
4531 kfree(obj_priv->bit_17);
4532 kfree(obj_priv);
4533}
4534
Eric Anholt673a3942008-07-30 12:06:12 -07004535void i915_gem_free_object(struct drm_gem_object *obj)
4536{
Jesse Barnesde151cf2008-11-12 10:03:55 -08004537 struct drm_device *dev = obj->dev;
Daniel Vetter23010e42010-03-08 13:35:02 +01004538 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004539
Chris Wilson1c5d22f2009-08-25 11:15:50 +01004540 trace_i915_gem_object_destroy(obj);
4541
Eric Anholt673a3942008-07-30 12:06:12 -07004542 while (obj_priv->pin_count > 0)
4543 i915_gem_object_unpin(obj);
4544
Dave Airlie71acb5e2008-12-30 20:31:46 +10004545 if (obj_priv->phys_obj)
4546 i915_gem_detach_phys_object(dev, obj);
4547
Chris Wilsonbe726152010-07-23 23:18:50 +01004548 i915_gem_free_object_tail(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004549}
4550
Chris Wilsonab5ee572009-09-20 19:25:47 +01004551/** Unbinds all inactive objects. */
Eric Anholt673a3942008-07-30 12:06:12 -07004552static int
Chris Wilsonab5ee572009-09-20 19:25:47 +01004553i915_gem_evict_from_inactive_list(struct drm_device *dev)
Eric Anholt673a3942008-07-30 12:06:12 -07004554{
Chris Wilsonab5ee572009-09-20 19:25:47 +01004555 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07004556
Chris Wilsonab5ee572009-09-20 19:25:47 +01004557 while (!list_empty(&dev_priv->mm.inactive_list)) {
4558 struct drm_gem_object *obj;
4559 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004560
Daniel Vettera8089e82010-04-09 19:05:09 +00004561 obj = &list_first_entry(&dev_priv->mm.inactive_list,
4562 struct drm_i915_gem_object,
4563 list)->base;
Eric Anholt673a3942008-07-30 12:06:12 -07004564
4565 ret = i915_gem_object_unbind(obj);
4566 if (ret != 0) {
Chris Wilsonab5ee572009-09-20 19:25:47 +01004567 DRM_ERROR("Error unbinding object: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07004568 return ret;
4569 }
4570 }
4571
Eric Anholt673a3942008-07-30 12:06:12 -07004572 return 0;
4573}
4574
Jesse Barnes5669fca2009-02-17 15:13:31 -08004575int
Eric Anholt673a3942008-07-30 12:06:12 -07004576i915_gem_idle(struct drm_device *dev)
4577{
4578 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson29105cc2010-01-07 10:39:13 +00004579 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004580
Keith Packard6dbe2772008-10-14 21:41:13 -07004581 mutex_lock(&dev->struct_mutex);
4582
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004583 if (dev_priv->mm.suspended ||
Zou Nan haid1b851f2010-05-21 09:08:57 +08004584 (dev_priv->render_ring.gem_object == NULL) ||
4585 (HAS_BSD(dev) &&
4586 dev_priv->bsd_ring.gem_object == NULL)) {
Keith Packard6dbe2772008-10-14 21:41:13 -07004587 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004588 return 0;
Keith Packard6dbe2772008-10-14 21:41:13 -07004589 }
Eric Anholt673a3942008-07-30 12:06:12 -07004590
Chris Wilson29105cc2010-01-07 10:39:13 +00004591 ret = i915_gpu_idle(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004592 if (ret) {
4593 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004594 return ret;
Keith Packard6dbe2772008-10-14 21:41:13 -07004595 }
Eric Anholt673a3942008-07-30 12:06:12 -07004596
Chris Wilson29105cc2010-01-07 10:39:13 +00004597 /* Under UMS, be paranoid and evict. */
4598 if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
4599 ret = i915_gem_evict_from_inactive_list(dev);
4600 if (ret) {
4601 mutex_unlock(&dev->struct_mutex);
4602 return ret;
4603 }
4604 }
4605
4606 /* Hack! Don't let anybody do execbuf while we don't control the chip.
4607 * We need to replace this with a semaphore, or something.
4608 * And not confound mm.suspended!
4609 */
4610 dev_priv->mm.suspended = 1;
4611 del_timer(&dev_priv->hangcheck_timer);
4612
4613 i915_kernel_lost_context(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004614 i915_gem_cleanup_ringbuffer(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00004615
Keith Packard6dbe2772008-10-14 21:41:13 -07004616 mutex_unlock(&dev->struct_mutex);
4617
Chris Wilson29105cc2010-01-07 10:39:13 +00004618 /* Cancel the retire work handler, which should be idle now. */
4619 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
4620
Eric Anholt673a3942008-07-30 12:06:12 -07004621 return 0;
4622}
4623
Jesse Barnese552eb72010-04-21 11:39:23 -07004624/*
4625 * 965+ support PIPE_CONTROL commands, which provide finer grained control
4626 * over cache flushing.
4627 */
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004628static int
Jesse Barnese552eb72010-04-21 11:39:23 -07004629i915_gem_init_pipe_control(struct drm_device *dev)
4630{
4631 drm_i915_private_t *dev_priv = dev->dev_private;
4632 struct drm_gem_object *obj;
4633 struct drm_i915_gem_object *obj_priv;
4634 int ret;
4635
Eric Anholt34dc4d42010-05-07 14:30:03 -07004636 obj = i915_gem_alloc_object(dev, 4096);
Jesse Barnese552eb72010-04-21 11:39:23 -07004637 if (obj == NULL) {
4638 DRM_ERROR("Failed to allocate seqno page\n");
4639 ret = -ENOMEM;
4640 goto err;
4641 }
4642 obj_priv = to_intel_bo(obj);
4643 obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
4644
4645 ret = i915_gem_object_pin(obj, 4096);
4646 if (ret)
4647 goto err_unref;
4648
4649 dev_priv->seqno_gfx_addr = obj_priv->gtt_offset;
4650 dev_priv->seqno_page = kmap(obj_priv->pages[0]);
4651 if (dev_priv->seqno_page == NULL)
4652 goto err_unpin;
4653
4654 dev_priv->seqno_obj = obj;
4655 memset(dev_priv->seqno_page, 0, PAGE_SIZE);
4656
4657 return 0;
4658
4659err_unpin:
4660 i915_gem_object_unpin(obj);
4661err_unref:
4662 drm_gem_object_unreference(obj);
4663err:
4664 return ret;
4665}
4666
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004667
4668static void
Jesse Barnese552eb72010-04-21 11:39:23 -07004669i915_gem_cleanup_pipe_control(struct drm_device *dev)
4670{
4671 drm_i915_private_t *dev_priv = dev->dev_private;
4672 struct drm_gem_object *obj;
4673 struct drm_i915_gem_object *obj_priv;
4674
4675 obj = dev_priv->seqno_obj;
4676 obj_priv = to_intel_bo(obj);
4677 kunmap(obj_priv->pages[0]);
4678 i915_gem_object_unpin(obj);
4679 drm_gem_object_unreference(obj);
4680 dev_priv->seqno_obj = NULL;
4681
4682 dev_priv->seqno_page = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07004683}
4684
Eric Anholt673a3942008-07-30 12:06:12 -07004685int
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004686i915_gem_init_ringbuffer(struct drm_device *dev)
4687{
4688 drm_i915_private_t *dev_priv = dev->dev_private;
4689 int ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01004690
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004691 dev_priv->render_ring = render_ring;
Chris Wilson68f95ba2010-05-27 13:18:22 +01004692
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004693 if (!I915_NEED_GFX_HWS(dev)) {
4694 dev_priv->render_ring.status_page.page_addr
4695 = dev_priv->status_page_dmah->vaddr;
4696 memset(dev_priv->render_ring.status_page.page_addr,
4697 0, PAGE_SIZE);
4698 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004699
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004700 if (HAS_PIPE_CONTROL(dev)) {
4701 ret = i915_gem_init_pipe_control(dev);
4702 if (ret)
4703 return ret;
4704 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004705
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004706 ret = intel_init_ring_buffer(dev, &dev_priv->render_ring);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004707 if (ret)
4708 goto cleanup_pipe_control;
4709
4710 if (HAS_BSD(dev)) {
Zou Nan haid1b851f2010-05-21 09:08:57 +08004711 dev_priv->bsd_ring = bsd_ring;
4712 ret = intel_init_ring_buffer(dev, &dev_priv->bsd_ring);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004713 if (ret)
4714 goto cleanup_render_ring;
Zou Nan haid1b851f2010-05-21 09:08:57 +08004715 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004716
Chris Wilson6f392d5482010-08-07 11:01:22 +01004717 dev_priv->next_seqno = 1;
4718
Chris Wilson68f95ba2010-05-27 13:18:22 +01004719 return 0;
4720
4721cleanup_render_ring:
4722 intel_cleanup_ring_buffer(dev, &dev_priv->render_ring);
4723cleanup_pipe_control:
4724 if (HAS_PIPE_CONTROL(dev))
4725 i915_gem_cleanup_pipe_control(dev);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004726 return ret;
4727}
4728
4729void
4730i915_gem_cleanup_ringbuffer(struct drm_device *dev)
4731{
4732 drm_i915_private_t *dev_priv = dev->dev_private;
4733
4734 intel_cleanup_ring_buffer(dev, &dev_priv->render_ring);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004735 if (HAS_BSD(dev))
4736 intel_cleanup_ring_buffer(dev, &dev_priv->bsd_ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004737 if (HAS_PIPE_CONTROL(dev))
4738 i915_gem_cleanup_pipe_control(dev);
4739}
4740
4741int
Eric Anholt673a3942008-07-30 12:06:12 -07004742i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
4743 struct drm_file *file_priv)
4744{
4745 drm_i915_private_t *dev_priv = dev->dev_private;
4746 int ret;
4747
Jesse Barnes79e53942008-11-07 14:24:08 -08004748 if (drm_core_check_feature(dev, DRIVER_MODESET))
4749 return 0;
4750
Ben Gamariba1234d2009-09-14 17:48:47 -04004751 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07004752 DRM_ERROR("Reenabling wedged hardware, good luck\n");
Ben Gamariba1234d2009-09-14 17:48:47 -04004753 atomic_set(&dev_priv->mm.wedged, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004754 }
4755
Eric Anholt673a3942008-07-30 12:06:12 -07004756 mutex_lock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004757 dev_priv->mm.suspended = 0;
4758
4759 ret = i915_gem_init_ringbuffer(dev);
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004760 if (ret != 0) {
4761 mutex_unlock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004762 return ret;
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004763 }
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004764
Carl Worth5e118f42009-03-20 11:54:25 -07004765 spin_lock(&dev_priv->mm.active_list_lock);
Zou Nan hai852835f2010-05-21 09:08:56 +08004766 BUG_ON(!list_empty(&dev_priv->render_ring.active_list));
Zou Nan haid1b851f2010-05-21 09:08:57 +08004767 BUG_ON(HAS_BSD(dev) && !list_empty(&dev_priv->bsd_ring.active_list));
Carl Worth5e118f42009-03-20 11:54:25 -07004768 spin_unlock(&dev_priv->mm.active_list_lock);
4769
Eric Anholt673a3942008-07-30 12:06:12 -07004770 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
4771 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
Zou Nan hai852835f2010-05-21 09:08:56 +08004772 BUG_ON(!list_empty(&dev_priv->render_ring.request_list));
Zou Nan haid1b851f2010-05-21 09:08:57 +08004773 BUG_ON(HAS_BSD(dev) && !list_empty(&dev_priv->bsd_ring.request_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004774 mutex_unlock(&dev->struct_mutex);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004775
Chris Wilson5f353082010-06-07 14:03:03 +01004776 ret = drm_irq_install(dev);
4777 if (ret)
4778 goto cleanup_ringbuffer;
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004779
Eric Anholt673a3942008-07-30 12:06:12 -07004780 return 0;
Chris Wilson5f353082010-06-07 14:03:03 +01004781
4782cleanup_ringbuffer:
4783 mutex_lock(&dev->struct_mutex);
4784 i915_gem_cleanup_ringbuffer(dev);
4785 dev_priv->mm.suspended = 1;
4786 mutex_unlock(&dev->struct_mutex);
4787
4788 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004789}
4790
4791int
4792i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
4793 struct drm_file *file_priv)
4794{
Jesse Barnes79e53942008-11-07 14:24:08 -08004795 if (drm_core_check_feature(dev, DRIVER_MODESET))
4796 return 0;
4797
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004798 drm_irq_uninstall(dev);
Linus Torvaldse6890f62009-09-08 17:09:24 -07004799 return i915_gem_idle(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004800}
4801
4802void
4803i915_gem_lastclose(struct drm_device *dev)
4804{
4805 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004806
Eric Anholte806b492009-01-22 09:56:58 -08004807 if (drm_core_check_feature(dev, DRIVER_MODESET))
4808 return;
4809
Keith Packard6dbe2772008-10-14 21:41:13 -07004810 ret = i915_gem_idle(dev);
4811 if (ret)
4812 DRM_ERROR("failed to idle hardware: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07004813}
4814
4815void
4816i915_gem_load(struct drm_device *dev)
4817{
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004818 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07004819 drm_i915_private_t *dev_priv = dev->dev_private;
4820
Carl Worth5e118f42009-03-20 11:54:25 -07004821 spin_lock_init(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07004822 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
Daniel Vetter99fcb762010-02-07 16:20:18 +01004823 INIT_LIST_HEAD(&dev_priv->mm.gpu_write_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004824 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07004825 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Chris Wilsonbe726152010-07-23 23:18:50 +01004826 INIT_LIST_HEAD(&dev_priv->mm.deferred_free_list);
Zou Nan hai852835f2010-05-21 09:08:56 +08004827 INIT_LIST_HEAD(&dev_priv->render_ring.active_list);
4828 INIT_LIST_HEAD(&dev_priv->render_ring.request_list);
Zou Nan haid1b851f2010-05-21 09:08:57 +08004829 if (HAS_BSD(dev)) {
4830 INIT_LIST_HEAD(&dev_priv->bsd_ring.active_list);
4831 INIT_LIST_HEAD(&dev_priv->bsd_ring.request_list);
4832 }
Daniel Vetter007cc8a2010-04-28 11:02:31 +02004833 for (i = 0; i < 16; i++)
4834 INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004835 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
4836 i915_gem_retire_work_handler);
Chris Wilson31169712009-09-14 16:50:28 +01004837 spin_lock(&shrink_list_lock);
4838 list_add(&dev_priv->mm.shrink_list, &shrink_list);
4839 spin_unlock(&shrink_list_lock);
4840
Dave Airlie94400122010-07-20 13:15:31 +10004841 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
4842 if (IS_GEN3(dev)) {
4843 u32 tmp = I915_READ(MI_ARB_STATE);
4844 if (!(tmp & MI_ARB_C3_LP_WRITE_ENABLE)) {
4845 /* arb state is a masked write, so set bit + bit in mask */
4846 tmp = MI_ARB_C3_LP_WRITE_ENABLE | (MI_ARB_C3_LP_WRITE_ENABLE << MI_ARB_MASK_SHIFT);
4847 I915_WRITE(MI_ARB_STATE, tmp);
4848 }
4849 }
4850
Jesse Barnesde151cf2008-11-12 10:03:55 -08004851 /* Old X drivers will take 0-2 for front, back, depth buffers */
Eric Anholtb397c832010-01-26 09:43:10 -08004852 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4853 dev_priv->fence_reg_start = 3;
Jesse Barnesde151cf2008-11-12 10:03:55 -08004854
Jesse Barnes0f973f22009-01-26 17:10:45 -08004855 if (IS_I965G(dev) || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08004856 dev_priv->num_fence_regs = 16;
4857 else
4858 dev_priv->num_fence_regs = 8;
4859
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004860 /* Initialize fence registers to zero */
4861 if (IS_I965G(dev)) {
4862 for (i = 0; i < 16; i++)
4863 I915_WRITE64(FENCE_REG_965_0 + (i * 8), 0);
4864 } else {
4865 for (i = 0; i < 8; i++)
4866 I915_WRITE(FENCE_REG_830_0 + (i * 4), 0);
4867 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4868 for (i = 0; i < 8; i++)
4869 I915_WRITE(FENCE_REG_945_8 + (i * 4), 0);
4870 }
Eric Anholt673a3942008-07-30 12:06:12 -07004871 i915_gem_detect_bit_6_swizzle(dev);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05004872 init_waitqueue_head(&dev_priv->pending_flip_queue);
Eric Anholt673a3942008-07-30 12:06:12 -07004873}
Dave Airlie71acb5e2008-12-30 20:31:46 +10004874
4875/*
4876 * Create a physically contiguous memory object for this object
4877 * e.g. for cursor + overlay regs
4878 */
4879int i915_gem_init_phys_object(struct drm_device *dev,
4880 int id, int size)
4881{
4882 drm_i915_private_t *dev_priv = dev->dev_private;
4883 struct drm_i915_gem_phys_object *phys_obj;
4884 int ret;
4885
4886 if (dev_priv->mm.phys_objs[id - 1] || !size)
4887 return 0;
4888
Eric Anholt9a298b22009-03-24 12:23:04 -07004889 phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004890 if (!phys_obj)
4891 return -ENOMEM;
4892
4893 phys_obj->id = id;
4894
Zhenyu Wange6be8d92010-01-05 11:25:05 +08004895 phys_obj->handle = drm_pci_alloc(dev, size, 0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004896 if (!phys_obj->handle) {
4897 ret = -ENOMEM;
4898 goto kfree_obj;
4899 }
4900#ifdef CONFIG_X86
4901 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4902#endif
4903
4904 dev_priv->mm.phys_objs[id - 1] = phys_obj;
4905
4906 return 0;
4907kfree_obj:
Eric Anholt9a298b22009-03-24 12:23:04 -07004908 kfree(phys_obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004909 return ret;
4910}
4911
4912void i915_gem_free_phys_object(struct drm_device *dev, int id)
4913{
4914 drm_i915_private_t *dev_priv = dev->dev_private;
4915 struct drm_i915_gem_phys_object *phys_obj;
4916
4917 if (!dev_priv->mm.phys_objs[id - 1])
4918 return;
4919
4920 phys_obj = dev_priv->mm.phys_objs[id - 1];
4921 if (phys_obj->cur_obj) {
4922 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
4923 }
4924
4925#ifdef CONFIG_X86
4926 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4927#endif
4928 drm_pci_free(dev, phys_obj->handle);
4929 kfree(phys_obj);
4930 dev_priv->mm.phys_objs[id - 1] = NULL;
4931}
4932
4933void i915_gem_free_all_phys_object(struct drm_device *dev)
4934{
4935 int i;
4936
Dave Airlie260883c2009-01-22 17:58:49 +10004937 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004938 i915_gem_free_phys_object(dev, i);
4939}
4940
4941void i915_gem_detach_phys_object(struct drm_device *dev,
4942 struct drm_gem_object *obj)
4943{
4944 struct drm_i915_gem_object *obj_priv;
4945 int i;
4946 int ret;
4947 int page_count;
4948
Daniel Vetter23010e42010-03-08 13:35:02 +01004949 obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004950 if (!obj_priv->phys_obj)
4951 return;
4952
Chris Wilson4bdadb92010-01-27 13:36:32 +00004953 ret = i915_gem_object_get_pages(obj, 0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004954 if (ret)
4955 goto out;
4956
4957 page_count = obj->size / PAGE_SIZE;
4958
4959 for (i = 0; i < page_count; i++) {
Eric Anholt856fa192009-03-19 14:10:50 -07004960 char *dst = kmap_atomic(obj_priv->pages[i], KM_USER0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004961 char *src = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4962
4963 memcpy(dst, src, PAGE_SIZE);
4964 kunmap_atomic(dst, KM_USER0);
4965 }
Eric Anholt856fa192009-03-19 14:10:50 -07004966 drm_clflush_pages(obj_priv->pages, page_count);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004967 drm_agp_chipset_flush(dev);
Chris Wilsond78b47b2009-06-17 21:52:49 +01004968
4969 i915_gem_object_put_pages(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004970out:
4971 obj_priv->phys_obj->cur_obj = NULL;
4972 obj_priv->phys_obj = NULL;
4973}
4974
4975int
4976i915_gem_attach_phys_object(struct drm_device *dev,
4977 struct drm_gem_object *obj, int id)
4978{
4979 drm_i915_private_t *dev_priv = dev->dev_private;
4980 struct drm_i915_gem_object *obj_priv;
4981 int ret = 0;
4982 int page_count;
4983 int i;
4984
4985 if (id > I915_MAX_PHYS_OBJECT)
4986 return -EINVAL;
4987
Daniel Vetter23010e42010-03-08 13:35:02 +01004988 obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004989
4990 if (obj_priv->phys_obj) {
4991 if (obj_priv->phys_obj->id == id)
4992 return 0;
4993 i915_gem_detach_phys_object(dev, obj);
4994 }
4995
4996
4997 /* create a new object */
4998 if (!dev_priv->mm.phys_objs[id - 1]) {
4999 ret = i915_gem_init_phys_object(dev, id,
5000 obj->size);
5001 if (ret) {
Linus Torvaldsaeb565d2009-01-26 10:01:53 -08005002 DRM_ERROR("failed to init phys object %d size: %zu\n", id, obj->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10005003 goto out;
5004 }
5005 }
5006
5007 /* bind to the object */
5008 obj_priv->phys_obj = dev_priv->mm.phys_objs[id - 1];
5009 obj_priv->phys_obj->cur_obj = obj;
5010
Chris Wilson4bdadb92010-01-27 13:36:32 +00005011 ret = i915_gem_object_get_pages(obj, 0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10005012 if (ret) {
5013 DRM_ERROR("failed to get page list\n");
5014 goto out;
5015 }
5016
5017 page_count = obj->size / PAGE_SIZE;
5018
5019 for (i = 0; i < page_count; i++) {
Eric Anholt856fa192009-03-19 14:10:50 -07005020 char *src = kmap_atomic(obj_priv->pages[i], KM_USER0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10005021 char *dst = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
5022
5023 memcpy(dst, src, PAGE_SIZE);
5024 kunmap_atomic(src, KM_USER0);
5025 }
5026
Chris Wilsond78b47b2009-06-17 21:52:49 +01005027 i915_gem_object_put_pages(obj);
5028
Dave Airlie71acb5e2008-12-30 20:31:46 +10005029 return 0;
5030out:
5031 return ret;
5032}
5033
5034static int
5035i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
5036 struct drm_i915_gem_pwrite *args,
5037 struct drm_file *file_priv)
5038{
Daniel Vetter23010e42010-03-08 13:35:02 +01005039 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10005040 void *obj_addr;
5041 int ret;
5042 char __user *user_data;
5043
5044 user_data = (char __user *) (uintptr_t) args->data_ptr;
5045 obj_addr = obj_priv->phys_obj->handle->vaddr + args->offset;
5046
Zhao Yakui44d98a62009-10-09 11:39:40 +08005047 DRM_DEBUG_DRIVER("obj_addr %p, %lld\n", obj_addr, args->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10005048 ret = copy_from_user(obj_addr, user_data, args->size);
5049 if (ret)
5050 return -EFAULT;
5051
5052 drm_agp_chipset_flush(dev);
5053 return 0;
5054}
Eric Anholtb9624422009-06-03 07:27:35 +00005055
5056void i915_gem_release(struct drm_device * dev, struct drm_file *file_priv)
5057{
5058 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
5059
5060 /* Clean up our request list when the client is going away, so that
5061 * later retire_requests won't dereference our soon-to-be-gone
5062 * file_priv.
5063 */
5064 mutex_lock(&dev->struct_mutex);
5065 while (!list_empty(&i915_file_priv->mm.request_list))
5066 list_del_init(i915_file_priv->mm.request_list.next);
5067 mutex_unlock(&dev->struct_mutex);
5068}
Chris Wilson31169712009-09-14 16:50:28 +01005069
Chris Wilson31169712009-09-14 16:50:28 +01005070static int
Chris Wilson1637ef42010-04-20 17:10:35 +01005071i915_gpu_is_active(struct drm_device *dev)
5072{
5073 drm_i915_private_t *dev_priv = dev->dev_private;
5074 int lists_empty;
5075
5076 spin_lock(&dev_priv->mm.active_list_lock);
5077 lists_empty = list_empty(&dev_priv->mm.flushing_list) &&
Zou Nan hai852835f2010-05-21 09:08:56 +08005078 list_empty(&dev_priv->render_ring.active_list);
Zou Nan haid1b851f2010-05-21 09:08:57 +08005079 if (HAS_BSD(dev))
5080 lists_empty &= list_empty(&dev_priv->bsd_ring.active_list);
Chris Wilson1637ef42010-04-20 17:10:35 +01005081 spin_unlock(&dev_priv->mm.active_list_lock);
5082
5083 return !lists_empty;
5084}
5085
5086static int
Dave Chinner7f8275d2010-07-19 14:56:17 +10005087i915_gem_shrink(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask)
Chris Wilson31169712009-09-14 16:50:28 +01005088{
5089 drm_i915_private_t *dev_priv, *next_dev;
5090 struct drm_i915_gem_object *obj_priv, *next_obj;
5091 int cnt = 0;
5092 int would_deadlock = 1;
5093
5094 /* "fast-path" to count number of available objects */
5095 if (nr_to_scan == 0) {
5096 spin_lock(&shrink_list_lock);
5097 list_for_each_entry(dev_priv, &shrink_list, mm.shrink_list) {
5098 struct drm_device *dev = dev_priv->dev;
5099
5100 if (mutex_trylock(&dev->struct_mutex)) {
5101 list_for_each_entry(obj_priv,
5102 &dev_priv->mm.inactive_list,
5103 list)
5104 cnt++;
5105 mutex_unlock(&dev->struct_mutex);
5106 }
5107 }
5108 spin_unlock(&shrink_list_lock);
5109
5110 return (cnt / 100) * sysctl_vfs_cache_pressure;
5111 }
5112
5113 spin_lock(&shrink_list_lock);
5114
Chris Wilson1637ef42010-04-20 17:10:35 +01005115rescan:
Chris Wilson31169712009-09-14 16:50:28 +01005116 /* first scan for clean buffers */
5117 list_for_each_entry_safe(dev_priv, next_dev,
5118 &shrink_list, mm.shrink_list) {
5119 struct drm_device *dev = dev_priv->dev;
5120
5121 if (! mutex_trylock(&dev->struct_mutex))
5122 continue;
5123
5124 spin_unlock(&shrink_list_lock);
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01005125 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08005126
Chris Wilson31169712009-09-14 16:50:28 +01005127 list_for_each_entry_safe(obj_priv, next_obj,
5128 &dev_priv->mm.inactive_list,
5129 list) {
5130 if (i915_gem_object_is_purgeable(obj_priv)) {
Daniel Vettera8089e82010-04-09 19:05:09 +00005131 i915_gem_object_unbind(&obj_priv->base);
Chris Wilson31169712009-09-14 16:50:28 +01005132 if (--nr_to_scan <= 0)
5133 break;
5134 }
5135 }
5136
5137 spin_lock(&shrink_list_lock);
5138 mutex_unlock(&dev->struct_mutex);
5139
Chris Wilson963b4832009-09-20 23:03:54 +01005140 would_deadlock = 0;
5141
Chris Wilson31169712009-09-14 16:50:28 +01005142 if (nr_to_scan <= 0)
5143 break;
5144 }
5145
5146 /* second pass, evict/count anything still on the inactive list */
5147 list_for_each_entry_safe(dev_priv, next_dev,
5148 &shrink_list, mm.shrink_list) {
5149 struct drm_device *dev = dev_priv->dev;
5150
5151 if (! mutex_trylock(&dev->struct_mutex))
5152 continue;
5153
5154 spin_unlock(&shrink_list_lock);
5155
5156 list_for_each_entry_safe(obj_priv, next_obj,
5157 &dev_priv->mm.inactive_list,
5158 list) {
5159 if (nr_to_scan > 0) {
Daniel Vettera8089e82010-04-09 19:05:09 +00005160 i915_gem_object_unbind(&obj_priv->base);
Chris Wilson31169712009-09-14 16:50:28 +01005161 nr_to_scan--;
5162 } else
5163 cnt++;
5164 }
5165
5166 spin_lock(&shrink_list_lock);
5167 mutex_unlock(&dev->struct_mutex);
5168
5169 would_deadlock = 0;
5170 }
5171
Chris Wilson1637ef42010-04-20 17:10:35 +01005172 if (nr_to_scan) {
5173 int active = 0;
5174
5175 /*
5176 * We are desperate for pages, so as a last resort, wait
5177 * for the GPU to finish and discard whatever we can.
5178 * This has a dramatic impact to reduce the number of
5179 * OOM-killer events whilst running the GPU aggressively.
5180 */
5181 list_for_each_entry(dev_priv, &shrink_list, mm.shrink_list) {
5182 struct drm_device *dev = dev_priv->dev;
5183
5184 if (!mutex_trylock(&dev->struct_mutex))
5185 continue;
5186
5187 spin_unlock(&shrink_list_lock);
5188
5189 if (i915_gpu_is_active(dev)) {
5190 i915_gpu_idle(dev);
5191 active++;
5192 }
5193
5194 spin_lock(&shrink_list_lock);
5195 mutex_unlock(&dev->struct_mutex);
5196 }
5197
5198 if (active)
5199 goto rescan;
5200 }
5201
Chris Wilson31169712009-09-14 16:50:28 +01005202 spin_unlock(&shrink_list_lock);
5203
5204 if (would_deadlock)
5205 return -1;
5206 else if (cnt > 0)
5207 return (cnt / 100) * sysctl_vfs_cache_pressure;
5208 else
5209 return 0;
5210}
5211
5212static struct shrinker shrinker = {
5213 .shrink = i915_gem_shrink,
5214 .seeks = DEFAULT_SEEKS,
5215};
5216
5217__init void
5218i915_gem_shrinker_init(void)
5219{
5220 register_shrinker(&shrinker);
5221}
5222
5223__exit void
5224i915_gem_shrinker_exit(void)
5225{
5226 unregister_shrinker(&shrinker);
5227}