blob: 6a9e3a8750836bcd2dcb9bf1c707baea308abf93 [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
Keith Packardc0d90822008-11-20 23:11:08 -080037static void
38i915_gem_object_set_to_gpu_domain(struct drm_gem_object *obj,
39 uint32_t read_domains,
40 uint32_t write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -080041static void i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj);
42static void i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj);
43static void i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj);
Eric Anholte47c68e2008-11-14 13:35:19 -080044static int i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj,
45 int write);
46static int i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
47 uint64_t offset,
48 uint64_t size);
49static void i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj);
Eric Anholt673a3942008-07-30 12:06:12 -070050static int i915_gem_object_get_page_list(struct drm_gem_object *obj);
51static void i915_gem_object_free_page_list(struct drm_gem_object *obj);
52static int i915_gem_object_wait_rendering(struct drm_gem_object *obj);
Jesse Barnesde151cf2008-11-12 10:03:55 -080053static int i915_gem_object_bind_to_gtt(struct drm_gem_object *obj,
54 unsigned alignment);
Jesse Barnes0f973f22009-01-26 17:10:45 -080055static int i915_gem_object_get_fence_reg(struct drm_gem_object *obj, bool write);
Jesse Barnesde151cf2008-11-12 10:03:55 -080056static void i915_gem_clear_fence_reg(struct drm_gem_object *obj);
57static int i915_gem_evict_something(struct drm_device *dev);
Dave Airlie71acb5e2008-12-30 20:31:46 +100058static int i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
59 struct drm_i915_gem_pwrite *args,
60 struct drm_file *file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -070061
Jesse Barnes79e53942008-11-07 14:24:08 -080062int i915_gem_do_init(struct drm_device *dev, unsigned long start,
63 unsigned long end)
64{
65 drm_i915_private_t *dev_priv = dev->dev_private;
66
67 if (start >= end ||
68 (start & (PAGE_SIZE - 1)) != 0 ||
69 (end & (PAGE_SIZE - 1)) != 0) {
70 return -EINVAL;
71 }
72
73 drm_mm_init(&dev_priv->mm.gtt_space, start,
74 end - start);
75
76 dev->gtt_total = (uint32_t) (end - start);
77
78 return 0;
79}
Keith Packard6dbe2772008-10-14 21:41:13 -070080
Eric Anholt673a3942008-07-30 12:06:12 -070081int
82i915_gem_init_ioctl(struct drm_device *dev, void *data,
83 struct drm_file *file_priv)
84{
Eric Anholt673a3942008-07-30 12:06:12 -070085 struct drm_i915_gem_init *args = data;
Jesse Barnes79e53942008-11-07 14:24:08 -080086 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -070087
88 mutex_lock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080089 ret = i915_gem_do_init(dev, args->gtt_start, args->gtt_end);
Eric Anholt673a3942008-07-30 12:06:12 -070090 mutex_unlock(&dev->struct_mutex);
91
Jesse Barnes79e53942008-11-07 14:24:08 -080092 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -070093}
94
Eric Anholt5a125c32008-10-22 21:40:13 -070095int
96i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
97 struct drm_file *file_priv)
98{
Eric Anholt5a125c32008-10-22 21:40:13 -070099 struct drm_i915_gem_get_aperture *args = data;
Eric Anholt5a125c32008-10-22 21:40:13 -0700100
101 if (!(dev->driver->driver_features & DRIVER_GEM))
102 return -ENODEV;
103
104 args->aper_size = dev->gtt_total;
Keith Packard2678d9d2008-11-20 22:54:54 -0800105 args->aper_available_size = (args->aper_size -
106 atomic_read(&dev->pin_memory));
Eric Anholt5a125c32008-10-22 21:40:13 -0700107
108 return 0;
109}
110
Eric Anholt673a3942008-07-30 12:06:12 -0700111
112/**
113 * Creates a new mm object and returns a handle to it.
114 */
115int
116i915_gem_create_ioctl(struct drm_device *dev, void *data,
117 struct drm_file *file_priv)
118{
119 struct drm_i915_gem_create *args = data;
120 struct drm_gem_object *obj;
121 int handle, ret;
122
123 args->size = roundup(args->size, PAGE_SIZE);
124
125 /* Allocate the new object */
126 obj = drm_gem_object_alloc(dev, args->size);
127 if (obj == NULL)
128 return -ENOMEM;
129
130 ret = drm_gem_handle_create(file_priv, obj, &handle);
131 mutex_lock(&dev->struct_mutex);
132 drm_gem_object_handle_unreference(obj);
133 mutex_unlock(&dev->struct_mutex);
134
135 if (ret)
136 return ret;
137
138 args->handle = handle;
139
140 return 0;
141}
142
143/**
144 * Reads data from the object referenced by handle.
145 *
146 * On error, the contents of *data are undefined.
147 */
148int
149i915_gem_pread_ioctl(struct drm_device *dev, void *data,
150 struct drm_file *file_priv)
151{
152 struct drm_i915_gem_pread *args = data;
153 struct drm_gem_object *obj;
154 struct drm_i915_gem_object *obj_priv;
155 ssize_t read;
156 loff_t offset;
157 int ret;
158
159 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
160 if (obj == NULL)
161 return -EBADF;
162 obj_priv = obj->driver_private;
163
164 /* Bounds check source.
165 *
166 * XXX: This could use review for overflow issues...
167 */
168 if (args->offset > obj->size || args->size > obj->size ||
169 args->offset + args->size > obj->size) {
170 drm_gem_object_unreference(obj);
171 return -EINVAL;
172 }
173
174 mutex_lock(&dev->struct_mutex);
175
Eric Anholte47c68e2008-11-14 13:35:19 -0800176 ret = i915_gem_object_set_cpu_read_domain_range(obj, args->offset,
177 args->size);
Eric Anholt673a3942008-07-30 12:06:12 -0700178 if (ret != 0) {
179 drm_gem_object_unreference(obj);
180 mutex_unlock(&dev->struct_mutex);
Dave Airliee7d22bc2008-10-07 13:40:36 +1000181 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700182 }
183
184 offset = args->offset;
185
186 read = vfs_read(obj->filp, (char __user *)(uintptr_t)args->data_ptr,
187 args->size, &offset);
188 if (read != args->size) {
189 drm_gem_object_unreference(obj);
190 mutex_unlock(&dev->struct_mutex);
191 if (read < 0)
192 return read;
193 else
194 return -EINVAL;
195 }
196
197 drm_gem_object_unreference(obj);
198 mutex_unlock(&dev->struct_mutex);
199
200 return 0;
201}
202
Keith Packard0839ccb2008-10-30 19:38:48 -0700203/* This is the fast write path which cannot handle
204 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700205 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700206
Keith Packard0839ccb2008-10-30 19:38:48 -0700207static inline int
208fast_user_write(struct io_mapping *mapping,
209 loff_t page_base, int page_offset,
210 char __user *user_data,
211 int length)
212{
213 char *vaddr_atomic;
214 unsigned long unwritten;
215
216 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
217 unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
218 user_data, length);
219 io_mapping_unmap_atomic(vaddr_atomic);
220 if (unwritten)
221 return -EFAULT;
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700222 return 0;
Keith Packard0839ccb2008-10-30 19:38:48 -0700223}
224
225/* Here's the write path which can sleep for
226 * page faults
227 */
228
229static inline int
230slow_user_write(struct io_mapping *mapping,
231 loff_t page_base, int page_offset,
232 char __user *user_data,
233 int length)
234{
235 char __iomem *vaddr;
236 unsigned long unwritten;
237
238 vaddr = io_mapping_map_wc(mapping, page_base);
239 if (vaddr == NULL)
240 return -EFAULT;
241 unwritten = __copy_from_user(vaddr + page_offset,
242 user_data, length);
243 io_mapping_unmap(vaddr);
244 if (unwritten)
245 return -EFAULT;
246 return 0;
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700247}
248
Eric Anholt673a3942008-07-30 12:06:12 -0700249static int
250i915_gem_gtt_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
251 struct drm_i915_gem_pwrite *args,
252 struct drm_file *file_priv)
253{
254 struct drm_i915_gem_object *obj_priv = obj->driver_private;
Keith Packard0839ccb2008-10-30 19:38:48 -0700255 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700256 ssize_t remain;
Keith Packard0839ccb2008-10-30 19:38:48 -0700257 loff_t offset, page_base;
Eric Anholt673a3942008-07-30 12:06:12 -0700258 char __user *user_data;
Keith Packard0839ccb2008-10-30 19:38:48 -0700259 int page_offset, page_length;
260 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700261
262 user_data = (char __user *) (uintptr_t) args->data_ptr;
263 remain = args->size;
264 if (!access_ok(VERIFY_READ, user_data, remain))
265 return -EFAULT;
266
267
268 mutex_lock(&dev->struct_mutex);
269 ret = i915_gem_object_pin(obj, 0);
270 if (ret) {
271 mutex_unlock(&dev->struct_mutex);
272 return ret;
273 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800274 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
Eric Anholt673a3942008-07-30 12:06:12 -0700275 if (ret)
276 goto fail;
277
278 obj_priv = obj->driver_private;
279 offset = obj_priv->gtt_offset + args->offset;
280 obj_priv->dirty = 1;
281
282 while (remain > 0) {
283 /* Operation in this page
284 *
Keith Packard0839ccb2008-10-30 19:38:48 -0700285 * page_base = page offset within aperture
286 * page_offset = offset within page
287 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -0700288 */
Keith Packard0839ccb2008-10-30 19:38:48 -0700289 page_base = (offset & ~(PAGE_SIZE-1));
290 page_offset = offset & (PAGE_SIZE-1);
291 page_length = remain;
292 if ((page_offset + remain) > PAGE_SIZE)
293 page_length = PAGE_SIZE - page_offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700294
Keith Packard0839ccb2008-10-30 19:38:48 -0700295 ret = fast_user_write (dev_priv->mm.gtt_mapping, page_base,
296 page_offset, user_data, page_length);
Eric Anholt673a3942008-07-30 12:06:12 -0700297
Keith Packard0839ccb2008-10-30 19:38:48 -0700298 /* If we get a fault while copying data, then (presumably) our
299 * source page isn't available. In this case, use the
300 * non-atomic function
301 */
302 if (ret) {
303 ret = slow_user_write (dev_priv->mm.gtt_mapping,
304 page_base, page_offset,
305 user_data, page_length);
306 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -0700307 goto fail;
Eric Anholt673a3942008-07-30 12:06:12 -0700308 }
309
Keith Packard0839ccb2008-10-30 19:38:48 -0700310 remain -= page_length;
311 user_data += page_length;
312 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700313 }
Eric Anholt673a3942008-07-30 12:06:12 -0700314
315fail:
316 i915_gem_object_unpin(obj);
317 mutex_unlock(&dev->struct_mutex);
318
319 return ret;
320}
321
Eric Anholt3043c602008-10-02 12:24:47 -0700322static int
Eric Anholt673a3942008-07-30 12:06:12 -0700323i915_gem_shmem_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
324 struct drm_i915_gem_pwrite *args,
325 struct drm_file *file_priv)
326{
327 int ret;
328 loff_t offset;
329 ssize_t written;
330
331 mutex_lock(&dev->struct_mutex);
332
Eric Anholte47c68e2008-11-14 13:35:19 -0800333 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Eric Anholt673a3942008-07-30 12:06:12 -0700334 if (ret) {
335 mutex_unlock(&dev->struct_mutex);
336 return ret;
337 }
338
339 offset = args->offset;
340
341 written = vfs_write(obj->filp,
342 (char __user *)(uintptr_t) args->data_ptr,
343 args->size, &offset);
344 if (written != args->size) {
345 mutex_unlock(&dev->struct_mutex);
346 if (written < 0)
347 return written;
348 else
349 return -EINVAL;
350 }
351
352 mutex_unlock(&dev->struct_mutex);
353
354 return 0;
355}
356
357/**
358 * Writes data to the object referenced by handle.
359 *
360 * On error, the contents of the buffer that were to be modified are undefined.
361 */
362int
363i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
364 struct drm_file *file_priv)
365{
366 struct drm_i915_gem_pwrite *args = data;
367 struct drm_gem_object *obj;
368 struct drm_i915_gem_object *obj_priv;
369 int ret = 0;
370
371 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
372 if (obj == NULL)
373 return -EBADF;
374 obj_priv = obj->driver_private;
375
376 /* Bounds check destination.
377 *
378 * XXX: This could use review for overflow issues...
379 */
380 if (args->offset > obj->size || args->size > obj->size ||
381 args->offset + args->size > obj->size) {
382 drm_gem_object_unreference(obj);
383 return -EINVAL;
384 }
385
386 /* We can only do the GTT pwrite on untiled buffers, as otherwise
387 * it would end up going through the fenced access, and we'll get
388 * different detiling behavior between reading and writing.
389 * pread/pwrite currently are reading and writing from the CPU
390 * perspective, requiring manual detiling by the client.
391 */
Dave Airlie71acb5e2008-12-30 20:31:46 +1000392 if (obj_priv->phys_obj)
393 ret = i915_gem_phys_pwrite(dev, obj, args, file_priv);
394 else if (obj_priv->tiling_mode == I915_TILING_NONE &&
395 dev->gtt_total != 0)
Eric Anholt673a3942008-07-30 12:06:12 -0700396 ret = i915_gem_gtt_pwrite(dev, obj, args, file_priv);
397 else
398 ret = i915_gem_shmem_pwrite(dev, obj, args, file_priv);
399
400#if WATCH_PWRITE
401 if (ret)
402 DRM_INFO("pwrite failed %d\n", ret);
403#endif
404
405 drm_gem_object_unreference(obj);
406
407 return ret;
408}
409
410/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800411 * Called when user space prepares to use an object with the CPU, either
412 * through the mmap ioctl's mapping or a GTT mapping.
Eric Anholt673a3942008-07-30 12:06:12 -0700413 */
414int
415i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
416 struct drm_file *file_priv)
417{
418 struct drm_i915_gem_set_domain *args = data;
419 struct drm_gem_object *obj;
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800420 uint32_t read_domains = args->read_domains;
421 uint32_t write_domain = args->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -0700422 int ret;
423
424 if (!(dev->driver->driver_features & DRIVER_GEM))
425 return -ENODEV;
426
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800427 /* Only handle setting domains to types used by the CPU. */
428 if (write_domain & ~(I915_GEM_DOMAIN_CPU | I915_GEM_DOMAIN_GTT))
429 return -EINVAL;
430
431 if (read_domains & ~(I915_GEM_DOMAIN_CPU | I915_GEM_DOMAIN_GTT))
432 return -EINVAL;
433
434 /* Having something in the write domain implies it's in the read
435 * domain, and only that read domain. Enforce that in the request.
436 */
437 if (write_domain != 0 && read_domains != write_domain)
438 return -EINVAL;
439
Eric Anholt673a3942008-07-30 12:06:12 -0700440 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
441 if (obj == NULL)
442 return -EBADF;
443
444 mutex_lock(&dev->struct_mutex);
445#if WATCH_BUF
446 DRM_INFO("set_domain_ioctl %p(%d), %08x %08x\n",
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800447 obj, obj->size, read_domains, write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -0700448#endif
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800449 if (read_domains & I915_GEM_DOMAIN_GTT) {
450 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
Eric Anholt02354392008-11-26 13:58:13 -0800451
452 /* Silently promote "you're not bound, there was nothing to do"
453 * to success, since the client was just asking us to
454 * make sure everything was done.
455 */
456 if (ret == -EINVAL)
457 ret = 0;
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800458 } else {
Eric Anholte47c68e2008-11-14 13:35:19 -0800459 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
Eric Anholt2ef7eea2008-11-10 10:53:25 -0800460 }
461
Eric Anholt673a3942008-07-30 12:06:12 -0700462 drm_gem_object_unreference(obj);
463 mutex_unlock(&dev->struct_mutex);
464 return ret;
465}
466
467/**
468 * Called when user space has done writes to this buffer
469 */
470int
471i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
472 struct drm_file *file_priv)
473{
474 struct drm_i915_gem_sw_finish *args = data;
475 struct drm_gem_object *obj;
476 struct drm_i915_gem_object *obj_priv;
477 int ret = 0;
478
479 if (!(dev->driver->driver_features & DRIVER_GEM))
480 return -ENODEV;
481
482 mutex_lock(&dev->struct_mutex);
483 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
484 if (obj == NULL) {
485 mutex_unlock(&dev->struct_mutex);
486 return -EBADF;
487 }
488
489#if WATCH_BUF
490 DRM_INFO("%s: sw_finish %d (%p %d)\n",
491 __func__, args->handle, obj, obj->size);
492#endif
493 obj_priv = obj->driver_private;
494
495 /* Pinned buffers may be scanout, so flush the cache */
Eric Anholte47c68e2008-11-14 13:35:19 -0800496 if (obj_priv->pin_count)
497 i915_gem_object_flush_cpu_write_domain(obj);
498
Eric Anholt673a3942008-07-30 12:06:12 -0700499 drm_gem_object_unreference(obj);
500 mutex_unlock(&dev->struct_mutex);
501 return ret;
502}
503
504/**
505 * Maps the contents of an object, returning the address it is mapped
506 * into.
507 *
508 * While the mapping holds a reference on the contents of the object, it doesn't
509 * imply a ref on the object itself.
510 */
511int
512i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
513 struct drm_file *file_priv)
514{
515 struct drm_i915_gem_mmap *args = data;
516 struct drm_gem_object *obj;
517 loff_t offset;
518 unsigned long addr;
519
520 if (!(dev->driver->driver_features & DRIVER_GEM))
521 return -ENODEV;
522
523 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
524 if (obj == NULL)
525 return -EBADF;
526
527 offset = args->offset;
528
529 down_write(&current->mm->mmap_sem);
530 addr = do_mmap(obj->filp, 0, args->size,
531 PROT_READ | PROT_WRITE, MAP_SHARED,
532 args->offset);
533 up_write(&current->mm->mmap_sem);
534 mutex_lock(&dev->struct_mutex);
535 drm_gem_object_unreference(obj);
536 mutex_unlock(&dev->struct_mutex);
537 if (IS_ERR((void *)addr))
538 return addr;
539
540 args->addr_ptr = (uint64_t) addr;
541
542 return 0;
543}
544
Jesse Barnesde151cf2008-11-12 10:03:55 -0800545/**
546 * i915_gem_fault - fault a page into the GTT
547 * vma: VMA in question
548 * vmf: fault info
549 *
550 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
551 * from userspace. The fault handler takes care of binding the object to
552 * the GTT (if needed), allocating and programming a fence register (again,
553 * only if needed based on whether the old reg is still valid or the object
554 * is tiled) and inserting a new PTE into the faulting process.
555 *
556 * Note that the faulting process may involve evicting existing objects
557 * from the GTT and/or fence registers to make room. So performance may
558 * suffer if the GTT working set is large or there are few fence registers
559 * left.
560 */
561int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
562{
563 struct drm_gem_object *obj = vma->vm_private_data;
564 struct drm_device *dev = obj->dev;
565 struct drm_i915_private *dev_priv = dev->dev_private;
566 struct drm_i915_gem_object *obj_priv = obj->driver_private;
567 pgoff_t page_offset;
568 unsigned long pfn;
569 int ret = 0;
Jesse Barnes0f973f22009-01-26 17:10:45 -0800570 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Jesse Barnesde151cf2008-11-12 10:03:55 -0800571
572 /* We don't use vmf->pgoff since that has the fake offset */
573 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
574 PAGE_SHIFT;
575
576 /* Now bind it into the GTT if needed */
577 mutex_lock(&dev->struct_mutex);
578 if (!obj_priv->gtt_space) {
579 ret = i915_gem_object_bind_to_gtt(obj, obj_priv->gtt_alignment);
580 if (ret) {
581 mutex_unlock(&dev->struct_mutex);
582 return VM_FAULT_SIGBUS;
583 }
584 list_add(&obj_priv->list, &dev_priv->mm.inactive_list);
585 }
586
587 /* Need a new fence register? */
588 if (obj_priv->fence_reg == I915_FENCE_REG_NONE &&
Eric Anholtd9ddcb92009-01-27 10:33:49 -0800589 obj_priv->tiling_mode != I915_TILING_NONE) {
Jesse Barnes0f973f22009-01-26 17:10:45 -0800590 ret = i915_gem_object_get_fence_reg(obj, write);
Eric Anholtd9ddcb92009-01-27 10:33:49 -0800591 if (ret != 0)
592 return VM_FAULT_SIGBUS;
593 }
Jesse Barnesde151cf2008-11-12 10:03:55 -0800594
595 pfn = ((dev->agp->base + obj_priv->gtt_offset) >> PAGE_SHIFT) +
596 page_offset;
597
598 /* Finally, remap it using the new GTT offset */
599 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
600
601 mutex_unlock(&dev->struct_mutex);
602
603 switch (ret) {
604 case -ENOMEM:
605 case -EAGAIN:
606 return VM_FAULT_OOM;
607 case -EFAULT:
608 case -EBUSY:
609 DRM_ERROR("can't insert pfn?? fault or busy...\n");
610 return VM_FAULT_SIGBUS;
611 default:
612 return VM_FAULT_NOPAGE;
613 }
614}
615
616/**
617 * i915_gem_create_mmap_offset - create a fake mmap offset for an object
618 * @obj: obj in question
619 *
620 * GEM memory mapping works by handing back to userspace a fake mmap offset
621 * it can use in a subsequent mmap(2) call. The DRM core code then looks
622 * up the object based on the offset and sets up the various memory mapping
623 * structures.
624 *
625 * This routine allocates and attaches a fake offset for @obj.
626 */
627static int
628i915_gem_create_mmap_offset(struct drm_gem_object *obj)
629{
630 struct drm_device *dev = obj->dev;
631 struct drm_gem_mm *mm = dev->mm_private;
632 struct drm_i915_gem_object *obj_priv = obj->driver_private;
633 struct drm_map_list *list;
634 struct drm_map *map;
635 int ret = 0;
636
637 /* Set the object up for mmap'ing */
638 list = &obj->map_list;
639 list->map = drm_calloc(1, sizeof(struct drm_map_list),
640 DRM_MEM_DRIVER);
641 if (!list->map)
642 return -ENOMEM;
643
644 map = list->map;
645 map->type = _DRM_GEM;
646 map->size = obj->size;
647 map->handle = obj;
648
649 /* Get a DRM GEM mmap offset allocated... */
650 list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
651 obj->size / PAGE_SIZE, 0, 0);
652 if (!list->file_offset_node) {
653 DRM_ERROR("failed to allocate offset for bo %d\n", obj->name);
654 ret = -ENOMEM;
655 goto out_free_list;
656 }
657
658 list->file_offset_node = drm_mm_get_block(list->file_offset_node,
659 obj->size / PAGE_SIZE, 0);
660 if (!list->file_offset_node) {
661 ret = -ENOMEM;
662 goto out_free_list;
663 }
664
665 list->hash.key = list->file_offset_node->start;
666 if (drm_ht_insert_item(&mm->offset_hash, &list->hash)) {
667 DRM_ERROR("failed to add to map hash\n");
668 goto out_free_mm;
669 }
670
671 /* By now we should be all set, any drm_mmap request on the offset
672 * below will get to our mmap & fault handler */
673 obj_priv->mmap_offset = ((uint64_t) list->hash.key) << PAGE_SHIFT;
674
675 return 0;
676
677out_free_mm:
678 drm_mm_put_block(list->file_offset_node);
679out_free_list:
680 drm_free(list->map, sizeof(struct drm_map_list), DRM_MEM_DRIVER);
681
682 return ret;
683}
684
685/**
686 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
687 * @obj: object to check
688 *
689 * Return the required GTT alignment for an object, taking into account
690 * potential fence register mapping if needed.
691 */
692static uint32_t
693i915_gem_get_gtt_alignment(struct drm_gem_object *obj)
694{
695 struct drm_device *dev = obj->dev;
696 struct drm_i915_gem_object *obj_priv = obj->driver_private;
697 int start, i;
698
699 /*
700 * Minimum alignment is 4k (GTT page size), but might be greater
701 * if a fence register is needed for the object.
702 */
703 if (IS_I965G(dev) || obj_priv->tiling_mode == I915_TILING_NONE)
704 return 4096;
705
706 /*
707 * Previous chips need to be aligned to the size of the smallest
708 * fence register that can contain the object.
709 */
710 if (IS_I9XX(dev))
711 start = 1024*1024;
712 else
713 start = 512*1024;
714
715 for (i = start; i < obj->size; i <<= 1)
716 ;
717
718 return i;
719}
720
721/**
722 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
723 * @dev: DRM device
724 * @data: GTT mapping ioctl data
725 * @file_priv: GEM object info
726 *
727 * Simply returns the fake offset to userspace so it can mmap it.
728 * The mmap call will end up in drm_gem_mmap(), which will set things
729 * up so we can get faults in the handler above.
730 *
731 * The fault handler will take care of binding the object into the GTT
732 * (since it may have been evicted to make room for something), allocating
733 * a fence register, and mapping the appropriate aperture address into
734 * userspace.
735 */
736int
737i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
738 struct drm_file *file_priv)
739{
740 struct drm_i915_gem_mmap_gtt *args = data;
741 struct drm_i915_private *dev_priv = dev->dev_private;
742 struct drm_gem_object *obj;
743 struct drm_i915_gem_object *obj_priv;
744 int ret;
745
746 if (!(dev->driver->driver_features & DRIVER_GEM))
747 return -ENODEV;
748
749 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
750 if (obj == NULL)
751 return -EBADF;
752
753 mutex_lock(&dev->struct_mutex);
754
755 obj_priv = obj->driver_private;
756
757 if (!obj_priv->mmap_offset) {
758 ret = i915_gem_create_mmap_offset(obj);
759 if (ret)
760 return ret;
761 }
762
763 args->offset = obj_priv->mmap_offset;
764
765 obj_priv->gtt_alignment = i915_gem_get_gtt_alignment(obj);
766
767 /* Make sure the alignment is correct for fence regs etc */
768 if (obj_priv->agp_mem &&
769 (obj_priv->gtt_offset & (obj_priv->gtt_alignment - 1))) {
770 drm_gem_object_unreference(obj);
771 mutex_unlock(&dev->struct_mutex);
772 return -EINVAL;
773 }
774
775 /*
776 * Pull it into the GTT so that we have a page list (makes the
777 * initial fault faster and any subsequent flushing possible).
778 */
779 if (!obj_priv->agp_mem) {
780 ret = i915_gem_object_bind_to_gtt(obj, obj_priv->gtt_alignment);
781 if (ret) {
782 drm_gem_object_unreference(obj);
783 mutex_unlock(&dev->struct_mutex);
784 return ret;
785 }
786 list_add(&obj_priv->list, &dev_priv->mm.inactive_list);
787 }
788
789 drm_gem_object_unreference(obj);
790 mutex_unlock(&dev->struct_mutex);
791
792 return 0;
793}
794
Eric Anholt673a3942008-07-30 12:06:12 -0700795static void
796i915_gem_object_free_page_list(struct drm_gem_object *obj)
797{
798 struct drm_i915_gem_object *obj_priv = obj->driver_private;
799 int page_count = obj->size / PAGE_SIZE;
800 int i;
801
802 if (obj_priv->page_list == NULL)
803 return;
804
805
806 for (i = 0; i < page_count; i++)
807 if (obj_priv->page_list[i] != NULL) {
808 if (obj_priv->dirty)
809 set_page_dirty(obj_priv->page_list[i]);
810 mark_page_accessed(obj_priv->page_list[i]);
811 page_cache_release(obj_priv->page_list[i]);
812 }
813 obj_priv->dirty = 0;
814
815 drm_free(obj_priv->page_list,
816 page_count * sizeof(struct page *),
817 DRM_MEM_DRIVER);
818 obj_priv->page_list = NULL;
819}
820
821static void
Eric Anholtce44b0e2008-11-06 16:00:31 -0800822i915_gem_object_move_to_active(struct drm_gem_object *obj, uint32_t seqno)
Eric Anholt673a3942008-07-30 12:06:12 -0700823{
824 struct drm_device *dev = obj->dev;
825 drm_i915_private_t *dev_priv = dev->dev_private;
826 struct drm_i915_gem_object *obj_priv = obj->driver_private;
827
828 /* Add a reference if we're newly entering the active list. */
829 if (!obj_priv->active) {
830 drm_gem_object_reference(obj);
831 obj_priv->active = 1;
832 }
833 /* Move from whatever list we were on to the tail of execution. */
834 list_move_tail(&obj_priv->list,
835 &dev_priv->mm.active_list);
Eric Anholtce44b0e2008-11-06 16:00:31 -0800836 obj_priv->last_rendering_seqno = seqno;
Eric Anholt673a3942008-07-30 12:06:12 -0700837}
838
Eric Anholtce44b0e2008-11-06 16:00:31 -0800839static void
840i915_gem_object_move_to_flushing(struct drm_gem_object *obj)
841{
842 struct drm_device *dev = obj->dev;
843 drm_i915_private_t *dev_priv = dev->dev_private;
844 struct drm_i915_gem_object *obj_priv = obj->driver_private;
845
846 BUG_ON(!obj_priv->active);
847 list_move_tail(&obj_priv->list, &dev_priv->mm.flushing_list);
848 obj_priv->last_rendering_seqno = 0;
849}
Eric Anholt673a3942008-07-30 12:06:12 -0700850
851static void
852i915_gem_object_move_to_inactive(struct drm_gem_object *obj)
853{
854 struct drm_device *dev = obj->dev;
855 drm_i915_private_t *dev_priv = dev->dev_private;
856 struct drm_i915_gem_object *obj_priv = obj->driver_private;
857
858 i915_verify_inactive(dev, __FILE__, __LINE__);
859 if (obj_priv->pin_count != 0)
860 list_del_init(&obj_priv->list);
861 else
862 list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
863
Eric Anholtce44b0e2008-11-06 16:00:31 -0800864 obj_priv->last_rendering_seqno = 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700865 if (obj_priv->active) {
866 obj_priv->active = 0;
867 drm_gem_object_unreference(obj);
868 }
869 i915_verify_inactive(dev, __FILE__, __LINE__);
870}
871
872/**
873 * Creates a new sequence number, emitting a write of it to the status page
874 * plus an interrupt, which will trigger i915_user_interrupt_handler.
875 *
876 * Must be called with struct_lock held.
877 *
878 * Returned sequence numbers are nonzero on success.
879 */
880static uint32_t
881i915_add_request(struct drm_device *dev, uint32_t flush_domains)
882{
883 drm_i915_private_t *dev_priv = dev->dev_private;
884 struct drm_i915_gem_request *request;
885 uint32_t seqno;
886 int was_empty;
887 RING_LOCALS;
888
889 request = drm_calloc(1, sizeof(*request), DRM_MEM_DRIVER);
890 if (request == NULL)
891 return 0;
892
893 /* Grab the seqno we're going to make this request be, and bump the
894 * next (skipping 0 so it can be the reserved no-seqno value).
895 */
896 seqno = dev_priv->mm.next_gem_seqno;
897 dev_priv->mm.next_gem_seqno++;
898 if (dev_priv->mm.next_gem_seqno == 0)
899 dev_priv->mm.next_gem_seqno++;
900
901 BEGIN_LP_RING(4);
902 OUT_RING(MI_STORE_DWORD_INDEX);
903 OUT_RING(I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
904 OUT_RING(seqno);
905
906 OUT_RING(MI_USER_INTERRUPT);
907 ADVANCE_LP_RING();
908
909 DRM_DEBUG("%d\n", seqno);
910
911 request->seqno = seqno;
912 request->emitted_jiffies = jiffies;
Eric Anholt673a3942008-07-30 12:06:12 -0700913 was_empty = list_empty(&dev_priv->mm.request_list);
914 list_add_tail(&request->list, &dev_priv->mm.request_list);
915
Eric Anholtce44b0e2008-11-06 16:00:31 -0800916 /* Associate any objects on the flushing list matching the write
917 * domain we're flushing with our flush.
918 */
919 if (flush_domains != 0) {
920 struct drm_i915_gem_object *obj_priv, *next;
921
922 list_for_each_entry_safe(obj_priv, next,
923 &dev_priv->mm.flushing_list, list) {
924 struct drm_gem_object *obj = obj_priv->obj;
925
926 if ((obj->write_domain & flush_domains) ==
927 obj->write_domain) {
928 obj->write_domain = 0;
929 i915_gem_object_move_to_active(obj, seqno);
930 }
931 }
932
933 }
934
Keith Packard6dbe2772008-10-14 21:41:13 -0700935 if (was_empty && !dev_priv->mm.suspended)
Eric Anholt673a3942008-07-30 12:06:12 -0700936 schedule_delayed_work(&dev_priv->mm.retire_work, HZ);
937 return seqno;
938}
939
940/**
941 * Command execution barrier
942 *
943 * Ensures that all commands in the ring are finished
944 * before signalling the CPU
945 */
Eric Anholt3043c602008-10-02 12:24:47 -0700946static uint32_t
Eric Anholt673a3942008-07-30 12:06:12 -0700947i915_retire_commands(struct drm_device *dev)
948{
949 drm_i915_private_t *dev_priv = dev->dev_private;
950 uint32_t cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
951 uint32_t flush_domains = 0;
952 RING_LOCALS;
953
954 /* The sampler always gets flushed on i965 (sigh) */
955 if (IS_I965G(dev))
956 flush_domains |= I915_GEM_DOMAIN_SAMPLER;
957 BEGIN_LP_RING(2);
958 OUT_RING(cmd);
959 OUT_RING(0); /* noop */
960 ADVANCE_LP_RING();
961 return flush_domains;
962}
963
964/**
965 * Moves buffers associated only with the given active seqno from the active
966 * to inactive list, potentially freeing them.
967 */
968static void
969i915_gem_retire_request(struct drm_device *dev,
970 struct drm_i915_gem_request *request)
971{
972 drm_i915_private_t *dev_priv = dev->dev_private;
973
974 /* Move any buffers on the active list that are no longer referenced
975 * by the ringbuffer to the flushing/inactive lists as appropriate.
976 */
977 while (!list_empty(&dev_priv->mm.active_list)) {
978 struct drm_gem_object *obj;
979 struct drm_i915_gem_object *obj_priv;
980
981 obj_priv = list_first_entry(&dev_priv->mm.active_list,
982 struct drm_i915_gem_object,
983 list);
984 obj = obj_priv->obj;
985
986 /* If the seqno being retired doesn't match the oldest in the
987 * list, then the oldest in the list must still be newer than
988 * this seqno.
989 */
990 if (obj_priv->last_rendering_seqno != request->seqno)
991 return;
Jesse Barnesde151cf2008-11-12 10:03:55 -0800992
Eric Anholt673a3942008-07-30 12:06:12 -0700993#if WATCH_LRU
994 DRM_INFO("%s: retire %d moves to inactive list %p\n",
995 __func__, request->seqno, obj);
996#endif
997
Eric Anholtce44b0e2008-11-06 16:00:31 -0800998 if (obj->write_domain != 0)
999 i915_gem_object_move_to_flushing(obj);
1000 else
Eric Anholt673a3942008-07-30 12:06:12 -07001001 i915_gem_object_move_to_inactive(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001002 }
1003}
1004
1005/**
1006 * Returns true if seq1 is later than seq2.
1007 */
1008static int
1009i915_seqno_passed(uint32_t seq1, uint32_t seq2)
1010{
1011 return (int32_t)(seq1 - seq2) >= 0;
1012}
1013
1014uint32_t
1015i915_get_gem_seqno(struct drm_device *dev)
1016{
1017 drm_i915_private_t *dev_priv = dev->dev_private;
1018
1019 return READ_HWSP(dev_priv, I915_GEM_HWS_INDEX);
1020}
1021
1022/**
1023 * This function clears the request list as sequence numbers are passed.
1024 */
1025void
1026i915_gem_retire_requests(struct drm_device *dev)
1027{
1028 drm_i915_private_t *dev_priv = dev->dev_private;
1029 uint32_t seqno;
1030
1031 seqno = i915_get_gem_seqno(dev);
1032
1033 while (!list_empty(&dev_priv->mm.request_list)) {
1034 struct drm_i915_gem_request *request;
1035 uint32_t retiring_seqno;
1036
1037 request = list_first_entry(&dev_priv->mm.request_list,
1038 struct drm_i915_gem_request,
1039 list);
1040 retiring_seqno = request->seqno;
1041
1042 if (i915_seqno_passed(seqno, retiring_seqno) ||
1043 dev_priv->mm.wedged) {
1044 i915_gem_retire_request(dev, request);
1045
1046 list_del(&request->list);
1047 drm_free(request, sizeof(*request), DRM_MEM_DRIVER);
1048 } else
1049 break;
1050 }
1051}
1052
1053void
1054i915_gem_retire_work_handler(struct work_struct *work)
1055{
1056 drm_i915_private_t *dev_priv;
1057 struct drm_device *dev;
1058
1059 dev_priv = container_of(work, drm_i915_private_t,
1060 mm.retire_work.work);
1061 dev = dev_priv->dev;
1062
1063 mutex_lock(&dev->struct_mutex);
1064 i915_gem_retire_requests(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07001065 if (!dev_priv->mm.suspended &&
1066 !list_empty(&dev_priv->mm.request_list))
Eric Anholt673a3942008-07-30 12:06:12 -07001067 schedule_delayed_work(&dev_priv->mm.retire_work, HZ);
1068 mutex_unlock(&dev->struct_mutex);
1069}
1070
1071/**
1072 * Waits for a sequence number to be signaled, and cleans up the
1073 * request and object lists appropriately for that event.
1074 */
Eric Anholt3043c602008-10-02 12:24:47 -07001075static int
Eric Anholt673a3942008-07-30 12:06:12 -07001076i915_wait_request(struct drm_device *dev, uint32_t seqno)
1077{
1078 drm_i915_private_t *dev_priv = dev->dev_private;
1079 int ret = 0;
1080
1081 BUG_ON(seqno == 0);
1082
1083 if (!i915_seqno_passed(i915_get_gem_seqno(dev), seqno)) {
1084 dev_priv->mm.waiting_gem_seqno = seqno;
1085 i915_user_irq_get(dev);
1086 ret = wait_event_interruptible(dev_priv->irq_queue,
1087 i915_seqno_passed(i915_get_gem_seqno(dev),
1088 seqno) ||
1089 dev_priv->mm.wedged);
1090 i915_user_irq_put(dev);
1091 dev_priv->mm.waiting_gem_seqno = 0;
1092 }
1093 if (dev_priv->mm.wedged)
1094 ret = -EIO;
1095
1096 if (ret && ret != -ERESTARTSYS)
1097 DRM_ERROR("%s returns %d (awaiting %d at %d)\n",
1098 __func__, ret, seqno, i915_get_gem_seqno(dev));
1099
1100 /* Directly dispatch request retiring. While we have the work queue
1101 * to handle this, the waiter on a request often wants an associated
1102 * buffer to have made it to the inactive list, and we would need
1103 * a separate wait queue to handle that.
1104 */
1105 if (ret == 0)
1106 i915_gem_retire_requests(dev);
1107
1108 return ret;
1109}
1110
1111static void
1112i915_gem_flush(struct drm_device *dev,
1113 uint32_t invalidate_domains,
1114 uint32_t flush_domains)
1115{
1116 drm_i915_private_t *dev_priv = dev->dev_private;
1117 uint32_t cmd;
1118 RING_LOCALS;
1119
1120#if WATCH_EXEC
1121 DRM_INFO("%s: invalidate %08x flush %08x\n", __func__,
1122 invalidate_domains, flush_domains);
1123#endif
1124
1125 if (flush_domains & I915_GEM_DOMAIN_CPU)
1126 drm_agp_chipset_flush(dev);
1127
1128 if ((invalidate_domains | flush_domains) & ~(I915_GEM_DOMAIN_CPU |
1129 I915_GEM_DOMAIN_GTT)) {
1130 /*
1131 * read/write caches:
1132 *
1133 * I915_GEM_DOMAIN_RENDER is always invalidated, but is
1134 * only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is
1135 * also flushed at 2d versus 3d pipeline switches.
1136 *
1137 * read-only caches:
1138 *
1139 * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if
1140 * MI_READ_FLUSH is set, and is always flushed on 965.
1141 *
1142 * I915_GEM_DOMAIN_COMMAND may not exist?
1143 *
1144 * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is
1145 * invalidated when MI_EXE_FLUSH is set.
1146 *
1147 * I915_GEM_DOMAIN_VERTEX, which exists on 965, is
1148 * invalidated with every MI_FLUSH.
1149 *
1150 * TLBs:
1151 *
1152 * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND
1153 * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and
1154 * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER
1155 * are flushed at any MI_FLUSH.
1156 */
1157
1158 cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
1159 if ((invalidate_domains|flush_domains) &
1160 I915_GEM_DOMAIN_RENDER)
1161 cmd &= ~MI_NO_WRITE_FLUSH;
1162 if (!IS_I965G(dev)) {
1163 /*
1164 * On the 965, the sampler cache always gets flushed
1165 * and this bit is reserved.
1166 */
1167 if (invalidate_domains & I915_GEM_DOMAIN_SAMPLER)
1168 cmd |= MI_READ_FLUSH;
1169 }
1170 if (invalidate_domains & I915_GEM_DOMAIN_INSTRUCTION)
1171 cmd |= MI_EXE_FLUSH;
1172
1173#if WATCH_EXEC
1174 DRM_INFO("%s: queue flush %08x to ring\n", __func__, cmd);
1175#endif
1176 BEGIN_LP_RING(2);
1177 OUT_RING(cmd);
1178 OUT_RING(0); /* noop */
1179 ADVANCE_LP_RING();
1180 }
1181}
1182
1183/**
1184 * Ensures that all rendering to the object has completed and the object is
1185 * safe to unbind from the GTT or access from the CPU.
1186 */
1187static int
1188i915_gem_object_wait_rendering(struct drm_gem_object *obj)
1189{
1190 struct drm_device *dev = obj->dev;
1191 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1192 int ret;
1193
Eric Anholte47c68e2008-11-14 13:35:19 -08001194 /* This function only exists to support waiting for existing rendering,
1195 * not for emitting required flushes.
Eric Anholt673a3942008-07-30 12:06:12 -07001196 */
Eric Anholte47c68e2008-11-14 13:35:19 -08001197 BUG_ON((obj->write_domain & I915_GEM_GPU_DOMAINS) != 0);
Eric Anholt673a3942008-07-30 12:06:12 -07001198
1199 /* If there is rendering queued on the buffer being evicted, wait for
1200 * it.
1201 */
1202 if (obj_priv->active) {
1203#if WATCH_BUF
1204 DRM_INFO("%s: object %p wait for seqno %08x\n",
1205 __func__, obj, obj_priv->last_rendering_seqno);
1206#endif
1207 ret = i915_wait_request(dev, obj_priv->last_rendering_seqno);
1208 if (ret != 0)
1209 return ret;
1210 }
1211
1212 return 0;
1213}
1214
1215/**
1216 * Unbinds an object from the GTT aperture.
1217 */
Jesse Barnes0f973f22009-01-26 17:10:45 -08001218int
Eric Anholt673a3942008-07-30 12:06:12 -07001219i915_gem_object_unbind(struct drm_gem_object *obj)
1220{
1221 struct drm_device *dev = obj->dev;
1222 struct drm_i915_gem_object *obj_priv = obj->driver_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001223 loff_t offset;
Eric Anholt673a3942008-07-30 12:06:12 -07001224 int ret = 0;
1225
1226#if WATCH_BUF
1227 DRM_INFO("%s:%d %p\n", __func__, __LINE__, obj);
1228 DRM_INFO("gtt_space %p\n", obj_priv->gtt_space);
1229#endif
1230 if (obj_priv->gtt_space == NULL)
1231 return 0;
1232
1233 if (obj_priv->pin_count != 0) {
1234 DRM_ERROR("Attempting to unbind pinned buffer\n");
1235 return -EINVAL;
1236 }
1237
Eric Anholt673a3942008-07-30 12:06:12 -07001238 /* Move the object to the CPU domain to ensure that
1239 * any possible CPU writes while it's not in the GTT
1240 * are flushed when we go to remap it. This will
1241 * also ensure that all pending GPU writes are finished
1242 * before we unbind.
1243 */
Eric Anholte47c68e2008-11-14 13:35:19 -08001244 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
Eric Anholt673a3942008-07-30 12:06:12 -07001245 if (ret) {
Eric Anholte47c68e2008-11-14 13:35:19 -08001246 if (ret != -ERESTARTSYS)
1247 DRM_ERROR("set_domain failed: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07001248 return ret;
1249 }
1250
1251 if (obj_priv->agp_mem != NULL) {
1252 drm_unbind_agp(obj_priv->agp_mem);
1253 drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE);
1254 obj_priv->agp_mem = NULL;
1255 }
1256
1257 BUG_ON(obj_priv->active);
1258
Jesse Barnesde151cf2008-11-12 10:03:55 -08001259 /* blow away mappings if mapped through GTT */
1260 offset = ((loff_t) obj->map_list.hash.key) << PAGE_SHIFT;
Jesse Barnes79e53942008-11-07 14:24:08 -08001261 if (dev->dev_mapping)
1262 unmap_mapping_range(dev->dev_mapping, offset, obj->size, 1);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001263
1264 if (obj_priv->fence_reg != I915_FENCE_REG_NONE)
1265 i915_gem_clear_fence_reg(obj);
1266
Eric Anholt673a3942008-07-30 12:06:12 -07001267 i915_gem_object_free_page_list(obj);
1268
1269 if (obj_priv->gtt_space) {
1270 atomic_dec(&dev->gtt_count);
1271 atomic_sub(obj->size, &dev->gtt_memory);
1272
1273 drm_mm_put_block(obj_priv->gtt_space);
1274 obj_priv->gtt_space = NULL;
1275 }
1276
1277 /* Remove ourselves from the LRU list if present. */
1278 if (!list_empty(&obj_priv->list))
1279 list_del_init(&obj_priv->list);
1280
1281 return 0;
1282}
1283
1284static int
1285i915_gem_evict_something(struct drm_device *dev)
1286{
1287 drm_i915_private_t *dev_priv = dev->dev_private;
1288 struct drm_gem_object *obj;
1289 struct drm_i915_gem_object *obj_priv;
1290 int ret = 0;
1291
1292 for (;;) {
1293 /* If there's an inactive buffer available now, grab it
1294 * and be done.
1295 */
1296 if (!list_empty(&dev_priv->mm.inactive_list)) {
1297 obj_priv = list_first_entry(&dev_priv->mm.inactive_list,
1298 struct drm_i915_gem_object,
1299 list);
1300 obj = obj_priv->obj;
1301 BUG_ON(obj_priv->pin_count != 0);
1302#if WATCH_LRU
1303 DRM_INFO("%s: evicting %p\n", __func__, obj);
1304#endif
1305 BUG_ON(obj_priv->active);
1306
1307 /* Wait on the rendering and unbind the buffer. */
1308 ret = i915_gem_object_unbind(obj);
1309 break;
1310 }
1311
1312 /* If we didn't get anything, but the ring is still processing
1313 * things, wait for one of those things to finish and hopefully
1314 * leave us a buffer to evict.
1315 */
1316 if (!list_empty(&dev_priv->mm.request_list)) {
1317 struct drm_i915_gem_request *request;
1318
1319 request = list_first_entry(&dev_priv->mm.request_list,
1320 struct drm_i915_gem_request,
1321 list);
1322
1323 ret = i915_wait_request(dev, request->seqno);
1324 if (ret)
1325 break;
1326
1327 /* if waiting caused an object to become inactive,
1328 * then loop around and wait for it. Otherwise, we
1329 * assume that waiting freed and unbound something,
1330 * so there should now be some space in the GTT
1331 */
1332 if (!list_empty(&dev_priv->mm.inactive_list))
1333 continue;
1334 break;
1335 }
1336
1337 /* If we didn't have anything on the request list but there
1338 * are buffers awaiting a flush, emit one and try again.
1339 * When we wait on it, those buffers waiting for that flush
1340 * will get moved to inactive.
1341 */
1342 if (!list_empty(&dev_priv->mm.flushing_list)) {
1343 obj_priv = list_first_entry(&dev_priv->mm.flushing_list,
1344 struct drm_i915_gem_object,
1345 list);
1346 obj = obj_priv->obj;
1347
1348 i915_gem_flush(dev,
1349 obj->write_domain,
1350 obj->write_domain);
1351 i915_add_request(dev, obj->write_domain);
1352
1353 obj = NULL;
1354 continue;
1355 }
1356
1357 DRM_ERROR("inactive empty %d request empty %d "
1358 "flushing empty %d\n",
1359 list_empty(&dev_priv->mm.inactive_list),
1360 list_empty(&dev_priv->mm.request_list),
1361 list_empty(&dev_priv->mm.flushing_list));
1362 /* If we didn't do any of the above, there's nothing to be done
1363 * and we just can't fit it in.
1364 */
1365 return -ENOMEM;
1366 }
1367 return ret;
1368}
1369
1370static int
Keith Packardac94a962008-11-20 23:30:27 -08001371i915_gem_evict_everything(struct drm_device *dev)
1372{
1373 int ret;
1374
1375 for (;;) {
1376 ret = i915_gem_evict_something(dev);
1377 if (ret != 0)
1378 break;
1379 }
Owain Ainsworth15c35332008-12-06 20:42:20 -08001380 if (ret == -ENOMEM)
1381 return 0;
Keith Packardac94a962008-11-20 23:30:27 -08001382 return ret;
1383}
1384
1385static int
Eric Anholt673a3942008-07-30 12:06:12 -07001386i915_gem_object_get_page_list(struct drm_gem_object *obj)
1387{
1388 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1389 int page_count, i;
1390 struct address_space *mapping;
1391 struct inode *inode;
1392 struct page *page;
1393 int ret;
1394
1395 if (obj_priv->page_list)
1396 return 0;
1397
1398 /* Get the list of pages out of our struct file. They'll be pinned
1399 * at this point until we release them.
1400 */
1401 page_count = obj->size / PAGE_SIZE;
1402 BUG_ON(obj_priv->page_list != NULL);
1403 obj_priv->page_list = drm_calloc(page_count, sizeof(struct page *),
1404 DRM_MEM_DRIVER);
1405 if (obj_priv->page_list == NULL) {
1406 DRM_ERROR("Faled to allocate page list\n");
1407 return -ENOMEM;
1408 }
1409
1410 inode = obj->filp->f_path.dentry->d_inode;
1411 mapping = inode->i_mapping;
1412 for (i = 0; i < page_count; i++) {
1413 page = read_mapping_page(mapping, i, NULL);
1414 if (IS_ERR(page)) {
1415 ret = PTR_ERR(page);
1416 DRM_ERROR("read_mapping_page failed: %d\n", ret);
1417 i915_gem_object_free_page_list(obj);
1418 return ret;
1419 }
1420 obj_priv->page_list[i] = page;
1421 }
1422 return 0;
1423}
1424
Jesse Barnesde151cf2008-11-12 10:03:55 -08001425static void i965_write_fence_reg(struct drm_i915_fence_reg *reg)
1426{
1427 struct drm_gem_object *obj = reg->obj;
1428 struct drm_device *dev = obj->dev;
1429 drm_i915_private_t *dev_priv = dev->dev_private;
1430 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1431 int regnum = obj_priv->fence_reg;
1432 uint64_t val;
1433
1434 val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
1435 0xfffff000) << 32;
1436 val |= obj_priv->gtt_offset & 0xfffff000;
1437 val |= ((obj_priv->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
1438 if (obj_priv->tiling_mode == I915_TILING_Y)
1439 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
1440 val |= I965_FENCE_REG_VALID;
1441
1442 I915_WRITE64(FENCE_REG_965_0 + (regnum * 8), val);
1443}
1444
1445static void i915_write_fence_reg(struct drm_i915_fence_reg *reg)
1446{
1447 struct drm_gem_object *obj = reg->obj;
1448 struct drm_device *dev = obj->dev;
1449 drm_i915_private_t *dev_priv = dev->dev_private;
1450 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1451 int regnum = obj_priv->fence_reg;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001452 int tile_width;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001453 uint32_t val;
1454 uint32_t pitch_val;
1455
1456 if ((obj_priv->gtt_offset & ~I915_FENCE_START_MASK) ||
1457 (obj_priv->gtt_offset & (obj->size - 1))) {
Jesse Barnes0f973f22009-01-26 17:10:45 -08001458 WARN(1, "%s: object 0x%08x not 1M or size (0x%x) aligned\n",
1459 __func__, obj_priv->gtt_offset, obj->size);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001460 return;
1461 }
1462
Jesse Barnes0f973f22009-01-26 17:10:45 -08001463 if (obj_priv->tiling_mode == I915_TILING_Y &&
1464 HAS_128_BYTE_Y_TILING(dev))
1465 tile_width = 128;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001466 else
Jesse Barnes0f973f22009-01-26 17:10:45 -08001467 tile_width = 512;
1468
1469 /* Note: pitch better be a power of two tile widths */
1470 pitch_val = obj_priv->stride / tile_width;
1471 pitch_val = ffs(pitch_val) - 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001472
1473 val = obj_priv->gtt_offset;
1474 if (obj_priv->tiling_mode == I915_TILING_Y)
1475 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
1476 val |= I915_FENCE_SIZE_BITS(obj->size);
1477 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
1478 val |= I830_FENCE_REG_VALID;
1479
1480 I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val);
1481}
1482
1483static void i830_write_fence_reg(struct drm_i915_fence_reg *reg)
1484{
1485 struct drm_gem_object *obj = reg->obj;
1486 struct drm_device *dev = obj->dev;
1487 drm_i915_private_t *dev_priv = dev->dev_private;
1488 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1489 int regnum = obj_priv->fence_reg;
1490 uint32_t val;
1491 uint32_t pitch_val;
1492
1493 if ((obj_priv->gtt_offset & ~I915_FENCE_START_MASK) ||
1494 (obj_priv->gtt_offset & (obj->size - 1))) {
Jesse Barnes0f973f22009-01-26 17:10:45 -08001495 WARN(1, "%s: object 0x%08x not 1M or size aligned\n",
1496 __func__, obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001497 return;
1498 }
1499
1500 pitch_val = (obj_priv->stride / 128) - 1;
1501
1502 val = obj_priv->gtt_offset;
1503 if (obj_priv->tiling_mode == I915_TILING_Y)
1504 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
1505 val |= I830_FENCE_SIZE_BITS(obj->size);
1506 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
1507 val |= I830_FENCE_REG_VALID;
1508
1509 I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val);
1510
1511}
1512
1513/**
1514 * i915_gem_object_get_fence_reg - set up a fence reg for an object
1515 * @obj: object to map through a fence reg
Jesse Barnes0f973f22009-01-26 17:10:45 -08001516 * @write: object is about to be written
Jesse Barnesde151cf2008-11-12 10:03:55 -08001517 *
1518 * When mapping objects through the GTT, userspace wants to be able to write
1519 * to them without having to worry about swizzling if the object is tiled.
1520 *
1521 * This function walks the fence regs looking for a free one for @obj,
1522 * stealing one if it can't find any.
1523 *
1524 * It then sets up the reg based on the object's properties: address, pitch
1525 * and tiling format.
1526 */
Eric Anholtd9ddcb92009-01-27 10:33:49 -08001527static int
Jesse Barnes0f973f22009-01-26 17:10:45 -08001528i915_gem_object_get_fence_reg(struct drm_gem_object *obj, bool write)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001529{
1530 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08001531 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001532 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1533 struct drm_i915_fence_reg *reg = NULL;
1534 int i, ret;
1535
1536 switch (obj_priv->tiling_mode) {
1537 case I915_TILING_NONE:
1538 WARN(1, "allocating a fence for non-tiled object?\n");
1539 break;
1540 case I915_TILING_X:
Jesse Barnes0f973f22009-01-26 17:10:45 -08001541 if (!obj_priv->stride)
1542 return -EINVAL;
1543 WARN((obj_priv->stride & (512 - 1)),
1544 "object 0x%08x is X tiled but has non-512B pitch\n",
1545 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001546 break;
1547 case I915_TILING_Y:
Jesse Barnes0f973f22009-01-26 17:10:45 -08001548 if (!obj_priv->stride)
1549 return -EINVAL;
1550 WARN((obj_priv->stride & (128 - 1)),
1551 "object 0x%08x is Y tiled but has non-128B pitch\n",
1552 obj_priv->gtt_offset);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001553 break;
1554 }
1555
1556 /* First try to find a free reg */
1557 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
1558 reg = &dev_priv->fence_regs[i];
1559 if (!reg->obj)
1560 break;
1561 }
1562
1563 /* None available, try to steal one or wait for a user to finish */
1564 if (i == dev_priv->num_fence_regs) {
1565 struct drm_i915_gem_object *old_obj_priv = NULL;
1566 loff_t offset;
1567
1568try_again:
1569 /* Could try to use LRU here instead... */
1570 for (i = dev_priv->fence_reg_start;
1571 i < dev_priv->num_fence_regs; i++) {
1572 reg = &dev_priv->fence_regs[i];
1573 old_obj_priv = reg->obj->driver_private;
1574 if (!old_obj_priv->pin_count)
1575 break;
1576 }
1577
1578 /*
1579 * Now things get ugly... we have to wait for one of the
1580 * objects to finish before trying again.
1581 */
1582 if (i == dev_priv->num_fence_regs) {
Eric Anholtd9ddcb92009-01-27 10:33:49 -08001583 ret = i915_gem_object_set_to_gtt_domain(reg->obj, 0);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001584 if (ret) {
Eric Anholtd9ddcb92009-01-27 10:33:49 -08001585 WARN(ret != -ERESTARTSYS,
1586 "switch to GTT domain failed: %d\n", ret);
1587 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001588 }
1589 goto try_again;
1590 }
1591
1592 /*
1593 * Zap this virtual mapping so we can set up a fence again
1594 * for this object next time we need it.
1595 */
1596 offset = ((loff_t) reg->obj->map_list.hash.key) << PAGE_SHIFT;
Jesse Barnes79e53942008-11-07 14:24:08 -08001597 if (dev->dev_mapping)
1598 unmap_mapping_range(dev->dev_mapping, offset,
1599 reg->obj->size, 1);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001600 old_obj_priv->fence_reg = I915_FENCE_REG_NONE;
1601 }
1602
1603 obj_priv->fence_reg = i;
1604 reg->obj = obj;
1605
1606 if (IS_I965G(dev))
1607 i965_write_fence_reg(reg);
1608 else if (IS_I9XX(dev))
1609 i915_write_fence_reg(reg);
1610 else
1611 i830_write_fence_reg(reg);
Eric Anholtd9ddcb92009-01-27 10:33:49 -08001612
1613 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001614}
1615
1616/**
1617 * i915_gem_clear_fence_reg - clear out fence register info
1618 * @obj: object to clear
1619 *
1620 * Zeroes out the fence register itself and clears out the associated
1621 * data structures in dev_priv and obj_priv.
1622 */
1623static void
1624i915_gem_clear_fence_reg(struct drm_gem_object *obj)
1625{
1626 struct drm_device *dev = obj->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08001627 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001628 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1629
1630 if (IS_I965G(dev))
1631 I915_WRITE64(FENCE_REG_965_0 + (obj_priv->fence_reg * 8), 0);
1632 else
1633 I915_WRITE(FENCE_REG_830_0 + (obj_priv->fence_reg * 4), 0);
1634
1635 dev_priv->fence_regs[obj_priv->fence_reg].obj = NULL;
1636 obj_priv->fence_reg = I915_FENCE_REG_NONE;
1637}
1638
Eric Anholt673a3942008-07-30 12:06:12 -07001639/**
1640 * Finds free space in the GTT aperture and binds the object there.
1641 */
1642static int
1643i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment)
1644{
1645 struct drm_device *dev = obj->dev;
1646 drm_i915_private_t *dev_priv = dev->dev_private;
1647 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1648 struct drm_mm_node *free_space;
1649 int page_count, ret;
1650
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08001651 if (dev_priv->mm.suspended)
1652 return -EBUSY;
Eric Anholt673a3942008-07-30 12:06:12 -07001653 if (alignment == 0)
Jesse Barnes0f973f22009-01-26 17:10:45 -08001654 alignment = i915_gem_get_gtt_alignment(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001655 if (alignment & (PAGE_SIZE - 1)) {
1656 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
1657 return -EINVAL;
1658 }
1659
1660 search_free:
1661 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
1662 obj->size, alignment, 0);
1663 if (free_space != NULL) {
1664 obj_priv->gtt_space = drm_mm_get_block(free_space, obj->size,
1665 alignment);
1666 if (obj_priv->gtt_space != NULL) {
1667 obj_priv->gtt_space->private = obj;
1668 obj_priv->gtt_offset = obj_priv->gtt_space->start;
1669 }
1670 }
1671 if (obj_priv->gtt_space == NULL) {
1672 /* If the gtt is empty and we're still having trouble
1673 * fitting our object in, we're out of memory.
1674 */
1675#if WATCH_LRU
1676 DRM_INFO("%s: GTT full, evicting something\n", __func__);
1677#endif
1678 if (list_empty(&dev_priv->mm.inactive_list) &&
1679 list_empty(&dev_priv->mm.flushing_list) &&
1680 list_empty(&dev_priv->mm.active_list)) {
1681 DRM_ERROR("GTT full, but LRU list empty\n");
1682 return -ENOMEM;
1683 }
1684
1685 ret = i915_gem_evict_something(dev);
1686 if (ret != 0) {
Keith Packardac94a962008-11-20 23:30:27 -08001687 if (ret != -ERESTARTSYS)
1688 DRM_ERROR("Failed to evict a buffer %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07001689 return ret;
1690 }
1691 goto search_free;
1692 }
1693
1694#if WATCH_BUF
1695 DRM_INFO("Binding object of size %d at 0x%08x\n",
1696 obj->size, obj_priv->gtt_offset);
1697#endif
1698 ret = i915_gem_object_get_page_list(obj);
1699 if (ret) {
1700 drm_mm_put_block(obj_priv->gtt_space);
1701 obj_priv->gtt_space = NULL;
1702 return ret;
1703 }
1704
1705 page_count = obj->size / PAGE_SIZE;
1706 /* Create an AGP memory structure pointing at our pages, and bind it
1707 * into the GTT.
1708 */
1709 obj_priv->agp_mem = drm_agp_bind_pages(dev,
1710 obj_priv->page_list,
1711 page_count,
Keith Packardba1eb1d2008-10-14 19:55:10 -07001712 obj_priv->gtt_offset,
1713 obj_priv->agp_type);
Eric Anholt673a3942008-07-30 12:06:12 -07001714 if (obj_priv->agp_mem == NULL) {
1715 i915_gem_object_free_page_list(obj);
1716 drm_mm_put_block(obj_priv->gtt_space);
1717 obj_priv->gtt_space = NULL;
1718 return -ENOMEM;
1719 }
1720 atomic_inc(&dev->gtt_count);
1721 atomic_add(obj->size, &dev->gtt_memory);
1722
1723 /* Assert that the object is not currently in any GPU domain. As it
1724 * wasn't in the GTT, there shouldn't be any way it could have been in
1725 * a GPU cache
1726 */
1727 BUG_ON(obj->read_domains & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT));
1728 BUG_ON(obj->write_domain & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT));
1729
1730 return 0;
1731}
1732
1733void
1734i915_gem_clflush_object(struct drm_gem_object *obj)
1735{
1736 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1737
1738 /* If we don't have a page list set up, then we're not pinned
1739 * to GPU, and we can ignore the cache flush because it'll happen
1740 * again at bind time.
1741 */
1742 if (obj_priv->page_list == NULL)
1743 return;
1744
1745 drm_clflush_pages(obj_priv->page_list, obj->size / PAGE_SIZE);
1746}
1747
Eric Anholte47c68e2008-11-14 13:35:19 -08001748/** Flushes any GPU write domain for the object if it's dirty. */
1749static void
1750i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj)
1751{
1752 struct drm_device *dev = obj->dev;
1753 uint32_t seqno;
1754
1755 if ((obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
1756 return;
1757
1758 /* Queue the GPU write cache flushing we need. */
1759 i915_gem_flush(dev, 0, obj->write_domain);
1760 seqno = i915_add_request(dev, obj->write_domain);
1761 obj->write_domain = 0;
1762 i915_gem_object_move_to_active(obj, seqno);
1763}
1764
1765/** Flushes the GTT write domain for the object if it's dirty. */
1766static void
1767i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj)
1768{
1769 if (obj->write_domain != I915_GEM_DOMAIN_GTT)
1770 return;
1771
1772 /* No actual flushing is required for the GTT write domain. Writes
1773 * to it immediately go to main memory as far as we know, so there's
1774 * no chipset flush. It also doesn't land in render cache.
1775 */
1776 obj->write_domain = 0;
1777}
1778
1779/** Flushes the CPU write domain for the object if it's dirty. */
1780static void
1781i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj)
1782{
1783 struct drm_device *dev = obj->dev;
1784
1785 if (obj->write_domain != I915_GEM_DOMAIN_CPU)
1786 return;
1787
1788 i915_gem_clflush_object(obj);
1789 drm_agp_chipset_flush(dev);
1790 obj->write_domain = 0;
1791}
1792
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001793/**
1794 * Moves a single object to the GTT read, and possibly write domain.
1795 *
1796 * This function returns when the move is complete, including waiting on
1797 * flushes to occur.
1798 */
Jesse Barnes79e53942008-11-07 14:24:08 -08001799int
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001800i915_gem_object_set_to_gtt_domain(struct drm_gem_object *obj, int write)
1801{
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001802 struct drm_i915_gem_object *obj_priv = obj->driver_private;
Eric Anholte47c68e2008-11-14 13:35:19 -08001803 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001804
Eric Anholt02354392008-11-26 13:58:13 -08001805 /* Not valid to be called on unbound objects. */
1806 if (obj_priv->gtt_space == NULL)
1807 return -EINVAL;
1808
Eric Anholte47c68e2008-11-14 13:35:19 -08001809 i915_gem_object_flush_gpu_write_domain(obj);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001810 /* Wait on any GPU rendering and flushing to occur. */
Eric Anholte47c68e2008-11-14 13:35:19 -08001811 ret = i915_gem_object_wait_rendering(obj);
1812 if (ret != 0)
1813 return ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001814
1815 /* If we're writing through the GTT domain, then CPU and GPU caches
1816 * will need to be invalidated at next use.
1817 */
1818 if (write)
Eric Anholte47c68e2008-11-14 13:35:19 -08001819 obj->read_domains &= I915_GEM_DOMAIN_GTT;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001820
Eric Anholte47c68e2008-11-14 13:35:19 -08001821 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001822
1823 /* It should now be out of any other write domains, and we can update
1824 * the domain values for our changes.
1825 */
1826 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
1827 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08001828 if (write) {
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001829 obj->write_domain = I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08001830 obj_priv->dirty = 1;
1831 }
1832
1833 return 0;
1834}
1835
1836/**
1837 * Moves a single object to the CPU read, and possibly write domain.
1838 *
1839 * This function returns when the move is complete, including waiting on
1840 * flushes to occur.
1841 */
1842static int
1843i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj, int write)
1844{
1845 struct drm_device *dev = obj->dev;
1846 int ret;
1847
1848 i915_gem_object_flush_gpu_write_domain(obj);
1849 /* Wait on any GPU rendering and flushing to occur. */
1850 ret = i915_gem_object_wait_rendering(obj);
1851 if (ret != 0)
1852 return ret;
1853
1854 i915_gem_object_flush_gtt_write_domain(obj);
1855
1856 /* If we have a partially-valid cache of the object in the CPU,
1857 * finish invalidating it and free the per-page flags.
1858 */
1859 i915_gem_object_set_to_full_cpu_read_domain(obj);
1860
1861 /* Flush the CPU cache if it's still invalid. */
1862 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
1863 i915_gem_clflush_object(obj);
1864 drm_agp_chipset_flush(dev);
1865
1866 obj->read_domains |= I915_GEM_DOMAIN_CPU;
1867 }
1868
1869 /* It should now be out of any other write domains, and we can update
1870 * the domain values for our changes.
1871 */
1872 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
1873
1874 /* If we're writing through the CPU, then the GPU read domains will
1875 * need to be invalidated at next use.
1876 */
1877 if (write) {
1878 obj->read_domains &= I915_GEM_DOMAIN_CPU;
1879 obj->write_domain = I915_GEM_DOMAIN_CPU;
1880 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001881
1882 return 0;
1883}
1884
Eric Anholt673a3942008-07-30 12:06:12 -07001885/*
1886 * Set the next domain for the specified object. This
1887 * may not actually perform the necessary flushing/invaliding though,
1888 * as that may want to be batched with other set_domain operations
1889 *
1890 * This is (we hope) the only really tricky part of gem. The goal
1891 * is fairly simple -- track which caches hold bits of the object
1892 * and make sure they remain coherent. A few concrete examples may
1893 * help to explain how it works. For shorthand, we use the notation
1894 * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
1895 * a pair of read and write domain masks.
1896 *
1897 * Case 1: the batch buffer
1898 *
1899 * 1. Allocated
1900 * 2. Written by CPU
1901 * 3. Mapped to GTT
1902 * 4. Read by GPU
1903 * 5. Unmapped from GTT
1904 * 6. Freed
1905 *
1906 * Let's take these a step at a time
1907 *
1908 * 1. Allocated
1909 * Pages allocated from the kernel may still have
1910 * cache contents, so we set them to (CPU, CPU) always.
1911 * 2. Written by CPU (using pwrite)
1912 * The pwrite function calls set_domain (CPU, CPU) and
1913 * this function does nothing (as nothing changes)
1914 * 3. Mapped by GTT
1915 * This function asserts that the object is not
1916 * currently in any GPU-based read or write domains
1917 * 4. Read by GPU
1918 * i915_gem_execbuffer calls set_domain (COMMAND, 0).
1919 * As write_domain is zero, this function adds in the
1920 * current read domains (CPU+COMMAND, 0).
1921 * flush_domains is set to CPU.
1922 * invalidate_domains is set to COMMAND
1923 * clflush is run to get data out of the CPU caches
1924 * then i915_dev_set_domain calls i915_gem_flush to
1925 * emit an MI_FLUSH and drm_agp_chipset_flush
1926 * 5. Unmapped from GTT
1927 * i915_gem_object_unbind calls set_domain (CPU, CPU)
1928 * flush_domains and invalidate_domains end up both zero
1929 * so no flushing/invalidating happens
1930 * 6. Freed
1931 * yay, done
1932 *
1933 * Case 2: The shared render buffer
1934 *
1935 * 1. Allocated
1936 * 2. Mapped to GTT
1937 * 3. Read/written by GPU
1938 * 4. set_domain to (CPU,CPU)
1939 * 5. Read/written by CPU
1940 * 6. Read/written by GPU
1941 *
1942 * 1. Allocated
1943 * Same as last example, (CPU, CPU)
1944 * 2. Mapped to GTT
1945 * Nothing changes (assertions find that it is not in the GPU)
1946 * 3. Read/written by GPU
1947 * execbuffer calls set_domain (RENDER, RENDER)
1948 * flush_domains gets CPU
1949 * invalidate_domains gets GPU
1950 * clflush (obj)
1951 * MI_FLUSH and drm_agp_chipset_flush
1952 * 4. set_domain (CPU, CPU)
1953 * flush_domains gets GPU
1954 * invalidate_domains gets CPU
1955 * wait_rendering (obj) to make sure all drawing is complete.
1956 * This will include an MI_FLUSH to get the data from GPU
1957 * to memory
1958 * clflush (obj) to invalidate the CPU cache
1959 * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
1960 * 5. Read/written by CPU
1961 * cache lines are loaded and dirtied
1962 * 6. Read written by GPU
1963 * Same as last GPU access
1964 *
1965 * Case 3: The constant buffer
1966 *
1967 * 1. Allocated
1968 * 2. Written by CPU
1969 * 3. Read by GPU
1970 * 4. Updated (written) by CPU again
1971 * 5. Read by GPU
1972 *
1973 * 1. Allocated
1974 * (CPU, CPU)
1975 * 2. Written by CPU
1976 * (CPU, CPU)
1977 * 3. Read by GPU
1978 * (CPU+RENDER, 0)
1979 * flush_domains = CPU
1980 * invalidate_domains = RENDER
1981 * clflush (obj)
1982 * MI_FLUSH
1983 * drm_agp_chipset_flush
1984 * 4. Updated (written) by CPU again
1985 * (CPU, CPU)
1986 * flush_domains = 0 (no previous write domain)
1987 * invalidate_domains = 0 (no new read domains)
1988 * 5. Read by GPU
1989 * (CPU+RENDER, 0)
1990 * flush_domains = CPU
1991 * invalidate_domains = RENDER
1992 * clflush (obj)
1993 * MI_FLUSH
1994 * drm_agp_chipset_flush
1995 */
Keith Packardc0d90822008-11-20 23:11:08 -08001996static void
1997i915_gem_object_set_to_gpu_domain(struct drm_gem_object *obj,
1998 uint32_t read_domains,
1999 uint32_t write_domain)
Eric Anholt673a3942008-07-30 12:06:12 -07002000{
2001 struct drm_device *dev = obj->dev;
2002 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2003 uint32_t invalidate_domains = 0;
2004 uint32_t flush_domains = 0;
Eric Anholte47c68e2008-11-14 13:35:19 -08002005
2006 BUG_ON(read_domains & I915_GEM_DOMAIN_CPU);
2007 BUG_ON(write_domain == I915_GEM_DOMAIN_CPU);
Eric Anholt673a3942008-07-30 12:06:12 -07002008
2009#if WATCH_BUF
2010 DRM_INFO("%s: object %p read %08x -> %08x write %08x -> %08x\n",
2011 __func__, obj,
2012 obj->read_domains, read_domains,
2013 obj->write_domain, write_domain);
2014#endif
2015 /*
2016 * If the object isn't moving to a new write domain,
2017 * let the object stay in multiple read domains
2018 */
2019 if (write_domain == 0)
2020 read_domains |= obj->read_domains;
2021 else
2022 obj_priv->dirty = 1;
2023
2024 /*
2025 * Flush the current write domain if
2026 * the new read domains don't match. Invalidate
2027 * any read domains which differ from the old
2028 * write domain
2029 */
2030 if (obj->write_domain && obj->write_domain != read_domains) {
2031 flush_domains |= obj->write_domain;
2032 invalidate_domains |= read_domains & ~obj->write_domain;
2033 }
2034 /*
2035 * Invalidate any read caches which may have
2036 * stale data. That is, any new read domains.
2037 */
2038 invalidate_domains |= read_domains & ~obj->read_domains;
2039 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU) {
2040#if WATCH_BUF
2041 DRM_INFO("%s: CPU domain flush %08x invalidate %08x\n",
2042 __func__, flush_domains, invalidate_domains);
2043#endif
Eric Anholt673a3942008-07-30 12:06:12 -07002044 i915_gem_clflush_object(obj);
2045 }
2046
2047 if ((write_domain | flush_domains) != 0)
2048 obj->write_domain = write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07002049 obj->read_domains = read_domains;
2050
2051 dev->invalidate_domains |= invalidate_domains;
2052 dev->flush_domains |= flush_domains;
2053#if WATCH_BUF
2054 DRM_INFO("%s: read %08x write %08x invalidate %08x flush %08x\n",
2055 __func__,
2056 obj->read_domains, obj->write_domain,
2057 dev->invalidate_domains, dev->flush_domains);
2058#endif
Eric Anholt673a3942008-07-30 12:06:12 -07002059}
2060
2061/**
Eric Anholte47c68e2008-11-14 13:35:19 -08002062 * Moves the object from a partially CPU read to a full one.
Eric Anholt673a3942008-07-30 12:06:12 -07002063 *
Eric Anholte47c68e2008-11-14 13:35:19 -08002064 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
2065 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
2066 */
2067static void
2068i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj)
2069{
2070 struct drm_device *dev = obj->dev;
2071 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2072
2073 if (!obj_priv->page_cpu_valid)
2074 return;
2075
2076 /* If we're partially in the CPU read domain, finish moving it in.
2077 */
2078 if (obj->read_domains & I915_GEM_DOMAIN_CPU) {
2079 int i;
2080
2081 for (i = 0; i <= (obj->size - 1) / PAGE_SIZE; i++) {
2082 if (obj_priv->page_cpu_valid[i])
2083 continue;
2084 drm_clflush_pages(obj_priv->page_list + i, 1);
2085 }
2086 drm_agp_chipset_flush(dev);
2087 }
2088
2089 /* Free the page_cpu_valid mappings which are now stale, whether
2090 * or not we've got I915_GEM_DOMAIN_CPU.
2091 */
2092 drm_free(obj_priv->page_cpu_valid, obj->size / PAGE_SIZE,
2093 DRM_MEM_DRIVER);
2094 obj_priv->page_cpu_valid = NULL;
2095}
2096
2097/**
2098 * Set the CPU read domain on a range of the object.
2099 *
2100 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
2101 * not entirely valid. The page_cpu_valid member of the object flags which
2102 * pages have been flushed, and will be respected by
2103 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
2104 * of the whole object.
2105 *
2106 * This function returns when the move is complete, including waiting on
2107 * flushes to occur.
Eric Anholt673a3942008-07-30 12:06:12 -07002108 */
2109static int
Eric Anholte47c68e2008-11-14 13:35:19 -08002110i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
2111 uint64_t offset, uint64_t size)
Eric Anholt673a3942008-07-30 12:06:12 -07002112{
2113 struct drm_i915_gem_object *obj_priv = obj->driver_private;
Eric Anholte47c68e2008-11-14 13:35:19 -08002114 int i, ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002115
Eric Anholte47c68e2008-11-14 13:35:19 -08002116 if (offset == 0 && size == obj->size)
2117 return i915_gem_object_set_to_cpu_domain(obj, 0);
2118
2119 i915_gem_object_flush_gpu_write_domain(obj);
2120 /* Wait on any GPU rendering and flushing to occur. */
2121 ret = i915_gem_object_wait_rendering(obj);
2122 if (ret != 0)
2123 return ret;
2124 i915_gem_object_flush_gtt_write_domain(obj);
2125
2126 /* If we're already fully in the CPU read domain, we're done. */
2127 if (obj_priv->page_cpu_valid == NULL &&
2128 (obj->read_domains & I915_GEM_DOMAIN_CPU) != 0)
Eric Anholt673a3942008-07-30 12:06:12 -07002129 return 0;
2130
Eric Anholte47c68e2008-11-14 13:35:19 -08002131 /* Otherwise, create/clear the per-page CPU read domain flag if we're
2132 * newly adding I915_GEM_DOMAIN_CPU
2133 */
Eric Anholt673a3942008-07-30 12:06:12 -07002134 if (obj_priv->page_cpu_valid == NULL) {
2135 obj_priv->page_cpu_valid = drm_calloc(1, obj->size / PAGE_SIZE,
2136 DRM_MEM_DRIVER);
Eric Anholte47c68e2008-11-14 13:35:19 -08002137 if (obj_priv->page_cpu_valid == NULL)
2138 return -ENOMEM;
2139 } else if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0)
2140 memset(obj_priv->page_cpu_valid, 0, obj->size / PAGE_SIZE);
Eric Anholt673a3942008-07-30 12:06:12 -07002141
2142 /* Flush the cache on any pages that are still invalid from the CPU's
2143 * perspective.
2144 */
Eric Anholte47c68e2008-11-14 13:35:19 -08002145 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
2146 i++) {
Eric Anholt673a3942008-07-30 12:06:12 -07002147 if (obj_priv->page_cpu_valid[i])
2148 continue;
2149
2150 drm_clflush_pages(obj_priv->page_list + i, 1);
2151
2152 obj_priv->page_cpu_valid[i] = 1;
2153 }
2154
Eric Anholte47c68e2008-11-14 13:35:19 -08002155 /* It should now be out of any other write domains, and we can update
2156 * the domain values for our changes.
2157 */
2158 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
2159
2160 obj->read_domains |= I915_GEM_DOMAIN_CPU;
2161
Eric Anholt673a3942008-07-30 12:06:12 -07002162 return 0;
2163}
2164
2165/**
Eric Anholt673a3942008-07-30 12:06:12 -07002166 * Pin an object to the GTT and evaluate the relocations landing in it.
2167 */
2168static int
2169i915_gem_object_pin_and_relocate(struct drm_gem_object *obj,
2170 struct drm_file *file_priv,
2171 struct drm_i915_gem_exec_object *entry)
2172{
2173 struct drm_device *dev = obj->dev;
Keith Packard0839ccb2008-10-30 19:38:48 -07002174 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07002175 struct drm_i915_gem_relocation_entry reloc;
2176 struct drm_i915_gem_relocation_entry __user *relocs;
2177 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2178 int i, ret;
Keith Packard0839ccb2008-10-30 19:38:48 -07002179 void __iomem *reloc_page;
Eric Anholt673a3942008-07-30 12:06:12 -07002180
2181 /* Choose the GTT offset for our buffer and put it there. */
2182 ret = i915_gem_object_pin(obj, (uint32_t) entry->alignment);
2183 if (ret)
2184 return ret;
2185
2186 entry->offset = obj_priv->gtt_offset;
2187
2188 relocs = (struct drm_i915_gem_relocation_entry __user *)
2189 (uintptr_t) entry->relocs_ptr;
2190 /* Apply the relocations, using the GTT aperture to avoid cache
2191 * flushing requirements.
2192 */
2193 for (i = 0; i < entry->relocation_count; i++) {
2194 struct drm_gem_object *target_obj;
2195 struct drm_i915_gem_object *target_obj_priv;
Eric Anholt3043c602008-10-02 12:24:47 -07002196 uint32_t reloc_val, reloc_offset;
2197 uint32_t __iomem *reloc_entry;
Eric Anholt673a3942008-07-30 12:06:12 -07002198
2199 ret = copy_from_user(&reloc, relocs + i, sizeof(reloc));
2200 if (ret != 0) {
2201 i915_gem_object_unpin(obj);
2202 return ret;
2203 }
2204
2205 target_obj = drm_gem_object_lookup(obj->dev, file_priv,
2206 reloc.target_handle);
2207 if (target_obj == NULL) {
2208 i915_gem_object_unpin(obj);
2209 return -EBADF;
2210 }
2211 target_obj_priv = target_obj->driver_private;
2212
2213 /* The target buffer should have appeared before us in the
2214 * exec_object list, so it should have a GTT space bound by now.
2215 */
2216 if (target_obj_priv->gtt_space == NULL) {
2217 DRM_ERROR("No GTT space found for object %d\n",
2218 reloc.target_handle);
2219 drm_gem_object_unreference(target_obj);
2220 i915_gem_object_unpin(obj);
2221 return -EINVAL;
2222 }
2223
2224 if (reloc.offset > obj->size - 4) {
2225 DRM_ERROR("Relocation beyond object bounds: "
2226 "obj %p target %d offset %d size %d.\n",
2227 obj, reloc.target_handle,
2228 (int) reloc.offset, (int) obj->size);
2229 drm_gem_object_unreference(target_obj);
2230 i915_gem_object_unpin(obj);
2231 return -EINVAL;
2232 }
2233 if (reloc.offset & 3) {
2234 DRM_ERROR("Relocation not 4-byte aligned: "
2235 "obj %p target %d offset %d.\n",
2236 obj, reloc.target_handle,
2237 (int) reloc.offset);
2238 drm_gem_object_unreference(target_obj);
2239 i915_gem_object_unpin(obj);
2240 return -EINVAL;
2241 }
2242
Eric Anholte47c68e2008-11-14 13:35:19 -08002243 if (reloc.write_domain & I915_GEM_DOMAIN_CPU ||
2244 reloc.read_domains & I915_GEM_DOMAIN_CPU) {
2245 DRM_ERROR("reloc with read/write CPU domains: "
2246 "obj %p target %d offset %d "
2247 "read %08x write %08x",
2248 obj, reloc.target_handle,
2249 (int) reloc.offset,
2250 reloc.read_domains,
2251 reloc.write_domain);
2252 return -EINVAL;
2253 }
2254
Eric Anholt673a3942008-07-30 12:06:12 -07002255 if (reloc.write_domain && target_obj->pending_write_domain &&
2256 reloc.write_domain != target_obj->pending_write_domain) {
2257 DRM_ERROR("Write domain conflict: "
2258 "obj %p target %d offset %d "
2259 "new %08x old %08x\n",
2260 obj, reloc.target_handle,
2261 (int) reloc.offset,
2262 reloc.write_domain,
2263 target_obj->pending_write_domain);
2264 drm_gem_object_unreference(target_obj);
2265 i915_gem_object_unpin(obj);
2266 return -EINVAL;
2267 }
2268
2269#if WATCH_RELOC
2270 DRM_INFO("%s: obj %p offset %08x target %d "
2271 "read %08x write %08x gtt %08x "
2272 "presumed %08x delta %08x\n",
2273 __func__,
2274 obj,
2275 (int) reloc.offset,
2276 (int) reloc.target_handle,
2277 (int) reloc.read_domains,
2278 (int) reloc.write_domain,
2279 (int) target_obj_priv->gtt_offset,
2280 (int) reloc.presumed_offset,
2281 reloc.delta);
2282#endif
2283
2284 target_obj->pending_read_domains |= reloc.read_domains;
2285 target_obj->pending_write_domain |= reloc.write_domain;
2286
2287 /* If the relocation already has the right value in it, no
2288 * more work needs to be done.
2289 */
2290 if (target_obj_priv->gtt_offset == reloc.presumed_offset) {
2291 drm_gem_object_unreference(target_obj);
2292 continue;
2293 }
2294
Eric Anholt2ef7eea2008-11-10 10:53:25 -08002295 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
2296 if (ret != 0) {
2297 drm_gem_object_unreference(target_obj);
2298 i915_gem_object_unpin(obj);
2299 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -07002300 }
2301
2302 /* Map the page containing the relocation we're going to
2303 * perform.
2304 */
2305 reloc_offset = obj_priv->gtt_offset + reloc.offset;
Keith Packard0839ccb2008-10-30 19:38:48 -07002306 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
2307 (reloc_offset &
2308 ~(PAGE_SIZE - 1)));
Eric Anholt3043c602008-10-02 12:24:47 -07002309 reloc_entry = (uint32_t __iomem *)(reloc_page +
Keith Packard0839ccb2008-10-30 19:38:48 -07002310 (reloc_offset & (PAGE_SIZE - 1)));
Eric Anholt673a3942008-07-30 12:06:12 -07002311 reloc_val = target_obj_priv->gtt_offset + reloc.delta;
2312
2313#if WATCH_BUF
2314 DRM_INFO("Applied relocation: %p@0x%08x %08x -> %08x\n",
2315 obj, (unsigned int) reloc.offset,
2316 readl(reloc_entry), reloc_val);
2317#endif
2318 writel(reloc_val, reloc_entry);
Keith Packard0839ccb2008-10-30 19:38:48 -07002319 io_mapping_unmap_atomic(reloc_page);
Eric Anholt673a3942008-07-30 12:06:12 -07002320
2321 /* Write the updated presumed offset for this entry back out
2322 * to the user.
2323 */
2324 reloc.presumed_offset = target_obj_priv->gtt_offset;
2325 ret = copy_to_user(relocs + i, &reloc, sizeof(reloc));
2326 if (ret != 0) {
2327 drm_gem_object_unreference(target_obj);
2328 i915_gem_object_unpin(obj);
2329 return ret;
2330 }
2331
2332 drm_gem_object_unreference(target_obj);
2333 }
2334
Eric Anholt673a3942008-07-30 12:06:12 -07002335#if WATCH_BUF
2336 if (0)
2337 i915_gem_dump_object(obj, 128, __func__, ~0);
2338#endif
2339 return 0;
2340}
2341
2342/** Dispatch a batchbuffer to the ring
2343 */
2344static int
2345i915_dispatch_gem_execbuffer(struct drm_device *dev,
2346 struct drm_i915_gem_execbuffer *exec,
2347 uint64_t exec_offset)
2348{
2349 drm_i915_private_t *dev_priv = dev->dev_private;
2350 struct drm_clip_rect __user *boxes = (struct drm_clip_rect __user *)
2351 (uintptr_t) exec->cliprects_ptr;
2352 int nbox = exec->num_cliprects;
2353 int i = 0, count;
2354 uint32_t exec_start, exec_len;
2355 RING_LOCALS;
2356
2357 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
2358 exec_len = (uint32_t) exec->batch_len;
2359
2360 if ((exec_start | exec_len) & 0x7) {
2361 DRM_ERROR("alignment\n");
2362 return -EINVAL;
2363 }
2364
2365 if (!exec_start)
2366 return -EINVAL;
2367
2368 count = nbox ? nbox : 1;
2369
2370 for (i = 0; i < count; i++) {
2371 if (i < nbox) {
2372 int ret = i915_emit_box(dev, boxes, i,
2373 exec->DR1, exec->DR4);
2374 if (ret)
2375 return ret;
2376 }
2377
2378 if (IS_I830(dev) || IS_845G(dev)) {
2379 BEGIN_LP_RING(4);
2380 OUT_RING(MI_BATCH_BUFFER);
2381 OUT_RING(exec_start | MI_BATCH_NON_SECURE);
2382 OUT_RING(exec_start + exec_len - 4);
2383 OUT_RING(0);
2384 ADVANCE_LP_RING();
2385 } else {
2386 BEGIN_LP_RING(2);
2387 if (IS_I965G(dev)) {
2388 OUT_RING(MI_BATCH_BUFFER_START |
2389 (2 << 6) |
2390 MI_BATCH_NON_SECURE_I965);
2391 OUT_RING(exec_start);
2392 } else {
2393 OUT_RING(MI_BATCH_BUFFER_START |
2394 (2 << 6));
2395 OUT_RING(exec_start | MI_BATCH_NON_SECURE);
2396 }
2397 ADVANCE_LP_RING();
2398 }
2399 }
2400
2401 /* XXX breadcrumb */
2402 return 0;
2403}
2404
2405/* Throttle our rendering by waiting until the ring has completed our requests
2406 * emitted over 20 msec ago.
2407 *
2408 * This should get us reasonable parallelism between CPU and GPU but also
2409 * relatively low latency when blocking on a particular request to finish.
2410 */
2411static int
2412i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file_priv)
2413{
2414 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
2415 int ret = 0;
2416 uint32_t seqno;
2417
2418 mutex_lock(&dev->struct_mutex);
2419 seqno = i915_file_priv->mm.last_gem_throttle_seqno;
2420 i915_file_priv->mm.last_gem_throttle_seqno =
2421 i915_file_priv->mm.last_gem_seqno;
2422 if (seqno)
2423 ret = i915_wait_request(dev, seqno);
2424 mutex_unlock(&dev->struct_mutex);
2425 return ret;
2426}
2427
2428int
2429i915_gem_execbuffer(struct drm_device *dev, void *data,
2430 struct drm_file *file_priv)
2431{
2432 drm_i915_private_t *dev_priv = dev->dev_private;
2433 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
2434 struct drm_i915_gem_execbuffer *args = data;
2435 struct drm_i915_gem_exec_object *exec_list = NULL;
2436 struct drm_gem_object **object_list = NULL;
2437 struct drm_gem_object *batch_obj;
2438 int ret, i, pinned = 0;
2439 uint64_t exec_offset;
2440 uint32_t seqno, flush_domains;
Keith Packardac94a962008-11-20 23:30:27 -08002441 int pin_tries;
Eric Anholt673a3942008-07-30 12:06:12 -07002442
2443#if WATCH_EXEC
2444 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
2445 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
2446#endif
2447
Eric Anholt4f481ed2008-09-10 14:22:49 -07002448 if (args->buffer_count < 1) {
2449 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
2450 return -EINVAL;
2451 }
Eric Anholt673a3942008-07-30 12:06:12 -07002452 /* Copy in the exec list from userland */
2453 exec_list = drm_calloc(sizeof(*exec_list), args->buffer_count,
2454 DRM_MEM_DRIVER);
2455 object_list = drm_calloc(sizeof(*object_list), args->buffer_count,
2456 DRM_MEM_DRIVER);
2457 if (exec_list == NULL || object_list == NULL) {
2458 DRM_ERROR("Failed to allocate exec or object list "
2459 "for %d buffers\n",
2460 args->buffer_count);
2461 ret = -ENOMEM;
2462 goto pre_mutex_err;
2463 }
2464 ret = copy_from_user(exec_list,
2465 (struct drm_i915_relocation_entry __user *)
2466 (uintptr_t) args->buffers_ptr,
2467 sizeof(*exec_list) * args->buffer_count);
2468 if (ret != 0) {
2469 DRM_ERROR("copy %d exec entries failed %d\n",
2470 args->buffer_count, ret);
2471 goto pre_mutex_err;
2472 }
2473
2474 mutex_lock(&dev->struct_mutex);
2475
2476 i915_verify_inactive(dev, __FILE__, __LINE__);
2477
2478 if (dev_priv->mm.wedged) {
2479 DRM_ERROR("Execbuf while wedged\n");
2480 mutex_unlock(&dev->struct_mutex);
2481 return -EIO;
2482 }
2483
2484 if (dev_priv->mm.suspended) {
2485 DRM_ERROR("Execbuf while VT-switched.\n");
2486 mutex_unlock(&dev->struct_mutex);
2487 return -EBUSY;
2488 }
2489
Keith Packardac94a962008-11-20 23:30:27 -08002490 /* Look up object handles */
Eric Anholt673a3942008-07-30 12:06:12 -07002491 for (i = 0; i < args->buffer_count; i++) {
2492 object_list[i] = drm_gem_object_lookup(dev, file_priv,
2493 exec_list[i].handle);
2494 if (object_list[i] == NULL) {
2495 DRM_ERROR("Invalid object handle %d at index %d\n",
2496 exec_list[i].handle, i);
2497 ret = -EBADF;
2498 goto err;
2499 }
Keith Packardac94a962008-11-20 23:30:27 -08002500 }
Eric Anholt673a3942008-07-30 12:06:12 -07002501
Keith Packardac94a962008-11-20 23:30:27 -08002502 /* Pin and relocate */
2503 for (pin_tries = 0; ; pin_tries++) {
2504 ret = 0;
2505 for (i = 0; i < args->buffer_count; i++) {
2506 object_list[i]->pending_read_domains = 0;
2507 object_list[i]->pending_write_domain = 0;
2508 ret = i915_gem_object_pin_and_relocate(object_list[i],
2509 file_priv,
2510 &exec_list[i]);
2511 if (ret)
2512 break;
2513 pinned = i + 1;
2514 }
2515 /* success */
2516 if (ret == 0)
2517 break;
2518
2519 /* error other than GTT full, or we've already tried again */
2520 if (ret != -ENOMEM || pin_tries >= 1) {
Eric Anholtf1acec92008-12-19 14:47:48 -08002521 if (ret != -ERESTARTSYS)
2522 DRM_ERROR("Failed to pin buffers %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07002523 goto err;
2524 }
Keith Packardac94a962008-11-20 23:30:27 -08002525
2526 /* unpin all of our buffers */
2527 for (i = 0; i < pinned; i++)
2528 i915_gem_object_unpin(object_list[i]);
Eric Anholtb1177632008-12-10 10:09:41 -08002529 pinned = 0;
Keith Packardac94a962008-11-20 23:30:27 -08002530
2531 /* evict everyone we can from the aperture */
2532 ret = i915_gem_evict_everything(dev);
2533 if (ret)
2534 goto err;
Eric Anholt673a3942008-07-30 12:06:12 -07002535 }
2536
2537 /* Set the pending read domains for the batch buffer to COMMAND */
2538 batch_obj = object_list[args->buffer_count-1];
2539 batch_obj->pending_read_domains = I915_GEM_DOMAIN_COMMAND;
2540 batch_obj->pending_write_domain = 0;
2541
2542 i915_verify_inactive(dev, __FILE__, __LINE__);
2543
Keith Packard646f0f62008-11-20 23:23:03 -08002544 /* Zero the global flush/invalidate flags. These
2545 * will be modified as new domains are computed
2546 * for each object
2547 */
2548 dev->invalidate_domains = 0;
2549 dev->flush_domains = 0;
2550
Eric Anholt673a3942008-07-30 12:06:12 -07002551 for (i = 0; i < args->buffer_count; i++) {
2552 struct drm_gem_object *obj = object_list[i];
Eric Anholt673a3942008-07-30 12:06:12 -07002553
Keith Packard646f0f62008-11-20 23:23:03 -08002554 /* Compute new gpu domains and update invalidate/flush */
Keith Packardc0d90822008-11-20 23:11:08 -08002555 i915_gem_object_set_to_gpu_domain(obj,
2556 obj->pending_read_domains,
2557 obj->pending_write_domain);
Eric Anholt673a3942008-07-30 12:06:12 -07002558 }
2559
2560 i915_verify_inactive(dev, __FILE__, __LINE__);
2561
Keith Packard646f0f62008-11-20 23:23:03 -08002562 if (dev->invalidate_domains | dev->flush_domains) {
2563#if WATCH_EXEC
2564 DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
2565 __func__,
2566 dev->invalidate_domains,
2567 dev->flush_domains);
2568#endif
2569 i915_gem_flush(dev,
2570 dev->invalidate_domains,
2571 dev->flush_domains);
2572 if (dev->flush_domains)
2573 (void)i915_add_request(dev, dev->flush_domains);
2574 }
Eric Anholt673a3942008-07-30 12:06:12 -07002575
2576 i915_verify_inactive(dev, __FILE__, __LINE__);
2577
2578#if WATCH_COHERENCY
2579 for (i = 0; i < args->buffer_count; i++) {
2580 i915_gem_object_check_coherency(object_list[i],
2581 exec_list[i].handle);
2582 }
2583#endif
2584
2585 exec_offset = exec_list[args->buffer_count - 1].offset;
2586
2587#if WATCH_EXEC
2588 i915_gem_dump_object(object_list[args->buffer_count - 1],
2589 args->batch_len,
2590 __func__,
2591 ~0);
2592#endif
2593
Eric Anholt673a3942008-07-30 12:06:12 -07002594 /* Exec the batchbuffer */
2595 ret = i915_dispatch_gem_execbuffer(dev, args, exec_offset);
2596 if (ret) {
2597 DRM_ERROR("dispatch failed %d\n", ret);
2598 goto err;
2599 }
2600
2601 /*
2602 * Ensure that the commands in the batch buffer are
2603 * finished before the interrupt fires
2604 */
2605 flush_domains = i915_retire_commands(dev);
2606
2607 i915_verify_inactive(dev, __FILE__, __LINE__);
2608
2609 /*
2610 * Get a seqno representing the execution of the current buffer,
2611 * which we can wait on. We would like to mitigate these interrupts,
2612 * likely by only creating seqnos occasionally (so that we have
2613 * *some* interrupts representing completion of buffers that we can
2614 * wait on when trying to clear up gtt space).
2615 */
2616 seqno = i915_add_request(dev, flush_domains);
2617 BUG_ON(seqno == 0);
2618 i915_file_priv->mm.last_gem_seqno = seqno;
2619 for (i = 0; i < args->buffer_count; i++) {
2620 struct drm_gem_object *obj = object_list[i];
Eric Anholt673a3942008-07-30 12:06:12 -07002621
Eric Anholtce44b0e2008-11-06 16:00:31 -08002622 i915_gem_object_move_to_active(obj, seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07002623#if WATCH_LRU
2624 DRM_INFO("%s: move to exec list %p\n", __func__, obj);
2625#endif
2626 }
2627#if WATCH_LRU
2628 i915_dump_lru(dev, __func__);
2629#endif
2630
2631 i915_verify_inactive(dev, __FILE__, __LINE__);
2632
2633 /* Copy the new buffer offsets back to the user's exec list. */
2634 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
2635 (uintptr_t) args->buffers_ptr,
2636 exec_list,
2637 sizeof(*exec_list) * args->buffer_count);
2638 if (ret)
2639 DRM_ERROR("failed to copy %d exec entries "
2640 "back to user (%d)\n",
2641 args->buffer_count, ret);
2642err:
Julia Lawallaad87df2008-12-21 16:28:47 +01002643 for (i = 0; i < pinned; i++)
2644 i915_gem_object_unpin(object_list[i]);
Eric Anholt673a3942008-07-30 12:06:12 -07002645
Julia Lawallaad87df2008-12-21 16:28:47 +01002646 for (i = 0; i < args->buffer_count; i++)
2647 drm_gem_object_unreference(object_list[i]);
2648
Eric Anholt673a3942008-07-30 12:06:12 -07002649 mutex_unlock(&dev->struct_mutex);
2650
2651pre_mutex_err:
2652 drm_free(object_list, sizeof(*object_list) * args->buffer_count,
2653 DRM_MEM_DRIVER);
2654 drm_free(exec_list, sizeof(*exec_list) * args->buffer_count,
2655 DRM_MEM_DRIVER);
2656
2657 return ret;
2658}
2659
2660int
2661i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment)
2662{
2663 struct drm_device *dev = obj->dev;
2664 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2665 int ret;
2666
2667 i915_verify_inactive(dev, __FILE__, __LINE__);
2668 if (obj_priv->gtt_space == NULL) {
2669 ret = i915_gem_object_bind_to_gtt(obj, alignment);
2670 if (ret != 0) {
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08002671 if (ret != -EBUSY && ret != -ERESTARTSYS)
Eric Anholtf1acec92008-12-19 14:47:48 -08002672 DRM_ERROR("Failure to bind: %d", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07002673 return ret;
2674 }
Jesse Barnes0f973f22009-01-26 17:10:45 -08002675 /*
2676 * Pre-965 chips need a fence register set up in order to
2677 * properly handle tiled surfaces.
2678 */
2679 if (!IS_I965G(dev) &&
2680 obj_priv->fence_reg == I915_FENCE_REG_NONE &&
2681 obj_priv->tiling_mode != I915_TILING_NONE)
2682 i915_gem_object_get_fence_reg(obj, true);
Eric Anholt673a3942008-07-30 12:06:12 -07002683 }
2684 obj_priv->pin_count++;
2685
2686 /* If the object is not active and not pending a flush,
2687 * remove it from the inactive list
2688 */
2689 if (obj_priv->pin_count == 1) {
2690 atomic_inc(&dev->pin_count);
2691 atomic_add(obj->size, &dev->pin_memory);
2692 if (!obj_priv->active &&
2693 (obj->write_domain & ~(I915_GEM_DOMAIN_CPU |
2694 I915_GEM_DOMAIN_GTT)) == 0 &&
2695 !list_empty(&obj_priv->list))
2696 list_del_init(&obj_priv->list);
2697 }
2698 i915_verify_inactive(dev, __FILE__, __LINE__);
2699
2700 return 0;
2701}
2702
2703void
2704i915_gem_object_unpin(struct drm_gem_object *obj)
2705{
2706 struct drm_device *dev = obj->dev;
2707 drm_i915_private_t *dev_priv = dev->dev_private;
2708 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2709
2710 i915_verify_inactive(dev, __FILE__, __LINE__);
2711 obj_priv->pin_count--;
2712 BUG_ON(obj_priv->pin_count < 0);
2713 BUG_ON(obj_priv->gtt_space == NULL);
2714
2715 /* If the object is no longer pinned, and is
2716 * neither active nor being flushed, then stick it on
2717 * the inactive list
2718 */
2719 if (obj_priv->pin_count == 0) {
2720 if (!obj_priv->active &&
2721 (obj->write_domain & ~(I915_GEM_DOMAIN_CPU |
2722 I915_GEM_DOMAIN_GTT)) == 0)
2723 list_move_tail(&obj_priv->list,
2724 &dev_priv->mm.inactive_list);
2725 atomic_dec(&dev->pin_count);
2726 atomic_sub(obj->size, &dev->pin_memory);
2727 }
2728 i915_verify_inactive(dev, __FILE__, __LINE__);
2729}
2730
2731int
2732i915_gem_pin_ioctl(struct drm_device *dev, void *data,
2733 struct drm_file *file_priv)
2734{
2735 struct drm_i915_gem_pin *args = data;
2736 struct drm_gem_object *obj;
2737 struct drm_i915_gem_object *obj_priv;
2738 int ret;
2739
2740 mutex_lock(&dev->struct_mutex);
2741
2742 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
2743 if (obj == NULL) {
2744 DRM_ERROR("Bad handle in i915_gem_pin_ioctl(): %d\n",
2745 args->handle);
2746 mutex_unlock(&dev->struct_mutex);
2747 return -EBADF;
2748 }
2749 obj_priv = obj->driver_private;
2750
Jesse Barnes79e53942008-11-07 14:24:08 -08002751 if (obj_priv->pin_filp != NULL && obj_priv->pin_filp != file_priv) {
2752 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
2753 args->handle);
Eric Anholt673a3942008-07-30 12:06:12 -07002754 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08002755 return -EINVAL;
2756 }
2757
2758 obj_priv->user_pin_count++;
2759 obj_priv->pin_filp = file_priv;
2760 if (obj_priv->user_pin_count == 1) {
2761 ret = i915_gem_object_pin(obj, args->alignment);
2762 if (ret != 0) {
2763 drm_gem_object_unreference(obj);
2764 mutex_unlock(&dev->struct_mutex);
2765 return ret;
2766 }
Eric Anholt673a3942008-07-30 12:06:12 -07002767 }
2768
2769 /* XXX - flush the CPU caches for pinned objects
2770 * as the X server doesn't manage domains yet
2771 */
Eric Anholte47c68e2008-11-14 13:35:19 -08002772 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002773 args->offset = obj_priv->gtt_offset;
2774 drm_gem_object_unreference(obj);
2775 mutex_unlock(&dev->struct_mutex);
2776
2777 return 0;
2778}
2779
2780int
2781i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
2782 struct drm_file *file_priv)
2783{
2784 struct drm_i915_gem_pin *args = data;
2785 struct drm_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08002786 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07002787
2788 mutex_lock(&dev->struct_mutex);
2789
2790 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
2791 if (obj == NULL) {
2792 DRM_ERROR("Bad handle in i915_gem_unpin_ioctl(): %d\n",
2793 args->handle);
2794 mutex_unlock(&dev->struct_mutex);
2795 return -EBADF;
2796 }
2797
Jesse Barnes79e53942008-11-07 14:24:08 -08002798 obj_priv = obj->driver_private;
2799 if (obj_priv->pin_filp != file_priv) {
2800 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
2801 args->handle);
2802 drm_gem_object_unreference(obj);
2803 mutex_unlock(&dev->struct_mutex);
2804 return -EINVAL;
2805 }
2806 obj_priv->user_pin_count--;
2807 if (obj_priv->user_pin_count == 0) {
2808 obj_priv->pin_filp = NULL;
2809 i915_gem_object_unpin(obj);
2810 }
Eric Anholt673a3942008-07-30 12:06:12 -07002811
2812 drm_gem_object_unreference(obj);
2813 mutex_unlock(&dev->struct_mutex);
2814 return 0;
2815}
2816
2817int
2818i915_gem_busy_ioctl(struct drm_device *dev, void *data,
2819 struct drm_file *file_priv)
2820{
2821 struct drm_i915_gem_busy *args = data;
2822 struct drm_gem_object *obj;
2823 struct drm_i915_gem_object *obj_priv;
2824
2825 mutex_lock(&dev->struct_mutex);
2826 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
2827 if (obj == NULL) {
2828 DRM_ERROR("Bad handle in i915_gem_busy_ioctl(): %d\n",
2829 args->handle);
2830 mutex_unlock(&dev->struct_mutex);
2831 return -EBADF;
2832 }
2833
2834 obj_priv = obj->driver_private;
Eric Anholtc4de0a52008-12-14 19:05:04 -08002835 /* Don't count being on the flushing list against the object being
2836 * done. Otherwise, a buffer left on the flushing list but not getting
2837 * flushed (because nobody's flushing that domain) won't ever return
2838 * unbusy and get reused by libdrm's bo cache. The other expected
2839 * consumer of this interface, OpenGL's occlusion queries, also specs
2840 * that the objects get unbusy "eventually" without any interference.
2841 */
2842 args->busy = obj_priv->active && obj_priv->last_rendering_seqno != 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002843
2844 drm_gem_object_unreference(obj);
2845 mutex_unlock(&dev->struct_mutex);
2846 return 0;
2847}
2848
2849int
2850i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
2851 struct drm_file *file_priv)
2852{
2853 return i915_gem_ring_throttle(dev, file_priv);
2854}
2855
2856int i915_gem_init_object(struct drm_gem_object *obj)
2857{
2858 struct drm_i915_gem_object *obj_priv;
2859
2860 obj_priv = drm_calloc(1, sizeof(*obj_priv), DRM_MEM_DRIVER);
2861 if (obj_priv == NULL)
2862 return -ENOMEM;
2863
2864 /*
2865 * We've just allocated pages from the kernel,
2866 * so they've just been written by the CPU with
2867 * zeros. They'll need to be clflushed before we
2868 * use them with the GPU.
2869 */
2870 obj->write_domain = I915_GEM_DOMAIN_CPU;
2871 obj->read_domains = I915_GEM_DOMAIN_CPU;
2872
Keith Packardba1eb1d2008-10-14 19:55:10 -07002873 obj_priv->agp_type = AGP_USER_MEMORY;
2874
Eric Anholt673a3942008-07-30 12:06:12 -07002875 obj->driver_private = obj_priv;
2876 obj_priv->obj = obj;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002877 obj_priv->fence_reg = I915_FENCE_REG_NONE;
Eric Anholt673a3942008-07-30 12:06:12 -07002878 INIT_LIST_HEAD(&obj_priv->list);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002879
Eric Anholt673a3942008-07-30 12:06:12 -07002880 return 0;
2881}
2882
2883void i915_gem_free_object(struct drm_gem_object *obj)
2884{
Jesse Barnesde151cf2008-11-12 10:03:55 -08002885 struct drm_device *dev = obj->dev;
2886 struct drm_gem_mm *mm = dev->mm_private;
2887 struct drm_map_list *list;
2888 struct drm_map *map;
Eric Anholt673a3942008-07-30 12:06:12 -07002889 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2890
2891 while (obj_priv->pin_count > 0)
2892 i915_gem_object_unpin(obj);
2893
Dave Airlie71acb5e2008-12-30 20:31:46 +10002894 if (obj_priv->phys_obj)
2895 i915_gem_detach_phys_object(dev, obj);
2896
Eric Anholt673a3942008-07-30 12:06:12 -07002897 i915_gem_object_unbind(obj);
2898
Jesse Barnesde151cf2008-11-12 10:03:55 -08002899 list = &obj->map_list;
2900 drm_ht_remove_item(&mm->offset_hash, &list->hash);
2901
2902 if (list->file_offset_node) {
2903 drm_mm_put_block(list->file_offset_node);
2904 list->file_offset_node = NULL;
2905 }
2906
2907 map = list->map;
2908 if (map) {
2909 drm_free(map, sizeof(*map), DRM_MEM_DRIVER);
2910 list->map = NULL;
2911 }
2912
Eric Anholt673a3942008-07-30 12:06:12 -07002913 drm_free(obj_priv->page_cpu_valid, 1, DRM_MEM_DRIVER);
2914 drm_free(obj->driver_private, 1, DRM_MEM_DRIVER);
2915}
2916
Eric Anholt673a3942008-07-30 12:06:12 -07002917/** Unbinds all objects that are on the given buffer list. */
2918static int
2919i915_gem_evict_from_list(struct drm_device *dev, struct list_head *head)
2920{
2921 struct drm_gem_object *obj;
2922 struct drm_i915_gem_object *obj_priv;
2923 int ret;
2924
2925 while (!list_empty(head)) {
2926 obj_priv = list_first_entry(head,
2927 struct drm_i915_gem_object,
2928 list);
2929 obj = obj_priv->obj;
2930
2931 if (obj_priv->pin_count != 0) {
2932 DRM_ERROR("Pinned object in unbind list\n");
2933 mutex_unlock(&dev->struct_mutex);
2934 return -EINVAL;
2935 }
2936
2937 ret = i915_gem_object_unbind(obj);
2938 if (ret != 0) {
2939 DRM_ERROR("Error unbinding object in LeaveVT: %d\n",
2940 ret);
2941 mutex_unlock(&dev->struct_mutex);
2942 return ret;
2943 }
2944 }
2945
2946
2947 return 0;
2948}
2949
2950static int
2951i915_gem_idle(struct drm_device *dev)
2952{
2953 drm_i915_private_t *dev_priv = dev->dev_private;
2954 uint32_t seqno, cur_seqno, last_seqno;
2955 int stuck, ret;
2956
Keith Packard6dbe2772008-10-14 21:41:13 -07002957 mutex_lock(&dev->struct_mutex);
2958
2959 if (dev_priv->mm.suspended || dev_priv->ring.ring_obj == NULL) {
2960 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07002961 return 0;
Keith Packard6dbe2772008-10-14 21:41:13 -07002962 }
Eric Anholt673a3942008-07-30 12:06:12 -07002963
2964 /* Hack! Don't let anybody do execbuf while we don't control the chip.
2965 * We need to replace this with a semaphore, or something.
2966 */
2967 dev_priv->mm.suspended = 1;
2968
Keith Packard6dbe2772008-10-14 21:41:13 -07002969 /* Cancel the retire work handler, wait for it to finish if running
2970 */
2971 mutex_unlock(&dev->struct_mutex);
2972 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
2973 mutex_lock(&dev->struct_mutex);
2974
Eric Anholt673a3942008-07-30 12:06:12 -07002975 i915_kernel_lost_context(dev);
2976
2977 /* Flush the GPU along with all non-CPU write domains
2978 */
2979 i915_gem_flush(dev, ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT),
2980 ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT));
Jesse Barnesde151cf2008-11-12 10:03:55 -08002981 seqno = i915_add_request(dev, ~I915_GEM_DOMAIN_CPU);
Eric Anholt673a3942008-07-30 12:06:12 -07002982
2983 if (seqno == 0) {
2984 mutex_unlock(&dev->struct_mutex);
2985 return -ENOMEM;
2986 }
2987
2988 dev_priv->mm.waiting_gem_seqno = seqno;
2989 last_seqno = 0;
2990 stuck = 0;
2991 for (;;) {
2992 cur_seqno = i915_get_gem_seqno(dev);
2993 if (i915_seqno_passed(cur_seqno, seqno))
2994 break;
2995 if (last_seqno == cur_seqno) {
2996 if (stuck++ > 100) {
2997 DRM_ERROR("hardware wedged\n");
2998 dev_priv->mm.wedged = 1;
2999 DRM_WAKEUP(&dev_priv->irq_queue);
3000 break;
3001 }
3002 }
3003 msleep(10);
3004 last_seqno = cur_seqno;
3005 }
3006 dev_priv->mm.waiting_gem_seqno = 0;
3007
3008 i915_gem_retire_requests(dev);
3009
Eric Anholt28dfe522008-11-13 15:00:55 -08003010 if (!dev_priv->mm.wedged) {
3011 /* Active and flushing should now be empty as we've
3012 * waited for a sequence higher than any pending execbuffer
3013 */
3014 WARN_ON(!list_empty(&dev_priv->mm.active_list));
3015 WARN_ON(!list_empty(&dev_priv->mm.flushing_list));
3016 /* Request should now be empty as we've also waited
3017 * for the last request in the list
3018 */
3019 WARN_ON(!list_empty(&dev_priv->mm.request_list));
3020 }
Eric Anholt673a3942008-07-30 12:06:12 -07003021
Eric Anholt28dfe522008-11-13 15:00:55 -08003022 /* Empty the active and flushing lists to inactive. If there's
3023 * anything left at this point, it means that we're wedged and
3024 * nothing good's going to happen by leaving them there. So strip
3025 * the GPU domains and just stuff them onto inactive.
Eric Anholt673a3942008-07-30 12:06:12 -07003026 */
Eric Anholt28dfe522008-11-13 15:00:55 -08003027 while (!list_empty(&dev_priv->mm.active_list)) {
3028 struct drm_i915_gem_object *obj_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07003029
Eric Anholt28dfe522008-11-13 15:00:55 -08003030 obj_priv = list_first_entry(&dev_priv->mm.active_list,
3031 struct drm_i915_gem_object,
3032 list);
3033 obj_priv->obj->write_domain &= ~I915_GEM_GPU_DOMAINS;
3034 i915_gem_object_move_to_inactive(obj_priv->obj);
3035 }
3036
3037 while (!list_empty(&dev_priv->mm.flushing_list)) {
3038 struct drm_i915_gem_object *obj_priv;
3039
Eric Anholt151903d2008-12-01 10:23:21 +10003040 obj_priv = list_first_entry(&dev_priv->mm.flushing_list,
Eric Anholt28dfe522008-11-13 15:00:55 -08003041 struct drm_i915_gem_object,
3042 list);
3043 obj_priv->obj->write_domain &= ~I915_GEM_GPU_DOMAINS;
3044 i915_gem_object_move_to_inactive(obj_priv->obj);
3045 }
3046
3047
3048 /* Move all inactive buffers out of the GTT. */
Eric Anholt673a3942008-07-30 12:06:12 -07003049 ret = i915_gem_evict_from_list(dev, &dev_priv->mm.inactive_list);
Eric Anholt28dfe522008-11-13 15:00:55 -08003050 WARN_ON(!list_empty(&dev_priv->mm.inactive_list));
Keith Packard6dbe2772008-10-14 21:41:13 -07003051 if (ret) {
3052 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07003053 return ret;
Keith Packard6dbe2772008-10-14 21:41:13 -07003054 }
Eric Anholt673a3942008-07-30 12:06:12 -07003055
Keith Packard6dbe2772008-10-14 21:41:13 -07003056 i915_gem_cleanup_ringbuffer(dev);
3057 mutex_unlock(&dev->struct_mutex);
3058
Eric Anholt673a3942008-07-30 12:06:12 -07003059 return 0;
3060}
3061
3062static int
3063i915_gem_init_hws(struct drm_device *dev)
3064{
3065 drm_i915_private_t *dev_priv = dev->dev_private;
3066 struct drm_gem_object *obj;
3067 struct drm_i915_gem_object *obj_priv;
3068 int ret;
3069
3070 /* If we need a physical address for the status page, it's already
3071 * initialized at driver load time.
3072 */
3073 if (!I915_NEED_GFX_HWS(dev))
3074 return 0;
3075
3076 obj = drm_gem_object_alloc(dev, 4096);
3077 if (obj == NULL) {
3078 DRM_ERROR("Failed to allocate status page\n");
3079 return -ENOMEM;
3080 }
3081 obj_priv = obj->driver_private;
Keith Packardba1eb1d2008-10-14 19:55:10 -07003082 obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
Eric Anholt673a3942008-07-30 12:06:12 -07003083
3084 ret = i915_gem_object_pin(obj, 4096);
3085 if (ret != 0) {
3086 drm_gem_object_unreference(obj);
3087 return ret;
3088 }
3089
3090 dev_priv->status_gfx_addr = obj_priv->gtt_offset;
Eric Anholt673a3942008-07-30 12:06:12 -07003091
Keith Packardba1eb1d2008-10-14 19:55:10 -07003092 dev_priv->hw_status_page = kmap(obj_priv->page_list[0]);
3093 if (dev_priv->hw_status_page == NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -07003094 DRM_ERROR("Failed to map status page.\n");
3095 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
3096 drm_gem_object_unreference(obj);
3097 return -EINVAL;
3098 }
3099 dev_priv->hws_obj = obj;
Eric Anholt673a3942008-07-30 12:06:12 -07003100 memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
3101 I915_WRITE(HWS_PGA, dev_priv->status_gfx_addr);
Keith Packardba1eb1d2008-10-14 19:55:10 -07003102 I915_READ(HWS_PGA); /* posting read */
Eric Anholt673a3942008-07-30 12:06:12 -07003103 DRM_DEBUG("hws offset: 0x%08x\n", dev_priv->status_gfx_addr);
3104
3105 return 0;
3106}
3107
Jesse Barnes79e53942008-11-07 14:24:08 -08003108int
Eric Anholt673a3942008-07-30 12:06:12 -07003109i915_gem_init_ringbuffer(struct drm_device *dev)
3110{
3111 drm_i915_private_t *dev_priv = dev->dev_private;
3112 struct drm_gem_object *obj;
3113 struct drm_i915_gem_object *obj_priv;
Jesse Barnes79e53942008-11-07 14:24:08 -08003114 drm_i915_ring_buffer_t *ring = &dev_priv->ring;
Eric Anholt673a3942008-07-30 12:06:12 -07003115 int ret;
Keith Packard50aa253d2008-10-14 17:20:35 -07003116 u32 head;
Eric Anholt673a3942008-07-30 12:06:12 -07003117
3118 ret = i915_gem_init_hws(dev);
3119 if (ret != 0)
3120 return ret;
3121
3122 obj = drm_gem_object_alloc(dev, 128 * 1024);
3123 if (obj == NULL) {
3124 DRM_ERROR("Failed to allocate ringbuffer\n");
3125 return -ENOMEM;
3126 }
3127 obj_priv = obj->driver_private;
3128
3129 ret = i915_gem_object_pin(obj, 4096);
3130 if (ret != 0) {
3131 drm_gem_object_unreference(obj);
3132 return ret;
3133 }
3134
3135 /* Set up the kernel mapping for the ring. */
Jesse Barnes79e53942008-11-07 14:24:08 -08003136 ring->Size = obj->size;
3137 ring->tail_mask = obj->size - 1;
Eric Anholt673a3942008-07-30 12:06:12 -07003138
Jesse Barnes79e53942008-11-07 14:24:08 -08003139 ring->map.offset = dev->agp->base + obj_priv->gtt_offset;
3140 ring->map.size = obj->size;
3141 ring->map.type = 0;
3142 ring->map.flags = 0;
3143 ring->map.mtrr = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07003144
Jesse Barnes79e53942008-11-07 14:24:08 -08003145 drm_core_ioremap_wc(&ring->map, dev);
3146 if (ring->map.handle == NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -07003147 DRM_ERROR("Failed to map ringbuffer.\n");
3148 memset(&dev_priv->ring, 0, sizeof(dev_priv->ring));
3149 drm_gem_object_unreference(obj);
3150 return -EINVAL;
3151 }
Jesse Barnes79e53942008-11-07 14:24:08 -08003152 ring->ring_obj = obj;
3153 ring->virtual_start = ring->map.handle;
Eric Anholt673a3942008-07-30 12:06:12 -07003154
3155 /* Stop the ring if it's running. */
3156 I915_WRITE(PRB0_CTL, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07003157 I915_WRITE(PRB0_TAIL, 0);
Keith Packard50aa253d2008-10-14 17:20:35 -07003158 I915_WRITE(PRB0_HEAD, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07003159
3160 /* Initialize the ring. */
3161 I915_WRITE(PRB0_START, obj_priv->gtt_offset);
Keith Packard50aa253d2008-10-14 17:20:35 -07003162 head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
3163
3164 /* G45 ring initialization fails to reset head to zero */
3165 if (head != 0) {
3166 DRM_ERROR("Ring head not reset to zero "
3167 "ctl %08x head %08x tail %08x start %08x\n",
3168 I915_READ(PRB0_CTL),
3169 I915_READ(PRB0_HEAD),
3170 I915_READ(PRB0_TAIL),
3171 I915_READ(PRB0_START));
3172 I915_WRITE(PRB0_HEAD, 0);
3173
3174 DRM_ERROR("Ring head forced to zero "
3175 "ctl %08x head %08x tail %08x start %08x\n",
3176 I915_READ(PRB0_CTL),
3177 I915_READ(PRB0_HEAD),
3178 I915_READ(PRB0_TAIL),
3179 I915_READ(PRB0_START));
3180 }
3181
Eric Anholt673a3942008-07-30 12:06:12 -07003182 I915_WRITE(PRB0_CTL,
3183 ((obj->size - 4096) & RING_NR_PAGES) |
3184 RING_NO_REPORT |
3185 RING_VALID);
3186
Keith Packard50aa253d2008-10-14 17:20:35 -07003187 head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
3188
3189 /* If the head is still not zero, the ring is dead */
3190 if (head != 0) {
3191 DRM_ERROR("Ring initialization failed "
3192 "ctl %08x head %08x tail %08x start %08x\n",
3193 I915_READ(PRB0_CTL),
3194 I915_READ(PRB0_HEAD),
3195 I915_READ(PRB0_TAIL),
3196 I915_READ(PRB0_START));
3197 return -EIO;
3198 }
3199
Eric Anholt673a3942008-07-30 12:06:12 -07003200 /* Update our cache of the ring state */
Jesse Barnes79e53942008-11-07 14:24:08 -08003201 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3202 i915_kernel_lost_context(dev);
3203 else {
3204 ring->head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
3205 ring->tail = I915_READ(PRB0_TAIL) & TAIL_ADDR;
3206 ring->space = ring->head - (ring->tail + 8);
3207 if (ring->space < 0)
3208 ring->space += ring->Size;
3209 }
Eric Anholt673a3942008-07-30 12:06:12 -07003210
3211 return 0;
3212}
3213
Jesse Barnes79e53942008-11-07 14:24:08 -08003214void
Eric Anholt673a3942008-07-30 12:06:12 -07003215i915_gem_cleanup_ringbuffer(struct drm_device *dev)
3216{
3217 drm_i915_private_t *dev_priv = dev->dev_private;
3218
3219 if (dev_priv->ring.ring_obj == NULL)
3220 return;
3221
3222 drm_core_ioremapfree(&dev_priv->ring.map, dev);
3223
3224 i915_gem_object_unpin(dev_priv->ring.ring_obj);
3225 drm_gem_object_unreference(dev_priv->ring.ring_obj);
3226 dev_priv->ring.ring_obj = NULL;
3227 memset(&dev_priv->ring, 0, sizeof(dev_priv->ring));
3228
3229 if (dev_priv->hws_obj != NULL) {
Keith Packardba1eb1d2008-10-14 19:55:10 -07003230 struct drm_gem_object *obj = dev_priv->hws_obj;
3231 struct drm_i915_gem_object *obj_priv = obj->driver_private;
3232
3233 kunmap(obj_priv->page_list[0]);
3234 i915_gem_object_unpin(obj);
3235 drm_gem_object_unreference(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07003236 dev_priv->hws_obj = NULL;
3237 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
Keith Packardba1eb1d2008-10-14 19:55:10 -07003238 dev_priv->hw_status_page = NULL;
Eric Anholt673a3942008-07-30 12:06:12 -07003239
3240 /* Write high address into HWS_PGA when disabling. */
3241 I915_WRITE(HWS_PGA, 0x1ffff000);
3242 }
3243}
3244
3245int
3246i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
3247 struct drm_file *file_priv)
3248{
3249 drm_i915_private_t *dev_priv = dev->dev_private;
3250 int ret;
3251
Jesse Barnes79e53942008-11-07 14:24:08 -08003252 if (drm_core_check_feature(dev, DRIVER_MODESET))
3253 return 0;
3254
Eric Anholt673a3942008-07-30 12:06:12 -07003255 if (dev_priv->mm.wedged) {
3256 DRM_ERROR("Reenabling wedged hardware, good luck\n");
3257 dev_priv->mm.wedged = 0;
3258 }
3259
Eric Anholt673a3942008-07-30 12:06:12 -07003260 mutex_lock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08003261 dev_priv->mm.suspended = 0;
3262
3263 ret = i915_gem_init_ringbuffer(dev);
3264 if (ret != 0)
3265 return ret;
3266
Eric Anholt673a3942008-07-30 12:06:12 -07003267 BUG_ON(!list_empty(&dev_priv->mm.active_list));
3268 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
3269 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
3270 BUG_ON(!list_empty(&dev_priv->mm.request_list));
Eric Anholt673a3942008-07-30 12:06:12 -07003271 mutex_unlock(&dev->struct_mutex);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04003272
3273 drm_irq_install(dev);
3274
Eric Anholt673a3942008-07-30 12:06:12 -07003275 return 0;
3276}
3277
3278int
3279i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
3280 struct drm_file *file_priv)
3281{
3282 int ret;
3283
Jesse Barnes79e53942008-11-07 14:24:08 -08003284 if (drm_core_check_feature(dev, DRIVER_MODESET))
3285 return 0;
3286
Eric Anholt673a3942008-07-30 12:06:12 -07003287 ret = i915_gem_idle(dev);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04003288 drm_irq_uninstall(dev);
3289
Keith Packard6dbe2772008-10-14 21:41:13 -07003290 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003291}
3292
3293void
3294i915_gem_lastclose(struct drm_device *dev)
3295{
3296 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003297
Eric Anholte806b492009-01-22 09:56:58 -08003298 if (drm_core_check_feature(dev, DRIVER_MODESET))
3299 return;
3300
Keith Packard6dbe2772008-10-14 21:41:13 -07003301 ret = i915_gem_idle(dev);
3302 if (ret)
3303 DRM_ERROR("failed to idle hardware: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07003304}
3305
3306void
3307i915_gem_load(struct drm_device *dev)
3308{
3309 drm_i915_private_t *dev_priv = dev->dev_private;
3310
3311 INIT_LIST_HEAD(&dev_priv->mm.active_list);
3312 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
3313 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
3314 INIT_LIST_HEAD(&dev_priv->mm.request_list);
3315 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
3316 i915_gem_retire_work_handler);
Eric Anholt673a3942008-07-30 12:06:12 -07003317 dev_priv->mm.next_gem_seqno = 1;
3318
Jesse Barnesde151cf2008-11-12 10:03:55 -08003319 /* Old X drivers will take 0-2 for front, back, depth buffers */
3320 dev_priv->fence_reg_start = 3;
3321
Jesse Barnes0f973f22009-01-26 17:10:45 -08003322 if (IS_I965G(dev) || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08003323 dev_priv->num_fence_regs = 16;
3324 else
3325 dev_priv->num_fence_regs = 8;
3326
Eric Anholt673a3942008-07-30 12:06:12 -07003327 i915_gem_detect_bit_6_swizzle(dev);
3328}
Dave Airlie71acb5e2008-12-30 20:31:46 +10003329
3330/*
3331 * Create a physically contiguous memory object for this object
3332 * e.g. for cursor + overlay regs
3333 */
3334int i915_gem_init_phys_object(struct drm_device *dev,
3335 int id, int size)
3336{
3337 drm_i915_private_t *dev_priv = dev->dev_private;
3338 struct drm_i915_gem_phys_object *phys_obj;
3339 int ret;
3340
3341 if (dev_priv->mm.phys_objs[id - 1] || !size)
3342 return 0;
3343
3344 phys_obj = drm_calloc(1, sizeof(struct drm_i915_gem_phys_object), DRM_MEM_DRIVER);
3345 if (!phys_obj)
3346 return -ENOMEM;
3347
3348 phys_obj->id = id;
3349
3350 phys_obj->handle = drm_pci_alloc(dev, size, 0, 0xffffffff);
3351 if (!phys_obj->handle) {
3352 ret = -ENOMEM;
3353 goto kfree_obj;
3354 }
3355#ifdef CONFIG_X86
3356 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
3357#endif
3358
3359 dev_priv->mm.phys_objs[id - 1] = phys_obj;
3360
3361 return 0;
3362kfree_obj:
3363 drm_free(phys_obj, sizeof(struct drm_i915_gem_phys_object), DRM_MEM_DRIVER);
3364 return ret;
3365}
3366
3367void i915_gem_free_phys_object(struct drm_device *dev, int id)
3368{
3369 drm_i915_private_t *dev_priv = dev->dev_private;
3370 struct drm_i915_gem_phys_object *phys_obj;
3371
3372 if (!dev_priv->mm.phys_objs[id - 1])
3373 return;
3374
3375 phys_obj = dev_priv->mm.phys_objs[id - 1];
3376 if (phys_obj->cur_obj) {
3377 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
3378 }
3379
3380#ifdef CONFIG_X86
3381 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
3382#endif
3383 drm_pci_free(dev, phys_obj->handle);
3384 kfree(phys_obj);
3385 dev_priv->mm.phys_objs[id - 1] = NULL;
3386}
3387
3388void i915_gem_free_all_phys_object(struct drm_device *dev)
3389{
3390 int i;
3391
Dave Airlie260883c2009-01-22 17:58:49 +10003392 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
Dave Airlie71acb5e2008-12-30 20:31:46 +10003393 i915_gem_free_phys_object(dev, i);
3394}
3395
3396void i915_gem_detach_phys_object(struct drm_device *dev,
3397 struct drm_gem_object *obj)
3398{
3399 struct drm_i915_gem_object *obj_priv;
3400 int i;
3401 int ret;
3402 int page_count;
3403
3404 obj_priv = obj->driver_private;
3405 if (!obj_priv->phys_obj)
3406 return;
3407
3408 ret = i915_gem_object_get_page_list(obj);
3409 if (ret)
3410 goto out;
3411
3412 page_count = obj->size / PAGE_SIZE;
3413
3414 for (i = 0; i < page_count; i++) {
3415 char *dst = kmap_atomic(obj_priv->page_list[i], KM_USER0);
3416 char *src = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
3417
3418 memcpy(dst, src, PAGE_SIZE);
3419 kunmap_atomic(dst, KM_USER0);
3420 }
3421 drm_clflush_pages(obj_priv->page_list, page_count);
3422 drm_agp_chipset_flush(dev);
3423out:
3424 obj_priv->phys_obj->cur_obj = NULL;
3425 obj_priv->phys_obj = NULL;
3426}
3427
3428int
3429i915_gem_attach_phys_object(struct drm_device *dev,
3430 struct drm_gem_object *obj, int id)
3431{
3432 drm_i915_private_t *dev_priv = dev->dev_private;
3433 struct drm_i915_gem_object *obj_priv;
3434 int ret = 0;
3435 int page_count;
3436 int i;
3437
3438 if (id > I915_MAX_PHYS_OBJECT)
3439 return -EINVAL;
3440
3441 obj_priv = obj->driver_private;
3442
3443 if (obj_priv->phys_obj) {
3444 if (obj_priv->phys_obj->id == id)
3445 return 0;
3446 i915_gem_detach_phys_object(dev, obj);
3447 }
3448
3449
3450 /* create a new object */
3451 if (!dev_priv->mm.phys_objs[id - 1]) {
3452 ret = i915_gem_init_phys_object(dev, id,
3453 obj->size);
3454 if (ret) {
Linus Torvaldsaeb565d2009-01-26 10:01:53 -08003455 DRM_ERROR("failed to init phys object %d size: %zu\n", id, obj->size);
Dave Airlie71acb5e2008-12-30 20:31:46 +10003456 goto out;
3457 }
3458 }
3459
3460 /* bind to the object */
3461 obj_priv->phys_obj = dev_priv->mm.phys_objs[id - 1];
3462 obj_priv->phys_obj->cur_obj = obj;
3463
3464 ret = i915_gem_object_get_page_list(obj);
3465 if (ret) {
3466 DRM_ERROR("failed to get page list\n");
3467 goto out;
3468 }
3469
3470 page_count = obj->size / PAGE_SIZE;
3471
3472 for (i = 0; i < page_count; i++) {
3473 char *src = kmap_atomic(obj_priv->page_list[i], KM_USER0);
3474 char *dst = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
3475
3476 memcpy(dst, src, PAGE_SIZE);
3477 kunmap_atomic(src, KM_USER0);
3478 }
3479
3480 return 0;
3481out:
3482 return ret;
3483}
3484
3485static int
3486i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
3487 struct drm_i915_gem_pwrite *args,
3488 struct drm_file *file_priv)
3489{
3490 struct drm_i915_gem_object *obj_priv = obj->driver_private;
3491 void *obj_addr;
3492 int ret;
3493 char __user *user_data;
3494
3495 user_data = (char __user *) (uintptr_t) args->data_ptr;
3496 obj_addr = obj_priv->phys_obj->handle->vaddr + args->offset;
3497
3498 DRM_ERROR("obj_addr %p, %lld\n", obj_addr, args->size);
3499 ret = copy_from_user(obj_addr, user_data, args->size);
3500 if (ret)
3501 return -EFAULT;
3502
3503 drm_agp_chipset_flush(dev);
3504 return 0;
3505}