blob: 4f345414fe7cb02f749a70ddd53070de22ff5f69 [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);
2165
2166}
2167
2168/**
2169 * i915_gem_object_get_fence_reg - set up a fence reg for an object
2170 * @obj: object to map through a fence reg
2171 *
2172 * When mapping objects through the GTT, userspace wants to be able to write
2173 * to them without having to worry about swizzling if the object is tiled.
2174 *
2175 * This function walks the fence regs looking for a free one for @obj,
2176 * stealing one if it can't find any.
2177 *
2178 * It then sets up the reg based on the object's properties: address, pitch
2179 * and tiling format.
2180 */
Chris Wilson8c4b8c32009-06-17 22:08:52 +01002181int
2182i915_gem_object_get_fence_reg(struct drm_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002183{
2184 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002185 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002186 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2187 struct drm_i915_fence_reg *reg = NULL;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002188 struct drm_i915_gem_object *old_obj_priv = NULL;
2189 int i, ret, avail;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002190
2191 switch (obj_priv->tiling_mode) {
2192 case I915_TILING_NONE:
2193 WARN(1, "allocating a fence for non-tiled object?\n");
2194 break;
2195 case I915_TILING_X:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002196 if (!obj_priv->stride)
2197 return -EINVAL;
2198 WARN((obj_priv->stride & (512 - 1)),
2199 "object 0x%08x is X tiled but has non-512B pitch\n",
2200 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002201 break;
2202 case I915_TILING_Y:
Jesse Barnes0f973f22009-01-26 17:10:45 -08002203 if (!obj_priv->stride)
2204 return -EINVAL;
2205 WARN((obj_priv->stride & (128 - 1)),
2206 "object 0x%08x is Y tiled but has non-128B pitch\n",
2207 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002208 break;
2209 }
2210
2211 /* First try to find a free reg */
Chris Wilson9b2412f2009-02-11 14:26:44 +00002212try_again:
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002213 avail = 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002214 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2215 reg = &dev_priv->fence_regs[i];
2216 if (!reg->obj)
2217 break;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002218
2219 old_obj_priv = reg->obj->driver_private;
2220 if (!old_obj_priv->pin_count)
2221 avail++;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002222 }
2223
2224 /* None available, try to steal one or wait for a user to finish */
2225 if (i == dev_priv->num_fence_regs) {
Chris Wilsond7619c42009-02-11 14:26:47 +00002226 uint32_t seqno = dev_priv->mm.next_gem_seqno;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002227 loff_t offset;
2228
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002229 if (avail == 0)
Chris Wilson2939e1f2009-06-06 09:46:03 +01002230 return -ENOSPC;
Chris Wilsonfc7170b2009-02-11 14:26:46 +00002231
Jesse Barnesde151cf2008-11-12 10:03:55 -08002232 for (i = dev_priv->fence_reg_start;
2233 i < dev_priv->num_fence_regs; i++) {
Chris Wilsond7619c42009-02-11 14:26:47 +00002234 uint32_t this_seqno;
2235
Jesse Barnesde151cf2008-11-12 10:03:55 -08002236 reg = &dev_priv->fence_regs[i];
2237 old_obj_priv = reg->obj->driver_private;
Chris Wilsond7619c42009-02-11 14:26:47 +00002238
2239 if (old_obj_priv->pin_count)
2240 continue;
2241
2242 /* i915 uses fences for GPU access to tiled buffers */
2243 if (IS_I965G(dev) || !old_obj_priv->active)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002244 break;
Chris Wilsond7619c42009-02-11 14:26:47 +00002245
2246 /* find the seqno of the first available fence */
2247 this_seqno = old_obj_priv->last_rendering_seqno;
2248 if (this_seqno != 0 &&
2249 reg->obj->write_domain == 0 &&
2250 i915_seqno_passed(seqno, this_seqno))
2251 seqno = this_seqno;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002252 }
2253
2254 /*
2255 * Now things get ugly... we have to wait for one of the
2256 * objects to finish before trying again.
2257 */
2258 if (i == dev_priv->num_fence_regs) {
Chris Wilsond7619c42009-02-11 14:26:47 +00002259 if (seqno == dev_priv->mm.next_gem_seqno) {
2260 i915_gem_flush(dev,
2261 I915_GEM_GPU_DOMAINS,
2262 I915_GEM_GPU_DOMAINS);
Eric Anholtb9624422009-06-03 07:27:35 +00002263 seqno = i915_add_request(dev, NULL,
Chris Wilsond7619c42009-02-11 14:26:47 +00002264 I915_GEM_GPU_DOMAINS);
2265 if (seqno == 0)
2266 return -ENOMEM;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002267 }
Chris Wilsond7619c42009-02-11 14:26:47 +00002268
2269 ret = i915_wait_request(dev, seqno);
2270 if (ret)
2271 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002272 goto try_again;
2273 }
2274
2275 /*
2276 * Zap this virtual mapping so we can set up a fence again
2277 * for this object next time we need it.
2278 */
2279 offset = ((loff_t) reg->obj->map_list.hash.key) << PAGE_SHIFT;
Jesse Barnes79e53942008-11-07 14:24:08 -08002280 if (dev->dev_mapping)
2281 unmap_mapping_range(dev->dev_mapping, offset,
2282 reg->obj->size, 1);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002283 old_obj_priv->fence_reg = I915_FENCE_REG_NONE;
2284 }
2285
2286 obj_priv->fence_reg = i;
2287 reg->obj = obj;
2288
2289 if (IS_I965G(dev))
2290 i965_write_fence_reg(reg);
2291 else if (IS_I9XX(dev))
2292 i915_write_fence_reg(reg);
2293 else
2294 i830_write_fence_reg(reg);
Eric Anholtd9ddcb92009-01-27 10:33:49 -08002295
2296 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002297}
2298
2299/**
2300 * i915_gem_clear_fence_reg - clear out fence register info
2301 * @obj: object to clear
2302 *
2303 * Zeroes out the fence register itself and clears out the associated
2304 * data structures in dev_priv and obj_priv.
2305 */
2306static void
2307i915_gem_clear_fence_reg(struct drm_gem_object *obj)
2308{
2309 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002310 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002311 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2312
2313 if (IS_I965G(dev))
2314 I915_WRITE64(FENCE_REG_965_0 + (obj_priv->fence_reg * 8), 0);
Eric Anholtdc529a42009-03-10 22:34:49 -07002315 else {
2316 uint32_t fence_reg;
2317
2318 if (obj_priv->fence_reg < 8)
2319 fence_reg = FENCE_REG_830_0 + obj_priv->fence_reg * 4;
2320 else
2321 fence_reg = FENCE_REG_945_8 + (obj_priv->fence_reg -
2322 8) * 4;
2323
2324 I915_WRITE(fence_reg, 0);
2325 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002326
2327 dev_priv->fence_regs[obj_priv->fence_reg].obj = NULL;
2328 obj_priv->fence_reg = I915_FENCE_REG_NONE;
2329}
2330
Eric Anholt673a3942008-07-30 12:06:12 -07002331/**
2332 * Finds free space in the GTT aperture and binds the object there.
2333 */
2334static int
2335i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment)
2336{
2337 struct drm_device *dev = obj->dev;
2338 drm_i915_private_t *dev_priv = dev->dev_private;
2339 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2340 struct drm_mm_node *free_space;
2341 int page_count, ret;
2342
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08002343 if (dev_priv->mm.suspended)
2344 return -EBUSY;
Eric Anholt673a3942008-07-30 12:06:12 -07002345 if (alignment == 0)
Jesse Barnes0f973f22009-01-26 17:10:45 -08002346 alignment = i915_gem_get_gtt_alignment(obj);
Daniel Vetter8d7773a2009-03-29 14:09:41 +02002347 if (alignment & (i915_gem_get_gtt_alignment(obj) - 1)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002348 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2349 return -EINVAL;
2350 }
2351
2352 search_free:
2353 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
2354 obj->size, alignment, 0);
2355 if (free_space != NULL) {
2356 obj_priv->gtt_space = drm_mm_get_block(free_space, obj->size,
2357 alignment);
2358 if (obj_priv->gtt_space != NULL) {
2359 obj_priv->gtt_space->private = obj;
2360 obj_priv->gtt_offset = obj_priv->gtt_space->start;
2361 }
2362 }
2363 if (obj_priv->gtt_space == NULL) {
Carl Worth5e118f42009-03-20 11:54:25 -07002364 bool lists_empty;
2365
Eric Anholt673a3942008-07-30 12:06:12 -07002366 /* If the gtt is empty and we're still having trouble
2367 * fitting our object in, we're out of memory.
2368 */
2369#if WATCH_LRU
2370 DRM_INFO("%s: GTT full, evicting something\n", __func__);
2371#endif
Carl Worth5e118f42009-03-20 11:54:25 -07002372 spin_lock(&dev_priv->mm.active_list_lock);
2373 lists_empty = (list_empty(&dev_priv->mm.inactive_list) &&
2374 list_empty(&dev_priv->mm.flushing_list) &&
2375 list_empty(&dev_priv->mm.active_list));
2376 spin_unlock(&dev_priv->mm.active_list_lock);
2377 if (lists_empty) {
Eric Anholt673a3942008-07-30 12:06:12 -07002378 DRM_ERROR("GTT full, but LRU list empty\n");
Chris Wilson2939e1f2009-06-06 09:46:03 +01002379 return -ENOSPC;
Eric Anholt673a3942008-07-30 12:06:12 -07002380 }
2381
2382 ret = i915_gem_evict_something(dev);
2383 if (ret != 0) {
Keith Packardac94a962008-11-20 23:30:27 -08002384 if (ret != -ERESTARTSYS)
2385 DRM_ERROR("Failed to evict a buffer %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07002386 return ret;
2387 }
2388 goto search_free;
2389 }
2390
2391#if WATCH_BUF
2392 DRM_INFO("Binding object of size %d at 0x%08x\n",
2393 obj->size, obj_priv->gtt_offset);
2394#endif
Eric Anholt856fa192009-03-19 14:10:50 -07002395 ret = i915_gem_object_get_pages(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002396 if (ret) {
2397 drm_mm_put_block(obj_priv->gtt_space);
2398 obj_priv->gtt_space = NULL;
2399 return ret;
2400 }
2401
2402 page_count = obj->size / PAGE_SIZE;
2403 /* Create an AGP memory structure pointing at our pages, and bind it
2404 * into the GTT.
2405 */
2406 obj_priv->agp_mem = drm_agp_bind_pages(dev,
Eric Anholt856fa192009-03-19 14:10:50 -07002407 obj_priv->pages,
Eric Anholt673a3942008-07-30 12:06:12 -07002408 page_count,
Keith Packardba1eb1d2008-10-14 19:55:10 -07002409 obj_priv->gtt_offset,
2410 obj_priv->agp_type);
Eric Anholt673a3942008-07-30 12:06:12 -07002411 if (obj_priv->agp_mem == NULL) {
Eric Anholt856fa192009-03-19 14:10:50 -07002412 i915_gem_object_put_pages(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002413 drm_mm_put_block(obj_priv->gtt_space);
2414 obj_priv->gtt_space = NULL;
2415 return -ENOMEM;
2416 }
2417 atomic_inc(&dev->gtt_count);
2418 atomic_add(obj->size, &dev->gtt_memory);
2419
2420 /* Assert that the object is not currently in any GPU domain. As it
2421 * wasn't in the GTT, there shouldn't be any way it could have been in
2422 * a GPU cache
2423 */
Chris Wilson21d509e2009-06-06 09:46:02 +01002424 BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS);
2425 BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS);
Eric Anholt673a3942008-07-30 12:06:12 -07002426
2427 return 0;
2428}
2429
2430void
2431i915_gem_clflush_object(struct drm_gem_object *obj)
2432{
2433 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2434
2435 /* If we don't have a page list set up, then we're not pinned
2436 * to GPU, and we can ignore the cache flush because it'll happen
2437 * again at bind time.
2438 */
Eric Anholt856fa192009-03-19 14:10:50 -07002439 if (obj_priv->pages == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002440 return;
2441
Eric Anholtcfa16a02009-05-26 18:46:16 -07002442 /* XXX: The 865 in particular appears to be weird in how it handles
2443 * cache flushing. We haven't figured it out, but the
2444 * clflush+agp_chipset_flush doesn't appear to successfully get the
2445 * data visible to the PGU, while wbinvd + agp_chipset_flush does.
2446 */
2447 if (IS_I865G(obj->dev)) {
2448 wbinvd();
2449 return;
2450 }
2451
Eric Anholt856fa192009-03-19 14:10:50 -07002452 drm_clflush_pages(obj_priv->pages, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002453}
2454
Eric Anholte47c68e2008-11-14 13:35:19 -08002455/** Flushes any GPU write domain for the object if it's dirty. */
2456static void
2457i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj)
2458{
2459 struct drm_device *dev = obj->dev;
2460 uint32_t seqno;
2461
2462 if ((obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
2463 return;
2464
2465 /* Queue the GPU write cache flushing we need. */
2466 i915_gem_flush(dev, 0, obj->write_domain);
Eric Anholtb9624422009-06-03 07:27:35 +00002467 seqno = i915_add_request(dev, NULL, obj->write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08002468 obj->write_domain = 0;
2469 i915_gem_object_move_to_active(obj, seqno);
2470}
2471
2472/** Flushes the GTT write domain for the object if it's dirty. */
2473static void
2474i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj)
2475{
2476 if (obj->write_domain != I915_GEM_DOMAIN_GTT)
2477 return;
2478
2479 /* No actual flushing is required for the GTT write domain. Writes
2480 * to it immediately go to main memory as far as we know, so there's
2481 * no chipset flush. It also doesn't land in render cache.
2482 */
2483 obj->write_domain = 0;
2484}
2485
2486/** Flushes the CPU write domain for the object if it's dirty. */
2487static void
2488i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj)
2489{
2490 struct drm_device *dev = obj->dev;
2491
2492 if (obj->write_domain != I915_GEM_DOMAIN_CPU)
2493 return;
2494
2495 i915_gem_clflush_object(obj);
2496 drm_agp_chipset_flush(dev);
2497 obj->write_domain = 0;
2498}
2499
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002500/**
2501 * Moves a single object to the GTT read, and possibly write domain.
2502 *
2503 * This function returns when the move is complete, including waiting on
2504 * flushes to occur.
2505 */
Jesse Barnes79e53942008-11-07 14:24:08 -08002506int
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002507i915_gem_object_set_to_gtt_domain(struct drm_gem_object *obj, int write)
2508{
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002509 struct drm_i915_gem_object *obj_priv = obj->driver_private;
Eric Anholte47c68e2008-11-14 13:35:19 -08002510 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002511
Eric Anholt02354392008-11-26 13:58:13 -08002512 /* Not valid to be called on unbound objects. */
2513 if (obj_priv->gtt_space == NULL)
2514 return -EINVAL;
2515
Eric Anholte47c68e2008-11-14 13:35:19 -08002516 i915_gem_object_flush_gpu_write_domain(obj);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002517 /* Wait on any GPU rendering and flushing to occur. */
Eric Anholte47c68e2008-11-14 13:35:19 -08002518 ret = i915_gem_object_wait_rendering(obj);
2519 if (ret != 0)
2520 return ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002521
2522 /* If we're writing through the GTT domain, then CPU and GPU caches
2523 * will need to be invalidated at next use.
2524 */
2525 if (write)
Eric Anholte47c68e2008-11-14 13:35:19 -08002526 obj->read_domains &= I915_GEM_DOMAIN_GTT;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002527
Eric Anholte47c68e2008-11-14 13:35:19 -08002528 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002529
2530 /* It should now be out of any other write domains, and we can update
2531 * the domain values for our changes.
2532 */
2533 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2534 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002535 if (write) {
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002536 obj->write_domain = I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08002537 obj_priv->dirty = 1;
2538 }
2539
2540 return 0;
2541}
2542
2543/**
2544 * Moves a single object to the CPU read, and possibly write domain.
2545 *
2546 * This function returns when the move is complete, including waiting on
2547 * flushes to occur.
2548 */
2549static int
2550i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj, int write)
2551{
Eric Anholte47c68e2008-11-14 13:35:19 -08002552 int ret;
2553
2554 i915_gem_object_flush_gpu_write_domain(obj);
2555 /* Wait on any GPU rendering and flushing to occur. */
2556 ret = i915_gem_object_wait_rendering(obj);
2557 if (ret != 0)
2558 return ret;
2559
2560 i915_gem_object_flush_gtt_write_domain(obj);
2561
2562 /* If we have a partially-valid cache of the object in the CPU,
2563 * finish invalidating it and free the per-page flags.
2564 */
2565 i915_gem_object_set_to_full_cpu_read_domain(obj);
2566
2567 /* Flush the CPU cache if it's still invalid. */
2568 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
2569 i915_gem_clflush_object(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08002570
2571 obj->read_domains |= I915_GEM_DOMAIN_CPU;
2572 }
2573
2574 /* It should now be out of any other write domains, and we can update
2575 * the domain values for our changes.
2576 */
2577 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
2578
2579 /* If we're writing through the CPU, then the GPU read domains will
2580 * need to be invalidated at next use.
2581 */
2582 if (write) {
2583 obj->read_domains &= I915_GEM_DOMAIN_CPU;
2584 obj->write_domain = I915_GEM_DOMAIN_CPU;
2585 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002586
2587 return 0;
2588}
2589
Eric Anholt673a3942008-07-30 12:06:12 -07002590/*
2591 * Set the next domain for the specified object. This
2592 * may not actually perform the necessary flushing/invaliding though,
2593 * as that may want to be batched with other set_domain operations
2594 *
2595 * This is (we hope) the only really tricky part of gem. The goal
2596 * is fairly simple -- track which caches hold bits of the object
2597 * and make sure they remain coherent. A few concrete examples may
2598 * help to explain how it works. For shorthand, we use the notation
2599 * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
2600 * a pair of read and write domain masks.
2601 *
2602 * Case 1: the batch buffer
2603 *
2604 * 1. Allocated
2605 * 2. Written by CPU
2606 * 3. Mapped to GTT
2607 * 4. Read by GPU
2608 * 5. Unmapped from GTT
2609 * 6. Freed
2610 *
2611 * Let's take these a step at a time
2612 *
2613 * 1. Allocated
2614 * Pages allocated from the kernel may still have
2615 * cache contents, so we set them to (CPU, CPU) always.
2616 * 2. Written by CPU (using pwrite)
2617 * The pwrite function calls set_domain (CPU, CPU) and
2618 * this function does nothing (as nothing changes)
2619 * 3. Mapped by GTT
2620 * This function asserts that the object is not
2621 * currently in any GPU-based read or write domains
2622 * 4. Read by GPU
2623 * i915_gem_execbuffer calls set_domain (COMMAND, 0).
2624 * As write_domain is zero, this function adds in the
2625 * current read domains (CPU+COMMAND, 0).
2626 * flush_domains is set to CPU.
2627 * invalidate_domains is set to COMMAND
2628 * clflush is run to get data out of the CPU caches
2629 * then i915_dev_set_domain calls i915_gem_flush to
2630 * emit an MI_FLUSH and drm_agp_chipset_flush
2631 * 5. Unmapped from GTT
2632 * i915_gem_object_unbind calls set_domain (CPU, CPU)
2633 * flush_domains and invalidate_domains end up both zero
2634 * so no flushing/invalidating happens
2635 * 6. Freed
2636 * yay, done
2637 *
2638 * Case 2: The shared render buffer
2639 *
2640 * 1. Allocated
2641 * 2. Mapped to GTT
2642 * 3. Read/written by GPU
2643 * 4. set_domain to (CPU,CPU)
2644 * 5. Read/written by CPU
2645 * 6. Read/written by GPU
2646 *
2647 * 1. Allocated
2648 * Same as last example, (CPU, CPU)
2649 * 2. Mapped to GTT
2650 * Nothing changes (assertions find that it is not in the GPU)
2651 * 3. Read/written by GPU
2652 * execbuffer calls set_domain (RENDER, RENDER)
2653 * flush_domains gets CPU
2654 * invalidate_domains gets GPU
2655 * clflush (obj)
2656 * MI_FLUSH and drm_agp_chipset_flush
2657 * 4. set_domain (CPU, CPU)
2658 * flush_domains gets GPU
2659 * invalidate_domains gets CPU
2660 * wait_rendering (obj) to make sure all drawing is complete.
2661 * This will include an MI_FLUSH to get the data from GPU
2662 * to memory
2663 * clflush (obj) to invalidate the CPU cache
2664 * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
2665 * 5. Read/written by CPU
2666 * cache lines are loaded and dirtied
2667 * 6. Read written by GPU
2668 * Same as last GPU access
2669 *
2670 * Case 3: The constant buffer
2671 *
2672 * 1. Allocated
2673 * 2. Written by CPU
2674 * 3. Read by GPU
2675 * 4. Updated (written) by CPU again
2676 * 5. Read by GPU
2677 *
2678 * 1. Allocated
2679 * (CPU, CPU)
2680 * 2. Written by CPU
2681 * (CPU, CPU)
2682 * 3. Read by GPU
2683 * (CPU+RENDER, 0)
2684 * flush_domains = CPU
2685 * invalidate_domains = RENDER
2686 * clflush (obj)
2687 * MI_FLUSH
2688 * drm_agp_chipset_flush
2689 * 4. Updated (written) by CPU again
2690 * (CPU, CPU)
2691 * flush_domains = 0 (no previous write domain)
2692 * invalidate_domains = 0 (no new read domains)
2693 * 5. Read by GPU
2694 * (CPU+RENDER, 0)
2695 * flush_domains = CPU
2696 * invalidate_domains = RENDER
2697 * clflush (obj)
2698 * MI_FLUSH
2699 * drm_agp_chipset_flush
2700 */
Keith Packardc0d90822008-11-20 23:11:08 -08002701static void
Eric Anholt8b0e3782009-02-19 14:40:50 -08002702i915_gem_object_set_to_gpu_domain(struct drm_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002703{
2704 struct drm_device *dev = obj->dev;
2705 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2706 uint32_t invalidate_domains = 0;
2707 uint32_t flush_domains = 0;
Eric Anholte47c68e2008-11-14 13:35:19 -08002708
Eric Anholt8b0e3782009-02-19 14:40:50 -08002709 BUG_ON(obj->pending_read_domains & I915_GEM_DOMAIN_CPU);
2710 BUG_ON(obj->pending_write_domain == I915_GEM_DOMAIN_CPU);
Eric Anholt673a3942008-07-30 12:06:12 -07002711
2712#if WATCH_BUF
2713 DRM_INFO("%s: object %p read %08x -> %08x write %08x -> %08x\n",
2714 __func__, obj,
Eric Anholt8b0e3782009-02-19 14:40:50 -08002715 obj->read_domains, obj->pending_read_domains,
2716 obj->write_domain, obj->pending_write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07002717#endif
2718 /*
2719 * If the object isn't moving to a new write domain,
2720 * let the object stay in multiple read domains
2721 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08002722 if (obj->pending_write_domain == 0)
2723 obj->pending_read_domains |= obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07002724 else
2725 obj_priv->dirty = 1;
2726
2727 /*
2728 * Flush the current write domain if
2729 * the new read domains don't match. Invalidate
2730 * any read domains which differ from the old
2731 * write domain
2732 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08002733 if (obj->write_domain &&
2734 obj->write_domain != obj->pending_read_domains) {
Eric Anholt673a3942008-07-30 12:06:12 -07002735 flush_domains |= obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08002736 invalidate_domains |=
2737 obj->pending_read_domains & ~obj->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07002738 }
2739 /*
2740 * Invalidate any read caches which may have
2741 * stale data. That is, any new read domains.
2742 */
Eric Anholt8b0e3782009-02-19 14:40:50 -08002743 invalidate_domains |= obj->pending_read_domains & ~obj->read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07002744 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU) {
2745#if WATCH_BUF
2746 DRM_INFO("%s: CPU domain flush %08x invalidate %08x\n",
2747 __func__, flush_domains, invalidate_domains);
2748#endif
Eric Anholt673a3942008-07-30 12:06:12 -07002749 i915_gem_clflush_object(obj);
2750 }
2751
Eric Anholtefbeed92009-02-19 14:54:51 -08002752 /* The actual obj->write_domain will be updated with
2753 * pending_write_domain after we emit the accumulated flush for all
2754 * of our domain changes in execbuffers (which clears objects'
2755 * write_domains). So if we have a current write domain that we
2756 * aren't changing, set pending_write_domain to that.
2757 */
2758 if (flush_domains == 0 && obj->pending_write_domain == 0)
2759 obj->pending_write_domain = obj->write_domain;
Eric Anholt8b0e3782009-02-19 14:40:50 -08002760 obj->read_domains = obj->pending_read_domains;
Eric Anholt673a3942008-07-30 12:06:12 -07002761
2762 dev->invalidate_domains |= invalidate_domains;
2763 dev->flush_domains |= flush_domains;
2764#if WATCH_BUF
2765 DRM_INFO("%s: read %08x write %08x invalidate %08x flush %08x\n",
2766 __func__,
2767 obj->read_domains, obj->write_domain,
2768 dev->invalidate_domains, dev->flush_domains);
2769#endif
Eric Anholt673a3942008-07-30 12:06:12 -07002770}
2771
2772/**
Eric Anholte47c68e2008-11-14 13:35:19 -08002773 * Moves the object from a partially CPU read to a full one.
Eric Anholt673a3942008-07-30 12:06:12 -07002774 *
Eric Anholte47c68e2008-11-14 13:35:19 -08002775 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
2776 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
2777 */
2778static void
2779i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj)
2780{
Eric Anholte47c68e2008-11-14 13:35:19 -08002781 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2782
2783 if (!obj_priv->page_cpu_valid)
2784 return;
2785
2786 /* If we're partially in the CPU read domain, finish moving it in.
2787 */
2788 if (obj->read_domains & I915_GEM_DOMAIN_CPU) {
2789 int i;
2790
2791 for (i = 0; i <= (obj->size - 1) / PAGE_SIZE; i++) {
2792 if (obj_priv->page_cpu_valid[i])
2793 continue;
Eric Anholt856fa192009-03-19 14:10:50 -07002794 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholte47c68e2008-11-14 13:35:19 -08002795 }
Eric Anholte47c68e2008-11-14 13:35:19 -08002796 }
2797
2798 /* Free the page_cpu_valid mappings which are now stale, whether
2799 * or not we've got I915_GEM_DOMAIN_CPU.
2800 */
2801 drm_free(obj_priv->page_cpu_valid, obj->size / PAGE_SIZE,
2802 DRM_MEM_DRIVER);
2803 obj_priv->page_cpu_valid = NULL;
2804}
2805
2806/**
2807 * Set the CPU read domain on a range of the object.
2808 *
2809 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
2810 * not entirely valid. The page_cpu_valid member of the object flags which
2811 * pages have been flushed, and will be respected by
2812 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
2813 * of the whole object.
2814 *
2815 * This function returns when the move is complete, including waiting on
2816 * flushes to occur.
Eric Anholt673a3942008-07-30 12:06:12 -07002817 */
2818static int
Eric Anholte47c68e2008-11-14 13:35:19 -08002819i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
2820 uint64_t offset, uint64_t size)
Eric Anholt673a3942008-07-30 12:06:12 -07002821{
2822 struct drm_i915_gem_object *obj_priv = obj->driver_private;
Eric Anholte47c68e2008-11-14 13:35:19 -08002823 int i, ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002824
Eric Anholte47c68e2008-11-14 13:35:19 -08002825 if (offset == 0 && size == obj->size)
2826 return i915_gem_object_set_to_cpu_domain(obj, 0);
2827
2828 i915_gem_object_flush_gpu_write_domain(obj);
2829 /* Wait on any GPU rendering and flushing to occur. */
2830 ret = i915_gem_object_wait_rendering(obj);
2831 if (ret != 0)
2832 return ret;
2833 i915_gem_object_flush_gtt_write_domain(obj);
2834
2835 /* If we're already fully in the CPU read domain, we're done. */
2836 if (obj_priv->page_cpu_valid == NULL &&
2837 (obj->read_domains & I915_GEM_DOMAIN_CPU) != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07002838 return 0;
2839
Eric Anholte47c68e2008-11-14 13:35:19 -08002840 /* Otherwise, create/clear the per-page CPU read domain flag if we're
2841 * newly adding I915_GEM_DOMAIN_CPU
2842 */
Eric Anholt673a3942008-07-30 12:06:12 -07002843 if (obj_priv->page_cpu_valid == NULL) {
2844 obj_priv->page_cpu_valid = drm_calloc(1, obj->size / PAGE_SIZE,
2845 DRM_MEM_DRIVER);
Eric Anholte47c68e2008-11-14 13:35:19 -08002846 if (obj_priv->page_cpu_valid == NULL)
2847 return -ENOMEM;
2848 } else if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0)
2849 memset(obj_priv->page_cpu_valid, 0, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002850
2851 /* Flush the cache on any pages that are still invalid from the CPU's
2852 * perspective.
2853 */
Eric Anholte47c68e2008-11-14 13:35:19 -08002854 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
2855 i++) {
Eric Anholt673a3942008-07-30 12:06:12 -07002856 if (obj_priv->page_cpu_valid[i])
2857 continue;
2858
Eric Anholt856fa192009-03-19 14:10:50 -07002859 drm_clflush_pages(obj_priv->pages + i, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07002860
2861 obj_priv->page_cpu_valid[i] = 1;
2862 }
2863
Eric Anholte47c68e2008-11-14 13:35:19 -08002864 /* It should now be out of any other write domains, and we can update
2865 * the domain values for our changes.
2866 */
2867 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
2868
2869 obj->read_domains |= I915_GEM_DOMAIN_CPU;
2870
Eric Anholt673a3942008-07-30 12:06:12 -07002871 return 0;
2872}
2873
2874/**
Eric Anholt673a3942008-07-30 12:06:12 -07002875 * Pin an object to the GTT and evaluate the relocations landing in it.
2876 */
2877static int
2878i915_gem_object_pin_and_relocate(struct drm_gem_object *obj,
2879 struct drm_file *file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002880 struct drm_i915_gem_exec_object *entry,
2881 struct drm_i915_gem_relocation_entry *relocs)
Eric Anholt673a3942008-07-30 12:06:12 -07002882{
2883 struct drm_device *dev = obj->dev;
Keith Packard0839ccb2008-10-30 19:38:48 -07002884 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07002885 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2886 int i, ret;
Keith Packard0839ccb2008-10-30 19:38:48 -07002887 void __iomem *reloc_page;
Eric Anholt673a3942008-07-30 12:06:12 -07002888
2889 /* Choose the GTT offset for our buffer and put it there. */
2890 ret = i915_gem_object_pin(obj, (uint32_t) entry->alignment);
2891 if (ret)
2892 return ret;
2893
2894 entry->offset = obj_priv->gtt_offset;
2895
Eric Anholt673a3942008-07-30 12:06:12 -07002896 /* Apply the relocations, using the GTT aperture to avoid cache
2897 * flushing requirements.
2898 */
2899 for (i = 0; i < entry->relocation_count; i++) {
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002900 struct drm_i915_gem_relocation_entry *reloc= &relocs[i];
Eric Anholt673a3942008-07-30 12:06:12 -07002901 struct drm_gem_object *target_obj;
2902 struct drm_i915_gem_object *target_obj_priv;
Eric Anholt3043c602008-10-02 12:24:47 -07002903 uint32_t reloc_val, reloc_offset;
2904 uint32_t __iomem *reloc_entry;
Eric Anholt673a3942008-07-30 12:06:12 -07002905
Eric Anholt673a3942008-07-30 12:06:12 -07002906 target_obj = drm_gem_object_lookup(obj->dev, file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002907 reloc->target_handle);
Eric Anholt673a3942008-07-30 12:06:12 -07002908 if (target_obj == NULL) {
2909 i915_gem_object_unpin(obj);
2910 return -EBADF;
2911 }
2912 target_obj_priv = target_obj->driver_private;
2913
2914 /* The target buffer should have appeared before us in the
2915 * exec_object list, so it should have a GTT space bound by now.
2916 */
2917 if (target_obj_priv->gtt_space == NULL) {
2918 DRM_ERROR("No GTT space found for object %d\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002919 reloc->target_handle);
Eric Anholt673a3942008-07-30 12:06:12 -07002920 drm_gem_object_unreference(target_obj);
2921 i915_gem_object_unpin(obj);
2922 return -EINVAL;
2923 }
2924
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002925 if (reloc->offset > obj->size - 4) {
Eric Anholt673a3942008-07-30 12:06:12 -07002926 DRM_ERROR("Relocation beyond object bounds: "
2927 "obj %p target %d offset %d size %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002928 obj, reloc->target_handle,
2929 (int) reloc->offset, (int) obj->size);
Eric Anholt673a3942008-07-30 12:06:12 -07002930 drm_gem_object_unreference(target_obj);
2931 i915_gem_object_unpin(obj);
2932 return -EINVAL;
2933 }
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002934 if (reloc->offset & 3) {
Eric Anholt673a3942008-07-30 12:06:12 -07002935 DRM_ERROR("Relocation not 4-byte aligned: "
2936 "obj %p target %d offset %d.\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002937 obj, reloc->target_handle,
2938 (int) reloc->offset);
Eric Anholt673a3942008-07-30 12:06:12 -07002939 drm_gem_object_unreference(target_obj);
2940 i915_gem_object_unpin(obj);
2941 return -EINVAL;
2942 }
2943
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002944 if (reloc->write_domain & I915_GEM_DOMAIN_CPU ||
2945 reloc->read_domains & I915_GEM_DOMAIN_CPU) {
Eric Anholte47c68e2008-11-14 13:35:19 -08002946 DRM_ERROR("reloc with read/write CPU domains: "
2947 "obj %p target %d offset %d "
2948 "read %08x write %08x",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002949 obj, reloc->target_handle,
2950 (int) reloc->offset,
2951 reloc->read_domains,
2952 reloc->write_domain);
Chris Wilson491152b2009-02-11 14:26:32 +00002953 drm_gem_object_unreference(target_obj);
2954 i915_gem_object_unpin(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08002955 return -EINVAL;
2956 }
2957
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002958 if (reloc->write_domain && target_obj->pending_write_domain &&
2959 reloc->write_domain != target_obj->pending_write_domain) {
Eric Anholt673a3942008-07-30 12:06:12 -07002960 DRM_ERROR("Write domain conflict: "
2961 "obj %p target %d offset %d "
2962 "new %08x old %08x\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002963 obj, reloc->target_handle,
2964 (int) reloc->offset,
2965 reloc->write_domain,
Eric Anholt673a3942008-07-30 12:06:12 -07002966 target_obj->pending_write_domain);
2967 drm_gem_object_unreference(target_obj);
2968 i915_gem_object_unpin(obj);
2969 return -EINVAL;
2970 }
2971
2972#if WATCH_RELOC
2973 DRM_INFO("%s: obj %p offset %08x target %d "
2974 "read %08x write %08x gtt %08x "
2975 "presumed %08x delta %08x\n",
2976 __func__,
2977 obj,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002978 (int) reloc->offset,
2979 (int) reloc->target_handle,
2980 (int) reloc->read_domains,
2981 (int) reloc->write_domain,
Eric Anholt673a3942008-07-30 12:06:12 -07002982 (int) target_obj_priv->gtt_offset,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002983 (int) reloc->presumed_offset,
2984 reloc->delta);
Eric Anholt673a3942008-07-30 12:06:12 -07002985#endif
2986
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002987 target_obj->pending_read_domains |= reloc->read_domains;
2988 target_obj->pending_write_domain |= reloc->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07002989
2990 /* If the relocation already has the right value in it, no
2991 * more work needs to be done.
2992 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07002993 if (target_obj_priv->gtt_offset == reloc->presumed_offset) {
Eric Anholt673a3942008-07-30 12:06:12 -07002994 drm_gem_object_unreference(target_obj);
2995 continue;
2996 }
2997
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002998 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
2999 if (ret != 0) {
3000 drm_gem_object_unreference(target_obj);
3001 i915_gem_object_unpin(obj);
3002 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -07003003 }
3004
3005 /* Map the page containing the relocation we're going to
3006 * perform.
3007 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003008 reloc_offset = obj_priv->gtt_offset + reloc->offset;
Keith Packard0839ccb2008-10-30 19:38:48 -07003009 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
3010 (reloc_offset &
3011 ~(PAGE_SIZE - 1)));
Eric Anholt3043c602008-10-02 12:24:47 -07003012 reloc_entry = (uint32_t __iomem *)(reloc_page +
Keith Packard0839ccb2008-10-30 19:38:48 -07003013 (reloc_offset & (PAGE_SIZE - 1)));
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003014 reloc_val = target_obj_priv->gtt_offset + reloc->delta;
Eric Anholt673a3942008-07-30 12:06:12 -07003015
3016#if WATCH_BUF
3017 DRM_INFO("Applied relocation: %p@0x%08x %08x -> %08x\n",
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003018 obj, (unsigned int) reloc->offset,
Eric Anholt673a3942008-07-30 12:06:12 -07003019 readl(reloc_entry), reloc_val);
3020#endif
3021 writel(reloc_val, reloc_entry);
Keith Packard0839ccb2008-10-30 19:38:48 -07003022 io_mapping_unmap_atomic(reloc_page);
Eric Anholt673a3942008-07-30 12:06:12 -07003023
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003024 /* The updated presumed offset for this entry will be
3025 * copied back out to the user.
Eric Anholt673a3942008-07-30 12:06:12 -07003026 */
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003027 reloc->presumed_offset = target_obj_priv->gtt_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003028
3029 drm_gem_object_unreference(target_obj);
3030 }
3031
Eric Anholt673a3942008-07-30 12:06:12 -07003032#if WATCH_BUF
3033 if (0)
3034 i915_gem_dump_object(obj, 128, __func__, ~0);
3035#endif
3036 return 0;
3037}
3038
3039/** Dispatch a batchbuffer to the ring
3040 */
3041static int
3042i915_dispatch_gem_execbuffer(struct drm_device *dev,
3043 struct drm_i915_gem_execbuffer *exec,
Eric Anholt201361a2009-03-11 12:30:04 -07003044 struct drm_clip_rect *cliprects,
Eric Anholt673a3942008-07-30 12:06:12 -07003045 uint64_t exec_offset)
3046{
3047 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003048 int nbox = exec->num_cliprects;
3049 int i = 0, count;
Chris Wilson83d60792009-06-06 09:45:57 +01003050 uint32_t exec_start, exec_len;
Eric Anholt673a3942008-07-30 12:06:12 -07003051 RING_LOCALS;
3052
3053 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
3054 exec_len = (uint32_t) exec->batch_len;
3055
Eric Anholt673a3942008-07-30 12:06:12 -07003056 count = nbox ? nbox : 1;
3057
3058 for (i = 0; i < count; i++) {
3059 if (i < nbox) {
Eric Anholt201361a2009-03-11 12:30:04 -07003060 int ret = i915_emit_box(dev, cliprects, i,
Eric Anholt673a3942008-07-30 12:06:12 -07003061 exec->DR1, exec->DR4);
3062 if (ret)
3063 return ret;
3064 }
3065
3066 if (IS_I830(dev) || IS_845G(dev)) {
3067 BEGIN_LP_RING(4);
3068 OUT_RING(MI_BATCH_BUFFER);
3069 OUT_RING(exec_start | MI_BATCH_NON_SECURE);
3070 OUT_RING(exec_start + exec_len - 4);
3071 OUT_RING(0);
3072 ADVANCE_LP_RING();
3073 } else {
3074 BEGIN_LP_RING(2);
3075 if (IS_I965G(dev)) {
3076 OUT_RING(MI_BATCH_BUFFER_START |
3077 (2 << 6) |
3078 MI_BATCH_NON_SECURE_I965);
3079 OUT_RING(exec_start);
3080 } else {
3081 OUT_RING(MI_BATCH_BUFFER_START |
3082 (2 << 6));
3083 OUT_RING(exec_start | MI_BATCH_NON_SECURE);
3084 }
3085 ADVANCE_LP_RING();
3086 }
3087 }
3088
3089 /* XXX breadcrumb */
3090 return 0;
3091}
3092
3093/* Throttle our rendering by waiting until the ring has completed our requests
3094 * emitted over 20 msec ago.
3095 *
Eric Anholtb9624422009-06-03 07:27:35 +00003096 * Note that if we were to use the current jiffies each time around the loop,
3097 * we wouldn't escape the function with any frames outstanding if the time to
3098 * render a frame was over 20ms.
3099 *
Eric Anholt673a3942008-07-30 12:06:12 -07003100 * This should get us reasonable parallelism between CPU and GPU but also
3101 * relatively low latency when blocking on a particular request to finish.
3102 */
3103static int
3104i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file_priv)
3105{
3106 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
3107 int ret = 0;
Eric Anholtb9624422009-06-03 07:27:35 +00003108 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
Eric Anholt673a3942008-07-30 12:06:12 -07003109
3110 mutex_lock(&dev->struct_mutex);
Eric Anholtb9624422009-06-03 07:27:35 +00003111 while (!list_empty(&i915_file_priv->mm.request_list)) {
3112 struct drm_i915_gem_request *request;
3113
3114 request = list_first_entry(&i915_file_priv->mm.request_list,
3115 struct drm_i915_gem_request,
3116 client_list);
3117
3118 if (time_after_eq(request->emitted_jiffies, recent_enough))
3119 break;
3120
3121 ret = i915_wait_request(dev, request->seqno);
3122 if (ret != 0)
3123 break;
3124 }
Eric Anholt673a3942008-07-30 12:06:12 -07003125 mutex_unlock(&dev->struct_mutex);
Eric Anholtb9624422009-06-03 07:27:35 +00003126
Eric Anholt673a3942008-07-30 12:06:12 -07003127 return ret;
3128}
3129
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003130static int
3131i915_gem_get_relocs_from_user(struct drm_i915_gem_exec_object *exec_list,
3132 uint32_t buffer_count,
3133 struct drm_i915_gem_relocation_entry **relocs)
3134{
3135 uint32_t reloc_count = 0, reloc_index = 0, i;
3136 int ret;
3137
3138 *relocs = NULL;
3139 for (i = 0; i < buffer_count; i++) {
3140 if (reloc_count + exec_list[i].relocation_count < reloc_count)
3141 return -EINVAL;
3142 reloc_count += exec_list[i].relocation_count;
3143 }
3144
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003145 *relocs = drm_calloc_large(reloc_count, sizeof(**relocs));
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003146 if (*relocs == NULL)
3147 return -ENOMEM;
3148
3149 for (i = 0; i < buffer_count; i++) {
3150 struct drm_i915_gem_relocation_entry __user *user_relocs;
3151
3152 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3153
3154 ret = copy_from_user(&(*relocs)[reloc_index],
3155 user_relocs,
3156 exec_list[i].relocation_count *
3157 sizeof(**relocs));
3158 if (ret != 0) {
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003159 drm_free_large(*relocs);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003160 *relocs = NULL;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003161 return -EFAULT;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003162 }
3163
3164 reloc_index += exec_list[i].relocation_count;
3165 }
3166
Florian Mickler2bc43b52009-04-06 22:55:41 +02003167 return 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003168}
3169
3170static int
3171i915_gem_put_relocs_to_user(struct drm_i915_gem_exec_object *exec_list,
3172 uint32_t buffer_count,
3173 struct drm_i915_gem_relocation_entry *relocs)
3174{
3175 uint32_t reloc_count = 0, i;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003176 int ret = 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003177
3178 for (i = 0; i < buffer_count; i++) {
3179 struct drm_i915_gem_relocation_entry __user *user_relocs;
Florian Mickler2bc43b52009-04-06 22:55:41 +02003180 int unwritten;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003181
3182 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3183
Florian Mickler2bc43b52009-04-06 22:55:41 +02003184 unwritten = copy_to_user(user_relocs,
3185 &relocs[reloc_count],
3186 exec_list[i].relocation_count *
3187 sizeof(*relocs));
3188
3189 if (unwritten) {
3190 ret = -EFAULT;
3191 goto err;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003192 }
3193
3194 reloc_count += exec_list[i].relocation_count;
3195 }
3196
Florian Mickler2bc43b52009-04-06 22:55:41 +02003197err:
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003198 drm_free_large(relocs);
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003199
3200 return ret;
3201}
3202
Chris Wilson83d60792009-06-06 09:45:57 +01003203static int
3204i915_gem_check_execbuffer (struct drm_i915_gem_execbuffer *exec,
3205 uint64_t exec_offset)
3206{
3207 uint32_t exec_start, exec_len;
3208
3209 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
3210 exec_len = (uint32_t) exec->batch_len;
3211
3212 if ((exec_start | exec_len) & 0x7)
3213 return -EINVAL;
3214
3215 if (!exec_start)
3216 return -EINVAL;
3217
3218 return 0;
3219}
3220
Eric Anholt673a3942008-07-30 12:06:12 -07003221int
3222i915_gem_execbuffer(struct drm_device *dev, void *data,
3223 struct drm_file *file_priv)
3224{
3225 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07003226 struct drm_i915_gem_execbuffer *args = data;
3227 struct drm_i915_gem_exec_object *exec_list = NULL;
3228 struct drm_gem_object **object_list = NULL;
3229 struct drm_gem_object *batch_obj;
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003230 struct drm_i915_gem_object *obj_priv;
Eric Anholt201361a2009-03-11 12:30:04 -07003231 struct drm_clip_rect *cliprects = NULL;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003232 struct drm_i915_gem_relocation_entry *relocs;
3233 int ret, ret2, i, pinned = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003234 uint64_t exec_offset;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003235 uint32_t seqno, flush_domains, reloc_index;
Keith Packardac94a962008-11-20 23:30:27 -08003236 int pin_tries;
Eric Anholt673a3942008-07-30 12:06:12 -07003237
3238#if WATCH_EXEC
3239 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3240 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3241#endif
3242
Eric Anholt4f481ed2008-09-10 14:22:49 -07003243 if (args->buffer_count < 1) {
3244 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3245 return -EINVAL;
3246 }
Eric Anholt673a3942008-07-30 12:06:12 -07003247 /* Copy in the exec list from userland */
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003248 exec_list = drm_calloc_large(sizeof(*exec_list), args->buffer_count);
3249 object_list = drm_calloc_large(sizeof(*object_list), args->buffer_count);
Eric Anholt673a3942008-07-30 12:06:12 -07003250 if (exec_list == NULL || object_list == NULL) {
3251 DRM_ERROR("Failed to allocate exec or object list "
3252 "for %d buffers\n",
3253 args->buffer_count);
3254 ret = -ENOMEM;
3255 goto pre_mutex_err;
3256 }
3257 ret = copy_from_user(exec_list,
3258 (struct drm_i915_relocation_entry __user *)
3259 (uintptr_t) args->buffers_ptr,
3260 sizeof(*exec_list) * args->buffer_count);
3261 if (ret != 0) {
3262 DRM_ERROR("copy %d exec entries failed %d\n",
3263 args->buffer_count, ret);
3264 goto pre_mutex_err;
3265 }
3266
Eric Anholt201361a2009-03-11 12:30:04 -07003267 if (args->num_cliprects != 0) {
3268 cliprects = drm_calloc(args->num_cliprects, sizeof(*cliprects),
3269 DRM_MEM_DRIVER);
3270 if (cliprects == NULL)
3271 goto pre_mutex_err;
3272
3273 ret = copy_from_user(cliprects,
3274 (struct drm_clip_rect __user *)
3275 (uintptr_t) args->cliprects_ptr,
3276 sizeof(*cliprects) * args->num_cliprects);
3277 if (ret != 0) {
3278 DRM_ERROR("copy %d cliprects failed: %d\n",
3279 args->num_cliprects, ret);
3280 goto pre_mutex_err;
3281 }
3282 }
3283
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003284 ret = i915_gem_get_relocs_from_user(exec_list, args->buffer_count,
3285 &relocs);
3286 if (ret != 0)
3287 goto pre_mutex_err;
3288
Eric Anholt673a3942008-07-30 12:06:12 -07003289 mutex_lock(&dev->struct_mutex);
3290
3291 i915_verify_inactive(dev, __FILE__, __LINE__);
3292
3293 if (dev_priv->mm.wedged) {
3294 DRM_ERROR("Execbuf while wedged\n");
3295 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003296 ret = -EIO;
3297 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003298 }
3299
3300 if (dev_priv->mm.suspended) {
3301 DRM_ERROR("Execbuf while VT-switched.\n");
3302 mutex_unlock(&dev->struct_mutex);
Chris Wilsona198bc82009-02-06 16:55:20 +00003303 ret = -EBUSY;
3304 goto pre_mutex_err;
Eric Anholt673a3942008-07-30 12:06:12 -07003305 }
3306
Keith Packardac94a962008-11-20 23:30:27 -08003307 /* Look up object handles */
Eric Anholt673a3942008-07-30 12:06:12 -07003308 for (i = 0; i < args->buffer_count; i++) {
3309 object_list[i] = drm_gem_object_lookup(dev, file_priv,
3310 exec_list[i].handle);
3311 if (object_list[i] == NULL) {
3312 DRM_ERROR("Invalid object handle %d at index %d\n",
3313 exec_list[i].handle, i);
3314 ret = -EBADF;
3315 goto err;
3316 }
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003317
3318 obj_priv = object_list[i]->driver_private;
3319 if (obj_priv->in_execbuffer) {
3320 DRM_ERROR("Object %p appears more than once in object list\n",
3321 object_list[i]);
3322 ret = -EBADF;
3323 goto err;
3324 }
3325 obj_priv->in_execbuffer = true;
Keith Packardac94a962008-11-20 23:30:27 -08003326 }
Eric Anholt673a3942008-07-30 12:06:12 -07003327
Keith Packardac94a962008-11-20 23:30:27 -08003328 /* Pin and relocate */
3329 for (pin_tries = 0; ; pin_tries++) {
3330 ret = 0;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003331 reloc_index = 0;
3332
Keith Packardac94a962008-11-20 23:30:27 -08003333 for (i = 0; i < args->buffer_count; i++) {
3334 object_list[i]->pending_read_domains = 0;
3335 object_list[i]->pending_write_domain = 0;
3336 ret = i915_gem_object_pin_and_relocate(object_list[i],
3337 file_priv,
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003338 &exec_list[i],
3339 &relocs[reloc_index]);
Keith Packardac94a962008-11-20 23:30:27 -08003340 if (ret)
3341 break;
3342 pinned = i + 1;
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003343 reloc_index += exec_list[i].relocation_count;
Keith Packardac94a962008-11-20 23:30:27 -08003344 }
3345 /* success */
3346 if (ret == 0)
3347 break;
3348
3349 /* error other than GTT full, or we've already tried again */
Chris Wilson2939e1f2009-06-06 09:46:03 +01003350 if (ret != -ENOSPC || pin_tries >= 1) {
Eric Anholtf1acec92008-12-19 14:47:48 -08003351 if (ret != -ERESTARTSYS)
3352 DRM_ERROR("Failed to pin buffers %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07003353 goto err;
3354 }
Keith Packardac94a962008-11-20 23:30:27 -08003355
3356 /* unpin all of our buffers */
3357 for (i = 0; i < pinned; i++)
3358 i915_gem_object_unpin(object_list[i]);
Eric Anholtb1177632008-12-10 10:09:41 -08003359 pinned = 0;
Keith Packardac94a962008-11-20 23:30:27 -08003360
3361 /* evict everyone we can from the aperture */
3362 ret = i915_gem_evict_everything(dev);
3363 if (ret)
3364 goto err;
Eric Anholt673a3942008-07-30 12:06:12 -07003365 }
3366
3367 /* Set the pending read domains for the batch buffer to COMMAND */
3368 batch_obj = object_list[args->buffer_count-1];
Chris Wilson5f26a2c2009-06-06 09:45:58 +01003369 if (batch_obj->pending_write_domain) {
3370 DRM_ERROR("Attempting to use self-modifying batch buffer\n");
3371 ret = -EINVAL;
3372 goto err;
3373 }
3374 batch_obj->pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
Eric Anholt673a3942008-07-30 12:06:12 -07003375
Chris Wilson83d60792009-06-06 09:45:57 +01003376 /* Sanity check the batch buffer, prior to moving objects */
3377 exec_offset = exec_list[args->buffer_count - 1].offset;
3378 ret = i915_gem_check_execbuffer (args, exec_offset);
3379 if (ret != 0) {
3380 DRM_ERROR("execbuf with invalid offset/length\n");
3381 goto err;
3382 }
3383
Eric Anholt673a3942008-07-30 12:06:12 -07003384 i915_verify_inactive(dev, __FILE__, __LINE__);
3385
Keith Packard646f0f62008-11-20 23:23:03 -08003386 /* Zero the global flush/invalidate flags. These
3387 * will be modified as new domains are computed
3388 * for each object
3389 */
3390 dev->invalidate_domains = 0;
3391 dev->flush_domains = 0;
3392
Eric Anholt673a3942008-07-30 12:06:12 -07003393 for (i = 0; i < args->buffer_count; i++) {
3394 struct drm_gem_object *obj = object_list[i];
Eric Anholt673a3942008-07-30 12:06:12 -07003395
Keith Packard646f0f62008-11-20 23:23:03 -08003396 /* Compute new gpu domains and update invalidate/flush */
Eric Anholt8b0e3782009-02-19 14:40:50 -08003397 i915_gem_object_set_to_gpu_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003398 }
3399
3400 i915_verify_inactive(dev, __FILE__, __LINE__);
3401
Keith Packard646f0f62008-11-20 23:23:03 -08003402 if (dev->invalidate_domains | dev->flush_domains) {
3403#if WATCH_EXEC
3404 DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
3405 __func__,
3406 dev->invalidate_domains,
3407 dev->flush_domains);
3408#endif
3409 i915_gem_flush(dev,
3410 dev->invalidate_domains,
3411 dev->flush_domains);
3412 if (dev->flush_domains)
Eric Anholtb9624422009-06-03 07:27:35 +00003413 (void)i915_add_request(dev, file_priv,
3414 dev->flush_domains);
Keith Packard646f0f62008-11-20 23:23:03 -08003415 }
Eric Anholt673a3942008-07-30 12:06:12 -07003416
Eric Anholtefbeed92009-02-19 14:54:51 -08003417 for (i = 0; i < args->buffer_count; i++) {
3418 struct drm_gem_object *obj = object_list[i];
3419
3420 obj->write_domain = obj->pending_write_domain;
3421 }
3422
Eric Anholt673a3942008-07-30 12:06:12 -07003423 i915_verify_inactive(dev, __FILE__, __LINE__);
3424
3425#if WATCH_COHERENCY
3426 for (i = 0; i < args->buffer_count; i++) {
3427 i915_gem_object_check_coherency(object_list[i],
3428 exec_list[i].handle);
3429 }
3430#endif
3431
Eric Anholt673a3942008-07-30 12:06:12 -07003432#if WATCH_EXEC
Ben Gamari6911a9b2009-04-02 11:24:54 -07003433 i915_gem_dump_object(batch_obj,
Eric Anholt673a3942008-07-30 12:06:12 -07003434 args->batch_len,
3435 __func__,
3436 ~0);
3437#endif
3438
Eric Anholt673a3942008-07-30 12:06:12 -07003439 /* Exec the batchbuffer */
Eric Anholt201361a2009-03-11 12:30:04 -07003440 ret = i915_dispatch_gem_execbuffer(dev, args, cliprects, exec_offset);
Eric Anholt673a3942008-07-30 12:06:12 -07003441 if (ret) {
3442 DRM_ERROR("dispatch failed %d\n", ret);
3443 goto err;
3444 }
3445
3446 /*
3447 * Ensure that the commands in the batch buffer are
3448 * finished before the interrupt fires
3449 */
3450 flush_domains = i915_retire_commands(dev);
3451
3452 i915_verify_inactive(dev, __FILE__, __LINE__);
3453
3454 /*
3455 * Get a seqno representing the execution of the current buffer,
3456 * which we can wait on. We would like to mitigate these interrupts,
3457 * likely by only creating seqnos occasionally (so that we have
3458 * *some* interrupts representing completion of buffers that we can
3459 * wait on when trying to clear up gtt space).
3460 */
Eric Anholtb9624422009-06-03 07:27:35 +00003461 seqno = i915_add_request(dev, file_priv, flush_domains);
Eric Anholt673a3942008-07-30 12:06:12 -07003462 BUG_ON(seqno == 0);
Eric Anholt673a3942008-07-30 12:06:12 -07003463 for (i = 0; i < args->buffer_count; i++) {
3464 struct drm_gem_object *obj = object_list[i];
Eric Anholt673a3942008-07-30 12:06:12 -07003465
Eric Anholtce44b0e2008-11-06 16:00:31 -08003466 i915_gem_object_move_to_active(obj, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07003467#if WATCH_LRU
3468 DRM_INFO("%s: move to exec list %p\n", __func__, obj);
3469#endif
3470 }
3471#if WATCH_LRU
3472 i915_dump_lru(dev, __func__);
3473#endif
3474
3475 i915_verify_inactive(dev, __FILE__, __LINE__);
3476
Eric Anholt673a3942008-07-30 12:06:12 -07003477err:
Julia Lawallaad87df2008-12-21 16:28:47 +01003478 for (i = 0; i < pinned; i++)
3479 i915_gem_object_unpin(object_list[i]);
Eric Anholt673a3942008-07-30 12:06:12 -07003480
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003481 for (i = 0; i < args->buffer_count; i++) {
3482 if (object_list[i]) {
3483 obj_priv = object_list[i]->driver_private;
3484 obj_priv->in_execbuffer = false;
3485 }
Julia Lawallaad87df2008-12-21 16:28:47 +01003486 drm_gem_object_unreference(object_list[i]);
Kristian Høgsbergb70d11d2009-03-03 14:45:57 -05003487 }
Julia Lawallaad87df2008-12-21 16:28:47 +01003488
Eric Anholt673a3942008-07-30 12:06:12 -07003489 mutex_unlock(&dev->struct_mutex);
3490
Roland Dreiera35f2e22009-02-06 17:48:09 -08003491 if (!ret) {
3492 /* Copy the new buffer offsets back to the user's exec list. */
3493 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
3494 (uintptr_t) args->buffers_ptr,
3495 exec_list,
3496 sizeof(*exec_list) * args->buffer_count);
Florian Mickler2bc43b52009-04-06 22:55:41 +02003497 if (ret) {
3498 ret = -EFAULT;
Roland Dreiera35f2e22009-02-06 17:48:09 -08003499 DRM_ERROR("failed to copy %d exec entries "
3500 "back to user (%d)\n",
3501 args->buffer_count, ret);
Florian Mickler2bc43b52009-04-06 22:55:41 +02003502 }
Roland Dreiera35f2e22009-02-06 17:48:09 -08003503 }
3504
Eric Anholt40a5f0d2009-03-12 11:23:52 -07003505 /* Copy the updated relocations out regardless of current error
3506 * state. Failure to update the relocs would mean that the next
3507 * time userland calls execbuf, it would do so with presumed offset
3508 * state that didn't match the actual object state.
3509 */
3510 ret2 = i915_gem_put_relocs_to_user(exec_list, args->buffer_count,
3511 relocs);
3512 if (ret2 != 0) {
3513 DRM_ERROR("Failed to copy relocations back out: %d\n", ret2);
3514
3515 if (ret == 0)
3516 ret = ret2;
3517 }
3518
Eric Anholt673a3942008-07-30 12:06:12 -07003519pre_mutex_err:
Jesse Barnes8e7d2b22009-05-08 16:13:25 -07003520 drm_free_large(object_list);
3521 drm_free_large(exec_list);
Eric Anholt201361a2009-03-11 12:30:04 -07003522 drm_free(cliprects, sizeof(*cliprects) * args->num_cliprects,
3523 DRM_MEM_DRIVER);
Eric Anholt673a3942008-07-30 12:06:12 -07003524
3525 return ret;
3526}
3527
3528int
3529i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment)
3530{
3531 struct drm_device *dev = obj->dev;
3532 struct drm_i915_gem_object *obj_priv = obj->driver_private;
3533 int ret;
3534
3535 i915_verify_inactive(dev, __FILE__, __LINE__);
3536 if (obj_priv->gtt_space == NULL) {
3537 ret = i915_gem_object_bind_to_gtt(obj, alignment);
3538 if (ret != 0) {
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08003539 if (ret != -EBUSY && ret != -ERESTARTSYS)
Kyle McMartin0fce81e2009-02-28 15:01:16 -05003540 DRM_ERROR("Failure to bind: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07003541 return ret;
3542 }
Chris Wilson22c344e2009-02-11 14:26:45 +00003543 }
3544 /*
3545 * Pre-965 chips need a fence register set up in order to
3546 * properly handle tiled surfaces.
3547 */
3548 if (!IS_I965G(dev) &&
3549 obj_priv->fence_reg == I915_FENCE_REG_NONE &&
3550 obj_priv->tiling_mode != I915_TILING_NONE) {
Chris Wilson8c4b8c32009-06-17 22:08:52 +01003551 ret = i915_gem_object_get_fence_reg(obj);
Chris Wilson22c344e2009-02-11 14:26:45 +00003552 if (ret != 0) {
3553 if (ret != -EBUSY && ret != -ERESTARTSYS)
3554 DRM_ERROR("Failure to install fence: %d\n",
3555 ret);
3556 return ret;
3557 }
Eric Anholt673a3942008-07-30 12:06:12 -07003558 }
3559 obj_priv->pin_count++;
3560
3561 /* If the object is not active and not pending a flush,
3562 * remove it from the inactive list
3563 */
3564 if (obj_priv->pin_count == 1) {
3565 atomic_inc(&dev->pin_count);
3566 atomic_add(obj->size, &dev->pin_memory);
3567 if (!obj_priv->active &&
Chris Wilson21d509e2009-06-06 09:46:02 +01003568 (obj->write_domain & I915_GEM_GPU_DOMAINS) == 0 &&
Eric Anholt673a3942008-07-30 12:06:12 -07003569 !list_empty(&obj_priv->list))
3570 list_del_init(&obj_priv->list);
3571 }
3572 i915_verify_inactive(dev, __FILE__, __LINE__);
3573
3574 return 0;
3575}
3576
3577void
3578i915_gem_object_unpin(struct drm_gem_object *obj)
3579{
3580 struct drm_device *dev = obj->dev;
3581 drm_i915_private_t *dev_priv = dev->dev_private;
3582 struct drm_i915_gem_object *obj_priv = obj->driver_private;
3583
3584 i915_verify_inactive(dev, __FILE__, __LINE__);
3585 obj_priv->pin_count--;
3586 BUG_ON(obj_priv->pin_count < 0);
3587 BUG_ON(obj_priv->gtt_space == NULL);
3588
3589 /* If the object is no longer pinned, and is
3590 * neither active nor being flushed, then stick it on
3591 * the inactive list
3592 */
3593 if (obj_priv->pin_count == 0) {
3594 if (!obj_priv->active &&
Chris Wilson21d509e2009-06-06 09:46:02 +01003595 (obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
Eric Anholt673a3942008-07-30 12:06:12 -07003596 list_move_tail(&obj_priv->list,
3597 &dev_priv->mm.inactive_list);
3598 atomic_dec(&dev->pin_count);
3599 atomic_sub(obj->size, &dev->pin_memory);
3600 }
3601 i915_verify_inactive(dev, __FILE__, __LINE__);
3602}
3603
3604int
3605i915_gem_pin_ioctl(struct drm_device *dev, void *data,
3606 struct drm_file *file_priv)
3607{
3608 struct drm_i915_gem_pin *args = data;
3609 struct drm_gem_object *obj;
3610 struct drm_i915_gem_object *obj_priv;
3611 int ret;
3612
3613 mutex_lock(&dev->struct_mutex);
3614
3615 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
3616 if (obj == NULL) {
3617 DRM_ERROR("Bad handle in i915_gem_pin_ioctl(): %d\n",
3618 args->handle);
3619 mutex_unlock(&dev->struct_mutex);
3620 return -EBADF;
3621 }
3622 obj_priv = obj->driver_private;
3623
Jesse Barnes79e53942008-11-07 14:24:08 -08003624 if (obj_priv->pin_filp != NULL && obj_priv->pin_filp != file_priv) {
3625 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
3626 args->handle);
Chris Wilson96dec612009-02-08 19:08:04 +00003627 drm_gem_object_unreference(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003628 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08003629 return -EINVAL;
3630 }
3631
3632 obj_priv->user_pin_count++;
3633 obj_priv->pin_filp = file_priv;
3634 if (obj_priv->user_pin_count == 1) {
3635 ret = i915_gem_object_pin(obj, args->alignment);
3636 if (ret != 0) {
3637 drm_gem_object_unreference(obj);
3638 mutex_unlock(&dev->struct_mutex);
3639 return ret;
3640 }
Eric Anholt673a3942008-07-30 12:06:12 -07003641 }
3642
3643 /* XXX - flush the CPU caches for pinned objects
3644 * as the X server doesn't manage domains yet
3645 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003646 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003647 args->offset = obj_priv->gtt_offset;
3648 drm_gem_object_unreference(obj);
3649 mutex_unlock(&dev->struct_mutex);
3650
3651 return 0;
3652}
3653
3654int
3655i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
3656 struct drm_file *file_priv)
3657{
3658 struct drm_i915_gem_pin *args = data;
3659 struct drm_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08003660 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07003661
3662 mutex_lock(&dev->struct_mutex);
3663
3664 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
3665 if (obj == NULL) {
3666 DRM_ERROR("Bad handle in i915_gem_unpin_ioctl(): %d\n",
3667 args->handle);
3668 mutex_unlock(&dev->struct_mutex);
3669 return -EBADF;
3670 }
3671
Jesse Barnes79e53942008-11-07 14:24:08 -08003672 obj_priv = obj->driver_private;
3673 if (obj_priv->pin_filp != file_priv) {
3674 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
3675 args->handle);
3676 drm_gem_object_unreference(obj);
3677 mutex_unlock(&dev->struct_mutex);
3678 return -EINVAL;
3679 }
3680 obj_priv->user_pin_count--;
3681 if (obj_priv->user_pin_count == 0) {
3682 obj_priv->pin_filp = NULL;
3683 i915_gem_object_unpin(obj);
3684 }
Eric Anholt673a3942008-07-30 12:06:12 -07003685
3686 drm_gem_object_unreference(obj);
3687 mutex_unlock(&dev->struct_mutex);
3688 return 0;
3689}
3690
3691int
3692i915_gem_busy_ioctl(struct drm_device *dev, void *data,
3693 struct drm_file *file_priv)
3694{
3695 struct drm_i915_gem_busy *args = data;
3696 struct drm_gem_object *obj;
3697 struct drm_i915_gem_object *obj_priv;
3698
Eric Anholt673a3942008-07-30 12:06:12 -07003699 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
3700 if (obj == NULL) {
3701 DRM_ERROR("Bad handle in i915_gem_busy_ioctl(): %d\n",
3702 args->handle);
Eric Anholt673a3942008-07-30 12:06:12 -07003703 return -EBADF;
3704 }
3705
Chris Wilsonb1ce7862009-06-06 09:46:00 +01003706 mutex_lock(&dev->struct_mutex);
Eric Anholtf21289b2009-02-18 09:44:56 -08003707 /* Update the active list for the hardware's current position.
3708 * Otherwise this only updates on a delayed timer or when irqs are
3709 * actually unmasked, and our working set ends up being larger than
3710 * required.
3711 */
3712 i915_gem_retire_requests(dev);
3713
Eric Anholt673a3942008-07-30 12:06:12 -07003714 obj_priv = obj->driver_private;
Eric Anholtc4de0a52008-12-14 19:05:04 -08003715 /* Don't count being on the flushing list against the object being
3716 * done. Otherwise, a buffer left on the flushing list but not getting
3717 * flushed (because nobody's flushing that domain) won't ever return
3718 * unbusy and get reused by libdrm's bo cache. The other expected
3719 * consumer of this interface, OpenGL's occlusion queries, also specs
3720 * that the objects get unbusy "eventually" without any interference.
3721 */
3722 args->busy = obj_priv->active && obj_priv->last_rendering_seqno != 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003723
3724 drm_gem_object_unreference(obj);
3725 mutex_unlock(&dev->struct_mutex);
3726 return 0;
3727}
3728
3729int
3730i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
3731 struct drm_file *file_priv)
3732{
3733 return i915_gem_ring_throttle(dev, file_priv);
3734}
3735
3736int i915_gem_init_object(struct drm_gem_object *obj)
3737{
3738 struct drm_i915_gem_object *obj_priv;
3739
3740 obj_priv = drm_calloc(1, sizeof(*obj_priv), DRM_MEM_DRIVER);
3741 if (obj_priv == NULL)
3742 return -ENOMEM;
3743
3744 /*
3745 * We've just allocated pages from the kernel,
3746 * so they've just been written by the CPU with
3747 * zeros. They'll need to be clflushed before we
3748 * use them with the GPU.
3749 */
3750 obj->write_domain = I915_GEM_DOMAIN_CPU;
3751 obj->read_domains = I915_GEM_DOMAIN_CPU;
3752
Keith Packardba1eb1d2008-10-14 19:55:10 -07003753 obj_priv->agp_type = AGP_USER_MEMORY;
3754
Eric Anholt673a3942008-07-30 12:06:12 -07003755 obj->driver_private = obj_priv;
3756 obj_priv->obj = obj;
Jesse Barnesde151cf2008-11-12 10:03:55 -08003757 obj_priv->fence_reg = I915_FENCE_REG_NONE;
Eric Anholt673a3942008-07-30 12:06:12 -07003758 INIT_LIST_HEAD(&obj_priv->list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08003759
Eric Anholt673a3942008-07-30 12:06:12 -07003760 return 0;
3761}
3762
3763void i915_gem_free_object(struct drm_gem_object *obj)
3764{
Jesse Barnesde151cf2008-11-12 10:03:55 -08003765 struct drm_device *dev = obj->dev;
Eric Anholt673a3942008-07-30 12:06:12 -07003766 struct drm_i915_gem_object *obj_priv = obj->driver_private;
3767
3768 while (obj_priv->pin_count > 0)
3769 i915_gem_object_unpin(obj);
3770
Dave Airlie71acb5e2008-12-30 20:31:46 +10003771 if (obj_priv->phys_obj)
3772 i915_gem_detach_phys_object(dev, obj);
3773
Eric Anholt673a3942008-07-30 12:06:12 -07003774 i915_gem_object_unbind(obj);
3775
Jesse Barnesab00b3e2009-02-11 14:01:46 -08003776 i915_gem_free_mmap_offset(obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -08003777
Eric Anholt673a3942008-07-30 12:06:12 -07003778 drm_free(obj_priv->page_cpu_valid, 1, DRM_MEM_DRIVER);
Eric Anholt280b7132009-03-12 16:56:27 -07003779 kfree(obj_priv->bit_17);
Eric Anholt673a3942008-07-30 12:06:12 -07003780 drm_free(obj->driver_private, 1, DRM_MEM_DRIVER);
3781}
3782
Eric Anholt673a3942008-07-30 12:06:12 -07003783/** Unbinds all objects that are on the given buffer list. */
3784static int
3785i915_gem_evict_from_list(struct drm_device *dev, struct list_head *head)
3786{
3787 struct drm_gem_object *obj;
3788 struct drm_i915_gem_object *obj_priv;
3789 int ret;
3790
3791 while (!list_empty(head)) {
3792 obj_priv = list_first_entry(head,
3793 struct drm_i915_gem_object,
3794 list);
3795 obj = obj_priv->obj;
3796
3797 if (obj_priv->pin_count != 0) {
3798 DRM_ERROR("Pinned object in unbind list\n");
3799 mutex_unlock(&dev->struct_mutex);
3800 return -EINVAL;
3801 }
3802
3803 ret = i915_gem_object_unbind(obj);
3804 if (ret != 0) {
3805 DRM_ERROR("Error unbinding object in LeaveVT: %d\n",
3806 ret);
3807 mutex_unlock(&dev->struct_mutex);
3808 return ret;
3809 }
3810 }
3811
3812
3813 return 0;
3814}
3815
Jesse Barnes5669fca2009-02-17 15:13:31 -08003816int
Eric Anholt673a3942008-07-30 12:06:12 -07003817i915_gem_idle(struct drm_device *dev)
3818{
3819 drm_i915_private_t *dev_priv = dev->dev_private;
3820 uint32_t seqno, cur_seqno, last_seqno;
3821 int stuck, ret;
3822
Keith Packard6dbe2772008-10-14 21:41:13 -07003823 mutex_lock(&dev->struct_mutex);
3824
3825 if (dev_priv->mm.suspended || dev_priv->ring.ring_obj == NULL) {
3826 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07003827 return 0;
Keith Packard6dbe2772008-10-14 21:41:13 -07003828 }
Eric Anholt673a3942008-07-30 12:06:12 -07003829
3830 /* Hack! Don't let anybody do execbuf while we don't control the chip.
3831 * We need to replace this with a semaphore, or something.
3832 */
3833 dev_priv->mm.suspended = 1;
3834
Keith Packard6dbe2772008-10-14 21:41:13 -07003835 /* Cancel the retire work handler, wait for it to finish if running
3836 */
3837 mutex_unlock(&dev->struct_mutex);
3838 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
3839 mutex_lock(&dev->struct_mutex);
3840
Eric Anholt673a3942008-07-30 12:06:12 -07003841 i915_kernel_lost_context(dev);
3842
3843 /* Flush the GPU along with all non-CPU write domains
3844 */
Chris Wilson21d509e2009-06-06 09:46:02 +01003845 i915_gem_flush(dev, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
3846 seqno = i915_add_request(dev, NULL, I915_GEM_GPU_DOMAINS);
Eric Anholt673a3942008-07-30 12:06:12 -07003847
3848 if (seqno == 0) {
3849 mutex_unlock(&dev->struct_mutex);
3850 return -ENOMEM;
3851 }
3852
3853 dev_priv->mm.waiting_gem_seqno = seqno;
3854 last_seqno = 0;
3855 stuck = 0;
3856 for (;;) {
3857 cur_seqno = i915_get_gem_seqno(dev);
3858 if (i915_seqno_passed(cur_seqno, seqno))
3859 break;
3860 if (last_seqno == cur_seqno) {
3861 if (stuck++ > 100) {
3862 DRM_ERROR("hardware wedged\n");
3863 dev_priv->mm.wedged = 1;
3864 DRM_WAKEUP(&dev_priv->irq_queue);
3865 break;
3866 }
3867 }
3868 msleep(10);
3869 last_seqno = cur_seqno;
3870 }
3871 dev_priv->mm.waiting_gem_seqno = 0;
3872
3873 i915_gem_retire_requests(dev);
3874
Carl Worth5e118f42009-03-20 11:54:25 -07003875 spin_lock(&dev_priv->mm.active_list_lock);
Eric Anholt28dfe522008-11-13 15:00:55 -08003876 if (!dev_priv->mm.wedged) {
3877 /* Active and flushing should now be empty as we've
3878 * waited for a sequence higher than any pending execbuffer
3879 */
3880 WARN_ON(!list_empty(&dev_priv->mm.active_list));
3881 WARN_ON(!list_empty(&dev_priv->mm.flushing_list));
3882 /* Request should now be empty as we've also waited
3883 * for the last request in the list
3884 */
3885 WARN_ON(!list_empty(&dev_priv->mm.request_list));
3886 }
Eric Anholt673a3942008-07-30 12:06:12 -07003887
Eric Anholt28dfe522008-11-13 15:00:55 -08003888 /* Empty the active and flushing lists to inactive. If there's
3889 * anything left at this point, it means that we're wedged and
3890 * nothing good's going to happen by leaving them there. So strip
3891 * the GPU domains and just stuff them onto inactive.
Eric Anholt673a3942008-07-30 12:06:12 -07003892 */
Eric Anholt28dfe522008-11-13 15:00:55 -08003893 while (!list_empty(&dev_priv->mm.active_list)) {
3894 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07003895
Eric Anholt28dfe522008-11-13 15:00:55 -08003896 obj_priv = list_first_entry(&dev_priv->mm.active_list,
3897 struct drm_i915_gem_object,
3898 list);
3899 obj_priv->obj->write_domain &= ~I915_GEM_GPU_DOMAINS;
3900 i915_gem_object_move_to_inactive(obj_priv->obj);
3901 }
Carl Worth5e118f42009-03-20 11:54:25 -07003902 spin_unlock(&dev_priv->mm.active_list_lock);
Eric Anholt28dfe522008-11-13 15:00:55 -08003903
3904 while (!list_empty(&dev_priv->mm.flushing_list)) {
3905 struct drm_i915_gem_object *obj_priv;
3906
Eric Anholt151903d2008-12-01 10:23:21 +10003907 obj_priv = list_first_entry(&dev_priv->mm.flushing_list,
Eric Anholt28dfe522008-11-13 15:00:55 -08003908 struct drm_i915_gem_object,
3909 list);
3910 obj_priv->obj->write_domain &= ~I915_GEM_GPU_DOMAINS;
3911 i915_gem_object_move_to_inactive(obj_priv->obj);
3912 }
3913
3914
3915 /* Move all inactive buffers out of the GTT. */
Eric Anholt673a3942008-07-30 12:06:12 -07003916 ret = i915_gem_evict_from_list(dev, &dev_priv->mm.inactive_list);
Eric Anholt28dfe522008-11-13 15:00:55 -08003917 WARN_ON(!list_empty(&dev_priv->mm.inactive_list));
Keith Packard6dbe2772008-10-14 21:41:13 -07003918 if (ret) {
3919 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07003920 return ret;
Keith Packard6dbe2772008-10-14 21:41:13 -07003921 }
Eric Anholt673a3942008-07-30 12:06:12 -07003922
Keith Packard6dbe2772008-10-14 21:41:13 -07003923 i915_gem_cleanup_ringbuffer(dev);
3924 mutex_unlock(&dev->struct_mutex);
3925
Eric Anholt673a3942008-07-30 12:06:12 -07003926 return 0;
3927}
3928
3929static int
3930i915_gem_init_hws(struct drm_device *dev)
3931{
3932 drm_i915_private_t *dev_priv = dev->dev_private;
3933 struct drm_gem_object *obj;
3934 struct drm_i915_gem_object *obj_priv;
3935 int ret;
3936
3937 /* If we need a physical address for the status page, it's already
3938 * initialized at driver load time.
3939 */
3940 if (!I915_NEED_GFX_HWS(dev))
3941 return 0;
3942
3943 obj = drm_gem_object_alloc(dev, 4096);
3944 if (obj == NULL) {
3945 DRM_ERROR("Failed to allocate status page\n");
3946 return -ENOMEM;
3947 }
3948 obj_priv = obj->driver_private;
Keith Packardba1eb1d2008-10-14 19:55:10 -07003949 obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
Eric Anholt673a3942008-07-30 12:06:12 -07003950
3951 ret = i915_gem_object_pin(obj, 4096);
3952 if (ret != 0) {
3953 drm_gem_object_unreference(obj);
3954 return ret;
3955 }
3956
3957 dev_priv->status_gfx_addr = obj_priv->gtt_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003958
Eric Anholt856fa192009-03-19 14:10:50 -07003959 dev_priv->hw_status_page = kmap(obj_priv->pages[0]);
Keith Packardba1eb1d2008-10-14 19:55:10 -07003960 if (dev_priv->hw_status_page == NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -07003961 DRM_ERROR("Failed to map status page.\n");
3962 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
Chris Wilson3eb2ee72009-02-11 14:26:34 +00003963 i915_gem_object_unpin(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003964 drm_gem_object_unreference(obj);
3965 return -EINVAL;
3966 }
3967 dev_priv->hws_obj = obj;
Eric Anholt673a3942008-07-30 12:06:12 -07003968 memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
3969 I915_WRITE(HWS_PGA, dev_priv->status_gfx_addr);
Keith Packardba1eb1d2008-10-14 19:55:10 -07003970 I915_READ(HWS_PGA); /* posting read */
Eric Anholt673a3942008-07-30 12:06:12 -07003971 DRM_DEBUG("hws offset: 0x%08x\n", dev_priv->status_gfx_addr);
3972
3973 return 0;
3974}
3975
Chris Wilson85a7bb92009-02-11 14:52:44 +00003976static void
3977i915_gem_cleanup_hws(struct drm_device *dev)
3978{
3979 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonbab2d1f2009-02-20 17:52:20 +00003980 struct drm_gem_object *obj;
3981 struct drm_i915_gem_object *obj_priv;
Chris Wilson85a7bb92009-02-11 14:52:44 +00003982
3983 if (dev_priv->hws_obj == NULL)
3984 return;
3985
Chris Wilsonbab2d1f2009-02-20 17:52:20 +00003986 obj = dev_priv->hws_obj;
3987 obj_priv = obj->driver_private;
3988
Eric Anholt856fa192009-03-19 14:10:50 -07003989 kunmap(obj_priv->pages[0]);
Chris Wilson85a7bb92009-02-11 14:52:44 +00003990 i915_gem_object_unpin(obj);
3991 drm_gem_object_unreference(obj);
3992 dev_priv->hws_obj = NULL;
Chris Wilsonbab2d1f2009-02-20 17:52:20 +00003993
Chris Wilson85a7bb92009-02-11 14:52:44 +00003994 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
3995 dev_priv->hw_status_page = NULL;
3996
3997 /* Write high address into HWS_PGA when disabling. */
3998 I915_WRITE(HWS_PGA, 0x1ffff000);
3999}
4000
Jesse Barnes79e53942008-11-07 14:24:08 -08004001int
Eric Anholt673a3942008-07-30 12:06:12 -07004002i915_gem_init_ringbuffer(struct drm_device *dev)
4003{
4004 drm_i915_private_t *dev_priv = dev->dev_private;
4005 struct drm_gem_object *obj;
4006 struct drm_i915_gem_object *obj_priv;
Jesse Barnes79e53942008-11-07 14:24:08 -08004007 drm_i915_ring_buffer_t *ring = &dev_priv->ring;
Eric Anholt673a3942008-07-30 12:06:12 -07004008 int ret;
Keith Packard50aa253d2008-10-14 17:20:35 -07004009 u32 head;
Eric Anholt673a3942008-07-30 12:06:12 -07004010
4011 ret = i915_gem_init_hws(dev);
4012 if (ret != 0)
4013 return ret;
4014
4015 obj = drm_gem_object_alloc(dev, 128 * 1024);
4016 if (obj == NULL) {
4017 DRM_ERROR("Failed to allocate ringbuffer\n");
Chris Wilson85a7bb92009-02-11 14:52:44 +00004018 i915_gem_cleanup_hws(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004019 return -ENOMEM;
4020 }
4021 obj_priv = obj->driver_private;
4022
4023 ret = i915_gem_object_pin(obj, 4096);
4024 if (ret != 0) {
4025 drm_gem_object_unreference(obj);
Chris Wilson85a7bb92009-02-11 14:52:44 +00004026 i915_gem_cleanup_hws(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004027 return ret;
4028 }
4029
4030 /* Set up the kernel mapping for the ring. */
Jesse Barnes79e53942008-11-07 14:24:08 -08004031 ring->Size = obj->size;
4032 ring->tail_mask = obj->size - 1;
Eric Anholt673a3942008-07-30 12:06:12 -07004033
Jesse Barnes79e53942008-11-07 14:24:08 -08004034 ring->map.offset = dev->agp->base + obj_priv->gtt_offset;
4035 ring->map.size = obj->size;
4036 ring->map.type = 0;
4037 ring->map.flags = 0;
4038 ring->map.mtrr = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07004039
Jesse Barnes79e53942008-11-07 14:24:08 -08004040 drm_core_ioremap_wc(&ring->map, dev);
4041 if (ring->map.handle == NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -07004042 DRM_ERROR("Failed to map ringbuffer.\n");
4043 memset(&dev_priv->ring, 0, sizeof(dev_priv->ring));
Chris Wilson47ed1852009-02-11 14:26:33 +00004044 i915_gem_object_unpin(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004045 drm_gem_object_unreference(obj);
Chris Wilson85a7bb92009-02-11 14:52:44 +00004046 i915_gem_cleanup_hws(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004047 return -EINVAL;
4048 }
Jesse Barnes79e53942008-11-07 14:24:08 -08004049 ring->ring_obj = obj;
4050 ring->virtual_start = ring->map.handle;
Eric Anholt673a3942008-07-30 12:06:12 -07004051
4052 /* Stop the ring if it's running. */
4053 I915_WRITE(PRB0_CTL, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004054 I915_WRITE(PRB0_TAIL, 0);
Keith Packard50aa253d2008-10-14 17:20:35 -07004055 I915_WRITE(PRB0_HEAD, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004056
4057 /* Initialize the ring. */
4058 I915_WRITE(PRB0_START, obj_priv->gtt_offset);
Keith Packard50aa253d2008-10-14 17:20:35 -07004059 head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
4060
4061 /* G45 ring initialization fails to reset head to zero */
4062 if (head != 0) {
4063 DRM_ERROR("Ring head not reset to zero "
4064 "ctl %08x head %08x tail %08x start %08x\n",
4065 I915_READ(PRB0_CTL),
4066 I915_READ(PRB0_HEAD),
4067 I915_READ(PRB0_TAIL),
4068 I915_READ(PRB0_START));
4069 I915_WRITE(PRB0_HEAD, 0);
4070
4071 DRM_ERROR("Ring head forced to zero "
4072 "ctl %08x head %08x tail %08x start %08x\n",
4073 I915_READ(PRB0_CTL),
4074 I915_READ(PRB0_HEAD),
4075 I915_READ(PRB0_TAIL),
4076 I915_READ(PRB0_START));
4077 }
4078
Eric Anholt673a3942008-07-30 12:06:12 -07004079 I915_WRITE(PRB0_CTL,
4080 ((obj->size - 4096) & RING_NR_PAGES) |
4081 RING_NO_REPORT |
4082 RING_VALID);
4083
Keith Packard50aa253d2008-10-14 17:20:35 -07004084 head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
4085
4086 /* If the head is still not zero, the ring is dead */
4087 if (head != 0) {
4088 DRM_ERROR("Ring initialization failed "
4089 "ctl %08x head %08x tail %08x start %08x\n",
4090 I915_READ(PRB0_CTL),
4091 I915_READ(PRB0_HEAD),
4092 I915_READ(PRB0_TAIL),
4093 I915_READ(PRB0_START));
4094 return -EIO;
4095 }
4096
Eric Anholt673a3942008-07-30 12:06:12 -07004097 /* Update our cache of the ring state */
Jesse Barnes79e53942008-11-07 14:24:08 -08004098 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4099 i915_kernel_lost_context(dev);
4100 else {
4101 ring->head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
4102 ring->tail = I915_READ(PRB0_TAIL) & TAIL_ADDR;
4103 ring->space = ring->head - (ring->tail + 8);
4104 if (ring->space < 0)
4105 ring->space += ring->Size;
4106 }
Eric Anholt673a3942008-07-30 12:06:12 -07004107
4108 return 0;
4109}
4110
Jesse Barnes79e53942008-11-07 14:24:08 -08004111void
Eric Anholt673a3942008-07-30 12:06:12 -07004112i915_gem_cleanup_ringbuffer(struct drm_device *dev)
4113{
4114 drm_i915_private_t *dev_priv = dev->dev_private;
4115
4116 if (dev_priv->ring.ring_obj == NULL)
4117 return;
4118
4119 drm_core_ioremapfree(&dev_priv->ring.map, dev);
4120
4121 i915_gem_object_unpin(dev_priv->ring.ring_obj);
4122 drm_gem_object_unreference(dev_priv->ring.ring_obj);
4123 dev_priv->ring.ring_obj = NULL;
4124 memset(&dev_priv->ring, 0, sizeof(dev_priv->ring));
4125
Chris Wilson85a7bb92009-02-11 14:52:44 +00004126 i915_gem_cleanup_hws(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004127}
4128
4129int
4130i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
4131 struct drm_file *file_priv)
4132{
4133 drm_i915_private_t *dev_priv = dev->dev_private;
4134 int ret;
4135
Jesse Barnes79e53942008-11-07 14:24:08 -08004136 if (drm_core_check_feature(dev, DRIVER_MODESET))
4137 return 0;
4138
Eric Anholt673a3942008-07-30 12:06:12 -07004139 if (dev_priv->mm.wedged) {
4140 DRM_ERROR("Reenabling wedged hardware, good luck\n");
4141 dev_priv->mm.wedged = 0;
4142 }
4143
Eric Anholt673a3942008-07-30 12:06:12 -07004144 mutex_lock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004145 dev_priv->mm.suspended = 0;
4146
4147 ret = i915_gem_init_ringbuffer(dev);
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004148 if (ret != 0) {
4149 mutex_unlock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004150 return ret;
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004151 }
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004152
Carl Worth5e118f42009-03-20 11:54:25 -07004153 spin_lock(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07004154 BUG_ON(!list_empty(&dev_priv->mm.active_list));
Carl Worth5e118f42009-03-20 11:54:25 -07004155 spin_unlock(&dev_priv->mm.active_list_lock);
4156
Eric Anholt673a3942008-07-30 12:06:12 -07004157 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
4158 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
4159 BUG_ON(!list_empty(&dev_priv->mm.request_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004160 mutex_unlock(&dev->struct_mutex);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004161
4162 drm_irq_install(dev);
4163
Eric Anholt673a3942008-07-30 12:06:12 -07004164 return 0;
4165}
4166
4167int
4168i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
4169 struct drm_file *file_priv)
4170{
4171 int ret;
4172
Jesse Barnes79e53942008-11-07 14:24:08 -08004173 if (drm_core_check_feature(dev, DRIVER_MODESET))
4174 return 0;
4175
Eric Anholt673a3942008-07-30 12:06:12 -07004176 ret = i915_gem_idle(dev);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004177 drm_irq_uninstall(dev);
4178
Keith Packard6dbe2772008-10-14 21:41:13 -07004179 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004180}
4181
4182void
4183i915_gem_lastclose(struct drm_device *dev)
4184{
4185 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004186
Eric Anholte806b492009-01-22 09:56:58 -08004187 if (drm_core_check_feature(dev, DRIVER_MODESET))
4188 return;
4189
Keith Packard6dbe2772008-10-14 21:41:13 -07004190 ret = i915_gem_idle(dev);
4191 if (ret)
4192 DRM_ERROR("failed to idle hardware: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07004193}
4194
4195void
4196i915_gem_load(struct drm_device *dev)
4197{
4198 drm_i915_private_t *dev_priv = dev->dev_private;
4199
Carl Worth5e118f42009-03-20 11:54:25 -07004200 spin_lock_init(&dev_priv->mm.active_list_lock);
Eric Anholt673a3942008-07-30 12:06:12 -07004201 INIT_LIST_HEAD(&dev_priv->mm.active_list);
4202 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
4203 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
4204 INIT_LIST_HEAD(&dev_priv->mm.request_list);
4205 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
4206 i915_gem_retire_work_handler);
Eric Anholt673a3942008-07-30 12:06:12 -07004207 dev_priv->mm.next_gem_seqno = 1;
4208
Jesse Barnesde151cf2008-11-12 10:03:55 -08004209 /* Old X drivers will take 0-2 for front, back, depth buffers */
4210 dev_priv->fence_reg_start = 3;
4211
Jesse Barnes0f973f22009-01-26 17:10:45 -08004212 if (IS_I965G(dev) || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08004213 dev_priv->num_fence_regs = 16;
4214 else
4215 dev_priv->num_fence_regs = 8;
4216
Eric Anholt673a3942008-07-30 12:06:12 -07004217 i915_gem_detect_bit_6_swizzle(dev);
4218}
Dave Airlie71acb5e2008-12-30 20:31:46 +10004219
4220/*
4221 * Create a physically contiguous memory object for this object
4222 * e.g. for cursor + overlay regs
4223 */
4224int i915_gem_init_phys_object(struct drm_device *dev,
4225 int id, int size)
4226{
4227 drm_i915_private_t *dev_priv = dev->dev_private;
4228 struct drm_i915_gem_phys_object *phys_obj;
4229 int ret;
4230
4231 if (dev_priv->mm.phys_objs[id - 1] || !size)
4232 return 0;
4233
4234 phys_obj = drm_calloc(1, sizeof(struct drm_i915_gem_phys_object), DRM_MEM_DRIVER);
4235 if (!phys_obj)
4236 return -ENOMEM;
4237
4238 phys_obj->id = id;
4239
4240 phys_obj->handle = drm_pci_alloc(dev, size, 0, 0xffffffff);
4241 if (!phys_obj->handle) {
4242 ret = -ENOMEM;
4243 goto kfree_obj;
4244 }
4245#ifdef CONFIG_X86
4246 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4247#endif
4248
4249 dev_priv->mm.phys_objs[id - 1] = phys_obj;
4250
4251 return 0;
4252kfree_obj:
4253 drm_free(phys_obj, sizeof(struct drm_i915_gem_phys_object), DRM_MEM_DRIVER);
4254 return ret;
4255}
4256
4257void i915_gem_free_phys_object(struct drm_device *dev, int id)
4258{
4259 drm_i915_private_t *dev_priv = dev->dev_private;
4260 struct drm_i915_gem_phys_object *phys_obj;
4261
4262 if (!dev_priv->mm.phys_objs[id - 1])
4263 return;
4264
4265 phys_obj = dev_priv->mm.phys_objs[id - 1];
4266 if (phys_obj->cur_obj) {
4267 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
4268 }
4269
4270#ifdef CONFIG_X86
4271 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4272#endif
4273 drm_pci_free(dev, phys_obj->handle);
4274 kfree(phys_obj);
4275 dev_priv->mm.phys_objs[id - 1] = NULL;
4276}
4277
4278void i915_gem_free_all_phys_object(struct drm_device *dev)
4279{
4280 int i;
4281
Dave Airlie260883c2009-01-22 17:58:49 +10004282 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004283 i915_gem_free_phys_object(dev, i);
4284}
4285
4286void i915_gem_detach_phys_object(struct drm_device *dev,
4287 struct drm_gem_object *obj)
4288{
4289 struct drm_i915_gem_object *obj_priv;
4290 int i;
4291 int ret;
4292 int page_count;
4293
4294 obj_priv = obj->driver_private;
4295 if (!obj_priv->phys_obj)
4296 return;
4297
Eric Anholt856fa192009-03-19 14:10:50 -07004298 ret = i915_gem_object_get_pages(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004299 if (ret)
4300 goto out;
4301
4302 page_count = obj->size / PAGE_SIZE;
4303
4304 for (i = 0; i < page_count; i++) {
Eric Anholt856fa192009-03-19 14:10:50 -07004305 char *dst = kmap_atomic(obj_priv->pages[i], KM_USER0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004306 char *src = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4307
4308 memcpy(dst, src, PAGE_SIZE);
4309 kunmap_atomic(dst, KM_USER0);
4310 }
Eric Anholt856fa192009-03-19 14:10:50 -07004311 drm_clflush_pages(obj_priv->pages, page_count);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004312 drm_agp_chipset_flush(dev);
Chris Wilsond78b47b2009-06-17 21:52:49 +01004313
4314 i915_gem_object_put_pages(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004315out:
4316 obj_priv->phys_obj->cur_obj = NULL;
4317 obj_priv->phys_obj = NULL;
4318}
4319
4320int
4321i915_gem_attach_phys_object(struct drm_device *dev,
4322 struct drm_gem_object *obj, int id)
4323{
4324 drm_i915_private_t *dev_priv = dev->dev_private;
4325 struct drm_i915_gem_object *obj_priv;
4326 int ret = 0;
4327 int page_count;
4328 int i;
4329
4330 if (id > I915_MAX_PHYS_OBJECT)
4331 return -EINVAL;
4332
4333 obj_priv = obj->driver_private;
4334
4335 if (obj_priv->phys_obj) {
4336 if (obj_priv->phys_obj->id == id)
4337 return 0;
4338 i915_gem_detach_phys_object(dev, obj);
4339 }
4340
4341
4342 /* create a new object */
4343 if (!dev_priv->mm.phys_objs[id - 1]) {
4344 ret = i915_gem_init_phys_object(dev, id,
4345 obj->size);
4346 if (ret) {
Linus Torvaldsaeb565d2009-01-26 10:01:53 -08004347 DRM_ERROR("failed to init phys object %d size: %zu\n", id, obj->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004348 goto out;
4349 }
4350 }
4351
4352 /* bind to the object */
4353 obj_priv->phys_obj = dev_priv->mm.phys_objs[id - 1];
4354 obj_priv->phys_obj->cur_obj = obj;
4355
Eric Anholt856fa192009-03-19 14:10:50 -07004356 ret = i915_gem_object_get_pages(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004357 if (ret) {
4358 DRM_ERROR("failed to get page list\n");
4359 goto out;
4360 }
4361
4362 page_count = obj->size / PAGE_SIZE;
4363
4364 for (i = 0; i < page_count; i++) {
Eric Anholt856fa192009-03-19 14:10:50 -07004365 char *src = kmap_atomic(obj_priv->pages[i], KM_USER0);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004366 char *dst = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4367
4368 memcpy(dst, src, PAGE_SIZE);
4369 kunmap_atomic(src, KM_USER0);
4370 }
4371
Chris Wilsond78b47b2009-06-17 21:52:49 +01004372 i915_gem_object_put_pages(obj);
4373
Dave Airlie71acb5e2008-12-30 20:31:46 +10004374 return 0;
4375out:
4376 return ret;
4377}
4378
4379static int
4380i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
4381 struct drm_i915_gem_pwrite *args,
4382 struct drm_file *file_priv)
4383{
4384 struct drm_i915_gem_object *obj_priv = obj->driver_private;
4385 void *obj_addr;
4386 int ret;
4387 char __user *user_data;
4388
4389 user_data = (char __user *) (uintptr_t) args->data_ptr;
4390 obj_addr = obj_priv->phys_obj->handle->vaddr + args->offset;
4391
Dave Airliee08fb4f2009-02-25 14:52:30 +10004392 DRM_DEBUG("obj_addr %p, %lld\n", obj_addr, args->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004393 ret = copy_from_user(obj_addr, user_data, args->size);
4394 if (ret)
4395 return -EFAULT;
4396
4397 drm_agp_chipset_flush(dev);
4398 return 0;
4399}
Eric Anholtb9624422009-06-03 07:27:35 +00004400
4401void i915_gem_release(struct drm_device * dev, struct drm_file *file_priv)
4402{
4403 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
4404
4405 /* Clean up our request list when the client is going away, so that
4406 * later retire_requests won't dereference our soon-to-be-gone
4407 * file_priv.
4408 */
4409 mutex_lock(&dev->struct_mutex);
4410 while (!list_empty(&i915_file_priv->mm.request_list))
4411 list_del_init(i915_file_priv->mm.request_list.next);
4412 mutex_unlock(&dev->struct_mutex);
4413}