blob: 174aef2d64815feddc14cdc7fea57f1ae602dfc6 [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"
32#include <linux/swap.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080033#include <linux/pci.h>
Eric Anholt673a3942008-07-30 12:06:12 -070034
Eric Anholt28dfe522008-11-13 15:00:55 -080035#define I915_GEM_GPU_DOMAINS (~(I915_GEM_DOMAIN_CPU | I915_GEM_DOMAIN_GTT))
36
Eric Anholte47c68e2008-11-14 13:35:19 -080037static void i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj);
38static void i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj);
39static void i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj);
Eric Anholte47c68e2008-11-14 13:35:19 -080040static int i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj,
41 int write);
42static int i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
43 uint64_t offset,
44 uint64_t size);
45static void i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj);
Eric Anholt673a3942008-07-30 12:06:12 -070046static int i915_gem_object_wait_rendering(struct drm_gem_object *obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -080047static int i915_gem_object_bind_to_gtt(struct drm_gem_object *obj,
48 unsigned alignment);
Jesse Barnesde151cf2008-11-12 10:03:55 -080049static void i915_gem_clear_fence_reg(struct drm_gem_object *obj);
50static int i915_gem_evict_something(struct drm_device *dev);
Dave Airlie71acb5e2008-12-30 20:31:46 +100051static int i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
52 struct drm_i915_gem_pwrite *args,
53 struct drm_file *file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -070054
Jesse Barnes79e53942008-11-07 14:24:08 -080055int i915_gem_do_init(struct drm_device *dev, unsigned long start,
56 unsigned long end)
57{
58 drm_i915_private_t *dev_priv = dev->dev_private;
59
60 if (start >= end ||
61 (start & (PAGE_SIZE - 1)) != 0 ||
62 (end & (PAGE_SIZE - 1)) != 0) {
63 return -EINVAL;
64 }
65
66 drm_mm_init(&dev_priv->mm.gtt_space, start,
67 end - start);
68
69 dev->gtt_total = (uint32_t) (end - start);
70
71 return 0;
72}
Keith Packard6dbe2772008-10-14 21:41:13 -070073
Eric Anholt673a3942008-07-30 12:06:12 -070074int
75i915_gem_init_ioctl(struct drm_device *dev, void *data,
76 struct drm_file *file_priv)
77{
Eric Anholt673a3942008-07-30 12:06:12 -070078 struct drm_i915_gem_init *args = data;
Jesse Barnes79e53942008-11-07 14:24:08 -080079 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -070080
81 mutex_lock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080082 ret = i915_gem_do_init(dev, args->gtt_start, args->gtt_end);
Eric Anholt673a3942008-07-30 12:06:12 -070083 mutex_unlock(&dev->struct_mutex);
84
Jesse Barnes79e53942008-11-07 14:24:08 -080085 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -070086}
87
Eric Anholt5a125c32008-10-22 21:40:13 -070088int
89i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
90 struct drm_file *file_priv)
91{
Eric Anholt5a125c32008-10-22 21:40:13 -070092 struct drm_i915_gem_get_aperture *args = data;
Eric Anholt5a125c32008-10-22 21:40:13 -070093
94 if (!(dev->driver->driver_features & DRIVER_GEM))
95 return -ENODEV;
96
97 args->aper_size = dev->gtt_total;
Keith Packard2678d9d2008-11-20 22:54:54 -080098 args->aper_available_size = (args->aper_size -
99 atomic_read(&dev->pin_memory));
Eric Anholt5a125c32008-10-22 21:40:13 -0700100
101 return 0;
102}
103
Eric Anholt673a3942008-07-30 12:06:12 -0700104
105/**
106 * Creates a new mm object and returns a handle to it.
107 */
108int
109i915_gem_create_ioctl(struct drm_device *dev, void *data,
110 struct drm_file *file_priv)
111{
112 struct drm_i915_gem_create *args = data;
113 struct drm_gem_object *obj;
114 int handle, ret;
115
116 args->size = roundup(args->size, PAGE_SIZE);
117
118 /* Allocate the new object */
119 obj = drm_gem_object_alloc(dev, args->size);
120 if (obj == NULL)
121 return -ENOMEM;
122
123 ret = drm_gem_handle_create(file_priv, obj, &handle);
124 mutex_lock(&dev->struct_mutex);
125 drm_gem_object_handle_unreference(obj);
126 mutex_unlock(&dev->struct_mutex);
127
128 if (ret)
129 return ret;
130
131 args->handle = handle;
132
133 return 0;
134}
135
Eric Anholt40123c12009-03-09 13:42:30 -0700136static inline int
Eric Anholteb014592009-03-10 11:44:52 -0700137fast_shmem_read(struct page **pages,
138 loff_t page_base, int page_offset,
139 char __user *data,
140 int length)
141{
142 char __iomem *vaddr;
Florian Mickler2bc43b52009-04-06 22:55:41 +0200143 int unwritten;
Eric Anholteb014592009-03-10 11:44:52 -0700144
145 vaddr = kmap_atomic(pages[page_base >> PAGE_SHIFT], KM_USER0);
146 if (vaddr == NULL)
147 return -ENOMEM;
Florian Mickler2bc43b52009-04-06 22:55:41 +0200148 unwritten = __copy_to_user_inatomic(data, vaddr + page_offset, length);
Eric Anholteb014592009-03-10 11:44:52 -0700149 kunmap_atomic(vaddr, KM_USER0);
150
Florian Mickler2bc43b52009-04-06 22:55:41 +0200151 if (unwritten)
152 return -EFAULT;
153
154 return 0;
Eric Anholteb014592009-03-10 11:44:52 -0700155}
156
Eric Anholt280b7132009-03-12 16:56:27 -0700157static int i915_gem_object_needs_bit17_swizzle(struct drm_gem_object *obj)
158{
159 drm_i915_private_t *dev_priv = obj->dev->dev_private;
160 struct drm_i915_gem_object *obj_priv = obj->driver_private;
161
162 return dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_9_10_17 &&
163 obj_priv->tiling_mode != I915_TILING_NONE;
164}
165
Eric Anholteb014592009-03-10 11:44:52 -0700166static inline int
Eric Anholt40123c12009-03-09 13:42:30 -0700167slow_shmem_copy(struct page *dst_page,
168 int dst_offset,
169 struct page *src_page,
170 int src_offset,
171 int length)
172{
173 char *dst_vaddr, *src_vaddr;
174
175 dst_vaddr = kmap_atomic(dst_page, KM_USER0);
176 if (dst_vaddr == NULL)
177 return -ENOMEM;
178
179 src_vaddr = kmap_atomic(src_page, KM_USER1);
180 if (src_vaddr == NULL) {
181 kunmap_atomic(dst_vaddr, KM_USER0);
182 return -ENOMEM;
183 }
184
185 memcpy(dst_vaddr + dst_offset, src_vaddr + src_offset, length);
186
187 kunmap_atomic(src_vaddr, KM_USER1);
188 kunmap_atomic(dst_vaddr, KM_USER0);
189
190 return 0;
191}
192
Eric Anholt280b7132009-03-12 16:56:27 -0700193static inline int
194slow_shmem_bit17_copy(struct page *gpu_page,
195 int gpu_offset,
196 struct page *cpu_page,
197 int cpu_offset,
198 int length,
199 int is_read)
200{
201 char *gpu_vaddr, *cpu_vaddr;
202
203 /* Use the unswizzled path if this page isn't affected. */
204 if ((page_to_phys(gpu_page) & (1 << 17)) == 0) {
205 if (is_read)
206 return slow_shmem_copy(cpu_page, cpu_offset,
207 gpu_page, gpu_offset, length);
208 else
209 return slow_shmem_copy(gpu_page, gpu_offset,
210 cpu_page, cpu_offset, length);
211 }
212
213 gpu_vaddr = kmap_atomic(gpu_page, KM_USER0);
214 if (gpu_vaddr == NULL)
215 return -ENOMEM;
216
217 cpu_vaddr = kmap_atomic(cpu_page, KM_USER1);
218 if (cpu_vaddr == NULL) {
219 kunmap_atomic(gpu_vaddr, KM_USER0);
220 return -ENOMEM;
221 }
222
223 /* Copy the data, XORing A6 with A17 (1). The user already knows he's
224 * XORing with the other bits (A9 for Y, A9 and A10 for X)
225 */
226 while (length > 0) {
227 int cacheline_end = ALIGN(gpu_offset + 1, 64);
228 int this_length = min(cacheline_end - gpu_offset, length);
229 int swizzled_gpu_offset = gpu_offset ^ 64;
230
231 if (is_read) {
232 memcpy(cpu_vaddr + cpu_offset,
233 gpu_vaddr + swizzled_gpu_offset,
234 this_length);
235 } else {
236 memcpy(gpu_vaddr + swizzled_gpu_offset,
237 cpu_vaddr + cpu_offset,
238 this_length);
239 }
240 cpu_offset += this_length;
241 gpu_offset += this_length;
242 length -= this_length;
243 }
244
245 kunmap_atomic(cpu_vaddr, KM_USER1);
246 kunmap_atomic(gpu_vaddr, KM_USER0);
247
248 return 0;
249}
250
Eric Anholt673a3942008-07-30 12:06:12 -0700251/**
Eric Anholteb014592009-03-10 11:44:52 -0700252 * This is the fast shmem pread path, which attempts to copy_from_user directly
253 * from the backing pages of the object to the user's address space. On a
254 * fault, it fails so we can fall back to i915_gem_shmem_pwrite_slow().
255 */
256static int
257i915_gem_shmem_pread_fast(struct drm_device *dev, struct drm_gem_object *obj,
258 struct drm_i915_gem_pread *args,
259 struct drm_file *file_priv)
260{
261 struct drm_i915_gem_object *obj_priv = obj->driver_private;
262 ssize_t remain;
263 loff_t offset, page_base;
264 char __user *user_data;
265 int page_offset, page_length;
266 int ret;
267
268 user_data = (char __user *) (uintptr_t) args->data_ptr;
269 remain = args->size;
270
271 mutex_lock(&dev->struct_mutex);
272
273 ret = i915_gem_object_get_pages(obj);
274 if (ret != 0)
275 goto fail_unlock;
276
277 ret = i915_gem_object_set_cpu_read_domain_range(obj, args->offset,
278 args->size);
279 if (ret != 0)
280 goto fail_put_pages;
281
282 obj_priv = obj->driver_private;
283 offset = args->offset;
284
285 while (remain > 0) {
286 /* Operation in this page
287 *
288 * page_base = page offset within aperture
289 * page_offset = offset within page
290 * page_length = bytes to copy for this page
291 */
292 page_base = (offset & ~(PAGE_SIZE-1));
293 page_offset = offset & (PAGE_SIZE-1);
294 page_length = remain;
295 if ((page_offset + remain) > PAGE_SIZE)
296 page_length = PAGE_SIZE - page_offset;
297
298 ret = fast_shmem_read(obj_priv->pages,
299 page_base, page_offset,
300 user_data, page_length);
301 if (ret)
302 goto fail_put_pages;
303
304 remain -= page_length;
305 user_data += page_length;
306 offset += page_length;
307 }
308
309fail_put_pages:
310 i915_gem_object_put_pages(obj);
311fail_unlock:
312 mutex_unlock(&dev->struct_mutex);
313
314 return ret;
315}
316
317/**
318 * This is the fallback shmem pread path, which allocates temporary storage
319 * in kernel space to copy_to_user into outside of the struct_mutex, so we
320 * can copy out of the object's backing pages while holding the struct mutex
321 * and not take page faults.
322 */
323static int
324i915_gem_shmem_pread_slow(struct drm_device *dev, struct drm_gem_object *obj,
325 struct drm_i915_gem_pread *args,
326 struct drm_file *file_priv)
327{
328 struct drm_i915_gem_object *obj_priv = obj->driver_private;
329 struct mm_struct *mm = current->mm;
330 struct page **user_pages;
331 ssize_t remain;
332 loff_t offset, pinned_pages, i;
333 loff_t first_data_page, last_data_page, num_pages;
334 int shmem_page_index, shmem_page_offset;
335 int data_page_index, data_page_offset;
336 int page_length;
337 int ret;
338 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700339 int do_bit17_swizzling;
Eric Anholteb014592009-03-10 11:44:52 -0700340
341 remain = args->size;
342
343 /* Pin the user pages containing the data. We can't fault while
344 * holding the struct mutex, yet we want to hold it while
345 * dereferencing the user data.
346 */
347 first_data_page = data_ptr / PAGE_SIZE;
348 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
349 num_pages = last_data_page - first_data_page + 1;
350
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700351 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
Eric Anholteb014592009-03-10 11:44:52 -0700352 if (user_pages == NULL)
353 return -ENOMEM;
354
355 down_read(&mm->mmap_sem);
356 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
Eric Anholte5e9ecd2009-04-07 16:01:22 -0700357 num_pages, 1, 0, user_pages, NULL);
Eric Anholteb014592009-03-10 11:44:52 -0700358 up_read(&mm->mmap_sem);
359 if (pinned_pages < num_pages) {
360 ret = -EFAULT;
361 goto fail_put_user_pages;
362 }
363
Eric Anholt280b7132009-03-12 16:56:27 -0700364 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
365
Eric Anholteb014592009-03-10 11:44:52 -0700366 mutex_lock(&dev->struct_mutex);
367
368 ret = i915_gem_object_get_pages(obj);
369 if (ret != 0)
370 goto fail_unlock;
371
372 ret = i915_gem_object_set_cpu_read_domain_range(obj, args->offset,
373 args->size);
374 if (ret != 0)
375 goto fail_put_pages;
376
377 obj_priv = obj->driver_private;
378 offset = args->offset;
379
380 while (remain > 0) {
381 /* Operation in this page
382 *
383 * shmem_page_index = page number within shmem file
384 * shmem_page_offset = offset within page in shmem file
385 * data_page_index = page number in get_user_pages return
386 * data_page_offset = offset with data_page_index page.
387 * page_length = bytes to copy for this page
388 */
389 shmem_page_index = offset / PAGE_SIZE;
390 shmem_page_offset = offset & ~PAGE_MASK;
391 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
392 data_page_offset = data_ptr & ~PAGE_MASK;
393
394 page_length = remain;
395 if ((shmem_page_offset + page_length) > PAGE_SIZE)
396 page_length = PAGE_SIZE - shmem_page_offset;
397 if ((data_page_offset + page_length) > PAGE_SIZE)
398 page_length = PAGE_SIZE - data_page_offset;
399
Eric Anholt280b7132009-03-12 16:56:27 -0700400 if (do_bit17_swizzling) {
401 ret = slow_shmem_bit17_copy(obj_priv->pages[shmem_page_index],
402 shmem_page_offset,
403 user_pages[data_page_index],
404 data_page_offset,
405 page_length,
406 1);
407 } else {
408 ret = slow_shmem_copy(user_pages[data_page_index],
409 data_page_offset,
410 obj_priv->pages[shmem_page_index],
411 shmem_page_offset,
412 page_length);
413 }
Eric Anholteb014592009-03-10 11:44:52 -0700414 if (ret)
415 goto fail_put_pages;
416
417 remain -= page_length;
418 data_ptr += page_length;
419 offset += page_length;
420 }
421
422fail_put_pages:
423 i915_gem_object_put_pages(obj);
424fail_unlock:
425 mutex_unlock(&dev->struct_mutex);
426fail_put_user_pages:
427 for (i = 0; i < pinned_pages; i++) {
428 SetPageDirty(user_pages[i]);
429 page_cache_release(user_pages[i]);
430 }
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700431 drm_free_large(user_pages);
Eric Anholteb014592009-03-10 11:44:52 -0700432
433 return ret;
434}
435
Eric Anholt673a3942008-07-30 12:06:12 -0700436/**
437 * Reads data from the object referenced by handle.
438 *
439 * On error, the contents of *data are undefined.
440 */
441int
442i915_gem_pread_ioctl(struct drm_device *dev, void *data,
443 struct drm_file *file_priv)
444{
445 struct drm_i915_gem_pread *args = data;
446 struct drm_gem_object *obj;
447 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -0700448 int ret;
449
450 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
451 if (obj == NULL)
452 return -EBADF;
453 obj_priv = obj->driver_private;
454
455 /* Bounds check source.
456 *
457 * XXX: This could use review for overflow issues...
458 */
459 if (args->offset > obj->size || args->size > obj->size ||
460 args->offset + args->size > obj->size) {
461 drm_gem_object_unreference(obj);
462 return -EINVAL;
463 }
464
Eric Anholt280b7132009-03-12 16:56:27 -0700465 if (i915_gem_object_needs_bit17_swizzle(obj)) {
Eric Anholteb014592009-03-10 11:44:52 -0700466 ret = i915_gem_shmem_pread_slow(dev, obj, args, file_priv);
Eric Anholt280b7132009-03-12 16:56:27 -0700467 } else {
468 ret = i915_gem_shmem_pread_fast(dev, obj, args, file_priv);
469 if (ret != 0)
470 ret = i915_gem_shmem_pread_slow(dev, obj, args,
471 file_priv);
472 }
Eric Anholt673a3942008-07-30 12:06:12 -0700473
474 drm_gem_object_unreference(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700475
Eric Anholteb014592009-03-10 11:44:52 -0700476 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700477}
478
Keith Packard0839ccb2008-10-30 19:38:48 -0700479/* This is the fast write path which cannot handle
480 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700481 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700482
Keith Packard0839ccb2008-10-30 19:38:48 -0700483static inline int
484fast_user_write(struct io_mapping *mapping,
485 loff_t page_base, int page_offset,
486 char __user *user_data,
487 int length)
488{
489 char *vaddr_atomic;
490 unsigned long unwritten;
491
492 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
493 unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
494 user_data, length);
495 io_mapping_unmap_atomic(vaddr_atomic);
496 if (unwritten)
497 return -EFAULT;
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700498 return 0;
Keith Packard0839ccb2008-10-30 19:38:48 -0700499}
500
501/* Here's the write path which can sleep for
502 * page faults
503 */
504
505static inline int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700506slow_kernel_write(struct io_mapping *mapping,
507 loff_t gtt_base, int gtt_offset,
508 struct page *user_page, int user_offset,
509 int length)
Keith Packard0839ccb2008-10-30 19:38:48 -0700510{
Eric Anholt3de09aa2009-03-09 09:42:23 -0700511 char *src_vaddr, *dst_vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -0700512 unsigned long unwritten;
513
Eric Anholt3de09aa2009-03-09 09:42:23 -0700514 dst_vaddr = io_mapping_map_atomic_wc(mapping, gtt_base);
515 src_vaddr = kmap_atomic(user_page, KM_USER1);
516 unwritten = __copy_from_user_inatomic_nocache(dst_vaddr + gtt_offset,
517 src_vaddr + user_offset,
518 length);
519 kunmap_atomic(src_vaddr, KM_USER1);
520 io_mapping_unmap_atomic(dst_vaddr);
Keith Packard0839ccb2008-10-30 19:38:48 -0700521 if (unwritten)
522 return -EFAULT;
523 return 0;
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700524}
525
Eric Anholt40123c12009-03-09 13:42:30 -0700526static inline int
527fast_shmem_write(struct page **pages,
528 loff_t page_base, int page_offset,
529 char __user *data,
530 int length)
531{
532 char __iomem *vaddr;
Dave Airlied0088772009-03-28 20:29:48 -0400533 unsigned long unwritten;
Eric Anholt40123c12009-03-09 13:42:30 -0700534
535 vaddr = kmap_atomic(pages[page_base >> PAGE_SHIFT], KM_USER0);
536 if (vaddr == NULL)
537 return -ENOMEM;
Dave Airlied0088772009-03-28 20:29:48 -0400538 unwritten = __copy_from_user_inatomic(vaddr + page_offset, data, length);
Eric Anholt40123c12009-03-09 13:42:30 -0700539 kunmap_atomic(vaddr, KM_USER0);
540
Dave Airlied0088772009-03-28 20:29:48 -0400541 if (unwritten)
542 return -EFAULT;
Eric Anholt40123c12009-03-09 13:42:30 -0700543 return 0;
544}
545
Eric Anholt3de09aa2009-03-09 09:42:23 -0700546/**
547 * This is the fast pwrite path, where we copy the data directly from the
548 * user into the GTT, uncached.
549 */
Eric Anholt673a3942008-07-30 12:06:12 -0700550static int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700551i915_gem_gtt_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
552 struct drm_i915_gem_pwrite *args,
553 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700554{
555 struct drm_i915_gem_object *obj_priv = obj->driver_private;
Keith Packard0839ccb2008-10-30 19:38:48 -0700556 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700557 ssize_t remain;
Keith Packard0839ccb2008-10-30 19:38:48 -0700558 loff_t offset, page_base;
Eric Anholt673a3942008-07-30 12:06:12 -0700559 char __user *user_data;
Keith Packard0839ccb2008-10-30 19:38:48 -0700560 int page_offset, page_length;
561 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700562
563 user_data = (char __user *) (uintptr_t) args->data_ptr;
564 remain = args->size;
565 if (!access_ok(VERIFY_READ, user_data, remain))
566 return -EFAULT;
567
568
569 mutex_lock(&dev->struct_mutex);
570 ret = i915_gem_object_pin(obj, 0);
571 if (ret) {
572 mutex_unlock(&dev->struct_mutex);
573 return ret;
574 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800575 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
Eric Anholt673a3942008-07-30 12:06:12 -0700576 if (ret)
577 goto fail;
578
579 obj_priv = obj->driver_private;
580 offset = obj_priv->gtt_offset + args->offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700581
582 while (remain > 0) {
583 /* Operation in this page
584 *
Keith Packard0839ccb2008-10-30 19:38:48 -0700585 * page_base = page offset within aperture
586 * page_offset = offset within page
587 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -0700588 */
Keith Packard0839ccb2008-10-30 19:38:48 -0700589 page_base = (offset & ~(PAGE_SIZE-1));
590 page_offset = offset & (PAGE_SIZE-1);
591 page_length = remain;
592 if ((page_offset + remain) > PAGE_SIZE)
593 page_length = PAGE_SIZE - page_offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700594
Keith Packard0839ccb2008-10-30 19:38:48 -0700595 ret = fast_user_write (dev_priv->mm.gtt_mapping, page_base,
596 page_offset, user_data, page_length);
Eric Anholt673a3942008-07-30 12:06:12 -0700597
Keith Packard0839ccb2008-10-30 19:38:48 -0700598 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -0700599 * source page isn't available. Return the error and we'll
600 * retry in the slow path.
Keith Packard0839ccb2008-10-30 19:38:48 -0700601 */
Eric Anholt3de09aa2009-03-09 09:42:23 -0700602 if (ret)
603 goto fail;
Eric Anholt673a3942008-07-30 12:06:12 -0700604
Keith Packard0839ccb2008-10-30 19:38:48 -0700605 remain -= page_length;
606 user_data += page_length;
607 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700608 }
Eric Anholt673a3942008-07-30 12:06:12 -0700609
610fail:
611 i915_gem_object_unpin(obj);
612 mutex_unlock(&dev->struct_mutex);
613
614 return ret;
615}
616
Eric Anholt3de09aa2009-03-09 09:42:23 -0700617/**
618 * This is the fallback GTT pwrite path, which uses get_user_pages to pin
619 * the memory and maps it using kmap_atomic for copying.
620 *
621 * This code resulted in x11perf -rgb10text consuming about 10% more CPU
622 * than using i915_gem_gtt_pwrite_fast on a G45 (32-bit).
623 */
Eric Anholt3043c602008-10-02 12:24:47 -0700624static int
Eric Anholt3de09aa2009-03-09 09:42:23 -0700625i915_gem_gtt_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
626 struct drm_i915_gem_pwrite *args,
627 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700628{
Eric Anholt3de09aa2009-03-09 09:42:23 -0700629 struct drm_i915_gem_object *obj_priv = obj->driver_private;
630 drm_i915_private_t *dev_priv = dev->dev_private;
631 ssize_t remain;
632 loff_t gtt_page_base, offset;
633 loff_t first_data_page, last_data_page, num_pages;
634 loff_t pinned_pages, i;
635 struct page **user_pages;
636 struct mm_struct *mm = current->mm;
637 int gtt_page_offset, data_page_offset, data_page_index, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700638 int ret;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700639 uint64_t data_ptr = args->data_ptr;
640
641 remain = args->size;
642
643 /* Pin the user pages containing the data. We can't fault while
644 * holding the struct mutex, and all of the pwrite implementations
645 * want to hold it while dereferencing the user data.
646 */
647 first_data_page = data_ptr / PAGE_SIZE;
648 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
649 num_pages = last_data_page - first_data_page + 1;
650
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700651 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
Eric Anholt3de09aa2009-03-09 09:42:23 -0700652 if (user_pages == NULL)
653 return -ENOMEM;
654
655 down_read(&mm->mmap_sem);
656 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
657 num_pages, 0, 0, user_pages, NULL);
658 up_read(&mm->mmap_sem);
659 if (pinned_pages < num_pages) {
660 ret = -EFAULT;
661 goto out_unpin_pages;
662 }
663
664 mutex_lock(&dev->struct_mutex);
665 ret = i915_gem_object_pin(obj, 0);
666 if (ret)
667 goto out_unlock;
668
669 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
670 if (ret)
671 goto out_unpin_object;
672
673 obj_priv = obj->driver_private;
674 offset = obj_priv->gtt_offset + args->offset;
675
676 while (remain > 0) {
677 /* Operation in this page
678 *
679 * gtt_page_base = page offset within aperture
680 * gtt_page_offset = offset within page in aperture
681 * data_page_index = page number in get_user_pages return
682 * data_page_offset = offset with data_page_index page.
683 * page_length = bytes to copy for this page
684 */
685 gtt_page_base = offset & PAGE_MASK;
686 gtt_page_offset = offset & ~PAGE_MASK;
687 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
688 data_page_offset = data_ptr & ~PAGE_MASK;
689
690 page_length = remain;
691 if ((gtt_page_offset + page_length) > PAGE_SIZE)
692 page_length = PAGE_SIZE - gtt_page_offset;
693 if ((data_page_offset + page_length) > PAGE_SIZE)
694 page_length = PAGE_SIZE - data_page_offset;
695
696 ret = slow_kernel_write(dev_priv->mm.gtt_mapping,
697 gtt_page_base, gtt_page_offset,
698 user_pages[data_page_index],
699 data_page_offset,
700 page_length);
701
702 /* If we get a fault while copying data, then (presumably) our
703 * source page isn't available. Return the error and we'll
704 * retry in the slow path.
705 */
706 if (ret)
707 goto out_unpin_object;
708
709 remain -= page_length;
710 offset += page_length;
711 data_ptr += page_length;
712 }
713
714out_unpin_object:
715 i915_gem_object_unpin(obj);
716out_unlock:
717 mutex_unlock(&dev->struct_mutex);
718out_unpin_pages:
719 for (i = 0; i < pinned_pages; i++)
720 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700721 drm_free_large(user_pages);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700722
723 return ret;
724}
725
Eric Anholt40123c12009-03-09 13:42:30 -0700726/**
727 * This is the fast shmem pwrite path, which attempts to directly
728 * copy_from_user into the kmapped pages backing the object.
729 */
Eric Anholt673a3942008-07-30 12:06:12 -0700730static int
Eric Anholt40123c12009-03-09 13:42:30 -0700731i915_gem_shmem_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
732 struct drm_i915_gem_pwrite *args,
733 struct drm_file *file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -0700734{
Eric Anholt40123c12009-03-09 13:42:30 -0700735 struct drm_i915_gem_object *obj_priv = obj->driver_private;
736 ssize_t remain;
737 loff_t offset, page_base;
738 char __user *user_data;
739 int page_offset, page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700740 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -0700741
742 user_data = (char __user *) (uintptr_t) args->data_ptr;
743 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700744
745 mutex_lock(&dev->struct_mutex);
746
Eric Anholt40123c12009-03-09 13:42:30 -0700747 ret = i915_gem_object_get_pages(obj);
748 if (ret != 0)
749 goto fail_unlock;
750
Eric Anholte47c68e2008-11-14 13:35:19 -0800751 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Eric Anholt40123c12009-03-09 13:42:30 -0700752 if (ret != 0)
753 goto fail_put_pages;
Eric Anholt673a3942008-07-30 12:06:12 -0700754
Eric Anholt40123c12009-03-09 13:42:30 -0700755 obj_priv = obj->driver_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700756 offset = args->offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700757 obj_priv->dirty = 1;
Eric Anholt673a3942008-07-30 12:06:12 -0700758
Eric Anholt40123c12009-03-09 13:42:30 -0700759 while (remain > 0) {
760 /* Operation in this page
761 *
762 * page_base = page offset within aperture
763 * page_offset = offset within page
764 * page_length = bytes to copy for this page
765 */
766 page_base = (offset & ~(PAGE_SIZE-1));
767 page_offset = offset & (PAGE_SIZE-1);
768 page_length = remain;
769 if ((page_offset + remain) > PAGE_SIZE)
770 page_length = PAGE_SIZE - page_offset;
771
772 ret = fast_shmem_write(obj_priv->pages,
773 page_base, page_offset,
774 user_data, page_length);
775 if (ret)
776 goto fail_put_pages;
777
778 remain -= page_length;
779 user_data += page_length;
780 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700781 }
782
Eric Anholt40123c12009-03-09 13:42:30 -0700783fail_put_pages:
784 i915_gem_object_put_pages(obj);
785fail_unlock:
Eric Anholt673a3942008-07-30 12:06:12 -0700786 mutex_unlock(&dev->struct_mutex);
787
Eric Anholt40123c12009-03-09 13:42:30 -0700788 return ret;
789}
790
791/**
792 * This is the fallback shmem pwrite path, which uses get_user_pages to pin
793 * the memory and maps it using kmap_atomic for copying.
794 *
795 * This avoids taking mmap_sem for faulting on the user's address while the
796 * struct_mutex is held.
797 */
798static int
799i915_gem_shmem_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
800 struct drm_i915_gem_pwrite *args,
801 struct drm_file *file_priv)
802{
803 struct drm_i915_gem_object *obj_priv = obj->driver_private;
804 struct mm_struct *mm = current->mm;
805 struct page **user_pages;
806 ssize_t remain;
807 loff_t offset, pinned_pages, i;
808 loff_t first_data_page, last_data_page, num_pages;
809 int shmem_page_index, shmem_page_offset;
810 int data_page_index, data_page_offset;
811 int page_length;
812 int ret;
813 uint64_t data_ptr = args->data_ptr;
Eric Anholt280b7132009-03-12 16:56:27 -0700814 int do_bit17_swizzling;
Eric Anholt40123c12009-03-09 13:42:30 -0700815
816 remain = args->size;
817
818 /* Pin the user pages containing the data. We can't fault while
819 * holding the struct mutex, and all of the pwrite implementations
820 * want to hold it while dereferencing the user data.
821 */
822 first_data_page = data_ptr / PAGE_SIZE;
823 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
824 num_pages = last_data_page - first_data_page + 1;
825
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700826 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
Eric Anholt40123c12009-03-09 13:42:30 -0700827 if (user_pages == NULL)
828 return -ENOMEM;
829
830 down_read(&mm->mmap_sem);
831 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
832 num_pages, 0, 0, user_pages, NULL);
833 up_read(&mm->mmap_sem);
834 if (pinned_pages < num_pages) {
835 ret = -EFAULT;
836 goto fail_put_user_pages;
837 }
838
Eric Anholt280b7132009-03-12 16:56:27 -0700839 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
840
Eric Anholt40123c12009-03-09 13:42:30 -0700841 mutex_lock(&dev->struct_mutex);
842
843 ret = i915_gem_object_get_pages(obj);
844 if (ret != 0)
845 goto fail_unlock;
846
847 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
848 if (ret != 0)
849 goto fail_put_pages;
850
851 obj_priv = obj->driver_private;
852 offset = args->offset;
853 obj_priv->dirty = 1;
854
855 while (remain > 0) {
856 /* Operation in this page
857 *
858 * shmem_page_index = page number within shmem file
859 * shmem_page_offset = offset within page in shmem file
860 * data_page_index = page number in get_user_pages return
861 * data_page_offset = offset with data_page_index page.
862 * page_length = bytes to copy for this page
863 */
864 shmem_page_index = offset / PAGE_SIZE;
865 shmem_page_offset = offset & ~PAGE_MASK;
866 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
867 data_page_offset = data_ptr & ~PAGE_MASK;
868
869 page_length = remain;
870 if ((shmem_page_offset + page_length) > PAGE_SIZE)
871 page_length = PAGE_SIZE - shmem_page_offset;
872 if ((data_page_offset + page_length) > PAGE_SIZE)
873 page_length = PAGE_SIZE - data_page_offset;
874
Eric Anholt280b7132009-03-12 16:56:27 -0700875 if (do_bit17_swizzling) {
876 ret = slow_shmem_bit17_copy(obj_priv->pages[shmem_page_index],
877 shmem_page_offset,
878 user_pages[data_page_index],
879 data_page_offset,
880 page_length,
881 0);
882 } else {
883 ret = slow_shmem_copy(obj_priv->pages[shmem_page_index],
884 shmem_page_offset,
885 user_pages[data_page_index],
886 data_page_offset,
887 page_length);
888 }
Eric Anholt40123c12009-03-09 13:42:30 -0700889 if (ret)
890 goto fail_put_pages;
891
892 remain -= page_length;
893 data_ptr += page_length;
894 offset += page_length;
895 }
896
897fail_put_pages:
898 i915_gem_object_put_pages(obj);
899fail_unlock:
900 mutex_unlock(&dev->struct_mutex);
901fail_put_user_pages:
902 for (i = 0; i < pinned_pages; i++)
903 page_cache_release(user_pages[i]);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -0700904 drm_free_large(user_pages);
Eric Anholt40123c12009-03-09 13:42:30 -0700905
906 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700907}
908
909/**
910 * Writes data to the object referenced by handle.
911 *
912 * On error, the contents of the buffer that were to be modified are undefined.
913 */
914int
915i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
916 struct drm_file *file_priv)
917{
918 struct drm_i915_gem_pwrite *args = data;
919 struct drm_gem_object *obj;
920 struct drm_i915_gem_object *obj_priv;
921 int ret = 0;
922
923 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
924 if (obj == NULL)
925 return -EBADF;
926 obj_priv = obj->driver_private;
927
928 /* Bounds check destination.
929 *
930 * XXX: This could use review for overflow issues...
931 */
932 if (args->offset > obj->size || args->size > obj->size ||
933 args->offset + args->size > obj->size) {
934 drm_gem_object_unreference(obj);
935 return -EINVAL;
936 }
937
938 /* We can only do the GTT pwrite on untiled buffers, as otherwise
939 * it would end up going through the fenced access, and we'll get
940 * different detiling behavior between reading and writing.
941 * pread/pwrite currently are reading and writing from the CPU
942 * perspective, requiring manual detiling by the client.
943 */
Dave Airlie71acb5e2008-12-30 20:31:46 +1000944 if (obj_priv->phys_obj)
945 ret = i915_gem_phys_pwrite(dev, obj, args, file_priv);
946 else if (obj_priv->tiling_mode == I915_TILING_NONE &&
Eric Anholt3de09aa2009-03-09 09:42:23 -0700947 dev->gtt_total != 0) {
948 ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file_priv);
949 if (ret == -EFAULT) {
950 ret = i915_gem_gtt_pwrite_slow(dev, obj, args,
951 file_priv);
952 }
Eric Anholt280b7132009-03-12 16:56:27 -0700953 } else if (i915_gem_object_needs_bit17_swizzle(obj)) {
954 ret = i915_gem_shmem_pwrite_slow(dev, obj, args, file_priv);
Eric Anholt40123c12009-03-09 13:42:30 -0700955 } else {
956 ret = i915_gem_shmem_pwrite_fast(dev, obj, args, file_priv);
957 if (ret == -EFAULT) {
958 ret = i915_gem_shmem_pwrite_slow(dev, obj, args,
959 file_priv);
960 }
961 }
Eric Anholt673a3942008-07-30 12:06:12 -0700962
963#if WATCH_PWRITE
964 if (ret)
965 DRM_INFO("pwrite failed %d\n", ret);
966#endif
967
968 drm_gem_object_unreference(obj);
969
970 return ret;
971}
972
973/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800974 * Called when user space prepares to use an object with the CPU, either
975 * through the mmap ioctl's mapping or a GTT mapping.
Eric Anholt673a3942008-07-30 12:06:12 -0700976 */
977int
978i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
979 struct drm_file *file_priv)
980{
981 struct drm_i915_gem_set_domain *args = data;
982 struct drm_gem_object *obj;
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800983 uint32_t read_domains = args->read_domains;
984 uint32_t write_domain = args->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -0700985 int ret;
986
987 if (!(dev->driver->driver_features & DRIVER_GEM))
988 return -ENODEV;
989
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800990 /* Only handle setting domains to types used by the CPU. */
Chris Wilson21d509e2009-06-06 09:46:02 +0100991 if (write_domain & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800992 return -EINVAL;
993
Chris Wilson21d509e2009-06-06 09:46:02 +0100994 if (read_domains & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800995 return -EINVAL;
996
997 /* Having something in the write domain implies it's in the read
998 * domain, and only that read domain. Enforce that in the request.
999 */
1000 if (write_domain != 0 && read_domains != write_domain)
1001 return -EINVAL;
1002
Eric Anholt673a3942008-07-30 12:06:12 -07001003 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1004 if (obj == NULL)
1005 return -EBADF;
1006
1007 mutex_lock(&dev->struct_mutex);
1008#if WATCH_BUF
1009 DRM_INFO("set_domain_ioctl %p(%d), %08x %08x\n",
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001010 obj, obj->size, read_domains, write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07001011#endif
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001012 if (read_domains & I915_GEM_DOMAIN_GTT) {
1013 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
Eric Anholt02354392008-11-26 13:58:13 -08001014
1015 /* Silently promote "you're not bound, there was nothing to do"
1016 * to success, since the client was just asking us to
1017 * make sure everything was done.
1018 */
1019 if (ret == -EINVAL)
1020 ret = 0;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001021 } else {
Eric Anholte47c68e2008-11-14 13:35:19 -08001022 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001023 }
1024
Eric Anholt673a3942008-07-30 12:06:12 -07001025 drm_gem_object_unreference(obj);
1026 mutex_unlock(&dev->struct_mutex);
1027 return ret;
1028}
1029
1030/**
1031 * Called when user space has done writes to this buffer
1032 */
1033int
1034i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
1035 struct drm_file *file_priv)
1036{
1037 struct drm_i915_gem_sw_finish *args = data;
1038 struct drm_gem_object *obj;
1039 struct drm_i915_gem_object *obj_priv;
1040 int ret = 0;
1041
1042 if (!(dev->driver->driver_features & DRIVER_GEM))
1043 return -ENODEV;
1044
1045 mutex_lock(&dev->struct_mutex);
1046 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1047 if (obj == NULL) {
1048 mutex_unlock(&dev->struct_mutex);
1049 return -EBADF;
1050 }
1051
1052#if WATCH_BUF
1053 DRM_INFO("%s: sw_finish %d (%p %d)\n",
1054 __func__, args->handle, obj, obj->size);
1055#endif
1056 obj_priv = obj->driver_private;
1057
1058 /* Pinned buffers may be scanout, so flush the cache */
Eric Anholte47c68e2008-11-14 13:35:19 -08001059 if (obj_priv->pin_count)
1060 i915_gem_object_flush_cpu_write_domain(obj);
1061
Eric Anholt673a3942008-07-30 12:06:12 -07001062 drm_gem_object_unreference(obj);
1063 mutex_unlock(&dev->struct_mutex);
1064 return ret;
1065}
1066
1067/**
1068 * Maps the contents of an object, returning the address it is mapped
1069 * into.
1070 *
1071 * While the mapping holds a reference on the contents of the object, it doesn't
1072 * imply a ref on the object itself.
1073 */
1074int
1075i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
1076 struct drm_file *file_priv)
1077{
1078 struct drm_i915_gem_mmap *args = data;
1079 struct drm_gem_object *obj;
1080 loff_t offset;
1081 unsigned long addr;
1082
1083 if (!(dev->driver->driver_features & DRIVER_GEM))
1084 return -ENODEV;
1085
1086 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1087 if (obj == NULL)
1088 return -EBADF;
1089
1090 offset = args->offset;
1091
1092 down_write(&current->mm->mmap_sem);
1093 addr = do_mmap(obj->filp, 0, args->size,
1094 PROT_READ | PROT_WRITE, MAP_SHARED,
1095 args->offset);
1096 up_write(&current->mm->mmap_sem);
1097 mutex_lock(&dev->struct_mutex);
1098 drm_gem_object_unreference(obj);
1099 mutex_unlock(&dev->struct_mutex);
1100 if (IS_ERR((void *)addr))
1101 return addr;
1102
1103 args->addr_ptr = (uint64_t) addr;
1104
1105 return 0;
1106}
1107
Jesse Barnesde151cf2008-11-12 10:03:55 -08001108/**
1109 * i915_gem_fault - fault a page into the GTT
1110 * vma: VMA in question
1111 * vmf: fault info
1112 *
1113 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1114 * from userspace. The fault handler takes care of binding the object to
1115 * the GTT (if needed), allocating and programming a fence register (again,
1116 * only if needed based on whether the old reg is still valid or the object
1117 * is tiled) and inserting a new PTE into the faulting process.
1118 *
1119 * Note that the faulting process may involve evicting existing objects
1120 * from the GTT and/or fence registers to make room. So performance may
1121 * suffer if the GTT working set is large or there are few fence registers
1122 * left.
1123 */
1124int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1125{
1126 struct drm_gem_object *obj = vma->vm_private_data;
1127 struct drm_device *dev = obj->dev;
1128 struct drm_i915_private *dev_priv = dev->dev_private;
1129 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1130 pgoff_t page_offset;
1131 unsigned long pfn;
1132 int ret = 0;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001133 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001134
1135 /* We don't use vmf->pgoff since that has the fake offset */
1136 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1137 PAGE_SHIFT;
1138
1139 /* Now bind it into the GTT if needed */
1140 mutex_lock(&dev->struct_mutex);
1141 if (!obj_priv->gtt_space) {
1142 ret = i915_gem_object_bind_to_gtt(obj, obj_priv->gtt_alignment);
1143 if (ret) {
1144 mutex_unlock(&dev->struct_mutex);
1145 return VM_FAULT_SIGBUS;
1146 }
Kristian Høgsberg07f4f3e2009-05-27 14:37:28 -04001147
1148 ret = i915_gem_object_set_to_gtt_domain(obj, write);
1149 if (ret) {
1150 mutex_unlock(&dev->struct_mutex);
1151 return VM_FAULT_SIGBUS;
1152 }
1153
Jesse Barnes14b60392009-05-20 16:47:08 -04001154 list_add_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001155 }
1156
1157 /* Need a new fence register? */
1158 if (obj_priv->fence_reg == I915_FENCE_REG_NONE &&
Eric Anholtd9ddcb92009-01-27 10:33:49 -08001159 obj_priv->tiling_mode != I915_TILING_NONE) {
Chris Wilson8c4b8c32009-06-17 22:08:52 +01001160 ret = i915_gem_object_get_fence_reg(obj);
Chris Wilson7d8d58b2009-02-04 14:15:10 +00001161 if (ret) {
1162 mutex_unlock(&dev->struct_mutex);
Eric Anholtd9ddcb92009-01-27 10:33:49 -08001163 return VM_FAULT_SIGBUS;
Chris Wilson7d8d58b2009-02-04 14:15:10 +00001164 }
Eric Anholtd9ddcb92009-01-27 10:33:49 -08001165 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001166
1167 pfn = ((dev->agp->base + obj_priv->gtt_offset) >> PAGE_SHIFT) +
1168 page_offset;
1169
1170 /* Finally, remap it using the new GTT offset */
1171 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
1172
1173 mutex_unlock(&dev->struct_mutex);
1174
1175 switch (ret) {
1176 case -ENOMEM:
1177 case -EAGAIN:
1178 return VM_FAULT_OOM;
1179 case -EFAULT:
Jesse Barnes959b8872009-03-20 14:16:33 -07001180 case -EINVAL:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001181 return VM_FAULT_SIGBUS;
1182 default:
1183 return VM_FAULT_NOPAGE;
1184 }
1185}
1186
1187/**
1188 * i915_gem_create_mmap_offset - create a fake mmap offset for an object
1189 * @obj: obj in question
1190 *
1191 * GEM memory mapping works by handing back to userspace a fake mmap offset
1192 * it can use in a subsequent mmap(2) call. The DRM core code then looks
1193 * up the object based on the offset and sets up the various memory mapping
1194 * structures.
1195 *
1196 * This routine allocates and attaches a fake offset for @obj.
1197 */
1198static int
1199i915_gem_create_mmap_offset(struct drm_gem_object *obj)
1200{
1201 struct drm_device *dev = obj->dev;
1202 struct drm_gem_mm *mm = dev->mm_private;
1203 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1204 struct drm_map_list *list;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +11001205 struct drm_local_map *map;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001206 int ret = 0;
1207
1208 /* Set the object up for mmap'ing */
1209 list = &obj->map_list;
1210 list->map = drm_calloc(1, sizeof(struct drm_map_list),
1211 DRM_MEM_DRIVER);
1212 if (!list->map)
1213 return -ENOMEM;
1214
1215 map = list->map;
1216 map->type = _DRM_GEM;
1217 map->size = obj->size;
1218 map->handle = obj;
1219
1220 /* Get a DRM GEM mmap offset allocated... */
1221 list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
1222 obj->size / PAGE_SIZE, 0, 0);
1223 if (!list->file_offset_node) {
1224 DRM_ERROR("failed to allocate offset for bo %d\n", obj->name);
1225 ret = -ENOMEM;
1226 goto out_free_list;
1227 }
1228
1229 list->file_offset_node = drm_mm_get_block(list->file_offset_node,
1230 obj->size / PAGE_SIZE, 0);
1231 if (!list->file_offset_node) {
1232 ret = -ENOMEM;
1233 goto out_free_list;
1234 }
1235
1236 list->hash.key = list->file_offset_node->start;
1237 if (drm_ht_insert_item(&mm->offset_hash, &list->hash)) {
1238 DRM_ERROR("failed to add to map hash\n");
1239 goto out_free_mm;
1240 }
1241
1242 /* By now we should be all set, any drm_mmap request on the offset
1243 * below will get to our mmap & fault handler */
1244 obj_priv->mmap_offset = ((uint64_t) list->hash.key) << PAGE_SHIFT;
1245
1246 return 0;
1247
1248out_free_mm:
1249 drm_mm_put_block(list->file_offset_node);
1250out_free_list:
1251 drm_free(list->map, sizeof(struct drm_map_list), DRM_MEM_DRIVER);
1252
1253 return ret;
1254}
1255
Jesse Barnesab00b3e2009-02-11 14:01:46 -08001256static void
1257i915_gem_free_mmap_offset(struct drm_gem_object *obj)
1258{
1259 struct drm_device *dev = obj->dev;
1260 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1261 struct drm_gem_mm *mm = dev->mm_private;
1262 struct drm_map_list *list;
1263
1264 list = &obj->map_list;
1265 drm_ht_remove_item(&mm->offset_hash, &list->hash);
1266
1267 if (list->file_offset_node) {
1268 drm_mm_put_block(list->file_offset_node);
1269 list->file_offset_node = NULL;
1270 }
1271
1272 if (list->map) {
1273 drm_free(list->map, sizeof(struct drm_map), DRM_MEM_DRIVER);
1274 list->map = NULL;
1275 }
1276
1277 obj_priv->mmap_offset = 0;
1278}
1279
Jesse Barnesde151cf2008-11-12 10:03:55 -08001280/**
1281 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1282 * @obj: object to check
1283 *
1284 * Return the required GTT alignment for an object, taking into account
1285 * potential fence register mapping if needed.
1286 */
1287static uint32_t
1288i915_gem_get_gtt_alignment(struct drm_gem_object *obj)
1289{
1290 struct drm_device *dev = obj->dev;
1291 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1292 int start, i;
1293
1294 /*
1295 * Minimum alignment is 4k (GTT page size), but might be greater
1296 * if a fence register is needed for the object.
1297 */
1298 if (IS_I965G(dev) || obj_priv->tiling_mode == I915_TILING_NONE)
1299 return 4096;
1300
1301 /*
1302 * Previous chips need to be aligned to the size of the smallest
1303 * fence register that can contain the object.
1304 */
1305 if (IS_I9XX(dev))
1306 start = 1024*1024;
1307 else
1308 start = 512*1024;
1309
1310 for (i = start; i < obj->size; i <<= 1)
1311 ;
1312
1313 return i;
1314}
1315
1316/**
1317 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1318 * @dev: DRM device
1319 * @data: GTT mapping ioctl data
1320 * @file_priv: GEM object info
1321 *
1322 * Simply returns the fake offset to userspace so it can mmap it.
1323 * The mmap call will end up in drm_gem_mmap(), which will set things
1324 * up so we can get faults in the handler above.
1325 *
1326 * The fault handler will take care of binding the object into the GTT
1327 * (since it may have been evicted to make room for something), allocating
1328 * a fence register, and mapping the appropriate aperture address into
1329 * userspace.
1330 */
1331int
1332i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
1333 struct drm_file *file_priv)
1334{
1335 struct drm_i915_gem_mmap_gtt *args = data;
1336 struct drm_i915_private *dev_priv = dev->dev_private;
1337 struct drm_gem_object *obj;
1338 struct drm_i915_gem_object *obj_priv;
1339 int ret;
1340
1341 if (!(dev->driver->driver_features & DRIVER_GEM))
1342 return -ENODEV;
1343
1344 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1345 if (obj == NULL)
1346 return -EBADF;
1347
1348 mutex_lock(&dev->struct_mutex);
1349
1350 obj_priv = obj->driver_private;
1351
1352 if (!obj_priv->mmap_offset) {
1353 ret = i915_gem_create_mmap_offset(obj);
Chris Wilson13af1062009-02-11 14:26:31 +00001354 if (ret) {
1355 drm_gem_object_unreference(obj);
1356 mutex_unlock(&dev->struct_mutex);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001357 return ret;
Chris Wilson13af1062009-02-11 14:26:31 +00001358 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001359 }
1360
1361 args->offset = obj_priv->mmap_offset;
1362
1363 obj_priv->gtt_alignment = i915_gem_get_gtt_alignment(obj);
1364
1365 /* Make sure the alignment is correct for fence regs etc */
1366 if (obj_priv->agp_mem &&
1367 (obj_priv->gtt_offset & (obj_priv->gtt_alignment - 1))) {
1368 drm_gem_object_unreference(obj);
1369 mutex_unlock(&dev->struct_mutex);
1370 return -EINVAL;
1371 }
1372
1373 /*
1374 * Pull it into the GTT so that we have a page list (makes the
1375 * initial fault faster and any subsequent flushing possible).
1376 */
1377 if (!obj_priv->agp_mem) {
1378 ret = i915_gem_object_bind_to_gtt(obj, obj_priv->gtt_alignment);
1379 if (ret) {
1380 drm_gem_object_unreference(obj);
1381 mutex_unlock(&dev->struct_mutex);
1382 return ret;
1383 }
Jesse Barnes14b60392009-05-20 16:47:08 -04001384 list_add_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001385 }
1386
1387 drm_gem_object_unreference(obj);
1388 mutex_unlock(&dev->struct_mutex);
1389
1390 return 0;
1391}
1392
Ben Gamari6911a9b2009-04-02 11:24:54 -07001393void
Eric Anholt856fa192009-03-19 14:10:50 -07001394i915_gem_object_put_pages(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07001395{
1396 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1397 int page_count = obj->size / PAGE_SIZE;
1398 int i;
1399
Eric Anholt856fa192009-03-19 14:10:50 -07001400 BUG_ON(obj_priv->pages_refcount == 0);
1401
1402 if (--obj_priv->pages_refcount != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07001403 return;
1404
Eric Anholt280b7132009-03-12 16:56:27 -07001405 if (obj_priv->tiling_mode != I915_TILING_NONE)
1406 i915_gem_object_save_bit_17_swizzle(obj);
1407
Eric Anholt673a3942008-07-30 12:06:12 -07001408 for (i = 0; i < page_count; i++)
Eric Anholt856fa192009-03-19 14:10:50 -07001409 if (obj_priv->pages[i] != NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -07001410 if (obj_priv->dirty)
Eric Anholt856fa192009-03-19 14:10:50 -07001411 set_page_dirty(obj_priv->pages[i]);
1412 mark_page_accessed(obj_priv->pages[i]);
1413 page_cache_release(obj_priv->pages[i]);
Eric Anholt673a3942008-07-30 12:06:12 -07001414 }
1415 obj_priv->dirty = 0;
1416
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07001417 drm_free_large(obj_priv->pages);
Eric Anholt856fa192009-03-19 14:10:50 -07001418 obj_priv->pages = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001419}
1420
1421static void
Eric Anholtce44b0e2008-11-06 16:00:31 -08001422i915_gem_object_move_to_active(struct drm_gem_object *obj, uint32_t seqno)
Eric Anholt673a3942008-07-30 12:06:12 -07001423{
1424 struct drm_device *dev = obj->dev;
1425 drm_i915_private_t *dev_priv = dev->dev_private;
1426 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1427
1428 /* Add a reference if we're newly entering the active list. */
1429 if (!obj_priv->active) {
1430 drm_gem_object_reference(obj);
1431 obj_priv->active = 1;
1432 }
1433 /* Move from whatever list we were on to the tail of execution. */
Carl Worth5e118f42009-03-20 11:54:25 -07001434 spin_lock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001435 list_move_tail(&obj_priv->list,
1436 &dev_priv->mm.active_list);
Carl Worth5e118f42009-03-20 11:54:25 -07001437 spin_unlock(&dev_priv->mm.active_list_lock);
Eric Anholtce44b0e2008-11-06 16:00:31 -08001438 obj_priv->last_rendering_seqno = seqno;
Eric Anholt673a3942008-07-30 12:06:12 -07001439}
1440
Eric Anholtce44b0e2008-11-06 16:00:31 -08001441static void
1442i915_gem_object_move_to_flushing(struct drm_gem_object *obj)
1443{
1444 struct drm_device *dev = obj->dev;
1445 drm_i915_private_t *dev_priv = dev->dev_private;
1446 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1447
1448 BUG_ON(!obj_priv->active);
1449 list_move_tail(&obj_priv->list, &dev_priv->mm.flushing_list);
1450 obj_priv->last_rendering_seqno = 0;
1451}
Eric Anholt673a3942008-07-30 12:06:12 -07001452
1453static void
1454i915_gem_object_move_to_inactive(struct drm_gem_object *obj)
1455{
1456 struct drm_device *dev = obj->dev;
1457 drm_i915_private_t *dev_priv = dev->dev_private;
1458 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1459
1460 i915_verify_inactive(dev, __FILE__, __LINE__);
1461 if (obj_priv->pin_count != 0)
1462 list_del_init(&obj_priv->list);
1463 else
1464 list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
1465
Eric Anholtce44b0e2008-11-06 16:00:31 -08001466 obj_priv->last_rendering_seqno = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001467 if (obj_priv->active) {
1468 obj_priv->active = 0;
1469 drm_gem_object_unreference(obj);
1470 }
1471 i915_verify_inactive(dev, __FILE__, __LINE__);
1472}
1473
1474/**
1475 * Creates a new sequence number, emitting a write of it to the status page
1476 * plus an interrupt, which will trigger i915_user_interrupt_handler.
1477 *
1478 * Must be called with struct_lock held.
1479 *
1480 * Returned sequence numbers are nonzero on success.
1481 */
1482static uint32_t
Eric Anholtb9624422009-06-03 07:27:35 +00001483i915_add_request(struct drm_device *dev, struct drm_file *file_priv,
1484 uint32_t flush_domains)
Eric Anholt673a3942008-07-30 12:06:12 -07001485{
1486 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtb9624422009-06-03 07:27:35 +00001487 struct drm_i915_file_private *i915_file_priv = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07001488 struct drm_i915_gem_request *request;
1489 uint32_t seqno;
1490 int was_empty;
1491 RING_LOCALS;
1492
Eric Anholtb9624422009-06-03 07:27:35 +00001493 if (file_priv != NULL)
1494 i915_file_priv = file_priv->driver_priv;
1495
Eric Anholt673a3942008-07-30 12:06:12 -07001496 request = drm_calloc(1, sizeof(*request), DRM_MEM_DRIVER);
1497 if (request == NULL)
1498 return 0;
1499
1500 /* Grab the seqno we're going to make this request be, and bump the
1501 * next (skipping 0 so it can be the reserved no-seqno value).
1502 */
1503 seqno = dev_priv->mm.next_gem_seqno;
1504 dev_priv->mm.next_gem_seqno++;
1505 if (dev_priv->mm.next_gem_seqno == 0)
1506 dev_priv->mm.next_gem_seqno++;
1507
1508 BEGIN_LP_RING(4);
1509 OUT_RING(MI_STORE_DWORD_INDEX);
1510 OUT_RING(I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
1511 OUT_RING(seqno);
1512
1513 OUT_RING(MI_USER_INTERRUPT);
1514 ADVANCE_LP_RING();
1515
1516 DRM_DEBUG("%d\n", seqno);
1517
1518 request->seqno = seqno;
1519 request->emitted_jiffies = jiffies;
Eric Anholt673a3942008-07-30 12:06:12 -07001520 was_empty = list_empty(&dev_priv->mm.request_list);
1521 list_add_tail(&request->list, &dev_priv->mm.request_list);
Eric Anholtb9624422009-06-03 07:27:35 +00001522 if (i915_file_priv) {
1523 list_add_tail(&request->client_list,
1524 &i915_file_priv->mm.request_list);
1525 } else {
1526 INIT_LIST_HEAD(&request->client_list);
1527 }
Eric Anholt673a3942008-07-30 12:06:12 -07001528
Eric Anholtce44b0e2008-11-06 16:00:31 -08001529 /* Associate any objects on the flushing list matching the write
1530 * domain we're flushing with our flush.
1531 */
1532 if (flush_domains != 0) {
1533 struct drm_i915_gem_object *obj_priv, *next;
1534
1535 list_for_each_entry_safe(obj_priv, next,
1536 &dev_priv->mm.flushing_list, list) {
1537 struct drm_gem_object *obj = obj_priv->obj;
1538
1539 if ((obj->write_domain & flush_domains) ==
1540 obj->write_domain) {
1541 obj->write_domain = 0;
1542 i915_gem_object_move_to_active(obj, seqno);
1543 }
1544 }
1545
1546 }
1547
Keith Packard6dbe2772008-10-14 21:41:13 -07001548 if (was_empty && !dev_priv->mm.suspended)
Eric Anholt673a3942008-07-30 12:06:12 -07001549 schedule_delayed_work(&dev_priv->mm.retire_work, HZ);
1550 return seqno;
1551}
1552
1553/**
1554 * Command execution barrier
1555 *
1556 * Ensures that all commands in the ring are finished
1557 * before signalling the CPU
1558 */
Eric Anholt3043c602008-10-02 12:24:47 -07001559static uint32_t
Eric Anholt673a3942008-07-30 12:06:12 -07001560i915_retire_commands(struct drm_device *dev)
1561{
1562 drm_i915_private_t *dev_priv = dev->dev_private;
1563 uint32_t cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
1564 uint32_t flush_domains = 0;
1565 RING_LOCALS;
1566
1567 /* The sampler always gets flushed on i965 (sigh) */
1568 if (IS_I965G(dev))
1569 flush_domains |= I915_GEM_DOMAIN_SAMPLER;
1570 BEGIN_LP_RING(2);
1571 OUT_RING(cmd);
1572 OUT_RING(0); /* noop */
1573 ADVANCE_LP_RING();
1574 return flush_domains;
1575}
1576
1577/**
1578 * Moves buffers associated only with the given active seqno from the active
1579 * to inactive list, potentially freeing them.
1580 */
1581static void
1582i915_gem_retire_request(struct drm_device *dev,
1583 struct drm_i915_gem_request *request)
1584{
1585 drm_i915_private_t *dev_priv = dev->dev_private;
1586
1587 /* Move any buffers on the active list that are no longer referenced
1588 * by the ringbuffer to the flushing/inactive lists as appropriate.
1589 */
Carl Worth5e118f42009-03-20 11:54:25 -07001590 spin_lock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001591 while (!list_empty(&dev_priv->mm.active_list)) {
1592 struct drm_gem_object *obj;
1593 struct drm_i915_gem_object *obj_priv;
1594
1595 obj_priv = list_first_entry(&dev_priv->mm.active_list,
1596 struct drm_i915_gem_object,
1597 list);
1598 obj = obj_priv->obj;
1599
1600 /* If the seqno being retired doesn't match the oldest in the
1601 * list, then the oldest in the list must still be newer than
1602 * this seqno.
1603 */
1604 if (obj_priv->last_rendering_seqno != request->seqno)
Carl Worth5e118f42009-03-20 11:54:25 -07001605 goto out;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001606
Eric Anholt673a3942008-07-30 12:06:12 -07001607#if WATCH_LRU
1608 DRM_INFO("%s: retire %d moves to inactive list %p\n",
1609 __func__, request->seqno, obj);
1610#endif
1611
Eric Anholtce44b0e2008-11-06 16:00:31 -08001612 if (obj->write_domain != 0)
1613 i915_gem_object_move_to_flushing(obj);
Shaohua Li68c84342009-04-08 10:58:23 +08001614 else {
1615 /* Take a reference on the object so it won't be
1616 * freed while the spinlock is held. The list
1617 * protection for this spinlock is safe when breaking
1618 * the lock like this since the next thing we do
1619 * is just get the head of the list again.
1620 */
1621 drm_gem_object_reference(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001622 i915_gem_object_move_to_inactive(obj);
Shaohua Li68c84342009-04-08 10:58:23 +08001623 spin_unlock(&dev_priv->mm.active_list_lock);
1624 drm_gem_object_unreference(obj);
1625 spin_lock(&dev_priv->mm.active_list_lock);
1626 }
Eric Anholt673a3942008-07-30 12:06:12 -07001627 }
Carl Worth5e118f42009-03-20 11:54:25 -07001628out:
1629 spin_unlock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07001630}
1631
1632/**
1633 * Returns true if seq1 is later than seq2.
1634 */
1635static int
1636i915_seqno_passed(uint32_t seq1, uint32_t seq2)
1637{
1638 return (int32_t)(seq1 - seq2) >= 0;
1639}
1640
1641uint32_t
1642i915_get_gem_seqno(struct drm_device *dev)
1643{
1644 drm_i915_private_t *dev_priv = dev->dev_private;
1645
1646 return READ_HWSP(dev_priv, I915_GEM_HWS_INDEX);
1647}
1648
1649/**
1650 * This function clears the request list as sequence numbers are passed.
1651 */
1652void
1653i915_gem_retire_requests(struct drm_device *dev)
1654{
1655 drm_i915_private_t *dev_priv = dev->dev_private;
1656 uint32_t seqno;
1657
Karsten Wiese6c0594a2009-02-23 15:07:57 +01001658 if (!dev_priv->hw_status_page)
1659 return;
1660
Eric Anholt673a3942008-07-30 12:06:12 -07001661 seqno = i915_get_gem_seqno(dev);
1662
1663 while (!list_empty(&dev_priv->mm.request_list)) {
1664 struct drm_i915_gem_request *request;
1665 uint32_t retiring_seqno;
1666
1667 request = list_first_entry(&dev_priv->mm.request_list,
1668 struct drm_i915_gem_request,
1669 list);
1670 retiring_seqno = request->seqno;
1671
1672 if (i915_seqno_passed(seqno, retiring_seqno) ||
1673 dev_priv->mm.wedged) {
1674 i915_gem_retire_request(dev, request);
1675
1676 list_del(&request->list);
Eric Anholtb9624422009-06-03 07:27:35 +00001677 list_del(&request->client_list);
Eric Anholt673a3942008-07-30 12:06:12 -07001678 drm_free(request, sizeof(*request), DRM_MEM_DRIVER);
1679 } else
1680 break;
1681 }
1682}
1683
1684void
1685i915_gem_retire_work_handler(struct work_struct *work)
1686{
1687 drm_i915_private_t *dev_priv;
1688 struct drm_device *dev;
1689
1690 dev_priv = container_of(work, drm_i915_private_t,
1691 mm.retire_work.work);
1692 dev = dev_priv->dev;
1693
1694 mutex_lock(&dev->struct_mutex);
1695 i915_gem_retire_requests(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07001696 if (!dev_priv->mm.suspended &&
1697 !list_empty(&dev_priv->mm.request_list))
Eric Anholt673a3942008-07-30 12:06:12 -07001698 schedule_delayed_work(&dev_priv->mm.retire_work, HZ);
1699 mutex_unlock(&dev->struct_mutex);
1700}
1701
1702/**
1703 * Waits for a sequence number to be signaled, and cleans up the
1704 * request and object lists appropriately for that event.
1705 */
Eric Anholt3043c602008-10-02 12:24:47 -07001706static int
Eric Anholt673a3942008-07-30 12:06:12 -07001707i915_wait_request(struct drm_device *dev, uint32_t seqno)
1708{
1709 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001710 u32 ier;
Eric Anholt673a3942008-07-30 12:06:12 -07001711 int ret = 0;
1712
1713 BUG_ON(seqno == 0);
1714
1715 if (!i915_seqno_passed(i915_get_gem_seqno(dev), seqno)) {
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001716 if (IS_IGDNG(dev))
1717 ier = I915_READ(DEIER) | I915_READ(GTIER);
1718 else
1719 ier = I915_READ(IER);
Jesse Barnes802c7eb2009-05-05 16:03:48 -07001720 if (!ier) {
1721 DRM_ERROR("something (likely vbetool) disabled "
1722 "interrupts, re-enabling\n");
1723 i915_driver_irq_preinstall(dev);
1724 i915_driver_irq_postinstall(dev);
1725 }
1726
Eric Anholt673a3942008-07-30 12:06:12 -07001727 dev_priv->mm.waiting_gem_seqno = seqno;
1728 i915_user_irq_get(dev);
1729 ret = wait_event_interruptible(dev_priv->irq_queue,
1730 i915_seqno_passed(i915_get_gem_seqno(dev),
1731 seqno) ||
1732 dev_priv->mm.wedged);
1733 i915_user_irq_put(dev);
1734 dev_priv->mm.waiting_gem_seqno = 0;
1735 }
1736 if (dev_priv->mm.wedged)
1737 ret = -EIO;
1738
1739 if (ret && ret != -ERESTARTSYS)
1740 DRM_ERROR("%s returns %d (awaiting %d at %d)\n",
1741 __func__, ret, seqno, i915_get_gem_seqno(dev));
1742
1743 /* Directly dispatch request retiring. While we have the work queue
1744 * to handle this, the waiter on a request often wants an associated
1745 * buffer to have made it to the inactive list, and we would need
1746 * a separate wait queue to handle that.
1747 */
1748 if (ret == 0)
1749 i915_gem_retire_requests(dev);
1750
1751 return ret;
1752}
1753
1754static void
1755i915_gem_flush(struct drm_device *dev,
1756 uint32_t invalidate_domains,
1757 uint32_t flush_domains)
1758{
1759 drm_i915_private_t *dev_priv = dev->dev_private;
1760 uint32_t cmd;
1761 RING_LOCALS;
1762
1763#if WATCH_EXEC
1764 DRM_INFO("%s: invalidate %08x flush %08x\n", __func__,
1765 invalidate_domains, flush_domains);
1766#endif
1767
1768 if (flush_domains & I915_GEM_DOMAIN_CPU)
1769 drm_agp_chipset_flush(dev);
1770
Chris Wilson21d509e2009-06-06 09:46:02 +01001771 if ((invalidate_domains | flush_domains) & I915_GEM_GPU_DOMAINS) {
Eric Anholt673a3942008-07-30 12:06:12 -07001772 /*
1773 * read/write caches:
1774 *
1775 * I915_GEM_DOMAIN_RENDER is always invalidated, but is
1776 * only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is
1777 * also flushed at 2d versus 3d pipeline switches.
1778 *
1779 * read-only caches:
1780 *
1781 * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if
1782 * MI_READ_FLUSH is set, and is always flushed on 965.
1783 *
1784 * I915_GEM_DOMAIN_COMMAND may not exist?
1785 *
1786 * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is
1787 * invalidated when MI_EXE_FLUSH is set.
1788 *
1789 * I915_GEM_DOMAIN_VERTEX, which exists on 965, is
1790 * invalidated with every MI_FLUSH.
1791 *
1792 * TLBs:
1793 *
1794 * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND
1795 * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and
1796 * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER
1797 * are flushed at any MI_FLUSH.
1798 */
1799
1800 cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
1801 if ((invalidate_domains|flush_domains) &
1802 I915_GEM_DOMAIN_RENDER)
1803 cmd &= ~MI_NO_WRITE_FLUSH;
1804 if (!IS_I965G(dev)) {
1805 /*
1806 * On the 965, the sampler cache always gets flushed
1807 * and this bit is reserved.
1808 */
1809 if (invalidate_domains & I915_GEM_DOMAIN_SAMPLER)
1810 cmd |= MI_READ_FLUSH;
1811 }
1812 if (invalidate_domains & I915_GEM_DOMAIN_INSTRUCTION)
1813 cmd |= MI_EXE_FLUSH;
1814
1815#if WATCH_EXEC
1816 DRM_INFO("%s: queue flush %08x to ring\n", __func__, cmd);
1817#endif
1818 BEGIN_LP_RING(2);
1819 OUT_RING(cmd);
1820 OUT_RING(0); /* noop */
1821 ADVANCE_LP_RING();
1822 }
1823}
1824
1825/**
1826 * Ensures that all rendering to the object has completed and the object is
1827 * safe to unbind from the GTT or access from the CPU.
1828 */
1829static int
1830i915_gem_object_wait_rendering(struct drm_gem_object *obj)
1831{
1832 struct drm_device *dev = obj->dev;
1833 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1834 int ret;
1835
Eric Anholte47c68e2008-11-14 13:35:19 -08001836 /* This function only exists to support waiting for existing rendering,
1837 * not for emitting required flushes.
Eric Anholt673a3942008-07-30 12:06:12 -07001838 */
Eric Anholte47c68e2008-11-14 13:35:19 -08001839 BUG_ON((obj->write_domain & I915_GEM_GPU_DOMAINS) != 0);
Eric Anholt673a3942008-07-30 12:06:12 -07001840
1841 /* If there is rendering queued on the buffer being evicted, wait for
1842 * it.
1843 */
1844 if (obj_priv->active) {
1845#if WATCH_BUF
1846 DRM_INFO("%s: object %p wait for seqno %08x\n",
1847 __func__, obj, obj_priv->last_rendering_seqno);
1848#endif
1849 ret = i915_wait_request(dev, obj_priv->last_rendering_seqno);
1850 if (ret != 0)
1851 return ret;
1852 }
1853
1854 return 0;
1855}
1856
1857/**
1858 * Unbinds an object from the GTT aperture.
1859 */
Jesse Barnes0f973f22009-01-26 17:10:45 -08001860int
Eric Anholt673a3942008-07-30 12:06:12 -07001861i915_gem_object_unbind(struct drm_gem_object *obj)
1862{
1863 struct drm_device *dev = obj->dev;
1864 struct drm_i915_gem_object *obj_priv = obj->driver_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001865 loff_t offset;
Eric Anholt673a3942008-07-30 12:06:12 -07001866 int ret = 0;
1867
1868#if WATCH_BUF
1869 DRM_INFO("%s:%d %p\n", __func__, __LINE__, obj);
1870 DRM_INFO("gtt_space %p\n", obj_priv->gtt_space);
1871#endif
1872 if (obj_priv->gtt_space == NULL)
1873 return 0;
1874
1875 if (obj_priv->pin_count != 0) {
1876 DRM_ERROR("Attempting to unbind pinned buffer\n");
1877 return -EINVAL;
1878 }
1879
Eric Anholt673a3942008-07-30 12:06:12 -07001880 /* Move the object to the CPU domain to ensure that
1881 * any possible CPU writes while it's not in the GTT
1882 * are flushed when we go to remap it. This will
1883 * also ensure that all pending GPU writes are finished
1884 * before we unbind.
1885 */
Eric Anholte47c68e2008-11-14 13:35:19 -08001886 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07001887 if (ret) {
Eric Anholte47c68e2008-11-14 13:35:19 -08001888 if (ret != -ERESTARTSYS)
1889 DRM_ERROR("set_domain failed: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07001890 return ret;
1891 }
1892
1893 if (obj_priv->agp_mem != NULL) {
1894 drm_unbind_agp(obj_priv->agp_mem);
1895 drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE);
1896 obj_priv->agp_mem = NULL;
1897 }
1898
1899 BUG_ON(obj_priv->active);
1900
Jesse Barnesde151cf2008-11-12 10:03:55 -08001901 /* blow away mappings if mapped through GTT */
1902 offset = ((loff_t) obj->map_list.hash.key) << PAGE_SHIFT;
Jesse Barnes79e53942008-11-07 14:24:08 -08001903 if (dev->dev_mapping)
1904 unmap_mapping_range(dev->dev_mapping, offset, obj->size, 1);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001905
1906 if (obj_priv->fence_reg != I915_FENCE_REG_NONE)
1907 i915_gem_clear_fence_reg(obj);
1908
Eric Anholt856fa192009-03-19 14:10:50 -07001909 i915_gem_object_put_pages(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001910
1911 if (obj_priv->gtt_space) {
1912 atomic_dec(&dev->gtt_count);
1913 atomic_sub(obj->size, &dev->gtt_memory);
1914
1915 drm_mm_put_block(obj_priv->gtt_space);
1916 obj_priv->gtt_space = NULL;
1917 }
1918
1919 /* Remove ourselves from the LRU list if present. */
1920 if (!list_empty(&obj_priv->list))
1921 list_del_init(&obj_priv->list);
1922
1923 return 0;
1924}
1925
1926static int
1927i915_gem_evict_something(struct drm_device *dev)
1928{
1929 drm_i915_private_t *dev_priv = dev->dev_private;
1930 struct drm_gem_object *obj;
1931 struct drm_i915_gem_object *obj_priv;
1932 int ret = 0;
1933
1934 for (;;) {
1935 /* If there's an inactive buffer available now, grab it
1936 * and be done.
1937 */
1938 if (!list_empty(&dev_priv->mm.inactive_list)) {
1939 obj_priv = list_first_entry(&dev_priv->mm.inactive_list,
1940 struct drm_i915_gem_object,
1941 list);
1942 obj = obj_priv->obj;
1943 BUG_ON(obj_priv->pin_count != 0);
1944#if WATCH_LRU
1945 DRM_INFO("%s: evicting %p\n", __func__, obj);
1946#endif
1947 BUG_ON(obj_priv->active);
1948
1949 /* Wait on the rendering and unbind the buffer. */
1950 ret = i915_gem_object_unbind(obj);
1951 break;
1952 }
1953
1954 /* If we didn't get anything, but the ring is still processing
1955 * things, wait for one of those things to finish and hopefully
1956 * leave us a buffer to evict.
1957 */
1958 if (!list_empty(&dev_priv->mm.request_list)) {
1959 struct drm_i915_gem_request *request;
1960
1961 request = list_first_entry(&dev_priv->mm.request_list,
1962 struct drm_i915_gem_request,
1963 list);
1964
1965 ret = i915_wait_request(dev, request->seqno);
1966 if (ret)
1967 break;
1968
1969 /* if waiting caused an object to become inactive,
1970 * then loop around and wait for it. Otherwise, we
1971 * assume that waiting freed and unbound something,
1972 * so there should now be some space in the GTT
1973 */
1974 if (!list_empty(&dev_priv->mm.inactive_list))
1975 continue;
1976 break;
1977 }
1978
1979 /* If we didn't have anything on the request list but there
1980 * are buffers awaiting a flush, emit one and try again.
1981 * When we wait on it, those buffers waiting for that flush
1982 * will get moved to inactive.
1983 */
1984 if (!list_empty(&dev_priv->mm.flushing_list)) {
1985 obj_priv = list_first_entry(&dev_priv->mm.flushing_list,
1986 struct drm_i915_gem_object,
1987 list);
1988 obj = obj_priv->obj;
1989
1990 i915_gem_flush(dev,
1991 obj->write_domain,
1992 obj->write_domain);
Eric Anholtb9624422009-06-03 07:27:35 +00001993 i915_add_request(dev, NULL, obj->write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07001994
1995 obj = NULL;
1996 continue;
1997 }
1998
1999 DRM_ERROR("inactive empty %d request empty %d "
2000 "flushing empty %d\n",
2001 list_empty(&dev_priv->mm.inactive_list),
2002 list_empty(&dev_priv->mm.request_list),
2003 list_empty(&dev_priv->mm.flushing_list));
2004 /* If we didn't do any of the above, there's nothing to be done
2005 * and we just can't fit it in.
2006 */
Chris Wilson2939e1f2009-06-06 09:46:03 +01002007 return -ENOSPC;
Eric Anholt673a3942008-07-30 12:06:12 -07002008 }
2009 return ret;
2010}
2011
2012static int
Keith Packardac94a962008-11-20 23:30:27 -08002013i915_gem_evict_everything(struct drm_device *dev)
2014{
2015 int ret;
2016
2017 for (;;) {
2018 ret = i915_gem_evict_something(dev);
2019 if (ret != 0)
2020 break;
2021 }
Chris Wilson2939e1f2009-06-06 09:46:03 +01002022 if (ret == -ENOSPC)
Owain Ainsworth15c35332008-12-06 20:42:20 -08002023 return 0;
Keith Packardac94a962008-11-20 23:30:27 -08002024 return ret;
2025}
2026
Ben Gamari6911a9b2009-04-02 11:24:54 -07002027int
Eric Anholt856fa192009-03-19 14:10:50 -07002028i915_gem_object_get_pages(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002029{
2030 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2031 int page_count, i;
2032 struct address_space *mapping;
2033 struct inode *inode;
2034 struct page *page;
2035 int ret;
2036
Eric Anholt856fa192009-03-19 14:10:50 -07002037 if (obj_priv->pages_refcount++ != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07002038 return 0;
2039
2040 /* Get the list of pages out of our struct file. They'll be pinned
2041 * at this point until we release them.
2042 */
2043 page_count = obj->size / PAGE_SIZE;
Eric Anholt856fa192009-03-19 14:10:50 -07002044 BUG_ON(obj_priv->pages != NULL);
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07002045 obj_priv->pages = drm_calloc_large(page_count, sizeof(struct page *));
Eric Anholt856fa192009-03-19 14:10:50 -07002046 if (obj_priv->pages == NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -07002047 DRM_ERROR("Faled to allocate page list\n");
Eric Anholt856fa192009-03-19 14:10:50 -07002048 obj_priv->pages_refcount--;
Eric Anholt673a3942008-07-30 12:06:12 -07002049 return -ENOMEM;
2050 }
2051
2052 inode = obj->filp->f_path.dentry->d_inode;
2053 mapping = inode->i_mapping;
2054 for (i = 0; i < page_count; i++) {
2055 page = read_mapping_page(mapping, i, NULL);
2056 if (IS_ERR(page)) {
2057 ret = PTR_ERR(page);
2058 DRM_ERROR("read_mapping_page failed: %d\n", ret);
Eric Anholt856fa192009-03-19 14:10:50 -07002059 i915_gem_object_put_pages(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002060 return ret;
2061 }
Eric Anholt856fa192009-03-19 14:10:50 -07002062 obj_priv->pages[i] = page;
Eric Anholt673a3942008-07-30 12:06:12 -07002063 }
Eric Anholt280b7132009-03-12 16:56:27 -07002064
2065 if (obj_priv->tiling_mode != I915_TILING_NONE)
2066 i915_gem_object_do_bit_17_swizzle(obj);
2067
Eric Anholt673a3942008-07-30 12:06:12 -07002068 return 0;
2069}
2070
Jesse Barnesde151cf2008-11-12 10:03:55 -08002071static void i965_write_fence_reg(struct drm_i915_fence_reg *reg)
2072{
2073 struct drm_gem_object *obj = reg->obj;
2074 struct drm_device *dev = obj->dev;
2075 drm_i915_private_t *dev_priv = dev->dev_private;
2076 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2077 int regnum = obj_priv->fence_reg;
2078 uint64_t val;
2079
2080 val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
2081 0xfffff000) << 32;
2082 val |= obj_priv->gtt_offset & 0xfffff000;
2083 val |= ((obj_priv->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
2084 if (obj_priv->tiling_mode == I915_TILING_Y)
2085 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2086 val |= I965_FENCE_REG_VALID;
2087
2088 I915_WRITE64(FENCE_REG_965_0 + (regnum * 8), val);
2089}
2090
2091static void i915_write_fence_reg(struct drm_i915_fence_reg *reg)
2092{
2093 struct drm_gem_object *obj = reg->obj;
2094 struct drm_device *dev = obj->dev;
2095 drm_i915_private_t *dev_priv = dev->dev_private;
2096 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2097 int regnum = obj_priv->fence_reg;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002098 int tile_width;
Eric Anholtdc529a42009-03-10 22:34:49 -07002099 uint32_t fence_reg, val;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002100 uint32_t pitch_val;
2101
2102 if ((obj_priv->gtt_offset & ~I915_FENCE_START_MASK) ||
2103 (obj_priv->gtt_offset & (obj->size - 1))) {
Linus Torvaldsf06da262009-02-09 08:57:29 -08002104 WARN(1, "%s: object 0x%08x not 1M or size (0x%zx) aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002105 __func__, obj_priv->gtt_offset, obj->size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002106 return;
2107 }
2108
Jesse Barnes0f973f22009-01-26 17:10:45 -08002109 if (obj_priv->tiling_mode == I915_TILING_Y &&
2110 HAS_128_BYTE_Y_TILING(dev))
2111 tile_width = 128;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002112 else
Jesse Barnes0f973f22009-01-26 17:10:45 -08002113 tile_width = 512;
2114
2115 /* Note: pitch better be a power of two tile widths */
2116 pitch_val = obj_priv->stride / tile_width;
2117 pitch_val = ffs(pitch_val) - 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002118
2119 val = obj_priv->gtt_offset;
2120 if (obj_priv->tiling_mode == I915_TILING_Y)
2121 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2122 val |= I915_FENCE_SIZE_BITS(obj->size);
2123 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2124 val |= I830_FENCE_REG_VALID;
2125
Eric Anholtdc529a42009-03-10 22:34:49 -07002126 if (regnum < 8)
2127 fence_reg = FENCE_REG_830_0 + (regnum * 4);
2128 else
2129 fence_reg = FENCE_REG_945_8 + ((regnum - 8) * 4);
2130 I915_WRITE(fence_reg, val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002131}
2132
2133static void i830_write_fence_reg(struct drm_i915_fence_reg *reg)
2134{
2135 struct drm_gem_object *obj = reg->obj;
2136 struct drm_device *dev = obj->dev;
2137 drm_i915_private_t *dev_priv = dev->dev_private;
2138 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2139 int regnum = obj_priv->fence_reg;
2140 uint32_t val;
2141 uint32_t pitch_val;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002142 uint32_t fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002143
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002144 if ((obj_priv->gtt_offset & ~I830_FENCE_START_MASK) ||
Jesse Barnesde151cf2008-11-12 10:03:55 -08002145 (obj_priv->gtt_offset & (obj->size - 1))) {
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002146 WARN(1, "%s: object 0x%08x not 512K or size aligned\n",
Jesse Barnes0f973f22009-01-26 17:10:45 -08002147 __func__, obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002148 return;
2149 }
2150
Eric Anholte76a16d2009-05-26 17:44:56 -07002151 pitch_val = obj_priv->stride / 128;
2152 pitch_val = ffs(pitch_val) - 1;
2153 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2154
Jesse Barnesde151cf2008-11-12 10:03:55 -08002155 val = obj_priv->gtt_offset;
2156 if (obj_priv->tiling_mode == I915_TILING_Y)
2157 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002158 fence_size_bits = I830_FENCE_SIZE_BITS(obj->size);
2159 WARN_ON(fence_size_bits & ~0x00000f00);
2160 val |= fence_size_bits;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002161 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2162 val |= I830_FENCE_REG_VALID;
2163
2164 I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002165}
2166
2167/**
2168 * i915_gem_object_get_fence_reg - set up a fence reg for an object
2169 * @obj: object to map through a fence reg
2170 *
2171 * When mapping objects through the GTT, userspace wants to be able to write
2172 * to them without having to worry about swizzling if the object is tiled.
2173 *
2174 * This function walks the fence regs looking for a free one for @obj,
2175 * stealing one if it can't find any.
2176 *
2177 * It then sets up the reg based on the object's properties: address, pitch
2178 * and tiling format.
2179 */
Chris Wilson8c4b8c32009-06-17 22:08:52 +01002180int
2181i915_gem_object_get_fence_reg(struct drm_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002182{
2183 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002184 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002185 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2186 struct drm_i915_fence_reg *reg = NULL;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002187 struct drm_i915_gem_object *old_obj_priv = NULL;
2188 int i, ret, avail;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002189
2190 switch (obj_priv->tiling_mode) {
2191 case I915_TILING_NONE:
2192 WARN(1, "allocating a fence for non-tiled object?\n");
2193 break;
2194 case I915_TILING_X:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002195 if (!obj_priv->stride)
2196 return -EINVAL;
2197 WARN((obj_priv->stride & (512 - 1)),
2198 "object 0x%08x is X tiled but has non-512B pitch\n",
2199 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002200 break;
2201 case I915_TILING_Y:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002202 if (!obj_priv->stride)
2203 return -EINVAL;
2204 WARN((obj_priv->stride & (128 - 1)),
2205 "object 0x%08x is Y tiled but has non-128B pitch\n",
2206 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002207 break;
2208 }
2209
2210 /* First try to find a free reg */
Chris Wilson9b2412f2009-02-11 14:26:44 +00002211try_again:
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002212 avail = 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002213 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2214 reg = &dev_priv->fence_regs[i];
2215 if (!reg->obj)
2216 break;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002217
2218 old_obj_priv = reg->obj->driver_private;
2219 if (!old_obj_priv->pin_count)
2220 avail++;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002221 }
2222
2223 /* None available, try to steal one or wait for a user to finish */
2224 if (i == dev_priv->num_fence_regs) {
Chris Wilsond7619c42009-02-11 14:26:47 +00002225 uint32_t seqno = dev_priv->mm.next_gem_seqno;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002226 loff_t offset;
2227
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002228 if (avail == 0)
Chris Wilson2939e1f2009-06-06 09:46:03 +01002229 return -ENOSPC;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002230
Jesse Barnesde151cf2008-11-12 10:03:55 -08002231 for (i = dev_priv->fence_reg_start;
2232 i < dev_priv->num_fence_regs; i++) {
Chris Wilsond7619c42009-02-11 14:26:47 +00002233 uint32_t this_seqno;
2234
Jesse Barnesde151cf2008-11-12 10:03:55 -08002235 reg = &dev_priv->fence_regs[i];
2236 old_obj_priv = reg->obj->driver_private;
Chris Wilsond7619c42009-02-11 14:26:47 +00002237
2238 if (old_obj_priv->pin_count)
2239 continue;
2240
2241 /* i915 uses fences for GPU access to tiled buffers */
2242 if (IS_I965G(dev) || !old_obj_priv->active)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002243 break;
Chris Wilsond7619c42009-02-11 14:26:47 +00002244
2245 /* find the seqno of the first available fence */
2246 this_seqno = old_obj_priv->last_rendering_seqno;
2247 if (this_seqno != 0 &&
2248 reg->obj->write_domain == 0 &&
2249 i915_seqno_passed(seqno, this_seqno))
2250 seqno = this_seqno;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002251 }
2252
2253 /*
2254 * Now things get ugly... we have to wait for one of the
2255 * objects to finish before trying again.
2256 */
2257 if (i == dev_priv->num_fence_regs) {
Chris Wilsond7619c42009-02-11 14:26:47 +00002258 if (seqno == dev_priv->mm.next_gem_seqno) {
2259 i915_gem_flush(dev,
2260 I915_GEM_GPU_DOMAINS,
2261 I915_GEM_GPU_DOMAINS);
Eric Anholtb9624422009-06-03 07:27:35 +00002262 seqno = i915_add_request(dev, NULL,
Chris Wilsond7619c42009-02-11 14:26:47 +00002263 I915_GEM_GPU_DOMAINS);
2264 if (seqno == 0)
2265 return -ENOMEM;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002266 }
Chris Wilsond7619c42009-02-11 14:26:47 +00002267
2268 ret = i915_wait_request(dev, seqno);
2269 if (ret)
2270 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002271 goto try_again;
2272 }
2273
2274 /*
2275 * Zap this virtual mapping so we can set up a fence again
2276 * for this object next time we need it.
2277 */
2278 offset = ((loff_t) reg->obj->map_list.hash.key) << PAGE_SHIFT;
Jesse Barnes79e53942008-11-07 14:24:08 -08002279 if (dev->dev_mapping)
2280 unmap_mapping_range(dev->dev_mapping, offset,
2281 reg->obj->size, 1);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002282 old_obj_priv->fence_reg = I915_FENCE_REG_NONE;
2283 }
2284
2285 obj_priv->fence_reg = i;
2286 reg->obj = obj;
2287
2288 if (IS_I965G(dev))
2289 i965_write_fence_reg(reg);
2290 else if (IS_I9XX(dev))
2291 i915_write_fence_reg(reg);
2292 else
2293 i830_write_fence_reg(reg);
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002294
2295 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002296}
2297
2298/**
2299 * i915_gem_clear_fence_reg - clear out fence register info
2300 * @obj: object to clear
2301 *
2302 * Zeroes out the fence register itself and clears out the associated
2303 * data structures in dev_priv and obj_priv.
2304 */
2305static void
2306i915_gem_clear_fence_reg(struct drm_gem_object *obj)
2307{
2308 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002309 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002310 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2311
2312 if (IS_I965G(dev))
2313 I915_WRITE64(FENCE_REG_965_0 + (obj_priv->fence_reg * 8), 0);
Eric Anholtdc529a42009-03-10 22:34:49 -07002314 else {
2315 uint32_t fence_reg;
2316
2317 if (obj_priv->fence_reg < 8)
2318 fence_reg = FENCE_REG_830_0 + obj_priv->fence_reg * 4;
2319 else
2320 fence_reg = FENCE_REG_945_8 + (obj_priv->fence_reg -
2321 8) * 4;
2322
2323 I915_WRITE(fence_reg, 0);
2324 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002325
2326 dev_priv->fence_regs[obj_priv->fence_reg].obj = NULL;
2327 obj_priv->fence_reg = I915_FENCE_REG_NONE;
2328}
2329
Eric Anholt673a3942008-07-30 12:06:12 -07002330/**
Chris Wilson52dc7d32009-06-06 09:46:01 +01002331 * i915_gem_object_put_fence_reg - waits on outstanding fenced access
2332 * to the buffer to finish, and then resets the fence register.
2333 * @obj: tiled object holding a fence register.
2334 *
2335 * Zeroes out the fence register itself and clears out the associated
2336 * data structures in dev_priv and obj_priv.
2337 */
2338int
2339i915_gem_object_put_fence_reg(struct drm_gem_object *obj)
2340{
2341 struct drm_device *dev = obj->dev;
2342 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2343
2344 if (obj_priv->fence_reg == I915_FENCE_REG_NONE)
2345 return 0;
2346
2347 /* On the i915, GPU access to tiled buffers is via a fence,
2348 * therefore we must wait for any outstanding access to complete
2349 * before clearing the fence.
2350 */
2351 if (!IS_I965G(dev)) {
2352 int ret;
2353
2354 i915_gem_object_flush_gpu_write_domain(obj);
2355 i915_gem_object_flush_gtt_write_domain(obj);
2356 ret = i915_gem_object_wait_rendering(obj);
2357 if (ret != 0)
2358 return ret;
2359 }
2360
2361 i915_gem_clear_fence_reg (obj);
2362
2363 return 0;
2364}
2365
2366/**
Eric Anholt673a3942008-07-30 12:06:12 -07002367 * Finds free space in the GTT aperture and binds the object there.
2368 */
2369static int
2370i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment)
2371{
2372 struct drm_device *dev = obj->dev;
2373 drm_i915_private_t *dev_priv = dev->dev_private;
2374 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2375 struct drm_mm_node *free_space;
2376 int page_count, ret;
2377
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08002378 if (dev_priv->mm.suspended)
2379 return -EBUSY;
Eric Anholt673a3942008-07-30 12:06:12 -07002380 if (alignment == 0)
Jesse Barnes0f973f22009-01-26 17:10:45 -08002381 alignment = i915_gem_get_gtt_alignment(obj);
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002382 if (alignment & (i915_gem_get_gtt_alignment(obj) - 1)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002383 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2384 return -EINVAL;
2385 }
2386
2387 search_free:
2388 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
2389 obj->size, alignment, 0);
2390 if (free_space != NULL) {
2391 obj_priv->gtt_space = drm_mm_get_block(free_space, obj->size,
2392 alignment);
2393 if (obj_priv->gtt_space != NULL) {
2394 obj_priv->gtt_space->private = obj;
2395 obj_priv->gtt_offset = obj_priv->gtt_space->start;
2396 }
2397 }
2398 if (obj_priv->gtt_space == NULL) {
Carl Worth5e118f42009-03-20 11:54:25 -07002399 bool lists_empty;
2400
Eric Anholt673a3942008-07-30 12:06:12 -07002401 /* If the gtt is empty and we're still having trouble
2402 * fitting our object in, we're out of memory.
2403 */
2404#if WATCH_LRU
2405 DRM_INFO("%s: GTT full, evicting something\n", __func__);
2406#endif
Carl Worth5e118f42009-03-20 11:54:25 -07002407 spin_lock(&dev_priv->mm.active_list_lock);
2408 lists_empty = (list_empty(&dev_priv->mm.inactive_list) &&
2409 list_empty(&dev_priv->mm.flushing_list) &&
2410 list_empty(&dev_priv->mm.active_list));
2411 spin_unlock(&dev_priv->mm.active_list_lock);
2412 if (lists_empty) {
Eric Anholt673a3942008-07-30 12:06:12 -07002413 DRM_ERROR("GTT full, but LRU list empty\n");
Chris Wilson2939e1f2009-06-06 09:46:03 +01002414 return -ENOSPC;
Eric Anholt673a3942008-07-30 12:06:12 -07002415 }
2416
2417 ret = i915_gem_evict_something(dev);
2418 if (ret != 0) {
Keith Packardac94a962008-11-20 23:30:27 -08002419 if (ret != -ERESTARTSYS)
2420 DRM_ERROR("Failed to evict a buffer %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07002421 return ret;
2422 }
2423 goto search_free;
2424 }
2425
2426#if WATCH_BUF
2427 DRM_INFO("Binding object of size %d at 0x%08x\n",
2428 obj->size, obj_priv->gtt_offset);
2429#endif
Eric Anholt856fa192009-03-19 14:10:50 -07002430 ret = i915_gem_object_get_pages(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002431 if (ret) {
2432 drm_mm_put_block(obj_priv->gtt_space);
2433 obj_priv->gtt_space = NULL;
2434 return ret;
2435 }
2436
2437 page_count = obj->size / PAGE_SIZE;
2438 /* Create an AGP memory structure pointing at our pages, and bind it
2439 * into the GTT.
2440 */
2441 obj_priv->agp_mem = drm_agp_bind_pages(dev,
Eric Anholt856fa192009-03-19 14:10:50 -07002442 obj_priv->pages,
Eric Anholt673a3942008-07-30 12:06:12 -07002443 page_count,
Keith Packardba1eb1d2008-10-14 19:55:10 -07002444 obj_priv->gtt_offset,
2445 obj_priv->agp_type);
Eric Anholt673a3942008-07-30 12:06:12 -07002446 if (obj_priv->agp_mem == NULL) {
Eric Anholt856fa192009-03-19 14:10:50 -07002447 i915_gem_object_put_pages(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002448 drm_mm_put_block(obj_priv->gtt_space);
2449 obj_priv->gtt_space = NULL;
2450 return -ENOMEM;
2451 }
2452 atomic_inc(&dev->gtt_count);
2453 atomic_add(obj->size, &dev->gtt_memory);
2454
2455 /* Assert that the object is not currently in any GPU domain. As it
2456 * wasn't in the GTT, there shouldn't be any way it could have been in
2457 * a GPU cache
2458 */
Chris Wilson21d509e2009-06-06 09:46:02 +01002459 BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS);
2460 BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS);
Eric Anholt673a3942008-07-30 12:06:12 -07002461
2462 return 0;
2463}
2464
2465void
2466i915_gem_clflush_object(struct drm_gem_object *obj)
2467{
2468 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2469
2470 /* If we don't have a page list set up, then we're not pinned
2471 * to GPU, and we can ignore the cache flush because it'll happen
2472 * again at bind time.
2473 */
Eric Anholt856fa192009-03-19 14:10:50 -07002474 if (obj_priv->pages == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002475 return;
2476
Eric Anholtcfa16a02009-05-26 18:46:16 -07002477 /* XXX: The 865 in particular appears to be weird in how it handles
2478 * cache flushing. We haven't figured it out, but the
2479 * clflush+agp_chipset_flush doesn't appear to successfully get the
2480 * data visible to the PGU, while wbinvd + agp_chipset_flush does.
2481 */
2482 if (IS_I865G(obj->dev)) {
2483 wbinvd();
2484 return;
2485 }
2486
Eric Anholt856fa192009-03-19 14:10:50 -07002487 drm_clflush_pages(obj_priv->pages, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002488}
2489
Eric Anholte47c68e2008-11-14 13:35:19 -08002490/** Flushes any GPU write domain for the object if it's dirty. */
2491static void
2492i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj)
2493{
2494 struct drm_device *dev = obj->dev;
2495 uint32_t seqno;
2496
2497 if ((obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
2498 return;
2499
2500 /* Queue the GPU write cache flushing we need. */
2501 i915_gem_flush(dev, 0, obj->write_domain);
Eric Anholtb9624422009-06-03 07:27:35 +00002502 seqno = i915_add_request(dev, NULL, obj->write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002503 obj->write_domain = 0;
2504 i915_gem_object_move_to_active(obj, seqno);
2505}
2506
2507/** Flushes the GTT write domain for the object if it's dirty. */
2508static void
2509i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj)
2510{
2511 if (obj->write_domain != I915_GEM_DOMAIN_GTT)
2512 return;
2513
2514 /* No actual flushing is required for the GTT write domain. Writes
2515 * to it immediately go to main memory as far as we know, so there's
2516 * no chipset flush. It also doesn't land in render cache.
2517 */
2518 obj->write_domain = 0;
2519}
2520
2521/** Flushes the CPU write domain for the object if it's dirty. */
2522static void
2523i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj)
2524{
2525 struct drm_device *dev = obj->dev;
2526
2527 if (obj->write_domain != I915_GEM_DOMAIN_CPU)
2528 return;
2529
2530 i915_gem_clflush_object(obj);
2531 drm_agp_chipset_flush(dev);
2532 obj->write_domain = 0;
2533}
2534
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002535/**
2536 * Moves a single object to the GTT read, and possibly write domain.
2537 *
2538 * This function returns when the move is complete, including waiting on
2539 * flushes to occur.
2540 */
Jesse Barnes79e53942008-11-07 14:24:08 -08002541int
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002542i915_gem_object_set_to_gtt_domain(struct drm_gem_object *obj, int write)
2543{
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002544 struct drm_i915_gem_object *obj_priv = obj->driver_private;
Eric Anholte47c68e2008-11-14 13:35:19 -08002545 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002546
Eric Anholt02354392008-11-26 13:58:13 -08002547 /* Not valid to be called on unbound objects. */
2548 if (obj_priv->gtt_space == NULL)
2549 return -EINVAL;
2550
Eric Anholte47c68e2008-11-14 13:35:19 -08002551 i915_gem_object_flush_gpu_write_domain(obj);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002552 /* Wait on any GPU rendering and flushing to occur. */
Eric Anholte47c68e2008-11-14 13:35:19 -08002553 ret = i915_gem_object_wait_rendering(obj);
2554 if (ret != 0)
2555 return ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002556
2557 /* If we're writing through the GTT domain, then CPU and GPU caches
2558 * will need to be invalidated at next use.
2559 */
2560 if (write)
Eric Anholte47c68e2008-11-14 13:35:19 -08002561 obj->read_domains &= I915_GEM_DOMAIN_GTT;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002562
Eric Anholte47c68e2008-11-14 13:35:19 -08002563 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002564
2565 /* It should now be out of any other write domains, and we can update
2566 * the domain values for our changes.
2567 */
2568 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2569 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002570 if (write) {
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002571 obj->write_domain = I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002572 obj_priv->dirty = 1;
2573 }
2574
2575 return 0;
2576}
2577
2578/**
2579 * Moves a single object to the CPU read, and possibly write domain.
2580 *
2581 * This function returns when the move is complete, including waiting on
2582 * flushes to occur.
2583 */
2584static int
2585i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj, int write)
2586{
Eric Anholte47c68e2008-11-14 13:35:19 -08002587 int ret;
2588
2589 i915_gem_object_flush_gpu_write_domain(obj);
2590 /* Wait on any GPU rendering and flushing to occur. */
2591 ret = i915_gem_object_wait_rendering(obj);
2592 if (ret != 0)
2593 return ret;
2594
2595 i915_gem_object_flush_gtt_write_domain(obj);
2596
2597 /* If we have a partially-valid cache of the object in the CPU,
2598 * finish invalidating it and free the per-page flags.
2599 */
2600 i915_gem_object_set_to_full_cpu_read_domain(obj);
2601
2602 /* Flush the CPU cache if it's still invalid. */
2603 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
2604 i915_gem_clflush_object(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08002605
2606 obj->read_domains |= I915_GEM_DOMAIN_CPU;
2607 }
2608
2609 /* It should now be out of any other write domains, and we can update
2610 * the domain values for our changes.
2611 */
2612 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
2613
2614 /* If we're writing through the CPU, then the GPU read domains will
2615 * need to be invalidated at next use.
2616 */
2617 if (write) {
2618 obj->read_domains &= I915_GEM_DOMAIN_CPU;
2619 obj->write_domain = I915_GEM_DOMAIN_CPU;
2620 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002621
2622 return 0;
2623}
2624
Eric Anholt673a3942008-07-30 12:06:12 -07002625/*
2626 * Set the next domain for the specified object. This
2627 * may not actually perform the necessary flushing/invaliding though,
2628 * as that may want to be batched with other set_domain operations
2629 *
2630 * This is (we hope) the only really tricky part of gem. The goal
2631 * is fairly simple -- track which caches hold bits of the object
2632 * and make sure they remain coherent. A few concrete examples may
2633 * help to explain how it works. For shorthand, we use the notation
2634 * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
2635 * a pair of read and write domain masks.
2636 *
2637 * Case 1: the batch buffer
2638 *
2639 * 1. Allocated
2640 * 2. Written by CPU
2641 * 3. Mapped to GTT
2642 * 4. Read by GPU
2643 * 5. Unmapped from GTT
2644 * 6. Freed
2645 *
2646 * Let's take these a step at a time
2647 *
2648 * 1. Allocated
2649 * Pages allocated from the kernel may still have
2650 * cache contents, so we set them to (CPU, CPU) always.
2651 * 2. Written by CPU (using pwrite)
2652 * The pwrite function calls set_domain (CPU, CPU) and
2653 * this function does nothing (as nothing changes)
2654 * 3. Mapped by GTT
2655 * This function asserts that the object is not
2656 * currently in any GPU-based read or write domains
2657 * 4. Read by GPU
2658 * i915_gem_execbuffer calls set_domain (COMMAND, 0).
2659 * As write_domain is zero, this function adds in the
2660 * current read domains (CPU+COMMAND, 0).
2661 * flush_domains is set to CPU.
2662 * invalidate_domains is set to COMMAND
2663 * clflush is run to get data out of the CPU caches
2664 * then i915_dev_set_domain calls i915_gem_flush to
2665 * emit an MI_FLUSH and drm_agp_chipset_flush
2666 * 5. Unmapped from GTT
2667 * i915_gem_object_unbind calls set_domain (CPU, CPU)
2668 * flush_domains and invalidate_domains end up both zero
2669 * so no flushing/invalidating happens
2670 * 6. Freed
2671 * yay, done
2672 *
2673 * Case 2: The shared render buffer
2674 *
2675 * 1. Allocated
2676 * 2. Mapped to GTT
2677 * 3. Read/written by GPU
2678 * 4. set_domain to (CPU,CPU)
2679 * 5. Read/written by CPU
2680 * 6. Read/written by GPU
2681 *
2682 * 1. Allocated
2683 * Same as last example, (CPU, CPU)
2684 * 2. Mapped to GTT
2685 * Nothing changes (assertions find that it is not in the GPU)
2686 * 3. Read/written by GPU
2687 * execbuffer calls set_domain (RENDER, RENDER)
2688 * flush_domains gets CPU
2689 * invalidate_domains gets GPU
2690 * clflush (obj)
2691 * MI_FLUSH and drm_agp_chipset_flush
2692 * 4. set_domain (CPU, CPU)
2693 * flush_domains gets GPU
2694 * invalidate_domains gets CPU
2695 * wait_rendering (obj) to make sure all drawing is complete.
2696 * This will include an MI_FLUSH to get the data from GPU
2697 * to memory
2698 * clflush (obj) to invalidate the CPU cache
2699 * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
2700 * 5. Read/written by CPU
2701 * cache lines are loaded and dirtied
2702 * 6. Read written by GPU
2703 * Same as last GPU access
2704 *
2705 * Case 3: The constant buffer
2706 *
2707 * 1. Allocated
2708 * 2. Written by CPU
2709 * 3. Read by GPU
2710 * 4. Updated (written) by CPU again
2711 * 5. Read by GPU
2712 *
2713 * 1. Allocated
2714 * (CPU, CPU)
2715 * 2. Written by CPU
2716 * (CPU, CPU)
2717 * 3. Read by GPU
2718 * (CPU+RENDER, 0)
2719 * flush_domains = CPU
2720 * invalidate_domains = RENDER
2721 * clflush (obj)
2722 * MI_FLUSH
2723 * drm_agp_chipset_flush
2724 * 4. Updated (written) by CPU again
2725 * (CPU, CPU)
2726 * flush_domains = 0 (no previous write domain)
2727 * invalidate_domains = 0 (no new read domains)
2728 * 5. Read by GPU
2729 * (CPU+RENDER, 0)
2730 * flush_domains = CPU
2731 * invalidate_domains = RENDER
2732 * clflush (obj)
2733 * MI_FLUSH
2734 * drm_agp_chipset_flush
2735 */
Keith Packardc0d90822008-11-20 23:11:08 -08002736static void
Eric Anholt8b0e3782009-02-19 14:40:50 -08002737i915_gem_object_set_to_gpu_domain(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002738{
2739 struct drm_device *dev = obj->dev;
2740 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2741 uint32_t invalidate_domains = 0;
2742 uint32_t flush_domains = 0;
Eric Anholte47c68e2008-11-14 13:35:19 -08002743
Eric Anholt8b0e3782009-02-19 14:40:50 -08002744 BUG_ON(obj->pending_read_domains & I915_GEM_DOMAIN_CPU);
2745 BUG_ON(obj->pending_write_domain == I915_GEM_DOMAIN_CPU);
Eric Anholt673a3942008-07-30 12:06:12 -07002746
2747#if WATCH_BUF
2748 DRM_INFO("%s: object %p read %08x -> %08x write %08x -> %08x\n",
2749 __func__, obj,
Eric Anholt8b0e3782009-02-19 14:40:50 -08002750 obj->read_domains, obj->pending_read_domains,
2751 obj->write_domain, obj->pending_write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07002752#endif
2753 /*
2754 * If the object isn't moving to a new write domain,
2755 * let the object stay in multiple read domains
2756 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08002757 if (obj->pending_write_domain == 0)
2758 obj->pending_read_domains |= obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07002759 else
2760 obj_priv->dirty = 1;
2761
2762 /*
2763 * Flush the current write domain if
2764 * the new read domains don't match. Invalidate
2765 * any read domains which differ from the old
2766 * write domain
2767 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08002768 if (obj->write_domain &&
2769 obj->write_domain != obj->pending_read_domains) {
Eric Anholt673a3942008-07-30 12:06:12 -07002770 flush_domains |= obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08002771 invalidate_domains |=
2772 obj->pending_read_domains & ~obj->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07002773 }
2774 /*
2775 * Invalidate any read caches which may have
2776 * stale data. That is, any new read domains.
2777 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08002778 invalidate_domains |= obj->pending_read_domains & ~obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07002779 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU) {
2780#if WATCH_BUF
2781 DRM_INFO("%s: CPU domain flush %08x invalidate %08x\n",
2782 __func__, flush_domains, invalidate_domains);
2783#endif
Eric Anholt673a3942008-07-30 12:06:12 -07002784 i915_gem_clflush_object(obj);
2785 }
2786
Eric Anholtefbeed92009-02-19 14:54:51 -08002787 /* The actual obj->write_domain will be updated with
2788 * pending_write_domain after we emit the accumulated flush for all
2789 * of our domain changes in execbuffers (which clears objects'
2790 * write_domains). So if we have a current write domain that we
2791 * aren't changing, set pending_write_domain to that.
2792 */
2793 if (flush_domains == 0 && obj->pending_write_domain == 0)
2794 obj->pending_write_domain = obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08002795 obj->read_domains = obj->pending_read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07002796
2797 dev->invalidate_domains |= invalidate_domains;
2798 dev->flush_domains |= flush_domains;
2799#if WATCH_BUF
2800 DRM_INFO("%s: read %08x write %08x invalidate %08x flush %08x\n",
2801 __func__,
2802 obj->read_domains, obj->write_domain,
2803 dev->invalidate_domains, dev->flush_domains);
2804#endif
Eric Anholt673a3942008-07-30 12:06:12 -07002805}
2806
2807/**
Eric Anholte47c68e2008-11-14 13:35:19 -08002808 * Moves the object from a partially CPU read to a full one.
Eric Anholt673a3942008-07-30 12:06:12 -07002809 *
Eric Anholte47c68e2008-11-14 13:35:19 -08002810 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
2811 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
2812 */
2813static void
2814i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj)
2815{
Eric Anholte47c68e2008-11-14 13:35:19 -08002816 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2817
2818 if (!obj_priv->page_cpu_valid)
2819 return;
2820
2821 /* If we're partially in the CPU read domain, finish moving it in.
2822 */
2823 if (obj->read_domains & I915_GEM_DOMAIN_CPU) {
2824 int i;
2825
2826 for (i = 0; i <= (obj->size - 1) / PAGE_SIZE; i++) {
2827 if (obj_priv->page_cpu_valid[i])
2828 continue;
Eric Anholt856fa192009-03-19 14:10:50 -07002829 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholte47c68e2008-11-14 13:35:19 -08002830 }
Eric Anholte47c68e2008-11-14 13:35:19 -08002831 }
2832
2833 /* Free the page_cpu_valid mappings which are now stale, whether
2834 * or not we've got I915_GEM_DOMAIN_CPU.
2835 */
2836 drm_free(obj_priv->page_cpu_valid, obj->size / PAGE_SIZE,
2837 DRM_MEM_DRIVER);
2838 obj_priv->page_cpu_valid = NULL;
2839}
2840
2841/**
2842 * Set the CPU read domain on a range of the object.
2843 *
2844 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
2845 * not entirely valid. The page_cpu_valid member of the object flags which
2846 * pages have been flushed, and will be respected by
2847 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
2848 * of the whole object.
2849 *
2850 * This function returns when the move is complete, including waiting on
2851 * flushes to occur.
Eric Anholt673a3942008-07-30 12:06:12 -07002852 */
2853static int
Eric Anholte47c68e2008-11-14 13:35:19 -08002854i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
2855 uint64_t offset, uint64_t size)
Eric Anholt673a3942008-07-30 12:06:12 -07002856{
2857 struct drm_i915_gem_object *obj_priv = obj->driver_private;
Eric Anholte47c68e2008-11-14 13:35:19 -08002858 int i, ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002859
Eric Anholte47c68e2008-11-14 13:35:19 -08002860 if (offset == 0 && size == obj->size)
2861 return i915_gem_object_set_to_cpu_domain(obj, 0);
2862
2863 i915_gem_object_flush_gpu_write_domain(obj);
2864 /* Wait on any GPU rendering and flushing to occur. */
2865 ret = i915_gem_object_wait_rendering(obj);
2866 if (ret != 0)
2867 return ret;
2868 i915_gem_object_flush_gtt_write_domain(obj);
2869
2870 /* If we're already fully in the CPU read domain, we're done. */
2871 if (obj_priv->page_cpu_valid == NULL &&
2872 (obj->read_domains & I915_GEM_DOMAIN_CPU) != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07002873 return 0;
2874
Eric Anholte47c68e2008-11-14 13:35:19 -08002875 /* Otherwise, create/clear the per-page CPU read domain flag if we're
2876 * newly adding I915_GEM_DOMAIN_CPU
2877 */
Eric Anholt673a3942008-07-30 12:06:12 -07002878 if (obj_priv->page_cpu_valid == NULL) {
2879 obj_priv->page_cpu_valid = drm_calloc(1, obj->size / PAGE_SIZE,
2880 DRM_MEM_DRIVER);
Eric Anholte47c68e2008-11-14 13:35:19 -08002881 if (obj_priv->page_cpu_valid == NULL)
2882 return -ENOMEM;
2883 } else if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0)
2884 memset(obj_priv->page_cpu_valid, 0, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002885
2886 /* Flush the cache on any pages that are still invalid from the CPU's
2887 * perspective.
2888 */
Eric Anholte47c68e2008-11-14 13:35:19 -08002889 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
2890 i++) {
Eric Anholt673a3942008-07-30 12:06:12 -07002891 if (obj_priv->page_cpu_valid[i])
2892 continue;
2893
Eric Anholt856fa192009-03-19 14:10:50 -07002894 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07002895
2896 obj_priv->page_cpu_valid[i] = 1;
2897 }
2898
Eric Anholte47c68e2008-11-14 13:35:19 -08002899 /* It should now be out of any other write domains, and we can update
2900 * the domain values for our changes.
2901 */
2902 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
2903
2904 obj->read_domains |= I915_GEM_DOMAIN_CPU;
2905
Eric Anholt673a3942008-07-30 12:06:12 -07002906 return 0;
2907}
2908
2909/**
Eric Anholt673a3942008-07-30 12:06:12 -07002910 * Pin an object to the GTT and evaluate the relocations landing in it.
2911 */
2912static int
2913i915_gem_object_pin_and_relocate(struct drm_gem_object *obj,
2914 struct drm_file *file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002915 struct drm_i915_gem_exec_object *entry,
2916 struct drm_i915_gem_relocation_entry *relocs)
Eric Anholt673a3942008-07-30 12:06:12 -07002917{
2918 struct drm_device *dev = obj->dev;
Keith Packard0839ccb2008-10-30 19:38:48 -07002919 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07002920 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2921 int i, ret;
Keith Packard0839ccb2008-10-30 19:38:48 -07002922 void __iomem *reloc_page;
Eric Anholt673a3942008-07-30 12:06:12 -07002923
2924 /* Choose the GTT offset for our buffer and put it there. */
2925 ret = i915_gem_object_pin(obj, (uint32_t) entry->alignment);
2926 if (ret)
2927 return ret;
2928
2929 entry->offset = obj_priv->gtt_offset;
2930
Eric Anholt673a3942008-07-30 12:06:12 -07002931 /* Apply the relocations, using the GTT aperture to avoid cache
2932 * flushing requirements.
2933 */
2934 for (i = 0; i < entry->relocation_count; i++) {
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002935 struct drm_i915_gem_relocation_entry *reloc= &relocs[i];
Eric Anholt673a3942008-07-30 12:06:12 -07002936 struct drm_gem_object *target_obj;
2937 struct drm_i915_gem_object *target_obj_priv;
Eric Anholt3043c602008-10-02 12:24:47 -07002938 uint32_t reloc_val, reloc_offset;
2939 uint32_t __iomem *reloc_entry;
Eric Anholt673a3942008-07-30 12:06:12 -07002940
Eric Anholt673a3942008-07-30 12:06:12 -07002941 target_obj = drm_gem_object_lookup(obj->dev, file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002942 reloc->target_handle);
Eric Anholt673a3942008-07-30 12:06:12 -07002943 if (target_obj == NULL) {
2944 i915_gem_object_unpin(obj);
2945 return -EBADF;
2946 }
2947 target_obj_priv = target_obj->driver_private;
2948
2949 /* The target buffer should have appeared before us in the
2950 * exec_object list, so it should have a GTT space bound by now.
2951 */
2952 if (target_obj_priv->gtt_space == NULL) {
2953 DRM_ERROR("No GTT space found for object %d\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002954 reloc->target_handle);
Eric Anholt673a3942008-07-30 12:06:12 -07002955 drm_gem_object_unreference(target_obj);
2956 i915_gem_object_unpin(obj);
2957 return -EINVAL;
2958 }
2959
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002960 if (reloc->offset > obj->size - 4) {
Eric Anholt673a3942008-07-30 12:06:12 -07002961 DRM_ERROR("Relocation beyond object bounds: "
2962 "obj %p target %d offset %d size %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002963 obj, reloc->target_handle,
2964 (int) reloc->offset, (int) obj->size);
Eric Anholt673a3942008-07-30 12:06:12 -07002965 drm_gem_object_unreference(target_obj);
2966 i915_gem_object_unpin(obj);
2967 return -EINVAL;
2968 }
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002969 if (reloc->offset & 3) {
Eric Anholt673a3942008-07-30 12:06:12 -07002970 DRM_ERROR("Relocation not 4-byte aligned: "
2971 "obj %p target %d offset %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002972 obj, reloc->target_handle,
2973 (int) reloc->offset);
Eric Anholt673a3942008-07-30 12:06:12 -07002974 drm_gem_object_unreference(target_obj);
2975 i915_gem_object_unpin(obj);
2976 return -EINVAL;
2977 }
2978
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002979 if (reloc->write_domain & I915_GEM_DOMAIN_CPU ||
2980 reloc->read_domains & I915_GEM_DOMAIN_CPU) {
Eric Anholte47c68e2008-11-14 13:35:19 -08002981 DRM_ERROR("reloc with read/write CPU domains: "
2982 "obj %p target %d offset %d "
2983 "read %08x write %08x",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002984 obj, reloc->target_handle,
2985 (int) reloc->offset,
2986 reloc->read_domains,
2987 reloc->write_domain);
Chris Wilson491152b2009-02-11 14:26:32 +00002988 drm_gem_object_unreference(target_obj);
2989 i915_gem_object_unpin(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08002990 return -EINVAL;
2991 }
2992
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002993 if (reloc->write_domain && target_obj->pending_write_domain &&
2994 reloc->write_domain != target_obj->pending_write_domain) {
Eric Anholt673a3942008-07-30 12:06:12 -07002995 DRM_ERROR("Write domain conflict: "
2996 "obj %p target %d offset %d "
2997 "new %08x old %08x\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002998 obj, reloc->target_handle,
2999 (int) reloc->offset,
3000 reloc->write_domain,
Eric Anholt673a3942008-07-30 12:06:12 -07003001 target_obj->pending_write_domain);
3002 drm_gem_object_unreference(target_obj);
3003 i915_gem_object_unpin(obj);
3004 return -EINVAL;
3005 }
3006
3007#if WATCH_RELOC
3008 DRM_INFO("%s: obj %p offset %08x target %d "
3009 "read %08x write %08x gtt %08x "
3010 "presumed %08x delta %08x\n",
3011 __func__,
3012 obj,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003013 (int) reloc->offset,
3014 (int) reloc->target_handle,
3015 (int) reloc->read_domains,
3016 (int) reloc->write_domain,
Eric Anholt673a3942008-07-30 12:06:12 -07003017 (int) target_obj_priv->gtt_offset,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003018 (int) reloc->presumed_offset,
3019 reloc->delta);
Eric Anholt673a3942008-07-30 12:06:12 -07003020#endif
3021
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003022 target_obj->pending_read_domains |= reloc->read_domains;
3023 target_obj->pending_write_domain |= reloc->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07003024
3025 /* If the relocation already has the right value in it, no
3026 * more work needs to be done.
3027 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003028 if (target_obj_priv->gtt_offset == reloc->presumed_offset) {
Eric Anholt673a3942008-07-30 12:06:12 -07003029 drm_gem_object_unreference(target_obj);
3030 continue;
3031 }
3032
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003033 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
3034 if (ret != 0) {
3035 drm_gem_object_unreference(target_obj);
3036 i915_gem_object_unpin(obj);
3037 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -07003038 }
3039
3040 /* Map the page containing the relocation we're going to
3041 * perform.
3042 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003043 reloc_offset = obj_priv->gtt_offset + reloc->offset;
Keith Packard0839ccb2008-10-30 19:38:48 -07003044 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
3045 (reloc_offset &
3046 ~(PAGE_SIZE - 1)));
Eric Anholt3043c602008-10-02 12:24:47 -07003047 reloc_entry = (uint32_t __iomem *)(reloc_page +
Keith Packard0839ccb2008-10-30 19:38:48 -07003048 (reloc_offset & (PAGE_SIZE - 1)));
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003049 reloc_val = target_obj_priv->gtt_offset + reloc->delta;
Eric Anholt673a3942008-07-30 12:06:12 -07003050
3051#if WATCH_BUF
3052 DRM_INFO("Applied relocation: %p@0x%08x %08x -> %08x\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003053 obj, (unsigned int) reloc->offset,
Eric Anholt673a3942008-07-30 12:06:12 -07003054 readl(reloc_entry), reloc_val);
3055#endif
3056 writel(reloc_val, reloc_entry);
Keith Packard0839ccb2008-10-30 19:38:48 -07003057 io_mapping_unmap_atomic(reloc_page);
Eric Anholt673a3942008-07-30 12:06:12 -07003058
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003059 /* The updated presumed offset for this entry will be
3060 * copied back out to the user.
Eric Anholt673a3942008-07-30 12:06:12 -07003061 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003062 reloc->presumed_offset = target_obj_priv->gtt_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003063
3064 drm_gem_object_unreference(target_obj);
3065 }
3066
Eric Anholt673a3942008-07-30 12:06:12 -07003067#if WATCH_BUF
3068 if (0)
3069 i915_gem_dump_object(obj, 128, __func__, ~0);
3070#endif
3071 return 0;
3072}
3073
3074/** Dispatch a batchbuffer to the ring
3075 */
3076static int
3077i915_dispatch_gem_execbuffer(struct drm_device *dev,
3078 struct drm_i915_gem_execbuffer *exec,
Eric Anholt201361a2009-03-11 12:30:04 -07003079 struct drm_clip_rect *cliprects,
Eric Anholt673a3942008-07-30 12:06:12 -07003080 uint64_t exec_offset)
3081{
3082 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003083 int nbox = exec->num_cliprects;
3084 int i = 0, count;
Chris Wilson83d60792009-06-06 09:45:57 +01003085 uint32_t exec_start, exec_len;
Eric Anholt673a3942008-07-30 12:06:12 -07003086 RING_LOCALS;
3087
3088 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
3089 exec_len = (uint32_t) exec->batch_len;
3090
Eric Anholt673a3942008-07-30 12:06:12 -07003091 count = nbox ? nbox : 1;
3092
3093 for (i = 0; i < count; i++) {
3094 if (i < nbox) {
Eric Anholt201361a2009-03-11 12:30:04 -07003095 int ret = i915_emit_box(dev, cliprects, i,
Eric Anholt673a3942008-07-30 12:06:12 -07003096 exec->DR1, exec->DR4);
3097 if (ret)
3098 return ret;
3099 }
3100
3101 if (IS_I830(dev) || IS_845G(dev)) {
3102 BEGIN_LP_RING(4);
3103 OUT_RING(MI_BATCH_BUFFER);
3104 OUT_RING(exec_start | MI_BATCH_NON_SECURE);
3105 OUT_RING(exec_start + exec_len - 4);
3106 OUT_RING(0);
3107 ADVANCE_LP_RING();
3108 } else {
3109 BEGIN_LP_RING(2);
3110 if (IS_I965G(dev)) {
3111 OUT_RING(MI_BATCH_BUFFER_START |
3112 (2 << 6) |
3113 MI_BATCH_NON_SECURE_I965);
3114 OUT_RING(exec_start);
3115 } else {
3116 OUT_RING(MI_BATCH_BUFFER_START |
3117 (2 << 6));
3118 OUT_RING(exec_start | MI_BATCH_NON_SECURE);
3119 }
3120 ADVANCE_LP_RING();
3121 }
3122 }
3123
3124 /* XXX breadcrumb */
3125 return 0;
3126}
3127
3128/* Throttle our rendering by waiting until the ring has completed our requests
3129 * emitted over 20 msec ago.
3130 *
Eric Anholtb9624422009-06-03 07:27:35 +00003131 * Note that if we were to use the current jiffies each time around the loop,
3132 * we wouldn't escape the function with any frames outstanding if the time to
3133 * render a frame was over 20ms.
3134 *
Eric Anholt673a3942008-07-30 12:06:12 -07003135 * This should get us reasonable parallelism between CPU and GPU but also
3136 * relatively low latency when blocking on a particular request to finish.
3137 */
3138static int
3139i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file_priv)
3140{
3141 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
3142 int ret = 0;
Eric Anholtb9624422009-06-03 07:27:35 +00003143 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
Eric Anholt673a3942008-07-30 12:06:12 -07003144
3145 mutex_lock(&dev->struct_mutex);
Eric Anholtb9624422009-06-03 07:27:35 +00003146 while (!list_empty(&i915_file_priv->mm.request_list)) {
3147 struct drm_i915_gem_request *request;
3148
3149 request = list_first_entry(&i915_file_priv->mm.request_list,
3150 struct drm_i915_gem_request,
3151 client_list);
3152
3153 if (time_after_eq(request->emitted_jiffies, recent_enough))
3154 break;
3155
3156 ret = i915_wait_request(dev, request->seqno);
3157 if (ret != 0)
3158 break;
3159 }
Eric Anholt673a3942008-07-30 12:06:12 -07003160 mutex_unlock(&dev->struct_mutex);
Eric Anholtb9624422009-06-03 07:27:35 +00003161
Eric Anholt673a3942008-07-30 12:06:12 -07003162 return ret;
3163}
3164
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003165static int
3166i915_gem_get_relocs_from_user(struct drm_i915_gem_exec_object *exec_list,
3167 uint32_t buffer_count,
3168 struct drm_i915_gem_relocation_entry **relocs)
3169{
3170 uint32_t reloc_count = 0, reloc_index = 0, i;
3171 int ret;
3172
3173 *relocs = NULL;
3174 for (i = 0; i < buffer_count; i++) {
3175 if (reloc_count + exec_list[i].relocation_count < reloc_count)
3176 return -EINVAL;
3177 reloc_count += exec_list[i].relocation_count;
3178 }
3179
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003180 *relocs = drm_calloc_large(reloc_count, sizeof(**relocs));
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003181 if (*relocs == NULL)
3182 return -ENOMEM;
3183
3184 for (i = 0; i < buffer_count; i++) {
3185 struct drm_i915_gem_relocation_entry __user *user_relocs;
3186
3187 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3188
3189 ret = copy_from_user(&(*relocs)[reloc_index],
3190 user_relocs,
3191 exec_list[i].relocation_count *
3192 sizeof(**relocs));
3193 if (ret != 0) {
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003194 drm_free_large(*relocs);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003195 *relocs = NULL;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003196 return -EFAULT;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003197 }
3198
3199 reloc_index += exec_list[i].relocation_count;
3200 }
3201
Florian Mickler2bc43b52009-04-06 22:55:41 +02003202 return 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003203}
3204
3205static int
3206i915_gem_put_relocs_to_user(struct drm_i915_gem_exec_object *exec_list,
3207 uint32_t buffer_count,
3208 struct drm_i915_gem_relocation_entry *relocs)
3209{
3210 uint32_t reloc_count = 0, i;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003211 int ret = 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003212
3213 for (i = 0; i < buffer_count; i++) {
3214 struct drm_i915_gem_relocation_entry __user *user_relocs;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003215 int unwritten;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003216
3217 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3218
Florian Mickler2bc43b52009-04-06 22:55:41 +02003219 unwritten = copy_to_user(user_relocs,
3220 &relocs[reloc_count],
3221 exec_list[i].relocation_count *
3222 sizeof(*relocs));
3223
3224 if (unwritten) {
3225 ret = -EFAULT;
3226 goto err;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003227 }
3228
3229 reloc_count += exec_list[i].relocation_count;
3230 }
3231
Florian Mickler2bc43b52009-04-06 22:55:41 +02003232err:
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003233 drm_free_large(relocs);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003234
3235 return ret;
3236}
3237
Chris Wilson83d60792009-06-06 09:45:57 +01003238static int
3239i915_gem_check_execbuffer (struct drm_i915_gem_execbuffer *exec,
3240 uint64_t exec_offset)
3241{
3242 uint32_t exec_start, exec_len;
3243
3244 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
3245 exec_len = (uint32_t) exec->batch_len;
3246
3247 if ((exec_start | exec_len) & 0x7)
3248 return -EINVAL;
3249
3250 if (!exec_start)
3251 return -EINVAL;
3252
3253 return 0;
3254}
3255
Eric Anholt673a3942008-07-30 12:06:12 -07003256int
3257i915_gem_execbuffer(struct drm_device *dev, void *data,
3258 struct drm_file *file_priv)
3259{
3260 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003261 struct drm_i915_gem_execbuffer *args = data;
3262 struct drm_i915_gem_exec_object *exec_list = NULL;
3263 struct drm_gem_object **object_list = NULL;
3264 struct drm_gem_object *batch_obj;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003265 struct drm_i915_gem_object *obj_priv;
Eric Anholt201361a2009-03-11 12:30:04 -07003266 struct drm_clip_rect *cliprects = NULL;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003267 struct drm_i915_gem_relocation_entry *relocs;
3268 int ret, ret2, i, pinned = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003269 uint64_t exec_offset;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003270 uint32_t seqno, flush_domains, reloc_index;
Keith Packardac94a962008-11-20 23:30:27 -08003271 int pin_tries;
Eric Anholt673a3942008-07-30 12:06:12 -07003272
3273#if WATCH_EXEC
3274 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3275 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3276#endif
3277
Eric Anholt4f481ed2008-09-10 14:22:49 -07003278 if (args->buffer_count < 1) {
3279 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3280 return -EINVAL;
3281 }
Eric Anholt673a3942008-07-30 12:06:12 -07003282 /* Copy in the exec list from userland */
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003283 exec_list = drm_calloc_large(sizeof(*exec_list), args->buffer_count);
3284 object_list = drm_calloc_large(sizeof(*object_list), args->buffer_count);
Eric Anholt673a3942008-07-30 12:06:12 -07003285 if (exec_list == NULL || object_list == NULL) {
3286 DRM_ERROR("Failed to allocate exec or object list "
3287 "for %d buffers\n",
3288 args->buffer_count);
3289 ret = -ENOMEM;
3290 goto pre_mutex_err;
3291 }
3292 ret = copy_from_user(exec_list,
3293 (struct drm_i915_relocation_entry __user *)
3294 (uintptr_t) args->buffers_ptr,
3295 sizeof(*exec_list) * args->buffer_count);
3296 if (ret != 0) {
3297 DRM_ERROR("copy %d exec entries failed %d\n",
3298 args->buffer_count, ret);
3299 goto pre_mutex_err;
3300 }
3301
Eric Anholt201361a2009-03-11 12:30:04 -07003302 if (args->num_cliprects != 0) {
3303 cliprects = drm_calloc(args->num_cliprects, sizeof(*cliprects),
3304 DRM_MEM_DRIVER);
3305 if (cliprects == NULL)
3306 goto pre_mutex_err;
3307
3308 ret = copy_from_user(cliprects,
3309 (struct drm_clip_rect __user *)
3310 (uintptr_t) args->cliprects_ptr,
3311 sizeof(*cliprects) * args->num_cliprects);
3312 if (ret != 0) {
3313 DRM_ERROR("copy %d cliprects failed: %d\n",
3314 args->num_cliprects, ret);
3315 goto pre_mutex_err;
3316 }
3317 }
3318
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003319 ret = i915_gem_get_relocs_from_user(exec_list, args->buffer_count,
3320 &relocs);
3321 if (ret != 0)
3322 goto pre_mutex_err;
3323
Eric Anholt673a3942008-07-30 12:06:12 -07003324 mutex_lock(&dev->struct_mutex);
3325
3326 i915_verify_inactive(dev, __FILE__, __LINE__);
3327
3328 if (dev_priv->mm.wedged) {
3329 DRM_ERROR("Execbuf while wedged\n");
3330 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003331 ret = -EIO;
3332 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003333 }
3334
3335 if (dev_priv->mm.suspended) {
3336 DRM_ERROR("Execbuf while VT-switched.\n");
3337 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003338 ret = -EBUSY;
3339 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003340 }
3341
Keith Packardac94a962008-11-20 23:30:27 -08003342 /* Look up object handles */
Eric Anholt673a3942008-07-30 12:06:12 -07003343 for (i = 0; i < args->buffer_count; i++) {
3344 object_list[i] = drm_gem_object_lookup(dev, file_priv,
3345 exec_list[i].handle);
3346 if (object_list[i] == NULL) {
3347 DRM_ERROR("Invalid object handle %d at index %d\n",
3348 exec_list[i].handle, i);
3349 ret = -EBADF;
3350 goto err;
3351 }
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003352
3353 obj_priv = object_list[i]->driver_private;
3354 if (obj_priv->in_execbuffer) {
3355 DRM_ERROR("Object %p appears more than once in object list\n",
3356 object_list[i]);
3357 ret = -EBADF;
3358 goto err;
3359 }
3360 obj_priv->in_execbuffer = true;
Keith Packardac94a962008-11-20 23:30:27 -08003361 }
Eric Anholt673a3942008-07-30 12:06:12 -07003362
Keith Packardac94a962008-11-20 23:30:27 -08003363 /* Pin and relocate */
3364 for (pin_tries = 0; ; pin_tries++) {
3365 ret = 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003366 reloc_index = 0;
3367
Keith Packardac94a962008-11-20 23:30:27 -08003368 for (i = 0; i < args->buffer_count; i++) {
3369 object_list[i]->pending_read_domains = 0;
3370 object_list[i]->pending_write_domain = 0;
3371 ret = i915_gem_object_pin_and_relocate(object_list[i],
3372 file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003373 &exec_list[i],
3374 &relocs[reloc_index]);
Keith Packardac94a962008-11-20 23:30:27 -08003375 if (ret)
3376 break;
3377 pinned = i + 1;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003378 reloc_index += exec_list[i].relocation_count;
Keith Packardac94a962008-11-20 23:30:27 -08003379 }
3380 /* success */
3381 if (ret == 0)
3382 break;
3383
3384 /* error other than GTT full, or we've already tried again */
Chris Wilson2939e1f2009-06-06 09:46:03 +01003385 if (ret != -ENOSPC || pin_tries >= 1) {
Eric Anholtf1acec92008-12-19 14:47:48 -08003386 if (ret != -ERESTARTSYS)
3387 DRM_ERROR("Failed to pin buffers %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07003388 goto err;
3389 }
Keith Packardac94a962008-11-20 23:30:27 -08003390
3391 /* unpin all of our buffers */
3392 for (i = 0; i < pinned; i++)
3393 i915_gem_object_unpin(object_list[i]);
Eric Anholtb1177632008-12-10 10:09:41 -08003394 pinned = 0;
Keith Packardac94a962008-11-20 23:30:27 -08003395
3396 /* evict everyone we can from the aperture */
3397 ret = i915_gem_evict_everything(dev);
3398 if (ret)
3399 goto err;
Eric Anholt673a3942008-07-30 12:06:12 -07003400 }
3401
3402 /* Set the pending read domains for the batch buffer to COMMAND */
3403 batch_obj = object_list[args->buffer_count-1];
Chris Wilson5f26a2c2009-06-06 09:45:58 +01003404 if (batch_obj->pending_write_domain) {
3405 DRM_ERROR("Attempting to use self-modifying batch buffer\n");
3406 ret = -EINVAL;
3407 goto err;
3408 }
3409 batch_obj->pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
Eric Anholt673a3942008-07-30 12:06:12 -07003410
Chris Wilson83d60792009-06-06 09:45:57 +01003411 /* Sanity check the batch buffer, prior to moving objects */
3412 exec_offset = exec_list[args->buffer_count - 1].offset;
3413 ret = i915_gem_check_execbuffer (args, exec_offset);
3414 if (ret != 0) {
3415 DRM_ERROR("execbuf with invalid offset/length\n");
3416 goto err;
3417 }
3418
Eric Anholt673a3942008-07-30 12:06:12 -07003419 i915_verify_inactive(dev, __FILE__, __LINE__);
3420
Keith Packard646f0f62008-11-20 23:23:03 -08003421 /* Zero the global flush/invalidate flags. These
3422 * will be modified as new domains are computed
3423 * for each object
3424 */
3425 dev->invalidate_domains = 0;
3426 dev->flush_domains = 0;
3427
Eric Anholt673a3942008-07-30 12:06:12 -07003428 for (i = 0; i < args->buffer_count; i++) {
3429 struct drm_gem_object *obj = object_list[i];
Eric Anholt673a3942008-07-30 12:06:12 -07003430
Keith Packard646f0f62008-11-20 23:23:03 -08003431 /* Compute new gpu domains and update invalidate/flush */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003432 i915_gem_object_set_to_gpu_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003433 }
3434
3435 i915_verify_inactive(dev, __FILE__, __LINE__);
3436
Keith Packard646f0f62008-11-20 23:23:03 -08003437 if (dev->invalidate_domains | dev->flush_domains) {
3438#if WATCH_EXEC
3439 DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
3440 __func__,
3441 dev->invalidate_domains,
3442 dev->flush_domains);
3443#endif
3444 i915_gem_flush(dev,
3445 dev->invalidate_domains,
3446 dev->flush_domains);
3447 if (dev->flush_domains)
Eric Anholtb9624422009-06-03 07:27:35 +00003448 (void)i915_add_request(dev, file_priv,
3449 dev->flush_domains);
Keith Packard646f0f62008-11-20 23:23:03 -08003450 }
Eric Anholt673a3942008-07-30 12:06:12 -07003451
Eric Anholtefbeed92009-02-19 14:54:51 -08003452 for (i = 0; i < args->buffer_count; i++) {
3453 struct drm_gem_object *obj = object_list[i];
3454
3455 obj->write_domain = obj->pending_write_domain;
3456 }
3457
Eric Anholt673a3942008-07-30 12:06:12 -07003458 i915_verify_inactive(dev, __FILE__, __LINE__);
3459
3460#if WATCH_COHERENCY
3461 for (i = 0; i < args->buffer_count; i++) {
3462 i915_gem_object_check_coherency(object_list[i],
3463 exec_list[i].handle);
3464 }
3465#endif
3466
Eric Anholt673a3942008-07-30 12:06:12 -07003467#if WATCH_EXEC
Ben Gamari6911a9b2009-04-02 11:24:54 -07003468 i915_gem_dump_object(batch_obj,
Eric Anholt673a3942008-07-30 12:06:12 -07003469 args->batch_len,
3470 __func__,
3471 ~0);
3472#endif
3473
Eric Anholt673a3942008-07-30 12:06:12 -07003474 /* Exec the batchbuffer */
Eric Anholt201361a2009-03-11 12:30:04 -07003475 ret = i915_dispatch_gem_execbuffer(dev, args, cliprects, exec_offset);
Eric Anholt673a3942008-07-30 12:06:12 -07003476 if (ret) {
3477 DRM_ERROR("dispatch failed %d\n", ret);
3478 goto err;
3479 }
3480
3481 /*
3482 * Ensure that the commands in the batch buffer are
3483 * finished before the interrupt fires
3484 */
3485 flush_domains = i915_retire_commands(dev);
3486
3487 i915_verify_inactive(dev, __FILE__, __LINE__);
3488
3489 /*
3490 * Get a seqno representing the execution of the current buffer,
3491 * which we can wait on. We would like to mitigate these interrupts,
3492 * likely by only creating seqnos occasionally (so that we have
3493 * *some* interrupts representing completion of buffers that we can
3494 * wait on when trying to clear up gtt space).
3495 */
Eric Anholtb9624422009-06-03 07:27:35 +00003496 seqno = i915_add_request(dev, file_priv, flush_domains);
Eric Anholt673a3942008-07-30 12:06:12 -07003497 BUG_ON(seqno == 0);
Eric Anholt673a3942008-07-30 12:06:12 -07003498 for (i = 0; i < args->buffer_count; i++) {
3499 struct drm_gem_object *obj = object_list[i];
Eric Anholt673a3942008-07-30 12:06:12 -07003500
Eric Anholtce44b0e2008-11-06 16:00:31 -08003501 i915_gem_object_move_to_active(obj, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07003502#if WATCH_LRU
3503 DRM_INFO("%s: move to exec list %p\n", __func__, obj);
3504#endif
3505 }
3506#if WATCH_LRU
3507 i915_dump_lru(dev, __func__);
3508#endif
3509
3510 i915_verify_inactive(dev, __FILE__, __LINE__);
3511
Eric Anholt673a3942008-07-30 12:06:12 -07003512err:
Julia Lawallaad87df2008-12-21 16:28:47 +01003513 for (i = 0; i < pinned; i++)
3514 i915_gem_object_unpin(object_list[i]);
Eric Anholt673a3942008-07-30 12:06:12 -07003515
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003516 for (i = 0; i < args->buffer_count; i++) {
3517 if (object_list[i]) {
3518 obj_priv = object_list[i]->driver_private;
3519 obj_priv->in_execbuffer = false;
3520 }
Julia Lawallaad87df2008-12-21 16:28:47 +01003521 drm_gem_object_unreference(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003522 }
Julia Lawallaad87df2008-12-21 16:28:47 +01003523
Eric Anholt673a3942008-07-30 12:06:12 -07003524 mutex_unlock(&dev->struct_mutex);
3525
Roland Dreiera35f2e22009-02-06 17:48:09 -08003526 if (!ret) {
3527 /* Copy the new buffer offsets back to the user's exec list. */
3528 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
3529 (uintptr_t) args->buffers_ptr,
3530 exec_list,
3531 sizeof(*exec_list) * args->buffer_count);
Florian Mickler2bc43b52009-04-06 22:55:41 +02003532 if (ret) {
3533 ret = -EFAULT;
Roland Dreiera35f2e22009-02-06 17:48:09 -08003534 DRM_ERROR("failed to copy %d exec entries "
3535 "back to user (%d)\n",
3536 args->buffer_count, ret);
Florian Mickler2bc43b52009-04-06 22:55:41 +02003537 }
Roland Dreiera35f2e22009-02-06 17:48:09 -08003538 }
3539
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003540 /* Copy the updated relocations out regardless of current error
3541 * state. Failure to update the relocs would mean that the next
3542 * time userland calls execbuf, it would do so with presumed offset
3543 * state that didn't match the actual object state.
3544 */
3545 ret2 = i915_gem_put_relocs_to_user(exec_list, args->buffer_count,
3546 relocs);
3547 if (ret2 != 0) {
3548 DRM_ERROR("Failed to copy relocations back out: %d\n", ret2);
3549
3550 if (ret == 0)
3551 ret = ret2;
3552 }
3553
Eric Anholt673a3942008-07-30 12:06:12 -07003554pre_mutex_err:
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003555 drm_free_large(object_list);
3556 drm_free_large(exec_list);
Eric Anholt201361a2009-03-11 12:30:04 -07003557 drm_free(cliprects, sizeof(*cliprects) * args->num_cliprects,
3558 DRM_MEM_DRIVER);
Eric Anholt673a3942008-07-30 12:06:12 -07003559
3560 return ret;
3561}
3562
3563int
3564i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment)
3565{
3566 struct drm_device *dev = obj->dev;
3567 struct drm_i915_gem_object *obj_priv = obj->driver_private;
3568 int ret;
3569
3570 i915_verify_inactive(dev, __FILE__, __LINE__);
3571 if (obj_priv->gtt_space == NULL) {
3572 ret = i915_gem_object_bind_to_gtt(obj, alignment);
3573 if (ret != 0) {
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08003574 if (ret != -EBUSY && ret != -ERESTARTSYS)
Kyle McMartin0fce81e2009-02-28 15:01:16 -05003575 DRM_ERROR("Failure to bind: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07003576 return ret;
3577 }
Chris Wilson22c344e2009-02-11 14:26:45 +00003578 }
3579 /*
3580 * Pre-965 chips need a fence register set up in order to
3581 * properly handle tiled surfaces.
3582 */
3583 if (!IS_I965G(dev) &&
3584 obj_priv->fence_reg == I915_FENCE_REG_NONE &&
3585 obj_priv->tiling_mode != I915_TILING_NONE) {
Chris Wilson8c4b8c32009-06-17 22:08:52 +01003586 ret = i915_gem_object_get_fence_reg(obj);
Chris Wilson22c344e2009-02-11 14:26:45 +00003587 if (ret != 0) {
3588 if (ret != -EBUSY && ret != -ERESTARTSYS)
3589 DRM_ERROR("Failure to install fence: %d\n",
3590 ret);
3591 return ret;
3592 }
Eric Anholt673a3942008-07-30 12:06:12 -07003593 }
3594 obj_priv->pin_count++;
3595
3596 /* If the object is not active and not pending a flush,
3597 * remove it from the inactive list
3598 */
3599 if (obj_priv->pin_count == 1) {
3600 atomic_inc(&dev->pin_count);
3601 atomic_add(obj->size, &dev->pin_memory);
3602 if (!obj_priv->active &&
Chris Wilson21d509e2009-06-06 09:46:02 +01003603 (obj->write_domain & I915_GEM_GPU_DOMAINS) == 0 &&
Eric Anholt673a3942008-07-30 12:06:12 -07003604 !list_empty(&obj_priv->list))
3605 list_del_init(&obj_priv->list);
3606 }
3607 i915_verify_inactive(dev, __FILE__, __LINE__);
3608
3609 return 0;
3610}
3611
3612void
3613i915_gem_object_unpin(struct drm_gem_object *obj)
3614{
3615 struct drm_device *dev = obj->dev;
3616 drm_i915_private_t *dev_priv = dev->dev_private;
3617 struct drm_i915_gem_object *obj_priv = obj->driver_private;
3618
3619 i915_verify_inactive(dev, __FILE__, __LINE__);
3620 obj_priv->pin_count--;
3621 BUG_ON(obj_priv->pin_count < 0);
3622 BUG_ON(obj_priv->gtt_space == NULL);
3623
3624 /* If the object is no longer pinned, and is
3625 * neither active nor being flushed, then stick it on
3626 * the inactive list
3627 */
3628 if (obj_priv->pin_count == 0) {
3629 if (!obj_priv->active &&
Chris Wilson21d509e2009-06-06 09:46:02 +01003630 (obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
Eric Anholt673a3942008-07-30 12:06:12 -07003631 list_move_tail(&obj_priv->list,
3632 &dev_priv->mm.inactive_list);
3633 atomic_dec(&dev->pin_count);
3634 atomic_sub(obj->size, &dev->pin_memory);
3635 }
3636 i915_verify_inactive(dev, __FILE__, __LINE__);
3637}
3638
3639int
3640i915_gem_pin_ioctl(struct drm_device *dev, void *data,
3641 struct drm_file *file_priv)
3642{
3643 struct drm_i915_gem_pin *args = data;
3644 struct drm_gem_object *obj;
3645 struct drm_i915_gem_object *obj_priv;
3646 int ret;
3647
3648 mutex_lock(&dev->struct_mutex);
3649
3650 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
3651 if (obj == NULL) {
3652 DRM_ERROR("Bad handle in i915_gem_pin_ioctl(): %d\n",
3653 args->handle);
3654 mutex_unlock(&dev->struct_mutex);
3655 return -EBADF;
3656 }
3657 obj_priv = obj->driver_private;
3658
Jesse Barnes79e53942008-11-07 14:24:08 -08003659 if (obj_priv->pin_filp != NULL && obj_priv->pin_filp != file_priv) {
3660 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
3661 args->handle);
Chris Wilson96dec612009-02-08 19:08:04 +00003662 drm_gem_object_unreference(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003663 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08003664 return -EINVAL;
3665 }
3666
3667 obj_priv->user_pin_count++;
3668 obj_priv->pin_filp = file_priv;
3669 if (obj_priv->user_pin_count == 1) {
3670 ret = i915_gem_object_pin(obj, args->alignment);
3671 if (ret != 0) {
3672 drm_gem_object_unreference(obj);
3673 mutex_unlock(&dev->struct_mutex);
3674 return ret;
3675 }
Eric Anholt673a3942008-07-30 12:06:12 -07003676 }
3677
3678 /* XXX - flush the CPU caches for pinned objects
3679 * as the X server doesn't manage domains yet
3680 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003681 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003682 args->offset = obj_priv->gtt_offset;
3683 drm_gem_object_unreference(obj);
3684 mutex_unlock(&dev->struct_mutex);
3685
3686 return 0;
3687}
3688
3689int
3690i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
3691 struct drm_file *file_priv)
3692{
3693 struct drm_i915_gem_pin *args = data;
3694 struct drm_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08003695 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07003696
3697 mutex_lock(&dev->struct_mutex);
3698
3699 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
3700 if (obj == NULL) {
3701 DRM_ERROR("Bad handle in i915_gem_unpin_ioctl(): %d\n",
3702 args->handle);
3703 mutex_unlock(&dev->struct_mutex);
3704 return -EBADF;
3705 }
3706
Jesse Barnes79e53942008-11-07 14:24:08 -08003707 obj_priv = obj->driver_private;
3708 if (obj_priv->pin_filp != file_priv) {
3709 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
3710 args->handle);
3711 drm_gem_object_unreference(obj);
3712 mutex_unlock(&dev->struct_mutex);
3713 return -EINVAL;
3714 }
3715 obj_priv->user_pin_count--;
3716 if (obj_priv->user_pin_count == 0) {
3717 obj_priv->pin_filp = NULL;
3718 i915_gem_object_unpin(obj);
3719 }
Eric Anholt673a3942008-07-30 12:06:12 -07003720
3721 drm_gem_object_unreference(obj);
3722 mutex_unlock(&dev->struct_mutex);
3723 return 0;
3724}
3725
3726int
3727i915_gem_busy_ioctl(struct drm_device *dev, void *data,
3728 struct drm_file *file_priv)
3729{
3730 struct drm_i915_gem_busy *args = data;
3731 struct drm_gem_object *obj;
3732 struct drm_i915_gem_object *obj_priv;
3733
Eric Anholt673a3942008-07-30 12:06:12 -07003734 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
3735 if (obj == NULL) {
3736 DRM_ERROR("Bad handle in i915_gem_busy_ioctl(): %d\n",
3737 args->handle);
Eric Anholt673a3942008-07-30 12:06:12 -07003738 return -EBADF;
3739 }
3740
Chris Wilsonb1ce7862009-06-06 09:46:00 +01003741 mutex_lock(&dev->struct_mutex);
Eric Anholtf21289b2009-02-18 09:44:56 -08003742 /* Update the active list for the hardware's current position.
3743 * Otherwise this only updates on a delayed timer or when irqs are
3744 * actually unmasked, and our working set ends up being larger than
3745 * required.
3746 */
3747 i915_gem_retire_requests(dev);
3748
Eric Anholt673a3942008-07-30 12:06:12 -07003749 obj_priv = obj->driver_private;
Eric Anholtc4de0a52008-12-14 19:05:04 -08003750 /* Don't count being on the flushing list against the object being
3751 * done. Otherwise, a buffer left on the flushing list but not getting
3752 * flushed (because nobody's flushing that domain) won't ever return
3753 * unbusy and get reused by libdrm's bo cache. The other expected
3754 * consumer of this interface, OpenGL's occlusion queries, also specs
3755 * that the objects get unbusy "eventually" without any interference.
3756 */
3757 args->busy = obj_priv->active && obj_priv->last_rendering_seqno != 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003758
3759 drm_gem_object_unreference(obj);
3760 mutex_unlock(&dev->struct_mutex);
3761 return 0;
3762}
3763
3764int
3765i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
3766 struct drm_file *file_priv)
3767{
3768 return i915_gem_ring_throttle(dev, file_priv);
3769}
3770
3771int i915_gem_init_object(struct drm_gem_object *obj)
3772{
3773 struct drm_i915_gem_object *obj_priv;
3774
3775 obj_priv = drm_calloc(1, sizeof(*obj_priv), DRM_MEM_DRIVER);
3776 if (obj_priv == NULL)
3777 return -ENOMEM;
3778
3779 /*
3780 * We've just allocated pages from the kernel,
3781 * so they've just been written by the CPU with
3782 * zeros. They'll need to be clflushed before we
3783 * use them with the GPU.
3784 */
3785 obj->write_domain = I915_GEM_DOMAIN_CPU;
3786 obj->read_domains = I915_GEM_DOMAIN_CPU;
3787
Keith Packardba1eb1d2008-10-14 19:55:10 -07003788 obj_priv->agp_type = AGP_USER_MEMORY;
3789
Eric Anholt673a3942008-07-30 12:06:12 -07003790 obj->driver_private = obj_priv;
3791 obj_priv->obj = obj;
Jesse Barnesde151cf2008-11-12 10:03:55 -08003792 obj_priv->fence_reg = I915_FENCE_REG_NONE;
Eric Anholt673a3942008-07-30 12:06:12 -07003793 INIT_LIST_HEAD(&obj_priv->list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08003794
Eric Anholt673a3942008-07-30 12:06:12 -07003795 return 0;
3796}
3797
3798void i915_gem_free_object(struct drm_gem_object *obj)
3799{
Jesse Barnesde151cf2008-11-12 10:03:55 -08003800 struct drm_device *dev = obj->dev;
Eric Anholt673a3942008-07-30 12:06:12 -07003801 struct drm_i915_gem_object *obj_priv = obj->driver_private;
3802
3803 while (obj_priv->pin_count > 0)
3804 i915_gem_object_unpin(obj);
3805
Dave Airlie71acb5e2008-12-30 20:31:46 +10003806 if (obj_priv->phys_obj)
3807 i915_gem_detach_phys_object(dev, obj);
3808
Eric Anholt673a3942008-07-30 12:06:12 -07003809 i915_gem_object_unbind(obj);
3810
Jesse Barnesab00b3e2009-02-11 14:01:46 -08003811 i915_gem_free_mmap_offset(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08003812
Eric Anholt673a3942008-07-30 12:06:12 -07003813 drm_free(obj_priv->page_cpu_valid, 1, DRM_MEM_DRIVER);
Eric Anholt280b7132009-03-12 16:56:27 -07003814 kfree(obj_priv->bit_17);
Eric Anholt673a3942008-07-30 12:06:12 -07003815 drm_free(obj->driver_private, 1, DRM_MEM_DRIVER);
3816}
3817
Eric Anholt673a3942008-07-30 12:06:12 -07003818/** Unbinds all objects that are on the given buffer list. */
3819static int
3820i915_gem_evict_from_list(struct drm_device *dev, struct list_head *head)
3821{
3822 struct drm_gem_object *obj;
3823 struct drm_i915_gem_object *obj_priv;
3824 int ret;
3825
3826 while (!list_empty(head)) {
3827 obj_priv = list_first_entry(head,
3828 struct drm_i915_gem_object,
3829 list);
3830 obj = obj_priv->obj;
3831
3832 if (obj_priv->pin_count != 0) {
3833 DRM_ERROR("Pinned object in unbind list\n");
3834 mutex_unlock(&dev->struct_mutex);
3835 return -EINVAL;
3836 }
3837
3838 ret = i915_gem_object_unbind(obj);
3839 if (ret != 0) {
3840 DRM_ERROR("Error unbinding object in LeaveVT: %d\n",
3841 ret);
3842 mutex_unlock(&dev->struct_mutex);
3843 return ret;
3844 }
3845 }
3846
3847
3848 return 0;
3849}
3850
Jesse Barnes5669fca2009-02-17 15:13:31 -08003851int
Eric Anholt673a3942008-07-30 12:06:12 -07003852i915_gem_idle(struct drm_device *dev)
3853{
3854 drm_i915_private_t *dev_priv = dev->dev_private;
3855 uint32_t seqno, cur_seqno, last_seqno;
3856 int stuck, ret;
3857
Keith Packard6dbe2772008-10-14 21:41:13 -07003858 mutex_lock(&dev->struct_mutex);
3859
3860 if (dev_priv->mm.suspended || dev_priv->ring.ring_obj == NULL) {
3861 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07003862 return 0;
Keith Packard6dbe2772008-10-14 21:41:13 -07003863 }
Eric Anholt673a3942008-07-30 12:06:12 -07003864
3865 /* Hack! Don't let anybody do execbuf while we don't control the chip.
3866 * We need to replace this with a semaphore, or something.
3867 */
3868 dev_priv->mm.suspended = 1;
3869
Keith Packard6dbe2772008-10-14 21:41:13 -07003870 /* Cancel the retire work handler, wait for it to finish if running
3871 */
3872 mutex_unlock(&dev->struct_mutex);
3873 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
3874 mutex_lock(&dev->struct_mutex);
3875
Eric Anholt673a3942008-07-30 12:06:12 -07003876 i915_kernel_lost_context(dev);
3877
3878 /* Flush the GPU along with all non-CPU write domains
3879 */
Chris Wilson21d509e2009-06-06 09:46:02 +01003880 i915_gem_flush(dev, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
3881 seqno = i915_add_request(dev, NULL, I915_GEM_GPU_DOMAINS);
Eric Anholt673a3942008-07-30 12:06:12 -07003882
3883 if (seqno == 0) {
3884 mutex_unlock(&dev->struct_mutex);
3885 return -ENOMEM;
3886 }
3887
3888 dev_priv->mm.waiting_gem_seqno = seqno;
3889 last_seqno = 0;
3890 stuck = 0;
3891 for (;;) {
3892 cur_seqno = i915_get_gem_seqno(dev);
3893 if (i915_seqno_passed(cur_seqno, seqno))
3894 break;
3895 if (last_seqno == cur_seqno) {
3896 if (stuck++ > 100) {
3897 DRM_ERROR("hardware wedged\n");
3898 dev_priv->mm.wedged = 1;
3899 DRM_WAKEUP(&dev_priv->irq_queue);
3900 break;
3901 }
3902 }
3903 msleep(10);
3904 last_seqno = cur_seqno;
3905 }
3906 dev_priv->mm.waiting_gem_seqno = 0;
3907
3908 i915_gem_retire_requests(dev);
3909
Carl Worth5e118f42009-03-20 11:54:25 -07003910 spin_lock(&dev_priv->mm.active_list_lock);
Eric Anholt28dfe522008-11-13 15:00:55 -08003911 if (!dev_priv->mm.wedged) {
3912 /* Active and flushing should now be empty as we've
3913 * waited for a sequence higher than any pending execbuffer
3914 */
3915 WARN_ON(!list_empty(&dev_priv->mm.active_list));
3916 WARN_ON(!list_empty(&dev_priv->mm.flushing_list));
3917 /* Request should now be empty as we've also waited
3918 * for the last request in the list
3919 */
3920 WARN_ON(!list_empty(&dev_priv->mm.request_list));
3921 }
Eric Anholt673a3942008-07-30 12:06:12 -07003922
Eric Anholt28dfe522008-11-13 15:00:55 -08003923 /* Empty the active and flushing lists to inactive. If there's
3924 * anything left at this point, it means that we're wedged and
3925 * nothing good's going to happen by leaving them there. So strip
3926 * the GPU domains and just stuff them onto inactive.
Eric Anholt673a3942008-07-30 12:06:12 -07003927 */
Eric Anholt28dfe522008-11-13 15:00:55 -08003928 while (!list_empty(&dev_priv->mm.active_list)) {
3929 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07003930
Eric Anholt28dfe522008-11-13 15:00:55 -08003931 obj_priv = list_first_entry(&dev_priv->mm.active_list,
3932 struct drm_i915_gem_object,
3933 list);
3934 obj_priv->obj->write_domain &= ~I915_GEM_GPU_DOMAINS;
3935 i915_gem_object_move_to_inactive(obj_priv->obj);
3936 }
Carl Worth5e118f42009-03-20 11:54:25 -07003937 spin_unlock(&dev_priv->mm.active_list_lock);
Eric Anholt28dfe522008-11-13 15:00:55 -08003938
3939 while (!list_empty(&dev_priv->mm.flushing_list)) {
3940 struct drm_i915_gem_object *obj_priv;
3941
Eric Anholt151903d2008-12-01 10:23:21 +10003942 obj_priv = list_first_entry(&dev_priv->mm.flushing_list,
Eric Anholt28dfe522008-11-13 15:00:55 -08003943 struct drm_i915_gem_object,
3944 list);
3945 obj_priv->obj->write_domain &= ~I915_GEM_GPU_DOMAINS;
3946 i915_gem_object_move_to_inactive(obj_priv->obj);
3947 }
3948
3949
3950 /* Move all inactive buffers out of the GTT. */
Eric Anholt673a3942008-07-30 12:06:12 -07003951 ret = i915_gem_evict_from_list(dev, &dev_priv->mm.inactive_list);
Eric Anholt28dfe522008-11-13 15:00:55 -08003952 WARN_ON(!list_empty(&dev_priv->mm.inactive_list));
Keith Packard6dbe2772008-10-14 21:41:13 -07003953 if (ret) {
3954 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07003955 return ret;
Keith Packard6dbe2772008-10-14 21:41:13 -07003956 }
Eric Anholt673a3942008-07-30 12:06:12 -07003957
Keith Packard6dbe2772008-10-14 21:41:13 -07003958 i915_gem_cleanup_ringbuffer(dev);
3959 mutex_unlock(&dev->struct_mutex);
3960
Eric Anholt673a3942008-07-30 12:06:12 -07003961 return 0;
3962}
3963
3964static int
3965i915_gem_init_hws(struct drm_device *dev)
3966{
3967 drm_i915_private_t *dev_priv = dev->dev_private;
3968 struct drm_gem_object *obj;
3969 struct drm_i915_gem_object *obj_priv;
3970 int ret;
3971
3972 /* If we need a physical address for the status page, it's already
3973 * initialized at driver load time.
3974 */
3975 if (!I915_NEED_GFX_HWS(dev))
3976 return 0;
3977
3978 obj = drm_gem_object_alloc(dev, 4096);
3979 if (obj == NULL) {
3980 DRM_ERROR("Failed to allocate status page\n");
3981 return -ENOMEM;
3982 }
3983 obj_priv = obj->driver_private;
Keith Packardba1eb1d2008-10-14 19:55:10 -07003984 obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
Eric Anholt673a3942008-07-30 12:06:12 -07003985
3986 ret = i915_gem_object_pin(obj, 4096);
3987 if (ret != 0) {
3988 drm_gem_object_unreference(obj);
3989 return ret;
3990 }
3991
3992 dev_priv->status_gfx_addr = obj_priv->gtt_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003993
Eric Anholt856fa192009-03-19 14:10:50 -07003994 dev_priv->hw_status_page = kmap(obj_priv->pages[0]);
Keith Packardba1eb1d2008-10-14 19:55:10 -07003995 if (dev_priv->hw_status_page == NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -07003996 DRM_ERROR("Failed to map status page.\n");
3997 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
Chris Wilson3eb2ee72009-02-11 14:26:34 +00003998 i915_gem_object_unpin(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003999 drm_gem_object_unreference(obj);
4000 return -EINVAL;
4001 }
4002 dev_priv->hws_obj = obj;
Eric Anholt673a3942008-07-30 12:06:12 -07004003 memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
4004 I915_WRITE(HWS_PGA, dev_priv->status_gfx_addr);
Keith Packardba1eb1d2008-10-14 19:55:10 -07004005 I915_READ(HWS_PGA); /* posting read */
Eric Anholt673a3942008-07-30 12:06:12 -07004006 DRM_DEBUG("hws offset: 0x%08x\n", dev_priv->status_gfx_addr);
4007
4008 return 0;
4009}
4010
Chris Wilson85a7bb92009-02-11 14:52:44 +00004011static void
4012i915_gem_cleanup_hws(struct drm_device *dev)
4013{
4014 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonbab2d1f2009-02-20 17:52:20 +00004015 struct drm_gem_object *obj;
4016 struct drm_i915_gem_object *obj_priv;
Chris Wilson85a7bb92009-02-11 14:52:44 +00004017
4018 if (dev_priv->hws_obj == NULL)
4019 return;
4020
Chris Wilsonbab2d1f2009-02-20 17:52:20 +00004021 obj = dev_priv->hws_obj;
4022 obj_priv = obj->driver_private;
4023
Eric Anholt856fa192009-03-19 14:10:50 -07004024 kunmap(obj_priv->pages[0]);
Chris Wilson85a7bb92009-02-11 14:52:44 +00004025 i915_gem_object_unpin(obj);
4026 drm_gem_object_unreference(obj);
4027 dev_priv->hws_obj = NULL;
Chris Wilsonbab2d1f2009-02-20 17:52:20 +00004028
Chris Wilson85a7bb92009-02-11 14:52:44 +00004029 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
4030 dev_priv->hw_status_page = NULL;
4031
4032 /* Write high address into HWS_PGA when disabling. */
4033 I915_WRITE(HWS_PGA, 0x1ffff000);
4034}
4035
Jesse Barnes79e53942008-11-07 14:24:08 -08004036int
Eric Anholt673a3942008-07-30 12:06:12 -07004037i915_gem_init_ringbuffer(struct drm_device *dev)
4038{
4039 drm_i915_private_t *dev_priv = dev->dev_private;
4040 struct drm_gem_object *obj;
4041 struct drm_i915_gem_object *obj_priv;
Jesse Barnes79e53942008-11-07 14:24:08 -08004042 drm_i915_ring_buffer_t *ring = &dev_priv->ring;
Eric Anholt673a3942008-07-30 12:06:12 -07004043 int ret;
Keith Packard50aa253d2008-10-14 17:20:35 -07004044 u32 head;
Eric Anholt673a3942008-07-30 12:06:12 -07004045
4046 ret = i915_gem_init_hws(dev);
4047 if (ret != 0)
4048 return ret;
4049
4050 obj = drm_gem_object_alloc(dev, 128 * 1024);
4051 if (obj == NULL) {
4052 DRM_ERROR("Failed to allocate ringbuffer\n");
Chris Wilson85a7bb92009-02-11 14:52:44 +00004053 i915_gem_cleanup_hws(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004054 return -ENOMEM;
4055 }
4056 obj_priv = obj->driver_private;
4057
4058 ret = i915_gem_object_pin(obj, 4096);
4059 if (ret != 0) {
4060 drm_gem_object_unreference(obj);
Chris Wilson85a7bb92009-02-11 14:52:44 +00004061 i915_gem_cleanup_hws(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004062 return ret;
4063 }
4064
4065 /* Set up the kernel mapping for the ring. */
Jesse Barnes79e53942008-11-07 14:24:08 -08004066 ring->Size = obj->size;
4067 ring->tail_mask = obj->size - 1;
Eric Anholt673a3942008-07-30 12:06:12 -07004068
Jesse Barnes79e53942008-11-07 14:24:08 -08004069 ring->map.offset = dev->agp->base + obj_priv->gtt_offset;
4070 ring->map.size = obj->size;
4071 ring->map.type = 0;
4072 ring->map.flags = 0;
4073 ring->map.mtrr = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07004074
Jesse Barnes79e53942008-11-07 14:24:08 -08004075 drm_core_ioremap_wc(&ring->map, dev);
4076 if (ring->map.handle == NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -07004077 DRM_ERROR("Failed to map ringbuffer.\n");
4078 memset(&dev_priv->ring, 0, sizeof(dev_priv->ring));
Chris Wilson47ed1852009-02-11 14:26:33 +00004079 i915_gem_object_unpin(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004080 drm_gem_object_unreference(obj);
Chris Wilson85a7bb92009-02-11 14:52:44 +00004081 i915_gem_cleanup_hws(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004082 return -EINVAL;
4083 }
Jesse Barnes79e53942008-11-07 14:24:08 -08004084 ring->ring_obj = obj;
4085 ring->virtual_start = ring->map.handle;
Eric Anholt673a3942008-07-30 12:06:12 -07004086
4087 /* Stop the ring if it's running. */
4088 I915_WRITE(PRB0_CTL, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004089 I915_WRITE(PRB0_TAIL, 0);
Keith Packard50aa253d2008-10-14 17:20:35 -07004090 I915_WRITE(PRB0_HEAD, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004091
4092 /* Initialize the ring. */
4093 I915_WRITE(PRB0_START, obj_priv->gtt_offset);
Keith Packard50aa253d2008-10-14 17:20:35 -07004094 head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
4095
4096 /* G45 ring initialization fails to reset head to zero */
4097 if (head != 0) {
4098 DRM_ERROR("Ring head not reset to zero "
4099 "ctl %08x head %08x tail %08x start %08x\n",
4100 I915_READ(PRB0_CTL),
4101 I915_READ(PRB0_HEAD),
4102 I915_READ(PRB0_TAIL),
4103 I915_READ(PRB0_START));
4104 I915_WRITE(PRB0_HEAD, 0);
4105
4106 DRM_ERROR("Ring head forced to zero "
4107 "ctl %08x head %08x tail %08x start %08x\n",
4108 I915_READ(PRB0_CTL),
4109 I915_READ(PRB0_HEAD),
4110 I915_READ(PRB0_TAIL),
4111 I915_READ(PRB0_START));
4112 }
4113
Eric Anholt673a3942008-07-30 12:06:12 -07004114 I915_WRITE(PRB0_CTL,
4115 ((obj->size - 4096) & RING_NR_PAGES) |
4116 RING_NO_REPORT |
4117 RING_VALID);
4118
Keith Packard50aa253d2008-10-14 17:20:35 -07004119 head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
4120
4121 /* If the head is still not zero, the ring is dead */
4122 if (head != 0) {
4123 DRM_ERROR("Ring initialization failed "
4124 "ctl %08x head %08x tail %08x start %08x\n",
4125 I915_READ(PRB0_CTL),
4126 I915_READ(PRB0_HEAD),
4127 I915_READ(PRB0_TAIL),
4128 I915_READ(PRB0_START));
4129 return -EIO;
4130 }
4131
Eric Anholt673a3942008-07-30 12:06:12 -07004132 /* Update our cache of the ring state */
Jesse Barnes79e53942008-11-07 14:24:08 -08004133 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4134 i915_kernel_lost_context(dev);
4135 else {
4136 ring->head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
4137 ring->tail = I915_READ(PRB0_TAIL) & TAIL_ADDR;
4138 ring->space = ring->head - (ring->tail + 8);
4139 if (ring->space < 0)
4140 ring->space += ring->Size;
4141 }
Eric Anholt673a3942008-07-30 12:06:12 -07004142
4143 return 0;
4144}
4145
Jesse Barnes79e53942008-11-07 14:24:08 -08004146void
Eric Anholt673a3942008-07-30 12:06:12 -07004147i915_gem_cleanup_ringbuffer(struct drm_device *dev)
4148{
4149 drm_i915_private_t *dev_priv = dev->dev_private;
4150
4151 if (dev_priv->ring.ring_obj == NULL)
4152 return;
4153
4154 drm_core_ioremapfree(&dev_priv->ring.map, dev);
4155
4156 i915_gem_object_unpin(dev_priv->ring.ring_obj);
4157 drm_gem_object_unreference(dev_priv->ring.ring_obj);
4158 dev_priv->ring.ring_obj = NULL;
4159 memset(&dev_priv->ring, 0, sizeof(dev_priv->ring));
4160
Chris Wilson85a7bb92009-02-11 14:52:44 +00004161 i915_gem_cleanup_hws(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004162}
4163
4164int
4165i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
4166 struct drm_file *file_priv)
4167{
4168 drm_i915_private_t *dev_priv = dev->dev_private;
4169 int ret;
4170
Jesse Barnes79e53942008-11-07 14:24:08 -08004171 if (drm_core_check_feature(dev, DRIVER_MODESET))
4172 return 0;
4173
Eric Anholt673a3942008-07-30 12:06:12 -07004174 if (dev_priv->mm.wedged) {
4175 DRM_ERROR("Reenabling wedged hardware, good luck\n");
4176 dev_priv->mm.wedged = 0;
4177 }
4178
Eric Anholt673a3942008-07-30 12:06:12 -07004179 mutex_lock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004180 dev_priv->mm.suspended = 0;
4181
4182 ret = i915_gem_init_ringbuffer(dev);
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004183 if (ret != 0) {
4184 mutex_unlock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004185 return ret;
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004186 }
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004187
Carl Worth5e118f42009-03-20 11:54:25 -07004188 spin_lock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07004189 BUG_ON(!list_empty(&dev_priv->mm.active_list));
Carl Worth5e118f42009-03-20 11:54:25 -07004190 spin_unlock(&dev_priv->mm.active_list_lock);
4191
Eric Anholt673a3942008-07-30 12:06:12 -07004192 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
4193 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
4194 BUG_ON(!list_empty(&dev_priv->mm.request_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004195 mutex_unlock(&dev->struct_mutex);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004196
4197 drm_irq_install(dev);
4198
Eric Anholt673a3942008-07-30 12:06:12 -07004199 return 0;
4200}
4201
4202int
4203i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
4204 struct drm_file *file_priv)
4205{
4206 int ret;
4207
Jesse Barnes79e53942008-11-07 14:24:08 -08004208 if (drm_core_check_feature(dev, DRIVER_MODESET))
4209 return 0;
4210
Eric Anholt673a3942008-07-30 12:06:12 -07004211 ret = i915_gem_idle(dev);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004212 drm_irq_uninstall(dev);
4213
Keith Packard6dbe2772008-10-14 21:41:13 -07004214 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004215}
4216
4217void
4218i915_gem_lastclose(struct drm_device *dev)
4219{
4220 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004221
Eric Anholte806b492009-01-22 09:56:58 -08004222 if (drm_core_check_feature(dev, DRIVER_MODESET))
4223 return;
4224
Keith Packard6dbe2772008-10-14 21:41:13 -07004225 ret = i915_gem_idle(dev);
4226 if (ret)
4227 DRM_ERROR("failed to idle hardware: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07004228}
4229
4230void
4231i915_gem_load(struct drm_device *dev)
4232{
4233 drm_i915_private_t *dev_priv = dev->dev_private;
4234
Carl Worth5e118f42009-03-20 11:54:25 -07004235 spin_lock_init(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07004236 INIT_LIST_HEAD(&dev_priv->mm.active_list);
4237 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
4238 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
4239 INIT_LIST_HEAD(&dev_priv->mm.request_list);
4240 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
4241 i915_gem_retire_work_handler);
Eric Anholt673a3942008-07-30 12:06:12 -07004242 dev_priv->mm.next_gem_seqno = 1;
4243
Jesse Barnesde151cf2008-11-12 10:03:55 -08004244 /* Old X drivers will take 0-2 for front, back, depth buffers */
4245 dev_priv->fence_reg_start = 3;
4246
Jesse Barnes0f973f22009-01-26 17:10:45 -08004247 if (IS_I965G(dev) || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08004248 dev_priv->num_fence_regs = 16;
4249 else
4250 dev_priv->num_fence_regs = 8;
4251
Eric Anholt673a3942008-07-30 12:06:12 -07004252 i915_gem_detect_bit_6_swizzle(dev);
4253}
Dave Airlie71acb5e2008-12-30 20:31:46 +10004254
4255/*
4256 * Create a physically contiguous memory object for this object
4257 * e.g. for cursor + overlay regs
4258 */
4259int i915_gem_init_phys_object(struct drm_device *dev,
4260 int id, int size)
4261{
4262 drm_i915_private_t *dev_priv = dev->dev_private;
4263 struct drm_i915_gem_phys_object *phys_obj;
4264 int ret;
4265
4266 if (dev_priv->mm.phys_objs[id - 1] || !size)
4267 return 0;
4268
4269 phys_obj = drm_calloc(1, sizeof(struct drm_i915_gem_phys_object), DRM_MEM_DRIVER);
4270 if (!phys_obj)
4271 return -ENOMEM;
4272
4273 phys_obj->id = id;
4274
4275 phys_obj->handle = drm_pci_alloc(dev, size, 0, 0xffffffff);
4276 if (!phys_obj->handle) {
4277 ret = -ENOMEM;
4278 goto kfree_obj;
4279 }
4280#ifdef CONFIG_X86
4281 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4282#endif
4283
4284 dev_priv->mm.phys_objs[id - 1] = phys_obj;
4285
4286 return 0;
4287kfree_obj:
4288 drm_free(phys_obj, sizeof(struct drm_i915_gem_phys_object), DRM_MEM_DRIVER);
4289 return ret;
4290}
4291
4292void i915_gem_free_phys_object(struct drm_device *dev, int id)
4293{
4294 drm_i915_private_t *dev_priv = dev->dev_private;
4295 struct drm_i915_gem_phys_object *phys_obj;
4296
4297 if (!dev_priv->mm.phys_objs[id - 1])
4298 return;
4299
4300 phys_obj = dev_priv->mm.phys_objs[id - 1];
4301 if (phys_obj->cur_obj) {
4302 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
4303 }
4304
4305#ifdef CONFIG_X86
4306 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4307#endif
4308 drm_pci_free(dev, phys_obj->handle);
4309 kfree(phys_obj);
4310 dev_priv->mm.phys_objs[id - 1] = NULL;
4311}
4312
4313void i915_gem_free_all_phys_object(struct drm_device *dev)
4314{
4315 int i;
4316
Dave Airlie260883c2009-01-22 17:58:49 +10004317 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004318 i915_gem_free_phys_object(dev, i);
4319}
4320
4321void i915_gem_detach_phys_object(struct drm_device *dev,
4322 struct drm_gem_object *obj)
4323{
4324 struct drm_i915_gem_object *obj_priv;
4325 int i;
4326 int ret;
4327 int page_count;
4328
4329 obj_priv = obj->driver_private;
4330 if (!obj_priv->phys_obj)
4331 return;
4332
Eric Anholt856fa192009-03-19 14:10:50 -07004333 ret = i915_gem_object_get_pages(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004334 if (ret)
4335 goto out;
4336
4337 page_count = obj->size / PAGE_SIZE;
4338
4339 for (i = 0; i < page_count; i++) {
Eric Anholt856fa192009-03-19 14:10:50 -07004340 char *dst = kmap_atomic(obj_priv->pages[i], KM_USER0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004341 char *src = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4342
4343 memcpy(dst, src, PAGE_SIZE);
4344 kunmap_atomic(dst, KM_USER0);
4345 }
Eric Anholt856fa192009-03-19 14:10:50 -07004346 drm_clflush_pages(obj_priv->pages, page_count);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004347 drm_agp_chipset_flush(dev);
Chris Wilsond78b47b2009-06-17 21:52:49 +01004348
4349 i915_gem_object_put_pages(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004350out:
4351 obj_priv->phys_obj->cur_obj = NULL;
4352 obj_priv->phys_obj = NULL;
4353}
4354
4355int
4356i915_gem_attach_phys_object(struct drm_device *dev,
4357 struct drm_gem_object *obj, int id)
4358{
4359 drm_i915_private_t *dev_priv = dev->dev_private;
4360 struct drm_i915_gem_object *obj_priv;
4361 int ret = 0;
4362 int page_count;
4363 int i;
4364
4365 if (id > I915_MAX_PHYS_OBJECT)
4366 return -EINVAL;
4367
4368 obj_priv = obj->driver_private;
4369
4370 if (obj_priv->phys_obj) {
4371 if (obj_priv->phys_obj->id == id)
4372 return 0;
4373 i915_gem_detach_phys_object(dev, obj);
4374 }
4375
4376
4377 /* create a new object */
4378 if (!dev_priv->mm.phys_objs[id - 1]) {
4379 ret = i915_gem_init_phys_object(dev, id,
4380 obj->size);
4381 if (ret) {
Linus Torvaldsaeb565d2009-01-26 10:01:53 -08004382 DRM_ERROR("failed to init phys object %d size: %zu\n", id, obj->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004383 goto out;
4384 }
4385 }
4386
4387 /* bind to the object */
4388 obj_priv->phys_obj = dev_priv->mm.phys_objs[id - 1];
4389 obj_priv->phys_obj->cur_obj = obj;
4390
Eric Anholt856fa192009-03-19 14:10:50 -07004391 ret = i915_gem_object_get_pages(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004392 if (ret) {
4393 DRM_ERROR("failed to get page list\n");
4394 goto out;
4395 }
4396
4397 page_count = obj->size / PAGE_SIZE;
4398
4399 for (i = 0; i < page_count; i++) {
Eric Anholt856fa192009-03-19 14:10:50 -07004400 char *src = kmap_atomic(obj_priv->pages[i], KM_USER0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004401 char *dst = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4402
4403 memcpy(dst, src, PAGE_SIZE);
4404 kunmap_atomic(src, KM_USER0);
4405 }
4406
Chris Wilsond78b47b2009-06-17 21:52:49 +01004407 i915_gem_object_put_pages(obj);
4408
Dave Airlie71acb5e2008-12-30 20:31:46 +10004409 return 0;
4410out:
4411 return ret;
4412}
4413
4414static int
4415i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
4416 struct drm_i915_gem_pwrite *args,
4417 struct drm_file *file_priv)
4418{
4419 struct drm_i915_gem_object *obj_priv = obj->driver_private;
4420 void *obj_addr;
4421 int ret;
4422 char __user *user_data;
4423
4424 user_data = (char __user *) (uintptr_t) args->data_ptr;
4425 obj_addr = obj_priv->phys_obj->handle->vaddr + args->offset;
4426
Dave Airliee08fb4f2009-02-25 14:52:30 +10004427 DRM_DEBUG("obj_addr %p, %lld\n", obj_addr, args->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004428 ret = copy_from_user(obj_addr, user_data, args->size);
4429 if (ret)
4430 return -EFAULT;
4431
4432 drm_agp_chipset_flush(dev);
4433 return 0;
4434}
Eric Anholtb9624422009-06-03 07:27:35 +00004435
4436void i915_gem_release(struct drm_device * dev, struct drm_file *file_priv)
4437{
4438 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
4439
4440 /* Clean up our request list when the client is going away, so that
4441 * later retire_requests won't dereference our soon-to-be-gone
4442 * file_priv.
4443 */
4444 mutex_lock(&dev->struct_mutex);
4445 while (!list_empty(&i915_file_priv->mm.request_list))
4446 list_del_init(i915_file_priv->mm.request_list.next);
4447 mutex_unlock(&dev->struct_mutex);
4448}