blob: 77cc6f57166c6fc39976681f4661fc819c5ca024 [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"
Jesse Barnes652c3932009-08-17 13:31:43 -070032#include "intel_drv.h"
Eric Anholt673a3942008-07-30 12:06:12 -070033#include <linux/swap.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080034#include <linux/pci.h>
Eric Anholt673a3942008-07-30 12:06:12 -070035
Eric Anholt28dfe522008-11-13 15:00:55 -080036#define I915_GEM_GPU_DOMAINS (~(I915_GEM_DOMAIN_CPU | I915_GEM_DOMAIN_GTT))
37
Eric Anholte47c68e2008-11-14 13:35:19 -080038static void i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj);
39static void i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj);
40static void i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj);
Eric Anholte47c68e2008-11-14 13:35:19 -080041static int i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj,
42 int write);
43static int i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
44 uint64_t offset,
45 uint64_t size);
46static void i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj);
Eric Anholt673a3942008-07-30 12:06:12 -070047static int i915_gem_object_wait_rendering(struct drm_gem_object *obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -080048static int i915_gem_object_bind_to_gtt(struct drm_gem_object *obj,
49 unsigned alignment);
Jesse Barnesde151cf2008-11-12 10:03:55 -080050static void i915_gem_clear_fence_reg(struct drm_gem_object *obj);
51static int i915_gem_evict_something(struct drm_device *dev);
Dave Airlie71acb5e2008-12-30 20:31:46 +100052static int i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
53 struct drm_i915_gem_pwrite *args,
54 struct drm_file *file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -070055
Jesse Barnes79e53942008-11-07 14:24:08 -080056int i915_gem_do_init(struct drm_device *dev, unsigned long start,
57 unsigned long end)
58{
59 drm_i915_private_t *dev_priv = dev->dev_private;
60
61 if (start >= end ||
62 (start & (PAGE_SIZE - 1)) != 0 ||
63 (end & (PAGE_SIZE - 1)) != 0) {
64 return -EINVAL;
65 }
66
67 drm_mm_init(&dev_priv->mm.gtt_space, start,
68 end - start);
69
70 dev->gtt_total = (uint32_t) (end - start);
71
72 return 0;
73}
Keith Packard6dbe2772008-10-14 21:41:13 -070074
Eric Anholt673a3942008-07-30 12:06:12 -070075int
76i915_gem_init_ioctl(struct drm_device *dev, void *data,
77 struct drm_file *file_priv)
78{
Eric Anholt673a3942008-07-30 12:06:12 -070079 struct drm_i915_gem_init *args = data;
Jesse Barnes79e53942008-11-07 14:24:08 -080080 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -070081
82 mutex_lock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080083 ret = i915_gem_do_init(dev, args->gtt_start, args->gtt_end);
Eric Anholt673a3942008-07-30 12:06:12 -070084 mutex_unlock(&dev->struct_mutex);
85
Jesse Barnes79e53942008-11-07 14:24:08 -080086 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -070087}
88
Eric Anholt5a125c32008-10-22 21:40:13 -070089int
90i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
91 struct drm_file *file_priv)
92{
Eric Anholt5a125c32008-10-22 21:40:13 -070093 struct drm_i915_gem_get_aperture *args = data;
Eric Anholt5a125c32008-10-22 21:40:13 -070094
95 if (!(dev->driver->driver_features & DRIVER_GEM))
96 return -ENODEV;
97
98 args->aper_size = dev->gtt_total;
Keith Packard2678d9d2008-11-20 22:54:54 -080099 args->aper_available_size = (args->aper_size -
100 atomic_read(&dev->pin_memory));
Eric Anholt5a125c32008-10-22 21:40:13 -0700101
102 return 0;
103}
104
Eric Anholt673a3942008-07-30 12:06:12 -0700105
106/**
107 * Creates a new mm object and returns a handle to it.
108 */
109int
110i915_gem_create_ioctl(struct drm_device *dev, void *data,
111 struct drm_file *file_priv)
112{
113 struct drm_i915_gem_create *args = data;
114 struct drm_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300115 int ret;
116 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700117
118 args->size = roundup(args->size, PAGE_SIZE);
119
120 /* Allocate the new object */
121 obj = drm_gem_object_alloc(dev, args->size);
122 if (obj == NULL)
123 return -ENOMEM;
124
125 ret = drm_gem_handle_create(file_priv, obj, &handle);
126 mutex_lock(&dev->struct_mutex);
127 drm_gem_object_handle_unreference(obj);
128 mutex_unlock(&dev->struct_mutex);
129
130 if (ret)
131 return ret;
132
133 args->handle = handle;
134
135 return 0;
136}
137
Eric Anholt40123c12009-03-09 13:42:30 -0700138static inline int
Eric Anholteb014592009-03-10 11:44:52 -0700139fast_shmem_read(struct page **pages,
140 loff_t page_base, int page_offset,
141 char __user *data,
142 int length)
143{
144 char __iomem *vaddr;
Florian Mickler2bc43b52009-04-06 22:55:41 +0200145 int unwritten;
Eric Anholteb014592009-03-10 11:44:52 -0700146
147 vaddr = kmap_atomic(pages[page_base >> PAGE_SHIFT], KM_USER0);
148 if (vaddr == NULL)
149 return -ENOMEM;
Florian Mickler2bc43b52009-04-06 22:55:41 +0200150 unwritten = __copy_to_user_inatomic(data, vaddr + page_offset, length);
Eric Anholteb014592009-03-10 11:44:52 -0700151 kunmap_atomic(vaddr, KM_USER0);
152
Florian Mickler2bc43b52009-04-06 22:55:41 +0200153 if (unwritten)
154 return -EFAULT;
155
156 return 0;
Eric Anholteb014592009-03-10 11:44:52 -0700157}
158
Eric Anholt280b7132009-03-12 16:56:27 -0700159static int i915_gem_object_needs_bit17_swizzle(struct drm_gem_object *obj)
160{
161 drm_i915_private_t *dev_priv = obj->dev->dev_private;
162 struct drm_i915_gem_object *obj_priv = obj->driver_private;
163
164 return dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_9_10_17 &&
165 obj_priv->tiling_mode != I915_TILING_NONE;
166}
167
Eric Anholteb014592009-03-10 11:44:52 -0700168static inline int
Eric Anholt40123c12009-03-09 13:42:30 -0700169slow_shmem_copy(struct page *dst_page,
170 int dst_offset,
171 struct page *src_page,
172 int src_offset,
173 int length)
174{
175 char *dst_vaddr, *src_vaddr;
176
177 dst_vaddr = kmap_atomic(dst_page, KM_USER0);
178 if (dst_vaddr == NULL)
179 return -ENOMEM;
180
181 src_vaddr = kmap_atomic(src_page, KM_USER1);
182 if (src_vaddr == NULL) {
183 kunmap_atomic(dst_vaddr, KM_USER0);
184 return -ENOMEM;
185 }
186
187 memcpy(dst_vaddr + dst_offset, src_vaddr + src_offset, length);
188
189 kunmap_atomic(src_vaddr, KM_USER1);
190 kunmap_atomic(dst_vaddr, KM_USER0);
191
192 return 0;
193}
194
Eric Anholt280b7132009-03-12 16:56:27 -0700195static inline int
196slow_shmem_bit17_copy(struct page *gpu_page,
197 int gpu_offset,
198 struct page *cpu_page,
199 int cpu_offset,
200 int length,
201 int is_read)
202{
203 char *gpu_vaddr, *cpu_vaddr;
204
205 /* Use the unswizzled path if this page isn't affected. */
206 if ((page_to_phys(gpu_page) & (1 << 17)) == 0) {
207 if (is_read)
208 return slow_shmem_copy(cpu_page, cpu_offset,
209 gpu_page, gpu_offset, length);
210 else
211 return slow_shmem_copy(gpu_page, gpu_offset,
212 cpu_page, cpu_offset, length);
213 }
214
215 gpu_vaddr = kmap_atomic(gpu_page, KM_USER0);
216 if (gpu_vaddr == NULL)
217 return -ENOMEM;
218
219 cpu_vaddr = kmap_atomic(cpu_page, KM_USER1);
220 if (cpu_vaddr == NULL) {
221 kunmap_atomic(gpu_vaddr, KM_USER0);
222 return -ENOMEM;
223 }
224
225 /* Copy the data, XORing A6 with A17 (1). The user already knows he's
226 * XORing with the other bits (A9 for Y, A9 and A10 for X)
227 */
228 while (length > 0) {
229 int cacheline_end = ALIGN(gpu_offset + 1, 64);
230 int this_length = min(cacheline_end - gpu_offset, length);
231 int swizzled_gpu_offset = gpu_offset ^ 64;
232
233 if (is_read) {
234 memcpy(cpu_vaddr + cpu_offset,
235 gpu_vaddr + swizzled_gpu_offset,
236 this_length);
237 } else {
238 memcpy(gpu_vaddr + swizzled_gpu_offset,
239 cpu_vaddr + cpu_offset,
240 this_length);
241 }
242 cpu_offset += this_length;
243 gpu_offset += this_length;
244 length -= this_length;
245 }
246
247 kunmap_atomic(cpu_vaddr, KM_USER1);
248 kunmap_atomic(gpu_vaddr, KM_USER0);
249
250 return 0;
251}
252
Eric Anholt673a3942008-07-30 12:06:12 -0700253/**
Eric Anholteb014592009-03-10 11:44:52 -0700254 * This is the fast shmem pread path, which attempts to copy_from_user directly
255 * from the backing pages of the object to the user's address space. On a
256 * fault, it fails so we can fall back to i915_gem_shmem_pwrite_slow().
257 */
258static int
259i915_gem_shmem_pread_fast(struct drm_device *dev, struct drm_gem_object *obj,
260 struct drm_i915_gem_pread *args,
261 struct drm_file *file_priv)
262{
263 struct drm_i915_gem_object *obj_priv = obj->driver_private;
264 ssize_t remain;
265 loff_t offset, page_base;
266 char __user *user_data;
267 int page_offset, page_length;
268 int ret;
269
270 user_data = (char __user *) (uintptr_t) args->data_ptr;
271 remain = args->size;
272
273 mutex_lock(&dev->struct_mutex);
274
275 ret = i915_gem_object_get_pages(obj);
276 if (ret != 0)
277 goto fail_unlock;
278
279 ret = i915_gem_object_set_cpu_read_domain_range(obj, args->offset,
280 args->size);
281 if (ret != 0)
282 goto fail_put_pages;
283
284 obj_priv = obj->driver_private;
285 offset = args->offset;
286
287 while (remain > 0) {
288 /* Operation in this page
289 *
290 * page_base = page offset within aperture
291 * page_offset = offset within page
292 * page_length = bytes to copy for this page
293 */
294 page_base = (offset & ~(PAGE_SIZE-1));
295 page_offset = offset & (PAGE_SIZE-1);
296 page_length = remain;
297 if ((page_offset + remain) > PAGE_SIZE)
298 page_length = PAGE_SIZE - page_offset;
299
300 ret = fast_shmem_read(obj_priv->pages,
301 page_base, page_offset,
302 user_data, page_length);
303 if (ret)
304 goto fail_put_pages;
305
306 remain -= page_length;
307 user_data += page_length;
308 offset += page_length;
309 }
310
311fail_put_pages:
312 i915_gem_object_put_pages(obj);
313fail_unlock:
314 mutex_unlock(&dev->struct_mutex);
315
316 return ret;
317}
318
319/**
320 * This is the fallback shmem pread path, which allocates temporary storage
321 * in kernel space to copy_to_user into outside of the struct_mutex, so we
322 * can copy out of the object's backing pages while holding the struct mutex
323 * and not take page faults.
324 */
325static int
326i915_gem_shmem_pread_slow(struct drm_device *dev, struct drm_gem_object *obj,
327 struct drm_i915_gem_pread *args,
328 struct drm_file *file_priv)
329{
330 struct drm_i915_gem_object *obj_priv = obj->driver_private;
331 struct mm_struct *mm = current->mm;
332 struct page **user_pages;
333 ssize_t remain;
334 loff_t offset, pinned_pages, i;
335 loff_t first_data_page, last_data_page, num_pages;
336 int shmem_page_index, shmem_page_offset;
337 int data_page_index, data_page_offset;
338 int page_length;
339 int ret;
340 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700341 int do_bit17_swizzling;
Eric Anholteb014592009-03-10 11:44:52 -0700342
343 remain = args->size;
344
345 /* Pin the user pages containing the data. We can't fault while
346 * holding the struct mutex, yet we want to hold it while
347 * dereferencing the user data.
348 */
349 first_data_page = data_ptr / PAGE_SIZE;
350 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
351 num_pages = last_data_page - first_data_page + 1;
352
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700353 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
Eric Anholteb014592009-03-10 11:44:52 -0700354 if (user_pages == NULL)
355 return -ENOMEM;
356
357 down_read(&mm->mmap_sem);
358 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
Eric Anholte5e9ecd2009-04-07 16:01:22 -0700359 num_pages, 1, 0, user_pages, NULL);
Eric Anholteb014592009-03-10 11:44:52 -0700360 up_read(&mm->mmap_sem);
361 if (pinned_pages < num_pages) {
362 ret = -EFAULT;
363 goto fail_put_user_pages;
364 }
365
Eric Anholt280b7132009-03-12 16:56:27 -0700366 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
367
Eric Anholteb014592009-03-10 11:44:52 -0700368 mutex_lock(&dev->struct_mutex);
369
370 ret = i915_gem_object_get_pages(obj);
371 if (ret != 0)
372 goto fail_unlock;
373
374 ret = i915_gem_object_set_cpu_read_domain_range(obj, args->offset,
375 args->size);
376 if (ret != 0)
377 goto fail_put_pages;
378
379 obj_priv = obj->driver_private;
380 offset = args->offset;
381
382 while (remain > 0) {
383 /* Operation in this page
384 *
385 * shmem_page_index = page number within shmem file
386 * shmem_page_offset = offset within page in shmem file
387 * data_page_index = page number in get_user_pages return
388 * data_page_offset = offset with data_page_index page.
389 * page_length = bytes to copy for this page
390 */
391 shmem_page_index = offset / PAGE_SIZE;
392 shmem_page_offset = offset & ~PAGE_MASK;
393 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
394 data_page_offset = data_ptr & ~PAGE_MASK;
395
396 page_length = remain;
397 if ((shmem_page_offset + page_length) > PAGE_SIZE)
398 page_length = PAGE_SIZE - shmem_page_offset;
399 if ((data_page_offset + page_length) > PAGE_SIZE)
400 page_length = PAGE_SIZE - data_page_offset;
401
Eric Anholt280b7132009-03-12 16:56:27 -0700402 if (do_bit17_swizzling) {
403 ret = slow_shmem_bit17_copy(obj_priv->pages[shmem_page_index],
404 shmem_page_offset,
405 user_pages[data_page_index],
406 data_page_offset,
407 page_length,
408 1);
409 } else {
410 ret = slow_shmem_copy(user_pages[data_page_index],
411 data_page_offset,
412 obj_priv->pages[shmem_page_index],
413 shmem_page_offset,
414 page_length);
415 }
Eric Anholteb014592009-03-10 11:44:52 -0700416 if (ret)
417 goto fail_put_pages;
418
419 remain -= page_length;
420 data_ptr += page_length;
421 offset += page_length;
422 }
423
424fail_put_pages:
425 i915_gem_object_put_pages(obj);
426fail_unlock:
427 mutex_unlock(&dev->struct_mutex);
428fail_put_user_pages:
429 for (i = 0; i < pinned_pages; i++) {
430 SetPageDirty(user_pages[i]);
431 page_cache_release(user_pages[i]);
432 }
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700433 drm_free_large(user_pages);
Eric Anholteb014592009-03-10 11:44:52 -0700434
435 return ret;
436}
437
Eric Anholt673a3942008-07-30 12:06:12 -0700438/**
439 * Reads data from the object referenced by handle.
440 *
441 * On error, the contents of *data are undefined.
442 */
443int
444i915_gem_pread_ioctl(struct drm_device *dev, void *data,
445 struct drm_file *file_priv)
446{
447 struct drm_i915_gem_pread *args = data;
448 struct drm_gem_object *obj;
449 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -0700450 int ret;
451
452 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
453 if (obj == NULL)
454 return -EBADF;
455 obj_priv = obj->driver_private;
456
457 /* Bounds check source.
458 *
459 * XXX: This could use review for overflow issues...
460 */
461 if (args->offset > obj->size || args->size > obj->size ||
462 args->offset + args->size > obj->size) {
463 drm_gem_object_unreference(obj);
464 return -EINVAL;
465 }
466
Eric Anholt280b7132009-03-12 16:56:27 -0700467 if (i915_gem_object_needs_bit17_swizzle(obj)) {
Eric Anholteb014592009-03-10 11:44:52 -0700468 ret = i915_gem_shmem_pread_slow(dev, obj, args, file_priv);
Eric Anholt280b7132009-03-12 16:56:27 -0700469 } else {
470 ret = i915_gem_shmem_pread_fast(dev, obj, args, file_priv);
471 if (ret != 0)
472 ret = i915_gem_shmem_pread_slow(dev, obj, args,
473 file_priv);
474 }
Eric Anholt673a3942008-07-30 12:06:12 -0700475
476 drm_gem_object_unreference(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700477
Eric Anholteb014592009-03-10 11:44:52 -0700478 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700479}
480
Keith Packard0839ccb2008-10-30 19:38:48 -0700481/* This is the fast write path which cannot handle
482 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700483 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700484
Keith Packard0839ccb2008-10-30 19:38:48 -0700485static inline int
486fast_user_write(struct io_mapping *mapping,
487 loff_t page_base, int page_offset,
488 char __user *user_data,
489 int length)
490{
491 char *vaddr_atomic;
492 unsigned long unwritten;
493
494 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
495 unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
496 user_data, length);
497 io_mapping_unmap_atomic(vaddr_atomic);
498 if (unwritten)
499 return -EFAULT;
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700500 return 0;
Keith Packard0839ccb2008-10-30 19:38:48 -0700501}
502
503/* Here's the write path which can sleep for
504 * page faults
505 */
506
507static inline int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700508slow_kernel_write(struct io_mapping *mapping,
509 loff_t gtt_base, int gtt_offset,
510 struct page *user_page, int user_offset,
511 int length)
Keith Packard0839ccb2008-10-30 19:38:48 -0700512{
Eric Anholt3de09aa2009-03-09 09:42:23 -0700513 char *src_vaddr, *dst_vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -0700514 unsigned long unwritten;
515
Eric Anholt3de09aa2009-03-09 09:42:23 -0700516 dst_vaddr = io_mapping_map_atomic_wc(mapping, gtt_base);
517 src_vaddr = kmap_atomic(user_page, KM_USER1);
518 unwritten = __copy_from_user_inatomic_nocache(dst_vaddr + gtt_offset,
519 src_vaddr + user_offset,
520 length);
521 kunmap_atomic(src_vaddr, KM_USER1);
522 io_mapping_unmap_atomic(dst_vaddr);
Keith Packard0839ccb2008-10-30 19:38:48 -0700523 if (unwritten)
524 return -EFAULT;
525 return 0;
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700526}
527
Eric Anholt40123c12009-03-09 13:42:30 -0700528static inline int
529fast_shmem_write(struct page **pages,
530 loff_t page_base, int page_offset,
531 char __user *data,
532 int length)
533{
534 char __iomem *vaddr;
Dave Airlied0088772009-03-28 20:29:48 -0400535 unsigned long unwritten;
Eric Anholt40123c12009-03-09 13:42:30 -0700536
537 vaddr = kmap_atomic(pages[page_base >> PAGE_SHIFT], KM_USER0);
538 if (vaddr == NULL)
539 return -ENOMEM;
Dave Airlied0088772009-03-28 20:29:48 -0400540 unwritten = __copy_from_user_inatomic(vaddr + page_offset, data, length);
Eric Anholt40123c12009-03-09 13:42:30 -0700541 kunmap_atomic(vaddr, KM_USER0);
542
Dave Airlied0088772009-03-28 20:29:48 -0400543 if (unwritten)
544 return -EFAULT;
Eric Anholt40123c12009-03-09 13:42:30 -0700545 return 0;
546}
547
Eric Anholt3de09aa2009-03-09 09:42:23 -0700548/**
549 * This is the fast pwrite path, where we copy the data directly from the
550 * user into the GTT, uncached.
551 */
Eric Anholt673a3942008-07-30 12:06:12 -0700552static int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700553i915_gem_gtt_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
554 struct drm_i915_gem_pwrite *args,
555 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700556{
557 struct drm_i915_gem_object *obj_priv = obj->driver_private;
Keith Packard0839ccb2008-10-30 19:38:48 -0700558 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700559 ssize_t remain;
Keith Packard0839ccb2008-10-30 19:38:48 -0700560 loff_t offset, page_base;
Eric Anholt673a3942008-07-30 12:06:12 -0700561 char __user *user_data;
Keith Packard0839ccb2008-10-30 19:38:48 -0700562 int page_offset, page_length;
563 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700564
565 user_data = (char __user *) (uintptr_t) args->data_ptr;
566 remain = args->size;
567 if (!access_ok(VERIFY_READ, user_data, remain))
568 return -EFAULT;
569
570
571 mutex_lock(&dev->struct_mutex);
572 ret = i915_gem_object_pin(obj, 0);
573 if (ret) {
574 mutex_unlock(&dev->struct_mutex);
575 return ret;
576 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800577 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
Eric Anholt673a3942008-07-30 12:06:12 -0700578 if (ret)
579 goto fail;
580
581 obj_priv = obj->driver_private;
582 offset = obj_priv->gtt_offset + args->offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700583
584 while (remain > 0) {
585 /* Operation in this page
586 *
Keith Packard0839ccb2008-10-30 19:38:48 -0700587 * page_base = page offset within aperture
588 * page_offset = offset within page
589 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -0700590 */
Keith Packard0839ccb2008-10-30 19:38:48 -0700591 page_base = (offset & ~(PAGE_SIZE-1));
592 page_offset = offset & (PAGE_SIZE-1);
593 page_length = remain;
594 if ((page_offset + remain) > PAGE_SIZE)
595 page_length = PAGE_SIZE - page_offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700596
Keith Packard0839ccb2008-10-30 19:38:48 -0700597 ret = fast_user_write (dev_priv->mm.gtt_mapping, page_base,
598 page_offset, user_data, page_length);
Eric Anholt673a3942008-07-30 12:06:12 -0700599
Keith Packard0839ccb2008-10-30 19:38:48 -0700600 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -0700601 * source page isn't available. Return the error and we'll
602 * retry in the slow path.
Keith Packard0839ccb2008-10-30 19:38:48 -0700603 */
Eric Anholt3de09aa2009-03-09 09:42:23 -0700604 if (ret)
605 goto fail;
Eric Anholt673a3942008-07-30 12:06:12 -0700606
Keith Packard0839ccb2008-10-30 19:38:48 -0700607 remain -= page_length;
608 user_data += page_length;
609 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700610 }
Eric Anholt673a3942008-07-30 12:06:12 -0700611
612fail:
613 i915_gem_object_unpin(obj);
614 mutex_unlock(&dev->struct_mutex);
615
616 return ret;
617}
618
Eric Anholt3de09aa2009-03-09 09:42:23 -0700619/**
620 * This is the fallback GTT pwrite path, which uses get_user_pages to pin
621 * the memory and maps it using kmap_atomic for copying.
622 *
623 * This code resulted in x11perf -rgb10text consuming about 10% more CPU
624 * than using i915_gem_gtt_pwrite_fast on a G45 (32-bit).
625 */
Eric Anholt3043c602008-10-02 12:24:47 -0700626static int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700627i915_gem_gtt_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
628 struct drm_i915_gem_pwrite *args,
629 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700630{
Eric Anholt3de09aa2009-03-09 09:42:23 -0700631 struct drm_i915_gem_object *obj_priv = obj->driver_private;
632 drm_i915_private_t *dev_priv = dev->dev_private;
633 ssize_t remain;
634 loff_t gtt_page_base, offset;
635 loff_t first_data_page, last_data_page, num_pages;
636 loff_t pinned_pages, i;
637 struct page **user_pages;
638 struct mm_struct *mm = current->mm;
639 int gtt_page_offset, data_page_offset, data_page_index, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700640 int ret;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700641 uint64_t data_ptr = args->data_ptr;
642
643 remain = args->size;
644
645 /* Pin the user pages containing the data. We can't fault while
646 * holding the struct mutex, and all of the pwrite implementations
647 * want to hold it while dereferencing the user data.
648 */
649 first_data_page = data_ptr / PAGE_SIZE;
650 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
651 num_pages = last_data_page - first_data_page + 1;
652
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700653 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
Eric Anholt3de09aa2009-03-09 09:42:23 -0700654 if (user_pages == NULL)
655 return -ENOMEM;
656
657 down_read(&mm->mmap_sem);
658 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
659 num_pages, 0, 0, user_pages, NULL);
660 up_read(&mm->mmap_sem);
661 if (pinned_pages < num_pages) {
662 ret = -EFAULT;
663 goto out_unpin_pages;
664 }
665
666 mutex_lock(&dev->struct_mutex);
667 ret = i915_gem_object_pin(obj, 0);
668 if (ret)
669 goto out_unlock;
670
671 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
672 if (ret)
673 goto out_unpin_object;
674
675 obj_priv = obj->driver_private;
676 offset = obj_priv->gtt_offset + args->offset;
677
678 while (remain > 0) {
679 /* Operation in this page
680 *
681 * gtt_page_base = page offset within aperture
682 * gtt_page_offset = offset within page in aperture
683 * data_page_index = page number in get_user_pages return
684 * data_page_offset = offset with data_page_index page.
685 * page_length = bytes to copy for this page
686 */
687 gtt_page_base = offset & PAGE_MASK;
688 gtt_page_offset = offset & ~PAGE_MASK;
689 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
690 data_page_offset = data_ptr & ~PAGE_MASK;
691
692 page_length = remain;
693 if ((gtt_page_offset + page_length) > PAGE_SIZE)
694 page_length = PAGE_SIZE - gtt_page_offset;
695 if ((data_page_offset + page_length) > PAGE_SIZE)
696 page_length = PAGE_SIZE - data_page_offset;
697
698 ret = slow_kernel_write(dev_priv->mm.gtt_mapping,
699 gtt_page_base, gtt_page_offset,
700 user_pages[data_page_index],
701 data_page_offset,
702 page_length);
703
704 /* If we get a fault while copying data, then (presumably) our
705 * source page isn't available. Return the error and we'll
706 * retry in the slow path.
707 */
708 if (ret)
709 goto out_unpin_object;
710
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{
Eric Anholt40123c12009-03-09 13:42:30 -0700737 struct drm_i915_gem_object *obj_priv = obj->driver_private;
738 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
Eric Anholt40123c12009-03-09 13:42:30 -0700749 ret = i915_gem_object_get_pages(obj);
750 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
Eric Anholt40123c12009-03-09 13:42:30 -0700757 obj_priv = obj->driver_private;
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{
805 struct drm_i915_gem_object *obj_priv = obj->driver_private;
806 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
845 ret = i915_gem_object_get_pages(obj);
846 if (ret != 0)
847 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
853 obj_priv = obj->driver_private;
854 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) {
878 ret = slow_shmem_bit17_copy(obj_priv->pages[shmem_page_index],
879 shmem_page_offset,
880 user_pages[data_page_index],
881 data_page_offset,
882 page_length,
883 0);
884 } else {
885 ret = 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);
890 }
Eric Anholt40123c12009-03-09 13:42:30 -0700891 if (ret)
892 goto fail_put_pages;
893
894 remain -= page_length;
895 data_ptr += page_length;
896 offset += page_length;
897 }
898
899fail_put_pages:
900 i915_gem_object_put_pages(obj);
901fail_unlock:
902 mutex_unlock(&dev->struct_mutex);
903fail_put_user_pages:
904 for (i = 0; i < pinned_pages; i++)
905 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700906 drm_free_large(user_pages);
Eric Anholt40123c12009-03-09 13:42:30 -0700907
908 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700909}
910
911/**
912 * Writes data to the object referenced by handle.
913 *
914 * On error, the contents of the buffer that were to be modified are undefined.
915 */
916int
917i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
918 struct drm_file *file_priv)
919{
920 struct drm_i915_gem_pwrite *args = data;
921 struct drm_gem_object *obj;
922 struct drm_i915_gem_object *obj_priv;
923 int ret = 0;
924
925 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
926 if (obj == NULL)
927 return -EBADF;
928 obj_priv = obj->driver_private;
929
930 /* Bounds check destination.
931 *
932 * XXX: This could use review for overflow issues...
933 */
934 if (args->offset > obj->size || args->size > obj->size ||
935 args->offset + args->size > obj->size) {
936 drm_gem_object_unreference(obj);
937 return -EINVAL;
938 }
939
940 /* We can only do the GTT pwrite on untiled buffers, as otherwise
941 * it would end up going through the fenced access, and we'll get
942 * different detiling behavior between reading and writing.
943 * pread/pwrite currently are reading and writing from the CPU
944 * perspective, requiring manual detiling by the client.
945 */
Dave Airlie71acb5e2008-12-30 20:31:46 +1000946 if (obj_priv->phys_obj)
947 ret = i915_gem_phys_pwrite(dev, obj, args, file_priv);
948 else if (obj_priv->tiling_mode == I915_TILING_NONE &&
Eric Anholt3de09aa2009-03-09 09:42:23 -0700949 dev->gtt_total != 0) {
950 ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file_priv);
951 if (ret == -EFAULT) {
952 ret = i915_gem_gtt_pwrite_slow(dev, obj, args,
953 file_priv);
954 }
Eric Anholt280b7132009-03-12 16:56:27 -0700955 } else if (i915_gem_object_needs_bit17_swizzle(obj)) {
956 ret = i915_gem_shmem_pwrite_slow(dev, obj, args, file_priv);
Eric Anholt40123c12009-03-09 13:42:30 -0700957 } else {
958 ret = i915_gem_shmem_pwrite_fast(dev, obj, args, file_priv);
959 if (ret == -EFAULT) {
960 ret = i915_gem_shmem_pwrite_slow(dev, obj, args,
961 file_priv);
962 }
963 }
Eric Anholt673a3942008-07-30 12:06:12 -0700964
965#if WATCH_PWRITE
966 if (ret)
967 DRM_INFO("pwrite failed %d\n", ret);
968#endif
969
970 drm_gem_object_unreference(obj);
971
972 return ret;
973}
974
975/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800976 * Called when user space prepares to use an object with the CPU, either
977 * through the mmap ioctl's mapping or a GTT mapping.
Eric Anholt673a3942008-07-30 12:06:12 -0700978 */
979int
980i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
981 struct drm_file *file_priv)
982{
Eric Anholta09ba7f2009-08-29 12:49:51 -0700983 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700984 struct drm_i915_gem_set_domain *args = data;
985 struct drm_gem_object *obj;
Jesse Barnes652c3932009-08-17 13:31:43 -0700986 struct drm_i915_gem_object *obj_priv;
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800987 uint32_t read_domains = args->read_domains;
988 uint32_t write_domain = args->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -0700989 int ret;
990
991 if (!(dev->driver->driver_features & DRIVER_GEM))
992 return -ENODEV;
993
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800994 /* Only handle setting domains to types used by the CPU. */
Chris Wilson21d509e2009-06-06 09:46:02 +0100995 if (write_domain & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800996 return -EINVAL;
997
Chris Wilson21d509e2009-06-06 09:46:02 +0100998 if (read_domains & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800999 return -EINVAL;
1000
1001 /* Having something in the write domain implies it's in the read
1002 * domain, and only that read domain. Enforce that in the request.
1003 */
1004 if (write_domain != 0 && read_domains != write_domain)
1005 return -EINVAL;
1006
Eric Anholt673a3942008-07-30 12:06:12 -07001007 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1008 if (obj == NULL)
1009 return -EBADF;
Jesse Barnes652c3932009-08-17 13:31:43 -07001010 obj_priv = obj->driver_private;
Eric Anholt673a3942008-07-30 12:06:12 -07001011
1012 mutex_lock(&dev->struct_mutex);
Jesse Barnes652c3932009-08-17 13:31:43 -07001013
1014 intel_mark_busy(dev, obj);
1015
Eric Anholt673a3942008-07-30 12:06:12 -07001016#if WATCH_BUF
Krzysztof Halasacfd43c02009-06-20 00:31:28 +02001017 DRM_INFO("set_domain_ioctl %p(%zd), %08x %08x\n",
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001018 obj, obj->size, read_domains, write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07001019#endif
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001020 if (read_domains & I915_GEM_DOMAIN_GTT) {
1021 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
Eric Anholt02354392008-11-26 13:58:13 -08001022
Eric Anholta09ba7f2009-08-29 12:49:51 -07001023 /* Update the LRU on the fence for the CPU access that's
1024 * about to occur.
1025 */
1026 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
1027 list_move_tail(&obj_priv->fence_list,
1028 &dev_priv->mm.fence_list);
1029 }
1030
Eric Anholt02354392008-11-26 13:58:13 -08001031 /* Silently promote "you're not bound, there was nothing to do"
1032 * to success, since the client was just asking us to
1033 * make sure everything was done.
1034 */
1035 if (ret == -EINVAL)
1036 ret = 0;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001037 } else {
Eric Anholte47c68e2008-11-14 13:35:19 -08001038 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001039 }
1040
Eric Anholt673a3942008-07-30 12:06:12 -07001041 drm_gem_object_unreference(obj);
1042 mutex_unlock(&dev->struct_mutex);
1043 return ret;
1044}
1045
1046/**
1047 * Called when user space has done writes to this buffer
1048 */
1049int
1050i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
1051 struct drm_file *file_priv)
1052{
1053 struct drm_i915_gem_sw_finish *args = data;
1054 struct drm_gem_object *obj;
1055 struct drm_i915_gem_object *obj_priv;
1056 int ret = 0;
1057
1058 if (!(dev->driver->driver_features & DRIVER_GEM))
1059 return -ENODEV;
1060
1061 mutex_lock(&dev->struct_mutex);
1062 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1063 if (obj == NULL) {
1064 mutex_unlock(&dev->struct_mutex);
1065 return -EBADF;
1066 }
1067
1068#if WATCH_BUF
Krzysztof Halasacfd43c02009-06-20 00:31:28 +02001069 DRM_INFO("%s: sw_finish %d (%p %zd)\n",
Eric Anholt673a3942008-07-30 12:06:12 -07001070 __func__, args->handle, obj, obj->size);
1071#endif
1072 obj_priv = obj->driver_private;
1073
1074 /* Pinned buffers may be scanout, so flush the cache */
Eric Anholte47c68e2008-11-14 13:35:19 -08001075 if (obj_priv->pin_count)
1076 i915_gem_object_flush_cpu_write_domain(obj);
1077
Eric Anholt673a3942008-07-30 12:06:12 -07001078 drm_gem_object_unreference(obj);
1079 mutex_unlock(&dev->struct_mutex);
1080 return ret;
1081}
1082
1083/**
1084 * Maps the contents of an object, returning the address it is mapped
1085 * into.
1086 *
1087 * While the mapping holds a reference on the contents of the object, it doesn't
1088 * imply a ref on the object itself.
1089 */
1090int
1091i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
1092 struct drm_file *file_priv)
1093{
1094 struct drm_i915_gem_mmap *args = data;
1095 struct drm_gem_object *obj;
1096 loff_t offset;
1097 unsigned long addr;
1098
1099 if (!(dev->driver->driver_features & DRIVER_GEM))
1100 return -ENODEV;
1101
1102 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1103 if (obj == NULL)
1104 return -EBADF;
1105
1106 offset = args->offset;
1107
1108 down_write(&current->mm->mmap_sem);
1109 addr = do_mmap(obj->filp, 0, args->size,
1110 PROT_READ | PROT_WRITE, MAP_SHARED,
1111 args->offset);
1112 up_write(&current->mm->mmap_sem);
1113 mutex_lock(&dev->struct_mutex);
1114 drm_gem_object_unreference(obj);
1115 mutex_unlock(&dev->struct_mutex);
1116 if (IS_ERR((void *)addr))
1117 return addr;
1118
1119 args->addr_ptr = (uint64_t) addr;
1120
1121 return 0;
1122}
1123
Jesse Barnesde151cf2008-11-12 10:03:55 -08001124/**
1125 * i915_gem_fault - fault a page into the GTT
1126 * vma: VMA in question
1127 * vmf: fault info
1128 *
1129 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1130 * from userspace. The fault handler takes care of binding the object to
1131 * the GTT (if needed), allocating and programming a fence register (again,
1132 * only if needed based on whether the old reg is still valid or the object
1133 * is tiled) and inserting a new PTE into the faulting process.
1134 *
1135 * Note that the faulting process may involve evicting existing objects
1136 * from the GTT and/or fence registers to make room. So performance may
1137 * suffer if the GTT working set is large or there are few fence registers
1138 * left.
1139 */
1140int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1141{
1142 struct drm_gem_object *obj = vma->vm_private_data;
1143 struct drm_device *dev = obj->dev;
1144 struct drm_i915_private *dev_priv = dev->dev_private;
1145 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1146 pgoff_t page_offset;
1147 unsigned long pfn;
1148 int ret = 0;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001149 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001150
1151 /* We don't use vmf->pgoff since that has the fake offset */
1152 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1153 PAGE_SHIFT;
1154
1155 /* Now bind it into the GTT if needed */
1156 mutex_lock(&dev->struct_mutex);
1157 if (!obj_priv->gtt_space) {
Chris Wilsone67b8ce2009-09-14 16:50:26 +01001158 ret = i915_gem_object_bind_to_gtt(obj, 0);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001159 if (ret) {
1160 mutex_unlock(&dev->struct_mutex);
1161 return VM_FAULT_SIGBUS;
1162 }
Chris Wilson4960aac2009-09-14 16:50:25 +01001163 list_add_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
Kristian Høgsberg07f4f3e2009-05-27 14:37:28 -04001164
1165 ret = i915_gem_object_set_to_gtt_domain(obj, write);
1166 if (ret) {
1167 mutex_unlock(&dev->struct_mutex);
1168 return VM_FAULT_SIGBUS;
1169 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001170 }
1171
1172 /* Need a new fence register? */
Eric Anholta09ba7f2009-08-29 12:49:51 -07001173 if (obj_priv->tiling_mode != I915_TILING_NONE) {
Chris Wilson8c4b8c32009-06-17 22:08:52 +01001174 ret = i915_gem_object_get_fence_reg(obj);
Chris Wilson7d8d58b2009-02-04 14:15:10 +00001175 if (ret) {
1176 mutex_unlock(&dev->struct_mutex);
Eric Anholtd9ddcb92009-01-27 10:33:49 -08001177 return VM_FAULT_SIGBUS;
Chris Wilson7d8d58b2009-02-04 14:15:10 +00001178 }
Eric Anholtd9ddcb92009-01-27 10:33:49 -08001179 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001180
1181 pfn = ((dev->agp->base + obj_priv->gtt_offset) >> PAGE_SHIFT) +
1182 page_offset;
1183
1184 /* Finally, remap it using the new GTT offset */
1185 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
1186
1187 mutex_unlock(&dev->struct_mutex);
1188
1189 switch (ret) {
1190 case -ENOMEM:
1191 case -EAGAIN:
1192 return VM_FAULT_OOM;
1193 case -EFAULT:
Jesse Barnes959b8872009-03-20 14:16:33 -07001194 case -EINVAL:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001195 return VM_FAULT_SIGBUS;
1196 default:
1197 return VM_FAULT_NOPAGE;
1198 }
1199}
1200
1201/**
1202 * i915_gem_create_mmap_offset - create a fake mmap offset for an object
1203 * @obj: obj in question
1204 *
1205 * GEM memory mapping works by handing back to userspace a fake mmap offset
1206 * it can use in a subsequent mmap(2) call. The DRM core code then looks
1207 * up the object based on the offset and sets up the various memory mapping
1208 * structures.
1209 *
1210 * This routine allocates and attaches a fake offset for @obj.
1211 */
1212static int
1213i915_gem_create_mmap_offset(struct drm_gem_object *obj)
1214{
1215 struct drm_device *dev = obj->dev;
1216 struct drm_gem_mm *mm = dev->mm_private;
1217 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1218 struct drm_map_list *list;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +11001219 struct drm_local_map *map;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001220 int ret = 0;
1221
1222 /* Set the object up for mmap'ing */
1223 list = &obj->map_list;
Eric Anholt9a298b22009-03-24 12:23:04 -07001224 list->map = kzalloc(sizeof(struct drm_map_list), GFP_KERNEL);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001225 if (!list->map)
1226 return -ENOMEM;
1227
1228 map = list->map;
1229 map->type = _DRM_GEM;
1230 map->size = obj->size;
1231 map->handle = obj;
1232
1233 /* Get a DRM GEM mmap offset allocated... */
1234 list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
1235 obj->size / PAGE_SIZE, 0, 0);
1236 if (!list->file_offset_node) {
1237 DRM_ERROR("failed to allocate offset for bo %d\n", obj->name);
1238 ret = -ENOMEM;
1239 goto out_free_list;
1240 }
1241
1242 list->file_offset_node = drm_mm_get_block(list->file_offset_node,
1243 obj->size / PAGE_SIZE, 0);
1244 if (!list->file_offset_node) {
1245 ret = -ENOMEM;
1246 goto out_free_list;
1247 }
1248
1249 list->hash.key = list->file_offset_node->start;
1250 if (drm_ht_insert_item(&mm->offset_hash, &list->hash)) {
1251 DRM_ERROR("failed to add to map hash\n");
1252 goto out_free_mm;
1253 }
1254
1255 /* By now we should be all set, any drm_mmap request on the offset
1256 * below will get to our mmap & fault handler */
1257 obj_priv->mmap_offset = ((uint64_t) list->hash.key) << PAGE_SHIFT;
1258
1259 return 0;
1260
1261out_free_mm:
1262 drm_mm_put_block(list->file_offset_node);
1263out_free_list:
Eric Anholt9a298b22009-03-24 12:23:04 -07001264 kfree(list->map);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001265
1266 return ret;
1267}
1268
Chris Wilson901782b2009-07-10 08:18:50 +01001269/**
1270 * i915_gem_release_mmap - remove physical page mappings
1271 * @obj: obj in question
1272 *
1273 * Preserve the reservation of the mmaping with the DRM core code, but
1274 * relinquish ownership of the pages back to the system.
1275 *
1276 * It is vital that we remove the page mapping if we have mapped a tiled
1277 * object through the GTT and then lose the fence register due to
1278 * resource pressure. Similarly if the object has been moved out of the
1279 * aperture, than pages mapped into userspace must be revoked. Removing the
1280 * mapping will then trigger a page fault on the next user access, allowing
1281 * fixup by i915_gem_fault().
1282 */
Eric Anholtd05ca302009-07-10 13:02:26 -07001283void
Chris Wilson901782b2009-07-10 08:18:50 +01001284i915_gem_release_mmap(struct drm_gem_object *obj)
1285{
1286 struct drm_device *dev = obj->dev;
1287 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1288
1289 if (dev->dev_mapping)
1290 unmap_mapping_range(dev->dev_mapping,
1291 obj_priv->mmap_offset, obj->size, 1);
1292}
1293
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001294static void
1295i915_gem_free_mmap_offset(struct drm_gem_object *obj)
1296{
1297 struct drm_device *dev = obj->dev;
1298 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1299 struct drm_gem_mm *mm = dev->mm_private;
1300 struct drm_map_list *list;
1301
1302 list = &obj->map_list;
1303 drm_ht_remove_item(&mm->offset_hash, &list->hash);
1304
1305 if (list->file_offset_node) {
1306 drm_mm_put_block(list->file_offset_node);
1307 list->file_offset_node = NULL;
1308 }
1309
1310 if (list->map) {
Eric Anholt9a298b22009-03-24 12:23:04 -07001311 kfree(list->map);
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001312 list->map = NULL;
1313 }
1314
1315 obj_priv->mmap_offset = 0;
1316}
1317
Jesse Barnesde151cf2008-11-12 10:03:55 -08001318/**
1319 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1320 * @obj: object to check
1321 *
1322 * Return the required GTT alignment for an object, taking into account
1323 * potential fence register mapping if needed.
1324 */
1325static uint32_t
1326i915_gem_get_gtt_alignment(struct drm_gem_object *obj)
1327{
1328 struct drm_device *dev = obj->dev;
1329 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1330 int start, i;
1331
1332 /*
1333 * Minimum alignment is 4k (GTT page size), but might be greater
1334 * if a fence register is needed for the object.
1335 */
1336 if (IS_I965G(dev) || obj_priv->tiling_mode == I915_TILING_NONE)
1337 return 4096;
1338
1339 /*
1340 * Previous chips need to be aligned to the size of the smallest
1341 * fence register that can contain the object.
1342 */
1343 if (IS_I9XX(dev))
1344 start = 1024*1024;
1345 else
1346 start = 512*1024;
1347
1348 for (i = start; i < obj->size; i <<= 1)
1349 ;
1350
1351 return i;
1352}
1353
1354/**
1355 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1356 * @dev: DRM device
1357 * @data: GTT mapping ioctl data
1358 * @file_priv: GEM object info
1359 *
1360 * Simply returns the fake offset to userspace so it can mmap it.
1361 * The mmap call will end up in drm_gem_mmap(), which will set things
1362 * up so we can get faults in the handler above.
1363 *
1364 * The fault handler will take care of binding the object into the GTT
1365 * (since it may have been evicted to make room for something), allocating
1366 * a fence register, and mapping the appropriate aperture address into
1367 * userspace.
1368 */
1369int
1370i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
1371 struct drm_file *file_priv)
1372{
1373 struct drm_i915_gem_mmap_gtt *args = data;
1374 struct drm_i915_private *dev_priv = dev->dev_private;
1375 struct drm_gem_object *obj;
1376 struct drm_i915_gem_object *obj_priv;
1377 int ret;
1378
1379 if (!(dev->driver->driver_features & DRIVER_GEM))
1380 return -ENODEV;
1381
1382 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1383 if (obj == NULL)
1384 return -EBADF;
1385
1386 mutex_lock(&dev->struct_mutex);
1387
1388 obj_priv = obj->driver_private;
1389
1390 if (!obj_priv->mmap_offset) {
1391 ret = i915_gem_create_mmap_offset(obj);
Chris Wilson13af1062009-02-11 14:26:31 +00001392 if (ret) {
1393 drm_gem_object_unreference(obj);
1394 mutex_unlock(&dev->struct_mutex);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001395 return ret;
Chris Wilson13af1062009-02-11 14:26:31 +00001396 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001397 }
1398
1399 args->offset = obj_priv->mmap_offset;
1400
Jesse Barnesde151cf2008-11-12 10:03:55 -08001401 /*
1402 * Pull it into the GTT so that we have a page list (makes the
1403 * initial fault faster and any subsequent flushing possible).
1404 */
1405 if (!obj_priv->agp_mem) {
Chris Wilsone67b8ce2009-09-14 16:50:26 +01001406 ret = i915_gem_object_bind_to_gtt(obj, 0);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001407 if (ret) {
1408 drm_gem_object_unreference(obj);
1409 mutex_unlock(&dev->struct_mutex);
1410 return ret;
1411 }
Jesse Barnes14b60392009-05-20 16:47:08 -04001412 list_add_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001413 }
1414
1415 drm_gem_object_unreference(obj);
1416 mutex_unlock(&dev->struct_mutex);
1417
1418 return 0;
1419}
1420
Ben Gamari6911a9b2009-04-02 11:24:54 -07001421void
Eric Anholt856fa192009-03-19 14:10:50 -07001422i915_gem_object_put_pages(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07001423{
1424 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1425 int page_count = obj->size / PAGE_SIZE;
1426 int i;
1427
Eric Anholt856fa192009-03-19 14:10:50 -07001428 BUG_ON(obj_priv->pages_refcount == 0);
1429
1430 if (--obj_priv->pages_refcount != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07001431 return;
1432
Eric Anholt280b7132009-03-12 16:56:27 -07001433 if (obj_priv->tiling_mode != I915_TILING_NONE)
1434 i915_gem_object_save_bit_17_swizzle(obj);
1435
Eric Anholt673a3942008-07-30 12:06:12 -07001436 for (i = 0; i < page_count; i++)
Eric Anholt856fa192009-03-19 14:10:50 -07001437 if (obj_priv->pages[i] != NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -07001438 if (obj_priv->dirty)
Eric Anholt856fa192009-03-19 14:10:50 -07001439 set_page_dirty(obj_priv->pages[i]);
1440 mark_page_accessed(obj_priv->pages[i]);
1441 page_cache_release(obj_priv->pages[i]);
Eric Anholt673a3942008-07-30 12:06:12 -07001442 }
1443 obj_priv->dirty = 0;
1444
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07001445 drm_free_large(obj_priv->pages);
Eric Anholt856fa192009-03-19 14:10:50 -07001446 obj_priv->pages = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001447}
1448
1449static void
Eric Anholtce44b0e2008-11-06 16:00:31 -08001450i915_gem_object_move_to_active(struct drm_gem_object *obj, uint32_t seqno)
Eric Anholt673a3942008-07-30 12:06:12 -07001451{
1452 struct drm_device *dev = obj->dev;
1453 drm_i915_private_t *dev_priv = dev->dev_private;
1454 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1455
1456 /* Add a reference if we're newly entering the active list. */
1457 if (!obj_priv->active) {
1458 drm_gem_object_reference(obj);
1459 obj_priv->active = 1;
1460 }
1461 /* Move from whatever list we were on to the tail of execution. */
Carl Worth5e118f42009-03-20 11:54:25 -07001462 spin_lock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001463 list_move_tail(&obj_priv->list,
1464 &dev_priv->mm.active_list);
Carl Worth5e118f42009-03-20 11:54:25 -07001465 spin_unlock(&dev_priv->mm.active_list_lock);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001466 obj_priv->last_rendering_seqno = seqno;
Eric Anholt673a3942008-07-30 12:06:12 -07001467}
1468
Eric Anholtce44b0e2008-11-06 16:00:31 -08001469static void
1470i915_gem_object_move_to_flushing(struct drm_gem_object *obj)
1471{
1472 struct drm_device *dev = obj->dev;
1473 drm_i915_private_t *dev_priv = dev->dev_private;
1474 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1475
1476 BUG_ON(!obj_priv->active);
1477 list_move_tail(&obj_priv->list, &dev_priv->mm.flushing_list);
1478 obj_priv->last_rendering_seqno = 0;
1479}
Eric Anholt673a3942008-07-30 12:06:12 -07001480
1481static void
1482i915_gem_object_move_to_inactive(struct drm_gem_object *obj)
1483{
1484 struct drm_device *dev = obj->dev;
1485 drm_i915_private_t *dev_priv = dev->dev_private;
1486 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1487
1488 i915_verify_inactive(dev, __FILE__, __LINE__);
1489 if (obj_priv->pin_count != 0)
1490 list_del_init(&obj_priv->list);
1491 else
1492 list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
1493
Eric Anholtce44b0e2008-11-06 16:00:31 -08001494 obj_priv->last_rendering_seqno = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001495 if (obj_priv->active) {
1496 obj_priv->active = 0;
1497 drm_gem_object_unreference(obj);
1498 }
1499 i915_verify_inactive(dev, __FILE__, __LINE__);
1500}
1501
1502/**
1503 * Creates a new sequence number, emitting a write of it to the status page
1504 * plus an interrupt, which will trigger i915_user_interrupt_handler.
1505 *
1506 * Must be called with struct_lock held.
1507 *
1508 * Returned sequence numbers are nonzero on success.
1509 */
1510static uint32_t
Eric Anholtb9624422009-06-03 07:27:35 +00001511i915_add_request(struct drm_device *dev, struct drm_file *file_priv,
1512 uint32_t flush_domains)
Eric Anholt673a3942008-07-30 12:06:12 -07001513{
1514 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtb9624422009-06-03 07:27:35 +00001515 struct drm_i915_file_private *i915_file_priv = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001516 struct drm_i915_gem_request *request;
1517 uint32_t seqno;
1518 int was_empty;
1519 RING_LOCALS;
1520
Eric Anholtb9624422009-06-03 07:27:35 +00001521 if (file_priv != NULL)
1522 i915_file_priv = file_priv->driver_priv;
1523
Eric Anholt9a298b22009-03-24 12:23:04 -07001524 request = kzalloc(sizeof(*request), GFP_KERNEL);
Eric Anholt673a3942008-07-30 12:06:12 -07001525 if (request == NULL)
1526 return 0;
1527
1528 /* Grab the seqno we're going to make this request be, and bump the
1529 * next (skipping 0 so it can be the reserved no-seqno value).
1530 */
1531 seqno = dev_priv->mm.next_gem_seqno;
1532 dev_priv->mm.next_gem_seqno++;
1533 if (dev_priv->mm.next_gem_seqno == 0)
1534 dev_priv->mm.next_gem_seqno++;
1535
1536 BEGIN_LP_RING(4);
1537 OUT_RING(MI_STORE_DWORD_INDEX);
1538 OUT_RING(I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
1539 OUT_RING(seqno);
1540
1541 OUT_RING(MI_USER_INTERRUPT);
1542 ADVANCE_LP_RING();
1543
1544 DRM_DEBUG("%d\n", seqno);
1545
1546 request->seqno = seqno;
1547 request->emitted_jiffies = jiffies;
Eric Anholt673a3942008-07-30 12:06:12 -07001548 was_empty = list_empty(&dev_priv->mm.request_list);
1549 list_add_tail(&request->list, &dev_priv->mm.request_list);
Eric Anholtb9624422009-06-03 07:27:35 +00001550 if (i915_file_priv) {
1551 list_add_tail(&request->client_list,
1552 &i915_file_priv->mm.request_list);
1553 } else {
1554 INIT_LIST_HEAD(&request->client_list);
1555 }
Eric Anholt673a3942008-07-30 12:06:12 -07001556
Eric Anholtce44b0e2008-11-06 16:00:31 -08001557 /* Associate any objects on the flushing list matching the write
1558 * domain we're flushing with our flush.
1559 */
1560 if (flush_domains != 0) {
1561 struct drm_i915_gem_object *obj_priv, *next;
1562
1563 list_for_each_entry_safe(obj_priv, next,
1564 &dev_priv->mm.flushing_list, list) {
1565 struct drm_gem_object *obj = obj_priv->obj;
1566
1567 if ((obj->write_domain & flush_domains) ==
1568 obj->write_domain) {
1569 obj->write_domain = 0;
1570 i915_gem_object_move_to_active(obj, seqno);
1571 }
1572 }
1573
1574 }
1575
Ben Gamarif65d9422009-09-14 17:48:44 -04001576 if (!dev_priv->mm.suspended) {
1577 mod_timer(&dev_priv->hangcheck_timer, jiffies + DRM_I915_HANGCHECK_PERIOD);
1578 if (was_empty)
1579 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
1580 }
Eric Anholt673a3942008-07-30 12:06:12 -07001581 return seqno;
1582}
1583
1584/**
1585 * Command execution barrier
1586 *
1587 * Ensures that all commands in the ring are finished
1588 * before signalling the CPU
1589 */
Eric Anholt3043c602008-10-02 12:24:47 -07001590static uint32_t
Eric Anholt673a3942008-07-30 12:06:12 -07001591i915_retire_commands(struct drm_device *dev)
1592{
1593 drm_i915_private_t *dev_priv = dev->dev_private;
1594 uint32_t cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
1595 uint32_t flush_domains = 0;
1596 RING_LOCALS;
1597
1598 /* The sampler always gets flushed on i965 (sigh) */
1599 if (IS_I965G(dev))
1600 flush_domains |= I915_GEM_DOMAIN_SAMPLER;
1601 BEGIN_LP_RING(2);
1602 OUT_RING(cmd);
1603 OUT_RING(0); /* noop */
1604 ADVANCE_LP_RING();
1605 return flush_domains;
1606}
1607
1608/**
1609 * Moves buffers associated only with the given active seqno from the active
1610 * to inactive list, potentially freeing them.
1611 */
1612static void
1613i915_gem_retire_request(struct drm_device *dev,
1614 struct drm_i915_gem_request *request)
1615{
1616 drm_i915_private_t *dev_priv = dev->dev_private;
1617
1618 /* Move any buffers on the active list that are no longer referenced
1619 * by the ringbuffer to the flushing/inactive lists as appropriate.
1620 */
Carl Worth5e118f42009-03-20 11:54:25 -07001621 spin_lock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001622 while (!list_empty(&dev_priv->mm.active_list)) {
1623 struct drm_gem_object *obj;
1624 struct drm_i915_gem_object *obj_priv;
1625
1626 obj_priv = list_first_entry(&dev_priv->mm.active_list,
1627 struct drm_i915_gem_object,
1628 list);
1629 obj = obj_priv->obj;
1630
1631 /* If the seqno being retired doesn't match the oldest in the
1632 * list, then the oldest in the list must still be newer than
1633 * this seqno.
1634 */
1635 if (obj_priv->last_rendering_seqno != request->seqno)
Carl Worth5e118f42009-03-20 11:54:25 -07001636 goto out;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001637
Eric Anholt673a3942008-07-30 12:06:12 -07001638#if WATCH_LRU
1639 DRM_INFO("%s: retire %d moves to inactive list %p\n",
1640 __func__, request->seqno, obj);
1641#endif
1642
Eric Anholtce44b0e2008-11-06 16:00:31 -08001643 if (obj->write_domain != 0)
1644 i915_gem_object_move_to_flushing(obj);
Shaohua Li68c84342009-04-08 10:58:23 +08001645 else {
1646 /* Take a reference on the object so it won't be
1647 * freed while the spinlock is held. The list
1648 * protection for this spinlock is safe when breaking
1649 * the lock like this since the next thing we do
1650 * is just get the head of the list again.
1651 */
1652 drm_gem_object_reference(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001653 i915_gem_object_move_to_inactive(obj);
Shaohua Li68c84342009-04-08 10:58:23 +08001654 spin_unlock(&dev_priv->mm.active_list_lock);
1655 drm_gem_object_unreference(obj);
1656 spin_lock(&dev_priv->mm.active_list_lock);
1657 }
Eric Anholt673a3942008-07-30 12:06:12 -07001658 }
Carl Worth5e118f42009-03-20 11:54:25 -07001659out:
1660 spin_unlock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001661}
1662
1663/**
1664 * Returns true if seq1 is later than seq2.
1665 */
Ben Gamari22be1722009-09-14 17:48:43 -04001666bool
Eric Anholt673a3942008-07-30 12:06:12 -07001667i915_seqno_passed(uint32_t seq1, uint32_t seq2)
1668{
1669 return (int32_t)(seq1 - seq2) >= 0;
1670}
1671
1672uint32_t
1673i915_get_gem_seqno(struct drm_device *dev)
1674{
1675 drm_i915_private_t *dev_priv = dev->dev_private;
1676
1677 return READ_HWSP(dev_priv, I915_GEM_HWS_INDEX);
1678}
1679
1680/**
1681 * This function clears the request list as sequence numbers are passed.
1682 */
1683void
1684i915_gem_retire_requests(struct drm_device *dev)
1685{
1686 drm_i915_private_t *dev_priv = dev->dev_private;
1687 uint32_t seqno;
1688
Karsten Wiese6c0594a2009-02-23 15:07:57 +01001689 if (!dev_priv->hw_status_page)
1690 return;
1691
Eric Anholt673a3942008-07-30 12:06:12 -07001692 seqno = i915_get_gem_seqno(dev);
1693
1694 while (!list_empty(&dev_priv->mm.request_list)) {
1695 struct drm_i915_gem_request *request;
1696 uint32_t retiring_seqno;
1697
1698 request = list_first_entry(&dev_priv->mm.request_list,
1699 struct drm_i915_gem_request,
1700 list);
1701 retiring_seqno = request->seqno;
1702
1703 if (i915_seqno_passed(seqno, retiring_seqno) ||
Ben Gamariba1234d2009-09-14 17:48:47 -04001704 atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07001705 i915_gem_retire_request(dev, request);
1706
1707 list_del(&request->list);
Eric Anholtb9624422009-06-03 07:27:35 +00001708 list_del(&request->client_list);
Eric Anholt9a298b22009-03-24 12:23:04 -07001709 kfree(request);
Eric Anholt673a3942008-07-30 12:06:12 -07001710 } else
1711 break;
1712 }
1713}
1714
1715void
1716i915_gem_retire_work_handler(struct work_struct *work)
1717{
1718 drm_i915_private_t *dev_priv;
1719 struct drm_device *dev;
1720
1721 dev_priv = container_of(work, drm_i915_private_t,
1722 mm.retire_work.work);
1723 dev = dev_priv->dev;
1724
1725 mutex_lock(&dev->struct_mutex);
1726 i915_gem_retire_requests(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07001727 if (!dev_priv->mm.suspended &&
1728 !list_empty(&dev_priv->mm.request_list))
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001729 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
Eric Anholt673a3942008-07-30 12:06:12 -07001730 mutex_unlock(&dev->struct_mutex);
1731}
1732
1733/**
1734 * Waits for a sequence number to be signaled, and cleans up the
1735 * request and object lists appropriately for that event.
1736 */
Eric Anholt3043c602008-10-02 12:24:47 -07001737static int
Eric Anholt673a3942008-07-30 12:06:12 -07001738i915_wait_request(struct drm_device *dev, uint32_t seqno)
1739{
1740 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001741 u32 ier;
Eric Anholt673a3942008-07-30 12:06:12 -07001742 int ret = 0;
1743
1744 BUG_ON(seqno == 0);
1745
Ben Gamariba1234d2009-09-14 17:48:47 -04001746 if (atomic_read(&dev_priv->mm.wedged))
Ben Gamariffed1d02009-09-14 17:48:41 -04001747 return -EIO;
1748
Eric Anholt673a3942008-07-30 12:06:12 -07001749 if (!i915_seqno_passed(i915_get_gem_seqno(dev), seqno)) {
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001750 if (IS_IGDNG(dev))
1751 ier = I915_READ(DEIER) | I915_READ(GTIER);
1752 else
1753 ier = I915_READ(IER);
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001754 if (!ier) {
1755 DRM_ERROR("something (likely vbetool) disabled "
1756 "interrupts, re-enabling\n");
1757 i915_driver_irq_preinstall(dev);
1758 i915_driver_irq_postinstall(dev);
1759 }
1760
Eric Anholt673a3942008-07-30 12:06:12 -07001761 dev_priv->mm.waiting_gem_seqno = seqno;
1762 i915_user_irq_get(dev);
1763 ret = wait_event_interruptible(dev_priv->irq_queue,
1764 i915_seqno_passed(i915_get_gem_seqno(dev),
1765 seqno) ||
Ben Gamariba1234d2009-09-14 17:48:47 -04001766 atomic_read(&dev_priv->mm.wedged));
Eric Anholt673a3942008-07-30 12:06:12 -07001767 i915_user_irq_put(dev);
1768 dev_priv->mm.waiting_gem_seqno = 0;
1769 }
Ben Gamariba1234d2009-09-14 17:48:47 -04001770 if (atomic_read(&dev_priv->mm.wedged))
Eric Anholt673a3942008-07-30 12:06:12 -07001771 ret = -EIO;
1772
1773 if (ret && ret != -ERESTARTSYS)
1774 DRM_ERROR("%s returns %d (awaiting %d at %d)\n",
1775 __func__, ret, seqno, i915_get_gem_seqno(dev));
1776
1777 /* Directly dispatch request retiring. While we have the work queue
1778 * to handle this, the waiter on a request often wants an associated
1779 * buffer to have made it to the inactive list, and we would need
1780 * a separate wait queue to handle that.
1781 */
1782 if (ret == 0)
1783 i915_gem_retire_requests(dev);
1784
1785 return ret;
1786}
1787
1788static void
1789i915_gem_flush(struct drm_device *dev,
1790 uint32_t invalidate_domains,
1791 uint32_t flush_domains)
1792{
1793 drm_i915_private_t *dev_priv = dev->dev_private;
1794 uint32_t cmd;
1795 RING_LOCALS;
1796
1797#if WATCH_EXEC
1798 DRM_INFO("%s: invalidate %08x flush %08x\n", __func__,
1799 invalidate_domains, flush_domains);
1800#endif
1801
1802 if (flush_domains & I915_GEM_DOMAIN_CPU)
1803 drm_agp_chipset_flush(dev);
1804
Chris Wilson21d509e2009-06-06 09:46:02 +01001805 if ((invalidate_domains | flush_domains) & I915_GEM_GPU_DOMAINS) {
Eric Anholt673a3942008-07-30 12:06:12 -07001806 /*
1807 * read/write caches:
1808 *
1809 * I915_GEM_DOMAIN_RENDER is always invalidated, but is
1810 * only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is
1811 * also flushed at 2d versus 3d pipeline switches.
1812 *
1813 * read-only caches:
1814 *
1815 * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if
1816 * MI_READ_FLUSH is set, and is always flushed on 965.
1817 *
1818 * I915_GEM_DOMAIN_COMMAND may not exist?
1819 *
1820 * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is
1821 * invalidated when MI_EXE_FLUSH is set.
1822 *
1823 * I915_GEM_DOMAIN_VERTEX, which exists on 965, is
1824 * invalidated with every MI_FLUSH.
1825 *
1826 * TLBs:
1827 *
1828 * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND
1829 * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and
1830 * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER
1831 * are flushed at any MI_FLUSH.
1832 */
1833
1834 cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
1835 if ((invalidate_domains|flush_domains) &
1836 I915_GEM_DOMAIN_RENDER)
1837 cmd &= ~MI_NO_WRITE_FLUSH;
1838 if (!IS_I965G(dev)) {
1839 /*
1840 * On the 965, the sampler cache always gets flushed
1841 * and this bit is reserved.
1842 */
1843 if (invalidate_domains & I915_GEM_DOMAIN_SAMPLER)
1844 cmd |= MI_READ_FLUSH;
1845 }
1846 if (invalidate_domains & I915_GEM_DOMAIN_INSTRUCTION)
1847 cmd |= MI_EXE_FLUSH;
1848
1849#if WATCH_EXEC
1850 DRM_INFO("%s: queue flush %08x to ring\n", __func__, cmd);
1851#endif
1852 BEGIN_LP_RING(2);
1853 OUT_RING(cmd);
1854 OUT_RING(0); /* noop */
1855 ADVANCE_LP_RING();
1856 }
1857}
1858
1859/**
1860 * Ensures that all rendering to the object has completed and the object is
1861 * safe to unbind from the GTT or access from the CPU.
1862 */
1863static int
1864i915_gem_object_wait_rendering(struct drm_gem_object *obj)
1865{
1866 struct drm_device *dev = obj->dev;
1867 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1868 int ret;
1869
Eric Anholte47c68e2008-11-14 13:35:19 -08001870 /* This function only exists to support waiting for existing rendering,
1871 * not for emitting required flushes.
Eric Anholt673a3942008-07-30 12:06:12 -07001872 */
Eric Anholte47c68e2008-11-14 13:35:19 -08001873 BUG_ON((obj->write_domain & I915_GEM_GPU_DOMAINS) != 0);
Eric Anholt673a3942008-07-30 12:06:12 -07001874
1875 /* If there is rendering queued on the buffer being evicted, wait for
1876 * it.
1877 */
1878 if (obj_priv->active) {
1879#if WATCH_BUF
1880 DRM_INFO("%s: object %p wait for seqno %08x\n",
1881 __func__, obj, obj_priv->last_rendering_seqno);
1882#endif
1883 ret = i915_wait_request(dev, obj_priv->last_rendering_seqno);
1884 if (ret != 0)
1885 return ret;
1886 }
1887
1888 return 0;
1889}
1890
1891/**
1892 * Unbinds an object from the GTT aperture.
1893 */
Jesse Barnes0f973f22009-01-26 17:10:45 -08001894int
Eric Anholt673a3942008-07-30 12:06:12 -07001895i915_gem_object_unbind(struct drm_gem_object *obj)
1896{
1897 struct drm_device *dev = obj->dev;
1898 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1899 int ret = 0;
1900
1901#if WATCH_BUF
1902 DRM_INFO("%s:%d %p\n", __func__, __LINE__, obj);
1903 DRM_INFO("gtt_space %p\n", obj_priv->gtt_space);
1904#endif
1905 if (obj_priv->gtt_space == NULL)
1906 return 0;
1907
1908 if (obj_priv->pin_count != 0) {
1909 DRM_ERROR("Attempting to unbind pinned buffer\n");
1910 return -EINVAL;
1911 }
1912
Eric Anholt5323fd02009-09-09 11:50:45 -07001913 /* blow away mappings if mapped through GTT */
1914 i915_gem_release_mmap(obj);
1915
1916 if (obj_priv->fence_reg != I915_FENCE_REG_NONE)
1917 i915_gem_clear_fence_reg(obj);
1918
Eric Anholt673a3942008-07-30 12:06:12 -07001919 /* Move the object to the CPU domain to ensure that
1920 * any possible CPU writes while it's not in the GTT
1921 * are flushed when we go to remap it. This will
1922 * also ensure that all pending GPU writes are finished
1923 * before we unbind.
1924 */
Eric Anholte47c68e2008-11-14 13:35:19 -08001925 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07001926 if (ret) {
Eric Anholte47c68e2008-11-14 13:35:19 -08001927 if (ret != -ERESTARTSYS)
1928 DRM_ERROR("set_domain failed: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07001929 return ret;
1930 }
1931
Eric Anholt5323fd02009-09-09 11:50:45 -07001932 BUG_ON(obj_priv->active);
1933
Eric Anholt673a3942008-07-30 12:06:12 -07001934 if (obj_priv->agp_mem != NULL) {
1935 drm_unbind_agp(obj_priv->agp_mem);
1936 drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE);
1937 obj_priv->agp_mem = NULL;
1938 }
1939
Eric Anholt856fa192009-03-19 14:10:50 -07001940 i915_gem_object_put_pages(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001941
1942 if (obj_priv->gtt_space) {
1943 atomic_dec(&dev->gtt_count);
1944 atomic_sub(obj->size, &dev->gtt_memory);
1945
1946 drm_mm_put_block(obj_priv->gtt_space);
1947 obj_priv->gtt_space = NULL;
1948 }
1949
1950 /* Remove ourselves from the LRU list if present. */
1951 if (!list_empty(&obj_priv->list))
1952 list_del_init(&obj_priv->list);
1953
1954 return 0;
1955}
1956
1957static int
1958i915_gem_evict_something(struct drm_device *dev)
1959{
1960 drm_i915_private_t *dev_priv = dev->dev_private;
1961 struct drm_gem_object *obj;
1962 struct drm_i915_gem_object *obj_priv;
1963 int ret = 0;
1964
1965 for (;;) {
1966 /* If there's an inactive buffer available now, grab it
1967 * and be done.
1968 */
1969 if (!list_empty(&dev_priv->mm.inactive_list)) {
1970 obj_priv = list_first_entry(&dev_priv->mm.inactive_list,
1971 struct drm_i915_gem_object,
1972 list);
1973 obj = obj_priv->obj;
1974 BUG_ON(obj_priv->pin_count != 0);
1975#if WATCH_LRU
1976 DRM_INFO("%s: evicting %p\n", __func__, obj);
1977#endif
1978 BUG_ON(obj_priv->active);
1979
1980 /* Wait on the rendering and unbind the buffer. */
1981 ret = i915_gem_object_unbind(obj);
1982 break;
1983 }
1984
1985 /* If we didn't get anything, but the ring is still processing
1986 * things, wait for one of those things to finish and hopefully
1987 * leave us a buffer to evict.
1988 */
1989 if (!list_empty(&dev_priv->mm.request_list)) {
1990 struct drm_i915_gem_request *request;
1991
1992 request = list_first_entry(&dev_priv->mm.request_list,
1993 struct drm_i915_gem_request,
1994 list);
1995
1996 ret = i915_wait_request(dev, request->seqno);
1997 if (ret)
1998 break;
1999
2000 /* if waiting caused an object to become inactive,
2001 * then loop around and wait for it. Otherwise, we
2002 * assume that waiting freed and unbound something,
2003 * so there should now be some space in the GTT
2004 */
2005 if (!list_empty(&dev_priv->mm.inactive_list))
2006 continue;
2007 break;
2008 }
2009
2010 /* If we didn't have anything on the request list but there
2011 * are buffers awaiting a flush, emit one and try again.
2012 * When we wait on it, those buffers waiting for that flush
2013 * will get moved to inactive.
2014 */
2015 if (!list_empty(&dev_priv->mm.flushing_list)) {
2016 obj_priv = list_first_entry(&dev_priv->mm.flushing_list,
2017 struct drm_i915_gem_object,
2018 list);
2019 obj = obj_priv->obj;
2020
2021 i915_gem_flush(dev,
2022 obj->write_domain,
2023 obj->write_domain);
Eric Anholtb9624422009-06-03 07:27:35 +00002024 i915_add_request(dev, NULL, obj->write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07002025
2026 obj = NULL;
2027 continue;
2028 }
2029
2030 DRM_ERROR("inactive empty %d request empty %d "
2031 "flushing empty %d\n",
2032 list_empty(&dev_priv->mm.inactive_list),
2033 list_empty(&dev_priv->mm.request_list),
2034 list_empty(&dev_priv->mm.flushing_list));
2035 /* If we didn't do any of the above, there's nothing to be done
2036 * and we just can't fit it in.
2037 */
Chris Wilson2939e1f2009-06-06 09:46:03 +01002038 return -ENOSPC;
Eric Anholt673a3942008-07-30 12:06:12 -07002039 }
2040 return ret;
2041}
2042
2043static int
Keith Packardac94a962008-11-20 23:30:27 -08002044i915_gem_evict_everything(struct drm_device *dev)
2045{
2046 int ret;
2047
2048 for (;;) {
2049 ret = i915_gem_evict_something(dev);
2050 if (ret != 0)
2051 break;
2052 }
Chris Wilson2939e1f2009-06-06 09:46:03 +01002053 if (ret == -ENOSPC)
Owain Ainsworth15c35332008-12-06 20:42:20 -08002054 return 0;
Keith Packardac94a962008-11-20 23:30:27 -08002055 return ret;
2056}
2057
Ben Gamari6911a9b2009-04-02 11:24:54 -07002058int
Eric Anholt856fa192009-03-19 14:10:50 -07002059i915_gem_object_get_pages(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002060{
2061 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2062 int page_count, i;
2063 struct address_space *mapping;
2064 struct inode *inode;
2065 struct page *page;
2066 int ret;
2067
Eric Anholt856fa192009-03-19 14:10:50 -07002068 if (obj_priv->pages_refcount++ != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07002069 return 0;
2070
2071 /* Get the list of pages out of our struct file. They'll be pinned
2072 * at this point until we release them.
2073 */
2074 page_count = obj->size / PAGE_SIZE;
Eric Anholt856fa192009-03-19 14:10:50 -07002075 BUG_ON(obj_priv->pages != NULL);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07002076 obj_priv->pages = drm_calloc_large(page_count, sizeof(struct page *));
Eric Anholt856fa192009-03-19 14:10:50 -07002077 if (obj_priv->pages == NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -07002078 DRM_ERROR("Faled to allocate page list\n");
Eric Anholt856fa192009-03-19 14:10:50 -07002079 obj_priv->pages_refcount--;
Eric Anholt673a3942008-07-30 12:06:12 -07002080 return -ENOMEM;
2081 }
2082
2083 inode = obj->filp->f_path.dentry->d_inode;
2084 mapping = inode->i_mapping;
2085 for (i = 0; i < page_count; i++) {
2086 page = read_mapping_page(mapping, i, NULL);
2087 if (IS_ERR(page)) {
2088 ret = PTR_ERR(page);
2089 DRM_ERROR("read_mapping_page failed: %d\n", ret);
Eric Anholt856fa192009-03-19 14:10:50 -07002090 i915_gem_object_put_pages(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002091 return ret;
2092 }
Eric Anholt856fa192009-03-19 14:10:50 -07002093 obj_priv->pages[i] = page;
Eric Anholt673a3942008-07-30 12:06:12 -07002094 }
Eric Anholt280b7132009-03-12 16:56:27 -07002095
2096 if (obj_priv->tiling_mode != I915_TILING_NONE)
2097 i915_gem_object_do_bit_17_swizzle(obj);
2098
Eric Anholt673a3942008-07-30 12:06:12 -07002099 return 0;
2100}
2101
Jesse Barnesde151cf2008-11-12 10:03:55 -08002102static void i965_write_fence_reg(struct drm_i915_fence_reg *reg)
2103{
2104 struct drm_gem_object *obj = reg->obj;
2105 struct drm_device *dev = obj->dev;
2106 drm_i915_private_t *dev_priv = dev->dev_private;
2107 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2108 int regnum = obj_priv->fence_reg;
2109 uint64_t val;
2110
2111 val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
2112 0xfffff000) << 32;
2113 val |= obj_priv->gtt_offset & 0xfffff000;
2114 val |= ((obj_priv->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
2115 if (obj_priv->tiling_mode == I915_TILING_Y)
2116 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2117 val |= I965_FENCE_REG_VALID;
2118
2119 I915_WRITE64(FENCE_REG_965_0 + (regnum * 8), val);
2120}
2121
2122static void i915_write_fence_reg(struct drm_i915_fence_reg *reg)
2123{
2124 struct drm_gem_object *obj = reg->obj;
2125 struct drm_device *dev = obj->dev;
2126 drm_i915_private_t *dev_priv = dev->dev_private;
2127 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2128 int regnum = obj_priv->fence_reg;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002129 int tile_width;
Eric Anholtdc529a42009-03-10 22:34:49 -07002130 uint32_t fence_reg, val;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002131 uint32_t pitch_val;
2132
2133 if ((obj_priv->gtt_offset & ~I915_FENCE_START_MASK) ||
2134 (obj_priv->gtt_offset & (obj->size - 1))) {
Linus Torvaldsf06da262009-02-09 08:57:29 -08002135 WARN(1, "%s: object 0x%08x not 1M or size (0x%zx) aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002136 __func__, obj_priv->gtt_offset, obj->size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002137 return;
2138 }
2139
Jesse Barnes0f973f22009-01-26 17:10:45 -08002140 if (obj_priv->tiling_mode == I915_TILING_Y &&
2141 HAS_128_BYTE_Y_TILING(dev))
2142 tile_width = 128;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002143 else
Jesse Barnes0f973f22009-01-26 17:10:45 -08002144 tile_width = 512;
2145
2146 /* Note: pitch better be a power of two tile widths */
2147 pitch_val = obj_priv->stride / tile_width;
2148 pitch_val = ffs(pitch_val) - 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002149
2150 val = obj_priv->gtt_offset;
2151 if (obj_priv->tiling_mode == I915_TILING_Y)
2152 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2153 val |= I915_FENCE_SIZE_BITS(obj->size);
2154 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2155 val |= I830_FENCE_REG_VALID;
2156
Eric Anholtdc529a42009-03-10 22:34:49 -07002157 if (regnum < 8)
2158 fence_reg = FENCE_REG_830_0 + (regnum * 4);
2159 else
2160 fence_reg = FENCE_REG_945_8 + ((regnum - 8) * 4);
2161 I915_WRITE(fence_reg, val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002162}
2163
2164static void i830_write_fence_reg(struct drm_i915_fence_reg *reg)
2165{
2166 struct drm_gem_object *obj = reg->obj;
2167 struct drm_device *dev = obj->dev;
2168 drm_i915_private_t *dev_priv = dev->dev_private;
2169 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2170 int regnum = obj_priv->fence_reg;
2171 uint32_t val;
2172 uint32_t pitch_val;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002173 uint32_t fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002174
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002175 if ((obj_priv->gtt_offset & ~I830_FENCE_START_MASK) ||
Jesse Barnesde151cf2008-11-12 10:03:55 -08002176 (obj_priv->gtt_offset & (obj->size - 1))) {
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002177 WARN(1, "%s: object 0x%08x not 512K or size aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002178 __func__, obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002179 return;
2180 }
2181
Eric Anholte76a16d2009-05-26 17:44:56 -07002182 pitch_val = obj_priv->stride / 128;
2183 pitch_val = ffs(pitch_val) - 1;
2184 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2185
Jesse Barnesde151cf2008-11-12 10:03:55 -08002186 val = obj_priv->gtt_offset;
2187 if (obj_priv->tiling_mode == I915_TILING_Y)
2188 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002189 fence_size_bits = I830_FENCE_SIZE_BITS(obj->size);
2190 WARN_ON(fence_size_bits & ~0x00000f00);
2191 val |= fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002192 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2193 val |= I830_FENCE_REG_VALID;
2194
2195 I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002196}
2197
2198/**
2199 * i915_gem_object_get_fence_reg - set up a fence reg for an object
2200 * @obj: object to map through a fence reg
2201 *
2202 * When mapping objects through the GTT, userspace wants to be able to write
2203 * to them without having to worry about swizzling if the object is tiled.
2204 *
2205 * This function walks the fence regs looking for a free one for @obj,
2206 * stealing one if it can't find any.
2207 *
2208 * It then sets up the reg based on the object's properties: address, pitch
2209 * and tiling format.
2210 */
Chris Wilson8c4b8c32009-06-17 22:08:52 +01002211int
2212i915_gem_object_get_fence_reg(struct drm_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002213{
2214 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002215 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002216 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2217 struct drm_i915_fence_reg *reg = NULL;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002218 struct drm_i915_gem_object *old_obj_priv = NULL;
2219 int i, ret, avail;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002220
Eric Anholta09ba7f2009-08-29 12:49:51 -07002221 /* Just update our place in the LRU if our fence is getting used. */
2222 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
2223 list_move_tail(&obj_priv->fence_list, &dev_priv->mm.fence_list);
2224 return 0;
2225 }
2226
Jesse Barnesde151cf2008-11-12 10:03:55 -08002227 switch (obj_priv->tiling_mode) {
2228 case I915_TILING_NONE:
2229 WARN(1, "allocating a fence for non-tiled object?\n");
2230 break;
2231 case I915_TILING_X:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002232 if (!obj_priv->stride)
2233 return -EINVAL;
2234 WARN((obj_priv->stride & (512 - 1)),
2235 "object 0x%08x is X tiled but has non-512B pitch\n",
2236 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002237 break;
2238 case I915_TILING_Y:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002239 if (!obj_priv->stride)
2240 return -EINVAL;
2241 WARN((obj_priv->stride & (128 - 1)),
2242 "object 0x%08x is Y tiled but has non-128B pitch\n",
2243 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002244 break;
2245 }
2246
2247 /* First try to find a free reg */
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002248 avail = 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002249 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2250 reg = &dev_priv->fence_regs[i];
2251 if (!reg->obj)
2252 break;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002253
2254 old_obj_priv = reg->obj->driver_private;
2255 if (!old_obj_priv->pin_count)
2256 avail++;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002257 }
2258
2259 /* None available, try to steal one or wait for a user to finish */
2260 if (i == dev_priv->num_fence_regs) {
Eric Anholta09ba7f2009-08-29 12:49:51 -07002261 struct drm_gem_object *old_obj = NULL;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002262
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002263 if (avail == 0)
Chris Wilson2939e1f2009-06-06 09:46:03 +01002264 return -ENOSPC;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002265
Eric Anholta09ba7f2009-08-29 12:49:51 -07002266 list_for_each_entry(old_obj_priv, &dev_priv->mm.fence_list,
2267 fence_list) {
2268 old_obj = old_obj_priv->obj;
Chris Wilsond7619c42009-02-11 14:26:47 +00002269
2270 if (old_obj_priv->pin_count)
2271 continue;
2272
Eric Anholta09ba7f2009-08-29 12:49:51 -07002273 /* Take a reference, as otherwise the wait_rendering
2274 * below may cause the object to get freed out from
2275 * under us.
2276 */
2277 drm_gem_object_reference(old_obj);
2278
Chris Wilsond7619c42009-02-11 14:26:47 +00002279 /* i915 uses fences for GPU access to tiled buffers */
2280 if (IS_I965G(dev) || !old_obj_priv->active)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002281 break;
Chris Wilsond7619c42009-02-11 14:26:47 +00002282
Eric Anholta09ba7f2009-08-29 12:49:51 -07002283 /* This brings the object to the head of the LRU if it
2284 * had been written to. The only way this should
2285 * result in us waiting longer than the expected
2286 * optimal amount of time is if there was a
2287 * fence-using buffer later that was read-only.
2288 */
2289 i915_gem_object_flush_gpu_write_domain(old_obj);
2290 ret = i915_gem_object_wait_rendering(old_obj);
Chris Wilson58c2fb62009-09-01 12:02:39 +01002291 if (ret != 0) {
2292 drm_gem_object_unreference(old_obj);
Chris Wilsond7619c42009-02-11 14:26:47 +00002293 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002294 }
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002295
Eric Anholta09ba7f2009-08-29 12:49:51 -07002296 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002297 }
2298
2299 /*
2300 * Zap this virtual mapping so we can set up a fence again
2301 * for this object next time we need it.
2302 */
Chris Wilson58c2fb62009-09-01 12:02:39 +01002303 i915_gem_release_mmap(old_obj);
2304
Eric Anholta09ba7f2009-08-29 12:49:51 -07002305 i = old_obj_priv->fence_reg;
Chris Wilson58c2fb62009-09-01 12:02:39 +01002306 reg = &dev_priv->fence_regs[i];
2307
Jesse Barnesde151cf2008-11-12 10:03:55 -08002308 old_obj_priv->fence_reg = I915_FENCE_REG_NONE;
Eric Anholta09ba7f2009-08-29 12:49:51 -07002309 list_del_init(&old_obj_priv->fence_list);
Chris Wilson58c2fb62009-09-01 12:02:39 +01002310
Eric Anholta09ba7f2009-08-29 12:49:51 -07002311 drm_gem_object_unreference(old_obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002312 }
2313
2314 obj_priv->fence_reg = i;
Eric Anholta09ba7f2009-08-29 12:49:51 -07002315 list_add_tail(&obj_priv->fence_list, &dev_priv->mm.fence_list);
2316
Jesse Barnesde151cf2008-11-12 10:03:55 -08002317 reg->obj = obj;
2318
2319 if (IS_I965G(dev))
2320 i965_write_fence_reg(reg);
2321 else if (IS_I9XX(dev))
2322 i915_write_fence_reg(reg);
2323 else
2324 i830_write_fence_reg(reg);
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002325
2326 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002327}
2328
2329/**
2330 * i915_gem_clear_fence_reg - clear out fence register info
2331 * @obj: object to clear
2332 *
2333 * Zeroes out the fence register itself and clears out the associated
2334 * data structures in dev_priv and obj_priv.
2335 */
2336static void
2337i915_gem_clear_fence_reg(struct drm_gem_object *obj)
2338{
2339 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002340 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002341 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2342
2343 if (IS_I965G(dev))
2344 I915_WRITE64(FENCE_REG_965_0 + (obj_priv->fence_reg * 8), 0);
Eric Anholtdc529a42009-03-10 22:34:49 -07002345 else {
2346 uint32_t fence_reg;
2347
2348 if (obj_priv->fence_reg < 8)
2349 fence_reg = FENCE_REG_830_0 + obj_priv->fence_reg * 4;
2350 else
2351 fence_reg = FENCE_REG_945_8 + (obj_priv->fence_reg -
2352 8) * 4;
2353
2354 I915_WRITE(fence_reg, 0);
2355 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002356
2357 dev_priv->fence_regs[obj_priv->fence_reg].obj = NULL;
2358 obj_priv->fence_reg = I915_FENCE_REG_NONE;
Eric Anholta09ba7f2009-08-29 12:49:51 -07002359 list_del_init(&obj_priv->fence_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002360}
2361
Eric Anholt673a3942008-07-30 12:06:12 -07002362/**
Chris Wilson52dc7d32009-06-06 09:46:01 +01002363 * i915_gem_object_put_fence_reg - waits on outstanding fenced access
2364 * to the buffer to finish, and then resets the fence register.
2365 * @obj: tiled object holding a fence register.
2366 *
2367 * Zeroes out the fence register itself and clears out the associated
2368 * data structures in dev_priv and obj_priv.
2369 */
2370int
2371i915_gem_object_put_fence_reg(struct drm_gem_object *obj)
2372{
2373 struct drm_device *dev = obj->dev;
2374 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2375
2376 if (obj_priv->fence_reg == I915_FENCE_REG_NONE)
2377 return 0;
2378
2379 /* On the i915, GPU access to tiled buffers is via a fence,
2380 * therefore we must wait for any outstanding access to complete
2381 * before clearing the fence.
2382 */
2383 if (!IS_I965G(dev)) {
2384 int ret;
2385
2386 i915_gem_object_flush_gpu_write_domain(obj);
2387 i915_gem_object_flush_gtt_write_domain(obj);
2388 ret = i915_gem_object_wait_rendering(obj);
2389 if (ret != 0)
2390 return ret;
2391 }
2392
2393 i915_gem_clear_fence_reg (obj);
2394
2395 return 0;
2396}
2397
2398/**
Eric Anholt673a3942008-07-30 12:06:12 -07002399 * Finds free space in the GTT aperture and binds the object there.
2400 */
2401static int
2402i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment)
2403{
2404 struct drm_device *dev = obj->dev;
2405 drm_i915_private_t *dev_priv = dev->dev_private;
2406 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2407 struct drm_mm_node *free_space;
2408 int page_count, ret;
2409
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08002410 if (dev_priv->mm.suspended)
2411 return -EBUSY;
Eric Anholt673a3942008-07-30 12:06:12 -07002412 if (alignment == 0)
Jesse Barnes0f973f22009-01-26 17:10:45 -08002413 alignment = i915_gem_get_gtt_alignment(obj);
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002414 if (alignment & (i915_gem_get_gtt_alignment(obj) - 1)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002415 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2416 return -EINVAL;
2417 }
2418
2419 search_free:
2420 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
2421 obj->size, alignment, 0);
2422 if (free_space != NULL) {
2423 obj_priv->gtt_space = drm_mm_get_block(free_space, obj->size,
2424 alignment);
2425 if (obj_priv->gtt_space != NULL) {
2426 obj_priv->gtt_space->private = obj;
2427 obj_priv->gtt_offset = obj_priv->gtt_space->start;
2428 }
2429 }
2430 if (obj_priv->gtt_space == NULL) {
Carl Worth5e118f42009-03-20 11:54:25 -07002431 bool lists_empty;
2432
Eric Anholt673a3942008-07-30 12:06:12 -07002433 /* If the gtt is empty and we're still having trouble
2434 * fitting our object in, we're out of memory.
2435 */
2436#if WATCH_LRU
2437 DRM_INFO("%s: GTT full, evicting something\n", __func__);
2438#endif
Carl Worth5e118f42009-03-20 11:54:25 -07002439 spin_lock(&dev_priv->mm.active_list_lock);
2440 lists_empty = (list_empty(&dev_priv->mm.inactive_list) &&
2441 list_empty(&dev_priv->mm.flushing_list) &&
2442 list_empty(&dev_priv->mm.active_list));
2443 spin_unlock(&dev_priv->mm.active_list_lock);
2444 if (lists_empty) {
Eric Anholt673a3942008-07-30 12:06:12 -07002445 DRM_ERROR("GTT full, but LRU list empty\n");
Chris Wilson2939e1f2009-06-06 09:46:03 +01002446 return -ENOSPC;
Eric Anholt673a3942008-07-30 12:06:12 -07002447 }
2448
2449 ret = i915_gem_evict_something(dev);
2450 if (ret != 0) {
Keith Packardac94a962008-11-20 23:30:27 -08002451 if (ret != -ERESTARTSYS)
2452 DRM_ERROR("Failed to evict a buffer %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07002453 return ret;
2454 }
2455 goto search_free;
2456 }
2457
2458#if WATCH_BUF
Krzysztof Halasacfd43c02009-06-20 00:31:28 +02002459 DRM_INFO("Binding object of size %zd at 0x%08x\n",
Eric Anholt673a3942008-07-30 12:06:12 -07002460 obj->size, obj_priv->gtt_offset);
2461#endif
Eric Anholt856fa192009-03-19 14:10:50 -07002462 ret = i915_gem_object_get_pages(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002463 if (ret) {
2464 drm_mm_put_block(obj_priv->gtt_space);
2465 obj_priv->gtt_space = NULL;
2466 return ret;
2467 }
2468
2469 page_count = obj->size / PAGE_SIZE;
2470 /* Create an AGP memory structure pointing at our pages, and bind it
2471 * into the GTT.
2472 */
2473 obj_priv->agp_mem = drm_agp_bind_pages(dev,
Eric Anholt856fa192009-03-19 14:10:50 -07002474 obj_priv->pages,
Eric Anholt673a3942008-07-30 12:06:12 -07002475 page_count,
Keith Packardba1eb1d2008-10-14 19:55:10 -07002476 obj_priv->gtt_offset,
2477 obj_priv->agp_type);
Eric Anholt673a3942008-07-30 12:06:12 -07002478 if (obj_priv->agp_mem == NULL) {
Eric Anholt856fa192009-03-19 14:10:50 -07002479 i915_gem_object_put_pages(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002480 drm_mm_put_block(obj_priv->gtt_space);
2481 obj_priv->gtt_space = NULL;
2482 return -ENOMEM;
2483 }
2484 atomic_inc(&dev->gtt_count);
2485 atomic_add(obj->size, &dev->gtt_memory);
2486
2487 /* Assert that the object is not currently in any GPU domain. As it
2488 * wasn't in the GTT, there shouldn't be any way it could have been in
2489 * a GPU cache
2490 */
Chris Wilson21d509e2009-06-06 09:46:02 +01002491 BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS);
2492 BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS);
Eric Anholt673a3942008-07-30 12:06:12 -07002493
2494 return 0;
2495}
2496
2497void
2498i915_gem_clflush_object(struct drm_gem_object *obj)
2499{
2500 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2501
2502 /* If we don't have a page list set up, then we're not pinned
2503 * to GPU, and we can ignore the cache flush because it'll happen
2504 * again at bind time.
2505 */
Eric Anholt856fa192009-03-19 14:10:50 -07002506 if (obj_priv->pages == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002507 return;
2508
Eric Anholt856fa192009-03-19 14:10:50 -07002509 drm_clflush_pages(obj_priv->pages, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002510}
2511
Eric Anholte47c68e2008-11-14 13:35:19 -08002512/** Flushes any GPU write domain for the object if it's dirty. */
2513static void
2514i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj)
2515{
2516 struct drm_device *dev = obj->dev;
2517 uint32_t seqno;
2518
2519 if ((obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
2520 return;
2521
2522 /* Queue the GPU write cache flushing we need. */
2523 i915_gem_flush(dev, 0, obj->write_domain);
Eric Anholtb9624422009-06-03 07:27:35 +00002524 seqno = i915_add_request(dev, NULL, obj->write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002525 obj->write_domain = 0;
2526 i915_gem_object_move_to_active(obj, seqno);
2527}
2528
2529/** Flushes the GTT write domain for the object if it's dirty. */
2530static void
2531i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj)
2532{
2533 if (obj->write_domain != I915_GEM_DOMAIN_GTT)
2534 return;
2535
2536 /* No actual flushing is required for the GTT write domain. Writes
2537 * to it immediately go to main memory as far as we know, so there's
2538 * no chipset flush. It also doesn't land in render cache.
2539 */
2540 obj->write_domain = 0;
2541}
2542
2543/** Flushes the CPU write domain for the object if it's dirty. */
2544static void
2545i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj)
2546{
2547 struct drm_device *dev = obj->dev;
2548
2549 if (obj->write_domain != I915_GEM_DOMAIN_CPU)
2550 return;
2551
2552 i915_gem_clflush_object(obj);
2553 drm_agp_chipset_flush(dev);
2554 obj->write_domain = 0;
2555}
2556
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002557/**
2558 * Moves a single object to the GTT read, and possibly write domain.
2559 *
2560 * This function returns when the move is complete, including waiting on
2561 * flushes to occur.
2562 */
Jesse Barnes79e53942008-11-07 14:24:08 -08002563int
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002564i915_gem_object_set_to_gtt_domain(struct drm_gem_object *obj, int write)
2565{
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002566 struct drm_i915_gem_object *obj_priv = obj->driver_private;
Eric Anholte47c68e2008-11-14 13:35:19 -08002567 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002568
Eric Anholt02354392008-11-26 13:58:13 -08002569 /* Not valid to be called on unbound objects. */
2570 if (obj_priv->gtt_space == NULL)
2571 return -EINVAL;
2572
Eric Anholte47c68e2008-11-14 13:35:19 -08002573 i915_gem_object_flush_gpu_write_domain(obj);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002574 /* Wait on any GPU rendering and flushing to occur. */
Eric Anholte47c68e2008-11-14 13:35:19 -08002575 ret = i915_gem_object_wait_rendering(obj);
2576 if (ret != 0)
2577 return ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002578
2579 /* If we're writing through the GTT domain, then CPU and GPU caches
2580 * will need to be invalidated at next use.
2581 */
2582 if (write)
Eric Anholte47c68e2008-11-14 13:35:19 -08002583 obj->read_domains &= I915_GEM_DOMAIN_GTT;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002584
Eric Anholte47c68e2008-11-14 13:35:19 -08002585 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002586
2587 /* It should now be out of any other write domains, and we can update
2588 * the domain values for our changes.
2589 */
2590 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2591 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002592 if (write) {
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002593 obj->write_domain = I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002594 obj_priv->dirty = 1;
2595 }
2596
2597 return 0;
2598}
2599
2600/**
2601 * Moves a single object to the CPU read, and possibly write domain.
2602 *
2603 * This function returns when the move is complete, including waiting on
2604 * flushes to occur.
2605 */
2606static int
2607i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj, int write)
2608{
Eric Anholte47c68e2008-11-14 13:35:19 -08002609 int ret;
2610
2611 i915_gem_object_flush_gpu_write_domain(obj);
2612 /* Wait on any GPU rendering and flushing to occur. */
2613 ret = i915_gem_object_wait_rendering(obj);
2614 if (ret != 0)
2615 return ret;
2616
2617 i915_gem_object_flush_gtt_write_domain(obj);
2618
2619 /* If we have a partially-valid cache of the object in the CPU,
2620 * finish invalidating it and free the per-page flags.
2621 */
2622 i915_gem_object_set_to_full_cpu_read_domain(obj);
2623
2624 /* Flush the CPU cache if it's still invalid. */
2625 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
2626 i915_gem_clflush_object(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08002627
2628 obj->read_domains |= I915_GEM_DOMAIN_CPU;
2629 }
2630
2631 /* It should now be out of any other write domains, and we can update
2632 * the domain values for our changes.
2633 */
2634 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
2635
2636 /* If we're writing through the CPU, then the GPU read domains will
2637 * need to be invalidated at next use.
2638 */
2639 if (write) {
2640 obj->read_domains &= I915_GEM_DOMAIN_CPU;
2641 obj->write_domain = I915_GEM_DOMAIN_CPU;
2642 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002643
2644 return 0;
2645}
2646
Eric Anholt673a3942008-07-30 12:06:12 -07002647/*
2648 * Set the next domain for the specified object. This
2649 * may not actually perform the necessary flushing/invaliding though,
2650 * as that may want to be batched with other set_domain operations
2651 *
2652 * This is (we hope) the only really tricky part of gem. The goal
2653 * is fairly simple -- track which caches hold bits of the object
2654 * and make sure they remain coherent. A few concrete examples may
2655 * help to explain how it works. For shorthand, we use the notation
2656 * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
2657 * a pair of read and write domain masks.
2658 *
2659 * Case 1: the batch buffer
2660 *
2661 * 1. Allocated
2662 * 2. Written by CPU
2663 * 3. Mapped to GTT
2664 * 4. Read by GPU
2665 * 5. Unmapped from GTT
2666 * 6. Freed
2667 *
2668 * Let's take these a step at a time
2669 *
2670 * 1. Allocated
2671 * Pages allocated from the kernel may still have
2672 * cache contents, so we set them to (CPU, CPU) always.
2673 * 2. Written by CPU (using pwrite)
2674 * The pwrite function calls set_domain (CPU, CPU) and
2675 * this function does nothing (as nothing changes)
2676 * 3. Mapped by GTT
2677 * This function asserts that the object is not
2678 * currently in any GPU-based read or write domains
2679 * 4. Read by GPU
2680 * i915_gem_execbuffer calls set_domain (COMMAND, 0).
2681 * As write_domain is zero, this function adds in the
2682 * current read domains (CPU+COMMAND, 0).
2683 * flush_domains is set to CPU.
2684 * invalidate_domains is set to COMMAND
2685 * clflush is run to get data out of the CPU caches
2686 * then i915_dev_set_domain calls i915_gem_flush to
2687 * emit an MI_FLUSH and drm_agp_chipset_flush
2688 * 5. Unmapped from GTT
2689 * i915_gem_object_unbind calls set_domain (CPU, CPU)
2690 * flush_domains and invalidate_domains end up both zero
2691 * so no flushing/invalidating happens
2692 * 6. Freed
2693 * yay, done
2694 *
2695 * Case 2: The shared render buffer
2696 *
2697 * 1. Allocated
2698 * 2. Mapped to GTT
2699 * 3. Read/written by GPU
2700 * 4. set_domain to (CPU,CPU)
2701 * 5. Read/written by CPU
2702 * 6. Read/written by GPU
2703 *
2704 * 1. Allocated
2705 * Same as last example, (CPU, CPU)
2706 * 2. Mapped to GTT
2707 * Nothing changes (assertions find that it is not in the GPU)
2708 * 3. Read/written by GPU
2709 * execbuffer calls set_domain (RENDER, RENDER)
2710 * flush_domains gets CPU
2711 * invalidate_domains gets GPU
2712 * clflush (obj)
2713 * MI_FLUSH and drm_agp_chipset_flush
2714 * 4. set_domain (CPU, CPU)
2715 * flush_domains gets GPU
2716 * invalidate_domains gets CPU
2717 * wait_rendering (obj) to make sure all drawing is complete.
2718 * This will include an MI_FLUSH to get the data from GPU
2719 * to memory
2720 * clflush (obj) to invalidate the CPU cache
2721 * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
2722 * 5. Read/written by CPU
2723 * cache lines are loaded and dirtied
2724 * 6. Read written by GPU
2725 * Same as last GPU access
2726 *
2727 * Case 3: The constant buffer
2728 *
2729 * 1. Allocated
2730 * 2. Written by CPU
2731 * 3. Read by GPU
2732 * 4. Updated (written) by CPU again
2733 * 5. Read by GPU
2734 *
2735 * 1. Allocated
2736 * (CPU, CPU)
2737 * 2. Written by CPU
2738 * (CPU, CPU)
2739 * 3. Read by GPU
2740 * (CPU+RENDER, 0)
2741 * flush_domains = CPU
2742 * invalidate_domains = RENDER
2743 * clflush (obj)
2744 * MI_FLUSH
2745 * drm_agp_chipset_flush
2746 * 4. Updated (written) by CPU again
2747 * (CPU, CPU)
2748 * flush_domains = 0 (no previous write domain)
2749 * invalidate_domains = 0 (no new read domains)
2750 * 5. Read by GPU
2751 * (CPU+RENDER, 0)
2752 * flush_domains = CPU
2753 * invalidate_domains = RENDER
2754 * clflush (obj)
2755 * MI_FLUSH
2756 * drm_agp_chipset_flush
2757 */
Keith Packardc0d90822008-11-20 23:11:08 -08002758static void
Eric Anholt8b0e3782009-02-19 14:40:50 -08002759i915_gem_object_set_to_gpu_domain(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002760{
2761 struct drm_device *dev = obj->dev;
2762 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2763 uint32_t invalidate_domains = 0;
2764 uint32_t flush_domains = 0;
Eric Anholte47c68e2008-11-14 13:35:19 -08002765
Eric Anholt8b0e3782009-02-19 14:40:50 -08002766 BUG_ON(obj->pending_read_domains & I915_GEM_DOMAIN_CPU);
2767 BUG_ON(obj->pending_write_domain == I915_GEM_DOMAIN_CPU);
Eric Anholt673a3942008-07-30 12:06:12 -07002768
Jesse Barnes652c3932009-08-17 13:31:43 -07002769 intel_mark_busy(dev, obj);
2770
Eric Anholt673a3942008-07-30 12:06:12 -07002771#if WATCH_BUF
2772 DRM_INFO("%s: object %p read %08x -> %08x write %08x -> %08x\n",
2773 __func__, obj,
Eric Anholt8b0e3782009-02-19 14:40:50 -08002774 obj->read_domains, obj->pending_read_domains,
2775 obj->write_domain, obj->pending_write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07002776#endif
2777 /*
2778 * If the object isn't moving to a new write domain,
2779 * let the object stay in multiple read domains
2780 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08002781 if (obj->pending_write_domain == 0)
2782 obj->pending_read_domains |= obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07002783 else
2784 obj_priv->dirty = 1;
2785
2786 /*
2787 * Flush the current write domain if
2788 * the new read domains don't match. Invalidate
2789 * any read domains which differ from the old
2790 * write domain
2791 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08002792 if (obj->write_domain &&
2793 obj->write_domain != obj->pending_read_domains) {
Eric Anholt673a3942008-07-30 12:06:12 -07002794 flush_domains |= obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08002795 invalidate_domains |=
2796 obj->pending_read_domains & ~obj->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07002797 }
2798 /*
2799 * Invalidate any read caches which may have
2800 * stale data. That is, any new read domains.
2801 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08002802 invalidate_domains |= obj->pending_read_domains & ~obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07002803 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU) {
2804#if WATCH_BUF
2805 DRM_INFO("%s: CPU domain flush %08x invalidate %08x\n",
2806 __func__, flush_domains, invalidate_domains);
2807#endif
Eric Anholt673a3942008-07-30 12:06:12 -07002808 i915_gem_clflush_object(obj);
2809 }
2810
Eric Anholtefbeed92009-02-19 14:54:51 -08002811 /* The actual obj->write_domain will be updated with
2812 * pending_write_domain after we emit the accumulated flush for all
2813 * of our domain changes in execbuffers (which clears objects'
2814 * write_domains). So if we have a current write domain that we
2815 * aren't changing, set pending_write_domain to that.
2816 */
2817 if (flush_domains == 0 && obj->pending_write_domain == 0)
2818 obj->pending_write_domain = obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08002819 obj->read_domains = obj->pending_read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07002820
2821 dev->invalidate_domains |= invalidate_domains;
2822 dev->flush_domains |= flush_domains;
2823#if WATCH_BUF
2824 DRM_INFO("%s: read %08x write %08x invalidate %08x flush %08x\n",
2825 __func__,
2826 obj->read_domains, obj->write_domain,
2827 dev->invalidate_domains, dev->flush_domains);
2828#endif
Eric Anholt673a3942008-07-30 12:06:12 -07002829}
2830
2831/**
Eric Anholte47c68e2008-11-14 13:35:19 -08002832 * Moves the object from a partially CPU read to a full one.
Eric Anholt673a3942008-07-30 12:06:12 -07002833 *
Eric Anholte47c68e2008-11-14 13:35:19 -08002834 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
2835 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
2836 */
2837static void
2838i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj)
2839{
Eric Anholte47c68e2008-11-14 13:35:19 -08002840 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2841
2842 if (!obj_priv->page_cpu_valid)
2843 return;
2844
2845 /* If we're partially in the CPU read domain, finish moving it in.
2846 */
2847 if (obj->read_domains & I915_GEM_DOMAIN_CPU) {
2848 int i;
2849
2850 for (i = 0; i <= (obj->size - 1) / PAGE_SIZE; i++) {
2851 if (obj_priv->page_cpu_valid[i])
2852 continue;
Eric Anholt856fa192009-03-19 14:10:50 -07002853 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholte47c68e2008-11-14 13:35:19 -08002854 }
Eric Anholte47c68e2008-11-14 13:35:19 -08002855 }
2856
2857 /* Free the page_cpu_valid mappings which are now stale, whether
2858 * or not we've got I915_GEM_DOMAIN_CPU.
2859 */
Eric Anholt9a298b22009-03-24 12:23:04 -07002860 kfree(obj_priv->page_cpu_valid);
Eric Anholte47c68e2008-11-14 13:35:19 -08002861 obj_priv->page_cpu_valid = NULL;
2862}
2863
2864/**
2865 * Set the CPU read domain on a range of the object.
2866 *
2867 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
2868 * not entirely valid. The page_cpu_valid member of the object flags which
2869 * pages have been flushed, and will be respected by
2870 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
2871 * of the whole object.
2872 *
2873 * This function returns when the move is complete, including waiting on
2874 * flushes to occur.
Eric Anholt673a3942008-07-30 12:06:12 -07002875 */
2876static int
Eric Anholte47c68e2008-11-14 13:35:19 -08002877i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
2878 uint64_t offset, uint64_t size)
Eric Anholt673a3942008-07-30 12:06:12 -07002879{
2880 struct drm_i915_gem_object *obj_priv = obj->driver_private;
Eric Anholte47c68e2008-11-14 13:35:19 -08002881 int i, ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002882
Eric Anholte47c68e2008-11-14 13:35:19 -08002883 if (offset == 0 && size == obj->size)
2884 return i915_gem_object_set_to_cpu_domain(obj, 0);
2885
2886 i915_gem_object_flush_gpu_write_domain(obj);
2887 /* Wait on any GPU rendering and flushing to occur. */
2888 ret = i915_gem_object_wait_rendering(obj);
2889 if (ret != 0)
2890 return ret;
2891 i915_gem_object_flush_gtt_write_domain(obj);
2892
2893 /* If we're already fully in the CPU read domain, we're done. */
2894 if (obj_priv->page_cpu_valid == NULL &&
2895 (obj->read_domains & I915_GEM_DOMAIN_CPU) != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07002896 return 0;
2897
Eric Anholte47c68e2008-11-14 13:35:19 -08002898 /* Otherwise, create/clear the per-page CPU read domain flag if we're
2899 * newly adding I915_GEM_DOMAIN_CPU
2900 */
Eric Anholt673a3942008-07-30 12:06:12 -07002901 if (obj_priv->page_cpu_valid == NULL) {
Eric Anholt9a298b22009-03-24 12:23:04 -07002902 obj_priv->page_cpu_valid = kzalloc(obj->size / PAGE_SIZE,
2903 GFP_KERNEL);
Eric Anholte47c68e2008-11-14 13:35:19 -08002904 if (obj_priv->page_cpu_valid == NULL)
2905 return -ENOMEM;
2906 } else if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0)
2907 memset(obj_priv->page_cpu_valid, 0, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002908
2909 /* Flush the cache on any pages that are still invalid from the CPU's
2910 * perspective.
2911 */
Eric Anholte47c68e2008-11-14 13:35:19 -08002912 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
2913 i++) {
Eric Anholt673a3942008-07-30 12:06:12 -07002914 if (obj_priv->page_cpu_valid[i])
2915 continue;
2916
Eric Anholt856fa192009-03-19 14:10:50 -07002917 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07002918
2919 obj_priv->page_cpu_valid[i] = 1;
2920 }
2921
Eric Anholte47c68e2008-11-14 13:35:19 -08002922 /* It should now be out of any other write domains, and we can update
2923 * the domain values for our changes.
2924 */
2925 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
2926
2927 obj->read_domains |= I915_GEM_DOMAIN_CPU;
2928
Eric Anholt673a3942008-07-30 12:06:12 -07002929 return 0;
2930}
2931
2932/**
Eric Anholt673a3942008-07-30 12:06:12 -07002933 * Pin an object to the GTT and evaluate the relocations landing in it.
2934 */
2935static int
2936i915_gem_object_pin_and_relocate(struct drm_gem_object *obj,
2937 struct drm_file *file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002938 struct drm_i915_gem_exec_object *entry,
2939 struct drm_i915_gem_relocation_entry *relocs)
Eric Anholt673a3942008-07-30 12:06:12 -07002940{
2941 struct drm_device *dev = obj->dev;
Keith Packard0839ccb2008-10-30 19:38:48 -07002942 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07002943 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2944 int i, ret;
Keith Packard0839ccb2008-10-30 19:38:48 -07002945 void __iomem *reloc_page;
Eric Anholt673a3942008-07-30 12:06:12 -07002946
2947 /* Choose the GTT offset for our buffer and put it there. */
2948 ret = i915_gem_object_pin(obj, (uint32_t) entry->alignment);
2949 if (ret)
2950 return ret;
2951
2952 entry->offset = obj_priv->gtt_offset;
2953
Eric Anholt673a3942008-07-30 12:06:12 -07002954 /* Apply the relocations, using the GTT aperture to avoid cache
2955 * flushing requirements.
2956 */
2957 for (i = 0; i < entry->relocation_count; i++) {
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002958 struct drm_i915_gem_relocation_entry *reloc= &relocs[i];
Eric Anholt673a3942008-07-30 12:06:12 -07002959 struct drm_gem_object *target_obj;
2960 struct drm_i915_gem_object *target_obj_priv;
Eric Anholt3043c602008-10-02 12:24:47 -07002961 uint32_t reloc_val, reloc_offset;
2962 uint32_t __iomem *reloc_entry;
Eric Anholt673a3942008-07-30 12:06:12 -07002963
Eric Anholt673a3942008-07-30 12:06:12 -07002964 target_obj = drm_gem_object_lookup(obj->dev, file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002965 reloc->target_handle);
Eric Anholt673a3942008-07-30 12:06:12 -07002966 if (target_obj == NULL) {
2967 i915_gem_object_unpin(obj);
2968 return -EBADF;
2969 }
2970 target_obj_priv = target_obj->driver_private;
2971
2972 /* The target buffer should have appeared before us in the
2973 * exec_object list, so it should have a GTT space bound by now.
2974 */
2975 if (target_obj_priv->gtt_space == NULL) {
2976 DRM_ERROR("No GTT space found for object %d\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002977 reloc->target_handle);
Eric Anholt673a3942008-07-30 12:06:12 -07002978 drm_gem_object_unreference(target_obj);
2979 i915_gem_object_unpin(obj);
2980 return -EINVAL;
2981 }
2982
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002983 if (reloc->offset > obj->size - 4) {
Eric Anholt673a3942008-07-30 12:06:12 -07002984 DRM_ERROR("Relocation beyond object bounds: "
2985 "obj %p target %d offset %d size %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002986 obj, reloc->target_handle,
2987 (int) reloc->offset, (int) obj->size);
Eric Anholt673a3942008-07-30 12:06:12 -07002988 drm_gem_object_unreference(target_obj);
2989 i915_gem_object_unpin(obj);
2990 return -EINVAL;
2991 }
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002992 if (reloc->offset & 3) {
Eric Anholt673a3942008-07-30 12:06:12 -07002993 DRM_ERROR("Relocation not 4-byte aligned: "
2994 "obj %p target %d offset %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002995 obj, reloc->target_handle,
2996 (int) reloc->offset);
Eric Anholt673a3942008-07-30 12:06:12 -07002997 drm_gem_object_unreference(target_obj);
2998 i915_gem_object_unpin(obj);
2999 return -EINVAL;
3000 }
3001
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003002 if (reloc->write_domain & I915_GEM_DOMAIN_CPU ||
3003 reloc->read_domains & I915_GEM_DOMAIN_CPU) {
Eric Anholte47c68e2008-11-14 13:35:19 -08003004 DRM_ERROR("reloc with read/write CPU domains: "
3005 "obj %p target %d offset %d "
3006 "read %08x write %08x",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003007 obj, reloc->target_handle,
3008 (int) reloc->offset,
3009 reloc->read_domains,
3010 reloc->write_domain);
Chris Wilson491152b2009-02-11 14:26:32 +00003011 drm_gem_object_unreference(target_obj);
3012 i915_gem_object_unpin(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003013 return -EINVAL;
3014 }
3015
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003016 if (reloc->write_domain && target_obj->pending_write_domain &&
3017 reloc->write_domain != target_obj->pending_write_domain) {
Eric Anholt673a3942008-07-30 12:06:12 -07003018 DRM_ERROR("Write domain conflict: "
3019 "obj %p target %d offset %d "
3020 "new %08x old %08x\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003021 obj, reloc->target_handle,
3022 (int) reloc->offset,
3023 reloc->write_domain,
Eric Anholt673a3942008-07-30 12:06:12 -07003024 target_obj->pending_write_domain);
3025 drm_gem_object_unreference(target_obj);
3026 i915_gem_object_unpin(obj);
3027 return -EINVAL;
3028 }
3029
3030#if WATCH_RELOC
3031 DRM_INFO("%s: obj %p offset %08x target %d "
3032 "read %08x write %08x gtt %08x "
3033 "presumed %08x delta %08x\n",
3034 __func__,
3035 obj,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003036 (int) reloc->offset,
3037 (int) reloc->target_handle,
3038 (int) reloc->read_domains,
3039 (int) reloc->write_domain,
Eric Anholt673a3942008-07-30 12:06:12 -07003040 (int) target_obj_priv->gtt_offset,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003041 (int) reloc->presumed_offset,
3042 reloc->delta);
Eric Anholt673a3942008-07-30 12:06:12 -07003043#endif
3044
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003045 target_obj->pending_read_domains |= reloc->read_domains;
3046 target_obj->pending_write_domain |= reloc->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07003047
3048 /* If the relocation already has the right value in it, no
3049 * more work needs to be done.
3050 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003051 if (target_obj_priv->gtt_offset == reloc->presumed_offset) {
Eric Anholt673a3942008-07-30 12:06:12 -07003052 drm_gem_object_unreference(target_obj);
3053 continue;
3054 }
3055
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003056 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
3057 if (ret != 0) {
3058 drm_gem_object_unreference(target_obj);
3059 i915_gem_object_unpin(obj);
3060 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -07003061 }
3062
3063 /* Map the page containing the relocation we're going to
3064 * perform.
3065 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003066 reloc_offset = obj_priv->gtt_offset + reloc->offset;
Keith Packard0839ccb2008-10-30 19:38:48 -07003067 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
3068 (reloc_offset &
3069 ~(PAGE_SIZE - 1)));
Eric Anholt3043c602008-10-02 12:24:47 -07003070 reloc_entry = (uint32_t __iomem *)(reloc_page +
Keith Packard0839ccb2008-10-30 19:38:48 -07003071 (reloc_offset & (PAGE_SIZE - 1)));
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003072 reloc_val = target_obj_priv->gtt_offset + reloc->delta;
Eric Anholt673a3942008-07-30 12:06:12 -07003073
3074#if WATCH_BUF
3075 DRM_INFO("Applied relocation: %p@0x%08x %08x -> %08x\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003076 obj, (unsigned int) reloc->offset,
Eric Anholt673a3942008-07-30 12:06:12 -07003077 readl(reloc_entry), reloc_val);
3078#endif
3079 writel(reloc_val, reloc_entry);
Keith Packard0839ccb2008-10-30 19:38:48 -07003080 io_mapping_unmap_atomic(reloc_page);
Eric Anholt673a3942008-07-30 12:06:12 -07003081
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003082 /* The updated presumed offset for this entry will be
3083 * copied back out to the user.
Eric Anholt673a3942008-07-30 12:06:12 -07003084 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003085 reloc->presumed_offset = target_obj_priv->gtt_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003086
3087 drm_gem_object_unreference(target_obj);
3088 }
3089
Eric Anholt673a3942008-07-30 12:06:12 -07003090#if WATCH_BUF
3091 if (0)
3092 i915_gem_dump_object(obj, 128, __func__, ~0);
3093#endif
3094 return 0;
3095}
3096
3097/** Dispatch a batchbuffer to the ring
3098 */
3099static int
3100i915_dispatch_gem_execbuffer(struct drm_device *dev,
3101 struct drm_i915_gem_execbuffer *exec,
Eric Anholt201361a2009-03-11 12:30:04 -07003102 struct drm_clip_rect *cliprects,
Eric Anholt673a3942008-07-30 12:06:12 -07003103 uint64_t exec_offset)
3104{
3105 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003106 int nbox = exec->num_cliprects;
3107 int i = 0, count;
Chris Wilson83d60792009-06-06 09:45:57 +01003108 uint32_t exec_start, exec_len;
Eric Anholt673a3942008-07-30 12:06:12 -07003109 RING_LOCALS;
3110
3111 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
3112 exec_len = (uint32_t) exec->batch_len;
3113
Eric Anholt673a3942008-07-30 12:06:12 -07003114 count = nbox ? nbox : 1;
3115
3116 for (i = 0; i < count; i++) {
3117 if (i < nbox) {
Eric Anholt201361a2009-03-11 12:30:04 -07003118 int ret = i915_emit_box(dev, cliprects, i,
Eric Anholt673a3942008-07-30 12:06:12 -07003119 exec->DR1, exec->DR4);
3120 if (ret)
3121 return ret;
3122 }
3123
3124 if (IS_I830(dev) || IS_845G(dev)) {
3125 BEGIN_LP_RING(4);
3126 OUT_RING(MI_BATCH_BUFFER);
3127 OUT_RING(exec_start | MI_BATCH_NON_SECURE);
3128 OUT_RING(exec_start + exec_len - 4);
3129 OUT_RING(0);
3130 ADVANCE_LP_RING();
3131 } else {
3132 BEGIN_LP_RING(2);
3133 if (IS_I965G(dev)) {
3134 OUT_RING(MI_BATCH_BUFFER_START |
3135 (2 << 6) |
3136 MI_BATCH_NON_SECURE_I965);
3137 OUT_RING(exec_start);
3138 } else {
3139 OUT_RING(MI_BATCH_BUFFER_START |
3140 (2 << 6));
3141 OUT_RING(exec_start | MI_BATCH_NON_SECURE);
3142 }
3143 ADVANCE_LP_RING();
3144 }
3145 }
3146
3147 /* XXX breadcrumb */
3148 return 0;
3149}
3150
3151/* Throttle our rendering by waiting until the ring has completed our requests
3152 * emitted over 20 msec ago.
3153 *
Eric Anholtb9624422009-06-03 07:27:35 +00003154 * Note that if we were to use the current jiffies each time around the loop,
3155 * we wouldn't escape the function with any frames outstanding if the time to
3156 * render a frame was over 20ms.
3157 *
Eric Anholt673a3942008-07-30 12:06:12 -07003158 * This should get us reasonable parallelism between CPU and GPU but also
3159 * relatively low latency when blocking on a particular request to finish.
3160 */
3161static int
3162i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file_priv)
3163{
3164 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
3165 int ret = 0;
Eric Anholtb9624422009-06-03 07:27:35 +00003166 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
Eric Anholt673a3942008-07-30 12:06:12 -07003167
3168 mutex_lock(&dev->struct_mutex);
Eric Anholtb9624422009-06-03 07:27:35 +00003169 while (!list_empty(&i915_file_priv->mm.request_list)) {
3170 struct drm_i915_gem_request *request;
3171
3172 request = list_first_entry(&i915_file_priv->mm.request_list,
3173 struct drm_i915_gem_request,
3174 client_list);
3175
3176 if (time_after_eq(request->emitted_jiffies, recent_enough))
3177 break;
3178
3179 ret = i915_wait_request(dev, request->seqno);
3180 if (ret != 0)
3181 break;
3182 }
Eric Anholt673a3942008-07-30 12:06:12 -07003183 mutex_unlock(&dev->struct_mutex);
Eric Anholtb9624422009-06-03 07:27:35 +00003184
Eric Anholt673a3942008-07-30 12:06:12 -07003185 return ret;
3186}
3187
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003188static int
3189i915_gem_get_relocs_from_user(struct drm_i915_gem_exec_object *exec_list,
3190 uint32_t buffer_count,
3191 struct drm_i915_gem_relocation_entry **relocs)
3192{
3193 uint32_t reloc_count = 0, reloc_index = 0, i;
3194 int ret;
3195
3196 *relocs = NULL;
3197 for (i = 0; i < buffer_count; i++) {
3198 if (reloc_count + exec_list[i].relocation_count < reloc_count)
3199 return -EINVAL;
3200 reloc_count += exec_list[i].relocation_count;
3201 }
3202
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003203 *relocs = drm_calloc_large(reloc_count, sizeof(**relocs));
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003204 if (*relocs == NULL)
3205 return -ENOMEM;
3206
3207 for (i = 0; i < buffer_count; i++) {
3208 struct drm_i915_gem_relocation_entry __user *user_relocs;
3209
3210 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3211
3212 ret = copy_from_user(&(*relocs)[reloc_index],
3213 user_relocs,
3214 exec_list[i].relocation_count *
3215 sizeof(**relocs));
3216 if (ret != 0) {
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003217 drm_free_large(*relocs);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003218 *relocs = NULL;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003219 return -EFAULT;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003220 }
3221
3222 reloc_index += exec_list[i].relocation_count;
3223 }
3224
Florian Mickler2bc43b52009-04-06 22:55:41 +02003225 return 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003226}
3227
3228static int
3229i915_gem_put_relocs_to_user(struct drm_i915_gem_exec_object *exec_list,
3230 uint32_t buffer_count,
3231 struct drm_i915_gem_relocation_entry *relocs)
3232{
3233 uint32_t reloc_count = 0, i;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003234 int ret = 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003235
3236 for (i = 0; i < buffer_count; i++) {
3237 struct drm_i915_gem_relocation_entry __user *user_relocs;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003238 int unwritten;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003239
3240 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3241
Florian Mickler2bc43b52009-04-06 22:55:41 +02003242 unwritten = copy_to_user(user_relocs,
3243 &relocs[reloc_count],
3244 exec_list[i].relocation_count *
3245 sizeof(*relocs));
3246
3247 if (unwritten) {
3248 ret = -EFAULT;
3249 goto err;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003250 }
3251
3252 reloc_count += exec_list[i].relocation_count;
3253 }
3254
Florian Mickler2bc43b52009-04-06 22:55:41 +02003255err:
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003256 drm_free_large(relocs);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003257
3258 return ret;
3259}
3260
Chris Wilson83d60792009-06-06 09:45:57 +01003261static int
3262i915_gem_check_execbuffer (struct drm_i915_gem_execbuffer *exec,
3263 uint64_t exec_offset)
3264{
3265 uint32_t exec_start, exec_len;
3266
3267 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
3268 exec_len = (uint32_t) exec->batch_len;
3269
3270 if ((exec_start | exec_len) & 0x7)
3271 return -EINVAL;
3272
3273 if (!exec_start)
3274 return -EINVAL;
3275
3276 return 0;
3277}
3278
Eric Anholt673a3942008-07-30 12:06:12 -07003279int
3280i915_gem_execbuffer(struct drm_device *dev, void *data,
3281 struct drm_file *file_priv)
3282{
3283 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003284 struct drm_i915_gem_execbuffer *args = data;
3285 struct drm_i915_gem_exec_object *exec_list = NULL;
3286 struct drm_gem_object **object_list = NULL;
3287 struct drm_gem_object *batch_obj;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003288 struct drm_i915_gem_object *obj_priv;
Eric Anholt201361a2009-03-11 12:30:04 -07003289 struct drm_clip_rect *cliprects = NULL;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003290 struct drm_i915_gem_relocation_entry *relocs;
3291 int ret, ret2, i, pinned = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003292 uint64_t exec_offset;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003293 uint32_t seqno, flush_domains, reloc_index;
Keith Packardac94a962008-11-20 23:30:27 -08003294 int pin_tries;
Eric Anholt673a3942008-07-30 12:06:12 -07003295
3296#if WATCH_EXEC
3297 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3298 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3299#endif
3300
Eric Anholt4f481ed2008-09-10 14:22:49 -07003301 if (args->buffer_count < 1) {
3302 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3303 return -EINVAL;
3304 }
Eric Anholt673a3942008-07-30 12:06:12 -07003305 /* Copy in the exec list from userland */
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003306 exec_list = drm_calloc_large(sizeof(*exec_list), args->buffer_count);
3307 object_list = drm_calloc_large(sizeof(*object_list), args->buffer_count);
Eric Anholt673a3942008-07-30 12:06:12 -07003308 if (exec_list == NULL || object_list == NULL) {
3309 DRM_ERROR("Failed to allocate exec or object list "
3310 "for %d buffers\n",
3311 args->buffer_count);
3312 ret = -ENOMEM;
3313 goto pre_mutex_err;
3314 }
3315 ret = copy_from_user(exec_list,
3316 (struct drm_i915_relocation_entry __user *)
3317 (uintptr_t) args->buffers_ptr,
3318 sizeof(*exec_list) * args->buffer_count);
3319 if (ret != 0) {
3320 DRM_ERROR("copy %d exec entries failed %d\n",
3321 args->buffer_count, ret);
3322 goto pre_mutex_err;
3323 }
3324
Eric Anholt201361a2009-03-11 12:30:04 -07003325 if (args->num_cliprects != 0) {
Eric Anholt9a298b22009-03-24 12:23:04 -07003326 cliprects = kcalloc(args->num_cliprects, sizeof(*cliprects),
3327 GFP_KERNEL);
Eric Anholt201361a2009-03-11 12:30:04 -07003328 if (cliprects == NULL)
3329 goto pre_mutex_err;
3330
3331 ret = copy_from_user(cliprects,
3332 (struct drm_clip_rect __user *)
3333 (uintptr_t) args->cliprects_ptr,
3334 sizeof(*cliprects) * args->num_cliprects);
3335 if (ret != 0) {
3336 DRM_ERROR("copy %d cliprects failed: %d\n",
3337 args->num_cliprects, ret);
3338 goto pre_mutex_err;
3339 }
3340 }
3341
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003342 ret = i915_gem_get_relocs_from_user(exec_list, args->buffer_count,
3343 &relocs);
3344 if (ret != 0)
3345 goto pre_mutex_err;
3346
Eric Anholt673a3942008-07-30 12:06:12 -07003347 mutex_lock(&dev->struct_mutex);
3348
3349 i915_verify_inactive(dev, __FILE__, __LINE__);
3350
Ben Gamariba1234d2009-09-14 17:48:47 -04003351 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07003352 DRM_ERROR("Execbuf while wedged\n");
3353 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003354 ret = -EIO;
3355 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003356 }
3357
3358 if (dev_priv->mm.suspended) {
3359 DRM_ERROR("Execbuf while VT-switched.\n");
3360 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003361 ret = -EBUSY;
3362 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003363 }
3364
Keith Packardac94a962008-11-20 23:30:27 -08003365 /* Look up object handles */
Eric Anholt673a3942008-07-30 12:06:12 -07003366 for (i = 0; i < args->buffer_count; i++) {
3367 object_list[i] = drm_gem_object_lookup(dev, file_priv,
3368 exec_list[i].handle);
3369 if (object_list[i] == NULL) {
3370 DRM_ERROR("Invalid object handle %d at index %d\n",
3371 exec_list[i].handle, i);
3372 ret = -EBADF;
3373 goto err;
3374 }
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003375
3376 obj_priv = object_list[i]->driver_private;
3377 if (obj_priv->in_execbuffer) {
3378 DRM_ERROR("Object %p appears more than once in object list\n",
3379 object_list[i]);
3380 ret = -EBADF;
3381 goto err;
3382 }
3383 obj_priv->in_execbuffer = true;
Keith Packardac94a962008-11-20 23:30:27 -08003384 }
Eric Anholt673a3942008-07-30 12:06:12 -07003385
Keith Packardac94a962008-11-20 23:30:27 -08003386 /* Pin and relocate */
3387 for (pin_tries = 0; ; pin_tries++) {
3388 ret = 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003389 reloc_index = 0;
3390
Keith Packardac94a962008-11-20 23:30:27 -08003391 for (i = 0; i < args->buffer_count; i++) {
3392 object_list[i]->pending_read_domains = 0;
3393 object_list[i]->pending_write_domain = 0;
3394 ret = i915_gem_object_pin_and_relocate(object_list[i],
3395 file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003396 &exec_list[i],
3397 &relocs[reloc_index]);
Keith Packardac94a962008-11-20 23:30:27 -08003398 if (ret)
3399 break;
3400 pinned = i + 1;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003401 reloc_index += exec_list[i].relocation_count;
Keith Packardac94a962008-11-20 23:30:27 -08003402 }
3403 /* success */
3404 if (ret == 0)
3405 break;
3406
3407 /* error other than GTT full, or we've already tried again */
Chris Wilson2939e1f2009-06-06 09:46:03 +01003408 if (ret != -ENOSPC || pin_tries >= 1) {
Eric Anholtf1acec92008-12-19 14:47:48 -08003409 if (ret != -ERESTARTSYS)
3410 DRM_ERROR("Failed to pin buffers %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07003411 goto err;
3412 }
Keith Packardac94a962008-11-20 23:30:27 -08003413
3414 /* unpin all of our buffers */
3415 for (i = 0; i < pinned; i++)
3416 i915_gem_object_unpin(object_list[i]);
Eric Anholtb1177632008-12-10 10:09:41 -08003417 pinned = 0;
Keith Packardac94a962008-11-20 23:30:27 -08003418
3419 /* evict everyone we can from the aperture */
3420 ret = i915_gem_evict_everything(dev);
3421 if (ret)
3422 goto err;
Eric Anholt673a3942008-07-30 12:06:12 -07003423 }
3424
3425 /* Set the pending read domains for the batch buffer to COMMAND */
3426 batch_obj = object_list[args->buffer_count-1];
Chris Wilson5f26a2c2009-06-06 09:45:58 +01003427 if (batch_obj->pending_write_domain) {
3428 DRM_ERROR("Attempting to use self-modifying batch buffer\n");
3429 ret = -EINVAL;
3430 goto err;
3431 }
3432 batch_obj->pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
Eric Anholt673a3942008-07-30 12:06:12 -07003433
Chris Wilson83d60792009-06-06 09:45:57 +01003434 /* Sanity check the batch buffer, prior to moving objects */
3435 exec_offset = exec_list[args->buffer_count - 1].offset;
3436 ret = i915_gem_check_execbuffer (args, exec_offset);
3437 if (ret != 0) {
3438 DRM_ERROR("execbuf with invalid offset/length\n");
3439 goto err;
3440 }
3441
Eric Anholt673a3942008-07-30 12:06:12 -07003442 i915_verify_inactive(dev, __FILE__, __LINE__);
3443
Keith Packard646f0f62008-11-20 23:23:03 -08003444 /* Zero the global flush/invalidate flags. These
3445 * will be modified as new domains are computed
3446 * for each object
3447 */
3448 dev->invalidate_domains = 0;
3449 dev->flush_domains = 0;
3450
Eric Anholt673a3942008-07-30 12:06:12 -07003451 for (i = 0; i < args->buffer_count; i++) {
3452 struct drm_gem_object *obj = object_list[i];
Eric Anholt673a3942008-07-30 12:06:12 -07003453
Keith Packard646f0f62008-11-20 23:23:03 -08003454 /* Compute new gpu domains and update invalidate/flush */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003455 i915_gem_object_set_to_gpu_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003456 }
3457
3458 i915_verify_inactive(dev, __FILE__, __LINE__);
3459
Keith Packard646f0f62008-11-20 23:23:03 -08003460 if (dev->invalidate_domains | dev->flush_domains) {
3461#if WATCH_EXEC
3462 DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
3463 __func__,
3464 dev->invalidate_domains,
3465 dev->flush_domains);
3466#endif
3467 i915_gem_flush(dev,
3468 dev->invalidate_domains,
3469 dev->flush_domains);
3470 if (dev->flush_domains)
Eric Anholtb9624422009-06-03 07:27:35 +00003471 (void)i915_add_request(dev, file_priv,
3472 dev->flush_domains);
Keith Packard646f0f62008-11-20 23:23:03 -08003473 }
Eric Anholt673a3942008-07-30 12:06:12 -07003474
Eric Anholtefbeed92009-02-19 14:54:51 -08003475 for (i = 0; i < args->buffer_count; i++) {
3476 struct drm_gem_object *obj = object_list[i];
3477
3478 obj->write_domain = obj->pending_write_domain;
3479 }
3480
Eric Anholt673a3942008-07-30 12:06:12 -07003481 i915_verify_inactive(dev, __FILE__, __LINE__);
3482
3483#if WATCH_COHERENCY
3484 for (i = 0; i < args->buffer_count; i++) {
3485 i915_gem_object_check_coherency(object_list[i],
3486 exec_list[i].handle);
3487 }
3488#endif
3489
Eric Anholt673a3942008-07-30 12:06:12 -07003490#if WATCH_EXEC
Ben Gamari6911a9b2009-04-02 11:24:54 -07003491 i915_gem_dump_object(batch_obj,
Eric Anholt673a3942008-07-30 12:06:12 -07003492 args->batch_len,
3493 __func__,
3494 ~0);
3495#endif
3496
Eric Anholt673a3942008-07-30 12:06:12 -07003497 /* Exec the batchbuffer */
Eric Anholt201361a2009-03-11 12:30:04 -07003498 ret = i915_dispatch_gem_execbuffer(dev, args, cliprects, exec_offset);
Eric Anholt673a3942008-07-30 12:06:12 -07003499 if (ret) {
3500 DRM_ERROR("dispatch failed %d\n", ret);
3501 goto err;
3502 }
3503
3504 /*
3505 * Ensure that the commands in the batch buffer are
3506 * finished before the interrupt fires
3507 */
3508 flush_domains = i915_retire_commands(dev);
3509
3510 i915_verify_inactive(dev, __FILE__, __LINE__);
3511
3512 /*
3513 * Get a seqno representing the execution of the current buffer,
3514 * which we can wait on. We would like to mitigate these interrupts,
3515 * likely by only creating seqnos occasionally (so that we have
3516 * *some* interrupts representing completion of buffers that we can
3517 * wait on when trying to clear up gtt space).
3518 */
Eric Anholtb9624422009-06-03 07:27:35 +00003519 seqno = i915_add_request(dev, file_priv, flush_domains);
Eric Anholt673a3942008-07-30 12:06:12 -07003520 BUG_ON(seqno == 0);
Eric Anholt673a3942008-07-30 12:06:12 -07003521 for (i = 0; i < args->buffer_count; i++) {
3522 struct drm_gem_object *obj = object_list[i];
Eric Anholt673a3942008-07-30 12:06:12 -07003523
Eric Anholtce44b0e2008-11-06 16:00:31 -08003524 i915_gem_object_move_to_active(obj, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07003525#if WATCH_LRU
3526 DRM_INFO("%s: move to exec list %p\n", __func__, obj);
3527#endif
3528 }
3529#if WATCH_LRU
3530 i915_dump_lru(dev, __func__);
3531#endif
3532
3533 i915_verify_inactive(dev, __FILE__, __LINE__);
3534
Eric Anholt673a3942008-07-30 12:06:12 -07003535err:
Julia Lawallaad87df2008-12-21 16:28:47 +01003536 for (i = 0; i < pinned; i++)
3537 i915_gem_object_unpin(object_list[i]);
Eric Anholt673a3942008-07-30 12:06:12 -07003538
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003539 for (i = 0; i < args->buffer_count; i++) {
3540 if (object_list[i]) {
3541 obj_priv = object_list[i]->driver_private;
3542 obj_priv->in_execbuffer = false;
3543 }
Julia Lawallaad87df2008-12-21 16:28:47 +01003544 drm_gem_object_unreference(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003545 }
Julia Lawallaad87df2008-12-21 16:28:47 +01003546
Eric Anholt673a3942008-07-30 12:06:12 -07003547 mutex_unlock(&dev->struct_mutex);
3548
Roland Dreiera35f2e22009-02-06 17:48:09 -08003549 if (!ret) {
3550 /* Copy the new buffer offsets back to the user's exec list. */
3551 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
3552 (uintptr_t) args->buffers_ptr,
3553 exec_list,
3554 sizeof(*exec_list) * args->buffer_count);
Florian Mickler2bc43b52009-04-06 22:55:41 +02003555 if (ret) {
3556 ret = -EFAULT;
Roland Dreiera35f2e22009-02-06 17:48:09 -08003557 DRM_ERROR("failed to copy %d exec entries "
3558 "back to user (%d)\n",
3559 args->buffer_count, ret);
Florian Mickler2bc43b52009-04-06 22:55:41 +02003560 }
Roland Dreiera35f2e22009-02-06 17:48:09 -08003561 }
3562
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003563 /* Copy the updated relocations out regardless of current error
3564 * state. Failure to update the relocs would mean that the next
3565 * time userland calls execbuf, it would do so with presumed offset
3566 * state that didn't match the actual object state.
3567 */
3568 ret2 = i915_gem_put_relocs_to_user(exec_list, args->buffer_count,
3569 relocs);
3570 if (ret2 != 0) {
3571 DRM_ERROR("Failed to copy relocations back out: %d\n", ret2);
3572
3573 if (ret == 0)
3574 ret = ret2;
3575 }
3576
Eric Anholt673a3942008-07-30 12:06:12 -07003577pre_mutex_err:
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003578 drm_free_large(object_list);
3579 drm_free_large(exec_list);
Eric Anholt9a298b22009-03-24 12:23:04 -07003580 kfree(cliprects);
Eric Anholt673a3942008-07-30 12:06:12 -07003581
3582 return ret;
3583}
3584
3585int
3586i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment)
3587{
3588 struct drm_device *dev = obj->dev;
3589 struct drm_i915_gem_object *obj_priv = obj->driver_private;
3590 int ret;
3591
3592 i915_verify_inactive(dev, __FILE__, __LINE__);
3593 if (obj_priv->gtt_space == NULL) {
3594 ret = i915_gem_object_bind_to_gtt(obj, alignment);
3595 if (ret != 0) {
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08003596 if (ret != -EBUSY && ret != -ERESTARTSYS)
Kyle McMartin0fce81e2009-02-28 15:01:16 -05003597 DRM_ERROR("Failure to bind: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07003598 return ret;
3599 }
Chris Wilson22c344e2009-02-11 14:26:45 +00003600 }
3601 /*
3602 * Pre-965 chips need a fence register set up in order to
3603 * properly handle tiled surfaces.
3604 */
Eric Anholta09ba7f2009-08-29 12:49:51 -07003605 if (!IS_I965G(dev) && obj_priv->tiling_mode != I915_TILING_NONE) {
Chris Wilson8c4b8c32009-06-17 22:08:52 +01003606 ret = i915_gem_object_get_fence_reg(obj);
Chris Wilson22c344e2009-02-11 14:26:45 +00003607 if (ret != 0) {
3608 if (ret != -EBUSY && ret != -ERESTARTSYS)
3609 DRM_ERROR("Failure to install fence: %d\n",
3610 ret);
3611 return ret;
3612 }
Eric Anholt673a3942008-07-30 12:06:12 -07003613 }
3614 obj_priv->pin_count++;
3615
3616 /* If the object is not active and not pending a flush,
3617 * remove it from the inactive list
3618 */
3619 if (obj_priv->pin_count == 1) {
3620 atomic_inc(&dev->pin_count);
3621 atomic_add(obj->size, &dev->pin_memory);
3622 if (!obj_priv->active &&
Chris Wilson21d509e2009-06-06 09:46:02 +01003623 (obj->write_domain & I915_GEM_GPU_DOMAINS) == 0 &&
Eric Anholt673a3942008-07-30 12:06:12 -07003624 !list_empty(&obj_priv->list))
3625 list_del_init(&obj_priv->list);
3626 }
3627 i915_verify_inactive(dev, __FILE__, __LINE__);
3628
3629 return 0;
3630}
3631
3632void
3633i915_gem_object_unpin(struct drm_gem_object *obj)
3634{
3635 struct drm_device *dev = obj->dev;
3636 drm_i915_private_t *dev_priv = dev->dev_private;
3637 struct drm_i915_gem_object *obj_priv = obj->driver_private;
3638
3639 i915_verify_inactive(dev, __FILE__, __LINE__);
3640 obj_priv->pin_count--;
3641 BUG_ON(obj_priv->pin_count < 0);
3642 BUG_ON(obj_priv->gtt_space == NULL);
3643
3644 /* If the object is no longer pinned, and is
3645 * neither active nor being flushed, then stick it on
3646 * the inactive list
3647 */
3648 if (obj_priv->pin_count == 0) {
3649 if (!obj_priv->active &&
Chris Wilson21d509e2009-06-06 09:46:02 +01003650 (obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
Eric Anholt673a3942008-07-30 12:06:12 -07003651 list_move_tail(&obj_priv->list,
3652 &dev_priv->mm.inactive_list);
3653 atomic_dec(&dev->pin_count);
3654 atomic_sub(obj->size, &dev->pin_memory);
3655 }
3656 i915_verify_inactive(dev, __FILE__, __LINE__);
3657}
3658
3659int
3660i915_gem_pin_ioctl(struct drm_device *dev, void *data,
3661 struct drm_file *file_priv)
3662{
3663 struct drm_i915_gem_pin *args = data;
3664 struct drm_gem_object *obj;
3665 struct drm_i915_gem_object *obj_priv;
3666 int ret;
3667
3668 mutex_lock(&dev->struct_mutex);
3669
3670 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
3671 if (obj == NULL) {
3672 DRM_ERROR("Bad handle in i915_gem_pin_ioctl(): %d\n",
3673 args->handle);
3674 mutex_unlock(&dev->struct_mutex);
3675 return -EBADF;
3676 }
3677 obj_priv = obj->driver_private;
3678
Jesse Barnes79e53942008-11-07 14:24:08 -08003679 if (obj_priv->pin_filp != NULL && obj_priv->pin_filp != file_priv) {
3680 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
3681 args->handle);
Chris Wilson96dec612009-02-08 19:08:04 +00003682 drm_gem_object_unreference(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003683 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08003684 return -EINVAL;
3685 }
3686
3687 obj_priv->user_pin_count++;
3688 obj_priv->pin_filp = file_priv;
3689 if (obj_priv->user_pin_count == 1) {
3690 ret = i915_gem_object_pin(obj, args->alignment);
3691 if (ret != 0) {
3692 drm_gem_object_unreference(obj);
3693 mutex_unlock(&dev->struct_mutex);
3694 return ret;
3695 }
Eric Anholt673a3942008-07-30 12:06:12 -07003696 }
3697
3698 /* XXX - flush the CPU caches for pinned objects
3699 * as the X server doesn't manage domains yet
3700 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003701 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003702 args->offset = obj_priv->gtt_offset;
3703 drm_gem_object_unreference(obj);
3704 mutex_unlock(&dev->struct_mutex);
3705
3706 return 0;
3707}
3708
3709int
3710i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
3711 struct drm_file *file_priv)
3712{
3713 struct drm_i915_gem_pin *args = data;
3714 struct drm_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08003715 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07003716
3717 mutex_lock(&dev->struct_mutex);
3718
3719 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
3720 if (obj == NULL) {
3721 DRM_ERROR("Bad handle in i915_gem_unpin_ioctl(): %d\n",
3722 args->handle);
3723 mutex_unlock(&dev->struct_mutex);
3724 return -EBADF;
3725 }
3726
Jesse Barnes79e53942008-11-07 14:24:08 -08003727 obj_priv = obj->driver_private;
3728 if (obj_priv->pin_filp != file_priv) {
3729 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
3730 args->handle);
3731 drm_gem_object_unreference(obj);
3732 mutex_unlock(&dev->struct_mutex);
3733 return -EINVAL;
3734 }
3735 obj_priv->user_pin_count--;
3736 if (obj_priv->user_pin_count == 0) {
3737 obj_priv->pin_filp = NULL;
3738 i915_gem_object_unpin(obj);
3739 }
Eric Anholt673a3942008-07-30 12:06:12 -07003740
3741 drm_gem_object_unreference(obj);
3742 mutex_unlock(&dev->struct_mutex);
3743 return 0;
3744}
3745
3746int
3747i915_gem_busy_ioctl(struct drm_device *dev, void *data,
3748 struct drm_file *file_priv)
3749{
3750 struct drm_i915_gem_busy *args = data;
3751 struct drm_gem_object *obj;
3752 struct drm_i915_gem_object *obj_priv;
3753
Eric Anholt673a3942008-07-30 12:06:12 -07003754 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
3755 if (obj == NULL) {
3756 DRM_ERROR("Bad handle in i915_gem_busy_ioctl(): %d\n",
3757 args->handle);
Eric Anholt673a3942008-07-30 12:06:12 -07003758 return -EBADF;
3759 }
3760
Chris Wilsonb1ce7862009-06-06 09:46:00 +01003761 mutex_lock(&dev->struct_mutex);
Eric Anholtf21289b2009-02-18 09:44:56 -08003762 /* Update the active list for the hardware's current position.
3763 * Otherwise this only updates on a delayed timer or when irqs are
3764 * actually unmasked, and our working set ends up being larger than
3765 * required.
3766 */
3767 i915_gem_retire_requests(dev);
3768
Eric Anholt673a3942008-07-30 12:06:12 -07003769 obj_priv = obj->driver_private;
Eric Anholtc4de0a52008-12-14 19:05:04 -08003770 /* Don't count being on the flushing list against the object being
3771 * done. Otherwise, a buffer left on the flushing list but not getting
3772 * flushed (because nobody's flushing that domain) won't ever return
3773 * unbusy and get reused by libdrm's bo cache. The other expected
3774 * consumer of this interface, OpenGL's occlusion queries, also specs
3775 * that the objects get unbusy "eventually" without any interference.
3776 */
3777 args->busy = obj_priv->active && obj_priv->last_rendering_seqno != 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003778
3779 drm_gem_object_unreference(obj);
3780 mutex_unlock(&dev->struct_mutex);
3781 return 0;
3782}
3783
3784int
3785i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
3786 struct drm_file *file_priv)
3787{
3788 return i915_gem_ring_throttle(dev, file_priv);
3789}
3790
3791int i915_gem_init_object(struct drm_gem_object *obj)
3792{
3793 struct drm_i915_gem_object *obj_priv;
3794
Eric Anholt9a298b22009-03-24 12:23:04 -07003795 obj_priv = kzalloc(sizeof(*obj_priv), GFP_KERNEL);
Eric Anholt673a3942008-07-30 12:06:12 -07003796 if (obj_priv == NULL)
3797 return -ENOMEM;
3798
3799 /*
3800 * We've just allocated pages from the kernel,
3801 * so they've just been written by the CPU with
3802 * zeros. They'll need to be clflushed before we
3803 * use them with the GPU.
3804 */
3805 obj->write_domain = I915_GEM_DOMAIN_CPU;
3806 obj->read_domains = I915_GEM_DOMAIN_CPU;
3807
Keith Packardba1eb1d2008-10-14 19:55:10 -07003808 obj_priv->agp_type = AGP_USER_MEMORY;
3809
Eric Anholt673a3942008-07-30 12:06:12 -07003810 obj->driver_private = obj_priv;
3811 obj_priv->obj = obj;
Jesse Barnesde151cf2008-11-12 10:03:55 -08003812 obj_priv->fence_reg = I915_FENCE_REG_NONE;
Eric Anholt673a3942008-07-30 12:06:12 -07003813 INIT_LIST_HEAD(&obj_priv->list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07003814 INIT_LIST_HEAD(&obj_priv->fence_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08003815
Eric Anholt673a3942008-07-30 12:06:12 -07003816 return 0;
3817}
3818
3819void i915_gem_free_object(struct drm_gem_object *obj)
3820{
Jesse Barnesde151cf2008-11-12 10:03:55 -08003821 struct drm_device *dev = obj->dev;
Eric Anholt673a3942008-07-30 12:06:12 -07003822 struct drm_i915_gem_object *obj_priv = obj->driver_private;
3823
3824 while (obj_priv->pin_count > 0)
3825 i915_gem_object_unpin(obj);
3826
Dave Airlie71acb5e2008-12-30 20:31:46 +10003827 if (obj_priv->phys_obj)
3828 i915_gem_detach_phys_object(dev, obj);
3829
Eric Anholt673a3942008-07-30 12:06:12 -07003830 i915_gem_object_unbind(obj);
3831
Chris Wilson7e616152009-09-10 08:53:04 +01003832 if (obj_priv->mmap_offset)
3833 i915_gem_free_mmap_offset(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08003834
Eric Anholt9a298b22009-03-24 12:23:04 -07003835 kfree(obj_priv->page_cpu_valid);
Eric Anholt280b7132009-03-12 16:56:27 -07003836 kfree(obj_priv->bit_17);
Eric Anholt9a298b22009-03-24 12:23:04 -07003837 kfree(obj->driver_private);
Eric Anholt673a3942008-07-30 12:06:12 -07003838}
3839
Eric Anholt673a3942008-07-30 12:06:12 -07003840/** Unbinds all objects that are on the given buffer list. */
3841static int
3842i915_gem_evict_from_list(struct drm_device *dev, struct list_head *head)
3843{
3844 struct drm_gem_object *obj;
3845 struct drm_i915_gem_object *obj_priv;
3846 int ret;
3847
3848 while (!list_empty(head)) {
3849 obj_priv = list_first_entry(head,
3850 struct drm_i915_gem_object,
3851 list);
3852 obj = obj_priv->obj;
3853
3854 if (obj_priv->pin_count != 0) {
3855 DRM_ERROR("Pinned object in unbind list\n");
3856 mutex_unlock(&dev->struct_mutex);
3857 return -EINVAL;
3858 }
3859
3860 ret = i915_gem_object_unbind(obj);
3861 if (ret != 0) {
3862 DRM_ERROR("Error unbinding object in LeaveVT: %d\n",
3863 ret);
3864 mutex_unlock(&dev->struct_mutex);
3865 return ret;
3866 }
3867 }
3868
3869
3870 return 0;
3871}
3872
Jesse Barnes5669fca2009-02-17 15:13:31 -08003873int
Eric Anholt673a3942008-07-30 12:06:12 -07003874i915_gem_idle(struct drm_device *dev)
3875{
3876 drm_i915_private_t *dev_priv = dev->dev_private;
3877 uint32_t seqno, cur_seqno, last_seqno;
3878 int stuck, ret;
3879
Keith Packard6dbe2772008-10-14 21:41:13 -07003880 mutex_lock(&dev->struct_mutex);
3881
3882 if (dev_priv->mm.suspended || dev_priv->ring.ring_obj == NULL) {
3883 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07003884 return 0;
Keith Packard6dbe2772008-10-14 21:41:13 -07003885 }
Eric Anholt673a3942008-07-30 12:06:12 -07003886
3887 /* Hack! Don't let anybody do execbuf while we don't control the chip.
3888 * We need to replace this with a semaphore, or something.
3889 */
3890 dev_priv->mm.suspended = 1;
Ben Gamarif65d9422009-09-14 17:48:44 -04003891 del_timer(&dev_priv->hangcheck_timer);
Eric Anholt673a3942008-07-30 12:06:12 -07003892
Keith Packard6dbe2772008-10-14 21:41:13 -07003893 /* Cancel the retire work handler, wait for it to finish if running
3894 */
3895 mutex_unlock(&dev->struct_mutex);
3896 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
3897 mutex_lock(&dev->struct_mutex);
3898
Eric Anholt673a3942008-07-30 12:06:12 -07003899 i915_kernel_lost_context(dev);
3900
3901 /* Flush the GPU along with all non-CPU write domains
3902 */
Chris Wilson21d509e2009-06-06 09:46:02 +01003903 i915_gem_flush(dev, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
3904 seqno = i915_add_request(dev, NULL, I915_GEM_GPU_DOMAINS);
Eric Anholt673a3942008-07-30 12:06:12 -07003905
3906 if (seqno == 0) {
3907 mutex_unlock(&dev->struct_mutex);
3908 return -ENOMEM;
3909 }
3910
3911 dev_priv->mm.waiting_gem_seqno = seqno;
3912 last_seqno = 0;
3913 stuck = 0;
3914 for (;;) {
3915 cur_seqno = i915_get_gem_seqno(dev);
3916 if (i915_seqno_passed(cur_seqno, seqno))
3917 break;
3918 if (last_seqno == cur_seqno) {
3919 if (stuck++ > 100) {
3920 DRM_ERROR("hardware wedged\n");
Ben Gamariba1234d2009-09-14 17:48:47 -04003921 atomic_set(&dev_priv->mm.wedged, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07003922 DRM_WAKEUP(&dev_priv->irq_queue);
3923 break;
3924 }
3925 }
3926 msleep(10);
3927 last_seqno = cur_seqno;
3928 }
3929 dev_priv->mm.waiting_gem_seqno = 0;
3930
3931 i915_gem_retire_requests(dev);
3932
Carl Worth5e118f42009-03-20 11:54:25 -07003933 spin_lock(&dev_priv->mm.active_list_lock);
Ben Gamariba1234d2009-09-14 17:48:47 -04003934 if (!atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt28dfe522008-11-13 15:00:55 -08003935 /* Active and flushing should now be empty as we've
3936 * waited for a sequence higher than any pending execbuffer
3937 */
3938 WARN_ON(!list_empty(&dev_priv->mm.active_list));
3939 WARN_ON(!list_empty(&dev_priv->mm.flushing_list));
3940 /* Request should now be empty as we've also waited
3941 * for the last request in the list
3942 */
3943 WARN_ON(!list_empty(&dev_priv->mm.request_list));
3944 }
Eric Anholt673a3942008-07-30 12:06:12 -07003945
Eric Anholt28dfe522008-11-13 15:00:55 -08003946 /* Empty the active and flushing lists to inactive. If there's
3947 * anything left at this point, it means that we're wedged and
3948 * nothing good's going to happen by leaving them there. So strip
3949 * the GPU domains and just stuff them onto inactive.
Eric Anholt673a3942008-07-30 12:06:12 -07003950 */
Eric Anholt28dfe522008-11-13 15:00:55 -08003951 while (!list_empty(&dev_priv->mm.active_list)) {
3952 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07003953
Eric Anholt28dfe522008-11-13 15:00:55 -08003954 obj_priv = list_first_entry(&dev_priv->mm.active_list,
3955 struct drm_i915_gem_object,
3956 list);
3957 obj_priv->obj->write_domain &= ~I915_GEM_GPU_DOMAINS;
3958 i915_gem_object_move_to_inactive(obj_priv->obj);
3959 }
Carl Worth5e118f42009-03-20 11:54:25 -07003960 spin_unlock(&dev_priv->mm.active_list_lock);
Eric Anholt28dfe522008-11-13 15:00:55 -08003961
3962 while (!list_empty(&dev_priv->mm.flushing_list)) {
3963 struct drm_i915_gem_object *obj_priv;
3964
Eric Anholt151903d2008-12-01 10:23:21 +10003965 obj_priv = list_first_entry(&dev_priv->mm.flushing_list,
Eric Anholt28dfe522008-11-13 15:00:55 -08003966 struct drm_i915_gem_object,
3967 list);
3968 obj_priv->obj->write_domain &= ~I915_GEM_GPU_DOMAINS;
3969 i915_gem_object_move_to_inactive(obj_priv->obj);
3970 }
3971
3972
3973 /* Move all inactive buffers out of the GTT. */
Eric Anholt673a3942008-07-30 12:06:12 -07003974 ret = i915_gem_evict_from_list(dev, &dev_priv->mm.inactive_list);
Eric Anholt28dfe522008-11-13 15:00:55 -08003975 WARN_ON(!list_empty(&dev_priv->mm.inactive_list));
Keith Packard6dbe2772008-10-14 21:41:13 -07003976 if (ret) {
3977 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07003978 return ret;
Keith Packard6dbe2772008-10-14 21:41:13 -07003979 }
Eric Anholt673a3942008-07-30 12:06:12 -07003980
Keith Packard6dbe2772008-10-14 21:41:13 -07003981 i915_gem_cleanup_ringbuffer(dev);
3982 mutex_unlock(&dev->struct_mutex);
3983
Eric Anholt673a3942008-07-30 12:06:12 -07003984 return 0;
3985}
3986
3987static int
3988i915_gem_init_hws(struct drm_device *dev)
3989{
3990 drm_i915_private_t *dev_priv = dev->dev_private;
3991 struct drm_gem_object *obj;
3992 struct drm_i915_gem_object *obj_priv;
3993 int ret;
3994
3995 /* If we need a physical address for the status page, it's already
3996 * initialized at driver load time.
3997 */
3998 if (!I915_NEED_GFX_HWS(dev))
3999 return 0;
4000
4001 obj = drm_gem_object_alloc(dev, 4096);
4002 if (obj == NULL) {
4003 DRM_ERROR("Failed to allocate status page\n");
4004 return -ENOMEM;
4005 }
4006 obj_priv = obj->driver_private;
Keith Packardba1eb1d2008-10-14 19:55:10 -07004007 obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
Eric Anholt673a3942008-07-30 12:06:12 -07004008
4009 ret = i915_gem_object_pin(obj, 4096);
4010 if (ret != 0) {
4011 drm_gem_object_unreference(obj);
4012 return ret;
4013 }
4014
4015 dev_priv->status_gfx_addr = obj_priv->gtt_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07004016
Eric Anholt856fa192009-03-19 14:10:50 -07004017 dev_priv->hw_status_page = kmap(obj_priv->pages[0]);
Keith Packardba1eb1d2008-10-14 19:55:10 -07004018 if (dev_priv->hw_status_page == NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -07004019 DRM_ERROR("Failed to map status page.\n");
4020 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
Chris Wilson3eb2ee72009-02-11 14:26:34 +00004021 i915_gem_object_unpin(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004022 drm_gem_object_unreference(obj);
4023 return -EINVAL;
4024 }
4025 dev_priv->hws_obj = obj;
Eric Anholt673a3942008-07-30 12:06:12 -07004026 memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
4027 I915_WRITE(HWS_PGA, dev_priv->status_gfx_addr);
Keith Packardba1eb1d2008-10-14 19:55:10 -07004028 I915_READ(HWS_PGA); /* posting read */
Eric Anholt673a3942008-07-30 12:06:12 -07004029 DRM_DEBUG("hws offset: 0x%08x\n", dev_priv->status_gfx_addr);
4030
4031 return 0;
4032}
4033
Chris Wilson85a7bb92009-02-11 14:52:44 +00004034static void
4035i915_gem_cleanup_hws(struct drm_device *dev)
4036{
4037 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonbab2d1f2009-02-20 17:52:20 +00004038 struct drm_gem_object *obj;
4039 struct drm_i915_gem_object *obj_priv;
Chris Wilson85a7bb92009-02-11 14:52:44 +00004040
4041 if (dev_priv->hws_obj == NULL)
4042 return;
4043
Chris Wilsonbab2d1f2009-02-20 17:52:20 +00004044 obj = dev_priv->hws_obj;
4045 obj_priv = obj->driver_private;
4046
Eric Anholt856fa192009-03-19 14:10:50 -07004047 kunmap(obj_priv->pages[0]);
Chris Wilson85a7bb92009-02-11 14:52:44 +00004048 i915_gem_object_unpin(obj);
4049 drm_gem_object_unreference(obj);
4050 dev_priv->hws_obj = NULL;
Chris Wilsonbab2d1f2009-02-20 17:52:20 +00004051
Chris Wilson85a7bb92009-02-11 14:52:44 +00004052 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
4053 dev_priv->hw_status_page = NULL;
4054
4055 /* Write high address into HWS_PGA when disabling. */
4056 I915_WRITE(HWS_PGA, 0x1ffff000);
4057}
4058
Jesse Barnes79e53942008-11-07 14:24:08 -08004059int
Eric Anholt673a3942008-07-30 12:06:12 -07004060i915_gem_init_ringbuffer(struct drm_device *dev)
4061{
4062 drm_i915_private_t *dev_priv = dev->dev_private;
4063 struct drm_gem_object *obj;
4064 struct drm_i915_gem_object *obj_priv;
Jesse Barnes79e53942008-11-07 14:24:08 -08004065 drm_i915_ring_buffer_t *ring = &dev_priv->ring;
Eric Anholt673a3942008-07-30 12:06:12 -07004066 int ret;
Keith Packard50aa253d2008-10-14 17:20:35 -07004067 u32 head;
Eric Anholt673a3942008-07-30 12:06:12 -07004068
4069 ret = i915_gem_init_hws(dev);
4070 if (ret != 0)
4071 return ret;
4072
4073 obj = drm_gem_object_alloc(dev, 128 * 1024);
4074 if (obj == NULL) {
4075 DRM_ERROR("Failed to allocate ringbuffer\n");
Chris Wilson85a7bb92009-02-11 14:52:44 +00004076 i915_gem_cleanup_hws(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004077 return -ENOMEM;
4078 }
4079 obj_priv = obj->driver_private;
4080
4081 ret = i915_gem_object_pin(obj, 4096);
4082 if (ret != 0) {
4083 drm_gem_object_unreference(obj);
Chris Wilson85a7bb92009-02-11 14:52:44 +00004084 i915_gem_cleanup_hws(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004085 return ret;
4086 }
4087
4088 /* Set up the kernel mapping for the ring. */
Jesse Barnes79e53942008-11-07 14:24:08 -08004089 ring->Size = obj->size;
Eric Anholt673a3942008-07-30 12:06:12 -07004090
Jesse Barnes79e53942008-11-07 14:24:08 -08004091 ring->map.offset = dev->agp->base + obj_priv->gtt_offset;
4092 ring->map.size = obj->size;
4093 ring->map.type = 0;
4094 ring->map.flags = 0;
4095 ring->map.mtrr = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07004096
Jesse Barnes79e53942008-11-07 14:24:08 -08004097 drm_core_ioremap_wc(&ring->map, dev);
4098 if (ring->map.handle == NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -07004099 DRM_ERROR("Failed to map ringbuffer.\n");
4100 memset(&dev_priv->ring, 0, sizeof(dev_priv->ring));
Chris Wilson47ed1852009-02-11 14:26:33 +00004101 i915_gem_object_unpin(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004102 drm_gem_object_unreference(obj);
Chris Wilson85a7bb92009-02-11 14:52:44 +00004103 i915_gem_cleanup_hws(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004104 return -EINVAL;
4105 }
Jesse Barnes79e53942008-11-07 14:24:08 -08004106 ring->ring_obj = obj;
4107 ring->virtual_start = ring->map.handle;
Eric Anholt673a3942008-07-30 12:06:12 -07004108
4109 /* Stop the ring if it's running. */
4110 I915_WRITE(PRB0_CTL, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004111 I915_WRITE(PRB0_TAIL, 0);
Keith Packard50aa253d2008-10-14 17:20:35 -07004112 I915_WRITE(PRB0_HEAD, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004113
4114 /* Initialize the ring. */
4115 I915_WRITE(PRB0_START, obj_priv->gtt_offset);
Keith Packard50aa253d2008-10-14 17:20:35 -07004116 head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
4117
4118 /* G45 ring initialization fails to reset head to zero */
4119 if (head != 0) {
4120 DRM_ERROR("Ring head not reset to zero "
4121 "ctl %08x head %08x tail %08x start %08x\n",
4122 I915_READ(PRB0_CTL),
4123 I915_READ(PRB0_HEAD),
4124 I915_READ(PRB0_TAIL),
4125 I915_READ(PRB0_START));
4126 I915_WRITE(PRB0_HEAD, 0);
4127
4128 DRM_ERROR("Ring head forced to zero "
4129 "ctl %08x head %08x tail %08x start %08x\n",
4130 I915_READ(PRB0_CTL),
4131 I915_READ(PRB0_HEAD),
4132 I915_READ(PRB0_TAIL),
4133 I915_READ(PRB0_START));
4134 }
4135
Eric Anholt673a3942008-07-30 12:06:12 -07004136 I915_WRITE(PRB0_CTL,
4137 ((obj->size - 4096) & RING_NR_PAGES) |
4138 RING_NO_REPORT |
4139 RING_VALID);
4140
Keith Packard50aa253d2008-10-14 17:20:35 -07004141 head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
4142
4143 /* If the head is still not zero, the ring is dead */
4144 if (head != 0) {
4145 DRM_ERROR("Ring initialization failed "
4146 "ctl %08x head %08x tail %08x start %08x\n",
4147 I915_READ(PRB0_CTL),
4148 I915_READ(PRB0_HEAD),
4149 I915_READ(PRB0_TAIL),
4150 I915_READ(PRB0_START));
4151 return -EIO;
4152 }
4153
Eric Anholt673a3942008-07-30 12:06:12 -07004154 /* Update our cache of the ring state */
Jesse Barnes79e53942008-11-07 14:24:08 -08004155 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4156 i915_kernel_lost_context(dev);
4157 else {
4158 ring->head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
4159 ring->tail = I915_READ(PRB0_TAIL) & TAIL_ADDR;
4160 ring->space = ring->head - (ring->tail + 8);
4161 if (ring->space < 0)
4162 ring->space += ring->Size;
4163 }
Eric Anholt673a3942008-07-30 12:06:12 -07004164
4165 return 0;
4166}
4167
Jesse Barnes79e53942008-11-07 14:24:08 -08004168void
Eric Anholt673a3942008-07-30 12:06:12 -07004169i915_gem_cleanup_ringbuffer(struct drm_device *dev)
4170{
4171 drm_i915_private_t *dev_priv = dev->dev_private;
4172
4173 if (dev_priv->ring.ring_obj == NULL)
4174 return;
4175
4176 drm_core_ioremapfree(&dev_priv->ring.map, dev);
4177
4178 i915_gem_object_unpin(dev_priv->ring.ring_obj);
4179 drm_gem_object_unreference(dev_priv->ring.ring_obj);
4180 dev_priv->ring.ring_obj = NULL;
4181 memset(&dev_priv->ring, 0, sizeof(dev_priv->ring));
4182
Chris Wilson85a7bb92009-02-11 14:52:44 +00004183 i915_gem_cleanup_hws(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004184}
4185
4186int
4187i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
4188 struct drm_file *file_priv)
4189{
4190 drm_i915_private_t *dev_priv = dev->dev_private;
4191 int ret;
4192
Jesse Barnes79e53942008-11-07 14:24:08 -08004193 if (drm_core_check_feature(dev, DRIVER_MODESET))
4194 return 0;
4195
Ben Gamariba1234d2009-09-14 17:48:47 -04004196 if (atomic_read(&dev_priv->mm.wedged)) {
Eric Anholt673a3942008-07-30 12:06:12 -07004197 DRM_ERROR("Reenabling wedged hardware, good luck\n");
Ben Gamariba1234d2009-09-14 17:48:47 -04004198 atomic_set(&dev_priv->mm.wedged, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004199 }
4200
Eric Anholt673a3942008-07-30 12:06:12 -07004201 mutex_lock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004202 dev_priv->mm.suspended = 0;
4203
4204 ret = i915_gem_init_ringbuffer(dev);
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004205 if (ret != 0) {
4206 mutex_unlock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004207 return ret;
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004208 }
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004209
Carl Worth5e118f42009-03-20 11:54:25 -07004210 spin_lock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07004211 BUG_ON(!list_empty(&dev_priv->mm.active_list));
Carl Worth5e118f42009-03-20 11:54:25 -07004212 spin_unlock(&dev_priv->mm.active_list_lock);
4213
Eric Anholt673a3942008-07-30 12:06:12 -07004214 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
4215 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
4216 BUG_ON(!list_empty(&dev_priv->mm.request_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004217 mutex_unlock(&dev->struct_mutex);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004218
4219 drm_irq_install(dev);
4220
Eric Anholt673a3942008-07-30 12:06:12 -07004221 return 0;
4222}
4223
4224int
4225i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
4226 struct drm_file *file_priv)
4227{
4228 int ret;
4229
Jesse Barnes79e53942008-11-07 14:24:08 -08004230 if (drm_core_check_feature(dev, DRIVER_MODESET))
4231 return 0;
4232
Eric Anholt673a3942008-07-30 12:06:12 -07004233 ret = i915_gem_idle(dev);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004234 drm_irq_uninstall(dev);
4235
Keith Packard6dbe2772008-10-14 21:41:13 -07004236 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004237}
4238
4239void
4240i915_gem_lastclose(struct drm_device *dev)
4241{
4242 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004243
Eric Anholte806b492009-01-22 09:56:58 -08004244 if (drm_core_check_feature(dev, DRIVER_MODESET))
4245 return;
4246
Keith Packard6dbe2772008-10-14 21:41:13 -07004247 ret = i915_gem_idle(dev);
4248 if (ret)
4249 DRM_ERROR("failed to idle hardware: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07004250}
4251
4252void
4253i915_gem_load(struct drm_device *dev)
4254{
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004255 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07004256 drm_i915_private_t *dev_priv = dev->dev_private;
4257
Carl Worth5e118f42009-03-20 11:54:25 -07004258 spin_lock_init(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07004259 INIT_LIST_HEAD(&dev_priv->mm.active_list);
4260 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
4261 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
4262 INIT_LIST_HEAD(&dev_priv->mm.request_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07004263 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004264 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
4265 i915_gem_retire_work_handler);
Eric Anholt673a3942008-07-30 12:06:12 -07004266 dev_priv->mm.next_gem_seqno = 1;
4267
Jesse Barnesde151cf2008-11-12 10:03:55 -08004268 /* Old X drivers will take 0-2 for front, back, depth buffers */
4269 dev_priv->fence_reg_start = 3;
4270
Jesse Barnes0f973f22009-01-26 17:10:45 -08004271 if (IS_I965G(dev) || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08004272 dev_priv->num_fence_regs = 16;
4273 else
4274 dev_priv->num_fence_regs = 8;
4275
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004276 /* Initialize fence registers to zero */
4277 if (IS_I965G(dev)) {
4278 for (i = 0; i < 16; i++)
4279 I915_WRITE64(FENCE_REG_965_0 + (i * 8), 0);
4280 } else {
4281 for (i = 0; i < 8; i++)
4282 I915_WRITE(FENCE_REG_830_0 + (i * 4), 0);
4283 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4284 for (i = 0; i < 8; i++)
4285 I915_WRITE(FENCE_REG_945_8 + (i * 4), 0);
4286 }
4287
Eric Anholt673a3942008-07-30 12:06:12 -07004288 i915_gem_detect_bit_6_swizzle(dev);
4289}
Dave Airlie71acb5e2008-12-30 20:31:46 +10004290
4291/*
4292 * Create a physically contiguous memory object for this object
4293 * e.g. for cursor + overlay regs
4294 */
4295int i915_gem_init_phys_object(struct drm_device *dev,
4296 int id, int size)
4297{
4298 drm_i915_private_t *dev_priv = dev->dev_private;
4299 struct drm_i915_gem_phys_object *phys_obj;
4300 int ret;
4301
4302 if (dev_priv->mm.phys_objs[id - 1] || !size)
4303 return 0;
4304
Eric Anholt9a298b22009-03-24 12:23:04 -07004305 phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004306 if (!phys_obj)
4307 return -ENOMEM;
4308
4309 phys_obj->id = id;
4310
4311 phys_obj->handle = drm_pci_alloc(dev, size, 0, 0xffffffff);
4312 if (!phys_obj->handle) {
4313 ret = -ENOMEM;
4314 goto kfree_obj;
4315 }
4316#ifdef CONFIG_X86
4317 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4318#endif
4319
4320 dev_priv->mm.phys_objs[id - 1] = phys_obj;
4321
4322 return 0;
4323kfree_obj:
Eric Anholt9a298b22009-03-24 12:23:04 -07004324 kfree(phys_obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004325 return ret;
4326}
4327
4328void i915_gem_free_phys_object(struct drm_device *dev, int id)
4329{
4330 drm_i915_private_t *dev_priv = dev->dev_private;
4331 struct drm_i915_gem_phys_object *phys_obj;
4332
4333 if (!dev_priv->mm.phys_objs[id - 1])
4334 return;
4335
4336 phys_obj = dev_priv->mm.phys_objs[id - 1];
4337 if (phys_obj->cur_obj) {
4338 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
4339 }
4340
4341#ifdef CONFIG_X86
4342 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4343#endif
4344 drm_pci_free(dev, phys_obj->handle);
4345 kfree(phys_obj);
4346 dev_priv->mm.phys_objs[id - 1] = NULL;
4347}
4348
4349void i915_gem_free_all_phys_object(struct drm_device *dev)
4350{
4351 int i;
4352
Dave Airlie260883c2009-01-22 17:58:49 +10004353 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004354 i915_gem_free_phys_object(dev, i);
4355}
4356
4357void i915_gem_detach_phys_object(struct drm_device *dev,
4358 struct drm_gem_object *obj)
4359{
4360 struct drm_i915_gem_object *obj_priv;
4361 int i;
4362 int ret;
4363 int page_count;
4364
4365 obj_priv = obj->driver_private;
4366 if (!obj_priv->phys_obj)
4367 return;
4368
Eric Anholt856fa192009-03-19 14:10:50 -07004369 ret = i915_gem_object_get_pages(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004370 if (ret)
4371 goto out;
4372
4373 page_count = obj->size / PAGE_SIZE;
4374
4375 for (i = 0; i < page_count; i++) {
Eric Anholt856fa192009-03-19 14:10:50 -07004376 char *dst = kmap_atomic(obj_priv->pages[i], KM_USER0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004377 char *src = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4378
4379 memcpy(dst, src, PAGE_SIZE);
4380 kunmap_atomic(dst, KM_USER0);
4381 }
Eric Anholt856fa192009-03-19 14:10:50 -07004382 drm_clflush_pages(obj_priv->pages, page_count);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004383 drm_agp_chipset_flush(dev);
Chris Wilsond78b47b2009-06-17 21:52:49 +01004384
4385 i915_gem_object_put_pages(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004386out:
4387 obj_priv->phys_obj->cur_obj = NULL;
4388 obj_priv->phys_obj = NULL;
4389}
4390
4391int
4392i915_gem_attach_phys_object(struct drm_device *dev,
4393 struct drm_gem_object *obj, int id)
4394{
4395 drm_i915_private_t *dev_priv = dev->dev_private;
4396 struct drm_i915_gem_object *obj_priv;
4397 int ret = 0;
4398 int page_count;
4399 int i;
4400
4401 if (id > I915_MAX_PHYS_OBJECT)
4402 return -EINVAL;
4403
4404 obj_priv = obj->driver_private;
4405
4406 if (obj_priv->phys_obj) {
4407 if (obj_priv->phys_obj->id == id)
4408 return 0;
4409 i915_gem_detach_phys_object(dev, obj);
4410 }
4411
4412
4413 /* create a new object */
4414 if (!dev_priv->mm.phys_objs[id - 1]) {
4415 ret = i915_gem_init_phys_object(dev, id,
4416 obj->size);
4417 if (ret) {
Linus Torvaldsaeb565d2009-01-26 10:01:53 -08004418 DRM_ERROR("failed to init phys object %d size: %zu\n", id, obj->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004419 goto out;
4420 }
4421 }
4422
4423 /* bind to the object */
4424 obj_priv->phys_obj = dev_priv->mm.phys_objs[id - 1];
4425 obj_priv->phys_obj->cur_obj = obj;
4426
Eric Anholt856fa192009-03-19 14:10:50 -07004427 ret = i915_gem_object_get_pages(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004428 if (ret) {
4429 DRM_ERROR("failed to get page list\n");
4430 goto out;
4431 }
4432
4433 page_count = obj->size / PAGE_SIZE;
4434
4435 for (i = 0; i < page_count; i++) {
Eric Anholt856fa192009-03-19 14:10:50 -07004436 char *src = kmap_atomic(obj_priv->pages[i], KM_USER0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004437 char *dst = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4438
4439 memcpy(dst, src, PAGE_SIZE);
4440 kunmap_atomic(src, KM_USER0);
4441 }
4442
Chris Wilsond78b47b2009-06-17 21:52:49 +01004443 i915_gem_object_put_pages(obj);
4444
Dave Airlie71acb5e2008-12-30 20:31:46 +10004445 return 0;
4446out:
4447 return ret;
4448}
4449
4450static int
4451i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
4452 struct drm_i915_gem_pwrite *args,
4453 struct drm_file *file_priv)
4454{
4455 struct drm_i915_gem_object *obj_priv = obj->driver_private;
4456 void *obj_addr;
4457 int ret;
4458 char __user *user_data;
4459
4460 user_data = (char __user *) (uintptr_t) args->data_ptr;
4461 obj_addr = obj_priv->phys_obj->handle->vaddr + args->offset;
4462
Dave Airliee08fb4f2009-02-25 14:52:30 +10004463 DRM_DEBUG("obj_addr %p, %lld\n", obj_addr, args->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004464 ret = copy_from_user(obj_addr, user_data, args->size);
4465 if (ret)
4466 return -EFAULT;
4467
4468 drm_agp_chipset_flush(dev);
4469 return 0;
4470}
Eric Anholtb9624422009-06-03 07:27:35 +00004471
4472void i915_gem_release(struct drm_device * dev, struct drm_file *file_priv)
4473{
4474 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
4475
4476 /* Clean up our request list when the client is going away, so that
4477 * later retire_requests won't dereference our soon-to-be-gone
4478 * file_priv.
4479 */
4480 mutex_lock(&dev->struct_mutex);
4481 while (!list_empty(&i915_file_priv->mm.request_list))
4482 list_del_init(i915_file_priv->mm.request_list.next);
4483 mutex_unlock(&dev->struct_mutex);
4484}